blob: fb0fdb3631d4506cf8a8b56fe582667beb42f981 [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);
Anton Korobeynikov66fac792008-01-15 07:02:33 +0000560
Nate Begemanacc398c2006-01-25 18:21:52 +0000561 // VASTART needs to be custom lowered to use the VarArgsFrameIndex
Owen Anderson825b72b2009-08-11 20:47:22 +0000562 setOperationAction(ISD::VASTART , MVT::Other, Custom);
563 setOperationAction(ISD::VAEND , MVT::Other, Expand);
Dan Gohman9018e832008-05-10 01:26:14 +0000564 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000565 setOperationAction(ISD::VAARG , MVT::Other, Custom);
566 setOperationAction(ISD::VACOPY , MVT::Other, Custom);
Dan Gohman9018e832008-05-10 01:26:14 +0000567 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000568 setOperationAction(ISD::VAARG , MVT::Other, Expand);
569 setOperationAction(ISD::VACOPY , MVT::Other, Expand);
Dan Gohman9018e832008-05-10 01:26:14 +0000570 }
Evan Chengae642192007-03-02 23:16:35 +0000571
Owen Anderson825b72b2009-08-11 20:47:22 +0000572 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
573 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
Eric Christopherc967ad82011-08-31 04:17:21 +0000574
575 if (Subtarget->isTargetCOFF() && !Subtarget->isTargetEnvMacho())
576 setOperationAction(ISD::DYNAMIC_STACKALLOC, Subtarget->is64Bit() ?
577 MVT::i64 : MVT::i32, Custom);
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000578 else if (TM.Options.EnableSegmentedStacks)
Eric Christopherc967ad82011-08-31 04:17:21 +0000579 setOperationAction(ISD::DYNAMIC_STACKALLOC, Subtarget->is64Bit() ?
580 MVT::i64 : MVT::i32, Custom);
581 else
582 setOperationAction(ISD::DYNAMIC_STACKALLOC, Subtarget->is64Bit() ?
583 MVT::i64 : MVT::i32, Expand);
Chris Lattnerb99329e2006-01-13 02:42:53 +0000584
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000585 if (!TM.Options.UseSoftFloat && X86ScalarSSEf64) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000586 // f32 and f64 use SSE.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000587 // Set up the FP register classes.
Craig Topperc9099502012-04-20 06:31:50 +0000588 addRegisterClass(MVT::f32, &X86::FR32RegClass);
589 addRegisterClass(MVT::f64, &X86::FR64RegClass);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000590
Evan Cheng223547a2006-01-31 22:28:30 +0000591 // Use ANDPD to simulate FABS.
Owen Anderson825b72b2009-08-11 20:47:22 +0000592 setOperationAction(ISD::FABS , MVT::f64, Custom);
593 setOperationAction(ISD::FABS , MVT::f32, Custom);
Evan Cheng223547a2006-01-31 22:28:30 +0000594
595 // Use XORP to simulate FNEG.
Owen Anderson825b72b2009-08-11 20:47:22 +0000596 setOperationAction(ISD::FNEG , MVT::f64, Custom);
597 setOperationAction(ISD::FNEG , MVT::f32, Custom);
Evan Cheng223547a2006-01-31 22:28:30 +0000598
Evan Cheng68c47cb2007-01-05 07:55:56 +0000599 // Use ANDPD and ORPD to simulate FCOPYSIGN.
Owen Anderson825b72b2009-08-11 20:47:22 +0000600 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
601 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
Evan Cheng68c47cb2007-01-05 07:55:56 +0000602
Stuart Hastings4fd0dee2011-06-01 04:39:42 +0000603 // Lower this to FGETSIGNx86 plus an AND.
604 setOperationAction(ISD::FGETSIGN, MVT::i64, Custom);
605 setOperationAction(ISD::FGETSIGN, MVT::i32, Custom);
606
Evan Chengd25e9e82006-02-02 00:28:23 +0000607 // We don't support sin/cos/fmod
Owen Anderson825b72b2009-08-11 20:47:22 +0000608 setOperationAction(ISD::FSIN , MVT::f64, Expand);
609 setOperationAction(ISD::FCOS , MVT::f64, Expand);
610 setOperationAction(ISD::FSIN , MVT::f32, Expand);
611 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000612
Chris Lattnera54aa942006-01-29 06:26:08 +0000613 // Expand FP immediates into loads from the stack, except for the special
614 // cases we handle.
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000615 addLegalFPImmediate(APFloat(+0.0)); // xorpd
616 addLegalFPImmediate(APFloat(+0.0f)); // xorps
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000617 } else if (!TM.Options.UseSoftFloat && X86ScalarSSEf32) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000618 // Use SSE for f32, x87 for f64.
619 // Set up the FP register classes.
Craig Topperc9099502012-04-20 06:31:50 +0000620 addRegisterClass(MVT::f32, &X86::FR32RegClass);
621 addRegisterClass(MVT::f64, &X86::RFP64RegClass);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000622
623 // Use ANDPS to simulate FABS.
Owen Anderson825b72b2009-08-11 20:47:22 +0000624 setOperationAction(ISD::FABS , MVT::f32, Custom);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000625
626 // Use XORP to simulate FNEG.
Owen Anderson825b72b2009-08-11 20:47:22 +0000627 setOperationAction(ISD::FNEG , MVT::f32, Custom);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000628
Owen Anderson825b72b2009-08-11 20:47:22 +0000629 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000630
631 // Use ANDPS and ORPS to simulate FCOPYSIGN.
Owen Anderson825b72b2009-08-11 20:47:22 +0000632 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
633 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000634
635 // We don't support sin/cos/fmod
Owen Anderson825b72b2009-08-11 20:47:22 +0000636 setOperationAction(ISD::FSIN , MVT::f32, Expand);
637 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000638
Nate Begemane1795842008-02-14 08:57:00 +0000639 // Special cases we handle for FP constants.
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000640 addLegalFPImmediate(APFloat(+0.0f)); // xorps
641 addLegalFPImmediate(APFloat(+0.0)); // FLD0
642 addLegalFPImmediate(APFloat(+1.0)); // FLD1
643 addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS
644 addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS
645
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000646 if (!TM.Options.UnsafeFPMath) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000647 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
648 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000649 }
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000650 } else if (!TM.Options.UseSoftFloat) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000651 // f32 and f64 in x87.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000652 // Set up the FP register classes.
Craig Topperc9099502012-04-20 06:31:50 +0000653 addRegisterClass(MVT::f64, &X86::RFP64RegClass);
654 addRegisterClass(MVT::f32, &X86::RFP32RegClass);
Anton Korobeynikov12c49af2006-11-21 00:01:06 +0000655
Owen Anderson825b72b2009-08-11 20:47:22 +0000656 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
657 setOperationAction(ISD::UNDEF, MVT::f32, Expand);
658 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
659 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Expand);
Dale Johannesen5411a392007-08-09 01:04:01 +0000660
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000661 if (!TM.Options.UnsafeFPMath) {
Benjamin Kramer562b2402012-09-15 12:44:27 +0000662 setOperationAction(ISD::FSIN , MVT::f32 , Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000663 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
Benjamin Kramer562b2402012-09-15 12:44:27 +0000664 setOperationAction(ISD::FCOS , MVT::f32 , Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000665 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000666 }
Dale Johannesenf04afdb2007-08-30 00:23:21 +0000667 addLegalFPImmediate(APFloat(+0.0)); // FLD0
668 addLegalFPImmediate(APFloat(+1.0)); // FLD1
669 addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS
670 addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000671 addLegalFPImmediate(APFloat(+0.0f)); // FLD0
672 addLegalFPImmediate(APFloat(+1.0f)); // FLD1
673 addLegalFPImmediate(APFloat(-0.0f)); // FLD0/FCHS
674 addLegalFPImmediate(APFloat(-1.0f)); // FLD1/FCHS
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000675 }
Evan Cheng470a6ad2006-02-22 02:26:30 +0000676
Cameron Zwarich33390842011-07-08 21:39:21 +0000677 // We don't support FMA.
678 setOperationAction(ISD::FMA, MVT::f64, Expand);
679 setOperationAction(ISD::FMA, MVT::f32, Expand);
680
Dale Johannesen59a58732007-08-05 18:49:15 +0000681 // Long double always uses X87.
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000682 if (!TM.Options.UseSoftFloat) {
Craig Topperc9099502012-04-20 06:31:50 +0000683 addRegisterClass(MVT::f80, &X86::RFP80RegClass);
Owen Anderson825b72b2009-08-11 20:47:22 +0000684 setOperationAction(ISD::UNDEF, MVT::f80, Expand);
685 setOperationAction(ISD::FCOPYSIGN, MVT::f80, Expand);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000686 {
Benjamin Kramer98383962010-12-04 14:22:24 +0000687 APFloat TmpFlt = APFloat::getZero(APFloat::x87DoubleExtended);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000688 addLegalFPImmediate(TmpFlt); // FLD0
689 TmpFlt.changeSign();
690 addLegalFPImmediate(TmpFlt); // FLD0/FCHS
Benjamin Kramer98383962010-12-04 14:22:24 +0000691
692 bool ignored;
Evan Chengc7ce29b2009-02-13 22:36:38 +0000693 APFloat TmpFlt2(+1.0);
694 TmpFlt2.convert(APFloat::x87DoubleExtended, APFloat::rmNearestTiesToEven,
695 &ignored);
696 addLegalFPImmediate(TmpFlt2); // FLD1
697 TmpFlt2.changeSign();
698 addLegalFPImmediate(TmpFlt2); // FLD1/FCHS
699 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000700
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000701 if (!TM.Options.UnsafeFPMath) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000702 setOperationAction(ISD::FSIN , MVT::f80 , Expand);
703 setOperationAction(ISD::FCOS , MVT::f80 , Expand);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000704 }
Cameron Zwarich33390842011-07-08 21:39:21 +0000705
Owen Anderson4a4fdf32011-12-08 19:32:14 +0000706 setOperationAction(ISD::FFLOOR, MVT::f80, Expand);
707 setOperationAction(ISD::FCEIL, MVT::f80, Expand);
708 setOperationAction(ISD::FTRUNC, MVT::f80, Expand);
709 setOperationAction(ISD::FRINT, MVT::f80, Expand);
710 setOperationAction(ISD::FNEARBYINT, MVT::f80, Expand);
Cameron Zwarich33390842011-07-08 21:39:21 +0000711 setOperationAction(ISD::FMA, MVT::f80, Expand);
Dale Johannesen2f429012007-09-26 21:10:55 +0000712 }
Dale Johannesen59a58732007-08-05 18:49:15 +0000713
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000714 // Always use a library call for pow.
Owen Anderson825b72b2009-08-11 20:47:22 +0000715 setOperationAction(ISD::FPOW , MVT::f32 , Expand);
716 setOperationAction(ISD::FPOW , MVT::f64 , Expand);
717 setOperationAction(ISD::FPOW , MVT::f80 , Expand);
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000718
Owen Anderson825b72b2009-08-11 20:47:22 +0000719 setOperationAction(ISD::FLOG, MVT::f80, Expand);
720 setOperationAction(ISD::FLOG2, MVT::f80, Expand);
721 setOperationAction(ISD::FLOG10, MVT::f80, Expand);
722 setOperationAction(ISD::FEXP, MVT::f80, Expand);
723 setOperationAction(ISD::FEXP2, MVT::f80, Expand);
Dale Johannesen7794f2a2008-09-04 00:47:13 +0000724
Mon P Wangf007a8b2008-11-06 05:31:54 +0000725 // First set operation action for all vector types to either promote
Mon P Wang0c397192008-10-30 08:01:45 +0000726 // (for widening) or expand (for scalarization). Then we will selectively
727 // turn on ones that can be effectively codegen'd.
Jakub Staszak6610b1d2012-04-29 20:52:53 +0000728 for (int VT = MVT::FIRST_VECTOR_VALUETYPE;
729 VT <= MVT::LAST_VECTOR_VALUETYPE; ++VT) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000730 setOperationAction(ISD::ADD , (MVT::SimpleValueType)VT, Expand);
731 setOperationAction(ISD::SUB , (MVT::SimpleValueType)VT, Expand);
732 setOperationAction(ISD::FADD, (MVT::SimpleValueType)VT, Expand);
733 setOperationAction(ISD::FNEG, (MVT::SimpleValueType)VT, Expand);
734 setOperationAction(ISD::FSUB, (MVT::SimpleValueType)VT, Expand);
735 setOperationAction(ISD::MUL , (MVT::SimpleValueType)VT, Expand);
736 setOperationAction(ISD::FMUL, (MVT::SimpleValueType)VT, Expand);
737 setOperationAction(ISD::SDIV, (MVT::SimpleValueType)VT, Expand);
738 setOperationAction(ISD::UDIV, (MVT::SimpleValueType)VT, Expand);
739 setOperationAction(ISD::FDIV, (MVT::SimpleValueType)VT, Expand);
740 setOperationAction(ISD::SREM, (MVT::SimpleValueType)VT, Expand);
741 setOperationAction(ISD::UREM, (MVT::SimpleValueType)VT, Expand);
742 setOperationAction(ISD::LOAD, (MVT::SimpleValueType)VT, Expand);
743 setOperationAction(ISD::VECTOR_SHUFFLE, (MVT::SimpleValueType)VT, Expand);
744 setOperationAction(ISD::EXTRACT_VECTOR_ELT,(MVT::SimpleValueType)VT,Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000745 setOperationAction(ISD::INSERT_VECTOR_ELT,(MVT::SimpleValueType)VT, Expand);
David Greenecfe33c42011-01-26 19:13:22 +0000746 setOperationAction(ISD::EXTRACT_SUBVECTOR,(MVT::SimpleValueType)VT,Expand);
747 setOperationAction(ISD::INSERT_SUBVECTOR,(MVT::SimpleValueType)VT,Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000748 setOperationAction(ISD::FABS, (MVT::SimpleValueType)VT, Expand);
749 setOperationAction(ISD::FSIN, (MVT::SimpleValueType)VT, Expand);
750 setOperationAction(ISD::FCOS, (MVT::SimpleValueType)VT, Expand);
751 setOperationAction(ISD::FREM, (MVT::SimpleValueType)VT, Expand);
Elena Demikhovsky1503aba2012-08-01 12:06:00 +0000752 setOperationAction(ISD::FMA, (MVT::SimpleValueType)VT, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000753 setOperationAction(ISD::FPOWI, (MVT::SimpleValueType)VT, Expand);
754 setOperationAction(ISD::FSQRT, (MVT::SimpleValueType)VT, Expand);
755 setOperationAction(ISD::FCOPYSIGN, (MVT::SimpleValueType)VT, Expand);
Craig Toppera1fb1d22012-09-08 04:58:43 +0000756 setOperationAction(ISD::FFLOOR, (MVT::SimpleValueType)VT, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000757 setOperationAction(ISD::SMUL_LOHI, (MVT::SimpleValueType)VT, Expand);
758 setOperationAction(ISD::UMUL_LOHI, (MVT::SimpleValueType)VT, Expand);
759 setOperationAction(ISD::SDIVREM, (MVT::SimpleValueType)VT, Expand);
760 setOperationAction(ISD::UDIVREM, (MVT::SimpleValueType)VT, Expand);
761 setOperationAction(ISD::FPOW, (MVT::SimpleValueType)VT, Expand);
762 setOperationAction(ISD::CTPOP, (MVT::SimpleValueType)VT, Expand);
763 setOperationAction(ISD::CTTZ, (MVT::SimpleValueType)VT, Expand);
Chandler Carruth63974b22011-12-13 01:56:10 +0000764 setOperationAction(ISD::CTTZ_ZERO_UNDEF, (MVT::SimpleValueType)VT, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000765 setOperationAction(ISD::CTLZ, (MVT::SimpleValueType)VT, Expand);
Chandler Carruth63974b22011-12-13 01:56:10 +0000766 setOperationAction(ISD::CTLZ_ZERO_UNDEF, (MVT::SimpleValueType)VT, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000767 setOperationAction(ISD::SHL, (MVT::SimpleValueType)VT, Expand);
768 setOperationAction(ISD::SRA, (MVT::SimpleValueType)VT, Expand);
769 setOperationAction(ISD::SRL, (MVT::SimpleValueType)VT, Expand);
770 setOperationAction(ISD::ROTL, (MVT::SimpleValueType)VT, Expand);
771 setOperationAction(ISD::ROTR, (MVT::SimpleValueType)VT, Expand);
772 setOperationAction(ISD::BSWAP, (MVT::SimpleValueType)VT, Expand);
Duncan Sands28b77e92011-09-06 19:07:46 +0000773 setOperationAction(ISD::SETCC, (MVT::SimpleValueType)VT, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000774 setOperationAction(ISD::FLOG, (MVT::SimpleValueType)VT, Expand);
775 setOperationAction(ISD::FLOG2, (MVT::SimpleValueType)VT, Expand);
776 setOperationAction(ISD::FLOG10, (MVT::SimpleValueType)VT, Expand);
777 setOperationAction(ISD::FEXP, (MVT::SimpleValueType)VT, Expand);
778 setOperationAction(ISD::FEXP2, (MVT::SimpleValueType)VT, Expand);
779 setOperationAction(ISD::FP_TO_UINT, (MVT::SimpleValueType)VT, Expand);
780 setOperationAction(ISD::FP_TO_SINT, (MVT::SimpleValueType)VT, Expand);
781 setOperationAction(ISD::UINT_TO_FP, (MVT::SimpleValueType)VT, Expand);
782 setOperationAction(ISD::SINT_TO_FP, (MVT::SimpleValueType)VT, Expand);
Dan Gohman87862e72009-12-11 21:31:27 +0000783 setOperationAction(ISD::SIGN_EXTEND_INREG, (MVT::SimpleValueType)VT,Expand);
Dan Gohman2e141d72009-12-14 23:40:38 +0000784 setOperationAction(ISD::TRUNCATE, (MVT::SimpleValueType)VT, Expand);
785 setOperationAction(ISD::SIGN_EXTEND, (MVT::SimpleValueType)VT, Expand);
786 setOperationAction(ISD::ZERO_EXTEND, (MVT::SimpleValueType)VT, Expand);
787 setOperationAction(ISD::ANY_EXTEND, (MVT::SimpleValueType)VT, Expand);
Nadav Rotemaec58612011-09-13 19:17:42 +0000788 setOperationAction(ISD::VSELECT, (MVT::SimpleValueType)VT, Expand);
Jakub Staszak6610b1d2012-04-29 20:52:53 +0000789 for (int InnerVT = MVT::FIRST_VECTOR_VALUETYPE;
790 InnerVT <= MVT::LAST_VECTOR_VALUETYPE; ++InnerVT)
Dan Gohman2e141d72009-12-14 23:40:38 +0000791 setTruncStoreAction((MVT::SimpleValueType)VT,
792 (MVT::SimpleValueType)InnerVT, Expand);
793 setLoadExtAction(ISD::SEXTLOAD, (MVT::SimpleValueType)VT, Expand);
794 setLoadExtAction(ISD::ZEXTLOAD, (MVT::SimpleValueType)VT, Expand);
795 setLoadExtAction(ISD::EXTLOAD, (MVT::SimpleValueType)VT, Expand);
Evan Chengd30bf012006-03-01 01:11:20 +0000796 }
797
Evan Chengc7ce29b2009-02-13 22:36:38 +0000798 // FIXME: In order to prevent SSE instructions being expanded to MMX ones
799 // with -msoft-float, disable use of MMX as well.
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000800 if (!TM.Options.UseSoftFloat && Subtarget->hasMMX()) {
Craig Topperc9099502012-04-20 06:31:50 +0000801 addRegisterClass(MVT::x86mmx, &X86::VR64RegClass);
Dale Johannesen0488fb62010-09-30 23:57:10 +0000802 // No operations on x86mmx supported, everything uses intrinsics.
Evan Cheng470a6ad2006-02-22 02:26:30 +0000803 }
804
Dale Johannesen0488fb62010-09-30 23:57:10 +0000805 // MMX-sized vectors (other than x86mmx) are expected to be expanded
806 // into smaller operations.
807 setOperationAction(ISD::MULHS, MVT::v8i8, Expand);
808 setOperationAction(ISD::MULHS, MVT::v4i16, Expand);
809 setOperationAction(ISD::MULHS, MVT::v2i32, Expand);
810 setOperationAction(ISD::MULHS, MVT::v1i64, Expand);
811 setOperationAction(ISD::AND, MVT::v8i8, Expand);
812 setOperationAction(ISD::AND, MVT::v4i16, Expand);
813 setOperationAction(ISD::AND, MVT::v2i32, Expand);
814 setOperationAction(ISD::AND, MVT::v1i64, Expand);
815 setOperationAction(ISD::OR, MVT::v8i8, Expand);
816 setOperationAction(ISD::OR, MVT::v4i16, Expand);
817 setOperationAction(ISD::OR, MVT::v2i32, Expand);
818 setOperationAction(ISD::OR, MVT::v1i64, Expand);
819 setOperationAction(ISD::XOR, MVT::v8i8, Expand);
820 setOperationAction(ISD::XOR, MVT::v4i16, Expand);
821 setOperationAction(ISD::XOR, MVT::v2i32, Expand);
822 setOperationAction(ISD::XOR, MVT::v1i64, Expand);
823 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i8, Expand);
824 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4i16, Expand);
825 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v2i32, Expand);
826 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v1i64, Expand);
827 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v1i64, Expand);
828 setOperationAction(ISD::SELECT, MVT::v8i8, Expand);
829 setOperationAction(ISD::SELECT, MVT::v4i16, Expand);
830 setOperationAction(ISD::SELECT, MVT::v2i32, Expand);
831 setOperationAction(ISD::SELECT, MVT::v1i64, Expand);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000832 setOperationAction(ISD::BITCAST, MVT::v8i8, Expand);
833 setOperationAction(ISD::BITCAST, MVT::v4i16, Expand);
834 setOperationAction(ISD::BITCAST, MVT::v2i32, Expand);
835 setOperationAction(ISD::BITCAST, MVT::v1i64, Expand);
Dale Johannesen0488fb62010-09-30 23:57:10 +0000836
Craig Topper1accb7e2012-01-10 06:54:16 +0000837 if (!TM.Options.UseSoftFloat && Subtarget->hasSSE1()) {
Craig Topperc9099502012-04-20 06:31:50 +0000838 addRegisterClass(MVT::v4f32, &X86::VR128RegClass);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000839
Owen Anderson825b72b2009-08-11 20:47:22 +0000840 setOperationAction(ISD::FADD, MVT::v4f32, Legal);
841 setOperationAction(ISD::FSUB, MVT::v4f32, Legal);
842 setOperationAction(ISD::FMUL, MVT::v4f32, Legal);
843 setOperationAction(ISD::FDIV, MVT::v4f32, Legal);
844 setOperationAction(ISD::FSQRT, MVT::v4f32, Legal);
845 setOperationAction(ISD::FNEG, MVT::v4f32, Custom);
Craig Topper43620672012-09-08 07:31:51 +0000846 setOperationAction(ISD::FABS, MVT::v4f32, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000847 setOperationAction(ISD::LOAD, MVT::v4f32, Legal);
848 setOperationAction(ISD::BUILD_VECTOR, MVT::v4f32, Custom);
849 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4f32, Custom);
850 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
851 setOperationAction(ISD::SELECT, MVT::v4f32, Custom);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000852 }
853
Craig Topper1accb7e2012-01-10 06:54:16 +0000854 if (!TM.Options.UseSoftFloat && Subtarget->hasSSE2()) {
Craig Topperc9099502012-04-20 06:31:50 +0000855 addRegisterClass(MVT::v2f64, &X86::VR128RegClass);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000856
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000857 // FIXME: Unfortunately -soft-float and -no-implicit-float means XMM
858 // registers cannot be used even for integer operations.
Craig Topperc9099502012-04-20 06:31:50 +0000859 addRegisterClass(MVT::v16i8, &X86::VR128RegClass);
860 addRegisterClass(MVT::v8i16, &X86::VR128RegClass);
861 addRegisterClass(MVT::v4i32, &X86::VR128RegClass);
862 addRegisterClass(MVT::v2i64, &X86::VR128RegClass);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000863
Owen Anderson825b72b2009-08-11 20:47:22 +0000864 setOperationAction(ISD::ADD, MVT::v16i8, Legal);
865 setOperationAction(ISD::ADD, MVT::v8i16, Legal);
866 setOperationAction(ISD::ADD, MVT::v4i32, Legal);
867 setOperationAction(ISD::ADD, MVT::v2i64, Legal);
868 setOperationAction(ISD::MUL, MVT::v2i64, Custom);
869 setOperationAction(ISD::SUB, MVT::v16i8, Legal);
870 setOperationAction(ISD::SUB, MVT::v8i16, Legal);
871 setOperationAction(ISD::SUB, MVT::v4i32, Legal);
872 setOperationAction(ISD::SUB, MVT::v2i64, Legal);
873 setOperationAction(ISD::MUL, MVT::v8i16, Legal);
874 setOperationAction(ISD::FADD, MVT::v2f64, Legal);
875 setOperationAction(ISD::FSUB, MVT::v2f64, Legal);
876 setOperationAction(ISD::FMUL, MVT::v2f64, Legal);
877 setOperationAction(ISD::FDIV, MVT::v2f64, Legal);
878 setOperationAction(ISD::FSQRT, MVT::v2f64, Legal);
879 setOperationAction(ISD::FNEG, MVT::v2f64, Custom);
Craig Topper43620672012-09-08 07:31:51 +0000880 setOperationAction(ISD::FABS, MVT::v2f64, Custom);
Evan Cheng2c3ae372006-04-12 21:21:57 +0000881
Nadav Rotem354efd82011-09-18 14:57:03 +0000882 setOperationAction(ISD::SETCC, MVT::v2i64, Custom);
Duncan Sands28b77e92011-09-06 19:07:46 +0000883 setOperationAction(ISD::SETCC, MVT::v16i8, Custom);
884 setOperationAction(ISD::SETCC, MVT::v8i16, Custom);
885 setOperationAction(ISD::SETCC, MVT::v4i32, Custom);
Nate Begemanc2616e42008-05-12 20:34:32 +0000886
Owen Anderson825b72b2009-08-11 20:47:22 +0000887 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v16i8, Custom);
888 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i16, Custom);
889 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
890 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom);
891 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
Evan Chengf7c378e2006-04-10 07:23:14 +0000892
Evan Cheng2c3ae372006-04-12 21:21:57 +0000893 // Custom lower build_vector, vector_shuffle, and extract_vector_elt.
Jakub Staszak6610b1d2012-04-29 20:52:53 +0000894 for (int i = MVT::v16i8; i != MVT::v2i64; ++i) {
Craig Topper0d1f1762012-08-12 00:34:56 +0000895 MVT VT = (MVT::SimpleValueType)i;
Nate Begeman844e0f92007-12-11 01:41:33 +0000896 // Do not attempt to custom lower non-power-of-2 vectors
Duncan Sands83ec4b62008-06-06 12:08:01 +0000897 if (!isPowerOf2_32(VT.getVectorNumElements()))
Nate Begeman844e0f92007-12-11 01:41:33 +0000898 continue;
David Greene9b9838d2009-06-29 16:47:10 +0000899 // Do not attempt to custom lower non-128-bit vectors
900 if (!VT.is128BitVector())
901 continue;
Craig Topper0d1f1762012-08-12 00:34:56 +0000902 setOperationAction(ISD::BUILD_VECTOR, VT, Custom);
903 setOperationAction(ISD::VECTOR_SHUFFLE, VT, Custom);
904 setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT, Custom);
Evan Cheng2c3ae372006-04-12 21:21:57 +0000905 }
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000906
Owen Anderson825b72b2009-08-11 20:47:22 +0000907 setOperationAction(ISD::BUILD_VECTOR, MVT::v2f64, Custom);
908 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i64, Custom);
909 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2f64, Custom);
910 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i64, Custom);
911 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2f64, Custom);
912 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2f64, Custom);
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000913
Nate Begemancdd1eec2008-02-12 22:51:28 +0000914 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000915 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i64, Custom);
916 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Custom);
Nate Begemancdd1eec2008-02-12 22:51:28 +0000917 }
Evan Cheng2c3ae372006-04-12 21:21:57 +0000918
Anton Korobeynikov12c49af2006-11-21 00:01:06 +0000919 // Promote v16i8, v8i16, v4i32 load, select, and, or, xor to v2i64.
Craig Topper31a207a2012-05-04 06:39:13 +0000920 for (int i = MVT::v16i8; i != MVT::v2i64; ++i) {
Craig Topper0d1f1762012-08-12 00:34:56 +0000921 MVT VT = (MVT::SimpleValueType)i;
David Greene9b9838d2009-06-29 16:47:10 +0000922
923 // Do not attempt to promote non-128-bit vectors
Chris Lattner32b4b5a2010-07-05 05:53:14 +0000924 if (!VT.is128BitVector())
David Greene9b9838d2009-06-29 16:47:10 +0000925 continue;
Michael J. Spencerec38de22010-10-10 22:04:20 +0000926
Craig Topper0d1f1762012-08-12 00:34:56 +0000927 setOperationAction(ISD::AND, VT, Promote);
928 AddPromotedToType (ISD::AND, VT, MVT::v2i64);
929 setOperationAction(ISD::OR, VT, Promote);
930 AddPromotedToType (ISD::OR, VT, MVT::v2i64);
931 setOperationAction(ISD::XOR, VT, Promote);
932 AddPromotedToType (ISD::XOR, VT, MVT::v2i64);
933 setOperationAction(ISD::LOAD, VT, Promote);
934 AddPromotedToType (ISD::LOAD, VT, MVT::v2i64);
935 setOperationAction(ISD::SELECT, VT, Promote);
936 AddPromotedToType (ISD::SELECT, VT, MVT::v2i64);
Evan Chengf7c378e2006-04-10 07:23:14 +0000937 }
Evan Cheng2c3ae372006-04-12 21:21:57 +0000938
Owen Anderson825b72b2009-08-11 20:47:22 +0000939 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000940
Evan Cheng2c3ae372006-04-12 21:21:57 +0000941 // Custom lower v2i64 and v2f64 selects.
Owen Anderson825b72b2009-08-11 20:47:22 +0000942 setOperationAction(ISD::LOAD, MVT::v2f64, Legal);
943 setOperationAction(ISD::LOAD, MVT::v2i64, Legal);
944 setOperationAction(ISD::SELECT, MVT::v2f64, Custom);
945 setOperationAction(ISD::SELECT, MVT::v2i64, Custom);
Scott Michelfdc40a02009-02-17 22:15:04 +0000946
Owen Anderson825b72b2009-08-11 20:47:22 +0000947 setOperationAction(ISD::FP_TO_SINT, MVT::v4i32, Legal);
948 setOperationAction(ISD::SINT_TO_FP, MVT::v4i32, Legal);
Michael Liaob8150d82012-09-10 18:33:51 +0000949
Michael Liao9d796db2012-10-10 16:32:15 +0000950 setOperationAction(ISD::FP_EXTEND, MVT::v2f32, Custom);
Michael Liao44c2d612012-10-10 16:53:28 +0000951 setOperationAction(ISD::FP_ROUND, MVT::v2f32, Custom);
Michael Liao9d796db2012-10-10 16:32:15 +0000952
Michael Liaob8150d82012-09-10 18:33:51 +0000953 setLoadExtAction(ISD::EXTLOAD, MVT::v2f32, Legal);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000954 }
Evan Chengc7ce29b2009-02-13 22:36:38 +0000955
Craig Topperd0a31172012-01-10 06:37:29 +0000956 if (Subtarget->hasSSE41()) {
Benjamin Kramerb6533972011-12-09 15:44:03 +0000957 setOperationAction(ISD::FFLOOR, MVT::f32, Legal);
958 setOperationAction(ISD::FCEIL, MVT::f32, Legal);
959 setOperationAction(ISD::FTRUNC, MVT::f32, Legal);
960 setOperationAction(ISD::FRINT, MVT::f32, Legal);
961 setOperationAction(ISD::FNEARBYINT, MVT::f32, Legal);
962 setOperationAction(ISD::FFLOOR, MVT::f64, Legal);
963 setOperationAction(ISD::FCEIL, MVT::f64, Legal);
964 setOperationAction(ISD::FTRUNC, MVT::f64, Legal);
965 setOperationAction(ISD::FRINT, MVT::f64, Legal);
966 setOperationAction(ISD::FNEARBYINT, MVT::f64, Legal);
967
Craig Topper12fb5c62012-09-08 17:42:27 +0000968 setOperationAction(ISD::FFLOOR, MVT::v4f32, Legal);
969 setOperationAction(ISD::FFLOOR, MVT::v2f64, Legal);
970
Nate Begeman14d12ca2008-02-11 04:19:36 +0000971 // FIXME: Do we need to handle scalar-to-vector here?
Owen Anderson825b72b2009-08-11 20:47:22 +0000972 setOperationAction(ISD::MUL, MVT::v4i32, Legal);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000973
Nadav Rotemfbad25e2011-09-11 15:02:23 +0000974 setOperationAction(ISD::VSELECT, MVT::v2f64, Legal);
975 setOperationAction(ISD::VSELECT, MVT::v2i64, Legal);
976 setOperationAction(ISD::VSELECT, MVT::v16i8, Legal);
977 setOperationAction(ISD::VSELECT, MVT::v4i32, Legal);
978 setOperationAction(ISD::VSELECT, MVT::v4f32, Legal);
Nadav Rotemffe3e7d2011-09-08 08:11:19 +0000979
Nate Begeman14d12ca2008-02-11 04:19:36 +0000980 // i8 and i16 vectors are custom , because the source register and source
981 // source memory operand types are not the same width. f32 vectors are
982 // custom since the immediate controlling the insert encodes additional
983 // information.
Owen Anderson825b72b2009-08-11 20:47:22 +0000984 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v16i8, Custom);
985 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
986 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom);
987 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000988
Owen Anderson825b72b2009-08-11 20:47:22 +0000989 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v16i8, Custom);
990 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v8i16, Custom);
991 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4i32, Custom);
992 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000993
Pete Coopera77214a2011-11-14 19:38:42 +0000994 // FIXME: these should be Legal but thats only for the case where
Chad Rosier30450e82011-12-22 22:35:21 +0000995 // the index is constant. For now custom expand to deal with that.
Nate Begeman14d12ca2008-02-11 04:19:36 +0000996 if (Subtarget->is64Bit()) {
Pete Coopera77214a2011-11-14 19:38:42 +0000997 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i64, Custom);
998 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Custom);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000999 }
1000 }
Evan Cheng470a6ad2006-02-22 02:26:30 +00001001
Craig Topper1accb7e2012-01-10 06:54:16 +00001002 if (Subtarget->hasSSE2()) {
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +00001003 setOperationAction(ISD::SRL, MVT::v8i16, Custom);
Craig Topper7be5dfd2011-11-12 09:58:49 +00001004 setOperationAction(ISD::SRL, MVT::v16i8, Custom);
Nadav Rotem43012222011-05-11 08:12:09 +00001005
Nadav Rotem43012222011-05-11 08:12:09 +00001006 setOperationAction(ISD::SHL, MVT::v8i16, Custom);
Craig Topper7be5dfd2011-11-12 09:58:49 +00001007 setOperationAction(ISD::SHL, MVT::v16i8, Custom);
Nadav Rotem43012222011-05-11 08:12:09 +00001008
Nadav Rotem43012222011-05-11 08:12:09 +00001009 setOperationAction(ISD::SRA, MVT::v8i16, Custom);
Eli Friedmanf6aa6b12011-11-01 21:18:39 +00001010 setOperationAction(ISD::SRA, MVT::v16i8, Custom);
Craig Topper7be5dfd2011-11-12 09:58:49 +00001011
1012 if (Subtarget->hasAVX2()) {
1013 setOperationAction(ISD::SRL, MVT::v2i64, Legal);
1014 setOperationAction(ISD::SRL, MVT::v4i32, Legal);
1015
1016 setOperationAction(ISD::SHL, MVT::v2i64, Legal);
1017 setOperationAction(ISD::SHL, MVT::v4i32, Legal);
1018
1019 setOperationAction(ISD::SRA, MVT::v4i32, Legal);
1020 } else {
1021 setOperationAction(ISD::SRL, MVT::v2i64, Custom);
1022 setOperationAction(ISD::SRL, MVT::v4i32, Custom);
1023
1024 setOperationAction(ISD::SHL, MVT::v2i64, Custom);
1025 setOperationAction(ISD::SHL, MVT::v4i32, Custom);
1026
1027 setOperationAction(ISD::SRA, MVT::v4i32, Custom);
1028 }
Nadav Rotem43012222011-05-11 08:12:09 +00001029 }
1030
Nick Lewycky8a8d4792011-12-02 22:16:29 +00001031 if (!TM.Options.UseSoftFloat && Subtarget->hasAVX()) {
Craig Topperc9099502012-04-20 06:31:50 +00001032 addRegisterClass(MVT::v32i8, &X86::VR256RegClass);
1033 addRegisterClass(MVT::v16i16, &X86::VR256RegClass);
1034 addRegisterClass(MVT::v8i32, &X86::VR256RegClass);
1035 addRegisterClass(MVT::v8f32, &X86::VR256RegClass);
1036 addRegisterClass(MVT::v4i64, &X86::VR256RegClass);
1037 addRegisterClass(MVT::v4f64, &X86::VR256RegClass);
David Greened94c1012009-06-29 22:50:51 +00001038
Owen Anderson825b72b2009-08-11 20:47:22 +00001039 setOperationAction(ISD::LOAD, MVT::v8f32, Legal);
Owen Anderson825b72b2009-08-11 20:47:22 +00001040 setOperationAction(ISD::LOAD, MVT::v4f64, Legal);
1041 setOperationAction(ISD::LOAD, MVT::v4i64, Legal);
David Greene54d8eba2011-01-27 22:38:56 +00001042
Owen Anderson825b72b2009-08-11 20:47:22 +00001043 setOperationAction(ISD::FADD, MVT::v8f32, Legal);
1044 setOperationAction(ISD::FSUB, MVT::v8f32, Legal);
1045 setOperationAction(ISD::FMUL, MVT::v8f32, Legal);
1046 setOperationAction(ISD::FDIV, MVT::v8f32, Legal);
1047 setOperationAction(ISD::FSQRT, MVT::v8f32, Legal);
Craig Topper12fb5c62012-09-08 17:42:27 +00001048 setOperationAction(ISD::FFLOOR, MVT::v8f32, Legal);
Owen Anderson825b72b2009-08-11 20:47:22 +00001049 setOperationAction(ISD::FNEG, MVT::v8f32, Custom);
Craig Topper43620672012-09-08 07:31:51 +00001050 setOperationAction(ISD::FABS, MVT::v8f32, Custom);
David Greene9b9838d2009-06-29 16:47:10 +00001051
Owen Anderson825b72b2009-08-11 20:47:22 +00001052 setOperationAction(ISD::FADD, MVT::v4f64, Legal);
1053 setOperationAction(ISD::FSUB, MVT::v4f64, Legal);
1054 setOperationAction(ISD::FMUL, MVT::v4f64, Legal);
1055 setOperationAction(ISD::FDIV, MVT::v4f64, Legal);
1056 setOperationAction(ISD::FSQRT, MVT::v4f64, Legal);
Craig Topper12fb5c62012-09-08 17:42:27 +00001057 setOperationAction(ISD::FFLOOR, MVT::v4f64, Legal);
Owen Anderson825b72b2009-08-11 20:47:22 +00001058 setOperationAction(ISD::FNEG, MVT::v4f64, Custom);
Craig Topper43620672012-09-08 07:31:51 +00001059 setOperationAction(ISD::FABS, MVT::v4f64, Custom);
David Greene9b9838d2009-06-29 16:47:10 +00001060
Michael Liaobedcbd42012-10-16 18:14:11 +00001061 setOperationAction(ISD::TRUNCATE, MVT::v8i16, Custom);
1062
1063 setOperationAction(ISD::FP_TO_SINT, MVT::v8i16, Custom);
1064
Bruno Cardoso Lopes2e64ae42011-07-28 01:26:39 +00001065 setOperationAction(ISD::FP_TO_SINT, MVT::v8i32, Legal);
1066 setOperationAction(ISD::SINT_TO_FP, MVT::v8i32, Legal);
Bruno Cardoso Lopes55244ce2011-08-01 21:54:09 +00001067 setOperationAction(ISD::FP_ROUND, MVT::v4f32, Legal);
Bruno Cardoso Lopes2e64ae42011-07-28 01:26:39 +00001068
Michael Liaob8150d82012-09-10 18:33:51 +00001069 setLoadExtAction(ISD::EXTLOAD, MVT::v4f32, Legal);
1070
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +00001071 setOperationAction(ISD::SRL, MVT::v16i16, Custom);
1072 setOperationAction(ISD::SRL, MVT::v32i8, Custom);
1073
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +00001074 setOperationAction(ISD::SHL, MVT::v16i16, Custom);
1075 setOperationAction(ISD::SHL, MVT::v32i8, Custom);
1076
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +00001077 setOperationAction(ISD::SRA, MVT::v16i16, Custom);
Craig Topper7be5dfd2011-11-12 09:58:49 +00001078 setOperationAction(ISD::SRA, MVT::v32i8, Custom);
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +00001079
Duncan Sands28b77e92011-09-06 19:07:46 +00001080 setOperationAction(ISD::SETCC, MVT::v32i8, Custom);
1081 setOperationAction(ISD::SETCC, MVT::v16i16, Custom);
1082 setOperationAction(ISD::SETCC, MVT::v8i32, Custom);
1083 setOperationAction(ISD::SETCC, MVT::v4i64, Custom);
Bruno Cardoso Lopes0f0e0a02011-08-09 00:46:57 +00001084
Bruno Cardoso Lopesd40aa242011-08-09 23:27:13 +00001085 setOperationAction(ISD::SELECT, MVT::v4f64, Custom);
1086 setOperationAction(ISD::SELECT, MVT::v4i64, Custom);
1087 setOperationAction(ISD::SELECT, MVT::v8f32, Custom);
1088
Craig Topperaaa643c2011-11-09 07:28:55 +00001089 setOperationAction(ISD::VSELECT, MVT::v4f64, Legal);
1090 setOperationAction(ISD::VSELECT, MVT::v4i64, Legal);
1091 setOperationAction(ISD::VSELECT, MVT::v8i32, Legal);
1092 setOperationAction(ISD::VSELECT, MVT::v8f32, Legal);
Nadav Rotem8ffad562011-09-09 20:29:17 +00001093
Craig Topperbf404372012-08-31 15:40:30 +00001094 if (Subtarget->hasFMA() || Subtarget->hasFMA4()) {
Elena Demikhovsky1503aba2012-08-01 12:06:00 +00001095 setOperationAction(ISD::FMA, MVT::v8f32, Custom);
1096 setOperationAction(ISD::FMA, MVT::v4f64, Custom);
1097 setOperationAction(ISD::FMA, MVT::v4f32, Custom);
1098 setOperationAction(ISD::FMA, MVT::v2f64, Custom);
1099 setOperationAction(ISD::FMA, MVT::f32, Custom);
1100 setOperationAction(ISD::FMA, MVT::f64, Custom);
1101 }
Craig Topper880ef452012-08-11 22:34:26 +00001102
Craig Topperaaa643c2011-11-09 07:28:55 +00001103 if (Subtarget->hasAVX2()) {
1104 setOperationAction(ISD::ADD, MVT::v4i64, Legal);
1105 setOperationAction(ISD::ADD, MVT::v8i32, Legal);
1106 setOperationAction(ISD::ADD, MVT::v16i16, Legal);
1107 setOperationAction(ISD::ADD, MVT::v32i8, Legal);
Craig Topper13894fa2011-08-24 06:14:18 +00001108
Craig Topperaaa643c2011-11-09 07:28:55 +00001109 setOperationAction(ISD::SUB, MVT::v4i64, Legal);
1110 setOperationAction(ISD::SUB, MVT::v8i32, Legal);
1111 setOperationAction(ISD::SUB, MVT::v16i16, Legal);
1112 setOperationAction(ISD::SUB, MVT::v32i8, Legal);
Craig Topper13894fa2011-08-24 06:14:18 +00001113
Craig Topperaaa643c2011-11-09 07:28:55 +00001114 setOperationAction(ISD::MUL, MVT::v4i64, Custom);
1115 setOperationAction(ISD::MUL, MVT::v8i32, Legal);
1116 setOperationAction(ISD::MUL, MVT::v16i16, Legal);
Craig Topper46154eb2011-11-11 07:39:23 +00001117 // Don't lower v32i8 because there is no 128-bit byte mul
Nadav Rotembb539bf2011-11-09 13:21:28 +00001118
1119 setOperationAction(ISD::VSELECT, MVT::v32i8, Legal);
Craig Topper7be5dfd2011-11-12 09:58:49 +00001120
1121 setOperationAction(ISD::SRL, MVT::v4i64, Legal);
1122 setOperationAction(ISD::SRL, MVT::v8i32, Legal);
1123
1124 setOperationAction(ISD::SHL, MVT::v4i64, Legal);
1125 setOperationAction(ISD::SHL, MVT::v8i32, Legal);
1126
1127 setOperationAction(ISD::SRA, MVT::v8i32, Legal);
Craig Topperaaa643c2011-11-09 07:28:55 +00001128 } else {
1129 setOperationAction(ISD::ADD, MVT::v4i64, Custom);
1130 setOperationAction(ISD::ADD, MVT::v8i32, Custom);
1131 setOperationAction(ISD::ADD, MVT::v16i16, Custom);
1132 setOperationAction(ISD::ADD, MVT::v32i8, Custom);
1133
1134 setOperationAction(ISD::SUB, MVT::v4i64, Custom);
1135 setOperationAction(ISD::SUB, MVT::v8i32, Custom);
1136 setOperationAction(ISD::SUB, MVT::v16i16, Custom);
1137 setOperationAction(ISD::SUB, MVT::v32i8, Custom);
1138
1139 setOperationAction(ISD::MUL, MVT::v4i64, Custom);
1140 setOperationAction(ISD::MUL, MVT::v8i32, Custom);
1141 setOperationAction(ISD::MUL, MVT::v16i16, Custom);
1142 // Don't lower v32i8 because there is no 128-bit byte mul
Craig Topper7be5dfd2011-11-12 09:58:49 +00001143
1144 setOperationAction(ISD::SRL, MVT::v4i64, Custom);
1145 setOperationAction(ISD::SRL, MVT::v8i32, Custom);
1146
1147 setOperationAction(ISD::SHL, MVT::v4i64, Custom);
1148 setOperationAction(ISD::SHL, MVT::v8i32, Custom);
1149
1150 setOperationAction(ISD::SRA, MVT::v8i32, Custom);
Craig Topperaaa643c2011-11-09 07:28:55 +00001151 }
Craig Topper13894fa2011-08-24 06:14:18 +00001152
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001153 // Custom lower several nodes for 256-bit types.
Jakub Staszak6610b1d2012-04-29 20:52:53 +00001154 for (int i = MVT::FIRST_VECTOR_VALUETYPE;
1155 i <= MVT::LAST_VECTOR_VALUETYPE; ++i) {
Craig Topper0d1f1762012-08-12 00:34:56 +00001156 MVT VT = (MVT::SimpleValueType)i;
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001157
1158 // Extract subvector is special because the value type
1159 // (result) is 128-bit but the source is 256-bit wide.
1160 if (VT.is128BitVector())
Craig Topper0d1f1762012-08-12 00:34:56 +00001161 setOperationAction(ISD::EXTRACT_SUBVECTOR, VT, Custom);
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001162
1163 // Do not attempt to custom lower other non-256-bit vectors
1164 if (!VT.is256BitVector())
David Greene9b9838d2009-06-29 16:47:10 +00001165 continue;
David Greene54d8eba2011-01-27 22:38:56 +00001166
Craig Topper0d1f1762012-08-12 00:34:56 +00001167 setOperationAction(ISD::BUILD_VECTOR, VT, Custom);
1168 setOperationAction(ISD::VECTOR_SHUFFLE, VT, Custom);
1169 setOperationAction(ISD::INSERT_VECTOR_ELT, VT, Custom);
1170 setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT, Custom);
1171 setOperationAction(ISD::SCALAR_TO_VECTOR, VT, Custom);
1172 setOperationAction(ISD::INSERT_SUBVECTOR, VT, Custom);
1173 setOperationAction(ISD::CONCAT_VECTORS, VT, Custom);
David Greene9b9838d2009-06-29 16:47:10 +00001174 }
1175
David Greene54d8eba2011-01-27 22:38:56 +00001176 // Promote v32i8, v16i16, v8i32 select, and, or, xor to v4i64.
Jakub Staszak6610b1d2012-04-29 20:52:53 +00001177 for (int i = MVT::v32i8; i != MVT::v4i64; ++i) {
Craig Topper0d1f1762012-08-12 00:34:56 +00001178 MVT VT = (MVT::SimpleValueType)i;
David Greene54d8eba2011-01-27 22:38:56 +00001179
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001180 // Do not attempt to promote non-256-bit vectors
1181 if (!VT.is256BitVector())
David Greene54d8eba2011-01-27 22:38:56 +00001182 continue;
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001183
Craig Topper0d1f1762012-08-12 00:34:56 +00001184 setOperationAction(ISD::AND, VT, Promote);
1185 AddPromotedToType (ISD::AND, VT, MVT::v4i64);
1186 setOperationAction(ISD::OR, VT, Promote);
1187 AddPromotedToType (ISD::OR, VT, MVT::v4i64);
1188 setOperationAction(ISD::XOR, VT, Promote);
1189 AddPromotedToType (ISD::XOR, VT, MVT::v4i64);
1190 setOperationAction(ISD::LOAD, VT, Promote);
1191 AddPromotedToType (ISD::LOAD, VT, MVT::v4i64);
1192 setOperationAction(ISD::SELECT, VT, Promote);
1193 AddPromotedToType (ISD::SELECT, VT, MVT::v4i64);
David Greene54d8eba2011-01-27 22:38:56 +00001194 }
David Greene9b9838d2009-06-29 16:47:10 +00001195 }
1196
Nadav Rotemd0f3ef82011-07-14 11:11:14 +00001197 // SIGN_EXTEND_INREGs are evaluated by the extend type. Handle the expansion
1198 // of this type with custom code.
Jakub Staszak6610b1d2012-04-29 20:52:53 +00001199 for (int VT = MVT::FIRST_VECTOR_VALUETYPE;
1200 VT != MVT::LAST_VECTOR_VALUETYPE; VT++) {
Chad Rosier30450e82011-12-22 22:35:21 +00001201 setOperationAction(ISD::SIGN_EXTEND_INREG, (MVT::SimpleValueType)VT,
1202 Custom);
Nadav Rotemd0f3ef82011-07-14 11:11:14 +00001203 }
1204
Evan Cheng6be2c582006-04-05 23:38:46 +00001205 // We want to custom lower some of our intrinsics.
Owen Anderson825b72b2009-08-11 20:47:22 +00001206 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
Benjamin Kramerb9bee042012-07-12 09:31:43 +00001207 setOperationAction(ISD::INTRINSIC_W_CHAIN, MVT::Other, Custom);
Evan Cheng6be2c582006-04-05 23:38:46 +00001208
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00001209
Eli Friedman962f5492010-06-02 19:35:46 +00001210 // Only custom-lower 64-bit SADDO and friends on 64-bit because we don't
1211 // handle type legalization for these operations here.
Dan Gohman71c62a22010-06-02 19:13:40 +00001212 //
Eli Friedman962f5492010-06-02 19:35:46 +00001213 // FIXME: We really should do custom legalization for addition and
1214 // subtraction on x86-32 once PR3203 is fixed. We really can't do much better
1215 // than generic legalization for 64-bit multiplication-with-overflow, though.
Chris Lattnera34b3cf2010-12-19 20:03:11 +00001216 for (unsigned i = 0, e = 3+Subtarget->is64Bit(); i != e; ++i) {
1217 // Add/Sub/Mul with overflow operations are custom lowered.
1218 MVT VT = IntVTs[i];
1219 setOperationAction(ISD::SADDO, VT, Custom);
1220 setOperationAction(ISD::UADDO, VT, Custom);
1221 setOperationAction(ISD::SSUBO, VT, Custom);
1222 setOperationAction(ISD::USUBO, VT, Custom);
1223 setOperationAction(ISD::SMULO, VT, Custom);
1224 setOperationAction(ISD::UMULO, VT, Custom);
Eli Friedmana993f0a2010-06-02 00:27:18 +00001225 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00001226
Chris Lattnera34b3cf2010-12-19 20:03:11 +00001227 // There are no 8-bit 3-address imul/mul instructions
1228 setOperationAction(ISD::SMULO, MVT::i8, Expand);
1229 setOperationAction(ISD::UMULO, MVT::i8, Expand);
Bill Wendling41ea7e72008-11-24 19:21:46 +00001230
Evan Chengd54f2d52009-03-31 19:38:51 +00001231 if (!Subtarget->is64Bit()) {
1232 // These libcalls are not available in 32-bit.
1233 setLibcallName(RTLIB::SHL_I128, 0);
1234 setLibcallName(RTLIB::SRL_I128, 0);
1235 setLibcallName(RTLIB::SRA_I128, 0);
1236 }
1237
Evan Cheng206ee9d2006-07-07 08:33:52 +00001238 // We have target-specific dag combine patterns for the following nodes:
1239 setTargetDAGCombine(ISD::VECTOR_SHUFFLE);
Dan Gohman1bbf72b2010-03-15 23:23:03 +00001240 setTargetDAGCombine(ISD::EXTRACT_VECTOR_ELT);
Duncan Sands6bcd2192011-09-17 16:49:39 +00001241 setTargetDAGCombine(ISD::VSELECT);
Chris Lattner83e6c992006-10-04 06:57:07 +00001242 setTargetDAGCombine(ISD::SELECT);
Nate Begeman740ab032009-01-26 00:52:55 +00001243 setTargetDAGCombine(ISD::SHL);
1244 setTargetDAGCombine(ISD::SRA);
1245 setTargetDAGCombine(ISD::SRL);
Evan Cheng760d1942010-01-04 21:22:48 +00001246 setTargetDAGCombine(ISD::OR);
Nate Begemanb65c1752010-12-17 22:55:37 +00001247 setTargetDAGCombine(ISD::AND);
Benjamin Kramer7d6fe132010-12-21 21:41:44 +00001248 setTargetDAGCombine(ISD::ADD);
Duncan Sands17470be2011-09-22 20:15:48 +00001249 setTargetDAGCombine(ISD::FADD);
1250 setTargetDAGCombine(ISD::FSUB);
Elena Demikhovsky1503aba2012-08-01 12:06:00 +00001251 setTargetDAGCombine(ISD::FMA);
Benjamin Kramer7d6fe132010-12-21 21:41:44 +00001252 setTargetDAGCombine(ISD::SUB);
Nadav Rotem91e43fd2011-09-18 10:39:32 +00001253 setTargetDAGCombine(ISD::LOAD);
Chris Lattner149a4e52008-02-22 02:09:43 +00001254 setTargetDAGCombine(ISD::STORE);
Evan Cheng2e489c42009-12-16 00:53:11 +00001255 setTargetDAGCombine(ISD::ZERO_EXTEND);
Elena Demikhovsky1da58672012-04-22 09:39:03 +00001256 setTargetDAGCombine(ISD::ANY_EXTEND);
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +00001257 setTargetDAGCombine(ISD::SIGN_EXTEND);
Elena Demikhovsky3ae98152012-02-01 07:56:44 +00001258 setTargetDAGCombine(ISD::TRUNCATE);
Nadav Rotema3540772012-04-23 21:53:37 +00001259 setTargetDAGCombine(ISD::UINT_TO_FP);
Stuart Hastingsf99a4b82011-06-06 23:15:58 +00001260 setTargetDAGCombine(ISD::SINT_TO_FP);
Chad Rosiera73b6fc2012-04-27 22:33:25 +00001261 setTargetDAGCombine(ISD::SETCC);
Evan Cheng0b0cd912009-03-28 05:57:29 +00001262 if (Subtarget->is64Bit())
1263 setTargetDAGCombine(ISD::MUL);
Manman Ren92363622012-06-07 22:39:10 +00001264 setTargetDAGCombine(ISD::XOR);
Evan Cheng206ee9d2006-07-07 08:33:52 +00001265
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001266 computeRegisterProperties();
1267
Evan Cheng05219282011-01-06 06:52:41 +00001268 // On Darwin, -Os means optimize for size without hurting performance,
1269 // do not reduce the limit.
Dan Gohman87060f52008-06-30 21:00:56 +00001270 maxStoresPerMemset = 16; // For @llvm.memset -> sequence of stores
Evan Cheng05219282011-01-06 06:52:41 +00001271 maxStoresPerMemsetOptSize = Subtarget->isTargetDarwin() ? 16 : 8;
Evan Cheng255f20f2010-04-01 06:04:33 +00001272 maxStoresPerMemcpy = 8; // For @llvm.memcpy -> sequence of stores
Evan Cheng05219282011-01-06 06:52:41 +00001273 maxStoresPerMemcpyOptSize = Subtarget->isTargetDarwin() ? 8 : 4;
1274 maxStoresPerMemmove = 8; // For @llvm.memmove -> sequence of stores
1275 maxStoresPerMemmoveOptSize = Subtarget->isTargetDarwin() ? 8 : 4;
Jakob Stoklund Olesen8c741b82011-12-06 01:26:19 +00001276 setPrefLoopAlignment(4); // 2^4 bytes.
Evan Cheng6ebf7bc2009-05-13 21:42:09 +00001277 benefitFromCodePlacementOpt = true;
Eli Friedmanfc5d3052011-05-06 20:34:06 +00001278
Benjamin Krameraaf723d2012-05-05 12:49:14 +00001279 // Predictable cmov don't hurt on atom because it's in-order.
1280 predictableSelectIsExpensive = !Subtarget->isAtom();
1281
Jakob Stoklund Olesen8c741b82011-12-06 01:26:19 +00001282 setPrefFunctionAlignment(4); // 2^4 bytes.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001283}
1284
Scott Michel5b8f82e2008-03-10 15:42:14 +00001285
Duncan Sands28b77e92011-09-06 19:07:46 +00001286EVT X86TargetLowering::getSetCCResultType(EVT VT) const {
1287 if (!VT.isVector()) return MVT::i8;
1288 return VT.changeVectorElementTypeToInteger();
Scott Michel5b8f82e2008-03-10 15:42:14 +00001289}
1290
1291
Evan Cheng29286502008-01-23 23:17:41 +00001292/// getMaxByValAlign - Helper for getByValTypeAlignment to determine
1293/// the desired ByVal argument alignment.
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001294static void getMaxByValAlign(Type *Ty, unsigned &MaxAlign) {
Evan Cheng29286502008-01-23 23:17:41 +00001295 if (MaxAlign == 16)
1296 return;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001297 if (VectorType *VTy = dyn_cast<VectorType>(Ty)) {
Evan Cheng29286502008-01-23 23:17:41 +00001298 if (VTy->getBitWidth() == 128)
1299 MaxAlign = 16;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001300 } else if (ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
Evan Cheng29286502008-01-23 23:17:41 +00001301 unsigned EltAlign = 0;
1302 getMaxByValAlign(ATy->getElementType(), EltAlign);
1303 if (EltAlign > MaxAlign)
1304 MaxAlign = EltAlign;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001305 } else if (StructType *STy = dyn_cast<StructType>(Ty)) {
Evan Cheng29286502008-01-23 23:17:41 +00001306 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
1307 unsigned EltAlign = 0;
1308 getMaxByValAlign(STy->getElementType(i), EltAlign);
1309 if (EltAlign > MaxAlign)
1310 MaxAlign = EltAlign;
1311 if (MaxAlign == 16)
1312 break;
1313 }
1314 }
Evan Cheng29286502008-01-23 23:17:41 +00001315}
1316
1317/// getByValTypeAlignment - Return the desired alignment for ByVal aggregate
1318/// function arguments in the caller parameter area. For X86, aggregates
Dale Johannesen0c191872008-02-08 19:48:20 +00001319/// that contain SSE vectors are placed at 16-byte boundaries while the rest
1320/// are at 4-byte boundaries.
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001321unsigned X86TargetLowering::getByValTypeAlignment(Type *Ty) const {
Evan Cheng1887c1c2008-08-21 21:00:15 +00001322 if (Subtarget->is64Bit()) {
1323 // Max of 8 and alignment of type.
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00001324 unsigned TyAlign = TD->getABITypeAlignment(Ty);
Evan Cheng1887c1c2008-08-21 21:00:15 +00001325 if (TyAlign > 8)
1326 return TyAlign;
1327 return 8;
1328 }
1329
Evan Cheng29286502008-01-23 23:17:41 +00001330 unsigned Align = 4;
Craig Topper1accb7e2012-01-10 06:54:16 +00001331 if (Subtarget->hasSSE1())
Dale Johannesen0c191872008-02-08 19:48:20 +00001332 getMaxByValAlign(Ty, Align);
Evan Cheng29286502008-01-23 23:17:41 +00001333 return Align;
1334}
Chris Lattner2b02a442007-02-25 08:29:00 +00001335
Evan Chengf0df0312008-05-15 08:39:06 +00001336/// getOptimalMemOpType - Returns the target specific optimal type for load
Evan Chengc3b0c342010-04-08 07:37:57 +00001337/// and store operations as a result of memset, memcpy, and memmove
1338/// lowering. If DstAlign is zero that means it's safe to destination
1339/// alignment can satisfy any constraint. Similarly if SrcAlign is zero it
1340/// means there isn't a need to check it against alignment requirement,
1341/// probably because the source does not need to be loaded. If
Lang Hames15701f82011-10-26 23:50:43 +00001342/// 'IsZeroVal' is true, that means it's safe to return a
Evan Chengc3b0c342010-04-08 07:37:57 +00001343/// non-scalar-integer type, e.g. empty string source, constant, or loaded
1344/// from memory. 'MemcpyStrSrc' indicates whether the memcpy source is
1345/// constant so it does not need to be loaded.
Dan Gohman37f32ee2010-04-16 20:11:05 +00001346/// It returns EVT::Other if the type should be determined using generic
1347/// target-independent logic.
Owen Andersone50ed302009-08-10 22:56:29 +00001348EVT
Evan Cheng255f20f2010-04-01 06:04:33 +00001349X86TargetLowering::getOptimalMemOpType(uint64_t Size,
1350 unsigned DstAlign, unsigned SrcAlign,
Lang Hames15701f82011-10-26 23:50:43 +00001351 bool IsZeroVal,
Evan Chengc3b0c342010-04-08 07:37:57 +00001352 bool MemcpyStrSrc,
Dan Gohman37f32ee2010-04-16 20:11:05 +00001353 MachineFunction &MF) const {
Chris Lattner4002a1b2008-10-28 05:49:35 +00001354 // FIXME: This turns off use of xmm stores for memset/memcpy on targets like
1355 // linux. This is because the stack realignment code can't handle certain
1356 // cases like PR2962. This should be removed when PR2962 is fixed.
Dan Gohman37f32ee2010-04-16 20:11:05 +00001357 const Function *F = MF.getFunction();
Lang Hames15701f82011-10-26 23:50:43 +00001358 if (IsZeroVal &&
Bill Wendling67658342012-10-09 07:45:08 +00001359 !F->getFnAttributes().hasAttribute(Attributes::NoImplicitFloat)) {
Evan Cheng255f20f2010-04-01 06:04:33 +00001360 if (Size >= 16 &&
Evan Chenga5e13622011-01-07 19:35:30 +00001361 (Subtarget->isUnalignedMemAccessFast() ||
1362 ((DstAlign == 0 || DstAlign >= 16) &&
1363 (SrcAlign == 0 || SrcAlign >= 16))) &&
Evan Cheng255f20f2010-04-01 06:04:33 +00001364 Subtarget->getStackAlignment() >= 16) {
Craig Topper562659f2012-01-13 08:32:21 +00001365 if (Subtarget->getStackAlignment() >= 32) {
1366 if (Subtarget->hasAVX2())
1367 return MVT::v8i32;
1368 if (Subtarget->hasAVX())
1369 return MVT::v8f32;
1370 }
Craig Topper1accb7e2012-01-10 06:54:16 +00001371 if (Subtarget->hasSSE2())
Evan Cheng255f20f2010-04-01 06:04:33 +00001372 return MVT::v4i32;
Craig Topper1accb7e2012-01-10 06:54:16 +00001373 if (Subtarget->hasSSE1())
Evan Cheng255f20f2010-04-01 06:04:33 +00001374 return MVT::v4f32;
Evan Chengc3b0c342010-04-08 07:37:57 +00001375 } else if (!MemcpyStrSrc && Size >= 8 &&
Evan Cheng3ea97552010-04-01 20:27:45 +00001376 !Subtarget->is64Bit() &&
Evan Cheng255f20f2010-04-01 06:04:33 +00001377 Subtarget->getStackAlignment() >= 8 &&
Craig Topper1accb7e2012-01-10 06:54:16 +00001378 Subtarget->hasSSE2()) {
Evan Chengc3b0c342010-04-08 07:37:57 +00001379 // Do not use f64 to lower memcpy if source is string constant. It's
1380 // better to use i32 to avoid the loads.
Evan Cheng255f20f2010-04-01 06:04:33 +00001381 return MVT::f64;
Evan Chengc3b0c342010-04-08 07:37:57 +00001382 }
Chris Lattner4002a1b2008-10-28 05:49:35 +00001383 }
Evan Chengf0df0312008-05-15 08:39:06 +00001384 if (Subtarget->is64Bit() && Size >= 8)
Owen Anderson825b72b2009-08-11 20:47:22 +00001385 return MVT::i64;
1386 return MVT::i32;
Evan Chengf0df0312008-05-15 08:39:06 +00001387}
1388
Chris Lattner5e1df8d2010-01-25 23:38:14 +00001389/// getJumpTableEncoding - Return the entry encoding for a jump table in the
1390/// current function. The returned value is a member of the
1391/// MachineJumpTableInfo::JTEntryKind enum.
1392unsigned X86TargetLowering::getJumpTableEncoding() const {
1393 // In GOT pic mode, each entry in the jump table is emitted as a @GOTOFF
1394 // symbol.
1395 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1396 Subtarget->isPICStyleGOT())
Chris Lattnerc64daab2010-01-26 05:02:42 +00001397 return MachineJumpTableInfo::EK_Custom32;
Michael J. Spencerec38de22010-10-10 22:04:20 +00001398
Chris Lattner5e1df8d2010-01-25 23:38:14 +00001399 // Otherwise, use the normal jump table encoding heuristics.
1400 return TargetLowering::getJumpTableEncoding();
1401}
1402
Chris Lattnerc64daab2010-01-26 05:02:42 +00001403const MCExpr *
1404X86TargetLowering::LowerCustomJumpTableEntry(const MachineJumpTableInfo *MJTI,
1405 const MachineBasicBlock *MBB,
1406 unsigned uid,MCContext &Ctx) const{
1407 assert(getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1408 Subtarget->isPICStyleGOT());
1409 // In 32-bit ELF systems, our jump table entries are formed with @GOTOFF
1410 // entries.
Daniel Dunbar4e815f82010-03-15 23:51:06 +00001411 return MCSymbolRefExpr::Create(MBB->getSymbol(),
1412 MCSymbolRefExpr::VK_GOTOFF, Ctx);
Chris Lattnerc64daab2010-01-26 05:02:42 +00001413}
1414
Evan Chengcc415862007-11-09 01:32:10 +00001415/// getPICJumpTableRelocaBase - Returns relocation base for the given PIC
1416/// jumptable.
Dan Gohman475871a2008-07-27 21:46:04 +00001417SDValue X86TargetLowering::getPICJumpTableRelocBase(SDValue Table,
Chris Lattner589c6f62010-01-26 06:28:43 +00001418 SelectionDAG &DAG) const {
Chris Lattnere4df7562009-07-09 03:15:51 +00001419 if (!Subtarget->is64Bit())
Dale Johannesenb300d2a2009-02-07 00:55:49 +00001420 // This doesn't have DebugLoc associated with it, but is not really the
1421 // same as a Register.
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00001422 return DAG.getNode(X86ISD::GlobalBaseReg, DebugLoc(), getPointerTy());
Evan Chengcc415862007-11-09 01:32:10 +00001423 return Table;
1424}
1425
Chris Lattner589c6f62010-01-26 06:28:43 +00001426/// getPICJumpTableRelocBaseExpr - This returns the relocation base for the
1427/// given PIC jumptable, the same as getPICJumpTableRelocBase, but as an
1428/// MCExpr.
1429const MCExpr *X86TargetLowering::
1430getPICJumpTableRelocBaseExpr(const MachineFunction *MF, unsigned JTI,
1431 MCContext &Ctx) const {
1432 // X86-64 uses RIP relative addressing based on the jump table label.
1433 if (Subtarget->isPICStyleRIPRel())
1434 return TargetLowering::getPICJumpTableRelocBaseExpr(MF, JTI, Ctx);
1435
1436 // Otherwise, the reference is relative to the PIC base.
Chris Lattner142b5312010-11-14 22:48:15 +00001437 return MCSymbolRefExpr::Create(MF->getPICBaseSymbol(), Ctx);
Chris Lattner589c6f62010-01-26 06:28:43 +00001438}
1439
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00001440// FIXME: Why this routine is here? Move to RegInfo!
Evan Chengdee81012010-07-26 21:50:05 +00001441std::pair<const TargetRegisterClass*, uint8_t>
1442X86TargetLowering::findRepresentativeClass(EVT VT) const{
1443 const TargetRegisterClass *RRC = 0;
1444 uint8_t Cost = 1;
1445 switch (VT.getSimpleVT().SimpleTy) {
1446 default:
1447 return TargetLowering::findRepresentativeClass(VT);
1448 case MVT::i8: case MVT::i16: case MVT::i32: case MVT::i64:
Craig Topperc9099502012-04-20 06:31:50 +00001449 RRC = Subtarget->is64Bit() ?
1450 (const TargetRegisterClass*)&X86::GR64RegClass :
1451 (const TargetRegisterClass*)&X86::GR32RegClass;
Evan Chengdee81012010-07-26 21:50:05 +00001452 break;
Dale Johannesen0488fb62010-09-30 23:57:10 +00001453 case MVT::x86mmx:
Craig Topperc9099502012-04-20 06:31:50 +00001454 RRC = &X86::VR64RegClass;
Evan Chengdee81012010-07-26 21:50:05 +00001455 break;
1456 case MVT::f32: case MVT::f64:
1457 case MVT::v16i8: case MVT::v8i16: case MVT::v4i32: case MVT::v2i64:
1458 case MVT::v4f32: case MVT::v2f64:
1459 case MVT::v32i8: case MVT::v8i32: case MVT::v4i64: case MVT::v8f32:
1460 case MVT::v4f64:
Craig Topperc9099502012-04-20 06:31:50 +00001461 RRC = &X86::VR128RegClass;
Evan Chengdee81012010-07-26 21:50:05 +00001462 break;
1463 }
1464 return std::make_pair(RRC, Cost);
1465}
1466
Eric Christopherf7a0c7b2010-07-06 05:18:56 +00001467bool X86TargetLowering::getStackCookieLocation(unsigned &AddressSpace,
1468 unsigned &Offset) const {
1469 if (!Subtarget->isTargetLinux())
1470 return false;
1471
1472 if (Subtarget->is64Bit()) {
1473 // %fs:0x28, unless we're using a Kernel code model, in which case it's %gs:
1474 Offset = 0x28;
1475 if (getTargetMachine().getCodeModel() == CodeModel::Kernel)
1476 AddressSpace = 256;
1477 else
1478 AddressSpace = 257;
1479 } else {
1480 // %gs:0x14 on i386
1481 Offset = 0x14;
1482 AddressSpace = 256;
1483 }
1484 return true;
1485}
1486
1487
Chris Lattner2b02a442007-02-25 08:29:00 +00001488//===----------------------------------------------------------------------===//
1489// Return Value Calling Convention Implementation
1490//===----------------------------------------------------------------------===//
1491
Chris Lattner59ed56b2007-02-28 04:55:35 +00001492#include "X86GenCallingConv.inc"
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001493
Michael J. Spencerec38de22010-10-10 22:04:20 +00001494bool
Eric Christopher471e4222011-06-08 23:55:35 +00001495X86TargetLowering::CanLowerReturn(CallingConv::ID CallConv,
Craig Topper0fbf3642012-04-23 03:28:34 +00001496 MachineFunction &MF, bool isVarArg,
Dan Gohman84023e02010-07-10 09:00:22 +00001497 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9af33c2010-07-06 22:19:37 +00001498 LLVMContext &Context) const {
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +00001499 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00001500 CCState CCInfo(CallConv, isVarArg, MF, getTargetMachine(),
Dan Gohmanc9af33c2010-07-06 22:19:37 +00001501 RVLocs, Context);
Dan Gohman84023e02010-07-10 09:00:22 +00001502 return CCInfo.CheckReturn(Outs, RetCC_X86);
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +00001503}
1504
Dan Gohman98ca4f22009-08-05 01:29:28 +00001505SDValue
1506X86TargetLowering::LowerReturn(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001507 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001508 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001509 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohmand858e902010-04-17 15:26:15 +00001510 DebugLoc dl, SelectionDAG &DAG) const {
Dan Gohman1e93df62010-04-17 14:41:14 +00001511 MachineFunction &MF = DAG.getMachineFunction();
1512 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
Scott Michelfdc40a02009-02-17 22:15:04 +00001513
Chris Lattner9774c912007-02-27 05:28:59 +00001514 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00001515 CCState CCInfo(CallConv, isVarArg, MF, getTargetMachine(),
Dan Gohman98ca4f22009-08-05 01:29:28 +00001516 RVLocs, *DAG.getContext());
1517 CCInfo.AnalyzeReturn(Outs, RetCC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001518
Evan Chengdcea1632010-02-04 02:40:39 +00001519 // Add the regs to the liveout set for the function.
1520 MachineRegisterInfo &MRI = DAG.getMachineFunction().getRegInfo();
1521 for (unsigned i = 0; i != RVLocs.size(); ++i)
1522 if (RVLocs[i].isRegLoc() && !MRI.isLiveOut(RVLocs[i].getLocReg()))
1523 MRI.addLiveOut(RVLocs[i].getLocReg());
Scott Michelfdc40a02009-02-17 22:15:04 +00001524
Dan Gohman475871a2008-07-27 21:46:04 +00001525 SDValue Flag;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001526
Dan Gohman475871a2008-07-27 21:46:04 +00001527 SmallVector<SDValue, 6> RetOps;
Chris Lattner447ff682008-03-11 03:23:40 +00001528 RetOps.push_back(Chain); // Operand #0 = Chain (updated below)
1529 // Operand #1 = Bytes To Pop
Dan Gohman1e93df62010-04-17 14:41:14 +00001530 RetOps.push_back(DAG.getTargetConstant(FuncInfo->getBytesToPopOnReturn(),
1531 MVT::i16));
Scott Michelfdc40a02009-02-17 22:15:04 +00001532
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001533 // Copy the result values into the output registers.
Chris Lattner8e6da152008-03-10 21:08:41 +00001534 for (unsigned i = 0; i != RVLocs.size(); ++i) {
1535 CCValAssign &VA = RVLocs[i];
1536 assert(VA.isRegLoc() && "Can only return in registers!");
Dan Gohmanc9403652010-07-07 15:54:55 +00001537 SDValue ValToCopy = OutVals[i];
Dale Johannesenc76d23f2010-07-23 00:30:35 +00001538 EVT ValVT = ValToCopy.getValueType();
1539
Jakob Stoklund Olesenee66b412012-05-31 17:28:20 +00001540 // Promote values to the appropriate types
1541 if (VA.getLocInfo() == CCValAssign::SExt)
1542 ValToCopy = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), ValToCopy);
1543 else if (VA.getLocInfo() == CCValAssign::ZExt)
1544 ValToCopy = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), ValToCopy);
1545 else if (VA.getLocInfo() == CCValAssign::AExt)
1546 ValToCopy = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), ValToCopy);
1547 else if (VA.getLocInfo() == CCValAssign::BCvt)
1548 ValToCopy = DAG.getNode(ISD::BITCAST, dl, VA.getLocVT(), ValToCopy);
1549
Dale Johannesenc4510512010-09-24 19:05:48 +00001550 // If this is x86-64, and we disabled SSE, we can't return FP values,
1551 // or SSE or MMX vectors.
1552 if ((ValVT == MVT::f32 || ValVT == MVT::f64 ||
1553 VA.getLocReg() == X86::XMM0 || VA.getLocReg() == X86::XMM1) &&
Craig Topper1accb7e2012-01-10 06:54:16 +00001554 (Subtarget->is64Bit() && !Subtarget->hasSSE1())) {
Dale Johannesenc76d23f2010-07-23 00:30:35 +00001555 report_fatal_error("SSE register return with SSE disabled");
1556 }
1557 // Likewise we can't return F64 values with SSE1 only. gcc does so, but
1558 // llvm-gcc has never done it right and no one has noticed, so this
1559 // should be OK for now.
1560 if (ValVT == MVT::f64 &&
Craig Topper1accb7e2012-01-10 06:54:16 +00001561 (Subtarget->is64Bit() && !Subtarget->hasSSE2()))
Dale Johannesenc76d23f2010-07-23 00:30:35 +00001562 report_fatal_error("SSE2 register return with SSE2 disabled");
Scott Michelfdc40a02009-02-17 22:15:04 +00001563
Chris Lattner447ff682008-03-11 03:23:40 +00001564 // Returns in ST0/ST1 are handled specially: these are pushed as operands to
1565 // the RET instruction and handled by the FP Stackifier.
Dan Gohman37eed792009-02-04 17:28:58 +00001566 if (VA.getLocReg() == X86::ST0 ||
1567 VA.getLocReg() == X86::ST1) {
Chris Lattner447ff682008-03-11 03:23:40 +00001568 // If this is a copy from an xmm register to ST(0), use an FPExtend to
1569 // change the value to the FP stack register class.
Dan Gohman37eed792009-02-04 17:28:58 +00001570 if (isScalarFPTypeInSSEReg(VA.getValVT()))
Owen Anderson825b72b2009-08-11 20:47:22 +00001571 ValToCopy = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f80, ValToCopy);
Chris Lattner447ff682008-03-11 03:23:40 +00001572 RetOps.push_back(ValToCopy);
1573 // Don't emit a copytoreg.
1574 continue;
1575 }
Dale Johannesena68f9012008-06-24 22:01:44 +00001576
Evan Cheng242b38b2009-02-23 09:03:22 +00001577 // 64-bit vector (MMX) values are returned in XMM0 / XMM1 except for v1i64
1578 // which is returned in RAX / RDX.
Evan Cheng6140a8b2009-02-22 08:05:12 +00001579 if (Subtarget->is64Bit()) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00001580 if (ValVT == MVT::x86mmx) {
Chris Lattner97a2a562010-08-26 05:24:29 +00001581 if (VA.getLocReg() == X86::XMM0 || VA.getLocReg() == X86::XMM1) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001582 ValToCopy = DAG.getNode(ISD::BITCAST, dl, MVT::i64, ValToCopy);
Eric Christopher90eb4022010-07-22 00:26:08 +00001583 ValToCopy = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i64,
1584 ValToCopy);
Chris Lattner97a2a562010-08-26 05:24:29 +00001585 // If we don't have SSE2 available, convert to v4f32 so the generated
1586 // register is legal.
Craig Topper1accb7e2012-01-10 06:54:16 +00001587 if (!Subtarget->hasSSE2())
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001588 ValToCopy = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32,ValToCopy);
Chris Lattner97a2a562010-08-26 05:24:29 +00001589 }
Evan Cheng242b38b2009-02-23 09:03:22 +00001590 }
Evan Cheng6140a8b2009-02-22 08:05:12 +00001591 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00001592
Dale Johannesendd64c412009-02-04 00:33:20 +00001593 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), ValToCopy, Flag);
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001594 Flag = Chain.getValue(1);
1595 }
Dan Gohman61a92132008-04-21 23:59:07 +00001596
1597 // The x86-64 ABI for returning structs by value requires that we copy
1598 // the sret argument into %rax for the return. We saved the argument into
1599 // a virtual register in the entry block, so now we copy the value out
1600 // and into %rax.
1601 if (Subtarget->is64Bit() &&
1602 DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
1603 MachineFunction &MF = DAG.getMachineFunction();
1604 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1605 unsigned Reg = FuncInfo->getSRetReturnReg();
Michael J. Spencerec38de22010-10-10 22:04:20 +00001606 assert(Reg &&
Zhongxing Xuc2798a12010-05-26 08:10:02 +00001607 "SRetReturnReg should have been set in LowerFormalArguments().");
Dale Johannesendd64c412009-02-04 00:33:20 +00001608 SDValue Val = DAG.getCopyFromReg(Chain, dl, Reg, getPointerTy());
Dan Gohman61a92132008-04-21 23:59:07 +00001609
Dale Johannesendd64c412009-02-04 00:33:20 +00001610 Chain = DAG.getCopyToReg(Chain, dl, X86::RAX, Val, Flag);
Dan Gohman61a92132008-04-21 23:59:07 +00001611 Flag = Chain.getValue(1);
Dan Gohman00326812009-10-12 16:36:12 +00001612
1613 // RAX now acts like a return value.
Evan Chengdcea1632010-02-04 02:40:39 +00001614 MRI.addLiveOut(X86::RAX);
Dan Gohman61a92132008-04-21 23:59:07 +00001615 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001616
Chris Lattner447ff682008-03-11 03:23:40 +00001617 RetOps[0] = Chain; // Update chain.
1618
1619 // Add the flag if we have it.
Gabor Greifba36cb52008-08-28 21:40:38 +00001620 if (Flag.getNode())
Chris Lattner447ff682008-03-11 03:23:40 +00001621 RetOps.push_back(Flag);
Scott Michelfdc40a02009-02-17 22:15:04 +00001622
1623 return DAG.getNode(X86ISD::RET_FLAG, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001624 MVT::Other, &RetOps[0], RetOps.size());
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001625}
1626
Evan Chengbf010eb2012-04-10 01:51:00 +00001627bool X86TargetLowering::isUsedByReturnOnly(SDNode *N, SDValue &Chain) const {
Evan Cheng3d2125c2010-11-30 23:55:39 +00001628 if (N->getNumValues() != 1)
1629 return false;
1630 if (!N->hasNUsesOfValue(1, 0))
1631 return false;
1632
Evan Chengbf010eb2012-04-10 01:51:00 +00001633 SDValue TCChain = Chain;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001634 SDNode *Copy = *N->use_begin();
Chad Rosierc8d7eea2012-03-05 19:27:12 +00001635 if (Copy->getOpcode() == ISD::CopyToReg) {
1636 // If the copy has a glue operand, we conservatively assume it isn't safe to
1637 // perform a tail call.
1638 if (Copy->getOperand(Copy->getNumOperands()-1).getValueType() == MVT::Glue)
1639 return false;
Evan Chengbf010eb2012-04-10 01:51:00 +00001640 TCChain = Copy->getOperand(0);
Chad Rosierc8d7eea2012-03-05 19:27:12 +00001641 } else if (Copy->getOpcode() != ISD::FP_EXTEND)
Chad Rosier74bab7f2012-03-02 02:50:46 +00001642 return false;
1643
Evan Cheng1bf891a2010-12-01 22:59:46 +00001644 bool HasRet = false;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001645 for (SDNode::use_iterator UI = Copy->use_begin(), UE = Copy->use_end();
Evan Cheng1bf891a2010-12-01 22:59:46 +00001646 UI != UE; ++UI) {
Evan Cheng3d2125c2010-11-30 23:55:39 +00001647 if (UI->getOpcode() != X86ISD::RET_FLAG)
1648 return false;
Evan Cheng1bf891a2010-12-01 22:59:46 +00001649 HasRet = true;
1650 }
Evan Cheng3d2125c2010-11-30 23:55:39 +00001651
Evan Chengbf010eb2012-04-10 01:51:00 +00001652 if (!HasRet)
1653 return false;
1654
1655 Chain = TCChain;
1656 return true;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001657}
1658
Cameron Zwarich7bbf0ee2011-03-17 14:53:37 +00001659EVT
1660X86TargetLowering::getTypeForExtArgOrReturn(LLVMContext &Context, EVT VT,
Cameron Zwarich44579682011-03-17 14:21:56 +00001661 ISD::NodeType ExtendKind) const {
Cameron Zwarich7bbf0ee2011-03-17 14:53:37 +00001662 MVT ReturnMVT;
Cameron Zwarichebe81732011-03-16 22:20:18 +00001663 // TODO: Is this also valid on 32-bit?
1664 if (Subtarget->is64Bit() && VT == MVT::i1 && ExtendKind == ISD::ZERO_EXTEND)
Cameron Zwarich7bbf0ee2011-03-17 14:53:37 +00001665 ReturnMVT = MVT::i8;
1666 else
1667 ReturnMVT = MVT::i32;
1668
1669 EVT MinVT = getRegisterType(Context, ReturnMVT);
1670 return VT.bitsLT(MinVT) ? MinVT : VT;
Cameron Zwarichebe81732011-03-16 22:20:18 +00001671}
1672
Dan Gohman98ca4f22009-08-05 01:29:28 +00001673/// LowerCallResult - Lower the result values of a call into the
1674/// appropriate copies out of appropriate physical registers.
1675///
1676SDValue
1677X86TargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001678 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001679 const SmallVectorImpl<ISD::InputArg> &Ins,
1680 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001681 SmallVectorImpl<SDValue> &InVals) const {
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001682
Chris Lattnere32bbf62007-02-28 07:09:55 +00001683 // Assign locations to each value returned by this call.
Chris Lattner9774c912007-02-27 05:28:59 +00001684 SmallVector<CCValAssign, 16> RVLocs;
Torok Edwin3f142c32009-02-01 18:15:56 +00001685 bool Is64Bit = Subtarget->is64Bit();
Eric Christopher471e4222011-06-08 23:55:35 +00001686 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
Craig Topper0fbf3642012-04-23 03:28:34 +00001687 getTargetMachine(), RVLocs, *DAG.getContext());
Dan Gohman98ca4f22009-08-05 01:29:28 +00001688 CCInfo.AnalyzeCallResult(Ins, RetCC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001689
Chris Lattner3085e152007-02-25 08:59:22 +00001690 // Copy all of the result registers out of their specified physreg.
Chris Lattner8e6da152008-03-10 21:08:41 +00001691 for (unsigned i = 0; i != RVLocs.size(); ++i) {
Dan Gohman37eed792009-02-04 17:28:58 +00001692 CCValAssign &VA = RVLocs[i];
Owen Andersone50ed302009-08-10 22:56:29 +00001693 EVT CopyVT = VA.getValVT();
Scott Michelfdc40a02009-02-17 22:15:04 +00001694
Torok Edwin3f142c32009-02-01 18:15:56 +00001695 // If this is x86-64, and we disabled SSE, we can't return FP values
Owen Anderson825b72b2009-08-11 20:47:22 +00001696 if ((CopyVT == MVT::f32 || CopyVT == MVT::f64) &&
Craig Topper1accb7e2012-01-10 06:54:16 +00001697 ((Is64Bit || Ins[i].Flags.isInReg()) && !Subtarget->hasSSE1())) {
Chris Lattner75361b62010-04-07 22:58:41 +00001698 report_fatal_error("SSE register return with SSE disabled");
Torok Edwin3f142c32009-02-01 18:15:56 +00001699 }
1700
Evan Cheng79fb3b42009-02-20 20:43:02 +00001701 SDValue Val;
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001702
1703 // If this is a call to a function that returns an fp value on the floating
Sylvestre Ledruc8e41c52012-07-23 08:51:15 +00001704 // point stack, we must guarantee the value is popped from the stack, so
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001705 // a CopyFromReg is not good enough - the copy instruction may be eliminated
Jakob Stoklund Olesen9bbe4d62011-06-28 18:32:28 +00001706 // if the return value is not used. We use the FpPOP_RETVAL instruction
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001707 // instead.
1708 if (VA.getLocReg() == X86::ST0 || VA.getLocReg() == X86::ST1) {
1709 // If we prefer to use the value in xmm registers, copy it out as f80 and
1710 // use a truncate to move it from fp stack reg to xmm reg.
1711 if (isScalarFPTypeInSSEReg(VA.getValVT())) CopyVT = MVT::f80;
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001712 SDValue Ops[] = { Chain, InFlag };
Jakob Stoklund Olesen9bbe4d62011-06-28 18:32:28 +00001713 Chain = SDValue(DAG.getMachineNode(X86::FpPOP_RETVAL, dl, CopyVT,
1714 MVT::Other, MVT::Glue, Ops, 2), 1);
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001715 Val = Chain.getValue(0);
1716
1717 // Round the f80 to the right size, which also moves it to the appropriate
1718 // xmm register.
1719 if (CopyVT != VA.getValVT())
1720 Val = DAG.getNode(ISD::FP_ROUND, dl, VA.getValVT(), Val,
1721 // This truncation won't change the value.
1722 DAG.getIntPtrConstant(1));
Evan Cheng79fb3b42009-02-20 20:43:02 +00001723 } else {
1724 Chain = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(),
1725 CopyVT, InFlag).getValue(1);
1726 Val = Chain.getValue(0);
1727 }
Chris Lattner8e6da152008-03-10 21:08:41 +00001728 InFlag = Chain.getValue(2);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001729 InVals.push_back(Val);
Chris Lattner3085e152007-02-25 08:59:22 +00001730 }
Duncan Sands4bdcb612008-07-02 17:40:58 +00001731
Dan Gohman98ca4f22009-08-05 01:29:28 +00001732 return Chain;
Chris Lattner2b02a442007-02-25 08:29:00 +00001733}
1734
1735
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001736//===----------------------------------------------------------------------===//
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001737// C & StdCall & Fast Calling Convention implementation
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001738//===----------------------------------------------------------------------===//
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001739// StdCall calling convention seems to be standard for many Windows' API
1740// routines and around. It differs from C calling convention just a little:
1741// callee should clean up the stack, not caller. Symbols should be also
1742// decorated in some fancy way :) It doesn't support any vector arguments.
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001743// For info on fast calling convention see Fast Calling Convention (tail call)
1744// implementation LowerX86_32FastCCCallTo.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001745
Dan Gohman98ca4f22009-08-05 01:29:28 +00001746/// CallIsStructReturn - Determines whether a call uses struct return
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001747/// semantics.
Rafael Espindola1cee7102012-07-25 13:41:10 +00001748enum StructReturnType {
1749 NotStructReturn,
1750 RegStructReturn,
1751 StackStructReturn
1752};
1753static StructReturnType
1754callIsStructReturn(const SmallVectorImpl<ISD::OutputArg> &Outs) {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001755 if (Outs.empty())
Rafael Espindola1cee7102012-07-25 13:41:10 +00001756 return NotStructReturn;
Duncan Sands276dcbd2008-03-21 09:14:45 +00001757
Rafael Espindola1cee7102012-07-25 13:41:10 +00001758 const ISD::ArgFlagsTy &Flags = Outs[0].Flags;
1759 if (!Flags.isSRet())
1760 return NotStructReturn;
1761 if (Flags.isInReg())
1762 return RegStructReturn;
1763 return StackStructReturn;
Gordon Henriksen86737662008-01-05 16:56:59 +00001764}
1765
Dan Gohman7e77b0f2009-08-01 19:14:37 +00001766/// ArgsAreStructReturn - Determines whether a function uses struct
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001767/// return semantics.
Rafael Espindola1cee7102012-07-25 13:41:10 +00001768static StructReturnType
1769argsAreStructReturn(const SmallVectorImpl<ISD::InputArg> &Ins) {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001770 if (Ins.empty())
Rafael Espindola1cee7102012-07-25 13:41:10 +00001771 return NotStructReturn;
Duncan Sands276dcbd2008-03-21 09:14:45 +00001772
Rafael Espindola1cee7102012-07-25 13:41:10 +00001773 const ISD::ArgFlagsTy &Flags = Ins[0].Flags;
1774 if (!Flags.isSRet())
1775 return NotStructReturn;
1776 if (Flags.isInReg())
1777 return RegStructReturn;
1778 return StackStructReturn;
Gordon Henriksen86737662008-01-05 16:56:59 +00001779}
1780
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001781/// CreateCopyOfByValArgument - Make a copy of an aggregate at address specified
1782/// by "Src" to address "Dst" with size and alignment information specified by
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001783/// the specific parameter attribute. The copy will be passed as a byval
1784/// function parameter.
Scott Michelfdc40a02009-02-17 22:15:04 +00001785static SDValue
Dan Gohman475871a2008-07-27 21:46:04 +00001786CreateCopyOfByValArgument(SDValue Src, SDValue Dst, SDValue Chain,
Dale Johannesendd64c412009-02-04 00:33:20 +00001787 ISD::ArgFlagsTy Flags, SelectionDAG &DAG,
1788 DebugLoc dl) {
Chris Lattnere72f2022010-09-21 05:40:29 +00001789 SDValue SizeNode = DAG.getConstant(Flags.getByValSize(), MVT::i32);
Michael J. Spencerec38de22010-10-10 22:04:20 +00001790
Dale Johannesendd64c412009-02-04 00:33:20 +00001791 return DAG.getMemcpy(Chain, dl, Dst, Src, SizeNode, Flags.getByValAlign(),
Stuart Hastings03d58262011-03-10 00:25:53 +00001792 /*isVolatile*/false, /*AlwaysInline=*/true,
Chris Lattnerfc448ff2010-09-21 18:51:21 +00001793 MachinePointerInfo(), MachinePointerInfo());
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001794}
1795
Chris Lattner29689432010-03-11 00:22:57 +00001796/// IsTailCallConvention - Return true if the calling convention is one that
1797/// supports tail call optimization.
1798static bool IsTailCallConvention(CallingConv::ID CC) {
1799 return (CC == CallingConv::Fast || CC == CallingConv::GHC);
1800}
1801
Evan Cheng485fafc2011-03-21 01:19:09 +00001802bool X86TargetLowering::mayBeEmittedAsTailCall(CallInst *CI) const {
Nick Lewycky22de16d2012-01-19 00:34:10 +00001803 if (!CI->isTailCall() || getTargetMachine().Options.DisableTailCalls)
Evan Cheng485fafc2011-03-21 01:19:09 +00001804 return false;
1805
1806 CallSite CS(CI);
1807 CallingConv::ID CalleeCC = CS.getCallingConv();
1808 if (!IsTailCallConvention(CalleeCC) && CalleeCC != CallingConv::C)
1809 return false;
1810
1811 return true;
1812}
1813
Evan Cheng0c439eb2010-01-27 00:07:07 +00001814/// FuncIsMadeTailCallSafe - Return true if the function is being made into
1815/// a tailcall target by changing its ABI.
Nick Lewycky8a8d4792011-12-02 22:16:29 +00001816static bool FuncIsMadeTailCallSafe(CallingConv::ID CC,
1817 bool GuaranteedTailCallOpt) {
Chris Lattner29689432010-03-11 00:22:57 +00001818 return GuaranteedTailCallOpt && IsTailCallConvention(CC);
Evan Cheng0c439eb2010-01-27 00:07:07 +00001819}
1820
Dan Gohman98ca4f22009-08-05 01:29:28 +00001821SDValue
1822X86TargetLowering::LowerMemArgument(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001823 CallingConv::ID CallConv,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001824 const SmallVectorImpl<ISD::InputArg> &Ins,
1825 DebugLoc dl, SelectionDAG &DAG,
1826 const CCValAssign &VA,
1827 MachineFrameInfo *MFI,
Dan Gohmand858e902010-04-17 15:26:15 +00001828 unsigned i) const {
Rafael Espindola7effac52007-09-14 15:48:13 +00001829 // Create the nodes corresponding to a load from this parameter slot.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001830 ISD::ArgFlagsTy Flags = Ins[i].Flags;
Nick Lewycky8a8d4792011-12-02 22:16:29 +00001831 bool AlwaysUseMutable = FuncIsMadeTailCallSafe(CallConv,
1832 getTargetMachine().Options.GuaranteedTailCallOpt);
Duncan Sands276dcbd2008-03-21 09:14:45 +00001833 bool isImmutable = !AlwaysUseMutable && !Flags.isByVal();
Anton Korobeynikov22472762009-08-14 18:19:10 +00001834 EVT ValVT;
1835
1836 // If value is passed by pointer we have address passed instead of the value
1837 // itself.
1838 if (VA.getLocInfo() == CCValAssign::Indirect)
1839 ValVT = VA.getLocVT();
1840 else
1841 ValVT = VA.getValVT();
Evan Chenge70bb592008-01-10 02:24:25 +00001842
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001843 // FIXME: For now, all byval parameter objects are marked mutable. This can be
Scott Michelfdc40a02009-02-17 22:15:04 +00001844 // changed with more analysis.
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001845 // In case of tail call optimization mark all arguments mutable. Since they
1846 // could be overwritten by lowering of arguments in case of a tail call.
Evan Cheng90567c32010-02-02 23:58:13 +00001847 if (Flags.isByVal()) {
Evan Chengee2e0e32011-03-30 23:44:13 +00001848 unsigned Bytes = Flags.getByValSize();
1849 if (Bytes == 0) Bytes = 1; // Don't create zero-sized stack objects.
1850 int FI = MFI->CreateFixedObject(Bytes, VA.getLocMemOffset(), isImmutable);
Evan Cheng90567c32010-02-02 23:58:13 +00001851 return DAG.getFrameIndex(FI, getPointerTy());
1852 } else {
1853 int FI = MFI->CreateFixedObject(ValVT.getSizeInBits()/8,
Evan Chenged2ae132010-07-03 00:40:23 +00001854 VA.getLocMemOffset(), isImmutable);
Evan Cheng90567c32010-02-02 23:58:13 +00001855 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
1856 return DAG.getLoad(ValVT, dl, Chain, FIN,
Chris Lattnere8639032010-09-21 06:22:23 +00001857 MachinePointerInfo::getFixedStack(FI),
Pete Cooperd752e0f2011-11-08 18:42:53 +00001858 false, false, false, 0);
Evan Cheng90567c32010-02-02 23:58:13 +00001859 }
Rafael Espindola7effac52007-09-14 15:48:13 +00001860}
1861
Dan Gohman475871a2008-07-27 21:46:04 +00001862SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00001863X86TargetLowering::LowerFormalArguments(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001864 CallingConv::ID CallConv,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001865 bool isVarArg,
1866 const SmallVectorImpl<ISD::InputArg> &Ins,
1867 DebugLoc dl,
1868 SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001869 SmallVectorImpl<SDValue> &InVals)
1870 const {
Evan Cheng1bc78042006-04-26 01:20:17 +00001871 MachineFunction &MF = DAG.getMachineFunction();
Gordon Henriksen86737662008-01-05 16:56:59 +00001872 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
Scott Michelfdc40a02009-02-17 22:15:04 +00001873
Gordon Henriksen86737662008-01-05 16:56:59 +00001874 const Function* Fn = MF.getFunction();
1875 if (Fn->hasExternalLinkage() &&
1876 Subtarget->isTargetCygMing() &&
1877 Fn->getName() == "main")
1878 FuncInfo->setForceFramePointer(true);
1879
Evan Cheng1bc78042006-04-26 01:20:17 +00001880 MachineFrameInfo *MFI = MF.getFrameInfo();
Gordon Henriksen86737662008-01-05 16:56:59 +00001881 bool Is64Bit = Subtarget->is64Bit();
Eli Friedman9a2478a2012-01-20 00:05:46 +00001882 bool IsWindows = Subtarget->isTargetWindows();
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001883 bool IsWin64 = Subtarget->isTargetWin64();
Gordon Henriksenae636f82008-01-03 16:47:34 +00001884
Chris Lattner29689432010-03-11 00:22:57 +00001885 assert(!(isVarArg && IsTailCallConvention(CallConv)) &&
1886 "Var args not supported with calling convention fastcc or ghc");
Gordon Henriksenae636f82008-01-03 16:47:34 +00001887
Chris Lattner638402b2007-02-28 07:00:42 +00001888 // Assign locations to all of the incoming arguments.
Chris Lattnerf39f7712007-02-28 05:46:49 +00001889 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00001890 CCState CCInfo(CallConv, isVarArg, MF, getTargetMachine(),
Dan Gohman98ca4f22009-08-05 01:29:28 +00001891 ArgLocs, *DAG.getContext());
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00001892
1893 // Allocate shadow area for Win64
1894 if (IsWin64) {
1895 CCInfo.AllocateStack(32, 8);
1896 }
1897
Duncan Sands45907662010-10-31 13:21:44 +00001898 CCInfo.AnalyzeFormalArguments(Ins, CC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001899
Chris Lattnerf39f7712007-02-28 05:46:49 +00001900 unsigned LastVal = ~0U;
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001901 SDValue ArgValue;
Chris Lattnerf39f7712007-02-28 05:46:49 +00001902 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1903 CCValAssign &VA = ArgLocs[i];
1904 // TODO: If an arg is passed in two places (e.g. reg and stack), skip later
1905 // places.
1906 assert(VA.getValNo() != LastVal &&
1907 "Don't support value assigned to multiple locs yet");
Duncan Sands17001ce2011-10-18 12:44:00 +00001908 (void)LastVal;
Chris Lattnerf39f7712007-02-28 05:46:49 +00001909 LastVal = VA.getValNo();
Scott Michelfdc40a02009-02-17 22:15:04 +00001910
Chris Lattnerf39f7712007-02-28 05:46:49 +00001911 if (VA.isRegLoc()) {
Owen Andersone50ed302009-08-10 22:56:29 +00001912 EVT RegVT = VA.getLocVT();
Craig Topper44d23822012-02-22 05:59:10 +00001913 const TargetRegisterClass *RC;
Owen Anderson825b72b2009-08-11 20:47:22 +00001914 if (RegVT == MVT::i32)
Craig Topperc9099502012-04-20 06:31:50 +00001915 RC = &X86::GR32RegClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001916 else if (Is64Bit && RegVT == MVT::i64)
Craig Topperc9099502012-04-20 06:31:50 +00001917 RC = &X86::GR64RegClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001918 else if (RegVT == MVT::f32)
Craig Topperc9099502012-04-20 06:31:50 +00001919 RC = &X86::FR32RegClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001920 else if (RegVT == MVT::f64)
Craig Topperc9099502012-04-20 06:31:50 +00001921 RC = &X86::FR64RegClass;
Craig Topper7a9a28b2012-08-12 02:23:29 +00001922 else if (RegVT.is256BitVector())
Craig Topperc9099502012-04-20 06:31:50 +00001923 RC = &X86::VR256RegClass;
Craig Topper7a9a28b2012-08-12 02:23:29 +00001924 else if (RegVT.is128BitVector())
Craig Topperc9099502012-04-20 06:31:50 +00001925 RC = &X86::VR128RegClass;
Dale Johannesen0488fb62010-09-30 23:57:10 +00001926 else if (RegVT == MVT::x86mmx)
Craig Topperc9099502012-04-20 06:31:50 +00001927 RC = &X86::VR64RegClass;
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001928 else
Torok Edwinc23197a2009-07-14 16:55:14 +00001929 llvm_unreachable("Unknown argument type!");
Gordon Henriksenae636f82008-01-03 16:47:34 +00001930
Devang Patel68e6bee2011-02-21 23:21:26 +00001931 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001932 ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT);
Scott Michelfdc40a02009-02-17 22:15:04 +00001933
Chris Lattnerf39f7712007-02-28 05:46:49 +00001934 // If this is an 8 or 16-bit value, it is really passed promoted to 32
1935 // bits. Insert an assert[sz]ext to capture this, then truncate to the
1936 // right size.
1937 if (VA.getLocInfo() == CCValAssign::SExt)
Dale Johannesenace16102009-02-03 19:33:06 +00001938 ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue,
Chris Lattnerf39f7712007-02-28 05:46:49 +00001939 DAG.getValueType(VA.getValVT()));
1940 else if (VA.getLocInfo() == CCValAssign::ZExt)
Dale Johannesenace16102009-02-03 19:33:06 +00001941 ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue,
Chris Lattnerf39f7712007-02-28 05:46:49 +00001942 DAG.getValueType(VA.getValVT()));
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001943 else if (VA.getLocInfo() == CCValAssign::BCvt)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001944 ArgValue = DAG.getNode(ISD::BITCAST, dl, VA.getValVT(), ArgValue);
Scott Michelfdc40a02009-02-17 22:15:04 +00001945
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001946 if (VA.isExtInLoc()) {
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001947 // Handle MMX values passed in XMM regs.
1948 if (RegVT.isVector()) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00001949 ArgValue = DAG.getNode(X86ISD::MOVDQ2Q, dl, VA.getValVT(),
1950 ArgValue);
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001951 } else
1952 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
Evan Cheng44c0fd12008-04-25 20:13:28 +00001953 }
Chris Lattnerf39f7712007-02-28 05:46:49 +00001954 } else {
1955 assert(VA.isMemLoc());
Dan Gohman98ca4f22009-08-05 01:29:28 +00001956 ArgValue = LowerMemArgument(Chain, CallConv, Ins, dl, DAG, VA, MFI, i);
Evan Cheng1bc78042006-04-26 01:20:17 +00001957 }
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001958
1959 // If value is passed via pointer - do a load.
1960 if (VA.getLocInfo() == CCValAssign::Indirect)
Chris Lattner51abfe42010-09-21 06:02:19 +00001961 ArgValue = DAG.getLoad(VA.getValVT(), dl, Chain, ArgValue,
Pete Cooperd752e0f2011-11-08 18:42:53 +00001962 MachinePointerInfo(), false, false, false, 0);
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001963
Dan Gohman98ca4f22009-08-05 01:29:28 +00001964 InVals.push_back(ArgValue);
Evan Cheng1bc78042006-04-26 01:20:17 +00001965 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001966
Dan Gohman61a92132008-04-21 23:59:07 +00001967 // The x86-64 ABI for returning structs by value requires that we copy
1968 // the sret argument into %rax for the return. Save the argument into
1969 // a virtual register so that we can access it from the return points.
Dan Gohman7e77b0f2009-08-01 19:14:37 +00001970 if (Is64Bit && MF.getFunction()->hasStructRetAttr()) {
Dan Gohman61a92132008-04-21 23:59:07 +00001971 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1972 unsigned Reg = FuncInfo->getSRetReturnReg();
1973 if (!Reg) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001974 Reg = MF.getRegInfo().createVirtualRegister(getRegClassFor(MVT::i64));
Dan Gohman61a92132008-04-21 23:59:07 +00001975 FuncInfo->setSRetReturnReg(Reg);
1976 }
Dan Gohman98ca4f22009-08-05 01:29:28 +00001977 SDValue Copy = DAG.getCopyToReg(DAG.getEntryNode(), dl, Reg, InVals[0]);
Owen Anderson825b72b2009-08-11 20:47:22 +00001978 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Copy, Chain);
Dan Gohman61a92132008-04-21 23:59:07 +00001979 }
1980
Chris Lattnerf39f7712007-02-28 05:46:49 +00001981 unsigned StackSize = CCInfo.getNextStackOffset();
Evan Cheng0c439eb2010-01-27 00:07:07 +00001982 // Align stack specially for tail calls.
Nick Lewycky8a8d4792011-12-02 22:16:29 +00001983 if (FuncIsMadeTailCallSafe(CallConv,
1984 MF.getTarget().Options.GuaranteedTailCallOpt))
Gordon Henriksenae636f82008-01-03 16:47:34 +00001985 StackSize = GetAlignedArgumentStackSize(StackSize, DAG);
Evan Cheng25caf632006-05-23 21:06:34 +00001986
Evan Cheng1bc78042006-04-26 01:20:17 +00001987 // If the function takes variable number of arguments, make a frame index for
1988 // the start of the first vararg value... for expansion of llvm.va_start.
Gordon Henriksenae636f82008-01-03 16:47:34 +00001989 if (isVarArg) {
NAKAMURA Takumi3ca99432011-03-09 11:33:15 +00001990 if (Is64Bit || (CallConv != CallingConv::X86_FastCall &&
1991 CallConv != CallingConv::X86_ThisCall)) {
Jakob Stoklund Olesenb2eeed72010-07-29 17:42:27 +00001992 FuncInfo->setVarArgsFrameIndex(MFI->CreateFixedObject(1, StackSize,true));
Gordon Henriksen86737662008-01-05 16:56:59 +00001993 }
1994 if (Is64Bit) {
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001995 unsigned TotalNumIntRegs = 0, TotalNumXMMRegs = 0;
1996
1997 // FIXME: We should really autogenerate these arrays
Craig Topperc5eaae42012-03-11 07:57:25 +00001998 static const uint16_t GPR64ArgRegsWin64[] = {
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001999 X86::RCX, X86::RDX, X86::R8, X86::R9
Gordon Henriksen86737662008-01-05 16:56:59 +00002000 };
Craig Topperc5eaae42012-03-11 07:57:25 +00002001 static const uint16_t GPR64ArgRegs64Bit[] = {
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002002 X86::RDI, X86::RSI, X86::RDX, X86::RCX, X86::R8, X86::R9
2003 };
Craig Topperc5eaae42012-03-11 07:57:25 +00002004 static const uint16_t XMMArgRegs64Bit[] = {
Gordon Henriksen86737662008-01-05 16:56:59 +00002005 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
2006 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
2007 };
Craig Topperc5eaae42012-03-11 07:57:25 +00002008 const uint16_t *GPR64ArgRegs;
Anton Korobeynikove7beda12010-10-03 22:52:07 +00002009 unsigned NumXMMRegs = 0;
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002010
2011 if (IsWin64) {
Anton Korobeynikove7beda12010-10-03 22:52:07 +00002012 // The XMM registers which might contain var arg parameters are shadowed
2013 // in their paired GPR. So we only need to save the GPR to their home
2014 // slots.
2015 TotalNumIntRegs = 4;
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002016 GPR64ArgRegs = GPR64ArgRegsWin64;
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002017 } else {
2018 TotalNumIntRegs = 6; TotalNumXMMRegs = 8;
2019 GPR64ArgRegs = GPR64ArgRegs64Bit;
Anton Korobeynikove7beda12010-10-03 22:52:07 +00002020
Chad Rosier30450e82011-12-22 22:35:21 +00002021 NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs64Bit,
2022 TotalNumXMMRegs);
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002023 }
2024 unsigned NumIntRegs = CCInfo.getFirstUnallocated(GPR64ArgRegs,
2025 TotalNumIntRegs);
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002026
Bill Wendling67658342012-10-09 07:45:08 +00002027 bool NoImplicitFloatOps = Fn->getFnAttributes().
2028 hasAttribute(Attributes::NoImplicitFloat);
Craig Topper1accb7e2012-01-10 06:54:16 +00002029 assert(!(NumXMMRegs && !Subtarget->hasSSE1()) &&
Torok Edwin3f142c32009-02-01 18:15:56 +00002030 "SSE register cannot be used when SSE is disabled!");
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002031 assert(!(NumXMMRegs && MF.getTarget().Options.UseSoftFloat &&
2032 NoImplicitFloatOps) &&
Evan Chengc7ce29b2009-02-13 22:36:38 +00002033 "SSE register cannot be used when SSE is disabled!");
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002034 if (MF.getTarget().Options.UseSoftFloat || NoImplicitFloatOps ||
Craig Topper1accb7e2012-01-10 06:54:16 +00002035 !Subtarget->hasSSE1())
Torok Edwin3f142c32009-02-01 18:15:56 +00002036 // Kernel mode asks for SSE to be disabled, so don't push them
2037 // on the stack.
2038 TotalNumXMMRegs = 0;
Bill Wendlingf9abd7e2009-03-11 22:30:01 +00002039
Anton Korobeynikove7beda12010-10-03 22:52:07 +00002040 if (IsWin64) {
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00002041 const TargetFrameLowering &TFI = *getTargetMachine().getFrameLowering();
Cameron Esfahaniec37b002010-10-08 19:24:18 +00002042 // Get to the caller-allocated home save location. Add 8 to account
2043 // for the return address.
2044 int HomeOffset = TFI.getOffsetOfLocalArea() + 8;
Anton Korobeynikove7beda12010-10-03 22:52:07 +00002045 FuncInfo->setRegSaveFrameIndex(
Cameron Esfahaniec37b002010-10-08 19:24:18 +00002046 MFI->CreateFixedObject(1, NumIntRegs * 8 + HomeOffset, false));
NAKAMURA Takumi3ca99432011-03-09 11:33:15 +00002047 // Fixup to set vararg frame on shadow area (4 x i64).
2048 if (NumIntRegs < 4)
2049 FuncInfo->setVarArgsFrameIndex(FuncInfo->getRegSaveFrameIndex());
Anton Korobeynikove7beda12010-10-03 22:52:07 +00002050 } else {
2051 // For X86-64, if there are vararg parameters that are passed via
Chad Rosier30450e82011-12-22 22:35:21 +00002052 // registers, then we must store them to their spots on the stack so
2053 // they may be loaded by deferencing the result of va_next.
Anton Korobeynikove7beda12010-10-03 22:52:07 +00002054 FuncInfo->setVarArgsGPOffset(NumIntRegs * 8);
2055 FuncInfo->setVarArgsFPOffset(TotalNumIntRegs * 8 + NumXMMRegs * 16);
2056 FuncInfo->setRegSaveFrameIndex(
2057 MFI->CreateStackObject(TotalNumIntRegs * 8 + TotalNumXMMRegs * 16, 16,
Dan Gohman1e93df62010-04-17 14:41:14 +00002058 false));
Anton Korobeynikove7beda12010-10-03 22:52:07 +00002059 }
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002060
Gordon Henriksen86737662008-01-05 16:56:59 +00002061 // Store the integer parameter registers.
Dan Gohman475871a2008-07-27 21:46:04 +00002062 SmallVector<SDValue, 8> MemOps;
Dan Gohman1e93df62010-04-17 14:41:14 +00002063 SDValue RSFIN = DAG.getFrameIndex(FuncInfo->getRegSaveFrameIndex(),
2064 getPointerTy());
2065 unsigned Offset = FuncInfo->getVarArgsGPOffset();
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002066 for (; NumIntRegs != TotalNumIntRegs; ++NumIntRegs) {
Dan Gohmand6708ea2009-08-15 01:38:56 +00002067 SDValue FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), RSFIN,
2068 DAG.getIntPtrConstant(Offset));
Bob Wilson998e1252009-04-20 18:36:57 +00002069 unsigned VReg = MF.addLiveIn(GPR64ArgRegs[NumIntRegs],
Craig Topperc9099502012-04-20 06:31:50 +00002070 &X86::GR64RegClass);
Owen Anderson825b72b2009-08-11 20:47:22 +00002071 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i64);
Dan Gohman475871a2008-07-27 21:46:04 +00002072 SDValue Store =
Dale Johannesenace16102009-02-03 19:33:06 +00002073 DAG.getStore(Val.getValue(1), dl, Val, FIN,
Chris Lattnere8639032010-09-21 06:22:23 +00002074 MachinePointerInfo::getFixedStack(
2075 FuncInfo->getRegSaveFrameIndex(), Offset),
2076 false, false, 0);
Gordon Henriksen86737662008-01-05 16:56:59 +00002077 MemOps.push_back(Store);
Dan Gohmand6708ea2009-08-15 01:38:56 +00002078 Offset += 8;
Gordon Henriksen86737662008-01-05 16:56:59 +00002079 }
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002080
Dan Gohmanface41a2009-08-16 21:24:25 +00002081 if (TotalNumXMMRegs != 0 && NumXMMRegs != TotalNumXMMRegs) {
2082 // Now store the XMM (fp + vector) parameter registers.
2083 SmallVector<SDValue, 11> SaveXMMOps;
2084 SaveXMMOps.push_back(Chain);
Dan Gohmand6708ea2009-08-15 01:38:56 +00002085
Craig Topperc9099502012-04-20 06:31:50 +00002086 unsigned AL = MF.addLiveIn(X86::AL, &X86::GR8RegClass);
Dan Gohmanface41a2009-08-16 21:24:25 +00002087 SDValue ALVal = DAG.getCopyFromReg(DAG.getEntryNode(), dl, AL, MVT::i8);
2088 SaveXMMOps.push_back(ALVal);
Dan Gohmand6708ea2009-08-15 01:38:56 +00002089
Dan Gohman1e93df62010-04-17 14:41:14 +00002090 SaveXMMOps.push_back(DAG.getIntPtrConstant(
2091 FuncInfo->getRegSaveFrameIndex()));
2092 SaveXMMOps.push_back(DAG.getIntPtrConstant(
2093 FuncInfo->getVarArgsFPOffset()));
Dan Gohmand6708ea2009-08-15 01:38:56 +00002094
Dan Gohmanface41a2009-08-16 21:24:25 +00002095 for (; NumXMMRegs != TotalNumXMMRegs; ++NumXMMRegs) {
Anton Korobeynikove7beda12010-10-03 22:52:07 +00002096 unsigned VReg = MF.addLiveIn(XMMArgRegs64Bit[NumXMMRegs],
Craig Topperc9099502012-04-20 06:31:50 +00002097 &X86::VR128RegClass);
Dan Gohmanface41a2009-08-16 21:24:25 +00002098 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::v4f32);
2099 SaveXMMOps.push_back(Val);
2100 }
2101 MemOps.push_back(DAG.getNode(X86ISD::VASTART_SAVE_XMM_REGS, dl,
2102 MVT::Other,
2103 &SaveXMMOps[0], SaveXMMOps.size()));
Gordon Henriksen86737662008-01-05 16:56:59 +00002104 }
Dan Gohmanface41a2009-08-16 21:24:25 +00002105
2106 if (!MemOps.empty())
2107 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
2108 &MemOps[0], MemOps.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00002109 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00002110 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002111
Gordon Henriksen86737662008-01-05 16:56:59 +00002112 // Some CCs need callee pop.
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002113 if (X86::isCalleePop(CallConv, Is64Bit, isVarArg,
2114 MF.getTarget().Options.GuaranteedTailCallOpt)) {
Dan Gohman1e93df62010-04-17 14:41:14 +00002115 FuncInfo->setBytesToPopOnReturn(StackSize); // Callee pops everything.
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00002116 } else {
Dan Gohman1e93df62010-04-17 14:41:14 +00002117 FuncInfo->setBytesToPopOnReturn(0); // Callee pops nothing.
Chris Lattnerf39f7712007-02-28 05:46:49 +00002118 // If this is an sret function, the return should pop the hidden pointer.
Eli Friedman9a2478a2012-01-20 00:05:46 +00002119 if (!Is64Bit && !IsTailCallConvention(CallConv) && !IsWindows &&
Rafael Espindola1cee7102012-07-25 13:41:10 +00002120 argsAreStructReturn(Ins) == StackStructReturn)
Dan Gohman1e93df62010-04-17 14:41:14 +00002121 FuncInfo->setBytesToPopOnReturn(4);
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00002122 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00002123
Gordon Henriksen86737662008-01-05 16:56:59 +00002124 if (!Is64Bit) {
Dan Gohman1e93df62010-04-17 14:41:14 +00002125 // RegSaveFrameIndex is X86-64 only.
2126 FuncInfo->setRegSaveFrameIndex(0xAAAAAAA);
Anton Korobeynikovded05e32010-05-16 09:08:45 +00002127 if (CallConv == CallingConv::X86_FastCall ||
2128 CallConv == CallingConv::X86_ThisCall)
Dan Gohman1e93df62010-04-17 14:41:14 +00002129 // fastcc functions can't have varargs.
2130 FuncInfo->setVarArgsFrameIndex(0xAAAAAAA);
Gordon Henriksen86737662008-01-05 16:56:59 +00002131 }
Evan Cheng25caf632006-05-23 21:06:34 +00002132
Rafael Espindola76927d752011-08-30 19:39:58 +00002133 FuncInfo->setArgumentStackSize(StackSize);
2134
Dan Gohman98ca4f22009-08-05 01:29:28 +00002135 return Chain;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002136}
2137
Dan Gohman475871a2008-07-27 21:46:04 +00002138SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00002139X86TargetLowering::LowerMemOpCallTo(SDValue Chain,
2140 SDValue StackPtr, SDValue Arg,
2141 DebugLoc dl, SelectionDAG &DAG,
Evan Chengdffbd832008-01-10 00:09:10 +00002142 const CCValAssign &VA,
Dan Gohmand858e902010-04-17 15:26:15 +00002143 ISD::ArgFlagsTy Flags) const {
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00002144 unsigned LocMemOffset = VA.getLocMemOffset();
Dan Gohman475871a2008-07-27 21:46:04 +00002145 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset);
Dale Johannesenace16102009-02-03 19:33:06 +00002146 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, PtrOff);
Chris Lattnerfc448ff2010-09-21 18:51:21 +00002147 if (Flags.isByVal())
Dale Johannesendd64c412009-02-04 00:33:20 +00002148 return CreateCopyOfByValArgument(Arg, PtrOff, Chain, Flags, DAG, dl);
Chris Lattnerfc448ff2010-09-21 18:51:21 +00002149
2150 return DAG.getStore(Chain, dl, Arg, PtrOff,
2151 MachinePointerInfo::getStack(LocMemOffset),
David Greene67c9d422010-02-15 16:53:33 +00002152 false, false, 0);
Evan Chengdffbd832008-01-10 00:09:10 +00002153}
2154
Bill Wendling64e87322009-01-16 19:25:27 +00002155/// EmitTailCallLoadRetAddr - Emit a load of return address if tail call
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002156/// optimization is performed and it is required.
Scott Michelfdc40a02009-02-17 22:15:04 +00002157SDValue
2158X86TargetLowering::EmitTailCallLoadRetAddr(SelectionDAG &DAG,
Evan Chengddc419c2010-01-26 19:04:47 +00002159 SDValue &OutRetAddr, SDValue Chain,
2160 bool IsTailCall, bool Is64Bit,
Dan Gohmand858e902010-04-17 15:26:15 +00002161 int FPDiff, DebugLoc dl) const {
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002162 // Adjust the Return address stack slot.
Owen Andersone50ed302009-08-10 22:56:29 +00002163 EVT VT = getPointerTy();
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002164 OutRetAddr = getReturnAddressFrameIndex(DAG);
Bill Wendling64e87322009-01-16 19:25:27 +00002165
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002166 // Load the "old" Return address.
Chris Lattner51abfe42010-09-21 06:02:19 +00002167 OutRetAddr = DAG.getLoad(VT, dl, Chain, OutRetAddr, MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002168 false, false, false, 0);
Gabor Greifba36cb52008-08-28 21:40:38 +00002169 return SDValue(OutRetAddr.getNode(), 1);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002170}
2171
Chris Lattner7a2bdde2011-04-15 05:18:47 +00002172/// EmitTailCallStoreRetAddr - Emit a store of the return address if tail call
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002173/// optimization is performed and it is required (FPDiff!=0).
Scott Michelfdc40a02009-02-17 22:15:04 +00002174static SDValue
2175EmitTailCallStoreRetAddr(SelectionDAG & DAG, MachineFunction &MF,
Dan Gohman475871a2008-07-27 21:46:04 +00002176 SDValue Chain, SDValue RetAddrFrIdx,
Dale Johannesenace16102009-02-03 19:33:06 +00002177 bool Is64Bit, int FPDiff, DebugLoc dl) {
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002178 // Store the return address to the appropriate stack slot.
2179 if (!FPDiff) return Chain;
2180 // Calculate the new stack slot for the return address.
2181 int SlotSize = Is64Bit ? 8 : 4;
Scott Michelfdc40a02009-02-17 22:15:04 +00002182 int NewReturnAddrFI =
Evan Chenged2ae132010-07-03 00:40:23 +00002183 MF.getFrameInfo()->CreateFixedObject(SlotSize, FPDiff-SlotSize, false);
Owen Anderson825b72b2009-08-11 20:47:22 +00002184 EVT VT = Is64Bit ? MVT::i64 : MVT::i32;
Dan Gohman475871a2008-07-27 21:46:04 +00002185 SDValue NewRetAddrFrIdx = DAG.getFrameIndex(NewReturnAddrFI, VT);
Scott Michelfdc40a02009-02-17 22:15:04 +00002186 Chain = DAG.getStore(Chain, dl, RetAddrFrIdx, NewRetAddrFrIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00002187 MachinePointerInfo::getFixedStack(NewReturnAddrFI),
David Greene67c9d422010-02-15 16:53:33 +00002188 false, false, 0);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002189 return Chain;
2190}
2191
Dan Gohman98ca4f22009-08-05 01:29:28 +00002192SDValue
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00002193X86TargetLowering::LowerCall(TargetLowering::CallLoweringInfo &CLI,
Dan Gohmand858e902010-04-17 15:26:15 +00002194 SmallVectorImpl<SDValue> &InVals) const {
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00002195 SelectionDAG &DAG = CLI.DAG;
2196 DebugLoc &dl = CLI.DL;
2197 SmallVector<ISD::OutputArg, 32> &Outs = CLI.Outs;
2198 SmallVector<SDValue, 32> &OutVals = CLI.OutVals;
2199 SmallVector<ISD::InputArg, 32> &Ins = CLI.Ins;
2200 SDValue Chain = CLI.Chain;
2201 SDValue Callee = CLI.Callee;
2202 CallingConv::ID CallConv = CLI.CallConv;
2203 bool &isTailCall = CLI.IsTailCall;
2204 bool isVarArg = CLI.IsVarArg;
2205
Dan Gohman98ca4f22009-08-05 01:29:28 +00002206 MachineFunction &MF = DAG.getMachineFunction();
2207 bool Is64Bit = Subtarget->is64Bit();
NAKAMURA Takumifb840c92011-02-05 15:11:13 +00002208 bool IsWin64 = Subtarget->isTargetWin64();
Eli Friedman9a2478a2012-01-20 00:05:46 +00002209 bool IsWindows = Subtarget->isTargetWindows();
Rafael Espindola1cee7102012-07-25 13:41:10 +00002210 StructReturnType SR = callIsStructReturn(Outs);
Evan Cheng5f941932010-02-05 02:21:12 +00002211 bool IsSibcall = false;
Dan Gohman98ca4f22009-08-05 01:29:28 +00002212
Nick Lewycky22de16d2012-01-19 00:34:10 +00002213 if (MF.getTarget().Options.DisableTailCalls)
2214 isTailCall = false;
2215
Evan Cheng5f941932010-02-05 02:21:12 +00002216 if (isTailCall) {
Evan Cheng0c439eb2010-01-27 00:07:07 +00002217 // Check if it's really possible to do a tail call.
Evan Chenga375d472010-03-15 18:54:48 +00002218 isTailCall = IsEligibleForTailCallOptimization(Callee, CallConv,
Rafael Espindola1cee7102012-07-25 13:41:10 +00002219 isVarArg, SR != NotStructReturn,
Evan Chengb1cacc72012-09-25 05:32:34 +00002220 MF.getFunction()->hasStructRetAttr(), CLI.RetTy,
Rafael Espindola1cee7102012-07-25 13:41:10 +00002221 Outs, OutVals, Ins, DAG);
Evan Chengf22f9b32010-02-06 03:28:46 +00002222
2223 // Sibcalls are automatically detected tailcalls which do not require
2224 // ABI changes.
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002225 if (!MF.getTarget().Options.GuaranteedTailCallOpt && isTailCall)
Evan Cheng5f941932010-02-05 02:21:12 +00002226 IsSibcall = true;
Evan Chengf22f9b32010-02-06 03:28:46 +00002227
2228 if (isTailCall)
2229 ++NumTailCalls;
Evan Cheng5f941932010-02-05 02:21:12 +00002230 }
Evan Cheng0c439eb2010-01-27 00:07:07 +00002231
Chris Lattner29689432010-03-11 00:22:57 +00002232 assert(!(isVarArg && IsTailCallConvention(CallConv)) &&
2233 "Var args not supported with calling convention fastcc or ghc");
Gordon Henriksenae636f82008-01-03 16:47:34 +00002234
Chris Lattner638402b2007-02-28 07:00:42 +00002235 // Analyze operands of the call, assigning locations to each operand.
Chris Lattner423c5f42007-02-28 05:31:48 +00002236 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002237 CCState CCInfo(CallConv, isVarArg, MF, getTargetMachine(),
Dan Gohman98ca4f22009-08-05 01:29:28 +00002238 ArgLocs, *DAG.getContext());
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00002239
2240 // Allocate shadow area for Win64
2241 if (IsWin64) {
2242 CCInfo.AllocateStack(32, 8);
2243 }
2244
Duncan Sands45907662010-10-31 13:21:44 +00002245 CCInfo.AnalyzeCallOperands(Outs, CC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00002246
Chris Lattner423c5f42007-02-28 05:31:48 +00002247 // Get a count of how many bytes are to be pushed on the stack.
2248 unsigned NumBytes = CCInfo.getNextStackOffset();
Evan Chengf22f9b32010-02-06 03:28:46 +00002249 if (IsSibcall)
Evan Chengb2c92902010-02-02 02:22:50 +00002250 // This is a sibcall. The memory operands are available in caller's
2251 // own caller's stack.
2252 NumBytes = 0;
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002253 else if (getTargetMachine().Options.GuaranteedTailCallOpt &&
2254 IsTailCallConvention(CallConv))
Evan Chengf22f9b32010-02-06 03:28:46 +00002255 NumBytes = GetAlignedArgumentStackSize(NumBytes, DAG);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002256
Gordon Henriksen86737662008-01-05 16:56:59 +00002257 int FPDiff = 0;
Evan Chengf22f9b32010-02-06 03:28:46 +00002258 if (isTailCall && !IsSibcall) {
Gordon Henriksen86737662008-01-05 16:56:59 +00002259 // Lower arguments at fp - stackoffset + fpdiff.
Scott Michelfdc40a02009-02-17 22:15:04 +00002260 unsigned NumBytesCallerPushed =
Gordon Henriksen86737662008-01-05 16:56:59 +00002261 MF.getInfo<X86MachineFunctionInfo>()->getBytesToPopOnReturn();
2262 FPDiff = NumBytesCallerPushed - NumBytes;
2263
2264 // Set the delta of movement of the returnaddr stackslot.
2265 // But only set if delta is greater than previous delta.
2266 if (FPDiff < (MF.getInfo<X86MachineFunctionInfo>()->getTCReturnAddrDelta()))
2267 MF.getInfo<X86MachineFunctionInfo>()->setTCReturnAddrDelta(FPDiff);
2268 }
2269
Evan Chengf22f9b32010-02-06 03:28:46 +00002270 if (!IsSibcall)
2271 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002272
Dan Gohman475871a2008-07-27 21:46:04 +00002273 SDValue RetAddrFrIdx;
Chris Lattner7a2bdde2011-04-15 05:18:47 +00002274 // Load return address for tail calls.
Evan Chengf22f9b32010-02-06 03:28:46 +00002275 if (isTailCall && FPDiff)
2276 Chain = EmitTailCallLoadRetAddr(DAG, RetAddrFrIdx, Chain, isTailCall,
2277 Is64Bit, FPDiff, dl);
Gordon Henriksen86737662008-01-05 16:56:59 +00002278
Dan Gohman475871a2008-07-27 21:46:04 +00002279 SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass;
2280 SmallVector<SDValue, 8> MemOpChains;
2281 SDValue StackPtr;
Chris Lattner423c5f42007-02-28 05:31:48 +00002282
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002283 // Walk the register/memloc assignments, inserting copies/loads. In the case
2284 // of tail call optimization arguments are handle later.
Chris Lattner423c5f42007-02-28 05:31:48 +00002285 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2286 CCValAssign &VA = ArgLocs[i];
Owen Andersone50ed302009-08-10 22:56:29 +00002287 EVT RegVT = VA.getLocVT();
Dan Gohmanc9403652010-07-07 15:54:55 +00002288 SDValue Arg = OutVals[i];
Dan Gohman98ca4f22009-08-05 01:29:28 +00002289 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Dan Gohman095cc292008-09-13 01:54:27 +00002290 bool isByVal = Flags.isByVal();
Scott Michelfdc40a02009-02-17 22:15:04 +00002291
Chris Lattner423c5f42007-02-28 05:31:48 +00002292 // Promote the value if needed.
2293 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002294 default: llvm_unreachable("Unknown loc info!");
Chris Lattner423c5f42007-02-28 05:31:48 +00002295 case CCValAssign::Full: break;
2296 case CCValAssign::SExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002297 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00002298 break;
2299 case CCValAssign::ZExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002300 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00002301 break;
2302 case CCValAssign::AExt:
Craig Topper7a9a28b2012-08-12 02:23:29 +00002303 if (RegVT.is128BitVector()) {
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002304 // Special case: passing MMX values in XMM registers.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002305 Arg = DAG.getNode(ISD::BITCAST, dl, MVT::i64, Arg);
Owen Anderson825b72b2009-08-11 20:47:22 +00002306 Arg = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i64, Arg);
2307 Arg = getMOVL(DAG, dl, MVT::v2i64, DAG.getUNDEF(MVT::v2i64), Arg);
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002308 } else
2309 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, RegVT, Arg);
2310 break;
2311 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002312 Arg = DAG.getNode(ISD::BITCAST, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00002313 break;
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00002314 case CCValAssign::Indirect: {
2315 // Store the argument.
2316 SDValue SpillSlot = DAG.CreateStackTemporary(VA.getValVT());
Evan Chengff89dcb2009-10-18 18:16:27 +00002317 int FI = cast<FrameIndexSDNode>(SpillSlot)->getIndex();
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00002318 Chain = DAG.getStore(Chain, dl, Arg, SpillSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00002319 MachinePointerInfo::getFixedStack(FI),
David Greene67c9d422010-02-15 16:53:33 +00002320 false, false, 0);
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00002321 Arg = SpillSlot;
2322 break;
2323 }
Evan Cheng6b5783d2006-05-25 18:56:34 +00002324 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002325
Chris Lattner423c5f42007-02-28 05:31:48 +00002326 if (VA.isRegLoc()) {
Stuart Hastings2aa0f232011-05-26 04:09:49 +00002327 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
2328 if (isVarArg && IsWin64) {
2329 // Win64 ABI requires argument XMM reg to be copied to the corresponding
2330 // shadow reg if callee is a varargs function.
2331 unsigned ShadowReg = 0;
2332 switch (VA.getLocReg()) {
2333 case X86::XMM0: ShadowReg = X86::RCX; break;
2334 case X86::XMM1: ShadowReg = X86::RDX; break;
2335 case X86::XMM2: ShadowReg = X86::R8; break;
2336 case X86::XMM3: ShadowReg = X86::R9; break;
Anton Korobeynikovc52bedb2010-08-27 14:43:06 +00002337 }
Stuart Hastings2aa0f232011-05-26 04:09:49 +00002338 if (ShadowReg)
2339 RegsToPass.push_back(std::make_pair(ShadowReg, Arg));
Anton Korobeynikovc52bedb2010-08-27 14:43:06 +00002340 }
Evan Chengf22f9b32010-02-06 03:28:46 +00002341 } else if (!IsSibcall && (!isTailCall || isByVal)) {
Evan Cheng5f941932010-02-05 02:21:12 +00002342 assert(VA.isMemLoc());
2343 if (StackPtr.getNode() == 0)
2344 StackPtr = DAG.getCopyFromReg(Chain, dl, X86StackPtr, getPointerTy());
2345 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Arg,
2346 dl, DAG, VA, Flags));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002347 }
Stuart Hastings2aa0f232011-05-26 04:09:49 +00002348 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002349
Evan Cheng32fe1032006-05-25 00:59:30 +00002350 if (!MemOpChains.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00002351 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002352 &MemOpChains[0], MemOpChains.size());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002353
Chris Lattner88e1fd52009-07-09 04:24:46 +00002354 if (Subtarget->isPICStyleGOT()) {
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002355 // ELF / PIC requires GOT in the EBX register before function calls via PLT
2356 // GOT pointer.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002357 if (!isTailCall) {
Jakob Stoklund Olesenb8720782012-07-04 19:28:31 +00002358 RegsToPass.push_back(std::make_pair(unsigned(X86::EBX),
2359 DAG.getNode(X86ISD::GlobalBaseReg, DebugLoc(), getPointerTy())));
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002360 } else {
2361 // If we are tail calling and generating PIC/GOT style code load the
2362 // address of the callee into ECX. The value in ecx is used as target of
2363 // the tail jump. This is done to circumvent the ebx/callee-saved problem
2364 // for tail calls on PIC/GOT architectures. Normally we would just put the
2365 // address of GOT into ebx and then call target@PLT. But for tail calls
2366 // ebx would be restored (since ebx is callee saved) before jumping to the
2367 // target@PLT.
2368
2369 // Note: The actual moving to ECX is done further down.
2370 GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee);
2371 if (G && !G->getGlobal()->hasHiddenVisibility() &&
2372 !G->getGlobal()->hasProtectedVisibility())
2373 Callee = LowerGlobalAddress(Callee, DAG);
2374 else if (isa<ExternalSymbolSDNode>(Callee))
Chris Lattner15a380a2009-07-09 04:39:06 +00002375 Callee = LowerExternalSymbol(Callee, DAG);
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002376 }
Anton Korobeynikov7f705592007-01-12 19:20:47 +00002377 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00002378
NAKAMURA Takumifb840c92011-02-05 15:11:13 +00002379 if (Is64Bit && isVarArg && !IsWin64) {
Gordon Henriksen86737662008-01-05 16:56:59 +00002380 // From AMD64 ABI document:
2381 // For calls that may call functions that use varargs or stdargs
2382 // (prototype-less calls or calls to functions containing ellipsis (...) in
2383 // the declaration) %al is used as hidden argument to specify the number
2384 // of SSE registers used. The contents of %al do not need to match exactly
2385 // the number of registers, but must be an ubound on the number of SSE
2386 // registers used and is in the range 0 - 8 inclusive.
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002387
Gordon Henriksen86737662008-01-05 16:56:59 +00002388 // Count the number of XMM registers allocated.
Craig Topperc5eaae42012-03-11 07:57:25 +00002389 static const uint16_t XMMArgRegs[] = {
Gordon Henriksen86737662008-01-05 16:56:59 +00002390 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
2391 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
2392 };
2393 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs, 8);
Craig Topper1accb7e2012-01-10 06:54:16 +00002394 assert((Subtarget->hasSSE1() || !NumXMMRegs)
Torok Edwin3f142c32009-02-01 18:15:56 +00002395 && "SSE registers cannot be used when SSE is disabled");
Scott Michelfdc40a02009-02-17 22:15:04 +00002396
Jakob Stoklund Olesenb8720782012-07-04 19:28:31 +00002397 RegsToPass.push_back(std::make_pair(unsigned(X86::AL),
2398 DAG.getConstant(NumXMMRegs, MVT::i8)));
Gordon Henriksen86737662008-01-05 16:56:59 +00002399 }
2400
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00002401 // For tail calls lower the arguments to the 'real' stack slot.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002402 if (isTailCall) {
2403 // Force all the incoming stack arguments to be loaded from the stack
2404 // before any new outgoing arguments are stored to the stack, because the
2405 // outgoing stack slots may alias the incoming argument stack slots, and
2406 // the alias isn't otherwise explicit. This is slightly more conservative
2407 // than necessary, because it means that each store effectively depends
2408 // on every argument instead of just those arguments it would clobber.
2409 SDValue ArgChain = DAG.getStackArgumentTokenFactor(Chain);
2410
Dan Gohman475871a2008-07-27 21:46:04 +00002411 SmallVector<SDValue, 8> MemOpChains2;
2412 SDValue FIN;
Gordon Henriksen86737662008-01-05 16:56:59 +00002413 int FI = 0;
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002414 if (getTargetMachine().Options.GuaranteedTailCallOpt) {
Evan Chengb2c92902010-02-02 02:22:50 +00002415 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2416 CCValAssign &VA = ArgLocs[i];
2417 if (VA.isRegLoc())
2418 continue;
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00002419 assert(VA.isMemLoc());
Dan Gohmanc9403652010-07-07 15:54:55 +00002420 SDValue Arg = OutVals[i];
Dan Gohman98ca4f22009-08-05 01:29:28 +00002421 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Gordon Henriksen86737662008-01-05 16:56:59 +00002422 // Create frame index.
2423 int32_t Offset = VA.getLocMemOffset()+FPDiff;
Duncan Sands83ec4b62008-06-06 12:08:01 +00002424 uint32_t OpSize = (VA.getLocVT().getSizeInBits()+7)/8;
Evan Chenged2ae132010-07-03 00:40:23 +00002425 FI = MF.getFrameInfo()->CreateFixedObject(OpSize, Offset, true);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002426 FIN = DAG.getFrameIndex(FI, getPointerTy());
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00002427
Duncan Sands276dcbd2008-03-21 09:14:45 +00002428 if (Flags.isByVal()) {
Evan Cheng8e5712b2008-01-12 01:08:07 +00002429 // Copy relative to framepointer.
Dan Gohman475871a2008-07-27 21:46:04 +00002430 SDValue Source = DAG.getIntPtrConstant(VA.getLocMemOffset());
Gabor Greifba36cb52008-08-28 21:40:38 +00002431 if (StackPtr.getNode() == 0)
Scott Michelfdc40a02009-02-17 22:15:04 +00002432 StackPtr = DAG.getCopyFromReg(Chain, dl, X86StackPtr,
Dale Johannesendd64c412009-02-04 00:33:20 +00002433 getPointerTy());
Dale Johannesenace16102009-02-03 19:33:06 +00002434 Source = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, Source);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002435
Dan Gohman98ca4f22009-08-05 01:29:28 +00002436 MemOpChains2.push_back(CreateCopyOfByValArgument(Source, FIN,
2437 ArgChain,
Dale Johannesendd64c412009-02-04 00:33:20 +00002438 Flags, DAG, dl));
Gordon Henriksen86737662008-01-05 16:56:59 +00002439 } else {
Evan Cheng8e5712b2008-01-12 01:08:07 +00002440 // Store relative to framepointer.
Dan Gohman69de1932008-02-06 22:27:42 +00002441 MemOpChains2.push_back(
Dan Gohman98ca4f22009-08-05 01:29:28 +00002442 DAG.getStore(ArgChain, dl, Arg, FIN,
Chris Lattnere8639032010-09-21 06:22:23 +00002443 MachinePointerInfo::getFixedStack(FI),
David Greene67c9d422010-02-15 16:53:33 +00002444 false, false, 0));
Scott Michelfdc40a02009-02-17 22:15:04 +00002445 }
Gordon Henriksen86737662008-01-05 16:56:59 +00002446 }
2447 }
2448
2449 if (!MemOpChains2.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00002450 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Arnold Schwaighofer719eb022008-01-11 14:34:56 +00002451 &MemOpChains2[0], MemOpChains2.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00002452
2453 // Store the return address to the appropriate stack slot.
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002454 Chain = EmitTailCallStoreRetAddr(DAG, MF, Chain, RetAddrFrIdx, Is64Bit,
Dale Johannesenace16102009-02-03 19:33:06 +00002455 FPDiff, dl);
Gordon Henriksen86737662008-01-05 16:56:59 +00002456 }
2457
Jakob Stoklund Olesenb8720782012-07-04 19:28:31 +00002458 // Build a sequence of copy-to-reg nodes chained together with token chain
2459 // and flag operands which copy the outgoing args into registers.
2460 SDValue InFlag;
2461 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
2462 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
2463 RegsToPass[i].second, InFlag);
2464 InFlag = Chain.getValue(1);
2465 }
2466
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002467 if (getTargetMachine().getCodeModel() == CodeModel::Large) {
2468 assert(Is64Bit && "Large code model is only legal in 64-bit mode.");
2469 // In the 64-bit large code model, we have to make all calls
2470 // through a register, since the call instruction's 32-bit
2471 // pc-relative offset may not be large enough to hold the whole
2472 // address.
2473 } else if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002474 // If the callee is a GlobalAddress node (quite common, every direct call
2475 // is) turn it into a TargetGlobalAddress node so that legalize doesn't hack
2476 // it.
2477
Anton Korobeynikov2b2bc682006-12-22 22:29:05 +00002478 // We should use extra load for direct calls to dllimported functions in
2479 // non-JIT mode.
Dan Gohman46510a72010-04-15 01:51:59 +00002480 const GlobalValue *GV = G->getGlobal();
Chris Lattner754b7652009-07-10 05:48:03 +00002481 if (!GV->hasDLLImportLinkage()) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002482 unsigned char OpFlags = 0;
John McCall3a3465b2011-06-15 20:36:13 +00002483 bool ExtraLoad = false;
2484 unsigned WrapperKind = ISD::DELETED_NODE;
Eric Christopherfd179292009-08-27 18:07:15 +00002485
Chris Lattner48a7d022009-07-09 05:02:21 +00002486 // On ELF targets, in both X86-64 and X86-32 mode, direct calls to
2487 // external symbols most go through the PLT in PIC mode. If the symbol
2488 // has hidden or protected visibility, or if it is static or local, then
2489 // we don't need to use the PLT - we can directly call it.
2490 if (Subtarget->isTargetELF() &&
2491 getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
Chris Lattner74e726e2009-07-09 05:27:35 +00002492 GV->hasDefaultVisibility() && !GV->hasLocalLinkage()) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002493 OpFlags = X86II::MO_PLT;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00002494 } else if (Subtarget->isPICStyleStubAny() &&
Chris Lattner80945782010-09-27 06:34:01 +00002495 (GV->isDeclaration() || GV->isWeakForLinker()) &&
Daniel Dunbar558692f2011-04-20 00:14:25 +00002496 (!Subtarget->getTargetTriple().isMacOSX() ||
2497 Subtarget->getTargetTriple().isMacOSXVersionLT(10, 5))) {
Chris Lattner74e726e2009-07-09 05:27:35 +00002498 // PC-relative references to external symbols should go through $stub,
2499 // unless we're building with the leopard linker or later, which
2500 // automatically synthesizes these stubs.
2501 OpFlags = X86II::MO_DARWIN_STUB;
John McCall3a3465b2011-06-15 20:36:13 +00002502 } else if (Subtarget->isPICStyleRIPRel() &&
2503 isa<Function>(GV) &&
Bill Wendling67658342012-10-09 07:45:08 +00002504 cast<Function>(GV)->getFnAttributes().
2505 hasAttribute(Attributes::NonLazyBind)) {
John McCall3a3465b2011-06-15 20:36:13 +00002506 // If the function is marked as non-lazy, generate an indirect call
2507 // which loads from the GOT directly. This avoids runtime overhead
2508 // at the cost of eager binding (and one extra byte of encoding).
2509 OpFlags = X86II::MO_GOTPCREL;
2510 WrapperKind = X86ISD::WrapperRIP;
2511 ExtraLoad = true;
Chris Lattner74e726e2009-07-09 05:27:35 +00002512 }
Chris Lattner48a7d022009-07-09 05:02:21 +00002513
Devang Patel0d881da2010-07-06 22:08:15 +00002514 Callee = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(),
Chris Lattner48a7d022009-07-09 05:02:21 +00002515 G->getOffset(), OpFlags);
John McCall3a3465b2011-06-15 20:36:13 +00002516
2517 // Add a wrapper if needed.
2518 if (WrapperKind != ISD::DELETED_NODE)
2519 Callee = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Callee);
2520 // Add extra indirection if needed.
2521 if (ExtraLoad)
2522 Callee = DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(), Callee,
2523 MachinePointerInfo::getGOT(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002524 false, false, false, 0);
Chris Lattner48a7d022009-07-09 05:02:21 +00002525 }
Bill Wendling056292f2008-09-16 21:48:12 +00002526 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002527 unsigned char OpFlags = 0;
2528
Evan Cheng1bf891a2010-12-01 22:59:46 +00002529 // On ELF targets, in either X86-64 or X86-32 mode, direct calls to
2530 // external symbols should go through the PLT.
2531 if (Subtarget->isTargetELF() &&
2532 getTargetMachine().getRelocationModel() == Reloc::PIC_) {
2533 OpFlags = X86II::MO_PLT;
2534 } else if (Subtarget->isPICStyleStubAny() &&
Daniel Dunbar558692f2011-04-20 00:14:25 +00002535 (!Subtarget->getTargetTriple().isMacOSX() ||
2536 Subtarget->getTargetTriple().isMacOSXVersionLT(10, 5))) {
Evan Cheng1bf891a2010-12-01 22:59:46 +00002537 // PC-relative references to external symbols should go through $stub,
2538 // unless we're building with the leopard linker or later, which
2539 // automatically synthesizes these stubs.
2540 OpFlags = X86II::MO_DARWIN_STUB;
Chris Lattner74e726e2009-07-09 05:27:35 +00002541 }
Eric Christopherfd179292009-08-27 18:07:15 +00002542
Chris Lattner48a7d022009-07-09 05:02:21 +00002543 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy(),
2544 OpFlags);
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002545 }
2546
Chris Lattnerd96d0722007-02-25 06:40:16 +00002547 // Returns a chain & a flag for retval copy to use.
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002548 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Dan Gohman475871a2008-07-27 21:46:04 +00002549 SmallVector<SDValue, 8> Ops;
Gordon Henriksen86737662008-01-05 16:56:59 +00002550
Evan Chengf22f9b32010-02-06 03:28:46 +00002551 if (!IsSibcall && isTailCall) {
Dale Johannesene8d72302009-02-06 23:05:02 +00002552 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
2553 DAG.getIntPtrConstant(0, true), InFlag);
Gordon Henriksen86737662008-01-05 16:56:59 +00002554 InFlag = Chain.getValue(1);
Gordon Henriksen86737662008-01-05 16:56:59 +00002555 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002556
Nate Begeman4c5dcf52006-02-17 00:03:04 +00002557 Ops.push_back(Chain);
2558 Ops.push_back(Callee);
Evan Chengb69d1132006-06-14 18:17:40 +00002559
Dan Gohman98ca4f22009-08-05 01:29:28 +00002560 if (isTailCall)
Owen Anderson825b72b2009-08-11 20:47:22 +00002561 Ops.push_back(DAG.getConstant(FPDiff, MVT::i32));
Evan Chengf4684712007-02-21 21:18:14 +00002562
Gordon Henriksen86737662008-01-05 16:56:59 +00002563 // Add argument registers to the end of the list so that they are known live
2564 // into the call.
Evan Cheng9b449442008-01-07 23:08:23 +00002565 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
2566 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
2567 RegsToPass[i].second.getValueType()));
Scott Michelfdc40a02009-02-17 22:15:04 +00002568
Jakob Stoklund Olesen8bcde2a2012-02-16 00:02:50 +00002569 // Add a register mask operand representing the call-preserved registers.
2570 const TargetRegisterInfo *TRI = getTargetMachine().getRegisterInfo();
2571 const uint32_t *Mask = TRI->getCallPreservedMask(CallConv);
2572 assert(Mask && "Missing call preserved mask for calling convention");
2573 Ops.push_back(DAG.getRegisterMask(Mask));
Jakob Stoklund Olesenc38c4562012-01-18 23:52:22 +00002574
Gabor Greifba36cb52008-08-28 21:40:38 +00002575 if (InFlag.getNode())
Evan Cheng347d5f72006-04-28 21:29:37 +00002576 Ops.push_back(InFlag);
Gordon Henriksenae636f82008-01-03 16:47:34 +00002577
Dan Gohman98ca4f22009-08-05 01:29:28 +00002578 if (isTailCall) {
Dale Johannesen88004c22010-06-05 00:30:45 +00002579 // We used to do:
2580 //// If this is the first return lowered for this function, add the regs
2581 //// to the liveout set for the function.
2582 // This isn't right, although it's probably harmless on x86; liveouts
2583 // should be computed from returns not tail calls. Consider a void
2584 // function making a tail call to a function returning int.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002585 return DAG.getNode(X86ISD::TC_RETURN, dl,
2586 NodeTys, &Ops[0], Ops.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00002587 }
2588
Dale Johannesenace16102009-02-03 19:33:06 +00002589 Chain = DAG.getNode(X86ISD::CALL, dl, NodeTys, &Ops[0], Ops.size());
Evan Cheng347d5f72006-04-28 21:29:37 +00002590 InFlag = Chain.getValue(1);
Evan Chengd90eb7f2006-01-05 00:27:02 +00002591
Chris Lattner2d297092006-05-23 18:50:38 +00002592 // Create the CALLSEQ_END node.
Gordon Henriksen86737662008-01-05 16:56:59 +00002593 unsigned NumBytesForCalleeToPush;
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002594 if (X86::isCalleePop(CallConv, Is64Bit, isVarArg,
2595 getTargetMachine().Options.GuaranteedTailCallOpt))
Gordon Henriksen86737662008-01-05 16:56:59 +00002596 NumBytesForCalleeToPush = NumBytes; // Callee pops everything
Eli Friedman9a2478a2012-01-20 00:05:46 +00002597 else if (!Is64Bit && !IsTailCallConvention(CallConv) && !IsWindows &&
Rafael Espindola1cee7102012-07-25 13:41:10 +00002598 SR == StackStructReturn)
Dan Gohmanf451cb82010-02-10 16:03:48 +00002599 // If this is a call to a struct-return function, the callee
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00002600 // pops the hidden struct pointer, so we have to push it back.
2601 // This is common for Darwin/X86, Linux & Mingw32 targets.
Eli Friedman9a2478a2012-01-20 00:05:46 +00002602 // For MSVC Win32 targets, the caller pops the hidden struct pointer.
Gordon Henriksenae636f82008-01-03 16:47:34 +00002603 NumBytesForCalleeToPush = 4;
Gordon Henriksen86737662008-01-05 16:56:59 +00002604 else
Gordon Henriksenae636f82008-01-03 16:47:34 +00002605 NumBytesForCalleeToPush = 0; // Callee pops nothing.
Scott Michelfdc40a02009-02-17 22:15:04 +00002606
Gordon Henriksenae636f82008-01-03 16:47:34 +00002607 // Returns a flag for retval copy to use.
Evan Chengf22f9b32010-02-06 03:28:46 +00002608 if (!IsSibcall) {
2609 Chain = DAG.getCALLSEQ_END(Chain,
2610 DAG.getIntPtrConstant(NumBytes, true),
2611 DAG.getIntPtrConstant(NumBytesForCalleeToPush,
2612 true),
2613 InFlag);
2614 InFlag = Chain.getValue(1);
2615 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00002616
Chris Lattner3085e152007-02-25 08:59:22 +00002617 // Handle result values, copying them out of physregs into vregs that we
2618 // return.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002619 return LowerCallResult(Chain, InFlag, CallConv, isVarArg,
2620 Ins, dl, DAG, InVals);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002621}
2622
Evan Cheng25ab6902006-09-08 06:48:29 +00002623
2624//===----------------------------------------------------------------------===//
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002625// Fast Calling Convention (tail call) implementation
2626//===----------------------------------------------------------------------===//
2627
2628// Like std call, callee cleans arguments, convention except that ECX is
2629// reserved for storing the tail called function address. Only 2 registers are
2630// free for argument passing (inreg). Tail call optimization is performed
2631// provided:
2632// * tailcallopt is enabled
2633// * caller/callee are fastcc
Arnold Schwaighofera2a4b472008-02-26 10:21:54 +00002634// On X86_64 architecture with GOT-style position independent code only local
2635// (within module) calls are supported at the moment.
Arnold Schwaighofer48abc5c2007-10-12 21:30:57 +00002636// To keep the stack aligned according to platform abi the function
2637// GetAlignedArgumentStackSize ensures that argument delta is always multiples
2638// of stack alignment. (Dynamic linkers need this - darwin's dyld for example)
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002639// If a tail called function callee has more arguments than the caller the
2640// caller needs to make sure that there is room to move the RETADDR to. This is
Arnold Schwaighofer48abc5c2007-10-12 21:30:57 +00002641// achieved by reserving an area the size of the argument delta right after the
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002642// original REtADDR, but before the saved framepointer or the spilled registers
2643// e.g. caller(arg1, arg2) calls callee(arg1, arg2,arg3,arg4)
2644// stack layout:
2645// arg1
2646// arg2
2647// RETADDR
Scott Michelfdc40a02009-02-17 22:15:04 +00002648// [ new RETADDR
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002649// move area ]
2650// (possible EBP)
2651// ESI
2652// EDI
2653// local1 ..
2654
2655/// GetAlignedArgumentStackSize - Make the stack size align e.g 16n + 12 aligned
2656/// for a 16 byte align requirement.
Dan Gohmand858e902010-04-17 15:26:15 +00002657unsigned
2658X86TargetLowering::GetAlignedArgumentStackSize(unsigned StackSize,
2659 SelectionDAG& DAG) const {
Evan Chenge9ac9e62008-09-07 09:07:23 +00002660 MachineFunction &MF = DAG.getMachineFunction();
2661 const TargetMachine &TM = MF.getTarget();
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00002662 const TargetFrameLowering &TFI = *TM.getFrameLowering();
Evan Chenge9ac9e62008-09-07 09:07:23 +00002663 unsigned StackAlignment = TFI.getStackAlignment();
Scott Michelfdc40a02009-02-17 22:15:04 +00002664 uint64_t AlignMask = StackAlignment - 1;
Evan Chenge9ac9e62008-09-07 09:07:23 +00002665 int64_t Offset = StackSize;
Micah Villmow2c39b152012-10-15 16:24:29 +00002666 uint64_t SlotSize = TD->getPointerSize(0);
Evan Chenge9ac9e62008-09-07 09:07:23 +00002667 if ( (Offset & AlignMask) <= (StackAlignment - SlotSize) ) {
2668 // Number smaller than 12 so just add the difference.
2669 Offset += ((StackAlignment - SlotSize) - (Offset & AlignMask));
2670 } else {
2671 // Mask out lower bits, add stackalignment once plus the 12 bytes.
Scott Michelfdc40a02009-02-17 22:15:04 +00002672 Offset = ((~AlignMask) & Offset) + StackAlignment +
Evan Chenge9ac9e62008-09-07 09:07:23 +00002673 (StackAlignment-SlotSize);
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002674 }
Evan Chenge9ac9e62008-09-07 09:07:23 +00002675 return Offset;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002676}
2677
Evan Cheng5f941932010-02-05 02:21:12 +00002678/// MatchingStackOffset - Return true if the given stack call argument is
2679/// already available in the same position (relatively) of the caller's
2680/// incoming argument stack.
2681static
2682bool MatchingStackOffset(SDValue Arg, unsigned Offset, ISD::ArgFlagsTy Flags,
2683 MachineFrameInfo *MFI, const MachineRegisterInfo *MRI,
2684 const X86InstrInfo *TII) {
Evan Cheng4cae1332010-03-05 08:38:04 +00002685 unsigned Bytes = Arg.getValueType().getSizeInBits() / 8;
2686 int FI = INT_MAX;
Evan Cheng5f941932010-02-05 02:21:12 +00002687 if (Arg.getOpcode() == ISD::CopyFromReg) {
2688 unsigned VR = cast<RegisterSDNode>(Arg.getOperand(1))->getReg();
Jakob Stoklund Olesenc9df0252011-01-10 02:58:51 +00002689 if (!TargetRegisterInfo::isVirtualRegister(VR))
Evan Cheng5f941932010-02-05 02:21:12 +00002690 return false;
2691 MachineInstr *Def = MRI->getVRegDef(VR);
2692 if (!Def)
2693 return false;
2694 if (!Flags.isByVal()) {
2695 if (!TII->isLoadFromStackSlot(Def, FI))
2696 return false;
2697 } else {
2698 unsigned Opcode = Def->getOpcode();
2699 if ((Opcode == X86::LEA32r || Opcode == X86::LEA64r) &&
2700 Def->getOperand(1).isFI()) {
2701 FI = Def->getOperand(1).getIndex();
Evan Cheng4cae1332010-03-05 08:38:04 +00002702 Bytes = Flags.getByValSize();
Evan Cheng5f941932010-02-05 02:21:12 +00002703 } else
2704 return false;
2705 }
Evan Cheng4cae1332010-03-05 08:38:04 +00002706 } else if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Arg)) {
2707 if (Flags.isByVal())
2708 // ByVal argument is passed in as a pointer but it's now being
Evan Cheng10718492010-03-05 19:55:55 +00002709 // dereferenced. e.g.
Evan Cheng4cae1332010-03-05 08:38:04 +00002710 // define @foo(%struct.X* %A) {
2711 // tail call @bar(%struct.X* byval %A)
2712 // }
Evan Cheng5f941932010-02-05 02:21:12 +00002713 return false;
2714 SDValue Ptr = Ld->getBasePtr();
2715 FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr);
2716 if (!FINode)
2717 return false;
2718 FI = FINode->getIndex();
Chad Rosierdf78fcd2011-06-25 02:04:56 +00002719 } else if (Arg.getOpcode() == ISD::FrameIndex && Flags.isByVal()) {
Chad Rosier14d71aa2011-06-25 18:51:28 +00002720 FrameIndexSDNode *FINode = cast<FrameIndexSDNode>(Arg);
Chad Rosierdf78fcd2011-06-25 02:04:56 +00002721 FI = FINode->getIndex();
2722 Bytes = Flags.getByValSize();
Evan Cheng4cae1332010-03-05 08:38:04 +00002723 } else
2724 return false;
Evan Cheng5f941932010-02-05 02:21:12 +00002725
Evan Cheng4cae1332010-03-05 08:38:04 +00002726 assert(FI != INT_MAX);
Evan Cheng5f941932010-02-05 02:21:12 +00002727 if (!MFI->isFixedObjectIndex(FI))
2728 return false;
Evan Cheng4cae1332010-03-05 08:38:04 +00002729 return Offset == MFI->getObjectOffset(FI) && Bytes == MFI->getObjectSize(FI);
Evan Cheng5f941932010-02-05 02:21:12 +00002730}
2731
Dan Gohman98ca4f22009-08-05 01:29:28 +00002732/// IsEligibleForTailCallOptimization - Check whether the call is eligible
2733/// for tail call optimization. Targets which want to do tail call
2734/// optimization should implement this function.
2735bool
2736X86TargetLowering::IsEligibleForTailCallOptimization(SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00002737 CallingConv::ID CalleeCC,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002738 bool isVarArg,
Evan Chenga375d472010-03-15 18:54:48 +00002739 bool isCalleeStructRet,
2740 bool isCallerStructRet,
Evan Chengb1cacc72012-09-25 05:32:34 +00002741 Type *RetTy,
Evan Chengb1712452010-01-27 06:25:16 +00002742 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00002743 const SmallVectorImpl<SDValue> &OutVals,
Evan Chengb1712452010-01-27 06:25:16 +00002744 const SmallVectorImpl<ISD::InputArg> &Ins,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002745 SelectionDAG& DAG) const {
Chris Lattner29689432010-03-11 00:22:57 +00002746 if (!IsTailCallConvention(CalleeCC) &&
Evan Chengb1712452010-01-27 06:25:16 +00002747 CalleeCC != CallingConv::C)
2748 return false;
2749
Evan Cheng7096ae42010-01-29 06:45:59 +00002750 // If -tailcallopt is specified, make fastcc functions tail-callable.
Evan Cheng2c12cb42010-03-26 16:26:03 +00002751 const MachineFunction &MF = DAG.getMachineFunction();
Evan Cheng7096ae42010-01-29 06:45:59 +00002752 const Function *CallerF = DAG.getMachineFunction().getFunction();
Evan Chengb1cacc72012-09-25 05:32:34 +00002753
2754 // If the function return type is x86_fp80 and the callee return type is not,
2755 // then the FP_EXTEND of the call result is not a nop. It's not safe to
2756 // perform a tailcall optimization here.
2757 if (CallerF->getReturnType()->isX86_FP80Ty() && !RetTy->isX86_FP80Ty())
2758 return false;
2759
Evan Cheng13617962010-04-30 01:12:32 +00002760 CallingConv::ID CallerCC = CallerF->getCallingConv();
2761 bool CCMatch = CallerCC == CalleeCC;
2762
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002763 if (getTargetMachine().Options.GuaranteedTailCallOpt) {
Evan Cheng13617962010-04-30 01:12:32 +00002764 if (IsTailCallConvention(CalleeCC) && CCMatch)
Evan Cheng843bd692010-01-31 06:44:49 +00002765 return true;
2766 return false;
2767 }
2768
Dale Johannesen2f05cc02010-05-28 23:24:28 +00002769 // Look for obvious safe cases to perform tail call optimization that do not
2770 // require ABI changes. This is what gcc calls sibcall.
Evan Chengb2c92902010-02-02 02:22:50 +00002771
Evan Cheng2c12cb42010-03-26 16:26:03 +00002772 // Can't do sibcall if stack needs to be dynamically re-aligned. PEI needs to
2773 // emit a special epilogue.
2774 if (RegInfo->needsStackRealignment(MF))
2775 return false;
2776
Evan Chenga375d472010-03-15 18:54:48 +00002777 // Also avoid sibcall optimization if either caller or callee uses struct
2778 // return semantics.
2779 if (isCalleeStructRet || isCallerStructRet)
2780 return false;
2781
Chad Rosier2416da32011-06-24 21:15:36 +00002782 // An stdcall caller is expected to clean up its arguments; the callee
2783 // isn't going to do that.
2784 if (!CCMatch && CallerCC==CallingConv::X86_StdCall)
2785 return false;
2786
Chad Rosier871f6642011-05-18 19:59:50 +00002787 // Do not sibcall optimize vararg calls unless all arguments are passed via
Chad Rosiera1660892011-05-20 00:59:28 +00002788 // registers.
Chad Rosier871f6642011-05-18 19:59:50 +00002789 if (isVarArg && !Outs.empty()) {
Chad Rosiera1660892011-05-20 00:59:28 +00002790
2791 // Optimizing for varargs on Win64 is unlikely to be safe without
2792 // additional testing.
2793 if (Subtarget->isTargetWin64())
2794 return false;
2795
Chad Rosier871f6642011-05-18 19:59:50 +00002796 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002797 CCState CCInfo(CalleeCC, isVarArg, DAG.getMachineFunction(),
Craig Topper0fbf3642012-04-23 03:28:34 +00002798 getTargetMachine(), ArgLocs, *DAG.getContext());
Chad Rosier871f6642011-05-18 19:59:50 +00002799
Chad Rosier871f6642011-05-18 19:59:50 +00002800 CCInfo.AnalyzeCallOperands(Outs, CC_X86);
2801 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i)
2802 if (!ArgLocs[i].isRegLoc())
2803 return false;
2804 }
2805
Chad Rosier30450e82011-12-22 22:35:21 +00002806 // If the call result is in ST0 / ST1, it needs to be popped off the x87
2807 // stack. Therefore, if it's not used by the call it is not safe to optimize
2808 // this into a sibcall.
Evan Chengf5b9d6c2010-03-20 02:58:15 +00002809 bool Unused = false;
2810 for (unsigned i = 0, e = Ins.size(); i != e; ++i) {
2811 if (!Ins[i].Used) {
2812 Unused = true;
2813 break;
2814 }
2815 }
2816 if (Unused) {
2817 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002818 CCState CCInfo(CalleeCC, false, DAG.getMachineFunction(),
Craig Topper0fbf3642012-04-23 03:28:34 +00002819 getTargetMachine(), RVLocs, *DAG.getContext());
Evan Chengf5b9d6c2010-03-20 02:58:15 +00002820 CCInfo.AnalyzeCallResult(Ins, RetCC_X86);
Evan Cheng13617962010-04-30 01:12:32 +00002821 for (unsigned i = 0, e = RVLocs.size(); i != e; ++i) {
Evan Chengf5b9d6c2010-03-20 02:58:15 +00002822 CCValAssign &VA = RVLocs[i];
2823 if (VA.getLocReg() == X86::ST0 || VA.getLocReg() == X86::ST1)
2824 return false;
2825 }
2826 }
2827
Evan Cheng13617962010-04-30 01:12:32 +00002828 // If the calling conventions do not match, then we'd better make sure the
2829 // results are returned in the same way as what the caller expects.
2830 if (!CCMatch) {
2831 SmallVector<CCValAssign, 16> RVLocs1;
Eric Christopher471e4222011-06-08 23:55:35 +00002832 CCState CCInfo1(CalleeCC, false, DAG.getMachineFunction(),
Craig Topper0fbf3642012-04-23 03:28:34 +00002833 getTargetMachine(), RVLocs1, *DAG.getContext());
Evan Cheng13617962010-04-30 01:12:32 +00002834 CCInfo1.AnalyzeCallResult(Ins, RetCC_X86);
2835
2836 SmallVector<CCValAssign, 16> RVLocs2;
Eric Christopher471e4222011-06-08 23:55:35 +00002837 CCState CCInfo2(CallerCC, false, DAG.getMachineFunction(),
Craig Topper0fbf3642012-04-23 03:28:34 +00002838 getTargetMachine(), RVLocs2, *DAG.getContext());
Evan Cheng13617962010-04-30 01:12:32 +00002839 CCInfo2.AnalyzeCallResult(Ins, RetCC_X86);
2840
2841 if (RVLocs1.size() != RVLocs2.size())
2842 return false;
2843 for (unsigned i = 0, e = RVLocs1.size(); i != e; ++i) {
2844 if (RVLocs1[i].isRegLoc() != RVLocs2[i].isRegLoc())
2845 return false;
2846 if (RVLocs1[i].getLocInfo() != RVLocs2[i].getLocInfo())
2847 return false;
2848 if (RVLocs1[i].isRegLoc()) {
2849 if (RVLocs1[i].getLocReg() != RVLocs2[i].getLocReg())
2850 return false;
2851 } else {
2852 if (RVLocs1[i].getLocMemOffset() != RVLocs2[i].getLocMemOffset())
2853 return false;
2854 }
2855 }
2856 }
2857
Evan Chenga6bff982010-01-30 01:22:00 +00002858 // If the callee takes no arguments then go on to check the results of the
2859 // call.
2860 if (!Outs.empty()) {
2861 // Check if stack adjustment is needed. For now, do not do this if any
2862 // argument is passed on the stack.
2863 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002864 CCState CCInfo(CalleeCC, isVarArg, DAG.getMachineFunction(),
Craig Topper0fbf3642012-04-23 03:28:34 +00002865 getTargetMachine(), ArgLocs, *DAG.getContext());
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00002866
2867 // Allocate shadow area for Win64
2868 if (Subtarget->isTargetWin64()) {
2869 CCInfo.AllocateStack(32, 8);
2870 }
2871
Duncan Sands45907662010-10-31 13:21:44 +00002872 CCInfo.AnalyzeCallOperands(Outs, CC_X86);
Stuart Hastings6db2c2f2011-05-17 16:59:46 +00002873 if (CCInfo.getNextStackOffset()) {
Evan Chengb2c92902010-02-02 02:22:50 +00002874 MachineFunction &MF = DAG.getMachineFunction();
2875 if (MF.getInfo<X86MachineFunctionInfo>()->getBytesToPopOnReturn())
2876 return false;
Evan Chengb2c92902010-02-02 02:22:50 +00002877
2878 // Check if the arguments are already laid out in the right way as
2879 // the caller's fixed stack objects.
2880 MachineFrameInfo *MFI = MF.getFrameInfo();
Evan Cheng5f941932010-02-05 02:21:12 +00002881 const MachineRegisterInfo *MRI = &MF.getRegInfo();
2882 const X86InstrInfo *TII =
Roman Divacky59324292012-09-05 22:26:57 +00002883 ((const X86TargetMachine&)getTargetMachine()).getInstrInfo();
Evan Chengb2c92902010-02-02 02:22:50 +00002884 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2885 CCValAssign &VA = ArgLocs[i];
Dan Gohmanc9403652010-07-07 15:54:55 +00002886 SDValue Arg = OutVals[i];
Evan Chengb2c92902010-02-02 02:22:50 +00002887 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Evan Chengb2c92902010-02-02 02:22:50 +00002888 if (VA.getLocInfo() == CCValAssign::Indirect)
2889 return false;
2890 if (!VA.isRegLoc()) {
Evan Cheng5f941932010-02-05 02:21:12 +00002891 if (!MatchingStackOffset(Arg, VA.getLocMemOffset(), Flags,
2892 MFI, MRI, TII))
Evan Chengb2c92902010-02-02 02:22:50 +00002893 return false;
2894 }
2895 }
2896 }
Evan Cheng9c044672010-05-29 01:35:22 +00002897
2898 // If the tailcall address may be in a register, then make sure it's
2899 // possible to register allocate for it. In 32-bit, the call address can
2900 // only target EAX, EDX, or ECX since the tail call must be scheduled after
Evan Chengdedd9742010-07-14 06:44:01 +00002901 // callee-saved registers are restored. These happen to be the same
2902 // registers used to pass 'inreg' arguments so watch out for those.
2903 if (!Subtarget->is64Bit() &&
2904 !isa<GlobalAddressSDNode>(Callee) &&
Evan Cheng9c044672010-05-29 01:35:22 +00002905 !isa<ExternalSymbolSDNode>(Callee)) {
Evan Cheng9c044672010-05-29 01:35:22 +00002906 unsigned NumInRegs = 0;
2907 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2908 CCValAssign &VA = ArgLocs[i];
Evan Chengdedd9742010-07-14 06:44:01 +00002909 if (!VA.isRegLoc())
2910 continue;
2911 unsigned Reg = VA.getLocReg();
2912 switch (Reg) {
2913 default: break;
2914 case X86::EAX: case X86::EDX: case X86::ECX:
2915 if (++NumInRegs == 3)
Evan Cheng9c044672010-05-29 01:35:22 +00002916 return false;
Evan Chengdedd9742010-07-14 06:44:01 +00002917 break;
Evan Cheng9c044672010-05-29 01:35:22 +00002918 }
2919 }
2920 }
Evan Chenga6bff982010-01-30 01:22:00 +00002921 }
Evan Chengb1712452010-01-27 06:25:16 +00002922
Evan Cheng86809cc2010-02-03 03:28:02 +00002923 return true;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002924}
2925
Dan Gohman3df24e62008-09-03 23:12:08 +00002926FastISel *
Bob Wilsond49edb72012-08-03 04:06:28 +00002927X86TargetLowering::createFastISel(FunctionLoweringInfo &funcInfo,
2928 const TargetLibraryInfo *libInfo) const {
2929 return X86::createFastISel(funcInfo, libInfo);
Dan Gohmand9f3c482008-08-19 21:32:53 +00002930}
2931
2932
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00002933//===----------------------------------------------------------------------===//
2934// Other Lowering Hooks
2935//===----------------------------------------------------------------------===//
2936
Bruno Cardoso Lopese654b562010-09-01 00:51:36 +00002937static bool MayFoldLoad(SDValue Op) {
2938 return Op.hasOneUse() && ISD::isNormalLoad(Op.getNode());
2939}
2940
2941static bool MayFoldIntoStore(SDValue Op) {
2942 return Op.hasOneUse() && ISD::isNormalStore(*Op.getNode()->use_begin());
2943}
2944
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002945static bool isTargetShuffle(unsigned Opcode) {
2946 switch(Opcode) {
2947 default: return false;
2948 case X86ISD::PSHUFD:
2949 case X86ISD::PSHUFHW:
2950 case X86ISD::PSHUFLW:
Craig Topperb3982da2011-12-31 23:50:21 +00002951 case X86ISD::SHUFP:
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00002952 case X86ISD::PALIGN:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002953 case X86ISD::MOVLHPS:
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00002954 case X86ISD::MOVLHPD:
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00002955 case X86ISD::MOVHLPS:
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00002956 case X86ISD::MOVLPS:
2957 case X86ISD::MOVLPD:
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00002958 case X86ISD::MOVSHDUP:
Bruno Cardoso Lopes013bb3d2010-08-31 22:35:05 +00002959 case X86ISD::MOVSLDUP:
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00002960 case X86ISD::MOVDDUP:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002961 case X86ISD::MOVSS:
2962 case X86ISD::MOVSD:
Craig Topper34671b82011-12-06 08:21:25 +00002963 case X86ISD::UNPCKL:
2964 case X86ISD::UNPCKH:
Craig Topper316cd2a2011-11-30 06:25:25 +00002965 case X86ISD::VPERMILP:
Craig Topperec24e612011-11-30 07:47:51 +00002966 case X86ISD::VPERM2X128:
Craig Topperbdcbcb32012-05-06 18:54:26 +00002967 case X86ISD::VPERMI:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002968 return true;
2969 }
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002970}
2971
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00002972static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
Craig Topper3d092db2012-03-21 02:14:01 +00002973 SDValue V1, SelectionDAG &DAG) {
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00002974 switch(Opc) {
2975 default: llvm_unreachable("Unknown x86 shuffle node");
2976 case X86ISD::MOVSHDUP:
Bruno Cardoso Lopes013bb3d2010-08-31 22:35:05 +00002977 case X86ISD::MOVSLDUP:
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00002978 case X86ISD::MOVDDUP:
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00002979 return DAG.getNode(Opc, dl, VT, V1);
2980 }
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00002981}
2982
2983static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
Craig Topper3d092db2012-03-21 02:14:01 +00002984 SDValue V1, unsigned TargetMask,
2985 SelectionDAG &DAG) {
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00002986 switch(Opc) {
2987 default: llvm_unreachable("Unknown x86 shuffle node");
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002988 case X86ISD::PSHUFD:
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00002989 case X86ISD::PSHUFHW:
2990 case X86ISD::PSHUFLW:
Craig Topper316cd2a2011-11-30 06:25:25 +00002991 case X86ISD::VPERMILP:
Craig Topper8325c112012-04-16 00:41:45 +00002992 case X86ISD::VPERMI:
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00002993 return DAG.getNode(Opc, dl, VT, V1, DAG.getConstant(TargetMask, MVT::i8));
2994 }
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00002995}
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00002996
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002997static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
Craig Topper3d092db2012-03-21 02:14:01 +00002998 SDValue V1, SDValue V2, unsigned TargetMask,
2999 SelectionDAG &DAG) {
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00003000 switch(Opc) {
3001 default: llvm_unreachable("Unknown x86 shuffle node");
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00003002 case X86ISD::PALIGN:
Craig Topperb3982da2011-12-31 23:50:21 +00003003 case X86ISD::SHUFP:
Craig Topperec24e612011-11-30 07:47:51 +00003004 case X86ISD::VPERM2X128:
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00003005 return DAG.getNode(Opc, dl, VT, V1, V2,
3006 DAG.getConstant(TargetMask, MVT::i8));
3007 }
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00003008}
3009
3010static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
3011 SDValue V1, SDValue V2, SelectionDAG &DAG) {
3012 switch(Opc) {
3013 default: llvm_unreachable("Unknown x86 shuffle node");
3014 case X86ISD::MOVLHPS:
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00003015 case X86ISD::MOVLHPD:
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00003016 case X86ISD::MOVHLPS:
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00003017 case X86ISD::MOVLPS:
3018 case X86ISD::MOVLPD:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003019 case X86ISD::MOVSS:
3020 case X86ISD::MOVSD:
Craig Topper34671b82011-12-06 08:21:25 +00003021 case X86ISD::UNPCKL:
3022 case X86ISD::UNPCKH:
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00003023 return DAG.getNode(Opc, dl, VT, V1, V2);
3024 }
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00003025}
3026
Dan Gohmand858e902010-04-17 15:26:15 +00003027SDValue X86TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) const {
Anton Korobeynikova2780e12007-08-15 17:12:32 +00003028 MachineFunction &MF = DAG.getMachineFunction();
3029 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
3030 int ReturnAddrIndex = FuncInfo->getRAIndex();
3031
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00003032 if (ReturnAddrIndex == 0) {
3033 // Set up a frame object for the return address.
Micah Villmow2c39b152012-10-15 16:24:29 +00003034 uint64_t SlotSize = TD->getPointerSize(0);
David Greene3f2bf852009-11-12 20:49:22 +00003035 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(SlotSize, -SlotSize,
Evan Chenged2ae132010-07-03 00:40:23 +00003036 false);
Anton Korobeynikova2780e12007-08-15 17:12:32 +00003037 FuncInfo->setRAIndex(ReturnAddrIndex);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00003038 }
3039
Evan Cheng25ab6902006-09-08 06:48:29 +00003040 return DAG.getFrameIndex(ReturnAddrIndex, getPointerTy());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00003041}
3042
3043
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00003044bool X86::isOffsetSuitableForCodeModel(int64_t Offset, CodeModel::Model M,
3045 bool hasSymbolicDisplacement) {
3046 // Offset should fit into 32 bit immediate field.
Benjamin Kramer34247a02010-03-29 21:13:41 +00003047 if (!isInt<32>(Offset))
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00003048 return false;
3049
3050 // If we don't have a symbolic displacement - we don't have any extra
3051 // restrictions.
3052 if (!hasSymbolicDisplacement)
3053 return true;
3054
3055 // FIXME: Some tweaks might be needed for medium code model.
3056 if (M != CodeModel::Small && M != CodeModel::Kernel)
3057 return false;
3058
3059 // For small code model we assume that latest object is 16MB before end of 31
3060 // bits boundary. We may also accept pretty large negative constants knowing
3061 // that all objects are in the positive half of address space.
3062 if (M == CodeModel::Small && Offset < 16*1024*1024)
3063 return true;
3064
3065 // For kernel code model we know that all object resist in the negative half
3066 // of 32bits address space. We may not accept negative offsets, since they may
3067 // be just off and we may accept pretty large positive ones.
3068 if (M == CodeModel::Kernel && Offset > 0)
3069 return true;
3070
3071 return false;
3072}
3073
Evan Chengef41ff62011-06-23 17:54:54 +00003074/// isCalleePop - Determines whether the callee is required to pop its
3075/// own arguments. Callee pop is necessary to support tail calls.
3076bool X86::isCalleePop(CallingConv::ID CallingConv,
3077 bool is64Bit, bool IsVarArg, bool TailCallOpt) {
3078 if (IsVarArg)
3079 return false;
3080
3081 switch (CallingConv) {
3082 default:
3083 return false;
3084 case CallingConv::X86_StdCall:
3085 return !is64Bit;
3086 case CallingConv::X86_FastCall:
3087 return !is64Bit;
3088 case CallingConv::X86_ThisCall:
3089 return !is64Bit;
3090 case CallingConv::Fast:
3091 return TailCallOpt;
3092 case CallingConv::GHC:
3093 return TailCallOpt;
3094 }
3095}
3096
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003097/// TranslateX86CC - do a one to one translation of a ISD::CondCode to the X86
3098/// specific condition code, returning the condition code and the LHS/RHS of the
3099/// comparison to make.
3100static unsigned TranslateX86CC(ISD::CondCode SetCCOpcode, bool isFP,
3101 SDValue &LHS, SDValue &RHS, SelectionDAG &DAG) {
Evan Chengd9558e02006-01-06 00:43:03 +00003102 if (!isFP) {
Chris Lattnerbfd68a72006-09-13 17:04:54 +00003103 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS)) {
3104 if (SetCCOpcode == ISD::SETGT && RHSC->isAllOnesValue()) {
3105 // X > -1 -> X == 0, jump !sign.
3106 RHS = DAG.getConstant(0, RHS.getValueType());
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003107 return X86::COND_NS;
Craig Topper69947b92012-04-23 06:57:04 +00003108 }
3109 if (SetCCOpcode == ISD::SETLT && RHSC->isNullValue()) {
Chris Lattnerbfd68a72006-09-13 17:04:54 +00003110 // X < 0 -> X == 0, jump on sign.
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003111 return X86::COND_S;
Craig Topper69947b92012-04-23 06:57:04 +00003112 }
3113 if (SetCCOpcode == ISD::SETLT && RHSC->getZExtValue() == 1) {
Dan Gohman5f6913c2007-09-17 14:49:27 +00003114 // X < 1 -> X <= 0
3115 RHS = DAG.getConstant(0, RHS.getValueType());
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003116 return X86::COND_LE;
Chris Lattnerbfd68a72006-09-13 17:04:54 +00003117 }
Chris Lattnerf9570512006-09-13 03:22:10 +00003118 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00003119
Evan Chengd9558e02006-01-06 00:43:03 +00003120 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00003121 default: llvm_unreachable("Invalid integer condition!");
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003122 case ISD::SETEQ: return X86::COND_E;
3123 case ISD::SETGT: return X86::COND_G;
3124 case ISD::SETGE: return X86::COND_GE;
3125 case ISD::SETLT: return X86::COND_L;
3126 case ISD::SETLE: return X86::COND_LE;
3127 case ISD::SETNE: return X86::COND_NE;
3128 case ISD::SETULT: return X86::COND_B;
3129 case ISD::SETUGT: return X86::COND_A;
3130 case ISD::SETULE: return X86::COND_BE;
3131 case ISD::SETUGE: return X86::COND_AE;
Evan Chengd9558e02006-01-06 00:43:03 +00003132 }
Chris Lattner4c78e022008-12-23 23:42:27 +00003133 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003134
Chris Lattner4c78e022008-12-23 23:42:27 +00003135 // First determine if it is required or is profitable to flip the operands.
Duncan Sands4047f4a2008-10-24 13:03:10 +00003136
Chris Lattner4c78e022008-12-23 23:42:27 +00003137 // If LHS is a foldable load, but RHS is not, flip the condition.
Rafael Espindolaf297c932011-02-03 03:58:05 +00003138 if (ISD::isNON_EXTLoad(LHS.getNode()) &&
3139 !ISD::isNON_EXTLoad(RHS.getNode())) {
Chris Lattner4c78e022008-12-23 23:42:27 +00003140 SetCCOpcode = getSetCCSwappedOperands(SetCCOpcode);
3141 std::swap(LHS, RHS);
Evan Cheng4d46d0a2008-08-28 23:48:31 +00003142 }
3143
Chris Lattner4c78e022008-12-23 23:42:27 +00003144 switch (SetCCOpcode) {
3145 default: break;
3146 case ISD::SETOLT:
3147 case ISD::SETOLE:
3148 case ISD::SETUGT:
3149 case ISD::SETUGE:
3150 std::swap(LHS, RHS);
3151 break;
3152 }
3153
3154 // On a floating point condition, the flags are set as follows:
3155 // ZF PF CF op
3156 // 0 | 0 | 0 | X > Y
3157 // 0 | 0 | 1 | X < Y
3158 // 1 | 0 | 0 | X == Y
3159 // 1 | 1 | 1 | unordered
3160 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00003161 default: llvm_unreachable("Condcode should be pre-legalized away");
Chris Lattner4c78e022008-12-23 23:42:27 +00003162 case ISD::SETUEQ:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003163 case ISD::SETEQ: return X86::COND_E;
Chris Lattner4c78e022008-12-23 23:42:27 +00003164 case ISD::SETOLT: // flipped
3165 case ISD::SETOGT:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003166 case ISD::SETGT: return X86::COND_A;
Chris Lattner4c78e022008-12-23 23:42:27 +00003167 case ISD::SETOLE: // flipped
3168 case ISD::SETOGE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003169 case ISD::SETGE: return X86::COND_AE;
Chris Lattner4c78e022008-12-23 23:42:27 +00003170 case ISD::SETUGT: // flipped
3171 case ISD::SETULT:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003172 case ISD::SETLT: return X86::COND_B;
Chris Lattner4c78e022008-12-23 23:42:27 +00003173 case ISD::SETUGE: // flipped
3174 case ISD::SETULE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003175 case ISD::SETLE: return X86::COND_BE;
Chris Lattner4c78e022008-12-23 23:42:27 +00003176 case ISD::SETONE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003177 case ISD::SETNE: return X86::COND_NE;
3178 case ISD::SETUO: return X86::COND_P;
3179 case ISD::SETO: return X86::COND_NP;
Dan Gohman1a492952009-10-20 16:22:37 +00003180 case ISD::SETOEQ:
3181 case ISD::SETUNE: return X86::COND_INVALID;
Chris Lattner4c78e022008-12-23 23:42:27 +00003182 }
Evan Chengd9558e02006-01-06 00:43:03 +00003183}
3184
Evan Cheng4a460802006-01-11 00:33:36 +00003185/// hasFPCMov - is there a floating point cmov for the specific X86 condition
3186/// code. Current x86 isa includes the following FP cmov instructions:
Evan Chengaaca22c2006-01-10 20:26:56 +00003187/// fcmovb, fcomvbe, fcomve, fcmovu, fcmovae, fcmova, fcmovne, fcmovnu.
Evan Cheng4a460802006-01-11 00:33:36 +00003188static bool hasFPCMov(unsigned X86CC) {
Evan Chengaaca22c2006-01-10 20:26:56 +00003189 switch (X86CC) {
3190 default:
3191 return false;
Chris Lattner7fbe9722006-10-20 17:42:20 +00003192 case X86::COND_B:
3193 case X86::COND_BE:
3194 case X86::COND_E:
3195 case X86::COND_P:
3196 case X86::COND_A:
3197 case X86::COND_AE:
3198 case X86::COND_NE:
3199 case X86::COND_NP:
Evan Chengaaca22c2006-01-10 20:26:56 +00003200 return true;
3201 }
3202}
3203
Evan Chengeb2f9692009-10-27 19:56:55 +00003204/// isFPImmLegal - Returns true if the target can instruction select the
3205/// specified FP immediate natively. If false, the legalizer will
3206/// materialize the FP immediate as a load from a constant pool.
Evan Chenga1eaa3c2009-10-28 01:43:28 +00003207bool X86TargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
Evan Chengeb2f9692009-10-27 19:56:55 +00003208 for (unsigned i = 0, e = LegalFPImmediates.size(); i != e; ++i) {
3209 if (Imm.bitwiseIsEqual(LegalFPImmediates[i]))
3210 return true;
3211 }
3212 return false;
3213}
3214
Nate Begeman9008ca62009-04-27 18:41:29 +00003215/// isUndefOrInRange - Return true if Val is undef or if its value falls within
3216/// the specified range (L, H].
3217static bool isUndefOrInRange(int Val, int Low, int Hi) {
3218 return (Val < 0) || (Val >= Low && Val < Hi);
3219}
3220
3221/// isUndefOrEqual - Val is either less than zero (undef) or equal to the
3222/// specified value.
3223static bool isUndefOrEqual(int Val, int CmpVal) {
3224 if (Val < 0 || Val == CmpVal)
Evan Cheng5ced1d82006-04-06 23:23:56 +00003225 return true;
Nate Begeman9008ca62009-04-27 18:41:29 +00003226 return false;
Evan Chengc5cdff22006-04-07 21:53:05 +00003227}
3228
Benjamin Kramerd9b0b022012-06-02 10:20:22 +00003229/// isSequentialOrUndefInRange - Return true if every element in Mask, beginning
Bruno Cardoso Lopes4002d7e2011-08-12 21:54:42 +00003230/// from position Pos and ending in Pos+Size, falls within the specified
3231/// sequential range (L, L+Pos]. or is undef.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003232static bool isSequentialOrUndefInRange(ArrayRef<int> Mask,
Craig Topperb6072642012-05-03 07:26:59 +00003233 unsigned Pos, unsigned Size, int Low) {
3234 for (unsigned i = Pos, e = Pos+Size; i != e; ++i, ++Low)
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003235 if (!isUndefOrEqual(Mask[i], Low))
3236 return false;
3237 return true;
3238}
3239
Nate Begeman9008ca62009-04-27 18:41:29 +00003240/// isPSHUFDMask - Return true if the node specifies a shuffle of elements that
3241/// is suitable for input to PSHUFD or PSHUFW. That is, it doesn't reference
3242/// the second operand.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003243static bool isPSHUFDMask(ArrayRef<int> Mask, EVT VT) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00003244 if (VT == MVT::v4f32 || VT == MVT::v4i32 )
Nate Begeman9008ca62009-04-27 18:41:29 +00003245 return (Mask[0] < 4 && Mask[1] < 4 && Mask[2] < 4 && Mask[3] < 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00003246 if (VT == MVT::v2f64 || VT == MVT::v2i64)
Nate Begeman9008ca62009-04-27 18:41:29 +00003247 return (Mask[0] < 2 && Mask[1] < 2);
3248 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003249}
3250
Nate Begeman9008ca62009-04-27 18:41:29 +00003251/// isPSHUFHWMask - Return true if the node specifies a shuffle of elements that
3252/// is suitable for input to PSHUFHW.
Craig Toppera9a568a2012-05-02 08:03:44 +00003253static bool isPSHUFHWMask(ArrayRef<int> Mask, EVT VT, bool HasAVX2) {
3254 if (VT != MVT::v8i16 && (!HasAVX2 || VT != MVT::v16i16))
Evan Cheng0188ecb2006-03-22 18:59:22 +00003255 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003256
Nate Begeman9008ca62009-04-27 18:41:29 +00003257 // Lower quadword copied in order or undef.
Craig Topperc612d792012-01-02 09:17:37 +00003258 if (!isSequentialOrUndefInRange(Mask, 0, 4, 0))
3259 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003260
Evan Cheng506d3df2006-03-29 23:07:14 +00003261 // Upper quadword shuffled.
Craig Topperc612d792012-01-02 09:17:37 +00003262 for (unsigned i = 4; i != 8; ++i)
Craig Toppera9a568a2012-05-02 08:03:44 +00003263 if (!isUndefOrInRange(Mask[i], 4, 8))
Evan Cheng506d3df2006-03-29 23:07:14 +00003264 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003265
Craig Toppera9a568a2012-05-02 08:03:44 +00003266 if (VT == MVT::v16i16) {
3267 // Lower quadword copied in order or undef.
3268 if (!isSequentialOrUndefInRange(Mask, 8, 4, 8))
3269 return false;
3270
3271 // Upper quadword shuffled.
3272 for (unsigned i = 12; i != 16; ++i)
3273 if (!isUndefOrInRange(Mask[i], 12, 16))
3274 return false;
3275 }
3276
Evan Cheng506d3df2006-03-29 23:07:14 +00003277 return true;
3278}
3279
Nate Begeman9008ca62009-04-27 18:41:29 +00003280/// isPSHUFLWMask - Return true if the node specifies a shuffle of elements that
3281/// is suitable for input to PSHUFLW.
Craig Toppera9a568a2012-05-02 08:03:44 +00003282static bool isPSHUFLWMask(ArrayRef<int> Mask, EVT VT, bool HasAVX2) {
3283 if (VT != MVT::v8i16 && (!HasAVX2 || VT != MVT::v16i16))
Evan Cheng506d3df2006-03-29 23:07:14 +00003284 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003285
Rafael Espindola15684b22009-04-24 12:40:33 +00003286 // Upper quadword copied in order.
Craig Topperc612d792012-01-02 09:17:37 +00003287 if (!isSequentialOrUndefInRange(Mask, 4, 4, 4))
3288 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003289
Rafael Espindola15684b22009-04-24 12:40:33 +00003290 // Lower quadword shuffled.
Craig Topperc612d792012-01-02 09:17:37 +00003291 for (unsigned i = 0; i != 4; ++i)
Craig Toppera9a568a2012-05-02 08:03:44 +00003292 if (!isUndefOrInRange(Mask[i], 0, 4))
Rafael Espindola15684b22009-04-24 12:40:33 +00003293 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003294
Craig Toppera9a568a2012-05-02 08:03:44 +00003295 if (VT == MVT::v16i16) {
3296 // Upper quadword copied in order.
3297 if (!isSequentialOrUndefInRange(Mask, 12, 4, 12))
3298 return false;
3299
3300 // Lower quadword shuffled.
3301 for (unsigned i = 8; i != 12; ++i)
3302 if (!isUndefOrInRange(Mask[i], 8, 12))
3303 return false;
3304 }
3305
Rafael Espindola15684b22009-04-24 12:40:33 +00003306 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00003307}
3308
Nate Begemana09008b2009-10-19 02:17:23 +00003309/// isPALIGNRMask - Return true if the node specifies a shuffle of elements that
3310/// is suitable for input to PALIGNR.
Craig Topper0e2037b2012-01-20 05:53:00 +00003311static bool isPALIGNRMask(ArrayRef<int> Mask, EVT VT,
3312 const X86Subtarget *Subtarget) {
3313 if ((VT.getSizeInBits() == 128 && !Subtarget->hasSSSE3()) ||
3314 (VT.getSizeInBits() == 256 && !Subtarget->hasAVX2()))
Bruno Cardoso Lopes9065d4b2011-07-29 01:30:59 +00003315 return false;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003316
Craig Topper0e2037b2012-01-20 05:53:00 +00003317 unsigned NumElts = VT.getVectorNumElements();
3318 unsigned NumLanes = VT.getSizeInBits()/128;
3319 unsigned NumLaneElts = NumElts/NumLanes;
3320
3321 // Do not handle 64-bit element shuffles with palignr.
3322 if (NumLaneElts == 2)
Nate Begemana09008b2009-10-19 02:17:23 +00003323 return false;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003324
Craig Topper0e2037b2012-01-20 05:53:00 +00003325 for (unsigned l = 0; l != NumElts; l+=NumLaneElts) {
3326 unsigned i;
3327 for (i = 0; i != NumLaneElts; ++i) {
3328 if (Mask[i+l] >= 0)
3329 break;
3330 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00003331
Craig Topper0e2037b2012-01-20 05:53:00 +00003332 // Lane is all undef, go to next lane
3333 if (i == NumLaneElts)
3334 continue;
Nate Begemana09008b2009-10-19 02:17:23 +00003335
Craig Topper0e2037b2012-01-20 05:53:00 +00003336 int Start = Mask[i+l];
Nate Begemana09008b2009-10-19 02:17:23 +00003337
Craig Topper0e2037b2012-01-20 05:53:00 +00003338 // Make sure its in this lane in one of the sources
3339 if (!isUndefOrInRange(Start, l, l+NumLaneElts) &&
3340 !isUndefOrInRange(Start, l+NumElts, l+NumElts+NumLaneElts))
Nate Begemana09008b2009-10-19 02:17:23 +00003341 return false;
Craig Topper0e2037b2012-01-20 05:53:00 +00003342
3343 // If not lane 0, then we must match lane 0
3344 if (l != 0 && Mask[i] >= 0 && !isUndefOrEqual(Start, Mask[i]+l))
3345 return false;
3346
3347 // Correct second source to be contiguous with first source
3348 if (Start >= (int)NumElts)
3349 Start -= NumElts - NumLaneElts;
3350
3351 // Make sure we're shifting in the right direction.
3352 if (Start <= (int)(i+l))
3353 return false;
3354
3355 Start -= i;
3356
3357 // Check the rest of the elements to see if they are consecutive.
3358 for (++i; i != NumLaneElts; ++i) {
3359 int Idx = Mask[i+l];
3360
3361 // Make sure its in this lane
3362 if (!isUndefOrInRange(Idx, l, l+NumLaneElts) &&
3363 !isUndefOrInRange(Idx, l+NumElts, l+NumElts+NumLaneElts))
3364 return false;
3365
3366 // If not lane 0, then we must match lane 0
3367 if (l != 0 && Mask[i] >= 0 && !isUndefOrEqual(Idx, Mask[i]+l))
3368 return false;
3369
3370 if (Idx >= (int)NumElts)
3371 Idx -= NumElts - NumLaneElts;
3372
3373 if (!isUndefOrEqual(Idx, Start+i))
3374 return false;
3375
3376 }
Nate Begemana09008b2009-10-19 02:17:23 +00003377 }
Craig Topper0e2037b2012-01-20 05:53:00 +00003378
Nate Begemana09008b2009-10-19 02:17:23 +00003379 return true;
3380}
3381
Craig Topper1a7700a2012-01-19 08:19:12 +00003382/// CommuteVectorShuffleMask - Change values in a shuffle permute mask assuming
3383/// the two vector operands have swapped position.
3384static void CommuteVectorShuffleMask(SmallVectorImpl<int> &Mask,
3385 unsigned NumElems) {
3386 for (unsigned i = 0; i != NumElems; ++i) {
3387 int idx = Mask[i];
3388 if (idx < 0)
3389 continue;
3390 else if (idx < (int)NumElems)
3391 Mask[i] = idx + NumElems;
3392 else
3393 Mask[i] = idx - NumElems;
3394 }
3395}
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00003396
Craig Topper1a7700a2012-01-19 08:19:12 +00003397/// isSHUFPMask - Return true if the specified VECTOR_SHUFFLE operand
3398/// specifies a shuffle of elements that is suitable for input to 128/256-bit
3399/// SHUFPS and SHUFPD. If Commuted is true, then it checks for sources to be
3400/// reverse of what x86 shuffles want.
3401static bool isSHUFPMask(ArrayRef<int> Mask, EVT VT, bool HasAVX,
3402 bool Commuted = false) {
3403 if (!HasAVX && VT.getSizeInBits() == 256)
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00003404 return false;
3405
Craig Topper1a7700a2012-01-19 08:19:12 +00003406 unsigned NumElems = VT.getVectorNumElements();
3407 unsigned NumLanes = VT.getSizeInBits()/128;
3408 unsigned NumLaneElems = NumElems/NumLanes;
3409
3410 if (NumLaneElems != 2 && NumLaneElems != 4)
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00003411 return false;
3412
3413 // VSHUFPSY divides the resulting vector into 4 chunks.
3414 // The sources are also splitted into 4 chunks, and each destination
3415 // chunk must come from a different source chunk.
3416 //
3417 // SRC1 => X7 X6 X5 X4 X3 X2 X1 X0
3418 // SRC2 => Y7 Y6 Y5 Y4 Y3 Y2 Y1 Y9
3419 //
3420 // DST => Y7..Y4, Y7..Y4, X7..X4, X7..X4,
3421 // Y3..Y0, Y3..Y0, X3..X0, X3..X0
3422 //
Craig Topper9d7025b2011-11-27 21:41:12 +00003423 // VSHUFPDY divides the resulting vector into 4 chunks.
3424 // The sources are also splitted into 4 chunks, and each destination
3425 // chunk must come from a different source chunk.
3426 //
3427 // SRC1 => X3 X2 X1 X0
3428 // SRC2 => Y3 Y2 Y1 Y0
3429 //
3430 // DST => Y3..Y2, X3..X2, Y1..Y0, X1..X0
3431 //
Craig Topper1a7700a2012-01-19 08:19:12 +00003432 unsigned HalfLaneElems = NumLaneElems/2;
3433 for (unsigned l = 0; l != NumElems; l += NumLaneElems) {
3434 for (unsigned i = 0; i != NumLaneElems; ++i) {
3435 int Idx = Mask[i+l];
3436 unsigned RngStart = l + ((Commuted == (i<HalfLaneElems)) ? NumElems : 0);
3437 if (!isUndefOrInRange(Idx, RngStart, RngStart+NumLaneElems))
3438 return false;
3439 // For VSHUFPSY, the mask of the second half must be the same as the
3440 // first but with the appropriate offsets. This works in the same way as
3441 // VPERMILPS works with masks.
3442 if (NumElems != 8 || l == 0 || Mask[i] < 0)
3443 continue;
3444 if (!isUndefOrEqual(Idx, Mask[i]+l))
3445 return false;
Craig Topper1ff73d72011-12-06 04:59:07 +00003446 }
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00003447 }
3448
3449 return true;
3450}
3451
Evan Cheng2c0dbd02006-03-24 02:58:06 +00003452/// isMOVHLPSMask - Return true if the specified VECTOR_SHUFFLE operand
3453/// specifies a shuffle of elements that is suitable for input to MOVHLPS.
Craig Topperdd637ae2012-02-19 05:41:45 +00003454static bool isMOVHLPSMask(ArrayRef<int> Mask, EVT VT) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00003455 if (!VT.is128BitVector())
Bruno Cardoso Lopes61260052011-07-29 02:05:28 +00003456 return false;
3457
Craig Topper7a9a28b2012-08-12 02:23:29 +00003458 unsigned NumElems = VT.getVectorNumElements();
3459
Bruno Cardoso Lopes61260052011-07-29 02:05:28 +00003460 if (NumElems != 4)
Evan Cheng2c0dbd02006-03-24 02:58:06 +00003461 return false;
3462
Evan Cheng2064a2b2006-03-28 06:50:32 +00003463 // Expect bit0 == 6, bit1 == 7, bit2 == 2, bit3 == 3
Craig Topperdd637ae2012-02-19 05:41:45 +00003464 return isUndefOrEqual(Mask[0], 6) &&
3465 isUndefOrEqual(Mask[1], 7) &&
3466 isUndefOrEqual(Mask[2], 2) &&
3467 isUndefOrEqual(Mask[3], 3);
Evan Cheng6e56e2c2006-11-07 22:14:24 +00003468}
3469
Nate Begeman0b10b912009-11-07 23:17:15 +00003470/// isMOVHLPS_v_undef_Mask - Special case of isMOVHLPSMask for canonical form
3471/// of vector_shuffle v, v, <2, 3, 2, 3>, i.e. vector_shuffle v, undef,
3472/// <2, 3, 2, 3>
Craig Topperdd637ae2012-02-19 05:41:45 +00003473static bool isMOVHLPS_v_undef_Mask(ArrayRef<int> Mask, EVT VT) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00003474 if (!VT.is128BitVector())
Bruno Cardoso Lopes61260052011-07-29 02:05:28 +00003475 return false;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003476
Craig Topper7a9a28b2012-08-12 02:23:29 +00003477 unsigned NumElems = VT.getVectorNumElements();
3478
Nate Begeman0b10b912009-11-07 23:17:15 +00003479 if (NumElems != 4)
3480 return false;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003481
Craig Topperdd637ae2012-02-19 05:41:45 +00003482 return isUndefOrEqual(Mask[0], 2) &&
3483 isUndefOrEqual(Mask[1], 3) &&
3484 isUndefOrEqual(Mask[2], 2) &&
3485 isUndefOrEqual(Mask[3], 3);
Nate Begeman0b10b912009-11-07 23:17:15 +00003486}
3487
Evan Cheng5ced1d82006-04-06 23:23:56 +00003488/// isMOVLPMask - Return true if the specified VECTOR_SHUFFLE operand
3489/// specifies a shuffle of elements that is suitable for input to MOVLP{S|D}.
Craig Topperdd637ae2012-02-19 05:41:45 +00003490static bool isMOVLPMask(ArrayRef<int> Mask, EVT VT) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00003491 if (!VT.is128BitVector())
Elena Demikhovsky021c0a22011-12-28 08:14:01 +00003492 return false;
3493
Craig Topperdd637ae2012-02-19 05:41:45 +00003494 unsigned NumElems = VT.getVectorNumElements();
Evan Cheng5ced1d82006-04-06 23:23:56 +00003495
Evan Cheng5ced1d82006-04-06 23:23:56 +00003496 if (NumElems != 2 && NumElems != 4)
3497 return false;
3498
Chad Rosier238ae312012-04-30 17:47:15 +00003499 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
Craig Topperdd637ae2012-02-19 05:41:45 +00003500 if (!isUndefOrEqual(Mask[i], i + NumElems))
Evan Chengc5cdff22006-04-07 21:53:05 +00003501 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003502
Chad Rosier238ae312012-04-30 17:47:15 +00003503 for (unsigned i = NumElems/2, e = NumElems; i != e; ++i)
Craig Topperdd637ae2012-02-19 05:41:45 +00003504 if (!isUndefOrEqual(Mask[i], i))
Evan Chengc5cdff22006-04-07 21:53:05 +00003505 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003506
3507 return true;
3508}
3509
Nate Begeman0b10b912009-11-07 23:17:15 +00003510/// isMOVLHPSMask - Return true if the specified VECTOR_SHUFFLE operand
3511/// specifies a shuffle of elements that is suitable for input to MOVLHPS.
Craig Topperdd637ae2012-02-19 05:41:45 +00003512static bool isMOVLHPSMask(ArrayRef<int> Mask, EVT VT) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00003513 if (!VT.is128BitVector())
3514 return false;
3515
Craig Topperdd637ae2012-02-19 05:41:45 +00003516 unsigned NumElems = VT.getVectorNumElements();
Evan Cheng5ced1d82006-04-06 23:23:56 +00003517
Craig Topper7a9a28b2012-08-12 02:23:29 +00003518 if (NumElems != 2 && NumElems != 4)
Evan Cheng5ced1d82006-04-06 23:23:56 +00003519 return false;
3520
Chad Rosier238ae312012-04-30 17:47:15 +00003521 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
Craig Topperdd637ae2012-02-19 05:41:45 +00003522 if (!isUndefOrEqual(Mask[i], i))
Evan Chengc5cdff22006-04-07 21:53:05 +00003523 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003524
Chad Rosier238ae312012-04-30 17:47:15 +00003525 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
3526 if (!isUndefOrEqual(Mask[i + e], i + NumElems))
Evan Chengc5cdff22006-04-07 21:53:05 +00003527 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003528
3529 return true;
3530}
3531
Elena Demikhovsky15963732012-06-26 08:04:10 +00003532//
3533// Some special combinations that can be optimized.
3534//
3535static
3536SDValue Compact8x32ShuffleNode(ShuffleVectorSDNode *SVOp,
3537 SelectionDAG &DAG) {
3538 EVT VT = SVOp->getValueType(0);
Elena Demikhovsky15963732012-06-26 08:04:10 +00003539 DebugLoc dl = SVOp->getDebugLoc();
3540
3541 if (VT != MVT::v8i32 && VT != MVT::v8f32)
3542 return SDValue();
3543
3544 ArrayRef<int> Mask = SVOp->getMask();
3545
3546 // These are the special masks that may be optimized.
3547 static const int MaskToOptimizeEven[] = {0, 8, 2, 10, 4, 12, 6, 14};
3548 static const int MaskToOptimizeOdd[] = {1, 9, 3, 11, 5, 13, 7, 15};
3549 bool MatchEvenMask = true;
3550 bool MatchOddMask = true;
3551 for (int i=0; i<8; ++i) {
3552 if (!isUndefOrEqual(Mask[i], MaskToOptimizeEven[i]))
3553 MatchEvenMask = false;
3554 if (!isUndefOrEqual(Mask[i], MaskToOptimizeOdd[i]))
3555 MatchOddMask = false;
3556 }
Elena Demikhovsky15963732012-06-26 08:04:10 +00003557
Elena Demikhovsky32510202012-09-04 12:49:02 +00003558 if (!MatchEvenMask && !MatchOddMask)
Elena Demikhovsky15963732012-06-26 08:04:10 +00003559 return SDValue();
Michael Liao471b9172012-10-03 23:43:52 +00003560
Elena Demikhovsky15963732012-06-26 08:04:10 +00003561 SDValue UndefNode = DAG.getNode(ISD::UNDEF, dl, VT);
3562
Elena Demikhovsky32510202012-09-04 12:49:02 +00003563 SDValue Op0 = SVOp->getOperand(0);
3564 SDValue Op1 = SVOp->getOperand(1);
3565
3566 if (MatchEvenMask) {
3567 // Shift the second operand right to 32 bits.
3568 static const int ShiftRightMask[] = {-1, 0, -1, 2, -1, 4, -1, 6 };
3569 Op1 = DAG.getVectorShuffle(VT, dl, Op1, UndefNode, ShiftRightMask);
3570 } else {
3571 // Shift the first operand left to 32 bits.
3572 static const int ShiftLeftMask[] = {1, -1, 3, -1, 5, -1, 7, -1 };
3573 Op0 = DAG.getVectorShuffle(VT, dl, Op0, UndefNode, ShiftLeftMask);
3574 }
3575 static const int BlendMask[] = {0, 9, 2, 11, 4, 13, 6, 15};
3576 return DAG.getVectorShuffle(VT, dl, Op0, Op1, BlendMask);
Elena Demikhovsky15963732012-06-26 08:04:10 +00003577}
3578
Evan Cheng0038e592006-03-28 00:39:58 +00003579/// isUNPCKLMask - Return true if the specified VECTOR_SHUFFLE operand
3580/// specifies a shuffle of elements that is suitable for input to UNPCKL.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003581static bool isUNPCKLMask(ArrayRef<int> Mask, EVT VT,
Craig Topper6347e862011-11-21 06:57:39 +00003582 bool HasAVX2, bool V2IsSplat = false) {
Craig Topper94438ba2011-12-16 08:06:31 +00003583 unsigned NumElts = VT.getVectorNumElements();
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003584
3585 assert((VT.is128BitVector() || VT.is256BitVector()) &&
3586 "Unsupported vector type for unpckh");
3587
Craig Topper6347e862011-11-21 06:57:39 +00003588 if (VT.getSizeInBits() == 256 && NumElts != 4 && NumElts != 8 &&
Craig Topper6fa583d2011-11-21 08:26:50 +00003589 (!HasAVX2 || (NumElts != 16 && NumElts != 32)))
Evan Cheng0038e592006-03-28 00:39:58 +00003590 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003591
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003592 // Handle 128 and 256-bit vector lengths. AVX defines UNPCK* to operate
3593 // independently on 128-bit lanes.
3594 unsigned NumLanes = VT.getSizeInBits()/128;
3595 unsigned NumLaneElts = NumElts/NumLanes;
David Greenea20244d2011-03-02 17:23:43 +00003596
Craig Topper94438ba2011-12-16 08:06:31 +00003597 for (unsigned l = 0; l != NumLanes; ++l) {
3598 for (unsigned i = l*NumLaneElts, j = l*NumLaneElts;
3599 i != (l+1)*NumLaneElts;
David Greenea20244d2011-03-02 17:23:43 +00003600 i += 2, ++j) {
3601 int BitI = Mask[i];
3602 int BitI1 = Mask[i+1];
3603 if (!isUndefOrEqual(BitI, j))
Evan Cheng39623da2006-04-20 08:58:49 +00003604 return false;
David Greenea20244d2011-03-02 17:23:43 +00003605 if (V2IsSplat) {
3606 if (!isUndefOrEqual(BitI1, NumElts))
3607 return false;
3608 } else {
3609 if (!isUndefOrEqual(BitI1, j + NumElts))
3610 return false;
3611 }
Evan Cheng39623da2006-04-20 08:58:49 +00003612 }
Evan Cheng0038e592006-03-28 00:39:58 +00003613 }
David Greenea20244d2011-03-02 17:23:43 +00003614
Evan Cheng0038e592006-03-28 00:39:58 +00003615 return true;
3616}
3617
Evan Cheng4fcb9222006-03-28 02:43:26 +00003618/// isUNPCKHMask - Return true if the specified VECTOR_SHUFFLE operand
3619/// specifies a shuffle of elements that is suitable for input to UNPCKH.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003620static bool isUNPCKHMask(ArrayRef<int> Mask, EVT VT,
Craig Topper6347e862011-11-21 06:57:39 +00003621 bool HasAVX2, bool V2IsSplat = false) {
Craig Topper94438ba2011-12-16 08:06:31 +00003622 unsigned NumElts = VT.getVectorNumElements();
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003623
3624 assert((VT.is128BitVector() || VT.is256BitVector()) &&
3625 "Unsupported vector type for unpckh");
3626
Craig Topper6347e862011-11-21 06:57:39 +00003627 if (VT.getSizeInBits() == 256 && NumElts != 4 && NumElts != 8 &&
Craig Topper6fa583d2011-11-21 08:26:50 +00003628 (!HasAVX2 || (NumElts != 16 && NumElts != 32)))
Evan Cheng4fcb9222006-03-28 02:43:26 +00003629 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003630
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003631 // Handle 128 and 256-bit vector lengths. AVX defines UNPCK* to operate
3632 // independently on 128-bit lanes.
3633 unsigned NumLanes = VT.getSizeInBits()/128;
3634 unsigned NumLaneElts = NumElts/NumLanes;
3635
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003636 for (unsigned l = 0; l != NumLanes; ++l) {
Craig Topper94438ba2011-12-16 08:06:31 +00003637 for (unsigned i = l*NumLaneElts, j = (l*NumLaneElts)+NumLaneElts/2;
3638 i != (l+1)*NumLaneElts; i += 2, ++j) {
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003639 int BitI = Mask[i];
3640 int BitI1 = Mask[i+1];
3641 if (!isUndefOrEqual(BitI, j))
Evan Cheng39623da2006-04-20 08:58:49 +00003642 return false;
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003643 if (V2IsSplat) {
3644 if (isUndefOrEqual(BitI1, NumElts))
3645 return false;
3646 } else {
3647 if (!isUndefOrEqual(BitI1, j+NumElts))
3648 return false;
3649 }
Evan Cheng39623da2006-04-20 08:58:49 +00003650 }
Evan Cheng4fcb9222006-03-28 02:43:26 +00003651 }
Evan Cheng4fcb9222006-03-28 02:43:26 +00003652 return true;
3653}
3654
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003655/// isUNPCKL_v_undef_Mask - Special case of isUNPCKLMask for canonical form
3656/// of vector_shuffle v, v, <0, 4, 1, 5>, i.e. vector_shuffle v, undef,
3657/// <0, 0, 1, 1>
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003658static bool isUNPCKL_v_undef_Mask(ArrayRef<int> Mask, EVT VT,
Craig Topper94438ba2011-12-16 08:06:31 +00003659 bool HasAVX2) {
3660 unsigned NumElts = VT.getVectorNumElements();
3661
3662 assert((VT.is128BitVector() || VT.is256BitVector()) &&
3663 "Unsupported vector type for unpckh");
3664
3665 if (VT.getSizeInBits() == 256 && NumElts != 4 && NumElts != 8 &&
3666 (!HasAVX2 || (NumElts != 16 && NumElts != 32)))
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003667 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003668
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00003669 // For 256-bit i64/f64, use MOVDDUPY instead, so reject the matching pattern
3670 // FIXME: Need a better way to get rid of this, there's no latency difference
3671 // between UNPCKLPD and MOVDDUP, the later should always be checked first and
3672 // the former later. We should also remove the "_undef" special mask.
Craig Topper94438ba2011-12-16 08:06:31 +00003673 if (NumElts == 4 && VT.getSizeInBits() == 256)
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00003674 return false;
3675
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003676 // Handle 128 and 256-bit vector lengths. AVX defines UNPCK* to operate
3677 // independently on 128-bit lanes.
Craig Topper94438ba2011-12-16 08:06:31 +00003678 unsigned NumLanes = VT.getSizeInBits()/128;
3679 unsigned NumLaneElts = NumElts/NumLanes;
David Greenea20244d2011-03-02 17:23:43 +00003680
Craig Topper94438ba2011-12-16 08:06:31 +00003681 for (unsigned l = 0; l != NumLanes; ++l) {
3682 for (unsigned i = l*NumLaneElts, j = l*NumLaneElts;
3683 i != (l+1)*NumLaneElts;
David Greenea20244d2011-03-02 17:23:43 +00003684 i += 2, ++j) {
3685 int BitI = Mask[i];
3686 int BitI1 = Mask[i+1];
3687
3688 if (!isUndefOrEqual(BitI, j))
3689 return false;
3690 if (!isUndefOrEqual(BitI1, j))
3691 return false;
3692 }
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003693 }
David Greenea20244d2011-03-02 17:23:43 +00003694
Rafael Espindola15684b22009-04-24 12:40:33 +00003695 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00003696}
3697
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003698/// isUNPCKH_v_undef_Mask - Special case of isUNPCKHMask for canonical form
3699/// of vector_shuffle v, v, <2, 6, 3, 7>, i.e. vector_shuffle v, undef,
3700/// <2, 2, 3, 3>
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003701static bool isUNPCKH_v_undef_Mask(ArrayRef<int> Mask, EVT VT, bool HasAVX2) {
Craig Topper94438ba2011-12-16 08:06:31 +00003702 unsigned NumElts = VT.getVectorNumElements();
3703
3704 assert((VT.is128BitVector() || VT.is256BitVector()) &&
3705 "Unsupported vector type for unpckh");
3706
3707 if (VT.getSizeInBits() == 256 && NumElts != 4 && NumElts != 8 &&
3708 (!HasAVX2 || (NumElts != 16 && NumElts != 32)))
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003709 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003710
Craig Topper94438ba2011-12-16 08:06:31 +00003711 // Handle 128 and 256-bit vector lengths. AVX defines UNPCK* to operate
3712 // independently on 128-bit lanes.
3713 unsigned NumLanes = VT.getSizeInBits()/128;
3714 unsigned NumLaneElts = NumElts/NumLanes;
3715
3716 for (unsigned l = 0; l != NumLanes; ++l) {
3717 for (unsigned i = l*NumLaneElts, j = (l*NumLaneElts)+NumLaneElts/2;
3718 i != (l+1)*NumLaneElts; i += 2, ++j) {
3719 int BitI = Mask[i];
3720 int BitI1 = Mask[i+1];
3721 if (!isUndefOrEqual(BitI, j))
3722 return false;
3723 if (!isUndefOrEqual(BitI1, j))
3724 return false;
3725 }
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003726 }
Rafael Espindola15684b22009-04-24 12:40:33 +00003727 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00003728}
3729
Evan Cheng017dcc62006-04-21 01:05:10 +00003730/// isMOVLMask - Return true if the specified VECTOR_SHUFFLE operand
3731/// specifies a shuffle of elements that is suitable for input to MOVSS,
3732/// MOVSD, and MOVD, i.e. setting the lowest element.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003733static bool isMOVLMask(ArrayRef<int> Mask, EVT VT) {
Eli Friedman10415532009-06-06 06:05:10 +00003734 if (VT.getVectorElementType().getSizeInBits() < 32)
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003735 return false;
Craig Topper7a9a28b2012-08-12 02:23:29 +00003736 if (!VT.is128BitVector())
Elena Demikhovsky021c0a22011-12-28 08:14:01 +00003737 return false;
Eli Friedman10415532009-06-06 06:05:10 +00003738
Craig Topperc612d792012-01-02 09:17:37 +00003739 unsigned NumElts = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00003740
Nate Begeman9008ca62009-04-27 18:41:29 +00003741 if (!isUndefOrEqual(Mask[0], NumElts))
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003742 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003743
Craig Topperc612d792012-01-02 09:17:37 +00003744 for (unsigned i = 1; i != NumElts; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003745 if (!isUndefOrEqual(Mask[i], i))
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003746 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003747
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003748 return true;
3749}
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003750
Craig Topper70b883b2011-11-28 10:14:51 +00003751/// isVPERM2X128Mask - Match 256-bit shuffles where the elements are considered
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003752/// as permutations between 128-bit chunks or halves. As an example: this
3753/// shuffle bellow:
3754/// vector_shuffle <4, 5, 6, 7, 12, 13, 14, 15>
3755/// The first half comes from the second half of V1 and the second half from the
3756/// the second half of V2.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003757static bool isVPERM2X128Mask(ArrayRef<int> Mask, EVT VT, bool HasAVX) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00003758 if (!HasAVX || !VT.is256BitVector())
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003759 return false;
3760
3761 // The shuffle result is divided into half A and half B. In total the two
3762 // sources have 4 halves, namely: C, D, E, F. The final values of A and
3763 // B must come from C, D, E or F.
Craig Topperc612d792012-01-02 09:17:37 +00003764 unsigned HalfSize = VT.getVectorNumElements()/2;
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003765 bool MatchA = false, MatchB = false;
3766
3767 // Check if A comes from one of C, D, E, F.
Craig Topperc612d792012-01-02 09:17:37 +00003768 for (unsigned Half = 0; Half != 4; ++Half) {
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003769 if (isSequentialOrUndefInRange(Mask, 0, HalfSize, Half*HalfSize)) {
3770 MatchA = true;
3771 break;
3772 }
3773 }
3774
3775 // Check if B comes from one of C, D, E, F.
Craig Topperc612d792012-01-02 09:17:37 +00003776 for (unsigned Half = 0; Half != 4; ++Half) {
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003777 if (isSequentialOrUndefInRange(Mask, HalfSize, HalfSize, Half*HalfSize)) {
3778 MatchB = true;
3779 break;
3780 }
3781 }
3782
3783 return MatchA && MatchB;
3784}
3785
Craig Topper70b883b2011-11-28 10:14:51 +00003786/// getShuffleVPERM2X128Immediate - Return the appropriate immediate to shuffle
3787/// the specified VECTOR_MASK mask with VPERM2F128/VPERM2I128 instructions.
Craig Topperd93e4c32011-12-11 19:12:35 +00003788static unsigned getShuffleVPERM2X128Immediate(ShuffleVectorSDNode *SVOp) {
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003789 EVT VT = SVOp->getValueType(0);
3790
Craig Topperc612d792012-01-02 09:17:37 +00003791 unsigned HalfSize = VT.getVectorNumElements()/2;
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003792
Craig Topperc612d792012-01-02 09:17:37 +00003793 unsigned FstHalf = 0, SndHalf = 0;
3794 for (unsigned i = 0; i < HalfSize; ++i) {
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003795 if (SVOp->getMaskElt(i) > 0) {
3796 FstHalf = SVOp->getMaskElt(i)/HalfSize;
3797 break;
3798 }
3799 }
Craig Topperc612d792012-01-02 09:17:37 +00003800 for (unsigned i = HalfSize; i < HalfSize*2; ++i) {
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003801 if (SVOp->getMaskElt(i) > 0) {
3802 SndHalf = SVOp->getMaskElt(i)/HalfSize;
3803 break;
3804 }
3805 }
3806
3807 return (FstHalf | (SndHalf << 4));
3808}
3809
Craig Topper70b883b2011-11-28 10:14:51 +00003810/// isVPERMILPMask - Return true if the specified VECTOR_SHUFFLE operand
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00003811/// specifies a shuffle of elements that is suitable for input to VPERMILPD*.
3812/// Note that VPERMIL mask matching is different depending whether theunderlying
3813/// type is 32 or 64. In the VPERMILPS the high half of the mask should point
3814/// to the same elements of the low, but to the higher half of the source.
3815/// In VPERMILPD the two lanes could be shuffled independently of each other
Craig Topperdbd98a42012-02-07 06:28:42 +00003816/// with the same restriction that lanes can't be crossed. Also handles PSHUFDY.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003817static bool isVPERMILPMask(ArrayRef<int> Mask, EVT VT, bool HasAVX) {
Craig Topper70b883b2011-11-28 10:14:51 +00003818 if (!HasAVX)
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00003819 return false;
3820
Craig Topperc612d792012-01-02 09:17:37 +00003821 unsigned NumElts = VT.getVectorNumElements();
Craig Topper70b883b2011-11-28 10:14:51 +00003822 // Only match 256-bit with 32/64-bit types
3823 if (VT.getSizeInBits() != 256 || (NumElts != 4 && NumElts != 8))
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00003824 return false;
3825
Craig Topperc612d792012-01-02 09:17:37 +00003826 unsigned NumLanes = VT.getSizeInBits()/128;
3827 unsigned LaneSize = NumElts/NumLanes;
Craig Topper1a7700a2012-01-19 08:19:12 +00003828 for (unsigned l = 0; l != NumElts; l += LaneSize) {
Craig Topperc612d792012-01-02 09:17:37 +00003829 for (unsigned i = 0; i != LaneSize; ++i) {
Craig Topper1a7700a2012-01-19 08:19:12 +00003830 if (!isUndefOrInRange(Mask[i+l], l, l+LaneSize))
Craig Topper70b883b2011-11-28 10:14:51 +00003831 return false;
Craig Topper1a7700a2012-01-19 08:19:12 +00003832 if (NumElts != 8 || l == 0)
Craig Topper70b883b2011-11-28 10:14:51 +00003833 continue;
3834 // VPERMILPS handling
3835 if (Mask[i] < 0)
3836 continue;
Craig Topper1a7700a2012-01-19 08:19:12 +00003837 if (!isUndefOrEqual(Mask[i+l], Mask[i]+l))
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00003838 return false;
3839 }
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00003840 }
3841
3842 return true;
3843}
3844
Craig Topper5aaffa82012-02-19 02:53:47 +00003845/// isCommutedMOVLMask - Returns true if the shuffle mask is except the reverse
Evan Cheng017dcc62006-04-21 01:05:10 +00003846/// of what x86 movss want. X86 movs requires the lowest element to be lowest
Evan Cheng39623da2006-04-20 08:58:49 +00003847/// element of vector 2 and the other elements to come from vector 1 in order.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003848static bool isCommutedMOVLMask(ArrayRef<int> Mask, EVT VT,
Nate Begeman9008ca62009-04-27 18:41:29 +00003849 bool V2IsSplat = false, bool V2IsUndef = false) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00003850 if (!VT.is128BitVector())
Craig Topper97327dc2012-03-18 22:50:10 +00003851 return false;
Craig Topper7a9a28b2012-08-12 02:23:29 +00003852
3853 unsigned NumOps = VT.getVectorNumElements();
Chris Lattner5a88b832007-02-25 07:10:00 +00003854 if (NumOps != 2 && NumOps != 4 && NumOps != 8 && NumOps != 16)
Evan Cheng39623da2006-04-20 08:58:49 +00003855 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003856
Nate Begeman9008ca62009-04-27 18:41:29 +00003857 if (!isUndefOrEqual(Mask[0], 0))
Evan Cheng39623da2006-04-20 08:58:49 +00003858 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003859
Craig Topperc612d792012-01-02 09:17:37 +00003860 for (unsigned i = 1; i != NumOps; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003861 if (!(isUndefOrEqual(Mask[i], i+NumOps) ||
3862 (V2IsUndef && isUndefOrInRange(Mask[i], NumOps, NumOps*2)) ||
3863 (V2IsSplat && isUndefOrEqual(Mask[i], NumOps))))
Evan Cheng8cf723d2006-09-08 01:50:06 +00003864 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003865
Evan Cheng39623da2006-04-20 08:58:49 +00003866 return true;
3867}
3868
Evan Chengd9539472006-04-14 21:59:03 +00003869/// isMOVSHDUPMask - Return true if the specified VECTOR_SHUFFLE operand
3870/// specifies a shuffle of elements that is suitable for input to MOVSHDUP.
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003871/// Masks to match: <1, 1, 3, 3> or <1, 1, 3, 3, 5, 5, 7, 7>
Craig Topperdd637ae2012-02-19 05:41:45 +00003872static bool isMOVSHDUPMask(ArrayRef<int> Mask, EVT VT,
Craig Topper5aaffa82012-02-19 02:53:47 +00003873 const X86Subtarget *Subtarget) {
Craig Topperd0a31172012-01-10 06:37:29 +00003874 if (!Subtarget->hasSSE3())
Evan Chengd9539472006-04-14 21:59:03 +00003875 return false;
3876
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003877 unsigned NumElems = VT.getVectorNumElements();
3878
3879 if ((VT.getSizeInBits() == 128 && NumElems != 4) ||
3880 (VT.getSizeInBits() == 256 && NumElems != 8))
3881 return false;
3882
3883 // "i+1" is the value the indexed mask element must have
Craig Topperdd637ae2012-02-19 05:41:45 +00003884 for (unsigned i = 0; i != NumElems; i += 2)
3885 if (!isUndefOrEqual(Mask[i], i+1) ||
3886 !isUndefOrEqual(Mask[i+1], i+1))
Nate Begeman9008ca62009-04-27 18:41:29 +00003887 return false;
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003888
3889 return true;
Evan Chengd9539472006-04-14 21:59:03 +00003890}
3891
3892/// isMOVSLDUPMask - Return true if the specified VECTOR_SHUFFLE operand
3893/// specifies a shuffle of elements that is suitable for input to MOVSLDUP.
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003894/// Masks to match: <0, 0, 2, 2> or <0, 0, 2, 2, 4, 4, 6, 6>
Craig Topperdd637ae2012-02-19 05:41:45 +00003895static bool isMOVSLDUPMask(ArrayRef<int> Mask, EVT VT,
Craig Topper5aaffa82012-02-19 02:53:47 +00003896 const X86Subtarget *Subtarget) {
Craig Topperd0a31172012-01-10 06:37:29 +00003897 if (!Subtarget->hasSSE3())
Evan Chengd9539472006-04-14 21:59:03 +00003898 return false;
3899
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003900 unsigned NumElems = VT.getVectorNumElements();
3901
3902 if ((VT.getSizeInBits() == 128 && NumElems != 4) ||
3903 (VT.getSizeInBits() == 256 && NumElems != 8))
3904 return false;
3905
3906 // "i" is the value the indexed mask element must have
Craig Topperc612d792012-01-02 09:17:37 +00003907 for (unsigned i = 0; i != NumElems; i += 2)
Craig Topperdd637ae2012-02-19 05:41:45 +00003908 if (!isUndefOrEqual(Mask[i], i) ||
3909 !isUndefOrEqual(Mask[i+1], i))
Nate Begeman9008ca62009-04-27 18:41:29 +00003910 return false;
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003911
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003912 return true;
Evan Chengd9539472006-04-14 21:59:03 +00003913}
3914
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00003915/// isMOVDDUPYMask - Return true if the specified VECTOR_SHUFFLE operand
3916/// specifies a shuffle of elements that is suitable for input to 256-bit
3917/// version of MOVDDUP.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003918static bool isMOVDDUPYMask(ArrayRef<int> Mask, EVT VT, bool HasAVX) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00003919 if (!HasAVX || !VT.is256BitVector())
3920 return false;
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00003921
Craig Topper7a9a28b2012-08-12 02:23:29 +00003922 unsigned NumElts = VT.getVectorNumElements();
3923 if (NumElts != 4)
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00003924 return false;
3925
Craig Topperc612d792012-01-02 09:17:37 +00003926 for (unsigned i = 0; i != NumElts/2; ++i)
Craig Topperbeabc6c2011-12-05 06:56:46 +00003927 if (!isUndefOrEqual(Mask[i], 0))
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00003928 return false;
Craig Topperc612d792012-01-02 09:17:37 +00003929 for (unsigned i = NumElts/2; i != NumElts; ++i)
Craig Topperbeabc6c2011-12-05 06:56:46 +00003930 if (!isUndefOrEqual(Mask[i], NumElts/2))
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00003931 return false;
3932 return true;
3933}
3934
Evan Cheng0b457f02008-09-25 20:50:48 +00003935/// isMOVDDUPMask - Return true if the specified VECTOR_SHUFFLE operand
Bruno Cardoso Lopes06ef9232011-08-25 21:40:34 +00003936/// specifies a shuffle of elements that is suitable for input to 128-bit
3937/// version of MOVDDUP.
Craig Topperdd637ae2012-02-19 05:41:45 +00003938static bool isMOVDDUPMask(ArrayRef<int> Mask, EVT VT) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00003939 if (!VT.is128BitVector())
Bruno Cardoso Lopes06ef9232011-08-25 21:40:34 +00003940 return false;
3941
Craig Topperc612d792012-01-02 09:17:37 +00003942 unsigned e = VT.getVectorNumElements() / 2;
3943 for (unsigned i = 0; i != e; ++i)
Craig Topperdd637ae2012-02-19 05:41:45 +00003944 if (!isUndefOrEqual(Mask[i], i))
Evan Cheng0b457f02008-09-25 20:50:48 +00003945 return false;
Craig Topperc612d792012-01-02 09:17:37 +00003946 for (unsigned i = 0; i != e; ++i)
Craig Topperdd637ae2012-02-19 05:41:45 +00003947 if (!isUndefOrEqual(Mask[e+i], i))
Evan Cheng0b457f02008-09-25 20:50:48 +00003948 return false;
3949 return true;
3950}
3951
David Greenec38a03e2011-02-03 15:50:00 +00003952/// isVEXTRACTF128Index - Return true if the specified
3953/// EXTRACT_SUBVECTOR operand specifies a vector extract that is
3954/// suitable for input to VEXTRACTF128.
3955bool X86::isVEXTRACTF128Index(SDNode *N) {
3956 if (!isa<ConstantSDNode>(N->getOperand(1).getNode()))
3957 return false;
3958
3959 // The index should be aligned on a 128-bit boundary.
3960 uint64_t Index =
3961 cast<ConstantSDNode>(N->getOperand(1).getNode())->getZExtValue();
3962
3963 unsigned VL = N->getValueType(0).getVectorNumElements();
3964 unsigned VBits = N->getValueType(0).getSizeInBits();
3965 unsigned ElSize = VBits / VL;
3966 bool Result = (Index * ElSize) % 128 == 0;
3967
3968 return Result;
3969}
3970
David Greeneccacdc12011-02-04 16:08:29 +00003971/// isVINSERTF128Index - Return true if the specified INSERT_SUBVECTOR
3972/// operand specifies a subvector insert that is suitable for input to
3973/// VINSERTF128.
3974bool X86::isVINSERTF128Index(SDNode *N) {
3975 if (!isa<ConstantSDNode>(N->getOperand(2).getNode()))
3976 return false;
3977
3978 // The index should be aligned on a 128-bit boundary.
3979 uint64_t Index =
3980 cast<ConstantSDNode>(N->getOperand(2).getNode())->getZExtValue();
3981
3982 unsigned VL = N->getValueType(0).getVectorNumElements();
3983 unsigned VBits = N->getValueType(0).getSizeInBits();
3984 unsigned ElSize = VBits / VL;
3985 bool Result = (Index * ElSize) % 128 == 0;
3986
3987 return Result;
3988}
3989
Evan Cheng63d33002006-03-22 08:01:21 +00003990/// getShuffleSHUFImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00003991/// the specified VECTOR_SHUFFLE mask with PSHUF* and SHUFP* instructions.
Craig Topper1a7700a2012-01-19 08:19:12 +00003992/// Handles 128-bit and 256-bit.
Craig Topper5aaffa82012-02-19 02:53:47 +00003993static unsigned getShuffleSHUFImmediate(ShuffleVectorSDNode *N) {
Craig Topper1a7700a2012-01-19 08:19:12 +00003994 EVT VT = N->getValueType(0);
Nate Begeman9008ca62009-04-27 18:41:29 +00003995
Craig Topper1a7700a2012-01-19 08:19:12 +00003996 assert((VT.is128BitVector() || VT.is256BitVector()) &&
3997 "Unsupported vector type for PSHUF/SHUFP");
3998
3999 // Handle 128 and 256-bit vector lengths. AVX defines PSHUF/SHUFP to operate
4000 // independently on 128-bit lanes.
4001 unsigned NumElts = VT.getVectorNumElements();
4002 unsigned NumLanes = VT.getSizeInBits()/128;
4003 unsigned NumLaneElts = NumElts/NumLanes;
4004
4005 assert((NumLaneElts == 2 || NumLaneElts == 4) &&
4006 "Only supports 2 or 4 elements per lane");
4007
4008 unsigned Shift = (NumLaneElts == 4) ? 1 : 0;
Evan Chengb9df0ca2006-03-22 02:53:00 +00004009 unsigned Mask = 0;
Craig Topper1a7700a2012-01-19 08:19:12 +00004010 for (unsigned i = 0; i != NumElts; ++i) {
4011 int Elt = N->getMaskElt(i);
4012 if (Elt < 0) continue;
Craig Topper6b28d352012-05-03 07:12:59 +00004013 Elt &= NumLaneElts - 1;
4014 unsigned ShAmt = (i << Shift) % 8;
Craig Topper1a7700a2012-01-19 08:19:12 +00004015 Mask |= Elt << ShAmt;
Evan Cheng36b27f32006-03-28 23:41:33 +00004016 }
Craig Topper1a7700a2012-01-19 08:19:12 +00004017
Evan Cheng63d33002006-03-22 08:01:21 +00004018 return Mask;
4019}
4020
Evan Cheng506d3df2006-03-29 23:07:14 +00004021/// getShufflePSHUFHWImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00004022/// the specified VECTOR_SHUFFLE mask with the PSHUFHW instruction.
Craig Topperdd637ae2012-02-19 05:41:45 +00004023static unsigned getShufflePSHUFHWImmediate(ShuffleVectorSDNode *N) {
Craig Topper6b28d352012-05-03 07:12:59 +00004024 EVT VT = N->getValueType(0);
4025
4026 assert((VT == MVT::v8i16 || VT == MVT::v16i16) &&
4027 "Unsupported vector type for PSHUFHW");
4028
4029 unsigned NumElts = VT.getVectorNumElements();
4030
Evan Cheng506d3df2006-03-29 23:07:14 +00004031 unsigned Mask = 0;
Craig Topper6b28d352012-05-03 07:12:59 +00004032 for (unsigned l = 0; l != NumElts; l += 8) {
4033 // 8 nodes per lane, but we only care about the last 4.
4034 for (unsigned i = 0; i < 4; ++i) {
4035 int Elt = N->getMaskElt(l+i+4);
4036 if (Elt < 0) continue;
4037 Elt &= 0x3; // only 2-bits.
4038 Mask |= Elt << (i * 2);
4039 }
Evan Cheng506d3df2006-03-29 23:07:14 +00004040 }
Craig Topper6b28d352012-05-03 07:12:59 +00004041
Evan Cheng506d3df2006-03-29 23:07:14 +00004042 return Mask;
4043}
4044
4045/// getShufflePSHUFLWImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00004046/// the specified VECTOR_SHUFFLE mask with the PSHUFLW instruction.
Craig Topperdd637ae2012-02-19 05:41:45 +00004047static unsigned getShufflePSHUFLWImmediate(ShuffleVectorSDNode *N) {
Craig Topper6b28d352012-05-03 07:12:59 +00004048 EVT VT = N->getValueType(0);
4049
4050 assert((VT == MVT::v8i16 || VT == MVT::v16i16) &&
4051 "Unsupported vector type for PSHUFHW");
4052
4053 unsigned NumElts = VT.getVectorNumElements();
4054
Evan Cheng506d3df2006-03-29 23:07:14 +00004055 unsigned Mask = 0;
Craig Topper6b28d352012-05-03 07:12:59 +00004056 for (unsigned l = 0; l != NumElts; l += 8) {
4057 // 8 nodes per lane, but we only care about the first 4.
4058 for (unsigned i = 0; i < 4; ++i) {
4059 int Elt = N->getMaskElt(l+i);
4060 if (Elt < 0) continue;
4061 Elt &= 0x3; // only 2-bits
4062 Mask |= Elt << (i * 2);
4063 }
Evan Cheng506d3df2006-03-29 23:07:14 +00004064 }
Craig Topper6b28d352012-05-03 07:12:59 +00004065
Evan Cheng506d3df2006-03-29 23:07:14 +00004066 return Mask;
4067}
4068
Nate Begemana09008b2009-10-19 02:17:23 +00004069/// getShufflePALIGNRImmediate - Return the appropriate immediate to shuffle
4070/// the specified VECTOR_SHUFFLE mask with the PALIGNR instruction.
Craig Topperd93e4c32011-12-11 19:12:35 +00004071static unsigned getShufflePALIGNRImmediate(ShuffleVectorSDNode *SVOp) {
4072 EVT VT = SVOp->getValueType(0);
4073 unsigned EltSize = VT.getVectorElementType().getSizeInBits() >> 3;
Nate Begemana09008b2009-10-19 02:17:23 +00004074
Craig Topper0e2037b2012-01-20 05:53:00 +00004075 unsigned NumElts = VT.getVectorNumElements();
4076 unsigned NumLanes = VT.getSizeInBits()/128;
4077 unsigned NumLaneElts = NumElts/NumLanes;
4078
4079 int Val = 0;
4080 unsigned i;
4081 for (i = 0; i != NumElts; ++i) {
Nate Begemana09008b2009-10-19 02:17:23 +00004082 Val = SVOp->getMaskElt(i);
4083 if (Val >= 0)
4084 break;
4085 }
Craig Topper0e2037b2012-01-20 05:53:00 +00004086 if (Val >= (int)NumElts)
4087 Val -= NumElts - NumLaneElts;
4088
Eli Friedman63f8dde2011-07-25 21:36:45 +00004089 assert(Val - i > 0 && "PALIGNR imm should be positive");
Nate Begemana09008b2009-10-19 02:17:23 +00004090 return (Val - i) * EltSize;
4091}
4092
David Greenec38a03e2011-02-03 15:50:00 +00004093/// getExtractVEXTRACTF128Immediate - Return the appropriate immediate
4094/// to extract the specified EXTRACT_SUBVECTOR index with VEXTRACTF128
4095/// instructions.
4096unsigned X86::getExtractVEXTRACTF128Immediate(SDNode *N) {
4097 if (!isa<ConstantSDNode>(N->getOperand(1).getNode()))
4098 llvm_unreachable("Illegal extract subvector for VEXTRACTF128");
4099
4100 uint64_t Index =
4101 cast<ConstantSDNode>(N->getOperand(1).getNode())->getZExtValue();
4102
4103 EVT VecVT = N->getOperand(0).getValueType();
4104 EVT ElVT = VecVT.getVectorElementType();
4105
4106 unsigned NumElemsPerChunk = 128 / ElVT.getSizeInBits();
David Greenec38a03e2011-02-03 15:50:00 +00004107 return Index / NumElemsPerChunk;
4108}
4109
David Greeneccacdc12011-02-04 16:08:29 +00004110/// getInsertVINSERTF128Immediate - Return the appropriate immediate
4111/// to insert at the specified INSERT_SUBVECTOR index with VINSERTF128
4112/// instructions.
4113unsigned X86::getInsertVINSERTF128Immediate(SDNode *N) {
4114 if (!isa<ConstantSDNode>(N->getOperand(2).getNode()))
4115 llvm_unreachable("Illegal insert subvector for VINSERTF128");
4116
4117 uint64_t Index =
NAKAMURA Takumi27635382011-02-05 15:10:54 +00004118 cast<ConstantSDNode>(N->getOperand(2).getNode())->getZExtValue();
David Greeneccacdc12011-02-04 16:08:29 +00004119
4120 EVT VecVT = N->getValueType(0);
4121 EVT ElVT = VecVT.getVectorElementType();
4122
4123 unsigned NumElemsPerChunk = 128 / ElVT.getSizeInBits();
David Greeneccacdc12011-02-04 16:08:29 +00004124 return Index / NumElemsPerChunk;
4125}
4126
Elena Demikhovsky73c504a2012-04-15 11:18:59 +00004127/// getShuffleCLImmediate - Return the appropriate immediate to shuffle
4128/// the specified VECTOR_SHUFFLE mask with VPERMQ and VPERMPD instructions.
4129/// Handles 256-bit.
4130static unsigned getShuffleCLImmediate(ShuffleVectorSDNode *N) {
4131 EVT VT = N->getValueType(0);
4132
Elena Demikhovsky73c504a2012-04-15 11:18:59 +00004133 unsigned NumElts = VT.getVectorNumElements();
4134
Craig Topper095c5282012-04-15 23:48:57 +00004135 assert((VT.is256BitVector() && NumElts == 4) &&
4136 "Unsupported vector type for VPERMQ/VPERMPD");
4137
Elena Demikhovsky73c504a2012-04-15 11:18:59 +00004138 unsigned Mask = 0;
4139 for (unsigned i = 0; i != NumElts; ++i) {
4140 int Elt = N->getMaskElt(i);
Craig Topper095c5282012-04-15 23:48:57 +00004141 if (Elt < 0)
Elena Demikhovsky73c504a2012-04-15 11:18:59 +00004142 continue;
4143 Mask |= Elt << (i*2);
4144 }
4145
4146 return Mask;
4147}
Evan Cheng37b73872009-07-30 08:33:02 +00004148/// isZeroNode - Returns true if Elt is a constant zero or a floating point
4149/// constant +0.0.
4150bool X86::isZeroNode(SDValue Elt) {
4151 return ((isa<ConstantSDNode>(Elt) &&
Dan Gohmane368b462010-06-18 14:22:04 +00004152 cast<ConstantSDNode>(Elt)->isNullValue()) ||
Evan Cheng37b73872009-07-30 08:33:02 +00004153 (isa<ConstantFPSDNode>(Elt) &&
4154 cast<ConstantFPSDNode>(Elt)->getValueAPF().isPosZero()));
4155}
4156
Nate Begeman9008ca62009-04-27 18:41:29 +00004157/// CommuteVectorShuffle - Swap vector_shuffle operands as well as values in
4158/// their permute mask.
4159static SDValue CommuteVectorShuffle(ShuffleVectorSDNode *SVOp,
4160 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00004161 EVT VT = SVOp->getValueType(0);
Nate Begeman5a5ca152009-04-29 05:20:52 +00004162 unsigned NumElems = VT.getVectorNumElements();
Nate Begeman9008ca62009-04-27 18:41:29 +00004163 SmallVector<int, 8> MaskVec;
Eric Christopherfd179292009-08-27 18:07:15 +00004164
Nate Begeman5a5ca152009-04-29 05:20:52 +00004165 for (unsigned i = 0; i != NumElems; ++i) {
Craig Topper8ae97ba2012-05-21 06:40:16 +00004166 int Idx = SVOp->getMaskElt(i);
4167 if (Idx >= 0) {
4168 if (Idx < (int)NumElems)
4169 Idx += NumElems;
4170 else
4171 Idx -= NumElems;
4172 }
4173 MaskVec.push_back(Idx);
Evan Cheng5ced1d82006-04-06 23:23:56 +00004174 }
Nate Begeman9008ca62009-04-27 18:41:29 +00004175 return DAG.getVectorShuffle(VT, SVOp->getDebugLoc(), SVOp->getOperand(1),
4176 SVOp->getOperand(0), &MaskVec[0]);
Evan Cheng5ced1d82006-04-06 23:23:56 +00004177}
4178
Evan Cheng533a0aa2006-04-19 20:35:22 +00004179/// ShouldXformToMOVHLPS - Return true if the node should be transformed to
4180/// match movhlps. The lower half elements should come from upper half of
4181/// V1 (and in order), and the upper half elements should come from the upper
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00004182/// half of V2 (and in order).
Craig Topperdd637ae2012-02-19 05:41:45 +00004183static bool ShouldXformToMOVHLPS(ArrayRef<int> Mask, EVT VT) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00004184 if (!VT.is128BitVector())
Bruno Cardoso Lopes59353b42011-08-11 18:59:13 +00004185 return false;
4186 if (VT.getVectorNumElements() != 4)
Evan Cheng533a0aa2006-04-19 20:35:22 +00004187 return false;
4188 for (unsigned i = 0, e = 2; i != e; ++i)
Craig Topperdd637ae2012-02-19 05:41:45 +00004189 if (!isUndefOrEqual(Mask[i], i+2))
Evan Cheng533a0aa2006-04-19 20:35:22 +00004190 return false;
4191 for (unsigned i = 2; i != 4; ++i)
Craig Topperdd637ae2012-02-19 05:41:45 +00004192 if (!isUndefOrEqual(Mask[i], i+4))
Evan Cheng533a0aa2006-04-19 20:35:22 +00004193 return false;
4194 return true;
4195}
4196
Evan Cheng5ced1d82006-04-06 23:23:56 +00004197/// isScalarLoadToVector - Returns true if the node is a scalar load that
Evan Cheng7e2ff772008-05-08 00:57:18 +00004198/// is promoted to a vector. It also returns the LoadSDNode by reference if
4199/// required.
4200static bool isScalarLoadToVector(SDNode *N, LoadSDNode **LD = NULL) {
Evan Cheng0b457f02008-09-25 20:50:48 +00004201 if (N->getOpcode() != ISD::SCALAR_TO_VECTOR)
4202 return false;
4203 N = N->getOperand(0).getNode();
4204 if (!ISD::isNON_EXTLoad(N))
4205 return false;
4206 if (LD)
4207 *LD = cast<LoadSDNode>(N);
4208 return true;
Evan Cheng5ced1d82006-04-06 23:23:56 +00004209}
4210
Dan Gohman65fd6562011-11-03 21:49:52 +00004211// Test whether the given value is a vector value which will be legalized
4212// into a load.
4213static bool WillBeConstantPoolLoad(SDNode *N) {
4214 if (N->getOpcode() != ISD::BUILD_VECTOR)
4215 return false;
4216
4217 // Check for any non-constant elements.
4218 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
4219 switch (N->getOperand(i).getNode()->getOpcode()) {
4220 case ISD::UNDEF:
4221 case ISD::ConstantFP:
4222 case ISD::Constant:
4223 break;
4224 default:
4225 return false;
4226 }
4227
4228 // Vectors of all-zeros and all-ones are materialized with special
4229 // instructions rather than being loaded.
4230 return !ISD::isBuildVectorAllZeros(N) &&
4231 !ISD::isBuildVectorAllOnes(N);
4232}
4233
Evan Cheng533a0aa2006-04-19 20:35:22 +00004234/// ShouldXformToMOVLP{S|D} - Return true if the node should be transformed to
4235/// match movlp{s|d}. The lower half elements should come from lower half of
4236/// V1 (and in order), and the upper half elements should come from the upper
4237/// half of V2 (and in order). And since V1 will become the source of the
4238/// MOVLP, it must be either a vector load or a scalar load to vector.
Nate Begeman9008ca62009-04-27 18:41:29 +00004239static bool ShouldXformToMOVLP(SDNode *V1, SDNode *V2,
Craig Topperdd637ae2012-02-19 05:41:45 +00004240 ArrayRef<int> Mask, EVT VT) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00004241 if (!VT.is128BitVector())
Bruno Cardoso Lopes59353b42011-08-11 18:59:13 +00004242 return false;
4243
Evan Cheng466685d2006-10-09 20:57:25 +00004244 if (!ISD::isNON_EXTLoad(V1) && !isScalarLoadToVector(V1))
Evan Cheng533a0aa2006-04-19 20:35:22 +00004245 return false;
Evan Cheng23425f52006-10-09 21:39:25 +00004246 // Is V2 is a vector load, don't do this transformation. We will try to use
4247 // load folding shufps op.
Dan Gohman65fd6562011-11-03 21:49:52 +00004248 if (ISD::isNON_EXTLoad(V2) || WillBeConstantPoolLoad(V2))
Evan Cheng23425f52006-10-09 21:39:25 +00004249 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00004250
Bruno Cardoso Lopes59353b42011-08-11 18:59:13 +00004251 unsigned NumElems = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00004252
Evan Cheng533a0aa2006-04-19 20:35:22 +00004253 if (NumElems != 2 && NumElems != 4)
4254 return false;
Nate Begeman5a5ca152009-04-29 05:20:52 +00004255 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
Craig Topperdd637ae2012-02-19 05:41:45 +00004256 if (!isUndefOrEqual(Mask[i], i))
Evan Cheng533a0aa2006-04-19 20:35:22 +00004257 return false;
Chad Rosier238ae312012-04-30 17:47:15 +00004258 for (unsigned i = NumElems/2, e = NumElems; i != e; ++i)
Craig Topperdd637ae2012-02-19 05:41:45 +00004259 if (!isUndefOrEqual(Mask[i], i+NumElems))
Evan Cheng533a0aa2006-04-19 20:35:22 +00004260 return false;
4261 return true;
Evan Cheng5ced1d82006-04-06 23:23:56 +00004262}
4263
Evan Cheng39623da2006-04-20 08:58:49 +00004264/// isSplatVector - Returns true if N is a BUILD_VECTOR node whose elements are
4265/// all the same.
4266static bool isSplatVector(SDNode *N) {
4267 if (N->getOpcode() != ISD::BUILD_VECTOR)
4268 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00004269
Dan Gohman475871a2008-07-27 21:46:04 +00004270 SDValue SplatValue = N->getOperand(0);
Evan Cheng39623da2006-04-20 08:58:49 +00004271 for (unsigned i = 1, e = N->getNumOperands(); i != e; ++i)
4272 if (N->getOperand(i) != SplatValue)
Evan Cheng5ced1d82006-04-06 23:23:56 +00004273 return false;
4274 return true;
4275}
4276
Evan Cheng213d2cf2007-05-17 18:45:50 +00004277/// isZeroShuffle - Returns true if N is a VECTOR_SHUFFLE that can be resolved
Eric Christopherfd179292009-08-27 18:07:15 +00004278/// to an zero vector.
Nate Begeman5a5ca152009-04-29 05:20:52 +00004279/// FIXME: move to dag combiner / method on ShuffleVectorSDNode
Nate Begeman9008ca62009-04-27 18:41:29 +00004280static bool isZeroShuffle(ShuffleVectorSDNode *N) {
Dan Gohman475871a2008-07-27 21:46:04 +00004281 SDValue V1 = N->getOperand(0);
4282 SDValue V2 = N->getOperand(1);
Nate Begeman5a5ca152009-04-29 05:20:52 +00004283 unsigned NumElems = N->getValueType(0).getVectorNumElements();
4284 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004285 int Idx = N->getMaskElt(i);
Nate Begeman5a5ca152009-04-29 05:20:52 +00004286 if (Idx >= (int)NumElems) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004287 unsigned Opc = V2.getOpcode();
Rafael Espindola15684b22009-04-24 12:40:33 +00004288 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V2.getNode()))
4289 continue;
Evan Cheng37b73872009-07-30 08:33:02 +00004290 if (Opc != ISD::BUILD_VECTOR ||
4291 !X86::isZeroNode(V2.getOperand(Idx-NumElems)))
Nate Begeman9008ca62009-04-27 18:41:29 +00004292 return false;
4293 } else if (Idx >= 0) {
4294 unsigned Opc = V1.getOpcode();
4295 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V1.getNode()))
4296 continue;
Evan Cheng37b73872009-07-30 08:33:02 +00004297 if (Opc != ISD::BUILD_VECTOR ||
4298 !X86::isZeroNode(V1.getOperand(Idx)))
Chris Lattner8a594482007-11-25 00:24:49 +00004299 return false;
Evan Cheng213d2cf2007-05-17 18:45:50 +00004300 }
4301 }
4302 return true;
4303}
4304
4305/// getZeroVector - Returns a vector of specified type with all zero elements.
4306///
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00004307static SDValue getZeroVector(EVT VT, const X86Subtarget *Subtarget,
Craig Topper12216172012-01-13 08:12:35 +00004308 SelectionDAG &DAG, DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004309 assert(VT.isVector() && "Expected a vector type");
Craig Topper9d352402012-04-23 07:24:41 +00004310 unsigned Size = VT.getSizeInBits();
Scott Michelfdc40a02009-02-17 22:15:04 +00004311
Dale Johannesen0488fb62010-09-30 23:57:10 +00004312 // Always build SSE zero vectors as <4 x i32> bitcasted
Bruno Cardoso Lopes8c05a852010-08-12 02:06:36 +00004313 // to their dest type. This ensures they get CSE'd.
Dan Gohman475871a2008-07-27 21:46:04 +00004314 SDValue Vec;
Craig Topper9d352402012-04-23 07:24:41 +00004315 if (Size == 128) { // SSE
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00004316 if (Subtarget->hasSSE2()) { // SSE2
Bruno Cardoso Lopes8c05a852010-08-12 02:06:36 +00004317 SDValue Cst = DAG.getTargetConstant(0, MVT::i32);
4318 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
4319 } else { // SSE1
4320 SDValue Cst = DAG.getTargetConstantFP(+0.0, MVT::f32);
4321 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4f32, Cst, Cst, Cst, Cst);
4322 }
Craig Topper9d352402012-04-23 07:24:41 +00004323 } else if (Size == 256) { // AVX
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00004324 if (Subtarget->hasAVX2()) { // AVX2
Craig Topper12216172012-01-13 08:12:35 +00004325 SDValue Cst = DAG.getTargetConstant(0, MVT::i32);
4326 SDValue Ops[] = { Cst, Cst, Cst, Cst, Cst, Cst, Cst, Cst };
4327 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v8i32, Ops, 8);
4328 } else {
4329 // 256-bit logic and arithmetic instructions in AVX are all
4330 // floating-point, no support for integer ops. Emit fp zeroed vectors.
4331 SDValue Cst = DAG.getTargetConstantFP(+0.0, MVT::f32);
4332 SDValue Ops[] = { Cst, Cst, Cst, Cst, Cst, Cst, Cst, Cst };
4333 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v8f32, Ops, 8);
4334 }
Craig Topper9d352402012-04-23 07:24:41 +00004335 } else
4336 llvm_unreachable("Unexpected vector type");
4337
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004338 return DAG.getNode(ISD::BITCAST, dl, VT, Vec);
Evan Cheng213d2cf2007-05-17 18:45:50 +00004339}
4340
Chris Lattner8a594482007-11-25 00:24:49 +00004341/// getOnesVector - Returns a vector of specified type with all bits set.
Craig Topper745a86b2011-11-19 22:34:59 +00004342/// Always build ones vectors as <4 x i32> or <8 x i32>. For 256-bit types with
4343/// no AVX2 supprt, use two <4 x i32> inserted in a <8 x i32> appropriately.
4344/// Then bitcast to their original type, ensuring they get CSE'd.
4345static SDValue getOnesVector(EVT VT, bool HasAVX2, SelectionDAG &DAG,
4346 DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004347 assert(VT.isVector() && "Expected a vector type");
Craig Topper9d352402012-04-23 07:24:41 +00004348 unsigned Size = VT.getSizeInBits();
Scott Michelfdc40a02009-02-17 22:15:04 +00004349
Owen Anderson825b72b2009-08-11 20:47:22 +00004350 SDValue Cst = DAG.getTargetConstant(~0U, MVT::i32);
Craig Topper745a86b2011-11-19 22:34:59 +00004351 SDValue Vec;
Craig Topper9d352402012-04-23 07:24:41 +00004352 if (Size == 256) {
Craig Topper745a86b2011-11-19 22:34:59 +00004353 if (HasAVX2) { // AVX2
4354 SDValue Ops[] = { Cst, Cst, Cst, Cst, Cst, Cst, Cst, Cst };
4355 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v8i32, Ops, 8);
4356 } else { // AVX
4357 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
Craig Topper4c7972d2012-04-22 18:15:59 +00004358 Vec = Concat128BitVectors(Vec, Vec, MVT::v8i32, 8, DAG, dl);
Craig Topper745a86b2011-11-19 22:34:59 +00004359 }
Craig Topper9d352402012-04-23 07:24:41 +00004360 } else if (Size == 128) {
Craig Topper745a86b2011-11-19 22:34:59 +00004361 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
Craig Topper9d352402012-04-23 07:24:41 +00004362 } else
4363 llvm_unreachable("Unexpected vector type");
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +00004364
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004365 return DAG.getNode(ISD::BITCAST, dl, VT, Vec);
Chris Lattner8a594482007-11-25 00:24:49 +00004366}
4367
Evan Cheng39623da2006-04-20 08:58:49 +00004368/// NormalizeMask - V2 is a splat, modify the mask (if needed) so all elements
4369/// that point to V2 points to its first element.
Craig Topper39a9e482012-02-11 06:24:48 +00004370static void NormalizeMask(SmallVectorImpl<int> &Mask, unsigned NumElems) {
Nate Begeman5a5ca152009-04-29 05:20:52 +00004371 for (unsigned i = 0; i != NumElems; ++i) {
Craig Topper39a9e482012-02-11 06:24:48 +00004372 if (Mask[i] > (int)NumElems) {
4373 Mask[i] = NumElems;
Evan Cheng39623da2006-04-20 08:58:49 +00004374 }
Evan Cheng39623da2006-04-20 08:58:49 +00004375 }
Evan Cheng39623da2006-04-20 08:58:49 +00004376}
4377
Evan Cheng017dcc62006-04-21 01:05:10 +00004378/// getMOVLMask - Returns a vector_shuffle mask for an movs{s|d}, movd
4379/// operation of specified width.
Owen Andersone50ed302009-08-10 22:56:29 +00004380static SDValue getMOVL(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00004381 SDValue V2) {
4382 unsigned NumElems = VT.getVectorNumElements();
4383 SmallVector<int, 8> Mask;
4384 Mask.push_back(NumElems);
Evan Cheng39623da2006-04-20 08:58:49 +00004385 for (unsigned i = 1; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004386 Mask.push_back(i);
4387 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Evan Cheng39623da2006-04-20 08:58:49 +00004388}
4389
Nate Begeman9008ca62009-04-27 18:41:29 +00004390/// getUnpackl - Returns a vector_shuffle node for an unpackl operation.
Owen Andersone50ed302009-08-10 22:56:29 +00004391static SDValue getUnpackl(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00004392 SDValue V2) {
4393 unsigned NumElems = VT.getVectorNumElements();
4394 SmallVector<int, 8> Mask;
Evan Chengc575ca22006-04-17 20:43:08 +00004395 for (unsigned i = 0, e = NumElems/2; i != e; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004396 Mask.push_back(i);
4397 Mask.push_back(i + NumElems);
Evan Chengc575ca22006-04-17 20:43:08 +00004398 }
Nate Begeman9008ca62009-04-27 18:41:29 +00004399 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Evan Chengc575ca22006-04-17 20:43:08 +00004400}
4401
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004402/// getUnpackh - Returns a vector_shuffle node for an unpackh operation.
Owen Andersone50ed302009-08-10 22:56:29 +00004403static SDValue getUnpackh(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00004404 SDValue V2) {
4405 unsigned NumElems = VT.getVectorNumElements();
Nate Begeman9008ca62009-04-27 18:41:29 +00004406 SmallVector<int, 8> Mask;
Chad Rosier238ae312012-04-30 17:47:15 +00004407 for (unsigned i = 0, Half = NumElems/2; i != Half; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004408 Mask.push_back(i + Half);
4409 Mask.push_back(i + NumElems + Half);
Evan Cheng39623da2006-04-20 08:58:49 +00004410 }
Nate Begeman9008ca62009-04-27 18:41:29 +00004411 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Chris Lattner62098042008-03-09 01:05:04 +00004412}
4413
Bruno Cardoso Lopes5f1d8ab2011-08-11 02:49:44 +00004414// PromoteSplati8i16 - All i16 and i8 vector types can't be used directly by
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004415// a generic shuffle instruction because the target has no such instructions.
4416// Generate shuffles which repeat i16 and i8 several times until they can be
4417// represented by v4f32 and then be manipulated by target suported shuffles.
Bruno Cardoso Lopes5f1d8ab2011-08-11 02:49:44 +00004418static SDValue PromoteSplati8i16(SDValue V, SelectionDAG &DAG, int &EltNo) {
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004419 EVT VT = V.getValueType();
Nate Begeman9008ca62009-04-27 18:41:29 +00004420 int NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004421 DebugLoc dl = V.getDebugLoc();
Rafael Espindola15684b22009-04-24 12:40:33 +00004422
Nate Begeman9008ca62009-04-27 18:41:29 +00004423 while (NumElems > 4) {
4424 if (EltNo < NumElems/2) {
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004425 V = getUnpackl(DAG, dl, VT, V, V);
Nate Begeman9008ca62009-04-27 18:41:29 +00004426 } else {
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004427 V = getUnpackh(DAG, dl, VT, V, V);
Nate Begeman9008ca62009-04-27 18:41:29 +00004428 EltNo -= NumElems/2;
4429 }
4430 NumElems >>= 1;
4431 }
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004432 return V;
4433}
Eric Christopherfd179292009-08-27 18:07:15 +00004434
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004435/// getLegalSplat - Generate a legal splat with supported x86 shuffles
4436static SDValue getLegalSplat(SelectionDAG &DAG, SDValue V, int EltNo) {
4437 EVT VT = V.getValueType();
4438 DebugLoc dl = V.getDebugLoc();
Craig Topper9d352402012-04-23 07:24:41 +00004439 unsigned Size = VT.getSizeInBits();
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004440
Craig Topper9d352402012-04-23 07:24:41 +00004441 if (Size == 128) {
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004442 V = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, V);
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004443 int SplatMask[4] = { EltNo, EltNo, EltNo, EltNo };
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004444 V = DAG.getVectorShuffle(MVT::v4f32, dl, V, DAG.getUNDEF(MVT::v4f32),
4445 &SplatMask[0]);
Craig Topper9d352402012-04-23 07:24:41 +00004446 } else if (Size == 256) {
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004447 // To use VPERMILPS to splat scalars, the second half of indicies must
4448 // refer to the higher part, which is a duplication of the lower one,
4449 // because VPERMILPS can only handle in-lane permutations.
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004450 int SplatMask[8] = { EltNo, EltNo, EltNo, EltNo,
4451 EltNo+4, EltNo+4, EltNo+4, EltNo+4 };
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004452
4453 V = DAG.getNode(ISD::BITCAST, dl, MVT::v8f32, V);
4454 V = DAG.getVectorShuffle(MVT::v8f32, dl, V, DAG.getUNDEF(MVT::v8f32),
4455 &SplatMask[0]);
Craig Topper9d352402012-04-23 07:24:41 +00004456 } else
4457 llvm_unreachable("Vector size not supported");
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004458
4459 return DAG.getNode(ISD::BITCAST, dl, VT, V);
4460}
4461
Bruno Cardoso Lopes8a5b2622011-08-17 02:29:13 +00004462/// PromoteSplat - Splat is promoted to target supported vector shuffles.
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004463static SDValue PromoteSplat(ShuffleVectorSDNode *SV, SelectionDAG &DAG) {
4464 EVT SrcVT = SV->getValueType(0);
4465 SDValue V1 = SV->getOperand(0);
4466 DebugLoc dl = SV->getDebugLoc();
4467
4468 int EltNo = SV->getSplatIndex();
4469 int NumElems = SrcVT.getVectorNumElements();
4470 unsigned Size = SrcVT.getSizeInBits();
4471
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004472 assert(((Size == 128 && NumElems > 4) || Size == 256) &&
4473 "Unknown how to promote splat for type");
4474
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004475 // Extract the 128-bit part containing the splat element and update
4476 // the splat element index when it refers to the higher register.
4477 if (Size == 256) {
Craig Topper7d1e3dc2012-04-30 05:17:10 +00004478 V1 = Extract128BitVector(V1, EltNo, DAG, dl);
4479 if (EltNo >= NumElems/2)
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004480 EltNo -= NumElems/2;
4481 }
4482
Bruno Cardoso Lopes8a5b2622011-08-17 02:29:13 +00004483 // All i16 and i8 vector types can't be used directly by a generic shuffle
4484 // instruction because the target has no such instruction. Generate shuffles
4485 // which repeat i16 and i8 several times until they fit in i32, and then can
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004486 // be manipulated by target suported shuffles.
Bruno Cardoso Lopes5f1d8ab2011-08-11 02:49:44 +00004487 EVT EltVT = SrcVT.getVectorElementType();
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004488 if (EltVT == MVT::i8 || EltVT == MVT::i16)
Bruno Cardoso Lopes5f1d8ab2011-08-11 02:49:44 +00004489 V1 = PromoteSplati8i16(V1, DAG, EltNo);
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004490
4491 // Recreate the 256-bit vector and place the same 128-bit vector
4492 // into the low and high part. This is necessary because we want
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004493 // to use VPERM* to shuffle the vectors
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004494 if (Size == 256) {
Craig Topper4c7972d2012-04-22 18:15:59 +00004495 V1 = DAG.getNode(ISD::CONCAT_VECTORS, dl, SrcVT, V1, V1);
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004496 }
4497
4498 return getLegalSplat(DAG, V1, EltNo);
Evan Chengc575ca22006-04-17 20:43:08 +00004499}
4500
Evan Chengba05f722006-04-21 23:03:30 +00004501/// getShuffleVectorZeroOrUndef - Return a vector_shuffle of the specified
Chris Lattner8a594482007-11-25 00:24:49 +00004502/// vector of zero or undef vector. This produces a shuffle where the low
4503/// element of V2 is swizzled into the zero/undef vector, landing at element
4504/// 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 +00004505static SDValue getShuffleVectorZeroOrUndef(SDValue V2, unsigned Idx,
Craig Topper12216172012-01-13 08:12:35 +00004506 bool IsZero,
4507 const X86Subtarget *Subtarget,
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00004508 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00004509 EVT VT = V2.getValueType();
Craig Topper12216172012-01-13 08:12:35 +00004510 SDValue V1 = IsZero
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00004511 ? getZeroVector(VT, Subtarget, DAG, V2.getDebugLoc()) : DAG.getUNDEF(VT);
Nate Begeman9008ca62009-04-27 18:41:29 +00004512 unsigned NumElems = VT.getVectorNumElements();
4513 SmallVector<int, 16> MaskVec;
Chris Lattner8a594482007-11-25 00:24:49 +00004514 for (unsigned i = 0; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004515 // If this is the insertion idx, put the low elt of V2 here.
4516 MaskVec.push_back(i == Idx ? NumElems : i);
4517 return DAG.getVectorShuffle(VT, V2.getDebugLoc(), V1, V2, &MaskVec[0]);
Evan Cheng017dcc62006-04-21 01:05:10 +00004518}
4519
Craig Toppera1ffc682012-03-20 06:42:26 +00004520/// getTargetShuffleMask - Calculates the shuffle mask corresponding to the
4521/// target specific opcode. Returns true if the Mask could be calculated.
Craig Topper89f4e662012-03-20 07:17:59 +00004522/// Sets IsUnary to true if only uses one source.
Craig Topperd978c542012-05-06 19:46:21 +00004523static bool getTargetShuffleMask(SDNode *N, MVT VT,
Craig Topper89f4e662012-03-20 07:17:59 +00004524 SmallVectorImpl<int> &Mask, bool &IsUnary) {
Craig Toppera1ffc682012-03-20 06:42:26 +00004525 unsigned NumElems = VT.getVectorNumElements();
4526 SDValue ImmN;
4527
Craig Topper89f4e662012-03-20 07:17:59 +00004528 IsUnary = false;
Craig Toppera1ffc682012-03-20 06:42:26 +00004529 switch(N->getOpcode()) {
4530 case X86ISD::SHUFP:
4531 ImmN = N->getOperand(N->getNumOperands()-1);
4532 DecodeSHUFPMask(VT, cast<ConstantSDNode>(ImmN)->getZExtValue(), Mask);
4533 break;
4534 case X86ISD::UNPCKH:
4535 DecodeUNPCKHMask(VT, Mask);
4536 break;
4537 case X86ISD::UNPCKL:
4538 DecodeUNPCKLMask(VT, Mask);
4539 break;
4540 case X86ISD::MOVHLPS:
4541 DecodeMOVHLPSMask(NumElems, Mask);
4542 break;
4543 case X86ISD::MOVLHPS:
4544 DecodeMOVLHPSMask(NumElems, Mask);
4545 break;
4546 case X86ISD::PSHUFD:
4547 case X86ISD::VPERMILP:
4548 ImmN = N->getOperand(N->getNumOperands()-1);
4549 DecodePSHUFMask(VT, cast<ConstantSDNode>(ImmN)->getZExtValue(), Mask);
Craig Topper89f4e662012-03-20 07:17:59 +00004550 IsUnary = true;
Craig Toppera1ffc682012-03-20 06:42:26 +00004551 break;
4552 case X86ISD::PSHUFHW:
4553 ImmN = N->getOperand(N->getNumOperands()-1);
Craig Toppera9a568a2012-05-02 08:03:44 +00004554 DecodePSHUFHWMask(VT, cast<ConstantSDNode>(ImmN)->getZExtValue(), Mask);
Craig Topper89f4e662012-03-20 07:17:59 +00004555 IsUnary = true;
Craig Toppera1ffc682012-03-20 06:42:26 +00004556 break;
4557 case X86ISD::PSHUFLW:
4558 ImmN = N->getOperand(N->getNumOperands()-1);
Craig Toppera9a568a2012-05-02 08:03:44 +00004559 DecodePSHUFLWMask(VT, cast<ConstantSDNode>(ImmN)->getZExtValue(), Mask);
Craig Topper89f4e662012-03-20 07:17:59 +00004560 IsUnary = true;
Craig Toppera1ffc682012-03-20 06:42:26 +00004561 break;
Craig Topperbdcbcb32012-05-06 18:54:26 +00004562 case X86ISD::VPERMI:
4563 ImmN = N->getOperand(N->getNumOperands()-1);
4564 DecodeVPERMMask(cast<ConstantSDNode>(ImmN)->getZExtValue(), Mask);
4565 IsUnary = true;
4566 break;
Craig Toppera1ffc682012-03-20 06:42:26 +00004567 case X86ISD::MOVSS:
4568 case X86ISD::MOVSD: {
4569 // The index 0 always comes from the first element of the second source,
4570 // this is why MOVSS and MOVSD are used in the first place. The other
4571 // elements come from the other positions of the first source vector
4572 Mask.push_back(NumElems);
4573 for (unsigned i = 1; i != NumElems; ++i) {
4574 Mask.push_back(i);
4575 }
4576 break;
4577 }
4578 case X86ISD::VPERM2X128:
4579 ImmN = N->getOperand(N->getNumOperands()-1);
4580 DecodeVPERM2X128Mask(VT, cast<ConstantSDNode>(ImmN)->getZExtValue(), Mask);
Craig Topper2091df32012-04-17 05:54:54 +00004581 if (Mask.empty()) return false;
Craig Toppera1ffc682012-03-20 06:42:26 +00004582 break;
4583 case X86ISD::MOVDDUP:
4584 case X86ISD::MOVLHPD:
4585 case X86ISD::MOVLPD:
4586 case X86ISD::MOVLPS:
4587 case X86ISD::MOVSHDUP:
4588 case X86ISD::MOVSLDUP:
4589 case X86ISD::PALIGN:
4590 // Not yet implemented
4591 return false;
4592 default: llvm_unreachable("unknown target shuffle node");
4593 }
4594
4595 return true;
4596}
4597
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004598/// getShuffleScalarElt - Returns the scalar element that will make up the ith
4599/// element of the result of the vector shuffle.
Craig Topper3d092db2012-03-21 02:14:01 +00004600static SDValue getShuffleScalarElt(SDNode *N, unsigned Index, SelectionDAG &DAG,
Benjamin Kramer050db522011-03-26 12:38:19 +00004601 unsigned Depth) {
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004602 if (Depth == 6)
4603 return SDValue(); // Limit search depth.
4604
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004605 SDValue V = SDValue(N, 0);
4606 EVT VT = V.getValueType();
4607 unsigned Opcode = V.getOpcode();
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004608
4609 // Recurse into ISD::VECTOR_SHUFFLE node to find scalars.
4610 if (const ShuffleVectorSDNode *SV = dyn_cast<ShuffleVectorSDNode>(N)) {
Craig Topper3d092db2012-03-21 02:14:01 +00004611 int Elt = SV->getMaskElt(Index);
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004612
Craig Topper3d092db2012-03-21 02:14:01 +00004613 if (Elt < 0)
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004614 return DAG.getUNDEF(VT.getVectorElementType());
4615
Craig Topperd156dc12012-02-06 07:17:51 +00004616 unsigned NumElems = VT.getVectorNumElements();
Craig Topper3d092db2012-03-21 02:14:01 +00004617 SDValue NewV = (Elt < (int)NumElems) ? SV->getOperand(0)
4618 : SV->getOperand(1);
4619 return getShuffleScalarElt(NewV.getNode(), Elt % NumElems, DAG, Depth+1);
Evan Chengf26ffe92008-05-29 08:22:04 +00004620 }
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004621
4622 // Recurse into target specific vector shuffles to find scalars.
4623 if (isTargetShuffle(Opcode)) {
Craig Topperd978c542012-05-06 19:46:21 +00004624 MVT ShufVT = V.getValueType().getSimpleVT();
4625 unsigned NumElems = ShufVT.getVectorNumElements();
Craig Toppera1ffc682012-03-20 06:42:26 +00004626 SmallVector<int, 16> ShuffleMask;
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004627 SDValue ImmN;
Craig Topper89f4e662012-03-20 07:17:59 +00004628 bool IsUnary;
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004629
Craig Topperd978c542012-05-06 19:46:21 +00004630 if (!getTargetShuffleMask(N, ShufVT, ShuffleMask, IsUnary))
Craig Toppera1ffc682012-03-20 06:42:26 +00004631 return SDValue();
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004632
Craig Topper3d092db2012-03-21 02:14:01 +00004633 int Elt = ShuffleMask[Index];
4634 if (Elt < 0)
Craig Topperd978c542012-05-06 19:46:21 +00004635 return DAG.getUNDEF(ShufVT.getVectorElementType());
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004636
Craig Topper3d092db2012-03-21 02:14:01 +00004637 SDValue NewV = (Elt < (int)NumElems) ? N->getOperand(0)
Craig Topperd978c542012-05-06 19:46:21 +00004638 : N->getOperand(1);
Craig Topper3d092db2012-03-21 02:14:01 +00004639 return getShuffleScalarElt(NewV.getNode(), Elt % NumElems, DAG,
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004640 Depth+1);
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004641 }
4642
4643 // Actual nodes that may contain scalar elements
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004644 if (Opcode == ISD::BITCAST) {
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004645 V = V.getOperand(0);
4646 EVT SrcVT = V.getValueType();
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00004647 unsigned NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004648
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00004649 if (!SrcVT.isVector() || SrcVT.getVectorNumElements() != NumElems)
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004650 return SDValue();
4651 }
4652
4653 if (V.getOpcode() == ISD::SCALAR_TO_VECTOR)
4654 return (Index == 0) ? V.getOperand(0)
Craig Topper3d092db2012-03-21 02:14:01 +00004655 : DAG.getUNDEF(VT.getVectorElementType());
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004656
4657 if (V.getOpcode() == ISD::BUILD_VECTOR)
4658 return V.getOperand(Index);
4659
4660 return SDValue();
4661}
4662
4663/// getNumOfConsecutiveZeros - Return the number of elements of a vector
4664/// shuffle operation which come from a consecutively from a zero. The
Chris Lattner7a2bdde2011-04-15 05:18:47 +00004665/// search can start in two different directions, from left or right.
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004666static
Craig Topper3d092db2012-03-21 02:14:01 +00004667unsigned getNumOfConsecutiveZeros(ShuffleVectorSDNode *SVOp, unsigned NumElems,
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004668 bool ZerosFromLeft, SelectionDAG &DAG) {
Craig Topper3d092db2012-03-21 02:14:01 +00004669 unsigned i;
4670 for (i = 0; i != NumElems; ++i) {
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004671 unsigned Index = ZerosFromLeft ? i : NumElems-i-1;
Craig Topper3d092db2012-03-21 02:14:01 +00004672 SDValue Elt = getShuffleScalarElt(SVOp, Index, DAG, 0);
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004673 if (!(Elt.getNode() &&
4674 (Elt.getOpcode() == ISD::UNDEF || X86::isZeroNode(Elt))))
4675 break;
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004676 }
4677
4678 return i;
4679}
4680
Craig Topper3d092db2012-03-21 02:14:01 +00004681/// isShuffleMaskConsecutive - Check if the shuffle mask indicies [MaskI, MaskE)
4682/// correspond consecutively to elements from one of the vector operands,
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004683/// starting from its index OpIdx. Also tell OpNum which source vector operand.
4684static
Craig Topper3d092db2012-03-21 02:14:01 +00004685bool isShuffleMaskConsecutive(ShuffleVectorSDNode *SVOp,
4686 unsigned MaskI, unsigned MaskE, unsigned OpIdx,
4687 unsigned NumElems, unsigned &OpNum) {
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004688 bool SeenV1 = false;
4689 bool SeenV2 = false;
4690
Craig Topper3d092db2012-03-21 02:14:01 +00004691 for (unsigned i = MaskI; i != MaskE; ++i, ++OpIdx) {
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004692 int Idx = SVOp->getMaskElt(i);
4693 // Ignore undef indicies
4694 if (Idx < 0)
4695 continue;
4696
Craig Topper3d092db2012-03-21 02:14:01 +00004697 if (Idx < (int)NumElems)
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004698 SeenV1 = true;
4699 else
4700 SeenV2 = true;
4701
4702 // Only accept consecutive elements from the same vector
4703 if ((Idx % NumElems != OpIdx) || (SeenV1 && SeenV2))
4704 return false;
4705 }
4706
4707 OpNum = SeenV1 ? 0 : 1;
4708 return true;
4709}
4710
4711/// isVectorShiftRight - Returns true if the shuffle can be implemented as a
4712/// logical left shift of a vector.
4713static bool isVectorShiftRight(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
4714 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
4715 unsigned NumElems = SVOp->getValueType(0).getVectorNumElements();
4716 unsigned NumZeros = getNumOfConsecutiveZeros(SVOp, NumElems,
4717 false /* check zeros from right */, DAG);
4718 unsigned OpSrc;
4719
4720 if (!NumZeros)
4721 return false;
4722
4723 // Considering the elements in the mask that are not consecutive zeros,
4724 // check if they consecutively come from only one of the source vectors.
4725 //
4726 // V1 = {X, A, B, C} 0
4727 // \ \ \ /
4728 // vector_shuffle V1, V2 <1, 2, 3, X>
4729 //
4730 if (!isShuffleMaskConsecutive(SVOp,
4731 0, // Mask Start Index
Craig Topper3d092db2012-03-21 02:14:01 +00004732 NumElems-NumZeros, // Mask End Index(exclusive)
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004733 NumZeros, // Where to start looking in the src vector
4734 NumElems, // Number of elements in vector
4735 OpSrc)) // Which source operand ?
4736 return false;
4737
4738 isLeft = false;
4739 ShAmt = NumZeros;
4740 ShVal = SVOp->getOperand(OpSrc);
4741 return true;
4742}
4743
4744/// isVectorShiftLeft - Returns true if the shuffle can be implemented as a
4745/// logical left shift of a vector.
4746static bool isVectorShiftLeft(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
4747 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
4748 unsigned NumElems = SVOp->getValueType(0).getVectorNumElements();
4749 unsigned NumZeros = getNumOfConsecutiveZeros(SVOp, NumElems,
4750 true /* check zeros from left */, DAG);
4751 unsigned OpSrc;
4752
4753 if (!NumZeros)
4754 return false;
4755
4756 // Considering the elements in the mask that are not consecutive zeros,
4757 // check if they consecutively come from only one of the source vectors.
4758 //
4759 // 0 { A, B, X, X } = V2
4760 // / \ / /
4761 // vector_shuffle V1, V2 <X, X, 4, 5>
4762 //
4763 if (!isShuffleMaskConsecutive(SVOp,
4764 NumZeros, // Mask Start Index
Craig Topper3d092db2012-03-21 02:14:01 +00004765 NumElems, // Mask End Index(exclusive)
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004766 0, // Where to start looking in the src vector
4767 NumElems, // Number of elements in vector
4768 OpSrc)) // Which source operand ?
4769 return false;
4770
4771 isLeft = true;
4772 ShAmt = NumZeros;
4773 ShVal = SVOp->getOperand(OpSrc);
4774 return true;
Evan Chengf26ffe92008-05-29 08:22:04 +00004775}
4776
4777/// isVectorShift - Returns true if the shuffle can be implemented as a
4778/// logical left or right shift of a vector.
Nate Begeman9008ca62009-04-27 18:41:29 +00004779static bool isVectorShift(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
Dan Gohman475871a2008-07-27 21:46:04 +00004780 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00004781 // Although the logic below support any bitwidth size, there are no
4782 // shift instructions which handle more than 128-bit vectors.
Craig Topper7a9a28b2012-08-12 02:23:29 +00004783 if (!SVOp->getValueType(0).is128BitVector())
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00004784 return false;
4785
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004786 if (isVectorShiftLeft(SVOp, DAG, isLeft, ShVal, ShAmt) ||
4787 isVectorShiftRight(SVOp, DAG, isLeft, ShVal, ShAmt))
4788 return true;
Evan Chengf26ffe92008-05-29 08:22:04 +00004789
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004790 return false;
Evan Chengf26ffe92008-05-29 08:22:04 +00004791}
4792
Evan Chengc78d3b42006-04-24 18:01:45 +00004793/// LowerBuildVectorv16i8 - Custom lower build_vector of v16i8.
4794///
Dan Gohman475871a2008-07-27 21:46:04 +00004795static SDValue LowerBuildVectorv16i8(SDValue Op, unsigned NonZeros,
Evan Chengc78d3b42006-04-24 18:01:45 +00004796 unsigned NumNonZero, unsigned NumZero,
Dan Gohmand858e902010-04-17 15:26:15 +00004797 SelectionDAG &DAG,
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00004798 const X86Subtarget* Subtarget,
Dan Gohmand858e902010-04-17 15:26:15 +00004799 const TargetLowering &TLI) {
Evan Chengc78d3b42006-04-24 18:01:45 +00004800 if (NumNonZero > 8)
Dan Gohman475871a2008-07-27 21:46:04 +00004801 return SDValue();
Evan Chengc78d3b42006-04-24 18:01:45 +00004802
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004803 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00004804 SDValue V(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00004805 bool First = true;
4806 for (unsigned i = 0; i < 16; ++i) {
4807 bool ThisIsNonZero = (NonZeros & (1 << i)) != 0;
4808 if (ThisIsNonZero && First) {
4809 if (NumZero)
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00004810 V = getZeroVector(MVT::v8i16, Subtarget, DAG, dl);
Evan Chengc78d3b42006-04-24 18:01:45 +00004811 else
Owen Anderson825b72b2009-08-11 20:47:22 +00004812 V = DAG.getUNDEF(MVT::v8i16);
Evan Chengc78d3b42006-04-24 18:01:45 +00004813 First = false;
4814 }
4815
4816 if ((i & 1) != 0) {
Dan Gohman475871a2008-07-27 21:46:04 +00004817 SDValue ThisElt(0, 0), LastElt(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00004818 bool LastIsNonZero = (NonZeros & (1 << (i-1))) != 0;
4819 if (LastIsNonZero) {
Scott Michelfdc40a02009-02-17 22:15:04 +00004820 LastElt = DAG.getNode(ISD::ZERO_EXTEND, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004821 MVT::i16, Op.getOperand(i-1));
Evan Chengc78d3b42006-04-24 18:01:45 +00004822 }
4823 if (ThisIsNonZero) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004824 ThisElt = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i16, Op.getOperand(i));
4825 ThisElt = DAG.getNode(ISD::SHL, dl, MVT::i16,
4826 ThisElt, DAG.getConstant(8, MVT::i8));
Evan Chengc78d3b42006-04-24 18:01:45 +00004827 if (LastIsNonZero)
Owen Anderson825b72b2009-08-11 20:47:22 +00004828 ThisElt = DAG.getNode(ISD::OR, dl, MVT::i16, ThisElt, LastElt);
Evan Chengc78d3b42006-04-24 18:01:45 +00004829 } else
4830 ThisElt = LastElt;
4831
Gabor Greifba36cb52008-08-28 21:40:38 +00004832 if (ThisElt.getNode())
Owen Anderson825b72b2009-08-11 20:47:22 +00004833 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, V, ThisElt,
Chris Lattner0bd48932008-01-17 07:00:52 +00004834 DAG.getIntPtrConstant(i/2));
Evan Chengc78d3b42006-04-24 18:01:45 +00004835 }
4836 }
4837
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004838 return DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, V);
Evan Chengc78d3b42006-04-24 18:01:45 +00004839}
4840
Bill Wendlinga348c562007-03-22 18:42:45 +00004841/// LowerBuildVectorv8i16 - Custom lower build_vector of v8i16.
Evan Chengc78d3b42006-04-24 18:01:45 +00004842///
Dan Gohman475871a2008-07-27 21:46:04 +00004843static SDValue LowerBuildVectorv8i16(SDValue Op, unsigned NonZeros,
Dan Gohmand858e902010-04-17 15:26:15 +00004844 unsigned NumNonZero, unsigned NumZero,
4845 SelectionDAG &DAG,
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00004846 const X86Subtarget* Subtarget,
Dan Gohmand858e902010-04-17 15:26:15 +00004847 const TargetLowering &TLI) {
Evan Chengc78d3b42006-04-24 18:01:45 +00004848 if (NumNonZero > 4)
Dan Gohman475871a2008-07-27 21:46:04 +00004849 return SDValue();
Evan Chengc78d3b42006-04-24 18:01:45 +00004850
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004851 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00004852 SDValue V(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00004853 bool First = true;
4854 for (unsigned i = 0; i < 8; ++i) {
4855 bool isNonZero = (NonZeros & (1 << i)) != 0;
4856 if (isNonZero) {
4857 if (First) {
4858 if (NumZero)
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00004859 V = getZeroVector(MVT::v8i16, Subtarget, DAG, dl);
Evan Chengc78d3b42006-04-24 18:01:45 +00004860 else
Owen Anderson825b72b2009-08-11 20:47:22 +00004861 V = DAG.getUNDEF(MVT::v8i16);
Evan Chengc78d3b42006-04-24 18:01:45 +00004862 First = false;
4863 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004864 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004865 MVT::v8i16, V, Op.getOperand(i),
Chris Lattner0bd48932008-01-17 07:00:52 +00004866 DAG.getIntPtrConstant(i));
Evan Chengc78d3b42006-04-24 18:01:45 +00004867 }
4868 }
4869
4870 return V;
4871}
4872
Evan Chengf26ffe92008-05-29 08:22:04 +00004873/// getVShift - Return a vector logical shift node.
4874///
Owen Andersone50ed302009-08-10 22:56:29 +00004875static SDValue getVShift(bool isLeft, EVT VT, SDValue SrcOp,
Nate Begeman9008ca62009-04-27 18:41:29 +00004876 unsigned NumBits, SelectionDAG &DAG,
4877 const TargetLowering &TLI, DebugLoc dl) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00004878 assert(VT.is128BitVector() && "Unknown type for VShift");
Dale Johannesen0488fb62010-09-30 23:57:10 +00004879 EVT ShVT = MVT::v2i64;
Craig Toppered2e13d2012-01-22 19:15:14 +00004880 unsigned Opc = isLeft ? X86ISD::VSHLDQ : X86ISD::VSRLDQ;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004881 SrcOp = DAG.getNode(ISD::BITCAST, dl, ShVT, SrcOp);
4882 return DAG.getNode(ISD::BITCAST, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00004883 DAG.getNode(Opc, dl, ShVT, SrcOp,
Owen Anderson95771af2011-02-25 21:41:48 +00004884 DAG.getConstant(NumBits,
4885 TLI.getShiftAmountTy(SrcOp.getValueType()))));
Evan Chengf26ffe92008-05-29 08:22:04 +00004886}
4887
Dan Gohman475871a2008-07-27 21:46:04 +00004888SDValue
Evan Chengc3630942009-12-09 21:00:30 +00004889X86TargetLowering::LowerAsSplatVectorLoad(SDValue SrcOp, EVT VT, DebugLoc dl,
Dan Gohmand858e902010-04-17 15:26:15 +00004890 SelectionDAG &DAG) const {
Michael J. Spencerec38de22010-10-10 22:04:20 +00004891
Evan Chengc3630942009-12-09 21:00:30 +00004892 // Check if the scalar load can be widened into a vector load. And if
4893 // the address is "base + cst" see if the cst can be "absorbed" into
4894 // the shuffle mask.
4895 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(SrcOp)) {
4896 SDValue Ptr = LD->getBasePtr();
4897 if (!ISD::isNormalLoad(LD) || LD->isVolatile())
4898 return SDValue();
4899 EVT PVT = LD->getValueType(0);
4900 if (PVT != MVT::i32 && PVT != MVT::f32)
4901 return SDValue();
4902
4903 int FI = -1;
4904 int64_t Offset = 0;
4905 if (FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr)) {
4906 FI = FINode->getIndex();
4907 Offset = 0;
Chris Lattner0a9481f2011-02-13 22:25:43 +00004908 } else if (DAG.isBaseWithConstantOffset(Ptr) &&
Evan Chengc3630942009-12-09 21:00:30 +00004909 isa<FrameIndexSDNode>(Ptr.getOperand(0))) {
4910 FI = cast<FrameIndexSDNode>(Ptr.getOperand(0))->getIndex();
4911 Offset = Ptr.getConstantOperandVal(1);
4912 Ptr = Ptr.getOperand(0);
4913 } else {
4914 return SDValue();
4915 }
4916
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004917 // FIXME: 256-bit vector instructions don't require a strict alignment,
4918 // improve this code to support it better.
4919 unsigned RequiredAlign = VT.getSizeInBits()/8;
Evan Chengc3630942009-12-09 21:00:30 +00004920 SDValue Chain = LD->getChain();
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004921 // Make sure the stack object alignment is at least 16 or 32.
Evan Chengc3630942009-12-09 21:00:30 +00004922 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004923 if (DAG.InferPtrAlignment(Ptr) < RequiredAlign) {
Evan Chengc3630942009-12-09 21:00:30 +00004924 if (MFI->isFixedObjectIndex(FI)) {
Eric Christophere9625cf2010-01-23 06:02:43 +00004925 // Can't change the alignment. FIXME: It's possible to compute
4926 // the exact stack offset and reference FI + adjust offset instead.
4927 // If someone *really* cares about this. That's the way to implement it.
4928 return SDValue();
Evan Chengc3630942009-12-09 21:00:30 +00004929 } else {
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004930 MFI->setObjectAlignment(FI, RequiredAlign);
Evan Chengc3630942009-12-09 21:00:30 +00004931 }
4932 }
4933
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004934 // (Offset % 16 or 32) must be multiple of 4. Then address is then
Evan Chengc3630942009-12-09 21:00:30 +00004935 // Ptr + (Offset & ~15).
4936 if (Offset < 0)
4937 return SDValue();
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004938 if ((Offset % RequiredAlign) & 3)
Evan Chengc3630942009-12-09 21:00:30 +00004939 return SDValue();
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004940 int64_t StartOffset = Offset & ~(RequiredAlign-1);
Evan Chengc3630942009-12-09 21:00:30 +00004941 if (StartOffset)
4942 Ptr = DAG.getNode(ISD::ADD, Ptr.getDebugLoc(), Ptr.getValueType(),
4943 Ptr,DAG.getConstant(StartOffset, Ptr.getValueType()));
4944
4945 int EltNo = (Offset - StartOffset) >> 2;
Craig Topper66ddd152012-04-27 22:54:43 +00004946 unsigned NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004947
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004948 EVT NVT = EVT::getVectorVT(*DAG.getContext(), PVT, NumElems);
4949 SDValue V1 = DAG.getLoad(NVT, dl, Chain, Ptr,
Chris Lattner51abfe42010-09-21 06:02:19 +00004950 LD->getPointerInfo().getWithOffset(StartOffset),
Pete Cooperd752e0f2011-11-08 18:42:53 +00004951 false, false, false, 0);
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004952
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004953 SmallVector<int, 8> Mask;
Craig Topper66ddd152012-04-27 22:54:43 +00004954 for (unsigned i = 0; i != NumElems; ++i)
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004955 Mask.push_back(EltNo);
4956
Craig Toppercc3000632012-01-30 07:50:31 +00004957 return DAG.getVectorShuffle(NVT, dl, V1, DAG.getUNDEF(NVT), &Mask[0]);
Evan Chengc3630942009-12-09 21:00:30 +00004958 }
4959
4960 return SDValue();
4961}
4962
Michael J. Spencerec38de22010-10-10 22:04:20 +00004963/// EltsFromConsecutiveLoads - Given the initializing elements 'Elts' of a
4964/// vector of type 'VT', see if the elements can be replaced by a single large
Nate Begeman1449f292010-03-24 22:19:06 +00004965/// load which has the same value as a build_vector whose operands are 'elts'.
4966///
4967/// Example: <load i32 *a, load i32 *a+4, undef, undef> -> zextload a
Michael J. Spencerec38de22010-10-10 22:04:20 +00004968///
Nate Begeman1449f292010-03-24 22:19:06 +00004969/// FIXME: we'd also like to handle the case where the last elements are zero
4970/// rather than undef via VZEXT_LOAD, but we do not detect that case today.
4971/// There's even a handy isZeroNode for that purpose.
Nate Begemanfdea31a2010-03-24 20:49:50 +00004972static SDValue EltsFromConsecutiveLoads(EVT VT, SmallVectorImpl<SDValue> &Elts,
Chris Lattner88641552010-09-22 00:34:38 +00004973 DebugLoc &DL, SelectionDAG &DAG) {
Nate Begemanfdea31a2010-03-24 20:49:50 +00004974 EVT EltVT = VT.getVectorElementType();
4975 unsigned NumElems = Elts.size();
Michael J. Spencerec38de22010-10-10 22:04:20 +00004976
Nate Begemanfdea31a2010-03-24 20:49:50 +00004977 LoadSDNode *LDBase = NULL;
4978 unsigned LastLoadedElt = -1U;
Michael J. Spencerec38de22010-10-10 22:04:20 +00004979
Nate Begeman1449f292010-03-24 22:19:06 +00004980 // For each element in the initializer, see if we've found a load or an undef.
Michael J. Spencerec38de22010-10-10 22:04:20 +00004981 // If we don't find an initial load element, or later load elements are
Nate Begeman1449f292010-03-24 22:19:06 +00004982 // non-consecutive, bail out.
Nate Begemanfdea31a2010-03-24 20:49:50 +00004983 for (unsigned i = 0; i < NumElems; ++i) {
4984 SDValue Elt = Elts[i];
Michael J. Spencerec38de22010-10-10 22:04:20 +00004985
Nate Begemanfdea31a2010-03-24 20:49:50 +00004986 if (!Elt.getNode() ||
4987 (Elt.getOpcode() != ISD::UNDEF && !ISD::isNON_EXTLoad(Elt.getNode())))
4988 return SDValue();
4989 if (!LDBase) {
4990 if (Elt.getNode()->getOpcode() == ISD::UNDEF)
4991 return SDValue();
4992 LDBase = cast<LoadSDNode>(Elt.getNode());
4993 LastLoadedElt = i;
4994 continue;
4995 }
4996 if (Elt.getOpcode() == ISD::UNDEF)
4997 continue;
4998
4999 LoadSDNode *LD = cast<LoadSDNode>(Elt);
5000 if (!DAG.isConsecutiveLoad(LD, LDBase, EltVT.getSizeInBits()/8, i))
5001 return SDValue();
5002 LastLoadedElt = i;
5003 }
Nate Begeman1449f292010-03-24 22:19:06 +00005004
5005 // If we have found an entire vector of loads and undefs, then return a large
5006 // load of the entire vector width starting at the base pointer. If we found
5007 // consecutive loads for the low half, generate a vzext_load node.
Nate Begemanfdea31a2010-03-24 20:49:50 +00005008 if (LastLoadedElt == NumElems - 1) {
5009 if (DAG.InferPtrAlignment(LDBase->getBasePtr()) >= 16)
Chris Lattner88641552010-09-22 00:34:38 +00005010 return DAG.getLoad(VT, DL, LDBase->getChain(), LDBase->getBasePtr(),
Chris Lattner51abfe42010-09-21 06:02:19 +00005011 LDBase->getPointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00005012 LDBase->isVolatile(), LDBase->isNonTemporal(),
5013 LDBase->isInvariant(), 0);
Chris Lattner88641552010-09-22 00:34:38 +00005014 return DAG.getLoad(VT, DL, LDBase->getChain(), LDBase->getBasePtr(),
Chris Lattner51abfe42010-09-21 06:02:19 +00005015 LDBase->getPointerInfo(),
Nate Begemanfdea31a2010-03-24 20:49:50 +00005016 LDBase->isVolatile(), LDBase->isNonTemporal(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00005017 LDBase->isInvariant(), LDBase->getAlignment());
Craig Topper69947b92012-04-23 06:57:04 +00005018 }
5019 if (NumElems == 4 && LastLoadedElt == 1 &&
5020 DAG.getTargetLoweringInfo().isTypeLegal(MVT::v2i64)) {
Nate Begemanfdea31a2010-03-24 20:49:50 +00005021 SDVTList Tys = DAG.getVTList(MVT::v2i64, MVT::Other);
5022 SDValue Ops[] = { LDBase->getChain(), LDBase->getBasePtr() };
Eli Friedman322ea082011-09-14 23:42:45 +00005023 SDValue ResNode =
5024 DAG.getMemIntrinsicNode(X86ISD::VZEXT_LOAD, DL, Tys, Ops, 2, MVT::i64,
5025 LDBase->getPointerInfo(),
5026 LDBase->getAlignment(),
5027 false/*isVolatile*/, true/*ReadMem*/,
5028 false/*WriteMem*/);
Manman Ren2b7a2e82012-08-31 23:16:57 +00005029
5030 // Make sure the newly-created LOAD is in the same position as LDBase in
5031 // terms of dependency. We create a TokenFactor for LDBase and ResNode, and
5032 // update uses of LDBase's output chain to use the TokenFactor.
5033 if (LDBase->hasAnyUseOfValue(1)) {
5034 SDValue NewChain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other,
5035 SDValue(LDBase, 1), SDValue(ResNode.getNode(), 1));
5036 DAG.ReplaceAllUsesOfValueWith(SDValue(LDBase, 1), NewChain);
5037 DAG.UpdateNodeOperands(NewChain.getNode(), SDValue(LDBase, 1),
5038 SDValue(ResNode.getNode(), 1));
5039 }
5040
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005041 return DAG.getNode(ISD::BITCAST, DL, VT, ResNode);
Nate Begemanfdea31a2010-03-24 20:49:50 +00005042 }
5043 return SDValue();
5044}
5045
Nadav Rotem9d68b062012-04-08 12:54:54 +00005046/// LowerVectorBroadcast - Attempt to use the vbroadcast instruction
5047/// to generate a splat value for the following cases:
5048/// 1. A splat BUILD_VECTOR which uses a single scalar load, or a constant.
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005049/// 2. A splat shuffle which uses a scalar_to_vector node which comes from
Nadav Rotem9d68b062012-04-08 12:54:54 +00005050/// a scalar load, or a constant.
5051/// The VBROADCAST node is returned when a pattern is found,
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00005052/// or SDValue() otherwise.
Nadav Rotem154819d2012-04-09 07:45:58 +00005053SDValue
Craig Topper55b24052012-09-11 06:15:32 +00005054X86TargetLowering::LowerVectorBroadcast(SDValue Op, SelectionDAG &DAG) const {
Craig Toppera9376332012-01-10 08:23:59 +00005055 if (!Subtarget->hasAVX())
5056 return SDValue();
5057
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005058 EVT VT = Op.getValueType();
Nadav Rotem154819d2012-04-09 07:45:58 +00005059 DebugLoc dl = Op.getDebugLoc();
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005060
Craig Topper5da8a802012-05-04 05:49:51 +00005061 assert((VT.is128BitVector() || VT.is256BitVector()) &&
5062 "Unsupported vector type for broadcast.");
5063
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005064 SDValue Ld;
Nadav Rotem9d68b062012-04-08 12:54:54 +00005065 bool ConstSplatVal;
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005066
Nadav Rotem9d68b062012-04-08 12:54:54 +00005067 switch (Op.getOpcode()) {
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005068 default:
5069 // Unknown pattern found.
5070 return SDValue();
5071
5072 case ISD::BUILD_VECTOR: {
5073 // The BUILD_VECTOR node must be a splat.
Nadav Rotem9d68b062012-04-08 12:54:54 +00005074 if (!isSplatVector(Op.getNode()))
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005075 return SDValue();
5076
Nadav Rotem9d68b062012-04-08 12:54:54 +00005077 Ld = Op.getOperand(0);
5078 ConstSplatVal = (Ld.getOpcode() == ISD::Constant ||
5079 Ld.getOpcode() == ISD::ConstantFP);
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00005080
5081 // The suspected load node has several users. Make sure that all
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005082 // of its users are from the BUILD_VECTOR node.
Nadav Rotem9d68b062012-04-08 12:54:54 +00005083 // Constants may have multiple users.
5084 if (!ConstSplatVal && !Ld->hasNUsesOfValue(VT.getVectorNumElements(), 0))
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005085 return SDValue();
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00005086 break;
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005087 }
5088
5089 case ISD::VECTOR_SHUFFLE: {
5090 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
5091
5092 // Shuffles must have a splat mask where the first element is
5093 // broadcasted.
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00005094 if ((!SVOp->isSplat()) || SVOp->getMaskElt(0) != 0)
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005095 return SDValue();
5096
5097 SDValue Sc = Op.getOperand(0);
Nadav Rotemb88e8dd2012-05-10 12:50:02 +00005098 if (Sc.getOpcode() != ISD::SCALAR_TO_VECTOR &&
Elena Demikhovsky8f40f7b2012-07-01 06:12:26 +00005099 Sc.getOpcode() != ISD::BUILD_VECTOR) {
5100
5101 if (!Subtarget->hasAVX2())
5102 return SDValue();
5103
5104 // Use the register form of the broadcast instruction available on AVX2.
5105 if (VT.is256BitVector())
5106 Sc = Extract128BitVector(Sc, 0, DAG, dl);
5107 return DAG.getNode(X86ISD::VBROADCAST, dl, VT, Sc);
5108 }
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005109
5110 Ld = Sc.getOperand(0);
Nadav Rotem9d68b062012-04-08 12:54:54 +00005111 ConstSplatVal = (Ld.getOpcode() == ISD::Constant ||
Nadav Rotem154819d2012-04-09 07:45:58 +00005112 Ld.getOpcode() == ISD::ConstantFP);
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005113
5114 // The scalar_to_vector node and the suspected
5115 // load node must have exactly one user.
Nadav Rotem9d68b062012-04-08 12:54:54 +00005116 // Constants may have multiple users.
5117 if (!ConstSplatVal && (!Sc.hasOneUse() || !Ld.hasOneUse()))
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005118 return SDValue();
5119 break;
5120 }
5121 }
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00005122
Craig Topper7a9a28b2012-08-12 02:23:29 +00005123 bool Is256 = VT.is256BitVector();
Nadav Rotem9d68b062012-04-08 12:54:54 +00005124
5125 // Handle the broadcasting a single constant scalar from the constant pool
5126 // into a vector. On Sandybridge it is still better to load a constant vector
5127 // from the constant pool and not to broadcast it from a scalar.
5128 if (ConstSplatVal && Subtarget->hasAVX2()) {
5129 EVT CVT = Ld.getValueType();
5130 assert(!CVT.isVector() && "Must not broadcast a vector type");
5131 unsigned ScalarSize = CVT.getSizeInBits();
5132
Craig Topper5da8a802012-05-04 05:49:51 +00005133 if (ScalarSize == 32 || (Is256 && ScalarSize == 64)) {
Nadav Rotem9d68b062012-04-08 12:54:54 +00005134 const Constant *C = 0;
5135 if (ConstantSDNode *CI = dyn_cast<ConstantSDNode>(Ld))
5136 C = CI->getConstantIntValue();
5137 else if (ConstantFPSDNode *CF = dyn_cast<ConstantFPSDNode>(Ld))
5138 C = CF->getConstantFPValue();
5139
5140 assert(C && "Invalid constant type");
5141
Nadav Rotem154819d2012-04-09 07:45:58 +00005142 SDValue CP = DAG.getConstantPool(C, getPointerTy());
Nadav Rotem9d68b062012-04-08 12:54:54 +00005143 unsigned Alignment = cast<ConstantPoolSDNode>(CP)->getAlignment();
Nadav Rotem154819d2012-04-09 07:45:58 +00005144 Ld = DAG.getLoad(CVT, dl, DAG.getEntryNode(), CP,
Craig Topper6643d9c2012-05-04 06:18:33 +00005145 MachinePointerInfo::getConstantPool(),
5146 false, false, false, Alignment);
Nadav Rotem9d68b062012-04-08 12:54:54 +00005147
Nadav Rotem9d68b062012-04-08 12:54:54 +00005148 return DAG.getNode(X86ISD::VBROADCAST, dl, VT, Ld);
5149 }
5150 }
5151
Nadav Rotem4fc8a5d2012-05-19 19:57:37 +00005152 bool IsLoad = ISD::isNormalLoad(Ld.getNode());
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005153 unsigned ScalarSize = Ld.getValueType().getSizeInBits();
5154
Nadav Rotem4fc8a5d2012-05-19 19:57:37 +00005155 // Handle AVX2 in-register broadcasts.
5156 if (!IsLoad && Subtarget->hasAVX2() &&
5157 (ScalarSize == 32 || (Is256 && ScalarSize == 64)))
5158 return DAG.getNode(X86ISD::VBROADCAST, dl, VT, Ld);
5159
5160 // The scalar source must be a normal load.
5161 if (!IsLoad)
5162 return SDValue();
5163
Craig Topper5da8a802012-05-04 05:49:51 +00005164 if (ScalarSize == 32 || (Is256 && ScalarSize == 64))
Nadav Rotem9d68b062012-04-08 12:54:54 +00005165 return DAG.getNode(X86ISD::VBROADCAST, dl, VT, Ld);
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005166
Craig Toppera9376332012-01-10 08:23:59 +00005167 // The integer check is needed for the 64-bit into 128-bit so it doesn't match
Craig Topper5da8a802012-05-04 05:49:51 +00005168 // double since there is no vbroadcastsd xmm
Craig Toppera9376332012-01-10 08:23:59 +00005169 if (Subtarget->hasAVX2() && Ld.getValueType().isInteger()) {
Craig Topper5da8a802012-05-04 05:49:51 +00005170 if (ScalarSize == 8 || ScalarSize == 16 || ScalarSize == 64)
Nadav Rotem9d68b062012-04-08 12:54:54 +00005171 return DAG.getNode(X86ISD::VBROADCAST, dl, VT, Ld);
Craig Toppera9376332012-01-10 08:23:59 +00005172 }
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00005173
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005174 // Unsupported broadcast.
5175 return SDValue();
5176}
5177
Evan Chengc3630942009-12-09 21:00:30 +00005178SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00005179X86TargetLowering::LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005180 DebugLoc dl = Op.getDebugLoc();
David Greenea5f26012011-02-07 19:36:54 +00005181
David Greenef125a292011-02-08 19:04:41 +00005182 EVT VT = Op.getValueType();
5183 EVT ExtVT = VT.getVectorElementType();
David Greenef125a292011-02-08 19:04:41 +00005184 unsigned NumElems = Op.getNumOperands();
5185
Bruno Cardoso Lopes531f19f2011-08-01 19:51:53 +00005186 // Vectors containing all zeros can be matched by pxor and xorps later
5187 if (ISD::isBuildVectorAllZeros(Op.getNode())) {
5188 // Canonicalize this to <4 x i32> to 1) ensure the zero vectors are CSE'd
5189 // and 2) ensure that i64 scalars are eliminated on x86-32 hosts.
Craig Topper07a27622012-01-22 03:07:48 +00005190 if (VT == MVT::v4i32 || VT == MVT::v8i32)
Chris Lattner8a594482007-11-25 00:24:49 +00005191 return Op;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005192
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00005193 return getZeroVector(VT, Subtarget, DAG, dl);
Chris Lattner8a594482007-11-25 00:24:49 +00005194 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00005195
Bruno Cardoso Lopes531f19f2011-08-01 19:51:53 +00005196 // Vectors containing all ones can be matched by pcmpeqd on 128-bit width
Craig Topper745a86b2011-11-19 22:34:59 +00005197 // vectors or broken into v4i32 operations on 256-bit vectors. AVX2 can use
5198 // vpcmpeqd on 256-bit vectors.
Bruno Cardoso Lopes531f19f2011-08-01 19:51:53 +00005199 if (ISD::isBuildVectorAllOnes(Op.getNode())) {
Craig Topper07a27622012-01-22 03:07:48 +00005200 if (VT == MVT::v4i32 || (VT == MVT::v8i32 && Subtarget->hasAVX2()))
Bruno Cardoso Lopes531f19f2011-08-01 19:51:53 +00005201 return Op;
5202
Craig Topper07a27622012-01-22 03:07:48 +00005203 return getOnesVector(VT, Subtarget->hasAVX2(), DAG, dl);
Bruno Cardoso Lopes531f19f2011-08-01 19:51:53 +00005204 }
5205
Nadav Rotem154819d2012-04-09 07:45:58 +00005206 SDValue Broadcast = LowerVectorBroadcast(Op, DAG);
Nadav Rotem9d68b062012-04-08 12:54:54 +00005207 if (Broadcast.getNode())
5208 return Broadcast;
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005209
Owen Andersone50ed302009-08-10 22:56:29 +00005210 unsigned EVTBits = ExtVT.getSizeInBits();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005211
Evan Cheng0db9fe62006-04-25 20:13:52 +00005212 unsigned NumZero = 0;
5213 unsigned NumNonZero = 0;
5214 unsigned NonZeros = 0;
Chris Lattnerc9517fb2008-03-08 22:48:29 +00005215 bool IsAllConstants = true;
Dan Gohman475871a2008-07-27 21:46:04 +00005216 SmallSet<SDValue, 8> Values;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005217 for (unsigned i = 0; i < NumElems; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00005218 SDValue Elt = Op.getOperand(i);
Evan Chengdb2d5242007-12-12 06:45:40 +00005219 if (Elt.getOpcode() == ISD::UNDEF)
5220 continue;
5221 Values.insert(Elt);
5222 if (Elt.getOpcode() != ISD::Constant &&
5223 Elt.getOpcode() != ISD::ConstantFP)
Chris Lattnerc9517fb2008-03-08 22:48:29 +00005224 IsAllConstants = false;
Evan Cheng37b73872009-07-30 08:33:02 +00005225 if (X86::isZeroNode(Elt))
Evan Chengdb2d5242007-12-12 06:45:40 +00005226 NumZero++;
5227 else {
5228 NonZeros |= (1 << i);
5229 NumNonZero++;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005230 }
5231 }
5232
Chris Lattner97a2a562010-08-26 05:24:29 +00005233 // All undef vector. Return an UNDEF. All zero vectors were handled above.
5234 if (NumNonZero == 0)
Dale Johannesene8d72302009-02-06 23:05:02 +00005235 return DAG.getUNDEF(VT);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005236
Chris Lattner67f453a2008-03-09 05:42:06 +00005237 // Special case for single non-zero, non-undef, element.
Eli Friedman10415532009-06-06 06:05:10 +00005238 if (NumNonZero == 1) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00005239 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dan Gohman475871a2008-07-27 21:46:04 +00005240 SDValue Item = Op.getOperand(Idx);
Scott Michelfdc40a02009-02-17 22:15:04 +00005241
Chris Lattner62098042008-03-09 01:05:04 +00005242 // If this is an insertion of an i64 value on x86-32, and if the top bits of
5243 // the value are obviously zero, truncate the value to i32 and do the
5244 // insertion that way. Only do this if the value is non-constant or if the
5245 // value is a constant being inserted into element 0. It is cheaper to do
5246 // a constant pool load than it is to do a movd + shuffle.
Owen Anderson825b72b2009-08-11 20:47:22 +00005247 if (ExtVT == MVT::i64 && !Subtarget->is64Bit() &&
Chris Lattner62098042008-03-09 01:05:04 +00005248 (!IsAllConstants || Idx == 0)) {
5249 if (DAG.MaskedValueIsZero(Item, APInt::getBitsSet(64, 32, 64))) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00005250 // Handle SSE only.
5251 assert(VT == MVT::v2i64 && "Expected an SSE value type!");
5252 EVT VecVT = MVT::v4i32;
5253 unsigned VecElts = 4;
Scott Michelfdc40a02009-02-17 22:15:04 +00005254
Chris Lattner62098042008-03-09 01:05:04 +00005255 // Truncate the value (which may itself be a constant) to i32, and
5256 // convert it to a vector with movd (S2V+shuffle to zero extend).
Owen Anderson825b72b2009-08-11 20:47:22 +00005257 Item = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Item);
Dale Johannesenace16102009-02-03 19:33:06 +00005258 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VecVT, Item);
Craig Topper12216172012-01-13 08:12:35 +00005259 Item = getShuffleVectorZeroOrUndef(Item, 0, true, Subtarget, DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +00005260
Chris Lattner62098042008-03-09 01:05:04 +00005261 // Now we have our 32-bit value zero extended in the low element of
5262 // a vector. If Idx != 0, swizzle it into place.
5263 if (Idx != 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005264 SmallVector<int, 4> Mask;
5265 Mask.push_back(Idx);
5266 for (unsigned i = 1; i != VecElts; ++i)
5267 Mask.push_back(i);
Craig Topperdf966f62012-04-22 19:17:57 +00005268 Item = DAG.getVectorShuffle(VecVT, dl, Item, DAG.getUNDEF(VecVT),
Nate Begeman9008ca62009-04-27 18:41:29 +00005269 &Mask[0]);
Chris Lattner62098042008-03-09 01:05:04 +00005270 }
Craig Topper07a27622012-01-22 03:07:48 +00005271 return DAG.getNode(ISD::BITCAST, dl, VT, Item);
Chris Lattner62098042008-03-09 01:05:04 +00005272 }
5273 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005274
Chris Lattner19f79692008-03-08 22:59:52 +00005275 // If we have a constant or non-constant insertion into the low element of
5276 // a vector, we can do this with SCALAR_TO_VECTOR + shuffle of zero into
5277 // the rest of the elements. This will be matched as movd/movq/movss/movsd
Eli Friedman10415532009-06-06 06:05:10 +00005278 // depending on what the source datatype is.
5279 if (Idx == 0) {
Craig Topperd62c16e2011-12-29 03:20:51 +00005280 if (NumZero == 0)
Eli Friedman10415532009-06-06 06:05:10 +00005281 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
Craig Topperd62c16e2011-12-29 03:20:51 +00005282
5283 if (ExtVT == MVT::i32 || ExtVT == MVT::f32 || ExtVT == MVT::f64 ||
Owen Anderson825b72b2009-08-11 20:47:22 +00005284 (ExtVT == MVT::i64 && Subtarget->is64Bit())) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00005285 if (VT.is256BitVector()) {
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00005286 SDValue ZeroVec = getZeroVector(VT, Subtarget, DAG, dl);
Nadav Rotem394a1f52012-01-11 14:07:51 +00005287 return DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, ZeroVec,
5288 Item, DAG.getIntPtrConstant(0));
Elena Demikhovsky021c0a22011-12-28 08:14:01 +00005289 }
Craig Topper7a9a28b2012-08-12 02:23:29 +00005290 assert(VT.is128BitVector() && "Expected an SSE value type!");
Eli Friedman10415532009-06-06 06:05:10 +00005291 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
5292 // Turn it into a MOVL (i.e. movss, movsd, or movd) to a zero vector.
Craig Topper12216172012-01-13 08:12:35 +00005293 return getShuffleVectorZeroOrUndef(Item, 0, true, Subtarget, DAG);
Craig Topperd62c16e2011-12-29 03:20:51 +00005294 }
5295
5296 if (ExtVT == MVT::i16 || ExtVT == MVT::i8) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005297 Item = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, Item);
Craig Topper3224e6b2011-12-29 03:09:33 +00005298 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32, Item);
Craig Topper7a9a28b2012-08-12 02:23:29 +00005299 if (VT.is256BitVector()) {
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00005300 SDValue ZeroVec = getZeroVector(MVT::v8i32, Subtarget, DAG, dl);
Craig Topperb14940a2012-04-22 20:55:18 +00005301 Item = Insert128BitVector(ZeroVec, Item, 0, DAG, dl);
Craig Topper19ec2a92011-12-29 03:34:54 +00005302 } else {
Craig Topper7a9a28b2012-08-12 02:23:29 +00005303 assert(VT.is128BitVector() && "Expected an SSE value type!");
Craig Topper12216172012-01-13 08:12:35 +00005304 Item = getShuffleVectorZeroOrUndef(Item, 0, true, Subtarget, DAG);
Craig Topper19ec2a92011-12-29 03:34:54 +00005305 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005306 return DAG.getNode(ISD::BITCAST, dl, VT, Item);
Eli Friedman10415532009-06-06 06:05:10 +00005307 }
Chris Lattnerc9517fb2008-03-08 22:48:29 +00005308 }
Evan Chengf26ffe92008-05-29 08:22:04 +00005309
5310 // Is it a vector logical left shift?
5311 if (NumElems == 2 && Idx == 1 &&
Evan Cheng37b73872009-07-30 08:33:02 +00005312 X86::isZeroNode(Op.getOperand(0)) &&
5313 !X86::isZeroNode(Op.getOperand(1))) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00005314 unsigned NumBits = VT.getSizeInBits();
Evan Chengf26ffe92008-05-29 08:22:04 +00005315 return getVShift(true, VT,
Scott Michelfdc40a02009-02-17 22:15:04 +00005316 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Dale Johannesenb300d2a2009-02-07 00:55:49 +00005317 VT, Op.getOperand(1)),
Dale Johannesenace16102009-02-03 19:33:06 +00005318 NumBits/2, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00005319 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005320
Chris Lattnerc9517fb2008-03-08 22:48:29 +00005321 if (IsAllConstants) // Otherwise, it's better to do a constpool load.
Dan Gohman475871a2008-07-27 21:46:04 +00005322 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005323
Chris Lattner19f79692008-03-08 22:59:52 +00005324 // Otherwise, if this is a vector with i32 or f32 elements, and the element
5325 // is a non-constant being inserted into an element other than the low one,
5326 // we can't use a constant pool load. Instead, use SCALAR_TO_VECTOR (aka
5327 // movd/movss) to move this into the low element, then shuffle it into
5328 // place.
Evan Cheng0db9fe62006-04-25 20:13:52 +00005329 if (EVTBits == 32) {
Dale Johannesenace16102009-02-03 19:33:06 +00005330 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
Scott Michelfdc40a02009-02-17 22:15:04 +00005331
Evan Cheng0db9fe62006-04-25 20:13:52 +00005332 // Turn it into a shuffle of zero and zero-extended scalar to vector.
Craig Topper12216172012-01-13 08:12:35 +00005333 Item = getShuffleVectorZeroOrUndef(Item, 0, NumZero > 0, Subtarget, DAG);
Nate Begeman9008ca62009-04-27 18:41:29 +00005334 SmallVector<int, 8> MaskVec;
Craig Topper31a207a2012-05-04 06:39:13 +00005335 for (unsigned i = 0; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00005336 MaskVec.push_back(i == Idx ? 0 : 1);
5337 return DAG.getVectorShuffle(VT, dl, Item, DAG.getUNDEF(VT), &MaskVec[0]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005338 }
5339 }
5340
Chris Lattner67f453a2008-03-09 05:42:06 +00005341 // Splat is obviously ok. Let legalizer expand it to a shuffle.
Evan Chengc3630942009-12-09 21:00:30 +00005342 if (Values.size() == 1) {
5343 if (EVTBits == 32) {
5344 // Instead of a shuffle like this:
5345 // shuffle (scalar_to_vector (load (ptr + 4))), undef, <0, 0, 0, 0>
5346 // Check if it's possible to issue this instead.
5347 // shuffle (vload ptr)), undef, <1, 1, 1, 1>
5348 unsigned Idx = CountTrailingZeros_32(NonZeros);
5349 SDValue Item = Op.getOperand(Idx);
5350 if (Op.getNode()->isOnlyUserOf(Item.getNode()))
5351 return LowerAsSplatVectorLoad(Item, VT, dl, DAG);
5352 }
Dan Gohman475871a2008-07-27 21:46:04 +00005353 return SDValue();
Evan Chengc3630942009-12-09 21:00:30 +00005354 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005355
Dan Gohmana3941172007-07-24 22:55:08 +00005356 // A vector full of immediates; various special cases are already
5357 // handled, so this is best done with a single constant-pool load.
Chris Lattnerc9517fb2008-03-08 22:48:29 +00005358 if (IsAllConstants)
Dan Gohman475871a2008-07-27 21:46:04 +00005359 return SDValue();
Dan Gohmana3941172007-07-24 22:55:08 +00005360
Bruno Cardoso Lopes6683efb2011-07-22 00:15:07 +00005361 // For AVX-length vectors, build the individual 128-bit pieces and use
5362 // shuffles to put them in place.
Craig Topper7a9a28b2012-08-12 02:23:29 +00005363 if (VT.is256BitVector()) {
Bruno Cardoso Lopes6683efb2011-07-22 00:15:07 +00005364 SmallVector<SDValue, 32> V;
Craig Topperfa5b70e2012-02-03 06:32:21 +00005365 for (unsigned i = 0; i != NumElems; ++i)
Bruno Cardoso Lopes6683efb2011-07-22 00:15:07 +00005366 V.push_back(Op.getOperand(i));
5367
5368 EVT HVT = EVT::getVectorVT(*DAG.getContext(), ExtVT, NumElems/2);
5369
5370 // Build both the lower and upper subvector.
5371 SDValue Lower = DAG.getNode(ISD::BUILD_VECTOR, dl, HVT, &V[0], NumElems/2);
5372 SDValue Upper = DAG.getNode(ISD::BUILD_VECTOR, dl, HVT, &V[NumElems / 2],
5373 NumElems/2);
5374
5375 // Recreate the wider vector with the lower and upper part.
Craig Topper4c7972d2012-04-22 18:15:59 +00005376 return Concat128BitVectors(Lower, Upper, VT, NumElems, DAG, dl);
Bruno Cardoso Lopes6683efb2011-07-22 00:15:07 +00005377 }
5378
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00005379 // Let legalizer expand 2-wide build_vectors.
Evan Cheng7e2ff772008-05-08 00:57:18 +00005380 if (EVTBits == 64) {
5381 if (NumNonZero == 1) {
5382 // One half is zero or undef.
5383 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dale Johannesenace16102009-02-03 19:33:06 +00005384 SDValue V2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT,
Evan Cheng7e2ff772008-05-08 00:57:18 +00005385 Op.getOperand(Idx));
Craig Topper12216172012-01-13 08:12:35 +00005386 return getShuffleVectorZeroOrUndef(V2, Idx, true, Subtarget, DAG);
Evan Cheng7e2ff772008-05-08 00:57:18 +00005387 }
Dan Gohman475871a2008-07-27 21:46:04 +00005388 return SDValue();
Evan Cheng7e2ff772008-05-08 00:57:18 +00005389 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00005390
5391 // If element VT is < 32 bits, convert it to inserts into a zero vector.
Bill Wendling826f36f2007-03-28 00:57:11 +00005392 if (EVTBits == 8 && NumElems == 16) {
Dan Gohman475871a2008-07-27 21:46:04 +00005393 SDValue V = LowerBuildVectorv16i8(Op, NonZeros,NumNonZero,NumZero, DAG,
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00005394 Subtarget, *this);
Gabor Greifba36cb52008-08-28 21:40:38 +00005395 if (V.getNode()) return V;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005396 }
5397
Bill Wendling826f36f2007-03-28 00:57:11 +00005398 if (EVTBits == 16 && NumElems == 8) {
Dan Gohman475871a2008-07-27 21:46:04 +00005399 SDValue V = LowerBuildVectorv8i16(Op, NonZeros,NumNonZero,NumZero, DAG,
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00005400 Subtarget, *this);
Gabor Greifba36cb52008-08-28 21:40:38 +00005401 if (V.getNode()) return V;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005402 }
5403
5404 // If element VT is == 32 bits, turn it into a number of shuffles.
Benjamin Kramer9c683542012-01-30 15:16:21 +00005405 SmallVector<SDValue, 8> V(NumElems);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005406 if (NumElems == 4 && NumZero > 0) {
5407 for (unsigned i = 0; i < 4; ++i) {
5408 bool isZero = !(NonZeros & (1 << i));
5409 if (isZero)
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00005410 V[i] = getZeroVector(VT, Subtarget, DAG, dl);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005411 else
Dale Johannesenace16102009-02-03 19:33:06 +00005412 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(i));
Evan Cheng0db9fe62006-04-25 20:13:52 +00005413 }
5414
5415 for (unsigned i = 0; i < 2; ++i) {
5416 switch ((NonZeros & (0x3 << i*2)) >> (i*2)) {
5417 default: break;
5418 case 0:
5419 V[i] = V[i*2]; // Must be a zero vector.
5420 break;
5421 case 1:
Nate Begeman9008ca62009-04-27 18:41:29 +00005422 V[i] = getMOVL(DAG, dl, VT, V[i*2+1], V[i*2]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005423 break;
5424 case 2:
Nate Begeman9008ca62009-04-27 18:41:29 +00005425 V[i] = getMOVL(DAG, dl, VT, V[i*2], V[i*2+1]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005426 break;
5427 case 3:
Nate Begeman9008ca62009-04-27 18:41:29 +00005428 V[i] = getUnpackl(DAG, dl, VT, V[i*2], V[i*2+1]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005429 break;
5430 }
5431 }
5432
Benjamin Kramer9c683542012-01-30 15:16:21 +00005433 bool Reverse1 = (NonZeros & 0x3) == 2;
5434 bool Reverse2 = ((NonZeros & (0x3 << 2)) >> 2) == 2;
5435 int MaskVec[] = {
5436 Reverse1 ? 1 : 0,
5437 Reverse1 ? 0 : 1,
Benjamin Kramer630ecf02012-01-30 20:01:35 +00005438 static_cast<int>(Reverse2 ? NumElems+1 : NumElems),
5439 static_cast<int>(Reverse2 ? NumElems : NumElems+1)
Benjamin Kramer9c683542012-01-30 15:16:21 +00005440 };
Nate Begeman9008ca62009-04-27 18:41:29 +00005441 return DAG.getVectorShuffle(VT, dl, V[0], V[1], &MaskVec[0]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005442 }
5443
Craig Topper7a9a28b2012-08-12 02:23:29 +00005444 if (Values.size() > 1 && VT.is128BitVector()) {
Nate Begemanfdea31a2010-03-24 20:49:50 +00005445 // Check for a build vector of consecutive loads.
5446 for (unsigned i = 0; i < NumElems; ++i)
5447 V[i] = Op.getOperand(i);
Michael J. Spencerec38de22010-10-10 22:04:20 +00005448
Nate Begemanfdea31a2010-03-24 20:49:50 +00005449 // Check for elements which are consecutive loads.
5450 SDValue LD = EltsFromConsecutiveLoads(VT, V, dl, DAG);
5451 if (LD.getNode())
5452 return LD;
Michael J. Spencerec38de22010-10-10 22:04:20 +00005453
5454 // For SSE 4.1, use insertps to put the high elements into the low element.
Craig Topperd0a31172012-01-10 06:37:29 +00005455 if (getSubtarget()->hasSSE41()) {
Chris Lattner24faf612010-08-28 17:59:08 +00005456 SDValue Result;
5457 if (Op.getOperand(0).getOpcode() != ISD::UNDEF)
5458 Result = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(0));
5459 else
5460 Result = DAG.getUNDEF(VT);
Michael J. Spencerec38de22010-10-10 22:04:20 +00005461
Chris Lattner24faf612010-08-28 17:59:08 +00005462 for (unsigned i = 1; i < NumElems; ++i) {
5463 if (Op.getOperand(i).getOpcode() == ISD::UNDEF) continue;
5464 Result = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, Result,
Nate Begeman9008ca62009-04-27 18:41:29 +00005465 Op.getOperand(i), DAG.getIntPtrConstant(i));
Chris Lattner24faf612010-08-28 17:59:08 +00005466 }
5467 return Result;
Nate Begeman9008ca62009-04-27 18:41:29 +00005468 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00005469
Chris Lattner6e80e442010-08-28 17:15:43 +00005470 // Otherwise, expand into a number of unpckl*, start by extending each of
5471 // our (non-undef) elements to the full vector width with the element in the
5472 // bottom slot of the vector (which generates no code for SSE).
5473 for (unsigned i = 0; i < NumElems; ++i) {
5474 if (Op.getOperand(i).getOpcode() != ISD::UNDEF)
5475 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(i));
5476 else
5477 V[i] = DAG.getUNDEF(VT);
5478 }
5479
5480 // Next, we iteratively mix elements, e.g. for v4f32:
Evan Cheng0db9fe62006-04-25 20:13:52 +00005481 // Step 1: unpcklps 0, 2 ==> X: <?, ?, 2, 0>
5482 // : unpcklps 1, 3 ==> Y: <?, ?, 3, 1>
5483 // Step 2: unpcklps X, Y ==> <3, 2, 1, 0>
Chris Lattner6e80e442010-08-28 17:15:43 +00005484 unsigned EltStride = NumElems >> 1;
5485 while (EltStride != 0) {
Chris Lattner3ddcc432010-08-28 17:28:30 +00005486 for (unsigned i = 0; i < EltStride; ++i) {
5487 // If V[i+EltStride] is undef and this is the first round of mixing,
5488 // then it is safe to just drop this shuffle: V[i] is already in the
5489 // right place, the one element (since it's the first round) being
5490 // inserted as undef can be dropped. This isn't safe for successive
5491 // rounds because they will permute elements within both vectors.
5492 if (V[i+EltStride].getOpcode() == ISD::UNDEF &&
5493 EltStride == NumElems/2)
5494 continue;
Michael J. Spencerec38de22010-10-10 22:04:20 +00005495
Chris Lattner6e80e442010-08-28 17:15:43 +00005496 V[i] = getUnpackl(DAG, dl, VT, V[i], V[i + EltStride]);
Chris Lattner3ddcc432010-08-28 17:28:30 +00005497 }
Chris Lattner6e80e442010-08-28 17:15:43 +00005498 EltStride >>= 1;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005499 }
5500 return V[0];
5501 }
Dan Gohman475871a2008-07-27 21:46:04 +00005502 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005503}
5504
Bruno Cardoso Lopes8af24512011-08-01 21:54:02 +00005505// LowerAVXCONCAT_VECTORS - 256-bit AVX can use the vinsertf128 instruction
5506// to create 256-bit vectors from two other 128-bit ones.
5507static SDValue LowerAVXCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) {
5508 DebugLoc dl = Op.getDebugLoc();
5509 EVT ResVT = Op.getValueType();
5510
Craig Topper7a9a28b2012-08-12 02:23:29 +00005511 assert(ResVT.is256BitVector() && "Value type must be 256-bit wide");
Bruno Cardoso Lopes8af24512011-08-01 21:54:02 +00005512
5513 SDValue V1 = Op.getOperand(0);
5514 SDValue V2 = Op.getOperand(1);
5515 unsigned NumElems = ResVT.getVectorNumElements();
5516
Craig Topper4c7972d2012-04-22 18:15:59 +00005517 return Concat128BitVectors(V1, V2, ResVT, NumElems, DAG, dl);
Bruno Cardoso Lopes8af24512011-08-01 21:54:02 +00005518}
5519
Craig Topper55b24052012-09-11 06:15:32 +00005520static SDValue LowerCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) {
Bruno Cardoso Lopes8af24512011-08-01 21:54:02 +00005521 assert(Op.getNumOperands() == 2);
Bruno Cardoso Lopes8af24512011-08-01 21:54:02 +00005522
5523 // 256-bit AVX can use the vinsertf128 instruction to create 256-bit vectors
5524 // from two other 128-bit ones.
5525 return LowerAVXCONCAT_VECTORS(Op, DAG);
5526}
5527
Nadav Roteme80aa7c2012-04-09 08:33:21 +00005528// Try to lower a shuffle node into a simple blend instruction.
Craig Topper55b24052012-09-11 06:15:32 +00005529static SDValue
5530LowerVECTOR_SHUFFLEtoBlend(ShuffleVectorSDNode *SVOp,
5531 const X86Subtarget *Subtarget, SelectionDAG &DAG) {
Nadav Roteme80aa7c2012-04-09 08:33:21 +00005532 SDValue V1 = SVOp->getOperand(0);
5533 SDValue V2 = SVOp->getOperand(1);
5534 DebugLoc dl = SVOp->getDebugLoc();
Craig Topper708e44f2012-04-23 07:36:33 +00005535 MVT VT = SVOp->getValueType(0).getSimpleVT();
Craig Topper1842ba02012-04-23 06:38:28 +00005536 unsigned NumElems = VT.getVectorNumElements();
Nadav Roteme80aa7c2012-04-09 08:33:21 +00005537
Nadav Roteme6113782012-04-11 06:40:27 +00005538 if (!Subtarget->hasSSE41())
Nadav Roteme80aa7c2012-04-09 08:33:21 +00005539 return SDValue();
5540
Craig Topper1842ba02012-04-23 06:38:28 +00005541 unsigned ISDNo = 0;
Nadav Roteme6113782012-04-11 06:40:27 +00005542 MVT OpTy;
5543
Craig Topper708e44f2012-04-23 07:36:33 +00005544 switch (VT.SimpleTy) {
Nadav Roteme6113782012-04-11 06:40:27 +00005545 default: return SDValue();
5546 case MVT::v8i16:
Craig Topper1842ba02012-04-23 06:38:28 +00005547 ISDNo = X86ISD::BLENDPW;
5548 OpTy = MVT::v8i16;
5549 break;
Nadav Roteme6113782012-04-11 06:40:27 +00005550 case MVT::v4i32:
5551 case MVT::v4f32:
Craig Topper1842ba02012-04-23 06:38:28 +00005552 ISDNo = X86ISD::BLENDPS;
5553 OpTy = MVT::v4f32;
5554 break;
Nadav Roteme6113782012-04-11 06:40:27 +00005555 case MVT::v2i64:
5556 case MVT::v2f64:
Craig Topper1842ba02012-04-23 06:38:28 +00005557 ISDNo = X86ISD::BLENDPD;
5558 OpTy = MVT::v2f64;
5559 break;
Nadav Roteme6113782012-04-11 06:40:27 +00005560 case MVT::v8i32:
5561 case MVT::v8f32:
Craig Topper1842ba02012-04-23 06:38:28 +00005562 if (!Subtarget->hasAVX())
5563 return SDValue();
5564 ISDNo = X86ISD::BLENDPS;
5565 OpTy = MVT::v8f32;
5566 break;
Nadav Roteme6113782012-04-11 06:40:27 +00005567 case MVT::v4i64:
5568 case MVT::v4f64:
Craig Topper1842ba02012-04-23 06:38:28 +00005569 if (!Subtarget->hasAVX())
5570 return SDValue();
5571 ISDNo = X86ISD::BLENDPD;
5572 OpTy = MVT::v4f64;
5573 break;
Nadav Roteme6113782012-04-11 06:40:27 +00005574 }
5575 assert(ISDNo && "Invalid Op Number");
5576
5577 unsigned MaskVals = 0;
Nadav Roteme80aa7c2012-04-09 08:33:21 +00005578
Craig Topper1842ba02012-04-23 06:38:28 +00005579 for (unsigned i = 0; i != NumElems; ++i) {
Nadav Roteme80aa7c2012-04-09 08:33:21 +00005580 int EltIdx = SVOp->getMaskElt(i);
Craig Topper1842ba02012-04-23 06:38:28 +00005581 if (EltIdx == (int)i || EltIdx < 0)
Nadav Roteme6113782012-04-11 06:40:27 +00005582 MaskVals |= (1<<i);
Craig Topper1842ba02012-04-23 06:38:28 +00005583 else if (EltIdx == (int)(i + NumElems))
Nadav Roteme6113782012-04-11 06:40:27 +00005584 continue; // Bit is set to zero;
Craig Topper1842ba02012-04-23 06:38:28 +00005585 else
5586 return SDValue();
Nadav Roteme80aa7c2012-04-09 08:33:21 +00005587 }
5588
Nadav Roteme6113782012-04-11 06:40:27 +00005589 V1 = DAG.getNode(ISD::BITCAST, dl, OpTy, V1);
5590 V2 = DAG.getNode(ISD::BITCAST, dl, OpTy, V2);
5591 SDValue Ret = DAG.getNode(ISDNo, dl, OpTy, V1, V2,
5592 DAG.getConstant(MaskVals, MVT::i32));
5593 return DAG.getNode(ISD::BITCAST, dl, VT, Ret);
Nadav Roteme80aa7c2012-04-09 08:33:21 +00005594}
5595
Nate Begemanb9a47b82009-02-23 08:49:38 +00005596// v8i16 shuffles - Prefer shuffles in the following order:
5597// 1. [all] pshuflw, pshufhw, optional move
5598// 2. [ssse3] 1 x pshufb
5599// 3. [ssse3] 2 x pshufb + 1 x por
5600// 4. [all] mov + pshuflw + pshufhw + N x (pextrw + pinsrw)
Craig Topper55b24052012-09-11 06:15:32 +00005601static SDValue
5602LowerVECTOR_SHUFFLEv8i16(SDValue Op, const X86Subtarget *Subtarget,
5603 SelectionDAG &DAG) {
Bruno Cardoso Lopesbf8154a2010-08-21 01:32:18 +00005604 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Nate Begeman9008ca62009-04-27 18:41:29 +00005605 SDValue V1 = SVOp->getOperand(0);
5606 SDValue V2 = SVOp->getOperand(1);
5607 DebugLoc dl = SVOp->getDebugLoc();
Nate Begemanb9a47b82009-02-23 08:49:38 +00005608 SmallVector<int, 8> MaskVals;
Evan Cheng14b32e12007-12-11 01:46:18 +00005609
Nate Begemanb9a47b82009-02-23 08:49:38 +00005610 // Determine if more than 1 of the words in each of the low and high quadwords
5611 // of the result come from the same quadword of one of the two inputs. Undef
5612 // mask values count as coming from any quadword, for better codegen.
Benjamin Kramer003fad92011-10-15 13:28:31 +00005613 unsigned LoQuad[] = { 0, 0, 0, 0 };
5614 unsigned HiQuad[] = { 0, 0, 0, 0 };
Benjamin Kramer699ddcb2012-02-06 12:06:18 +00005615 std::bitset<4> InputQuads;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005616 for (unsigned i = 0; i < 8; ++i) {
Benjamin Kramer003fad92011-10-15 13:28:31 +00005617 unsigned *Quad = i < 4 ? LoQuad : HiQuad;
Nate Begeman9008ca62009-04-27 18:41:29 +00005618 int EltIdx = SVOp->getMaskElt(i);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005619 MaskVals.push_back(EltIdx);
5620 if (EltIdx < 0) {
5621 ++Quad[0];
5622 ++Quad[1];
5623 ++Quad[2];
5624 ++Quad[3];
Evan Cheng14b32e12007-12-11 01:46:18 +00005625 continue;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005626 }
5627 ++Quad[EltIdx / 4];
5628 InputQuads.set(EltIdx / 4);
Evan Cheng14b32e12007-12-11 01:46:18 +00005629 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00005630
Nate Begemanb9a47b82009-02-23 08:49:38 +00005631 int BestLoQuad = -1;
Evan Cheng14b32e12007-12-11 01:46:18 +00005632 unsigned MaxQuad = 1;
5633 for (unsigned i = 0; i < 4; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005634 if (LoQuad[i] > MaxQuad) {
5635 BestLoQuad = i;
5636 MaxQuad = LoQuad[i];
Evan Cheng14b32e12007-12-11 01:46:18 +00005637 }
Evan Cheng8a86c3f2007-12-07 08:07:39 +00005638 }
5639
Nate Begemanb9a47b82009-02-23 08:49:38 +00005640 int BestHiQuad = -1;
Evan Cheng14b32e12007-12-11 01:46:18 +00005641 MaxQuad = 1;
5642 for (unsigned i = 0; i < 4; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005643 if (HiQuad[i] > MaxQuad) {
5644 BestHiQuad = i;
5645 MaxQuad = HiQuad[i];
Evan Cheng14b32e12007-12-11 01:46:18 +00005646 }
5647 }
5648
Nate Begemanb9a47b82009-02-23 08:49:38 +00005649 // For SSSE3, If all 8 words of the result come from only 1 quadword of each
Eric Christopherfd179292009-08-27 18:07:15 +00005650 // of the two input vectors, shuffle them into one input vector so only a
Nate Begemanb9a47b82009-02-23 08:49:38 +00005651 // single pshufb instruction is necessary. If There are more than 2 input
5652 // quads, disable the next transformation since it does not help SSSE3.
5653 bool V1Used = InputQuads[0] || InputQuads[1];
5654 bool V2Used = InputQuads[2] || InputQuads[3];
Craig Topperd0a31172012-01-10 06:37:29 +00005655 if (Subtarget->hasSSSE3()) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005656 if (InputQuads.count() == 2 && V1Used && V2Used) {
Benjamin Kramer699ddcb2012-02-06 12:06:18 +00005657 BestLoQuad = InputQuads[0] ? 0 : 1;
5658 BestHiQuad = InputQuads[2] ? 2 : 3;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005659 }
5660 if (InputQuads.count() > 2) {
5661 BestLoQuad = -1;
5662 BestHiQuad = -1;
5663 }
5664 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00005665
Nate Begemanb9a47b82009-02-23 08:49:38 +00005666 // If BestLoQuad or BestHiQuad are set, shuffle the quads together and update
5667 // the shuffle mask. If a quad is scored as -1, that means that it contains
5668 // words from all 4 input quadwords.
5669 SDValue NewV;
5670 if (BestLoQuad >= 0 || BestHiQuad >= 0) {
Benjamin Kramer9c683542012-01-30 15:16:21 +00005671 int MaskV[] = {
5672 BestLoQuad < 0 ? 0 : BestLoQuad,
5673 BestHiQuad < 0 ? 1 : BestHiQuad
5674 };
Eric Christopherfd179292009-08-27 18:07:15 +00005675 NewV = DAG.getVectorShuffle(MVT::v2i64, dl,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005676 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64, V1),
5677 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64, V2), &MaskV[0]);
5678 NewV = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, NewV);
Evan Cheng14b32e12007-12-11 01:46:18 +00005679
Nate Begemanb9a47b82009-02-23 08:49:38 +00005680 // Rewrite the MaskVals and assign NewV to V1 if NewV now contains all the
5681 // source words for the shuffle, to aid later transformations.
5682 bool AllWordsInNewV = true;
Mon P Wang37b9a192009-03-11 06:35:11 +00005683 bool InOrder[2] = { true, true };
Evan Cheng14b32e12007-12-11 01:46:18 +00005684 for (unsigned i = 0; i != 8; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005685 int idx = MaskVals[i];
Mon P Wang37b9a192009-03-11 06:35:11 +00005686 if (idx != (int)i)
5687 InOrder[i/4] = false;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005688 if (idx < 0 || (idx/4) == BestLoQuad || (idx/4) == BestHiQuad)
Evan Cheng14b32e12007-12-11 01:46:18 +00005689 continue;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005690 AllWordsInNewV = false;
5691 break;
Evan Cheng14b32e12007-12-11 01:46:18 +00005692 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00005693
Nate Begemanb9a47b82009-02-23 08:49:38 +00005694 bool pshuflw = AllWordsInNewV, pshufhw = AllWordsInNewV;
5695 if (AllWordsInNewV) {
5696 for (int i = 0; i != 8; ++i) {
5697 int idx = MaskVals[i];
5698 if (idx < 0)
Evan Cheng14b32e12007-12-11 01:46:18 +00005699 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00005700 idx = MaskVals[i] = (idx / 4) == BestLoQuad ? (idx & 3) : (idx & 3) + 4;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005701 if ((idx != i) && idx < 4)
5702 pshufhw = false;
5703 if ((idx != i) && idx > 3)
5704 pshuflw = false;
Evan Cheng14b32e12007-12-11 01:46:18 +00005705 }
Nate Begemanb9a47b82009-02-23 08:49:38 +00005706 V1 = NewV;
5707 V2Used = false;
5708 BestLoQuad = 0;
5709 BestHiQuad = 1;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00005710 }
Evan Cheng14b32e12007-12-11 01:46:18 +00005711
Nate Begemanb9a47b82009-02-23 08:49:38 +00005712 // If we've eliminated the use of V2, and the new mask is a pshuflw or
5713 // pshufhw, that's as cheap as it gets. Return the new shuffle.
Mon P Wang37b9a192009-03-11 06:35:11 +00005714 if ((pshufhw && InOrder[0]) || (pshuflw && InOrder[1])) {
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00005715 unsigned Opc = pshufhw ? X86ISD::PSHUFHW : X86ISD::PSHUFLW;
5716 unsigned TargetMask = 0;
5717 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV,
Owen Anderson825b72b2009-08-11 20:47:22 +00005718 DAG.getUNDEF(MVT::v8i16), &MaskVals[0]);
Craig Topperdd637ae2012-02-19 05:41:45 +00005719 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(NewV.getNode());
5720 TargetMask = pshufhw ? getShufflePSHUFHWImmediate(SVOp):
5721 getShufflePSHUFLWImmediate(SVOp);
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00005722 V1 = NewV.getOperand(0);
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00005723 return getTargetShuffleNode(Opc, dl, MVT::v8i16, V1, TargetMask, DAG);
Evan Cheng14b32e12007-12-11 01:46:18 +00005724 }
Evan Cheng14b32e12007-12-11 01:46:18 +00005725 }
Eric Christopherfd179292009-08-27 18:07:15 +00005726
Nate Begemanb9a47b82009-02-23 08:49:38 +00005727 // If we have SSSE3, and all words of the result are from 1 input vector,
5728 // case 2 is generated, otherwise case 3 is generated. If no SSSE3
5729 // is present, fall back to case 4.
Craig Topperd0a31172012-01-10 06:37:29 +00005730 if (Subtarget->hasSSSE3()) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005731 SmallVector<SDValue,16> pshufbMask;
Eric Christopherfd179292009-08-27 18:07:15 +00005732
Nate Begemanb9a47b82009-02-23 08:49:38 +00005733 // If we have elements from both input vectors, set the high bit of the
Eric Christopherfd179292009-08-27 18:07:15 +00005734 // shuffle mask element to zero out elements that come from V2 in the V1
Nate Begemanb9a47b82009-02-23 08:49:38 +00005735 // mask, and elements that come from V1 in the V2 mask, so that the two
5736 // results can be OR'd together.
5737 bool TwoInputs = V1Used && V2Used;
5738 for (unsigned i = 0; i != 8; ++i) {
5739 int EltIdx = MaskVals[i] * 2;
Craig Topperbe97ae92012-05-18 07:07:36 +00005740 int Idx0 = (TwoInputs && (EltIdx >= 16)) ? 0x80 : EltIdx;
5741 int Idx1 = (TwoInputs && (EltIdx >= 16)) ? 0x80 : EltIdx+1;
5742 pshufbMask.push_back(DAG.getConstant(Idx0, MVT::i8));
5743 pshufbMask.push_back(DAG.getConstant(Idx1, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005744 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005745 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, V1);
Eric Christopherfd179292009-08-27 18:07:15 +00005746 V1 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V1,
Evan Chenga87008d2009-02-25 22:49:59 +00005747 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005748 MVT::v16i8, &pshufbMask[0], 16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005749 if (!TwoInputs)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005750 return DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V1);
Eric Christopherfd179292009-08-27 18:07:15 +00005751
Nate Begemanb9a47b82009-02-23 08:49:38 +00005752 // Calculate the shuffle mask for the second input, shuffle it, and
5753 // OR it with the first shuffled input.
5754 pshufbMask.clear();
5755 for (unsigned i = 0; i != 8; ++i) {
5756 int EltIdx = MaskVals[i] * 2;
Craig Topperbe97ae92012-05-18 07:07:36 +00005757 int Idx0 = (EltIdx < 16) ? 0x80 : EltIdx - 16;
5758 int Idx1 = (EltIdx < 16) ? 0x80 : EltIdx - 15;
5759 pshufbMask.push_back(DAG.getConstant(Idx0, MVT::i8));
5760 pshufbMask.push_back(DAG.getConstant(Idx1, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005761 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005762 V2 = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, V2);
Eric Christopherfd179292009-08-27 18:07:15 +00005763 V2 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V2,
Evan Chenga87008d2009-02-25 22:49:59 +00005764 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005765 MVT::v16i8, &pshufbMask[0], 16));
5766 V1 = DAG.getNode(ISD::OR, dl, MVT::v16i8, V1, V2);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005767 return DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005768 }
5769
5770 // If BestLoQuad >= 0, generate a pshuflw to put the low elements in order,
5771 // and update MaskVals with new element order.
Benjamin Kramer9c683542012-01-30 15:16:21 +00005772 std::bitset<8> InOrder;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005773 if (BestLoQuad >= 0) {
Benjamin Kramer9c683542012-01-30 15:16:21 +00005774 int MaskV[] = { -1, -1, -1, -1, 4, 5, 6, 7 };
Nate Begemanb9a47b82009-02-23 08:49:38 +00005775 for (int i = 0; i != 4; ++i) {
5776 int idx = MaskVals[i];
5777 if (idx < 0) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005778 InOrder.set(i);
5779 } else if ((idx / 4) == BestLoQuad) {
Benjamin Kramer9c683542012-01-30 15:16:21 +00005780 MaskV[i] = idx & 3;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005781 InOrder.set(i);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005782 }
5783 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005784 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV, DAG.getUNDEF(MVT::v8i16),
Nate Begeman9008ca62009-04-27 18:41:29 +00005785 &MaskV[0]);
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00005786
Craig Topperdd637ae2012-02-19 05:41:45 +00005787 if (NewV.getOpcode() == ISD::VECTOR_SHUFFLE && Subtarget->hasSSSE3()) {
5788 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(NewV.getNode());
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00005789 NewV = getTargetShuffleNode(X86ISD::PSHUFLW, dl, MVT::v8i16,
Craig Topperdd637ae2012-02-19 05:41:45 +00005790 NewV.getOperand(0),
5791 getShufflePSHUFLWImmediate(SVOp), DAG);
5792 }
Nate Begemanb9a47b82009-02-23 08:49:38 +00005793 }
Eric Christopherfd179292009-08-27 18:07:15 +00005794
Nate Begemanb9a47b82009-02-23 08:49:38 +00005795 // If BestHi >= 0, generate a pshufhw to put the high elements in order,
5796 // and update MaskVals with the new element order.
5797 if (BestHiQuad >= 0) {
Benjamin Kramer9c683542012-01-30 15:16:21 +00005798 int MaskV[] = { 0, 1, 2, 3, -1, -1, -1, -1 };
Nate Begemanb9a47b82009-02-23 08:49:38 +00005799 for (unsigned i = 4; i != 8; ++i) {
5800 int idx = MaskVals[i];
5801 if (idx < 0) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005802 InOrder.set(i);
5803 } else if ((idx / 4) == BestHiQuad) {
Benjamin Kramer9c683542012-01-30 15:16:21 +00005804 MaskV[i] = (idx & 3) + 4;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005805 InOrder.set(i);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005806 }
5807 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005808 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV, DAG.getUNDEF(MVT::v8i16),
Nate Begeman9008ca62009-04-27 18:41:29 +00005809 &MaskV[0]);
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00005810
Craig Topperdd637ae2012-02-19 05:41:45 +00005811 if (NewV.getOpcode() == ISD::VECTOR_SHUFFLE && Subtarget->hasSSSE3()) {
5812 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(NewV.getNode());
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00005813 NewV = getTargetShuffleNode(X86ISD::PSHUFHW, dl, MVT::v8i16,
Craig Topperdd637ae2012-02-19 05:41:45 +00005814 NewV.getOperand(0),
5815 getShufflePSHUFHWImmediate(SVOp), DAG);
5816 }
Nate Begemanb9a47b82009-02-23 08:49:38 +00005817 }
Eric Christopherfd179292009-08-27 18:07:15 +00005818
Nate Begemanb9a47b82009-02-23 08:49:38 +00005819 // In case BestHi & BestLo were both -1, which means each quadword has a word
5820 // from each of the four input quadwords, calculate the InOrder bitvector now
5821 // before falling through to the insert/extract cleanup.
5822 if (BestLoQuad == -1 && BestHiQuad == -1) {
5823 NewV = V1;
5824 for (int i = 0; i != 8; ++i)
5825 if (MaskVals[i] < 0 || MaskVals[i] == i)
5826 InOrder.set(i);
5827 }
Eric Christopherfd179292009-08-27 18:07:15 +00005828
Nate Begemanb9a47b82009-02-23 08:49:38 +00005829 // The other elements are put in the right place using pextrw and pinsrw.
5830 for (unsigned i = 0; i != 8; ++i) {
5831 if (InOrder[i])
5832 continue;
5833 int EltIdx = MaskVals[i];
5834 if (EltIdx < 0)
5835 continue;
Craig Topper6643d9c2012-05-04 06:18:33 +00005836 SDValue ExtOp = (EltIdx < 8) ?
5837 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, V1,
5838 DAG.getIntPtrConstant(EltIdx)) :
5839 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, V2,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005840 DAG.getIntPtrConstant(EltIdx - 8));
Owen Anderson825b72b2009-08-11 20:47:22 +00005841 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, ExtOp,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005842 DAG.getIntPtrConstant(i));
5843 }
5844 return NewV;
5845}
5846
5847// v16i8 shuffles - Prefer shuffles in the following order:
5848// 1. [ssse3] 1 x pshufb
5849// 2. [ssse3] 2 x pshufb + 1 x por
5850// 3. [all] v8i16 shuffle + N x pextrw + rotate + pinsrw
5851static
Nate Begeman9008ca62009-04-27 18:41:29 +00005852SDValue LowerVECTOR_SHUFFLEv16i8(ShuffleVectorSDNode *SVOp,
Dan Gohmand858e902010-04-17 15:26:15 +00005853 SelectionDAG &DAG,
5854 const X86TargetLowering &TLI) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005855 SDValue V1 = SVOp->getOperand(0);
5856 SDValue V2 = SVOp->getOperand(1);
5857 DebugLoc dl = SVOp->getDebugLoc();
Benjamin Kramered4c8c62012-01-15 13:16:05 +00005858 ArrayRef<int> MaskVals = SVOp->getMask();
Eric Christopherfd179292009-08-27 18:07:15 +00005859
Nate Begemanb9a47b82009-02-23 08:49:38 +00005860 // If we have SSSE3, case 1 is generated when all result bytes come from
Eric Christopherfd179292009-08-27 18:07:15 +00005861 // one of the inputs. Otherwise, case 2 is generated. If no SSSE3 is
Nate Begemanb9a47b82009-02-23 08:49:38 +00005862 // present, fall back to case 3.
Eric Christopherfd179292009-08-27 18:07:15 +00005863
Nate Begemanb9a47b82009-02-23 08:49:38 +00005864 // If SSSE3, use 1 pshufb instruction per vector with elements in the result.
Craig Topperd0a31172012-01-10 06:37:29 +00005865 if (TLI.getSubtarget()->hasSSSE3()) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005866 SmallVector<SDValue,16> pshufbMask;
Eric Christopherfd179292009-08-27 18:07:15 +00005867
Nate Begemanb9a47b82009-02-23 08:49:38 +00005868 // If all result elements are from one input vector, then only translate
Eric Christopherfd179292009-08-27 18:07:15 +00005869 // undef mask values to 0x80 (zero out result) in the pshufb mask.
Nate Begemanb9a47b82009-02-23 08:49:38 +00005870 //
5871 // Otherwise, we have elements from both input vectors, and must zero out
5872 // elements that come from V2 in the first mask, and V1 in the second mask
5873 // so that we can OR them together.
Nate Begemanb9a47b82009-02-23 08:49:38 +00005874 for (unsigned i = 0; i != 16; ++i) {
5875 int EltIdx = MaskVals[i];
Craig Topperb82b5ab2012-05-18 06:42:06 +00005876 if (EltIdx < 0 || EltIdx >= 16)
5877 EltIdx = 0x80;
Owen Anderson825b72b2009-08-11 20:47:22 +00005878 pshufbMask.push_back(DAG.getConstant(EltIdx, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005879 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005880 V1 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V1,
Evan Chenga87008d2009-02-25 22:49:59 +00005881 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005882 MVT::v16i8, &pshufbMask[0], 16));
Michael Liao265bcb12012-08-31 20:12:31 +00005883
5884 // As PSHUFB will zero elements with negative indices, it's safe to ignore
5885 // the 2nd operand if it's undefined or zero.
5886 if (V2.getOpcode() == ISD::UNDEF ||
5887 ISD::isBuildVectorAllZeros(V2.getNode()))
Nate Begemanb9a47b82009-02-23 08:49:38 +00005888 return V1;
Eric Christopherfd179292009-08-27 18:07:15 +00005889
Nate Begemanb9a47b82009-02-23 08:49:38 +00005890 // Calculate the shuffle mask for the second input, shuffle it, and
5891 // OR it with the first shuffled input.
5892 pshufbMask.clear();
5893 for (unsigned i = 0; i != 16; ++i) {
5894 int EltIdx = MaskVals[i];
Craig Topperb82b5ab2012-05-18 06:42:06 +00005895 EltIdx = (EltIdx < 16) ? 0x80 : EltIdx - 16;
Craig Topper85b9e562012-05-22 06:09:38 +00005896 pshufbMask.push_back(DAG.getConstant(EltIdx, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005897 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005898 V2 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V2,
Evan Chenga87008d2009-02-25 22:49:59 +00005899 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005900 MVT::v16i8, &pshufbMask[0], 16));
5901 return DAG.getNode(ISD::OR, dl, MVT::v16i8, V1, V2);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005902 }
Eric Christopherfd179292009-08-27 18:07:15 +00005903
Nate Begemanb9a47b82009-02-23 08:49:38 +00005904 // No SSSE3 - Calculate in place words and then fix all out of place words
5905 // With 0-16 extracts & inserts. Worst case is 16 bytes out of order from
5906 // the 16 different words that comprise the two doublequadword input vectors.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005907 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V1);
5908 V2 = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V2);
Craig Topperb82b5ab2012-05-18 06:42:06 +00005909 SDValue NewV = V1;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005910 for (int i = 0; i != 8; ++i) {
5911 int Elt0 = MaskVals[i*2];
5912 int Elt1 = MaskVals[i*2+1];
Eric Christopherfd179292009-08-27 18:07:15 +00005913
Nate Begemanb9a47b82009-02-23 08:49:38 +00005914 // This word of the result is all undef, skip it.
5915 if (Elt0 < 0 && Elt1 < 0)
5916 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00005917
Nate Begemanb9a47b82009-02-23 08:49:38 +00005918 // This word of the result is already in the correct place, skip it.
Craig Topperb82b5ab2012-05-18 06:42:06 +00005919 if ((Elt0 == i*2) && (Elt1 == i*2+1))
Nate Begemanb9a47b82009-02-23 08:49:38 +00005920 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00005921
Nate Begemanb9a47b82009-02-23 08:49:38 +00005922 SDValue Elt0Src = Elt0 < 16 ? V1 : V2;
5923 SDValue Elt1Src = Elt1 < 16 ? V1 : V2;
5924 SDValue InsElt;
Mon P Wang6b3ef692009-03-11 18:47:57 +00005925
5926 // If Elt0 and Elt1 are defined, are consecutive, and can be load
5927 // using a single extract together, load it and store it.
5928 if ((Elt0 >= 0) && ((Elt0 + 1) == Elt1) && ((Elt0 & 1) == 0)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005929 InsElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, Elt1Src,
Mon P Wang6b3ef692009-03-11 18:47:57 +00005930 DAG.getIntPtrConstant(Elt1 / 2));
Owen Anderson825b72b2009-08-11 20:47:22 +00005931 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, InsElt,
Mon P Wang6b3ef692009-03-11 18:47:57 +00005932 DAG.getIntPtrConstant(i));
5933 continue;
5934 }
5935
Nate Begemanb9a47b82009-02-23 08:49:38 +00005936 // If Elt1 is defined, extract it from the appropriate source. If the
Mon P Wang6b3ef692009-03-11 18:47:57 +00005937 // source byte is not also odd, shift the extracted word left 8 bits
5938 // otherwise clear the bottom 8 bits if we need to do an or.
Nate Begemanb9a47b82009-02-23 08:49:38 +00005939 if (Elt1 >= 0) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005940 InsElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, Elt1Src,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005941 DAG.getIntPtrConstant(Elt1 / 2));
5942 if ((Elt1 & 1) == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00005943 InsElt = DAG.getNode(ISD::SHL, dl, MVT::i16, InsElt,
Owen Anderson95771af2011-02-25 21:41:48 +00005944 DAG.getConstant(8,
5945 TLI.getShiftAmountTy(InsElt.getValueType())));
Mon P Wang6b3ef692009-03-11 18:47:57 +00005946 else if (Elt0 >= 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00005947 InsElt = DAG.getNode(ISD::AND, dl, MVT::i16, InsElt,
5948 DAG.getConstant(0xFF00, MVT::i16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005949 }
5950 // If Elt0 is defined, extract it from the appropriate source. If the
5951 // source byte is not also even, shift the extracted word right 8 bits. If
5952 // Elt1 was also defined, OR the extracted values together before
5953 // inserting them in the result.
5954 if (Elt0 >= 0) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005955 SDValue InsElt0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005956 Elt0Src, DAG.getIntPtrConstant(Elt0 / 2));
5957 if ((Elt0 & 1) != 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00005958 InsElt0 = DAG.getNode(ISD::SRL, dl, MVT::i16, InsElt0,
Owen Anderson95771af2011-02-25 21:41:48 +00005959 DAG.getConstant(8,
5960 TLI.getShiftAmountTy(InsElt0.getValueType())));
Mon P Wang6b3ef692009-03-11 18:47:57 +00005961 else if (Elt1 >= 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00005962 InsElt0 = DAG.getNode(ISD::AND, dl, MVT::i16, InsElt0,
5963 DAG.getConstant(0x00FF, MVT::i16));
5964 InsElt = Elt1 >= 0 ? DAG.getNode(ISD::OR, dl, MVT::i16, InsElt, InsElt0)
Nate Begemanb9a47b82009-02-23 08:49:38 +00005965 : InsElt0;
5966 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005967 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, InsElt,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005968 DAG.getIntPtrConstant(i));
5969 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005970 return DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, NewV);
Evan Cheng14b32e12007-12-11 01:46:18 +00005971}
5972
Elena Demikhovsky41789462012-09-06 12:42:01 +00005973// v32i8 shuffles - Translate to VPSHUFB if possible.
5974static
5975SDValue LowerVECTOR_SHUFFLEv32i8(ShuffleVectorSDNode *SVOp,
Craig Topper55b24052012-09-11 06:15:32 +00005976 const X86Subtarget *Subtarget,
5977 SelectionDAG &DAG) {
Elena Demikhovsky41789462012-09-06 12:42:01 +00005978 EVT VT = SVOp->getValueType(0);
5979 SDValue V1 = SVOp->getOperand(0);
5980 SDValue V2 = SVOp->getOperand(1);
5981 DebugLoc dl = SVOp->getDebugLoc();
Elena Demikhovsky8100d242012-09-10 12:13:11 +00005982 SmallVector<int, 32> MaskVals(SVOp->getMask().begin(), SVOp->getMask().end());
Elena Demikhovsky41789462012-09-06 12:42:01 +00005983
5984 bool V2IsUndef = V2.getOpcode() == ISD::UNDEF;
Elena Demikhovsky8100d242012-09-10 12:13:11 +00005985 bool V1IsAllZero = ISD::isBuildVectorAllZeros(V1.getNode());
5986 bool V2IsAllZero = ISD::isBuildVectorAllZeros(V2.getNode());
Elena Demikhovsky41789462012-09-06 12:42:01 +00005987
Michael Liao471b9172012-10-03 23:43:52 +00005988 // VPSHUFB may be generated if
Elena Demikhovsky8100d242012-09-10 12:13:11 +00005989 // (1) one of input vector is undefined or zeroinitializer.
5990 // The mask value 0x80 puts 0 in the corresponding slot of the vector.
5991 // And (2) the mask indexes don't cross the 128-bit lane.
Craig Topper55b24052012-09-11 06:15:32 +00005992 if (VT != MVT::v32i8 || !Subtarget->hasAVX2() ||
Elena Demikhovsky8100d242012-09-10 12:13:11 +00005993 (!V2IsUndef && !V2IsAllZero && !V1IsAllZero))
Elena Demikhovsky41789462012-09-06 12:42:01 +00005994 return SDValue();
5995
Elena Demikhovsky8100d242012-09-10 12:13:11 +00005996 if (V1IsAllZero && !V2IsAllZero) {
5997 CommuteVectorShuffleMask(MaskVals, 32);
5998 V1 = V2;
5999 }
6000 SmallVector<SDValue, 32> pshufbMask;
Elena Demikhovsky41789462012-09-06 12:42:01 +00006001 for (unsigned i = 0; i != 32; i++) {
6002 int EltIdx = MaskVals[i];
6003 if (EltIdx < 0 || EltIdx >= 32)
6004 EltIdx = 0x80;
6005 else {
6006 if ((EltIdx >= 16 && i < 16) || (EltIdx < 16 && i >= 16))
6007 // Cross lane is not allowed.
6008 return SDValue();
6009 EltIdx &= 0xf;
6010 }
6011 pshufbMask.push_back(DAG.getConstant(EltIdx, MVT::i8));
6012 }
6013 return DAG.getNode(X86ISD::PSHUFB, dl, MVT::v32i8, V1,
6014 DAG.getNode(ISD::BUILD_VECTOR, dl,
6015 MVT::v32i8, &pshufbMask[0], 32));
6016}
6017
Evan Cheng7a831ce2007-12-15 03:00:47 +00006018/// RewriteAsNarrowerShuffle - Try rewriting v8i16 and v16i8 shuffles as 4 wide
Bruno Cardoso Lopes0a7dd4f2010-09-08 18:12:31 +00006019/// ones, or rewriting v4i32 / v4f32 as 2 wide ones if possible. This can be
Evan Cheng7a831ce2007-12-15 03:00:47 +00006020/// done when every pair / quad of shuffle mask elements point to elements in
6021/// the right sequence. e.g.
Bruno Cardoso Lopes0a7dd4f2010-09-08 18:12:31 +00006022/// vector_shuffle X, Y, <2, 3, | 10, 11, | 0, 1, | 14, 15>
Evan Cheng14b32e12007-12-11 01:46:18 +00006023static
Nate Begeman9008ca62009-04-27 18:41:29 +00006024SDValue RewriteAsNarrowerShuffle(ShuffleVectorSDNode *SVOp,
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006025 SelectionDAG &DAG, DebugLoc dl) {
Craig Topper11ac1f82012-05-04 04:08:44 +00006026 MVT VT = SVOp->getValueType(0).getSimpleVT();
Nate Begeman9008ca62009-04-27 18:41:29 +00006027 unsigned NumElems = VT.getVectorNumElements();
Craig Topper11ac1f82012-05-04 04:08:44 +00006028 MVT NewVT;
6029 unsigned Scale;
6030 switch (VT.SimpleTy) {
Craig Topperabb94d02012-02-05 03:43:23 +00006031 default: llvm_unreachable("Unexpected!");
Craig Topperf3640d72012-05-04 04:44:49 +00006032 case MVT::v4f32: NewVT = MVT::v2f64; Scale = 2; break;
6033 case MVT::v4i32: NewVT = MVT::v2i64; Scale = 2; break;
6034 case MVT::v8i16: NewVT = MVT::v4i32; Scale = 2; break;
6035 case MVT::v16i8: NewVT = MVT::v4i32; Scale = 4; break;
6036 case MVT::v16i16: NewVT = MVT::v8i32; Scale = 2; break;
6037 case MVT::v32i8: NewVT = MVT::v8i32; Scale = 4; break;
Evan Cheng7a831ce2007-12-15 03:00:47 +00006038 }
6039
Nate Begeman9008ca62009-04-27 18:41:29 +00006040 SmallVector<int, 8> MaskVec;
Craig Topper11ac1f82012-05-04 04:08:44 +00006041 for (unsigned i = 0; i != NumElems; i += Scale) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006042 int StartIdx = -1;
Craig Topper11ac1f82012-05-04 04:08:44 +00006043 for (unsigned j = 0; j != Scale; ++j) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006044 int EltIdx = SVOp->getMaskElt(i+j);
6045 if (EltIdx < 0)
Evan Cheng14b32e12007-12-11 01:46:18 +00006046 continue;
Craig Topper11ac1f82012-05-04 04:08:44 +00006047 if (StartIdx < 0)
6048 StartIdx = (EltIdx / Scale);
6049 if (EltIdx != (int)(StartIdx*Scale + j))
Dan Gohman475871a2008-07-27 21:46:04 +00006050 return SDValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00006051 }
Craig Topper11ac1f82012-05-04 04:08:44 +00006052 MaskVec.push_back(StartIdx);
Evan Cheng8a86c3f2007-12-07 08:07:39 +00006053 }
6054
Craig Topper11ac1f82012-05-04 04:08:44 +00006055 SDValue V1 = DAG.getNode(ISD::BITCAST, dl, NewVT, SVOp->getOperand(0));
6056 SDValue V2 = DAG.getNode(ISD::BITCAST, dl, NewVT, SVOp->getOperand(1));
Nate Begeman9008ca62009-04-27 18:41:29 +00006057 return DAG.getVectorShuffle(NewVT, dl, V1, V2, &MaskVec[0]);
Evan Cheng8a86c3f2007-12-07 08:07:39 +00006058}
6059
Evan Chengd880b972008-05-09 21:53:03 +00006060/// getVZextMovL - Return a zero-extending vector move low node.
Evan Cheng7e2ff772008-05-08 00:57:18 +00006061///
Owen Andersone50ed302009-08-10 22:56:29 +00006062static SDValue getVZextMovL(EVT VT, EVT OpVT,
Nate Begeman9008ca62009-04-27 18:41:29 +00006063 SDValue SrcOp, SelectionDAG &DAG,
6064 const X86Subtarget *Subtarget, DebugLoc dl) {
Owen Anderson825b72b2009-08-11 20:47:22 +00006065 if (VT == MVT::v2f64 || VT == MVT::v4f32) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00006066 LoadSDNode *LD = NULL;
Gabor Greifba36cb52008-08-28 21:40:38 +00006067 if (!isScalarLoadToVector(SrcOp.getNode(), &LD))
Evan Cheng7e2ff772008-05-08 00:57:18 +00006068 LD = dyn_cast<LoadSDNode>(SrcOp);
6069 if (!LD) {
6070 // movssrr and movsdrr do not clear top bits. Try to use movd, movq
6071 // instead.
Owen Anderson766b5ef2009-08-11 21:59:30 +00006072 MVT ExtVT = (OpVT == MVT::v2f64) ? MVT::i64 : MVT::i32;
Duncan Sandscdfad362010-11-03 12:17:33 +00006073 if ((ExtVT != MVT::i64 || Subtarget->is64Bit()) &&
Evan Cheng7e2ff772008-05-08 00:57:18 +00006074 SrcOp.getOpcode() == ISD::SCALAR_TO_VECTOR &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006075 SrcOp.getOperand(0).getOpcode() == ISD::BITCAST &&
Owen Anderson766b5ef2009-08-11 21:59:30 +00006076 SrcOp.getOperand(0).getOperand(0).getValueType() == ExtVT) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00006077 // PR2108
Owen Anderson825b72b2009-08-11 20:47:22 +00006078 OpVT = (OpVT == MVT::v2f64) ? MVT::v2i64 : MVT::v4i32;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006079 return DAG.getNode(ISD::BITCAST, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00006080 DAG.getNode(X86ISD::VZEXT_MOVL, dl, OpVT,
6081 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
6082 OpVT,
Gabor Greif327ef032008-08-28 23:19:51 +00006083 SrcOp.getOperand(0)
6084 .getOperand(0))));
Evan Cheng7e2ff772008-05-08 00:57:18 +00006085 }
6086 }
6087 }
6088
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006089 return DAG.getNode(ISD::BITCAST, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00006090 DAG.getNode(X86ISD::VZEXT_MOVL, dl, OpVT,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006091 DAG.getNode(ISD::BITCAST, dl,
Dale Johannesenace16102009-02-03 19:33:06 +00006092 OpVT, SrcOp)));
Evan Cheng7e2ff772008-05-08 00:57:18 +00006093}
6094
Bruno Cardoso Lopesd0888342011-07-22 00:14:56 +00006095/// LowerVECTOR_SHUFFLE_256 - Handle all 256-bit wide vectors shuffles
6096/// which could not be matched by any known target speficic shuffle
6097static SDValue
6098LowerVECTOR_SHUFFLE_256(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG) {
Elena Demikhovsky15963732012-06-26 08:04:10 +00006099
6100 SDValue NewOp = Compact8x32ShuffleNode(SVOp, DAG);
6101 if (NewOp.getNode())
6102 return NewOp;
6103
Craig Topper8f35c132012-01-20 09:29:03 +00006104 EVT VT = SVOp->getValueType(0);
Bruno Cardoso Lopes3b865982011-08-16 18:21:54 +00006105
Craig Topper8f35c132012-01-20 09:29:03 +00006106 unsigned NumElems = VT.getVectorNumElements();
6107 unsigned NumLaneElems = NumElems / 2;
6108
Craig Topper8f35c132012-01-20 09:29:03 +00006109 DebugLoc dl = SVOp->getDebugLoc();
6110 MVT EltVT = VT.getVectorElementType().getSimpleVT();
Craig Topper9a2b6e12012-04-06 07:45:23 +00006111 EVT NVT = MVT::getVectorVT(EltVT, NumLaneElems);
Craig Topper8ae97ba2012-05-21 06:40:16 +00006112 SDValue Output[2];
Craig Topper8f35c132012-01-20 09:29:03 +00006113
Craig Topper9a2b6e12012-04-06 07:45:23 +00006114 SmallVector<int, 16> Mask;
Craig Topper8f35c132012-01-20 09:29:03 +00006115 for (unsigned l = 0; l < 2; ++l) {
Craig Topper9a2b6e12012-04-06 07:45:23 +00006116 // Build a shuffle mask for the output, discovering on the fly which
6117 // input vectors to use as shuffle operands (recorded in InputUsed).
6118 // If building a suitable shuffle vector proves too hard, then bail
Craig Topper8ae97ba2012-05-21 06:40:16 +00006119 // out with UseBuildVector set.
6120 bool UseBuildVector = false;
Benjamin Kramer9e5512a2012-04-06 13:33:52 +00006121 int InputUsed[2] = { -1, -1 }; // Not yet discovered.
Craig Topper9a2b6e12012-04-06 07:45:23 +00006122 unsigned LaneStart = l * NumLaneElems;
6123 for (unsigned i = 0; i != NumLaneElems; ++i) {
6124 // The mask element. This indexes into the input.
6125 int Idx = SVOp->getMaskElt(i+LaneStart);
6126 if (Idx < 0) {
6127 // the mask element does not index into any input vector.
6128 Mask.push_back(-1);
6129 continue;
6130 }
Craig Topper8f35c132012-01-20 09:29:03 +00006131
Craig Topper9a2b6e12012-04-06 07:45:23 +00006132 // The input vector this mask element indexes into.
6133 int Input = Idx / NumLaneElems;
Craig Topper8f35c132012-01-20 09:29:03 +00006134
Craig Topper9a2b6e12012-04-06 07:45:23 +00006135 // Turn the index into an offset from the start of the input vector.
6136 Idx -= Input * NumLaneElems;
6137
6138 // Find or create a shuffle vector operand to hold this input.
6139 unsigned OpNo;
6140 for (OpNo = 0; OpNo < array_lengthof(InputUsed); ++OpNo) {
6141 if (InputUsed[OpNo] == Input)
6142 // This input vector is already an operand.
6143 break;
6144 if (InputUsed[OpNo] < 0) {
6145 // Create a new operand for this input vector.
6146 InputUsed[OpNo] = Input;
6147 break;
6148 }
6149 }
6150
6151 if (OpNo >= array_lengthof(InputUsed)) {
Craig Topper8ae97ba2012-05-21 06:40:16 +00006152 // More than two input vectors used! Give up on trying to create a
6153 // shuffle vector. Insert all elements into a BUILD_VECTOR instead.
6154 UseBuildVector = true;
6155 break;
Craig Topper9a2b6e12012-04-06 07:45:23 +00006156 }
6157
6158 // Add the mask index for the new shuffle vector.
6159 Mask.push_back(Idx + OpNo * NumLaneElems);
6160 }
6161
Craig Topper8ae97ba2012-05-21 06:40:16 +00006162 if (UseBuildVector) {
6163 SmallVector<SDValue, 16> SVOps;
6164 for (unsigned i = 0; i != NumLaneElems; ++i) {
6165 // The mask element. This indexes into the input.
6166 int Idx = SVOp->getMaskElt(i+LaneStart);
6167 if (Idx < 0) {
6168 SVOps.push_back(DAG.getUNDEF(EltVT));
6169 continue;
6170 }
6171
6172 // The input vector this mask element indexes into.
6173 int Input = Idx / NumElems;
6174
6175 // Turn the index into an offset from the start of the input vector.
6176 Idx -= Input * NumElems;
6177
6178 // Extract the vector element by hand.
6179 SVOps.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EltVT,
6180 SVOp->getOperand(Input),
6181 DAG.getIntPtrConstant(Idx)));
6182 }
6183
6184 // Construct the output using a BUILD_VECTOR.
6185 Output[l] = DAG.getNode(ISD::BUILD_VECTOR, dl, NVT, &SVOps[0],
6186 SVOps.size());
6187 } else if (InputUsed[0] < 0) {
Craig Topper9a2b6e12012-04-06 07:45:23 +00006188 // No input vectors were used! The result is undefined.
Craig Topper8ae97ba2012-05-21 06:40:16 +00006189 Output[l] = DAG.getUNDEF(NVT);
Craig Topper9a2b6e12012-04-06 07:45:23 +00006190 } else {
6191 SDValue Op0 = Extract128BitVector(SVOp->getOperand(InputUsed[0] / 2),
Craig Topperb14940a2012-04-22 20:55:18 +00006192 (InputUsed[0] % 2) * NumLaneElems,
6193 DAG, dl);
Craig Topper9a2b6e12012-04-06 07:45:23 +00006194 // If only one input was used, use an undefined vector for the other.
6195 SDValue Op1 = (InputUsed[1] < 0) ? DAG.getUNDEF(NVT) :
6196 Extract128BitVector(SVOp->getOperand(InputUsed[1] / 2),
Craig Topperb14940a2012-04-22 20:55:18 +00006197 (InputUsed[1] % 2) * NumLaneElems, DAG, dl);
Craig Topper9a2b6e12012-04-06 07:45:23 +00006198 // At least one input vector was used. Create a new shuffle vector.
Craig Topper8ae97ba2012-05-21 06:40:16 +00006199 Output[l] = DAG.getVectorShuffle(NVT, dl, Op0, Op1, &Mask[0]);
Craig Topper9a2b6e12012-04-06 07:45:23 +00006200 }
6201
6202 Mask.clear();
6203 }
Craig Topper8f35c132012-01-20 09:29:03 +00006204
6205 // Concatenate the result back
Craig Topper8ae97ba2012-05-21 06:40:16 +00006206 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT, Output[0], Output[1]);
Bruno Cardoso Lopesd0888342011-07-22 00:14:56 +00006207}
6208
Bruno Cardoso Lopes589b8972011-07-22 00:14:53 +00006209/// LowerVECTOR_SHUFFLE_128v4 - Handle all 128-bit wide vectors with
6210/// 4 elements, and match them with several different shuffle types.
Dan Gohman475871a2008-07-27 21:46:04 +00006211static SDValue
Bruno Cardoso Lopes589b8972011-07-22 00:14:53 +00006212LowerVECTOR_SHUFFLE_128v4(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006213 SDValue V1 = SVOp->getOperand(0);
6214 SDValue V2 = SVOp->getOperand(1);
6215 DebugLoc dl = SVOp->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00006216 EVT VT = SVOp->getValueType(0);
Eric Christopherfd179292009-08-27 18:07:15 +00006217
Craig Topper7a9a28b2012-08-12 02:23:29 +00006218 assert(VT.is128BitVector() && "Unsupported vector size");
Bruno Cardoso Lopes589b8972011-07-22 00:14:53 +00006219
Benjamin Kramer9c683542012-01-30 15:16:21 +00006220 std::pair<int, int> Locs[4];
6221 int Mask1[] = { -1, -1, -1, -1 };
Benjamin Kramered4c8c62012-01-15 13:16:05 +00006222 SmallVector<int, 8> PermMask(SVOp->getMask().begin(), SVOp->getMask().end());
Nate Begeman9008ca62009-04-27 18:41:29 +00006223
Evan Chengace3c172008-07-22 21:13:36 +00006224 unsigned NumHi = 0;
6225 unsigned NumLo = 0;
Evan Chengace3c172008-07-22 21:13:36 +00006226 for (unsigned i = 0; i != 4; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006227 int Idx = PermMask[i];
6228 if (Idx < 0) {
Evan Chengace3c172008-07-22 21:13:36 +00006229 Locs[i] = std::make_pair(-1, -1);
6230 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00006231 assert(Idx < 8 && "Invalid VECTOR_SHUFFLE index!");
6232 if (Idx < 4) {
Evan Chengace3c172008-07-22 21:13:36 +00006233 Locs[i] = std::make_pair(0, NumLo);
Nate Begeman9008ca62009-04-27 18:41:29 +00006234 Mask1[NumLo] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00006235 NumLo++;
6236 } else {
6237 Locs[i] = std::make_pair(1, NumHi);
6238 if (2+NumHi < 4)
Nate Begeman9008ca62009-04-27 18:41:29 +00006239 Mask1[2+NumHi] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00006240 NumHi++;
6241 }
6242 }
6243 }
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006244
Evan Chengace3c172008-07-22 21:13:36 +00006245 if (NumLo <= 2 && NumHi <= 2) {
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006246 // If no more than two elements come from either vector. This can be
6247 // implemented with two shuffles. First shuffle gather the elements.
6248 // The second shuffle, which takes the first shuffle as both of its
6249 // vector operands, put the elements into the right order.
Nate Begeman9008ca62009-04-27 18:41:29 +00006250 V1 = DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006251
Benjamin Kramer9c683542012-01-30 15:16:21 +00006252 int Mask2[] = { -1, -1, -1, -1 };
Eric Christopherfd179292009-08-27 18:07:15 +00006253
Benjamin Kramer9c683542012-01-30 15:16:21 +00006254 for (unsigned i = 0; i != 4; ++i)
6255 if (Locs[i].first != -1) {
Evan Chengace3c172008-07-22 21:13:36 +00006256 unsigned Idx = (i < 2) ? 0 : 4;
6257 Idx += Locs[i].first * 2 + Locs[i].second;
Nate Begeman9008ca62009-04-27 18:41:29 +00006258 Mask2[i] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00006259 }
Evan Chengace3c172008-07-22 21:13:36 +00006260
Nate Begeman9008ca62009-04-27 18:41:29 +00006261 return DAG.getVectorShuffle(VT, dl, V1, V1, &Mask2[0]);
Craig Topper69947b92012-04-23 06:57:04 +00006262 }
6263
6264 if (NumLo == 3 || NumHi == 3) {
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006265 // Otherwise, we must have three elements from one vector, call it X, and
6266 // one element from the other, call it Y. First, use a shufps to build an
6267 // intermediate vector with the one element from Y and the element from X
6268 // that will be in the same half in the final destination (the indexes don't
6269 // matter). Then, use a shufps to build the final vector, taking the half
6270 // containing the element from Y from the intermediate, and the other half
6271 // from X.
6272 if (NumHi == 3) {
6273 // Normalize it so the 3 elements come from V1.
Craig Topperbeabc6c2011-12-05 06:56:46 +00006274 CommuteVectorShuffleMask(PermMask, 4);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006275 std::swap(V1, V2);
6276 }
6277
6278 // Find the element from V2.
6279 unsigned HiIndex;
6280 for (HiIndex = 0; HiIndex < 3; ++HiIndex) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006281 int Val = PermMask[HiIndex];
6282 if (Val < 0)
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006283 continue;
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006284 if (Val >= 4)
6285 break;
6286 }
6287
Nate Begeman9008ca62009-04-27 18:41:29 +00006288 Mask1[0] = PermMask[HiIndex];
6289 Mask1[1] = -1;
6290 Mask1[2] = PermMask[HiIndex^1];
6291 Mask1[3] = -1;
6292 V2 = DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006293
6294 if (HiIndex >= 2) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006295 Mask1[0] = PermMask[0];
6296 Mask1[1] = PermMask[1];
6297 Mask1[2] = HiIndex & 1 ? 6 : 4;
6298 Mask1[3] = HiIndex & 1 ? 4 : 6;
6299 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006300 }
Craig Topper69947b92012-04-23 06:57:04 +00006301
6302 Mask1[0] = HiIndex & 1 ? 2 : 0;
6303 Mask1[1] = HiIndex & 1 ? 0 : 2;
6304 Mask1[2] = PermMask[2];
6305 Mask1[3] = PermMask[3];
6306 if (Mask1[2] >= 0)
6307 Mask1[2] += 4;
6308 if (Mask1[3] >= 0)
6309 Mask1[3] += 4;
6310 return DAG.getVectorShuffle(VT, dl, V2, V1, &Mask1[0]);
Evan Chengace3c172008-07-22 21:13:36 +00006311 }
6312
6313 // Break it into (shuffle shuffle_hi, shuffle_lo).
Benjamin Kramer9c683542012-01-30 15:16:21 +00006314 int LoMask[] = { -1, -1, -1, -1 };
6315 int HiMask[] = { -1, -1, -1, -1 };
Nate Begeman9008ca62009-04-27 18:41:29 +00006316
Benjamin Kramer9c683542012-01-30 15:16:21 +00006317 int *MaskPtr = LoMask;
Evan Chengace3c172008-07-22 21:13:36 +00006318 unsigned MaskIdx = 0;
6319 unsigned LoIdx = 0;
6320 unsigned HiIdx = 2;
6321 for (unsigned i = 0; i != 4; ++i) {
6322 if (i == 2) {
Benjamin Kramer9c683542012-01-30 15:16:21 +00006323 MaskPtr = HiMask;
Evan Chengace3c172008-07-22 21:13:36 +00006324 MaskIdx = 1;
6325 LoIdx = 0;
6326 HiIdx = 2;
6327 }
Nate Begeman9008ca62009-04-27 18:41:29 +00006328 int Idx = PermMask[i];
6329 if (Idx < 0) {
Evan Chengace3c172008-07-22 21:13:36 +00006330 Locs[i] = std::make_pair(-1, -1);
Nate Begeman9008ca62009-04-27 18:41:29 +00006331 } else if (Idx < 4) {
Evan Chengace3c172008-07-22 21:13:36 +00006332 Locs[i] = std::make_pair(MaskIdx, LoIdx);
Benjamin Kramer9c683542012-01-30 15:16:21 +00006333 MaskPtr[LoIdx] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00006334 LoIdx++;
6335 } else {
6336 Locs[i] = std::make_pair(MaskIdx, HiIdx);
Benjamin Kramer9c683542012-01-30 15:16:21 +00006337 MaskPtr[HiIdx] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00006338 HiIdx++;
6339 }
6340 }
6341
Nate Begeman9008ca62009-04-27 18:41:29 +00006342 SDValue LoShuffle = DAG.getVectorShuffle(VT, dl, V1, V2, &LoMask[0]);
6343 SDValue HiShuffle = DAG.getVectorShuffle(VT, dl, V1, V2, &HiMask[0]);
Benjamin Kramer9c683542012-01-30 15:16:21 +00006344 int MaskOps[] = { -1, -1, -1, -1 };
6345 for (unsigned i = 0; i != 4; ++i)
6346 if (Locs[i].first != -1)
6347 MaskOps[i] = Locs[i].first * 4 + Locs[i].second;
Nate Begeman9008ca62009-04-27 18:41:29 +00006348 return DAG.getVectorShuffle(VT, dl, LoShuffle, HiShuffle, &MaskOps[0]);
Evan Chengace3c172008-07-22 21:13:36 +00006349}
6350
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00006351static bool MayFoldVectorLoad(SDValue V) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006352 if (V.hasOneUse() && V.getOpcode() == ISD::BITCAST)
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00006353 V = V.getOperand(0);
6354 if (V.hasOneUse() && V.getOpcode() == ISD::SCALAR_TO_VECTOR)
6355 V = V.getOperand(0);
Evan Cheng7bc389b2011-11-08 00:31:58 +00006356 if (V.hasOneUse() && V.getOpcode() == ISD::BUILD_VECTOR &&
6357 V.getNumOperands() == 2 && V.getOperand(1).getOpcode() == ISD::UNDEF)
6358 // BUILD_VECTOR (load), undef
6359 V = V.getOperand(0);
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00006360 if (MayFoldLoad(V))
6361 return true;
6362 return false;
6363}
6364
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006365// FIXME: the version above should always be used. Since there's
6366// a bug where several vector shuffles can't be folded because the
6367// DAG is not updated during lowering and a node claims to have two
6368// uses while it only has one, use this version, and let isel match
6369// another instruction if the load really happens to have more than
6370// one use. Remove this version after this bug get fixed.
Evan Cheng835580f2010-10-07 20:50:20 +00006371// rdar://8434668, PR8156
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006372static bool RelaxedMayFoldVectorLoad(SDValue V) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006373 if (V.hasOneUse() && V.getOpcode() == ISD::BITCAST)
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006374 V = V.getOperand(0);
6375 if (V.hasOneUse() && V.getOpcode() == ISD::SCALAR_TO_VECTOR)
6376 V = V.getOperand(0);
6377 if (ISD::isNormalLoad(V.getNode()))
6378 return true;
6379 return false;
6380}
6381
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006382static
Evan Cheng835580f2010-10-07 20:50:20 +00006383SDValue getMOVDDup(SDValue &Op, DebugLoc &dl, SDValue V1, SelectionDAG &DAG) {
6384 EVT VT = Op.getValueType();
6385
6386 // Canonizalize to v2f64.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006387 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, V1);
6388 return DAG.getNode(ISD::BITCAST, dl, VT,
Evan Cheng835580f2010-10-07 20:50:20 +00006389 getTargetShuffleNode(X86ISD::MOVDDUP, dl, MVT::v2f64,
6390 V1, DAG));
6391}
6392
6393static
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006394SDValue getMOVLowToHigh(SDValue &Op, DebugLoc &dl, SelectionDAG &DAG,
Craig Topper1accb7e2012-01-10 06:54:16 +00006395 bool HasSSE2) {
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006396 SDValue V1 = Op.getOperand(0);
6397 SDValue V2 = Op.getOperand(1);
6398 EVT VT = Op.getValueType();
6399
6400 assert(VT != MVT::v2i64 && "unsupported shuffle type");
6401
Craig Topper1accb7e2012-01-10 06:54:16 +00006402 if (HasSSE2 && VT == MVT::v2f64)
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006403 return getTargetShuffleNode(X86ISD::MOVLHPD, dl, VT, V1, V2, DAG);
6404
Evan Cheng0899f5c2011-08-31 02:05:24 +00006405 // v4f32 or v4i32: canonizalized to v4f32 (which is legal for SSE1)
6406 return DAG.getNode(ISD::BITCAST, dl, VT,
6407 getTargetShuffleNode(X86ISD::MOVLHPS, dl, MVT::v4f32,
6408 DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, V1),
6409 DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, V2), DAG));
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006410}
6411
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00006412static
6413SDValue getMOVHighToLow(SDValue &Op, DebugLoc &dl, SelectionDAG &DAG) {
6414 SDValue V1 = Op.getOperand(0);
6415 SDValue V2 = Op.getOperand(1);
6416 EVT VT = Op.getValueType();
6417
6418 assert((VT == MVT::v4i32 || VT == MVT::v4f32) &&
6419 "unsupported shuffle type");
6420
6421 if (V2.getOpcode() == ISD::UNDEF)
6422 V2 = V1;
6423
6424 // v4i32 or v4f32
6425 return getTargetShuffleNode(X86ISD::MOVHLPS, dl, VT, V1, V2, DAG);
6426}
6427
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006428static
Craig Topper1accb7e2012-01-10 06:54:16 +00006429SDValue getMOVLP(SDValue &Op, DebugLoc &dl, SelectionDAG &DAG, bool HasSSE2) {
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006430 SDValue V1 = Op.getOperand(0);
6431 SDValue V2 = Op.getOperand(1);
6432 EVT VT = Op.getValueType();
6433 unsigned NumElems = VT.getVectorNumElements();
6434
6435 // Use MOVLPS and MOVLPD in case V1 or V2 are loads. During isel, the second
6436 // operand of these instructions is only memory, so check if there's a
6437 // potencial load folding here, otherwise use SHUFPS or MOVSD to match the
6438 // same masks.
6439 bool CanFoldLoad = false;
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006440
Bruno Cardoso Lopesd00bfe12010-09-02 02:35:51 +00006441 // Trivial case, when V2 comes from a load.
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00006442 if (MayFoldVectorLoad(V2))
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006443 CanFoldLoad = true;
6444
6445 // When V1 is a load, it can be folded later into a store in isel, example:
6446 // (store (v4f32 (X86Movlps (load addr:$src1), VR128:$src2)), addr:$src1)
6447 // turns into:
6448 // (MOVLPSmr addr:$src1, VR128:$src2)
6449 // So, recognize this potential and also use MOVLPS or MOVLPD
Evan Cheng7bc389b2011-11-08 00:31:58 +00006450 else if (MayFoldVectorLoad(V1) && MayFoldIntoStore(Op))
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006451 CanFoldLoad = true;
6452
Dan Gohman65fd6562011-11-03 21:49:52 +00006453 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006454 if (CanFoldLoad) {
Craig Topper1accb7e2012-01-10 06:54:16 +00006455 if (HasSSE2 && NumElems == 2)
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006456 return getTargetShuffleNode(X86ISD::MOVLPD, dl, VT, V1, V2, DAG);
6457
6458 if (NumElems == 4)
Benjamin Kramerd9b0b022012-06-02 10:20:22 +00006459 // If we don't care about the second element, proceed to use movss.
Dan Gohman65fd6562011-11-03 21:49:52 +00006460 if (SVOp->getMaskElt(1) != -1)
6461 return getTargetShuffleNode(X86ISD::MOVLPS, dl, VT, V1, V2, DAG);
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006462 }
6463
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006464 // movl and movlp will both match v2i64, but v2i64 is never matched by
6465 // movl earlier because we make it strict to avoid messing with the movlp load
6466 // folding logic (see the code above getMOVLP call). Match it here then,
6467 // this is horrible, but will stay like this until we move all shuffle
6468 // matching to x86 specific nodes. Note that for the 1st condition all
6469 // types are matched with movsd.
Craig Topper1accb7e2012-01-10 06:54:16 +00006470 if (HasSSE2) {
Bruno Cardoso Lopes5ca0d142011-09-14 02:36:14 +00006471 // FIXME: isMOVLMask should be checked and matched before getMOVLP,
6472 // as to remove this logic from here, as much as possible
Craig Topper5aaffa82012-02-19 02:53:47 +00006473 if (NumElems == 2 || !isMOVLMask(SVOp->getMask(), VT))
Bruno Cardoso Lopes57d6a5e2011-08-31 03:04:20 +00006474 return getTargetShuffleNode(X86ISD::MOVSD, dl, VT, V1, V2, DAG);
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006475 return getTargetShuffleNode(X86ISD::MOVSS, dl, VT, V1, V2, DAG);
Bruno Cardoso Lopes57d6a5e2011-08-31 03:04:20 +00006476 }
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006477
6478 assert(VT != MVT::v4i32 && "unsupported shuffle type");
6479
6480 // Invert the operand order and use SHUFPS to match it.
Craig Topperb3982da2011-12-31 23:50:21 +00006481 return getTargetShuffleNode(X86ISD::SHUFP, dl, VT, V2, V1,
Craig Topper5aaffa82012-02-19 02:53:47 +00006482 getShuffleSHUFImmediate(SVOp), DAG);
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006483}
6484
Nadav Rotem154819d2012-04-09 07:45:58 +00006485SDValue
6486X86TargetLowering::NormalizeVectorShuffle(SDValue Op, SelectionDAG &DAG) const {
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006487 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
6488 EVT VT = Op.getValueType();
6489 DebugLoc dl = Op.getDebugLoc();
6490 SDValue V1 = Op.getOperand(0);
6491 SDValue V2 = Op.getOperand(1);
6492
6493 if (isZeroShuffle(SVOp))
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00006494 return getZeroVector(VT, Subtarget, DAG, dl);
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006495
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006496 // Handle splat operations
6497 if (SVOp->isSplat()) {
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00006498 unsigned NumElem = VT.getVectorNumElements();
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00006499 int Size = VT.getSizeInBits();
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006500
Bruno Cardoso Lopes0e6d2302011-08-17 02:29:19 +00006501 // Use vbroadcast whenever the splat comes from a foldable load
Nadav Rotem154819d2012-04-09 07:45:58 +00006502 SDValue Broadcast = LowerVectorBroadcast(Op, DAG);
Nadav Rotem9d68b062012-04-08 12:54:54 +00006503 if (Broadcast.getNode())
6504 return Broadcast;
Bruno Cardoso Lopes0e6d2302011-08-17 02:29:19 +00006505
Bruno Cardoso Lopes6a32adc2011-07-25 23:05:25 +00006506 // Handle splats by matching through known shuffle masks
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00006507 if ((Size == 128 && NumElem <= 4) ||
6508 (Size == 256 && NumElem < 8))
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006509 return SDValue();
6510
Bruno Cardoso Lopes8a5b2622011-08-17 02:29:13 +00006511 // All remaning splats are promoted to target supported vector shuffles.
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006512 return PromoteSplat(SVOp, DAG);
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006513 }
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006514
6515 // If the shuffle can be profitably rewritten as a narrower shuffle, then
6516 // do it!
Craig Topperf3640d72012-05-04 04:44:49 +00006517 if (VT == MVT::v8i16 || VT == MVT::v16i8 ||
6518 VT == MVT::v16i16 || VT == MVT::v32i8) {
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006519 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, dl);
6520 if (NewOp.getNode())
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006521 return DAG.getNode(ISD::BITCAST, dl, VT, NewOp);
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006522 } else if ((VT == MVT::v4i32 ||
Craig Topper1accb7e2012-01-10 06:54:16 +00006523 (VT == MVT::v4f32 && Subtarget->hasSSE2()))) {
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006524 // FIXME: Figure out a cleaner way to do this.
6525 // Try to make use of movq to zero out the top part.
6526 if (ISD::isBuildVectorAllZeros(V2.getNode())) {
6527 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, dl);
6528 if (NewOp.getNode()) {
Craig Topper5aaffa82012-02-19 02:53:47 +00006529 EVT NewVT = NewOp.getValueType();
6530 if (isCommutedMOVLMask(cast<ShuffleVectorSDNode>(NewOp)->getMask(),
6531 NewVT, true, false))
6532 return getVZextMovL(VT, NewVT, NewOp.getOperand(0),
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006533 DAG, Subtarget, dl);
6534 }
6535 } else if (ISD::isBuildVectorAllZeros(V1.getNode())) {
6536 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, dl);
Craig Topper5aaffa82012-02-19 02:53:47 +00006537 if (NewOp.getNode()) {
6538 EVT NewVT = NewOp.getValueType();
6539 if (isMOVLMask(cast<ShuffleVectorSDNode>(NewOp)->getMask(), NewVT))
6540 return getVZextMovL(VT, NewVT, NewOp.getOperand(1),
6541 DAG, Subtarget, dl);
6542 }
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006543 }
6544 }
6545 return SDValue();
6546}
6547
Dan Gohman475871a2008-07-27 21:46:04 +00006548SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006549X86TargetLowering::LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) const {
Nate Begeman9008ca62009-04-27 18:41:29 +00006550 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Dan Gohman475871a2008-07-27 21:46:04 +00006551 SDValue V1 = Op.getOperand(0);
6552 SDValue V2 = Op.getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +00006553 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006554 DebugLoc dl = Op.getDebugLoc();
Nate Begeman9008ca62009-04-27 18:41:29 +00006555 unsigned NumElems = VT.getVectorNumElements();
Elena Demikhovsky16db7102012-01-12 20:33:10 +00006556 bool V1IsUndef = V1.getOpcode() == ISD::UNDEF;
Evan Cheng0db9fe62006-04-25 20:13:52 +00006557 bool V2IsUndef = V2.getOpcode() == ISD::UNDEF;
Evan Chengd9b8e402006-10-16 06:36:00 +00006558 bool V1IsSplat = false;
6559 bool V2IsSplat = false;
Craig Topper1accb7e2012-01-10 06:54:16 +00006560 bool HasSSE2 = Subtarget->hasSSE2();
Craig Topperbeabc6c2011-12-05 06:56:46 +00006561 bool HasAVX = Subtarget->hasAVX();
Craig Topper6347e862011-11-21 06:57:39 +00006562 bool HasAVX2 = Subtarget->hasAVX2();
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006563 MachineFunction &MF = DAG.getMachineFunction();
Bill Wendling67658342012-10-09 07:45:08 +00006564 bool OptForSize = MF.getFunction()->getFnAttributes().
6565 hasAttribute(Attributes::OptimizeForSize);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006566
Craig Topper3426a3e2011-11-14 06:46:21 +00006567 assert(VT.getSizeInBits() != 64 && "Can't lower MMX shuffles");
Bruno Cardoso Lopes58277b12010-09-07 18:41:45 +00006568
Elena Demikhovsky16db7102012-01-12 20:33:10 +00006569 if (V1IsUndef && V2IsUndef)
6570 return DAG.getUNDEF(VT);
6571
6572 assert(!V1IsUndef && "Op 1 of shuffle should not be undef");
Craig Topper38034c52011-11-26 22:55:48 +00006573
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006574 // Vector shuffle lowering takes 3 steps:
6575 //
6576 // 1) Normalize the input vectors. Here splats, zeroed vectors, profitable
6577 // narrowing and commutation of operands should be handled.
6578 // 2) Matching of shuffles with known shuffle masks to x86 target specific
6579 // shuffle nodes.
6580 // 3) Rewriting of unmatched masks into new generic shuffle operations,
6581 // so the shuffle can be broken into other shuffles and the legalizer can
6582 // try the lowering again.
6583 //
Craig Topper3426a3e2011-11-14 06:46:21 +00006584 // The general idea is that no vector_shuffle operation should be left to
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006585 // be matched during isel, all of them must be converted to a target specific
6586 // node here.
Bruno Cardoso Lopes0d1340b2010-09-07 20:20:27 +00006587
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006588 // Normalize the input vectors. Here splats, zeroed vectors, profitable
6589 // narrowing and commutation of operands should be handled. The actual code
6590 // doesn't include all of those, work in progress...
Nadav Rotem154819d2012-04-09 07:45:58 +00006591 SDValue NewOp = NormalizeVectorShuffle(Op, DAG);
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006592 if (NewOp.getNode())
6593 return NewOp;
Eric Christopherfd179292009-08-27 18:07:15 +00006594
Craig Topper5aaffa82012-02-19 02:53:47 +00006595 SmallVector<int, 8> M(SVOp->getMask().begin(), SVOp->getMask().end());
6596
Bruno Cardoso Lopesa22c8452010-09-04 00:39:43 +00006597 // NOTE: isPSHUFDMask can also match both masks below (unpckl_undef and
6598 // unpckh_undef). Only use pshufd if speed is more important than size.
Craig Topper5aaffa82012-02-19 02:53:47 +00006599 if (OptForSize && isUNPCKL_v_undef_Mask(M, VT, HasAVX2))
Craig Topper34671b82011-12-06 08:21:25 +00006600 return getTargetShuffleNode(X86ISD::UNPCKL, dl, VT, V1, V1, DAG);
Craig Topper5aaffa82012-02-19 02:53:47 +00006601 if (OptForSize && isUNPCKH_v_undef_Mask(M, VT, HasAVX2))
Craig Topper34671b82011-12-06 08:21:25 +00006602 return getTargetShuffleNode(X86ISD::UNPCKH, dl, VT, V1, V1, DAG);
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00006603
Craig Topperdd637ae2012-02-19 05:41:45 +00006604 if (isMOVDDUPMask(M, VT) && Subtarget->hasSSE3() &&
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006605 V2IsUndef && RelaxedMayFoldVectorLoad(V1))
Evan Cheng835580f2010-10-07 20:50:20 +00006606 return getMOVDDup(Op, dl, V1, DAG);
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006607
Craig Topperdd637ae2012-02-19 05:41:45 +00006608 if (isMOVHLPS_v_undef_Mask(M, VT))
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006609 return getMOVHighToLow(Op, dl, DAG);
6610
6611 // Use to match splats
Craig Topper5aaffa82012-02-19 02:53:47 +00006612 if (HasSSE2 && isUNPCKHMask(M, VT, HasAVX2) && V2IsUndef &&
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006613 (VT == MVT::v2f64 || VT == MVT::v2i64))
Craig Topper34671b82011-12-06 08:21:25 +00006614 return getTargetShuffleNode(X86ISD::UNPCKH, dl, VT, V1, V1, DAG);
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006615
Craig Topper5aaffa82012-02-19 02:53:47 +00006616 if (isPSHUFDMask(M, VT)) {
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006617 // The actual implementation will match the mask in the if above and then
6618 // during isel it can match several different instructions, not only pshufd
6619 // as its name says, sad but true, emulate the behavior for now...
Craig Topperdd637ae2012-02-19 05:41:45 +00006620 if (isMOVDDUPMask(M, VT) && ((VT == MVT::v4f32 || VT == MVT::v2i64)))
6621 return getTargetShuffleNode(X86ISD::MOVLHPS, dl, VT, V1, V1, DAG);
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006622
Craig Topper5aaffa82012-02-19 02:53:47 +00006623 unsigned TargetMask = getShuffleSHUFImmediate(SVOp);
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006624
Craig Topperdbd98a42012-02-07 06:28:42 +00006625 if (HasAVX && (VT == MVT::v4f32 || VT == MVT::v2f64))
6626 return getTargetShuffleNode(X86ISD::VPERMILP, dl, VT, V1, TargetMask, DAG);
6627
Craig Topper1accb7e2012-01-10 06:54:16 +00006628 if (HasSSE2 && (VT == MVT::v4f32 || VT == MVT::v4i32))
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006629 return getTargetShuffleNode(X86ISD::PSHUFD, dl, VT, V1, TargetMask, DAG);
6630
Craig Topperb3982da2011-12-31 23:50:21 +00006631 return getTargetShuffleNode(X86ISD::SHUFP, dl, VT, V1, V1,
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00006632 TargetMask, DAG);
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006633 }
Eric Christopherfd179292009-08-27 18:07:15 +00006634
Evan Chengf26ffe92008-05-29 08:22:04 +00006635 // Check if this can be converted into a logical shift.
6636 bool isLeft = false;
6637 unsigned ShAmt = 0;
Dan Gohman475871a2008-07-27 21:46:04 +00006638 SDValue ShVal;
Craig Topper1accb7e2012-01-10 06:54:16 +00006639 bool isShift = HasSSE2 && isVectorShift(SVOp, DAG, isLeft, ShVal, ShAmt);
Evan Chengf26ffe92008-05-29 08:22:04 +00006640 if (isShift && ShVal.hasOneUse()) {
Scott Michelfdc40a02009-02-17 22:15:04 +00006641 // If the shifted value has multiple uses, it may be cheaper to use
Evan Chengf26ffe92008-05-29 08:22:04 +00006642 // v_set0 + movlhps or movhlps, etc.
Dan Gohman8a55ce42009-09-23 21:02:20 +00006643 EVT EltVT = VT.getVectorElementType();
6644 ShAmt *= EltVT.getSizeInBits();
Dale Johannesenace16102009-02-03 19:33:06 +00006645 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00006646 }
Eric Christopherfd179292009-08-27 18:07:15 +00006647
Craig Topper5aaffa82012-02-19 02:53:47 +00006648 if (isMOVLMask(M, VT)) {
Gabor Greifba36cb52008-08-28 21:40:38 +00006649 if (ISD::isBuildVectorAllZeros(V1.getNode()))
Dale Johannesenace16102009-02-03 19:33:06 +00006650 return getVZextMovL(VT, VT, V2, DAG, Subtarget, dl);
Craig Topperdd637ae2012-02-19 05:41:45 +00006651 if (!isMOVLPMask(M, VT)) {
Craig Topper1accb7e2012-01-10 06:54:16 +00006652 if (HasSSE2 && (VT == MVT::v2i64 || VT == MVT::v2f64))
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00006653 return getTargetShuffleNode(X86ISD::MOVSD, dl, VT, V1, V2, DAG);
6654
Bruno Cardoso Lopes4783a3e2010-09-01 22:59:03 +00006655 if (VT == MVT::v4i32 || VT == MVT::v4f32)
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00006656 return getTargetShuffleNode(X86ISD::MOVSS, dl, VT, V1, V2, DAG);
6657 }
Evan Cheng7e2ff772008-05-08 00:57:18 +00006658 }
Eric Christopherfd179292009-08-27 18:07:15 +00006659
Nate Begeman9008ca62009-04-27 18:41:29 +00006660 // FIXME: fold these into legal mask.
Craig Topperdd637ae2012-02-19 05:41:45 +00006661 if (isMOVLHPSMask(M, VT) && !isUNPCKLMask(M, VT, HasAVX2))
Craig Topper1accb7e2012-01-10 06:54:16 +00006662 return getMOVLowToHigh(Op, dl, DAG, HasSSE2);
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006663
Craig Topperdd637ae2012-02-19 05:41:45 +00006664 if (isMOVHLPSMask(M, VT))
Dale Johannesen0488fb62010-09-30 23:57:10 +00006665 return getMOVHighToLow(Op, dl, DAG);
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00006666
Craig Topperdd637ae2012-02-19 05:41:45 +00006667 if (V2IsUndef && isMOVSHDUPMask(M, VT, Subtarget))
Dale Johannesen0488fb62010-09-30 23:57:10 +00006668 return getTargetShuffleNode(X86ISD::MOVSHDUP, dl, VT, V1, DAG);
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00006669
Craig Topperdd637ae2012-02-19 05:41:45 +00006670 if (V2IsUndef && isMOVSLDUPMask(M, VT, Subtarget))
Dale Johannesen0488fb62010-09-30 23:57:10 +00006671 return getTargetShuffleNode(X86ISD::MOVSLDUP, dl, VT, V1, DAG);
Bruno Cardoso Lopes013bb3d2010-08-31 22:35:05 +00006672
Craig Topperdd637ae2012-02-19 05:41:45 +00006673 if (isMOVLPMask(M, VT))
Craig Topper1accb7e2012-01-10 06:54:16 +00006674 return getMOVLP(Op, dl, DAG, HasSSE2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006675
Craig Topperdd637ae2012-02-19 05:41:45 +00006676 if (ShouldXformToMOVHLPS(M, VT) ||
6677 ShouldXformToMOVLP(V1.getNode(), V2.getNode(), M, VT))
Nate Begeman9008ca62009-04-27 18:41:29 +00006678 return CommuteVectorShuffle(SVOp, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006679
Evan Chengf26ffe92008-05-29 08:22:04 +00006680 if (isShift) {
Craig Toppered2e13d2012-01-22 19:15:14 +00006681 // No better options. Use a vshldq / vsrldq.
Dan Gohman8a55ce42009-09-23 21:02:20 +00006682 EVT EltVT = VT.getVectorElementType();
6683 ShAmt *= EltVT.getSizeInBits();
Dale Johannesenace16102009-02-03 19:33:06 +00006684 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00006685 }
Eric Christopherfd179292009-08-27 18:07:15 +00006686
Evan Cheng9eca5e82006-10-25 21:49:50 +00006687 bool Commuted = false;
Chris Lattner8a594482007-11-25 00:24:49 +00006688 // FIXME: This should also accept a bitcast of a splat? Be careful, not
6689 // 1,1,1,1 -> v8i16 though.
Gabor Greifba36cb52008-08-28 21:40:38 +00006690 V1IsSplat = isSplatVector(V1.getNode());
6691 V2IsSplat = isSplatVector(V2.getNode());
Scott Michelfdc40a02009-02-17 22:15:04 +00006692
Chris Lattner8a594482007-11-25 00:24:49 +00006693 // Canonicalize the splat or undef, if present, to be on the RHS.
Craig Topper39a9e482012-02-11 06:24:48 +00006694 if (!V2IsUndef && V1IsSplat && !V2IsSplat) {
6695 CommuteVectorShuffleMask(M, NumElems);
6696 std::swap(V1, V2);
Evan Cheng9bbbb982006-10-25 20:48:19 +00006697 std::swap(V1IsSplat, V2IsSplat);
Evan Cheng9eca5e82006-10-25 21:49:50 +00006698 Commuted = true;
Evan Cheng9bbbb982006-10-25 20:48:19 +00006699 }
6700
Craig Topperbeabc6c2011-12-05 06:56:46 +00006701 if (isCommutedMOVLMask(M, VT, V2IsSplat, V2IsUndef)) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006702 // Shuffling low element of v1 into undef, just return v1.
Eric Christopherfd179292009-08-27 18:07:15 +00006703 if (V2IsUndef)
Nate Begeman9008ca62009-04-27 18:41:29 +00006704 return V1;
6705 // If V2 is a splat, the mask may be malformed such as <4,3,3,3>, which
6706 // the instruction selector will not match, so get a canonical MOVL with
6707 // swapped operands to undo the commute.
6708 return getMOVL(DAG, dl, VT, V2, V1);
Evan Chengd9b8e402006-10-16 06:36:00 +00006709 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00006710
Craig Topperbeabc6c2011-12-05 06:56:46 +00006711 if (isUNPCKLMask(M, VT, HasAVX2))
Craig Topper34671b82011-12-06 08:21:25 +00006712 return getTargetShuffleNode(X86ISD::UNPCKL, dl, VT, V1, V2, DAG);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00006713
Craig Topperbeabc6c2011-12-05 06:56:46 +00006714 if (isUNPCKHMask(M, VT, HasAVX2))
Craig Topper34671b82011-12-06 08:21:25 +00006715 return getTargetShuffleNode(X86ISD::UNPCKH, dl, VT, V1, V2, DAG);
Evan Chenge1113032006-10-04 18:33:38 +00006716
Evan Cheng9bbbb982006-10-25 20:48:19 +00006717 if (V2IsSplat) {
6718 // Normalize mask so all entries that point to V2 points to its first
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00006719 // element then try to match unpck{h|l} again. If match, return a
Craig Topper39a9e482012-02-11 06:24:48 +00006720 // new vector_shuffle with the corrected mask.p
6721 SmallVector<int, 8> NewMask(M.begin(), M.end());
6722 NormalizeMask(NewMask, NumElems);
Craig Topper69947b92012-04-23 06:57:04 +00006723 if (isUNPCKLMask(NewMask, VT, HasAVX2, true))
Craig Topper39a9e482012-02-11 06:24:48 +00006724 return getTargetShuffleNode(X86ISD::UNPCKL, dl, VT, V1, V2, DAG);
Craig Topper69947b92012-04-23 06:57:04 +00006725 if (isUNPCKHMask(NewMask, VT, HasAVX2, true))
Craig Topper39a9e482012-02-11 06:24:48 +00006726 return getTargetShuffleNode(X86ISD::UNPCKH, dl, VT, V1, V2, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006727 }
6728
Evan Cheng9eca5e82006-10-25 21:49:50 +00006729 if (Commuted) {
6730 // Commute is back and try unpck* again.
Nate Begeman9008ca62009-04-27 18:41:29 +00006731 // FIXME: this seems wrong.
Craig Topper39a9e482012-02-11 06:24:48 +00006732 CommuteVectorShuffleMask(M, NumElems);
6733 std::swap(V1, V2);
6734 std::swap(V1IsSplat, V2IsSplat);
6735 Commuted = false;
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00006736
Craig Topper39a9e482012-02-11 06:24:48 +00006737 if (isUNPCKLMask(M, VT, HasAVX2))
6738 return getTargetShuffleNode(X86ISD::UNPCKL, dl, VT, V1, V2, DAG);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00006739
Craig Topper39a9e482012-02-11 06:24:48 +00006740 if (isUNPCKHMask(M, VT, HasAVX2))
6741 return getTargetShuffleNode(X86ISD::UNPCKH, dl, VT, V1, V2, DAG);
Evan Cheng9eca5e82006-10-25 21:49:50 +00006742 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00006743
Nate Begeman9008ca62009-04-27 18:41:29 +00006744 // Normalize the node to match x86 shuffle ops if needed
Craig Topper1a7700a2012-01-19 08:19:12 +00006745 if (!V2IsUndef && (isSHUFPMask(M, VT, HasAVX, /* Commuted */ true)))
Nate Begeman9008ca62009-04-27 18:41:29 +00006746 return CommuteVectorShuffle(SVOp, DAG);
6747
Bruno Cardoso Lopes7256e222010-09-03 23:24:06 +00006748 // The checks below are all present in isShuffleMaskLegal, but they are
6749 // inlined here right now to enable us to directly emit target specific
6750 // nodes, and remove one by one until they don't return Op anymore.
Bruno Cardoso Lopes7256e222010-09-03 23:24:06 +00006751
Craig Topper0e2037b2012-01-20 05:53:00 +00006752 if (isPALIGNRMask(M, VT, Subtarget))
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00006753 return getTargetShuffleNode(X86ISD::PALIGN, dl, VT, V1, V2,
Craig Topperd93e4c32011-12-11 19:12:35 +00006754 getShufflePALIGNRImmediate(SVOp),
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00006755 DAG);
6756
Bruno Cardoso Lopesc800c0d2010-09-04 02:02:14 +00006757 if (ShuffleVectorSDNode::isSplatMask(&M[0], VT) &&
6758 SVOp->getSplatIndex() == 0 && V2IsUndef) {
Craig Topperbeabc6c2011-12-05 06:56:46 +00006759 if (VT == MVT::v2f64 || VT == MVT::v2i64)
Craig Topper34671b82011-12-06 08:21:25 +00006760 return getTargetShuffleNode(X86ISD::UNPCKL, dl, VT, V1, V1, DAG);
Bruno Cardoso Lopesc800c0d2010-09-04 02:02:14 +00006761 }
6762
Craig Toppera9a568a2012-05-02 08:03:44 +00006763 if (isPSHUFHWMask(M, VT, HasAVX2))
Bruno Cardoso Lopesbbfc3102010-09-04 01:36:45 +00006764 return getTargetShuffleNode(X86ISD::PSHUFHW, dl, VT, V1,
Craig Topper5aaffa82012-02-19 02:53:47 +00006765 getShufflePSHUFHWImmediate(SVOp),
Bruno Cardoso Lopesbbfc3102010-09-04 01:36:45 +00006766 DAG);
6767
Craig Toppera9a568a2012-05-02 08:03:44 +00006768 if (isPSHUFLWMask(M, VT, HasAVX2))
Bruno Cardoso Lopesbbfc3102010-09-04 01:36:45 +00006769 return getTargetShuffleNode(X86ISD::PSHUFLW, dl, VT, V1,
Craig Topper5aaffa82012-02-19 02:53:47 +00006770 getShufflePSHUFLWImmediate(SVOp),
Bruno Cardoso Lopesbbfc3102010-09-04 01:36:45 +00006771 DAG);
6772
Craig Topper1a7700a2012-01-19 08:19:12 +00006773 if (isSHUFPMask(M, VT, HasAVX))
Craig Topperb3982da2011-12-31 23:50:21 +00006774 return getTargetShuffleNode(X86ISD::SHUFP, dl, VT, V1, V2,
Craig Topper5aaffa82012-02-19 02:53:47 +00006775 getShuffleSHUFImmediate(SVOp), DAG);
Bruno Cardoso Lopes4c827f52010-09-04 01:22:57 +00006776
Craig Topper94438ba2011-12-16 08:06:31 +00006777 if (isUNPCKL_v_undef_Mask(M, VT, HasAVX2))
Craig Topper34671b82011-12-06 08:21:25 +00006778 return getTargetShuffleNode(X86ISD::UNPCKL, dl, VT, V1, V1, DAG);
Craig Topper94438ba2011-12-16 08:06:31 +00006779 if (isUNPCKH_v_undef_Mask(M, VT, HasAVX2))
Craig Topper34671b82011-12-06 08:21:25 +00006780 return getTargetShuffleNode(X86ISD::UNPCKH, dl, VT, V1, V1, DAG);
Bruno Cardoso Lopesa22c8452010-09-04 00:39:43 +00006781
Bruno Cardoso Lopesd0888342011-07-22 00:14:56 +00006782 //===--------------------------------------------------------------------===//
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00006783 // Generate target specific nodes for 128 or 256-bit shuffles only
6784 // supported in the AVX instruction set.
6785 //
Bruno Cardoso Lopesd0888342011-07-22 00:14:56 +00006786
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00006787 // Handle VMOVDDUPY permutations
Craig Topperbeabc6c2011-12-05 06:56:46 +00006788 if (V2IsUndef && isMOVDDUPYMask(M, VT, HasAVX))
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00006789 return getTargetShuffleNode(X86ISD::MOVDDUP, dl, VT, V1, DAG);
6790
Craig Topper70b883b2011-11-28 10:14:51 +00006791 // Handle VPERMILPS/D* permutations
Craig Topperdbd98a42012-02-07 06:28:42 +00006792 if (isVPERMILPMask(M, VT, HasAVX)) {
6793 if (HasAVX2 && VT == MVT::v8i32)
6794 return getTargetShuffleNode(X86ISD::PSHUFD, dl, VT, V1,
Craig Topper5aaffa82012-02-19 02:53:47 +00006795 getShuffleSHUFImmediate(SVOp), DAG);
Craig Topper316cd2a2011-11-30 06:25:25 +00006796 return getTargetShuffleNode(X86ISD::VPERMILP, dl, VT, V1,
Craig Topper5aaffa82012-02-19 02:53:47 +00006797 getShuffleSHUFImmediate(SVOp), DAG);
Craig Topperdbd98a42012-02-07 06:28:42 +00006798 }
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00006799
Craig Topper70b883b2011-11-28 10:14:51 +00006800 // Handle VPERM2F128/VPERM2I128 permutations
Craig Topperbeabc6c2011-12-05 06:56:46 +00006801 if (isVPERM2X128Mask(M, VT, HasAVX))
Craig Topperec24e612011-11-30 07:47:51 +00006802 return getTargetShuffleNode(X86ISD::VPERM2X128, dl, VT, V1,
Craig Topper70b883b2011-11-28 10:14:51 +00006803 V2, getShuffleVPERM2X128Immediate(SVOp), DAG);
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00006804
Craig Topper1842ba02012-04-23 06:38:28 +00006805 SDValue BlendOp = LowerVECTOR_SHUFFLEtoBlend(SVOp, Subtarget, DAG);
Nadav Roteme80aa7c2012-04-09 08:33:21 +00006806 if (BlendOp.getNode())
6807 return BlendOp;
Craig Topper095c5282012-04-15 23:48:57 +00006808
Elena Demikhovsky73c504a2012-04-15 11:18:59 +00006809 if (V2IsUndef && HasAVX2 && (VT == MVT::v8i32 || VT == MVT::v8f32)) {
Craig Topper095c5282012-04-15 23:48:57 +00006810 SmallVector<SDValue, 8> permclMask;
Elena Demikhovsky73c504a2012-04-15 11:18:59 +00006811 for (unsigned i = 0; i != 8; ++i) {
Craig Topper095c5282012-04-15 23:48:57 +00006812 permclMask.push_back(DAG.getConstant((M[i]>=0) ? M[i] : 0, MVT::i32));
Elena Demikhovsky73c504a2012-04-15 11:18:59 +00006813 }
Craig Topper92040742012-04-16 06:43:40 +00006814 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v8i32,
6815 &permclMask[0], 8);
6816 // Bitcast is for VPERMPS since mask is v8i32 but node takes v8f32
Craig Topper8325c112012-04-16 00:41:45 +00006817 return DAG.getNode(X86ISD::VPERMV, dl, VT,
Craig Topper92040742012-04-16 06:43:40 +00006818 DAG.getNode(ISD::BITCAST, dl, VT, Mask), V1);
Elena Demikhovsky73c504a2012-04-15 11:18:59 +00006819 }
Craig Topper095c5282012-04-15 23:48:57 +00006820
Craig Topper8325c112012-04-16 00:41:45 +00006821 if (V2IsUndef && HasAVX2 && (VT == MVT::v4i64 || VT == MVT::v4f64))
6822 return getTargetShuffleNode(X86ISD::VPERMI, dl, VT, V1,
Elena Demikhovsky73c504a2012-04-15 11:18:59 +00006823 getShuffleCLImmediate(SVOp), DAG);
6824
Nadav Roteme80aa7c2012-04-09 08:33:21 +00006825
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00006826 //===--------------------------------------------------------------------===//
6827 // Since no target specific shuffle was selected for this generic one,
6828 // lower it into other known shuffles. FIXME: this isn't true yet, but
6829 // this is the plan.
6830 //
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00006831
Bruno Cardoso Lopes9b4ad122011-07-27 00:56:37 +00006832 // Handle v8i16 specifically since SSE can do byte extraction and insertion.
6833 if (VT == MVT::v8i16) {
Craig Topper55b24052012-09-11 06:15:32 +00006834 SDValue NewOp = LowerVECTOR_SHUFFLEv8i16(Op, Subtarget, DAG);
Bruno Cardoso Lopes9b4ad122011-07-27 00:56:37 +00006835 if (NewOp.getNode())
6836 return NewOp;
6837 }
6838
6839 if (VT == MVT::v16i8) {
6840 SDValue NewOp = LowerVECTOR_SHUFFLEv16i8(SVOp, DAG, *this);
6841 if (NewOp.getNode())
6842 return NewOp;
6843 }
6844
Elena Demikhovsky41789462012-09-06 12:42:01 +00006845 if (VT == MVT::v32i8) {
Craig Topper55b24052012-09-11 06:15:32 +00006846 SDValue NewOp = LowerVECTOR_SHUFFLEv32i8(SVOp, Subtarget, DAG);
Elena Demikhovsky41789462012-09-06 12:42:01 +00006847 if (NewOp.getNode())
6848 return NewOp;
6849 }
6850
Bruno Cardoso Lopes9b4ad122011-07-27 00:56:37 +00006851 // Handle all 128-bit wide vectors with 4 elements, and match them with
6852 // several different shuffle types.
Craig Topper7a9a28b2012-08-12 02:23:29 +00006853 if (NumElems == 4 && VT.is128BitVector())
Bruno Cardoso Lopes9b4ad122011-07-27 00:56:37 +00006854 return LowerVECTOR_SHUFFLE_128v4(SVOp, DAG);
6855
Bruno Cardoso Lopesd0888342011-07-22 00:14:56 +00006856 // Handle general 256-bit shuffles
6857 if (VT.is256BitVector())
6858 return LowerVECTOR_SHUFFLE_256(SVOp, DAG);
6859
Dan Gohman475871a2008-07-27 21:46:04 +00006860 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006861}
6862
Dan Gohman475871a2008-07-27 21:46:04 +00006863SDValue
6864X86TargetLowering::LowerEXTRACT_VECTOR_ELT_SSE4(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00006865 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00006866 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006867 DebugLoc dl = Op.getDebugLoc();
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00006868
Craig Topper7a9a28b2012-08-12 02:23:29 +00006869 if (!Op.getOperand(0).getValueType().is128BitVector())
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00006870 return SDValue();
6871
Duncan Sands83ec4b62008-06-06 12:08:01 +00006872 if (VT.getSizeInBits() == 8) {
Owen Anderson825b72b2009-08-11 20:47:22 +00006873 SDValue Extract = DAG.getNode(X86ISD::PEXTRB, dl, MVT::i32,
Craig Topper7c022842012-09-12 06:20:41 +00006874 Op.getOperand(0), Op.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00006875 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Extract,
Craig Topper7c022842012-09-12 06:20:41 +00006876 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00006877 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Craig Topper69947b92012-04-23 06:57:04 +00006878 }
6879
6880 if (VT.getSizeInBits() == 16) {
Evan Cheng52ceafa2009-01-02 05:29:08 +00006881 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
6882 // If Idx is 0, it's cheaper to do a move instead of a pextrw.
6883 if (Idx == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00006884 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i16,
6885 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006886 DAG.getNode(ISD::BITCAST, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00006887 MVT::v4i32,
Evan Cheng52ceafa2009-01-02 05:29:08 +00006888 Op.getOperand(0)),
6889 Op.getOperand(1)));
Owen Anderson825b72b2009-08-11 20:47:22 +00006890 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, dl, MVT::i32,
Craig Topper7c022842012-09-12 06:20:41 +00006891 Op.getOperand(0), Op.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00006892 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Extract,
Craig Topper7c022842012-09-12 06:20:41 +00006893 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00006894 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Craig Topper69947b92012-04-23 06:57:04 +00006895 }
6896
6897 if (VT == MVT::f32) {
Evan Cheng62a3f152008-03-24 21:52:23 +00006898 // EXTRACTPS outputs to a GPR32 register which will require a movd to copy
6899 // the result back to FR32 register. It's only worth matching if the
Dan Gohmand17cfbe2008-10-31 00:57:24 +00006900 // result has a single use which is a store or a bitcast to i32. And in
6901 // the case of a store, it's not worth it if the index is a constant 0,
6902 // because a MOVSSmr can be used instead, which is smaller and faster.
Evan Cheng62a3f152008-03-24 21:52:23 +00006903 if (!Op.hasOneUse())
Dan Gohman475871a2008-07-27 21:46:04 +00006904 return SDValue();
Gabor Greifba36cb52008-08-28 21:40:38 +00006905 SDNode *User = *Op.getNode()->use_begin();
Dan Gohmand17cfbe2008-10-31 00:57:24 +00006906 if ((User->getOpcode() != ISD::STORE ||
6907 (isa<ConstantSDNode>(Op.getOperand(1)) &&
6908 cast<ConstantSDNode>(Op.getOperand(1))->isNullValue())) &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006909 (User->getOpcode() != ISD::BITCAST ||
Owen Anderson825b72b2009-08-11 20:47:22 +00006910 User->getValueType(0) != MVT::i32))
Dan Gohman475871a2008-07-27 21:46:04 +00006911 return SDValue();
Owen Anderson825b72b2009-08-11 20:47:22 +00006912 SDValue Extract = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006913 DAG.getNode(ISD::BITCAST, dl, MVT::v4i32,
Dale Johannesenace16102009-02-03 19:33:06 +00006914 Op.getOperand(0)),
6915 Op.getOperand(1));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006916 return DAG.getNode(ISD::BITCAST, dl, MVT::f32, Extract);
Craig Topper69947b92012-04-23 06:57:04 +00006917 }
6918
6919 if (VT == MVT::i32 || VT == MVT::i64) {
Pete Coopera77214a2011-11-14 19:38:42 +00006920 // ExtractPS/pextrq works with constant index.
Mon P Wangf0fcdd82009-01-15 21:10:20 +00006921 if (isa<ConstantSDNode>(Op.getOperand(1)))
6922 return Op;
Nate Begeman14d12ca2008-02-11 04:19:36 +00006923 }
Dan Gohman475871a2008-07-27 21:46:04 +00006924 return SDValue();
Nate Begeman14d12ca2008-02-11 04:19:36 +00006925}
6926
6927
Dan Gohman475871a2008-07-27 21:46:04 +00006928SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006929X86TargetLowering::LowerEXTRACT_VECTOR_ELT(SDValue Op,
6930 SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00006931 if (!isa<ConstantSDNode>(Op.getOperand(1)))
Dan Gohman475871a2008-07-27 21:46:04 +00006932 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006933
David Greene74a579d2011-02-10 16:57:36 +00006934 SDValue Vec = Op.getOperand(0);
6935 EVT VecVT = Vec.getValueType();
6936
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00006937 // If this is a 256-bit vector result, first extract the 128-bit vector and
6938 // then extract the element from the 128-bit vector.
Craig Topper7a9a28b2012-08-12 02:23:29 +00006939 if (VecVT.is256BitVector()) {
David Greene74a579d2011-02-10 16:57:36 +00006940 DebugLoc dl = Op.getNode()->getDebugLoc();
6941 unsigned NumElems = VecVT.getVectorNumElements();
6942 SDValue Idx = Op.getOperand(1);
David Greene74a579d2011-02-10 16:57:36 +00006943 unsigned IdxVal = cast<ConstantSDNode>(Idx)->getZExtValue();
6944
6945 // Get the 128-bit vector.
Craig Topper7d1e3dc2012-04-30 05:17:10 +00006946 Vec = Extract128BitVector(Vec, IdxVal, DAG, dl);
David Greene74a579d2011-02-10 16:57:36 +00006947
Craig Topper7d1e3dc2012-04-30 05:17:10 +00006948 if (IdxVal >= NumElems/2)
6949 IdxVal -= NumElems/2;
David Greene74a579d2011-02-10 16:57:36 +00006950 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, Op.getValueType(), Vec,
Craig Topper7d1e3dc2012-04-30 05:17:10 +00006951 DAG.getConstant(IdxVal, MVT::i32));
David Greene74a579d2011-02-10 16:57:36 +00006952 }
6953
Craig Topper7a9a28b2012-08-12 02:23:29 +00006954 assert(VecVT.is128BitVector() && "Unexpected vector length");
David Greene74a579d2011-02-10 16:57:36 +00006955
Craig Topperd0a31172012-01-10 06:37:29 +00006956 if (Subtarget->hasSSE41()) {
Dan Gohman475871a2008-07-27 21:46:04 +00006957 SDValue Res = LowerEXTRACT_VECTOR_ELT_SSE4(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00006958 if (Res.getNode())
Evan Cheng62a3f152008-03-24 21:52:23 +00006959 return Res;
6960 }
Nate Begeman14d12ca2008-02-11 04:19:36 +00006961
Owen Andersone50ed302009-08-10 22:56:29 +00006962 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006963 DebugLoc dl = Op.getDebugLoc();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006964 // TODO: handle v16i8.
Duncan Sands83ec4b62008-06-06 12:08:01 +00006965 if (VT.getSizeInBits() == 16) {
Dan Gohman475871a2008-07-27 21:46:04 +00006966 SDValue Vec = Op.getOperand(0);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006967 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00006968 if (Idx == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00006969 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i16,
6970 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006971 DAG.getNode(ISD::BITCAST, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00006972 MVT::v4i32, Vec),
Evan Cheng14b32e12007-12-11 01:46:18 +00006973 Op.getOperand(1)));
Evan Cheng0db9fe62006-04-25 20:13:52 +00006974 // Transform it so it match pextrw which produces a 32-bit result.
Ken Dyck70d0ef12009-12-17 15:31:52 +00006975 EVT EltVT = MVT::i32;
Dan Gohman8a55ce42009-09-23 21:02:20 +00006976 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, dl, EltVT,
Craig Topper7c022842012-09-12 06:20:41 +00006977 Op.getOperand(0), Op.getOperand(1));
Dan Gohman8a55ce42009-09-23 21:02:20 +00006978 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, EltVT, Extract,
Craig Topper7c022842012-09-12 06:20:41 +00006979 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00006980 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Craig Topper69947b92012-04-23 06:57:04 +00006981 }
6982
6983 if (VT.getSizeInBits() == 32) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006984 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006985 if (Idx == 0)
6986 return Op;
Eric Christopherfd179292009-08-27 18:07:15 +00006987
Evan Cheng0db9fe62006-04-25 20:13:52 +00006988 // SHUFPS the element to the lowest double word, then movss.
Jeffrey Yasskina44defe2011-07-27 06:22:51 +00006989 int Mask[4] = { static_cast<int>(Idx), -1, -1, -1 };
Owen Andersone50ed302009-08-10 22:56:29 +00006990 EVT VVT = Op.getOperand(0).getValueType();
Eric Christopherfd179292009-08-27 18:07:15 +00006991 SDValue Vec = DAG.getVectorShuffle(VVT, dl, Op.getOperand(0),
Nate Begeman9008ca62009-04-27 18:41:29 +00006992 DAG.getUNDEF(VVT), Mask);
Dale Johannesenace16102009-02-03 19:33:06 +00006993 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, Vec,
Chris Lattner0bd48932008-01-17 07:00:52 +00006994 DAG.getIntPtrConstant(0));
Craig Topper69947b92012-04-23 06:57:04 +00006995 }
6996
6997 if (VT.getSizeInBits() == 64) {
Nate Begeman14d12ca2008-02-11 04:19:36 +00006998 // FIXME: .td only matches this for <2 x f64>, not <2 x i64> on 32b
6999 // FIXME: seems like this should be unnecessary if mov{h,l}pd were taught
7000 // to match extract_elt for f64.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007001 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007002 if (Idx == 0)
7003 return Op;
7004
7005 // UNPCKHPD the element to the lowest double word, then movsd.
7006 // Note if the lower 64 bits of the result of the UNPCKHPD is then stored
7007 // to a f64mem, the whole operation is folded into a single MOVHPDmr.
Nate Begeman9008ca62009-04-27 18:41:29 +00007008 int Mask[2] = { 1, -1 };
Owen Andersone50ed302009-08-10 22:56:29 +00007009 EVT VVT = Op.getOperand(0).getValueType();
Eric Christopherfd179292009-08-27 18:07:15 +00007010 SDValue Vec = DAG.getVectorShuffle(VVT, dl, Op.getOperand(0),
Nate Begeman9008ca62009-04-27 18:41:29 +00007011 DAG.getUNDEF(VVT), Mask);
Dale Johannesenace16102009-02-03 19:33:06 +00007012 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, Vec,
Chris Lattner0bd48932008-01-17 07:00:52 +00007013 DAG.getIntPtrConstant(0));
Evan Cheng0db9fe62006-04-25 20:13:52 +00007014 }
7015
Dan Gohman475871a2008-07-27 21:46:04 +00007016 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007017}
7018
Dan Gohman475871a2008-07-27 21:46:04 +00007019SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007020X86TargetLowering::LowerINSERT_VECTOR_ELT_SSE4(SDValue Op,
7021 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00007022 EVT VT = Op.getValueType();
Dan Gohman8a55ce42009-09-23 21:02:20 +00007023 EVT EltVT = VT.getVectorElementType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007024 DebugLoc dl = Op.getDebugLoc();
Nate Begeman14d12ca2008-02-11 04:19:36 +00007025
Dan Gohman475871a2008-07-27 21:46:04 +00007026 SDValue N0 = Op.getOperand(0);
7027 SDValue N1 = Op.getOperand(1);
7028 SDValue N2 = Op.getOperand(2);
Nate Begeman14d12ca2008-02-11 04:19:36 +00007029
Craig Topper7a9a28b2012-08-12 02:23:29 +00007030 if (!VT.is128BitVector())
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007031 return SDValue();
7032
Dan Gohman8a55ce42009-09-23 21:02:20 +00007033 if ((EltVT.getSizeInBits() == 8 || EltVT.getSizeInBits() == 16) &&
Dan Gohmanef521f12008-08-14 22:53:18 +00007034 isa<ConstantSDNode>(N2)) {
Chris Lattner8f2b4cc2010-02-23 02:07:48 +00007035 unsigned Opc;
7036 if (VT == MVT::v8i16)
7037 Opc = X86ISD::PINSRW;
Chris Lattner8f2b4cc2010-02-23 02:07:48 +00007038 else if (VT == MVT::v16i8)
7039 Opc = X86ISD::PINSRB;
7040 else
7041 Opc = X86ISD::PINSRB;
7042
Nate Begeman14d12ca2008-02-11 04:19:36 +00007043 // Transform it so it match pinsr{b,w} which expects a GR32 as its second
7044 // argument.
Owen Anderson825b72b2009-08-11 20:47:22 +00007045 if (N1.getValueType() != MVT::i32)
7046 N1 = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, N1);
7047 if (N2.getValueType() != MVT::i32)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007048 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Dale Johannesenace16102009-02-03 19:33:06 +00007049 return DAG.getNode(Opc, dl, VT, N0, N1, N2);
Craig Topper69947b92012-04-23 06:57:04 +00007050 }
7051
7052 if (EltVT == MVT::f32 && isa<ConstantSDNode>(N2)) {
Nate Begeman14d12ca2008-02-11 04:19:36 +00007053 // Bits [7:6] of the constant are the source select. This will always be
7054 // zero here. The DAG Combiner may combine an extract_elt index into these
7055 // bits. For example (insert (extract, 3), 2) could be matched by putting
7056 // the '3' into bits [7:6] of X86ISD::INSERTPS.
Scott Michelfdc40a02009-02-17 22:15:04 +00007057 // Bits [5:4] of the constant are the destination select. This is the
Nate Begeman14d12ca2008-02-11 04:19:36 +00007058 // value of the incoming immediate.
Scott Michelfdc40a02009-02-17 22:15:04 +00007059 // Bits [3:0] of the constant are the zero mask. The DAG Combiner may
Nate Begeman14d12ca2008-02-11 04:19:36 +00007060 // combine either bitwise AND or insert of float 0.0 to set these bits.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007061 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue() << 4);
Eric Christopherfbd66872009-07-24 00:33:09 +00007062 // Create this as a scalar to vector..
Owen Anderson825b72b2009-08-11 20:47:22 +00007063 N1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4f32, N1);
Dale Johannesenace16102009-02-03 19:33:06 +00007064 return DAG.getNode(X86ISD::INSERTPS, dl, VT, N0, N1, N2);
Craig Topper69947b92012-04-23 06:57:04 +00007065 }
7066
7067 if ((EltVT == MVT::i32 || EltVT == MVT::i64) && isa<ConstantSDNode>(N2)) {
Eric Christopherfbd66872009-07-24 00:33:09 +00007068 // PINSR* works with constant index.
7069 return Op;
Nate Begeman14d12ca2008-02-11 04:19:36 +00007070 }
Dan Gohman475871a2008-07-27 21:46:04 +00007071 return SDValue();
Nate Begeman14d12ca2008-02-11 04:19:36 +00007072}
7073
Dan Gohman475871a2008-07-27 21:46:04 +00007074SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007075X86TargetLowering::LowerINSERT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00007076 EVT VT = Op.getValueType();
Dan Gohman8a55ce42009-09-23 21:02:20 +00007077 EVT EltVT = VT.getVectorElementType();
Nate Begeman14d12ca2008-02-11 04:19:36 +00007078
David Greene6b381262011-02-09 15:32:06 +00007079 DebugLoc dl = Op.getDebugLoc();
7080 SDValue N0 = Op.getOperand(0);
7081 SDValue N1 = Op.getOperand(1);
7082 SDValue N2 = Op.getOperand(2);
7083
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007084 // If this is a 256-bit vector result, first extract the 128-bit vector,
7085 // insert the element into the extracted half and then place it back.
Craig Topper7a9a28b2012-08-12 02:23:29 +00007086 if (VT.is256BitVector()) {
David Greene6b381262011-02-09 15:32:06 +00007087 if (!isa<ConstantSDNode>(N2))
7088 return SDValue();
7089
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007090 // Get the desired 128-bit vector half.
David Greene6b381262011-02-09 15:32:06 +00007091 unsigned NumElems = VT.getVectorNumElements();
7092 unsigned IdxVal = cast<ConstantSDNode>(N2)->getZExtValue();
Craig Topper7d1e3dc2012-04-30 05:17:10 +00007093 SDValue V = Extract128BitVector(N0, IdxVal, DAG, dl);
David Greene6b381262011-02-09 15:32:06 +00007094
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007095 // Insert the element into the desired half.
Craig Topper7d1e3dc2012-04-30 05:17:10 +00007096 bool Upper = IdxVal >= NumElems/2;
7097 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, V.getValueType(), V, N1,
7098 DAG.getConstant(Upper ? IdxVal-NumElems/2 : IdxVal, MVT::i32));
David Greene6b381262011-02-09 15:32:06 +00007099
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007100 // Insert the changed part back to the 256-bit vector
Craig Topper7d1e3dc2012-04-30 05:17:10 +00007101 return Insert128BitVector(N0, V, IdxVal, DAG, dl);
David Greene6b381262011-02-09 15:32:06 +00007102 }
7103
Craig Topperd0a31172012-01-10 06:37:29 +00007104 if (Subtarget->hasSSE41())
Nate Begeman14d12ca2008-02-11 04:19:36 +00007105 return LowerINSERT_VECTOR_ELT_SSE4(Op, DAG);
7106
Dan Gohman8a55ce42009-09-23 21:02:20 +00007107 if (EltVT == MVT::i8)
Dan Gohman475871a2008-07-27 21:46:04 +00007108 return SDValue();
Evan Cheng794405e2007-12-12 07:55:34 +00007109
Dan Gohman8a55ce42009-09-23 21:02:20 +00007110 if (EltVT.getSizeInBits() == 16 && isa<ConstantSDNode>(N2)) {
Evan Cheng794405e2007-12-12 07:55:34 +00007111 // Transform it so it match pinsrw which expects a 16-bit value in a GR32
7112 // as its second argument.
Owen Anderson825b72b2009-08-11 20:47:22 +00007113 if (N1.getValueType() != MVT::i32)
7114 N1 = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, N1);
7115 if (N2.getValueType() != MVT::i32)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007116 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Dale Johannesen0488fb62010-09-30 23:57:10 +00007117 return DAG.getNode(X86ISD::PINSRW, dl, VT, N0, N1, N2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007118 }
Dan Gohman475871a2008-07-27 21:46:04 +00007119 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007120}
7121
Craig Topper55b24052012-09-11 06:15:32 +00007122static SDValue LowerSCALAR_TO_VECTOR(SDValue Op, SelectionDAG &DAG) {
Bruno Cardoso Lopes233fa392011-07-25 23:05:16 +00007123 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007124 DebugLoc dl = Op.getDebugLoc();
David Greene2fcdfb42011-02-10 23:11:29 +00007125 EVT OpVT = Op.getValueType();
7126
Bruno Cardoso Lopes233fa392011-07-25 23:05:16 +00007127 // If this is a 256-bit vector result, first insert into a 128-bit
7128 // vector and then insert into the 256-bit vector.
Craig Topper7a9a28b2012-08-12 02:23:29 +00007129 if (!OpVT.is128BitVector()) {
Bruno Cardoso Lopes233fa392011-07-25 23:05:16 +00007130 // Insert into a 128-bit vector.
7131 EVT VT128 = EVT::getVectorVT(*Context,
7132 OpVT.getVectorElementType(),
7133 OpVT.getVectorNumElements() / 2);
7134
7135 Op = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT128, Op.getOperand(0));
7136
7137 // Insert the 128-bit vector.
Craig Topperb14940a2012-04-22 20:55:18 +00007138 return Insert128BitVector(DAG.getUNDEF(OpVT), Op, 0, DAG, dl);
Bruno Cardoso Lopes233fa392011-07-25 23:05:16 +00007139 }
7140
Craig Topperd77d2fe2012-04-29 20:22:05 +00007141 if (OpVT == MVT::v1i64 &&
Chris Lattnerf172ecd2010-07-04 23:07:25 +00007142 Op.getOperand(0).getValueType() == MVT::i64)
Owen Anderson825b72b2009-08-11 20:47:22 +00007143 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v1i64, Op.getOperand(0));
Rafael Espindoladef390a2009-08-03 02:45:34 +00007144
Owen Anderson825b72b2009-08-11 20:47:22 +00007145 SDValue AnyExt = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, Op.getOperand(0));
Craig Topper7a9a28b2012-08-12 02:23:29 +00007146 assert(OpVT.is128BitVector() && "Expected an SSE type!");
Craig Topperd77d2fe2012-04-29 20:22:05 +00007147 return DAG.getNode(ISD::BITCAST, dl, OpVT,
Dale Johannesen0488fb62010-09-30 23:57:10 +00007148 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,AnyExt));
Evan Cheng0db9fe62006-04-25 20:13:52 +00007149}
7150
David Greene91585092011-01-26 15:38:49 +00007151// Lower a node with an EXTRACT_SUBVECTOR opcode. This may result in
7152// a simple subregister reference or explicit instructions to grab
7153// upper bits of a vector.
Craig Topper55b24052012-09-11 06:15:32 +00007154static SDValue LowerEXTRACT_SUBVECTOR(SDValue Op, const X86Subtarget *Subtarget,
7155 SelectionDAG &DAG) {
David Greene91585092011-01-26 15:38:49 +00007156 if (Subtarget->hasAVX()) {
David Greenea5f26012011-02-07 19:36:54 +00007157 DebugLoc dl = Op.getNode()->getDebugLoc();
7158 SDValue Vec = Op.getNode()->getOperand(0);
7159 SDValue Idx = Op.getNode()->getOperand(1);
7160
Craig Topper7a9a28b2012-08-12 02:23:29 +00007161 if (Op.getNode()->getValueType(0).is128BitVector() &&
7162 Vec.getNode()->getValueType(0).is256BitVector() &&
Craig Topperb14940a2012-04-22 20:55:18 +00007163 isa<ConstantSDNode>(Idx)) {
7164 unsigned IdxVal = cast<ConstantSDNode>(Idx)->getZExtValue();
7165 return Extract128BitVector(Vec, IdxVal, DAG, dl);
David Greenea5f26012011-02-07 19:36:54 +00007166 }
David Greene91585092011-01-26 15:38:49 +00007167 }
7168 return SDValue();
7169}
7170
David Greenecfe33c42011-01-26 19:13:22 +00007171// Lower a node with an INSERT_SUBVECTOR opcode. This may result in a
7172// simple superregister reference or explicit instructions to insert
7173// the upper bits of a vector.
Craig Topper55b24052012-09-11 06:15:32 +00007174static SDValue LowerINSERT_SUBVECTOR(SDValue Op, const X86Subtarget *Subtarget,
7175 SelectionDAG &DAG) {
David Greenecfe33c42011-01-26 19:13:22 +00007176 if (Subtarget->hasAVX()) {
7177 DebugLoc dl = Op.getNode()->getDebugLoc();
7178 SDValue Vec = Op.getNode()->getOperand(0);
7179 SDValue SubVec = Op.getNode()->getOperand(1);
7180 SDValue Idx = Op.getNode()->getOperand(2);
7181
Craig Topper7a9a28b2012-08-12 02:23:29 +00007182 if (Op.getNode()->getValueType(0).is256BitVector() &&
7183 SubVec.getNode()->getValueType(0).is128BitVector() &&
Craig Topperb14940a2012-04-22 20:55:18 +00007184 isa<ConstantSDNode>(Idx)) {
7185 unsigned IdxVal = cast<ConstantSDNode>(Idx)->getZExtValue();
7186 return Insert128BitVector(Vec, SubVec, IdxVal, DAG, dl);
David Greenecfe33c42011-01-26 19:13:22 +00007187 }
7188 }
7189 return SDValue();
7190}
7191
Bill Wendling056292f2008-09-16 21:48:12 +00007192// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
7193// their target countpart wrapped in the X86ISD::Wrapper node. Suppose N is
7194// one of the above mentioned nodes. It has to be wrapped because otherwise
7195// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
7196// be used to form addressing mode. These wrapped nodes will be selected
7197// into MOV32ri.
Dan Gohman475871a2008-07-27 21:46:04 +00007198SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007199X86TargetLowering::LowerConstantPool(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00007200 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Eric Christopherfd179292009-08-27 18:07:15 +00007201
Chris Lattner41621a22009-06-26 19:22:52 +00007202 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
7203 // global base reg.
7204 unsigned char OpFlag = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00007205 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007206 CodeModel::Model M = getTargetMachine().getCodeModel();
7207
Chris Lattner4f066492009-07-11 20:29:19 +00007208 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007209 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattnere4df7562009-07-09 03:15:51 +00007210 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00007211 else if (Subtarget->isPICStyleGOT())
Chris Lattner88e1fd52009-07-09 04:24:46 +00007212 OpFlag = X86II::MO_GOTOFF;
Chris Lattnere2c92082009-07-10 21:00:45 +00007213 else if (Subtarget->isPICStyleStubPIC())
Chris Lattner88e1fd52009-07-09 04:24:46 +00007214 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Eric Christopherfd179292009-08-27 18:07:15 +00007215
Evan Cheng1606e8e2009-03-13 07:51:59 +00007216 SDValue Result = DAG.getTargetConstantPool(CP->getConstVal(), getPointerTy(),
Chris Lattner41621a22009-06-26 19:22:52 +00007217 CP->getAlignment(),
7218 CP->getOffset(), OpFlag);
7219 DebugLoc DL = CP->getDebugLoc();
Chris Lattner18c59872009-06-27 04:16:01 +00007220 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00007221 // With PIC, the address is actually $g + Offset.
Chris Lattner41621a22009-06-26 19:22:52 +00007222 if (OpFlag) {
7223 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesenb300d2a2009-02-07 00:55:49 +00007224 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00007225 DebugLoc(), getPointerTy()),
Anton Korobeynikov7f705592007-01-12 19:20:47 +00007226 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007227 }
7228
7229 return Result;
7230}
7231
Dan Gohmand858e902010-04-17 15:26:15 +00007232SDValue X86TargetLowering::LowerJumpTable(SDValue Op, SelectionDAG &DAG) const {
Chris Lattner18c59872009-06-27 04:16:01 +00007233 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
Eric Christopherfd179292009-08-27 18:07:15 +00007234
Chris Lattner18c59872009-06-27 04:16:01 +00007235 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
7236 // global base reg.
7237 unsigned char OpFlag = 0;
7238 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007239 CodeModel::Model M = getTargetMachine().getCodeModel();
7240
Chris Lattner4f066492009-07-11 20:29:19 +00007241 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007242 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattnere4df7562009-07-09 03:15:51 +00007243 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00007244 else if (Subtarget->isPICStyleGOT())
Chris Lattner88e1fd52009-07-09 04:24:46 +00007245 OpFlag = X86II::MO_GOTOFF;
Chris Lattnere2c92082009-07-10 21:00:45 +00007246 else if (Subtarget->isPICStyleStubPIC())
Chris Lattner88e1fd52009-07-09 04:24:46 +00007247 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Eric Christopherfd179292009-08-27 18:07:15 +00007248
Chris Lattner18c59872009-06-27 04:16:01 +00007249 SDValue Result = DAG.getTargetJumpTable(JT->getIndex(), getPointerTy(),
7250 OpFlag);
7251 DebugLoc DL = JT->getDebugLoc();
7252 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Eric Christopherfd179292009-08-27 18:07:15 +00007253
Chris Lattner18c59872009-06-27 04:16:01 +00007254 // With PIC, the address is actually $g + Offset.
Chris Lattner1e61e692010-11-15 02:46:57 +00007255 if (OpFlag)
Chris Lattner18c59872009-06-27 04:16:01 +00007256 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
7257 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00007258 DebugLoc(), getPointerTy()),
Chris Lattner18c59872009-06-27 04:16:01 +00007259 Result);
Eric Christopherfd179292009-08-27 18:07:15 +00007260
Chris Lattner18c59872009-06-27 04:16:01 +00007261 return Result;
7262}
7263
7264SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007265X86TargetLowering::LowerExternalSymbol(SDValue Op, SelectionDAG &DAG) const {
Chris Lattner18c59872009-06-27 04:16:01 +00007266 const char *Sym = cast<ExternalSymbolSDNode>(Op)->getSymbol();
Eric Christopherfd179292009-08-27 18:07:15 +00007267
Chris Lattner18c59872009-06-27 04:16:01 +00007268 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
7269 // global base reg.
7270 unsigned char OpFlag = 0;
7271 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007272 CodeModel::Model M = getTargetMachine().getCodeModel();
7273
Chris Lattner4f066492009-07-11 20:29:19 +00007274 if (Subtarget->isPICStyleRIPRel() &&
Eli Friedman586272d2011-08-11 01:48:05 +00007275 (M == CodeModel::Small || M == CodeModel::Kernel)) {
7276 if (Subtarget->isTargetDarwin() || Subtarget->isTargetELF())
7277 OpFlag = X86II::MO_GOTPCREL;
Chris Lattnere4df7562009-07-09 03:15:51 +00007278 WrapperKind = X86ISD::WrapperRIP;
Eli Friedman586272d2011-08-11 01:48:05 +00007279 } else if (Subtarget->isPICStyleGOT()) {
7280 OpFlag = X86II::MO_GOT;
7281 } else if (Subtarget->isPICStyleStubPIC()) {
7282 OpFlag = X86II::MO_DARWIN_NONLAZY_PIC_BASE;
7283 } else if (Subtarget->isPICStyleStubNoDynamic()) {
7284 OpFlag = X86II::MO_DARWIN_NONLAZY;
7285 }
Eric Christopherfd179292009-08-27 18:07:15 +00007286
Chris Lattner18c59872009-06-27 04:16:01 +00007287 SDValue Result = DAG.getTargetExternalSymbol(Sym, getPointerTy(), OpFlag);
Eric Christopherfd179292009-08-27 18:07:15 +00007288
Chris Lattner18c59872009-06-27 04:16:01 +00007289 DebugLoc DL = Op.getDebugLoc();
7290 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Eric Christopherfd179292009-08-27 18:07:15 +00007291
7292
Chris Lattner18c59872009-06-27 04:16:01 +00007293 // With PIC, the address is actually $g + Offset.
7294 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
Chris Lattnere4df7562009-07-09 03:15:51 +00007295 !Subtarget->is64Bit()) {
Chris Lattner18c59872009-06-27 04:16:01 +00007296 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
7297 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00007298 DebugLoc(), getPointerTy()),
Chris Lattner18c59872009-06-27 04:16:01 +00007299 Result);
7300 }
Eric Christopherfd179292009-08-27 18:07:15 +00007301
Eli Friedman586272d2011-08-11 01:48:05 +00007302 // For symbols that require a load from a stub to get the address, emit the
7303 // load.
7304 if (isGlobalStubReference(OpFlag))
7305 Result = DAG.getLoad(getPointerTy(), DL, DAG.getEntryNode(), Result,
Pete Cooperd752e0f2011-11-08 18:42:53 +00007306 MachinePointerInfo::getGOT(), false, false, false, 0);
Eli Friedman586272d2011-08-11 01:48:05 +00007307
Chris Lattner18c59872009-06-27 04:16:01 +00007308 return Result;
7309}
7310
Dan Gohman475871a2008-07-27 21:46:04 +00007311SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007312X86TargetLowering::LowerBlockAddress(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman29cbade2009-11-20 23:18:13 +00007313 // Create the TargetBlockAddressAddress node.
7314 unsigned char OpFlags =
7315 Subtarget->ClassifyBlockAddressReference();
Dan Gohmanf705adb2009-10-30 01:28:02 +00007316 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman46510a72010-04-15 01:51:59 +00007317 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
Michael Liao6c7ccaa2012-09-12 21:43:09 +00007318 int64_t Offset = cast<BlockAddressSDNode>(Op)->getOffset();
Dan Gohman29cbade2009-11-20 23:18:13 +00007319 DebugLoc dl = Op.getDebugLoc();
Michael Liao6c7ccaa2012-09-12 21:43:09 +00007320 SDValue Result = DAG.getTargetBlockAddress(BA, getPointerTy(), Offset,
7321 OpFlags);
Dan Gohman29cbade2009-11-20 23:18:13 +00007322
Dan Gohmanf705adb2009-10-30 01:28:02 +00007323 if (Subtarget->isPICStyleRIPRel() &&
7324 (M == CodeModel::Small || M == CodeModel::Kernel))
Dan Gohman29cbade2009-11-20 23:18:13 +00007325 Result = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Result);
7326 else
7327 Result = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), Result);
Dan Gohmanf705adb2009-10-30 01:28:02 +00007328
Dan Gohman29cbade2009-11-20 23:18:13 +00007329 // With PIC, the address is actually $g + Offset.
7330 if (isGlobalRelativeToPICBase(OpFlags)) {
7331 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(),
7332 DAG.getNode(X86ISD::GlobalBaseReg, dl, getPointerTy()),
7333 Result);
7334 }
Dan Gohmanf705adb2009-10-30 01:28:02 +00007335
7336 return Result;
7337}
7338
7339SDValue
Dale Johannesen33c960f2009-02-04 20:06:27 +00007340X86TargetLowering::LowerGlobalAddress(const GlobalValue *GV, DebugLoc dl,
Dan Gohman6520e202008-10-18 02:06:02 +00007341 int64_t Offset,
Evan Chengda43bcf2008-09-24 00:05:32 +00007342 SelectionDAG &DAG) const {
Dan Gohman6520e202008-10-18 02:06:02 +00007343 // Create the TargetGlobalAddress node, folding in the constant
7344 // offset if it is legal.
Chris Lattnerd392bd92009-07-10 07:20:05 +00007345 unsigned char OpFlags =
7346 Subtarget->ClassifyGlobalReference(GV, getTargetMachine());
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007347 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman6520e202008-10-18 02:06:02 +00007348 SDValue Result;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007349 if (OpFlags == X86II::MO_NO_FLAG &&
7350 X86::isOffsetSuitableForCodeModel(Offset, M)) {
Chris Lattner4aa21aa2009-07-09 00:58:53 +00007351 // A direct static reference to a global.
Devang Patel0d881da2010-07-06 22:08:15 +00007352 Result = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(), Offset);
Dan Gohman6520e202008-10-18 02:06:02 +00007353 Offset = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00007354 } else {
Devang Patel0d881da2010-07-06 22:08:15 +00007355 Result = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(), 0, OpFlags);
Chris Lattner18c59872009-06-27 04:16:01 +00007356 }
Eric Christopherfd179292009-08-27 18:07:15 +00007357
Chris Lattner4f066492009-07-11 20:29:19 +00007358 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007359 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattner18c59872009-06-27 04:16:01 +00007360 Result = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Result);
7361 else
7362 Result = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), Result);
Dan Gohman6520e202008-10-18 02:06:02 +00007363
Anton Korobeynikov7f705592007-01-12 19:20:47 +00007364 // With PIC, the address is actually $g + Offset.
Chris Lattner36c25012009-07-10 07:34:39 +00007365 if (isGlobalRelativeToPICBase(OpFlags)) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00007366 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(),
7367 DAG.getNode(X86ISD::GlobalBaseReg, dl, getPointerTy()),
Anton Korobeynikov7f705592007-01-12 19:20:47 +00007368 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007369 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007370
Chris Lattner36c25012009-07-10 07:34:39 +00007371 // For globals that require a load from a stub to get the address, emit the
7372 // load.
7373 if (isGlobalStubReference(OpFlags))
Dale Johannesen33c960f2009-02-04 20:06:27 +00007374 Result = DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(), Result,
Pete Cooperd752e0f2011-11-08 18:42:53 +00007375 MachinePointerInfo::getGOT(), false, false, false, 0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007376
Dan Gohman6520e202008-10-18 02:06:02 +00007377 // If there was a non-zero offset that we didn't fold, create an explicit
7378 // addition for it.
7379 if (Offset != 0)
Dale Johannesen33c960f2009-02-04 20:06:27 +00007380 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(), Result,
Dan Gohman6520e202008-10-18 02:06:02 +00007381 DAG.getConstant(Offset, getPointerTy()));
7382
Evan Cheng0db9fe62006-04-25 20:13:52 +00007383 return Result;
7384}
7385
Evan Chengda43bcf2008-09-24 00:05:32 +00007386SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007387X86TargetLowering::LowerGlobalAddress(SDValue Op, SelectionDAG &DAG) const {
Evan Chengda43bcf2008-09-24 00:05:32 +00007388 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Dan Gohman6520e202008-10-18 02:06:02 +00007389 int64_t Offset = cast<GlobalAddressSDNode>(Op)->getOffset();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007390 return LowerGlobalAddress(GV, Op.getDebugLoc(), Offset, DAG);
Evan Chengda43bcf2008-09-24 00:05:32 +00007391}
7392
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007393static SDValue
7394GetTLSADDR(SelectionDAG &DAG, SDValue Chain, GlobalAddressSDNode *GA,
Owen Andersone50ed302009-08-10 22:56:29 +00007395 SDValue *InFlag, const EVT PtrVT, unsigned ReturnReg,
Hans Wennborgf0234fc2012-06-01 16:27:21 +00007396 unsigned char OperandFlags, bool LocalDynamic = false) {
Anton Korobeynikov817a4642009-12-11 19:39:55 +00007397 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00007398 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007399 DebugLoc dl = GA->getDebugLoc();
Devang Patel0d881da2010-07-06 22:08:15 +00007400 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl,
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007401 GA->getValueType(0),
Chris Lattnerb903bed2009-06-26 21:20:29 +00007402 GA->getOffset(),
7403 OperandFlags);
Hans Wennborgf0234fc2012-06-01 16:27:21 +00007404
7405 X86ISD::NodeType CallType = LocalDynamic ? X86ISD::TLSBASEADDR
7406 : X86ISD::TLSADDR;
7407
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007408 if (InFlag) {
7409 SDValue Ops[] = { Chain, TGA, *InFlag };
Hans Wennborgf0234fc2012-06-01 16:27:21 +00007410 Chain = DAG.getNode(CallType, dl, NodeTys, Ops, 3);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007411 } else {
7412 SDValue Ops[] = { Chain, TGA };
Hans Wennborgf0234fc2012-06-01 16:27:21 +00007413 Chain = DAG.getNode(CallType, dl, NodeTys, Ops, 2);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007414 }
Anton Korobeynikov817a4642009-12-11 19:39:55 +00007415
7416 // TLSADDR will be codegen'ed as call. Inform MFI that function has calls.
Bill Wendlingb92187a2010-05-14 21:14:32 +00007417 MFI->setAdjustsStack(true);
Anton Korobeynikov817a4642009-12-11 19:39:55 +00007418
Rafael Espindola15f1b662009-04-24 12:59:40 +00007419 SDValue Flag = Chain.getValue(1);
7420 return DAG.getCopyFromReg(Chain, dl, ReturnReg, PtrVT, Flag);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007421}
7422
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007423// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 32 bit
Dan Gohman475871a2008-07-27 21:46:04 +00007424static SDValue
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007425LowerToTLSGeneralDynamicModel32(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00007426 const EVT PtrVT) {
Dan Gohman475871a2008-07-27 21:46:04 +00007427 SDValue InFlag;
Dale Johannesendd64c412009-02-04 00:33:20 +00007428 DebugLoc dl = GA->getDebugLoc(); // ? function entry point might be better
7429 SDValue Chain = DAG.getCopyToReg(DAG.getEntryNode(), dl, X86::EBX,
Craig Topper7c022842012-09-12 06:20:41 +00007430 DAG.getNode(X86ISD::GlobalBaseReg,
7431 DebugLoc(), PtrVT), InFlag);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007432 InFlag = Chain.getValue(1);
7433
Chris Lattnerb903bed2009-06-26 21:20:29 +00007434 return GetTLSADDR(DAG, Chain, GA, &InFlag, PtrVT, X86::EAX, X86II::MO_TLSGD);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007435}
7436
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007437// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 64 bit
Dan Gohman475871a2008-07-27 21:46:04 +00007438static SDValue
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007439LowerToTLSGeneralDynamicModel64(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00007440 const EVT PtrVT) {
Chris Lattnerb903bed2009-06-26 21:20:29 +00007441 return GetTLSADDR(DAG, DAG.getEntryNode(), GA, NULL, PtrVT,
7442 X86::RAX, X86II::MO_TLSGD);
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007443}
7444
Hans Wennborgf0234fc2012-06-01 16:27:21 +00007445static SDValue LowerToTLSLocalDynamicModel(GlobalAddressSDNode *GA,
7446 SelectionDAG &DAG,
7447 const EVT PtrVT,
7448 bool is64Bit) {
7449 DebugLoc dl = GA->getDebugLoc();
7450
7451 // Get the start address of the TLS block for this module.
7452 X86MachineFunctionInfo* MFI = DAG.getMachineFunction()
7453 .getInfo<X86MachineFunctionInfo>();
7454 MFI->incNumLocalDynamicTLSAccesses();
7455
7456 SDValue Base;
7457 if (is64Bit) {
7458 Base = GetTLSADDR(DAG, DAG.getEntryNode(), GA, NULL, PtrVT, X86::RAX,
7459 X86II::MO_TLSLD, /*LocalDynamic=*/true);
7460 } else {
7461 SDValue InFlag;
7462 SDValue Chain = DAG.getCopyToReg(DAG.getEntryNode(), dl, X86::EBX,
7463 DAG.getNode(X86ISD::GlobalBaseReg, DebugLoc(), PtrVT), InFlag);
7464 InFlag = Chain.getValue(1);
7465 Base = GetTLSADDR(DAG, Chain, GA, &InFlag, PtrVT, X86::EAX,
7466 X86II::MO_TLSLDM, /*LocalDynamic=*/true);
7467 }
7468
7469 // Note: the CleanupLocalDynamicTLSPass will remove redundant computations
7470 // of Base.
7471
7472 // Build x@dtpoff.
7473 unsigned char OperandFlags = X86II::MO_DTPOFF;
7474 unsigned WrapperKind = X86ISD::Wrapper;
7475 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl,
7476 GA->getValueType(0),
7477 GA->getOffset(), OperandFlags);
7478 SDValue Offset = DAG.getNode(WrapperKind, dl, PtrVT, TGA);
7479
7480 // Add x@dtpoff with the base.
7481 return DAG.getNode(ISD::ADD, dl, PtrVT, Offset, Base);
7482}
7483
Hans Wennborg228756c2012-05-11 10:11:01 +00007484// Lower ISD::GlobalTLSAddress using the "initial exec" or "local exec" model.
Dan Gohman475871a2008-07-27 21:46:04 +00007485static SDValue LowerToTLSExecModel(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00007486 const EVT PtrVT, TLSModel::Model model,
Hans Wennborg228756c2012-05-11 10:11:01 +00007487 bool is64Bit, bool isPIC) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00007488 DebugLoc dl = GA->getDebugLoc();
Michael J. Spencerec38de22010-10-10 22:04:20 +00007489
Chris Lattnerf93b90c2010-09-22 04:39:11 +00007490 // Get the Thread Pointer, which is %gs:0 (32-bit) or %fs:0 (64-bit).
7491 Value *Ptr = Constant::getNullValue(Type::getInt8PtrTy(*DAG.getContext(),
7492 is64Bit ? 257 : 256));
Rafael Espindola094fad32009-04-08 21:14:34 +00007493
Michael J. Spencerec38de22010-10-10 22:04:20 +00007494 SDValue ThreadPointer = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(),
Chris Lattnerf93b90c2010-09-22 04:39:11 +00007495 DAG.getIntPtrConstant(0),
Pete Cooperd752e0f2011-11-08 18:42:53 +00007496 MachinePointerInfo(Ptr),
7497 false, false, false, 0);
Rafael Espindola094fad32009-04-08 21:14:34 +00007498
Chris Lattnerb903bed2009-06-26 21:20:29 +00007499 unsigned char OperandFlags = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00007500 // Most TLS accesses are not RIP relative, even on x86-64. One exception is
7501 // initialexec.
7502 unsigned WrapperKind = X86ISD::Wrapper;
7503 if (model == TLSModel::LocalExec) {
Chris Lattnerb903bed2009-06-26 21:20:29 +00007504 OperandFlags = is64Bit ? X86II::MO_TPOFF : X86II::MO_NTPOFF;
Hans Wennborg228756c2012-05-11 10:11:01 +00007505 } else if (model == TLSModel::InitialExec) {
7506 if (is64Bit) {
7507 OperandFlags = X86II::MO_GOTTPOFF;
7508 WrapperKind = X86ISD::WrapperRIP;
7509 } else {
7510 OperandFlags = isPIC ? X86II::MO_GOTNTPOFF : X86II::MO_INDNTPOFF;
7511 }
Chris Lattner18c59872009-06-27 04:16:01 +00007512 } else {
Hans Wennborg228756c2012-05-11 10:11:01 +00007513 llvm_unreachable("Unexpected model");
Chris Lattnerb903bed2009-06-26 21:20:29 +00007514 }
Eric Christopherfd179292009-08-27 18:07:15 +00007515
Hans Wennborg228756c2012-05-11 10:11:01 +00007516 // emit "addl x@ntpoff,%eax" (local exec)
7517 // or "addl x@indntpoff,%eax" (initial exec)
7518 // or "addl x@gotntpoff(%ebx) ,%eax" (initial exec, 32-bit pic)
Michael J. Spencerec38de22010-10-10 22:04:20 +00007519 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl,
Devang Patel0d881da2010-07-06 22:08:15 +00007520 GA->getValueType(0),
Chris Lattnerb903bed2009-06-26 21:20:29 +00007521 GA->getOffset(), OperandFlags);
Chris Lattner18c59872009-06-27 04:16:01 +00007522 SDValue Offset = DAG.getNode(WrapperKind, dl, PtrVT, TGA);
Lauro Ramos Venancio7d2cc2b2007-04-22 22:50:52 +00007523
Hans Wennborg228756c2012-05-11 10:11:01 +00007524 if (model == TLSModel::InitialExec) {
7525 if (isPIC && !is64Bit) {
7526 Offset = DAG.getNode(ISD::ADD, dl, PtrVT,
7527 DAG.getNode(X86ISD::GlobalBaseReg, DebugLoc(), PtrVT),
7528 Offset);
Hans Wennborg228756c2012-05-11 10:11:01 +00007529 }
Rafael Espindola94e3b382012-06-29 04:22:35 +00007530
7531 Offset = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Offset,
7532 MachinePointerInfo::getGOT(), false, false, false,
7533 0);
Hans Wennborg228756c2012-05-11 10:11:01 +00007534 }
Lauro Ramos Venancio7d2cc2b2007-04-22 22:50:52 +00007535
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007536 // The address of the thread local variable is the add of the thread
7537 // pointer with the offset of the variable.
Dale Johannesen33c960f2009-02-04 20:06:27 +00007538 return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007539}
7540
Dan Gohman475871a2008-07-27 21:46:04 +00007541SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007542X86TargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const {
Michael J. Spencerec38de22010-10-10 22:04:20 +00007543
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007544 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
Chris Lattnerb903bed2009-06-26 21:20:29 +00007545 const GlobalValue *GV = GA->getGlobal();
Eric Christopherfd179292009-08-27 18:07:15 +00007546
Eric Christopher30ef0e52010-06-03 04:07:48 +00007547 if (Subtarget->isTargetELF()) {
Chandler Carruth34797132012-04-08 17:20:55 +00007548 TLSModel::Model model = getTargetMachine().getTLSModel(GV);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007549
Eric Christopher30ef0e52010-06-03 04:07:48 +00007550 switch (model) {
7551 case TLSModel::GeneralDynamic:
Eric Christopher30ef0e52010-06-03 04:07:48 +00007552 if (Subtarget->is64Bit())
7553 return LowerToTLSGeneralDynamicModel64(GA, DAG, getPointerTy());
7554 return LowerToTLSGeneralDynamicModel32(GA, DAG, getPointerTy());
Hans Wennborgf0234fc2012-06-01 16:27:21 +00007555 case TLSModel::LocalDynamic:
7556 return LowerToTLSLocalDynamicModel(GA, DAG, getPointerTy(),
7557 Subtarget->is64Bit());
Eric Christopher30ef0e52010-06-03 04:07:48 +00007558 case TLSModel::InitialExec:
7559 case TLSModel::LocalExec:
7560 return LowerToTLSExecModel(GA, DAG, getPointerTy(), model,
Hans Wennborg228756c2012-05-11 10:11:01 +00007561 Subtarget->is64Bit(),
7562 getTargetMachine().getRelocationModel() == Reloc::PIC_);
Eric Christopher30ef0e52010-06-03 04:07:48 +00007563 }
Craig Toppere8eb1162012-04-23 03:26:18 +00007564 llvm_unreachable("Unknown TLS model.");
7565 }
7566
7567 if (Subtarget->isTargetDarwin()) {
Eric Christopher30ef0e52010-06-03 04:07:48 +00007568 // Darwin only has one model of TLS. Lower to that.
7569 unsigned char OpFlag = 0;
7570 unsigned WrapperKind = Subtarget->isPICStyleRIPRel() ?
7571 X86ISD::WrapperRIP : X86ISD::Wrapper;
Michael J. Spencerec38de22010-10-10 22:04:20 +00007572
Eric Christopher30ef0e52010-06-03 04:07:48 +00007573 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
7574 // global base reg.
7575 bool PIC32 = (getTargetMachine().getRelocationModel() == Reloc::PIC_) &&
7576 !Subtarget->is64Bit();
7577 if (PIC32)
7578 OpFlag = X86II::MO_TLVP_PIC_BASE;
7579 else
7580 OpFlag = X86II::MO_TLVP;
Michael J. Spencerec38de22010-10-10 22:04:20 +00007581 DebugLoc DL = Op.getDebugLoc();
Devang Patel0d881da2010-07-06 22:08:15 +00007582 SDValue Result = DAG.getTargetGlobalAddress(GA->getGlobal(), DL,
Eric Christopherd8c05362010-12-09 06:25:53 +00007583 GA->getValueType(0),
Eric Christopher30ef0e52010-06-03 04:07:48 +00007584 GA->getOffset(), OpFlag);
Eric Christopher30ef0e52010-06-03 04:07:48 +00007585 SDValue Offset = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007586
Eric Christopher30ef0e52010-06-03 04:07:48 +00007587 // With PIC32, the address is actually $g + Offset.
7588 if (PIC32)
7589 Offset = DAG.getNode(ISD::ADD, DL, getPointerTy(),
7590 DAG.getNode(X86ISD::GlobalBaseReg,
7591 DebugLoc(), getPointerTy()),
7592 Offset);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007593
Eric Christopher30ef0e52010-06-03 04:07:48 +00007594 // Lowering the machine isd will make sure everything is in the right
7595 // location.
Eric Christopherd8c05362010-12-09 06:25:53 +00007596 SDValue Chain = DAG.getEntryNode();
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00007597 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Eric Christopherd8c05362010-12-09 06:25:53 +00007598 SDValue Args[] = { Chain, Offset };
7599 Chain = DAG.getNode(X86ISD::TLSCALL, DL, NodeTys, Args, 2);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007600
Eric Christopher30ef0e52010-06-03 04:07:48 +00007601 // TLSCALL will be codegen'ed as call. Inform MFI that function has calls.
7602 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
7603 MFI->setAdjustsStack(true);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00007604
Eric Christopher30ef0e52010-06-03 04:07:48 +00007605 // And our return value (tls address) is in the standard call return value
7606 // location.
Eric Christopherd8c05362010-12-09 06:25:53 +00007607 unsigned Reg = Subtarget->is64Bit() ? X86::RAX : X86::EAX;
Evan Chengfd230df2011-10-19 22:22:54 +00007608 return DAG.getCopyFromReg(Chain, DL, Reg, getPointerTy(),
7609 Chain.getValue(1));
Craig Toppere8eb1162012-04-23 03:26:18 +00007610 }
7611
7612 if (Subtarget->isTargetWindows()) {
Anton Korobeynikovd4a19b62012-02-11 17:26:53 +00007613 // Just use the implicit TLS architecture
7614 // Need to generate someting similar to:
7615 // mov rdx, qword [gs:abs 58H]; Load pointer to ThreadLocalStorage
7616 // ; from TEB
7617 // mov ecx, dword [rel _tls_index]: Load index (from C runtime)
7618 // mov rcx, qword [rdx+rcx*8]
7619 // mov eax, .tls$:tlsvar
7620 // [rax+rcx] contains the address
7621 // Windows 64bit: gs:0x58
7622 // Windows 32bit: fs:__tls_array
7623
7624 // If GV is an alias then use the aliasee for determining
7625 // thread-localness.
7626 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV))
7627 GV = GA->resolveAliasedGlobal(false);
7628 DebugLoc dl = GA->getDebugLoc();
7629 SDValue Chain = DAG.getEntryNode();
7630
7631 // Get the Thread Pointer, which is %fs:__tls_array (32-bit) or
7632 // %gs:0x58 (64-bit).
7633 Value *Ptr = Constant::getNullValue(Subtarget->is64Bit()
7634 ? Type::getInt8PtrTy(*DAG.getContext(),
7635 256)
7636 : Type::getInt32PtrTy(*DAG.getContext(),
7637 257));
7638
7639 SDValue ThreadPointer = DAG.getLoad(getPointerTy(), dl, Chain,
7640 Subtarget->is64Bit()
7641 ? DAG.getIntPtrConstant(0x58)
7642 : DAG.getExternalSymbol("_tls_array",
7643 getPointerTy()),
7644 MachinePointerInfo(Ptr),
7645 false, false, false, 0);
7646
7647 // Load the _tls_index variable
7648 SDValue IDX = DAG.getExternalSymbol("_tls_index", getPointerTy());
7649 if (Subtarget->is64Bit())
7650 IDX = DAG.getExtLoad(ISD::ZEXTLOAD, dl, getPointerTy(), Chain,
7651 IDX, MachinePointerInfo(), MVT::i32,
7652 false, false, 0);
7653 else
7654 IDX = DAG.getLoad(getPointerTy(), dl, Chain, IDX, MachinePointerInfo(),
7655 false, false, false, 0);
7656
Micah Villmow2c39b152012-10-15 16:24:29 +00007657 SDValue Scale = DAG.getConstant(Log2_64_Ceil(TD->getPointerSize(0)),
Craig Topper0fbf3642012-04-23 03:28:34 +00007658 getPointerTy());
Anton Korobeynikovd4a19b62012-02-11 17:26:53 +00007659 IDX = DAG.getNode(ISD::SHL, dl, getPointerTy(), IDX, Scale);
7660
7661 SDValue res = DAG.getNode(ISD::ADD, dl, getPointerTy(), ThreadPointer, IDX);
7662 res = DAG.getLoad(getPointerTy(), dl, Chain, res, MachinePointerInfo(),
7663 false, false, false, 0);
7664
7665 // Get the offset of start of .tls section
7666 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl,
7667 GA->getValueType(0),
7668 GA->getOffset(), X86II::MO_SECREL);
7669 SDValue Offset = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), TGA);
7670
7671 // The address of the thread local variable is the add of the thread
7672 // pointer with the offset of the variable.
7673 return DAG.getNode(ISD::ADD, dl, getPointerTy(), res, Offset);
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007674 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00007675
David Blaikie4d6ccb52012-01-20 21:51:11 +00007676 llvm_unreachable("TLS not implemented for this target.");
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007677}
7678
Evan Cheng0db9fe62006-04-25 20:13:52 +00007679
Chad Rosierb90d2a92012-01-03 23:19:12 +00007680/// LowerShiftParts - Lower SRA_PARTS and friends, which return two i32 values
7681/// and take a 2 x i32 value to shift plus a shift amount.
7682SDValue X86TargetLowering::LowerShiftParts(SDValue Op, SelectionDAG &DAG) const{
Dan Gohman4c1fa612008-03-03 22:22:09 +00007683 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
Owen Andersone50ed302009-08-10 22:56:29 +00007684 EVT VT = Op.getValueType();
Duncan Sands83ec4b62008-06-06 12:08:01 +00007685 unsigned VTBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007686 DebugLoc dl = Op.getDebugLoc();
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007687 bool isSRA = Op.getOpcode() == ISD::SRA_PARTS;
Dan Gohman475871a2008-07-27 21:46:04 +00007688 SDValue ShOpLo = Op.getOperand(0);
7689 SDValue ShOpHi = Op.getOperand(1);
7690 SDValue ShAmt = Op.getOperand(2);
Chris Lattner31dcfe62009-07-29 05:48:09 +00007691 SDValue Tmp1 = isSRA ? DAG.getNode(ISD::SRA, dl, VT, ShOpHi,
Owen Anderson825b72b2009-08-11 20:47:22 +00007692 DAG.getConstant(VTBits - 1, MVT::i8))
Chris Lattner31dcfe62009-07-29 05:48:09 +00007693 : DAG.getConstant(0, VT);
Evan Chenge3413162006-01-09 18:33:28 +00007694
Dan Gohman475871a2008-07-27 21:46:04 +00007695 SDValue Tmp2, Tmp3;
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007696 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dale Johannesenace16102009-02-03 19:33:06 +00007697 Tmp2 = DAG.getNode(X86ISD::SHLD, dl, VT, ShOpHi, ShOpLo, ShAmt);
7698 Tmp3 = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ShAmt);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007699 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00007700 Tmp2 = DAG.getNode(X86ISD::SHRD, dl, VT, ShOpLo, ShOpHi, ShAmt);
7701 Tmp3 = DAG.getNode(isSRA ? ISD::SRA : ISD::SRL, dl, VT, ShOpHi, ShAmt);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007702 }
Evan Chenge3413162006-01-09 18:33:28 +00007703
Owen Anderson825b72b2009-08-11 20:47:22 +00007704 SDValue AndNode = DAG.getNode(ISD::AND, dl, MVT::i8, ShAmt,
7705 DAG.getConstant(VTBits, MVT::i8));
Chris Lattnerccfea352010-02-22 00:28:59 +00007706 SDValue Cond = DAG.getNode(X86ISD::CMP, dl, MVT::i32,
Owen Anderson825b72b2009-08-11 20:47:22 +00007707 AndNode, DAG.getConstant(0, MVT::i8));
Evan Chenge3413162006-01-09 18:33:28 +00007708
Dan Gohman475871a2008-07-27 21:46:04 +00007709 SDValue Hi, Lo;
Owen Anderson825b72b2009-08-11 20:47:22 +00007710 SDValue CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Dan Gohman475871a2008-07-27 21:46:04 +00007711 SDValue Ops0[4] = { Tmp2, Tmp3, CC, Cond };
7712 SDValue Ops1[4] = { Tmp3, Tmp1, CC, Cond };
Duncan Sandsf9516202008-06-30 10:19:09 +00007713
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007714 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dale Johannesenace16102009-02-03 19:33:06 +00007715 Hi = DAG.getNode(X86ISD::CMOV, dl, VT, Ops0, 4);
7716 Lo = DAG.getNode(X86ISD::CMOV, dl, VT, Ops1, 4);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007717 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00007718 Lo = DAG.getNode(X86ISD::CMOV, dl, VT, Ops0, 4);
7719 Hi = DAG.getNode(X86ISD::CMOV, dl, VT, Ops1, 4);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007720 }
7721
Dan Gohman475871a2008-07-27 21:46:04 +00007722 SDValue Ops[2] = { Lo, Hi };
Dale Johannesenace16102009-02-03 19:33:06 +00007723 return DAG.getMergeValues(Ops, 2, dl);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007724}
Evan Chenga3195e82006-01-12 22:54:21 +00007725
Dan Gohmand858e902010-04-17 15:26:15 +00007726SDValue X86TargetLowering::LowerSINT_TO_FP(SDValue Op,
7727 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00007728 EVT SrcVT = Op.getOperand(0).getValueType();
Eli Friedman23ef1052009-06-06 03:57:58 +00007729
Dale Johannesen0488fb62010-09-30 23:57:10 +00007730 if (SrcVT.isVector())
Eli Friedman23ef1052009-06-06 03:57:58 +00007731 return SDValue();
Eli Friedman23ef1052009-06-06 03:57:58 +00007732
Owen Anderson825b72b2009-08-11 20:47:22 +00007733 assert(SrcVT.getSimpleVT() <= MVT::i64 && SrcVT.getSimpleVT() >= MVT::i16 &&
Chris Lattnerb09916b2008-02-27 05:57:41 +00007734 "Unknown SINT_TO_FP to lower!");
Scott Michelfdc40a02009-02-17 22:15:04 +00007735
Eli Friedman36df4992009-05-27 00:47:34 +00007736 // These are really Legal; return the operand so the caller accepts it as
7737 // Legal.
Owen Anderson825b72b2009-08-11 20:47:22 +00007738 if (SrcVT == MVT::i32 && isScalarFPTypeInSSEReg(Op.getValueType()))
Eli Friedman36df4992009-05-27 00:47:34 +00007739 return Op;
Owen Anderson825b72b2009-08-11 20:47:22 +00007740 if (SrcVT == MVT::i64 && isScalarFPTypeInSSEReg(Op.getValueType()) &&
Eli Friedman36df4992009-05-27 00:47:34 +00007741 Subtarget->is64Bit()) {
7742 return Op;
7743 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007744
Bruno Cardoso Lopesa511b8e2011-08-09 17:39:01 +00007745 DebugLoc dl = Op.getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +00007746 unsigned Size = SrcVT.getSizeInBits()/8;
Evan Cheng0db9fe62006-04-25 20:13:52 +00007747 MachineFunction &MF = DAG.getMachineFunction();
David Greene3f2bf852009-11-12 20:49:22 +00007748 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size, false);
Dan Gohman475871a2008-07-27 21:46:04 +00007749 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Dale Johannesenace16102009-02-03 19:33:06 +00007750 SDValue Chain = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Bill Wendling105be5a2009-03-13 08:41:47 +00007751 StackSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00007752 MachinePointerInfo::getFixedStack(SSFI),
David Greene67c9d422010-02-15 16:53:33 +00007753 false, false, 0);
Eli Friedman948e95a2009-05-23 09:59:16 +00007754 return BuildFILD(Op, SrcVT, Chain, StackSlot, DAG);
7755}
Evan Cheng0db9fe62006-04-25 20:13:52 +00007756
Owen Andersone50ed302009-08-10 22:56:29 +00007757SDValue X86TargetLowering::BuildFILD(SDValue Op, EVT SrcVT, SDValue Chain,
Michael J. Spencerec38de22010-10-10 22:04:20 +00007758 SDValue StackSlot,
Dan Gohmand858e902010-04-17 15:26:15 +00007759 SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00007760 // Build the FILD
Chris Lattner492a43e2010-09-22 01:28:21 +00007761 DebugLoc DL = Op.getDebugLoc();
Chris Lattner5a88b832007-02-25 07:10:00 +00007762 SDVTList Tys;
Chris Lattner78631162008-01-16 06:24:21 +00007763 bool useSSE = isScalarFPTypeInSSEReg(Op.getValueType());
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00007764 if (useSSE)
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00007765 Tys = DAG.getVTList(MVT::f64, MVT::Other, MVT::Glue);
Chris Lattner5a88b832007-02-25 07:10:00 +00007766 else
Owen Anderson825b72b2009-08-11 20:47:22 +00007767 Tys = DAG.getVTList(Op.getValueType(), MVT::Other);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007768
Chris Lattner492a43e2010-09-22 01:28:21 +00007769 unsigned ByteSize = SrcVT.getSizeInBits()/8;
Michael J. Spencerec38de22010-10-10 22:04:20 +00007770
Stuart Hastings84be9582011-06-02 15:57:11 +00007771 FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(StackSlot);
7772 MachineMemOperand *MMO;
7773 if (FI) {
7774 int SSFI = FI->getIndex();
7775 MMO =
7776 DAG.getMachineFunction()
7777 .getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
7778 MachineMemOperand::MOLoad, ByteSize, ByteSize);
7779 } else {
7780 MMO = cast<LoadSDNode>(StackSlot)->getMemOperand();
7781 StackSlot = StackSlot.getOperand(1);
7782 }
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00007783 SDValue Ops[] = { Chain, StackSlot, DAG.getValueType(SrcVT) };
Chris Lattner492a43e2010-09-22 01:28:21 +00007784 SDValue Result = DAG.getMemIntrinsicNode(useSSE ? X86ISD::FILD_FLAG :
7785 X86ISD::FILD, DL,
7786 Tys, Ops, array_lengthof(Ops),
7787 SrcVT, MMO);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007788
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00007789 if (useSSE) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00007790 Chain = Result.getValue(1);
Dan Gohman475871a2008-07-27 21:46:04 +00007791 SDValue InFlag = Result.getValue(2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007792
7793 // FIXME: Currently the FST is flagged to the FILD_FLAG. This
7794 // shouldn't be necessary except that RFP cannot be live across
7795 // multiple blocks. When stackifier is fixed, they can be uncoupled.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00007796 MachineFunction &MF = DAG.getMachineFunction();
Bob Wilsoneafca4e2010-09-22 17:35:14 +00007797 unsigned SSFISize = Op.getValueType().getSizeInBits()/8;
7798 int SSFI = MF.getFrameInfo()->CreateStackObject(SSFISize, SSFISize, false);
Dan Gohman475871a2008-07-27 21:46:04 +00007799 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Owen Anderson825b72b2009-08-11 20:47:22 +00007800 Tys = DAG.getVTList(MVT::Other);
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00007801 SDValue Ops[] = {
7802 Chain, Result, StackSlot, DAG.getValueType(Op.getValueType()), InFlag
7803 };
Chris Lattner492a43e2010-09-22 01:28:21 +00007804 MachineMemOperand *MMO =
7805 DAG.getMachineFunction()
7806 .getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
Bob Wilsoneafca4e2010-09-22 17:35:14 +00007807 MachineMemOperand::MOStore, SSFISize, SSFISize);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007808
Chris Lattner492a43e2010-09-22 01:28:21 +00007809 Chain = DAG.getMemIntrinsicNode(X86ISD::FST, DL, Tys,
7810 Ops, array_lengthof(Ops),
7811 Op.getValueType(), MMO);
7812 Result = DAG.getLoad(Op.getValueType(), DL, Chain, StackSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00007813 MachinePointerInfo::getFixedStack(SSFI),
Pete Cooperd752e0f2011-11-08 18:42:53 +00007814 false, false, false, 0);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00007815 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00007816
Evan Cheng0db9fe62006-04-25 20:13:52 +00007817 return Result;
7818}
7819
Bill Wendling8b8a6362009-01-17 03:56:04 +00007820// LowerUINT_TO_FP_i64 - 64-bit unsigned integer to double expansion.
Dan Gohmand858e902010-04-17 15:26:15 +00007821SDValue X86TargetLowering::LowerUINT_TO_FP_i64(SDValue Op,
7822 SelectionDAG &DAG) const {
Bill Wendling397ae212012-01-05 02:13:20 +00007823 // This algorithm is not obvious. Here it is what we're trying to output:
Bill Wendling8b8a6362009-01-17 03:56:04 +00007824 /*
Bill Wendling397ae212012-01-05 02:13:20 +00007825 movq %rax, %xmm0
7826 punpckldq (c0), %xmm0 // c0: (uint4){ 0x43300000U, 0x45300000U, 0U, 0U }
7827 subpd (c1), %xmm0 // c1: (double2){ 0x1.0p52, 0x1.0p52 * 0x1.0p32 }
7828 #ifdef __SSE3__
Chad Rosiera20e1e72012-08-01 18:39:17 +00007829 haddpd %xmm0, %xmm0
Bill Wendling397ae212012-01-05 02:13:20 +00007830 #else
Chad Rosiera20e1e72012-08-01 18:39:17 +00007831 pshufd $0x4e, %xmm0, %xmm1
Bill Wendling397ae212012-01-05 02:13:20 +00007832 addpd %xmm1, %xmm0
7833 #endif
Bill Wendling8b8a6362009-01-17 03:56:04 +00007834 */
Dale Johannesen040225f2008-10-21 23:07:49 +00007835
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007836 DebugLoc dl = Op.getDebugLoc();
Owen Andersona90b3dc2009-07-15 21:51:10 +00007837 LLVMContext *Context = DAG.getContext();
Dale Johannesenace16102009-02-03 19:33:06 +00007838
Dale Johannesen1c15bf52008-10-21 20:50:01 +00007839 // Build some magic constants.
Chris Lattner7302d802012-02-06 21:56:39 +00007840 const uint32_t CV0[] = { 0x43300000, 0x45300000, 0, 0 };
7841 Constant *C0 = ConstantDataVector::get(*Context, CV0);
Evan Cheng1606e8e2009-03-13 07:51:59 +00007842 SDValue CPIdx0 = DAG.getConstantPool(C0, getPointerTy(), 16);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00007843
Chris Lattner97484792012-01-25 09:56:22 +00007844 SmallVector<Constant*,2> CV1;
7845 CV1.push_back(
Chris Lattner4ca829e2012-01-25 06:02:56 +00007846 ConstantFP::get(*Context, APFloat(APInt(64, 0x4330000000000000ULL))));
Chris Lattner97484792012-01-25 09:56:22 +00007847 CV1.push_back(
7848 ConstantFP::get(*Context, APFloat(APInt(64, 0x4530000000000000ULL))));
7849 Constant *C1 = ConstantVector::get(CV1);
Evan Cheng1606e8e2009-03-13 07:51:59 +00007850 SDValue CPIdx1 = DAG.getConstantPool(C1, getPointerTy(), 16);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00007851
Bill Wendling397ae212012-01-05 02:13:20 +00007852 // Load the 64-bit value into an XMM register.
7853 SDValue XR1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i64,
7854 Op.getOperand(0));
Owen Anderson825b72b2009-08-11 20:47:22 +00007855 SDValue CLod0 = DAG.getLoad(MVT::v4i32, dl, DAG.getEntryNode(), CPIdx0,
Chris Lattnere8639032010-09-21 06:22:23 +00007856 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00007857 false, false, false, 16);
Bill Wendling397ae212012-01-05 02:13:20 +00007858 SDValue Unpck1 = getUnpackl(DAG, dl, MVT::v4i32,
7859 DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, XR1),
7860 CLod0);
7861
Owen Anderson825b72b2009-08-11 20:47:22 +00007862 SDValue CLod1 = DAG.getLoad(MVT::v2f64, dl, CLod0.getValue(1), CPIdx1,
Chris Lattnere8639032010-09-21 06:22:23 +00007863 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00007864 false, false, false, 16);
Bill Wendling397ae212012-01-05 02:13:20 +00007865 SDValue XR2F = DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Unpck1);
Owen Anderson825b72b2009-08-11 20:47:22 +00007866 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::v2f64, XR2F, CLod1);
Bill Wendling397ae212012-01-05 02:13:20 +00007867 SDValue Result;
Bill Wendling8b8a6362009-01-17 03:56:04 +00007868
Craig Topperd0a31172012-01-10 06:37:29 +00007869 if (Subtarget->hasSSE3()) {
Bill Wendling397ae212012-01-05 02:13:20 +00007870 // FIXME: The 'haddpd' instruction may be slower than 'movhlps + addsd'.
7871 Result = DAG.getNode(X86ISD::FHADD, dl, MVT::v2f64, Sub, Sub);
7872 } else {
7873 SDValue S2F = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, Sub);
7874 SDValue Shuffle = getTargetShuffleNode(X86ISD::PSHUFD, dl, MVT::v4i32,
7875 S2F, 0x4E, DAG);
7876 Result = DAG.getNode(ISD::FADD, dl, MVT::v2f64,
7877 DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Shuffle),
7878 Sub);
7879 }
7880
7881 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Result,
Dale Johannesen1c15bf52008-10-21 20:50:01 +00007882 DAG.getIntPtrConstant(0));
7883}
7884
Bill Wendling8b8a6362009-01-17 03:56:04 +00007885// LowerUINT_TO_FP_i32 - 32-bit unsigned integer to float expansion.
Dan Gohmand858e902010-04-17 15:26:15 +00007886SDValue X86TargetLowering::LowerUINT_TO_FP_i32(SDValue Op,
7887 SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007888 DebugLoc dl = Op.getDebugLoc();
Bill Wendling8b8a6362009-01-17 03:56:04 +00007889 // FP constant to bias correct the final result.
7890 SDValue Bias = DAG.getConstantFP(BitsToDouble(0x4330000000000000ULL),
Owen Anderson825b72b2009-08-11 20:47:22 +00007891 MVT::f64);
Bill Wendling8b8a6362009-01-17 03:56:04 +00007892
7893 // Load the 32-bit value into an XMM register.
Owen Anderson825b72b2009-08-11 20:47:22 +00007894 SDValue Load = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
Eli Friedman6cdc1f42011-08-02 18:38:35 +00007895 Op.getOperand(0));
Bill Wendling8b8a6362009-01-17 03:56:04 +00007896
Eli Friedmanf3704762011-08-29 21:15:46 +00007897 // Zero out the upper parts of the register.
Craig Topper12216172012-01-13 08:12:35 +00007898 Load = getShuffleVectorZeroOrUndef(Load, 0, true, Subtarget, DAG);
Eli Friedmanf3704762011-08-29 21:15:46 +00007899
Owen Anderson825b72b2009-08-11 20:47:22 +00007900 Load = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007901 DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Load),
Bill Wendling8b8a6362009-01-17 03:56:04 +00007902 DAG.getIntPtrConstant(0));
7903
7904 // Or the load with the bias.
Owen Anderson825b72b2009-08-11 20:47:22 +00007905 SDValue Or = DAG.getNode(ISD::OR, dl, MVT::v2i64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007906 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00007907 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00007908 MVT::v2f64, Load)),
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007909 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00007910 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00007911 MVT::v2f64, Bias)));
7912 Or = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007913 DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Or),
Bill Wendling8b8a6362009-01-17 03:56:04 +00007914 DAG.getIntPtrConstant(0));
7915
7916 // Subtract the bias.
Owen Anderson825b72b2009-08-11 20:47:22 +00007917 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::f64, Or, Bias);
Bill Wendling8b8a6362009-01-17 03:56:04 +00007918
7919 // Handle final rounding.
Owen Andersone50ed302009-08-10 22:56:29 +00007920 EVT DestVT = Op.getValueType();
Bill Wendling030939c2009-01-17 07:40:19 +00007921
Craig Topper69947b92012-04-23 06:57:04 +00007922 if (DestVT.bitsLT(MVT::f64))
Dale Johannesenace16102009-02-03 19:33:06 +00007923 return DAG.getNode(ISD::FP_ROUND, dl, DestVT, Sub,
Bill Wendling030939c2009-01-17 07:40:19 +00007924 DAG.getIntPtrConstant(0));
Craig Topper69947b92012-04-23 06:57:04 +00007925 if (DestVT.bitsGT(MVT::f64))
Dale Johannesenace16102009-02-03 19:33:06 +00007926 return DAG.getNode(ISD::FP_EXTEND, dl, DestVT, Sub);
Bill Wendling030939c2009-01-17 07:40:19 +00007927
7928 // Handle final rounding.
7929 return Sub;
Bill Wendling8b8a6362009-01-17 03:56:04 +00007930}
7931
Dan Gohmand858e902010-04-17 15:26:15 +00007932SDValue X86TargetLowering::LowerUINT_TO_FP(SDValue Op,
7933 SelectionDAG &DAG) const {
Evan Chenga06ec9e2009-01-19 08:08:22 +00007934 SDValue N0 = Op.getOperand(0);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007935 DebugLoc dl = Op.getDebugLoc();
Bill Wendling8b8a6362009-01-17 03:56:04 +00007936
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007937 // Since UINT_TO_FP is legal (it's marked custom), dag combiner won't
Evan Chenga06ec9e2009-01-19 08:08:22 +00007938 // optimize it to a SINT_TO_FP when the sign bit is known zero. Perform
7939 // the optimization here.
7940 if (DAG.SignBitIsZero(N0))
Dale Johannesenace16102009-02-03 19:33:06 +00007941 return DAG.getNode(ISD::SINT_TO_FP, dl, Op.getValueType(), N0);
Evan Chenga06ec9e2009-01-19 08:08:22 +00007942
Owen Andersone50ed302009-08-10 22:56:29 +00007943 EVT SrcVT = N0.getValueType();
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007944 EVT DstVT = Op.getValueType();
7945 if (SrcVT == MVT::i64 && DstVT == MVT::f64 && X86ScalarSSEf64)
Bill Wendling8b8a6362009-01-17 03:56:04 +00007946 return LowerUINT_TO_FP_i64(Op, DAG);
Craig Topper69947b92012-04-23 06:57:04 +00007947 if (SrcVT == MVT::i32 && X86ScalarSSEf64)
Bill Wendling8b8a6362009-01-17 03:56:04 +00007948 return LowerUINT_TO_FP_i32(Op, DAG);
Craig Topper69947b92012-04-23 06:57:04 +00007949 if (Subtarget->is64Bit() && SrcVT == MVT::i64 && DstVT == MVT::f32)
Bill Wendling397ae212012-01-05 02:13:20 +00007950 return SDValue();
Eli Friedman948e95a2009-05-23 09:59:16 +00007951
7952 // Make a 64-bit buffer, and use it to build an FILD.
Owen Anderson825b72b2009-08-11 20:47:22 +00007953 SDValue StackSlot = DAG.CreateStackTemporary(MVT::i64);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007954 if (SrcVT == MVT::i32) {
7955 SDValue WordOff = DAG.getConstant(4, getPointerTy());
7956 SDValue OffsetSlot = DAG.getNode(ISD::ADD, dl,
7957 getPointerTy(), StackSlot, WordOff);
7958 SDValue Store1 = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Chris Lattner8026a9d2010-09-21 17:50:43 +00007959 StackSlot, MachinePointerInfo(),
7960 false, false, 0);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007961 SDValue Store2 = DAG.getStore(Store1, dl, DAG.getConstant(0, MVT::i32),
Chris Lattner8026a9d2010-09-21 17:50:43 +00007962 OffsetSlot, MachinePointerInfo(),
7963 false, false, 0);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007964 SDValue Fild = BuildFILD(Op, MVT::i64, Store2, StackSlot, DAG);
7965 return Fild;
7966 }
7967
7968 assert(SrcVT == MVT::i64 && "Unexpected type in UINT_TO_FP");
7969 SDValue Store = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Bill Wendlingf6c07472012-01-10 19:41:30 +00007970 StackSlot, MachinePointerInfo(),
Chris Lattner8026a9d2010-09-21 17:50:43 +00007971 false, false, 0);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007972 // For i64 source, we need to add the appropriate power of 2 if the input
7973 // was negative. This is the same as the optimization in
7974 // DAGTypeLegalizer::ExpandIntOp_UNIT_TO_FP, and for it to be safe here,
7975 // we must be careful to do the computation in x87 extended precision, not
7976 // in SSE. (The generic code can't know it's OK to do this, or how to.)
Chris Lattner492a43e2010-09-22 01:28:21 +00007977 int SSFI = cast<FrameIndexSDNode>(StackSlot)->getIndex();
7978 MachineMemOperand *MMO =
7979 DAG.getMachineFunction()
7980 .getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
7981 MachineMemOperand::MOLoad, 8, 8);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007982
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007983 SDVTList Tys = DAG.getVTList(MVT::f80, MVT::Other);
7984 SDValue Ops[] = { Store, StackSlot, DAG.getValueType(MVT::i64) };
Chris Lattner492a43e2010-09-22 01:28:21 +00007985 SDValue Fild = DAG.getMemIntrinsicNode(X86ISD::FILD, dl, Tys, Ops, 3,
7986 MVT::i64, MMO);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007987
7988 APInt FF(32, 0x5F800000ULL);
7989
7990 // Check whether the sign bit is set.
7991 SDValue SignSet = DAG.getSetCC(dl, getSetCCResultType(MVT::i64),
7992 Op.getOperand(0), DAG.getConstant(0, MVT::i64),
7993 ISD::SETLT);
7994
7995 // Build a 64 bit pair (0, FF) in the constant pool, with FF in the lo bits.
7996 SDValue FudgePtr = DAG.getConstantPool(
7997 ConstantInt::get(*DAG.getContext(), FF.zext(64)),
7998 getPointerTy());
7999
8000 // Get a pointer to FF if the sign bit was set, or to 0 otherwise.
8001 SDValue Zero = DAG.getIntPtrConstant(0);
8002 SDValue Four = DAG.getIntPtrConstant(4);
8003 SDValue Offset = DAG.getNode(ISD::SELECT, dl, Zero.getValueType(), SignSet,
8004 Zero, Four);
8005 FudgePtr = DAG.getNode(ISD::ADD, dl, getPointerTy(), FudgePtr, Offset);
8006
8007 // Load the value out, extending it from f32 to f80.
8008 // FIXME: Avoid the extend by constructing the right constant pool?
Stuart Hastingsa9011292011-02-16 16:23:55 +00008009 SDValue Fudge = DAG.getExtLoad(ISD::EXTLOAD, dl, MVT::f80, DAG.getEntryNode(),
Chris Lattnere8639032010-09-21 06:22:23 +00008010 FudgePtr, MachinePointerInfo::getConstantPool(),
8011 MVT::f32, false, false, 4);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00008012 // Extend everything to 80 bits to force it to be done on x87.
8013 SDValue Add = DAG.getNode(ISD::FADD, dl, MVT::f80, Fild, Fudge);
8014 return DAG.getNode(ISD::FP_ROUND, dl, DstVT, Add, DAG.getIntPtrConstant(0));
Bill Wendling8b8a6362009-01-17 03:56:04 +00008015}
8016
Dan Gohman475871a2008-07-27 21:46:04 +00008017std::pair<SDValue,SDValue> X86TargetLowering::
NAKAMURA Takumi9a68fdc2012-02-25 03:37:25 +00008018FP_TO_INTHelper(SDValue Op, SelectionDAG &DAG, bool IsSigned, bool IsReplace) const {
Chris Lattner07290932010-09-22 01:05:16 +00008019 DebugLoc DL = Op.getDebugLoc();
Eli Friedman948e95a2009-05-23 09:59:16 +00008020
Owen Andersone50ed302009-08-10 22:56:29 +00008021 EVT DstTy = Op.getValueType();
Eli Friedman948e95a2009-05-23 09:59:16 +00008022
Michael J. Spencer1a2d0612012-02-24 19:01:22 +00008023 if (!IsSigned && !isIntegerTypeFTOL(DstTy)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00008024 assert(DstTy == MVT::i32 && "Unexpected FP_TO_UINT");
8025 DstTy = MVT::i64;
Eli Friedman948e95a2009-05-23 09:59:16 +00008026 }
8027
Owen Anderson825b72b2009-08-11 20:47:22 +00008028 assert(DstTy.getSimpleVT() <= MVT::i64 &&
8029 DstTy.getSimpleVT() >= MVT::i16 &&
Michael J. Spencer1a2d0612012-02-24 19:01:22 +00008030 "Unknown FP_TO_INT to lower!");
Evan Cheng0db9fe62006-04-25 20:13:52 +00008031
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00008032 // These are really Legal.
Owen Anderson825b72b2009-08-11 20:47:22 +00008033 if (DstTy == MVT::i32 &&
Chris Lattner78631162008-01-16 06:24:21 +00008034 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Dan Gohman475871a2008-07-27 21:46:04 +00008035 return std::make_pair(SDValue(), SDValue());
Dale Johannesen73328d12007-09-19 23:55:34 +00008036 if (Subtarget->is64Bit() &&
Owen Anderson825b72b2009-08-11 20:47:22 +00008037 DstTy == MVT::i64 &&
Eli Friedman36df4992009-05-27 00:47:34 +00008038 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Dan Gohman475871a2008-07-27 21:46:04 +00008039 return std::make_pair(SDValue(), SDValue());
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00008040
Michael J. Spencer1a2d0612012-02-24 19:01:22 +00008041 // We lower FP->int64 either into FISTP64 followed by a load from a temporary
8042 // stack slot, or into the FTOL runtime function.
Evan Cheng87c89352007-10-15 20:11:21 +00008043 MachineFunction &MF = DAG.getMachineFunction();
Eli Friedman948e95a2009-05-23 09:59:16 +00008044 unsigned MemSize = DstTy.getSizeInBits()/8;
David Greene3f2bf852009-11-12 20:49:22 +00008045 int SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize, false);
Dan Gohman475871a2008-07-27 21:46:04 +00008046 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Eric Christopherfd179292009-08-27 18:07:15 +00008047
Evan Cheng0db9fe62006-04-25 20:13:52 +00008048 unsigned Opc;
Michael J. Spencer1a2d0612012-02-24 19:01:22 +00008049 if (!IsSigned && isIntegerTypeFTOL(DstTy))
8050 Opc = X86ISD::WIN_FTOL;
8051 else
8052 switch (DstTy.getSimpleVT().SimpleTy) {
8053 default: llvm_unreachable("Invalid FP_TO_SINT to lower!");
8054 case MVT::i16: Opc = X86ISD::FP_TO_INT16_IN_MEM; break;
8055 case MVT::i32: Opc = X86ISD::FP_TO_INT32_IN_MEM; break;
8056 case MVT::i64: Opc = X86ISD::FP_TO_INT64_IN_MEM; break;
8057 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00008058
Dan Gohman475871a2008-07-27 21:46:04 +00008059 SDValue Chain = DAG.getEntryNode();
8060 SDValue Value = Op.getOperand(0);
Chris Lattner492a43e2010-09-22 01:28:21 +00008061 EVT TheVT = Op.getOperand(0).getValueType();
Michael J. Spencer1a2d0612012-02-24 19:01:22 +00008062 // FIXME This causes a redundant load/store if the SSE-class value is already
8063 // in memory, such as if it is on the callstack.
Chris Lattner492a43e2010-09-22 01:28:21 +00008064 if (isScalarFPTypeInSSEReg(TheVT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00008065 assert(DstTy == MVT::i64 && "Invalid FP_TO_SINT to lower!");
Chris Lattner07290932010-09-22 01:05:16 +00008066 Chain = DAG.getStore(Chain, DL, Value, StackSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00008067 MachinePointerInfo::getFixedStack(SSFI),
David Greene67c9d422010-02-15 16:53:33 +00008068 false, false, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +00008069 SDVTList Tys = DAG.getVTList(Op.getOperand(0).getValueType(), MVT::Other);
Dan Gohman475871a2008-07-27 21:46:04 +00008070 SDValue Ops[] = {
Chris Lattner492a43e2010-09-22 01:28:21 +00008071 Chain, StackSlot, DAG.getValueType(TheVT)
Chris Lattner5a88b832007-02-25 07:10:00 +00008072 };
Michael J. Spencerec38de22010-10-10 22:04:20 +00008073
Chris Lattner492a43e2010-09-22 01:28:21 +00008074 MachineMemOperand *MMO =
8075 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
8076 MachineMemOperand::MOLoad, MemSize, MemSize);
8077 Value = DAG.getMemIntrinsicNode(X86ISD::FLD, DL, Tys, Ops, 3,
8078 DstTy, MMO);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008079 Chain = Value.getValue(1);
David Greene3f2bf852009-11-12 20:49:22 +00008080 SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize, false);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008081 StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
8082 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00008083
Chris Lattner07290932010-09-22 01:05:16 +00008084 MachineMemOperand *MMO =
8085 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
8086 MachineMemOperand::MOStore, MemSize, MemSize);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00008087
Michael J. Spencer1a2d0612012-02-24 19:01:22 +00008088 if (Opc != X86ISD::WIN_FTOL) {
8089 // Build the FP_TO_INT*_IN_MEM
8090 SDValue Ops[] = { Chain, Value, StackSlot };
8091 SDValue FIST = DAG.getMemIntrinsicNode(Opc, DL, DAG.getVTList(MVT::Other),
8092 Ops, 3, DstTy, MMO);
8093 return std::make_pair(FIST, StackSlot);
8094 } else {
8095 SDValue ftol = DAG.getNode(X86ISD::WIN_FTOL, DL,
8096 DAG.getVTList(MVT::Other, MVT::Glue),
8097 Chain, Value);
8098 SDValue eax = DAG.getCopyFromReg(ftol, DL, X86::EAX,
8099 MVT::i32, ftol.getValue(1));
8100 SDValue edx = DAG.getCopyFromReg(eax.getValue(1), DL, X86::EDX,
8101 MVT::i32, eax.getValue(2));
NAKAMURA Takumi9a68fdc2012-02-25 03:37:25 +00008102 SDValue Ops[] = { eax, edx };
8103 SDValue pair = IsReplace
8104 ? DAG.getNode(ISD::BUILD_PAIR, DL, MVT::i64, Ops, 2)
8105 : DAG.getMergeValues(Ops, 2, DL);
Michael J. Spencer1a2d0612012-02-24 19:01:22 +00008106 return std::make_pair(pair, SDValue());
8107 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00008108}
8109
Michael Liaobedcbd42012-10-16 18:14:11 +00008110SDValue X86TargetLowering::lowerTRUNCATE(SDValue Op, SelectionDAG &DAG) const {
8111 DebugLoc DL = Op.getDebugLoc();
8112 EVT VT = Op.getValueType();
8113 EVT SVT = Op.getOperand(0).getValueType();
8114
8115 if (!VT.is128BitVector() || !SVT.is256BitVector() ||
8116 VT.getVectorNumElements() != SVT.getVectorNumElements())
8117 return SDValue();
8118
8119 assert(Subtarget->hasAVX() && "256-bit vector is observed without AVX!");
8120
8121 unsigned NumElems = VT.getVectorNumElements();
8122 EVT NVT = EVT::getVectorVT(*DAG.getContext(), VT.getVectorElementType(),
8123 NumElems * 2);
8124
8125 SDValue In = Op.getOperand(0);
8126 SmallVector<int, 16> MaskVec(NumElems * 2, -1);
8127 // Prepare truncation shuffle mask
8128 for (unsigned i = 0; i != NumElems; ++i)
8129 MaskVec[i] = i * 2;
8130 SDValue V = DAG.getVectorShuffle(NVT, DL,
8131 DAG.getNode(ISD::BITCAST, DL, NVT, In),
8132 DAG.getUNDEF(NVT), &MaskVec[0]);
8133 return DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, VT, V,
8134 DAG.getIntPtrConstant(0));
8135}
8136
Dan Gohmand858e902010-04-17 15:26:15 +00008137SDValue X86TargetLowering::LowerFP_TO_SINT(SDValue Op,
8138 SelectionDAG &DAG) const {
Michael Liaobedcbd42012-10-16 18:14:11 +00008139 if (Op.getValueType().isVector()) {
8140 if (Op.getValueType() == MVT::v8i16)
8141 return DAG.getNode(ISD::TRUNCATE, Op.getDebugLoc(), Op.getValueType(),
8142 DAG.getNode(ISD::FP_TO_SINT, Op.getDebugLoc(),
8143 MVT::v8i32, Op.getOperand(0)));
Eli Friedman23ef1052009-06-06 03:57:58 +00008144 return SDValue();
Michael Liaobedcbd42012-10-16 18:14:11 +00008145 }
Eli Friedman23ef1052009-06-06 03:57:58 +00008146
NAKAMURA Takumi9a68fdc2012-02-25 03:37:25 +00008147 std::pair<SDValue,SDValue> Vals = FP_TO_INTHelper(Op, DAG,
8148 /*IsSigned=*/ true, /*IsReplace=*/ false);
Dan Gohman475871a2008-07-27 21:46:04 +00008149 SDValue FIST = Vals.first, StackSlot = Vals.second;
Eli Friedman36df4992009-05-27 00:47:34 +00008150 // If FP_TO_INTHelper failed, the node is actually supposed to be Legal.
8151 if (FIST.getNode() == 0) return Op;
Scott Michelfdc40a02009-02-17 22:15:04 +00008152
Michael J. Spencer1a2d0612012-02-24 19:01:22 +00008153 if (StackSlot.getNode())
8154 // Load the result.
8155 return DAG.getLoad(Op.getValueType(), Op.getDebugLoc(),
8156 FIST, StackSlot, MachinePointerInfo(),
8157 false, false, false, 0);
Craig Topper69947b92012-04-23 06:57:04 +00008158
8159 // The node is the result.
8160 return FIST;
Chris Lattner27a6c732007-11-24 07:07:01 +00008161}
8162
Dan Gohmand858e902010-04-17 15:26:15 +00008163SDValue X86TargetLowering::LowerFP_TO_UINT(SDValue Op,
8164 SelectionDAG &DAG) const {
NAKAMURA Takumi9a68fdc2012-02-25 03:37:25 +00008165 std::pair<SDValue,SDValue> Vals = FP_TO_INTHelper(Op, DAG,
8166 /*IsSigned=*/ false, /*IsReplace=*/ false);
Eli Friedman948e95a2009-05-23 09:59:16 +00008167 SDValue FIST = Vals.first, StackSlot = Vals.second;
8168 assert(FIST.getNode() && "Unexpected failure");
8169
NAKAMURA Takumi9a68fdc2012-02-25 03:37:25 +00008170 if (StackSlot.getNode())
8171 // Load the result.
8172 return DAG.getLoad(Op.getValueType(), Op.getDebugLoc(),
8173 FIST, StackSlot, MachinePointerInfo(),
8174 false, false, false, 0);
Craig Topper69947b92012-04-23 06:57:04 +00008175
8176 // The node is the result.
8177 return FIST;
Eli Friedman948e95a2009-05-23 09:59:16 +00008178}
8179
Michael Liao9d796db2012-10-10 16:32:15 +00008180SDValue X86TargetLowering::lowerFP_EXTEND(SDValue Op,
8181 SelectionDAG &DAG) const {
8182 DebugLoc DL = Op.getDebugLoc();
8183 EVT VT = Op.getValueType();
8184 SDValue In = Op.getOperand(0);
8185 EVT SVT = In.getValueType();
8186
8187 assert(SVT == MVT::v2f32 && "Only customize MVT::v2f32 type legalization!");
8188
8189 return DAG.getNode(X86ISD::VFPEXT, DL, VT,
8190 DAG.getNode(ISD::CONCAT_VECTORS, DL, MVT::v4f32,
8191 In, DAG.getUNDEF(SVT)));
8192}
8193
Craig Topper43620672012-09-08 07:31:51 +00008194SDValue X86TargetLowering::LowerFABS(SDValue Op, SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00008195 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008196 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00008197 EVT VT = Op.getValueType();
8198 EVT EltVT = VT;
Craig Topper43620672012-09-08 07:31:51 +00008199 unsigned NumElts = VT == MVT::f64 ? 2 : 4;
8200 if (VT.isVector()) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00008201 EltVT = VT.getVectorElementType();
Craig Topper43620672012-09-08 07:31:51 +00008202 NumElts = VT.getVectorNumElements();
Evan Cheng0db9fe62006-04-25 20:13:52 +00008203 }
Craig Topper43620672012-09-08 07:31:51 +00008204 Constant *C;
8205 if (EltVT == MVT::f64)
8206 C = ConstantFP::get(*Context, APFloat(APInt(64, ~(1ULL << 63))));
8207 else
8208 C = ConstantFP::get(*Context, APFloat(APInt(32, ~(1U << 31))));
8209 C = ConstantVector::getSplat(NumElts, C);
8210 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy());
8211 unsigned Alignment = cast<ConstantPoolSDNode>(CPIdx)->getAlignment();
Dale Johannesenace16102009-02-03 19:33:06 +00008212 SDValue Mask = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00008213 MachinePointerInfo::getConstantPool(),
Craig Topper43620672012-09-08 07:31:51 +00008214 false, false, false, Alignment);
8215 if (VT.isVector()) {
8216 MVT ANDVT = VT.is128BitVector() ? MVT::v2i64 : MVT::v4i64;
8217 return DAG.getNode(ISD::BITCAST, dl, VT,
8218 DAG.getNode(ISD::AND, dl, ANDVT,
8219 DAG.getNode(ISD::BITCAST, dl, ANDVT,
8220 Op.getOperand(0)),
8221 DAG.getNode(ISD::BITCAST, dl, ANDVT, Mask)));
8222 }
Dale Johannesenace16102009-02-03 19:33:06 +00008223 return DAG.getNode(X86ISD::FAND, dl, VT, Op.getOperand(0), Mask);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008224}
8225
Dan Gohmand858e902010-04-17 15:26:15 +00008226SDValue X86TargetLowering::LowerFNEG(SDValue Op, SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00008227 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008228 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00008229 EVT VT = Op.getValueType();
8230 EVT EltVT = VT;
Chad Rosiera860b182011-12-15 01:02:25 +00008231 unsigned NumElts = VT == MVT::f64 ? 2 : 4;
8232 if (VT.isVector()) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00008233 EltVT = VT.getVectorElementType();
Chad Rosiera860b182011-12-15 01:02:25 +00008234 NumElts = VT.getVectorNumElements();
8235 }
Chris Lattner4ca829e2012-01-25 06:02:56 +00008236 Constant *C;
8237 if (EltVT == MVT::f64)
8238 C = ConstantFP::get(*Context, APFloat(APInt(64, 1ULL << 63)));
8239 else
8240 C = ConstantFP::get(*Context, APFloat(APInt(32, 1U << 31)));
8241 C = ConstantVector::getSplat(NumElts, C);
Craig Toppercacd9d62012-09-08 07:46:05 +00008242 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy());
8243 unsigned Alignment = cast<ConstantPoolSDNode>(CPIdx)->getAlignment();
Dale Johannesenace16102009-02-03 19:33:06 +00008244 SDValue Mask = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00008245 MachinePointerInfo::getConstantPool(),
Craig Toppercacd9d62012-09-08 07:46:05 +00008246 false, false, false, Alignment);
Duncan Sands83ec4b62008-06-06 12:08:01 +00008247 if (VT.isVector()) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00008248 MVT XORVT = VT.is128BitVector() ? MVT::v2i64 : MVT::v4i64;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008249 return DAG.getNode(ISD::BITCAST, dl, VT,
Chad Rosiera860b182011-12-15 01:02:25 +00008250 DAG.getNode(ISD::XOR, dl, XORVT,
Craig Topper69947b92012-04-23 06:57:04 +00008251 DAG.getNode(ISD::BITCAST, dl, XORVT,
8252 Op.getOperand(0)),
8253 DAG.getNode(ISD::BITCAST, dl, XORVT, Mask)));
Evan Chengd4d01b72007-07-19 23:36:01 +00008254 }
Craig Topper69947b92012-04-23 06:57:04 +00008255
8256 return DAG.getNode(X86ISD::FXOR, dl, VT, Op.getOperand(0), Mask);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008257}
8258
Dan Gohmand858e902010-04-17 15:26:15 +00008259SDValue X86TargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00008260 LLVMContext *Context = DAG.getContext();
Dan Gohman475871a2008-07-27 21:46:04 +00008261 SDValue Op0 = Op.getOperand(0);
8262 SDValue Op1 = Op.getOperand(1);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008263 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00008264 EVT VT = Op.getValueType();
8265 EVT SrcVT = Op1.getValueType();
Evan Cheng73d6cf12007-01-05 21:37:56 +00008266
8267 // If second operand is smaller, extend it first.
Duncan Sands8e4eb092008-06-08 20:54:56 +00008268 if (SrcVT.bitsLT(VT)) {
Dale Johannesenace16102009-02-03 19:33:06 +00008269 Op1 = DAG.getNode(ISD::FP_EXTEND, dl, VT, Op1);
Evan Cheng73d6cf12007-01-05 21:37:56 +00008270 SrcVT = VT;
8271 }
Dale Johannesen61c7ef32007-10-21 01:07:44 +00008272 // And if it is bigger, shrink it first.
Duncan Sands8e4eb092008-06-08 20:54:56 +00008273 if (SrcVT.bitsGT(VT)) {
Dale Johannesenace16102009-02-03 19:33:06 +00008274 Op1 = DAG.getNode(ISD::FP_ROUND, dl, VT, Op1, DAG.getIntPtrConstant(1));
Dale Johannesen61c7ef32007-10-21 01:07:44 +00008275 SrcVT = VT;
Dale Johannesen61c7ef32007-10-21 01:07:44 +00008276 }
8277
8278 // At this point the operands and the result should have the same
8279 // type, and that won't be f80 since that is not custom lowered.
Evan Cheng73d6cf12007-01-05 21:37:56 +00008280
Evan Cheng68c47cb2007-01-05 07:55:56 +00008281 // First get the sign bit of second operand.
Chad Rosier01d426e2011-12-15 01:16:09 +00008282 SmallVector<Constant*,4> CV;
Owen Anderson825b72b2009-08-11 20:47:22 +00008283 if (SrcVT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00008284 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 1ULL << 63))));
8285 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 0))));
Evan Cheng68c47cb2007-01-05 07:55:56 +00008286 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00008287 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 1U << 31))));
8288 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
8289 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
8290 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
Evan Cheng68c47cb2007-01-05 07:55:56 +00008291 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00008292 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00008293 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00008294 SDValue Mask1 = DAG.getLoad(SrcVT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00008295 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00008296 false, false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00008297 SDValue SignBit = DAG.getNode(X86ISD::FAND, dl, SrcVT, Op1, Mask1);
Evan Cheng68c47cb2007-01-05 07:55:56 +00008298
8299 // Shift sign bit right or left if the two operands have different types.
Duncan Sands8e4eb092008-06-08 20:54:56 +00008300 if (SrcVT.bitsGT(VT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00008301 // Op0 is MVT::f32, Op1 is MVT::f64.
8302 SignBit = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f64, SignBit);
8303 SignBit = DAG.getNode(X86ISD::FSRL, dl, MVT::v2f64, SignBit,
8304 DAG.getConstant(32, MVT::i32));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008305 SignBit = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, SignBit);
Owen Anderson825b72b2009-08-11 20:47:22 +00008306 SignBit = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f32, SignBit,
Chris Lattner0bd48932008-01-17 07:00:52 +00008307 DAG.getIntPtrConstant(0));
Evan Cheng68c47cb2007-01-05 07:55:56 +00008308 }
8309
Evan Cheng73d6cf12007-01-05 21:37:56 +00008310 // Clear first operand sign bit.
8311 CV.clear();
Owen Anderson825b72b2009-08-11 20:47:22 +00008312 if (VT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00008313 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, ~(1ULL << 63)))));
8314 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 0))));
Evan Cheng73d6cf12007-01-05 21:37:56 +00008315 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00008316 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, ~(1U << 31)))));
8317 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
8318 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
8319 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
Evan Cheng73d6cf12007-01-05 21:37:56 +00008320 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00008321 C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00008322 CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00008323 SDValue Mask2 = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00008324 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00008325 false, false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00008326 SDValue Val = DAG.getNode(X86ISD::FAND, dl, VT, Op0, Mask2);
Evan Cheng73d6cf12007-01-05 21:37:56 +00008327
8328 // Or the value with the sign bit.
Dale Johannesenace16102009-02-03 19:33:06 +00008329 return DAG.getNode(X86ISD::FOR, dl, VT, Val, SignBit);
Evan Cheng68c47cb2007-01-05 07:55:56 +00008330}
8331
Craig Topper55b24052012-09-11 06:15:32 +00008332static SDValue LowerFGETSIGN(SDValue Op, SelectionDAG &DAG) {
Stuart Hastings4fd0dee2011-06-01 04:39:42 +00008333 SDValue N0 = Op.getOperand(0);
8334 DebugLoc dl = Op.getDebugLoc();
8335 EVT VT = Op.getValueType();
8336
8337 // Lower ISD::FGETSIGN to (AND (X86ISD::FGETSIGNx86 ...) 1).
8338 SDValue xFGETSIGN = DAG.getNode(X86ISD::FGETSIGNx86, dl, VT, N0,
8339 DAG.getConstant(1, VT));
8340 return DAG.getNode(ISD::AND, dl, VT, xFGETSIGN, DAG.getConstant(1, VT));
8341}
8342
Michael Liaof966e4e2012-09-13 20:24:54 +00008343// LowerVectorAllZeroTest - Check whether an OR'd tree is PTEST-able.
8344//
8345SDValue X86TargetLowering::LowerVectorAllZeroTest(SDValue Op, SelectionDAG &DAG) const {
8346 assert(Op.getOpcode() == ISD::OR && "Only check OR'd tree.");
8347
8348 if (!Subtarget->hasSSE41())
8349 return SDValue();
8350
8351 if (!Op->hasOneUse())
8352 return SDValue();
8353
8354 SDNode *N = Op.getNode();
8355 DebugLoc DL = N->getDebugLoc();
8356
8357 SmallVector<SDValue, 8> Opnds;
8358 DenseMap<SDValue, unsigned> VecInMap;
8359 EVT VT = MVT::Other;
8360
8361 // Recognize a special case where a vector is casted into wide integer to
8362 // test all 0s.
8363 Opnds.push_back(N->getOperand(0));
8364 Opnds.push_back(N->getOperand(1));
8365
8366 for (unsigned Slot = 0, e = Opnds.size(); Slot < e; ++Slot) {
8367 SmallVector<SDValue, 8>::const_iterator I = Opnds.begin() + Slot;
8368 // BFS traverse all OR'd operands.
8369 if (I->getOpcode() == ISD::OR) {
8370 Opnds.push_back(I->getOperand(0));
8371 Opnds.push_back(I->getOperand(1));
8372 // Re-evaluate the number of nodes to be traversed.
8373 e += 2; // 2 more nodes (LHS and RHS) are pushed.
8374 continue;
8375 }
8376
8377 // Quit if a non-EXTRACT_VECTOR_ELT
8378 if (I->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
8379 return SDValue();
8380
8381 // Quit if without a constant index.
8382 SDValue Idx = I->getOperand(1);
8383 if (!isa<ConstantSDNode>(Idx))
8384 return SDValue();
8385
8386 SDValue ExtractedFromVec = I->getOperand(0);
8387 DenseMap<SDValue, unsigned>::iterator M = VecInMap.find(ExtractedFromVec);
8388 if (M == VecInMap.end()) {
8389 VT = ExtractedFromVec.getValueType();
8390 // Quit if not 128/256-bit vector.
8391 if (!VT.is128BitVector() && !VT.is256BitVector())
8392 return SDValue();
8393 // Quit if not the same type.
8394 if (VecInMap.begin() != VecInMap.end() &&
8395 VT != VecInMap.begin()->first.getValueType())
8396 return SDValue();
8397 M = VecInMap.insert(std::make_pair(ExtractedFromVec, 0)).first;
8398 }
8399 M->second |= 1U << cast<ConstantSDNode>(Idx)->getZExtValue();
8400 }
8401
8402 assert((VT.is128BitVector() || VT.is256BitVector()) &&
Michael Liao9aba7ea2012-09-13 20:30:16 +00008403 "Not extracted from 128-/256-bit vector.");
Michael Liaof966e4e2012-09-13 20:24:54 +00008404
8405 unsigned FullMask = (1U << VT.getVectorNumElements()) - 1U;
8406 SmallVector<SDValue, 8> VecIns;
8407
8408 for (DenseMap<SDValue, unsigned>::const_iterator
8409 I = VecInMap.begin(), E = VecInMap.end(); I != E; ++I) {
8410 // Quit if not all elements are used.
8411 if (I->second != FullMask)
8412 return SDValue();
8413 VecIns.push_back(I->first);
8414 }
8415
8416 EVT TestVT = VT.is128BitVector() ? MVT::v2i64 : MVT::v4i64;
8417
8418 // Cast all vectors into TestVT for PTEST.
8419 for (unsigned i = 0, e = VecIns.size(); i < e; ++i)
8420 VecIns[i] = DAG.getNode(ISD::BITCAST, DL, TestVT, VecIns[i]);
8421
8422 // If more than one full vectors are evaluated, OR them first before PTEST.
8423 for (unsigned Slot = 0, e = VecIns.size(); e - Slot > 1; Slot += 2, e += 1) {
8424 // Each iteration will OR 2 nodes and append the result until there is only
8425 // 1 node left, i.e. the final OR'd value of all vectors.
8426 SDValue LHS = VecIns[Slot];
8427 SDValue RHS = VecIns[Slot + 1];
8428 VecIns.push_back(DAG.getNode(ISD::OR, DL, TestVT, LHS, RHS));
8429 }
8430
8431 return DAG.getNode(X86ISD::PTEST, DL, MVT::i32,
8432 VecIns.back(), VecIns.back());
8433}
8434
Dan Gohman076aee32009-03-04 19:44:21 +00008435/// Emit nodes that will be selected as "test Op0,Op0", or something
8436/// equivalent.
Dan Gohman31125812009-03-07 01:58:32 +00008437SDValue X86TargetLowering::EmitTest(SDValue Op, unsigned X86CC,
Evan Cheng552f09a2010-04-26 19:06:11 +00008438 SelectionDAG &DAG) const {
Dan Gohman076aee32009-03-04 19:44:21 +00008439 DebugLoc dl = Op.getDebugLoc();
8440
Dan Gohman31125812009-03-07 01:58:32 +00008441 // CF and OF aren't always set the way we want. Determine which
8442 // of these we need.
8443 bool NeedCF = false;
8444 bool NeedOF = false;
8445 switch (X86CC) {
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008446 default: break;
Dan Gohman31125812009-03-07 01:58:32 +00008447 case X86::COND_A: case X86::COND_AE:
8448 case X86::COND_B: case X86::COND_BE:
8449 NeedCF = true;
8450 break;
8451 case X86::COND_G: case X86::COND_GE:
8452 case X86::COND_L: case X86::COND_LE:
8453 case X86::COND_O: case X86::COND_NO:
8454 NeedOF = true;
8455 break;
Dan Gohman31125812009-03-07 01:58:32 +00008456 }
8457
Dan Gohman076aee32009-03-04 19:44:21 +00008458 // See if we can use the EFLAGS value from the operand instead of
Dan Gohman31125812009-03-07 01:58:32 +00008459 // doing a separate TEST. TEST always sets OF and CF to 0, so unless
8460 // we prove that the arithmetic won't overflow, we can't use OF or CF.
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008461 if (Op.getResNo() != 0 || NeedOF || NeedCF)
8462 // Emit a CMP with 0, which is the TEST pattern.
8463 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op,
8464 DAG.getConstant(0, Op.getValueType()));
8465
8466 unsigned Opcode = 0;
8467 unsigned NumOperands = 0;
Nadav Rotemb9d6b842012-08-18 17:53:03 +00008468
8469 // Truncate operations may prevent the merge of the SETCC instruction
8470 // and the arithmetic intruction before it. Attempt to truncate the operands
8471 // of the arithmetic instruction and use a reduced bit-width instruction.
8472 bool NeedTruncation = false;
8473 SDValue ArithOp = Op;
8474 if (Op->getOpcode() == ISD::TRUNCATE && Op->hasOneUse()) {
8475 SDValue Arith = Op->getOperand(0);
8476 // Both the trunc and the arithmetic op need to have one user each.
8477 if (Arith->hasOneUse())
8478 switch (Arith.getOpcode()) {
8479 default: break;
8480 case ISD::ADD:
8481 case ISD::SUB:
8482 case ISD::AND:
8483 case ISD::OR:
8484 case ISD::XOR: {
8485 NeedTruncation = true;
8486 ArithOp = Arith;
8487 }
8488 }
8489 }
8490
8491 // NOTICE: In the code below we use ArithOp to hold the arithmetic operation
8492 // which may be the result of a CAST. We use the variable 'Op', which is the
8493 // non-casted variable when we check for possible users.
8494 switch (ArithOp.getOpcode()) {
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008495 case ISD::ADD:
8496 // Due to an isel shortcoming, be conservative if this add is likely to be
8497 // selected as part of a load-modify-store instruction. When the root node
8498 // in a match is a store, isel doesn't know how to remap non-chain non-flag
8499 // uses of other nodes in the match, such as the ADD in this case. This
8500 // leads to the ADD being left around and reselected, with the result being
8501 // two adds in the output. Alas, even if none our users are stores, that
8502 // doesn't prove we're O.K. Ergo, if we have any parents that aren't
8503 // CopyToReg or SETCC, eschew INC/DEC. A better fix seems to require
8504 // climbing the DAG back to the root, and it doesn't seem to be worth the
8505 // effort.
8506 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
Pete Cooper2d496892011-11-15 21:57:53 +00008507 UE = Op.getNode()->use_end(); UI != UE; ++UI)
8508 if (UI->getOpcode() != ISD::CopyToReg &&
8509 UI->getOpcode() != ISD::SETCC &&
8510 UI->getOpcode() != ISD::STORE)
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008511 goto default_case;
8512
8513 if (ConstantSDNode *C =
Nadav Rotemb9d6b842012-08-18 17:53:03 +00008514 dyn_cast<ConstantSDNode>(ArithOp.getNode()->getOperand(1))) {
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008515 // An add of one will be selected as an INC.
8516 if (C->getAPIntValue() == 1) {
8517 Opcode = X86ISD::INC;
8518 NumOperands = 1;
8519 break;
Dan Gohmane220c4b2009-09-18 19:59:53 +00008520 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008521
8522 // An add of negative one (subtract of one) will be selected as a DEC.
8523 if (C->getAPIntValue().isAllOnesValue()) {
8524 Opcode = X86ISD::DEC;
8525 NumOperands = 1;
8526 break;
8527 }
Dan Gohman076aee32009-03-04 19:44:21 +00008528 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008529
8530 // Otherwise use a regular EFLAGS-setting add.
8531 Opcode = X86ISD::ADD;
8532 NumOperands = 2;
8533 break;
8534 case ISD::AND: {
8535 // If the primary and result isn't used, don't bother using X86ISD::AND,
8536 // because a TEST instruction will be better.
8537 bool NonFlagUse = false;
8538 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
8539 UE = Op.getNode()->use_end(); UI != UE; ++UI) {
8540 SDNode *User = *UI;
8541 unsigned UOpNo = UI.getOperandNo();
8542 if (User->getOpcode() == ISD::TRUNCATE && User->hasOneUse()) {
8543 // Look pass truncate.
8544 UOpNo = User->use_begin().getOperandNo();
8545 User = *User->use_begin();
8546 }
8547
8548 if (User->getOpcode() != ISD::BRCOND &&
8549 User->getOpcode() != ISD::SETCC &&
Nadav Rotemb9d6b842012-08-18 17:53:03 +00008550 !(User->getOpcode() == ISD::SELECT && UOpNo == 0)) {
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008551 NonFlagUse = true;
8552 break;
8553 }
Dan Gohman076aee32009-03-04 19:44:21 +00008554 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008555
8556 if (!NonFlagUse)
8557 break;
8558 }
8559 // FALL THROUGH
8560 case ISD::SUB:
8561 case ISD::OR:
8562 case ISD::XOR:
8563 // Due to the ISEL shortcoming noted above, be conservative if this op is
8564 // likely to be selected as part of a load-modify-store instruction.
8565 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
8566 UE = Op.getNode()->use_end(); UI != UE; ++UI)
8567 if (UI->getOpcode() == ISD::STORE)
8568 goto default_case;
8569
8570 // Otherwise use a regular EFLAGS-setting instruction.
Nadav Rotemb9d6b842012-08-18 17:53:03 +00008571 switch (ArithOp.getOpcode()) {
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008572 default: llvm_unreachable("unexpected operator!");
Nadav Rotemb9d6b842012-08-18 17:53:03 +00008573 case ISD::SUB: Opcode = X86ISD::SUB; break;
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008574 case ISD::XOR: Opcode = X86ISD::XOR; break;
8575 case ISD::AND: Opcode = X86ISD::AND; break;
Michael Liaof966e4e2012-09-13 20:24:54 +00008576 case ISD::OR: {
8577 if (!NeedTruncation && (X86CC == X86::COND_E || X86CC == X86::COND_NE)) {
8578 SDValue EFLAGS = LowerVectorAllZeroTest(Op, DAG);
8579 if (EFLAGS.getNode())
8580 return EFLAGS;
8581 }
8582 Opcode = X86ISD::OR;
8583 break;
8584 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008585 }
8586
8587 NumOperands = 2;
8588 break;
8589 case X86ISD::ADD:
8590 case X86ISD::SUB:
8591 case X86ISD::INC:
8592 case X86ISD::DEC:
8593 case X86ISD::OR:
8594 case X86ISD::XOR:
8595 case X86ISD::AND:
8596 return SDValue(Op.getNode(), 1);
8597 default:
8598 default_case:
8599 break;
Dan Gohman076aee32009-03-04 19:44:21 +00008600 }
8601
Nadav Rotemb9d6b842012-08-18 17:53:03 +00008602 // If we found that truncation is beneficial, perform the truncation and
8603 // update 'Op'.
8604 if (NeedTruncation) {
8605 EVT VT = Op.getValueType();
8606 SDValue WideVal = Op->getOperand(0);
8607 EVT WideVT = WideVal.getValueType();
8608 unsigned ConvertedOp = 0;
8609 // Use a target machine opcode to prevent further DAGCombine
8610 // optimizations that may separate the arithmetic operations
8611 // from the setcc node.
8612 switch (WideVal.getOpcode()) {
8613 default: break;
8614 case ISD::ADD: ConvertedOp = X86ISD::ADD; break;
8615 case ISD::SUB: ConvertedOp = X86ISD::SUB; break;
8616 case ISD::AND: ConvertedOp = X86ISD::AND; break;
8617 case ISD::OR: ConvertedOp = X86ISD::OR; break;
8618 case ISD::XOR: ConvertedOp = X86ISD::XOR; break;
8619 }
8620
8621 if (ConvertedOp) {
8622 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
8623 if (TLI.isOperationLegal(WideVal.getOpcode(), WideVT)) {
8624 SDValue V0 = DAG.getNode(ISD::TRUNCATE, dl, VT, WideVal.getOperand(0));
8625 SDValue V1 = DAG.getNode(ISD::TRUNCATE, dl, VT, WideVal.getOperand(1));
8626 Op = DAG.getNode(ConvertedOp, dl, VT, V0, V1);
8627 }
8628 }
8629 }
8630
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008631 if (Opcode == 0)
8632 // Emit a CMP with 0, which is the TEST pattern.
8633 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op,
8634 DAG.getConstant(0, Op.getValueType()));
8635
8636 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::i32);
8637 SmallVector<SDValue, 4> Ops;
8638 for (unsigned i = 0; i != NumOperands; ++i)
8639 Ops.push_back(Op.getOperand(i));
8640
8641 SDValue New = DAG.getNode(Opcode, dl, VTs, &Ops[0], NumOperands);
8642 DAG.ReplaceAllUsesWith(Op, New);
8643 return SDValue(New.getNode(), 1);
Dan Gohman076aee32009-03-04 19:44:21 +00008644}
8645
8646/// Emit nodes that will be selected as "cmp Op0,Op1", or something
8647/// equivalent.
Dan Gohman31125812009-03-07 01:58:32 +00008648SDValue X86TargetLowering::EmitCmp(SDValue Op0, SDValue Op1, unsigned X86CC,
Evan Cheng552f09a2010-04-26 19:06:11 +00008649 SelectionDAG &DAG) const {
Dan Gohman076aee32009-03-04 19:44:21 +00008650 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op1))
8651 if (C->getAPIntValue() == 0)
Evan Cheng552f09a2010-04-26 19:06:11 +00008652 return EmitTest(Op0, X86CC, DAG);
Dan Gohman076aee32009-03-04 19:44:21 +00008653
8654 DebugLoc dl = Op0.getDebugLoc();
Manman Ren39ad5682012-08-08 00:51:41 +00008655 if ((Op0.getValueType() == MVT::i8 || Op0.getValueType() == MVT::i16 ||
8656 Op0.getValueType() == MVT::i32 || Op0.getValueType() == MVT::i64)) {
8657 // Use SUB instead of CMP to enable CSE between SUB and CMP.
8658 SDVTList VTs = DAG.getVTList(Op0.getValueType(), MVT::i32);
8659 SDValue Sub = DAG.getNode(X86ISD::SUB, dl, VTs,
8660 Op0, Op1);
8661 return SDValue(Sub.getNode(), 1);
8662 }
Owen Anderson825b72b2009-08-11 20:47:22 +00008663 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op0, Op1);
Dan Gohman076aee32009-03-04 19:44:21 +00008664}
8665
Benjamin Kramer17c836c2012-04-27 12:07:43 +00008666/// Convert a comparison if required by the subtarget.
8667SDValue X86TargetLowering::ConvertCmpIfNecessary(SDValue Cmp,
8668 SelectionDAG &DAG) const {
8669 // If the subtarget does not support the FUCOMI instruction, floating-point
8670 // comparisons have to be converted.
8671 if (Subtarget->hasCMov() ||
8672 Cmp.getOpcode() != X86ISD::CMP ||
8673 !Cmp.getOperand(0).getValueType().isFloatingPoint() ||
8674 !Cmp.getOperand(1).getValueType().isFloatingPoint())
8675 return Cmp;
8676
8677 // The instruction selector will select an FUCOM instruction instead of
8678 // FUCOMI, which writes the comparison result to FPSW instead of EFLAGS. Hence
8679 // build an SDNode sequence that transfers the result from FPSW into EFLAGS:
8680 // (X86sahf (trunc (srl (X86fp_stsw (trunc (X86cmp ...)), 8))))
8681 DebugLoc dl = Cmp.getDebugLoc();
8682 SDValue TruncFPSW = DAG.getNode(ISD::TRUNCATE, dl, MVT::i16, Cmp);
8683 SDValue FNStSW = DAG.getNode(X86ISD::FNSTSW16r, dl, MVT::i16, TruncFPSW);
8684 SDValue Srl = DAG.getNode(ISD::SRL, dl, MVT::i16, FNStSW,
8685 DAG.getConstant(8, MVT::i8));
8686 SDValue TruncSrl = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Srl);
8687 return DAG.getNode(X86ISD::SAHF, dl, MVT::i32, TruncSrl);
8688}
8689
Evan Chengd40d03e2010-01-06 19:38:29 +00008690/// LowerToBT - Result of 'and' is compared against zero. Turn it into a BT node
8691/// if it's possible.
Evan Cheng5528e7b2010-04-21 01:47:12 +00008692SDValue X86TargetLowering::LowerToBT(SDValue And, ISD::CondCode CC,
8693 DebugLoc dl, SelectionDAG &DAG) const {
Evan Cheng2c755ba2010-02-27 07:36:59 +00008694 SDValue Op0 = And.getOperand(0);
8695 SDValue Op1 = And.getOperand(1);
8696 if (Op0.getOpcode() == ISD::TRUNCATE)
8697 Op0 = Op0.getOperand(0);
8698 if (Op1.getOpcode() == ISD::TRUNCATE)
8699 Op1 = Op1.getOperand(0);
8700
Evan Chengd40d03e2010-01-06 19:38:29 +00008701 SDValue LHS, RHS;
Dan Gohman6b13cbc2010-06-24 02:07:59 +00008702 if (Op1.getOpcode() == ISD::SHL)
8703 std::swap(Op0, Op1);
8704 if (Op0.getOpcode() == ISD::SHL) {
Evan Cheng2c755ba2010-02-27 07:36:59 +00008705 if (ConstantSDNode *And00C = dyn_cast<ConstantSDNode>(Op0.getOperand(0)))
8706 if (And00C->getZExtValue() == 1) {
Dan Gohman6b13cbc2010-06-24 02:07:59 +00008707 // If we looked past a truncate, check that it's only truncating away
8708 // known zeros.
8709 unsigned BitWidth = Op0.getValueSizeInBits();
8710 unsigned AndBitWidth = And.getValueSizeInBits();
8711 if (BitWidth > AndBitWidth) {
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00008712 APInt Zeros, Ones;
8713 DAG.ComputeMaskedBits(Op0, Zeros, Ones);
Dan Gohman6b13cbc2010-06-24 02:07:59 +00008714 if (Zeros.countLeadingOnes() < BitWidth - AndBitWidth)
8715 return SDValue();
8716 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00008717 LHS = Op1;
8718 RHS = Op0.getOperand(1);
Evan Chengd40d03e2010-01-06 19:38:29 +00008719 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00008720 } else if (Op1.getOpcode() == ISD::Constant) {
8721 ConstantSDNode *AndRHS = cast<ConstantSDNode>(Op1);
Benjamin Kramerf238f502011-11-23 13:54:17 +00008722 uint64_t AndRHSVal = AndRHS->getZExtValue();
Evan Cheng2c755ba2010-02-27 07:36:59 +00008723 SDValue AndLHS = Op0;
Benjamin Kramerf238f502011-11-23 13:54:17 +00008724
8725 if (AndRHSVal == 1 && AndLHS.getOpcode() == ISD::SRL) {
Evan Chengd40d03e2010-01-06 19:38:29 +00008726 LHS = AndLHS.getOperand(0);
8727 RHS = AndLHS.getOperand(1);
Dan Gohmane5af2d32009-01-29 01:59:02 +00008728 }
Benjamin Kramerf238f502011-11-23 13:54:17 +00008729
8730 // Use BT if the immediate can't be encoded in a TEST instruction.
8731 if (!isUInt<32>(AndRHSVal) && isPowerOf2_64(AndRHSVal)) {
8732 LHS = AndLHS;
8733 RHS = DAG.getConstant(Log2_64_Ceil(AndRHSVal), LHS.getValueType());
8734 }
Evan Chengd40d03e2010-01-06 19:38:29 +00008735 }
Evan Cheng0488db92007-09-25 01:57:46 +00008736
Evan Chengd40d03e2010-01-06 19:38:29 +00008737 if (LHS.getNode()) {
Evan Chenge5b51ac2010-04-17 06:13:15 +00008738 // If LHS is i8, promote it to i32 with any_extend. There is no i8 BT
Evan Chengd40d03e2010-01-06 19:38:29 +00008739 // instruction. Since the shift amount is in-range-or-undefined, we know
Evan Chenge5b51ac2010-04-17 06:13:15 +00008740 // that doing a bittest on the i32 value is ok. We extend to i32 because
Evan Chengd40d03e2010-01-06 19:38:29 +00008741 // the encoding for the i16 version is larger than the i32 version.
Evan Chenge5b51ac2010-04-17 06:13:15 +00008742 // Also promote i16 to i32 for performance / code size reason.
8743 if (LHS.getValueType() == MVT::i8 ||
Evan Cheng2bce5f4b2010-04-28 08:30:49 +00008744 LHS.getValueType() == MVT::i16)
Evan Chengd40d03e2010-01-06 19:38:29 +00008745 LHS = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, LHS);
Chris Lattnere55484e2008-12-25 05:34:37 +00008746
Evan Chengd40d03e2010-01-06 19:38:29 +00008747 // If the operand types disagree, extend the shift amount to match. Since
8748 // BT ignores high bits (like shifts) we can use anyextend.
8749 if (LHS.getValueType() != RHS.getValueType())
8750 RHS = DAG.getNode(ISD::ANY_EXTEND, dl, LHS.getValueType(), RHS);
Dan Gohmane5af2d32009-01-29 01:59:02 +00008751
Evan Chengd40d03e2010-01-06 19:38:29 +00008752 SDValue BT = DAG.getNode(X86ISD::BT, dl, MVT::i32, LHS, RHS);
8753 unsigned Cond = CC == ISD::SETEQ ? X86::COND_AE : X86::COND_B;
8754 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
8755 DAG.getConstant(Cond, MVT::i8), BT);
Chris Lattnere55484e2008-12-25 05:34:37 +00008756 }
8757
Evan Cheng54de3ea2010-01-05 06:52:31 +00008758 return SDValue();
8759}
8760
Dan Gohmand858e902010-04-17 15:26:15 +00008761SDValue X86TargetLowering::LowerSETCC(SDValue Op, SelectionDAG &DAG) const {
Duncan Sands28b77e92011-09-06 19:07:46 +00008762
8763 if (Op.getValueType().isVector()) return LowerVSETCC(Op, DAG);
8764
Evan Cheng54de3ea2010-01-05 06:52:31 +00008765 assert(Op.getValueType() == MVT::i8 && "SetCC type must be 8-bit integer");
8766 SDValue Op0 = Op.getOperand(0);
8767 SDValue Op1 = Op.getOperand(1);
8768 DebugLoc dl = Op.getDebugLoc();
8769 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
8770
8771 // Optimize to BT if possible.
Evan Chengd40d03e2010-01-06 19:38:29 +00008772 // Lower (X & (1 << N)) == 0 to BT(X, N).
8773 // Lower ((X >>u N) & 1) != 0 to BT(X, N).
8774 // Lower ((X >>s N) & 1) != 0 to BT(X, N).
Andrew Trickf6c39412011-03-23 23:11:02 +00008775 if (Op0.getOpcode() == ISD::AND && Op0.hasOneUse() &&
Evan Chengd40d03e2010-01-06 19:38:29 +00008776 Op1.getOpcode() == ISD::Constant &&
Dan Gohmane368b462010-06-18 14:22:04 +00008777 cast<ConstantSDNode>(Op1)->isNullValue() &&
Evan Chengd40d03e2010-01-06 19:38:29 +00008778 (CC == ISD::SETEQ || CC == ISD::SETNE)) {
8779 SDValue NewSetCC = LowerToBT(Op0, CC, dl, DAG);
8780 if (NewSetCC.getNode())
8781 return NewSetCC;
8782 }
Evan Cheng54de3ea2010-01-05 06:52:31 +00008783
Chris Lattner481eebc2010-12-19 21:23:48 +00008784 // Look for X == 0, X == 1, X != 0, or X != 1. We can simplify some forms of
8785 // these.
8786 if (Op1.getOpcode() == ISD::Constant &&
Andrew Trickf6c39412011-03-23 23:11:02 +00008787 (cast<ConstantSDNode>(Op1)->getZExtValue() == 1 ||
Evan Cheng2c755ba2010-02-27 07:36:59 +00008788 cast<ConstantSDNode>(Op1)->isNullValue()) &&
8789 (CC == ISD::SETEQ || CC == ISD::SETNE)) {
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008790
Chris Lattner481eebc2010-12-19 21:23:48 +00008791 // If the input is a setcc, then reuse the input setcc or use a new one with
8792 // the inverted condition.
8793 if (Op0.getOpcode() == X86ISD::SETCC) {
8794 X86::CondCode CCode = (X86::CondCode)Op0.getConstantOperandVal(0);
8795 bool Invert = (CC == ISD::SETNE) ^
8796 cast<ConstantSDNode>(Op1)->isNullValue();
8797 if (!Invert) return Op0;
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008798
Evan Cheng2c755ba2010-02-27 07:36:59 +00008799 CCode = X86::GetOppositeBranchCondition(CCode);
Chris Lattner481eebc2010-12-19 21:23:48 +00008800 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
8801 DAG.getConstant(CCode, MVT::i8), Op0.getOperand(1));
8802 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00008803 }
8804
Evan Chenge5b51ac2010-04-17 06:13:15 +00008805 bool isFP = Op1.getValueType().isFloatingPoint();
Chris Lattnere55484e2008-12-25 05:34:37 +00008806 unsigned X86CC = TranslateX86CC(CC, isFP, Op0, Op1, DAG);
Dan Gohman1a492952009-10-20 16:22:37 +00008807 if (X86CC == X86::COND_INVALID)
8808 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00008809
Chris Lattnerc19d1c32010-12-19 22:08:31 +00008810 SDValue EFLAGS = EmitCmp(Op0, Op1, X86CC, DAG);
Benjamin Kramer17c836c2012-04-27 12:07:43 +00008811 EFLAGS = ConvertCmpIfNecessary(EFLAGS, DAG);
Owen Anderson825b72b2009-08-11 20:47:22 +00008812 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
Chris Lattnerc19d1c32010-12-19 22:08:31 +00008813 DAG.getConstant(X86CC, MVT::i8), EFLAGS);
Evan Cheng0488db92007-09-25 01:57:46 +00008814}
8815
Craig Topper89af15e2011-09-18 08:03:58 +00008816// Lower256IntVSETCC - Break a VSETCC 256-bit integer VSETCC into two new 128
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00008817// ones, and then concatenate the result back.
Craig Topper89af15e2011-09-18 08:03:58 +00008818static SDValue Lower256IntVSETCC(SDValue Op, SelectionDAG &DAG) {
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00008819 EVT VT = Op.getValueType();
8820
Craig Topper7a9a28b2012-08-12 02:23:29 +00008821 assert(VT.is256BitVector() && Op.getOpcode() == ISD::SETCC &&
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00008822 "Unsupported value type for operation");
8823
Craig Topper66ddd152012-04-27 22:54:43 +00008824 unsigned NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00008825 DebugLoc dl = Op.getDebugLoc();
8826 SDValue CC = Op.getOperand(2);
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00008827
8828 // Extract the LHS vectors
8829 SDValue LHS = Op.getOperand(0);
Craig Topperb14940a2012-04-22 20:55:18 +00008830 SDValue LHS1 = Extract128BitVector(LHS, 0, DAG, dl);
8831 SDValue LHS2 = Extract128BitVector(LHS, NumElems/2, DAG, dl);
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00008832
8833 // Extract the RHS vectors
8834 SDValue RHS = Op.getOperand(1);
Craig Topperb14940a2012-04-22 20:55:18 +00008835 SDValue RHS1 = Extract128BitVector(RHS, 0, DAG, dl);
8836 SDValue RHS2 = Extract128BitVector(RHS, NumElems/2, DAG, dl);
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00008837
8838 // Issue the operation on the smaller types and concatenate the result back
8839 MVT EltVT = VT.getVectorElementType().getSimpleVT();
8840 EVT NewVT = MVT::getVectorVT(EltVT, NumElems/2);
8841 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT,
8842 DAG.getNode(Op.getOpcode(), dl, NewVT, LHS1, RHS1, CC),
8843 DAG.getNode(Op.getOpcode(), dl, NewVT, LHS2, RHS2, CC));
8844}
8845
8846
Dan Gohmand858e902010-04-17 15:26:15 +00008847SDValue X86TargetLowering::LowerVSETCC(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +00008848 SDValue Cond;
8849 SDValue Op0 = Op.getOperand(0);
8850 SDValue Op1 = Op.getOperand(1);
8851 SDValue CC = Op.getOperand(2);
Owen Andersone50ed302009-08-10 22:56:29 +00008852 EVT VT = Op.getValueType();
Nate Begeman30a0de92008-07-17 16:51:19 +00008853 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
8854 bool isFP = Op.getOperand(1).getValueType().isFloatingPoint();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008855 DebugLoc dl = Op.getDebugLoc();
Nate Begeman30a0de92008-07-17 16:51:19 +00008856
8857 if (isFP) {
Craig Topper523908d2012-08-13 02:34:03 +00008858#ifndef NDEBUG
Bruno Cardoso Lopes0f0e0a02011-08-09 00:46:57 +00008859 EVT EltVT = Op0.getValueType().getVectorElementType();
Craig Topper523908d2012-08-13 02:34:03 +00008860 assert(EltVT == MVT::f32 || EltVT == MVT::f64);
8861#endif
Bruno Cardoso Lopes0f0e0a02011-08-09 00:46:57 +00008862
Craig Topper523908d2012-08-13 02:34:03 +00008863 unsigned SSECC;
Nate Begeman30a0de92008-07-17 16:51:19 +00008864 bool Swap = false;
8865
Bruno Cardoso Lopes8e03a822011-09-12 19:30:40 +00008866 // SSE Condition code mapping:
8867 // 0 - EQ
8868 // 1 - LT
8869 // 2 - LE
8870 // 3 - UNORD
8871 // 4 - NEQ
8872 // 5 - NLT
8873 // 6 - NLE
8874 // 7 - ORD
Nate Begeman30a0de92008-07-17 16:51:19 +00008875 switch (SetCCOpcode) {
Craig Topper2f1b2ec2012-08-13 03:42:38 +00008876 default: llvm_unreachable("Unexpected SETCC condition");
Nate Begemanfb8ead02008-07-25 19:05:58 +00008877 case ISD::SETOEQ:
Nate Begeman30a0de92008-07-17 16:51:19 +00008878 case ISD::SETEQ: SSECC = 0; break;
Bruno Cardoso Lopes8e03a822011-09-12 19:30:40 +00008879 case ISD::SETOGT:
8880 case ISD::SETGT: Swap = true; // Fallthrough
Bruno Cardoso Lopes457d53d2011-09-12 21:24:07 +00008881 case ISD::SETLT:
8882 case ISD::SETOLT: SSECC = 1; break;
8883 case ISD::SETOGE:
8884 case ISD::SETGE: Swap = true; // Fallthrough
Nate Begeman30a0de92008-07-17 16:51:19 +00008885 case ISD::SETLE:
8886 case ISD::SETOLE: SSECC = 2; break;
8887 case ISD::SETUO: SSECC = 3; break;
Nate Begemanfb8ead02008-07-25 19:05:58 +00008888 case ISD::SETUNE:
Nate Begeman30a0de92008-07-17 16:51:19 +00008889 case ISD::SETNE: SSECC = 4; break;
Craig Topper523908d2012-08-13 02:34:03 +00008890 case ISD::SETULE: Swap = true; // Fallthrough
Nate Begeman30a0de92008-07-17 16:51:19 +00008891 case ISD::SETUGE: SSECC = 5; break;
Craig Topper523908d2012-08-13 02:34:03 +00008892 case ISD::SETULT: Swap = true; // Fallthrough
Nate Begeman30a0de92008-07-17 16:51:19 +00008893 case ISD::SETUGT: SSECC = 6; break;
8894 case ISD::SETO: SSECC = 7; break;
Craig Topper523908d2012-08-13 02:34:03 +00008895 case ISD::SETUEQ:
8896 case ISD::SETONE: SSECC = 8; break;
Nate Begeman30a0de92008-07-17 16:51:19 +00008897 }
8898 if (Swap)
8899 std::swap(Op0, Op1);
8900
Nate Begemanfb8ead02008-07-25 19:05:58 +00008901 // In the two special cases we can't handle, emit two comparisons.
Nate Begeman30a0de92008-07-17 16:51:19 +00008902 if (SSECC == 8) {
Craig Topper523908d2012-08-13 02:34:03 +00008903 unsigned CC0, CC1;
8904 unsigned CombineOpc;
Nate Begemanfb8ead02008-07-25 19:05:58 +00008905 if (SetCCOpcode == ISD::SETUEQ) {
Craig Topper523908d2012-08-13 02:34:03 +00008906 CC0 = 3; CC1 = 0; CombineOpc = ISD::OR;
8907 } else {
8908 assert(SetCCOpcode == ISD::SETONE);
8909 CC0 = 7; CC1 = 4; CombineOpc = ISD::AND;
Craig Topper69947b92012-04-23 06:57:04 +00008910 }
Craig Topper523908d2012-08-13 02:34:03 +00008911
8912 SDValue Cmp0 = DAG.getNode(X86ISD::CMPP, dl, VT, Op0, Op1,
8913 DAG.getConstant(CC0, MVT::i8));
8914 SDValue Cmp1 = DAG.getNode(X86ISD::CMPP, dl, VT, Op0, Op1,
8915 DAG.getConstant(CC1, MVT::i8));
8916 return DAG.getNode(CombineOpc, dl, VT, Cmp0, Cmp1);
Nate Begeman30a0de92008-07-17 16:51:19 +00008917 }
8918 // Handle all other FP comparisons here.
Craig Topper1906d322012-01-22 23:36:02 +00008919 return DAG.getNode(X86ISD::CMPP, dl, VT, Op0, Op1,
8920 DAG.getConstant(SSECC, MVT::i8));
Nate Begeman30a0de92008-07-17 16:51:19 +00008921 }
Scott Michelfdc40a02009-02-17 22:15:04 +00008922
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00008923 // Break 256-bit integer vector compare into smaller ones.
Craig Topper7a9a28b2012-08-12 02:23:29 +00008924 if (VT.is256BitVector() && !Subtarget->hasAVX2())
Craig Topper89af15e2011-09-18 08:03:58 +00008925 return Lower256IntVSETCC(Op, DAG);
Bruno Cardoso Lopes0f0e0a02011-08-09 00:46:57 +00008926
Nate Begeman30a0de92008-07-17 16:51:19 +00008927 // We are handling one of the integer comparisons here. Since SSE only has
8928 // GT and EQ comparisons for integer, swapping operands and multiple
8929 // operations may be required for some comparisons.
Craig Topper2f1b2ec2012-08-13 03:42:38 +00008930 unsigned Opc;
Nate Begeman30a0de92008-07-17 16:51:19 +00008931 bool Swap = false, Invert = false, FlipSigns = false;
Scott Michelfdc40a02009-02-17 22:15:04 +00008932
Nate Begeman30a0de92008-07-17 16:51:19 +00008933 switch (SetCCOpcode) {
Craig Topper2f1b2ec2012-08-13 03:42:38 +00008934 default: llvm_unreachable("Unexpected SETCC condition");
Nate Begeman30a0de92008-07-17 16:51:19 +00008935 case ISD::SETNE: Invert = true;
Craig Topper67609fd2012-01-22 22:42:16 +00008936 case ISD::SETEQ: Opc = X86ISD::PCMPEQ; break;
Nate Begeman30a0de92008-07-17 16:51:19 +00008937 case ISD::SETLT: Swap = true;
Craig Topper67609fd2012-01-22 22:42:16 +00008938 case ISD::SETGT: Opc = X86ISD::PCMPGT; break;
Nate Begeman30a0de92008-07-17 16:51:19 +00008939 case ISD::SETGE: Swap = true;
Craig Topper67609fd2012-01-22 22:42:16 +00008940 case ISD::SETLE: Opc = X86ISD::PCMPGT; Invert = true; break;
Nate Begeman30a0de92008-07-17 16:51:19 +00008941 case ISD::SETULT: Swap = true;
Craig Topper67609fd2012-01-22 22:42:16 +00008942 case ISD::SETUGT: Opc = X86ISD::PCMPGT; FlipSigns = true; break;
Nate Begeman30a0de92008-07-17 16:51:19 +00008943 case ISD::SETUGE: Swap = true;
Craig Topper67609fd2012-01-22 22:42:16 +00008944 case ISD::SETULE: Opc = X86ISD::PCMPGT; FlipSigns = true; Invert = true; break;
Nate Begeman30a0de92008-07-17 16:51:19 +00008945 }
8946 if (Swap)
8947 std::swap(Op0, Op1);
Scott Michelfdc40a02009-02-17 22:15:04 +00008948
Eli Friedman7d3e2b72011-09-28 21:00:25 +00008949 // Check that the operation in question is available (most are plain SSE2,
8950 // but PCMPGTQ and PCMPEQQ have different requirements).
Craig Topper2f1b2ec2012-08-13 03:42:38 +00008951 if (VT == MVT::v2i64) {
8952 if (Opc == X86ISD::PCMPGT && !Subtarget->hasSSE42())
8953 return SDValue();
8954 if (Opc == X86ISD::PCMPEQ && !Subtarget->hasSSE41())
8955 return SDValue();
8956 }
Eli Friedman7d3e2b72011-09-28 21:00:25 +00008957
Nate Begeman30a0de92008-07-17 16:51:19 +00008958 // Since SSE has no unsigned integer comparisons, we need to flip the sign
8959 // bits of the inputs before performing those operations.
8960 if (FlipSigns) {
Owen Andersone50ed302009-08-10 22:56:29 +00008961 EVT EltVT = VT.getVectorElementType();
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00008962 SDValue SignBit = DAG.getConstant(APInt::getSignBit(EltVT.getSizeInBits()),
8963 EltVT);
Dan Gohman475871a2008-07-27 21:46:04 +00008964 std::vector<SDValue> SignBits(VT.getVectorNumElements(), SignBit);
Evan Chenga87008d2009-02-25 22:49:59 +00008965 SDValue SignVec = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &SignBits[0],
8966 SignBits.size());
Dale Johannesenace16102009-02-03 19:33:06 +00008967 Op0 = DAG.getNode(ISD::XOR, dl, VT, Op0, SignVec);
8968 Op1 = DAG.getNode(ISD::XOR, dl, VT, Op1, SignVec);
Nate Begeman30a0de92008-07-17 16:51:19 +00008969 }
Scott Michelfdc40a02009-02-17 22:15:04 +00008970
Dale Johannesenace16102009-02-03 19:33:06 +00008971 SDValue Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
Nate Begeman30a0de92008-07-17 16:51:19 +00008972
8973 // If the logical-not of the result is required, perform that now.
Bob Wilson4c245462009-01-22 17:39:32 +00008974 if (Invert)
Dale Johannesenace16102009-02-03 19:33:06 +00008975 Result = DAG.getNOT(dl, Result, VT);
Bob Wilson4c245462009-01-22 17:39:32 +00008976
Nate Begeman30a0de92008-07-17 16:51:19 +00008977 return Result;
8978}
Evan Cheng0488db92007-09-25 01:57:46 +00008979
Evan Cheng370e5342008-12-03 08:38:43 +00008980// isX86LogicalCmp - Return true if opcode is a X86 logical comparison.
Dan Gohman076aee32009-03-04 19:44:21 +00008981static bool isX86LogicalCmp(SDValue Op) {
8982 unsigned Opc = Op.getNode()->getOpcode();
Benjamin Kramer17c836c2012-04-27 12:07:43 +00008983 if (Opc == X86ISD::CMP || Opc == X86ISD::COMI || Opc == X86ISD::UCOMI ||
8984 Opc == X86ISD::SAHF)
Dan Gohman076aee32009-03-04 19:44:21 +00008985 return true;
8986 if (Op.getResNo() == 1 &&
8987 (Opc == X86ISD::ADD ||
8988 Opc == X86ISD::SUB ||
Chris Lattner5b856542010-12-20 00:59:46 +00008989 Opc == X86ISD::ADC ||
8990 Opc == X86ISD::SBB ||
Dan Gohman076aee32009-03-04 19:44:21 +00008991 Opc == X86ISD::SMUL ||
8992 Opc == X86ISD::UMUL ||
8993 Opc == X86ISD::INC ||
Dan Gohmane220c4b2009-09-18 19:59:53 +00008994 Opc == X86ISD::DEC ||
8995 Opc == X86ISD::OR ||
8996 Opc == X86ISD::XOR ||
8997 Opc == X86ISD::AND))
Dan Gohman076aee32009-03-04 19:44:21 +00008998 return true;
8999
Chris Lattner9637d5b2010-12-05 07:49:54 +00009000 if (Op.getResNo() == 2 && Opc == X86ISD::UMUL)
9001 return true;
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009002
Dan Gohman076aee32009-03-04 19:44:21 +00009003 return false;
Evan Cheng370e5342008-12-03 08:38:43 +00009004}
9005
Chris Lattnera2b56002010-12-05 01:23:24 +00009006static bool isZero(SDValue V) {
9007 ConstantSDNode *C = dyn_cast<ConstantSDNode>(V);
9008 return C && C->isNullValue();
9009}
9010
Chris Lattner96908b12010-12-05 02:00:51 +00009011static bool isAllOnes(SDValue V) {
9012 ConstantSDNode *C = dyn_cast<ConstantSDNode>(V);
9013 return C && C->isAllOnesValue();
9014}
9015
Evan Chengb64dd5f2012-08-07 22:21:00 +00009016static bool isTruncWithZeroHighBitsInput(SDValue V, SelectionDAG &DAG) {
9017 if (V.getOpcode() != ISD::TRUNCATE)
9018 return false;
9019
9020 SDValue VOp0 = V.getOperand(0);
9021 unsigned InBits = VOp0.getValueSizeInBits();
9022 unsigned Bits = V.getValueSizeInBits();
9023 return DAG.MaskedValueIsZero(VOp0, APInt::getHighBitsSet(InBits,InBits-Bits));
9024}
9025
Dan Gohmand858e902010-04-17 15:26:15 +00009026SDValue X86TargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng734503b2006-09-11 02:19:56 +00009027 bool addTest = true;
Dan Gohman475871a2008-07-27 21:46:04 +00009028 SDValue Cond = Op.getOperand(0);
Chris Lattnera2b56002010-12-05 01:23:24 +00009029 SDValue Op1 = Op.getOperand(1);
9030 SDValue Op2 = Op.getOperand(2);
9031 DebugLoc DL = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00009032 SDValue CC;
Evan Cheng9bba8942006-01-26 02:13:10 +00009033
Dan Gohman1a492952009-10-20 16:22:37 +00009034 if (Cond.getOpcode() == ISD::SETCC) {
9035 SDValue NewCond = LowerSETCC(Cond, DAG);
9036 if (NewCond.getNode())
9037 Cond = NewCond;
9038 }
Evan Cheng734503b2006-09-11 02:19:56 +00009039
Chris Lattnera2b56002010-12-05 01:23:24 +00009040 // (select (x == 0), -1, y) -> (sign_bit (x - 1)) | y
Chris Lattner96908b12010-12-05 02:00:51 +00009041 // (select (x == 0), y, -1) -> ~(sign_bit (x - 1)) | y
Chris Lattnera2b56002010-12-05 01:23:24 +00009042 // (select (x != 0), y, -1) -> (sign_bit (x - 1)) | y
Chris Lattner96908b12010-12-05 02:00:51 +00009043 // (select (x != 0), -1, y) -> ~(sign_bit (x - 1)) | y
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00009044 if (Cond.getOpcode() == X86ISD::SETCC &&
Chris Lattner96908b12010-12-05 02:00:51 +00009045 Cond.getOperand(1).getOpcode() == X86ISD::CMP &&
9046 isZero(Cond.getOperand(1).getOperand(1))) {
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00009047 SDValue Cmp = Cond.getOperand(1);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009048
Chris Lattnera2b56002010-12-05 01:23:24 +00009049 unsigned CondCode =cast<ConstantSDNode>(Cond.getOperand(0))->getZExtValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009050
9051 if ((isAllOnes(Op1) || isAllOnes(Op2)) &&
Chris Lattner96908b12010-12-05 02:00:51 +00009052 (CondCode == X86::COND_E || CondCode == X86::COND_NE)) {
9053 SDValue Y = isAllOnes(Op2) ? Op1 : Op2;
Chris Lattnera2b56002010-12-05 01:23:24 +00009054
9055 SDValue CmpOp0 = Cmp.getOperand(0);
Manman Rened579842012-05-07 18:06:23 +00009056 // Apply further optimizations for special cases
9057 // (select (x != 0), -1, 0) -> neg & sbb
9058 // (select (x == 0), 0, -1) -> neg & sbb
9059 if (ConstantSDNode *YC = dyn_cast<ConstantSDNode>(Y))
Chad Rosiera20e1e72012-08-01 18:39:17 +00009060 if (YC->isNullValue() &&
Manman Rened579842012-05-07 18:06:23 +00009061 (isAllOnes(Op1) == (CondCode == X86::COND_NE))) {
9062 SDVTList VTs = DAG.getVTList(CmpOp0.getValueType(), MVT::i32);
Chad Rosiera20e1e72012-08-01 18:39:17 +00009063 SDValue Neg = DAG.getNode(X86ISD::SUB, DL, VTs,
9064 DAG.getConstant(0, CmpOp0.getValueType()),
Manman Rened579842012-05-07 18:06:23 +00009065 CmpOp0);
9066 SDValue Res = DAG.getNode(X86ISD::SETCC_CARRY, DL, Op.getValueType(),
9067 DAG.getConstant(X86::COND_B, MVT::i8),
9068 SDValue(Neg.getNode(), 1));
9069 return Res;
9070 }
9071
Chris Lattnera2b56002010-12-05 01:23:24 +00009072 Cmp = DAG.getNode(X86ISD::CMP, DL, MVT::i32,
9073 CmpOp0, DAG.getConstant(1, CmpOp0.getValueType()));
Benjamin Kramer17c836c2012-04-27 12:07:43 +00009074 Cmp = ConvertCmpIfNecessary(Cmp, DAG);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009075
Chris Lattner96908b12010-12-05 02:00:51 +00009076 SDValue Res = // Res = 0 or -1.
Chris Lattnera2b56002010-12-05 01:23:24 +00009077 DAG.getNode(X86ISD::SETCC_CARRY, DL, Op.getValueType(),
9078 DAG.getConstant(X86::COND_B, MVT::i8), Cmp);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009079
Chris Lattner96908b12010-12-05 02:00:51 +00009080 if (isAllOnes(Op1) != (CondCode == X86::COND_E))
9081 Res = DAG.getNOT(DL, Res, Res.getValueType());
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009082
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00009083 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(Op2);
Chris Lattnera2b56002010-12-05 01:23:24 +00009084 if (N2C == 0 || !N2C->isNullValue())
9085 Res = DAG.getNode(ISD::OR, DL, Res.getValueType(), Res, Y);
9086 return Res;
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00009087 }
9088 }
9089
Chris Lattnera2b56002010-12-05 01:23:24 +00009090 // Look past (and (setcc_carry (cmp ...)), 1).
Evan Chengad9c0a32009-12-15 00:53:42 +00009091 if (Cond.getOpcode() == ISD::AND &&
9092 Cond.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY) {
9093 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Cond.getOperand(1));
Michael J. Spencerec38de22010-10-10 22:04:20 +00009094 if (C && C->getAPIntValue() == 1)
Evan Chengad9c0a32009-12-15 00:53:42 +00009095 Cond = Cond.getOperand(0);
9096 }
9097
Evan Cheng3f41d662007-10-08 22:16:29 +00009098 // If condition flag is set by a X86ISD::CMP, then use it as the condition
9099 // setting operand in place of the X86ISD::SETCC.
Dan Gohman65fd6562011-11-03 21:49:52 +00009100 unsigned CondOpcode = Cond.getOpcode();
9101 if (CondOpcode == X86ISD::SETCC ||
9102 CondOpcode == X86ISD::SETCC_CARRY) {
Evan Cheng734503b2006-09-11 02:19:56 +00009103 CC = Cond.getOperand(0);
9104
Dan Gohman475871a2008-07-27 21:46:04 +00009105 SDValue Cmp = Cond.getOperand(1);
Evan Cheng734503b2006-09-11 02:19:56 +00009106 unsigned Opc = Cmp.getOpcode();
Owen Andersone50ed302009-08-10 22:56:29 +00009107 EVT VT = Op.getValueType();
Scott Michelfdc40a02009-02-17 22:15:04 +00009108
Evan Cheng3f41d662007-10-08 22:16:29 +00009109 bool IllegalFPCMov = false;
Duncan Sands83ec4b62008-06-06 12:08:01 +00009110 if (VT.isFloatingPoint() && !VT.isVector() &&
Chris Lattner78631162008-01-16 06:24:21 +00009111 !isScalarFPTypeInSSEReg(VT)) // FPStack?
Dan Gohman7810bfe2008-09-26 21:54:37 +00009112 IllegalFPCMov = !hasFPCMov(cast<ConstantSDNode>(CC)->getSExtValue());
Scott Michelfdc40a02009-02-17 22:15:04 +00009113
Chris Lattnerd1980a52009-03-12 06:52:53 +00009114 if ((isX86LogicalCmp(Cmp) && !IllegalFPCMov) ||
9115 Opc == X86ISD::BT) { // FIXME
Evan Cheng3f41d662007-10-08 22:16:29 +00009116 Cond = Cmp;
Evan Cheng0488db92007-09-25 01:57:46 +00009117 addTest = false;
9118 }
Dan Gohman65fd6562011-11-03 21:49:52 +00009119 } else if (CondOpcode == ISD::USUBO || CondOpcode == ISD::SSUBO ||
9120 CondOpcode == ISD::UADDO || CondOpcode == ISD::SADDO ||
9121 ((CondOpcode == ISD::UMULO || CondOpcode == ISD::SMULO) &&
9122 Cond.getOperand(0).getValueType() != MVT::i8)) {
9123 SDValue LHS = Cond.getOperand(0);
9124 SDValue RHS = Cond.getOperand(1);
9125 unsigned X86Opcode;
9126 unsigned X86Cond;
9127 SDVTList VTs;
9128 switch (CondOpcode) {
9129 case ISD::UADDO: X86Opcode = X86ISD::ADD; X86Cond = X86::COND_B; break;
9130 case ISD::SADDO: X86Opcode = X86ISD::ADD; X86Cond = X86::COND_O; break;
9131 case ISD::USUBO: X86Opcode = X86ISD::SUB; X86Cond = X86::COND_B; break;
9132 case ISD::SSUBO: X86Opcode = X86ISD::SUB; X86Cond = X86::COND_O; break;
9133 case ISD::UMULO: X86Opcode = X86ISD::UMUL; X86Cond = X86::COND_O; break;
9134 case ISD::SMULO: X86Opcode = X86ISD::SMUL; X86Cond = X86::COND_O; break;
9135 default: llvm_unreachable("unexpected overflowing operator");
9136 }
9137 if (CondOpcode == ISD::UMULO)
9138 VTs = DAG.getVTList(LHS.getValueType(), LHS.getValueType(),
9139 MVT::i32);
9140 else
9141 VTs = DAG.getVTList(LHS.getValueType(), MVT::i32);
9142
9143 SDValue X86Op = DAG.getNode(X86Opcode, DL, VTs, LHS, RHS);
9144
9145 if (CondOpcode == ISD::UMULO)
9146 Cond = X86Op.getValue(2);
9147 else
9148 Cond = X86Op.getValue(1);
9149
9150 CC = DAG.getConstant(X86Cond, MVT::i8);
9151 addTest = false;
Evan Cheng0488db92007-09-25 01:57:46 +00009152 }
9153
9154 if (addTest) {
Evan Chengb64dd5f2012-08-07 22:21:00 +00009155 // Look pass the truncate if the high bits are known zero.
9156 if (isTruncWithZeroHighBitsInput(Cond, DAG))
9157 Cond = Cond.getOperand(0);
Evan Chengd40d03e2010-01-06 19:38:29 +00009158
9159 // We know the result of AND is compared against zero. Try to match
9160 // it to BT.
Michael J. Spencerec38de22010-10-10 22:04:20 +00009161 if (Cond.getOpcode() == ISD::AND && Cond.hasOneUse()) {
Chris Lattnera2b56002010-12-05 01:23:24 +00009162 SDValue NewSetCC = LowerToBT(Cond, ISD::SETNE, DL, DAG);
Evan Chengd40d03e2010-01-06 19:38:29 +00009163 if (NewSetCC.getNode()) {
9164 CC = NewSetCC.getOperand(0);
9165 Cond = NewSetCC.getOperand(1);
9166 addTest = false;
9167 }
9168 }
9169 }
9170
9171 if (addTest) {
Owen Anderson825b72b2009-08-11 20:47:22 +00009172 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng552f09a2010-04-26 19:06:11 +00009173 Cond = EmitTest(Cond, X86::COND_NE, DAG);
Evan Cheng0488db92007-09-25 01:57:46 +00009174 }
9175
Benjamin Kramere915ff32010-12-22 23:09:28 +00009176 // a < b ? -1 : 0 -> RES = ~setcc_carry
9177 // a < b ? 0 : -1 -> RES = setcc_carry
9178 // a >= b ? -1 : 0 -> RES = setcc_carry
9179 // a >= b ? 0 : -1 -> RES = ~setcc_carry
Manman Ren39ad5682012-08-08 00:51:41 +00009180 if (Cond.getOpcode() == X86ISD::SUB) {
Benjamin Kramer17c836c2012-04-27 12:07:43 +00009181 Cond = ConvertCmpIfNecessary(Cond, DAG);
Benjamin Kramere915ff32010-12-22 23:09:28 +00009182 unsigned CondCode = cast<ConstantSDNode>(CC)->getZExtValue();
9183
9184 if ((CondCode == X86::COND_AE || CondCode == X86::COND_B) &&
9185 (isAllOnes(Op1) || isAllOnes(Op2)) && (isZero(Op1) || isZero(Op2))) {
9186 SDValue Res = DAG.getNode(X86ISD::SETCC_CARRY, DL, Op.getValueType(),
9187 DAG.getConstant(X86::COND_B, MVT::i8), Cond);
9188 if (isAllOnes(Op1) != (CondCode == X86::COND_B))
9189 return DAG.getNOT(DL, Res, Res.getValueType());
9190 return Res;
9191 }
9192 }
9193
Benjamin Kramer444dcce2012-10-13 10:39:49 +00009194 // X86 doesn't have an i8 cmov. If both operands are the result of a truncate
9195 // widen the cmov and push the truncate through. This avoids introducing a new
9196 // branch during isel and doesn't add any extensions.
9197 if (Op.getValueType() == MVT::i8 &&
9198 Op1.getOpcode() == ISD::TRUNCATE && Op2.getOpcode() == ISD::TRUNCATE) {
9199 SDValue T1 = Op1.getOperand(0), T2 = Op2.getOperand(0);
9200 if (T1.getValueType() == T2.getValueType() &&
9201 // Blacklist CopyFromReg to avoid partial register stalls.
9202 T1.getOpcode() != ISD::CopyFromReg && T2.getOpcode()!=ISD::CopyFromReg){
9203 SDVTList VTs = DAG.getVTList(T1.getValueType(), MVT::Glue);
Benjamin Kramerf8b65aa2012-10-13 12:50:19 +00009204 SDValue Cmov = DAG.getNode(X86ISD::CMOV, DL, VTs, T2, T1, CC, Cond);
Benjamin Kramer444dcce2012-10-13 10:39:49 +00009205 return DAG.getNode(ISD::TRUNCATE, DL, Op.getValueType(), Cmov);
9206 }
9207 }
9208
Evan Cheng0488db92007-09-25 01:57:46 +00009209 // X86ISD::CMOV means set the result (which is operand 1) to the RHS if
9210 // condition is true.
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00009211 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::Glue);
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00009212 SDValue Ops[] = { Op2, Op1, CC, Cond };
Chris Lattnera2b56002010-12-05 01:23:24 +00009213 return DAG.getNode(X86ISD::CMOV, DL, VTs, Ops, array_lengthof(Ops));
Evan Cheng0488db92007-09-25 01:57:46 +00009214}
9215
Evan Cheng370e5342008-12-03 08:38:43 +00009216// isAndOrOfSingleUseSetCCs - Return true if node is an ISD::AND or
9217// ISD::OR of two X86ISD::SETCC nodes each of which has no other use apart
9218// from the AND / OR.
9219static bool isAndOrOfSetCCs(SDValue Op, unsigned &Opc) {
9220 Opc = Op.getOpcode();
9221 if (Opc != ISD::OR && Opc != ISD::AND)
9222 return false;
9223 return (Op.getOperand(0).getOpcode() == X86ISD::SETCC &&
9224 Op.getOperand(0).hasOneUse() &&
9225 Op.getOperand(1).getOpcode() == X86ISD::SETCC &&
9226 Op.getOperand(1).hasOneUse());
9227}
9228
Evan Cheng961d6d42009-02-02 08:19:07 +00009229// isXor1OfSetCC - Return true if node is an ISD::XOR of a X86ISD::SETCC and
9230// 1 and that the SETCC node has a single use.
Evan Cheng67ad9db2009-02-02 08:07:36 +00009231static bool isXor1OfSetCC(SDValue Op) {
9232 if (Op.getOpcode() != ISD::XOR)
9233 return false;
9234 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
9235 if (N1C && N1C->getAPIntValue() == 1) {
9236 return Op.getOperand(0).getOpcode() == X86ISD::SETCC &&
9237 Op.getOperand(0).hasOneUse();
9238 }
9239 return false;
9240}
9241
Dan Gohmand858e902010-04-17 15:26:15 +00009242SDValue X86TargetLowering::LowerBRCOND(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng734503b2006-09-11 02:19:56 +00009243 bool addTest = true;
Dan Gohman475871a2008-07-27 21:46:04 +00009244 SDValue Chain = Op.getOperand(0);
9245 SDValue Cond = Op.getOperand(1);
9246 SDValue Dest = Op.getOperand(2);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009247 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00009248 SDValue CC;
Dan Gohman65fd6562011-11-03 21:49:52 +00009249 bool Inverted = false;
Evan Cheng734503b2006-09-11 02:19:56 +00009250
Dan Gohman1a492952009-10-20 16:22:37 +00009251 if (Cond.getOpcode() == ISD::SETCC) {
Dan Gohman65fd6562011-11-03 21:49:52 +00009252 // Check for setcc([su]{add,sub,mul}o == 0).
9253 if (cast<CondCodeSDNode>(Cond.getOperand(2))->get() == ISD::SETEQ &&
9254 isa<ConstantSDNode>(Cond.getOperand(1)) &&
9255 cast<ConstantSDNode>(Cond.getOperand(1))->isNullValue() &&
9256 Cond.getOperand(0).getResNo() == 1 &&
9257 (Cond.getOperand(0).getOpcode() == ISD::SADDO ||
9258 Cond.getOperand(0).getOpcode() == ISD::UADDO ||
9259 Cond.getOperand(0).getOpcode() == ISD::SSUBO ||
9260 Cond.getOperand(0).getOpcode() == ISD::USUBO ||
9261 Cond.getOperand(0).getOpcode() == ISD::SMULO ||
9262 Cond.getOperand(0).getOpcode() == ISD::UMULO)) {
9263 Inverted = true;
9264 Cond = Cond.getOperand(0);
9265 } else {
9266 SDValue NewCond = LowerSETCC(Cond, DAG);
9267 if (NewCond.getNode())
9268 Cond = NewCond;
9269 }
Dan Gohman1a492952009-10-20 16:22:37 +00009270 }
Chris Lattnere55484e2008-12-25 05:34:37 +00009271#if 0
9272 // FIXME: LowerXALUO doesn't handle these!!
Bill Wendlingd350e022008-12-12 21:15:41 +00009273 else if (Cond.getOpcode() == X86ISD::ADD ||
9274 Cond.getOpcode() == X86ISD::SUB ||
9275 Cond.getOpcode() == X86ISD::SMUL ||
9276 Cond.getOpcode() == X86ISD::UMUL)
Bill Wendling74c37652008-12-09 22:08:41 +00009277 Cond = LowerXALUO(Cond, DAG);
Chris Lattnere55484e2008-12-25 05:34:37 +00009278#endif
Scott Michelfdc40a02009-02-17 22:15:04 +00009279
Evan Chengad9c0a32009-12-15 00:53:42 +00009280 // Look pass (and (setcc_carry (cmp ...)), 1).
9281 if (Cond.getOpcode() == ISD::AND &&
9282 Cond.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY) {
9283 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Cond.getOperand(1));
Michael J. Spencerec38de22010-10-10 22:04:20 +00009284 if (C && C->getAPIntValue() == 1)
Evan Chengad9c0a32009-12-15 00:53:42 +00009285 Cond = Cond.getOperand(0);
9286 }
9287
Evan Cheng3f41d662007-10-08 22:16:29 +00009288 // If condition flag is set by a X86ISD::CMP, then use it as the condition
9289 // setting operand in place of the X86ISD::SETCC.
Dan Gohman65fd6562011-11-03 21:49:52 +00009290 unsigned CondOpcode = Cond.getOpcode();
9291 if (CondOpcode == X86ISD::SETCC ||
9292 CondOpcode == X86ISD::SETCC_CARRY) {
Evan Cheng734503b2006-09-11 02:19:56 +00009293 CC = Cond.getOperand(0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00009294
Dan Gohman475871a2008-07-27 21:46:04 +00009295 SDValue Cmp = Cond.getOperand(1);
Evan Cheng734503b2006-09-11 02:19:56 +00009296 unsigned Opc = Cmp.getOpcode();
Chris Lattnere55484e2008-12-25 05:34:37 +00009297 // FIXME: WHY THE SPECIAL CASING OF LogicalCmp??
Dan Gohman076aee32009-03-04 19:44:21 +00009298 if (isX86LogicalCmp(Cmp) || Opc == X86ISD::BT) {
Evan Cheng3f41d662007-10-08 22:16:29 +00009299 Cond = Cmp;
Evan Cheng0488db92007-09-25 01:57:46 +00009300 addTest = false;
Bill Wendling61edeb52008-12-02 01:06:39 +00009301 } else {
Evan Cheng370e5342008-12-03 08:38:43 +00009302 switch (cast<ConstantSDNode>(CC)->getZExtValue()) {
Bill Wendling0ea25cb2008-12-03 08:32:02 +00009303 default: break;
9304 case X86::COND_O:
Dan Gohman653456c2009-01-07 00:15:08 +00009305 case X86::COND_B:
Chris Lattnere55484e2008-12-25 05:34:37 +00009306 // These can only come from an arithmetic instruction with overflow,
9307 // e.g. SADDO, UADDO.
Bill Wendling0ea25cb2008-12-03 08:32:02 +00009308 Cond = Cond.getNode()->getOperand(1);
9309 addTest = false;
9310 break;
Bill Wendling61edeb52008-12-02 01:06:39 +00009311 }
Evan Cheng0488db92007-09-25 01:57:46 +00009312 }
Dan Gohman65fd6562011-11-03 21:49:52 +00009313 }
9314 CondOpcode = Cond.getOpcode();
9315 if (CondOpcode == ISD::UADDO || CondOpcode == ISD::SADDO ||
9316 CondOpcode == ISD::USUBO || CondOpcode == ISD::SSUBO ||
9317 ((CondOpcode == ISD::UMULO || CondOpcode == ISD::SMULO) &&
9318 Cond.getOperand(0).getValueType() != MVT::i8)) {
9319 SDValue LHS = Cond.getOperand(0);
9320 SDValue RHS = Cond.getOperand(1);
9321 unsigned X86Opcode;
9322 unsigned X86Cond;
9323 SDVTList VTs;
9324 switch (CondOpcode) {
9325 case ISD::UADDO: X86Opcode = X86ISD::ADD; X86Cond = X86::COND_B; break;
9326 case ISD::SADDO: X86Opcode = X86ISD::ADD; X86Cond = X86::COND_O; break;
9327 case ISD::USUBO: X86Opcode = X86ISD::SUB; X86Cond = X86::COND_B; break;
9328 case ISD::SSUBO: X86Opcode = X86ISD::SUB; X86Cond = X86::COND_O; break;
9329 case ISD::UMULO: X86Opcode = X86ISD::UMUL; X86Cond = X86::COND_O; break;
9330 case ISD::SMULO: X86Opcode = X86ISD::SMUL; X86Cond = X86::COND_O; break;
9331 default: llvm_unreachable("unexpected overflowing operator");
9332 }
9333 if (Inverted)
9334 X86Cond = X86::GetOppositeBranchCondition((X86::CondCode)X86Cond);
9335 if (CondOpcode == ISD::UMULO)
9336 VTs = DAG.getVTList(LHS.getValueType(), LHS.getValueType(),
9337 MVT::i32);
9338 else
9339 VTs = DAG.getVTList(LHS.getValueType(), MVT::i32);
9340
9341 SDValue X86Op = DAG.getNode(X86Opcode, dl, VTs, LHS, RHS);
9342
9343 if (CondOpcode == ISD::UMULO)
9344 Cond = X86Op.getValue(2);
9345 else
9346 Cond = X86Op.getValue(1);
9347
9348 CC = DAG.getConstant(X86Cond, MVT::i8);
9349 addTest = false;
Evan Cheng370e5342008-12-03 08:38:43 +00009350 } else {
9351 unsigned CondOpc;
9352 if (Cond.hasOneUse() && isAndOrOfSetCCs(Cond, CondOpc)) {
9353 SDValue Cmp = Cond.getOperand(0).getOperand(1);
Evan Cheng370e5342008-12-03 08:38:43 +00009354 if (CondOpc == ISD::OR) {
9355 // Also, recognize the pattern generated by an FCMP_UNE. We can emit
9356 // two branches instead of an explicit OR instruction with a
9357 // separate test.
9358 if (Cmp == Cond.getOperand(1).getOperand(1) &&
Dan Gohman076aee32009-03-04 19:44:21 +00009359 isX86LogicalCmp(Cmp)) {
Evan Cheng370e5342008-12-03 08:38:43 +00009360 CC = Cond.getOperand(0).getOperand(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009361 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Evan Cheng370e5342008-12-03 08:38:43 +00009362 Chain, Dest, CC, Cmp);
9363 CC = Cond.getOperand(1).getOperand(0);
9364 Cond = Cmp;
9365 addTest = false;
9366 }
9367 } else { // ISD::AND
9368 // Also, recognize the pattern generated by an FCMP_OEQ. We can emit
9369 // two branches instead of an explicit AND instruction with a
9370 // separate test. However, we only do this if this block doesn't
9371 // have a fall-through edge, because this requires an explicit
9372 // jmp when the condition is false.
9373 if (Cmp == Cond.getOperand(1).getOperand(1) &&
Dan Gohman076aee32009-03-04 19:44:21 +00009374 isX86LogicalCmp(Cmp) &&
Evan Cheng370e5342008-12-03 08:38:43 +00009375 Op.getNode()->hasOneUse()) {
9376 X86::CondCode CCode =
9377 (X86::CondCode)Cond.getOperand(0).getConstantOperandVal(0);
9378 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00009379 CC = DAG.getConstant(CCode, MVT::i8);
Dan Gohman027657d2010-06-18 15:30:29 +00009380 SDNode *User = *Op.getNode()->use_begin();
Evan Cheng370e5342008-12-03 08:38:43 +00009381 // Look for an unconditional branch following this conditional branch.
9382 // We need this because we need to reverse the successors in order
9383 // to implement FCMP_OEQ.
Dan Gohman027657d2010-06-18 15:30:29 +00009384 if (User->getOpcode() == ISD::BR) {
9385 SDValue FalseBB = User->getOperand(1);
9386 SDNode *NewBR =
9387 DAG.UpdateNodeOperands(User, User->getOperand(0), Dest);
Evan Cheng370e5342008-12-03 08:38:43 +00009388 assert(NewBR == User);
Nick Lewycky2a3ee5e2010-06-20 20:27:42 +00009389 (void)NewBR;
Evan Cheng370e5342008-12-03 08:38:43 +00009390 Dest = FalseBB;
Dan Gohman279c22e2008-10-21 03:29:32 +00009391
Dale Johannesene4d209d2009-02-03 20:21:25 +00009392 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Evan Cheng370e5342008-12-03 08:38:43 +00009393 Chain, Dest, CC, Cmp);
9394 X86::CondCode CCode =
9395 (X86::CondCode)Cond.getOperand(1).getConstantOperandVal(0);
9396 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00009397 CC = DAG.getConstant(CCode, MVT::i8);
Evan Cheng370e5342008-12-03 08:38:43 +00009398 Cond = Cmp;
9399 addTest = false;
9400 }
9401 }
Dan Gohman279c22e2008-10-21 03:29:32 +00009402 }
Evan Cheng67ad9db2009-02-02 08:07:36 +00009403 } else if (Cond.hasOneUse() && isXor1OfSetCC(Cond)) {
9404 // Recognize for xorb (setcc), 1 patterns. The xor inverts the condition.
9405 // It should be transformed during dag combiner except when the condition
9406 // is set by a arithmetics with overflow node.
9407 X86::CondCode CCode =
9408 (X86::CondCode)Cond.getOperand(0).getConstantOperandVal(0);
9409 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00009410 CC = DAG.getConstant(CCode, MVT::i8);
Evan Cheng67ad9db2009-02-02 08:07:36 +00009411 Cond = Cond.getOperand(0).getOperand(1);
9412 addTest = false;
Dan Gohman65fd6562011-11-03 21:49:52 +00009413 } else if (Cond.getOpcode() == ISD::SETCC &&
9414 cast<CondCodeSDNode>(Cond.getOperand(2))->get() == ISD::SETOEQ) {
9415 // For FCMP_OEQ, we can emit
9416 // two branches instead of an explicit AND instruction with a
9417 // separate test. However, we only do this if this block doesn't
9418 // have a fall-through edge, because this requires an explicit
9419 // jmp when the condition is false.
9420 if (Op.getNode()->hasOneUse()) {
9421 SDNode *User = *Op.getNode()->use_begin();
9422 // Look for an unconditional branch following this conditional branch.
9423 // We need this because we need to reverse the successors in order
9424 // to implement FCMP_OEQ.
9425 if (User->getOpcode() == ISD::BR) {
9426 SDValue FalseBB = User->getOperand(1);
9427 SDNode *NewBR =
9428 DAG.UpdateNodeOperands(User, User->getOperand(0), Dest);
9429 assert(NewBR == User);
9430 (void)NewBR;
9431 Dest = FalseBB;
9432
9433 SDValue Cmp = DAG.getNode(X86ISD::CMP, dl, MVT::i32,
9434 Cond.getOperand(0), Cond.getOperand(1));
Benjamin Kramer17c836c2012-04-27 12:07:43 +00009435 Cmp = ConvertCmpIfNecessary(Cmp, DAG);
Dan Gohman65fd6562011-11-03 21:49:52 +00009436 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
9437 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
9438 Chain, Dest, CC, Cmp);
9439 CC = DAG.getConstant(X86::COND_P, MVT::i8);
9440 Cond = Cmp;
9441 addTest = false;
9442 }
9443 }
9444 } else if (Cond.getOpcode() == ISD::SETCC &&
9445 cast<CondCodeSDNode>(Cond.getOperand(2))->get() == ISD::SETUNE) {
9446 // For FCMP_UNE, we can emit
9447 // two branches instead of an explicit AND instruction with a
9448 // separate test. However, we only do this if this block doesn't
9449 // have a fall-through edge, because this requires an explicit
9450 // jmp when the condition is false.
9451 if (Op.getNode()->hasOneUse()) {
9452 SDNode *User = *Op.getNode()->use_begin();
9453 // Look for an unconditional branch following this conditional branch.
9454 // We need this because we need to reverse the successors in order
9455 // to implement FCMP_UNE.
9456 if (User->getOpcode() == ISD::BR) {
9457 SDValue FalseBB = User->getOperand(1);
9458 SDNode *NewBR =
9459 DAG.UpdateNodeOperands(User, User->getOperand(0), Dest);
9460 assert(NewBR == User);
9461 (void)NewBR;
9462
9463 SDValue Cmp = DAG.getNode(X86ISD::CMP, dl, MVT::i32,
9464 Cond.getOperand(0), Cond.getOperand(1));
Benjamin Kramer17c836c2012-04-27 12:07:43 +00009465 Cmp = ConvertCmpIfNecessary(Cmp, DAG);
Dan Gohman65fd6562011-11-03 21:49:52 +00009466 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
9467 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
9468 Chain, Dest, CC, Cmp);
9469 CC = DAG.getConstant(X86::COND_NP, MVT::i8);
9470 Cond = Cmp;
9471 addTest = false;
9472 Dest = FalseBB;
9473 }
9474 }
Dan Gohman279c22e2008-10-21 03:29:32 +00009475 }
Evan Cheng0488db92007-09-25 01:57:46 +00009476 }
9477
9478 if (addTest) {
Evan Chengb64dd5f2012-08-07 22:21:00 +00009479 // Look pass the truncate if the high bits are known zero.
9480 if (isTruncWithZeroHighBitsInput(Cond, DAG))
9481 Cond = Cond.getOperand(0);
Evan Chengd40d03e2010-01-06 19:38:29 +00009482
9483 // We know the result of AND is compared against zero. Try to match
9484 // it to BT.
Michael J. Spencerec38de22010-10-10 22:04:20 +00009485 if (Cond.getOpcode() == ISD::AND && Cond.hasOneUse()) {
Evan Chengd40d03e2010-01-06 19:38:29 +00009486 SDValue NewSetCC = LowerToBT(Cond, ISD::SETNE, dl, DAG);
9487 if (NewSetCC.getNode()) {
9488 CC = NewSetCC.getOperand(0);
9489 Cond = NewSetCC.getOperand(1);
9490 addTest = false;
9491 }
9492 }
9493 }
9494
9495 if (addTest) {
Owen Anderson825b72b2009-08-11 20:47:22 +00009496 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng552f09a2010-04-26 19:06:11 +00009497 Cond = EmitTest(Cond, X86::COND_NE, DAG);
Evan Cheng0488db92007-09-25 01:57:46 +00009498 }
Benjamin Kramer17c836c2012-04-27 12:07:43 +00009499 Cond = ConvertCmpIfNecessary(Cond, DAG);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009500 return DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Dan Gohman279c22e2008-10-21 03:29:32 +00009501 Chain, Dest, CC, Cond);
Evan Cheng0488db92007-09-25 01:57:46 +00009502}
9503
Anton Korobeynikove060b532007-04-17 19:34:00 +00009504
9505// Lower dynamic stack allocation to _alloca call for Cygwin/Mingw targets.
9506// Calls to _alloca is needed to probe the stack when allocating more than 4k
9507// bytes in one go. Touching the stack at 4K increments is necessary to ensure
9508// that the guard pages used by the OS virtual memory manager are allocated in
9509// correct sequence.
Dan Gohman475871a2008-07-27 21:46:04 +00009510SDValue
9511X86TargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00009512 SelectionDAG &DAG) const {
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009513 assert((Subtarget->isTargetCygMing() || Subtarget->isTargetWindows() ||
Nick Lewycky8a8d4792011-12-02 22:16:29 +00009514 getTargetMachine().Options.EnableSegmentedStacks) &&
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009515 "This should be used only on Windows targets or when segmented stacks "
Rafael Espindola96428ce2011-09-06 18:43:08 +00009516 "are being used");
9517 assert(!Subtarget->isTargetEnvMacho() && "Not implemented");
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009518 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov096b4612008-06-11 20:16:42 +00009519
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00009520 // Get the inputs.
Dan Gohman475871a2008-07-27 21:46:04 +00009521 SDValue Chain = Op.getOperand(0);
9522 SDValue Size = Op.getOperand(1);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00009523 // FIXME: Ensure alignment here
9524
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009525 bool Is64Bit = Subtarget->is64Bit();
9526 EVT SPTy = Is64Bit ? MVT::i64 : MVT::i32;
Anton Korobeynikov096b4612008-06-11 20:16:42 +00009527
Nick Lewycky8a8d4792011-12-02 22:16:29 +00009528 if (getTargetMachine().Options.EnableSegmentedStacks) {
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009529 MachineFunction &MF = DAG.getMachineFunction();
9530 MachineRegisterInfo &MRI = MF.getRegInfo();
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00009531
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009532 if (Is64Bit) {
9533 // The 64 bit implementation of segmented stacks needs to clobber both r10
Rafael Espindola96428ce2011-09-06 18:43:08 +00009534 // r11. This makes it impossible to use it along with nested parameters.
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009535 const Function *F = MF.getFunction();
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00009536
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009537 for (Function::const_arg_iterator I = F->arg_begin(), E = F->arg_end();
Craig Topper31a207a2012-05-04 06:39:13 +00009538 I != E; ++I)
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009539 if (I->hasNestAttr())
9540 report_fatal_error("Cannot use segmented stacks with functions that "
9541 "have nested arguments.");
9542 }
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00009543
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009544 const TargetRegisterClass *AddrRegClass =
9545 getRegClassFor(Subtarget->is64Bit() ? MVT::i64:MVT::i32);
9546 unsigned Vreg = MRI.createVirtualRegister(AddrRegClass);
9547 Chain = DAG.getCopyToReg(Chain, dl, Vreg, Size);
9548 SDValue Value = DAG.getNode(X86ISD::SEG_ALLOCA, dl, SPTy, Chain,
9549 DAG.getRegister(Vreg, SPTy));
9550 SDValue Ops1[2] = { Value, Chain };
9551 return DAG.getMergeValues(Ops1, 2, dl);
9552 } else {
9553 SDValue Flag;
9554 unsigned Reg = (Subtarget->is64Bit() ? X86::RAX : X86::EAX);
Anton Korobeynikov096b4612008-06-11 20:16:42 +00009555
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009556 Chain = DAG.getCopyToReg(Chain, dl, Reg, Size, Flag);
9557 Flag = Chain.getValue(1);
9558 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Anton Korobeynikov096b4612008-06-11 20:16:42 +00009559
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009560 Chain = DAG.getNode(X86ISD::WIN_ALLOCA, dl, NodeTys, Chain, Flag);
9561 Flag = Chain.getValue(1);
9562
9563 Chain = DAG.getCopyFromReg(Chain, dl, X86StackPtr, SPTy).getValue(1);
9564
9565 SDValue Ops1[2] = { Chain.getValue(0), Chain };
9566 return DAG.getMergeValues(Ops1, 2, dl);
9567 }
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00009568}
9569
Dan Gohmand858e902010-04-17 15:26:15 +00009570SDValue X86TargetLowering::LowerVASTART(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman1e93df62010-04-17 14:41:14 +00009571 MachineFunction &MF = DAG.getMachineFunction();
9572 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
9573
Dan Gohman69de1932008-02-06 22:27:42 +00009574 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Chris Lattner8026a9d2010-09-21 17:50:43 +00009575 DebugLoc DL = Op.getDebugLoc();
Evan Cheng8b2794a2006-10-13 21:14:26 +00009576
Anton Korobeynikove7beda12010-10-03 22:52:07 +00009577 if (!Subtarget->is64Bit() || Subtarget->isTargetWin64()) {
Evan Cheng25ab6902006-09-08 06:48:29 +00009578 // vastart just stores the address of the VarArgsFrameIndex slot into the
9579 // memory location argument.
Dan Gohman1e93df62010-04-17 14:41:14 +00009580 SDValue FR = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
9581 getPointerTy());
Chris Lattner8026a9d2010-09-21 17:50:43 +00009582 return DAG.getStore(Op.getOperand(0), DL, FR, Op.getOperand(1),
9583 MachinePointerInfo(SV), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00009584 }
9585
9586 // __va_list_tag:
9587 // gp_offset (0 - 6 * 8)
9588 // fp_offset (48 - 48 + 8 * 16)
9589 // overflow_arg_area (point to parameters coming in memory).
9590 // reg_save_area
Dan Gohman475871a2008-07-27 21:46:04 +00009591 SmallVector<SDValue, 8> MemOps;
9592 SDValue FIN = Op.getOperand(1);
Evan Cheng25ab6902006-09-08 06:48:29 +00009593 // Store gp_offset
Chris Lattner8026a9d2010-09-21 17:50:43 +00009594 SDValue Store = DAG.getStore(Op.getOperand(0), DL,
Dan Gohman1e93df62010-04-17 14:41:14 +00009595 DAG.getConstant(FuncInfo->getVarArgsGPOffset(),
9596 MVT::i32),
Chris Lattner8026a9d2010-09-21 17:50:43 +00009597 FIN, MachinePointerInfo(SV), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00009598 MemOps.push_back(Store);
9599
9600 // Store fp_offset
Chris Lattner8026a9d2010-09-21 17:50:43 +00009601 FIN = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00009602 FIN, DAG.getIntPtrConstant(4));
Chris Lattner8026a9d2010-09-21 17:50:43 +00009603 Store = DAG.getStore(Op.getOperand(0), DL,
Dan Gohman1e93df62010-04-17 14:41:14 +00009604 DAG.getConstant(FuncInfo->getVarArgsFPOffset(),
9605 MVT::i32),
Chris Lattner8026a9d2010-09-21 17:50:43 +00009606 FIN, MachinePointerInfo(SV, 4), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00009607 MemOps.push_back(Store);
9608
9609 // Store ptr to overflow_arg_area
Chris Lattner8026a9d2010-09-21 17:50:43 +00009610 FIN = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00009611 FIN, DAG.getIntPtrConstant(4));
Dan Gohman1e93df62010-04-17 14:41:14 +00009612 SDValue OVFIN = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
9613 getPointerTy());
Chris Lattner8026a9d2010-09-21 17:50:43 +00009614 Store = DAG.getStore(Op.getOperand(0), DL, OVFIN, FIN,
9615 MachinePointerInfo(SV, 8),
David Greene67c9d422010-02-15 16:53:33 +00009616 false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00009617 MemOps.push_back(Store);
9618
9619 // Store ptr to reg_save_area.
Chris Lattner8026a9d2010-09-21 17:50:43 +00009620 FIN = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00009621 FIN, DAG.getIntPtrConstant(8));
Dan Gohman1e93df62010-04-17 14:41:14 +00009622 SDValue RSFIN = DAG.getFrameIndex(FuncInfo->getRegSaveFrameIndex(),
9623 getPointerTy());
Chris Lattner8026a9d2010-09-21 17:50:43 +00009624 Store = DAG.getStore(Op.getOperand(0), DL, RSFIN, FIN,
9625 MachinePointerInfo(SV, 16), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00009626 MemOps.push_back(Store);
Chris Lattner8026a9d2010-09-21 17:50:43 +00009627 return DAG.getNode(ISD::TokenFactor, DL, MVT::Other,
Dale Johannesene4d209d2009-02-03 20:21:25 +00009628 &MemOps[0], MemOps.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00009629}
9630
Dan Gohmand858e902010-04-17 15:26:15 +00009631SDValue X86TargetLowering::LowerVAARG(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman320afb82010-10-12 18:00:49 +00009632 assert(Subtarget->is64Bit() &&
9633 "LowerVAARG only handles 64-bit va_arg!");
9634 assert((Subtarget->isTargetLinux() ||
9635 Subtarget->isTargetDarwin()) &&
9636 "Unhandled target in LowerVAARG");
9637 assert(Op.getNode()->getNumOperands() == 4);
9638 SDValue Chain = Op.getOperand(0);
9639 SDValue SrcPtr = Op.getOperand(1);
9640 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
9641 unsigned Align = Op.getConstantOperandVal(3);
9642 DebugLoc dl = Op.getDebugLoc();
Dan Gohman9018e832008-05-10 01:26:14 +00009643
Dan Gohman320afb82010-10-12 18:00:49 +00009644 EVT ArgVT = Op.getNode()->getValueType(0);
Chris Lattnerdb125cf2011-07-18 04:54:35 +00009645 Type *ArgTy = ArgVT.getTypeForEVT(*DAG.getContext());
Micah Villmow3574eca2012-10-08 16:38:25 +00009646 uint32_t ArgSize = getDataLayout()->getTypeAllocSize(ArgTy);
Dan Gohman320afb82010-10-12 18:00:49 +00009647 uint8_t ArgMode;
9648
9649 // Decide which area this value should be read from.
9650 // TODO: Implement the AMD64 ABI in its entirety. This simple
9651 // selection mechanism works only for the basic types.
9652 if (ArgVT == MVT::f80) {
9653 llvm_unreachable("va_arg for f80 not yet implemented");
9654 } else if (ArgVT.isFloatingPoint() && ArgSize <= 16 /*bytes*/) {
9655 ArgMode = 2; // Argument passed in XMM register. Use fp_offset.
9656 } else if (ArgVT.isInteger() && ArgSize <= 32 /*bytes*/) {
9657 ArgMode = 1; // Argument passed in GPR64 register(s). Use gp_offset.
9658 } else {
9659 llvm_unreachable("Unhandled argument type in LowerVAARG");
9660 }
9661
9662 if (ArgMode == 2) {
9663 // Sanity Check: Make sure using fp_offset makes sense.
Nick Lewycky8a8d4792011-12-02 22:16:29 +00009664 assert(!getTargetMachine().Options.UseSoftFloat &&
Eric Christopher52b45052010-10-12 19:44:17 +00009665 !(DAG.getMachineFunction()
Bill Wendling67658342012-10-09 07:45:08 +00009666 .getFunction()->getFnAttributes()
9667 .hasAttribute(Attributes::NoImplicitFloat)) &&
Craig Topper1accb7e2012-01-10 06:54:16 +00009668 Subtarget->hasSSE1());
Dan Gohman320afb82010-10-12 18:00:49 +00009669 }
9670
9671 // Insert VAARG_64 node into the DAG
9672 // VAARG_64 returns two values: Variable Argument Address, Chain
9673 SmallVector<SDValue, 11> InstOps;
9674 InstOps.push_back(Chain);
9675 InstOps.push_back(SrcPtr);
9676 InstOps.push_back(DAG.getConstant(ArgSize, MVT::i32));
9677 InstOps.push_back(DAG.getConstant(ArgMode, MVT::i8));
9678 InstOps.push_back(DAG.getConstant(Align, MVT::i32));
9679 SDVTList VTs = DAG.getVTList(getPointerTy(), MVT::Other);
9680 SDValue VAARG = DAG.getMemIntrinsicNode(X86ISD::VAARG_64, dl,
9681 VTs, &InstOps[0], InstOps.size(),
9682 MVT::i64,
9683 MachinePointerInfo(SV),
9684 /*Align=*/0,
9685 /*Volatile=*/false,
9686 /*ReadMem=*/true,
9687 /*WriteMem=*/true);
9688 Chain = VAARG.getValue(1);
9689
9690 // Load the next argument and return it
9691 return DAG.getLoad(ArgVT, dl,
9692 Chain,
9693 VAARG,
9694 MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00009695 false, false, false, 0);
Dan Gohman9018e832008-05-10 01:26:14 +00009696}
9697
Craig Topper55b24052012-09-11 06:15:32 +00009698static SDValue LowerVACOPY(SDValue Op, const X86Subtarget *Subtarget,
9699 SelectionDAG &DAG) {
Evan Chengae642192007-03-02 23:16:35 +00009700 // X86-64 va_list is a struct { i32, i32, i8*, i8* }.
Dan Gohman28269132008-04-18 20:55:41 +00009701 assert(Subtarget->is64Bit() && "This code only handles 64-bit va_copy!");
Dan Gohman475871a2008-07-27 21:46:04 +00009702 SDValue Chain = Op.getOperand(0);
9703 SDValue DstPtr = Op.getOperand(1);
9704 SDValue SrcPtr = Op.getOperand(2);
Dan Gohman69de1932008-02-06 22:27:42 +00009705 const Value *DstSV = cast<SrcValueSDNode>(Op.getOperand(3))->getValue();
9706 const Value *SrcSV = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Chris Lattnere72f2022010-09-21 05:40:29 +00009707 DebugLoc DL = Op.getDebugLoc();
Evan Chengae642192007-03-02 23:16:35 +00009708
Chris Lattnere72f2022010-09-21 05:40:29 +00009709 return DAG.getMemcpy(Chain, DL, DstPtr, SrcPtr,
Mon P Wang20adc9d2010-04-04 03:10:48 +00009710 DAG.getIntPtrConstant(24), 8, /*isVolatile*/false,
Michael J. Spencerec38de22010-10-10 22:04:20 +00009711 false,
Chris Lattnere72f2022010-09-21 05:40:29 +00009712 MachinePointerInfo(DstSV), MachinePointerInfo(SrcSV));
Evan Chengae642192007-03-02 23:16:35 +00009713}
9714
Craig Topper80e46362012-01-23 06:16:53 +00009715// getTargetVShiftNOde - Handle vector element shifts where the shift amount
9716// may or may not be a constant. Takes immediate version of shift as input.
9717static SDValue getTargetVShiftNode(unsigned Opc, DebugLoc dl, EVT VT,
9718 SDValue SrcOp, SDValue ShAmt,
9719 SelectionDAG &DAG) {
9720 assert(ShAmt.getValueType() == MVT::i32 && "ShAmt is not i32");
9721
9722 if (isa<ConstantSDNode>(ShAmt)) {
Nadav Rotemd896e242012-07-15 20:27:43 +00009723 // Constant may be a TargetConstant. Use a regular constant.
9724 uint32_t ShiftAmt = cast<ConstantSDNode>(ShAmt)->getZExtValue();
Craig Topper80e46362012-01-23 06:16:53 +00009725 switch (Opc) {
9726 default: llvm_unreachable("Unknown target vector shift node");
9727 case X86ISD::VSHLI:
9728 case X86ISD::VSRLI:
9729 case X86ISD::VSRAI:
Nadav Rotemd896e242012-07-15 20:27:43 +00009730 return DAG.getNode(Opc, dl, VT, SrcOp,
9731 DAG.getConstant(ShiftAmt, MVT::i32));
Craig Topper80e46362012-01-23 06:16:53 +00009732 }
9733 }
9734
9735 // Change opcode to non-immediate version
9736 switch (Opc) {
9737 default: llvm_unreachable("Unknown target vector shift node");
9738 case X86ISD::VSHLI: Opc = X86ISD::VSHL; break;
9739 case X86ISD::VSRLI: Opc = X86ISD::VSRL; break;
9740 case X86ISD::VSRAI: Opc = X86ISD::VSRA; break;
9741 }
9742
9743 // Need to build a vector containing shift amount
9744 // Shift amount is 32-bits, but SSE instructions read 64-bit, so fill with 0
9745 SDValue ShOps[4];
9746 ShOps[0] = ShAmt;
9747 ShOps[1] = DAG.getConstant(0, MVT::i32);
Craig Topper6d688152012-08-14 07:43:25 +00009748 ShOps[2] = ShOps[3] = DAG.getUNDEF(MVT::i32);
Craig Topper80e46362012-01-23 06:16:53 +00009749 ShAmt = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, &ShOps[0], 4);
Nadav Rotem65f489f2012-07-14 22:26:05 +00009750
9751 // The return type has to be a 128-bit type with the same element
9752 // type as the input type.
9753 MVT EltVT = VT.getVectorElementType().getSimpleVT();
9754 EVT ShVT = MVT::getVectorVT(EltVT, 128/EltVT.getSizeInBits());
9755
9756 ShAmt = DAG.getNode(ISD::BITCAST, dl, ShVT, ShAmt);
Craig Topper80e46362012-01-23 06:16:53 +00009757 return DAG.getNode(Opc, dl, VT, SrcOp, ShAmt);
9758}
9759
Craig Topper55b24052012-09-11 06:15:32 +00009760static SDValue LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG) {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009761 DebugLoc dl = Op.getDebugLoc();
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00009762 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00009763 switch (IntNo) {
Dan Gohman475871a2008-07-27 21:46:04 +00009764 default: return SDValue(); // Don't custom lower most intrinsics.
Evan Cheng5759f972008-05-04 09:15:50 +00009765 // Comparison intrinsics.
Evan Cheng0db9fe62006-04-25 20:13:52 +00009766 case Intrinsic::x86_sse_comieq_ss:
9767 case Intrinsic::x86_sse_comilt_ss:
9768 case Intrinsic::x86_sse_comile_ss:
9769 case Intrinsic::x86_sse_comigt_ss:
9770 case Intrinsic::x86_sse_comige_ss:
9771 case Intrinsic::x86_sse_comineq_ss:
9772 case Intrinsic::x86_sse_ucomieq_ss:
9773 case Intrinsic::x86_sse_ucomilt_ss:
9774 case Intrinsic::x86_sse_ucomile_ss:
9775 case Intrinsic::x86_sse_ucomigt_ss:
9776 case Intrinsic::x86_sse_ucomige_ss:
9777 case Intrinsic::x86_sse_ucomineq_ss:
9778 case Intrinsic::x86_sse2_comieq_sd:
9779 case Intrinsic::x86_sse2_comilt_sd:
9780 case Intrinsic::x86_sse2_comile_sd:
9781 case Intrinsic::x86_sse2_comigt_sd:
9782 case Intrinsic::x86_sse2_comige_sd:
9783 case Intrinsic::x86_sse2_comineq_sd:
9784 case Intrinsic::x86_sse2_ucomieq_sd:
9785 case Intrinsic::x86_sse2_ucomilt_sd:
9786 case Intrinsic::x86_sse2_ucomile_sd:
9787 case Intrinsic::x86_sse2_ucomigt_sd:
9788 case Intrinsic::x86_sse2_ucomige_sd:
9789 case Intrinsic::x86_sse2_ucomineq_sd: {
Craig Topper6d688152012-08-14 07:43:25 +00009790 unsigned Opc;
9791 ISD::CondCode CC;
Evan Cheng0db9fe62006-04-25 20:13:52 +00009792 switch (IntNo) {
Craig Topper86c7c582012-01-30 01:10:15 +00009793 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00009794 case Intrinsic::x86_sse_comieq_ss:
9795 case Intrinsic::x86_sse2_comieq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009796 Opc = X86ISD::COMI;
9797 CC = ISD::SETEQ;
9798 break;
Evan Cheng6be2c582006-04-05 23:38:46 +00009799 case Intrinsic::x86_sse_comilt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009800 case Intrinsic::x86_sse2_comilt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009801 Opc = X86ISD::COMI;
9802 CC = ISD::SETLT;
9803 break;
9804 case Intrinsic::x86_sse_comile_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009805 case Intrinsic::x86_sse2_comile_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009806 Opc = X86ISD::COMI;
9807 CC = ISD::SETLE;
9808 break;
9809 case Intrinsic::x86_sse_comigt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009810 case Intrinsic::x86_sse2_comigt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009811 Opc = X86ISD::COMI;
9812 CC = ISD::SETGT;
9813 break;
9814 case Intrinsic::x86_sse_comige_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009815 case Intrinsic::x86_sse2_comige_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009816 Opc = X86ISD::COMI;
9817 CC = ISD::SETGE;
9818 break;
9819 case Intrinsic::x86_sse_comineq_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009820 case Intrinsic::x86_sse2_comineq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009821 Opc = X86ISD::COMI;
9822 CC = ISD::SETNE;
9823 break;
9824 case Intrinsic::x86_sse_ucomieq_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009825 case Intrinsic::x86_sse2_ucomieq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009826 Opc = X86ISD::UCOMI;
9827 CC = ISD::SETEQ;
9828 break;
9829 case Intrinsic::x86_sse_ucomilt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009830 case Intrinsic::x86_sse2_ucomilt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009831 Opc = X86ISD::UCOMI;
9832 CC = ISD::SETLT;
9833 break;
9834 case Intrinsic::x86_sse_ucomile_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009835 case Intrinsic::x86_sse2_ucomile_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009836 Opc = X86ISD::UCOMI;
9837 CC = ISD::SETLE;
9838 break;
9839 case Intrinsic::x86_sse_ucomigt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009840 case Intrinsic::x86_sse2_ucomigt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009841 Opc = X86ISD::UCOMI;
9842 CC = ISD::SETGT;
9843 break;
9844 case Intrinsic::x86_sse_ucomige_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009845 case Intrinsic::x86_sse2_ucomige_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009846 Opc = X86ISD::UCOMI;
9847 CC = ISD::SETGE;
9848 break;
9849 case Intrinsic::x86_sse_ucomineq_ss:
9850 case Intrinsic::x86_sse2_ucomineq_sd:
9851 Opc = X86ISD::UCOMI;
9852 CC = ISD::SETNE;
9853 break;
Evan Cheng6be2c582006-04-05 23:38:46 +00009854 }
Evan Cheng734503b2006-09-11 02:19:56 +00009855
Dan Gohman475871a2008-07-27 21:46:04 +00009856 SDValue LHS = Op.getOperand(1);
9857 SDValue RHS = Op.getOperand(2);
Chris Lattner1c39d4c2008-12-24 23:53:05 +00009858 unsigned X86CC = TranslateX86CC(CC, true, LHS, RHS, DAG);
Dan Gohman1a492952009-10-20 16:22:37 +00009859 assert(X86CC != X86::COND_INVALID && "Unexpected illegal condition!");
Owen Anderson825b72b2009-08-11 20:47:22 +00009860 SDValue Cond = DAG.getNode(Opc, dl, MVT::i32, LHS, RHS);
9861 SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
9862 DAG.getConstant(X86CC, MVT::i8), Cond);
9863 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC);
Evan Cheng6be2c582006-04-05 23:38:46 +00009864 }
Craig Topper6d688152012-08-14 07:43:25 +00009865
Duncan Sands04aa4ae2011-09-23 16:10:22 +00009866 // Arithmetic intrinsics.
Craig Topper5b209e82012-02-05 03:14:49 +00009867 case Intrinsic::x86_sse2_pmulu_dq:
9868 case Intrinsic::x86_avx2_pmulu_dq:
9869 return DAG.getNode(X86ISD::PMULUDQ, dl, Op.getValueType(),
9870 Op.getOperand(1), Op.getOperand(2));
Craig Topper6d688152012-08-14 07:43:25 +00009871
9872 // SSE3/AVX horizontal add/sub intrinsics
Duncan Sands04aa4ae2011-09-23 16:10:22 +00009873 case Intrinsic::x86_sse3_hadd_ps:
9874 case Intrinsic::x86_sse3_hadd_pd:
9875 case Intrinsic::x86_avx_hadd_ps_256:
9876 case Intrinsic::x86_avx_hadd_pd_256:
Duncan Sands04aa4ae2011-09-23 16:10:22 +00009877 case Intrinsic::x86_sse3_hsub_ps:
9878 case Intrinsic::x86_sse3_hsub_pd:
9879 case Intrinsic::x86_avx_hsub_ps_256:
9880 case Intrinsic::x86_avx_hsub_pd_256:
Craig Topper4bb3f342012-01-25 05:37:32 +00009881 case Intrinsic::x86_ssse3_phadd_w_128:
9882 case Intrinsic::x86_ssse3_phadd_d_128:
9883 case Intrinsic::x86_avx2_phadd_w:
9884 case Intrinsic::x86_avx2_phadd_d:
Craig Topper4bb3f342012-01-25 05:37:32 +00009885 case Intrinsic::x86_ssse3_phsub_w_128:
9886 case Intrinsic::x86_ssse3_phsub_d_128:
9887 case Intrinsic::x86_avx2_phsub_w:
Craig Topper6d688152012-08-14 07:43:25 +00009888 case Intrinsic::x86_avx2_phsub_d: {
9889 unsigned Opcode;
9890 switch (IntNo) {
9891 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
9892 case Intrinsic::x86_sse3_hadd_ps:
9893 case Intrinsic::x86_sse3_hadd_pd:
9894 case Intrinsic::x86_avx_hadd_ps_256:
9895 case Intrinsic::x86_avx_hadd_pd_256:
9896 Opcode = X86ISD::FHADD;
9897 break;
9898 case Intrinsic::x86_sse3_hsub_ps:
9899 case Intrinsic::x86_sse3_hsub_pd:
9900 case Intrinsic::x86_avx_hsub_ps_256:
9901 case Intrinsic::x86_avx_hsub_pd_256:
9902 Opcode = X86ISD::FHSUB;
9903 break;
9904 case Intrinsic::x86_ssse3_phadd_w_128:
9905 case Intrinsic::x86_ssse3_phadd_d_128:
9906 case Intrinsic::x86_avx2_phadd_w:
9907 case Intrinsic::x86_avx2_phadd_d:
9908 Opcode = X86ISD::HADD;
9909 break;
9910 case Intrinsic::x86_ssse3_phsub_w_128:
9911 case Intrinsic::x86_ssse3_phsub_d_128:
9912 case Intrinsic::x86_avx2_phsub_w:
9913 case Intrinsic::x86_avx2_phsub_d:
9914 Opcode = X86ISD::HSUB;
9915 break;
9916 }
9917 return DAG.getNode(Opcode, dl, Op.getValueType(),
Craig Topper4bb3f342012-01-25 05:37:32 +00009918 Op.getOperand(1), Op.getOperand(2));
Craig Topper6d688152012-08-14 07:43:25 +00009919 }
9920
9921 // AVX2 variable shift intrinsics
Craig Topper98fc7292011-11-19 17:46:46 +00009922 case Intrinsic::x86_avx2_psllv_d:
9923 case Intrinsic::x86_avx2_psllv_q:
9924 case Intrinsic::x86_avx2_psllv_d_256:
9925 case Intrinsic::x86_avx2_psllv_q_256:
Craig Topper98fc7292011-11-19 17:46:46 +00009926 case Intrinsic::x86_avx2_psrlv_d:
9927 case Intrinsic::x86_avx2_psrlv_q:
9928 case Intrinsic::x86_avx2_psrlv_d_256:
9929 case Intrinsic::x86_avx2_psrlv_q_256:
Craig Topper98fc7292011-11-19 17:46:46 +00009930 case Intrinsic::x86_avx2_psrav_d:
Craig Topper6d688152012-08-14 07:43:25 +00009931 case Intrinsic::x86_avx2_psrav_d_256: {
9932 unsigned Opcode;
9933 switch (IntNo) {
9934 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
9935 case Intrinsic::x86_avx2_psllv_d:
9936 case Intrinsic::x86_avx2_psllv_q:
9937 case Intrinsic::x86_avx2_psllv_d_256:
9938 case Intrinsic::x86_avx2_psllv_q_256:
9939 Opcode = ISD::SHL;
9940 break;
9941 case Intrinsic::x86_avx2_psrlv_d:
9942 case Intrinsic::x86_avx2_psrlv_q:
9943 case Intrinsic::x86_avx2_psrlv_d_256:
9944 case Intrinsic::x86_avx2_psrlv_q_256:
9945 Opcode = ISD::SRL;
9946 break;
9947 case Intrinsic::x86_avx2_psrav_d:
9948 case Intrinsic::x86_avx2_psrav_d_256:
9949 Opcode = ISD::SRA;
9950 break;
9951 }
9952 return DAG.getNode(Opcode, dl, Op.getValueType(),
9953 Op.getOperand(1), Op.getOperand(2));
9954 }
9955
Craig Topper969ba282012-01-25 06:43:11 +00009956 case Intrinsic::x86_ssse3_pshuf_b_128:
9957 case Intrinsic::x86_avx2_pshuf_b:
9958 return DAG.getNode(X86ISD::PSHUFB, dl, Op.getValueType(),
9959 Op.getOperand(1), Op.getOperand(2));
Craig Topper6d688152012-08-14 07:43:25 +00009960
Craig Topper969ba282012-01-25 06:43:11 +00009961 case Intrinsic::x86_ssse3_psign_b_128:
9962 case Intrinsic::x86_ssse3_psign_w_128:
9963 case Intrinsic::x86_ssse3_psign_d_128:
9964 case Intrinsic::x86_avx2_psign_b:
9965 case Intrinsic::x86_avx2_psign_w:
9966 case Intrinsic::x86_avx2_psign_d:
9967 return DAG.getNode(X86ISD::PSIGN, dl, Op.getValueType(),
9968 Op.getOperand(1), Op.getOperand(2));
Craig Topper6d688152012-08-14 07:43:25 +00009969
Craig Toppere566cd02012-01-26 07:18:03 +00009970 case Intrinsic::x86_sse41_insertps:
9971 return DAG.getNode(X86ISD::INSERTPS, dl, Op.getValueType(),
9972 Op.getOperand(1), Op.getOperand(2), Op.getOperand(3));
Craig Topper6d688152012-08-14 07:43:25 +00009973
Craig Toppere566cd02012-01-26 07:18:03 +00009974 case Intrinsic::x86_avx_vperm2f128_ps_256:
9975 case Intrinsic::x86_avx_vperm2f128_pd_256:
9976 case Intrinsic::x86_avx_vperm2f128_si_256:
9977 case Intrinsic::x86_avx2_vperm2i128:
9978 return DAG.getNode(X86ISD::VPERM2X128, dl, Op.getValueType(),
9979 Op.getOperand(1), Op.getOperand(2), Op.getOperand(3));
Craig Topper6d688152012-08-14 07:43:25 +00009980
Craig Topperffa6c402012-04-16 07:13:00 +00009981 case Intrinsic::x86_avx2_permd:
9982 case Intrinsic::x86_avx2_permps:
9983 // Operands intentionally swapped. Mask is last operand to intrinsic,
9984 // but second operand for node/intruction.
9985 return DAG.getNode(X86ISD::VPERMV, dl, Op.getValueType(),
9986 Op.getOperand(2), Op.getOperand(1));
Craig Topper98fc7292011-11-19 17:46:46 +00009987
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009988 // ptest and testp intrinsics. The intrinsic these come from are designed to
9989 // return an integer value, not just an instruction so lower it to the ptest
9990 // or testp pattern and a setcc for the result.
Eric Christopher71c67532009-07-29 00:28:05 +00009991 case Intrinsic::x86_sse41_ptestz:
9992 case Intrinsic::x86_sse41_ptestc:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009993 case Intrinsic::x86_sse41_ptestnzc:
9994 case Intrinsic::x86_avx_ptestz_256:
9995 case Intrinsic::x86_avx_ptestc_256:
9996 case Intrinsic::x86_avx_ptestnzc_256:
9997 case Intrinsic::x86_avx_vtestz_ps:
9998 case Intrinsic::x86_avx_vtestc_ps:
9999 case Intrinsic::x86_avx_vtestnzc_ps:
10000 case Intrinsic::x86_avx_vtestz_pd:
10001 case Intrinsic::x86_avx_vtestc_pd:
10002 case Intrinsic::x86_avx_vtestnzc_pd:
10003 case Intrinsic::x86_avx_vtestz_ps_256:
10004 case Intrinsic::x86_avx_vtestc_ps_256:
10005 case Intrinsic::x86_avx_vtestnzc_ps_256:
10006 case Intrinsic::x86_avx_vtestz_pd_256:
10007 case Intrinsic::x86_avx_vtestc_pd_256:
10008 case Intrinsic::x86_avx_vtestnzc_pd_256: {
10009 bool IsTestPacked = false;
Craig Topper6d688152012-08-14 07:43:25 +000010010 unsigned X86CC;
Eric Christopher71c67532009-07-29 00:28:05 +000010011 switch (IntNo) {
Eric Christopher978dae32009-07-29 18:14:04 +000010012 default: llvm_unreachable("Bad fallthrough in Intrinsic lowering.");
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +000010013 case Intrinsic::x86_avx_vtestz_ps:
10014 case Intrinsic::x86_avx_vtestz_pd:
10015 case Intrinsic::x86_avx_vtestz_ps_256:
10016 case Intrinsic::x86_avx_vtestz_pd_256:
10017 IsTestPacked = true; // Fallthrough
Eric Christopher71c67532009-07-29 00:28:05 +000010018 case Intrinsic::x86_sse41_ptestz:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +000010019 case Intrinsic::x86_avx_ptestz_256:
Eric Christopher71c67532009-07-29 00:28:05 +000010020 // ZF = 1
10021 X86CC = X86::COND_E;
10022 break;
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +000010023 case Intrinsic::x86_avx_vtestc_ps:
10024 case Intrinsic::x86_avx_vtestc_pd:
10025 case Intrinsic::x86_avx_vtestc_ps_256:
10026 case Intrinsic::x86_avx_vtestc_pd_256:
10027 IsTestPacked = true; // Fallthrough
Eric Christopher71c67532009-07-29 00:28:05 +000010028 case Intrinsic::x86_sse41_ptestc:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +000010029 case Intrinsic::x86_avx_ptestc_256:
Eric Christopher71c67532009-07-29 00:28:05 +000010030 // CF = 1
10031 X86CC = X86::COND_B;
10032 break;
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +000010033 case Intrinsic::x86_avx_vtestnzc_ps:
10034 case Intrinsic::x86_avx_vtestnzc_pd:
10035 case Intrinsic::x86_avx_vtestnzc_ps_256:
10036 case Intrinsic::x86_avx_vtestnzc_pd_256:
10037 IsTestPacked = true; // Fallthrough
Eric Christopherfd179292009-08-27 18:07:15 +000010038 case Intrinsic::x86_sse41_ptestnzc:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +000010039 case Intrinsic::x86_avx_ptestnzc_256:
Eric Christopher71c67532009-07-29 00:28:05 +000010040 // ZF and CF = 0
10041 X86CC = X86::COND_A;
10042 break;
10043 }
Eric Christopherfd179292009-08-27 18:07:15 +000010044
Eric Christopher71c67532009-07-29 00:28:05 +000010045 SDValue LHS = Op.getOperand(1);
10046 SDValue RHS = Op.getOperand(2);
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +000010047 unsigned TestOpc = IsTestPacked ? X86ISD::TESTP : X86ISD::PTEST;
10048 SDValue Test = DAG.getNode(TestOpc, dl, MVT::i32, LHS, RHS);
Owen Anderson825b72b2009-08-11 20:47:22 +000010049 SDValue CC = DAG.getConstant(X86CC, MVT::i8);
10050 SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8, CC, Test);
10051 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC);
Eric Christopher71c67532009-07-29 00:28:05 +000010052 }
Evan Cheng5759f972008-05-04 09:15:50 +000010053
Craig Topper80e46362012-01-23 06:16:53 +000010054 // SSE/AVX shift intrinsics
10055 case Intrinsic::x86_sse2_psll_w:
10056 case Intrinsic::x86_sse2_psll_d:
10057 case Intrinsic::x86_sse2_psll_q:
10058 case Intrinsic::x86_avx2_psll_w:
10059 case Intrinsic::x86_avx2_psll_d:
10060 case Intrinsic::x86_avx2_psll_q:
Craig Topper80e46362012-01-23 06:16:53 +000010061 case Intrinsic::x86_sse2_psrl_w:
10062 case Intrinsic::x86_sse2_psrl_d:
10063 case Intrinsic::x86_sse2_psrl_q:
10064 case Intrinsic::x86_avx2_psrl_w:
10065 case Intrinsic::x86_avx2_psrl_d:
10066 case Intrinsic::x86_avx2_psrl_q:
Craig Topper80e46362012-01-23 06:16:53 +000010067 case Intrinsic::x86_sse2_psra_w:
10068 case Intrinsic::x86_sse2_psra_d:
10069 case Intrinsic::x86_avx2_psra_w:
Craig Topper6d688152012-08-14 07:43:25 +000010070 case Intrinsic::x86_avx2_psra_d: {
10071 unsigned Opcode;
10072 switch (IntNo) {
10073 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
10074 case Intrinsic::x86_sse2_psll_w:
10075 case Intrinsic::x86_sse2_psll_d:
10076 case Intrinsic::x86_sse2_psll_q:
10077 case Intrinsic::x86_avx2_psll_w:
10078 case Intrinsic::x86_avx2_psll_d:
10079 case Intrinsic::x86_avx2_psll_q:
10080 Opcode = X86ISD::VSHL;
10081 break;
10082 case Intrinsic::x86_sse2_psrl_w:
10083 case Intrinsic::x86_sse2_psrl_d:
10084 case Intrinsic::x86_sse2_psrl_q:
10085 case Intrinsic::x86_avx2_psrl_w:
10086 case Intrinsic::x86_avx2_psrl_d:
10087 case Intrinsic::x86_avx2_psrl_q:
10088 Opcode = X86ISD::VSRL;
10089 break;
10090 case Intrinsic::x86_sse2_psra_w:
10091 case Intrinsic::x86_sse2_psra_d:
10092 case Intrinsic::x86_avx2_psra_w:
10093 case Intrinsic::x86_avx2_psra_d:
10094 Opcode = X86ISD::VSRA;
10095 break;
10096 }
10097 return DAG.getNode(Opcode, dl, Op.getValueType(),
Craig Topper80e46362012-01-23 06:16:53 +000010098 Op.getOperand(1), Op.getOperand(2));
Craig Topper6d688152012-08-14 07:43:25 +000010099 }
10100
10101 // SSE/AVX immediate shift intrinsics
Evan Cheng5759f972008-05-04 09:15:50 +000010102 case Intrinsic::x86_sse2_pslli_w:
10103 case Intrinsic::x86_sse2_pslli_d:
10104 case Intrinsic::x86_sse2_pslli_q:
Craig Topper80e46362012-01-23 06:16:53 +000010105 case Intrinsic::x86_avx2_pslli_w:
10106 case Intrinsic::x86_avx2_pslli_d:
10107 case Intrinsic::x86_avx2_pslli_q:
Evan Cheng5759f972008-05-04 09:15:50 +000010108 case Intrinsic::x86_sse2_psrli_w:
10109 case Intrinsic::x86_sse2_psrli_d:
10110 case Intrinsic::x86_sse2_psrli_q:
Craig Topper80e46362012-01-23 06:16:53 +000010111 case Intrinsic::x86_avx2_psrli_w:
10112 case Intrinsic::x86_avx2_psrli_d:
10113 case Intrinsic::x86_avx2_psrli_q:
Evan Cheng5759f972008-05-04 09:15:50 +000010114 case Intrinsic::x86_sse2_psrai_w:
10115 case Intrinsic::x86_sse2_psrai_d:
Craig Topper80e46362012-01-23 06:16:53 +000010116 case Intrinsic::x86_avx2_psrai_w:
Craig Topper6d688152012-08-14 07:43:25 +000010117 case Intrinsic::x86_avx2_psrai_d: {
10118 unsigned Opcode;
10119 switch (IntNo) {
10120 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
10121 case Intrinsic::x86_sse2_pslli_w:
10122 case Intrinsic::x86_sse2_pslli_d:
10123 case Intrinsic::x86_sse2_pslli_q:
10124 case Intrinsic::x86_avx2_pslli_w:
10125 case Intrinsic::x86_avx2_pslli_d:
10126 case Intrinsic::x86_avx2_pslli_q:
10127 Opcode = X86ISD::VSHLI;
10128 break;
10129 case Intrinsic::x86_sse2_psrli_w:
10130 case Intrinsic::x86_sse2_psrli_d:
10131 case Intrinsic::x86_sse2_psrli_q:
10132 case Intrinsic::x86_avx2_psrli_w:
10133 case Intrinsic::x86_avx2_psrli_d:
10134 case Intrinsic::x86_avx2_psrli_q:
10135 Opcode = X86ISD::VSRLI;
10136 break;
10137 case Intrinsic::x86_sse2_psrai_w:
10138 case Intrinsic::x86_sse2_psrai_d:
10139 case Intrinsic::x86_avx2_psrai_w:
10140 case Intrinsic::x86_avx2_psrai_d:
10141 Opcode = X86ISD::VSRAI;
10142 break;
10143 }
10144 return getTargetVShiftNode(Opcode, dl, Op.getValueType(),
Craig Topper80e46362012-01-23 06:16:53 +000010145 Op.getOperand(1), Op.getOperand(2), DAG);
Craig Topper6d688152012-08-14 07:43:25 +000010146 }
10147
Craig Topper4feb6472012-08-06 06:22:36 +000010148 case Intrinsic::x86_sse42_pcmpistria128:
10149 case Intrinsic::x86_sse42_pcmpestria128:
10150 case Intrinsic::x86_sse42_pcmpistric128:
10151 case Intrinsic::x86_sse42_pcmpestric128:
10152 case Intrinsic::x86_sse42_pcmpistrio128:
10153 case Intrinsic::x86_sse42_pcmpestrio128:
10154 case Intrinsic::x86_sse42_pcmpistris128:
10155 case Intrinsic::x86_sse42_pcmpestris128:
10156 case Intrinsic::x86_sse42_pcmpistriz128:
10157 case Intrinsic::x86_sse42_pcmpestriz128: {
10158 unsigned Opcode;
10159 unsigned X86CC;
10160 switch (IntNo) {
10161 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
10162 case Intrinsic::x86_sse42_pcmpistria128:
10163 Opcode = X86ISD::PCMPISTRI;
10164 X86CC = X86::COND_A;
10165 break;
10166 case Intrinsic::x86_sse42_pcmpestria128:
10167 Opcode = X86ISD::PCMPESTRI;
10168 X86CC = X86::COND_A;
10169 break;
10170 case Intrinsic::x86_sse42_pcmpistric128:
10171 Opcode = X86ISD::PCMPISTRI;
10172 X86CC = X86::COND_B;
10173 break;
10174 case Intrinsic::x86_sse42_pcmpestric128:
10175 Opcode = X86ISD::PCMPESTRI;
10176 X86CC = X86::COND_B;
10177 break;
10178 case Intrinsic::x86_sse42_pcmpistrio128:
10179 Opcode = X86ISD::PCMPISTRI;
10180 X86CC = X86::COND_O;
10181 break;
10182 case Intrinsic::x86_sse42_pcmpestrio128:
10183 Opcode = X86ISD::PCMPESTRI;
10184 X86CC = X86::COND_O;
10185 break;
10186 case Intrinsic::x86_sse42_pcmpistris128:
10187 Opcode = X86ISD::PCMPISTRI;
10188 X86CC = X86::COND_S;
10189 break;
10190 case Intrinsic::x86_sse42_pcmpestris128:
10191 Opcode = X86ISD::PCMPESTRI;
10192 X86CC = X86::COND_S;
10193 break;
10194 case Intrinsic::x86_sse42_pcmpistriz128:
10195 Opcode = X86ISD::PCMPISTRI;
10196 X86CC = X86::COND_E;
10197 break;
10198 case Intrinsic::x86_sse42_pcmpestriz128:
10199 Opcode = X86ISD::PCMPESTRI;
10200 X86CC = X86::COND_E;
10201 break;
10202 }
10203 SmallVector<SDValue, 5> NewOps;
10204 NewOps.append(Op->op_begin()+1, Op->op_end());
10205 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::i32);
10206 SDValue PCMP = DAG.getNode(Opcode, dl, VTs, NewOps.data(), NewOps.size());
10207 SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
10208 DAG.getConstant(X86CC, MVT::i8),
10209 SDValue(PCMP.getNode(), 1));
10210 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC);
10211 }
Craig Topper6d688152012-08-14 07:43:25 +000010212
Craig Topper4feb6472012-08-06 06:22:36 +000010213 case Intrinsic::x86_sse42_pcmpistri128:
10214 case Intrinsic::x86_sse42_pcmpestri128: {
10215 unsigned Opcode;
10216 if (IntNo == Intrinsic::x86_sse42_pcmpistri128)
10217 Opcode = X86ISD::PCMPISTRI;
10218 else
10219 Opcode = X86ISD::PCMPESTRI;
10220
10221 SmallVector<SDValue, 5> NewOps;
10222 NewOps.append(Op->op_begin()+1, Op->op_end());
10223 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::i32);
10224 return DAG.getNode(Opcode, dl, VTs, NewOps.data(), NewOps.size());
10225 }
Craig Topper0e292372012-08-24 04:03:22 +000010226 case Intrinsic::x86_fma_vfmadd_ps:
10227 case Intrinsic::x86_fma_vfmadd_pd:
10228 case Intrinsic::x86_fma_vfmsub_ps:
10229 case Intrinsic::x86_fma_vfmsub_pd:
10230 case Intrinsic::x86_fma_vfnmadd_ps:
10231 case Intrinsic::x86_fma_vfnmadd_pd:
10232 case Intrinsic::x86_fma_vfnmsub_ps:
10233 case Intrinsic::x86_fma_vfnmsub_pd:
10234 case Intrinsic::x86_fma_vfmaddsub_ps:
10235 case Intrinsic::x86_fma_vfmaddsub_pd:
10236 case Intrinsic::x86_fma_vfmsubadd_ps:
10237 case Intrinsic::x86_fma_vfmsubadd_pd:
10238 case Intrinsic::x86_fma_vfmadd_ps_256:
10239 case Intrinsic::x86_fma_vfmadd_pd_256:
10240 case Intrinsic::x86_fma_vfmsub_ps_256:
10241 case Intrinsic::x86_fma_vfmsub_pd_256:
10242 case Intrinsic::x86_fma_vfnmadd_ps_256:
10243 case Intrinsic::x86_fma_vfnmadd_pd_256:
10244 case Intrinsic::x86_fma_vfnmsub_ps_256:
10245 case Intrinsic::x86_fma_vfnmsub_pd_256:
10246 case Intrinsic::x86_fma_vfmaddsub_ps_256:
10247 case Intrinsic::x86_fma_vfmaddsub_pd_256:
10248 case Intrinsic::x86_fma_vfmsubadd_ps_256:
10249 case Intrinsic::x86_fma_vfmsubadd_pd_256: {
Craig Topper0e292372012-08-24 04:03:22 +000010250 unsigned Opc;
10251 switch (IntNo) {
10252 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
10253 case Intrinsic::x86_fma_vfmadd_ps:
10254 case Intrinsic::x86_fma_vfmadd_pd:
10255 case Intrinsic::x86_fma_vfmadd_ps_256:
10256 case Intrinsic::x86_fma_vfmadd_pd_256:
10257 Opc = X86ISD::FMADD;
10258 break;
10259 case Intrinsic::x86_fma_vfmsub_ps:
10260 case Intrinsic::x86_fma_vfmsub_pd:
10261 case Intrinsic::x86_fma_vfmsub_ps_256:
10262 case Intrinsic::x86_fma_vfmsub_pd_256:
10263 Opc = X86ISD::FMSUB;
10264 break;
10265 case Intrinsic::x86_fma_vfnmadd_ps:
10266 case Intrinsic::x86_fma_vfnmadd_pd:
10267 case Intrinsic::x86_fma_vfnmadd_ps_256:
10268 case Intrinsic::x86_fma_vfnmadd_pd_256:
10269 Opc = X86ISD::FNMADD;
10270 break;
10271 case Intrinsic::x86_fma_vfnmsub_ps:
10272 case Intrinsic::x86_fma_vfnmsub_pd:
10273 case Intrinsic::x86_fma_vfnmsub_ps_256:
10274 case Intrinsic::x86_fma_vfnmsub_pd_256:
10275 Opc = X86ISD::FNMSUB;
10276 break;
10277 case Intrinsic::x86_fma_vfmaddsub_ps:
10278 case Intrinsic::x86_fma_vfmaddsub_pd:
10279 case Intrinsic::x86_fma_vfmaddsub_ps_256:
10280 case Intrinsic::x86_fma_vfmaddsub_pd_256:
10281 Opc = X86ISD::FMADDSUB;
10282 break;
10283 case Intrinsic::x86_fma_vfmsubadd_ps:
10284 case Intrinsic::x86_fma_vfmsubadd_pd:
10285 case Intrinsic::x86_fma_vfmsubadd_ps_256:
10286 case Intrinsic::x86_fma_vfmsubadd_pd_256:
10287 Opc = X86ISD::FMSUBADD;
10288 break;
10289 }
10290
10291 return DAG.getNode(Opc, dl, Op.getValueType(), Op.getOperand(1),
10292 Op.getOperand(2), Op.getOperand(3));
10293 }
Evan Cheng38bcbaf2005-12-23 07:31:11 +000010294 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000010295}
Evan Cheng72261582005-12-20 06:22:03 +000010296
Craig Topper55b24052012-09-11 06:15:32 +000010297static SDValue LowerINTRINSIC_W_CHAIN(SDValue Op, SelectionDAG &DAG) {
Benjamin Kramerb9bee042012-07-12 09:31:43 +000010298 DebugLoc dl = Op.getDebugLoc();
10299 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
10300 switch (IntNo) {
10301 default: return SDValue(); // Don't custom lower most intrinsics.
10302
10303 // RDRAND intrinsics.
10304 case Intrinsic::x86_rdrand_16:
10305 case Intrinsic::x86_rdrand_32:
10306 case Intrinsic::x86_rdrand_64: {
10307 // Emit the node with the right value type.
Benjamin Kramerfeae00a2012-07-12 18:14:57 +000010308 SDVTList VTs = DAG.getVTList(Op->getValueType(0), MVT::Glue, MVT::Other);
10309 SDValue Result = DAG.getNode(X86ISD::RDRAND, dl, VTs, Op.getOperand(0));
Benjamin Kramerb9bee042012-07-12 09:31:43 +000010310
10311 // If the value returned by RDRAND was valid (CF=1), return 1. Otherwise
10312 // return the value from Rand, which is always 0, casted to i32.
10313 SDValue Ops[] = { DAG.getZExtOrTrunc(Result, dl, Op->getValueType(1)),
10314 DAG.getConstant(1, Op->getValueType(1)),
10315 DAG.getConstant(X86::COND_B, MVT::i32),
10316 SDValue(Result.getNode(), 1) };
10317 SDValue isValid = DAG.getNode(X86ISD::CMOV, dl,
10318 DAG.getVTList(Op->getValueType(1), MVT::Glue),
10319 Ops, 4);
10320
10321 // Return { result, isValid, chain }.
10322 return DAG.getNode(ISD::MERGE_VALUES, dl, Op->getVTList(), Result, isValid,
Benjamin Kramerfeae00a2012-07-12 18:14:57 +000010323 SDValue(Result.getNode(), 2));
Benjamin Kramerb9bee042012-07-12 09:31:43 +000010324 }
10325 }
10326}
10327
Dan Gohmand858e902010-04-17 15:26:15 +000010328SDValue X86TargetLowering::LowerRETURNADDR(SDValue Op,
10329 SelectionDAG &DAG) const {
Evan Cheng2457f2c2010-05-22 01:47:14 +000010330 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
10331 MFI->setReturnAddressIsTaken(true);
10332
Bill Wendling64e87322009-01-16 19:25:27 +000010333 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Dale Johannesen6f38cb62009-02-07 19:59:05 +000010334 DebugLoc dl = Op.getDebugLoc();
Bill Wendling64e87322009-01-16 19:25:27 +000010335
10336 if (Depth > 0) {
10337 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
10338 SDValue Offset =
Micah Villmow2c39b152012-10-15 16:24:29 +000010339 DAG.getConstant(TD->getPointerSize(0),
Owen Anderson825b72b2009-08-11 20:47:22 +000010340 Subtarget->is64Bit() ? MVT::i64 : MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +000010341 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
Scott Michelfdc40a02009-02-17 22:15:04 +000010342 DAG.getNode(ISD::ADD, dl, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +000010343 FrameAddr, Offset),
Pete Cooperd752e0f2011-11-08 18:42:53 +000010344 MachinePointerInfo(), false, false, false, 0);
Bill Wendling64e87322009-01-16 19:25:27 +000010345 }
10346
10347 // Just load the return address.
Dan Gohman475871a2008-07-27 21:46:04 +000010348 SDValue RetAddrFI = getReturnAddressFrameIndex(DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +000010349 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000010350 RetAddrFI, MachinePointerInfo(), false, false, false, 0);
Nate Begemanbcc5f362007-01-29 22:58:52 +000010351}
10352
Dan Gohmand858e902010-04-17 15:26:15 +000010353SDValue X86TargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng184793f2008-09-27 01:56:22 +000010354 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
10355 MFI->setFrameAddressIsTaken(true);
Evan Cheng2457f2c2010-05-22 01:47:14 +000010356
Owen Andersone50ed302009-08-10 22:56:29 +000010357 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +000010358 DebugLoc dl = Op.getDebugLoc(); // FIXME probably not meaningful
Evan Cheng184793f2008-09-27 01:56:22 +000010359 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
10360 unsigned FrameReg = Subtarget->is64Bit() ? X86::RBP : X86::EBP;
Dale Johannesendd64c412009-02-04 00:33:20 +000010361 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, VT);
Evan Cheng184793f2008-09-27 01:56:22 +000010362 while (Depth--)
Chris Lattner51abfe42010-09-21 06:02:19 +000010363 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr,
10364 MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000010365 false, false, false, 0);
Evan Cheng184793f2008-09-27 01:56:22 +000010366 return FrameAddr;
Nate Begemanbcc5f362007-01-29 22:58:52 +000010367}
10368
Dan Gohman475871a2008-07-27 21:46:04 +000010369SDValue X86TargetLowering::LowerFRAME_TO_ARGS_OFFSET(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +000010370 SelectionDAG &DAG) const {
Micah Villmow2c39b152012-10-15 16:24:29 +000010371 return DAG.getIntPtrConstant(2*TD->getPointerSize(0));
Anton Korobeynikov2365f512007-07-14 14:06:15 +000010372}
10373
Dan Gohmand858e902010-04-17 15:26:15 +000010374SDValue X86TargetLowering::LowerEH_RETURN(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +000010375 SDValue Chain = Op.getOperand(0);
10376 SDValue Offset = Op.getOperand(1);
10377 SDValue Handler = Op.getOperand(2);
Dale Johannesen6f38cb62009-02-07 19:59:05 +000010378 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov2365f512007-07-14 14:06:15 +000010379
Dan Gohmand8816272010-08-11 18:14:00 +000010380 SDValue Frame = DAG.getCopyFromReg(DAG.getEntryNode(), dl,
10381 Subtarget->is64Bit() ? X86::RBP : X86::EBP,
10382 getPointerTy());
Anton Korobeynikovb84c1672008-09-08 21:12:47 +000010383 unsigned StoreAddrReg = (Subtarget->is64Bit() ? X86::RCX : X86::ECX);
Anton Korobeynikov2365f512007-07-14 14:06:15 +000010384
Dan Gohmand8816272010-08-11 18:14:00 +000010385 SDValue StoreAddr = DAG.getNode(ISD::ADD, dl, getPointerTy(), Frame,
Micah Villmow2c39b152012-10-15 16:24:29 +000010386 DAG.getIntPtrConstant(TD->getPointerSize(0)));
Dale Johannesene4d209d2009-02-03 20:21:25 +000010387 StoreAddr = DAG.getNode(ISD::ADD, dl, getPointerTy(), StoreAddr, Offset);
Chris Lattner8026a9d2010-09-21 17:50:43 +000010388 Chain = DAG.getStore(Chain, dl, Handler, StoreAddr, MachinePointerInfo(),
10389 false, false, 0);
Dale Johannesendd64c412009-02-04 00:33:20 +000010390 Chain = DAG.getCopyToReg(Chain, dl, StoreAddrReg, StoreAddr);
Anton Korobeynikov2365f512007-07-14 14:06:15 +000010391
Dale Johannesene4d209d2009-02-03 20:21:25 +000010392 return DAG.getNode(X86ISD::EH_RETURN, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +000010393 MVT::Other,
Anton Korobeynikovb84c1672008-09-08 21:12:47 +000010394 Chain, DAG.getRegister(StoreAddrReg, getPointerTy()));
Anton Korobeynikov2365f512007-07-14 14:06:15 +000010395}
10396
Michael Liao6c0e04c2012-10-15 22:39:43 +000010397SDValue X86TargetLowering::lowerEH_SJLJ_SETJMP(SDValue Op,
10398 SelectionDAG &DAG) const {
10399 DebugLoc DL = Op.getDebugLoc();
10400 return DAG.getNode(X86ISD::EH_SJLJ_SETJMP, DL,
10401 DAG.getVTList(MVT::i32, MVT::Other),
10402 Op.getOperand(0), Op.getOperand(1));
10403}
10404
10405SDValue X86TargetLowering::lowerEH_SJLJ_LONGJMP(SDValue Op,
10406 SelectionDAG &DAG) const {
10407 DebugLoc DL = Op.getDebugLoc();
10408 return DAG.getNode(X86ISD::EH_SJLJ_LONGJMP, DL, MVT::Other,
10409 Op.getOperand(0), Op.getOperand(1));
10410}
10411
Craig Topper55b24052012-09-11 06:15:32 +000010412static SDValue LowerADJUST_TRAMPOLINE(SDValue Op, SelectionDAG &DAG) {
Duncan Sands4a544a72011-09-06 13:37:06 +000010413 return Op.getOperand(0);
10414}
10415
10416SDValue X86TargetLowering::LowerINIT_TRAMPOLINE(SDValue Op,
10417 SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +000010418 SDValue Root = Op.getOperand(0);
10419 SDValue Trmp = Op.getOperand(1); // trampoline
10420 SDValue FPtr = Op.getOperand(2); // nested function
10421 SDValue Nest = Op.getOperand(3); // 'nest' parameter value
Dale Johannesen6f38cb62009-02-07 19:59:05 +000010422 DebugLoc dl = Op.getDebugLoc();
Duncan Sandsb116fac2007-07-27 20:02:49 +000010423
Dan Gohman69de1932008-02-06 22:27:42 +000010424 const Value *TrmpAddr = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Michael Liao7abf67a2012-10-04 19:50:43 +000010425 const TargetRegisterInfo* TRI = getTargetMachine().getRegisterInfo();
Duncan Sandsb116fac2007-07-27 20:02:49 +000010426
10427 if (Subtarget->is64Bit()) {
Dan Gohman475871a2008-07-27 21:46:04 +000010428 SDValue OutChains[6];
Duncan Sands339e14f2008-01-16 22:55:25 +000010429
10430 // Large code-model.
Chris Lattnera62fe662010-02-05 19:20:30 +000010431 const unsigned char JMP64r = 0xFF; // 64-bit jmp through register opcode.
10432 const unsigned char MOV64ri = 0xB8; // X86::MOV64ri opcode.
Duncan Sands339e14f2008-01-16 22:55:25 +000010433
Michael Liao7abf67a2012-10-04 19:50:43 +000010434 const unsigned char N86R10 = TRI->getEncodingValue(X86::R10) & 0x7;
10435 const unsigned char N86R11 = TRI->getEncodingValue(X86::R11) & 0x7;
Duncan Sands339e14f2008-01-16 22:55:25 +000010436
10437 const unsigned char REX_WB = 0x40 | 0x08 | 0x01; // REX prefix
10438
10439 // Load the pointer to the nested function into R11.
10440 unsigned OpCode = ((MOV64ri | N86R11) << 8) | REX_WB; // movabsq r11
Dan Gohman475871a2008-07-27 21:46:04 +000010441 SDValue Addr = Trmp;
Owen Anderson825b72b2009-08-11 20:47:22 +000010442 OutChains[0] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Chris Lattner8026a9d2010-09-21 17:50:43 +000010443 Addr, MachinePointerInfo(TrmpAddr),
10444 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +000010445
Owen Anderson825b72b2009-08-11 20:47:22 +000010446 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
10447 DAG.getConstant(2, MVT::i64));
Chris Lattner8026a9d2010-09-21 17:50:43 +000010448 OutChains[1] = DAG.getStore(Root, dl, FPtr, Addr,
10449 MachinePointerInfo(TrmpAddr, 2),
David Greene67c9d422010-02-15 16:53:33 +000010450 false, false, 2);
Duncan Sands339e14f2008-01-16 22:55:25 +000010451
10452 // Load the 'nest' parameter value into R10.
10453 // R10 is specified in X86CallingConv.td
10454 OpCode = ((MOV64ri | N86R10) << 8) | REX_WB; // movabsq r10
Owen Anderson825b72b2009-08-11 20:47:22 +000010455 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
10456 DAG.getConstant(10, MVT::i64));
10457 OutChains[2] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Chris Lattner8026a9d2010-09-21 17:50:43 +000010458 Addr, MachinePointerInfo(TrmpAddr, 10),
10459 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +000010460
Owen Anderson825b72b2009-08-11 20:47:22 +000010461 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
10462 DAG.getConstant(12, MVT::i64));
Chris Lattner8026a9d2010-09-21 17:50:43 +000010463 OutChains[3] = DAG.getStore(Root, dl, Nest, Addr,
10464 MachinePointerInfo(TrmpAddr, 12),
David Greene67c9d422010-02-15 16:53:33 +000010465 false, false, 2);
Duncan Sands339e14f2008-01-16 22:55:25 +000010466
10467 // Jump to the nested function.
10468 OpCode = (JMP64r << 8) | REX_WB; // jmpq *...
Owen Anderson825b72b2009-08-11 20:47:22 +000010469 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
10470 DAG.getConstant(20, MVT::i64));
10471 OutChains[4] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Chris Lattner8026a9d2010-09-21 17:50:43 +000010472 Addr, MachinePointerInfo(TrmpAddr, 20),
10473 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +000010474
10475 unsigned char ModRM = N86R11 | (4 << 3) | (3 << 6); // ...r11
Owen Anderson825b72b2009-08-11 20:47:22 +000010476 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
10477 DAG.getConstant(22, MVT::i64));
10478 OutChains[5] = DAG.getStore(Root, dl, DAG.getConstant(ModRM, MVT::i8), Addr,
Chris Lattner8026a9d2010-09-21 17:50:43 +000010479 MachinePointerInfo(TrmpAddr, 22),
10480 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +000010481
Duncan Sands4a544a72011-09-06 13:37:06 +000010482 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains, 6);
Duncan Sandsb116fac2007-07-27 20:02:49 +000010483 } else {
Dan Gohmanbbfb9c52008-01-31 01:01:48 +000010484 const Function *Func =
Duncan Sandsb116fac2007-07-27 20:02:49 +000010485 cast<Function>(cast<SrcValueSDNode>(Op.getOperand(5))->getValue());
Sandeep Patel65c3c8f2009-09-02 08:44:58 +000010486 CallingConv::ID CC = Func->getCallingConv();
Duncan Sandsee465742007-08-29 19:01:20 +000010487 unsigned NestReg;
Duncan Sandsb116fac2007-07-27 20:02:49 +000010488
10489 switch (CC) {
10490 default:
Torok Edwinc23197a2009-07-14 16:55:14 +000010491 llvm_unreachable("Unsupported calling convention");
Duncan Sandsb116fac2007-07-27 20:02:49 +000010492 case CallingConv::C:
Duncan Sandsb116fac2007-07-27 20:02:49 +000010493 case CallingConv::X86_StdCall: {
10494 // Pass 'nest' parameter in ECX.
10495 // Must be kept in sync with X86CallingConv.td
Duncan Sandsee465742007-08-29 19:01:20 +000010496 NestReg = X86::ECX;
Duncan Sandsb116fac2007-07-27 20:02:49 +000010497
10498 // Check that ECX wasn't needed by an 'inreg' parameter.
Chris Lattnerdb125cf2011-07-18 04:54:35 +000010499 FunctionType *FTy = Func->getFunctionType();
Devang Patel05988662008-09-25 21:00:45 +000010500 const AttrListPtr &Attrs = Func->getAttributes();
Duncan Sandsb116fac2007-07-27 20:02:49 +000010501
Chris Lattner58d74912008-03-12 17:45:29 +000010502 if (!Attrs.isEmpty() && !Func->isVarArg()) {
Duncan Sandsb116fac2007-07-27 20:02:49 +000010503 unsigned InRegCount = 0;
10504 unsigned Idx = 1;
10505
10506 for (FunctionType::param_iterator I = FTy->param_begin(),
10507 E = FTy->param_end(); I != E; ++I, ++Idx)
Bill Wendling67658342012-10-09 07:45:08 +000010508 if (Attrs.getParamAttributes(Idx).hasAttribute(Attributes::InReg))
Duncan Sandsb116fac2007-07-27 20:02:49 +000010509 // FIXME: should only count parameters that are lowered to integers.
Anton Korobeynikovbff66b02008-09-09 18:22:57 +000010510 InRegCount += (TD->getTypeSizeInBits(*I) + 31) / 32;
Duncan Sandsb116fac2007-07-27 20:02:49 +000010511
10512 if (InRegCount > 2) {
Eric Christopher90eb4022010-07-22 00:26:08 +000010513 report_fatal_error("Nest register in use - reduce number of inreg"
10514 " parameters!");
Duncan Sandsb116fac2007-07-27 20:02:49 +000010515 }
10516 }
10517 break;
10518 }
10519 case CallingConv::X86_FastCall:
Anton Korobeynikovded05e32010-05-16 09:08:45 +000010520 case CallingConv::X86_ThisCall:
Duncan Sandsbf53c292008-09-10 13:22:10 +000010521 case CallingConv::Fast:
Duncan Sandsb116fac2007-07-27 20:02:49 +000010522 // Pass 'nest' parameter in EAX.
10523 // Must be kept in sync with X86CallingConv.td
Duncan Sandsee465742007-08-29 19:01:20 +000010524 NestReg = X86::EAX;
Duncan Sandsb116fac2007-07-27 20:02:49 +000010525 break;
10526 }
10527
Dan Gohman475871a2008-07-27 21:46:04 +000010528 SDValue OutChains[4];
10529 SDValue Addr, Disp;
Duncan Sandsb116fac2007-07-27 20:02:49 +000010530
Owen Anderson825b72b2009-08-11 20:47:22 +000010531 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
10532 DAG.getConstant(10, MVT::i32));
10533 Disp = DAG.getNode(ISD::SUB, dl, MVT::i32, FPtr, Addr);
Duncan Sandsb116fac2007-07-27 20:02:49 +000010534
Chris Lattnera62fe662010-02-05 19:20:30 +000010535 // This is storing the opcode for MOV32ri.
10536 const unsigned char MOV32ri = 0xB8; // X86::MOV32ri's opcode byte.
Michael Liao7abf67a2012-10-04 19:50:43 +000010537 const unsigned char N86Reg = TRI->getEncodingValue(NestReg) & 0x7;
Scott Michelfdc40a02009-02-17 22:15:04 +000010538 OutChains[0] = DAG.getStore(Root, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +000010539 DAG.getConstant(MOV32ri|N86Reg, MVT::i8),
Chris Lattner8026a9d2010-09-21 17:50:43 +000010540 Trmp, MachinePointerInfo(TrmpAddr),
10541 false, false, 0);
Duncan Sandsb116fac2007-07-27 20:02:49 +000010542
Owen Anderson825b72b2009-08-11 20:47:22 +000010543 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
10544 DAG.getConstant(1, MVT::i32));
Chris Lattner8026a9d2010-09-21 17:50:43 +000010545 OutChains[1] = DAG.getStore(Root, dl, Nest, Addr,
10546 MachinePointerInfo(TrmpAddr, 1),
David Greene67c9d422010-02-15 16:53:33 +000010547 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +000010548
Chris Lattnera62fe662010-02-05 19:20:30 +000010549 const unsigned char JMP = 0xE9; // jmp <32bit dst> opcode.
Owen Anderson825b72b2009-08-11 20:47:22 +000010550 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
10551 DAG.getConstant(5, MVT::i32));
10552 OutChains[2] = DAG.getStore(Root, dl, DAG.getConstant(JMP, MVT::i8), Addr,
Chris Lattner8026a9d2010-09-21 17:50:43 +000010553 MachinePointerInfo(TrmpAddr, 5),
10554 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +000010555
Owen Anderson825b72b2009-08-11 20:47:22 +000010556 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
10557 DAG.getConstant(6, MVT::i32));
Chris Lattner8026a9d2010-09-21 17:50:43 +000010558 OutChains[3] = DAG.getStore(Root, dl, Disp, Addr,
10559 MachinePointerInfo(TrmpAddr, 6),
David Greene67c9d422010-02-15 16:53:33 +000010560 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +000010561
Duncan Sands4a544a72011-09-06 13:37:06 +000010562 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains, 4);
Duncan Sandsb116fac2007-07-27 20:02:49 +000010563 }
10564}
10565
Dan Gohmand858e902010-04-17 15:26:15 +000010566SDValue X86TargetLowering::LowerFLT_ROUNDS_(SDValue Op,
10567 SelectionDAG &DAG) const {
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000010568 /*
10569 The rounding mode is in bits 11:10 of FPSR, and has the following
10570 settings:
10571 00 Round to nearest
10572 01 Round to -inf
10573 10 Round to +inf
10574 11 Round to 0
10575
10576 FLT_ROUNDS, on the other hand, expects the following:
10577 -1 Undefined
10578 0 Round to 0
10579 1 Round to nearest
10580 2 Round to +inf
10581 3 Round to -inf
10582
10583 To perform the conversion, we do:
10584 (((((FPSR & 0x800) >> 11) | ((FPSR & 0x400) >> 9)) + 1) & 3)
10585 */
10586
10587 MachineFunction &MF = DAG.getMachineFunction();
10588 const TargetMachine &TM = MF.getTarget();
Anton Korobeynikov16c29b52011-01-10 12:39:04 +000010589 const TargetFrameLowering &TFI = *TM.getFrameLowering();
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000010590 unsigned StackAlignment = TFI.getStackAlignment();
Owen Andersone50ed302009-08-10 22:56:29 +000010591 EVT VT = Op.getValueType();
Chris Lattner2156b792010-09-22 01:11:26 +000010592 DebugLoc DL = Op.getDebugLoc();
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000010593
10594 // Save FP Control Word to stack slot
David Greene3f2bf852009-11-12 20:49:22 +000010595 int SSFI = MF.getFrameInfo()->CreateStackObject(2, StackAlignment, false);
Dan Gohman475871a2008-07-27 21:46:04 +000010596 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000010597
Michael J. Spencerec38de22010-10-10 22:04:20 +000010598
Chris Lattner2156b792010-09-22 01:11:26 +000010599 MachineMemOperand *MMO =
10600 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
10601 MachineMemOperand::MOStore, 2, 2);
Michael J. Spencerec38de22010-10-10 22:04:20 +000010602
Chris Lattner2156b792010-09-22 01:11:26 +000010603 SDValue Ops[] = { DAG.getEntryNode(), StackSlot };
10604 SDValue Chain = DAG.getMemIntrinsicNode(X86ISD::FNSTCW16m, DL,
10605 DAG.getVTList(MVT::Other),
10606 Ops, 2, MVT::i16, MMO);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000010607
10608 // Load FP Control Word from stack slot
Chris Lattner2156b792010-09-22 01:11:26 +000010609 SDValue CWD = DAG.getLoad(MVT::i16, DL, Chain, StackSlot,
Pete Cooperd752e0f2011-11-08 18:42:53 +000010610 MachinePointerInfo(), false, false, false, 0);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000010611
10612 // Transform as necessary
Dan Gohman475871a2008-07-27 21:46:04 +000010613 SDValue CWD1 =
Chris Lattner2156b792010-09-22 01:11:26 +000010614 DAG.getNode(ISD::SRL, DL, MVT::i16,
10615 DAG.getNode(ISD::AND, DL, MVT::i16,
Owen Anderson825b72b2009-08-11 20:47:22 +000010616 CWD, DAG.getConstant(0x800, MVT::i16)),
10617 DAG.getConstant(11, MVT::i8));
Dan Gohman475871a2008-07-27 21:46:04 +000010618 SDValue CWD2 =
Chris Lattner2156b792010-09-22 01:11:26 +000010619 DAG.getNode(ISD::SRL, DL, MVT::i16,
10620 DAG.getNode(ISD::AND, DL, MVT::i16,
Owen Anderson825b72b2009-08-11 20:47:22 +000010621 CWD, DAG.getConstant(0x400, MVT::i16)),
10622 DAG.getConstant(9, MVT::i8));
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000010623
Dan Gohman475871a2008-07-27 21:46:04 +000010624 SDValue RetVal =
Chris Lattner2156b792010-09-22 01:11:26 +000010625 DAG.getNode(ISD::AND, DL, MVT::i16,
10626 DAG.getNode(ISD::ADD, DL, MVT::i16,
10627 DAG.getNode(ISD::OR, DL, MVT::i16, CWD1, CWD2),
Owen Anderson825b72b2009-08-11 20:47:22 +000010628 DAG.getConstant(1, MVT::i16)),
10629 DAG.getConstant(3, MVT::i16));
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000010630
10631
Duncan Sands83ec4b62008-06-06 12:08:01 +000010632 return DAG.getNode((VT.getSizeInBits() < 16 ?
Chris Lattner2156b792010-09-22 01:11:26 +000010633 ISD::TRUNCATE : ISD::ZERO_EXTEND), DL, VT, RetVal);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000010634}
10635
Craig Topper55b24052012-09-11 06:15:32 +000010636static SDValue LowerCTLZ(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +000010637 EVT VT = Op.getValueType();
10638 EVT OpVT = VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +000010639 unsigned NumBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +000010640 DebugLoc dl = Op.getDebugLoc();
Evan Cheng18efe262007-12-14 02:13:44 +000010641
10642 Op = Op.getOperand(0);
Owen Anderson825b72b2009-08-11 20:47:22 +000010643 if (VT == MVT::i8) {
Evan Cheng152804e2007-12-14 08:30:15 +000010644 // Zero extend to i32 since there is not an i8 bsr.
Owen Anderson825b72b2009-08-11 20:47:22 +000010645 OpVT = MVT::i32;
Dale Johannesene4d209d2009-02-03 20:21:25 +000010646 Op = DAG.getNode(ISD::ZERO_EXTEND, dl, OpVT, Op);
Evan Cheng18efe262007-12-14 02:13:44 +000010647 }
Evan Cheng18efe262007-12-14 02:13:44 +000010648
Evan Cheng152804e2007-12-14 08:30:15 +000010649 // Issue a bsr (scan bits in reverse) which also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +000010650 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +000010651 Op = DAG.getNode(X86ISD::BSR, dl, VTs, Op);
Evan Cheng152804e2007-12-14 08:30:15 +000010652
10653 // If src is zero (i.e. bsr sets ZF), returns NumBits.
Benjamin Kramer7f1a5602009-12-29 16:57:26 +000010654 SDValue Ops[] = {
10655 Op,
10656 DAG.getConstant(NumBits+NumBits-1, OpVT),
10657 DAG.getConstant(X86::COND_E, MVT::i8),
10658 Op.getValue(1)
10659 };
10660 Op = DAG.getNode(X86ISD::CMOV, dl, OpVT, Ops, array_lengthof(Ops));
Evan Cheng152804e2007-12-14 08:30:15 +000010661
10662 // Finally xor with NumBits-1.
Dale Johannesene4d209d2009-02-03 20:21:25 +000010663 Op = DAG.getNode(ISD::XOR, dl, OpVT, Op, DAG.getConstant(NumBits-1, OpVT));
Evan Cheng152804e2007-12-14 08:30:15 +000010664
Owen Anderson825b72b2009-08-11 20:47:22 +000010665 if (VT == MVT::i8)
10666 Op = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Op);
Evan Cheng18efe262007-12-14 02:13:44 +000010667 return Op;
10668}
10669
Craig Topper55b24052012-09-11 06:15:32 +000010670static SDValue LowerCTLZ_ZERO_UNDEF(SDValue Op, SelectionDAG &DAG) {
Chandler Carruthacc068e2011-12-24 10:55:54 +000010671 EVT VT = Op.getValueType();
10672 EVT OpVT = VT;
10673 unsigned NumBits = VT.getSizeInBits();
10674 DebugLoc dl = Op.getDebugLoc();
10675
10676 Op = Op.getOperand(0);
10677 if (VT == MVT::i8) {
10678 // Zero extend to i32 since there is not an i8 bsr.
10679 OpVT = MVT::i32;
10680 Op = DAG.getNode(ISD::ZERO_EXTEND, dl, OpVT, Op);
10681 }
10682
10683 // Issue a bsr (scan bits in reverse).
10684 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
10685 Op = DAG.getNode(X86ISD::BSR, dl, VTs, Op);
10686
10687 // And xor with NumBits-1.
10688 Op = DAG.getNode(ISD::XOR, dl, OpVT, Op, DAG.getConstant(NumBits-1, OpVT));
10689
10690 if (VT == MVT::i8)
10691 Op = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Op);
10692 return Op;
10693}
10694
Craig Topper55b24052012-09-11 06:15:32 +000010695static SDValue LowerCTTZ(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +000010696 EVT VT = Op.getValueType();
Duncan Sands83ec4b62008-06-06 12:08:01 +000010697 unsigned NumBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +000010698 DebugLoc dl = Op.getDebugLoc();
Evan Cheng18efe262007-12-14 02:13:44 +000010699 Op = Op.getOperand(0);
Evan Cheng152804e2007-12-14 08:30:15 +000010700
10701 // Issue a bsf (scan bits forward) which also sets EFLAGS.
Chandler Carruth77821022011-12-24 12:12:34 +000010702 SDVTList VTs = DAG.getVTList(VT, MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +000010703 Op = DAG.getNode(X86ISD::BSF, dl, VTs, Op);
Evan Cheng152804e2007-12-14 08:30:15 +000010704
10705 // If src is zero (i.e. bsf sets ZF), returns NumBits.
Benjamin Kramer7f1a5602009-12-29 16:57:26 +000010706 SDValue Ops[] = {
10707 Op,
Chandler Carruth77821022011-12-24 12:12:34 +000010708 DAG.getConstant(NumBits, VT),
Benjamin Kramer7f1a5602009-12-29 16:57:26 +000010709 DAG.getConstant(X86::COND_E, MVT::i8),
10710 Op.getValue(1)
10711 };
Chandler Carruth77821022011-12-24 12:12:34 +000010712 return DAG.getNode(X86ISD::CMOV, dl, VT, Ops, array_lengthof(Ops));
Evan Cheng18efe262007-12-14 02:13:44 +000010713}
10714
Craig Topper13894fa2011-08-24 06:14:18 +000010715// Lower256IntArith - Break a 256-bit integer operation into two new 128-bit
10716// ones, and then concatenate the result back.
10717static SDValue Lower256IntArith(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +000010718 EVT VT = Op.getValueType();
Craig Topper13894fa2011-08-24 06:14:18 +000010719
Craig Topper7a9a28b2012-08-12 02:23:29 +000010720 assert(VT.is256BitVector() && VT.isInteger() &&
Craig Topper13894fa2011-08-24 06:14:18 +000010721 "Unsupported value type for operation");
10722
Craig Topper66ddd152012-04-27 22:54:43 +000010723 unsigned NumElems = VT.getVectorNumElements();
Craig Topper13894fa2011-08-24 06:14:18 +000010724 DebugLoc dl = Op.getDebugLoc();
Craig Topper13894fa2011-08-24 06:14:18 +000010725
10726 // Extract the LHS vectors
10727 SDValue LHS = Op.getOperand(0);
Craig Topperb14940a2012-04-22 20:55:18 +000010728 SDValue LHS1 = Extract128BitVector(LHS, 0, DAG, dl);
10729 SDValue LHS2 = Extract128BitVector(LHS, NumElems/2, DAG, dl);
Craig Topper13894fa2011-08-24 06:14:18 +000010730
10731 // Extract the RHS vectors
10732 SDValue RHS = Op.getOperand(1);
Craig Topperb14940a2012-04-22 20:55:18 +000010733 SDValue RHS1 = Extract128BitVector(RHS, 0, DAG, dl);
10734 SDValue RHS2 = Extract128BitVector(RHS, NumElems/2, DAG, dl);
Craig Topper13894fa2011-08-24 06:14:18 +000010735
10736 MVT EltVT = VT.getVectorElementType().getSimpleVT();
10737 EVT NewVT = MVT::getVectorVT(EltVT, NumElems/2);
10738
10739 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT,
10740 DAG.getNode(Op.getOpcode(), dl, NewVT, LHS1, RHS1),
10741 DAG.getNode(Op.getOpcode(), dl, NewVT, LHS2, RHS2));
10742}
10743
Craig Topper55b24052012-09-11 06:15:32 +000010744static SDValue LowerADD(SDValue Op, SelectionDAG &DAG) {
Craig Topper7a9a28b2012-08-12 02:23:29 +000010745 assert(Op.getValueType().is256BitVector() &&
Craig Topper13894fa2011-08-24 06:14:18 +000010746 Op.getValueType().isInteger() &&
10747 "Only handle AVX 256-bit vector integer operation");
10748 return Lower256IntArith(Op, DAG);
10749}
10750
Craig Topper55b24052012-09-11 06:15:32 +000010751static SDValue LowerSUB(SDValue Op, SelectionDAG &DAG) {
Craig Topper7a9a28b2012-08-12 02:23:29 +000010752 assert(Op.getValueType().is256BitVector() &&
Craig Topper13894fa2011-08-24 06:14:18 +000010753 Op.getValueType().isInteger() &&
10754 "Only handle AVX 256-bit vector integer operation");
10755 return Lower256IntArith(Op, DAG);
10756}
10757
Craig Topper55b24052012-09-11 06:15:32 +000010758static SDValue LowerMUL(SDValue Op, const X86Subtarget *Subtarget,
10759 SelectionDAG &DAG) {
Craig Topper13894fa2011-08-24 06:14:18 +000010760 EVT VT = Op.getValueType();
10761
10762 // Decompose 256-bit ops into smaller 128-bit ops.
Craig Topper7a9a28b2012-08-12 02:23:29 +000010763 if (VT.is256BitVector() && !Subtarget->hasAVX2())
Craig Topper13894fa2011-08-24 06:14:18 +000010764 return Lower256IntArith(Op, DAG);
10765
Craig Topper5b209e82012-02-05 03:14:49 +000010766 assert((VT == MVT::v2i64 || VT == MVT::v4i64) &&
10767 "Only know how to lower V2I64/V4I64 multiply");
10768
Dale Johannesen6f38cb62009-02-07 19:59:05 +000010769 DebugLoc dl = Op.getDebugLoc();
Scott Michelfdc40a02009-02-17 22:15:04 +000010770
Craig Topper5b209e82012-02-05 03:14:49 +000010771 // Ahi = psrlqi(a, 32);
10772 // Bhi = psrlqi(b, 32);
10773 //
10774 // AloBlo = pmuludq(a, b);
10775 // AloBhi = pmuludq(a, Bhi);
10776 // AhiBlo = pmuludq(Ahi, b);
10777
10778 // AloBhi = psllqi(AloBhi, 32);
10779 // AhiBlo = psllqi(AhiBlo, 32);
10780 // return AloBlo + AloBhi + AhiBlo;
10781
Craig Topperaaa643c2011-11-09 07:28:55 +000010782 SDValue A = Op.getOperand(0);
10783 SDValue B = Op.getOperand(1);
10784
Craig Topper5b209e82012-02-05 03:14:49 +000010785 SDValue ShAmt = DAG.getConstant(32, MVT::i32);
Craig Topperaaa643c2011-11-09 07:28:55 +000010786
Craig Topper5b209e82012-02-05 03:14:49 +000010787 SDValue Ahi = DAG.getNode(X86ISD::VSRLI, dl, VT, A, ShAmt);
10788 SDValue Bhi = DAG.getNode(X86ISD::VSRLI, dl, VT, B, ShAmt);
Craig Topperaaa643c2011-11-09 07:28:55 +000010789
Craig Topper5b209e82012-02-05 03:14:49 +000010790 // Bit cast to 32-bit vectors for MULUDQ
10791 EVT MulVT = (VT == MVT::v2i64) ? MVT::v4i32 : MVT::v8i32;
10792 A = DAG.getNode(ISD::BITCAST, dl, MulVT, A);
10793 B = DAG.getNode(ISD::BITCAST, dl, MulVT, B);
10794 Ahi = DAG.getNode(ISD::BITCAST, dl, MulVT, Ahi);
10795 Bhi = DAG.getNode(ISD::BITCAST, dl, MulVT, Bhi);
Craig Topperaaa643c2011-11-09 07:28:55 +000010796
Craig Topper5b209e82012-02-05 03:14:49 +000010797 SDValue AloBlo = DAG.getNode(X86ISD::PMULUDQ, dl, VT, A, B);
10798 SDValue AloBhi = DAG.getNode(X86ISD::PMULUDQ, dl, VT, A, Bhi);
10799 SDValue AhiBlo = DAG.getNode(X86ISD::PMULUDQ, dl, VT, Ahi, B);
Craig Topperaaa643c2011-11-09 07:28:55 +000010800
Craig Topper5b209e82012-02-05 03:14:49 +000010801 AloBhi = DAG.getNode(X86ISD::VSHLI, dl, VT, AloBhi, ShAmt);
10802 AhiBlo = DAG.getNode(X86ISD::VSHLI, dl, VT, AhiBlo, ShAmt);
Mon P Wangaf9b9522008-12-18 21:42:19 +000010803
Dale Johannesene4d209d2009-02-03 20:21:25 +000010804 SDValue Res = DAG.getNode(ISD::ADD, dl, VT, AloBlo, AloBhi);
Craig Topper5b209e82012-02-05 03:14:49 +000010805 return DAG.getNode(ISD::ADD, dl, VT, Res, AhiBlo);
Mon P Wangaf9b9522008-12-18 21:42:19 +000010806}
10807
Nadav Rotem43012222011-05-11 08:12:09 +000010808SDValue X86TargetLowering::LowerShift(SDValue Op, SelectionDAG &DAG) const {
10809
Nate Begemanbdcb5af2010-07-27 22:37:06 +000010810 EVT VT = Op.getValueType();
10811 DebugLoc dl = Op.getDebugLoc();
10812 SDValue R = Op.getOperand(0);
Nadav Rotem43012222011-05-11 08:12:09 +000010813 SDValue Amt = Op.getOperand(1);
Nate Begemanbdcb5af2010-07-27 22:37:06 +000010814 LLVMContext *Context = DAG.getContext();
Nate Begemanbdcb5af2010-07-27 22:37:06 +000010815
Craig Topper1accb7e2012-01-10 06:54:16 +000010816 if (!Subtarget->hasSSE2())
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +000010817 return SDValue();
10818
Nadav Rotem43012222011-05-11 08:12:09 +000010819 // Optimize shl/srl/sra with constant shift amount.
10820 if (isSplatVector(Amt.getNode())) {
10821 SDValue SclrAmt = Amt->getOperand(0);
10822 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(SclrAmt)) {
10823 uint64_t ShiftAmt = C->getZExtValue();
10824
Craig Toppered2e13d2012-01-22 19:15:14 +000010825 if (VT == MVT::v2i64 || VT == MVT::v4i32 || VT == MVT::v8i16 ||
10826 (Subtarget->hasAVX2() &&
10827 (VT == MVT::v4i64 || VT == MVT::v8i32 || VT == MVT::v16i16))) {
10828 if (Op.getOpcode() == ISD::SHL)
10829 return DAG.getNode(X86ISD::VSHLI, dl, VT, R,
10830 DAG.getConstant(ShiftAmt, MVT::i32));
10831 if (Op.getOpcode() == ISD::SRL)
10832 return DAG.getNode(X86ISD::VSRLI, dl, VT, R,
10833 DAG.getConstant(ShiftAmt, MVT::i32));
10834 if (Op.getOpcode() == ISD::SRA && VT != MVT::v2i64 && VT != MVT::v4i64)
10835 return DAG.getNode(X86ISD::VSRAI, dl, VT, R,
10836 DAG.getConstant(ShiftAmt, MVT::i32));
Benjamin Kramerdade3c12011-10-30 17:31:21 +000010837 }
10838
Craig Toppered2e13d2012-01-22 19:15:14 +000010839 if (VT == MVT::v16i8) {
10840 if (Op.getOpcode() == ISD::SHL) {
10841 // Make a large shift.
10842 SDValue SHL = DAG.getNode(X86ISD::VSHLI, dl, MVT::v8i16, R,
10843 DAG.getConstant(ShiftAmt, MVT::i32));
10844 SHL = DAG.getNode(ISD::BITCAST, dl, VT, SHL);
10845 // Zero out the rightmost bits.
10846 SmallVector<SDValue, 16> V(16,
10847 DAG.getConstant(uint8_t(-1U << ShiftAmt),
10848 MVT::i8));
10849 return DAG.getNode(ISD::AND, dl, VT, SHL,
10850 DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &V[0], 16));
Eli Friedmanf6aa6b12011-11-01 21:18:39 +000010851 }
Craig Toppered2e13d2012-01-22 19:15:14 +000010852 if (Op.getOpcode() == ISD::SRL) {
10853 // Make a large shift.
10854 SDValue SRL = DAG.getNode(X86ISD::VSRLI, dl, MVT::v8i16, R,
10855 DAG.getConstant(ShiftAmt, MVT::i32));
10856 SRL = DAG.getNode(ISD::BITCAST, dl, VT, SRL);
10857 // Zero out the leftmost bits.
10858 SmallVector<SDValue, 16> V(16,
10859 DAG.getConstant(uint8_t(-1U) >> ShiftAmt,
10860 MVT::i8));
10861 return DAG.getNode(ISD::AND, dl, VT, SRL,
10862 DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &V[0], 16));
10863 }
10864 if (Op.getOpcode() == ISD::SRA) {
10865 if (ShiftAmt == 7) {
10866 // R s>> 7 === R s< 0
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +000010867 SDValue Zeros = getZeroVector(VT, Subtarget, DAG, dl);
Craig Topper67609fd2012-01-22 22:42:16 +000010868 return DAG.getNode(X86ISD::PCMPGT, dl, VT, Zeros, R);
Craig Toppered2e13d2012-01-22 19:15:14 +000010869 }
Eli Friedmanf6aa6b12011-11-01 21:18:39 +000010870
Craig Toppered2e13d2012-01-22 19:15:14 +000010871 // R s>> a === ((R u>> a) ^ m) - m
10872 SDValue Res = DAG.getNode(ISD::SRL, dl, VT, R, Amt);
10873 SmallVector<SDValue, 16> V(16, DAG.getConstant(128 >> ShiftAmt,
10874 MVT::i8));
10875 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &V[0], 16);
10876 Res = DAG.getNode(ISD::XOR, dl, VT, Res, Mask);
10877 Res = DAG.getNode(ISD::SUB, dl, VT, Res, Mask);
10878 return Res;
10879 }
Craig Topper731dfd02012-04-23 03:42:40 +000010880 llvm_unreachable("Unknown shift opcode.");
Eli Friedmanf6aa6b12011-11-01 21:18:39 +000010881 }
Craig Topper46154eb2011-11-11 07:39:23 +000010882
Craig Topper0d86d462011-11-20 00:12:05 +000010883 if (Subtarget->hasAVX2() && VT == MVT::v32i8) {
10884 if (Op.getOpcode() == ISD::SHL) {
10885 // Make a large shift.
Craig Toppered2e13d2012-01-22 19:15:14 +000010886 SDValue SHL = DAG.getNode(X86ISD::VSHLI, dl, MVT::v16i16, R,
10887 DAG.getConstant(ShiftAmt, MVT::i32));
10888 SHL = DAG.getNode(ISD::BITCAST, dl, VT, SHL);
Craig Topper0d86d462011-11-20 00:12:05 +000010889 // Zero out the rightmost bits.
Craig Toppered2e13d2012-01-22 19:15:14 +000010890 SmallVector<SDValue, 32> V(32,
10891 DAG.getConstant(uint8_t(-1U << ShiftAmt),
10892 MVT::i8));
Craig Topper0d86d462011-11-20 00:12:05 +000010893 return DAG.getNode(ISD::AND, dl, VT, SHL,
10894 DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &V[0], 32));
Craig Topper46154eb2011-11-11 07:39:23 +000010895 }
Craig Topper0d86d462011-11-20 00:12:05 +000010896 if (Op.getOpcode() == ISD::SRL) {
10897 // Make a large shift.
Craig Toppered2e13d2012-01-22 19:15:14 +000010898 SDValue SRL = DAG.getNode(X86ISD::VSRLI, dl, MVT::v16i16, R,
10899 DAG.getConstant(ShiftAmt, MVT::i32));
10900 SRL = DAG.getNode(ISD::BITCAST, dl, VT, SRL);
Craig Topper0d86d462011-11-20 00:12:05 +000010901 // Zero out the leftmost bits.
Craig Toppered2e13d2012-01-22 19:15:14 +000010902 SmallVector<SDValue, 32> V(32,
10903 DAG.getConstant(uint8_t(-1U) >> ShiftAmt,
10904 MVT::i8));
Craig Topper0d86d462011-11-20 00:12:05 +000010905 return DAG.getNode(ISD::AND, dl, VT, SRL,
10906 DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &V[0], 32));
10907 }
10908 if (Op.getOpcode() == ISD::SRA) {
10909 if (ShiftAmt == 7) {
10910 // R s>> 7 === R s< 0
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +000010911 SDValue Zeros = getZeroVector(VT, Subtarget, DAG, dl);
Craig Topper67609fd2012-01-22 22:42:16 +000010912 return DAG.getNode(X86ISD::PCMPGT, dl, VT, Zeros, R);
Craig Topper0d86d462011-11-20 00:12:05 +000010913 }
10914
10915 // R s>> a === ((R u>> a) ^ m) - m
10916 SDValue Res = DAG.getNode(ISD::SRL, dl, VT, R, Amt);
10917 SmallVector<SDValue, 32> V(32, DAG.getConstant(128 >> ShiftAmt,
10918 MVT::i8));
10919 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &V[0], 32);
10920 Res = DAG.getNode(ISD::XOR, dl, VT, Res, Mask);
10921 Res = DAG.getNode(ISD::SUB, dl, VT, Res, Mask);
10922 return Res;
10923 }
Craig Topper731dfd02012-04-23 03:42:40 +000010924 llvm_unreachable("Unknown shift opcode.");
Craig Topper0d86d462011-11-20 00:12:05 +000010925 }
Nadav Rotem43012222011-05-11 08:12:09 +000010926 }
10927 }
10928
10929 // Lower SHL with variable shift amount.
Nadav Rotem43012222011-05-11 08:12:09 +000010930 if (VT == MVT::v4i32 && Op->getOpcode() == ISD::SHL) {
Craig Toppered2e13d2012-01-22 19:15:14 +000010931 Op = DAG.getNode(X86ISD::VSHLI, dl, VT, Op.getOperand(1),
10932 DAG.getConstant(23, MVT::i32));
Nate Begeman51409212010-07-28 00:21:48 +000010933
Chris Lattner7302d802012-02-06 21:56:39 +000010934 const uint32_t CV[] = { 0x3f800000U, 0x3f800000U, 0x3f800000U, 0x3f800000U};
10935 Constant *C = ConstantDataVector::get(*Context, CV);
Nate Begeman51409212010-07-28 00:21:48 +000010936 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
10937 SDValue Addend = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +000010938 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000010939 false, false, false, 16);
Nate Begeman51409212010-07-28 00:21:48 +000010940
10941 Op = DAG.getNode(ISD::ADD, dl, VT, Op, Addend);
Wesley Peckbf17cfa2010-11-23 03:31:01 +000010942 Op = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, Op);
Nate Begeman51409212010-07-28 00:21:48 +000010943 Op = DAG.getNode(ISD::FP_TO_SINT, dl, VT, Op);
10944 return DAG.getNode(ISD::MUL, dl, VT, Op, R);
10945 }
Nadav Rotem43012222011-05-11 08:12:09 +000010946 if (VT == MVT::v16i8 && Op->getOpcode() == ISD::SHL) {
Craig Topper8b5a6b62012-01-17 08:23:44 +000010947 assert(Subtarget->hasSSE2() && "Need SSE2 for pslli/pcmpeq.");
Lang Hames8b99c1e2011-12-17 01:08:46 +000010948
Nate Begeman51409212010-07-28 00:21:48 +000010949 // a = a << 5;
Craig Toppered2e13d2012-01-22 19:15:14 +000010950 Op = DAG.getNode(X86ISD::VSHLI, dl, MVT::v8i16, Op.getOperand(1),
10951 DAG.getConstant(5, MVT::i32));
10952 Op = DAG.getNode(ISD::BITCAST, dl, VT, Op);
Nate Begeman51409212010-07-28 00:21:48 +000010953
Lang Hames8b99c1e2011-12-17 01:08:46 +000010954 // Turn 'a' into a mask suitable for VSELECT
10955 SDValue VSelM = DAG.getConstant(0x80, VT);
10956 SDValue OpVSel = DAG.getNode(ISD::AND, dl, VT, VSelM, Op);
Craig Topper67609fd2012-01-22 22:42:16 +000010957 OpVSel = DAG.getNode(X86ISD::PCMPEQ, dl, VT, OpVSel, VSelM);
Nate Begeman51409212010-07-28 00:21:48 +000010958
Lang Hames8b99c1e2011-12-17 01:08:46 +000010959 SDValue CM1 = DAG.getConstant(0x0f, VT);
10960 SDValue CM2 = DAG.getConstant(0x3f, VT);
Nate Begeman51409212010-07-28 00:21:48 +000010961
Lang Hames8b99c1e2011-12-17 01:08:46 +000010962 // r = VSELECT(r, psllw(r & (char16)15, 4), a);
10963 SDValue M = DAG.getNode(ISD::AND, dl, VT, R, CM1);
Craig Toppered2e13d2012-01-22 19:15:14 +000010964 M = getTargetVShiftNode(X86ISD::VSHLI, dl, MVT::v8i16, M,
10965 DAG.getConstant(4, MVT::i32), DAG);
10966 M = DAG.getNode(ISD::BITCAST, dl, VT, M);
Lang Hames8b99c1e2011-12-17 01:08:46 +000010967 R = DAG.getNode(ISD::VSELECT, dl, VT, OpVSel, M, R);
10968
Nate Begeman51409212010-07-28 00:21:48 +000010969 // a += a
10970 Op = DAG.getNode(ISD::ADD, dl, VT, Op, Op);
Lang Hames8b99c1e2011-12-17 01:08:46 +000010971 OpVSel = DAG.getNode(ISD::AND, dl, VT, VSelM, Op);
Craig Topper67609fd2012-01-22 22:42:16 +000010972 OpVSel = DAG.getNode(X86ISD::PCMPEQ, dl, VT, OpVSel, VSelM);
Michael J. Spencerec38de22010-10-10 22:04:20 +000010973
Lang Hames8b99c1e2011-12-17 01:08:46 +000010974 // r = VSELECT(r, psllw(r & (char16)63, 2), a);
10975 M = DAG.getNode(ISD::AND, dl, VT, R, CM2);
Craig Toppered2e13d2012-01-22 19:15:14 +000010976 M = getTargetVShiftNode(X86ISD::VSHLI, dl, MVT::v8i16, M,
10977 DAG.getConstant(2, MVT::i32), DAG);
10978 M = DAG.getNode(ISD::BITCAST, dl, VT, M);
Lang Hames8b99c1e2011-12-17 01:08:46 +000010979 R = DAG.getNode(ISD::VSELECT, dl, VT, OpVSel, M, R);
10980
Nate Begeman51409212010-07-28 00:21:48 +000010981 // a += a
10982 Op = DAG.getNode(ISD::ADD, dl, VT, Op, Op);
Lang Hames8b99c1e2011-12-17 01:08:46 +000010983 OpVSel = DAG.getNode(ISD::AND, dl, VT, VSelM, Op);
Craig Topper67609fd2012-01-22 22:42:16 +000010984 OpVSel = DAG.getNode(X86ISD::PCMPEQ, dl, VT, OpVSel, VSelM);
Michael J. Spencerec38de22010-10-10 22:04:20 +000010985
Lang Hames8b99c1e2011-12-17 01:08:46 +000010986 // return VSELECT(r, r+r, a);
10987 R = DAG.getNode(ISD::VSELECT, dl, VT, OpVSel,
Lang Hamesa0a25132011-12-15 18:57:27 +000010988 DAG.getNode(ISD::ADD, dl, VT, R, R), R);
Nate Begeman51409212010-07-28 00:21:48 +000010989 return R;
10990 }
Craig Topper46154eb2011-11-11 07:39:23 +000010991
10992 // Decompose 256-bit shifts into smaller 128-bit shifts.
Craig Topper7a9a28b2012-08-12 02:23:29 +000010993 if (VT.is256BitVector()) {
Craig Toppered2e13d2012-01-22 19:15:14 +000010994 unsigned NumElems = VT.getVectorNumElements();
Craig Topper46154eb2011-11-11 07:39:23 +000010995 MVT EltVT = VT.getVectorElementType().getSimpleVT();
10996 EVT NewVT = MVT::getVectorVT(EltVT, NumElems/2);
10997
10998 // Extract the two vectors
Craig Topperb14940a2012-04-22 20:55:18 +000010999 SDValue V1 = Extract128BitVector(R, 0, DAG, dl);
11000 SDValue V2 = Extract128BitVector(R, NumElems/2, DAG, dl);
Craig Topper46154eb2011-11-11 07:39:23 +000011001
11002 // Recreate the shift amount vectors
11003 SDValue Amt1, Amt2;
11004 if (Amt.getOpcode() == ISD::BUILD_VECTOR) {
11005 // Constant shift amount
11006 SmallVector<SDValue, 4> Amt1Csts;
11007 SmallVector<SDValue, 4> Amt2Csts;
Craig Toppered2e13d2012-01-22 19:15:14 +000011008 for (unsigned i = 0; i != NumElems/2; ++i)
Craig Topper46154eb2011-11-11 07:39:23 +000011009 Amt1Csts.push_back(Amt->getOperand(i));
Craig Toppered2e13d2012-01-22 19:15:14 +000011010 for (unsigned i = NumElems/2; i != NumElems; ++i)
Craig Topper46154eb2011-11-11 07:39:23 +000011011 Amt2Csts.push_back(Amt->getOperand(i));
11012
11013 Amt1 = DAG.getNode(ISD::BUILD_VECTOR, dl, NewVT,
11014 &Amt1Csts[0], NumElems/2);
11015 Amt2 = DAG.getNode(ISD::BUILD_VECTOR, dl, NewVT,
11016 &Amt2Csts[0], NumElems/2);
11017 } else {
11018 // Variable shift amount
Craig Topperb14940a2012-04-22 20:55:18 +000011019 Amt1 = Extract128BitVector(Amt, 0, DAG, dl);
11020 Amt2 = Extract128BitVector(Amt, NumElems/2, DAG, dl);
Craig Topper46154eb2011-11-11 07:39:23 +000011021 }
11022
11023 // Issue new vector shifts for the smaller types
11024 V1 = DAG.getNode(Op.getOpcode(), dl, NewVT, V1, Amt1);
11025 V2 = DAG.getNode(Op.getOpcode(), dl, NewVT, V2, Amt2);
11026
11027 // Concatenate the result back
11028 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT, V1, V2);
11029 }
11030
Nate Begeman51409212010-07-28 00:21:48 +000011031 return SDValue();
Nate Begemanbdcb5af2010-07-27 22:37:06 +000011032}
Mon P Wangaf9b9522008-12-18 21:42:19 +000011033
Craig Topper55b24052012-09-11 06:15:32 +000011034static SDValue LowerXALUO(SDValue Op, SelectionDAG &DAG) {
Bill Wendling74c37652008-12-09 22:08:41 +000011035 // Lower the "add/sub/mul with overflow" instruction into a regular ins plus
11036 // a "setcc" instruction that checks the overflow flag. The "brcond" lowering
Bill Wendling61edeb52008-12-02 01:06:39 +000011037 // looks for this combo and may remove the "setcc" instruction if the "setcc"
11038 // has only one use.
Bill Wendling3fafd932008-11-26 22:37:40 +000011039 SDNode *N = Op.getNode();
Bill Wendling61edeb52008-12-02 01:06:39 +000011040 SDValue LHS = N->getOperand(0);
11041 SDValue RHS = N->getOperand(1);
Bill Wendling74c37652008-12-09 22:08:41 +000011042 unsigned BaseOp = 0;
11043 unsigned Cond = 0;
Chris Lattnerb20e0b12010-12-05 07:30:36 +000011044 DebugLoc DL = Op.getDebugLoc();
Bill Wendling74c37652008-12-09 22:08:41 +000011045 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +000011046 default: llvm_unreachable("Unknown ovf instruction!");
Bill Wendling74c37652008-12-09 22:08:41 +000011047 case ISD::SADDO:
Dan Gohman076aee32009-03-04 19:44:21 +000011048 // A subtract of one will be selected as a INC. Note that INC doesn't
11049 // set CF, so we can't do this for UADDO.
Benjamin Kramerc175a4b2011-03-08 15:20:20 +000011050 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(RHS))
11051 if (C->isOne()) {
Dan Gohman076aee32009-03-04 19:44:21 +000011052 BaseOp = X86ISD::INC;
11053 Cond = X86::COND_O;
11054 break;
11055 }
Bill Wendlingab55ebd2008-12-12 00:56:36 +000011056 BaseOp = X86ISD::ADD;
Bill Wendling74c37652008-12-09 22:08:41 +000011057 Cond = X86::COND_O;
11058 break;
11059 case ISD::UADDO:
Bill Wendlingab55ebd2008-12-12 00:56:36 +000011060 BaseOp = X86ISD::ADD;
Dan Gohman653456c2009-01-07 00:15:08 +000011061 Cond = X86::COND_B;
Bill Wendling74c37652008-12-09 22:08:41 +000011062 break;
11063 case ISD::SSUBO:
Dan Gohman076aee32009-03-04 19:44:21 +000011064 // A subtract of one will be selected as a DEC. Note that DEC doesn't
11065 // set CF, so we can't do this for USUBO.
Benjamin Kramerc175a4b2011-03-08 15:20:20 +000011066 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(RHS))
11067 if (C->isOne()) {
Dan Gohman076aee32009-03-04 19:44:21 +000011068 BaseOp = X86ISD::DEC;
11069 Cond = X86::COND_O;
11070 break;
11071 }
Bill Wendlingab55ebd2008-12-12 00:56:36 +000011072 BaseOp = X86ISD::SUB;
Bill Wendling74c37652008-12-09 22:08:41 +000011073 Cond = X86::COND_O;
11074 break;
11075 case ISD::USUBO:
Bill Wendlingab55ebd2008-12-12 00:56:36 +000011076 BaseOp = X86ISD::SUB;
Dan Gohman653456c2009-01-07 00:15:08 +000011077 Cond = X86::COND_B;
Bill Wendling74c37652008-12-09 22:08:41 +000011078 break;
11079 case ISD::SMULO:
Bill Wendlingd350e022008-12-12 21:15:41 +000011080 BaseOp = X86ISD::SMUL;
Bill Wendling74c37652008-12-09 22:08:41 +000011081 Cond = X86::COND_O;
11082 break;
Chris Lattnerb20e0b12010-12-05 07:30:36 +000011083 case ISD::UMULO: { // i64, i8 = umulo lhs, rhs --> i64, i64, i32 umul lhs,rhs
11084 SDVTList VTs = DAG.getVTList(N->getValueType(0), N->getValueType(0),
11085 MVT::i32);
11086 SDValue Sum = DAG.getNode(X86ISD::UMUL, DL, VTs, LHS, RHS);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011087
Chris Lattnerb20e0b12010-12-05 07:30:36 +000011088 SDValue SetCC =
11089 DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
11090 DAG.getConstant(X86::COND_O, MVT::i32),
11091 SDValue(Sum.getNode(), 2));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011092
Dan Gohman6e5fda22011-07-22 18:45:15 +000011093 return DAG.getNode(ISD::MERGE_VALUES, DL, N->getVTList(), Sum, SetCC);
Chris Lattnerb20e0b12010-12-05 07:30:36 +000011094 }
Bill Wendling74c37652008-12-09 22:08:41 +000011095 }
Bill Wendling3fafd932008-11-26 22:37:40 +000011096
Bill Wendling61edeb52008-12-02 01:06:39 +000011097 // Also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +000011098 SDVTList VTs = DAG.getVTList(N->getValueType(0), MVT::i32);
Chris Lattnerb20e0b12010-12-05 07:30:36 +000011099 SDValue Sum = DAG.getNode(BaseOp, DL, VTs, LHS, RHS);
Bill Wendling3fafd932008-11-26 22:37:40 +000011100
Bill Wendling61edeb52008-12-02 01:06:39 +000011101 SDValue SetCC =
Chris Lattnerb20e0b12010-12-05 07:30:36 +000011102 DAG.getNode(X86ISD::SETCC, DL, N->getValueType(1),
11103 DAG.getConstant(Cond, MVT::i32),
11104 SDValue(Sum.getNode(), 1));
Bill Wendling3fafd932008-11-26 22:37:40 +000011105
Dan Gohman6e5fda22011-07-22 18:45:15 +000011106 return DAG.getNode(ISD::MERGE_VALUES, DL, N->getVTList(), Sum, SetCC);
Bill Wendling41ea7e72008-11-24 19:21:46 +000011107}
11108
Chad Rosier30450e82011-12-22 22:35:21 +000011109SDValue X86TargetLowering::LowerSIGN_EXTEND_INREG(SDValue Op,
11110 SelectionDAG &DAG) const {
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000011111 DebugLoc dl = Op.getDebugLoc();
Craig Toppera124f942011-11-21 01:12:36 +000011112 EVT ExtraVT = cast<VTSDNode>(Op.getOperand(1))->getVT();
11113 EVT VT = Op.getValueType();
11114
Craig Toppered2e13d2012-01-22 19:15:14 +000011115 if (!Subtarget->hasSSE2() || !VT.isVector())
11116 return SDValue();
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000011117
Craig Toppered2e13d2012-01-22 19:15:14 +000011118 unsigned BitsDiff = VT.getScalarType().getSizeInBits() -
11119 ExtraVT.getScalarType().getSizeInBits();
11120 SDValue ShAmt = DAG.getConstant(BitsDiff, MVT::i32);
11121
11122 switch (VT.getSimpleVT().SimpleTy) {
11123 default: return SDValue();
11124 case MVT::v8i32:
11125 case MVT::v16i16:
11126 if (!Subtarget->hasAVX())
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000011127 return SDValue();
Craig Toppered2e13d2012-01-22 19:15:14 +000011128 if (!Subtarget->hasAVX2()) {
11129 // needs to be split
Craig Topper66ddd152012-04-27 22:54:43 +000011130 unsigned NumElems = VT.getVectorNumElements();
Craig Toppera124f942011-11-21 01:12:36 +000011131
Craig Toppered2e13d2012-01-22 19:15:14 +000011132 // Extract the LHS vectors
11133 SDValue LHS = Op.getOperand(0);
Craig Topperb14940a2012-04-22 20:55:18 +000011134 SDValue LHS1 = Extract128BitVector(LHS, 0, DAG, dl);
11135 SDValue LHS2 = Extract128BitVector(LHS, NumElems/2, DAG, dl);
Craig Toppera124f942011-11-21 01:12:36 +000011136
Craig Toppered2e13d2012-01-22 19:15:14 +000011137 MVT EltVT = VT.getVectorElementType().getSimpleVT();
11138 EVT NewVT = MVT::getVectorVT(EltVT, NumElems/2);
Craig Toppera124f942011-11-21 01:12:36 +000011139
Craig Toppered2e13d2012-01-22 19:15:14 +000011140 EVT ExtraEltVT = ExtraVT.getVectorElementType();
Craig Topperb6072642012-05-03 07:26:59 +000011141 unsigned ExtraNumElems = ExtraVT.getVectorNumElements();
Craig Toppered2e13d2012-01-22 19:15:14 +000011142 ExtraVT = EVT::getVectorVT(*DAG.getContext(), ExtraEltVT,
11143 ExtraNumElems/2);
11144 SDValue Extra = DAG.getValueType(ExtraVT);
Craig Toppera124f942011-11-21 01:12:36 +000011145
Craig Toppered2e13d2012-01-22 19:15:14 +000011146 LHS1 = DAG.getNode(Op.getOpcode(), dl, NewVT, LHS1, Extra);
11147 LHS2 = DAG.getNode(Op.getOpcode(), dl, NewVT, LHS2, Extra);
Craig Toppera124f942011-11-21 01:12:36 +000011148
Dmitri Gribenko2de05722012-09-10 21:26:47 +000011149 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT, LHS1, LHS2);
Craig Toppered2e13d2012-01-22 19:15:14 +000011150 }
11151 // fall through
11152 case MVT::v4i32:
11153 case MVT::v8i16: {
11154 SDValue Tmp1 = getTargetVShiftNode(X86ISD::VSHLI, dl, VT,
11155 Op.getOperand(0), ShAmt, DAG);
11156 return getTargetVShiftNode(X86ISD::VSRAI, dl, VT, Tmp1, ShAmt, DAG);
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000011157 }
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000011158 }
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000011159}
11160
11161
Craig Topper55b24052012-09-11 06:15:32 +000011162static SDValue LowerMEMBARRIER(SDValue Op, const X86Subtarget *Subtarget,
11163 SelectionDAG &DAG) {
Eric Christopher9a9d2752010-07-22 02:48:34 +000011164 DebugLoc dl = Op.getDebugLoc();
Michael J. Spencerec38de22010-10-10 22:04:20 +000011165
Eric Christopher77ed1352011-07-08 00:04:56 +000011166 // Go ahead and emit the fence on x86-64 even if we asked for no-sse2.
11167 // There isn't any reason to disable it if the target processor supports it.
Craig Topper1accb7e2012-01-10 06:54:16 +000011168 if (!Subtarget->hasSSE2() && !Subtarget->is64Bit()) {
Eric Christopherc0b2a202010-08-14 21:51:50 +000011169 SDValue Chain = Op.getOperand(0);
Eric Christopher77ed1352011-07-08 00:04:56 +000011170 SDValue Zero = DAG.getConstant(0, MVT::i32);
Eric Christopherc0b2a202010-08-14 21:51:50 +000011171 SDValue Ops[] = {
11172 DAG.getRegister(X86::ESP, MVT::i32), // Base
11173 DAG.getTargetConstant(1, MVT::i8), // Scale
11174 DAG.getRegister(0, MVT::i32), // Index
11175 DAG.getTargetConstant(0, MVT::i32), // Disp
11176 DAG.getRegister(0, MVT::i32), // Segment.
11177 Zero,
11178 Chain
11179 };
Michael J. Spencerec38de22010-10-10 22:04:20 +000011180 SDNode *Res =
Eric Christopherc0b2a202010-08-14 21:51:50 +000011181 DAG.getMachineNode(X86::OR32mrLocked, dl, MVT::Other, Ops,
11182 array_lengthof(Ops));
11183 return SDValue(Res, 0);
Eric Christopherb6729dc2010-08-04 23:03:04 +000011184 }
Michael J. Spencerec38de22010-10-10 22:04:20 +000011185
Eric Christopher9a9d2752010-07-22 02:48:34 +000011186 unsigned isDev = cast<ConstantSDNode>(Op.getOperand(5))->getZExtValue();
Chris Lattner132929a2010-08-14 17:26:09 +000011187 if (!isDev)
Eric Christopher9a9d2752010-07-22 02:48:34 +000011188 return DAG.getNode(X86ISD::MEMBARRIER, dl, MVT::Other, Op.getOperand(0));
Michael J. Spencerec38de22010-10-10 22:04:20 +000011189
Chris Lattner132929a2010-08-14 17:26:09 +000011190 unsigned Op1 = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
11191 unsigned Op2 = cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue();
11192 unsigned Op3 = cast<ConstantSDNode>(Op.getOperand(3))->getZExtValue();
11193 unsigned Op4 = cast<ConstantSDNode>(Op.getOperand(4))->getZExtValue();
Michael J. Spencerec38de22010-10-10 22:04:20 +000011194
Chris Lattner132929a2010-08-14 17:26:09 +000011195 // def : Pat<(membarrier (i8 0), (i8 0), (i8 0), (i8 1), (i8 1)), (SFENCE)>;
11196 if (!Op1 && !Op2 && !Op3 && Op4)
11197 return DAG.getNode(X86ISD::SFENCE, dl, MVT::Other, Op.getOperand(0));
Michael J. Spencerec38de22010-10-10 22:04:20 +000011198
Chris Lattner132929a2010-08-14 17:26:09 +000011199 // def : Pat<(membarrier (i8 1), (i8 0), (i8 0), (i8 0), (i8 1)), (LFENCE)>;
11200 if (Op1 && !Op2 && !Op3 && !Op4)
11201 return DAG.getNode(X86ISD::LFENCE, dl, MVT::Other, Op.getOperand(0));
Michael J. Spencerec38de22010-10-10 22:04:20 +000011202
11203 // def : Pat<(membarrier (i8 imm), (i8 imm), (i8 imm), (i8 imm), (i8 1)),
Chris Lattner132929a2010-08-14 17:26:09 +000011204 // (MFENCE)>;
11205 return DAG.getNode(X86ISD::MFENCE, dl, MVT::Other, Op.getOperand(0));
Eric Christopher9a9d2752010-07-22 02:48:34 +000011206}
11207
Craig Topper55b24052012-09-11 06:15:32 +000011208static SDValue LowerATOMIC_FENCE(SDValue Op, const X86Subtarget *Subtarget,
11209 SelectionDAG &DAG) {
Eli Friedman14648462011-07-27 22:21:52 +000011210 DebugLoc dl = Op.getDebugLoc();
11211 AtomicOrdering FenceOrdering = static_cast<AtomicOrdering>(
11212 cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue());
11213 SynchronizationScope FenceScope = static_cast<SynchronizationScope>(
11214 cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue());
11215
11216 // The only fence that needs an instruction is a sequentially-consistent
11217 // cross-thread fence.
11218 if (FenceOrdering == SequentiallyConsistent && FenceScope == CrossThread) {
11219 // Use mfence if we have SSE2 or we're on x86-64 (even if we asked for
11220 // no-sse2). There isn't any reason to disable it if the target processor
11221 // supports it.
Craig Topper1accb7e2012-01-10 06:54:16 +000011222 if (Subtarget->hasSSE2() || Subtarget->is64Bit())
Eli Friedman14648462011-07-27 22:21:52 +000011223 return DAG.getNode(X86ISD::MFENCE, dl, MVT::Other, Op.getOperand(0));
11224
11225 SDValue Chain = Op.getOperand(0);
11226 SDValue Zero = DAG.getConstant(0, MVT::i32);
11227 SDValue Ops[] = {
11228 DAG.getRegister(X86::ESP, MVT::i32), // Base
11229 DAG.getTargetConstant(1, MVT::i8), // Scale
11230 DAG.getRegister(0, MVT::i32), // Index
11231 DAG.getTargetConstant(0, MVT::i32), // Disp
11232 DAG.getRegister(0, MVT::i32), // Segment.
11233 Zero,
11234 Chain
11235 };
11236 SDNode *Res =
11237 DAG.getMachineNode(X86::OR32mrLocked, dl, MVT::Other, Ops,
11238 array_lengthof(Ops));
11239 return SDValue(Res, 0);
11240 }
11241
11242 // MEMBARRIER is a compiler barrier; it codegens to a no-op.
11243 return DAG.getNode(X86ISD::MEMBARRIER, dl, MVT::Other, Op.getOperand(0));
11244}
11245
11246
Craig Topper55b24052012-09-11 06:15:32 +000011247static SDValue LowerCMP_SWAP(SDValue Op, const X86Subtarget *Subtarget,
11248 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +000011249 EVT T = Op.getValueType();
Chris Lattner93c4a5b2010-09-21 23:59:42 +000011250 DebugLoc DL = Op.getDebugLoc();
Andrew Lenhartha76e2f02008-03-04 21:13:33 +000011251 unsigned Reg = 0;
11252 unsigned size = 0;
Owen Anderson825b72b2009-08-11 20:47:22 +000011253 switch(T.getSimpleVT().SimpleTy) {
Craig Topperabb94d02012-02-05 03:43:23 +000011254 default: llvm_unreachable("Invalid value type!");
Owen Anderson825b72b2009-08-11 20:47:22 +000011255 case MVT::i8: Reg = X86::AL; size = 1; break;
11256 case MVT::i16: Reg = X86::AX; size = 2; break;
11257 case MVT::i32: Reg = X86::EAX; size = 4; break;
11258 case MVT::i64:
Duncan Sands1607f052008-12-01 11:39:25 +000011259 assert(Subtarget->is64Bit() && "Node not type legal!");
11260 Reg = X86::RAX; size = 8;
Andrew Lenharthd19189e2008-03-05 01:15:49 +000011261 break;
Bill Wendling61edeb52008-12-02 01:06:39 +000011262 }
Chris Lattner93c4a5b2010-09-21 23:59:42 +000011263 SDValue cpIn = DAG.getCopyToReg(Op.getOperand(0), DL, Reg,
Dale Johannesend18a4622008-09-11 03:12:59 +000011264 Op.getOperand(2), SDValue());
Dan Gohman475871a2008-07-27 21:46:04 +000011265 SDValue Ops[] = { cpIn.getValue(0),
Evan Cheng8a186ae2008-09-24 23:26:36 +000011266 Op.getOperand(1),
11267 Op.getOperand(3),
Owen Anderson825b72b2009-08-11 20:47:22 +000011268 DAG.getTargetConstant(size, MVT::i8),
Evan Cheng8a186ae2008-09-24 23:26:36 +000011269 cpIn.getValue(1) };
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +000011270 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Chris Lattner93c4a5b2010-09-21 23:59:42 +000011271 MachineMemOperand *MMO = cast<AtomicSDNode>(Op)->getMemOperand();
11272 SDValue Result = DAG.getMemIntrinsicNode(X86ISD::LCMPXCHG_DAG, DL, Tys,
11273 Ops, 5, T, MMO);
Scott Michelfdc40a02009-02-17 22:15:04 +000011274 SDValue cpOut =
Chris Lattner93c4a5b2010-09-21 23:59:42 +000011275 DAG.getCopyFromReg(Result.getValue(0), DL, Reg, T, Result.getValue(1));
Andrew Lenharth26ed8692008-03-01 21:52:34 +000011276 return cpOut;
11277}
11278
Craig Topper55b24052012-09-11 06:15:32 +000011279static SDValue LowerREADCYCLECOUNTER(SDValue Op, const X86Subtarget *Subtarget,
11280 SelectionDAG &DAG) {
Duncan Sands1607f052008-12-01 11:39:25 +000011281 assert(Subtarget->is64Bit() && "Result not type legalized?");
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +000011282 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Duncan Sands1607f052008-12-01 11:39:25 +000011283 SDValue TheChain = Op.getOperand(0);
Dale Johannesen6f38cb62009-02-07 19:59:05 +000011284 DebugLoc dl = Op.getDebugLoc();
Dale Johannesene4d209d2009-02-03 20:21:25 +000011285 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, dl, Tys, &TheChain, 1);
Owen Anderson825b72b2009-08-11 20:47:22 +000011286 SDValue rax = DAG.getCopyFromReg(rd, dl, X86::RAX, MVT::i64, rd.getValue(1));
11287 SDValue rdx = DAG.getCopyFromReg(rax.getValue(1), dl, X86::RDX, MVT::i64,
Duncan Sands1607f052008-12-01 11:39:25 +000011288 rax.getValue(2));
Owen Anderson825b72b2009-08-11 20:47:22 +000011289 SDValue Tmp = DAG.getNode(ISD::SHL, dl, MVT::i64, rdx,
11290 DAG.getConstant(32, MVT::i8));
Duncan Sands1607f052008-12-01 11:39:25 +000011291 SDValue Ops[] = {
Owen Anderson825b72b2009-08-11 20:47:22 +000011292 DAG.getNode(ISD::OR, dl, MVT::i64, rax, Tmp),
Duncan Sands1607f052008-12-01 11:39:25 +000011293 rdx.getValue(1)
11294 };
Dale Johannesene4d209d2009-02-03 20:21:25 +000011295 return DAG.getMergeValues(Ops, 2, dl);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011296}
11297
Craig Topper55b24052012-09-11 06:15:32 +000011298SDValue X86TargetLowering::LowerBITCAST(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen7d07b482010-05-21 00:52:33 +000011299 EVT SrcVT = Op.getOperand(0).getValueType();
11300 EVT DstVT = Op.getValueType();
Craig Topper1accb7e2012-01-10 06:54:16 +000011301 assert(Subtarget->is64Bit() && !Subtarget->hasSSE2() &&
Chris Lattner2a786eb2010-12-19 20:19:20 +000011302 Subtarget->hasMMX() && "Unexpected custom BITCAST");
Michael J. Spencerec38de22010-10-10 22:04:20 +000011303 assert((DstVT == MVT::i64 ||
Dale Johannesen7d07b482010-05-21 00:52:33 +000011304 (DstVT.isVector() && DstVT.getSizeInBits()==64)) &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +000011305 "Unexpected custom BITCAST");
Dale Johannesen7d07b482010-05-21 00:52:33 +000011306 // i64 <=> MMX conversions are Legal.
11307 if (SrcVT==MVT::i64 && DstVT.isVector())
11308 return Op;
11309 if (DstVT==MVT::i64 && SrcVT.isVector())
11310 return Op;
Dale Johannesene39859a2010-05-21 18:40:15 +000011311 // MMX <=> MMX conversions are Legal.
11312 if (SrcVT.isVector() && DstVT.isVector())
11313 return Op;
Dale Johannesen7d07b482010-05-21 00:52:33 +000011314 // All other conversions need to be expanded.
11315 return SDValue();
11316}
Chris Lattner5b856542010-12-20 00:59:46 +000011317
Craig Topper55b24052012-09-11 06:15:32 +000011318static SDValue LowerLOAD_SUB(SDValue Op, SelectionDAG &DAG) {
Dale Johannesen71d1bf52008-09-29 22:25:26 +000011319 SDNode *Node = Op.getNode();
Dale Johannesene4d209d2009-02-03 20:21:25 +000011320 DebugLoc dl = Node->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +000011321 EVT T = Node->getValueType(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +000011322 SDValue negOp = DAG.getNode(ISD::SUB, dl, T,
Evan Cheng242b38b2009-02-23 09:03:22 +000011323 DAG.getConstant(0, T), Node->getOperand(2));
Dale Johannesene4d209d2009-02-03 20:21:25 +000011324 return DAG.getAtomic(ISD::ATOMIC_LOAD_ADD, dl,
Dan Gohman0b1d4a72008-12-23 21:37:04 +000011325 cast<AtomicSDNode>(Node)->getMemoryVT(),
Dale Johannesen71d1bf52008-09-29 22:25:26 +000011326 Node->getOperand(0),
11327 Node->getOperand(1), negOp,
11328 cast<AtomicSDNode>(Node)->getSrcValue(),
Eli Friedman55ba8162011-07-29 03:05:32 +000011329 cast<AtomicSDNode>(Node)->getAlignment(),
11330 cast<AtomicSDNode>(Node)->getOrdering(),
11331 cast<AtomicSDNode>(Node)->getSynchScope());
Mon P Wang63307c32008-05-05 19:05:59 +000011332}
11333
Eli Friedman327236c2011-08-24 20:50:09 +000011334static SDValue LowerATOMIC_STORE(SDValue Op, SelectionDAG &DAG) {
11335 SDNode *Node = Op.getNode();
11336 DebugLoc dl = Node->getDebugLoc();
Eli Friedmanf8f90f02011-08-24 22:33:28 +000011337 EVT VT = cast<AtomicSDNode>(Node)->getMemoryVT();
Eli Friedman327236c2011-08-24 20:50:09 +000011338
11339 // Convert seq_cst store -> xchg
Eli Friedmanf8f90f02011-08-24 22:33:28 +000011340 // Convert wide store -> swap (-> cmpxchg8b/cmpxchg16b)
11341 // FIXME: On 32-bit, store -> fist or movq would be more efficient
11342 // (The only way to get a 16-byte store is cmpxchg16b)
11343 // FIXME: 16-byte ATOMIC_SWAP isn't actually hooked up at the moment.
11344 if (cast<AtomicSDNode>(Node)->getOrdering() == SequentiallyConsistent ||
11345 !DAG.getTargetLoweringInfo().isTypeLegal(VT)) {
Eli Friedman4317fe12011-08-24 21:17:30 +000011346 SDValue Swap = DAG.getAtomic(ISD::ATOMIC_SWAP, dl,
11347 cast<AtomicSDNode>(Node)->getMemoryVT(),
11348 Node->getOperand(0),
11349 Node->getOperand(1), Node->getOperand(2),
Eli Friedmanf8f90f02011-08-24 22:33:28 +000011350 cast<AtomicSDNode>(Node)->getMemOperand(),
Eli Friedman4317fe12011-08-24 21:17:30 +000011351 cast<AtomicSDNode>(Node)->getOrdering(),
11352 cast<AtomicSDNode>(Node)->getSynchScope());
Eli Friedman327236c2011-08-24 20:50:09 +000011353 return Swap.getValue(1);
11354 }
11355 // Other atomic stores have a simple pattern.
11356 return Op;
11357}
11358
Chris Lattner5b856542010-12-20 00:59:46 +000011359static SDValue LowerADDC_ADDE_SUBC_SUBE(SDValue Op, SelectionDAG &DAG) {
11360 EVT VT = Op.getNode()->getValueType(0);
11361
11362 // Let legalize expand this if it isn't a legal type yet.
11363 if (!DAG.getTargetLoweringInfo().isTypeLegal(VT))
11364 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011365
Chris Lattner5b856542010-12-20 00:59:46 +000011366 SDVTList VTs = DAG.getVTList(VT, MVT::i32);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011367
Chris Lattner5b856542010-12-20 00:59:46 +000011368 unsigned Opc;
11369 bool ExtraOp = false;
11370 switch (Op.getOpcode()) {
Craig Topperabb94d02012-02-05 03:43:23 +000011371 default: llvm_unreachable("Invalid code");
Chris Lattner5b856542010-12-20 00:59:46 +000011372 case ISD::ADDC: Opc = X86ISD::ADD; break;
11373 case ISD::ADDE: Opc = X86ISD::ADC; ExtraOp = true; break;
11374 case ISD::SUBC: Opc = X86ISD::SUB; break;
11375 case ISD::SUBE: Opc = X86ISD::SBB; ExtraOp = true; break;
11376 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011377
Chris Lattner5b856542010-12-20 00:59:46 +000011378 if (!ExtraOp)
11379 return DAG.getNode(Opc, Op->getDebugLoc(), VTs, Op.getOperand(0),
11380 Op.getOperand(1));
11381 return DAG.getNode(Opc, Op->getDebugLoc(), VTs, Op.getOperand(0),
11382 Op.getOperand(1), Op.getOperand(2));
11383}
11384
Evan Cheng0db9fe62006-04-25 20:13:52 +000011385/// LowerOperation - Provide custom lowering hooks for some operations.
11386///
Dan Gohmand858e902010-04-17 15:26:15 +000011387SDValue X86TargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +000011388 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +000011389 default: llvm_unreachable("Should not custom lower this!");
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000011390 case ISD::SIGN_EXTEND_INREG: return LowerSIGN_EXTEND_INREG(Op,DAG);
Craig Topper55b24052012-09-11 06:15:32 +000011391 case ISD::MEMBARRIER: return LowerMEMBARRIER(Op, Subtarget, DAG);
11392 case ISD::ATOMIC_FENCE: return LowerATOMIC_FENCE(Op, Subtarget, DAG);
11393 case ISD::ATOMIC_CMP_SWAP: return LowerCMP_SWAP(Op, Subtarget, DAG);
Dan Gohman0b1d4a72008-12-23 21:37:04 +000011394 case ISD::ATOMIC_LOAD_SUB: return LowerLOAD_SUB(Op,DAG);
Eli Friedman327236c2011-08-24 20:50:09 +000011395 case ISD::ATOMIC_STORE: return LowerATOMIC_STORE(Op,DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000011396 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
Mon P Wangeb38ebf2010-01-24 00:05:03 +000011397 case ISD::CONCAT_VECTORS: return LowerCONCAT_VECTORS(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000011398 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
11399 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
11400 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
Craig Topper55b24052012-09-11 06:15:32 +000011401 case ISD::EXTRACT_SUBVECTOR: return LowerEXTRACT_SUBVECTOR(Op,Subtarget,DAG);
11402 case ISD::INSERT_SUBVECTOR: return LowerINSERT_SUBVECTOR(Op, Subtarget,DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000011403 case ISD::SCALAR_TO_VECTOR: return LowerSCALAR_TO_VECTOR(Op, DAG);
11404 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
11405 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +000011406 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Bill Wendling056292f2008-09-16 21:48:12 +000011407 case ISD::ExternalSymbol: return LowerExternalSymbol(Op, DAG);
Dan Gohmanf705adb2009-10-30 01:28:02 +000011408 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000011409 case ISD::SHL_PARTS:
11410 case ISD::SRA_PARTS:
Nadav Rotem43012222011-05-11 08:12:09 +000011411 case ISD::SRL_PARTS: return LowerShiftParts(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000011412 case ISD::SINT_TO_FP: return LowerSINT_TO_FP(Op, DAG);
Dale Johannesen1c15bf52008-10-21 20:50:01 +000011413 case ISD::UINT_TO_FP: return LowerUINT_TO_FP(Op, DAG);
Michael Liaobedcbd42012-10-16 18:14:11 +000011414 case ISD::TRUNCATE: return lowerTRUNCATE(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000011415 case ISD::FP_TO_SINT: return LowerFP_TO_SINT(Op, DAG);
Eli Friedman948e95a2009-05-23 09:59:16 +000011416 case ISD::FP_TO_UINT: return LowerFP_TO_UINT(Op, DAG);
Michael Liao9d796db2012-10-10 16:32:15 +000011417 case ISD::FP_EXTEND: return lowerFP_EXTEND(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000011418 case ISD::FABS: return LowerFABS(Op, DAG);
11419 case ISD::FNEG: return LowerFNEG(Op, DAG);
Evan Cheng68c47cb2007-01-05 07:55:56 +000011420 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Stuart Hastings4fd0dee2011-06-01 04:39:42 +000011421 case ISD::FGETSIGN: return LowerFGETSIGN(Op, DAG);
Evan Chenge5f62042007-09-29 00:00:36 +000011422 case ISD::SETCC: return LowerSETCC(Op, DAG);
11423 case ISD::SELECT: return LowerSELECT(Op, DAG);
11424 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000011425 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000011426 case ISD::VASTART: return LowerVASTART(Op, DAG);
Dan Gohman9018e832008-05-10 01:26:14 +000011427 case ISD::VAARG: return LowerVAARG(Op, DAG);
Craig Topper55b24052012-09-11 06:15:32 +000011428 case ISD::VACOPY: return LowerVACOPY(Op, Subtarget, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000011429 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
Benjamin Kramerb9bee042012-07-12 09:31:43 +000011430 case ISD::INTRINSIC_W_CHAIN: return LowerINTRINSIC_W_CHAIN(Op, DAG);
Nate Begemanbcc5f362007-01-29 22:58:52 +000011431 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
11432 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Anton Korobeynikov2365f512007-07-14 14:06:15 +000011433 case ISD::FRAME_TO_ARGS_OFFSET:
11434 return LowerFRAME_TO_ARGS_OFFSET(Op, DAG);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +000011435 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
Anton Korobeynikov2365f512007-07-14 14:06:15 +000011436 case ISD::EH_RETURN: return LowerEH_RETURN(Op, DAG);
Michael Liao6c0e04c2012-10-15 22:39:43 +000011437 case ISD::EH_SJLJ_SETJMP: return lowerEH_SJLJ_SETJMP(Op, DAG);
11438 case ISD::EH_SJLJ_LONGJMP: return lowerEH_SJLJ_LONGJMP(Op, DAG);
Duncan Sands4a544a72011-09-06 13:37:06 +000011439 case ISD::INIT_TRAMPOLINE: return LowerINIT_TRAMPOLINE(Op, DAG);
11440 case ISD::ADJUST_TRAMPOLINE: return LowerADJUST_TRAMPOLINE(Op, DAG);
Dan Gohman1a024862008-01-31 00:41:03 +000011441 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
Evan Cheng18efe262007-12-14 02:13:44 +000011442 case ISD::CTLZ: return LowerCTLZ(Op, DAG);
Chandler Carruthacc068e2011-12-24 10:55:54 +000011443 case ISD::CTLZ_ZERO_UNDEF: return LowerCTLZ_ZERO_UNDEF(Op, DAG);
Evan Cheng18efe262007-12-14 02:13:44 +000011444 case ISD::CTTZ: return LowerCTTZ(Op, DAG);
Craig Topper55b24052012-09-11 06:15:32 +000011445 case ISD::MUL: return LowerMUL(Op, Subtarget, DAG);
Nadav Rotem43012222011-05-11 08:12:09 +000011446 case ISD::SRA:
11447 case ISD::SRL:
11448 case ISD::SHL: return LowerShift(Op, DAG);
Bill Wendling74c37652008-12-09 22:08:41 +000011449 case ISD::SADDO:
11450 case ISD::UADDO:
11451 case ISD::SSUBO:
11452 case ISD::USUBO:
11453 case ISD::SMULO:
11454 case ISD::UMULO: return LowerXALUO(Op, DAG);
Craig Topper55b24052012-09-11 06:15:32 +000011455 case ISD::READCYCLECOUNTER: return LowerREADCYCLECOUNTER(Op, Subtarget,DAG);
Wesley Peckbf17cfa2010-11-23 03:31:01 +000011456 case ISD::BITCAST: return LowerBITCAST(Op, DAG);
Chris Lattner5b856542010-12-20 00:59:46 +000011457 case ISD::ADDC:
11458 case ISD::ADDE:
11459 case ISD::SUBC:
11460 case ISD::SUBE: return LowerADDC_ADDE_SUBC_SUBE(Op, DAG);
Craig Topper13894fa2011-08-24 06:14:18 +000011461 case ISD::ADD: return LowerADD(Op, DAG);
11462 case ISD::SUB: return LowerSUB(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000011463 }
Chris Lattner27a6c732007-11-24 07:07:01 +000011464}
11465
Eli Friedmanf8f90f02011-08-24 22:33:28 +000011466static void ReplaceATOMIC_LOAD(SDNode *Node,
11467 SmallVectorImpl<SDValue> &Results,
11468 SelectionDAG &DAG) {
11469 DebugLoc dl = Node->getDebugLoc();
11470 EVT VT = cast<AtomicSDNode>(Node)->getMemoryVT();
11471
11472 // Convert wide load -> cmpxchg8b/cmpxchg16b
11473 // FIXME: On 32-bit, load -> fild or movq would be more efficient
11474 // (The only way to get a 16-byte load is cmpxchg16b)
11475 // FIXME: 16-byte ATOMIC_CMP_SWAP isn't actually hooked up at the moment.
Benjamin Kramer2753ae32011-08-27 17:36:14 +000011476 SDValue Zero = DAG.getConstant(0, VT);
11477 SDValue Swap = DAG.getAtomic(ISD::ATOMIC_CMP_SWAP, dl, VT,
Eli Friedmanf8f90f02011-08-24 22:33:28 +000011478 Node->getOperand(0),
11479 Node->getOperand(1), Zero, Zero,
11480 cast<AtomicSDNode>(Node)->getMemOperand(),
11481 cast<AtomicSDNode>(Node)->getOrdering(),
11482 cast<AtomicSDNode>(Node)->getSynchScope());
11483 Results.push_back(Swap.getValue(0));
11484 Results.push_back(Swap.getValue(1));
11485}
11486
Craig Topperc0878702012-08-17 06:55:11 +000011487static void
Duncan Sands1607f052008-12-01 11:39:25 +000011488ReplaceATOMIC_BINARY_64(SDNode *Node, SmallVectorImpl<SDValue>&Results,
Craig Topperc0878702012-08-17 06:55:11 +000011489 SelectionDAG &DAG, unsigned NewOp) {
Dale Johannesene4d209d2009-02-03 20:21:25 +000011490 DebugLoc dl = Node->getDebugLoc();
Duncan Sands17001ce2011-10-18 12:44:00 +000011491 assert (Node->getValueType(0) == MVT::i64 &&
11492 "Only know how to expand i64 atomics");
Duncan Sands1607f052008-12-01 11:39:25 +000011493
11494 SDValue Chain = Node->getOperand(0);
11495 SDValue In1 = Node->getOperand(1);
Owen Anderson825b72b2009-08-11 20:47:22 +000011496 SDValue In2L = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +000011497 Node->getOperand(2), DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +000011498 SDValue In2H = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +000011499 Node->getOperand(2), DAG.getIntPtrConstant(1));
Dan Gohmanc76909a2009-09-25 20:36:54 +000011500 SDValue Ops[] = { Chain, In1, In2L, In2H };
Owen Anderson825b72b2009-08-11 20:47:22 +000011501 SDVTList Tys = DAG.getVTList(MVT::i32, MVT::i32, MVT::Other);
Dan Gohmanc76909a2009-09-25 20:36:54 +000011502 SDValue Result =
11503 DAG.getMemIntrinsicNode(NewOp, dl, Tys, Ops, 4, MVT::i64,
11504 cast<MemSDNode>(Node)->getMemOperand());
Duncan Sands1607f052008-12-01 11:39:25 +000011505 SDValue OpsF[] = { Result.getValue(0), Result.getValue(1)};
Owen Anderson825b72b2009-08-11 20:47:22 +000011506 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, OpsF, 2));
Duncan Sands1607f052008-12-01 11:39:25 +000011507 Results.push_back(Result.getValue(2));
11508}
11509
Duncan Sands126d9072008-07-04 11:47:58 +000011510/// ReplaceNodeResults - Replace a node with an illegal result type
11511/// with a new node built out of custom code.
Duncan Sands1607f052008-12-01 11:39:25 +000011512void X86TargetLowering::ReplaceNodeResults(SDNode *N,
11513 SmallVectorImpl<SDValue>&Results,
Dan Gohmand858e902010-04-17 15:26:15 +000011514 SelectionDAG &DAG) const {
Dale Johannesene4d209d2009-02-03 20:21:25 +000011515 DebugLoc dl = N->getDebugLoc();
Chris Lattner27a6c732007-11-24 07:07:01 +000011516 switch (N->getOpcode()) {
Duncan Sandsed294c42008-10-20 15:56:33 +000011517 default:
Craig Topperabb94d02012-02-05 03:43:23 +000011518 llvm_unreachable("Do not know how to custom type legalize this operation!");
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000011519 case ISD::SIGN_EXTEND_INREG:
Chris Lattner5b856542010-12-20 00:59:46 +000011520 case ISD::ADDC:
11521 case ISD::ADDE:
11522 case ISD::SUBC:
11523 case ISD::SUBE:
11524 // We don't want to expand or promote these.
11525 return;
Michael J. Spencer1a2d0612012-02-24 19:01:22 +000011526 case ISD::FP_TO_SINT:
11527 case ISD::FP_TO_UINT: {
11528 bool IsSigned = N->getOpcode() == ISD::FP_TO_SINT;
11529
11530 if (!IsSigned && !isIntegerTypeFTOL(SDValue(N, 0).getValueType()))
11531 return;
11532
Eli Friedman948e95a2009-05-23 09:59:16 +000011533 std::pair<SDValue,SDValue> Vals =
NAKAMURA Takumi9a68fdc2012-02-25 03:37:25 +000011534 FP_TO_INTHelper(SDValue(N, 0), DAG, IsSigned, /*IsReplace=*/ true);
Duncan Sands1607f052008-12-01 11:39:25 +000011535 SDValue FIST = Vals.first, StackSlot = Vals.second;
11536 if (FIST.getNode() != 0) {
Owen Andersone50ed302009-08-10 22:56:29 +000011537 EVT VT = N->getValueType(0);
Duncan Sands1607f052008-12-01 11:39:25 +000011538 // Return a load from the stack slot.
Michael J. Spencer1a2d0612012-02-24 19:01:22 +000011539 if (StackSlot.getNode() != 0)
11540 Results.push_back(DAG.getLoad(VT, dl, FIST, StackSlot,
11541 MachinePointerInfo(),
11542 false, false, false, 0));
11543 else
11544 Results.push_back(FIST);
Duncan Sands1607f052008-12-01 11:39:25 +000011545 }
11546 return;
11547 }
Michael Liao44c2d612012-10-10 16:53:28 +000011548 case ISD::FP_ROUND: {
11549 SDValue V = DAG.getNode(X86ISD::VFPROUND, dl, MVT::v4f32, N->getOperand(0));
11550 Results.push_back(V);
11551 return;
11552 }
Duncan Sands1607f052008-12-01 11:39:25 +000011553 case ISD::READCYCLECOUNTER: {
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +000011554 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Duncan Sands1607f052008-12-01 11:39:25 +000011555 SDValue TheChain = N->getOperand(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +000011556 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, dl, Tys, &TheChain, 1);
Owen Anderson825b72b2009-08-11 20:47:22 +000011557 SDValue eax = DAG.getCopyFromReg(rd, dl, X86::EAX, MVT::i32,
Dale Johannesendd64c412009-02-04 00:33:20 +000011558 rd.getValue(1));
Owen Anderson825b72b2009-08-11 20:47:22 +000011559 SDValue edx = DAG.getCopyFromReg(eax.getValue(1), dl, X86::EDX, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +000011560 eax.getValue(2));
11561 // Use a buildpair to merge the two 32-bit values into a 64-bit one.
11562 SDValue Ops[] = { eax, edx };
Owen Anderson825b72b2009-08-11 20:47:22 +000011563 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Ops, 2));
Duncan Sands1607f052008-12-01 11:39:25 +000011564 Results.push_back(edx.getValue(1));
11565 return;
11566 }
Dan Gohman0b1d4a72008-12-23 21:37:04 +000011567 case ISD::ATOMIC_CMP_SWAP: {
Owen Andersone50ed302009-08-10 22:56:29 +000011568 EVT T = N->getValueType(0);
Benjamin Kramer2753ae32011-08-27 17:36:14 +000011569 assert((T == MVT::i64 || T == MVT::i128) && "can only expand cmpxchg pair");
Eli Friedman43f51ae2011-08-26 21:21:21 +000011570 bool Regs64bit = T == MVT::i128;
11571 EVT HalfT = Regs64bit ? MVT::i64 : MVT::i32;
Duncan Sands1607f052008-12-01 11:39:25 +000011572 SDValue cpInL, cpInH;
Eli Friedman43f51ae2011-08-26 21:21:21 +000011573 cpInL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, HalfT, N->getOperand(2),
11574 DAG.getConstant(0, HalfT));
11575 cpInH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, HalfT, N->getOperand(2),
11576 DAG.getConstant(1, HalfT));
11577 cpInL = DAG.getCopyToReg(N->getOperand(0), dl,
11578 Regs64bit ? X86::RAX : X86::EAX,
11579 cpInL, SDValue());
11580 cpInH = DAG.getCopyToReg(cpInL.getValue(0), dl,
11581 Regs64bit ? X86::RDX : X86::EDX,
11582 cpInH, cpInL.getValue(1));
Duncan Sands1607f052008-12-01 11:39:25 +000011583 SDValue swapInL, swapInH;
Eli Friedman43f51ae2011-08-26 21:21:21 +000011584 swapInL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, HalfT, N->getOperand(3),
11585 DAG.getConstant(0, HalfT));
11586 swapInH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, HalfT, N->getOperand(3),
11587 DAG.getConstant(1, HalfT));
11588 swapInL = DAG.getCopyToReg(cpInH.getValue(0), dl,
11589 Regs64bit ? X86::RBX : X86::EBX,
11590 swapInL, cpInH.getValue(1));
11591 swapInH = DAG.getCopyToReg(swapInL.getValue(0), dl,
Chad Rosiera20e1e72012-08-01 18:39:17 +000011592 Regs64bit ? X86::RCX : X86::ECX,
Eli Friedman43f51ae2011-08-26 21:21:21 +000011593 swapInH, swapInL.getValue(1));
Duncan Sands1607f052008-12-01 11:39:25 +000011594 SDValue Ops[] = { swapInH.getValue(0),
11595 N->getOperand(1),
11596 swapInH.getValue(1) };
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +000011597 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Andrew Trick1a2cf3b2010-10-11 19:02:04 +000011598 MachineMemOperand *MMO = cast<AtomicSDNode>(N)->getMemOperand();
Eli Friedman43f51ae2011-08-26 21:21:21 +000011599 unsigned Opcode = Regs64bit ? X86ISD::LCMPXCHG16_DAG :
11600 X86ISD::LCMPXCHG8_DAG;
11601 SDValue Result = DAG.getMemIntrinsicNode(Opcode, dl, Tys,
Andrew Trick1a2cf3b2010-10-11 19:02:04 +000011602 Ops, 3, T, MMO);
Eli Friedman43f51ae2011-08-26 21:21:21 +000011603 SDValue cpOutL = DAG.getCopyFromReg(Result.getValue(0), dl,
11604 Regs64bit ? X86::RAX : X86::EAX,
11605 HalfT, Result.getValue(1));
11606 SDValue cpOutH = DAG.getCopyFromReg(cpOutL.getValue(1), dl,
11607 Regs64bit ? X86::RDX : X86::EDX,
11608 HalfT, cpOutL.getValue(2));
Duncan Sands1607f052008-12-01 11:39:25 +000011609 SDValue OpsF[] = { cpOutL.getValue(0), cpOutH.getValue(0)};
Eli Friedman43f51ae2011-08-26 21:21:21 +000011610 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, T, OpsF, 2));
Duncan Sands1607f052008-12-01 11:39:25 +000011611 Results.push_back(cpOutH.getValue(1));
11612 return;
11613 }
Dan Gohman0b1d4a72008-12-23 21:37:04 +000011614 case ISD::ATOMIC_LOAD_ADD:
Dan Gohman0b1d4a72008-12-23 21:37:04 +000011615 case ISD::ATOMIC_LOAD_AND:
Dan Gohman0b1d4a72008-12-23 21:37:04 +000011616 case ISD::ATOMIC_LOAD_NAND:
Dan Gohman0b1d4a72008-12-23 21:37:04 +000011617 case ISD::ATOMIC_LOAD_OR:
Dan Gohman0b1d4a72008-12-23 21:37:04 +000011618 case ISD::ATOMIC_LOAD_SUB:
Dan Gohman0b1d4a72008-12-23 21:37:04 +000011619 case ISD::ATOMIC_LOAD_XOR:
Michael Liaoe5e8f762012-09-25 18:08:13 +000011620 case ISD::ATOMIC_LOAD_MAX:
11621 case ISD::ATOMIC_LOAD_MIN:
11622 case ISD::ATOMIC_LOAD_UMAX:
11623 case ISD::ATOMIC_LOAD_UMIN:
Craig Topperc0878702012-08-17 06:55:11 +000011624 case ISD::ATOMIC_SWAP: {
11625 unsigned Opc;
11626 switch (N->getOpcode()) {
11627 default: llvm_unreachable("Unexpected opcode");
11628 case ISD::ATOMIC_LOAD_ADD:
11629 Opc = X86ISD::ATOMADD64_DAG;
11630 break;
11631 case ISD::ATOMIC_LOAD_AND:
11632 Opc = X86ISD::ATOMAND64_DAG;
11633 break;
11634 case ISD::ATOMIC_LOAD_NAND:
11635 Opc = X86ISD::ATOMNAND64_DAG;
11636 break;
11637 case ISD::ATOMIC_LOAD_OR:
11638 Opc = X86ISD::ATOMOR64_DAG;
11639 break;
11640 case ISD::ATOMIC_LOAD_SUB:
11641 Opc = X86ISD::ATOMSUB64_DAG;
11642 break;
11643 case ISD::ATOMIC_LOAD_XOR:
11644 Opc = X86ISD::ATOMXOR64_DAG;
11645 break;
Michael Liaoe5e8f762012-09-25 18:08:13 +000011646 case ISD::ATOMIC_LOAD_MAX:
11647 Opc = X86ISD::ATOMMAX64_DAG;
11648 break;
11649 case ISD::ATOMIC_LOAD_MIN:
11650 Opc = X86ISD::ATOMMIN64_DAG;
11651 break;
11652 case ISD::ATOMIC_LOAD_UMAX:
11653 Opc = X86ISD::ATOMUMAX64_DAG;
11654 break;
11655 case ISD::ATOMIC_LOAD_UMIN:
11656 Opc = X86ISD::ATOMUMIN64_DAG;
11657 break;
Craig Topperc0878702012-08-17 06:55:11 +000011658 case ISD::ATOMIC_SWAP:
11659 Opc = X86ISD::ATOMSWAP64_DAG;
11660 break;
11661 }
11662 ReplaceATOMIC_BINARY_64(N, Results, DAG, Opc);
Duncan Sands1607f052008-12-01 11:39:25 +000011663 return;
Craig Topperc0878702012-08-17 06:55:11 +000011664 }
Eli Friedmanf8f90f02011-08-24 22:33:28 +000011665 case ISD::ATOMIC_LOAD:
11666 ReplaceATOMIC_LOAD(N, Results, DAG);
Chris Lattner27a6c732007-11-24 07:07:01 +000011667 }
Evan Cheng0db9fe62006-04-25 20:13:52 +000011668}
11669
Evan Cheng72261582005-12-20 06:22:03 +000011670const char *X86TargetLowering::getTargetNodeName(unsigned Opcode) const {
11671 switch (Opcode) {
11672 default: return NULL;
Evan Cheng18efe262007-12-14 02:13:44 +000011673 case X86ISD::BSF: return "X86ISD::BSF";
11674 case X86ISD::BSR: return "X86ISD::BSR";
Evan Chenge3413162006-01-09 18:33:28 +000011675 case X86ISD::SHLD: return "X86ISD::SHLD";
11676 case X86ISD::SHRD: return "X86ISD::SHRD";
Evan Chengef6ffb12006-01-31 03:14:29 +000011677 case X86ISD::FAND: return "X86ISD::FAND";
Evan Cheng68c47cb2007-01-05 07:55:56 +000011678 case X86ISD::FOR: return "X86ISD::FOR";
Evan Cheng223547a2006-01-31 22:28:30 +000011679 case X86ISD::FXOR: return "X86ISD::FXOR";
Evan Cheng68c47cb2007-01-05 07:55:56 +000011680 case X86ISD::FSRL: return "X86ISD::FSRL";
Evan Chenga3195e82006-01-12 22:54:21 +000011681 case X86ISD::FILD: return "X86ISD::FILD";
Evan Chenge3de85b2006-02-04 02:20:30 +000011682 case X86ISD::FILD_FLAG: return "X86ISD::FILD_FLAG";
Evan Cheng72261582005-12-20 06:22:03 +000011683 case X86ISD::FP_TO_INT16_IN_MEM: return "X86ISD::FP_TO_INT16_IN_MEM";
11684 case X86ISD::FP_TO_INT32_IN_MEM: return "X86ISD::FP_TO_INT32_IN_MEM";
11685 case X86ISD::FP_TO_INT64_IN_MEM: return "X86ISD::FP_TO_INT64_IN_MEM";
Evan Chengb077b842005-12-21 02:39:21 +000011686 case X86ISD::FLD: return "X86ISD::FLD";
Evan Chengd90eb7f2006-01-05 00:27:02 +000011687 case X86ISD::FST: return "X86ISD::FST";
Evan Cheng72261582005-12-20 06:22:03 +000011688 case X86ISD::CALL: return "X86ISD::CALL";
Evan Cheng72261582005-12-20 06:22:03 +000011689 case X86ISD::RDTSC_DAG: return "X86ISD::RDTSC_DAG";
Dan Gohmanc7a37d42008-12-23 22:45:23 +000011690 case X86ISD::BT: return "X86ISD::BT";
Evan Cheng72261582005-12-20 06:22:03 +000011691 case X86ISD::CMP: return "X86ISD::CMP";
Evan Cheng6be2c582006-04-05 23:38:46 +000011692 case X86ISD::COMI: return "X86ISD::COMI";
11693 case X86ISD::UCOMI: return "X86ISD::UCOMI";
Evan Chengd5781fc2005-12-21 20:21:51 +000011694 case X86ISD::SETCC: return "X86ISD::SETCC";
Evan Chengad9c0a32009-12-15 00:53:42 +000011695 case X86ISD::SETCC_CARRY: return "X86ISD::SETCC_CARRY";
Stuart Hastings865f0932011-06-03 23:53:54 +000011696 case X86ISD::FSETCCsd: return "X86ISD::FSETCCsd";
11697 case X86ISD::FSETCCss: return "X86ISD::FSETCCss";
Evan Cheng72261582005-12-20 06:22:03 +000011698 case X86ISD::CMOV: return "X86ISD::CMOV";
11699 case X86ISD::BRCOND: return "X86ISD::BRCOND";
Evan Chengb077b842005-12-21 02:39:21 +000011700 case X86ISD::RET_FLAG: return "X86ISD::RET_FLAG";
Evan Cheng8df346b2006-03-04 01:12:00 +000011701 case X86ISD::REP_STOS: return "X86ISD::REP_STOS";
11702 case X86ISD::REP_MOVS: return "X86ISD::REP_MOVS";
Evan Cheng7ccced62006-02-18 00:15:05 +000011703 case X86ISD::GlobalBaseReg: return "X86ISD::GlobalBaseReg";
Evan Cheng020d2e82006-02-23 20:41:18 +000011704 case X86ISD::Wrapper: return "X86ISD::Wrapper";
Chris Lattner18c59872009-06-27 04:16:01 +000011705 case X86ISD::WrapperRIP: return "X86ISD::WrapperRIP";
Nate Begeman14d12ca2008-02-11 04:19:36 +000011706 case X86ISD::PEXTRB: return "X86ISD::PEXTRB";
Evan Chengb067a1e2006-03-31 19:22:53 +000011707 case X86ISD::PEXTRW: return "X86ISD::PEXTRW";
Nate Begeman14d12ca2008-02-11 04:19:36 +000011708 case X86ISD::INSERTPS: return "X86ISD::INSERTPS";
11709 case X86ISD::PINSRB: return "X86ISD::PINSRB";
Evan Cheng653159f2006-03-31 21:55:24 +000011710 case X86ISD::PINSRW: return "X86ISD::PINSRW";
Nate Begemanb9a47b82009-02-23 08:49:38 +000011711 case X86ISD::PSHUFB: return "X86ISD::PSHUFB";
Bruno Cardoso Lopesc1af4772011-07-13 21:36:47 +000011712 case X86ISD::ANDNP: return "X86ISD::ANDNP";
Craig Topper31133842011-11-19 07:33:10 +000011713 case X86ISD::PSIGN: return "X86ISD::PSIGN";
Craig Toppere6a62772011-11-13 17:31:07 +000011714 case X86ISD::BLENDV: return "X86ISD::BLENDV";
Nadav Roteme6113782012-04-11 06:40:27 +000011715 case X86ISD::BLENDPW: return "X86ISD::BLENDPW";
11716 case X86ISD::BLENDPS: return "X86ISD::BLENDPS";
11717 case X86ISD::BLENDPD: return "X86ISD::BLENDPD";
Craig Topperfe033152011-12-06 09:31:36 +000011718 case X86ISD::HADD: return "X86ISD::HADD";
11719 case X86ISD::HSUB: return "X86ISD::HSUB";
Craig Toppere6a62772011-11-13 17:31:07 +000011720 case X86ISD::FHADD: return "X86ISD::FHADD";
11721 case X86ISD::FHSUB: return "X86ISD::FHSUB";
Evan Cheng8ca29322006-11-10 21:43:37 +000011722 case X86ISD::FMAX: return "X86ISD::FMAX";
11723 case X86ISD::FMIN: return "X86ISD::FMIN";
Nadav Rotemd60cb112012-08-19 13:06:16 +000011724 case X86ISD::FMAXC: return "X86ISD::FMAXC";
11725 case X86ISD::FMINC: return "X86ISD::FMINC";
Dan Gohman20382522007-07-10 00:05:58 +000011726 case X86ISD::FRSQRT: return "X86ISD::FRSQRT";
11727 case X86ISD::FRCP: return "X86ISD::FRCP";
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +000011728 case X86ISD::TLSADDR: return "X86ISD::TLSADDR";
Hans Wennborgf0234fc2012-06-01 16:27:21 +000011729 case X86ISD::TLSBASEADDR: return "X86ISD::TLSBASEADDR";
Eric Christopher30ef0e52010-06-03 04:07:48 +000011730 case X86ISD::TLSCALL: return "X86ISD::TLSCALL";
Michael Liao6c0e04c2012-10-15 22:39:43 +000011731 case X86ISD::EH_SJLJ_SETJMP: return "X86ISD::EH_SJLJ_SETJMP";
11732 case X86ISD::EH_SJLJ_LONGJMP: return "X86ISD::EH_SJLJ_LONGJMP";
Anton Korobeynikov2365f512007-07-14 14:06:15 +000011733 case X86ISD::EH_RETURN: return "X86ISD::EH_RETURN";
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +000011734 case X86ISD::TC_RETURN: return "X86ISD::TC_RETURN";
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000011735 case X86ISD::FNSTCW16m: return "X86ISD::FNSTCW16m";
Benjamin Kramer17c836c2012-04-27 12:07:43 +000011736 case X86ISD::FNSTSW16r: return "X86ISD::FNSTSW16r";
Evan Cheng7e2ff772008-05-08 00:57:18 +000011737 case X86ISD::LCMPXCHG_DAG: return "X86ISD::LCMPXCHG_DAG";
11738 case X86ISD::LCMPXCHG8_DAG: return "X86ISD::LCMPXCHG8_DAG";
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011739 case X86ISD::ATOMADD64_DAG: return "X86ISD::ATOMADD64_DAG";
11740 case X86ISD::ATOMSUB64_DAG: return "X86ISD::ATOMSUB64_DAG";
11741 case X86ISD::ATOMOR64_DAG: return "X86ISD::ATOMOR64_DAG";
11742 case X86ISD::ATOMXOR64_DAG: return "X86ISD::ATOMXOR64_DAG";
11743 case X86ISD::ATOMAND64_DAG: return "X86ISD::ATOMAND64_DAG";
11744 case X86ISD::ATOMNAND64_DAG: return "X86ISD::ATOMNAND64_DAG";
Evan Chengd880b972008-05-09 21:53:03 +000011745 case X86ISD::VZEXT_MOVL: return "X86ISD::VZEXT_MOVL";
Michael Liaob7bf7262012-08-14 22:53:17 +000011746 case X86ISD::VSEXT_MOVL: return "X86ISD::VSEXT_MOVL";
Evan Chengd880b972008-05-09 21:53:03 +000011747 case X86ISD::VZEXT_LOAD: return "X86ISD::VZEXT_LOAD";
Michael Liao7091b242012-08-14 21:24:47 +000011748 case X86ISD::VFPEXT: return "X86ISD::VFPEXT";
Michael Liao44c2d612012-10-10 16:53:28 +000011749 case X86ISD::VFPROUND: return "X86ISD::VFPROUND";
Craig Toppered2e13d2012-01-22 19:15:14 +000011750 case X86ISD::VSHLDQ: return "X86ISD::VSHLDQ";
11751 case X86ISD::VSRLDQ: return "X86ISD::VSRLDQ";
Evan Chengf26ffe92008-05-29 08:22:04 +000011752 case X86ISD::VSHL: return "X86ISD::VSHL";
11753 case X86ISD::VSRL: return "X86ISD::VSRL";
Craig Toppered2e13d2012-01-22 19:15:14 +000011754 case X86ISD::VSRA: return "X86ISD::VSRA";
11755 case X86ISD::VSHLI: return "X86ISD::VSHLI";
11756 case X86ISD::VSRLI: return "X86ISD::VSRLI";
11757 case X86ISD::VSRAI: return "X86ISD::VSRAI";
Craig Topper1906d322012-01-22 23:36:02 +000011758 case X86ISD::CMPP: return "X86ISD::CMPP";
Craig Topper67609fd2012-01-22 22:42:16 +000011759 case X86ISD::PCMPEQ: return "X86ISD::PCMPEQ";
11760 case X86ISD::PCMPGT: return "X86ISD::PCMPGT";
Bill Wendlingab55ebd2008-12-12 00:56:36 +000011761 case X86ISD::ADD: return "X86ISD::ADD";
11762 case X86ISD::SUB: return "X86ISD::SUB";
Chris Lattner5b856542010-12-20 00:59:46 +000011763 case X86ISD::ADC: return "X86ISD::ADC";
11764 case X86ISD::SBB: return "X86ISD::SBB";
Bill Wendlingd350e022008-12-12 21:15:41 +000011765 case X86ISD::SMUL: return "X86ISD::SMUL";
11766 case X86ISD::UMUL: return "X86ISD::UMUL";
Dan Gohman076aee32009-03-04 19:44:21 +000011767 case X86ISD::INC: return "X86ISD::INC";
11768 case X86ISD::DEC: return "X86ISD::DEC";
Dan Gohmane220c4b2009-09-18 19:59:53 +000011769 case X86ISD::OR: return "X86ISD::OR";
11770 case X86ISD::XOR: return "X86ISD::XOR";
11771 case X86ISD::AND: return "X86ISD::AND";
Craig Topper54a11172011-10-14 07:06:56 +000011772 case X86ISD::ANDN: return "X86ISD::ANDN";
Craig Toppere6a62772011-11-13 17:31:07 +000011773 case X86ISD::BLSI: return "X86ISD::BLSI";
11774 case X86ISD::BLSMSK: return "X86ISD::BLSMSK";
11775 case X86ISD::BLSR: return "X86ISD::BLSR";
Evan Cheng73f24c92009-03-30 21:36:47 +000011776 case X86ISD::MUL_IMM: return "X86ISD::MUL_IMM";
Eric Christopher71c67532009-07-29 00:28:05 +000011777 case X86ISD::PTEST: return "X86ISD::PTEST";
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +000011778 case X86ISD::TESTP: return "X86ISD::TESTP";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000011779 case X86ISD::PALIGN: return "X86ISD::PALIGN";
11780 case X86ISD::PSHUFD: return "X86ISD::PSHUFD";
11781 case X86ISD::PSHUFHW: return "X86ISD::PSHUFHW";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000011782 case X86ISD::PSHUFLW: return "X86ISD::PSHUFLW";
Craig Topperb3982da2011-12-31 23:50:21 +000011783 case X86ISD::SHUFP: return "X86ISD::SHUFP";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000011784 case X86ISD::MOVLHPS: return "X86ISD::MOVLHPS";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000011785 case X86ISD::MOVLHPD: return "X86ISD::MOVLHPD";
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +000011786 case X86ISD::MOVHLPS: return "X86ISD::MOVHLPS";
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +000011787 case X86ISD::MOVLPS: return "X86ISD::MOVLPS";
11788 case X86ISD::MOVLPD: return "X86ISD::MOVLPD";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000011789 case X86ISD::MOVDDUP: return "X86ISD::MOVDDUP";
11790 case X86ISD::MOVSHDUP: return "X86ISD::MOVSHDUP";
11791 case X86ISD::MOVSLDUP: return "X86ISD::MOVSLDUP";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000011792 case X86ISD::MOVSD: return "X86ISD::MOVSD";
11793 case X86ISD::MOVSS: return "X86ISD::MOVSS";
Craig Topper34671b82011-12-06 08:21:25 +000011794 case X86ISD::UNPCKL: return "X86ISD::UNPCKL";
11795 case X86ISD::UNPCKH: return "X86ISD::UNPCKH";
Bruno Cardoso Lopes0e6d2302011-08-17 02:29:19 +000011796 case X86ISD::VBROADCAST: return "X86ISD::VBROADCAST";
Craig Topper316cd2a2011-11-30 06:25:25 +000011797 case X86ISD::VPERMILP: return "X86ISD::VPERMILP";
Craig Topperec24e612011-11-30 07:47:51 +000011798 case X86ISD::VPERM2X128: return "X86ISD::VPERM2X128";
Craig Topper8325c112012-04-16 00:41:45 +000011799 case X86ISD::VPERMV: return "X86ISD::VPERMV";
11800 case X86ISD::VPERMI: return "X86ISD::VPERMI";
Craig Topper5b209e82012-02-05 03:14:49 +000011801 case X86ISD::PMULUDQ: return "X86ISD::PMULUDQ";
Dan Gohmand6708ea2009-08-15 01:38:56 +000011802 case X86ISD::VASTART_SAVE_XMM_REGS: return "X86ISD::VASTART_SAVE_XMM_REGS";
Dan Gohman320afb82010-10-12 18:00:49 +000011803 case X86ISD::VAARG_64: return "X86ISD::VAARG_64";
Michael J. Spencere9c253e2010-10-21 01:41:01 +000011804 case X86ISD::WIN_ALLOCA: return "X86ISD::WIN_ALLOCA";
Eli Friedman14648462011-07-27 22:21:52 +000011805 case X86ISD::MEMBARRIER: return "X86ISD::MEMBARRIER";
Rafael Espindolad07b7ec2011-08-30 19:43:21 +000011806 case X86ISD::SEG_ALLOCA: return "X86ISD::SEG_ALLOCA";
Michael J. Spencer1a2d0612012-02-24 19:01:22 +000011807 case X86ISD::WIN_FTOL: return "X86ISD::WIN_FTOL";
Benjamin Kramer17c836c2012-04-27 12:07:43 +000011808 case X86ISD::SAHF: return "X86ISD::SAHF";
Benjamin Kramerb9bee042012-07-12 09:31:43 +000011809 case X86ISD::RDRAND: return "X86ISD::RDRAND";
Elena Demikhovsky1503aba2012-08-01 12:06:00 +000011810 case X86ISD::FMADD: return "X86ISD::FMADD";
11811 case X86ISD::FMSUB: return "X86ISD::FMSUB";
11812 case X86ISD::FNMADD: return "X86ISD::FNMADD";
11813 case X86ISD::FNMSUB: return "X86ISD::FNMSUB";
11814 case X86ISD::FMADDSUB: return "X86ISD::FMADDSUB";
11815 case X86ISD::FMSUBADD: return "X86ISD::FMSUBADD";
Evan Cheng72261582005-12-20 06:22:03 +000011816 }
11817}
Evan Cheng3a03ebb2005-12-21 23:05:39 +000011818
Chris Lattnerc9addb72007-03-30 23:15:24 +000011819// isLegalAddressingMode - Return true if the addressing mode represented
11820// by AM is legal for this target, for a load/store of the specified type.
Scott Michelfdc40a02009-02-17 22:15:04 +000011821bool X86TargetLowering::isLegalAddressingMode(const AddrMode &AM,
Chris Lattnerdb125cf2011-07-18 04:54:35 +000011822 Type *Ty) const {
Chris Lattnerc9addb72007-03-30 23:15:24 +000011823 // X86 supports extremely general addressing modes.
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000011824 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman92b651f2010-08-24 15:55:12 +000011825 Reloc::Model R = getTargetMachine().getRelocationModel();
Scott Michelfdc40a02009-02-17 22:15:04 +000011826
Chris Lattnerc9addb72007-03-30 23:15:24 +000011827 // X86 allows a sign-extended 32-bit immediate field as a displacement.
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000011828 if (!X86::isOffsetSuitableForCodeModel(AM.BaseOffs, M, AM.BaseGV != NULL))
Chris Lattnerc9addb72007-03-30 23:15:24 +000011829 return false;
Scott Michelfdc40a02009-02-17 22:15:04 +000011830
Chris Lattnerc9addb72007-03-30 23:15:24 +000011831 if (AM.BaseGV) {
Chris Lattnerdfed4132009-07-10 07:38:24 +000011832 unsigned GVFlags =
11833 Subtarget->ClassifyGlobalReference(AM.BaseGV, getTargetMachine());
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000011834
Chris Lattnerdfed4132009-07-10 07:38:24 +000011835 // If a reference to this global requires an extra load, we can't fold it.
11836 if (isGlobalStubReference(GVFlags))
Chris Lattnerc9addb72007-03-30 23:15:24 +000011837 return false;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000011838
Chris Lattnerdfed4132009-07-10 07:38:24 +000011839 // If BaseGV requires a register for the PIC base, we cannot also have a
11840 // BaseReg specified.
11841 if (AM.HasBaseReg && isGlobalRelativeToPICBase(GVFlags))
Dale Johannesen203af582008-12-05 21:47:27 +000011842 return false;
Evan Cheng52787842007-08-01 23:46:47 +000011843
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000011844 // If lower 4G is not available, then we must use rip-relative addressing.
Dan Gohman92b651f2010-08-24 15:55:12 +000011845 if ((M != CodeModel::Small || R != Reloc::Static) &&
11846 Subtarget->is64Bit() && (AM.BaseOffs || AM.Scale > 1))
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000011847 return false;
Chris Lattnerc9addb72007-03-30 23:15:24 +000011848 }
Scott Michelfdc40a02009-02-17 22:15:04 +000011849
Chris Lattnerc9addb72007-03-30 23:15:24 +000011850 switch (AM.Scale) {
11851 case 0:
11852 case 1:
11853 case 2:
11854 case 4:
11855 case 8:
11856 // These scales always work.
11857 break;
11858 case 3:
11859 case 5:
11860 case 9:
11861 // These scales are formed with basereg+scalereg. Only accept if there is
11862 // no basereg yet.
11863 if (AM.HasBaseReg)
11864 return false;
11865 break;
11866 default: // Other stuff never works.
11867 return false;
11868 }
Scott Michelfdc40a02009-02-17 22:15:04 +000011869
Chris Lattnerc9addb72007-03-30 23:15:24 +000011870 return true;
11871}
11872
11873
Chris Lattnerdb125cf2011-07-18 04:54:35 +000011874bool X86TargetLowering::isTruncateFree(Type *Ty1, Type *Ty2) const {
Duncan Sandsb0bc6c32010-02-15 16:12:20 +000011875 if (!Ty1->isIntegerTy() || !Ty2->isIntegerTy())
Evan Cheng2bd122c2007-10-26 01:56:11 +000011876 return false;
Evan Chenge127a732007-10-29 07:57:50 +000011877 unsigned NumBits1 = Ty1->getPrimitiveSizeInBits();
11878 unsigned NumBits2 = Ty2->getPrimitiveSizeInBits();
Evan Cheng260e07e2008-03-20 02:18:41 +000011879 if (NumBits1 <= NumBits2)
Evan Chenge127a732007-10-29 07:57:50 +000011880 return false;
Dan Gohman377fbc02010-02-25 03:04:36 +000011881 return true;
Evan Cheng2bd122c2007-10-26 01:56:11 +000011882}
11883
Evan Cheng70e10d32012-07-17 06:53:39 +000011884bool X86TargetLowering::isLegalICmpImmediate(int64_t Imm) const {
11885 return Imm == (int32_t)Imm;
11886}
11887
11888bool X86TargetLowering::isLegalAddImmediate(int64_t Imm) const {
Evan Chenga9e13ba2012-07-17 18:54:11 +000011889 // Can also use sub to handle negated immediates.
Evan Cheng70e10d32012-07-17 06:53:39 +000011890 return Imm == (int32_t)Imm;
11891}
11892
Owen Andersone50ed302009-08-10 22:56:29 +000011893bool X86TargetLowering::isTruncateFree(EVT VT1, EVT VT2) const {
Duncan Sands83ec4b62008-06-06 12:08:01 +000011894 if (!VT1.isInteger() || !VT2.isInteger())
Evan Cheng3c3ddb32007-10-29 19:58:20 +000011895 return false;
Duncan Sands83ec4b62008-06-06 12:08:01 +000011896 unsigned NumBits1 = VT1.getSizeInBits();
11897 unsigned NumBits2 = VT2.getSizeInBits();
Evan Cheng260e07e2008-03-20 02:18:41 +000011898 if (NumBits1 <= NumBits2)
Evan Cheng3c3ddb32007-10-29 19:58:20 +000011899 return false;
Dan Gohman377fbc02010-02-25 03:04:36 +000011900 return true;
Evan Cheng3c3ddb32007-10-29 19:58:20 +000011901}
Evan Cheng2bd122c2007-10-26 01:56:11 +000011902
Chris Lattnerdb125cf2011-07-18 04:54:35 +000011903bool X86TargetLowering::isZExtFree(Type *Ty1, Type *Ty2) const {
Dan Gohman349ba492009-04-09 02:06:09 +000011904 // x86-64 implicitly zero-extends 32-bit results in 64-bit registers.
Duncan Sandsb0bc6c32010-02-15 16:12:20 +000011905 return Ty1->isIntegerTy(32) && Ty2->isIntegerTy(64) && Subtarget->is64Bit();
Dan Gohman97121ba2009-04-08 00:15:30 +000011906}
11907
Owen Andersone50ed302009-08-10 22:56:29 +000011908bool X86TargetLowering::isZExtFree(EVT VT1, EVT VT2) const {
Dan Gohman349ba492009-04-09 02:06:09 +000011909 // x86-64 implicitly zero-extends 32-bit results in 64-bit registers.
Owen Anderson825b72b2009-08-11 20:47:22 +000011910 return VT1 == MVT::i32 && VT2 == MVT::i64 && Subtarget->is64Bit();
Dan Gohman97121ba2009-04-08 00:15:30 +000011911}
11912
Owen Andersone50ed302009-08-10 22:56:29 +000011913bool X86TargetLowering::isNarrowingProfitable(EVT VT1, EVT VT2) const {
Evan Cheng8b944d32009-05-28 00:35:15 +000011914 // i16 instructions are longer (0x66 prefix) and potentially slower.
Owen Anderson825b72b2009-08-11 20:47:22 +000011915 return !(VT1 == MVT::i32 && VT2 == MVT::i16);
Evan Cheng8b944d32009-05-28 00:35:15 +000011916}
11917
Evan Cheng60c07e12006-07-05 22:17:51 +000011918/// isShuffleMaskLegal - Targets can use this to indicate that they only
11919/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
11920/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
11921/// are assumed to be legal.
11922bool
Eric Christopherfd179292009-08-27 18:07:15 +000011923X86TargetLowering::isShuffleMaskLegal(const SmallVectorImpl<int> &M,
Owen Andersone50ed302009-08-10 22:56:29 +000011924 EVT VT) const {
Eric Christophercff6f852010-04-15 01:40:20 +000011925 // Very little shuffling can be done for 64-bit vectors right now.
Nate Begeman9008ca62009-04-27 18:41:29 +000011926 if (VT.getSizeInBits() == 64)
Craig Topper1dc0fbc2011-12-05 07:27:14 +000011927 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +000011928
Nate Begemana09008b2009-10-19 02:17:23 +000011929 // FIXME: pshufb, blends, shifts.
Nate Begeman9008ca62009-04-27 18:41:29 +000011930 return (VT.getVectorNumElements() == 2 ||
11931 ShuffleVectorSDNode::isSplatMask(&M[0], VT) ||
11932 isMOVLMask(M, VT) ||
Craig Topper1a7700a2012-01-19 08:19:12 +000011933 isSHUFPMask(M, VT, Subtarget->hasAVX()) ||
Nate Begeman9008ca62009-04-27 18:41:29 +000011934 isPSHUFDMask(M, VT) ||
Craig Toppera9a568a2012-05-02 08:03:44 +000011935 isPSHUFHWMask(M, VT, Subtarget->hasAVX2()) ||
11936 isPSHUFLWMask(M, VT, Subtarget->hasAVX2()) ||
Craig Topper0e2037b2012-01-20 05:53:00 +000011937 isPALIGNRMask(M, VT, Subtarget) ||
Craig Topper6347e862011-11-21 06:57:39 +000011938 isUNPCKLMask(M, VT, Subtarget->hasAVX2()) ||
11939 isUNPCKHMask(M, VT, Subtarget->hasAVX2()) ||
Craig Topper94438ba2011-12-16 08:06:31 +000011940 isUNPCKL_v_undef_Mask(M, VT, Subtarget->hasAVX2()) ||
11941 isUNPCKH_v_undef_Mask(M, VT, Subtarget->hasAVX2()));
Evan Cheng60c07e12006-07-05 22:17:51 +000011942}
11943
Dan Gohman7d8143f2008-04-09 20:09:42 +000011944bool
Nate Begeman5a5ca152009-04-29 05:20:52 +000011945X86TargetLowering::isVectorClearMaskLegal(const SmallVectorImpl<int> &Mask,
Owen Andersone50ed302009-08-10 22:56:29 +000011946 EVT VT) const {
Nate Begeman9008ca62009-04-27 18:41:29 +000011947 unsigned NumElts = VT.getVectorNumElements();
11948 // FIXME: This collection of masks seems suspect.
11949 if (NumElts == 2)
11950 return true;
Craig Topper7a9a28b2012-08-12 02:23:29 +000011951 if (NumElts == 4 && VT.is128BitVector()) {
Nate Begeman9008ca62009-04-27 18:41:29 +000011952 return (isMOVLMask(Mask, VT) ||
11953 isCommutedMOVLMask(Mask, VT, true) ||
Craig Topper1a7700a2012-01-19 08:19:12 +000011954 isSHUFPMask(Mask, VT, Subtarget->hasAVX()) ||
11955 isSHUFPMask(Mask, VT, Subtarget->hasAVX(), /* Commuted */ true));
Evan Cheng60c07e12006-07-05 22:17:51 +000011956 }
11957 return false;
11958}
11959
11960//===----------------------------------------------------------------------===//
11961// X86 Scheduler Hooks
11962//===----------------------------------------------------------------------===//
11963
Mon P Wang63307c32008-05-05 19:05:59 +000011964// private utility function
Scott Michelfdc40a02009-02-17 22:15:04 +000011965
Michael Liaob118a072012-09-20 03:06:15 +000011966// Get CMPXCHG opcode for the specified data type.
11967static unsigned getCmpXChgOpcode(EVT VT) {
11968 switch (VT.getSimpleVT().SimpleTy) {
11969 case MVT::i8: return X86::LCMPXCHG8;
11970 case MVT::i16: return X86::LCMPXCHG16;
11971 case MVT::i32: return X86::LCMPXCHG32;
11972 case MVT::i64: return X86::LCMPXCHG64;
11973 default:
11974 break;
Richard Smith42fc29e2012-04-13 22:47:00 +000011975 }
Michael Liaob118a072012-09-20 03:06:15 +000011976 llvm_unreachable("Invalid operand size!");
Mon P Wang63307c32008-05-05 19:05:59 +000011977}
11978
Michael Liaob118a072012-09-20 03:06:15 +000011979// Get LOAD opcode for the specified data type.
11980static unsigned getLoadOpcode(EVT VT) {
11981 switch (VT.getSimpleVT().SimpleTy) {
11982 case MVT::i8: return X86::MOV8rm;
11983 case MVT::i16: return X86::MOV16rm;
11984 case MVT::i32: return X86::MOV32rm;
11985 case MVT::i64: return X86::MOV64rm;
11986 default:
11987 break;
11988 }
11989 llvm_unreachable("Invalid operand size!");
11990}
11991
11992// Get opcode of the non-atomic one from the specified atomic instruction.
11993static unsigned getNonAtomicOpcode(unsigned Opc) {
11994 switch (Opc) {
11995 case X86::ATOMAND8: return X86::AND8rr;
11996 case X86::ATOMAND16: return X86::AND16rr;
11997 case X86::ATOMAND32: return X86::AND32rr;
11998 case X86::ATOMAND64: return X86::AND64rr;
11999 case X86::ATOMOR8: return X86::OR8rr;
12000 case X86::ATOMOR16: return X86::OR16rr;
12001 case X86::ATOMOR32: return X86::OR32rr;
12002 case X86::ATOMOR64: return X86::OR64rr;
12003 case X86::ATOMXOR8: return X86::XOR8rr;
12004 case X86::ATOMXOR16: return X86::XOR16rr;
12005 case X86::ATOMXOR32: return X86::XOR32rr;
12006 case X86::ATOMXOR64: return X86::XOR64rr;
12007 }
12008 llvm_unreachable("Unhandled atomic-load-op opcode!");
12009}
12010
12011// Get opcode of the non-atomic one from the specified atomic instruction with
12012// extra opcode.
12013static unsigned getNonAtomicOpcodeWithExtraOpc(unsigned Opc,
12014 unsigned &ExtraOpc) {
12015 switch (Opc) {
12016 case X86::ATOMNAND8: ExtraOpc = X86::NOT8r; return X86::AND8rr;
12017 case X86::ATOMNAND16: ExtraOpc = X86::NOT16r; return X86::AND16rr;
12018 case X86::ATOMNAND32: ExtraOpc = X86::NOT32r; return X86::AND32rr;
12019 case X86::ATOMNAND64: ExtraOpc = X86::NOT64r; return X86::AND64rr;
Michael Liaofe87c302012-09-21 03:18:52 +000012020 case X86::ATOMMAX8: ExtraOpc = X86::CMP8rr; return X86::CMOVL32rr;
Michael Liaob118a072012-09-20 03:06:15 +000012021 case X86::ATOMMAX16: ExtraOpc = X86::CMP16rr; return X86::CMOVL16rr;
12022 case X86::ATOMMAX32: ExtraOpc = X86::CMP32rr; return X86::CMOVL32rr;
12023 case X86::ATOMMAX64: ExtraOpc = X86::CMP64rr; return X86::CMOVL64rr;
Michael Liaofe87c302012-09-21 03:18:52 +000012024 case X86::ATOMMIN8: ExtraOpc = X86::CMP8rr; return X86::CMOVG32rr;
Michael Liaob118a072012-09-20 03:06:15 +000012025 case X86::ATOMMIN16: ExtraOpc = X86::CMP16rr; return X86::CMOVG16rr;
12026 case X86::ATOMMIN32: ExtraOpc = X86::CMP32rr; return X86::CMOVG32rr;
12027 case X86::ATOMMIN64: ExtraOpc = X86::CMP64rr; return X86::CMOVG64rr;
Michael Liaofe87c302012-09-21 03:18:52 +000012028 case X86::ATOMUMAX8: ExtraOpc = X86::CMP8rr; return X86::CMOVB32rr;
Michael Liaob118a072012-09-20 03:06:15 +000012029 case X86::ATOMUMAX16: ExtraOpc = X86::CMP16rr; return X86::CMOVB16rr;
12030 case X86::ATOMUMAX32: ExtraOpc = X86::CMP32rr; return X86::CMOVB32rr;
12031 case X86::ATOMUMAX64: ExtraOpc = X86::CMP64rr; return X86::CMOVB64rr;
Michael Liaofe87c302012-09-21 03:18:52 +000012032 case X86::ATOMUMIN8: ExtraOpc = X86::CMP8rr; return X86::CMOVA32rr;
Michael Liaob118a072012-09-20 03:06:15 +000012033 case X86::ATOMUMIN16: ExtraOpc = X86::CMP16rr; return X86::CMOVA16rr;
12034 case X86::ATOMUMIN32: ExtraOpc = X86::CMP32rr; return X86::CMOVA32rr;
12035 case X86::ATOMUMIN64: ExtraOpc = X86::CMP64rr; return X86::CMOVA64rr;
12036 }
12037 llvm_unreachable("Unhandled atomic-load-op opcode!");
12038}
12039
12040// Get opcode of the non-atomic one from the specified atomic instruction for
12041// 64-bit data type on 32-bit target.
12042static unsigned getNonAtomic6432Opcode(unsigned Opc, unsigned &HiOpc) {
12043 switch (Opc) {
12044 case X86::ATOMAND6432: HiOpc = X86::AND32rr; return X86::AND32rr;
12045 case X86::ATOMOR6432: HiOpc = X86::OR32rr; return X86::OR32rr;
12046 case X86::ATOMXOR6432: HiOpc = X86::XOR32rr; return X86::XOR32rr;
12047 case X86::ATOMADD6432: HiOpc = X86::ADC32rr; return X86::ADD32rr;
12048 case X86::ATOMSUB6432: HiOpc = X86::SBB32rr; return X86::SUB32rr;
12049 case X86::ATOMSWAP6432: HiOpc = X86::MOV32rr; return X86::MOV32rr;
Michael Liaoe5e8f762012-09-25 18:08:13 +000012050 case X86::ATOMMAX6432: HiOpc = X86::SETLr; return X86::SETLr;
12051 case X86::ATOMMIN6432: HiOpc = X86::SETGr; return X86::SETGr;
12052 case X86::ATOMUMAX6432: HiOpc = X86::SETBr; return X86::SETBr;
12053 case X86::ATOMUMIN6432: HiOpc = X86::SETAr; return X86::SETAr;
Michael Liaob118a072012-09-20 03:06:15 +000012054 }
12055 llvm_unreachable("Unhandled atomic-load-op opcode!");
12056}
12057
12058// Get opcode of the non-atomic one from the specified atomic instruction for
12059// 64-bit data type on 32-bit target with extra opcode.
12060static unsigned getNonAtomic6432OpcodeWithExtraOpc(unsigned Opc,
12061 unsigned &HiOpc,
12062 unsigned &ExtraOpc) {
12063 switch (Opc) {
12064 case X86::ATOMNAND6432:
12065 ExtraOpc = X86::NOT32r;
12066 HiOpc = X86::AND32rr;
12067 return X86::AND32rr;
12068 }
12069 llvm_unreachable("Unhandled atomic-load-op opcode!");
12070}
12071
12072// Get pseudo CMOV opcode from the specified data type.
12073static unsigned getPseudoCMOVOpc(EVT VT) {
12074 switch (VT.getSimpleVT().SimpleTy) {
Michael Liaofe87c302012-09-21 03:18:52 +000012075 case MVT::i8: return X86::CMOV_GR8;
Michael Liaob118a072012-09-20 03:06:15 +000012076 case MVT::i16: return X86::CMOV_GR16;
12077 case MVT::i32: return X86::CMOV_GR32;
12078 default:
12079 break;
12080 }
12081 llvm_unreachable("Unknown CMOV opcode!");
12082}
12083
12084// EmitAtomicLoadArith - emit the code sequence for pseudo atomic instructions.
12085// They will be translated into a spin-loop or compare-exchange loop from
12086//
12087// ...
12088// dst = atomic-fetch-op MI.addr, MI.val
12089// ...
12090//
12091// to
12092//
12093// ...
12094// EAX = LOAD MI.addr
12095// loop:
12096// t1 = OP MI.val, EAX
12097// LCMPXCHG [MI.addr], t1, [EAX is implicitly used & defined]
12098// JNE loop
12099// sink:
12100// dst = EAX
12101// ...
Mon P Wang63307c32008-05-05 19:05:59 +000012102MachineBasicBlock *
Michael Liaob118a072012-09-20 03:06:15 +000012103X86TargetLowering::EmitAtomicLoadArith(MachineInstr *MI,
12104 MachineBasicBlock *MBB) const {
12105 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
12106 DebugLoc DL = MI->getDebugLoc();
12107
12108 MachineFunction *MF = MBB->getParent();
12109 MachineRegisterInfo &MRI = MF->getRegInfo();
12110
12111 const BasicBlock *BB = MBB->getBasicBlock();
12112 MachineFunction::iterator I = MBB;
12113 ++I;
12114
12115 assert(MI->getNumOperands() <= X86::AddrNumOperands + 2 &&
12116 "Unexpected number of operands");
12117
12118 assert(MI->hasOneMemOperand() &&
12119 "Expected atomic-load-op to have one memoperand");
12120
12121 // Memory Reference
12122 MachineInstr::mmo_iterator MMOBegin = MI->memoperands_begin();
12123 MachineInstr::mmo_iterator MMOEnd = MI->memoperands_end();
12124
12125 unsigned DstReg, SrcReg;
12126 unsigned MemOpndSlot;
12127
12128 unsigned CurOp = 0;
12129
12130 DstReg = MI->getOperand(CurOp++).getReg();
12131 MemOpndSlot = CurOp;
12132 CurOp += X86::AddrNumOperands;
12133 SrcReg = MI->getOperand(CurOp++).getReg();
12134
12135 const TargetRegisterClass *RC = MRI.getRegClass(DstReg);
Craig Topperf4d25a22012-09-30 19:49:56 +000012136 MVT::SimpleValueType VT = *RC->vt_begin();
Michael Liaob118a072012-09-20 03:06:15 +000012137 unsigned AccPhyReg = getX86SubSuperRegister(X86::EAX, VT);
12138
12139 unsigned LCMPXCHGOpc = getCmpXChgOpcode(VT);
12140 unsigned LOADOpc = getLoadOpcode(VT);
12141
12142 // For the atomic load-arith operator, we generate
12143 //
12144 // thisMBB:
12145 // EAX = LOAD [MI.addr]
12146 // mainMBB:
12147 // t1 = OP MI.val, EAX
12148 // LCMPXCHG [MI.addr], t1, [EAX is implicitly used & defined]
12149 // JNE mainMBB
12150 // sinkMBB:
12151
12152 MachineBasicBlock *thisMBB = MBB;
12153 MachineBasicBlock *mainMBB = MF->CreateMachineBasicBlock(BB);
12154 MachineBasicBlock *sinkMBB = MF->CreateMachineBasicBlock(BB);
12155 MF->insert(I, mainMBB);
12156 MF->insert(I, sinkMBB);
12157
12158 MachineInstrBuilder MIB;
12159
12160 // Transfer the remainder of BB and its successor edges to sinkMBB.
12161 sinkMBB->splice(sinkMBB->begin(), MBB,
12162 llvm::next(MachineBasicBlock::iterator(MI)), MBB->end());
12163 sinkMBB->transferSuccessorsAndUpdatePHIs(MBB);
12164
12165 // thisMBB:
12166 MIB = BuildMI(thisMBB, DL, TII->get(LOADOpc), AccPhyReg);
12167 for (unsigned i = 0; i < X86::AddrNumOperands; ++i)
12168 MIB.addOperand(MI->getOperand(MemOpndSlot + i));
12169 MIB.setMemRefs(MMOBegin, MMOEnd);
12170
12171 thisMBB->addSuccessor(mainMBB);
12172
12173 // mainMBB:
12174 MachineBasicBlock *origMainMBB = mainMBB;
12175 mainMBB->addLiveIn(AccPhyReg);
12176
12177 // Copy AccPhyReg as it is used more than once.
12178 unsigned AccReg = MRI.createVirtualRegister(RC);
12179 BuildMI(mainMBB, DL, TII->get(TargetOpcode::COPY), AccReg)
12180 .addReg(AccPhyReg);
12181
12182 unsigned t1 = MRI.createVirtualRegister(RC);
12183 unsigned Opc = MI->getOpcode();
12184 switch (Opc) {
12185 default:
12186 llvm_unreachable("Unhandled atomic-load-op opcode!");
12187 case X86::ATOMAND8:
12188 case X86::ATOMAND16:
12189 case X86::ATOMAND32:
12190 case X86::ATOMAND64:
12191 case X86::ATOMOR8:
12192 case X86::ATOMOR16:
12193 case X86::ATOMOR32:
12194 case X86::ATOMOR64:
12195 case X86::ATOMXOR8:
12196 case X86::ATOMXOR16:
12197 case X86::ATOMXOR32:
12198 case X86::ATOMXOR64: {
12199 unsigned ARITHOpc = getNonAtomicOpcode(Opc);
12200 BuildMI(mainMBB, DL, TII->get(ARITHOpc), t1).addReg(SrcReg)
12201 .addReg(AccReg);
12202 break;
12203 }
12204 case X86::ATOMNAND8:
12205 case X86::ATOMNAND16:
12206 case X86::ATOMNAND32:
12207 case X86::ATOMNAND64: {
12208 unsigned t2 = MRI.createVirtualRegister(RC);
12209 unsigned NOTOpc;
12210 unsigned ANDOpc = getNonAtomicOpcodeWithExtraOpc(Opc, NOTOpc);
12211 BuildMI(mainMBB, DL, TII->get(ANDOpc), t2).addReg(SrcReg)
12212 .addReg(AccReg);
12213 BuildMI(mainMBB, DL, TII->get(NOTOpc), t1).addReg(t2);
12214 break;
12215 }
Michael Liao08382492012-09-21 03:00:17 +000012216 case X86::ATOMMAX8:
Michael Liaob118a072012-09-20 03:06:15 +000012217 case X86::ATOMMAX16:
12218 case X86::ATOMMAX32:
12219 case X86::ATOMMAX64:
Michael Liaofe87c302012-09-21 03:18:52 +000012220 case X86::ATOMMIN8:
Michael Liaob118a072012-09-20 03:06:15 +000012221 case X86::ATOMMIN16:
12222 case X86::ATOMMIN32:
12223 case X86::ATOMMIN64:
Michael Liaofe87c302012-09-21 03:18:52 +000012224 case X86::ATOMUMAX8:
Michael Liaob118a072012-09-20 03:06:15 +000012225 case X86::ATOMUMAX16:
12226 case X86::ATOMUMAX32:
12227 case X86::ATOMUMAX64:
Michael Liaofe87c302012-09-21 03:18:52 +000012228 case X86::ATOMUMIN8:
Michael Liaob118a072012-09-20 03:06:15 +000012229 case X86::ATOMUMIN16:
12230 case X86::ATOMUMIN32:
12231 case X86::ATOMUMIN64: {
12232 unsigned CMPOpc;
12233 unsigned CMOVOpc = getNonAtomicOpcodeWithExtraOpc(Opc, CMPOpc);
12234
12235 BuildMI(mainMBB, DL, TII->get(CMPOpc))
12236 .addReg(SrcReg)
12237 .addReg(AccReg);
12238
12239 if (Subtarget->hasCMov()) {
Michael Liaofe87c302012-09-21 03:18:52 +000012240 if (VT != MVT::i8) {
12241 // Native support
12242 BuildMI(mainMBB, DL, TII->get(CMOVOpc), t1)
12243 .addReg(SrcReg)
12244 .addReg(AccReg);
12245 } else {
12246 // Promote i8 to i32 to use CMOV32
12247 const TargetRegisterClass *RC32 = getRegClassFor(MVT::i32);
12248 unsigned SrcReg32 = MRI.createVirtualRegister(RC32);
12249 unsigned AccReg32 = MRI.createVirtualRegister(RC32);
12250 unsigned t2 = MRI.createVirtualRegister(RC32);
12251
12252 unsigned Undef = MRI.createVirtualRegister(RC32);
12253 BuildMI(mainMBB, DL, TII->get(TargetOpcode::IMPLICIT_DEF), Undef);
12254
12255 BuildMI(mainMBB, DL, TII->get(TargetOpcode::INSERT_SUBREG), SrcReg32)
12256 .addReg(Undef)
12257 .addReg(SrcReg)
12258 .addImm(X86::sub_8bit);
12259 BuildMI(mainMBB, DL, TII->get(TargetOpcode::INSERT_SUBREG), AccReg32)
12260 .addReg(Undef)
12261 .addReg(AccReg)
12262 .addImm(X86::sub_8bit);
12263
12264 BuildMI(mainMBB, DL, TII->get(CMOVOpc), t2)
12265 .addReg(SrcReg32)
12266 .addReg(AccReg32);
12267
12268 BuildMI(mainMBB, DL, TII->get(TargetOpcode::COPY), t1)
12269 .addReg(t2, 0, X86::sub_8bit);
12270 }
Michael Liaob118a072012-09-20 03:06:15 +000012271 } else {
12272 // Use pseudo select and lower them.
Michael Liaofe87c302012-09-21 03:18:52 +000012273 assert((VT == MVT::i8 || VT == MVT::i16 || VT == MVT::i32) &&
Michael Liaob118a072012-09-20 03:06:15 +000012274 "Invalid atomic-load-op transformation!");
12275 unsigned SelOpc = getPseudoCMOVOpc(VT);
12276 X86::CondCode CC = X86::getCondFromCMovOpc(CMOVOpc);
12277 assert(CC != X86::COND_INVALID && "Invalid atomic-load-op transformation!");
12278 MIB = BuildMI(mainMBB, DL, TII->get(SelOpc), t1)
12279 .addReg(SrcReg).addReg(AccReg)
12280 .addImm(CC);
12281 mainMBB = EmitLoweredSelect(MIB, mainMBB);
12282 }
12283 break;
12284 }
12285 }
12286
12287 // Copy AccPhyReg back from virtual register.
12288 BuildMI(mainMBB, DL, TII->get(TargetOpcode::COPY), AccPhyReg)
12289 .addReg(AccReg);
12290
12291 MIB = BuildMI(mainMBB, DL, TII->get(LCMPXCHGOpc));
12292 for (unsigned i = 0; i < X86::AddrNumOperands; ++i)
12293 MIB.addOperand(MI->getOperand(MemOpndSlot + i));
12294 MIB.addReg(t1);
12295 MIB.setMemRefs(MMOBegin, MMOEnd);
12296
12297 BuildMI(mainMBB, DL, TII->get(X86::JNE_4)).addMBB(origMainMBB);
12298
12299 mainMBB->addSuccessor(origMainMBB);
12300 mainMBB->addSuccessor(sinkMBB);
12301
12302 // sinkMBB:
12303 sinkMBB->addLiveIn(AccPhyReg);
12304
12305 BuildMI(*sinkMBB, sinkMBB->begin(), DL,
12306 TII->get(TargetOpcode::COPY), DstReg)
12307 .addReg(AccPhyReg);
12308
12309 MI->eraseFromParent();
12310 return sinkMBB;
12311}
12312
12313// EmitAtomicLoadArith6432 - emit the code sequence for pseudo atomic
12314// instructions. They will be translated into a spin-loop or compare-exchange
12315// loop from
12316//
12317// ...
12318// dst = atomic-fetch-op MI.addr, MI.val
12319// ...
12320//
12321// to
12322//
12323// ...
12324// EAX = LOAD [MI.addr + 0]
12325// EDX = LOAD [MI.addr + 4]
12326// loop:
12327// EBX = OP MI.val.lo, EAX
12328// ECX = OP MI.val.hi, EDX
12329// LCMPXCHG8B [MI.addr], [ECX:EBX & EDX:EAX are implicitly used and EDX:EAX is implicitly defined]
12330// JNE loop
12331// sink:
12332// dst = EDX:EAX
12333// ...
12334MachineBasicBlock *
12335X86TargetLowering::EmitAtomicLoadArith6432(MachineInstr *MI,
12336 MachineBasicBlock *MBB) const {
12337 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
12338 DebugLoc DL = MI->getDebugLoc();
12339
12340 MachineFunction *MF = MBB->getParent();
12341 MachineRegisterInfo &MRI = MF->getRegInfo();
12342
12343 const BasicBlock *BB = MBB->getBasicBlock();
12344 MachineFunction::iterator I = MBB;
12345 ++I;
12346
12347 assert(MI->getNumOperands() <= X86::AddrNumOperands + 4 &&
12348 "Unexpected number of operands");
12349
12350 assert(MI->hasOneMemOperand() &&
12351 "Expected atomic-load-op32 to have one memoperand");
12352
12353 // Memory Reference
12354 MachineInstr::mmo_iterator MMOBegin = MI->memoperands_begin();
12355 MachineInstr::mmo_iterator MMOEnd = MI->memoperands_end();
12356
12357 unsigned DstLoReg, DstHiReg;
12358 unsigned SrcLoReg, SrcHiReg;
12359 unsigned MemOpndSlot;
12360
12361 unsigned CurOp = 0;
12362
12363 DstLoReg = MI->getOperand(CurOp++).getReg();
12364 DstHiReg = MI->getOperand(CurOp++).getReg();
12365 MemOpndSlot = CurOp;
12366 CurOp += X86::AddrNumOperands;
12367 SrcLoReg = MI->getOperand(CurOp++).getReg();
12368 SrcHiReg = MI->getOperand(CurOp++).getReg();
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012369
Craig Topperc9099502012-04-20 06:31:50 +000012370 const TargetRegisterClass *RC = &X86::GR32RegClass;
Michael Liaoe5e8f762012-09-25 18:08:13 +000012371 const TargetRegisterClass *RC8 = &X86::GR8RegClass;
Scott Michelfdc40a02009-02-17 22:15:04 +000012372
Michael Liaob118a072012-09-20 03:06:15 +000012373 unsigned LCMPXCHGOpc = X86::LCMPXCHG8B;
12374 unsigned LOADOpc = X86::MOV32rm;
Scott Michelfdc40a02009-02-17 22:15:04 +000012375
Michael Liaob118a072012-09-20 03:06:15 +000012376 // For the atomic load-arith operator, we generate
Mon P Wang63307c32008-05-05 19:05:59 +000012377 //
Michael Liaob118a072012-09-20 03:06:15 +000012378 // thisMBB:
12379 // EAX = LOAD [MI.addr + 0]
12380 // EDX = LOAD [MI.addr + 4]
12381 // mainMBB:
12382 // EBX = OP MI.vallo, EAX
12383 // ECX = OP MI.valhi, EDX
12384 // LCMPXCHG8B [MI.addr], [ECX:EBX & EDX:EAX are implicitly used and EDX:EAX is implicitly defined]
12385 // JNE mainMBB
12386 // sinkMBB:
Scott Michelfdc40a02009-02-17 22:15:04 +000012387
Mon P Wang63307c32008-05-05 19:05:59 +000012388 MachineBasicBlock *thisMBB = MBB;
Michael Liaob118a072012-09-20 03:06:15 +000012389 MachineBasicBlock *mainMBB = MF->CreateMachineBasicBlock(BB);
12390 MachineBasicBlock *sinkMBB = MF->CreateMachineBasicBlock(BB);
12391 MF->insert(I, mainMBB);
12392 MF->insert(I, sinkMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000012393
Michael Liaob118a072012-09-20 03:06:15 +000012394 MachineInstrBuilder MIB;
Scott Michelfdc40a02009-02-17 22:15:04 +000012395
Michael Liaob118a072012-09-20 03:06:15 +000012396 // Transfer the remainder of BB and its successor edges to sinkMBB.
12397 sinkMBB->splice(sinkMBB->begin(), MBB,
12398 llvm::next(MachineBasicBlock::iterator(MI)), MBB->end());
12399 sinkMBB->transferSuccessorsAndUpdatePHIs(MBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000012400
Michael Liaob118a072012-09-20 03:06:15 +000012401 // thisMBB:
12402 // Lo
12403 MIB = BuildMI(thisMBB, DL, TII->get(LOADOpc), X86::EAX);
12404 for (unsigned i = 0; i < X86::AddrNumOperands; ++i)
12405 MIB.addOperand(MI->getOperand(MemOpndSlot + i));
12406 MIB.setMemRefs(MMOBegin, MMOEnd);
12407 // Hi
12408 MIB = BuildMI(thisMBB, DL, TII->get(LOADOpc), X86::EDX);
12409 for (unsigned i = 0; i < X86::AddrNumOperands; ++i) {
Evan Chenga395f4d2012-10-11 00:15:48 +000012410 if (i == X86::AddrDisp)
Michael Liaob118a072012-09-20 03:06:15 +000012411 MIB.addDisp(MI->getOperand(MemOpndSlot + i), 4); // 4 == sizeof(i32)
Evan Chenga395f4d2012-10-11 00:15:48 +000012412 else
Michael Liaob118a072012-09-20 03:06:15 +000012413 MIB.addOperand(MI->getOperand(MemOpndSlot + i));
12414 }
12415 MIB.setMemRefs(MMOBegin, MMOEnd);
Scott Michelfdc40a02009-02-17 22:15:04 +000012416
Michael Liaob118a072012-09-20 03:06:15 +000012417 thisMBB->addSuccessor(mainMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000012418
Michael Liaob118a072012-09-20 03:06:15 +000012419 // mainMBB:
12420 MachineBasicBlock *origMainMBB = mainMBB;
12421 mainMBB->addLiveIn(X86::EAX);
12422 mainMBB->addLiveIn(X86::EDX);
Scott Michelfdc40a02009-02-17 22:15:04 +000012423
Michael Liaob118a072012-09-20 03:06:15 +000012424 // Copy EDX:EAX as they are used more than once.
12425 unsigned LoReg = MRI.createVirtualRegister(RC);
12426 unsigned HiReg = MRI.createVirtualRegister(RC);
12427 BuildMI(mainMBB, DL, TII->get(TargetOpcode::COPY), LoReg).addReg(X86::EAX);
12428 BuildMI(mainMBB, DL, TII->get(TargetOpcode::COPY), HiReg).addReg(X86::EDX);
Mon P Wangab3e7472008-05-05 22:56:23 +000012429
Michael Liaob118a072012-09-20 03:06:15 +000012430 unsigned t1L = MRI.createVirtualRegister(RC);
12431 unsigned t1H = MRI.createVirtualRegister(RC);
Scott Michelfdc40a02009-02-17 22:15:04 +000012432
Michael Liaob118a072012-09-20 03:06:15 +000012433 unsigned Opc = MI->getOpcode();
12434 switch (Opc) {
12435 default:
12436 llvm_unreachable("Unhandled atomic-load-op6432 opcode!");
12437 case X86::ATOMAND6432:
12438 case X86::ATOMOR6432:
12439 case X86::ATOMXOR6432:
12440 case X86::ATOMADD6432:
12441 case X86::ATOMSUB6432: {
12442 unsigned HiOpc;
12443 unsigned LoOpc = getNonAtomic6432Opcode(Opc, HiOpc);
12444 BuildMI(mainMBB, DL, TII->get(LoOpc), t1L).addReg(SrcLoReg).addReg(LoReg);
12445 BuildMI(mainMBB, DL, TII->get(HiOpc), t1H).addReg(SrcHiReg).addReg(HiReg);
12446 break;
12447 }
12448 case X86::ATOMNAND6432: {
12449 unsigned HiOpc, NOTOpc;
12450 unsigned LoOpc = getNonAtomic6432OpcodeWithExtraOpc(Opc, HiOpc, NOTOpc);
12451 unsigned t2L = MRI.createVirtualRegister(RC);
12452 unsigned t2H = MRI.createVirtualRegister(RC);
12453 BuildMI(mainMBB, DL, TII->get(LoOpc), t2L).addReg(SrcLoReg).addReg(LoReg);
12454 BuildMI(mainMBB, DL, TII->get(HiOpc), t2H).addReg(SrcHiReg).addReg(HiReg);
12455 BuildMI(mainMBB, DL, TII->get(NOTOpc), t1L).addReg(t2L);
12456 BuildMI(mainMBB, DL, TII->get(NOTOpc), t1H).addReg(t2H);
12457 break;
12458 }
Michael Liaoe5e8f762012-09-25 18:08:13 +000012459 case X86::ATOMMAX6432:
12460 case X86::ATOMMIN6432:
12461 case X86::ATOMUMAX6432:
12462 case X86::ATOMUMIN6432: {
12463 unsigned HiOpc;
12464 unsigned LoOpc = getNonAtomic6432Opcode(Opc, HiOpc);
12465 unsigned cL = MRI.createVirtualRegister(RC8);
12466 unsigned cH = MRI.createVirtualRegister(RC8);
12467 unsigned cL32 = MRI.createVirtualRegister(RC);
12468 unsigned cH32 = MRI.createVirtualRegister(RC);
12469 unsigned cc = MRI.createVirtualRegister(RC);
12470 // cl := cmp src_lo, lo
12471 BuildMI(mainMBB, DL, TII->get(X86::CMP32rr))
12472 .addReg(SrcLoReg).addReg(LoReg);
12473 BuildMI(mainMBB, DL, TII->get(LoOpc), cL);
12474 BuildMI(mainMBB, DL, TII->get(X86::MOVZX32rr8), cL32).addReg(cL);
12475 // ch := cmp src_hi, hi
12476 BuildMI(mainMBB, DL, TII->get(X86::CMP32rr))
12477 .addReg(SrcHiReg).addReg(HiReg);
12478 BuildMI(mainMBB, DL, TII->get(HiOpc), cH);
12479 BuildMI(mainMBB, DL, TII->get(X86::MOVZX32rr8), cH32).addReg(cH);
12480 // cc := if (src_hi == hi) ? cl : ch;
12481 if (Subtarget->hasCMov()) {
12482 BuildMI(mainMBB, DL, TII->get(X86::CMOVE32rr), cc)
12483 .addReg(cH32).addReg(cL32);
12484 } else {
12485 MIB = BuildMI(mainMBB, DL, TII->get(X86::CMOV_GR32), cc)
12486 .addReg(cH32).addReg(cL32)
12487 .addImm(X86::COND_E);
12488 mainMBB = EmitLoweredSelect(MIB, mainMBB);
12489 }
12490 BuildMI(mainMBB, DL, TII->get(X86::TEST32rr)).addReg(cc).addReg(cc);
12491 if (Subtarget->hasCMov()) {
12492 BuildMI(mainMBB, DL, TII->get(X86::CMOVNE32rr), t1L)
12493 .addReg(SrcLoReg).addReg(LoReg);
12494 BuildMI(mainMBB, DL, TII->get(X86::CMOVNE32rr), t1H)
12495 .addReg(SrcHiReg).addReg(HiReg);
12496 } else {
12497 MIB = BuildMI(mainMBB, DL, TII->get(X86::CMOV_GR32), t1L)
12498 .addReg(SrcLoReg).addReg(LoReg)
12499 .addImm(X86::COND_NE);
12500 mainMBB = EmitLoweredSelect(MIB, mainMBB);
12501 MIB = BuildMI(mainMBB, DL, TII->get(X86::CMOV_GR32), t1H)
12502 .addReg(SrcHiReg).addReg(HiReg)
12503 .addImm(X86::COND_NE);
12504 mainMBB = EmitLoweredSelect(MIB, mainMBB);
12505 }
12506 break;
12507 }
Michael Liaob118a072012-09-20 03:06:15 +000012508 case X86::ATOMSWAP6432: {
12509 unsigned HiOpc;
12510 unsigned LoOpc = getNonAtomic6432Opcode(Opc, HiOpc);
12511 BuildMI(mainMBB, DL, TII->get(LoOpc), t1L).addReg(SrcLoReg);
12512 BuildMI(mainMBB, DL, TII->get(HiOpc), t1H).addReg(SrcHiReg);
12513 break;
12514 }
12515 }
Mon P Wang63307c32008-05-05 19:05:59 +000012516
Michael Liaob118a072012-09-20 03:06:15 +000012517 // Copy EDX:EAX back from HiReg:LoReg
12518 BuildMI(mainMBB, DL, TII->get(TargetOpcode::COPY), X86::EAX).addReg(LoReg);
12519 BuildMI(mainMBB, DL, TII->get(TargetOpcode::COPY), X86::EDX).addReg(HiReg);
12520 // Copy ECX:EBX from t1H:t1L
12521 BuildMI(mainMBB, DL, TII->get(TargetOpcode::COPY), X86::EBX).addReg(t1L);
12522 BuildMI(mainMBB, DL, TII->get(TargetOpcode::COPY), X86::ECX).addReg(t1H);
Mon P Wangab3e7472008-05-05 22:56:23 +000012523
Michael Liaob118a072012-09-20 03:06:15 +000012524 MIB = BuildMI(mainMBB, DL, TII->get(LCMPXCHGOpc));
12525 for (unsigned i = 0; i < X86::AddrNumOperands; ++i)
12526 MIB.addOperand(MI->getOperand(MemOpndSlot + i));
12527 MIB.setMemRefs(MMOBegin, MMOEnd);
Mon P Wang63307c32008-05-05 19:05:59 +000012528
Michael Liaob118a072012-09-20 03:06:15 +000012529 BuildMI(mainMBB, DL, TII->get(X86::JNE_4)).addMBB(origMainMBB);
Mon P Wang63307c32008-05-05 19:05:59 +000012530
Michael Liaob118a072012-09-20 03:06:15 +000012531 mainMBB->addSuccessor(origMainMBB);
12532 mainMBB->addSuccessor(sinkMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000012533
Michael Liaob118a072012-09-20 03:06:15 +000012534 // sinkMBB:
12535 sinkMBB->addLiveIn(X86::EAX);
12536 sinkMBB->addLiveIn(X86::EDX);
Scott Michelfdc40a02009-02-17 22:15:04 +000012537
Michael Liaob118a072012-09-20 03:06:15 +000012538 BuildMI(*sinkMBB, sinkMBB->begin(), DL,
12539 TII->get(TargetOpcode::COPY), DstLoReg)
12540 .addReg(X86::EAX);
12541 BuildMI(*sinkMBB, sinkMBB->begin(), DL,
12542 TII->get(TargetOpcode::COPY), DstHiReg)
12543 .addReg(X86::EDX);
Mon P Wang63307c32008-05-05 19:05:59 +000012544
Michael Liaob118a072012-09-20 03:06:15 +000012545 MI->eraseFromParent();
12546 return sinkMBB;
Mon P Wang63307c32008-05-05 19:05:59 +000012547}
12548
Eric Christopherf83a5de2009-08-27 18:08:16 +000012549// FIXME: When we get size specific XMM0 registers, i.e. XMM0_V16I8
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000012550// or XMM0_V32I8 in AVX all of this code can be replaced with that
12551// in the .td file.
Dan Gohmand6708ea2009-08-15 01:38:56 +000012552MachineBasicBlock *
Eric Christopherb120ab42009-08-18 22:50:32 +000012553X86TargetLowering::EmitPCMP(MachineInstr *MI, MachineBasicBlock *BB,
Daniel Dunbara279bc32009-09-20 02:20:51 +000012554 unsigned numArgs, bool memArg) const {
Craig Topperd0a31172012-01-10 06:37:29 +000012555 assert(Subtarget->hasSSE42() &&
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000012556 "Target must have SSE4.2 or AVX features enabled");
12557
Eric Christopherb120ab42009-08-18 22:50:32 +000012558 DebugLoc dl = MI->getDebugLoc();
12559 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Eric Christopherb120ab42009-08-18 22:50:32 +000012560 unsigned Opc;
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000012561 if (!Subtarget->hasAVX()) {
12562 if (memArg)
12563 Opc = numArgs == 3 ? X86::PCMPISTRM128rm : X86::PCMPESTRM128rm;
12564 else
12565 Opc = numArgs == 3 ? X86::PCMPISTRM128rr : X86::PCMPESTRM128rr;
12566 } else {
12567 if (memArg)
12568 Opc = numArgs == 3 ? X86::VPCMPISTRM128rm : X86::VPCMPESTRM128rm;
12569 else
12570 Opc = numArgs == 3 ? X86::VPCMPISTRM128rr : X86::VPCMPESTRM128rr;
12571 }
Eric Christopherb120ab42009-08-18 22:50:32 +000012572
Eric Christopher41c902f2010-11-30 08:20:21 +000012573 MachineInstrBuilder MIB = BuildMI(*BB, MI, dl, TII->get(Opc));
Eric Christopherb120ab42009-08-18 22:50:32 +000012574 for (unsigned i = 0; i < numArgs; ++i) {
12575 MachineOperand &Op = MI->getOperand(i+1);
Eric Christopherb120ab42009-08-18 22:50:32 +000012576 if (!(Op.isReg() && Op.isImplicit()))
12577 MIB.addOperand(Op);
12578 }
Bruno Cardoso Lopes5affa512011-08-31 03:04:09 +000012579 BuildMI(*BB, MI, dl,
Craig Topper638aa682012-08-05 00:17:48 +000012580 TII->get(TargetOpcode::COPY), MI->getOperand(0).getReg())
Eric Christopherb120ab42009-08-18 22:50:32 +000012581 .addReg(X86::XMM0);
12582
Dan Gohman14152b42010-07-06 20:24:04 +000012583 MI->eraseFromParent();
Eric Christopherb120ab42009-08-18 22:50:32 +000012584 return BB;
12585}
12586
12587MachineBasicBlock *
Eric Christopher228232b2010-11-30 07:20:12 +000012588X86TargetLowering::EmitMonitor(MachineInstr *MI, MachineBasicBlock *BB) const {
Eric Christopher228232b2010-11-30 07:20:12 +000012589 DebugLoc dl = MI->getDebugLoc();
12590 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000012591
Eric Christopher228232b2010-11-30 07:20:12 +000012592 // Address into RAX/EAX, other two args into ECX, EDX.
12593 unsigned MemOpc = Subtarget->is64Bit() ? X86::LEA64r : X86::LEA32r;
12594 unsigned MemReg = Subtarget->is64Bit() ? X86::RAX : X86::EAX;
12595 MachineInstrBuilder MIB = BuildMI(*BB, MI, dl, TII->get(MemOpc), MemReg);
12596 for (int i = 0; i < X86::AddrNumOperands; ++i)
Eric Christopher82be2202010-11-30 08:10:28 +000012597 MIB.addOperand(MI->getOperand(i));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000012598
Eric Christopher228232b2010-11-30 07:20:12 +000012599 unsigned ValOps = X86::AddrNumOperands;
12600 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::ECX)
12601 .addReg(MI->getOperand(ValOps).getReg());
12602 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::EDX)
12603 .addReg(MI->getOperand(ValOps+1).getReg());
12604
12605 // The instruction doesn't actually take any operands though.
12606 BuildMI(*BB, MI, dl, TII->get(X86::MONITORrrr));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000012607
Eric Christopher228232b2010-11-30 07:20:12 +000012608 MI->eraseFromParent(); // The pseudo is gone now.
12609 return BB;
12610}
12611
12612MachineBasicBlock *
Dan Gohman320afb82010-10-12 18:00:49 +000012613X86TargetLowering::EmitVAARG64WithCustomInserter(
12614 MachineInstr *MI,
12615 MachineBasicBlock *MBB) const {
12616 // Emit va_arg instruction on X86-64.
12617
12618 // Operands to this pseudo-instruction:
12619 // 0 ) Output : destination address (reg)
12620 // 1-5) Input : va_list address (addr, i64mem)
12621 // 6 ) ArgSize : Size (in bytes) of vararg type
12622 // 7 ) ArgMode : 0=overflow only, 1=use gp_offset, 2=use fp_offset
12623 // 8 ) Align : Alignment of type
12624 // 9 ) EFLAGS (implicit-def)
12625
12626 assert(MI->getNumOperands() == 10 && "VAARG_64 should have 10 operands!");
12627 assert(X86::AddrNumOperands == 5 && "VAARG_64 assumes 5 address operands");
12628
12629 unsigned DestReg = MI->getOperand(0).getReg();
12630 MachineOperand &Base = MI->getOperand(1);
12631 MachineOperand &Scale = MI->getOperand(2);
12632 MachineOperand &Index = MI->getOperand(3);
12633 MachineOperand &Disp = MI->getOperand(4);
12634 MachineOperand &Segment = MI->getOperand(5);
12635 unsigned ArgSize = MI->getOperand(6).getImm();
12636 unsigned ArgMode = MI->getOperand(7).getImm();
12637 unsigned Align = MI->getOperand(8).getImm();
12638
12639 // Memory Reference
12640 assert(MI->hasOneMemOperand() && "Expected VAARG_64 to have one memoperand");
12641 MachineInstr::mmo_iterator MMOBegin = MI->memoperands_begin();
12642 MachineInstr::mmo_iterator MMOEnd = MI->memoperands_end();
12643
12644 // Machine Information
12645 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
12646 MachineRegisterInfo &MRI = MBB->getParent()->getRegInfo();
12647 const TargetRegisterClass *AddrRegClass = getRegClassFor(MVT::i64);
12648 const TargetRegisterClass *OffsetRegClass = getRegClassFor(MVT::i32);
12649 DebugLoc DL = MI->getDebugLoc();
12650
12651 // struct va_list {
12652 // i32 gp_offset
12653 // i32 fp_offset
12654 // i64 overflow_area (address)
12655 // i64 reg_save_area (address)
12656 // }
12657 // sizeof(va_list) = 24
12658 // alignment(va_list) = 8
12659
12660 unsigned TotalNumIntRegs = 6;
12661 unsigned TotalNumXMMRegs = 8;
12662 bool UseGPOffset = (ArgMode == 1);
12663 bool UseFPOffset = (ArgMode == 2);
12664 unsigned MaxOffset = TotalNumIntRegs * 8 +
12665 (UseFPOffset ? TotalNumXMMRegs * 16 : 0);
12666
12667 /* Align ArgSize to a multiple of 8 */
12668 unsigned ArgSizeA8 = (ArgSize + 7) & ~7;
12669 bool NeedsAlign = (Align > 8);
12670
12671 MachineBasicBlock *thisMBB = MBB;
12672 MachineBasicBlock *overflowMBB;
12673 MachineBasicBlock *offsetMBB;
12674 MachineBasicBlock *endMBB;
12675
12676 unsigned OffsetDestReg = 0; // Argument address computed by offsetMBB
12677 unsigned OverflowDestReg = 0; // Argument address computed by overflowMBB
12678 unsigned OffsetReg = 0;
12679
12680 if (!UseGPOffset && !UseFPOffset) {
12681 // If we only pull from the overflow region, we don't create a branch.
12682 // We don't need to alter control flow.
12683 OffsetDestReg = 0; // unused
12684 OverflowDestReg = DestReg;
12685
12686 offsetMBB = NULL;
12687 overflowMBB = thisMBB;
12688 endMBB = thisMBB;
12689 } else {
12690 // First emit code to check if gp_offset (or fp_offset) is below the bound.
12691 // If so, pull the argument from reg_save_area. (branch to offsetMBB)
12692 // If not, pull from overflow_area. (branch to overflowMBB)
12693 //
12694 // thisMBB
12695 // | .
12696 // | .
12697 // offsetMBB overflowMBB
12698 // | .
12699 // | .
12700 // endMBB
12701
12702 // Registers for the PHI in endMBB
12703 OffsetDestReg = MRI.createVirtualRegister(AddrRegClass);
12704 OverflowDestReg = MRI.createVirtualRegister(AddrRegClass);
12705
12706 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
12707 MachineFunction *MF = MBB->getParent();
12708 overflowMBB = MF->CreateMachineBasicBlock(LLVM_BB);
12709 offsetMBB = MF->CreateMachineBasicBlock(LLVM_BB);
12710 endMBB = MF->CreateMachineBasicBlock(LLVM_BB);
12711
12712 MachineFunction::iterator MBBIter = MBB;
12713 ++MBBIter;
12714
12715 // Insert the new basic blocks
12716 MF->insert(MBBIter, offsetMBB);
12717 MF->insert(MBBIter, overflowMBB);
12718 MF->insert(MBBIter, endMBB);
12719
12720 // Transfer the remainder of MBB and its successor edges to endMBB.
12721 endMBB->splice(endMBB->begin(), thisMBB,
12722 llvm::next(MachineBasicBlock::iterator(MI)),
12723 thisMBB->end());
12724 endMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
12725
12726 // Make offsetMBB and overflowMBB successors of thisMBB
12727 thisMBB->addSuccessor(offsetMBB);
12728 thisMBB->addSuccessor(overflowMBB);
12729
12730 // endMBB is a successor of both offsetMBB and overflowMBB
12731 offsetMBB->addSuccessor(endMBB);
12732 overflowMBB->addSuccessor(endMBB);
12733
12734 // Load the offset value into a register
12735 OffsetReg = MRI.createVirtualRegister(OffsetRegClass);
12736 BuildMI(thisMBB, DL, TII->get(X86::MOV32rm), OffsetReg)
12737 .addOperand(Base)
12738 .addOperand(Scale)
12739 .addOperand(Index)
12740 .addDisp(Disp, UseFPOffset ? 4 : 0)
12741 .addOperand(Segment)
12742 .setMemRefs(MMOBegin, MMOEnd);
12743
12744 // Check if there is enough room left to pull this argument.
12745 BuildMI(thisMBB, DL, TII->get(X86::CMP32ri))
12746 .addReg(OffsetReg)
12747 .addImm(MaxOffset + 8 - ArgSizeA8);
12748
12749 // Branch to "overflowMBB" if offset >= max
12750 // Fall through to "offsetMBB" otherwise
12751 BuildMI(thisMBB, DL, TII->get(X86::GetCondBranchFromCond(X86::COND_AE)))
12752 .addMBB(overflowMBB);
12753 }
12754
12755 // In offsetMBB, emit code to use the reg_save_area.
12756 if (offsetMBB) {
12757 assert(OffsetReg != 0);
12758
12759 // Read the reg_save_area address.
12760 unsigned RegSaveReg = MRI.createVirtualRegister(AddrRegClass);
12761 BuildMI(offsetMBB, DL, TII->get(X86::MOV64rm), RegSaveReg)
12762 .addOperand(Base)
12763 .addOperand(Scale)
12764 .addOperand(Index)
12765 .addDisp(Disp, 16)
12766 .addOperand(Segment)
12767 .setMemRefs(MMOBegin, MMOEnd);
12768
12769 // Zero-extend the offset
12770 unsigned OffsetReg64 = MRI.createVirtualRegister(AddrRegClass);
12771 BuildMI(offsetMBB, DL, TII->get(X86::SUBREG_TO_REG), OffsetReg64)
12772 .addImm(0)
12773 .addReg(OffsetReg)
12774 .addImm(X86::sub_32bit);
12775
12776 // Add the offset to the reg_save_area to get the final address.
12777 BuildMI(offsetMBB, DL, TII->get(X86::ADD64rr), OffsetDestReg)
12778 .addReg(OffsetReg64)
12779 .addReg(RegSaveReg);
12780
12781 // Compute the offset for the next argument
12782 unsigned NextOffsetReg = MRI.createVirtualRegister(OffsetRegClass);
12783 BuildMI(offsetMBB, DL, TII->get(X86::ADD32ri), NextOffsetReg)
12784 .addReg(OffsetReg)
12785 .addImm(UseFPOffset ? 16 : 8);
12786
12787 // Store it back into the va_list.
12788 BuildMI(offsetMBB, DL, TII->get(X86::MOV32mr))
12789 .addOperand(Base)
12790 .addOperand(Scale)
12791 .addOperand(Index)
12792 .addDisp(Disp, UseFPOffset ? 4 : 0)
12793 .addOperand(Segment)
12794 .addReg(NextOffsetReg)
12795 .setMemRefs(MMOBegin, MMOEnd);
12796
12797 // Jump to endMBB
12798 BuildMI(offsetMBB, DL, TII->get(X86::JMP_4))
12799 .addMBB(endMBB);
12800 }
12801
12802 //
12803 // Emit code to use overflow area
12804 //
12805
12806 // Load the overflow_area address into a register.
12807 unsigned OverflowAddrReg = MRI.createVirtualRegister(AddrRegClass);
12808 BuildMI(overflowMBB, DL, TII->get(X86::MOV64rm), OverflowAddrReg)
12809 .addOperand(Base)
12810 .addOperand(Scale)
12811 .addOperand(Index)
12812 .addDisp(Disp, 8)
12813 .addOperand(Segment)
12814 .setMemRefs(MMOBegin, MMOEnd);
12815
12816 // If we need to align it, do so. Otherwise, just copy the address
12817 // to OverflowDestReg.
12818 if (NeedsAlign) {
12819 // Align the overflow address
12820 assert((Align & (Align-1)) == 0 && "Alignment must be a power of 2");
12821 unsigned TmpReg = MRI.createVirtualRegister(AddrRegClass);
12822
12823 // aligned_addr = (addr + (align-1)) & ~(align-1)
12824 BuildMI(overflowMBB, DL, TII->get(X86::ADD64ri32), TmpReg)
12825 .addReg(OverflowAddrReg)
12826 .addImm(Align-1);
12827
12828 BuildMI(overflowMBB, DL, TII->get(X86::AND64ri32), OverflowDestReg)
12829 .addReg(TmpReg)
12830 .addImm(~(uint64_t)(Align-1));
12831 } else {
12832 BuildMI(overflowMBB, DL, TII->get(TargetOpcode::COPY), OverflowDestReg)
12833 .addReg(OverflowAddrReg);
12834 }
12835
12836 // Compute the next overflow address after this argument.
12837 // (the overflow address should be kept 8-byte aligned)
12838 unsigned NextAddrReg = MRI.createVirtualRegister(AddrRegClass);
12839 BuildMI(overflowMBB, DL, TII->get(X86::ADD64ri32), NextAddrReg)
12840 .addReg(OverflowDestReg)
12841 .addImm(ArgSizeA8);
12842
12843 // Store the new overflow address.
12844 BuildMI(overflowMBB, DL, TII->get(X86::MOV64mr))
12845 .addOperand(Base)
12846 .addOperand(Scale)
12847 .addOperand(Index)
12848 .addDisp(Disp, 8)
12849 .addOperand(Segment)
12850 .addReg(NextAddrReg)
12851 .setMemRefs(MMOBegin, MMOEnd);
12852
12853 // If we branched, emit the PHI to the front of endMBB.
12854 if (offsetMBB) {
12855 BuildMI(*endMBB, endMBB->begin(), DL,
12856 TII->get(X86::PHI), DestReg)
12857 .addReg(OffsetDestReg).addMBB(offsetMBB)
12858 .addReg(OverflowDestReg).addMBB(overflowMBB);
12859 }
12860
12861 // Erase the pseudo instruction
12862 MI->eraseFromParent();
12863
12864 return endMBB;
12865}
12866
12867MachineBasicBlock *
Dan Gohmand6708ea2009-08-15 01:38:56 +000012868X86TargetLowering::EmitVAStartSaveXMMRegsWithCustomInserter(
12869 MachineInstr *MI,
12870 MachineBasicBlock *MBB) const {
12871 // Emit code to save XMM registers to the stack. The ABI says that the
12872 // number of registers to save is given in %al, so it's theoretically
12873 // possible to do an indirect jump trick to avoid saving all of them,
12874 // however this code takes a simpler approach and just executes all
12875 // of the stores if %al is non-zero. It's less code, and it's probably
12876 // easier on the hardware branch predictor, and stores aren't all that
12877 // expensive anyway.
12878
12879 // Create the new basic blocks. One block contains all the XMM stores,
12880 // and one block is the final destination regardless of whether any
12881 // stores were performed.
12882 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
12883 MachineFunction *F = MBB->getParent();
12884 MachineFunction::iterator MBBIter = MBB;
12885 ++MBBIter;
12886 MachineBasicBlock *XMMSaveMBB = F->CreateMachineBasicBlock(LLVM_BB);
12887 MachineBasicBlock *EndMBB = F->CreateMachineBasicBlock(LLVM_BB);
12888 F->insert(MBBIter, XMMSaveMBB);
12889 F->insert(MBBIter, EndMBB);
12890
Dan Gohman14152b42010-07-06 20:24:04 +000012891 // Transfer the remainder of MBB and its successor edges to EndMBB.
12892 EndMBB->splice(EndMBB->begin(), MBB,
12893 llvm::next(MachineBasicBlock::iterator(MI)),
12894 MBB->end());
12895 EndMBB->transferSuccessorsAndUpdatePHIs(MBB);
12896
Dan Gohmand6708ea2009-08-15 01:38:56 +000012897 // The original block will now fall through to the XMM save block.
12898 MBB->addSuccessor(XMMSaveMBB);
12899 // The XMMSaveMBB will fall through to the end block.
12900 XMMSaveMBB->addSuccessor(EndMBB);
12901
12902 // Now add the instructions.
12903 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
12904 DebugLoc DL = MI->getDebugLoc();
12905
12906 unsigned CountReg = MI->getOperand(0).getReg();
12907 int64_t RegSaveFrameIndex = MI->getOperand(1).getImm();
12908 int64_t VarArgsFPOffset = MI->getOperand(2).getImm();
12909
12910 if (!Subtarget->isTargetWin64()) {
12911 // If %al is 0, branch around the XMM save block.
12912 BuildMI(MBB, DL, TII->get(X86::TEST8rr)).addReg(CountReg).addReg(CountReg);
Chris Lattnerbd13fb62010-02-11 19:25:55 +000012913 BuildMI(MBB, DL, TII->get(X86::JE_4)).addMBB(EndMBB);
Dan Gohmand6708ea2009-08-15 01:38:56 +000012914 MBB->addSuccessor(EndMBB);
12915 }
12916
Bruno Cardoso Lopes5affa512011-08-31 03:04:09 +000012917 unsigned MOVOpc = Subtarget->hasAVX() ? X86::VMOVAPSmr : X86::MOVAPSmr;
Dan Gohmand6708ea2009-08-15 01:38:56 +000012918 // In the XMM save block, save all the XMM argument registers.
12919 for (int i = 3, e = MI->getNumOperands(); i != e; ++i) {
12920 int64_t Offset = (i - 3) * 16 + VarArgsFPOffset;
Dan Gohmanc76909a2009-09-25 20:36:54 +000012921 MachineMemOperand *MMO =
Evan Chengff89dcb2009-10-18 18:16:27 +000012922 F->getMachineMemOperand(
Chris Lattnere8639032010-09-21 06:22:23 +000012923 MachinePointerInfo::getFixedStack(RegSaveFrameIndex, Offset),
Chris Lattner59db5492010-09-21 04:39:43 +000012924 MachineMemOperand::MOStore,
Evan Chengff89dcb2009-10-18 18:16:27 +000012925 /*Size=*/16, /*Align=*/16);
Bruno Cardoso Lopes5affa512011-08-31 03:04:09 +000012926 BuildMI(XMMSaveMBB, DL, TII->get(MOVOpc))
Dan Gohmand6708ea2009-08-15 01:38:56 +000012927 .addFrameIndex(RegSaveFrameIndex)
12928 .addImm(/*Scale=*/1)
12929 .addReg(/*IndexReg=*/0)
12930 .addImm(/*Disp=*/Offset)
12931 .addReg(/*Segment=*/0)
12932 .addReg(MI->getOperand(i).getReg())
Dan Gohmanc76909a2009-09-25 20:36:54 +000012933 .addMemOperand(MMO);
Dan Gohmand6708ea2009-08-15 01:38:56 +000012934 }
12935
Dan Gohman14152b42010-07-06 20:24:04 +000012936 MI->eraseFromParent(); // The pseudo instruction is gone now.
Dan Gohmand6708ea2009-08-15 01:38:56 +000012937
12938 return EndMBB;
12939}
Mon P Wang63307c32008-05-05 19:05:59 +000012940
Lang Hames6e3f7e42012-02-03 01:13:49 +000012941// The EFLAGS operand of SelectItr might be missing a kill marker
12942// because there were multiple uses of EFLAGS, and ISel didn't know
12943// which to mark. Figure out whether SelectItr should have had a
12944// kill marker, and set it if it should. Returns the correct kill
12945// marker value.
12946static bool checkAndUpdateEFLAGSKill(MachineBasicBlock::iterator SelectItr,
12947 MachineBasicBlock* BB,
12948 const TargetRegisterInfo* TRI) {
12949 // Scan forward through BB for a use/def of EFLAGS.
12950 MachineBasicBlock::iterator miI(llvm::next(SelectItr));
12951 for (MachineBasicBlock::iterator miE = BB->end(); miI != miE; ++miI) {
Lang Hames50a36f72012-02-02 07:48:37 +000012952 const MachineInstr& mi = *miI;
Lang Hames6e3f7e42012-02-03 01:13:49 +000012953 if (mi.readsRegister(X86::EFLAGS))
Lang Hames50a36f72012-02-02 07:48:37 +000012954 return false;
Lang Hames6e3f7e42012-02-03 01:13:49 +000012955 if (mi.definesRegister(X86::EFLAGS))
12956 break; // Should have kill-flag - update below.
12957 }
12958
12959 // If we hit the end of the block, check whether EFLAGS is live into a
12960 // successor.
12961 if (miI == BB->end()) {
12962 for (MachineBasicBlock::succ_iterator sItr = BB->succ_begin(),
12963 sEnd = BB->succ_end();
12964 sItr != sEnd; ++sItr) {
12965 MachineBasicBlock* succ = *sItr;
12966 if (succ->isLiveIn(X86::EFLAGS))
12967 return false;
Lang Hames50a36f72012-02-02 07:48:37 +000012968 }
12969 }
12970
Lang Hames6e3f7e42012-02-03 01:13:49 +000012971 // We found a def, or hit the end of the basic block and EFLAGS wasn't live
12972 // out. SelectMI should have a kill flag on EFLAGS.
12973 SelectItr->addRegisterKilled(X86::EFLAGS, TRI);
Lang Hames50a36f72012-02-02 07:48:37 +000012974 return true;
12975}
12976
Evan Cheng60c07e12006-07-05 22:17:51 +000012977MachineBasicBlock *
Chris Lattner52600972009-09-02 05:57:00 +000012978X86TargetLowering::EmitLoweredSelect(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000012979 MachineBasicBlock *BB) const {
Chris Lattner52600972009-09-02 05:57:00 +000012980 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
12981 DebugLoc DL = MI->getDebugLoc();
Daniel Dunbara279bc32009-09-20 02:20:51 +000012982
Chris Lattner52600972009-09-02 05:57:00 +000012983 // To "insert" a SELECT_CC instruction, we actually have to insert the
12984 // diamond control-flow pattern. The incoming instruction knows the
12985 // destination vreg to set, the condition code register to branch on, the
12986 // true/false values to select between, and a branch opcode to use.
12987 const BasicBlock *LLVM_BB = BB->getBasicBlock();
12988 MachineFunction::iterator It = BB;
12989 ++It;
Daniel Dunbara279bc32009-09-20 02:20:51 +000012990
Chris Lattner52600972009-09-02 05:57:00 +000012991 // thisMBB:
12992 // ...
12993 // TrueVal = ...
12994 // cmpTY ccX, r1, r2
12995 // bCC copy1MBB
12996 // fallthrough --> copy0MBB
12997 MachineBasicBlock *thisMBB = BB;
12998 MachineFunction *F = BB->getParent();
12999 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
13000 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Chris Lattner52600972009-09-02 05:57:00 +000013001 F->insert(It, copy0MBB);
13002 F->insert(It, sinkMBB);
Bill Wendling730c07e2010-06-25 20:48:10 +000013003
Bill Wendling730c07e2010-06-25 20:48:10 +000013004 // If the EFLAGS register isn't dead in the terminator, then claim that it's
13005 // live into the sink and copy blocks.
Lang Hames6e3f7e42012-02-03 01:13:49 +000013006 const TargetRegisterInfo* TRI = getTargetMachine().getRegisterInfo();
13007 if (!MI->killsRegister(X86::EFLAGS) &&
13008 !checkAndUpdateEFLAGSKill(MI, BB, TRI)) {
13009 copy0MBB->addLiveIn(X86::EFLAGS);
13010 sinkMBB->addLiveIn(X86::EFLAGS);
Bill Wendling730c07e2010-06-25 20:48:10 +000013011 }
13012
Dan Gohman14152b42010-07-06 20:24:04 +000013013 // Transfer the remainder of BB and its successor edges to sinkMBB.
13014 sinkMBB->splice(sinkMBB->begin(), BB,
13015 llvm::next(MachineBasicBlock::iterator(MI)),
13016 BB->end());
13017 sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
13018
13019 // Add the true and fallthrough blocks as its successors.
13020 BB->addSuccessor(copy0MBB);
13021 BB->addSuccessor(sinkMBB);
13022
13023 // Create the conditional branch instruction.
13024 unsigned Opc =
13025 X86::GetCondBranchFromCond((X86::CondCode)MI->getOperand(3).getImm());
13026 BuildMI(BB, DL, TII->get(Opc)).addMBB(sinkMBB);
13027
Chris Lattner52600972009-09-02 05:57:00 +000013028 // copy0MBB:
13029 // %FalseValue = ...
13030 // # fallthrough to sinkMBB
Dan Gohman3335a222010-04-30 20:14:26 +000013031 copy0MBB->addSuccessor(sinkMBB);
Daniel Dunbara279bc32009-09-20 02:20:51 +000013032
Chris Lattner52600972009-09-02 05:57:00 +000013033 // sinkMBB:
13034 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
13035 // ...
Dan Gohman14152b42010-07-06 20:24:04 +000013036 BuildMI(*sinkMBB, sinkMBB->begin(), DL,
13037 TII->get(X86::PHI), MI->getOperand(0).getReg())
Chris Lattner52600972009-09-02 05:57:00 +000013038 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
13039 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
13040
Dan Gohman14152b42010-07-06 20:24:04 +000013041 MI->eraseFromParent(); // The pseudo instruction is gone now.
Dan Gohman3335a222010-04-30 20:14:26 +000013042 return sinkMBB;
Chris Lattner52600972009-09-02 05:57:00 +000013043}
13044
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000013045MachineBasicBlock *
Rafael Espindola151ab3e2011-08-30 19:47:04 +000013046X86TargetLowering::EmitLoweredSegAlloca(MachineInstr *MI, MachineBasicBlock *BB,
13047 bool Is64Bit) const {
13048 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
13049 DebugLoc DL = MI->getDebugLoc();
13050 MachineFunction *MF = BB->getParent();
13051 const BasicBlock *LLVM_BB = BB->getBasicBlock();
13052
Nick Lewycky8a8d4792011-12-02 22:16:29 +000013053 assert(getTargetMachine().Options.EnableSegmentedStacks);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000013054
13055 unsigned TlsReg = Is64Bit ? X86::FS : X86::GS;
13056 unsigned TlsOffset = Is64Bit ? 0x70 : 0x30;
13057
13058 // BB:
13059 // ... [Till the alloca]
13060 // If stacklet is not large enough, jump to mallocMBB
13061 //
13062 // bumpMBB:
13063 // Allocate by subtracting from RSP
13064 // Jump to continueMBB
13065 //
13066 // mallocMBB:
13067 // Allocate by call to runtime
13068 //
13069 // continueMBB:
13070 // ...
13071 // [rest of original BB]
13072 //
13073
13074 MachineBasicBlock *mallocMBB = MF->CreateMachineBasicBlock(LLVM_BB);
13075 MachineBasicBlock *bumpMBB = MF->CreateMachineBasicBlock(LLVM_BB);
13076 MachineBasicBlock *continueMBB = MF->CreateMachineBasicBlock(LLVM_BB);
13077
13078 MachineRegisterInfo &MRI = MF->getRegInfo();
13079 const TargetRegisterClass *AddrRegClass =
13080 getRegClassFor(Is64Bit ? MVT::i64:MVT::i32);
13081
13082 unsigned mallocPtrVReg = MRI.createVirtualRegister(AddrRegClass),
13083 bumpSPPtrVReg = MRI.createVirtualRegister(AddrRegClass),
13084 tmpSPVReg = MRI.createVirtualRegister(AddrRegClass),
Rafael Espindola66bf7432011-10-26 21:16:41 +000013085 SPLimitVReg = MRI.createVirtualRegister(AddrRegClass),
Rafael Espindola151ab3e2011-08-30 19:47:04 +000013086 sizeVReg = MI->getOperand(1).getReg(),
13087 physSPReg = Is64Bit ? X86::RSP : X86::ESP;
13088
13089 MachineFunction::iterator MBBIter = BB;
13090 ++MBBIter;
13091
13092 MF->insert(MBBIter, bumpMBB);
13093 MF->insert(MBBIter, mallocMBB);
13094 MF->insert(MBBIter, continueMBB);
13095
13096 continueMBB->splice(continueMBB->begin(), BB, llvm::next
13097 (MachineBasicBlock::iterator(MI)), BB->end());
13098 continueMBB->transferSuccessorsAndUpdatePHIs(BB);
13099
13100 // Add code to the main basic block to check if the stack limit has been hit,
13101 // and if so, jump to mallocMBB otherwise to bumpMBB.
13102 BuildMI(BB, DL, TII->get(TargetOpcode::COPY), tmpSPVReg).addReg(physSPReg);
Rafael Espindola66bf7432011-10-26 21:16:41 +000013103 BuildMI(BB, DL, TII->get(Is64Bit ? X86::SUB64rr:X86::SUB32rr), SPLimitVReg)
Rafael Espindola151ab3e2011-08-30 19:47:04 +000013104 .addReg(tmpSPVReg).addReg(sizeVReg);
13105 BuildMI(BB, DL, TII->get(Is64Bit ? X86::CMP64mr:X86::CMP32mr))
Rafael Espindola014f7a32012-01-11 18:14:03 +000013106 .addReg(0).addImm(1).addReg(0).addImm(TlsOffset).addReg(TlsReg)
Rafael Espindola66bf7432011-10-26 21:16:41 +000013107 .addReg(SPLimitVReg);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000013108 BuildMI(BB, DL, TII->get(X86::JG_4)).addMBB(mallocMBB);
13109
13110 // bumpMBB simply decreases the stack pointer, since we know the current
13111 // stacklet has enough space.
13112 BuildMI(bumpMBB, DL, TII->get(TargetOpcode::COPY), physSPReg)
Rafael Espindola66bf7432011-10-26 21:16:41 +000013113 .addReg(SPLimitVReg);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000013114 BuildMI(bumpMBB, DL, TII->get(TargetOpcode::COPY), bumpSPPtrVReg)
Rafael Espindola66bf7432011-10-26 21:16:41 +000013115 .addReg(SPLimitVReg);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000013116 BuildMI(bumpMBB, DL, TII->get(X86::JMP_4)).addMBB(continueMBB);
13117
13118 // Calls into a routine in libgcc to allocate more space from the heap.
Jakob Stoklund Olesen8bcde2a2012-02-16 00:02:50 +000013119 const uint32_t *RegMask =
13120 getTargetMachine().getRegisterInfo()->getCallPreservedMask(CallingConv::C);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000013121 if (Is64Bit) {
13122 BuildMI(mallocMBB, DL, TII->get(X86::MOV64rr), X86::RDI)
13123 .addReg(sizeVReg);
13124 BuildMI(mallocMBB, DL, TII->get(X86::CALL64pcrel32))
Jakob Stoklund Olesen85dccf12012-07-04 23:53:27 +000013125 .addExternalSymbol("__morestack_allocate_stack_space")
Jakob Stoklund Olesen8bcde2a2012-02-16 00:02:50 +000013126 .addRegMask(RegMask)
Jakob Stoklund Olesen85dccf12012-07-04 23:53:27 +000013127 .addReg(X86::RDI, RegState::Implicit)
Jakob Stoklund Olesen8bcde2a2012-02-16 00:02:50 +000013128 .addReg(X86::RAX, RegState::ImplicitDefine);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000013129 } else {
13130 BuildMI(mallocMBB, DL, TII->get(X86::SUB32ri), physSPReg).addReg(physSPReg)
13131 .addImm(12);
13132 BuildMI(mallocMBB, DL, TII->get(X86::PUSH32r)).addReg(sizeVReg);
13133 BuildMI(mallocMBB, DL, TII->get(X86::CALLpcrel32))
Jakob Stoklund Olesen8bcde2a2012-02-16 00:02:50 +000013134 .addExternalSymbol("__morestack_allocate_stack_space")
13135 .addRegMask(RegMask)
13136 .addReg(X86::EAX, RegState::ImplicitDefine);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000013137 }
13138
13139 if (!Is64Bit)
13140 BuildMI(mallocMBB, DL, TII->get(X86::ADD32ri), physSPReg).addReg(physSPReg)
13141 .addImm(16);
13142
13143 BuildMI(mallocMBB, DL, TII->get(TargetOpcode::COPY), mallocPtrVReg)
13144 .addReg(Is64Bit ? X86::RAX : X86::EAX);
13145 BuildMI(mallocMBB, DL, TII->get(X86::JMP_4)).addMBB(continueMBB);
13146
13147 // Set up the CFG correctly.
13148 BB->addSuccessor(bumpMBB);
13149 BB->addSuccessor(mallocMBB);
13150 mallocMBB->addSuccessor(continueMBB);
13151 bumpMBB->addSuccessor(continueMBB);
13152
13153 // Take care of the PHI nodes.
13154 BuildMI(*continueMBB, continueMBB->begin(), DL, TII->get(X86::PHI),
13155 MI->getOperand(0).getReg())
13156 .addReg(mallocPtrVReg).addMBB(mallocMBB)
13157 .addReg(bumpSPPtrVReg).addMBB(bumpMBB);
13158
13159 // Delete the original pseudo instruction.
13160 MI->eraseFromParent();
13161
13162 // And we're done.
13163 return continueMBB;
13164}
13165
13166MachineBasicBlock *
Michael J. Spencere9c253e2010-10-21 01:41:01 +000013167X86TargetLowering::EmitLoweredWinAlloca(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000013168 MachineBasicBlock *BB) const {
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000013169 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
13170 DebugLoc DL = MI->getDebugLoc();
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000013171
NAKAMURA Takumia2e07622011-03-24 07:07:00 +000013172 assert(!Subtarget->isTargetEnvMacho());
13173
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000013174 // The lowering is pretty easy: we're just emitting the call to _alloca. The
13175 // non-trivial part is impdef of ESP.
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000013176
NAKAMURA Takumia2e07622011-03-24 07:07:00 +000013177 if (Subtarget->isTargetWin64()) {
13178 if (Subtarget->isTargetCygMing()) {
13179 // ___chkstk(Mingw64):
13180 // Clobbers R10, R11, RAX and EFLAGS.
13181 // Updates RSP.
13182 BuildMI(*BB, MI, DL, TII->get(X86::W64ALLOCA))
13183 .addExternalSymbol("___chkstk")
13184 .addReg(X86::RAX, RegState::Implicit)
13185 .addReg(X86::RSP, RegState::Implicit)
13186 .addReg(X86::RAX, RegState::Define | RegState::Implicit)
13187 .addReg(X86::RSP, RegState::Define | RegState::Implicit)
13188 .addReg(X86::EFLAGS, RegState::Define | RegState::Implicit);
13189 } else {
13190 // __chkstk(MSVCRT): does not update stack pointer.
13191 // Clobbers R10, R11 and EFLAGS.
13192 // FIXME: RAX(allocated size) might be reused and not killed.
13193 BuildMI(*BB, MI, DL, TII->get(X86::W64ALLOCA))
13194 .addExternalSymbol("__chkstk")
13195 .addReg(X86::RAX, RegState::Implicit)
13196 .addReg(X86::EFLAGS, RegState::Define | RegState::Implicit);
13197 // RAX has the offset to subtracted from RSP.
13198 BuildMI(*BB, MI, DL, TII->get(X86::SUB64rr), X86::RSP)
13199 .addReg(X86::RSP)
13200 .addReg(X86::RAX);
13201 }
13202 } else {
13203 const char *StackProbeSymbol =
Michael J. Spencere9c253e2010-10-21 01:41:01 +000013204 Subtarget->isTargetWindows() ? "_chkstk" : "_alloca";
13205
NAKAMURA Takumia2e07622011-03-24 07:07:00 +000013206 BuildMI(*BB, MI, DL, TII->get(X86::CALLpcrel32))
13207 .addExternalSymbol(StackProbeSymbol)
13208 .addReg(X86::EAX, RegState::Implicit)
13209 .addReg(X86::ESP, RegState::Implicit)
13210 .addReg(X86::EAX, RegState::Define | RegState::Implicit)
13211 .addReg(X86::ESP, RegState::Define | RegState::Implicit)
13212 .addReg(X86::EFLAGS, RegState::Define | RegState::Implicit);
13213 }
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000013214
Dan Gohman14152b42010-07-06 20:24:04 +000013215 MI->eraseFromParent(); // The pseudo instruction is gone now.
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000013216 return BB;
13217}
Chris Lattner52600972009-09-02 05:57:00 +000013218
13219MachineBasicBlock *
Eric Christopher30ef0e52010-06-03 04:07:48 +000013220X86TargetLowering::EmitLoweredTLSCall(MachineInstr *MI,
13221 MachineBasicBlock *BB) const {
13222 // This is pretty easy. We're taking the value that we received from
13223 // our load from the relocation, sticking it in either RDI (x86-64)
13224 // or EAX and doing an indirect call. The return value will then
13225 // be in the normal return register.
Michael J. Spencerec38de22010-10-10 22:04:20 +000013226 const X86InstrInfo *TII
Eric Christopher54415362010-06-08 22:04:25 +000013227 = static_cast<const X86InstrInfo*>(getTargetMachine().getInstrInfo());
Eric Christopher30ef0e52010-06-03 04:07:48 +000013228 DebugLoc DL = MI->getDebugLoc();
13229 MachineFunction *F = BB->getParent();
Eric Christopher722d3152010-09-27 06:01:51 +000013230
13231 assert(Subtarget->isTargetDarwin() && "Darwin only instr emitted?");
Eric Christopher54415362010-06-08 22:04:25 +000013232 assert(MI->getOperand(3).isGlobal() && "This should be a global");
Michael J. Spencerec38de22010-10-10 22:04:20 +000013233
Jakob Stoklund Olesen8bcde2a2012-02-16 00:02:50 +000013234 // Get a register mask for the lowered call.
13235 // FIXME: The 32-bit calls have non-standard calling conventions. Use a
13236 // proper register mask.
13237 const uint32_t *RegMask =
13238 getTargetMachine().getRegisterInfo()->getCallPreservedMask(CallingConv::C);
Eric Christopher30ef0e52010-06-03 04:07:48 +000013239 if (Subtarget->is64Bit()) {
Dan Gohman14152b42010-07-06 20:24:04 +000013240 MachineInstrBuilder MIB = BuildMI(*BB, MI, DL,
13241 TII->get(X86::MOV64rm), X86::RDI)
Eric Christopher54415362010-06-08 22:04:25 +000013242 .addReg(X86::RIP)
13243 .addImm(0).addReg(0)
Michael J. Spencerec38de22010-10-10 22:04:20 +000013244 .addGlobalAddress(MI->getOperand(3).getGlobal(), 0,
Eric Christopher54415362010-06-08 22:04:25 +000013245 MI->getOperand(3).getTargetFlags())
13246 .addReg(0);
Eric Christopher722d3152010-09-27 06:01:51 +000013247 MIB = BuildMI(*BB, MI, DL, TII->get(X86::CALL64m));
Chris Lattner599b5312010-07-08 23:46:44 +000013248 addDirectMem(MIB, X86::RDI);
Jakob Stoklund Olesen8bcde2a2012-02-16 00:02:50 +000013249 MIB.addReg(X86::RAX, RegState::ImplicitDefine).addRegMask(RegMask);
Eric Christopher61025492010-06-15 23:08:42 +000013250 } else if (getTargetMachine().getRelocationModel() != Reloc::PIC_) {
Dan Gohman14152b42010-07-06 20:24:04 +000013251 MachineInstrBuilder MIB = BuildMI(*BB, MI, DL,
13252 TII->get(X86::MOV32rm), X86::EAX)
Eric Christopher61025492010-06-15 23:08:42 +000013253 .addReg(0)
13254 .addImm(0).addReg(0)
Michael J. Spencerec38de22010-10-10 22:04:20 +000013255 .addGlobalAddress(MI->getOperand(3).getGlobal(), 0,
Eric Christopher61025492010-06-15 23:08:42 +000013256 MI->getOperand(3).getTargetFlags())
13257 .addReg(0);
Dan Gohman14152b42010-07-06 20:24:04 +000013258 MIB = BuildMI(*BB, MI, DL, TII->get(X86::CALL32m));
Chris Lattner599b5312010-07-08 23:46:44 +000013259 addDirectMem(MIB, X86::EAX);
Jakob Stoklund Olesen8bcde2a2012-02-16 00:02:50 +000013260 MIB.addReg(X86::EAX, RegState::ImplicitDefine).addRegMask(RegMask);
Eric Christopher30ef0e52010-06-03 04:07:48 +000013261 } else {
Dan Gohman14152b42010-07-06 20:24:04 +000013262 MachineInstrBuilder MIB = BuildMI(*BB, MI, DL,
13263 TII->get(X86::MOV32rm), X86::EAX)
Eric Christopher54415362010-06-08 22:04:25 +000013264 .addReg(TII->getGlobalBaseReg(F))
13265 .addImm(0).addReg(0)
Michael J. Spencerec38de22010-10-10 22:04:20 +000013266 .addGlobalAddress(MI->getOperand(3).getGlobal(), 0,
Eric Christopher54415362010-06-08 22:04:25 +000013267 MI->getOperand(3).getTargetFlags())
13268 .addReg(0);
Dan Gohman14152b42010-07-06 20:24:04 +000013269 MIB = BuildMI(*BB, MI, DL, TII->get(X86::CALL32m));
Chris Lattner599b5312010-07-08 23:46:44 +000013270 addDirectMem(MIB, X86::EAX);
Jakob Stoklund Olesen8bcde2a2012-02-16 00:02:50 +000013271 MIB.addReg(X86::EAX, RegState::ImplicitDefine).addRegMask(RegMask);
Eric Christopher30ef0e52010-06-03 04:07:48 +000013272 }
Michael J. Spencerec38de22010-10-10 22:04:20 +000013273
Dan Gohman14152b42010-07-06 20:24:04 +000013274 MI->eraseFromParent(); // The pseudo instruction is gone now.
Eric Christopher30ef0e52010-06-03 04:07:48 +000013275 return BB;
13276}
13277
13278MachineBasicBlock *
Michael Liao6c0e04c2012-10-15 22:39:43 +000013279X86TargetLowering::emitEHSjLjSetJmp(MachineInstr *MI,
13280 MachineBasicBlock *MBB) const {
13281 DebugLoc DL = MI->getDebugLoc();
13282 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
13283
13284 MachineFunction *MF = MBB->getParent();
13285 MachineRegisterInfo &MRI = MF->getRegInfo();
13286
13287 const BasicBlock *BB = MBB->getBasicBlock();
13288 MachineFunction::iterator I = MBB;
13289 ++I;
13290
13291 // Memory Reference
13292 MachineInstr::mmo_iterator MMOBegin = MI->memoperands_begin();
13293 MachineInstr::mmo_iterator MMOEnd = MI->memoperands_end();
13294
13295 unsigned DstReg;
13296 unsigned MemOpndSlot = 0;
13297
13298 unsigned CurOp = 0;
13299
13300 DstReg = MI->getOperand(CurOp++).getReg();
13301 const TargetRegisterClass *RC = MRI.getRegClass(DstReg);
13302 assert(RC->hasType(MVT::i32) && "Invalid destination!");
13303 unsigned mainDstReg = MRI.createVirtualRegister(RC);
13304 unsigned restoreDstReg = MRI.createVirtualRegister(RC);
13305
13306 MemOpndSlot = CurOp;
13307
13308 MVT PVT = getPointerTy();
13309 assert((PVT == MVT::i64 || PVT == MVT::i32) &&
13310 "Invalid Pointer Size!");
13311
13312 // For v = setjmp(buf), we generate
13313 //
13314 // thisMBB:
Michael Liao281ae5a2012-10-17 02:22:27 +000013315 // buf[LabelOffset] = restoreMBB
Michael Liao6c0e04c2012-10-15 22:39:43 +000013316 // SjLjSetup restoreMBB
13317 //
13318 // mainMBB:
13319 // v_main = 0
13320 //
13321 // sinkMBB:
13322 // v = phi(main, restore)
13323 //
13324 // restoreMBB:
13325 // v_restore = 1
13326
13327 MachineBasicBlock *thisMBB = MBB;
13328 MachineBasicBlock *mainMBB = MF->CreateMachineBasicBlock(BB);
13329 MachineBasicBlock *sinkMBB = MF->CreateMachineBasicBlock(BB);
13330 MachineBasicBlock *restoreMBB = MF->CreateMachineBasicBlock(BB);
13331 MF->insert(I, mainMBB);
13332 MF->insert(I, sinkMBB);
13333 MF->push_back(restoreMBB);
13334
13335 MachineInstrBuilder MIB;
13336
13337 // Transfer the remainder of BB and its successor edges to sinkMBB.
13338 sinkMBB->splice(sinkMBB->begin(), MBB,
13339 llvm::next(MachineBasicBlock::iterator(MI)), MBB->end());
13340 sinkMBB->transferSuccessorsAndUpdatePHIs(MBB);
13341
13342 // thisMBB:
Michael Liao281ae5a2012-10-17 02:22:27 +000013343 unsigned PtrStoreOpc = 0;
13344 unsigned LabelReg = 0;
13345 const int64_t LabelOffset = 1 * PVT.getStoreSize();
13346 Reloc::Model RM = getTargetMachine().getRelocationModel();
13347 bool UseImmLabel = (getTargetMachine().getCodeModel() == CodeModel::Small) &&
13348 (RM == Reloc::Static || RM == Reloc::DynamicNoPIC);
Michael Liao6c0e04c2012-10-15 22:39:43 +000013349
Michael Liao281ae5a2012-10-17 02:22:27 +000013350 // Prepare IP either in reg or imm.
13351 if (!UseImmLabel) {
13352 PtrStoreOpc = (PVT == MVT::i64) ? X86::MOV64mr : X86::MOV32mr;
13353 const TargetRegisterClass *PtrRC = getRegClassFor(PVT);
13354 LabelReg = MRI.createVirtualRegister(PtrRC);
13355 if (Subtarget->is64Bit()) {
13356 MIB = BuildMI(*thisMBB, MI, DL, TII->get(X86::LEA64r), LabelReg)
13357 .addReg(X86::RIP)
13358 .addImm(0)
13359 .addReg(0)
13360 .addMBB(restoreMBB)
13361 .addReg(0);
13362 } else {
13363 const X86InstrInfo *XII = static_cast<const X86InstrInfo*>(TII);
13364 MIB = BuildMI(*thisMBB, MI, DL, TII->get(X86::LEA32r), LabelReg)
13365 .addReg(XII->getGlobalBaseReg(MF))
13366 .addImm(0)
13367 .addReg(0)
13368 .addMBB(restoreMBB, Subtarget->ClassifyBlockAddressReference())
13369 .addReg(0);
13370 }
13371 } else
13372 PtrStoreOpc = (PVT == MVT::i64) ? X86::MOV64mi32 : X86::MOV32mi;
Michael Liao6c0e04c2012-10-15 22:39:43 +000013373 // Store IP
Michael Liao281ae5a2012-10-17 02:22:27 +000013374 MIB = BuildMI(*thisMBB, MI, DL, TII->get(PtrStoreOpc));
Michael Liao6c0e04c2012-10-15 22:39:43 +000013375 for (unsigned i = 0; i < X86::AddrNumOperands; ++i) {
13376 if (i == X86::AddrDisp)
Michael Liao281ae5a2012-10-17 02:22:27 +000013377 MIB.addDisp(MI->getOperand(MemOpndSlot + i), LabelOffset);
Michael Liao6c0e04c2012-10-15 22:39:43 +000013378 else
13379 MIB.addOperand(MI->getOperand(MemOpndSlot + i));
13380 }
Michael Liao281ae5a2012-10-17 02:22:27 +000013381 if (!UseImmLabel)
13382 MIB.addReg(LabelReg);
13383 else
13384 MIB.addMBB(restoreMBB);
Michael Liao6c0e04c2012-10-15 22:39:43 +000013385 MIB.setMemRefs(MMOBegin, MMOEnd);
13386 // Setup
13387 MIB = BuildMI(*thisMBB, MI, DL, TII->get(X86::EH_SjLj_Setup))
13388 .addMBB(restoreMBB);
13389 MIB.addRegMask(RegInfo->getNoPreservedMask());
13390 thisMBB->addSuccessor(mainMBB);
13391 thisMBB->addSuccessor(restoreMBB);
13392
13393 // mainMBB:
13394 // EAX = 0
13395 BuildMI(mainMBB, DL, TII->get(X86::MOV32r0), mainDstReg);
13396 mainMBB->addSuccessor(sinkMBB);
13397
13398 // sinkMBB:
13399 BuildMI(*sinkMBB, sinkMBB->begin(), DL,
13400 TII->get(X86::PHI), DstReg)
13401 .addReg(mainDstReg).addMBB(mainMBB)
13402 .addReg(restoreDstReg).addMBB(restoreMBB);
13403
13404 // restoreMBB:
13405 BuildMI(restoreMBB, DL, TII->get(X86::MOV32ri), restoreDstReg).addImm(1);
13406 BuildMI(restoreMBB, DL, TII->get(X86::JMP_4)).addMBB(sinkMBB);
13407 restoreMBB->addSuccessor(sinkMBB);
13408
13409 MI->eraseFromParent();
13410 return sinkMBB;
13411}
13412
13413MachineBasicBlock *
13414X86TargetLowering::emitEHSjLjLongJmp(MachineInstr *MI,
13415 MachineBasicBlock *MBB) const {
13416 DebugLoc DL = MI->getDebugLoc();
13417 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
13418
13419 MachineFunction *MF = MBB->getParent();
13420 MachineRegisterInfo &MRI = MF->getRegInfo();
13421
13422 // Memory Reference
13423 MachineInstr::mmo_iterator MMOBegin = MI->memoperands_begin();
13424 MachineInstr::mmo_iterator MMOEnd = MI->memoperands_end();
13425
13426 MVT PVT = getPointerTy();
13427 assert((PVT == MVT::i64 || PVT == MVT::i32) &&
13428 "Invalid Pointer Size!");
13429
13430 const TargetRegisterClass *RC =
13431 (PVT == MVT::i64) ? &X86::GR64RegClass : &X86::GR32RegClass;
13432 unsigned Tmp = MRI.createVirtualRegister(RC);
13433 // Since FP is only updated here but NOT referenced, it's treated as GPR.
13434 unsigned FP = (PVT == MVT::i64) ? X86::RBP : X86::EBP;
13435 unsigned SP = RegInfo->getStackRegister();
13436
13437 MachineInstrBuilder MIB;
13438
Michael Liao281ae5a2012-10-17 02:22:27 +000013439 const int64_t LabelOffset = 1 * PVT.getStoreSize();
13440 const int64_t SPOffset = 2 * PVT.getStoreSize();
Michael Liao6c0e04c2012-10-15 22:39:43 +000013441
13442 unsigned PtrLoadOpc = (PVT == MVT::i64) ? X86::MOV64rm : X86::MOV32rm;
13443 unsigned IJmpOpc = (PVT == MVT::i64) ? X86::JMP64r : X86::JMP32r;
13444
13445 // Reload FP
13446 MIB = BuildMI(*MBB, MI, DL, TII->get(PtrLoadOpc), FP);
13447 for (unsigned i = 0; i < X86::AddrNumOperands; ++i)
13448 MIB.addOperand(MI->getOperand(i));
13449 MIB.setMemRefs(MMOBegin, MMOEnd);
13450 // Reload IP
13451 MIB = BuildMI(*MBB, MI, DL, TII->get(PtrLoadOpc), Tmp);
13452 for (unsigned i = 0; i < X86::AddrNumOperands; ++i) {
13453 if (i == X86::AddrDisp)
Michael Liao281ae5a2012-10-17 02:22:27 +000013454 MIB.addDisp(MI->getOperand(i), LabelOffset);
Michael Liao6c0e04c2012-10-15 22:39:43 +000013455 else
13456 MIB.addOperand(MI->getOperand(i));
13457 }
13458 MIB.setMemRefs(MMOBegin, MMOEnd);
13459 // Reload SP
13460 MIB = BuildMI(*MBB, MI, DL, TII->get(PtrLoadOpc), SP);
13461 for (unsigned i = 0; i < X86::AddrNumOperands; ++i) {
13462 if (i == X86::AddrDisp)
Michael Liao281ae5a2012-10-17 02:22:27 +000013463 MIB.addDisp(MI->getOperand(i), SPOffset);
Michael Liao6c0e04c2012-10-15 22:39:43 +000013464 else
13465 MIB.addOperand(MI->getOperand(i));
13466 }
13467 MIB.setMemRefs(MMOBegin, MMOEnd);
13468 // Jump
13469 BuildMI(*MBB, MI, DL, TII->get(IJmpOpc)).addReg(Tmp);
13470
13471 MI->eraseFromParent();
13472 return MBB;
13473}
13474
13475MachineBasicBlock *
Evan Chengff9b3732008-01-30 18:18:23 +000013476X86TargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000013477 MachineBasicBlock *BB) const {
Evan Cheng60c07e12006-07-05 22:17:51 +000013478 switch (MI->getOpcode()) {
Craig Topperabb94d02012-02-05 03:43:23 +000013479 default: llvm_unreachable("Unexpected instr type to insert");
NAKAMURA Takumi7754f852011-01-26 02:04:09 +000013480 case X86::TAILJMPd64:
13481 case X86::TAILJMPr64:
13482 case X86::TAILJMPm64:
Craig Topper6d1263a2012-02-05 05:38:58 +000013483 llvm_unreachable("TAILJMP64 would not be touched here.");
NAKAMURA Takumi7754f852011-01-26 02:04:09 +000013484 case X86::TCRETURNdi64:
13485 case X86::TCRETURNri64:
13486 case X86::TCRETURNmi64:
NAKAMURA Takumi7754f852011-01-26 02:04:09 +000013487 return BB;
Michael J. Spencere9c253e2010-10-21 01:41:01 +000013488 case X86::WIN_ALLOCA:
13489 return EmitLoweredWinAlloca(MI, BB);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000013490 case X86::SEG_ALLOCA_32:
13491 return EmitLoweredSegAlloca(MI, BB, false);
13492 case X86::SEG_ALLOCA_64:
13493 return EmitLoweredSegAlloca(MI, BB, true);
Eric Christopher30ef0e52010-06-03 04:07:48 +000013494 case X86::TLSCall_32:
13495 case X86::TLSCall_64:
13496 return EmitLoweredTLSCall(MI, BB);
Dan Gohmancbbea0f2009-08-27 00:14:12 +000013497 case X86::CMOV_GR8:
Evan Cheng60c07e12006-07-05 22:17:51 +000013498 case X86::CMOV_FR32:
13499 case X86::CMOV_FR64:
13500 case X86::CMOV_V4F32:
13501 case X86::CMOV_V2F64:
Chris Lattner52600972009-09-02 05:57:00 +000013502 case X86::CMOV_V2I64:
Bruno Cardoso Lopesd40aa242011-08-09 23:27:13 +000013503 case X86::CMOV_V8F32:
13504 case X86::CMOV_V4F64:
13505 case X86::CMOV_V4I64:
Chris Lattner314a1132010-03-14 18:31:44 +000013506 case X86::CMOV_GR16:
13507 case X86::CMOV_GR32:
13508 case X86::CMOV_RFP32:
13509 case X86::CMOV_RFP64:
13510 case X86::CMOV_RFP80:
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000013511 return EmitLoweredSelect(MI, BB);
Evan Cheng60c07e12006-07-05 22:17:51 +000013512
Dale Johannesen849f2142007-07-03 00:53:03 +000013513 case X86::FP32_TO_INT16_IN_MEM:
13514 case X86::FP32_TO_INT32_IN_MEM:
13515 case X86::FP32_TO_INT64_IN_MEM:
13516 case X86::FP64_TO_INT16_IN_MEM:
13517 case X86::FP64_TO_INT32_IN_MEM:
Dale Johannesena996d522007-08-07 01:17:37 +000013518 case X86::FP64_TO_INT64_IN_MEM:
13519 case X86::FP80_TO_INT16_IN_MEM:
13520 case X86::FP80_TO_INT32_IN_MEM:
13521 case X86::FP80_TO_INT64_IN_MEM: {
Chris Lattner52600972009-09-02 05:57:00 +000013522 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
13523 DebugLoc DL = MI->getDebugLoc();
13524
Evan Cheng60c07e12006-07-05 22:17:51 +000013525 // Change the floating point control register to use "round towards zero"
13526 // mode when truncating to an integer value.
13527 MachineFunction *F = BB->getParent();
David Greene3f2bf852009-11-12 20:49:22 +000013528 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2, false);
Dan Gohman14152b42010-07-06 20:24:04 +000013529 addFrameReference(BuildMI(*BB, MI, DL,
13530 TII->get(X86::FNSTCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000013531
13532 // Load the old value of the high byte of the control word...
13533 unsigned OldCW =
Craig Topperc9099502012-04-20 06:31:50 +000013534 F->getRegInfo().createVirtualRegister(&X86::GR16RegClass);
Dan Gohman14152b42010-07-06 20:24:04 +000013535 addFrameReference(BuildMI(*BB, MI, DL, TII->get(X86::MOV16rm), OldCW),
Dale Johannesene4d209d2009-02-03 20:21:25 +000013536 CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000013537
13538 // Set the high part to be round to zero...
Dan Gohman14152b42010-07-06 20:24:04 +000013539 addFrameReference(BuildMI(*BB, MI, DL, TII->get(X86::MOV16mi)), CWFrameIdx)
Evan Chengc0f64ff2006-11-27 23:37:22 +000013540 .addImm(0xC7F);
Evan Cheng60c07e12006-07-05 22:17:51 +000013541
13542 // Reload the modified control word now...
Dan Gohman14152b42010-07-06 20:24:04 +000013543 addFrameReference(BuildMI(*BB, MI, DL,
13544 TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000013545
13546 // Restore the memory image of control word to original value
Dan Gohman14152b42010-07-06 20:24:04 +000013547 addFrameReference(BuildMI(*BB, MI, DL, TII->get(X86::MOV16mr)), CWFrameIdx)
Evan Chengc0f64ff2006-11-27 23:37:22 +000013548 .addReg(OldCW);
Evan Cheng60c07e12006-07-05 22:17:51 +000013549
13550 // Get the X86 opcode to use.
13551 unsigned Opc;
13552 switch (MI->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +000013553 default: llvm_unreachable("illegal opcode!");
Dale Johannesene377d4d2007-07-04 21:07:47 +000013554 case X86::FP32_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m32; break;
13555 case X86::FP32_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m32; break;
13556 case X86::FP32_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m32; break;
13557 case X86::FP64_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m64; break;
13558 case X86::FP64_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m64; break;
13559 case X86::FP64_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m64; break;
Dale Johannesena996d522007-08-07 01:17:37 +000013560 case X86::FP80_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m80; break;
13561 case X86::FP80_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m80; break;
13562 case X86::FP80_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m80; break;
Evan Cheng60c07e12006-07-05 22:17:51 +000013563 }
13564
13565 X86AddressMode AM;
13566 MachineOperand &Op = MI->getOperand(0);
Dan Gohmand735b802008-10-03 15:45:36 +000013567 if (Op.isReg()) {
Evan Cheng60c07e12006-07-05 22:17:51 +000013568 AM.BaseType = X86AddressMode::RegBase;
13569 AM.Base.Reg = Op.getReg();
13570 } else {
13571 AM.BaseType = X86AddressMode::FrameIndexBase;
Chris Lattner8aa797a2007-12-30 23:10:15 +000013572 AM.Base.FrameIndex = Op.getIndex();
Evan Cheng60c07e12006-07-05 22:17:51 +000013573 }
13574 Op = MI->getOperand(1);
Dan Gohmand735b802008-10-03 15:45:36 +000013575 if (Op.isImm())
Chris Lattner7fbe9722006-10-20 17:42:20 +000013576 AM.Scale = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +000013577 Op = MI->getOperand(2);
Dan Gohmand735b802008-10-03 15:45:36 +000013578 if (Op.isImm())
Chris Lattner7fbe9722006-10-20 17:42:20 +000013579 AM.IndexReg = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +000013580 Op = MI->getOperand(3);
Dan Gohmand735b802008-10-03 15:45:36 +000013581 if (Op.isGlobal()) {
Evan Cheng60c07e12006-07-05 22:17:51 +000013582 AM.GV = Op.getGlobal();
13583 } else {
Chris Lattner7fbe9722006-10-20 17:42:20 +000013584 AM.Disp = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +000013585 }
Dan Gohman14152b42010-07-06 20:24:04 +000013586 addFullAddress(BuildMI(*BB, MI, DL, TII->get(Opc)), AM)
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000013587 .addReg(MI->getOperand(X86::AddrNumOperands).getReg());
Evan Cheng60c07e12006-07-05 22:17:51 +000013588
13589 // Reload the original control word now.
Dan Gohman14152b42010-07-06 20:24:04 +000013590 addFrameReference(BuildMI(*BB, MI, DL,
13591 TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000013592
Dan Gohman14152b42010-07-06 20:24:04 +000013593 MI->eraseFromParent(); // The pseudo instruction is gone now.
Evan Cheng60c07e12006-07-05 22:17:51 +000013594 return BB;
13595 }
Eric Christopherb120ab42009-08-18 22:50:32 +000013596 // String/text processing lowering.
13597 case X86::PCMPISTRM128REG:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000013598 case X86::VPCMPISTRM128REG:
Eric Christopherb120ab42009-08-18 22:50:32 +000013599 case X86::PCMPISTRM128MEM:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000013600 case X86::VPCMPISTRM128MEM:
Eric Christopherb120ab42009-08-18 22:50:32 +000013601 case X86::PCMPESTRM128REG:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000013602 case X86::VPCMPESTRM128REG:
Eric Christopherb120ab42009-08-18 22:50:32 +000013603 case X86::PCMPESTRM128MEM:
Craig Topper63a99ff2012-08-17 07:15:56 +000013604 case X86::VPCMPESTRM128MEM: {
13605 unsigned NumArgs;
13606 bool MemArg;
13607 switch (MI->getOpcode()) {
13608 default: llvm_unreachable("illegal opcode!");
13609 case X86::PCMPISTRM128REG:
13610 case X86::VPCMPISTRM128REG:
13611 NumArgs = 3; MemArg = false; break;
13612 case X86::PCMPISTRM128MEM:
13613 case X86::VPCMPISTRM128MEM:
13614 NumArgs = 3; MemArg = true; break;
13615 case X86::PCMPESTRM128REG:
13616 case X86::VPCMPESTRM128REG:
13617 NumArgs = 5; MemArg = false; break;
13618 case X86::PCMPESTRM128MEM:
13619 case X86::VPCMPESTRM128MEM:
13620 NumArgs = 5; MemArg = true; break;
13621 }
13622 return EmitPCMP(MI, BB, NumArgs, MemArg);
13623 }
Eric Christopherb120ab42009-08-18 22:50:32 +000013624
Eric Christopher228232b2010-11-30 07:20:12 +000013625 // Thread synchronization.
13626 case X86::MONITOR:
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013627 return EmitMonitor(MI, BB);
Eric Christopher228232b2010-11-30 07:20:12 +000013628
Eric Christopherb120ab42009-08-18 22:50:32 +000013629 // Atomic Lowering.
Dale Johannesen140be2d2008-08-19 18:47:28 +000013630 case X86::ATOMAND8:
Michael Liaob118a072012-09-20 03:06:15 +000013631 case X86::ATOMAND16:
13632 case X86::ATOMAND32:
Dale Johannesena99e3842008-08-20 00:48:50 +000013633 case X86::ATOMAND64:
Michael Liaob118a072012-09-20 03:06:15 +000013634 // Fall through
13635 case X86::ATOMOR8:
13636 case X86::ATOMOR16:
13637 case X86::ATOMOR32:
Dale Johannesena99e3842008-08-20 00:48:50 +000013638 case X86::ATOMOR64:
Michael Liaob118a072012-09-20 03:06:15 +000013639 // Fall through
13640 case X86::ATOMXOR16:
13641 case X86::ATOMXOR8:
13642 case X86::ATOMXOR32:
Dale Johannesena99e3842008-08-20 00:48:50 +000013643 case X86::ATOMXOR64:
Michael Liaob118a072012-09-20 03:06:15 +000013644 // Fall through
13645 case X86::ATOMNAND8:
13646 case X86::ATOMNAND16:
13647 case X86::ATOMNAND32:
13648 case X86::ATOMNAND64:
13649 // Fall through
Michael Liaofe87c302012-09-21 03:18:52 +000013650 case X86::ATOMMAX8:
Michael Liaob118a072012-09-20 03:06:15 +000013651 case X86::ATOMMAX16:
13652 case X86::ATOMMAX32:
13653 case X86::ATOMMAX64:
13654 // Fall through
Michael Liaofe87c302012-09-21 03:18:52 +000013655 case X86::ATOMMIN8:
Michael Liaob118a072012-09-20 03:06:15 +000013656 case X86::ATOMMIN16:
13657 case X86::ATOMMIN32:
13658 case X86::ATOMMIN64:
13659 // Fall through
Michael Liaofe87c302012-09-21 03:18:52 +000013660 case X86::ATOMUMAX8:
Michael Liaob118a072012-09-20 03:06:15 +000013661 case X86::ATOMUMAX16:
13662 case X86::ATOMUMAX32:
13663 case X86::ATOMUMAX64:
13664 // Fall through
Michael Liaofe87c302012-09-21 03:18:52 +000013665 case X86::ATOMUMIN8:
Michael Liaob118a072012-09-20 03:06:15 +000013666 case X86::ATOMUMIN16:
13667 case X86::ATOMUMIN32:
13668 case X86::ATOMUMIN64:
13669 return EmitAtomicLoadArith(MI, BB);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000013670
13671 // This group does 64-bit operations on a 32-bit host.
13672 case X86::ATOMAND6432:
Dale Johannesen48c1bc22008-10-02 18:53:47 +000013673 case X86::ATOMOR6432:
Dale Johannesen48c1bc22008-10-02 18:53:47 +000013674 case X86::ATOMXOR6432:
Dale Johannesen48c1bc22008-10-02 18:53:47 +000013675 case X86::ATOMNAND6432:
Dale Johannesen48c1bc22008-10-02 18:53:47 +000013676 case X86::ATOMADD6432:
Dale Johannesen48c1bc22008-10-02 18:53:47 +000013677 case X86::ATOMSUB6432:
Michael Liaoe5e8f762012-09-25 18:08:13 +000013678 case X86::ATOMMAX6432:
13679 case X86::ATOMMIN6432:
13680 case X86::ATOMUMAX6432:
13681 case X86::ATOMUMIN6432:
Michael Liaob118a072012-09-20 03:06:15 +000013682 case X86::ATOMSWAP6432:
13683 return EmitAtomicLoadArith6432(MI, BB);
Craig Topperacaaa6f2012-08-18 06:39:34 +000013684
Dan Gohmand6708ea2009-08-15 01:38:56 +000013685 case X86::VASTART_SAVE_XMM_REGS:
13686 return EmitVAStartSaveXMMRegsWithCustomInserter(MI, BB);
Dan Gohman320afb82010-10-12 18:00:49 +000013687
13688 case X86::VAARG_64:
13689 return EmitVAARG64WithCustomInserter(MI, BB);
Michael Liao6c0e04c2012-10-15 22:39:43 +000013690
13691 case X86::EH_SjLj_SetJmp32:
13692 case X86::EH_SjLj_SetJmp64:
13693 return emitEHSjLjSetJmp(MI, BB);
13694
13695 case X86::EH_SjLj_LongJmp32:
13696 case X86::EH_SjLj_LongJmp64:
13697 return emitEHSjLjLongJmp(MI, BB);
Evan Cheng60c07e12006-07-05 22:17:51 +000013698 }
13699}
13700
13701//===----------------------------------------------------------------------===//
13702// X86 Optimization Hooks
13703//===----------------------------------------------------------------------===//
13704
Dan Gohman475871a2008-07-27 21:46:04 +000013705void X86TargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
Dan Gohmanfd29e0e2008-02-13 00:35:47 +000013706 APInt &KnownZero,
13707 APInt &KnownOne,
Dan Gohmanea859be2007-06-22 14:59:07 +000013708 const SelectionDAG &DAG,
Nate Begeman368e18d2006-02-16 21:11:51 +000013709 unsigned Depth) const {
Rafael Espindola26c8dcc2012-04-04 12:51:34 +000013710 unsigned BitWidth = KnownZero.getBitWidth();
Evan Cheng3a03ebb2005-12-21 23:05:39 +000013711 unsigned Opc = Op.getOpcode();
Evan Cheng865f0602006-04-05 06:11:20 +000013712 assert((Opc >= ISD::BUILTIN_OP_END ||
13713 Opc == ISD::INTRINSIC_WO_CHAIN ||
13714 Opc == ISD::INTRINSIC_W_CHAIN ||
13715 Opc == ISD::INTRINSIC_VOID) &&
13716 "Should use MaskedValueIsZero if you don't know whether Op"
13717 " is a target node!");
Evan Cheng3a03ebb2005-12-21 23:05:39 +000013718
Rafael Espindola26c8dcc2012-04-04 12:51:34 +000013719 KnownZero = KnownOne = APInt(BitWidth, 0); // Don't know anything.
Evan Cheng3a03ebb2005-12-21 23:05:39 +000013720 switch (Opc) {
Evan Cheng865f0602006-04-05 06:11:20 +000013721 default: break;
Evan Cheng97d0e0e2009-02-02 09:15:04 +000013722 case X86ISD::ADD:
13723 case X86ISD::SUB:
Chris Lattner5b856542010-12-20 00:59:46 +000013724 case X86ISD::ADC:
13725 case X86ISD::SBB:
Evan Cheng97d0e0e2009-02-02 09:15:04 +000013726 case X86ISD::SMUL:
13727 case X86ISD::UMUL:
Dan Gohman076aee32009-03-04 19:44:21 +000013728 case X86ISD::INC:
13729 case X86ISD::DEC:
Dan Gohmane220c4b2009-09-18 19:59:53 +000013730 case X86ISD::OR:
13731 case X86ISD::XOR:
13732 case X86ISD::AND:
Evan Cheng97d0e0e2009-02-02 09:15:04 +000013733 // These nodes' second result is a boolean.
13734 if (Op.getResNo() == 0)
13735 break;
13736 // Fallthrough
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000013737 case X86ISD::SETCC:
Rafael Espindola26c8dcc2012-04-04 12:51:34 +000013738 KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - 1);
Nate Begeman368e18d2006-02-16 21:11:51 +000013739 break;
Evan Cheng7c1780c2011-10-07 17:21:44 +000013740 case ISD::INTRINSIC_WO_CHAIN: {
13741 unsigned IntId = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
13742 unsigned NumLoBits = 0;
13743 switch (IntId) {
13744 default: break;
13745 case Intrinsic::x86_sse_movmsk_ps:
13746 case Intrinsic::x86_avx_movmsk_ps_256:
13747 case Intrinsic::x86_sse2_movmsk_pd:
13748 case Intrinsic::x86_avx_movmsk_pd_256:
13749 case Intrinsic::x86_mmx_pmovmskb:
Craig Topper3738ccd2011-12-27 06:27:23 +000013750 case Intrinsic::x86_sse2_pmovmskb_128:
13751 case Intrinsic::x86_avx2_pmovmskb: {
Evan Cheng7c1780c2011-10-07 17:21:44 +000013752 // High bits of movmskp{s|d}, pmovmskb are known zero.
13753 switch (IntId) {
Craig Topperabb94d02012-02-05 03:43:23 +000013754 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
Evan Cheng7c1780c2011-10-07 17:21:44 +000013755 case Intrinsic::x86_sse_movmsk_ps: NumLoBits = 4; break;
13756 case Intrinsic::x86_avx_movmsk_ps_256: NumLoBits = 8; break;
13757 case Intrinsic::x86_sse2_movmsk_pd: NumLoBits = 2; break;
13758 case Intrinsic::x86_avx_movmsk_pd_256: NumLoBits = 4; break;
13759 case Intrinsic::x86_mmx_pmovmskb: NumLoBits = 8; break;
13760 case Intrinsic::x86_sse2_pmovmskb_128: NumLoBits = 16; break;
Craig Topper3738ccd2011-12-27 06:27:23 +000013761 case Intrinsic::x86_avx2_pmovmskb: NumLoBits = 32; break;
Evan Cheng7c1780c2011-10-07 17:21:44 +000013762 }
Rafael Espindola26c8dcc2012-04-04 12:51:34 +000013763 KnownZero = APInt::getHighBitsSet(BitWidth, BitWidth - NumLoBits);
Evan Cheng7c1780c2011-10-07 17:21:44 +000013764 break;
13765 }
13766 }
13767 break;
13768 }
Evan Cheng3a03ebb2005-12-21 23:05:39 +000013769 }
Evan Cheng3a03ebb2005-12-21 23:05:39 +000013770}
Chris Lattner259e97c2006-01-31 19:43:35 +000013771
Owen Andersonbc146b02010-09-21 20:42:50 +000013772unsigned X86TargetLowering::ComputeNumSignBitsForTargetNode(SDValue Op,
13773 unsigned Depth) const {
13774 // SETCC_CARRY sets the dest to ~0 for true or 0 for false.
13775 if (Op.getOpcode() == X86ISD::SETCC_CARRY)
13776 return Op.getValueType().getScalarType().getSizeInBits();
Michael J. Spencerec38de22010-10-10 22:04:20 +000013777
Owen Andersonbc146b02010-09-21 20:42:50 +000013778 // Fallback case.
13779 return 1;
13780}
13781
Evan Cheng206ee9d2006-07-07 08:33:52 +000013782/// isGAPlusOffset - Returns true (and the GlobalValue and the offset) if the
Evan Chengad4196b2008-05-12 19:56:52 +000013783/// node is a GlobalAddress + offset.
13784bool X86TargetLowering::isGAPlusOffset(SDNode *N,
Dan Gohman46510a72010-04-15 01:51:59 +000013785 const GlobalValue* &GA,
13786 int64_t &Offset) const {
Evan Chengad4196b2008-05-12 19:56:52 +000013787 if (N->getOpcode() == X86ISD::Wrapper) {
13788 if (isa<GlobalAddressSDNode>(N->getOperand(0))) {
Evan Cheng206ee9d2006-07-07 08:33:52 +000013789 GA = cast<GlobalAddressSDNode>(N->getOperand(0))->getGlobal();
Dan Gohman6520e202008-10-18 02:06:02 +000013790 Offset = cast<GlobalAddressSDNode>(N->getOperand(0))->getOffset();
Evan Cheng206ee9d2006-07-07 08:33:52 +000013791 return true;
13792 }
Evan Cheng206ee9d2006-07-07 08:33:52 +000013793 }
Evan Chengad4196b2008-05-12 19:56:52 +000013794 return TargetLowering::isGAPlusOffset(N, GA, Offset);
Evan Cheng206ee9d2006-07-07 08:33:52 +000013795}
13796
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000013797/// isShuffleHigh128VectorInsertLow - Checks whether the shuffle node is the
13798/// same as extracting the high 128-bit part of 256-bit vector and then
13799/// inserting the result into the low part of a new 256-bit vector
13800static bool isShuffleHigh128VectorInsertLow(ShuffleVectorSDNode *SVOp) {
13801 EVT VT = SVOp->getValueType(0);
Craig Topper66ddd152012-04-27 22:54:43 +000013802 unsigned NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000013803
13804 // vector_shuffle <4, 5, 6, 7, u, u, u, u> or <2, 3, u, u>
Craig Topper66ddd152012-04-27 22:54:43 +000013805 for (unsigned i = 0, j = NumElems/2; i != NumElems/2; ++i, ++j)
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000013806 if (!isUndefOrEqual(SVOp->getMaskElt(i), j) ||
13807 SVOp->getMaskElt(j) >= 0)
13808 return false;
13809
13810 return true;
13811}
13812
13813/// isShuffleLow128VectorInsertHigh - Checks whether the shuffle node is the
13814/// same as extracting the low 128-bit part of 256-bit vector and then
13815/// inserting the result into the high part of a new 256-bit vector
13816static bool isShuffleLow128VectorInsertHigh(ShuffleVectorSDNode *SVOp) {
13817 EVT VT = SVOp->getValueType(0);
Craig Topper66ddd152012-04-27 22:54:43 +000013818 unsigned NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000013819
13820 // vector_shuffle <u, u, u, u, 0, 1, 2, 3> or <u, u, 0, 1>
Craig Topper66ddd152012-04-27 22:54:43 +000013821 for (unsigned i = NumElems/2, j = 0; i != NumElems; ++i, ++j)
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000013822 if (!isUndefOrEqual(SVOp->getMaskElt(i), j) ||
13823 SVOp->getMaskElt(j) >= 0)
13824 return false;
13825
13826 return true;
13827}
13828
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000013829/// PerformShuffleCombine256 - Performs shuffle combines for 256-bit vectors.
13830static SDValue PerformShuffleCombine256(SDNode *N, SelectionDAG &DAG,
Craig Topper12216172012-01-13 08:12:35 +000013831 TargetLowering::DAGCombinerInfo &DCI,
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +000013832 const X86Subtarget* Subtarget) {
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000013833 DebugLoc dl = N->getDebugLoc();
13834 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
13835 SDValue V1 = SVOp->getOperand(0);
13836 SDValue V2 = SVOp->getOperand(1);
13837 EVT VT = SVOp->getValueType(0);
Craig Topper66ddd152012-04-27 22:54:43 +000013838 unsigned NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000013839
13840 if (V1.getOpcode() == ISD::CONCAT_VECTORS &&
13841 V2.getOpcode() == ISD::CONCAT_VECTORS) {
13842 //
13843 // 0,0,0,...
Benjamin Kramer558cc5a2011-07-22 01:02:57 +000013844 // |
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000013845 // V UNDEF BUILD_VECTOR UNDEF
13846 // \ / \ /
13847 // CONCAT_VECTOR CONCAT_VECTOR
13848 // \ /
13849 // \ /
13850 // RESULT: V + zero extended
13851 //
13852 if (V2.getOperand(0).getOpcode() != ISD::BUILD_VECTOR ||
13853 V2.getOperand(1).getOpcode() != ISD::UNDEF ||
13854 V1.getOperand(1).getOpcode() != ISD::UNDEF)
13855 return SDValue();
13856
13857 if (!ISD::isBuildVectorAllZeros(V2.getOperand(0).getNode()))
13858 return SDValue();
13859
13860 // To match the shuffle mask, the first half of the mask should
13861 // be exactly the first vector, and all the rest a splat with the
13862 // first element of the second one.
Craig Topper66ddd152012-04-27 22:54:43 +000013863 for (unsigned i = 0; i != NumElems/2; ++i)
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000013864 if (!isUndefOrEqual(SVOp->getMaskElt(i), i) ||
13865 !isUndefOrEqual(SVOp->getMaskElt(i+NumElems/2), NumElems))
13866 return SDValue();
13867
Chad Rosier3d1161e2012-01-03 21:05:52 +000013868 // If V1 is coming from a vector load then just fold to a VZEXT_LOAD.
13869 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(V1.getOperand(0))) {
Chad Rosier42726832012-05-07 18:47:44 +000013870 if (Ld->hasNUsesOfValue(1, 0)) {
13871 SDVTList Tys = DAG.getVTList(MVT::v4i64, MVT::Other);
13872 SDValue Ops[] = { Ld->getChain(), Ld->getBasePtr() };
13873 SDValue ResNode =
13874 DAG.getMemIntrinsicNode(X86ISD::VZEXT_LOAD, dl, Tys, Ops, 2,
13875 Ld->getMemoryVT(),
13876 Ld->getPointerInfo(),
13877 Ld->getAlignment(),
13878 false/*isVolatile*/, true/*ReadMem*/,
13879 false/*WriteMem*/);
13880 return DAG.getNode(ISD::BITCAST, dl, VT, ResNode);
13881 }
Chad Rosiera20e1e72012-08-01 18:39:17 +000013882 }
Chad Rosier3d1161e2012-01-03 21:05:52 +000013883
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000013884 // Emit a zeroed vector and insert the desired subvector on its
13885 // first half.
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +000013886 SDValue Zeros = getZeroVector(VT, Subtarget, DAG, dl);
Craig Topperb14940a2012-04-22 20:55:18 +000013887 SDValue InsV = Insert128BitVector(Zeros, V1.getOperand(0), 0, DAG, dl);
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000013888 return DCI.CombineTo(N, InsV);
13889 }
13890
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000013891 //===--------------------------------------------------------------------===//
13892 // Combine some shuffles into subvector extracts and inserts:
13893 //
13894
13895 // vector_shuffle <4, 5, 6, 7, u, u, u, u> or <2, 3, u, u>
13896 if (isShuffleHigh128VectorInsertLow(SVOp)) {
Craig Topperb14940a2012-04-22 20:55:18 +000013897 SDValue V = Extract128BitVector(V1, NumElems/2, DAG, dl);
13898 SDValue InsV = Insert128BitVector(DAG.getUNDEF(VT), V, 0, DAG, dl);
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000013899 return DCI.CombineTo(N, InsV);
13900 }
13901
13902 // vector_shuffle <u, u, u, u, 0, 1, 2, 3> or <u, u, 0, 1>
13903 if (isShuffleLow128VectorInsertHigh(SVOp)) {
Craig Topperb14940a2012-04-22 20:55:18 +000013904 SDValue V = Extract128BitVector(V1, 0, DAG, dl);
13905 SDValue InsV = Insert128BitVector(DAG.getUNDEF(VT), V, NumElems/2, DAG, dl);
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000013906 return DCI.CombineTo(N, InsV);
13907 }
13908
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000013909 return SDValue();
13910}
13911
13912/// PerformShuffleCombine - Performs several different shuffle combines.
Dan Gohman475871a2008-07-27 21:46:04 +000013913static SDValue PerformShuffleCombine(SDNode *N, SelectionDAG &DAG,
Bruno Cardoso Lopes50b37c72011-08-15 21:45:54 +000013914 TargetLowering::DAGCombinerInfo &DCI,
13915 const X86Subtarget *Subtarget) {
Dale Johannesene4d209d2009-02-03 20:21:25 +000013916 DebugLoc dl = N->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +000013917 EVT VT = N->getValueType(0);
Mon P Wang1e955802009-04-03 02:43:30 +000013918
Mon P Wanga0fd0d52010-12-19 23:55:53 +000013919 // Don't create instructions with illegal types after legalize types has run.
13920 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
13921 if (!DCI.isBeforeLegalize() && !TLI.isTypeLegal(VT.getVectorElementType()))
13922 return SDValue();
13923
Bruno Cardoso Lopes50b37c72011-08-15 21:45:54 +000013924 // Combine 256-bit vector shuffles. This is only profitable when in AVX mode
Craig Topper7a9a28b2012-08-12 02:23:29 +000013925 if (Subtarget->hasAVX() && VT.is256BitVector() &&
Bruno Cardoso Lopes50b37c72011-08-15 21:45:54 +000013926 N->getOpcode() == ISD::VECTOR_SHUFFLE)
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +000013927 return PerformShuffleCombine256(N, DAG, DCI, Subtarget);
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000013928
13929 // Only handle 128 wide vector from here on.
Craig Topper7a9a28b2012-08-12 02:23:29 +000013930 if (!VT.is128BitVector())
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000013931 return SDValue();
13932
13933 // Combine a vector_shuffle that is equal to build_vector load1, load2, load3,
13934 // load4, <0, 1, 2, 3> into a 128-bit load if the load addresses are
13935 // consecutive, non-overlapping, and in the right order.
Nate Begemanfdea31a2010-03-24 20:49:50 +000013936 SmallVector<SDValue, 16> Elts;
13937 for (unsigned i = 0, e = VT.getVectorNumElements(); i != e; ++i)
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000013938 Elts.push_back(getShuffleScalarElt(N, i, DAG, 0));
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +000013939
Nate Begemanfdea31a2010-03-24 20:49:50 +000013940 return EltsFromConsecutiveLoads(VT, Elts, dl, DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +000013941}
Evan Chengd880b972008-05-09 21:53:03 +000013942
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000013943
Craig Topper55b24052012-09-11 06:15:32 +000013944/// PerformTruncateCombine - Converts truncate operation to
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000013945/// a sequence of vector shuffle operations.
13946/// It is possible when we truncate 256-bit vector to 128-bit vector
Craig Topper55b24052012-09-11 06:15:32 +000013947static SDValue PerformTruncateCombine(SDNode *N, SelectionDAG &DAG,
13948 TargetLowering::DAGCombinerInfo &DCI,
13949 const X86Subtarget *Subtarget) {
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000013950 if (!DCI.isBeforeLegalizeOps())
13951 return SDValue();
13952
Craig Topper3ef43cf2012-04-24 06:36:35 +000013953 if (!Subtarget->hasAVX())
13954 return SDValue();
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000013955
13956 EVT VT = N->getValueType(0);
13957 SDValue Op = N->getOperand(0);
13958 EVT OpVT = Op.getValueType();
13959 DebugLoc dl = N->getDebugLoc();
13960
13961 if ((VT == MVT::v4i32) && (OpVT == MVT::v4i64)) {
13962
Elena Demikhovsky1da58672012-04-22 09:39:03 +000013963 if (Subtarget->hasAVX2()) {
13964 // AVX2: v4i64 -> v4i32
13965
13966 // VPERMD
13967 static const int ShufMask[] = {0, 2, 4, 6, -1, -1, -1, -1};
13968
13969 Op = DAG.getNode(ISD::BITCAST, dl, MVT::v8i32, Op);
13970 Op = DAG.getVectorShuffle(MVT::v8i32, dl, Op, DAG.getUNDEF(MVT::v8i32),
13971 ShufMask);
13972
Craig Topperd63fa652012-04-22 18:51:37 +000013973 return DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT, Op,
13974 DAG.getIntPtrConstant(0));
Elena Demikhovsky1da58672012-04-22 09:39:03 +000013975 }
13976
13977 // AVX: v4i64 -> v4i32
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000013978 SDValue OpLo = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v2i64, Op,
Craig Topperd63fa652012-04-22 18:51:37 +000013979 DAG.getIntPtrConstant(0));
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000013980
13981 SDValue OpHi = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v2i64, Op,
Craig Topperd63fa652012-04-22 18:51:37 +000013982 DAG.getIntPtrConstant(2));
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000013983
13984 OpLo = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, OpLo);
13985 OpHi = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, OpHi);
13986
13987 // PSHUFD
Craig Topper9e401f22012-04-21 18:58:38 +000013988 static const int ShufMask1[] = {0, 2, 0, 0};
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000013989
Craig Toppercacafd42012-08-14 08:18:43 +000013990 SDValue Undef = DAG.getUNDEF(VT);
13991 OpLo = DAG.getVectorShuffle(VT, dl, OpLo, Undef, ShufMask1);
13992 OpHi = DAG.getVectorShuffle(VT, dl, OpHi, Undef, ShufMask1);
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000013993
13994 // MOVLHPS
Craig Topper9e401f22012-04-21 18:58:38 +000013995 static const int ShufMask2[] = {0, 1, 4, 5};
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000013996
Elena Demikhovsky73252572012-02-01 10:33:05 +000013997 return DAG.getVectorShuffle(VT, dl, OpLo, OpHi, ShufMask2);
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000013998 }
Craig Topperd63fa652012-04-22 18:51:37 +000013999
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000014000 if ((VT == MVT::v8i16) && (OpVT == MVT::v8i32)) {
14001
Elena Demikhovsky1da58672012-04-22 09:39:03 +000014002 if (Subtarget->hasAVX2()) {
14003 // AVX2: v8i32 -> v8i16
14004
14005 Op = DAG.getNode(ISD::BITCAST, dl, MVT::v32i8, Op);
Craig Topperd63fa652012-04-22 18:51:37 +000014006
Elena Demikhovsky1da58672012-04-22 09:39:03 +000014007 // PSHUFB
14008 SmallVector<SDValue,32> pshufbMask;
14009 for (unsigned i = 0; i < 2; ++i) {
14010 pshufbMask.push_back(DAG.getConstant(0x0, MVT::i8));
14011 pshufbMask.push_back(DAG.getConstant(0x1, MVT::i8));
14012 pshufbMask.push_back(DAG.getConstant(0x4, MVT::i8));
14013 pshufbMask.push_back(DAG.getConstant(0x5, MVT::i8));
14014 pshufbMask.push_back(DAG.getConstant(0x8, MVT::i8));
14015 pshufbMask.push_back(DAG.getConstant(0x9, MVT::i8));
14016 pshufbMask.push_back(DAG.getConstant(0xc, MVT::i8));
14017 pshufbMask.push_back(DAG.getConstant(0xd, MVT::i8));
14018 for (unsigned j = 0; j < 8; ++j)
14019 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
14020 }
Craig Topperd63fa652012-04-22 18:51:37 +000014021 SDValue BV = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v32i8,
14022 &pshufbMask[0], 32);
Elena Demikhovsky1da58672012-04-22 09:39:03 +000014023 Op = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v32i8, Op, BV);
14024
14025 Op = DAG.getNode(ISD::BITCAST, dl, MVT::v4i64, Op);
14026
14027 static const int ShufMask[] = {0, 2, -1, -1};
Craig Topperd63fa652012-04-22 18:51:37 +000014028 Op = DAG.getVectorShuffle(MVT::v4i64, dl, Op, DAG.getUNDEF(MVT::v4i64),
Elena Demikhovsky1da58672012-04-22 09:39:03 +000014029 &ShufMask[0]);
14030
Craig Topperd63fa652012-04-22 18:51:37 +000014031 Op = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v2i64, Op,
14032 DAG.getIntPtrConstant(0));
Elena Demikhovsky1da58672012-04-22 09:39:03 +000014033
14034 return DAG.getNode(ISD::BITCAST, dl, VT, Op);
14035 }
14036
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000014037 SDValue OpLo = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i32, Op,
Craig Topperd63fa652012-04-22 18:51:37 +000014038 DAG.getIntPtrConstant(0));
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000014039
14040 SDValue OpHi = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i32, Op,
Craig Topperd63fa652012-04-22 18:51:37 +000014041 DAG.getIntPtrConstant(4));
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000014042
14043 OpLo = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, OpLo);
14044 OpHi = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, OpHi);
14045
14046 // PSHUFB
Craig Topper9e401f22012-04-21 18:58:38 +000014047 static const int ShufMask1[] = {0, 1, 4, 5, 8, 9, 12, 13,
14048 -1, -1, -1, -1, -1, -1, -1, -1};
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000014049
Craig Toppercacafd42012-08-14 08:18:43 +000014050 SDValue Undef = DAG.getUNDEF(MVT::v16i8);
14051 OpLo = DAG.getVectorShuffle(MVT::v16i8, dl, OpLo, Undef, ShufMask1);
14052 OpHi = DAG.getVectorShuffle(MVT::v16i8, dl, OpHi, Undef, ShufMask1);
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000014053
14054 OpLo = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, OpLo);
14055 OpHi = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, OpHi);
14056
14057 // MOVLHPS
Craig Topper9e401f22012-04-21 18:58:38 +000014058 static const int ShufMask2[] = {0, 1, 4, 5};
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000014059
Elena Demikhovsky73252572012-02-01 10:33:05 +000014060 SDValue res = DAG.getVectorShuffle(MVT::v4i32, dl, OpLo, OpHi, ShufMask2);
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000014061 return DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, res);
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000014062 }
14063
14064 return SDValue();
14065}
14066
Craig Topper89f4e662012-03-20 07:17:59 +000014067/// XFormVExtractWithShuffleIntoLoad - Check if a vector extract from a target
14068/// specific shuffle of a load can be folded into a single element load.
14069/// Similar handling for VECTOR_SHUFFLE is performed by DAGCombiner, but
14070/// shuffles have been customed lowered so we need to handle those here.
14071static SDValue XFormVExtractWithShuffleIntoLoad(SDNode *N, SelectionDAG &DAG,
14072 TargetLowering::DAGCombinerInfo &DCI) {
14073 if (DCI.isBeforeLegalizeOps())
14074 return SDValue();
14075
14076 SDValue InVec = N->getOperand(0);
14077 SDValue EltNo = N->getOperand(1);
14078
14079 if (!isa<ConstantSDNode>(EltNo))
14080 return SDValue();
14081
14082 EVT VT = InVec.getValueType();
14083
14084 bool HasShuffleIntoBitcast = false;
14085 if (InVec.getOpcode() == ISD::BITCAST) {
14086 // Don't duplicate a load with other uses.
14087 if (!InVec.hasOneUse())
14088 return SDValue();
14089 EVT BCVT = InVec.getOperand(0).getValueType();
14090 if (BCVT.getVectorNumElements() != VT.getVectorNumElements())
14091 return SDValue();
14092 InVec = InVec.getOperand(0);
14093 HasShuffleIntoBitcast = true;
14094 }
14095
14096 if (!isTargetShuffle(InVec.getOpcode()))
14097 return SDValue();
14098
14099 // Don't duplicate a load with other uses.
14100 if (!InVec.hasOneUse())
14101 return SDValue();
14102
14103 SmallVector<int, 16> ShuffleMask;
14104 bool UnaryShuffle;
Craig Topperd978c542012-05-06 19:46:21 +000014105 if (!getTargetShuffleMask(InVec.getNode(), VT.getSimpleVT(), ShuffleMask,
14106 UnaryShuffle))
Craig Topper89f4e662012-03-20 07:17:59 +000014107 return SDValue();
14108
14109 // Select the input vector, guarding against out of range extract vector.
14110 unsigned NumElems = VT.getVectorNumElements();
14111 int Elt = cast<ConstantSDNode>(EltNo)->getZExtValue();
14112 int Idx = (Elt > (int)NumElems) ? -1 : ShuffleMask[Elt];
14113 SDValue LdNode = (Idx < (int)NumElems) ? InVec.getOperand(0)
14114 : InVec.getOperand(1);
14115
14116 // If inputs to shuffle are the same for both ops, then allow 2 uses
14117 unsigned AllowedUses = InVec.getOperand(0) == InVec.getOperand(1) ? 2 : 1;
14118
14119 if (LdNode.getOpcode() == ISD::BITCAST) {
14120 // Don't duplicate a load with other uses.
14121 if (!LdNode.getNode()->hasNUsesOfValue(AllowedUses, 0))
14122 return SDValue();
14123
14124 AllowedUses = 1; // only allow 1 load use if we have a bitcast
14125 LdNode = LdNode.getOperand(0);
14126 }
14127
14128 if (!ISD::isNormalLoad(LdNode.getNode()))
14129 return SDValue();
14130
14131 LoadSDNode *LN0 = cast<LoadSDNode>(LdNode);
14132
14133 if (!LN0 ||!LN0->hasNUsesOfValue(AllowedUses, 0) || LN0->isVolatile())
14134 return SDValue();
14135
14136 if (HasShuffleIntoBitcast) {
14137 // If there's a bitcast before the shuffle, check if the load type and
14138 // alignment is valid.
14139 unsigned Align = LN0->getAlignment();
14140 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Micah Villmow3574eca2012-10-08 16:38:25 +000014141 unsigned NewAlign = TLI.getDataLayout()->
Craig Topper89f4e662012-03-20 07:17:59 +000014142 getABITypeAlignment(VT.getTypeForEVT(*DAG.getContext()));
14143
14144 if (NewAlign > Align || !TLI.isOperationLegalOrCustom(ISD::LOAD, VT))
14145 return SDValue();
14146 }
14147
14148 // All checks match so transform back to vector_shuffle so that DAG combiner
14149 // can finish the job
14150 DebugLoc dl = N->getDebugLoc();
14151
14152 // Create shuffle node taking into account the case that its a unary shuffle
14153 SDValue Shuffle = (UnaryShuffle) ? DAG.getUNDEF(VT) : InVec.getOperand(1);
14154 Shuffle = DAG.getVectorShuffle(InVec.getValueType(), dl,
14155 InVec.getOperand(0), Shuffle,
14156 &ShuffleMask[0]);
14157 Shuffle = DAG.getNode(ISD::BITCAST, dl, VT, Shuffle);
14158 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, N->getValueType(0), Shuffle,
14159 EltNo);
14160}
14161
Bruno Cardoso Lopesb3e06692010-09-03 19:55:05 +000014162/// PerformEXTRACT_VECTOR_ELTCombine - Detect vector gather/scatter index
14163/// generation and convert it from being a bunch of shuffles and extracts
14164/// to a simple store and scalar loads to extract the elements.
Dan Gohman1bbf72b2010-03-15 23:23:03 +000014165static SDValue PerformEXTRACT_VECTOR_ELTCombine(SDNode *N, SelectionDAG &DAG,
Craig Topper89f4e662012-03-20 07:17:59 +000014166 TargetLowering::DAGCombinerInfo &DCI) {
14167 SDValue NewOp = XFormVExtractWithShuffleIntoLoad(N, DAG, DCI);
14168 if (NewOp.getNode())
14169 return NewOp;
14170
Dan Gohman1bbf72b2010-03-15 23:23:03 +000014171 SDValue InputVector = N->getOperand(0);
14172
14173 // Only operate on vectors of 4 elements, where the alternative shuffling
14174 // gets to be more expensive.
14175 if (InputVector.getValueType() != MVT::v4i32)
14176 return SDValue();
14177
14178 // Check whether every use of InputVector is an EXTRACT_VECTOR_ELT with a
14179 // single use which is a sign-extend or zero-extend, and all elements are
14180 // used.
14181 SmallVector<SDNode *, 4> Uses;
14182 unsigned ExtractedElements = 0;
14183 for (SDNode::use_iterator UI = InputVector.getNode()->use_begin(),
14184 UE = InputVector.getNode()->use_end(); UI != UE; ++UI) {
14185 if (UI.getUse().getResNo() != InputVector.getResNo())
14186 return SDValue();
14187
14188 SDNode *Extract = *UI;
14189 if (Extract->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
14190 return SDValue();
14191
14192 if (Extract->getValueType(0) != MVT::i32)
14193 return SDValue();
14194 if (!Extract->hasOneUse())
14195 return SDValue();
14196 if (Extract->use_begin()->getOpcode() != ISD::SIGN_EXTEND &&
14197 Extract->use_begin()->getOpcode() != ISD::ZERO_EXTEND)
14198 return SDValue();
14199 if (!isa<ConstantSDNode>(Extract->getOperand(1)))
14200 return SDValue();
14201
14202 // Record which element was extracted.
14203 ExtractedElements |=
14204 1 << cast<ConstantSDNode>(Extract->getOperand(1))->getZExtValue();
14205
14206 Uses.push_back(Extract);
14207 }
14208
14209 // If not all the elements were used, this may not be worthwhile.
14210 if (ExtractedElements != 15)
14211 return SDValue();
14212
14213 // Ok, we've now decided to do the transformation.
14214 DebugLoc dl = InputVector.getDebugLoc();
14215
14216 // Store the value to a temporary stack slot.
14217 SDValue StackPtr = DAG.CreateStackTemporary(InputVector.getValueType());
Chris Lattner8026a9d2010-09-21 17:50:43 +000014218 SDValue Ch = DAG.getStore(DAG.getEntryNode(), dl, InputVector, StackPtr,
14219 MachinePointerInfo(), false, false, 0);
Dan Gohman1bbf72b2010-03-15 23:23:03 +000014220
14221 // Replace each use (extract) with a load of the appropriate element.
14222 for (SmallVectorImpl<SDNode *>::iterator UI = Uses.begin(),
14223 UE = Uses.end(); UI != UE; ++UI) {
14224 SDNode *Extract = *UI;
14225
Nadav Rotem86694292011-05-17 08:31:57 +000014226 // cOMpute the element's address.
Dan Gohman1bbf72b2010-03-15 23:23:03 +000014227 SDValue Idx = Extract->getOperand(1);
14228 unsigned EltSize =
14229 InputVector.getValueType().getVectorElementType().getSizeInBits()/8;
14230 uint64_t Offset = EltSize * cast<ConstantSDNode>(Idx)->getZExtValue();
Craig Topper89f4e662012-03-20 07:17:59 +000014231 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Dan Gohman1bbf72b2010-03-15 23:23:03 +000014232 SDValue OffsetVal = DAG.getConstant(Offset, TLI.getPointerTy());
14233
Nadav Rotem86694292011-05-17 08:31:57 +000014234 SDValue ScalarAddr = DAG.getNode(ISD::ADD, dl, TLI.getPointerTy(),
Chris Lattner51abfe42010-09-21 06:02:19 +000014235 StackPtr, OffsetVal);
Dan Gohman1bbf72b2010-03-15 23:23:03 +000014236
14237 // Load the scalar.
Eric Christopher90eb4022010-07-22 00:26:08 +000014238 SDValue LoadScalar = DAG.getLoad(Extract->getValueType(0), dl, Ch,
Chris Lattner51abfe42010-09-21 06:02:19 +000014239 ScalarAddr, MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000014240 false, false, false, 0);
Dan Gohman1bbf72b2010-03-15 23:23:03 +000014241
14242 // Replace the exact with the load.
14243 DAG.ReplaceAllUsesOfValueWith(SDValue(Extract, 0), LoadScalar);
14244 }
14245
14246 // The replacement was made in place; don't return anything.
14247 return SDValue();
14248}
14249
Duncan Sands6bcd2192011-09-17 16:49:39 +000014250/// PerformSELECTCombine - Do target-specific dag combines on SELECT and VSELECT
14251/// nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000014252static SDValue PerformSELECTCombine(SDNode *N, SelectionDAG &DAG,
Nadav Rotemcc616562012-01-15 19:27:55 +000014253 TargetLowering::DAGCombinerInfo &DCI,
Chris Lattner47b4ce82009-03-11 05:48:52 +000014254 const X86Subtarget *Subtarget) {
14255 DebugLoc DL = N->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +000014256 SDValue Cond = N->getOperand(0);
Chris Lattner47b4ce82009-03-11 05:48:52 +000014257 // Get the LHS/RHS of the select.
14258 SDValue LHS = N->getOperand(1);
14259 SDValue RHS = N->getOperand(2);
Bruno Cardoso Lopes149f29f2011-09-20 22:34:45 +000014260 EVT VT = LHS.getValueType();
Eric Christopherfd179292009-08-27 18:07:15 +000014261
Dan Gohman670e5392009-09-21 18:03:22 +000014262 // If we have SSE[12] support, try to form min/max nodes. SSE min/max
Dan Gohman8ce05da2010-02-22 04:03:39 +000014263 // instructions match the semantics of the common C idiom x<y?x:y but not
14264 // x<=y?x:y, because of how they handle negative zero (which can be
14265 // ignored in unsafe-math mode).
Benjamin Kramer2c2ccbf2011-09-22 03:27:22 +000014266 if (Cond.getOpcode() == ISD::SETCC && VT.isFloatingPoint() &&
14267 VT != MVT::f80 && DAG.getTargetLoweringInfo().isTypeLegal(VT) &&
Craig Topper1accb7e2012-01-10 06:54:16 +000014268 (Subtarget->hasSSE2() ||
14269 (Subtarget->hasSSE1() && VT.getScalarType() == MVT::f32))) {
Chris Lattner47b4ce82009-03-11 05:48:52 +000014270 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000014271
Chris Lattner47b4ce82009-03-11 05:48:52 +000014272 unsigned Opcode = 0;
Dan Gohman670e5392009-09-21 18:03:22 +000014273 // Check for x CC y ? x : y.
Dan Gohmane8326932010-02-24 06:52:40 +000014274 if (DAG.isEqualTo(LHS, Cond.getOperand(0)) &&
14275 DAG.isEqualTo(RHS, Cond.getOperand(1))) {
Chris Lattner47b4ce82009-03-11 05:48:52 +000014276 switch (CC) {
14277 default: break;
Dan Gohman670e5392009-09-21 18:03:22 +000014278 case ISD::SETULT:
Dan Gohmane8326932010-02-24 06:52:40 +000014279 // Converting this to a min would handle NaNs incorrectly, and swapping
14280 // the operands would cause it to handle comparisons between positive
14281 // and negative zero incorrectly.
Evan Cheng60108e92010-07-15 22:07:12 +000014282 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)) {
Nick Lewycky8a8d4792011-12-02 22:16:29 +000014283 if (!DAG.getTarget().Options.UnsafeFPMath &&
Dan Gohmane8326932010-02-24 06:52:40 +000014284 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
14285 break;
14286 std::swap(LHS, RHS);
14287 }
Dan Gohman670e5392009-09-21 18:03:22 +000014288 Opcode = X86ISD::FMIN;
14289 break;
14290 case ISD::SETOLE:
Dan Gohmane8326932010-02-24 06:52:40 +000014291 // Converting this to a min would handle comparisons between positive
14292 // and negative zero incorrectly.
Nick Lewycky8a8d4792011-12-02 22:16:29 +000014293 if (!DAG.getTarget().Options.UnsafeFPMath &&
Dan Gohmane8326932010-02-24 06:52:40 +000014294 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(RHS))
14295 break;
Dan Gohman670e5392009-09-21 18:03:22 +000014296 Opcode = X86ISD::FMIN;
14297 break;
Chris Lattner47b4ce82009-03-11 05:48:52 +000014298 case ISD::SETULE:
Dan Gohmane8326932010-02-24 06:52:40 +000014299 // Converting this to a min would handle both negative zeros and NaNs
14300 // incorrectly, but we can swap the operands to fix both.
14301 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000014302 case ISD::SETOLT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000014303 case ISD::SETLT:
Dan Gohman670e5392009-09-21 18:03:22 +000014304 case ISD::SETLE:
Chris Lattner47b4ce82009-03-11 05:48:52 +000014305 Opcode = X86ISD::FMIN;
14306 break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000014307
Dan Gohman670e5392009-09-21 18:03:22 +000014308 case ISD::SETOGE:
Dan Gohmane8326932010-02-24 06:52:40 +000014309 // Converting this to a max would handle comparisons between positive
14310 // and negative zero incorrectly.
Nick Lewycky8a8d4792011-12-02 22:16:29 +000014311 if (!DAG.getTarget().Options.UnsafeFPMath &&
Evan Chengdd5663c2011-08-04 18:38:15 +000014312 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000014313 break;
Dan Gohman670e5392009-09-21 18:03:22 +000014314 Opcode = X86ISD::FMAX;
14315 break;
Chris Lattner47b4ce82009-03-11 05:48:52 +000014316 case ISD::SETUGT:
Dan Gohmane8326932010-02-24 06:52:40 +000014317 // Converting this to a max would handle NaNs incorrectly, and swapping
14318 // the operands would cause it to handle comparisons between positive
14319 // and negative zero incorrectly.
Evan Cheng60108e92010-07-15 22:07:12 +000014320 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)) {
Nick Lewycky8a8d4792011-12-02 22:16:29 +000014321 if (!DAG.getTarget().Options.UnsafeFPMath &&
Dan Gohmane8326932010-02-24 06:52:40 +000014322 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
14323 break;
14324 std::swap(LHS, RHS);
14325 }
Dan Gohman670e5392009-09-21 18:03:22 +000014326 Opcode = X86ISD::FMAX;
14327 break;
14328 case ISD::SETUGE:
Dan Gohmane8326932010-02-24 06:52:40 +000014329 // Converting this to a max would handle both negative zeros and NaNs
14330 // incorrectly, but we can swap the operands to fix both.
14331 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000014332 case ISD::SETOGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000014333 case ISD::SETGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000014334 case ISD::SETGE:
14335 Opcode = X86ISD::FMAX;
14336 break;
Chris Lattner83e6c992006-10-04 06:57:07 +000014337 }
Dan Gohman670e5392009-09-21 18:03:22 +000014338 // Check for x CC y ? y : x -- a min/max with reversed arms.
Dan Gohmane8326932010-02-24 06:52:40 +000014339 } else if (DAG.isEqualTo(LHS, Cond.getOperand(1)) &&
14340 DAG.isEqualTo(RHS, Cond.getOperand(0))) {
Chris Lattner47b4ce82009-03-11 05:48:52 +000014341 switch (CC) {
14342 default: break;
Dan Gohman670e5392009-09-21 18:03:22 +000014343 case ISD::SETOGE:
Dan Gohmane8326932010-02-24 06:52:40 +000014344 // Converting this to a min would handle comparisons between positive
14345 // and negative zero incorrectly, and swapping the operands would
14346 // cause it to handle NaNs incorrectly.
Nick Lewycky8a8d4792011-12-02 22:16:29 +000014347 if (!DAG.getTarget().Options.UnsafeFPMath &&
Dan Gohmane8326932010-02-24 06:52:40 +000014348 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS))) {
Evan Cheng60108e92010-07-15 22:07:12 +000014349 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000014350 break;
14351 std::swap(LHS, RHS);
14352 }
Dan Gohman670e5392009-09-21 18:03:22 +000014353 Opcode = X86ISD::FMIN;
Dan Gohman8d44b282009-09-03 20:34:31 +000014354 break;
Dan Gohman670e5392009-09-21 18:03:22 +000014355 case ISD::SETUGT:
Dan Gohmane8326932010-02-24 06:52:40 +000014356 // Converting this to a min would handle NaNs incorrectly.
Nick Lewycky8a8d4792011-12-02 22:16:29 +000014357 if (!DAG.getTarget().Options.UnsafeFPMath &&
Dan Gohmane8326932010-02-24 06:52:40 +000014358 (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)))
14359 break;
Dan Gohman670e5392009-09-21 18:03:22 +000014360 Opcode = X86ISD::FMIN;
14361 break;
14362 case ISD::SETUGE:
Dan Gohmane8326932010-02-24 06:52:40 +000014363 // Converting this to a min would handle both negative zeros and NaNs
14364 // incorrectly, but we can swap the operands to fix both.
14365 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000014366 case ISD::SETOGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000014367 case ISD::SETGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000014368 case ISD::SETGE:
14369 Opcode = X86ISD::FMIN;
14370 break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000014371
Dan Gohman670e5392009-09-21 18:03:22 +000014372 case ISD::SETULT:
Dan Gohmane8326932010-02-24 06:52:40 +000014373 // Converting this to a max would handle NaNs incorrectly.
Evan Cheng60108e92010-07-15 22:07:12 +000014374 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000014375 break;
Dan Gohman670e5392009-09-21 18:03:22 +000014376 Opcode = X86ISD::FMAX;
Dan Gohman8d44b282009-09-03 20:34:31 +000014377 break;
Dan Gohman670e5392009-09-21 18:03:22 +000014378 case ISD::SETOLE:
Dan Gohmane8326932010-02-24 06:52:40 +000014379 // Converting this to a max would handle comparisons between positive
14380 // and negative zero incorrectly, and swapping the operands would
14381 // cause it to handle NaNs incorrectly.
Nick Lewycky8a8d4792011-12-02 22:16:29 +000014382 if (!DAG.getTarget().Options.UnsafeFPMath &&
Dan Gohmane8326932010-02-24 06:52:40 +000014383 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(RHS)) {
Evan Cheng60108e92010-07-15 22:07:12 +000014384 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000014385 break;
14386 std::swap(LHS, RHS);
14387 }
Dan Gohman670e5392009-09-21 18:03:22 +000014388 Opcode = X86ISD::FMAX;
14389 break;
14390 case ISD::SETULE:
Dan Gohmane8326932010-02-24 06:52:40 +000014391 // Converting this to a max would handle both negative zeros and NaNs
14392 // incorrectly, but we can swap the operands to fix both.
14393 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000014394 case ISD::SETOLT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000014395 case ISD::SETLT:
Dan Gohman670e5392009-09-21 18:03:22 +000014396 case ISD::SETLE:
Chris Lattner47b4ce82009-03-11 05:48:52 +000014397 Opcode = X86ISD::FMAX;
14398 break;
14399 }
Chris Lattner83e6c992006-10-04 06:57:07 +000014400 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000014401
Chris Lattner47b4ce82009-03-11 05:48:52 +000014402 if (Opcode)
14403 return DAG.getNode(Opcode, DL, N->getValueType(0), LHS, RHS);
Chris Lattner83e6c992006-10-04 06:57:07 +000014404 }
Eric Christopherfd179292009-08-27 18:07:15 +000014405
Chris Lattnerd1980a52009-03-12 06:52:53 +000014406 // If this is a select between two integer constants, try to do some
14407 // optimizations.
Chris Lattnercee56e72009-03-13 05:53:31 +000014408 if (ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(LHS)) {
14409 if (ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(RHS))
Chris Lattnerd1980a52009-03-12 06:52:53 +000014410 // Don't do this for crazy integer types.
14411 if (DAG.getTargetLoweringInfo().isTypeLegal(LHS.getValueType())) {
14412 // If this is efficiently invertible, canonicalize the LHSC/RHSC values
Chris Lattnercee56e72009-03-13 05:53:31 +000014413 // so that TrueC (the true value) is larger than FalseC.
Chris Lattnerd1980a52009-03-12 06:52:53 +000014414 bool NeedsCondInvert = false;
Eric Christopherfd179292009-08-27 18:07:15 +000014415
Chris Lattnercee56e72009-03-13 05:53:31 +000014416 if (TrueC->getAPIntValue().ult(FalseC->getAPIntValue()) &&
Chris Lattnerd1980a52009-03-12 06:52:53 +000014417 // Efficiently invertible.
14418 (Cond.getOpcode() == ISD::SETCC || // setcc -> invertible.
14419 (Cond.getOpcode() == ISD::XOR && // xor(X, C) -> invertible.
14420 isa<ConstantSDNode>(Cond.getOperand(1))))) {
14421 NeedsCondInvert = true;
Chris Lattnercee56e72009-03-13 05:53:31 +000014422 std::swap(TrueC, FalseC);
Chris Lattnerd1980a52009-03-12 06:52:53 +000014423 }
Eric Christopherfd179292009-08-27 18:07:15 +000014424
Chris Lattnerd1980a52009-03-12 06:52:53 +000014425 // Optimize C ? 8 : 0 -> zext(C) << 3. Likewise for any pow2/0.
Chris Lattnercee56e72009-03-13 05:53:31 +000014426 if (FalseC->getAPIntValue() == 0 &&
14427 TrueC->getAPIntValue().isPowerOf2()) {
Chris Lattnerd1980a52009-03-12 06:52:53 +000014428 if (NeedsCondInvert) // Invert the condition if needed.
14429 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
14430 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000014431
Chris Lattnerd1980a52009-03-12 06:52:53 +000014432 // Zero extend the condition if needed.
14433 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, LHS.getValueType(), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000014434
Chris Lattnercee56e72009-03-13 05:53:31 +000014435 unsigned ShAmt = TrueC->getAPIntValue().logBase2();
Chris Lattnerd1980a52009-03-12 06:52:53 +000014436 return DAG.getNode(ISD::SHL, DL, LHS.getValueType(), Cond,
Owen Anderson825b72b2009-08-11 20:47:22 +000014437 DAG.getConstant(ShAmt, MVT::i8));
Chris Lattnerd1980a52009-03-12 06:52:53 +000014438 }
Eric Christopherfd179292009-08-27 18:07:15 +000014439
Chris Lattner97a29a52009-03-13 05:22:11 +000014440 // Optimize Cond ? cst+1 : cst -> zext(setcc(C)+cst.
Chris Lattnercee56e72009-03-13 05:53:31 +000014441 if (FalseC->getAPIntValue()+1 == TrueC->getAPIntValue()) {
Chris Lattner97a29a52009-03-13 05:22:11 +000014442 if (NeedsCondInvert) // Invert the condition if needed.
14443 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
14444 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000014445
Chris Lattner97a29a52009-03-13 05:22:11 +000014446 // Zero extend the condition if needed.
Chris Lattnercee56e72009-03-13 05:53:31 +000014447 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL,
14448 FalseC->getValueType(0), Cond);
Chris Lattner97a29a52009-03-13 05:22:11 +000014449 return DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
Chris Lattnercee56e72009-03-13 05:53:31 +000014450 SDValue(FalseC, 0));
Chris Lattner97a29a52009-03-13 05:22:11 +000014451 }
Eric Christopherfd179292009-08-27 18:07:15 +000014452
Chris Lattnercee56e72009-03-13 05:53:31 +000014453 // Optimize cases that will turn into an LEA instruction. This requires
14454 // an i32 or i64 and an efficient multiplier (1, 2, 3, 4, 5, 8, 9).
Owen Anderson825b72b2009-08-11 20:47:22 +000014455 if (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i64) {
Chris Lattnercee56e72009-03-13 05:53:31 +000014456 uint64_t Diff = TrueC->getZExtValue()-FalseC->getZExtValue();
Owen Anderson825b72b2009-08-11 20:47:22 +000014457 if (N->getValueType(0) == MVT::i32) Diff = (unsigned)Diff;
Eric Christopherfd179292009-08-27 18:07:15 +000014458
Chris Lattnercee56e72009-03-13 05:53:31 +000014459 bool isFastMultiplier = false;
14460 if (Diff < 10) {
14461 switch ((unsigned char)Diff) {
14462 default: break;
14463 case 1: // result = add base, cond
14464 case 2: // result = lea base( , cond*2)
14465 case 3: // result = lea base(cond, cond*2)
14466 case 4: // result = lea base( , cond*4)
14467 case 5: // result = lea base(cond, cond*4)
14468 case 8: // result = lea base( , cond*8)
14469 case 9: // result = lea base(cond, cond*8)
14470 isFastMultiplier = true;
14471 break;
14472 }
14473 }
Eric Christopherfd179292009-08-27 18:07:15 +000014474
Chris Lattnercee56e72009-03-13 05:53:31 +000014475 if (isFastMultiplier) {
14476 APInt Diff = TrueC->getAPIntValue()-FalseC->getAPIntValue();
14477 if (NeedsCondInvert) // Invert the condition if needed.
14478 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
14479 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000014480
Chris Lattnercee56e72009-03-13 05:53:31 +000014481 // Zero extend the condition if needed.
14482 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, FalseC->getValueType(0),
14483 Cond);
14484 // Scale the condition by the difference.
14485 if (Diff != 1)
14486 Cond = DAG.getNode(ISD::MUL, DL, Cond.getValueType(), Cond,
14487 DAG.getConstant(Diff, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000014488
Chris Lattnercee56e72009-03-13 05:53:31 +000014489 // Add the base if non-zero.
14490 if (FalseC->getAPIntValue() != 0)
14491 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
14492 SDValue(FalseC, 0));
14493 return Cond;
14494 }
Eric Christopherfd179292009-08-27 18:07:15 +000014495 }
Chris Lattnerd1980a52009-03-12 06:52:53 +000014496 }
14497 }
Eric Christopherfd179292009-08-27 18:07:15 +000014498
Evan Cheng56f582d2012-01-04 01:41:39 +000014499 // Canonicalize max and min:
14500 // (x > y) ? x : y -> (x >= y) ? x : y
14501 // (x < y) ? x : y -> (x <= y) ? x : y
14502 // This allows use of COND_S / COND_NS (see TranslateX86CC) which eliminates
14503 // the need for an extra compare
14504 // against zero. e.g.
14505 // (x - y) > 0 : (x - y) ? 0 -> (x - y) >= 0 : (x - y) ? 0
14506 // subl %esi, %edi
14507 // testl %edi, %edi
14508 // movl $0, %eax
14509 // cmovgl %edi, %eax
14510 // =>
14511 // xorl %eax, %eax
14512 // subl %esi, $edi
14513 // cmovsl %eax, %edi
14514 if (N->getOpcode() == ISD::SELECT && Cond.getOpcode() == ISD::SETCC &&
14515 DAG.isEqualTo(LHS, Cond.getOperand(0)) &&
14516 DAG.isEqualTo(RHS, Cond.getOperand(1))) {
14517 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
14518 switch (CC) {
14519 default: break;
14520 case ISD::SETLT:
14521 case ISD::SETGT: {
14522 ISD::CondCode NewCC = (CC == ISD::SETLT) ? ISD::SETLE : ISD::SETGE;
14523 Cond = DAG.getSetCC(Cond.getDebugLoc(), Cond.getValueType(),
14524 Cond.getOperand(0), Cond.getOperand(1), NewCC);
14525 return DAG.getNode(ISD::SELECT, DL, VT, Cond, LHS, RHS);
14526 }
14527 }
14528 }
14529
Nadav Rotemcc616562012-01-15 19:27:55 +000014530 // If we know that this node is legal then we know that it is going to be
14531 // matched by one of the SSE/AVX BLEND instructions. These instructions only
14532 // depend on the highest bit in each word. Try to use SimplifyDemandedBits
14533 // to simplify previous instructions.
14534 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
14535 if (N->getOpcode() == ISD::VSELECT && DCI.isBeforeLegalizeOps() &&
Nadav Rotembdcae382012-06-07 20:53:48 +000014536 !DCI.isBeforeLegalize() && TLI.isOperationLegal(ISD::VSELECT, VT)) {
Nadav Rotemcc616562012-01-15 19:27:55 +000014537 unsigned BitWidth = Cond.getValueType().getScalarType().getSizeInBits();
Nadav Rotembdcae382012-06-07 20:53:48 +000014538
14539 // Don't optimize vector selects that map to mask-registers.
14540 if (BitWidth == 1)
14541 return SDValue();
14542
Nadav Rotemcc616562012-01-15 19:27:55 +000014543 assert(BitWidth >= 8 && BitWidth <= 64 && "Invalid mask size");
14544 APInt DemandedMask = APInt::getHighBitsSet(BitWidth, 1);
14545
14546 APInt KnownZero, KnownOne;
14547 TargetLowering::TargetLoweringOpt TLO(DAG, DCI.isBeforeLegalize(),
14548 DCI.isBeforeLegalizeOps());
14549 if (TLO.ShrinkDemandedConstant(Cond, DemandedMask) ||
14550 TLI.SimplifyDemandedBits(Cond, DemandedMask, KnownZero, KnownOne, TLO))
14551 DCI.CommitTargetLoweringOpt(TLO);
14552 }
14553
Dan Gohman475871a2008-07-27 21:46:04 +000014554 return SDValue();
Chris Lattner83e6c992006-10-04 06:57:07 +000014555}
14556
Michael Liao2a33cec2012-08-10 19:58:13 +000014557// Check whether a boolean test is testing a boolean value generated by
14558// X86ISD::SETCC. If so, return the operand of that SETCC and proper condition
14559// code.
14560//
14561// Simplify the following patterns:
14562// (Op (CMP (SETCC Cond EFLAGS) 1) EQ) or
14563// (Op (CMP (SETCC Cond EFLAGS) 0) NEQ)
14564// to (Op EFLAGS Cond)
14565//
14566// (Op (CMP (SETCC Cond EFLAGS) 0) EQ) or
14567// (Op (CMP (SETCC Cond EFLAGS) 1) NEQ)
14568// to (Op EFLAGS !Cond)
14569//
14570// where Op could be BRCOND or CMOV.
14571//
Michael Liaodbf8b5b2012-08-28 03:34:40 +000014572static SDValue checkBoolTestSetCCCombine(SDValue Cmp, X86::CondCode &CC) {
Michael Liao2a33cec2012-08-10 19:58:13 +000014573 // Quit if not CMP and SUB with its value result used.
14574 if (Cmp.getOpcode() != X86ISD::CMP &&
14575 (Cmp.getOpcode() != X86ISD::SUB || Cmp.getNode()->hasAnyUseOfValue(0)))
14576 return SDValue();
14577
14578 // Quit if not used as a boolean value.
14579 if (CC != X86::COND_E && CC != X86::COND_NE)
14580 return SDValue();
14581
14582 // Check CMP operands. One of them should be 0 or 1 and the other should be
14583 // an SetCC or extended from it.
14584 SDValue Op1 = Cmp.getOperand(0);
14585 SDValue Op2 = Cmp.getOperand(1);
14586
14587 SDValue SetCC;
14588 const ConstantSDNode* C = 0;
14589 bool needOppositeCond = (CC == X86::COND_E);
14590
14591 if ((C = dyn_cast<ConstantSDNode>(Op1)))
14592 SetCC = Op2;
14593 else if ((C = dyn_cast<ConstantSDNode>(Op2)))
14594 SetCC = Op1;
14595 else // Quit if all operands are not constants.
14596 return SDValue();
14597
14598 if (C->getZExtValue() == 1)
14599 needOppositeCond = !needOppositeCond;
14600 else if (C->getZExtValue() != 0)
14601 // Quit if the constant is neither 0 or 1.
14602 return SDValue();
14603
14604 // Skip 'zext' node.
14605 if (SetCC.getOpcode() == ISD::ZERO_EXTEND)
14606 SetCC = SetCC.getOperand(0);
14607
Michael Liao7fdc66b2012-09-10 16:36:16 +000014608 switch (SetCC.getOpcode()) {
14609 case X86ISD::SETCC:
14610 // Set the condition code or opposite one if necessary.
14611 CC = X86::CondCode(SetCC.getConstantOperandVal(0));
14612 if (needOppositeCond)
14613 CC = X86::GetOppositeBranchCondition(CC);
14614 return SetCC.getOperand(1);
14615 case X86ISD::CMOV: {
14616 // Check whether false/true value has canonical one, i.e. 0 or 1.
14617 ConstantSDNode *FVal = dyn_cast<ConstantSDNode>(SetCC.getOperand(0));
14618 ConstantSDNode *TVal = dyn_cast<ConstantSDNode>(SetCC.getOperand(1));
14619 // Quit if true value is not a constant.
14620 if (!TVal)
14621 return SDValue();
14622 // Quit if false value is not a constant.
14623 if (!FVal) {
14624 // A special case for rdrand, where 0 is set if false cond is found.
14625 SDValue Op = SetCC.getOperand(0);
14626 if (Op.getOpcode() != X86ISD::RDRAND)
14627 return SDValue();
14628 }
14629 // Quit if false value is not the constant 0 or 1.
14630 bool FValIsFalse = true;
14631 if (FVal && FVal->getZExtValue() != 0) {
14632 if (FVal->getZExtValue() != 1)
14633 return SDValue();
14634 // If FVal is 1, opposite cond is needed.
14635 needOppositeCond = !needOppositeCond;
14636 FValIsFalse = false;
14637 }
14638 // Quit if TVal is not the constant opposite of FVal.
14639 if (FValIsFalse && TVal->getZExtValue() != 1)
14640 return SDValue();
14641 if (!FValIsFalse && TVal->getZExtValue() != 0)
14642 return SDValue();
14643 CC = X86::CondCode(SetCC.getConstantOperandVal(2));
14644 if (needOppositeCond)
14645 CC = X86::GetOppositeBranchCondition(CC);
14646 return SetCC.getOperand(3);
14647 }
14648 }
Michael Liao2a33cec2012-08-10 19:58:13 +000014649
Michael Liao7fdc66b2012-09-10 16:36:16 +000014650 return SDValue();
Michael Liao2a33cec2012-08-10 19:58:13 +000014651}
14652
Chris Lattnerd1980a52009-03-12 06:52:53 +000014653/// Optimize X86ISD::CMOV [LHS, RHS, CONDCODE (e.g. X86::COND_NE), CONDVAL]
14654static SDValue PerformCMOVCombine(SDNode *N, SelectionDAG &DAG,
Michael Liaodbf8b5b2012-08-28 03:34:40 +000014655 TargetLowering::DAGCombinerInfo &DCI,
14656 const X86Subtarget *Subtarget) {
Chris Lattnerd1980a52009-03-12 06:52:53 +000014657 DebugLoc DL = N->getDebugLoc();
Eric Christopherfd179292009-08-27 18:07:15 +000014658
Chris Lattnerd1980a52009-03-12 06:52:53 +000014659 // If the flag operand isn't dead, don't touch this CMOV.
14660 if (N->getNumValues() == 2 && !SDValue(N, 1).use_empty())
14661 return SDValue();
Eric Christopherfd179292009-08-27 18:07:15 +000014662
Evan Chengb5a55d92011-05-24 01:48:22 +000014663 SDValue FalseOp = N->getOperand(0);
14664 SDValue TrueOp = N->getOperand(1);
14665 X86::CondCode CC = (X86::CondCode)N->getConstantOperandVal(2);
14666 SDValue Cond = N->getOperand(3);
Michael Liao2a33cec2012-08-10 19:58:13 +000014667
Evan Chengb5a55d92011-05-24 01:48:22 +000014668 if (CC == X86::COND_E || CC == X86::COND_NE) {
14669 switch (Cond.getOpcode()) {
14670 default: break;
14671 case X86ISD::BSR:
14672 case X86ISD::BSF:
14673 // If operand of BSR / BSF are proven never zero, then ZF cannot be set.
14674 if (DAG.isKnownNeverZero(Cond.getOperand(0)))
14675 return (CC == X86::COND_E) ? FalseOp : TrueOp;
14676 }
14677 }
14678
Michael Liao2a33cec2012-08-10 19:58:13 +000014679 SDValue Flags;
14680
Michael Liaodbf8b5b2012-08-28 03:34:40 +000014681 Flags = checkBoolTestSetCCCombine(Cond, CC);
Michael Liao9eac20a2012-08-11 23:47:06 +000014682 if (Flags.getNode() &&
14683 // Extra check as FCMOV only supports a subset of X86 cond.
Michael Liao7859f432012-09-06 07:11:22 +000014684 (FalseOp.getValueType() != MVT::f80 || hasFPCMov(CC))) {
Michael Liaodbf8b5b2012-08-28 03:34:40 +000014685 SDValue Ops[] = { FalseOp, TrueOp,
14686 DAG.getConstant(CC, MVT::i8), Flags };
14687 return DAG.getNode(X86ISD::CMOV, DL, N->getVTList(),
14688 Ops, array_lengthof(Ops));
14689 }
14690
Chris Lattnerd1980a52009-03-12 06:52:53 +000014691 // If this is a select between two integer constants, try to do some
14692 // optimizations. Note that the operands are ordered the opposite of SELECT
14693 // operands.
Evan Chengb5a55d92011-05-24 01:48:22 +000014694 if (ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(TrueOp)) {
14695 if (ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(FalseOp)) {
Chris Lattnerd1980a52009-03-12 06:52:53 +000014696 // Canonicalize the TrueC/FalseC values so that TrueC (the true value) is
14697 // larger than FalseC (the false value).
Chris Lattnerd1980a52009-03-12 06:52:53 +000014698 if (TrueC->getAPIntValue().ult(FalseC->getAPIntValue())) {
14699 CC = X86::GetOppositeBranchCondition(CC);
14700 std::swap(TrueC, FalseC);
NAKAMURA Takumie2687452012-10-16 06:28:34 +000014701 std::swap(TrueOp, FalseOp);
Chris Lattnerd1980a52009-03-12 06:52:53 +000014702 }
Eric Christopherfd179292009-08-27 18:07:15 +000014703
Chris Lattnerd1980a52009-03-12 06:52:53 +000014704 // Optimize C ? 8 : 0 -> zext(setcc(C)) << 3. Likewise for any pow2/0.
Chris Lattnercee56e72009-03-13 05:53:31 +000014705 // This is efficient for any integer data type (including i8/i16) and
14706 // shift amount.
Chris Lattnerd1980a52009-03-12 06:52:53 +000014707 if (FalseC->getAPIntValue() == 0 && TrueC->getAPIntValue().isPowerOf2()) {
Owen Anderson825b72b2009-08-11 20:47:22 +000014708 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
14709 DAG.getConstant(CC, MVT::i8), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000014710
Chris Lattnerd1980a52009-03-12 06:52:53 +000014711 // Zero extend the condition if needed.
14712 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, TrueC->getValueType(0), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000014713
Chris Lattnerd1980a52009-03-12 06:52:53 +000014714 unsigned ShAmt = TrueC->getAPIntValue().logBase2();
14715 Cond = DAG.getNode(ISD::SHL, DL, Cond.getValueType(), Cond,
Owen Anderson825b72b2009-08-11 20:47:22 +000014716 DAG.getConstant(ShAmt, MVT::i8));
Chris Lattnerd1980a52009-03-12 06:52:53 +000014717 if (N->getNumValues() == 2) // Dead flag value?
14718 return DCI.CombineTo(N, Cond, SDValue());
14719 return Cond;
14720 }
Eric Christopherfd179292009-08-27 18:07:15 +000014721
Chris Lattnercee56e72009-03-13 05:53:31 +000014722 // Optimize Cond ? cst+1 : cst -> zext(setcc(C)+cst. This is efficient
14723 // for any integer data type, including i8/i16.
Chris Lattner97a29a52009-03-13 05:22:11 +000014724 if (FalseC->getAPIntValue()+1 == TrueC->getAPIntValue()) {
Owen Anderson825b72b2009-08-11 20:47:22 +000014725 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
14726 DAG.getConstant(CC, MVT::i8), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000014727
Chris Lattner97a29a52009-03-13 05:22:11 +000014728 // Zero extend the condition if needed.
Chris Lattnercee56e72009-03-13 05:53:31 +000014729 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL,
14730 FalseC->getValueType(0), Cond);
Chris Lattner97a29a52009-03-13 05:22:11 +000014731 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
14732 SDValue(FalseC, 0));
Eric Christopherfd179292009-08-27 18:07:15 +000014733
Chris Lattner97a29a52009-03-13 05:22:11 +000014734 if (N->getNumValues() == 2) // Dead flag value?
14735 return DCI.CombineTo(N, Cond, SDValue());
14736 return Cond;
14737 }
Eric Christopherfd179292009-08-27 18:07:15 +000014738
Chris Lattnercee56e72009-03-13 05:53:31 +000014739 // Optimize cases that will turn into an LEA instruction. This requires
14740 // an i32 or i64 and an efficient multiplier (1, 2, 3, 4, 5, 8, 9).
Owen Anderson825b72b2009-08-11 20:47:22 +000014741 if (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i64) {
Chris Lattnercee56e72009-03-13 05:53:31 +000014742 uint64_t Diff = TrueC->getZExtValue()-FalseC->getZExtValue();
Owen Anderson825b72b2009-08-11 20:47:22 +000014743 if (N->getValueType(0) == MVT::i32) Diff = (unsigned)Diff;
Eric Christopherfd179292009-08-27 18:07:15 +000014744
Chris Lattnercee56e72009-03-13 05:53:31 +000014745 bool isFastMultiplier = false;
14746 if (Diff < 10) {
14747 switch ((unsigned char)Diff) {
14748 default: break;
14749 case 1: // result = add base, cond
14750 case 2: // result = lea base( , cond*2)
14751 case 3: // result = lea base(cond, cond*2)
14752 case 4: // result = lea base( , cond*4)
14753 case 5: // result = lea base(cond, cond*4)
14754 case 8: // result = lea base( , cond*8)
14755 case 9: // result = lea base(cond, cond*8)
14756 isFastMultiplier = true;
14757 break;
14758 }
14759 }
Eric Christopherfd179292009-08-27 18:07:15 +000014760
Chris Lattnercee56e72009-03-13 05:53:31 +000014761 if (isFastMultiplier) {
14762 APInt Diff = TrueC->getAPIntValue()-FalseC->getAPIntValue();
Owen Anderson825b72b2009-08-11 20:47:22 +000014763 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
14764 DAG.getConstant(CC, MVT::i8), Cond);
Chris Lattnercee56e72009-03-13 05:53:31 +000014765 // Zero extend the condition if needed.
14766 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, FalseC->getValueType(0),
14767 Cond);
14768 // Scale the condition by the difference.
14769 if (Diff != 1)
14770 Cond = DAG.getNode(ISD::MUL, DL, Cond.getValueType(), Cond,
14771 DAG.getConstant(Diff, Cond.getValueType()));
14772
14773 // Add the base if non-zero.
14774 if (FalseC->getAPIntValue() != 0)
14775 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
14776 SDValue(FalseC, 0));
14777 if (N->getNumValues() == 2) // Dead flag value?
14778 return DCI.CombineTo(N, Cond, SDValue());
14779 return Cond;
14780 }
Eric Christopherfd179292009-08-27 18:07:15 +000014781 }
Chris Lattnerd1980a52009-03-12 06:52:53 +000014782 }
14783 }
NAKAMURA Takumie2687452012-10-16 06:28:34 +000014784
14785 // Handle these cases:
14786 // (select (x != c), e, c) -> select (x != c), e, x),
14787 // (select (x == c), c, e) -> select (x == c), x, e)
14788 // where the c is an integer constant, and the "select" is the combination
14789 // of CMOV and CMP.
14790 //
14791 // The rationale for this change is that the conditional-move from a constant
14792 // needs two instructions, however, conditional-move from a register needs
14793 // only one instruction.
14794 //
14795 // CAVEAT: By replacing a constant with a symbolic value, it may obscure
14796 // some instruction-combining opportunities. This opt needs to be
14797 // postponed as late as possible.
14798 //
14799 if (!DCI.isBeforeLegalize() && !DCI.isBeforeLegalizeOps()) {
14800 // the DCI.xxxx conditions are provided to postpone the optimization as
14801 // late as possible.
14802
14803 ConstantSDNode *CmpAgainst = 0;
14804 if ((Cond.getOpcode() == X86ISD::CMP || Cond.getOpcode() == X86ISD::SUB) &&
14805 (CmpAgainst = dyn_cast<ConstantSDNode>(Cond.getOperand(1))) &&
14806 dyn_cast<ConstantSDNode>(Cond.getOperand(0)) == 0) {
14807
14808 if (CC == X86::COND_NE &&
14809 CmpAgainst == dyn_cast<ConstantSDNode>(FalseOp)) {
14810 CC = X86::GetOppositeBranchCondition(CC);
14811 std::swap(TrueOp, FalseOp);
14812 }
14813
14814 if (CC == X86::COND_E &&
14815 CmpAgainst == dyn_cast<ConstantSDNode>(TrueOp)) {
14816 SDValue Ops[] = { FalseOp, Cond.getOperand(0),
14817 DAG.getConstant(CC, MVT::i8), Cond };
14818 return DAG.getNode(X86ISD::CMOV, DL, N->getVTList (), Ops,
14819 array_lengthof(Ops));
14820 }
14821 }
14822 }
14823
Chris Lattnerd1980a52009-03-12 06:52:53 +000014824 return SDValue();
14825}
14826
14827
Evan Cheng0b0cd912009-03-28 05:57:29 +000014828/// PerformMulCombine - Optimize a single multiply with constant into two
14829/// in order to implement it with two cheaper instructions, e.g.
14830/// LEA + SHL, LEA + LEA.
14831static SDValue PerformMulCombine(SDNode *N, SelectionDAG &DAG,
14832 TargetLowering::DAGCombinerInfo &DCI) {
Evan Cheng0b0cd912009-03-28 05:57:29 +000014833 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
14834 return SDValue();
14835
Owen Andersone50ed302009-08-10 22:56:29 +000014836 EVT VT = N->getValueType(0);
Owen Anderson825b72b2009-08-11 20:47:22 +000014837 if (VT != MVT::i64)
Evan Cheng0b0cd912009-03-28 05:57:29 +000014838 return SDValue();
14839
14840 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1));
14841 if (!C)
14842 return SDValue();
14843 uint64_t MulAmt = C->getZExtValue();
14844 if (isPowerOf2_64(MulAmt) || MulAmt == 3 || MulAmt == 5 || MulAmt == 9)
14845 return SDValue();
14846
14847 uint64_t MulAmt1 = 0;
14848 uint64_t MulAmt2 = 0;
14849 if ((MulAmt % 9) == 0) {
14850 MulAmt1 = 9;
14851 MulAmt2 = MulAmt / 9;
14852 } else if ((MulAmt % 5) == 0) {
14853 MulAmt1 = 5;
14854 MulAmt2 = MulAmt / 5;
14855 } else if ((MulAmt % 3) == 0) {
14856 MulAmt1 = 3;
14857 MulAmt2 = MulAmt / 3;
14858 }
14859 if (MulAmt2 &&
14860 (isPowerOf2_64(MulAmt2) || MulAmt2 == 3 || MulAmt2 == 5 || MulAmt2 == 9)){
14861 DebugLoc DL = N->getDebugLoc();
14862
14863 if (isPowerOf2_64(MulAmt2) &&
14864 !(N->hasOneUse() && N->use_begin()->getOpcode() == ISD::ADD))
14865 // If second multiplifer is pow2, issue it first. We want the multiply by
14866 // 3, 5, or 9 to be folded into the addressing mode unless the lone use
14867 // is an add.
14868 std::swap(MulAmt1, MulAmt2);
14869
14870 SDValue NewMul;
Eric Christopherfd179292009-08-27 18:07:15 +000014871 if (isPowerOf2_64(MulAmt1))
Evan Cheng0b0cd912009-03-28 05:57:29 +000014872 NewMul = DAG.getNode(ISD::SHL, DL, VT, N->getOperand(0),
Owen Anderson825b72b2009-08-11 20:47:22 +000014873 DAG.getConstant(Log2_64(MulAmt1), MVT::i8));
Evan Cheng0b0cd912009-03-28 05:57:29 +000014874 else
Evan Cheng73f24c92009-03-30 21:36:47 +000014875 NewMul = DAG.getNode(X86ISD::MUL_IMM, DL, VT, N->getOperand(0),
Evan Cheng0b0cd912009-03-28 05:57:29 +000014876 DAG.getConstant(MulAmt1, VT));
14877
Eric Christopherfd179292009-08-27 18:07:15 +000014878 if (isPowerOf2_64(MulAmt2))
Evan Cheng0b0cd912009-03-28 05:57:29 +000014879 NewMul = DAG.getNode(ISD::SHL, DL, VT, NewMul,
Owen Anderson825b72b2009-08-11 20:47:22 +000014880 DAG.getConstant(Log2_64(MulAmt2), MVT::i8));
Eric Christopherfd179292009-08-27 18:07:15 +000014881 else
Evan Cheng73f24c92009-03-30 21:36:47 +000014882 NewMul = DAG.getNode(X86ISD::MUL_IMM, DL, VT, NewMul,
Evan Cheng0b0cd912009-03-28 05:57:29 +000014883 DAG.getConstant(MulAmt2, VT));
14884
14885 // Do not add new nodes to DAG combiner worklist.
14886 DCI.CombineTo(N, NewMul, false);
14887 }
14888 return SDValue();
14889}
14890
Evan Chengad9c0a32009-12-15 00:53:42 +000014891static SDValue PerformSHLCombine(SDNode *N, SelectionDAG &DAG) {
14892 SDValue N0 = N->getOperand(0);
14893 SDValue N1 = N->getOperand(1);
14894 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
14895 EVT VT = N0.getValueType();
14896
14897 // fold (shl (and (setcc_c), c1), c2) -> (and setcc_c, (c1 << c2))
14898 // since the result of setcc_c is all zero's or all ones.
Nadav Rotemfb0dfbb2011-10-30 13:24:22 +000014899 if (VT.isInteger() && !VT.isVector() &&
14900 N1C && N0.getOpcode() == ISD::AND &&
Evan Chengad9c0a32009-12-15 00:53:42 +000014901 N0.getOperand(1).getOpcode() == ISD::Constant) {
14902 SDValue N00 = N0.getOperand(0);
14903 if (N00.getOpcode() == X86ISD::SETCC_CARRY ||
14904 ((N00.getOpcode() == ISD::ANY_EXTEND ||
14905 N00.getOpcode() == ISD::ZERO_EXTEND) &&
14906 N00.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY)) {
14907 APInt Mask = cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
14908 APInt ShAmt = N1C->getAPIntValue();
14909 Mask = Mask.shl(ShAmt);
14910 if (Mask != 0)
14911 return DAG.getNode(ISD::AND, N->getDebugLoc(), VT,
14912 N00, DAG.getConstant(Mask, VT));
14913 }
14914 }
14915
Nadav Rotemfb0dfbb2011-10-30 13:24:22 +000014916
14917 // Hardware support for vector shifts is sparse which makes us scalarize the
14918 // vector operations in many cases. Also, on sandybridge ADD is faster than
14919 // shl.
14920 // (shl V, 1) -> add V,V
14921 if (isSplatVector(N1.getNode())) {
14922 assert(N0.getValueType().isVector() && "Invalid vector shift type");
14923 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1->getOperand(0));
14924 // We shift all of the values by one. In many cases we do not have
14925 // hardware support for this operation. This is better expressed as an ADD
14926 // of two values.
14927 if (N1C && (1 == N1C->getZExtValue())) {
14928 return DAG.getNode(ISD::ADD, N->getDebugLoc(), VT, N0, N0);
14929 }
14930 }
14931
Evan Chengad9c0a32009-12-15 00:53:42 +000014932 return SDValue();
14933}
Evan Cheng0b0cd912009-03-28 05:57:29 +000014934
Nate Begeman740ab032009-01-26 00:52:55 +000014935/// PerformShiftCombine - Transforms vector shift nodes to use vector shifts
14936/// when possible.
14937static SDValue PerformShiftCombine(SDNode* N, SelectionDAG &DAG,
Mon P Wang845b1892012-02-01 22:15:20 +000014938 TargetLowering::DAGCombinerInfo &DCI,
Nate Begeman740ab032009-01-26 00:52:55 +000014939 const X86Subtarget *Subtarget) {
Evan Chengad9c0a32009-12-15 00:53:42 +000014940 EVT VT = N->getValueType(0);
Nadav Rotemfb0dfbb2011-10-30 13:24:22 +000014941 if (N->getOpcode() == ISD::SHL) {
14942 SDValue V = PerformSHLCombine(N, DAG);
14943 if (V.getNode()) return V;
14944 }
Evan Chengad9c0a32009-12-15 00:53:42 +000014945
Nate Begeman740ab032009-01-26 00:52:55 +000014946 // On X86 with SSE2 support, we can transform this to a vector shift if
14947 // all elements are shifted by the same amount. We can't do this in legalize
14948 // because the a constant vector is typically transformed to a constant pool
14949 // so we have no knowledge of the shift amount.
Craig Topper1accb7e2012-01-10 06:54:16 +000014950 if (!Subtarget->hasSSE2())
Nate Begemanc2fd67f2009-01-26 03:15:31 +000014951 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +000014952
Craig Topper7be5dfd2011-11-12 09:58:49 +000014953 if (VT != MVT::v2i64 && VT != MVT::v4i32 && VT != MVT::v8i16 &&
14954 (!Subtarget->hasAVX2() ||
14955 (VT != MVT::v4i64 && VT != MVT::v8i32 && VT != MVT::v16i16)))
Nate Begemanc2fd67f2009-01-26 03:15:31 +000014956 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +000014957
Mon P Wang3becd092009-01-28 08:12:05 +000014958 SDValue ShAmtOp = N->getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +000014959 EVT EltVT = VT.getVectorElementType();
Chris Lattner47b4ce82009-03-11 05:48:52 +000014960 DebugLoc DL = N->getDebugLoc();
Mon P Wangefa42202009-09-03 19:56:25 +000014961 SDValue BaseShAmt = SDValue();
Mon P Wang3becd092009-01-28 08:12:05 +000014962 if (ShAmtOp.getOpcode() == ISD::BUILD_VECTOR) {
14963 unsigned NumElts = VT.getVectorNumElements();
14964 unsigned i = 0;
14965 for (; i != NumElts; ++i) {
14966 SDValue Arg = ShAmtOp.getOperand(i);
14967 if (Arg.getOpcode() == ISD::UNDEF) continue;
14968 BaseShAmt = Arg;
14969 break;
14970 }
Craig Topper37c26772012-01-17 04:44:50 +000014971 // Handle the case where the build_vector is all undef
14972 // FIXME: Should DAG allow this?
14973 if (i == NumElts)
14974 return SDValue();
14975
Mon P Wang3becd092009-01-28 08:12:05 +000014976 for (; i != NumElts; ++i) {
14977 SDValue Arg = ShAmtOp.getOperand(i);
14978 if (Arg.getOpcode() == ISD::UNDEF) continue;
14979 if (Arg != BaseShAmt) {
14980 return SDValue();
14981 }
14982 }
14983 } else if (ShAmtOp.getOpcode() == ISD::VECTOR_SHUFFLE &&
Nate Begeman9008ca62009-04-27 18:41:29 +000014984 cast<ShuffleVectorSDNode>(ShAmtOp)->isSplat()) {
Mon P Wangefa42202009-09-03 19:56:25 +000014985 SDValue InVec = ShAmtOp.getOperand(0);
14986 if (InVec.getOpcode() == ISD::BUILD_VECTOR) {
14987 unsigned NumElts = InVec.getValueType().getVectorNumElements();
14988 unsigned i = 0;
14989 for (; i != NumElts; ++i) {
14990 SDValue Arg = InVec.getOperand(i);
14991 if (Arg.getOpcode() == ISD::UNDEF) continue;
14992 BaseShAmt = Arg;
14993 break;
14994 }
14995 } else if (InVec.getOpcode() == ISD::INSERT_VECTOR_ELT) {
14996 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(InVec.getOperand(2))) {
Evan Chengae3ecf92010-02-16 21:09:44 +000014997 unsigned SplatIdx= cast<ShuffleVectorSDNode>(ShAmtOp)->getSplatIndex();
Mon P Wangefa42202009-09-03 19:56:25 +000014998 if (C->getZExtValue() == SplatIdx)
14999 BaseShAmt = InVec.getOperand(1);
15000 }
15001 }
Mon P Wang845b1892012-02-01 22:15:20 +000015002 if (BaseShAmt.getNode() == 0) {
15003 // Don't create instructions with illegal types after legalize
15004 // types has run.
15005 if (!DAG.getTargetLoweringInfo().isTypeLegal(EltVT) &&
15006 !DCI.isBeforeLegalize())
15007 return SDValue();
15008
Mon P Wangefa42202009-09-03 19:56:25 +000015009 BaseShAmt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, EltVT, ShAmtOp,
15010 DAG.getIntPtrConstant(0));
Mon P Wang845b1892012-02-01 22:15:20 +000015011 }
Mon P Wang3becd092009-01-28 08:12:05 +000015012 } else
Nate Begemanc2fd67f2009-01-26 03:15:31 +000015013 return SDValue();
Nate Begeman740ab032009-01-26 00:52:55 +000015014
Mon P Wangefa42202009-09-03 19:56:25 +000015015 // The shift amount is an i32.
Owen Anderson825b72b2009-08-11 20:47:22 +000015016 if (EltVT.bitsGT(MVT::i32))
15017 BaseShAmt = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, BaseShAmt);
15018 else if (EltVT.bitsLT(MVT::i32))
Mon P Wangefa42202009-09-03 19:56:25 +000015019 BaseShAmt = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::i32, BaseShAmt);
Nate Begeman740ab032009-01-26 00:52:55 +000015020
Nate Begemanc2fd67f2009-01-26 03:15:31 +000015021 // The shift amount is identical so we can do a vector shift.
15022 SDValue ValOp = N->getOperand(0);
15023 switch (N->getOpcode()) {
15024 default:
Torok Edwinc23197a2009-07-14 16:55:14 +000015025 llvm_unreachable("Unknown shift opcode!");
Nate Begemanc2fd67f2009-01-26 03:15:31 +000015026 case ISD::SHL:
Craig Toppered2e13d2012-01-22 19:15:14 +000015027 switch (VT.getSimpleVT().SimpleTy) {
15028 default: return SDValue();
15029 case MVT::v2i64:
15030 case MVT::v4i32:
15031 case MVT::v8i16:
15032 case MVT::v4i64:
15033 case MVT::v8i32:
15034 case MVT::v16i16:
15035 return getTargetVShiftNode(X86ISD::VSHLI, DL, VT, ValOp, BaseShAmt, DAG);
15036 }
Nate Begemanc2fd67f2009-01-26 03:15:31 +000015037 case ISD::SRA:
Craig Toppered2e13d2012-01-22 19:15:14 +000015038 switch (VT.getSimpleVT().SimpleTy) {
15039 default: return SDValue();
15040 case MVT::v4i32:
15041 case MVT::v8i16:
15042 case MVT::v8i32:
15043 case MVT::v16i16:
15044 return getTargetVShiftNode(X86ISD::VSRAI, DL, VT, ValOp, BaseShAmt, DAG);
15045 }
Nate Begemanc2fd67f2009-01-26 03:15:31 +000015046 case ISD::SRL:
Craig Toppered2e13d2012-01-22 19:15:14 +000015047 switch (VT.getSimpleVT().SimpleTy) {
15048 default: return SDValue();
15049 case MVT::v2i64:
15050 case MVT::v4i32:
15051 case MVT::v8i16:
15052 case MVT::v4i64:
15053 case MVT::v8i32:
15054 case MVT::v16i16:
15055 return getTargetVShiftNode(X86ISD::VSRLI, DL, VT, ValOp, BaseShAmt, DAG);
15056 }
Nate Begeman740ab032009-01-26 00:52:55 +000015057 }
Nate Begeman740ab032009-01-26 00:52:55 +000015058}
15059
Nate Begemanb65c1752010-12-17 22:55:37 +000015060
Stuart Hastings865f0932011-06-03 23:53:54 +000015061// CMPEQCombine - Recognize the distinctive (AND (setcc ...) (setcc ..))
15062// where both setccs reference the same FP CMP, and rewrite for CMPEQSS
15063// and friends. Likewise for OR -> CMPNEQSS.
15064static SDValue CMPEQCombine(SDNode *N, SelectionDAG &DAG,
15065 TargetLowering::DAGCombinerInfo &DCI,
15066 const X86Subtarget *Subtarget) {
15067 unsigned opcode;
15068
15069 // SSE1 supports CMP{eq|ne}SS, and SSE2 added CMP{eq|ne}SD, but
15070 // we're requiring SSE2 for both.
Craig Topper1accb7e2012-01-10 06:54:16 +000015071 if (Subtarget->hasSSE2() && isAndOrOfSetCCs(SDValue(N, 0U), opcode)) {
Stuart Hastings865f0932011-06-03 23:53:54 +000015072 SDValue N0 = N->getOperand(0);
15073 SDValue N1 = N->getOperand(1);
15074 SDValue CMP0 = N0->getOperand(1);
15075 SDValue CMP1 = N1->getOperand(1);
15076 DebugLoc DL = N->getDebugLoc();
15077
15078 // The SETCCs should both refer to the same CMP.
15079 if (CMP0.getOpcode() != X86ISD::CMP || CMP0 != CMP1)
15080 return SDValue();
15081
15082 SDValue CMP00 = CMP0->getOperand(0);
15083 SDValue CMP01 = CMP0->getOperand(1);
15084 EVT VT = CMP00.getValueType();
15085
15086 if (VT == MVT::f32 || VT == MVT::f64) {
15087 bool ExpectingFlags = false;
15088 // Check for any users that want flags:
15089 for (SDNode::use_iterator UI = N->use_begin(),
15090 UE = N->use_end();
15091 !ExpectingFlags && UI != UE; ++UI)
15092 switch (UI->getOpcode()) {
15093 default:
15094 case ISD::BR_CC:
15095 case ISD::BRCOND:
15096 case ISD::SELECT:
15097 ExpectingFlags = true;
15098 break;
15099 case ISD::CopyToReg:
15100 case ISD::SIGN_EXTEND:
15101 case ISD::ZERO_EXTEND:
15102 case ISD::ANY_EXTEND:
15103 break;
15104 }
15105
15106 if (!ExpectingFlags) {
15107 enum X86::CondCode cc0 = (enum X86::CondCode)N0.getConstantOperandVal(0);
15108 enum X86::CondCode cc1 = (enum X86::CondCode)N1.getConstantOperandVal(0);
15109
15110 if (cc1 == X86::COND_E || cc1 == X86::COND_NE) {
15111 X86::CondCode tmp = cc0;
15112 cc0 = cc1;
15113 cc1 = tmp;
15114 }
15115
15116 if ((cc0 == X86::COND_E && cc1 == X86::COND_NP) ||
15117 (cc0 == X86::COND_NE && cc1 == X86::COND_P)) {
15118 bool is64BitFP = (CMP00.getValueType() == MVT::f64);
15119 X86ISD::NodeType NTOperator = is64BitFP ?
15120 X86ISD::FSETCCsd : X86ISD::FSETCCss;
15121 // FIXME: need symbolic constants for these magic numbers.
15122 // See X86ATTInstPrinter.cpp:printSSECC().
15123 unsigned x86cc = (cc0 == X86::COND_E) ? 0 : 4;
15124 SDValue OnesOrZeroesF = DAG.getNode(NTOperator, DL, MVT::f32, CMP00, CMP01,
15125 DAG.getConstant(x86cc, MVT::i8));
15126 SDValue OnesOrZeroesI = DAG.getNode(ISD::BITCAST, DL, MVT::i32,
15127 OnesOrZeroesF);
15128 SDValue ANDed = DAG.getNode(ISD::AND, DL, MVT::i32, OnesOrZeroesI,
15129 DAG.getConstant(1, MVT::i32));
15130 SDValue OneBitOfTruth = DAG.getNode(ISD::TRUNCATE, DL, MVT::i8, ANDed);
15131 return OneBitOfTruth;
15132 }
15133 }
15134 }
15135 }
15136 return SDValue();
15137}
15138
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +000015139/// CanFoldXORWithAllOnes - Test whether the XOR operand is a AllOnes vector
15140/// so it can be folded inside ANDNP.
15141static bool CanFoldXORWithAllOnes(const SDNode *N) {
15142 EVT VT = N->getValueType(0);
15143
15144 // Match direct AllOnes for 128 and 256-bit vectors
15145 if (ISD::isBuildVectorAllOnes(N))
15146 return true;
15147
15148 // Look through a bit convert.
15149 if (N->getOpcode() == ISD::BITCAST)
15150 N = N->getOperand(0).getNode();
15151
15152 // Sometimes the operand may come from a insert_subvector building a 256-bit
15153 // allones vector
Craig Topper7a9a28b2012-08-12 02:23:29 +000015154 if (VT.is256BitVector() &&
Bill Wendling456a9252011-08-04 00:32:58 +000015155 N->getOpcode() == ISD::INSERT_SUBVECTOR) {
15156 SDValue V1 = N->getOperand(0);
15157 SDValue V2 = N->getOperand(1);
15158
15159 if (V1.getOpcode() == ISD::INSERT_SUBVECTOR &&
15160 V1.getOperand(0).getOpcode() == ISD::UNDEF &&
15161 ISD::isBuildVectorAllOnes(V1.getOperand(1).getNode()) &&
15162 ISD::isBuildVectorAllOnes(V2.getNode()))
15163 return true;
15164 }
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +000015165
15166 return false;
15167}
15168
Nate Begemanb65c1752010-12-17 22:55:37 +000015169static SDValue PerformAndCombine(SDNode *N, SelectionDAG &DAG,
15170 TargetLowering::DAGCombinerInfo &DCI,
15171 const X86Subtarget *Subtarget) {
15172 if (DCI.isBeforeLegalizeOps())
15173 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000015174
Stuart Hastings865f0932011-06-03 23:53:54 +000015175 SDValue R = CMPEQCombine(N, DAG, DCI, Subtarget);
15176 if (R.getNode())
15177 return R;
15178
Craig Topper54a11172011-10-14 07:06:56 +000015179 EVT VT = N->getValueType(0);
15180
Craig Topperb4c94572011-10-21 06:55:01 +000015181 // Create ANDN, BLSI, and BLSR instructions
15182 // BLSI is X & (-X)
15183 // BLSR is X & (X-1)
Craig Topper54a11172011-10-14 07:06:56 +000015184 if (Subtarget->hasBMI() && (VT == MVT::i32 || VT == MVT::i64)) {
15185 SDValue N0 = N->getOperand(0);
15186 SDValue N1 = N->getOperand(1);
15187 DebugLoc DL = N->getDebugLoc();
15188
15189 // Check LHS for not
15190 if (N0.getOpcode() == ISD::XOR && isAllOnes(N0.getOperand(1)))
15191 return DAG.getNode(X86ISD::ANDN, DL, VT, N0.getOperand(0), N1);
15192 // Check RHS for not
15193 if (N1.getOpcode() == ISD::XOR && isAllOnes(N1.getOperand(1)))
15194 return DAG.getNode(X86ISD::ANDN, DL, VT, N1.getOperand(0), N0);
15195
Craig Topperb4c94572011-10-21 06:55:01 +000015196 // Check LHS for neg
15197 if (N0.getOpcode() == ISD::SUB && N0.getOperand(1) == N1 &&
15198 isZero(N0.getOperand(0)))
15199 return DAG.getNode(X86ISD::BLSI, DL, VT, N1);
15200
15201 // Check RHS for neg
15202 if (N1.getOpcode() == ISD::SUB && N1.getOperand(1) == N0 &&
15203 isZero(N1.getOperand(0)))
15204 return DAG.getNode(X86ISD::BLSI, DL, VT, N0);
15205
15206 // Check LHS for X-1
15207 if (N0.getOpcode() == ISD::ADD && N0.getOperand(0) == N1 &&
15208 isAllOnes(N0.getOperand(1)))
15209 return DAG.getNode(X86ISD::BLSR, DL, VT, N1);
15210
15211 // Check RHS for X-1
15212 if (N1.getOpcode() == ISD::ADD && N1.getOperand(0) == N0 &&
15213 isAllOnes(N1.getOperand(1)))
15214 return DAG.getNode(X86ISD::BLSR, DL, VT, N0);
15215
Craig Topper54a11172011-10-14 07:06:56 +000015216 return SDValue();
15217 }
15218
Bruno Cardoso Lopes466b0222011-07-13 21:36:51 +000015219 // Want to form ANDNP nodes:
15220 // 1) In the hopes of then easily combining them with OR and AND nodes
15221 // to form PBLEND/PSIGN.
15222 // 2) To match ANDN packed intrinsics
Bruno Cardoso Lopes466b0222011-07-13 21:36:51 +000015223 if (VT != MVT::v2i64 && VT != MVT::v4i64)
Nate Begemanb65c1752010-12-17 22:55:37 +000015224 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000015225
Nate Begemanb65c1752010-12-17 22:55:37 +000015226 SDValue N0 = N->getOperand(0);
15227 SDValue N1 = N->getOperand(1);
15228 DebugLoc DL = N->getDebugLoc();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000015229
Nate Begemanb65c1752010-12-17 22:55:37 +000015230 // Check LHS for vnot
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000015231 if (N0.getOpcode() == ISD::XOR &&
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +000015232 //ISD::isBuildVectorAllOnes(N0.getOperand(1).getNode()))
15233 CanFoldXORWithAllOnes(N0.getOperand(1).getNode()))
Bruno Cardoso Lopesc1af4772011-07-13 21:36:47 +000015234 return DAG.getNode(X86ISD::ANDNP, DL, VT, N0.getOperand(0), N1);
Nate Begemanb65c1752010-12-17 22:55:37 +000015235
15236 // Check RHS for vnot
15237 if (N1.getOpcode() == ISD::XOR &&
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +000015238 //ISD::isBuildVectorAllOnes(N1.getOperand(1).getNode()))
15239 CanFoldXORWithAllOnes(N1.getOperand(1).getNode()))
Bruno Cardoso Lopesc1af4772011-07-13 21:36:47 +000015240 return DAG.getNode(X86ISD::ANDNP, DL, VT, N1.getOperand(0), N0);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000015241
Nate Begemanb65c1752010-12-17 22:55:37 +000015242 return SDValue();
15243}
15244
Evan Cheng760d1942010-01-04 21:22:48 +000015245static SDValue PerformOrCombine(SDNode *N, SelectionDAG &DAG,
Evan Cheng8b1190a2010-04-28 01:18:01 +000015246 TargetLowering::DAGCombinerInfo &DCI,
Evan Cheng760d1942010-01-04 21:22:48 +000015247 const X86Subtarget *Subtarget) {
Evan Cheng39cfeec2010-04-28 02:25:18 +000015248 if (DCI.isBeforeLegalizeOps())
Evan Cheng8b1190a2010-04-28 01:18:01 +000015249 return SDValue();
15250
Stuart Hastings865f0932011-06-03 23:53:54 +000015251 SDValue R = CMPEQCombine(N, DAG, DCI, Subtarget);
15252 if (R.getNode())
15253 return R;
15254
Evan Cheng760d1942010-01-04 21:22:48 +000015255 EVT VT = N->getValueType(0);
Evan Cheng760d1942010-01-04 21:22:48 +000015256
Evan Cheng760d1942010-01-04 21:22:48 +000015257 SDValue N0 = N->getOperand(0);
15258 SDValue N1 = N->getOperand(1);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000015259
Nate Begemanb65c1752010-12-17 22:55:37 +000015260 // look for psign/blend
Craig Topper1666cb62011-11-19 07:07:26 +000015261 if (VT == MVT::v2i64 || VT == MVT::v4i64) {
Craig Topperd0a31172012-01-10 06:37:29 +000015262 if (!Subtarget->hasSSSE3() ||
Craig Topper1666cb62011-11-19 07:07:26 +000015263 (VT == MVT::v4i64 && !Subtarget->hasAVX2()))
15264 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000015265
Craig Topper1666cb62011-11-19 07:07:26 +000015266 // Canonicalize pandn to RHS
15267 if (N0.getOpcode() == X86ISD::ANDNP)
15268 std::swap(N0, N1);
Lang Hames9ffaa6a2012-01-10 22:53:20 +000015269 // or (and (m, y), (pandn m, x))
Craig Topper1666cb62011-11-19 07:07:26 +000015270 if (N0.getOpcode() == ISD::AND && N1.getOpcode() == X86ISD::ANDNP) {
15271 SDValue Mask = N1.getOperand(0);
15272 SDValue X = N1.getOperand(1);
15273 SDValue Y;
15274 if (N0.getOperand(0) == Mask)
15275 Y = N0.getOperand(1);
15276 if (N0.getOperand(1) == Mask)
15277 Y = N0.getOperand(0);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000015278
Craig Topper1666cb62011-11-19 07:07:26 +000015279 // Check to see if the mask appeared in both the AND and ANDNP and
15280 if (!Y.getNode())
15281 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000015282
Craig Topper1666cb62011-11-19 07:07:26 +000015283 // Validate that X, Y, and Mask are BIT_CONVERTS, and see through them.
Craig Topper1666cb62011-11-19 07:07:26 +000015284 // Look through mask bitcast.
Nadav Rotem4ac90812012-04-01 19:31:22 +000015285 if (Mask.getOpcode() == ISD::BITCAST)
15286 Mask = Mask.getOperand(0);
15287 if (X.getOpcode() == ISD::BITCAST)
15288 X = X.getOperand(0);
15289 if (Y.getOpcode() == ISD::BITCAST)
15290 Y = Y.getOperand(0);
15291
Craig Topper1666cb62011-11-19 07:07:26 +000015292 EVT MaskVT = Mask.getValueType();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000015293
Craig Toppered2e13d2012-01-22 19:15:14 +000015294 // Validate that the Mask operand is a vector sra node.
Craig Topper1666cb62011-11-19 07:07:26 +000015295 // FIXME: what to do for bytes, since there is a psignb/pblendvb, but
15296 // there is no psrai.b
Craig Topper7fb8b0c2012-01-23 06:46:22 +000015297 if (Mask.getOpcode() != X86ISD::VSRAI)
Craig Toppered2e13d2012-01-22 19:15:14 +000015298 return SDValue();
Craig Topper1666cb62011-11-19 07:07:26 +000015299
15300 // Check that the SRA is all signbits.
Craig Topper7fb8b0c2012-01-23 06:46:22 +000015301 SDValue SraC = Mask.getOperand(1);
Craig Topper1666cb62011-11-19 07:07:26 +000015302 unsigned SraAmt = cast<ConstantSDNode>(SraC)->getZExtValue();
15303 unsigned EltBits = MaskVT.getVectorElementType().getSizeInBits();
15304 if ((SraAmt + 1) != EltBits)
15305 return SDValue();
15306
15307 DebugLoc DL = N->getDebugLoc();
15308
15309 // Now we know we at least have a plendvb with the mask val. See if
15310 // we can form a psignb/w/d.
15311 // psign = x.type == y.type == mask.type && y = sub(0, x);
Craig Topper1666cb62011-11-19 07:07:26 +000015312 if (Y.getOpcode() == ISD::SUB && Y.getOperand(1) == X &&
15313 ISD::isBuildVectorAllZeros(Y.getOperand(0).getNode()) &&
Craig Toppered2e13d2012-01-22 19:15:14 +000015314 X.getValueType() == MaskVT && Y.getValueType() == MaskVT) {
15315 assert((EltBits == 8 || EltBits == 16 || EltBits == 32) &&
15316 "Unsupported VT for PSIGN");
Craig Topper7fb8b0c2012-01-23 06:46:22 +000015317 Mask = DAG.getNode(X86ISD::PSIGN, DL, MaskVT, X, Mask.getOperand(0));
Craig Toppered2e13d2012-01-22 19:15:14 +000015318 return DAG.getNode(ISD::BITCAST, DL, VT, Mask);
Craig Topper1666cb62011-11-19 07:07:26 +000015319 }
15320 // PBLENDVB only available on SSE 4.1
Craig Topperd0a31172012-01-10 06:37:29 +000015321 if (!Subtarget->hasSSE41())
Craig Topper1666cb62011-11-19 07:07:26 +000015322 return SDValue();
15323
15324 EVT BlendVT = (VT == MVT::v4i64) ? MVT::v32i8 : MVT::v16i8;
15325
15326 X = DAG.getNode(ISD::BITCAST, DL, BlendVT, X);
15327 Y = DAG.getNode(ISD::BITCAST, DL, BlendVT, Y);
15328 Mask = DAG.getNode(ISD::BITCAST, DL, BlendVT, Mask);
Nadav Rotem18197d72011-11-30 10:13:37 +000015329 Mask = DAG.getNode(ISD::VSELECT, DL, BlendVT, Mask, Y, X);
Craig Topper1666cb62011-11-19 07:07:26 +000015330 return DAG.getNode(ISD::BITCAST, DL, VT, Mask);
Nate Begemanb65c1752010-12-17 22:55:37 +000015331 }
15332 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000015333
Craig Topper1666cb62011-11-19 07:07:26 +000015334 if (VT != MVT::i16 && VT != MVT::i32 && VT != MVT::i64)
15335 return SDValue();
15336
Nate Begemanb65c1752010-12-17 22:55:37 +000015337 // fold (or (x << c) | (y >> (64 - c))) ==> (shld64 x, y, c)
Evan Cheng760d1942010-01-04 21:22:48 +000015338 if (N0.getOpcode() == ISD::SRL && N1.getOpcode() == ISD::SHL)
15339 std::swap(N0, N1);
15340 if (N0.getOpcode() != ISD::SHL || N1.getOpcode() != ISD::SRL)
15341 return SDValue();
Evan Cheng8b1190a2010-04-28 01:18:01 +000015342 if (!N0.hasOneUse() || !N1.hasOneUse())
15343 return SDValue();
Evan Cheng760d1942010-01-04 21:22:48 +000015344
15345 SDValue ShAmt0 = N0.getOperand(1);
15346 if (ShAmt0.getValueType() != MVT::i8)
15347 return SDValue();
15348 SDValue ShAmt1 = N1.getOperand(1);
15349 if (ShAmt1.getValueType() != MVT::i8)
15350 return SDValue();
15351 if (ShAmt0.getOpcode() == ISD::TRUNCATE)
15352 ShAmt0 = ShAmt0.getOperand(0);
15353 if (ShAmt1.getOpcode() == ISD::TRUNCATE)
15354 ShAmt1 = ShAmt1.getOperand(0);
15355
15356 DebugLoc DL = N->getDebugLoc();
15357 unsigned Opc = X86ISD::SHLD;
15358 SDValue Op0 = N0.getOperand(0);
15359 SDValue Op1 = N1.getOperand(0);
15360 if (ShAmt0.getOpcode() == ISD::SUB) {
15361 Opc = X86ISD::SHRD;
15362 std::swap(Op0, Op1);
15363 std::swap(ShAmt0, ShAmt1);
15364 }
15365
Evan Cheng8b1190a2010-04-28 01:18:01 +000015366 unsigned Bits = VT.getSizeInBits();
Evan Cheng760d1942010-01-04 21:22:48 +000015367 if (ShAmt1.getOpcode() == ISD::SUB) {
15368 SDValue Sum = ShAmt1.getOperand(0);
15369 if (ConstantSDNode *SumC = dyn_cast<ConstantSDNode>(Sum)) {
Dan Gohman4e39e9d2010-06-24 14:30:44 +000015370 SDValue ShAmt1Op1 = ShAmt1.getOperand(1);
15371 if (ShAmt1Op1.getNode()->getOpcode() == ISD::TRUNCATE)
15372 ShAmt1Op1 = ShAmt1Op1.getOperand(0);
15373 if (SumC->getSExtValue() == Bits && ShAmt1Op1 == ShAmt0)
Evan Cheng760d1942010-01-04 21:22:48 +000015374 return DAG.getNode(Opc, DL, VT,
15375 Op0, Op1,
15376 DAG.getNode(ISD::TRUNCATE, DL,
15377 MVT::i8, ShAmt0));
15378 }
15379 } else if (ConstantSDNode *ShAmt1C = dyn_cast<ConstantSDNode>(ShAmt1)) {
15380 ConstantSDNode *ShAmt0C = dyn_cast<ConstantSDNode>(ShAmt0);
15381 if (ShAmt0C &&
Evan Cheng8b1190a2010-04-28 01:18:01 +000015382 ShAmt0C->getSExtValue() + ShAmt1C->getSExtValue() == Bits)
Evan Cheng760d1942010-01-04 21:22:48 +000015383 return DAG.getNode(Opc, DL, VT,
15384 N0.getOperand(0), N1.getOperand(0),
15385 DAG.getNode(ISD::TRUNCATE, DL,
15386 MVT::i8, ShAmt0));
15387 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000015388
Evan Cheng760d1942010-01-04 21:22:48 +000015389 return SDValue();
15390}
15391
Manman Ren92363622012-06-07 22:39:10 +000015392// Generate NEG and CMOV for integer abs.
15393static SDValue performIntegerAbsCombine(SDNode *N, SelectionDAG &DAG) {
15394 EVT VT = N->getValueType(0);
15395
15396 // Since X86 does not have CMOV for 8-bit integer, we don't convert
15397 // 8-bit integer abs to NEG and CMOV.
15398 if (VT.isInteger() && VT.getSizeInBits() == 8)
15399 return SDValue();
15400
15401 SDValue N0 = N->getOperand(0);
15402 SDValue N1 = N->getOperand(1);
15403 DebugLoc DL = N->getDebugLoc();
15404
15405 // Check pattern of XOR(ADD(X,Y), Y) where Y is SRA(X, size(X)-1)
15406 // and change it to SUB and CMOV.
15407 if (VT.isInteger() && N->getOpcode() == ISD::XOR &&
15408 N0.getOpcode() == ISD::ADD &&
15409 N0.getOperand(1) == N1 &&
15410 N1.getOpcode() == ISD::SRA &&
15411 N1.getOperand(0) == N0.getOperand(0))
15412 if (ConstantSDNode *Y1C = dyn_cast<ConstantSDNode>(N1.getOperand(1)))
15413 if (Y1C->getAPIntValue() == VT.getSizeInBits()-1) {
15414 // Generate SUB & CMOV.
15415 SDValue Neg = DAG.getNode(X86ISD::SUB, DL, DAG.getVTList(VT, MVT::i32),
15416 DAG.getConstant(0, VT), N0.getOperand(0));
15417
15418 SDValue Ops[] = { N0.getOperand(0), Neg,
15419 DAG.getConstant(X86::COND_GE, MVT::i8),
15420 SDValue(Neg.getNode(), 1) };
15421 return DAG.getNode(X86ISD::CMOV, DL, DAG.getVTList(VT, MVT::Glue),
15422 Ops, array_lengthof(Ops));
15423 }
15424 return SDValue();
15425}
15426
Craig Topper3738ccd2011-12-27 06:27:23 +000015427// PerformXorCombine - Attempts to turn XOR nodes into BLSMSK nodes
Craig Topperb4c94572011-10-21 06:55:01 +000015428static SDValue PerformXorCombine(SDNode *N, SelectionDAG &DAG,
15429 TargetLowering::DAGCombinerInfo &DCI,
15430 const X86Subtarget *Subtarget) {
15431 if (DCI.isBeforeLegalizeOps())
15432 return SDValue();
15433
Manman Ren45d53b82012-06-08 18:58:26 +000015434 if (Subtarget->hasCMov()) {
15435 SDValue RV = performIntegerAbsCombine(N, DAG);
15436 if (RV.getNode())
15437 return RV;
15438 }
Manman Ren92363622012-06-07 22:39:10 +000015439
15440 // Try forming BMI if it is available.
15441 if (!Subtarget->hasBMI())
15442 return SDValue();
15443
Craig Topperb4c94572011-10-21 06:55:01 +000015444 EVT VT = N->getValueType(0);
15445
15446 if (VT != MVT::i32 && VT != MVT::i64)
15447 return SDValue();
15448
Craig Topper3738ccd2011-12-27 06:27:23 +000015449 assert(Subtarget->hasBMI() && "Creating BLSMSK requires BMI instructions");
15450
Craig Topperb4c94572011-10-21 06:55:01 +000015451 // Create BLSMSK instructions by finding X ^ (X-1)
15452 SDValue N0 = N->getOperand(0);
15453 SDValue N1 = N->getOperand(1);
15454 DebugLoc DL = N->getDebugLoc();
15455
15456 if (N0.getOpcode() == ISD::ADD && N0.getOperand(0) == N1 &&
15457 isAllOnes(N0.getOperand(1)))
15458 return DAG.getNode(X86ISD::BLSMSK, DL, VT, N1);
15459
15460 if (N1.getOpcode() == ISD::ADD && N1.getOperand(0) == N0 &&
15461 isAllOnes(N1.getOperand(1)))
15462 return DAG.getNode(X86ISD::BLSMSK, DL, VT, N0);
15463
15464 return SDValue();
15465}
15466
Nadav Rotem91e43fd2011-09-18 10:39:32 +000015467/// PerformLOADCombine - Do target-specific dag combines on LOAD nodes.
15468static SDValue PerformLOADCombine(SDNode *N, SelectionDAG &DAG,
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000015469 TargetLowering::DAGCombinerInfo &DCI,
15470 const X86Subtarget *Subtarget) {
Nadav Rotem91e43fd2011-09-18 10:39:32 +000015471 LoadSDNode *Ld = cast<LoadSDNode>(N);
15472 EVT RegVT = Ld->getValueType(0);
15473 EVT MemVT = Ld->getMemoryVT();
15474 DebugLoc dl = Ld->getDebugLoc();
15475 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
15476
15477 ISD::LoadExtType Ext = Ld->getExtensionType();
15478
Nadav Rotemca6f2962011-09-18 19:00:23 +000015479 // If this is a vector EXT Load then attempt to optimize it using a
Nadav Rotem91e43fd2011-09-18 10:39:32 +000015480 // shuffle. We need SSE4 for the shuffles.
15481 // TODO: It is possible to support ZExt by zeroing the undef values
15482 // during the shuffle phase or after the shuffle.
Eli Friedmanda813f42011-12-28 21:24:44 +000015483 if (RegVT.isVector() && RegVT.isInteger() &&
15484 Ext == ISD::EXTLOAD && Subtarget->hasSSE41()) {
Nadav Rotem91e43fd2011-09-18 10:39:32 +000015485 assert(MemVT != RegVT && "Cannot extend to the same type");
15486 assert(MemVT.isVector() && "Must load a vector from memory");
15487
15488 unsigned NumElems = RegVT.getVectorNumElements();
15489 unsigned RegSz = RegVT.getSizeInBits();
15490 unsigned MemSz = MemVT.getSizeInBits();
15491 assert(RegSz > MemSz && "Register size must be greater than the mem size");
Nadav Rotem91e43fd2011-09-18 10:39:32 +000015492
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000015493 // All sizes must be a power of two.
15494 if (!isPowerOf2_32(RegSz * MemSz * NumElems))
15495 return SDValue();
15496
15497 // Attempt to load the original value using scalar loads.
15498 // Find the largest scalar type that divides the total loaded size.
Nadav Rotem91e43fd2011-09-18 10:39:32 +000015499 MVT SclrLoadTy = MVT::i8;
15500 for (unsigned tp = MVT::FIRST_INTEGER_VALUETYPE;
15501 tp < MVT::LAST_INTEGER_VALUETYPE; ++tp) {
15502 MVT Tp = (MVT::SimpleValueType)tp;
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000015503 if (TLI.isTypeLegal(Tp) && ((MemSz % Tp.getSizeInBits()) == 0)) {
Nadav Rotem91e43fd2011-09-18 10:39:32 +000015504 SclrLoadTy = Tp;
Nadav Rotem91e43fd2011-09-18 10:39:32 +000015505 }
15506 }
15507
Nadav Rotem5cd95e12012-07-11 13:27:05 +000015508 // On 32bit systems, we can't save 64bit integers. Try bitcasting to F64.
15509 if (TLI.isTypeLegal(MVT::f64) && SclrLoadTy.getSizeInBits() < 64 &&
15510 (64 <= MemSz))
15511 SclrLoadTy = MVT::f64;
15512
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000015513 // Calculate the number of scalar loads that we need to perform
15514 // in order to load our vector from memory.
15515 unsigned NumLoads = MemSz / SclrLoadTy.getSizeInBits();
Nadav Rotem91e43fd2011-09-18 10:39:32 +000015516
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000015517 // Represent our vector as a sequence of elements which are the
15518 // largest scalar that we can load.
Nadav Rotem91e43fd2011-09-18 10:39:32 +000015519 EVT LoadUnitVecVT = EVT::getVectorVT(*DAG.getContext(), SclrLoadTy,
15520 RegSz/SclrLoadTy.getSizeInBits());
15521
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000015522 // Represent the data using the same element type that is stored in
15523 // memory. In practice, we ''widen'' MemVT.
Nadav Rotem91e43fd2011-09-18 10:39:32 +000015524 EVT WideVecVT = EVT::getVectorVT(*DAG.getContext(), MemVT.getScalarType(),
15525 RegSz/MemVT.getScalarType().getSizeInBits());
Nadav Rotem91e43fd2011-09-18 10:39:32 +000015526
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000015527 assert(WideVecVT.getSizeInBits() == LoadUnitVecVT.getSizeInBits() &&
15528 "Invalid vector type");
Nadav Rotem91e43fd2011-09-18 10:39:32 +000015529
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000015530 // We can't shuffle using an illegal type.
15531 if (!TLI.isTypeLegal(WideVecVT))
15532 return SDValue();
15533
15534 SmallVector<SDValue, 8> Chains;
15535 SDValue Ptr = Ld->getBasePtr();
15536 SDValue Increment = DAG.getConstant(SclrLoadTy.getSizeInBits()/8,
15537 TLI.getPointerTy());
15538 SDValue Res = DAG.getUNDEF(LoadUnitVecVT);
15539
15540 for (unsigned i = 0; i < NumLoads; ++i) {
15541 // Perform a single load.
15542 SDValue ScalarLoad = DAG.getLoad(SclrLoadTy, dl, Ld->getChain(),
15543 Ptr, Ld->getPointerInfo(),
15544 Ld->isVolatile(), Ld->isNonTemporal(),
15545 Ld->isInvariant(), Ld->getAlignment());
15546 Chains.push_back(ScalarLoad.getValue(1));
15547 // Create the first element type using SCALAR_TO_VECTOR in order to avoid
15548 // another round of DAGCombining.
15549 if (i == 0)
15550 Res = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, LoadUnitVecVT, ScalarLoad);
15551 else
15552 Res = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, LoadUnitVecVT, Res,
15553 ScalarLoad, DAG.getIntPtrConstant(i));
15554
15555 Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr, Increment);
15556 }
15557
15558 SDValue TF = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &Chains[0],
15559 Chains.size());
Nadav Rotem91e43fd2011-09-18 10:39:32 +000015560
15561 // Bitcast the loaded value to a vector of the original element type, in
15562 // the size of the target vector type.
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000015563 SDValue SlicedVec = DAG.getNode(ISD::BITCAST, dl, WideVecVT, Res);
Nadav Rotem91e43fd2011-09-18 10:39:32 +000015564 unsigned SizeRatio = RegSz/MemSz;
15565
15566 // Redistribute the loaded elements into the different locations.
15567 SmallVector<int, 8> ShuffleVec(NumElems * SizeRatio, -1);
Craig Topper31a207a2012-05-04 06:39:13 +000015568 for (unsigned i = 0; i != NumElems; ++i)
15569 ShuffleVec[i*SizeRatio] = i;
Nadav Rotem91e43fd2011-09-18 10:39:32 +000015570
15571 SDValue Shuff = DAG.getVectorShuffle(WideVecVT, dl, SlicedVec,
Craig Topperdf966f62012-04-22 19:17:57 +000015572 DAG.getUNDEF(WideVecVT),
15573 &ShuffleVec[0]);
Nadav Rotem91e43fd2011-09-18 10:39:32 +000015574
15575 // Bitcast to the requested type.
15576 Shuff = DAG.getNode(ISD::BITCAST, dl, RegVT, Shuff);
15577 // Replace the original load with the new sequence
15578 // and return the new chain.
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000015579 return DCI.CombineTo(N, Shuff, TF, true);
Nadav Rotem91e43fd2011-09-18 10:39:32 +000015580 }
15581
15582 return SDValue();
15583}
15584
Chris Lattner149a4e52008-02-22 02:09:43 +000015585/// PerformSTORECombine - Do target-specific dag combines on STORE nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000015586static SDValue PerformSTORECombine(SDNode *N, SelectionDAG &DAG,
Evan Cheng536e6672009-03-12 05:59:15 +000015587 const X86Subtarget *Subtarget) {
Nadav Rotem614061b2011-08-10 19:30:14 +000015588 StoreSDNode *St = cast<StoreSDNode>(N);
15589 EVT VT = St->getValue().getValueType();
15590 EVT StVT = St->getMemoryVT();
15591 DebugLoc dl = St->getDebugLoc();
Nadav Rotem5e742a32011-08-11 16:41:21 +000015592 SDValue StoredVal = St->getOperand(1);
15593 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
15594
Nick Lewycky8a8d4792011-12-02 22:16:29 +000015595 // If we are saving a concatenation of two XMM registers, perform two stores.
Nadav Rotem87d35e82012-05-19 20:30:08 +000015596 // On Sandy Bridge, 256-bit memory operations are executed by two
15597 // 128-bit ports. However, on Haswell it is better to issue a single 256-bit
15598 // memory operation.
Craig Topper7a9a28b2012-08-12 02:23:29 +000015599 if (VT.is256BitVector() && !Subtarget->hasAVX2() &&
Craig Topperb4a8aef2012-04-27 21:05:09 +000015600 StoredVal.getNode()->getOpcode() == ISD::CONCAT_VECTORS &&
15601 StoredVal.getNumOperands() == 2) {
Nadav Rotem5e742a32011-08-11 16:41:21 +000015602 SDValue Value0 = StoredVal.getOperand(0);
15603 SDValue Value1 = StoredVal.getOperand(1);
15604
15605 SDValue Stride = DAG.getConstant(16, TLI.getPointerTy());
15606 SDValue Ptr0 = St->getBasePtr();
15607 SDValue Ptr1 = DAG.getNode(ISD::ADD, dl, Ptr0.getValueType(), Ptr0, Stride);
15608
15609 SDValue Ch0 = DAG.getStore(St->getChain(), dl, Value0, Ptr0,
15610 St->getPointerInfo(), St->isVolatile(),
15611 St->isNonTemporal(), St->getAlignment());
15612 SDValue Ch1 = DAG.getStore(St->getChain(), dl, Value1, Ptr1,
15613 St->getPointerInfo(), St->isVolatile(),
15614 St->isNonTemporal(), St->getAlignment());
15615 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Ch0, Ch1);
15616 }
Nadav Rotem614061b2011-08-10 19:30:14 +000015617
15618 // Optimize trunc store (of multiple scalars) to shuffle and store.
15619 // First, pack all of the elements in one place. Next, store to memory
15620 // in fewer chunks.
15621 if (St->isTruncatingStore() && VT.isVector()) {
15622 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
15623 unsigned NumElems = VT.getVectorNumElements();
15624 assert(StVT != VT && "Cannot truncate to the same type");
15625 unsigned FromSz = VT.getVectorElementType().getSizeInBits();
15626 unsigned ToSz = StVT.getVectorElementType().getSizeInBits();
15627
15628 // From, To sizes and ElemCount must be pow of two
15629 if (!isPowerOf2_32(NumElems * FromSz * ToSz)) return SDValue();
Nadav Rotem9c6cdf42011-09-21 08:45:10 +000015630 // We are going to use the original vector elt for storing.
Nadav Rotem64ac73b2011-09-21 17:14:40 +000015631 // Accumulated smaller vector elements must be a multiple of the store size.
Nadav Rotem9c6cdf42011-09-21 08:45:10 +000015632 if (0 != (NumElems * FromSz) % ToSz) return SDValue();
Nadav Rotem91e43fd2011-09-18 10:39:32 +000015633
Nadav Rotem614061b2011-08-10 19:30:14 +000015634 unsigned SizeRatio = FromSz / ToSz;
15635
15636 assert(SizeRatio * NumElems * ToSz == VT.getSizeInBits());
15637
15638 // Create a type on which we perform the shuffle
15639 EVT WideVecVT = EVT::getVectorVT(*DAG.getContext(),
15640 StVT.getScalarType(), NumElems*SizeRatio);
15641
15642 assert(WideVecVT.getSizeInBits() == VT.getSizeInBits());
15643
15644 SDValue WideVec = DAG.getNode(ISD::BITCAST, dl, WideVecVT, St->getValue());
15645 SmallVector<int, 8> ShuffleVec(NumElems * SizeRatio, -1);
Craig Topper31a207a2012-05-04 06:39:13 +000015646 for (unsigned i = 0; i != NumElems; ++i)
15647 ShuffleVec[i] = i * SizeRatio;
Nadav Rotem614061b2011-08-10 19:30:14 +000015648
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000015649 // Can't shuffle using an illegal type.
15650 if (!TLI.isTypeLegal(WideVecVT))
15651 return SDValue();
Nadav Rotem614061b2011-08-10 19:30:14 +000015652
15653 SDValue Shuff = DAG.getVectorShuffle(WideVecVT, dl, WideVec,
Craig Topperdf966f62012-04-22 19:17:57 +000015654 DAG.getUNDEF(WideVecVT),
15655 &ShuffleVec[0]);
Nadav Rotem614061b2011-08-10 19:30:14 +000015656 // At this point all of the data is stored at the bottom of the
15657 // register. We now need to save it to mem.
15658
15659 // Find the largest store unit
15660 MVT StoreType = MVT::i8;
15661 for (unsigned tp = MVT::FIRST_INTEGER_VALUETYPE;
15662 tp < MVT::LAST_INTEGER_VALUETYPE; ++tp) {
15663 MVT Tp = (MVT::SimpleValueType)tp;
Nadav Rotem5cd95e12012-07-11 13:27:05 +000015664 if (TLI.isTypeLegal(Tp) && Tp.getSizeInBits() <= NumElems * ToSz)
Nadav Rotem614061b2011-08-10 19:30:14 +000015665 StoreType = Tp;
15666 }
15667
Nadav Rotem5cd95e12012-07-11 13:27:05 +000015668 // On 32bit systems, we can't save 64bit integers. Try bitcasting to F64.
15669 if (TLI.isTypeLegal(MVT::f64) && StoreType.getSizeInBits() < 64 &&
15670 (64 <= NumElems * ToSz))
15671 StoreType = MVT::f64;
15672
Nadav Rotem614061b2011-08-10 19:30:14 +000015673 // Bitcast the original vector into a vector of store-size units
15674 EVT StoreVecVT = EVT::getVectorVT(*DAG.getContext(),
Nadav Rotem5cd95e12012-07-11 13:27:05 +000015675 StoreType, VT.getSizeInBits()/StoreType.getSizeInBits());
Nadav Rotem614061b2011-08-10 19:30:14 +000015676 assert(StoreVecVT.getSizeInBits() == VT.getSizeInBits());
15677 SDValue ShuffWide = DAG.getNode(ISD::BITCAST, dl, StoreVecVT, Shuff);
15678 SmallVector<SDValue, 8> Chains;
15679 SDValue Increment = DAG.getConstant(StoreType.getSizeInBits()/8,
15680 TLI.getPointerTy());
15681 SDValue Ptr = St->getBasePtr();
15682
15683 // Perform one or more big stores into memory.
Craig Topper31a207a2012-05-04 06:39:13 +000015684 for (unsigned i=0, e=(ToSz*NumElems)/StoreType.getSizeInBits(); i!=e; ++i) {
Nadav Rotem614061b2011-08-10 19:30:14 +000015685 SDValue SubVec = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl,
15686 StoreType, ShuffWide,
15687 DAG.getIntPtrConstant(i));
15688 SDValue Ch = DAG.getStore(St->getChain(), dl, SubVec, Ptr,
15689 St->getPointerInfo(), St->isVolatile(),
15690 St->isNonTemporal(), St->getAlignment());
15691 Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr, Increment);
15692 Chains.push_back(Ch);
15693 }
15694
15695 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &Chains[0],
15696 Chains.size());
15697 }
15698
15699
Chris Lattner149a4e52008-02-22 02:09:43 +000015700 // Turn load->store of MMX types into GPR load/stores. This avoids clobbering
15701 // the FP state in cases where an emms may be missing.
Dale Johannesen079f2a62008-02-25 19:20:14 +000015702 // A preferable solution to the general problem is to figure out the right
15703 // places to insert EMMS. This qualifies as a quick hack.
Evan Cheng536e6672009-03-12 05:59:15 +000015704
15705 // Similarly, turn load->store of i64 into double load/stores in 32-bit mode.
Evan Cheng536e6672009-03-12 05:59:15 +000015706 if (VT.getSizeInBits() != 64)
15707 return SDValue();
15708
Devang Patel578efa92009-06-05 21:57:13 +000015709 const Function *F = DAG.getMachineFunction().getFunction();
Bill Wendling67658342012-10-09 07:45:08 +000015710 bool NoImplicitFloatOps = F->getFnAttributes().
15711 hasAttribute(Attributes::NoImplicitFloat);
Nick Lewycky8a8d4792011-12-02 22:16:29 +000015712 bool F64IsLegal = !DAG.getTarget().Options.UseSoftFloat && !NoImplicitFloatOps
Craig Topper1accb7e2012-01-10 06:54:16 +000015713 && Subtarget->hasSSE2();
Evan Cheng536e6672009-03-12 05:59:15 +000015714 if ((VT.isVector() ||
Owen Anderson825b72b2009-08-11 20:47:22 +000015715 (VT == MVT::i64 && F64IsLegal && !Subtarget->is64Bit())) &&
Dale Johannesen079f2a62008-02-25 19:20:14 +000015716 isa<LoadSDNode>(St->getValue()) &&
15717 !cast<LoadSDNode>(St->getValue())->isVolatile() &&
15718 St->getChain().hasOneUse() && !St->isVolatile()) {
Gabor Greifba36cb52008-08-28 21:40:38 +000015719 SDNode* LdVal = St->getValue().getNode();
Dale Johannesen079f2a62008-02-25 19:20:14 +000015720 LoadSDNode *Ld = 0;
15721 int TokenFactorIndex = -1;
Dan Gohman475871a2008-07-27 21:46:04 +000015722 SmallVector<SDValue, 8> Ops;
Gabor Greifba36cb52008-08-28 21:40:38 +000015723 SDNode* ChainVal = St->getChain().getNode();
Dale Johannesen079f2a62008-02-25 19:20:14 +000015724 // Must be a store of a load. We currently handle two cases: the load
15725 // is a direct child, and it's under an intervening TokenFactor. It is
15726 // possible to dig deeper under nested TokenFactors.
Dale Johannesen14e2ea92008-02-25 22:29:22 +000015727 if (ChainVal == LdVal)
Dale Johannesen079f2a62008-02-25 19:20:14 +000015728 Ld = cast<LoadSDNode>(St->getChain());
15729 else if (St->getValue().hasOneUse() &&
15730 ChainVal->getOpcode() == ISD::TokenFactor) {
Chad Rosierc2348d52012-02-01 18:45:51 +000015731 for (unsigned i = 0, e = ChainVal->getNumOperands(); i != e; ++i) {
Gabor Greifba36cb52008-08-28 21:40:38 +000015732 if (ChainVal->getOperand(i).getNode() == LdVal) {
Dale Johannesen079f2a62008-02-25 19:20:14 +000015733 TokenFactorIndex = i;
15734 Ld = cast<LoadSDNode>(St->getValue());
15735 } else
15736 Ops.push_back(ChainVal->getOperand(i));
15737 }
15738 }
Dale Johannesen079f2a62008-02-25 19:20:14 +000015739
Evan Cheng536e6672009-03-12 05:59:15 +000015740 if (!Ld || !ISD::isNormalLoad(Ld))
15741 return SDValue();
Dale Johannesen079f2a62008-02-25 19:20:14 +000015742
Evan Cheng536e6672009-03-12 05:59:15 +000015743 // If this is not the MMX case, i.e. we are just turning i64 load/store
15744 // into f64 load/store, avoid the transformation if there are multiple
15745 // uses of the loaded value.
15746 if (!VT.isVector() && !Ld->hasNUsesOfValue(1, 0))
15747 return SDValue();
Dale Johannesen079f2a62008-02-25 19:20:14 +000015748
Evan Cheng536e6672009-03-12 05:59:15 +000015749 DebugLoc LdDL = Ld->getDebugLoc();
15750 DebugLoc StDL = N->getDebugLoc();
15751 // If we are a 64-bit capable x86, lower to a single movq load/store pair.
15752 // Otherwise, if it's legal to use f64 SSE instructions, use f64 load/store
15753 // pair instead.
15754 if (Subtarget->is64Bit() || F64IsLegal) {
Owen Anderson825b72b2009-08-11 20:47:22 +000015755 EVT LdVT = Subtarget->is64Bit() ? MVT::i64 : MVT::f64;
Chris Lattner51abfe42010-09-21 06:02:19 +000015756 SDValue NewLd = DAG.getLoad(LdVT, LdDL, Ld->getChain(), Ld->getBasePtr(),
15757 Ld->getPointerInfo(), Ld->isVolatile(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000015758 Ld->isNonTemporal(), Ld->isInvariant(),
15759 Ld->getAlignment());
Evan Cheng536e6672009-03-12 05:59:15 +000015760 SDValue NewChain = NewLd.getValue(1);
Dale Johannesen079f2a62008-02-25 19:20:14 +000015761 if (TokenFactorIndex != -1) {
Evan Cheng536e6672009-03-12 05:59:15 +000015762 Ops.push_back(NewChain);
Owen Anderson825b72b2009-08-11 20:47:22 +000015763 NewChain = DAG.getNode(ISD::TokenFactor, LdDL, MVT::Other, &Ops[0],
Dale Johannesen079f2a62008-02-25 19:20:14 +000015764 Ops.size());
15765 }
Evan Cheng536e6672009-03-12 05:59:15 +000015766 return DAG.getStore(NewChain, StDL, NewLd, St->getBasePtr(),
Chris Lattner51abfe42010-09-21 06:02:19 +000015767 St->getPointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +000015768 St->isVolatile(), St->isNonTemporal(),
15769 St->getAlignment());
Chris Lattner149a4e52008-02-22 02:09:43 +000015770 }
Evan Cheng536e6672009-03-12 05:59:15 +000015771
15772 // Otherwise, lower to two pairs of 32-bit loads / stores.
15773 SDValue LoAddr = Ld->getBasePtr();
Owen Anderson825b72b2009-08-11 20:47:22 +000015774 SDValue HiAddr = DAG.getNode(ISD::ADD, LdDL, MVT::i32, LoAddr,
15775 DAG.getConstant(4, MVT::i32));
Evan Cheng536e6672009-03-12 05:59:15 +000015776
Owen Anderson825b72b2009-08-11 20:47:22 +000015777 SDValue LoLd = DAG.getLoad(MVT::i32, LdDL, Ld->getChain(), LoAddr,
Chris Lattner51abfe42010-09-21 06:02:19 +000015778 Ld->getPointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +000015779 Ld->isVolatile(), Ld->isNonTemporal(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000015780 Ld->isInvariant(), Ld->getAlignment());
Owen Anderson825b72b2009-08-11 20:47:22 +000015781 SDValue HiLd = DAG.getLoad(MVT::i32, LdDL, Ld->getChain(), HiAddr,
Chris Lattner51abfe42010-09-21 06:02:19 +000015782 Ld->getPointerInfo().getWithOffset(4),
David Greene67c9d422010-02-15 16:53:33 +000015783 Ld->isVolatile(), Ld->isNonTemporal(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000015784 Ld->isInvariant(),
Evan Cheng536e6672009-03-12 05:59:15 +000015785 MinAlign(Ld->getAlignment(), 4));
15786
15787 SDValue NewChain = LoLd.getValue(1);
15788 if (TokenFactorIndex != -1) {
15789 Ops.push_back(LoLd);
15790 Ops.push_back(HiLd);
Owen Anderson825b72b2009-08-11 20:47:22 +000015791 NewChain = DAG.getNode(ISD::TokenFactor, LdDL, MVT::Other, &Ops[0],
Evan Cheng536e6672009-03-12 05:59:15 +000015792 Ops.size());
15793 }
15794
15795 LoAddr = St->getBasePtr();
Owen Anderson825b72b2009-08-11 20:47:22 +000015796 HiAddr = DAG.getNode(ISD::ADD, StDL, MVT::i32, LoAddr,
15797 DAG.getConstant(4, MVT::i32));
Evan Cheng536e6672009-03-12 05:59:15 +000015798
15799 SDValue LoSt = DAG.getStore(NewChain, StDL, LoLd, LoAddr,
Chris Lattner8026a9d2010-09-21 17:50:43 +000015800 St->getPointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +000015801 St->isVolatile(), St->isNonTemporal(),
15802 St->getAlignment());
Evan Cheng536e6672009-03-12 05:59:15 +000015803 SDValue HiSt = DAG.getStore(NewChain, StDL, HiLd, HiAddr,
Chris Lattner8026a9d2010-09-21 17:50:43 +000015804 St->getPointerInfo().getWithOffset(4),
Evan Cheng536e6672009-03-12 05:59:15 +000015805 St->isVolatile(),
David Greene67c9d422010-02-15 16:53:33 +000015806 St->isNonTemporal(),
Evan Cheng536e6672009-03-12 05:59:15 +000015807 MinAlign(St->getAlignment(), 4));
Owen Anderson825b72b2009-08-11 20:47:22 +000015808 return DAG.getNode(ISD::TokenFactor, StDL, MVT::Other, LoSt, HiSt);
Chris Lattner149a4e52008-02-22 02:09:43 +000015809 }
Dan Gohman475871a2008-07-27 21:46:04 +000015810 return SDValue();
Chris Lattner149a4e52008-02-22 02:09:43 +000015811}
15812
Duncan Sands17470be2011-09-22 20:15:48 +000015813/// isHorizontalBinOp - Return 'true' if this vector operation is "horizontal"
15814/// and return the operands for the horizontal operation in LHS and RHS. A
15815/// horizontal operation performs the binary operation on successive elements
15816/// of its first operand, then on successive elements of its second operand,
15817/// returning the resulting values in a vector. For example, if
15818/// A = < float a0, float a1, float a2, float a3 >
15819/// and
15820/// B = < float b0, float b1, float b2, float b3 >
15821/// then the result of doing a horizontal operation on A and B is
15822/// A horizontal-op B = < a0 op a1, a2 op a3, b0 op b1, b2 op b3 >.
15823/// In short, LHS and RHS are inspected to see if LHS op RHS is of the form
15824/// A horizontal-op B, for some already available A and B, and if so then LHS is
15825/// set to A, RHS to B, and the routine returns 'true'.
15826/// Note that the binary operation should have the property that if one of the
15827/// operands is UNDEF then the result is UNDEF.
Craig Topperbeabc6c2011-12-05 06:56:46 +000015828static bool isHorizontalBinOp(SDValue &LHS, SDValue &RHS, bool IsCommutative) {
Duncan Sands17470be2011-09-22 20:15:48 +000015829 // Look for the following pattern: if
15830 // A = < float a0, float a1, float a2, float a3 >
15831 // B = < float b0, float b1, float b2, float b3 >
15832 // and
15833 // LHS = VECTOR_SHUFFLE A, B, <0, 2, 4, 6>
15834 // RHS = VECTOR_SHUFFLE A, B, <1, 3, 5, 7>
15835 // then LHS op RHS = < a0 op a1, a2 op a3, b0 op b1, b2 op b3 >
15836 // which is A horizontal-op B.
15837
15838 // At least one of the operands should be a vector shuffle.
15839 if (LHS.getOpcode() != ISD::VECTOR_SHUFFLE &&
15840 RHS.getOpcode() != ISD::VECTOR_SHUFFLE)
15841 return false;
15842
15843 EVT VT = LHS.getValueType();
Craig Topperf8363302011-12-02 08:18:41 +000015844
15845 assert((VT.is128BitVector() || VT.is256BitVector()) &&
15846 "Unsupported vector type for horizontal add/sub");
15847
15848 // Handle 128 and 256-bit vector lengths. AVX defines horizontal add/sub to
15849 // operate independently on 128-bit lanes.
Craig Topperb72039c2011-11-30 09:10:50 +000015850 unsigned NumElts = VT.getVectorNumElements();
15851 unsigned NumLanes = VT.getSizeInBits()/128;
15852 unsigned NumLaneElts = NumElts / NumLanes;
Craig Topperf8363302011-12-02 08:18:41 +000015853 assert((NumLaneElts % 2 == 0) &&
15854 "Vector type should have an even number of elements in each lane");
15855 unsigned HalfLaneElts = NumLaneElts/2;
Duncan Sands17470be2011-09-22 20:15:48 +000015856
15857 // View LHS in the form
15858 // LHS = VECTOR_SHUFFLE A, B, LMask
15859 // If LHS is not a shuffle then pretend it is the shuffle
15860 // LHS = VECTOR_SHUFFLE LHS, undef, <0, 1, ..., N-1>
15861 // NOTE: in what follows a default initialized SDValue represents an UNDEF of
15862 // type VT.
15863 SDValue A, B;
Craig Topperb72039c2011-11-30 09:10:50 +000015864 SmallVector<int, 16> LMask(NumElts);
Duncan Sands17470be2011-09-22 20:15:48 +000015865 if (LHS.getOpcode() == ISD::VECTOR_SHUFFLE) {
15866 if (LHS.getOperand(0).getOpcode() != ISD::UNDEF)
15867 A = LHS.getOperand(0);
15868 if (LHS.getOperand(1).getOpcode() != ISD::UNDEF)
15869 B = LHS.getOperand(1);
Benjamin Kramered4c8c62012-01-15 13:16:05 +000015870 ArrayRef<int> Mask = cast<ShuffleVectorSDNode>(LHS.getNode())->getMask();
15871 std::copy(Mask.begin(), Mask.end(), LMask.begin());
Duncan Sands17470be2011-09-22 20:15:48 +000015872 } else {
15873 if (LHS.getOpcode() != ISD::UNDEF)
15874 A = LHS;
Craig Topperb72039c2011-11-30 09:10:50 +000015875 for (unsigned i = 0; i != NumElts; ++i)
Duncan Sands17470be2011-09-22 20:15:48 +000015876 LMask[i] = i;
15877 }
15878
15879 // Likewise, view RHS in the form
15880 // RHS = VECTOR_SHUFFLE C, D, RMask
15881 SDValue C, D;
Craig Topperb72039c2011-11-30 09:10:50 +000015882 SmallVector<int, 16> RMask(NumElts);
Duncan Sands17470be2011-09-22 20:15:48 +000015883 if (RHS.getOpcode() == ISD::VECTOR_SHUFFLE) {
15884 if (RHS.getOperand(0).getOpcode() != ISD::UNDEF)
15885 C = RHS.getOperand(0);
15886 if (RHS.getOperand(1).getOpcode() != ISD::UNDEF)
15887 D = RHS.getOperand(1);
Benjamin Kramered4c8c62012-01-15 13:16:05 +000015888 ArrayRef<int> Mask = cast<ShuffleVectorSDNode>(RHS.getNode())->getMask();
15889 std::copy(Mask.begin(), Mask.end(), RMask.begin());
Duncan Sands17470be2011-09-22 20:15:48 +000015890 } else {
15891 if (RHS.getOpcode() != ISD::UNDEF)
15892 C = RHS;
Craig Topperb72039c2011-11-30 09:10:50 +000015893 for (unsigned i = 0; i != NumElts; ++i)
Duncan Sands17470be2011-09-22 20:15:48 +000015894 RMask[i] = i;
15895 }
15896
15897 // Check that the shuffles are both shuffling the same vectors.
15898 if (!(A == C && B == D) && !(A == D && B == C))
15899 return false;
15900
15901 // If everything is UNDEF then bail out: it would be better to fold to UNDEF.
15902 if (!A.getNode() && !B.getNode())
15903 return false;
15904
15905 // If A and B occur in reverse order in RHS, then "swap" them (which means
15906 // rewriting the mask).
15907 if (A != C)
Craig Topperbeabc6c2011-12-05 06:56:46 +000015908 CommuteVectorShuffleMask(RMask, NumElts);
Duncan Sands17470be2011-09-22 20:15:48 +000015909
15910 // At this point LHS and RHS are equivalent to
15911 // LHS = VECTOR_SHUFFLE A, B, LMask
15912 // RHS = VECTOR_SHUFFLE A, B, RMask
15913 // Check that the masks correspond to performing a horizontal operation.
Craig Topperf8363302011-12-02 08:18:41 +000015914 for (unsigned i = 0; i != NumElts; ++i) {
Craig Topperbeabc6c2011-12-05 06:56:46 +000015915 int LIdx = LMask[i], RIdx = RMask[i];
Duncan Sands17470be2011-09-22 20:15:48 +000015916
Craig Topperf8363302011-12-02 08:18:41 +000015917 // Ignore any UNDEF components.
Craig Topperbeabc6c2011-12-05 06:56:46 +000015918 if (LIdx < 0 || RIdx < 0 ||
15919 (!A.getNode() && (LIdx < (int)NumElts || RIdx < (int)NumElts)) ||
15920 (!B.getNode() && (LIdx >= (int)NumElts || RIdx >= (int)NumElts)))
Craig Topperf8363302011-12-02 08:18:41 +000015921 continue;
Duncan Sands17470be2011-09-22 20:15:48 +000015922
Craig Topperf8363302011-12-02 08:18:41 +000015923 // Check that successive elements are being operated on. If not, this is
15924 // not a horizontal operation.
15925 unsigned Src = (i/HalfLaneElts) % 2; // each lane is split between srcs
15926 unsigned LaneStart = (i/NumLaneElts) * NumLaneElts;
Craig Topperbeabc6c2011-12-05 06:56:46 +000015927 int Index = 2*(i%HalfLaneElts) + NumElts*Src + LaneStart;
Craig Topperf8363302011-12-02 08:18:41 +000015928 if (!(LIdx == Index && RIdx == Index + 1) &&
Craig Topperbeabc6c2011-12-05 06:56:46 +000015929 !(IsCommutative && LIdx == Index + 1 && RIdx == Index))
Craig Topperf8363302011-12-02 08:18:41 +000015930 return false;
Duncan Sands17470be2011-09-22 20:15:48 +000015931 }
15932
15933 LHS = A.getNode() ? A : B; // If A is 'UNDEF', use B for it.
15934 RHS = B.getNode() ? B : A; // If B is 'UNDEF', use A for it.
15935 return true;
15936}
15937
15938/// PerformFADDCombine - Do target-specific dag combines on floating point adds.
15939static SDValue PerformFADDCombine(SDNode *N, SelectionDAG &DAG,
15940 const X86Subtarget *Subtarget) {
15941 EVT VT = N->getValueType(0);
15942 SDValue LHS = N->getOperand(0);
15943 SDValue RHS = N->getOperand(1);
15944
15945 // Try to synthesize horizontal adds from adds of shuffles.
Craig Topperd0a31172012-01-10 06:37:29 +000015946 if (((Subtarget->hasSSE3() && (VT == MVT::v4f32 || VT == MVT::v2f64)) ||
Craig Topper138a5c62011-12-02 07:16:01 +000015947 (Subtarget->hasAVX() && (VT == MVT::v8f32 || VT == MVT::v4f64))) &&
Duncan Sands17470be2011-09-22 20:15:48 +000015948 isHorizontalBinOp(LHS, RHS, true))
15949 return DAG.getNode(X86ISD::FHADD, N->getDebugLoc(), VT, LHS, RHS);
15950 return SDValue();
15951}
15952
15953/// PerformFSUBCombine - Do target-specific dag combines on floating point subs.
15954static SDValue PerformFSUBCombine(SDNode *N, SelectionDAG &DAG,
15955 const X86Subtarget *Subtarget) {
15956 EVT VT = N->getValueType(0);
15957 SDValue LHS = N->getOperand(0);
15958 SDValue RHS = N->getOperand(1);
15959
15960 // Try to synthesize horizontal subs from subs of shuffles.
Craig Topperd0a31172012-01-10 06:37:29 +000015961 if (((Subtarget->hasSSE3() && (VT == MVT::v4f32 || VT == MVT::v2f64)) ||
Craig Topper138a5c62011-12-02 07:16:01 +000015962 (Subtarget->hasAVX() && (VT == MVT::v8f32 || VT == MVT::v4f64))) &&
Duncan Sands17470be2011-09-22 20:15:48 +000015963 isHorizontalBinOp(LHS, RHS, false))
15964 return DAG.getNode(X86ISD::FHSUB, N->getDebugLoc(), VT, LHS, RHS);
15965 return SDValue();
15966}
15967
Chris Lattner6cf73262008-01-25 06:14:17 +000015968/// PerformFORCombine - Do target-specific dag combines on X86ISD::FOR and
15969/// X86ISD::FXOR nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000015970static SDValue PerformFORCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattner6cf73262008-01-25 06:14:17 +000015971 assert(N->getOpcode() == X86ISD::FOR || N->getOpcode() == X86ISD::FXOR);
15972 // F[X]OR(0.0, x) -> x
15973 // F[X]OR(x, 0.0) -> x
Chris Lattneraf723b92008-01-25 05:46:26 +000015974 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
15975 if (C->getValueAPF().isPosZero())
15976 return N->getOperand(1);
15977 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
15978 if (C->getValueAPF().isPosZero())
15979 return N->getOperand(0);
Dan Gohman475871a2008-07-27 21:46:04 +000015980 return SDValue();
Chris Lattneraf723b92008-01-25 05:46:26 +000015981}
15982
Nadav Rotemd60cb112012-08-19 13:06:16 +000015983/// PerformFMinFMaxCombine - Do target-specific dag combines on X86ISD::FMIN and
15984/// X86ISD::FMAX nodes.
15985static SDValue PerformFMinFMaxCombine(SDNode *N, SelectionDAG &DAG) {
15986 assert(N->getOpcode() == X86ISD::FMIN || N->getOpcode() == X86ISD::FMAX);
15987
15988 // Only perform optimizations if UnsafeMath is used.
15989 if (!DAG.getTarget().Options.UnsafeFPMath)
15990 return SDValue();
15991
15992 // If we run in unsafe-math mode, then convert the FMAX and FMIN nodes
Craig Topper8365e9b2012-09-01 06:33:50 +000015993 // into FMINC and FMAXC, which are Commutative operations.
Nadav Rotemd60cb112012-08-19 13:06:16 +000015994 unsigned NewOp = 0;
15995 switch (N->getOpcode()) {
15996 default: llvm_unreachable("unknown opcode");
15997 case X86ISD::FMIN: NewOp = X86ISD::FMINC; break;
15998 case X86ISD::FMAX: NewOp = X86ISD::FMAXC; break;
15999 }
16000
16001 return DAG.getNode(NewOp, N->getDebugLoc(), N->getValueType(0),
16002 N->getOperand(0), N->getOperand(1));
16003}
16004
16005
Chris Lattneraf723b92008-01-25 05:46:26 +000016006/// PerformFANDCombine - Do target-specific dag combines on X86ISD::FAND nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000016007static SDValue PerformFANDCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattneraf723b92008-01-25 05:46:26 +000016008 // FAND(0.0, x) -> 0.0
16009 // FAND(x, 0.0) -> 0.0
16010 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
16011 if (C->getValueAPF().isPosZero())
16012 return N->getOperand(0);
16013 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
16014 if (C->getValueAPF().isPosZero())
16015 return N->getOperand(1);
Dan Gohman475871a2008-07-27 21:46:04 +000016016 return SDValue();
Chris Lattneraf723b92008-01-25 05:46:26 +000016017}
16018
Dan Gohmane5af2d32009-01-29 01:59:02 +000016019static SDValue PerformBTCombine(SDNode *N,
16020 SelectionDAG &DAG,
16021 TargetLowering::DAGCombinerInfo &DCI) {
16022 // BT ignores high bits in the bit index operand.
16023 SDValue Op1 = N->getOperand(1);
16024 if (Op1.hasOneUse()) {
16025 unsigned BitWidth = Op1.getValueSizeInBits();
16026 APInt DemandedMask = APInt::getLowBitsSet(BitWidth, Log2_32(BitWidth));
16027 APInt KnownZero, KnownOne;
Evan Chenge5b51ac2010-04-17 06:13:15 +000016028 TargetLowering::TargetLoweringOpt TLO(DAG, !DCI.isBeforeLegalize(),
16029 !DCI.isBeforeLegalizeOps());
Dan Gohmand858e902010-04-17 15:26:15 +000016030 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Dan Gohmane5af2d32009-01-29 01:59:02 +000016031 if (TLO.ShrinkDemandedConstant(Op1, DemandedMask) ||
16032 TLI.SimplifyDemandedBits(Op1, DemandedMask, KnownZero, KnownOne, TLO))
16033 DCI.CommitTargetLoweringOpt(TLO);
16034 }
16035 return SDValue();
16036}
Chris Lattner83e6c992006-10-04 06:57:07 +000016037
Eli Friedman7a5e5552009-06-07 06:52:44 +000016038static SDValue PerformVZEXT_MOVLCombine(SDNode *N, SelectionDAG &DAG) {
16039 SDValue Op = N->getOperand(0);
Wesley Peckbf17cfa2010-11-23 03:31:01 +000016040 if (Op.getOpcode() == ISD::BITCAST)
Eli Friedman7a5e5552009-06-07 06:52:44 +000016041 Op = Op.getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +000016042 EVT VT = N->getValueType(0), OpVT = Op.getValueType();
Eli Friedman7a5e5552009-06-07 06:52:44 +000016043 if (Op.getOpcode() == X86ISD::VZEXT_LOAD &&
Eric Christopherfd179292009-08-27 18:07:15 +000016044 VT.getVectorElementType().getSizeInBits() ==
Eli Friedman7a5e5552009-06-07 06:52:44 +000016045 OpVT.getVectorElementType().getSizeInBits()) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +000016046 return DAG.getNode(ISD::BITCAST, N->getDebugLoc(), VT, Op);
Eli Friedman7a5e5552009-06-07 06:52:44 +000016047 }
16048 return SDValue();
16049}
16050
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +000016051static SDValue PerformSExtCombine(SDNode *N, SelectionDAG &DAG,
16052 TargetLowering::DAGCombinerInfo &DCI,
16053 const X86Subtarget *Subtarget) {
16054 if (!DCI.isBeforeLegalizeOps())
16055 return SDValue();
16056
Craig Topper3ef43cf2012-04-24 06:36:35 +000016057 if (!Subtarget->hasAVX())
Elena Demikhovskyf6020402012-02-08 08:37:26 +000016058 return SDValue();
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +000016059
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +000016060 EVT VT = N->getValueType(0);
16061 SDValue Op = N->getOperand(0);
16062 EVT OpVT = Op.getValueType();
16063 DebugLoc dl = N->getDebugLoc();
16064
Elena Demikhovskyf6020402012-02-08 08:37:26 +000016065 if ((VT == MVT::v4i64 && OpVT == MVT::v4i32) ||
16066 (VT == MVT::v8i32 && OpVT == MVT::v8i16)) {
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +000016067
Craig Topper3ef43cf2012-04-24 06:36:35 +000016068 if (Subtarget->hasAVX2())
Elena Demikhovsky1da58672012-04-22 09:39:03 +000016069 return DAG.getNode(X86ISD::VSEXT_MOVL, dl, VT, Op);
Elena Demikhovsky1da58672012-04-22 09:39:03 +000016070
16071 // Optimize vectors in AVX mode
16072 // Sign extend v8i16 to v8i32 and
16073 // v4i32 to v4i64
16074 //
16075 // Divide input vector into two parts
16076 // for v4i32 the shuffle mask will be { 0, 1, -1, -1} {2, 3, -1, -1}
16077 // use vpmovsx instruction to extend v4i32 -> v2i64; v8i16 -> v4i32
16078 // concat the vectors to original VT
16079
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +000016080 unsigned NumElems = OpVT.getVectorNumElements();
Craig Toppercacafd42012-08-14 08:18:43 +000016081 SDValue Undef = DAG.getUNDEF(OpVT);
16082
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +000016083 SmallVector<int,8> ShufMask1(NumElems, -1);
Craig Topper3ef43cf2012-04-24 06:36:35 +000016084 for (unsigned i = 0; i != NumElems/2; ++i)
16085 ShufMask1[i] = i;
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +000016086
Craig Toppercacafd42012-08-14 08:18:43 +000016087 SDValue OpLo = DAG.getVectorShuffle(OpVT, dl, Op, Undef, &ShufMask1[0]);
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +000016088
16089 SmallVector<int,8> ShufMask2(NumElems, -1);
Craig Topper3ef43cf2012-04-24 06:36:35 +000016090 for (unsigned i = 0; i != NumElems/2; ++i)
16091 ShufMask2[i] = i + NumElems/2;
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +000016092
Craig Toppercacafd42012-08-14 08:18:43 +000016093 SDValue OpHi = DAG.getVectorShuffle(OpVT, dl, Op, Undef, &ShufMask2[0]);
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +000016094
Craig Topper3ef43cf2012-04-24 06:36:35 +000016095 EVT HalfVT = EVT::getVectorVT(*DAG.getContext(), VT.getScalarType(),
Elena Demikhovskyf6020402012-02-08 08:37:26 +000016096 VT.getVectorNumElements()/2);
16097
Craig Topper3ef43cf2012-04-24 06:36:35 +000016098 OpLo = DAG.getNode(X86ISD::VSEXT_MOVL, dl, HalfVT, OpLo);
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +000016099 OpHi = DAG.getNode(X86ISD::VSEXT_MOVL, dl, HalfVT, OpHi);
16100
16101 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT, OpLo, OpHi);
16102 }
16103 return SDValue();
16104}
16105
Michael Liaof6c24ee2012-08-10 14:39:24 +000016106static SDValue PerformFMACombine(SDNode *N, SelectionDAG &DAG,
Elena Demikhovsky1503aba2012-08-01 12:06:00 +000016107 const X86Subtarget* Subtarget) {
16108 DebugLoc dl = N->getDebugLoc();
16109 EVT VT = N->getValueType(0);
16110
Craig Topperb1bdd7d2012-08-30 06:56:15 +000016111 // Let legalize expand this if it isn't a legal type yet.
16112 if (!DAG.getTargetLoweringInfo().isTypeLegal(VT))
16113 return SDValue();
16114
Elena Demikhovsky1503aba2012-08-01 12:06:00 +000016115 EVT ScalarVT = VT.getScalarType();
Craig Topperbf404372012-08-31 15:40:30 +000016116 if ((ScalarVT != MVT::f32 && ScalarVT != MVT::f64) ||
16117 (!Subtarget->hasFMA() && !Subtarget->hasFMA4()))
Elena Demikhovsky1503aba2012-08-01 12:06:00 +000016118 return SDValue();
16119
16120 SDValue A = N->getOperand(0);
16121 SDValue B = N->getOperand(1);
16122 SDValue C = N->getOperand(2);
16123
16124 bool NegA = (A.getOpcode() == ISD::FNEG);
16125 bool NegB = (B.getOpcode() == ISD::FNEG);
16126 bool NegC = (C.getOpcode() == ISD::FNEG);
16127
Michael Liaof6c24ee2012-08-10 14:39:24 +000016128 // Negative multiplication when NegA xor NegB
16129 bool NegMul = (NegA != NegB);
Elena Demikhovsky1503aba2012-08-01 12:06:00 +000016130 if (NegA)
16131 A = A.getOperand(0);
16132 if (NegB)
16133 B = B.getOperand(0);
16134 if (NegC)
16135 C = C.getOperand(0);
16136
16137 unsigned Opcode;
16138 if (!NegMul)
Craig Topperbf404372012-08-31 15:40:30 +000016139 Opcode = (!NegC) ? X86ISD::FMADD : X86ISD::FMSUB;
Elena Demikhovsky1503aba2012-08-01 12:06:00 +000016140 else
Craig Topperbf404372012-08-31 15:40:30 +000016141 Opcode = (!NegC) ? X86ISD::FNMADD : X86ISD::FNMSUB;
16142
Elena Demikhovsky1503aba2012-08-01 12:06:00 +000016143 return DAG.getNode(Opcode, dl, VT, A, B, C);
16144}
16145
Elena Demikhovsky28d7e712012-01-24 13:54:13 +000016146static SDValue PerformZExtCombine(SDNode *N, SelectionDAG &DAG,
Craig Topperc16f8512012-04-25 06:39:39 +000016147 TargetLowering::DAGCombinerInfo &DCI,
Elena Demikhovsky28d7e712012-01-24 13:54:13 +000016148 const X86Subtarget *Subtarget) {
Evan Cheng2e489c42009-12-16 00:53:11 +000016149 // (i32 zext (and (i8 x86isd::setcc_carry), 1)) ->
16150 // (and (i32 x86isd::setcc_carry), 1)
16151 // This eliminates the zext. This transformation is necessary because
16152 // ISD::SETCC is always legalized to i8.
16153 DebugLoc dl = N->getDebugLoc();
16154 SDValue N0 = N->getOperand(0);
16155 EVT VT = N->getValueType(0);
Elena Demikhovsky28d7e712012-01-24 13:54:13 +000016156 EVT OpVT = N0.getValueType();
16157
Evan Cheng2e489c42009-12-16 00:53:11 +000016158 if (N0.getOpcode() == ISD::AND &&
16159 N0.hasOneUse() &&
16160 N0.getOperand(0).hasOneUse()) {
16161 SDValue N00 = N0.getOperand(0);
16162 if (N00.getOpcode() != X86ISD::SETCC_CARRY)
16163 return SDValue();
16164 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
16165 if (!C || C->getZExtValue() != 1)
16166 return SDValue();
16167 return DAG.getNode(ISD::AND, dl, VT,
16168 DAG.getNode(X86ISD::SETCC_CARRY, dl, VT,
16169 N00.getOperand(0), N00.getOperand(1)),
16170 DAG.getConstant(1, VT));
16171 }
Craig Topperd0cf5652012-04-21 18:13:35 +000016172
Elena Demikhovsky28d7e712012-01-24 13:54:13 +000016173 // Optimize vectors in AVX mode:
16174 //
16175 // v8i16 -> v8i32
16176 // Use vpunpcklwd for 4 lower elements v8i16 -> v4i32.
16177 // Use vpunpckhwd for 4 upper elements v8i16 -> v4i32.
16178 // Concat upper and lower parts.
16179 //
16180 // v4i32 -> v4i64
16181 // Use vpunpckldq for 4 lower elements v4i32 -> v2i64.
16182 // Use vpunpckhdq for 4 upper elements v4i32 -> v2i64.
16183 // Concat upper and lower parts.
16184 //
Craig Topperc16f8512012-04-25 06:39:39 +000016185 if (!DCI.isBeforeLegalizeOps())
16186 return SDValue();
Elena Demikhovsky28d7e712012-01-24 13:54:13 +000016187
Craig Topperc16f8512012-04-25 06:39:39 +000016188 if (!Subtarget->hasAVX())
16189 return SDValue();
Elena Demikhovsky28d7e712012-01-24 13:54:13 +000016190
Craig Topperc16f8512012-04-25 06:39:39 +000016191 if (((VT == MVT::v8i32) && (OpVT == MVT::v8i16)) ||
16192 ((VT == MVT::v4i64) && (OpVT == MVT::v4i32))) {
Elena Demikhovsky1da58672012-04-22 09:39:03 +000016193
Craig Topperc16f8512012-04-25 06:39:39 +000016194 if (Subtarget->hasAVX2())
16195 return DAG.getNode(X86ISD::VZEXT_MOVL, dl, VT, N0);
Elena Demikhovsky28d7e712012-01-24 13:54:13 +000016196
Craig Topperc16f8512012-04-25 06:39:39 +000016197 SDValue ZeroVec = getZeroVector(OpVT, Subtarget, DAG, dl);
16198 SDValue OpLo = getUnpackl(DAG, dl, OpVT, N0, ZeroVec);
16199 SDValue OpHi = getUnpackh(DAG, dl, OpVT, N0, ZeroVec);
Elena Demikhovsky28d7e712012-01-24 13:54:13 +000016200
Craig Topperc16f8512012-04-25 06:39:39 +000016201 EVT HVT = EVT::getVectorVT(*DAG.getContext(), VT.getVectorElementType(),
16202 VT.getVectorNumElements()/2);
Elena Demikhovsky28d7e712012-01-24 13:54:13 +000016203
Craig Topperc16f8512012-04-25 06:39:39 +000016204 OpLo = DAG.getNode(ISD::BITCAST, dl, HVT, OpLo);
16205 OpHi = DAG.getNode(ISD::BITCAST, dl, HVT, OpHi);
16206
16207 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT, OpLo, OpHi);
Elena Demikhovsky28d7e712012-01-24 13:54:13 +000016208 }
16209
Evan Cheng2e489c42009-12-16 00:53:11 +000016210 return SDValue();
16211}
16212
Chad Rosiera73b6fc2012-04-27 22:33:25 +000016213// Optimize x == -y --> x+y == 0
16214// x != -y --> x+y != 0
16215static SDValue PerformISDSETCCCombine(SDNode *N, SelectionDAG &DAG) {
16216 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(2))->get();
16217 SDValue LHS = N->getOperand(0);
Chad Rosiera20e1e72012-08-01 18:39:17 +000016218 SDValue RHS = N->getOperand(1);
Chad Rosiera73b6fc2012-04-27 22:33:25 +000016219
16220 if ((CC == ISD::SETNE || CC == ISD::SETEQ) && LHS.getOpcode() == ISD::SUB)
16221 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(LHS.getOperand(0)))
16222 if (C->getAPIntValue() == 0 && LHS.hasOneUse()) {
16223 SDValue addV = DAG.getNode(ISD::ADD, N->getDebugLoc(),
16224 LHS.getValueType(), RHS, LHS.getOperand(1));
16225 return DAG.getSetCC(N->getDebugLoc(), N->getValueType(0),
16226 addV, DAG.getConstant(0, addV.getValueType()), CC);
16227 }
16228 if ((CC == ISD::SETNE || CC == ISD::SETEQ) && RHS.getOpcode() == ISD::SUB)
16229 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(RHS.getOperand(0)))
16230 if (C->getAPIntValue() == 0 && RHS.hasOneUse()) {
16231 SDValue addV = DAG.getNode(ISD::ADD, N->getDebugLoc(),
16232 RHS.getValueType(), LHS, RHS.getOperand(1));
16233 return DAG.getSetCC(N->getDebugLoc(), N->getValueType(0),
16234 addV, DAG.getConstant(0, addV.getValueType()), CC);
16235 }
16236 return SDValue();
16237}
16238
Chris Lattnerc19d1c32010-12-19 22:08:31 +000016239// Optimize RES = X86ISD::SETCC CONDCODE, EFLAG_INPUT
Michael Liaodbf8b5b2012-08-28 03:34:40 +000016240static SDValue PerformSETCCCombine(SDNode *N, SelectionDAG &DAG,
16241 TargetLowering::DAGCombinerInfo &DCI,
16242 const X86Subtarget *Subtarget) {
Chris Lattnerc19d1c32010-12-19 22:08:31 +000016243 DebugLoc DL = N->getDebugLoc();
Michael Liao2a33cec2012-08-10 19:58:13 +000016244 X86::CondCode CC = X86::CondCode(N->getConstantOperandVal(0));
16245 SDValue EFLAGS = N->getOperand(1);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000016246
Chris Lattnerc19d1c32010-12-19 22:08:31 +000016247 // Materialize "setb reg" as "sbb reg,reg", since it can be extended without
16248 // a zext and produces an all-ones bit which is more useful than 0/1 in some
16249 // cases.
Michael Liao2a33cec2012-08-10 19:58:13 +000016250 if (CC == X86::COND_B)
Chris Lattnerc19d1c32010-12-19 22:08:31 +000016251 return DAG.getNode(ISD::AND, DL, MVT::i8,
16252 DAG.getNode(X86ISD::SETCC_CARRY, DL, MVT::i8,
Michael Liao2a33cec2012-08-10 19:58:13 +000016253 DAG.getConstant(CC, MVT::i8), EFLAGS),
Chris Lattnerc19d1c32010-12-19 22:08:31 +000016254 DAG.getConstant(1, MVT::i8));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000016255
Michael Liao2a33cec2012-08-10 19:58:13 +000016256 SDValue Flags;
16257
Michael Liaodbf8b5b2012-08-28 03:34:40 +000016258 Flags = checkBoolTestSetCCCombine(EFLAGS, CC);
16259 if (Flags.getNode()) {
16260 SDValue Cond = DAG.getConstant(CC, MVT::i8);
16261 return DAG.getNode(X86ISD::SETCC, DL, N->getVTList(), Cond, Flags);
16262 }
16263
Michael Liao2a33cec2012-08-10 19:58:13 +000016264 return SDValue();
16265}
16266
16267// Optimize branch condition evaluation.
16268//
16269static SDValue PerformBrCondCombine(SDNode *N, SelectionDAG &DAG,
16270 TargetLowering::DAGCombinerInfo &DCI,
16271 const X86Subtarget *Subtarget) {
16272 DebugLoc DL = N->getDebugLoc();
16273 SDValue Chain = N->getOperand(0);
16274 SDValue Dest = N->getOperand(1);
16275 SDValue EFLAGS = N->getOperand(3);
16276 X86::CondCode CC = X86::CondCode(N->getConstantOperandVal(2));
16277
16278 SDValue Flags;
16279
Michael Liaodbf8b5b2012-08-28 03:34:40 +000016280 Flags = checkBoolTestSetCCCombine(EFLAGS, CC);
16281 if (Flags.getNode()) {
16282 SDValue Cond = DAG.getConstant(CC, MVT::i8);
16283 return DAG.getNode(X86ISD::BRCOND, DL, N->getVTList(), Chain, Dest, Cond,
16284 Flags);
16285 }
16286
Chris Lattnerc19d1c32010-12-19 22:08:31 +000016287 return SDValue();
16288}
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000016289
Craig Topper7fd5e162012-04-24 06:02:29 +000016290static SDValue PerformUINT_TO_FPCombine(SDNode *N, SelectionDAG &DAG) {
Nadav Rotema3540772012-04-23 21:53:37 +000016291 SDValue Op0 = N->getOperand(0);
16292 EVT InVT = Op0->getValueType(0);
Nadav Rotema3540772012-04-23 21:53:37 +000016293
16294 // UINT_TO_FP(v4i8) -> SINT_TO_FP(ZEXT(v4i8 to v4i32))
Craig Topper7fd5e162012-04-24 06:02:29 +000016295 if (InVT == MVT::v8i8 || InVT == MVT::v4i8) {
Nadav Rotema3540772012-04-23 21:53:37 +000016296 DebugLoc dl = N->getDebugLoc();
Craig Topper7fd5e162012-04-24 06:02:29 +000016297 MVT DstVT = InVT == MVT::v4i8 ? MVT::v4i32 : MVT::v8i32;
Nadav Rotema3540772012-04-23 21:53:37 +000016298 SDValue P = DAG.getNode(ISD::ZERO_EXTEND, dl, DstVT, Op0);
16299 // Notice that we use SINT_TO_FP because we know that the high bits
16300 // are zero and SINT_TO_FP is better supported by the hardware.
16301 return DAG.getNode(ISD::SINT_TO_FP, dl, N->getValueType(0), P);
16302 }
16303
16304 return SDValue();
16305}
16306
Benjamin Kramer1396c402011-06-18 11:09:41 +000016307static SDValue PerformSINT_TO_FPCombine(SDNode *N, SelectionDAG &DAG,
16308 const X86TargetLowering *XTLI) {
Stuart Hastingsf99a4b82011-06-06 23:15:58 +000016309 SDValue Op0 = N->getOperand(0);
Nadav Rotema3540772012-04-23 21:53:37 +000016310 EVT InVT = Op0->getValueType(0);
Nadav Rotema3540772012-04-23 21:53:37 +000016311
16312 // SINT_TO_FP(v4i8) -> SINT_TO_FP(SEXT(v4i8 to v4i32))
Craig Topper7fd5e162012-04-24 06:02:29 +000016313 if (InVT == MVT::v8i8 || InVT == MVT::v4i8) {
Nadav Rotema3540772012-04-23 21:53:37 +000016314 DebugLoc dl = N->getDebugLoc();
Craig Topper7fd5e162012-04-24 06:02:29 +000016315 MVT DstVT = InVT == MVT::v4i8 ? MVT::v4i32 : MVT::v8i32;
Nadav Rotema3540772012-04-23 21:53:37 +000016316 SDValue P = DAG.getNode(ISD::SIGN_EXTEND, dl, DstVT, Op0);
16317 return DAG.getNode(ISD::SINT_TO_FP, dl, N->getValueType(0), P);
16318 }
16319
Stuart Hastingsf99a4b82011-06-06 23:15:58 +000016320 // Transform (SINT_TO_FP (i64 ...)) into an x87 operation if we have
16321 // a 32-bit target where SSE doesn't support i64->FP operations.
16322 if (Op0.getOpcode() == ISD::LOAD) {
16323 LoadSDNode *Ld = cast<LoadSDNode>(Op0.getNode());
16324 EVT VT = Ld->getValueType(0);
16325 if (!Ld->isVolatile() && !N->getValueType(0).isVector() &&
16326 ISD::isNON_EXTLoad(Op0.getNode()) && Op0.hasOneUse() &&
16327 !XTLI->getSubtarget()->is64Bit() &&
16328 !DAG.getTargetLoweringInfo().isTypeLegal(VT)) {
Benjamin Kramer1396c402011-06-18 11:09:41 +000016329 SDValue FILDChain = XTLI->BuildFILD(SDValue(N, 0), Ld->getValueType(0),
16330 Ld->getChain(), Op0, DAG);
Stuart Hastingsf99a4b82011-06-06 23:15:58 +000016331 DAG.ReplaceAllUsesOfValueWith(Op0.getValue(1), FILDChain.getValue(1));
16332 return FILDChain;
16333 }
16334 }
16335 return SDValue();
16336}
16337
Chris Lattner23a01992010-12-20 01:37:09 +000016338// Optimize RES, EFLAGS = X86ISD::ADC LHS, RHS, EFLAGS
16339static SDValue PerformADCCombine(SDNode *N, SelectionDAG &DAG,
16340 X86TargetLowering::DAGCombinerInfo &DCI) {
16341 // If the LHS and RHS of the ADC node are zero, then it can't overflow and
16342 // the result is either zero or one (depending on the input carry bit).
16343 // Strength reduce this down to a "set on carry" aka SETCC_CARRY&1.
16344 if (X86::isZeroNode(N->getOperand(0)) &&
16345 X86::isZeroNode(N->getOperand(1)) &&
16346 // We don't have a good way to replace an EFLAGS use, so only do this when
16347 // dead right now.
16348 SDValue(N, 1).use_empty()) {
16349 DebugLoc DL = N->getDebugLoc();
16350 EVT VT = N->getValueType(0);
16351 SDValue CarryOut = DAG.getConstant(0, N->getValueType(1));
16352 SDValue Res1 = DAG.getNode(ISD::AND, DL, VT,
16353 DAG.getNode(X86ISD::SETCC_CARRY, DL, VT,
16354 DAG.getConstant(X86::COND_B,MVT::i8),
16355 N->getOperand(2)),
16356 DAG.getConstant(1, VT));
16357 return DCI.CombineTo(N, Res1, CarryOut);
16358 }
16359
16360 return SDValue();
16361}
16362
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000016363// fold (add Y, (sete X, 0)) -> adc 0, Y
16364// (add Y, (setne X, 0)) -> sbb -1, Y
16365// (sub (sete X, 0), Y) -> sbb 0, Y
16366// (sub (setne X, 0), Y) -> adc -1, Y
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000016367static SDValue OptimizeConditionalInDecrement(SDNode *N, SelectionDAG &DAG) {
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000016368 DebugLoc DL = N->getDebugLoc();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000016369
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000016370 // Look through ZExts.
16371 SDValue Ext = N->getOperand(N->getOpcode() == ISD::SUB ? 1 : 0);
16372 if (Ext.getOpcode() != ISD::ZERO_EXTEND || !Ext.hasOneUse())
16373 return SDValue();
16374
16375 SDValue SetCC = Ext.getOperand(0);
16376 if (SetCC.getOpcode() != X86ISD::SETCC || !SetCC.hasOneUse())
16377 return SDValue();
16378
16379 X86::CondCode CC = (X86::CondCode)SetCC.getConstantOperandVal(0);
16380 if (CC != X86::COND_E && CC != X86::COND_NE)
16381 return SDValue();
16382
16383 SDValue Cmp = SetCC.getOperand(1);
16384 if (Cmp.getOpcode() != X86ISD::CMP || !Cmp.hasOneUse() ||
Chris Lattner9cd3da42011-01-16 02:56:53 +000016385 !X86::isZeroNode(Cmp.getOperand(1)) ||
16386 !Cmp.getOperand(0).getValueType().isInteger())
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000016387 return SDValue();
16388
16389 SDValue CmpOp0 = Cmp.getOperand(0);
16390 SDValue NewCmp = DAG.getNode(X86ISD::CMP, DL, MVT::i32, CmpOp0,
16391 DAG.getConstant(1, CmpOp0.getValueType()));
16392
16393 SDValue OtherVal = N->getOperand(N->getOpcode() == ISD::SUB ? 0 : 1);
16394 if (CC == X86::COND_NE)
16395 return DAG.getNode(N->getOpcode() == ISD::SUB ? X86ISD::ADC : X86ISD::SBB,
16396 DL, OtherVal.getValueType(), OtherVal,
16397 DAG.getConstant(-1ULL, OtherVal.getValueType()), NewCmp);
16398 return DAG.getNode(N->getOpcode() == ISD::SUB ? X86ISD::SBB : X86ISD::ADC,
16399 DL, OtherVal.getValueType(), OtherVal,
16400 DAG.getConstant(0, OtherVal.getValueType()), NewCmp);
16401}
Chris Lattnerc19d1c32010-12-19 22:08:31 +000016402
Craig Topper54f952a2011-11-19 09:02:40 +000016403/// PerformADDCombine - Do target-specific dag combines on integer adds.
16404static SDValue PerformAddCombine(SDNode *N, SelectionDAG &DAG,
16405 const X86Subtarget *Subtarget) {
16406 EVT VT = N->getValueType(0);
16407 SDValue Op0 = N->getOperand(0);
16408 SDValue Op1 = N->getOperand(1);
16409
16410 // Try to synthesize horizontal adds from adds of shuffles.
Craig Topperd0a31172012-01-10 06:37:29 +000016411 if (((Subtarget->hasSSSE3() && (VT == MVT::v8i16 || VT == MVT::v4i32)) ||
Craig Toppere6cf4a02012-01-13 05:04:25 +000016412 (Subtarget->hasAVX2() && (VT == MVT::v16i16 || VT == MVT::v8i32))) &&
Craig Topper54f952a2011-11-19 09:02:40 +000016413 isHorizontalBinOp(Op0, Op1, true))
16414 return DAG.getNode(X86ISD::HADD, N->getDebugLoc(), VT, Op0, Op1);
16415
16416 return OptimizeConditionalInDecrement(N, DAG);
16417}
16418
16419static SDValue PerformSubCombine(SDNode *N, SelectionDAG &DAG,
16420 const X86Subtarget *Subtarget) {
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000016421 SDValue Op0 = N->getOperand(0);
16422 SDValue Op1 = N->getOperand(1);
16423
16424 // X86 can't encode an immediate LHS of a sub. See if we can push the
16425 // negation into a preceding instruction.
16426 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op0)) {
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000016427 // If the RHS of the sub is a XOR with one use and a constant, invert the
16428 // immediate. Then add one to the LHS of the sub so we can turn
16429 // X-Y -> X+~Y+1, saving one register.
16430 if (Op1->hasOneUse() && Op1.getOpcode() == ISD::XOR &&
16431 isa<ConstantSDNode>(Op1.getOperand(1))) {
Nick Lewycky726ebd62011-08-23 19:01:24 +000016432 APInt XorC = cast<ConstantSDNode>(Op1.getOperand(1))->getAPIntValue();
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000016433 EVT VT = Op0.getValueType();
16434 SDValue NewXor = DAG.getNode(ISD::XOR, Op1.getDebugLoc(), VT,
16435 Op1.getOperand(0),
16436 DAG.getConstant(~XorC, VT));
16437 return DAG.getNode(ISD::ADD, N->getDebugLoc(), VT, NewXor,
Nick Lewycky726ebd62011-08-23 19:01:24 +000016438 DAG.getConstant(C->getAPIntValue()+1, VT));
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000016439 }
16440 }
16441
Craig Topper54f952a2011-11-19 09:02:40 +000016442 // Try to synthesize horizontal adds from adds of shuffles.
16443 EVT VT = N->getValueType(0);
Craig Topperd0a31172012-01-10 06:37:29 +000016444 if (((Subtarget->hasSSSE3() && (VT == MVT::v8i16 || VT == MVT::v4i32)) ||
Craig Topperb72039c2011-11-30 09:10:50 +000016445 (Subtarget->hasAVX2() && (VT == MVT::v16i16 || VT == MVT::v8i32))) &&
16446 isHorizontalBinOp(Op0, Op1, true))
Craig Topper54f952a2011-11-19 09:02:40 +000016447 return DAG.getNode(X86ISD::HSUB, N->getDebugLoc(), VT, Op0, Op1);
16448
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000016449 return OptimizeConditionalInDecrement(N, DAG);
16450}
16451
Dan Gohman475871a2008-07-27 21:46:04 +000016452SDValue X86TargetLowering::PerformDAGCombine(SDNode *N,
Evan Cheng9dd93b32008-11-05 06:03:38 +000016453 DAGCombinerInfo &DCI) const {
Evan Cheng206ee9d2006-07-07 08:33:52 +000016454 SelectionDAG &DAG = DCI.DAG;
16455 switch (N->getOpcode()) {
16456 default: break;
Dan Gohman1bbf72b2010-03-15 23:23:03 +000016457 case ISD::EXTRACT_VECTOR_ELT:
Craig Topper89f4e662012-03-20 07:17:59 +000016458 return PerformEXTRACT_VECTOR_ELTCombine(N, DAG, DCI);
Duncan Sands6bcd2192011-09-17 16:49:39 +000016459 case ISD::VSELECT:
Nadav Rotemcc616562012-01-15 19:27:55 +000016460 case ISD::SELECT: return PerformSELECTCombine(N, DAG, DCI, Subtarget);
Michael Liaodbf8b5b2012-08-28 03:34:40 +000016461 case X86ISD::CMOV: return PerformCMOVCombine(N, DAG, DCI, Subtarget);
Craig Topper54f952a2011-11-19 09:02:40 +000016462 case ISD::ADD: return PerformAddCombine(N, DAG, Subtarget);
16463 case ISD::SUB: return PerformSubCombine(N, DAG, Subtarget);
Chris Lattner23a01992010-12-20 01:37:09 +000016464 case X86ISD::ADC: return PerformADCCombine(N, DAG, DCI);
Evan Cheng0b0cd912009-03-28 05:57:29 +000016465 case ISD::MUL: return PerformMulCombine(N, DAG, DCI);
Nate Begeman740ab032009-01-26 00:52:55 +000016466 case ISD::SHL:
16467 case ISD::SRA:
Mon P Wang845b1892012-02-01 22:15:20 +000016468 case ISD::SRL: return PerformShiftCombine(N, DAG, DCI, Subtarget);
Nate Begemanb65c1752010-12-17 22:55:37 +000016469 case ISD::AND: return PerformAndCombine(N, DAG, DCI, Subtarget);
Evan Cheng8b1190a2010-04-28 01:18:01 +000016470 case ISD::OR: return PerformOrCombine(N, DAG, DCI, Subtarget);
Craig Topperb4c94572011-10-21 06:55:01 +000016471 case ISD::XOR: return PerformXorCombine(N, DAG, DCI, Subtarget);
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000016472 case ISD::LOAD: return PerformLOADCombine(N, DAG, DCI, Subtarget);
Evan Cheng7e2ff772008-05-08 00:57:18 +000016473 case ISD::STORE: return PerformSTORECombine(N, DAG, Subtarget);
Craig Topper7fd5e162012-04-24 06:02:29 +000016474 case ISD::UINT_TO_FP: return PerformUINT_TO_FPCombine(N, DAG);
Stuart Hastingsf99a4b82011-06-06 23:15:58 +000016475 case ISD::SINT_TO_FP: return PerformSINT_TO_FPCombine(N, DAG, this);
Duncan Sands17470be2011-09-22 20:15:48 +000016476 case ISD::FADD: return PerformFADDCombine(N, DAG, Subtarget);
16477 case ISD::FSUB: return PerformFSUBCombine(N, DAG, Subtarget);
Chris Lattner6cf73262008-01-25 06:14:17 +000016478 case X86ISD::FXOR:
Chris Lattneraf723b92008-01-25 05:46:26 +000016479 case X86ISD::FOR: return PerformFORCombine(N, DAG);
Nadav Rotemd60cb112012-08-19 13:06:16 +000016480 case X86ISD::FMIN:
16481 case X86ISD::FMAX: return PerformFMinFMaxCombine(N, DAG);
Chris Lattneraf723b92008-01-25 05:46:26 +000016482 case X86ISD::FAND: return PerformFANDCombine(N, DAG);
Dan Gohmane5af2d32009-01-29 01:59:02 +000016483 case X86ISD::BT: return PerformBTCombine(N, DAG, DCI);
Eli Friedman7a5e5552009-06-07 06:52:44 +000016484 case X86ISD::VZEXT_MOVL: return PerformVZEXT_MOVLCombine(N, DAG);
Elena Demikhovsky1da58672012-04-22 09:39:03 +000016485 case ISD::ANY_EXTEND:
Craig Topperc16f8512012-04-25 06:39:39 +000016486 case ISD::ZERO_EXTEND: return PerformZExtCombine(N, DAG, DCI, Subtarget);
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +000016487 case ISD::SIGN_EXTEND: return PerformSExtCombine(N, DAG, DCI, Subtarget);
Craig Topper55b24052012-09-11 06:15:32 +000016488 case ISD::TRUNCATE: return PerformTruncateCombine(N, DAG,DCI,Subtarget);
Chad Rosiera73b6fc2012-04-27 22:33:25 +000016489 case ISD::SETCC: return PerformISDSETCCCombine(N, DAG);
Michael Liaodbf8b5b2012-08-28 03:34:40 +000016490 case X86ISD::SETCC: return PerformSETCCCombine(N, DAG, DCI, Subtarget);
Michael Liao2a33cec2012-08-10 19:58:13 +000016491 case X86ISD::BRCOND: return PerformBrCondCombine(N, DAG, DCI, Subtarget);
Craig Topperb3982da2011-12-31 23:50:21 +000016492 case X86ISD::SHUFP: // Handle all target specific shuffles
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +000016493 case X86ISD::PALIGN:
Craig Topper34671b82011-12-06 08:21:25 +000016494 case X86ISD::UNPCKH:
16495 case X86ISD::UNPCKL:
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000016496 case X86ISD::MOVHLPS:
16497 case X86ISD::MOVLHPS:
16498 case X86ISD::PSHUFD:
16499 case X86ISD::PSHUFHW:
16500 case X86ISD::PSHUFLW:
16501 case X86ISD::MOVSS:
16502 case X86ISD::MOVSD:
Craig Topper316cd2a2011-11-30 06:25:25 +000016503 case X86ISD::VPERMILP:
Craig Topperec24e612011-11-30 07:47:51 +000016504 case X86ISD::VPERM2X128:
Bruno Cardoso Lopes50b37c72011-08-15 21:45:54 +000016505 case ISD::VECTOR_SHUFFLE: return PerformShuffleCombine(N, DAG, DCI,Subtarget);
Elena Demikhovsky1503aba2012-08-01 12:06:00 +000016506 case ISD::FMA: return PerformFMACombine(N, DAG, Subtarget);
Evan Cheng206ee9d2006-07-07 08:33:52 +000016507 }
16508
Dan Gohman475871a2008-07-27 21:46:04 +000016509 return SDValue();
Evan Cheng206ee9d2006-07-07 08:33:52 +000016510}
16511
Evan Chenge5b51ac2010-04-17 06:13:15 +000016512/// isTypeDesirableForOp - Return true if the target has native support for
16513/// the specified value type and it is 'desirable' to use the type for the
16514/// given node type. e.g. On x86 i16 is legal, but undesirable since i16
16515/// instruction encodings are longer and some i16 instructions are slow.
16516bool X86TargetLowering::isTypeDesirableForOp(unsigned Opc, EVT VT) const {
16517 if (!isTypeLegal(VT))
16518 return false;
Evan Cheng2bce5f4b2010-04-28 08:30:49 +000016519 if (VT != MVT::i16)
Evan Chenge5b51ac2010-04-17 06:13:15 +000016520 return true;
16521
16522 switch (Opc) {
16523 default:
16524 return true;
Evan Cheng4c26e932010-04-19 19:29:22 +000016525 case ISD::LOAD:
16526 case ISD::SIGN_EXTEND:
16527 case ISD::ZERO_EXTEND:
16528 case ISD::ANY_EXTEND:
Evan Chenge5b51ac2010-04-17 06:13:15 +000016529 case ISD::SHL:
Evan Chenge5b51ac2010-04-17 06:13:15 +000016530 case ISD::SRL:
16531 case ISD::SUB:
16532 case ISD::ADD:
16533 case ISD::MUL:
16534 case ISD::AND:
16535 case ISD::OR:
16536 case ISD::XOR:
16537 return false;
16538 }
16539}
16540
16541/// IsDesirableToPromoteOp - This method query the target whether it is
Evan Cheng64b7bf72010-04-16 06:14:10 +000016542/// beneficial for dag combiner to promote the specified node. If true, it
16543/// should return the desired promotion type by reference.
Evan Chenge5b51ac2010-04-17 06:13:15 +000016544bool X86TargetLowering::IsDesirableToPromoteOp(SDValue Op, EVT &PVT) const {
Evan Cheng64b7bf72010-04-16 06:14:10 +000016545 EVT VT = Op.getValueType();
16546 if (VT != MVT::i16)
16547 return false;
16548
Evan Cheng4c26e932010-04-19 19:29:22 +000016549 bool Promote = false;
16550 bool Commute = false;
Evan Cheng64b7bf72010-04-16 06:14:10 +000016551 switch (Op.getOpcode()) {
Evan Cheng4c26e932010-04-19 19:29:22 +000016552 default: break;
16553 case ISD::LOAD: {
16554 LoadSDNode *LD = cast<LoadSDNode>(Op);
16555 // If the non-extending load has a single use and it's not live out, then it
16556 // might be folded.
Evan Cheng2bce5f4b2010-04-28 08:30:49 +000016557 if (LD->getExtensionType() == ISD::NON_EXTLOAD /*&&
16558 Op.hasOneUse()*/) {
16559 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
16560 UE = Op.getNode()->use_end(); UI != UE; ++UI) {
16561 // The only case where we'd want to promote LOAD (rather then it being
16562 // promoted as an operand is when it's only use is liveout.
16563 if (UI->getOpcode() != ISD::CopyToReg)
16564 return false;
16565 }
16566 }
Evan Cheng4c26e932010-04-19 19:29:22 +000016567 Promote = true;
16568 break;
16569 }
16570 case ISD::SIGN_EXTEND:
16571 case ISD::ZERO_EXTEND:
16572 case ISD::ANY_EXTEND:
16573 Promote = true;
16574 break;
Evan Chenge5b51ac2010-04-17 06:13:15 +000016575 case ISD::SHL:
Evan Cheng2bce5f4b2010-04-28 08:30:49 +000016576 case ISD::SRL: {
Evan Chenge5b51ac2010-04-17 06:13:15 +000016577 SDValue N0 = Op.getOperand(0);
16578 // Look out for (store (shl (load), x)).
Evan Chengc82c20b2010-04-24 04:44:57 +000016579 if (MayFoldLoad(N0) && MayFoldIntoStore(Op))
Evan Chenge5b51ac2010-04-17 06:13:15 +000016580 return false;
Evan Cheng4c26e932010-04-19 19:29:22 +000016581 Promote = true;
Evan Chenge5b51ac2010-04-17 06:13:15 +000016582 break;
16583 }
Evan Cheng64b7bf72010-04-16 06:14:10 +000016584 case ISD::ADD:
16585 case ISD::MUL:
16586 case ISD::AND:
16587 case ISD::OR:
Evan Cheng4c26e932010-04-19 19:29:22 +000016588 case ISD::XOR:
16589 Commute = true;
16590 // fallthrough
16591 case ISD::SUB: {
Evan Cheng64b7bf72010-04-16 06:14:10 +000016592 SDValue N0 = Op.getOperand(0);
16593 SDValue N1 = Op.getOperand(1);
Evan Chengc82c20b2010-04-24 04:44:57 +000016594 if (!Commute && MayFoldLoad(N1))
Evan Cheng64b7bf72010-04-16 06:14:10 +000016595 return false;
16596 // Avoid disabling potential load folding opportunities.
Evan Chengc82c20b2010-04-24 04:44:57 +000016597 if (MayFoldLoad(N0) && (!isa<ConstantSDNode>(N1) || MayFoldIntoStore(Op)))
Evan Cheng64b7bf72010-04-16 06:14:10 +000016598 return false;
Evan Chengc82c20b2010-04-24 04:44:57 +000016599 if (MayFoldLoad(N1) && (!isa<ConstantSDNode>(N0) || MayFoldIntoStore(Op)))
Evan Cheng64b7bf72010-04-16 06:14:10 +000016600 return false;
Evan Cheng4c26e932010-04-19 19:29:22 +000016601 Promote = true;
Evan Cheng64b7bf72010-04-16 06:14:10 +000016602 }
16603 }
16604
16605 PVT = MVT::i32;
Evan Cheng4c26e932010-04-19 19:29:22 +000016606 return Promote;
Evan Cheng64b7bf72010-04-16 06:14:10 +000016607}
16608
Evan Cheng60c07e12006-07-05 22:17:51 +000016609//===----------------------------------------------------------------------===//
16610// X86 Inline Assembly Support
16611//===----------------------------------------------------------------------===//
16612
Benjamin Kramer2ea4cdb2011-12-18 19:59:20 +000016613namespace {
16614 // Helper to match a string separated by whitespace.
Benjamin Kramer0581ed72011-12-18 20:51:31 +000016615 bool matchAsmImpl(StringRef s, ArrayRef<const StringRef *> args) {
Benjamin Kramer2ea4cdb2011-12-18 19:59:20 +000016616 s = s.substr(s.find_first_not_of(" \t")); // Skip leading whitespace.
Benjamin Kramere6cddb72011-12-17 14:36:05 +000016617
Benjamin Kramer0581ed72011-12-18 20:51:31 +000016618 for (unsigned i = 0, e = args.size(); i != e; ++i) {
Benjamin Kramer2ea4cdb2011-12-18 19:59:20 +000016619 StringRef piece(*args[i]);
16620 if (!s.startswith(piece)) // Check if the piece matches.
16621 return false;
16622
16623 s = s.substr(piece.size());
16624 StringRef::size_type pos = s.find_first_not_of(" \t");
16625 if (pos == 0) // We matched a prefix.
16626 return false;
16627
16628 s = s.substr(pos);
Benjamin Kramere6cddb72011-12-17 14:36:05 +000016629 }
16630
Benjamin Kramer2ea4cdb2011-12-18 19:59:20 +000016631 return s.empty();
Benjamin Kramere6cddb72011-12-17 14:36:05 +000016632 }
Benjamin Kramer0581ed72011-12-18 20:51:31 +000016633 const VariadicFunction1<bool, StringRef, StringRef, matchAsmImpl> matchAsm={};
Benjamin Kramere6cddb72011-12-17 14:36:05 +000016634}
16635
Chris Lattnerb8105652009-07-20 17:51:36 +000016636bool X86TargetLowering::ExpandInlineAsm(CallInst *CI) const {
16637 InlineAsm *IA = cast<InlineAsm>(CI->getCalledValue());
Chris Lattnerb8105652009-07-20 17:51:36 +000016638
16639 std::string AsmStr = IA->getAsmString();
16640
Benjamin Kramere6cddb72011-12-17 14:36:05 +000016641 IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
16642 if (!Ty || Ty->getBitWidth() % 16 != 0)
16643 return false;
16644
Chris Lattnerb8105652009-07-20 17:51:36 +000016645 // TODO: should remove alternatives from the asmstring: "foo {a|b}" -> "foo a"
Benjamin Kramerd4f19592010-01-11 18:03:24 +000016646 SmallVector<StringRef, 4> AsmPieces;
Peter Collingbourne98361182010-11-13 19:54:23 +000016647 SplitString(AsmStr, AsmPieces, ";\n");
Chris Lattnerb8105652009-07-20 17:51:36 +000016648
16649 switch (AsmPieces.size()) {
16650 default: return false;
16651 case 1:
Chris Lattner7a2bdde2011-04-15 05:18:47 +000016652 // FIXME: this should verify that we are targeting a 486 or better. If not,
Benjamin Kramere6cddb72011-12-17 14:36:05 +000016653 // we will turn this bswap into something that will be lowered to logical
16654 // ops instead of emitting the bswap asm. For now, we don't support 486 or
16655 // lower so don't worry about this.
Chris Lattnerb8105652009-07-20 17:51:36 +000016656 // bswap $0
Benjamin Kramer2ea4cdb2011-12-18 19:59:20 +000016657 if (matchAsm(AsmPieces[0], "bswap", "$0") ||
16658 matchAsm(AsmPieces[0], "bswapl", "$0") ||
16659 matchAsm(AsmPieces[0], "bswapq", "$0") ||
16660 matchAsm(AsmPieces[0], "bswap", "${0:q}") ||
16661 matchAsm(AsmPieces[0], "bswapl", "${0:q}") ||
16662 matchAsm(AsmPieces[0], "bswapq", "${0:q}")) {
Chris Lattnerb8105652009-07-20 17:51:36 +000016663 // No need to check constraints, nothing other than the equivalent of
16664 // "=r,0" would be valid here.
Evan Cheng55d42002011-01-08 01:24:27 +000016665 return IntrinsicLowering::LowerToByteSwap(CI);
Chris Lattnerb8105652009-07-20 17:51:36 +000016666 }
Benjamin Kramere6cddb72011-12-17 14:36:05 +000016667
Chris Lattnerb8105652009-07-20 17:51:36 +000016668 // rorw $$8, ${0:w} --> llvm.bswap.i16
Duncan Sandsb0bc6c32010-02-15 16:12:20 +000016669 if (CI->getType()->isIntegerTy(16) &&
Benjamin Kramere6cddb72011-12-17 14:36:05 +000016670 IA->getConstraintString().compare(0, 5, "=r,0,") == 0 &&
Benjamin Kramer2ea4cdb2011-12-18 19:59:20 +000016671 (matchAsm(AsmPieces[0], "rorw", "$$8,", "${0:w}") ||
16672 matchAsm(AsmPieces[0], "rolw", "$$8,", "${0:w}"))) {
Dan Gohman0ef701e2010-03-04 19:58:08 +000016673 AsmPieces.clear();
Evan Cheng55d42002011-01-08 01:24:27 +000016674 const std::string &ConstraintsStr = IA->getConstraintString();
16675 SplitString(StringRef(ConstraintsStr).substr(5), AsmPieces, ",");
Dan Gohman0ef701e2010-03-04 19:58:08 +000016676 std::sort(AsmPieces.begin(), AsmPieces.end());
16677 if (AsmPieces.size() == 4 &&
16678 AsmPieces[0] == "~{cc}" &&
16679 AsmPieces[1] == "~{dirflag}" &&
16680 AsmPieces[2] == "~{flags}" &&
Benjamin Kramere6cddb72011-12-17 14:36:05 +000016681 AsmPieces[3] == "~{fpsr}")
16682 return IntrinsicLowering::LowerToByteSwap(CI);
Chris Lattnerb8105652009-07-20 17:51:36 +000016683 }
16684 break;
16685 case 3:
Peter Collingbourne948cf022010-11-13 19:54:30 +000016686 if (CI->getType()->isIntegerTy(32) &&
Benjamin Kramere6cddb72011-12-17 14:36:05 +000016687 IA->getConstraintString().compare(0, 5, "=r,0,") == 0 &&
Benjamin Kramer2ea4cdb2011-12-18 19:59:20 +000016688 matchAsm(AsmPieces[0], "rorw", "$$8,", "${0:w}") &&
16689 matchAsm(AsmPieces[1], "rorl", "$$16,", "$0") &&
16690 matchAsm(AsmPieces[2], "rorw", "$$8,", "${0:w}")) {
Benjamin Kramere6cddb72011-12-17 14:36:05 +000016691 AsmPieces.clear();
16692 const std::string &ConstraintsStr = IA->getConstraintString();
16693 SplitString(StringRef(ConstraintsStr).substr(5), AsmPieces, ",");
16694 std::sort(AsmPieces.begin(), AsmPieces.end());
16695 if (AsmPieces.size() == 4 &&
16696 AsmPieces[0] == "~{cc}" &&
16697 AsmPieces[1] == "~{dirflag}" &&
16698 AsmPieces[2] == "~{flags}" &&
16699 AsmPieces[3] == "~{fpsr}")
16700 return IntrinsicLowering::LowerToByteSwap(CI);
Peter Collingbourne948cf022010-11-13 19:54:30 +000016701 }
Evan Cheng55d42002011-01-08 01:24:27 +000016702
16703 if (CI->getType()->isIntegerTy(64)) {
16704 InlineAsm::ConstraintInfoVector Constraints = IA->ParseConstraints();
16705 if (Constraints.size() >= 2 &&
16706 Constraints[0].Codes.size() == 1 && Constraints[0].Codes[0] == "A" &&
16707 Constraints[1].Codes.size() == 1 && Constraints[1].Codes[0] == "0") {
16708 // bswap %eax / bswap %edx / xchgl %eax, %edx -> llvm.bswap.i64
Benjamin Kramer2ea4cdb2011-12-18 19:59:20 +000016709 if (matchAsm(AsmPieces[0], "bswap", "%eax") &&
16710 matchAsm(AsmPieces[1], "bswap", "%edx") &&
16711 matchAsm(AsmPieces[2], "xchgl", "%eax,", "%edx"))
Benjamin Kramere6cddb72011-12-17 14:36:05 +000016712 return IntrinsicLowering::LowerToByteSwap(CI);
Chris Lattnerb8105652009-07-20 17:51:36 +000016713 }
16714 }
16715 break;
16716 }
16717 return false;
16718}
16719
16720
16721
Chris Lattnerf4dff842006-07-11 02:54:03 +000016722/// getConstraintType - Given a constraint letter, return the type of
16723/// constraint it is for this target.
16724X86TargetLowering::ConstraintType
Chris Lattner4234f572007-03-25 02:14:49 +000016725X86TargetLowering::getConstraintType(const std::string &Constraint) const {
16726 if (Constraint.size() == 1) {
16727 switch (Constraint[0]) {
Chris Lattner4234f572007-03-25 02:14:49 +000016728 case 'R':
Chris Lattner4234f572007-03-25 02:14:49 +000016729 case 'q':
16730 case 'Q':
John Thompson44ab89e2010-10-29 17:29:13 +000016731 case 'f':
16732 case 't':
16733 case 'u':
Dale Johannesen2ffbcac2008-04-01 00:57:48 +000016734 case 'y':
John Thompson44ab89e2010-10-29 17:29:13 +000016735 case 'x':
Chris Lattner4234f572007-03-25 02:14:49 +000016736 case 'Y':
Eric Christopher31b5f002011-07-07 22:29:07 +000016737 case 'l':
Chris Lattner4234f572007-03-25 02:14:49 +000016738 return C_RegisterClass;
John Thompson44ab89e2010-10-29 17:29:13 +000016739 case 'a':
16740 case 'b':
16741 case 'c':
16742 case 'd':
16743 case 'S':
16744 case 'D':
16745 case 'A':
16746 return C_Register;
16747 case 'I':
16748 case 'J':
16749 case 'K':
16750 case 'L':
16751 case 'M':
16752 case 'N':
16753 case 'G':
16754 case 'C':
Dale Johannesen78e3e522009-02-12 20:58:09 +000016755 case 'e':
16756 case 'Z':
16757 return C_Other;
Chris Lattner4234f572007-03-25 02:14:49 +000016758 default:
16759 break;
16760 }
Chris Lattnerf4dff842006-07-11 02:54:03 +000016761 }
Chris Lattner4234f572007-03-25 02:14:49 +000016762 return TargetLowering::getConstraintType(Constraint);
Chris Lattnerf4dff842006-07-11 02:54:03 +000016763}
16764
John Thompson44ab89e2010-10-29 17:29:13 +000016765/// Examine constraint type and operand type and determine a weight value.
John Thompsoneac6e1d2010-09-13 18:15:37 +000016766/// This object must already have been set up with the operand type
16767/// and the current alternative constraint selected.
John Thompson44ab89e2010-10-29 17:29:13 +000016768TargetLowering::ConstraintWeight
16769 X86TargetLowering::getSingleConstraintMatchWeight(
John Thompsoneac6e1d2010-09-13 18:15:37 +000016770 AsmOperandInfo &info, const char *constraint) const {
John Thompson44ab89e2010-10-29 17:29:13 +000016771 ConstraintWeight weight = CW_Invalid;
John Thompsoneac6e1d2010-09-13 18:15:37 +000016772 Value *CallOperandVal = info.CallOperandVal;
16773 // If we don't have a value, we can't do a match,
16774 // but allow it at the lowest weight.
16775 if (CallOperandVal == NULL)
John Thompson44ab89e2010-10-29 17:29:13 +000016776 return CW_Default;
Chris Lattnerdb125cf2011-07-18 04:54:35 +000016777 Type *type = CallOperandVal->getType();
John Thompsoneac6e1d2010-09-13 18:15:37 +000016778 // Look at the constraint type.
16779 switch (*constraint) {
16780 default:
John Thompson44ab89e2010-10-29 17:29:13 +000016781 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
16782 case 'R':
16783 case 'q':
16784 case 'Q':
16785 case 'a':
16786 case 'b':
16787 case 'c':
16788 case 'd':
16789 case 'S':
16790 case 'D':
16791 case 'A':
16792 if (CallOperandVal->getType()->isIntegerTy())
16793 weight = CW_SpecificReg;
16794 break;
16795 case 'f':
16796 case 't':
16797 case 'u':
16798 if (type->isFloatingPointTy())
16799 weight = CW_SpecificReg;
16800 break;
16801 case 'y':
Chris Lattner2a786eb2010-12-19 20:19:20 +000016802 if (type->isX86_MMXTy() && Subtarget->hasMMX())
John Thompson44ab89e2010-10-29 17:29:13 +000016803 weight = CW_SpecificReg;
16804 break;
16805 case 'x':
16806 case 'Y':
Craig Topper1accb7e2012-01-10 06:54:16 +000016807 if (((type->getPrimitiveSizeInBits() == 128) && Subtarget->hasSSE1()) ||
Eric Christopher55487552012-01-07 01:02:09 +000016808 ((type->getPrimitiveSizeInBits() == 256) && Subtarget->hasAVX()))
John Thompson44ab89e2010-10-29 17:29:13 +000016809 weight = CW_Register;
John Thompsoneac6e1d2010-09-13 18:15:37 +000016810 break;
16811 case 'I':
16812 if (ConstantInt *C = dyn_cast<ConstantInt>(info.CallOperandVal)) {
16813 if (C->getZExtValue() <= 31)
John Thompson44ab89e2010-10-29 17:29:13 +000016814 weight = CW_Constant;
John Thompsoneac6e1d2010-09-13 18:15:37 +000016815 }
16816 break;
John Thompson44ab89e2010-10-29 17:29:13 +000016817 case 'J':
16818 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
16819 if (C->getZExtValue() <= 63)
16820 weight = CW_Constant;
16821 }
16822 break;
16823 case 'K':
16824 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
16825 if ((C->getSExtValue() >= -0x80) && (C->getSExtValue() <= 0x7f))
16826 weight = CW_Constant;
16827 }
16828 break;
16829 case 'L':
16830 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
16831 if ((C->getZExtValue() == 0xff) || (C->getZExtValue() == 0xffff))
16832 weight = CW_Constant;
16833 }
16834 break;
16835 case 'M':
16836 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
16837 if (C->getZExtValue() <= 3)
16838 weight = CW_Constant;
16839 }
16840 break;
16841 case 'N':
16842 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
16843 if (C->getZExtValue() <= 0xff)
16844 weight = CW_Constant;
16845 }
16846 break;
16847 case 'G':
16848 case 'C':
16849 if (dyn_cast<ConstantFP>(CallOperandVal)) {
16850 weight = CW_Constant;
16851 }
16852 break;
16853 case 'e':
16854 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
16855 if ((C->getSExtValue() >= -0x80000000LL) &&
16856 (C->getSExtValue() <= 0x7fffffffLL))
16857 weight = CW_Constant;
16858 }
16859 break;
16860 case 'Z':
16861 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
16862 if (C->getZExtValue() <= 0xffffffff)
16863 weight = CW_Constant;
16864 }
16865 break;
John Thompsoneac6e1d2010-09-13 18:15:37 +000016866 }
16867 return weight;
16868}
16869
Dale Johannesenba2a0b92008-01-29 02:21:21 +000016870/// LowerXConstraint - try to replace an X constraint, which matches anything,
16871/// with another that has more specific requirements based on the type of the
16872/// corresponding operand.
Chris Lattner5e764232008-04-26 23:02:14 +000016873const char *X86TargetLowering::
Owen Andersone50ed302009-08-10 22:56:29 +000016874LowerXConstraint(EVT ConstraintVT) const {
Chris Lattner5e764232008-04-26 23:02:14 +000016875 // FP X constraints get lowered to SSE1/2 registers if available, otherwise
16876 // 'f' like normal targets.
Duncan Sands83ec4b62008-06-06 12:08:01 +000016877 if (ConstraintVT.isFloatingPoint()) {
Craig Topper1accb7e2012-01-10 06:54:16 +000016878 if (Subtarget->hasSSE2())
Chris Lattner5e764232008-04-26 23:02:14 +000016879 return "Y";
Craig Topper1accb7e2012-01-10 06:54:16 +000016880 if (Subtarget->hasSSE1())
Chris Lattner5e764232008-04-26 23:02:14 +000016881 return "x";
16882 }
Scott Michelfdc40a02009-02-17 22:15:04 +000016883
Chris Lattner5e764232008-04-26 23:02:14 +000016884 return TargetLowering::LowerXConstraint(ConstraintVT);
Dale Johannesenba2a0b92008-01-29 02:21:21 +000016885}
16886
Chris Lattner48884cd2007-08-25 00:47:38 +000016887/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
16888/// vector. If it is invalid, don't add anything to Ops.
Dan Gohman475871a2008-07-27 21:46:04 +000016889void X86TargetLowering::LowerAsmOperandForConstraint(SDValue Op,
Eric Christopher100c8332011-06-02 23:16:42 +000016890 std::string &Constraint,
Dan Gohman475871a2008-07-27 21:46:04 +000016891 std::vector<SDValue>&Ops,
Chris Lattner5e764232008-04-26 23:02:14 +000016892 SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +000016893 SDValue Result(0, 0);
Scott Michelfdc40a02009-02-17 22:15:04 +000016894
Eric Christopher100c8332011-06-02 23:16:42 +000016895 // Only support length 1 constraints for now.
16896 if (Constraint.length() > 1) return;
Eric Christopher471e4222011-06-08 23:55:35 +000016897
Eric Christopher100c8332011-06-02 23:16:42 +000016898 char ConstraintLetter = Constraint[0];
16899 switch (ConstraintLetter) {
Chris Lattner22aaf1d2006-10-31 20:13:11 +000016900 default: break;
Devang Patel84f7fd22007-03-17 00:13:28 +000016901 case 'I':
Chris Lattner188b9fe2007-03-25 01:57:35 +000016902 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +000016903 if (C->getZExtValue() <= 31) {
16904 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattner48884cd2007-08-25 00:47:38 +000016905 break;
16906 }
Devang Patel84f7fd22007-03-17 00:13:28 +000016907 }
Chris Lattner48884cd2007-08-25 00:47:38 +000016908 return;
Evan Cheng364091e2008-09-22 23:57:37 +000016909 case 'J':
16910 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattner2e06dd22009-06-15 04:39:05 +000016911 if (C->getZExtValue() <= 63) {
Chris Lattnere4935152009-06-15 04:01:39 +000016912 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
16913 break;
16914 }
16915 }
16916 return;
16917 case 'K':
16918 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattner2e06dd22009-06-15 04:39:05 +000016919 if ((int8_t)C->getSExtValue() == C->getSExtValue()) {
Evan Cheng364091e2008-09-22 23:57:37 +000016920 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
16921 break;
16922 }
16923 }
16924 return;
Chris Lattner188b9fe2007-03-25 01:57:35 +000016925 case 'N':
16926 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +000016927 if (C->getZExtValue() <= 255) {
16928 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattner48884cd2007-08-25 00:47:38 +000016929 break;
16930 }
Chris Lattner188b9fe2007-03-25 01:57:35 +000016931 }
Chris Lattner48884cd2007-08-25 00:47:38 +000016932 return;
Dale Johannesen78e3e522009-02-12 20:58:09 +000016933 case 'e': {
16934 // 32-bit signed value
16935 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohman7720cb32010-06-18 14:01:07 +000016936 if (ConstantInt::isValueValidForType(Type::getInt32Ty(*DAG.getContext()),
16937 C->getSExtValue())) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000016938 // Widen to 64 bits here to get it sign extended.
Owen Anderson825b72b2009-08-11 20:47:22 +000016939 Result = DAG.getTargetConstant(C->getSExtValue(), MVT::i64);
Dale Johannesen78e3e522009-02-12 20:58:09 +000016940 break;
16941 }
16942 // FIXME gcc accepts some relocatable values here too, but only in certain
16943 // memory models; it's complicated.
16944 }
16945 return;
16946 }
16947 case 'Z': {
16948 // 32-bit unsigned value
16949 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohman7720cb32010-06-18 14:01:07 +000016950 if (ConstantInt::isValueValidForType(Type::getInt32Ty(*DAG.getContext()),
16951 C->getZExtValue())) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000016952 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
16953 break;
16954 }
16955 }
16956 // FIXME gcc accepts some relocatable values here too, but only in certain
16957 // memory models; it's complicated.
16958 return;
16959 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000016960 case 'i': {
Chris Lattner22aaf1d2006-10-31 20:13:11 +000016961 // Literal immediates are always ok.
Chris Lattner48884cd2007-08-25 00:47:38 +000016962 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Op)) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000016963 // Widen to 64 bits here to get it sign extended.
Owen Anderson825b72b2009-08-11 20:47:22 +000016964 Result = DAG.getTargetConstant(CST->getSExtValue(), MVT::i64);
Chris Lattner48884cd2007-08-25 00:47:38 +000016965 break;
16966 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000016967
Dale Johannesene5ff9ef2010-06-24 20:14:51 +000016968 // In any sort of PIC mode addresses need to be computed at runtime by
16969 // adding in a register or some sort of table lookup. These can't
16970 // be used as immediates.
Dale Johannesene2b448c2010-07-06 23:27:00 +000016971 if (Subtarget->isPICStyleGOT() || Subtarget->isPICStyleStubPIC())
Dale Johannesene5ff9ef2010-06-24 20:14:51 +000016972 return;
16973
Chris Lattnerdc43a882007-05-03 16:52:29 +000016974 // If we are in non-pic codegen mode, we allow the address of a global (with
16975 // an optional displacement) to be used with 'i'.
Chris Lattner49921962009-05-08 18:23:14 +000016976 GlobalAddressSDNode *GA = 0;
Chris Lattnerdc43a882007-05-03 16:52:29 +000016977 int64_t Offset = 0;
Scott Michelfdc40a02009-02-17 22:15:04 +000016978
Chris Lattner49921962009-05-08 18:23:14 +000016979 // Match either (GA), (GA+C), (GA+C1+C2), etc.
16980 while (1) {
16981 if ((GA = dyn_cast<GlobalAddressSDNode>(Op))) {
16982 Offset += GA->getOffset();
16983 break;
16984 } else if (Op.getOpcode() == ISD::ADD) {
16985 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
16986 Offset += C->getZExtValue();
16987 Op = Op.getOperand(0);
16988 continue;
16989 }
16990 } else if (Op.getOpcode() == ISD::SUB) {
16991 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
16992 Offset += -C->getZExtValue();
16993 Op = Op.getOperand(0);
16994 continue;
16995 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000016996 }
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000016997
Chris Lattner49921962009-05-08 18:23:14 +000016998 // Otherwise, this isn't something we can handle, reject it.
16999 return;
Chris Lattnerdc43a882007-05-03 16:52:29 +000017000 }
Eric Christopherfd179292009-08-27 18:07:15 +000017001
Dan Gohman46510a72010-04-15 01:51:59 +000017002 const GlobalValue *GV = GA->getGlobal();
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000017003 // If we require an extra load to get this address, as in PIC mode, we
17004 // can't accept it.
Chris Lattner36c25012009-07-10 07:34:39 +000017005 if (isGlobalStubReference(Subtarget->ClassifyGlobalReference(GV,
17006 getTargetMachine())))
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000017007 return;
Scott Michelfdc40a02009-02-17 22:15:04 +000017008
Devang Patel0d881da2010-07-06 22:08:15 +000017009 Result = DAG.getTargetGlobalAddress(GV, Op.getDebugLoc(),
17010 GA->getValueType(0), Offset);
Chris Lattner49921962009-05-08 18:23:14 +000017011 break;
Chris Lattner22aaf1d2006-10-31 20:13:11 +000017012 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000017013 }
Scott Michelfdc40a02009-02-17 22:15:04 +000017014
Gabor Greifba36cb52008-08-28 21:40:38 +000017015 if (Result.getNode()) {
Chris Lattner48884cd2007-08-25 00:47:38 +000017016 Ops.push_back(Result);
17017 return;
17018 }
Dale Johannesen1784d162010-06-25 21:55:36 +000017019 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
Chris Lattner22aaf1d2006-10-31 20:13:11 +000017020}
17021
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000017022std::pair<unsigned, const TargetRegisterClass*>
Chris Lattnerf76d1802006-07-31 23:26:50 +000017023X86TargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +000017024 EVT VT) const {
Chris Lattnerad043e82007-04-09 05:11:28 +000017025 // First, see if this is a constraint that directly corresponds to an LLVM
17026 // register class.
17027 if (Constraint.size() == 1) {
17028 // GCC Constraint Letters
17029 switch (Constraint[0]) {
17030 default: break;
Eric Christopherd176af82011-06-29 17:23:50 +000017031 // TODO: Slight differences here in allocation order and leaving
17032 // RIP in the class. Do they matter any more here than they do
17033 // in the normal allocation?
17034 case 'q': // GENERAL_REGS in 64-bit mode, Q_REGS in 32-bit mode.
17035 if (Subtarget->is64Bit()) {
Craig Topperc9099502012-04-20 06:31:50 +000017036 if (VT == MVT::i32 || VT == MVT::f32)
17037 return std::make_pair(0U, &X86::GR32RegClass);
17038 if (VT == MVT::i16)
17039 return std::make_pair(0U, &X86::GR16RegClass);
17040 if (VT == MVT::i8 || VT == MVT::i1)
17041 return std::make_pair(0U, &X86::GR8RegClass);
17042 if (VT == MVT::i64 || VT == MVT::f64)
17043 return std::make_pair(0U, &X86::GR64RegClass);
17044 break;
Eric Christopherd176af82011-06-29 17:23:50 +000017045 }
17046 // 32-bit fallthrough
17047 case 'Q': // Q_REGS
Nick Lewycky9bf45d02011-07-08 00:19:27 +000017048 if (VT == MVT::i32 || VT == MVT::f32)
Craig Topperc9099502012-04-20 06:31:50 +000017049 return std::make_pair(0U, &X86::GR32_ABCDRegClass);
17050 if (VT == MVT::i16)
17051 return std::make_pair(0U, &X86::GR16_ABCDRegClass);
17052 if (VT == MVT::i8 || VT == MVT::i1)
17053 return std::make_pair(0U, &X86::GR8_ABCD_LRegClass);
17054 if (VT == MVT::i64)
17055 return std::make_pair(0U, &X86::GR64_ABCDRegClass);
Eric Christopherd176af82011-06-29 17:23:50 +000017056 break;
Chris Lattner0f65cad2007-04-09 05:49:22 +000017057 case 'r': // GENERAL_REGS
Chris Lattner0f65cad2007-04-09 05:49:22 +000017058 case 'l': // INDEX_REGS
Eric Christopher5427ede2011-07-14 20:13:52 +000017059 if (VT == MVT::i8 || VT == MVT::i1)
Craig Topperc9099502012-04-20 06:31:50 +000017060 return std::make_pair(0U, &X86::GR8RegClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000017061 if (VT == MVT::i16)
Craig Topperc9099502012-04-20 06:31:50 +000017062 return std::make_pair(0U, &X86::GR16RegClass);
Eric Christopher2bbecd82011-05-19 21:33:47 +000017063 if (VT == MVT::i32 || VT == MVT::f32 || !Subtarget->is64Bit())
Craig Topperc9099502012-04-20 06:31:50 +000017064 return std::make_pair(0U, &X86::GR32RegClass);
17065 return std::make_pair(0U, &X86::GR64RegClass);
Dale Johannesen5f3663e2009-10-07 22:47:20 +000017066 case 'R': // LEGACY_REGS
Eric Christopher5427ede2011-07-14 20:13:52 +000017067 if (VT == MVT::i8 || VT == MVT::i1)
Craig Topperc9099502012-04-20 06:31:50 +000017068 return std::make_pair(0U, &X86::GR8_NOREXRegClass);
Dale Johannesen5f3663e2009-10-07 22:47:20 +000017069 if (VT == MVT::i16)
Craig Topperc9099502012-04-20 06:31:50 +000017070 return std::make_pair(0U, &X86::GR16_NOREXRegClass);
Dale Johannesen5f3663e2009-10-07 22:47:20 +000017071 if (VT == MVT::i32 || !Subtarget->is64Bit())
Craig Topperc9099502012-04-20 06:31:50 +000017072 return std::make_pair(0U, &X86::GR32_NOREXRegClass);
17073 return std::make_pair(0U, &X86::GR64_NOREXRegClass);
Chris Lattnerfce84ac2008-03-11 19:06:29 +000017074 case 'f': // FP Stack registers.
17075 // If SSE is enabled for this VT, use f80 to ensure the isel moves the
17076 // value to the correct fpstack register class.
Owen Anderson825b72b2009-08-11 20:47:22 +000017077 if (VT == MVT::f32 && !isScalarFPTypeInSSEReg(VT))
Craig Topperc9099502012-04-20 06:31:50 +000017078 return std::make_pair(0U, &X86::RFP32RegClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000017079 if (VT == MVT::f64 && !isScalarFPTypeInSSEReg(VT))
Craig Topperc9099502012-04-20 06:31:50 +000017080 return std::make_pair(0U, &X86::RFP64RegClass);
17081 return std::make_pair(0U, &X86::RFP80RegClass);
Chris Lattner6c284d72007-04-12 04:14:49 +000017082 case 'y': // MMX_REGS if MMX allowed.
17083 if (!Subtarget->hasMMX()) break;
Craig Topperc9099502012-04-20 06:31:50 +000017084 return std::make_pair(0U, &X86::VR64RegClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +000017085 case 'Y': // SSE_REGS if SSE2 allowed
Craig Topper1accb7e2012-01-10 06:54:16 +000017086 if (!Subtarget->hasSSE2()) break;
Chris Lattner0f65cad2007-04-09 05:49:22 +000017087 // FALL THROUGH.
Eric Christopher55487552012-01-07 01:02:09 +000017088 case 'x': // SSE_REGS if SSE1 allowed or AVX_REGS if AVX allowed
Craig Topper1accb7e2012-01-10 06:54:16 +000017089 if (!Subtarget->hasSSE1()) break;
Duncan Sands83ec4b62008-06-06 12:08:01 +000017090
Owen Anderson825b72b2009-08-11 20:47:22 +000017091 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner0f65cad2007-04-09 05:49:22 +000017092 default: break;
17093 // Scalar SSE types.
Owen Anderson825b72b2009-08-11 20:47:22 +000017094 case MVT::f32:
17095 case MVT::i32:
Craig Topperc9099502012-04-20 06:31:50 +000017096 return std::make_pair(0U, &X86::FR32RegClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000017097 case MVT::f64:
17098 case MVT::i64:
Craig Topperc9099502012-04-20 06:31:50 +000017099 return std::make_pair(0U, &X86::FR64RegClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +000017100 // Vector types.
Owen Anderson825b72b2009-08-11 20:47:22 +000017101 case MVT::v16i8:
17102 case MVT::v8i16:
17103 case MVT::v4i32:
17104 case MVT::v2i64:
17105 case MVT::v4f32:
17106 case MVT::v2f64:
Craig Topperc9099502012-04-20 06:31:50 +000017107 return std::make_pair(0U, &X86::VR128RegClass);
Eric Christopher55487552012-01-07 01:02:09 +000017108 // AVX types.
17109 case MVT::v32i8:
17110 case MVT::v16i16:
17111 case MVT::v8i32:
17112 case MVT::v4i64:
17113 case MVT::v8f32:
17114 case MVT::v4f64:
Craig Topperc9099502012-04-20 06:31:50 +000017115 return std::make_pair(0U, &X86::VR256RegClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +000017116 }
Chris Lattnerad043e82007-04-09 05:11:28 +000017117 break;
17118 }
17119 }
Scott Michelfdc40a02009-02-17 22:15:04 +000017120
Chris Lattnerf76d1802006-07-31 23:26:50 +000017121 // Use the default implementation in TargetLowering to convert the register
17122 // constraint into a member of a register class.
17123 std::pair<unsigned, const TargetRegisterClass*> Res;
17124 Res = TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
Chris Lattner1a60aa72006-10-31 19:42:44 +000017125
17126 // Not found as a standard register?
17127 if (Res.second == 0) {
Chris Lattner56d77c72009-09-13 22:41:48 +000017128 // Map st(0) -> st(7) -> ST0
17129 if (Constraint.size() == 7 && Constraint[0] == '{' &&
17130 tolower(Constraint[1]) == 's' &&
17131 tolower(Constraint[2]) == 't' &&
17132 Constraint[3] == '(' &&
17133 (Constraint[4] >= '0' && Constraint[4] <= '7') &&
17134 Constraint[5] == ')' &&
17135 Constraint[6] == '}') {
Daniel Dunbara279bc32009-09-20 02:20:51 +000017136
Chris Lattner56d77c72009-09-13 22:41:48 +000017137 Res.first = X86::ST0+Constraint[4]-'0';
Craig Topperc9099502012-04-20 06:31:50 +000017138 Res.second = &X86::RFP80RegClass;
Chris Lattner56d77c72009-09-13 22:41:48 +000017139 return Res;
17140 }
Daniel Dunbara279bc32009-09-20 02:20:51 +000017141
Chris Lattner56d77c72009-09-13 22:41:48 +000017142 // GCC allows "st(0)" to be called just plain "st".
Benjamin Kramer05872ea2009-11-12 20:36:59 +000017143 if (StringRef("{st}").equals_lower(Constraint)) {
Chris Lattner1a60aa72006-10-31 19:42:44 +000017144 Res.first = X86::ST0;
Craig Topperc9099502012-04-20 06:31:50 +000017145 Res.second = &X86::RFP80RegClass;
Chris Lattner56d77c72009-09-13 22:41:48 +000017146 return Res;
Chris Lattner1a60aa72006-10-31 19:42:44 +000017147 }
Chris Lattner56d77c72009-09-13 22:41:48 +000017148
17149 // flags -> EFLAGS
Benjamin Kramer05872ea2009-11-12 20:36:59 +000017150 if (StringRef("{flags}").equals_lower(Constraint)) {
Chris Lattner56d77c72009-09-13 22:41:48 +000017151 Res.first = X86::EFLAGS;
Craig Topperc9099502012-04-20 06:31:50 +000017152 Res.second = &X86::CCRRegClass;
Chris Lattner56d77c72009-09-13 22:41:48 +000017153 return Res;
17154 }
Daniel Dunbara279bc32009-09-20 02:20:51 +000017155
Dale Johannesen330169f2008-11-13 21:52:36 +000017156 // 'A' means EAX + EDX.
17157 if (Constraint == "A") {
17158 Res.first = X86::EAX;
Craig Topperc9099502012-04-20 06:31:50 +000017159 Res.second = &X86::GR32_ADRegClass;
Chris Lattner56d77c72009-09-13 22:41:48 +000017160 return Res;
Dale Johannesen330169f2008-11-13 21:52:36 +000017161 }
Chris Lattner1a60aa72006-10-31 19:42:44 +000017162 return Res;
17163 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000017164
Chris Lattnerf76d1802006-07-31 23:26:50 +000017165 // Otherwise, check to see if this is a register class of the wrong value
17166 // type. For example, we want to map "{ax},i32" -> {eax}, we don't want it to
17167 // turn into {ax},{dx}.
17168 if (Res.second->hasType(VT))
17169 return Res; // Correct type already, nothing to do.
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000017170
Chris Lattnerf76d1802006-07-31 23:26:50 +000017171 // All of the single-register GCC register classes map their values onto
17172 // 16-bit register pieces "ax","dx","cx","bx","si","di","bp","sp". If we
17173 // really want an 8-bit or 32-bit register, map to the appropriate register
17174 // class and return the appropriate register.
Craig Topperc9099502012-04-20 06:31:50 +000017175 if (Res.second == &X86::GR16RegClass) {
Owen Anderson825b72b2009-08-11 20:47:22 +000017176 if (VT == MVT::i8) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000017177 unsigned DestReg = 0;
17178 switch (Res.first) {
17179 default: break;
17180 case X86::AX: DestReg = X86::AL; break;
17181 case X86::DX: DestReg = X86::DL; break;
17182 case X86::CX: DestReg = X86::CL; break;
17183 case X86::BX: DestReg = X86::BL; break;
17184 }
17185 if (DestReg) {
17186 Res.first = DestReg;
Craig Topperc9099502012-04-20 06:31:50 +000017187 Res.second = &X86::GR8RegClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000017188 }
Owen Anderson825b72b2009-08-11 20:47:22 +000017189 } else if (VT == MVT::i32) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000017190 unsigned DestReg = 0;
17191 switch (Res.first) {
17192 default: break;
17193 case X86::AX: DestReg = X86::EAX; break;
17194 case X86::DX: DestReg = X86::EDX; break;
17195 case X86::CX: DestReg = X86::ECX; break;
17196 case X86::BX: DestReg = X86::EBX; break;
17197 case X86::SI: DestReg = X86::ESI; break;
17198 case X86::DI: DestReg = X86::EDI; break;
17199 case X86::BP: DestReg = X86::EBP; break;
17200 case X86::SP: DestReg = X86::ESP; break;
17201 }
17202 if (DestReg) {
17203 Res.first = DestReg;
Craig Topperc9099502012-04-20 06:31:50 +000017204 Res.second = &X86::GR32RegClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000017205 }
Owen Anderson825b72b2009-08-11 20:47:22 +000017206 } else if (VT == MVT::i64) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000017207 unsigned DestReg = 0;
17208 switch (Res.first) {
17209 default: break;
17210 case X86::AX: DestReg = X86::RAX; break;
17211 case X86::DX: DestReg = X86::RDX; break;
17212 case X86::CX: DestReg = X86::RCX; break;
17213 case X86::BX: DestReg = X86::RBX; break;
17214 case X86::SI: DestReg = X86::RSI; break;
17215 case X86::DI: DestReg = X86::RDI; break;
17216 case X86::BP: DestReg = X86::RBP; break;
17217 case X86::SP: DestReg = X86::RSP; break;
17218 }
17219 if (DestReg) {
17220 Res.first = DestReg;
Craig Topperc9099502012-04-20 06:31:50 +000017221 Res.second = &X86::GR64RegClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000017222 }
Chris Lattnerf76d1802006-07-31 23:26:50 +000017223 }
Craig Topperc9099502012-04-20 06:31:50 +000017224 } else if (Res.second == &X86::FR32RegClass ||
17225 Res.second == &X86::FR64RegClass ||
17226 Res.second == &X86::VR128RegClass) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000017227 // Handle references to XMM physical registers that got mapped into the
17228 // wrong class. This can happen with constraints like {xmm0} where the
17229 // target independent register mapper will just pick the first match it can
17230 // find, ignoring the required type.
Eli Friedman52d418d2012-06-25 23:42:33 +000017231
17232 if (VT == MVT::f32 || VT == MVT::i32)
Craig Topperc9099502012-04-20 06:31:50 +000017233 Res.second = &X86::FR32RegClass;
Eli Friedman52d418d2012-06-25 23:42:33 +000017234 else if (VT == MVT::f64 || VT == MVT::i64)
Craig Topperc9099502012-04-20 06:31:50 +000017235 Res.second = &X86::FR64RegClass;
17236 else if (X86::VR128RegClass.hasType(VT))
17237 Res.second = &X86::VR128RegClass;
Eli Friedman52d418d2012-06-25 23:42:33 +000017238 else if (X86::VR256RegClass.hasType(VT))
17239 Res.second = &X86::VR256RegClass;
Chris Lattnerf76d1802006-07-31 23:26:50 +000017240 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000017241
Chris Lattnerf76d1802006-07-31 23:26:50 +000017242 return Res;
17243}