blob: 5034cc0330a16e1e87bd563b48aad207ec382d81 [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);
Michael Liao6c0e04c2012-10-15 22:39:43 +0000460 // NOTE: EH_SJLJ_SETJMP/_LONGJMP supported here is NOT intened to support
461 // SjLj exception handling but a light-weight setjmp/longjmp replacement to
Michael Liao281ae5a2012-10-17 02:22:27 +0000462 // support continuation, user-level threading, and etc.. As a result, no
Michael Liao6c0e04c2012-10-15 22:39:43 +0000463 // other SjLj exception interfaces are implemented and please don't build
464 // your own exception handling based on them.
465 // LLVM/Clang supports zero-cost DWARF exception handling.
466 setOperationAction(ISD::EH_SJLJ_SETJMP, MVT::i32, Custom);
467 setOperationAction(ISD::EH_SJLJ_LONGJMP, MVT::Other, Custom);
Anton Korobeynikov2365f512007-07-14 14:06:15 +0000468
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000469 // Darwin ABI issue.
Owen Anderson825b72b2009-08-11 20:47:22 +0000470 setOperationAction(ISD::ConstantPool , MVT::i32 , Custom);
471 setOperationAction(ISD::JumpTable , MVT::i32 , Custom);
472 setOperationAction(ISD::GlobalAddress , MVT::i32 , Custom);
473 setOperationAction(ISD::GlobalTLSAddress, MVT::i32 , Custom);
Anton Korobeynikov6625eff2008-05-04 21:36:32 +0000474 if (Subtarget->is64Bit())
Owen Anderson825b72b2009-08-11 20:47:22 +0000475 setOperationAction(ISD::GlobalTLSAddress, MVT::i64, Custom);
476 setOperationAction(ISD::ExternalSymbol , MVT::i32 , Custom);
Dan Gohmanf705adb2009-10-30 01:28:02 +0000477 setOperationAction(ISD::BlockAddress , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000478 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000479 setOperationAction(ISD::ConstantPool , MVT::i64 , Custom);
480 setOperationAction(ISD::JumpTable , MVT::i64 , Custom);
481 setOperationAction(ISD::GlobalAddress , MVT::i64 , Custom);
482 setOperationAction(ISD::ExternalSymbol, MVT::i64 , Custom);
Dan Gohmanf705adb2009-10-30 01:28:02 +0000483 setOperationAction(ISD::BlockAddress , MVT::i64 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000484 }
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000485 // 64-bit addm sub, shl, sra, srl (iff 32-bit x86)
Owen Anderson825b72b2009-08-11 20:47:22 +0000486 setOperationAction(ISD::SHL_PARTS , MVT::i32 , Custom);
487 setOperationAction(ISD::SRA_PARTS , MVT::i32 , Custom);
488 setOperationAction(ISD::SRL_PARTS , MVT::i32 , Custom);
Dan Gohman4c1fa612008-03-03 22:22:09 +0000489 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000490 setOperationAction(ISD::SHL_PARTS , MVT::i64 , Custom);
491 setOperationAction(ISD::SRA_PARTS , MVT::i64 , Custom);
492 setOperationAction(ISD::SRL_PARTS , MVT::i64 , Custom);
Dan Gohman4c1fa612008-03-03 22:22:09 +0000493 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000494
Craig Topper1accb7e2012-01-10 06:54:16 +0000495 if (Subtarget->hasSSE1())
Owen Anderson825b72b2009-08-11 20:47:22 +0000496 setOperationAction(ISD::PREFETCH , MVT::Other, Legal);
Evan Cheng27b7db52008-03-08 00:58:38 +0000497
Eric Christopher9a9d2752010-07-22 02:48:34 +0000498 setOperationAction(ISD::MEMBARRIER , MVT::Other, Custom);
Eli Friedman14648462011-07-27 22:21:52 +0000499 setOperationAction(ISD::ATOMIC_FENCE , MVT::Other, Custom);
Michael J. Spencerec38de22010-10-10 22:04:20 +0000500
Jim Grosbachf1ab49e2010-06-23 16:25:07 +0000501 // On X86 and X86-64, atomic operations are lowered to locked instructions.
502 // Locked instructions, in turn, have implicit fence semantics (all memory
503 // operations are flushed before issuing the locked instruction, and they
504 // are not buffered), so we can fold away the common pattern of
505 // fence-atomic-fence.
506 setShouldFoldAtomicFences(true);
Andrew Lenharthd497d9f2008-02-16 14:46:26 +0000507
Mon P Wang63307c32008-05-05 19:05:59 +0000508 // Expand certain atomics
Craig Topper9e401f22012-04-21 18:58:38 +0000509 for (unsigned i = 0; i != array_lengthof(IntVTs); ++i) {
Chris Lattnere019ec12010-12-19 20:07:10 +0000510 MVT VT = IntVTs[i];
511 setOperationAction(ISD::ATOMIC_CMP_SWAP, VT, Custom);
512 setOperationAction(ISD::ATOMIC_LOAD_SUB, VT, Custom);
Eli Friedman327236c2011-08-24 20:50:09 +0000513 setOperationAction(ISD::ATOMIC_STORE, VT, Custom);
Chris Lattnere019ec12010-12-19 20:07:10 +0000514 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000515
Dale Johannesen48c1bc22008-10-02 18:53:47 +0000516 if (!Subtarget->is64Bit()) {
Eli Friedmanf8f90f02011-08-24 22:33:28 +0000517 setOperationAction(ISD::ATOMIC_LOAD, MVT::i64, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000518 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i64, Custom);
519 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i64, Custom);
520 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i64, Custom);
521 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i64, Custom);
522 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i64, Custom);
523 setOperationAction(ISD::ATOMIC_LOAD_NAND, MVT::i64, Custom);
524 setOperationAction(ISD::ATOMIC_SWAP, MVT::i64, Custom);
Michael Liaoe5e8f762012-09-25 18:08:13 +0000525 setOperationAction(ISD::ATOMIC_LOAD_MAX, MVT::i64, Custom);
526 setOperationAction(ISD::ATOMIC_LOAD_MIN, MVT::i64, Custom);
527 setOperationAction(ISD::ATOMIC_LOAD_UMAX, MVT::i64, Custom);
528 setOperationAction(ISD::ATOMIC_LOAD_UMIN, MVT::i64, Custom);
Dale Johannesen48c1bc22008-10-02 18:53:47 +0000529 }
530
Eli Friedman43f51ae2011-08-26 21:21:21 +0000531 if (Subtarget->hasCmpxchg16b()) {
532 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i128, Custom);
533 }
534
Evan Cheng3c992d22006-03-07 02:02:57 +0000535 // FIXME - use subtarget debug flags
Anton Korobeynikovab4022f2006-10-31 08:31:24 +0000536 if (!Subtarget->isTargetDarwin() &&
537 !Subtarget->isTargetELF() &&
Dan Gohman44066042008-07-01 00:05:16 +0000538 !Subtarget->isTargetCygMing()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000539 setOperationAction(ISD::EH_LABEL, MVT::Other, Expand);
Dan Gohman44066042008-07-01 00:05:16 +0000540 }
Chris Lattnerf73bae12005-11-29 06:16:21 +0000541
Owen Anderson825b72b2009-08-11 20:47:22 +0000542 setOperationAction(ISD::EXCEPTIONADDR, MVT::i64, Expand);
543 setOperationAction(ISD::EHSELECTION, MVT::i64, Expand);
544 setOperationAction(ISD::EXCEPTIONADDR, MVT::i32, Expand);
545 setOperationAction(ISD::EHSELECTION, MVT::i32, Expand);
Anton Korobeynikovce3b4652007-05-02 19:53:33 +0000546 if (Subtarget->is64Bit()) {
Anton Korobeynikovce3b4652007-05-02 19:53:33 +0000547 setExceptionPointerRegister(X86::RAX);
548 setExceptionSelectorRegister(X86::RDX);
549 } else {
550 setExceptionPointerRegister(X86::EAX);
551 setExceptionSelectorRegister(X86::EDX);
552 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000553 setOperationAction(ISD::FRAME_TO_ARGS_OFFSET, MVT::i32, Custom);
554 setOperationAction(ISD::FRAME_TO_ARGS_OFFSET, MVT::i64, Custom);
Anton Korobeynikov260a6b82008-09-08 21:12:11 +0000555
Duncan Sands4a544a72011-09-06 13:37:06 +0000556 setOperationAction(ISD::INIT_TRAMPOLINE, MVT::Other, Custom);
557 setOperationAction(ISD::ADJUST_TRAMPOLINE, MVT::Other, Custom);
Duncan Sandsb116fac2007-07-27 20:02:49 +0000558
Owen Anderson825b72b2009-08-11 20:47:22 +0000559 setOperationAction(ISD::TRAP, MVT::Other, Legal);
Shuxin Yang970755e2012-10-19 20:11:16 +0000560 setOperationAction(ISD::DEBUGTRAP, MVT::Other, Legal);
Anton Korobeynikov66fac792008-01-15 07:02:33 +0000561
Nate Begemanacc398c2006-01-25 18:21:52 +0000562 // VASTART needs to be custom lowered to use the VarArgsFrameIndex
Owen Anderson825b72b2009-08-11 20:47:22 +0000563 setOperationAction(ISD::VASTART , MVT::Other, Custom);
564 setOperationAction(ISD::VAEND , MVT::Other, Expand);
Dan Gohman9018e832008-05-10 01:26:14 +0000565 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000566 setOperationAction(ISD::VAARG , MVT::Other, Custom);
567 setOperationAction(ISD::VACOPY , MVT::Other, Custom);
Dan Gohman9018e832008-05-10 01:26:14 +0000568 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000569 setOperationAction(ISD::VAARG , MVT::Other, Expand);
570 setOperationAction(ISD::VACOPY , MVT::Other, Expand);
Dan Gohman9018e832008-05-10 01:26:14 +0000571 }
Evan Chengae642192007-03-02 23:16:35 +0000572
Owen Anderson825b72b2009-08-11 20:47:22 +0000573 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
574 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
Eric Christopherc967ad82011-08-31 04:17:21 +0000575
576 if (Subtarget->isTargetCOFF() && !Subtarget->isTargetEnvMacho())
577 setOperationAction(ISD::DYNAMIC_STACKALLOC, Subtarget->is64Bit() ?
578 MVT::i64 : MVT::i32, Custom);
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000579 else if (TM.Options.EnableSegmentedStacks)
Eric Christopherc967ad82011-08-31 04:17:21 +0000580 setOperationAction(ISD::DYNAMIC_STACKALLOC, Subtarget->is64Bit() ?
581 MVT::i64 : MVT::i32, Custom);
582 else
583 setOperationAction(ISD::DYNAMIC_STACKALLOC, Subtarget->is64Bit() ?
584 MVT::i64 : MVT::i32, Expand);
Chris Lattnerb99329e2006-01-13 02:42:53 +0000585
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000586 if (!TM.Options.UseSoftFloat && X86ScalarSSEf64) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000587 // f32 and f64 use SSE.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000588 // Set up the FP register classes.
Craig Topperc9099502012-04-20 06:31:50 +0000589 addRegisterClass(MVT::f32, &X86::FR32RegClass);
590 addRegisterClass(MVT::f64, &X86::FR64RegClass);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000591
Evan Cheng223547a2006-01-31 22:28:30 +0000592 // Use ANDPD to simulate FABS.
Owen Anderson825b72b2009-08-11 20:47:22 +0000593 setOperationAction(ISD::FABS , MVT::f64, Custom);
594 setOperationAction(ISD::FABS , MVT::f32, Custom);
Evan Cheng223547a2006-01-31 22:28:30 +0000595
596 // Use XORP to simulate FNEG.
Owen Anderson825b72b2009-08-11 20:47:22 +0000597 setOperationAction(ISD::FNEG , MVT::f64, Custom);
598 setOperationAction(ISD::FNEG , MVT::f32, Custom);
Evan Cheng223547a2006-01-31 22:28:30 +0000599
Evan Cheng68c47cb2007-01-05 07:55:56 +0000600 // Use ANDPD and ORPD to simulate FCOPYSIGN.
Owen Anderson825b72b2009-08-11 20:47:22 +0000601 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
602 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
Evan Cheng68c47cb2007-01-05 07:55:56 +0000603
Stuart Hastings4fd0dee2011-06-01 04:39:42 +0000604 // Lower this to FGETSIGNx86 plus an AND.
605 setOperationAction(ISD::FGETSIGN, MVT::i64, Custom);
606 setOperationAction(ISD::FGETSIGN, MVT::i32, Custom);
607
Evan Chengd25e9e82006-02-02 00:28:23 +0000608 // We don't support sin/cos/fmod
Owen Anderson825b72b2009-08-11 20:47:22 +0000609 setOperationAction(ISD::FSIN , MVT::f64, Expand);
610 setOperationAction(ISD::FCOS , MVT::f64, Expand);
611 setOperationAction(ISD::FSIN , MVT::f32, Expand);
612 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000613
Chris Lattnera54aa942006-01-29 06:26:08 +0000614 // Expand FP immediates into loads from the stack, except for the special
615 // cases we handle.
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000616 addLegalFPImmediate(APFloat(+0.0)); // xorpd
617 addLegalFPImmediate(APFloat(+0.0f)); // xorps
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000618 } else if (!TM.Options.UseSoftFloat && X86ScalarSSEf32) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000619 // Use SSE for f32, x87 for f64.
620 // Set up the FP register classes.
Craig Topperc9099502012-04-20 06:31:50 +0000621 addRegisterClass(MVT::f32, &X86::FR32RegClass);
622 addRegisterClass(MVT::f64, &X86::RFP64RegClass);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000623
624 // Use ANDPS to simulate FABS.
Owen Anderson825b72b2009-08-11 20:47:22 +0000625 setOperationAction(ISD::FABS , MVT::f32, Custom);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000626
627 // Use XORP to simulate FNEG.
Owen Anderson825b72b2009-08-11 20:47:22 +0000628 setOperationAction(ISD::FNEG , MVT::f32, Custom);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000629
Owen Anderson825b72b2009-08-11 20:47:22 +0000630 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000631
632 // Use ANDPS and ORPS to simulate FCOPYSIGN.
Owen Anderson825b72b2009-08-11 20:47:22 +0000633 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
634 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000635
636 // We don't support sin/cos/fmod
Owen Anderson825b72b2009-08-11 20:47:22 +0000637 setOperationAction(ISD::FSIN , MVT::f32, Expand);
638 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000639
Nate Begemane1795842008-02-14 08:57:00 +0000640 // Special cases we handle for FP constants.
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000641 addLegalFPImmediate(APFloat(+0.0f)); // xorps
642 addLegalFPImmediate(APFloat(+0.0)); // FLD0
643 addLegalFPImmediate(APFloat(+1.0)); // FLD1
644 addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS
645 addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS
646
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000647 if (!TM.Options.UnsafeFPMath) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000648 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
649 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000650 }
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000651 } else if (!TM.Options.UseSoftFloat) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000652 // f32 and f64 in x87.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000653 // Set up the FP register classes.
Craig Topperc9099502012-04-20 06:31:50 +0000654 addRegisterClass(MVT::f64, &X86::RFP64RegClass);
655 addRegisterClass(MVT::f32, &X86::RFP32RegClass);
Anton Korobeynikov12c49af2006-11-21 00:01:06 +0000656
Owen Anderson825b72b2009-08-11 20:47:22 +0000657 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
658 setOperationAction(ISD::UNDEF, MVT::f32, Expand);
659 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
660 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Expand);
Dale Johannesen5411a392007-08-09 01:04:01 +0000661
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000662 if (!TM.Options.UnsafeFPMath) {
Benjamin Kramer562b2402012-09-15 12:44:27 +0000663 setOperationAction(ISD::FSIN , MVT::f32 , Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000664 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
Benjamin Kramer562b2402012-09-15 12:44:27 +0000665 setOperationAction(ISD::FCOS , MVT::f32 , Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000666 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000667 }
Dale Johannesenf04afdb2007-08-30 00:23:21 +0000668 addLegalFPImmediate(APFloat(+0.0)); // FLD0
669 addLegalFPImmediate(APFloat(+1.0)); // FLD1
670 addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS
671 addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000672 addLegalFPImmediate(APFloat(+0.0f)); // FLD0
673 addLegalFPImmediate(APFloat(+1.0f)); // FLD1
674 addLegalFPImmediate(APFloat(-0.0f)); // FLD0/FCHS
675 addLegalFPImmediate(APFloat(-1.0f)); // FLD1/FCHS
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000676 }
Evan Cheng470a6ad2006-02-22 02:26:30 +0000677
Cameron Zwarich33390842011-07-08 21:39:21 +0000678 // We don't support FMA.
679 setOperationAction(ISD::FMA, MVT::f64, Expand);
680 setOperationAction(ISD::FMA, MVT::f32, Expand);
681
Dale Johannesen59a58732007-08-05 18:49:15 +0000682 // Long double always uses X87.
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000683 if (!TM.Options.UseSoftFloat) {
Craig Topperc9099502012-04-20 06:31:50 +0000684 addRegisterClass(MVT::f80, &X86::RFP80RegClass);
Owen Anderson825b72b2009-08-11 20:47:22 +0000685 setOperationAction(ISD::UNDEF, MVT::f80, Expand);
686 setOperationAction(ISD::FCOPYSIGN, MVT::f80, Expand);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000687 {
Benjamin Kramer98383962010-12-04 14:22:24 +0000688 APFloat TmpFlt = APFloat::getZero(APFloat::x87DoubleExtended);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000689 addLegalFPImmediate(TmpFlt); // FLD0
690 TmpFlt.changeSign();
691 addLegalFPImmediate(TmpFlt); // FLD0/FCHS
Benjamin Kramer98383962010-12-04 14:22:24 +0000692
693 bool ignored;
Evan Chengc7ce29b2009-02-13 22:36:38 +0000694 APFloat TmpFlt2(+1.0);
695 TmpFlt2.convert(APFloat::x87DoubleExtended, APFloat::rmNearestTiesToEven,
696 &ignored);
697 addLegalFPImmediate(TmpFlt2); // FLD1
698 TmpFlt2.changeSign();
699 addLegalFPImmediate(TmpFlt2); // FLD1/FCHS
700 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000701
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000702 if (!TM.Options.UnsafeFPMath) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000703 setOperationAction(ISD::FSIN , MVT::f80 , Expand);
704 setOperationAction(ISD::FCOS , MVT::f80 , Expand);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000705 }
Cameron Zwarich33390842011-07-08 21:39:21 +0000706
Owen Anderson4a4fdf32011-12-08 19:32:14 +0000707 setOperationAction(ISD::FFLOOR, MVT::f80, Expand);
708 setOperationAction(ISD::FCEIL, MVT::f80, Expand);
709 setOperationAction(ISD::FTRUNC, MVT::f80, Expand);
710 setOperationAction(ISD::FRINT, MVT::f80, Expand);
711 setOperationAction(ISD::FNEARBYINT, MVT::f80, Expand);
Cameron Zwarich33390842011-07-08 21:39:21 +0000712 setOperationAction(ISD::FMA, MVT::f80, Expand);
Dale Johannesen2f429012007-09-26 21:10:55 +0000713 }
Dale Johannesen59a58732007-08-05 18:49:15 +0000714
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000715 // Always use a library call for pow.
Owen Anderson825b72b2009-08-11 20:47:22 +0000716 setOperationAction(ISD::FPOW , MVT::f32 , Expand);
717 setOperationAction(ISD::FPOW , MVT::f64 , Expand);
718 setOperationAction(ISD::FPOW , MVT::f80 , Expand);
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000719
Owen Anderson825b72b2009-08-11 20:47:22 +0000720 setOperationAction(ISD::FLOG, MVT::f80, Expand);
721 setOperationAction(ISD::FLOG2, MVT::f80, Expand);
722 setOperationAction(ISD::FLOG10, MVT::f80, Expand);
723 setOperationAction(ISD::FEXP, MVT::f80, Expand);
724 setOperationAction(ISD::FEXP2, MVT::f80, Expand);
Dale Johannesen7794f2a2008-09-04 00:47:13 +0000725
Mon P Wangf007a8b2008-11-06 05:31:54 +0000726 // First set operation action for all vector types to either promote
Mon P Wang0c397192008-10-30 08:01:45 +0000727 // (for widening) or expand (for scalarization). Then we will selectively
728 // turn on ones that can be effectively codegen'd.
Jakub Staszak6610b1d2012-04-29 20:52:53 +0000729 for (int VT = MVT::FIRST_VECTOR_VALUETYPE;
730 VT <= MVT::LAST_VECTOR_VALUETYPE; ++VT) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000731 setOperationAction(ISD::ADD , (MVT::SimpleValueType)VT, Expand);
732 setOperationAction(ISD::SUB , (MVT::SimpleValueType)VT, Expand);
733 setOperationAction(ISD::FADD, (MVT::SimpleValueType)VT, Expand);
734 setOperationAction(ISD::FNEG, (MVT::SimpleValueType)VT, Expand);
735 setOperationAction(ISD::FSUB, (MVT::SimpleValueType)VT, Expand);
736 setOperationAction(ISD::MUL , (MVT::SimpleValueType)VT, Expand);
737 setOperationAction(ISD::FMUL, (MVT::SimpleValueType)VT, Expand);
738 setOperationAction(ISD::SDIV, (MVT::SimpleValueType)VT, Expand);
739 setOperationAction(ISD::UDIV, (MVT::SimpleValueType)VT, Expand);
740 setOperationAction(ISD::FDIV, (MVT::SimpleValueType)VT, Expand);
741 setOperationAction(ISD::SREM, (MVT::SimpleValueType)VT, Expand);
742 setOperationAction(ISD::UREM, (MVT::SimpleValueType)VT, Expand);
743 setOperationAction(ISD::LOAD, (MVT::SimpleValueType)VT, Expand);
744 setOperationAction(ISD::VECTOR_SHUFFLE, (MVT::SimpleValueType)VT, Expand);
745 setOperationAction(ISD::EXTRACT_VECTOR_ELT,(MVT::SimpleValueType)VT,Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000746 setOperationAction(ISD::INSERT_VECTOR_ELT,(MVT::SimpleValueType)VT, Expand);
David Greenecfe33c42011-01-26 19:13:22 +0000747 setOperationAction(ISD::EXTRACT_SUBVECTOR,(MVT::SimpleValueType)VT,Expand);
748 setOperationAction(ISD::INSERT_SUBVECTOR,(MVT::SimpleValueType)VT,Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000749 setOperationAction(ISD::FABS, (MVT::SimpleValueType)VT, Expand);
750 setOperationAction(ISD::FSIN, (MVT::SimpleValueType)VT, Expand);
751 setOperationAction(ISD::FCOS, (MVT::SimpleValueType)VT, Expand);
752 setOperationAction(ISD::FREM, (MVT::SimpleValueType)VT, Expand);
Elena Demikhovsky1503aba2012-08-01 12:06:00 +0000753 setOperationAction(ISD::FMA, (MVT::SimpleValueType)VT, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000754 setOperationAction(ISD::FPOWI, (MVT::SimpleValueType)VT, Expand);
755 setOperationAction(ISD::FSQRT, (MVT::SimpleValueType)VT, Expand);
756 setOperationAction(ISD::FCOPYSIGN, (MVT::SimpleValueType)VT, Expand);
Craig Toppera1fb1d22012-09-08 04:58:43 +0000757 setOperationAction(ISD::FFLOOR, (MVT::SimpleValueType)VT, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000758 setOperationAction(ISD::SMUL_LOHI, (MVT::SimpleValueType)VT, Expand);
759 setOperationAction(ISD::UMUL_LOHI, (MVT::SimpleValueType)VT, Expand);
760 setOperationAction(ISD::SDIVREM, (MVT::SimpleValueType)VT, Expand);
761 setOperationAction(ISD::UDIVREM, (MVT::SimpleValueType)VT, Expand);
762 setOperationAction(ISD::FPOW, (MVT::SimpleValueType)VT, Expand);
763 setOperationAction(ISD::CTPOP, (MVT::SimpleValueType)VT, Expand);
764 setOperationAction(ISD::CTTZ, (MVT::SimpleValueType)VT, Expand);
Chandler Carruth63974b22011-12-13 01:56:10 +0000765 setOperationAction(ISD::CTTZ_ZERO_UNDEF, (MVT::SimpleValueType)VT, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000766 setOperationAction(ISD::CTLZ, (MVT::SimpleValueType)VT, Expand);
Chandler Carruth63974b22011-12-13 01:56:10 +0000767 setOperationAction(ISD::CTLZ_ZERO_UNDEF, (MVT::SimpleValueType)VT, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000768 setOperationAction(ISD::SHL, (MVT::SimpleValueType)VT, Expand);
769 setOperationAction(ISD::SRA, (MVT::SimpleValueType)VT, Expand);
770 setOperationAction(ISD::SRL, (MVT::SimpleValueType)VT, Expand);
771 setOperationAction(ISD::ROTL, (MVT::SimpleValueType)VT, Expand);
772 setOperationAction(ISD::ROTR, (MVT::SimpleValueType)VT, Expand);
773 setOperationAction(ISD::BSWAP, (MVT::SimpleValueType)VT, Expand);
Duncan Sands28b77e92011-09-06 19:07:46 +0000774 setOperationAction(ISD::SETCC, (MVT::SimpleValueType)VT, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000775 setOperationAction(ISD::FLOG, (MVT::SimpleValueType)VT, Expand);
776 setOperationAction(ISD::FLOG2, (MVT::SimpleValueType)VT, Expand);
777 setOperationAction(ISD::FLOG10, (MVT::SimpleValueType)VT, Expand);
778 setOperationAction(ISD::FEXP, (MVT::SimpleValueType)VT, Expand);
779 setOperationAction(ISD::FEXP2, (MVT::SimpleValueType)VT, Expand);
780 setOperationAction(ISD::FP_TO_UINT, (MVT::SimpleValueType)VT, Expand);
781 setOperationAction(ISD::FP_TO_SINT, (MVT::SimpleValueType)VT, Expand);
782 setOperationAction(ISD::UINT_TO_FP, (MVT::SimpleValueType)VT, Expand);
783 setOperationAction(ISD::SINT_TO_FP, (MVT::SimpleValueType)VT, Expand);
Dan Gohman87862e72009-12-11 21:31:27 +0000784 setOperationAction(ISD::SIGN_EXTEND_INREG, (MVT::SimpleValueType)VT,Expand);
Dan Gohman2e141d72009-12-14 23:40:38 +0000785 setOperationAction(ISD::TRUNCATE, (MVT::SimpleValueType)VT, Expand);
786 setOperationAction(ISD::SIGN_EXTEND, (MVT::SimpleValueType)VT, Expand);
787 setOperationAction(ISD::ZERO_EXTEND, (MVT::SimpleValueType)VT, Expand);
788 setOperationAction(ISD::ANY_EXTEND, (MVT::SimpleValueType)VT, Expand);
Nadav Rotemaec58612011-09-13 19:17:42 +0000789 setOperationAction(ISD::VSELECT, (MVT::SimpleValueType)VT, Expand);
Jakub Staszak6610b1d2012-04-29 20:52:53 +0000790 for (int InnerVT = MVT::FIRST_VECTOR_VALUETYPE;
791 InnerVT <= MVT::LAST_VECTOR_VALUETYPE; ++InnerVT)
Dan Gohman2e141d72009-12-14 23:40:38 +0000792 setTruncStoreAction((MVT::SimpleValueType)VT,
793 (MVT::SimpleValueType)InnerVT, Expand);
794 setLoadExtAction(ISD::SEXTLOAD, (MVT::SimpleValueType)VT, Expand);
795 setLoadExtAction(ISD::ZEXTLOAD, (MVT::SimpleValueType)VT, Expand);
796 setLoadExtAction(ISD::EXTLOAD, (MVT::SimpleValueType)VT, Expand);
Evan Chengd30bf012006-03-01 01:11:20 +0000797 }
798
Evan Chengc7ce29b2009-02-13 22:36:38 +0000799 // FIXME: In order to prevent SSE instructions being expanded to MMX ones
800 // with -msoft-float, disable use of MMX as well.
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000801 if (!TM.Options.UseSoftFloat && Subtarget->hasMMX()) {
Craig Topperc9099502012-04-20 06:31:50 +0000802 addRegisterClass(MVT::x86mmx, &X86::VR64RegClass);
Dale Johannesen0488fb62010-09-30 23:57:10 +0000803 // No operations on x86mmx supported, everything uses intrinsics.
Evan Cheng470a6ad2006-02-22 02:26:30 +0000804 }
805
Dale Johannesen0488fb62010-09-30 23:57:10 +0000806 // MMX-sized vectors (other than x86mmx) are expected to be expanded
807 // into smaller operations.
808 setOperationAction(ISD::MULHS, MVT::v8i8, Expand);
809 setOperationAction(ISD::MULHS, MVT::v4i16, Expand);
810 setOperationAction(ISD::MULHS, MVT::v2i32, Expand);
811 setOperationAction(ISD::MULHS, MVT::v1i64, Expand);
812 setOperationAction(ISD::AND, MVT::v8i8, Expand);
813 setOperationAction(ISD::AND, MVT::v4i16, Expand);
814 setOperationAction(ISD::AND, MVT::v2i32, Expand);
815 setOperationAction(ISD::AND, MVT::v1i64, Expand);
816 setOperationAction(ISD::OR, MVT::v8i8, Expand);
817 setOperationAction(ISD::OR, MVT::v4i16, Expand);
818 setOperationAction(ISD::OR, MVT::v2i32, Expand);
819 setOperationAction(ISD::OR, MVT::v1i64, Expand);
820 setOperationAction(ISD::XOR, MVT::v8i8, Expand);
821 setOperationAction(ISD::XOR, MVT::v4i16, Expand);
822 setOperationAction(ISD::XOR, MVT::v2i32, Expand);
823 setOperationAction(ISD::XOR, MVT::v1i64, Expand);
824 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i8, Expand);
825 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4i16, Expand);
826 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v2i32, Expand);
827 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v1i64, Expand);
828 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v1i64, Expand);
829 setOperationAction(ISD::SELECT, MVT::v8i8, Expand);
830 setOperationAction(ISD::SELECT, MVT::v4i16, Expand);
831 setOperationAction(ISD::SELECT, MVT::v2i32, Expand);
832 setOperationAction(ISD::SELECT, MVT::v1i64, Expand);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000833 setOperationAction(ISD::BITCAST, MVT::v8i8, Expand);
834 setOperationAction(ISD::BITCAST, MVT::v4i16, Expand);
835 setOperationAction(ISD::BITCAST, MVT::v2i32, Expand);
836 setOperationAction(ISD::BITCAST, MVT::v1i64, Expand);
Dale Johannesen0488fb62010-09-30 23:57:10 +0000837
Craig Topper1accb7e2012-01-10 06:54:16 +0000838 if (!TM.Options.UseSoftFloat && Subtarget->hasSSE1()) {
Craig Topperc9099502012-04-20 06:31:50 +0000839 addRegisterClass(MVT::v4f32, &X86::VR128RegClass);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000840
Owen Anderson825b72b2009-08-11 20:47:22 +0000841 setOperationAction(ISD::FADD, MVT::v4f32, Legal);
842 setOperationAction(ISD::FSUB, MVT::v4f32, Legal);
843 setOperationAction(ISD::FMUL, MVT::v4f32, Legal);
844 setOperationAction(ISD::FDIV, MVT::v4f32, Legal);
845 setOperationAction(ISD::FSQRT, MVT::v4f32, Legal);
846 setOperationAction(ISD::FNEG, MVT::v4f32, Custom);
Craig Topper43620672012-09-08 07:31:51 +0000847 setOperationAction(ISD::FABS, MVT::v4f32, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000848 setOperationAction(ISD::LOAD, MVT::v4f32, Legal);
849 setOperationAction(ISD::BUILD_VECTOR, MVT::v4f32, Custom);
850 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4f32, Custom);
851 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
852 setOperationAction(ISD::SELECT, MVT::v4f32, Custom);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000853 }
854
Craig Topper1accb7e2012-01-10 06:54:16 +0000855 if (!TM.Options.UseSoftFloat && Subtarget->hasSSE2()) {
Craig Topperc9099502012-04-20 06:31:50 +0000856 addRegisterClass(MVT::v2f64, &X86::VR128RegClass);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000857
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000858 // FIXME: Unfortunately -soft-float and -no-implicit-float means XMM
859 // registers cannot be used even for integer operations.
Craig Topperc9099502012-04-20 06:31:50 +0000860 addRegisterClass(MVT::v16i8, &X86::VR128RegClass);
861 addRegisterClass(MVT::v8i16, &X86::VR128RegClass);
862 addRegisterClass(MVT::v4i32, &X86::VR128RegClass);
863 addRegisterClass(MVT::v2i64, &X86::VR128RegClass);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000864
Owen Anderson825b72b2009-08-11 20:47:22 +0000865 setOperationAction(ISD::ADD, MVT::v16i8, Legal);
866 setOperationAction(ISD::ADD, MVT::v8i16, Legal);
867 setOperationAction(ISD::ADD, MVT::v4i32, Legal);
868 setOperationAction(ISD::ADD, MVT::v2i64, Legal);
869 setOperationAction(ISD::MUL, MVT::v2i64, Custom);
870 setOperationAction(ISD::SUB, MVT::v16i8, Legal);
871 setOperationAction(ISD::SUB, MVT::v8i16, Legal);
872 setOperationAction(ISD::SUB, MVT::v4i32, Legal);
873 setOperationAction(ISD::SUB, MVT::v2i64, Legal);
874 setOperationAction(ISD::MUL, MVT::v8i16, Legal);
875 setOperationAction(ISD::FADD, MVT::v2f64, Legal);
876 setOperationAction(ISD::FSUB, MVT::v2f64, Legal);
877 setOperationAction(ISD::FMUL, MVT::v2f64, Legal);
878 setOperationAction(ISD::FDIV, MVT::v2f64, Legal);
879 setOperationAction(ISD::FSQRT, MVT::v2f64, Legal);
880 setOperationAction(ISD::FNEG, MVT::v2f64, Custom);
Craig Topper43620672012-09-08 07:31:51 +0000881 setOperationAction(ISD::FABS, MVT::v2f64, Custom);
Evan Cheng2c3ae372006-04-12 21:21:57 +0000882
Nadav Rotem354efd82011-09-18 14:57:03 +0000883 setOperationAction(ISD::SETCC, MVT::v2i64, Custom);
Duncan Sands28b77e92011-09-06 19:07:46 +0000884 setOperationAction(ISD::SETCC, MVT::v16i8, Custom);
885 setOperationAction(ISD::SETCC, MVT::v8i16, Custom);
886 setOperationAction(ISD::SETCC, MVT::v4i32, Custom);
Nate Begemanc2616e42008-05-12 20:34:32 +0000887
Owen Anderson825b72b2009-08-11 20:47:22 +0000888 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v16i8, Custom);
889 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i16, Custom);
890 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
891 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom);
892 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
Evan Chengf7c378e2006-04-10 07:23:14 +0000893
Evan Cheng2c3ae372006-04-12 21:21:57 +0000894 // Custom lower build_vector, vector_shuffle, and extract_vector_elt.
Jakub Staszak6610b1d2012-04-29 20:52:53 +0000895 for (int i = MVT::v16i8; i != MVT::v2i64; ++i) {
Craig Topper0d1f1762012-08-12 00:34:56 +0000896 MVT VT = (MVT::SimpleValueType)i;
Nate Begeman844e0f92007-12-11 01:41:33 +0000897 // Do not attempt to custom lower non-power-of-2 vectors
Duncan Sands83ec4b62008-06-06 12:08:01 +0000898 if (!isPowerOf2_32(VT.getVectorNumElements()))
Nate Begeman844e0f92007-12-11 01:41:33 +0000899 continue;
David Greene9b9838d2009-06-29 16:47:10 +0000900 // Do not attempt to custom lower non-128-bit vectors
901 if (!VT.is128BitVector())
902 continue;
Craig Topper0d1f1762012-08-12 00:34:56 +0000903 setOperationAction(ISD::BUILD_VECTOR, VT, Custom);
904 setOperationAction(ISD::VECTOR_SHUFFLE, VT, Custom);
905 setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT, Custom);
Evan Cheng2c3ae372006-04-12 21:21:57 +0000906 }
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000907
Owen Anderson825b72b2009-08-11 20:47:22 +0000908 setOperationAction(ISD::BUILD_VECTOR, MVT::v2f64, Custom);
909 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i64, Custom);
910 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2f64, Custom);
911 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i64, Custom);
912 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2f64, Custom);
913 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2f64, Custom);
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000914
Nate Begemancdd1eec2008-02-12 22:51:28 +0000915 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000916 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i64, Custom);
917 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Custom);
Nate Begemancdd1eec2008-02-12 22:51:28 +0000918 }
Evan Cheng2c3ae372006-04-12 21:21:57 +0000919
Anton Korobeynikov12c49af2006-11-21 00:01:06 +0000920 // Promote v16i8, v8i16, v4i32 load, select, and, or, xor to v2i64.
Craig Topper31a207a2012-05-04 06:39:13 +0000921 for (int i = MVT::v16i8; i != MVT::v2i64; ++i) {
Craig Topper0d1f1762012-08-12 00:34:56 +0000922 MVT VT = (MVT::SimpleValueType)i;
David Greene9b9838d2009-06-29 16:47:10 +0000923
924 // Do not attempt to promote non-128-bit vectors
Chris Lattner32b4b5a2010-07-05 05:53:14 +0000925 if (!VT.is128BitVector())
David Greene9b9838d2009-06-29 16:47:10 +0000926 continue;
Michael J. Spencerec38de22010-10-10 22:04:20 +0000927
Craig Topper0d1f1762012-08-12 00:34:56 +0000928 setOperationAction(ISD::AND, VT, Promote);
929 AddPromotedToType (ISD::AND, VT, MVT::v2i64);
930 setOperationAction(ISD::OR, VT, Promote);
931 AddPromotedToType (ISD::OR, VT, MVT::v2i64);
932 setOperationAction(ISD::XOR, VT, Promote);
933 AddPromotedToType (ISD::XOR, VT, MVT::v2i64);
934 setOperationAction(ISD::LOAD, VT, Promote);
935 AddPromotedToType (ISD::LOAD, VT, MVT::v2i64);
936 setOperationAction(ISD::SELECT, VT, Promote);
937 AddPromotedToType (ISD::SELECT, VT, MVT::v2i64);
Evan Chengf7c378e2006-04-10 07:23:14 +0000938 }
Evan Cheng2c3ae372006-04-12 21:21:57 +0000939
Owen Anderson825b72b2009-08-11 20:47:22 +0000940 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000941
Evan Cheng2c3ae372006-04-12 21:21:57 +0000942 // Custom lower v2i64 and v2f64 selects.
Owen Anderson825b72b2009-08-11 20:47:22 +0000943 setOperationAction(ISD::LOAD, MVT::v2f64, Legal);
944 setOperationAction(ISD::LOAD, MVT::v2i64, Legal);
945 setOperationAction(ISD::SELECT, MVT::v2f64, Custom);
946 setOperationAction(ISD::SELECT, MVT::v2i64, Custom);
Scott Michelfdc40a02009-02-17 22:15:04 +0000947
Owen Anderson825b72b2009-08-11 20:47:22 +0000948 setOperationAction(ISD::FP_TO_SINT, MVT::v4i32, Legal);
949 setOperationAction(ISD::SINT_TO_FP, MVT::v4i32, Legal);
Michael Liaob8150d82012-09-10 18:33:51 +0000950
Michael Liao9d796db2012-10-10 16:32:15 +0000951 setOperationAction(ISD::FP_EXTEND, MVT::v2f32, Custom);
Michael Liao44c2d612012-10-10 16:53:28 +0000952 setOperationAction(ISD::FP_ROUND, MVT::v2f32, Custom);
Michael Liao9d796db2012-10-10 16:32:15 +0000953
Michael Liaob8150d82012-09-10 18:33:51 +0000954 setLoadExtAction(ISD::EXTLOAD, MVT::v2f32, Legal);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000955 }
Evan Chengc7ce29b2009-02-13 22:36:38 +0000956
Craig Topperd0a31172012-01-10 06:37:29 +0000957 if (Subtarget->hasSSE41()) {
Benjamin Kramerb6533972011-12-09 15:44:03 +0000958 setOperationAction(ISD::FFLOOR, MVT::f32, Legal);
959 setOperationAction(ISD::FCEIL, MVT::f32, Legal);
960 setOperationAction(ISD::FTRUNC, MVT::f32, Legal);
961 setOperationAction(ISD::FRINT, MVT::f32, Legal);
962 setOperationAction(ISD::FNEARBYINT, MVT::f32, Legal);
963 setOperationAction(ISD::FFLOOR, MVT::f64, Legal);
964 setOperationAction(ISD::FCEIL, MVT::f64, Legal);
965 setOperationAction(ISD::FTRUNC, MVT::f64, Legal);
966 setOperationAction(ISD::FRINT, MVT::f64, Legal);
967 setOperationAction(ISD::FNEARBYINT, MVT::f64, Legal);
968
Craig Topper12fb5c62012-09-08 17:42:27 +0000969 setOperationAction(ISD::FFLOOR, MVT::v4f32, Legal);
970 setOperationAction(ISD::FFLOOR, MVT::v2f64, Legal);
971
Nate Begeman14d12ca2008-02-11 04:19:36 +0000972 // FIXME: Do we need to handle scalar-to-vector here?
Owen Anderson825b72b2009-08-11 20:47:22 +0000973 setOperationAction(ISD::MUL, MVT::v4i32, Legal);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000974
Nadav Rotemfbad25e2011-09-11 15:02:23 +0000975 setOperationAction(ISD::VSELECT, MVT::v2f64, Legal);
976 setOperationAction(ISD::VSELECT, MVT::v2i64, Legal);
977 setOperationAction(ISD::VSELECT, MVT::v16i8, Legal);
978 setOperationAction(ISD::VSELECT, MVT::v4i32, Legal);
979 setOperationAction(ISD::VSELECT, MVT::v4f32, Legal);
Nadav Rotemffe3e7d2011-09-08 08:11:19 +0000980
Nate Begeman14d12ca2008-02-11 04:19:36 +0000981 // i8 and i16 vectors are custom , because the source register and source
982 // source memory operand types are not the same width. f32 vectors are
983 // custom since the immediate controlling the insert encodes additional
984 // information.
Owen Anderson825b72b2009-08-11 20:47:22 +0000985 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v16i8, Custom);
986 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
987 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom);
988 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000989
Owen Anderson825b72b2009-08-11 20:47:22 +0000990 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v16i8, Custom);
991 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v8i16, Custom);
992 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4i32, Custom);
993 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000994
Pete Coopera77214a2011-11-14 19:38:42 +0000995 // FIXME: these should be Legal but thats only for the case where
Chad Rosier30450e82011-12-22 22:35:21 +0000996 // the index is constant. For now custom expand to deal with that.
Nate Begeman14d12ca2008-02-11 04:19:36 +0000997 if (Subtarget->is64Bit()) {
Pete Coopera77214a2011-11-14 19:38:42 +0000998 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i64, Custom);
999 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Custom);
Nate Begeman14d12ca2008-02-11 04:19:36 +00001000 }
1001 }
Evan Cheng470a6ad2006-02-22 02:26:30 +00001002
Craig Topper1accb7e2012-01-10 06:54:16 +00001003 if (Subtarget->hasSSE2()) {
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +00001004 setOperationAction(ISD::SRL, MVT::v8i16, Custom);
Craig Topper7be5dfd2011-11-12 09:58:49 +00001005 setOperationAction(ISD::SRL, MVT::v16i8, Custom);
Nadav Rotem43012222011-05-11 08:12:09 +00001006
Nadav Rotem43012222011-05-11 08:12:09 +00001007 setOperationAction(ISD::SHL, MVT::v8i16, Custom);
Craig Topper7be5dfd2011-11-12 09:58:49 +00001008 setOperationAction(ISD::SHL, MVT::v16i8, Custom);
Nadav Rotem43012222011-05-11 08:12:09 +00001009
Nadav Rotem43012222011-05-11 08:12:09 +00001010 setOperationAction(ISD::SRA, MVT::v8i16, Custom);
Eli Friedmanf6aa6b12011-11-01 21:18:39 +00001011 setOperationAction(ISD::SRA, MVT::v16i8, Custom);
Craig Topper7be5dfd2011-11-12 09:58:49 +00001012
1013 if (Subtarget->hasAVX2()) {
1014 setOperationAction(ISD::SRL, MVT::v2i64, Legal);
1015 setOperationAction(ISD::SRL, MVT::v4i32, Legal);
1016
1017 setOperationAction(ISD::SHL, MVT::v2i64, Legal);
1018 setOperationAction(ISD::SHL, MVT::v4i32, Legal);
1019
1020 setOperationAction(ISD::SRA, MVT::v4i32, Legal);
1021 } else {
1022 setOperationAction(ISD::SRL, MVT::v2i64, Custom);
1023 setOperationAction(ISD::SRL, MVT::v4i32, Custom);
1024
1025 setOperationAction(ISD::SHL, MVT::v2i64, Custom);
1026 setOperationAction(ISD::SHL, MVT::v4i32, Custom);
1027
1028 setOperationAction(ISD::SRA, MVT::v4i32, Custom);
1029 }
Nadav Rotem43012222011-05-11 08:12:09 +00001030 }
1031
Nick Lewycky8a8d4792011-12-02 22:16:29 +00001032 if (!TM.Options.UseSoftFloat && Subtarget->hasAVX()) {
Craig Topperc9099502012-04-20 06:31:50 +00001033 addRegisterClass(MVT::v32i8, &X86::VR256RegClass);
1034 addRegisterClass(MVT::v16i16, &X86::VR256RegClass);
1035 addRegisterClass(MVT::v8i32, &X86::VR256RegClass);
1036 addRegisterClass(MVT::v8f32, &X86::VR256RegClass);
1037 addRegisterClass(MVT::v4i64, &X86::VR256RegClass);
1038 addRegisterClass(MVT::v4f64, &X86::VR256RegClass);
David Greened94c1012009-06-29 22:50:51 +00001039
Owen Anderson825b72b2009-08-11 20:47:22 +00001040 setOperationAction(ISD::LOAD, MVT::v8f32, Legal);
Owen Anderson825b72b2009-08-11 20:47:22 +00001041 setOperationAction(ISD::LOAD, MVT::v4f64, Legal);
1042 setOperationAction(ISD::LOAD, MVT::v4i64, Legal);
David Greene54d8eba2011-01-27 22:38:56 +00001043
Owen Anderson825b72b2009-08-11 20:47:22 +00001044 setOperationAction(ISD::FADD, MVT::v8f32, Legal);
1045 setOperationAction(ISD::FSUB, MVT::v8f32, Legal);
1046 setOperationAction(ISD::FMUL, MVT::v8f32, Legal);
1047 setOperationAction(ISD::FDIV, MVT::v8f32, Legal);
1048 setOperationAction(ISD::FSQRT, MVT::v8f32, Legal);
Craig Topper12fb5c62012-09-08 17:42:27 +00001049 setOperationAction(ISD::FFLOOR, MVT::v8f32, Legal);
Owen Anderson825b72b2009-08-11 20:47:22 +00001050 setOperationAction(ISD::FNEG, MVT::v8f32, Custom);
Craig Topper43620672012-09-08 07:31:51 +00001051 setOperationAction(ISD::FABS, MVT::v8f32, Custom);
David Greene9b9838d2009-06-29 16:47:10 +00001052
Owen Anderson825b72b2009-08-11 20:47:22 +00001053 setOperationAction(ISD::FADD, MVT::v4f64, Legal);
1054 setOperationAction(ISD::FSUB, MVT::v4f64, Legal);
1055 setOperationAction(ISD::FMUL, MVT::v4f64, Legal);
1056 setOperationAction(ISD::FDIV, MVT::v4f64, Legal);
1057 setOperationAction(ISD::FSQRT, MVT::v4f64, Legal);
Craig Topper12fb5c62012-09-08 17:42:27 +00001058 setOperationAction(ISD::FFLOOR, MVT::v4f64, Legal);
Owen Anderson825b72b2009-08-11 20:47:22 +00001059 setOperationAction(ISD::FNEG, MVT::v4f64, Custom);
Craig Topper43620672012-09-08 07:31:51 +00001060 setOperationAction(ISD::FABS, MVT::v4f64, Custom);
David Greene9b9838d2009-06-29 16:47:10 +00001061
Michael Liaobedcbd42012-10-16 18:14:11 +00001062 setOperationAction(ISD::TRUNCATE, MVT::v8i16, Custom);
1063
1064 setOperationAction(ISD::FP_TO_SINT, MVT::v8i16, Custom);
1065
Bruno Cardoso Lopes2e64ae42011-07-28 01:26:39 +00001066 setOperationAction(ISD::FP_TO_SINT, MVT::v8i32, Legal);
1067 setOperationAction(ISD::SINT_TO_FP, MVT::v8i32, Legal);
Bruno Cardoso Lopes55244ce2011-08-01 21:54:09 +00001068 setOperationAction(ISD::FP_ROUND, MVT::v4f32, Legal);
Bruno Cardoso Lopes2e64ae42011-07-28 01:26:39 +00001069
Michael Liaob8150d82012-09-10 18:33:51 +00001070 setLoadExtAction(ISD::EXTLOAD, MVT::v4f32, Legal);
1071
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +00001072 setOperationAction(ISD::SRL, MVT::v16i16, Custom);
1073 setOperationAction(ISD::SRL, MVT::v32i8, Custom);
1074
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +00001075 setOperationAction(ISD::SHL, MVT::v16i16, Custom);
1076 setOperationAction(ISD::SHL, MVT::v32i8, Custom);
1077
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +00001078 setOperationAction(ISD::SRA, MVT::v16i16, Custom);
Craig Topper7be5dfd2011-11-12 09:58:49 +00001079 setOperationAction(ISD::SRA, MVT::v32i8, Custom);
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +00001080
Duncan Sands28b77e92011-09-06 19:07:46 +00001081 setOperationAction(ISD::SETCC, MVT::v32i8, Custom);
1082 setOperationAction(ISD::SETCC, MVT::v16i16, Custom);
1083 setOperationAction(ISD::SETCC, MVT::v8i32, Custom);
1084 setOperationAction(ISD::SETCC, MVT::v4i64, Custom);
Bruno Cardoso Lopes0f0e0a02011-08-09 00:46:57 +00001085
Bruno Cardoso Lopesd40aa242011-08-09 23:27:13 +00001086 setOperationAction(ISD::SELECT, MVT::v4f64, Custom);
1087 setOperationAction(ISD::SELECT, MVT::v4i64, Custom);
1088 setOperationAction(ISD::SELECT, MVT::v8f32, Custom);
1089
Craig Topperaaa643c2011-11-09 07:28:55 +00001090 setOperationAction(ISD::VSELECT, MVT::v4f64, Legal);
1091 setOperationAction(ISD::VSELECT, MVT::v4i64, Legal);
1092 setOperationAction(ISD::VSELECT, MVT::v8i32, Legal);
1093 setOperationAction(ISD::VSELECT, MVT::v8f32, Legal);
Nadav Rotem8ffad562011-09-09 20:29:17 +00001094
Craig Topperbf404372012-08-31 15:40:30 +00001095 if (Subtarget->hasFMA() || Subtarget->hasFMA4()) {
Elena Demikhovsky1503aba2012-08-01 12:06:00 +00001096 setOperationAction(ISD::FMA, MVT::v8f32, Custom);
1097 setOperationAction(ISD::FMA, MVT::v4f64, Custom);
1098 setOperationAction(ISD::FMA, MVT::v4f32, Custom);
1099 setOperationAction(ISD::FMA, MVT::v2f64, Custom);
1100 setOperationAction(ISD::FMA, MVT::f32, Custom);
1101 setOperationAction(ISD::FMA, MVT::f64, Custom);
1102 }
Craig Topper880ef452012-08-11 22:34:26 +00001103
Craig Topperaaa643c2011-11-09 07:28:55 +00001104 if (Subtarget->hasAVX2()) {
1105 setOperationAction(ISD::ADD, MVT::v4i64, Legal);
1106 setOperationAction(ISD::ADD, MVT::v8i32, Legal);
1107 setOperationAction(ISD::ADD, MVT::v16i16, Legal);
1108 setOperationAction(ISD::ADD, MVT::v32i8, Legal);
Craig Topper13894fa2011-08-24 06:14:18 +00001109
Craig Topperaaa643c2011-11-09 07:28:55 +00001110 setOperationAction(ISD::SUB, MVT::v4i64, Legal);
1111 setOperationAction(ISD::SUB, MVT::v8i32, Legal);
1112 setOperationAction(ISD::SUB, MVT::v16i16, Legal);
1113 setOperationAction(ISD::SUB, MVT::v32i8, Legal);
Craig Topper13894fa2011-08-24 06:14:18 +00001114
Craig Topperaaa643c2011-11-09 07:28:55 +00001115 setOperationAction(ISD::MUL, MVT::v4i64, Custom);
1116 setOperationAction(ISD::MUL, MVT::v8i32, Legal);
1117 setOperationAction(ISD::MUL, MVT::v16i16, Legal);
Craig Topper46154eb2011-11-11 07:39:23 +00001118 // Don't lower v32i8 because there is no 128-bit byte mul
Nadav Rotembb539bf2011-11-09 13:21:28 +00001119
1120 setOperationAction(ISD::VSELECT, MVT::v32i8, Legal);
Craig Topper7be5dfd2011-11-12 09:58:49 +00001121
1122 setOperationAction(ISD::SRL, MVT::v4i64, Legal);
1123 setOperationAction(ISD::SRL, MVT::v8i32, Legal);
1124
1125 setOperationAction(ISD::SHL, MVT::v4i64, Legal);
1126 setOperationAction(ISD::SHL, MVT::v8i32, Legal);
1127
1128 setOperationAction(ISD::SRA, MVT::v8i32, Legal);
Craig Topperaaa643c2011-11-09 07:28:55 +00001129 } else {
1130 setOperationAction(ISD::ADD, MVT::v4i64, Custom);
1131 setOperationAction(ISD::ADD, MVT::v8i32, Custom);
1132 setOperationAction(ISD::ADD, MVT::v16i16, Custom);
1133 setOperationAction(ISD::ADD, MVT::v32i8, Custom);
1134
1135 setOperationAction(ISD::SUB, MVT::v4i64, Custom);
1136 setOperationAction(ISD::SUB, MVT::v8i32, Custom);
1137 setOperationAction(ISD::SUB, MVT::v16i16, Custom);
1138 setOperationAction(ISD::SUB, MVT::v32i8, Custom);
1139
1140 setOperationAction(ISD::MUL, MVT::v4i64, Custom);
1141 setOperationAction(ISD::MUL, MVT::v8i32, Custom);
1142 setOperationAction(ISD::MUL, MVT::v16i16, Custom);
1143 // Don't lower v32i8 because there is no 128-bit byte mul
Craig Topper7be5dfd2011-11-12 09:58:49 +00001144
1145 setOperationAction(ISD::SRL, MVT::v4i64, Custom);
1146 setOperationAction(ISD::SRL, MVT::v8i32, Custom);
1147
1148 setOperationAction(ISD::SHL, MVT::v4i64, Custom);
1149 setOperationAction(ISD::SHL, MVT::v8i32, Custom);
1150
1151 setOperationAction(ISD::SRA, MVT::v8i32, Custom);
Craig Topperaaa643c2011-11-09 07:28:55 +00001152 }
Craig Topper13894fa2011-08-24 06:14:18 +00001153
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001154 // Custom lower several nodes for 256-bit types.
Jakub Staszak6610b1d2012-04-29 20:52:53 +00001155 for (int i = MVT::FIRST_VECTOR_VALUETYPE;
1156 i <= MVT::LAST_VECTOR_VALUETYPE; ++i) {
Craig Topper0d1f1762012-08-12 00:34:56 +00001157 MVT VT = (MVT::SimpleValueType)i;
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001158
1159 // Extract subvector is special because the value type
1160 // (result) is 128-bit but the source is 256-bit wide.
1161 if (VT.is128BitVector())
Craig Topper0d1f1762012-08-12 00:34:56 +00001162 setOperationAction(ISD::EXTRACT_SUBVECTOR, VT, Custom);
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001163
1164 // Do not attempt to custom lower other non-256-bit vectors
1165 if (!VT.is256BitVector())
David Greene9b9838d2009-06-29 16:47:10 +00001166 continue;
David Greene54d8eba2011-01-27 22:38:56 +00001167
Craig Topper0d1f1762012-08-12 00:34:56 +00001168 setOperationAction(ISD::BUILD_VECTOR, VT, Custom);
1169 setOperationAction(ISD::VECTOR_SHUFFLE, VT, Custom);
1170 setOperationAction(ISD::INSERT_VECTOR_ELT, VT, Custom);
1171 setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT, Custom);
1172 setOperationAction(ISD::SCALAR_TO_VECTOR, VT, Custom);
1173 setOperationAction(ISD::INSERT_SUBVECTOR, VT, Custom);
1174 setOperationAction(ISD::CONCAT_VECTORS, VT, Custom);
David Greene9b9838d2009-06-29 16:47:10 +00001175 }
1176
David Greene54d8eba2011-01-27 22:38:56 +00001177 // Promote v32i8, v16i16, v8i32 select, and, or, xor to v4i64.
Jakub Staszak6610b1d2012-04-29 20:52:53 +00001178 for (int i = MVT::v32i8; i != MVT::v4i64; ++i) {
Craig Topper0d1f1762012-08-12 00:34:56 +00001179 MVT VT = (MVT::SimpleValueType)i;
David Greene54d8eba2011-01-27 22:38:56 +00001180
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001181 // Do not attempt to promote non-256-bit vectors
1182 if (!VT.is256BitVector())
David Greene54d8eba2011-01-27 22:38:56 +00001183 continue;
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001184
Craig Topper0d1f1762012-08-12 00:34:56 +00001185 setOperationAction(ISD::AND, VT, Promote);
1186 AddPromotedToType (ISD::AND, VT, MVT::v4i64);
1187 setOperationAction(ISD::OR, VT, Promote);
1188 AddPromotedToType (ISD::OR, VT, MVT::v4i64);
1189 setOperationAction(ISD::XOR, VT, Promote);
1190 AddPromotedToType (ISD::XOR, VT, MVT::v4i64);
1191 setOperationAction(ISD::LOAD, VT, Promote);
1192 AddPromotedToType (ISD::LOAD, VT, MVT::v4i64);
1193 setOperationAction(ISD::SELECT, VT, Promote);
1194 AddPromotedToType (ISD::SELECT, VT, MVT::v4i64);
David Greene54d8eba2011-01-27 22:38:56 +00001195 }
David Greene9b9838d2009-06-29 16:47:10 +00001196 }
1197
Nadav Rotemd0f3ef82011-07-14 11:11:14 +00001198 // SIGN_EXTEND_INREGs are evaluated by the extend type. Handle the expansion
1199 // of this type with custom code.
Jakub Staszak6610b1d2012-04-29 20:52:53 +00001200 for (int VT = MVT::FIRST_VECTOR_VALUETYPE;
1201 VT != MVT::LAST_VECTOR_VALUETYPE; VT++) {
Chad Rosier30450e82011-12-22 22:35:21 +00001202 setOperationAction(ISD::SIGN_EXTEND_INREG, (MVT::SimpleValueType)VT,
1203 Custom);
Nadav Rotemd0f3ef82011-07-14 11:11:14 +00001204 }
1205
Evan Cheng6be2c582006-04-05 23:38:46 +00001206 // We want to custom lower some of our intrinsics.
Owen Anderson825b72b2009-08-11 20:47:22 +00001207 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
Benjamin Kramerb9bee042012-07-12 09:31:43 +00001208 setOperationAction(ISD::INTRINSIC_W_CHAIN, MVT::Other, Custom);
Evan Cheng6be2c582006-04-05 23:38:46 +00001209
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00001210
Eli Friedman962f5492010-06-02 19:35:46 +00001211 // Only custom-lower 64-bit SADDO and friends on 64-bit because we don't
1212 // handle type legalization for these operations here.
Dan Gohman71c62a22010-06-02 19:13:40 +00001213 //
Eli Friedman962f5492010-06-02 19:35:46 +00001214 // FIXME: We really should do custom legalization for addition and
1215 // subtraction on x86-32 once PR3203 is fixed. We really can't do much better
1216 // than generic legalization for 64-bit multiplication-with-overflow, though.
Chris Lattnera34b3cf2010-12-19 20:03:11 +00001217 for (unsigned i = 0, e = 3+Subtarget->is64Bit(); i != e; ++i) {
1218 // Add/Sub/Mul with overflow operations are custom lowered.
1219 MVT VT = IntVTs[i];
1220 setOperationAction(ISD::SADDO, VT, Custom);
1221 setOperationAction(ISD::UADDO, VT, Custom);
1222 setOperationAction(ISD::SSUBO, VT, Custom);
1223 setOperationAction(ISD::USUBO, VT, Custom);
1224 setOperationAction(ISD::SMULO, VT, Custom);
1225 setOperationAction(ISD::UMULO, VT, Custom);
Eli Friedmana993f0a2010-06-02 00:27:18 +00001226 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00001227
Chris Lattnera34b3cf2010-12-19 20:03:11 +00001228 // There are no 8-bit 3-address imul/mul instructions
1229 setOperationAction(ISD::SMULO, MVT::i8, Expand);
1230 setOperationAction(ISD::UMULO, MVT::i8, Expand);
Bill Wendling41ea7e72008-11-24 19:21:46 +00001231
Evan Chengd54f2d52009-03-31 19:38:51 +00001232 if (!Subtarget->is64Bit()) {
1233 // These libcalls are not available in 32-bit.
1234 setLibcallName(RTLIB::SHL_I128, 0);
1235 setLibcallName(RTLIB::SRL_I128, 0);
1236 setLibcallName(RTLIB::SRA_I128, 0);
1237 }
1238
Evan Cheng206ee9d2006-07-07 08:33:52 +00001239 // We have target-specific dag combine patterns for the following nodes:
1240 setTargetDAGCombine(ISD::VECTOR_SHUFFLE);
Dan Gohman1bbf72b2010-03-15 23:23:03 +00001241 setTargetDAGCombine(ISD::EXTRACT_VECTOR_ELT);
Duncan Sands6bcd2192011-09-17 16:49:39 +00001242 setTargetDAGCombine(ISD::VSELECT);
Chris Lattner83e6c992006-10-04 06:57:07 +00001243 setTargetDAGCombine(ISD::SELECT);
Nate Begeman740ab032009-01-26 00:52:55 +00001244 setTargetDAGCombine(ISD::SHL);
1245 setTargetDAGCombine(ISD::SRA);
1246 setTargetDAGCombine(ISD::SRL);
Evan Cheng760d1942010-01-04 21:22:48 +00001247 setTargetDAGCombine(ISD::OR);
Nate Begemanb65c1752010-12-17 22:55:37 +00001248 setTargetDAGCombine(ISD::AND);
Benjamin Kramer7d6fe132010-12-21 21:41:44 +00001249 setTargetDAGCombine(ISD::ADD);
Duncan Sands17470be2011-09-22 20:15:48 +00001250 setTargetDAGCombine(ISD::FADD);
1251 setTargetDAGCombine(ISD::FSUB);
Elena Demikhovsky1503aba2012-08-01 12:06:00 +00001252 setTargetDAGCombine(ISD::FMA);
Benjamin Kramer7d6fe132010-12-21 21:41:44 +00001253 setTargetDAGCombine(ISD::SUB);
Nadav Rotem91e43fd2011-09-18 10:39:32 +00001254 setTargetDAGCombine(ISD::LOAD);
Chris Lattner149a4e52008-02-22 02:09:43 +00001255 setTargetDAGCombine(ISD::STORE);
Evan Cheng2e489c42009-12-16 00:53:11 +00001256 setTargetDAGCombine(ISD::ZERO_EXTEND);
Elena Demikhovsky1da58672012-04-22 09:39:03 +00001257 setTargetDAGCombine(ISD::ANY_EXTEND);
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +00001258 setTargetDAGCombine(ISD::SIGN_EXTEND);
Elena Demikhovsky3ae98152012-02-01 07:56:44 +00001259 setTargetDAGCombine(ISD::TRUNCATE);
Nadav Rotema3540772012-04-23 21:53:37 +00001260 setTargetDAGCombine(ISD::UINT_TO_FP);
Stuart Hastingsf99a4b82011-06-06 23:15:58 +00001261 setTargetDAGCombine(ISD::SINT_TO_FP);
Chad Rosiera73b6fc2012-04-27 22:33:25 +00001262 setTargetDAGCombine(ISD::SETCC);
Evan Cheng0b0cd912009-03-28 05:57:29 +00001263 if (Subtarget->is64Bit())
1264 setTargetDAGCombine(ISD::MUL);
Manman Ren92363622012-06-07 22:39:10 +00001265 setTargetDAGCombine(ISD::XOR);
Evan Cheng206ee9d2006-07-07 08:33:52 +00001266
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001267 computeRegisterProperties();
1268
Evan Cheng05219282011-01-06 06:52:41 +00001269 // On Darwin, -Os means optimize for size without hurting performance,
1270 // do not reduce the limit.
Dan Gohman87060f52008-06-30 21:00:56 +00001271 maxStoresPerMemset = 16; // For @llvm.memset -> sequence of stores
Evan Cheng05219282011-01-06 06:52:41 +00001272 maxStoresPerMemsetOptSize = Subtarget->isTargetDarwin() ? 16 : 8;
Evan Cheng255f20f2010-04-01 06:04:33 +00001273 maxStoresPerMemcpy = 8; // For @llvm.memcpy -> sequence of stores
Evan Cheng05219282011-01-06 06:52:41 +00001274 maxStoresPerMemcpyOptSize = Subtarget->isTargetDarwin() ? 8 : 4;
1275 maxStoresPerMemmove = 8; // For @llvm.memmove -> sequence of stores
1276 maxStoresPerMemmoveOptSize = Subtarget->isTargetDarwin() ? 8 : 4;
Jakob Stoklund Olesen8c741b82011-12-06 01:26:19 +00001277 setPrefLoopAlignment(4); // 2^4 bytes.
Evan Cheng6ebf7bc2009-05-13 21:42:09 +00001278 benefitFromCodePlacementOpt = true;
Eli Friedmanfc5d3052011-05-06 20:34:06 +00001279
Benjamin Krameraaf723d2012-05-05 12:49:14 +00001280 // Predictable cmov don't hurt on atom because it's in-order.
1281 predictableSelectIsExpensive = !Subtarget->isAtom();
1282
Jakob Stoklund Olesen8c741b82011-12-06 01:26:19 +00001283 setPrefFunctionAlignment(4); // 2^4 bytes.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001284}
1285
Scott Michel5b8f82e2008-03-10 15:42:14 +00001286
Duncan Sands28b77e92011-09-06 19:07:46 +00001287EVT X86TargetLowering::getSetCCResultType(EVT VT) const {
1288 if (!VT.isVector()) return MVT::i8;
1289 return VT.changeVectorElementTypeToInteger();
Scott Michel5b8f82e2008-03-10 15:42:14 +00001290}
1291
1292
Evan Cheng29286502008-01-23 23:17:41 +00001293/// getMaxByValAlign - Helper for getByValTypeAlignment to determine
1294/// the desired ByVal argument alignment.
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001295static void getMaxByValAlign(Type *Ty, unsigned &MaxAlign) {
Evan Cheng29286502008-01-23 23:17:41 +00001296 if (MaxAlign == 16)
1297 return;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001298 if (VectorType *VTy = dyn_cast<VectorType>(Ty)) {
Evan Cheng29286502008-01-23 23:17:41 +00001299 if (VTy->getBitWidth() == 128)
1300 MaxAlign = 16;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001301 } else if (ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
Evan Cheng29286502008-01-23 23:17:41 +00001302 unsigned EltAlign = 0;
1303 getMaxByValAlign(ATy->getElementType(), EltAlign);
1304 if (EltAlign > MaxAlign)
1305 MaxAlign = EltAlign;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001306 } else if (StructType *STy = dyn_cast<StructType>(Ty)) {
Evan Cheng29286502008-01-23 23:17:41 +00001307 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
1308 unsigned EltAlign = 0;
1309 getMaxByValAlign(STy->getElementType(i), EltAlign);
1310 if (EltAlign > MaxAlign)
1311 MaxAlign = EltAlign;
1312 if (MaxAlign == 16)
1313 break;
1314 }
1315 }
Evan Cheng29286502008-01-23 23:17:41 +00001316}
1317
1318/// getByValTypeAlignment - Return the desired alignment for ByVal aggregate
1319/// function arguments in the caller parameter area. For X86, aggregates
Dale Johannesen0c191872008-02-08 19:48:20 +00001320/// that contain SSE vectors are placed at 16-byte boundaries while the rest
1321/// are at 4-byte boundaries.
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001322unsigned X86TargetLowering::getByValTypeAlignment(Type *Ty) const {
Evan Cheng1887c1c2008-08-21 21:00:15 +00001323 if (Subtarget->is64Bit()) {
1324 // Max of 8 and alignment of type.
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00001325 unsigned TyAlign = TD->getABITypeAlignment(Ty);
Evan Cheng1887c1c2008-08-21 21:00:15 +00001326 if (TyAlign > 8)
1327 return TyAlign;
1328 return 8;
1329 }
1330
Evan Cheng29286502008-01-23 23:17:41 +00001331 unsigned Align = 4;
Craig Topper1accb7e2012-01-10 06:54:16 +00001332 if (Subtarget->hasSSE1())
Dale Johannesen0c191872008-02-08 19:48:20 +00001333 getMaxByValAlign(Ty, Align);
Evan Cheng29286502008-01-23 23:17:41 +00001334 return Align;
1335}
Chris Lattner2b02a442007-02-25 08:29:00 +00001336
Evan Chengf0df0312008-05-15 08:39:06 +00001337/// getOptimalMemOpType - Returns the target specific optimal type for load
Evan Chengc3b0c342010-04-08 07:37:57 +00001338/// and store operations as a result of memset, memcpy, and memmove
1339/// lowering. If DstAlign is zero that means it's safe to destination
1340/// alignment can satisfy any constraint. Similarly if SrcAlign is zero it
1341/// means there isn't a need to check it against alignment requirement,
1342/// probably because the source does not need to be loaded. If
Lang Hames15701f82011-10-26 23:50:43 +00001343/// 'IsZeroVal' is true, that means it's safe to return a
Evan Chengc3b0c342010-04-08 07:37:57 +00001344/// non-scalar-integer type, e.g. empty string source, constant, or loaded
1345/// from memory. 'MemcpyStrSrc' indicates whether the memcpy source is
1346/// constant so it does not need to be loaded.
Dan Gohman37f32ee2010-04-16 20:11:05 +00001347/// It returns EVT::Other if the type should be determined using generic
1348/// target-independent logic.
Owen Andersone50ed302009-08-10 22:56:29 +00001349EVT
Evan Cheng255f20f2010-04-01 06:04:33 +00001350X86TargetLowering::getOptimalMemOpType(uint64_t Size,
1351 unsigned DstAlign, unsigned SrcAlign,
Lang Hames15701f82011-10-26 23:50:43 +00001352 bool IsZeroVal,
Evan Chengc3b0c342010-04-08 07:37:57 +00001353 bool MemcpyStrSrc,
Dan Gohman37f32ee2010-04-16 20:11:05 +00001354 MachineFunction &MF) const {
Chris Lattner4002a1b2008-10-28 05:49:35 +00001355 // FIXME: This turns off use of xmm stores for memset/memcpy on targets like
1356 // linux. This is because the stack realignment code can't handle certain
1357 // cases like PR2962. This should be removed when PR2962 is fixed.
Dan Gohman37f32ee2010-04-16 20:11:05 +00001358 const Function *F = MF.getFunction();
Lang Hames15701f82011-10-26 23:50:43 +00001359 if (IsZeroVal &&
Bill Wendling67658342012-10-09 07:45:08 +00001360 !F->getFnAttributes().hasAttribute(Attributes::NoImplicitFloat)) {
Evan Cheng255f20f2010-04-01 06:04:33 +00001361 if (Size >= 16 &&
Evan Chenga5e13622011-01-07 19:35:30 +00001362 (Subtarget->isUnalignedMemAccessFast() ||
1363 ((DstAlign == 0 || DstAlign >= 16) &&
1364 (SrcAlign == 0 || SrcAlign >= 16))) &&
Evan Cheng255f20f2010-04-01 06:04:33 +00001365 Subtarget->getStackAlignment() >= 16) {
Craig Topper562659f2012-01-13 08:32:21 +00001366 if (Subtarget->getStackAlignment() >= 32) {
1367 if (Subtarget->hasAVX2())
1368 return MVT::v8i32;
1369 if (Subtarget->hasAVX())
1370 return MVT::v8f32;
1371 }
Craig Topper1accb7e2012-01-10 06:54:16 +00001372 if (Subtarget->hasSSE2())
Evan Cheng255f20f2010-04-01 06:04:33 +00001373 return MVT::v4i32;
Craig Topper1accb7e2012-01-10 06:54:16 +00001374 if (Subtarget->hasSSE1())
Evan Cheng255f20f2010-04-01 06:04:33 +00001375 return MVT::v4f32;
Evan Chengc3b0c342010-04-08 07:37:57 +00001376 } else if (!MemcpyStrSrc && Size >= 8 &&
Evan Cheng3ea97552010-04-01 20:27:45 +00001377 !Subtarget->is64Bit() &&
Evan Cheng255f20f2010-04-01 06:04:33 +00001378 Subtarget->getStackAlignment() >= 8 &&
Craig Topper1accb7e2012-01-10 06:54:16 +00001379 Subtarget->hasSSE2()) {
Evan Chengc3b0c342010-04-08 07:37:57 +00001380 // Do not use f64 to lower memcpy if source is string constant. It's
1381 // better to use i32 to avoid the loads.
Evan Cheng255f20f2010-04-01 06:04:33 +00001382 return MVT::f64;
Evan Chengc3b0c342010-04-08 07:37:57 +00001383 }
Chris Lattner4002a1b2008-10-28 05:49:35 +00001384 }
Evan Chengf0df0312008-05-15 08:39:06 +00001385 if (Subtarget->is64Bit() && Size >= 8)
Owen Anderson825b72b2009-08-11 20:47:22 +00001386 return MVT::i64;
1387 return MVT::i32;
Evan Chengf0df0312008-05-15 08:39:06 +00001388}
1389
Chris Lattner5e1df8d2010-01-25 23:38:14 +00001390/// getJumpTableEncoding - Return the entry encoding for a jump table in the
1391/// current function. The returned value is a member of the
1392/// MachineJumpTableInfo::JTEntryKind enum.
1393unsigned X86TargetLowering::getJumpTableEncoding() const {
1394 // In GOT pic mode, each entry in the jump table is emitted as a @GOTOFF
1395 // symbol.
1396 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1397 Subtarget->isPICStyleGOT())
Chris Lattnerc64daab2010-01-26 05:02:42 +00001398 return MachineJumpTableInfo::EK_Custom32;
Michael J. Spencerec38de22010-10-10 22:04:20 +00001399
Chris Lattner5e1df8d2010-01-25 23:38:14 +00001400 // Otherwise, use the normal jump table encoding heuristics.
1401 return TargetLowering::getJumpTableEncoding();
1402}
1403
Chris Lattnerc64daab2010-01-26 05:02:42 +00001404const MCExpr *
1405X86TargetLowering::LowerCustomJumpTableEntry(const MachineJumpTableInfo *MJTI,
1406 const MachineBasicBlock *MBB,
1407 unsigned uid,MCContext &Ctx) const{
1408 assert(getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1409 Subtarget->isPICStyleGOT());
1410 // In 32-bit ELF systems, our jump table entries are formed with @GOTOFF
1411 // entries.
Daniel Dunbar4e815f82010-03-15 23:51:06 +00001412 return MCSymbolRefExpr::Create(MBB->getSymbol(),
1413 MCSymbolRefExpr::VK_GOTOFF, Ctx);
Chris Lattnerc64daab2010-01-26 05:02:42 +00001414}
1415
Evan Chengcc415862007-11-09 01:32:10 +00001416/// getPICJumpTableRelocaBase - Returns relocation base for the given PIC
1417/// jumptable.
Dan Gohman475871a2008-07-27 21:46:04 +00001418SDValue X86TargetLowering::getPICJumpTableRelocBase(SDValue Table,
Chris Lattner589c6f62010-01-26 06:28:43 +00001419 SelectionDAG &DAG) const {
Chris Lattnere4df7562009-07-09 03:15:51 +00001420 if (!Subtarget->is64Bit())
Dale Johannesenb300d2a2009-02-07 00:55:49 +00001421 // This doesn't have DebugLoc associated with it, but is not really the
1422 // same as a Register.
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00001423 return DAG.getNode(X86ISD::GlobalBaseReg, DebugLoc(), getPointerTy());
Evan Chengcc415862007-11-09 01:32:10 +00001424 return Table;
1425}
1426
Chris Lattner589c6f62010-01-26 06:28:43 +00001427/// getPICJumpTableRelocBaseExpr - This returns the relocation base for the
1428/// given PIC jumptable, the same as getPICJumpTableRelocBase, but as an
1429/// MCExpr.
1430const MCExpr *X86TargetLowering::
1431getPICJumpTableRelocBaseExpr(const MachineFunction *MF, unsigned JTI,
1432 MCContext &Ctx) const {
1433 // X86-64 uses RIP relative addressing based on the jump table label.
1434 if (Subtarget->isPICStyleRIPRel())
1435 return TargetLowering::getPICJumpTableRelocBaseExpr(MF, JTI, Ctx);
1436
1437 // Otherwise, the reference is relative to the PIC base.
Chris Lattner142b5312010-11-14 22:48:15 +00001438 return MCSymbolRefExpr::Create(MF->getPICBaseSymbol(), Ctx);
Chris Lattner589c6f62010-01-26 06:28:43 +00001439}
1440
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00001441// FIXME: Why this routine is here? Move to RegInfo!
Evan Chengdee81012010-07-26 21:50:05 +00001442std::pair<const TargetRegisterClass*, uint8_t>
1443X86TargetLowering::findRepresentativeClass(EVT VT) const{
1444 const TargetRegisterClass *RRC = 0;
1445 uint8_t Cost = 1;
1446 switch (VT.getSimpleVT().SimpleTy) {
1447 default:
1448 return TargetLowering::findRepresentativeClass(VT);
1449 case MVT::i8: case MVT::i16: case MVT::i32: case MVT::i64:
Craig Topperc9099502012-04-20 06:31:50 +00001450 RRC = Subtarget->is64Bit() ?
1451 (const TargetRegisterClass*)&X86::GR64RegClass :
1452 (const TargetRegisterClass*)&X86::GR32RegClass;
Evan Chengdee81012010-07-26 21:50:05 +00001453 break;
Dale Johannesen0488fb62010-09-30 23:57:10 +00001454 case MVT::x86mmx:
Craig Topperc9099502012-04-20 06:31:50 +00001455 RRC = &X86::VR64RegClass;
Evan Chengdee81012010-07-26 21:50:05 +00001456 break;
1457 case MVT::f32: case MVT::f64:
1458 case MVT::v16i8: case MVT::v8i16: case MVT::v4i32: case MVT::v2i64:
1459 case MVT::v4f32: case MVT::v2f64:
1460 case MVT::v32i8: case MVT::v8i32: case MVT::v4i64: case MVT::v8f32:
1461 case MVT::v4f64:
Craig Topperc9099502012-04-20 06:31:50 +00001462 RRC = &X86::VR128RegClass;
Evan Chengdee81012010-07-26 21:50:05 +00001463 break;
1464 }
1465 return std::make_pair(RRC, Cost);
1466}
1467
Eric Christopherf7a0c7b2010-07-06 05:18:56 +00001468bool X86TargetLowering::getStackCookieLocation(unsigned &AddressSpace,
1469 unsigned &Offset) const {
1470 if (!Subtarget->isTargetLinux())
1471 return false;
1472
1473 if (Subtarget->is64Bit()) {
1474 // %fs:0x28, unless we're using a Kernel code model, in which case it's %gs:
1475 Offset = 0x28;
1476 if (getTargetMachine().getCodeModel() == CodeModel::Kernel)
1477 AddressSpace = 256;
1478 else
1479 AddressSpace = 257;
1480 } else {
1481 // %gs:0x14 on i386
1482 Offset = 0x14;
1483 AddressSpace = 256;
1484 }
1485 return true;
1486}
1487
1488
Chris Lattner2b02a442007-02-25 08:29:00 +00001489//===----------------------------------------------------------------------===//
1490// Return Value Calling Convention Implementation
1491//===----------------------------------------------------------------------===//
1492
Chris Lattner59ed56b2007-02-28 04:55:35 +00001493#include "X86GenCallingConv.inc"
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001494
Michael J. Spencerec38de22010-10-10 22:04:20 +00001495bool
Eric Christopher471e4222011-06-08 23:55:35 +00001496X86TargetLowering::CanLowerReturn(CallingConv::ID CallConv,
Craig Topper0fbf3642012-04-23 03:28:34 +00001497 MachineFunction &MF, bool isVarArg,
Dan Gohman84023e02010-07-10 09:00:22 +00001498 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9af33c2010-07-06 22:19:37 +00001499 LLVMContext &Context) const {
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +00001500 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00001501 CCState CCInfo(CallConv, isVarArg, MF, getTargetMachine(),
Dan Gohmanc9af33c2010-07-06 22:19:37 +00001502 RVLocs, Context);
Dan Gohman84023e02010-07-10 09:00:22 +00001503 return CCInfo.CheckReturn(Outs, RetCC_X86);
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +00001504}
1505
Dan Gohman98ca4f22009-08-05 01:29:28 +00001506SDValue
1507X86TargetLowering::LowerReturn(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001508 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001509 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001510 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohmand858e902010-04-17 15:26:15 +00001511 DebugLoc dl, SelectionDAG &DAG) const {
Dan Gohman1e93df62010-04-17 14:41:14 +00001512 MachineFunction &MF = DAG.getMachineFunction();
1513 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
Scott Michelfdc40a02009-02-17 22:15:04 +00001514
Chris Lattner9774c912007-02-27 05:28:59 +00001515 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00001516 CCState CCInfo(CallConv, isVarArg, MF, getTargetMachine(),
Dan Gohman98ca4f22009-08-05 01:29:28 +00001517 RVLocs, *DAG.getContext());
1518 CCInfo.AnalyzeReturn(Outs, RetCC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001519
Evan Chengdcea1632010-02-04 02:40:39 +00001520 // Add the regs to the liveout set for the function.
1521 MachineRegisterInfo &MRI = DAG.getMachineFunction().getRegInfo();
1522 for (unsigned i = 0; i != RVLocs.size(); ++i)
1523 if (RVLocs[i].isRegLoc() && !MRI.isLiveOut(RVLocs[i].getLocReg()))
1524 MRI.addLiveOut(RVLocs[i].getLocReg());
Scott Michelfdc40a02009-02-17 22:15:04 +00001525
Dan Gohman475871a2008-07-27 21:46:04 +00001526 SDValue Flag;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001527
Dan Gohman475871a2008-07-27 21:46:04 +00001528 SmallVector<SDValue, 6> RetOps;
Chris Lattner447ff682008-03-11 03:23:40 +00001529 RetOps.push_back(Chain); // Operand #0 = Chain (updated below)
1530 // Operand #1 = Bytes To Pop
Dan Gohman1e93df62010-04-17 14:41:14 +00001531 RetOps.push_back(DAG.getTargetConstant(FuncInfo->getBytesToPopOnReturn(),
1532 MVT::i16));
Scott Michelfdc40a02009-02-17 22:15:04 +00001533
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001534 // Copy the result values into the output registers.
Chris Lattner8e6da152008-03-10 21:08:41 +00001535 for (unsigned i = 0; i != RVLocs.size(); ++i) {
1536 CCValAssign &VA = RVLocs[i];
1537 assert(VA.isRegLoc() && "Can only return in registers!");
Dan Gohmanc9403652010-07-07 15:54:55 +00001538 SDValue ValToCopy = OutVals[i];
Dale Johannesenc76d23f2010-07-23 00:30:35 +00001539 EVT ValVT = ValToCopy.getValueType();
1540
Jakob Stoklund Olesenee66b412012-05-31 17:28:20 +00001541 // Promote values to the appropriate types
1542 if (VA.getLocInfo() == CCValAssign::SExt)
1543 ValToCopy = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), ValToCopy);
1544 else if (VA.getLocInfo() == CCValAssign::ZExt)
1545 ValToCopy = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), ValToCopy);
1546 else if (VA.getLocInfo() == CCValAssign::AExt)
1547 ValToCopy = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), ValToCopy);
1548 else if (VA.getLocInfo() == CCValAssign::BCvt)
1549 ValToCopy = DAG.getNode(ISD::BITCAST, dl, VA.getLocVT(), ValToCopy);
1550
Dale Johannesenc4510512010-09-24 19:05:48 +00001551 // If this is x86-64, and we disabled SSE, we can't return FP values,
1552 // or SSE or MMX vectors.
1553 if ((ValVT == MVT::f32 || ValVT == MVT::f64 ||
1554 VA.getLocReg() == X86::XMM0 || VA.getLocReg() == X86::XMM1) &&
Craig Topper1accb7e2012-01-10 06:54:16 +00001555 (Subtarget->is64Bit() && !Subtarget->hasSSE1())) {
Dale Johannesenc76d23f2010-07-23 00:30:35 +00001556 report_fatal_error("SSE register return with SSE disabled");
1557 }
1558 // Likewise we can't return F64 values with SSE1 only. gcc does so, but
1559 // llvm-gcc has never done it right and no one has noticed, so this
1560 // should be OK for now.
1561 if (ValVT == MVT::f64 &&
Craig Topper1accb7e2012-01-10 06:54:16 +00001562 (Subtarget->is64Bit() && !Subtarget->hasSSE2()))
Dale Johannesenc76d23f2010-07-23 00:30:35 +00001563 report_fatal_error("SSE2 register return with SSE2 disabled");
Scott Michelfdc40a02009-02-17 22:15:04 +00001564
Chris Lattner447ff682008-03-11 03:23:40 +00001565 // Returns in ST0/ST1 are handled specially: these are pushed as operands to
1566 // the RET instruction and handled by the FP Stackifier.
Dan Gohman37eed792009-02-04 17:28:58 +00001567 if (VA.getLocReg() == X86::ST0 ||
1568 VA.getLocReg() == X86::ST1) {
Chris Lattner447ff682008-03-11 03:23:40 +00001569 // If this is a copy from an xmm register to ST(0), use an FPExtend to
1570 // change the value to the FP stack register class.
Dan Gohman37eed792009-02-04 17:28:58 +00001571 if (isScalarFPTypeInSSEReg(VA.getValVT()))
Owen Anderson825b72b2009-08-11 20:47:22 +00001572 ValToCopy = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f80, ValToCopy);
Chris Lattner447ff682008-03-11 03:23:40 +00001573 RetOps.push_back(ValToCopy);
1574 // Don't emit a copytoreg.
1575 continue;
1576 }
Dale Johannesena68f9012008-06-24 22:01:44 +00001577
Evan Cheng242b38b2009-02-23 09:03:22 +00001578 // 64-bit vector (MMX) values are returned in XMM0 / XMM1 except for v1i64
1579 // which is returned in RAX / RDX.
Evan Cheng6140a8b2009-02-22 08:05:12 +00001580 if (Subtarget->is64Bit()) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00001581 if (ValVT == MVT::x86mmx) {
Chris Lattner97a2a562010-08-26 05:24:29 +00001582 if (VA.getLocReg() == X86::XMM0 || VA.getLocReg() == X86::XMM1) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001583 ValToCopy = DAG.getNode(ISD::BITCAST, dl, MVT::i64, ValToCopy);
Eric Christopher90eb4022010-07-22 00:26:08 +00001584 ValToCopy = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i64,
1585 ValToCopy);
Chris Lattner97a2a562010-08-26 05:24:29 +00001586 // If we don't have SSE2 available, convert to v4f32 so the generated
1587 // register is legal.
Craig Topper1accb7e2012-01-10 06:54:16 +00001588 if (!Subtarget->hasSSE2())
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001589 ValToCopy = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32,ValToCopy);
Chris Lattner97a2a562010-08-26 05:24:29 +00001590 }
Evan Cheng242b38b2009-02-23 09:03:22 +00001591 }
Evan Cheng6140a8b2009-02-22 08:05:12 +00001592 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00001593
Dale Johannesendd64c412009-02-04 00:33:20 +00001594 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), ValToCopy, Flag);
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001595 Flag = Chain.getValue(1);
1596 }
Dan Gohman61a92132008-04-21 23:59:07 +00001597
1598 // The x86-64 ABI for returning structs by value requires that we copy
1599 // the sret argument into %rax for the return. We saved the argument into
1600 // a virtual register in the entry block, so now we copy the value out
1601 // and into %rax.
1602 if (Subtarget->is64Bit() &&
1603 DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
1604 MachineFunction &MF = DAG.getMachineFunction();
1605 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1606 unsigned Reg = FuncInfo->getSRetReturnReg();
Michael J. Spencerec38de22010-10-10 22:04:20 +00001607 assert(Reg &&
Zhongxing Xuc2798a12010-05-26 08:10:02 +00001608 "SRetReturnReg should have been set in LowerFormalArguments().");
Dale Johannesendd64c412009-02-04 00:33:20 +00001609 SDValue Val = DAG.getCopyFromReg(Chain, dl, Reg, getPointerTy());
Dan Gohman61a92132008-04-21 23:59:07 +00001610
Dale Johannesendd64c412009-02-04 00:33:20 +00001611 Chain = DAG.getCopyToReg(Chain, dl, X86::RAX, Val, Flag);
Dan Gohman61a92132008-04-21 23:59:07 +00001612 Flag = Chain.getValue(1);
Dan Gohman00326812009-10-12 16:36:12 +00001613
1614 // RAX now acts like a return value.
Evan Chengdcea1632010-02-04 02:40:39 +00001615 MRI.addLiveOut(X86::RAX);
Dan Gohman61a92132008-04-21 23:59:07 +00001616 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001617
Chris Lattner447ff682008-03-11 03:23:40 +00001618 RetOps[0] = Chain; // Update chain.
1619
1620 // Add the flag if we have it.
Gabor Greifba36cb52008-08-28 21:40:38 +00001621 if (Flag.getNode())
Chris Lattner447ff682008-03-11 03:23:40 +00001622 RetOps.push_back(Flag);
Scott Michelfdc40a02009-02-17 22:15:04 +00001623
1624 return DAG.getNode(X86ISD::RET_FLAG, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001625 MVT::Other, &RetOps[0], RetOps.size());
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001626}
1627
Evan Chengbf010eb2012-04-10 01:51:00 +00001628bool X86TargetLowering::isUsedByReturnOnly(SDNode *N, SDValue &Chain) const {
Evan Cheng3d2125c2010-11-30 23:55:39 +00001629 if (N->getNumValues() != 1)
1630 return false;
1631 if (!N->hasNUsesOfValue(1, 0))
1632 return false;
1633
Evan Chengbf010eb2012-04-10 01:51:00 +00001634 SDValue TCChain = Chain;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001635 SDNode *Copy = *N->use_begin();
Chad Rosierc8d7eea2012-03-05 19:27:12 +00001636 if (Copy->getOpcode() == ISD::CopyToReg) {
1637 // If the copy has a glue operand, we conservatively assume it isn't safe to
1638 // perform a tail call.
1639 if (Copy->getOperand(Copy->getNumOperands()-1).getValueType() == MVT::Glue)
1640 return false;
Evan Chengbf010eb2012-04-10 01:51:00 +00001641 TCChain = Copy->getOperand(0);
Chad Rosierc8d7eea2012-03-05 19:27:12 +00001642 } else if (Copy->getOpcode() != ISD::FP_EXTEND)
Chad Rosier74bab7f2012-03-02 02:50:46 +00001643 return false;
1644
Evan Cheng1bf891a2010-12-01 22:59:46 +00001645 bool HasRet = false;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001646 for (SDNode::use_iterator UI = Copy->use_begin(), UE = Copy->use_end();
Evan Cheng1bf891a2010-12-01 22:59:46 +00001647 UI != UE; ++UI) {
Evan Cheng3d2125c2010-11-30 23:55:39 +00001648 if (UI->getOpcode() != X86ISD::RET_FLAG)
1649 return false;
Evan Cheng1bf891a2010-12-01 22:59:46 +00001650 HasRet = true;
1651 }
Evan Cheng3d2125c2010-11-30 23:55:39 +00001652
Evan Chengbf010eb2012-04-10 01:51:00 +00001653 if (!HasRet)
1654 return false;
1655
1656 Chain = TCChain;
1657 return true;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001658}
1659
Cameron Zwarich7bbf0ee2011-03-17 14:53:37 +00001660EVT
1661X86TargetLowering::getTypeForExtArgOrReturn(LLVMContext &Context, EVT VT,
Cameron Zwarich44579682011-03-17 14:21:56 +00001662 ISD::NodeType ExtendKind) const {
Cameron Zwarich7bbf0ee2011-03-17 14:53:37 +00001663 MVT ReturnMVT;
Cameron Zwarichebe81732011-03-16 22:20:18 +00001664 // TODO: Is this also valid on 32-bit?
1665 if (Subtarget->is64Bit() && VT == MVT::i1 && ExtendKind == ISD::ZERO_EXTEND)
Cameron Zwarich7bbf0ee2011-03-17 14:53:37 +00001666 ReturnMVT = MVT::i8;
1667 else
1668 ReturnMVT = MVT::i32;
1669
1670 EVT MinVT = getRegisterType(Context, ReturnMVT);
1671 return VT.bitsLT(MinVT) ? MinVT : VT;
Cameron Zwarichebe81732011-03-16 22:20:18 +00001672}
1673
Dan Gohman98ca4f22009-08-05 01:29:28 +00001674/// LowerCallResult - Lower the result values of a call into the
1675/// appropriate copies out of appropriate physical registers.
1676///
1677SDValue
1678X86TargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001679 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001680 const SmallVectorImpl<ISD::InputArg> &Ins,
1681 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001682 SmallVectorImpl<SDValue> &InVals) const {
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001683
Chris Lattnere32bbf62007-02-28 07:09:55 +00001684 // Assign locations to each value returned by this call.
Chris Lattner9774c912007-02-27 05:28:59 +00001685 SmallVector<CCValAssign, 16> RVLocs;
Torok Edwin3f142c32009-02-01 18:15:56 +00001686 bool Is64Bit = Subtarget->is64Bit();
Eric Christopher471e4222011-06-08 23:55:35 +00001687 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
Craig Topper0fbf3642012-04-23 03:28:34 +00001688 getTargetMachine(), RVLocs, *DAG.getContext());
Dan Gohman98ca4f22009-08-05 01:29:28 +00001689 CCInfo.AnalyzeCallResult(Ins, RetCC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001690
Chris Lattner3085e152007-02-25 08:59:22 +00001691 // Copy all of the result registers out of their specified physreg.
Chris Lattner8e6da152008-03-10 21:08:41 +00001692 for (unsigned i = 0; i != RVLocs.size(); ++i) {
Dan Gohman37eed792009-02-04 17:28:58 +00001693 CCValAssign &VA = RVLocs[i];
Owen Andersone50ed302009-08-10 22:56:29 +00001694 EVT CopyVT = VA.getValVT();
Scott Michelfdc40a02009-02-17 22:15:04 +00001695
Torok Edwin3f142c32009-02-01 18:15:56 +00001696 // If this is x86-64, and we disabled SSE, we can't return FP values
Owen Anderson825b72b2009-08-11 20:47:22 +00001697 if ((CopyVT == MVT::f32 || CopyVT == MVT::f64) &&
Craig Topper1accb7e2012-01-10 06:54:16 +00001698 ((Is64Bit || Ins[i].Flags.isInReg()) && !Subtarget->hasSSE1())) {
Chris Lattner75361b62010-04-07 22:58:41 +00001699 report_fatal_error("SSE register return with SSE disabled");
Torok Edwin3f142c32009-02-01 18:15:56 +00001700 }
1701
Evan Cheng79fb3b42009-02-20 20:43:02 +00001702 SDValue Val;
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001703
1704 // If this is a call to a function that returns an fp value on the floating
Sylvestre Ledruc8e41c52012-07-23 08:51:15 +00001705 // point stack, we must guarantee the value is popped from the stack, so
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001706 // a CopyFromReg is not good enough - the copy instruction may be eliminated
Jakob Stoklund Olesen9bbe4d62011-06-28 18:32:28 +00001707 // if the return value is not used. We use the FpPOP_RETVAL instruction
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001708 // instead.
1709 if (VA.getLocReg() == X86::ST0 || VA.getLocReg() == X86::ST1) {
1710 // If we prefer to use the value in xmm registers, copy it out as f80 and
1711 // use a truncate to move it from fp stack reg to xmm reg.
1712 if (isScalarFPTypeInSSEReg(VA.getValVT())) CopyVT = MVT::f80;
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001713 SDValue Ops[] = { Chain, InFlag };
Jakob Stoklund Olesen9bbe4d62011-06-28 18:32:28 +00001714 Chain = SDValue(DAG.getMachineNode(X86::FpPOP_RETVAL, dl, CopyVT,
1715 MVT::Other, MVT::Glue, Ops, 2), 1);
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001716 Val = Chain.getValue(0);
1717
1718 // Round the f80 to the right size, which also moves it to the appropriate
1719 // xmm register.
1720 if (CopyVT != VA.getValVT())
1721 Val = DAG.getNode(ISD::FP_ROUND, dl, VA.getValVT(), Val,
1722 // This truncation won't change the value.
1723 DAG.getIntPtrConstant(1));
Evan Cheng79fb3b42009-02-20 20:43:02 +00001724 } else {
1725 Chain = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(),
1726 CopyVT, InFlag).getValue(1);
1727 Val = Chain.getValue(0);
1728 }
Chris Lattner8e6da152008-03-10 21:08:41 +00001729 InFlag = Chain.getValue(2);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001730 InVals.push_back(Val);
Chris Lattner3085e152007-02-25 08:59:22 +00001731 }
Duncan Sands4bdcb612008-07-02 17:40:58 +00001732
Dan Gohman98ca4f22009-08-05 01:29:28 +00001733 return Chain;
Chris Lattner2b02a442007-02-25 08:29:00 +00001734}
1735
1736
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001737//===----------------------------------------------------------------------===//
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001738// C & StdCall & Fast Calling Convention implementation
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001739//===----------------------------------------------------------------------===//
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001740// StdCall calling convention seems to be standard for many Windows' API
1741// routines and around. It differs from C calling convention just a little:
1742// callee should clean up the stack, not caller. Symbols should be also
1743// decorated in some fancy way :) It doesn't support any vector arguments.
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001744// For info on fast calling convention see Fast Calling Convention (tail call)
1745// implementation LowerX86_32FastCCCallTo.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001746
Dan Gohman98ca4f22009-08-05 01:29:28 +00001747/// CallIsStructReturn - Determines whether a call uses struct return
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001748/// semantics.
Rafael Espindola1cee7102012-07-25 13:41:10 +00001749enum StructReturnType {
1750 NotStructReturn,
1751 RegStructReturn,
1752 StackStructReturn
1753};
1754static StructReturnType
1755callIsStructReturn(const SmallVectorImpl<ISD::OutputArg> &Outs) {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001756 if (Outs.empty())
Rafael Espindola1cee7102012-07-25 13:41:10 +00001757 return NotStructReturn;
Duncan Sands276dcbd2008-03-21 09:14:45 +00001758
Rafael Espindola1cee7102012-07-25 13:41:10 +00001759 const ISD::ArgFlagsTy &Flags = Outs[0].Flags;
1760 if (!Flags.isSRet())
1761 return NotStructReturn;
1762 if (Flags.isInReg())
1763 return RegStructReturn;
1764 return StackStructReturn;
Gordon Henriksen86737662008-01-05 16:56:59 +00001765}
1766
Dan Gohman7e77b0f2009-08-01 19:14:37 +00001767/// ArgsAreStructReturn - Determines whether a function uses struct
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001768/// return semantics.
Rafael Espindola1cee7102012-07-25 13:41:10 +00001769static StructReturnType
1770argsAreStructReturn(const SmallVectorImpl<ISD::InputArg> &Ins) {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001771 if (Ins.empty())
Rafael Espindola1cee7102012-07-25 13:41:10 +00001772 return NotStructReturn;
Duncan Sands276dcbd2008-03-21 09:14:45 +00001773
Rafael Espindola1cee7102012-07-25 13:41:10 +00001774 const ISD::ArgFlagsTy &Flags = Ins[0].Flags;
1775 if (!Flags.isSRet())
1776 return NotStructReturn;
1777 if (Flags.isInReg())
1778 return RegStructReturn;
1779 return StackStructReturn;
Gordon Henriksen86737662008-01-05 16:56:59 +00001780}
1781
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001782/// CreateCopyOfByValArgument - Make a copy of an aggregate at address specified
1783/// by "Src" to address "Dst" with size and alignment information specified by
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001784/// the specific parameter attribute. The copy will be passed as a byval
1785/// function parameter.
Scott Michelfdc40a02009-02-17 22:15:04 +00001786static SDValue
Dan Gohman475871a2008-07-27 21:46:04 +00001787CreateCopyOfByValArgument(SDValue Src, SDValue Dst, SDValue Chain,
Dale Johannesendd64c412009-02-04 00:33:20 +00001788 ISD::ArgFlagsTy Flags, SelectionDAG &DAG,
1789 DebugLoc dl) {
Chris Lattnere72f2022010-09-21 05:40:29 +00001790 SDValue SizeNode = DAG.getConstant(Flags.getByValSize(), MVT::i32);
Michael J. Spencerec38de22010-10-10 22:04:20 +00001791
Dale Johannesendd64c412009-02-04 00:33:20 +00001792 return DAG.getMemcpy(Chain, dl, Dst, Src, SizeNode, Flags.getByValAlign(),
Stuart Hastings03d58262011-03-10 00:25:53 +00001793 /*isVolatile*/false, /*AlwaysInline=*/true,
Chris Lattnerfc448ff2010-09-21 18:51:21 +00001794 MachinePointerInfo(), MachinePointerInfo());
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001795}
1796
Chris Lattner29689432010-03-11 00:22:57 +00001797/// IsTailCallConvention - Return true if the calling convention is one that
1798/// supports tail call optimization.
1799static bool IsTailCallConvention(CallingConv::ID CC) {
1800 return (CC == CallingConv::Fast || CC == CallingConv::GHC);
1801}
1802
Evan Cheng485fafc2011-03-21 01:19:09 +00001803bool X86TargetLowering::mayBeEmittedAsTailCall(CallInst *CI) const {
Nick Lewycky22de16d2012-01-19 00:34:10 +00001804 if (!CI->isTailCall() || getTargetMachine().Options.DisableTailCalls)
Evan Cheng485fafc2011-03-21 01:19:09 +00001805 return false;
1806
1807 CallSite CS(CI);
1808 CallingConv::ID CalleeCC = CS.getCallingConv();
1809 if (!IsTailCallConvention(CalleeCC) && CalleeCC != CallingConv::C)
1810 return false;
1811
1812 return true;
1813}
1814
Evan Cheng0c439eb2010-01-27 00:07:07 +00001815/// FuncIsMadeTailCallSafe - Return true if the function is being made into
1816/// a tailcall target by changing its ABI.
Nick Lewycky8a8d4792011-12-02 22:16:29 +00001817static bool FuncIsMadeTailCallSafe(CallingConv::ID CC,
1818 bool GuaranteedTailCallOpt) {
Chris Lattner29689432010-03-11 00:22:57 +00001819 return GuaranteedTailCallOpt && IsTailCallConvention(CC);
Evan Cheng0c439eb2010-01-27 00:07:07 +00001820}
1821
Dan Gohman98ca4f22009-08-05 01:29:28 +00001822SDValue
1823X86TargetLowering::LowerMemArgument(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001824 CallingConv::ID CallConv,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001825 const SmallVectorImpl<ISD::InputArg> &Ins,
1826 DebugLoc dl, SelectionDAG &DAG,
1827 const CCValAssign &VA,
1828 MachineFrameInfo *MFI,
Dan Gohmand858e902010-04-17 15:26:15 +00001829 unsigned i) const {
Rafael Espindola7effac52007-09-14 15:48:13 +00001830 // Create the nodes corresponding to a load from this parameter slot.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001831 ISD::ArgFlagsTy Flags = Ins[i].Flags;
Nick Lewycky8a8d4792011-12-02 22:16:29 +00001832 bool AlwaysUseMutable = FuncIsMadeTailCallSafe(CallConv,
1833 getTargetMachine().Options.GuaranteedTailCallOpt);
Duncan Sands276dcbd2008-03-21 09:14:45 +00001834 bool isImmutable = !AlwaysUseMutable && !Flags.isByVal();
Anton Korobeynikov22472762009-08-14 18:19:10 +00001835 EVT ValVT;
1836
1837 // If value is passed by pointer we have address passed instead of the value
1838 // itself.
1839 if (VA.getLocInfo() == CCValAssign::Indirect)
1840 ValVT = VA.getLocVT();
1841 else
1842 ValVT = VA.getValVT();
Evan Chenge70bb592008-01-10 02:24:25 +00001843
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001844 // FIXME: For now, all byval parameter objects are marked mutable. This can be
Scott Michelfdc40a02009-02-17 22:15:04 +00001845 // changed with more analysis.
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001846 // In case of tail call optimization mark all arguments mutable. Since they
1847 // could be overwritten by lowering of arguments in case of a tail call.
Evan Cheng90567c32010-02-02 23:58:13 +00001848 if (Flags.isByVal()) {
Evan Chengee2e0e32011-03-30 23:44:13 +00001849 unsigned Bytes = Flags.getByValSize();
1850 if (Bytes == 0) Bytes = 1; // Don't create zero-sized stack objects.
1851 int FI = MFI->CreateFixedObject(Bytes, VA.getLocMemOffset(), isImmutable);
Evan Cheng90567c32010-02-02 23:58:13 +00001852 return DAG.getFrameIndex(FI, getPointerTy());
1853 } else {
1854 int FI = MFI->CreateFixedObject(ValVT.getSizeInBits()/8,
Evan Chenged2ae132010-07-03 00:40:23 +00001855 VA.getLocMemOffset(), isImmutable);
Evan Cheng90567c32010-02-02 23:58:13 +00001856 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
1857 return DAG.getLoad(ValVT, dl, Chain, FIN,
Chris Lattnere8639032010-09-21 06:22:23 +00001858 MachinePointerInfo::getFixedStack(FI),
Pete Cooperd752e0f2011-11-08 18:42:53 +00001859 false, false, false, 0);
Evan Cheng90567c32010-02-02 23:58:13 +00001860 }
Rafael Espindola7effac52007-09-14 15:48:13 +00001861}
1862
Dan Gohman475871a2008-07-27 21:46:04 +00001863SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00001864X86TargetLowering::LowerFormalArguments(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001865 CallingConv::ID CallConv,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001866 bool isVarArg,
1867 const SmallVectorImpl<ISD::InputArg> &Ins,
1868 DebugLoc dl,
1869 SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001870 SmallVectorImpl<SDValue> &InVals)
1871 const {
Evan Cheng1bc78042006-04-26 01:20:17 +00001872 MachineFunction &MF = DAG.getMachineFunction();
Gordon Henriksen86737662008-01-05 16:56:59 +00001873 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
Scott Michelfdc40a02009-02-17 22:15:04 +00001874
Gordon Henriksen86737662008-01-05 16:56:59 +00001875 const Function* Fn = MF.getFunction();
1876 if (Fn->hasExternalLinkage() &&
1877 Subtarget->isTargetCygMing() &&
1878 Fn->getName() == "main")
1879 FuncInfo->setForceFramePointer(true);
1880
Evan Cheng1bc78042006-04-26 01:20:17 +00001881 MachineFrameInfo *MFI = MF.getFrameInfo();
Gordon Henriksen86737662008-01-05 16:56:59 +00001882 bool Is64Bit = Subtarget->is64Bit();
Eli Friedman9a2478a2012-01-20 00:05:46 +00001883 bool IsWindows = Subtarget->isTargetWindows();
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001884 bool IsWin64 = Subtarget->isTargetWin64();
Gordon Henriksenae636f82008-01-03 16:47:34 +00001885
Chris Lattner29689432010-03-11 00:22:57 +00001886 assert(!(isVarArg && IsTailCallConvention(CallConv)) &&
1887 "Var args not supported with calling convention fastcc or ghc");
Gordon Henriksenae636f82008-01-03 16:47:34 +00001888
Chris Lattner638402b2007-02-28 07:00:42 +00001889 // Assign locations to all of the incoming arguments.
Chris Lattnerf39f7712007-02-28 05:46:49 +00001890 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00001891 CCState CCInfo(CallConv, isVarArg, MF, getTargetMachine(),
Dan Gohman98ca4f22009-08-05 01:29:28 +00001892 ArgLocs, *DAG.getContext());
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00001893
1894 // Allocate shadow area for Win64
1895 if (IsWin64) {
1896 CCInfo.AllocateStack(32, 8);
1897 }
1898
Duncan Sands45907662010-10-31 13:21:44 +00001899 CCInfo.AnalyzeFormalArguments(Ins, CC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001900
Chris Lattnerf39f7712007-02-28 05:46:49 +00001901 unsigned LastVal = ~0U;
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001902 SDValue ArgValue;
Chris Lattnerf39f7712007-02-28 05:46:49 +00001903 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1904 CCValAssign &VA = ArgLocs[i];
1905 // TODO: If an arg is passed in two places (e.g. reg and stack), skip later
1906 // places.
1907 assert(VA.getValNo() != LastVal &&
1908 "Don't support value assigned to multiple locs yet");
Duncan Sands17001ce2011-10-18 12:44:00 +00001909 (void)LastVal;
Chris Lattnerf39f7712007-02-28 05:46:49 +00001910 LastVal = VA.getValNo();
Scott Michelfdc40a02009-02-17 22:15:04 +00001911
Chris Lattnerf39f7712007-02-28 05:46:49 +00001912 if (VA.isRegLoc()) {
Owen Andersone50ed302009-08-10 22:56:29 +00001913 EVT RegVT = VA.getLocVT();
Craig Topper44d23822012-02-22 05:59:10 +00001914 const TargetRegisterClass *RC;
Owen Anderson825b72b2009-08-11 20:47:22 +00001915 if (RegVT == MVT::i32)
Craig Topperc9099502012-04-20 06:31:50 +00001916 RC = &X86::GR32RegClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001917 else if (Is64Bit && RegVT == MVT::i64)
Craig Topperc9099502012-04-20 06:31:50 +00001918 RC = &X86::GR64RegClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001919 else if (RegVT == MVT::f32)
Craig Topperc9099502012-04-20 06:31:50 +00001920 RC = &X86::FR32RegClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001921 else if (RegVT == MVT::f64)
Craig Topperc9099502012-04-20 06:31:50 +00001922 RC = &X86::FR64RegClass;
Craig Topper7a9a28b2012-08-12 02:23:29 +00001923 else if (RegVT.is256BitVector())
Craig Topperc9099502012-04-20 06:31:50 +00001924 RC = &X86::VR256RegClass;
Craig Topper7a9a28b2012-08-12 02:23:29 +00001925 else if (RegVT.is128BitVector())
Craig Topperc9099502012-04-20 06:31:50 +00001926 RC = &X86::VR128RegClass;
Dale Johannesen0488fb62010-09-30 23:57:10 +00001927 else if (RegVT == MVT::x86mmx)
Craig Topperc9099502012-04-20 06:31:50 +00001928 RC = &X86::VR64RegClass;
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001929 else
Torok Edwinc23197a2009-07-14 16:55:14 +00001930 llvm_unreachable("Unknown argument type!");
Gordon Henriksenae636f82008-01-03 16:47:34 +00001931
Devang Patel68e6bee2011-02-21 23:21:26 +00001932 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001933 ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT);
Scott Michelfdc40a02009-02-17 22:15:04 +00001934
Chris Lattnerf39f7712007-02-28 05:46:49 +00001935 // If this is an 8 or 16-bit value, it is really passed promoted to 32
1936 // bits. Insert an assert[sz]ext to capture this, then truncate to the
1937 // right size.
1938 if (VA.getLocInfo() == CCValAssign::SExt)
Dale Johannesenace16102009-02-03 19:33:06 +00001939 ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue,
Chris Lattnerf39f7712007-02-28 05:46:49 +00001940 DAG.getValueType(VA.getValVT()));
1941 else if (VA.getLocInfo() == CCValAssign::ZExt)
Dale Johannesenace16102009-02-03 19:33:06 +00001942 ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue,
Chris Lattnerf39f7712007-02-28 05:46:49 +00001943 DAG.getValueType(VA.getValVT()));
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001944 else if (VA.getLocInfo() == CCValAssign::BCvt)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001945 ArgValue = DAG.getNode(ISD::BITCAST, dl, VA.getValVT(), ArgValue);
Scott Michelfdc40a02009-02-17 22:15:04 +00001946
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001947 if (VA.isExtInLoc()) {
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001948 // Handle MMX values passed in XMM regs.
1949 if (RegVT.isVector()) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00001950 ArgValue = DAG.getNode(X86ISD::MOVDQ2Q, dl, VA.getValVT(),
1951 ArgValue);
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001952 } else
1953 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
Evan Cheng44c0fd12008-04-25 20:13:28 +00001954 }
Chris Lattnerf39f7712007-02-28 05:46:49 +00001955 } else {
1956 assert(VA.isMemLoc());
Dan Gohman98ca4f22009-08-05 01:29:28 +00001957 ArgValue = LowerMemArgument(Chain, CallConv, Ins, dl, DAG, VA, MFI, i);
Evan Cheng1bc78042006-04-26 01:20:17 +00001958 }
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001959
1960 // If value is passed via pointer - do a load.
1961 if (VA.getLocInfo() == CCValAssign::Indirect)
Chris Lattner51abfe42010-09-21 06:02:19 +00001962 ArgValue = DAG.getLoad(VA.getValVT(), dl, Chain, ArgValue,
Pete Cooperd752e0f2011-11-08 18:42:53 +00001963 MachinePointerInfo(), false, false, false, 0);
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001964
Dan Gohman98ca4f22009-08-05 01:29:28 +00001965 InVals.push_back(ArgValue);
Evan Cheng1bc78042006-04-26 01:20:17 +00001966 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001967
Dan Gohman61a92132008-04-21 23:59:07 +00001968 // The x86-64 ABI for returning structs by value requires that we copy
1969 // the sret argument into %rax for the return. Save the argument into
1970 // a virtual register so that we can access it from the return points.
Dan Gohman7e77b0f2009-08-01 19:14:37 +00001971 if (Is64Bit && MF.getFunction()->hasStructRetAttr()) {
Dan Gohman61a92132008-04-21 23:59:07 +00001972 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1973 unsigned Reg = FuncInfo->getSRetReturnReg();
1974 if (!Reg) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001975 Reg = MF.getRegInfo().createVirtualRegister(getRegClassFor(MVT::i64));
Dan Gohman61a92132008-04-21 23:59:07 +00001976 FuncInfo->setSRetReturnReg(Reg);
1977 }
Dan Gohman98ca4f22009-08-05 01:29:28 +00001978 SDValue Copy = DAG.getCopyToReg(DAG.getEntryNode(), dl, Reg, InVals[0]);
Owen Anderson825b72b2009-08-11 20:47:22 +00001979 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Copy, Chain);
Dan Gohman61a92132008-04-21 23:59:07 +00001980 }
1981
Chris Lattnerf39f7712007-02-28 05:46:49 +00001982 unsigned StackSize = CCInfo.getNextStackOffset();
Evan Cheng0c439eb2010-01-27 00:07:07 +00001983 // Align stack specially for tail calls.
Nick Lewycky8a8d4792011-12-02 22:16:29 +00001984 if (FuncIsMadeTailCallSafe(CallConv,
1985 MF.getTarget().Options.GuaranteedTailCallOpt))
Gordon Henriksenae636f82008-01-03 16:47:34 +00001986 StackSize = GetAlignedArgumentStackSize(StackSize, DAG);
Evan Cheng25caf632006-05-23 21:06:34 +00001987
Evan Cheng1bc78042006-04-26 01:20:17 +00001988 // If the function takes variable number of arguments, make a frame index for
1989 // the start of the first vararg value... for expansion of llvm.va_start.
Gordon Henriksenae636f82008-01-03 16:47:34 +00001990 if (isVarArg) {
NAKAMURA Takumi3ca99432011-03-09 11:33:15 +00001991 if (Is64Bit || (CallConv != CallingConv::X86_FastCall &&
1992 CallConv != CallingConv::X86_ThisCall)) {
Jakob Stoklund Olesenb2eeed72010-07-29 17:42:27 +00001993 FuncInfo->setVarArgsFrameIndex(MFI->CreateFixedObject(1, StackSize,true));
Gordon Henriksen86737662008-01-05 16:56:59 +00001994 }
1995 if (Is64Bit) {
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001996 unsigned TotalNumIntRegs = 0, TotalNumXMMRegs = 0;
1997
1998 // FIXME: We should really autogenerate these arrays
Craig Topperc5eaae42012-03-11 07:57:25 +00001999 static const uint16_t GPR64ArgRegsWin64[] = {
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002000 X86::RCX, X86::RDX, X86::R8, X86::R9
Gordon Henriksen86737662008-01-05 16:56:59 +00002001 };
Craig Topperc5eaae42012-03-11 07:57:25 +00002002 static const uint16_t GPR64ArgRegs64Bit[] = {
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002003 X86::RDI, X86::RSI, X86::RDX, X86::RCX, X86::R8, X86::R9
2004 };
Craig Topperc5eaae42012-03-11 07:57:25 +00002005 static const uint16_t XMMArgRegs64Bit[] = {
Gordon Henriksen86737662008-01-05 16:56:59 +00002006 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
2007 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
2008 };
Craig Topperc5eaae42012-03-11 07:57:25 +00002009 const uint16_t *GPR64ArgRegs;
Anton Korobeynikove7beda12010-10-03 22:52:07 +00002010 unsigned NumXMMRegs = 0;
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002011
2012 if (IsWin64) {
Anton Korobeynikove7beda12010-10-03 22:52:07 +00002013 // The XMM registers which might contain var arg parameters are shadowed
2014 // in their paired GPR. So we only need to save the GPR to their home
2015 // slots.
2016 TotalNumIntRegs = 4;
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002017 GPR64ArgRegs = GPR64ArgRegsWin64;
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002018 } else {
2019 TotalNumIntRegs = 6; TotalNumXMMRegs = 8;
2020 GPR64ArgRegs = GPR64ArgRegs64Bit;
Anton Korobeynikove7beda12010-10-03 22:52:07 +00002021
Chad Rosier30450e82011-12-22 22:35:21 +00002022 NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs64Bit,
2023 TotalNumXMMRegs);
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002024 }
2025 unsigned NumIntRegs = CCInfo.getFirstUnallocated(GPR64ArgRegs,
2026 TotalNumIntRegs);
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002027
Bill Wendling67658342012-10-09 07:45:08 +00002028 bool NoImplicitFloatOps = Fn->getFnAttributes().
2029 hasAttribute(Attributes::NoImplicitFloat);
Craig Topper1accb7e2012-01-10 06:54:16 +00002030 assert(!(NumXMMRegs && !Subtarget->hasSSE1()) &&
Torok Edwin3f142c32009-02-01 18:15:56 +00002031 "SSE register cannot be used when SSE is disabled!");
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002032 assert(!(NumXMMRegs && MF.getTarget().Options.UseSoftFloat &&
2033 NoImplicitFloatOps) &&
Evan Chengc7ce29b2009-02-13 22:36:38 +00002034 "SSE register cannot be used when SSE is disabled!");
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002035 if (MF.getTarget().Options.UseSoftFloat || NoImplicitFloatOps ||
Craig Topper1accb7e2012-01-10 06:54:16 +00002036 !Subtarget->hasSSE1())
Torok Edwin3f142c32009-02-01 18:15:56 +00002037 // Kernel mode asks for SSE to be disabled, so don't push them
2038 // on the stack.
2039 TotalNumXMMRegs = 0;
Bill Wendlingf9abd7e2009-03-11 22:30:01 +00002040
Anton Korobeynikove7beda12010-10-03 22:52:07 +00002041 if (IsWin64) {
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00002042 const TargetFrameLowering &TFI = *getTargetMachine().getFrameLowering();
Cameron Esfahaniec37b002010-10-08 19:24:18 +00002043 // Get to the caller-allocated home save location. Add 8 to account
2044 // for the return address.
2045 int HomeOffset = TFI.getOffsetOfLocalArea() + 8;
Anton Korobeynikove7beda12010-10-03 22:52:07 +00002046 FuncInfo->setRegSaveFrameIndex(
Cameron Esfahaniec37b002010-10-08 19:24:18 +00002047 MFI->CreateFixedObject(1, NumIntRegs * 8 + HomeOffset, false));
NAKAMURA Takumi3ca99432011-03-09 11:33:15 +00002048 // Fixup to set vararg frame on shadow area (4 x i64).
2049 if (NumIntRegs < 4)
2050 FuncInfo->setVarArgsFrameIndex(FuncInfo->getRegSaveFrameIndex());
Anton Korobeynikove7beda12010-10-03 22:52:07 +00002051 } else {
2052 // For X86-64, if there are vararg parameters that are passed via
Chad Rosier30450e82011-12-22 22:35:21 +00002053 // registers, then we must store them to their spots on the stack so
2054 // they may be loaded by deferencing the result of va_next.
Anton Korobeynikove7beda12010-10-03 22:52:07 +00002055 FuncInfo->setVarArgsGPOffset(NumIntRegs * 8);
2056 FuncInfo->setVarArgsFPOffset(TotalNumIntRegs * 8 + NumXMMRegs * 16);
2057 FuncInfo->setRegSaveFrameIndex(
2058 MFI->CreateStackObject(TotalNumIntRegs * 8 + TotalNumXMMRegs * 16, 16,
Dan Gohman1e93df62010-04-17 14:41:14 +00002059 false));
Anton Korobeynikove7beda12010-10-03 22:52:07 +00002060 }
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002061
Gordon Henriksen86737662008-01-05 16:56:59 +00002062 // Store the integer parameter registers.
Dan Gohman475871a2008-07-27 21:46:04 +00002063 SmallVector<SDValue, 8> MemOps;
Dan Gohman1e93df62010-04-17 14:41:14 +00002064 SDValue RSFIN = DAG.getFrameIndex(FuncInfo->getRegSaveFrameIndex(),
2065 getPointerTy());
2066 unsigned Offset = FuncInfo->getVarArgsGPOffset();
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002067 for (; NumIntRegs != TotalNumIntRegs; ++NumIntRegs) {
Dan Gohmand6708ea2009-08-15 01:38:56 +00002068 SDValue FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), RSFIN,
2069 DAG.getIntPtrConstant(Offset));
Bob Wilson998e1252009-04-20 18:36:57 +00002070 unsigned VReg = MF.addLiveIn(GPR64ArgRegs[NumIntRegs],
Craig Topperc9099502012-04-20 06:31:50 +00002071 &X86::GR64RegClass);
Owen Anderson825b72b2009-08-11 20:47:22 +00002072 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i64);
Dan Gohman475871a2008-07-27 21:46:04 +00002073 SDValue Store =
Dale Johannesenace16102009-02-03 19:33:06 +00002074 DAG.getStore(Val.getValue(1), dl, Val, FIN,
Chris Lattnere8639032010-09-21 06:22:23 +00002075 MachinePointerInfo::getFixedStack(
2076 FuncInfo->getRegSaveFrameIndex(), Offset),
2077 false, false, 0);
Gordon Henriksen86737662008-01-05 16:56:59 +00002078 MemOps.push_back(Store);
Dan Gohmand6708ea2009-08-15 01:38:56 +00002079 Offset += 8;
Gordon Henriksen86737662008-01-05 16:56:59 +00002080 }
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002081
Dan Gohmanface41a2009-08-16 21:24:25 +00002082 if (TotalNumXMMRegs != 0 && NumXMMRegs != TotalNumXMMRegs) {
2083 // Now store the XMM (fp + vector) parameter registers.
2084 SmallVector<SDValue, 11> SaveXMMOps;
2085 SaveXMMOps.push_back(Chain);
Dan Gohmand6708ea2009-08-15 01:38:56 +00002086
Craig Topperc9099502012-04-20 06:31:50 +00002087 unsigned AL = MF.addLiveIn(X86::AL, &X86::GR8RegClass);
Dan Gohmanface41a2009-08-16 21:24:25 +00002088 SDValue ALVal = DAG.getCopyFromReg(DAG.getEntryNode(), dl, AL, MVT::i8);
2089 SaveXMMOps.push_back(ALVal);
Dan Gohmand6708ea2009-08-15 01:38:56 +00002090
Dan Gohman1e93df62010-04-17 14:41:14 +00002091 SaveXMMOps.push_back(DAG.getIntPtrConstant(
2092 FuncInfo->getRegSaveFrameIndex()));
2093 SaveXMMOps.push_back(DAG.getIntPtrConstant(
2094 FuncInfo->getVarArgsFPOffset()));
Dan Gohmand6708ea2009-08-15 01:38:56 +00002095
Dan Gohmanface41a2009-08-16 21:24:25 +00002096 for (; NumXMMRegs != TotalNumXMMRegs; ++NumXMMRegs) {
Anton Korobeynikove7beda12010-10-03 22:52:07 +00002097 unsigned VReg = MF.addLiveIn(XMMArgRegs64Bit[NumXMMRegs],
Craig Topperc9099502012-04-20 06:31:50 +00002098 &X86::VR128RegClass);
Dan Gohmanface41a2009-08-16 21:24:25 +00002099 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::v4f32);
2100 SaveXMMOps.push_back(Val);
2101 }
2102 MemOps.push_back(DAG.getNode(X86ISD::VASTART_SAVE_XMM_REGS, dl,
2103 MVT::Other,
2104 &SaveXMMOps[0], SaveXMMOps.size()));
Gordon Henriksen86737662008-01-05 16:56:59 +00002105 }
Dan Gohmanface41a2009-08-16 21:24:25 +00002106
2107 if (!MemOps.empty())
2108 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
2109 &MemOps[0], MemOps.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00002110 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00002111 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002112
Gordon Henriksen86737662008-01-05 16:56:59 +00002113 // Some CCs need callee pop.
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002114 if (X86::isCalleePop(CallConv, Is64Bit, isVarArg,
2115 MF.getTarget().Options.GuaranteedTailCallOpt)) {
Dan Gohman1e93df62010-04-17 14:41:14 +00002116 FuncInfo->setBytesToPopOnReturn(StackSize); // Callee pops everything.
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00002117 } else {
Dan Gohman1e93df62010-04-17 14:41:14 +00002118 FuncInfo->setBytesToPopOnReturn(0); // Callee pops nothing.
Chris Lattnerf39f7712007-02-28 05:46:49 +00002119 // If this is an sret function, the return should pop the hidden pointer.
Eli Friedman9a2478a2012-01-20 00:05:46 +00002120 if (!Is64Bit && !IsTailCallConvention(CallConv) && !IsWindows &&
Rafael Espindola1cee7102012-07-25 13:41:10 +00002121 argsAreStructReturn(Ins) == StackStructReturn)
Dan Gohman1e93df62010-04-17 14:41:14 +00002122 FuncInfo->setBytesToPopOnReturn(4);
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00002123 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00002124
Gordon Henriksen86737662008-01-05 16:56:59 +00002125 if (!Is64Bit) {
Dan Gohman1e93df62010-04-17 14:41:14 +00002126 // RegSaveFrameIndex is X86-64 only.
2127 FuncInfo->setRegSaveFrameIndex(0xAAAAAAA);
Anton Korobeynikovded05e32010-05-16 09:08:45 +00002128 if (CallConv == CallingConv::X86_FastCall ||
2129 CallConv == CallingConv::X86_ThisCall)
Dan Gohman1e93df62010-04-17 14:41:14 +00002130 // fastcc functions can't have varargs.
2131 FuncInfo->setVarArgsFrameIndex(0xAAAAAAA);
Gordon Henriksen86737662008-01-05 16:56:59 +00002132 }
Evan Cheng25caf632006-05-23 21:06:34 +00002133
Rafael Espindola76927d752011-08-30 19:39:58 +00002134 FuncInfo->setArgumentStackSize(StackSize);
2135
Dan Gohman98ca4f22009-08-05 01:29:28 +00002136 return Chain;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002137}
2138
Dan Gohman475871a2008-07-27 21:46:04 +00002139SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00002140X86TargetLowering::LowerMemOpCallTo(SDValue Chain,
2141 SDValue StackPtr, SDValue Arg,
2142 DebugLoc dl, SelectionDAG &DAG,
Evan Chengdffbd832008-01-10 00:09:10 +00002143 const CCValAssign &VA,
Dan Gohmand858e902010-04-17 15:26:15 +00002144 ISD::ArgFlagsTy Flags) const {
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00002145 unsigned LocMemOffset = VA.getLocMemOffset();
Dan Gohman475871a2008-07-27 21:46:04 +00002146 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset);
Dale Johannesenace16102009-02-03 19:33:06 +00002147 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, PtrOff);
Chris Lattnerfc448ff2010-09-21 18:51:21 +00002148 if (Flags.isByVal())
Dale Johannesendd64c412009-02-04 00:33:20 +00002149 return CreateCopyOfByValArgument(Arg, PtrOff, Chain, Flags, DAG, dl);
Chris Lattnerfc448ff2010-09-21 18:51:21 +00002150
2151 return DAG.getStore(Chain, dl, Arg, PtrOff,
2152 MachinePointerInfo::getStack(LocMemOffset),
David Greene67c9d422010-02-15 16:53:33 +00002153 false, false, 0);
Evan Chengdffbd832008-01-10 00:09:10 +00002154}
2155
Bill Wendling64e87322009-01-16 19:25:27 +00002156/// EmitTailCallLoadRetAddr - Emit a load of return address if tail call
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002157/// optimization is performed and it is required.
Scott Michelfdc40a02009-02-17 22:15:04 +00002158SDValue
2159X86TargetLowering::EmitTailCallLoadRetAddr(SelectionDAG &DAG,
Evan Chengddc419c2010-01-26 19:04:47 +00002160 SDValue &OutRetAddr, SDValue Chain,
2161 bool IsTailCall, bool Is64Bit,
Dan Gohmand858e902010-04-17 15:26:15 +00002162 int FPDiff, DebugLoc dl) const {
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002163 // Adjust the Return address stack slot.
Owen Andersone50ed302009-08-10 22:56:29 +00002164 EVT VT = getPointerTy();
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002165 OutRetAddr = getReturnAddressFrameIndex(DAG);
Bill Wendling64e87322009-01-16 19:25:27 +00002166
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002167 // Load the "old" Return address.
Chris Lattner51abfe42010-09-21 06:02:19 +00002168 OutRetAddr = DAG.getLoad(VT, dl, Chain, OutRetAddr, MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002169 false, false, false, 0);
Gabor Greifba36cb52008-08-28 21:40:38 +00002170 return SDValue(OutRetAddr.getNode(), 1);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002171}
2172
Chris Lattner7a2bdde2011-04-15 05:18:47 +00002173/// EmitTailCallStoreRetAddr - Emit a store of the return address if tail call
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002174/// optimization is performed and it is required (FPDiff!=0).
Scott Michelfdc40a02009-02-17 22:15:04 +00002175static SDValue
2176EmitTailCallStoreRetAddr(SelectionDAG & DAG, MachineFunction &MF,
Dan Gohman475871a2008-07-27 21:46:04 +00002177 SDValue Chain, SDValue RetAddrFrIdx,
Dale Johannesenace16102009-02-03 19:33:06 +00002178 bool Is64Bit, int FPDiff, DebugLoc dl) {
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002179 // Store the return address to the appropriate stack slot.
2180 if (!FPDiff) return Chain;
2181 // Calculate the new stack slot for the return address.
2182 int SlotSize = Is64Bit ? 8 : 4;
Scott Michelfdc40a02009-02-17 22:15:04 +00002183 int NewReturnAddrFI =
Evan Chenged2ae132010-07-03 00:40:23 +00002184 MF.getFrameInfo()->CreateFixedObject(SlotSize, FPDiff-SlotSize, false);
Owen Anderson825b72b2009-08-11 20:47:22 +00002185 EVT VT = Is64Bit ? MVT::i64 : MVT::i32;
Dan Gohman475871a2008-07-27 21:46:04 +00002186 SDValue NewRetAddrFrIdx = DAG.getFrameIndex(NewReturnAddrFI, VT);
Scott Michelfdc40a02009-02-17 22:15:04 +00002187 Chain = DAG.getStore(Chain, dl, RetAddrFrIdx, NewRetAddrFrIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00002188 MachinePointerInfo::getFixedStack(NewReturnAddrFI),
David Greene67c9d422010-02-15 16:53:33 +00002189 false, false, 0);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002190 return Chain;
2191}
2192
Dan Gohman98ca4f22009-08-05 01:29:28 +00002193SDValue
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00002194X86TargetLowering::LowerCall(TargetLowering::CallLoweringInfo &CLI,
Dan Gohmand858e902010-04-17 15:26:15 +00002195 SmallVectorImpl<SDValue> &InVals) const {
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00002196 SelectionDAG &DAG = CLI.DAG;
2197 DebugLoc &dl = CLI.DL;
2198 SmallVector<ISD::OutputArg, 32> &Outs = CLI.Outs;
2199 SmallVector<SDValue, 32> &OutVals = CLI.OutVals;
2200 SmallVector<ISD::InputArg, 32> &Ins = CLI.Ins;
2201 SDValue Chain = CLI.Chain;
2202 SDValue Callee = CLI.Callee;
2203 CallingConv::ID CallConv = CLI.CallConv;
2204 bool &isTailCall = CLI.IsTailCall;
2205 bool isVarArg = CLI.IsVarArg;
2206
Dan Gohman98ca4f22009-08-05 01:29:28 +00002207 MachineFunction &MF = DAG.getMachineFunction();
2208 bool Is64Bit = Subtarget->is64Bit();
NAKAMURA Takumifb840c92011-02-05 15:11:13 +00002209 bool IsWin64 = Subtarget->isTargetWin64();
Eli Friedman9a2478a2012-01-20 00:05:46 +00002210 bool IsWindows = Subtarget->isTargetWindows();
Rafael Espindola1cee7102012-07-25 13:41:10 +00002211 StructReturnType SR = callIsStructReturn(Outs);
Evan Cheng5f941932010-02-05 02:21:12 +00002212 bool IsSibcall = false;
Dan Gohman98ca4f22009-08-05 01:29:28 +00002213
Nick Lewycky22de16d2012-01-19 00:34:10 +00002214 if (MF.getTarget().Options.DisableTailCalls)
2215 isTailCall = false;
2216
Evan Cheng5f941932010-02-05 02:21:12 +00002217 if (isTailCall) {
Evan Cheng0c439eb2010-01-27 00:07:07 +00002218 // Check if it's really possible to do a tail call.
Evan Chenga375d472010-03-15 18:54:48 +00002219 isTailCall = IsEligibleForTailCallOptimization(Callee, CallConv,
Rafael Espindola1cee7102012-07-25 13:41:10 +00002220 isVarArg, SR != NotStructReturn,
Evan Chengb1cacc72012-09-25 05:32:34 +00002221 MF.getFunction()->hasStructRetAttr(), CLI.RetTy,
Rafael Espindola1cee7102012-07-25 13:41:10 +00002222 Outs, OutVals, Ins, DAG);
Evan Chengf22f9b32010-02-06 03:28:46 +00002223
2224 // Sibcalls are automatically detected tailcalls which do not require
2225 // ABI changes.
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002226 if (!MF.getTarget().Options.GuaranteedTailCallOpt && isTailCall)
Evan Cheng5f941932010-02-05 02:21:12 +00002227 IsSibcall = true;
Evan Chengf22f9b32010-02-06 03:28:46 +00002228
2229 if (isTailCall)
2230 ++NumTailCalls;
Evan Cheng5f941932010-02-05 02:21:12 +00002231 }
Evan Cheng0c439eb2010-01-27 00:07:07 +00002232
Chris Lattner29689432010-03-11 00:22:57 +00002233 assert(!(isVarArg && IsTailCallConvention(CallConv)) &&
2234 "Var args not supported with calling convention fastcc or ghc");
Gordon Henriksenae636f82008-01-03 16:47:34 +00002235
Chris Lattner638402b2007-02-28 07:00:42 +00002236 // Analyze operands of the call, assigning locations to each operand.
Chris Lattner423c5f42007-02-28 05:31:48 +00002237 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002238 CCState CCInfo(CallConv, isVarArg, MF, getTargetMachine(),
Dan Gohman98ca4f22009-08-05 01:29:28 +00002239 ArgLocs, *DAG.getContext());
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00002240
2241 // Allocate shadow area for Win64
2242 if (IsWin64) {
2243 CCInfo.AllocateStack(32, 8);
2244 }
2245
Duncan Sands45907662010-10-31 13:21:44 +00002246 CCInfo.AnalyzeCallOperands(Outs, CC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00002247
Chris Lattner423c5f42007-02-28 05:31:48 +00002248 // Get a count of how many bytes are to be pushed on the stack.
2249 unsigned NumBytes = CCInfo.getNextStackOffset();
Evan Chengf22f9b32010-02-06 03:28:46 +00002250 if (IsSibcall)
Evan Chengb2c92902010-02-02 02:22:50 +00002251 // This is a sibcall. The memory operands are available in caller's
2252 // own caller's stack.
2253 NumBytes = 0;
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002254 else if (getTargetMachine().Options.GuaranteedTailCallOpt &&
2255 IsTailCallConvention(CallConv))
Evan Chengf22f9b32010-02-06 03:28:46 +00002256 NumBytes = GetAlignedArgumentStackSize(NumBytes, DAG);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002257
Gordon Henriksen86737662008-01-05 16:56:59 +00002258 int FPDiff = 0;
Evan Chengf22f9b32010-02-06 03:28:46 +00002259 if (isTailCall && !IsSibcall) {
Gordon Henriksen86737662008-01-05 16:56:59 +00002260 // Lower arguments at fp - stackoffset + fpdiff.
Scott Michelfdc40a02009-02-17 22:15:04 +00002261 unsigned NumBytesCallerPushed =
Gordon Henriksen86737662008-01-05 16:56:59 +00002262 MF.getInfo<X86MachineFunctionInfo>()->getBytesToPopOnReturn();
2263 FPDiff = NumBytesCallerPushed - NumBytes;
2264
2265 // Set the delta of movement of the returnaddr stackslot.
2266 // But only set if delta is greater than previous delta.
2267 if (FPDiff < (MF.getInfo<X86MachineFunctionInfo>()->getTCReturnAddrDelta()))
2268 MF.getInfo<X86MachineFunctionInfo>()->setTCReturnAddrDelta(FPDiff);
2269 }
2270
Evan Chengf22f9b32010-02-06 03:28:46 +00002271 if (!IsSibcall)
2272 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002273
Dan Gohman475871a2008-07-27 21:46:04 +00002274 SDValue RetAddrFrIdx;
Chris Lattner7a2bdde2011-04-15 05:18:47 +00002275 // Load return address for tail calls.
Evan Chengf22f9b32010-02-06 03:28:46 +00002276 if (isTailCall && FPDiff)
2277 Chain = EmitTailCallLoadRetAddr(DAG, RetAddrFrIdx, Chain, isTailCall,
2278 Is64Bit, FPDiff, dl);
Gordon Henriksen86737662008-01-05 16:56:59 +00002279
Dan Gohman475871a2008-07-27 21:46:04 +00002280 SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass;
2281 SmallVector<SDValue, 8> MemOpChains;
2282 SDValue StackPtr;
Chris Lattner423c5f42007-02-28 05:31:48 +00002283
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002284 // Walk the register/memloc assignments, inserting copies/loads. In the case
2285 // of tail call optimization arguments are handle later.
Chris Lattner423c5f42007-02-28 05:31:48 +00002286 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2287 CCValAssign &VA = ArgLocs[i];
Owen Andersone50ed302009-08-10 22:56:29 +00002288 EVT RegVT = VA.getLocVT();
Dan Gohmanc9403652010-07-07 15:54:55 +00002289 SDValue Arg = OutVals[i];
Dan Gohman98ca4f22009-08-05 01:29:28 +00002290 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Dan Gohman095cc292008-09-13 01:54:27 +00002291 bool isByVal = Flags.isByVal();
Scott Michelfdc40a02009-02-17 22:15:04 +00002292
Chris Lattner423c5f42007-02-28 05:31:48 +00002293 // Promote the value if needed.
2294 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002295 default: llvm_unreachable("Unknown loc info!");
Chris Lattner423c5f42007-02-28 05:31:48 +00002296 case CCValAssign::Full: break;
2297 case CCValAssign::SExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002298 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00002299 break;
2300 case CCValAssign::ZExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002301 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00002302 break;
2303 case CCValAssign::AExt:
Craig Topper7a9a28b2012-08-12 02:23:29 +00002304 if (RegVT.is128BitVector()) {
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002305 // Special case: passing MMX values in XMM registers.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002306 Arg = DAG.getNode(ISD::BITCAST, dl, MVT::i64, Arg);
Owen Anderson825b72b2009-08-11 20:47:22 +00002307 Arg = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i64, Arg);
2308 Arg = getMOVL(DAG, dl, MVT::v2i64, DAG.getUNDEF(MVT::v2i64), Arg);
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002309 } else
2310 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, RegVT, Arg);
2311 break;
2312 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002313 Arg = DAG.getNode(ISD::BITCAST, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00002314 break;
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00002315 case CCValAssign::Indirect: {
2316 // Store the argument.
2317 SDValue SpillSlot = DAG.CreateStackTemporary(VA.getValVT());
Evan Chengff89dcb2009-10-18 18:16:27 +00002318 int FI = cast<FrameIndexSDNode>(SpillSlot)->getIndex();
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00002319 Chain = DAG.getStore(Chain, dl, Arg, SpillSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00002320 MachinePointerInfo::getFixedStack(FI),
David Greene67c9d422010-02-15 16:53:33 +00002321 false, false, 0);
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00002322 Arg = SpillSlot;
2323 break;
2324 }
Evan Cheng6b5783d2006-05-25 18:56:34 +00002325 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002326
Chris Lattner423c5f42007-02-28 05:31:48 +00002327 if (VA.isRegLoc()) {
Stuart Hastings2aa0f232011-05-26 04:09:49 +00002328 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
2329 if (isVarArg && IsWin64) {
2330 // Win64 ABI requires argument XMM reg to be copied to the corresponding
2331 // shadow reg if callee is a varargs function.
2332 unsigned ShadowReg = 0;
2333 switch (VA.getLocReg()) {
2334 case X86::XMM0: ShadowReg = X86::RCX; break;
2335 case X86::XMM1: ShadowReg = X86::RDX; break;
2336 case X86::XMM2: ShadowReg = X86::R8; break;
2337 case X86::XMM3: ShadowReg = X86::R9; break;
Anton Korobeynikovc52bedb2010-08-27 14:43:06 +00002338 }
Stuart Hastings2aa0f232011-05-26 04:09:49 +00002339 if (ShadowReg)
2340 RegsToPass.push_back(std::make_pair(ShadowReg, Arg));
Anton Korobeynikovc52bedb2010-08-27 14:43:06 +00002341 }
Evan Chengf22f9b32010-02-06 03:28:46 +00002342 } else if (!IsSibcall && (!isTailCall || isByVal)) {
Evan Cheng5f941932010-02-05 02:21:12 +00002343 assert(VA.isMemLoc());
2344 if (StackPtr.getNode() == 0)
2345 StackPtr = DAG.getCopyFromReg(Chain, dl, X86StackPtr, getPointerTy());
2346 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Arg,
2347 dl, DAG, VA, Flags));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002348 }
Stuart Hastings2aa0f232011-05-26 04:09:49 +00002349 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002350
Evan Cheng32fe1032006-05-25 00:59:30 +00002351 if (!MemOpChains.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00002352 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002353 &MemOpChains[0], MemOpChains.size());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002354
Chris Lattner88e1fd52009-07-09 04:24:46 +00002355 if (Subtarget->isPICStyleGOT()) {
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002356 // ELF / PIC requires GOT in the EBX register before function calls via PLT
2357 // GOT pointer.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002358 if (!isTailCall) {
Jakob Stoklund Olesenb8720782012-07-04 19:28:31 +00002359 RegsToPass.push_back(std::make_pair(unsigned(X86::EBX),
2360 DAG.getNode(X86ISD::GlobalBaseReg, DebugLoc(), getPointerTy())));
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002361 } else {
2362 // If we are tail calling and generating PIC/GOT style code load the
2363 // address of the callee into ECX. The value in ecx is used as target of
2364 // the tail jump. This is done to circumvent the ebx/callee-saved problem
2365 // for tail calls on PIC/GOT architectures. Normally we would just put the
2366 // address of GOT into ebx and then call target@PLT. But for tail calls
2367 // ebx would be restored (since ebx is callee saved) before jumping to the
2368 // target@PLT.
2369
2370 // Note: The actual moving to ECX is done further down.
2371 GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee);
2372 if (G && !G->getGlobal()->hasHiddenVisibility() &&
2373 !G->getGlobal()->hasProtectedVisibility())
2374 Callee = LowerGlobalAddress(Callee, DAG);
2375 else if (isa<ExternalSymbolSDNode>(Callee))
Chris Lattner15a380a2009-07-09 04:39:06 +00002376 Callee = LowerExternalSymbol(Callee, DAG);
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002377 }
Anton Korobeynikov7f705592007-01-12 19:20:47 +00002378 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00002379
NAKAMURA Takumifb840c92011-02-05 15:11:13 +00002380 if (Is64Bit && isVarArg && !IsWin64) {
Gordon Henriksen86737662008-01-05 16:56:59 +00002381 // From AMD64 ABI document:
2382 // For calls that may call functions that use varargs or stdargs
2383 // (prototype-less calls or calls to functions containing ellipsis (...) in
2384 // the declaration) %al is used as hidden argument to specify the number
2385 // of SSE registers used. The contents of %al do not need to match exactly
2386 // the number of registers, but must be an ubound on the number of SSE
2387 // registers used and is in the range 0 - 8 inclusive.
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002388
Gordon Henriksen86737662008-01-05 16:56:59 +00002389 // Count the number of XMM registers allocated.
Craig Topperc5eaae42012-03-11 07:57:25 +00002390 static const uint16_t XMMArgRegs[] = {
Gordon Henriksen86737662008-01-05 16:56:59 +00002391 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
2392 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
2393 };
2394 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs, 8);
Craig Topper1accb7e2012-01-10 06:54:16 +00002395 assert((Subtarget->hasSSE1() || !NumXMMRegs)
Torok Edwin3f142c32009-02-01 18:15:56 +00002396 && "SSE registers cannot be used when SSE is disabled");
Scott Michelfdc40a02009-02-17 22:15:04 +00002397
Jakob Stoklund Olesenb8720782012-07-04 19:28:31 +00002398 RegsToPass.push_back(std::make_pair(unsigned(X86::AL),
2399 DAG.getConstant(NumXMMRegs, MVT::i8)));
Gordon Henriksen86737662008-01-05 16:56:59 +00002400 }
2401
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00002402 // For tail calls lower the arguments to the 'real' stack slot.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002403 if (isTailCall) {
2404 // Force all the incoming stack arguments to be loaded from the stack
2405 // before any new outgoing arguments are stored to the stack, because the
2406 // outgoing stack slots may alias the incoming argument stack slots, and
2407 // the alias isn't otherwise explicit. This is slightly more conservative
2408 // than necessary, because it means that each store effectively depends
2409 // on every argument instead of just those arguments it would clobber.
2410 SDValue ArgChain = DAG.getStackArgumentTokenFactor(Chain);
2411
Dan Gohman475871a2008-07-27 21:46:04 +00002412 SmallVector<SDValue, 8> MemOpChains2;
2413 SDValue FIN;
Gordon Henriksen86737662008-01-05 16:56:59 +00002414 int FI = 0;
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002415 if (getTargetMachine().Options.GuaranteedTailCallOpt) {
Evan Chengb2c92902010-02-02 02:22:50 +00002416 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2417 CCValAssign &VA = ArgLocs[i];
2418 if (VA.isRegLoc())
2419 continue;
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00002420 assert(VA.isMemLoc());
Dan Gohmanc9403652010-07-07 15:54:55 +00002421 SDValue Arg = OutVals[i];
Dan Gohman98ca4f22009-08-05 01:29:28 +00002422 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Gordon Henriksen86737662008-01-05 16:56:59 +00002423 // Create frame index.
2424 int32_t Offset = VA.getLocMemOffset()+FPDiff;
Duncan Sands83ec4b62008-06-06 12:08:01 +00002425 uint32_t OpSize = (VA.getLocVT().getSizeInBits()+7)/8;
Evan Chenged2ae132010-07-03 00:40:23 +00002426 FI = MF.getFrameInfo()->CreateFixedObject(OpSize, Offset, true);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002427 FIN = DAG.getFrameIndex(FI, getPointerTy());
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00002428
Duncan Sands276dcbd2008-03-21 09:14:45 +00002429 if (Flags.isByVal()) {
Evan Cheng8e5712b2008-01-12 01:08:07 +00002430 // Copy relative to framepointer.
Dan Gohman475871a2008-07-27 21:46:04 +00002431 SDValue Source = DAG.getIntPtrConstant(VA.getLocMemOffset());
Gabor Greifba36cb52008-08-28 21:40:38 +00002432 if (StackPtr.getNode() == 0)
Scott Michelfdc40a02009-02-17 22:15:04 +00002433 StackPtr = DAG.getCopyFromReg(Chain, dl, X86StackPtr,
Dale Johannesendd64c412009-02-04 00:33:20 +00002434 getPointerTy());
Dale Johannesenace16102009-02-03 19:33:06 +00002435 Source = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, Source);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002436
Dan Gohman98ca4f22009-08-05 01:29:28 +00002437 MemOpChains2.push_back(CreateCopyOfByValArgument(Source, FIN,
2438 ArgChain,
Dale Johannesendd64c412009-02-04 00:33:20 +00002439 Flags, DAG, dl));
Gordon Henriksen86737662008-01-05 16:56:59 +00002440 } else {
Evan Cheng8e5712b2008-01-12 01:08:07 +00002441 // Store relative to framepointer.
Dan Gohman69de1932008-02-06 22:27:42 +00002442 MemOpChains2.push_back(
Dan Gohman98ca4f22009-08-05 01:29:28 +00002443 DAG.getStore(ArgChain, dl, Arg, FIN,
Chris Lattnere8639032010-09-21 06:22:23 +00002444 MachinePointerInfo::getFixedStack(FI),
David Greene67c9d422010-02-15 16:53:33 +00002445 false, false, 0));
Scott Michelfdc40a02009-02-17 22:15:04 +00002446 }
Gordon Henriksen86737662008-01-05 16:56:59 +00002447 }
2448 }
2449
2450 if (!MemOpChains2.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00002451 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Arnold Schwaighofer719eb022008-01-11 14:34:56 +00002452 &MemOpChains2[0], MemOpChains2.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00002453
2454 // Store the return address to the appropriate stack slot.
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002455 Chain = EmitTailCallStoreRetAddr(DAG, MF, Chain, RetAddrFrIdx, Is64Bit,
Dale Johannesenace16102009-02-03 19:33:06 +00002456 FPDiff, dl);
Gordon Henriksen86737662008-01-05 16:56:59 +00002457 }
2458
Jakob Stoklund Olesenb8720782012-07-04 19:28:31 +00002459 // Build a sequence of copy-to-reg nodes chained together with token chain
2460 // and flag operands which copy the outgoing args into registers.
2461 SDValue InFlag;
2462 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
2463 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
2464 RegsToPass[i].second, InFlag);
2465 InFlag = Chain.getValue(1);
2466 }
2467
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002468 if (getTargetMachine().getCodeModel() == CodeModel::Large) {
2469 assert(Is64Bit && "Large code model is only legal in 64-bit mode.");
2470 // In the 64-bit large code model, we have to make all calls
2471 // through a register, since the call instruction's 32-bit
2472 // pc-relative offset may not be large enough to hold the whole
2473 // address.
2474 } else if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002475 // If the callee is a GlobalAddress node (quite common, every direct call
2476 // is) turn it into a TargetGlobalAddress node so that legalize doesn't hack
2477 // it.
2478
Anton Korobeynikov2b2bc682006-12-22 22:29:05 +00002479 // We should use extra load for direct calls to dllimported functions in
2480 // non-JIT mode.
Dan Gohman46510a72010-04-15 01:51:59 +00002481 const GlobalValue *GV = G->getGlobal();
Chris Lattner754b7652009-07-10 05:48:03 +00002482 if (!GV->hasDLLImportLinkage()) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002483 unsigned char OpFlags = 0;
John McCall3a3465b2011-06-15 20:36:13 +00002484 bool ExtraLoad = false;
2485 unsigned WrapperKind = ISD::DELETED_NODE;
Eric Christopherfd179292009-08-27 18:07:15 +00002486
Chris Lattner48a7d022009-07-09 05:02:21 +00002487 // On ELF targets, in both X86-64 and X86-32 mode, direct calls to
2488 // external symbols most go through the PLT in PIC mode. If the symbol
2489 // has hidden or protected visibility, or if it is static or local, then
2490 // we don't need to use the PLT - we can directly call it.
2491 if (Subtarget->isTargetELF() &&
2492 getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
Chris Lattner74e726e2009-07-09 05:27:35 +00002493 GV->hasDefaultVisibility() && !GV->hasLocalLinkage()) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002494 OpFlags = X86II::MO_PLT;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00002495 } else if (Subtarget->isPICStyleStubAny() &&
Chris Lattner80945782010-09-27 06:34:01 +00002496 (GV->isDeclaration() || GV->isWeakForLinker()) &&
Daniel Dunbar558692f2011-04-20 00:14:25 +00002497 (!Subtarget->getTargetTriple().isMacOSX() ||
2498 Subtarget->getTargetTriple().isMacOSXVersionLT(10, 5))) {
Chris Lattner74e726e2009-07-09 05:27:35 +00002499 // PC-relative references to external symbols should go through $stub,
2500 // unless we're building with the leopard linker or later, which
2501 // automatically synthesizes these stubs.
2502 OpFlags = X86II::MO_DARWIN_STUB;
John McCall3a3465b2011-06-15 20:36:13 +00002503 } else if (Subtarget->isPICStyleRIPRel() &&
2504 isa<Function>(GV) &&
Bill Wendling67658342012-10-09 07:45:08 +00002505 cast<Function>(GV)->getFnAttributes().
2506 hasAttribute(Attributes::NonLazyBind)) {
John McCall3a3465b2011-06-15 20:36:13 +00002507 // If the function is marked as non-lazy, generate an indirect call
2508 // which loads from the GOT directly. This avoids runtime overhead
2509 // at the cost of eager binding (and one extra byte of encoding).
2510 OpFlags = X86II::MO_GOTPCREL;
2511 WrapperKind = X86ISD::WrapperRIP;
2512 ExtraLoad = true;
Chris Lattner74e726e2009-07-09 05:27:35 +00002513 }
Chris Lattner48a7d022009-07-09 05:02:21 +00002514
Devang Patel0d881da2010-07-06 22:08:15 +00002515 Callee = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(),
Chris Lattner48a7d022009-07-09 05:02:21 +00002516 G->getOffset(), OpFlags);
John McCall3a3465b2011-06-15 20:36:13 +00002517
2518 // Add a wrapper if needed.
2519 if (WrapperKind != ISD::DELETED_NODE)
2520 Callee = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Callee);
2521 // Add extra indirection if needed.
2522 if (ExtraLoad)
2523 Callee = DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(), Callee,
2524 MachinePointerInfo::getGOT(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002525 false, false, false, 0);
Chris Lattner48a7d022009-07-09 05:02:21 +00002526 }
Bill Wendling056292f2008-09-16 21:48:12 +00002527 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002528 unsigned char OpFlags = 0;
2529
Evan Cheng1bf891a2010-12-01 22:59:46 +00002530 // On ELF targets, in either X86-64 or X86-32 mode, direct calls to
2531 // external symbols should go through the PLT.
2532 if (Subtarget->isTargetELF() &&
2533 getTargetMachine().getRelocationModel() == Reloc::PIC_) {
2534 OpFlags = X86II::MO_PLT;
2535 } else if (Subtarget->isPICStyleStubAny() &&
Daniel Dunbar558692f2011-04-20 00:14:25 +00002536 (!Subtarget->getTargetTriple().isMacOSX() ||
2537 Subtarget->getTargetTriple().isMacOSXVersionLT(10, 5))) {
Evan Cheng1bf891a2010-12-01 22:59:46 +00002538 // PC-relative references to external symbols should go through $stub,
2539 // unless we're building with the leopard linker or later, which
2540 // automatically synthesizes these stubs.
2541 OpFlags = X86II::MO_DARWIN_STUB;
Chris Lattner74e726e2009-07-09 05:27:35 +00002542 }
Eric Christopherfd179292009-08-27 18:07:15 +00002543
Chris Lattner48a7d022009-07-09 05:02:21 +00002544 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy(),
2545 OpFlags);
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002546 }
2547
Chris Lattnerd96d0722007-02-25 06:40:16 +00002548 // Returns a chain & a flag for retval copy to use.
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002549 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Dan Gohman475871a2008-07-27 21:46:04 +00002550 SmallVector<SDValue, 8> Ops;
Gordon Henriksen86737662008-01-05 16:56:59 +00002551
Evan Chengf22f9b32010-02-06 03:28:46 +00002552 if (!IsSibcall && isTailCall) {
Dale Johannesene8d72302009-02-06 23:05:02 +00002553 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
2554 DAG.getIntPtrConstant(0, true), InFlag);
Gordon Henriksen86737662008-01-05 16:56:59 +00002555 InFlag = Chain.getValue(1);
Gordon Henriksen86737662008-01-05 16:56:59 +00002556 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002557
Nate Begeman4c5dcf52006-02-17 00:03:04 +00002558 Ops.push_back(Chain);
2559 Ops.push_back(Callee);
Evan Chengb69d1132006-06-14 18:17:40 +00002560
Dan Gohman98ca4f22009-08-05 01:29:28 +00002561 if (isTailCall)
Owen Anderson825b72b2009-08-11 20:47:22 +00002562 Ops.push_back(DAG.getConstant(FPDiff, MVT::i32));
Evan Chengf4684712007-02-21 21:18:14 +00002563
Gordon Henriksen86737662008-01-05 16:56:59 +00002564 // Add argument registers to the end of the list so that they are known live
2565 // into the call.
Evan Cheng9b449442008-01-07 23:08:23 +00002566 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
2567 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
2568 RegsToPass[i].second.getValueType()));
Scott Michelfdc40a02009-02-17 22:15:04 +00002569
Jakob Stoklund Olesen8bcde2a2012-02-16 00:02:50 +00002570 // Add a register mask operand representing the call-preserved registers.
2571 const TargetRegisterInfo *TRI = getTargetMachine().getRegisterInfo();
2572 const uint32_t *Mask = TRI->getCallPreservedMask(CallConv);
2573 assert(Mask && "Missing call preserved mask for calling convention");
2574 Ops.push_back(DAG.getRegisterMask(Mask));
Jakob Stoklund Olesenc38c4562012-01-18 23:52:22 +00002575
Gabor Greifba36cb52008-08-28 21:40:38 +00002576 if (InFlag.getNode())
Evan Cheng347d5f72006-04-28 21:29:37 +00002577 Ops.push_back(InFlag);
Gordon Henriksenae636f82008-01-03 16:47:34 +00002578
Dan Gohman98ca4f22009-08-05 01:29:28 +00002579 if (isTailCall) {
Dale Johannesen88004c22010-06-05 00:30:45 +00002580 // We used to do:
2581 //// If this is the first return lowered for this function, add the regs
2582 //// to the liveout set for the function.
2583 // This isn't right, although it's probably harmless on x86; liveouts
2584 // should be computed from returns not tail calls. Consider a void
2585 // function making a tail call to a function returning int.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002586 return DAG.getNode(X86ISD::TC_RETURN, dl,
2587 NodeTys, &Ops[0], Ops.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00002588 }
2589
Dale Johannesenace16102009-02-03 19:33:06 +00002590 Chain = DAG.getNode(X86ISD::CALL, dl, NodeTys, &Ops[0], Ops.size());
Evan Cheng347d5f72006-04-28 21:29:37 +00002591 InFlag = Chain.getValue(1);
Evan Chengd90eb7f2006-01-05 00:27:02 +00002592
Chris Lattner2d297092006-05-23 18:50:38 +00002593 // Create the CALLSEQ_END node.
Gordon Henriksen86737662008-01-05 16:56:59 +00002594 unsigned NumBytesForCalleeToPush;
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002595 if (X86::isCalleePop(CallConv, Is64Bit, isVarArg,
2596 getTargetMachine().Options.GuaranteedTailCallOpt))
Gordon Henriksen86737662008-01-05 16:56:59 +00002597 NumBytesForCalleeToPush = NumBytes; // Callee pops everything
Eli Friedman9a2478a2012-01-20 00:05:46 +00002598 else if (!Is64Bit && !IsTailCallConvention(CallConv) && !IsWindows &&
Rafael Espindola1cee7102012-07-25 13:41:10 +00002599 SR == StackStructReturn)
Dan Gohmanf451cb82010-02-10 16:03:48 +00002600 // If this is a call to a struct-return function, the callee
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00002601 // pops the hidden struct pointer, so we have to push it back.
2602 // This is common for Darwin/X86, Linux & Mingw32 targets.
Eli Friedman9a2478a2012-01-20 00:05:46 +00002603 // For MSVC Win32 targets, the caller pops the hidden struct pointer.
Gordon Henriksenae636f82008-01-03 16:47:34 +00002604 NumBytesForCalleeToPush = 4;
Gordon Henriksen86737662008-01-05 16:56:59 +00002605 else
Gordon Henriksenae636f82008-01-03 16:47:34 +00002606 NumBytesForCalleeToPush = 0; // Callee pops nothing.
Scott Michelfdc40a02009-02-17 22:15:04 +00002607
Gordon Henriksenae636f82008-01-03 16:47:34 +00002608 // Returns a flag for retval copy to use.
Evan Chengf22f9b32010-02-06 03:28:46 +00002609 if (!IsSibcall) {
2610 Chain = DAG.getCALLSEQ_END(Chain,
2611 DAG.getIntPtrConstant(NumBytes, true),
2612 DAG.getIntPtrConstant(NumBytesForCalleeToPush,
2613 true),
2614 InFlag);
2615 InFlag = Chain.getValue(1);
2616 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00002617
Chris Lattner3085e152007-02-25 08:59:22 +00002618 // Handle result values, copying them out of physregs into vregs that we
2619 // return.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002620 return LowerCallResult(Chain, InFlag, CallConv, isVarArg,
2621 Ins, dl, DAG, InVals);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002622}
2623
Evan Cheng25ab6902006-09-08 06:48:29 +00002624
2625//===----------------------------------------------------------------------===//
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002626// Fast Calling Convention (tail call) implementation
2627//===----------------------------------------------------------------------===//
2628
2629// Like std call, callee cleans arguments, convention except that ECX is
2630// reserved for storing the tail called function address. Only 2 registers are
2631// free for argument passing (inreg). Tail call optimization is performed
2632// provided:
2633// * tailcallopt is enabled
2634// * caller/callee are fastcc
Arnold Schwaighofera2a4b472008-02-26 10:21:54 +00002635// On X86_64 architecture with GOT-style position independent code only local
2636// (within module) calls are supported at the moment.
Arnold Schwaighofer48abc5c2007-10-12 21:30:57 +00002637// To keep the stack aligned according to platform abi the function
2638// GetAlignedArgumentStackSize ensures that argument delta is always multiples
2639// of stack alignment. (Dynamic linkers need this - darwin's dyld for example)
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002640// If a tail called function callee has more arguments than the caller the
2641// caller needs to make sure that there is room to move the RETADDR to. This is
Arnold Schwaighofer48abc5c2007-10-12 21:30:57 +00002642// achieved by reserving an area the size of the argument delta right after the
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002643// original REtADDR, but before the saved framepointer or the spilled registers
2644// e.g. caller(arg1, arg2) calls callee(arg1, arg2,arg3,arg4)
2645// stack layout:
2646// arg1
2647// arg2
2648// RETADDR
Scott Michelfdc40a02009-02-17 22:15:04 +00002649// [ new RETADDR
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002650// move area ]
2651// (possible EBP)
2652// ESI
2653// EDI
2654// local1 ..
2655
2656/// GetAlignedArgumentStackSize - Make the stack size align e.g 16n + 12 aligned
2657/// for a 16 byte align requirement.
Dan Gohmand858e902010-04-17 15:26:15 +00002658unsigned
2659X86TargetLowering::GetAlignedArgumentStackSize(unsigned StackSize,
2660 SelectionDAG& DAG) const {
Evan Chenge9ac9e62008-09-07 09:07:23 +00002661 MachineFunction &MF = DAG.getMachineFunction();
2662 const TargetMachine &TM = MF.getTarget();
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00002663 const TargetFrameLowering &TFI = *TM.getFrameLowering();
Evan Chenge9ac9e62008-09-07 09:07:23 +00002664 unsigned StackAlignment = TFI.getStackAlignment();
Scott Michelfdc40a02009-02-17 22:15:04 +00002665 uint64_t AlignMask = StackAlignment - 1;
Evan Chenge9ac9e62008-09-07 09:07:23 +00002666 int64_t Offset = StackSize;
Micah Villmow2c39b152012-10-15 16:24:29 +00002667 uint64_t SlotSize = TD->getPointerSize(0);
Evan Chenge9ac9e62008-09-07 09:07:23 +00002668 if ( (Offset & AlignMask) <= (StackAlignment - SlotSize) ) {
2669 // Number smaller than 12 so just add the difference.
2670 Offset += ((StackAlignment - SlotSize) - (Offset & AlignMask));
2671 } else {
2672 // Mask out lower bits, add stackalignment once plus the 12 bytes.
Scott Michelfdc40a02009-02-17 22:15:04 +00002673 Offset = ((~AlignMask) & Offset) + StackAlignment +
Evan Chenge9ac9e62008-09-07 09:07:23 +00002674 (StackAlignment-SlotSize);
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002675 }
Evan Chenge9ac9e62008-09-07 09:07:23 +00002676 return Offset;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002677}
2678
Evan Cheng5f941932010-02-05 02:21:12 +00002679/// MatchingStackOffset - Return true if the given stack call argument is
2680/// already available in the same position (relatively) of the caller's
2681/// incoming argument stack.
2682static
2683bool MatchingStackOffset(SDValue Arg, unsigned Offset, ISD::ArgFlagsTy Flags,
2684 MachineFrameInfo *MFI, const MachineRegisterInfo *MRI,
2685 const X86InstrInfo *TII) {
Evan Cheng4cae1332010-03-05 08:38:04 +00002686 unsigned Bytes = Arg.getValueType().getSizeInBits() / 8;
2687 int FI = INT_MAX;
Evan Cheng5f941932010-02-05 02:21:12 +00002688 if (Arg.getOpcode() == ISD::CopyFromReg) {
2689 unsigned VR = cast<RegisterSDNode>(Arg.getOperand(1))->getReg();
Jakob Stoklund Olesenc9df0252011-01-10 02:58:51 +00002690 if (!TargetRegisterInfo::isVirtualRegister(VR))
Evan Cheng5f941932010-02-05 02:21:12 +00002691 return false;
2692 MachineInstr *Def = MRI->getVRegDef(VR);
2693 if (!Def)
2694 return false;
2695 if (!Flags.isByVal()) {
2696 if (!TII->isLoadFromStackSlot(Def, FI))
2697 return false;
2698 } else {
2699 unsigned Opcode = Def->getOpcode();
2700 if ((Opcode == X86::LEA32r || Opcode == X86::LEA64r) &&
2701 Def->getOperand(1).isFI()) {
2702 FI = Def->getOperand(1).getIndex();
Evan Cheng4cae1332010-03-05 08:38:04 +00002703 Bytes = Flags.getByValSize();
Evan Cheng5f941932010-02-05 02:21:12 +00002704 } else
2705 return false;
2706 }
Evan Cheng4cae1332010-03-05 08:38:04 +00002707 } else if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Arg)) {
2708 if (Flags.isByVal())
2709 // ByVal argument is passed in as a pointer but it's now being
Evan Cheng10718492010-03-05 19:55:55 +00002710 // dereferenced. e.g.
Evan Cheng4cae1332010-03-05 08:38:04 +00002711 // define @foo(%struct.X* %A) {
2712 // tail call @bar(%struct.X* byval %A)
2713 // }
Evan Cheng5f941932010-02-05 02:21:12 +00002714 return false;
2715 SDValue Ptr = Ld->getBasePtr();
2716 FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr);
2717 if (!FINode)
2718 return false;
2719 FI = FINode->getIndex();
Chad Rosierdf78fcd2011-06-25 02:04:56 +00002720 } else if (Arg.getOpcode() == ISD::FrameIndex && Flags.isByVal()) {
Chad Rosier14d71aa2011-06-25 18:51:28 +00002721 FrameIndexSDNode *FINode = cast<FrameIndexSDNode>(Arg);
Chad Rosierdf78fcd2011-06-25 02:04:56 +00002722 FI = FINode->getIndex();
2723 Bytes = Flags.getByValSize();
Evan Cheng4cae1332010-03-05 08:38:04 +00002724 } else
2725 return false;
Evan Cheng5f941932010-02-05 02:21:12 +00002726
Evan Cheng4cae1332010-03-05 08:38:04 +00002727 assert(FI != INT_MAX);
Evan Cheng5f941932010-02-05 02:21:12 +00002728 if (!MFI->isFixedObjectIndex(FI))
2729 return false;
Evan Cheng4cae1332010-03-05 08:38:04 +00002730 return Offset == MFI->getObjectOffset(FI) && Bytes == MFI->getObjectSize(FI);
Evan Cheng5f941932010-02-05 02:21:12 +00002731}
2732
Dan Gohman98ca4f22009-08-05 01:29:28 +00002733/// IsEligibleForTailCallOptimization - Check whether the call is eligible
2734/// for tail call optimization. Targets which want to do tail call
2735/// optimization should implement this function.
2736bool
2737X86TargetLowering::IsEligibleForTailCallOptimization(SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00002738 CallingConv::ID CalleeCC,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002739 bool isVarArg,
Evan Chenga375d472010-03-15 18:54:48 +00002740 bool isCalleeStructRet,
2741 bool isCallerStructRet,
Evan Chengb1cacc72012-09-25 05:32:34 +00002742 Type *RetTy,
Evan Chengb1712452010-01-27 06:25:16 +00002743 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00002744 const SmallVectorImpl<SDValue> &OutVals,
Evan Chengb1712452010-01-27 06:25:16 +00002745 const SmallVectorImpl<ISD::InputArg> &Ins,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002746 SelectionDAG& DAG) const {
Chris Lattner29689432010-03-11 00:22:57 +00002747 if (!IsTailCallConvention(CalleeCC) &&
Evan Chengb1712452010-01-27 06:25:16 +00002748 CalleeCC != CallingConv::C)
2749 return false;
2750
Evan Cheng7096ae42010-01-29 06:45:59 +00002751 // If -tailcallopt is specified, make fastcc functions tail-callable.
Evan Cheng2c12cb42010-03-26 16:26:03 +00002752 const MachineFunction &MF = DAG.getMachineFunction();
Evan Cheng7096ae42010-01-29 06:45:59 +00002753 const Function *CallerF = DAG.getMachineFunction().getFunction();
Evan Chengb1cacc72012-09-25 05:32:34 +00002754
2755 // If the function return type is x86_fp80 and the callee return type is not,
2756 // then the FP_EXTEND of the call result is not a nop. It's not safe to
2757 // perform a tailcall optimization here.
2758 if (CallerF->getReturnType()->isX86_FP80Ty() && !RetTy->isX86_FP80Ty())
2759 return false;
2760
Evan Cheng13617962010-04-30 01:12:32 +00002761 CallingConv::ID CallerCC = CallerF->getCallingConv();
2762 bool CCMatch = CallerCC == CalleeCC;
2763
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002764 if (getTargetMachine().Options.GuaranteedTailCallOpt) {
Evan Cheng13617962010-04-30 01:12:32 +00002765 if (IsTailCallConvention(CalleeCC) && CCMatch)
Evan Cheng843bd692010-01-31 06:44:49 +00002766 return true;
2767 return false;
2768 }
2769
Dale Johannesen2f05cc02010-05-28 23:24:28 +00002770 // Look for obvious safe cases to perform tail call optimization that do not
2771 // require ABI changes. This is what gcc calls sibcall.
Evan Chengb2c92902010-02-02 02:22:50 +00002772
Evan Cheng2c12cb42010-03-26 16:26:03 +00002773 // Can't do sibcall if stack needs to be dynamically re-aligned. PEI needs to
2774 // emit a special epilogue.
2775 if (RegInfo->needsStackRealignment(MF))
2776 return false;
2777
Evan Chenga375d472010-03-15 18:54:48 +00002778 // Also avoid sibcall optimization if either caller or callee uses struct
2779 // return semantics.
2780 if (isCalleeStructRet || isCallerStructRet)
2781 return false;
2782
Chad Rosier2416da32011-06-24 21:15:36 +00002783 // An stdcall caller is expected to clean up its arguments; the callee
2784 // isn't going to do that.
2785 if (!CCMatch && CallerCC==CallingConv::X86_StdCall)
2786 return false;
2787
Chad Rosier871f6642011-05-18 19:59:50 +00002788 // Do not sibcall optimize vararg calls unless all arguments are passed via
Chad Rosiera1660892011-05-20 00:59:28 +00002789 // registers.
Chad Rosier871f6642011-05-18 19:59:50 +00002790 if (isVarArg && !Outs.empty()) {
Chad Rosiera1660892011-05-20 00:59:28 +00002791
2792 // Optimizing for varargs on Win64 is unlikely to be safe without
2793 // additional testing.
2794 if (Subtarget->isTargetWin64())
2795 return false;
2796
Chad Rosier871f6642011-05-18 19:59:50 +00002797 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002798 CCState CCInfo(CalleeCC, isVarArg, DAG.getMachineFunction(),
Craig Topper0fbf3642012-04-23 03:28:34 +00002799 getTargetMachine(), ArgLocs, *DAG.getContext());
Chad Rosier871f6642011-05-18 19:59:50 +00002800
Chad Rosier871f6642011-05-18 19:59:50 +00002801 CCInfo.AnalyzeCallOperands(Outs, CC_X86);
2802 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i)
2803 if (!ArgLocs[i].isRegLoc())
2804 return false;
2805 }
2806
Chad Rosier30450e82011-12-22 22:35:21 +00002807 // If the call result is in ST0 / ST1, it needs to be popped off the x87
2808 // stack. Therefore, if it's not used by the call it is not safe to optimize
2809 // this into a sibcall.
Evan Chengf5b9d6c2010-03-20 02:58:15 +00002810 bool Unused = false;
2811 for (unsigned i = 0, e = Ins.size(); i != e; ++i) {
2812 if (!Ins[i].Used) {
2813 Unused = true;
2814 break;
2815 }
2816 }
2817 if (Unused) {
2818 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002819 CCState CCInfo(CalleeCC, false, DAG.getMachineFunction(),
Craig Topper0fbf3642012-04-23 03:28:34 +00002820 getTargetMachine(), RVLocs, *DAG.getContext());
Evan Chengf5b9d6c2010-03-20 02:58:15 +00002821 CCInfo.AnalyzeCallResult(Ins, RetCC_X86);
Evan Cheng13617962010-04-30 01:12:32 +00002822 for (unsigned i = 0, e = RVLocs.size(); i != e; ++i) {
Evan Chengf5b9d6c2010-03-20 02:58:15 +00002823 CCValAssign &VA = RVLocs[i];
2824 if (VA.getLocReg() == X86::ST0 || VA.getLocReg() == X86::ST1)
2825 return false;
2826 }
2827 }
2828
Evan Cheng13617962010-04-30 01:12:32 +00002829 // If the calling conventions do not match, then we'd better make sure the
2830 // results are returned in the same way as what the caller expects.
2831 if (!CCMatch) {
2832 SmallVector<CCValAssign, 16> RVLocs1;
Eric Christopher471e4222011-06-08 23:55:35 +00002833 CCState CCInfo1(CalleeCC, false, DAG.getMachineFunction(),
Craig Topper0fbf3642012-04-23 03:28:34 +00002834 getTargetMachine(), RVLocs1, *DAG.getContext());
Evan Cheng13617962010-04-30 01:12:32 +00002835 CCInfo1.AnalyzeCallResult(Ins, RetCC_X86);
2836
2837 SmallVector<CCValAssign, 16> RVLocs2;
Eric Christopher471e4222011-06-08 23:55:35 +00002838 CCState CCInfo2(CallerCC, false, DAG.getMachineFunction(),
Craig Topper0fbf3642012-04-23 03:28:34 +00002839 getTargetMachine(), RVLocs2, *DAG.getContext());
Evan Cheng13617962010-04-30 01:12:32 +00002840 CCInfo2.AnalyzeCallResult(Ins, RetCC_X86);
2841
2842 if (RVLocs1.size() != RVLocs2.size())
2843 return false;
2844 for (unsigned i = 0, e = RVLocs1.size(); i != e; ++i) {
2845 if (RVLocs1[i].isRegLoc() != RVLocs2[i].isRegLoc())
2846 return false;
2847 if (RVLocs1[i].getLocInfo() != RVLocs2[i].getLocInfo())
2848 return false;
2849 if (RVLocs1[i].isRegLoc()) {
2850 if (RVLocs1[i].getLocReg() != RVLocs2[i].getLocReg())
2851 return false;
2852 } else {
2853 if (RVLocs1[i].getLocMemOffset() != RVLocs2[i].getLocMemOffset())
2854 return false;
2855 }
2856 }
2857 }
2858
Evan Chenga6bff982010-01-30 01:22:00 +00002859 // If the callee takes no arguments then go on to check the results of the
2860 // call.
2861 if (!Outs.empty()) {
2862 // Check if stack adjustment is needed. For now, do not do this if any
2863 // argument is passed on the stack.
2864 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002865 CCState CCInfo(CalleeCC, isVarArg, DAG.getMachineFunction(),
Craig Topper0fbf3642012-04-23 03:28:34 +00002866 getTargetMachine(), ArgLocs, *DAG.getContext());
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00002867
2868 // Allocate shadow area for Win64
2869 if (Subtarget->isTargetWin64()) {
2870 CCInfo.AllocateStack(32, 8);
2871 }
2872
Duncan Sands45907662010-10-31 13:21:44 +00002873 CCInfo.AnalyzeCallOperands(Outs, CC_X86);
Stuart Hastings6db2c2f2011-05-17 16:59:46 +00002874 if (CCInfo.getNextStackOffset()) {
Evan Chengb2c92902010-02-02 02:22:50 +00002875 MachineFunction &MF = DAG.getMachineFunction();
2876 if (MF.getInfo<X86MachineFunctionInfo>()->getBytesToPopOnReturn())
2877 return false;
Evan Chengb2c92902010-02-02 02:22:50 +00002878
2879 // Check if the arguments are already laid out in the right way as
2880 // the caller's fixed stack objects.
2881 MachineFrameInfo *MFI = MF.getFrameInfo();
Evan Cheng5f941932010-02-05 02:21:12 +00002882 const MachineRegisterInfo *MRI = &MF.getRegInfo();
2883 const X86InstrInfo *TII =
Roman Divacky59324292012-09-05 22:26:57 +00002884 ((const X86TargetMachine&)getTargetMachine()).getInstrInfo();
Evan Chengb2c92902010-02-02 02:22:50 +00002885 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2886 CCValAssign &VA = ArgLocs[i];
Dan Gohmanc9403652010-07-07 15:54:55 +00002887 SDValue Arg = OutVals[i];
Evan Chengb2c92902010-02-02 02:22:50 +00002888 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Evan Chengb2c92902010-02-02 02:22:50 +00002889 if (VA.getLocInfo() == CCValAssign::Indirect)
2890 return false;
2891 if (!VA.isRegLoc()) {
Evan Cheng5f941932010-02-05 02:21:12 +00002892 if (!MatchingStackOffset(Arg, VA.getLocMemOffset(), Flags,
2893 MFI, MRI, TII))
Evan Chengb2c92902010-02-02 02:22:50 +00002894 return false;
2895 }
2896 }
2897 }
Evan Cheng9c044672010-05-29 01:35:22 +00002898
2899 // If the tailcall address may be in a register, then make sure it's
2900 // possible to register allocate for it. In 32-bit, the call address can
2901 // only target EAX, EDX, or ECX since the tail call must be scheduled after
Evan Chengdedd9742010-07-14 06:44:01 +00002902 // callee-saved registers are restored. These happen to be the same
2903 // registers used to pass 'inreg' arguments so watch out for those.
2904 if (!Subtarget->is64Bit() &&
2905 !isa<GlobalAddressSDNode>(Callee) &&
Evan Cheng9c044672010-05-29 01:35:22 +00002906 !isa<ExternalSymbolSDNode>(Callee)) {
Evan Cheng9c044672010-05-29 01:35:22 +00002907 unsigned NumInRegs = 0;
2908 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2909 CCValAssign &VA = ArgLocs[i];
Evan Chengdedd9742010-07-14 06:44:01 +00002910 if (!VA.isRegLoc())
2911 continue;
2912 unsigned Reg = VA.getLocReg();
2913 switch (Reg) {
2914 default: break;
2915 case X86::EAX: case X86::EDX: case X86::ECX:
2916 if (++NumInRegs == 3)
Evan Cheng9c044672010-05-29 01:35:22 +00002917 return false;
Evan Chengdedd9742010-07-14 06:44:01 +00002918 break;
Evan Cheng9c044672010-05-29 01:35:22 +00002919 }
2920 }
2921 }
Evan Chenga6bff982010-01-30 01:22:00 +00002922 }
Evan Chengb1712452010-01-27 06:25:16 +00002923
Evan Cheng86809cc2010-02-03 03:28:02 +00002924 return true;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002925}
2926
Dan Gohman3df24e62008-09-03 23:12:08 +00002927FastISel *
Bob Wilsond49edb72012-08-03 04:06:28 +00002928X86TargetLowering::createFastISel(FunctionLoweringInfo &funcInfo,
2929 const TargetLibraryInfo *libInfo) const {
2930 return X86::createFastISel(funcInfo, libInfo);
Dan Gohmand9f3c482008-08-19 21:32:53 +00002931}
2932
2933
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00002934//===----------------------------------------------------------------------===//
2935// Other Lowering Hooks
2936//===----------------------------------------------------------------------===//
2937
Bruno Cardoso Lopese654b562010-09-01 00:51:36 +00002938static bool MayFoldLoad(SDValue Op) {
2939 return Op.hasOneUse() && ISD::isNormalLoad(Op.getNode());
2940}
2941
2942static bool MayFoldIntoStore(SDValue Op) {
2943 return Op.hasOneUse() && ISD::isNormalStore(*Op.getNode()->use_begin());
2944}
2945
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002946static bool isTargetShuffle(unsigned Opcode) {
2947 switch(Opcode) {
2948 default: return false;
2949 case X86ISD::PSHUFD:
2950 case X86ISD::PSHUFHW:
2951 case X86ISD::PSHUFLW:
Craig Topperb3982da2011-12-31 23:50:21 +00002952 case X86ISD::SHUFP:
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00002953 case X86ISD::PALIGN:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002954 case X86ISD::MOVLHPS:
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00002955 case X86ISD::MOVLHPD:
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00002956 case X86ISD::MOVHLPS:
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00002957 case X86ISD::MOVLPS:
2958 case X86ISD::MOVLPD:
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00002959 case X86ISD::MOVSHDUP:
Bruno Cardoso Lopes013bb3d2010-08-31 22:35:05 +00002960 case X86ISD::MOVSLDUP:
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00002961 case X86ISD::MOVDDUP:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002962 case X86ISD::MOVSS:
2963 case X86ISD::MOVSD:
Craig Topper34671b82011-12-06 08:21:25 +00002964 case X86ISD::UNPCKL:
2965 case X86ISD::UNPCKH:
Craig Topper316cd2a2011-11-30 06:25:25 +00002966 case X86ISD::VPERMILP:
Craig Topperec24e612011-11-30 07:47:51 +00002967 case X86ISD::VPERM2X128:
Craig Topperbdcbcb32012-05-06 18:54:26 +00002968 case X86ISD::VPERMI:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002969 return true;
2970 }
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002971}
2972
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00002973static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
Craig Topper3d092db2012-03-21 02:14:01 +00002974 SDValue V1, SelectionDAG &DAG) {
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00002975 switch(Opc) {
2976 default: llvm_unreachable("Unknown x86 shuffle node");
2977 case X86ISD::MOVSHDUP:
Bruno Cardoso Lopes013bb3d2010-08-31 22:35:05 +00002978 case X86ISD::MOVSLDUP:
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00002979 case X86ISD::MOVDDUP:
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00002980 return DAG.getNode(Opc, dl, VT, V1);
2981 }
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00002982}
2983
2984static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
Craig Topper3d092db2012-03-21 02:14:01 +00002985 SDValue V1, unsigned TargetMask,
2986 SelectionDAG &DAG) {
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00002987 switch(Opc) {
2988 default: llvm_unreachable("Unknown x86 shuffle node");
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002989 case X86ISD::PSHUFD:
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00002990 case X86ISD::PSHUFHW:
2991 case X86ISD::PSHUFLW:
Craig Topper316cd2a2011-11-30 06:25:25 +00002992 case X86ISD::VPERMILP:
Craig Topper8325c112012-04-16 00:41:45 +00002993 case X86ISD::VPERMI:
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00002994 return DAG.getNode(Opc, dl, VT, V1, DAG.getConstant(TargetMask, MVT::i8));
2995 }
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00002996}
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00002997
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002998static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
Craig Topper3d092db2012-03-21 02:14:01 +00002999 SDValue V1, SDValue V2, unsigned TargetMask,
3000 SelectionDAG &DAG) {
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00003001 switch(Opc) {
3002 default: llvm_unreachable("Unknown x86 shuffle node");
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00003003 case X86ISD::PALIGN:
Craig Topperb3982da2011-12-31 23:50:21 +00003004 case X86ISD::SHUFP:
Craig Topperec24e612011-11-30 07:47:51 +00003005 case X86ISD::VPERM2X128:
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00003006 return DAG.getNode(Opc, dl, VT, V1, V2,
3007 DAG.getConstant(TargetMask, MVT::i8));
3008 }
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00003009}
3010
3011static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
3012 SDValue V1, SDValue V2, SelectionDAG &DAG) {
3013 switch(Opc) {
3014 default: llvm_unreachable("Unknown x86 shuffle node");
3015 case X86ISD::MOVLHPS:
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00003016 case X86ISD::MOVLHPD:
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00003017 case X86ISD::MOVHLPS:
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00003018 case X86ISD::MOVLPS:
3019 case X86ISD::MOVLPD:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003020 case X86ISD::MOVSS:
3021 case X86ISD::MOVSD:
Craig Topper34671b82011-12-06 08:21:25 +00003022 case X86ISD::UNPCKL:
3023 case X86ISD::UNPCKH:
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00003024 return DAG.getNode(Opc, dl, VT, V1, V2);
3025 }
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00003026}
3027
Dan Gohmand858e902010-04-17 15:26:15 +00003028SDValue X86TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) const {
Anton Korobeynikova2780e12007-08-15 17:12:32 +00003029 MachineFunction &MF = DAG.getMachineFunction();
3030 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
3031 int ReturnAddrIndex = FuncInfo->getRAIndex();
3032
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00003033 if (ReturnAddrIndex == 0) {
3034 // Set up a frame object for the return address.
Micah Villmow2c39b152012-10-15 16:24:29 +00003035 uint64_t SlotSize = TD->getPointerSize(0);
David Greene3f2bf852009-11-12 20:49:22 +00003036 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(SlotSize, -SlotSize,
Evan Chenged2ae132010-07-03 00:40:23 +00003037 false);
Anton Korobeynikova2780e12007-08-15 17:12:32 +00003038 FuncInfo->setRAIndex(ReturnAddrIndex);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00003039 }
3040
Evan Cheng25ab6902006-09-08 06:48:29 +00003041 return DAG.getFrameIndex(ReturnAddrIndex, getPointerTy());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00003042}
3043
3044
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00003045bool X86::isOffsetSuitableForCodeModel(int64_t Offset, CodeModel::Model M,
3046 bool hasSymbolicDisplacement) {
3047 // Offset should fit into 32 bit immediate field.
Benjamin Kramer34247a02010-03-29 21:13:41 +00003048 if (!isInt<32>(Offset))
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00003049 return false;
3050
3051 // If we don't have a symbolic displacement - we don't have any extra
3052 // restrictions.
3053 if (!hasSymbolicDisplacement)
3054 return true;
3055
3056 // FIXME: Some tweaks might be needed for medium code model.
3057 if (M != CodeModel::Small && M != CodeModel::Kernel)
3058 return false;
3059
3060 // For small code model we assume that latest object is 16MB before end of 31
3061 // bits boundary. We may also accept pretty large negative constants knowing
3062 // that all objects are in the positive half of address space.
3063 if (M == CodeModel::Small && Offset < 16*1024*1024)
3064 return true;
3065
3066 // For kernel code model we know that all object resist in the negative half
3067 // of 32bits address space. We may not accept negative offsets, since they may
3068 // be just off and we may accept pretty large positive ones.
3069 if (M == CodeModel::Kernel && Offset > 0)
3070 return true;
3071
3072 return false;
3073}
3074
Evan Chengef41ff62011-06-23 17:54:54 +00003075/// isCalleePop - Determines whether the callee is required to pop its
3076/// own arguments. Callee pop is necessary to support tail calls.
3077bool X86::isCalleePop(CallingConv::ID CallingConv,
3078 bool is64Bit, bool IsVarArg, bool TailCallOpt) {
3079 if (IsVarArg)
3080 return false;
3081
3082 switch (CallingConv) {
3083 default:
3084 return false;
3085 case CallingConv::X86_StdCall:
3086 return !is64Bit;
3087 case CallingConv::X86_FastCall:
3088 return !is64Bit;
3089 case CallingConv::X86_ThisCall:
3090 return !is64Bit;
3091 case CallingConv::Fast:
3092 return TailCallOpt;
3093 case CallingConv::GHC:
3094 return TailCallOpt;
3095 }
3096}
3097
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003098/// TranslateX86CC - do a one to one translation of a ISD::CondCode to the X86
3099/// specific condition code, returning the condition code and the LHS/RHS of the
3100/// comparison to make.
3101static unsigned TranslateX86CC(ISD::CondCode SetCCOpcode, bool isFP,
3102 SDValue &LHS, SDValue &RHS, SelectionDAG &DAG) {
Evan Chengd9558e02006-01-06 00:43:03 +00003103 if (!isFP) {
Chris Lattnerbfd68a72006-09-13 17:04:54 +00003104 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS)) {
3105 if (SetCCOpcode == ISD::SETGT && RHSC->isAllOnesValue()) {
3106 // X > -1 -> X == 0, jump !sign.
3107 RHS = DAG.getConstant(0, RHS.getValueType());
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003108 return X86::COND_NS;
Craig Topper69947b92012-04-23 06:57:04 +00003109 }
3110 if (SetCCOpcode == ISD::SETLT && RHSC->isNullValue()) {
Chris Lattnerbfd68a72006-09-13 17:04:54 +00003111 // X < 0 -> X == 0, jump on sign.
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003112 return X86::COND_S;
Craig Topper69947b92012-04-23 06:57:04 +00003113 }
3114 if (SetCCOpcode == ISD::SETLT && RHSC->getZExtValue() == 1) {
Dan Gohman5f6913c2007-09-17 14:49:27 +00003115 // X < 1 -> X <= 0
3116 RHS = DAG.getConstant(0, RHS.getValueType());
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003117 return X86::COND_LE;
Chris Lattnerbfd68a72006-09-13 17:04:54 +00003118 }
Chris Lattnerf9570512006-09-13 03:22:10 +00003119 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00003120
Evan Chengd9558e02006-01-06 00:43:03 +00003121 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00003122 default: llvm_unreachable("Invalid integer condition!");
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003123 case ISD::SETEQ: return X86::COND_E;
3124 case ISD::SETGT: return X86::COND_G;
3125 case ISD::SETGE: return X86::COND_GE;
3126 case ISD::SETLT: return X86::COND_L;
3127 case ISD::SETLE: return X86::COND_LE;
3128 case ISD::SETNE: return X86::COND_NE;
3129 case ISD::SETULT: return X86::COND_B;
3130 case ISD::SETUGT: return X86::COND_A;
3131 case ISD::SETULE: return X86::COND_BE;
3132 case ISD::SETUGE: return X86::COND_AE;
Evan Chengd9558e02006-01-06 00:43:03 +00003133 }
Chris Lattner4c78e022008-12-23 23:42:27 +00003134 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003135
Chris Lattner4c78e022008-12-23 23:42:27 +00003136 // First determine if it is required or is profitable to flip the operands.
Duncan Sands4047f4a2008-10-24 13:03:10 +00003137
Chris Lattner4c78e022008-12-23 23:42:27 +00003138 // If LHS is a foldable load, but RHS is not, flip the condition.
Rafael Espindolaf297c932011-02-03 03:58:05 +00003139 if (ISD::isNON_EXTLoad(LHS.getNode()) &&
3140 !ISD::isNON_EXTLoad(RHS.getNode())) {
Chris Lattner4c78e022008-12-23 23:42:27 +00003141 SetCCOpcode = getSetCCSwappedOperands(SetCCOpcode);
3142 std::swap(LHS, RHS);
Evan Cheng4d46d0a2008-08-28 23:48:31 +00003143 }
3144
Chris Lattner4c78e022008-12-23 23:42:27 +00003145 switch (SetCCOpcode) {
3146 default: break;
3147 case ISD::SETOLT:
3148 case ISD::SETOLE:
3149 case ISD::SETUGT:
3150 case ISD::SETUGE:
3151 std::swap(LHS, RHS);
3152 break;
3153 }
3154
3155 // On a floating point condition, the flags are set as follows:
3156 // ZF PF CF op
3157 // 0 | 0 | 0 | X > Y
3158 // 0 | 0 | 1 | X < Y
3159 // 1 | 0 | 0 | X == Y
3160 // 1 | 1 | 1 | unordered
3161 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00003162 default: llvm_unreachable("Condcode should be pre-legalized away");
Chris Lattner4c78e022008-12-23 23:42:27 +00003163 case ISD::SETUEQ:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003164 case ISD::SETEQ: return X86::COND_E;
Chris Lattner4c78e022008-12-23 23:42:27 +00003165 case ISD::SETOLT: // flipped
3166 case ISD::SETOGT:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003167 case ISD::SETGT: return X86::COND_A;
Chris Lattner4c78e022008-12-23 23:42:27 +00003168 case ISD::SETOLE: // flipped
3169 case ISD::SETOGE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003170 case ISD::SETGE: return X86::COND_AE;
Chris Lattner4c78e022008-12-23 23:42:27 +00003171 case ISD::SETUGT: // flipped
3172 case ISD::SETULT:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003173 case ISD::SETLT: return X86::COND_B;
Chris Lattner4c78e022008-12-23 23:42:27 +00003174 case ISD::SETUGE: // flipped
3175 case ISD::SETULE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003176 case ISD::SETLE: return X86::COND_BE;
Chris Lattner4c78e022008-12-23 23:42:27 +00003177 case ISD::SETONE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003178 case ISD::SETNE: return X86::COND_NE;
3179 case ISD::SETUO: return X86::COND_P;
3180 case ISD::SETO: return X86::COND_NP;
Dan Gohman1a492952009-10-20 16:22:37 +00003181 case ISD::SETOEQ:
3182 case ISD::SETUNE: return X86::COND_INVALID;
Chris Lattner4c78e022008-12-23 23:42:27 +00003183 }
Evan Chengd9558e02006-01-06 00:43:03 +00003184}
3185
Evan Cheng4a460802006-01-11 00:33:36 +00003186/// hasFPCMov - is there a floating point cmov for the specific X86 condition
3187/// code. Current x86 isa includes the following FP cmov instructions:
Evan Chengaaca22c2006-01-10 20:26:56 +00003188/// fcmovb, fcomvbe, fcomve, fcmovu, fcmovae, fcmova, fcmovne, fcmovnu.
Evan Cheng4a460802006-01-11 00:33:36 +00003189static bool hasFPCMov(unsigned X86CC) {
Evan Chengaaca22c2006-01-10 20:26:56 +00003190 switch (X86CC) {
3191 default:
3192 return false;
Chris Lattner7fbe9722006-10-20 17:42:20 +00003193 case X86::COND_B:
3194 case X86::COND_BE:
3195 case X86::COND_E:
3196 case X86::COND_P:
3197 case X86::COND_A:
3198 case X86::COND_AE:
3199 case X86::COND_NE:
3200 case X86::COND_NP:
Evan Chengaaca22c2006-01-10 20:26:56 +00003201 return true;
3202 }
3203}
3204
Evan Chengeb2f9692009-10-27 19:56:55 +00003205/// isFPImmLegal - Returns true if the target can instruction select the
3206/// specified FP immediate natively. If false, the legalizer will
3207/// materialize the FP immediate as a load from a constant pool.
Evan Chenga1eaa3c2009-10-28 01:43:28 +00003208bool X86TargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
Evan Chengeb2f9692009-10-27 19:56:55 +00003209 for (unsigned i = 0, e = LegalFPImmediates.size(); i != e; ++i) {
3210 if (Imm.bitwiseIsEqual(LegalFPImmediates[i]))
3211 return true;
3212 }
3213 return false;
3214}
3215
Nate Begeman9008ca62009-04-27 18:41:29 +00003216/// isUndefOrInRange - Return true if Val is undef or if its value falls within
3217/// the specified range (L, H].
3218static bool isUndefOrInRange(int Val, int Low, int Hi) {
3219 return (Val < 0) || (Val >= Low && Val < Hi);
3220}
3221
3222/// isUndefOrEqual - Val is either less than zero (undef) or equal to the
3223/// specified value.
3224static bool isUndefOrEqual(int Val, int CmpVal) {
3225 if (Val < 0 || Val == CmpVal)
Evan Cheng5ced1d82006-04-06 23:23:56 +00003226 return true;
Nate Begeman9008ca62009-04-27 18:41:29 +00003227 return false;
Evan Chengc5cdff22006-04-07 21:53:05 +00003228}
3229
Benjamin Kramerd9b0b022012-06-02 10:20:22 +00003230/// isSequentialOrUndefInRange - Return true if every element in Mask, beginning
Bruno Cardoso Lopes4002d7e2011-08-12 21:54:42 +00003231/// from position Pos and ending in Pos+Size, falls within the specified
3232/// sequential range (L, L+Pos]. or is undef.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003233static bool isSequentialOrUndefInRange(ArrayRef<int> Mask,
Craig Topperb6072642012-05-03 07:26:59 +00003234 unsigned Pos, unsigned Size, int Low) {
3235 for (unsigned i = Pos, e = Pos+Size; i != e; ++i, ++Low)
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003236 if (!isUndefOrEqual(Mask[i], Low))
3237 return false;
3238 return true;
3239}
3240
Nate Begeman9008ca62009-04-27 18:41:29 +00003241/// isPSHUFDMask - Return true if the node specifies a shuffle of elements that
3242/// is suitable for input to PSHUFD or PSHUFW. That is, it doesn't reference
3243/// the second operand.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003244static bool isPSHUFDMask(ArrayRef<int> Mask, EVT VT) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00003245 if (VT == MVT::v4f32 || VT == MVT::v4i32 )
Nate Begeman9008ca62009-04-27 18:41:29 +00003246 return (Mask[0] < 4 && Mask[1] < 4 && Mask[2] < 4 && Mask[3] < 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00003247 if (VT == MVT::v2f64 || VT == MVT::v2i64)
Nate Begeman9008ca62009-04-27 18:41:29 +00003248 return (Mask[0] < 2 && Mask[1] < 2);
3249 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003250}
3251
Nate Begeman9008ca62009-04-27 18:41:29 +00003252/// isPSHUFHWMask - Return true if the node specifies a shuffle of elements that
3253/// is suitable for input to PSHUFHW.
Craig Toppera9a568a2012-05-02 08:03:44 +00003254static bool isPSHUFHWMask(ArrayRef<int> Mask, EVT VT, bool HasAVX2) {
3255 if (VT != MVT::v8i16 && (!HasAVX2 || VT != MVT::v16i16))
Evan Cheng0188ecb2006-03-22 18:59:22 +00003256 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003257
Nate Begeman9008ca62009-04-27 18:41:29 +00003258 // Lower quadword copied in order or undef.
Craig Topperc612d792012-01-02 09:17:37 +00003259 if (!isSequentialOrUndefInRange(Mask, 0, 4, 0))
3260 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003261
Evan Cheng506d3df2006-03-29 23:07:14 +00003262 // Upper quadword shuffled.
Craig Topperc612d792012-01-02 09:17:37 +00003263 for (unsigned i = 4; i != 8; ++i)
Craig Toppera9a568a2012-05-02 08:03:44 +00003264 if (!isUndefOrInRange(Mask[i], 4, 8))
Evan Cheng506d3df2006-03-29 23:07:14 +00003265 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003266
Craig Toppera9a568a2012-05-02 08:03:44 +00003267 if (VT == MVT::v16i16) {
3268 // Lower quadword copied in order or undef.
3269 if (!isSequentialOrUndefInRange(Mask, 8, 4, 8))
3270 return false;
3271
3272 // Upper quadword shuffled.
3273 for (unsigned i = 12; i != 16; ++i)
3274 if (!isUndefOrInRange(Mask[i], 12, 16))
3275 return false;
3276 }
3277
Evan Cheng506d3df2006-03-29 23:07:14 +00003278 return true;
3279}
3280
Nate Begeman9008ca62009-04-27 18:41:29 +00003281/// isPSHUFLWMask - Return true if the node specifies a shuffle of elements that
3282/// is suitable for input to PSHUFLW.
Craig Toppera9a568a2012-05-02 08:03:44 +00003283static bool isPSHUFLWMask(ArrayRef<int> Mask, EVT VT, bool HasAVX2) {
3284 if (VT != MVT::v8i16 && (!HasAVX2 || VT != MVT::v16i16))
Evan Cheng506d3df2006-03-29 23:07:14 +00003285 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003286
Rafael Espindola15684b22009-04-24 12:40:33 +00003287 // Upper quadword copied in order.
Craig Topperc612d792012-01-02 09:17:37 +00003288 if (!isSequentialOrUndefInRange(Mask, 4, 4, 4))
3289 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003290
Rafael Espindola15684b22009-04-24 12:40:33 +00003291 // Lower quadword shuffled.
Craig Topperc612d792012-01-02 09:17:37 +00003292 for (unsigned i = 0; i != 4; ++i)
Craig Toppera9a568a2012-05-02 08:03:44 +00003293 if (!isUndefOrInRange(Mask[i], 0, 4))
Rafael Espindola15684b22009-04-24 12:40:33 +00003294 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003295
Craig Toppera9a568a2012-05-02 08:03:44 +00003296 if (VT == MVT::v16i16) {
3297 // Upper quadword copied in order.
3298 if (!isSequentialOrUndefInRange(Mask, 12, 4, 12))
3299 return false;
3300
3301 // Lower quadword shuffled.
3302 for (unsigned i = 8; i != 12; ++i)
3303 if (!isUndefOrInRange(Mask[i], 8, 12))
3304 return false;
3305 }
3306
Rafael Espindola15684b22009-04-24 12:40:33 +00003307 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00003308}
3309
Nate Begemana09008b2009-10-19 02:17:23 +00003310/// isPALIGNRMask - Return true if the node specifies a shuffle of elements that
3311/// is suitable for input to PALIGNR.
Craig Topper0e2037b2012-01-20 05:53:00 +00003312static bool isPALIGNRMask(ArrayRef<int> Mask, EVT VT,
3313 const X86Subtarget *Subtarget) {
3314 if ((VT.getSizeInBits() == 128 && !Subtarget->hasSSSE3()) ||
3315 (VT.getSizeInBits() == 256 && !Subtarget->hasAVX2()))
Bruno Cardoso Lopes9065d4b2011-07-29 01:30:59 +00003316 return false;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003317
Craig Topper0e2037b2012-01-20 05:53:00 +00003318 unsigned NumElts = VT.getVectorNumElements();
3319 unsigned NumLanes = VT.getSizeInBits()/128;
3320 unsigned NumLaneElts = NumElts/NumLanes;
3321
3322 // Do not handle 64-bit element shuffles with palignr.
3323 if (NumLaneElts == 2)
Nate Begemana09008b2009-10-19 02:17:23 +00003324 return false;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003325
Craig Topper0e2037b2012-01-20 05:53:00 +00003326 for (unsigned l = 0; l != NumElts; l+=NumLaneElts) {
3327 unsigned i;
3328 for (i = 0; i != NumLaneElts; ++i) {
3329 if (Mask[i+l] >= 0)
3330 break;
3331 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00003332
Craig Topper0e2037b2012-01-20 05:53:00 +00003333 // Lane is all undef, go to next lane
3334 if (i == NumLaneElts)
3335 continue;
Nate Begemana09008b2009-10-19 02:17:23 +00003336
Craig Topper0e2037b2012-01-20 05:53:00 +00003337 int Start = Mask[i+l];
Nate Begemana09008b2009-10-19 02:17:23 +00003338
Craig Topper0e2037b2012-01-20 05:53:00 +00003339 // Make sure its in this lane in one of the sources
3340 if (!isUndefOrInRange(Start, l, l+NumLaneElts) &&
3341 !isUndefOrInRange(Start, l+NumElts, l+NumElts+NumLaneElts))
Nate Begemana09008b2009-10-19 02:17:23 +00003342 return false;
Craig Topper0e2037b2012-01-20 05:53:00 +00003343
3344 // If not lane 0, then we must match lane 0
3345 if (l != 0 && Mask[i] >= 0 && !isUndefOrEqual(Start, Mask[i]+l))
3346 return false;
3347
3348 // Correct second source to be contiguous with first source
3349 if (Start >= (int)NumElts)
3350 Start -= NumElts - NumLaneElts;
3351
3352 // Make sure we're shifting in the right direction.
3353 if (Start <= (int)(i+l))
3354 return false;
3355
3356 Start -= i;
3357
3358 // Check the rest of the elements to see if they are consecutive.
3359 for (++i; i != NumLaneElts; ++i) {
3360 int Idx = Mask[i+l];
3361
3362 // Make sure its in this lane
3363 if (!isUndefOrInRange(Idx, l, l+NumLaneElts) &&
3364 !isUndefOrInRange(Idx, l+NumElts, l+NumElts+NumLaneElts))
3365 return false;
3366
3367 // If not lane 0, then we must match lane 0
3368 if (l != 0 && Mask[i] >= 0 && !isUndefOrEqual(Idx, Mask[i]+l))
3369 return false;
3370
3371 if (Idx >= (int)NumElts)
3372 Idx -= NumElts - NumLaneElts;
3373
3374 if (!isUndefOrEqual(Idx, Start+i))
3375 return false;
3376
3377 }
Nate Begemana09008b2009-10-19 02:17:23 +00003378 }
Craig Topper0e2037b2012-01-20 05:53:00 +00003379
Nate Begemana09008b2009-10-19 02:17:23 +00003380 return true;
3381}
3382
Craig Topper1a7700a2012-01-19 08:19:12 +00003383/// CommuteVectorShuffleMask - Change values in a shuffle permute mask assuming
3384/// the two vector operands have swapped position.
3385static void CommuteVectorShuffleMask(SmallVectorImpl<int> &Mask,
3386 unsigned NumElems) {
3387 for (unsigned i = 0; i != NumElems; ++i) {
3388 int idx = Mask[i];
3389 if (idx < 0)
3390 continue;
3391 else if (idx < (int)NumElems)
3392 Mask[i] = idx + NumElems;
3393 else
3394 Mask[i] = idx - NumElems;
3395 }
3396}
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00003397
Craig Topper1a7700a2012-01-19 08:19:12 +00003398/// isSHUFPMask - Return true if the specified VECTOR_SHUFFLE operand
3399/// specifies a shuffle of elements that is suitable for input to 128/256-bit
3400/// SHUFPS and SHUFPD. If Commuted is true, then it checks for sources to be
3401/// reverse of what x86 shuffles want.
3402static bool isSHUFPMask(ArrayRef<int> Mask, EVT VT, bool HasAVX,
3403 bool Commuted = false) {
3404 if (!HasAVX && VT.getSizeInBits() == 256)
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00003405 return false;
3406
Craig Topper1a7700a2012-01-19 08:19:12 +00003407 unsigned NumElems = VT.getVectorNumElements();
3408 unsigned NumLanes = VT.getSizeInBits()/128;
3409 unsigned NumLaneElems = NumElems/NumLanes;
3410
3411 if (NumLaneElems != 2 && NumLaneElems != 4)
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00003412 return false;
3413
3414 // VSHUFPSY divides the resulting vector into 4 chunks.
3415 // The sources are also splitted into 4 chunks, and each destination
3416 // chunk must come from a different source chunk.
3417 //
3418 // SRC1 => X7 X6 X5 X4 X3 X2 X1 X0
3419 // SRC2 => Y7 Y6 Y5 Y4 Y3 Y2 Y1 Y9
3420 //
3421 // DST => Y7..Y4, Y7..Y4, X7..X4, X7..X4,
3422 // Y3..Y0, Y3..Y0, X3..X0, X3..X0
3423 //
Craig Topper9d7025b2011-11-27 21:41:12 +00003424 // VSHUFPDY divides the resulting vector into 4 chunks.
3425 // The sources are also splitted into 4 chunks, and each destination
3426 // chunk must come from a different source chunk.
3427 //
3428 // SRC1 => X3 X2 X1 X0
3429 // SRC2 => Y3 Y2 Y1 Y0
3430 //
3431 // DST => Y3..Y2, X3..X2, Y1..Y0, X1..X0
3432 //
Craig Topper1a7700a2012-01-19 08:19:12 +00003433 unsigned HalfLaneElems = NumLaneElems/2;
3434 for (unsigned l = 0; l != NumElems; l += NumLaneElems) {
3435 for (unsigned i = 0; i != NumLaneElems; ++i) {
3436 int Idx = Mask[i+l];
3437 unsigned RngStart = l + ((Commuted == (i<HalfLaneElems)) ? NumElems : 0);
3438 if (!isUndefOrInRange(Idx, RngStart, RngStart+NumLaneElems))
3439 return false;
3440 // For VSHUFPSY, the mask of the second half must be the same as the
3441 // first but with the appropriate offsets. This works in the same way as
3442 // VPERMILPS works with masks.
3443 if (NumElems != 8 || l == 0 || Mask[i] < 0)
3444 continue;
3445 if (!isUndefOrEqual(Idx, Mask[i]+l))
3446 return false;
Craig Topper1ff73d72011-12-06 04:59:07 +00003447 }
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00003448 }
3449
3450 return true;
3451}
3452
Evan Cheng2c0dbd02006-03-24 02:58:06 +00003453/// isMOVHLPSMask - Return true if the specified VECTOR_SHUFFLE operand
3454/// specifies a shuffle of elements that is suitable for input to MOVHLPS.
Craig Topperdd637ae2012-02-19 05:41:45 +00003455static bool isMOVHLPSMask(ArrayRef<int> Mask, EVT VT) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00003456 if (!VT.is128BitVector())
Bruno Cardoso Lopes61260052011-07-29 02:05:28 +00003457 return false;
3458
Craig Topper7a9a28b2012-08-12 02:23:29 +00003459 unsigned NumElems = VT.getVectorNumElements();
3460
Bruno Cardoso Lopes61260052011-07-29 02:05:28 +00003461 if (NumElems != 4)
Evan Cheng2c0dbd02006-03-24 02:58:06 +00003462 return false;
3463
Evan Cheng2064a2b2006-03-28 06:50:32 +00003464 // Expect bit0 == 6, bit1 == 7, bit2 == 2, bit3 == 3
Craig Topperdd637ae2012-02-19 05:41:45 +00003465 return isUndefOrEqual(Mask[0], 6) &&
3466 isUndefOrEqual(Mask[1], 7) &&
3467 isUndefOrEqual(Mask[2], 2) &&
3468 isUndefOrEqual(Mask[3], 3);
Evan Cheng6e56e2c2006-11-07 22:14:24 +00003469}
3470
Nate Begeman0b10b912009-11-07 23:17:15 +00003471/// isMOVHLPS_v_undef_Mask - Special case of isMOVHLPSMask for canonical form
3472/// of vector_shuffle v, v, <2, 3, 2, 3>, i.e. vector_shuffle v, undef,
3473/// <2, 3, 2, 3>
Craig Topperdd637ae2012-02-19 05:41:45 +00003474static bool isMOVHLPS_v_undef_Mask(ArrayRef<int> Mask, EVT VT) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00003475 if (!VT.is128BitVector())
Bruno Cardoso Lopes61260052011-07-29 02:05:28 +00003476 return false;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003477
Craig Topper7a9a28b2012-08-12 02:23:29 +00003478 unsigned NumElems = VT.getVectorNumElements();
3479
Nate Begeman0b10b912009-11-07 23:17:15 +00003480 if (NumElems != 4)
3481 return false;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003482
Craig Topperdd637ae2012-02-19 05:41:45 +00003483 return isUndefOrEqual(Mask[0], 2) &&
3484 isUndefOrEqual(Mask[1], 3) &&
3485 isUndefOrEqual(Mask[2], 2) &&
3486 isUndefOrEqual(Mask[3], 3);
Nate Begeman0b10b912009-11-07 23:17:15 +00003487}
3488
Evan Cheng5ced1d82006-04-06 23:23:56 +00003489/// isMOVLPMask - Return true if the specified VECTOR_SHUFFLE operand
3490/// specifies a shuffle of elements that is suitable for input to MOVLP{S|D}.
Craig Topperdd637ae2012-02-19 05:41:45 +00003491static bool isMOVLPMask(ArrayRef<int> Mask, EVT VT) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00003492 if (!VT.is128BitVector())
Elena Demikhovsky021c0a22011-12-28 08:14:01 +00003493 return false;
3494
Craig Topperdd637ae2012-02-19 05:41:45 +00003495 unsigned NumElems = VT.getVectorNumElements();
Evan Cheng5ced1d82006-04-06 23:23:56 +00003496
Evan Cheng5ced1d82006-04-06 23:23:56 +00003497 if (NumElems != 2 && NumElems != 4)
3498 return false;
3499
Chad Rosier238ae312012-04-30 17:47:15 +00003500 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
Craig Topperdd637ae2012-02-19 05:41:45 +00003501 if (!isUndefOrEqual(Mask[i], i + NumElems))
Evan Chengc5cdff22006-04-07 21:53:05 +00003502 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003503
Chad Rosier238ae312012-04-30 17:47:15 +00003504 for (unsigned i = NumElems/2, e = NumElems; i != e; ++i)
Craig Topperdd637ae2012-02-19 05:41:45 +00003505 if (!isUndefOrEqual(Mask[i], i))
Evan Chengc5cdff22006-04-07 21:53:05 +00003506 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003507
3508 return true;
3509}
3510
Nate Begeman0b10b912009-11-07 23:17:15 +00003511/// isMOVLHPSMask - Return true if the specified VECTOR_SHUFFLE operand
3512/// specifies a shuffle of elements that is suitable for input to MOVLHPS.
Craig Topperdd637ae2012-02-19 05:41:45 +00003513static bool isMOVLHPSMask(ArrayRef<int> Mask, EVT VT) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00003514 if (!VT.is128BitVector())
3515 return false;
3516
Craig Topperdd637ae2012-02-19 05:41:45 +00003517 unsigned NumElems = VT.getVectorNumElements();
Evan Cheng5ced1d82006-04-06 23:23:56 +00003518
Craig Topper7a9a28b2012-08-12 02:23:29 +00003519 if (NumElems != 2 && NumElems != 4)
Evan Cheng5ced1d82006-04-06 23:23:56 +00003520 return false;
3521
Chad Rosier238ae312012-04-30 17:47:15 +00003522 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
Craig Topperdd637ae2012-02-19 05:41:45 +00003523 if (!isUndefOrEqual(Mask[i], i))
Evan Chengc5cdff22006-04-07 21:53:05 +00003524 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003525
Chad Rosier238ae312012-04-30 17:47:15 +00003526 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
3527 if (!isUndefOrEqual(Mask[i + e], i + NumElems))
Evan Chengc5cdff22006-04-07 21:53:05 +00003528 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003529
3530 return true;
3531}
3532
Elena Demikhovsky15963732012-06-26 08:04:10 +00003533//
3534// Some special combinations that can be optimized.
3535//
3536static
3537SDValue Compact8x32ShuffleNode(ShuffleVectorSDNode *SVOp,
3538 SelectionDAG &DAG) {
3539 EVT VT = SVOp->getValueType(0);
Elena Demikhovsky15963732012-06-26 08:04:10 +00003540 DebugLoc dl = SVOp->getDebugLoc();
3541
3542 if (VT != MVT::v8i32 && VT != MVT::v8f32)
3543 return SDValue();
3544
3545 ArrayRef<int> Mask = SVOp->getMask();
3546
3547 // These are the special masks that may be optimized.
3548 static const int MaskToOptimizeEven[] = {0, 8, 2, 10, 4, 12, 6, 14};
3549 static const int MaskToOptimizeOdd[] = {1, 9, 3, 11, 5, 13, 7, 15};
3550 bool MatchEvenMask = true;
3551 bool MatchOddMask = true;
3552 for (int i=0; i<8; ++i) {
3553 if (!isUndefOrEqual(Mask[i], MaskToOptimizeEven[i]))
3554 MatchEvenMask = false;
3555 if (!isUndefOrEqual(Mask[i], MaskToOptimizeOdd[i]))
3556 MatchOddMask = false;
3557 }
Elena Demikhovsky15963732012-06-26 08:04:10 +00003558
Elena Demikhovsky32510202012-09-04 12:49:02 +00003559 if (!MatchEvenMask && !MatchOddMask)
Elena Demikhovsky15963732012-06-26 08:04:10 +00003560 return SDValue();
Michael Liao471b9172012-10-03 23:43:52 +00003561
Elena Demikhovsky15963732012-06-26 08:04:10 +00003562 SDValue UndefNode = DAG.getNode(ISD::UNDEF, dl, VT);
3563
Elena Demikhovsky32510202012-09-04 12:49:02 +00003564 SDValue Op0 = SVOp->getOperand(0);
3565 SDValue Op1 = SVOp->getOperand(1);
3566
3567 if (MatchEvenMask) {
3568 // Shift the second operand right to 32 bits.
3569 static const int ShiftRightMask[] = {-1, 0, -1, 2, -1, 4, -1, 6 };
3570 Op1 = DAG.getVectorShuffle(VT, dl, Op1, UndefNode, ShiftRightMask);
3571 } else {
3572 // Shift the first operand left to 32 bits.
3573 static const int ShiftLeftMask[] = {1, -1, 3, -1, 5, -1, 7, -1 };
3574 Op0 = DAG.getVectorShuffle(VT, dl, Op0, UndefNode, ShiftLeftMask);
3575 }
3576 static const int BlendMask[] = {0, 9, 2, 11, 4, 13, 6, 15};
3577 return DAG.getVectorShuffle(VT, dl, Op0, Op1, BlendMask);
Elena Demikhovsky15963732012-06-26 08:04:10 +00003578}
3579
Evan Cheng0038e592006-03-28 00:39:58 +00003580/// isUNPCKLMask - Return true if the specified VECTOR_SHUFFLE operand
3581/// specifies a shuffle of elements that is suitable for input to UNPCKL.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003582static bool isUNPCKLMask(ArrayRef<int> Mask, EVT VT,
Craig Topper6347e862011-11-21 06:57:39 +00003583 bool HasAVX2, bool V2IsSplat = false) {
Craig Topper94438ba2011-12-16 08:06:31 +00003584 unsigned NumElts = VT.getVectorNumElements();
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003585
3586 assert((VT.is128BitVector() || VT.is256BitVector()) &&
3587 "Unsupported vector type for unpckh");
3588
Craig Topper6347e862011-11-21 06:57:39 +00003589 if (VT.getSizeInBits() == 256 && NumElts != 4 && NumElts != 8 &&
Craig Topper6fa583d2011-11-21 08:26:50 +00003590 (!HasAVX2 || (NumElts != 16 && NumElts != 32)))
Evan Cheng0038e592006-03-28 00:39:58 +00003591 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003592
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003593 // Handle 128 and 256-bit vector lengths. AVX defines UNPCK* to operate
3594 // independently on 128-bit lanes.
3595 unsigned NumLanes = VT.getSizeInBits()/128;
3596 unsigned NumLaneElts = NumElts/NumLanes;
David Greenea20244d2011-03-02 17:23:43 +00003597
Craig Topper94438ba2011-12-16 08:06:31 +00003598 for (unsigned l = 0; l != NumLanes; ++l) {
3599 for (unsigned i = l*NumLaneElts, j = l*NumLaneElts;
3600 i != (l+1)*NumLaneElts;
David Greenea20244d2011-03-02 17:23:43 +00003601 i += 2, ++j) {
3602 int BitI = Mask[i];
3603 int BitI1 = Mask[i+1];
3604 if (!isUndefOrEqual(BitI, j))
Evan Cheng39623da2006-04-20 08:58:49 +00003605 return false;
David Greenea20244d2011-03-02 17:23:43 +00003606 if (V2IsSplat) {
3607 if (!isUndefOrEqual(BitI1, NumElts))
3608 return false;
3609 } else {
3610 if (!isUndefOrEqual(BitI1, j + NumElts))
3611 return false;
3612 }
Evan Cheng39623da2006-04-20 08:58:49 +00003613 }
Evan Cheng0038e592006-03-28 00:39:58 +00003614 }
David Greenea20244d2011-03-02 17:23:43 +00003615
Evan Cheng0038e592006-03-28 00:39:58 +00003616 return true;
3617}
3618
Evan Cheng4fcb9222006-03-28 02:43:26 +00003619/// isUNPCKHMask - Return true if the specified VECTOR_SHUFFLE operand
3620/// specifies a shuffle of elements that is suitable for input to UNPCKH.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003621static bool isUNPCKHMask(ArrayRef<int> Mask, EVT VT,
Craig Topper6347e862011-11-21 06:57:39 +00003622 bool HasAVX2, bool V2IsSplat = false) {
Craig Topper94438ba2011-12-16 08:06:31 +00003623 unsigned NumElts = VT.getVectorNumElements();
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003624
3625 assert((VT.is128BitVector() || VT.is256BitVector()) &&
3626 "Unsupported vector type for unpckh");
3627
Craig Topper6347e862011-11-21 06:57:39 +00003628 if (VT.getSizeInBits() == 256 && NumElts != 4 && NumElts != 8 &&
Craig Topper6fa583d2011-11-21 08:26:50 +00003629 (!HasAVX2 || (NumElts != 16 && NumElts != 32)))
Evan Cheng4fcb9222006-03-28 02:43:26 +00003630 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003631
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003632 // Handle 128 and 256-bit vector lengths. AVX defines UNPCK* to operate
3633 // independently on 128-bit lanes.
3634 unsigned NumLanes = VT.getSizeInBits()/128;
3635 unsigned NumLaneElts = NumElts/NumLanes;
3636
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003637 for (unsigned l = 0; l != NumLanes; ++l) {
Craig Topper94438ba2011-12-16 08:06:31 +00003638 for (unsigned i = l*NumLaneElts, j = (l*NumLaneElts)+NumLaneElts/2;
3639 i != (l+1)*NumLaneElts; i += 2, ++j) {
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003640 int BitI = Mask[i];
3641 int BitI1 = Mask[i+1];
3642 if (!isUndefOrEqual(BitI, j))
Evan Cheng39623da2006-04-20 08:58:49 +00003643 return false;
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003644 if (V2IsSplat) {
3645 if (isUndefOrEqual(BitI1, NumElts))
3646 return false;
3647 } else {
3648 if (!isUndefOrEqual(BitI1, j+NumElts))
3649 return false;
3650 }
Evan Cheng39623da2006-04-20 08:58:49 +00003651 }
Evan Cheng4fcb9222006-03-28 02:43:26 +00003652 }
Evan Cheng4fcb9222006-03-28 02:43:26 +00003653 return true;
3654}
3655
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003656/// isUNPCKL_v_undef_Mask - Special case of isUNPCKLMask for canonical form
3657/// of vector_shuffle v, v, <0, 4, 1, 5>, i.e. vector_shuffle v, undef,
3658/// <0, 0, 1, 1>
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003659static bool isUNPCKL_v_undef_Mask(ArrayRef<int> Mask, EVT VT,
Craig Topper94438ba2011-12-16 08:06:31 +00003660 bool HasAVX2) {
3661 unsigned NumElts = VT.getVectorNumElements();
3662
3663 assert((VT.is128BitVector() || VT.is256BitVector()) &&
3664 "Unsupported vector type for unpckh");
3665
3666 if (VT.getSizeInBits() == 256 && NumElts != 4 && NumElts != 8 &&
3667 (!HasAVX2 || (NumElts != 16 && NumElts != 32)))
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003668 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003669
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00003670 // For 256-bit i64/f64, use MOVDDUPY instead, so reject the matching pattern
3671 // FIXME: Need a better way to get rid of this, there's no latency difference
3672 // between UNPCKLPD and MOVDDUP, the later should always be checked first and
3673 // the former later. We should also remove the "_undef" special mask.
Craig Topper94438ba2011-12-16 08:06:31 +00003674 if (NumElts == 4 && VT.getSizeInBits() == 256)
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00003675 return false;
3676
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003677 // Handle 128 and 256-bit vector lengths. AVX defines UNPCK* to operate
3678 // independently on 128-bit lanes.
Craig Topper94438ba2011-12-16 08:06:31 +00003679 unsigned NumLanes = VT.getSizeInBits()/128;
3680 unsigned NumLaneElts = NumElts/NumLanes;
David Greenea20244d2011-03-02 17:23:43 +00003681
Craig Topper94438ba2011-12-16 08:06:31 +00003682 for (unsigned l = 0; l != NumLanes; ++l) {
3683 for (unsigned i = l*NumLaneElts, j = l*NumLaneElts;
3684 i != (l+1)*NumLaneElts;
David Greenea20244d2011-03-02 17:23:43 +00003685 i += 2, ++j) {
3686 int BitI = Mask[i];
3687 int BitI1 = Mask[i+1];
3688
3689 if (!isUndefOrEqual(BitI, j))
3690 return false;
3691 if (!isUndefOrEqual(BitI1, j))
3692 return false;
3693 }
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003694 }
David Greenea20244d2011-03-02 17:23:43 +00003695
Rafael Espindola15684b22009-04-24 12:40:33 +00003696 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00003697}
3698
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003699/// isUNPCKH_v_undef_Mask - Special case of isUNPCKHMask for canonical form
3700/// of vector_shuffle v, v, <2, 6, 3, 7>, i.e. vector_shuffle v, undef,
3701/// <2, 2, 3, 3>
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003702static bool isUNPCKH_v_undef_Mask(ArrayRef<int> Mask, EVT VT, bool HasAVX2) {
Craig Topper94438ba2011-12-16 08:06:31 +00003703 unsigned NumElts = VT.getVectorNumElements();
3704
3705 assert((VT.is128BitVector() || VT.is256BitVector()) &&
3706 "Unsupported vector type for unpckh");
3707
3708 if (VT.getSizeInBits() == 256 && NumElts != 4 && NumElts != 8 &&
3709 (!HasAVX2 || (NumElts != 16 && NumElts != 32)))
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003710 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003711
Craig Topper94438ba2011-12-16 08:06:31 +00003712 // Handle 128 and 256-bit vector lengths. AVX defines UNPCK* to operate
3713 // independently on 128-bit lanes.
3714 unsigned NumLanes = VT.getSizeInBits()/128;
3715 unsigned NumLaneElts = NumElts/NumLanes;
3716
3717 for (unsigned l = 0; l != NumLanes; ++l) {
3718 for (unsigned i = l*NumLaneElts, j = (l*NumLaneElts)+NumLaneElts/2;
3719 i != (l+1)*NumLaneElts; i += 2, ++j) {
3720 int BitI = Mask[i];
3721 int BitI1 = Mask[i+1];
3722 if (!isUndefOrEqual(BitI, j))
3723 return false;
3724 if (!isUndefOrEqual(BitI1, j))
3725 return false;
3726 }
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003727 }
Rafael Espindola15684b22009-04-24 12:40:33 +00003728 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00003729}
3730
Evan Cheng017dcc62006-04-21 01:05:10 +00003731/// isMOVLMask - Return true if the specified VECTOR_SHUFFLE operand
3732/// specifies a shuffle of elements that is suitable for input to MOVSS,
3733/// MOVSD, and MOVD, i.e. setting the lowest element.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003734static bool isMOVLMask(ArrayRef<int> Mask, EVT VT) {
Eli Friedman10415532009-06-06 06:05:10 +00003735 if (VT.getVectorElementType().getSizeInBits() < 32)
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003736 return false;
Craig Topper7a9a28b2012-08-12 02:23:29 +00003737 if (!VT.is128BitVector())
Elena Demikhovsky021c0a22011-12-28 08:14:01 +00003738 return false;
Eli Friedman10415532009-06-06 06:05:10 +00003739
Craig Topperc612d792012-01-02 09:17:37 +00003740 unsigned NumElts = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00003741
Nate Begeman9008ca62009-04-27 18:41:29 +00003742 if (!isUndefOrEqual(Mask[0], NumElts))
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003743 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003744
Craig Topperc612d792012-01-02 09:17:37 +00003745 for (unsigned i = 1; i != NumElts; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003746 if (!isUndefOrEqual(Mask[i], i))
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003747 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003748
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003749 return true;
3750}
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003751
Craig Topper70b883b2011-11-28 10:14:51 +00003752/// isVPERM2X128Mask - Match 256-bit shuffles where the elements are considered
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003753/// as permutations between 128-bit chunks or halves. As an example: this
3754/// shuffle bellow:
3755/// vector_shuffle <4, 5, 6, 7, 12, 13, 14, 15>
3756/// The first half comes from the second half of V1 and the second half from the
3757/// the second half of V2.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003758static bool isVPERM2X128Mask(ArrayRef<int> Mask, EVT VT, bool HasAVX) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00003759 if (!HasAVX || !VT.is256BitVector())
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003760 return false;
3761
3762 // The shuffle result is divided into half A and half B. In total the two
3763 // sources have 4 halves, namely: C, D, E, F. The final values of A and
3764 // B must come from C, D, E or F.
Craig Topperc612d792012-01-02 09:17:37 +00003765 unsigned HalfSize = VT.getVectorNumElements()/2;
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003766 bool MatchA = false, MatchB = false;
3767
3768 // Check if A comes from one of C, D, E, F.
Craig Topperc612d792012-01-02 09:17:37 +00003769 for (unsigned Half = 0; Half != 4; ++Half) {
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003770 if (isSequentialOrUndefInRange(Mask, 0, HalfSize, Half*HalfSize)) {
3771 MatchA = true;
3772 break;
3773 }
3774 }
3775
3776 // Check if B comes from one of C, D, E, F.
Craig Topperc612d792012-01-02 09:17:37 +00003777 for (unsigned Half = 0; Half != 4; ++Half) {
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003778 if (isSequentialOrUndefInRange(Mask, HalfSize, HalfSize, Half*HalfSize)) {
3779 MatchB = true;
3780 break;
3781 }
3782 }
3783
3784 return MatchA && MatchB;
3785}
3786
Craig Topper70b883b2011-11-28 10:14:51 +00003787/// getShuffleVPERM2X128Immediate - Return the appropriate immediate to shuffle
3788/// the specified VECTOR_MASK mask with VPERM2F128/VPERM2I128 instructions.
Craig Topperd93e4c32011-12-11 19:12:35 +00003789static unsigned getShuffleVPERM2X128Immediate(ShuffleVectorSDNode *SVOp) {
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003790 EVT VT = SVOp->getValueType(0);
3791
Craig Topperc612d792012-01-02 09:17:37 +00003792 unsigned HalfSize = VT.getVectorNumElements()/2;
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003793
Craig Topperc612d792012-01-02 09:17:37 +00003794 unsigned FstHalf = 0, SndHalf = 0;
3795 for (unsigned i = 0; i < HalfSize; ++i) {
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003796 if (SVOp->getMaskElt(i) > 0) {
3797 FstHalf = SVOp->getMaskElt(i)/HalfSize;
3798 break;
3799 }
3800 }
Craig Topperc612d792012-01-02 09:17:37 +00003801 for (unsigned i = HalfSize; i < HalfSize*2; ++i) {
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003802 if (SVOp->getMaskElt(i) > 0) {
3803 SndHalf = SVOp->getMaskElt(i)/HalfSize;
3804 break;
3805 }
3806 }
3807
3808 return (FstHalf | (SndHalf << 4));
3809}
3810
Craig Topper70b883b2011-11-28 10:14:51 +00003811/// isVPERMILPMask - Return true if the specified VECTOR_SHUFFLE operand
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00003812/// specifies a shuffle of elements that is suitable for input to VPERMILPD*.
3813/// Note that VPERMIL mask matching is different depending whether theunderlying
3814/// type is 32 or 64. In the VPERMILPS the high half of the mask should point
3815/// to the same elements of the low, but to the higher half of the source.
3816/// In VPERMILPD the two lanes could be shuffled independently of each other
Craig Topperdbd98a42012-02-07 06:28:42 +00003817/// with the same restriction that lanes can't be crossed. Also handles PSHUFDY.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003818static bool isVPERMILPMask(ArrayRef<int> Mask, EVT VT, bool HasAVX) {
Craig Topper70b883b2011-11-28 10:14:51 +00003819 if (!HasAVX)
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00003820 return false;
3821
Craig Topperc612d792012-01-02 09:17:37 +00003822 unsigned NumElts = VT.getVectorNumElements();
Craig Topper70b883b2011-11-28 10:14:51 +00003823 // Only match 256-bit with 32/64-bit types
3824 if (VT.getSizeInBits() != 256 || (NumElts != 4 && NumElts != 8))
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00003825 return false;
3826
Craig Topperc612d792012-01-02 09:17:37 +00003827 unsigned NumLanes = VT.getSizeInBits()/128;
3828 unsigned LaneSize = NumElts/NumLanes;
Craig Topper1a7700a2012-01-19 08:19:12 +00003829 for (unsigned l = 0; l != NumElts; l += LaneSize) {
Craig Topperc612d792012-01-02 09:17:37 +00003830 for (unsigned i = 0; i != LaneSize; ++i) {
Craig Topper1a7700a2012-01-19 08:19:12 +00003831 if (!isUndefOrInRange(Mask[i+l], l, l+LaneSize))
Craig Topper70b883b2011-11-28 10:14:51 +00003832 return false;
Craig Topper1a7700a2012-01-19 08:19:12 +00003833 if (NumElts != 8 || l == 0)
Craig Topper70b883b2011-11-28 10:14:51 +00003834 continue;
3835 // VPERMILPS handling
3836 if (Mask[i] < 0)
3837 continue;
Craig Topper1a7700a2012-01-19 08:19:12 +00003838 if (!isUndefOrEqual(Mask[i+l], Mask[i]+l))
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00003839 return false;
3840 }
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00003841 }
3842
3843 return true;
3844}
3845
Craig Topper5aaffa82012-02-19 02:53:47 +00003846/// isCommutedMOVLMask - Returns true if the shuffle mask is except the reverse
Evan Cheng017dcc62006-04-21 01:05:10 +00003847/// of what x86 movss want. X86 movs requires the lowest element to be lowest
Evan Cheng39623da2006-04-20 08:58:49 +00003848/// element of vector 2 and the other elements to come from vector 1 in order.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003849static bool isCommutedMOVLMask(ArrayRef<int> Mask, EVT VT,
Nate Begeman9008ca62009-04-27 18:41:29 +00003850 bool V2IsSplat = false, bool V2IsUndef = false) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00003851 if (!VT.is128BitVector())
Craig Topper97327dc2012-03-18 22:50:10 +00003852 return false;
Craig Topper7a9a28b2012-08-12 02:23:29 +00003853
3854 unsigned NumOps = VT.getVectorNumElements();
Chris Lattner5a88b832007-02-25 07:10:00 +00003855 if (NumOps != 2 && NumOps != 4 && NumOps != 8 && NumOps != 16)
Evan Cheng39623da2006-04-20 08:58:49 +00003856 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003857
Nate Begeman9008ca62009-04-27 18:41:29 +00003858 if (!isUndefOrEqual(Mask[0], 0))
Evan Cheng39623da2006-04-20 08:58:49 +00003859 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003860
Craig Topperc612d792012-01-02 09:17:37 +00003861 for (unsigned i = 1; i != NumOps; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003862 if (!(isUndefOrEqual(Mask[i], i+NumOps) ||
3863 (V2IsUndef && isUndefOrInRange(Mask[i], NumOps, NumOps*2)) ||
3864 (V2IsSplat && isUndefOrEqual(Mask[i], NumOps))))
Evan Cheng8cf723d2006-09-08 01:50:06 +00003865 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003866
Evan Cheng39623da2006-04-20 08:58:49 +00003867 return true;
3868}
3869
Evan Chengd9539472006-04-14 21:59:03 +00003870/// isMOVSHDUPMask - Return true if the specified VECTOR_SHUFFLE operand
3871/// specifies a shuffle of elements that is suitable for input to MOVSHDUP.
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003872/// Masks to match: <1, 1, 3, 3> or <1, 1, 3, 3, 5, 5, 7, 7>
Craig Topperdd637ae2012-02-19 05:41:45 +00003873static bool isMOVSHDUPMask(ArrayRef<int> Mask, EVT VT,
Craig Topper5aaffa82012-02-19 02:53:47 +00003874 const X86Subtarget *Subtarget) {
Craig Topperd0a31172012-01-10 06:37:29 +00003875 if (!Subtarget->hasSSE3())
Evan Chengd9539472006-04-14 21:59:03 +00003876 return false;
3877
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003878 unsigned NumElems = VT.getVectorNumElements();
3879
3880 if ((VT.getSizeInBits() == 128 && NumElems != 4) ||
3881 (VT.getSizeInBits() == 256 && NumElems != 8))
3882 return false;
3883
3884 // "i+1" is the value the indexed mask element must have
Craig Topperdd637ae2012-02-19 05:41:45 +00003885 for (unsigned i = 0; i != NumElems; i += 2)
3886 if (!isUndefOrEqual(Mask[i], i+1) ||
3887 !isUndefOrEqual(Mask[i+1], i+1))
Nate Begeman9008ca62009-04-27 18:41:29 +00003888 return false;
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003889
3890 return true;
Evan Chengd9539472006-04-14 21:59:03 +00003891}
3892
3893/// isMOVSLDUPMask - Return true if the specified VECTOR_SHUFFLE operand
3894/// specifies a shuffle of elements that is suitable for input to MOVSLDUP.
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003895/// Masks to match: <0, 0, 2, 2> or <0, 0, 2, 2, 4, 4, 6, 6>
Craig Topperdd637ae2012-02-19 05:41:45 +00003896static bool isMOVSLDUPMask(ArrayRef<int> Mask, EVT VT,
Craig Topper5aaffa82012-02-19 02:53:47 +00003897 const X86Subtarget *Subtarget) {
Craig Topperd0a31172012-01-10 06:37:29 +00003898 if (!Subtarget->hasSSE3())
Evan Chengd9539472006-04-14 21:59:03 +00003899 return false;
3900
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003901 unsigned NumElems = VT.getVectorNumElements();
3902
3903 if ((VT.getSizeInBits() == 128 && NumElems != 4) ||
3904 (VT.getSizeInBits() == 256 && NumElems != 8))
3905 return false;
3906
3907 // "i" is the value the indexed mask element must have
Craig Topperc612d792012-01-02 09:17:37 +00003908 for (unsigned i = 0; i != NumElems; i += 2)
Craig Topperdd637ae2012-02-19 05:41:45 +00003909 if (!isUndefOrEqual(Mask[i], i) ||
3910 !isUndefOrEqual(Mask[i+1], i))
Nate Begeman9008ca62009-04-27 18:41:29 +00003911 return false;
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003912
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003913 return true;
Evan Chengd9539472006-04-14 21:59:03 +00003914}
3915
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00003916/// isMOVDDUPYMask - Return true if the specified VECTOR_SHUFFLE operand
3917/// specifies a shuffle of elements that is suitable for input to 256-bit
3918/// version of MOVDDUP.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003919static bool isMOVDDUPYMask(ArrayRef<int> Mask, EVT VT, bool HasAVX) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00003920 if (!HasAVX || !VT.is256BitVector())
3921 return false;
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00003922
Craig Topper7a9a28b2012-08-12 02:23:29 +00003923 unsigned NumElts = VT.getVectorNumElements();
3924 if (NumElts != 4)
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00003925 return false;
3926
Craig Topperc612d792012-01-02 09:17:37 +00003927 for (unsigned i = 0; i != NumElts/2; ++i)
Craig Topperbeabc6c2011-12-05 06:56:46 +00003928 if (!isUndefOrEqual(Mask[i], 0))
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00003929 return false;
Craig Topperc612d792012-01-02 09:17:37 +00003930 for (unsigned i = NumElts/2; i != NumElts; ++i)
Craig Topperbeabc6c2011-12-05 06:56:46 +00003931 if (!isUndefOrEqual(Mask[i], NumElts/2))
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00003932 return false;
3933 return true;
3934}
3935
Evan Cheng0b457f02008-09-25 20:50:48 +00003936/// isMOVDDUPMask - Return true if the specified VECTOR_SHUFFLE operand
Bruno Cardoso Lopes06ef9232011-08-25 21:40:34 +00003937/// specifies a shuffle of elements that is suitable for input to 128-bit
3938/// version of MOVDDUP.
Craig Topperdd637ae2012-02-19 05:41:45 +00003939static bool isMOVDDUPMask(ArrayRef<int> Mask, EVT VT) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00003940 if (!VT.is128BitVector())
Bruno Cardoso Lopes06ef9232011-08-25 21:40:34 +00003941 return false;
3942
Craig Topperc612d792012-01-02 09:17:37 +00003943 unsigned e = VT.getVectorNumElements() / 2;
3944 for (unsigned i = 0; i != e; ++i)
Craig Topperdd637ae2012-02-19 05:41:45 +00003945 if (!isUndefOrEqual(Mask[i], i))
Evan Cheng0b457f02008-09-25 20:50:48 +00003946 return false;
Craig Topperc612d792012-01-02 09:17:37 +00003947 for (unsigned i = 0; i != e; ++i)
Craig Topperdd637ae2012-02-19 05:41:45 +00003948 if (!isUndefOrEqual(Mask[e+i], i))
Evan Cheng0b457f02008-09-25 20:50:48 +00003949 return false;
3950 return true;
3951}
3952
David Greenec38a03e2011-02-03 15:50:00 +00003953/// isVEXTRACTF128Index - Return true if the specified
3954/// EXTRACT_SUBVECTOR operand specifies a vector extract that is
3955/// suitable for input to VEXTRACTF128.
3956bool X86::isVEXTRACTF128Index(SDNode *N) {
3957 if (!isa<ConstantSDNode>(N->getOperand(1).getNode()))
3958 return false;
3959
3960 // The index should be aligned on a 128-bit boundary.
3961 uint64_t Index =
3962 cast<ConstantSDNode>(N->getOperand(1).getNode())->getZExtValue();
3963
3964 unsigned VL = N->getValueType(0).getVectorNumElements();
3965 unsigned VBits = N->getValueType(0).getSizeInBits();
3966 unsigned ElSize = VBits / VL;
3967 bool Result = (Index * ElSize) % 128 == 0;
3968
3969 return Result;
3970}
3971
David Greeneccacdc12011-02-04 16:08:29 +00003972/// isVINSERTF128Index - Return true if the specified INSERT_SUBVECTOR
3973/// operand specifies a subvector insert that is suitable for input to
3974/// VINSERTF128.
3975bool X86::isVINSERTF128Index(SDNode *N) {
3976 if (!isa<ConstantSDNode>(N->getOperand(2).getNode()))
3977 return false;
3978
3979 // The index should be aligned on a 128-bit boundary.
3980 uint64_t Index =
3981 cast<ConstantSDNode>(N->getOperand(2).getNode())->getZExtValue();
3982
3983 unsigned VL = N->getValueType(0).getVectorNumElements();
3984 unsigned VBits = N->getValueType(0).getSizeInBits();
3985 unsigned ElSize = VBits / VL;
3986 bool Result = (Index * ElSize) % 128 == 0;
3987
3988 return Result;
3989}
3990
Evan Cheng63d33002006-03-22 08:01:21 +00003991/// getShuffleSHUFImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00003992/// the specified VECTOR_SHUFFLE mask with PSHUF* and SHUFP* instructions.
Craig Topper1a7700a2012-01-19 08:19:12 +00003993/// Handles 128-bit and 256-bit.
Craig Topper5aaffa82012-02-19 02:53:47 +00003994static unsigned getShuffleSHUFImmediate(ShuffleVectorSDNode *N) {
Craig Topper1a7700a2012-01-19 08:19:12 +00003995 EVT VT = N->getValueType(0);
Nate Begeman9008ca62009-04-27 18:41:29 +00003996
Craig Topper1a7700a2012-01-19 08:19:12 +00003997 assert((VT.is128BitVector() || VT.is256BitVector()) &&
3998 "Unsupported vector type for PSHUF/SHUFP");
3999
4000 // Handle 128 and 256-bit vector lengths. AVX defines PSHUF/SHUFP to operate
4001 // independently on 128-bit lanes.
4002 unsigned NumElts = VT.getVectorNumElements();
4003 unsigned NumLanes = VT.getSizeInBits()/128;
4004 unsigned NumLaneElts = NumElts/NumLanes;
4005
4006 assert((NumLaneElts == 2 || NumLaneElts == 4) &&
4007 "Only supports 2 or 4 elements per lane");
4008
4009 unsigned Shift = (NumLaneElts == 4) ? 1 : 0;
Evan Chengb9df0ca2006-03-22 02:53:00 +00004010 unsigned Mask = 0;
Craig Topper1a7700a2012-01-19 08:19:12 +00004011 for (unsigned i = 0; i != NumElts; ++i) {
4012 int Elt = N->getMaskElt(i);
4013 if (Elt < 0) continue;
Craig Topper6b28d352012-05-03 07:12:59 +00004014 Elt &= NumLaneElts - 1;
4015 unsigned ShAmt = (i << Shift) % 8;
Craig Topper1a7700a2012-01-19 08:19:12 +00004016 Mask |= Elt << ShAmt;
Evan Cheng36b27f32006-03-28 23:41:33 +00004017 }
Craig Topper1a7700a2012-01-19 08:19:12 +00004018
Evan Cheng63d33002006-03-22 08:01:21 +00004019 return Mask;
4020}
4021
Evan Cheng506d3df2006-03-29 23:07:14 +00004022/// getShufflePSHUFHWImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00004023/// the specified VECTOR_SHUFFLE mask with the PSHUFHW instruction.
Craig Topperdd637ae2012-02-19 05:41:45 +00004024static unsigned getShufflePSHUFHWImmediate(ShuffleVectorSDNode *N) {
Craig Topper6b28d352012-05-03 07:12:59 +00004025 EVT VT = N->getValueType(0);
4026
4027 assert((VT == MVT::v8i16 || VT == MVT::v16i16) &&
4028 "Unsupported vector type for PSHUFHW");
4029
4030 unsigned NumElts = VT.getVectorNumElements();
4031
Evan Cheng506d3df2006-03-29 23:07:14 +00004032 unsigned Mask = 0;
Craig Topper6b28d352012-05-03 07:12:59 +00004033 for (unsigned l = 0; l != NumElts; l += 8) {
4034 // 8 nodes per lane, but we only care about the last 4.
4035 for (unsigned i = 0; i < 4; ++i) {
4036 int Elt = N->getMaskElt(l+i+4);
4037 if (Elt < 0) continue;
4038 Elt &= 0x3; // only 2-bits.
4039 Mask |= Elt << (i * 2);
4040 }
Evan Cheng506d3df2006-03-29 23:07:14 +00004041 }
Craig Topper6b28d352012-05-03 07:12:59 +00004042
Evan Cheng506d3df2006-03-29 23:07:14 +00004043 return Mask;
4044}
4045
4046/// getShufflePSHUFLWImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00004047/// the specified VECTOR_SHUFFLE mask with the PSHUFLW instruction.
Craig Topperdd637ae2012-02-19 05:41:45 +00004048static unsigned getShufflePSHUFLWImmediate(ShuffleVectorSDNode *N) {
Craig Topper6b28d352012-05-03 07:12:59 +00004049 EVT VT = N->getValueType(0);
4050
4051 assert((VT == MVT::v8i16 || VT == MVT::v16i16) &&
4052 "Unsupported vector type for PSHUFHW");
4053
4054 unsigned NumElts = VT.getVectorNumElements();
4055
Evan Cheng506d3df2006-03-29 23:07:14 +00004056 unsigned Mask = 0;
Craig Topper6b28d352012-05-03 07:12:59 +00004057 for (unsigned l = 0; l != NumElts; l += 8) {
4058 // 8 nodes per lane, but we only care about the first 4.
4059 for (unsigned i = 0; i < 4; ++i) {
4060 int Elt = N->getMaskElt(l+i);
4061 if (Elt < 0) continue;
4062 Elt &= 0x3; // only 2-bits
4063 Mask |= Elt << (i * 2);
4064 }
Evan Cheng506d3df2006-03-29 23:07:14 +00004065 }
Craig Topper6b28d352012-05-03 07:12:59 +00004066
Evan Cheng506d3df2006-03-29 23:07:14 +00004067 return Mask;
4068}
4069
Nate Begemana09008b2009-10-19 02:17:23 +00004070/// getShufflePALIGNRImmediate - Return the appropriate immediate to shuffle
4071/// the specified VECTOR_SHUFFLE mask with the PALIGNR instruction.
Craig Topperd93e4c32011-12-11 19:12:35 +00004072static unsigned getShufflePALIGNRImmediate(ShuffleVectorSDNode *SVOp) {
4073 EVT VT = SVOp->getValueType(0);
4074 unsigned EltSize = VT.getVectorElementType().getSizeInBits() >> 3;
Nate Begemana09008b2009-10-19 02:17:23 +00004075
Craig Topper0e2037b2012-01-20 05:53:00 +00004076 unsigned NumElts = VT.getVectorNumElements();
4077 unsigned NumLanes = VT.getSizeInBits()/128;
4078 unsigned NumLaneElts = NumElts/NumLanes;
4079
4080 int Val = 0;
4081 unsigned i;
4082 for (i = 0; i != NumElts; ++i) {
Nate Begemana09008b2009-10-19 02:17:23 +00004083 Val = SVOp->getMaskElt(i);
4084 if (Val >= 0)
4085 break;
4086 }
Craig Topper0e2037b2012-01-20 05:53:00 +00004087 if (Val >= (int)NumElts)
4088 Val -= NumElts - NumLaneElts;
4089
Eli Friedman63f8dde2011-07-25 21:36:45 +00004090 assert(Val - i > 0 && "PALIGNR imm should be positive");
Nate Begemana09008b2009-10-19 02:17:23 +00004091 return (Val - i) * EltSize;
4092}
4093
David Greenec38a03e2011-02-03 15:50:00 +00004094/// getExtractVEXTRACTF128Immediate - Return the appropriate immediate
4095/// to extract the specified EXTRACT_SUBVECTOR index with VEXTRACTF128
4096/// instructions.
4097unsigned X86::getExtractVEXTRACTF128Immediate(SDNode *N) {
4098 if (!isa<ConstantSDNode>(N->getOperand(1).getNode()))
4099 llvm_unreachable("Illegal extract subvector for VEXTRACTF128");
4100
4101 uint64_t Index =
4102 cast<ConstantSDNode>(N->getOperand(1).getNode())->getZExtValue();
4103
4104 EVT VecVT = N->getOperand(0).getValueType();
4105 EVT ElVT = VecVT.getVectorElementType();
4106
4107 unsigned NumElemsPerChunk = 128 / ElVT.getSizeInBits();
David Greenec38a03e2011-02-03 15:50:00 +00004108 return Index / NumElemsPerChunk;
4109}
4110
David Greeneccacdc12011-02-04 16:08:29 +00004111/// getInsertVINSERTF128Immediate - Return the appropriate immediate
4112/// to insert at the specified INSERT_SUBVECTOR index with VINSERTF128
4113/// instructions.
4114unsigned X86::getInsertVINSERTF128Immediate(SDNode *N) {
4115 if (!isa<ConstantSDNode>(N->getOperand(2).getNode()))
4116 llvm_unreachable("Illegal insert subvector for VINSERTF128");
4117
4118 uint64_t Index =
NAKAMURA Takumi27635382011-02-05 15:10:54 +00004119 cast<ConstantSDNode>(N->getOperand(2).getNode())->getZExtValue();
David Greeneccacdc12011-02-04 16:08:29 +00004120
4121 EVT VecVT = N->getValueType(0);
4122 EVT ElVT = VecVT.getVectorElementType();
4123
4124 unsigned NumElemsPerChunk = 128 / ElVT.getSizeInBits();
David Greeneccacdc12011-02-04 16:08:29 +00004125 return Index / NumElemsPerChunk;
4126}
4127
Elena Demikhovsky73c504a2012-04-15 11:18:59 +00004128/// getShuffleCLImmediate - Return the appropriate immediate to shuffle
4129/// the specified VECTOR_SHUFFLE mask with VPERMQ and VPERMPD instructions.
4130/// Handles 256-bit.
4131static unsigned getShuffleCLImmediate(ShuffleVectorSDNode *N) {
4132 EVT VT = N->getValueType(0);
4133
Elena Demikhovsky73c504a2012-04-15 11:18:59 +00004134 unsigned NumElts = VT.getVectorNumElements();
4135
Craig Topper095c5282012-04-15 23:48:57 +00004136 assert((VT.is256BitVector() && NumElts == 4) &&
4137 "Unsupported vector type for VPERMQ/VPERMPD");
4138
Elena Demikhovsky73c504a2012-04-15 11:18:59 +00004139 unsigned Mask = 0;
4140 for (unsigned i = 0; i != NumElts; ++i) {
4141 int Elt = N->getMaskElt(i);
Craig Topper095c5282012-04-15 23:48:57 +00004142 if (Elt < 0)
Elena Demikhovsky73c504a2012-04-15 11:18:59 +00004143 continue;
4144 Mask |= Elt << (i*2);
4145 }
4146
4147 return Mask;
4148}
Evan Cheng37b73872009-07-30 08:33:02 +00004149/// isZeroNode - Returns true if Elt is a constant zero or a floating point
4150/// constant +0.0.
4151bool X86::isZeroNode(SDValue Elt) {
4152 return ((isa<ConstantSDNode>(Elt) &&
Dan Gohmane368b462010-06-18 14:22:04 +00004153 cast<ConstantSDNode>(Elt)->isNullValue()) ||
Evan Cheng37b73872009-07-30 08:33:02 +00004154 (isa<ConstantFPSDNode>(Elt) &&
4155 cast<ConstantFPSDNode>(Elt)->getValueAPF().isPosZero()));
4156}
4157
Nate Begeman9008ca62009-04-27 18:41:29 +00004158/// CommuteVectorShuffle - Swap vector_shuffle operands as well as values in
4159/// their permute mask.
4160static SDValue CommuteVectorShuffle(ShuffleVectorSDNode *SVOp,
4161 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00004162 EVT VT = SVOp->getValueType(0);
Nate Begeman5a5ca152009-04-29 05:20:52 +00004163 unsigned NumElems = VT.getVectorNumElements();
Nate Begeman9008ca62009-04-27 18:41:29 +00004164 SmallVector<int, 8> MaskVec;
Eric Christopherfd179292009-08-27 18:07:15 +00004165
Nate Begeman5a5ca152009-04-29 05:20:52 +00004166 for (unsigned i = 0; i != NumElems; ++i) {
Craig Topper8ae97ba2012-05-21 06:40:16 +00004167 int Idx = SVOp->getMaskElt(i);
4168 if (Idx >= 0) {
4169 if (Idx < (int)NumElems)
4170 Idx += NumElems;
4171 else
4172 Idx -= NumElems;
4173 }
4174 MaskVec.push_back(Idx);
Evan Cheng5ced1d82006-04-06 23:23:56 +00004175 }
Nate Begeman9008ca62009-04-27 18:41:29 +00004176 return DAG.getVectorShuffle(VT, SVOp->getDebugLoc(), SVOp->getOperand(1),
4177 SVOp->getOperand(0), &MaskVec[0]);
Evan Cheng5ced1d82006-04-06 23:23:56 +00004178}
4179
Evan Cheng533a0aa2006-04-19 20:35:22 +00004180/// ShouldXformToMOVHLPS - Return true if the node should be transformed to
4181/// match movhlps. The lower half elements should come from upper half of
4182/// V1 (and in order), and the upper half elements should come from the upper
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00004183/// half of V2 (and in order).
Craig Topperdd637ae2012-02-19 05:41:45 +00004184static bool ShouldXformToMOVHLPS(ArrayRef<int> Mask, EVT VT) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00004185 if (!VT.is128BitVector())
Bruno Cardoso Lopes59353b42011-08-11 18:59:13 +00004186 return false;
4187 if (VT.getVectorNumElements() != 4)
Evan Cheng533a0aa2006-04-19 20:35:22 +00004188 return false;
4189 for (unsigned i = 0, e = 2; i != e; ++i)
Craig Topperdd637ae2012-02-19 05:41:45 +00004190 if (!isUndefOrEqual(Mask[i], i+2))
Evan Cheng533a0aa2006-04-19 20:35:22 +00004191 return false;
4192 for (unsigned i = 2; i != 4; ++i)
Craig Topperdd637ae2012-02-19 05:41:45 +00004193 if (!isUndefOrEqual(Mask[i], i+4))
Evan Cheng533a0aa2006-04-19 20:35:22 +00004194 return false;
4195 return true;
4196}
4197
Evan Cheng5ced1d82006-04-06 23:23:56 +00004198/// isScalarLoadToVector - Returns true if the node is a scalar load that
Evan Cheng7e2ff772008-05-08 00:57:18 +00004199/// is promoted to a vector. It also returns the LoadSDNode by reference if
4200/// required.
4201static bool isScalarLoadToVector(SDNode *N, LoadSDNode **LD = NULL) {
Evan Cheng0b457f02008-09-25 20:50:48 +00004202 if (N->getOpcode() != ISD::SCALAR_TO_VECTOR)
4203 return false;
4204 N = N->getOperand(0).getNode();
4205 if (!ISD::isNON_EXTLoad(N))
4206 return false;
4207 if (LD)
4208 *LD = cast<LoadSDNode>(N);
4209 return true;
Evan Cheng5ced1d82006-04-06 23:23:56 +00004210}
4211
Dan Gohman65fd6562011-11-03 21:49:52 +00004212// Test whether the given value is a vector value which will be legalized
4213// into a load.
4214static bool WillBeConstantPoolLoad(SDNode *N) {
4215 if (N->getOpcode() != ISD::BUILD_VECTOR)
4216 return false;
4217
4218 // Check for any non-constant elements.
4219 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
4220 switch (N->getOperand(i).getNode()->getOpcode()) {
4221 case ISD::UNDEF:
4222 case ISD::ConstantFP:
4223 case ISD::Constant:
4224 break;
4225 default:
4226 return false;
4227 }
4228
4229 // Vectors of all-zeros and all-ones are materialized with special
4230 // instructions rather than being loaded.
4231 return !ISD::isBuildVectorAllZeros(N) &&
4232 !ISD::isBuildVectorAllOnes(N);
4233}
4234
Evan Cheng533a0aa2006-04-19 20:35:22 +00004235/// ShouldXformToMOVLP{S|D} - Return true if the node should be transformed to
4236/// match movlp{s|d}. The lower half elements should come from lower half of
4237/// V1 (and in order), and the upper half elements should come from the upper
4238/// half of V2 (and in order). And since V1 will become the source of the
4239/// MOVLP, it must be either a vector load or a scalar load to vector.
Nate Begeman9008ca62009-04-27 18:41:29 +00004240static bool ShouldXformToMOVLP(SDNode *V1, SDNode *V2,
Craig Topperdd637ae2012-02-19 05:41:45 +00004241 ArrayRef<int> Mask, EVT VT) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00004242 if (!VT.is128BitVector())
Bruno Cardoso Lopes59353b42011-08-11 18:59:13 +00004243 return false;
4244
Evan Cheng466685d2006-10-09 20:57:25 +00004245 if (!ISD::isNON_EXTLoad(V1) && !isScalarLoadToVector(V1))
Evan Cheng533a0aa2006-04-19 20:35:22 +00004246 return false;
Evan Cheng23425f52006-10-09 21:39:25 +00004247 // Is V2 is a vector load, don't do this transformation. We will try to use
4248 // load folding shufps op.
Dan Gohman65fd6562011-11-03 21:49:52 +00004249 if (ISD::isNON_EXTLoad(V2) || WillBeConstantPoolLoad(V2))
Evan Cheng23425f52006-10-09 21:39:25 +00004250 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00004251
Bruno Cardoso Lopes59353b42011-08-11 18:59:13 +00004252 unsigned NumElems = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00004253
Evan Cheng533a0aa2006-04-19 20:35:22 +00004254 if (NumElems != 2 && NumElems != 4)
4255 return false;
Nate Begeman5a5ca152009-04-29 05:20:52 +00004256 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
Craig Topperdd637ae2012-02-19 05:41:45 +00004257 if (!isUndefOrEqual(Mask[i], i))
Evan Cheng533a0aa2006-04-19 20:35:22 +00004258 return false;
Chad Rosier238ae312012-04-30 17:47:15 +00004259 for (unsigned i = NumElems/2, e = NumElems; i != e; ++i)
Craig Topperdd637ae2012-02-19 05:41:45 +00004260 if (!isUndefOrEqual(Mask[i], i+NumElems))
Evan Cheng533a0aa2006-04-19 20:35:22 +00004261 return false;
4262 return true;
Evan Cheng5ced1d82006-04-06 23:23:56 +00004263}
4264
Evan Cheng39623da2006-04-20 08:58:49 +00004265/// isSplatVector - Returns true if N is a BUILD_VECTOR node whose elements are
4266/// all the same.
4267static bool isSplatVector(SDNode *N) {
4268 if (N->getOpcode() != ISD::BUILD_VECTOR)
4269 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00004270
Dan Gohman475871a2008-07-27 21:46:04 +00004271 SDValue SplatValue = N->getOperand(0);
Evan Cheng39623da2006-04-20 08:58:49 +00004272 for (unsigned i = 1, e = N->getNumOperands(); i != e; ++i)
4273 if (N->getOperand(i) != SplatValue)
Evan Cheng5ced1d82006-04-06 23:23:56 +00004274 return false;
4275 return true;
4276}
4277
Evan Cheng213d2cf2007-05-17 18:45:50 +00004278/// isZeroShuffle - Returns true if N is a VECTOR_SHUFFLE that can be resolved
Eric Christopherfd179292009-08-27 18:07:15 +00004279/// to an zero vector.
Nate Begeman5a5ca152009-04-29 05:20:52 +00004280/// FIXME: move to dag combiner / method on ShuffleVectorSDNode
Nate Begeman9008ca62009-04-27 18:41:29 +00004281static bool isZeroShuffle(ShuffleVectorSDNode *N) {
Dan Gohman475871a2008-07-27 21:46:04 +00004282 SDValue V1 = N->getOperand(0);
4283 SDValue V2 = N->getOperand(1);
Nate Begeman5a5ca152009-04-29 05:20:52 +00004284 unsigned NumElems = N->getValueType(0).getVectorNumElements();
4285 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004286 int Idx = N->getMaskElt(i);
Nate Begeman5a5ca152009-04-29 05:20:52 +00004287 if (Idx >= (int)NumElems) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004288 unsigned Opc = V2.getOpcode();
Rafael Espindola15684b22009-04-24 12:40:33 +00004289 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V2.getNode()))
4290 continue;
Evan Cheng37b73872009-07-30 08:33:02 +00004291 if (Opc != ISD::BUILD_VECTOR ||
4292 !X86::isZeroNode(V2.getOperand(Idx-NumElems)))
Nate Begeman9008ca62009-04-27 18:41:29 +00004293 return false;
4294 } else if (Idx >= 0) {
4295 unsigned Opc = V1.getOpcode();
4296 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V1.getNode()))
4297 continue;
Evan Cheng37b73872009-07-30 08:33:02 +00004298 if (Opc != ISD::BUILD_VECTOR ||
4299 !X86::isZeroNode(V1.getOperand(Idx)))
Chris Lattner8a594482007-11-25 00:24:49 +00004300 return false;
Evan Cheng213d2cf2007-05-17 18:45:50 +00004301 }
4302 }
4303 return true;
4304}
4305
4306/// getZeroVector - Returns a vector of specified type with all zero elements.
4307///
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00004308static SDValue getZeroVector(EVT VT, const X86Subtarget *Subtarget,
Craig Topper12216172012-01-13 08:12:35 +00004309 SelectionDAG &DAG, DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004310 assert(VT.isVector() && "Expected a vector type");
Craig Topper9d352402012-04-23 07:24:41 +00004311 unsigned Size = VT.getSizeInBits();
Scott Michelfdc40a02009-02-17 22:15:04 +00004312
Dale Johannesen0488fb62010-09-30 23:57:10 +00004313 // Always build SSE zero vectors as <4 x i32> bitcasted
Bruno Cardoso Lopes8c05a852010-08-12 02:06:36 +00004314 // to their dest type. This ensures they get CSE'd.
Dan Gohman475871a2008-07-27 21:46:04 +00004315 SDValue Vec;
Craig Topper9d352402012-04-23 07:24:41 +00004316 if (Size == 128) { // SSE
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00004317 if (Subtarget->hasSSE2()) { // SSE2
Bruno Cardoso Lopes8c05a852010-08-12 02:06:36 +00004318 SDValue Cst = DAG.getTargetConstant(0, MVT::i32);
4319 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
4320 } else { // SSE1
4321 SDValue Cst = DAG.getTargetConstantFP(+0.0, MVT::f32);
4322 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4f32, Cst, Cst, Cst, Cst);
4323 }
Craig Topper9d352402012-04-23 07:24:41 +00004324 } else if (Size == 256) { // AVX
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00004325 if (Subtarget->hasAVX2()) { // AVX2
Craig Topper12216172012-01-13 08:12:35 +00004326 SDValue Cst = DAG.getTargetConstant(0, MVT::i32);
4327 SDValue Ops[] = { Cst, Cst, Cst, Cst, Cst, Cst, Cst, Cst };
4328 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v8i32, Ops, 8);
4329 } else {
4330 // 256-bit logic and arithmetic instructions in AVX are all
4331 // floating-point, no support for integer ops. Emit fp zeroed vectors.
4332 SDValue Cst = DAG.getTargetConstantFP(+0.0, MVT::f32);
4333 SDValue Ops[] = { Cst, Cst, Cst, Cst, Cst, Cst, Cst, Cst };
4334 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v8f32, Ops, 8);
4335 }
Craig Topper9d352402012-04-23 07:24:41 +00004336 } else
4337 llvm_unreachable("Unexpected vector type");
4338
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004339 return DAG.getNode(ISD::BITCAST, dl, VT, Vec);
Evan Cheng213d2cf2007-05-17 18:45:50 +00004340}
4341
Chris Lattner8a594482007-11-25 00:24:49 +00004342/// getOnesVector - Returns a vector of specified type with all bits set.
Craig Topper745a86b2011-11-19 22:34:59 +00004343/// Always build ones vectors as <4 x i32> or <8 x i32>. For 256-bit types with
4344/// no AVX2 supprt, use two <4 x i32> inserted in a <8 x i32> appropriately.
4345/// Then bitcast to their original type, ensuring they get CSE'd.
4346static SDValue getOnesVector(EVT VT, bool HasAVX2, SelectionDAG &DAG,
4347 DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004348 assert(VT.isVector() && "Expected a vector type");
Craig Topper9d352402012-04-23 07:24:41 +00004349 unsigned Size = VT.getSizeInBits();
Scott Michelfdc40a02009-02-17 22:15:04 +00004350
Owen Anderson825b72b2009-08-11 20:47:22 +00004351 SDValue Cst = DAG.getTargetConstant(~0U, MVT::i32);
Craig Topper745a86b2011-11-19 22:34:59 +00004352 SDValue Vec;
Craig Topper9d352402012-04-23 07:24:41 +00004353 if (Size == 256) {
Craig Topper745a86b2011-11-19 22:34:59 +00004354 if (HasAVX2) { // AVX2
4355 SDValue Ops[] = { Cst, Cst, Cst, Cst, Cst, Cst, Cst, Cst };
4356 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v8i32, Ops, 8);
4357 } else { // AVX
4358 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
Craig Topper4c7972d2012-04-22 18:15:59 +00004359 Vec = Concat128BitVectors(Vec, Vec, MVT::v8i32, 8, DAG, dl);
Craig Topper745a86b2011-11-19 22:34:59 +00004360 }
Craig Topper9d352402012-04-23 07:24:41 +00004361 } else if (Size == 128) {
Craig Topper745a86b2011-11-19 22:34:59 +00004362 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
Craig Topper9d352402012-04-23 07:24:41 +00004363 } else
4364 llvm_unreachable("Unexpected vector type");
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +00004365
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004366 return DAG.getNode(ISD::BITCAST, dl, VT, Vec);
Chris Lattner8a594482007-11-25 00:24:49 +00004367}
4368
Evan Cheng39623da2006-04-20 08:58:49 +00004369/// NormalizeMask - V2 is a splat, modify the mask (if needed) so all elements
4370/// that point to V2 points to its first element.
Craig Topper39a9e482012-02-11 06:24:48 +00004371static void NormalizeMask(SmallVectorImpl<int> &Mask, unsigned NumElems) {
Nate Begeman5a5ca152009-04-29 05:20:52 +00004372 for (unsigned i = 0; i != NumElems; ++i) {
Craig Topper39a9e482012-02-11 06:24:48 +00004373 if (Mask[i] > (int)NumElems) {
4374 Mask[i] = NumElems;
Evan Cheng39623da2006-04-20 08:58:49 +00004375 }
Evan Cheng39623da2006-04-20 08:58:49 +00004376 }
Evan Cheng39623da2006-04-20 08:58:49 +00004377}
4378
Evan Cheng017dcc62006-04-21 01:05:10 +00004379/// getMOVLMask - Returns a vector_shuffle mask for an movs{s|d}, movd
4380/// operation of specified width.
Owen Andersone50ed302009-08-10 22:56:29 +00004381static SDValue getMOVL(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00004382 SDValue V2) {
4383 unsigned NumElems = VT.getVectorNumElements();
4384 SmallVector<int, 8> Mask;
4385 Mask.push_back(NumElems);
Evan Cheng39623da2006-04-20 08:58:49 +00004386 for (unsigned i = 1; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004387 Mask.push_back(i);
4388 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Evan Cheng39623da2006-04-20 08:58:49 +00004389}
4390
Nate Begeman9008ca62009-04-27 18:41:29 +00004391/// getUnpackl - Returns a vector_shuffle node for an unpackl operation.
Owen Andersone50ed302009-08-10 22:56:29 +00004392static SDValue getUnpackl(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00004393 SDValue V2) {
4394 unsigned NumElems = VT.getVectorNumElements();
4395 SmallVector<int, 8> Mask;
Evan Chengc575ca22006-04-17 20:43:08 +00004396 for (unsigned i = 0, e = NumElems/2; i != e; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004397 Mask.push_back(i);
4398 Mask.push_back(i + NumElems);
Evan Chengc575ca22006-04-17 20:43:08 +00004399 }
Nate Begeman9008ca62009-04-27 18:41:29 +00004400 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Evan Chengc575ca22006-04-17 20:43:08 +00004401}
4402
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004403/// getUnpackh - Returns a vector_shuffle node for an unpackh operation.
Owen Andersone50ed302009-08-10 22:56:29 +00004404static SDValue getUnpackh(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00004405 SDValue V2) {
4406 unsigned NumElems = VT.getVectorNumElements();
Nate Begeman9008ca62009-04-27 18:41:29 +00004407 SmallVector<int, 8> Mask;
Chad Rosier238ae312012-04-30 17:47:15 +00004408 for (unsigned i = 0, Half = NumElems/2; i != Half; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004409 Mask.push_back(i + Half);
4410 Mask.push_back(i + NumElems + Half);
Evan Cheng39623da2006-04-20 08:58:49 +00004411 }
Nate Begeman9008ca62009-04-27 18:41:29 +00004412 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Chris Lattner62098042008-03-09 01:05:04 +00004413}
4414
Bruno Cardoso Lopes5f1d8ab2011-08-11 02:49:44 +00004415// PromoteSplati8i16 - All i16 and i8 vector types can't be used directly by
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004416// a generic shuffle instruction because the target has no such instructions.
4417// Generate shuffles which repeat i16 and i8 several times until they can be
4418// represented by v4f32 and then be manipulated by target suported shuffles.
Bruno Cardoso Lopes5f1d8ab2011-08-11 02:49:44 +00004419static SDValue PromoteSplati8i16(SDValue V, SelectionDAG &DAG, int &EltNo) {
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004420 EVT VT = V.getValueType();
Nate Begeman9008ca62009-04-27 18:41:29 +00004421 int NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004422 DebugLoc dl = V.getDebugLoc();
Rafael Espindola15684b22009-04-24 12:40:33 +00004423
Nate Begeman9008ca62009-04-27 18:41:29 +00004424 while (NumElems > 4) {
4425 if (EltNo < NumElems/2) {
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004426 V = getUnpackl(DAG, dl, VT, V, V);
Nate Begeman9008ca62009-04-27 18:41:29 +00004427 } else {
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004428 V = getUnpackh(DAG, dl, VT, V, V);
Nate Begeman9008ca62009-04-27 18:41:29 +00004429 EltNo -= NumElems/2;
4430 }
4431 NumElems >>= 1;
4432 }
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004433 return V;
4434}
Eric Christopherfd179292009-08-27 18:07:15 +00004435
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004436/// getLegalSplat - Generate a legal splat with supported x86 shuffles
4437static SDValue getLegalSplat(SelectionDAG &DAG, SDValue V, int EltNo) {
4438 EVT VT = V.getValueType();
4439 DebugLoc dl = V.getDebugLoc();
Craig Topper9d352402012-04-23 07:24:41 +00004440 unsigned Size = VT.getSizeInBits();
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004441
Craig Topper9d352402012-04-23 07:24:41 +00004442 if (Size == 128) {
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004443 V = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, V);
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004444 int SplatMask[4] = { EltNo, EltNo, EltNo, EltNo };
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004445 V = DAG.getVectorShuffle(MVT::v4f32, dl, V, DAG.getUNDEF(MVT::v4f32),
4446 &SplatMask[0]);
Craig Topper9d352402012-04-23 07:24:41 +00004447 } else if (Size == 256) {
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004448 // To use VPERMILPS to splat scalars, the second half of indicies must
4449 // refer to the higher part, which is a duplication of the lower one,
4450 // because VPERMILPS can only handle in-lane permutations.
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004451 int SplatMask[8] = { EltNo, EltNo, EltNo, EltNo,
4452 EltNo+4, EltNo+4, EltNo+4, EltNo+4 };
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004453
4454 V = DAG.getNode(ISD::BITCAST, dl, MVT::v8f32, V);
4455 V = DAG.getVectorShuffle(MVT::v8f32, dl, V, DAG.getUNDEF(MVT::v8f32),
4456 &SplatMask[0]);
Craig Topper9d352402012-04-23 07:24:41 +00004457 } else
4458 llvm_unreachable("Vector size not supported");
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004459
4460 return DAG.getNode(ISD::BITCAST, dl, VT, V);
4461}
4462
Bruno Cardoso Lopes8a5b2622011-08-17 02:29:13 +00004463/// PromoteSplat - Splat is promoted to target supported vector shuffles.
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004464static SDValue PromoteSplat(ShuffleVectorSDNode *SV, SelectionDAG &DAG) {
4465 EVT SrcVT = SV->getValueType(0);
4466 SDValue V1 = SV->getOperand(0);
4467 DebugLoc dl = SV->getDebugLoc();
4468
4469 int EltNo = SV->getSplatIndex();
4470 int NumElems = SrcVT.getVectorNumElements();
4471 unsigned Size = SrcVT.getSizeInBits();
4472
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004473 assert(((Size == 128 && NumElems > 4) || Size == 256) &&
4474 "Unknown how to promote splat for type");
4475
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004476 // Extract the 128-bit part containing the splat element and update
4477 // the splat element index when it refers to the higher register.
4478 if (Size == 256) {
Craig Topper7d1e3dc2012-04-30 05:17:10 +00004479 V1 = Extract128BitVector(V1, EltNo, DAG, dl);
4480 if (EltNo >= NumElems/2)
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004481 EltNo -= NumElems/2;
4482 }
4483
Bruno Cardoso Lopes8a5b2622011-08-17 02:29:13 +00004484 // All i16 and i8 vector types can't be used directly by a generic shuffle
4485 // instruction because the target has no such instruction. Generate shuffles
4486 // which repeat i16 and i8 several times until they fit in i32, and then can
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004487 // be manipulated by target suported shuffles.
Bruno Cardoso Lopes5f1d8ab2011-08-11 02:49:44 +00004488 EVT EltVT = SrcVT.getVectorElementType();
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004489 if (EltVT == MVT::i8 || EltVT == MVT::i16)
Bruno Cardoso Lopes5f1d8ab2011-08-11 02:49:44 +00004490 V1 = PromoteSplati8i16(V1, DAG, EltNo);
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004491
4492 // Recreate the 256-bit vector and place the same 128-bit vector
4493 // into the low and high part. This is necessary because we want
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004494 // to use VPERM* to shuffle the vectors
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004495 if (Size == 256) {
Craig Topper4c7972d2012-04-22 18:15:59 +00004496 V1 = DAG.getNode(ISD::CONCAT_VECTORS, dl, SrcVT, V1, V1);
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004497 }
4498
4499 return getLegalSplat(DAG, V1, EltNo);
Evan Chengc575ca22006-04-17 20:43:08 +00004500}
4501
Evan Chengba05f722006-04-21 23:03:30 +00004502/// getShuffleVectorZeroOrUndef - Return a vector_shuffle of the specified
Chris Lattner8a594482007-11-25 00:24:49 +00004503/// vector of zero or undef vector. This produces a shuffle where the low
4504/// element of V2 is swizzled into the zero/undef vector, landing at element
4505/// 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 +00004506static SDValue getShuffleVectorZeroOrUndef(SDValue V2, unsigned Idx,
Craig Topper12216172012-01-13 08:12:35 +00004507 bool IsZero,
4508 const X86Subtarget *Subtarget,
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00004509 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00004510 EVT VT = V2.getValueType();
Craig Topper12216172012-01-13 08:12:35 +00004511 SDValue V1 = IsZero
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00004512 ? getZeroVector(VT, Subtarget, DAG, V2.getDebugLoc()) : DAG.getUNDEF(VT);
Nate Begeman9008ca62009-04-27 18:41:29 +00004513 unsigned NumElems = VT.getVectorNumElements();
4514 SmallVector<int, 16> MaskVec;
Chris Lattner8a594482007-11-25 00:24:49 +00004515 for (unsigned i = 0; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004516 // If this is the insertion idx, put the low elt of V2 here.
4517 MaskVec.push_back(i == Idx ? NumElems : i);
4518 return DAG.getVectorShuffle(VT, V2.getDebugLoc(), V1, V2, &MaskVec[0]);
Evan Cheng017dcc62006-04-21 01:05:10 +00004519}
4520
Craig Toppera1ffc682012-03-20 06:42:26 +00004521/// getTargetShuffleMask - Calculates the shuffle mask corresponding to the
4522/// target specific opcode. Returns true if the Mask could be calculated.
Craig Topper89f4e662012-03-20 07:17:59 +00004523/// Sets IsUnary to true if only uses one source.
Craig Topperd978c542012-05-06 19:46:21 +00004524static bool getTargetShuffleMask(SDNode *N, MVT VT,
Craig Topper89f4e662012-03-20 07:17:59 +00004525 SmallVectorImpl<int> &Mask, bool &IsUnary) {
Craig Toppera1ffc682012-03-20 06:42:26 +00004526 unsigned NumElems = VT.getVectorNumElements();
4527 SDValue ImmN;
4528
Craig Topper89f4e662012-03-20 07:17:59 +00004529 IsUnary = false;
Craig Toppera1ffc682012-03-20 06:42:26 +00004530 switch(N->getOpcode()) {
4531 case X86ISD::SHUFP:
4532 ImmN = N->getOperand(N->getNumOperands()-1);
4533 DecodeSHUFPMask(VT, cast<ConstantSDNode>(ImmN)->getZExtValue(), Mask);
4534 break;
4535 case X86ISD::UNPCKH:
4536 DecodeUNPCKHMask(VT, Mask);
4537 break;
4538 case X86ISD::UNPCKL:
4539 DecodeUNPCKLMask(VT, Mask);
4540 break;
4541 case X86ISD::MOVHLPS:
4542 DecodeMOVHLPSMask(NumElems, Mask);
4543 break;
4544 case X86ISD::MOVLHPS:
4545 DecodeMOVLHPSMask(NumElems, Mask);
4546 break;
4547 case X86ISD::PSHUFD:
4548 case X86ISD::VPERMILP:
4549 ImmN = N->getOperand(N->getNumOperands()-1);
4550 DecodePSHUFMask(VT, cast<ConstantSDNode>(ImmN)->getZExtValue(), Mask);
Craig Topper89f4e662012-03-20 07:17:59 +00004551 IsUnary = true;
Craig Toppera1ffc682012-03-20 06:42:26 +00004552 break;
4553 case X86ISD::PSHUFHW:
4554 ImmN = N->getOperand(N->getNumOperands()-1);
Craig Toppera9a568a2012-05-02 08:03:44 +00004555 DecodePSHUFHWMask(VT, cast<ConstantSDNode>(ImmN)->getZExtValue(), Mask);
Craig Topper89f4e662012-03-20 07:17:59 +00004556 IsUnary = true;
Craig Toppera1ffc682012-03-20 06:42:26 +00004557 break;
4558 case X86ISD::PSHUFLW:
4559 ImmN = N->getOperand(N->getNumOperands()-1);
Craig Toppera9a568a2012-05-02 08:03:44 +00004560 DecodePSHUFLWMask(VT, cast<ConstantSDNode>(ImmN)->getZExtValue(), Mask);
Craig Topper89f4e662012-03-20 07:17:59 +00004561 IsUnary = true;
Craig Toppera1ffc682012-03-20 06:42:26 +00004562 break;
Craig Topperbdcbcb32012-05-06 18:54:26 +00004563 case X86ISD::VPERMI:
4564 ImmN = N->getOperand(N->getNumOperands()-1);
4565 DecodeVPERMMask(cast<ConstantSDNode>(ImmN)->getZExtValue(), Mask);
4566 IsUnary = true;
4567 break;
Craig Toppera1ffc682012-03-20 06:42:26 +00004568 case X86ISD::MOVSS:
4569 case X86ISD::MOVSD: {
4570 // The index 0 always comes from the first element of the second source,
4571 // this is why MOVSS and MOVSD are used in the first place. The other
4572 // elements come from the other positions of the first source vector
4573 Mask.push_back(NumElems);
4574 for (unsigned i = 1; i != NumElems; ++i) {
4575 Mask.push_back(i);
4576 }
4577 break;
4578 }
4579 case X86ISD::VPERM2X128:
4580 ImmN = N->getOperand(N->getNumOperands()-1);
4581 DecodeVPERM2X128Mask(VT, cast<ConstantSDNode>(ImmN)->getZExtValue(), Mask);
Craig Topper2091df32012-04-17 05:54:54 +00004582 if (Mask.empty()) return false;
Craig Toppera1ffc682012-03-20 06:42:26 +00004583 break;
4584 case X86ISD::MOVDDUP:
4585 case X86ISD::MOVLHPD:
4586 case X86ISD::MOVLPD:
4587 case X86ISD::MOVLPS:
4588 case X86ISD::MOVSHDUP:
4589 case X86ISD::MOVSLDUP:
4590 case X86ISD::PALIGN:
4591 // Not yet implemented
4592 return false;
4593 default: llvm_unreachable("unknown target shuffle node");
4594 }
4595
4596 return true;
4597}
4598
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004599/// getShuffleScalarElt - Returns the scalar element that will make up the ith
4600/// element of the result of the vector shuffle.
Craig Topper3d092db2012-03-21 02:14:01 +00004601static SDValue getShuffleScalarElt(SDNode *N, unsigned Index, SelectionDAG &DAG,
Benjamin Kramer050db522011-03-26 12:38:19 +00004602 unsigned Depth) {
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004603 if (Depth == 6)
4604 return SDValue(); // Limit search depth.
4605
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004606 SDValue V = SDValue(N, 0);
4607 EVT VT = V.getValueType();
4608 unsigned Opcode = V.getOpcode();
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004609
4610 // Recurse into ISD::VECTOR_SHUFFLE node to find scalars.
4611 if (const ShuffleVectorSDNode *SV = dyn_cast<ShuffleVectorSDNode>(N)) {
Craig Topper3d092db2012-03-21 02:14:01 +00004612 int Elt = SV->getMaskElt(Index);
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004613
Craig Topper3d092db2012-03-21 02:14:01 +00004614 if (Elt < 0)
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004615 return DAG.getUNDEF(VT.getVectorElementType());
4616
Craig Topperd156dc12012-02-06 07:17:51 +00004617 unsigned NumElems = VT.getVectorNumElements();
Craig Topper3d092db2012-03-21 02:14:01 +00004618 SDValue NewV = (Elt < (int)NumElems) ? SV->getOperand(0)
4619 : SV->getOperand(1);
4620 return getShuffleScalarElt(NewV.getNode(), Elt % NumElems, DAG, Depth+1);
Evan Chengf26ffe92008-05-29 08:22:04 +00004621 }
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004622
4623 // Recurse into target specific vector shuffles to find scalars.
4624 if (isTargetShuffle(Opcode)) {
Craig Topperd978c542012-05-06 19:46:21 +00004625 MVT ShufVT = V.getValueType().getSimpleVT();
4626 unsigned NumElems = ShufVT.getVectorNumElements();
Craig Toppera1ffc682012-03-20 06:42:26 +00004627 SmallVector<int, 16> ShuffleMask;
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004628 SDValue ImmN;
Craig Topper89f4e662012-03-20 07:17:59 +00004629 bool IsUnary;
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004630
Craig Topperd978c542012-05-06 19:46:21 +00004631 if (!getTargetShuffleMask(N, ShufVT, ShuffleMask, IsUnary))
Craig Toppera1ffc682012-03-20 06:42:26 +00004632 return SDValue();
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004633
Craig Topper3d092db2012-03-21 02:14:01 +00004634 int Elt = ShuffleMask[Index];
4635 if (Elt < 0)
Craig Topperd978c542012-05-06 19:46:21 +00004636 return DAG.getUNDEF(ShufVT.getVectorElementType());
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004637
Craig Topper3d092db2012-03-21 02:14:01 +00004638 SDValue NewV = (Elt < (int)NumElems) ? N->getOperand(0)
Craig Topperd978c542012-05-06 19:46:21 +00004639 : N->getOperand(1);
Craig Topper3d092db2012-03-21 02:14:01 +00004640 return getShuffleScalarElt(NewV.getNode(), Elt % NumElems, DAG,
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004641 Depth+1);
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004642 }
4643
4644 // Actual nodes that may contain scalar elements
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004645 if (Opcode == ISD::BITCAST) {
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004646 V = V.getOperand(0);
4647 EVT SrcVT = V.getValueType();
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00004648 unsigned NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004649
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00004650 if (!SrcVT.isVector() || SrcVT.getVectorNumElements() != NumElems)
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004651 return SDValue();
4652 }
4653
4654 if (V.getOpcode() == ISD::SCALAR_TO_VECTOR)
4655 return (Index == 0) ? V.getOperand(0)
Craig Topper3d092db2012-03-21 02:14:01 +00004656 : DAG.getUNDEF(VT.getVectorElementType());
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004657
4658 if (V.getOpcode() == ISD::BUILD_VECTOR)
4659 return V.getOperand(Index);
4660
4661 return SDValue();
4662}
4663
4664/// getNumOfConsecutiveZeros - Return the number of elements of a vector
4665/// shuffle operation which come from a consecutively from a zero. The
Chris Lattner7a2bdde2011-04-15 05:18:47 +00004666/// search can start in two different directions, from left or right.
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004667static
Craig Topper3d092db2012-03-21 02:14:01 +00004668unsigned getNumOfConsecutiveZeros(ShuffleVectorSDNode *SVOp, unsigned NumElems,
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004669 bool ZerosFromLeft, SelectionDAG &DAG) {
Craig Topper3d092db2012-03-21 02:14:01 +00004670 unsigned i;
4671 for (i = 0; i != NumElems; ++i) {
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004672 unsigned Index = ZerosFromLeft ? i : NumElems-i-1;
Craig Topper3d092db2012-03-21 02:14:01 +00004673 SDValue Elt = getShuffleScalarElt(SVOp, Index, DAG, 0);
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004674 if (!(Elt.getNode() &&
4675 (Elt.getOpcode() == ISD::UNDEF || X86::isZeroNode(Elt))))
4676 break;
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004677 }
4678
4679 return i;
4680}
4681
Craig Topper3d092db2012-03-21 02:14:01 +00004682/// isShuffleMaskConsecutive - Check if the shuffle mask indicies [MaskI, MaskE)
4683/// correspond consecutively to elements from one of the vector operands,
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004684/// starting from its index OpIdx. Also tell OpNum which source vector operand.
4685static
Craig Topper3d092db2012-03-21 02:14:01 +00004686bool isShuffleMaskConsecutive(ShuffleVectorSDNode *SVOp,
4687 unsigned MaskI, unsigned MaskE, unsigned OpIdx,
4688 unsigned NumElems, unsigned &OpNum) {
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004689 bool SeenV1 = false;
4690 bool SeenV2 = false;
4691
Craig Topper3d092db2012-03-21 02:14:01 +00004692 for (unsigned i = MaskI; i != MaskE; ++i, ++OpIdx) {
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004693 int Idx = SVOp->getMaskElt(i);
4694 // Ignore undef indicies
4695 if (Idx < 0)
4696 continue;
4697
Craig Topper3d092db2012-03-21 02:14:01 +00004698 if (Idx < (int)NumElems)
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004699 SeenV1 = true;
4700 else
4701 SeenV2 = true;
4702
4703 // Only accept consecutive elements from the same vector
4704 if ((Idx % NumElems != OpIdx) || (SeenV1 && SeenV2))
4705 return false;
4706 }
4707
4708 OpNum = SeenV1 ? 0 : 1;
4709 return true;
4710}
4711
4712/// isVectorShiftRight - Returns true if the shuffle can be implemented as a
4713/// logical left shift of a vector.
4714static bool isVectorShiftRight(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
4715 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
4716 unsigned NumElems = SVOp->getValueType(0).getVectorNumElements();
4717 unsigned NumZeros = getNumOfConsecutiveZeros(SVOp, NumElems,
4718 false /* check zeros from right */, DAG);
4719 unsigned OpSrc;
4720
4721 if (!NumZeros)
4722 return false;
4723
4724 // Considering the elements in the mask that are not consecutive zeros,
4725 // check if they consecutively come from only one of the source vectors.
4726 //
4727 // V1 = {X, A, B, C} 0
4728 // \ \ \ /
4729 // vector_shuffle V1, V2 <1, 2, 3, X>
4730 //
4731 if (!isShuffleMaskConsecutive(SVOp,
4732 0, // Mask Start Index
Craig Topper3d092db2012-03-21 02:14:01 +00004733 NumElems-NumZeros, // Mask End Index(exclusive)
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004734 NumZeros, // Where to start looking in the src vector
4735 NumElems, // Number of elements in vector
4736 OpSrc)) // Which source operand ?
4737 return false;
4738
4739 isLeft = false;
4740 ShAmt = NumZeros;
4741 ShVal = SVOp->getOperand(OpSrc);
4742 return true;
4743}
4744
4745/// isVectorShiftLeft - Returns true if the shuffle can be implemented as a
4746/// logical left shift of a vector.
4747static bool isVectorShiftLeft(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
4748 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
4749 unsigned NumElems = SVOp->getValueType(0).getVectorNumElements();
4750 unsigned NumZeros = getNumOfConsecutiveZeros(SVOp, NumElems,
4751 true /* check zeros from left */, DAG);
4752 unsigned OpSrc;
4753
4754 if (!NumZeros)
4755 return false;
4756
4757 // Considering the elements in the mask that are not consecutive zeros,
4758 // check if they consecutively come from only one of the source vectors.
4759 //
4760 // 0 { A, B, X, X } = V2
4761 // / \ / /
4762 // vector_shuffle V1, V2 <X, X, 4, 5>
4763 //
4764 if (!isShuffleMaskConsecutive(SVOp,
4765 NumZeros, // Mask Start Index
Craig Topper3d092db2012-03-21 02:14:01 +00004766 NumElems, // Mask End Index(exclusive)
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004767 0, // Where to start looking in the src vector
4768 NumElems, // Number of elements in vector
4769 OpSrc)) // Which source operand ?
4770 return false;
4771
4772 isLeft = true;
4773 ShAmt = NumZeros;
4774 ShVal = SVOp->getOperand(OpSrc);
4775 return true;
Evan Chengf26ffe92008-05-29 08:22:04 +00004776}
4777
4778/// isVectorShift - Returns true if the shuffle can be implemented as a
4779/// logical left or right shift of a vector.
Nate Begeman9008ca62009-04-27 18:41:29 +00004780static bool isVectorShift(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
Dan Gohman475871a2008-07-27 21:46:04 +00004781 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00004782 // Although the logic below support any bitwidth size, there are no
4783 // shift instructions which handle more than 128-bit vectors.
Craig Topper7a9a28b2012-08-12 02:23:29 +00004784 if (!SVOp->getValueType(0).is128BitVector())
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00004785 return false;
4786
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004787 if (isVectorShiftLeft(SVOp, DAG, isLeft, ShVal, ShAmt) ||
4788 isVectorShiftRight(SVOp, DAG, isLeft, ShVal, ShAmt))
4789 return true;
Evan Chengf26ffe92008-05-29 08:22:04 +00004790
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004791 return false;
Evan Chengf26ffe92008-05-29 08:22:04 +00004792}
4793
Evan Chengc78d3b42006-04-24 18:01:45 +00004794/// LowerBuildVectorv16i8 - Custom lower build_vector of v16i8.
4795///
Dan Gohman475871a2008-07-27 21:46:04 +00004796static SDValue LowerBuildVectorv16i8(SDValue Op, unsigned NonZeros,
Evan Chengc78d3b42006-04-24 18:01:45 +00004797 unsigned NumNonZero, unsigned NumZero,
Dan Gohmand858e902010-04-17 15:26:15 +00004798 SelectionDAG &DAG,
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00004799 const X86Subtarget* Subtarget,
Dan Gohmand858e902010-04-17 15:26:15 +00004800 const TargetLowering &TLI) {
Evan Chengc78d3b42006-04-24 18:01:45 +00004801 if (NumNonZero > 8)
Dan Gohman475871a2008-07-27 21:46:04 +00004802 return SDValue();
Evan Chengc78d3b42006-04-24 18:01:45 +00004803
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004804 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00004805 SDValue V(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00004806 bool First = true;
4807 for (unsigned i = 0; i < 16; ++i) {
4808 bool ThisIsNonZero = (NonZeros & (1 << i)) != 0;
4809 if (ThisIsNonZero && First) {
4810 if (NumZero)
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00004811 V = getZeroVector(MVT::v8i16, Subtarget, DAG, dl);
Evan Chengc78d3b42006-04-24 18:01:45 +00004812 else
Owen Anderson825b72b2009-08-11 20:47:22 +00004813 V = DAG.getUNDEF(MVT::v8i16);
Evan Chengc78d3b42006-04-24 18:01:45 +00004814 First = false;
4815 }
4816
4817 if ((i & 1) != 0) {
Dan Gohman475871a2008-07-27 21:46:04 +00004818 SDValue ThisElt(0, 0), LastElt(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00004819 bool LastIsNonZero = (NonZeros & (1 << (i-1))) != 0;
4820 if (LastIsNonZero) {
Scott Michelfdc40a02009-02-17 22:15:04 +00004821 LastElt = DAG.getNode(ISD::ZERO_EXTEND, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004822 MVT::i16, Op.getOperand(i-1));
Evan Chengc78d3b42006-04-24 18:01:45 +00004823 }
4824 if (ThisIsNonZero) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004825 ThisElt = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i16, Op.getOperand(i));
4826 ThisElt = DAG.getNode(ISD::SHL, dl, MVT::i16,
4827 ThisElt, DAG.getConstant(8, MVT::i8));
Evan Chengc78d3b42006-04-24 18:01:45 +00004828 if (LastIsNonZero)
Owen Anderson825b72b2009-08-11 20:47:22 +00004829 ThisElt = DAG.getNode(ISD::OR, dl, MVT::i16, ThisElt, LastElt);
Evan Chengc78d3b42006-04-24 18:01:45 +00004830 } else
4831 ThisElt = LastElt;
4832
Gabor Greifba36cb52008-08-28 21:40:38 +00004833 if (ThisElt.getNode())
Owen Anderson825b72b2009-08-11 20:47:22 +00004834 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, V, ThisElt,
Chris Lattner0bd48932008-01-17 07:00:52 +00004835 DAG.getIntPtrConstant(i/2));
Evan Chengc78d3b42006-04-24 18:01:45 +00004836 }
4837 }
4838
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004839 return DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, V);
Evan Chengc78d3b42006-04-24 18:01:45 +00004840}
4841
Bill Wendlinga348c562007-03-22 18:42:45 +00004842/// LowerBuildVectorv8i16 - Custom lower build_vector of v8i16.
Evan Chengc78d3b42006-04-24 18:01:45 +00004843///
Dan Gohman475871a2008-07-27 21:46:04 +00004844static SDValue LowerBuildVectorv8i16(SDValue Op, unsigned NonZeros,
Dan Gohmand858e902010-04-17 15:26:15 +00004845 unsigned NumNonZero, unsigned NumZero,
4846 SelectionDAG &DAG,
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00004847 const X86Subtarget* Subtarget,
Dan Gohmand858e902010-04-17 15:26:15 +00004848 const TargetLowering &TLI) {
Evan Chengc78d3b42006-04-24 18:01:45 +00004849 if (NumNonZero > 4)
Dan Gohman475871a2008-07-27 21:46:04 +00004850 return SDValue();
Evan Chengc78d3b42006-04-24 18:01:45 +00004851
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004852 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00004853 SDValue V(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00004854 bool First = true;
4855 for (unsigned i = 0; i < 8; ++i) {
4856 bool isNonZero = (NonZeros & (1 << i)) != 0;
4857 if (isNonZero) {
4858 if (First) {
4859 if (NumZero)
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00004860 V = getZeroVector(MVT::v8i16, Subtarget, DAG, dl);
Evan Chengc78d3b42006-04-24 18:01:45 +00004861 else
Owen Anderson825b72b2009-08-11 20:47:22 +00004862 V = DAG.getUNDEF(MVT::v8i16);
Evan Chengc78d3b42006-04-24 18:01:45 +00004863 First = false;
4864 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004865 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004866 MVT::v8i16, V, Op.getOperand(i),
Chris Lattner0bd48932008-01-17 07:00:52 +00004867 DAG.getIntPtrConstant(i));
Evan Chengc78d3b42006-04-24 18:01:45 +00004868 }
4869 }
4870
4871 return V;
4872}
4873
Evan Chengf26ffe92008-05-29 08:22:04 +00004874/// getVShift - Return a vector logical shift node.
4875///
Owen Andersone50ed302009-08-10 22:56:29 +00004876static SDValue getVShift(bool isLeft, EVT VT, SDValue SrcOp,
Nate Begeman9008ca62009-04-27 18:41:29 +00004877 unsigned NumBits, SelectionDAG &DAG,
4878 const TargetLowering &TLI, DebugLoc dl) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00004879 assert(VT.is128BitVector() && "Unknown type for VShift");
Dale Johannesen0488fb62010-09-30 23:57:10 +00004880 EVT ShVT = MVT::v2i64;
Craig Toppered2e13d2012-01-22 19:15:14 +00004881 unsigned Opc = isLeft ? X86ISD::VSHLDQ : X86ISD::VSRLDQ;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004882 SrcOp = DAG.getNode(ISD::BITCAST, dl, ShVT, SrcOp);
4883 return DAG.getNode(ISD::BITCAST, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00004884 DAG.getNode(Opc, dl, ShVT, SrcOp,
Owen Anderson95771af2011-02-25 21:41:48 +00004885 DAG.getConstant(NumBits,
4886 TLI.getShiftAmountTy(SrcOp.getValueType()))));
Evan Chengf26ffe92008-05-29 08:22:04 +00004887}
4888
Dan Gohman475871a2008-07-27 21:46:04 +00004889SDValue
Evan Chengc3630942009-12-09 21:00:30 +00004890X86TargetLowering::LowerAsSplatVectorLoad(SDValue SrcOp, EVT VT, DebugLoc dl,
Dan Gohmand858e902010-04-17 15:26:15 +00004891 SelectionDAG &DAG) const {
Michael J. Spencerec38de22010-10-10 22:04:20 +00004892
Evan Chengc3630942009-12-09 21:00:30 +00004893 // Check if the scalar load can be widened into a vector load. And if
4894 // the address is "base + cst" see if the cst can be "absorbed" into
4895 // the shuffle mask.
4896 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(SrcOp)) {
4897 SDValue Ptr = LD->getBasePtr();
4898 if (!ISD::isNormalLoad(LD) || LD->isVolatile())
4899 return SDValue();
4900 EVT PVT = LD->getValueType(0);
4901 if (PVT != MVT::i32 && PVT != MVT::f32)
4902 return SDValue();
4903
4904 int FI = -1;
4905 int64_t Offset = 0;
4906 if (FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr)) {
4907 FI = FINode->getIndex();
4908 Offset = 0;
Chris Lattner0a9481f2011-02-13 22:25:43 +00004909 } else if (DAG.isBaseWithConstantOffset(Ptr) &&
Evan Chengc3630942009-12-09 21:00:30 +00004910 isa<FrameIndexSDNode>(Ptr.getOperand(0))) {
4911 FI = cast<FrameIndexSDNode>(Ptr.getOperand(0))->getIndex();
4912 Offset = Ptr.getConstantOperandVal(1);
4913 Ptr = Ptr.getOperand(0);
4914 } else {
4915 return SDValue();
4916 }
4917
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004918 // FIXME: 256-bit vector instructions don't require a strict alignment,
4919 // improve this code to support it better.
4920 unsigned RequiredAlign = VT.getSizeInBits()/8;
Evan Chengc3630942009-12-09 21:00:30 +00004921 SDValue Chain = LD->getChain();
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004922 // Make sure the stack object alignment is at least 16 or 32.
Evan Chengc3630942009-12-09 21:00:30 +00004923 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004924 if (DAG.InferPtrAlignment(Ptr) < RequiredAlign) {
Evan Chengc3630942009-12-09 21:00:30 +00004925 if (MFI->isFixedObjectIndex(FI)) {
Eric Christophere9625cf2010-01-23 06:02:43 +00004926 // Can't change the alignment. FIXME: It's possible to compute
4927 // the exact stack offset and reference FI + adjust offset instead.
4928 // If someone *really* cares about this. That's the way to implement it.
4929 return SDValue();
Evan Chengc3630942009-12-09 21:00:30 +00004930 } else {
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004931 MFI->setObjectAlignment(FI, RequiredAlign);
Evan Chengc3630942009-12-09 21:00:30 +00004932 }
4933 }
4934
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004935 // (Offset % 16 or 32) must be multiple of 4. Then address is then
Evan Chengc3630942009-12-09 21:00:30 +00004936 // Ptr + (Offset & ~15).
4937 if (Offset < 0)
4938 return SDValue();
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004939 if ((Offset % RequiredAlign) & 3)
Evan Chengc3630942009-12-09 21:00:30 +00004940 return SDValue();
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004941 int64_t StartOffset = Offset & ~(RequiredAlign-1);
Evan Chengc3630942009-12-09 21:00:30 +00004942 if (StartOffset)
4943 Ptr = DAG.getNode(ISD::ADD, Ptr.getDebugLoc(), Ptr.getValueType(),
4944 Ptr,DAG.getConstant(StartOffset, Ptr.getValueType()));
4945
4946 int EltNo = (Offset - StartOffset) >> 2;
Craig Topper66ddd152012-04-27 22:54:43 +00004947 unsigned NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004948
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004949 EVT NVT = EVT::getVectorVT(*DAG.getContext(), PVT, NumElems);
4950 SDValue V1 = DAG.getLoad(NVT, dl, Chain, Ptr,
Chris Lattner51abfe42010-09-21 06:02:19 +00004951 LD->getPointerInfo().getWithOffset(StartOffset),
Pete Cooperd752e0f2011-11-08 18:42:53 +00004952 false, false, false, 0);
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004953
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004954 SmallVector<int, 8> Mask;
Craig Topper66ddd152012-04-27 22:54:43 +00004955 for (unsigned i = 0; i != NumElems; ++i)
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004956 Mask.push_back(EltNo);
4957
Craig Toppercc3000632012-01-30 07:50:31 +00004958 return DAG.getVectorShuffle(NVT, dl, V1, DAG.getUNDEF(NVT), &Mask[0]);
Evan Chengc3630942009-12-09 21:00:30 +00004959 }
4960
4961 return SDValue();
4962}
4963
Michael J. Spencerec38de22010-10-10 22:04:20 +00004964/// EltsFromConsecutiveLoads - Given the initializing elements 'Elts' of a
4965/// vector of type 'VT', see if the elements can be replaced by a single large
Nate Begeman1449f292010-03-24 22:19:06 +00004966/// load which has the same value as a build_vector whose operands are 'elts'.
4967///
4968/// Example: <load i32 *a, load i32 *a+4, undef, undef> -> zextload a
Michael J. Spencerec38de22010-10-10 22:04:20 +00004969///
Nate Begeman1449f292010-03-24 22:19:06 +00004970/// FIXME: we'd also like to handle the case where the last elements are zero
4971/// rather than undef via VZEXT_LOAD, but we do not detect that case today.
4972/// There's even a handy isZeroNode for that purpose.
Nate Begemanfdea31a2010-03-24 20:49:50 +00004973static SDValue EltsFromConsecutiveLoads(EVT VT, SmallVectorImpl<SDValue> &Elts,
Chris Lattner88641552010-09-22 00:34:38 +00004974 DebugLoc &DL, SelectionDAG &DAG) {
Nate Begemanfdea31a2010-03-24 20:49:50 +00004975 EVT EltVT = VT.getVectorElementType();
4976 unsigned NumElems = Elts.size();
Michael J. Spencerec38de22010-10-10 22:04:20 +00004977
Nate Begemanfdea31a2010-03-24 20:49:50 +00004978 LoadSDNode *LDBase = NULL;
4979 unsigned LastLoadedElt = -1U;
Michael J. Spencerec38de22010-10-10 22:04:20 +00004980
Nate Begeman1449f292010-03-24 22:19:06 +00004981 // For each element in the initializer, see if we've found a load or an undef.
Michael J. Spencerec38de22010-10-10 22:04:20 +00004982 // If we don't find an initial load element, or later load elements are
Nate Begeman1449f292010-03-24 22:19:06 +00004983 // non-consecutive, bail out.
Nate Begemanfdea31a2010-03-24 20:49:50 +00004984 for (unsigned i = 0; i < NumElems; ++i) {
4985 SDValue Elt = Elts[i];
Michael J. Spencerec38de22010-10-10 22:04:20 +00004986
Nate Begemanfdea31a2010-03-24 20:49:50 +00004987 if (!Elt.getNode() ||
4988 (Elt.getOpcode() != ISD::UNDEF && !ISD::isNON_EXTLoad(Elt.getNode())))
4989 return SDValue();
4990 if (!LDBase) {
4991 if (Elt.getNode()->getOpcode() == ISD::UNDEF)
4992 return SDValue();
4993 LDBase = cast<LoadSDNode>(Elt.getNode());
4994 LastLoadedElt = i;
4995 continue;
4996 }
4997 if (Elt.getOpcode() == ISD::UNDEF)
4998 continue;
4999
5000 LoadSDNode *LD = cast<LoadSDNode>(Elt);
5001 if (!DAG.isConsecutiveLoad(LD, LDBase, EltVT.getSizeInBits()/8, i))
5002 return SDValue();
5003 LastLoadedElt = i;
5004 }
Nate Begeman1449f292010-03-24 22:19:06 +00005005
5006 // If we have found an entire vector of loads and undefs, then return a large
5007 // load of the entire vector width starting at the base pointer. If we found
5008 // consecutive loads for the low half, generate a vzext_load node.
Nate Begemanfdea31a2010-03-24 20:49:50 +00005009 if (LastLoadedElt == NumElems - 1) {
5010 if (DAG.InferPtrAlignment(LDBase->getBasePtr()) >= 16)
Chris Lattner88641552010-09-22 00:34:38 +00005011 return DAG.getLoad(VT, DL, LDBase->getChain(), LDBase->getBasePtr(),
Chris Lattner51abfe42010-09-21 06:02:19 +00005012 LDBase->getPointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00005013 LDBase->isVolatile(), LDBase->isNonTemporal(),
5014 LDBase->isInvariant(), 0);
Chris Lattner88641552010-09-22 00:34:38 +00005015 return DAG.getLoad(VT, DL, LDBase->getChain(), LDBase->getBasePtr(),
Chris Lattner51abfe42010-09-21 06:02:19 +00005016 LDBase->getPointerInfo(),
Nate Begemanfdea31a2010-03-24 20:49:50 +00005017 LDBase->isVolatile(), LDBase->isNonTemporal(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00005018 LDBase->isInvariant(), LDBase->getAlignment());
Craig Topper69947b92012-04-23 06:57:04 +00005019 }
5020 if (NumElems == 4 && LastLoadedElt == 1 &&
5021 DAG.getTargetLoweringInfo().isTypeLegal(MVT::v2i64)) {
Nate Begemanfdea31a2010-03-24 20:49:50 +00005022 SDVTList Tys = DAG.getVTList(MVT::v2i64, MVT::Other);
5023 SDValue Ops[] = { LDBase->getChain(), LDBase->getBasePtr() };
Eli Friedman322ea082011-09-14 23:42:45 +00005024 SDValue ResNode =
5025 DAG.getMemIntrinsicNode(X86ISD::VZEXT_LOAD, DL, Tys, Ops, 2, MVT::i64,
5026 LDBase->getPointerInfo(),
5027 LDBase->getAlignment(),
5028 false/*isVolatile*/, true/*ReadMem*/,
5029 false/*WriteMem*/);
Manman Ren2b7a2e82012-08-31 23:16:57 +00005030
5031 // Make sure the newly-created LOAD is in the same position as LDBase in
5032 // terms of dependency. We create a TokenFactor for LDBase and ResNode, and
5033 // update uses of LDBase's output chain to use the TokenFactor.
5034 if (LDBase->hasAnyUseOfValue(1)) {
5035 SDValue NewChain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other,
5036 SDValue(LDBase, 1), SDValue(ResNode.getNode(), 1));
5037 DAG.ReplaceAllUsesOfValueWith(SDValue(LDBase, 1), NewChain);
5038 DAG.UpdateNodeOperands(NewChain.getNode(), SDValue(LDBase, 1),
5039 SDValue(ResNode.getNode(), 1));
5040 }
5041
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005042 return DAG.getNode(ISD::BITCAST, DL, VT, ResNode);
Nate Begemanfdea31a2010-03-24 20:49:50 +00005043 }
5044 return SDValue();
5045}
5046
Nadav Rotem9d68b062012-04-08 12:54:54 +00005047/// LowerVectorBroadcast - Attempt to use the vbroadcast instruction
5048/// to generate a splat value for the following cases:
5049/// 1. A splat BUILD_VECTOR which uses a single scalar load, or a constant.
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005050/// 2. A splat shuffle which uses a scalar_to_vector node which comes from
Nadav Rotem9d68b062012-04-08 12:54:54 +00005051/// a scalar load, or a constant.
5052/// The VBROADCAST node is returned when a pattern is found,
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00005053/// or SDValue() otherwise.
Nadav Rotem154819d2012-04-09 07:45:58 +00005054SDValue
Craig Topper55b24052012-09-11 06:15:32 +00005055X86TargetLowering::LowerVectorBroadcast(SDValue Op, SelectionDAG &DAG) const {
Craig Toppera9376332012-01-10 08:23:59 +00005056 if (!Subtarget->hasAVX())
5057 return SDValue();
5058
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005059 EVT VT = Op.getValueType();
Nadav Rotem154819d2012-04-09 07:45:58 +00005060 DebugLoc dl = Op.getDebugLoc();
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005061
Craig Topper5da8a802012-05-04 05:49:51 +00005062 assert((VT.is128BitVector() || VT.is256BitVector()) &&
5063 "Unsupported vector type for broadcast.");
5064
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005065 SDValue Ld;
Nadav Rotem9d68b062012-04-08 12:54:54 +00005066 bool ConstSplatVal;
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005067
Nadav Rotem9d68b062012-04-08 12:54:54 +00005068 switch (Op.getOpcode()) {
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005069 default:
5070 // Unknown pattern found.
5071 return SDValue();
5072
5073 case ISD::BUILD_VECTOR: {
5074 // The BUILD_VECTOR node must be a splat.
Nadav Rotem9d68b062012-04-08 12:54:54 +00005075 if (!isSplatVector(Op.getNode()))
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005076 return SDValue();
5077
Nadav Rotem9d68b062012-04-08 12:54:54 +00005078 Ld = Op.getOperand(0);
5079 ConstSplatVal = (Ld.getOpcode() == ISD::Constant ||
5080 Ld.getOpcode() == ISD::ConstantFP);
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00005081
5082 // The suspected load node has several users. Make sure that all
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005083 // of its users are from the BUILD_VECTOR node.
Nadav Rotem9d68b062012-04-08 12:54:54 +00005084 // Constants may have multiple users.
5085 if (!ConstSplatVal && !Ld->hasNUsesOfValue(VT.getVectorNumElements(), 0))
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005086 return SDValue();
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00005087 break;
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005088 }
5089
5090 case ISD::VECTOR_SHUFFLE: {
5091 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
5092
5093 // Shuffles must have a splat mask where the first element is
5094 // broadcasted.
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00005095 if ((!SVOp->isSplat()) || SVOp->getMaskElt(0) != 0)
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005096 return SDValue();
5097
5098 SDValue Sc = Op.getOperand(0);
Nadav Rotemb88e8dd2012-05-10 12:50:02 +00005099 if (Sc.getOpcode() != ISD::SCALAR_TO_VECTOR &&
Elena Demikhovsky8f40f7b2012-07-01 06:12:26 +00005100 Sc.getOpcode() != ISD::BUILD_VECTOR) {
5101
5102 if (!Subtarget->hasAVX2())
5103 return SDValue();
5104
5105 // Use the register form of the broadcast instruction available on AVX2.
5106 if (VT.is256BitVector())
5107 Sc = Extract128BitVector(Sc, 0, DAG, dl);
5108 return DAG.getNode(X86ISD::VBROADCAST, dl, VT, Sc);
5109 }
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005110
5111 Ld = Sc.getOperand(0);
Nadav Rotem9d68b062012-04-08 12:54:54 +00005112 ConstSplatVal = (Ld.getOpcode() == ISD::Constant ||
Nadav Rotem154819d2012-04-09 07:45:58 +00005113 Ld.getOpcode() == ISD::ConstantFP);
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005114
5115 // The scalar_to_vector node and the suspected
5116 // load node must have exactly one user.
Nadav Rotem9d68b062012-04-08 12:54:54 +00005117 // Constants may have multiple users.
5118 if (!ConstSplatVal && (!Sc.hasOneUse() || !Ld.hasOneUse()))
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005119 return SDValue();
5120 break;
5121 }
5122 }
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00005123
Craig Topper7a9a28b2012-08-12 02:23:29 +00005124 bool Is256 = VT.is256BitVector();
Nadav Rotem9d68b062012-04-08 12:54:54 +00005125
5126 // Handle the broadcasting a single constant scalar from the constant pool
5127 // into a vector. On Sandybridge it is still better to load a constant vector
5128 // from the constant pool and not to broadcast it from a scalar.
5129 if (ConstSplatVal && Subtarget->hasAVX2()) {
5130 EVT CVT = Ld.getValueType();
5131 assert(!CVT.isVector() && "Must not broadcast a vector type");
5132 unsigned ScalarSize = CVT.getSizeInBits();
5133
Craig Topper5da8a802012-05-04 05:49:51 +00005134 if (ScalarSize == 32 || (Is256 && ScalarSize == 64)) {
Nadav Rotem9d68b062012-04-08 12:54:54 +00005135 const Constant *C = 0;
5136 if (ConstantSDNode *CI = dyn_cast<ConstantSDNode>(Ld))
5137 C = CI->getConstantIntValue();
5138 else if (ConstantFPSDNode *CF = dyn_cast<ConstantFPSDNode>(Ld))
5139 C = CF->getConstantFPValue();
5140
5141 assert(C && "Invalid constant type");
5142
Nadav Rotem154819d2012-04-09 07:45:58 +00005143 SDValue CP = DAG.getConstantPool(C, getPointerTy());
Nadav Rotem9d68b062012-04-08 12:54:54 +00005144 unsigned Alignment = cast<ConstantPoolSDNode>(CP)->getAlignment();
Nadav Rotem154819d2012-04-09 07:45:58 +00005145 Ld = DAG.getLoad(CVT, dl, DAG.getEntryNode(), CP,
Craig Topper6643d9c2012-05-04 06:18:33 +00005146 MachinePointerInfo::getConstantPool(),
5147 false, false, false, Alignment);
Nadav Rotem9d68b062012-04-08 12:54:54 +00005148
Nadav Rotem9d68b062012-04-08 12:54:54 +00005149 return DAG.getNode(X86ISD::VBROADCAST, dl, VT, Ld);
5150 }
5151 }
5152
Nadav Rotem4fc8a5d2012-05-19 19:57:37 +00005153 bool IsLoad = ISD::isNormalLoad(Ld.getNode());
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005154 unsigned ScalarSize = Ld.getValueType().getSizeInBits();
5155
Nadav Rotem4fc8a5d2012-05-19 19:57:37 +00005156 // Handle AVX2 in-register broadcasts.
5157 if (!IsLoad && Subtarget->hasAVX2() &&
5158 (ScalarSize == 32 || (Is256 && ScalarSize == 64)))
5159 return DAG.getNode(X86ISD::VBROADCAST, dl, VT, Ld);
5160
5161 // The scalar source must be a normal load.
5162 if (!IsLoad)
5163 return SDValue();
5164
Craig Topper5da8a802012-05-04 05:49:51 +00005165 if (ScalarSize == 32 || (Is256 && ScalarSize == 64))
Nadav Rotem9d68b062012-04-08 12:54:54 +00005166 return DAG.getNode(X86ISD::VBROADCAST, dl, VT, Ld);
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005167
Craig Toppera9376332012-01-10 08:23:59 +00005168 // The integer check is needed for the 64-bit into 128-bit so it doesn't match
Craig Topper5da8a802012-05-04 05:49:51 +00005169 // double since there is no vbroadcastsd xmm
Craig Toppera9376332012-01-10 08:23:59 +00005170 if (Subtarget->hasAVX2() && Ld.getValueType().isInteger()) {
Craig Topper5da8a802012-05-04 05:49:51 +00005171 if (ScalarSize == 8 || ScalarSize == 16 || ScalarSize == 64)
Nadav Rotem9d68b062012-04-08 12:54:54 +00005172 return DAG.getNode(X86ISD::VBROADCAST, dl, VT, Ld);
Craig Toppera9376332012-01-10 08:23:59 +00005173 }
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00005174
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005175 // Unsupported broadcast.
5176 return SDValue();
5177}
5178
Evan Chengc3630942009-12-09 21:00:30 +00005179SDValue
Michael Liaofacace82012-10-19 17:15:18 +00005180X86TargetLowering::buildFromShuffleMostly(SDValue Op, SelectionDAG &DAG) const {
5181 EVT VT = Op.getValueType();
5182
5183 // Skip if insert_vec_elt is not supported.
5184 if (!isOperationLegalOrCustom(ISD::INSERT_VECTOR_ELT, VT))
5185 return SDValue();
5186
5187 DebugLoc DL = Op.getDebugLoc();
5188 unsigned NumElems = Op.getNumOperands();
5189
5190 SDValue VecIn1;
5191 SDValue VecIn2;
5192 SmallVector<unsigned, 4> InsertIndices;
5193 SmallVector<int, 8> Mask(NumElems, -1);
5194
5195 for (unsigned i = 0; i != NumElems; ++i) {
5196 unsigned Opc = Op.getOperand(i).getOpcode();
5197
5198 if (Opc == ISD::UNDEF)
5199 continue;
5200
5201 if (Opc != ISD::EXTRACT_VECTOR_ELT) {
5202 // Quit if more than 1 elements need inserting.
5203 if (InsertIndices.size() > 1)
5204 return SDValue();
5205
5206 InsertIndices.push_back(i);
5207 continue;
5208 }
5209
5210 SDValue ExtractedFromVec = Op.getOperand(i).getOperand(0);
5211 SDValue ExtIdx = Op.getOperand(i).getOperand(1);
5212
5213 // Quit if extracted from vector of different type.
5214 if (ExtractedFromVec.getValueType() != VT)
5215 return SDValue();
5216
5217 // Quit if non-constant index.
5218 if (!isa<ConstantSDNode>(ExtIdx))
5219 return SDValue();
5220
5221 if (VecIn1.getNode() == 0)
5222 VecIn1 = ExtractedFromVec;
5223 else if (VecIn1 != ExtractedFromVec) {
5224 if (VecIn2.getNode() == 0)
5225 VecIn2 = ExtractedFromVec;
5226 else if (VecIn2 != ExtractedFromVec)
5227 // Quit if more than 2 vectors to shuffle
5228 return SDValue();
5229 }
5230
5231 unsigned Idx = cast<ConstantSDNode>(ExtIdx)->getZExtValue();
5232
5233 if (ExtractedFromVec == VecIn1)
5234 Mask[i] = Idx;
5235 else if (ExtractedFromVec == VecIn2)
5236 Mask[i] = Idx + NumElems;
5237 }
5238
5239 if (VecIn1.getNode() == 0)
5240 return SDValue();
5241
5242 VecIn2 = VecIn2.getNode() ? VecIn2 : DAG.getUNDEF(VT);
5243 SDValue NV = DAG.getVectorShuffle(VT, DL, VecIn1, VecIn2, &Mask[0]);
5244 for (unsigned i = 0, e = InsertIndices.size(); i != e; ++i) {
5245 unsigned Idx = InsertIndices[i];
5246 NV = DAG.getNode(ISD::INSERT_VECTOR_ELT, DL, VT, NV, Op.getOperand(Idx),
5247 DAG.getIntPtrConstant(Idx));
5248 }
5249
5250 return NV;
5251}
5252
5253SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00005254X86TargetLowering::LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005255 DebugLoc dl = Op.getDebugLoc();
David Greenea5f26012011-02-07 19:36:54 +00005256
David Greenef125a292011-02-08 19:04:41 +00005257 EVT VT = Op.getValueType();
5258 EVT ExtVT = VT.getVectorElementType();
David Greenef125a292011-02-08 19:04:41 +00005259 unsigned NumElems = Op.getNumOperands();
5260
Bruno Cardoso Lopes531f19f2011-08-01 19:51:53 +00005261 // Vectors containing all zeros can be matched by pxor and xorps later
5262 if (ISD::isBuildVectorAllZeros(Op.getNode())) {
5263 // Canonicalize this to <4 x i32> to 1) ensure the zero vectors are CSE'd
5264 // and 2) ensure that i64 scalars are eliminated on x86-32 hosts.
Craig Topper07a27622012-01-22 03:07:48 +00005265 if (VT == MVT::v4i32 || VT == MVT::v8i32)
Chris Lattner8a594482007-11-25 00:24:49 +00005266 return Op;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005267
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00005268 return getZeroVector(VT, Subtarget, DAG, dl);
Chris Lattner8a594482007-11-25 00:24:49 +00005269 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00005270
Bruno Cardoso Lopes531f19f2011-08-01 19:51:53 +00005271 // Vectors containing all ones can be matched by pcmpeqd on 128-bit width
Craig Topper745a86b2011-11-19 22:34:59 +00005272 // vectors or broken into v4i32 operations on 256-bit vectors. AVX2 can use
5273 // vpcmpeqd on 256-bit vectors.
Bruno Cardoso Lopes531f19f2011-08-01 19:51:53 +00005274 if (ISD::isBuildVectorAllOnes(Op.getNode())) {
Craig Topper07a27622012-01-22 03:07:48 +00005275 if (VT == MVT::v4i32 || (VT == MVT::v8i32 && Subtarget->hasAVX2()))
Bruno Cardoso Lopes531f19f2011-08-01 19:51:53 +00005276 return Op;
5277
Craig Topper07a27622012-01-22 03:07:48 +00005278 return getOnesVector(VT, Subtarget->hasAVX2(), DAG, dl);
Bruno Cardoso Lopes531f19f2011-08-01 19:51:53 +00005279 }
5280
Nadav Rotem154819d2012-04-09 07:45:58 +00005281 SDValue Broadcast = LowerVectorBroadcast(Op, DAG);
Nadav Rotem9d68b062012-04-08 12:54:54 +00005282 if (Broadcast.getNode())
5283 return Broadcast;
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005284
Owen Andersone50ed302009-08-10 22:56:29 +00005285 unsigned EVTBits = ExtVT.getSizeInBits();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005286
Evan Cheng0db9fe62006-04-25 20:13:52 +00005287 unsigned NumZero = 0;
5288 unsigned NumNonZero = 0;
5289 unsigned NonZeros = 0;
Chris Lattnerc9517fb2008-03-08 22:48:29 +00005290 bool IsAllConstants = true;
Dan Gohman475871a2008-07-27 21:46:04 +00005291 SmallSet<SDValue, 8> Values;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005292 for (unsigned i = 0; i < NumElems; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00005293 SDValue Elt = Op.getOperand(i);
Evan Chengdb2d5242007-12-12 06:45:40 +00005294 if (Elt.getOpcode() == ISD::UNDEF)
5295 continue;
5296 Values.insert(Elt);
5297 if (Elt.getOpcode() != ISD::Constant &&
5298 Elt.getOpcode() != ISD::ConstantFP)
Chris Lattnerc9517fb2008-03-08 22:48:29 +00005299 IsAllConstants = false;
Evan Cheng37b73872009-07-30 08:33:02 +00005300 if (X86::isZeroNode(Elt))
Evan Chengdb2d5242007-12-12 06:45:40 +00005301 NumZero++;
5302 else {
5303 NonZeros |= (1 << i);
5304 NumNonZero++;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005305 }
5306 }
5307
Chris Lattner97a2a562010-08-26 05:24:29 +00005308 // All undef vector. Return an UNDEF. All zero vectors were handled above.
5309 if (NumNonZero == 0)
Dale Johannesene8d72302009-02-06 23:05:02 +00005310 return DAG.getUNDEF(VT);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005311
Chris Lattner67f453a2008-03-09 05:42:06 +00005312 // Special case for single non-zero, non-undef, element.
Eli Friedman10415532009-06-06 06:05:10 +00005313 if (NumNonZero == 1) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00005314 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dan Gohman475871a2008-07-27 21:46:04 +00005315 SDValue Item = Op.getOperand(Idx);
Scott Michelfdc40a02009-02-17 22:15:04 +00005316
Chris Lattner62098042008-03-09 01:05:04 +00005317 // If this is an insertion of an i64 value on x86-32, and if the top bits of
5318 // the value are obviously zero, truncate the value to i32 and do the
5319 // insertion that way. Only do this if the value is non-constant or if the
5320 // value is a constant being inserted into element 0. It is cheaper to do
5321 // a constant pool load than it is to do a movd + shuffle.
Owen Anderson825b72b2009-08-11 20:47:22 +00005322 if (ExtVT == MVT::i64 && !Subtarget->is64Bit() &&
Chris Lattner62098042008-03-09 01:05:04 +00005323 (!IsAllConstants || Idx == 0)) {
5324 if (DAG.MaskedValueIsZero(Item, APInt::getBitsSet(64, 32, 64))) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00005325 // Handle SSE only.
5326 assert(VT == MVT::v2i64 && "Expected an SSE value type!");
5327 EVT VecVT = MVT::v4i32;
5328 unsigned VecElts = 4;
Scott Michelfdc40a02009-02-17 22:15:04 +00005329
Chris Lattner62098042008-03-09 01:05:04 +00005330 // Truncate the value (which may itself be a constant) to i32, and
5331 // convert it to a vector with movd (S2V+shuffle to zero extend).
Owen Anderson825b72b2009-08-11 20:47:22 +00005332 Item = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Item);
Dale Johannesenace16102009-02-03 19:33:06 +00005333 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VecVT, Item);
Craig Topper12216172012-01-13 08:12:35 +00005334 Item = getShuffleVectorZeroOrUndef(Item, 0, true, Subtarget, DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +00005335
Chris Lattner62098042008-03-09 01:05:04 +00005336 // Now we have our 32-bit value zero extended in the low element of
5337 // a vector. If Idx != 0, swizzle it into place.
5338 if (Idx != 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005339 SmallVector<int, 4> Mask;
5340 Mask.push_back(Idx);
5341 for (unsigned i = 1; i != VecElts; ++i)
5342 Mask.push_back(i);
Craig Topperdf966f62012-04-22 19:17:57 +00005343 Item = DAG.getVectorShuffle(VecVT, dl, Item, DAG.getUNDEF(VecVT),
Nate Begeman9008ca62009-04-27 18:41:29 +00005344 &Mask[0]);
Chris Lattner62098042008-03-09 01:05:04 +00005345 }
Craig Topper07a27622012-01-22 03:07:48 +00005346 return DAG.getNode(ISD::BITCAST, dl, VT, Item);
Chris Lattner62098042008-03-09 01:05:04 +00005347 }
5348 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005349
Chris Lattner19f79692008-03-08 22:59:52 +00005350 // If we have a constant or non-constant insertion into the low element of
5351 // a vector, we can do this with SCALAR_TO_VECTOR + shuffle of zero into
5352 // the rest of the elements. This will be matched as movd/movq/movss/movsd
Eli Friedman10415532009-06-06 06:05:10 +00005353 // depending on what the source datatype is.
5354 if (Idx == 0) {
Craig Topperd62c16e2011-12-29 03:20:51 +00005355 if (NumZero == 0)
Eli Friedman10415532009-06-06 06:05:10 +00005356 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
Craig Topperd62c16e2011-12-29 03:20:51 +00005357
5358 if (ExtVT == MVT::i32 || ExtVT == MVT::f32 || ExtVT == MVT::f64 ||
Owen Anderson825b72b2009-08-11 20:47:22 +00005359 (ExtVT == MVT::i64 && Subtarget->is64Bit())) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00005360 if (VT.is256BitVector()) {
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00005361 SDValue ZeroVec = getZeroVector(VT, Subtarget, DAG, dl);
Nadav Rotem394a1f52012-01-11 14:07:51 +00005362 return DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, ZeroVec,
5363 Item, DAG.getIntPtrConstant(0));
Elena Demikhovsky021c0a22011-12-28 08:14:01 +00005364 }
Craig Topper7a9a28b2012-08-12 02:23:29 +00005365 assert(VT.is128BitVector() && "Expected an SSE value type!");
Eli Friedman10415532009-06-06 06:05:10 +00005366 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
5367 // Turn it into a MOVL (i.e. movss, movsd, or movd) to a zero vector.
Craig Topper12216172012-01-13 08:12:35 +00005368 return getShuffleVectorZeroOrUndef(Item, 0, true, Subtarget, DAG);
Craig Topperd62c16e2011-12-29 03:20:51 +00005369 }
5370
5371 if (ExtVT == MVT::i16 || ExtVT == MVT::i8) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005372 Item = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, Item);
Craig Topper3224e6b2011-12-29 03:09:33 +00005373 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32, Item);
Craig Topper7a9a28b2012-08-12 02:23:29 +00005374 if (VT.is256BitVector()) {
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00005375 SDValue ZeroVec = getZeroVector(MVT::v8i32, Subtarget, DAG, dl);
Craig Topperb14940a2012-04-22 20:55:18 +00005376 Item = Insert128BitVector(ZeroVec, Item, 0, DAG, dl);
Craig Topper19ec2a92011-12-29 03:34:54 +00005377 } else {
Craig Topper7a9a28b2012-08-12 02:23:29 +00005378 assert(VT.is128BitVector() && "Expected an SSE value type!");
Craig Topper12216172012-01-13 08:12:35 +00005379 Item = getShuffleVectorZeroOrUndef(Item, 0, true, Subtarget, DAG);
Craig Topper19ec2a92011-12-29 03:34:54 +00005380 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005381 return DAG.getNode(ISD::BITCAST, dl, VT, Item);
Eli Friedman10415532009-06-06 06:05:10 +00005382 }
Chris Lattnerc9517fb2008-03-08 22:48:29 +00005383 }
Evan Chengf26ffe92008-05-29 08:22:04 +00005384
5385 // Is it a vector logical left shift?
5386 if (NumElems == 2 && Idx == 1 &&
Evan Cheng37b73872009-07-30 08:33:02 +00005387 X86::isZeroNode(Op.getOperand(0)) &&
5388 !X86::isZeroNode(Op.getOperand(1))) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00005389 unsigned NumBits = VT.getSizeInBits();
Evan Chengf26ffe92008-05-29 08:22:04 +00005390 return getVShift(true, VT,
Scott Michelfdc40a02009-02-17 22:15:04 +00005391 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Dale Johannesenb300d2a2009-02-07 00:55:49 +00005392 VT, Op.getOperand(1)),
Dale Johannesenace16102009-02-03 19:33:06 +00005393 NumBits/2, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00005394 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005395
Chris Lattnerc9517fb2008-03-08 22:48:29 +00005396 if (IsAllConstants) // Otherwise, it's better to do a constpool load.
Dan Gohman475871a2008-07-27 21:46:04 +00005397 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005398
Chris Lattner19f79692008-03-08 22:59:52 +00005399 // Otherwise, if this is a vector with i32 or f32 elements, and the element
5400 // is a non-constant being inserted into an element other than the low one,
5401 // we can't use a constant pool load. Instead, use SCALAR_TO_VECTOR (aka
5402 // movd/movss) to move this into the low element, then shuffle it into
5403 // place.
Evan Cheng0db9fe62006-04-25 20:13:52 +00005404 if (EVTBits == 32) {
Dale Johannesenace16102009-02-03 19:33:06 +00005405 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
Scott Michelfdc40a02009-02-17 22:15:04 +00005406
Evan Cheng0db9fe62006-04-25 20:13:52 +00005407 // Turn it into a shuffle of zero and zero-extended scalar to vector.
Craig Topper12216172012-01-13 08:12:35 +00005408 Item = getShuffleVectorZeroOrUndef(Item, 0, NumZero > 0, Subtarget, DAG);
Nate Begeman9008ca62009-04-27 18:41:29 +00005409 SmallVector<int, 8> MaskVec;
Craig Topper31a207a2012-05-04 06:39:13 +00005410 for (unsigned i = 0; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00005411 MaskVec.push_back(i == Idx ? 0 : 1);
5412 return DAG.getVectorShuffle(VT, dl, Item, DAG.getUNDEF(VT), &MaskVec[0]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005413 }
5414 }
5415
Chris Lattner67f453a2008-03-09 05:42:06 +00005416 // Splat is obviously ok. Let legalizer expand it to a shuffle.
Evan Chengc3630942009-12-09 21:00:30 +00005417 if (Values.size() == 1) {
5418 if (EVTBits == 32) {
5419 // Instead of a shuffle like this:
5420 // shuffle (scalar_to_vector (load (ptr + 4))), undef, <0, 0, 0, 0>
5421 // Check if it's possible to issue this instead.
5422 // shuffle (vload ptr)), undef, <1, 1, 1, 1>
5423 unsigned Idx = CountTrailingZeros_32(NonZeros);
5424 SDValue Item = Op.getOperand(Idx);
5425 if (Op.getNode()->isOnlyUserOf(Item.getNode()))
5426 return LowerAsSplatVectorLoad(Item, VT, dl, DAG);
5427 }
Dan Gohman475871a2008-07-27 21:46:04 +00005428 return SDValue();
Evan Chengc3630942009-12-09 21:00:30 +00005429 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005430
Dan Gohmana3941172007-07-24 22:55:08 +00005431 // A vector full of immediates; various special cases are already
5432 // handled, so this is best done with a single constant-pool load.
Chris Lattnerc9517fb2008-03-08 22:48:29 +00005433 if (IsAllConstants)
Dan Gohman475871a2008-07-27 21:46:04 +00005434 return SDValue();
Dan Gohmana3941172007-07-24 22:55:08 +00005435
Bruno Cardoso Lopes6683efb2011-07-22 00:15:07 +00005436 // For AVX-length vectors, build the individual 128-bit pieces and use
5437 // shuffles to put them in place.
Craig Topper7a9a28b2012-08-12 02:23:29 +00005438 if (VT.is256BitVector()) {
Bruno Cardoso Lopes6683efb2011-07-22 00:15:07 +00005439 SmallVector<SDValue, 32> V;
Craig Topperfa5b70e2012-02-03 06:32:21 +00005440 for (unsigned i = 0; i != NumElems; ++i)
Bruno Cardoso Lopes6683efb2011-07-22 00:15:07 +00005441 V.push_back(Op.getOperand(i));
5442
5443 EVT HVT = EVT::getVectorVT(*DAG.getContext(), ExtVT, NumElems/2);
5444
5445 // Build both the lower and upper subvector.
5446 SDValue Lower = DAG.getNode(ISD::BUILD_VECTOR, dl, HVT, &V[0], NumElems/2);
5447 SDValue Upper = DAG.getNode(ISD::BUILD_VECTOR, dl, HVT, &V[NumElems / 2],
5448 NumElems/2);
5449
5450 // Recreate the wider vector with the lower and upper part.
Craig Topper4c7972d2012-04-22 18:15:59 +00005451 return Concat128BitVectors(Lower, Upper, VT, NumElems, DAG, dl);
Bruno Cardoso Lopes6683efb2011-07-22 00:15:07 +00005452 }
5453
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00005454 // Let legalizer expand 2-wide build_vectors.
Evan Cheng7e2ff772008-05-08 00:57:18 +00005455 if (EVTBits == 64) {
5456 if (NumNonZero == 1) {
5457 // One half is zero or undef.
5458 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dale Johannesenace16102009-02-03 19:33:06 +00005459 SDValue V2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT,
Evan Cheng7e2ff772008-05-08 00:57:18 +00005460 Op.getOperand(Idx));
Craig Topper12216172012-01-13 08:12:35 +00005461 return getShuffleVectorZeroOrUndef(V2, Idx, true, Subtarget, DAG);
Evan Cheng7e2ff772008-05-08 00:57:18 +00005462 }
Dan Gohman475871a2008-07-27 21:46:04 +00005463 return SDValue();
Evan Cheng7e2ff772008-05-08 00:57:18 +00005464 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00005465
5466 // If element VT is < 32 bits, convert it to inserts into a zero vector.
Bill Wendling826f36f2007-03-28 00:57:11 +00005467 if (EVTBits == 8 && NumElems == 16) {
Dan Gohman475871a2008-07-27 21:46:04 +00005468 SDValue V = LowerBuildVectorv16i8(Op, NonZeros,NumNonZero,NumZero, DAG,
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00005469 Subtarget, *this);
Gabor Greifba36cb52008-08-28 21:40:38 +00005470 if (V.getNode()) return V;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005471 }
5472
Bill Wendling826f36f2007-03-28 00:57:11 +00005473 if (EVTBits == 16 && NumElems == 8) {
Dan Gohman475871a2008-07-27 21:46:04 +00005474 SDValue V = LowerBuildVectorv8i16(Op, NonZeros,NumNonZero,NumZero, DAG,
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00005475 Subtarget, *this);
Gabor Greifba36cb52008-08-28 21:40:38 +00005476 if (V.getNode()) return V;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005477 }
5478
5479 // If element VT is == 32 bits, turn it into a number of shuffles.
Benjamin Kramer9c683542012-01-30 15:16:21 +00005480 SmallVector<SDValue, 8> V(NumElems);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005481 if (NumElems == 4 && NumZero > 0) {
5482 for (unsigned i = 0; i < 4; ++i) {
5483 bool isZero = !(NonZeros & (1 << i));
5484 if (isZero)
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00005485 V[i] = getZeroVector(VT, Subtarget, DAG, dl);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005486 else
Dale Johannesenace16102009-02-03 19:33:06 +00005487 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(i));
Evan Cheng0db9fe62006-04-25 20:13:52 +00005488 }
5489
5490 for (unsigned i = 0; i < 2; ++i) {
5491 switch ((NonZeros & (0x3 << i*2)) >> (i*2)) {
5492 default: break;
5493 case 0:
5494 V[i] = V[i*2]; // Must be a zero vector.
5495 break;
5496 case 1:
Nate Begeman9008ca62009-04-27 18:41:29 +00005497 V[i] = getMOVL(DAG, dl, VT, V[i*2+1], V[i*2]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005498 break;
5499 case 2:
Nate Begeman9008ca62009-04-27 18:41:29 +00005500 V[i] = getMOVL(DAG, dl, VT, V[i*2], V[i*2+1]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005501 break;
5502 case 3:
Nate Begeman9008ca62009-04-27 18:41:29 +00005503 V[i] = getUnpackl(DAG, dl, VT, V[i*2], V[i*2+1]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005504 break;
5505 }
5506 }
5507
Benjamin Kramer9c683542012-01-30 15:16:21 +00005508 bool Reverse1 = (NonZeros & 0x3) == 2;
5509 bool Reverse2 = ((NonZeros & (0x3 << 2)) >> 2) == 2;
5510 int MaskVec[] = {
5511 Reverse1 ? 1 : 0,
5512 Reverse1 ? 0 : 1,
Benjamin Kramer630ecf02012-01-30 20:01:35 +00005513 static_cast<int>(Reverse2 ? NumElems+1 : NumElems),
5514 static_cast<int>(Reverse2 ? NumElems : NumElems+1)
Benjamin Kramer9c683542012-01-30 15:16:21 +00005515 };
Nate Begeman9008ca62009-04-27 18:41:29 +00005516 return DAG.getVectorShuffle(VT, dl, V[0], V[1], &MaskVec[0]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005517 }
5518
Craig Topper7a9a28b2012-08-12 02:23:29 +00005519 if (Values.size() > 1 && VT.is128BitVector()) {
Nate Begemanfdea31a2010-03-24 20:49:50 +00005520 // Check for a build vector of consecutive loads.
5521 for (unsigned i = 0; i < NumElems; ++i)
5522 V[i] = Op.getOperand(i);
Michael J. Spencerec38de22010-10-10 22:04:20 +00005523
Nate Begemanfdea31a2010-03-24 20:49:50 +00005524 // Check for elements which are consecutive loads.
5525 SDValue LD = EltsFromConsecutiveLoads(VT, V, dl, DAG);
5526 if (LD.getNode())
5527 return LD;
Michael J. Spencerec38de22010-10-10 22:04:20 +00005528
Michael Liaofacace82012-10-19 17:15:18 +00005529 // Check for a build vector from mostly shuffle plus few inserting.
5530 SDValue Sh = buildFromShuffleMostly(Op, DAG);
5531 if (Sh.getNode())
5532 return Sh;
5533
Michael J. Spencerec38de22010-10-10 22:04:20 +00005534 // For SSE 4.1, use insertps to put the high elements into the low element.
Craig Topperd0a31172012-01-10 06:37:29 +00005535 if (getSubtarget()->hasSSE41()) {
Chris Lattner24faf612010-08-28 17:59:08 +00005536 SDValue Result;
5537 if (Op.getOperand(0).getOpcode() != ISD::UNDEF)
5538 Result = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(0));
5539 else
5540 Result = DAG.getUNDEF(VT);
Michael J. Spencerec38de22010-10-10 22:04:20 +00005541
Chris Lattner24faf612010-08-28 17:59:08 +00005542 for (unsigned i = 1; i < NumElems; ++i) {
5543 if (Op.getOperand(i).getOpcode() == ISD::UNDEF) continue;
5544 Result = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, Result,
Nate Begeman9008ca62009-04-27 18:41:29 +00005545 Op.getOperand(i), DAG.getIntPtrConstant(i));
Chris Lattner24faf612010-08-28 17:59:08 +00005546 }
5547 return Result;
Nate Begeman9008ca62009-04-27 18:41:29 +00005548 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00005549
Chris Lattner6e80e442010-08-28 17:15:43 +00005550 // Otherwise, expand into a number of unpckl*, start by extending each of
5551 // our (non-undef) elements to the full vector width with the element in the
5552 // bottom slot of the vector (which generates no code for SSE).
5553 for (unsigned i = 0; i < NumElems; ++i) {
5554 if (Op.getOperand(i).getOpcode() != ISD::UNDEF)
5555 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(i));
5556 else
5557 V[i] = DAG.getUNDEF(VT);
5558 }
5559
5560 // Next, we iteratively mix elements, e.g. for v4f32:
Evan Cheng0db9fe62006-04-25 20:13:52 +00005561 // Step 1: unpcklps 0, 2 ==> X: <?, ?, 2, 0>
5562 // : unpcklps 1, 3 ==> Y: <?, ?, 3, 1>
5563 // Step 2: unpcklps X, Y ==> <3, 2, 1, 0>
Chris Lattner6e80e442010-08-28 17:15:43 +00005564 unsigned EltStride = NumElems >> 1;
5565 while (EltStride != 0) {
Chris Lattner3ddcc432010-08-28 17:28:30 +00005566 for (unsigned i = 0; i < EltStride; ++i) {
5567 // If V[i+EltStride] is undef and this is the first round of mixing,
5568 // then it is safe to just drop this shuffle: V[i] is already in the
5569 // right place, the one element (since it's the first round) being
5570 // inserted as undef can be dropped. This isn't safe for successive
5571 // rounds because they will permute elements within both vectors.
5572 if (V[i+EltStride].getOpcode() == ISD::UNDEF &&
5573 EltStride == NumElems/2)
5574 continue;
Michael J. Spencerec38de22010-10-10 22:04:20 +00005575
Chris Lattner6e80e442010-08-28 17:15:43 +00005576 V[i] = getUnpackl(DAG, dl, VT, V[i], V[i + EltStride]);
Chris Lattner3ddcc432010-08-28 17:28:30 +00005577 }
Chris Lattner6e80e442010-08-28 17:15:43 +00005578 EltStride >>= 1;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005579 }
5580 return V[0];
5581 }
Dan Gohman475871a2008-07-27 21:46:04 +00005582 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005583}
5584
Bruno Cardoso Lopes8af24512011-08-01 21:54:02 +00005585// LowerAVXCONCAT_VECTORS - 256-bit AVX can use the vinsertf128 instruction
5586// to create 256-bit vectors from two other 128-bit ones.
5587static SDValue LowerAVXCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) {
5588 DebugLoc dl = Op.getDebugLoc();
5589 EVT ResVT = Op.getValueType();
5590
Craig Topper7a9a28b2012-08-12 02:23:29 +00005591 assert(ResVT.is256BitVector() && "Value type must be 256-bit wide");
Bruno Cardoso Lopes8af24512011-08-01 21:54:02 +00005592
5593 SDValue V1 = Op.getOperand(0);
5594 SDValue V2 = Op.getOperand(1);
5595 unsigned NumElems = ResVT.getVectorNumElements();
5596
Craig Topper4c7972d2012-04-22 18:15:59 +00005597 return Concat128BitVectors(V1, V2, ResVT, NumElems, DAG, dl);
Bruno Cardoso Lopes8af24512011-08-01 21:54:02 +00005598}
5599
Craig Topper55b24052012-09-11 06:15:32 +00005600static SDValue LowerCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) {
Bruno Cardoso Lopes8af24512011-08-01 21:54:02 +00005601 assert(Op.getNumOperands() == 2);
Bruno Cardoso Lopes8af24512011-08-01 21:54:02 +00005602
5603 // 256-bit AVX can use the vinsertf128 instruction to create 256-bit vectors
5604 // from two other 128-bit ones.
5605 return LowerAVXCONCAT_VECTORS(Op, DAG);
5606}
5607
Nadav Roteme80aa7c2012-04-09 08:33:21 +00005608// Try to lower a shuffle node into a simple blend instruction.
Craig Topper55b24052012-09-11 06:15:32 +00005609static SDValue
5610LowerVECTOR_SHUFFLEtoBlend(ShuffleVectorSDNode *SVOp,
5611 const X86Subtarget *Subtarget, SelectionDAG &DAG) {
Nadav Roteme80aa7c2012-04-09 08:33:21 +00005612 SDValue V1 = SVOp->getOperand(0);
5613 SDValue V2 = SVOp->getOperand(1);
5614 DebugLoc dl = SVOp->getDebugLoc();
Craig Topper708e44f2012-04-23 07:36:33 +00005615 MVT VT = SVOp->getValueType(0).getSimpleVT();
Craig Topper1842ba02012-04-23 06:38:28 +00005616 unsigned NumElems = VT.getVectorNumElements();
Nadav Roteme80aa7c2012-04-09 08:33:21 +00005617
Nadav Roteme6113782012-04-11 06:40:27 +00005618 if (!Subtarget->hasSSE41())
Nadav Roteme80aa7c2012-04-09 08:33:21 +00005619 return SDValue();
5620
Craig Topper1842ba02012-04-23 06:38:28 +00005621 unsigned ISDNo = 0;
Nadav Roteme6113782012-04-11 06:40:27 +00005622 MVT OpTy;
5623
Craig Topper708e44f2012-04-23 07:36:33 +00005624 switch (VT.SimpleTy) {
Nadav Roteme6113782012-04-11 06:40:27 +00005625 default: return SDValue();
5626 case MVT::v8i16:
Craig Topper1842ba02012-04-23 06:38:28 +00005627 ISDNo = X86ISD::BLENDPW;
5628 OpTy = MVT::v8i16;
5629 break;
Nadav Roteme6113782012-04-11 06:40:27 +00005630 case MVT::v4i32:
5631 case MVT::v4f32:
Craig Topper1842ba02012-04-23 06:38:28 +00005632 ISDNo = X86ISD::BLENDPS;
5633 OpTy = MVT::v4f32;
5634 break;
Nadav Roteme6113782012-04-11 06:40:27 +00005635 case MVT::v2i64:
5636 case MVT::v2f64:
Craig Topper1842ba02012-04-23 06:38:28 +00005637 ISDNo = X86ISD::BLENDPD;
5638 OpTy = MVT::v2f64;
5639 break;
Nadav Roteme6113782012-04-11 06:40:27 +00005640 case MVT::v8i32:
5641 case MVT::v8f32:
Craig Topper1842ba02012-04-23 06:38:28 +00005642 if (!Subtarget->hasAVX())
5643 return SDValue();
5644 ISDNo = X86ISD::BLENDPS;
5645 OpTy = MVT::v8f32;
5646 break;
Nadav Roteme6113782012-04-11 06:40:27 +00005647 case MVT::v4i64:
5648 case MVT::v4f64:
Craig Topper1842ba02012-04-23 06:38:28 +00005649 if (!Subtarget->hasAVX())
5650 return SDValue();
5651 ISDNo = X86ISD::BLENDPD;
5652 OpTy = MVT::v4f64;
5653 break;
Nadav Roteme6113782012-04-11 06:40:27 +00005654 }
5655 assert(ISDNo && "Invalid Op Number");
5656
5657 unsigned MaskVals = 0;
Nadav Roteme80aa7c2012-04-09 08:33:21 +00005658
Craig Topper1842ba02012-04-23 06:38:28 +00005659 for (unsigned i = 0; i != NumElems; ++i) {
Nadav Roteme80aa7c2012-04-09 08:33:21 +00005660 int EltIdx = SVOp->getMaskElt(i);
Craig Topper1842ba02012-04-23 06:38:28 +00005661 if (EltIdx == (int)i || EltIdx < 0)
Nadav Roteme6113782012-04-11 06:40:27 +00005662 MaskVals |= (1<<i);
Craig Topper1842ba02012-04-23 06:38:28 +00005663 else if (EltIdx == (int)(i + NumElems))
Nadav Roteme6113782012-04-11 06:40:27 +00005664 continue; // Bit is set to zero;
Craig Topper1842ba02012-04-23 06:38:28 +00005665 else
5666 return SDValue();
Nadav Roteme80aa7c2012-04-09 08:33:21 +00005667 }
5668
Nadav Roteme6113782012-04-11 06:40:27 +00005669 V1 = DAG.getNode(ISD::BITCAST, dl, OpTy, V1);
5670 V2 = DAG.getNode(ISD::BITCAST, dl, OpTy, V2);
5671 SDValue Ret = DAG.getNode(ISDNo, dl, OpTy, V1, V2,
5672 DAG.getConstant(MaskVals, MVT::i32));
5673 return DAG.getNode(ISD::BITCAST, dl, VT, Ret);
Nadav Roteme80aa7c2012-04-09 08:33:21 +00005674}
5675
Nate Begemanb9a47b82009-02-23 08:49:38 +00005676// v8i16 shuffles - Prefer shuffles in the following order:
5677// 1. [all] pshuflw, pshufhw, optional move
5678// 2. [ssse3] 1 x pshufb
5679// 3. [ssse3] 2 x pshufb + 1 x por
5680// 4. [all] mov + pshuflw + pshufhw + N x (pextrw + pinsrw)
Craig Topper55b24052012-09-11 06:15:32 +00005681static SDValue
5682LowerVECTOR_SHUFFLEv8i16(SDValue Op, const X86Subtarget *Subtarget,
5683 SelectionDAG &DAG) {
Bruno Cardoso Lopesbf8154a2010-08-21 01:32:18 +00005684 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Nate Begeman9008ca62009-04-27 18:41:29 +00005685 SDValue V1 = SVOp->getOperand(0);
5686 SDValue V2 = SVOp->getOperand(1);
5687 DebugLoc dl = SVOp->getDebugLoc();
Nate Begemanb9a47b82009-02-23 08:49:38 +00005688 SmallVector<int, 8> MaskVals;
Evan Cheng14b32e12007-12-11 01:46:18 +00005689
Nate Begemanb9a47b82009-02-23 08:49:38 +00005690 // Determine if more than 1 of the words in each of the low and high quadwords
5691 // of the result come from the same quadword of one of the two inputs. Undef
5692 // mask values count as coming from any quadword, for better codegen.
Benjamin Kramer003fad92011-10-15 13:28:31 +00005693 unsigned LoQuad[] = { 0, 0, 0, 0 };
5694 unsigned HiQuad[] = { 0, 0, 0, 0 };
Benjamin Kramer699ddcb2012-02-06 12:06:18 +00005695 std::bitset<4> InputQuads;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005696 for (unsigned i = 0; i < 8; ++i) {
Benjamin Kramer003fad92011-10-15 13:28:31 +00005697 unsigned *Quad = i < 4 ? LoQuad : HiQuad;
Nate Begeman9008ca62009-04-27 18:41:29 +00005698 int EltIdx = SVOp->getMaskElt(i);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005699 MaskVals.push_back(EltIdx);
5700 if (EltIdx < 0) {
5701 ++Quad[0];
5702 ++Quad[1];
5703 ++Quad[2];
5704 ++Quad[3];
Evan Cheng14b32e12007-12-11 01:46:18 +00005705 continue;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005706 }
5707 ++Quad[EltIdx / 4];
5708 InputQuads.set(EltIdx / 4);
Evan Cheng14b32e12007-12-11 01:46:18 +00005709 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00005710
Nate Begemanb9a47b82009-02-23 08:49:38 +00005711 int BestLoQuad = -1;
Evan Cheng14b32e12007-12-11 01:46:18 +00005712 unsigned MaxQuad = 1;
5713 for (unsigned i = 0; i < 4; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005714 if (LoQuad[i] > MaxQuad) {
5715 BestLoQuad = i;
5716 MaxQuad = LoQuad[i];
Evan Cheng14b32e12007-12-11 01:46:18 +00005717 }
Evan Cheng8a86c3f2007-12-07 08:07:39 +00005718 }
5719
Nate Begemanb9a47b82009-02-23 08:49:38 +00005720 int BestHiQuad = -1;
Evan Cheng14b32e12007-12-11 01:46:18 +00005721 MaxQuad = 1;
5722 for (unsigned i = 0; i < 4; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005723 if (HiQuad[i] > MaxQuad) {
5724 BestHiQuad = i;
5725 MaxQuad = HiQuad[i];
Evan Cheng14b32e12007-12-11 01:46:18 +00005726 }
5727 }
5728
Nate Begemanb9a47b82009-02-23 08:49:38 +00005729 // For SSSE3, If all 8 words of the result come from only 1 quadword of each
Eric Christopherfd179292009-08-27 18:07:15 +00005730 // of the two input vectors, shuffle them into one input vector so only a
Nate Begemanb9a47b82009-02-23 08:49:38 +00005731 // single pshufb instruction is necessary. If There are more than 2 input
5732 // quads, disable the next transformation since it does not help SSSE3.
5733 bool V1Used = InputQuads[0] || InputQuads[1];
5734 bool V2Used = InputQuads[2] || InputQuads[3];
Craig Topperd0a31172012-01-10 06:37:29 +00005735 if (Subtarget->hasSSSE3()) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005736 if (InputQuads.count() == 2 && V1Used && V2Used) {
Benjamin Kramer699ddcb2012-02-06 12:06:18 +00005737 BestLoQuad = InputQuads[0] ? 0 : 1;
5738 BestHiQuad = InputQuads[2] ? 2 : 3;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005739 }
5740 if (InputQuads.count() > 2) {
5741 BestLoQuad = -1;
5742 BestHiQuad = -1;
5743 }
5744 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00005745
Nate Begemanb9a47b82009-02-23 08:49:38 +00005746 // If BestLoQuad or BestHiQuad are set, shuffle the quads together and update
5747 // the shuffle mask. If a quad is scored as -1, that means that it contains
5748 // words from all 4 input quadwords.
5749 SDValue NewV;
5750 if (BestLoQuad >= 0 || BestHiQuad >= 0) {
Benjamin Kramer9c683542012-01-30 15:16:21 +00005751 int MaskV[] = {
5752 BestLoQuad < 0 ? 0 : BestLoQuad,
5753 BestHiQuad < 0 ? 1 : BestHiQuad
5754 };
Eric Christopherfd179292009-08-27 18:07:15 +00005755 NewV = DAG.getVectorShuffle(MVT::v2i64, dl,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005756 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64, V1),
5757 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64, V2), &MaskV[0]);
5758 NewV = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, NewV);
Evan Cheng14b32e12007-12-11 01:46:18 +00005759
Nate Begemanb9a47b82009-02-23 08:49:38 +00005760 // Rewrite the MaskVals and assign NewV to V1 if NewV now contains all the
5761 // source words for the shuffle, to aid later transformations.
5762 bool AllWordsInNewV = true;
Mon P Wang37b9a192009-03-11 06:35:11 +00005763 bool InOrder[2] = { true, true };
Evan Cheng14b32e12007-12-11 01:46:18 +00005764 for (unsigned i = 0; i != 8; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005765 int idx = MaskVals[i];
Mon P Wang37b9a192009-03-11 06:35:11 +00005766 if (idx != (int)i)
5767 InOrder[i/4] = false;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005768 if (idx < 0 || (idx/4) == BestLoQuad || (idx/4) == BestHiQuad)
Evan Cheng14b32e12007-12-11 01:46:18 +00005769 continue;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005770 AllWordsInNewV = false;
5771 break;
Evan Cheng14b32e12007-12-11 01:46:18 +00005772 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00005773
Nate Begemanb9a47b82009-02-23 08:49:38 +00005774 bool pshuflw = AllWordsInNewV, pshufhw = AllWordsInNewV;
5775 if (AllWordsInNewV) {
5776 for (int i = 0; i != 8; ++i) {
5777 int idx = MaskVals[i];
5778 if (idx < 0)
Evan Cheng14b32e12007-12-11 01:46:18 +00005779 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00005780 idx = MaskVals[i] = (idx / 4) == BestLoQuad ? (idx & 3) : (idx & 3) + 4;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005781 if ((idx != i) && idx < 4)
5782 pshufhw = false;
5783 if ((idx != i) && idx > 3)
5784 pshuflw = false;
Evan Cheng14b32e12007-12-11 01:46:18 +00005785 }
Nate Begemanb9a47b82009-02-23 08:49:38 +00005786 V1 = NewV;
5787 V2Used = false;
5788 BestLoQuad = 0;
5789 BestHiQuad = 1;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00005790 }
Evan Cheng14b32e12007-12-11 01:46:18 +00005791
Nate Begemanb9a47b82009-02-23 08:49:38 +00005792 // If we've eliminated the use of V2, and the new mask is a pshuflw or
5793 // pshufhw, that's as cheap as it gets. Return the new shuffle.
Mon P Wang37b9a192009-03-11 06:35:11 +00005794 if ((pshufhw && InOrder[0]) || (pshuflw && InOrder[1])) {
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00005795 unsigned Opc = pshufhw ? X86ISD::PSHUFHW : X86ISD::PSHUFLW;
5796 unsigned TargetMask = 0;
5797 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV,
Owen Anderson825b72b2009-08-11 20:47:22 +00005798 DAG.getUNDEF(MVT::v8i16), &MaskVals[0]);
Craig Topperdd637ae2012-02-19 05:41:45 +00005799 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(NewV.getNode());
5800 TargetMask = pshufhw ? getShufflePSHUFHWImmediate(SVOp):
5801 getShufflePSHUFLWImmediate(SVOp);
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00005802 V1 = NewV.getOperand(0);
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00005803 return getTargetShuffleNode(Opc, dl, MVT::v8i16, V1, TargetMask, DAG);
Evan Cheng14b32e12007-12-11 01:46:18 +00005804 }
Evan Cheng14b32e12007-12-11 01:46:18 +00005805 }
Eric Christopherfd179292009-08-27 18:07:15 +00005806
Nate Begemanb9a47b82009-02-23 08:49:38 +00005807 // If we have SSSE3, and all words of the result are from 1 input vector,
5808 // case 2 is generated, otherwise case 3 is generated. If no SSSE3
5809 // is present, fall back to case 4.
Craig Topperd0a31172012-01-10 06:37:29 +00005810 if (Subtarget->hasSSSE3()) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005811 SmallVector<SDValue,16> pshufbMask;
Eric Christopherfd179292009-08-27 18:07:15 +00005812
Nate Begemanb9a47b82009-02-23 08:49:38 +00005813 // If we have elements from both input vectors, set the high bit of the
Eric Christopherfd179292009-08-27 18:07:15 +00005814 // shuffle mask element to zero out elements that come from V2 in the V1
Nate Begemanb9a47b82009-02-23 08:49:38 +00005815 // mask, and elements that come from V1 in the V2 mask, so that the two
5816 // results can be OR'd together.
5817 bool TwoInputs = V1Used && V2Used;
5818 for (unsigned i = 0; i != 8; ++i) {
5819 int EltIdx = MaskVals[i] * 2;
Craig Topperbe97ae92012-05-18 07:07:36 +00005820 int Idx0 = (TwoInputs && (EltIdx >= 16)) ? 0x80 : EltIdx;
5821 int Idx1 = (TwoInputs && (EltIdx >= 16)) ? 0x80 : EltIdx+1;
5822 pshufbMask.push_back(DAG.getConstant(Idx0, MVT::i8));
5823 pshufbMask.push_back(DAG.getConstant(Idx1, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005824 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005825 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, V1);
Eric Christopherfd179292009-08-27 18:07:15 +00005826 V1 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V1,
Evan Chenga87008d2009-02-25 22:49:59 +00005827 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005828 MVT::v16i8, &pshufbMask[0], 16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005829 if (!TwoInputs)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005830 return DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V1);
Eric Christopherfd179292009-08-27 18:07:15 +00005831
Nate Begemanb9a47b82009-02-23 08:49:38 +00005832 // Calculate the shuffle mask for the second input, shuffle it, and
5833 // OR it with the first shuffled input.
5834 pshufbMask.clear();
5835 for (unsigned i = 0; i != 8; ++i) {
5836 int EltIdx = MaskVals[i] * 2;
Craig Topperbe97ae92012-05-18 07:07:36 +00005837 int Idx0 = (EltIdx < 16) ? 0x80 : EltIdx - 16;
5838 int Idx1 = (EltIdx < 16) ? 0x80 : EltIdx - 15;
5839 pshufbMask.push_back(DAG.getConstant(Idx0, MVT::i8));
5840 pshufbMask.push_back(DAG.getConstant(Idx1, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005841 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005842 V2 = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, V2);
Eric Christopherfd179292009-08-27 18:07:15 +00005843 V2 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V2,
Evan Chenga87008d2009-02-25 22:49:59 +00005844 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005845 MVT::v16i8, &pshufbMask[0], 16));
5846 V1 = DAG.getNode(ISD::OR, dl, MVT::v16i8, V1, V2);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005847 return DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005848 }
5849
5850 // If BestLoQuad >= 0, generate a pshuflw to put the low elements in order,
5851 // and update MaskVals with new element order.
Benjamin Kramer9c683542012-01-30 15:16:21 +00005852 std::bitset<8> InOrder;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005853 if (BestLoQuad >= 0) {
Benjamin Kramer9c683542012-01-30 15:16:21 +00005854 int MaskV[] = { -1, -1, -1, -1, 4, 5, 6, 7 };
Nate Begemanb9a47b82009-02-23 08:49:38 +00005855 for (int i = 0; i != 4; ++i) {
5856 int idx = MaskVals[i];
5857 if (idx < 0) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005858 InOrder.set(i);
5859 } else if ((idx / 4) == BestLoQuad) {
Benjamin Kramer9c683542012-01-30 15:16:21 +00005860 MaskV[i] = idx & 3;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005861 InOrder.set(i);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005862 }
5863 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005864 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV, DAG.getUNDEF(MVT::v8i16),
Nate Begeman9008ca62009-04-27 18:41:29 +00005865 &MaskV[0]);
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00005866
Craig Topperdd637ae2012-02-19 05:41:45 +00005867 if (NewV.getOpcode() == ISD::VECTOR_SHUFFLE && Subtarget->hasSSSE3()) {
5868 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(NewV.getNode());
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00005869 NewV = getTargetShuffleNode(X86ISD::PSHUFLW, dl, MVT::v8i16,
Craig Topperdd637ae2012-02-19 05:41:45 +00005870 NewV.getOperand(0),
5871 getShufflePSHUFLWImmediate(SVOp), DAG);
5872 }
Nate Begemanb9a47b82009-02-23 08:49:38 +00005873 }
Eric Christopherfd179292009-08-27 18:07:15 +00005874
Nate Begemanb9a47b82009-02-23 08:49:38 +00005875 // If BestHi >= 0, generate a pshufhw to put the high elements in order,
5876 // and update MaskVals with the new element order.
5877 if (BestHiQuad >= 0) {
Benjamin Kramer9c683542012-01-30 15:16:21 +00005878 int MaskV[] = { 0, 1, 2, 3, -1, -1, -1, -1 };
Nate Begemanb9a47b82009-02-23 08:49:38 +00005879 for (unsigned i = 4; i != 8; ++i) {
5880 int idx = MaskVals[i];
5881 if (idx < 0) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005882 InOrder.set(i);
5883 } else if ((idx / 4) == BestHiQuad) {
Benjamin Kramer9c683542012-01-30 15:16:21 +00005884 MaskV[i] = (idx & 3) + 4;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005885 InOrder.set(i);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005886 }
5887 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005888 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV, DAG.getUNDEF(MVT::v8i16),
Nate Begeman9008ca62009-04-27 18:41:29 +00005889 &MaskV[0]);
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00005890
Craig Topperdd637ae2012-02-19 05:41:45 +00005891 if (NewV.getOpcode() == ISD::VECTOR_SHUFFLE && Subtarget->hasSSSE3()) {
5892 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(NewV.getNode());
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00005893 NewV = getTargetShuffleNode(X86ISD::PSHUFHW, dl, MVT::v8i16,
Craig Topperdd637ae2012-02-19 05:41:45 +00005894 NewV.getOperand(0),
5895 getShufflePSHUFHWImmediate(SVOp), DAG);
5896 }
Nate Begemanb9a47b82009-02-23 08:49:38 +00005897 }
Eric Christopherfd179292009-08-27 18:07:15 +00005898
Nate Begemanb9a47b82009-02-23 08:49:38 +00005899 // In case BestHi & BestLo were both -1, which means each quadword has a word
5900 // from each of the four input quadwords, calculate the InOrder bitvector now
5901 // before falling through to the insert/extract cleanup.
5902 if (BestLoQuad == -1 && BestHiQuad == -1) {
5903 NewV = V1;
5904 for (int i = 0; i != 8; ++i)
5905 if (MaskVals[i] < 0 || MaskVals[i] == i)
5906 InOrder.set(i);
5907 }
Eric Christopherfd179292009-08-27 18:07:15 +00005908
Nate Begemanb9a47b82009-02-23 08:49:38 +00005909 // The other elements are put in the right place using pextrw and pinsrw.
5910 for (unsigned i = 0; i != 8; ++i) {
5911 if (InOrder[i])
5912 continue;
5913 int EltIdx = MaskVals[i];
5914 if (EltIdx < 0)
5915 continue;
Craig Topper6643d9c2012-05-04 06:18:33 +00005916 SDValue ExtOp = (EltIdx < 8) ?
5917 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, V1,
5918 DAG.getIntPtrConstant(EltIdx)) :
5919 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, V2,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005920 DAG.getIntPtrConstant(EltIdx - 8));
Owen Anderson825b72b2009-08-11 20:47:22 +00005921 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, ExtOp,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005922 DAG.getIntPtrConstant(i));
5923 }
5924 return NewV;
5925}
5926
5927// v16i8 shuffles - Prefer shuffles in the following order:
5928// 1. [ssse3] 1 x pshufb
5929// 2. [ssse3] 2 x pshufb + 1 x por
5930// 3. [all] v8i16 shuffle + N x pextrw + rotate + pinsrw
5931static
Nate Begeman9008ca62009-04-27 18:41:29 +00005932SDValue LowerVECTOR_SHUFFLEv16i8(ShuffleVectorSDNode *SVOp,
Dan Gohmand858e902010-04-17 15:26:15 +00005933 SelectionDAG &DAG,
5934 const X86TargetLowering &TLI) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005935 SDValue V1 = SVOp->getOperand(0);
5936 SDValue V2 = SVOp->getOperand(1);
5937 DebugLoc dl = SVOp->getDebugLoc();
Benjamin Kramered4c8c62012-01-15 13:16:05 +00005938 ArrayRef<int> MaskVals = SVOp->getMask();
Eric Christopherfd179292009-08-27 18:07:15 +00005939
Nate Begemanb9a47b82009-02-23 08:49:38 +00005940 // If we have SSSE3, case 1 is generated when all result bytes come from
Eric Christopherfd179292009-08-27 18:07:15 +00005941 // one of the inputs. Otherwise, case 2 is generated. If no SSSE3 is
Nate Begemanb9a47b82009-02-23 08:49:38 +00005942 // present, fall back to case 3.
Eric Christopherfd179292009-08-27 18:07:15 +00005943
Nate Begemanb9a47b82009-02-23 08:49:38 +00005944 // If SSSE3, use 1 pshufb instruction per vector with elements in the result.
Craig Topperd0a31172012-01-10 06:37:29 +00005945 if (TLI.getSubtarget()->hasSSSE3()) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005946 SmallVector<SDValue,16> pshufbMask;
Eric Christopherfd179292009-08-27 18:07:15 +00005947
Nate Begemanb9a47b82009-02-23 08:49:38 +00005948 // If all result elements are from one input vector, then only translate
Eric Christopherfd179292009-08-27 18:07:15 +00005949 // undef mask values to 0x80 (zero out result) in the pshufb mask.
Nate Begemanb9a47b82009-02-23 08:49:38 +00005950 //
5951 // Otherwise, we have elements from both input vectors, and must zero out
5952 // elements that come from V2 in the first mask, and V1 in the second mask
5953 // so that we can OR them together.
Nate Begemanb9a47b82009-02-23 08:49:38 +00005954 for (unsigned i = 0; i != 16; ++i) {
5955 int EltIdx = MaskVals[i];
Craig Topperb82b5ab2012-05-18 06:42:06 +00005956 if (EltIdx < 0 || EltIdx >= 16)
5957 EltIdx = 0x80;
Owen Anderson825b72b2009-08-11 20:47:22 +00005958 pshufbMask.push_back(DAG.getConstant(EltIdx, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005959 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005960 V1 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V1,
Evan Chenga87008d2009-02-25 22:49:59 +00005961 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005962 MVT::v16i8, &pshufbMask[0], 16));
Michael Liao265bcb12012-08-31 20:12:31 +00005963
5964 // As PSHUFB will zero elements with negative indices, it's safe to ignore
5965 // the 2nd operand if it's undefined or zero.
5966 if (V2.getOpcode() == ISD::UNDEF ||
5967 ISD::isBuildVectorAllZeros(V2.getNode()))
Nate Begemanb9a47b82009-02-23 08:49:38 +00005968 return V1;
Eric Christopherfd179292009-08-27 18:07:15 +00005969
Nate Begemanb9a47b82009-02-23 08:49:38 +00005970 // Calculate the shuffle mask for the second input, shuffle it, and
5971 // OR it with the first shuffled input.
5972 pshufbMask.clear();
5973 for (unsigned i = 0; i != 16; ++i) {
5974 int EltIdx = MaskVals[i];
Craig Topperb82b5ab2012-05-18 06:42:06 +00005975 EltIdx = (EltIdx < 16) ? 0x80 : EltIdx - 16;
Craig Topper85b9e562012-05-22 06:09:38 +00005976 pshufbMask.push_back(DAG.getConstant(EltIdx, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005977 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005978 V2 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V2,
Evan Chenga87008d2009-02-25 22:49:59 +00005979 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005980 MVT::v16i8, &pshufbMask[0], 16));
5981 return DAG.getNode(ISD::OR, dl, MVT::v16i8, V1, V2);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005982 }
Eric Christopherfd179292009-08-27 18:07:15 +00005983
Nate Begemanb9a47b82009-02-23 08:49:38 +00005984 // No SSSE3 - Calculate in place words and then fix all out of place words
5985 // With 0-16 extracts & inserts. Worst case is 16 bytes out of order from
5986 // the 16 different words that comprise the two doublequadword input vectors.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005987 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V1);
5988 V2 = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V2);
Craig Topperb82b5ab2012-05-18 06:42:06 +00005989 SDValue NewV = V1;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005990 for (int i = 0; i != 8; ++i) {
5991 int Elt0 = MaskVals[i*2];
5992 int Elt1 = MaskVals[i*2+1];
Eric Christopherfd179292009-08-27 18:07:15 +00005993
Nate Begemanb9a47b82009-02-23 08:49:38 +00005994 // This word of the result is all undef, skip it.
5995 if (Elt0 < 0 && Elt1 < 0)
5996 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00005997
Nate Begemanb9a47b82009-02-23 08:49:38 +00005998 // This word of the result is already in the correct place, skip it.
Craig Topperb82b5ab2012-05-18 06:42:06 +00005999 if ((Elt0 == i*2) && (Elt1 == i*2+1))
Nate Begemanb9a47b82009-02-23 08:49:38 +00006000 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00006001
Nate Begemanb9a47b82009-02-23 08:49:38 +00006002 SDValue Elt0Src = Elt0 < 16 ? V1 : V2;
6003 SDValue Elt1Src = Elt1 < 16 ? V1 : V2;
6004 SDValue InsElt;
Mon P Wang6b3ef692009-03-11 18:47:57 +00006005
6006 // If Elt0 and Elt1 are defined, are consecutive, and can be load
6007 // using a single extract together, load it and store it.
6008 if ((Elt0 >= 0) && ((Elt0 + 1) == Elt1) && ((Elt0 & 1) == 0)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00006009 InsElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, Elt1Src,
Mon P Wang6b3ef692009-03-11 18:47:57 +00006010 DAG.getIntPtrConstant(Elt1 / 2));
Owen Anderson825b72b2009-08-11 20:47:22 +00006011 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, InsElt,
Mon P Wang6b3ef692009-03-11 18:47:57 +00006012 DAG.getIntPtrConstant(i));
6013 continue;
6014 }
6015
Nate Begemanb9a47b82009-02-23 08:49:38 +00006016 // If Elt1 is defined, extract it from the appropriate source. If the
Mon P Wang6b3ef692009-03-11 18:47:57 +00006017 // source byte is not also odd, shift the extracted word left 8 bits
6018 // otherwise clear the bottom 8 bits if we need to do an or.
Nate Begemanb9a47b82009-02-23 08:49:38 +00006019 if (Elt1 >= 0) {
Owen Anderson825b72b2009-08-11 20:47:22 +00006020 InsElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, Elt1Src,
Nate Begemanb9a47b82009-02-23 08:49:38 +00006021 DAG.getIntPtrConstant(Elt1 / 2));
6022 if ((Elt1 & 1) == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00006023 InsElt = DAG.getNode(ISD::SHL, dl, MVT::i16, InsElt,
Owen Anderson95771af2011-02-25 21:41:48 +00006024 DAG.getConstant(8,
6025 TLI.getShiftAmountTy(InsElt.getValueType())));
Mon P Wang6b3ef692009-03-11 18:47:57 +00006026 else if (Elt0 >= 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00006027 InsElt = DAG.getNode(ISD::AND, dl, MVT::i16, InsElt,
6028 DAG.getConstant(0xFF00, MVT::i16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00006029 }
6030 // If Elt0 is defined, extract it from the appropriate source. If the
6031 // source byte is not also even, shift the extracted word right 8 bits. If
6032 // Elt1 was also defined, OR the extracted values together before
6033 // inserting them in the result.
6034 if (Elt0 >= 0) {
Owen Anderson825b72b2009-08-11 20:47:22 +00006035 SDValue InsElt0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16,
Nate Begemanb9a47b82009-02-23 08:49:38 +00006036 Elt0Src, DAG.getIntPtrConstant(Elt0 / 2));
6037 if ((Elt0 & 1) != 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00006038 InsElt0 = DAG.getNode(ISD::SRL, dl, MVT::i16, InsElt0,
Owen Anderson95771af2011-02-25 21:41:48 +00006039 DAG.getConstant(8,
6040 TLI.getShiftAmountTy(InsElt0.getValueType())));
Mon P Wang6b3ef692009-03-11 18:47:57 +00006041 else if (Elt1 >= 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00006042 InsElt0 = DAG.getNode(ISD::AND, dl, MVT::i16, InsElt0,
6043 DAG.getConstant(0x00FF, MVT::i16));
6044 InsElt = Elt1 >= 0 ? DAG.getNode(ISD::OR, dl, MVT::i16, InsElt, InsElt0)
Nate Begemanb9a47b82009-02-23 08:49:38 +00006045 : InsElt0;
6046 }
Owen Anderson825b72b2009-08-11 20:47:22 +00006047 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, InsElt,
Nate Begemanb9a47b82009-02-23 08:49:38 +00006048 DAG.getIntPtrConstant(i));
6049 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006050 return DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, NewV);
Evan Cheng14b32e12007-12-11 01:46:18 +00006051}
6052
Elena Demikhovsky41789462012-09-06 12:42:01 +00006053// v32i8 shuffles - Translate to VPSHUFB if possible.
6054static
6055SDValue LowerVECTOR_SHUFFLEv32i8(ShuffleVectorSDNode *SVOp,
Craig Topper55b24052012-09-11 06:15:32 +00006056 const X86Subtarget *Subtarget,
6057 SelectionDAG &DAG) {
Elena Demikhovsky41789462012-09-06 12:42:01 +00006058 EVT VT = SVOp->getValueType(0);
6059 SDValue V1 = SVOp->getOperand(0);
6060 SDValue V2 = SVOp->getOperand(1);
6061 DebugLoc dl = SVOp->getDebugLoc();
Elena Demikhovsky8100d242012-09-10 12:13:11 +00006062 SmallVector<int, 32> MaskVals(SVOp->getMask().begin(), SVOp->getMask().end());
Elena Demikhovsky41789462012-09-06 12:42:01 +00006063
6064 bool V2IsUndef = V2.getOpcode() == ISD::UNDEF;
Elena Demikhovsky8100d242012-09-10 12:13:11 +00006065 bool V1IsAllZero = ISD::isBuildVectorAllZeros(V1.getNode());
6066 bool V2IsAllZero = ISD::isBuildVectorAllZeros(V2.getNode());
Elena Demikhovsky41789462012-09-06 12:42:01 +00006067
Michael Liao471b9172012-10-03 23:43:52 +00006068 // VPSHUFB may be generated if
Elena Demikhovsky8100d242012-09-10 12:13:11 +00006069 // (1) one of input vector is undefined or zeroinitializer.
6070 // The mask value 0x80 puts 0 in the corresponding slot of the vector.
6071 // And (2) the mask indexes don't cross the 128-bit lane.
Craig Topper55b24052012-09-11 06:15:32 +00006072 if (VT != MVT::v32i8 || !Subtarget->hasAVX2() ||
Elena Demikhovsky8100d242012-09-10 12:13:11 +00006073 (!V2IsUndef && !V2IsAllZero && !V1IsAllZero))
Elena Demikhovsky41789462012-09-06 12:42:01 +00006074 return SDValue();
6075
Elena Demikhovsky8100d242012-09-10 12:13:11 +00006076 if (V1IsAllZero && !V2IsAllZero) {
6077 CommuteVectorShuffleMask(MaskVals, 32);
6078 V1 = V2;
6079 }
6080 SmallVector<SDValue, 32> pshufbMask;
Elena Demikhovsky41789462012-09-06 12:42:01 +00006081 for (unsigned i = 0; i != 32; i++) {
6082 int EltIdx = MaskVals[i];
6083 if (EltIdx < 0 || EltIdx >= 32)
6084 EltIdx = 0x80;
6085 else {
6086 if ((EltIdx >= 16 && i < 16) || (EltIdx < 16 && i >= 16))
6087 // Cross lane is not allowed.
6088 return SDValue();
6089 EltIdx &= 0xf;
6090 }
6091 pshufbMask.push_back(DAG.getConstant(EltIdx, MVT::i8));
6092 }
6093 return DAG.getNode(X86ISD::PSHUFB, dl, MVT::v32i8, V1,
6094 DAG.getNode(ISD::BUILD_VECTOR, dl,
6095 MVT::v32i8, &pshufbMask[0], 32));
6096}
6097
Evan Cheng7a831ce2007-12-15 03:00:47 +00006098/// RewriteAsNarrowerShuffle - Try rewriting v8i16 and v16i8 shuffles as 4 wide
Bruno Cardoso Lopes0a7dd4f2010-09-08 18:12:31 +00006099/// ones, or rewriting v4i32 / v4f32 as 2 wide ones if possible. This can be
Evan Cheng7a831ce2007-12-15 03:00:47 +00006100/// done when every pair / quad of shuffle mask elements point to elements in
6101/// the right sequence. e.g.
Bruno Cardoso Lopes0a7dd4f2010-09-08 18:12:31 +00006102/// vector_shuffle X, Y, <2, 3, | 10, 11, | 0, 1, | 14, 15>
Evan Cheng14b32e12007-12-11 01:46:18 +00006103static
Nate Begeman9008ca62009-04-27 18:41:29 +00006104SDValue RewriteAsNarrowerShuffle(ShuffleVectorSDNode *SVOp,
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006105 SelectionDAG &DAG, DebugLoc dl) {
Craig Topper11ac1f82012-05-04 04:08:44 +00006106 MVT VT = SVOp->getValueType(0).getSimpleVT();
Nate Begeman9008ca62009-04-27 18:41:29 +00006107 unsigned NumElems = VT.getVectorNumElements();
Craig Topper11ac1f82012-05-04 04:08:44 +00006108 MVT NewVT;
6109 unsigned Scale;
6110 switch (VT.SimpleTy) {
Craig Topperabb94d02012-02-05 03:43:23 +00006111 default: llvm_unreachable("Unexpected!");
Craig Topperf3640d72012-05-04 04:44:49 +00006112 case MVT::v4f32: NewVT = MVT::v2f64; Scale = 2; break;
6113 case MVT::v4i32: NewVT = MVT::v2i64; Scale = 2; break;
6114 case MVT::v8i16: NewVT = MVT::v4i32; Scale = 2; break;
6115 case MVT::v16i8: NewVT = MVT::v4i32; Scale = 4; break;
6116 case MVT::v16i16: NewVT = MVT::v8i32; Scale = 2; break;
6117 case MVT::v32i8: NewVT = MVT::v8i32; Scale = 4; break;
Evan Cheng7a831ce2007-12-15 03:00:47 +00006118 }
6119
Nate Begeman9008ca62009-04-27 18:41:29 +00006120 SmallVector<int, 8> MaskVec;
Craig Topper11ac1f82012-05-04 04:08:44 +00006121 for (unsigned i = 0; i != NumElems; i += Scale) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006122 int StartIdx = -1;
Craig Topper11ac1f82012-05-04 04:08:44 +00006123 for (unsigned j = 0; j != Scale; ++j) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006124 int EltIdx = SVOp->getMaskElt(i+j);
6125 if (EltIdx < 0)
Evan Cheng14b32e12007-12-11 01:46:18 +00006126 continue;
Craig Topper11ac1f82012-05-04 04:08:44 +00006127 if (StartIdx < 0)
6128 StartIdx = (EltIdx / Scale);
6129 if (EltIdx != (int)(StartIdx*Scale + j))
Dan Gohman475871a2008-07-27 21:46:04 +00006130 return SDValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00006131 }
Craig Topper11ac1f82012-05-04 04:08:44 +00006132 MaskVec.push_back(StartIdx);
Evan Cheng8a86c3f2007-12-07 08:07:39 +00006133 }
6134
Craig Topper11ac1f82012-05-04 04:08:44 +00006135 SDValue V1 = DAG.getNode(ISD::BITCAST, dl, NewVT, SVOp->getOperand(0));
6136 SDValue V2 = DAG.getNode(ISD::BITCAST, dl, NewVT, SVOp->getOperand(1));
Nate Begeman9008ca62009-04-27 18:41:29 +00006137 return DAG.getVectorShuffle(NewVT, dl, V1, V2, &MaskVec[0]);
Evan Cheng8a86c3f2007-12-07 08:07:39 +00006138}
6139
Evan Chengd880b972008-05-09 21:53:03 +00006140/// getVZextMovL - Return a zero-extending vector move low node.
Evan Cheng7e2ff772008-05-08 00:57:18 +00006141///
Owen Andersone50ed302009-08-10 22:56:29 +00006142static SDValue getVZextMovL(EVT VT, EVT OpVT,
Nate Begeman9008ca62009-04-27 18:41:29 +00006143 SDValue SrcOp, SelectionDAG &DAG,
6144 const X86Subtarget *Subtarget, DebugLoc dl) {
Owen Anderson825b72b2009-08-11 20:47:22 +00006145 if (VT == MVT::v2f64 || VT == MVT::v4f32) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00006146 LoadSDNode *LD = NULL;
Gabor Greifba36cb52008-08-28 21:40:38 +00006147 if (!isScalarLoadToVector(SrcOp.getNode(), &LD))
Evan Cheng7e2ff772008-05-08 00:57:18 +00006148 LD = dyn_cast<LoadSDNode>(SrcOp);
6149 if (!LD) {
6150 // movssrr and movsdrr do not clear top bits. Try to use movd, movq
6151 // instead.
Owen Anderson766b5ef2009-08-11 21:59:30 +00006152 MVT ExtVT = (OpVT == MVT::v2f64) ? MVT::i64 : MVT::i32;
Duncan Sandscdfad362010-11-03 12:17:33 +00006153 if ((ExtVT != MVT::i64 || Subtarget->is64Bit()) &&
Evan Cheng7e2ff772008-05-08 00:57:18 +00006154 SrcOp.getOpcode() == ISD::SCALAR_TO_VECTOR &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006155 SrcOp.getOperand(0).getOpcode() == ISD::BITCAST &&
Owen Anderson766b5ef2009-08-11 21:59:30 +00006156 SrcOp.getOperand(0).getOperand(0).getValueType() == ExtVT) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00006157 // PR2108
Owen Anderson825b72b2009-08-11 20:47:22 +00006158 OpVT = (OpVT == MVT::v2f64) ? MVT::v2i64 : MVT::v4i32;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006159 return DAG.getNode(ISD::BITCAST, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00006160 DAG.getNode(X86ISD::VZEXT_MOVL, dl, OpVT,
6161 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
6162 OpVT,
Gabor Greif327ef032008-08-28 23:19:51 +00006163 SrcOp.getOperand(0)
6164 .getOperand(0))));
Evan Cheng7e2ff772008-05-08 00:57:18 +00006165 }
6166 }
6167 }
6168
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006169 return DAG.getNode(ISD::BITCAST, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00006170 DAG.getNode(X86ISD::VZEXT_MOVL, dl, OpVT,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006171 DAG.getNode(ISD::BITCAST, dl,
Dale Johannesenace16102009-02-03 19:33:06 +00006172 OpVT, SrcOp)));
Evan Cheng7e2ff772008-05-08 00:57:18 +00006173}
6174
Bruno Cardoso Lopesd0888342011-07-22 00:14:56 +00006175/// LowerVECTOR_SHUFFLE_256 - Handle all 256-bit wide vectors shuffles
6176/// which could not be matched by any known target speficic shuffle
6177static SDValue
6178LowerVECTOR_SHUFFLE_256(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG) {
Elena Demikhovsky15963732012-06-26 08:04:10 +00006179
6180 SDValue NewOp = Compact8x32ShuffleNode(SVOp, DAG);
6181 if (NewOp.getNode())
6182 return NewOp;
6183
Craig Topper8f35c132012-01-20 09:29:03 +00006184 EVT VT = SVOp->getValueType(0);
Bruno Cardoso Lopes3b865982011-08-16 18:21:54 +00006185
Craig Topper8f35c132012-01-20 09:29:03 +00006186 unsigned NumElems = VT.getVectorNumElements();
6187 unsigned NumLaneElems = NumElems / 2;
6188
Craig Topper8f35c132012-01-20 09:29:03 +00006189 DebugLoc dl = SVOp->getDebugLoc();
6190 MVT EltVT = VT.getVectorElementType().getSimpleVT();
Craig Topper9a2b6e12012-04-06 07:45:23 +00006191 EVT NVT = MVT::getVectorVT(EltVT, NumLaneElems);
Craig Topper8ae97ba2012-05-21 06:40:16 +00006192 SDValue Output[2];
Craig Topper8f35c132012-01-20 09:29:03 +00006193
Craig Topper9a2b6e12012-04-06 07:45:23 +00006194 SmallVector<int, 16> Mask;
Craig Topper8f35c132012-01-20 09:29:03 +00006195 for (unsigned l = 0; l < 2; ++l) {
Craig Topper9a2b6e12012-04-06 07:45:23 +00006196 // Build a shuffle mask for the output, discovering on the fly which
6197 // input vectors to use as shuffle operands (recorded in InputUsed).
6198 // If building a suitable shuffle vector proves too hard, then bail
Craig Topper8ae97ba2012-05-21 06:40:16 +00006199 // out with UseBuildVector set.
6200 bool UseBuildVector = false;
Benjamin Kramer9e5512a2012-04-06 13:33:52 +00006201 int InputUsed[2] = { -1, -1 }; // Not yet discovered.
Craig Topper9a2b6e12012-04-06 07:45:23 +00006202 unsigned LaneStart = l * NumLaneElems;
6203 for (unsigned i = 0; i != NumLaneElems; ++i) {
6204 // The mask element. This indexes into the input.
6205 int Idx = SVOp->getMaskElt(i+LaneStart);
6206 if (Idx < 0) {
6207 // the mask element does not index into any input vector.
6208 Mask.push_back(-1);
6209 continue;
6210 }
Craig Topper8f35c132012-01-20 09:29:03 +00006211
Craig Topper9a2b6e12012-04-06 07:45:23 +00006212 // The input vector this mask element indexes into.
6213 int Input = Idx / NumLaneElems;
Craig Topper8f35c132012-01-20 09:29:03 +00006214
Craig Topper9a2b6e12012-04-06 07:45:23 +00006215 // Turn the index into an offset from the start of the input vector.
6216 Idx -= Input * NumLaneElems;
6217
6218 // Find or create a shuffle vector operand to hold this input.
6219 unsigned OpNo;
6220 for (OpNo = 0; OpNo < array_lengthof(InputUsed); ++OpNo) {
6221 if (InputUsed[OpNo] == Input)
6222 // This input vector is already an operand.
6223 break;
6224 if (InputUsed[OpNo] < 0) {
6225 // Create a new operand for this input vector.
6226 InputUsed[OpNo] = Input;
6227 break;
6228 }
6229 }
6230
6231 if (OpNo >= array_lengthof(InputUsed)) {
Craig Topper8ae97ba2012-05-21 06:40:16 +00006232 // More than two input vectors used! Give up on trying to create a
6233 // shuffle vector. Insert all elements into a BUILD_VECTOR instead.
6234 UseBuildVector = true;
6235 break;
Craig Topper9a2b6e12012-04-06 07:45:23 +00006236 }
6237
6238 // Add the mask index for the new shuffle vector.
6239 Mask.push_back(Idx + OpNo * NumLaneElems);
6240 }
6241
Craig Topper8ae97ba2012-05-21 06:40:16 +00006242 if (UseBuildVector) {
6243 SmallVector<SDValue, 16> SVOps;
6244 for (unsigned i = 0; i != NumLaneElems; ++i) {
6245 // The mask element. This indexes into the input.
6246 int Idx = SVOp->getMaskElt(i+LaneStart);
6247 if (Idx < 0) {
6248 SVOps.push_back(DAG.getUNDEF(EltVT));
6249 continue;
6250 }
6251
6252 // The input vector this mask element indexes into.
6253 int Input = Idx / NumElems;
6254
6255 // Turn the index into an offset from the start of the input vector.
6256 Idx -= Input * NumElems;
6257
6258 // Extract the vector element by hand.
6259 SVOps.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EltVT,
6260 SVOp->getOperand(Input),
6261 DAG.getIntPtrConstant(Idx)));
6262 }
6263
6264 // Construct the output using a BUILD_VECTOR.
6265 Output[l] = DAG.getNode(ISD::BUILD_VECTOR, dl, NVT, &SVOps[0],
6266 SVOps.size());
6267 } else if (InputUsed[0] < 0) {
Craig Topper9a2b6e12012-04-06 07:45:23 +00006268 // No input vectors were used! The result is undefined.
Craig Topper8ae97ba2012-05-21 06:40:16 +00006269 Output[l] = DAG.getUNDEF(NVT);
Craig Topper9a2b6e12012-04-06 07:45:23 +00006270 } else {
6271 SDValue Op0 = Extract128BitVector(SVOp->getOperand(InputUsed[0] / 2),
Craig Topperb14940a2012-04-22 20:55:18 +00006272 (InputUsed[0] % 2) * NumLaneElems,
6273 DAG, dl);
Craig Topper9a2b6e12012-04-06 07:45:23 +00006274 // If only one input was used, use an undefined vector for the other.
6275 SDValue Op1 = (InputUsed[1] < 0) ? DAG.getUNDEF(NVT) :
6276 Extract128BitVector(SVOp->getOperand(InputUsed[1] / 2),
Craig Topperb14940a2012-04-22 20:55:18 +00006277 (InputUsed[1] % 2) * NumLaneElems, DAG, dl);
Craig Topper9a2b6e12012-04-06 07:45:23 +00006278 // At least one input vector was used. Create a new shuffle vector.
Craig Topper8ae97ba2012-05-21 06:40:16 +00006279 Output[l] = DAG.getVectorShuffle(NVT, dl, Op0, Op1, &Mask[0]);
Craig Topper9a2b6e12012-04-06 07:45:23 +00006280 }
6281
6282 Mask.clear();
6283 }
Craig Topper8f35c132012-01-20 09:29:03 +00006284
6285 // Concatenate the result back
Craig Topper8ae97ba2012-05-21 06:40:16 +00006286 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT, Output[0], Output[1]);
Bruno Cardoso Lopesd0888342011-07-22 00:14:56 +00006287}
6288
Bruno Cardoso Lopes589b8972011-07-22 00:14:53 +00006289/// LowerVECTOR_SHUFFLE_128v4 - Handle all 128-bit wide vectors with
6290/// 4 elements, and match them with several different shuffle types.
Dan Gohman475871a2008-07-27 21:46:04 +00006291static SDValue
Bruno Cardoso Lopes589b8972011-07-22 00:14:53 +00006292LowerVECTOR_SHUFFLE_128v4(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006293 SDValue V1 = SVOp->getOperand(0);
6294 SDValue V2 = SVOp->getOperand(1);
6295 DebugLoc dl = SVOp->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00006296 EVT VT = SVOp->getValueType(0);
Eric Christopherfd179292009-08-27 18:07:15 +00006297
Craig Topper7a9a28b2012-08-12 02:23:29 +00006298 assert(VT.is128BitVector() && "Unsupported vector size");
Bruno Cardoso Lopes589b8972011-07-22 00:14:53 +00006299
Benjamin Kramer9c683542012-01-30 15:16:21 +00006300 std::pair<int, int> Locs[4];
6301 int Mask1[] = { -1, -1, -1, -1 };
Benjamin Kramered4c8c62012-01-15 13:16:05 +00006302 SmallVector<int, 8> PermMask(SVOp->getMask().begin(), SVOp->getMask().end());
Nate Begeman9008ca62009-04-27 18:41:29 +00006303
Evan Chengace3c172008-07-22 21:13:36 +00006304 unsigned NumHi = 0;
6305 unsigned NumLo = 0;
Evan Chengace3c172008-07-22 21:13:36 +00006306 for (unsigned i = 0; i != 4; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006307 int Idx = PermMask[i];
6308 if (Idx < 0) {
Evan Chengace3c172008-07-22 21:13:36 +00006309 Locs[i] = std::make_pair(-1, -1);
6310 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00006311 assert(Idx < 8 && "Invalid VECTOR_SHUFFLE index!");
6312 if (Idx < 4) {
Evan Chengace3c172008-07-22 21:13:36 +00006313 Locs[i] = std::make_pair(0, NumLo);
Nate Begeman9008ca62009-04-27 18:41:29 +00006314 Mask1[NumLo] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00006315 NumLo++;
6316 } else {
6317 Locs[i] = std::make_pair(1, NumHi);
6318 if (2+NumHi < 4)
Nate Begeman9008ca62009-04-27 18:41:29 +00006319 Mask1[2+NumHi] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00006320 NumHi++;
6321 }
6322 }
6323 }
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006324
Evan Chengace3c172008-07-22 21:13:36 +00006325 if (NumLo <= 2 && NumHi <= 2) {
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006326 // If no more than two elements come from either vector. This can be
6327 // implemented with two shuffles. First shuffle gather the elements.
6328 // The second shuffle, which takes the first shuffle as both of its
6329 // vector operands, put the elements into the right order.
Nate Begeman9008ca62009-04-27 18:41:29 +00006330 V1 = DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006331
Benjamin Kramer9c683542012-01-30 15:16:21 +00006332 int Mask2[] = { -1, -1, -1, -1 };
Eric Christopherfd179292009-08-27 18:07:15 +00006333
Benjamin Kramer9c683542012-01-30 15:16:21 +00006334 for (unsigned i = 0; i != 4; ++i)
6335 if (Locs[i].first != -1) {
Evan Chengace3c172008-07-22 21:13:36 +00006336 unsigned Idx = (i < 2) ? 0 : 4;
6337 Idx += Locs[i].first * 2 + Locs[i].second;
Nate Begeman9008ca62009-04-27 18:41:29 +00006338 Mask2[i] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00006339 }
Evan Chengace3c172008-07-22 21:13:36 +00006340
Nate Begeman9008ca62009-04-27 18:41:29 +00006341 return DAG.getVectorShuffle(VT, dl, V1, V1, &Mask2[0]);
Craig Topper69947b92012-04-23 06:57:04 +00006342 }
6343
6344 if (NumLo == 3 || NumHi == 3) {
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006345 // Otherwise, we must have three elements from one vector, call it X, and
6346 // one element from the other, call it Y. First, use a shufps to build an
6347 // intermediate vector with the one element from Y and the element from X
6348 // that will be in the same half in the final destination (the indexes don't
6349 // matter). Then, use a shufps to build the final vector, taking the half
6350 // containing the element from Y from the intermediate, and the other half
6351 // from X.
6352 if (NumHi == 3) {
6353 // Normalize it so the 3 elements come from V1.
Craig Topperbeabc6c2011-12-05 06:56:46 +00006354 CommuteVectorShuffleMask(PermMask, 4);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006355 std::swap(V1, V2);
6356 }
6357
6358 // Find the element from V2.
6359 unsigned HiIndex;
6360 for (HiIndex = 0; HiIndex < 3; ++HiIndex) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006361 int Val = PermMask[HiIndex];
6362 if (Val < 0)
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006363 continue;
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006364 if (Val >= 4)
6365 break;
6366 }
6367
Nate Begeman9008ca62009-04-27 18:41:29 +00006368 Mask1[0] = PermMask[HiIndex];
6369 Mask1[1] = -1;
6370 Mask1[2] = PermMask[HiIndex^1];
6371 Mask1[3] = -1;
6372 V2 = DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006373
6374 if (HiIndex >= 2) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006375 Mask1[0] = PermMask[0];
6376 Mask1[1] = PermMask[1];
6377 Mask1[2] = HiIndex & 1 ? 6 : 4;
6378 Mask1[3] = HiIndex & 1 ? 4 : 6;
6379 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006380 }
Craig Topper69947b92012-04-23 06:57:04 +00006381
6382 Mask1[0] = HiIndex & 1 ? 2 : 0;
6383 Mask1[1] = HiIndex & 1 ? 0 : 2;
6384 Mask1[2] = PermMask[2];
6385 Mask1[3] = PermMask[3];
6386 if (Mask1[2] >= 0)
6387 Mask1[2] += 4;
6388 if (Mask1[3] >= 0)
6389 Mask1[3] += 4;
6390 return DAG.getVectorShuffle(VT, dl, V2, V1, &Mask1[0]);
Evan Chengace3c172008-07-22 21:13:36 +00006391 }
6392
6393 // Break it into (shuffle shuffle_hi, shuffle_lo).
Benjamin Kramer9c683542012-01-30 15:16:21 +00006394 int LoMask[] = { -1, -1, -1, -1 };
6395 int HiMask[] = { -1, -1, -1, -1 };
Nate Begeman9008ca62009-04-27 18:41:29 +00006396
Benjamin Kramer9c683542012-01-30 15:16:21 +00006397 int *MaskPtr = LoMask;
Evan Chengace3c172008-07-22 21:13:36 +00006398 unsigned MaskIdx = 0;
6399 unsigned LoIdx = 0;
6400 unsigned HiIdx = 2;
6401 for (unsigned i = 0; i != 4; ++i) {
6402 if (i == 2) {
Benjamin Kramer9c683542012-01-30 15:16:21 +00006403 MaskPtr = HiMask;
Evan Chengace3c172008-07-22 21:13:36 +00006404 MaskIdx = 1;
6405 LoIdx = 0;
6406 HiIdx = 2;
6407 }
Nate Begeman9008ca62009-04-27 18:41:29 +00006408 int Idx = PermMask[i];
6409 if (Idx < 0) {
Evan Chengace3c172008-07-22 21:13:36 +00006410 Locs[i] = std::make_pair(-1, -1);
Nate Begeman9008ca62009-04-27 18:41:29 +00006411 } else if (Idx < 4) {
Evan Chengace3c172008-07-22 21:13:36 +00006412 Locs[i] = std::make_pair(MaskIdx, LoIdx);
Benjamin Kramer9c683542012-01-30 15:16:21 +00006413 MaskPtr[LoIdx] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00006414 LoIdx++;
6415 } else {
6416 Locs[i] = std::make_pair(MaskIdx, HiIdx);
Benjamin Kramer9c683542012-01-30 15:16:21 +00006417 MaskPtr[HiIdx] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00006418 HiIdx++;
6419 }
6420 }
6421
Nate Begeman9008ca62009-04-27 18:41:29 +00006422 SDValue LoShuffle = DAG.getVectorShuffle(VT, dl, V1, V2, &LoMask[0]);
6423 SDValue HiShuffle = DAG.getVectorShuffle(VT, dl, V1, V2, &HiMask[0]);
Benjamin Kramer9c683542012-01-30 15:16:21 +00006424 int MaskOps[] = { -1, -1, -1, -1 };
6425 for (unsigned i = 0; i != 4; ++i)
6426 if (Locs[i].first != -1)
6427 MaskOps[i] = Locs[i].first * 4 + Locs[i].second;
Nate Begeman9008ca62009-04-27 18:41:29 +00006428 return DAG.getVectorShuffle(VT, dl, LoShuffle, HiShuffle, &MaskOps[0]);
Evan Chengace3c172008-07-22 21:13:36 +00006429}
6430
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00006431static bool MayFoldVectorLoad(SDValue V) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006432 if (V.hasOneUse() && V.getOpcode() == ISD::BITCAST)
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00006433 V = V.getOperand(0);
6434 if (V.hasOneUse() && V.getOpcode() == ISD::SCALAR_TO_VECTOR)
6435 V = V.getOperand(0);
Evan Cheng7bc389b2011-11-08 00:31:58 +00006436 if (V.hasOneUse() && V.getOpcode() == ISD::BUILD_VECTOR &&
6437 V.getNumOperands() == 2 && V.getOperand(1).getOpcode() == ISD::UNDEF)
6438 // BUILD_VECTOR (load), undef
6439 V = V.getOperand(0);
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00006440 if (MayFoldLoad(V))
6441 return true;
6442 return false;
6443}
6444
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006445// FIXME: the version above should always be used. Since there's
6446// a bug where several vector shuffles can't be folded because the
6447// DAG is not updated during lowering and a node claims to have two
6448// uses while it only has one, use this version, and let isel match
6449// another instruction if the load really happens to have more than
6450// one use. Remove this version after this bug get fixed.
Evan Cheng835580f2010-10-07 20:50:20 +00006451// rdar://8434668, PR8156
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006452static bool RelaxedMayFoldVectorLoad(SDValue V) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006453 if (V.hasOneUse() && V.getOpcode() == ISD::BITCAST)
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006454 V = V.getOperand(0);
6455 if (V.hasOneUse() && V.getOpcode() == ISD::SCALAR_TO_VECTOR)
6456 V = V.getOperand(0);
6457 if (ISD::isNormalLoad(V.getNode()))
6458 return true;
6459 return false;
6460}
6461
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006462static
Evan Cheng835580f2010-10-07 20:50:20 +00006463SDValue getMOVDDup(SDValue &Op, DebugLoc &dl, SDValue V1, SelectionDAG &DAG) {
6464 EVT VT = Op.getValueType();
6465
6466 // Canonizalize to v2f64.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006467 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, V1);
6468 return DAG.getNode(ISD::BITCAST, dl, VT,
Evan Cheng835580f2010-10-07 20:50:20 +00006469 getTargetShuffleNode(X86ISD::MOVDDUP, dl, MVT::v2f64,
6470 V1, DAG));
6471}
6472
6473static
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006474SDValue getMOVLowToHigh(SDValue &Op, DebugLoc &dl, SelectionDAG &DAG,
Craig Topper1accb7e2012-01-10 06:54:16 +00006475 bool HasSSE2) {
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006476 SDValue V1 = Op.getOperand(0);
6477 SDValue V2 = Op.getOperand(1);
6478 EVT VT = Op.getValueType();
6479
6480 assert(VT != MVT::v2i64 && "unsupported shuffle type");
6481
Craig Topper1accb7e2012-01-10 06:54:16 +00006482 if (HasSSE2 && VT == MVT::v2f64)
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006483 return getTargetShuffleNode(X86ISD::MOVLHPD, dl, VT, V1, V2, DAG);
6484
Evan Cheng0899f5c2011-08-31 02:05:24 +00006485 // v4f32 or v4i32: canonizalized to v4f32 (which is legal for SSE1)
6486 return DAG.getNode(ISD::BITCAST, dl, VT,
6487 getTargetShuffleNode(X86ISD::MOVLHPS, dl, MVT::v4f32,
6488 DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, V1),
6489 DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, V2), DAG));
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006490}
6491
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00006492static
6493SDValue getMOVHighToLow(SDValue &Op, DebugLoc &dl, SelectionDAG &DAG) {
6494 SDValue V1 = Op.getOperand(0);
6495 SDValue V2 = Op.getOperand(1);
6496 EVT VT = Op.getValueType();
6497
6498 assert((VT == MVT::v4i32 || VT == MVT::v4f32) &&
6499 "unsupported shuffle type");
6500
6501 if (V2.getOpcode() == ISD::UNDEF)
6502 V2 = V1;
6503
6504 // v4i32 or v4f32
6505 return getTargetShuffleNode(X86ISD::MOVHLPS, dl, VT, V1, V2, DAG);
6506}
6507
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006508static
Craig Topper1accb7e2012-01-10 06:54:16 +00006509SDValue getMOVLP(SDValue &Op, DebugLoc &dl, SelectionDAG &DAG, bool HasSSE2) {
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006510 SDValue V1 = Op.getOperand(0);
6511 SDValue V2 = Op.getOperand(1);
6512 EVT VT = Op.getValueType();
6513 unsigned NumElems = VT.getVectorNumElements();
6514
6515 // Use MOVLPS and MOVLPD in case V1 or V2 are loads. During isel, the second
6516 // operand of these instructions is only memory, so check if there's a
6517 // potencial load folding here, otherwise use SHUFPS or MOVSD to match the
6518 // same masks.
6519 bool CanFoldLoad = false;
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006520
Bruno Cardoso Lopesd00bfe12010-09-02 02:35:51 +00006521 // Trivial case, when V2 comes from a load.
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00006522 if (MayFoldVectorLoad(V2))
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006523 CanFoldLoad = true;
6524
6525 // When V1 is a load, it can be folded later into a store in isel, example:
6526 // (store (v4f32 (X86Movlps (load addr:$src1), VR128:$src2)), addr:$src1)
6527 // turns into:
6528 // (MOVLPSmr addr:$src1, VR128:$src2)
6529 // So, recognize this potential and also use MOVLPS or MOVLPD
Evan Cheng7bc389b2011-11-08 00:31:58 +00006530 else if (MayFoldVectorLoad(V1) && MayFoldIntoStore(Op))
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006531 CanFoldLoad = true;
6532
Dan Gohman65fd6562011-11-03 21:49:52 +00006533 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006534 if (CanFoldLoad) {
Craig Topper1accb7e2012-01-10 06:54:16 +00006535 if (HasSSE2 && NumElems == 2)
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006536 return getTargetShuffleNode(X86ISD::MOVLPD, dl, VT, V1, V2, DAG);
6537
6538 if (NumElems == 4)
Benjamin Kramerd9b0b022012-06-02 10:20:22 +00006539 // If we don't care about the second element, proceed to use movss.
Dan Gohman65fd6562011-11-03 21:49:52 +00006540 if (SVOp->getMaskElt(1) != -1)
6541 return getTargetShuffleNode(X86ISD::MOVLPS, dl, VT, V1, V2, DAG);
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006542 }
6543
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006544 // movl and movlp will both match v2i64, but v2i64 is never matched by
6545 // movl earlier because we make it strict to avoid messing with the movlp load
6546 // folding logic (see the code above getMOVLP call). Match it here then,
6547 // this is horrible, but will stay like this until we move all shuffle
6548 // matching to x86 specific nodes. Note that for the 1st condition all
6549 // types are matched with movsd.
Craig Topper1accb7e2012-01-10 06:54:16 +00006550 if (HasSSE2) {
Bruno Cardoso Lopes5ca0d142011-09-14 02:36:14 +00006551 // FIXME: isMOVLMask should be checked and matched before getMOVLP,
6552 // as to remove this logic from here, as much as possible
Craig Topper5aaffa82012-02-19 02:53:47 +00006553 if (NumElems == 2 || !isMOVLMask(SVOp->getMask(), VT))
Bruno Cardoso Lopes57d6a5e2011-08-31 03:04:20 +00006554 return getTargetShuffleNode(X86ISD::MOVSD, dl, VT, V1, V2, DAG);
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006555 return getTargetShuffleNode(X86ISD::MOVSS, dl, VT, V1, V2, DAG);
Bruno Cardoso Lopes57d6a5e2011-08-31 03:04:20 +00006556 }
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006557
6558 assert(VT != MVT::v4i32 && "unsupported shuffle type");
6559
6560 // Invert the operand order and use SHUFPS to match it.
Craig Topperb3982da2011-12-31 23:50:21 +00006561 return getTargetShuffleNode(X86ISD::SHUFP, dl, VT, V2, V1,
Craig Topper5aaffa82012-02-19 02:53:47 +00006562 getShuffleSHUFImmediate(SVOp), DAG);
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006563}
6564
Nadav Rotem154819d2012-04-09 07:45:58 +00006565SDValue
6566X86TargetLowering::NormalizeVectorShuffle(SDValue Op, SelectionDAG &DAG) const {
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006567 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
6568 EVT VT = Op.getValueType();
6569 DebugLoc dl = Op.getDebugLoc();
6570 SDValue V1 = Op.getOperand(0);
6571 SDValue V2 = Op.getOperand(1);
6572
6573 if (isZeroShuffle(SVOp))
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00006574 return getZeroVector(VT, Subtarget, DAG, dl);
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006575
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006576 // Handle splat operations
6577 if (SVOp->isSplat()) {
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00006578 unsigned NumElem = VT.getVectorNumElements();
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00006579 int Size = VT.getSizeInBits();
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006580
Bruno Cardoso Lopes0e6d2302011-08-17 02:29:19 +00006581 // Use vbroadcast whenever the splat comes from a foldable load
Nadav Rotem154819d2012-04-09 07:45:58 +00006582 SDValue Broadcast = LowerVectorBroadcast(Op, DAG);
Nadav Rotem9d68b062012-04-08 12:54:54 +00006583 if (Broadcast.getNode())
6584 return Broadcast;
Bruno Cardoso Lopes0e6d2302011-08-17 02:29:19 +00006585
Bruno Cardoso Lopes6a32adc2011-07-25 23:05:25 +00006586 // Handle splats by matching through known shuffle masks
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00006587 if ((Size == 128 && NumElem <= 4) ||
6588 (Size == 256 && NumElem < 8))
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006589 return SDValue();
6590
Bruno Cardoso Lopes8a5b2622011-08-17 02:29:13 +00006591 // All remaning splats are promoted to target supported vector shuffles.
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006592 return PromoteSplat(SVOp, DAG);
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006593 }
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006594
6595 // If the shuffle can be profitably rewritten as a narrower shuffle, then
6596 // do it!
Craig Topperf3640d72012-05-04 04:44:49 +00006597 if (VT == MVT::v8i16 || VT == MVT::v16i8 ||
6598 VT == MVT::v16i16 || VT == MVT::v32i8) {
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006599 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, dl);
6600 if (NewOp.getNode())
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006601 return DAG.getNode(ISD::BITCAST, dl, VT, NewOp);
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006602 } else if ((VT == MVT::v4i32 ||
Craig Topper1accb7e2012-01-10 06:54:16 +00006603 (VT == MVT::v4f32 && Subtarget->hasSSE2()))) {
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006604 // FIXME: Figure out a cleaner way to do this.
6605 // Try to make use of movq to zero out the top part.
6606 if (ISD::isBuildVectorAllZeros(V2.getNode())) {
6607 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, dl);
6608 if (NewOp.getNode()) {
Craig Topper5aaffa82012-02-19 02:53:47 +00006609 EVT NewVT = NewOp.getValueType();
6610 if (isCommutedMOVLMask(cast<ShuffleVectorSDNode>(NewOp)->getMask(),
6611 NewVT, true, false))
6612 return getVZextMovL(VT, NewVT, NewOp.getOperand(0),
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006613 DAG, Subtarget, dl);
6614 }
6615 } else if (ISD::isBuildVectorAllZeros(V1.getNode())) {
6616 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, dl);
Craig Topper5aaffa82012-02-19 02:53:47 +00006617 if (NewOp.getNode()) {
6618 EVT NewVT = NewOp.getValueType();
6619 if (isMOVLMask(cast<ShuffleVectorSDNode>(NewOp)->getMask(), NewVT))
6620 return getVZextMovL(VT, NewVT, NewOp.getOperand(1),
6621 DAG, Subtarget, dl);
6622 }
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006623 }
6624 }
6625 return SDValue();
6626}
6627
Dan Gohman475871a2008-07-27 21:46:04 +00006628SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006629X86TargetLowering::LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) const {
Nate Begeman9008ca62009-04-27 18:41:29 +00006630 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Dan Gohman475871a2008-07-27 21:46:04 +00006631 SDValue V1 = Op.getOperand(0);
6632 SDValue V2 = Op.getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +00006633 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006634 DebugLoc dl = Op.getDebugLoc();
Nate Begeman9008ca62009-04-27 18:41:29 +00006635 unsigned NumElems = VT.getVectorNumElements();
Elena Demikhovsky16db7102012-01-12 20:33:10 +00006636 bool V1IsUndef = V1.getOpcode() == ISD::UNDEF;
Evan Cheng0db9fe62006-04-25 20:13:52 +00006637 bool V2IsUndef = V2.getOpcode() == ISD::UNDEF;
Evan Chengd9b8e402006-10-16 06:36:00 +00006638 bool V1IsSplat = false;
6639 bool V2IsSplat = false;
Craig Topper1accb7e2012-01-10 06:54:16 +00006640 bool HasSSE2 = Subtarget->hasSSE2();
Craig Topperbeabc6c2011-12-05 06:56:46 +00006641 bool HasAVX = Subtarget->hasAVX();
Craig Topper6347e862011-11-21 06:57:39 +00006642 bool HasAVX2 = Subtarget->hasAVX2();
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006643 MachineFunction &MF = DAG.getMachineFunction();
Bill Wendling67658342012-10-09 07:45:08 +00006644 bool OptForSize = MF.getFunction()->getFnAttributes().
6645 hasAttribute(Attributes::OptimizeForSize);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006646
Craig Topper3426a3e2011-11-14 06:46:21 +00006647 assert(VT.getSizeInBits() != 64 && "Can't lower MMX shuffles");
Bruno Cardoso Lopes58277b12010-09-07 18:41:45 +00006648
Elena Demikhovsky16db7102012-01-12 20:33:10 +00006649 if (V1IsUndef && V2IsUndef)
6650 return DAG.getUNDEF(VT);
6651
6652 assert(!V1IsUndef && "Op 1 of shuffle should not be undef");
Craig Topper38034c52011-11-26 22:55:48 +00006653
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006654 // Vector shuffle lowering takes 3 steps:
6655 //
6656 // 1) Normalize the input vectors. Here splats, zeroed vectors, profitable
6657 // narrowing and commutation of operands should be handled.
6658 // 2) Matching of shuffles with known shuffle masks to x86 target specific
6659 // shuffle nodes.
6660 // 3) Rewriting of unmatched masks into new generic shuffle operations,
6661 // so the shuffle can be broken into other shuffles and the legalizer can
6662 // try the lowering again.
6663 //
Craig Topper3426a3e2011-11-14 06:46:21 +00006664 // The general idea is that no vector_shuffle operation should be left to
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006665 // be matched during isel, all of them must be converted to a target specific
6666 // node here.
Bruno Cardoso Lopes0d1340b2010-09-07 20:20:27 +00006667
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006668 // Normalize the input vectors. Here splats, zeroed vectors, profitable
6669 // narrowing and commutation of operands should be handled. The actual code
6670 // doesn't include all of those, work in progress...
Nadav Rotem154819d2012-04-09 07:45:58 +00006671 SDValue NewOp = NormalizeVectorShuffle(Op, DAG);
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006672 if (NewOp.getNode())
6673 return NewOp;
Eric Christopherfd179292009-08-27 18:07:15 +00006674
Craig Topper5aaffa82012-02-19 02:53:47 +00006675 SmallVector<int, 8> M(SVOp->getMask().begin(), SVOp->getMask().end());
6676
Bruno Cardoso Lopesa22c8452010-09-04 00:39:43 +00006677 // NOTE: isPSHUFDMask can also match both masks below (unpckl_undef and
6678 // unpckh_undef). Only use pshufd if speed is more important than size.
Craig Topper5aaffa82012-02-19 02:53:47 +00006679 if (OptForSize && isUNPCKL_v_undef_Mask(M, VT, HasAVX2))
Craig Topper34671b82011-12-06 08:21:25 +00006680 return getTargetShuffleNode(X86ISD::UNPCKL, dl, VT, V1, V1, DAG);
Craig Topper5aaffa82012-02-19 02:53:47 +00006681 if (OptForSize && isUNPCKH_v_undef_Mask(M, VT, HasAVX2))
Craig Topper34671b82011-12-06 08:21:25 +00006682 return getTargetShuffleNode(X86ISD::UNPCKH, dl, VT, V1, V1, DAG);
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00006683
Craig Topperdd637ae2012-02-19 05:41:45 +00006684 if (isMOVDDUPMask(M, VT) && Subtarget->hasSSE3() &&
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006685 V2IsUndef && RelaxedMayFoldVectorLoad(V1))
Evan Cheng835580f2010-10-07 20:50:20 +00006686 return getMOVDDup(Op, dl, V1, DAG);
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006687
Craig Topperdd637ae2012-02-19 05:41:45 +00006688 if (isMOVHLPS_v_undef_Mask(M, VT))
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006689 return getMOVHighToLow(Op, dl, DAG);
6690
6691 // Use to match splats
Craig Topper5aaffa82012-02-19 02:53:47 +00006692 if (HasSSE2 && isUNPCKHMask(M, VT, HasAVX2) && V2IsUndef &&
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006693 (VT == MVT::v2f64 || VT == MVT::v2i64))
Craig Topper34671b82011-12-06 08:21:25 +00006694 return getTargetShuffleNode(X86ISD::UNPCKH, dl, VT, V1, V1, DAG);
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006695
Craig Topper5aaffa82012-02-19 02:53:47 +00006696 if (isPSHUFDMask(M, VT)) {
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006697 // The actual implementation will match the mask in the if above and then
6698 // during isel it can match several different instructions, not only pshufd
6699 // as its name says, sad but true, emulate the behavior for now...
Craig Topperdd637ae2012-02-19 05:41:45 +00006700 if (isMOVDDUPMask(M, VT) && ((VT == MVT::v4f32 || VT == MVT::v2i64)))
6701 return getTargetShuffleNode(X86ISD::MOVLHPS, dl, VT, V1, V1, DAG);
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006702
Craig Topper5aaffa82012-02-19 02:53:47 +00006703 unsigned TargetMask = getShuffleSHUFImmediate(SVOp);
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006704
Craig Topperdbd98a42012-02-07 06:28:42 +00006705 if (HasAVX && (VT == MVT::v4f32 || VT == MVT::v2f64))
6706 return getTargetShuffleNode(X86ISD::VPERMILP, dl, VT, V1, TargetMask, DAG);
6707
Craig Topper1accb7e2012-01-10 06:54:16 +00006708 if (HasSSE2 && (VT == MVT::v4f32 || VT == MVT::v4i32))
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006709 return getTargetShuffleNode(X86ISD::PSHUFD, dl, VT, V1, TargetMask, DAG);
6710
Craig Topperb3982da2011-12-31 23:50:21 +00006711 return getTargetShuffleNode(X86ISD::SHUFP, dl, VT, V1, V1,
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00006712 TargetMask, DAG);
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006713 }
Eric Christopherfd179292009-08-27 18:07:15 +00006714
Evan Chengf26ffe92008-05-29 08:22:04 +00006715 // Check if this can be converted into a logical shift.
6716 bool isLeft = false;
6717 unsigned ShAmt = 0;
Dan Gohman475871a2008-07-27 21:46:04 +00006718 SDValue ShVal;
Craig Topper1accb7e2012-01-10 06:54:16 +00006719 bool isShift = HasSSE2 && isVectorShift(SVOp, DAG, isLeft, ShVal, ShAmt);
Evan Chengf26ffe92008-05-29 08:22:04 +00006720 if (isShift && ShVal.hasOneUse()) {
Scott Michelfdc40a02009-02-17 22:15:04 +00006721 // If the shifted value has multiple uses, it may be cheaper to use
Evan Chengf26ffe92008-05-29 08:22:04 +00006722 // v_set0 + movlhps or movhlps, etc.
Dan Gohman8a55ce42009-09-23 21:02:20 +00006723 EVT EltVT = VT.getVectorElementType();
6724 ShAmt *= EltVT.getSizeInBits();
Dale Johannesenace16102009-02-03 19:33:06 +00006725 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00006726 }
Eric Christopherfd179292009-08-27 18:07:15 +00006727
Craig Topper5aaffa82012-02-19 02:53:47 +00006728 if (isMOVLMask(M, VT)) {
Gabor Greifba36cb52008-08-28 21:40:38 +00006729 if (ISD::isBuildVectorAllZeros(V1.getNode()))
Dale Johannesenace16102009-02-03 19:33:06 +00006730 return getVZextMovL(VT, VT, V2, DAG, Subtarget, dl);
Craig Topperdd637ae2012-02-19 05:41:45 +00006731 if (!isMOVLPMask(M, VT)) {
Craig Topper1accb7e2012-01-10 06:54:16 +00006732 if (HasSSE2 && (VT == MVT::v2i64 || VT == MVT::v2f64))
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00006733 return getTargetShuffleNode(X86ISD::MOVSD, dl, VT, V1, V2, DAG);
6734
Bruno Cardoso Lopes4783a3e2010-09-01 22:59:03 +00006735 if (VT == MVT::v4i32 || VT == MVT::v4f32)
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00006736 return getTargetShuffleNode(X86ISD::MOVSS, dl, VT, V1, V2, DAG);
6737 }
Evan Cheng7e2ff772008-05-08 00:57:18 +00006738 }
Eric Christopherfd179292009-08-27 18:07:15 +00006739
Nate Begeman9008ca62009-04-27 18:41:29 +00006740 // FIXME: fold these into legal mask.
Craig Topperdd637ae2012-02-19 05:41:45 +00006741 if (isMOVLHPSMask(M, VT) && !isUNPCKLMask(M, VT, HasAVX2))
Craig Topper1accb7e2012-01-10 06:54:16 +00006742 return getMOVLowToHigh(Op, dl, DAG, HasSSE2);
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006743
Craig Topperdd637ae2012-02-19 05:41:45 +00006744 if (isMOVHLPSMask(M, VT))
Dale Johannesen0488fb62010-09-30 23:57:10 +00006745 return getMOVHighToLow(Op, dl, DAG);
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00006746
Craig Topperdd637ae2012-02-19 05:41:45 +00006747 if (V2IsUndef && isMOVSHDUPMask(M, VT, Subtarget))
Dale Johannesen0488fb62010-09-30 23:57:10 +00006748 return getTargetShuffleNode(X86ISD::MOVSHDUP, dl, VT, V1, DAG);
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00006749
Craig Topperdd637ae2012-02-19 05:41:45 +00006750 if (V2IsUndef && isMOVSLDUPMask(M, VT, Subtarget))
Dale Johannesen0488fb62010-09-30 23:57:10 +00006751 return getTargetShuffleNode(X86ISD::MOVSLDUP, dl, VT, V1, DAG);
Bruno Cardoso Lopes013bb3d2010-08-31 22:35:05 +00006752
Craig Topperdd637ae2012-02-19 05:41:45 +00006753 if (isMOVLPMask(M, VT))
Craig Topper1accb7e2012-01-10 06:54:16 +00006754 return getMOVLP(Op, dl, DAG, HasSSE2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006755
Craig Topperdd637ae2012-02-19 05:41:45 +00006756 if (ShouldXformToMOVHLPS(M, VT) ||
6757 ShouldXformToMOVLP(V1.getNode(), V2.getNode(), M, VT))
Nate Begeman9008ca62009-04-27 18:41:29 +00006758 return CommuteVectorShuffle(SVOp, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006759
Evan Chengf26ffe92008-05-29 08:22:04 +00006760 if (isShift) {
Craig Toppered2e13d2012-01-22 19:15:14 +00006761 // No better options. Use a vshldq / vsrldq.
Dan Gohman8a55ce42009-09-23 21:02:20 +00006762 EVT EltVT = VT.getVectorElementType();
6763 ShAmt *= EltVT.getSizeInBits();
Dale Johannesenace16102009-02-03 19:33:06 +00006764 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00006765 }
Eric Christopherfd179292009-08-27 18:07:15 +00006766
Evan Cheng9eca5e82006-10-25 21:49:50 +00006767 bool Commuted = false;
Chris Lattner8a594482007-11-25 00:24:49 +00006768 // FIXME: This should also accept a bitcast of a splat? Be careful, not
6769 // 1,1,1,1 -> v8i16 though.
Gabor Greifba36cb52008-08-28 21:40:38 +00006770 V1IsSplat = isSplatVector(V1.getNode());
6771 V2IsSplat = isSplatVector(V2.getNode());
Scott Michelfdc40a02009-02-17 22:15:04 +00006772
Chris Lattner8a594482007-11-25 00:24:49 +00006773 // Canonicalize the splat or undef, if present, to be on the RHS.
Craig Topper39a9e482012-02-11 06:24:48 +00006774 if (!V2IsUndef && V1IsSplat && !V2IsSplat) {
6775 CommuteVectorShuffleMask(M, NumElems);
6776 std::swap(V1, V2);
Evan Cheng9bbbb982006-10-25 20:48:19 +00006777 std::swap(V1IsSplat, V2IsSplat);
Evan Cheng9eca5e82006-10-25 21:49:50 +00006778 Commuted = true;
Evan Cheng9bbbb982006-10-25 20:48:19 +00006779 }
6780
Craig Topperbeabc6c2011-12-05 06:56:46 +00006781 if (isCommutedMOVLMask(M, VT, V2IsSplat, V2IsUndef)) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006782 // Shuffling low element of v1 into undef, just return v1.
Eric Christopherfd179292009-08-27 18:07:15 +00006783 if (V2IsUndef)
Nate Begeman9008ca62009-04-27 18:41:29 +00006784 return V1;
6785 // If V2 is a splat, the mask may be malformed such as <4,3,3,3>, which
6786 // the instruction selector will not match, so get a canonical MOVL with
6787 // swapped operands to undo the commute.
6788 return getMOVL(DAG, dl, VT, V2, V1);
Evan Chengd9b8e402006-10-16 06:36:00 +00006789 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00006790
Craig Topperbeabc6c2011-12-05 06:56:46 +00006791 if (isUNPCKLMask(M, VT, HasAVX2))
Craig Topper34671b82011-12-06 08:21:25 +00006792 return getTargetShuffleNode(X86ISD::UNPCKL, dl, VT, V1, V2, DAG);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00006793
Craig Topperbeabc6c2011-12-05 06:56:46 +00006794 if (isUNPCKHMask(M, VT, HasAVX2))
Craig Topper34671b82011-12-06 08:21:25 +00006795 return getTargetShuffleNode(X86ISD::UNPCKH, dl, VT, V1, V2, DAG);
Evan Chenge1113032006-10-04 18:33:38 +00006796
Evan Cheng9bbbb982006-10-25 20:48:19 +00006797 if (V2IsSplat) {
6798 // Normalize mask so all entries that point to V2 points to its first
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00006799 // element then try to match unpck{h|l} again. If match, return a
Craig Topper39a9e482012-02-11 06:24:48 +00006800 // new vector_shuffle with the corrected mask.p
6801 SmallVector<int, 8> NewMask(M.begin(), M.end());
6802 NormalizeMask(NewMask, NumElems);
Craig Topper69947b92012-04-23 06:57:04 +00006803 if (isUNPCKLMask(NewMask, VT, HasAVX2, true))
Craig Topper39a9e482012-02-11 06:24:48 +00006804 return getTargetShuffleNode(X86ISD::UNPCKL, dl, VT, V1, V2, DAG);
Craig Topper69947b92012-04-23 06:57:04 +00006805 if (isUNPCKHMask(NewMask, VT, HasAVX2, true))
Craig Topper39a9e482012-02-11 06:24:48 +00006806 return getTargetShuffleNode(X86ISD::UNPCKH, dl, VT, V1, V2, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006807 }
6808
Evan Cheng9eca5e82006-10-25 21:49:50 +00006809 if (Commuted) {
6810 // Commute is back and try unpck* again.
Nate Begeman9008ca62009-04-27 18:41:29 +00006811 // FIXME: this seems wrong.
Craig Topper39a9e482012-02-11 06:24:48 +00006812 CommuteVectorShuffleMask(M, NumElems);
6813 std::swap(V1, V2);
6814 std::swap(V1IsSplat, V2IsSplat);
6815 Commuted = false;
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00006816
Craig Topper39a9e482012-02-11 06:24:48 +00006817 if (isUNPCKLMask(M, VT, HasAVX2))
6818 return getTargetShuffleNode(X86ISD::UNPCKL, dl, VT, V1, V2, DAG);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00006819
Craig Topper39a9e482012-02-11 06:24:48 +00006820 if (isUNPCKHMask(M, VT, HasAVX2))
6821 return getTargetShuffleNode(X86ISD::UNPCKH, dl, VT, V1, V2, DAG);
Evan Cheng9eca5e82006-10-25 21:49:50 +00006822 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00006823
Nate Begeman9008ca62009-04-27 18:41:29 +00006824 // Normalize the node to match x86 shuffle ops if needed
Craig Topper1a7700a2012-01-19 08:19:12 +00006825 if (!V2IsUndef && (isSHUFPMask(M, VT, HasAVX, /* Commuted */ true)))
Nate Begeman9008ca62009-04-27 18:41:29 +00006826 return CommuteVectorShuffle(SVOp, DAG);
6827
Bruno Cardoso Lopes7256e222010-09-03 23:24:06 +00006828 // The checks below are all present in isShuffleMaskLegal, but they are
6829 // inlined here right now to enable us to directly emit target specific
6830 // nodes, and remove one by one until they don't return Op anymore.
Bruno Cardoso Lopes7256e222010-09-03 23:24:06 +00006831
Craig Topper0e2037b2012-01-20 05:53:00 +00006832 if (isPALIGNRMask(M, VT, Subtarget))
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00006833 return getTargetShuffleNode(X86ISD::PALIGN, dl, VT, V1, V2,
Craig Topperd93e4c32011-12-11 19:12:35 +00006834 getShufflePALIGNRImmediate(SVOp),
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00006835 DAG);
6836
Bruno Cardoso Lopesc800c0d2010-09-04 02:02:14 +00006837 if (ShuffleVectorSDNode::isSplatMask(&M[0], VT) &&
6838 SVOp->getSplatIndex() == 0 && V2IsUndef) {
Craig Topperbeabc6c2011-12-05 06:56:46 +00006839 if (VT == MVT::v2f64 || VT == MVT::v2i64)
Craig Topper34671b82011-12-06 08:21:25 +00006840 return getTargetShuffleNode(X86ISD::UNPCKL, dl, VT, V1, V1, DAG);
Bruno Cardoso Lopesc800c0d2010-09-04 02:02:14 +00006841 }
6842
Craig Toppera9a568a2012-05-02 08:03:44 +00006843 if (isPSHUFHWMask(M, VT, HasAVX2))
Bruno Cardoso Lopesbbfc3102010-09-04 01:36:45 +00006844 return getTargetShuffleNode(X86ISD::PSHUFHW, dl, VT, V1,
Craig Topper5aaffa82012-02-19 02:53:47 +00006845 getShufflePSHUFHWImmediate(SVOp),
Bruno Cardoso Lopesbbfc3102010-09-04 01:36:45 +00006846 DAG);
6847
Craig Toppera9a568a2012-05-02 08:03:44 +00006848 if (isPSHUFLWMask(M, VT, HasAVX2))
Bruno Cardoso Lopesbbfc3102010-09-04 01:36:45 +00006849 return getTargetShuffleNode(X86ISD::PSHUFLW, dl, VT, V1,
Craig Topper5aaffa82012-02-19 02:53:47 +00006850 getShufflePSHUFLWImmediate(SVOp),
Bruno Cardoso Lopesbbfc3102010-09-04 01:36:45 +00006851 DAG);
6852
Craig Topper1a7700a2012-01-19 08:19:12 +00006853 if (isSHUFPMask(M, VT, HasAVX))
Craig Topperb3982da2011-12-31 23:50:21 +00006854 return getTargetShuffleNode(X86ISD::SHUFP, dl, VT, V1, V2,
Craig Topper5aaffa82012-02-19 02:53:47 +00006855 getShuffleSHUFImmediate(SVOp), DAG);
Bruno Cardoso Lopes4c827f52010-09-04 01:22:57 +00006856
Craig Topper94438ba2011-12-16 08:06:31 +00006857 if (isUNPCKL_v_undef_Mask(M, VT, HasAVX2))
Craig Topper34671b82011-12-06 08:21:25 +00006858 return getTargetShuffleNode(X86ISD::UNPCKL, dl, VT, V1, V1, DAG);
Craig Topper94438ba2011-12-16 08:06:31 +00006859 if (isUNPCKH_v_undef_Mask(M, VT, HasAVX2))
Craig Topper34671b82011-12-06 08:21:25 +00006860 return getTargetShuffleNode(X86ISD::UNPCKH, dl, VT, V1, V1, DAG);
Bruno Cardoso Lopesa22c8452010-09-04 00:39:43 +00006861
Bruno Cardoso Lopesd0888342011-07-22 00:14:56 +00006862 //===--------------------------------------------------------------------===//
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00006863 // Generate target specific nodes for 128 or 256-bit shuffles only
6864 // supported in the AVX instruction set.
6865 //
Bruno Cardoso Lopesd0888342011-07-22 00:14:56 +00006866
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00006867 // Handle VMOVDDUPY permutations
Craig Topperbeabc6c2011-12-05 06:56:46 +00006868 if (V2IsUndef && isMOVDDUPYMask(M, VT, HasAVX))
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00006869 return getTargetShuffleNode(X86ISD::MOVDDUP, dl, VT, V1, DAG);
6870
Craig Topper70b883b2011-11-28 10:14:51 +00006871 // Handle VPERMILPS/D* permutations
Craig Topperdbd98a42012-02-07 06:28:42 +00006872 if (isVPERMILPMask(M, VT, HasAVX)) {
6873 if (HasAVX2 && VT == MVT::v8i32)
6874 return getTargetShuffleNode(X86ISD::PSHUFD, dl, VT, V1,
Craig Topper5aaffa82012-02-19 02:53:47 +00006875 getShuffleSHUFImmediate(SVOp), DAG);
Craig Topper316cd2a2011-11-30 06:25:25 +00006876 return getTargetShuffleNode(X86ISD::VPERMILP, dl, VT, V1,
Craig Topper5aaffa82012-02-19 02:53:47 +00006877 getShuffleSHUFImmediate(SVOp), DAG);
Craig Topperdbd98a42012-02-07 06:28:42 +00006878 }
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00006879
Craig Topper70b883b2011-11-28 10:14:51 +00006880 // Handle VPERM2F128/VPERM2I128 permutations
Craig Topperbeabc6c2011-12-05 06:56:46 +00006881 if (isVPERM2X128Mask(M, VT, HasAVX))
Craig Topperec24e612011-11-30 07:47:51 +00006882 return getTargetShuffleNode(X86ISD::VPERM2X128, dl, VT, V1,
Craig Topper70b883b2011-11-28 10:14:51 +00006883 V2, getShuffleVPERM2X128Immediate(SVOp), DAG);
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00006884
Craig Topper1842ba02012-04-23 06:38:28 +00006885 SDValue BlendOp = LowerVECTOR_SHUFFLEtoBlend(SVOp, Subtarget, DAG);
Nadav Roteme80aa7c2012-04-09 08:33:21 +00006886 if (BlendOp.getNode())
6887 return BlendOp;
Craig Topper095c5282012-04-15 23:48:57 +00006888
Elena Demikhovsky73c504a2012-04-15 11:18:59 +00006889 if (V2IsUndef && HasAVX2 && (VT == MVT::v8i32 || VT == MVT::v8f32)) {
Craig Topper095c5282012-04-15 23:48:57 +00006890 SmallVector<SDValue, 8> permclMask;
Elena Demikhovsky73c504a2012-04-15 11:18:59 +00006891 for (unsigned i = 0; i != 8; ++i) {
Craig Topper095c5282012-04-15 23:48:57 +00006892 permclMask.push_back(DAG.getConstant((M[i]>=0) ? M[i] : 0, MVT::i32));
Elena Demikhovsky73c504a2012-04-15 11:18:59 +00006893 }
Craig Topper92040742012-04-16 06:43:40 +00006894 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v8i32,
6895 &permclMask[0], 8);
6896 // Bitcast is for VPERMPS since mask is v8i32 but node takes v8f32
Craig Topper8325c112012-04-16 00:41:45 +00006897 return DAG.getNode(X86ISD::VPERMV, dl, VT,
Craig Topper92040742012-04-16 06:43:40 +00006898 DAG.getNode(ISD::BITCAST, dl, VT, Mask), V1);
Elena Demikhovsky73c504a2012-04-15 11:18:59 +00006899 }
Craig Topper095c5282012-04-15 23:48:57 +00006900
Craig Topper8325c112012-04-16 00:41:45 +00006901 if (V2IsUndef && HasAVX2 && (VT == MVT::v4i64 || VT == MVT::v4f64))
6902 return getTargetShuffleNode(X86ISD::VPERMI, dl, VT, V1,
Elena Demikhovsky73c504a2012-04-15 11:18:59 +00006903 getShuffleCLImmediate(SVOp), DAG);
6904
Nadav Roteme80aa7c2012-04-09 08:33:21 +00006905
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00006906 //===--------------------------------------------------------------------===//
6907 // Since no target specific shuffle was selected for this generic one,
6908 // lower it into other known shuffles. FIXME: this isn't true yet, but
6909 // this is the plan.
6910 //
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00006911
Bruno Cardoso Lopes9b4ad122011-07-27 00:56:37 +00006912 // Handle v8i16 specifically since SSE can do byte extraction and insertion.
6913 if (VT == MVT::v8i16) {
Craig Topper55b24052012-09-11 06:15:32 +00006914 SDValue NewOp = LowerVECTOR_SHUFFLEv8i16(Op, Subtarget, DAG);
Bruno Cardoso Lopes9b4ad122011-07-27 00:56:37 +00006915 if (NewOp.getNode())
6916 return NewOp;
6917 }
6918
6919 if (VT == MVT::v16i8) {
6920 SDValue NewOp = LowerVECTOR_SHUFFLEv16i8(SVOp, DAG, *this);
6921 if (NewOp.getNode())
6922 return NewOp;
6923 }
6924
Elena Demikhovsky41789462012-09-06 12:42:01 +00006925 if (VT == MVT::v32i8) {
Craig Topper55b24052012-09-11 06:15:32 +00006926 SDValue NewOp = LowerVECTOR_SHUFFLEv32i8(SVOp, Subtarget, DAG);
Elena Demikhovsky41789462012-09-06 12:42:01 +00006927 if (NewOp.getNode())
6928 return NewOp;
6929 }
6930
Bruno Cardoso Lopes9b4ad122011-07-27 00:56:37 +00006931 // Handle all 128-bit wide vectors with 4 elements, and match them with
6932 // several different shuffle types.
Craig Topper7a9a28b2012-08-12 02:23:29 +00006933 if (NumElems == 4 && VT.is128BitVector())
Bruno Cardoso Lopes9b4ad122011-07-27 00:56:37 +00006934 return LowerVECTOR_SHUFFLE_128v4(SVOp, DAG);
6935
Bruno Cardoso Lopesd0888342011-07-22 00:14:56 +00006936 // Handle general 256-bit shuffles
6937 if (VT.is256BitVector())
6938 return LowerVECTOR_SHUFFLE_256(SVOp, DAG);
6939
Dan Gohman475871a2008-07-27 21:46:04 +00006940 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006941}
6942
Dan Gohman475871a2008-07-27 21:46:04 +00006943SDValue
6944X86TargetLowering::LowerEXTRACT_VECTOR_ELT_SSE4(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00006945 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00006946 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006947 DebugLoc dl = Op.getDebugLoc();
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00006948
Craig Topper7a9a28b2012-08-12 02:23:29 +00006949 if (!Op.getOperand(0).getValueType().is128BitVector())
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00006950 return SDValue();
6951
Duncan Sands83ec4b62008-06-06 12:08:01 +00006952 if (VT.getSizeInBits() == 8) {
Owen Anderson825b72b2009-08-11 20:47:22 +00006953 SDValue Extract = DAG.getNode(X86ISD::PEXTRB, dl, MVT::i32,
Craig Topper7c022842012-09-12 06:20:41 +00006954 Op.getOperand(0), Op.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00006955 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Extract,
Craig Topper7c022842012-09-12 06:20:41 +00006956 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00006957 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Craig Topper69947b92012-04-23 06:57:04 +00006958 }
6959
6960 if (VT.getSizeInBits() == 16) {
Evan Cheng52ceafa2009-01-02 05:29:08 +00006961 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
6962 // If Idx is 0, it's cheaper to do a move instead of a pextrw.
6963 if (Idx == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00006964 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i16,
6965 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006966 DAG.getNode(ISD::BITCAST, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00006967 MVT::v4i32,
Evan Cheng52ceafa2009-01-02 05:29:08 +00006968 Op.getOperand(0)),
6969 Op.getOperand(1)));
Owen Anderson825b72b2009-08-11 20:47:22 +00006970 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, dl, MVT::i32,
Craig Topper7c022842012-09-12 06:20:41 +00006971 Op.getOperand(0), Op.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00006972 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Extract,
Craig Topper7c022842012-09-12 06:20:41 +00006973 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00006974 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Craig Topper69947b92012-04-23 06:57:04 +00006975 }
6976
6977 if (VT == MVT::f32) {
Evan Cheng62a3f152008-03-24 21:52:23 +00006978 // EXTRACTPS outputs to a GPR32 register which will require a movd to copy
6979 // the result back to FR32 register. It's only worth matching if the
Dan Gohmand17cfbe2008-10-31 00:57:24 +00006980 // result has a single use which is a store or a bitcast to i32. And in
6981 // the case of a store, it's not worth it if the index is a constant 0,
6982 // because a MOVSSmr can be used instead, which is smaller and faster.
Evan Cheng62a3f152008-03-24 21:52:23 +00006983 if (!Op.hasOneUse())
Dan Gohman475871a2008-07-27 21:46:04 +00006984 return SDValue();
Gabor Greifba36cb52008-08-28 21:40:38 +00006985 SDNode *User = *Op.getNode()->use_begin();
Dan Gohmand17cfbe2008-10-31 00:57:24 +00006986 if ((User->getOpcode() != ISD::STORE ||
6987 (isa<ConstantSDNode>(Op.getOperand(1)) &&
6988 cast<ConstantSDNode>(Op.getOperand(1))->isNullValue())) &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006989 (User->getOpcode() != ISD::BITCAST ||
Owen Anderson825b72b2009-08-11 20:47:22 +00006990 User->getValueType(0) != MVT::i32))
Dan Gohman475871a2008-07-27 21:46:04 +00006991 return SDValue();
Owen Anderson825b72b2009-08-11 20:47:22 +00006992 SDValue Extract = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006993 DAG.getNode(ISD::BITCAST, dl, MVT::v4i32,
Dale Johannesenace16102009-02-03 19:33:06 +00006994 Op.getOperand(0)),
6995 Op.getOperand(1));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006996 return DAG.getNode(ISD::BITCAST, dl, MVT::f32, Extract);
Craig Topper69947b92012-04-23 06:57:04 +00006997 }
6998
6999 if (VT == MVT::i32 || VT == MVT::i64) {
Pete Coopera77214a2011-11-14 19:38:42 +00007000 // ExtractPS/pextrq works with constant index.
Mon P Wangf0fcdd82009-01-15 21:10:20 +00007001 if (isa<ConstantSDNode>(Op.getOperand(1)))
7002 return Op;
Nate Begeman14d12ca2008-02-11 04:19:36 +00007003 }
Dan Gohman475871a2008-07-27 21:46:04 +00007004 return SDValue();
Nate Begeman14d12ca2008-02-11 04:19:36 +00007005}
7006
7007
Dan Gohman475871a2008-07-27 21:46:04 +00007008SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007009X86TargetLowering::LowerEXTRACT_VECTOR_ELT(SDValue Op,
7010 SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00007011 if (!isa<ConstantSDNode>(Op.getOperand(1)))
Dan Gohman475871a2008-07-27 21:46:04 +00007012 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007013
David Greene74a579d2011-02-10 16:57:36 +00007014 SDValue Vec = Op.getOperand(0);
7015 EVT VecVT = Vec.getValueType();
7016
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007017 // If this is a 256-bit vector result, first extract the 128-bit vector and
7018 // then extract the element from the 128-bit vector.
Craig Topper7a9a28b2012-08-12 02:23:29 +00007019 if (VecVT.is256BitVector()) {
David Greene74a579d2011-02-10 16:57:36 +00007020 DebugLoc dl = Op.getNode()->getDebugLoc();
7021 unsigned NumElems = VecVT.getVectorNumElements();
7022 SDValue Idx = Op.getOperand(1);
David Greene74a579d2011-02-10 16:57:36 +00007023 unsigned IdxVal = cast<ConstantSDNode>(Idx)->getZExtValue();
7024
7025 // Get the 128-bit vector.
Craig Topper7d1e3dc2012-04-30 05:17:10 +00007026 Vec = Extract128BitVector(Vec, IdxVal, DAG, dl);
David Greene74a579d2011-02-10 16:57:36 +00007027
Craig Topper7d1e3dc2012-04-30 05:17:10 +00007028 if (IdxVal >= NumElems/2)
7029 IdxVal -= NumElems/2;
David Greene74a579d2011-02-10 16:57:36 +00007030 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, Op.getValueType(), Vec,
Craig Topper7d1e3dc2012-04-30 05:17:10 +00007031 DAG.getConstant(IdxVal, MVT::i32));
David Greene74a579d2011-02-10 16:57:36 +00007032 }
7033
Craig Topper7a9a28b2012-08-12 02:23:29 +00007034 assert(VecVT.is128BitVector() && "Unexpected vector length");
David Greene74a579d2011-02-10 16:57:36 +00007035
Craig Topperd0a31172012-01-10 06:37:29 +00007036 if (Subtarget->hasSSE41()) {
Dan Gohman475871a2008-07-27 21:46:04 +00007037 SDValue Res = LowerEXTRACT_VECTOR_ELT_SSE4(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00007038 if (Res.getNode())
Evan Cheng62a3f152008-03-24 21:52:23 +00007039 return Res;
7040 }
Nate Begeman14d12ca2008-02-11 04:19:36 +00007041
Owen Andersone50ed302009-08-10 22:56:29 +00007042 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007043 DebugLoc dl = Op.getDebugLoc();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007044 // TODO: handle v16i8.
Duncan Sands83ec4b62008-06-06 12:08:01 +00007045 if (VT.getSizeInBits() == 16) {
Dan Gohman475871a2008-07-27 21:46:04 +00007046 SDValue Vec = Op.getOperand(0);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007047 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00007048 if (Idx == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00007049 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i16,
7050 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007051 DAG.getNode(ISD::BITCAST, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00007052 MVT::v4i32, Vec),
Evan Cheng14b32e12007-12-11 01:46:18 +00007053 Op.getOperand(1)));
Evan Cheng0db9fe62006-04-25 20:13:52 +00007054 // Transform it so it match pextrw which produces a 32-bit result.
Ken Dyck70d0ef12009-12-17 15:31:52 +00007055 EVT EltVT = MVT::i32;
Dan Gohman8a55ce42009-09-23 21:02:20 +00007056 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, dl, EltVT,
Craig Topper7c022842012-09-12 06:20:41 +00007057 Op.getOperand(0), Op.getOperand(1));
Dan Gohman8a55ce42009-09-23 21:02:20 +00007058 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, EltVT, Extract,
Craig Topper7c022842012-09-12 06:20:41 +00007059 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00007060 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Craig Topper69947b92012-04-23 06:57:04 +00007061 }
7062
7063 if (VT.getSizeInBits() == 32) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007064 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007065 if (Idx == 0)
7066 return Op;
Eric Christopherfd179292009-08-27 18:07:15 +00007067
Evan Cheng0db9fe62006-04-25 20:13:52 +00007068 // SHUFPS the element to the lowest double word, then movss.
Jeffrey Yasskina44defe2011-07-27 06:22:51 +00007069 int Mask[4] = { static_cast<int>(Idx), -1, -1, -1 };
Owen Andersone50ed302009-08-10 22:56:29 +00007070 EVT VVT = Op.getOperand(0).getValueType();
Eric Christopherfd179292009-08-27 18:07:15 +00007071 SDValue Vec = DAG.getVectorShuffle(VVT, dl, Op.getOperand(0),
Nate Begeman9008ca62009-04-27 18:41:29 +00007072 DAG.getUNDEF(VVT), Mask);
Dale Johannesenace16102009-02-03 19:33:06 +00007073 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, Vec,
Chris Lattner0bd48932008-01-17 07:00:52 +00007074 DAG.getIntPtrConstant(0));
Craig Topper69947b92012-04-23 06:57:04 +00007075 }
7076
7077 if (VT.getSizeInBits() == 64) {
Nate Begeman14d12ca2008-02-11 04:19:36 +00007078 // FIXME: .td only matches this for <2 x f64>, not <2 x i64> on 32b
7079 // FIXME: seems like this should be unnecessary if mov{h,l}pd were taught
7080 // to match extract_elt for f64.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007081 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007082 if (Idx == 0)
7083 return Op;
7084
7085 // UNPCKHPD the element to the lowest double word, then movsd.
7086 // Note if the lower 64 bits of the result of the UNPCKHPD is then stored
7087 // to a f64mem, the whole operation is folded into a single MOVHPDmr.
Nate Begeman9008ca62009-04-27 18:41:29 +00007088 int Mask[2] = { 1, -1 };
Owen Andersone50ed302009-08-10 22:56:29 +00007089 EVT VVT = Op.getOperand(0).getValueType();
Eric Christopherfd179292009-08-27 18:07:15 +00007090 SDValue Vec = DAG.getVectorShuffle(VVT, dl, Op.getOperand(0),
Nate Begeman9008ca62009-04-27 18:41:29 +00007091 DAG.getUNDEF(VVT), Mask);
Dale Johannesenace16102009-02-03 19:33:06 +00007092 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, Vec,
Chris Lattner0bd48932008-01-17 07:00:52 +00007093 DAG.getIntPtrConstant(0));
Evan Cheng0db9fe62006-04-25 20:13:52 +00007094 }
7095
Dan Gohman475871a2008-07-27 21:46:04 +00007096 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007097}
7098
Dan Gohman475871a2008-07-27 21:46:04 +00007099SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007100X86TargetLowering::LowerINSERT_VECTOR_ELT_SSE4(SDValue Op,
7101 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00007102 EVT VT = Op.getValueType();
Dan Gohman8a55ce42009-09-23 21:02:20 +00007103 EVT EltVT = VT.getVectorElementType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007104 DebugLoc dl = Op.getDebugLoc();
Nate Begeman14d12ca2008-02-11 04:19:36 +00007105
Dan Gohman475871a2008-07-27 21:46:04 +00007106 SDValue N0 = Op.getOperand(0);
7107 SDValue N1 = Op.getOperand(1);
7108 SDValue N2 = Op.getOperand(2);
Nate Begeman14d12ca2008-02-11 04:19:36 +00007109
Craig Topper7a9a28b2012-08-12 02:23:29 +00007110 if (!VT.is128BitVector())
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007111 return SDValue();
7112
Dan Gohman8a55ce42009-09-23 21:02:20 +00007113 if ((EltVT.getSizeInBits() == 8 || EltVT.getSizeInBits() == 16) &&
Dan Gohmanef521f12008-08-14 22:53:18 +00007114 isa<ConstantSDNode>(N2)) {
Chris Lattner8f2b4cc2010-02-23 02:07:48 +00007115 unsigned Opc;
7116 if (VT == MVT::v8i16)
7117 Opc = X86ISD::PINSRW;
Chris Lattner8f2b4cc2010-02-23 02:07:48 +00007118 else if (VT == MVT::v16i8)
7119 Opc = X86ISD::PINSRB;
7120 else
7121 Opc = X86ISD::PINSRB;
7122
Nate Begeman14d12ca2008-02-11 04:19:36 +00007123 // Transform it so it match pinsr{b,w} which expects a GR32 as its second
7124 // argument.
Owen Anderson825b72b2009-08-11 20:47:22 +00007125 if (N1.getValueType() != MVT::i32)
7126 N1 = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, N1);
7127 if (N2.getValueType() != MVT::i32)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007128 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Dale Johannesenace16102009-02-03 19:33:06 +00007129 return DAG.getNode(Opc, dl, VT, N0, N1, N2);
Craig Topper69947b92012-04-23 06:57:04 +00007130 }
7131
7132 if (EltVT == MVT::f32 && isa<ConstantSDNode>(N2)) {
Nate Begeman14d12ca2008-02-11 04:19:36 +00007133 // Bits [7:6] of the constant are the source select. This will always be
7134 // zero here. The DAG Combiner may combine an extract_elt index into these
7135 // bits. For example (insert (extract, 3), 2) could be matched by putting
7136 // the '3' into bits [7:6] of X86ISD::INSERTPS.
Scott Michelfdc40a02009-02-17 22:15:04 +00007137 // Bits [5:4] of the constant are the destination select. This is the
Nate Begeman14d12ca2008-02-11 04:19:36 +00007138 // value of the incoming immediate.
Scott Michelfdc40a02009-02-17 22:15:04 +00007139 // Bits [3:0] of the constant are the zero mask. The DAG Combiner may
Nate Begeman14d12ca2008-02-11 04:19:36 +00007140 // combine either bitwise AND or insert of float 0.0 to set these bits.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007141 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue() << 4);
Eric Christopherfbd66872009-07-24 00:33:09 +00007142 // Create this as a scalar to vector..
Owen Anderson825b72b2009-08-11 20:47:22 +00007143 N1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4f32, N1);
Dale Johannesenace16102009-02-03 19:33:06 +00007144 return DAG.getNode(X86ISD::INSERTPS, dl, VT, N0, N1, N2);
Craig Topper69947b92012-04-23 06:57:04 +00007145 }
7146
7147 if ((EltVT == MVT::i32 || EltVT == MVT::i64) && isa<ConstantSDNode>(N2)) {
Eric Christopherfbd66872009-07-24 00:33:09 +00007148 // PINSR* works with constant index.
7149 return Op;
Nate Begeman14d12ca2008-02-11 04:19:36 +00007150 }
Dan Gohman475871a2008-07-27 21:46:04 +00007151 return SDValue();
Nate Begeman14d12ca2008-02-11 04:19:36 +00007152}
7153
Dan Gohman475871a2008-07-27 21:46:04 +00007154SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007155X86TargetLowering::LowerINSERT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00007156 EVT VT = Op.getValueType();
Dan Gohman8a55ce42009-09-23 21:02:20 +00007157 EVT EltVT = VT.getVectorElementType();
Nate Begeman14d12ca2008-02-11 04:19:36 +00007158
David Greene6b381262011-02-09 15:32:06 +00007159 DebugLoc dl = Op.getDebugLoc();
7160 SDValue N0 = Op.getOperand(0);
7161 SDValue N1 = Op.getOperand(1);
7162 SDValue N2 = Op.getOperand(2);
7163
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007164 // If this is a 256-bit vector result, first extract the 128-bit vector,
7165 // insert the element into the extracted half and then place it back.
Craig Topper7a9a28b2012-08-12 02:23:29 +00007166 if (VT.is256BitVector()) {
David Greene6b381262011-02-09 15:32:06 +00007167 if (!isa<ConstantSDNode>(N2))
7168 return SDValue();
7169
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007170 // Get the desired 128-bit vector half.
David Greene6b381262011-02-09 15:32:06 +00007171 unsigned NumElems = VT.getVectorNumElements();
7172 unsigned IdxVal = cast<ConstantSDNode>(N2)->getZExtValue();
Craig Topper7d1e3dc2012-04-30 05:17:10 +00007173 SDValue V = Extract128BitVector(N0, IdxVal, DAG, dl);
David Greene6b381262011-02-09 15:32:06 +00007174
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007175 // Insert the element into the desired half.
Craig Topper7d1e3dc2012-04-30 05:17:10 +00007176 bool Upper = IdxVal >= NumElems/2;
7177 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, V.getValueType(), V, N1,
7178 DAG.getConstant(Upper ? IdxVal-NumElems/2 : IdxVal, MVT::i32));
David Greene6b381262011-02-09 15:32:06 +00007179
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007180 // Insert the changed part back to the 256-bit vector
Craig Topper7d1e3dc2012-04-30 05:17:10 +00007181 return Insert128BitVector(N0, V, IdxVal, DAG, dl);
David Greene6b381262011-02-09 15:32:06 +00007182 }
7183
Craig Topperd0a31172012-01-10 06:37:29 +00007184 if (Subtarget->hasSSE41())
Nate Begeman14d12ca2008-02-11 04:19:36 +00007185 return LowerINSERT_VECTOR_ELT_SSE4(Op, DAG);
7186
Dan Gohman8a55ce42009-09-23 21:02:20 +00007187 if (EltVT == MVT::i8)
Dan Gohman475871a2008-07-27 21:46:04 +00007188 return SDValue();
Evan Cheng794405e2007-12-12 07:55:34 +00007189
Dan Gohman8a55ce42009-09-23 21:02:20 +00007190 if (EltVT.getSizeInBits() == 16 && isa<ConstantSDNode>(N2)) {
Evan Cheng794405e2007-12-12 07:55:34 +00007191 // Transform it so it match pinsrw which expects a 16-bit value in a GR32
7192 // as its second argument.
Owen Anderson825b72b2009-08-11 20:47:22 +00007193 if (N1.getValueType() != MVT::i32)
7194 N1 = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, N1);
7195 if (N2.getValueType() != MVT::i32)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007196 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Dale Johannesen0488fb62010-09-30 23:57:10 +00007197 return DAG.getNode(X86ISD::PINSRW, dl, VT, N0, N1, N2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007198 }
Dan Gohman475871a2008-07-27 21:46:04 +00007199 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007200}
7201
Craig Topper55b24052012-09-11 06:15:32 +00007202static SDValue LowerSCALAR_TO_VECTOR(SDValue Op, SelectionDAG &DAG) {
Bruno Cardoso Lopes233fa392011-07-25 23:05:16 +00007203 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007204 DebugLoc dl = Op.getDebugLoc();
David Greene2fcdfb42011-02-10 23:11:29 +00007205 EVT OpVT = Op.getValueType();
7206
Bruno Cardoso Lopes233fa392011-07-25 23:05:16 +00007207 // If this is a 256-bit vector result, first insert into a 128-bit
7208 // vector and then insert into the 256-bit vector.
Craig Topper7a9a28b2012-08-12 02:23:29 +00007209 if (!OpVT.is128BitVector()) {
Bruno Cardoso Lopes233fa392011-07-25 23:05:16 +00007210 // Insert into a 128-bit vector.
7211 EVT VT128 = EVT::getVectorVT(*Context,
7212 OpVT.getVectorElementType(),
7213 OpVT.getVectorNumElements() / 2);
7214
7215 Op = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT128, Op.getOperand(0));
7216
7217 // Insert the 128-bit vector.
Craig Topperb14940a2012-04-22 20:55:18 +00007218 return Insert128BitVector(DAG.getUNDEF(OpVT), Op, 0, DAG, dl);
Bruno Cardoso Lopes233fa392011-07-25 23:05:16 +00007219 }
7220
Craig Topperd77d2fe2012-04-29 20:22:05 +00007221 if (OpVT == MVT::v1i64 &&
Chris Lattnerf172ecd2010-07-04 23:07:25 +00007222 Op.getOperand(0).getValueType() == MVT::i64)
Owen Anderson825b72b2009-08-11 20:47:22 +00007223 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v1i64, Op.getOperand(0));
Rafael Espindoladef390a2009-08-03 02:45:34 +00007224
Owen Anderson825b72b2009-08-11 20:47:22 +00007225 SDValue AnyExt = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, Op.getOperand(0));
Craig Topper7a9a28b2012-08-12 02:23:29 +00007226 assert(OpVT.is128BitVector() && "Expected an SSE type!");
Craig Topperd77d2fe2012-04-29 20:22:05 +00007227 return DAG.getNode(ISD::BITCAST, dl, OpVT,
Dale Johannesen0488fb62010-09-30 23:57:10 +00007228 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,AnyExt));
Evan Cheng0db9fe62006-04-25 20:13:52 +00007229}
7230
David Greene91585092011-01-26 15:38:49 +00007231// Lower a node with an EXTRACT_SUBVECTOR opcode. This may result in
7232// a simple subregister reference or explicit instructions to grab
7233// upper bits of a vector.
Craig Topper55b24052012-09-11 06:15:32 +00007234static SDValue LowerEXTRACT_SUBVECTOR(SDValue Op, const X86Subtarget *Subtarget,
7235 SelectionDAG &DAG) {
David Greene91585092011-01-26 15:38:49 +00007236 if (Subtarget->hasAVX()) {
David Greenea5f26012011-02-07 19:36:54 +00007237 DebugLoc dl = Op.getNode()->getDebugLoc();
7238 SDValue Vec = Op.getNode()->getOperand(0);
7239 SDValue Idx = Op.getNode()->getOperand(1);
7240
Craig Topper7a9a28b2012-08-12 02:23:29 +00007241 if (Op.getNode()->getValueType(0).is128BitVector() &&
7242 Vec.getNode()->getValueType(0).is256BitVector() &&
Craig Topperb14940a2012-04-22 20:55:18 +00007243 isa<ConstantSDNode>(Idx)) {
7244 unsigned IdxVal = cast<ConstantSDNode>(Idx)->getZExtValue();
7245 return Extract128BitVector(Vec, IdxVal, DAG, dl);
David Greenea5f26012011-02-07 19:36:54 +00007246 }
David Greene91585092011-01-26 15:38:49 +00007247 }
7248 return SDValue();
7249}
7250
David Greenecfe33c42011-01-26 19:13:22 +00007251// Lower a node with an INSERT_SUBVECTOR opcode. This may result in a
7252// simple superregister reference or explicit instructions to insert
7253// the upper bits of a vector.
Craig Topper55b24052012-09-11 06:15:32 +00007254static SDValue LowerINSERT_SUBVECTOR(SDValue Op, const X86Subtarget *Subtarget,
7255 SelectionDAG &DAG) {
David Greenecfe33c42011-01-26 19:13:22 +00007256 if (Subtarget->hasAVX()) {
7257 DebugLoc dl = Op.getNode()->getDebugLoc();
7258 SDValue Vec = Op.getNode()->getOperand(0);
7259 SDValue SubVec = Op.getNode()->getOperand(1);
7260 SDValue Idx = Op.getNode()->getOperand(2);
7261
Craig Topper7a9a28b2012-08-12 02:23:29 +00007262 if (Op.getNode()->getValueType(0).is256BitVector() &&
7263 SubVec.getNode()->getValueType(0).is128BitVector() &&
Craig Topperb14940a2012-04-22 20:55:18 +00007264 isa<ConstantSDNode>(Idx)) {
7265 unsigned IdxVal = cast<ConstantSDNode>(Idx)->getZExtValue();
7266 return Insert128BitVector(Vec, SubVec, IdxVal, DAG, dl);
David Greenecfe33c42011-01-26 19:13:22 +00007267 }
7268 }
7269 return SDValue();
7270}
7271
Bill Wendling056292f2008-09-16 21:48:12 +00007272// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
7273// their target countpart wrapped in the X86ISD::Wrapper node. Suppose N is
7274// one of the above mentioned nodes. It has to be wrapped because otherwise
7275// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
7276// be used to form addressing mode. These wrapped nodes will be selected
7277// into MOV32ri.
Dan Gohman475871a2008-07-27 21:46:04 +00007278SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007279X86TargetLowering::LowerConstantPool(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00007280 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Eric Christopherfd179292009-08-27 18:07:15 +00007281
Chris Lattner41621a22009-06-26 19:22:52 +00007282 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
7283 // global base reg.
7284 unsigned char OpFlag = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00007285 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007286 CodeModel::Model M = getTargetMachine().getCodeModel();
7287
Chris Lattner4f066492009-07-11 20:29:19 +00007288 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007289 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattnere4df7562009-07-09 03:15:51 +00007290 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00007291 else if (Subtarget->isPICStyleGOT())
Chris Lattner88e1fd52009-07-09 04:24:46 +00007292 OpFlag = X86II::MO_GOTOFF;
Chris Lattnere2c92082009-07-10 21:00:45 +00007293 else if (Subtarget->isPICStyleStubPIC())
Chris Lattner88e1fd52009-07-09 04:24:46 +00007294 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Eric Christopherfd179292009-08-27 18:07:15 +00007295
Evan Cheng1606e8e2009-03-13 07:51:59 +00007296 SDValue Result = DAG.getTargetConstantPool(CP->getConstVal(), getPointerTy(),
Chris Lattner41621a22009-06-26 19:22:52 +00007297 CP->getAlignment(),
7298 CP->getOffset(), OpFlag);
7299 DebugLoc DL = CP->getDebugLoc();
Chris Lattner18c59872009-06-27 04:16:01 +00007300 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00007301 // With PIC, the address is actually $g + Offset.
Chris Lattner41621a22009-06-26 19:22:52 +00007302 if (OpFlag) {
7303 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesenb300d2a2009-02-07 00:55:49 +00007304 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00007305 DebugLoc(), getPointerTy()),
Anton Korobeynikov7f705592007-01-12 19:20:47 +00007306 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007307 }
7308
7309 return Result;
7310}
7311
Dan Gohmand858e902010-04-17 15:26:15 +00007312SDValue X86TargetLowering::LowerJumpTable(SDValue Op, SelectionDAG &DAG) const {
Chris Lattner18c59872009-06-27 04:16:01 +00007313 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
Eric Christopherfd179292009-08-27 18:07:15 +00007314
Chris Lattner18c59872009-06-27 04:16:01 +00007315 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
7316 // global base reg.
7317 unsigned char OpFlag = 0;
7318 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007319 CodeModel::Model M = getTargetMachine().getCodeModel();
7320
Chris Lattner4f066492009-07-11 20:29:19 +00007321 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007322 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattnere4df7562009-07-09 03:15:51 +00007323 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00007324 else if (Subtarget->isPICStyleGOT())
Chris Lattner88e1fd52009-07-09 04:24:46 +00007325 OpFlag = X86II::MO_GOTOFF;
Chris Lattnere2c92082009-07-10 21:00:45 +00007326 else if (Subtarget->isPICStyleStubPIC())
Chris Lattner88e1fd52009-07-09 04:24:46 +00007327 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Eric Christopherfd179292009-08-27 18:07:15 +00007328
Chris Lattner18c59872009-06-27 04:16:01 +00007329 SDValue Result = DAG.getTargetJumpTable(JT->getIndex(), getPointerTy(),
7330 OpFlag);
7331 DebugLoc DL = JT->getDebugLoc();
7332 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Eric Christopherfd179292009-08-27 18:07:15 +00007333
Chris Lattner18c59872009-06-27 04:16:01 +00007334 // With PIC, the address is actually $g + Offset.
Chris Lattner1e61e692010-11-15 02:46:57 +00007335 if (OpFlag)
Chris Lattner18c59872009-06-27 04:16:01 +00007336 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
7337 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00007338 DebugLoc(), getPointerTy()),
Chris Lattner18c59872009-06-27 04:16:01 +00007339 Result);
Eric Christopherfd179292009-08-27 18:07:15 +00007340
Chris Lattner18c59872009-06-27 04:16:01 +00007341 return Result;
7342}
7343
7344SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007345X86TargetLowering::LowerExternalSymbol(SDValue Op, SelectionDAG &DAG) const {
Chris Lattner18c59872009-06-27 04:16:01 +00007346 const char *Sym = cast<ExternalSymbolSDNode>(Op)->getSymbol();
Eric Christopherfd179292009-08-27 18:07:15 +00007347
Chris Lattner18c59872009-06-27 04:16:01 +00007348 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
7349 // global base reg.
7350 unsigned char OpFlag = 0;
7351 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007352 CodeModel::Model M = getTargetMachine().getCodeModel();
7353
Chris Lattner4f066492009-07-11 20:29:19 +00007354 if (Subtarget->isPICStyleRIPRel() &&
Eli Friedman586272d2011-08-11 01:48:05 +00007355 (M == CodeModel::Small || M == CodeModel::Kernel)) {
7356 if (Subtarget->isTargetDarwin() || Subtarget->isTargetELF())
7357 OpFlag = X86II::MO_GOTPCREL;
Chris Lattnere4df7562009-07-09 03:15:51 +00007358 WrapperKind = X86ISD::WrapperRIP;
Eli Friedman586272d2011-08-11 01:48:05 +00007359 } else if (Subtarget->isPICStyleGOT()) {
7360 OpFlag = X86II::MO_GOT;
7361 } else if (Subtarget->isPICStyleStubPIC()) {
7362 OpFlag = X86II::MO_DARWIN_NONLAZY_PIC_BASE;
7363 } else if (Subtarget->isPICStyleStubNoDynamic()) {
7364 OpFlag = X86II::MO_DARWIN_NONLAZY;
7365 }
Eric Christopherfd179292009-08-27 18:07:15 +00007366
Chris Lattner18c59872009-06-27 04:16:01 +00007367 SDValue Result = DAG.getTargetExternalSymbol(Sym, getPointerTy(), OpFlag);
Eric Christopherfd179292009-08-27 18:07:15 +00007368
Chris Lattner18c59872009-06-27 04:16:01 +00007369 DebugLoc DL = Op.getDebugLoc();
7370 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Eric Christopherfd179292009-08-27 18:07:15 +00007371
7372
Chris Lattner18c59872009-06-27 04:16:01 +00007373 // With PIC, the address is actually $g + Offset.
7374 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
Chris Lattnere4df7562009-07-09 03:15:51 +00007375 !Subtarget->is64Bit()) {
Chris Lattner18c59872009-06-27 04:16:01 +00007376 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
7377 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00007378 DebugLoc(), getPointerTy()),
Chris Lattner18c59872009-06-27 04:16:01 +00007379 Result);
7380 }
Eric Christopherfd179292009-08-27 18:07:15 +00007381
Eli Friedman586272d2011-08-11 01:48:05 +00007382 // For symbols that require a load from a stub to get the address, emit the
7383 // load.
7384 if (isGlobalStubReference(OpFlag))
7385 Result = DAG.getLoad(getPointerTy(), DL, DAG.getEntryNode(), Result,
Pete Cooperd752e0f2011-11-08 18:42:53 +00007386 MachinePointerInfo::getGOT(), false, false, false, 0);
Eli Friedman586272d2011-08-11 01:48:05 +00007387
Chris Lattner18c59872009-06-27 04:16:01 +00007388 return Result;
7389}
7390
Dan Gohman475871a2008-07-27 21:46:04 +00007391SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007392X86TargetLowering::LowerBlockAddress(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman29cbade2009-11-20 23:18:13 +00007393 // Create the TargetBlockAddressAddress node.
7394 unsigned char OpFlags =
7395 Subtarget->ClassifyBlockAddressReference();
Dan Gohmanf705adb2009-10-30 01:28:02 +00007396 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman46510a72010-04-15 01:51:59 +00007397 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
Michael Liao6c7ccaa2012-09-12 21:43:09 +00007398 int64_t Offset = cast<BlockAddressSDNode>(Op)->getOffset();
Dan Gohman29cbade2009-11-20 23:18:13 +00007399 DebugLoc dl = Op.getDebugLoc();
Michael Liao6c7ccaa2012-09-12 21:43:09 +00007400 SDValue Result = DAG.getTargetBlockAddress(BA, getPointerTy(), Offset,
7401 OpFlags);
Dan Gohman29cbade2009-11-20 23:18:13 +00007402
Dan Gohmanf705adb2009-10-30 01:28:02 +00007403 if (Subtarget->isPICStyleRIPRel() &&
7404 (M == CodeModel::Small || M == CodeModel::Kernel))
Dan Gohman29cbade2009-11-20 23:18:13 +00007405 Result = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Result);
7406 else
7407 Result = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), Result);
Dan Gohmanf705adb2009-10-30 01:28:02 +00007408
Dan Gohman29cbade2009-11-20 23:18:13 +00007409 // With PIC, the address is actually $g + Offset.
7410 if (isGlobalRelativeToPICBase(OpFlags)) {
7411 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(),
7412 DAG.getNode(X86ISD::GlobalBaseReg, dl, getPointerTy()),
7413 Result);
7414 }
Dan Gohmanf705adb2009-10-30 01:28:02 +00007415
7416 return Result;
7417}
7418
7419SDValue
Dale Johannesen33c960f2009-02-04 20:06:27 +00007420X86TargetLowering::LowerGlobalAddress(const GlobalValue *GV, DebugLoc dl,
Dan Gohman6520e202008-10-18 02:06:02 +00007421 int64_t Offset,
Evan Chengda43bcf2008-09-24 00:05:32 +00007422 SelectionDAG &DAG) const {
Dan Gohman6520e202008-10-18 02:06:02 +00007423 // Create the TargetGlobalAddress node, folding in the constant
7424 // offset if it is legal.
Chris Lattnerd392bd92009-07-10 07:20:05 +00007425 unsigned char OpFlags =
7426 Subtarget->ClassifyGlobalReference(GV, getTargetMachine());
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007427 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman6520e202008-10-18 02:06:02 +00007428 SDValue Result;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007429 if (OpFlags == X86II::MO_NO_FLAG &&
7430 X86::isOffsetSuitableForCodeModel(Offset, M)) {
Chris Lattner4aa21aa2009-07-09 00:58:53 +00007431 // A direct static reference to a global.
Devang Patel0d881da2010-07-06 22:08:15 +00007432 Result = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(), Offset);
Dan Gohman6520e202008-10-18 02:06:02 +00007433 Offset = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00007434 } else {
Devang Patel0d881da2010-07-06 22:08:15 +00007435 Result = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(), 0, OpFlags);
Chris Lattner18c59872009-06-27 04:16:01 +00007436 }
Eric Christopherfd179292009-08-27 18:07:15 +00007437
Chris Lattner4f066492009-07-11 20:29:19 +00007438 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007439 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattner18c59872009-06-27 04:16:01 +00007440 Result = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Result);
7441 else
7442 Result = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), Result);
Dan Gohman6520e202008-10-18 02:06:02 +00007443
Anton Korobeynikov7f705592007-01-12 19:20:47 +00007444 // With PIC, the address is actually $g + Offset.
Chris Lattner36c25012009-07-10 07:34:39 +00007445 if (isGlobalRelativeToPICBase(OpFlags)) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00007446 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(),
7447 DAG.getNode(X86ISD::GlobalBaseReg, dl, getPointerTy()),
Anton Korobeynikov7f705592007-01-12 19:20:47 +00007448 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007449 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007450
Chris Lattner36c25012009-07-10 07:34:39 +00007451 // For globals that require a load from a stub to get the address, emit the
7452 // load.
7453 if (isGlobalStubReference(OpFlags))
Dale Johannesen33c960f2009-02-04 20:06:27 +00007454 Result = DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(), Result,
Pete Cooperd752e0f2011-11-08 18:42:53 +00007455 MachinePointerInfo::getGOT(), false, false, false, 0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007456
Dan Gohman6520e202008-10-18 02:06:02 +00007457 // If there was a non-zero offset that we didn't fold, create an explicit
7458 // addition for it.
7459 if (Offset != 0)
Dale Johannesen33c960f2009-02-04 20:06:27 +00007460 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(), Result,
Dan Gohman6520e202008-10-18 02:06:02 +00007461 DAG.getConstant(Offset, getPointerTy()));
7462
Evan Cheng0db9fe62006-04-25 20:13:52 +00007463 return Result;
7464}
7465
Evan Chengda43bcf2008-09-24 00:05:32 +00007466SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007467X86TargetLowering::LowerGlobalAddress(SDValue Op, SelectionDAG &DAG) const {
Evan Chengda43bcf2008-09-24 00:05:32 +00007468 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Dan Gohman6520e202008-10-18 02:06:02 +00007469 int64_t Offset = cast<GlobalAddressSDNode>(Op)->getOffset();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007470 return LowerGlobalAddress(GV, Op.getDebugLoc(), Offset, DAG);
Evan Chengda43bcf2008-09-24 00:05:32 +00007471}
7472
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007473static SDValue
7474GetTLSADDR(SelectionDAG &DAG, SDValue Chain, GlobalAddressSDNode *GA,
Owen Andersone50ed302009-08-10 22:56:29 +00007475 SDValue *InFlag, const EVT PtrVT, unsigned ReturnReg,
Hans Wennborgf0234fc2012-06-01 16:27:21 +00007476 unsigned char OperandFlags, bool LocalDynamic = false) {
Anton Korobeynikov817a4642009-12-11 19:39:55 +00007477 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00007478 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007479 DebugLoc dl = GA->getDebugLoc();
Devang Patel0d881da2010-07-06 22:08:15 +00007480 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl,
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007481 GA->getValueType(0),
Chris Lattnerb903bed2009-06-26 21:20:29 +00007482 GA->getOffset(),
7483 OperandFlags);
Hans Wennborgf0234fc2012-06-01 16:27:21 +00007484
7485 X86ISD::NodeType CallType = LocalDynamic ? X86ISD::TLSBASEADDR
7486 : X86ISD::TLSADDR;
7487
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007488 if (InFlag) {
7489 SDValue Ops[] = { Chain, TGA, *InFlag };
Hans Wennborgf0234fc2012-06-01 16:27:21 +00007490 Chain = DAG.getNode(CallType, dl, NodeTys, Ops, 3);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007491 } else {
7492 SDValue Ops[] = { Chain, TGA };
Hans Wennborgf0234fc2012-06-01 16:27:21 +00007493 Chain = DAG.getNode(CallType, dl, NodeTys, Ops, 2);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007494 }
Anton Korobeynikov817a4642009-12-11 19:39:55 +00007495
7496 // TLSADDR will be codegen'ed as call. Inform MFI that function has calls.
Bill Wendlingb92187a2010-05-14 21:14:32 +00007497 MFI->setAdjustsStack(true);
Anton Korobeynikov817a4642009-12-11 19:39:55 +00007498
Rafael Espindola15f1b662009-04-24 12:59:40 +00007499 SDValue Flag = Chain.getValue(1);
7500 return DAG.getCopyFromReg(Chain, dl, ReturnReg, PtrVT, Flag);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007501}
7502
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007503// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 32 bit
Dan Gohman475871a2008-07-27 21:46:04 +00007504static SDValue
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007505LowerToTLSGeneralDynamicModel32(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00007506 const EVT PtrVT) {
Dan Gohman475871a2008-07-27 21:46:04 +00007507 SDValue InFlag;
Dale Johannesendd64c412009-02-04 00:33:20 +00007508 DebugLoc dl = GA->getDebugLoc(); // ? function entry point might be better
7509 SDValue Chain = DAG.getCopyToReg(DAG.getEntryNode(), dl, X86::EBX,
Craig Topper7c022842012-09-12 06:20:41 +00007510 DAG.getNode(X86ISD::GlobalBaseReg,
7511 DebugLoc(), PtrVT), InFlag);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007512 InFlag = Chain.getValue(1);
7513
Chris Lattnerb903bed2009-06-26 21:20:29 +00007514 return GetTLSADDR(DAG, Chain, GA, &InFlag, PtrVT, X86::EAX, X86II::MO_TLSGD);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007515}
7516
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007517// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 64 bit
Dan Gohman475871a2008-07-27 21:46:04 +00007518static SDValue
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007519LowerToTLSGeneralDynamicModel64(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00007520 const EVT PtrVT) {
Chris Lattnerb903bed2009-06-26 21:20:29 +00007521 return GetTLSADDR(DAG, DAG.getEntryNode(), GA, NULL, PtrVT,
7522 X86::RAX, X86II::MO_TLSGD);
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007523}
7524
Hans Wennborgf0234fc2012-06-01 16:27:21 +00007525static SDValue LowerToTLSLocalDynamicModel(GlobalAddressSDNode *GA,
7526 SelectionDAG &DAG,
7527 const EVT PtrVT,
7528 bool is64Bit) {
7529 DebugLoc dl = GA->getDebugLoc();
7530
7531 // Get the start address of the TLS block for this module.
7532 X86MachineFunctionInfo* MFI = DAG.getMachineFunction()
7533 .getInfo<X86MachineFunctionInfo>();
7534 MFI->incNumLocalDynamicTLSAccesses();
7535
7536 SDValue Base;
7537 if (is64Bit) {
7538 Base = GetTLSADDR(DAG, DAG.getEntryNode(), GA, NULL, PtrVT, X86::RAX,
7539 X86II::MO_TLSLD, /*LocalDynamic=*/true);
7540 } else {
7541 SDValue InFlag;
7542 SDValue Chain = DAG.getCopyToReg(DAG.getEntryNode(), dl, X86::EBX,
7543 DAG.getNode(X86ISD::GlobalBaseReg, DebugLoc(), PtrVT), InFlag);
7544 InFlag = Chain.getValue(1);
7545 Base = GetTLSADDR(DAG, Chain, GA, &InFlag, PtrVT, X86::EAX,
7546 X86II::MO_TLSLDM, /*LocalDynamic=*/true);
7547 }
7548
7549 // Note: the CleanupLocalDynamicTLSPass will remove redundant computations
7550 // of Base.
7551
7552 // Build x@dtpoff.
7553 unsigned char OperandFlags = X86II::MO_DTPOFF;
7554 unsigned WrapperKind = X86ISD::Wrapper;
7555 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl,
7556 GA->getValueType(0),
7557 GA->getOffset(), OperandFlags);
7558 SDValue Offset = DAG.getNode(WrapperKind, dl, PtrVT, TGA);
7559
7560 // Add x@dtpoff with the base.
7561 return DAG.getNode(ISD::ADD, dl, PtrVT, Offset, Base);
7562}
7563
Hans Wennborg228756c2012-05-11 10:11:01 +00007564// Lower ISD::GlobalTLSAddress using the "initial exec" or "local exec" model.
Dan Gohman475871a2008-07-27 21:46:04 +00007565static SDValue LowerToTLSExecModel(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00007566 const EVT PtrVT, TLSModel::Model model,
Hans Wennborg228756c2012-05-11 10:11:01 +00007567 bool is64Bit, bool isPIC) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00007568 DebugLoc dl = GA->getDebugLoc();
Michael J. Spencerec38de22010-10-10 22:04:20 +00007569
Chris Lattnerf93b90c2010-09-22 04:39:11 +00007570 // Get the Thread Pointer, which is %gs:0 (32-bit) or %fs:0 (64-bit).
7571 Value *Ptr = Constant::getNullValue(Type::getInt8PtrTy(*DAG.getContext(),
7572 is64Bit ? 257 : 256));
Rafael Espindola094fad32009-04-08 21:14:34 +00007573
Michael J. Spencerec38de22010-10-10 22:04:20 +00007574 SDValue ThreadPointer = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(),
Chris Lattnerf93b90c2010-09-22 04:39:11 +00007575 DAG.getIntPtrConstant(0),
Pete Cooperd752e0f2011-11-08 18:42:53 +00007576 MachinePointerInfo(Ptr),
7577 false, false, false, 0);
Rafael Espindola094fad32009-04-08 21:14:34 +00007578
Chris Lattnerb903bed2009-06-26 21:20:29 +00007579 unsigned char OperandFlags = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00007580 // Most TLS accesses are not RIP relative, even on x86-64. One exception is
7581 // initialexec.
7582 unsigned WrapperKind = X86ISD::Wrapper;
7583 if (model == TLSModel::LocalExec) {
Chris Lattnerb903bed2009-06-26 21:20:29 +00007584 OperandFlags = is64Bit ? X86II::MO_TPOFF : X86II::MO_NTPOFF;
Hans Wennborg228756c2012-05-11 10:11:01 +00007585 } else if (model == TLSModel::InitialExec) {
7586 if (is64Bit) {
7587 OperandFlags = X86II::MO_GOTTPOFF;
7588 WrapperKind = X86ISD::WrapperRIP;
7589 } else {
7590 OperandFlags = isPIC ? X86II::MO_GOTNTPOFF : X86II::MO_INDNTPOFF;
7591 }
Chris Lattner18c59872009-06-27 04:16:01 +00007592 } else {
Hans Wennborg228756c2012-05-11 10:11:01 +00007593 llvm_unreachable("Unexpected model");
Chris Lattnerb903bed2009-06-26 21:20:29 +00007594 }
Eric Christopherfd179292009-08-27 18:07:15 +00007595
Hans Wennborg228756c2012-05-11 10:11:01 +00007596 // emit "addl x@ntpoff,%eax" (local exec)
7597 // or "addl x@indntpoff,%eax" (initial exec)
7598 // or "addl x@gotntpoff(%ebx) ,%eax" (initial exec, 32-bit pic)
Michael J. Spencerec38de22010-10-10 22:04:20 +00007599 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl,
Devang Patel0d881da2010-07-06 22:08:15 +00007600 GA->getValueType(0),
Chris Lattnerb903bed2009-06-26 21:20:29 +00007601 GA->getOffset(), OperandFlags);
Chris Lattner18c59872009-06-27 04:16:01 +00007602 SDValue Offset = DAG.getNode(WrapperKind, dl, PtrVT, TGA);
Lauro Ramos Venancio7d2cc2b2007-04-22 22:50:52 +00007603
Hans Wennborg228756c2012-05-11 10:11:01 +00007604 if (model == TLSModel::InitialExec) {
7605 if (isPIC && !is64Bit) {
7606 Offset = DAG.getNode(ISD::ADD, dl, PtrVT,
7607 DAG.getNode(X86ISD::GlobalBaseReg, DebugLoc(), PtrVT),
7608 Offset);
Hans Wennborg228756c2012-05-11 10:11:01 +00007609 }
Rafael Espindola94e3b382012-06-29 04:22:35 +00007610
7611 Offset = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Offset,
7612 MachinePointerInfo::getGOT(), false, false, false,
7613 0);
Hans Wennborg228756c2012-05-11 10:11:01 +00007614 }
Lauro Ramos Venancio7d2cc2b2007-04-22 22:50:52 +00007615
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007616 // The address of the thread local variable is the add of the thread
7617 // pointer with the offset of the variable.
Dale Johannesen33c960f2009-02-04 20:06:27 +00007618 return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007619}
7620
Dan Gohman475871a2008-07-27 21:46:04 +00007621SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007622X86TargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const {
Michael J. Spencerec38de22010-10-10 22:04:20 +00007623
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007624 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
Chris Lattnerb903bed2009-06-26 21:20:29 +00007625 const GlobalValue *GV = GA->getGlobal();
Eric Christopherfd179292009-08-27 18:07:15 +00007626
Eric Christopher30ef0e52010-06-03 04:07:48 +00007627 if (Subtarget->isTargetELF()) {
Chandler Carruth34797132012-04-08 17:20:55 +00007628 TLSModel::Model model = getTargetMachine().getTLSModel(GV);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007629
Eric Christopher30ef0e52010-06-03 04:07:48 +00007630 switch (model) {
7631 case TLSModel::GeneralDynamic:
Eric Christopher30ef0e52010-06-03 04:07:48 +00007632 if (Subtarget->is64Bit())
7633 return LowerToTLSGeneralDynamicModel64(GA, DAG, getPointerTy());
7634 return LowerToTLSGeneralDynamicModel32(GA, DAG, getPointerTy());
Hans Wennborgf0234fc2012-06-01 16:27:21 +00007635 case TLSModel::LocalDynamic:
7636 return LowerToTLSLocalDynamicModel(GA, DAG, getPointerTy(),
7637 Subtarget->is64Bit());
Eric Christopher30ef0e52010-06-03 04:07:48 +00007638 case TLSModel::InitialExec:
7639 case TLSModel::LocalExec:
7640 return LowerToTLSExecModel(GA, DAG, getPointerTy(), model,
Hans Wennborg228756c2012-05-11 10:11:01 +00007641 Subtarget->is64Bit(),
7642 getTargetMachine().getRelocationModel() == Reloc::PIC_);
Eric Christopher30ef0e52010-06-03 04:07:48 +00007643 }
Craig Toppere8eb1162012-04-23 03:26:18 +00007644 llvm_unreachable("Unknown TLS model.");
7645 }
7646
7647 if (Subtarget->isTargetDarwin()) {
Eric Christopher30ef0e52010-06-03 04:07:48 +00007648 // Darwin only has one model of TLS. Lower to that.
7649 unsigned char OpFlag = 0;
7650 unsigned WrapperKind = Subtarget->isPICStyleRIPRel() ?
7651 X86ISD::WrapperRIP : X86ISD::Wrapper;
Michael J. Spencerec38de22010-10-10 22:04:20 +00007652
Eric Christopher30ef0e52010-06-03 04:07:48 +00007653 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
7654 // global base reg.
7655 bool PIC32 = (getTargetMachine().getRelocationModel() == Reloc::PIC_) &&
7656 !Subtarget->is64Bit();
7657 if (PIC32)
7658 OpFlag = X86II::MO_TLVP_PIC_BASE;
7659 else
7660 OpFlag = X86II::MO_TLVP;
Michael J. Spencerec38de22010-10-10 22:04:20 +00007661 DebugLoc DL = Op.getDebugLoc();
Devang Patel0d881da2010-07-06 22:08:15 +00007662 SDValue Result = DAG.getTargetGlobalAddress(GA->getGlobal(), DL,
Eric Christopherd8c05362010-12-09 06:25:53 +00007663 GA->getValueType(0),
Eric Christopher30ef0e52010-06-03 04:07:48 +00007664 GA->getOffset(), OpFlag);
Eric Christopher30ef0e52010-06-03 04:07:48 +00007665 SDValue Offset = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007666
Eric Christopher30ef0e52010-06-03 04:07:48 +00007667 // With PIC32, the address is actually $g + Offset.
7668 if (PIC32)
7669 Offset = DAG.getNode(ISD::ADD, DL, getPointerTy(),
7670 DAG.getNode(X86ISD::GlobalBaseReg,
7671 DebugLoc(), getPointerTy()),
7672 Offset);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007673
Eric Christopher30ef0e52010-06-03 04:07:48 +00007674 // Lowering the machine isd will make sure everything is in the right
7675 // location.
Eric Christopherd8c05362010-12-09 06:25:53 +00007676 SDValue Chain = DAG.getEntryNode();
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00007677 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Eric Christopherd8c05362010-12-09 06:25:53 +00007678 SDValue Args[] = { Chain, Offset };
7679 Chain = DAG.getNode(X86ISD::TLSCALL, DL, NodeTys, Args, 2);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007680
Eric Christopher30ef0e52010-06-03 04:07:48 +00007681 // TLSCALL will be codegen'ed as call. Inform MFI that function has calls.
7682 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
7683 MFI->setAdjustsStack(true);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00007684
Eric Christopher30ef0e52010-06-03 04:07:48 +00007685 // And our return value (tls address) is in the standard call return value
7686 // location.
Eric Christopherd8c05362010-12-09 06:25:53 +00007687 unsigned Reg = Subtarget->is64Bit() ? X86::RAX : X86::EAX;
Evan Chengfd230df2011-10-19 22:22:54 +00007688 return DAG.getCopyFromReg(Chain, DL, Reg, getPointerTy(),
7689 Chain.getValue(1));
Craig Toppere8eb1162012-04-23 03:26:18 +00007690 }
7691
7692 if (Subtarget->isTargetWindows()) {
Anton Korobeynikovd4a19b62012-02-11 17:26:53 +00007693 // Just use the implicit TLS architecture
7694 // Need to generate someting similar to:
7695 // mov rdx, qword [gs:abs 58H]; Load pointer to ThreadLocalStorage
7696 // ; from TEB
7697 // mov ecx, dword [rel _tls_index]: Load index (from C runtime)
7698 // mov rcx, qword [rdx+rcx*8]
7699 // mov eax, .tls$:tlsvar
7700 // [rax+rcx] contains the address
7701 // Windows 64bit: gs:0x58
7702 // Windows 32bit: fs:__tls_array
7703
7704 // If GV is an alias then use the aliasee for determining
7705 // thread-localness.
7706 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV))
7707 GV = GA->resolveAliasedGlobal(false);
7708 DebugLoc dl = GA->getDebugLoc();
7709 SDValue Chain = DAG.getEntryNode();
7710
7711 // Get the Thread Pointer, which is %fs:__tls_array (32-bit) or
7712 // %gs:0x58 (64-bit).
7713 Value *Ptr = Constant::getNullValue(Subtarget->is64Bit()
7714 ? Type::getInt8PtrTy(*DAG.getContext(),
7715 256)
7716 : Type::getInt32PtrTy(*DAG.getContext(),
7717 257));
7718
7719 SDValue ThreadPointer = DAG.getLoad(getPointerTy(), dl, Chain,
7720 Subtarget->is64Bit()
7721 ? DAG.getIntPtrConstant(0x58)
7722 : DAG.getExternalSymbol("_tls_array",
7723 getPointerTy()),
7724 MachinePointerInfo(Ptr),
7725 false, false, false, 0);
7726
7727 // Load the _tls_index variable
7728 SDValue IDX = DAG.getExternalSymbol("_tls_index", getPointerTy());
7729 if (Subtarget->is64Bit())
7730 IDX = DAG.getExtLoad(ISD::ZEXTLOAD, dl, getPointerTy(), Chain,
7731 IDX, MachinePointerInfo(), MVT::i32,
7732 false, false, 0);
7733 else
7734 IDX = DAG.getLoad(getPointerTy(), dl, Chain, IDX, MachinePointerInfo(),
7735 false, false, false, 0);
7736
Micah Villmow2c39b152012-10-15 16:24:29 +00007737 SDValue Scale = DAG.getConstant(Log2_64_Ceil(TD->getPointerSize(0)),
Craig Topper0fbf3642012-04-23 03:28:34 +00007738 getPointerTy());
Anton Korobeynikovd4a19b62012-02-11 17:26:53 +00007739 IDX = DAG.getNode(ISD::SHL, dl, getPointerTy(), IDX, Scale);
7740
7741 SDValue res = DAG.getNode(ISD::ADD, dl, getPointerTy(), ThreadPointer, IDX);
7742 res = DAG.getLoad(getPointerTy(), dl, Chain, res, MachinePointerInfo(),
7743 false, false, false, 0);
7744
7745 // Get the offset of start of .tls section
7746 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl,
7747 GA->getValueType(0),
7748 GA->getOffset(), X86II::MO_SECREL);
7749 SDValue Offset = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), TGA);
7750
7751 // The address of the thread local variable is the add of the thread
7752 // pointer with the offset of the variable.
7753 return DAG.getNode(ISD::ADD, dl, getPointerTy(), res, Offset);
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007754 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00007755
David Blaikie4d6ccb52012-01-20 21:51:11 +00007756 llvm_unreachable("TLS not implemented for this target.");
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007757}
7758
Evan Cheng0db9fe62006-04-25 20:13:52 +00007759
Chad Rosierb90d2a92012-01-03 23:19:12 +00007760/// LowerShiftParts - Lower SRA_PARTS and friends, which return two i32 values
7761/// and take a 2 x i32 value to shift plus a shift amount.
7762SDValue X86TargetLowering::LowerShiftParts(SDValue Op, SelectionDAG &DAG) const{
Dan Gohman4c1fa612008-03-03 22:22:09 +00007763 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
Owen Andersone50ed302009-08-10 22:56:29 +00007764 EVT VT = Op.getValueType();
Duncan Sands83ec4b62008-06-06 12:08:01 +00007765 unsigned VTBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007766 DebugLoc dl = Op.getDebugLoc();
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007767 bool isSRA = Op.getOpcode() == ISD::SRA_PARTS;
Dan Gohman475871a2008-07-27 21:46:04 +00007768 SDValue ShOpLo = Op.getOperand(0);
7769 SDValue ShOpHi = Op.getOperand(1);
7770 SDValue ShAmt = Op.getOperand(2);
Chris Lattner31dcfe62009-07-29 05:48:09 +00007771 SDValue Tmp1 = isSRA ? DAG.getNode(ISD::SRA, dl, VT, ShOpHi,
Owen Anderson825b72b2009-08-11 20:47:22 +00007772 DAG.getConstant(VTBits - 1, MVT::i8))
Chris Lattner31dcfe62009-07-29 05:48:09 +00007773 : DAG.getConstant(0, VT);
Evan Chenge3413162006-01-09 18:33:28 +00007774
Dan Gohman475871a2008-07-27 21:46:04 +00007775 SDValue Tmp2, Tmp3;
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007776 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dale Johannesenace16102009-02-03 19:33:06 +00007777 Tmp2 = DAG.getNode(X86ISD::SHLD, dl, VT, ShOpHi, ShOpLo, ShAmt);
7778 Tmp3 = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ShAmt);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007779 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00007780 Tmp2 = DAG.getNode(X86ISD::SHRD, dl, VT, ShOpLo, ShOpHi, ShAmt);
7781 Tmp3 = DAG.getNode(isSRA ? ISD::SRA : ISD::SRL, dl, VT, ShOpHi, ShAmt);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007782 }
Evan Chenge3413162006-01-09 18:33:28 +00007783
Owen Anderson825b72b2009-08-11 20:47:22 +00007784 SDValue AndNode = DAG.getNode(ISD::AND, dl, MVT::i8, ShAmt,
7785 DAG.getConstant(VTBits, MVT::i8));
Chris Lattnerccfea352010-02-22 00:28:59 +00007786 SDValue Cond = DAG.getNode(X86ISD::CMP, dl, MVT::i32,
Owen Anderson825b72b2009-08-11 20:47:22 +00007787 AndNode, DAG.getConstant(0, MVT::i8));
Evan Chenge3413162006-01-09 18:33:28 +00007788
Dan Gohman475871a2008-07-27 21:46:04 +00007789 SDValue Hi, Lo;
Owen Anderson825b72b2009-08-11 20:47:22 +00007790 SDValue CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Dan Gohman475871a2008-07-27 21:46:04 +00007791 SDValue Ops0[4] = { Tmp2, Tmp3, CC, Cond };
7792 SDValue Ops1[4] = { Tmp3, Tmp1, CC, Cond };
Duncan Sandsf9516202008-06-30 10:19:09 +00007793
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007794 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dale Johannesenace16102009-02-03 19:33:06 +00007795 Hi = DAG.getNode(X86ISD::CMOV, dl, VT, Ops0, 4);
7796 Lo = DAG.getNode(X86ISD::CMOV, dl, VT, Ops1, 4);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007797 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00007798 Lo = DAG.getNode(X86ISD::CMOV, dl, VT, Ops0, 4);
7799 Hi = DAG.getNode(X86ISD::CMOV, dl, VT, Ops1, 4);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007800 }
7801
Dan Gohman475871a2008-07-27 21:46:04 +00007802 SDValue Ops[2] = { Lo, Hi };
Dale Johannesenace16102009-02-03 19:33:06 +00007803 return DAG.getMergeValues(Ops, 2, dl);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007804}
Evan Chenga3195e82006-01-12 22:54:21 +00007805
Dan Gohmand858e902010-04-17 15:26:15 +00007806SDValue X86TargetLowering::LowerSINT_TO_FP(SDValue Op,
7807 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00007808 EVT SrcVT = Op.getOperand(0).getValueType();
Eli Friedman23ef1052009-06-06 03:57:58 +00007809
Dale Johannesen0488fb62010-09-30 23:57:10 +00007810 if (SrcVT.isVector())
Eli Friedman23ef1052009-06-06 03:57:58 +00007811 return SDValue();
Eli Friedman23ef1052009-06-06 03:57:58 +00007812
Owen Anderson825b72b2009-08-11 20:47:22 +00007813 assert(SrcVT.getSimpleVT() <= MVT::i64 && SrcVT.getSimpleVT() >= MVT::i16 &&
Chris Lattnerb09916b2008-02-27 05:57:41 +00007814 "Unknown SINT_TO_FP to lower!");
Scott Michelfdc40a02009-02-17 22:15:04 +00007815
Eli Friedman36df4992009-05-27 00:47:34 +00007816 // These are really Legal; return the operand so the caller accepts it as
7817 // Legal.
Owen Anderson825b72b2009-08-11 20:47:22 +00007818 if (SrcVT == MVT::i32 && isScalarFPTypeInSSEReg(Op.getValueType()))
Eli Friedman36df4992009-05-27 00:47:34 +00007819 return Op;
Owen Anderson825b72b2009-08-11 20:47:22 +00007820 if (SrcVT == MVT::i64 && isScalarFPTypeInSSEReg(Op.getValueType()) &&
Eli Friedman36df4992009-05-27 00:47:34 +00007821 Subtarget->is64Bit()) {
7822 return Op;
7823 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007824
Bruno Cardoso Lopesa511b8e2011-08-09 17:39:01 +00007825 DebugLoc dl = Op.getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +00007826 unsigned Size = SrcVT.getSizeInBits()/8;
Evan Cheng0db9fe62006-04-25 20:13:52 +00007827 MachineFunction &MF = DAG.getMachineFunction();
David Greene3f2bf852009-11-12 20:49:22 +00007828 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size, false);
Dan Gohman475871a2008-07-27 21:46:04 +00007829 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Dale Johannesenace16102009-02-03 19:33:06 +00007830 SDValue Chain = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Bill Wendling105be5a2009-03-13 08:41:47 +00007831 StackSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00007832 MachinePointerInfo::getFixedStack(SSFI),
David Greene67c9d422010-02-15 16:53:33 +00007833 false, false, 0);
Eli Friedman948e95a2009-05-23 09:59:16 +00007834 return BuildFILD(Op, SrcVT, Chain, StackSlot, DAG);
7835}
Evan Cheng0db9fe62006-04-25 20:13:52 +00007836
Owen Andersone50ed302009-08-10 22:56:29 +00007837SDValue X86TargetLowering::BuildFILD(SDValue Op, EVT SrcVT, SDValue Chain,
Michael J. Spencerec38de22010-10-10 22:04:20 +00007838 SDValue StackSlot,
Dan Gohmand858e902010-04-17 15:26:15 +00007839 SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00007840 // Build the FILD
Chris Lattner492a43e2010-09-22 01:28:21 +00007841 DebugLoc DL = Op.getDebugLoc();
Chris Lattner5a88b832007-02-25 07:10:00 +00007842 SDVTList Tys;
Chris Lattner78631162008-01-16 06:24:21 +00007843 bool useSSE = isScalarFPTypeInSSEReg(Op.getValueType());
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00007844 if (useSSE)
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00007845 Tys = DAG.getVTList(MVT::f64, MVT::Other, MVT::Glue);
Chris Lattner5a88b832007-02-25 07:10:00 +00007846 else
Owen Anderson825b72b2009-08-11 20:47:22 +00007847 Tys = DAG.getVTList(Op.getValueType(), MVT::Other);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007848
Chris Lattner492a43e2010-09-22 01:28:21 +00007849 unsigned ByteSize = SrcVT.getSizeInBits()/8;
Michael J. Spencerec38de22010-10-10 22:04:20 +00007850
Stuart Hastings84be9582011-06-02 15:57:11 +00007851 FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(StackSlot);
7852 MachineMemOperand *MMO;
7853 if (FI) {
7854 int SSFI = FI->getIndex();
7855 MMO =
7856 DAG.getMachineFunction()
7857 .getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
7858 MachineMemOperand::MOLoad, ByteSize, ByteSize);
7859 } else {
7860 MMO = cast<LoadSDNode>(StackSlot)->getMemOperand();
7861 StackSlot = StackSlot.getOperand(1);
7862 }
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00007863 SDValue Ops[] = { Chain, StackSlot, DAG.getValueType(SrcVT) };
Chris Lattner492a43e2010-09-22 01:28:21 +00007864 SDValue Result = DAG.getMemIntrinsicNode(useSSE ? X86ISD::FILD_FLAG :
7865 X86ISD::FILD, DL,
7866 Tys, Ops, array_lengthof(Ops),
7867 SrcVT, MMO);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007868
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00007869 if (useSSE) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00007870 Chain = Result.getValue(1);
Dan Gohman475871a2008-07-27 21:46:04 +00007871 SDValue InFlag = Result.getValue(2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007872
7873 // FIXME: Currently the FST is flagged to the FILD_FLAG. This
7874 // shouldn't be necessary except that RFP cannot be live across
7875 // multiple blocks. When stackifier is fixed, they can be uncoupled.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00007876 MachineFunction &MF = DAG.getMachineFunction();
Bob Wilsoneafca4e2010-09-22 17:35:14 +00007877 unsigned SSFISize = Op.getValueType().getSizeInBits()/8;
7878 int SSFI = MF.getFrameInfo()->CreateStackObject(SSFISize, SSFISize, false);
Dan Gohman475871a2008-07-27 21:46:04 +00007879 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Owen Anderson825b72b2009-08-11 20:47:22 +00007880 Tys = DAG.getVTList(MVT::Other);
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00007881 SDValue Ops[] = {
7882 Chain, Result, StackSlot, DAG.getValueType(Op.getValueType()), InFlag
7883 };
Chris Lattner492a43e2010-09-22 01:28:21 +00007884 MachineMemOperand *MMO =
7885 DAG.getMachineFunction()
7886 .getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
Bob Wilsoneafca4e2010-09-22 17:35:14 +00007887 MachineMemOperand::MOStore, SSFISize, SSFISize);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007888
Chris Lattner492a43e2010-09-22 01:28:21 +00007889 Chain = DAG.getMemIntrinsicNode(X86ISD::FST, DL, Tys,
7890 Ops, array_lengthof(Ops),
7891 Op.getValueType(), MMO);
7892 Result = DAG.getLoad(Op.getValueType(), DL, Chain, StackSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00007893 MachinePointerInfo::getFixedStack(SSFI),
Pete Cooperd752e0f2011-11-08 18:42:53 +00007894 false, false, false, 0);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00007895 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00007896
Evan Cheng0db9fe62006-04-25 20:13:52 +00007897 return Result;
7898}
7899
Bill Wendling8b8a6362009-01-17 03:56:04 +00007900// LowerUINT_TO_FP_i64 - 64-bit unsigned integer to double expansion.
Dan Gohmand858e902010-04-17 15:26:15 +00007901SDValue X86TargetLowering::LowerUINT_TO_FP_i64(SDValue Op,
7902 SelectionDAG &DAG) const {
Bill Wendling397ae212012-01-05 02:13:20 +00007903 // This algorithm is not obvious. Here it is what we're trying to output:
Bill Wendling8b8a6362009-01-17 03:56:04 +00007904 /*
Bill Wendling397ae212012-01-05 02:13:20 +00007905 movq %rax, %xmm0
7906 punpckldq (c0), %xmm0 // c0: (uint4){ 0x43300000U, 0x45300000U, 0U, 0U }
7907 subpd (c1), %xmm0 // c1: (double2){ 0x1.0p52, 0x1.0p52 * 0x1.0p32 }
7908 #ifdef __SSE3__
Chad Rosiera20e1e72012-08-01 18:39:17 +00007909 haddpd %xmm0, %xmm0
Bill Wendling397ae212012-01-05 02:13:20 +00007910 #else
Chad Rosiera20e1e72012-08-01 18:39:17 +00007911 pshufd $0x4e, %xmm0, %xmm1
Bill Wendling397ae212012-01-05 02:13:20 +00007912 addpd %xmm1, %xmm0
7913 #endif
Bill Wendling8b8a6362009-01-17 03:56:04 +00007914 */
Dale Johannesen040225f2008-10-21 23:07:49 +00007915
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007916 DebugLoc dl = Op.getDebugLoc();
Owen Andersona90b3dc2009-07-15 21:51:10 +00007917 LLVMContext *Context = DAG.getContext();
Dale Johannesenace16102009-02-03 19:33:06 +00007918
Dale Johannesen1c15bf52008-10-21 20:50:01 +00007919 // Build some magic constants.
Chris Lattner7302d802012-02-06 21:56:39 +00007920 const uint32_t CV0[] = { 0x43300000, 0x45300000, 0, 0 };
7921 Constant *C0 = ConstantDataVector::get(*Context, CV0);
Evan Cheng1606e8e2009-03-13 07:51:59 +00007922 SDValue CPIdx0 = DAG.getConstantPool(C0, getPointerTy(), 16);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00007923
Chris Lattner97484792012-01-25 09:56:22 +00007924 SmallVector<Constant*,2> CV1;
7925 CV1.push_back(
Chris Lattner4ca829e2012-01-25 06:02:56 +00007926 ConstantFP::get(*Context, APFloat(APInt(64, 0x4330000000000000ULL))));
Chris Lattner97484792012-01-25 09:56:22 +00007927 CV1.push_back(
7928 ConstantFP::get(*Context, APFloat(APInt(64, 0x4530000000000000ULL))));
7929 Constant *C1 = ConstantVector::get(CV1);
Evan Cheng1606e8e2009-03-13 07:51:59 +00007930 SDValue CPIdx1 = DAG.getConstantPool(C1, getPointerTy(), 16);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00007931
Bill Wendling397ae212012-01-05 02:13:20 +00007932 // Load the 64-bit value into an XMM register.
7933 SDValue XR1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i64,
7934 Op.getOperand(0));
Owen Anderson825b72b2009-08-11 20:47:22 +00007935 SDValue CLod0 = DAG.getLoad(MVT::v4i32, dl, DAG.getEntryNode(), CPIdx0,
Chris Lattnere8639032010-09-21 06:22:23 +00007936 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00007937 false, false, false, 16);
Bill Wendling397ae212012-01-05 02:13:20 +00007938 SDValue Unpck1 = getUnpackl(DAG, dl, MVT::v4i32,
7939 DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, XR1),
7940 CLod0);
7941
Owen Anderson825b72b2009-08-11 20:47:22 +00007942 SDValue CLod1 = DAG.getLoad(MVT::v2f64, dl, CLod0.getValue(1), CPIdx1,
Chris Lattnere8639032010-09-21 06:22:23 +00007943 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00007944 false, false, false, 16);
Bill Wendling397ae212012-01-05 02:13:20 +00007945 SDValue XR2F = DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Unpck1);
Owen Anderson825b72b2009-08-11 20:47:22 +00007946 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::v2f64, XR2F, CLod1);
Bill Wendling397ae212012-01-05 02:13:20 +00007947 SDValue Result;
Bill Wendling8b8a6362009-01-17 03:56:04 +00007948
Craig Topperd0a31172012-01-10 06:37:29 +00007949 if (Subtarget->hasSSE3()) {
Bill Wendling397ae212012-01-05 02:13:20 +00007950 // FIXME: The 'haddpd' instruction may be slower than 'movhlps + addsd'.
7951 Result = DAG.getNode(X86ISD::FHADD, dl, MVT::v2f64, Sub, Sub);
7952 } else {
7953 SDValue S2F = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, Sub);
7954 SDValue Shuffle = getTargetShuffleNode(X86ISD::PSHUFD, dl, MVT::v4i32,
7955 S2F, 0x4E, DAG);
7956 Result = DAG.getNode(ISD::FADD, dl, MVT::v2f64,
7957 DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Shuffle),
7958 Sub);
7959 }
7960
7961 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Result,
Dale Johannesen1c15bf52008-10-21 20:50:01 +00007962 DAG.getIntPtrConstant(0));
7963}
7964
Bill Wendling8b8a6362009-01-17 03:56:04 +00007965// LowerUINT_TO_FP_i32 - 32-bit unsigned integer to float expansion.
Dan Gohmand858e902010-04-17 15:26:15 +00007966SDValue X86TargetLowering::LowerUINT_TO_FP_i32(SDValue Op,
7967 SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007968 DebugLoc dl = Op.getDebugLoc();
Bill Wendling8b8a6362009-01-17 03:56:04 +00007969 // FP constant to bias correct the final result.
7970 SDValue Bias = DAG.getConstantFP(BitsToDouble(0x4330000000000000ULL),
Owen Anderson825b72b2009-08-11 20:47:22 +00007971 MVT::f64);
Bill Wendling8b8a6362009-01-17 03:56:04 +00007972
7973 // Load the 32-bit value into an XMM register.
Owen Anderson825b72b2009-08-11 20:47:22 +00007974 SDValue Load = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
Eli Friedman6cdc1f42011-08-02 18:38:35 +00007975 Op.getOperand(0));
Bill Wendling8b8a6362009-01-17 03:56:04 +00007976
Eli Friedmanf3704762011-08-29 21:15:46 +00007977 // Zero out the upper parts of the register.
Craig Topper12216172012-01-13 08:12:35 +00007978 Load = getShuffleVectorZeroOrUndef(Load, 0, true, Subtarget, DAG);
Eli Friedmanf3704762011-08-29 21:15:46 +00007979
Owen Anderson825b72b2009-08-11 20:47:22 +00007980 Load = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007981 DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Load),
Bill Wendling8b8a6362009-01-17 03:56:04 +00007982 DAG.getIntPtrConstant(0));
7983
7984 // Or the load with the bias.
Owen Anderson825b72b2009-08-11 20:47:22 +00007985 SDValue Or = DAG.getNode(ISD::OR, dl, MVT::v2i64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007986 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00007987 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00007988 MVT::v2f64, Load)),
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007989 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00007990 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00007991 MVT::v2f64, Bias)));
7992 Or = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007993 DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Or),
Bill Wendling8b8a6362009-01-17 03:56:04 +00007994 DAG.getIntPtrConstant(0));
7995
7996 // Subtract the bias.
Owen Anderson825b72b2009-08-11 20:47:22 +00007997 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::f64, Or, Bias);
Bill Wendling8b8a6362009-01-17 03:56:04 +00007998
7999 // Handle final rounding.
Owen Andersone50ed302009-08-10 22:56:29 +00008000 EVT DestVT = Op.getValueType();
Bill Wendling030939c2009-01-17 07:40:19 +00008001
Craig Topper69947b92012-04-23 06:57:04 +00008002 if (DestVT.bitsLT(MVT::f64))
Dale Johannesenace16102009-02-03 19:33:06 +00008003 return DAG.getNode(ISD::FP_ROUND, dl, DestVT, Sub,
Bill Wendling030939c2009-01-17 07:40:19 +00008004 DAG.getIntPtrConstant(0));
Craig Topper69947b92012-04-23 06:57:04 +00008005 if (DestVT.bitsGT(MVT::f64))
Dale Johannesenace16102009-02-03 19:33:06 +00008006 return DAG.getNode(ISD::FP_EXTEND, dl, DestVT, Sub);
Bill Wendling030939c2009-01-17 07:40:19 +00008007
8008 // Handle final rounding.
8009 return Sub;
Bill Wendling8b8a6362009-01-17 03:56:04 +00008010}
8011
Dan Gohmand858e902010-04-17 15:26:15 +00008012SDValue X86TargetLowering::LowerUINT_TO_FP(SDValue Op,
8013 SelectionDAG &DAG) const {
Evan Chenga06ec9e2009-01-19 08:08:22 +00008014 SDValue N0 = Op.getOperand(0);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008015 DebugLoc dl = Op.getDebugLoc();
Bill Wendling8b8a6362009-01-17 03:56:04 +00008016
Dale Johannesen8d908eb2010-05-15 18:51:12 +00008017 // Since UINT_TO_FP is legal (it's marked custom), dag combiner won't
Evan Chenga06ec9e2009-01-19 08:08:22 +00008018 // optimize it to a SINT_TO_FP when the sign bit is known zero. Perform
8019 // the optimization here.
8020 if (DAG.SignBitIsZero(N0))
Dale Johannesenace16102009-02-03 19:33:06 +00008021 return DAG.getNode(ISD::SINT_TO_FP, dl, Op.getValueType(), N0);
Evan Chenga06ec9e2009-01-19 08:08:22 +00008022
Owen Andersone50ed302009-08-10 22:56:29 +00008023 EVT SrcVT = N0.getValueType();
Dale Johannesen8d908eb2010-05-15 18:51:12 +00008024 EVT DstVT = Op.getValueType();
8025 if (SrcVT == MVT::i64 && DstVT == MVT::f64 && X86ScalarSSEf64)
Bill Wendling8b8a6362009-01-17 03:56:04 +00008026 return LowerUINT_TO_FP_i64(Op, DAG);
Craig Topper69947b92012-04-23 06:57:04 +00008027 if (SrcVT == MVT::i32 && X86ScalarSSEf64)
Bill Wendling8b8a6362009-01-17 03:56:04 +00008028 return LowerUINT_TO_FP_i32(Op, DAG);
Craig Topper69947b92012-04-23 06:57:04 +00008029 if (Subtarget->is64Bit() && SrcVT == MVT::i64 && DstVT == MVT::f32)
Bill Wendling397ae212012-01-05 02:13:20 +00008030 return SDValue();
Eli Friedman948e95a2009-05-23 09:59:16 +00008031
8032 // Make a 64-bit buffer, and use it to build an FILD.
Owen Anderson825b72b2009-08-11 20:47:22 +00008033 SDValue StackSlot = DAG.CreateStackTemporary(MVT::i64);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00008034 if (SrcVT == MVT::i32) {
8035 SDValue WordOff = DAG.getConstant(4, getPointerTy());
8036 SDValue OffsetSlot = DAG.getNode(ISD::ADD, dl,
8037 getPointerTy(), StackSlot, WordOff);
8038 SDValue Store1 = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Chris Lattner8026a9d2010-09-21 17:50:43 +00008039 StackSlot, MachinePointerInfo(),
8040 false, false, 0);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00008041 SDValue Store2 = DAG.getStore(Store1, dl, DAG.getConstant(0, MVT::i32),
Chris Lattner8026a9d2010-09-21 17:50:43 +00008042 OffsetSlot, MachinePointerInfo(),
8043 false, false, 0);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00008044 SDValue Fild = BuildFILD(Op, MVT::i64, Store2, StackSlot, DAG);
8045 return Fild;
8046 }
8047
8048 assert(SrcVT == MVT::i64 && "Unexpected type in UINT_TO_FP");
8049 SDValue Store = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Bill Wendlingf6c07472012-01-10 19:41:30 +00008050 StackSlot, MachinePointerInfo(),
Chris Lattner8026a9d2010-09-21 17:50:43 +00008051 false, false, 0);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00008052 // For i64 source, we need to add the appropriate power of 2 if the input
8053 // was negative. This is the same as the optimization in
8054 // DAGTypeLegalizer::ExpandIntOp_UNIT_TO_FP, and for it to be safe here,
8055 // we must be careful to do the computation in x87 extended precision, not
8056 // in SSE. (The generic code can't know it's OK to do this, or how to.)
Chris Lattner492a43e2010-09-22 01:28:21 +00008057 int SSFI = cast<FrameIndexSDNode>(StackSlot)->getIndex();
8058 MachineMemOperand *MMO =
8059 DAG.getMachineFunction()
8060 .getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
8061 MachineMemOperand::MOLoad, 8, 8);
Michael J. Spencerec38de22010-10-10 22:04:20 +00008062
Dale Johannesen8d908eb2010-05-15 18:51:12 +00008063 SDVTList Tys = DAG.getVTList(MVT::f80, MVT::Other);
8064 SDValue Ops[] = { Store, StackSlot, DAG.getValueType(MVT::i64) };
Chris Lattner492a43e2010-09-22 01:28:21 +00008065 SDValue Fild = DAG.getMemIntrinsicNode(X86ISD::FILD, dl, Tys, Ops, 3,
8066 MVT::i64, MMO);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00008067
8068 APInt FF(32, 0x5F800000ULL);
8069
8070 // Check whether the sign bit is set.
8071 SDValue SignSet = DAG.getSetCC(dl, getSetCCResultType(MVT::i64),
8072 Op.getOperand(0), DAG.getConstant(0, MVT::i64),
8073 ISD::SETLT);
8074
8075 // Build a 64 bit pair (0, FF) in the constant pool, with FF in the lo bits.
8076 SDValue FudgePtr = DAG.getConstantPool(
8077 ConstantInt::get(*DAG.getContext(), FF.zext(64)),
8078 getPointerTy());
8079
8080 // Get a pointer to FF if the sign bit was set, or to 0 otherwise.
8081 SDValue Zero = DAG.getIntPtrConstant(0);
8082 SDValue Four = DAG.getIntPtrConstant(4);
8083 SDValue Offset = DAG.getNode(ISD::SELECT, dl, Zero.getValueType(), SignSet,
8084 Zero, Four);
8085 FudgePtr = DAG.getNode(ISD::ADD, dl, getPointerTy(), FudgePtr, Offset);
8086
8087 // Load the value out, extending it from f32 to f80.
8088 // FIXME: Avoid the extend by constructing the right constant pool?
Stuart Hastingsa9011292011-02-16 16:23:55 +00008089 SDValue Fudge = DAG.getExtLoad(ISD::EXTLOAD, dl, MVT::f80, DAG.getEntryNode(),
Chris Lattnere8639032010-09-21 06:22:23 +00008090 FudgePtr, MachinePointerInfo::getConstantPool(),
8091 MVT::f32, false, false, 4);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00008092 // Extend everything to 80 bits to force it to be done on x87.
8093 SDValue Add = DAG.getNode(ISD::FADD, dl, MVT::f80, Fild, Fudge);
8094 return DAG.getNode(ISD::FP_ROUND, dl, DstVT, Add, DAG.getIntPtrConstant(0));
Bill Wendling8b8a6362009-01-17 03:56:04 +00008095}
8096
Dan Gohman475871a2008-07-27 21:46:04 +00008097std::pair<SDValue,SDValue> X86TargetLowering::
NAKAMURA Takumi9a68fdc2012-02-25 03:37:25 +00008098FP_TO_INTHelper(SDValue Op, SelectionDAG &DAG, bool IsSigned, bool IsReplace) const {
Chris Lattner07290932010-09-22 01:05:16 +00008099 DebugLoc DL = Op.getDebugLoc();
Eli Friedman948e95a2009-05-23 09:59:16 +00008100
Owen Andersone50ed302009-08-10 22:56:29 +00008101 EVT DstTy = Op.getValueType();
Eli Friedman948e95a2009-05-23 09:59:16 +00008102
Michael J. Spencer1a2d0612012-02-24 19:01:22 +00008103 if (!IsSigned && !isIntegerTypeFTOL(DstTy)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00008104 assert(DstTy == MVT::i32 && "Unexpected FP_TO_UINT");
8105 DstTy = MVT::i64;
Eli Friedman948e95a2009-05-23 09:59:16 +00008106 }
8107
Owen Anderson825b72b2009-08-11 20:47:22 +00008108 assert(DstTy.getSimpleVT() <= MVT::i64 &&
8109 DstTy.getSimpleVT() >= MVT::i16 &&
Michael J. Spencer1a2d0612012-02-24 19:01:22 +00008110 "Unknown FP_TO_INT to lower!");
Evan Cheng0db9fe62006-04-25 20:13:52 +00008111
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00008112 // These are really Legal.
Owen Anderson825b72b2009-08-11 20:47:22 +00008113 if (DstTy == MVT::i32 &&
Chris Lattner78631162008-01-16 06:24:21 +00008114 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Dan Gohman475871a2008-07-27 21:46:04 +00008115 return std::make_pair(SDValue(), SDValue());
Dale Johannesen73328d12007-09-19 23:55:34 +00008116 if (Subtarget->is64Bit() &&
Owen Anderson825b72b2009-08-11 20:47:22 +00008117 DstTy == MVT::i64 &&
Eli Friedman36df4992009-05-27 00:47:34 +00008118 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Dan Gohman475871a2008-07-27 21:46:04 +00008119 return std::make_pair(SDValue(), SDValue());
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00008120
Michael J. Spencer1a2d0612012-02-24 19:01:22 +00008121 // We lower FP->int64 either into FISTP64 followed by a load from a temporary
8122 // stack slot, or into the FTOL runtime function.
Evan Cheng87c89352007-10-15 20:11:21 +00008123 MachineFunction &MF = DAG.getMachineFunction();
Eli Friedman948e95a2009-05-23 09:59:16 +00008124 unsigned MemSize = DstTy.getSizeInBits()/8;
David Greene3f2bf852009-11-12 20:49:22 +00008125 int SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize, false);
Dan Gohman475871a2008-07-27 21:46:04 +00008126 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Eric Christopherfd179292009-08-27 18:07:15 +00008127
Evan Cheng0db9fe62006-04-25 20:13:52 +00008128 unsigned Opc;
Michael J. Spencer1a2d0612012-02-24 19:01:22 +00008129 if (!IsSigned && isIntegerTypeFTOL(DstTy))
8130 Opc = X86ISD::WIN_FTOL;
8131 else
8132 switch (DstTy.getSimpleVT().SimpleTy) {
8133 default: llvm_unreachable("Invalid FP_TO_SINT to lower!");
8134 case MVT::i16: Opc = X86ISD::FP_TO_INT16_IN_MEM; break;
8135 case MVT::i32: Opc = X86ISD::FP_TO_INT32_IN_MEM; break;
8136 case MVT::i64: Opc = X86ISD::FP_TO_INT64_IN_MEM; break;
8137 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00008138
Dan Gohman475871a2008-07-27 21:46:04 +00008139 SDValue Chain = DAG.getEntryNode();
8140 SDValue Value = Op.getOperand(0);
Chris Lattner492a43e2010-09-22 01:28:21 +00008141 EVT TheVT = Op.getOperand(0).getValueType();
Michael J. Spencer1a2d0612012-02-24 19:01:22 +00008142 // FIXME This causes a redundant load/store if the SSE-class value is already
8143 // in memory, such as if it is on the callstack.
Chris Lattner492a43e2010-09-22 01:28:21 +00008144 if (isScalarFPTypeInSSEReg(TheVT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00008145 assert(DstTy == MVT::i64 && "Invalid FP_TO_SINT to lower!");
Chris Lattner07290932010-09-22 01:05:16 +00008146 Chain = DAG.getStore(Chain, DL, Value, StackSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00008147 MachinePointerInfo::getFixedStack(SSFI),
David Greene67c9d422010-02-15 16:53:33 +00008148 false, false, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +00008149 SDVTList Tys = DAG.getVTList(Op.getOperand(0).getValueType(), MVT::Other);
Dan Gohman475871a2008-07-27 21:46:04 +00008150 SDValue Ops[] = {
Chris Lattner492a43e2010-09-22 01:28:21 +00008151 Chain, StackSlot, DAG.getValueType(TheVT)
Chris Lattner5a88b832007-02-25 07:10:00 +00008152 };
Michael J. Spencerec38de22010-10-10 22:04:20 +00008153
Chris Lattner492a43e2010-09-22 01:28:21 +00008154 MachineMemOperand *MMO =
8155 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
8156 MachineMemOperand::MOLoad, MemSize, MemSize);
8157 Value = DAG.getMemIntrinsicNode(X86ISD::FLD, DL, Tys, Ops, 3,
8158 DstTy, MMO);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008159 Chain = Value.getValue(1);
David Greene3f2bf852009-11-12 20:49:22 +00008160 SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize, false);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008161 StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
8162 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00008163
Chris Lattner07290932010-09-22 01:05:16 +00008164 MachineMemOperand *MMO =
8165 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
8166 MachineMemOperand::MOStore, MemSize, MemSize);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00008167
Michael J. Spencer1a2d0612012-02-24 19:01:22 +00008168 if (Opc != X86ISD::WIN_FTOL) {
8169 // Build the FP_TO_INT*_IN_MEM
8170 SDValue Ops[] = { Chain, Value, StackSlot };
8171 SDValue FIST = DAG.getMemIntrinsicNode(Opc, DL, DAG.getVTList(MVT::Other),
8172 Ops, 3, DstTy, MMO);
8173 return std::make_pair(FIST, StackSlot);
8174 } else {
8175 SDValue ftol = DAG.getNode(X86ISD::WIN_FTOL, DL,
8176 DAG.getVTList(MVT::Other, MVT::Glue),
8177 Chain, Value);
8178 SDValue eax = DAG.getCopyFromReg(ftol, DL, X86::EAX,
8179 MVT::i32, ftol.getValue(1));
8180 SDValue edx = DAG.getCopyFromReg(eax.getValue(1), DL, X86::EDX,
8181 MVT::i32, eax.getValue(2));
NAKAMURA Takumi9a68fdc2012-02-25 03:37:25 +00008182 SDValue Ops[] = { eax, edx };
8183 SDValue pair = IsReplace
8184 ? DAG.getNode(ISD::BUILD_PAIR, DL, MVT::i64, Ops, 2)
8185 : DAG.getMergeValues(Ops, 2, DL);
Michael J. Spencer1a2d0612012-02-24 19:01:22 +00008186 return std::make_pair(pair, SDValue());
8187 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00008188}
8189
Michael Liaobedcbd42012-10-16 18:14:11 +00008190SDValue X86TargetLowering::lowerTRUNCATE(SDValue Op, SelectionDAG &DAG) const {
8191 DebugLoc DL = Op.getDebugLoc();
8192 EVT VT = Op.getValueType();
8193 EVT SVT = Op.getOperand(0).getValueType();
8194
8195 if (!VT.is128BitVector() || !SVT.is256BitVector() ||
8196 VT.getVectorNumElements() != SVT.getVectorNumElements())
8197 return SDValue();
8198
8199 assert(Subtarget->hasAVX() && "256-bit vector is observed without AVX!");
8200
8201 unsigned NumElems = VT.getVectorNumElements();
8202 EVT NVT = EVT::getVectorVT(*DAG.getContext(), VT.getVectorElementType(),
8203 NumElems * 2);
8204
8205 SDValue In = Op.getOperand(0);
8206 SmallVector<int, 16> MaskVec(NumElems * 2, -1);
8207 // Prepare truncation shuffle mask
8208 for (unsigned i = 0; i != NumElems; ++i)
8209 MaskVec[i] = i * 2;
8210 SDValue V = DAG.getVectorShuffle(NVT, DL,
8211 DAG.getNode(ISD::BITCAST, DL, NVT, In),
8212 DAG.getUNDEF(NVT), &MaskVec[0]);
8213 return DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, VT, V,
8214 DAG.getIntPtrConstant(0));
8215}
8216
Dan Gohmand858e902010-04-17 15:26:15 +00008217SDValue X86TargetLowering::LowerFP_TO_SINT(SDValue Op,
8218 SelectionDAG &DAG) const {
Michael Liaobedcbd42012-10-16 18:14:11 +00008219 if (Op.getValueType().isVector()) {
8220 if (Op.getValueType() == MVT::v8i16)
8221 return DAG.getNode(ISD::TRUNCATE, Op.getDebugLoc(), Op.getValueType(),
8222 DAG.getNode(ISD::FP_TO_SINT, Op.getDebugLoc(),
8223 MVT::v8i32, Op.getOperand(0)));
Eli Friedman23ef1052009-06-06 03:57:58 +00008224 return SDValue();
Michael Liaobedcbd42012-10-16 18:14:11 +00008225 }
Eli Friedman23ef1052009-06-06 03:57:58 +00008226
NAKAMURA Takumi9a68fdc2012-02-25 03:37:25 +00008227 std::pair<SDValue,SDValue> Vals = FP_TO_INTHelper(Op, DAG,
8228 /*IsSigned=*/ true, /*IsReplace=*/ false);
Dan Gohman475871a2008-07-27 21:46:04 +00008229 SDValue FIST = Vals.first, StackSlot = Vals.second;
Eli Friedman36df4992009-05-27 00:47:34 +00008230 // If FP_TO_INTHelper failed, the node is actually supposed to be Legal.
8231 if (FIST.getNode() == 0) return Op;
Scott Michelfdc40a02009-02-17 22:15:04 +00008232
Michael J. Spencer1a2d0612012-02-24 19:01:22 +00008233 if (StackSlot.getNode())
8234 // Load the result.
8235 return DAG.getLoad(Op.getValueType(), Op.getDebugLoc(),
8236 FIST, StackSlot, MachinePointerInfo(),
8237 false, false, false, 0);
Craig Topper69947b92012-04-23 06:57:04 +00008238
8239 // The node is the result.
8240 return FIST;
Chris Lattner27a6c732007-11-24 07:07:01 +00008241}
8242
Dan Gohmand858e902010-04-17 15:26:15 +00008243SDValue X86TargetLowering::LowerFP_TO_UINT(SDValue Op,
8244 SelectionDAG &DAG) const {
NAKAMURA Takumi9a68fdc2012-02-25 03:37:25 +00008245 std::pair<SDValue,SDValue> Vals = FP_TO_INTHelper(Op, DAG,
8246 /*IsSigned=*/ false, /*IsReplace=*/ false);
Eli Friedman948e95a2009-05-23 09:59:16 +00008247 SDValue FIST = Vals.first, StackSlot = Vals.second;
8248 assert(FIST.getNode() && "Unexpected failure");
8249
NAKAMURA Takumi9a68fdc2012-02-25 03:37:25 +00008250 if (StackSlot.getNode())
8251 // Load the result.
8252 return DAG.getLoad(Op.getValueType(), Op.getDebugLoc(),
8253 FIST, StackSlot, MachinePointerInfo(),
8254 false, false, false, 0);
Craig Topper69947b92012-04-23 06:57:04 +00008255
8256 // The node is the result.
8257 return FIST;
Eli Friedman948e95a2009-05-23 09:59:16 +00008258}
8259
Michael Liao9d796db2012-10-10 16:32:15 +00008260SDValue X86TargetLowering::lowerFP_EXTEND(SDValue Op,
8261 SelectionDAG &DAG) const {
8262 DebugLoc DL = Op.getDebugLoc();
8263 EVT VT = Op.getValueType();
8264 SDValue In = Op.getOperand(0);
8265 EVT SVT = In.getValueType();
8266
8267 assert(SVT == MVT::v2f32 && "Only customize MVT::v2f32 type legalization!");
8268
8269 return DAG.getNode(X86ISD::VFPEXT, DL, VT,
8270 DAG.getNode(ISD::CONCAT_VECTORS, DL, MVT::v4f32,
8271 In, DAG.getUNDEF(SVT)));
8272}
8273
Craig Topper43620672012-09-08 07:31:51 +00008274SDValue X86TargetLowering::LowerFABS(SDValue Op, SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00008275 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008276 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00008277 EVT VT = Op.getValueType();
8278 EVT EltVT = VT;
Craig Topper43620672012-09-08 07:31:51 +00008279 unsigned NumElts = VT == MVT::f64 ? 2 : 4;
8280 if (VT.isVector()) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00008281 EltVT = VT.getVectorElementType();
Craig Topper43620672012-09-08 07:31:51 +00008282 NumElts = VT.getVectorNumElements();
Evan Cheng0db9fe62006-04-25 20:13:52 +00008283 }
Craig Topper43620672012-09-08 07:31:51 +00008284 Constant *C;
8285 if (EltVT == MVT::f64)
8286 C = ConstantFP::get(*Context, APFloat(APInt(64, ~(1ULL << 63))));
8287 else
8288 C = ConstantFP::get(*Context, APFloat(APInt(32, ~(1U << 31))));
8289 C = ConstantVector::getSplat(NumElts, C);
8290 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy());
8291 unsigned Alignment = cast<ConstantPoolSDNode>(CPIdx)->getAlignment();
Dale Johannesenace16102009-02-03 19:33:06 +00008292 SDValue Mask = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00008293 MachinePointerInfo::getConstantPool(),
Craig Topper43620672012-09-08 07:31:51 +00008294 false, false, false, Alignment);
8295 if (VT.isVector()) {
8296 MVT ANDVT = VT.is128BitVector() ? MVT::v2i64 : MVT::v4i64;
8297 return DAG.getNode(ISD::BITCAST, dl, VT,
8298 DAG.getNode(ISD::AND, dl, ANDVT,
8299 DAG.getNode(ISD::BITCAST, dl, ANDVT,
8300 Op.getOperand(0)),
8301 DAG.getNode(ISD::BITCAST, dl, ANDVT, Mask)));
8302 }
Dale Johannesenace16102009-02-03 19:33:06 +00008303 return DAG.getNode(X86ISD::FAND, dl, VT, Op.getOperand(0), Mask);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008304}
8305
Dan Gohmand858e902010-04-17 15:26:15 +00008306SDValue X86TargetLowering::LowerFNEG(SDValue Op, SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00008307 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008308 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00008309 EVT VT = Op.getValueType();
8310 EVT EltVT = VT;
Chad Rosiera860b182011-12-15 01:02:25 +00008311 unsigned NumElts = VT == MVT::f64 ? 2 : 4;
8312 if (VT.isVector()) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00008313 EltVT = VT.getVectorElementType();
Chad Rosiera860b182011-12-15 01:02:25 +00008314 NumElts = VT.getVectorNumElements();
8315 }
Chris Lattner4ca829e2012-01-25 06:02:56 +00008316 Constant *C;
8317 if (EltVT == MVT::f64)
8318 C = ConstantFP::get(*Context, APFloat(APInt(64, 1ULL << 63)));
8319 else
8320 C = ConstantFP::get(*Context, APFloat(APInt(32, 1U << 31)));
8321 C = ConstantVector::getSplat(NumElts, C);
Craig Toppercacd9d62012-09-08 07:46:05 +00008322 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy());
8323 unsigned Alignment = cast<ConstantPoolSDNode>(CPIdx)->getAlignment();
Dale Johannesenace16102009-02-03 19:33:06 +00008324 SDValue Mask = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00008325 MachinePointerInfo::getConstantPool(),
Craig Toppercacd9d62012-09-08 07:46:05 +00008326 false, false, false, Alignment);
Duncan Sands83ec4b62008-06-06 12:08:01 +00008327 if (VT.isVector()) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00008328 MVT XORVT = VT.is128BitVector() ? MVT::v2i64 : MVT::v4i64;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008329 return DAG.getNode(ISD::BITCAST, dl, VT,
Chad Rosiera860b182011-12-15 01:02:25 +00008330 DAG.getNode(ISD::XOR, dl, XORVT,
Craig Topper69947b92012-04-23 06:57:04 +00008331 DAG.getNode(ISD::BITCAST, dl, XORVT,
8332 Op.getOperand(0)),
8333 DAG.getNode(ISD::BITCAST, dl, XORVT, Mask)));
Evan Chengd4d01b72007-07-19 23:36:01 +00008334 }
Craig Topper69947b92012-04-23 06:57:04 +00008335
8336 return DAG.getNode(X86ISD::FXOR, dl, VT, Op.getOperand(0), Mask);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008337}
8338
Dan Gohmand858e902010-04-17 15:26:15 +00008339SDValue X86TargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00008340 LLVMContext *Context = DAG.getContext();
Dan Gohman475871a2008-07-27 21:46:04 +00008341 SDValue Op0 = Op.getOperand(0);
8342 SDValue Op1 = Op.getOperand(1);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008343 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00008344 EVT VT = Op.getValueType();
8345 EVT SrcVT = Op1.getValueType();
Evan Cheng73d6cf12007-01-05 21:37:56 +00008346
8347 // If second operand is smaller, extend it first.
Duncan Sands8e4eb092008-06-08 20:54:56 +00008348 if (SrcVT.bitsLT(VT)) {
Dale Johannesenace16102009-02-03 19:33:06 +00008349 Op1 = DAG.getNode(ISD::FP_EXTEND, dl, VT, Op1);
Evan Cheng73d6cf12007-01-05 21:37:56 +00008350 SrcVT = VT;
8351 }
Dale Johannesen61c7ef32007-10-21 01:07:44 +00008352 // And if it is bigger, shrink it first.
Duncan Sands8e4eb092008-06-08 20:54:56 +00008353 if (SrcVT.bitsGT(VT)) {
Dale Johannesenace16102009-02-03 19:33:06 +00008354 Op1 = DAG.getNode(ISD::FP_ROUND, dl, VT, Op1, DAG.getIntPtrConstant(1));
Dale Johannesen61c7ef32007-10-21 01:07:44 +00008355 SrcVT = VT;
Dale Johannesen61c7ef32007-10-21 01:07:44 +00008356 }
8357
8358 // At this point the operands and the result should have the same
8359 // type, and that won't be f80 since that is not custom lowered.
Evan Cheng73d6cf12007-01-05 21:37:56 +00008360
Evan Cheng68c47cb2007-01-05 07:55:56 +00008361 // First get the sign bit of second operand.
Chad Rosier01d426e2011-12-15 01:16:09 +00008362 SmallVector<Constant*,4> CV;
Owen Anderson825b72b2009-08-11 20:47:22 +00008363 if (SrcVT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00008364 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 1ULL << 63))));
8365 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 0))));
Evan Cheng68c47cb2007-01-05 07:55:56 +00008366 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00008367 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 1U << 31))));
8368 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
8369 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
8370 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
Evan Cheng68c47cb2007-01-05 07:55:56 +00008371 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00008372 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00008373 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00008374 SDValue Mask1 = DAG.getLoad(SrcVT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00008375 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00008376 false, false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00008377 SDValue SignBit = DAG.getNode(X86ISD::FAND, dl, SrcVT, Op1, Mask1);
Evan Cheng68c47cb2007-01-05 07:55:56 +00008378
8379 // Shift sign bit right or left if the two operands have different types.
Duncan Sands8e4eb092008-06-08 20:54:56 +00008380 if (SrcVT.bitsGT(VT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00008381 // Op0 is MVT::f32, Op1 is MVT::f64.
8382 SignBit = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f64, SignBit);
8383 SignBit = DAG.getNode(X86ISD::FSRL, dl, MVT::v2f64, SignBit,
8384 DAG.getConstant(32, MVT::i32));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008385 SignBit = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, SignBit);
Owen Anderson825b72b2009-08-11 20:47:22 +00008386 SignBit = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f32, SignBit,
Chris Lattner0bd48932008-01-17 07:00:52 +00008387 DAG.getIntPtrConstant(0));
Evan Cheng68c47cb2007-01-05 07:55:56 +00008388 }
8389
Evan Cheng73d6cf12007-01-05 21:37:56 +00008390 // Clear first operand sign bit.
8391 CV.clear();
Owen Anderson825b72b2009-08-11 20:47:22 +00008392 if (VT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00008393 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, ~(1ULL << 63)))));
8394 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 0))));
Evan Cheng73d6cf12007-01-05 21:37:56 +00008395 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00008396 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, ~(1U << 31)))));
8397 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
8398 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
8399 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
Evan Cheng73d6cf12007-01-05 21:37:56 +00008400 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00008401 C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00008402 CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00008403 SDValue Mask2 = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00008404 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00008405 false, false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00008406 SDValue Val = DAG.getNode(X86ISD::FAND, dl, VT, Op0, Mask2);
Evan Cheng73d6cf12007-01-05 21:37:56 +00008407
8408 // Or the value with the sign bit.
Dale Johannesenace16102009-02-03 19:33:06 +00008409 return DAG.getNode(X86ISD::FOR, dl, VT, Val, SignBit);
Evan Cheng68c47cb2007-01-05 07:55:56 +00008410}
8411
Craig Topper55b24052012-09-11 06:15:32 +00008412static SDValue LowerFGETSIGN(SDValue Op, SelectionDAG &DAG) {
Stuart Hastings4fd0dee2011-06-01 04:39:42 +00008413 SDValue N0 = Op.getOperand(0);
8414 DebugLoc dl = Op.getDebugLoc();
8415 EVT VT = Op.getValueType();
8416
8417 // Lower ISD::FGETSIGN to (AND (X86ISD::FGETSIGNx86 ...) 1).
8418 SDValue xFGETSIGN = DAG.getNode(X86ISD::FGETSIGNx86, dl, VT, N0,
8419 DAG.getConstant(1, VT));
8420 return DAG.getNode(ISD::AND, dl, VT, xFGETSIGN, DAG.getConstant(1, VT));
8421}
8422
Michael Liaof966e4e2012-09-13 20:24:54 +00008423// LowerVectorAllZeroTest - Check whether an OR'd tree is PTEST-able.
8424//
8425SDValue X86TargetLowering::LowerVectorAllZeroTest(SDValue Op, SelectionDAG &DAG) const {
8426 assert(Op.getOpcode() == ISD::OR && "Only check OR'd tree.");
8427
8428 if (!Subtarget->hasSSE41())
8429 return SDValue();
8430
8431 if (!Op->hasOneUse())
8432 return SDValue();
8433
8434 SDNode *N = Op.getNode();
8435 DebugLoc DL = N->getDebugLoc();
8436
8437 SmallVector<SDValue, 8> Opnds;
8438 DenseMap<SDValue, unsigned> VecInMap;
8439 EVT VT = MVT::Other;
8440
8441 // Recognize a special case where a vector is casted into wide integer to
8442 // test all 0s.
8443 Opnds.push_back(N->getOperand(0));
8444 Opnds.push_back(N->getOperand(1));
8445
8446 for (unsigned Slot = 0, e = Opnds.size(); Slot < e; ++Slot) {
8447 SmallVector<SDValue, 8>::const_iterator I = Opnds.begin() + Slot;
8448 // BFS traverse all OR'd operands.
8449 if (I->getOpcode() == ISD::OR) {
8450 Opnds.push_back(I->getOperand(0));
8451 Opnds.push_back(I->getOperand(1));
8452 // Re-evaluate the number of nodes to be traversed.
8453 e += 2; // 2 more nodes (LHS and RHS) are pushed.
8454 continue;
8455 }
8456
8457 // Quit if a non-EXTRACT_VECTOR_ELT
8458 if (I->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
8459 return SDValue();
8460
8461 // Quit if without a constant index.
8462 SDValue Idx = I->getOperand(1);
8463 if (!isa<ConstantSDNode>(Idx))
8464 return SDValue();
8465
8466 SDValue ExtractedFromVec = I->getOperand(0);
8467 DenseMap<SDValue, unsigned>::iterator M = VecInMap.find(ExtractedFromVec);
8468 if (M == VecInMap.end()) {
8469 VT = ExtractedFromVec.getValueType();
8470 // Quit if not 128/256-bit vector.
8471 if (!VT.is128BitVector() && !VT.is256BitVector())
8472 return SDValue();
8473 // Quit if not the same type.
8474 if (VecInMap.begin() != VecInMap.end() &&
8475 VT != VecInMap.begin()->first.getValueType())
8476 return SDValue();
8477 M = VecInMap.insert(std::make_pair(ExtractedFromVec, 0)).first;
8478 }
8479 M->second |= 1U << cast<ConstantSDNode>(Idx)->getZExtValue();
8480 }
8481
8482 assert((VT.is128BitVector() || VT.is256BitVector()) &&
Michael Liao9aba7ea2012-09-13 20:30:16 +00008483 "Not extracted from 128-/256-bit vector.");
Michael Liaof966e4e2012-09-13 20:24:54 +00008484
8485 unsigned FullMask = (1U << VT.getVectorNumElements()) - 1U;
8486 SmallVector<SDValue, 8> VecIns;
8487
8488 for (DenseMap<SDValue, unsigned>::const_iterator
8489 I = VecInMap.begin(), E = VecInMap.end(); I != E; ++I) {
8490 // Quit if not all elements are used.
8491 if (I->second != FullMask)
8492 return SDValue();
8493 VecIns.push_back(I->first);
8494 }
8495
8496 EVT TestVT = VT.is128BitVector() ? MVT::v2i64 : MVT::v4i64;
8497
8498 // Cast all vectors into TestVT for PTEST.
8499 for (unsigned i = 0, e = VecIns.size(); i < e; ++i)
8500 VecIns[i] = DAG.getNode(ISD::BITCAST, DL, TestVT, VecIns[i]);
8501
8502 // If more than one full vectors are evaluated, OR them first before PTEST.
8503 for (unsigned Slot = 0, e = VecIns.size(); e - Slot > 1; Slot += 2, e += 1) {
8504 // Each iteration will OR 2 nodes and append the result until there is only
8505 // 1 node left, i.e. the final OR'd value of all vectors.
8506 SDValue LHS = VecIns[Slot];
8507 SDValue RHS = VecIns[Slot + 1];
8508 VecIns.push_back(DAG.getNode(ISD::OR, DL, TestVT, LHS, RHS));
8509 }
8510
8511 return DAG.getNode(X86ISD::PTEST, DL, MVT::i32,
8512 VecIns.back(), VecIns.back());
8513}
8514
Dan Gohman076aee32009-03-04 19:44:21 +00008515/// Emit nodes that will be selected as "test Op0,Op0", or something
8516/// equivalent.
Dan Gohman31125812009-03-07 01:58:32 +00008517SDValue X86TargetLowering::EmitTest(SDValue Op, unsigned X86CC,
Evan Cheng552f09a2010-04-26 19:06:11 +00008518 SelectionDAG &DAG) const {
Dan Gohman076aee32009-03-04 19:44:21 +00008519 DebugLoc dl = Op.getDebugLoc();
8520
Dan Gohman31125812009-03-07 01:58:32 +00008521 // CF and OF aren't always set the way we want. Determine which
8522 // of these we need.
8523 bool NeedCF = false;
8524 bool NeedOF = false;
8525 switch (X86CC) {
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008526 default: break;
Dan Gohman31125812009-03-07 01:58:32 +00008527 case X86::COND_A: case X86::COND_AE:
8528 case X86::COND_B: case X86::COND_BE:
8529 NeedCF = true;
8530 break;
8531 case X86::COND_G: case X86::COND_GE:
8532 case X86::COND_L: case X86::COND_LE:
8533 case X86::COND_O: case X86::COND_NO:
8534 NeedOF = true;
8535 break;
Dan Gohman31125812009-03-07 01:58:32 +00008536 }
8537
Dan Gohman076aee32009-03-04 19:44:21 +00008538 // See if we can use the EFLAGS value from the operand instead of
Dan Gohman31125812009-03-07 01:58:32 +00008539 // doing a separate TEST. TEST always sets OF and CF to 0, so unless
8540 // we prove that the arithmetic won't overflow, we can't use OF or CF.
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008541 if (Op.getResNo() != 0 || NeedOF || NeedCF)
8542 // Emit a CMP with 0, which is the TEST pattern.
8543 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op,
8544 DAG.getConstant(0, Op.getValueType()));
8545
8546 unsigned Opcode = 0;
8547 unsigned NumOperands = 0;
Nadav Rotemb9d6b842012-08-18 17:53:03 +00008548
8549 // Truncate operations may prevent the merge of the SETCC instruction
8550 // and the arithmetic intruction before it. Attempt to truncate the operands
8551 // of the arithmetic instruction and use a reduced bit-width instruction.
8552 bool NeedTruncation = false;
8553 SDValue ArithOp = Op;
8554 if (Op->getOpcode() == ISD::TRUNCATE && Op->hasOneUse()) {
8555 SDValue Arith = Op->getOperand(0);
8556 // Both the trunc and the arithmetic op need to have one user each.
8557 if (Arith->hasOneUse())
8558 switch (Arith.getOpcode()) {
8559 default: break;
8560 case ISD::ADD:
8561 case ISD::SUB:
8562 case ISD::AND:
8563 case ISD::OR:
8564 case ISD::XOR: {
8565 NeedTruncation = true;
8566 ArithOp = Arith;
8567 }
8568 }
8569 }
8570
8571 // NOTICE: In the code below we use ArithOp to hold the arithmetic operation
8572 // which may be the result of a CAST. We use the variable 'Op', which is the
8573 // non-casted variable when we check for possible users.
8574 switch (ArithOp.getOpcode()) {
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008575 case ISD::ADD:
8576 // Due to an isel shortcoming, be conservative if this add is likely to be
8577 // selected as part of a load-modify-store instruction. When the root node
8578 // in a match is a store, isel doesn't know how to remap non-chain non-flag
8579 // uses of other nodes in the match, such as the ADD in this case. This
8580 // leads to the ADD being left around and reselected, with the result being
8581 // two adds in the output. Alas, even if none our users are stores, that
8582 // doesn't prove we're O.K. Ergo, if we have any parents that aren't
8583 // CopyToReg or SETCC, eschew INC/DEC. A better fix seems to require
8584 // climbing the DAG back to the root, and it doesn't seem to be worth the
8585 // effort.
8586 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
Pete Cooper2d496892011-11-15 21:57:53 +00008587 UE = Op.getNode()->use_end(); UI != UE; ++UI)
8588 if (UI->getOpcode() != ISD::CopyToReg &&
8589 UI->getOpcode() != ISD::SETCC &&
8590 UI->getOpcode() != ISD::STORE)
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008591 goto default_case;
8592
8593 if (ConstantSDNode *C =
Nadav Rotemb9d6b842012-08-18 17:53:03 +00008594 dyn_cast<ConstantSDNode>(ArithOp.getNode()->getOperand(1))) {
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008595 // An add of one will be selected as an INC.
8596 if (C->getAPIntValue() == 1) {
8597 Opcode = X86ISD::INC;
8598 NumOperands = 1;
8599 break;
Dan Gohmane220c4b2009-09-18 19:59:53 +00008600 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008601
8602 // An add of negative one (subtract of one) will be selected as a DEC.
8603 if (C->getAPIntValue().isAllOnesValue()) {
8604 Opcode = X86ISD::DEC;
8605 NumOperands = 1;
8606 break;
8607 }
Dan Gohman076aee32009-03-04 19:44:21 +00008608 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008609
8610 // Otherwise use a regular EFLAGS-setting add.
8611 Opcode = X86ISD::ADD;
8612 NumOperands = 2;
8613 break;
8614 case ISD::AND: {
8615 // If the primary and result isn't used, don't bother using X86ISD::AND,
8616 // because a TEST instruction will be better.
8617 bool NonFlagUse = false;
8618 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
8619 UE = Op.getNode()->use_end(); UI != UE; ++UI) {
8620 SDNode *User = *UI;
8621 unsigned UOpNo = UI.getOperandNo();
8622 if (User->getOpcode() == ISD::TRUNCATE && User->hasOneUse()) {
8623 // Look pass truncate.
8624 UOpNo = User->use_begin().getOperandNo();
8625 User = *User->use_begin();
8626 }
8627
8628 if (User->getOpcode() != ISD::BRCOND &&
8629 User->getOpcode() != ISD::SETCC &&
Nadav Rotemb9d6b842012-08-18 17:53:03 +00008630 !(User->getOpcode() == ISD::SELECT && UOpNo == 0)) {
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008631 NonFlagUse = true;
8632 break;
8633 }
Dan Gohman076aee32009-03-04 19:44:21 +00008634 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008635
8636 if (!NonFlagUse)
8637 break;
8638 }
8639 // FALL THROUGH
8640 case ISD::SUB:
8641 case ISD::OR:
8642 case ISD::XOR:
8643 // Due to the ISEL shortcoming noted above, be conservative if this op is
8644 // likely to be selected as part of a load-modify-store instruction.
8645 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
8646 UE = Op.getNode()->use_end(); UI != UE; ++UI)
8647 if (UI->getOpcode() == ISD::STORE)
8648 goto default_case;
8649
8650 // Otherwise use a regular EFLAGS-setting instruction.
Nadav Rotemb9d6b842012-08-18 17:53:03 +00008651 switch (ArithOp.getOpcode()) {
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008652 default: llvm_unreachable("unexpected operator!");
Nadav Rotemb9d6b842012-08-18 17:53:03 +00008653 case ISD::SUB: Opcode = X86ISD::SUB; break;
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008654 case ISD::XOR: Opcode = X86ISD::XOR; break;
8655 case ISD::AND: Opcode = X86ISD::AND; break;
Michael Liaof966e4e2012-09-13 20:24:54 +00008656 case ISD::OR: {
8657 if (!NeedTruncation && (X86CC == X86::COND_E || X86CC == X86::COND_NE)) {
8658 SDValue EFLAGS = LowerVectorAllZeroTest(Op, DAG);
8659 if (EFLAGS.getNode())
8660 return EFLAGS;
8661 }
8662 Opcode = X86ISD::OR;
8663 break;
8664 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008665 }
8666
8667 NumOperands = 2;
8668 break;
8669 case X86ISD::ADD:
8670 case X86ISD::SUB:
8671 case X86ISD::INC:
8672 case X86ISD::DEC:
8673 case X86ISD::OR:
8674 case X86ISD::XOR:
8675 case X86ISD::AND:
8676 return SDValue(Op.getNode(), 1);
8677 default:
8678 default_case:
8679 break;
Dan Gohman076aee32009-03-04 19:44:21 +00008680 }
8681
Nadav Rotemb9d6b842012-08-18 17:53:03 +00008682 // If we found that truncation is beneficial, perform the truncation and
8683 // update 'Op'.
8684 if (NeedTruncation) {
8685 EVT VT = Op.getValueType();
8686 SDValue WideVal = Op->getOperand(0);
8687 EVT WideVT = WideVal.getValueType();
8688 unsigned ConvertedOp = 0;
8689 // Use a target machine opcode to prevent further DAGCombine
8690 // optimizations that may separate the arithmetic operations
8691 // from the setcc node.
8692 switch (WideVal.getOpcode()) {
8693 default: break;
8694 case ISD::ADD: ConvertedOp = X86ISD::ADD; break;
8695 case ISD::SUB: ConvertedOp = X86ISD::SUB; break;
8696 case ISD::AND: ConvertedOp = X86ISD::AND; break;
8697 case ISD::OR: ConvertedOp = X86ISD::OR; break;
8698 case ISD::XOR: ConvertedOp = X86ISD::XOR; break;
8699 }
8700
8701 if (ConvertedOp) {
8702 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
8703 if (TLI.isOperationLegal(WideVal.getOpcode(), WideVT)) {
8704 SDValue V0 = DAG.getNode(ISD::TRUNCATE, dl, VT, WideVal.getOperand(0));
8705 SDValue V1 = DAG.getNode(ISD::TRUNCATE, dl, VT, WideVal.getOperand(1));
8706 Op = DAG.getNode(ConvertedOp, dl, VT, V0, V1);
8707 }
8708 }
8709 }
8710
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008711 if (Opcode == 0)
8712 // Emit a CMP with 0, which is the TEST pattern.
8713 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op,
8714 DAG.getConstant(0, Op.getValueType()));
8715
8716 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::i32);
8717 SmallVector<SDValue, 4> Ops;
8718 for (unsigned i = 0; i != NumOperands; ++i)
8719 Ops.push_back(Op.getOperand(i));
8720
8721 SDValue New = DAG.getNode(Opcode, dl, VTs, &Ops[0], NumOperands);
8722 DAG.ReplaceAllUsesWith(Op, New);
8723 return SDValue(New.getNode(), 1);
Dan Gohman076aee32009-03-04 19:44:21 +00008724}
8725
8726/// Emit nodes that will be selected as "cmp Op0,Op1", or something
8727/// equivalent.
Dan Gohman31125812009-03-07 01:58:32 +00008728SDValue X86TargetLowering::EmitCmp(SDValue Op0, SDValue Op1, unsigned X86CC,
Evan Cheng552f09a2010-04-26 19:06:11 +00008729 SelectionDAG &DAG) const {
Dan Gohman076aee32009-03-04 19:44:21 +00008730 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op1))
8731 if (C->getAPIntValue() == 0)
Evan Cheng552f09a2010-04-26 19:06:11 +00008732 return EmitTest(Op0, X86CC, DAG);
Dan Gohman076aee32009-03-04 19:44:21 +00008733
8734 DebugLoc dl = Op0.getDebugLoc();
Manman Ren39ad5682012-08-08 00:51:41 +00008735 if ((Op0.getValueType() == MVT::i8 || Op0.getValueType() == MVT::i16 ||
8736 Op0.getValueType() == MVT::i32 || Op0.getValueType() == MVT::i64)) {
8737 // Use SUB instead of CMP to enable CSE between SUB and CMP.
8738 SDVTList VTs = DAG.getVTList(Op0.getValueType(), MVT::i32);
8739 SDValue Sub = DAG.getNode(X86ISD::SUB, dl, VTs,
8740 Op0, Op1);
8741 return SDValue(Sub.getNode(), 1);
8742 }
Owen Anderson825b72b2009-08-11 20:47:22 +00008743 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op0, Op1);
Dan Gohman076aee32009-03-04 19:44:21 +00008744}
8745
Benjamin Kramer17c836c2012-04-27 12:07:43 +00008746/// Convert a comparison if required by the subtarget.
8747SDValue X86TargetLowering::ConvertCmpIfNecessary(SDValue Cmp,
8748 SelectionDAG &DAG) const {
8749 // If the subtarget does not support the FUCOMI instruction, floating-point
8750 // comparisons have to be converted.
8751 if (Subtarget->hasCMov() ||
8752 Cmp.getOpcode() != X86ISD::CMP ||
8753 !Cmp.getOperand(0).getValueType().isFloatingPoint() ||
8754 !Cmp.getOperand(1).getValueType().isFloatingPoint())
8755 return Cmp;
8756
8757 // The instruction selector will select an FUCOM instruction instead of
8758 // FUCOMI, which writes the comparison result to FPSW instead of EFLAGS. Hence
8759 // build an SDNode sequence that transfers the result from FPSW into EFLAGS:
8760 // (X86sahf (trunc (srl (X86fp_stsw (trunc (X86cmp ...)), 8))))
8761 DebugLoc dl = Cmp.getDebugLoc();
8762 SDValue TruncFPSW = DAG.getNode(ISD::TRUNCATE, dl, MVT::i16, Cmp);
8763 SDValue FNStSW = DAG.getNode(X86ISD::FNSTSW16r, dl, MVT::i16, TruncFPSW);
8764 SDValue Srl = DAG.getNode(ISD::SRL, dl, MVT::i16, FNStSW,
8765 DAG.getConstant(8, MVT::i8));
8766 SDValue TruncSrl = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Srl);
8767 return DAG.getNode(X86ISD::SAHF, dl, MVT::i32, TruncSrl);
8768}
8769
Evan Chengd40d03e2010-01-06 19:38:29 +00008770/// LowerToBT - Result of 'and' is compared against zero. Turn it into a BT node
8771/// if it's possible.
Evan Cheng5528e7b2010-04-21 01:47:12 +00008772SDValue X86TargetLowering::LowerToBT(SDValue And, ISD::CondCode CC,
8773 DebugLoc dl, SelectionDAG &DAG) const {
Evan Cheng2c755ba2010-02-27 07:36:59 +00008774 SDValue Op0 = And.getOperand(0);
8775 SDValue Op1 = And.getOperand(1);
8776 if (Op0.getOpcode() == ISD::TRUNCATE)
8777 Op0 = Op0.getOperand(0);
8778 if (Op1.getOpcode() == ISD::TRUNCATE)
8779 Op1 = Op1.getOperand(0);
8780
Evan Chengd40d03e2010-01-06 19:38:29 +00008781 SDValue LHS, RHS;
Dan Gohman6b13cbc2010-06-24 02:07:59 +00008782 if (Op1.getOpcode() == ISD::SHL)
8783 std::swap(Op0, Op1);
8784 if (Op0.getOpcode() == ISD::SHL) {
Evan Cheng2c755ba2010-02-27 07:36:59 +00008785 if (ConstantSDNode *And00C = dyn_cast<ConstantSDNode>(Op0.getOperand(0)))
8786 if (And00C->getZExtValue() == 1) {
Dan Gohman6b13cbc2010-06-24 02:07:59 +00008787 // If we looked past a truncate, check that it's only truncating away
8788 // known zeros.
8789 unsigned BitWidth = Op0.getValueSizeInBits();
8790 unsigned AndBitWidth = And.getValueSizeInBits();
8791 if (BitWidth > AndBitWidth) {
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00008792 APInt Zeros, Ones;
8793 DAG.ComputeMaskedBits(Op0, Zeros, Ones);
Dan Gohman6b13cbc2010-06-24 02:07:59 +00008794 if (Zeros.countLeadingOnes() < BitWidth - AndBitWidth)
8795 return SDValue();
8796 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00008797 LHS = Op1;
8798 RHS = Op0.getOperand(1);
Evan Chengd40d03e2010-01-06 19:38:29 +00008799 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00008800 } else if (Op1.getOpcode() == ISD::Constant) {
8801 ConstantSDNode *AndRHS = cast<ConstantSDNode>(Op1);
Benjamin Kramerf238f502011-11-23 13:54:17 +00008802 uint64_t AndRHSVal = AndRHS->getZExtValue();
Evan Cheng2c755ba2010-02-27 07:36:59 +00008803 SDValue AndLHS = Op0;
Benjamin Kramerf238f502011-11-23 13:54:17 +00008804
8805 if (AndRHSVal == 1 && AndLHS.getOpcode() == ISD::SRL) {
Evan Chengd40d03e2010-01-06 19:38:29 +00008806 LHS = AndLHS.getOperand(0);
8807 RHS = AndLHS.getOperand(1);
Dan Gohmane5af2d32009-01-29 01:59:02 +00008808 }
Benjamin Kramerf238f502011-11-23 13:54:17 +00008809
8810 // Use BT if the immediate can't be encoded in a TEST instruction.
8811 if (!isUInt<32>(AndRHSVal) && isPowerOf2_64(AndRHSVal)) {
8812 LHS = AndLHS;
8813 RHS = DAG.getConstant(Log2_64_Ceil(AndRHSVal), LHS.getValueType());
8814 }
Evan Chengd40d03e2010-01-06 19:38:29 +00008815 }
Evan Cheng0488db92007-09-25 01:57:46 +00008816
Evan Chengd40d03e2010-01-06 19:38:29 +00008817 if (LHS.getNode()) {
Evan Chenge5b51ac2010-04-17 06:13:15 +00008818 // If LHS is i8, promote it to i32 with any_extend. There is no i8 BT
Evan Chengd40d03e2010-01-06 19:38:29 +00008819 // instruction. Since the shift amount is in-range-or-undefined, we know
Evan Chenge5b51ac2010-04-17 06:13:15 +00008820 // that doing a bittest on the i32 value is ok. We extend to i32 because
Evan Chengd40d03e2010-01-06 19:38:29 +00008821 // the encoding for the i16 version is larger than the i32 version.
Evan Chenge5b51ac2010-04-17 06:13:15 +00008822 // Also promote i16 to i32 for performance / code size reason.
8823 if (LHS.getValueType() == MVT::i8 ||
Evan Cheng2bce5f4b2010-04-28 08:30:49 +00008824 LHS.getValueType() == MVT::i16)
Evan Chengd40d03e2010-01-06 19:38:29 +00008825 LHS = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, LHS);
Chris Lattnere55484e2008-12-25 05:34:37 +00008826
Evan Chengd40d03e2010-01-06 19:38:29 +00008827 // If the operand types disagree, extend the shift amount to match. Since
8828 // BT ignores high bits (like shifts) we can use anyextend.
8829 if (LHS.getValueType() != RHS.getValueType())
8830 RHS = DAG.getNode(ISD::ANY_EXTEND, dl, LHS.getValueType(), RHS);
Dan Gohmane5af2d32009-01-29 01:59:02 +00008831
Evan Chengd40d03e2010-01-06 19:38:29 +00008832 SDValue BT = DAG.getNode(X86ISD::BT, dl, MVT::i32, LHS, RHS);
8833 unsigned Cond = CC == ISD::SETEQ ? X86::COND_AE : X86::COND_B;
8834 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
8835 DAG.getConstant(Cond, MVT::i8), BT);
Chris Lattnere55484e2008-12-25 05:34:37 +00008836 }
8837
Evan Cheng54de3ea2010-01-05 06:52:31 +00008838 return SDValue();
8839}
8840
Dan Gohmand858e902010-04-17 15:26:15 +00008841SDValue X86TargetLowering::LowerSETCC(SDValue Op, SelectionDAG &DAG) const {
Duncan Sands28b77e92011-09-06 19:07:46 +00008842
8843 if (Op.getValueType().isVector()) return LowerVSETCC(Op, DAG);
8844
Evan Cheng54de3ea2010-01-05 06:52:31 +00008845 assert(Op.getValueType() == MVT::i8 && "SetCC type must be 8-bit integer");
8846 SDValue Op0 = Op.getOperand(0);
8847 SDValue Op1 = Op.getOperand(1);
8848 DebugLoc dl = Op.getDebugLoc();
8849 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
8850
8851 // Optimize to BT if possible.
Evan Chengd40d03e2010-01-06 19:38:29 +00008852 // Lower (X & (1 << N)) == 0 to BT(X, N).
8853 // Lower ((X >>u N) & 1) != 0 to BT(X, N).
8854 // Lower ((X >>s N) & 1) != 0 to BT(X, N).
Andrew Trickf6c39412011-03-23 23:11:02 +00008855 if (Op0.getOpcode() == ISD::AND && Op0.hasOneUse() &&
Evan Chengd40d03e2010-01-06 19:38:29 +00008856 Op1.getOpcode() == ISD::Constant &&
Dan Gohmane368b462010-06-18 14:22:04 +00008857 cast<ConstantSDNode>(Op1)->isNullValue() &&
Evan Chengd40d03e2010-01-06 19:38:29 +00008858 (CC == ISD::SETEQ || CC == ISD::SETNE)) {
8859 SDValue NewSetCC = LowerToBT(Op0, CC, dl, DAG);
8860 if (NewSetCC.getNode())
8861 return NewSetCC;
8862 }
Evan Cheng54de3ea2010-01-05 06:52:31 +00008863
Chris Lattner481eebc2010-12-19 21:23:48 +00008864 // Look for X == 0, X == 1, X != 0, or X != 1. We can simplify some forms of
8865 // these.
8866 if (Op1.getOpcode() == ISD::Constant &&
Andrew Trickf6c39412011-03-23 23:11:02 +00008867 (cast<ConstantSDNode>(Op1)->getZExtValue() == 1 ||
Evan Cheng2c755ba2010-02-27 07:36:59 +00008868 cast<ConstantSDNode>(Op1)->isNullValue()) &&
8869 (CC == ISD::SETEQ || CC == ISD::SETNE)) {
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008870
Chris Lattner481eebc2010-12-19 21:23:48 +00008871 // If the input is a setcc, then reuse the input setcc or use a new one with
8872 // the inverted condition.
8873 if (Op0.getOpcode() == X86ISD::SETCC) {
8874 X86::CondCode CCode = (X86::CondCode)Op0.getConstantOperandVal(0);
8875 bool Invert = (CC == ISD::SETNE) ^
8876 cast<ConstantSDNode>(Op1)->isNullValue();
8877 if (!Invert) return Op0;
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008878
Evan Cheng2c755ba2010-02-27 07:36:59 +00008879 CCode = X86::GetOppositeBranchCondition(CCode);
Chris Lattner481eebc2010-12-19 21:23:48 +00008880 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
8881 DAG.getConstant(CCode, MVT::i8), Op0.getOperand(1));
8882 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00008883 }
8884
Evan Chenge5b51ac2010-04-17 06:13:15 +00008885 bool isFP = Op1.getValueType().isFloatingPoint();
Chris Lattnere55484e2008-12-25 05:34:37 +00008886 unsigned X86CC = TranslateX86CC(CC, isFP, Op0, Op1, DAG);
Dan Gohman1a492952009-10-20 16:22:37 +00008887 if (X86CC == X86::COND_INVALID)
8888 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00008889
Chris Lattnerc19d1c32010-12-19 22:08:31 +00008890 SDValue EFLAGS = EmitCmp(Op0, Op1, X86CC, DAG);
Benjamin Kramer17c836c2012-04-27 12:07:43 +00008891 EFLAGS = ConvertCmpIfNecessary(EFLAGS, DAG);
Owen Anderson825b72b2009-08-11 20:47:22 +00008892 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
Chris Lattnerc19d1c32010-12-19 22:08:31 +00008893 DAG.getConstant(X86CC, MVT::i8), EFLAGS);
Evan Cheng0488db92007-09-25 01:57:46 +00008894}
8895
Craig Topper89af15e2011-09-18 08:03:58 +00008896// Lower256IntVSETCC - Break a VSETCC 256-bit integer VSETCC into two new 128
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00008897// ones, and then concatenate the result back.
Craig Topper89af15e2011-09-18 08:03:58 +00008898static SDValue Lower256IntVSETCC(SDValue Op, SelectionDAG &DAG) {
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00008899 EVT VT = Op.getValueType();
8900
Craig Topper7a9a28b2012-08-12 02:23:29 +00008901 assert(VT.is256BitVector() && Op.getOpcode() == ISD::SETCC &&
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00008902 "Unsupported value type for operation");
8903
Craig Topper66ddd152012-04-27 22:54:43 +00008904 unsigned NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00008905 DebugLoc dl = Op.getDebugLoc();
8906 SDValue CC = Op.getOperand(2);
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00008907
8908 // Extract the LHS vectors
8909 SDValue LHS = Op.getOperand(0);
Craig Topperb14940a2012-04-22 20:55:18 +00008910 SDValue LHS1 = Extract128BitVector(LHS, 0, DAG, dl);
8911 SDValue LHS2 = Extract128BitVector(LHS, NumElems/2, DAG, dl);
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00008912
8913 // Extract the RHS vectors
8914 SDValue RHS = Op.getOperand(1);
Craig Topperb14940a2012-04-22 20:55:18 +00008915 SDValue RHS1 = Extract128BitVector(RHS, 0, DAG, dl);
8916 SDValue RHS2 = Extract128BitVector(RHS, NumElems/2, DAG, dl);
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00008917
8918 // Issue the operation on the smaller types and concatenate the result back
8919 MVT EltVT = VT.getVectorElementType().getSimpleVT();
8920 EVT NewVT = MVT::getVectorVT(EltVT, NumElems/2);
8921 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT,
8922 DAG.getNode(Op.getOpcode(), dl, NewVT, LHS1, RHS1, CC),
8923 DAG.getNode(Op.getOpcode(), dl, NewVT, LHS2, RHS2, CC));
8924}
8925
8926
Dan Gohmand858e902010-04-17 15:26:15 +00008927SDValue X86TargetLowering::LowerVSETCC(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +00008928 SDValue Cond;
8929 SDValue Op0 = Op.getOperand(0);
8930 SDValue Op1 = Op.getOperand(1);
8931 SDValue CC = Op.getOperand(2);
Owen Andersone50ed302009-08-10 22:56:29 +00008932 EVT VT = Op.getValueType();
Nate Begeman30a0de92008-07-17 16:51:19 +00008933 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
8934 bool isFP = Op.getOperand(1).getValueType().isFloatingPoint();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008935 DebugLoc dl = Op.getDebugLoc();
Nate Begeman30a0de92008-07-17 16:51:19 +00008936
8937 if (isFP) {
Craig Topper523908d2012-08-13 02:34:03 +00008938#ifndef NDEBUG
Bruno Cardoso Lopes0f0e0a02011-08-09 00:46:57 +00008939 EVT EltVT = Op0.getValueType().getVectorElementType();
Craig Topper523908d2012-08-13 02:34:03 +00008940 assert(EltVT == MVT::f32 || EltVT == MVT::f64);
8941#endif
Bruno Cardoso Lopes0f0e0a02011-08-09 00:46:57 +00008942
Craig Topper523908d2012-08-13 02:34:03 +00008943 unsigned SSECC;
Nate Begeman30a0de92008-07-17 16:51:19 +00008944 bool Swap = false;
8945
Bruno Cardoso Lopes8e03a822011-09-12 19:30:40 +00008946 // SSE Condition code mapping:
8947 // 0 - EQ
8948 // 1 - LT
8949 // 2 - LE
8950 // 3 - UNORD
8951 // 4 - NEQ
8952 // 5 - NLT
8953 // 6 - NLE
8954 // 7 - ORD
Nate Begeman30a0de92008-07-17 16:51:19 +00008955 switch (SetCCOpcode) {
Craig Topper2f1b2ec2012-08-13 03:42:38 +00008956 default: llvm_unreachable("Unexpected SETCC condition");
Nate Begemanfb8ead02008-07-25 19:05:58 +00008957 case ISD::SETOEQ:
Nate Begeman30a0de92008-07-17 16:51:19 +00008958 case ISD::SETEQ: SSECC = 0; break;
Bruno Cardoso Lopes8e03a822011-09-12 19:30:40 +00008959 case ISD::SETOGT:
8960 case ISD::SETGT: Swap = true; // Fallthrough
Bruno Cardoso Lopes457d53d2011-09-12 21:24:07 +00008961 case ISD::SETLT:
8962 case ISD::SETOLT: SSECC = 1; break;
8963 case ISD::SETOGE:
8964 case ISD::SETGE: Swap = true; // Fallthrough
Nate Begeman30a0de92008-07-17 16:51:19 +00008965 case ISD::SETLE:
8966 case ISD::SETOLE: SSECC = 2; break;
8967 case ISD::SETUO: SSECC = 3; break;
Nate Begemanfb8ead02008-07-25 19:05:58 +00008968 case ISD::SETUNE:
Nate Begeman30a0de92008-07-17 16:51:19 +00008969 case ISD::SETNE: SSECC = 4; break;
Craig Topper523908d2012-08-13 02:34:03 +00008970 case ISD::SETULE: Swap = true; // Fallthrough
Nate Begeman30a0de92008-07-17 16:51:19 +00008971 case ISD::SETUGE: SSECC = 5; break;
Craig Topper523908d2012-08-13 02:34:03 +00008972 case ISD::SETULT: Swap = true; // Fallthrough
Nate Begeman30a0de92008-07-17 16:51:19 +00008973 case ISD::SETUGT: SSECC = 6; break;
8974 case ISD::SETO: SSECC = 7; break;
Craig Topper523908d2012-08-13 02:34:03 +00008975 case ISD::SETUEQ:
8976 case ISD::SETONE: SSECC = 8; break;
Nate Begeman30a0de92008-07-17 16:51:19 +00008977 }
8978 if (Swap)
8979 std::swap(Op0, Op1);
8980
Nate Begemanfb8ead02008-07-25 19:05:58 +00008981 // In the two special cases we can't handle, emit two comparisons.
Nate Begeman30a0de92008-07-17 16:51:19 +00008982 if (SSECC == 8) {
Craig Topper523908d2012-08-13 02:34:03 +00008983 unsigned CC0, CC1;
8984 unsigned CombineOpc;
Nate Begemanfb8ead02008-07-25 19:05:58 +00008985 if (SetCCOpcode == ISD::SETUEQ) {
Craig Topper523908d2012-08-13 02:34:03 +00008986 CC0 = 3; CC1 = 0; CombineOpc = ISD::OR;
8987 } else {
8988 assert(SetCCOpcode == ISD::SETONE);
8989 CC0 = 7; CC1 = 4; CombineOpc = ISD::AND;
Craig Topper69947b92012-04-23 06:57:04 +00008990 }
Craig Topper523908d2012-08-13 02:34:03 +00008991
8992 SDValue Cmp0 = DAG.getNode(X86ISD::CMPP, dl, VT, Op0, Op1,
8993 DAG.getConstant(CC0, MVT::i8));
8994 SDValue Cmp1 = DAG.getNode(X86ISD::CMPP, dl, VT, Op0, Op1,
8995 DAG.getConstant(CC1, MVT::i8));
8996 return DAG.getNode(CombineOpc, dl, VT, Cmp0, Cmp1);
Nate Begeman30a0de92008-07-17 16:51:19 +00008997 }
8998 // Handle all other FP comparisons here.
Craig Topper1906d322012-01-22 23:36:02 +00008999 return DAG.getNode(X86ISD::CMPP, dl, VT, Op0, Op1,
9000 DAG.getConstant(SSECC, MVT::i8));
Nate Begeman30a0de92008-07-17 16:51:19 +00009001 }
Scott Michelfdc40a02009-02-17 22:15:04 +00009002
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00009003 // Break 256-bit integer vector compare into smaller ones.
Craig Topper7a9a28b2012-08-12 02:23:29 +00009004 if (VT.is256BitVector() && !Subtarget->hasAVX2())
Craig Topper89af15e2011-09-18 08:03:58 +00009005 return Lower256IntVSETCC(Op, DAG);
Bruno Cardoso Lopes0f0e0a02011-08-09 00:46:57 +00009006
Nate Begeman30a0de92008-07-17 16:51:19 +00009007 // We are handling one of the integer comparisons here. Since SSE only has
9008 // GT and EQ comparisons for integer, swapping operands and multiple
9009 // operations may be required for some comparisons.
Craig Topper2f1b2ec2012-08-13 03:42:38 +00009010 unsigned Opc;
Nate Begeman30a0de92008-07-17 16:51:19 +00009011 bool Swap = false, Invert = false, FlipSigns = false;
Scott Michelfdc40a02009-02-17 22:15:04 +00009012
Nate Begeman30a0de92008-07-17 16:51:19 +00009013 switch (SetCCOpcode) {
Craig Topper2f1b2ec2012-08-13 03:42:38 +00009014 default: llvm_unreachable("Unexpected SETCC condition");
Nate Begeman30a0de92008-07-17 16:51:19 +00009015 case ISD::SETNE: Invert = true;
Craig Topper67609fd2012-01-22 22:42:16 +00009016 case ISD::SETEQ: Opc = X86ISD::PCMPEQ; break;
Nate Begeman30a0de92008-07-17 16:51:19 +00009017 case ISD::SETLT: Swap = true;
Craig Topper67609fd2012-01-22 22:42:16 +00009018 case ISD::SETGT: Opc = X86ISD::PCMPGT; break;
Nate Begeman30a0de92008-07-17 16:51:19 +00009019 case ISD::SETGE: Swap = true;
Craig Topper67609fd2012-01-22 22:42:16 +00009020 case ISD::SETLE: Opc = X86ISD::PCMPGT; Invert = true; break;
Nate Begeman30a0de92008-07-17 16:51:19 +00009021 case ISD::SETULT: Swap = true;
Craig Topper67609fd2012-01-22 22:42:16 +00009022 case ISD::SETUGT: Opc = X86ISD::PCMPGT; FlipSigns = true; break;
Nate Begeman30a0de92008-07-17 16:51:19 +00009023 case ISD::SETUGE: Swap = true;
Craig Topper67609fd2012-01-22 22:42:16 +00009024 case ISD::SETULE: Opc = X86ISD::PCMPGT; FlipSigns = true; Invert = true; break;
Nate Begeman30a0de92008-07-17 16:51:19 +00009025 }
9026 if (Swap)
9027 std::swap(Op0, Op1);
Scott Michelfdc40a02009-02-17 22:15:04 +00009028
Eli Friedman7d3e2b72011-09-28 21:00:25 +00009029 // Check that the operation in question is available (most are plain SSE2,
9030 // but PCMPGTQ and PCMPEQQ have different requirements).
Craig Topper2f1b2ec2012-08-13 03:42:38 +00009031 if (VT == MVT::v2i64) {
9032 if (Opc == X86ISD::PCMPGT && !Subtarget->hasSSE42())
9033 return SDValue();
9034 if (Opc == X86ISD::PCMPEQ && !Subtarget->hasSSE41())
9035 return SDValue();
9036 }
Eli Friedman7d3e2b72011-09-28 21:00:25 +00009037
Nate Begeman30a0de92008-07-17 16:51:19 +00009038 // Since SSE has no unsigned integer comparisons, we need to flip the sign
9039 // bits of the inputs before performing those operations.
9040 if (FlipSigns) {
Owen Andersone50ed302009-08-10 22:56:29 +00009041 EVT EltVT = VT.getVectorElementType();
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00009042 SDValue SignBit = DAG.getConstant(APInt::getSignBit(EltVT.getSizeInBits()),
9043 EltVT);
Dan Gohman475871a2008-07-27 21:46:04 +00009044 std::vector<SDValue> SignBits(VT.getVectorNumElements(), SignBit);
Evan Chenga87008d2009-02-25 22:49:59 +00009045 SDValue SignVec = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &SignBits[0],
9046 SignBits.size());
Dale Johannesenace16102009-02-03 19:33:06 +00009047 Op0 = DAG.getNode(ISD::XOR, dl, VT, Op0, SignVec);
9048 Op1 = DAG.getNode(ISD::XOR, dl, VT, Op1, SignVec);
Nate Begeman30a0de92008-07-17 16:51:19 +00009049 }
Scott Michelfdc40a02009-02-17 22:15:04 +00009050
Dale Johannesenace16102009-02-03 19:33:06 +00009051 SDValue Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
Nate Begeman30a0de92008-07-17 16:51:19 +00009052
9053 // If the logical-not of the result is required, perform that now.
Bob Wilson4c245462009-01-22 17:39:32 +00009054 if (Invert)
Dale Johannesenace16102009-02-03 19:33:06 +00009055 Result = DAG.getNOT(dl, Result, VT);
Bob Wilson4c245462009-01-22 17:39:32 +00009056
Nate Begeman30a0de92008-07-17 16:51:19 +00009057 return Result;
9058}
Evan Cheng0488db92007-09-25 01:57:46 +00009059
Evan Cheng370e5342008-12-03 08:38:43 +00009060// isX86LogicalCmp - Return true if opcode is a X86 logical comparison.
Dan Gohman076aee32009-03-04 19:44:21 +00009061static bool isX86LogicalCmp(SDValue Op) {
9062 unsigned Opc = Op.getNode()->getOpcode();
Benjamin Kramer17c836c2012-04-27 12:07:43 +00009063 if (Opc == X86ISD::CMP || Opc == X86ISD::COMI || Opc == X86ISD::UCOMI ||
9064 Opc == X86ISD::SAHF)
Dan Gohman076aee32009-03-04 19:44:21 +00009065 return true;
9066 if (Op.getResNo() == 1 &&
9067 (Opc == X86ISD::ADD ||
9068 Opc == X86ISD::SUB ||
Chris Lattner5b856542010-12-20 00:59:46 +00009069 Opc == X86ISD::ADC ||
9070 Opc == X86ISD::SBB ||
Dan Gohman076aee32009-03-04 19:44:21 +00009071 Opc == X86ISD::SMUL ||
9072 Opc == X86ISD::UMUL ||
9073 Opc == X86ISD::INC ||
Dan Gohmane220c4b2009-09-18 19:59:53 +00009074 Opc == X86ISD::DEC ||
9075 Opc == X86ISD::OR ||
9076 Opc == X86ISD::XOR ||
9077 Opc == X86ISD::AND))
Dan Gohman076aee32009-03-04 19:44:21 +00009078 return true;
9079
Chris Lattner9637d5b2010-12-05 07:49:54 +00009080 if (Op.getResNo() == 2 && Opc == X86ISD::UMUL)
9081 return true;
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009082
Dan Gohman076aee32009-03-04 19:44:21 +00009083 return false;
Evan Cheng370e5342008-12-03 08:38:43 +00009084}
9085
Chris Lattnera2b56002010-12-05 01:23:24 +00009086static bool isZero(SDValue V) {
9087 ConstantSDNode *C = dyn_cast<ConstantSDNode>(V);
9088 return C && C->isNullValue();
9089}
9090
Chris Lattner96908b12010-12-05 02:00:51 +00009091static bool isAllOnes(SDValue V) {
9092 ConstantSDNode *C = dyn_cast<ConstantSDNode>(V);
9093 return C && C->isAllOnesValue();
9094}
9095
Evan Chengb64dd5f2012-08-07 22:21:00 +00009096static bool isTruncWithZeroHighBitsInput(SDValue V, SelectionDAG &DAG) {
9097 if (V.getOpcode() != ISD::TRUNCATE)
9098 return false;
9099
9100 SDValue VOp0 = V.getOperand(0);
9101 unsigned InBits = VOp0.getValueSizeInBits();
9102 unsigned Bits = V.getValueSizeInBits();
9103 return DAG.MaskedValueIsZero(VOp0, APInt::getHighBitsSet(InBits,InBits-Bits));
9104}
9105
Dan Gohmand858e902010-04-17 15:26:15 +00009106SDValue X86TargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng734503b2006-09-11 02:19:56 +00009107 bool addTest = true;
Dan Gohman475871a2008-07-27 21:46:04 +00009108 SDValue Cond = Op.getOperand(0);
Chris Lattnera2b56002010-12-05 01:23:24 +00009109 SDValue Op1 = Op.getOperand(1);
9110 SDValue Op2 = Op.getOperand(2);
9111 DebugLoc DL = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00009112 SDValue CC;
Evan Cheng9bba8942006-01-26 02:13:10 +00009113
Dan Gohman1a492952009-10-20 16:22:37 +00009114 if (Cond.getOpcode() == ISD::SETCC) {
9115 SDValue NewCond = LowerSETCC(Cond, DAG);
9116 if (NewCond.getNode())
9117 Cond = NewCond;
9118 }
Evan Cheng734503b2006-09-11 02:19:56 +00009119
Chris Lattnera2b56002010-12-05 01:23:24 +00009120 // (select (x == 0), -1, y) -> (sign_bit (x - 1)) | y
Chris Lattner96908b12010-12-05 02:00:51 +00009121 // (select (x == 0), y, -1) -> ~(sign_bit (x - 1)) | y
Chris Lattnera2b56002010-12-05 01:23:24 +00009122 // (select (x != 0), y, -1) -> (sign_bit (x - 1)) | y
Chris Lattner96908b12010-12-05 02:00:51 +00009123 // (select (x != 0), -1, y) -> ~(sign_bit (x - 1)) | y
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00009124 if (Cond.getOpcode() == X86ISD::SETCC &&
Chris Lattner96908b12010-12-05 02:00:51 +00009125 Cond.getOperand(1).getOpcode() == X86ISD::CMP &&
9126 isZero(Cond.getOperand(1).getOperand(1))) {
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00009127 SDValue Cmp = Cond.getOperand(1);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009128
Chris Lattnera2b56002010-12-05 01:23:24 +00009129 unsigned CondCode =cast<ConstantSDNode>(Cond.getOperand(0))->getZExtValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009130
9131 if ((isAllOnes(Op1) || isAllOnes(Op2)) &&
Chris Lattner96908b12010-12-05 02:00:51 +00009132 (CondCode == X86::COND_E || CondCode == X86::COND_NE)) {
9133 SDValue Y = isAllOnes(Op2) ? Op1 : Op2;
Chris Lattnera2b56002010-12-05 01:23:24 +00009134
9135 SDValue CmpOp0 = Cmp.getOperand(0);
Manman Rened579842012-05-07 18:06:23 +00009136 // Apply further optimizations for special cases
9137 // (select (x != 0), -1, 0) -> neg & sbb
9138 // (select (x == 0), 0, -1) -> neg & sbb
9139 if (ConstantSDNode *YC = dyn_cast<ConstantSDNode>(Y))
Chad Rosiera20e1e72012-08-01 18:39:17 +00009140 if (YC->isNullValue() &&
Manman Rened579842012-05-07 18:06:23 +00009141 (isAllOnes(Op1) == (CondCode == X86::COND_NE))) {
9142 SDVTList VTs = DAG.getVTList(CmpOp0.getValueType(), MVT::i32);
Chad Rosiera20e1e72012-08-01 18:39:17 +00009143 SDValue Neg = DAG.getNode(X86ISD::SUB, DL, VTs,
9144 DAG.getConstant(0, CmpOp0.getValueType()),
Manman Rened579842012-05-07 18:06:23 +00009145 CmpOp0);
9146 SDValue Res = DAG.getNode(X86ISD::SETCC_CARRY, DL, Op.getValueType(),
9147 DAG.getConstant(X86::COND_B, MVT::i8),
9148 SDValue(Neg.getNode(), 1));
9149 return Res;
9150 }
9151
Chris Lattnera2b56002010-12-05 01:23:24 +00009152 Cmp = DAG.getNode(X86ISD::CMP, DL, MVT::i32,
9153 CmpOp0, DAG.getConstant(1, CmpOp0.getValueType()));
Benjamin Kramer17c836c2012-04-27 12:07:43 +00009154 Cmp = ConvertCmpIfNecessary(Cmp, DAG);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009155
Chris Lattner96908b12010-12-05 02:00:51 +00009156 SDValue Res = // Res = 0 or -1.
Chris Lattnera2b56002010-12-05 01:23:24 +00009157 DAG.getNode(X86ISD::SETCC_CARRY, DL, Op.getValueType(),
9158 DAG.getConstant(X86::COND_B, MVT::i8), Cmp);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009159
Chris Lattner96908b12010-12-05 02:00:51 +00009160 if (isAllOnes(Op1) != (CondCode == X86::COND_E))
9161 Res = DAG.getNOT(DL, Res, Res.getValueType());
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009162
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00009163 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(Op2);
Chris Lattnera2b56002010-12-05 01:23:24 +00009164 if (N2C == 0 || !N2C->isNullValue())
9165 Res = DAG.getNode(ISD::OR, DL, Res.getValueType(), Res, Y);
9166 return Res;
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00009167 }
9168 }
9169
Chris Lattnera2b56002010-12-05 01:23:24 +00009170 // Look past (and (setcc_carry (cmp ...)), 1).
Evan Chengad9c0a32009-12-15 00:53:42 +00009171 if (Cond.getOpcode() == ISD::AND &&
9172 Cond.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY) {
9173 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Cond.getOperand(1));
Michael J. Spencerec38de22010-10-10 22:04:20 +00009174 if (C && C->getAPIntValue() == 1)
Evan Chengad9c0a32009-12-15 00:53:42 +00009175 Cond = Cond.getOperand(0);
9176 }
9177
Evan Cheng3f41d662007-10-08 22:16:29 +00009178 // If condition flag is set by a X86ISD::CMP, then use it as the condition
9179 // setting operand in place of the X86ISD::SETCC.
Dan Gohman65fd6562011-11-03 21:49:52 +00009180 unsigned CondOpcode = Cond.getOpcode();
9181 if (CondOpcode == X86ISD::SETCC ||
9182 CondOpcode == X86ISD::SETCC_CARRY) {
Evan Cheng734503b2006-09-11 02:19:56 +00009183 CC = Cond.getOperand(0);
9184
Dan Gohman475871a2008-07-27 21:46:04 +00009185 SDValue Cmp = Cond.getOperand(1);
Evan Cheng734503b2006-09-11 02:19:56 +00009186 unsigned Opc = Cmp.getOpcode();
Owen Andersone50ed302009-08-10 22:56:29 +00009187 EVT VT = Op.getValueType();
Scott Michelfdc40a02009-02-17 22:15:04 +00009188
Evan Cheng3f41d662007-10-08 22:16:29 +00009189 bool IllegalFPCMov = false;
Duncan Sands83ec4b62008-06-06 12:08:01 +00009190 if (VT.isFloatingPoint() && !VT.isVector() &&
Chris Lattner78631162008-01-16 06:24:21 +00009191 !isScalarFPTypeInSSEReg(VT)) // FPStack?
Dan Gohman7810bfe2008-09-26 21:54:37 +00009192 IllegalFPCMov = !hasFPCMov(cast<ConstantSDNode>(CC)->getSExtValue());
Scott Michelfdc40a02009-02-17 22:15:04 +00009193
Chris Lattnerd1980a52009-03-12 06:52:53 +00009194 if ((isX86LogicalCmp(Cmp) && !IllegalFPCMov) ||
9195 Opc == X86ISD::BT) { // FIXME
Evan Cheng3f41d662007-10-08 22:16:29 +00009196 Cond = Cmp;
Evan Cheng0488db92007-09-25 01:57:46 +00009197 addTest = false;
9198 }
Dan Gohman65fd6562011-11-03 21:49:52 +00009199 } else if (CondOpcode == ISD::USUBO || CondOpcode == ISD::SSUBO ||
9200 CondOpcode == ISD::UADDO || CondOpcode == ISD::SADDO ||
9201 ((CondOpcode == ISD::UMULO || CondOpcode == ISD::SMULO) &&
9202 Cond.getOperand(0).getValueType() != MVT::i8)) {
9203 SDValue LHS = Cond.getOperand(0);
9204 SDValue RHS = Cond.getOperand(1);
9205 unsigned X86Opcode;
9206 unsigned X86Cond;
9207 SDVTList VTs;
9208 switch (CondOpcode) {
9209 case ISD::UADDO: X86Opcode = X86ISD::ADD; X86Cond = X86::COND_B; break;
9210 case ISD::SADDO: X86Opcode = X86ISD::ADD; X86Cond = X86::COND_O; break;
9211 case ISD::USUBO: X86Opcode = X86ISD::SUB; X86Cond = X86::COND_B; break;
9212 case ISD::SSUBO: X86Opcode = X86ISD::SUB; X86Cond = X86::COND_O; break;
9213 case ISD::UMULO: X86Opcode = X86ISD::UMUL; X86Cond = X86::COND_O; break;
9214 case ISD::SMULO: X86Opcode = X86ISD::SMUL; X86Cond = X86::COND_O; break;
9215 default: llvm_unreachable("unexpected overflowing operator");
9216 }
9217 if (CondOpcode == ISD::UMULO)
9218 VTs = DAG.getVTList(LHS.getValueType(), LHS.getValueType(),
9219 MVT::i32);
9220 else
9221 VTs = DAG.getVTList(LHS.getValueType(), MVT::i32);
9222
9223 SDValue X86Op = DAG.getNode(X86Opcode, DL, VTs, LHS, RHS);
9224
9225 if (CondOpcode == ISD::UMULO)
9226 Cond = X86Op.getValue(2);
9227 else
9228 Cond = X86Op.getValue(1);
9229
9230 CC = DAG.getConstant(X86Cond, MVT::i8);
9231 addTest = false;
Evan Cheng0488db92007-09-25 01:57:46 +00009232 }
9233
9234 if (addTest) {
Evan Chengb64dd5f2012-08-07 22:21:00 +00009235 // Look pass the truncate if the high bits are known zero.
9236 if (isTruncWithZeroHighBitsInput(Cond, DAG))
9237 Cond = Cond.getOperand(0);
Evan Chengd40d03e2010-01-06 19:38:29 +00009238
9239 // We know the result of AND is compared against zero. Try to match
9240 // it to BT.
Michael J. Spencerec38de22010-10-10 22:04:20 +00009241 if (Cond.getOpcode() == ISD::AND && Cond.hasOneUse()) {
Chris Lattnera2b56002010-12-05 01:23:24 +00009242 SDValue NewSetCC = LowerToBT(Cond, ISD::SETNE, DL, DAG);
Evan Chengd40d03e2010-01-06 19:38:29 +00009243 if (NewSetCC.getNode()) {
9244 CC = NewSetCC.getOperand(0);
9245 Cond = NewSetCC.getOperand(1);
9246 addTest = false;
9247 }
9248 }
9249 }
9250
9251 if (addTest) {
Owen Anderson825b72b2009-08-11 20:47:22 +00009252 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng552f09a2010-04-26 19:06:11 +00009253 Cond = EmitTest(Cond, X86::COND_NE, DAG);
Evan Cheng0488db92007-09-25 01:57:46 +00009254 }
9255
Benjamin Kramere915ff32010-12-22 23:09:28 +00009256 // a < b ? -1 : 0 -> RES = ~setcc_carry
9257 // a < b ? 0 : -1 -> RES = setcc_carry
9258 // a >= b ? -1 : 0 -> RES = setcc_carry
9259 // a >= b ? 0 : -1 -> RES = ~setcc_carry
Manman Ren39ad5682012-08-08 00:51:41 +00009260 if (Cond.getOpcode() == X86ISD::SUB) {
Benjamin Kramer17c836c2012-04-27 12:07:43 +00009261 Cond = ConvertCmpIfNecessary(Cond, DAG);
Benjamin Kramere915ff32010-12-22 23:09:28 +00009262 unsigned CondCode = cast<ConstantSDNode>(CC)->getZExtValue();
9263
9264 if ((CondCode == X86::COND_AE || CondCode == X86::COND_B) &&
9265 (isAllOnes(Op1) || isAllOnes(Op2)) && (isZero(Op1) || isZero(Op2))) {
9266 SDValue Res = DAG.getNode(X86ISD::SETCC_CARRY, DL, Op.getValueType(),
9267 DAG.getConstant(X86::COND_B, MVT::i8), Cond);
9268 if (isAllOnes(Op1) != (CondCode == X86::COND_B))
9269 return DAG.getNOT(DL, Res, Res.getValueType());
9270 return Res;
9271 }
9272 }
9273
Benjamin Kramer444dcce2012-10-13 10:39:49 +00009274 // X86 doesn't have an i8 cmov. If both operands are the result of a truncate
9275 // widen the cmov and push the truncate through. This avoids introducing a new
9276 // branch during isel and doesn't add any extensions.
9277 if (Op.getValueType() == MVT::i8 &&
9278 Op1.getOpcode() == ISD::TRUNCATE && Op2.getOpcode() == ISD::TRUNCATE) {
9279 SDValue T1 = Op1.getOperand(0), T2 = Op2.getOperand(0);
9280 if (T1.getValueType() == T2.getValueType() &&
9281 // Blacklist CopyFromReg to avoid partial register stalls.
9282 T1.getOpcode() != ISD::CopyFromReg && T2.getOpcode()!=ISD::CopyFromReg){
9283 SDVTList VTs = DAG.getVTList(T1.getValueType(), MVT::Glue);
Benjamin Kramerf8b65aa2012-10-13 12:50:19 +00009284 SDValue Cmov = DAG.getNode(X86ISD::CMOV, DL, VTs, T2, T1, CC, Cond);
Benjamin Kramer444dcce2012-10-13 10:39:49 +00009285 return DAG.getNode(ISD::TRUNCATE, DL, Op.getValueType(), Cmov);
9286 }
9287 }
9288
Evan Cheng0488db92007-09-25 01:57:46 +00009289 // X86ISD::CMOV means set the result (which is operand 1) to the RHS if
9290 // condition is true.
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00009291 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::Glue);
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00009292 SDValue Ops[] = { Op2, Op1, CC, Cond };
Chris Lattnera2b56002010-12-05 01:23:24 +00009293 return DAG.getNode(X86ISD::CMOV, DL, VTs, Ops, array_lengthof(Ops));
Evan Cheng0488db92007-09-25 01:57:46 +00009294}
9295
Evan Cheng370e5342008-12-03 08:38:43 +00009296// isAndOrOfSingleUseSetCCs - Return true if node is an ISD::AND or
9297// ISD::OR of two X86ISD::SETCC nodes each of which has no other use apart
9298// from the AND / OR.
9299static bool isAndOrOfSetCCs(SDValue Op, unsigned &Opc) {
9300 Opc = Op.getOpcode();
9301 if (Opc != ISD::OR && Opc != ISD::AND)
9302 return false;
9303 return (Op.getOperand(0).getOpcode() == X86ISD::SETCC &&
9304 Op.getOperand(0).hasOneUse() &&
9305 Op.getOperand(1).getOpcode() == X86ISD::SETCC &&
9306 Op.getOperand(1).hasOneUse());
9307}
9308
Evan Cheng961d6d42009-02-02 08:19:07 +00009309// isXor1OfSetCC - Return true if node is an ISD::XOR of a X86ISD::SETCC and
9310// 1 and that the SETCC node has a single use.
Evan Cheng67ad9db2009-02-02 08:07:36 +00009311static bool isXor1OfSetCC(SDValue Op) {
9312 if (Op.getOpcode() != ISD::XOR)
9313 return false;
9314 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
9315 if (N1C && N1C->getAPIntValue() == 1) {
9316 return Op.getOperand(0).getOpcode() == X86ISD::SETCC &&
9317 Op.getOperand(0).hasOneUse();
9318 }
9319 return false;
9320}
9321
Dan Gohmand858e902010-04-17 15:26:15 +00009322SDValue X86TargetLowering::LowerBRCOND(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng734503b2006-09-11 02:19:56 +00009323 bool addTest = true;
Dan Gohman475871a2008-07-27 21:46:04 +00009324 SDValue Chain = Op.getOperand(0);
9325 SDValue Cond = Op.getOperand(1);
9326 SDValue Dest = Op.getOperand(2);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009327 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00009328 SDValue CC;
Dan Gohman65fd6562011-11-03 21:49:52 +00009329 bool Inverted = false;
Evan Cheng734503b2006-09-11 02:19:56 +00009330
Dan Gohman1a492952009-10-20 16:22:37 +00009331 if (Cond.getOpcode() == ISD::SETCC) {
Dan Gohman65fd6562011-11-03 21:49:52 +00009332 // Check for setcc([su]{add,sub,mul}o == 0).
9333 if (cast<CondCodeSDNode>(Cond.getOperand(2))->get() == ISD::SETEQ &&
9334 isa<ConstantSDNode>(Cond.getOperand(1)) &&
9335 cast<ConstantSDNode>(Cond.getOperand(1))->isNullValue() &&
9336 Cond.getOperand(0).getResNo() == 1 &&
9337 (Cond.getOperand(0).getOpcode() == ISD::SADDO ||
9338 Cond.getOperand(0).getOpcode() == ISD::UADDO ||
9339 Cond.getOperand(0).getOpcode() == ISD::SSUBO ||
9340 Cond.getOperand(0).getOpcode() == ISD::USUBO ||
9341 Cond.getOperand(0).getOpcode() == ISD::SMULO ||
9342 Cond.getOperand(0).getOpcode() == ISD::UMULO)) {
9343 Inverted = true;
9344 Cond = Cond.getOperand(0);
9345 } else {
9346 SDValue NewCond = LowerSETCC(Cond, DAG);
9347 if (NewCond.getNode())
9348 Cond = NewCond;
9349 }
Dan Gohman1a492952009-10-20 16:22:37 +00009350 }
Chris Lattnere55484e2008-12-25 05:34:37 +00009351#if 0
9352 // FIXME: LowerXALUO doesn't handle these!!
Bill Wendlingd350e022008-12-12 21:15:41 +00009353 else if (Cond.getOpcode() == X86ISD::ADD ||
9354 Cond.getOpcode() == X86ISD::SUB ||
9355 Cond.getOpcode() == X86ISD::SMUL ||
9356 Cond.getOpcode() == X86ISD::UMUL)
Bill Wendling74c37652008-12-09 22:08:41 +00009357 Cond = LowerXALUO(Cond, DAG);
Chris Lattnere55484e2008-12-25 05:34:37 +00009358#endif
Scott Michelfdc40a02009-02-17 22:15:04 +00009359
Evan Chengad9c0a32009-12-15 00:53:42 +00009360 // Look pass (and (setcc_carry (cmp ...)), 1).
9361 if (Cond.getOpcode() == ISD::AND &&
9362 Cond.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY) {
9363 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Cond.getOperand(1));
Michael J. Spencerec38de22010-10-10 22:04:20 +00009364 if (C && C->getAPIntValue() == 1)
Evan Chengad9c0a32009-12-15 00:53:42 +00009365 Cond = Cond.getOperand(0);
9366 }
9367
Evan Cheng3f41d662007-10-08 22:16:29 +00009368 // If condition flag is set by a X86ISD::CMP, then use it as the condition
9369 // setting operand in place of the X86ISD::SETCC.
Dan Gohman65fd6562011-11-03 21:49:52 +00009370 unsigned CondOpcode = Cond.getOpcode();
9371 if (CondOpcode == X86ISD::SETCC ||
9372 CondOpcode == X86ISD::SETCC_CARRY) {
Evan Cheng734503b2006-09-11 02:19:56 +00009373 CC = Cond.getOperand(0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00009374
Dan Gohman475871a2008-07-27 21:46:04 +00009375 SDValue Cmp = Cond.getOperand(1);
Evan Cheng734503b2006-09-11 02:19:56 +00009376 unsigned Opc = Cmp.getOpcode();
Chris Lattnere55484e2008-12-25 05:34:37 +00009377 // FIXME: WHY THE SPECIAL CASING OF LogicalCmp??
Dan Gohman076aee32009-03-04 19:44:21 +00009378 if (isX86LogicalCmp(Cmp) || Opc == X86ISD::BT) {
Evan Cheng3f41d662007-10-08 22:16:29 +00009379 Cond = Cmp;
Evan Cheng0488db92007-09-25 01:57:46 +00009380 addTest = false;
Bill Wendling61edeb52008-12-02 01:06:39 +00009381 } else {
Evan Cheng370e5342008-12-03 08:38:43 +00009382 switch (cast<ConstantSDNode>(CC)->getZExtValue()) {
Bill Wendling0ea25cb2008-12-03 08:32:02 +00009383 default: break;
9384 case X86::COND_O:
Dan Gohman653456c2009-01-07 00:15:08 +00009385 case X86::COND_B:
Chris Lattnere55484e2008-12-25 05:34:37 +00009386 // These can only come from an arithmetic instruction with overflow,
9387 // e.g. SADDO, UADDO.
Bill Wendling0ea25cb2008-12-03 08:32:02 +00009388 Cond = Cond.getNode()->getOperand(1);
9389 addTest = false;
9390 break;
Bill Wendling61edeb52008-12-02 01:06:39 +00009391 }
Evan Cheng0488db92007-09-25 01:57:46 +00009392 }
Dan Gohman65fd6562011-11-03 21:49:52 +00009393 }
9394 CondOpcode = Cond.getOpcode();
9395 if (CondOpcode == ISD::UADDO || CondOpcode == ISD::SADDO ||
9396 CondOpcode == ISD::USUBO || CondOpcode == ISD::SSUBO ||
9397 ((CondOpcode == ISD::UMULO || CondOpcode == ISD::SMULO) &&
9398 Cond.getOperand(0).getValueType() != MVT::i8)) {
9399 SDValue LHS = Cond.getOperand(0);
9400 SDValue RHS = Cond.getOperand(1);
9401 unsigned X86Opcode;
9402 unsigned X86Cond;
9403 SDVTList VTs;
9404 switch (CondOpcode) {
9405 case ISD::UADDO: X86Opcode = X86ISD::ADD; X86Cond = X86::COND_B; break;
9406 case ISD::SADDO: X86Opcode = X86ISD::ADD; X86Cond = X86::COND_O; break;
9407 case ISD::USUBO: X86Opcode = X86ISD::SUB; X86Cond = X86::COND_B; break;
9408 case ISD::SSUBO: X86Opcode = X86ISD::SUB; X86Cond = X86::COND_O; break;
9409 case ISD::UMULO: X86Opcode = X86ISD::UMUL; X86Cond = X86::COND_O; break;
9410 case ISD::SMULO: X86Opcode = X86ISD::SMUL; X86Cond = X86::COND_O; break;
9411 default: llvm_unreachable("unexpected overflowing operator");
9412 }
9413 if (Inverted)
9414 X86Cond = X86::GetOppositeBranchCondition((X86::CondCode)X86Cond);
9415 if (CondOpcode == ISD::UMULO)
9416 VTs = DAG.getVTList(LHS.getValueType(), LHS.getValueType(),
9417 MVT::i32);
9418 else
9419 VTs = DAG.getVTList(LHS.getValueType(), MVT::i32);
9420
9421 SDValue X86Op = DAG.getNode(X86Opcode, dl, VTs, LHS, RHS);
9422
9423 if (CondOpcode == ISD::UMULO)
9424 Cond = X86Op.getValue(2);
9425 else
9426 Cond = X86Op.getValue(1);
9427
9428 CC = DAG.getConstant(X86Cond, MVT::i8);
9429 addTest = false;
Evan Cheng370e5342008-12-03 08:38:43 +00009430 } else {
9431 unsigned CondOpc;
9432 if (Cond.hasOneUse() && isAndOrOfSetCCs(Cond, CondOpc)) {
9433 SDValue Cmp = Cond.getOperand(0).getOperand(1);
Evan Cheng370e5342008-12-03 08:38:43 +00009434 if (CondOpc == ISD::OR) {
9435 // Also, recognize the pattern generated by an FCMP_UNE. We can emit
9436 // two branches instead of an explicit OR instruction with a
9437 // separate test.
9438 if (Cmp == Cond.getOperand(1).getOperand(1) &&
Dan Gohman076aee32009-03-04 19:44:21 +00009439 isX86LogicalCmp(Cmp)) {
Evan Cheng370e5342008-12-03 08:38:43 +00009440 CC = Cond.getOperand(0).getOperand(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009441 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Evan Cheng370e5342008-12-03 08:38:43 +00009442 Chain, Dest, CC, Cmp);
9443 CC = Cond.getOperand(1).getOperand(0);
9444 Cond = Cmp;
9445 addTest = false;
9446 }
9447 } else { // ISD::AND
9448 // Also, recognize the pattern generated by an FCMP_OEQ. We can emit
9449 // two branches instead of an explicit AND instruction with a
9450 // separate test. However, we only do this if this block doesn't
9451 // have a fall-through edge, because this requires an explicit
9452 // jmp when the condition is false.
9453 if (Cmp == Cond.getOperand(1).getOperand(1) &&
Dan Gohman076aee32009-03-04 19:44:21 +00009454 isX86LogicalCmp(Cmp) &&
Evan Cheng370e5342008-12-03 08:38:43 +00009455 Op.getNode()->hasOneUse()) {
9456 X86::CondCode CCode =
9457 (X86::CondCode)Cond.getOperand(0).getConstantOperandVal(0);
9458 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00009459 CC = DAG.getConstant(CCode, MVT::i8);
Dan Gohman027657d2010-06-18 15:30:29 +00009460 SDNode *User = *Op.getNode()->use_begin();
Evan Cheng370e5342008-12-03 08:38:43 +00009461 // Look for an unconditional branch following this conditional branch.
9462 // We need this because we need to reverse the successors in order
9463 // to implement FCMP_OEQ.
Dan Gohman027657d2010-06-18 15:30:29 +00009464 if (User->getOpcode() == ISD::BR) {
9465 SDValue FalseBB = User->getOperand(1);
9466 SDNode *NewBR =
9467 DAG.UpdateNodeOperands(User, User->getOperand(0), Dest);
Evan Cheng370e5342008-12-03 08:38:43 +00009468 assert(NewBR == User);
Nick Lewycky2a3ee5e2010-06-20 20:27:42 +00009469 (void)NewBR;
Evan Cheng370e5342008-12-03 08:38:43 +00009470 Dest = FalseBB;
Dan Gohman279c22e2008-10-21 03:29:32 +00009471
Dale Johannesene4d209d2009-02-03 20:21:25 +00009472 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Evan Cheng370e5342008-12-03 08:38:43 +00009473 Chain, Dest, CC, Cmp);
9474 X86::CondCode CCode =
9475 (X86::CondCode)Cond.getOperand(1).getConstantOperandVal(0);
9476 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00009477 CC = DAG.getConstant(CCode, MVT::i8);
Evan Cheng370e5342008-12-03 08:38:43 +00009478 Cond = Cmp;
9479 addTest = false;
9480 }
9481 }
Dan Gohman279c22e2008-10-21 03:29:32 +00009482 }
Evan Cheng67ad9db2009-02-02 08:07:36 +00009483 } else if (Cond.hasOneUse() && isXor1OfSetCC(Cond)) {
9484 // Recognize for xorb (setcc), 1 patterns. The xor inverts the condition.
9485 // It should be transformed during dag combiner except when the condition
9486 // is set by a arithmetics with overflow node.
9487 X86::CondCode CCode =
9488 (X86::CondCode)Cond.getOperand(0).getConstantOperandVal(0);
9489 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00009490 CC = DAG.getConstant(CCode, MVT::i8);
Evan Cheng67ad9db2009-02-02 08:07:36 +00009491 Cond = Cond.getOperand(0).getOperand(1);
9492 addTest = false;
Dan Gohman65fd6562011-11-03 21:49:52 +00009493 } else if (Cond.getOpcode() == ISD::SETCC &&
9494 cast<CondCodeSDNode>(Cond.getOperand(2))->get() == ISD::SETOEQ) {
9495 // For FCMP_OEQ, we can emit
9496 // two branches instead of an explicit AND instruction with a
9497 // separate test. However, we only do this if this block doesn't
9498 // have a fall-through edge, because this requires an explicit
9499 // jmp when the condition is false.
9500 if (Op.getNode()->hasOneUse()) {
9501 SDNode *User = *Op.getNode()->use_begin();
9502 // Look for an unconditional branch following this conditional branch.
9503 // We need this because we need to reverse the successors in order
9504 // to implement FCMP_OEQ.
9505 if (User->getOpcode() == ISD::BR) {
9506 SDValue FalseBB = User->getOperand(1);
9507 SDNode *NewBR =
9508 DAG.UpdateNodeOperands(User, User->getOperand(0), Dest);
9509 assert(NewBR == User);
9510 (void)NewBR;
9511 Dest = FalseBB;
9512
9513 SDValue Cmp = DAG.getNode(X86ISD::CMP, dl, MVT::i32,
9514 Cond.getOperand(0), Cond.getOperand(1));
Benjamin Kramer17c836c2012-04-27 12:07:43 +00009515 Cmp = ConvertCmpIfNecessary(Cmp, DAG);
Dan Gohman65fd6562011-11-03 21:49:52 +00009516 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
9517 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
9518 Chain, Dest, CC, Cmp);
9519 CC = DAG.getConstant(X86::COND_P, MVT::i8);
9520 Cond = Cmp;
9521 addTest = false;
9522 }
9523 }
9524 } else if (Cond.getOpcode() == ISD::SETCC &&
9525 cast<CondCodeSDNode>(Cond.getOperand(2))->get() == ISD::SETUNE) {
9526 // For FCMP_UNE, we can emit
9527 // two branches instead of an explicit AND instruction with a
9528 // separate test. However, we only do this if this block doesn't
9529 // have a fall-through edge, because this requires an explicit
9530 // jmp when the condition is false.
9531 if (Op.getNode()->hasOneUse()) {
9532 SDNode *User = *Op.getNode()->use_begin();
9533 // Look for an unconditional branch following this conditional branch.
9534 // We need this because we need to reverse the successors in order
9535 // to implement FCMP_UNE.
9536 if (User->getOpcode() == ISD::BR) {
9537 SDValue FalseBB = User->getOperand(1);
9538 SDNode *NewBR =
9539 DAG.UpdateNodeOperands(User, User->getOperand(0), Dest);
9540 assert(NewBR == User);
9541 (void)NewBR;
9542
9543 SDValue Cmp = DAG.getNode(X86ISD::CMP, dl, MVT::i32,
9544 Cond.getOperand(0), Cond.getOperand(1));
Benjamin Kramer17c836c2012-04-27 12:07:43 +00009545 Cmp = ConvertCmpIfNecessary(Cmp, DAG);
Dan Gohman65fd6562011-11-03 21:49:52 +00009546 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
9547 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
9548 Chain, Dest, CC, Cmp);
9549 CC = DAG.getConstant(X86::COND_NP, MVT::i8);
9550 Cond = Cmp;
9551 addTest = false;
9552 Dest = FalseBB;
9553 }
9554 }
Dan Gohman279c22e2008-10-21 03:29:32 +00009555 }
Evan Cheng0488db92007-09-25 01:57:46 +00009556 }
9557
9558 if (addTest) {
Evan Chengb64dd5f2012-08-07 22:21:00 +00009559 // Look pass the truncate if the high bits are known zero.
9560 if (isTruncWithZeroHighBitsInput(Cond, DAG))
9561 Cond = Cond.getOperand(0);
Evan Chengd40d03e2010-01-06 19:38:29 +00009562
9563 // We know the result of AND is compared against zero. Try to match
9564 // it to BT.
Michael J. Spencerec38de22010-10-10 22:04:20 +00009565 if (Cond.getOpcode() == ISD::AND && Cond.hasOneUse()) {
Evan Chengd40d03e2010-01-06 19:38:29 +00009566 SDValue NewSetCC = LowerToBT(Cond, ISD::SETNE, dl, DAG);
9567 if (NewSetCC.getNode()) {
9568 CC = NewSetCC.getOperand(0);
9569 Cond = NewSetCC.getOperand(1);
9570 addTest = false;
9571 }
9572 }
9573 }
9574
9575 if (addTest) {
Owen Anderson825b72b2009-08-11 20:47:22 +00009576 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng552f09a2010-04-26 19:06:11 +00009577 Cond = EmitTest(Cond, X86::COND_NE, DAG);
Evan Cheng0488db92007-09-25 01:57:46 +00009578 }
Benjamin Kramer17c836c2012-04-27 12:07:43 +00009579 Cond = ConvertCmpIfNecessary(Cond, DAG);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009580 return DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Dan Gohman279c22e2008-10-21 03:29:32 +00009581 Chain, Dest, CC, Cond);
Evan Cheng0488db92007-09-25 01:57:46 +00009582}
9583
Anton Korobeynikove060b532007-04-17 19:34:00 +00009584
9585// Lower dynamic stack allocation to _alloca call for Cygwin/Mingw targets.
9586// Calls to _alloca is needed to probe the stack when allocating more than 4k
9587// bytes in one go. Touching the stack at 4K increments is necessary to ensure
9588// that the guard pages used by the OS virtual memory manager are allocated in
9589// correct sequence.
Dan Gohman475871a2008-07-27 21:46:04 +00009590SDValue
9591X86TargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00009592 SelectionDAG &DAG) const {
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009593 assert((Subtarget->isTargetCygMing() || Subtarget->isTargetWindows() ||
Nick Lewycky8a8d4792011-12-02 22:16:29 +00009594 getTargetMachine().Options.EnableSegmentedStacks) &&
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009595 "This should be used only on Windows targets or when segmented stacks "
Rafael Espindola96428ce2011-09-06 18:43:08 +00009596 "are being used");
9597 assert(!Subtarget->isTargetEnvMacho() && "Not implemented");
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009598 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov096b4612008-06-11 20:16:42 +00009599
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00009600 // Get the inputs.
Dan Gohman475871a2008-07-27 21:46:04 +00009601 SDValue Chain = Op.getOperand(0);
9602 SDValue Size = Op.getOperand(1);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00009603 // FIXME: Ensure alignment here
9604
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009605 bool Is64Bit = Subtarget->is64Bit();
9606 EVT SPTy = Is64Bit ? MVT::i64 : MVT::i32;
Anton Korobeynikov096b4612008-06-11 20:16:42 +00009607
Nick Lewycky8a8d4792011-12-02 22:16:29 +00009608 if (getTargetMachine().Options.EnableSegmentedStacks) {
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009609 MachineFunction &MF = DAG.getMachineFunction();
9610 MachineRegisterInfo &MRI = MF.getRegInfo();
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00009611
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009612 if (Is64Bit) {
9613 // The 64 bit implementation of segmented stacks needs to clobber both r10
Rafael Espindola96428ce2011-09-06 18:43:08 +00009614 // r11. This makes it impossible to use it along with nested parameters.
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009615 const Function *F = MF.getFunction();
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00009616
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009617 for (Function::const_arg_iterator I = F->arg_begin(), E = F->arg_end();
Craig Topper31a207a2012-05-04 06:39:13 +00009618 I != E; ++I)
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009619 if (I->hasNestAttr())
9620 report_fatal_error("Cannot use segmented stacks with functions that "
9621 "have nested arguments.");
9622 }
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00009623
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009624 const TargetRegisterClass *AddrRegClass =
9625 getRegClassFor(Subtarget->is64Bit() ? MVT::i64:MVT::i32);
9626 unsigned Vreg = MRI.createVirtualRegister(AddrRegClass);
9627 Chain = DAG.getCopyToReg(Chain, dl, Vreg, Size);
9628 SDValue Value = DAG.getNode(X86ISD::SEG_ALLOCA, dl, SPTy, Chain,
9629 DAG.getRegister(Vreg, SPTy));
9630 SDValue Ops1[2] = { Value, Chain };
9631 return DAG.getMergeValues(Ops1, 2, dl);
9632 } else {
9633 SDValue Flag;
9634 unsigned Reg = (Subtarget->is64Bit() ? X86::RAX : X86::EAX);
Anton Korobeynikov096b4612008-06-11 20:16:42 +00009635
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009636 Chain = DAG.getCopyToReg(Chain, dl, Reg, Size, Flag);
9637 Flag = Chain.getValue(1);
9638 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Anton Korobeynikov096b4612008-06-11 20:16:42 +00009639
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009640 Chain = DAG.getNode(X86ISD::WIN_ALLOCA, dl, NodeTys, Chain, Flag);
9641 Flag = Chain.getValue(1);
9642
9643 Chain = DAG.getCopyFromReg(Chain, dl, X86StackPtr, SPTy).getValue(1);
9644
9645 SDValue Ops1[2] = { Chain.getValue(0), Chain };
9646 return DAG.getMergeValues(Ops1, 2, dl);
9647 }
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00009648}
9649
Dan Gohmand858e902010-04-17 15:26:15 +00009650SDValue X86TargetLowering::LowerVASTART(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman1e93df62010-04-17 14:41:14 +00009651 MachineFunction &MF = DAG.getMachineFunction();
9652 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
9653
Dan Gohman69de1932008-02-06 22:27:42 +00009654 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Chris Lattner8026a9d2010-09-21 17:50:43 +00009655 DebugLoc DL = Op.getDebugLoc();
Evan Cheng8b2794a2006-10-13 21:14:26 +00009656
Anton Korobeynikove7beda12010-10-03 22:52:07 +00009657 if (!Subtarget->is64Bit() || Subtarget->isTargetWin64()) {
Evan Cheng25ab6902006-09-08 06:48:29 +00009658 // vastart just stores the address of the VarArgsFrameIndex slot into the
9659 // memory location argument.
Dan Gohman1e93df62010-04-17 14:41:14 +00009660 SDValue FR = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
9661 getPointerTy());
Chris Lattner8026a9d2010-09-21 17:50:43 +00009662 return DAG.getStore(Op.getOperand(0), DL, FR, Op.getOperand(1),
9663 MachinePointerInfo(SV), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00009664 }
9665
9666 // __va_list_tag:
9667 // gp_offset (0 - 6 * 8)
9668 // fp_offset (48 - 48 + 8 * 16)
9669 // overflow_arg_area (point to parameters coming in memory).
9670 // reg_save_area
Dan Gohman475871a2008-07-27 21:46:04 +00009671 SmallVector<SDValue, 8> MemOps;
9672 SDValue FIN = Op.getOperand(1);
Evan Cheng25ab6902006-09-08 06:48:29 +00009673 // Store gp_offset
Chris Lattner8026a9d2010-09-21 17:50:43 +00009674 SDValue Store = DAG.getStore(Op.getOperand(0), DL,
Dan Gohman1e93df62010-04-17 14:41:14 +00009675 DAG.getConstant(FuncInfo->getVarArgsGPOffset(),
9676 MVT::i32),
Chris Lattner8026a9d2010-09-21 17:50:43 +00009677 FIN, MachinePointerInfo(SV), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00009678 MemOps.push_back(Store);
9679
9680 // Store fp_offset
Chris Lattner8026a9d2010-09-21 17:50:43 +00009681 FIN = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00009682 FIN, DAG.getIntPtrConstant(4));
Chris Lattner8026a9d2010-09-21 17:50:43 +00009683 Store = DAG.getStore(Op.getOperand(0), DL,
Dan Gohman1e93df62010-04-17 14:41:14 +00009684 DAG.getConstant(FuncInfo->getVarArgsFPOffset(),
9685 MVT::i32),
Chris Lattner8026a9d2010-09-21 17:50:43 +00009686 FIN, MachinePointerInfo(SV, 4), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00009687 MemOps.push_back(Store);
9688
9689 // Store ptr to overflow_arg_area
Chris Lattner8026a9d2010-09-21 17:50:43 +00009690 FIN = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00009691 FIN, DAG.getIntPtrConstant(4));
Dan Gohman1e93df62010-04-17 14:41:14 +00009692 SDValue OVFIN = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
9693 getPointerTy());
Chris Lattner8026a9d2010-09-21 17:50:43 +00009694 Store = DAG.getStore(Op.getOperand(0), DL, OVFIN, FIN,
9695 MachinePointerInfo(SV, 8),
David Greene67c9d422010-02-15 16:53:33 +00009696 false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00009697 MemOps.push_back(Store);
9698
9699 // Store ptr to reg_save_area.
Chris Lattner8026a9d2010-09-21 17:50:43 +00009700 FIN = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00009701 FIN, DAG.getIntPtrConstant(8));
Dan Gohman1e93df62010-04-17 14:41:14 +00009702 SDValue RSFIN = DAG.getFrameIndex(FuncInfo->getRegSaveFrameIndex(),
9703 getPointerTy());
Chris Lattner8026a9d2010-09-21 17:50:43 +00009704 Store = DAG.getStore(Op.getOperand(0), DL, RSFIN, FIN,
9705 MachinePointerInfo(SV, 16), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00009706 MemOps.push_back(Store);
Chris Lattner8026a9d2010-09-21 17:50:43 +00009707 return DAG.getNode(ISD::TokenFactor, DL, MVT::Other,
Dale Johannesene4d209d2009-02-03 20:21:25 +00009708 &MemOps[0], MemOps.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00009709}
9710
Dan Gohmand858e902010-04-17 15:26:15 +00009711SDValue X86TargetLowering::LowerVAARG(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman320afb82010-10-12 18:00:49 +00009712 assert(Subtarget->is64Bit() &&
9713 "LowerVAARG only handles 64-bit va_arg!");
9714 assert((Subtarget->isTargetLinux() ||
9715 Subtarget->isTargetDarwin()) &&
9716 "Unhandled target in LowerVAARG");
9717 assert(Op.getNode()->getNumOperands() == 4);
9718 SDValue Chain = Op.getOperand(0);
9719 SDValue SrcPtr = Op.getOperand(1);
9720 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
9721 unsigned Align = Op.getConstantOperandVal(3);
9722 DebugLoc dl = Op.getDebugLoc();
Dan Gohman9018e832008-05-10 01:26:14 +00009723
Dan Gohman320afb82010-10-12 18:00:49 +00009724 EVT ArgVT = Op.getNode()->getValueType(0);
Chris Lattnerdb125cf2011-07-18 04:54:35 +00009725 Type *ArgTy = ArgVT.getTypeForEVT(*DAG.getContext());
Micah Villmow3574eca2012-10-08 16:38:25 +00009726 uint32_t ArgSize = getDataLayout()->getTypeAllocSize(ArgTy);
Dan Gohman320afb82010-10-12 18:00:49 +00009727 uint8_t ArgMode;
9728
9729 // Decide which area this value should be read from.
9730 // TODO: Implement the AMD64 ABI in its entirety. This simple
9731 // selection mechanism works only for the basic types.
9732 if (ArgVT == MVT::f80) {
9733 llvm_unreachable("va_arg for f80 not yet implemented");
9734 } else if (ArgVT.isFloatingPoint() && ArgSize <= 16 /*bytes*/) {
9735 ArgMode = 2; // Argument passed in XMM register. Use fp_offset.
9736 } else if (ArgVT.isInteger() && ArgSize <= 32 /*bytes*/) {
9737 ArgMode = 1; // Argument passed in GPR64 register(s). Use gp_offset.
9738 } else {
9739 llvm_unreachable("Unhandled argument type in LowerVAARG");
9740 }
9741
9742 if (ArgMode == 2) {
9743 // Sanity Check: Make sure using fp_offset makes sense.
Nick Lewycky8a8d4792011-12-02 22:16:29 +00009744 assert(!getTargetMachine().Options.UseSoftFloat &&
Eric Christopher52b45052010-10-12 19:44:17 +00009745 !(DAG.getMachineFunction()
Bill Wendling67658342012-10-09 07:45:08 +00009746 .getFunction()->getFnAttributes()
9747 .hasAttribute(Attributes::NoImplicitFloat)) &&
Craig Topper1accb7e2012-01-10 06:54:16 +00009748 Subtarget->hasSSE1());
Dan Gohman320afb82010-10-12 18:00:49 +00009749 }
9750
9751 // Insert VAARG_64 node into the DAG
9752 // VAARG_64 returns two values: Variable Argument Address, Chain
9753 SmallVector<SDValue, 11> InstOps;
9754 InstOps.push_back(Chain);
9755 InstOps.push_back(SrcPtr);
9756 InstOps.push_back(DAG.getConstant(ArgSize, MVT::i32));
9757 InstOps.push_back(DAG.getConstant(ArgMode, MVT::i8));
9758 InstOps.push_back(DAG.getConstant(Align, MVT::i32));
9759 SDVTList VTs = DAG.getVTList(getPointerTy(), MVT::Other);
9760 SDValue VAARG = DAG.getMemIntrinsicNode(X86ISD::VAARG_64, dl,
9761 VTs, &InstOps[0], InstOps.size(),
9762 MVT::i64,
9763 MachinePointerInfo(SV),
9764 /*Align=*/0,
9765 /*Volatile=*/false,
9766 /*ReadMem=*/true,
9767 /*WriteMem=*/true);
9768 Chain = VAARG.getValue(1);
9769
9770 // Load the next argument and return it
9771 return DAG.getLoad(ArgVT, dl,
9772 Chain,
9773 VAARG,
9774 MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00009775 false, false, false, 0);
Dan Gohman9018e832008-05-10 01:26:14 +00009776}
9777
Craig Topper55b24052012-09-11 06:15:32 +00009778static SDValue LowerVACOPY(SDValue Op, const X86Subtarget *Subtarget,
9779 SelectionDAG &DAG) {
Evan Chengae642192007-03-02 23:16:35 +00009780 // X86-64 va_list is a struct { i32, i32, i8*, i8* }.
Dan Gohman28269132008-04-18 20:55:41 +00009781 assert(Subtarget->is64Bit() && "This code only handles 64-bit va_copy!");
Dan Gohman475871a2008-07-27 21:46:04 +00009782 SDValue Chain = Op.getOperand(0);
9783 SDValue DstPtr = Op.getOperand(1);
9784 SDValue SrcPtr = Op.getOperand(2);
Dan Gohman69de1932008-02-06 22:27:42 +00009785 const Value *DstSV = cast<SrcValueSDNode>(Op.getOperand(3))->getValue();
9786 const Value *SrcSV = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Chris Lattnere72f2022010-09-21 05:40:29 +00009787 DebugLoc DL = Op.getDebugLoc();
Evan Chengae642192007-03-02 23:16:35 +00009788
Chris Lattnere72f2022010-09-21 05:40:29 +00009789 return DAG.getMemcpy(Chain, DL, DstPtr, SrcPtr,
Mon P Wang20adc9d2010-04-04 03:10:48 +00009790 DAG.getIntPtrConstant(24), 8, /*isVolatile*/false,
Michael J. Spencerec38de22010-10-10 22:04:20 +00009791 false,
Chris Lattnere72f2022010-09-21 05:40:29 +00009792 MachinePointerInfo(DstSV), MachinePointerInfo(SrcSV));
Evan Chengae642192007-03-02 23:16:35 +00009793}
9794
Craig Topper80e46362012-01-23 06:16:53 +00009795// getTargetVShiftNOde - Handle vector element shifts where the shift amount
9796// may or may not be a constant. Takes immediate version of shift as input.
9797static SDValue getTargetVShiftNode(unsigned Opc, DebugLoc dl, EVT VT,
9798 SDValue SrcOp, SDValue ShAmt,
9799 SelectionDAG &DAG) {
9800 assert(ShAmt.getValueType() == MVT::i32 && "ShAmt is not i32");
9801
9802 if (isa<ConstantSDNode>(ShAmt)) {
Nadav Rotemd896e242012-07-15 20:27:43 +00009803 // Constant may be a TargetConstant. Use a regular constant.
9804 uint32_t ShiftAmt = cast<ConstantSDNode>(ShAmt)->getZExtValue();
Craig Topper80e46362012-01-23 06:16:53 +00009805 switch (Opc) {
9806 default: llvm_unreachable("Unknown target vector shift node");
9807 case X86ISD::VSHLI:
9808 case X86ISD::VSRLI:
9809 case X86ISD::VSRAI:
Nadav Rotemd896e242012-07-15 20:27:43 +00009810 return DAG.getNode(Opc, dl, VT, SrcOp,
9811 DAG.getConstant(ShiftAmt, MVT::i32));
Craig Topper80e46362012-01-23 06:16:53 +00009812 }
9813 }
9814
9815 // Change opcode to non-immediate version
9816 switch (Opc) {
9817 default: llvm_unreachable("Unknown target vector shift node");
9818 case X86ISD::VSHLI: Opc = X86ISD::VSHL; break;
9819 case X86ISD::VSRLI: Opc = X86ISD::VSRL; break;
9820 case X86ISD::VSRAI: Opc = X86ISD::VSRA; break;
9821 }
9822
9823 // Need to build a vector containing shift amount
9824 // Shift amount is 32-bits, but SSE instructions read 64-bit, so fill with 0
9825 SDValue ShOps[4];
9826 ShOps[0] = ShAmt;
9827 ShOps[1] = DAG.getConstant(0, MVT::i32);
Craig Topper6d688152012-08-14 07:43:25 +00009828 ShOps[2] = ShOps[3] = DAG.getUNDEF(MVT::i32);
Craig Topper80e46362012-01-23 06:16:53 +00009829 ShAmt = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, &ShOps[0], 4);
Nadav Rotem65f489f2012-07-14 22:26:05 +00009830
9831 // The return type has to be a 128-bit type with the same element
9832 // type as the input type.
9833 MVT EltVT = VT.getVectorElementType().getSimpleVT();
9834 EVT ShVT = MVT::getVectorVT(EltVT, 128/EltVT.getSizeInBits());
9835
9836 ShAmt = DAG.getNode(ISD::BITCAST, dl, ShVT, ShAmt);
Craig Topper80e46362012-01-23 06:16:53 +00009837 return DAG.getNode(Opc, dl, VT, SrcOp, ShAmt);
9838}
9839
Craig Topper55b24052012-09-11 06:15:32 +00009840static SDValue LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG) {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009841 DebugLoc dl = Op.getDebugLoc();
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00009842 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00009843 switch (IntNo) {
Dan Gohman475871a2008-07-27 21:46:04 +00009844 default: return SDValue(); // Don't custom lower most intrinsics.
Evan Cheng5759f972008-05-04 09:15:50 +00009845 // Comparison intrinsics.
Evan Cheng0db9fe62006-04-25 20:13:52 +00009846 case Intrinsic::x86_sse_comieq_ss:
9847 case Intrinsic::x86_sse_comilt_ss:
9848 case Intrinsic::x86_sse_comile_ss:
9849 case Intrinsic::x86_sse_comigt_ss:
9850 case Intrinsic::x86_sse_comige_ss:
9851 case Intrinsic::x86_sse_comineq_ss:
9852 case Intrinsic::x86_sse_ucomieq_ss:
9853 case Intrinsic::x86_sse_ucomilt_ss:
9854 case Intrinsic::x86_sse_ucomile_ss:
9855 case Intrinsic::x86_sse_ucomigt_ss:
9856 case Intrinsic::x86_sse_ucomige_ss:
9857 case Intrinsic::x86_sse_ucomineq_ss:
9858 case Intrinsic::x86_sse2_comieq_sd:
9859 case Intrinsic::x86_sse2_comilt_sd:
9860 case Intrinsic::x86_sse2_comile_sd:
9861 case Intrinsic::x86_sse2_comigt_sd:
9862 case Intrinsic::x86_sse2_comige_sd:
9863 case Intrinsic::x86_sse2_comineq_sd:
9864 case Intrinsic::x86_sse2_ucomieq_sd:
9865 case Intrinsic::x86_sse2_ucomilt_sd:
9866 case Intrinsic::x86_sse2_ucomile_sd:
9867 case Intrinsic::x86_sse2_ucomigt_sd:
9868 case Intrinsic::x86_sse2_ucomige_sd:
9869 case Intrinsic::x86_sse2_ucomineq_sd: {
Craig Topper6d688152012-08-14 07:43:25 +00009870 unsigned Opc;
9871 ISD::CondCode CC;
Evan Cheng0db9fe62006-04-25 20:13:52 +00009872 switch (IntNo) {
Craig Topper86c7c582012-01-30 01:10:15 +00009873 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00009874 case Intrinsic::x86_sse_comieq_ss:
9875 case Intrinsic::x86_sse2_comieq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009876 Opc = X86ISD::COMI;
9877 CC = ISD::SETEQ;
9878 break;
Evan Cheng6be2c582006-04-05 23:38:46 +00009879 case Intrinsic::x86_sse_comilt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009880 case Intrinsic::x86_sse2_comilt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009881 Opc = X86ISD::COMI;
9882 CC = ISD::SETLT;
9883 break;
9884 case Intrinsic::x86_sse_comile_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009885 case Intrinsic::x86_sse2_comile_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009886 Opc = X86ISD::COMI;
9887 CC = ISD::SETLE;
9888 break;
9889 case Intrinsic::x86_sse_comigt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009890 case Intrinsic::x86_sse2_comigt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009891 Opc = X86ISD::COMI;
9892 CC = ISD::SETGT;
9893 break;
9894 case Intrinsic::x86_sse_comige_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009895 case Intrinsic::x86_sse2_comige_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009896 Opc = X86ISD::COMI;
9897 CC = ISD::SETGE;
9898 break;
9899 case Intrinsic::x86_sse_comineq_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009900 case Intrinsic::x86_sse2_comineq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009901 Opc = X86ISD::COMI;
9902 CC = ISD::SETNE;
9903 break;
9904 case Intrinsic::x86_sse_ucomieq_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009905 case Intrinsic::x86_sse2_ucomieq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009906 Opc = X86ISD::UCOMI;
9907 CC = ISD::SETEQ;
9908 break;
9909 case Intrinsic::x86_sse_ucomilt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009910 case Intrinsic::x86_sse2_ucomilt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009911 Opc = X86ISD::UCOMI;
9912 CC = ISD::SETLT;
9913 break;
9914 case Intrinsic::x86_sse_ucomile_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009915 case Intrinsic::x86_sse2_ucomile_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009916 Opc = X86ISD::UCOMI;
9917 CC = ISD::SETLE;
9918 break;
9919 case Intrinsic::x86_sse_ucomigt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009920 case Intrinsic::x86_sse2_ucomigt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009921 Opc = X86ISD::UCOMI;
9922 CC = ISD::SETGT;
9923 break;
9924 case Intrinsic::x86_sse_ucomige_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009925 case Intrinsic::x86_sse2_ucomige_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009926 Opc = X86ISD::UCOMI;
9927 CC = ISD::SETGE;
9928 break;
9929 case Intrinsic::x86_sse_ucomineq_ss:
9930 case Intrinsic::x86_sse2_ucomineq_sd:
9931 Opc = X86ISD::UCOMI;
9932 CC = ISD::SETNE;
9933 break;
Evan Cheng6be2c582006-04-05 23:38:46 +00009934 }
Evan Cheng734503b2006-09-11 02:19:56 +00009935
Dan Gohman475871a2008-07-27 21:46:04 +00009936 SDValue LHS = Op.getOperand(1);
9937 SDValue RHS = Op.getOperand(2);
Chris Lattner1c39d4c2008-12-24 23:53:05 +00009938 unsigned X86CC = TranslateX86CC(CC, true, LHS, RHS, DAG);
Dan Gohman1a492952009-10-20 16:22:37 +00009939 assert(X86CC != X86::COND_INVALID && "Unexpected illegal condition!");
Owen Anderson825b72b2009-08-11 20:47:22 +00009940 SDValue Cond = DAG.getNode(Opc, dl, MVT::i32, LHS, RHS);
9941 SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
9942 DAG.getConstant(X86CC, MVT::i8), Cond);
9943 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC);
Evan Cheng6be2c582006-04-05 23:38:46 +00009944 }
Craig Topper6d688152012-08-14 07:43:25 +00009945
Duncan Sands04aa4ae2011-09-23 16:10:22 +00009946 // Arithmetic intrinsics.
Craig Topper5b209e82012-02-05 03:14:49 +00009947 case Intrinsic::x86_sse2_pmulu_dq:
9948 case Intrinsic::x86_avx2_pmulu_dq:
9949 return DAG.getNode(X86ISD::PMULUDQ, dl, Op.getValueType(),
9950 Op.getOperand(1), Op.getOperand(2));
Craig Topper6d688152012-08-14 07:43:25 +00009951
9952 // SSE3/AVX horizontal add/sub intrinsics
Duncan Sands04aa4ae2011-09-23 16:10:22 +00009953 case Intrinsic::x86_sse3_hadd_ps:
9954 case Intrinsic::x86_sse3_hadd_pd:
9955 case Intrinsic::x86_avx_hadd_ps_256:
9956 case Intrinsic::x86_avx_hadd_pd_256:
Duncan Sands04aa4ae2011-09-23 16:10:22 +00009957 case Intrinsic::x86_sse3_hsub_ps:
9958 case Intrinsic::x86_sse3_hsub_pd:
9959 case Intrinsic::x86_avx_hsub_ps_256:
9960 case Intrinsic::x86_avx_hsub_pd_256:
Craig Topper4bb3f342012-01-25 05:37:32 +00009961 case Intrinsic::x86_ssse3_phadd_w_128:
9962 case Intrinsic::x86_ssse3_phadd_d_128:
9963 case Intrinsic::x86_avx2_phadd_w:
9964 case Intrinsic::x86_avx2_phadd_d:
Craig Topper4bb3f342012-01-25 05:37:32 +00009965 case Intrinsic::x86_ssse3_phsub_w_128:
9966 case Intrinsic::x86_ssse3_phsub_d_128:
9967 case Intrinsic::x86_avx2_phsub_w:
Craig Topper6d688152012-08-14 07:43:25 +00009968 case Intrinsic::x86_avx2_phsub_d: {
9969 unsigned Opcode;
9970 switch (IntNo) {
9971 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
9972 case Intrinsic::x86_sse3_hadd_ps:
9973 case Intrinsic::x86_sse3_hadd_pd:
9974 case Intrinsic::x86_avx_hadd_ps_256:
9975 case Intrinsic::x86_avx_hadd_pd_256:
9976 Opcode = X86ISD::FHADD;
9977 break;
9978 case Intrinsic::x86_sse3_hsub_ps:
9979 case Intrinsic::x86_sse3_hsub_pd:
9980 case Intrinsic::x86_avx_hsub_ps_256:
9981 case Intrinsic::x86_avx_hsub_pd_256:
9982 Opcode = X86ISD::FHSUB;
9983 break;
9984 case Intrinsic::x86_ssse3_phadd_w_128:
9985 case Intrinsic::x86_ssse3_phadd_d_128:
9986 case Intrinsic::x86_avx2_phadd_w:
9987 case Intrinsic::x86_avx2_phadd_d:
9988 Opcode = X86ISD::HADD;
9989 break;
9990 case Intrinsic::x86_ssse3_phsub_w_128:
9991 case Intrinsic::x86_ssse3_phsub_d_128:
9992 case Intrinsic::x86_avx2_phsub_w:
9993 case Intrinsic::x86_avx2_phsub_d:
9994 Opcode = X86ISD::HSUB;
9995 break;
9996 }
9997 return DAG.getNode(Opcode, dl, Op.getValueType(),
Craig Topper4bb3f342012-01-25 05:37:32 +00009998 Op.getOperand(1), Op.getOperand(2));
Craig Topper6d688152012-08-14 07:43:25 +00009999 }
10000
10001 // AVX2 variable shift intrinsics
Craig Topper98fc7292011-11-19 17:46:46 +000010002 case Intrinsic::x86_avx2_psllv_d:
10003 case Intrinsic::x86_avx2_psllv_q:
10004 case Intrinsic::x86_avx2_psllv_d_256:
10005 case Intrinsic::x86_avx2_psllv_q_256:
Craig Topper98fc7292011-11-19 17:46:46 +000010006 case Intrinsic::x86_avx2_psrlv_d:
10007 case Intrinsic::x86_avx2_psrlv_q:
10008 case Intrinsic::x86_avx2_psrlv_d_256:
10009 case Intrinsic::x86_avx2_psrlv_q_256:
Craig Topper98fc7292011-11-19 17:46:46 +000010010 case Intrinsic::x86_avx2_psrav_d:
Craig Topper6d688152012-08-14 07:43:25 +000010011 case Intrinsic::x86_avx2_psrav_d_256: {
10012 unsigned Opcode;
10013 switch (IntNo) {
10014 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
10015 case Intrinsic::x86_avx2_psllv_d:
10016 case Intrinsic::x86_avx2_psllv_q:
10017 case Intrinsic::x86_avx2_psllv_d_256:
10018 case Intrinsic::x86_avx2_psllv_q_256:
10019 Opcode = ISD::SHL;
10020 break;
10021 case Intrinsic::x86_avx2_psrlv_d:
10022 case Intrinsic::x86_avx2_psrlv_q:
10023 case Intrinsic::x86_avx2_psrlv_d_256:
10024 case Intrinsic::x86_avx2_psrlv_q_256:
10025 Opcode = ISD::SRL;
10026 break;
10027 case Intrinsic::x86_avx2_psrav_d:
10028 case Intrinsic::x86_avx2_psrav_d_256:
10029 Opcode = ISD::SRA;
10030 break;
10031 }
10032 return DAG.getNode(Opcode, dl, Op.getValueType(),
10033 Op.getOperand(1), Op.getOperand(2));
10034 }
10035
Craig Topper969ba282012-01-25 06:43:11 +000010036 case Intrinsic::x86_ssse3_pshuf_b_128:
10037 case Intrinsic::x86_avx2_pshuf_b:
10038 return DAG.getNode(X86ISD::PSHUFB, dl, Op.getValueType(),
10039 Op.getOperand(1), Op.getOperand(2));
Craig Topper6d688152012-08-14 07:43:25 +000010040
Craig Topper969ba282012-01-25 06:43:11 +000010041 case Intrinsic::x86_ssse3_psign_b_128:
10042 case Intrinsic::x86_ssse3_psign_w_128:
10043 case Intrinsic::x86_ssse3_psign_d_128:
10044 case Intrinsic::x86_avx2_psign_b:
10045 case Intrinsic::x86_avx2_psign_w:
10046 case Intrinsic::x86_avx2_psign_d:
10047 return DAG.getNode(X86ISD::PSIGN, dl, Op.getValueType(),
10048 Op.getOperand(1), Op.getOperand(2));
Craig Topper6d688152012-08-14 07:43:25 +000010049
Craig Toppere566cd02012-01-26 07:18:03 +000010050 case Intrinsic::x86_sse41_insertps:
10051 return DAG.getNode(X86ISD::INSERTPS, dl, Op.getValueType(),
10052 Op.getOperand(1), Op.getOperand(2), Op.getOperand(3));
Craig Topper6d688152012-08-14 07:43:25 +000010053
Craig Toppere566cd02012-01-26 07:18:03 +000010054 case Intrinsic::x86_avx_vperm2f128_ps_256:
10055 case Intrinsic::x86_avx_vperm2f128_pd_256:
10056 case Intrinsic::x86_avx_vperm2f128_si_256:
10057 case Intrinsic::x86_avx2_vperm2i128:
10058 return DAG.getNode(X86ISD::VPERM2X128, dl, Op.getValueType(),
10059 Op.getOperand(1), Op.getOperand(2), Op.getOperand(3));
Craig Topper6d688152012-08-14 07:43:25 +000010060
Craig Topperffa6c402012-04-16 07:13:00 +000010061 case Intrinsic::x86_avx2_permd:
10062 case Intrinsic::x86_avx2_permps:
10063 // Operands intentionally swapped. Mask is last operand to intrinsic,
10064 // but second operand for node/intruction.
10065 return DAG.getNode(X86ISD::VPERMV, dl, Op.getValueType(),
10066 Op.getOperand(2), Op.getOperand(1));
Craig Topper98fc7292011-11-19 17:46:46 +000010067
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +000010068 // ptest and testp intrinsics. The intrinsic these come from are designed to
10069 // return an integer value, not just an instruction so lower it to the ptest
10070 // or testp pattern and a setcc for the result.
Eric Christopher71c67532009-07-29 00:28:05 +000010071 case Intrinsic::x86_sse41_ptestz:
10072 case Intrinsic::x86_sse41_ptestc:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +000010073 case Intrinsic::x86_sse41_ptestnzc:
10074 case Intrinsic::x86_avx_ptestz_256:
10075 case Intrinsic::x86_avx_ptestc_256:
10076 case Intrinsic::x86_avx_ptestnzc_256:
10077 case Intrinsic::x86_avx_vtestz_ps:
10078 case Intrinsic::x86_avx_vtestc_ps:
10079 case Intrinsic::x86_avx_vtestnzc_ps:
10080 case Intrinsic::x86_avx_vtestz_pd:
10081 case Intrinsic::x86_avx_vtestc_pd:
10082 case Intrinsic::x86_avx_vtestnzc_pd:
10083 case Intrinsic::x86_avx_vtestz_ps_256:
10084 case Intrinsic::x86_avx_vtestc_ps_256:
10085 case Intrinsic::x86_avx_vtestnzc_ps_256:
10086 case Intrinsic::x86_avx_vtestz_pd_256:
10087 case Intrinsic::x86_avx_vtestc_pd_256:
10088 case Intrinsic::x86_avx_vtestnzc_pd_256: {
10089 bool IsTestPacked = false;
Craig Topper6d688152012-08-14 07:43:25 +000010090 unsigned X86CC;
Eric Christopher71c67532009-07-29 00:28:05 +000010091 switch (IntNo) {
Eric Christopher978dae32009-07-29 18:14:04 +000010092 default: llvm_unreachable("Bad fallthrough in Intrinsic lowering.");
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +000010093 case Intrinsic::x86_avx_vtestz_ps:
10094 case Intrinsic::x86_avx_vtestz_pd:
10095 case Intrinsic::x86_avx_vtestz_ps_256:
10096 case Intrinsic::x86_avx_vtestz_pd_256:
10097 IsTestPacked = true; // Fallthrough
Eric Christopher71c67532009-07-29 00:28:05 +000010098 case Intrinsic::x86_sse41_ptestz:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +000010099 case Intrinsic::x86_avx_ptestz_256:
Eric Christopher71c67532009-07-29 00:28:05 +000010100 // ZF = 1
10101 X86CC = X86::COND_E;
10102 break;
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +000010103 case Intrinsic::x86_avx_vtestc_ps:
10104 case Intrinsic::x86_avx_vtestc_pd:
10105 case Intrinsic::x86_avx_vtestc_ps_256:
10106 case Intrinsic::x86_avx_vtestc_pd_256:
10107 IsTestPacked = true; // Fallthrough
Eric Christopher71c67532009-07-29 00:28:05 +000010108 case Intrinsic::x86_sse41_ptestc:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +000010109 case Intrinsic::x86_avx_ptestc_256:
Eric Christopher71c67532009-07-29 00:28:05 +000010110 // CF = 1
10111 X86CC = X86::COND_B;
10112 break;
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +000010113 case Intrinsic::x86_avx_vtestnzc_ps:
10114 case Intrinsic::x86_avx_vtestnzc_pd:
10115 case Intrinsic::x86_avx_vtestnzc_ps_256:
10116 case Intrinsic::x86_avx_vtestnzc_pd_256:
10117 IsTestPacked = true; // Fallthrough
Eric Christopherfd179292009-08-27 18:07:15 +000010118 case Intrinsic::x86_sse41_ptestnzc:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +000010119 case Intrinsic::x86_avx_ptestnzc_256:
Eric Christopher71c67532009-07-29 00:28:05 +000010120 // ZF and CF = 0
10121 X86CC = X86::COND_A;
10122 break;
10123 }
Eric Christopherfd179292009-08-27 18:07:15 +000010124
Eric Christopher71c67532009-07-29 00:28:05 +000010125 SDValue LHS = Op.getOperand(1);
10126 SDValue RHS = Op.getOperand(2);
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +000010127 unsigned TestOpc = IsTestPacked ? X86ISD::TESTP : X86ISD::PTEST;
10128 SDValue Test = DAG.getNode(TestOpc, dl, MVT::i32, LHS, RHS);
Owen Anderson825b72b2009-08-11 20:47:22 +000010129 SDValue CC = DAG.getConstant(X86CC, MVT::i8);
10130 SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8, CC, Test);
10131 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC);
Eric Christopher71c67532009-07-29 00:28:05 +000010132 }
Evan Cheng5759f972008-05-04 09:15:50 +000010133
Craig Topper80e46362012-01-23 06:16:53 +000010134 // SSE/AVX shift intrinsics
10135 case Intrinsic::x86_sse2_psll_w:
10136 case Intrinsic::x86_sse2_psll_d:
10137 case Intrinsic::x86_sse2_psll_q:
10138 case Intrinsic::x86_avx2_psll_w:
10139 case Intrinsic::x86_avx2_psll_d:
10140 case Intrinsic::x86_avx2_psll_q:
Craig Topper80e46362012-01-23 06:16:53 +000010141 case Intrinsic::x86_sse2_psrl_w:
10142 case Intrinsic::x86_sse2_psrl_d:
10143 case Intrinsic::x86_sse2_psrl_q:
10144 case Intrinsic::x86_avx2_psrl_w:
10145 case Intrinsic::x86_avx2_psrl_d:
10146 case Intrinsic::x86_avx2_psrl_q:
Craig Topper80e46362012-01-23 06:16:53 +000010147 case Intrinsic::x86_sse2_psra_w:
10148 case Intrinsic::x86_sse2_psra_d:
10149 case Intrinsic::x86_avx2_psra_w:
Craig Topper6d688152012-08-14 07:43:25 +000010150 case Intrinsic::x86_avx2_psra_d: {
10151 unsigned Opcode;
10152 switch (IntNo) {
10153 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
10154 case Intrinsic::x86_sse2_psll_w:
10155 case Intrinsic::x86_sse2_psll_d:
10156 case Intrinsic::x86_sse2_psll_q:
10157 case Intrinsic::x86_avx2_psll_w:
10158 case Intrinsic::x86_avx2_psll_d:
10159 case Intrinsic::x86_avx2_psll_q:
10160 Opcode = X86ISD::VSHL;
10161 break;
10162 case Intrinsic::x86_sse2_psrl_w:
10163 case Intrinsic::x86_sse2_psrl_d:
10164 case Intrinsic::x86_sse2_psrl_q:
10165 case Intrinsic::x86_avx2_psrl_w:
10166 case Intrinsic::x86_avx2_psrl_d:
10167 case Intrinsic::x86_avx2_psrl_q:
10168 Opcode = X86ISD::VSRL;
10169 break;
10170 case Intrinsic::x86_sse2_psra_w:
10171 case Intrinsic::x86_sse2_psra_d:
10172 case Intrinsic::x86_avx2_psra_w:
10173 case Intrinsic::x86_avx2_psra_d:
10174 Opcode = X86ISD::VSRA;
10175 break;
10176 }
10177 return DAG.getNode(Opcode, dl, Op.getValueType(),
Craig Topper80e46362012-01-23 06:16:53 +000010178 Op.getOperand(1), Op.getOperand(2));
Craig Topper6d688152012-08-14 07:43:25 +000010179 }
10180
10181 // SSE/AVX immediate shift intrinsics
Evan Cheng5759f972008-05-04 09:15:50 +000010182 case Intrinsic::x86_sse2_pslli_w:
10183 case Intrinsic::x86_sse2_pslli_d:
10184 case Intrinsic::x86_sse2_pslli_q:
Craig Topper80e46362012-01-23 06:16:53 +000010185 case Intrinsic::x86_avx2_pslli_w:
10186 case Intrinsic::x86_avx2_pslli_d:
10187 case Intrinsic::x86_avx2_pslli_q:
Evan Cheng5759f972008-05-04 09:15:50 +000010188 case Intrinsic::x86_sse2_psrli_w:
10189 case Intrinsic::x86_sse2_psrli_d:
10190 case Intrinsic::x86_sse2_psrli_q:
Craig Topper80e46362012-01-23 06:16:53 +000010191 case Intrinsic::x86_avx2_psrli_w:
10192 case Intrinsic::x86_avx2_psrli_d:
10193 case Intrinsic::x86_avx2_psrli_q:
Evan Cheng5759f972008-05-04 09:15:50 +000010194 case Intrinsic::x86_sse2_psrai_w:
10195 case Intrinsic::x86_sse2_psrai_d:
Craig Topper80e46362012-01-23 06:16:53 +000010196 case Intrinsic::x86_avx2_psrai_w:
Craig Topper6d688152012-08-14 07:43:25 +000010197 case Intrinsic::x86_avx2_psrai_d: {
10198 unsigned Opcode;
10199 switch (IntNo) {
10200 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
10201 case Intrinsic::x86_sse2_pslli_w:
10202 case Intrinsic::x86_sse2_pslli_d:
10203 case Intrinsic::x86_sse2_pslli_q:
10204 case Intrinsic::x86_avx2_pslli_w:
10205 case Intrinsic::x86_avx2_pslli_d:
10206 case Intrinsic::x86_avx2_pslli_q:
10207 Opcode = X86ISD::VSHLI;
10208 break;
10209 case Intrinsic::x86_sse2_psrli_w:
10210 case Intrinsic::x86_sse2_psrli_d:
10211 case Intrinsic::x86_sse2_psrli_q:
10212 case Intrinsic::x86_avx2_psrli_w:
10213 case Intrinsic::x86_avx2_psrli_d:
10214 case Intrinsic::x86_avx2_psrli_q:
10215 Opcode = X86ISD::VSRLI;
10216 break;
10217 case Intrinsic::x86_sse2_psrai_w:
10218 case Intrinsic::x86_sse2_psrai_d:
10219 case Intrinsic::x86_avx2_psrai_w:
10220 case Intrinsic::x86_avx2_psrai_d:
10221 Opcode = X86ISD::VSRAI;
10222 break;
10223 }
10224 return getTargetVShiftNode(Opcode, dl, Op.getValueType(),
Craig Topper80e46362012-01-23 06:16:53 +000010225 Op.getOperand(1), Op.getOperand(2), DAG);
Craig Topper6d688152012-08-14 07:43:25 +000010226 }
10227
Craig Topper4feb6472012-08-06 06:22:36 +000010228 case Intrinsic::x86_sse42_pcmpistria128:
10229 case Intrinsic::x86_sse42_pcmpestria128:
10230 case Intrinsic::x86_sse42_pcmpistric128:
10231 case Intrinsic::x86_sse42_pcmpestric128:
10232 case Intrinsic::x86_sse42_pcmpistrio128:
10233 case Intrinsic::x86_sse42_pcmpestrio128:
10234 case Intrinsic::x86_sse42_pcmpistris128:
10235 case Intrinsic::x86_sse42_pcmpestris128:
10236 case Intrinsic::x86_sse42_pcmpistriz128:
10237 case Intrinsic::x86_sse42_pcmpestriz128: {
10238 unsigned Opcode;
10239 unsigned X86CC;
10240 switch (IntNo) {
10241 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
10242 case Intrinsic::x86_sse42_pcmpistria128:
10243 Opcode = X86ISD::PCMPISTRI;
10244 X86CC = X86::COND_A;
10245 break;
10246 case Intrinsic::x86_sse42_pcmpestria128:
10247 Opcode = X86ISD::PCMPESTRI;
10248 X86CC = X86::COND_A;
10249 break;
10250 case Intrinsic::x86_sse42_pcmpistric128:
10251 Opcode = X86ISD::PCMPISTRI;
10252 X86CC = X86::COND_B;
10253 break;
10254 case Intrinsic::x86_sse42_pcmpestric128:
10255 Opcode = X86ISD::PCMPESTRI;
10256 X86CC = X86::COND_B;
10257 break;
10258 case Intrinsic::x86_sse42_pcmpistrio128:
10259 Opcode = X86ISD::PCMPISTRI;
10260 X86CC = X86::COND_O;
10261 break;
10262 case Intrinsic::x86_sse42_pcmpestrio128:
10263 Opcode = X86ISD::PCMPESTRI;
10264 X86CC = X86::COND_O;
10265 break;
10266 case Intrinsic::x86_sse42_pcmpistris128:
10267 Opcode = X86ISD::PCMPISTRI;
10268 X86CC = X86::COND_S;
10269 break;
10270 case Intrinsic::x86_sse42_pcmpestris128:
10271 Opcode = X86ISD::PCMPESTRI;
10272 X86CC = X86::COND_S;
10273 break;
10274 case Intrinsic::x86_sse42_pcmpistriz128:
10275 Opcode = X86ISD::PCMPISTRI;
10276 X86CC = X86::COND_E;
10277 break;
10278 case Intrinsic::x86_sse42_pcmpestriz128:
10279 Opcode = X86ISD::PCMPESTRI;
10280 X86CC = X86::COND_E;
10281 break;
10282 }
10283 SmallVector<SDValue, 5> NewOps;
10284 NewOps.append(Op->op_begin()+1, Op->op_end());
10285 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::i32);
10286 SDValue PCMP = DAG.getNode(Opcode, dl, VTs, NewOps.data(), NewOps.size());
10287 SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
10288 DAG.getConstant(X86CC, MVT::i8),
10289 SDValue(PCMP.getNode(), 1));
10290 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC);
10291 }
Craig Topper6d688152012-08-14 07:43:25 +000010292
Craig Topper4feb6472012-08-06 06:22:36 +000010293 case Intrinsic::x86_sse42_pcmpistri128:
10294 case Intrinsic::x86_sse42_pcmpestri128: {
10295 unsigned Opcode;
10296 if (IntNo == Intrinsic::x86_sse42_pcmpistri128)
10297 Opcode = X86ISD::PCMPISTRI;
10298 else
10299 Opcode = X86ISD::PCMPESTRI;
10300
10301 SmallVector<SDValue, 5> NewOps;
10302 NewOps.append(Op->op_begin()+1, Op->op_end());
10303 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::i32);
10304 return DAG.getNode(Opcode, dl, VTs, NewOps.data(), NewOps.size());
10305 }
Craig Topper0e292372012-08-24 04:03:22 +000010306 case Intrinsic::x86_fma_vfmadd_ps:
10307 case Intrinsic::x86_fma_vfmadd_pd:
10308 case Intrinsic::x86_fma_vfmsub_ps:
10309 case Intrinsic::x86_fma_vfmsub_pd:
10310 case Intrinsic::x86_fma_vfnmadd_ps:
10311 case Intrinsic::x86_fma_vfnmadd_pd:
10312 case Intrinsic::x86_fma_vfnmsub_ps:
10313 case Intrinsic::x86_fma_vfnmsub_pd:
10314 case Intrinsic::x86_fma_vfmaddsub_ps:
10315 case Intrinsic::x86_fma_vfmaddsub_pd:
10316 case Intrinsic::x86_fma_vfmsubadd_ps:
10317 case Intrinsic::x86_fma_vfmsubadd_pd:
10318 case Intrinsic::x86_fma_vfmadd_ps_256:
10319 case Intrinsic::x86_fma_vfmadd_pd_256:
10320 case Intrinsic::x86_fma_vfmsub_ps_256:
10321 case Intrinsic::x86_fma_vfmsub_pd_256:
10322 case Intrinsic::x86_fma_vfnmadd_ps_256:
10323 case Intrinsic::x86_fma_vfnmadd_pd_256:
10324 case Intrinsic::x86_fma_vfnmsub_ps_256:
10325 case Intrinsic::x86_fma_vfnmsub_pd_256:
10326 case Intrinsic::x86_fma_vfmaddsub_ps_256:
10327 case Intrinsic::x86_fma_vfmaddsub_pd_256:
10328 case Intrinsic::x86_fma_vfmsubadd_ps_256:
10329 case Intrinsic::x86_fma_vfmsubadd_pd_256: {
Craig Topper0e292372012-08-24 04:03:22 +000010330 unsigned Opc;
10331 switch (IntNo) {
10332 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
10333 case Intrinsic::x86_fma_vfmadd_ps:
10334 case Intrinsic::x86_fma_vfmadd_pd:
10335 case Intrinsic::x86_fma_vfmadd_ps_256:
10336 case Intrinsic::x86_fma_vfmadd_pd_256:
10337 Opc = X86ISD::FMADD;
10338 break;
10339 case Intrinsic::x86_fma_vfmsub_ps:
10340 case Intrinsic::x86_fma_vfmsub_pd:
10341 case Intrinsic::x86_fma_vfmsub_ps_256:
10342 case Intrinsic::x86_fma_vfmsub_pd_256:
10343 Opc = X86ISD::FMSUB;
10344 break;
10345 case Intrinsic::x86_fma_vfnmadd_ps:
10346 case Intrinsic::x86_fma_vfnmadd_pd:
10347 case Intrinsic::x86_fma_vfnmadd_ps_256:
10348 case Intrinsic::x86_fma_vfnmadd_pd_256:
10349 Opc = X86ISD::FNMADD;
10350 break;
10351 case Intrinsic::x86_fma_vfnmsub_ps:
10352 case Intrinsic::x86_fma_vfnmsub_pd:
10353 case Intrinsic::x86_fma_vfnmsub_ps_256:
10354 case Intrinsic::x86_fma_vfnmsub_pd_256:
10355 Opc = X86ISD::FNMSUB;
10356 break;
10357 case Intrinsic::x86_fma_vfmaddsub_ps:
10358 case Intrinsic::x86_fma_vfmaddsub_pd:
10359 case Intrinsic::x86_fma_vfmaddsub_ps_256:
10360 case Intrinsic::x86_fma_vfmaddsub_pd_256:
10361 Opc = X86ISD::FMADDSUB;
10362 break;
10363 case Intrinsic::x86_fma_vfmsubadd_ps:
10364 case Intrinsic::x86_fma_vfmsubadd_pd:
10365 case Intrinsic::x86_fma_vfmsubadd_ps_256:
10366 case Intrinsic::x86_fma_vfmsubadd_pd_256:
10367 Opc = X86ISD::FMSUBADD;
10368 break;
10369 }
10370
10371 return DAG.getNode(Opc, dl, Op.getValueType(), Op.getOperand(1),
10372 Op.getOperand(2), Op.getOperand(3));
10373 }
Evan Cheng38bcbaf2005-12-23 07:31:11 +000010374 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000010375}
Evan Cheng72261582005-12-20 06:22:03 +000010376
Craig Topper55b24052012-09-11 06:15:32 +000010377static SDValue LowerINTRINSIC_W_CHAIN(SDValue Op, SelectionDAG &DAG) {
Benjamin Kramerb9bee042012-07-12 09:31:43 +000010378 DebugLoc dl = Op.getDebugLoc();
10379 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
10380 switch (IntNo) {
10381 default: return SDValue(); // Don't custom lower most intrinsics.
10382
10383 // RDRAND intrinsics.
10384 case Intrinsic::x86_rdrand_16:
10385 case Intrinsic::x86_rdrand_32:
10386 case Intrinsic::x86_rdrand_64: {
10387 // Emit the node with the right value type.
Benjamin Kramerfeae00a2012-07-12 18:14:57 +000010388 SDVTList VTs = DAG.getVTList(Op->getValueType(0), MVT::Glue, MVT::Other);
10389 SDValue Result = DAG.getNode(X86ISD::RDRAND, dl, VTs, Op.getOperand(0));
Benjamin Kramerb9bee042012-07-12 09:31:43 +000010390
10391 // If the value returned by RDRAND was valid (CF=1), return 1. Otherwise
10392 // return the value from Rand, which is always 0, casted to i32.
10393 SDValue Ops[] = { DAG.getZExtOrTrunc(Result, dl, Op->getValueType(1)),
10394 DAG.getConstant(1, Op->getValueType(1)),
10395 DAG.getConstant(X86::COND_B, MVT::i32),
10396 SDValue(Result.getNode(), 1) };
10397 SDValue isValid = DAG.getNode(X86ISD::CMOV, dl,
10398 DAG.getVTList(Op->getValueType(1), MVT::Glue),
10399 Ops, 4);
10400
10401 // Return { result, isValid, chain }.
10402 return DAG.getNode(ISD::MERGE_VALUES, dl, Op->getVTList(), Result, isValid,
Benjamin Kramerfeae00a2012-07-12 18:14:57 +000010403 SDValue(Result.getNode(), 2));
Benjamin Kramerb9bee042012-07-12 09:31:43 +000010404 }
10405 }
10406}
10407
Dan Gohmand858e902010-04-17 15:26:15 +000010408SDValue X86TargetLowering::LowerRETURNADDR(SDValue Op,
10409 SelectionDAG &DAG) const {
Evan Cheng2457f2c2010-05-22 01:47:14 +000010410 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
10411 MFI->setReturnAddressIsTaken(true);
10412
Bill Wendling64e87322009-01-16 19:25:27 +000010413 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Dale Johannesen6f38cb62009-02-07 19:59:05 +000010414 DebugLoc dl = Op.getDebugLoc();
Bill Wendling64e87322009-01-16 19:25:27 +000010415
10416 if (Depth > 0) {
10417 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
10418 SDValue Offset =
Micah Villmow2c39b152012-10-15 16:24:29 +000010419 DAG.getConstant(TD->getPointerSize(0),
Owen Anderson825b72b2009-08-11 20:47:22 +000010420 Subtarget->is64Bit() ? MVT::i64 : MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +000010421 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
Scott Michelfdc40a02009-02-17 22:15:04 +000010422 DAG.getNode(ISD::ADD, dl, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +000010423 FrameAddr, Offset),
Pete Cooperd752e0f2011-11-08 18:42:53 +000010424 MachinePointerInfo(), false, false, false, 0);
Bill Wendling64e87322009-01-16 19:25:27 +000010425 }
10426
10427 // Just load the return address.
Dan Gohman475871a2008-07-27 21:46:04 +000010428 SDValue RetAddrFI = getReturnAddressFrameIndex(DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +000010429 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000010430 RetAddrFI, MachinePointerInfo(), false, false, false, 0);
Nate Begemanbcc5f362007-01-29 22:58:52 +000010431}
10432
Dan Gohmand858e902010-04-17 15:26:15 +000010433SDValue X86TargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng184793f2008-09-27 01:56:22 +000010434 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
10435 MFI->setFrameAddressIsTaken(true);
Evan Cheng2457f2c2010-05-22 01:47:14 +000010436
Owen Andersone50ed302009-08-10 22:56:29 +000010437 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +000010438 DebugLoc dl = Op.getDebugLoc(); // FIXME probably not meaningful
Evan Cheng184793f2008-09-27 01:56:22 +000010439 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
10440 unsigned FrameReg = Subtarget->is64Bit() ? X86::RBP : X86::EBP;
Dale Johannesendd64c412009-02-04 00:33:20 +000010441 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, VT);
Evan Cheng184793f2008-09-27 01:56:22 +000010442 while (Depth--)
Chris Lattner51abfe42010-09-21 06:02:19 +000010443 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr,
10444 MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000010445 false, false, false, 0);
Evan Cheng184793f2008-09-27 01:56:22 +000010446 return FrameAddr;
Nate Begemanbcc5f362007-01-29 22:58:52 +000010447}
10448
Dan Gohman475871a2008-07-27 21:46:04 +000010449SDValue X86TargetLowering::LowerFRAME_TO_ARGS_OFFSET(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +000010450 SelectionDAG &DAG) const {
Micah Villmow2c39b152012-10-15 16:24:29 +000010451 return DAG.getIntPtrConstant(2*TD->getPointerSize(0));
Anton Korobeynikov2365f512007-07-14 14:06:15 +000010452}
10453
Dan Gohmand858e902010-04-17 15:26:15 +000010454SDValue X86TargetLowering::LowerEH_RETURN(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +000010455 SDValue Chain = Op.getOperand(0);
10456 SDValue Offset = Op.getOperand(1);
10457 SDValue Handler = Op.getOperand(2);
Dale Johannesen6f38cb62009-02-07 19:59:05 +000010458 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov2365f512007-07-14 14:06:15 +000010459
Dan Gohmand8816272010-08-11 18:14:00 +000010460 SDValue Frame = DAG.getCopyFromReg(DAG.getEntryNode(), dl,
10461 Subtarget->is64Bit() ? X86::RBP : X86::EBP,
10462 getPointerTy());
Anton Korobeynikovb84c1672008-09-08 21:12:47 +000010463 unsigned StoreAddrReg = (Subtarget->is64Bit() ? X86::RCX : X86::ECX);
Anton Korobeynikov2365f512007-07-14 14:06:15 +000010464
Dan Gohmand8816272010-08-11 18:14:00 +000010465 SDValue StoreAddr = DAG.getNode(ISD::ADD, dl, getPointerTy(), Frame,
Micah Villmow2c39b152012-10-15 16:24:29 +000010466 DAG.getIntPtrConstant(TD->getPointerSize(0)));
Dale Johannesene4d209d2009-02-03 20:21:25 +000010467 StoreAddr = DAG.getNode(ISD::ADD, dl, getPointerTy(), StoreAddr, Offset);
Chris Lattner8026a9d2010-09-21 17:50:43 +000010468 Chain = DAG.getStore(Chain, dl, Handler, StoreAddr, MachinePointerInfo(),
10469 false, false, 0);
Dale Johannesendd64c412009-02-04 00:33:20 +000010470 Chain = DAG.getCopyToReg(Chain, dl, StoreAddrReg, StoreAddr);
Anton Korobeynikov2365f512007-07-14 14:06:15 +000010471
Dale Johannesene4d209d2009-02-03 20:21:25 +000010472 return DAG.getNode(X86ISD::EH_RETURN, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +000010473 MVT::Other,
Anton Korobeynikovb84c1672008-09-08 21:12:47 +000010474 Chain, DAG.getRegister(StoreAddrReg, getPointerTy()));
Anton Korobeynikov2365f512007-07-14 14:06:15 +000010475}
10476
Michael Liao6c0e04c2012-10-15 22:39:43 +000010477SDValue X86TargetLowering::lowerEH_SJLJ_SETJMP(SDValue Op,
10478 SelectionDAG &DAG) const {
10479 DebugLoc DL = Op.getDebugLoc();
10480 return DAG.getNode(X86ISD::EH_SJLJ_SETJMP, DL,
10481 DAG.getVTList(MVT::i32, MVT::Other),
10482 Op.getOperand(0), Op.getOperand(1));
10483}
10484
10485SDValue X86TargetLowering::lowerEH_SJLJ_LONGJMP(SDValue Op,
10486 SelectionDAG &DAG) const {
10487 DebugLoc DL = Op.getDebugLoc();
10488 return DAG.getNode(X86ISD::EH_SJLJ_LONGJMP, DL, MVT::Other,
10489 Op.getOperand(0), Op.getOperand(1));
10490}
10491
Craig Topper55b24052012-09-11 06:15:32 +000010492static SDValue LowerADJUST_TRAMPOLINE(SDValue Op, SelectionDAG &DAG) {
Duncan Sands4a544a72011-09-06 13:37:06 +000010493 return Op.getOperand(0);
10494}
10495
10496SDValue X86TargetLowering::LowerINIT_TRAMPOLINE(SDValue Op,
10497 SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +000010498 SDValue Root = Op.getOperand(0);
10499 SDValue Trmp = Op.getOperand(1); // trampoline
10500 SDValue FPtr = Op.getOperand(2); // nested function
10501 SDValue Nest = Op.getOperand(3); // 'nest' parameter value
Dale Johannesen6f38cb62009-02-07 19:59:05 +000010502 DebugLoc dl = Op.getDebugLoc();
Duncan Sandsb116fac2007-07-27 20:02:49 +000010503
Dan Gohman69de1932008-02-06 22:27:42 +000010504 const Value *TrmpAddr = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Michael Liao7abf67a2012-10-04 19:50:43 +000010505 const TargetRegisterInfo* TRI = getTargetMachine().getRegisterInfo();
Duncan Sandsb116fac2007-07-27 20:02:49 +000010506
10507 if (Subtarget->is64Bit()) {
Dan Gohman475871a2008-07-27 21:46:04 +000010508 SDValue OutChains[6];
Duncan Sands339e14f2008-01-16 22:55:25 +000010509
10510 // Large code-model.
Chris Lattnera62fe662010-02-05 19:20:30 +000010511 const unsigned char JMP64r = 0xFF; // 64-bit jmp through register opcode.
10512 const unsigned char MOV64ri = 0xB8; // X86::MOV64ri opcode.
Duncan Sands339e14f2008-01-16 22:55:25 +000010513
Michael Liao7abf67a2012-10-04 19:50:43 +000010514 const unsigned char N86R10 = TRI->getEncodingValue(X86::R10) & 0x7;
10515 const unsigned char N86R11 = TRI->getEncodingValue(X86::R11) & 0x7;
Duncan Sands339e14f2008-01-16 22:55:25 +000010516
10517 const unsigned char REX_WB = 0x40 | 0x08 | 0x01; // REX prefix
10518
10519 // Load the pointer to the nested function into R11.
10520 unsigned OpCode = ((MOV64ri | N86R11) << 8) | REX_WB; // movabsq r11
Dan Gohman475871a2008-07-27 21:46:04 +000010521 SDValue Addr = Trmp;
Owen Anderson825b72b2009-08-11 20:47:22 +000010522 OutChains[0] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Chris Lattner8026a9d2010-09-21 17:50:43 +000010523 Addr, MachinePointerInfo(TrmpAddr),
10524 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +000010525
Owen Anderson825b72b2009-08-11 20:47:22 +000010526 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
10527 DAG.getConstant(2, MVT::i64));
Chris Lattner8026a9d2010-09-21 17:50:43 +000010528 OutChains[1] = DAG.getStore(Root, dl, FPtr, Addr,
10529 MachinePointerInfo(TrmpAddr, 2),
David Greene67c9d422010-02-15 16:53:33 +000010530 false, false, 2);
Duncan Sands339e14f2008-01-16 22:55:25 +000010531
10532 // Load the 'nest' parameter value into R10.
10533 // R10 is specified in X86CallingConv.td
10534 OpCode = ((MOV64ri | N86R10) << 8) | REX_WB; // movabsq r10
Owen Anderson825b72b2009-08-11 20:47:22 +000010535 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
10536 DAG.getConstant(10, MVT::i64));
10537 OutChains[2] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Chris Lattner8026a9d2010-09-21 17:50:43 +000010538 Addr, MachinePointerInfo(TrmpAddr, 10),
10539 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +000010540
Owen Anderson825b72b2009-08-11 20:47:22 +000010541 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
10542 DAG.getConstant(12, MVT::i64));
Chris Lattner8026a9d2010-09-21 17:50:43 +000010543 OutChains[3] = DAG.getStore(Root, dl, Nest, Addr,
10544 MachinePointerInfo(TrmpAddr, 12),
David Greene67c9d422010-02-15 16:53:33 +000010545 false, false, 2);
Duncan Sands339e14f2008-01-16 22:55:25 +000010546
10547 // Jump to the nested function.
10548 OpCode = (JMP64r << 8) | REX_WB; // jmpq *...
Owen Anderson825b72b2009-08-11 20:47:22 +000010549 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
10550 DAG.getConstant(20, MVT::i64));
10551 OutChains[4] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Chris Lattner8026a9d2010-09-21 17:50:43 +000010552 Addr, MachinePointerInfo(TrmpAddr, 20),
10553 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +000010554
10555 unsigned char ModRM = N86R11 | (4 << 3) | (3 << 6); // ...r11
Owen Anderson825b72b2009-08-11 20:47:22 +000010556 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
10557 DAG.getConstant(22, MVT::i64));
10558 OutChains[5] = DAG.getStore(Root, dl, DAG.getConstant(ModRM, MVT::i8), Addr,
Chris Lattner8026a9d2010-09-21 17:50:43 +000010559 MachinePointerInfo(TrmpAddr, 22),
10560 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +000010561
Duncan Sands4a544a72011-09-06 13:37:06 +000010562 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains, 6);
Duncan Sandsb116fac2007-07-27 20:02:49 +000010563 } else {
Dan Gohmanbbfb9c52008-01-31 01:01:48 +000010564 const Function *Func =
Duncan Sandsb116fac2007-07-27 20:02:49 +000010565 cast<Function>(cast<SrcValueSDNode>(Op.getOperand(5))->getValue());
Sandeep Patel65c3c8f2009-09-02 08:44:58 +000010566 CallingConv::ID CC = Func->getCallingConv();
Duncan Sandsee465742007-08-29 19:01:20 +000010567 unsigned NestReg;
Duncan Sandsb116fac2007-07-27 20:02:49 +000010568
10569 switch (CC) {
10570 default:
Torok Edwinc23197a2009-07-14 16:55:14 +000010571 llvm_unreachable("Unsupported calling convention");
Duncan Sandsb116fac2007-07-27 20:02:49 +000010572 case CallingConv::C:
Duncan Sandsb116fac2007-07-27 20:02:49 +000010573 case CallingConv::X86_StdCall: {
10574 // Pass 'nest' parameter in ECX.
10575 // Must be kept in sync with X86CallingConv.td
Duncan Sandsee465742007-08-29 19:01:20 +000010576 NestReg = X86::ECX;
Duncan Sandsb116fac2007-07-27 20:02:49 +000010577
10578 // Check that ECX wasn't needed by an 'inreg' parameter.
Chris Lattnerdb125cf2011-07-18 04:54:35 +000010579 FunctionType *FTy = Func->getFunctionType();
Devang Patel05988662008-09-25 21:00:45 +000010580 const AttrListPtr &Attrs = Func->getAttributes();
Duncan Sandsb116fac2007-07-27 20:02:49 +000010581
Chris Lattner58d74912008-03-12 17:45:29 +000010582 if (!Attrs.isEmpty() && !Func->isVarArg()) {
Duncan Sandsb116fac2007-07-27 20:02:49 +000010583 unsigned InRegCount = 0;
10584 unsigned Idx = 1;
10585
10586 for (FunctionType::param_iterator I = FTy->param_begin(),
10587 E = FTy->param_end(); I != E; ++I, ++Idx)
Bill Wendling67658342012-10-09 07:45:08 +000010588 if (Attrs.getParamAttributes(Idx).hasAttribute(Attributes::InReg))
Duncan Sandsb116fac2007-07-27 20:02:49 +000010589 // FIXME: should only count parameters that are lowered to integers.
Anton Korobeynikovbff66b02008-09-09 18:22:57 +000010590 InRegCount += (TD->getTypeSizeInBits(*I) + 31) / 32;
Duncan Sandsb116fac2007-07-27 20:02:49 +000010591
10592 if (InRegCount > 2) {
Eric Christopher90eb4022010-07-22 00:26:08 +000010593 report_fatal_error("Nest register in use - reduce number of inreg"
10594 " parameters!");
Duncan Sandsb116fac2007-07-27 20:02:49 +000010595 }
10596 }
10597 break;
10598 }
10599 case CallingConv::X86_FastCall:
Anton Korobeynikovded05e32010-05-16 09:08:45 +000010600 case CallingConv::X86_ThisCall:
Duncan Sandsbf53c292008-09-10 13:22:10 +000010601 case CallingConv::Fast:
Duncan Sandsb116fac2007-07-27 20:02:49 +000010602 // Pass 'nest' parameter in EAX.
10603 // Must be kept in sync with X86CallingConv.td
Duncan Sandsee465742007-08-29 19:01:20 +000010604 NestReg = X86::EAX;
Duncan Sandsb116fac2007-07-27 20:02:49 +000010605 break;
10606 }
10607
Dan Gohman475871a2008-07-27 21:46:04 +000010608 SDValue OutChains[4];
10609 SDValue Addr, Disp;
Duncan Sandsb116fac2007-07-27 20:02:49 +000010610
Owen Anderson825b72b2009-08-11 20:47:22 +000010611 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
10612 DAG.getConstant(10, MVT::i32));
10613 Disp = DAG.getNode(ISD::SUB, dl, MVT::i32, FPtr, Addr);
Duncan Sandsb116fac2007-07-27 20:02:49 +000010614
Chris Lattnera62fe662010-02-05 19:20:30 +000010615 // This is storing the opcode for MOV32ri.
10616 const unsigned char MOV32ri = 0xB8; // X86::MOV32ri's opcode byte.
Michael Liao7abf67a2012-10-04 19:50:43 +000010617 const unsigned char N86Reg = TRI->getEncodingValue(NestReg) & 0x7;
Scott Michelfdc40a02009-02-17 22:15:04 +000010618 OutChains[0] = DAG.getStore(Root, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +000010619 DAG.getConstant(MOV32ri|N86Reg, MVT::i8),
Chris Lattner8026a9d2010-09-21 17:50:43 +000010620 Trmp, MachinePointerInfo(TrmpAddr),
10621 false, false, 0);
Duncan Sandsb116fac2007-07-27 20:02:49 +000010622
Owen Anderson825b72b2009-08-11 20:47:22 +000010623 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
10624 DAG.getConstant(1, MVT::i32));
Chris Lattner8026a9d2010-09-21 17:50:43 +000010625 OutChains[1] = DAG.getStore(Root, dl, Nest, Addr,
10626 MachinePointerInfo(TrmpAddr, 1),
David Greene67c9d422010-02-15 16:53:33 +000010627 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +000010628
Chris Lattnera62fe662010-02-05 19:20:30 +000010629 const unsigned char JMP = 0xE9; // jmp <32bit dst> opcode.
Owen Anderson825b72b2009-08-11 20:47:22 +000010630 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
10631 DAG.getConstant(5, MVT::i32));
10632 OutChains[2] = DAG.getStore(Root, dl, DAG.getConstant(JMP, MVT::i8), Addr,
Chris Lattner8026a9d2010-09-21 17:50:43 +000010633 MachinePointerInfo(TrmpAddr, 5),
10634 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +000010635
Owen Anderson825b72b2009-08-11 20:47:22 +000010636 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
10637 DAG.getConstant(6, MVT::i32));
Chris Lattner8026a9d2010-09-21 17:50:43 +000010638 OutChains[3] = DAG.getStore(Root, dl, Disp, Addr,
10639 MachinePointerInfo(TrmpAddr, 6),
David Greene67c9d422010-02-15 16:53:33 +000010640 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +000010641
Duncan Sands4a544a72011-09-06 13:37:06 +000010642 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains, 4);
Duncan Sandsb116fac2007-07-27 20:02:49 +000010643 }
10644}
10645
Dan Gohmand858e902010-04-17 15:26:15 +000010646SDValue X86TargetLowering::LowerFLT_ROUNDS_(SDValue Op,
10647 SelectionDAG &DAG) const {
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000010648 /*
10649 The rounding mode is in bits 11:10 of FPSR, and has the following
10650 settings:
10651 00 Round to nearest
10652 01 Round to -inf
10653 10 Round to +inf
10654 11 Round to 0
10655
10656 FLT_ROUNDS, on the other hand, expects the following:
10657 -1 Undefined
10658 0 Round to 0
10659 1 Round to nearest
10660 2 Round to +inf
10661 3 Round to -inf
10662
10663 To perform the conversion, we do:
10664 (((((FPSR & 0x800) >> 11) | ((FPSR & 0x400) >> 9)) + 1) & 3)
10665 */
10666
10667 MachineFunction &MF = DAG.getMachineFunction();
10668 const TargetMachine &TM = MF.getTarget();
Anton Korobeynikov16c29b52011-01-10 12:39:04 +000010669 const TargetFrameLowering &TFI = *TM.getFrameLowering();
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000010670 unsigned StackAlignment = TFI.getStackAlignment();
Owen Andersone50ed302009-08-10 22:56:29 +000010671 EVT VT = Op.getValueType();
Chris Lattner2156b792010-09-22 01:11:26 +000010672 DebugLoc DL = Op.getDebugLoc();
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000010673
10674 // Save FP Control Word to stack slot
David Greene3f2bf852009-11-12 20:49:22 +000010675 int SSFI = MF.getFrameInfo()->CreateStackObject(2, StackAlignment, false);
Dan Gohman475871a2008-07-27 21:46:04 +000010676 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000010677
Michael J. Spencerec38de22010-10-10 22:04:20 +000010678
Chris Lattner2156b792010-09-22 01:11:26 +000010679 MachineMemOperand *MMO =
10680 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
10681 MachineMemOperand::MOStore, 2, 2);
Michael J. Spencerec38de22010-10-10 22:04:20 +000010682
Chris Lattner2156b792010-09-22 01:11:26 +000010683 SDValue Ops[] = { DAG.getEntryNode(), StackSlot };
10684 SDValue Chain = DAG.getMemIntrinsicNode(X86ISD::FNSTCW16m, DL,
10685 DAG.getVTList(MVT::Other),
10686 Ops, 2, MVT::i16, MMO);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000010687
10688 // Load FP Control Word from stack slot
Chris Lattner2156b792010-09-22 01:11:26 +000010689 SDValue CWD = DAG.getLoad(MVT::i16, DL, Chain, StackSlot,
Pete Cooperd752e0f2011-11-08 18:42:53 +000010690 MachinePointerInfo(), false, false, false, 0);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000010691
10692 // Transform as necessary
Dan Gohman475871a2008-07-27 21:46:04 +000010693 SDValue CWD1 =
Chris Lattner2156b792010-09-22 01:11:26 +000010694 DAG.getNode(ISD::SRL, DL, MVT::i16,
10695 DAG.getNode(ISD::AND, DL, MVT::i16,
Owen Anderson825b72b2009-08-11 20:47:22 +000010696 CWD, DAG.getConstant(0x800, MVT::i16)),
10697 DAG.getConstant(11, MVT::i8));
Dan Gohman475871a2008-07-27 21:46:04 +000010698 SDValue CWD2 =
Chris Lattner2156b792010-09-22 01:11:26 +000010699 DAG.getNode(ISD::SRL, DL, MVT::i16,
10700 DAG.getNode(ISD::AND, DL, MVT::i16,
Owen Anderson825b72b2009-08-11 20:47:22 +000010701 CWD, DAG.getConstant(0x400, MVT::i16)),
10702 DAG.getConstant(9, MVT::i8));
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000010703
Dan Gohman475871a2008-07-27 21:46:04 +000010704 SDValue RetVal =
Chris Lattner2156b792010-09-22 01:11:26 +000010705 DAG.getNode(ISD::AND, DL, MVT::i16,
10706 DAG.getNode(ISD::ADD, DL, MVT::i16,
10707 DAG.getNode(ISD::OR, DL, MVT::i16, CWD1, CWD2),
Owen Anderson825b72b2009-08-11 20:47:22 +000010708 DAG.getConstant(1, MVT::i16)),
10709 DAG.getConstant(3, MVT::i16));
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000010710
10711
Duncan Sands83ec4b62008-06-06 12:08:01 +000010712 return DAG.getNode((VT.getSizeInBits() < 16 ?
Chris Lattner2156b792010-09-22 01:11:26 +000010713 ISD::TRUNCATE : ISD::ZERO_EXTEND), DL, VT, RetVal);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000010714}
10715
Craig Topper55b24052012-09-11 06:15:32 +000010716static SDValue LowerCTLZ(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +000010717 EVT VT = Op.getValueType();
10718 EVT OpVT = VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +000010719 unsigned NumBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +000010720 DebugLoc dl = Op.getDebugLoc();
Evan Cheng18efe262007-12-14 02:13:44 +000010721
10722 Op = Op.getOperand(0);
Owen Anderson825b72b2009-08-11 20:47:22 +000010723 if (VT == MVT::i8) {
Evan Cheng152804e2007-12-14 08:30:15 +000010724 // Zero extend to i32 since there is not an i8 bsr.
Owen Anderson825b72b2009-08-11 20:47:22 +000010725 OpVT = MVT::i32;
Dale Johannesene4d209d2009-02-03 20:21:25 +000010726 Op = DAG.getNode(ISD::ZERO_EXTEND, dl, OpVT, Op);
Evan Cheng18efe262007-12-14 02:13:44 +000010727 }
Evan Cheng18efe262007-12-14 02:13:44 +000010728
Evan Cheng152804e2007-12-14 08:30:15 +000010729 // Issue a bsr (scan bits in reverse) which also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +000010730 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +000010731 Op = DAG.getNode(X86ISD::BSR, dl, VTs, Op);
Evan Cheng152804e2007-12-14 08:30:15 +000010732
10733 // If src is zero (i.e. bsr sets ZF), returns NumBits.
Benjamin Kramer7f1a5602009-12-29 16:57:26 +000010734 SDValue Ops[] = {
10735 Op,
10736 DAG.getConstant(NumBits+NumBits-1, OpVT),
10737 DAG.getConstant(X86::COND_E, MVT::i8),
10738 Op.getValue(1)
10739 };
10740 Op = DAG.getNode(X86ISD::CMOV, dl, OpVT, Ops, array_lengthof(Ops));
Evan Cheng152804e2007-12-14 08:30:15 +000010741
10742 // Finally xor with NumBits-1.
Dale Johannesene4d209d2009-02-03 20:21:25 +000010743 Op = DAG.getNode(ISD::XOR, dl, OpVT, Op, DAG.getConstant(NumBits-1, OpVT));
Evan Cheng152804e2007-12-14 08:30:15 +000010744
Owen Anderson825b72b2009-08-11 20:47:22 +000010745 if (VT == MVT::i8)
10746 Op = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Op);
Evan Cheng18efe262007-12-14 02:13:44 +000010747 return Op;
10748}
10749
Craig Topper55b24052012-09-11 06:15:32 +000010750static SDValue LowerCTLZ_ZERO_UNDEF(SDValue Op, SelectionDAG &DAG) {
Chandler Carruthacc068e2011-12-24 10:55:54 +000010751 EVT VT = Op.getValueType();
10752 EVT OpVT = VT;
10753 unsigned NumBits = VT.getSizeInBits();
10754 DebugLoc dl = Op.getDebugLoc();
10755
10756 Op = Op.getOperand(0);
10757 if (VT == MVT::i8) {
10758 // Zero extend to i32 since there is not an i8 bsr.
10759 OpVT = MVT::i32;
10760 Op = DAG.getNode(ISD::ZERO_EXTEND, dl, OpVT, Op);
10761 }
10762
10763 // Issue a bsr (scan bits in reverse).
10764 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
10765 Op = DAG.getNode(X86ISD::BSR, dl, VTs, Op);
10766
10767 // And xor with NumBits-1.
10768 Op = DAG.getNode(ISD::XOR, dl, OpVT, Op, DAG.getConstant(NumBits-1, OpVT));
10769
10770 if (VT == MVT::i8)
10771 Op = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Op);
10772 return Op;
10773}
10774
Craig Topper55b24052012-09-11 06:15:32 +000010775static SDValue LowerCTTZ(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +000010776 EVT VT = Op.getValueType();
Duncan Sands83ec4b62008-06-06 12:08:01 +000010777 unsigned NumBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +000010778 DebugLoc dl = Op.getDebugLoc();
Evan Cheng18efe262007-12-14 02:13:44 +000010779 Op = Op.getOperand(0);
Evan Cheng152804e2007-12-14 08:30:15 +000010780
10781 // Issue a bsf (scan bits forward) which also sets EFLAGS.
Chandler Carruth77821022011-12-24 12:12:34 +000010782 SDVTList VTs = DAG.getVTList(VT, MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +000010783 Op = DAG.getNode(X86ISD::BSF, dl, VTs, Op);
Evan Cheng152804e2007-12-14 08:30:15 +000010784
10785 // If src is zero (i.e. bsf sets ZF), returns NumBits.
Benjamin Kramer7f1a5602009-12-29 16:57:26 +000010786 SDValue Ops[] = {
10787 Op,
Chandler Carruth77821022011-12-24 12:12:34 +000010788 DAG.getConstant(NumBits, VT),
Benjamin Kramer7f1a5602009-12-29 16:57:26 +000010789 DAG.getConstant(X86::COND_E, MVT::i8),
10790 Op.getValue(1)
10791 };
Chandler Carruth77821022011-12-24 12:12:34 +000010792 return DAG.getNode(X86ISD::CMOV, dl, VT, Ops, array_lengthof(Ops));
Evan Cheng18efe262007-12-14 02:13:44 +000010793}
10794
Craig Topper13894fa2011-08-24 06:14:18 +000010795// Lower256IntArith - Break a 256-bit integer operation into two new 128-bit
10796// ones, and then concatenate the result back.
10797static SDValue Lower256IntArith(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +000010798 EVT VT = Op.getValueType();
Craig Topper13894fa2011-08-24 06:14:18 +000010799
Craig Topper7a9a28b2012-08-12 02:23:29 +000010800 assert(VT.is256BitVector() && VT.isInteger() &&
Craig Topper13894fa2011-08-24 06:14:18 +000010801 "Unsupported value type for operation");
10802
Craig Topper66ddd152012-04-27 22:54:43 +000010803 unsigned NumElems = VT.getVectorNumElements();
Craig Topper13894fa2011-08-24 06:14:18 +000010804 DebugLoc dl = Op.getDebugLoc();
Craig Topper13894fa2011-08-24 06:14:18 +000010805
10806 // Extract the LHS vectors
10807 SDValue LHS = Op.getOperand(0);
Craig Topperb14940a2012-04-22 20:55:18 +000010808 SDValue LHS1 = Extract128BitVector(LHS, 0, DAG, dl);
10809 SDValue LHS2 = Extract128BitVector(LHS, NumElems/2, DAG, dl);
Craig Topper13894fa2011-08-24 06:14:18 +000010810
10811 // Extract the RHS vectors
10812 SDValue RHS = Op.getOperand(1);
Craig Topperb14940a2012-04-22 20:55:18 +000010813 SDValue RHS1 = Extract128BitVector(RHS, 0, DAG, dl);
10814 SDValue RHS2 = Extract128BitVector(RHS, NumElems/2, DAG, dl);
Craig Topper13894fa2011-08-24 06:14:18 +000010815
10816 MVT EltVT = VT.getVectorElementType().getSimpleVT();
10817 EVT NewVT = MVT::getVectorVT(EltVT, NumElems/2);
10818
10819 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT,
10820 DAG.getNode(Op.getOpcode(), dl, NewVT, LHS1, RHS1),
10821 DAG.getNode(Op.getOpcode(), dl, NewVT, LHS2, RHS2));
10822}
10823
Craig Topper55b24052012-09-11 06:15:32 +000010824static SDValue LowerADD(SDValue Op, SelectionDAG &DAG) {
Craig Topper7a9a28b2012-08-12 02:23:29 +000010825 assert(Op.getValueType().is256BitVector() &&
Craig Topper13894fa2011-08-24 06:14:18 +000010826 Op.getValueType().isInteger() &&
10827 "Only handle AVX 256-bit vector integer operation");
10828 return Lower256IntArith(Op, DAG);
10829}
10830
Craig Topper55b24052012-09-11 06:15:32 +000010831static SDValue LowerSUB(SDValue Op, SelectionDAG &DAG) {
Craig Topper7a9a28b2012-08-12 02:23:29 +000010832 assert(Op.getValueType().is256BitVector() &&
Craig Topper13894fa2011-08-24 06:14:18 +000010833 Op.getValueType().isInteger() &&
10834 "Only handle AVX 256-bit vector integer operation");
10835 return Lower256IntArith(Op, DAG);
10836}
10837
Craig Topper55b24052012-09-11 06:15:32 +000010838static SDValue LowerMUL(SDValue Op, const X86Subtarget *Subtarget,
10839 SelectionDAG &DAG) {
Craig Topper13894fa2011-08-24 06:14:18 +000010840 EVT VT = Op.getValueType();
10841
10842 // Decompose 256-bit ops into smaller 128-bit ops.
Craig Topper7a9a28b2012-08-12 02:23:29 +000010843 if (VT.is256BitVector() && !Subtarget->hasAVX2())
Craig Topper13894fa2011-08-24 06:14:18 +000010844 return Lower256IntArith(Op, DAG);
10845
Craig Topper5b209e82012-02-05 03:14:49 +000010846 assert((VT == MVT::v2i64 || VT == MVT::v4i64) &&
10847 "Only know how to lower V2I64/V4I64 multiply");
10848
Dale Johannesen6f38cb62009-02-07 19:59:05 +000010849 DebugLoc dl = Op.getDebugLoc();
Scott Michelfdc40a02009-02-17 22:15:04 +000010850
Craig Topper5b209e82012-02-05 03:14:49 +000010851 // Ahi = psrlqi(a, 32);
10852 // Bhi = psrlqi(b, 32);
10853 //
10854 // AloBlo = pmuludq(a, b);
10855 // AloBhi = pmuludq(a, Bhi);
10856 // AhiBlo = pmuludq(Ahi, b);
10857
10858 // AloBhi = psllqi(AloBhi, 32);
10859 // AhiBlo = psllqi(AhiBlo, 32);
10860 // return AloBlo + AloBhi + AhiBlo;
10861
Craig Topperaaa643c2011-11-09 07:28:55 +000010862 SDValue A = Op.getOperand(0);
10863 SDValue B = Op.getOperand(1);
10864
Craig Topper5b209e82012-02-05 03:14:49 +000010865 SDValue ShAmt = DAG.getConstant(32, MVT::i32);
Craig Topperaaa643c2011-11-09 07:28:55 +000010866
Craig Topper5b209e82012-02-05 03:14:49 +000010867 SDValue Ahi = DAG.getNode(X86ISD::VSRLI, dl, VT, A, ShAmt);
10868 SDValue Bhi = DAG.getNode(X86ISD::VSRLI, dl, VT, B, ShAmt);
Craig Topperaaa643c2011-11-09 07:28:55 +000010869
Craig Topper5b209e82012-02-05 03:14:49 +000010870 // Bit cast to 32-bit vectors for MULUDQ
10871 EVT MulVT = (VT == MVT::v2i64) ? MVT::v4i32 : MVT::v8i32;
10872 A = DAG.getNode(ISD::BITCAST, dl, MulVT, A);
10873 B = DAG.getNode(ISD::BITCAST, dl, MulVT, B);
10874 Ahi = DAG.getNode(ISD::BITCAST, dl, MulVT, Ahi);
10875 Bhi = DAG.getNode(ISD::BITCAST, dl, MulVT, Bhi);
Craig Topperaaa643c2011-11-09 07:28:55 +000010876
Craig Topper5b209e82012-02-05 03:14:49 +000010877 SDValue AloBlo = DAG.getNode(X86ISD::PMULUDQ, dl, VT, A, B);
10878 SDValue AloBhi = DAG.getNode(X86ISD::PMULUDQ, dl, VT, A, Bhi);
10879 SDValue AhiBlo = DAG.getNode(X86ISD::PMULUDQ, dl, VT, Ahi, B);
Craig Topperaaa643c2011-11-09 07:28:55 +000010880
Craig Topper5b209e82012-02-05 03:14:49 +000010881 AloBhi = DAG.getNode(X86ISD::VSHLI, dl, VT, AloBhi, ShAmt);
10882 AhiBlo = DAG.getNode(X86ISD::VSHLI, dl, VT, AhiBlo, ShAmt);
Mon P Wangaf9b9522008-12-18 21:42:19 +000010883
Dale Johannesene4d209d2009-02-03 20:21:25 +000010884 SDValue Res = DAG.getNode(ISD::ADD, dl, VT, AloBlo, AloBhi);
Craig Topper5b209e82012-02-05 03:14:49 +000010885 return DAG.getNode(ISD::ADD, dl, VT, Res, AhiBlo);
Mon P Wangaf9b9522008-12-18 21:42:19 +000010886}
10887
Nadav Rotem43012222011-05-11 08:12:09 +000010888SDValue X86TargetLowering::LowerShift(SDValue Op, SelectionDAG &DAG) const {
10889
Nate Begemanbdcb5af2010-07-27 22:37:06 +000010890 EVT VT = Op.getValueType();
10891 DebugLoc dl = Op.getDebugLoc();
10892 SDValue R = Op.getOperand(0);
Nadav Rotem43012222011-05-11 08:12:09 +000010893 SDValue Amt = Op.getOperand(1);
Nate Begemanbdcb5af2010-07-27 22:37:06 +000010894 LLVMContext *Context = DAG.getContext();
Nate Begemanbdcb5af2010-07-27 22:37:06 +000010895
Craig Topper1accb7e2012-01-10 06:54:16 +000010896 if (!Subtarget->hasSSE2())
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +000010897 return SDValue();
10898
Nadav Rotem43012222011-05-11 08:12:09 +000010899 // Optimize shl/srl/sra with constant shift amount.
10900 if (isSplatVector(Amt.getNode())) {
10901 SDValue SclrAmt = Amt->getOperand(0);
10902 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(SclrAmt)) {
10903 uint64_t ShiftAmt = C->getZExtValue();
10904
Craig Toppered2e13d2012-01-22 19:15:14 +000010905 if (VT == MVT::v2i64 || VT == MVT::v4i32 || VT == MVT::v8i16 ||
10906 (Subtarget->hasAVX2() &&
10907 (VT == MVT::v4i64 || VT == MVT::v8i32 || VT == MVT::v16i16))) {
10908 if (Op.getOpcode() == ISD::SHL)
10909 return DAG.getNode(X86ISD::VSHLI, dl, VT, R,
10910 DAG.getConstant(ShiftAmt, MVT::i32));
10911 if (Op.getOpcode() == ISD::SRL)
10912 return DAG.getNode(X86ISD::VSRLI, dl, VT, R,
10913 DAG.getConstant(ShiftAmt, MVT::i32));
10914 if (Op.getOpcode() == ISD::SRA && VT != MVT::v2i64 && VT != MVT::v4i64)
10915 return DAG.getNode(X86ISD::VSRAI, dl, VT, R,
10916 DAG.getConstant(ShiftAmt, MVT::i32));
Benjamin Kramerdade3c12011-10-30 17:31:21 +000010917 }
10918
Craig Toppered2e13d2012-01-22 19:15:14 +000010919 if (VT == MVT::v16i8) {
10920 if (Op.getOpcode() == ISD::SHL) {
10921 // Make a large shift.
10922 SDValue SHL = DAG.getNode(X86ISD::VSHLI, dl, MVT::v8i16, R,
10923 DAG.getConstant(ShiftAmt, MVT::i32));
10924 SHL = DAG.getNode(ISD::BITCAST, dl, VT, SHL);
10925 // Zero out the rightmost bits.
10926 SmallVector<SDValue, 16> V(16,
10927 DAG.getConstant(uint8_t(-1U << ShiftAmt),
10928 MVT::i8));
10929 return DAG.getNode(ISD::AND, dl, VT, SHL,
10930 DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &V[0], 16));
Eli Friedmanf6aa6b12011-11-01 21:18:39 +000010931 }
Craig Toppered2e13d2012-01-22 19:15:14 +000010932 if (Op.getOpcode() == ISD::SRL) {
10933 // Make a large shift.
10934 SDValue SRL = DAG.getNode(X86ISD::VSRLI, dl, MVT::v8i16, R,
10935 DAG.getConstant(ShiftAmt, MVT::i32));
10936 SRL = DAG.getNode(ISD::BITCAST, dl, VT, SRL);
10937 // Zero out the leftmost bits.
10938 SmallVector<SDValue, 16> V(16,
10939 DAG.getConstant(uint8_t(-1U) >> ShiftAmt,
10940 MVT::i8));
10941 return DAG.getNode(ISD::AND, dl, VT, SRL,
10942 DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &V[0], 16));
10943 }
10944 if (Op.getOpcode() == ISD::SRA) {
10945 if (ShiftAmt == 7) {
10946 // R s>> 7 === R s< 0
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +000010947 SDValue Zeros = getZeroVector(VT, Subtarget, DAG, dl);
Craig Topper67609fd2012-01-22 22:42:16 +000010948 return DAG.getNode(X86ISD::PCMPGT, dl, VT, Zeros, R);
Craig Toppered2e13d2012-01-22 19:15:14 +000010949 }
Eli Friedmanf6aa6b12011-11-01 21:18:39 +000010950
Craig Toppered2e13d2012-01-22 19:15:14 +000010951 // R s>> a === ((R u>> a) ^ m) - m
10952 SDValue Res = DAG.getNode(ISD::SRL, dl, VT, R, Amt);
10953 SmallVector<SDValue, 16> V(16, DAG.getConstant(128 >> ShiftAmt,
10954 MVT::i8));
10955 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &V[0], 16);
10956 Res = DAG.getNode(ISD::XOR, dl, VT, Res, Mask);
10957 Res = DAG.getNode(ISD::SUB, dl, VT, Res, Mask);
10958 return Res;
10959 }
Craig Topper731dfd02012-04-23 03:42:40 +000010960 llvm_unreachable("Unknown shift opcode.");
Eli Friedmanf6aa6b12011-11-01 21:18:39 +000010961 }
Craig Topper46154eb2011-11-11 07:39:23 +000010962
Craig Topper0d86d462011-11-20 00:12:05 +000010963 if (Subtarget->hasAVX2() && VT == MVT::v32i8) {
10964 if (Op.getOpcode() == ISD::SHL) {
10965 // Make a large shift.
Craig Toppered2e13d2012-01-22 19:15:14 +000010966 SDValue SHL = DAG.getNode(X86ISD::VSHLI, dl, MVT::v16i16, R,
10967 DAG.getConstant(ShiftAmt, MVT::i32));
10968 SHL = DAG.getNode(ISD::BITCAST, dl, VT, SHL);
Craig Topper0d86d462011-11-20 00:12:05 +000010969 // Zero out the rightmost bits.
Craig Toppered2e13d2012-01-22 19:15:14 +000010970 SmallVector<SDValue, 32> V(32,
10971 DAG.getConstant(uint8_t(-1U << ShiftAmt),
10972 MVT::i8));
Craig Topper0d86d462011-11-20 00:12:05 +000010973 return DAG.getNode(ISD::AND, dl, VT, SHL,
10974 DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &V[0], 32));
Craig Topper46154eb2011-11-11 07:39:23 +000010975 }
Craig Topper0d86d462011-11-20 00:12:05 +000010976 if (Op.getOpcode() == ISD::SRL) {
10977 // Make a large shift.
Craig Toppered2e13d2012-01-22 19:15:14 +000010978 SDValue SRL = DAG.getNode(X86ISD::VSRLI, dl, MVT::v16i16, R,
10979 DAG.getConstant(ShiftAmt, MVT::i32));
10980 SRL = DAG.getNode(ISD::BITCAST, dl, VT, SRL);
Craig Topper0d86d462011-11-20 00:12:05 +000010981 // Zero out the leftmost bits.
Craig Toppered2e13d2012-01-22 19:15:14 +000010982 SmallVector<SDValue, 32> V(32,
10983 DAG.getConstant(uint8_t(-1U) >> ShiftAmt,
10984 MVT::i8));
Craig Topper0d86d462011-11-20 00:12:05 +000010985 return DAG.getNode(ISD::AND, dl, VT, SRL,
10986 DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &V[0], 32));
10987 }
10988 if (Op.getOpcode() == ISD::SRA) {
10989 if (ShiftAmt == 7) {
10990 // R s>> 7 === R s< 0
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +000010991 SDValue Zeros = getZeroVector(VT, Subtarget, DAG, dl);
Craig Topper67609fd2012-01-22 22:42:16 +000010992 return DAG.getNode(X86ISD::PCMPGT, dl, VT, Zeros, R);
Craig Topper0d86d462011-11-20 00:12:05 +000010993 }
10994
10995 // R s>> a === ((R u>> a) ^ m) - m
10996 SDValue Res = DAG.getNode(ISD::SRL, dl, VT, R, Amt);
10997 SmallVector<SDValue, 32> V(32, DAG.getConstant(128 >> ShiftAmt,
10998 MVT::i8));
10999 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &V[0], 32);
11000 Res = DAG.getNode(ISD::XOR, dl, VT, Res, Mask);
11001 Res = DAG.getNode(ISD::SUB, dl, VT, Res, Mask);
11002 return Res;
11003 }
Craig Topper731dfd02012-04-23 03:42:40 +000011004 llvm_unreachable("Unknown shift opcode.");
Craig Topper0d86d462011-11-20 00:12:05 +000011005 }
Nadav Rotem43012222011-05-11 08:12:09 +000011006 }
11007 }
11008
11009 // Lower SHL with variable shift amount.
Nadav Rotem43012222011-05-11 08:12:09 +000011010 if (VT == MVT::v4i32 && Op->getOpcode() == ISD::SHL) {
Craig Toppered2e13d2012-01-22 19:15:14 +000011011 Op = DAG.getNode(X86ISD::VSHLI, dl, VT, Op.getOperand(1),
11012 DAG.getConstant(23, MVT::i32));
Nate Begeman51409212010-07-28 00:21:48 +000011013
Chris Lattner7302d802012-02-06 21:56:39 +000011014 const uint32_t CV[] = { 0x3f800000U, 0x3f800000U, 0x3f800000U, 0x3f800000U};
11015 Constant *C = ConstantDataVector::get(*Context, CV);
Nate Begeman51409212010-07-28 00:21:48 +000011016 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
11017 SDValue Addend = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +000011018 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000011019 false, false, false, 16);
Nate Begeman51409212010-07-28 00:21:48 +000011020
11021 Op = DAG.getNode(ISD::ADD, dl, VT, Op, Addend);
Wesley Peckbf17cfa2010-11-23 03:31:01 +000011022 Op = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, Op);
Nate Begeman51409212010-07-28 00:21:48 +000011023 Op = DAG.getNode(ISD::FP_TO_SINT, dl, VT, Op);
11024 return DAG.getNode(ISD::MUL, dl, VT, Op, R);
11025 }
Nadav Rotem43012222011-05-11 08:12:09 +000011026 if (VT == MVT::v16i8 && Op->getOpcode() == ISD::SHL) {
Craig Topper8b5a6b62012-01-17 08:23:44 +000011027 assert(Subtarget->hasSSE2() && "Need SSE2 for pslli/pcmpeq.");
Lang Hames8b99c1e2011-12-17 01:08:46 +000011028
Nate Begeman51409212010-07-28 00:21:48 +000011029 // a = a << 5;
Craig Toppered2e13d2012-01-22 19:15:14 +000011030 Op = DAG.getNode(X86ISD::VSHLI, dl, MVT::v8i16, Op.getOperand(1),
11031 DAG.getConstant(5, MVT::i32));
11032 Op = DAG.getNode(ISD::BITCAST, dl, VT, Op);
Nate Begeman51409212010-07-28 00:21:48 +000011033
Lang Hames8b99c1e2011-12-17 01:08:46 +000011034 // Turn 'a' into a mask suitable for VSELECT
11035 SDValue VSelM = DAG.getConstant(0x80, VT);
11036 SDValue OpVSel = DAG.getNode(ISD::AND, dl, VT, VSelM, Op);
Craig Topper67609fd2012-01-22 22:42:16 +000011037 OpVSel = DAG.getNode(X86ISD::PCMPEQ, dl, VT, OpVSel, VSelM);
Nate Begeman51409212010-07-28 00:21:48 +000011038
Lang Hames8b99c1e2011-12-17 01:08:46 +000011039 SDValue CM1 = DAG.getConstant(0x0f, VT);
11040 SDValue CM2 = DAG.getConstant(0x3f, VT);
Nate Begeman51409212010-07-28 00:21:48 +000011041
Lang Hames8b99c1e2011-12-17 01:08:46 +000011042 // r = VSELECT(r, psllw(r & (char16)15, 4), a);
11043 SDValue M = DAG.getNode(ISD::AND, dl, VT, R, CM1);
Craig Toppered2e13d2012-01-22 19:15:14 +000011044 M = getTargetVShiftNode(X86ISD::VSHLI, dl, MVT::v8i16, M,
11045 DAG.getConstant(4, MVT::i32), DAG);
11046 M = DAG.getNode(ISD::BITCAST, dl, VT, M);
Lang Hames8b99c1e2011-12-17 01:08:46 +000011047 R = DAG.getNode(ISD::VSELECT, dl, VT, OpVSel, M, R);
11048
Nate Begeman51409212010-07-28 00:21:48 +000011049 // a += a
11050 Op = DAG.getNode(ISD::ADD, dl, VT, Op, Op);
Lang Hames8b99c1e2011-12-17 01:08:46 +000011051 OpVSel = DAG.getNode(ISD::AND, dl, VT, VSelM, Op);
Craig Topper67609fd2012-01-22 22:42:16 +000011052 OpVSel = DAG.getNode(X86ISD::PCMPEQ, dl, VT, OpVSel, VSelM);
Michael J. Spencerec38de22010-10-10 22:04:20 +000011053
Lang Hames8b99c1e2011-12-17 01:08:46 +000011054 // r = VSELECT(r, psllw(r & (char16)63, 2), a);
11055 M = DAG.getNode(ISD::AND, dl, VT, R, CM2);
Craig Toppered2e13d2012-01-22 19:15:14 +000011056 M = getTargetVShiftNode(X86ISD::VSHLI, dl, MVT::v8i16, M,
11057 DAG.getConstant(2, MVT::i32), DAG);
11058 M = DAG.getNode(ISD::BITCAST, dl, VT, M);
Lang Hames8b99c1e2011-12-17 01:08:46 +000011059 R = DAG.getNode(ISD::VSELECT, dl, VT, OpVSel, M, R);
11060
Nate Begeman51409212010-07-28 00:21:48 +000011061 // a += a
11062 Op = DAG.getNode(ISD::ADD, dl, VT, Op, Op);
Lang Hames8b99c1e2011-12-17 01:08:46 +000011063 OpVSel = DAG.getNode(ISD::AND, dl, VT, VSelM, Op);
Craig Topper67609fd2012-01-22 22:42:16 +000011064 OpVSel = DAG.getNode(X86ISD::PCMPEQ, dl, VT, OpVSel, VSelM);
Michael J. Spencerec38de22010-10-10 22:04:20 +000011065
Lang Hames8b99c1e2011-12-17 01:08:46 +000011066 // return VSELECT(r, r+r, a);
11067 R = DAG.getNode(ISD::VSELECT, dl, VT, OpVSel,
Lang Hamesa0a25132011-12-15 18:57:27 +000011068 DAG.getNode(ISD::ADD, dl, VT, R, R), R);
Nate Begeman51409212010-07-28 00:21:48 +000011069 return R;
11070 }
Craig Topper46154eb2011-11-11 07:39:23 +000011071
11072 // Decompose 256-bit shifts into smaller 128-bit shifts.
Craig Topper7a9a28b2012-08-12 02:23:29 +000011073 if (VT.is256BitVector()) {
Craig Toppered2e13d2012-01-22 19:15:14 +000011074 unsigned NumElems = VT.getVectorNumElements();
Craig Topper46154eb2011-11-11 07:39:23 +000011075 MVT EltVT = VT.getVectorElementType().getSimpleVT();
11076 EVT NewVT = MVT::getVectorVT(EltVT, NumElems/2);
11077
11078 // Extract the two vectors
Craig Topperb14940a2012-04-22 20:55:18 +000011079 SDValue V1 = Extract128BitVector(R, 0, DAG, dl);
11080 SDValue V2 = Extract128BitVector(R, NumElems/2, DAG, dl);
Craig Topper46154eb2011-11-11 07:39:23 +000011081
11082 // Recreate the shift amount vectors
11083 SDValue Amt1, Amt2;
11084 if (Amt.getOpcode() == ISD::BUILD_VECTOR) {
11085 // Constant shift amount
11086 SmallVector<SDValue, 4> Amt1Csts;
11087 SmallVector<SDValue, 4> Amt2Csts;
Craig Toppered2e13d2012-01-22 19:15:14 +000011088 for (unsigned i = 0; i != NumElems/2; ++i)
Craig Topper46154eb2011-11-11 07:39:23 +000011089 Amt1Csts.push_back(Amt->getOperand(i));
Craig Toppered2e13d2012-01-22 19:15:14 +000011090 for (unsigned i = NumElems/2; i != NumElems; ++i)
Craig Topper46154eb2011-11-11 07:39:23 +000011091 Amt2Csts.push_back(Amt->getOperand(i));
11092
11093 Amt1 = DAG.getNode(ISD::BUILD_VECTOR, dl, NewVT,
11094 &Amt1Csts[0], NumElems/2);
11095 Amt2 = DAG.getNode(ISD::BUILD_VECTOR, dl, NewVT,
11096 &Amt2Csts[0], NumElems/2);
11097 } else {
11098 // Variable shift amount
Craig Topperb14940a2012-04-22 20:55:18 +000011099 Amt1 = Extract128BitVector(Amt, 0, DAG, dl);
11100 Amt2 = Extract128BitVector(Amt, NumElems/2, DAG, dl);
Craig Topper46154eb2011-11-11 07:39:23 +000011101 }
11102
11103 // Issue new vector shifts for the smaller types
11104 V1 = DAG.getNode(Op.getOpcode(), dl, NewVT, V1, Amt1);
11105 V2 = DAG.getNode(Op.getOpcode(), dl, NewVT, V2, Amt2);
11106
11107 // Concatenate the result back
11108 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT, V1, V2);
11109 }
11110
Nate Begeman51409212010-07-28 00:21:48 +000011111 return SDValue();
Nate Begemanbdcb5af2010-07-27 22:37:06 +000011112}
Mon P Wangaf9b9522008-12-18 21:42:19 +000011113
Craig Topper55b24052012-09-11 06:15:32 +000011114static SDValue LowerXALUO(SDValue Op, SelectionDAG &DAG) {
Bill Wendling74c37652008-12-09 22:08:41 +000011115 // Lower the "add/sub/mul with overflow" instruction into a regular ins plus
11116 // a "setcc" instruction that checks the overflow flag. The "brcond" lowering
Bill Wendling61edeb52008-12-02 01:06:39 +000011117 // looks for this combo and may remove the "setcc" instruction if the "setcc"
11118 // has only one use.
Bill Wendling3fafd932008-11-26 22:37:40 +000011119 SDNode *N = Op.getNode();
Bill Wendling61edeb52008-12-02 01:06:39 +000011120 SDValue LHS = N->getOperand(0);
11121 SDValue RHS = N->getOperand(1);
Bill Wendling74c37652008-12-09 22:08:41 +000011122 unsigned BaseOp = 0;
11123 unsigned Cond = 0;
Chris Lattnerb20e0b12010-12-05 07:30:36 +000011124 DebugLoc DL = Op.getDebugLoc();
Bill Wendling74c37652008-12-09 22:08:41 +000011125 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +000011126 default: llvm_unreachable("Unknown ovf instruction!");
Bill Wendling74c37652008-12-09 22:08:41 +000011127 case ISD::SADDO:
Dan Gohman076aee32009-03-04 19:44:21 +000011128 // A subtract of one will be selected as a INC. Note that INC doesn't
11129 // set CF, so we can't do this for UADDO.
Benjamin Kramerc175a4b2011-03-08 15:20:20 +000011130 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(RHS))
11131 if (C->isOne()) {
Dan Gohman076aee32009-03-04 19:44:21 +000011132 BaseOp = X86ISD::INC;
11133 Cond = X86::COND_O;
11134 break;
11135 }
Bill Wendlingab55ebd2008-12-12 00:56:36 +000011136 BaseOp = X86ISD::ADD;
Bill Wendling74c37652008-12-09 22:08:41 +000011137 Cond = X86::COND_O;
11138 break;
11139 case ISD::UADDO:
Bill Wendlingab55ebd2008-12-12 00:56:36 +000011140 BaseOp = X86ISD::ADD;
Dan Gohman653456c2009-01-07 00:15:08 +000011141 Cond = X86::COND_B;
Bill Wendling74c37652008-12-09 22:08:41 +000011142 break;
11143 case ISD::SSUBO:
Dan Gohman076aee32009-03-04 19:44:21 +000011144 // A subtract of one will be selected as a DEC. Note that DEC doesn't
11145 // set CF, so we can't do this for USUBO.
Benjamin Kramerc175a4b2011-03-08 15:20:20 +000011146 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(RHS))
11147 if (C->isOne()) {
Dan Gohman076aee32009-03-04 19:44:21 +000011148 BaseOp = X86ISD::DEC;
11149 Cond = X86::COND_O;
11150 break;
11151 }
Bill Wendlingab55ebd2008-12-12 00:56:36 +000011152 BaseOp = X86ISD::SUB;
Bill Wendling74c37652008-12-09 22:08:41 +000011153 Cond = X86::COND_O;
11154 break;
11155 case ISD::USUBO:
Bill Wendlingab55ebd2008-12-12 00:56:36 +000011156 BaseOp = X86ISD::SUB;
Dan Gohman653456c2009-01-07 00:15:08 +000011157 Cond = X86::COND_B;
Bill Wendling74c37652008-12-09 22:08:41 +000011158 break;
11159 case ISD::SMULO:
Bill Wendlingd350e022008-12-12 21:15:41 +000011160 BaseOp = X86ISD::SMUL;
Bill Wendling74c37652008-12-09 22:08:41 +000011161 Cond = X86::COND_O;
11162 break;
Chris Lattnerb20e0b12010-12-05 07:30:36 +000011163 case ISD::UMULO: { // i64, i8 = umulo lhs, rhs --> i64, i64, i32 umul lhs,rhs
11164 SDVTList VTs = DAG.getVTList(N->getValueType(0), N->getValueType(0),
11165 MVT::i32);
11166 SDValue Sum = DAG.getNode(X86ISD::UMUL, DL, VTs, LHS, RHS);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011167
Chris Lattnerb20e0b12010-12-05 07:30:36 +000011168 SDValue SetCC =
11169 DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
11170 DAG.getConstant(X86::COND_O, MVT::i32),
11171 SDValue(Sum.getNode(), 2));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011172
Dan Gohman6e5fda22011-07-22 18:45:15 +000011173 return DAG.getNode(ISD::MERGE_VALUES, DL, N->getVTList(), Sum, SetCC);
Chris Lattnerb20e0b12010-12-05 07:30:36 +000011174 }
Bill Wendling74c37652008-12-09 22:08:41 +000011175 }
Bill Wendling3fafd932008-11-26 22:37:40 +000011176
Bill Wendling61edeb52008-12-02 01:06:39 +000011177 // Also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +000011178 SDVTList VTs = DAG.getVTList(N->getValueType(0), MVT::i32);
Chris Lattnerb20e0b12010-12-05 07:30:36 +000011179 SDValue Sum = DAG.getNode(BaseOp, DL, VTs, LHS, RHS);
Bill Wendling3fafd932008-11-26 22:37:40 +000011180
Bill Wendling61edeb52008-12-02 01:06:39 +000011181 SDValue SetCC =
Chris Lattnerb20e0b12010-12-05 07:30:36 +000011182 DAG.getNode(X86ISD::SETCC, DL, N->getValueType(1),
11183 DAG.getConstant(Cond, MVT::i32),
11184 SDValue(Sum.getNode(), 1));
Bill Wendling3fafd932008-11-26 22:37:40 +000011185
Dan Gohman6e5fda22011-07-22 18:45:15 +000011186 return DAG.getNode(ISD::MERGE_VALUES, DL, N->getVTList(), Sum, SetCC);
Bill Wendling41ea7e72008-11-24 19:21:46 +000011187}
11188
Chad Rosier30450e82011-12-22 22:35:21 +000011189SDValue X86TargetLowering::LowerSIGN_EXTEND_INREG(SDValue Op,
11190 SelectionDAG &DAG) const {
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000011191 DebugLoc dl = Op.getDebugLoc();
Craig Toppera124f942011-11-21 01:12:36 +000011192 EVT ExtraVT = cast<VTSDNode>(Op.getOperand(1))->getVT();
11193 EVT VT = Op.getValueType();
11194
Craig Toppered2e13d2012-01-22 19:15:14 +000011195 if (!Subtarget->hasSSE2() || !VT.isVector())
11196 return SDValue();
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000011197
Craig Toppered2e13d2012-01-22 19:15:14 +000011198 unsigned BitsDiff = VT.getScalarType().getSizeInBits() -
11199 ExtraVT.getScalarType().getSizeInBits();
11200 SDValue ShAmt = DAG.getConstant(BitsDiff, MVT::i32);
11201
11202 switch (VT.getSimpleVT().SimpleTy) {
11203 default: return SDValue();
11204 case MVT::v8i32:
11205 case MVT::v16i16:
11206 if (!Subtarget->hasAVX())
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000011207 return SDValue();
Craig Toppered2e13d2012-01-22 19:15:14 +000011208 if (!Subtarget->hasAVX2()) {
11209 // needs to be split
Craig Topper66ddd152012-04-27 22:54:43 +000011210 unsigned NumElems = VT.getVectorNumElements();
Craig Toppera124f942011-11-21 01:12:36 +000011211
Craig Toppered2e13d2012-01-22 19:15:14 +000011212 // Extract the LHS vectors
11213 SDValue LHS = Op.getOperand(0);
Craig Topperb14940a2012-04-22 20:55:18 +000011214 SDValue LHS1 = Extract128BitVector(LHS, 0, DAG, dl);
11215 SDValue LHS2 = Extract128BitVector(LHS, NumElems/2, DAG, dl);
Craig Toppera124f942011-11-21 01:12:36 +000011216
Craig Toppered2e13d2012-01-22 19:15:14 +000011217 MVT EltVT = VT.getVectorElementType().getSimpleVT();
11218 EVT NewVT = MVT::getVectorVT(EltVT, NumElems/2);
Craig Toppera124f942011-11-21 01:12:36 +000011219
Craig Toppered2e13d2012-01-22 19:15:14 +000011220 EVT ExtraEltVT = ExtraVT.getVectorElementType();
Craig Topperb6072642012-05-03 07:26:59 +000011221 unsigned ExtraNumElems = ExtraVT.getVectorNumElements();
Craig Toppered2e13d2012-01-22 19:15:14 +000011222 ExtraVT = EVT::getVectorVT(*DAG.getContext(), ExtraEltVT,
11223 ExtraNumElems/2);
11224 SDValue Extra = DAG.getValueType(ExtraVT);
Craig Toppera124f942011-11-21 01:12:36 +000011225
Craig Toppered2e13d2012-01-22 19:15:14 +000011226 LHS1 = DAG.getNode(Op.getOpcode(), dl, NewVT, LHS1, Extra);
11227 LHS2 = DAG.getNode(Op.getOpcode(), dl, NewVT, LHS2, Extra);
Craig Toppera124f942011-11-21 01:12:36 +000011228
Dmitri Gribenko2de05722012-09-10 21:26:47 +000011229 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT, LHS1, LHS2);
Craig Toppered2e13d2012-01-22 19:15:14 +000011230 }
11231 // fall through
11232 case MVT::v4i32:
11233 case MVT::v8i16: {
11234 SDValue Tmp1 = getTargetVShiftNode(X86ISD::VSHLI, dl, VT,
11235 Op.getOperand(0), ShAmt, DAG);
11236 return getTargetVShiftNode(X86ISD::VSRAI, dl, VT, Tmp1, ShAmt, DAG);
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000011237 }
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000011238 }
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000011239}
11240
11241
Craig Topper55b24052012-09-11 06:15:32 +000011242static SDValue LowerMEMBARRIER(SDValue Op, const X86Subtarget *Subtarget,
11243 SelectionDAG &DAG) {
Eric Christopher9a9d2752010-07-22 02:48:34 +000011244 DebugLoc dl = Op.getDebugLoc();
Michael J. Spencerec38de22010-10-10 22:04:20 +000011245
Eric Christopher77ed1352011-07-08 00:04:56 +000011246 // Go ahead and emit the fence on x86-64 even if we asked for no-sse2.
11247 // There isn't any reason to disable it if the target processor supports it.
Craig Topper1accb7e2012-01-10 06:54:16 +000011248 if (!Subtarget->hasSSE2() && !Subtarget->is64Bit()) {
Eric Christopherc0b2a202010-08-14 21:51:50 +000011249 SDValue Chain = Op.getOperand(0);
Eric Christopher77ed1352011-07-08 00:04:56 +000011250 SDValue Zero = DAG.getConstant(0, MVT::i32);
Eric Christopherc0b2a202010-08-14 21:51:50 +000011251 SDValue Ops[] = {
11252 DAG.getRegister(X86::ESP, MVT::i32), // Base
11253 DAG.getTargetConstant(1, MVT::i8), // Scale
11254 DAG.getRegister(0, MVT::i32), // Index
11255 DAG.getTargetConstant(0, MVT::i32), // Disp
11256 DAG.getRegister(0, MVT::i32), // Segment.
11257 Zero,
11258 Chain
11259 };
Michael J. Spencerec38de22010-10-10 22:04:20 +000011260 SDNode *Res =
Eric Christopherc0b2a202010-08-14 21:51:50 +000011261 DAG.getMachineNode(X86::OR32mrLocked, dl, MVT::Other, Ops,
11262 array_lengthof(Ops));
11263 return SDValue(Res, 0);
Eric Christopherb6729dc2010-08-04 23:03:04 +000011264 }
Michael J. Spencerec38de22010-10-10 22:04:20 +000011265
Eric Christopher9a9d2752010-07-22 02:48:34 +000011266 unsigned isDev = cast<ConstantSDNode>(Op.getOperand(5))->getZExtValue();
Chris Lattner132929a2010-08-14 17:26:09 +000011267 if (!isDev)
Eric Christopher9a9d2752010-07-22 02:48:34 +000011268 return DAG.getNode(X86ISD::MEMBARRIER, dl, MVT::Other, Op.getOperand(0));
Michael J. Spencerec38de22010-10-10 22:04:20 +000011269
Chris Lattner132929a2010-08-14 17:26:09 +000011270 unsigned Op1 = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
11271 unsigned Op2 = cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue();
11272 unsigned Op3 = cast<ConstantSDNode>(Op.getOperand(3))->getZExtValue();
11273 unsigned Op4 = cast<ConstantSDNode>(Op.getOperand(4))->getZExtValue();
Michael J. Spencerec38de22010-10-10 22:04:20 +000011274
Chris Lattner132929a2010-08-14 17:26:09 +000011275 // def : Pat<(membarrier (i8 0), (i8 0), (i8 0), (i8 1), (i8 1)), (SFENCE)>;
11276 if (!Op1 && !Op2 && !Op3 && Op4)
11277 return DAG.getNode(X86ISD::SFENCE, dl, MVT::Other, Op.getOperand(0));
Michael J. Spencerec38de22010-10-10 22:04:20 +000011278
Chris Lattner132929a2010-08-14 17:26:09 +000011279 // def : Pat<(membarrier (i8 1), (i8 0), (i8 0), (i8 0), (i8 1)), (LFENCE)>;
11280 if (Op1 && !Op2 && !Op3 && !Op4)
11281 return DAG.getNode(X86ISD::LFENCE, dl, MVT::Other, Op.getOperand(0));
Michael J. Spencerec38de22010-10-10 22:04:20 +000011282
11283 // def : Pat<(membarrier (i8 imm), (i8 imm), (i8 imm), (i8 imm), (i8 1)),
Chris Lattner132929a2010-08-14 17:26:09 +000011284 // (MFENCE)>;
11285 return DAG.getNode(X86ISD::MFENCE, dl, MVT::Other, Op.getOperand(0));
Eric Christopher9a9d2752010-07-22 02:48:34 +000011286}
11287
Craig Topper55b24052012-09-11 06:15:32 +000011288static SDValue LowerATOMIC_FENCE(SDValue Op, const X86Subtarget *Subtarget,
11289 SelectionDAG &DAG) {
Eli Friedman14648462011-07-27 22:21:52 +000011290 DebugLoc dl = Op.getDebugLoc();
11291 AtomicOrdering FenceOrdering = static_cast<AtomicOrdering>(
11292 cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue());
11293 SynchronizationScope FenceScope = static_cast<SynchronizationScope>(
11294 cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue());
11295
11296 // The only fence that needs an instruction is a sequentially-consistent
11297 // cross-thread fence.
11298 if (FenceOrdering == SequentiallyConsistent && FenceScope == CrossThread) {
11299 // Use mfence if we have SSE2 or we're on x86-64 (even if we asked for
11300 // no-sse2). There isn't any reason to disable it if the target processor
11301 // supports it.
Craig Topper1accb7e2012-01-10 06:54:16 +000011302 if (Subtarget->hasSSE2() || Subtarget->is64Bit())
Eli Friedman14648462011-07-27 22:21:52 +000011303 return DAG.getNode(X86ISD::MFENCE, dl, MVT::Other, Op.getOperand(0));
11304
11305 SDValue Chain = Op.getOperand(0);
11306 SDValue Zero = DAG.getConstant(0, MVT::i32);
11307 SDValue Ops[] = {
11308 DAG.getRegister(X86::ESP, MVT::i32), // Base
11309 DAG.getTargetConstant(1, MVT::i8), // Scale
11310 DAG.getRegister(0, MVT::i32), // Index
11311 DAG.getTargetConstant(0, MVT::i32), // Disp
11312 DAG.getRegister(0, MVT::i32), // Segment.
11313 Zero,
11314 Chain
11315 };
11316 SDNode *Res =
11317 DAG.getMachineNode(X86::OR32mrLocked, dl, MVT::Other, Ops,
11318 array_lengthof(Ops));
11319 return SDValue(Res, 0);
11320 }
11321
11322 // MEMBARRIER is a compiler barrier; it codegens to a no-op.
11323 return DAG.getNode(X86ISD::MEMBARRIER, dl, MVT::Other, Op.getOperand(0));
11324}
11325
11326
Craig Topper55b24052012-09-11 06:15:32 +000011327static SDValue LowerCMP_SWAP(SDValue Op, const X86Subtarget *Subtarget,
11328 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +000011329 EVT T = Op.getValueType();
Chris Lattner93c4a5b2010-09-21 23:59:42 +000011330 DebugLoc DL = Op.getDebugLoc();
Andrew Lenhartha76e2f02008-03-04 21:13:33 +000011331 unsigned Reg = 0;
11332 unsigned size = 0;
Owen Anderson825b72b2009-08-11 20:47:22 +000011333 switch(T.getSimpleVT().SimpleTy) {
Craig Topperabb94d02012-02-05 03:43:23 +000011334 default: llvm_unreachable("Invalid value type!");
Owen Anderson825b72b2009-08-11 20:47:22 +000011335 case MVT::i8: Reg = X86::AL; size = 1; break;
11336 case MVT::i16: Reg = X86::AX; size = 2; break;
11337 case MVT::i32: Reg = X86::EAX; size = 4; break;
11338 case MVT::i64:
Duncan Sands1607f052008-12-01 11:39:25 +000011339 assert(Subtarget->is64Bit() && "Node not type legal!");
11340 Reg = X86::RAX; size = 8;
Andrew Lenharthd19189e2008-03-05 01:15:49 +000011341 break;
Bill Wendling61edeb52008-12-02 01:06:39 +000011342 }
Chris Lattner93c4a5b2010-09-21 23:59:42 +000011343 SDValue cpIn = DAG.getCopyToReg(Op.getOperand(0), DL, Reg,
Dale Johannesend18a4622008-09-11 03:12:59 +000011344 Op.getOperand(2), SDValue());
Dan Gohman475871a2008-07-27 21:46:04 +000011345 SDValue Ops[] = { cpIn.getValue(0),
Evan Cheng8a186ae2008-09-24 23:26:36 +000011346 Op.getOperand(1),
11347 Op.getOperand(3),
Owen Anderson825b72b2009-08-11 20:47:22 +000011348 DAG.getTargetConstant(size, MVT::i8),
Evan Cheng8a186ae2008-09-24 23:26:36 +000011349 cpIn.getValue(1) };
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +000011350 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Chris Lattner93c4a5b2010-09-21 23:59:42 +000011351 MachineMemOperand *MMO = cast<AtomicSDNode>(Op)->getMemOperand();
11352 SDValue Result = DAG.getMemIntrinsicNode(X86ISD::LCMPXCHG_DAG, DL, Tys,
11353 Ops, 5, T, MMO);
Scott Michelfdc40a02009-02-17 22:15:04 +000011354 SDValue cpOut =
Chris Lattner93c4a5b2010-09-21 23:59:42 +000011355 DAG.getCopyFromReg(Result.getValue(0), DL, Reg, T, Result.getValue(1));
Andrew Lenharth26ed8692008-03-01 21:52:34 +000011356 return cpOut;
11357}
11358
Craig Topper55b24052012-09-11 06:15:32 +000011359static SDValue LowerREADCYCLECOUNTER(SDValue Op, const X86Subtarget *Subtarget,
11360 SelectionDAG &DAG) {
Duncan Sands1607f052008-12-01 11:39:25 +000011361 assert(Subtarget->is64Bit() && "Result not type legalized?");
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +000011362 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Duncan Sands1607f052008-12-01 11:39:25 +000011363 SDValue TheChain = Op.getOperand(0);
Dale Johannesen6f38cb62009-02-07 19:59:05 +000011364 DebugLoc dl = Op.getDebugLoc();
Dale Johannesene4d209d2009-02-03 20:21:25 +000011365 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, dl, Tys, &TheChain, 1);
Owen Anderson825b72b2009-08-11 20:47:22 +000011366 SDValue rax = DAG.getCopyFromReg(rd, dl, X86::RAX, MVT::i64, rd.getValue(1));
11367 SDValue rdx = DAG.getCopyFromReg(rax.getValue(1), dl, X86::RDX, MVT::i64,
Duncan Sands1607f052008-12-01 11:39:25 +000011368 rax.getValue(2));
Owen Anderson825b72b2009-08-11 20:47:22 +000011369 SDValue Tmp = DAG.getNode(ISD::SHL, dl, MVT::i64, rdx,
11370 DAG.getConstant(32, MVT::i8));
Duncan Sands1607f052008-12-01 11:39:25 +000011371 SDValue Ops[] = {
Owen Anderson825b72b2009-08-11 20:47:22 +000011372 DAG.getNode(ISD::OR, dl, MVT::i64, rax, Tmp),
Duncan Sands1607f052008-12-01 11:39:25 +000011373 rdx.getValue(1)
11374 };
Dale Johannesene4d209d2009-02-03 20:21:25 +000011375 return DAG.getMergeValues(Ops, 2, dl);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011376}
11377
Craig Topper55b24052012-09-11 06:15:32 +000011378SDValue X86TargetLowering::LowerBITCAST(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen7d07b482010-05-21 00:52:33 +000011379 EVT SrcVT = Op.getOperand(0).getValueType();
11380 EVT DstVT = Op.getValueType();
Craig Topper1accb7e2012-01-10 06:54:16 +000011381 assert(Subtarget->is64Bit() && !Subtarget->hasSSE2() &&
Chris Lattner2a786eb2010-12-19 20:19:20 +000011382 Subtarget->hasMMX() && "Unexpected custom BITCAST");
Michael J. Spencerec38de22010-10-10 22:04:20 +000011383 assert((DstVT == MVT::i64 ||
Dale Johannesen7d07b482010-05-21 00:52:33 +000011384 (DstVT.isVector() && DstVT.getSizeInBits()==64)) &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +000011385 "Unexpected custom BITCAST");
Dale Johannesen7d07b482010-05-21 00:52:33 +000011386 // i64 <=> MMX conversions are Legal.
11387 if (SrcVT==MVT::i64 && DstVT.isVector())
11388 return Op;
11389 if (DstVT==MVT::i64 && SrcVT.isVector())
11390 return Op;
Dale Johannesene39859a2010-05-21 18:40:15 +000011391 // MMX <=> MMX conversions are Legal.
11392 if (SrcVT.isVector() && DstVT.isVector())
11393 return Op;
Dale Johannesen7d07b482010-05-21 00:52:33 +000011394 // All other conversions need to be expanded.
11395 return SDValue();
11396}
Chris Lattner5b856542010-12-20 00:59:46 +000011397
Craig Topper55b24052012-09-11 06:15:32 +000011398static SDValue LowerLOAD_SUB(SDValue Op, SelectionDAG &DAG) {
Dale Johannesen71d1bf52008-09-29 22:25:26 +000011399 SDNode *Node = Op.getNode();
Dale Johannesene4d209d2009-02-03 20:21:25 +000011400 DebugLoc dl = Node->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +000011401 EVT T = Node->getValueType(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +000011402 SDValue negOp = DAG.getNode(ISD::SUB, dl, T,
Evan Cheng242b38b2009-02-23 09:03:22 +000011403 DAG.getConstant(0, T), Node->getOperand(2));
Dale Johannesene4d209d2009-02-03 20:21:25 +000011404 return DAG.getAtomic(ISD::ATOMIC_LOAD_ADD, dl,
Dan Gohman0b1d4a72008-12-23 21:37:04 +000011405 cast<AtomicSDNode>(Node)->getMemoryVT(),
Dale Johannesen71d1bf52008-09-29 22:25:26 +000011406 Node->getOperand(0),
11407 Node->getOperand(1), negOp,
11408 cast<AtomicSDNode>(Node)->getSrcValue(),
Eli Friedman55ba8162011-07-29 03:05:32 +000011409 cast<AtomicSDNode>(Node)->getAlignment(),
11410 cast<AtomicSDNode>(Node)->getOrdering(),
11411 cast<AtomicSDNode>(Node)->getSynchScope());
Mon P Wang63307c32008-05-05 19:05:59 +000011412}
11413
Eli Friedman327236c2011-08-24 20:50:09 +000011414static SDValue LowerATOMIC_STORE(SDValue Op, SelectionDAG &DAG) {
11415 SDNode *Node = Op.getNode();
11416 DebugLoc dl = Node->getDebugLoc();
Eli Friedmanf8f90f02011-08-24 22:33:28 +000011417 EVT VT = cast<AtomicSDNode>(Node)->getMemoryVT();
Eli Friedman327236c2011-08-24 20:50:09 +000011418
11419 // Convert seq_cst store -> xchg
Eli Friedmanf8f90f02011-08-24 22:33:28 +000011420 // Convert wide store -> swap (-> cmpxchg8b/cmpxchg16b)
11421 // FIXME: On 32-bit, store -> fist or movq would be more efficient
11422 // (The only way to get a 16-byte store is cmpxchg16b)
11423 // FIXME: 16-byte ATOMIC_SWAP isn't actually hooked up at the moment.
11424 if (cast<AtomicSDNode>(Node)->getOrdering() == SequentiallyConsistent ||
11425 !DAG.getTargetLoweringInfo().isTypeLegal(VT)) {
Eli Friedman4317fe12011-08-24 21:17:30 +000011426 SDValue Swap = DAG.getAtomic(ISD::ATOMIC_SWAP, dl,
11427 cast<AtomicSDNode>(Node)->getMemoryVT(),
11428 Node->getOperand(0),
11429 Node->getOperand(1), Node->getOperand(2),
Eli Friedmanf8f90f02011-08-24 22:33:28 +000011430 cast<AtomicSDNode>(Node)->getMemOperand(),
Eli Friedman4317fe12011-08-24 21:17:30 +000011431 cast<AtomicSDNode>(Node)->getOrdering(),
11432 cast<AtomicSDNode>(Node)->getSynchScope());
Eli Friedman327236c2011-08-24 20:50:09 +000011433 return Swap.getValue(1);
11434 }
11435 // Other atomic stores have a simple pattern.
11436 return Op;
11437}
11438
Chris Lattner5b856542010-12-20 00:59:46 +000011439static SDValue LowerADDC_ADDE_SUBC_SUBE(SDValue Op, SelectionDAG &DAG) {
11440 EVT VT = Op.getNode()->getValueType(0);
11441
11442 // Let legalize expand this if it isn't a legal type yet.
11443 if (!DAG.getTargetLoweringInfo().isTypeLegal(VT))
11444 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011445
Chris Lattner5b856542010-12-20 00:59:46 +000011446 SDVTList VTs = DAG.getVTList(VT, MVT::i32);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011447
Chris Lattner5b856542010-12-20 00:59:46 +000011448 unsigned Opc;
11449 bool ExtraOp = false;
11450 switch (Op.getOpcode()) {
Craig Topperabb94d02012-02-05 03:43:23 +000011451 default: llvm_unreachable("Invalid code");
Chris Lattner5b856542010-12-20 00:59:46 +000011452 case ISD::ADDC: Opc = X86ISD::ADD; break;
11453 case ISD::ADDE: Opc = X86ISD::ADC; ExtraOp = true; break;
11454 case ISD::SUBC: Opc = X86ISD::SUB; break;
11455 case ISD::SUBE: Opc = X86ISD::SBB; ExtraOp = true; break;
11456 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011457
Chris Lattner5b856542010-12-20 00:59:46 +000011458 if (!ExtraOp)
11459 return DAG.getNode(Opc, Op->getDebugLoc(), VTs, Op.getOperand(0),
11460 Op.getOperand(1));
11461 return DAG.getNode(Opc, Op->getDebugLoc(), VTs, Op.getOperand(0),
11462 Op.getOperand(1), Op.getOperand(2));
11463}
11464
Evan Cheng0db9fe62006-04-25 20:13:52 +000011465/// LowerOperation - Provide custom lowering hooks for some operations.
11466///
Dan Gohmand858e902010-04-17 15:26:15 +000011467SDValue X86TargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +000011468 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +000011469 default: llvm_unreachable("Should not custom lower this!");
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000011470 case ISD::SIGN_EXTEND_INREG: return LowerSIGN_EXTEND_INREG(Op,DAG);
Craig Topper55b24052012-09-11 06:15:32 +000011471 case ISD::MEMBARRIER: return LowerMEMBARRIER(Op, Subtarget, DAG);
11472 case ISD::ATOMIC_FENCE: return LowerATOMIC_FENCE(Op, Subtarget, DAG);
11473 case ISD::ATOMIC_CMP_SWAP: return LowerCMP_SWAP(Op, Subtarget, DAG);
Dan Gohman0b1d4a72008-12-23 21:37:04 +000011474 case ISD::ATOMIC_LOAD_SUB: return LowerLOAD_SUB(Op,DAG);
Eli Friedman327236c2011-08-24 20:50:09 +000011475 case ISD::ATOMIC_STORE: return LowerATOMIC_STORE(Op,DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000011476 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
Mon P Wangeb38ebf2010-01-24 00:05:03 +000011477 case ISD::CONCAT_VECTORS: return LowerCONCAT_VECTORS(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000011478 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
11479 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
11480 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
Craig Topper55b24052012-09-11 06:15:32 +000011481 case ISD::EXTRACT_SUBVECTOR: return LowerEXTRACT_SUBVECTOR(Op,Subtarget,DAG);
11482 case ISD::INSERT_SUBVECTOR: return LowerINSERT_SUBVECTOR(Op, Subtarget,DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000011483 case ISD::SCALAR_TO_VECTOR: return LowerSCALAR_TO_VECTOR(Op, DAG);
11484 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
11485 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +000011486 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Bill Wendling056292f2008-09-16 21:48:12 +000011487 case ISD::ExternalSymbol: return LowerExternalSymbol(Op, DAG);
Dan Gohmanf705adb2009-10-30 01:28:02 +000011488 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000011489 case ISD::SHL_PARTS:
11490 case ISD::SRA_PARTS:
Nadav Rotem43012222011-05-11 08:12:09 +000011491 case ISD::SRL_PARTS: return LowerShiftParts(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000011492 case ISD::SINT_TO_FP: return LowerSINT_TO_FP(Op, DAG);
Dale Johannesen1c15bf52008-10-21 20:50:01 +000011493 case ISD::UINT_TO_FP: return LowerUINT_TO_FP(Op, DAG);
Michael Liaobedcbd42012-10-16 18:14:11 +000011494 case ISD::TRUNCATE: return lowerTRUNCATE(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000011495 case ISD::FP_TO_SINT: return LowerFP_TO_SINT(Op, DAG);
Eli Friedman948e95a2009-05-23 09:59:16 +000011496 case ISD::FP_TO_UINT: return LowerFP_TO_UINT(Op, DAG);
Michael Liao9d796db2012-10-10 16:32:15 +000011497 case ISD::FP_EXTEND: return lowerFP_EXTEND(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000011498 case ISD::FABS: return LowerFABS(Op, DAG);
11499 case ISD::FNEG: return LowerFNEG(Op, DAG);
Evan Cheng68c47cb2007-01-05 07:55:56 +000011500 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Stuart Hastings4fd0dee2011-06-01 04:39:42 +000011501 case ISD::FGETSIGN: return LowerFGETSIGN(Op, DAG);
Evan Chenge5f62042007-09-29 00:00:36 +000011502 case ISD::SETCC: return LowerSETCC(Op, DAG);
11503 case ISD::SELECT: return LowerSELECT(Op, DAG);
11504 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000011505 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000011506 case ISD::VASTART: return LowerVASTART(Op, DAG);
Dan Gohman9018e832008-05-10 01:26:14 +000011507 case ISD::VAARG: return LowerVAARG(Op, DAG);
Craig Topper55b24052012-09-11 06:15:32 +000011508 case ISD::VACOPY: return LowerVACOPY(Op, Subtarget, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000011509 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
Benjamin Kramerb9bee042012-07-12 09:31:43 +000011510 case ISD::INTRINSIC_W_CHAIN: return LowerINTRINSIC_W_CHAIN(Op, DAG);
Nate Begemanbcc5f362007-01-29 22:58:52 +000011511 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
11512 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Anton Korobeynikov2365f512007-07-14 14:06:15 +000011513 case ISD::FRAME_TO_ARGS_OFFSET:
11514 return LowerFRAME_TO_ARGS_OFFSET(Op, DAG);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +000011515 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
Anton Korobeynikov2365f512007-07-14 14:06:15 +000011516 case ISD::EH_RETURN: return LowerEH_RETURN(Op, DAG);
Michael Liao6c0e04c2012-10-15 22:39:43 +000011517 case ISD::EH_SJLJ_SETJMP: return lowerEH_SJLJ_SETJMP(Op, DAG);
11518 case ISD::EH_SJLJ_LONGJMP: return lowerEH_SJLJ_LONGJMP(Op, DAG);
Duncan Sands4a544a72011-09-06 13:37:06 +000011519 case ISD::INIT_TRAMPOLINE: return LowerINIT_TRAMPOLINE(Op, DAG);
11520 case ISD::ADJUST_TRAMPOLINE: return LowerADJUST_TRAMPOLINE(Op, DAG);
Dan Gohman1a024862008-01-31 00:41:03 +000011521 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
Evan Cheng18efe262007-12-14 02:13:44 +000011522 case ISD::CTLZ: return LowerCTLZ(Op, DAG);
Chandler Carruthacc068e2011-12-24 10:55:54 +000011523 case ISD::CTLZ_ZERO_UNDEF: return LowerCTLZ_ZERO_UNDEF(Op, DAG);
Evan Cheng18efe262007-12-14 02:13:44 +000011524 case ISD::CTTZ: return LowerCTTZ(Op, DAG);
Craig Topper55b24052012-09-11 06:15:32 +000011525 case ISD::MUL: return LowerMUL(Op, Subtarget, DAG);
Nadav Rotem43012222011-05-11 08:12:09 +000011526 case ISD::SRA:
11527 case ISD::SRL:
11528 case ISD::SHL: return LowerShift(Op, DAG);
Bill Wendling74c37652008-12-09 22:08:41 +000011529 case ISD::SADDO:
11530 case ISD::UADDO:
11531 case ISD::SSUBO:
11532 case ISD::USUBO:
11533 case ISD::SMULO:
11534 case ISD::UMULO: return LowerXALUO(Op, DAG);
Craig Topper55b24052012-09-11 06:15:32 +000011535 case ISD::READCYCLECOUNTER: return LowerREADCYCLECOUNTER(Op, Subtarget,DAG);
Wesley Peckbf17cfa2010-11-23 03:31:01 +000011536 case ISD::BITCAST: return LowerBITCAST(Op, DAG);
Chris Lattner5b856542010-12-20 00:59:46 +000011537 case ISD::ADDC:
11538 case ISD::ADDE:
11539 case ISD::SUBC:
11540 case ISD::SUBE: return LowerADDC_ADDE_SUBC_SUBE(Op, DAG);
Craig Topper13894fa2011-08-24 06:14:18 +000011541 case ISD::ADD: return LowerADD(Op, DAG);
11542 case ISD::SUB: return LowerSUB(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000011543 }
Chris Lattner27a6c732007-11-24 07:07:01 +000011544}
11545
Eli Friedmanf8f90f02011-08-24 22:33:28 +000011546static void ReplaceATOMIC_LOAD(SDNode *Node,
11547 SmallVectorImpl<SDValue> &Results,
11548 SelectionDAG &DAG) {
11549 DebugLoc dl = Node->getDebugLoc();
11550 EVT VT = cast<AtomicSDNode>(Node)->getMemoryVT();
11551
11552 // Convert wide load -> cmpxchg8b/cmpxchg16b
11553 // FIXME: On 32-bit, load -> fild or movq would be more efficient
11554 // (The only way to get a 16-byte load is cmpxchg16b)
11555 // FIXME: 16-byte ATOMIC_CMP_SWAP isn't actually hooked up at the moment.
Benjamin Kramer2753ae32011-08-27 17:36:14 +000011556 SDValue Zero = DAG.getConstant(0, VT);
11557 SDValue Swap = DAG.getAtomic(ISD::ATOMIC_CMP_SWAP, dl, VT,
Eli Friedmanf8f90f02011-08-24 22:33:28 +000011558 Node->getOperand(0),
11559 Node->getOperand(1), Zero, Zero,
11560 cast<AtomicSDNode>(Node)->getMemOperand(),
11561 cast<AtomicSDNode>(Node)->getOrdering(),
11562 cast<AtomicSDNode>(Node)->getSynchScope());
11563 Results.push_back(Swap.getValue(0));
11564 Results.push_back(Swap.getValue(1));
11565}
11566
Craig Topperc0878702012-08-17 06:55:11 +000011567static void
Duncan Sands1607f052008-12-01 11:39:25 +000011568ReplaceATOMIC_BINARY_64(SDNode *Node, SmallVectorImpl<SDValue>&Results,
Craig Topperc0878702012-08-17 06:55:11 +000011569 SelectionDAG &DAG, unsigned NewOp) {
Dale Johannesene4d209d2009-02-03 20:21:25 +000011570 DebugLoc dl = Node->getDebugLoc();
Duncan Sands17001ce2011-10-18 12:44:00 +000011571 assert (Node->getValueType(0) == MVT::i64 &&
11572 "Only know how to expand i64 atomics");
Duncan Sands1607f052008-12-01 11:39:25 +000011573
11574 SDValue Chain = Node->getOperand(0);
11575 SDValue In1 = Node->getOperand(1);
Owen Anderson825b72b2009-08-11 20:47:22 +000011576 SDValue In2L = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +000011577 Node->getOperand(2), DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +000011578 SDValue In2H = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +000011579 Node->getOperand(2), DAG.getIntPtrConstant(1));
Dan Gohmanc76909a2009-09-25 20:36:54 +000011580 SDValue Ops[] = { Chain, In1, In2L, In2H };
Owen Anderson825b72b2009-08-11 20:47:22 +000011581 SDVTList Tys = DAG.getVTList(MVT::i32, MVT::i32, MVT::Other);
Dan Gohmanc76909a2009-09-25 20:36:54 +000011582 SDValue Result =
11583 DAG.getMemIntrinsicNode(NewOp, dl, Tys, Ops, 4, MVT::i64,
11584 cast<MemSDNode>(Node)->getMemOperand());
Duncan Sands1607f052008-12-01 11:39:25 +000011585 SDValue OpsF[] = { Result.getValue(0), Result.getValue(1)};
Owen Anderson825b72b2009-08-11 20:47:22 +000011586 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, OpsF, 2));
Duncan Sands1607f052008-12-01 11:39:25 +000011587 Results.push_back(Result.getValue(2));
11588}
11589
Duncan Sands126d9072008-07-04 11:47:58 +000011590/// ReplaceNodeResults - Replace a node with an illegal result type
11591/// with a new node built out of custom code.
Duncan Sands1607f052008-12-01 11:39:25 +000011592void X86TargetLowering::ReplaceNodeResults(SDNode *N,
11593 SmallVectorImpl<SDValue>&Results,
Dan Gohmand858e902010-04-17 15:26:15 +000011594 SelectionDAG &DAG) const {
Dale Johannesene4d209d2009-02-03 20:21:25 +000011595 DebugLoc dl = N->getDebugLoc();
Chris Lattner27a6c732007-11-24 07:07:01 +000011596 switch (N->getOpcode()) {
Duncan Sandsed294c42008-10-20 15:56:33 +000011597 default:
Craig Topperabb94d02012-02-05 03:43:23 +000011598 llvm_unreachable("Do not know how to custom type legalize this operation!");
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000011599 case ISD::SIGN_EXTEND_INREG:
Chris Lattner5b856542010-12-20 00:59:46 +000011600 case ISD::ADDC:
11601 case ISD::ADDE:
11602 case ISD::SUBC:
11603 case ISD::SUBE:
11604 // We don't want to expand or promote these.
11605 return;
Michael J. Spencer1a2d0612012-02-24 19:01:22 +000011606 case ISD::FP_TO_SINT:
11607 case ISD::FP_TO_UINT: {
11608 bool IsSigned = N->getOpcode() == ISD::FP_TO_SINT;
11609
11610 if (!IsSigned && !isIntegerTypeFTOL(SDValue(N, 0).getValueType()))
11611 return;
11612
Eli Friedman948e95a2009-05-23 09:59:16 +000011613 std::pair<SDValue,SDValue> Vals =
NAKAMURA Takumi9a68fdc2012-02-25 03:37:25 +000011614 FP_TO_INTHelper(SDValue(N, 0), DAG, IsSigned, /*IsReplace=*/ true);
Duncan Sands1607f052008-12-01 11:39:25 +000011615 SDValue FIST = Vals.first, StackSlot = Vals.second;
11616 if (FIST.getNode() != 0) {
Owen Andersone50ed302009-08-10 22:56:29 +000011617 EVT VT = N->getValueType(0);
Duncan Sands1607f052008-12-01 11:39:25 +000011618 // Return a load from the stack slot.
Michael J. Spencer1a2d0612012-02-24 19:01:22 +000011619 if (StackSlot.getNode() != 0)
11620 Results.push_back(DAG.getLoad(VT, dl, FIST, StackSlot,
11621 MachinePointerInfo(),
11622 false, false, false, 0));
11623 else
11624 Results.push_back(FIST);
Duncan Sands1607f052008-12-01 11:39:25 +000011625 }
11626 return;
11627 }
Michael Liao44c2d612012-10-10 16:53:28 +000011628 case ISD::FP_ROUND: {
11629 SDValue V = DAG.getNode(X86ISD::VFPROUND, dl, MVT::v4f32, N->getOperand(0));
11630 Results.push_back(V);
11631 return;
11632 }
Duncan Sands1607f052008-12-01 11:39:25 +000011633 case ISD::READCYCLECOUNTER: {
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +000011634 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Duncan Sands1607f052008-12-01 11:39:25 +000011635 SDValue TheChain = N->getOperand(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +000011636 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, dl, Tys, &TheChain, 1);
Owen Anderson825b72b2009-08-11 20:47:22 +000011637 SDValue eax = DAG.getCopyFromReg(rd, dl, X86::EAX, MVT::i32,
Dale Johannesendd64c412009-02-04 00:33:20 +000011638 rd.getValue(1));
Owen Anderson825b72b2009-08-11 20:47:22 +000011639 SDValue edx = DAG.getCopyFromReg(eax.getValue(1), dl, X86::EDX, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +000011640 eax.getValue(2));
11641 // Use a buildpair to merge the two 32-bit values into a 64-bit one.
11642 SDValue Ops[] = { eax, edx };
Owen Anderson825b72b2009-08-11 20:47:22 +000011643 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Ops, 2));
Duncan Sands1607f052008-12-01 11:39:25 +000011644 Results.push_back(edx.getValue(1));
11645 return;
11646 }
Dan Gohman0b1d4a72008-12-23 21:37:04 +000011647 case ISD::ATOMIC_CMP_SWAP: {
Owen Andersone50ed302009-08-10 22:56:29 +000011648 EVT T = N->getValueType(0);
Benjamin Kramer2753ae32011-08-27 17:36:14 +000011649 assert((T == MVT::i64 || T == MVT::i128) && "can only expand cmpxchg pair");
Eli Friedman43f51ae2011-08-26 21:21:21 +000011650 bool Regs64bit = T == MVT::i128;
11651 EVT HalfT = Regs64bit ? MVT::i64 : MVT::i32;
Duncan Sands1607f052008-12-01 11:39:25 +000011652 SDValue cpInL, cpInH;
Eli Friedman43f51ae2011-08-26 21:21:21 +000011653 cpInL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, HalfT, N->getOperand(2),
11654 DAG.getConstant(0, HalfT));
11655 cpInH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, HalfT, N->getOperand(2),
11656 DAG.getConstant(1, HalfT));
11657 cpInL = DAG.getCopyToReg(N->getOperand(0), dl,
11658 Regs64bit ? X86::RAX : X86::EAX,
11659 cpInL, SDValue());
11660 cpInH = DAG.getCopyToReg(cpInL.getValue(0), dl,
11661 Regs64bit ? X86::RDX : X86::EDX,
11662 cpInH, cpInL.getValue(1));
Duncan Sands1607f052008-12-01 11:39:25 +000011663 SDValue swapInL, swapInH;
Eli Friedman43f51ae2011-08-26 21:21:21 +000011664 swapInL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, HalfT, N->getOperand(3),
11665 DAG.getConstant(0, HalfT));
11666 swapInH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, HalfT, N->getOperand(3),
11667 DAG.getConstant(1, HalfT));
11668 swapInL = DAG.getCopyToReg(cpInH.getValue(0), dl,
11669 Regs64bit ? X86::RBX : X86::EBX,
11670 swapInL, cpInH.getValue(1));
11671 swapInH = DAG.getCopyToReg(swapInL.getValue(0), dl,
Chad Rosiera20e1e72012-08-01 18:39:17 +000011672 Regs64bit ? X86::RCX : X86::ECX,
Eli Friedman43f51ae2011-08-26 21:21:21 +000011673 swapInH, swapInL.getValue(1));
Duncan Sands1607f052008-12-01 11:39:25 +000011674 SDValue Ops[] = { swapInH.getValue(0),
11675 N->getOperand(1),
11676 swapInH.getValue(1) };
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +000011677 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Andrew Trick1a2cf3b2010-10-11 19:02:04 +000011678 MachineMemOperand *MMO = cast<AtomicSDNode>(N)->getMemOperand();
Eli Friedman43f51ae2011-08-26 21:21:21 +000011679 unsigned Opcode = Regs64bit ? X86ISD::LCMPXCHG16_DAG :
11680 X86ISD::LCMPXCHG8_DAG;
11681 SDValue Result = DAG.getMemIntrinsicNode(Opcode, dl, Tys,
Andrew Trick1a2cf3b2010-10-11 19:02:04 +000011682 Ops, 3, T, MMO);
Eli Friedman43f51ae2011-08-26 21:21:21 +000011683 SDValue cpOutL = DAG.getCopyFromReg(Result.getValue(0), dl,
11684 Regs64bit ? X86::RAX : X86::EAX,
11685 HalfT, Result.getValue(1));
11686 SDValue cpOutH = DAG.getCopyFromReg(cpOutL.getValue(1), dl,
11687 Regs64bit ? X86::RDX : X86::EDX,
11688 HalfT, cpOutL.getValue(2));
Duncan Sands1607f052008-12-01 11:39:25 +000011689 SDValue OpsF[] = { cpOutL.getValue(0), cpOutH.getValue(0)};
Eli Friedman43f51ae2011-08-26 21:21:21 +000011690 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, T, OpsF, 2));
Duncan Sands1607f052008-12-01 11:39:25 +000011691 Results.push_back(cpOutH.getValue(1));
11692 return;
11693 }
Dan Gohman0b1d4a72008-12-23 21:37:04 +000011694 case ISD::ATOMIC_LOAD_ADD:
Dan Gohman0b1d4a72008-12-23 21:37:04 +000011695 case ISD::ATOMIC_LOAD_AND:
Dan Gohman0b1d4a72008-12-23 21:37:04 +000011696 case ISD::ATOMIC_LOAD_NAND:
Dan Gohman0b1d4a72008-12-23 21:37:04 +000011697 case ISD::ATOMIC_LOAD_OR:
Dan Gohman0b1d4a72008-12-23 21:37:04 +000011698 case ISD::ATOMIC_LOAD_SUB:
Dan Gohman0b1d4a72008-12-23 21:37:04 +000011699 case ISD::ATOMIC_LOAD_XOR:
Michael Liaoe5e8f762012-09-25 18:08:13 +000011700 case ISD::ATOMIC_LOAD_MAX:
11701 case ISD::ATOMIC_LOAD_MIN:
11702 case ISD::ATOMIC_LOAD_UMAX:
11703 case ISD::ATOMIC_LOAD_UMIN:
Craig Topperc0878702012-08-17 06:55:11 +000011704 case ISD::ATOMIC_SWAP: {
11705 unsigned Opc;
11706 switch (N->getOpcode()) {
11707 default: llvm_unreachable("Unexpected opcode");
11708 case ISD::ATOMIC_LOAD_ADD:
11709 Opc = X86ISD::ATOMADD64_DAG;
11710 break;
11711 case ISD::ATOMIC_LOAD_AND:
11712 Opc = X86ISD::ATOMAND64_DAG;
11713 break;
11714 case ISD::ATOMIC_LOAD_NAND:
11715 Opc = X86ISD::ATOMNAND64_DAG;
11716 break;
11717 case ISD::ATOMIC_LOAD_OR:
11718 Opc = X86ISD::ATOMOR64_DAG;
11719 break;
11720 case ISD::ATOMIC_LOAD_SUB:
11721 Opc = X86ISD::ATOMSUB64_DAG;
11722 break;
11723 case ISD::ATOMIC_LOAD_XOR:
11724 Opc = X86ISD::ATOMXOR64_DAG;
11725 break;
Michael Liaoe5e8f762012-09-25 18:08:13 +000011726 case ISD::ATOMIC_LOAD_MAX:
11727 Opc = X86ISD::ATOMMAX64_DAG;
11728 break;
11729 case ISD::ATOMIC_LOAD_MIN:
11730 Opc = X86ISD::ATOMMIN64_DAG;
11731 break;
11732 case ISD::ATOMIC_LOAD_UMAX:
11733 Opc = X86ISD::ATOMUMAX64_DAG;
11734 break;
11735 case ISD::ATOMIC_LOAD_UMIN:
11736 Opc = X86ISD::ATOMUMIN64_DAG;
11737 break;
Craig Topperc0878702012-08-17 06:55:11 +000011738 case ISD::ATOMIC_SWAP:
11739 Opc = X86ISD::ATOMSWAP64_DAG;
11740 break;
11741 }
11742 ReplaceATOMIC_BINARY_64(N, Results, DAG, Opc);
Duncan Sands1607f052008-12-01 11:39:25 +000011743 return;
Craig Topperc0878702012-08-17 06:55:11 +000011744 }
Eli Friedmanf8f90f02011-08-24 22:33:28 +000011745 case ISD::ATOMIC_LOAD:
11746 ReplaceATOMIC_LOAD(N, Results, DAG);
Chris Lattner27a6c732007-11-24 07:07:01 +000011747 }
Evan Cheng0db9fe62006-04-25 20:13:52 +000011748}
11749
Evan Cheng72261582005-12-20 06:22:03 +000011750const char *X86TargetLowering::getTargetNodeName(unsigned Opcode) const {
11751 switch (Opcode) {
11752 default: return NULL;
Evan Cheng18efe262007-12-14 02:13:44 +000011753 case X86ISD::BSF: return "X86ISD::BSF";
11754 case X86ISD::BSR: return "X86ISD::BSR";
Evan Chenge3413162006-01-09 18:33:28 +000011755 case X86ISD::SHLD: return "X86ISD::SHLD";
11756 case X86ISD::SHRD: return "X86ISD::SHRD";
Evan Chengef6ffb12006-01-31 03:14:29 +000011757 case X86ISD::FAND: return "X86ISD::FAND";
Evan Cheng68c47cb2007-01-05 07:55:56 +000011758 case X86ISD::FOR: return "X86ISD::FOR";
Evan Cheng223547a2006-01-31 22:28:30 +000011759 case X86ISD::FXOR: return "X86ISD::FXOR";
Evan Cheng68c47cb2007-01-05 07:55:56 +000011760 case X86ISD::FSRL: return "X86ISD::FSRL";
Evan Chenga3195e82006-01-12 22:54:21 +000011761 case X86ISD::FILD: return "X86ISD::FILD";
Evan Chenge3de85b2006-02-04 02:20:30 +000011762 case X86ISD::FILD_FLAG: return "X86ISD::FILD_FLAG";
Evan Cheng72261582005-12-20 06:22:03 +000011763 case X86ISD::FP_TO_INT16_IN_MEM: return "X86ISD::FP_TO_INT16_IN_MEM";
11764 case X86ISD::FP_TO_INT32_IN_MEM: return "X86ISD::FP_TO_INT32_IN_MEM";
11765 case X86ISD::FP_TO_INT64_IN_MEM: return "X86ISD::FP_TO_INT64_IN_MEM";
Evan Chengb077b842005-12-21 02:39:21 +000011766 case X86ISD::FLD: return "X86ISD::FLD";
Evan Chengd90eb7f2006-01-05 00:27:02 +000011767 case X86ISD::FST: return "X86ISD::FST";
Evan Cheng72261582005-12-20 06:22:03 +000011768 case X86ISD::CALL: return "X86ISD::CALL";
Evan Cheng72261582005-12-20 06:22:03 +000011769 case X86ISD::RDTSC_DAG: return "X86ISD::RDTSC_DAG";
Dan Gohmanc7a37d42008-12-23 22:45:23 +000011770 case X86ISD::BT: return "X86ISD::BT";
Evan Cheng72261582005-12-20 06:22:03 +000011771 case X86ISD::CMP: return "X86ISD::CMP";
Evan Cheng6be2c582006-04-05 23:38:46 +000011772 case X86ISD::COMI: return "X86ISD::COMI";
11773 case X86ISD::UCOMI: return "X86ISD::UCOMI";
Evan Chengd5781fc2005-12-21 20:21:51 +000011774 case X86ISD::SETCC: return "X86ISD::SETCC";
Evan Chengad9c0a32009-12-15 00:53:42 +000011775 case X86ISD::SETCC_CARRY: return "X86ISD::SETCC_CARRY";
Stuart Hastings865f0932011-06-03 23:53:54 +000011776 case X86ISD::FSETCCsd: return "X86ISD::FSETCCsd";
11777 case X86ISD::FSETCCss: return "X86ISD::FSETCCss";
Evan Cheng72261582005-12-20 06:22:03 +000011778 case X86ISD::CMOV: return "X86ISD::CMOV";
11779 case X86ISD::BRCOND: return "X86ISD::BRCOND";
Evan Chengb077b842005-12-21 02:39:21 +000011780 case X86ISD::RET_FLAG: return "X86ISD::RET_FLAG";
Evan Cheng8df346b2006-03-04 01:12:00 +000011781 case X86ISD::REP_STOS: return "X86ISD::REP_STOS";
11782 case X86ISD::REP_MOVS: return "X86ISD::REP_MOVS";
Evan Cheng7ccced62006-02-18 00:15:05 +000011783 case X86ISD::GlobalBaseReg: return "X86ISD::GlobalBaseReg";
Evan Cheng020d2e82006-02-23 20:41:18 +000011784 case X86ISD::Wrapper: return "X86ISD::Wrapper";
Chris Lattner18c59872009-06-27 04:16:01 +000011785 case X86ISD::WrapperRIP: return "X86ISD::WrapperRIP";
Nate Begeman14d12ca2008-02-11 04:19:36 +000011786 case X86ISD::PEXTRB: return "X86ISD::PEXTRB";
Evan Chengb067a1e2006-03-31 19:22:53 +000011787 case X86ISD::PEXTRW: return "X86ISD::PEXTRW";
Nate Begeman14d12ca2008-02-11 04:19:36 +000011788 case X86ISD::INSERTPS: return "X86ISD::INSERTPS";
11789 case X86ISD::PINSRB: return "X86ISD::PINSRB";
Evan Cheng653159f2006-03-31 21:55:24 +000011790 case X86ISD::PINSRW: return "X86ISD::PINSRW";
Nate Begemanb9a47b82009-02-23 08:49:38 +000011791 case X86ISD::PSHUFB: return "X86ISD::PSHUFB";
Bruno Cardoso Lopesc1af4772011-07-13 21:36:47 +000011792 case X86ISD::ANDNP: return "X86ISD::ANDNP";
Craig Topper31133842011-11-19 07:33:10 +000011793 case X86ISD::PSIGN: return "X86ISD::PSIGN";
Craig Toppere6a62772011-11-13 17:31:07 +000011794 case X86ISD::BLENDV: return "X86ISD::BLENDV";
Nadav Roteme6113782012-04-11 06:40:27 +000011795 case X86ISD::BLENDPW: return "X86ISD::BLENDPW";
11796 case X86ISD::BLENDPS: return "X86ISD::BLENDPS";
11797 case X86ISD::BLENDPD: return "X86ISD::BLENDPD";
Craig Topperfe033152011-12-06 09:31:36 +000011798 case X86ISD::HADD: return "X86ISD::HADD";
11799 case X86ISD::HSUB: return "X86ISD::HSUB";
Craig Toppere6a62772011-11-13 17:31:07 +000011800 case X86ISD::FHADD: return "X86ISD::FHADD";
11801 case X86ISD::FHSUB: return "X86ISD::FHSUB";
Evan Cheng8ca29322006-11-10 21:43:37 +000011802 case X86ISD::FMAX: return "X86ISD::FMAX";
11803 case X86ISD::FMIN: return "X86ISD::FMIN";
Nadav Rotemd60cb112012-08-19 13:06:16 +000011804 case X86ISD::FMAXC: return "X86ISD::FMAXC";
11805 case X86ISD::FMINC: return "X86ISD::FMINC";
Dan Gohman20382522007-07-10 00:05:58 +000011806 case X86ISD::FRSQRT: return "X86ISD::FRSQRT";
11807 case X86ISD::FRCP: return "X86ISD::FRCP";
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +000011808 case X86ISD::TLSADDR: return "X86ISD::TLSADDR";
Hans Wennborgf0234fc2012-06-01 16:27:21 +000011809 case X86ISD::TLSBASEADDR: return "X86ISD::TLSBASEADDR";
Eric Christopher30ef0e52010-06-03 04:07:48 +000011810 case X86ISD::TLSCALL: return "X86ISD::TLSCALL";
Michael Liao6c0e04c2012-10-15 22:39:43 +000011811 case X86ISD::EH_SJLJ_SETJMP: return "X86ISD::EH_SJLJ_SETJMP";
11812 case X86ISD::EH_SJLJ_LONGJMP: return "X86ISD::EH_SJLJ_LONGJMP";
Anton Korobeynikov2365f512007-07-14 14:06:15 +000011813 case X86ISD::EH_RETURN: return "X86ISD::EH_RETURN";
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +000011814 case X86ISD::TC_RETURN: return "X86ISD::TC_RETURN";
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000011815 case X86ISD::FNSTCW16m: return "X86ISD::FNSTCW16m";
Benjamin Kramer17c836c2012-04-27 12:07:43 +000011816 case X86ISD::FNSTSW16r: return "X86ISD::FNSTSW16r";
Evan Cheng7e2ff772008-05-08 00:57:18 +000011817 case X86ISD::LCMPXCHG_DAG: return "X86ISD::LCMPXCHG_DAG";
11818 case X86ISD::LCMPXCHG8_DAG: return "X86ISD::LCMPXCHG8_DAG";
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011819 case X86ISD::ATOMADD64_DAG: return "X86ISD::ATOMADD64_DAG";
11820 case X86ISD::ATOMSUB64_DAG: return "X86ISD::ATOMSUB64_DAG";
11821 case X86ISD::ATOMOR64_DAG: return "X86ISD::ATOMOR64_DAG";
11822 case X86ISD::ATOMXOR64_DAG: return "X86ISD::ATOMXOR64_DAG";
11823 case X86ISD::ATOMAND64_DAG: return "X86ISD::ATOMAND64_DAG";
11824 case X86ISD::ATOMNAND64_DAG: return "X86ISD::ATOMNAND64_DAG";
Evan Chengd880b972008-05-09 21:53:03 +000011825 case X86ISD::VZEXT_MOVL: return "X86ISD::VZEXT_MOVL";
Michael Liaob7bf7262012-08-14 22:53:17 +000011826 case X86ISD::VSEXT_MOVL: return "X86ISD::VSEXT_MOVL";
Evan Chengd880b972008-05-09 21:53:03 +000011827 case X86ISD::VZEXT_LOAD: return "X86ISD::VZEXT_LOAD";
Michael Liao7091b242012-08-14 21:24:47 +000011828 case X86ISD::VFPEXT: return "X86ISD::VFPEXT";
Michael Liao44c2d612012-10-10 16:53:28 +000011829 case X86ISD::VFPROUND: return "X86ISD::VFPROUND";
Craig Toppered2e13d2012-01-22 19:15:14 +000011830 case X86ISD::VSHLDQ: return "X86ISD::VSHLDQ";
11831 case X86ISD::VSRLDQ: return "X86ISD::VSRLDQ";
Evan Chengf26ffe92008-05-29 08:22:04 +000011832 case X86ISD::VSHL: return "X86ISD::VSHL";
11833 case X86ISD::VSRL: return "X86ISD::VSRL";
Craig Toppered2e13d2012-01-22 19:15:14 +000011834 case X86ISD::VSRA: return "X86ISD::VSRA";
11835 case X86ISD::VSHLI: return "X86ISD::VSHLI";
11836 case X86ISD::VSRLI: return "X86ISD::VSRLI";
11837 case X86ISD::VSRAI: return "X86ISD::VSRAI";
Craig Topper1906d322012-01-22 23:36:02 +000011838 case X86ISD::CMPP: return "X86ISD::CMPP";
Craig Topper67609fd2012-01-22 22:42:16 +000011839 case X86ISD::PCMPEQ: return "X86ISD::PCMPEQ";
11840 case X86ISD::PCMPGT: return "X86ISD::PCMPGT";
Bill Wendlingab55ebd2008-12-12 00:56:36 +000011841 case X86ISD::ADD: return "X86ISD::ADD";
11842 case X86ISD::SUB: return "X86ISD::SUB";
Chris Lattner5b856542010-12-20 00:59:46 +000011843 case X86ISD::ADC: return "X86ISD::ADC";
11844 case X86ISD::SBB: return "X86ISD::SBB";
Bill Wendlingd350e022008-12-12 21:15:41 +000011845 case X86ISD::SMUL: return "X86ISD::SMUL";
11846 case X86ISD::UMUL: return "X86ISD::UMUL";
Dan Gohman076aee32009-03-04 19:44:21 +000011847 case X86ISD::INC: return "X86ISD::INC";
11848 case X86ISD::DEC: return "X86ISD::DEC";
Dan Gohmane220c4b2009-09-18 19:59:53 +000011849 case X86ISD::OR: return "X86ISD::OR";
11850 case X86ISD::XOR: return "X86ISD::XOR";
11851 case X86ISD::AND: return "X86ISD::AND";
Craig Topper54a11172011-10-14 07:06:56 +000011852 case X86ISD::ANDN: return "X86ISD::ANDN";
Craig Toppere6a62772011-11-13 17:31:07 +000011853 case X86ISD::BLSI: return "X86ISD::BLSI";
11854 case X86ISD::BLSMSK: return "X86ISD::BLSMSK";
11855 case X86ISD::BLSR: return "X86ISD::BLSR";
Evan Cheng73f24c92009-03-30 21:36:47 +000011856 case X86ISD::MUL_IMM: return "X86ISD::MUL_IMM";
Eric Christopher71c67532009-07-29 00:28:05 +000011857 case X86ISD::PTEST: return "X86ISD::PTEST";
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +000011858 case X86ISD::TESTP: return "X86ISD::TESTP";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000011859 case X86ISD::PALIGN: return "X86ISD::PALIGN";
11860 case X86ISD::PSHUFD: return "X86ISD::PSHUFD";
11861 case X86ISD::PSHUFHW: return "X86ISD::PSHUFHW";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000011862 case X86ISD::PSHUFLW: return "X86ISD::PSHUFLW";
Craig Topperb3982da2011-12-31 23:50:21 +000011863 case X86ISD::SHUFP: return "X86ISD::SHUFP";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000011864 case X86ISD::MOVLHPS: return "X86ISD::MOVLHPS";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000011865 case X86ISD::MOVLHPD: return "X86ISD::MOVLHPD";
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +000011866 case X86ISD::MOVHLPS: return "X86ISD::MOVHLPS";
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +000011867 case X86ISD::MOVLPS: return "X86ISD::MOVLPS";
11868 case X86ISD::MOVLPD: return "X86ISD::MOVLPD";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000011869 case X86ISD::MOVDDUP: return "X86ISD::MOVDDUP";
11870 case X86ISD::MOVSHDUP: return "X86ISD::MOVSHDUP";
11871 case X86ISD::MOVSLDUP: return "X86ISD::MOVSLDUP";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000011872 case X86ISD::MOVSD: return "X86ISD::MOVSD";
11873 case X86ISD::MOVSS: return "X86ISD::MOVSS";
Craig Topper34671b82011-12-06 08:21:25 +000011874 case X86ISD::UNPCKL: return "X86ISD::UNPCKL";
11875 case X86ISD::UNPCKH: return "X86ISD::UNPCKH";
Bruno Cardoso Lopes0e6d2302011-08-17 02:29:19 +000011876 case X86ISD::VBROADCAST: return "X86ISD::VBROADCAST";
Craig Topper316cd2a2011-11-30 06:25:25 +000011877 case X86ISD::VPERMILP: return "X86ISD::VPERMILP";
Craig Topperec24e612011-11-30 07:47:51 +000011878 case X86ISD::VPERM2X128: return "X86ISD::VPERM2X128";
Craig Topper8325c112012-04-16 00:41:45 +000011879 case X86ISD::VPERMV: return "X86ISD::VPERMV";
11880 case X86ISD::VPERMI: return "X86ISD::VPERMI";
Craig Topper5b209e82012-02-05 03:14:49 +000011881 case X86ISD::PMULUDQ: return "X86ISD::PMULUDQ";
Dan Gohmand6708ea2009-08-15 01:38:56 +000011882 case X86ISD::VASTART_SAVE_XMM_REGS: return "X86ISD::VASTART_SAVE_XMM_REGS";
Dan Gohman320afb82010-10-12 18:00:49 +000011883 case X86ISD::VAARG_64: return "X86ISD::VAARG_64";
Michael J. Spencere9c253e2010-10-21 01:41:01 +000011884 case X86ISD::WIN_ALLOCA: return "X86ISD::WIN_ALLOCA";
Eli Friedman14648462011-07-27 22:21:52 +000011885 case X86ISD::MEMBARRIER: return "X86ISD::MEMBARRIER";
Rafael Espindolad07b7ec2011-08-30 19:43:21 +000011886 case X86ISD::SEG_ALLOCA: return "X86ISD::SEG_ALLOCA";
Michael J. Spencer1a2d0612012-02-24 19:01:22 +000011887 case X86ISD::WIN_FTOL: return "X86ISD::WIN_FTOL";
Benjamin Kramer17c836c2012-04-27 12:07:43 +000011888 case X86ISD::SAHF: return "X86ISD::SAHF";
Benjamin Kramerb9bee042012-07-12 09:31:43 +000011889 case X86ISD::RDRAND: return "X86ISD::RDRAND";
Elena Demikhovsky1503aba2012-08-01 12:06:00 +000011890 case X86ISD::FMADD: return "X86ISD::FMADD";
11891 case X86ISD::FMSUB: return "X86ISD::FMSUB";
11892 case X86ISD::FNMADD: return "X86ISD::FNMADD";
11893 case X86ISD::FNMSUB: return "X86ISD::FNMSUB";
11894 case X86ISD::FMADDSUB: return "X86ISD::FMADDSUB";
11895 case X86ISD::FMSUBADD: return "X86ISD::FMSUBADD";
Evan Cheng72261582005-12-20 06:22:03 +000011896 }
11897}
Evan Cheng3a03ebb2005-12-21 23:05:39 +000011898
Chris Lattnerc9addb72007-03-30 23:15:24 +000011899// isLegalAddressingMode - Return true if the addressing mode represented
11900// by AM is legal for this target, for a load/store of the specified type.
Scott Michelfdc40a02009-02-17 22:15:04 +000011901bool X86TargetLowering::isLegalAddressingMode(const AddrMode &AM,
Chris Lattnerdb125cf2011-07-18 04:54:35 +000011902 Type *Ty) const {
Chris Lattnerc9addb72007-03-30 23:15:24 +000011903 // X86 supports extremely general addressing modes.
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000011904 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman92b651f2010-08-24 15:55:12 +000011905 Reloc::Model R = getTargetMachine().getRelocationModel();
Scott Michelfdc40a02009-02-17 22:15:04 +000011906
Chris Lattnerc9addb72007-03-30 23:15:24 +000011907 // X86 allows a sign-extended 32-bit immediate field as a displacement.
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000011908 if (!X86::isOffsetSuitableForCodeModel(AM.BaseOffs, M, AM.BaseGV != NULL))
Chris Lattnerc9addb72007-03-30 23:15:24 +000011909 return false;
Scott Michelfdc40a02009-02-17 22:15:04 +000011910
Chris Lattnerc9addb72007-03-30 23:15:24 +000011911 if (AM.BaseGV) {
Chris Lattnerdfed4132009-07-10 07:38:24 +000011912 unsigned GVFlags =
11913 Subtarget->ClassifyGlobalReference(AM.BaseGV, getTargetMachine());
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000011914
Chris Lattnerdfed4132009-07-10 07:38:24 +000011915 // If a reference to this global requires an extra load, we can't fold it.
11916 if (isGlobalStubReference(GVFlags))
Chris Lattnerc9addb72007-03-30 23:15:24 +000011917 return false;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000011918
Chris Lattnerdfed4132009-07-10 07:38:24 +000011919 // If BaseGV requires a register for the PIC base, we cannot also have a
11920 // BaseReg specified.
11921 if (AM.HasBaseReg && isGlobalRelativeToPICBase(GVFlags))
Dale Johannesen203af582008-12-05 21:47:27 +000011922 return false;
Evan Cheng52787842007-08-01 23:46:47 +000011923
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000011924 // If lower 4G is not available, then we must use rip-relative addressing.
Dan Gohman92b651f2010-08-24 15:55:12 +000011925 if ((M != CodeModel::Small || R != Reloc::Static) &&
11926 Subtarget->is64Bit() && (AM.BaseOffs || AM.Scale > 1))
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000011927 return false;
Chris Lattnerc9addb72007-03-30 23:15:24 +000011928 }
Scott Michelfdc40a02009-02-17 22:15:04 +000011929
Chris Lattnerc9addb72007-03-30 23:15:24 +000011930 switch (AM.Scale) {
11931 case 0:
11932 case 1:
11933 case 2:
11934 case 4:
11935 case 8:
11936 // These scales always work.
11937 break;
11938 case 3:
11939 case 5:
11940 case 9:
11941 // These scales are formed with basereg+scalereg. Only accept if there is
11942 // no basereg yet.
11943 if (AM.HasBaseReg)
11944 return false;
11945 break;
11946 default: // Other stuff never works.
11947 return false;
11948 }
Scott Michelfdc40a02009-02-17 22:15:04 +000011949
Chris Lattnerc9addb72007-03-30 23:15:24 +000011950 return true;
11951}
11952
11953
Chris Lattnerdb125cf2011-07-18 04:54:35 +000011954bool X86TargetLowering::isTruncateFree(Type *Ty1, Type *Ty2) const {
Duncan Sandsb0bc6c32010-02-15 16:12:20 +000011955 if (!Ty1->isIntegerTy() || !Ty2->isIntegerTy())
Evan Cheng2bd122c2007-10-26 01:56:11 +000011956 return false;
Evan Chenge127a732007-10-29 07:57:50 +000011957 unsigned NumBits1 = Ty1->getPrimitiveSizeInBits();
11958 unsigned NumBits2 = Ty2->getPrimitiveSizeInBits();
Evan Cheng260e07e2008-03-20 02:18:41 +000011959 if (NumBits1 <= NumBits2)
Evan Chenge127a732007-10-29 07:57:50 +000011960 return false;
Dan Gohman377fbc02010-02-25 03:04:36 +000011961 return true;
Evan Cheng2bd122c2007-10-26 01:56:11 +000011962}
11963
Evan Cheng70e10d32012-07-17 06:53:39 +000011964bool X86TargetLowering::isLegalICmpImmediate(int64_t Imm) const {
11965 return Imm == (int32_t)Imm;
11966}
11967
11968bool X86TargetLowering::isLegalAddImmediate(int64_t Imm) const {
Evan Chenga9e13ba2012-07-17 18:54:11 +000011969 // Can also use sub to handle negated immediates.
Evan Cheng70e10d32012-07-17 06:53:39 +000011970 return Imm == (int32_t)Imm;
11971}
11972
Owen Andersone50ed302009-08-10 22:56:29 +000011973bool X86TargetLowering::isTruncateFree(EVT VT1, EVT VT2) const {
Duncan Sands83ec4b62008-06-06 12:08:01 +000011974 if (!VT1.isInteger() || !VT2.isInteger())
Evan Cheng3c3ddb32007-10-29 19:58:20 +000011975 return false;
Duncan Sands83ec4b62008-06-06 12:08:01 +000011976 unsigned NumBits1 = VT1.getSizeInBits();
11977 unsigned NumBits2 = VT2.getSizeInBits();
Evan Cheng260e07e2008-03-20 02:18:41 +000011978 if (NumBits1 <= NumBits2)
Evan Cheng3c3ddb32007-10-29 19:58:20 +000011979 return false;
Dan Gohman377fbc02010-02-25 03:04:36 +000011980 return true;
Evan Cheng3c3ddb32007-10-29 19:58:20 +000011981}
Evan Cheng2bd122c2007-10-26 01:56:11 +000011982
Chris Lattnerdb125cf2011-07-18 04:54:35 +000011983bool X86TargetLowering::isZExtFree(Type *Ty1, Type *Ty2) const {
Dan Gohman349ba492009-04-09 02:06:09 +000011984 // x86-64 implicitly zero-extends 32-bit results in 64-bit registers.
Duncan Sandsb0bc6c32010-02-15 16:12:20 +000011985 return Ty1->isIntegerTy(32) && Ty2->isIntegerTy(64) && Subtarget->is64Bit();
Dan Gohman97121ba2009-04-08 00:15:30 +000011986}
11987
Owen Andersone50ed302009-08-10 22:56:29 +000011988bool X86TargetLowering::isZExtFree(EVT VT1, EVT VT2) const {
Dan Gohman349ba492009-04-09 02:06:09 +000011989 // x86-64 implicitly zero-extends 32-bit results in 64-bit registers.
Owen Anderson825b72b2009-08-11 20:47:22 +000011990 return VT1 == MVT::i32 && VT2 == MVT::i64 && Subtarget->is64Bit();
Dan Gohman97121ba2009-04-08 00:15:30 +000011991}
11992
Owen Andersone50ed302009-08-10 22:56:29 +000011993bool X86TargetLowering::isNarrowingProfitable(EVT VT1, EVT VT2) const {
Evan Cheng8b944d32009-05-28 00:35:15 +000011994 // i16 instructions are longer (0x66 prefix) and potentially slower.
Owen Anderson825b72b2009-08-11 20:47:22 +000011995 return !(VT1 == MVT::i32 && VT2 == MVT::i16);
Evan Cheng8b944d32009-05-28 00:35:15 +000011996}
11997
Evan Cheng60c07e12006-07-05 22:17:51 +000011998/// isShuffleMaskLegal - Targets can use this to indicate that they only
11999/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
12000/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
12001/// are assumed to be legal.
12002bool
Eric Christopherfd179292009-08-27 18:07:15 +000012003X86TargetLowering::isShuffleMaskLegal(const SmallVectorImpl<int> &M,
Owen Andersone50ed302009-08-10 22:56:29 +000012004 EVT VT) const {
Eric Christophercff6f852010-04-15 01:40:20 +000012005 // Very little shuffling can be done for 64-bit vectors right now.
Nate Begeman9008ca62009-04-27 18:41:29 +000012006 if (VT.getSizeInBits() == 64)
Craig Topper1dc0fbc2011-12-05 07:27:14 +000012007 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +000012008
Nate Begemana09008b2009-10-19 02:17:23 +000012009 // FIXME: pshufb, blends, shifts.
Nate Begeman9008ca62009-04-27 18:41:29 +000012010 return (VT.getVectorNumElements() == 2 ||
12011 ShuffleVectorSDNode::isSplatMask(&M[0], VT) ||
12012 isMOVLMask(M, VT) ||
Craig Topper1a7700a2012-01-19 08:19:12 +000012013 isSHUFPMask(M, VT, Subtarget->hasAVX()) ||
Nate Begeman9008ca62009-04-27 18:41:29 +000012014 isPSHUFDMask(M, VT) ||
Craig Toppera9a568a2012-05-02 08:03:44 +000012015 isPSHUFHWMask(M, VT, Subtarget->hasAVX2()) ||
12016 isPSHUFLWMask(M, VT, Subtarget->hasAVX2()) ||
Craig Topper0e2037b2012-01-20 05:53:00 +000012017 isPALIGNRMask(M, VT, Subtarget) ||
Craig Topper6347e862011-11-21 06:57:39 +000012018 isUNPCKLMask(M, VT, Subtarget->hasAVX2()) ||
12019 isUNPCKHMask(M, VT, Subtarget->hasAVX2()) ||
Craig Topper94438ba2011-12-16 08:06:31 +000012020 isUNPCKL_v_undef_Mask(M, VT, Subtarget->hasAVX2()) ||
12021 isUNPCKH_v_undef_Mask(M, VT, Subtarget->hasAVX2()));
Evan Cheng60c07e12006-07-05 22:17:51 +000012022}
12023
Dan Gohman7d8143f2008-04-09 20:09:42 +000012024bool
Nate Begeman5a5ca152009-04-29 05:20:52 +000012025X86TargetLowering::isVectorClearMaskLegal(const SmallVectorImpl<int> &Mask,
Owen Andersone50ed302009-08-10 22:56:29 +000012026 EVT VT) const {
Nate Begeman9008ca62009-04-27 18:41:29 +000012027 unsigned NumElts = VT.getVectorNumElements();
12028 // FIXME: This collection of masks seems suspect.
12029 if (NumElts == 2)
12030 return true;
Craig Topper7a9a28b2012-08-12 02:23:29 +000012031 if (NumElts == 4 && VT.is128BitVector()) {
Nate Begeman9008ca62009-04-27 18:41:29 +000012032 return (isMOVLMask(Mask, VT) ||
12033 isCommutedMOVLMask(Mask, VT, true) ||
Craig Topper1a7700a2012-01-19 08:19:12 +000012034 isSHUFPMask(Mask, VT, Subtarget->hasAVX()) ||
12035 isSHUFPMask(Mask, VT, Subtarget->hasAVX(), /* Commuted */ true));
Evan Cheng60c07e12006-07-05 22:17:51 +000012036 }
12037 return false;
12038}
12039
12040//===----------------------------------------------------------------------===//
12041// X86 Scheduler Hooks
12042//===----------------------------------------------------------------------===//
12043
Mon P Wang63307c32008-05-05 19:05:59 +000012044// private utility function
Scott Michelfdc40a02009-02-17 22:15:04 +000012045
Michael Liaob118a072012-09-20 03:06:15 +000012046// Get CMPXCHG opcode for the specified data type.
12047static unsigned getCmpXChgOpcode(EVT VT) {
12048 switch (VT.getSimpleVT().SimpleTy) {
12049 case MVT::i8: return X86::LCMPXCHG8;
12050 case MVT::i16: return X86::LCMPXCHG16;
12051 case MVT::i32: return X86::LCMPXCHG32;
12052 case MVT::i64: return X86::LCMPXCHG64;
12053 default:
12054 break;
Richard Smith42fc29e2012-04-13 22:47:00 +000012055 }
Michael Liaob118a072012-09-20 03:06:15 +000012056 llvm_unreachable("Invalid operand size!");
Mon P Wang63307c32008-05-05 19:05:59 +000012057}
12058
Michael Liaob118a072012-09-20 03:06:15 +000012059// Get LOAD opcode for the specified data type.
12060static unsigned getLoadOpcode(EVT VT) {
12061 switch (VT.getSimpleVT().SimpleTy) {
12062 case MVT::i8: return X86::MOV8rm;
12063 case MVT::i16: return X86::MOV16rm;
12064 case MVT::i32: return X86::MOV32rm;
12065 case MVT::i64: return X86::MOV64rm;
12066 default:
12067 break;
12068 }
12069 llvm_unreachable("Invalid operand size!");
12070}
12071
12072// Get opcode of the non-atomic one from the specified atomic instruction.
12073static unsigned getNonAtomicOpcode(unsigned Opc) {
12074 switch (Opc) {
12075 case X86::ATOMAND8: return X86::AND8rr;
12076 case X86::ATOMAND16: return X86::AND16rr;
12077 case X86::ATOMAND32: return X86::AND32rr;
12078 case X86::ATOMAND64: return X86::AND64rr;
12079 case X86::ATOMOR8: return X86::OR8rr;
12080 case X86::ATOMOR16: return X86::OR16rr;
12081 case X86::ATOMOR32: return X86::OR32rr;
12082 case X86::ATOMOR64: return X86::OR64rr;
12083 case X86::ATOMXOR8: return X86::XOR8rr;
12084 case X86::ATOMXOR16: return X86::XOR16rr;
12085 case X86::ATOMXOR32: return X86::XOR32rr;
12086 case X86::ATOMXOR64: return X86::XOR64rr;
12087 }
12088 llvm_unreachable("Unhandled atomic-load-op opcode!");
12089}
12090
12091// Get opcode of the non-atomic one from the specified atomic instruction with
12092// extra opcode.
12093static unsigned getNonAtomicOpcodeWithExtraOpc(unsigned Opc,
12094 unsigned &ExtraOpc) {
12095 switch (Opc) {
12096 case X86::ATOMNAND8: ExtraOpc = X86::NOT8r; return X86::AND8rr;
12097 case X86::ATOMNAND16: ExtraOpc = X86::NOT16r; return X86::AND16rr;
12098 case X86::ATOMNAND32: ExtraOpc = X86::NOT32r; return X86::AND32rr;
12099 case X86::ATOMNAND64: ExtraOpc = X86::NOT64r; return X86::AND64rr;
Michael Liaofe87c302012-09-21 03:18:52 +000012100 case X86::ATOMMAX8: ExtraOpc = X86::CMP8rr; return X86::CMOVL32rr;
Michael Liaob118a072012-09-20 03:06:15 +000012101 case X86::ATOMMAX16: ExtraOpc = X86::CMP16rr; return X86::CMOVL16rr;
12102 case X86::ATOMMAX32: ExtraOpc = X86::CMP32rr; return X86::CMOVL32rr;
12103 case X86::ATOMMAX64: ExtraOpc = X86::CMP64rr; return X86::CMOVL64rr;
Michael Liaofe87c302012-09-21 03:18:52 +000012104 case X86::ATOMMIN8: ExtraOpc = X86::CMP8rr; return X86::CMOVG32rr;
Michael Liaob118a072012-09-20 03:06:15 +000012105 case X86::ATOMMIN16: ExtraOpc = X86::CMP16rr; return X86::CMOVG16rr;
12106 case X86::ATOMMIN32: ExtraOpc = X86::CMP32rr; return X86::CMOVG32rr;
12107 case X86::ATOMMIN64: ExtraOpc = X86::CMP64rr; return X86::CMOVG64rr;
Michael Liaofe87c302012-09-21 03:18:52 +000012108 case X86::ATOMUMAX8: ExtraOpc = X86::CMP8rr; return X86::CMOVB32rr;
Michael Liaob118a072012-09-20 03:06:15 +000012109 case X86::ATOMUMAX16: ExtraOpc = X86::CMP16rr; return X86::CMOVB16rr;
12110 case X86::ATOMUMAX32: ExtraOpc = X86::CMP32rr; return X86::CMOVB32rr;
12111 case X86::ATOMUMAX64: ExtraOpc = X86::CMP64rr; return X86::CMOVB64rr;
Michael Liaofe87c302012-09-21 03:18:52 +000012112 case X86::ATOMUMIN8: ExtraOpc = X86::CMP8rr; return X86::CMOVA32rr;
Michael Liaob118a072012-09-20 03:06:15 +000012113 case X86::ATOMUMIN16: ExtraOpc = X86::CMP16rr; return X86::CMOVA16rr;
12114 case X86::ATOMUMIN32: ExtraOpc = X86::CMP32rr; return X86::CMOVA32rr;
12115 case X86::ATOMUMIN64: ExtraOpc = X86::CMP64rr; return X86::CMOVA64rr;
12116 }
12117 llvm_unreachable("Unhandled atomic-load-op opcode!");
12118}
12119
12120// Get opcode of the non-atomic one from the specified atomic instruction for
12121// 64-bit data type on 32-bit target.
12122static unsigned getNonAtomic6432Opcode(unsigned Opc, unsigned &HiOpc) {
12123 switch (Opc) {
12124 case X86::ATOMAND6432: HiOpc = X86::AND32rr; return X86::AND32rr;
12125 case X86::ATOMOR6432: HiOpc = X86::OR32rr; return X86::OR32rr;
12126 case X86::ATOMXOR6432: HiOpc = X86::XOR32rr; return X86::XOR32rr;
12127 case X86::ATOMADD6432: HiOpc = X86::ADC32rr; return X86::ADD32rr;
12128 case X86::ATOMSUB6432: HiOpc = X86::SBB32rr; return X86::SUB32rr;
12129 case X86::ATOMSWAP6432: HiOpc = X86::MOV32rr; return X86::MOV32rr;
Michael Liaoe5e8f762012-09-25 18:08:13 +000012130 case X86::ATOMMAX6432: HiOpc = X86::SETLr; return X86::SETLr;
12131 case X86::ATOMMIN6432: HiOpc = X86::SETGr; return X86::SETGr;
12132 case X86::ATOMUMAX6432: HiOpc = X86::SETBr; return X86::SETBr;
12133 case X86::ATOMUMIN6432: HiOpc = X86::SETAr; return X86::SETAr;
Michael Liaob118a072012-09-20 03:06:15 +000012134 }
12135 llvm_unreachable("Unhandled atomic-load-op opcode!");
12136}
12137
12138// Get opcode of the non-atomic one from the specified atomic instruction for
12139// 64-bit data type on 32-bit target with extra opcode.
12140static unsigned getNonAtomic6432OpcodeWithExtraOpc(unsigned Opc,
12141 unsigned &HiOpc,
12142 unsigned &ExtraOpc) {
12143 switch (Opc) {
12144 case X86::ATOMNAND6432:
12145 ExtraOpc = X86::NOT32r;
12146 HiOpc = X86::AND32rr;
12147 return X86::AND32rr;
12148 }
12149 llvm_unreachable("Unhandled atomic-load-op opcode!");
12150}
12151
12152// Get pseudo CMOV opcode from the specified data type.
12153static unsigned getPseudoCMOVOpc(EVT VT) {
12154 switch (VT.getSimpleVT().SimpleTy) {
Michael Liaofe87c302012-09-21 03:18:52 +000012155 case MVT::i8: return X86::CMOV_GR8;
Michael Liaob118a072012-09-20 03:06:15 +000012156 case MVT::i16: return X86::CMOV_GR16;
12157 case MVT::i32: return X86::CMOV_GR32;
12158 default:
12159 break;
12160 }
12161 llvm_unreachable("Unknown CMOV opcode!");
12162}
12163
12164// EmitAtomicLoadArith - emit the code sequence for pseudo atomic instructions.
12165// They will be translated into a spin-loop or compare-exchange loop from
12166//
12167// ...
12168// dst = atomic-fetch-op MI.addr, MI.val
12169// ...
12170//
12171// to
12172//
12173// ...
12174// EAX = LOAD MI.addr
12175// loop:
12176// t1 = OP MI.val, EAX
12177// LCMPXCHG [MI.addr], t1, [EAX is implicitly used & defined]
12178// JNE loop
12179// sink:
12180// dst = EAX
12181// ...
Mon P Wang63307c32008-05-05 19:05:59 +000012182MachineBasicBlock *
Michael Liaob118a072012-09-20 03:06:15 +000012183X86TargetLowering::EmitAtomicLoadArith(MachineInstr *MI,
12184 MachineBasicBlock *MBB) const {
12185 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
12186 DebugLoc DL = MI->getDebugLoc();
12187
12188 MachineFunction *MF = MBB->getParent();
12189 MachineRegisterInfo &MRI = MF->getRegInfo();
12190
12191 const BasicBlock *BB = MBB->getBasicBlock();
12192 MachineFunction::iterator I = MBB;
12193 ++I;
12194
12195 assert(MI->getNumOperands() <= X86::AddrNumOperands + 2 &&
12196 "Unexpected number of operands");
12197
12198 assert(MI->hasOneMemOperand() &&
12199 "Expected atomic-load-op to have one memoperand");
12200
12201 // Memory Reference
12202 MachineInstr::mmo_iterator MMOBegin = MI->memoperands_begin();
12203 MachineInstr::mmo_iterator MMOEnd = MI->memoperands_end();
12204
12205 unsigned DstReg, SrcReg;
12206 unsigned MemOpndSlot;
12207
12208 unsigned CurOp = 0;
12209
12210 DstReg = MI->getOperand(CurOp++).getReg();
12211 MemOpndSlot = CurOp;
12212 CurOp += X86::AddrNumOperands;
12213 SrcReg = MI->getOperand(CurOp++).getReg();
12214
12215 const TargetRegisterClass *RC = MRI.getRegClass(DstReg);
Craig Topperf4d25a22012-09-30 19:49:56 +000012216 MVT::SimpleValueType VT = *RC->vt_begin();
Michael Liaob118a072012-09-20 03:06:15 +000012217 unsigned AccPhyReg = getX86SubSuperRegister(X86::EAX, VT);
12218
12219 unsigned LCMPXCHGOpc = getCmpXChgOpcode(VT);
12220 unsigned LOADOpc = getLoadOpcode(VT);
12221
12222 // For the atomic load-arith operator, we generate
12223 //
12224 // thisMBB:
12225 // EAX = LOAD [MI.addr]
12226 // mainMBB:
12227 // t1 = OP MI.val, EAX
12228 // LCMPXCHG [MI.addr], t1, [EAX is implicitly used & defined]
12229 // JNE mainMBB
12230 // sinkMBB:
12231
12232 MachineBasicBlock *thisMBB = MBB;
12233 MachineBasicBlock *mainMBB = MF->CreateMachineBasicBlock(BB);
12234 MachineBasicBlock *sinkMBB = MF->CreateMachineBasicBlock(BB);
12235 MF->insert(I, mainMBB);
12236 MF->insert(I, sinkMBB);
12237
12238 MachineInstrBuilder MIB;
12239
12240 // Transfer the remainder of BB and its successor edges to sinkMBB.
12241 sinkMBB->splice(sinkMBB->begin(), MBB,
12242 llvm::next(MachineBasicBlock::iterator(MI)), MBB->end());
12243 sinkMBB->transferSuccessorsAndUpdatePHIs(MBB);
12244
12245 // thisMBB:
12246 MIB = BuildMI(thisMBB, DL, TII->get(LOADOpc), AccPhyReg);
12247 for (unsigned i = 0; i < X86::AddrNumOperands; ++i)
12248 MIB.addOperand(MI->getOperand(MemOpndSlot + i));
12249 MIB.setMemRefs(MMOBegin, MMOEnd);
12250
12251 thisMBB->addSuccessor(mainMBB);
12252
12253 // mainMBB:
12254 MachineBasicBlock *origMainMBB = mainMBB;
12255 mainMBB->addLiveIn(AccPhyReg);
12256
12257 // Copy AccPhyReg as it is used more than once.
12258 unsigned AccReg = MRI.createVirtualRegister(RC);
12259 BuildMI(mainMBB, DL, TII->get(TargetOpcode::COPY), AccReg)
12260 .addReg(AccPhyReg);
12261
12262 unsigned t1 = MRI.createVirtualRegister(RC);
12263 unsigned Opc = MI->getOpcode();
12264 switch (Opc) {
12265 default:
12266 llvm_unreachable("Unhandled atomic-load-op opcode!");
12267 case X86::ATOMAND8:
12268 case X86::ATOMAND16:
12269 case X86::ATOMAND32:
12270 case X86::ATOMAND64:
12271 case X86::ATOMOR8:
12272 case X86::ATOMOR16:
12273 case X86::ATOMOR32:
12274 case X86::ATOMOR64:
12275 case X86::ATOMXOR8:
12276 case X86::ATOMXOR16:
12277 case X86::ATOMXOR32:
12278 case X86::ATOMXOR64: {
12279 unsigned ARITHOpc = getNonAtomicOpcode(Opc);
12280 BuildMI(mainMBB, DL, TII->get(ARITHOpc), t1).addReg(SrcReg)
12281 .addReg(AccReg);
12282 break;
12283 }
12284 case X86::ATOMNAND8:
12285 case X86::ATOMNAND16:
12286 case X86::ATOMNAND32:
12287 case X86::ATOMNAND64: {
12288 unsigned t2 = MRI.createVirtualRegister(RC);
12289 unsigned NOTOpc;
12290 unsigned ANDOpc = getNonAtomicOpcodeWithExtraOpc(Opc, NOTOpc);
12291 BuildMI(mainMBB, DL, TII->get(ANDOpc), t2).addReg(SrcReg)
12292 .addReg(AccReg);
12293 BuildMI(mainMBB, DL, TII->get(NOTOpc), t1).addReg(t2);
12294 break;
12295 }
Michael Liao08382492012-09-21 03:00:17 +000012296 case X86::ATOMMAX8:
Michael Liaob118a072012-09-20 03:06:15 +000012297 case X86::ATOMMAX16:
12298 case X86::ATOMMAX32:
12299 case X86::ATOMMAX64:
Michael Liaofe87c302012-09-21 03:18:52 +000012300 case X86::ATOMMIN8:
Michael Liaob118a072012-09-20 03:06:15 +000012301 case X86::ATOMMIN16:
12302 case X86::ATOMMIN32:
12303 case X86::ATOMMIN64:
Michael Liaofe87c302012-09-21 03:18:52 +000012304 case X86::ATOMUMAX8:
Michael Liaob118a072012-09-20 03:06:15 +000012305 case X86::ATOMUMAX16:
12306 case X86::ATOMUMAX32:
12307 case X86::ATOMUMAX64:
Michael Liaofe87c302012-09-21 03:18:52 +000012308 case X86::ATOMUMIN8:
Michael Liaob118a072012-09-20 03:06:15 +000012309 case X86::ATOMUMIN16:
12310 case X86::ATOMUMIN32:
12311 case X86::ATOMUMIN64: {
12312 unsigned CMPOpc;
12313 unsigned CMOVOpc = getNonAtomicOpcodeWithExtraOpc(Opc, CMPOpc);
12314
12315 BuildMI(mainMBB, DL, TII->get(CMPOpc))
12316 .addReg(SrcReg)
12317 .addReg(AccReg);
12318
12319 if (Subtarget->hasCMov()) {
Michael Liaofe87c302012-09-21 03:18:52 +000012320 if (VT != MVT::i8) {
12321 // Native support
12322 BuildMI(mainMBB, DL, TII->get(CMOVOpc), t1)
12323 .addReg(SrcReg)
12324 .addReg(AccReg);
12325 } else {
12326 // Promote i8 to i32 to use CMOV32
12327 const TargetRegisterClass *RC32 = getRegClassFor(MVT::i32);
12328 unsigned SrcReg32 = MRI.createVirtualRegister(RC32);
12329 unsigned AccReg32 = MRI.createVirtualRegister(RC32);
12330 unsigned t2 = MRI.createVirtualRegister(RC32);
12331
12332 unsigned Undef = MRI.createVirtualRegister(RC32);
12333 BuildMI(mainMBB, DL, TII->get(TargetOpcode::IMPLICIT_DEF), Undef);
12334
12335 BuildMI(mainMBB, DL, TII->get(TargetOpcode::INSERT_SUBREG), SrcReg32)
12336 .addReg(Undef)
12337 .addReg(SrcReg)
12338 .addImm(X86::sub_8bit);
12339 BuildMI(mainMBB, DL, TII->get(TargetOpcode::INSERT_SUBREG), AccReg32)
12340 .addReg(Undef)
12341 .addReg(AccReg)
12342 .addImm(X86::sub_8bit);
12343
12344 BuildMI(mainMBB, DL, TII->get(CMOVOpc), t2)
12345 .addReg(SrcReg32)
12346 .addReg(AccReg32);
12347
12348 BuildMI(mainMBB, DL, TII->get(TargetOpcode::COPY), t1)
12349 .addReg(t2, 0, X86::sub_8bit);
12350 }
Michael Liaob118a072012-09-20 03:06:15 +000012351 } else {
12352 // Use pseudo select and lower them.
Michael Liaofe87c302012-09-21 03:18:52 +000012353 assert((VT == MVT::i8 || VT == MVT::i16 || VT == MVT::i32) &&
Michael Liaob118a072012-09-20 03:06:15 +000012354 "Invalid atomic-load-op transformation!");
12355 unsigned SelOpc = getPseudoCMOVOpc(VT);
12356 X86::CondCode CC = X86::getCondFromCMovOpc(CMOVOpc);
12357 assert(CC != X86::COND_INVALID && "Invalid atomic-load-op transformation!");
12358 MIB = BuildMI(mainMBB, DL, TII->get(SelOpc), t1)
12359 .addReg(SrcReg).addReg(AccReg)
12360 .addImm(CC);
12361 mainMBB = EmitLoweredSelect(MIB, mainMBB);
12362 }
12363 break;
12364 }
12365 }
12366
12367 // Copy AccPhyReg back from virtual register.
12368 BuildMI(mainMBB, DL, TII->get(TargetOpcode::COPY), AccPhyReg)
12369 .addReg(AccReg);
12370
12371 MIB = BuildMI(mainMBB, DL, TII->get(LCMPXCHGOpc));
12372 for (unsigned i = 0; i < X86::AddrNumOperands; ++i)
12373 MIB.addOperand(MI->getOperand(MemOpndSlot + i));
12374 MIB.addReg(t1);
12375 MIB.setMemRefs(MMOBegin, MMOEnd);
12376
12377 BuildMI(mainMBB, DL, TII->get(X86::JNE_4)).addMBB(origMainMBB);
12378
12379 mainMBB->addSuccessor(origMainMBB);
12380 mainMBB->addSuccessor(sinkMBB);
12381
12382 // sinkMBB:
12383 sinkMBB->addLiveIn(AccPhyReg);
12384
12385 BuildMI(*sinkMBB, sinkMBB->begin(), DL,
12386 TII->get(TargetOpcode::COPY), DstReg)
12387 .addReg(AccPhyReg);
12388
12389 MI->eraseFromParent();
12390 return sinkMBB;
12391}
12392
12393// EmitAtomicLoadArith6432 - emit the code sequence for pseudo atomic
12394// instructions. They will be translated into a spin-loop or compare-exchange
12395// loop from
12396//
12397// ...
12398// dst = atomic-fetch-op MI.addr, MI.val
12399// ...
12400//
12401// to
12402//
12403// ...
12404// EAX = LOAD [MI.addr + 0]
12405// EDX = LOAD [MI.addr + 4]
12406// loop:
12407// EBX = OP MI.val.lo, EAX
12408// ECX = OP MI.val.hi, EDX
12409// LCMPXCHG8B [MI.addr], [ECX:EBX & EDX:EAX are implicitly used and EDX:EAX is implicitly defined]
12410// JNE loop
12411// sink:
12412// dst = EDX:EAX
12413// ...
12414MachineBasicBlock *
12415X86TargetLowering::EmitAtomicLoadArith6432(MachineInstr *MI,
12416 MachineBasicBlock *MBB) const {
12417 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
12418 DebugLoc DL = MI->getDebugLoc();
12419
12420 MachineFunction *MF = MBB->getParent();
12421 MachineRegisterInfo &MRI = MF->getRegInfo();
12422
12423 const BasicBlock *BB = MBB->getBasicBlock();
12424 MachineFunction::iterator I = MBB;
12425 ++I;
12426
12427 assert(MI->getNumOperands() <= X86::AddrNumOperands + 4 &&
12428 "Unexpected number of operands");
12429
12430 assert(MI->hasOneMemOperand() &&
12431 "Expected atomic-load-op32 to have one memoperand");
12432
12433 // Memory Reference
12434 MachineInstr::mmo_iterator MMOBegin = MI->memoperands_begin();
12435 MachineInstr::mmo_iterator MMOEnd = MI->memoperands_end();
12436
12437 unsigned DstLoReg, DstHiReg;
12438 unsigned SrcLoReg, SrcHiReg;
12439 unsigned MemOpndSlot;
12440
12441 unsigned CurOp = 0;
12442
12443 DstLoReg = MI->getOperand(CurOp++).getReg();
12444 DstHiReg = MI->getOperand(CurOp++).getReg();
12445 MemOpndSlot = CurOp;
12446 CurOp += X86::AddrNumOperands;
12447 SrcLoReg = MI->getOperand(CurOp++).getReg();
12448 SrcHiReg = MI->getOperand(CurOp++).getReg();
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012449
Craig Topperc9099502012-04-20 06:31:50 +000012450 const TargetRegisterClass *RC = &X86::GR32RegClass;
Michael Liaoe5e8f762012-09-25 18:08:13 +000012451 const TargetRegisterClass *RC8 = &X86::GR8RegClass;
Scott Michelfdc40a02009-02-17 22:15:04 +000012452
Michael Liaob118a072012-09-20 03:06:15 +000012453 unsigned LCMPXCHGOpc = X86::LCMPXCHG8B;
12454 unsigned LOADOpc = X86::MOV32rm;
Scott Michelfdc40a02009-02-17 22:15:04 +000012455
Michael Liaob118a072012-09-20 03:06:15 +000012456 // For the atomic load-arith operator, we generate
Mon P Wang63307c32008-05-05 19:05:59 +000012457 //
Michael Liaob118a072012-09-20 03:06:15 +000012458 // thisMBB:
12459 // EAX = LOAD [MI.addr + 0]
12460 // EDX = LOAD [MI.addr + 4]
12461 // mainMBB:
12462 // EBX = OP MI.vallo, EAX
12463 // ECX = OP MI.valhi, EDX
12464 // LCMPXCHG8B [MI.addr], [ECX:EBX & EDX:EAX are implicitly used and EDX:EAX is implicitly defined]
12465 // JNE mainMBB
12466 // sinkMBB:
Scott Michelfdc40a02009-02-17 22:15:04 +000012467
Mon P Wang63307c32008-05-05 19:05:59 +000012468 MachineBasicBlock *thisMBB = MBB;
Michael Liaob118a072012-09-20 03:06:15 +000012469 MachineBasicBlock *mainMBB = MF->CreateMachineBasicBlock(BB);
12470 MachineBasicBlock *sinkMBB = MF->CreateMachineBasicBlock(BB);
12471 MF->insert(I, mainMBB);
12472 MF->insert(I, sinkMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000012473
Michael Liaob118a072012-09-20 03:06:15 +000012474 MachineInstrBuilder MIB;
Scott Michelfdc40a02009-02-17 22:15:04 +000012475
Michael Liaob118a072012-09-20 03:06:15 +000012476 // Transfer the remainder of BB and its successor edges to sinkMBB.
12477 sinkMBB->splice(sinkMBB->begin(), MBB,
12478 llvm::next(MachineBasicBlock::iterator(MI)), MBB->end());
12479 sinkMBB->transferSuccessorsAndUpdatePHIs(MBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000012480
Michael Liaob118a072012-09-20 03:06:15 +000012481 // thisMBB:
12482 // Lo
12483 MIB = BuildMI(thisMBB, DL, TII->get(LOADOpc), X86::EAX);
12484 for (unsigned i = 0; i < X86::AddrNumOperands; ++i)
12485 MIB.addOperand(MI->getOperand(MemOpndSlot + i));
12486 MIB.setMemRefs(MMOBegin, MMOEnd);
12487 // Hi
12488 MIB = BuildMI(thisMBB, DL, TII->get(LOADOpc), X86::EDX);
12489 for (unsigned i = 0; i < X86::AddrNumOperands; ++i) {
Evan Chenga395f4d2012-10-11 00:15:48 +000012490 if (i == X86::AddrDisp)
Michael Liaob118a072012-09-20 03:06:15 +000012491 MIB.addDisp(MI->getOperand(MemOpndSlot + i), 4); // 4 == sizeof(i32)
Evan Chenga395f4d2012-10-11 00:15:48 +000012492 else
Michael Liaob118a072012-09-20 03:06:15 +000012493 MIB.addOperand(MI->getOperand(MemOpndSlot + i));
12494 }
12495 MIB.setMemRefs(MMOBegin, MMOEnd);
Scott Michelfdc40a02009-02-17 22:15:04 +000012496
Michael Liaob118a072012-09-20 03:06:15 +000012497 thisMBB->addSuccessor(mainMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000012498
Michael Liaob118a072012-09-20 03:06:15 +000012499 // mainMBB:
12500 MachineBasicBlock *origMainMBB = mainMBB;
12501 mainMBB->addLiveIn(X86::EAX);
12502 mainMBB->addLiveIn(X86::EDX);
Scott Michelfdc40a02009-02-17 22:15:04 +000012503
Michael Liaob118a072012-09-20 03:06:15 +000012504 // Copy EDX:EAX as they are used more than once.
12505 unsigned LoReg = MRI.createVirtualRegister(RC);
12506 unsigned HiReg = MRI.createVirtualRegister(RC);
12507 BuildMI(mainMBB, DL, TII->get(TargetOpcode::COPY), LoReg).addReg(X86::EAX);
12508 BuildMI(mainMBB, DL, TII->get(TargetOpcode::COPY), HiReg).addReg(X86::EDX);
Mon P Wangab3e7472008-05-05 22:56:23 +000012509
Michael Liaob118a072012-09-20 03:06:15 +000012510 unsigned t1L = MRI.createVirtualRegister(RC);
12511 unsigned t1H = MRI.createVirtualRegister(RC);
Scott Michelfdc40a02009-02-17 22:15:04 +000012512
Michael Liaob118a072012-09-20 03:06:15 +000012513 unsigned Opc = MI->getOpcode();
12514 switch (Opc) {
12515 default:
12516 llvm_unreachable("Unhandled atomic-load-op6432 opcode!");
12517 case X86::ATOMAND6432:
12518 case X86::ATOMOR6432:
12519 case X86::ATOMXOR6432:
12520 case X86::ATOMADD6432:
12521 case X86::ATOMSUB6432: {
12522 unsigned HiOpc;
12523 unsigned LoOpc = getNonAtomic6432Opcode(Opc, HiOpc);
12524 BuildMI(mainMBB, DL, TII->get(LoOpc), t1L).addReg(SrcLoReg).addReg(LoReg);
12525 BuildMI(mainMBB, DL, TII->get(HiOpc), t1H).addReg(SrcHiReg).addReg(HiReg);
12526 break;
12527 }
12528 case X86::ATOMNAND6432: {
12529 unsigned HiOpc, NOTOpc;
12530 unsigned LoOpc = getNonAtomic6432OpcodeWithExtraOpc(Opc, HiOpc, NOTOpc);
12531 unsigned t2L = MRI.createVirtualRegister(RC);
12532 unsigned t2H = MRI.createVirtualRegister(RC);
12533 BuildMI(mainMBB, DL, TII->get(LoOpc), t2L).addReg(SrcLoReg).addReg(LoReg);
12534 BuildMI(mainMBB, DL, TII->get(HiOpc), t2H).addReg(SrcHiReg).addReg(HiReg);
12535 BuildMI(mainMBB, DL, TII->get(NOTOpc), t1L).addReg(t2L);
12536 BuildMI(mainMBB, DL, TII->get(NOTOpc), t1H).addReg(t2H);
12537 break;
12538 }
Michael Liaoe5e8f762012-09-25 18:08:13 +000012539 case X86::ATOMMAX6432:
12540 case X86::ATOMMIN6432:
12541 case X86::ATOMUMAX6432:
12542 case X86::ATOMUMIN6432: {
12543 unsigned HiOpc;
12544 unsigned LoOpc = getNonAtomic6432Opcode(Opc, HiOpc);
12545 unsigned cL = MRI.createVirtualRegister(RC8);
12546 unsigned cH = MRI.createVirtualRegister(RC8);
12547 unsigned cL32 = MRI.createVirtualRegister(RC);
12548 unsigned cH32 = MRI.createVirtualRegister(RC);
12549 unsigned cc = MRI.createVirtualRegister(RC);
12550 // cl := cmp src_lo, lo
12551 BuildMI(mainMBB, DL, TII->get(X86::CMP32rr))
12552 .addReg(SrcLoReg).addReg(LoReg);
12553 BuildMI(mainMBB, DL, TII->get(LoOpc), cL);
12554 BuildMI(mainMBB, DL, TII->get(X86::MOVZX32rr8), cL32).addReg(cL);
12555 // ch := cmp src_hi, hi
12556 BuildMI(mainMBB, DL, TII->get(X86::CMP32rr))
12557 .addReg(SrcHiReg).addReg(HiReg);
12558 BuildMI(mainMBB, DL, TII->get(HiOpc), cH);
12559 BuildMI(mainMBB, DL, TII->get(X86::MOVZX32rr8), cH32).addReg(cH);
12560 // cc := if (src_hi == hi) ? cl : ch;
12561 if (Subtarget->hasCMov()) {
12562 BuildMI(mainMBB, DL, TII->get(X86::CMOVE32rr), cc)
12563 .addReg(cH32).addReg(cL32);
12564 } else {
12565 MIB = BuildMI(mainMBB, DL, TII->get(X86::CMOV_GR32), cc)
12566 .addReg(cH32).addReg(cL32)
12567 .addImm(X86::COND_E);
12568 mainMBB = EmitLoweredSelect(MIB, mainMBB);
12569 }
12570 BuildMI(mainMBB, DL, TII->get(X86::TEST32rr)).addReg(cc).addReg(cc);
12571 if (Subtarget->hasCMov()) {
12572 BuildMI(mainMBB, DL, TII->get(X86::CMOVNE32rr), t1L)
12573 .addReg(SrcLoReg).addReg(LoReg);
12574 BuildMI(mainMBB, DL, TII->get(X86::CMOVNE32rr), t1H)
12575 .addReg(SrcHiReg).addReg(HiReg);
12576 } else {
12577 MIB = BuildMI(mainMBB, DL, TII->get(X86::CMOV_GR32), t1L)
12578 .addReg(SrcLoReg).addReg(LoReg)
12579 .addImm(X86::COND_NE);
12580 mainMBB = EmitLoweredSelect(MIB, mainMBB);
12581 MIB = BuildMI(mainMBB, DL, TII->get(X86::CMOV_GR32), t1H)
12582 .addReg(SrcHiReg).addReg(HiReg)
12583 .addImm(X86::COND_NE);
12584 mainMBB = EmitLoweredSelect(MIB, mainMBB);
12585 }
12586 break;
12587 }
Michael Liaob118a072012-09-20 03:06:15 +000012588 case X86::ATOMSWAP6432: {
12589 unsigned HiOpc;
12590 unsigned LoOpc = getNonAtomic6432Opcode(Opc, HiOpc);
12591 BuildMI(mainMBB, DL, TII->get(LoOpc), t1L).addReg(SrcLoReg);
12592 BuildMI(mainMBB, DL, TII->get(HiOpc), t1H).addReg(SrcHiReg);
12593 break;
12594 }
12595 }
Mon P Wang63307c32008-05-05 19:05:59 +000012596
Michael Liaob118a072012-09-20 03:06:15 +000012597 // Copy EDX:EAX back from HiReg:LoReg
12598 BuildMI(mainMBB, DL, TII->get(TargetOpcode::COPY), X86::EAX).addReg(LoReg);
12599 BuildMI(mainMBB, DL, TII->get(TargetOpcode::COPY), X86::EDX).addReg(HiReg);
12600 // Copy ECX:EBX from t1H:t1L
12601 BuildMI(mainMBB, DL, TII->get(TargetOpcode::COPY), X86::EBX).addReg(t1L);
12602 BuildMI(mainMBB, DL, TII->get(TargetOpcode::COPY), X86::ECX).addReg(t1H);
Mon P Wangab3e7472008-05-05 22:56:23 +000012603
Michael Liaob118a072012-09-20 03:06:15 +000012604 MIB = BuildMI(mainMBB, DL, TII->get(LCMPXCHGOpc));
12605 for (unsigned i = 0; i < X86::AddrNumOperands; ++i)
12606 MIB.addOperand(MI->getOperand(MemOpndSlot + i));
12607 MIB.setMemRefs(MMOBegin, MMOEnd);
Mon P Wang63307c32008-05-05 19:05:59 +000012608
Michael Liaob118a072012-09-20 03:06:15 +000012609 BuildMI(mainMBB, DL, TII->get(X86::JNE_4)).addMBB(origMainMBB);
Mon P Wang63307c32008-05-05 19:05:59 +000012610
Michael Liaob118a072012-09-20 03:06:15 +000012611 mainMBB->addSuccessor(origMainMBB);
12612 mainMBB->addSuccessor(sinkMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000012613
Michael Liaob118a072012-09-20 03:06:15 +000012614 // sinkMBB:
12615 sinkMBB->addLiveIn(X86::EAX);
12616 sinkMBB->addLiveIn(X86::EDX);
Scott Michelfdc40a02009-02-17 22:15:04 +000012617
Michael Liaob118a072012-09-20 03:06:15 +000012618 BuildMI(*sinkMBB, sinkMBB->begin(), DL,
12619 TII->get(TargetOpcode::COPY), DstLoReg)
12620 .addReg(X86::EAX);
12621 BuildMI(*sinkMBB, sinkMBB->begin(), DL,
12622 TII->get(TargetOpcode::COPY), DstHiReg)
12623 .addReg(X86::EDX);
Mon P Wang63307c32008-05-05 19:05:59 +000012624
Michael Liaob118a072012-09-20 03:06:15 +000012625 MI->eraseFromParent();
12626 return sinkMBB;
Mon P Wang63307c32008-05-05 19:05:59 +000012627}
12628
Eric Christopherf83a5de2009-08-27 18:08:16 +000012629// FIXME: When we get size specific XMM0 registers, i.e. XMM0_V16I8
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000012630// or XMM0_V32I8 in AVX all of this code can be replaced with that
12631// in the .td file.
Dan Gohmand6708ea2009-08-15 01:38:56 +000012632MachineBasicBlock *
Eric Christopherb120ab42009-08-18 22:50:32 +000012633X86TargetLowering::EmitPCMP(MachineInstr *MI, MachineBasicBlock *BB,
Daniel Dunbara279bc32009-09-20 02:20:51 +000012634 unsigned numArgs, bool memArg) const {
Craig Topperd0a31172012-01-10 06:37:29 +000012635 assert(Subtarget->hasSSE42() &&
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000012636 "Target must have SSE4.2 or AVX features enabled");
12637
Eric Christopherb120ab42009-08-18 22:50:32 +000012638 DebugLoc dl = MI->getDebugLoc();
12639 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Eric Christopherb120ab42009-08-18 22:50:32 +000012640 unsigned Opc;
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000012641 if (!Subtarget->hasAVX()) {
12642 if (memArg)
12643 Opc = numArgs == 3 ? X86::PCMPISTRM128rm : X86::PCMPESTRM128rm;
12644 else
12645 Opc = numArgs == 3 ? X86::PCMPISTRM128rr : X86::PCMPESTRM128rr;
12646 } else {
12647 if (memArg)
12648 Opc = numArgs == 3 ? X86::VPCMPISTRM128rm : X86::VPCMPESTRM128rm;
12649 else
12650 Opc = numArgs == 3 ? X86::VPCMPISTRM128rr : X86::VPCMPESTRM128rr;
12651 }
Eric Christopherb120ab42009-08-18 22:50:32 +000012652
Eric Christopher41c902f2010-11-30 08:20:21 +000012653 MachineInstrBuilder MIB = BuildMI(*BB, MI, dl, TII->get(Opc));
Eric Christopherb120ab42009-08-18 22:50:32 +000012654 for (unsigned i = 0; i < numArgs; ++i) {
12655 MachineOperand &Op = MI->getOperand(i+1);
Eric Christopherb120ab42009-08-18 22:50:32 +000012656 if (!(Op.isReg() && Op.isImplicit()))
12657 MIB.addOperand(Op);
12658 }
Bruno Cardoso Lopes5affa512011-08-31 03:04:09 +000012659 BuildMI(*BB, MI, dl,
Craig Topper638aa682012-08-05 00:17:48 +000012660 TII->get(TargetOpcode::COPY), MI->getOperand(0).getReg())
Eric Christopherb120ab42009-08-18 22:50:32 +000012661 .addReg(X86::XMM0);
12662
Dan Gohman14152b42010-07-06 20:24:04 +000012663 MI->eraseFromParent();
Eric Christopherb120ab42009-08-18 22:50:32 +000012664 return BB;
12665}
12666
12667MachineBasicBlock *
Eric Christopher228232b2010-11-30 07:20:12 +000012668X86TargetLowering::EmitMonitor(MachineInstr *MI, MachineBasicBlock *BB) const {
Eric Christopher228232b2010-11-30 07:20:12 +000012669 DebugLoc dl = MI->getDebugLoc();
12670 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000012671
Eric Christopher228232b2010-11-30 07:20:12 +000012672 // Address into RAX/EAX, other two args into ECX, EDX.
12673 unsigned MemOpc = Subtarget->is64Bit() ? X86::LEA64r : X86::LEA32r;
12674 unsigned MemReg = Subtarget->is64Bit() ? X86::RAX : X86::EAX;
12675 MachineInstrBuilder MIB = BuildMI(*BB, MI, dl, TII->get(MemOpc), MemReg);
12676 for (int i = 0; i < X86::AddrNumOperands; ++i)
Eric Christopher82be2202010-11-30 08:10:28 +000012677 MIB.addOperand(MI->getOperand(i));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000012678
Eric Christopher228232b2010-11-30 07:20:12 +000012679 unsigned ValOps = X86::AddrNumOperands;
12680 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::ECX)
12681 .addReg(MI->getOperand(ValOps).getReg());
12682 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::EDX)
12683 .addReg(MI->getOperand(ValOps+1).getReg());
12684
12685 // The instruction doesn't actually take any operands though.
12686 BuildMI(*BB, MI, dl, TII->get(X86::MONITORrrr));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000012687
Eric Christopher228232b2010-11-30 07:20:12 +000012688 MI->eraseFromParent(); // The pseudo is gone now.
12689 return BB;
12690}
12691
12692MachineBasicBlock *
Dan Gohman320afb82010-10-12 18:00:49 +000012693X86TargetLowering::EmitVAARG64WithCustomInserter(
12694 MachineInstr *MI,
12695 MachineBasicBlock *MBB) const {
12696 // Emit va_arg instruction on X86-64.
12697
12698 // Operands to this pseudo-instruction:
12699 // 0 ) Output : destination address (reg)
12700 // 1-5) Input : va_list address (addr, i64mem)
12701 // 6 ) ArgSize : Size (in bytes) of vararg type
12702 // 7 ) ArgMode : 0=overflow only, 1=use gp_offset, 2=use fp_offset
12703 // 8 ) Align : Alignment of type
12704 // 9 ) EFLAGS (implicit-def)
12705
12706 assert(MI->getNumOperands() == 10 && "VAARG_64 should have 10 operands!");
12707 assert(X86::AddrNumOperands == 5 && "VAARG_64 assumes 5 address operands");
12708
12709 unsigned DestReg = MI->getOperand(0).getReg();
12710 MachineOperand &Base = MI->getOperand(1);
12711 MachineOperand &Scale = MI->getOperand(2);
12712 MachineOperand &Index = MI->getOperand(3);
12713 MachineOperand &Disp = MI->getOperand(4);
12714 MachineOperand &Segment = MI->getOperand(5);
12715 unsigned ArgSize = MI->getOperand(6).getImm();
12716 unsigned ArgMode = MI->getOperand(7).getImm();
12717 unsigned Align = MI->getOperand(8).getImm();
12718
12719 // Memory Reference
12720 assert(MI->hasOneMemOperand() && "Expected VAARG_64 to have one memoperand");
12721 MachineInstr::mmo_iterator MMOBegin = MI->memoperands_begin();
12722 MachineInstr::mmo_iterator MMOEnd = MI->memoperands_end();
12723
12724 // Machine Information
12725 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
12726 MachineRegisterInfo &MRI = MBB->getParent()->getRegInfo();
12727 const TargetRegisterClass *AddrRegClass = getRegClassFor(MVT::i64);
12728 const TargetRegisterClass *OffsetRegClass = getRegClassFor(MVT::i32);
12729 DebugLoc DL = MI->getDebugLoc();
12730
12731 // struct va_list {
12732 // i32 gp_offset
12733 // i32 fp_offset
12734 // i64 overflow_area (address)
12735 // i64 reg_save_area (address)
12736 // }
12737 // sizeof(va_list) = 24
12738 // alignment(va_list) = 8
12739
12740 unsigned TotalNumIntRegs = 6;
12741 unsigned TotalNumXMMRegs = 8;
12742 bool UseGPOffset = (ArgMode == 1);
12743 bool UseFPOffset = (ArgMode == 2);
12744 unsigned MaxOffset = TotalNumIntRegs * 8 +
12745 (UseFPOffset ? TotalNumXMMRegs * 16 : 0);
12746
12747 /* Align ArgSize to a multiple of 8 */
12748 unsigned ArgSizeA8 = (ArgSize + 7) & ~7;
12749 bool NeedsAlign = (Align > 8);
12750
12751 MachineBasicBlock *thisMBB = MBB;
12752 MachineBasicBlock *overflowMBB;
12753 MachineBasicBlock *offsetMBB;
12754 MachineBasicBlock *endMBB;
12755
12756 unsigned OffsetDestReg = 0; // Argument address computed by offsetMBB
12757 unsigned OverflowDestReg = 0; // Argument address computed by overflowMBB
12758 unsigned OffsetReg = 0;
12759
12760 if (!UseGPOffset && !UseFPOffset) {
12761 // If we only pull from the overflow region, we don't create a branch.
12762 // We don't need to alter control flow.
12763 OffsetDestReg = 0; // unused
12764 OverflowDestReg = DestReg;
12765
12766 offsetMBB = NULL;
12767 overflowMBB = thisMBB;
12768 endMBB = thisMBB;
12769 } else {
12770 // First emit code to check if gp_offset (or fp_offset) is below the bound.
12771 // If so, pull the argument from reg_save_area. (branch to offsetMBB)
12772 // If not, pull from overflow_area. (branch to overflowMBB)
12773 //
12774 // thisMBB
12775 // | .
12776 // | .
12777 // offsetMBB overflowMBB
12778 // | .
12779 // | .
12780 // endMBB
12781
12782 // Registers for the PHI in endMBB
12783 OffsetDestReg = MRI.createVirtualRegister(AddrRegClass);
12784 OverflowDestReg = MRI.createVirtualRegister(AddrRegClass);
12785
12786 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
12787 MachineFunction *MF = MBB->getParent();
12788 overflowMBB = MF->CreateMachineBasicBlock(LLVM_BB);
12789 offsetMBB = MF->CreateMachineBasicBlock(LLVM_BB);
12790 endMBB = MF->CreateMachineBasicBlock(LLVM_BB);
12791
12792 MachineFunction::iterator MBBIter = MBB;
12793 ++MBBIter;
12794
12795 // Insert the new basic blocks
12796 MF->insert(MBBIter, offsetMBB);
12797 MF->insert(MBBIter, overflowMBB);
12798 MF->insert(MBBIter, endMBB);
12799
12800 // Transfer the remainder of MBB and its successor edges to endMBB.
12801 endMBB->splice(endMBB->begin(), thisMBB,
12802 llvm::next(MachineBasicBlock::iterator(MI)),
12803 thisMBB->end());
12804 endMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
12805
12806 // Make offsetMBB and overflowMBB successors of thisMBB
12807 thisMBB->addSuccessor(offsetMBB);
12808 thisMBB->addSuccessor(overflowMBB);
12809
12810 // endMBB is a successor of both offsetMBB and overflowMBB
12811 offsetMBB->addSuccessor(endMBB);
12812 overflowMBB->addSuccessor(endMBB);
12813
12814 // Load the offset value into a register
12815 OffsetReg = MRI.createVirtualRegister(OffsetRegClass);
12816 BuildMI(thisMBB, DL, TII->get(X86::MOV32rm), OffsetReg)
12817 .addOperand(Base)
12818 .addOperand(Scale)
12819 .addOperand(Index)
12820 .addDisp(Disp, UseFPOffset ? 4 : 0)
12821 .addOperand(Segment)
12822 .setMemRefs(MMOBegin, MMOEnd);
12823
12824 // Check if there is enough room left to pull this argument.
12825 BuildMI(thisMBB, DL, TII->get(X86::CMP32ri))
12826 .addReg(OffsetReg)
12827 .addImm(MaxOffset + 8 - ArgSizeA8);
12828
12829 // Branch to "overflowMBB" if offset >= max
12830 // Fall through to "offsetMBB" otherwise
12831 BuildMI(thisMBB, DL, TII->get(X86::GetCondBranchFromCond(X86::COND_AE)))
12832 .addMBB(overflowMBB);
12833 }
12834
12835 // In offsetMBB, emit code to use the reg_save_area.
12836 if (offsetMBB) {
12837 assert(OffsetReg != 0);
12838
12839 // Read the reg_save_area address.
12840 unsigned RegSaveReg = MRI.createVirtualRegister(AddrRegClass);
12841 BuildMI(offsetMBB, DL, TII->get(X86::MOV64rm), RegSaveReg)
12842 .addOperand(Base)
12843 .addOperand(Scale)
12844 .addOperand(Index)
12845 .addDisp(Disp, 16)
12846 .addOperand(Segment)
12847 .setMemRefs(MMOBegin, MMOEnd);
12848
12849 // Zero-extend the offset
12850 unsigned OffsetReg64 = MRI.createVirtualRegister(AddrRegClass);
12851 BuildMI(offsetMBB, DL, TII->get(X86::SUBREG_TO_REG), OffsetReg64)
12852 .addImm(0)
12853 .addReg(OffsetReg)
12854 .addImm(X86::sub_32bit);
12855
12856 // Add the offset to the reg_save_area to get the final address.
12857 BuildMI(offsetMBB, DL, TII->get(X86::ADD64rr), OffsetDestReg)
12858 .addReg(OffsetReg64)
12859 .addReg(RegSaveReg);
12860
12861 // Compute the offset for the next argument
12862 unsigned NextOffsetReg = MRI.createVirtualRegister(OffsetRegClass);
12863 BuildMI(offsetMBB, DL, TII->get(X86::ADD32ri), NextOffsetReg)
12864 .addReg(OffsetReg)
12865 .addImm(UseFPOffset ? 16 : 8);
12866
12867 // Store it back into the va_list.
12868 BuildMI(offsetMBB, DL, TII->get(X86::MOV32mr))
12869 .addOperand(Base)
12870 .addOperand(Scale)
12871 .addOperand(Index)
12872 .addDisp(Disp, UseFPOffset ? 4 : 0)
12873 .addOperand(Segment)
12874 .addReg(NextOffsetReg)
12875 .setMemRefs(MMOBegin, MMOEnd);
12876
12877 // Jump to endMBB
12878 BuildMI(offsetMBB, DL, TII->get(X86::JMP_4))
12879 .addMBB(endMBB);
12880 }
12881
12882 //
12883 // Emit code to use overflow area
12884 //
12885
12886 // Load the overflow_area address into a register.
12887 unsigned OverflowAddrReg = MRI.createVirtualRegister(AddrRegClass);
12888 BuildMI(overflowMBB, DL, TII->get(X86::MOV64rm), OverflowAddrReg)
12889 .addOperand(Base)
12890 .addOperand(Scale)
12891 .addOperand(Index)
12892 .addDisp(Disp, 8)
12893 .addOperand(Segment)
12894 .setMemRefs(MMOBegin, MMOEnd);
12895
12896 // If we need to align it, do so. Otherwise, just copy the address
12897 // to OverflowDestReg.
12898 if (NeedsAlign) {
12899 // Align the overflow address
12900 assert((Align & (Align-1)) == 0 && "Alignment must be a power of 2");
12901 unsigned TmpReg = MRI.createVirtualRegister(AddrRegClass);
12902
12903 // aligned_addr = (addr + (align-1)) & ~(align-1)
12904 BuildMI(overflowMBB, DL, TII->get(X86::ADD64ri32), TmpReg)
12905 .addReg(OverflowAddrReg)
12906 .addImm(Align-1);
12907
12908 BuildMI(overflowMBB, DL, TII->get(X86::AND64ri32), OverflowDestReg)
12909 .addReg(TmpReg)
12910 .addImm(~(uint64_t)(Align-1));
12911 } else {
12912 BuildMI(overflowMBB, DL, TII->get(TargetOpcode::COPY), OverflowDestReg)
12913 .addReg(OverflowAddrReg);
12914 }
12915
12916 // Compute the next overflow address after this argument.
12917 // (the overflow address should be kept 8-byte aligned)
12918 unsigned NextAddrReg = MRI.createVirtualRegister(AddrRegClass);
12919 BuildMI(overflowMBB, DL, TII->get(X86::ADD64ri32), NextAddrReg)
12920 .addReg(OverflowDestReg)
12921 .addImm(ArgSizeA8);
12922
12923 // Store the new overflow address.
12924 BuildMI(overflowMBB, DL, TII->get(X86::MOV64mr))
12925 .addOperand(Base)
12926 .addOperand(Scale)
12927 .addOperand(Index)
12928 .addDisp(Disp, 8)
12929 .addOperand(Segment)
12930 .addReg(NextAddrReg)
12931 .setMemRefs(MMOBegin, MMOEnd);
12932
12933 // If we branched, emit the PHI to the front of endMBB.
12934 if (offsetMBB) {
12935 BuildMI(*endMBB, endMBB->begin(), DL,
12936 TII->get(X86::PHI), DestReg)
12937 .addReg(OffsetDestReg).addMBB(offsetMBB)
12938 .addReg(OverflowDestReg).addMBB(overflowMBB);
12939 }
12940
12941 // Erase the pseudo instruction
12942 MI->eraseFromParent();
12943
12944 return endMBB;
12945}
12946
12947MachineBasicBlock *
Dan Gohmand6708ea2009-08-15 01:38:56 +000012948X86TargetLowering::EmitVAStartSaveXMMRegsWithCustomInserter(
12949 MachineInstr *MI,
12950 MachineBasicBlock *MBB) const {
12951 // Emit code to save XMM registers to the stack. The ABI says that the
12952 // number of registers to save is given in %al, so it's theoretically
12953 // possible to do an indirect jump trick to avoid saving all of them,
12954 // however this code takes a simpler approach and just executes all
12955 // of the stores if %al is non-zero. It's less code, and it's probably
12956 // easier on the hardware branch predictor, and stores aren't all that
12957 // expensive anyway.
12958
12959 // Create the new basic blocks. One block contains all the XMM stores,
12960 // and one block is the final destination regardless of whether any
12961 // stores were performed.
12962 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
12963 MachineFunction *F = MBB->getParent();
12964 MachineFunction::iterator MBBIter = MBB;
12965 ++MBBIter;
12966 MachineBasicBlock *XMMSaveMBB = F->CreateMachineBasicBlock(LLVM_BB);
12967 MachineBasicBlock *EndMBB = F->CreateMachineBasicBlock(LLVM_BB);
12968 F->insert(MBBIter, XMMSaveMBB);
12969 F->insert(MBBIter, EndMBB);
12970
Dan Gohman14152b42010-07-06 20:24:04 +000012971 // Transfer the remainder of MBB and its successor edges to EndMBB.
12972 EndMBB->splice(EndMBB->begin(), MBB,
12973 llvm::next(MachineBasicBlock::iterator(MI)),
12974 MBB->end());
12975 EndMBB->transferSuccessorsAndUpdatePHIs(MBB);
12976
Dan Gohmand6708ea2009-08-15 01:38:56 +000012977 // The original block will now fall through to the XMM save block.
12978 MBB->addSuccessor(XMMSaveMBB);
12979 // The XMMSaveMBB will fall through to the end block.
12980 XMMSaveMBB->addSuccessor(EndMBB);
12981
12982 // Now add the instructions.
12983 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
12984 DebugLoc DL = MI->getDebugLoc();
12985
12986 unsigned CountReg = MI->getOperand(0).getReg();
12987 int64_t RegSaveFrameIndex = MI->getOperand(1).getImm();
12988 int64_t VarArgsFPOffset = MI->getOperand(2).getImm();
12989
12990 if (!Subtarget->isTargetWin64()) {
12991 // If %al is 0, branch around the XMM save block.
12992 BuildMI(MBB, DL, TII->get(X86::TEST8rr)).addReg(CountReg).addReg(CountReg);
Chris Lattnerbd13fb62010-02-11 19:25:55 +000012993 BuildMI(MBB, DL, TII->get(X86::JE_4)).addMBB(EndMBB);
Dan Gohmand6708ea2009-08-15 01:38:56 +000012994 MBB->addSuccessor(EndMBB);
12995 }
12996
Bruno Cardoso Lopes5affa512011-08-31 03:04:09 +000012997 unsigned MOVOpc = Subtarget->hasAVX() ? X86::VMOVAPSmr : X86::MOVAPSmr;
Dan Gohmand6708ea2009-08-15 01:38:56 +000012998 // In the XMM save block, save all the XMM argument registers.
12999 for (int i = 3, e = MI->getNumOperands(); i != e; ++i) {
13000 int64_t Offset = (i - 3) * 16 + VarArgsFPOffset;
Dan Gohmanc76909a2009-09-25 20:36:54 +000013001 MachineMemOperand *MMO =
Evan Chengff89dcb2009-10-18 18:16:27 +000013002 F->getMachineMemOperand(
Chris Lattnere8639032010-09-21 06:22:23 +000013003 MachinePointerInfo::getFixedStack(RegSaveFrameIndex, Offset),
Chris Lattner59db5492010-09-21 04:39:43 +000013004 MachineMemOperand::MOStore,
Evan Chengff89dcb2009-10-18 18:16:27 +000013005 /*Size=*/16, /*Align=*/16);
Bruno Cardoso Lopes5affa512011-08-31 03:04:09 +000013006 BuildMI(XMMSaveMBB, DL, TII->get(MOVOpc))
Dan Gohmand6708ea2009-08-15 01:38:56 +000013007 .addFrameIndex(RegSaveFrameIndex)
13008 .addImm(/*Scale=*/1)
13009 .addReg(/*IndexReg=*/0)
13010 .addImm(/*Disp=*/Offset)
13011 .addReg(/*Segment=*/0)
13012 .addReg(MI->getOperand(i).getReg())
Dan Gohmanc76909a2009-09-25 20:36:54 +000013013 .addMemOperand(MMO);
Dan Gohmand6708ea2009-08-15 01:38:56 +000013014 }
13015
Dan Gohman14152b42010-07-06 20:24:04 +000013016 MI->eraseFromParent(); // The pseudo instruction is gone now.
Dan Gohmand6708ea2009-08-15 01:38:56 +000013017
13018 return EndMBB;
13019}
Mon P Wang63307c32008-05-05 19:05:59 +000013020
Lang Hames6e3f7e42012-02-03 01:13:49 +000013021// The EFLAGS operand of SelectItr might be missing a kill marker
13022// because there were multiple uses of EFLAGS, and ISel didn't know
13023// which to mark. Figure out whether SelectItr should have had a
13024// kill marker, and set it if it should. Returns the correct kill
13025// marker value.
13026static bool checkAndUpdateEFLAGSKill(MachineBasicBlock::iterator SelectItr,
13027 MachineBasicBlock* BB,
13028 const TargetRegisterInfo* TRI) {
13029 // Scan forward through BB for a use/def of EFLAGS.
13030 MachineBasicBlock::iterator miI(llvm::next(SelectItr));
13031 for (MachineBasicBlock::iterator miE = BB->end(); miI != miE; ++miI) {
Lang Hames50a36f72012-02-02 07:48:37 +000013032 const MachineInstr& mi = *miI;
Lang Hames6e3f7e42012-02-03 01:13:49 +000013033 if (mi.readsRegister(X86::EFLAGS))
Lang Hames50a36f72012-02-02 07:48:37 +000013034 return false;
Lang Hames6e3f7e42012-02-03 01:13:49 +000013035 if (mi.definesRegister(X86::EFLAGS))
13036 break; // Should have kill-flag - update below.
13037 }
13038
13039 // If we hit the end of the block, check whether EFLAGS is live into a
13040 // successor.
13041 if (miI == BB->end()) {
13042 for (MachineBasicBlock::succ_iterator sItr = BB->succ_begin(),
13043 sEnd = BB->succ_end();
13044 sItr != sEnd; ++sItr) {
13045 MachineBasicBlock* succ = *sItr;
13046 if (succ->isLiveIn(X86::EFLAGS))
13047 return false;
Lang Hames50a36f72012-02-02 07:48:37 +000013048 }
13049 }
13050
Lang Hames6e3f7e42012-02-03 01:13:49 +000013051 // We found a def, or hit the end of the basic block and EFLAGS wasn't live
13052 // out. SelectMI should have a kill flag on EFLAGS.
13053 SelectItr->addRegisterKilled(X86::EFLAGS, TRI);
Lang Hames50a36f72012-02-02 07:48:37 +000013054 return true;
13055}
13056
Evan Cheng60c07e12006-07-05 22:17:51 +000013057MachineBasicBlock *
Chris Lattner52600972009-09-02 05:57:00 +000013058X86TargetLowering::EmitLoweredSelect(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000013059 MachineBasicBlock *BB) const {
Chris Lattner52600972009-09-02 05:57:00 +000013060 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
13061 DebugLoc DL = MI->getDebugLoc();
Daniel Dunbara279bc32009-09-20 02:20:51 +000013062
Chris Lattner52600972009-09-02 05:57:00 +000013063 // To "insert" a SELECT_CC instruction, we actually have to insert the
13064 // diamond control-flow pattern. The incoming instruction knows the
13065 // destination vreg to set, the condition code register to branch on, the
13066 // true/false values to select between, and a branch opcode to use.
13067 const BasicBlock *LLVM_BB = BB->getBasicBlock();
13068 MachineFunction::iterator It = BB;
13069 ++It;
Daniel Dunbara279bc32009-09-20 02:20:51 +000013070
Chris Lattner52600972009-09-02 05:57:00 +000013071 // thisMBB:
13072 // ...
13073 // TrueVal = ...
13074 // cmpTY ccX, r1, r2
13075 // bCC copy1MBB
13076 // fallthrough --> copy0MBB
13077 MachineBasicBlock *thisMBB = BB;
13078 MachineFunction *F = BB->getParent();
13079 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
13080 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Chris Lattner52600972009-09-02 05:57:00 +000013081 F->insert(It, copy0MBB);
13082 F->insert(It, sinkMBB);
Bill Wendling730c07e2010-06-25 20:48:10 +000013083
Bill Wendling730c07e2010-06-25 20:48:10 +000013084 // If the EFLAGS register isn't dead in the terminator, then claim that it's
13085 // live into the sink and copy blocks.
Lang Hames6e3f7e42012-02-03 01:13:49 +000013086 const TargetRegisterInfo* TRI = getTargetMachine().getRegisterInfo();
13087 if (!MI->killsRegister(X86::EFLAGS) &&
13088 !checkAndUpdateEFLAGSKill(MI, BB, TRI)) {
13089 copy0MBB->addLiveIn(X86::EFLAGS);
13090 sinkMBB->addLiveIn(X86::EFLAGS);
Bill Wendling730c07e2010-06-25 20:48:10 +000013091 }
13092
Dan Gohman14152b42010-07-06 20:24:04 +000013093 // Transfer the remainder of BB and its successor edges to sinkMBB.
13094 sinkMBB->splice(sinkMBB->begin(), BB,
13095 llvm::next(MachineBasicBlock::iterator(MI)),
13096 BB->end());
13097 sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
13098
13099 // Add the true and fallthrough blocks as its successors.
13100 BB->addSuccessor(copy0MBB);
13101 BB->addSuccessor(sinkMBB);
13102
13103 // Create the conditional branch instruction.
13104 unsigned Opc =
13105 X86::GetCondBranchFromCond((X86::CondCode)MI->getOperand(3).getImm());
13106 BuildMI(BB, DL, TII->get(Opc)).addMBB(sinkMBB);
13107
Chris Lattner52600972009-09-02 05:57:00 +000013108 // copy0MBB:
13109 // %FalseValue = ...
13110 // # fallthrough to sinkMBB
Dan Gohman3335a222010-04-30 20:14:26 +000013111 copy0MBB->addSuccessor(sinkMBB);
Daniel Dunbara279bc32009-09-20 02:20:51 +000013112
Chris Lattner52600972009-09-02 05:57:00 +000013113 // sinkMBB:
13114 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
13115 // ...
Dan Gohman14152b42010-07-06 20:24:04 +000013116 BuildMI(*sinkMBB, sinkMBB->begin(), DL,
13117 TII->get(X86::PHI), MI->getOperand(0).getReg())
Chris Lattner52600972009-09-02 05:57:00 +000013118 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
13119 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
13120
Dan Gohman14152b42010-07-06 20:24:04 +000013121 MI->eraseFromParent(); // The pseudo instruction is gone now.
Dan Gohman3335a222010-04-30 20:14:26 +000013122 return sinkMBB;
Chris Lattner52600972009-09-02 05:57:00 +000013123}
13124
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000013125MachineBasicBlock *
Rafael Espindola151ab3e2011-08-30 19:47:04 +000013126X86TargetLowering::EmitLoweredSegAlloca(MachineInstr *MI, MachineBasicBlock *BB,
13127 bool Is64Bit) const {
13128 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
13129 DebugLoc DL = MI->getDebugLoc();
13130 MachineFunction *MF = BB->getParent();
13131 const BasicBlock *LLVM_BB = BB->getBasicBlock();
13132
Nick Lewycky8a8d4792011-12-02 22:16:29 +000013133 assert(getTargetMachine().Options.EnableSegmentedStacks);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000013134
13135 unsigned TlsReg = Is64Bit ? X86::FS : X86::GS;
13136 unsigned TlsOffset = Is64Bit ? 0x70 : 0x30;
13137
13138 // BB:
13139 // ... [Till the alloca]
13140 // If stacklet is not large enough, jump to mallocMBB
13141 //
13142 // bumpMBB:
13143 // Allocate by subtracting from RSP
13144 // Jump to continueMBB
13145 //
13146 // mallocMBB:
13147 // Allocate by call to runtime
13148 //
13149 // continueMBB:
13150 // ...
13151 // [rest of original BB]
13152 //
13153
13154 MachineBasicBlock *mallocMBB = MF->CreateMachineBasicBlock(LLVM_BB);
13155 MachineBasicBlock *bumpMBB = MF->CreateMachineBasicBlock(LLVM_BB);
13156 MachineBasicBlock *continueMBB = MF->CreateMachineBasicBlock(LLVM_BB);
13157
13158 MachineRegisterInfo &MRI = MF->getRegInfo();
13159 const TargetRegisterClass *AddrRegClass =
13160 getRegClassFor(Is64Bit ? MVT::i64:MVT::i32);
13161
13162 unsigned mallocPtrVReg = MRI.createVirtualRegister(AddrRegClass),
13163 bumpSPPtrVReg = MRI.createVirtualRegister(AddrRegClass),
13164 tmpSPVReg = MRI.createVirtualRegister(AddrRegClass),
Rafael Espindola66bf7432011-10-26 21:16:41 +000013165 SPLimitVReg = MRI.createVirtualRegister(AddrRegClass),
Rafael Espindola151ab3e2011-08-30 19:47:04 +000013166 sizeVReg = MI->getOperand(1).getReg(),
13167 physSPReg = Is64Bit ? X86::RSP : X86::ESP;
13168
13169 MachineFunction::iterator MBBIter = BB;
13170 ++MBBIter;
13171
13172 MF->insert(MBBIter, bumpMBB);
13173 MF->insert(MBBIter, mallocMBB);
13174 MF->insert(MBBIter, continueMBB);
13175
13176 continueMBB->splice(continueMBB->begin(), BB, llvm::next
13177 (MachineBasicBlock::iterator(MI)), BB->end());
13178 continueMBB->transferSuccessorsAndUpdatePHIs(BB);
13179
13180 // Add code to the main basic block to check if the stack limit has been hit,
13181 // and if so, jump to mallocMBB otherwise to bumpMBB.
13182 BuildMI(BB, DL, TII->get(TargetOpcode::COPY), tmpSPVReg).addReg(physSPReg);
Rafael Espindola66bf7432011-10-26 21:16:41 +000013183 BuildMI(BB, DL, TII->get(Is64Bit ? X86::SUB64rr:X86::SUB32rr), SPLimitVReg)
Rafael Espindola151ab3e2011-08-30 19:47:04 +000013184 .addReg(tmpSPVReg).addReg(sizeVReg);
13185 BuildMI(BB, DL, TII->get(Is64Bit ? X86::CMP64mr:X86::CMP32mr))
Rafael Espindola014f7a32012-01-11 18:14:03 +000013186 .addReg(0).addImm(1).addReg(0).addImm(TlsOffset).addReg(TlsReg)
Rafael Espindola66bf7432011-10-26 21:16:41 +000013187 .addReg(SPLimitVReg);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000013188 BuildMI(BB, DL, TII->get(X86::JG_4)).addMBB(mallocMBB);
13189
13190 // bumpMBB simply decreases the stack pointer, since we know the current
13191 // stacklet has enough space.
13192 BuildMI(bumpMBB, DL, TII->get(TargetOpcode::COPY), physSPReg)
Rafael Espindola66bf7432011-10-26 21:16:41 +000013193 .addReg(SPLimitVReg);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000013194 BuildMI(bumpMBB, DL, TII->get(TargetOpcode::COPY), bumpSPPtrVReg)
Rafael Espindola66bf7432011-10-26 21:16:41 +000013195 .addReg(SPLimitVReg);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000013196 BuildMI(bumpMBB, DL, TII->get(X86::JMP_4)).addMBB(continueMBB);
13197
13198 // Calls into a routine in libgcc to allocate more space from the heap.
Jakob Stoklund Olesen8bcde2a2012-02-16 00:02:50 +000013199 const uint32_t *RegMask =
13200 getTargetMachine().getRegisterInfo()->getCallPreservedMask(CallingConv::C);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000013201 if (Is64Bit) {
13202 BuildMI(mallocMBB, DL, TII->get(X86::MOV64rr), X86::RDI)
13203 .addReg(sizeVReg);
13204 BuildMI(mallocMBB, DL, TII->get(X86::CALL64pcrel32))
Jakob Stoklund Olesen85dccf12012-07-04 23:53:27 +000013205 .addExternalSymbol("__morestack_allocate_stack_space")
Jakob Stoklund Olesen8bcde2a2012-02-16 00:02:50 +000013206 .addRegMask(RegMask)
Jakob Stoklund Olesen85dccf12012-07-04 23:53:27 +000013207 .addReg(X86::RDI, RegState::Implicit)
Jakob Stoklund Olesen8bcde2a2012-02-16 00:02:50 +000013208 .addReg(X86::RAX, RegState::ImplicitDefine);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000013209 } else {
13210 BuildMI(mallocMBB, DL, TII->get(X86::SUB32ri), physSPReg).addReg(physSPReg)
13211 .addImm(12);
13212 BuildMI(mallocMBB, DL, TII->get(X86::PUSH32r)).addReg(sizeVReg);
13213 BuildMI(mallocMBB, DL, TII->get(X86::CALLpcrel32))
Jakob Stoklund Olesen8bcde2a2012-02-16 00:02:50 +000013214 .addExternalSymbol("__morestack_allocate_stack_space")
13215 .addRegMask(RegMask)
13216 .addReg(X86::EAX, RegState::ImplicitDefine);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000013217 }
13218
13219 if (!Is64Bit)
13220 BuildMI(mallocMBB, DL, TII->get(X86::ADD32ri), physSPReg).addReg(physSPReg)
13221 .addImm(16);
13222
13223 BuildMI(mallocMBB, DL, TII->get(TargetOpcode::COPY), mallocPtrVReg)
13224 .addReg(Is64Bit ? X86::RAX : X86::EAX);
13225 BuildMI(mallocMBB, DL, TII->get(X86::JMP_4)).addMBB(continueMBB);
13226
13227 // Set up the CFG correctly.
13228 BB->addSuccessor(bumpMBB);
13229 BB->addSuccessor(mallocMBB);
13230 mallocMBB->addSuccessor(continueMBB);
13231 bumpMBB->addSuccessor(continueMBB);
13232
13233 // Take care of the PHI nodes.
13234 BuildMI(*continueMBB, continueMBB->begin(), DL, TII->get(X86::PHI),
13235 MI->getOperand(0).getReg())
13236 .addReg(mallocPtrVReg).addMBB(mallocMBB)
13237 .addReg(bumpSPPtrVReg).addMBB(bumpMBB);
13238
13239 // Delete the original pseudo instruction.
13240 MI->eraseFromParent();
13241
13242 // And we're done.
13243 return continueMBB;
13244}
13245
13246MachineBasicBlock *
Michael J. Spencere9c253e2010-10-21 01:41:01 +000013247X86TargetLowering::EmitLoweredWinAlloca(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000013248 MachineBasicBlock *BB) const {
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000013249 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
13250 DebugLoc DL = MI->getDebugLoc();
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000013251
NAKAMURA Takumia2e07622011-03-24 07:07:00 +000013252 assert(!Subtarget->isTargetEnvMacho());
13253
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000013254 // The lowering is pretty easy: we're just emitting the call to _alloca. The
13255 // non-trivial part is impdef of ESP.
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000013256
NAKAMURA Takumia2e07622011-03-24 07:07:00 +000013257 if (Subtarget->isTargetWin64()) {
13258 if (Subtarget->isTargetCygMing()) {
13259 // ___chkstk(Mingw64):
13260 // Clobbers R10, R11, RAX and EFLAGS.
13261 // Updates RSP.
13262 BuildMI(*BB, MI, DL, TII->get(X86::W64ALLOCA))
13263 .addExternalSymbol("___chkstk")
13264 .addReg(X86::RAX, RegState::Implicit)
13265 .addReg(X86::RSP, RegState::Implicit)
13266 .addReg(X86::RAX, RegState::Define | RegState::Implicit)
13267 .addReg(X86::RSP, RegState::Define | RegState::Implicit)
13268 .addReg(X86::EFLAGS, RegState::Define | RegState::Implicit);
13269 } else {
13270 // __chkstk(MSVCRT): does not update stack pointer.
13271 // Clobbers R10, R11 and EFLAGS.
13272 // FIXME: RAX(allocated size) might be reused and not killed.
13273 BuildMI(*BB, MI, DL, TII->get(X86::W64ALLOCA))
13274 .addExternalSymbol("__chkstk")
13275 .addReg(X86::RAX, RegState::Implicit)
13276 .addReg(X86::EFLAGS, RegState::Define | RegState::Implicit);
13277 // RAX has the offset to subtracted from RSP.
13278 BuildMI(*BB, MI, DL, TII->get(X86::SUB64rr), X86::RSP)
13279 .addReg(X86::RSP)
13280 .addReg(X86::RAX);
13281 }
13282 } else {
13283 const char *StackProbeSymbol =
Michael J. Spencere9c253e2010-10-21 01:41:01 +000013284 Subtarget->isTargetWindows() ? "_chkstk" : "_alloca";
13285
NAKAMURA Takumia2e07622011-03-24 07:07:00 +000013286 BuildMI(*BB, MI, DL, TII->get(X86::CALLpcrel32))
13287 .addExternalSymbol(StackProbeSymbol)
13288 .addReg(X86::EAX, RegState::Implicit)
13289 .addReg(X86::ESP, RegState::Implicit)
13290 .addReg(X86::EAX, RegState::Define | RegState::Implicit)
13291 .addReg(X86::ESP, RegState::Define | RegState::Implicit)
13292 .addReg(X86::EFLAGS, RegState::Define | RegState::Implicit);
13293 }
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000013294
Dan Gohman14152b42010-07-06 20:24:04 +000013295 MI->eraseFromParent(); // The pseudo instruction is gone now.
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000013296 return BB;
13297}
Chris Lattner52600972009-09-02 05:57:00 +000013298
13299MachineBasicBlock *
Eric Christopher30ef0e52010-06-03 04:07:48 +000013300X86TargetLowering::EmitLoweredTLSCall(MachineInstr *MI,
13301 MachineBasicBlock *BB) const {
13302 // This is pretty easy. We're taking the value that we received from
13303 // our load from the relocation, sticking it in either RDI (x86-64)
13304 // or EAX and doing an indirect call. The return value will then
13305 // be in the normal return register.
Michael J. Spencerec38de22010-10-10 22:04:20 +000013306 const X86InstrInfo *TII
Eric Christopher54415362010-06-08 22:04:25 +000013307 = static_cast<const X86InstrInfo*>(getTargetMachine().getInstrInfo());
Eric Christopher30ef0e52010-06-03 04:07:48 +000013308 DebugLoc DL = MI->getDebugLoc();
13309 MachineFunction *F = BB->getParent();
Eric Christopher722d3152010-09-27 06:01:51 +000013310
13311 assert(Subtarget->isTargetDarwin() && "Darwin only instr emitted?");
Eric Christopher54415362010-06-08 22:04:25 +000013312 assert(MI->getOperand(3).isGlobal() && "This should be a global");
Michael J. Spencerec38de22010-10-10 22:04:20 +000013313
Jakob Stoklund Olesen8bcde2a2012-02-16 00:02:50 +000013314 // Get a register mask for the lowered call.
13315 // FIXME: The 32-bit calls have non-standard calling conventions. Use a
13316 // proper register mask.
13317 const uint32_t *RegMask =
13318 getTargetMachine().getRegisterInfo()->getCallPreservedMask(CallingConv::C);
Eric Christopher30ef0e52010-06-03 04:07:48 +000013319 if (Subtarget->is64Bit()) {
Dan Gohman14152b42010-07-06 20:24:04 +000013320 MachineInstrBuilder MIB = BuildMI(*BB, MI, DL,
13321 TII->get(X86::MOV64rm), X86::RDI)
Eric Christopher54415362010-06-08 22:04:25 +000013322 .addReg(X86::RIP)
13323 .addImm(0).addReg(0)
Michael J. Spencerec38de22010-10-10 22:04:20 +000013324 .addGlobalAddress(MI->getOperand(3).getGlobal(), 0,
Eric Christopher54415362010-06-08 22:04:25 +000013325 MI->getOperand(3).getTargetFlags())
13326 .addReg(0);
Eric Christopher722d3152010-09-27 06:01:51 +000013327 MIB = BuildMI(*BB, MI, DL, TII->get(X86::CALL64m));
Chris Lattner599b5312010-07-08 23:46:44 +000013328 addDirectMem(MIB, X86::RDI);
Jakob Stoklund Olesen8bcde2a2012-02-16 00:02:50 +000013329 MIB.addReg(X86::RAX, RegState::ImplicitDefine).addRegMask(RegMask);
Eric Christopher61025492010-06-15 23:08:42 +000013330 } else if (getTargetMachine().getRelocationModel() != Reloc::PIC_) {
Dan Gohman14152b42010-07-06 20:24:04 +000013331 MachineInstrBuilder MIB = BuildMI(*BB, MI, DL,
13332 TII->get(X86::MOV32rm), X86::EAX)
Eric Christopher61025492010-06-15 23:08:42 +000013333 .addReg(0)
13334 .addImm(0).addReg(0)
Michael J. Spencerec38de22010-10-10 22:04:20 +000013335 .addGlobalAddress(MI->getOperand(3).getGlobal(), 0,
Eric Christopher61025492010-06-15 23:08:42 +000013336 MI->getOperand(3).getTargetFlags())
13337 .addReg(0);
Dan Gohman14152b42010-07-06 20:24:04 +000013338 MIB = BuildMI(*BB, MI, DL, TII->get(X86::CALL32m));
Chris Lattner599b5312010-07-08 23:46:44 +000013339 addDirectMem(MIB, X86::EAX);
Jakob Stoklund Olesen8bcde2a2012-02-16 00:02:50 +000013340 MIB.addReg(X86::EAX, RegState::ImplicitDefine).addRegMask(RegMask);
Eric Christopher30ef0e52010-06-03 04:07:48 +000013341 } else {
Dan Gohman14152b42010-07-06 20:24:04 +000013342 MachineInstrBuilder MIB = BuildMI(*BB, MI, DL,
13343 TII->get(X86::MOV32rm), X86::EAX)
Eric Christopher54415362010-06-08 22:04:25 +000013344 .addReg(TII->getGlobalBaseReg(F))
13345 .addImm(0).addReg(0)
Michael J. Spencerec38de22010-10-10 22:04:20 +000013346 .addGlobalAddress(MI->getOperand(3).getGlobal(), 0,
Eric Christopher54415362010-06-08 22:04:25 +000013347 MI->getOperand(3).getTargetFlags())
13348 .addReg(0);
Dan Gohman14152b42010-07-06 20:24:04 +000013349 MIB = BuildMI(*BB, MI, DL, TII->get(X86::CALL32m));
Chris Lattner599b5312010-07-08 23:46:44 +000013350 addDirectMem(MIB, X86::EAX);
Jakob Stoklund Olesen8bcde2a2012-02-16 00:02:50 +000013351 MIB.addReg(X86::EAX, RegState::ImplicitDefine).addRegMask(RegMask);
Eric Christopher30ef0e52010-06-03 04:07:48 +000013352 }
Michael J. Spencerec38de22010-10-10 22:04:20 +000013353
Dan Gohman14152b42010-07-06 20:24:04 +000013354 MI->eraseFromParent(); // The pseudo instruction is gone now.
Eric Christopher30ef0e52010-06-03 04:07:48 +000013355 return BB;
13356}
13357
13358MachineBasicBlock *
Michael Liao6c0e04c2012-10-15 22:39:43 +000013359X86TargetLowering::emitEHSjLjSetJmp(MachineInstr *MI,
13360 MachineBasicBlock *MBB) const {
13361 DebugLoc DL = MI->getDebugLoc();
13362 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
13363
13364 MachineFunction *MF = MBB->getParent();
13365 MachineRegisterInfo &MRI = MF->getRegInfo();
13366
13367 const BasicBlock *BB = MBB->getBasicBlock();
13368 MachineFunction::iterator I = MBB;
13369 ++I;
13370
13371 // Memory Reference
13372 MachineInstr::mmo_iterator MMOBegin = MI->memoperands_begin();
13373 MachineInstr::mmo_iterator MMOEnd = MI->memoperands_end();
13374
13375 unsigned DstReg;
13376 unsigned MemOpndSlot = 0;
13377
13378 unsigned CurOp = 0;
13379
13380 DstReg = MI->getOperand(CurOp++).getReg();
13381 const TargetRegisterClass *RC = MRI.getRegClass(DstReg);
13382 assert(RC->hasType(MVT::i32) && "Invalid destination!");
13383 unsigned mainDstReg = MRI.createVirtualRegister(RC);
13384 unsigned restoreDstReg = MRI.createVirtualRegister(RC);
13385
13386 MemOpndSlot = CurOp;
13387
13388 MVT PVT = getPointerTy();
13389 assert((PVT == MVT::i64 || PVT == MVT::i32) &&
13390 "Invalid Pointer Size!");
13391
13392 // For v = setjmp(buf), we generate
13393 //
13394 // thisMBB:
Michael Liao281ae5a2012-10-17 02:22:27 +000013395 // buf[LabelOffset] = restoreMBB
Michael Liao6c0e04c2012-10-15 22:39:43 +000013396 // SjLjSetup restoreMBB
13397 //
13398 // mainMBB:
13399 // v_main = 0
13400 //
13401 // sinkMBB:
13402 // v = phi(main, restore)
13403 //
13404 // restoreMBB:
13405 // v_restore = 1
13406
13407 MachineBasicBlock *thisMBB = MBB;
13408 MachineBasicBlock *mainMBB = MF->CreateMachineBasicBlock(BB);
13409 MachineBasicBlock *sinkMBB = MF->CreateMachineBasicBlock(BB);
13410 MachineBasicBlock *restoreMBB = MF->CreateMachineBasicBlock(BB);
13411 MF->insert(I, mainMBB);
13412 MF->insert(I, sinkMBB);
13413 MF->push_back(restoreMBB);
13414
13415 MachineInstrBuilder MIB;
13416
13417 // Transfer the remainder of BB and its successor edges to sinkMBB.
13418 sinkMBB->splice(sinkMBB->begin(), MBB,
13419 llvm::next(MachineBasicBlock::iterator(MI)), MBB->end());
13420 sinkMBB->transferSuccessorsAndUpdatePHIs(MBB);
13421
13422 // thisMBB:
Michael Liao281ae5a2012-10-17 02:22:27 +000013423 unsigned PtrStoreOpc = 0;
13424 unsigned LabelReg = 0;
13425 const int64_t LabelOffset = 1 * PVT.getStoreSize();
13426 Reloc::Model RM = getTargetMachine().getRelocationModel();
13427 bool UseImmLabel = (getTargetMachine().getCodeModel() == CodeModel::Small) &&
13428 (RM == Reloc::Static || RM == Reloc::DynamicNoPIC);
Michael Liao6c0e04c2012-10-15 22:39:43 +000013429
Michael Liao281ae5a2012-10-17 02:22:27 +000013430 // Prepare IP either in reg or imm.
13431 if (!UseImmLabel) {
13432 PtrStoreOpc = (PVT == MVT::i64) ? X86::MOV64mr : X86::MOV32mr;
13433 const TargetRegisterClass *PtrRC = getRegClassFor(PVT);
13434 LabelReg = MRI.createVirtualRegister(PtrRC);
13435 if (Subtarget->is64Bit()) {
13436 MIB = BuildMI(*thisMBB, MI, DL, TII->get(X86::LEA64r), LabelReg)
13437 .addReg(X86::RIP)
13438 .addImm(0)
13439 .addReg(0)
13440 .addMBB(restoreMBB)
13441 .addReg(0);
13442 } else {
13443 const X86InstrInfo *XII = static_cast<const X86InstrInfo*>(TII);
13444 MIB = BuildMI(*thisMBB, MI, DL, TII->get(X86::LEA32r), LabelReg)
13445 .addReg(XII->getGlobalBaseReg(MF))
13446 .addImm(0)
13447 .addReg(0)
13448 .addMBB(restoreMBB, Subtarget->ClassifyBlockAddressReference())
13449 .addReg(0);
13450 }
13451 } else
13452 PtrStoreOpc = (PVT == MVT::i64) ? X86::MOV64mi32 : X86::MOV32mi;
Michael Liao6c0e04c2012-10-15 22:39:43 +000013453 // Store IP
Michael Liao281ae5a2012-10-17 02:22:27 +000013454 MIB = BuildMI(*thisMBB, MI, DL, TII->get(PtrStoreOpc));
Michael Liao6c0e04c2012-10-15 22:39:43 +000013455 for (unsigned i = 0; i < X86::AddrNumOperands; ++i) {
13456 if (i == X86::AddrDisp)
Michael Liao281ae5a2012-10-17 02:22:27 +000013457 MIB.addDisp(MI->getOperand(MemOpndSlot + i), LabelOffset);
Michael Liao6c0e04c2012-10-15 22:39:43 +000013458 else
13459 MIB.addOperand(MI->getOperand(MemOpndSlot + i));
13460 }
Michael Liao281ae5a2012-10-17 02:22:27 +000013461 if (!UseImmLabel)
13462 MIB.addReg(LabelReg);
13463 else
13464 MIB.addMBB(restoreMBB);
Michael Liao6c0e04c2012-10-15 22:39:43 +000013465 MIB.setMemRefs(MMOBegin, MMOEnd);
13466 // Setup
13467 MIB = BuildMI(*thisMBB, MI, DL, TII->get(X86::EH_SjLj_Setup))
13468 .addMBB(restoreMBB);
13469 MIB.addRegMask(RegInfo->getNoPreservedMask());
13470 thisMBB->addSuccessor(mainMBB);
13471 thisMBB->addSuccessor(restoreMBB);
13472
13473 // mainMBB:
13474 // EAX = 0
13475 BuildMI(mainMBB, DL, TII->get(X86::MOV32r0), mainDstReg);
13476 mainMBB->addSuccessor(sinkMBB);
13477
13478 // sinkMBB:
13479 BuildMI(*sinkMBB, sinkMBB->begin(), DL,
13480 TII->get(X86::PHI), DstReg)
13481 .addReg(mainDstReg).addMBB(mainMBB)
13482 .addReg(restoreDstReg).addMBB(restoreMBB);
13483
13484 // restoreMBB:
13485 BuildMI(restoreMBB, DL, TII->get(X86::MOV32ri), restoreDstReg).addImm(1);
13486 BuildMI(restoreMBB, DL, TII->get(X86::JMP_4)).addMBB(sinkMBB);
13487 restoreMBB->addSuccessor(sinkMBB);
13488
13489 MI->eraseFromParent();
13490 return sinkMBB;
13491}
13492
13493MachineBasicBlock *
13494X86TargetLowering::emitEHSjLjLongJmp(MachineInstr *MI,
13495 MachineBasicBlock *MBB) const {
13496 DebugLoc DL = MI->getDebugLoc();
13497 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
13498
13499 MachineFunction *MF = MBB->getParent();
13500 MachineRegisterInfo &MRI = MF->getRegInfo();
13501
13502 // Memory Reference
13503 MachineInstr::mmo_iterator MMOBegin = MI->memoperands_begin();
13504 MachineInstr::mmo_iterator MMOEnd = MI->memoperands_end();
13505
13506 MVT PVT = getPointerTy();
13507 assert((PVT == MVT::i64 || PVT == MVT::i32) &&
13508 "Invalid Pointer Size!");
13509
13510 const TargetRegisterClass *RC =
13511 (PVT == MVT::i64) ? &X86::GR64RegClass : &X86::GR32RegClass;
13512 unsigned Tmp = MRI.createVirtualRegister(RC);
13513 // Since FP is only updated here but NOT referenced, it's treated as GPR.
13514 unsigned FP = (PVT == MVT::i64) ? X86::RBP : X86::EBP;
13515 unsigned SP = RegInfo->getStackRegister();
13516
13517 MachineInstrBuilder MIB;
13518
Michael Liao281ae5a2012-10-17 02:22:27 +000013519 const int64_t LabelOffset = 1 * PVT.getStoreSize();
13520 const int64_t SPOffset = 2 * PVT.getStoreSize();
Michael Liao6c0e04c2012-10-15 22:39:43 +000013521
13522 unsigned PtrLoadOpc = (PVT == MVT::i64) ? X86::MOV64rm : X86::MOV32rm;
13523 unsigned IJmpOpc = (PVT == MVT::i64) ? X86::JMP64r : X86::JMP32r;
13524
13525 // Reload FP
13526 MIB = BuildMI(*MBB, MI, DL, TII->get(PtrLoadOpc), FP);
13527 for (unsigned i = 0; i < X86::AddrNumOperands; ++i)
13528 MIB.addOperand(MI->getOperand(i));
13529 MIB.setMemRefs(MMOBegin, MMOEnd);
13530 // Reload IP
13531 MIB = BuildMI(*MBB, MI, DL, TII->get(PtrLoadOpc), Tmp);
13532 for (unsigned i = 0; i < X86::AddrNumOperands; ++i) {
13533 if (i == X86::AddrDisp)
Michael Liao281ae5a2012-10-17 02:22:27 +000013534 MIB.addDisp(MI->getOperand(i), LabelOffset);
Michael Liao6c0e04c2012-10-15 22:39:43 +000013535 else
13536 MIB.addOperand(MI->getOperand(i));
13537 }
13538 MIB.setMemRefs(MMOBegin, MMOEnd);
13539 // Reload SP
13540 MIB = BuildMI(*MBB, MI, DL, TII->get(PtrLoadOpc), SP);
13541 for (unsigned i = 0; i < X86::AddrNumOperands; ++i) {
13542 if (i == X86::AddrDisp)
Michael Liao281ae5a2012-10-17 02:22:27 +000013543 MIB.addDisp(MI->getOperand(i), SPOffset);
Michael Liao6c0e04c2012-10-15 22:39:43 +000013544 else
13545 MIB.addOperand(MI->getOperand(i));
13546 }
13547 MIB.setMemRefs(MMOBegin, MMOEnd);
13548 // Jump
13549 BuildMI(*MBB, MI, DL, TII->get(IJmpOpc)).addReg(Tmp);
13550
13551 MI->eraseFromParent();
13552 return MBB;
13553}
13554
13555MachineBasicBlock *
Evan Chengff9b3732008-01-30 18:18:23 +000013556X86TargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000013557 MachineBasicBlock *BB) const {
Evan Cheng60c07e12006-07-05 22:17:51 +000013558 switch (MI->getOpcode()) {
Craig Topperabb94d02012-02-05 03:43:23 +000013559 default: llvm_unreachable("Unexpected instr type to insert");
NAKAMURA Takumi7754f852011-01-26 02:04:09 +000013560 case X86::TAILJMPd64:
13561 case X86::TAILJMPr64:
13562 case X86::TAILJMPm64:
Craig Topper6d1263a2012-02-05 05:38:58 +000013563 llvm_unreachable("TAILJMP64 would not be touched here.");
NAKAMURA Takumi7754f852011-01-26 02:04:09 +000013564 case X86::TCRETURNdi64:
13565 case X86::TCRETURNri64:
13566 case X86::TCRETURNmi64:
NAKAMURA Takumi7754f852011-01-26 02:04:09 +000013567 return BB;
Michael J. Spencere9c253e2010-10-21 01:41:01 +000013568 case X86::WIN_ALLOCA:
13569 return EmitLoweredWinAlloca(MI, BB);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000013570 case X86::SEG_ALLOCA_32:
13571 return EmitLoweredSegAlloca(MI, BB, false);
13572 case X86::SEG_ALLOCA_64:
13573 return EmitLoweredSegAlloca(MI, BB, true);
Eric Christopher30ef0e52010-06-03 04:07:48 +000013574 case X86::TLSCall_32:
13575 case X86::TLSCall_64:
13576 return EmitLoweredTLSCall(MI, BB);
Dan Gohmancbbea0f2009-08-27 00:14:12 +000013577 case X86::CMOV_GR8:
Evan Cheng60c07e12006-07-05 22:17:51 +000013578 case X86::CMOV_FR32:
13579 case X86::CMOV_FR64:
13580 case X86::CMOV_V4F32:
13581 case X86::CMOV_V2F64:
Chris Lattner52600972009-09-02 05:57:00 +000013582 case X86::CMOV_V2I64:
Bruno Cardoso Lopesd40aa242011-08-09 23:27:13 +000013583 case X86::CMOV_V8F32:
13584 case X86::CMOV_V4F64:
13585 case X86::CMOV_V4I64:
Chris Lattner314a1132010-03-14 18:31:44 +000013586 case X86::CMOV_GR16:
13587 case X86::CMOV_GR32:
13588 case X86::CMOV_RFP32:
13589 case X86::CMOV_RFP64:
13590 case X86::CMOV_RFP80:
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000013591 return EmitLoweredSelect(MI, BB);
Evan Cheng60c07e12006-07-05 22:17:51 +000013592
Dale Johannesen849f2142007-07-03 00:53:03 +000013593 case X86::FP32_TO_INT16_IN_MEM:
13594 case X86::FP32_TO_INT32_IN_MEM:
13595 case X86::FP32_TO_INT64_IN_MEM:
13596 case X86::FP64_TO_INT16_IN_MEM:
13597 case X86::FP64_TO_INT32_IN_MEM:
Dale Johannesena996d522007-08-07 01:17:37 +000013598 case X86::FP64_TO_INT64_IN_MEM:
13599 case X86::FP80_TO_INT16_IN_MEM:
13600 case X86::FP80_TO_INT32_IN_MEM:
13601 case X86::FP80_TO_INT64_IN_MEM: {
Chris Lattner52600972009-09-02 05:57:00 +000013602 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
13603 DebugLoc DL = MI->getDebugLoc();
13604
Evan Cheng60c07e12006-07-05 22:17:51 +000013605 // Change the floating point control register to use "round towards zero"
13606 // mode when truncating to an integer value.
13607 MachineFunction *F = BB->getParent();
David Greene3f2bf852009-11-12 20:49:22 +000013608 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2, false);
Dan Gohman14152b42010-07-06 20:24:04 +000013609 addFrameReference(BuildMI(*BB, MI, DL,
13610 TII->get(X86::FNSTCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000013611
13612 // Load the old value of the high byte of the control word...
13613 unsigned OldCW =
Craig Topperc9099502012-04-20 06:31:50 +000013614 F->getRegInfo().createVirtualRegister(&X86::GR16RegClass);
Dan Gohman14152b42010-07-06 20:24:04 +000013615 addFrameReference(BuildMI(*BB, MI, DL, TII->get(X86::MOV16rm), OldCW),
Dale Johannesene4d209d2009-02-03 20:21:25 +000013616 CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000013617
13618 // Set the high part to be round to zero...
Dan Gohman14152b42010-07-06 20:24:04 +000013619 addFrameReference(BuildMI(*BB, MI, DL, TII->get(X86::MOV16mi)), CWFrameIdx)
Evan Chengc0f64ff2006-11-27 23:37:22 +000013620 .addImm(0xC7F);
Evan Cheng60c07e12006-07-05 22:17:51 +000013621
13622 // Reload the modified control word now...
Dan Gohman14152b42010-07-06 20:24:04 +000013623 addFrameReference(BuildMI(*BB, MI, DL,
13624 TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000013625
13626 // Restore the memory image of control word to original value
Dan Gohman14152b42010-07-06 20:24:04 +000013627 addFrameReference(BuildMI(*BB, MI, DL, TII->get(X86::MOV16mr)), CWFrameIdx)
Evan Chengc0f64ff2006-11-27 23:37:22 +000013628 .addReg(OldCW);
Evan Cheng60c07e12006-07-05 22:17:51 +000013629
13630 // Get the X86 opcode to use.
13631 unsigned Opc;
13632 switch (MI->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +000013633 default: llvm_unreachable("illegal opcode!");
Dale Johannesene377d4d2007-07-04 21:07:47 +000013634 case X86::FP32_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m32; break;
13635 case X86::FP32_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m32; break;
13636 case X86::FP32_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m32; break;
13637 case X86::FP64_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m64; break;
13638 case X86::FP64_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m64; break;
13639 case X86::FP64_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m64; break;
Dale Johannesena996d522007-08-07 01:17:37 +000013640 case X86::FP80_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m80; break;
13641 case X86::FP80_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m80; break;
13642 case X86::FP80_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m80; break;
Evan Cheng60c07e12006-07-05 22:17:51 +000013643 }
13644
13645 X86AddressMode AM;
13646 MachineOperand &Op = MI->getOperand(0);
Dan Gohmand735b802008-10-03 15:45:36 +000013647 if (Op.isReg()) {
Evan Cheng60c07e12006-07-05 22:17:51 +000013648 AM.BaseType = X86AddressMode::RegBase;
13649 AM.Base.Reg = Op.getReg();
13650 } else {
13651 AM.BaseType = X86AddressMode::FrameIndexBase;
Chris Lattner8aa797a2007-12-30 23:10:15 +000013652 AM.Base.FrameIndex = Op.getIndex();
Evan Cheng60c07e12006-07-05 22:17:51 +000013653 }
13654 Op = MI->getOperand(1);
Dan Gohmand735b802008-10-03 15:45:36 +000013655 if (Op.isImm())
Chris Lattner7fbe9722006-10-20 17:42:20 +000013656 AM.Scale = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +000013657 Op = MI->getOperand(2);
Dan Gohmand735b802008-10-03 15:45:36 +000013658 if (Op.isImm())
Chris Lattner7fbe9722006-10-20 17:42:20 +000013659 AM.IndexReg = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +000013660 Op = MI->getOperand(3);
Dan Gohmand735b802008-10-03 15:45:36 +000013661 if (Op.isGlobal()) {
Evan Cheng60c07e12006-07-05 22:17:51 +000013662 AM.GV = Op.getGlobal();
13663 } else {
Chris Lattner7fbe9722006-10-20 17:42:20 +000013664 AM.Disp = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +000013665 }
Dan Gohman14152b42010-07-06 20:24:04 +000013666 addFullAddress(BuildMI(*BB, MI, DL, TII->get(Opc)), AM)
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000013667 .addReg(MI->getOperand(X86::AddrNumOperands).getReg());
Evan Cheng60c07e12006-07-05 22:17:51 +000013668
13669 // Reload the original control word now.
Dan Gohman14152b42010-07-06 20:24:04 +000013670 addFrameReference(BuildMI(*BB, MI, DL,
13671 TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000013672
Dan Gohman14152b42010-07-06 20:24:04 +000013673 MI->eraseFromParent(); // The pseudo instruction is gone now.
Evan Cheng60c07e12006-07-05 22:17:51 +000013674 return BB;
13675 }
Eric Christopherb120ab42009-08-18 22:50:32 +000013676 // String/text processing lowering.
13677 case X86::PCMPISTRM128REG:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000013678 case X86::VPCMPISTRM128REG:
Eric Christopherb120ab42009-08-18 22:50:32 +000013679 case X86::PCMPISTRM128MEM:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000013680 case X86::VPCMPISTRM128MEM:
Eric Christopherb120ab42009-08-18 22:50:32 +000013681 case X86::PCMPESTRM128REG:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000013682 case X86::VPCMPESTRM128REG:
Eric Christopherb120ab42009-08-18 22:50:32 +000013683 case X86::PCMPESTRM128MEM:
Craig Topper63a99ff2012-08-17 07:15:56 +000013684 case X86::VPCMPESTRM128MEM: {
13685 unsigned NumArgs;
13686 bool MemArg;
13687 switch (MI->getOpcode()) {
13688 default: llvm_unreachable("illegal opcode!");
13689 case X86::PCMPISTRM128REG:
13690 case X86::VPCMPISTRM128REG:
13691 NumArgs = 3; MemArg = false; break;
13692 case X86::PCMPISTRM128MEM:
13693 case X86::VPCMPISTRM128MEM:
13694 NumArgs = 3; MemArg = true; break;
13695 case X86::PCMPESTRM128REG:
13696 case X86::VPCMPESTRM128REG:
13697 NumArgs = 5; MemArg = false; break;
13698 case X86::PCMPESTRM128MEM:
13699 case X86::VPCMPESTRM128MEM:
13700 NumArgs = 5; MemArg = true; break;
13701 }
13702 return EmitPCMP(MI, BB, NumArgs, MemArg);
13703 }
Eric Christopherb120ab42009-08-18 22:50:32 +000013704
Eric Christopher228232b2010-11-30 07:20:12 +000013705 // Thread synchronization.
13706 case X86::MONITOR:
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013707 return EmitMonitor(MI, BB);
Eric Christopher228232b2010-11-30 07:20:12 +000013708
Eric Christopherb120ab42009-08-18 22:50:32 +000013709 // Atomic Lowering.
Dale Johannesen140be2d2008-08-19 18:47:28 +000013710 case X86::ATOMAND8:
Michael Liaob118a072012-09-20 03:06:15 +000013711 case X86::ATOMAND16:
13712 case X86::ATOMAND32:
Dale Johannesena99e3842008-08-20 00:48:50 +000013713 case X86::ATOMAND64:
Michael Liaob118a072012-09-20 03:06:15 +000013714 // Fall through
13715 case X86::ATOMOR8:
13716 case X86::ATOMOR16:
13717 case X86::ATOMOR32:
Dale Johannesena99e3842008-08-20 00:48:50 +000013718 case X86::ATOMOR64:
Michael Liaob118a072012-09-20 03:06:15 +000013719 // Fall through
13720 case X86::ATOMXOR16:
13721 case X86::ATOMXOR8:
13722 case X86::ATOMXOR32:
Dale Johannesena99e3842008-08-20 00:48:50 +000013723 case X86::ATOMXOR64:
Michael Liaob118a072012-09-20 03:06:15 +000013724 // Fall through
13725 case X86::ATOMNAND8:
13726 case X86::ATOMNAND16:
13727 case X86::ATOMNAND32:
13728 case X86::ATOMNAND64:
13729 // Fall through
Michael Liaofe87c302012-09-21 03:18:52 +000013730 case X86::ATOMMAX8:
Michael Liaob118a072012-09-20 03:06:15 +000013731 case X86::ATOMMAX16:
13732 case X86::ATOMMAX32:
13733 case X86::ATOMMAX64:
13734 // Fall through
Michael Liaofe87c302012-09-21 03:18:52 +000013735 case X86::ATOMMIN8:
Michael Liaob118a072012-09-20 03:06:15 +000013736 case X86::ATOMMIN16:
13737 case X86::ATOMMIN32:
13738 case X86::ATOMMIN64:
13739 // Fall through
Michael Liaofe87c302012-09-21 03:18:52 +000013740 case X86::ATOMUMAX8:
Michael Liaob118a072012-09-20 03:06:15 +000013741 case X86::ATOMUMAX16:
13742 case X86::ATOMUMAX32:
13743 case X86::ATOMUMAX64:
13744 // Fall through
Michael Liaofe87c302012-09-21 03:18:52 +000013745 case X86::ATOMUMIN8:
Michael Liaob118a072012-09-20 03:06:15 +000013746 case X86::ATOMUMIN16:
13747 case X86::ATOMUMIN32:
13748 case X86::ATOMUMIN64:
13749 return EmitAtomicLoadArith(MI, BB);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000013750
13751 // This group does 64-bit operations on a 32-bit host.
13752 case X86::ATOMAND6432:
Dale Johannesen48c1bc22008-10-02 18:53:47 +000013753 case X86::ATOMOR6432:
Dale Johannesen48c1bc22008-10-02 18:53:47 +000013754 case X86::ATOMXOR6432:
Dale Johannesen48c1bc22008-10-02 18:53:47 +000013755 case X86::ATOMNAND6432:
Dale Johannesen48c1bc22008-10-02 18:53:47 +000013756 case X86::ATOMADD6432:
Dale Johannesen48c1bc22008-10-02 18:53:47 +000013757 case X86::ATOMSUB6432:
Michael Liaoe5e8f762012-09-25 18:08:13 +000013758 case X86::ATOMMAX6432:
13759 case X86::ATOMMIN6432:
13760 case X86::ATOMUMAX6432:
13761 case X86::ATOMUMIN6432:
Michael Liaob118a072012-09-20 03:06:15 +000013762 case X86::ATOMSWAP6432:
13763 return EmitAtomicLoadArith6432(MI, BB);
Craig Topperacaaa6f2012-08-18 06:39:34 +000013764
Dan Gohmand6708ea2009-08-15 01:38:56 +000013765 case X86::VASTART_SAVE_XMM_REGS:
13766 return EmitVAStartSaveXMMRegsWithCustomInserter(MI, BB);
Dan Gohman320afb82010-10-12 18:00:49 +000013767
13768 case X86::VAARG_64:
13769 return EmitVAARG64WithCustomInserter(MI, BB);
Michael Liao6c0e04c2012-10-15 22:39:43 +000013770
13771 case X86::EH_SjLj_SetJmp32:
13772 case X86::EH_SjLj_SetJmp64:
13773 return emitEHSjLjSetJmp(MI, BB);
13774
13775 case X86::EH_SjLj_LongJmp32:
13776 case X86::EH_SjLj_LongJmp64:
13777 return emitEHSjLjLongJmp(MI, BB);
Evan Cheng60c07e12006-07-05 22:17:51 +000013778 }
13779}
13780
13781//===----------------------------------------------------------------------===//
13782// X86 Optimization Hooks
13783//===----------------------------------------------------------------------===//
13784
Dan Gohman475871a2008-07-27 21:46:04 +000013785void X86TargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
Dan Gohmanfd29e0e2008-02-13 00:35:47 +000013786 APInt &KnownZero,
13787 APInt &KnownOne,
Dan Gohmanea859be2007-06-22 14:59:07 +000013788 const SelectionDAG &DAG,
Nate Begeman368e18d2006-02-16 21:11:51 +000013789 unsigned Depth) const {
Rafael Espindola26c8dcc2012-04-04 12:51:34 +000013790 unsigned BitWidth = KnownZero.getBitWidth();
Evan Cheng3a03ebb2005-12-21 23:05:39 +000013791 unsigned Opc = Op.getOpcode();
Evan Cheng865f0602006-04-05 06:11:20 +000013792 assert((Opc >= ISD::BUILTIN_OP_END ||
13793 Opc == ISD::INTRINSIC_WO_CHAIN ||
13794 Opc == ISD::INTRINSIC_W_CHAIN ||
13795 Opc == ISD::INTRINSIC_VOID) &&
13796 "Should use MaskedValueIsZero if you don't know whether Op"
13797 " is a target node!");
Evan Cheng3a03ebb2005-12-21 23:05:39 +000013798
Rafael Espindola26c8dcc2012-04-04 12:51:34 +000013799 KnownZero = KnownOne = APInt(BitWidth, 0); // Don't know anything.
Evan Cheng3a03ebb2005-12-21 23:05:39 +000013800 switch (Opc) {
Evan Cheng865f0602006-04-05 06:11:20 +000013801 default: break;
Evan Cheng97d0e0e2009-02-02 09:15:04 +000013802 case X86ISD::ADD:
13803 case X86ISD::SUB:
Chris Lattner5b856542010-12-20 00:59:46 +000013804 case X86ISD::ADC:
13805 case X86ISD::SBB:
Evan Cheng97d0e0e2009-02-02 09:15:04 +000013806 case X86ISD::SMUL:
13807 case X86ISD::UMUL:
Dan Gohman076aee32009-03-04 19:44:21 +000013808 case X86ISD::INC:
13809 case X86ISD::DEC:
Dan Gohmane220c4b2009-09-18 19:59:53 +000013810 case X86ISD::OR:
13811 case X86ISD::XOR:
13812 case X86ISD::AND:
Evan Cheng97d0e0e2009-02-02 09:15:04 +000013813 // These nodes' second result is a boolean.
13814 if (Op.getResNo() == 0)
13815 break;
13816 // Fallthrough
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000013817 case X86ISD::SETCC:
Rafael Espindola26c8dcc2012-04-04 12:51:34 +000013818 KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - 1);
Nate Begeman368e18d2006-02-16 21:11:51 +000013819 break;
Evan Cheng7c1780c2011-10-07 17:21:44 +000013820 case ISD::INTRINSIC_WO_CHAIN: {
13821 unsigned IntId = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
13822 unsigned NumLoBits = 0;
13823 switch (IntId) {
13824 default: break;
13825 case Intrinsic::x86_sse_movmsk_ps:
13826 case Intrinsic::x86_avx_movmsk_ps_256:
13827 case Intrinsic::x86_sse2_movmsk_pd:
13828 case Intrinsic::x86_avx_movmsk_pd_256:
13829 case Intrinsic::x86_mmx_pmovmskb:
Craig Topper3738ccd2011-12-27 06:27:23 +000013830 case Intrinsic::x86_sse2_pmovmskb_128:
13831 case Intrinsic::x86_avx2_pmovmskb: {
Evan Cheng7c1780c2011-10-07 17:21:44 +000013832 // High bits of movmskp{s|d}, pmovmskb are known zero.
13833 switch (IntId) {
Craig Topperabb94d02012-02-05 03:43:23 +000013834 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
Evan Cheng7c1780c2011-10-07 17:21:44 +000013835 case Intrinsic::x86_sse_movmsk_ps: NumLoBits = 4; break;
13836 case Intrinsic::x86_avx_movmsk_ps_256: NumLoBits = 8; break;
13837 case Intrinsic::x86_sse2_movmsk_pd: NumLoBits = 2; break;
13838 case Intrinsic::x86_avx_movmsk_pd_256: NumLoBits = 4; break;
13839 case Intrinsic::x86_mmx_pmovmskb: NumLoBits = 8; break;
13840 case Intrinsic::x86_sse2_pmovmskb_128: NumLoBits = 16; break;
Craig Topper3738ccd2011-12-27 06:27:23 +000013841 case Intrinsic::x86_avx2_pmovmskb: NumLoBits = 32; break;
Evan Cheng7c1780c2011-10-07 17:21:44 +000013842 }
Rafael Espindola26c8dcc2012-04-04 12:51:34 +000013843 KnownZero = APInt::getHighBitsSet(BitWidth, BitWidth - NumLoBits);
Evan Cheng7c1780c2011-10-07 17:21:44 +000013844 break;
13845 }
13846 }
13847 break;
13848 }
Evan Cheng3a03ebb2005-12-21 23:05:39 +000013849 }
Evan Cheng3a03ebb2005-12-21 23:05:39 +000013850}
Chris Lattner259e97c2006-01-31 19:43:35 +000013851
Owen Andersonbc146b02010-09-21 20:42:50 +000013852unsigned X86TargetLowering::ComputeNumSignBitsForTargetNode(SDValue Op,
13853 unsigned Depth) const {
13854 // SETCC_CARRY sets the dest to ~0 for true or 0 for false.
13855 if (Op.getOpcode() == X86ISD::SETCC_CARRY)
13856 return Op.getValueType().getScalarType().getSizeInBits();
Michael J. Spencerec38de22010-10-10 22:04:20 +000013857
Owen Andersonbc146b02010-09-21 20:42:50 +000013858 // Fallback case.
13859 return 1;
13860}
13861
Evan Cheng206ee9d2006-07-07 08:33:52 +000013862/// isGAPlusOffset - Returns true (and the GlobalValue and the offset) if the
Evan Chengad4196b2008-05-12 19:56:52 +000013863/// node is a GlobalAddress + offset.
13864bool X86TargetLowering::isGAPlusOffset(SDNode *N,
Dan Gohman46510a72010-04-15 01:51:59 +000013865 const GlobalValue* &GA,
13866 int64_t &Offset) const {
Evan Chengad4196b2008-05-12 19:56:52 +000013867 if (N->getOpcode() == X86ISD::Wrapper) {
13868 if (isa<GlobalAddressSDNode>(N->getOperand(0))) {
Evan Cheng206ee9d2006-07-07 08:33:52 +000013869 GA = cast<GlobalAddressSDNode>(N->getOperand(0))->getGlobal();
Dan Gohman6520e202008-10-18 02:06:02 +000013870 Offset = cast<GlobalAddressSDNode>(N->getOperand(0))->getOffset();
Evan Cheng206ee9d2006-07-07 08:33:52 +000013871 return true;
13872 }
Evan Cheng206ee9d2006-07-07 08:33:52 +000013873 }
Evan Chengad4196b2008-05-12 19:56:52 +000013874 return TargetLowering::isGAPlusOffset(N, GA, Offset);
Evan Cheng206ee9d2006-07-07 08:33:52 +000013875}
13876
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000013877/// isShuffleHigh128VectorInsertLow - Checks whether the shuffle node is the
13878/// same as extracting the high 128-bit part of 256-bit vector and then
13879/// inserting the result into the low part of a new 256-bit vector
13880static bool isShuffleHigh128VectorInsertLow(ShuffleVectorSDNode *SVOp) {
13881 EVT VT = SVOp->getValueType(0);
Craig Topper66ddd152012-04-27 22:54:43 +000013882 unsigned NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000013883
13884 // vector_shuffle <4, 5, 6, 7, u, u, u, u> or <2, 3, u, u>
Craig Topper66ddd152012-04-27 22:54:43 +000013885 for (unsigned i = 0, j = NumElems/2; i != NumElems/2; ++i, ++j)
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000013886 if (!isUndefOrEqual(SVOp->getMaskElt(i), j) ||
13887 SVOp->getMaskElt(j) >= 0)
13888 return false;
13889
13890 return true;
13891}
13892
13893/// isShuffleLow128VectorInsertHigh - Checks whether the shuffle node is the
13894/// same as extracting the low 128-bit part of 256-bit vector and then
13895/// inserting the result into the high part of a new 256-bit vector
13896static bool isShuffleLow128VectorInsertHigh(ShuffleVectorSDNode *SVOp) {
13897 EVT VT = SVOp->getValueType(0);
Craig Topper66ddd152012-04-27 22:54:43 +000013898 unsigned NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000013899
13900 // vector_shuffle <u, u, u, u, 0, 1, 2, 3> or <u, u, 0, 1>
Craig Topper66ddd152012-04-27 22:54:43 +000013901 for (unsigned i = NumElems/2, j = 0; i != NumElems; ++i, ++j)
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000013902 if (!isUndefOrEqual(SVOp->getMaskElt(i), j) ||
13903 SVOp->getMaskElt(j) >= 0)
13904 return false;
13905
13906 return true;
13907}
13908
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000013909/// PerformShuffleCombine256 - Performs shuffle combines for 256-bit vectors.
13910static SDValue PerformShuffleCombine256(SDNode *N, SelectionDAG &DAG,
Craig Topper12216172012-01-13 08:12:35 +000013911 TargetLowering::DAGCombinerInfo &DCI,
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +000013912 const X86Subtarget* Subtarget) {
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000013913 DebugLoc dl = N->getDebugLoc();
13914 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
13915 SDValue V1 = SVOp->getOperand(0);
13916 SDValue V2 = SVOp->getOperand(1);
13917 EVT VT = SVOp->getValueType(0);
Craig Topper66ddd152012-04-27 22:54:43 +000013918 unsigned NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000013919
13920 if (V1.getOpcode() == ISD::CONCAT_VECTORS &&
13921 V2.getOpcode() == ISD::CONCAT_VECTORS) {
13922 //
13923 // 0,0,0,...
Benjamin Kramer558cc5a2011-07-22 01:02:57 +000013924 // |
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000013925 // V UNDEF BUILD_VECTOR UNDEF
13926 // \ / \ /
13927 // CONCAT_VECTOR CONCAT_VECTOR
13928 // \ /
13929 // \ /
13930 // RESULT: V + zero extended
13931 //
13932 if (V2.getOperand(0).getOpcode() != ISD::BUILD_VECTOR ||
13933 V2.getOperand(1).getOpcode() != ISD::UNDEF ||
13934 V1.getOperand(1).getOpcode() != ISD::UNDEF)
13935 return SDValue();
13936
13937 if (!ISD::isBuildVectorAllZeros(V2.getOperand(0).getNode()))
13938 return SDValue();
13939
13940 // To match the shuffle mask, the first half of the mask should
13941 // be exactly the first vector, and all the rest a splat with the
13942 // first element of the second one.
Craig Topper66ddd152012-04-27 22:54:43 +000013943 for (unsigned i = 0; i != NumElems/2; ++i)
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000013944 if (!isUndefOrEqual(SVOp->getMaskElt(i), i) ||
13945 !isUndefOrEqual(SVOp->getMaskElt(i+NumElems/2), NumElems))
13946 return SDValue();
13947
Chad Rosier3d1161e2012-01-03 21:05:52 +000013948 // If V1 is coming from a vector load then just fold to a VZEXT_LOAD.
13949 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(V1.getOperand(0))) {
Chad Rosier42726832012-05-07 18:47:44 +000013950 if (Ld->hasNUsesOfValue(1, 0)) {
13951 SDVTList Tys = DAG.getVTList(MVT::v4i64, MVT::Other);
13952 SDValue Ops[] = { Ld->getChain(), Ld->getBasePtr() };
13953 SDValue ResNode =
13954 DAG.getMemIntrinsicNode(X86ISD::VZEXT_LOAD, dl, Tys, Ops, 2,
13955 Ld->getMemoryVT(),
13956 Ld->getPointerInfo(),
13957 Ld->getAlignment(),
13958 false/*isVolatile*/, true/*ReadMem*/,
13959 false/*WriteMem*/);
13960 return DAG.getNode(ISD::BITCAST, dl, VT, ResNode);
13961 }
Chad Rosiera20e1e72012-08-01 18:39:17 +000013962 }
Chad Rosier3d1161e2012-01-03 21:05:52 +000013963
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000013964 // Emit a zeroed vector and insert the desired subvector on its
13965 // first half.
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +000013966 SDValue Zeros = getZeroVector(VT, Subtarget, DAG, dl);
Craig Topperb14940a2012-04-22 20:55:18 +000013967 SDValue InsV = Insert128BitVector(Zeros, V1.getOperand(0), 0, DAG, dl);
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000013968 return DCI.CombineTo(N, InsV);
13969 }
13970
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000013971 //===--------------------------------------------------------------------===//
13972 // Combine some shuffles into subvector extracts and inserts:
13973 //
13974
13975 // vector_shuffle <4, 5, 6, 7, u, u, u, u> or <2, 3, u, u>
13976 if (isShuffleHigh128VectorInsertLow(SVOp)) {
Craig Topperb14940a2012-04-22 20:55:18 +000013977 SDValue V = Extract128BitVector(V1, NumElems/2, DAG, dl);
13978 SDValue InsV = Insert128BitVector(DAG.getUNDEF(VT), V, 0, DAG, dl);
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000013979 return DCI.CombineTo(N, InsV);
13980 }
13981
13982 // vector_shuffle <u, u, u, u, 0, 1, 2, 3> or <u, u, 0, 1>
13983 if (isShuffleLow128VectorInsertHigh(SVOp)) {
Craig Topperb14940a2012-04-22 20:55:18 +000013984 SDValue V = Extract128BitVector(V1, 0, DAG, dl);
13985 SDValue InsV = Insert128BitVector(DAG.getUNDEF(VT), V, NumElems/2, DAG, dl);
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000013986 return DCI.CombineTo(N, InsV);
13987 }
13988
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000013989 return SDValue();
13990}
13991
13992/// PerformShuffleCombine - Performs several different shuffle combines.
Dan Gohman475871a2008-07-27 21:46:04 +000013993static SDValue PerformShuffleCombine(SDNode *N, SelectionDAG &DAG,
Bruno Cardoso Lopes50b37c72011-08-15 21:45:54 +000013994 TargetLowering::DAGCombinerInfo &DCI,
13995 const X86Subtarget *Subtarget) {
Dale Johannesene4d209d2009-02-03 20:21:25 +000013996 DebugLoc dl = N->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +000013997 EVT VT = N->getValueType(0);
Mon P Wang1e955802009-04-03 02:43:30 +000013998
Mon P Wanga0fd0d52010-12-19 23:55:53 +000013999 // Don't create instructions with illegal types after legalize types has run.
14000 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
14001 if (!DCI.isBeforeLegalize() && !TLI.isTypeLegal(VT.getVectorElementType()))
14002 return SDValue();
14003
Bruno Cardoso Lopes50b37c72011-08-15 21:45:54 +000014004 // Combine 256-bit vector shuffles. This is only profitable when in AVX mode
Craig Topper7a9a28b2012-08-12 02:23:29 +000014005 if (Subtarget->hasAVX() && VT.is256BitVector() &&
Bruno Cardoso Lopes50b37c72011-08-15 21:45:54 +000014006 N->getOpcode() == ISD::VECTOR_SHUFFLE)
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +000014007 return PerformShuffleCombine256(N, DAG, DCI, Subtarget);
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000014008
14009 // Only handle 128 wide vector from here on.
Craig Topper7a9a28b2012-08-12 02:23:29 +000014010 if (!VT.is128BitVector())
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000014011 return SDValue();
14012
14013 // Combine a vector_shuffle that is equal to build_vector load1, load2, load3,
14014 // load4, <0, 1, 2, 3> into a 128-bit load if the load addresses are
14015 // consecutive, non-overlapping, and in the right order.
Nate Begemanfdea31a2010-03-24 20:49:50 +000014016 SmallVector<SDValue, 16> Elts;
14017 for (unsigned i = 0, e = VT.getVectorNumElements(); i != e; ++i)
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000014018 Elts.push_back(getShuffleScalarElt(N, i, DAG, 0));
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +000014019
Nate Begemanfdea31a2010-03-24 20:49:50 +000014020 return EltsFromConsecutiveLoads(VT, Elts, dl, DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +000014021}
Evan Chengd880b972008-05-09 21:53:03 +000014022
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000014023
Craig Topper55b24052012-09-11 06:15:32 +000014024/// PerformTruncateCombine - Converts truncate operation to
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000014025/// a sequence of vector shuffle operations.
14026/// It is possible when we truncate 256-bit vector to 128-bit vector
Craig Topper55b24052012-09-11 06:15:32 +000014027static SDValue PerformTruncateCombine(SDNode *N, SelectionDAG &DAG,
14028 TargetLowering::DAGCombinerInfo &DCI,
14029 const X86Subtarget *Subtarget) {
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000014030 if (!DCI.isBeforeLegalizeOps())
14031 return SDValue();
14032
Craig Topper3ef43cf2012-04-24 06:36:35 +000014033 if (!Subtarget->hasAVX())
14034 return SDValue();
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000014035
14036 EVT VT = N->getValueType(0);
14037 SDValue Op = N->getOperand(0);
14038 EVT OpVT = Op.getValueType();
14039 DebugLoc dl = N->getDebugLoc();
14040
14041 if ((VT == MVT::v4i32) && (OpVT == MVT::v4i64)) {
14042
Elena Demikhovsky1da58672012-04-22 09:39:03 +000014043 if (Subtarget->hasAVX2()) {
14044 // AVX2: v4i64 -> v4i32
14045
14046 // VPERMD
14047 static const int ShufMask[] = {0, 2, 4, 6, -1, -1, -1, -1};
14048
14049 Op = DAG.getNode(ISD::BITCAST, dl, MVT::v8i32, Op);
14050 Op = DAG.getVectorShuffle(MVT::v8i32, dl, Op, DAG.getUNDEF(MVT::v8i32),
14051 ShufMask);
14052
Craig Topperd63fa652012-04-22 18:51:37 +000014053 return DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT, Op,
14054 DAG.getIntPtrConstant(0));
Elena Demikhovsky1da58672012-04-22 09:39:03 +000014055 }
14056
14057 // AVX: v4i64 -> v4i32
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000014058 SDValue OpLo = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v2i64, Op,
Craig Topperd63fa652012-04-22 18:51:37 +000014059 DAG.getIntPtrConstant(0));
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000014060
14061 SDValue OpHi = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v2i64, Op,
Craig Topperd63fa652012-04-22 18:51:37 +000014062 DAG.getIntPtrConstant(2));
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000014063
14064 OpLo = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, OpLo);
14065 OpHi = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, OpHi);
14066
14067 // PSHUFD
Craig Topper9e401f22012-04-21 18:58:38 +000014068 static const int ShufMask1[] = {0, 2, 0, 0};
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000014069
Craig Toppercacafd42012-08-14 08:18:43 +000014070 SDValue Undef = DAG.getUNDEF(VT);
14071 OpLo = DAG.getVectorShuffle(VT, dl, OpLo, Undef, ShufMask1);
14072 OpHi = DAG.getVectorShuffle(VT, dl, OpHi, Undef, ShufMask1);
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000014073
14074 // MOVLHPS
Craig Topper9e401f22012-04-21 18:58:38 +000014075 static const int ShufMask2[] = {0, 1, 4, 5};
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000014076
Elena Demikhovsky73252572012-02-01 10:33:05 +000014077 return DAG.getVectorShuffle(VT, dl, OpLo, OpHi, ShufMask2);
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000014078 }
Craig Topperd63fa652012-04-22 18:51:37 +000014079
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000014080 if ((VT == MVT::v8i16) && (OpVT == MVT::v8i32)) {
14081
Elena Demikhovsky1da58672012-04-22 09:39:03 +000014082 if (Subtarget->hasAVX2()) {
14083 // AVX2: v8i32 -> v8i16
14084
14085 Op = DAG.getNode(ISD::BITCAST, dl, MVT::v32i8, Op);
Craig Topperd63fa652012-04-22 18:51:37 +000014086
Elena Demikhovsky1da58672012-04-22 09:39:03 +000014087 // PSHUFB
14088 SmallVector<SDValue,32> pshufbMask;
14089 for (unsigned i = 0; i < 2; ++i) {
14090 pshufbMask.push_back(DAG.getConstant(0x0, MVT::i8));
14091 pshufbMask.push_back(DAG.getConstant(0x1, MVT::i8));
14092 pshufbMask.push_back(DAG.getConstant(0x4, MVT::i8));
14093 pshufbMask.push_back(DAG.getConstant(0x5, MVT::i8));
14094 pshufbMask.push_back(DAG.getConstant(0x8, MVT::i8));
14095 pshufbMask.push_back(DAG.getConstant(0x9, MVT::i8));
14096 pshufbMask.push_back(DAG.getConstant(0xc, MVT::i8));
14097 pshufbMask.push_back(DAG.getConstant(0xd, MVT::i8));
14098 for (unsigned j = 0; j < 8; ++j)
14099 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
14100 }
Craig Topperd63fa652012-04-22 18:51:37 +000014101 SDValue BV = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v32i8,
14102 &pshufbMask[0], 32);
Elena Demikhovsky1da58672012-04-22 09:39:03 +000014103 Op = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v32i8, Op, BV);
14104
14105 Op = DAG.getNode(ISD::BITCAST, dl, MVT::v4i64, Op);
14106
14107 static const int ShufMask[] = {0, 2, -1, -1};
Craig Topperd63fa652012-04-22 18:51:37 +000014108 Op = DAG.getVectorShuffle(MVT::v4i64, dl, Op, DAG.getUNDEF(MVT::v4i64),
Elena Demikhovsky1da58672012-04-22 09:39:03 +000014109 &ShufMask[0]);
14110
Craig Topperd63fa652012-04-22 18:51:37 +000014111 Op = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v2i64, Op,
14112 DAG.getIntPtrConstant(0));
Elena Demikhovsky1da58672012-04-22 09:39:03 +000014113
14114 return DAG.getNode(ISD::BITCAST, dl, VT, Op);
14115 }
14116
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000014117 SDValue OpLo = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i32, Op,
Craig Topperd63fa652012-04-22 18:51:37 +000014118 DAG.getIntPtrConstant(0));
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000014119
14120 SDValue OpHi = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i32, Op,
Craig Topperd63fa652012-04-22 18:51:37 +000014121 DAG.getIntPtrConstant(4));
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000014122
14123 OpLo = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, OpLo);
14124 OpHi = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, OpHi);
14125
14126 // PSHUFB
Craig Topper9e401f22012-04-21 18:58:38 +000014127 static const int ShufMask1[] = {0, 1, 4, 5, 8, 9, 12, 13,
14128 -1, -1, -1, -1, -1, -1, -1, -1};
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000014129
Craig Toppercacafd42012-08-14 08:18:43 +000014130 SDValue Undef = DAG.getUNDEF(MVT::v16i8);
14131 OpLo = DAG.getVectorShuffle(MVT::v16i8, dl, OpLo, Undef, ShufMask1);
14132 OpHi = DAG.getVectorShuffle(MVT::v16i8, dl, OpHi, Undef, ShufMask1);
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000014133
14134 OpLo = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, OpLo);
14135 OpHi = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, OpHi);
14136
14137 // MOVLHPS
Craig Topper9e401f22012-04-21 18:58:38 +000014138 static const int ShufMask2[] = {0, 1, 4, 5};
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000014139
Elena Demikhovsky73252572012-02-01 10:33:05 +000014140 SDValue res = DAG.getVectorShuffle(MVT::v4i32, dl, OpLo, OpHi, ShufMask2);
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000014141 return DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, res);
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000014142 }
14143
14144 return SDValue();
14145}
14146
Craig Topper89f4e662012-03-20 07:17:59 +000014147/// XFormVExtractWithShuffleIntoLoad - Check if a vector extract from a target
14148/// specific shuffle of a load can be folded into a single element load.
14149/// Similar handling for VECTOR_SHUFFLE is performed by DAGCombiner, but
14150/// shuffles have been customed lowered so we need to handle those here.
14151static SDValue XFormVExtractWithShuffleIntoLoad(SDNode *N, SelectionDAG &DAG,
14152 TargetLowering::DAGCombinerInfo &DCI) {
14153 if (DCI.isBeforeLegalizeOps())
14154 return SDValue();
14155
14156 SDValue InVec = N->getOperand(0);
14157 SDValue EltNo = N->getOperand(1);
14158
14159 if (!isa<ConstantSDNode>(EltNo))
14160 return SDValue();
14161
14162 EVT VT = InVec.getValueType();
14163
14164 bool HasShuffleIntoBitcast = false;
14165 if (InVec.getOpcode() == ISD::BITCAST) {
14166 // Don't duplicate a load with other uses.
14167 if (!InVec.hasOneUse())
14168 return SDValue();
14169 EVT BCVT = InVec.getOperand(0).getValueType();
14170 if (BCVT.getVectorNumElements() != VT.getVectorNumElements())
14171 return SDValue();
14172 InVec = InVec.getOperand(0);
14173 HasShuffleIntoBitcast = true;
14174 }
14175
14176 if (!isTargetShuffle(InVec.getOpcode()))
14177 return SDValue();
14178
14179 // Don't duplicate a load with other uses.
14180 if (!InVec.hasOneUse())
14181 return SDValue();
14182
14183 SmallVector<int, 16> ShuffleMask;
14184 bool UnaryShuffle;
Craig Topperd978c542012-05-06 19:46:21 +000014185 if (!getTargetShuffleMask(InVec.getNode(), VT.getSimpleVT(), ShuffleMask,
14186 UnaryShuffle))
Craig Topper89f4e662012-03-20 07:17:59 +000014187 return SDValue();
14188
14189 // Select the input vector, guarding against out of range extract vector.
14190 unsigned NumElems = VT.getVectorNumElements();
14191 int Elt = cast<ConstantSDNode>(EltNo)->getZExtValue();
14192 int Idx = (Elt > (int)NumElems) ? -1 : ShuffleMask[Elt];
14193 SDValue LdNode = (Idx < (int)NumElems) ? InVec.getOperand(0)
14194 : InVec.getOperand(1);
14195
14196 // If inputs to shuffle are the same for both ops, then allow 2 uses
14197 unsigned AllowedUses = InVec.getOperand(0) == InVec.getOperand(1) ? 2 : 1;
14198
14199 if (LdNode.getOpcode() == ISD::BITCAST) {
14200 // Don't duplicate a load with other uses.
14201 if (!LdNode.getNode()->hasNUsesOfValue(AllowedUses, 0))
14202 return SDValue();
14203
14204 AllowedUses = 1; // only allow 1 load use if we have a bitcast
14205 LdNode = LdNode.getOperand(0);
14206 }
14207
14208 if (!ISD::isNormalLoad(LdNode.getNode()))
14209 return SDValue();
14210
14211 LoadSDNode *LN0 = cast<LoadSDNode>(LdNode);
14212
14213 if (!LN0 ||!LN0->hasNUsesOfValue(AllowedUses, 0) || LN0->isVolatile())
14214 return SDValue();
14215
14216 if (HasShuffleIntoBitcast) {
14217 // If there's a bitcast before the shuffle, check if the load type and
14218 // alignment is valid.
14219 unsigned Align = LN0->getAlignment();
14220 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Micah Villmow3574eca2012-10-08 16:38:25 +000014221 unsigned NewAlign = TLI.getDataLayout()->
Craig Topper89f4e662012-03-20 07:17:59 +000014222 getABITypeAlignment(VT.getTypeForEVT(*DAG.getContext()));
14223
14224 if (NewAlign > Align || !TLI.isOperationLegalOrCustom(ISD::LOAD, VT))
14225 return SDValue();
14226 }
14227
14228 // All checks match so transform back to vector_shuffle so that DAG combiner
14229 // can finish the job
14230 DebugLoc dl = N->getDebugLoc();
14231
14232 // Create shuffle node taking into account the case that its a unary shuffle
14233 SDValue Shuffle = (UnaryShuffle) ? DAG.getUNDEF(VT) : InVec.getOperand(1);
14234 Shuffle = DAG.getVectorShuffle(InVec.getValueType(), dl,
14235 InVec.getOperand(0), Shuffle,
14236 &ShuffleMask[0]);
14237 Shuffle = DAG.getNode(ISD::BITCAST, dl, VT, Shuffle);
14238 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, N->getValueType(0), Shuffle,
14239 EltNo);
14240}
14241
Bruno Cardoso Lopesb3e06692010-09-03 19:55:05 +000014242/// PerformEXTRACT_VECTOR_ELTCombine - Detect vector gather/scatter index
14243/// generation and convert it from being a bunch of shuffles and extracts
14244/// to a simple store and scalar loads to extract the elements.
Dan Gohman1bbf72b2010-03-15 23:23:03 +000014245static SDValue PerformEXTRACT_VECTOR_ELTCombine(SDNode *N, SelectionDAG &DAG,
Craig Topper89f4e662012-03-20 07:17:59 +000014246 TargetLowering::DAGCombinerInfo &DCI) {
14247 SDValue NewOp = XFormVExtractWithShuffleIntoLoad(N, DAG, DCI);
14248 if (NewOp.getNode())
14249 return NewOp;
14250
Dan Gohman1bbf72b2010-03-15 23:23:03 +000014251 SDValue InputVector = N->getOperand(0);
14252
14253 // Only operate on vectors of 4 elements, where the alternative shuffling
14254 // gets to be more expensive.
14255 if (InputVector.getValueType() != MVT::v4i32)
14256 return SDValue();
14257
14258 // Check whether every use of InputVector is an EXTRACT_VECTOR_ELT with a
14259 // single use which is a sign-extend or zero-extend, and all elements are
14260 // used.
14261 SmallVector<SDNode *, 4> Uses;
14262 unsigned ExtractedElements = 0;
14263 for (SDNode::use_iterator UI = InputVector.getNode()->use_begin(),
14264 UE = InputVector.getNode()->use_end(); UI != UE; ++UI) {
14265 if (UI.getUse().getResNo() != InputVector.getResNo())
14266 return SDValue();
14267
14268 SDNode *Extract = *UI;
14269 if (Extract->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
14270 return SDValue();
14271
14272 if (Extract->getValueType(0) != MVT::i32)
14273 return SDValue();
14274 if (!Extract->hasOneUse())
14275 return SDValue();
14276 if (Extract->use_begin()->getOpcode() != ISD::SIGN_EXTEND &&
14277 Extract->use_begin()->getOpcode() != ISD::ZERO_EXTEND)
14278 return SDValue();
14279 if (!isa<ConstantSDNode>(Extract->getOperand(1)))
14280 return SDValue();
14281
14282 // Record which element was extracted.
14283 ExtractedElements |=
14284 1 << cast<ConstantSDNode>(Extract->getOperand(1))->getZExtValue();
14285
14286 Uses.push_back(Extract);
14287 }
14288
14289 // If not all the elements were used, this may not be worthwhile.
14290 if (ExtractedElements != 15)
14291 return SDValue();
14292
14293 // Ok, we've now decided to do the transformation.
14294 DebugLoc dl = InputVector.getDebugLoc();
14295
14296 // Store the value to a temporary stack slot.
14297 SDValue StackPtr = DAG.CreateStackTemporary(InputVector.getValueType());
Chris Lattner8026a9d2010-09-21 17:50:43 +000014298 SDValue Ch = DAG.getStore(DAG.getEntryNode(), dl, InputVector, StackPtr,
14299 MachinePointerInfo(), false, false, 0);
Dan Gohman1bbf72b2010-03-15 23:23:03 +000014300
14301 // Replace each use (extract) with a load of the appropriate element.
14302 for (SmallVectorImpl<SDNode *>::iterator UI = Uses.begin(),
14303 UE = Uses.end(); UI != UE; ++UI) {
14304 SDNode *Extract = *UI;
14305
Nadav Rotem86694292011-05-17 08:31:57 +000014306 // cOMpute the element's address.
Dan Gohman1bbf72b2010-03-15 23:23:03 +000014307 SDValue Idx = Extract->getOperand(1);
14308 unsigned EltSize =
14309 InputVector.getValueType().getVectorElementType().getSizeInBits()/8;
14310 uint64_t Offset = EltSize * cast<ConstantSDNode>(Idx)->getZExtValue();
Craig Topper89f4e662012-03-20 07:17:59 +000014311 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Dan Gohman1bbf72b2010-03-15 23:23:03 +000014312 SDValue OffsetVal = DAG.getConstant(Offset, TLI.getPointerTy());
14313
Nadav Rotem86694292011-05-17 08:31:57 +000014314 SDValue ScalarAddr = DAG.getNode(ISD::ADD, dl, TLI.getPointerTy(),
Chris Lattner51abfe42010-09-21 06:02:19 +000014315 StackPtr, OffsetVal);
Dan Gohman1bbf72b2010-03-15 23:23:03 +000014316
14317 // Load the scalar.
Eric Christopher90eb4022010-07-22 00:26:08 +000014318 SDValue LoadScalar = DAG.getLoad(Extract->getValueType(0), dl, Ch,
Chris Lattner51abfe42010-09-21 06:02:19 +000014319 ScalarAddr, MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000014320 false, false, false, 0);
Dan Gohman1bbf72b2010-03-15 23:23:03 +000014321
14322 // Replace the exact with the load.
14323 DAG.ReplaceAllUsesOfValueWith(SDValue(Extract, 0), LoadScalar);
14324 }
14325
14326 // The replacement was made in place; don't return anything.
14327 return SDValue();
14328}
14329
Duncan Sands6bcd2192011-09-17 16:49:39 +000014330/// PerformSELECTCombine - Do target-specific dag combines on SELECT and VSELECT
14331/// nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000014332static SDValue PerformSELECTCombine(SDNode *N, SelectionDAG &DAG,
Nadav Rotemcc616562012-01-15 19:27:55 +000014333 TargetLowering::DAGCombinerInfo &DCI,
Chris Lattner47b4ce82009-03-11 05:48:52 +000014334 const X86Subtarget *Subtarget) {
14335 DebugLoc DL = N->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +000014336 SDValue Cond = N->getOperand(0);
Chris Lattner47b4ce82009-03-11 05:48:52 +000014337 // Get the LHS/RHS of the select.
14338 SDValue LHS = N->getOperand(1);
14339 SDValue RHS = N->getOperand(2);
Bruno Cardoso Lopes149f29f2011-09-20 22:34:45 +000014340 EVT VT = LHS.getValueType();
Eric Christopherfd179292009-08-27 18:07:15 +000014341
Dan Gohman670e5392009-09-21 18:03:22 +000014342 // If we have SSE[12] support, try to form min/max nodes. SSE min/max
Dan Gohman8ce05da2010-02-22 04:03:39 +000014343 // instructions match the semantics of the common C idiom x<y?x:y but not
14344 // x<=y?x:y, because of how they handle negative zero (which can be
14345 // ignored in unsafe-math mode).
Benjamin Kramer2c2ccbf2011-09-22 03:27:22 +000014346 if (Cond.getOpcode() == ISD::SETCC && VT.isFloatingPoint() &&
14347 VT != MVT::f80 && DAG.getTargetLoweringInfo().isTypeLegal(VT) &&
Craig Topper1accb7e2012-01-10 06:54:16 +000014348 (Subtarget->hasSSE2() ||
14349 (Subtarget->hasSSE1() && VT.getScalarType() == MVT::f32))) {
Chris Lattner47b4ce82009-03-11 05:48:52 +000014350 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000014351
Chris Lattner47b4ce82009-03-11 05:48:52 +000014352 unsigned Opcode = 0;
Dan Gohman670e5392009-09-21 18:03:22 +000014353 // Check for x CC y ? x : y.
Dan Gohmane8326932010-02-24 06:52:40 +000014354 if (DAG.isEqualTo(LHS, Cond.getOperand(0)) &&
14355 DAG.isEqualTo(RHS, Cond.getOperand(1))) {
Chris Lattner47b4ce82009-03-11 05:48:52 +000014356 switch (CC) {
14357 default: break;
Dan Gohman670e5392009-09-21 18:03:22 +000014358 case ISD::SETULT:
Dan Gohmane8326932010-02-24 06:52:40 +000014359 // Converting this to a min would handle NaNs incorrectly, and swapping
14360 // the operands would cause it to handle comparisons between positive
14361 // and negative zero incorrectly.
Evan Cheng60108e92010-07-15 22:07:12 +000014362 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)) {
Nick Lewycky8a8d4792011-12-02 22:16:29 +000014363 if (!DAG.getTarget().Options.UnsafeFPMath &&
Dan Gohmane8326932010-02-24 06:52:40 +000014364 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
14365 break;
14366 std::swap(LHS, RHS);
14367 }
Dan Gohman670e5392009-09-21 18:03:22 +000014368 Opcode = X86ISD::FMIN;
14369 break;
14370 case ISD::SETOLE:
Dan Gohmane8326932010-02-24 06:52:40 +000014371 // Converting this to a min would handle comparisons between positive
14372 // and negative zero incorrectly.
Nick Lewycky8a8d4792011-12-02 22:16:29 +000014373 if (!DAG.getTarget().Options.UnsafeFPMath &&
Dan Gohmane8326932010-02-24 06:52:40 +000014374 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(RHS))
14375 break;
Dan Gohman670e5392009-09-21 18:03:22 +000014376 Opcode = X86ISD::FMIN;
14377 break;
Chris Lattner47b4ce82009-03-11 05:48:52 +000014378 case ISD::SETULE:
Dan Gohmane8326932010-02-24 06:52:40 +000014379 // Converting this to a min would handle both negative zeros and NaNs
14380 // incorrectly, but we can swap the operands to fix both.
14381 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000014382 case ISD::SETOLT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000014383 case ISD::SETLT:
Dan Gohman670e5392009-09-21 18:03:22 +000014384 case ISD::SETLE:
Chris Lattner47b4ce82009-03-11 05:48:52 +000014385 Opcode = X86ISD::FMIN;
14386 break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000014387
Dan Gohman670e5392009-09-21 18:03:22 +000014388 case ISD::SETOGE:
Dan Gohmane8326932010-02-24 06:52:40 +000014389 // Converting this to a max would handle comparisons between positive
14390 // and negative zero incorrectly.
Nick Lewycky8a8d4792011-12-02 22:16:29 +000014391 if (!DAG.getTarget().Options.UnsafeFPMath &&
Evan Chengdd5663c2011-08-04 18:38:15 +000014392 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000014393 break;
Dan Gohman670e5392009-09-21 18:03:22 +000014394 Opcode = X86ISD::FMAX;
14395 break;
Chris Lattner47b4ce82009-03-11 05:48:52 +000014396 case ISD::SETUGT:
Dan Gohmane8326932010-02-24 06:52:40 +000014397 // Converting this to a max would handle NaNs incorrectly, and swapping
14398 // the operands would cause it to handle comparisons between positive
14399 // and negative zero incorrectly.
Evan Cheng60108e92010-07-15 22:07:12 +000014400 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)) {
Nick Lewycky8a8d4792011-12-02 22:16:29 +000014401 if (!DAG.getTarget().Options.UnsafeFPMath &&
Dan Gohmane8326932010-02-24 06:52:40 +000014402 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
14403 break;
14404 std::swap(LHS, RHS);
14405 }
Dan Gohman670e5392009-09-21 18:03:22 +000014406 Opcode = X86ISD::FMAX;
14407 break;
14408 case ISD::SETUGE:
Dan Gohmane8326932010-02-24 06:52:40 +000014409 // Converting this to a max would handle both negative zeros and NaNs
14410 // incorrectly, but we can swap the operands to fix both.
14411 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000014412 case ISD::SETOGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000014413 case ISD::SETGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000014414 case ISD::SETGE:
14415 Opcode = X86ISD::FMAX;
14416 break;
Chris Lattner83e6c992006-10-04 06:57:07 +000014417 }
Dan Gohman670e5392009-09-21 18:03:22 +000014418 // Check for x CC y ? y : x -- a min/max with reversed arms.
Dan Gohmane8326932010-02-24 06:52:40 +000014419 } else if (DAG.isEqualTo(LHS, Cond.getOperand(1)) &&
14420 DAG.isEqualTo(RHS, Cond.getOperand(0))) {
Chris Lattner47b4ce82009-03-11 05:48:52 +000014421 switch (CC) {
14422 default: break;
Dan Gohman670e5392009-09-21 18:03:22 +000014423 case ISD::SETOGE:
Dan Gohmane8326932010-02-24 06:52:40 +000014424 // Converting this to a min would handle comparisons between positive
14425 // and negative zero incorrectly, and swapping the operands would
14426 // cause it to handle NaNs incorrectly.
Nick Lewycky8a8d4792011-12-02 22:16:29 +000014427 if (!DAG.getTarget().Options.UnsafeFPMath &&
Dan Gohmane8326932010-02-24 06:52:40 +000014428 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS))) {
Evan Cheng60108e92010-07-15 22:07:12 +000014429 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000014430 break;
14431 std::swap(LHS, RHS);
14432 }
Dan Gohman670e5392009-09-21 18:03:22 +000014433 Opcode = X86ISD::FMIN;
Dan Gohman8d44b282009-09-03 20:34:31 +000014434 break;
Dan Gohman670e5392009-09-21 18:03:22 +000014435 case ISD::SETUGT:
Dan Gohmane8326932010-02-24 06:52:40 +000014436 // Converting this to a min would handle NaNs incorrectly.
Nick Lewycky8a8d4792011-12-02 22:16:29 +000014437 if (!DAG.getTarget().Options.UnsafeFPMath &&
Dan Gohmane8326932010-02-24 06:52:40 +000014438 (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)))
14439 break;
Dan Gohman670e5392009-09-21 18:03:22 +000014440 Opcode = X86ISD::FMIN;
14441 break;
14442 case ISD::SETUGE:
Dan Gohmane8326932010-02-24 06:52:40 +000014443 // Converting this to a min would handle both negative zeros and NaNs
14444 // incorrectly, but we can swap the operands to fix both.
14445 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000014446 case ISD::SETOGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000014447 case ISD::SETGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000014448 case ISD::SETGE:
14449 Opcode = X86ISD::FMIN;
14450 break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000014451
Dan Gohman670e5392009-09-21 18:03:22 +000014452 case ISD::SETULT:
Dan Gohmane8326932010-02-24 06:52:40 +000014453 // Converting this to a max would handle NaNs incorrectly.
Evan Cheng60108e92010-07-15 22:07:12 +000014454 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000014455 break;
Dan Gohman670e5392009-09-21 18:03:22 +000014456 Opcode = X86ISD::FMAX;
Dan Gohman8d44b282009-09-03 20:34:31 +000014457 break;
Dan Gohman670e5392009-09-21 18:03:22 +000014458 case ISD::SETOLE:
Dan Gohmane8326932010-02-24 06:52:40 +000014459 // Converting this to a max would handle comparisons between positive
14460 // and negative zero incorrectly, and swapping the operands would
14461 // cause it to handle NaNs incorrectly.
Nick Lewycky8a8d4792011-12-02 22:16:29 +000014462 if (!DAG.getTarget().Options.UnsafeFPMath &&
Dan Gohmane8326932010-02-24 06:52:40 +000014463 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(RHS)) {
Evan Cheng60108e92010-07-15 22:07:12 +000014464 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000014465 break;
14466 std::swap(LHS, RHS);
14467 }
Dan Gohman670e5392009-09-21 18:03:22 +000014468 Opcode = X86ISD::FMAX;
14469 break;
14470 case ISD::SETULE:
Dan Gohmane8326932010-02-24 06:52:40 +000014471 // Converting this to a max would handle both negative zeros and NaNs
14472 // incorrectly, but we can swap the operands to fix both.
14473 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000014474 case ISD::SETOLT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000014475 case ISD::SETLT:
Dan Gohman670e5392009-09-21 18:03:22 +000014476 case ISD::SETLE:
Chris Lattner47b4ce82009-03-11 05:48:52 +000014477 Opcode = X86ISD::FMAX;
14478 break;
14479 }
Chris Lattner83e6c992006-10-04 06:57:07 +000014480 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000014481
Chris Lattner47b4ce82009-03-11 05:48:52 +000014482 if (Opcode)
14483 return DAG.getNode(Opcode, DL, N->getValueType(0), LHS, RHS);
Chris Lattner83e6c992006-10-04 06:57:07 +000014484 }
Eric Christopherfd179292009-08-27 18:07:15 +000014485
Chris Lattnerd1980a52009-03-12 06:52:53 +000014486 // If this is a select between two integer constants, try to do some
14487 // optimizations.
Chris Lattnercee56e72009-03-13 05:53:31 +000014488 if (ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(LHS)) {
14489 if (ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(RHS))
Chris Lattnerd1980a52009-03-12 06:52:53 +000014490 // Don't do this for crazy integer types.
14491 if (DAG.getTargetLoweringInfo().isTypeLegal(LHS.getValueType())) {
14492 // If this is efficiently invertible, canonicalize the LHSC/RHSC values
Chris Lattnercee56e72009-03-13 05:53:31 +000014493 // so that TrueC (the true value) is larger than FalseC.
Chris Lattnerd1980a52009-03-12 06:52:53 +000014494 bool NeedsCondInvert = false;
Eric Christopherfd179292009-08-27 18:07:15 +000014495
Chris Lattnercee56e72009-03-13 05:53:31 +000014496 if (TrueC->getAPIntValue().ult(FalseC->getAPIntValue()) &&
Chris Lattnerd1980a52009-03-12 06:52:53 +000014497 // Efficiently invertible.
14498 (Cond.getOpcode() == ISD::SETCC || // setcc -> invertible.
14499 (Cond.getOpcode() == ISD::XOR && // xor(X, C) -> invertible.
14500 isa<ConstantSDNode>(Cond.getOperand(1))))) {
14501 NeedsCondInvert = true;
Chris Lattnercee56e72009-03-13 05:53:31 +000014502 std::swap(TrueC, FalseC);
Chris Lattnerd1980a52009-03-12 06:52:53 +000014503 }
Eric Christopherfd179292009-08-27 18:07:15 +000014504
Chris Lattnerd1980a52009-03-12 06:52:53 +000014505 // Optimize C ? 8 : 0 -> zext(C) << 3. Likewise for any pow2/0.
Chris Lattnercee56e72009-03-13 05:53:31 +000014506 if (FalseC->getAPIntValue() == 0 &&
14507 TrueC->getAPIntValue().isPowerOf2()) {
Chris Lattnerd1980a52009-03-12 06:52:53 +000014508 if (NeedsCondInvert) // Invert the condition if needed.
14509 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
14510 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000014511
Chris Lattnerd1980a52009-03-12 06:52:53 +000014512 // Zero extend the condition if needed.
14513 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, LHS.getValueType(), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000014514
Chris Lattnercee56e72009-03-13 05:53:31 +000014515 unsigned ShAmt = TrueC->getAPIntValue().logBase2();
Chris Lattnerd1980a52009-03-12 06:52:53 +000014516 return DAG.getNode(ISD::SHL, DL, LHS.getValueType(), Cond,
Owen Anderson825b72b2009-08-11 20:47:22 +000014517 DAG.getConstant(ShAmt, MVT::i8));
Chris Lattnerd1980a52009-03-12 06:52:53 +000014518 }
Eric Christopherfd179292009-08-27 18:07:15 +000014519
Chris Lattner97a29a52009-03-13 05:22:11 +000014520 // Optimize Cond ? cst+1 : cst -> zext(setcc(C)+cst.
Chris Lattnercee56e72009-03-13 05:53:31 +000014521 if (FalseC->getAPIntValue()+1 == TrueC->getAPIntValue()) {
Chris Lattner97a29a52009-03-13 05:22:11 +000014522 if (NeedsCondInvert) // Invert the condition if needed.
14523 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
14524 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000014525
Chris Lattner97a29a52009-03-13 05:22:11 +000014526 // Zero extend the condition if needed.
Chris Lattnercee56e72009-03-13 05:53:31 +000014527 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL,
14528 FalseC->getValueType(0), Cond);
Chris Lattner97a29a52009-03-13 05:22:11 +000014529 return DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
Chris Lattnercee56e72009-03-13 05:53:31 +000014530 SDValue(FalseC, 0));
Chris Lattner97a29a52009-03-13 05:22:11 +000014531 }
Eric Christopherfd179292009-08-27 18:07:15 +000014532
Chris Lattnercee56e72009-03-13 05:53:31 +000014533 // Optimize cases that will turn into an LEA instruction. This requires
14534 // an i32 or i64 and an efficient multiplier (1, 2, 3, 4, 5, 8, 9).
Owen Anderson825b72b2009-08-11 20:47:22 +000014535 if (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i64) {
Chris Lattnercee56e72009-03-13 05:53:31 +000014536 uint64_t Diff = TrueC->getZExtValue()-FalseC->getZExtValue();
Owen Anderson825b72b2009-08-11 20:47:22 +000014537 if (N->getValueType(0) == MVT::i32) Diff = (unsigned)Diff;
Eric Christopherfd179292009-08-27 18:07:15 +000014538
Chris Lattnercee56e72009-03-13 05:53:31 +000014539 bool isFastMultiplier = false;
14540 if (Diff < 10) {
14541 switch ((unsigned char)Diff) {
14542 default: break;
14543 case 1: // result = add base, cond
14544 case 2: // result = lea base( , cond*2)
14545 case 3: // result = lea base(cond, cond*2)
14546 case 4: // result = lea base( , cond*4)
14547 case 5: // result = lea base(cond, cond*4)
14548 case 8: // result = lea base( , cond*8)
14549 case 9: // result = lea base(cond, cond*8)
14550 isFastMultiplier = true;
14551 break;
14552 }
14553 }
Eric Christopherfd179292009-08-27 18:07:15 +000014554
Chris Lattnercee56e72009-03-13 05:53:31 +000014555 if (isFastMultiplier) {
14556 APInt Diff = TrueC->getAPIntValue()-FalseC->getAPIntValue();
14557 if (NeedsCondInvert) // Invert the condition if needed.
14558 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
14559 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000014560
Chris Lattnercee56e72009-03-13 05:53:31 +000014561 // Zero extend the condition if needed.
14562 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, FalseC->getValueType(0),
14563 Cond);
14564 // Scale the condition by the difference.
14565 if (Diff != 1)
14566 Cond = DAG.getNode(ISD::MUL, DL, Cond.getValueType(), Cond,
14567 DAG.getConstant(Diff, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000014568
Chris Lattnercee56e72009-03-13 05:53:31 +000014569 // Add the base if non-zero.
14570 if (FalseC->getAPIntValue() != 0)
14571 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
14572 SDValue(FalseC, 0));
14573 return Cond;
14574 }
Eric Christopherfd179292009-08-27 18:07:15 +000014575 }
Chris Lattnerd1980a52009-03-12 06:52:53 +000014576 }
14577 }
Eric Christopherfd179292009-08-27 18:07:15 +000014578
Evan Cheng56f582d2012-01-04 01:41:39 +000014579 // Canonicalize max and min:
14580 // (x > y) ? x : y -> (x >= y) ? x : y
14581 // (x < y) ? x : y -> (x <= y) ? x : y
14582 // This allows use of COND_S / COND_NS (see TranslateX86CC) which eliminates
14583 // the need for an extra compare
14584 // against zero. e.g.
14585 // (x - y) > 0 : (x - y) ? 0 -> (x - y) >= 0 : (x - y) ? 0
14586 // subl %esi, %edi
14587 // testl %edi, %edi
14588 // movl $0, %eax
14589 // cmovgl %edi, %eax
14590 // =>
14591 // xorl %eax, %eax
14592 // subl %esi, $edi
14593 // cmovsl %eax, %edi
14594 if (N->getOpcode() == ISD::SELECT && Cond.getOpcode() == ISD::SETCC &&
14595 DAG.isEqualTo(LHS, Cond.getOperand(0)) &&
14596 DAG.isEqualTo(RHS, Cond.getOperand(1))) {
14597 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
14598 switch (CC) {
14599 default: break;
14600 case ISD::SETLT:
14601 case ISD::SETGT: {
14602 ISD::CondCode NewCC = (CC == ISD::SETLT) ? ISD::SETLE : ISD::SETGE;
14603 Cond = DAG.getSetCC(Cond.getDebugLoc(), Cond.getValueType(),
14604 Cond.getOperand(0), Cond.getOperand(1), NewCC);
14605 return DAG.getNode(ISD::SELECT, DL, VT, Cond, LHS, RHS);
14606 }
14607 }
14608 }
14609
Nadav Rotemcc616562012-01-15 19:27:55 +000014610 // If we know that this node is legal then we know that it is going to be
14611 // matched by one of the SSE/AVX BLEND instructions. These instructions only
14612 // depend on the highest bit in each word. Try to use SimplifyDemandedBits
14613 // to simplify previous instructions.
14614 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
14615 if (N->getOpcode() == ISD::VSELECT && DCI.isBeforeLegalizeOps() &&
Nadav Rotembdcae382012-06-07 20:53:48 +000014616 !DCI.isBeforeLegalize() && TLI.isOperationLegal(ISD::VSELECT, VT)) {
Nadav Rotemcc616562012-01-15 19:27:55 +000014617 unsigned BitWidth = Cond.getValueType().getScalarType().getSizeInBits();
Nadav Rotembdcae382012-06-07 20:53:48 +000014618
14619 // Don't optimize vector selects that map to mask-registers.
14620 if (BitWidth == 1)
14621 return SDValue();
14622
Nadav Rotemcc616562012-01-15 19:27:55 +000014623 assert(BitWidth >= 8 && BitWidth <= 64 && "Invalid mask size");
14624 APInt DemandedMask = APInt::getHighBitsSet(BitWidth, 1);
14625
14626 APInt KnownZero, KnownOne;
14627 TargetLowering::TargetLoweringOpt TLO(DAG, DCI.isBeforeLegalize(),
14628 DCI.isBeforeLegalizeOps());
14629 if (TLO.ShrinkDemandedConstant(Cond, DemandedMask) ||
14630 TLI.SimplifyDemandedBits(Cond, DemandedMask, KnownZero, KnownOne, TLO))
14631 DCI.CommitTargetLoweringOpt(TLO);
14632 }
14633
Dan Gohman475871a2008-07-27 21:46:04 +000014634 return SDValue();
Chris Lattner83e6c992006-10-04 06:57:07 +000014635}
14636
Michael Liao2a33cec2012-08-10 19:58:13 +000014637// Check whether a boolean test is testing a boolean value generated by
14638// X86ISD::SETCC. If so, return the operand of that SETCC and proper condition
14639// code.
14640//
14641// Simplify the following patterns:
14642// (Op (CMP (SETCC Cond EFLAGS) 1) EQ) or
14643// (Op (CMP (SETCC Cond EFLAGS) 0) NEQ)
14644// to (Op EFLAGS Cond)
14645//
14646// (Op (CMP (SETCC Cond EFLAGS) 0) EQ) or
14647// (Op (CMP (SETCC Cond EFLAGS) 1) NEQ)
14648// to (Op EFLAGS !Cond)
14649//
14650// where Op could be BRCOND or CMOV.
14651//
Michael Liaodbf8b5b2012-08-28 03:34:40 +000014652static SDValue checkBoolTestSetCCCombine(SDValue Cmp, X86::CondCode &CC) {
Michael Liao2a33cec2012-08-10 19:58:13 +000014653 // Quit if not CMP and SUB with its value result used.
14654 if (Cmp.getOpcode() != X86ISD::CMP &&
14655 (Cmp.getOpcode() != X86ISD::SUB || Cmp.getNode()->hasAnyUseOfValue(0)))
14656 return SDValue();
14657
14658 // Quit if not used as a boolean value.
14659 if (CC != X86::COND_E && CC != X86::COND_NE)
14660 return SDValue();
14661
14662 // Check CMP operands. One of them should be 0 or 1 and the other should be
14663 // an SetCC or extended from it.
14664 SDValue Op1 = Cmp.getOperand(0);
14665 SDValue Op2 = Cmp.getOperand(1);
14666
14667 SDValue SetCC;
14668 const ConstantSDNode* C = 0;
14669 bool needOppositeCond = (CC == X86::COND_E);
14670
14671 if ((C = dyn_cast<ConstantSDNode>(Op1)))
14672 SetCC = Op2;
14673 else if ((C = dyn_cast<ConstantSDNode>(Op2)))
14674 SetCC = Op1;
14675 else // Quit if all operands are not constants.
14676 return SDValue();
14677
14678 if (C->getZExtValue() == 1)
14679 needOppositeCond = !needOppositeCond;
14680 else if (C->getZExtValue() != 0)
14681 // Quit if the constant is neither 0 or 1.
14682 return SDValue();
14683
14684 // Skip 'zext' node.
14685 if (SetCC.getOpcode() == ISD::ZERO_EXTEND)
14686 SetCC = SetCC.getOperand(0);
14687
Michael Liao7fdc66b2012-09-10 16:36:16 +000014688 switch (SetCC.getOpcode()) {
14689 case X86ISD::SETCC:
14690 // Set the condition code or opposite one if necessary.
14691 CC = X86::CondCode(SetCC.getConstantOperandVal(0));
14692 if (needOppositeCond)
14693 CC = X86::GetOppositeBranchCondition(CC);
14694 return SetCC.getOperand(1);
14695 case X86ISD::CMOV: {
14696 // Check whether false/true value has canonical one, i.e. 0 or 1.
14697 ConstantSDNode *FVal = dyn_cast<ConstantSDNode>(SetCC.getOperand(0));
14698 ConstantSDNode *TVal = dyn_cast<ConstantSDNode>(SetCC.getOperand(1));
14699 // Quit if true value is not a constant.
14700 if (!TVal)
14701 return SDValue();
14702 // Quit if false value is not a constant.
14703 if (!FVal) {
14704 // A special case for rdrand, where 0 is set if false cond is found.
14705 SDValue Op = SetCC.getOperand(0);
14706 if (Op.getOpcode() != X86ISD::RDRAND)
14707 return SDValue();
14708 }
14709 // Quit if false value is not the constant 0 or 1.
14710 bool FValIsFalse = true;
14711 if (FVal && FVal->getZExtValue() != 0) {
14712 if (FVal->getZExtValue() != 1)
14713 return SDValue();
14714 // If FVal is 1, opposite cond is needed.
14715 needOppositeCond = !needOppositeCond;
14716 FValIsFalse = false;
14717 }
14718 // Quit if TVal is not the constant opposite of FVal.
14719 if (FValIsFalse && TVal->getZExtValue() != 1)
14720 return SDValue();
14721 if (!FValIsFalse && TVal->getZExtValue() != 0)
14722 return SDValue();
14723 CC = X86::CondCode(SetCC.getConstantOperandVal(2));
14724 if (needOppositeCond)
14725 CC = X86::GetOppositeBranchCondition(CC);
14726 return SetCC.getOperand(3);
14727 }
14728 }
Michael Liao2a33cec2012-08-10 19:58:13 +000014729
Michael Liao7fdc66b2012-09-10 16:36:16 +000014730 return SDValue();
Michael Liao2a33cec2012-08-10 19:58:13 +000014731}
14732
Chris Lattnerd1980a52009-03-12 06:52:53 +000014733/// Optimize X86ISD::CMOV [LHS, RHS, CONDCODE (e.g. X86::COND_NE), CONDVAL]
14734static SDValue PerformCMOVCombine(SDNode *N, SelectionDAG &DAG,
Michael Liaodbf8b5b2012-08-28 03:34:40 +000014735 TargetLowering::DAGCombinerInfo &DCI,
14736 const X86Subtarget *Subtarget) {
Chris Lattnerd1980a52009-03-12 06:52:53 +000014737 DebugLoc DL = N->getDebugLoc();
Eric Christopherfd179292009-08-27 18:07:15 +000014738
Chris Lattnerd1980a52009-03-12 06:52:53 +000014739 // If the flag operand isn't dead, don't touch this CMOV.
14740 if (N->getNumValues() == 2 && !SDValue(N, 1).use_empty())
14741 return SDValue();
Eric Christopherfd179292009-08-27 18:07:15 +000014742
Evan Chengb5a55d92011-05-24 01:48:22 +000014743 SDValue FalseOp = N->getOperand(0);
14744 SDValue TrueOp = N->getOperand(1);
14745 X86::CondCode CC = (X86::CondCode)N->getConstantOperandVal(2);
14746 SDValue Cond = N->getOperand(3);
Michael Liao2a33cec2012-08-10 19:58:13 +000014747
Evan Chengb5a55d92011-05-24 01:48:22 +000014748 if (CC == X86::COND_E || CC == X86::COND_NE) {
14749 switch (Cond.getOpcode()) {
14750 default: break;
14751 case X86ISD::BSR:
14752 case X86ISD::BSF:
14753 // If operand of BSR / BSF are proven never zero, then ZF cannot be set.
14754 if (DAG.isKnownNeverZero(Cond.getOperand(0)))
14755 return (CC == X86::COND_E) ? FalseOp : TrueOp;
14756 }
14757 }
14758
Michael Liao2a33cec2012-08-10 19:58:13 +000014759 SDValue Flags;
14760
Michael Liaodbf8b5b2012-08-28 03:34:40 +000014761 Flags = checkBoolTestSetCCCombine(Cond, CC);
Michael Liao9eac20a2012-08-11 23:47:06 +000014762 if (Flags.getNode() &&
14763 // Extra check as FCMOV only supports a subset of X86 cond.
Michael Liao7859f432012-09-06 07:11:22 +000014764 (FalseOp.getValueType() != MVT::f80 || hasFPCMov(CC))) {
Michael Liaodbf8b5b2012-08-28 03:34:40 +000014765 SDValue Ops[] = { FalseOp, TrueOp,
14766 DAG.getConstant(CC, MVT::i8), Flags };
14767 return DAG.getNode(X86ISD::CMOV, DL, N->getVTList(),
14768 Ops, array_lengthof(Ops));
14769 }
14770
Chris Lattnerd1980a52009-03-12 06:52:53 +000014771 // If this is a select between two integer constants, try to do some
14772 // optimizations. Note that the operands are ordered the opposite of SELECT
14773 // operands.
Evan Chengb5a55d92011-05-24 01:48:22 +000014774 if (ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(TrueOp)) {
14775 if (ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(FalseOp)) {
Chris Lattnerd1980a52009-03-12 06:52:53 +000014776 // Canonicalize the TrueC/FalseC values so that TrueC (the true value) is
14777 // larger than FalseC (the false value).
Chris Lattnerd1980a52009-03-12 06:52:53 +000014778 if (TrueC->getAPIntValue().ult(FalseC->getAPIntValue())) {
14779 CC = X86::GetOppositeBranchCondition(CC);
14780 std::swap(TrueC, FalseC);
NAKAMURA Takumie2687452012-10-16 06:28:34 +000014781 std::swap(TrueOp, FalseOp);
Chris Lattnerd1980a52009-03-12 06:52:53 +000014782 }
Eric Christopherfd179292009-08-27 18:07:15 +000014783
Chris Lattnerd1980a52009-03-12 06:52:53 +000014784 // Optimize C ? 8 : 0 -> zext(setcc(C)) << 3. Likewise for any pow2/0.
Chris Lattnercee56e72009-03-13 05:53:31 +000014785 // This is efficient for any integer data type (including i8/i16) and
14786 // shift amount.
Chris Lattnerd1980a52009-03-12 06:52:53 +000014787 if (FalseC->getAPIntValue() == 0 && TrueC->getAPIntValue().isPowerOf2()) {
Owen Anderson825b72b2009-08-11 20:47:22 +000014788 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
14789 DAG.getConstant(CC, MVT::i8), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000014790
Chris Lattnerd1980a52009-03-12 06:52:53 +000014791 // Zero extend the condition if needed.
14792 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, TrueC->getValueType(0), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000014793
Chris Lattnerd1980a52009-03-12 06:52:53 +000014794 unsigned ShAmt = TrueC->getAPIntValue().logBase2();
14795 Cond = DAG.getNode(ISD::SHL, DL, Cond.getValueType(), Cond,
Owen Anderson825b72b2009-08-11 20:47:22 +000014796 DAG.getConstant(ShAmt, MVT::i8));
Chris Lattnerd1980a52009-03-12 06:52:53 +000014797 if (N->getNumValues() == 2) // Dead flag value?
14798 return DCI.CombineTo(N, Cond, SDValue());
14799 return Cond;
14800 }
Eric Christopherfd179292009-08-27 18:07:15 +000014801
Chris Lattnercee56e72009-03-13 05:53:31 +000014802 // Optimize Cond ? cst+1 : cst -> zext(setcc(C)+cst. This is efficient
14803 // for any integer data type, including i8/i16.
Chris Lattner97a29a52009-03-13 05:22:11 +000014804 if (FalseC->getAPIntValue()+1 == TrueC->getAPIntValue()) {
Owen Anderson825b72b2009-08-11 20:47:22 +000014805 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
14806 DAG.getConstant(CC, MVT::i8), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000014807
Chris Lattner97a29a52009-03-13 05:22:11 +000014808 // Zero extend the condition if needed.
Chris Lattnercee56e72009-03-13 05:53:31 +000014809 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL,
14810 FalseC->getValueType(0), Cond);
Chris Lattner97a29a52009-03-13 05:22:11 +000014811 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
14812 SDValue(FalseC, 0));
Eric Christopherfd179292009-08-27 18:07:15 +000014813
Chris Lattner97a29a52009-03-13 05:22:11 +000014814 if (N->getNumValues() == 2) // Dead flag value?
14815 return DCI.CombineTo(N, Cond, SDValue());
14816 return Cond;
14817 }
Eric Christopherfd179292009-08-27 18:07:15 +000014818
Chris Lattnercee56e72009-03-13 05:53:31 +000014819 // Optimize cases that will turn into an LEA instruction. This requires
14820 // an i32 or i64 and an efficient multiplier (1, 2, 3, 4, 5, 8, 9).
Owen Anderson825b72b2009-08-11 20:47:22 +000014821 if (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i64) {
Chris Lattnercee56e72009-03-13 05:53:31 +000014822 uint64_t Diff = TrueC->getZExtValue()-FalseC->getZExtValue();
Owen Anderson825b72b2009-08-11 20:47:22 +000014823 if (N->getValueType(0) == MVT::i32) Diff = (unsigned)Diff;
Eric Christopherfd179292009-08-27 18:07:15 +000014824
Chris Lattnercee56e72009-03-13 05:53:31 +000014825 bool isFastMultiplier = false;
14826 if (Diff < 10) {
14827 switch ((unsigned char)Diff) {
14828 default: break;
14829 case 1: // result = add base, cond
14830 case 2: // result = lea base( , cond*2)
14831 case 3: // result = lea base(cond, cond*2)
14832 case 4: // result = lea base( , cond*4)
14833 case 5: // result = lea base(cond, cond*4)
14834 case 8: // result = lea base( , cond*8)
14835 case 9: // result = lea base(cond, cond*8)
14836 isFastMultiplier = true;
14837 break;
14838 }
14839 }
Eric Christopherfd179292009-08-27 18:07:15 +000014840
Chris Lattnercee56e72009-03-13 05:53:31 +000014841 if (isFastMultiplier) {
14842 APInt Diff = TrueC->getAPIntValue()-FalseC->getAPIntValue();
Owen Anderson825b72b2009-08-11 20:47:22 +000014843 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
14844 DAG.getConstant(CC, MVT::i8), Cond);
Chris Lattnercee56e72009-03-13 05:53:31 +000014845 // Zero extend the condition if needed.
14846 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, FalseC->getValueType(0),
14847 Cond);
14848 // Scale the condition by the difference.
14849 if (Diff != 1)
14850 Cond = DAG.getNode(ISD::MUL, DL, Cond.getValueType(), Cond,
14851 DAG.getConstant(Diff, Cond.getValueType()));
14852
14853 // Add the base if non-zero.
14854 if (FalseC->getAPIntValue() != 0)
14855 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
14856 SDValue(FalseC, 0));
14857 if (N->getNumValues() == 2) // Dead flag value?
14858 return DCI.CombineTo(N, Cond, SDValue());
14859 return Cond;
14860 }
Eric Christopherfd179292009-08-27 18:07:15 +000014861 }
Chris Lattnerd1980a52009-03-12 06:52:53 +000014862 }
14863 }
NAKAMURA Takumie2687452012-10-16 06:28:34 +000014864
14865 // Handle these cases:
14866 // (select (x != c), e, c) -> select (x != c), e, x),
14867 // (select (x == c), c, e) -> select (x == c), x, e)
14868 // where the c is an integer constant, and the "select" is the combination
14869 // of CMOV and CMP.
14870 //
14871 // The rationale for this change is that the conditional-move from a constant
14872 // needs two instructions, however, conditional-move from a register needs
14873 // only one instruction.
14874 //
14875 // CAVEAT: By replacing a constant with a symbolic value, it may obscure
14876 // some instruction-combining opportunities. This opt needs to be
14877 // postponed as late as possible.
14878 //
14879 if (!DCI.isBeforeLegalize() && !DCI.isBeforeLegalizeOps()) {
14880 // the DCI.xxxx conditions are provided to postpone the optimization as
14881 // late as possible.
14882
14883 ConstantSDNode *CmpAgainst = 0;
14884 if ((Cond.getOpcode() == X86ISD::CMP || Cond.getOpcode() == X86ISD::SUB) &&
14885 (CmpAgainst = dyn_cast<ConstantSDNode>(Cond.getOperand(1))) &&
14886 dyn_cast<ConstantSDNode>(Cond.getOperand(0)) == 0) {
14887
14888 if (CC == X86::COND_NE &&
14889 CmpAgainst == dyn_cast<ConstantSDNode>(FalseOp)) {
14890 CC = X86::GetOppositeBranchCondition(CC);
14891 std::swap(TrueOp, FalseOp);
14892 }
14893
14894 if (CC == X86::COND_E &&
14895 CmpAgainst == dyn_cast<ConstantSDNode>(TrueOp)) {
14896 SDValue Ops[] = { FalseOp, Cond.getOperand(0),
14897 DAG.getConstant(CC, MVT::i8), Cond };
14898 return DAG.getNode(X86ISD::CMOV, DL, N->getVTList (), Ops,
14899 array_lengthof(Ops));
14900 }
14901 }
14902 }
14903
Chris Lattnerd1980a52009-03-12 06:52:53 +000014904 return SDValue();
14905}
14906
14907
Evan Cheng0b0cd912009-03-28 05:57:29 +000014908/// PerformMulCombine - Optimize a single multiply with constant into two
14909/// in order to implement it with two cheaper instructions, e.g.
14910/// LEA + SHL, LEA + LEA.
14911static SDValue PerformMulCombine(SDNode *N, SelectionDAG &DAG,
14912 TargetLowering::DAGCombinerInfo &DCI) {
Evan Cheng0b0cd912009-03-28 05:57:29 +000014913 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
14914 return SDValue();
14915
Owen Andersone50ed302009-08-10 22:56:29 +000014916 EVT VT = N->getValueType(0);
Owen Anderson825b72b2009-08-11 20:47:22 +000014917 if (VT != MVT::i64)
Evan Cheng0b0cd912009-03-28 05:57:29 +000014918 return SDValue();
14919
14920 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1));
14921 if (!C)
14922 return SDValue();
14923 uint64_t MulAmt = C->getZExtValue();
14924 if (isPowerOf2_64(MulAmt) || MulAmt == 3 || MulAmt == 5 || MulAmt == 9)
14925 return SDValue();
14926
14927 uint64_t MulAmt1 = 0;
14928 uint64_t MulAmt2 = 0;
14929 if ((MulAmt % 9) == 0) {
14930 MulAmt1 = 9;
14931 MulAmt2 = MulAmt / 9;
14932 } else if ((MulAmt % 5) == 0) {
14933 MulAmt1 = 5;
14934 MulAmt2 = MulAmt / 5;
14935 } else if ((MulAmt % 3) == 0) {
14936 MulAmt1 = 3;
14937 MulAmt2 = MulAmt / 3;
14938 }
14939 if (MulAmt2 &&
14940 (isPowerOf2_64(MulAmt2) || MulAmt2 == 3 || MulAmt2 == 5 || MulAmt2 == 9)){
14941 DebugLoc DL = N->getDebugLoc();
14942
14943 if (isPowerOf2_64(MulAmt2) &&
14944 !(N->hasOneUse() && N->use_begin()->getOpcode() == ISD::ADD))
14945 // If second multiplifer is pow2, issue it first. We want the multiply by
14946 // 3, 5, or 9 to be folded into the addressing mode unless the lone use
14947 // is an add.
14948 std::swap(MulAmt1, MulAmt2);
14949
14950 SDValue NewMul;
Eric Christopherfd179292009-08-27 18:07:15 +000014951 if (isPowerOf2_64(MulAmt1))
Evan Cheng0b0cd912009-03-28 05:57:29 +000014952 NewMul = DAG.getNode(ISD::SHL, DL, VT, N->getOperand(0),
Owen Anderson825b72b2009-08-11 20:47:22 +000014953 DAG.getConstant(Log2_64(MulAmt1), MVT::i8));
Evan Cheng0b0cd912009-03-28 05:57:29 +000014954 else
Evan Cheng73f24c92009-03-30 21:36:47 +000014955 NewMul = DAG.getNode(X86ISD::MUL_IMM, DL, VT, N->getOperand(0),
Evan Cheng0b0cd912009-03-28 05:57:29 +000014956 DAG.getConstant(MulAmt1, VT));
14957
Eric Christopherfd179292009-08-27 18:07:15 +000014958 if (isPowerOf2_64(MulAmt2))
Evan Cheng0b0cd912009-03-28 05:57:29 +000014959 NewMul = DAG.getNode(ISD::SHL, DL, VT, NewMul,
Owen Anderson825b72b2009-08-11 20:47:22 +000014960 DAG.getConstant(Log2_64(MulAmt2), MVT::i8));
Eric Christopherfd179292009-08-27 18:07:15 +000014961 else
Evan Cheng73f24c92009-03-30 21:36:47 +000014962 NewMul = DAG.getNode(X86ISD::MUL_IMM, DL, VT, NewMul,
Evan Cheng0b0cd912009-03-28 05:57:29 +000014963 DAG.getConstant(MulAmt2, VT));
14964
14965 // Do not add new nodes to DAG combiner worklist.
14966 DCI.CombineTo(N, NewMul, false);
14967 }
14968 return SDValue();
14969}
14970
Evan Chengad9c0a32009-12-15 00:53:42 +000014971static SDValue PerformSHLCombine(SDNode *N, SelectionDAG &DAG) {
14972 SDValue N0 = N->getOperand(0);
14973 SDValue N1 = N->getOperand(1);
14974 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
14975 EVT VT = N0.getValueType();
14976
14977 // fold (shl (and (setcc_c), c1), c2) -> (and setcc_c, (c1 << c2))
14978 // since the result of setcc_c is all zero's or all ones.
Nadav Rotemfb0dfbb2011-10-30 13:24:22 +000014979 if (VT.isInteger() && !VT.isVector() &&
14980 N1C && N0.getOpcode() == ISD::AND &&
Evan Chengad9c0a32009-12-15 00:53:42 +000014981 N0.getOperand(1).getOpcode() == ISD::Constant) {
14982 SDValue N00 = N0.getOperand(0);
14983 if (N00.getOpcode() == X86ISD::SETCC_CARRY ||
14984 ((N00.getOpcode() == ISD::ANY_EXTEND ||
14985 N00.getOpcode() == ISD::ZERO_EXTEND) &&
14986 N00.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY)) {
14987 APInt Mask = cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
14988 APInt ShAmt = N1C->getAPIntValue();
14989 Mask = Mask.shl(ShAmt);
14990 if (Mask != 0)
14991 return DAG.getNode(ISD::AND, N->getDebugLoc(), VT,
14992 N00, DAG.getConstant(Mask, VT));
14993 }
14994 }
14995
Nadav Rotemfb0dfbb2011-10-30 13:24:22 +000014996
14997 // Hardware support for vector shifts is sparse which makes us scalarize the
14998 // vector operations in many cases. Also, on sandybridge ADD is faster than
14999 // shl.
15000 // (shl V, 1) -> add V,V
15001 if (isSplatVector(N1.getNode())) {
15002 assert(N0.getValueType().isVector() && "Invalid vector shift type");
15003 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1->getOperand(0));
15004 // We shift all of the values by one. In many cases we do not have
15005 // hardware support for this operation. This is better expressed as an ADD
15006 // of two values.
15007 if (N1C && (1 == N1C->getZExtValue())) {
15008 return DAG.getNode(ISD::ADD, N->getDebugLoc(), VT, N0, N0);
15009 }
15010 }
15011
Evan Chengad9c0a32009-12-15 00:53:42 +000015012 return SDValue();
15013}
Evan Cheng0b0cd912009-03-28 05:57:29 +000015014
Nate Begeman740ab032009-01-26 00:52:55 +000015015/// PerformShiftCombine - Transforms vector shift nodes to use vector shifts
15016/// when possible.
15017static SDValue PerformShiftCombine(SDNode* N, SelectionDAG &DAG,
Mon P Wang845b1892012-02-01 22:15:20 +000015018 TargetLowering::DAGCombinerInfo &DCI,
Nate Begeman740ab032009-01-26 00:52:55 +000015019 const X86Subtarget *Subtarget) {
Evan Chengad9c0a32009-12-15 00:53:42 +000015020 EVT VT = N->getValueType(0);
Nadav Rotemfb0dfbb2011-10-30 13:24:22 +000015021 if (N->getOpcode() == ISD::SHL) {
15022 SDValue V = PerformSHLCombine(N, DAG);
15023 if (V.getNode()) return V;
15024 }
Evan Chengad9c0a32009-12-15 00:53:42 +000015025
Nate Begeman740ab032009-01-26 00:52:55 +000015026 // On X86 with SSE2 support, we can transform this to a vector shift if
15027 // all elements are shifted by the same amount. We can't do this in legalize
15028 // because the a constant vector is typically transformed to a constant pool
15029 // so we have no knowledge of the shift amount.
Craig Topper1accb7e2012-01-10 06:54:16 +000015030 if (!Subtarget->hasSSE2())
Nate Begemanc2fd67f2009-01-26 03:15:31 +000015031 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +000015032
Craig Topper7be5dfd2011-11-12 09:58:49 +000015033 if (VT != MVT::v2i64 && VT != MVT::v4i32 && VT != MVT::v8i16 &&
15034 (!Subtarget->hasAVX2() ||
15035 (VT != MVT::v4i64 && VT != MVT::v8i32 && VT != MVT::v16i16)))
Nate Begemanc2fd67f2009-01-26 03:15:31 +000015036 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +000015037
Mon P Wang3becd092009-01-28 08:12:05 +000015038 SDValue ShAmtOp = N->getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +000015039 EVT EltVT = VT.getVectorElementType();
Chris Lattner47b4ce82009-03-11 05:48:52 +000015040 DebugLoc DL = N->getDebugLoc();
Mon P Wangefa42202009-09-03 19:56:25 +000015041 SDValue BaseShAmt = SDValue();
Mon P Wang3becd092009-01-28 08:12:05 +000015042 if (ShAmtOp.getOpcode() == ISD::BUILD_VECTOR) {
15043 unsigned NumElts = VT.getVectorNumElements();
15044 unsigned i = 0;
15045 for (; i != NumElts; ++i) {
15046 SDValue Arg = ShAmtOp.getOperand(i);
15047 if (Arg.getOpcode() == ISD::UNDEF) continue;
15048 BaseShAmt = Arg;
15049 break;
15050 }
Craig Topper37c26772012-01-17 04:44:50 +000015051 // Handle the case where the build_vector is all undef
15052 // FIXME: Should DAG allow this?
15053 if (i == NumElts)
15054 return SDValue();
15055
Mon P Wang3becd092009-01-28 08:12:05 +000015056 for (; i != NumElts; ++i) {
15057 SDValue Arg = ShAmtOp.getOperand(i);
15058 if (Arg.getOpcode() == ISD::UNDEF) continue;
15059 if (Arg != BaseShAmt) {
15060 return SDValue();
15061 }
15062 }
15063 } else if (ShAmtOp.getOpcode() == ISD::VECTOR_SHUFFLE &&
Nate Begeman9008ca62009-04-27 18:41:29 +000015064 cast<ShuffleVectorSDNode>(ShAmtOp)->isSplat()) {
Mon P Wangefa42202009-09-03 19:56:25 +000015065 SDValue InVec = ShAmtOp.getOperand(0);
15066 if (InVec.getOpcode() == ISD::BUILD_VECTOR) {
15067 unsigned NumElts = InVec.getValueType().getVectorNumElements();
15068 unsigned i = 0;
15069 for (; i != NumElts; ++i) {
15070 SDValue Arg = InVec.getOperand(i);
15071 if (Arg.getOpcode() == ISD::UNDEF) continue;
15072 BaseShAmt = Arg;
15073 break;
15074 }
15075 } else if (InVec.getOpcode() == ISD::INSERT_VECTOR_ELT) {
15076 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(InVec.getOperand(2))) {
Evan Chengae3ecf92010-02-16 21:09:44 +000015077 unsigned SplatIdx= cast<ShuffleVectorSDNode>(ShAmtOp)->getSplatIndex();
Mon P Wangefa42202009-09-03 19:56:25 +000015078 if (C->getZExtValue() == SplatIdx)
15079 BaseShAmt = InVec.getOperand(1);
15080 }
15081 }
Mon P Wang845b1892012-02-01 22:15:20 +000015082 if (BaseShAmt.getNode() == 0) {
15083 // Don't create instructions with illegal types after legalize
15084 // types has run.
15085 if (!DAG.getTargetLoweringInfo().isTypeLegal(EltVT) &&
15086 !DCI.isBeforeLegalize())
15087 return SDValue();
15088
Mon P Wangefa42202009-09-03 19:56:25 +000015089 BaseShAmt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, EltVT, ShAmtOp,
15090 DAG.getIntPtrConstant(0));
Mon P Wang845b1892012-02-01 22:15:20 +000015091 }
Mon P Wang3becd092009-01-28 08:12:05 +000015092 } else
Nate Begemanc2fd67f2009-01-26 03:15:31 +000015093 return SDValue();
Nate Begeman740ab032009-01-26 00:52:55 +000015094
Mon P Wangefa42202009-09-03 19:56:25 +000015095 // The shift amount is an i32.
Owen Anderson825b72b2009-08-11 20:47:22 +000015096 if (EltVT.bitsGT(MVT::i32))
15097 BaseShAmt = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, BaseShAmt);
15098 else if (EltVT.bitsLT(MVT::i32))
Mon P Wangefa42202009-09-03 19:56:25 +000015099 BaseShAmt = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::i32, BaseShAmt);
Nate Begeman740ab032009-01-26 00:52:55 +000015100
Nate Begemanc2fd67f2009-01-26 03:15:31 +000015101 // The shift amount is identical so we can do a vector shift.
15102 SDValue ValOp = N->getOperand(0);
15103 switch (N->getOpcode()) {
15104 default:
Torok Edwinc23197a2009-07-14 16:55:14 +000015105 llvm_unreachable("Unknown shift opcode!");
Nate Begemanc2fd67f2009-01-26 03:15:31 +000015106 case ISD::SHL:
Craig Toppered2e13d2012-01-22 19:15:14 +000015107 switch (VT.getSimpleVT().SimpleTy) {
15108 default: return SDValue();
15109 case MVT::v2i64:
15110 case MVT::v4i32:
15111 case MVT::v8i16:
15112 case MVT::v4i64:
15113 case MVT::v8i32:
15114 case MVT::v16i16:
15115 return getTargetVShiftNode(X86ISD::VSHLI, DL, VT, ValOp, BaseShAmt, DAG);
15116 }
Nate Begemanc2fd67f2009-01-26 03:15:31 +000015117 case ISD::SRA:
Craig Toppered2e13d2012-01-22 19:15:14 +000015118 switch (VT.getSimpleVT().SimpleTy) {
15119 default: return SDValue();
15120 case MVT::v4i32:
15121 case MVT::v8i16:
15122 case MVT::v8i32:
15123 case MVT::v16i16:
15124 return getTargetVShiftNode(X86ISD::VSRAI, DL, VT, ValOp, BaseShAmt, DAG);
15125 }
Nate Begemanc2fd67f2009-01-26 03:15:31 +000015126 case ISD::SRL:
Craig Toppered2e13d2012-01-22 19:15:14 +000015127 switch (VT.getSimpleVT().SimpleTy) {
15128 default: return SDValue();
15129 case MVT::v2i64:
15130 case MVT::v4i32:
15131 case MVT::v8i16:
15132 case MVT::v4i64:
15133 case MVT::v8i32:
15134 case MVT::v16i16:
15135 return getTargetVShiftNode(X86ISD::VSRLI, DL, VT, ValOp, BaseShAmt, DAG);
15136 }
Nate Begeman740ab032009-01-26 00:52:55 +000015137 }
Nate Begeman740ab032009-01-26 00:52:55 +000015138}
15139
Nate Begemanb65c1752010-12-17 22:55:37 +000015140
Stuart Hastings865f0932011-06-03 23:53:54 +000015141// CMPEQCombine - Recognize the distinctive (AND (setcc ...) (setcc ..))
15142// where both setccs reference the same FP CMP, and rewrite for CMPEQSS
15143// and friends. Likewise for OR -> CMPNEQSS.
15144static SDValue CMPEQCombine(SDNode *N, SelectionDAG &DAG,
15145 TargetLowering::DAGCombinerInfo &DCI,
15146 const X86Subtarget *Subtarget) {
15147 unsigned opcode;
15148
15149 // SSE1 supports CMP{eq|ne}SS, and SSE2 added CMP{eq|ne}SD, but
15150 // we're requiring SSE2 for both.
Craig Topper1accb7e2012-01-10 06:54:16 +000015151 if (Subtarget->hasSSE2() && isAndOrOfSetCCs(SDValue(N, 0U), opcode)) {
Stuart Hastings865f0932011-06-03 23:53:54 +000015152 SDValue N0 = N->getOperand(0);
15153 SDValue N1 = N->getOperand(1);
15154 SDValue CMP0 = N0->getOperand(1);
15155 SDValue CMP1 = N1->getOperand(1);
15156 DebugLoc DL = N->getDebugLoc();
15157
15158 // The SETCCs should both refer to the same CMP.
15159 if (CMP0.getOpcode() != X86ISD::CMP || CMP0 != CMP1)
15160 return SDValue();
15161
15162 SDValue CMP00 = CMP0->getOperand(0);
15163 SDValue CMP01 = CMP0->getOperand(1);
15164 EVT VT = CMP00.getValueType();
15165
15166 if (VT == MVT::f32 || VT == MVT::f64) {
15167 bool ExpectingFlags = false;
15168 // Check for any users that want flags:
15169 for (SDNode::use_iterator UI = N->use_begin(),
15170 UE = N->use_end();
15171 !ExpectingFlags && UI != UE; ++UI)
15172 switch (UI->getOpcode()) {
15173 default:
15174 case ISD::BR_CC:
15175 case ISD::BRCOND:
15176 case ISD::SELECT:
15177 ExpectingFlags = true;
15178 break;
15179 case ISD::CopyToReg:
15180 case ISD::SIGN_EXTEND:
15181 case ISD::ZERO_EXTEND:
15182 case ISD::ANY_EXTEND:
15183 break;
15184 }
15185
15186 if (!ExpectingFlags) {
15187 enum X86::CondCode cc0 = (enum X86::CondCode)N0.getConstantOperandVal(0);
15188 enum X86::CondCode cc1 = (enum X86::CondCode)N1.getConstantOperandVal(0);
15189
15190 if (cc1 == X86::COND_E || cc1 == X86::COND_NE) {
15191 X86::CondCode tmp = cc0;
15192 cc0 = cc1;
15193 cc1 = tmp;
15194 }
15195
15196 if ((cc0 == X86::COND_E && cc1 == X86::COND_NP) ||
15197 (cc0 == X86::COND_NE && cc1 == X86::COND_P)) {
15198 bool is64BitFP = (CMP00.getValueType() == MVT::f64);
15199 X86ISD::NodeType NTOperator = is64BitFP ?
15200 X86ISD::FSETCCsd : X86ISD::FSETCCss;
15201 // FIXME: need symbolic constants for these magic numbers.
15202 // See X86ATTInstPrinter.cpp:printSSECC().
15203 unsigned x86cc = (cc0 == X86::COND_E) ? 0 : 4;
15204 SDValue OnesOrZeroesF = DAG.getNode(NTOperator, DL, MVT::f32, CMP00, CMP01,
15205 DAG.getConstant(x86cc, MVT::i8));
15206 SDValue OnesOrZeroesI = DAG.getNode(ISD::BITCAST, DL, MVT::i32,
15207 OnesOrZeroesF);
15208 SDValue ANDed = DAG.getNode(ISD::AND, DL, MVT::i32, OnesOrZeroesI,
15209 DAG.getConstant(1, MVT::i32));
15210 SDValue OneBitOfTruth = DAG.getNode(ISD::TRUNCATE, DL, MVT::i8, ANDed);
15211 return OneBitOfTruth;
15212 }
15213 }
15214 }
15215 }
15216 return SDValue();
15217}
15218
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +000015219/// CanFoldXORWithAllOnes - Test whether the XOR operand is a AllOnes vector
15220/// so it can be folded inside ANDNP.
15221static bool CanFoldXORWithAllOnes(const SDNode *N) {
15222 EVT VT = N->getValueType(0);
15223
15224 // Match direct AllOnes for 128 and 256-bit vectors
15225 if (ISD::isBuildVectorAllOnes(N))
15226 return true;
15227
15228 // Look through a bit convert.
15229 if (N->getOpcode() == ISD::BITCAST)
15230 N = N->getOperand(0).getNode();
15231
15232 // Sometimes the operand may come from a insert_subvector building a 256-bit
15233 // allones vector
Craig Topper7a9a28b2012-08-12 02:23:29 +000015234 if (VT.is256BitVector() &&
Bill Wendling456a9252011-08-04 00:32:58 +000015235 N->getOpcode() == ISD::INSERT_SUBVECTOR) {
15236 SDValue V1 = N->getOperand(0);
15237 SDValue V2 = N->getOperand(1);
15238
15239 if (V1.getOpcode() == ISD::INSERT_SUBVECTOR &&
15240 V1.getOperand(0).getOpcode() == ISD::UNDEF &&
15241 ISD::isBuildVectorAllOnes(V1.getOperand(1).getNode()) &&
15242 ISD::isBuildVectorAllOnes(V2.getNode()))
15243 return true;
15244 }
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +000015245
15246 return false;
15247}
15248
Nate Begemanb65c1752010-12-17 22:55:37 +000015249static SDValue PerformAndCombine(SDNode *N, SelectionDAG &DAG,
15250 TargetLowering::DAGCombinerInfo &DCI,
15251 const X86Subtarget *Subtarget) {
15252 if (DCI.isBeforeLegalizeOps())
15253 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000015254
Stuart Hastings865f0932011-06-03 23:53:54 +000015255 SDValue R = CMPEQCombine(N, DAG, DCI, Subtarget);
15256 if (R.getNode())
15257 return R;
15258
Craig Topper54a11172011-10-14 07:06:56 +000015259 EVT VT = N->getValueType(0);
15260
Craig Topperb4c94572011-10-21 06:55:01 +000015261 // Create ANDN, BLSI, and BLSR instructions
15262 // BLSI is X & (-X)
15263 // BLSR is X & (X-1)
Craig Topper54a11172011-10-14 07:06:56 +000015264 if (Subtarget->hasBMI() && (VT == MVT::i32 || VT == MVT::i64)) {
15265 SDValue N0 = N->getOperand(0);
15266 SDValue N1 = N->getOperand(1);
15267 DebugLoc DL = N->getDebugLoc();
15268
15269 // Check LHS for not
15270 if (N0.getOpcode() == ISD::XOR && isAllOnes(N0.getOperand(1)))
15271 return DAG.getNode(X86ISD::ANDN, DL, VT, N0.getOperand(0), N1);
15272 // Check RHS for not
15273 if (N1.getOpcode() == ISD::XOR && isAllOnes(N1.getOperand(1)))
15274 return DAG.getNode(X86ISD::ANDN, DL, VT, N1.getOperand(0), N0);
15275
Craig Topperb4c94572011-10-21 06:55:01 +000015276 // Check LHS for neg
15277 if (N0.getOpcode() == ISD::SUB && N0.getOperand(1) == N1 &&
15278 isZero(N0.getOperand(0)))
15279 return DAG.getNode(X86ISD::BLSI, DL, VT, N1);
15280
15281 // Check RHS for neg
15282 if (N1.getOpcode() == ISD::SUB && N1.getOperand(1) == N0 &&
15283 isZero(N1.getOperand(0)))
15284 return DAG.getNode(X86ISD::BLSI, DL, VT, N0);
15285
15286 // Check LHS for X-1
15287 if (N0.getOpcode() == ISD::ADD && N0.getOperand(0) == N1 &&
15288 isAllOnes(N0.getOperand(1)))
15289 return DAG.getNode(X86ISD::BLSR, DL, VT, N1);
15290
15291 // Check RHS for X-1
15292 if (N1.getOpcode() == ISD::ADD && N1.getOperand(0) == N0 &&
15293 isAllOnes(N1.getOperand(1)))
15294 return DAG.getNode(X86ISD::BLSR, DL, VT, N0);
15295
Craig Topper54a11172011-10-14 07:06:56 +000015296 return SDValue();
15297 }
15298
Bruno Cardoso Lopes466b0222011-07-13 21:36:51 +000015299 // Want to form ANDNP nodes:
15300 // 1) In the hopes of then easily combining them with OR and AND nodes
15301 // to form PBLEND/PSIGN.
15302 // 2) To match ANDN packed intrinsics
Bruno Cardoso Lopes466b0222011-07-13 21:36:51 +000015303 if (VT != MVT::v2i64 && VT != MVT::v4i64)
Nate Begemanb65c1752010-12-17 22:55:37 +000015304 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000015305
Nate Begemanb65c1752010-12-17 22:55:37 +000015306 SDValue N0 = N->getOperand(0);
15307 SDValue N1 = N->getOperand(1);
15308 DebugLoc DL = N->getDebugLoc();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000015309
Nate Begemanb65c1752010-12-17 22:55:37 +000015310 // Check LHS for vnot
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000015311 if (N0.getOpcode() == ISD::XOR &&
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +000015312 //ISD::isBuildVectorAllOnes(N0.getOperand(1).getNode()))
15313 CanFoldXORWithAllOnes(N0.getOperand(1).getNode()))
Bruno Cardoso Lopesc1af4772011-07-13 21:36:47 +000015314 return DAG.getNode(X86ISD::ANDNP, DL, VT, N0.getOperand(0), N1);
Nate Begemanb65c1752010-12-17 22:55:37 +000015315
15316 // Check RHS for vnot
15317 if (N1.getOpcode() == ISD::XOR &&
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +000015318 //ISD::isBuildVectorAllOnes(N1.getOperand(1).getNode()))
15319 CanFoldXORWithAllOnes(N1.getOperand(1).getNode()))
Bruno Cardoso Lopesc1af4772011-07-13 21:36:47 +000015320 return DAG.getNode(X86ISD::ANDNP, DL, VT, N1.getOperand(0), N0);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000015321
Nate Begemanb65c1752010-12-17 22:55:37 +000015322 return SDValue();
15323}
15324
Evan Cheng760d1942010-01-04 21:22:48 +000015325static SDValue PerformOrCombine(SDNode *N, SelectionDAG &DAG,
Evan Cheng8b1190a2010-04-28 01:18:01 +000015326 TargetLowering::DAGCombinerInfo &DCI,
Evan Cheng760d1942010-01-04 21:22:48 +000015327 const X86Subtarget *Subtarget) {
Evan Cheng39cfeec2010-04-28 02:25:18 +000015328 if (DCI.isBeforeLegalizeOps())
Evan Cheng8b1190a2010-04-28 01:18:01 +000015329 return SDValue();
15330
Stuart Hastings865f0932011-06-03 23:53:54 +000015331 SDValue R = CMPEQCombine(N, DAG, DCI, Subtarget);
15332 if (R.getNode())
15333 return R;
15334
Evan Cheng760d1942010-01-04 21:22:48 +000015335 EVT VT = N->getValueType(0);
Evan Cheng760d1942010-01-04 21:22:48 +000015336
Evan Cheng760d1942010-01-04 21:22:48 +000015337 SDValue N0 = N->getOperand(0);
15338 SDValue N1 = N->getOperand(1);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000015339
Nate Begemanb65c1752010-12-17 22:55:37 +000015340 // look for psign/blend
Craig Topper1666cb62011-11-19 07:07:26 +000015341 if (VT == MVT::v2i64 || VT == MVT::v4i64) {
Craig Topperd0a31172012-01-10 06:37:29 +000015342 if (!Subtarget->hasSSSE3() ||
Craig Topper1666cb62011-11-19 07:07:26 +000015343 (VT == MVT::v4i64 && !Subtarget->hasAVX2()))
15344 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000015345
Craig Topper1666cb62011-11-19 07:07:26 +000015346 // Canonicalize pandn to RHS
15347 if (N0.getOpcode() == X86ISD::ANDNP)
15348 std::swap(N0, N1);
Lang Hames9ffaa6a2012-01-10 22:53:20 +000015349 // or (and (m, y), (pandn m, x))
Craig Topper1666cb62011-11-19 07:07:26 +000015350 if (N0.getOpcode() == ISD::AND && N1.getOpcode() == X86ISD::ANDNP) {
15351 SDValue Mask = N1.getOperand(0);
15352 SDValue X = N1.getOperand(1);
15353 SDValue Y;
15354 if (N0.getOperand(0) == Mask)
15355 Y = N0.getOperand(1);
15356 if (N0.getOperand(1) == Mask)
15357 Y = N0.getOperand(0);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000015358
Craig Topper1666cb62011-11-19 07:07:26 +000015359 // Check to see if the mask appeared in both the AND and ANDNP and
15360 if (!Y.getNode())
15361 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000015362
Craig Topper1666cb62011-11-19 07:07:26 +000015363 // Validate that X, Y, and Mask are BIT_CONVERTS, and see through them.
Craig Topper1666cb62011-11-19 07:07:26 +000015364 // Look through mask bitcast.
Nadav Rotem4ac90812012-04-01 19:31:22 +000015365 if (Mask.getOpcode() == ISD::BITCAST)
15366 Mask = Mask.getOperand(0);
15367 if (X.getOpcode() == ISD::BITCAST)
15368 X = X.getOperand(0);
15369 if (Y.getOpcode() == ISD::BITCAST)
15370 Y = Y.getOperand(0);
15371
Craig Topper1666cb62011-11-19 07:07:26 +000015372 EVT MaskVT = Mask.getValueType();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000015373
Craig Toppered2e13d2012-01-22 19:15:14 +000015374 // Validate that the Mask operand is a vector sra node.
Craig Topper1666cb62011-11-19 07:07:26 +000015375 // FIXME: what to do for bytes, since there is a psignb/pblendvb, but
15376 // there is no psrai.b
Craig Topper7fb8b0c2012-01-23 06:46:22 +000015377 if (Mask.getOpcode() != X86ISD::VSRAI)
Craig Toppered2e13d2012-01-22 19:15:14 +000015378 return SDValue();
Craig Topper1666cb62011-11-19 07:07:26 +000015379
15380 // Check that the SRA is all signbits.
Craig Topper7fb8b0c2012-01-23 06:46:22 +000015381 SDValue SraC = Mask.getOperand(1);
Craig Topper1666cb62011-11-19 07:07:26 +000015382 unsigned SraAmt = cast<ConstantSDNode>(SraC)->getZExtValue();
15383 unsigned EltBits = MaskVT.getVectorElementType().getSizeInBits();
15384 if ((SraAmt + 1) != EltBits)
15385 return SDValue();
15386
15387 DebugLoc DL = N->getDebugLoc();
15388
15389 // Now we know we at least have a plendvb with the mask val. See if
15390 // we can form a psignb/w/d.
15391 // psign = x.type == y.type == mask.type && y = sub(0, x);
Craig Topper1666cb62011-11-19 07:07:26 +000015392 if (Y.getOpcode() == ISD::SUB && Y.getOperand(1) == X &&
15393 ISD::isBuildVectorAllZeros(Y.getOperand(0).getNode()) &&
Craig Toppered2e13d2012-01-22 19:15:14 +000015394 X.getValueType() == MaskVT && Y.getValueType() == MaskVT) {
15395 assert((EltBits == 8 || EltBits == 16 || EltBits == 32) &&
15396 "Unsupported VT for PSIGN");
Craig Topper7fb8b0c2012-01-23 06:46:22 +000015397 Mask = DAG.getNode(X86ISD::PSIGN, DL, MaskVT, X, Mask.getOperand(0));
Craig Toppered2e13d2012-01-22 19:15:14 +000015398 return DAG.getNode(ISD::BITCAST, DL, VT, Mask);
Craig Topper1666cb62011-11-19 07:07:26 +000015399 }
15400 // PBLENDVB only available on SSE 4.1
Craig Topperd0a31172012-01-10 06:37:29 +000015401 if (!Subtarget->hasSSE41())
Craig Topper1666cb62011-11-19 07:07:26 +000015402 return SDValue();
15403
15404 EVT BlendVT = (VT == MVT::v4i64) ? MVT::v32i8 : MVT::v16i8;
15405
15406 X = DAG.getNode(ISD::BITCAST, DL, BlendVT, X);
15407 Y = DAG.getNode(ISD::BITCAST, DL, BlendVT, Y);
15408 Mask = DAG.getNode(ISD::BITCAST, DL, BlendVT, Mask);
Nadav Rotem18197d72011-11-30 10:13:37 +000015409 Mask = DAG.getNode(ISD::VSELECT, DL, BlendVT, Mask, Y, X);
Craig Topper1666cb62011-11-19 07:07:26 +000015410 return DAG.getNode(ISD::BITCAST, DL, VT, Mask);
Nate Begemanb65c1752010-12-17 22:55:37 +000015411 }
15412 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000015413
Craig Topper1666cb62011-11-19 07:07:26 +000015414 if (VT != MVT::i16 && VT != MVT::i32 && VT != MVT::i64)
15415 return SDValue();
15416
Nate Begemanb65c1752010-12-17 22:55:37 +000015417 // fold (or (x << c) | (y >> (64 - c))) ==> (shld64 x, y, c)
Evan Cheng760d1942010-01-04 21:22:48 +000015418 if (N0.getOpcode() == ISD::SRL && N1.getOpcode() == ISD::SHL)
15419 std::swap(N0, N1);
15420 if (N0.getOpcode() != ISD::SHL || N1.getOpcode() != ISD::SRL)
15421 return SDValue();
Evan Cheng8b1190a2010-04-28 01:18:01 +000015422 if (!N0.hasOneUse() || !N1.hasOneUse())
15423 return SDValue();
Evan Cheng760d1942010-01-04 21:22:48 +000015424
15425 SDValue ShAmt0 = N0.getOperand(1);
15426 if (ShAmt0.getValueType() != MVT::i8)
15427 return SDValue();
15428 SDValue ShAmt1 = N1.getOperand(1);
15429 if (ShAmt1.getValueType() != MVT::i8)
15430 return SDValue();
15431 if (ShAmt0.getOpcode() == ISD::TRUNCATE)
15432 ShAmt0 = ShAmt0.getOperand(0);
15433 if (ShAmt1.getOpcode() == ISD::TRUNCATE)
15434 ShAmt1 = ShAmt1.getOperand(0);
15435
15436 DebugLoc DL = N->getDebugLoc();
15437 unsigned Opc = X86ISD::SHLD;
15438 SDValue Op0 = N0.getOperand(0);
15439 SDValue Op1 = N1.getOperand(0);
15440 if (ShAmt0.getOpcode() == ISD::SUB) {
15441 Opc = X86ISD::SHRD;
15442 std::swap(Op0, Op1);
15443 std::swap(ShAmt0, ShAmt1);
15444 }
15445
Evan Cheng8b1190a2010-04-28 01:18:01 +000015446 unsigned Bits = VT.getSizeInBits();
Evan Cheng760d1942010-01-04 21:22:48 +000015447 if (ShAmt1.getOpcode() == ISD::SUB) {
15448 SDValue Sum = ShAmt1.getOperand(0);
15449 if (ConstantSDNode *SumC = dyn_cast<ConstantSDNode>(Sum)) {
Dan Gohman4e39e9d2010-06-24 14:30:44 +000015450 SDValue ShAmt1Op1 = ShAmt1.getOperand(1);
15451 if (ShAmt1Op1.getNode()->getOpcode() == ISD::TRUNCATE)
15452 ShAmt1Op1 = ShAmt1Op1.getOperand(0);
15453 if (SumC->getSExtValue() == Bits && ShAmt1Op1 == ShAmt0)
Evan Cheng760d1942010-01-04 21:22:48 +000015454 return DAG.getNode(Opc, DL, VT,
15455 Op0, Op1,
15456 DAG.getNode(ISD::TRUNCATE, DL,
15457 MVT::i8, ShAmt0));
15458 }
15459 } else if (ConstantSDNode *ShAmt1C = dyn_cast<ConstantSDNode>(ShAmt1)) {
15460 ConstantSDNode *ShAmt0C = dyn_cast<ConstantSDNode>(ShAmt0);
15461 if (ShAmt0C &&
Evan Cheng8b1190a2010-04-28 01:18:01 +000015462 ShAmt0C->getSExtValue() + ShAmt1C->getSExtValue() == Bits)
Evan Cheng760d1942010-01-04 21:22:48 +000015463 return DAG.getNode(Opc, DL, VT,
15464 N0.getOperand(0), N1.getOperand(0),
15465 DAG.getNode(ISD::TRUNCATE, DL,
15466 MVT::i8, ShAmt0));
15467 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000015468
Evan Cheng760d1942010-01-04 21:22:48 +000015469 return SDValue();
15470}
15471
Manman Ren92363622012-06-07 22:39:10 +000015472// Generate NEG and CMOV for integer abs.
15473static SDValue performIntegerAbsCombine(SDNode *N, SelectionDAG &DAG) {
15474 EVT VT = N->getValueType(0);
15475
15476 // Since X86 does not have CMOV for 8-bit integer, we don't convert
15477 // 8-bit integer abs to NEG and CMOV.
15478 if (VT.isInteger() && VT.getSizeInBits() == 8)
15479 return SDValue();
15480
15481 SDValue N0 = N->getOperand(0);
15482 SDValue N1 = N->getOperand(1);
15483 DebugLoc DL = N->getDebugLoc();
15484
15485 // Check pattern of XOR(ADD(X,Y), Y) where Y is SRA(X, size(X)-1)
15486 // and change it to SUB and CMOV.
15487 if (VT.isInteger() && N->getOpcode() == ISD::XOR &&
15488 N0.getOpcode() == ISD::ADD &&
15489 N0.getOperand(1) == N1 &&
15490 N1.getOpcode() == ISD::SRA &&
15491 N1.getOperand(0) == N0.getOperand(0))
15492 if (ConstantSDNode *Y1C = dyn_cast<ConstantSDNode>(N1.getOperand(1)))
15493 if (Y1C->getAPIntValue() == VT.getSizeInBits()-1) {
15494 // Generate SUB & CMOV.
15495 SDValue Neg = DAG.getNode(X86ISD::SUB, DL, DAG.getVTList(VT, MVT::i32),
15496 DAG.getConstant(0, VT), N0.getOperand(0));
15497
15498 SDValue Ops[] = { N0.getOperand(0), Neg,
15499 DAG.getConstant(X86::COND_GE, MVT::i8),
15500 SDValue(Neg.getNode(), 1) };
15501 return DAG.getNode(X86ISD::CMOV, DL, DAG.getVTList(VT, MVT::Glue),
15502 Ops, array_lengthof(Ops));
15503 }
15504 return SDValue();
15505}
15506
Craig Topper3738ccd2011-12-27 06:27:23 +000015507// PerformXorCombine - Attempts to turn XOR nodes into BLSMSK nodes
Craig Topperb4c94572011-10-21 06:55:01 +000015508static SDValue PerformXorCombine(SDNode *N, SelectionDAG &DAG,
15509 TargetLowering::DAGCombinerInfo &DCI,
15510 const X86Subtarget *Subtarget) {
15511 if (DCI.isBeforeLegalizeOps())
15512 return SDValue();
15513
Manman Ren45d53b82012-06-08 18:58:26 +000015514 if (Subtarget->hasCMov()) {
15515 SDValue RV = performIntegerAbsCombine(N, DAG);
15516 if (RV.getNode())
15517 return RV;
15518 }
Manman Ren92363622012-06-07 22:39:10 +000015519
15520 // Try forming BMI if it is available.
15521 if (!Subtarget->hasBMI())
15522 return SDValue();
15523
Craig Topperb4c94572011-10-21 06:55:01 +000015524 EVT VT = N->getValueType(0);
15525
15526 if (VT != MVT::i32 && VT != MVT::i64)
15527 return SDValue();
15528
Craig Topper3738ccd2011-12-27 06:27:23 +000015529 assert(Subtarget->hasBMI() && "Creating BLSMSK requires BMI instructions");
15530
Craig Topperb4c94572011-10-21 06:55:01 +000015531 // Create BLSMSK instructions by finding X ^ (X-1)
15532 SDValue N0 = N->getOperand(0);
15533 SDValue N1 = N->getOperand(1);
15534 DebugLoc DL = N->getDebugLoc();
15535
15536 if (N0.getOpcode() == ISD::ADD && N0.getOperand(0) == N1 &&
15537 isAllOnes(N0.getOperand(1)))
15538 return DAG.getNode(X86ISD::BLSMSK, DL, VT, N1);
15539
15540 if (N1.getOpcode() == ISD::ADD && N1.getOperand(0) == N0 &&
15541 isAllOnes(N1.getOperand(1)))
15542 return DAG.getNode(X86ISD::BLSMSK, DL, VT, N0);
15543
15544 return SDValue();
15545}
15546
Nadav Rotem91e43fd2011-09-18 10:39:32 +000015547/// PerformLOADCombine - Do target-specific dag combines on LOAD nodes.
15548static SDValue PerformLOADCombine(SDNode *N, SelectionDAG &DAG,
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000015549 TargetLowering::DAGCombinerInfo &DCI,
15550 const X86Subtarget *Subtarget) {
Nadav Rotem91e43fd2011-09-18 10:39:32 +000015551 LoadSDNode *Ld = cast<LoadSDNode>(N);
15552 EVT RegVT = Ld->getValueType(0);
15553 EVT MemVT = Ld->getMemoryVT();
15554 DebugLoc dl = Ld->getDebugLoc();
15555 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
15556
15557 ISD::LoadExtType Ext = Ld->getExtensionType();
15558
Nadav Rotemca6f2962011-09-18 19:00:23 +000015559 // If this is a vector EXT Load then attempt to optimize it using a
Michael Liao35a56402012-10-17 03:59:18 +000015560 // shuffle. We need SSSE3 shuffles.
Nadav Rotem91e43fd2011-09-18 10:39:32 +000015561 // TODO: It is possible to support ZExt by zeroing the undef values
15562 // during the shuffle phase or after the shuffle.
Eli Friedmanda813f42011-12-28 21:24:44 +000015563 if (RegVT.isVector() && RegVT.isInteger() &&
Michael Liao35a56402012-10-17 03:59:18 +000015564 Ext == ISD::EXTLOAD && Subtarget->hasSSSE3()) {
Nadav Rotem91e43fd2011-09-18 10:39:32 +000015565 assert(MemVT != RegVT && "Cannot extend to the same type");
15566 assert(MemVT.isVector() && "Must load a vector from memory");
15567
15568 unsigned NumElems = RegVT.getVectorNumElements();
15569 unsigned RegSz = RegVT.getSizeInBits();
15570 unsigned MemSz = MemVT.getSizeInBits();
15571 assert(RegSz > MemSz && "Register size must be greater than the mem size");
Nadav Rotem91e43fd2011-09-18 10:39:32 +000015572
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000015573 // All sizes must be a power of two.
15574 if (!isPowerOf2_32(RegSz * MemSz * NumElems))
15575 return SDValue();
15576
15577 // Attempt to load the original value using scalar loads.
15578 // Find the largest scalar type that divides the total loaded size.
Nadav Rotem91e43fd2011-09-18 10:39:32 +000015579 MVT SclrLoadTy = MVT::i8;
15580 for (unsigned tp = MVT::FIRST_INTEGER_VALUETYPE;
15581 tp < MVT::LAST_INTEGER_VALUETYPE; ++tp) {
15582 MVT Tp = (MVT::SimpleValueType)tp;
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000015583 if (TLI.isTypeLegal(Tp) && ((MemSz % Tp.getSizeInBits()) == 0)) {
Nadav Rotem91e43fd2011-09-18 10:39:32 +000015584 SclrLoadTy = Tp;
Nadav Rotem91e43fd2011-09-18 10:39:32 +000015585 }
15586 }
15587
Nadav Rotem5cd95e12012-07-11 13:27:05 +000015588 // On 32bit systems, we can't save 64bit integers. Try bitcasting to F64.
15589 if (TLI.isTypeLegal(MVT::f64) && SclrLoadTy.getSizeInBits() < 64 &&
15590 (64 <= MemSz))
15591 SclrLoadTy = MVT::f64;
15592
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000015593 // Calculate the number of scalar loads that we need to perform
15594 // in order to load our vector from memory.
15595 unsigned NumLoads = MemSz / SclrLoadTy.getSizeInBits();
Nadav Rotem91e43fd2011-09-18 10:39:32 +000015596
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000015597 // Represent our vector as a sequence of elements which are the
15598 // largest scalar that we can load.
Nadav Rotem91e43fd2011-09-18 10:39:32 +000015599 EVT LoadUnitVecVT = EVT::getVectorVT(*DAG.getContext(), SclrLoadTy,
15600 RegSz/SclrLoadTy.getSizeInBits());
15601
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000015602 // Represent the data using the same element type that is stored in
15603 // memory. In practice, we ''widen'' MemVT.
Nadav Rotem91e43fd2011-09-18 10:39:32 +000015604 EVT WideVecVT = EVT::getVectorVT(*DAG.getContext(), MemVT.getScalarType(),
15605 RegSz/MemVT.getScalarType().getSizeInBits());
Nadav Rotem91e43fd2011-09-18 10:39:32 +000015606
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000015607 assert(WideVecVT.getSizeInBits() == LoadUnitVecVT.getSizeInBits() &&
15608 "Invalid vector type");
Nadav Rotem91e43fd2011-09-18 10:39:32 +000015609
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000015610 // We can't shuffle using an illegal type.
15611 if (!TLI.isTypeLegal(WideVecVT))
15612 return SDValue();
15613
15614 SmallVector<SDValue, 8> Chains;
15615 SDValue Ptr = Ld->getBasePtr();
15616 SDValue Increment = DAG.getConstant(SclrLoadTy.getSizeInBits()/8,
15617 TLI.getPointerTy());
15618 SDValue Res = DAG.getUNDEF(LoadUnitVecVT);
15619
15620 for (unsigned i = 0; i < NumLoads; ++i) {
15621 // Perform a single load.
15622 SDValue ScalarLoad = DAG.getLoad(SclrLoadTy, dl, Ld->getChain(),
15623 Ptr, Ld->getPointerInfo(),
15624 Ld->isVolatile(), Ld->isNonTemporal(),
15625 Ld->isInvariant(), Ld->getAlignment());
15626 Chains.push_back(ScalarLoad.getValue(1));
15627 // Create the first element type using SCALAR_TO_VECTOR in order to avoid
15628 // another round of DAGCombining.
15629 if (i == 0)
15630 Res = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, LoadUnitVecVT, ScalarLoad);
15631 else
15632 Res = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, LoadUnitVecVT, Res,
15633 ScalarLoad, DAG.getIntPtrConstant(i));
15634
15635 Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr, Increment);
15636 }
15637
15638 SDValue TF = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &Chains[0],
15639 Chains.size());
Nadav Rotem91e43fd2011-09-18 10:39:32 +000015640
15641 // Bitcast the loaded value to a vector of the original element type, in
15642 // the size of the target vector type.
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000015643 SDValue SlicedVec = DAG.getNode(ISD::BITCAST, dl, WideVecVT, Res);
Nadav Rotem91e43fd2011-09-18 10:39:32 +000015644 unsigned SizeRatio = RegSz/MemSz;
15645
15646 // Redistribute the loaded elements into the different locations.
15647 SmallVector<int, 8> ShuffleVec(NumElems * SizeRatio, -1);
Craig Topper31a207a2012-05-04 06:39:13 +000015648 for (unsigned i = 0; i != NumElems; ++i)
15649 ShuffleVec[i*SizeRatio] = i;
Nadav Rotem91e43fd2011-09-18 10:39:32 +000015650
15651 SDValue Shuff = DAG.getVectorShuffle(WideVecVT, dl, SlicedVec,
Craig Topperdf966f62012-04-22 19:17:57 +000015652 DAG.getUNDEF(WideVecVT),
15653 &ShuffleVec[0]);
Nadav Rotem91e43fd2011-09-18 10:39:32 +000015654
15655 // Bitcast to the requested type.
15656 Shuff = DAG.getNode(ISD::BITCAST, dl, RegVT, Shuff);
15657 // Replace the original load with the new sequence
15658 // and return the new chain.
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000015659 return DCI.CombineTo(N, Shuff, TF, true);
Nadav Rotem91e43fd2011-09-18 10:39:32 +000015660 }
15661
15662 return SDValue();
15663}
15664
Chris Lattner149a4e52008-02-22 02:09:43 +000015665/// PerformSTORECombine - Do target-specific dag combines on STORE nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000015666static SDValue PerformSTORECombine(SDNode *N, SelectionDAG &DAG,
Evan Cheng536e6672009-03-12 05:59:15 +000015667 const X86Subtarget *Subtarget) {
Nadav Rotem614061b2011-08-10 19:30:14 +000015668 StoreSDNode *St = cast<StoreSDNode>(N);
15669 EVT VT = St->getValue().getValueType();
15670 EVT StVT = St->getMemoryVT();
15671 DebugLoc dl = St->getDebugLoc();
Nadav Rotem5e742a32011-08-11 16:41:21 +000015672 SDValue StoredVal = St->getOperand(1);
15673 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
15674
Nick Lewycky8a8d4792011-12-02 22:16:29 +000015675 // If we are saving a concatenation of two XMM registers, perform two stores.
Nadav Rotem87d35e82012-05-19 20:30:08 +000015676 // On Sandy Bridge, 256-bit memory operations are executed by two
15677 // 128-bit ports. However, on Haswell it is better to issue a single 256-bit
15678 // memory operation.
Craig Topper7a9a28b2012-08-12 02:23:29 +000015679 if (VT.is256BitVector() && !Subtarget->hasAVX2() &&
Craig Topperb4a8aef2012-04-27 21:05:09 +000015680 StoredVal.getNode()->getOpcode() == ISD::CONCAT_VECTORS &&
15681 StoredVal.getNumOperands() == 2) {
Nadav Rotem5e742a32011-08-11 16:41:21 +000015682 SDValue Value0 = StoredVal.getOperand(0);
15683 SDValue Value1 = StoredVal.getOperand(1);
15684
15685 SDValue Stride = DAG.getConstant(16, TLI.getPointerTy());
15686 SDValue Ptr0 = St->getBasePtr();
15687 SDValue Ptr1 = DAG.getNode(ISD::ADD, dl, Ptr0.getValueType(), Ptr0, Stride);
15688
15689 SDValue Ch0 = DAG.getStore(St->getChain(), dl, Value0, Ptr0,
15690 St->getPointerInfo(), St->isVolatile(),
15691 St->isNonTemporal(), St->getAlignment());
15692 SDValue Ch1 = DAG.getStore(St->getChain(), dl, Value1, Ptr1,
15693 St->getPointerInfo(), St->isVolatile(),
15694 St->isNonTemporal(), St->getAlignment());
15695 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Ch0, Ch1);
15696 }
Nadav Rotem614061b2011-08-10 19:30:14 +000015697
15698 // Optimize trunc store (of multiple scalars) to shuffle and store.
15699 // First, pack all of the elements in one place. Next, store to memory
15700 // in fewer chunks.
15701 if (St->isTruncatingStore() && VT.isVector()) {
15702 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
15703 unsigned NumElems = VT.getVectorNumElements();
15704 assert(StVT != VT && "Cannot truncate to the same type");
15705 unsigned FromSz = VT.getVectorElementType().getSizeInBits();
15706 unsigned ToSz = StVT.getVectorElementType().getSizeInBits();
15707
15708 // From, To sizes and ElemCount must be pow of two
15709 if (!isPowerOf2_32(NumElems * FromSz * ToSz)) return SDValue();
Nadav Rotem9c6cdf42011-09-21 08:45:10 +000015710 // We are going to use the original vector elt for storing.
Nadav Rotem64ac73b2011-09-21 17:14:40 +000015711 // Accumulated smaller vector elements must be a multiple of the store size.
Nadav Rotem9c6cdf42011-09-21 08:45:10 +000015712 if (0 != (NumElems * FromSz) % ToSz) return SDValue();
Nadav Rotem91e43fd2011-09-18 10:39:32 +000015713
Nadav Rotem614061b2011-08-10 19:30:14 +000015714 unsigned SizeRatio = FromSz / ToSz;
15715
15716 assert(SizeRatio * NumElems * ToSz == VT.getSizeInBits());
15717
15718 // Create a type on which we perform the shuffle
15719 EVT WideVecVT = EVT::getVectorVT(*DAG.getContext(),
15720 StVT.getScalarType(), NumElems*SizeRatio);
15721
15722 assert(WideVecVT.getSizeInBits() == VT.getSizeInBits());
15723
15724 SDValue WideVec = DAG.getNode(ISD::BITCAST, dl, WideVecVT, St->getValue());
15725 SmallVector<int, 8> ShuffleVec(NumElems * SizeRatio, -1);
Craig Topper31a207a2012-05-04 06:39:13 +000015726 for (unsigned i = 0; i != NumElems; ++i)
15727 ShuffleVec[i] = i * SizeRatio;
Nadav Rotem614061b2011-08-10 19:30:14 +000015728
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000015729 // Can't shuffle using an illegal type.
15730 if (!TLI.isTypeLegal(WideVecVT))
15731 return SDValue();
Nadav Rotem614061b2011-08-10 19:30:14 +000015732
15733 SDValue Shuff = DAG.getVectorShuffle(WideVecVT, dl, WideVec,
Craig Topperdf966f62012-04-22 19:17:57 +000015734 DAG.getUNDEF(WideVecVT),
15735 &ShuffleVec[0]);
Nadav Rotem614061b2011-08-10 19:30:14 +000015736 // At this point all of the data is stored at the bottom of the
15737 // register. We now need to save it to mem.
15738
15739 // Find the largest store unit
15740 MVT StoreType = MVT::i8;
15741 for (unsigned tp = MVT::FIRST_INTEGER_VALUETYPE;
15742 tp < MVT::LAST_INTEGER_VALUETYPE; ++tp) {
15743 MVT Tp = (MVT::SimpleValueType)tp;
Nadav Rotem5cd95e12012-07-11 13:27:05 +000015744 if (TLI.isTypeLegal(Tp) && Tp.getSizeInBits() <= NumElems * ToSz)
Nadav Rotem614061b2011-08-10 19:30:14 +000015745 StoreType = Tp;
15746 }
15747
Nadav Rotem5cd95e12012-07-11 13:27:05 +000015748 // On 32bit systems, we can't save 64bit integers. Try bitcasting to F64.
15749 if (TLI.isTypeLegal(MVT::f64) && StoreType.getSizeInBits() < 64 &&
15750 (64 <= NumElems * ToSz))
15751 StoreType = MVT::f64;
15752
Nadav Rotem614061b2011-08-10 19:30:14 +000015753 // Bitcast the original vector into a vector of store-size units
15754 EVT StoreVecVT = EVT::getVectorVT(*DAG.getContext(),
Nadav Rotem5cd95e12012-07-11 13:27:05 +000015755 StoreType, VT.getSizeInBits()/StoreType.getSizeInBits());
Nadav Rotem614061b2011-08-10 19:30:14 +000015756 assert(StoreVecVT.getSizeInBits() == VT.getSizeInBits());
15757 SDValue ShuffWide = DAG.getNode(ISD::BITCAST, dl, StoreVecVT, Shuff);
15758 SmallVector<SDValue, 8> Chains;
15759 SDValue Increment = DAG.getConstant(StoreType.getSizeInBits()/8,
15760 TLI.getPointerTy());
15761 SDValue Ptr = St->getBasePtr();
15762
15763 // Perform one or more big stores into memory.
Craig Topper31a207a2012-05-04 06:39:13 +000015764 for (unsigned i=0, e=(ToSz*NumElems)/StoreType.getSizeInBits(); i!=e; ++i) {
Nadav Rotem614061b2011-08-10 19:30:14 +000015765 SDValue SubVec = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl,
15766 StoreType, ShuffWide,
15767 DAG.getIntPtrConstant(i));
15768 SDValue Ch = DAG.getStore(St->getChain(), dl, SubVec, Ptr,
15769 St->getPointerInfo(), St->isVolatile(),
15770 St->isNonTemporal(), St->getAlignment());
15771 Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr, Increment);
15772 Chains.push_back(Ch);
15773 }
15774
15775 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &Chains[0],
15776 Chains.size());
15777 }
15778
15779
Chris Lattner149a4e52008-02-22 02:09:43 +000015780 // Turn load->store of MMX types into GPR load/stores. This avoids clobbering
15781 // the FP state in cases where an emms may be missing.
Dale Johannesen079f2a62008-02-25 19:20:14 +000015782 // A preferable solution to the general problem is to figure out the right
15783 // places to insert EMMS. This qualifies as a quick hack.
Evan Cheng536e6672009-03-12 05:59:15 +000015784
15785 // Similarly, turn load->store of i64 into double load/stores in 32-bit mode.
Evan Cheng536e6672009-03-12 05:59:15 +000015786 if (VT.getSizeInBits() != 64)
15787 return SDValue();
15788
Devang Patel578efa92009-06-05 21:57:13 +000015789 const Function *F = DAG.getMachineFunction().getFunction();
Bill Wendling67658342012-10-09 07:45:08 +000015790 bool NoImplicitFloatOps = F->getFnAttributes().
15791 hasAttribute(Attributes::NoImplicitFloat);
Nick Lewycky8a8d4792011-12-02 22:16:29 +000015792 bool F64IsLegal = !DAG.getTarget().Options.UseSoftFloat && !NoImplicitFloatOps
Craig Topper1accb7e2012-01-10 06:54:16 +000015793 && Subtarget->hasSSE2();
Evan Cheng536e6672009-03-12 05:59:15 +000015794 if ((VT.isVector() ||
Owen Anderson825b72b2009-08-11 20:47:22 +000015795 (VT == MVT::i64 && F64IsLegal && !Subtarget->is64Bit())) &&
Dale Johannesen079f2a62008-02-25 19:20:14 +000015796 isa<LoadSDNode>(St->getValue()) &&
15797 !cast<LoadSDNode>(St->getValue())->isVolatile() &&
15798 St->getChain().hasOneUse() && !St->isVolatile()) {
Gabor Greifba36cb52008-08-28 21:40:38 +000015799 SDNode* LdVal = St->getValue().getNode();
Dale Johannesen079f2a62008-02-25 19:20:14 +000015800 LoadSDNode *Ld = 0;
15801 int TokenFactorIndex = -1;
Dan Gohman475871a2008-07-27 21:46:04 +000015802 SmallVector<SDValue, 8> Ops;
Gabor Greifba36cb52008-08-28 21:40:38 +000015803 SDNode* ChainVal = St->getChain().getNode();
Dale Johannesen079f2a62008-02-25 19:20:14 +000015804 // Must be a store of a load. We currently handle two cases: the load
15805 // is a direct child, and it's under an intervening TokenFactor. It is
15806 // possible to dig deeper under nested TokenFactors.
Dale Johannesen14e2ea92008-02-25 22:29:22 +000015807 if (ChainVal == LdVal)
Dale Johannesen079f2a62008-02-25 19:20:14 +000015808 Ld = cast<LoadSDNode>(St->getChain());
15809 else if (St->getValue().hasOneUse() &&
15810 ChainVal->getOpcode() == ISD::TokenFactor) {
Chad Rosierc2348d52012-02-01 18:45:51 +000015811 for (unsigned i = 0, e = ChainVal->getNumOperands(); i != e; ++i) {
Gabor Greifba36cb52008-08-28 21:40:38 +000015812 if (ChainVal->getOperand(i).getNode() == LdVal) {
Dale Johannesen079f2a62008-02-25 19:20:14 +000015813 TokenFactorIndex = i;
15814 Ld = cast<LoadSDNode>(St->getValue());
15815 } else
15816 Ops.push_back(ChainVal->getOperand(i));
15817 }
15818 }
Dale Johannesen079f2a62008-02-25 19:20:14 +000015819
Evan Cheng536e6672009-03-12 05:59:15 +000015820 if (!Ld || !ISD::isNormalLoad(Ld))
15821 return SDValue();
Dale Johannesen079f2a62008-02-25 19:20:14 +000015822
Evan Cheng536e6672009-03-12 05:59:15 +000015823 // If this is not the MMX case, i.e. we are just turning i64 load/store
15824 // into f64 load/store, avoid the transformation if there are multiple
15825 // uses of the loaded value.
15826 if (!VT.isVector() && !Ld->hasNUsesOfValue(1, 0))
15827 return SDValue();
Dale Johannesen079f2a62008-02-25 19:20:14 +000015828
Evan Cheng536e6672009-03-12 05:59:15 +000015829 DebugLoc LdDL = Ld->getDebugLoc();
15830 DebugLoc StDL = N->getDebugLoc();
15831 // If we are a 64-bit capable x86, lower to a single movq load/store pair.
15832 // Otherwise, if it's legal to use f64 SSE instructions, use f64 load/store
15833 // pair instead.
15834 if (Subtarget->is64Bit() || F64IsLegal) {
Owen Anderson825b72b2009-08-11 20:47:22 +000015835 EVT LdVT = Subtarget->is64Bit() ? MVT::i64 : MVT::f64;
Chris Lattner51abfe42010-09-21 06:02:19 +000015836 SDValue NewLd = DAG.getLoad(LdVT, LdDL, Ld->getChain(), Ld->getBasePtr(),
15837 Ld->getPointerInfo(), Ld->isVolatile(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000015838 Ld->isNonTemporal(), Ld->isInvariant(),
15839 Ld->getAlignment());
Evan Cheng536e6672009-03-12 05:59:15 +000015840 SDValue NewChain = NewLd.getValue(1);
Dale Johannesen079f2a62008-02-25 19:20:14 +000015841 if (TokenFactorIndex != -1) {
Evan Cheng536e6672009-03-12 05:59:15 +000015842 Ops.push_back(NewChain);
Owen Anderson825b72b2009-08-11 20:47:22 +000015843 NewChain = DAG.getNode(ISD::TokenFactor, LdDL, MVT::Other, &Ops[0],
Dale Johannesen079f2a62008-02-25 19:20:14 +000015844 Ops.size());
15845 }
Evan Cheng536e6672009-03-12 05:59:15 +000015846 return DAG.getStore(NewChain, StDL, NewLd, St->getBasePtr(),
Chris Lattner51abfe42010-09-21 06:02:19 +000015847 St->getPointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +000015848 St->isVolatile(), St->isNonTemporal(),
15849 St->getAlignment());
Chris Lattner149a4e52008-02-22 02:09:43 +000015850 }
Evan Cheng536e6672009-03-12 05:59:15 +000015851
15852 // Otherwise, lower to two pairs of 32-bit loads / stores.
15853 SDValue LoAddr = Ld->getBasePtr();
Owen Anderson825b72b2009-08-11 20:47:22 +000015854 SDValue HiAddr = DAG.getNode(ISD::ADD, LdDL, MVT::i32, LoAddr,
15855 DAG.getConstant(4, MVT::i32));
Evan Cheng536e6672009-03-12 05:59:15 +000015856
Owen Anderson825b72b2009-08-11 20:47:22 +000015857 SDValue LoLd = DAG.getLoad(MVT::i32, LdDL, Ld->getChain(), LoAddr,
Chris Lattner51abfe42010-09-21 06:02:19 +000015858 Ld->getPointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +000015859 Ld->isVolatile(), Ld->isNonTemporal(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000015860 Ld->isInvariant(), Ld->getAlignment());
Owen Anderson825b72b2009-08-11 20:47:22 +000015861 SDValue HiLd = DAG.getLoad(MVT::i32, LdDL, Ld->getChain(), HiAddr,
Chris Lattner51abfe42010-09-21 06:02:19 +000015862 Ld->getPointerInfo().getWithOffset(4),
David Greene67c9d422010-02-15 16:53:33 +000015863 Ld->isVolatile(), Ld->isNonTemporal(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000015864 Ld->isInvariant(),
Evan Cheng536e6672009-03-12 05:59:15 +000015865 MinAlign(Ld->getAlignment(), 4));
15866
15867 SDValue NewChain = LoLd.getValue(1);
15868 if (TokenFactorIndex != -1) {
15869 Ops.push_back(LoLd);
15870 Ops.push_back(HiLd);
Owen Anderson825b72b2009-08-11 20:47:22 +000015871 NewChain = DAG.getNode(ISD::TokenFactor, LdDL, MVT::Other, &Ops[0],
Evan Cheng536e6672009-03-12 05:59:15 +000015872 Ops.size());
15873 }
15874
15875 LoAddr = St->getBasePtr();
Owen Anderson825b72b2009-08-11 20:47:22 +000015876 HiAddr = DAG.getNode(ISD::ADD, StDL, MVT::i32, LoAddr,
15877 DAG.getConstant(4, MVT::i32));
Evan Cheng536e6672009-03-12 05:59:15 +000015878
15879 SDValue LoSt = DAG.getStore(NewChain, StDL, LoLd, LoAddr,
Chris Lattner8026a9d2010-09-21 17:50:43 +000015880 St->getPointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +000015881 St->isVolatile(), St->isNonTemporal(),
15882 St->getAlignment());
Evan Cheng536e6672009-03-12 05:59:15 +000015883 SDValue HiSt = DAG.getStore(NewChain, StDL, HiLd, HiAddr,
Chris Lattner8026a9d2010-09-21 17:50:43 +000015884 St->getPointerInfo().getWithOffset(4),
Evan Cheng536e6672009-03-12 05:59:15 +000015885 St->isVolatile(),
David Greene67c9d422010-02-15 16:53:33 +000015886 St->isNonTemporal(),
Evan Cheng536e6672009-03-12 05:59:15 +000015887 MinAlign(St->getAlignment(), 4));
Owen Anderson825b72b2009-08-11 20:47:22 +000015888 return DAG.getNode(ISD::TokenFactor, StDL, MVT::Other, LoSt, HiSt);
Chris Lattner149a4e52008-02-22 02:09:43 +000015889 }
Dan Gohman475871a2008-07-27 21:46:04 +000015890 return SDValue();
Chris Lattner149a4e52008-02-22 02:09:43 +000015891}
15892
Duncan Sands17470be2011-09-22 20:15:48 +000015893/// isHorizontalBinOp - Return 'true' if this vector operation is "horizontal"
15894/// and return the operands for the horizontal operation in LHS and RHS. A
15895/// horizontal operation performs the binary operation on successive elements
15896/// of its first operand, then on successive elements of its second operand,
15897/// returning the resulting values in a vector. For example, if
15898/// A = < float a0, float a1, float a2, float a3 >
15899/// and
15900/// B = < float b0, float b1, float b2, float b3 >
15901/// then the result of doing a horizontal operation on A and B is
15902/// A horizontal-op B = < a0 op a1, a2 op a3, b0 op b1, b2 op b3 >.
15903/// In short, LHS and RHS are inspected to see if LHS op RHS is of the form
15904/// A horizontal-op B, for some already available A and B, and if so then LHS is
15905/// set to A, RHS to B, and the routine returns 'true'.
15906/// Note that the binary operation should have the property that if one of the
15907/// operands is UNDEF then the result is UNDEF.
Craig Topperbeabc6c2011-12-05 06:56:46 +000015908static bool isHorizontalBinOp(SDValue &LHS, SDValue &RHS, bool IsCommutative) {
Duncan Sands17470be2011-09-22 20:15:48 +000015909 // Look for the following pattern: if
15910 // A = < float a0, float a1, float a2, float a3 >
15911 // B = < float b0, float b1, float b2, float b3 >
15912 // and
15913 // LHS = VECTOR_SHUFFLE A, B, <0, 2, 4, 6>
15914 // RHS = VECTOR_SHUFFLE A, B, <1, 3, 5, 7>
15915 // then LHS op RHS = < a0 op a1, a2 op a3, b0 op b1, b2 op b3 >
15916 // which is A horizontal-op B.
15917
15918 // At least one of the operands should be a vector shuffle.
15919 if (LHS.getOpcode() != ISD::VECTOR_SHUFFLE &&
15920 RHS.getOpcode() != ISD::VECTOR_SHUFFLE)
15921 return false;
15922
15923 EVT VT = LHS.getValueType();
Craig Topperf8363302011-12-02 08:18:41 +000015924
15925 assert((VT.is128BitVector() || VT.is256BitVector()) &&
15926 "Unsupported vector type for horizontal add/sub");
15927
15928 // Handle 128 and 256-bit vector lengths. AVX defines horizontal add/sub to
15929 // operate independently on 128-bit lanes.
Craig Topperb72039c2011-11-30 09:10:50 +000015930 unsigned NumElts = VT.getVectorNumElements();
15931 unsigned NumLanes = VT.getSizeInBits()/128;
15932 unsigned NumLaneElts = NumElts / NumLanes;
Craig Topperf8363302011-12-02 08:18:41 +000015933 assert((NumLaneElts % 2 == 0) &&
15934 "Vector type should have an even number of elements in each lane");
15935 unsigned HalfLaneElts = NumLaneElts/2;
Duncan Sands17470be2011-09-22 20:15:48 +000015936
15937 // View LHS in the form
15938 // LHS = VECTOR_SHUFFLE A, B, LMask
15939 // If LHS is not a shuffle then pretend it is the shuffle
15940 // LHS = VECTOR_SHUFFLE LHS, undef, <0, 1, ..., N-1>
15941 // NOTE: in what follows a default initialized SDValue represents an UNDEF of
15942 // type VT.
15943 SDValue A, B;
Craig Topperb72039c2011-11-30 09:10:50 +000015944 SmallVector<int, 16> LMask(NumElts);
Duncan Sands17470be2011-09-22 20:15:48 +000015945 if (LHS.getOpcode() == ISD::VECTOR_SHUFFLE) {
15946 if (LHS.getOperand(0).getOpcode() != ISD::UNDEF)
15947 A = LHS.getOperand(0);
15948 if (LHS.getOperand(1).getOpcode() != ISD::UNDEF)
15949 B = LHS.getOperand(1);
Benjamin Kramered4c8c62012-01-15 13:16:05 +000015950 ArrayRef<int> Mask = cast<ShuffleVectorSDNode>(LHS.getNode())->getMask();
15951 std::copy(Mask.begin(), Mask.end(), LMask.begin());
Duncan Sands17470be2011-09-22 20:15:48 +000015952 } else {
15953 if (LHS.getOpcode() != ISD::UNDEF)
15954 A = LHS;
Craig Topperb72039c2011-11-30 09:10:50 +000015955 for (unsigned i = 0; i != NumElts; ++i)
Duncan Sands17470be2011-09-22 20:15:48 +000015956 LMask[i] = i;
15957 }
15958
15959 // Likewise, view RHS in the form
15960 // RHS = VECTOR_SHUFFLE C, D, RMask
15961 SDValue C, D;
Craig Topperb72039c2011-11-30 09:10:50 +000015962 SmallVector<int, 16> RMask(NumElts);
Duncan Sands17470be2011-09-22 20:15:48 +000015963 if (RHS.getOpcode() == ISD::VECTOR_SHUFFLE) {
15964 if (RHS.getOperand(0).getOpcode() != ISD::UNDEF)
15965 C = RHS.getOperand(0);
15966 if (RHS.getOperand(1).getOpcode() != ISD::UNDEF)
15967 D = RHS.getOperand(1);
Benjamin Kramered4c8c62012-01-15 13:16:05 +000015968 ArrayRef<int> Mask = cast<ShuffleVectorSDNode>(RHS.getNode())->getMask();
15969 std::copy(Mask.begin(), Mask.end(), RMask.begin());
Duncan Sands17470be2011-09-22 20:15:48 +000015970 } else {
15971 if (RHS.getOpcode() != ISD::UNDEF)
15972 C = RHS;
Craig Topperb72039c2011-11-30 09:10:50 +000015973 for (unsigned i = 0; i != NumElts; ++i)
Duncan Sands17470be2011-09-22 20:15:48 +000015974 RMask[i] = i;
15975 }
15976
15977 // Check that the shuffles are both shuffling the same vectors.
15978 if (!(A == C && B == D) && !(A == D && B == C))
15979 return false;
15980
15981 // If everything is UNDEF then bail out: it would be better to fold to UNDEF.
15982 if (!A.getNode() && !B.getNode())
15983 return false;
15984
15985 // If A and B occur in reverse order in RHS, then "swap" them (which means
15986 // rewriting the mask).
15987 if (A != C)
Craig Topperbeabc6c2011-12-05 06:56:46 +000015988 CommuteVectorShuffleMask(RMask, NumElts);
Duncan Sands17470be2011-09-22 20:15:48 +000015989
15990 // At this point LHS and RHS are equivalent to
15991 // LHS = VECTOR_SHUFFLE A, B, LMask
15992 // RHS = VECTOR_SHUFFLE A, B, RMask
15993 // Check that the masks correspond to performing a horizontal operation.
Craig Topperf8363302011-12-02 08:18:41 +000015994 for (unsigned i = 0; i != NumElts; ++i) {
Craig Topperbeabc6c2011-12-05 06:56:46 +000015995 int LIdx = LMask[i], RIdx = RMask[i];
Duncan Sands17470be2011-09-22 20:15:48 +000015996
Craig Topperf8363302011-12-02 08:18:41 +000015997 // Ignore any UNDEF components.
Craig Topperbeabc6c2011-12-05 06:56:46 +000015998 if (LIdx < 0 || RIdx < 0 ||
15999 (!A.getNode() && (LIdx < (int)NumElts || RIdx < (int)NumElts)) ||
16000 (!B.getNode() && (LIdx >= (int)NumElts || RIdx >= (int)NumElts)))
Craig Topperf8363302011-12-02 08:18:41 +000016001 continue;
Duncan Sands17470be2011-09-22 20:15:48 +000016002
Craig Topperf8363302011-12-02 08:18:41 +000016003 // Check that successive elements are being operated on. If not, this is
16004 // not a horizontal operation.
16005 unsigned Src = (i/HalfLaneElts) % 2; // each lane is split between srcs
16006 unsigned LaneStart = (i/NumLaneElts) * NumLaneElts;
Craig Topperbeabc6c2011-12-05 06:56:46 +000016007 int Index = 2*(i%HalfLaneElts) + NumElts*Src + LaneStart;
Craig Topperf8363302011-12-02 08:18:41 +000016008 if (!(LIdx == Index && RIdx == Index + 1) &&
Craig Topperbeabc6c2011-12-05 06:56:46 +000016009 !(IsCommutative && LIdx == Index + 1 && RIdx == Index))
Craig Topperf8363302011-12-02 08:18:41 +000016010 return false;
Duncan Sands17470be2011-09-22 20:15:48 +000016011 }
16012
16013 LHS = A.getNode() ? A : B; // If A is 'UNDEF', use B for it.
16014 RHS = B.getNode() ? B : A; // If B is 'UNDEF', use A for it.
16015 return true;
16016}
16017
16018/// PerformFADDCombine - Do target-specific dag combines on floating point adds.
16019static SDValue PerformFADDCombine(SDNode *N, SelectionDAG &DAG,
16020 const X86Subtarget *Subtarget) {
16021 EVT VT = N->getValueType(0);
16022 SDValue LHS = N->getOperand(0);
16023 SDValue RHS = N->getOperand(1);
16024
16025 // Try to synthesize horizontal adds from adds of shuffles.
Craig Topperd0a31172012-01-10 06:37:29 +000016026 if (((Subtarget->hasSSE3() && (VT == MVT::v4f32 || VT == MVT::v2f64)) ||
Craig Topper138a5c62011-12-02 07:16:01 +000016027 (Subtarget->hasAVX() && (VT == MVT::v8f32 || VT == MVT::v4f64))) &&
Duncan Sands17470be2011-09-22 20:15:48 +000016028 isHorizontalBinOp(LHS, RHS, true))
16029 return DAG.getNode(X86ISD::FHADD, N->getDebugLoc(), VT, LHS, RHS);
16030 return SDValue();
16031}
16032
16033/// PerformFSUBCombine - Do target-specific dag combines on floating point subs.
16034static SDValue PerformFSUBCombine(SDNode *N, SelectionDAG &DAG,
16035 const X86Subtarget *Subtarget) {
16036 EVT VT = N->getValueType(0);
16037 SDValue LHS = N->getOperand(0);
16038 SDValue RHS = N->getOperand(1);
16039
16040 // Try to synthesize horizontal subs from subs of shuffles.
Craig Topperd0a31172012-01-10 06:37:29 +000016041 if (((Subtarget->hasSSE3() && (VT == MVT::v4f32 || VT == MVT::v2f64)) ||
Craig Topper138a5c62011-12-02 07:16:01 +000016042 (Subtarget->hasAVX() && (VT == MVT::v8f32 || VT == MVT::v4f64))) &&
Duncan Sands17470be2011-09-22 20:15:48 +000016043 isHorizontalBinOp(LHS, RHS, false))
16044 return DAG.getNode(X86ISD::FHSUB, N->getDebugLoc(), VT, LHS, RHS);
16045 return SDValue();
16046}
16047
Chris Lattner6cf73262008-01-25 06:14:17 +000016048/// PerformFORCombine - Do target-specific dag combines on X86ISD::FOR and
16049/// X86ISD::FXOR nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000016050static SDValue PerformFORCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattner6cf73262008-01-25 06:14:17 +000016051 assert(N->getOpcode() == X86ISD::FOR || N->getOpcode() == X86ISD::FXOR);
16052 // F[X]OR(0.0, x) -> x
16053 // F[X]OR(x, 0.0) -> x
Chris Lattneraf723b92008-01-25 05:46:26 +000016054 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
16055 if (C->getValueAPF().isPosZero())
16056 return N->getOperand(1);
16057 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
16058 if (C->getValueAPF().isPosZero())
16059 return N->getOperand(0);
Dan Gohman475871a2008-07-27 21:46:04 +000016060 return SDValue();
Chris Lattneraf723b92008-01-25 05:46:26 +000016061}
16062
Nadav Rotemd60cb112012-08-19 13:06:16 +000016063/// PerformFMinFMaxCombine - Do target-specific dag combines on X86ISD::FMIN and
16064/// X86ISD::FMAX nodes.
16065static SDValue PerformFMinFMaxCombine(SDNode *N, SelectionDAG &DAG) {
16066 assert(N->getOpcode() == X86ISD::FMIN || N->getOpcode() == X86ISD::FMAX);
16067
16068 // Only perform optimizations if UnsafeMath is used.
16069 if (!DAG.getTarget().Options.UnsafeFPMath)
16070 return SDValue();
16071
16072 // If we run in unsafe-math mode, then convert the FMAX and FMIN nodes
Craig Topper8365e9b2012-09-01 06:33:50 +000016073 // into FMINC and FMAXC, which are Commutative operations.
Nadav Rotemd60cb112012-08-19 13:06:16 +000016074 unsigned NewOp = 0;
16075 switch (N->getOpcode()) {
16076 default: llvm_unreachable("unknown opcode");
16077 case X86ISD::FMIN: NewOp = X86ISD::FMINC; break;
16078 case X86ISD::FMAX: NewOp = X86ISD::FMAXC; break;
16079 }
16080
16081 return DAG.getNode(NewOp, N->getDebugLoc(), N->getValueType(0),
16082 N->getOperand(0), N->getOperand(1));
16083}
16084
16085
Chris Lattneraf723b92008-01-25 05:46:26 +000016086/// PerformFANDCombine - Do target-specific dag combines on X86ISD::FAND nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000016087static SDValue PerformFANDCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattneraf723b92008-01-25 05:46:26 +000016088 // FAND(0.0, x) -> 0.0
16089 // FAND(x, 0.0) -> 0.0
16090 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
16091 if (C->getValueAPF().isPosZero())
16092 return N->getOperand(0);
16093 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
16094 if (C->getValueAPF().isPosZero())
16095 return N->getOperand(1);
Dan Gohman475871a2008-07-27 21:46:04 +000016096 return SDValue();
Chris Lattneraf723b92008-01-25 05:46:26 +000016097}
16098
Dan Gohmane5af2d32009-01-29 01:59:02 +000016099static SDValue PerformBTCombine(SDNode *N,
16100 SelectionDAG &DAG,
16101 TargetLowering::DAGCombinerInfo &DCI) {
16102 // BT ignores high bits in the bit index operand.
16103 SDValue Op1 = N->getOperand(1);
16104 if (Op1.hasOneUse()) {
16105 unsigned BitWidth = Op1.getValueSizeInBits();
16106 APInt DemandedMask = APInt::getLowBitsSet(BitWidth, Log2_32(BitWidth));
16107 APInt KnownZero, KnownOne;
Evan Chenge5b51ac2010-04-17 06:13:15 +000016108 TargetLowering::TargetLoweringOpt TLO(DAG, !DCI.isBeforeLegalize(),
16109 !DCI.isBeforeLegalizeOps());
Dan Gohmand858e902010-04-17 15:26:15 +000016110 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Dan Gohmane5af2d32009-01-29 01:59:02 +000016111 if (TLO.ShrinkDemandedConstant(Op1, DemandedMask) ||
16112 TLI.SimplifyDemandedBits(Op1, DemandedMask, KnownZero, KnownOne, TLO))
16113 DCI.CommitTargetLoweringOpt(TLO);
16114 }
16115 return SDValue();
16116}
Chris Lattner83e6c992006-10-04 06:57:07 +000016117
Eli Friedman7a5e5552009-06-07 06:52:44 +000016118static SDValue PerformVZEXT_MOVLCombine(SDNode *N, SelectionDAG &DAG) {
16119 SDValue Op = N->getOperand(0);
Wesley Peckbf17cfa2010-11-23 03:31:01 +000016120 if (Op.getOpcode() == ISD::BITCAST)
Eli Friedman7a5e5552009-06-07 06:52:44 +000016121 Op = Op.getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +000016122 EVT VT = N->getValueType(0), OpVT = Op.getValueType();
Eli Friedman7a5e5552009-06-07 06:52:44 +000016123 if (Op.getOpcode() == X86ISD::VZEXT_LOAD &&
Eric Christopherfd179292009-08-27 18:07:15 +000016124 VT.getVectorElementType().getSizeInBits() ==
Eli Friedman7a5e5552009-06-07 06:52:44 +000016125 OpVT.getVectorElementType().getSizeInBits()) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +000016126 return DAG.getNode(ISD::BITCAST, N->getDebugLoc(), VT, Op);
Eli Friedman7a5e5552009-06-07 06:52:44 +000016127 }
16128 return SDValue();
16129}
16130
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +000016131static SDValue PerformSExtCombine(SDNode *N, SelectionDAG &DAG,
16132 TargetLowering::DAGCombinerInfo &DCI,
16133 const X86Subtarget *Subtarget) {
16134 if (!DCI.isBeforeLegalizeOps())
16135 return SDValue();
16136
Craig Topper3ef43cf2012-04-24 06:36:35 +000016137 if (!Subtarget->hasAVX())
Elena Demikhovskyf6020402012-02-08 08:37:26 +000016138 return SDValue();
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +000016139
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +000016140 EVT VT = N->getValueType(0);
16141 SDValue Op = N->getOperand(0);
16142 EVT OpVT = Op.getValueType();
16143 DebugLoc dl = N->getDebugLoc();
16144
Elena Demikhovskyf6020402012-02-08 08:37:26 +000016145 if ((VT == MVT::v4i64 && OpVT == MVT::v4i32) ||
16146 (VT == MVT::v8i32 && OpVT == MVT::v8i16)) {
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +000016147
Craig Topper3ef43cf2012-04-24 06:36:35 +000016148 if (Subtarget->hasAVX2())
Elena Demikhovsky1da58672012-04-22 09:39:03 +000016149 return DAG.getNode(X86ISD::VSEXT_MOVL, dl, VT, Op);
Elena Demikhovsky1da58672012-04-22 09:39:03 +000016150
16151 // Optimize vectors in AVX mode
16152 // Sign extend v8i16 to v8i32 and
16153 // v4i32 to v4i64
16154 //
16155 // Divide input vector into two parts
16156 // for v4i32 the shuffle mask will be { 0, 1, -1, -1} {2, 3, -1, -1}
16157 // use vpmovsx instruction to extend v4i32 -> v2i64; v8i16 -> v4i32
16158 // concat the vectors to original VT
16159
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +000016160 unsigned NumElems = OpVT.getVectorNumElements();
Craig Toppercacafd42012-08-14 08:18:43 +000016161 SDValue Undef = DAG.getUNDEF(OpVT);
16162
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +000016163 SmallVector<int,8> ShufMask1(NumElems, -1);
Craig Topper3ef43cf2012-04-24 06:36:35 +000016164 for (unsigned i = 0; i != NumElems/2; ++i)
16165 ShufMask1[i] = i;
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +000016166
Craig Toppercacafd42012-08-14 08:18:43 +000016167 SDValue OpLo = DAG.getVectorShuffle(OpVT, dl, Op, Undef, &ShufMask1[0]);
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +000016168
16169 SmallVector<int,8> ShufMask2(NumElems, -1);
Craig Topper3ef43cf2012-04-24 06:36:35 +000016170 for (unsigned i = 0; i != NumElems/2; ++i)
16171 ShufMask2[i] = i + NumElems/2;
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +000016172
Craig Toppercacafd42012-08-14 08:18:43 +000016173 SDValue OpHi = DAG.getVectorShuffle(OpVT, dl, Op, Undef, &ShufMask2[0]);
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +000016174
Craig Topper3ef43cf2012-04-24 06:36:35 +000016175 EVT HalfVT = EVT::getVectorVT(*DAG.getContext(), VT.getScalarType(),
Elena Demikhovskyf6020402012-02-08 08:37:26 +000016176 VT.getVectorNumElements()/2);
16177
Craig Topper3ef43cf2012-04-24 06:36:35 +000016178 OpLo = DAG.getNode(X86ISD::VSEXT_MOVL, dl, HalfVT, OpLo);
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +000016179 OpHi = DAG.getNode(X86ISD::VSEXT_MOVL, dl, HalfVT, OpHi);
16180
16181 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT, OpLo, OpHi);
16182 }
16183 return SDValue();
16184}
16185
Michael Liaof6c24ee2012-08-10 14:39:24 +000016186static SDValue PerformFMACombine(SDNode *N, SelectionDAG &DAG,
Elena Demikhovsky1503aba2012-08-01 12:06:00 +000016187 const X86Subtarget* Subtarget) {
16188 DebugLoc dl = N->getDebugLoc();
16189 EVT VT = N->getValueType(0);
16190
Craig Topperb1bdd7d2012-08-30 06:56:15 +000016191 // Let legalize expand this if it isn't a legal type yet.
16192 if (!DAG.getTargetLoweringInfo().isTypeLegal(VT))
16193 return SDValue();
16194
Elena Demikhovsky1503aba2012-08-01 12:06:00 +000016195 EVT ScalarVT = VT.getScalarType();
Craig Topperbf404372012-08-31 15:40:30 +000016196 if ((ScalarVT != MVT::f32 && ScalarVT != MVT::f64) ||
16197 (!Subtarget->hasFMA() && !Subtarget->hasFMA4()))
Elena Demikhovsky1503aba2012-08-01 12:06:00 +000016198 return SDValue();
16199
16200 SDValue A = N->getOperand(0);
16201 SDValue B = N->getOperand(1);
16202 SDValue C = N->getOperand(2);
16203
16204 bool NegA = (A.getOpcode() == ISD::FNEG);
16205 bool NegB = (B.getOpcode() == ISD::FNEG);
16206 bool NegC = (C.getOpcode() == ISD::FNEG);
16207
Michael Liaof6c24ee2012-08-10 14:39:24 +000016208 // Negative multiplication when NegA xor NegB
16209 bool NegMul = (NegA != NegB);
Elena Demikhovsky1503aba2012-08-01 12:06:00 +000016210 if (NegA)
16211 A = A.getOperand(0);
16212 if (NegB)
16213 B = B.getOperand(0);
16214 if (NegC)
16215 C = C.getOperand(0);
16216
16217 unsigned Opcode;
16218 if (!NegMul)
Craig Topperbf404372012-08-31 15:40:30 +000016219 Opcode = (!NegC) ? X86ISD::FMADD : X86ISD::FMSUB;
Elena Demikhovsky1503aba2012-08-01 12:06:00 +000016220 else
Craig Topperbf404372012-08-31 15:40:30 +000016221 Opcode = (!NegC) ? X86ISD::FNMADD : X86ISD::FNMSUB;
16222
Elena Demikhovsky1503aba2012-08-01 12:06:00 +000016223 return DAG.getNode(Opcode, dl, VT, A, B, C);
16224}
16225
Elena Demikhovsky28d7e712012-01-24 13:54:13 +000016226static SDValue PerformZExtCombine(SDNode *N, SelectionDAG &DAG,
Craig Topperc16f8512012-04-25 06:39:39 +000016227 TargetLowering::DAGCombinerInfo &DCI,
Elena Demikhovsky28d7e712012-01-24 13:54:13 +000016228 const X86Subtarget *Subtarget) {
Evan Cheng2e489c42009-12-16 00:53:11 +000016229 // (i32 zext (and (i8 x86isd::setcc_carry), 1)) ->
16230 // (and (i32 x86isd::setcc_carry), 1)
16231 // This eliminates the zext. This transformation is necessary because
16232 // ISD::SETCC is always legalized to i8.
16233 DebugLoc dl = N->getDebugLoc();
16234 SDValue N0 = N->getOperand(0);
16235 EVT VT = N->getValueType(0);
Elena Demikhovsky28d7e712012-01-24 13:54:13 +000016236 EVT OpVT = N0.getValueType();
16237
Evan Cheng2e489c42009-12-16 00:53:11 +000016238 if (N0.getOpcode() == ISD::AND &&
16239 N0.hasOneUse() &&
16240 N0.getOperand(0).hasOneUse()) {
16241 SDValue N00 = N0.getOperand(0);
16242 if (N00.getOpcode() != X86ISD::SETCC_CARRY)
16243 return SDValue();
16244 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
16245 if (!C || C->getZExtValue() != 1)
16246 return SDValue();
16247 return DAG.getNode(ISD::AND, dl, VT,
16248 DAG.getNode(X86ISD::SETCC_CARRY, dl, VT,
16249 N00.getOperand(0), N00.getOperand(1)),
16250 DAG.getConstant(1, VT));
16251 }
Craig Topperd0cf5652012-04-21 18:13:35 +000016252
Elena Demikhovsky28d7e712012-01-24 13:54:13 +000016253 // Optimize vectors in AVX mode:
16254 //
16255 // v8i16 -> v8i32
16256 // Use vpunpcklwd for 4 lower elements v8i16 -> v4i32.
16257 // Use vpunpckhwd for 4 upper elements v8i16 -> v4i32.
16258 // Concat upper and lower parts.
16259 //
16260 // v4i32 -> v4i64
16261 // Use vpunpckldq for 4 lower elements v4i32 -> v2i64.
16262 // Use vpunpckhdq for 4 upper elements v4i32 -> v2i64.
16263 // Concat upper and lower parts.
16264 //
Craig Topperc16f8512012-04-25 06:39:39 +000016265 if (!DCI.isBeforeLegalizeOps())
16266 return SDValue();
Elena Demikhovsky28d7e712012-01-24 13:54:13 +000016267
Craig Topperc16f8512012-04-25 06:39:39 +000016268 if (!Subtarget->hasAVX())
16269 return SDValue();
Elena Demikhovsky28d7e712012-01-24 13:54:13 +000016270
Craig Topperc16f8512012-04-25 06:39:39 +000016271 if (((VT == MVT::v8i32) && (OpVT == MVT::v8i16)) ||
16272 ((VT == MVT::v4i64) && (OpVT == MVT::v4i32))) {
Elena Demikhovsky1da58672012-04-22 09:39:03 +000016273
Craig Topperc16f8512012-04-25 06:39:39 +000016274 if (Subtarget->hasAVX2())
16275 return DAG.getNode(X86ISD::VZEXT_MOVL, dl, VT, N0);
Elena Demikhovsky28d7e712012-01-24 13:54:13 +000016276
Craig Topperc16f8512012-04-25 06:39:39 +000016277 SDValue ZeroVec = getZeroVector(OpVT, Subtarget, DAG, dl);
16278 SDValue OpLo = getUnpackl(DAG, dl, OpVT, N0, ZeroVec);
16279 SDValue OpHi = getUnpackh(DAG, dl, OpVT, N0, ZeroVec);
Elena Demikhovsky28d7e712012-01-24 13:54:13 +000016280
Craig Topperc16f8512012-04-25 06:39:39 +000016281 EVT HVT = EVT::getVectorVT(*DAG.getContext(), VT.getVectorElementType(),
16282 VT.getVectorNumElements()/2);
Elena Demikhovsky28d7e712012-01-24 13:54:13 +000016283
Craig Topperc16f8512012-04-25 06:39:39 +000016284 OpLo = DAG.getNode(ISD::BITCAST, dl, HVT, OpLo);
16285 OpHi = DAG.getNode(ISD::BITCAST, dl, HVT, OpHi);
16286
16287 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT, OpLo, OpHi);
Elena Demikhovsky28d7e712012-01-24 13:54:13 +000016288 }
16289
Evan Cheng2e489c42009-12-16 00:53:11 +000016290 return SDValue();
16291}
16292
Chad Rosiera73b6fc2012-04-27 22:33:25 +000016293// Optimize x == -y --> x+y == 0
16294// x != -y --> x+y != 0
16295static SDValue PerformISDSETCCCombine(SDNode *N, SelectionDAG &DAG) {
16296 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(2))->get();
16297 SDValue LHS = N->getOperand(0);
Chad Rosiera20e1e72012-08-01 18:39:17 +000016298 SDValue RHS = N->getOperand(1);
Chad Rosiera73b6fc2012-04-27 22:33:25 +000016299
16300 if ((CC == ISD::SETNE || CC == ISD::SETEQ) && LHS.getOpcode() == ISD::SUB)
16301 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(LHS.getOperand(0)))
16302 if (C->getAPIntValue() == 0 && LHS.hasOneUse()) {
16303 SDValue addV = DAG.getNode(ISD::ADD, N->getDebugLoc(),
16304 LHS.getValueType(), RHS, LHS.getOperand(1));
16305 return DAG.getSetCC(N->getDebugLoc(), N->getValueType(0),
16306 addV, DAG.getConstant(0, addV.getValueType()), CC);
16307 }
16308 if ((CC == ISD::SETNE || CC == ISD::SETEQ) && RHS.getOpcode() == ISD::SUB)
16309 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(RHS.getOperand(0)))
16310 if (C->getAPIntValue() == 0 && RHS.hasOneUse()) {
16311 SDValue addV = DAG.getNode(ISD::ADD, N->getDebugLoc(),
16312 RHS.getValueType(), LHS, RHS.getOperand(1));
16313 return DAG.getSetCC(N->getDebugLoc(), N->getValueType(0),
16314 addV, DAG.getConstant(0, addV.getValueType()), CC);
16315 }
16316 return SDValue();
16317}
16318
Chris Lattnerc19d1c32010-12-19 22:08:31 +000016319// Optimize RES = X86ISD::SETCC CONDCODE, EFLAG_INPUT
Michael Liaodbf8b5b2012-08-28 03:34:40 +000016320static SDValue PerformSETCCCombine(SDNode *N, SelectionDAG &DAG,
16321 TargetLowering::DAGCombinerInfo &DCI,
16322 const X86Subtarget *Subtarget) {
Chris Lattnerc19d1c32010-12-19 22:08:31 +000016323 DebugLoc DL = N->getDebugLoc();
Michael Liao2a33cec2012-08-10 19:58:13 +000016324 X86::CondCode CC = X86::CondCode(N->getConstantOperandVal(0));
16325 SDValue EFLAGS = N->getOperand(1);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000016326
Chris Lattnerc19d1c32010-12-19 22:08:31 +000016327 // Materialize "setb reg" as "sbb reg,reg", since it can be extended without
16328 // a zext and produces an all-ones bit which is more useful than 0/1 in some
16329 // cases.
Michael Liao2a33cec2012-08-10 19:58:13 +000016330 if (CC == X86::COND_B)
Chris Lattnerc19d1c32010-12-19 22:08:31 +000016331 return DAG.getNode(ISD::AND, DL, MVT::i8,
16332 DAG.getNode(X86ISD::SETCC_CARRY, DL, MVT::i8,
Michael Liao2a33cec2012-08-10 19:58:13 +000016333 DAG.getConstant(CC, MVT::i8), EFLAGS),
Chris Lattnerc19d1c32010-12-19 22:08:31 +000016334 DAG.getConstant(1, MVT::i8));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000016335
Michael Liao2a33cec2012-08-10 19:58:13 +000016336 SDValue Flags;
16337
Michael Liaodbf8b5b2012-08-28 03:34:40 +000016338 Flags = checkBoolTestSetCCCombine(EFLAGS, CC);
16339 if (Flags.getNode()) {
16340 SDValue Cond = DAG.getConstant(CC, MVT::i8);
16341 return DAG.getNode(X86ISD::SETCC, DL, N->getVTList(), Cond, Flags);
16342 }
16343
Michael Liao2a33cec2012-08-10 19:58:13 +000016344 return SDValue();
16345}
16346
16347// Optimize branch condition evaluation.
16348//
16349static SDValue PerformBrCondCombine(SDNode *N, SelectionDAG &DAG,
16350 TargetLowering::DAGCombinerInfo &DCI,
16351 const X86Subtarget *Subtarget) {
16352 DebugLoc DL = N->getDebugLoc();
16353 SDValue Chain = N->getOperand(0);
16354 SDValue Dest = N->getOperand(1);
16355 SDValue EFLAGS = N->getOperand(3);
16356 X86::CondCode CC = X86::CondCode(N->getConstantOperandVal(2));
16357
16358 SDValue Flags;
16359
Michael Liaodbf8b5b2012-08-28 03:34:40 +000016360 Flags = checkBoolTestSetCCCombine(EFLAGS, CC);
16361 if (Flags.getNode()) {
16362 SDValue Cond = DAG.getConstant(CC, MVT::i8);
16363 return DAG.getNode(X86ISD::BRCOND, DL, N->getVTList(), Chain, Dest, Cond,
16364 Flags);
16365 }
16366
Chris Lattnerc19d1c32010-12-19 22:08:31 +000016367 return SDValue();
16368}
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000016369
Craig Topper7fd5e162012-04-24 06:02:29 +000016370static SDValue PerformUINT_TO_FPCombine(SDNode *N, SelectionDAG &DAG) {
Nadav Rotema3540772012-04-23 21:53:37 +000016371 SDValue Op0 = N->getOperand(0);
16372 EVT InVT = Op0->getValueType(0);
Nadav Rotema3540772012-04-23 21:53:37 +000016373
16374 // UINT_TO_FP(v4i8) -> SINT_TO_FP(ZEXT(v4i8 to v4i32))
Craig Topper7fd5e162012-04-24 06:02:29 +000016375 if (InVT == MVT::v8i8 || InVT == MVT::v4i8) {
Nadav Rotema3540772012-04-23 21:53:37 +000016376 DebugLoc dl = N->getDebugLoc();
Craig Topper7fd5e162012-04-24 06:02:29 +000016377 MVT DstVT = InVT == MVT::v4i8 ? MVT::v4i32 : MVT::v8i32;
Nadav Rotema3540772012-04-23 21:53:37 +000016378 SDValue P = DAG.getNode(ISD::ZERO_EXTEND, dl, DstVT, Op0);
16379 // Notice that we use SINT_TO_FP because we know that the high bits
16380 // are zero and SINT_TO_FP is better supported by the hardware.
16381 return DAG.getNode(ISD::SINT_TO_FP, dl, N->getValueType(0), P);
16382 }
16383
16384 return SDValue();
16385}
16386
Benjamin Kramer1396c402011-06-18 11:09:41 +000016387static SDValue PerformSINT_TO_FPCombine(SDNode *N, SelectionDAG &DAG,
16388 const X86TargetLowering *XTLI) {
Stuart Hastingsf99a4b82011-06-06 23:15:58 +000016389 SDValue Op0 = N->getOperand(0);
Nadav Rotema3540772012-04-23 21:53:37 +000016390 EVT InVT = Op0->getValueType(0);
Nadav Rotema3540772012-04-23 21:53:37 +000016391
16392 // SINT_TO_FP(v4i8) -> SINT_TO_FP(SEXT(v4i8 to v4i32))
Craig Topper7fd5e162012-04-24 06:02:29 +000016393 if (InVT == MVT::v8i8 || InVT == MVT::v4i8) {
Nadav Rotema3540772012-04-23 21:53:37 +000016394 DebugLoc dl = N->getDebugLoc();
Craig Topper7fd5e162012-04-24 06:02:29 +000016395 MVT DstVT = InVT == MVT::v4i8 ? MVT::v4i32 : MVT::v8i32;
Nadav Rotema3540772012-04-23 21:53:37 +000016396 SDValue P = DAG.getNode(ISD::SIGN_EXTEND, dl, DstVT, Op0);
16397 return DAG.getNode(ISD::SINT_TO_FP, dl, N->getValueType(0), P);
16398 }
16399
Stuart Hastingsf99a4b82011-06-06 23:15:58 +000016400 // Transform (SINT_TO_FP (i64 ...)) into an x87 operation if we have
16401 // a 32-bit target where SSE doesn't support i64->FP operations.
16402 if (Op0.getOpcode() == ISD::LOAD) {
16403 LoadSDNode *Ld = cast<LoadSDNode>(Op0.getNode());
16404 EVT VT = Ld->getValueType(0);
16405 if (!Ld->isVolatile() && !N->getValueType(0).isVector() &&
16406 ISD::isNON_EXTLoad(Op0.getNode()) && Op0.hasOneUse() &&
16407 !XTLI->getSubtarget()->is64Bit() &&
16408 !DAG.getTargetLoweringInfo().isTypeLegal(VT)) {
Benjamin Kramer1396c402011-06-18 11:09:41 +000016409 SDValue FILDChain = XTLI->BuildFILD(SDValue(N, 0), Ld->getValueType(0),
16410 Ld->getChain(), Op0, DAG);
Stuart Hastingsf99a4b82011-06-06 23:15:58 +000016411 DAG.ReplaceAllUsesOfValueWith(Op0.getValue(1), FILDChain.getValue(1));
16412 return FILDChain;
16413 }
16414 }
16415 return SDValue();
16416}
16417
Chris Lattner23a01992010-12-20 01:37:09 +000016418// Optimize RES, EFLAGS = X86ISD::ADC LHS, RHS, EFLAGS
16419static SDValue PerformADCCombine(SDNode *N, SelectionDAG &DAG,
16420 X86TargetLowering::DAGCombinerInfo &DCI) {
16421 // If the LHS and RHS of the ADC node are zero, then it can't overflow and
16422 // the result is either zero or one (depending on the input carry bit).
16423 // Strength reduce this down to a "set on carry" aka SETCC_CARRY&1.
16424 if (X86::isZeroNode(N->getOperand(0)) &&
16425 X86::isZeroNode(N->getOperand(1)) &&
16426 // We don't have a good way to replace an EFLAGS use, so only do this when
16427 // dead right now.
16428 SDValue(N, 1).use_empty()) {
16429 DebugLoc DL = N->getDebugLoc();
16430 EVT VT = N->getValueType(0);
16431 SDValue CarryOut = DAG.getConstant(0, N->getValueType(1));
16432 SDValue Res1 = DAG.getNode(ISD::AND, DL, VT,
16433 DAG.getNode(X86ISD::SETCC_CARRY, DL, VT,
16434 DAG.getConstant(X86::COND_B,MVT::i8),
16435 N->getOperand(2)),
16436 DAG.getConstant(1, VT));
16437 return DCI.CombineTo(N, Res1, CarryOut);
16438 }
16439
16440 return SDValue();
16441}
16442
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000016443// fold (add Y, (sete X, 0)) -> adc 0, Y
16444// (add Y, (setne X, 0)) -> sbb -1, Y
16445// (sub (sete X, 0), Y) -> sbb 0, Y
16446// (sub (setne X, 0), Y) -> adc -1, Y
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000016447static SDValue OptimizeConditionalInDecrement(SDNode *N, SelectionDAG &DAG) {
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000016448 DebugLoc DL = N->getDebugLoc();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000016449
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000016450 // Look through ZExts.
16451 SDValue Ext = N->getOperand(N->getOpcode() == ISD::SUB ? 1 : 0);
16452 if (Ext.getOpcode() != ISD::ZERO_EXTEND || !Ext.hasOneUse())
16453 return SDValue();
16454
16455 SDValue SetCC = Ext.getOperand(0);
16456 if (SetCC.getOpcode() != X86ISD::SETCC || !SetCC.hasOneUse())
16457 return SDValue();
16458
16459 X86::CondCode CC = (X86::CondCode)SetCC.getConstantOperandVal(0);
16460 if (CC != X86::COND_E && CC != X86::COND_NE)
16461 return SDValue();
16462
16463 SDValue Cmp = SetCC.getOperand(1);
16464 if (Cmp.getOpcode() != X86ISD::CMP || !Cmp.hasOneUse() ||
Chris Lattner9cd3da42011-01-16 02:56:53 +000016465 !X86::isZeroNode(Cmp.getOperand(1)) ||
16466 !Cmp.getOperand(0).getValueType().isInteger())
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000016467 return SDValue();
16468
16469 SDValue CmpOp0 = Cmp.getOperand(0);
16470 SDValue NewCmp = DAG.getNode(X86ISD::CMP, DL, MVT::i32, CmpOp0,
16471 DAG.getConstant(1, CmpOp0.getValueType()));
16472
16473 SDValue OtherVal = N->getOperand(N->getOpcode() == ISD::SUB ? 0 : 1);
16474 if (CC == X86::COND_NE)
16475 return DAG.getNode(N->getOpcode() == ISD::SUB ? X86ISD::ADC : X86ISD::SBB,
16476 DL, OtherVal.getValueType(), OtherVal,
16477 DAG.getConstant(-1ULL, OtherVal.getValueType()), NewCmp);
16478 return DAG.getNode(N->getOpcode() == ISD::SUB ? X86ISD::SBB : X86ISD::ADC,
16479 DL, OtherVal.getValueType(), OtherVal,
16480 DAG.getConstant(0, OtherVal.getValueType()), NewCmp);
16481}
Chris Lattnerc19d1c32010-12-19 22:08:31 +000016482
Craig Topper54f952a2011-11-19 09:02:40 +000016483/// PerformADDCombine - Do target-specific dag combines on integer adds.
16484static SDValue PerformAddCombine(SDNode *N, SelectionDAG &DAG,
16485 const X86Subtarget *Subtarget) {
16486 EVT VT = N->getValueType(0);
16487 SDValue Op0 = N->getOperand(0);
16488 SDValue Op1 = N->getOperand(1);
16489
16490 // Try to synthesize horizontal adds from adds of shuffles.
Craig Topperd0a31172012-01-10 06:37:29 +000016491 if (((Subtarget->hasSSSE3() && (VT == MVT::v8i16 || VT == MVT::v4i32)) ||
Craig Toppere6cf4a02012-01-13 05:04:25 +000016492 (Subtarget->hasAVX2() && (VT == MVT::v16i16 || VT == MVT::v8i32))) &&
Craig Topper54f952a2011-11-19 09:02:40 +000016493 isHorizontalBinOp(Op0, Op1, true))
16494 return DAG.getNode(X86ISD::HADD, N->getDebugLoc(), VT, Op0, Op1);
16495
16496 return OptimizeConditionalInDecrement(N, DAG);
16497}
16498
16499static SDValue PerformSubCombine(SDNode *N, SelectionDAG &DAG,
16500 const X86Subtarget *Subtarget) {
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000016501 SDValue Op0 = N->getOperand(0);
16502 SDValue Op1 = N->getOperand(1);
16503
16504 // X86 can't encode an immediate LHS of a sub. See if we can push the
16505 // negation into a preceding instruction.
16506 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op0)) {
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000016507 // If the RHS of the sub is a XOR with one use and a constant, invert the
16508 // immediate. Then add one to the LHS of the sub so we can turn
16509 // X-Y -> X+~Y+1, saving one register.
16510 if (Op1->hasOneUse() && Op1.getOpcode() == ISD::XOR &&
16511 isa<ConstantSDNode>(Op1.getOperand(1))) {
Nick Lewycky726ebd62011-08-23 19:01:24 +000016512 APInt XorC = cast<ConstantSDNode>(Op1.getOperand(1))->getAPIntValue();
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000016513 EVT VT = Op0.getValueType();
16514 SDValue NewXor = DAG.getNode(ISD::XOR, Op1.getDebugLoc(), VT,
16515 Op1.getOperand(0),
16516 DAG.getConstant(~XorC, VT));
16517 return DAG.getNode(ISD::ADD, N->getDebugLoc(), VT, NewXor,
Nick Lewycky726ebd62011-08-23 19:01:24 +000016518 DAG.getConstant(C->getAPIntValue()+1, VT));
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000016519 }
16520 }
16521
Craig Topper54f952a2011-11-19 09:02:40 +000016522 // Try to synthesize horizontal adds from adds of shuffles.
16523 EVT VT = N->getValueType(0);
Craig Topperd0a31172012-01-10 06:37:29 +000016524 if (((Subtarget->hasSSSE3() && (VT == MVT::v8i16 || VT == MVT::v4i32)) ||
Craig Topperb72039c2011-11-30 09:10:50 +000016525 (Subtarget->hasAVX2() && (VT == MVT::v16i16 || VT == MVT::v8i32))) &&
16526 isHorizontalBinOp(Op0, Op1, true))
Craig Topper54f952a2011-11-19 09:02:40 +000016527 return DAG.getNode(X86ISD::HSUB, N->getDebugLoc(), VT, Op0, Op1);
16528
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000016529 return OptimizeConditionalInDecrement(N, DAG);
16530}
16531
Dan Gohman475871a2008-07-27 21:46:04 +000016532SDValue X86TargetLowering::PerformDAGCombine(SDNode *N,
Evan Cheng9dd93b32008-11-05 06:03:38 +000016533 DAGCombinerInfo &DCI) const {
Evan Cheng206ee9d2006-07-07 08:33:52 +000016534 SelectionDAG &DAG = DCI.DAG;
16535 switch (N->getOpcode()) {
16536 default: break;
Dan Gohman1bbf72b2010-03-15 23:23:03 +000016537 case ISD::EXTRACT_VECTOR_ELT:
Craig Topper89f4e662012-03-20 07:17:59 +000016538 return PerformEXTRACT_VECTOR_ELTCombine(N, DAG, DCI);
Duncan Sands6bcd2192011-09-17 16:49:39 +000016539 case ISD::VSELECT:
Nadav Rotemcc616562012-01-15 19:27:55 +000016540 case ISD::SELECT: return PerformSELECTCombine(N, DAG, DCI, Subtarget);
Michael Liaodbf8b5b2012-08-28 03:34:40 +000016541 case X86ISD::CMOV: return PerformCMOVCombine(N, DAG, DCI, Subtarget);
Craig Topper54f952a2011-11-19 09:02:40 +000016542 case ISD::ADD: return PerformAddCombine(N, DAG, Subtarget);
16543 case ISD::SUB: return PerformSubCombine(N, DAG, Subtarget);
Chris Lattner23a01992010-12-20 01:37:09 +000016544 case X86ISD::ADC: return PerformADCCombine(N, DAG, DCI);
Evan Cheng0b0cd912009-03-28 05:57:29 +000016545 case ISD::MUL: return PerformMulCombine(N, DAG, DCI);
Nate Begeman740ab032009-01-26 00:52:55 +000016546 case ISD::SHL:
16547 case ISD::SRA:
Mon P Wang845b1892012-02-01 22:15:20 +000016548 case ISD::SRL: return PerformShiftCombine(N, DAG, DCI, Subtarget);
Nate Begemanb65c1752010-12-17 22:55:37 +000016549 case ISD::AND: return PerformAndCombine(N, DAG, DCI, Subtarget);
Evan Cheng8b1190a2010-04-28 01:18:01 +000016550 case ISD::OR: return PerformOrCombine(N, DAG, DCI, Subtarget);
Craig Topperb4c94572011-10-21 06:55:01 +000016551 case ISD::XOR: return PerformXorCombine(N, DAG, DCI, Subtarget);
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000016552 case ISD::LOAD: return PerformLOADCombine(N, DAG, DCI, Subtarget);
Evan Cheng7e2ff772008-05-08 00:57:18 +000016553 case ISD::STORE: return PerformSTORECombine(N, DAG, Subtarget);
Craig Topper7fd5e162012-04-24 06:02:29 +000016554 case ISD::UINT_TO_FP: return PerformUINT_TO_FPCombine(N, DAG);
Stuart Hastingsf99a4b82011-06-06 23:15:58 +000016555 case ISD::SINT_TO_FP: return PerformSINT_TO_FPCombine(N, DAG, this);
Duncan Sands17470be2011-09-22 20:15:48 +000016556 case ISD::FADD: return PerformFADDCombine(N, DAG, Subtarget);
16557 case ISD::FSUB: return PerformFSUBCombine(N, DAG, Subtarget);
Chris Lattner6cf73262008-01-25 06:14:17 +000016558 case X86ISD::FXOR:
Chris Lattneraf723b92008-01-25 05:46:26 +000016559 case X86ISD::FOR: return PerformFORCombine(N, DAG);
Nadav Rotemd60cb112012-08-19 13:06:16 +000016560 case X86ISD::FMIN:
16561 case X86ISD::FMAX: return PerformFMinFMaxCombine(N, DAG);
Chris Lattneraf723b92008-01-25 05:46:26 +000016562 case X86ISD::FAND: return PerformFANDCombine(N, DAG);
Dan Gohmane5af2d32009-01-29 01:59:02 +000016563 case X86ISD::BT: return PerformBTCombine(N, DAG, DCI);
Eli Friedman7a5e5552009-06-07 06:52:44 +000016564 case X86ISD::VZEXT_MOVL: return PerformVZEXT_MOVLCombine(N, DAG);
Elena Demikhovsky1da58672012-04-22 09:39:03 +000016565 case ISD::ANY_EXTEND:
Craig Topperc16f8512012-04-25 06:39:39 +000016566 case ISD::ZERO_EXTEND: return PerformZExtCombine(N, DAG, DCI, Subtarget);
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +000016567 case ISD::SIGN_EXTEND: return PerformSExtCombine(N, DAG, DCI, Subtarget);
Craig Topper55b24052012-09-11 06:15:32 +000016568 case ISD::TRUNCATE: return PerformTruncateCombine(N, DAG,DCI,Subtarget);
Chad Rosiera73b6fc2012-04-27 22:33:25 +000016569 case ISD::SETCC: return PerformISDSETCCCombine(N, DAG);
Michael Liaodbf8b5b2012-08-28 03:34:40 +000016570 case X86ISD::SETCC: return PerformSETCCCombine(N, DAG, DCI, Subtarget);
Michael Liao2a33cec2012-08-10 19:58:13 +000016571 case X86ISD::BRCOND: return PerformBrCondCombine(N, DAG, DCI, Subtarget);
Craig Topperb3982da2011-12-31 23:50:21 +000016572 case X86ISD::SHUFP: // Handle all target specific shuffles
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +000016573 case X86ISD::PALIGN:
Craig Topper34671b82011-12-06 08:21:25 +000016574 case X86ISD::UNPCKH:
16575 case X86ISD::UNPCKL:
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000016576 case X86ISD::MOVHLPS:
16577 case X86ISD::MOVLHPS:
16578 case X86ISD::PSHUFD:
16579 case X86ISD::PSHUFHW:
16580 case X86ISD::PSHUFLW:
16581 case X86ISD::MOVSS:
16582 case X86ISD::MOVSD:
Craig Topper316cd2a2011-11-30 06:25:25 +000016583 case X86ISD::VPERMILP:
Craig Topperec24e612011-11-30 07:47:51 +000016584 case X86ISD::VPERM2X128:
Bruno Cardoso Lopes50b37c72011-08-15 21:45:54 +000016585 case ISD::VECTOR_SHUFFLE: return PerformShuffleCombine(N, DAG, DCI,Subtarget);
Elena Demikhovsky1503aba2012-08-01 12:06:00 +000016586 case ISD::FMA: return PerformFMACombine(N, DAG, Subtarget);
Evan Cheng206ee9d2006-07-07 08:33:52 +000016587 }
16588
Dan Gohman475871a2008-07-27 21:46:04 +000016589 return SDValue();
Evan Cheng206ee9d2006-07-07 08:33:52 +000016590}
16591
Evan Chenge5b51ac2010-04-17 06:13:15 +000016592/// isTypeDesirableForOp - Return true if the target has native support for
16593/// the specified value type and it is 'desirable' to use the type for the
16594/// given node type. e.g. On x86 i16 is legal, but undesirable since i16
16595/// instruction encodings are longer and some i16 instructions are slow.
16596bool X86TargetLowering::isTypeDesirableForOp(unsigned Opc, EVT VT) const {
16597 if (!isTypeLegal(VT))
16598 return false;
Evan Cheng2bce5f4b2010-04-28 08:30:49 +000016599 if (VT != MVT::i16)
Evan Chenge5b51ac2010-04-17 06:13:15 +000016600 return true;
16601
16602 switch (Opc) {
16603 default:
16604 return true;
Evan Cheng4c26e932010-04-19 19:29:22 +000016605 case ISD::LOAD:
16606 case ISD::SIGN_EXTEND:
16607 case ISD::ZERO_EXTEND:
16608 case ISD::ANY_EXTEND:
Evan Chenge5b51ac2010-04-17 06:13:15 +000016609 case ISD::SHL:
Evan Chenge5b51ac2010-04-17 06:13:15 +000016610 case ISD::SRL:
16611 case ISD::SUB:
16612 case ISD::ADD:
16613 case ISD::MUL:
16614 case ISD::AND:
16615 case ISD::OR:
16616 case ISD::XOR:
16617 return false;
16618 }
16619}
16620
16621/// IsDesirableToPromoteOp - This method query the target whether it is
Evan Cheng64b7bf72010-04-16 06:14:10 +000016622/// beneficial for dag combiner to promote the specified node. If true, it
16623/// should return the desired promotion type by reference.
Evan Chenge5b51ac2010-04-17 06:13:15 +000016624bool X86TargetLowering::IsDesirableToPromoteOp(SDValue Op, EVT &PVT) const {
Evan Cheng64b7bf72010-04-16 06:14:10 +000016625 EVT VT = Op.getValueType();
16626 if (VT != MVT::i16)
16627 return false;
16628
Evan Cheng4c26e932010-04-19 19:29:22 +000016629 bool Promote = false;
16630 bool Commute = false;
Evan Cheng64b7bf72010-04-16 06:14:10 +000016631 switch (Op.getOpcode()) {
Evan Cheng4c26e932010-04-19 19:29:22 +000016632 default: break;
16633 case ISD::LOAD: {
16634 LoadSDNode *LD = cast<LoadSDNode>(Op);
16635 // If the non-extending load has a single use and it's not live out, then it
16636 // might be folded.
Evan Cheng2bce5f4b2010-04-28 08:30:49 +000016637 if (LD->getExtensionType() == ISD::NON_EXTLOAD /*&&
16638 Op.hasOneUse()*/) {
16639 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
16640 UE = Op.getNode()->use_end(); UI != UE; ++UI) {
16641 // The only case where we'd want to promote LOAD (rather then it being
16642 // promoted as an operand is when it's only use is liveout.
16643 if (UI->getOpcode() != ISD::CopyToReg)
16644 return false;
16645 }
16646 }
Evan Cheng4c26e932010-04-19 19:29:22 +000016647 Promote = true;
16648 break;
16649 }
16650 case ISD::SIGN_EXTEND:
16651 case ISD::ZERO_EXTEND:
16652 case ISD::ANY_EXTEND:
16653 Promote = true;
16654 break;
Evan Chenge5b51ac2010-04-17 06:13:15 +000016655 case ISD::SHL:
Evan Cheng2bce5f4b2010-04-28 08:30:49 +000016656 case ISD::SRL: {
Evan Chenge5b51ac2010-04-17 06:13:15 +000016657 SDValue N0 = Op.getOperand(0);
16658 // Look out for (store (shl (load), x)).
Evan Chengc82c20b2010-04-24 04:44:57 +000016659 if (MayFoldLoad(N0) && MayFoldIntoStore(Op))
Evan Chenge5b51ac2010-04-17 06:13:15 +000016660 return false;
Evan Cheng4c26e932010-04-19 19:29:22 +000016661 Promote = true;
Evan Chenge5b51ac2010-04-17 06:13:15 +000016662 break;
16663 }
Evan Cheng64b7bf72010-04-16 06:14:10 +000016664 case ISD::ADD:
16665 case ISD::MUL:
16666 case ISD::AND:
16667 case ISD::OR:
Evan Cheng4c26e932010-04-19 19:29:22 +000016668 case ISD::XOR:
16669 Commute = true;
16670 // fallthrough
16671 case ISD::SUB: {
Evan Cheng64b7bf72010-04-16 06:14:10 +000016672 SDValue N0 = Op.getOperand(0);
16673 SDValue N1 = Op.getOperand(1);
Evan Chengc82c20b2010-04-24 04:44:57 +000016674 if (!Commute && MayFoldLoad(N1))
Evan Cheng64b7bf72010-04-16 06:14:10 +000016675 return false;
16676 // Avoid disabling potential load folding opportunities.
Evan Chengc82c20b2010-04-24 04:44:57 +000016677 if (MayFoldLoad(N0) && (!isa<ConstantSDNode>(N1) || MayFoldIntoStore(Op)))
Evan Cheng64b7bf72010-04-16 06:14:10 +000016678 return false;
Evan Chengc82c20b2010-04-24 04:44:57 +000016679 if (MayFoldLoad(N1) && (!isa<ConstantSDNode>(N0) || MayFoldIntoStore(Op)))
Evan Cheng64b7bf72010-04-16 06:14:10 +000016680 return false;
Evan Cheng4c26e932010-04-19 19:29:22 +000016681 Promote = true;
Evan Cheng64b7bf72010-04-16 06:14:10 +000016682 }
16683 }
16684
16685 PVT = MVT::i32;
Evan Cheng4c26e932010-04-19 19:29:22 +000016686 return Promote;
Evan Cheng64b7bf72010-04-16 06:14:10 +000016687}
16688
Evan Cheng60c07e12006-07-05 22:17:51 +000016689//===----------------------------------------------------------------------===//
16690// X86 Inline Assembly Support
16691//===----------------------------------------------------------------------===//
16692
Benjamin Kramer2ea4cdb2011-12-18 19:59:20 +000016693namespace {
16694 // Helper to match a string separated by whitespace.
Benjamin Kramer0581ed72011-12-18 20:51:31 +000016695 bool matchAsmImpl(StringRef s, ArrayRef<const StringRef *> args) {
Benjamin Kramer2ea4cdb2011-12-18 19:59:20 +000016696 s = s.substr(s.find_first_not_of(" \t")); // Skip leading whitespace.
Benjamin Kramere6cddb72011-12-17 14:36:05 +000016697
Benjamin Kramer0581ed72011-12-18 20:51:31 +000016698 for (unsigned i = 0, e = args.size(); i != e; ++i) {
Benjamin Kramer2ea4cdb2011-12-18 19:59:20 +000016699 StringRef piece(*args[i]);
16700 if (!s.startswith(piece)) // Check if the piece matches.
16701 return false;
16702
16703 s = s.substr(piece.size());
16704 StringRef::size_type pos = s.find_first_not_of(" \t");
16705 if (pos == 0) // We matched a prefix.
16706 return false;
16707
16708 s = s.substr(pos);
Benjamin Kramere6cddb72011-12-17 14:36:05 +000016709 }
16710
Benjamin Kramer2ea4cdb2011-12-18 19:59:20 +000016711 return s.empty();
Benjamin Kramere6cddb72011-12-17 14:36:05 +000016712 }
Benjamin Kramer0581ed72011-12-18 20:51:31 +000016713 const VariadicFunction1<bool, StringRef, StringRef, matchAsmImpl> matchAsm={};
Benjamin Kramere6cddb72011-12-17 14:36:05 +000016714}
16715
Chris Lattnerb8105652009-07-20 17:51:36 +000016716bool X86TargetLowering::ExpandInlineAsm(CallInst *CI) const {
16717 InlineAsm *IA = cast<InlineAsm>(CI->getCalledValue());
Chris Lattnerb8105652009-07-20 17:51:36 +000016718
16719 std::string AsmStr = IA->getAsmString();
16720
Benjamin Kramere6cddb72011-12-17 14:36:05 +000016721 IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
16722 if (!Ty || Ty->getBitWidth() % 16 != 0)
16723 return false;
16724
Chris Lattnerb8105652009-07-20 17:51:36 +000016725 // TODO: should remove alternatives from the asmstring: "foo {a|b}" -> "foo a"
Benjamin Kramerd4f19592010-01-11 18:03:24 +000016726 SmallVector<StringRef, 4> AsmPieces;
Peter Collingbourne98361182010-11-13 19:54:23 +000016727 SplitString(AsmStr, AsmPieces, ";\n");
Chris Lattnerb8105652009-07-20 17:51:36 +000016728
16729 switch (AsmPieces.size()) {
16730 default: return false;
16731 case 1:
Chris Lattner7a2bdde2011-04-15 05:18:47 +000016732 // FIXME: this should verify that we are targeting a 486 or better. If not,
Benjamin Kramere6cddb72011-12-17 14:36:05 +000016733 // we will turn this bswap into something that will be lowered to logical
16734 // ops instead of emitting the bswap asm. For now, we don't support 486 or
16735 // lower so don't worry about this.
Chris Lattnerb8105652009-07-20 17:51:36 +000016736 // bswap $0
Benjamin Kramer2ea4cdb2011-12-18 19:59:20 +000016737 if (matchAsm(AsmPieces[0], "bswap", "$0") ||
16738 matchAsm(AsmPieces[0], "bswapl", "$0") ||
16739 matchAsm(AsmPieces[0], "bswapq", "$0") ||
16740 matchAsm(AsmPieces[0], "bswap", "${0:q}") ||
16741 matchAsm(AsmPieces[0], "bswapl", "${0:q}") ||
16742 matchAsm(AsmPieces[0], "bswapq", "${0:q}")) {
Chris Lattnerb8105652009-07-20 17:51:36 +000016743 // No need to check constraints, nothing other than the equivalent of
16744 // "=r,0" would be valid here.
Evan Cheng55d42002011-01-08 01:24:27 +000016745 return IntrinsicLowering::LowerToByteSwap(CI);
Chris Lattnerb8105652009-07-20 17:51:36 +000016746 }
Benjamin Kramere6cddb72011-12-17 14:36:05 +000016747
Chris Lattnerb8105652009-07-20 17:51:36 +000016748 // rorw $$8, ${0:w} --> llvm.bswap.i16
Duncan Sandsb0bc6c32010-02-15 16:12:20 +000016749 if (CI->getType()->isIntegerTy(16) &&
Benjamin Kramere6cddb72011-12-17 14:36:05 +000016750 IA->getConstraintString().compare(0, 5, "=r,0,") == 0 &&
Benjamin Kramer2ea4cdb2011-12-18 19:59:20 +000016751 (matchAsm(AsmPieces[0], "rorw", "$$8,", "${0:w}") ||
16752 matchAsm(AsmPieces[0], "rolw", "$$8,", "${0:w}"))) {
Dan Gohman0ef701e2010-03-04 19:58:08 +000016753 AsmPieces.clear();
Evan Cheng55d42002011-01-08 01:24:27 +000016754 const std::string &ConstraintsStr = IA->getConstraintString();
16755 SplitString(StringRef(ConstraintsStr).substr(5), AsmPieces, ",");
Dan Gohman0ef701e2010-03-04 19:58:08 +000016756 std::sort(AsmPieces.begin(), AsmPieces.end());
16757 if (AsmPieces.size() == 4 &&
16758 AsmPieces[0] == "~{cc}" &&
16759 AsmPieces[1] == "~{dirflag}" &&
16760 AsmPieces[2] == "~{flags}" &&
Benjamin Kramere6cddb72011-12-17 14:36:05 +000016761 AsmPieces[3] == "~{fpsr}")
16762 return IntrinsicLowering::LowerToByteSwap(CI);
Chris Lattnerb8105652009-07-20 17:51:36 +000016763 }
16764 break;
16765 case 3:
Peter Collingbourne948cf022010-11-13 19:54:30 +000016766 if (CI->getType()->isIntegerTy(32) &&
Benjamin Kramere6cddb72011-12-17 14:36:05 +000016767 IA->getConstraintString().compare(0, 5, "=r,0,") == 0 &&
Benjamin Kramer2ea4cdb2011-12-18 19:59:20 +000016768 matchAsm(AsmPieces[0], "rorw", "$$8,", "${0:w}") &&
16769 matchAsm(AsmPieces[1], "rorl", "$$16,", "$0") &&
16770 matchAsm(AsmPieces[2], "rorw", "$$8,", "${0:w}")) {
Benjamin Kramere6cddb72011-12-17 14:36:05 +000016771 AsmPieces.clear();
16772 const std::string &ConstraintsStr = IA->getConstraintString();
16773 SplitString(StringRef(ConstraintsStr).substr(5), AsmPieces, ",");
16774 std::sort(AsmPieces.begin(), AsmPieces.end());
16775 if (AsmPieces.size() == 4 &&
16776 AsmPieces[0] == "~{cc}" &&
16777 AsmPieces[1] == "~{dirflag}" &&
16778 AsmPieces[2] == "~{flags}" &&
16779 AsmPieces[3] == "~{fpsr}")
16780 return IntrinsicLowering::LowerToByteSwap(CI);
Peter Collingbourne948cf022010-11-13 19:54:30 +000016781 }
Evan Cheng55d42002011-01-08 01:24:27 +000016782
16783 if (CI->getType()->isIntegerTy(64)) {
16784 InlineAsm::ConstraintInfoVector Constraints = IA->ParseConstraints();
16785 if (Constraints.size() >= 2 &&
16786 Constraints[0].Codes.size() == 1 && Constraints[0].Codes[0] == "A" &&
16787 Constraints[1].Codes.size() == 1 && Constraints[1].Codes[0] == "0") {
16788 // bswap %eax / bswap %edx / xchgl %eax, %edx -> llvm.bswap.i64
Benjamin Kramer2ea4cdb2011-12-18 19:59:20 +000016789 if (matchAsm(AsmPieces[0], "bswap", "%eax") &&
16790 matchAsm(AsmPieces[1], "bswap", "%edx") &&
16791 matchAsm(AsmPieces[2], "xchgl", "%eax,", "%edx"))
Benjamin Kramere6cddb72011-12-17 14:36:05 +000016792 return IntrinsicLowering::LowerToByteSwap(CI);
Chris Lattnerb8105652009-07-20 17:51:36 +000016793 }
16794 }
16795 break;
16796 }
16797 return false;
16798}
16799
16800
16801
Chris Lattnerf4dff842006-07-11 02:54:03 +000016802/// getConstraintType - Given a constraint letter, return the type of
16803/// constraint it is for this target.
16804X86TargetLowering::ConstraintType
Chris Lattner4234f572007-03-25 02:14:49 +000016805X86TargetLowering::getConstraintType(const std::string &Constraint) const {
16806 if (Constraint.size() == 1) {
16807 switch (Constraint[0]) {
Chris Lattner4234f572007-03-25 02:14:49 +000016808 case 'R':
Chris Lattner4234f572007-03-25 02:14:49 +000016809 case 'q':
16810 case 'Q':
John Thompson44ab89e2010-10-29 17:29:13 +000016811 case 'f':
16812 case 't':
16813 case 'u':
Dale Johannesen2ffbcac2008-04-01 00:57:48 +000016814 case 'y':
John Thompson44ab89e2010-10-29 17:29:13 +000016815 case 'x':
Chris Lattner4234f572007-03-25 02:14:49 +000016816 case 'Y':
Eric Christopher31b5f002011-07-07 22:29:07 +000016817 case 'l':
Chris Lattner4234f572007-03-25 02:14:49 +000016818 return C_RegisterClass;
John Thompson44ab89e2010-10-29 17:29:13 +000016819 case 'a':
16820 case 'b':
16821 case 'c':
16822 case 'd':
16823 case 'S':
16824 case 'D':
16825 case 'A':
16826 return C_Register;
16827 case 'I':
16828 case 'J':
16829 case 'K':
16830 case 'L':
16831 case 'M':
16832 case 'N':
16833 case 'G':
16834 case 'C':
Dale Johannesen78e3e522009-02-12 20:58:09 +000016835 case 'e':
16836 case 'Z':
16837 return C_Other;
Chris Lattner4234f572007-03-25 02:14:49 +000016838 default:
16839 break;
16840 }
Chris Lattnerf4dff842006-07-11 02:54:03 +000016841 }
Chris Lattner4234f572007-03-25 02:14:49 +000016842 return TargetLowering::getConstraintType(Constraint);
Chris Lattnerf4dff842006-07-11 02:54:03 +000016843}
16844
John Thompson44ab89e2010-10-29 17:29:13 +000016845/// Examine constraint type and operand type and determine a weight value.
John Thompsoneac6e1d2010-09-13 18:15:37 +000016846/// This object must already have been set up with the operand type
16847/// and the current alternative constraint selected.
John Thompson44ab89e2010-10-29 17:29:13 +000016848TargetLowering::ConstraintWeight
16849 X86TargetLowering::getSingleConstraintMatchWeight(
John Thompsoneac6e1d2010-09-13 18:15:37 +000016850 AsmOperandInfo &info, const char *constraint) const {
John Thompson44ab89e2010-10-29 17:29:13 +000016851 ConstraintWeight weight = CW_Invalid;
John Thompsoneac6e1d2010-09-13 18:15:37 +000016852 Value *CallOperandVal = info.CallOperandVal;
16853 // If we don't have a value, we can't do a match,
16854 // but allow it at the lowest weight.
16855 if (CallOperandVal == NULL)
John Thompson44ab89e2010-10-29 17:29:13 +000016856 return CW_Default;
Chris Lattnerdb125cf2011-07-18 04:54:35 +000016857 Type *type = CallOperandVal->getType();
John Thompsoneac6e1d2010-09-13 18:15:37 +000016858 // Look at the constraint type.
16859 switch (*constraint) {
16860 default:
John Thompson44ab89e2010-10-29 17:29:13 +000016861 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
16862 case 'R':
16863 case 'q':
16864 case 'Q':
16865 case 'a':
16866 case 'b':
16867 case 'c':
16868 case 'd':
16869 case 'S':
16870 case 'D':
16871 case 'A':
16872 if (CallOperandVal->getType()->isIntegerTy())
16873 weight = CW_SpecificReg;
16874 break;
16875 case 'f':
16876 case 't':
16877 case 'u':
16878 if (type->isFloatingPointTy())
16879 weight = CW_SpecificReg;
16880 break;
16881 case 'y':
Chris Lattner2a786eb2010-12-19 20:19:20 +000016882 if (type->isX86_MMXTy() && Subtarget->hasMMX())
John Thompson44ab89e2010-10-29 17:29:13 +000016883 weight = CW_SpecificReg;
16884 break;
16885 case 'x':
16886 case 'Y':
Craig Topper1accb7e2012-01-10 06:54:16 +000016887 if (((type->getPrimitiveSizeInBits() == 128) && Subtarget->hasSSE1()) ||
Eric Christopher55487552012-01-07 01:02:09 +000016888 ((type->getPrimitiveSizeInBits() == 256) && Subtarget->hasAVX()))
John Thompson44ab89e2010-10-29 17:29:13 +000016889 weight = CW_Register;
John Thompsoneac6e1d2010-09-13 18:15:37 +000016890 break;
16891 case 'I':
16892 if (ConstantInt *C = dyn_cast<ConstantInt>(info.CallOperandVal)) {
16893 if (C->getZExtValue() <= 31)
John Thompson44ab89e2010-10-29 17:29:13 +000016894 weight = CW_Constant;
John Thompsoneac6e1d2010-09-13 18:15:37 +000016895 }
16896 break;
John Thompson44ab89e2010-10-29 17:29:13 +000016897 case 'J':
16898 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
16899 if (C->getZExtValue() <= 63)
16900 weight = CW_Constant;
16901 }
16902 break;
16903 case 'K':
16904 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
16905 if ((C->getSExtValue() >= -0x80) && (C->getSExtValue() <= 0x7f))
16906 weight = CW_Constant;
16907 }
16908 break;
16909 case 'L':
16910 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
16911 if ((C->getZExtValue() == 0xff) || (C->getZExtValue() == 0xffff))
16912 weight = CW_Constant;
16913 }
16914 break;
16915 case 'M':
16916 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
16917 if (C->getZExtValue() <= 3)
16918 weight = CW_Constant;
16919 }
16920 break;
16921 case 'N':
16922 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
16923 if (C->getZExtValue() <= 0xff)
16924 weight = CW_Constant;
16925 }
16926 break;
16927 case 'G':
16928 case 'C':
16929 if (dyn_cast<ConstantFP>(CallOperandVal)) {
16930 weight = CW_Constant;
16931 }
16932 break;
16933 case 'e':
16934 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
16935 if ((C->getSExtValue() >= -0x80000000LL) &&
16936 (C->getSExtValue() <= 0x7fffffffLL))
16937 weight = CW_Constant;
16938 }
16939 break;
16940 case 'Z':
16941 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
16942 if (C->getZExtValue() <= 0xffffffff)
16943 weight = CW_Constant;
16944 }
16945 break;
John Thompsoneac6e1d2010-09-13 18:15:37 +000016946 }
16947 return weight;
16948}
16949
Dale Johannesenba2a0b92008-01-29 02:21:21 +000016950/// LowerXConstraint - try to replace an X constraint, which matches anything,
16951/// with another that has more specific requirements based on the type of the
16952/// corresponding operand.
Chris Lattner5e764232008-04-26 23:02:14 +000016953const char *X86TargetLowering::
Owen Andersone50ed302009-08-10 22:56:29 +000016954LowerXConstraint(EVT ConstraintVT) const {
Chris Lattner5e764232008-04-26 23:02:14 +000016955 // FP X constraints get lowered to SSE1/2 registers if available, otherwise
16956 // 'f' like normal targets.
Duncan Sands83ec4b62008-06-06 12:08:01 +000016957 if (ConstraintVT.isFloatingPoint()) {
Craig Topper1accb7e2012-01-10 06:54:16 +000016958 if (Subtarget->hasSSE2())
Chris Lattner5e764232008-04-26 23:02:14 +000016959 return "Y";
Craig Topper1accb7e2012-01-10 06:54:16 +000016960 if (Subtarget->hasSSE1())
Chris Lattner5e764232008-04-26 23:02:14 +000016961 return "x";
16962 }
Scott Michelfdc40a02009-02-17 22:15:04 +000016963
Chris Lattner5e764232008-04-26 23:02:14 +000016964 return TargetLowering::LowerXConstraint(ConstraintVT);
Dale Johannesenba2a0b92008-01-29 02:21:21 +000016965}
16966
Chris Lattner48884cd2007-08-25 00:47:38 +000016967/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
16968/// vector. If it is invalid, don't add anything to Ops.
Dan Gohman475871a2008-07-27 21:46:04 +000016969void X86TargetLowering::LowerAsmOperandForConstraint(SDValue Op,
Eric Christopher100c8332011-06-02 23:16:42 +000016970 std::string &Constraint,
Dan Gohman475871a2008-07-27 21:46:04 +000016971 std::vector<SDValue>&Ops,
Chris Lattner5e764232008-04-26 23:02:14 +000016972 SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +000016973 SDValue Result(0, 0);
Scott Michelfdc40a02009-02-17 22:15:04 +000016974
Eric Christopher100c8332011-06-02 23:16:42 +000016975 // Only support length 1 constraints for now.
16976 if (Constraint.length() > 1) return;
Eric Christopher471e4222011-06-08 23:55:35 +000016977
Eric Christopher100c8332011-06-02 23:16:42 +000016978 char ConstraintLetter = Constraint[0];
16979 switch (ConstraintLetter) {
Chris Lattner22aaf1d2006-10-31 20:13:11 +000016980 default: break;
Devang Patel84f7fd22007-03-17 00:13:28 +000016981 case 'I':
Chris Lattner188b9fe2007-03-25 01:57:35 +000016982 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +000016983 if (C->getZExtValue() <= 31) {
16984 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattner48884cd2007-08-25 00:47:38 +000016985 break;
16986 }
Devang Patel84f7fd22007-03-17 00:13:28 +000016987 }
Chris Lattner48884cd2007-08-25 00:47:38 +000016988 return;
Evan Cheng364091e2008-09-22 23:57:37 +000016989 case 'J':
16990 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattner2e06dd22009-06-15 04:39:05 +000016991 if (C->getZExtValue() <= 63) {
Chris Lattnere4935152009-06-15 04:01:39 +000016992 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
16993 break;
16994 }
16995 }
16996 return;
16997 case 'K':
16998 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattner2e06dd22009-06-15 04:39:05 +000016999 if ((int8_t)C->getSExtValue() == C->getSExtValue()) {
Evan Cheng364091e2008-09-22 23:57:37 +000017000 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
17001 break;
17002 }
17003 }
17004 return;
Chris Lattner188b9fe2007-03-25 01:57:35 +000017005 case 'N':
17006 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +000017007 if (C->getZExtValue() <= 255) {
17008 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattner48884cd2007-08-25 00:47:38 +000017009 break;
17010 }
Chris Lattner188b9fe2007-03-25 01:57:35 +000017011 }
Chris Lattner48884cd2007-08-25 00:47:38 +000017012 return;
Dale Johannesen78e3e522009-02-12 20:58:09 +000017013 case 'e': {
17014 // 32-bit signed value
17015 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohman7720cb32010-06-18 14:01:07 +000017016 if (ConstantInt::isValueValidForType(Type::getInt32Ty(*DAG.getContext()),
17017 C->getSExtValue())) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000017018 // Widen to 64 bits here to get it sign extended.
Owen Anderson825b72b2009-08-11 20:47:22 +000017019 Result = DAG.getTargetConstant(C->getSExtValue(), MVT::i64);
Dale Johannesen78e3e522009-02-12 20:58:09 +000017020 break;
17021 }
17022 // FIXME gcc accepts some relocatable values here too, but only in certain
17023 // memory models; it's complicated.
17024 }
17025 return;
17026 }
17027 case 'Z': {
17028 // 32-bit unsigned value
17029 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohman7720cb32010-06-18 14:01:07 +000017030 if (ConstantInt::isValueValidForType(Type::getInt32Ty(*DAG.getContext()),
17031 C->getZExtValue())) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000017032 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
17033 break;
17034 }
17035 }
17036 // FIXME gcc accepts some relocatable values here too, but only in certain
17037 // memory models; it's complicated.
17038 return;
17039 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000017040 case 'i': {
Chris Lattner22aaf1d2006-10-31 20:13:11 +000017041 // Literal immediates are always ok.
Chris Lattner48884cd2007-08-25 00:47:38 +000017042 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Op)) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000017043 // Widen to 64 bits here to get it sign extended.
Owen Anderson825b72b2009-08-11 20:47:22 +000017044 Result = DAG.getTargetConstant(CST->getSExtValue(), MVT::i64);
Chris Lattner48884cd2007-08-25 00:47:38 +000017045 break;
17046 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000017047
Dale Johannesene5ff9ef2010-06-24 20:14:51 +000017048 // In any sort of PIC mode addresses need to be computed at runtime by
17049 // adding in a register or some sort of table lookup. These can't
17050 // be used as immediates.
Dale Johannesene2b448c2010-07-06 23:27:00 +000017051 if (Subtarget->isPICStyleGOT() || Subtarget->isPICStyleStubPIC())
Dale Johannesene5ff9ef2010-06-24 20:14:51 +000017052 return;
17053
Chris Lattnerdc43a882007-05-03 16:52:29 +000017054 // If we are in non-pic codegen mode, we allow the address of a global (with
17055 // an optional displacement) to be used with 'i'.
Chris Lattner49921962009-05-08 18:23:14 +000017056 GlobalAddressSDNode *GA = 0;
Chris Lattnerdc43a882007-05-03 16:52:29 +000017057 int64_t Offset = 0;
Scott Michelfdc40a02009-02-17 22:15:04 +000017058
Chris Lattner49921962009-05-08 18:23:14 +000017059 // Match either (GA), (GA+C), (GA+C1+C2), etc.
17060 while (1) {
17061 if ((GA = dyn_cast<GlobalAddressSDNode>(Op))) {
17062 Offset += GA->getOffset();
17063 break;
17064 } else if (Op.getOpcode() == ISD::ADD) {
17065 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
17066 Offset += C->getZExtValue();
17067 Op = Op.getOperand(0);
17068 continue;
17069 }
17070 } else if (Op.getOpcode() == ISD::SUB) {
17071 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
17072 Offset += -C->getZExtValue();
17073 Op = Op.getOperand(0);
17074 continue;
17075 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000017076 }
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000017077
Chris Lattner49921962009-05-08 18:23:14 +000017078 // Otherwise, this isn't something we can handle, reject it.
17079 return;
Chris Lattnerdc43a882007-05-03 16:52:29 +000017080 }
Eric Christopherfd179292009-08-27 18:07:15 +000017081
Dan Gohman46510a72010-04-15 01:51:59 +000017082 const GlobalValue *GV = GA->getGlobal();
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000017083 // If we require an extra load to get this address, as in PIC mode, we
17084 // can't accept it.
Chris Lattner36c25012009-07-10 07:34:39 +000017085 if (isGlobalStubReference(Subtarget->ClassifyGlobalReference(GV,
17086 getTargetMachine())))
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000017087 return;
Scott Michelfdc40a02009-02-17 22:15:04 +000017088
Devang Patel0d881da2010-07-06 22:08:15 +000017089 Result = DAG.getTargetGlobalAddress(GV, Op.getDebugLoc(),
17090 GA->getValueType(0), Offset);
Chris Lattner49921962009-05-08 18:23:14 +000017091 break;
Chris Lattner22aaf1d2006-10-31 20:13:11 +000017092 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000017093 }
Scott Michelfdc40a02009-02-17 22:15:04 +000017094
Gabor Greifba36cb52008-08-28 21:40:38 +000017095 if (Result.getNode()) {
Chris Lattner48884cd2007-08-25 00:47:38 +000017096 Ops.push_back(Result);
17097 return;
17098 }
Dale Johannesen1784d162010-06-25 21:55:36 +000017099 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
Chris Lattner22aaf1d2006-10-31 20:13:11 +000017100}
17101
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000017102std::pair<unsigned, const TargetRegisterClass*>
Chris Lattnerf76d1802006-07-31 23:26:50 +000017103X86TargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +000017104 EVT VT) const {
Chris Lattnerad043e82007-04-09 05:11:28 +000017105 // First, see if this is a constraint that directly corresponds to an LLVM
17106 // register class.
17107 if (Constraint.size() == 1) {
17108 // GCC Constraint Letters
17109 switch (Constraint[0]) {
17110 default: break;
Eric Christopherd176af82011-06-29 17:23:50 +000017111 // TODO: Slight differences here in allocation order and leaving
17112 // RIP in the class. Do they matter any more here than they do
17113 // in the normal allocation?
17114 case 'q': // GENERAL_REGS in 64-bit mode, Q_REGS in 32-bit mode.
17115 if (Subtarget->is64Bit()) {
Craig Topperc9099502012-04-20 06:31:50 +000017116 if (VT == MVT::i32 || VT == MVT::f32)
17117 return std::make_pair(0U, &X86::GR32RegClass);
17118 if (VT == MVT::i16)
17119 return std::make_pair(0U, &X86::GR16RegClass);
17120 if (VT == MVT::i8 || VT == MVT::i1)
17121 return std::make_pair(0U, &X86::GR8RegClass);
17122 if (VT == MVT::i64 || VT == MVT::f64)
17123 return std::make_pair(0U, &X86::GR64RegClass);
17124 break;
Eric Christopherd176af82011-06-29 17:23:50 +000017125 }
17126 // 32-bit fallthrough
17127 case 'Q': // Q_REGS
Nick Lewycky9bf45d02011-07-08 00:19:27 +000017128 if (VT == MVT::i32 || VT == MVT::f32)
Craig Topperc9099502012-04-20 06:31:50 +000017129 return std::make_pair(0U, &X86::GR32_ABCDRegClass);
17130 if (VT == MVT::i16)
17131 return std::make_pair(0U, &X86::GR16_ABCDRegClass);
17132 if (VT == MVT::i8 || VT == MVT::i1)
17133 return std::make_pair(0U, &X86::GR8_ABCD_LRegClass);
17134 if (VT == MVT::i64)
17135 return std::make_pair(0U, &X86::GR64_ABCDRegClass);
Eric Christopherd176af82011-06-29 17:23:50 +000017136 break;
Chris Lattner0f65cad2007-04-09 05:49:22 +000017137 case 'r': // GENERAL_REGS
Chris Lattner0f65cad2007-04-09 05:49:22 +000017138 case 'l': // INDEX_REGS
Eric Christopher5427ede2011-07-14 20:13:52 +000017139 if (VT == MVT::i8 || VT == MVT::i1)
Craig Topperc9099502012-04-20 06:31:50 +000017140 return std::make_pair(0U, &X86::GR8RegClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000017141 if (VT == MVT::i16)
Craig Topperc9099502012-04-20 06:31:50 +000017142 return std::make_pair(0U, &X86::GR16RegClass);
Eric Christopher2bbecd82011-05-19 21:33:47 +000017143 if (VT == MVT::i32 || VT == MVT::f32 || !Subtarget->is64Bit())
Craig Topperc9099502012-04-20 06:31:50 +000017144 return std::make_pair(0U, &X86::GR32RegClass);
17145 return std::make_pair(0U, &X86::GR64RegClass);
Dale Johannesen5f3663e2009-10-07 22:47:20 +000017146 case 'R': // LEGACY_REGS
Eric Christopher5427ede2011-07-14 20:13:52 +000017147 if (VT == MVT::i8 || VT == MVT::i1)
Craig Topperc9099502012-04-20 06:31:50 +000017148 return std::make_pair(0U, &X86::GR8_NOREXRegClass);
Dale Johannesen5f3663e2009-10-07 22:47:20 +000017149 if (VT == MVT::i16)
Craig Topperc9099502012-04-20 06:31:50 +000017150 return std::make_pair(0U, &X86::GR16_NOREXRegClass);
Dale Johannesen5f3663e2009-10-07 22:47:20 +000017151 if (VT == MVT::i32 || !Subtarget->is64Bit())
Craig Topperc9099502012-04-20 06:31:50 +000017152 return std::make_pair(0U, &X86::GR32_NOREXRegClass);
17153 return std::make_pair(0U, &X86::GR64_NOREXRegClass);
Chris Lattnerfce84ac2008-03-11 19:06:29 +000017154 case 'f': // FP Stack registers.
17155 // If SSE is enabled for this VT, use f80 to ensure the isel moves the
17156 // value to the correct fpstack register class.
Owen Anderson825b72b2009-08-11 20:47:22 +000017157 if (VT == MVT::f32 && !isScalarFPTypeInSSEReg(VT))
Craig Topperc9099502012-04-20 06:31:50 +000017158 return std::make_pair(0U, &X86::RFP32RegClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000017159 if (VT == MVT::f64 && !isScalarFPTypeInSSEReg(VT))
Craig Topperc9099502012-04-20 06:31:50 +000017160 return std::make_pair(0U, &X86::RFP64RegClass);
17161 return std::make_pair(0U, &X86::RFP80RegClass);
Chris Lattner6c284d72007-04-12 04:14:49 +000017162 case 'y': // MMX_REGS if MMX allowed.
17163 if (!Subtarget->hasMMX()) break;
Craig Topperc9099502012-04-20 06:31:50 +000017164 return std::make_pair(0U, &X86::VR64RegClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +000017165 case 'Y': // SSE_REGS if SSE2 allowed
Craig Topper1accb7e2012-01-10 06:54:16 +000017166 if (!Subtarget->hasSSE2()) break;
Chris Lattner0f65cad2007-04-09 05:49:22 +000017167 // FALL THROUGH.
Eric Christopher55487552012-01-07 01:02:09 +000017168 case 'x': // SSE_REGS if SSE1 allowed or AVX_REGS if AVX allowed
Craig Topper1accb7e2012-01-10 06:54:16 +000017169 if (!Subtarget->hasSSE1()) break;
Duncan Sands83ec4b62008-06-06 12:08:01 +000017170
Owen Anderson825b72b2009-08-11 20:47:22 +000017171 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner0f65cad2007-04-09 05:49:22 +000017172 default: break;
17173 // Scalar SSE types.
Owen Anderson825b72b2009-08-11 20:47:22 +000017174 case MVT::f32:
17175 case MVT::i32:
Craig Topperc9099502012-04-20 06:31:50 +000017176 return std::make_pair(0U, &X86::FR32RegClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000017177 case MVT::f64:
17178 case MVT::i64:
Craig Topperc9099502012-04-20 06:31:50 +000017179 return std::make_pair(0U, &X86::FR64RegClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +000017180 // Vector types.
Owen Anderson825b72b2009-08-11 20:47:22 +000017181 case MVT::v16i8:
17182 case MVT::v8i16:
17183 case MVT::v4i32:
17184 case MVT::v2i64:
17185 case MVT::v4f32:
17186 case MVT::v2f64:
Craig Topperc9099502012-04-20 06:31:50 +000017187 return std::make_pair(0U, &X86::VR128RegClass);
Eric Christopher55487552012-01-07 01:02:09 +000017188 // AVX types.
17189 case MVT::v32i8:
17190 case MVT::v16i16:
17191 case MVT::v8i32:
17192 case MVT::v4i64:
17193 case MVT::v8f32:
17194 case MVT::v4f64:
Craig Topperc9099502012-04-20 06:31:50 +000017195 return std::make_pair(0U, &X86::VR256RegClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +000017196 }
Chris Lattnerad043e82007-04-09 05:11:28 +000017197 break;
17198 }
17199 }
Scott Michelfdc40a02009-02-17 22:15:04 +000017200
Chris Lattnerf76d1802006-07-31 23:26:50 +000017201 // Use the default implementation in TargetLowering to convert the register
17202 // constraint into a member of a register class.
17203 std::pair<unsigned, const TargetRegisterClass*> Res;
17204 Res = TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
Chris Lattner1a60aa72006-10-31 19:42:44 +000017205
17206 // Not found as a standard register?
17207 if (Res.second == 0) {
Chris Lattner56d77c72009-09-13 22:41:48 +000017208 // Map st(0) -> st(7) -> ST0
17209 if (Constraint.size() == 7 && Constraint[0] == '{' &&
17210 tolower(Constraint[1]) == 's' &&
17211 tolower(Constraint[2]) == 't' &&
17212 Constraint[3] == '(' &&
17213 (Constraint[4] >= '0' && Constraint[4] <= '7') &&
17214 Constraint[5] == ')' &&
17215 Constraint[6] == '}') {
Daniel Dunbara279bc32009-09-20 02:20:51 +000017216
Chris Lattner56d77c72009-09-13 22:41:48 +000017217 Res.first = X86::ST0+Constraint[4]-'0';
Craig Topperc9099502012-04-20 06:31:50 +000017218 Res.second = &X86::RFP80RegClass;
Chris Lattner56d77c72009-09-13 22:41:48 +000017219 return Res;
17220 }
Daniel Dunbara279bc32009-09-20 02:20:51 +000017221
Chris Lattner56d77c72009-09-13 22:41:48 +000017222 // GCC allows "st(0)" to be called just plain "st".
Benjamin Kramer05872ea2009-11-12 20:36:59 +000017223 if (StringRef("{st}").equals_lower(Constraint)) {
Chris Lattner1a60aa72006-10-31 19:42:44 +000017224 Res.first = X86::ST0;
Craig Topperc9099502012-04-20 06:31:50 +000017225 Res.second = &X86::RFP80RegClass;
Chris Lattner56d77c72009-09-13 22:41:48 +000017226 return Res;
Chris Lattner1a60aa72006-10-31 19:42:44 +000017227 }
Chris Lattner56d77c72009-09-13 22:41:48 +000017228
17229 // flags -> EFLAGS
Benjamin Kramer05872ea2009-11-12 20:36:59 +000017230 if (StringRef("{flags}").equals_lower(Constraint)) {
Chris Lattner56d77c72009-09-13 22:41:48 +000017231 Res.first = X86::EFLAGS;
Craig Topperc9099502012-04-20 06:31:50 +000017232 Res.second = &X86::CCRRegClass;
Chris Lattner56d77c72009-09-13 22:41:48 +000017233 return Res;
17234 }
Daniel Dunbara279bc32009-09-20 02:20:51 +000017235
Dale Johannesen330169f2008-11-13 21:52:36 +000017236 // 'A' means EAX + EDX.
17237 if (Constraint == "A") {
17238 Res.first = X86::EAX;
Craig Topperc9099502012-04-20 06:31:50 +000017239 Res.second = &X86::GR32_ADRegClass;
Chris Lattner56d77c72009-09-13 22:41:48 +000017240 return Res;
Dale Johannesen330169f2008-11-13 21:52:36 +000017241 }
Chris Lattner1a60aa72006-10-31 19:42:44 +000017242 return Res;
17243 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000017244
Chris Lattnerf76d1802006-07-31 23:26:50 +000017245 // Otherwise, check to see if this is a register class of the wrong value
17246 // type. For example, we want to map "{ax},i32" -> {eax}, we don't want it to
17247 // turn into {ax},{dx}.
17248 if (Res.second->hasType(VT))
17249 return Res; // Correct type already, nothing to do.
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000017250
Chris Lattnerf76d1802006-07-31 23:26:50 +000017251 // All of the single-register GCC register classes map their values onto
17252 // 16-bit register pieces "ax","dx","cx","bx","si","di","bp","sp". If we
17253 // really want an 8-bit or 32-bit register, map to the appropriate register
17254 // class and return the appropriate register.
Craig Topperc9099502012-04-20 06:31:50 +000017255 if (Res.second == &X86::GR16RegClass) {
Owen Anderson825b72b2009-08-11 20:47:22 +000017256 if (VT == MVT::i8) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000017257 unsigned DestReg = 0;
17258 switch (Res.first) {
17259 default: break;
17260 case X86::AX: DestReg = X86::AL; break;
17261 case X86::DX: DestReg = X86::DL; break;
17262 case X86::CX: DestReg = X86::CL; break;
17263 case X86::BX: DestReg = X86::BL; break;
17264 }
17265 if (DestReg) {
17266 Res.first = DestReg;
Craig Topperc9099502012-04-20 06:31:50 +000017267 Res.second = &X86::GR8RegClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000017268 }
Owen Anderson825b72b2009-08-11 20:47:22 +000017269 } else if (VT == MVT::i32) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000017270 unsigned DestReg = 0;
17271 switch (Res.first) {
17272 default: break;
17273 case X86::AX: DestReg = X86::EAX; break;
17274 case X86::DX: DestReg = X86::EDX; break;
17275 case X86::CX: DestReg = X86::ECX; break;
17276 case X86::BX: DestReg = X86::EBX; break;
17277 case X86::SI: DestReg = X86::ESI; break;
17278 case X86::DI: DestReg = X86::EDI; break;
17279 case X86::BP: DestReg = X86::EBP; break;
17280 case X86::SP: DestReg = X86::ESP; break;
17281 }
17282 if (DestReg) {
17283 Res.first = DestReg;
Craig Topperc9099502012-04-20 06:31:50 +000017284 Res.second = &X86::GR32RegClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000017285 }
Owen Anderson825b72b2009-08-11 20:47:22 +000017286 } else if (VT == MVT::i64) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000017287 unsigned DestReg = 0;
17288 switch (Res.first) {
17289 default: break;
17290 case X86::AX: DestReg = X86::RAX; break;
17291 case X86::DX: DestReg = X86::RDX; break;
17292 case X86::CX: DestReg = X86::RCX; break;
17293 case X86::BX: DestReg = X86::RBX; break;
17294 case X86::SI: DestReg = X86::RSI; break;
17295 case X86::DI: DestReg = X86::RDI; break;
17296 case X86::BP: DestReg = X86::RBP; break;
17297 case X86::SP: DestReg = X86::RSP; break;
17298 }
17299 if (DestReg) {
17300 Res.first = DestReg;
Craig Topperc9099502012-04-20 06:31:50 +000017301 Res.second = &X86::GR64RegClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000017302 }
Chris Lattnerf76d1802006-07-31 23:26:50 +000017303 }
Craig Topperc9099502012-04-20 06:31:50 +000017304 } else if (Res.second == &X86::FR32RegClass ||
17305 Res.second == &X86::FR64RegClass ||
17306 Res.second == &X86::VR128RegClass) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000017307 // Handle references to XMM physical registers that got mapped into the
17308 // wrong class. This can happen with constraints like {xmm0} where the
17309 // target independent register mapper will just pick the first match it can
17310 // find, ignoring the required type.
Eli Friedman52d418d2012-06-25 23:42:33 +000017311
17312 if (VT == MVT::f32 || VT == MVT::i32)
Craig Topperc9099502012-04-20 06:31:50 +000017313 Res.second = &X86::FR32RegClass;
Eli Friedman52d418d2012-06-25 23:42:33 +000017314 else if (VT == MVT::f64 || VT == MVT::i64)
Craig Topperc9099502012-04-20 06:31:50 +000017315 Res.second = &X86::FR64RegClass;
17316 else if (X86::VR128RegClass.hasType(VT))
17317 Res.second = &X86::VR128RegClass;
Eli Friedman52d418d2012-06-25 23:42:33 +000017318 else if (X86::VR256RegClass.hasType(VT))
17319 Res.second = &X86::VR256RegClass;
Chris Lattnerf76d1802006-07-31 23:26:50 +000017320 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000017321
Chris Lattnerf76d1802006-07-31 23:26:50 +000017322 return Res;
17323}