blob: c77355f9179679bb5e81ef7b82ed6e5c86c644e4 [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 Topperb14940a2012-04-22 20:55:18 +000088 SDValue VecIdx = DAG.getConstant(NormalizedIdxVal, MVT::i32);
89 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 Topperb14940a2012-04-22 20:55:18 +0000121 SDValue VecIdx = DAG.getConstant(NormalizedIdxVal, MVT::i32);
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();
Anton Korobeynikovbff66b02008-09-09 18:22:57 +0000164 TD = getTargetData();
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
Michael J. Spencer92bf38c2010-10-10 23:11:06 +0000185 if (Subtarget->isTargetWindows() && !Subtarget->isTargetCygMing()) {
Michael J. Spencer1802a9f2010-10-10 22:04:34 +0000186 // Setup Windows compiler runtime calls.
187 setLibcallName(RTLIB::SDIV_I64, "_alldiv");
Michael J. Spencer335b8062010-10-11 05:29:15 +0000188 setLibcallName(RTLIB::UDIV_I64, "_aulldiv");
Julien Lerougef2960822011-07-08 21:40:25 +0000189 setLibcallName(RTLIB::SREM_I64, "_allrem");
190 setLibcallName(RTLIB::UREM_I64, "_aullrem");
191 setLibcallName(RTLIB::MUL_I64, "_allmul");
Michael J. Spencer1802a9f2010-10-10 22:04:34 +0000192 setLibcallCallingConv(RTLIB::SDIV_I64, CallingConv::X86_StdCall);
Michael J. Spencer335b8062010-10-11 05:29:15 +0000193 setLibcallCallingConv(RTLIB::UDIV_I64, CallingConv::X86_StdCall);
Julien Lerougef2960822011-07-08 21:40:25 +0000194 setLibcallCallingConv(RTLIB::SREM_I64, CallingConv::X86_StdCall);
195 setLibcallCallingConv(RTLIB::UREM_I64, CallingConv::X86_StdCall);
196 setLibcallCallingConv(RTLIB::MUL_I64, CallingConv::X86_StdCall);
Michael J. Spencer1a2d0612012-02-24 19:01:22 +0000197
198 // The _ftol2 runtime function has an unusual calling conv, which
199 // is modeled by a special pseudo-instruction.
200 setLibcallName(RTLIB::FPTOUINT_F64_I64, 0);
201 setLibcallName(RTLIB::FPTOUINT_F32_I64, 0);
202 setLibcallName(RTLIB::FPTOUINT_F64_I32, 0);
203 setLibcallName(RTLIB::FPTOUINT_F32_I32, 0);
Michael J. Spencer1802a9f2010-10-10 22:04:34 +0000204 }
205
Anton Korobeynikovd27a2582006-12-10 23:12:42 +0000206 if (Subtarget->isTargetDarwin()) {
Evan Chengdf57fa02006-03-17 20:31:41 +0000207 // Darwin should use _setjmp/_longjmp instead of setjmp/longjmp.
Anton Korobeynikovd27a2582006-12-10 23:12:42 +0000208 setUseUnderscoreSetJmp(false);
209 setUseUnderscoreLongJmp(false);
Anton Korobeynikov317848f2007-01-03 11:43:14 +0000210 } else if (Subtarget->isTargetMingw()) {
Anton Korobeynikovd27a2582006-12-10 23:12:42 +0000211 // MS runtime is weird: it exports _setjmp, but longjmp!
212 setUseUnderscoreSetJmp(true);
213 setUseUnderscoreLongJmp(false);
214 } else {
215 setUseUnderscoreSetJmp(true);
216 setUseUnderscoreLongJmp(true);
217 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000218
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000219 // Set up the register classes.
Craig Topperc9099502012-04-20 06:31:50 +0000220 addRegisterClass(MVT::i8, &X86::GR8RegClass);
221 addRegisterClass(MVT::i16, &X86::GR16RegClass);
222 addRegisterClass(MVT::i32, &X86::GR32RegClass);
Evan Cheng25ab6902006-09-08 06:48:29 +0000223 if (Subtarget->is64Bit())
Craig Topperc9099502012-04-20 06:31:50 +0000224 addRegisterClass(MVT::i64, &X86::GR64RegClass);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000225
Owen Anderson825b72b2009-08-11 20:47:22 +0000226 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
Evan Chengc5484282006-10-04 00:56:09 +0000227
Scott Michelfdc40a02009-02-17 22:15:04 +0000228 // We don't accept any truncstore of integer registers.
Owen Anderson825b72b2009-08-11 20:47:22 +0000229 setTruncStoreAction(MVT::i64, MVT::i32, Expand);
Dan Gohman71edb242010-04-30 18:30:26 +0000230 setTruncStoreAction(MVT::i64, MVT::i16, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000231 setTruncStoreAction(MVT::i64, MVT::i8 , Expand);
Dan Gohman71edb242010-04-30 18:30:26 +0000232 setTruncStoreAction(MVT::i32, MVT::i16, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000233 setTruncStoreAction(MVT::i32, MVT::i8 , Expand);
234 setTruncStoreAction(MVT::i16, MVT::i8, Expand);
Evan Cheng7f042682008-10-15 02:05:31 +0000235
236 // SETOEQ and SETUNE require checking two conditions.
Owen Anderson825b72b2009-08-11 20:47:22 +0000237 setCondCodeAction(ISD::SETOEQ, MVT::f32, Expand);
238 setCondCodeAction(ISD::SETOEQ, MVT::f64, Expand);
239 setCondCodeAction(ISD::SETOEQ, MVT::f80, Expand);
240 setCondCodeAction(ISD::SETUNE, MVT::f32, Expand);
241 setCondCodeAction(ISD::SETUNE, MVT::f64, Expand);
242 setCondCodeAction(ISD::SETUNE, MVT::f80, Expand);
Chris Lattnerddf89562008-01-17 19:59:44 +0000243
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000244 // Promote all UINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have this
245 // operation.
Owen Anderson825b72b2009-08-11 20:47:22 +0000246 setOperationAction(ISD::UINT_TO_FP , MVT::i1 , Promote);
247 setOperationAction(ISD::UINT_TO_FP , MVT::i8 , Promote);
248 setOperationAction(ISD::UINT_TO_FP , MVT::i16 , Promote);
Evan Cheng6892f282006-01-17 02:32:49 +0000249
Evan Cheng25ab6902006-09-08 06:48:29 +0000250 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000251 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Promote);
Bill Wendling397ae212012-01-05 02:13:20 +0000252 setOperationAction(ISD::UINT_TO_FP , MVT::i64 , Custom);
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000253 } else if (!TM.Options.UseSoftFloat) {
Dale Johannesen8d908eb2010-05-15 18:51:12 +0000254 // We have an algorithm for SSE2->double, and we turn this into a
255 // 64-bit FILD followed by conditional FADD for other targets.
256 setOperationAction(ISD::UINT_TO_FP , MVT::i64 , Custom);
Eli Friedman948e95a2009-05-23 09:59:16 +0000257 // We have an algorithm for SSE2, and we turn this into a 64-bit
258 // FILD for other targets.
Dale Johannesen8d908eb2010-05-15 18:51:12 +0000259 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000260 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000261
262 // Promote i1/i8 SINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have
263 // this operation.
Owen Anderson825b72b2009-08-11 20:47:22 +0000264 setOperationAction(ISD::SINT_TO_FP , MVT::i1 , Promote);
265 setOperationAction(ISD::SINT_TO_FP , MVT::i8 , Promote);
Bill Wendling105be5a2009-03-13 08:41:47 +0000266
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000267 if (!TM.Options.UseSoftFloat) {
Bill Wendling105be5a2009-03-13 08:41:47 +0000268 // SSE has no i16 to fp conversion, only i32
269 if (X86ScalarSSEf32) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000270 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Promote);
Bill Wendling105be5a2009-03-13 08:41:47 +0000271 // f32 and f64 cases are Legal, f80 case is not
Owen Anderson825b72b2009-08-11 20:47:22 +0000272 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Custom);
Bill Wendling105be5a2009-03-13 08:41:47 +0000273 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000274 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Custom);
275 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Custom);
Bill Wendling105be5a2009-03-13 08:41:47 +0000276 }
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000277 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000278 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Promote);
279 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Promote);
Evan Cheng5298bcc2006-02-17 07:01:52 +0000280 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000281
Dale Johannesen73328d12007-09-19 23:55:34 +0000282 // In 32-bit mode these are custom lowered. In 64-bit mode F32 and F64
283 // are Legal, f80 is custom lowered.
Owen Anderson825b72b2009-08-11 20:47:22 +0000284 setOperationAction(ISD::FP_TO_SINT , MVT::i64 , Custom);
285 setOperationAction(ISD::SINT_TO_FP , MVT::i64 , Custom);
Evan Cheng6dab0532006-01-30 08:02:57 +0000286
Evan Cheng02568ff2006-01-30 22:13:22 +0000287 // Promote i1/i8 FP_TO_SINT to larger FP_TO_SINTS's, as X86 doesn't have
288 // this operation.
Owen Anderson825b72b2009-08-11 20:47:22 +0000289 setOperationAction(ISD::FP_TO_SINT , MVT::i1 , Promote);
290 setOperationAction(ISD::FP_TO_SINT , MVT::i8 , Promote);
Evan Cheng02568ff2006-01-30 22:13:22 +0000291
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000292 if (X86ScalarSSEf32) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000293 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Promote);
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000294 // f32 and f64 cases are Legal, f80 case is not
Owen Anderson825b72b2009-08-11 20:47:22 +0000295 setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom);
Evan Cheng02568ff2006-01-30 22:13:22 +0000296 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000297 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Custom);
298 setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000299 }
300
301 // Handle FP_TO_UINT by promoting the destination to a larger signed
302 // conversion.
Owen Anderson825b72b2009-08-11 20:47:22 +0000303 setOperationAction(ISD::FP_TO_UINT , MVT::i1 , Promote);
304 setOperationAction(ISD::FP_TO_UINT , MVT::i8 , Promote);
305 setOperationAction(ISD::FP_TO_UINT , MVT::i16 , Promote);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000306
Evan Cheng25ab6902006-09-08 06:48:29 +0000307 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000308 setOperationAction(ISD::FP_TO_UINT , MVT::i64 , Expand);
309 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Promote);
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000310 } else if (!TM.Options.UseSoftFloat) {
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +0000311 // Since AVX is a superset of SSE3, only check for SSE here.
312 if (Subtarget->hasSSE1() && !Subtarget->hasSSE3())
Evan Cheng25ab6902006-09-08 06:48:29 +0000313 // Expand FP_TO_UINT into a select.
314 // FIXME: We would like to use a Custom expander here eventually to do
315 // the optimal thing for SSE vs. the default expansion in the legalizer.
Owen Anderson825b72b2009-08-11 20:47:22 +0000316 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Expand);
Evan Cheng25ab6902006-09-08 06:48:29 +0000317 else
Eli Friedman948e95a2009-05-23 09:59:16 +0000318 // With SSE3 we can use fisttpll to convert to a signed i64; without
319 // SSE, we're stuck with a fistpll.
Owen Anderson825b72b2009-08-11 20:47:22 +0000320 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000321 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000322
Michael J. Spencer1a2d0612012-02-24 19:01:22 +0000323 if (isTargetFTOL()) {
324 // Use the _ftol2 runtime function, which has a pseudo-instruction
325 // to handle its weird calling convention.
326 setOperationAction(ISD::FP_TO_UINT , MVT::i64 , Custom);
327 }
328
Chris Lattner399610a2006-12-05 18:22:22 +0000329 // TODO: when we have SSE, these could be more efficient, by using movd/movq.
Michael J. Spencerec38de22010-10-10 22:04:20 +0000330 if (!X86ScalarSSEf64) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000331 setOperationAction(ISD::BITCAST , MVT::f32 , Expand);
332 setOperationAction(ISD::BITCAST , MVT::i32 , Expand);
Dale Johannesene39859a2010-05-21 18:40:15 +0000333 if (Subtarget->is64Bit()) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000334 setOperationAction(ISD::BITCAST , MVT::f64 , Expand);
Dale Johannesen0488fb62010-09-30 23:57:10 +0000335 // Without SSE, i64->f64 goes through memory.
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000336 setOperationAction(ISD::BITCAST , MVT::i64 , Expand);
Dale Johannesen7d07b482010-05-21 00:52:33 +0000337 }
Chris Lattnerf3597a12006-12-05 18:45:06 +0000338 }
Chris Lattner21f66852005-12-23 05:15:23 +0000339
Dan Gohmanb00ee212008-02-18 19:34:53 +0000340 // Scalar integer divide and remainder are lowered to use operations that
341 // produce two results, to match the available instructions. This exposes
342 // the two-result form to trivial CSE, which is able to combine x/y and x%y
343 // into a single instruction.
344 //
345 // Scalar integer multiply-high is also lowered to use two-result
346 // operations, to match the available instructions. However, plain multiply
347 // (low) operations are left as Legal, as there are single-result
348 // instructions for this in x86. Using the two-result multiply instructions
349 // when both high and low results are needed must be arranged by dagcombine.
Craig Topper9e401f22012-04-21 18:58:38 +0000350 for (unsigned i = 0; i != array_lengthof(IntVTs); ++i) {
Chris Lattnere019ec12010-12-19 20:07:10 +0000351 MVT VT = IntVTs[i];
352 setOperationAction(ISD::MULHS, VT, Expand);
353 setOperationAction(ISD::MULHU, VT, Expand);
354 setOperationAction(ISD::SDIV, VT, Expand);
355 setOperationAction(ISD::UDIV, VT, Expand);
356 setOperationAction(ISD::SREM, VT, Expand);
357 setOperationAction(ISD::UREM, VT, Expand);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000358
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +0000359 // Add/Sub overflow ops with MVT::Glues are lowered to EFLAGS dependences.
Chris Lattnerd8ff7ec2010-12-20 01:03:27 +0000360 setOperationAction(ISD::ADDC, VT, Custom);
361 setOperationAction(ISD::ADDE, VT, Custom);
362 setOperationAction(ISD::SUBC, VT, Custom);
363 setOperationAction(ISD::SUBE, VT, Custom);
Chris Lattnere019ec12010-12-19 20:07:10 +0000364 }
Dan Gohmana37c9f72007-09-25 18:23:27 +0000365
Owen Anderson825b72b2009-08-11 20:47:22 +0000366 setOperationAction(ISD::BR_JT , MVT::Other, Expand);
367 setOperationAction(ISD::BRCOND , MVT::Other, Custom);
368 setOperationAction(ISD::BR_CC , MVT::Other, Expand);
369 setOperationAction(ISD::SELECT_CC , MVT::Other, Expand);
Evan Cheng25ab6902006-09-08 06:48:29 +0000370 if (Subtarget->is64Bit())
Owen Anderson825b72b2009-08-11 20:47:22 +0000371 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i32, Legal);
372 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16 , Legal);
373 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8 , Legal);
374 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1 , Expand);
375 setOperationAction(ISD::FP_ROUND_INREG , MVT::f32 , Expand);
376 setOperationAction(ISD::FREM , MVT::f32 , Expand);
377 setOperationAction(ISD::FREM , MVT::f64 , Expand);
378 setOperationAction(ISD::FREM , MVT::f80 , Expand);
379 setOperationAction(ISD::FLT_ROUNDS_ , MVT::i32 , Custom);
Scott Michelfdc40a02009-02-17 22:15:04 +0000380
Chandler Carruth77821022011-12-24 12:12:34 +0000381 // Promote the i8 variants and force them on up to i32 which has a shorter
382 // encoding.
383 setOperationAction(ISD::CTTZ , MVT::i8 , Promote);
384 AddPromotedToType (ISD::CTTZ , MVT::i8 , MVT::i32);
385 setOperationAction(ISD::CTTZ_ZERO_UNDEF , MVT::i8 , Promote);
386 AddPromotedToType (ISD::CTTZ_ZERO_UNDEF , MVT::i8 , MVT::i32);
Craig Topper909652f2011-10-14 03:21:46 +0000387 if (Subtarget->hasBMI()) {
Chandler Carruthd873a4b2011-12-24 11:11:38 +0000388 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::i16 , Expand);
389 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::i32 , Expand);
390 if (Subtarget->is64Bit())
391 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::i64, Expand);
Craig Topper909652f2011-10-14 03:21:46 +0000392 } else {
Craig Topper909652f2011-10-14 03:21:46 +0000393 setOperationAction(ISD::CTTZ , MVT::i16 , Custom);
394 setOperationAction(ISD::CTTZ , MVT::i32 , Custom);
395 if (Subtarget->is64Bit())
396 setOperationAction(ISD::CTTZ , MVT::i64 , Custom);
397 }
Craig Topper37f21672011-10-11 06:44:02 +0000398
399 if (Subtarget->hasLZCNT()) {
Chandler Carruth77821022011-12-24 12:12:34 +0000400 // When promoting the i8 variants, force them to i32 for a shorter
401 // encoding.
Craig Topper37f21672011-10-11 06:44:02 +0000402 setOperationAction(ISD::CTLZ , MVT::i8 , Promote);
Chandler Carruth77821022011-12-24 12:12:34 +0000403 AddPromotedToType (ISD::CTLZ , MVT::i8 , MVT::i32);
404 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i8 , Promote);
405 AddPromotedToType (ISD::CTLZ_ZERO_UNDEF, MVT::i8 , MVT::i32);
Chandler Carruthacc068e2011-12-24 10:55:54 +0000406 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i16 , Expand);
407 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i32 , Expand);
408 if (Subtarget->is64Bit())
409 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i64, Expand);
Craig Topper37f21672011-10-11 06:44:02 +0000410 } else {
411 setOperationAction(ISD::CTLZ , MVT::i8 , Custom);
412 setOperationAction(ISD::CTLZ , MVT::i16 , Custom);
413 setOperationAction(ISD::CTLZ , MVT::i32 , Custom);
Chandler Carruthacc068e2011-12-24 10:55:54 +0000414 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i8 , Custom);
415 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i16 , Custom);
416 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i32 , Custom);
417 if (Subtarget->is64Bit()) {
Craig Topper37f21672011-10-11 06:44:02 +0000418 setOperationAction(ISD::CTLZ , MVT::i64 , Custom);
Chandler Carruthacc068e2011-12-24 10:55:54 +0000419 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i64, Custom);
420 }
Evan Cheng25ab6902006-09-08 06:48:29 +0000421 }
422
Benjamin Kramer1292c222010-12-04 20:32:23 +0000423 if (Subtarget->hasPOPCNT()) {
424 setOperationAction(ISD::CTPOP , MVT::i8 , Promote);
425 } else {
426 setOperationAction(ISD::CTPOP , MVT::i8 , Expand);
427 setOperationAction(ISD::CTPOP , MVT::i16 , Expand);
428 setOperationAction(ISD::CTPOP , MVT::i32 , Expand);
429 if (Subtarget->is64Bit())
430 setOperationAction(ISD::CTPOP , MVT::i64 , Expand);
431 }
432
Owen Anderson825b72b2009-08-11 20:47:22 +0000433 setOperationAction(ISD::READCYCLECOUNTER , MVT::i64 , Custom);
434 setOperationAction(ISD::BSWAP , MVT::i16 , Expand);
Nate Begeman35ef9132006-01-11 21:21:00 +0000435
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000436 // These should be promoted to a larger select which is supported.
Dan Gohmancbbea0f2009-08-27 00:14:12 +0000437 setOperationAction(ISD::SELECT , MVT::i1 , Promote);
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000438 // X86 wants to expand cmov itself.
Dan Gohmancbbea0f2009-08-27 00:14:12 +0000439 setOperationAction(ISD::SELECT , MVT::i8 , Custom);
Chris Lattnere019ec12010-12-19 20:07:10 +0000440 setOperationAction(ISD::SELECT , MVT::i16 , Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000441 setOperationAction(ISD::SELECT , MVT::i32 , Custom);
442 setOperationAction(ISD::SELECT , MVT::f32 , Custom);
443 setOperationAction(ISD::SELECT , MVT::f64 , Custom);
444 setOperationAction(ISD::SELECT , MVT::f80 , Custom);
445 setOperationAction(ISD::SETCC , MVT::i8 , Custom);
Dan Gohman71edb242010-04-30 18:30:26 +0000446 setOperationAction(ISD::SETCC , MVT::i16 , Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000447 setOperationAction(ISD::SETCC , MVT::i32 , Custom);
448 setOperationAction(ISD::SETCC , MVT::f32 , Custom);
449 setOperationAction(ISD::SETCC , MVT::f64 , Custom);
450 setOperationAction(ISD::SETCC , MVT::f80 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000451 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000452 setOperationAction(ISD::SELECT , MVT::i64 , Custom);
Andrew Trickf6c39412011-03-23 23:11:02 +0000453 setOperationAction(ISD::SETCC , MVT::i64 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000454 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000455 setOperationAction(ISD::EH_RETURN , MVT::Other, Custom);
Anton Korobeynikov2365f512007-07-14 14:06:15 +0000456
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000457 // Darwin ABI issue.
Owen Anderson825b72b2009-08-11 20:47:22 +0000458 setOperationAction(ISD::ConstantPool , MVT::i32 , Custom);
459 setOperationAction(ISD::JumpTable , MVT::i32 , Custom);
460 setOperationAction(ISD::GlobalAddress , MVT::i32 , Custom);
461 setOperationAction(ISD::GlobalTLSAddress, MVT::i32 , Custom);
Anton Korobeynikov6625eff2008-05-04 21:36:32 +0000462 if (Subtarget->is64Bit())
Owen Anderson825b72b2009-08-11 20:47:22 +0000463 setOperationAction(ISD::GlobalTLSAddress, MVT::i64, Custom);
464 setOperationAction(ISD::ExternalSymbol , MVT::i32 , Custom);
Dan Gohmanf705adb2009-10-30 01:28:02 +0000465 setOperationAction(ISD::BlockAddress , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000466 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000467 setOperationAction(ISD::ConstantPool , MVT::i64 , Custom);
468 setOperationAction(ISD::JumpTable , MVT::i64 , Custom);
469 setOperationAction(ISD::GlobalAddress , MVT::i64 , Custom);
470 setOperationAction(ISD::ExternalSymbol, MVT::i64 , Custom);
Dan Gohmanf705adb2009-10-30 01:28:02 +0000471 setOperationAction(ISD::BlockAddress , MVT::i64 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000472 }
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000473 // 64-bit addm sub, shl, sra, srl (iff 32-bit x86)
Owen Anderson825b72b2009-08-11 20:47:22 +0000474 setOperationAction(ISD::SHL_PARTS , MVT::i32 , Custom);
475 setOperationAction(ISD::SRA_PARTS , MVT::i32 , Custom);
476 setOperationAction(ISD::SRL_PARTS , MVT::i32 , Custom);
Dan Gohman4c1fa612008-03-03 22:22:09 +0000477 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000478 setOperationAction(ISD::SHL_PARTS , MVT::i64 , Custom);
479 setOperationAction(ISD::SRA_PARTS , MVT::i64 , Custom);
480 setOperationAction(ISD::SRL_PARTS , MVT::i64 , Custom);
Dan Gohman4c1fa612008-03-03 22:22:09 +0000481 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000482
Craig Topper1accb7e2012-01-10 06:54:16 +0000483 if (Subtarget->hasSSE1())
Owen Anderson825b72b2009-08-11 20:47:22 +0000484 setOperationAction(ISD::PREFETCH , MVT::Other, Legal);
Evan Cheng27b7db52008-03-08 00:58:38 +0000485
Eric Christopher9a9d2752010-07-22 02:48:34 +0000486 setOperationAction(ISD::MEMBARRIER , MVT::Other, Custom);
Eli Friedman14648462011-07-27 22:21:52 +0000487 setOperationAction(ISD::ATOMIC_FENCE , MVT::Other, Custom);
Michael J. Spencerec38de22010-10-10 22:04:20 +0000488
Jim Grosbachf1ab49e2010-06-23 16:25:07 +0000489 // On X86 and X86-64, atomic operations are lowered to locked instructions.
490 // Locked instructions, in turn, have implicit fence semantics (all memory
491 // operations are flushed before issuing the locked instruction, and they
492 // are not buffered), so we can fold away the common pattern of
493 // fence-atomic-fence.
494 setShouldFoldAtomicFences(true);
Andrew Lenharthd497d9f2008-02-16 14:46:26 +0000495
Mon P Wang63307c32008-05-05 19:05:59 +0000496 // Expand certain atomics
Craig Topper9e401f22012-04-21 18:58:38 +0000497 for (unsigned i = 0; i != array_lengthof(IntVTs); ++i) {
Chris Lattnere019ec12010-12-19 20:07:10 +0000498 MVT VT = IntVTs[i];
499 setOperationAction(ISD::ATOMIC_CMP_SWAP, VT, Custom);
500 setOperationAction(ISD::ATOMIC_LOAD_SUB, VT, Custom);
Eli Friedman327236c2011-08-24 20:50:09 +0000501 setOperationAction(ISD::ATOMIC_STORE, VT, Custom);
Chris Lattnere019ec12010-12-19 20:07:10 +0000502 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000503
Dale Johannesen48c1bc22008-10-02 18:53:47 +0000504 if (!Subtarget->is64Bit()) {
Eli Friedmanf8f90f02011-08-24 22:33:28 +0000505 setOperationAction(ISD::ATOMIC_LOAD, MVT::i64, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000506 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i64, Custom);
507 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i64, Custom);
508 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i64, Custom);
509 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i64, Custom);
510 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i64, Custom);
511 setOperationAction(ISD::ATOMIC_LOAD_NAND, MVT::i64, Custom);
512 setOperationAction(ISD::ATOMIC_SWAP, MVT::i64, Custom);
Dale Johannesen48c1bc22008-10-02 18:53:47 +0000513 }
514
Eli Friedman43f51ae2011-08-26 21:21:21 +0000515 if (Subtarget->hasCmpxchg16b()) {
516 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i128, Custom);
517 }
518
Evan Cheng3c992d22006-03-07 02:02:57 +0000519 // FIXME - use subtarget debug flags
Anton Korobeynikovab4022f2006-10-31 08:31:24 +0000520 if (!Subtarget->isTargetDarwin() &&
521 !Subtarget->isTargetELF() &&
Dan Gohman44066042008-07-01 00:05:16 +0000522 !Subtarget->isTargetCygMing()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000523 setOperationAction(ISD::EH_LABEL, MVT::Other, Expand);
Dan Gohman44066042008-07-01 00:05:16 +0000524 }
Chris Lattnerf73bae12005-11-29 06:16:21 +0000525
Owen Anderson825b72b2009-08-11 20:47:22 +0000526 setOperationAction(ISD::EXCEPTIONADDR, MVT::i64, Expand);
527 setOperationAction(ISD::EHSELECTION, MVT::i64, Expand);
528 setOperationAction(ISD::EXCEPTIONADDR, MVT::i32, Expand);
529 setOperationAction(ISD::EHSELECTION, MVT::i32, Expand);
Anton Korobeynikovce3b4652007-05-02 19:53:33 +0000530 if (Subtarget->is64Bit()) {
Anton Korobeynikovce3b4652007-05-02 19:53:33 +0000531 setExceptionPointerRegister(X86::RAX);
532 setExceptionSelectorRegister(X86::RDX);
533 } else {
534 setExceptionPointerRegister(X86::EAX);
535 setExceptionSelectorRegister(X86::EDX);
536 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000537 setOperationAction(ISD::FRAME_TO_ARGS_OFFSET, MVT::i32, Custom);
538 setOperationAction(ISD::FRAME_TO_ARGS_OFFSET, MVT::i64, Custom);
Anton Korobeynikov260a6b82008-09-08 21:12:11 +0000539
Duncan Sands4a544a72011-09-06 13:37:06 +0000540 setOperationAction(ISD::INIT_TRAMPOLINE, MVT::Other, Custom);
541 setOperationAction(ISD::ADJUST_TRAMPOLINE, MVT::Other, Custom);
Duncan Sandsb116fac2007-07-27 20:02:49 +0000542
Owen Anderson825b72b2009-08-11 20:47:22 +0000543 setOperationAction(ISD::TRAP, MVT::Other, Legal);
Anton Korobeynikov66fac792008-01-15 07:02:33 +0000544
Nate Begemanacc398c2006-01-25 18:21:52 +0000545 // VASTART needs to be custom lowered to use the VarArgsFrameIndex
Owen Anderson825b72b2009-08-11 20:47:22 +0000546 setOperationAction(ISD::VASTART , MVT::Other, Custom);
547 setOperationAction(ISD::VAEND , MVT::Other, Expand);
Dan Gohman9018e832008-05-10 01:26:14 +0000548 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000549 setOperationAction(ISD::VAARG , MVT::Other, Custom);
550 setOperationAction(ISD::VACOPY , MVT::Other, Custom);
Dan Gohman9018e832008-05-10 01:26:14 +0000551 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000552 setOperationAction(ISD::VAARG , MVT::Other, Expand);
553 setOperationAction(ISD::VACOPY , MVT::Other, Expand);
Dan Gohman9018e832008-05-10 01:26:14 +0000554 }
Evan Chengae642192007-03-02 23:16:35 +0000555
Owen Anderson825b72b2009-08-11 20:47:22 +0000556 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
557 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
Eric Christopherc967ad82011-08-31 04:17:21 +0000558
559 if (Subtarget->isTargetCOFF() && !Subtarget->isTargetEnvMacho())
560 setOperationAction(ISD::DYNAMIC_STACKALLOC, Subtarget->is64Bit() ?
561 MVT::i64 : MVT::i32, Custom);
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000562 else if (TM.Options.EnableSegmentedStacks)
Eric Christopherc967ad82011-08-31 04:17:21 +0000563 setOperationAction(ISD::DYNAMIC_STACKALLOC, Subtarget->is64Bit() ?
564 MVT::i64 : MVT::i32, Custom);
565 else
566 setOperationAction(ISD::DYNAMIC_STACKALLOC, Subtarget->is64Bit() ?
567 MVT::i64 : MVT::i32, Expand);
Chris Lattnerb99329e2006-01-13 02:42:53 +0000568
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000569 if (!TM.Options.UseSoftFloat && X86ScalarSSEf64) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000570 // f32 and f64 use SSE.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000571 // Set up the FP register classes.
Craig Topperc9099502012-04-20 06:31:50 +0000572 addRegisterClass(MVT::f32, &X86::FR32RegClass);
573 addRegisterClass(MVT::f64, &X86::FR64RegClass);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000574
Evan Cheng223547a2006-01-31 22:28:30 +0000575 // Use ANDPD to simulate FABS.
Owen Anderson825b72b2009-08-11 20:47:22 +0000576 setOperationAction(ISD::FABS , MVT::f64, Custom);
577 setOperationAction(ISD::FABS , MVT::f32, Custom);
Evan Cheng223547a2006-01-31 22:28:30 +0000578
579 // Use XORP to simulate FNEG.
Owen Anderson825b72b2009-08-11 20:47:22 +0000580 setOperationAction(ISD::FNEG , MVT::f64, Custom);
581 setOperationAction(ISD::FNEG , MVT::f32, Custom);
Evan Cheng223547a2006-01-31 22:28:30 +0000582
Evan Cheng68c47cb2007-01-05 07:55:56 +0000583 // Use ANDPD and ORPD to simulate FCOPYSIGN.
Owen Anderson825b72b2009-08-11 20:47:22 +0000584 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
585 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
Evan Cheng68c47cb2007-01-05 07:55:56 +0000586
Stuart Hastings4fd0dee2011-06-01 04:39:42 +0000587 // Lower this to FGETSIGNx86 plus an AND.
588 setOperationAction(ISD::FGETSIGN, MVT::i64, Custom);
589 setOperationAction(ISD::FGETSIGN, MVT::i32, Custom);
590
Evan Chengd25e9e82006-02-02 00:28:23 +0000591 // We don't support sin/cos/fmod
Owen Anderson825b72b2009-08-11 20:47:22 +0000592 setOperationAction(ISD::FSIN , MVT::f64, Expand);
593 setOperationAction(ISD::FCOS , MVT::f64, Expand);
594 setOperationAction(ISD::FSIN , MVT::f32, Expand);
595 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000596
Chris Lattnera54aa942006-01-29 06:26:08 +0000597 // Expand FP immediates into loads from the stack, except for the special
598 // cases we handle.
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000599 addLegalFPImmediate(APFloat(+0.0)); // xorpd
600 addLegalFPImmediate(APFloat(+0.0f)); // xorps
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000601 } else if (!TM.Options.UseSoftFloat && X86ScalarSSEf32) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000602 // Use SSE for f32, x87 for f64.
603 // Set up the FP register classes.
Craig Topperc9099502012-04-20 06:31:50 +0000604 addRegisterClass(MVT::f32, &X86::FR32RegClass);
605 addRegisterClass(MVT::f64, &X86::RFP64RegClass);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000606
607 // Use ANDPS to simulate FABS.
Owen Anderson825b72b2009-08-11 20:47:22 +0000608 setOperationAction(ISD::FABS , MVT::f32, Custom);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000609
610 // Use XORP to simulate FNEG.
Owen Anderson825b72b2009-08-11 20:47:22 +0000611 setOperationAction(ISD::FNEG , MVT::f32, Custom);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000612
Owen Anderson825b72b2009-08-11 20:47:22 +0000613 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000614
615 // Use ANDPS and ORPS to simulate FCOPYSIGN.
Owen Anderson825b72b2009-08-11 20:47:22 +0000616 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
617 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000618
619 // We don't support sin/cos/fmod
Owen Anderson825b72b2009-08-11 20:47:22 +0000620 setOperationAction(ISD::FSIN , MVT::f32, Expand);
621 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000622
Nate Begemane1795842008-02-14 08:57:00 +0000623 // Special cases we handle for FP constants.
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000624 addLegalFPImmediate(APFloat(+0.0f)); // xorps
625 addLegalFPImmediate(APFloat(+0.0)); // FLD0
626 addLegalFPImmediate(APFloat(+1.0)); // FLD1
627 addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS
628 addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS
629
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000630 if (!TM.Options.UnsafeFPMath) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000631 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
632 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000633 }
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000634 } else if (!TM.Options.UseSoftFloat) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000635 // f32 and f64 in x87.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000636 // Set up the FP register classes.
Craig Topperc9099502012-04-20 06:31:50 +0000637 addRegisterClass(MVT::f64, &X86::RFP64RegClass);
638 addRegisterClass(MVT::f32, &X86::RFP32RegClass);
Anton Korobeynikov12c49af2006-11-21 00:01:06 +0000639
Owen Anderson825b72b2009-08-11 20:47:22 +0000640 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
641 setOperationAction(ISD::UNDEF, MVT::f32, Expand);
642 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
643 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Expand);
Dale Johannesen5411a392007-08-09 01:04:01 +0000644
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000645 if (!TM.Options.UnsafeFPMath) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000646 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
647 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000648 }
Dale Johannesenf04afdb2007-08-30 00:23:21 +0000649 addLegalFPImmediate(APFloat(+0.0)); // FLD0
650 addLegalFPImmediate(APFloat(+1.0)); // FLD1
651 addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS
652 addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000653 addLegalFPImmediate(APFloat(+0.0f)); // FLD0
654 addLegalFPImmediate(APFloat(+1.0f)); // FLD1
655 addLegalFPImmediate(APFloat(-0.0f)); // FLD0/FCHS
656 addLegalFPImmediate(APFloat(-1.0f)); // FLD1/FCHS
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000657 }
Evan Cheng470a6ad2006-02-22 02:26:30 +0000658
Cameron Zwarich33390842011-07-08 21:39:21 +0000659 // We don't support FMA.
660 setOperationAction(ISD::FMA, MVT::f64, Expand);
661 setOperationAction(ISD::FMA, MVT::f32, Expand);
662
Dale Johannesen59a58732007-08-05 18:49:15 +0000663 // Long double always uses X87.
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000664 if (!TM.Options.UseSoftFloat) {
Craig Topperc9099502012-04-20 06:31:50 +0000665 addRegisterClass(MVT::f80, &X86::RFP80RegClass);
Owen Anderson825b72b2009-08-11 20:47:22 +0000666 setOperationAction(ISD::UNDEF, MVT::f80, Expand);
667 setOperationAction(ISD::FCOPYSIGN, MVT::f80, Expand);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000668 {
Benjamin Kramer98383962010-12-04 14:22:24 +0000669 APFloat TmpFlt = APFloat::getZero(APFloat::x87DoubleExtended);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000670 addLegalFPImmediate(TmpFlt); // FLD0
671 TmpFlt.changeSign();
672 addLegalFPImmediate(TmpFlt); // FLD0/FCHS
Benjamin Kramer98383962010-12-04 14:22:24 +0000673
674 bool ignored;
Evan Chengc7ce29b2009-02-13 22:36:38 +0000675 APFloat TmpFlt2(+1.0);
676 TmpFlt2.convert(APFloat::x87DoubleExtended, APFloat::rmNearestTiesToEven,
677 &ignored);
678 addLegalFPImmediate(TmpFlt2); // FLD1
679 TmpFlt2.changeSign();
680 addLegalFPImmediate(TmpFlt2); // FLD1/FCHS
681 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000682
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000683 if (!TM.Options.UnsafeFPMath) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000684 setOperationAction(ISD::FSIN , MVT::f80 , Expand);
685 setOperationAction(ISD::FCOS , MVT::f80 , Expand);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000686 }
Cameron Zwarich33390842011-07-08 21:39:21 +0000687
Owen Anderson4a4fdf32011-12-08 19:32:14 +0000688 setOperationAction(ISD::FFLOOR, MVT::f80, Expand);
689 setOperationAction(ISD::FCEIL, MVT::f80, Expand);
690 setOperationAction(ISD::FTRUNC, MVT::f80, Expand);
691 setOperationAction(ISD::FRINT, MVT::f80, Expand);
692 setOperationAction(ISD::FNEARBYINT, MVT::f80, Expand);
Cameron Zwarich33390842011-07-08 21:39:21 +0000693 setOperationAction(ISD::FMA, MVT::f80, Expand);
Dale Johannesen2f429012007-09-26 21:10:55 +0000694 }
Dale Johannesen59a58732007-08-05 18:49:15 +0000695
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000696 // Always use a library call for pow.
Owen Anderson825b72b2009-08-11 20:47:22 +0000697 setOperationAction(ISD::FPOW , MVT::f32 , Expand);
698 setOperationAction(ISD::FPOW , MVT::f64 , Expand);
699 setOperationAction(ISD::FPOW , MVT::f80 , Expand);
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000700
Owen Anderson825b72b2009-08-11 20:47:22 +0000701 setOperationAction(ISD::FLOG, MVT::f80, Expand);
702 setOperationAction(ISD::FLOG2, MVT::f80, Expand);
703 setOperationAction(ISD::FLOG10, MVT::f80, Expand);
704 setOperationAction(ISD::FEXP, MVT::f80, Expand);
705 setOperationAction(ISD::FEXP2, MVT::f80, Expand);
Dale Johannesen7794f2a2008-09-04 00:47:13 +0000706
Mon P Wangf007a8b2008-11-06 05:31:54 +0000707 // First set operation action for all vector types to either promote
Mon P Wang0c397192008-10-30 08:01:45 +0000708 // (for widening) or expand (for scalarization). Then we will selectively
709 // turn on ones that can be effectively codegen'd.
Jakub Staszak6610b1d2012-04-29 20:52:53 +0000710 for (int VT = MVT::FIRST_VECTOR_VALUETYPE;
711 VT <= MVT::LAST_VECTOR_VALUETYPE; ++VT) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000712 setOperationAction(ISD::ADD , (MVT::SimpleValueType)VT, Expand);
713 setOperationAction(ISD::SUB , (MVT::SimpleValueType)VT, Expand);
714 setOperationAction(ISD::FADD, (MVT::SimpleValueType)VT, Expand);
715 setOperationAction(ISD::FNEG, (MVT::SimpleValueType)VT, Expand);
716 setOperationAction(ISD::FSUB, (MVT::SimpleValueType)VT, Expand);
717 setOperationAction(ISD::MUL , (MVT::SimpleValueType)VT, Expand);
718 setOperationAction(ISD::FMUL, (MVT::SimpleValueType)VT, Expand);
719 setOperationAction(ISD::SDIV, (MVT::SimpleValueType)VT, Expand);
720 setOperationAction(ISD::UDIV, (MVT::SimpleValueType)VT, Expand);
721 setOperationAction(ISD::FDIV, (MVT::SimpleValueType)VT, Expand);
722 setOperationAction(ISD::SREM, (MVT::SimpleValueType)VT, Expand);
723 setOperationAction(ISD::UREM, (MVT::SimpleValueType)VT, Expand);
724 setOperationAction(ISD::LOAD, (MVT::SimpleValueType)VT, Expand);
725 setOperationAction(ISD::VECTOR_SHUFFLE, (MVT::SimpleValueType)VT, Expand);
726 setOperationAction(ISD::EXTRACT_VECTOR_ELT,(MVT::SimpleValueType)VT,Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000727 setOperationAction(ISD::INSERT_VECTOR_ELT,(MVT::SimpleValueType)VT, Expand);
David Greenecfe33c42011-01-26 19:13:22 +0000728 setOperationAction(ISD::EXTRACT_SUBVECTOR,(MVT::SimpleValueType)VT,Expand);
729 setOperationAction(ISD::INSERT_SUBVECTOR,(MVT::SimpleValueType)VT,Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000730 setOperationAction(ISD::FABS, (MVT::SimpleValueType)VT, Expand);
731 setOperationAction(ISD::FSIN, (MVT::SimpleValueType)VT, Expand);
732 setOperationAction(ISD::FCOS, (MVT::SimpleValueType)VT, Expand);
733 setOperationAction(ISD::FREM, (MVT::SimpleValueType)VT, Expand);
Elena Demikhovsky1503aba2012-08-01 12:06:00 +0000734 setOperationAction(ISD::FMA, (MVT::SimpleValueType)VT, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000735 setOperationAction(ISD::FPOWI, (MVT::SimpleValueType)VT, Expand);
736 setOperationAction(ISD::FSQRT, (MVT::SimpleValueType)VT, Expand);
737 setOperationAction(ISD::FCOPYSIGN, (MVT::SimpleValueType)VT, Expand);
738 setOperationAction(ISD::SMUL_LOHI, (MVT::SimpleValueType)VT, Expand);
739 setOperationAction(ISD::UMUL_LOHI, (MVT::SimpleValueType)VT, Expand);
740 setOperationAction(ISD::SDIVREM, (MVT::SimpleValueType)VT, Expand);
741 setOperationAction(ISD::UDIVREM, (MVT::SimpleValueType)VT, Expand);
742 setOperationAction(ISD::FPOW, (MVT::SimpleValueType)VT, Expand);
743 setOperationAction(ISD::CTPOP, (MVT::SimpleValueType)VT, Expand);
744 setOperationAction(ISD::CTTZ, (MVT::SimpleValueType)VT, Expand);
Chandler Carruth63974b22011-12-13 01:56:10 +0000745 setOperationAction(ISD::CTTZ_ZERO_UNDEF, (MVT::SimpleValueType)VT, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000746 setOperationAction(ISD::CTLZ, (MVT::SimpleValueType)VT, Expand);
Chandler Carruth63974b22011-12-13 01:56:10 +0000747 setOperationAction(ISD::CTLZ_ZERO_UNDEF, (MVT::SimpleValueType)VT, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000748 setOperationAction(ISD::SHL, (MVT::SimpleValueType)VT, Expand);
749 setOperationAction(ISD::SRA, (MVT::SimpleValueType)VT, Expand);
750 setOperationAction(ISD::SRL, (MVT::SimpleValueType)VT, Expand);
751 setOperationAction(ISD::ROTL, (MVT::SimpleValueType)VT, Expand);
752 setOperationAction(ISD::ROTR, (MVT::SimpleValueType)VT, Expand);
753 setOperationAction(ISD::BSWAP, (MVT::SimpleValueType)VT, Expand);
Duncan Sands28b77e92011-09-06 19:07:46 +0000754 setOperationAction(ISD::SETCC, (MVT::SimpleValueType)VT, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000755 setOperationAction(ISD::FLOG, (MVT::SimpleValueType)VT, Expand);
756 setOperationAction(ISD::FLOG2, (MVT::SimpleValueType)VT, Expand);
757 setOperationAction(ISD::FLOG10, (MVT::SimpleValueType)VT, Expand);
758 setOperationAction(ISD::FEXP, (MVT::SimpleValueType)VT, Expand);
759 setOperationAction(ISD::FEXP2, (MVT::SimpleValueType)VT, Expand);
760 setOperationAction(ISD::FP_TO_UINT, (MVT::SimpleValueType)VT, Expand);
761 setOperationAction(ISD::FP_TO_SINT, (MVT::SimpleValueType)VT, Expand);
762 setOperationAction(ISD::UINT_TO_FP, (MVT::SimpleValueType)VT, Expand);
763 setOperationAction(ISD::SINT_TO_FP, (MVT::SimpleValueType)VT, Expand);
Dan Gohman87862e72009-12-11 21:31:27 +0000764 setOperationAction(ISD::SIGN_EXTEND_INREG, (MVT::SimpleValueType)VT,Expand);
Dan Gohman2e141d72009-12-14 23:40:38 +0000765 setOperationAction(ISD::TRUNCATE, (MVT::SimpleValueType)VT, Expand);
766 setOperationAction(ISD::SIGN_EXTEND, (MVT::SimpleValueType)VT, Expand);
767 setOperationAction(ISD::ZERO_EXTEND, (MVT::SimpleValueType)VT, Expand);
768 setOperationAction(ISD::ANY_EXTEND, (MVT::SimpleValueType)VT, Expand);
Nadav Rotemaec58612011-09-13 19:17:42 +0000769 setOperationAction(ISD::VSELECT, (MVT::SimpleValueType)VT, Expand);
Jakub Staszak6610b1d2012-04-29 20:52:53 +0000770 for (int InnerVT = MVT::FIRST_VECTOR_VALUETYPE;
771 InnerVT <= MVT::LAST_VECTOR_VALUETYPE; ++InnerVT)
Dan Gohman2e141d72009-12-14 23:40:38 +0000772 setTruncStoreAction((MVT::SimpleValueType)VT,
773 (MVT::SimpleValueType)InnerVT, Expand);
774 setLoadExtAction(ISD::SEXTLOAD, (MVT::SimpleValueType)VT, Expand);
775 setLoadExtAction(ISD::ZEXTLOAD, (MVT::SimpleValueType)VT, Expand);
776 setLoadExtAction(ISD::EXTLOAD, (MVT::SimpleValueType)VT, Expand);
Evan Chengd30bf012006-03-01 01:11:20 +0000777 }
778
Evan Chengc7ce29b2009-02-13 22:36:38 +0000779 // FIXME: In order to prevent SSE instructions being expanded to MMX ones
780 // with -msoft-float, disable use of MMX as well.
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000781 if (!TM.Options.UseSoftFloat && Subtarget->hasMMX()) {
Craig Topperc9099502012-04-20 06:31:50 +0000782 addRegisterClass(MVT::x86mmx, &X86::VR64RegClass);
Dale Johannesen0488fb62010-09-30 23:57:10 +0000783 // No operations on x86mmx supported, everything uses intrinsics.
Evan Cheng470a6ad2006-02-22 02:26:30 +0000784 }
785
Dale Johannesen0488fb62010-09-30 23:57:10 +0000786 // MMX-sized vectors (other than x86mmx) are expected to be expanded
787 // into smaller operations.
788 setOperationAction(ISD::MULHS, MVT::v8i8, Expand);
789 setOperationAction(ISD::MULHS, MVT::v4i16, Expand);
790 setOperationAction(ISD::MULHS, MVT::v2i32, Expand);
791 setOperationAction(ISD::MULHS, MVT::v1i64, Expand);
792 setOperationAction(ISD::AND, MVT::v8i8, Expand);
793 setOperationAction(ISD::AND, MVT::v4i16, Expand);
794 setOperationAction(ISD::AND, MVT::v2i32, Expand);
795 setOperationAction(ISD::AND, MVT::v1i64, Expand);
796 setOperationAction(ISD::OR, MVT::v8i8, Expand);
797 setOperationAction(ISD::OR, MVT::v4i16, Expand);
798 setOperationAction(ISD::OR, MVT::v2i32, Expand);
799 setOperationAction(ISD::OR, MVT::v1i64, Expand);
800 setOperationAction(ISD::XOR, MVT::v8i8, Expand);
801 setOperationAction(ISD::XOR, MVT::v4i16, Expand);
802 setOperationAction(ISD::XOR, MVT::v2i32, Expand);
803 setOperationAction(ISD::XOR, MVT::v1i64, Expand);
804 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i8, Expand);
805 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4i16, Expand);
806 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v2i32, Expand);
807 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v1i64, Expand);
808 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v1i64, Expand);
809 setOperationAction(ISD::SELECT, MVT::v8i8, Expand);
810 setOperationAction(ISD::SELECT, MVT::v4i16, Expand);
811 setOperationAction(ISD::SELECT, MVT::v2i32, Expand);
812 setOperationAction(ISD::SELECT, MVT::v1i64, Expand);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000813 setOperationAction(ISD::BITCAST, MVT::v8i8, Expand);
814 setOperationAction(ISD::BITCAST, MVT::v4i16, Expand);
815 setOperationAction(ISD::BITCAST, MVT::v2i32, Expand);
816 setOperationAction(ISD::BITCAST, MVT::v1i64, Expand);
Dale Johannesen0488fb62010-09-30 23:57:10 +0000817
Craig Topper1accb7e2012-01-10 06:54:16 +0000818 if (!TM.Options.UseSoftFloat && Subtarget->hasSSE1()) {
Craig Topperc9099502012-04-20 06:31:50 +0000819 addRegisterClass(MVT::v4f32, &X86::VR128RegClass);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000820
Owen Anderson825b72b2009-08-11 20:47:22 +0000821 setOperationAction(ISD::FADD, MVT::v4f32, Legal);
822 setOperationAction(ISD::FSUB, MVT::v4f32, Legal);
823 setOperationAction(ISD::FMUL, MVT::v4f32, Legal);
824 setOperationAction(ISD::FDIV, MVT::v4f32, Legal);
825 setOperationAction(ISD::FSQRT, MVT::v4f32, Legal);
826 setOperationAction(ISD::FNEG, MVT::v4f32, Custom);
827 setOperationAction(ISD::LOAD, MVT::v4f32, Legal);
828 setOperationAction(ISD::BUILD_VECTOR, MVT::v4f32, Custom);
829 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4f32, Custom);
830 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
831 setOperationAction(ISD::SELECT, MVT::v4f32, Custom);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000832 }
833
Craig Topper1accb7e2012-01-10 06:54:16 +0000834 if (!TM.Options.UseSoftFloat && Subtarget->hasSSE2()) {
Craig Topperc9099502012-04-20 06:31:50 +0000835 addRegisterClass(MVT::v2f64, &X86::VR128RegClass);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000836
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000837 // FIXME: Unfortunately -soft-float and -no-implicit-float means XMM
838 // registers cannot be used even for integer operations.
Craig Topperc9099502012-04-20 06:31:50 +0000839 addRegisterClass(MVT::v16i8, &X86::VR128RegClass);
840 addRegisterClass(MVT::v8i16, &X86::VR128RegClass);
841 addRegisterClass(MVT::v4i32, &X86::VR128RegClass);
842 addRegisterClass(MVT::v2i64, &X86::VR128RegClass);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000843
Owen Anderson825b72b2009-08-11 20:47:22 +0000844 setOperationAction(ISD::ADD, MVT::v16i8, Legal);
845 setOperationAction(ISD::ADD, MVT::v8i16, Legal);
846 setOperationAction(ISD::ADD, MVT::v4i32, Legal);
847 setOperationAction(ISD::ADD, MVT::v2i64, Legal);
848 setOperationAction(ISD::MUL, MVT::v2i64, Custom);
849 setOperationAction(ISD::SUB, MVT::v16i8, Legal);
850 setOperationAction(ISD::SUB, MVT::v8i16, Legal);
851 setOperationAction(ISD::SUB, MVT::v4i32, Legal);
852 setOperationAction(ISD::SUB, MVT::v2i64, Legal);
853 setOperationAction(ISD::MUL, MVT::v8i16, Legal);
854 setOperationAction(ISD::FADD, MVT::v2f64, Legal);
855 setOperationAction(ISD::FSUB, MVT::v2f64, Legal);
856 setOperationAction(ISD::FMUL, MVT::v2f64, Legal);
857 setOperationAction(ISD::FDIV, MVT::v2f64, Legal);
858 setOperationAction(ISD::FSQRT, MVT::v2f64, Legal);
859 setOperationAction(ISD::FNEG, MVT::v2f64, Custom);
Evan Cheng2c3ae372006-04-12 21:21:57 +0000860
Nadav Rotem354efd82011-09-18 14:57:03 +0000861 setOperationAction(ISD::SETCC, MVT::v2i64, Custom);
Duncan Sands28b77e92011-09-06 19:07:46 +0000862 setOperationAction(ISD::SETCC, MVT::v16i8, Custom);
863 setOperationAction(ISD::SETCC, MVT::v8i16, Custom);
864 setOperationAction(ISD::SETCC, MVT::v4i32, Custom);
Nate Begemanc2616e42008-05-12 20:34:32 +0000865
Owen Anderson825b72b2009-08-11 20:47:22 +0000866 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v16i8, Custom);
867 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i16, Custom);
868 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
869 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom);
870 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
Evan Chengf7c378e2006-04-10 07:23:14 +0000871
Evan Cheng2c3ae372006-04-12 21:21:57 +0000872 // Custom lower build_vector, vector_shuffle, and extract_vector_elt.
Jakub Staszak6610b1d2012-04-29 20:52:53 +0000873 for (int i = MVT::v16i8; i != MVT::v2i64; ++i) {
Craig Topper0d1f1762012-08-12 00:34:56 +0000874 MVT VT = (MVT::SimpleValueType)i;
Nate Begeman844e0f92007-12-11 01:41:33 +0000875 // Do not attempt to custom lower non-power-of-2 vectors
Duncan Sands83ec4b62008-06-06 12:08:01 +0000876 if (!isPowerOf2_32(VT.getVectorNumElements()))
Nate Begeman844e0f92007-12-11 01:41:33 +0000877 continue;
David Greene9b9838d2009-06-29 16:47:10 +0000878 // Do not attempt to custom lower non-128-bit vectors
879 if (!VT.is128BitVector())
880 continue;
Craig Topper0d1f1762012-08-12 00:34:56 +0000881 setOperationAction(ISD::BUILD_VECTOR, VT, Custom);
882 setOperationAction(ISD::VECTOR_SHUFFLE, VT, Custom);
883 setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT, Custom);
Evan Cheng2c3ae372006-04-12 21:21:57 +0000884 }
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000885
Owen Anderson825b72b2009-08-11 20:47:22 +0000886 setOperationAction(ISD::BUILD_VECTOR, MVT::v2f64, Custom);
887 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i64, Custom);
888 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2f64, Custom);
889 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i64, Custom);
890 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2f64, Custom);
891 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2f64, Custom);
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000892
Nate Begemancdd1eec2008-02-12 22:51:28 +0000893 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000894 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i64, Custom);
895 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Custom);
Nate Begemancdd1eec2008-02-12 22:51:28 +0000896 }
Evan Cheng2c3ae372006-04-12 21:21:57 +0000897
Anton Korobeynikov12c49af2006-11-21 00:01:06 +0000898 // Promote v16i8, v8i16, v4i32 load, select, and, or, xor to v2i64.
Craig Topper31a207a2012-05-04 06:39:13 +0000899 for (int i = MVT::v16i8; i != MVT::v2i64; ++i) {
Craig Topper0d1f1762012-08-12 00:34:56 +0000900 MVT VT = (MVT::SimpleValueType)i;
David Greene9b9838d2009-06-29 16:47:10 +0000901
902 // Do not attempt to promote non-128-bit vectors
Chris Lattner32b4b5a2010-07-05 05:53:14 +0000903 if (!VT.is128BitVector())
David Greene9b9838d2009-06-29 16:47:10 +0000904 continue;
Michael J. Spencerec38de22010-10-10 22:04:20 +0000905
Craig Topper0d1f1762012-08-12 00:34:56 +0000906 setOperationAction(ISD::AND, VT, Promote);
907 AddPromotedToType (ISD::AND, VT, MVT::v2i64);
908 setOperationAction(ISD::OR, VT, Promote);
909 AddPromotedToType (ISD::OR, VT, MVT::v2i64);
910 setOperationAction(ISD::XOR, VT, Promote);
911 AddPromotedToType (ISD::XOR, VT, MVT::v2i64);
912 setOperationAction(ISD::LOAD, VT, Promote);
913 AddPromotedToType (ISD::LOAD, VT, MVT::v2i64);
914 setOperationAction(ISD::SELECT, VT, Promote);
915 AddPromotedToType (ISD::SELECT, VT, MVT::v2i64);
Evan Chengf7c378e2006-04-10 07:23:14 +0000916 }
Evan Cheng2c3ae372006-04-12 21:21:57 +0000917
Owen Anderson825b72b2009-08-11 20:47:22 +0000918 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000919
Evan Cheng2c3ae372006-04-12 21:21:57 +0000920 // Custom lower v2i64 and v2f64 selects.
Owen Anderson825b72b2009-08-11 20:47:22 +0000921 setOperationAction(ISD::LOAD, MVT::v2f64, Legal);
922 setOperationAction(ISD::LOAD, MVT::v2i64, Legal);
923 setOperationAction(ISD::SELECT, MVT::v2f64, Custom);
924 setOperationAction(ISD::SELECT, MVT::v2i64, Custom);
Scott Michelfdc40a02009-02-17 22:15:04 +0000925
Owen Anderson825b72b2009-08-11 20:47:22 +0000926 setOperationAction(ISD::FP_TO_SINT, MVT::v4i32, Legal);
927 setOperationAction(ISD::SINT_TO_FP, MVT::v4i32, Legal);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000928 }
Evan Chengc7ce29b2009-02-13 22:36:38 +0000929
Craig Topperd0a31172012-01-10 06:37:29 +0000930 if (Subtarget->hasSSE41()) {
Benjamin Kramerb6533972011-12-09 15:44:03 +0000931 setOperationAction(ISD::FFLOOR, MVT::f32, Legal);
932 setOperationAction(ISD::FCEIL, MVT::f32, Legal);
933 setOperationAction(ISD::FTRUNC, MVT::f32, Legal);
934 setOperationAction(ISD::FRINT, MVT::f32, Legal);
935 setOperationAction(ISD::FNEARBYINT, MVT::f32, Legal);
936 setOperationAction(ISD::FFLOOR, MVT::f64, Legal);
937 setOperationAction(ISD::FCEIL, MVT::f64, Legal);
938 setOperationAction(ISD::FTRUNC, MVT::f64, Legal);
939 setOperationAction(ISD::FRINT, MVT::f64, Legal);
940 setOperationAction(ISD::FNEARBYINT, MVT::f64, Legal);
941
Nate Begeman14d12ca2008-02-11 04:19:36 +0000942 // FIXME: Do we need to handle scalar-to-vector here?
Owen Anderson825b72b2009-08-11 20:47:22 +0000943 setOperationAction(ISD::MUL, MVT::v4i32, Legal);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000944
Nadav Rotemfbad25e2011-09-11 15:02:23 +0000945 setOperationAction(ISD::VSELECT, MVT::v2f64, Legal);
946 setOperationAction(ISD::VSELECT, MVT::v2i64, Legal);
947 setOperationAction(ISD::VSELECT, MVT::v16i8, Legal);
948 setOperationAction(ISD::VSELECT, MVT::v4i32, Legal);
949 setOperationAction(ISD::VSELECT, MVT::v4f32, Legal);
Nadav Rotemffe3e7d2011-09-08 08:11:19 +0000950
Nate Begeman14d12ca2008-02-11 04:19:36 +0000951 // i8 and i16 vectors are custom , because the source register and source
952 // source memory operand types are not the same width. f32 vectors are
953 // custom since the immediate controlling the insert encodes additional
954 // information.
Owen Anderson825b72b2009-08-11 20:47:22 +0000955 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v16i8, Custom);
956 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
957 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom);
958 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000959
Owen Anderson825b72b2009-08-11 20:47:22 +0000960 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v16i8, Custom);
961 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v8i16, Custom);
962 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4i32, Custom);
963 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000964
Pete Coopera77214a2011-11-14 19:38:42 +0000965 // FIXME: these should be Legal but thats only for the case where
Chad Rosier30450e82011-12-22 22:35:21 +0000966 // the index is constant. For now custom expand to deal with that.
Nate Begeman14d12ca2008-02-11 04:19:36 +0000967 if (Subtarget->is64Bit()) {
Pete Coopera77214a2011-11-14 19:38:42 +0000968 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i64, Custom);
969 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Custom);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000970 }
971 }
Evan Cheng470a6ad2006-02-22 02:26:30 +0000972
Craig Topper1accb7e2012-01-10 06:54:16 +0000973 if (Subtarget->hasSSE2()) {
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +0000974 setOperationAction(ISD::SRL, MVT::v8i16, Custom);
Craig Topper7be5dfd2011-11-12 09:58:49 +0000975 setOperationAction(ISD::SRL, MVT::v16i8, Custom);
Nadav Rotem43012222011-05-11 08:12:09 +0000976
Nadav Rotem43012222011-05-11 08:12:09 +0000977 setOperationAction(ISD::SHL, MVT::v8i16, Custom);
Craig Topper7be5dfd2011-11-12 09:58:49 +0000978 setOperationAction(ISD::SHL, MVT::v16i8, Custom);
Nadav Rotem43012222011-05-11 08:12:09 +0000979
Nadav Rotem43012222011-05-11 08:12:09 +0000980 setOperationAction(ISD::SRA, MVT::v8i16, Custom);
Eli Friedmanf6aa6b12011-11-01 21:18:39 +0000981 setOperationAction(ISD::SRA, MVT::v16i8, Custom);
Craig Topper7be5dfd2011-11-12 09:58:49 +0000982
983 if (Subtarget->hasAVX2()) {
984 setOperationAction(ISD::SRL, MVT::v2i64, Legal);
985 setOperationAction(ISD::SRL, MVT::v4i32, Legal);
986
987 setOperationAction(ISD::SHL, MVT::v2i64, Legal);
988 setOperationAction(ISD::SHL, MVT::v4i32, Legal);
989
990 setOperationAction(ISD::SRA, MVT::v4i32, Legal);
991 } else {
992 setOperationAction(ISD::SRL, MVT::v2i64, Custom);
993 setOperationAction(ISD::SRL, MVT::v4i32, Custom);
994
995 setOperationAction(ISD::SHL, MVT::v2i64, Custom);
996 setOperationAction(ISD::SHL, MVT::v4i32, Custom);
997
998 setOperationAction(ISD::SRA, MVT::v4i32, Custom);
999 }
Nadav Rotem43012222011-05-11 08:12:09 +00001000 }
1001
Nick Lewycky8a8d4792011-12-02 22:16:29 +00001002 if (!TM.Options.UseSoftFloat && Subtarget->hasAVX()) {
Craig Topperc9099502012-04-20 06:31:50 +00001003 addRegisterClass(MVT::v32i8, &X86::VR256RegClass);
1004 addRegisterClass(MVT::v16i16, &X86::VR256RegClass);
1005 addRegisterClass(MVT::v8i32, &X86::VR256RegClass);
1006 addRegisterClass(MVT::v8f32, &X86::VR256RegClass);
1007 addRegisterClass(MVT::v4i64, &X86::VR256RegClass);
1008 addRegisterClass(MVT::v4f64, &X86::VR256RegClass);
David Greened94c1012009-06-29 22:50:51 +00001009
Owen Anderson825b72b2009-08-11 20:47:22 +00001010 setOperationAction(ISD::LOAD, MVT::v8f32, Legal);
Owen Anderson825b72b2009-08-11 20:47:22 +00001011 setOperationAction(ISD::LOAD, MVT::v4f64, Legal);
1012 setOperationAction(ISD::LOAD, MVT::v4i64, Legal);
David Greene54d8eba2011-01-27 22:38:56 +00001013
Owen Anderson825b72b2009-08-11 20:47:22 +00001014 setOperationAction(ISD::FADD, MVT::v8f32, Legal);
1015 setOperationAction(ISD::FSUB, MVT::v8f32, Legal);
1016 setOperationAction(ISD::FMUL, MVT::v8f32, Legal);
1017 setOperationAction(ISD::FDIV, MVT::v8f32, Legal);
1018 setOperationAction(ISD::FSQRT, MVT::v8f32, Legal);
1019 setOperationAction(ISD::FNEG, MVT::v8f32, Custom);
David Greene9b9838d2009-06-29 16:47:10 +00001020
Owen Anderson825b72b2009-08-11 20:47:22 +00001021 setOperationAction(ISD::FADD, MVT::v4f64, Legal);
1022 setOperationAction(ISD::FSUB, MVT::v4f64, Legal);
1023 setOperationAction(ISD::FMUL, MVT::v4f64, Legal);
1024 setOperationAction(ISD::FDIV, MVT::v4f64, Legal);
1025 setOperationAction(ISD::FSQRT, MVT::v4f64, Legal);
1026 setOperationAction(ISD::FNEG, MVT::v4f64, Custom);
David Greene9b9838d2009-06-29 16:47:10 +00001027
Bruno Cardoso Lopes2e64ae42011-07-28 01:26:39 +00001028 setOperationAction(ISD::FP_TO_SINT, MVT::v8i32, Legal);
1029 setOperationAction(ISD::SINT_TO_FP, MVT::v8i32, Legal);
Bruno Cardoso Lopes55244ce2011-08-01 21:54:09 +00001030 setOperationAction(ISD::FP_ROUND, MVT::v4f32, Legal);
Bruno Cardoso Lopes2e64ae42011-07-28 01:26:39 +00001031
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +00001032 setOperationAction(ISD::SRL, MVT::v16i16, Custom);
1033 setOperationAction(ISD::SRL, MVT::v32i8, Custom);
1034
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +00001035 setOperationAction(ISD::SHL, MVT::v16i16, Custom);
1036 setOperationAction(ISD::SHL, MVT::v32i8, Custom);
1037
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +00001038 setOperationAction(ISD::SRA, MVT::v16i16, Custom);
Craig Topper7be5dfd2011-11-12 09:58:49 +00001039 setOperationAction(ISD::SRA, MVT::v32i8, Custom);
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +00001040
Duncan Sands28b77e92011-09-06 19:07:46 +00001041 setOperationAction(ISD::SETCC, MVT::v32i8, Custom);
1042 setOperationAction(ISD::SETCC, MVT::v16i16, Custom);
1043 setOperationAction(ISD::SETCC, MVT::v8i32, Custom);
1044 setOperationAction(ISD::SETCC, MVT::v4i64, Custom);
Bruno Cardoso Lopes0f0e0a02011-08-09 00:46:57 +00001045
Bruno Cardoso Lopesd40aa242011-08-09 23:27:13 +00001046 setOperationAction(ISD::SELECT, MVT::v4f64, Custom);
1047 setOperationAction(ISD::SELECT, MVT::v4i64, Custom);
1048 setOperationAction(ISD::SELECT, MVT::v8f32, Custom);
1049
Craig Topperaaa643c2011-11-09 07:28:55 +00001050 setOperationAction(ISD::VSELECT, MVT::v4f64, Legal);
1051 setOperationAction(ISD::VSELECT, MVT::v4i64, Legal);
1052 setOperationAction(ISD::VSELECT, MVT::v8i32, Legal);
1053 setOperationAction(ISD::VSELECT, MVT::v8f32, Legal);
Nadav Rotem8ffad562011-09-09 20:29:17 +00001054
Elena Demikhovsky1503aba2012-08-01 12:06:00 +00001055 if (Subtarget->hasFMA()) {
1056 setOperationAction(ISD::FMA, MVT::v8f32, Custom);
1057 setOperationAction(ISD::FMA, MVT::v4f64, Custom);
1058 setOperationAction(ISD::FMA, MVT::v4f32, Custom);
1059 setOperationAction(ISD::FMA, MVT::v2f64, Custom);
1060 setOperationAction(ISD::FMA, MVT::f32, Custom);
1061 setOperationAction(ISD::FMA, MVT::f64, Custom);
1062 }
Craig Topper880ef452012-08-11 22:34:26 +00001063
Craig Topperaaa643c2011-11-09 07:28:55 +00001064 if (Subtarget->hasAVX2()) {
1065 setOperationAction(ISD::ADD, MVT::v4i64, Legal);
1066 setOperationAction(ISD::ADD, MVT::v8i32, Legal);
1067 setOperationAction(ISD::ADD, MVT::v16i16, Legal);
1068 setOperationAction(ISD::ADD, MVT::v32i8, Legal);
Craig Topper13894fa2011-08-24 06:14:18 +00001069
Craig Topperaaa643c2011-11-09 07:28:55 +00001070 setOperationAction(ISD::SUB, MVT::v4i64, Legal);
1071 setOperationAction(ISD::SUB, MVT::v8i32, Legal);
1072 setOperationAction(ISD::SUB, MVT::v16i16, Legal);
1073 setOperationAction(ISD::SUB, MVT::v32i8, Legal);
Craig Topper13894fa2011-08-24 06:14:18 +00001074
Craig Topperaaa643c2011-11-09 07:28:55 +00001075 setOperationAction(ISD::MUL, MVT::v4i64, Custom);
1076 setOperationAction(ISD::MUL, MVT::v8i32, Legal);
1077 setOperationAction(ISD::MUL, MVT::v16i16, Legal);
Craig Topper46154eb2011-11-11 07:39:23 +00001078 // Don't lower v32i8 because there is no 128-bit byte mul
Nadav Rotembb539bf2011-11-09 13:21:28 +00001079
1080 setOperationAction(ISD::VSELECT, MVT::v32i8, Legal);
Craig Topper7be5dfd2011-11-12 09:58:49 +00001081
1082 setOperationAction(ISD::SRL, MVT::v4i64, Legal);
1083 setOperationAction(ISD::SRL, MVT::v8i32, Legal);
1084
1085 setOperationAction(ISD::SHL, MVT::v4i64, Legal);
1086 setOperationAction(ISD::SHL, MVT::v8i32, Legal);
1087
1088 setOperationAction(ISD::SRA, MVT::v8i32, Legal);
Craig Topperaaa643c2011-11-09 07:28:55 +00001089 } else {
1090 setOperationAction(ISD::ADD, MVT::v4i64, Custom);
1091 setOperationAction(ISD::ADD, MVT::v8i32, Custom);
1092 setOperationAction(ISD::ADD, MVT::v16i16, Custom);
1093 setOperationAction(ISD::ADD, MVT::v32i8, Custom);
1094
1095 setOperationAction(ISD::SUB, MVT::v4i64, Custom);
1096 setOperationAction(ISD::SUB, MVT::v8i32, Custom);
1097 setOperationAction(ISD::SUB, MVT::v16i16, Custom);
1098 setOperationAction(ISD::SUB, MVT::v32i8, Custom);
1099
1100 setOperationAction(ISD::MUL, MVT::v4i64, Custom);
1101 setOperationAction(ISD::MUL, MVT::v8i32, Custom);
1102 setOperationAction(ISD::MUL, MVT::v16i16, Custom);
1103 // Don't lower v32i8 because there is no 128-bit byte mul
Craig Topper7be5dfd2011-11-12 09:58:49 +00001104
1105 setOperationAction(ISD::SRL, MVT::v4i64, Custom);
1106 setOperationAction(ISD::SRL, MVT::v8i32, Custom);
1107
1108 setOperationAction(ISD::SHL, MVT::v4i64, Custom);
1109 setOperationAction(ISD::SHL, MVT::v8i32, Custom);
1110
1111 setOperationAction(ISD::SRA, MVT::v8i32, Custom);
Craig Topperaaa643c2011-11-09 07:28:55 +00001112 }
Craig Topper13894fa2011-08-24 06:14:18 +00001113
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001114 // Custom lower several nodes for 256-bit types.
Jakub Staszak6610b1d2012-04-29 20:52:53 +00001115 for (int i = MVT::FIRST_VECTOR_VALUETYPE;
1116 i <= MVT::LAST_VECTOR_VALUETYPE; ++i) {
Craig Topper0d1f1762012-08-12 00:34:56 +00001117 MVT VT = (MVT::SimpleValueType)i;
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001118
1119 // Extract subvector is special because the value type
1120 // (result) is 128-bit but the source is 256-bit wide.
1121 if (VT.is128BitVector())
Craig Topper0d1f1762012-08-12 00:34:56 +00001122 setOperationAction(ISD::EXTRACT_SUBVECTOR, VT, Custom);
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001123
1124 // Do not attempt to custom lower other non-256-bit vectors
1125 if (!VT.is256BitVector())
David Greene9b9838d2009-06-29 16:47:10 +00001126 continue;
David Greene54d8eba2011-01-27 22:38:56 +00001127
Craig Topper0d1f1762012-08-12 00:34:56 +00001128 setOperationAction(ISD::BUILD_VECTOR, VT, Custom);
1129 setOperationAction(ISD::VECTOR_SHUFFLE, VT, Custom);
1130 setOperationAction(ISD::INSERT_VECTOR_ELT, VT, Custom);
1131 setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT, Custom);
1132 setOperationAction(ISD::SCALAR_TO_VECTOR, VT, Custom);
1133 setOperationAction(ISD::INSERT_SUBVECTOR, VT, Custom);
1134 setOperationAction(ISD::CONCAT_VECTORS, VT, Custom);
David Greene9b9838d2009-06-29 16:47:10 +00001135 }
1136
David Greene54d8eba2011-01-27 22:38:56 +00001137 // Promote v32i8, v16i16, v8i32 select, and, or, xor to v4i64.
Jakub Staszak6610b1d2012-04-29 20:52:53 +00001138 for (int i = MVT::v32i8; i != MVT::v4i64; ++i) {
Craig Topper0d1f1762012-08-12 00:34:56 +00001139 MVT VT = (MVT::SimpleValueType)i;
David Greene54d8eba2011-01-27 22:38:56 +00001140
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001141 // Do not attempt to promote non-256-bit vectors
1142 if (!VT.is256BitVector())
David Greene54d8eba2011-01-27 22:38:56 +00001143 continue;
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001144
Craig Topper0d1f1762012-08-12 00:34:56 +00001145 setOperationAction(ISD::AND, VT, Promote);
1146 AddPromotedToType (ISD::AND, VT, MVT::v4i64);
1147 setOperationAction(ISD::OR, VT, Promote);
1148 AddPromotedToType (ISD::OR, VT, MVT::v4i64);
1149 setOperationAction(ISD::XOR, VT, Promote);
1150 AddPromotedToType (ISD::XOR, VT, MVT::v4i64);
1151 setOperationAction(ISD::LOAD, VT, Promote);
1152 AddPromotedToType (ISD::LOAD, VT, MVT::v4i64);
1153 setOperationAction(ISD::SELECT, VT, Promote);
1154 AddPromotedToType (ISD::SELECT, VT, MVT::v4i64);
David Greene54d8eba2011-01-27 22:38:56 +00001155 }
David Greene9b9838d2009-06-29 16:47:10 +00001156 }
1157
Nadav Rotemd0f3ef82011-07-14 11:11:14 +00001158 // SIGN_EXTEND_INREGs are evaluated by the extend type. Handle the expansion
1159 // of this type with custom code.
Jakub Staszak6610b1d2012-04-29 20:52:53 +00001160 for (int VT = MVT::FIRST_VECTOR_VALUETYPE;
1161 VT != MVT::LAST_VECTOR_VALUETYPE; VT++) {
Chad Rosier30450e82011-12-22 22:35:21 +00001162 setOperationAction(ISD::SIGN_EXTEND_INREG, (MVT::SimpleValueType)VT,
1163 Custom);
Nadav Rotemd0f3ef82011-07-14 11:11:14 +00001164 }
1165
Evan Cheng6be2c582006-04-05 23:38:46 +00001166 // We want to custom lower some of our intrinsics.
Owen Anderson825b72b2009-08-11 20:47:22 +00001167 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
Benjamin Kramerb9bee042012-07-12 09:31:43 +00001168 setOperationAction(ISD::INTRINSIC_W_CHAIN, MVT::Other, Custom);
Evan Cheng6be2c582006-04-05 23:38:46 +00001169
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00001170
Eli Friedman962f5492010-06-02 19:35:46 +00001171 // Only custom-lower 64-bit SADDO and friends on 64-bit because we don't
1172 // handle type legalization for these operations here.
Dan Gohman71c62a22010-06-02 19:13:40 +00001173 //
Eli Friedman962f5492010-06-02 19:35:46 +00001174 // FIXME: We really should do custom legalization for addition and
1175 // subtraction on x86-32 once PR3203 is fixed. We really can't do much better
1176 // than generic legalization for 64-bit multiplication-with-overflow, though.
Chris Lattnera34b3cf2010-12-19 20:03:11 +00001177 for (unsigned i = 0, e = 3+Subtarget->is64Bit(); i != e; ++i) {
1178 // Add/Sub/Mul with overflow operations are custom lowered.
1179 MVT VT = IntVTs[i];
1180 setOperationAction(ISD::SADDO, VT, Custom);
1181 setOperationAction(ISD::UADDO, VT, Custom);
1182 setOperationAction(ISD::SSUBO, VT, Custom);
1183 setOperationAction(ISD::USUBO, VT, Custom);
1184 setOperationAction(ISD::SMULO, VT, Custom);
1185 setOperationAction(ISD::UMULO, VT, Custom);
Eli Friedmana993f0a2010-06-02 00:27:18 +00001186 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00001187
Chris Lattnera34b3cf2010-12-19 20:03:11 +00001188 // There are no 8-bit 3-address imul/mul instructions
1189 setOperationAction(ISD::SMULO, MVT::i8, Expand);
1190 setOperationAction(ISD::UMULO, MVT::i8, Expand);
Bill Wendling41ea7e72008-11-24 19:21:46 +00001191
Evan Chengd54f2d52009-03-31 19:38:51 +00001192 if (!Subtarget->is64Bit()) {
1193 // These libcalls are not available in 32-bit.
1194 setLibcallName(RTLIB::SHL_I128, 0);
1195 setLibcallName(RTLIB::SRL_I128, 0);
1196 setLibcallName(RTLIB::SRA_I128, 0);
1197 }
1198
Evan Cheng206ee9d2006-07-07 08:33:52 +00001199 // We have target-specific dag combine patterns for the following nodes:
1200 setTargetDAGCombine(ISD::VECTOR_SHUFFLE);
Dan Gohman1bbf72b2010-03-15 23:23:03 +00001201 setTargetDAGCombine(ISD::EXTRACT_VECTOR_ELT);
Duncan Sands6bcd2192011-09-17 16:49:39 +00001202 setTargetDAGCombine(ISD::VSELECT);
Chris Lattner83e6c992006-10-04 06:57:07 +00001203 setTargetDAGCombine(ISD::SELECT);
Nate Begeman740ab032009-01-26 00:52:55 +00001204 setTargetDAGCombine(ISD::SHL);
1205 setTargetDAGCombine(ISD::SRA);
1206 setTargetDAGCombine(ISD::SRL);
Evan Cheng760d1942010-01-04 21:22:48 +00001207 setTargetDAGCombine(ISD::OR);
Nate Begemanb65c1752010-12-17 22:55:37 +00001208 setTargetDAGCombine(ISD::AND);
Benjamin Kramer7d6fe132010-12-21 21:41:44 +00001209 setTargetDAGCombine(ISD::ADD);
Duncan Sands17470be2011-09-22 20:15:48 +00001210 setTargetDAGCombine(ISD::FADD);
1211 setTargetDAGCombine(ISD::FSUB);
Elena Demikhovsky1503aba2012-08-01 12:06:00 +00001212 setTargetDAGCombine(ISD::FMA);
Benjamin Kramer7d6fe132010-12-21 21:41:44 +00001213 setTargetDAGCombine(ISD::SUB);
Nadav Rotem91e43fd2011-09-18 10:39:32 +00001214 setTargetDAGCombine(ISD::LOAD);
Chris Lattner149a4e52008-02-22 02:09:43 +00001215 setTargetDAGCombine(ISD::STORE);
Evan Cheng2e489c42009-12-16 00:53:11 +00001216 setTargetDAGCombine(ISD::ZERO_EXTEND);
Elena Demikhovsky1da58672012-04-22 09:39:03 +00001217 setTargetDAGCombine(ISD::ANY_EXTEND);
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +00001218 setTargetDAGCombine(ISD::SIGN_EXTEND);
Elena Demikhovsky3ae98152012-02-01 07:56:44 +00001219 setTargetDAGCombine(ISD::TRUNCATE);
Nadav Rotema3540772012-04-23 21:53:37 +00001220 setTargetDAGCombine(ISD::UINT_TO_FP);
Stuart Hastingsf99a4b82011-06-06 23:15:58 +00001221 setTargetDAGCombine(ISD::SINT_TO_FP);
Chad Rosiera73b6fc2012-04-27 22:33:25 +00001222 setTargetDAGCombine(ISD::SETCC);
Nadav Rotema3540772012-04-23 21:53:37 +00001223 setTargetDAGCombine(ISD::FP_TO_SINT);
Evan Cheng0b0cd912009-03-28 05:57:29 +00001224 if (Subtarget->is64Bit())
1225 setTargetDAGCombine(ISD::MUL);
Manman Ren92363622012-06-07 22:39:10 +00001226 setTargetDAGCombine(ISD::XOR);
Evan Cheng206ee9d2006-07-07 08:33:52 +00001227
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001228 computeRegisterProperties();
1229
Evan Cheng05219282011-01-06 06:52:41 +00001230 // On Darwin, -Os means optimize for size without hurting performance,
1231 // do not reduce the limit.
Dan Gohman87060f52008-06-30 21:00:56 +00001232 maxStoresPerMemset = 16; // For @llvm.memset -> sequence of stores
Evan Cheng05219282011-01-06 06:52:41 +00001233 maxStoresPerMemsetOptSize = Subtarget->isTargetDarwin() ? 16 : 8;
Evan Cheng255f20f2010-04-01 06:04:33 +00001234 maxStoresPerMemcpy = 8; // For @llvm.memcpy -> sequence of stores
Evan Cheng05219282011-01-06 06:52:41 +00001235 maxStoresPerMemcpyOptSize = Subtarget->isTargetDarwin() ? 8 : 4;
1236 maxStoresPerMemmove = 8; // For @llvm.memmove -> sequence of stores
1237 maxStoresPerMemmoveOptSize = Subtarget->isTargetDarwin() ? 8 : 4;
Jakob Stoklund Olesen8c741b82011-12-06 01:26:19 +00001238 setPrefLoopAlignment(4); // 2^4 bytes.
Evan Cheng6ebf7bc2009-05-13 21:42:09 +00001239 benefitFromCodePlacementOpt = true;
Eli Friedmanfc5d3052011-05-06 20:34:06 +00001240
Benjamin Krameraaf723d2012-05-05 12:49:14 +00001241 // Predictable cmov don't hurt on atom because it's in-order.
1242 predictableSelectIsExpensive = !Subtarget->isAtom();
1243
Jakob Stoklund Olesen8c741b82011-12-06 01:26:19 +00001244 setPrefFunctionAlignment(4); // 2^4 bytes.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001245}
1246
Scott Michel5b8f82e2008-03-10 15:42:14 +00001247
Duncan Sands28b77e92011-09-06 19:07:46 +00001248EVT X86TargetLowering::getSetCCResultType(EVT VT) const {
1249 if (!VT.isVector()) return MVT::i8;
1250 return VT.changeVectorElementTypeToInteger();
Scott Michel5b8f82e2008-03-10 15:42:14 +00001251}
1252
1253
Evan Cheng29286502008-01-23 23:17:41 +00001254/// getMaxByValAlign - Helper for getByValTypeAlignment to determine
1255/// the desired ByVal argument alignment.
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001256static void getMaxByValAlign(Type *Ty, unsigned &MaxAlign) {
Evan Cheng29286502008-01-23 23:17:41 +00001257 if (MaxAlign == 16)
1258 return;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001259 if (VectorType *VTy = dyn_cast<VectorType>(Ty)) {
Evan Cheng29286502008-01-23 23:17:41 +00001260 if (VTy->getBitWidth() == 128)
1261 MaxAlign = 16;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001262 } else if (ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
Evan Cheng29286502008-01-23 23:17:41 +00001263 unsigned EltAlign = 0;
1264 getMaxByValAlign(ATy->getElementType(), EltAlign);
1265 if (EltAlign > MaxAlign)
1266 MaxAlign = EltAlign;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001267 } else if (StructType *STy = dyn_cast<StructType>(Ty)) {
Evan Cheng29286502008-01-23 23:17:41 +00001268 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
1269 unsigned EltAlign = 0;
1270 getMaxByValAlign(STy->getElementType(i), EltAlign);
1271 if (EltAlign > MaxAlign)
1272 MaxAlign = EltAlign;
1273 if (MaxAlign == 16)
1274 break;
1275 }
1276 }
Evan Cheng29286502008-01-23 23:17:41 +00001277}
1278
1279/// getByValTypeAlignment - Return the desired alignment for ByVal aggregate
1280/// function arguments in the caller parameter area. For X86, aggregates
Dale Johannesen0c191872008-02-08 19:48:20 +00001281/// that contain SSE vectors are placed at 16-byte boundaries while the rest
1282/// are at 4-byte boundaries.
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001283unsigned X86TargetLowering::getByValTypeAlignment(Type *Ty) const {
Evan Cheng1887c1c2008-08-21 21:00:15 +00001284 if (Subtarget->is64Bit()) {
1285 // Max of 8 and alignment of type.
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00001286 unsigned TyAlign = TD->getABITypeAlignment(Ty);
Evan Cheng1887c1c2008-08-21 21:00:15 +00001287 if (TyAlign > 8)
1288 return TyAlign;
1289 return 8;
1290 }
1291
Evan Cheng29286502008-01-23 23:17:41 +00001292 unsigned Align = 4;
Craig Topper1accb7e2012-01-10 06:54:16 +00001293 if (Subtarget->hasSSE1())
Dale Johannesen0c191872008-02-08 19:48:20 +00001294 getMaxByValAlign(Ty, Align);
Evan Cheng29286502008-01-23 23:17:41 +00001295 return Align;
1296}
Chris Lattner2b02a442007-02-25 08:29:00 +00001297
Evan Chengf0df0312008-05-15 08:39:06 +00001298/// getOptimalMemOpType - Returns the target specific optimal type for load
Evan Chengc3b0c342010-04-08 07:37:57 +00001299/// and store operations as a result of memset, memcpy, and memmove
1300/// lowering. If DstAlign is zero that means it's safe to destination
1301/// alignment can satisfy any constraint. Similarly if SrcAlign is zero it
1302/// means there isn't a need to check it against alignment requirement,
1303/// probably because the source does not need to be loaded. If
Lang Hames15701f82011-10-26 23:50:43 +00001304/// 'IsZeroVal' is true, that means it's safe to return a
Evan Chengc3b0c342010-04-08 07:37:57 +00001305/// non-scalar-integer type, e.g. empty string source, constant, or loaded
1306/// from memory. 'MemcpyStrSrc' indicates whether the memcpy source is
1307/// constant so it does not need to be loaded.
Dan Gohman37f32ee2010-04-16 20:11:05 +00001308/// It returns EVT::Other if the type should be determined using generic
1309/// target-independent logic.
Owen Andersone50ed302009-08-10 22:56:29 +00001310EVT
Evan Cheng255f20f2010-04-01 06:04:33 +00001311X86TargetLowering::getOptimalMemOpType(uint64_t Size,
1312 unsigned DstAlign, unsigned SrcAlign,
Lang Hames15701f82011-10-26 23:50:43 +00001313 bool IsZeroVal,
Evan Chengc3b0c342010-04-08 07:37:57 +00001314 bool MemcpyStrSrc,
Dan Gohman37f32ee2010-04-16 20:11:05 +00001315 MachineFunction &MF) const {
Chris Lattner4002a1b2008-10-28 05:49:35 +00001316 // FIXME: This turns off use of xmm stores for memset/memcpy on targets like
1317 // linux. This is because the stack realignment code can't handle certain
1318 // cases like PR2962. This should be removed when PR2962 is fixed.
Dan Gohman37f32ee2010-04-16 20:11:05 +00001319 const Function *F = MF.getFunction();
Lang Hames15701f82011-10-26 23:50:43 +00001320 if (IsZeroVal &&
Evan Chenga5e13622011-01-07 19:35:30 +00001321 !F->hasFnAttr(Attribute::NoImplicitFloat)) {
Evan Cheng255f20f2010-04-01 06:04:33 +00001322 if (Size >= 16 &&
Evan Chenga5e13622011-01-07 19:35:30 +00001323 (Subtarget->isUnalignedMemAccessFast() ||
1324 ((DstAlign == 0 || DstAlign >= 16) &&
1325 (SrcAlign == 0 || SrcAlign >= 16))) &&
Evan Cheng255f20f2010-04-01 06:04:33 +00001326 Subtarget->getStackAlignment() >= 16) {
Craig Topper562659f2012-01-13 08:32:21 +00001327 if (Subtarget->getStackAlignment() >= 32) {
1328 if (Subtarget->hasAVX2())
1329 return MVT::v8i32;
1330 if (Subtarget->hasAVX())
1331 return MVT::v8f32;
1332 }
Craig Topper1accb7e2012-01-10 06:54:16 +00001333 if (Subtarget->hasSSE2())
Evan Cheng255f20f2010-04-01 06:04:33 +00001334 return MVT::v4i32;
Craig Topper1accb7e2012-01-10 06:54:16 +00001335 if (Subtarget->hasSSE1())
Evan Cheng255f20f2010-04-01 06:04:33 +00001336 return MVT::v4f32;
Evan Chengc3b0c342010-04-08 07:37:57 +00001337 } else if (!MemcpyStrSrc && Size >= 8 &&
Evan Cheng3ea97552010-04-01 20:27:45 +00001338 !Subtarget->is64Bit() &&
Evan Cheng255f20f2010-04-01 06:04:33 +00001339 Subtarget->getStackAlignment() >= 8 &&
Craig Topper1accb7e2012-01-10 06:54:16 +00001340 Subtarget->hasSSE2()) {
Evan Chengc3b0c342010-04-08 07:37:57 +00001341 // Do not use f64 to lower memcpy if source is string constant. It's
1342 // better to use i32 to avoid the loads.
Evan Cheng255f20f2010-04-01 06:04:33 +00001343 return MVT::f64;
Evan Chengc3b0c342010-04-08 07:37:57 +00001344 }
Chris Lattner4002a1b2008-10-28 05:49:35 +00001345 }
Evan Chengf0df0312008-05-15 08:39:06 +00001346 if (Subtarget->is64Bit() && Size >= 8)
Owen Anderson825b72b2009-08-11 20:47:22 +00001347 return MVT::i64;
1348 return MVT::i32;
Evan Chengf0df0312008-05-15 08:39:06 +00001349}
1350
Chris Lattner5e1df8d2010-01-25 23:38:14 +00001351/// getJumpTableEncoding - Return the entry encoding for a jump table in the
1352/// current function. The returned value is a member of the
1353/// MachineJumpTableInfo::JTEntryKind enum.
1354unsigned X86TargetLowering::getJumpTableEncoding() const {
1355 // In GOT pic mode, each entry in the jump table is emitted as a @GOTOFF
1356 // symbol.
1357 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1358 Subtarget->isPICStyleGOT())
Chris Lattnerc64daab2010-01-26 05:02:42 +00001359 return MachineJumpTableInfo::EK_Custom32;
Michael J. Spencerec38de22010-10-10 22:04:20 +00001360
Chris Lattner5e1df8d2010-01-25 23:38:14 +00001361 // Otherwise, use the normal jump table encoding heuristics.
1362 return TargetLowering::getJumpTableEncoding();
1363}
1364
Chris Lattnerc64daab2010-01-26 05:02:42 +00001365const MCExpr *
1366X86TargetLowering::LowerCustomJumpTableEntry(const MachineJumpTableInfo *MJTI,
1367 const MachineBasicBlock *MBB,
1368 unsigned uid,MCContext &Ctx) const{
1369 assert(getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1370 Subtarget->isPICStyleGOT());
1371 // In 32-bit ELF systems, our jump table entries are formed with @GOTOFF
1372 // entries.
Daniel Dunbar4e815f82010-03-15 23:51:06 +00001373 return MCSymbolRefExpr::Create(MBB->getSymbol(),
1374 MCSymbolRefExpr::VK_GOTOFF, Ctx);
Chris Lattnerc64daab2010-01-26 05:02:42 +00001375}
1376
Evan Chengcc415862007-11-09 01:32:10 +00001377/// getPICJumpTableRelocaBase - Returns relocation base for the given PIC
1378/// jumptable.
Dan Gohman475871a2008-07-27 21:46:04 +00001379SDValue X86TargetLowering::getPICJumpTableRelocBase(SDValue Table,
Chris Lattner589c6f62010-01-26 06:28:43 +00001380 SelectionDAG &DAG) const {
Chris Lattnere4df7562009-07-09 03:15:51 +00001381 if (!Subtarget->is64Bit())
Dale Johannesenb300d2a2009-02-07 00:55:49 +00001382 // This doesn't have DebugLoc associated with it, but is not really the
1383 // same as a Register.
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00001384 return DAG.getNode(X86ISD::GlobalBaseReg, DebugLoc(), getPointerTy());
Evan Chengcc415862007-11-09 01:32:10 +00001385 return Table;
1386}
1387
Chris Lattner589c6f62010-01-26 06:28:43 +00001388/// getPICJumpTableRelocBaseExpr - This returns the relocation base for the
1389/// given PIC jumptable, the same as getPICJumpTableRelocBase, but as an
1390/// MCExpr.
1391const MCExpr *X86TargetLowering::
1392getPICJumpTableRelocBaseExpr(const MachineFunction *MF, unsigned JTI,
1393 MCContext &Ctx) const {
1394 // X86-64 uses RIP relative addressing based on the jump table label.
1395 if (Subtarget->isPICStyleRIPRel())
1396 return TargetLowering::getPICJumpTableRelocBaseExpr(MF, JTI, Ctx);
1397
1398 // Otherwise, the reference is relative to the PIC base.
Chris Lattner142b5312010-11-14 22:48:15 +00001399 return MCSymbolRefExpr::Create(MF->getPICBaseSymbol(), Ctx);
Chris Lattner589c6f62010-01-26 06:28:43 +00001400}
1401
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00001402// FIXME: Why this routine is here? Move to RegInfo!
Evan Chengdee81012010-07-26 21:50:05 +00001403std::pair<const TargetRegisterClass*, uint8_t>
1404X86TargetLowering::findRepresentativeClass(EVT VT) const{
1405 const TargetRegisterClass *RRC = 0;
1406 uint8_t Cost = 1;
1407 switch (VT.getSimpleVT().SimpleTy) {
1408 default:
1409 return TargetLowering::findRepresentativeClass(VT);
1410 case MVT::i8: case MVT::i16: case MVT::i32: case MVT::i64:
Craig Topperc9099502012-04-20 06:31:50 +00001411 RRC = Subtarget->is64Bit() ?
1412 (const TargetRegisterClass*)&X86::GR64RegClass :
1413 (const TargetRegisterClass*)&X86::GR32RegClass;
Evan Chengdee81012010-07-26 21:50:05 +00001414 break;
Dale Johannesen0488fb62010-09-30 23:57:10 +00001415 case MVT::x86mmx:
Craig Topperc9099502012-04-20 06:31:50 +00001416 RRC = &X86::VR64RegClass;
Evan Chengdee81012010-07-26 21:50:05 +00001417 break;
1418 case MVT::f32: case MVT::f64:
1419 case MVT::v16i8: case MVT::v8i16: case MVT::v4i32: case MVT::v2i64:
1420 case MVT::v4f32: case MVT::v2f64:
1421 case MVT::v32i8: case MVT::v8i32: case MVT::v4i64: case MVT::v8f32:
1422 case MVT::v4f64:
Craig Topperc9099502012-04-20 06:31:50 +00001423 RRC = &X86::VR128RegClass;
Evan Chengdee81012010-07-26 21:50:05 +00001424 break;
1425 }
1426 return std::make_pair(RRC, Cost);
1427}
1428
Eric Christopherf7a0c7b2010-07-06 05:18:56 +00001429bool X86TargetLowering::getStackCookieLocation(unsigned &AddressSpace,
1430 unsigned &Offset) const {
1431 if (!Subtarget->isTargetLinux())
1432 return false;
1433
1434 if (Subtarget->is64Bit()) {
1435 // %fs:0x28, unless we're using a Kernel code model, in which case it's %gs:
1436 Offset = 0x28;
1437 if (getTargetMachine().getCodeModel() == CodeModel::Kernel)
1438 AddressSpace = 256;
1439 else
1440 AddressSpace = 257;
1441 } else {
1442 // %gs:0x14 on i386
1443 Offset = 0x14;
1444 AddressSpace = 256;
1445 }
1446 return true;
1447}
1448
1449
Chris Lattner2b02a442007-02-25 08:29:00 +00001450//===----------------------------------------------------------------------===//
1451// Return Value Calling Convention Implementation
1452//===----------------------------------------------------------------------===//
1453
Chris Lattner59ed56b2007-02-28 04:55:35 +00001454#include "X86GenCallingConv.inc"
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001455
Michael J. Spencerec38de22010-10-10 22:04:20 +00001456bool
Eric Christopher471e4222011-06-08 23:55:35 +00001457X86TargetLowering::CanLowerReturn(CallingConv::ID CallConv,
Craig Topper0fbf3642012-04-23 03:28:34 +00001458 MachineFunction &MF, bool isVarArg,
Dan Gohman84023e02010-07-10 09:00:22 +00001459 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9af33c2010-07-06 22:19:37 +00001460 LLVMContext &Context) const {
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +00001461 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00001462 CCState CCInfo(CallConv, isVarArg, MF, getTargetMachine(),
Dan Gohmanc9af33c2010-07-06 22:19:37 +00001463 RVLocs, Context);
Dan Gohman84023e02010-07-10 09:00:22 +00001464 return CCInfo.CheckReturn(Outs, RetCC_X86);
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +00001465}
1466
Dan Gohman98ca4f22009-08-05 01:29:28 +00001467SDValue
1468X86TargetLowering::LowerReturn(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001469 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001470 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001471 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohmand858e902010-04-17 15:26:15 +00001472 DebugLoc dl, SelectionDAG &DAG) const {
Dan Gohman1e93df62010-04-17 14:41:14 +00001473 MachineFunction &MF = DAG.getMachineFunction();
1474 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
Scott Michelfdc40a02009-02-17 22:15:04 +00001475
Chris Lattner9774c912007-02-27 05:28:59 +00001476 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00001477 CCState CCInfo(CallConv, isVarArg, MF, getTargetMachine(),
Dan Gohman98ca4f22009-08-05 01:29:28 +00001478 RVLocs, *DAG.getContext());
1479 CCInfo.AnalyzeReturn(Outs, RetCC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001480
Evan Chengdcea1632010-02-04 02:40:39 +00001481 // Add the regs to the liveout set for the function.
1482 MachineRegisterInfo &MRI = DAG.getMachineFunction().getRegInfo();
1483 for (unsigned i = 0; i != RVLocs.size(); ++i)
1484 if (RVLocs[i].isRegLoc() && !MRI.isLiveOut(RVLocs[i].getLocReg()))
1485 MRI.addLiveOut(RVLocs[i].getLocReg());
Scott Michelfdc40a02009-02-17 22:15:04 +00001486
Dan Gohman475871a2008-07-27 21:46:04 +00001487 SDValue Flag;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001488
Dan Gohman475871a2008-07-27 21:46:04 +00001489 SmallVector<SDValue, 6> RetOps;
Chris Lattner447ff682008-03-11 03:23:40 +00001490 RetOps.push_back(Chain); // Operand #0 = Chain (updated below)
1491 // Operand #1 = Bytes To Pop
Dan Gohman1e93df62010-04-17 14:41:14 +00001492 RetOps.push_back(DAG.getTargetConstant(FuncInfo->getBytesToPopOnReturn(),
1493 MVT::i16));
Scott Michelfdc40a02009-02-17 22:15:04 +00001494
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001495 // Copy the result values into the output registers.
Chris Lattner8e6da152008-03-10 21:08:41 +00001496 for (unsigned i = 0; i != RVLocs.size(); ++i) {
1497 CCValAssign &VA = RVLocs[i];
1498 assert(VA.isRegLoc() && "Can only return in registers!");
Dan Gohmanc9403652010-07-07 15:54:55 +00001499 SDValue ValToCopy = OutVals[i];
Dale Johannesenc76d23f2010-07-23 00:30:35 +00001500 EVT ValVT = ValToCopy.getValueType();
1501
Jakob Stoklund Olesenee66b412012-05-31 17:28:20 +00001502 // Promote values to the appropriate types
1503 if (VA.getLocInfo() == CCValAssign::SExt)
1504 ValToCopy = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), ValToCopy);
1505 else if (VA.getLocInfo() == CCValAssign::ZExt)
1506 ValToCopy = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), ValToCopy);
1507 else if (VA.getLocInfo() == CCValAssign::AExt)
1508 ValToCopy = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), ValToCopy);
1509 else if (VA.getLocInfo() == CCValAssign::BCvt)
1510 ValToCopy = DAG.getNode(ISD::BITCAST, dl, VA.getLocVT(), ValToCopy);
1511
Dale Johannesenc4510512010-09-24 19:05:48 +00001512 // If this is x86-64, and we disabled SSE, we can't return FP values,
1513 // or SSE or MMX vectors.
1514 if ((ValVT == MVT::f32 || ValVT == MVT::f64 ||
1515 VA.getLocReg() == X86::XMM0 || VA.getLocReg() == X86::XMM1) &&
Craig Topper1accb7e2012-01-10 06:54:16 +00001516 (Subtarget->is64Bit() && !Subtarget->hasSSE1())) {
Dale Johannesenc76d23f2010-07-23 00:30:35 +00001517 report_fatal_error("SSE register return with SSE disabled");
1518 }
1519 // Likewise we can't return F64 values with SSE1 only. gcc does so, but
1520 // llvm-gcc has never done it right and no one has noticed, so this
1521 // should be OK for now.
1522 if (ValVT == MVT::f64 &&
Craig Topper1accb7e2012-01-10 06:54:16 +00001523 (Subtarget->is64Bit() && !Subtarget->hasSSE2()))
Dale Johannesenc76d23f2010-07-23 00:30:35 +00001524 report_fatal_error("SSE2 register return with SSE2 disabled");
Scott Michelfdc40a02009-02-17 22:15:04 +00001525
Chris Lattner447ff682008-03-11 03:23:40 +00001526 // Returns in ST0/ST1 are handled specially: these are pushed as operands to
1527 // the RET instruction and handled by the FP Stackifier.
Dan Gohman37eed792009-02-04 17:28:58 +00001528 if (VA.getLocReg() == X86::ST0 ||
1529 VA.getLocReg() == X86::ST1) {
Chris Lattner447ff682008-03-11 03:23:40 +00001530 // If this is a copy from an xmm register to ST(0), use an FPExtend to
1531 // change the value to the FP stack register class.
Dan Gohman37eed792009-02-04 17:28:58 +00001532 if (isScalarFPTypeInSSEReg(VA.getValVT()))
Owen Anderson825b72b2009-08-11 20:47:22 +00001533 ValToCopy = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f80, ValToCopy);
Chris Lattner447ff682008-03-11 03:23:40 +00001534 RetOps.push_back(ValToCopy);
1535 // Don't emit a copytoreg.
1536 continue;
1537 }
Dale Johannesena68f9012008-06-24 22:01:44 +00001538
Evan Cheng242b38b2009-02-23 09:03:22 +00001539 // 64-bit vector (MMX) values are returned in XMM0 / XMM1 except for v1i64
1540 // which is returned in RAX / RDX.
Evan Cheng6140a8b2009-02-22 08:05:12 +00001541 if (Subtarget->is64Bit()) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00001542 if (ValVT == MVT::x86mmx) {
Chris Lattner97a2a562010-08-26 05:24:29 +00001543 if (VA.getLocReg() == X86::XMM0 || VA.getLocReg() == X86::XMM1) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001544 ValToCopy = DAG.getNode(ISD::BITCAST, dl, MVT::i64, ValToCopy);
Eric Christopher90eb4022010-07-22 00:26:08 +00001545 ValToCopy = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i64,
1546 ValToCopy);
Chris Lattner97a2a562010-08-26 05:24:29 +00001547 // If we don't have SSE2 available, convert to v4f32 so the generated
1548 // register is legal.
Craig Topper1accb7e2012-01-10 06:54:16 +00001549 if (!Subtarget->hasSSE2())
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001550 ValToCopy = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32,ValToCopy);
Chris Lattner97a2a562010-08-26 05:24:29 +00001551 }
Evan Cheng242b38b2009-02-23 09:03:22 +00001552 }
Evan Cheng6140a8b2009-02-22 08:05:12 +00001553 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00001554
Dale Johannesendd64c412009-02-04 00:33:20 +00001555 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), ValToCopy, Flag);
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001556 Flag = Chain.getValue(1);
1557 }
Dan Gohman61a92132008-04-21 23:59:07 +00001558
1559 // The x86-64 ABI for returning structs by value requires that we copy
1560 // the sret argument into %rax for the return. We saved the argument into
1561 // a virtual register in the entry block, so now we copy the value out
1562 // and into %rax.
1563 if (Subtarget->is64Bit() &&
1564 DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
1565 MachineFunction &MF = DAG.getMachineFunction();
1566 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1567 unsigned Reg = FuncInfo->getSRetReturnReg();
Michael J. Spencerec38de22010-10-10 22:04:20 +00001568 assert(Reg &&
Zhongxing Xuc2798a12010-05-26 08:10:02 +00001569 "SRetReturnReg should have been set in LowerFormalArguments().");
Dale Johannesendd64c412009-02-04 00:33:20 +00001570 SDValue Val = DAG.getCopyFromReg(Chain, dl, Reg, getPointerTy());
Dan Gohman61a92132008-04-21 23:59:07 +00001571
Dale Johannesendd64c412009-02-04 00:33:20 +00001572 Chain = DAG.getCopyToReg(Chain, dl, X86::RAX, Val, Flag);
Dan Gohman61a92132008-04-21 23:59:07 +00001573 Flag = Chain.getValue(1);
Dan Gohman00326812009-10-12 16:36:12 +00001574
1575 // RAX now acts like a return value.
Evan Chengdcea1632010-02-04 02:40:39 +00001576 MRI.addLiveOut(X86::RAX);
Dan Gohman61a92132008-04-21 23:59:07 +00001577 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001578
Chris Lattner447ff682008-03-11 03:23:40 +00001579 RetOps[0] = Chain; // Update chain.
1580
1581 // Add the flag if we have it.
Gabor Greifba36cb52008-08-28 21:40:38 +00001582 if (Flag.getNode())
Chris Lattner447ff682008-03-11 03:23:40 +00001583 RetOps.push_back(Flag);
Scott Michelfdc40a02009-02-17 22:15:04 +00001584
1585 return DAG.getNode(X86ISD::RET_FLAG, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001586 MVT::Other, &RetOps[0], RetOps.size());
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001587}
1588
Evan Chengbf010eb2012-04-10 01:51:00 +00001589bool X86TargetLowering::isUsedByReturnOnly(SDNode *N, SDValue &Chain) const {
Evan Cheng3d2125c2010-11-30 23:55:39 +00001590 if (N->getNumValues() != 1)
1591 return false;
1592 if (!N->hasNUsesOfValue(1, 0))
1593 return false;
1594
Evan Chengbf010eb2012-04-10 01:51:00 +00001595 SDValue TCChain = Chain;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001596 SDNode *Copy = *N->use_begin();
Chad Rosierc8d7eea2012-03-05 19:27:12 +00001597 if (Copy->getOpcode() == ISD::CopyToReg) {
1598 // If the copy has a glue operand, we conservatively assume it isn't safe to
1599 // perform a tail call.
1600 if (Copy->getOperand(Copy->getNumOperands()-1).getValueType() == MVT::Glue)
1601 return false;
Evan Chengbf010eb2012-04-10 01:51:00 +00001602 TCChain = Copy->getOperand(0);
Chad Rosierc8d7eea2012-03-05 19:27:12 +00001603 } else if (Copy->getOpcode() != ISD::FP_EXTEND)
Chad Rosier74bab7f2012-03-02 02:50:46 +00001604 return false;
1605
Evan Cheng1bf891a2010-12-01 22:59:46 +00001606 bool HasRet = false;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001607 for (SDNode::use_iterator UI = Copy->use_begin(), UE = Copy->use_end();
Evan Cheng1bf891a2010-12-01 22:59:46 +00001608 UI != UE; ++UI) {
Evan Cheng3d2125c2010-11-30 23:55:39 +00001609 if (UI->getOpcode() != X86ISD::RET_FLAG)
1610 return false;
Evan Cheng1bf891a2010-12-01 22:59:46 +00001611 HasRet = true;
1612 }
Evan Cheng3d2125c2010-11-30 23:55:39 +00001613
Evan Chengbf010eb2012-04-10 01:51:00 +00001614 if (!HasRet)
1615 return false;
1616
1617 Chain = TCChain;
1618 return true;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001619}
1620
Cameron Zwarich7bbf0ee2011-03-17 14:53:37 +00001621EVT
1622X86TargetLowering::getTypeForExtArgOrReturn(LLVMContext &Context, EVT VT,
Cameron Zwarich44579682011-03-17 14:21:56 +00001623 ISD::NodeType ExtendKind) const {
Cameron Zwarich7bbf0ee2011-03-17 14:53:37 +00001624 MVT ReturnMVT;
Cameron Zwarichebe81732011-03-16 22:20:18 +00001625 // TODO: Is this also valid on 32-bit?
1626 if (Subtarget->is64Bit() && VT == MVT::i1 && ExtendKind == ISD::ZERO_EXTEND)
Cameron Zwarich7bbf0ee2011-03-17 14:53:37 +00001627 ReturnMVT = MVT::i8;
1628 else
1629 ReturnMVT = MVT::i32;
1630
1631 EVT MinVT = getRegisterType(Context, ReturnMVT);
1632 return VT.bitsLT(MinVT) ? MinVT : VT;
Cameron Zwarichebe81732011-03-16 22:20:18 +00001633}
1634
Dan Gohman98ca4f22009-08-05 01:29:28 +00001635/// LowerCallResult - Lower the result values of a call into the
1636/// appropriate copies out of appropriate physical registers.
1637///
1638SDValue
1639X86TargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001640 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001641 const SmallVectorImpl<ISD::InputArg> &Ins,
1642 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001643 SmallVectorImpl<SDValue> &InVals) const {
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001644
Chris Lattnere32bbf62007-02-28 07:09:55 +00001645 // Assign locations to each value returned by this call.
Chris Lattner9774c912007-02-27 05:28:59 +00001646 SmallVector<CCValAssign, 16> RVLocs;
Torok Edwin3f142c32009-02-01 18:15:56 +00001647 bool Is64Bit = Subtarget->is64Bit();
Eric Christopher471e4222011-06-08 23:55:35 +00001648 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
Craig Topper0fbf3642012-04-23 03:28:34 +00001649 getTargetMachine(), RVLocs, *DAG.getContext());
Dan Gohman98ca4f22009-08-05 01:29:28 +00001650 CCInfo.AnalyzeCallResult(Ins, RetCC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001651
Chris Lattner3085e152007-02-25 08:59:22 +00001652 // Copy all of the result registers out of their specified physreg.
Chris Lattner8e6da152008-03-10 21:08:41 +00001653 for (unsigned i = 0; i != RVLocs.size(); ++i) {
Dan Gohman37eed792009-02-04 17:28:58 +00001654 CCValAssign &VA = RVLocs[i];
Owen Andersone50ed302009-08-10 22:56:29 +00001655 EVT CopyVT = VA.getValVT();
Scott Michelfdc40a02009-02-17 22:15:04 +00001656
Torok Edwin3f142c32009-02-01 18:15:56 +00001657 // If this is x86-64, and we disabled SSE, we can't return FP values
Owen Anderson825b72b2009-08-11 20:47:22 +00001658 if ((CopyVT == MVT::f32 || CopyVT == MVT::f64) &&
Craig Topper1accb7e2012-01-10 06:54:16 +00001659 ((Is64Bit || Ins[i].Flags.isInReg()) && !Subtarget->hasSSE1())) {
Chris Lattner75361b62010-04-07 22:58:41 +00001660 report_fatal_error("SSE register return with SSE disabled");
Torok Edwin3f142c32009-02-01 18:15:56 +00001661 }
1662
Evan Cheng79fb3b42009-02-20 20:43:02 +00001663 SDValue Val;
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001664
1665 // If this is a call to a function that returns an fp value on the floating
Sylvestre Ledruc8e41c52012-07-23 08:51:15 +00001666 // point stack, we must guarantee the value is popped from the stack, so
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001667 // a CopyFromReg is not good enough - the copy instruction may be eliminated
Jakob Stoklund Olesen9bbe4d62011-06-28 18:32:28 +00001668 // if the return value is not used. We use the FpPOP_RETVAL instruction
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001669 // instead.
1670 if (VA.getLocReg() == X86::ST0 || VA.getLocReg() == X86::ST1) {
1671 // If we prefer to use the value in xmm registers, copy it out as f80 and
1672 // use a truncate to move it from fp stack reg to xmm reg.
1673 if (isScalarFPTypeInSSEReg(VA.getValVT())) CopyVT = MVT::f80;
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001674 SDValue Ops[] = { Chain, InFlag };
Jakob Stoklund Olesen9bbe4d62011-06-28 18:32:28 +00001675 Chain = SDValue(DAG.getMachineNode(X86::FpPOP_RETVAL, dl, CopyVT,
1676 MVT::Other, MVT::Glue, Ops, 2), 1);
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001677 Val = Chain.getValue(0);
1678
1679 // Round the f80 to the right size, which also moves it to the appropriate
1680 // xmm register.
1681 if (CopyVT != VA.getValVT())
1682 Val = DAG.getNode(ISD::FP_ROUND, dl, VA.getValVT(), Val,
1683 // This truncation won't change the value.
1684 DAG.getIntPtrConstant(1));
Evan Cheng79fb3b42009-02-20 20:43:02 +00001685 } else {
1686 Chain = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(),
1687 CopyVT, InFlag).getValue(1);
1688 Val = Chain.getValue(0);
1689 }
Chris Lattner8e6da152008-03-10 21:08:41 +00001690 InFlag = Chain.getValue(2);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001691 InVals.push_back(Val);
Chris Lattner3085e152007-02-25 08:59:22 +00001692 }
Duncan Sands4bdcb612008-07-02 17:40:58 +00001693
Dan Gohman98ca4f22009-08-05 01:29:28 +00001694 return Chain;
Chris Lattner2b02a442007-02-25 08:29:00 +00001695}
1696
1697
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001698//===----------------------------------------------------------------------===//
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001699// C & StdCall & Fast Calling Convention implementation
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001700//===----------------------------------------------------------------------===//
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001701// StdCall calling convention seems to be standard for many Windows' API
1702// routines and around. It differs from C calling convention just a little:
1703// callee should clean up the stack, not caller. Symbols should be also
1704// decorated in some fancy way :) It doesn't support any vector arguments.
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001705// For info on fast calling convention see Fast Calling Convention (tail call)
1706// implementation LowerX86_32FastCCCallTo.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001707
Dan Gohman98ca4f22009-08-05 01:29:28 +00001708/// CallIsStructReturn - Determines whether a call uses struct return
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001709/// semantics.
Rafael Espindola1cee7102012-07-25 13:41:10 +00001710enum StructReturnType {
1711 NotStructReturn,
1712 RegStructReturn,
1713 StackStructReturn
1714};
1715static StructReturnType
1716callIsStructReturn(const SmallVectorImpl<ISD::OutputArg> &Outs) {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001717 if (Outs.empty())
Rafael Espindola1cee7102012-07-25 13:41:10 +00001718 return NotStructReturn;
Duncan Sands276dcbd2008-03-21 09:14:45 +00001719
Rafael Espindola1cee7102012-07-25 13:41:10 +00001720 const ISD::ArgFlagsTy &Flags = Outs[0].Flags;
1721 if (!Flags.isSRet())
1722 return NotStructReturn;
1723 if (Flags.isInReg())
1724 return RegStructReturn;
1725 return StackStructReturn;
Gordon Henriksen86737662008-01-05 16:56:59 +00001726}
1727
Dan Gohman7e77b0f2009-08-01 19:14:37 +00001728/// ArgsAreStructReturn - Determines whether a function uses struct
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001729/// return semantics.
Rafael Espindola1cee7102012-07-25 13:41:10 +00001730static StructReturnType
1731argsAreStructReturn(const SmallVectorImpl<ISD::InputArg> &Ins) {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001732 if (Ins.empty())
Rafael Espindola1cee7102012-07-25 13:41:10 +00001733 return NotStructReturn;
Duncan Sands276dcbd2008-03-21 09:14:45 +00001734
Rafael Espindola1cee7102012-07-25 13:41:10 +00001735 const ISD::ArgFlagsTy &Flags = Ins[0].Flags;
1736 if (!Flags.isSRet())
1737 return NotStructReturn;
1738 if (Flags.isInReg())
1739 return RegStructReturn;
1740 return StackStructReturn;
Gordon Henriksen86737662008-01-05 16:56:59 +00001741}
1742
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001743/// CreateCopyOfByValArgument - Make a copy of an aggregate at address specified
1744/// by "Src" to address "Dst" with size and alignment information specified by
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001745/// the specific parameter attribute. The copy will be passed as a byval
1746/// function parameter.
Scott Michelfdc40a02009-02-17 22:15:04 +00001747static SDValue
Dan Gohman475871a2008-07-27 21:46:04 +00001748CreateCopyOfByValArgument(SDValue Src, SDValue Dst, SDValue Chain,
Dale Johannesendd64c412009-02-04 00:33:20 +00001749 ISD::ArgFlagsTy Flags, SelectionDAG &DAG,
1750 DebugLoc dl) {
Chris Lattnere72f2022010-09-21 05:40:29 +00001751 SDValue SizeNode = DAG.getConstant(Flags.getByValSize(), MVT::i32);
Michael J. Spencerec38de22010-10-10 22:04:20 +00001752
Dale Johannesendd64c412009-02-04 00:33:20 +00001753 return DAG.getMemcpy(Chain, dl, Dst, Src, SizeNode, Flags.getByValAlign(),
Stuart Hastings03d58262011-03-10 00:25:53 +00001754 /*isVolatile*/false, /*AlwaysInline=*/true,
Chris Lattnerfc448ff2010-09-21 18:51:21 +00001755 MachinePointerInfo(), MachinePointerInfo());
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001756}
1757
Chris Lattner29689432010-03-11 00:22:57 +00001758/// IsTailCallConvention - Return true if the calling convention is one that
1759/// supports tail call optimization.
1760static bool IsTailCallConvention(CallingConv::ID CC) {
1761 return (CC == CallingConv::Fast || CC == CallingConv::GHC);
1762}
1763
Evan Cheng485fafc2011-03-21 01:19:09 +00001764bool X86TargetLowering::mayBeEmittedAsTailCall(CallInst *CI) const {
Nick Lewycky22de16d2012-01-19 00:34:10 +00001765 if (!CI->isTailCall() || getTargetMachine().Options.DisableTailCalls)
Evan Cheng485fafc2011-03-21 01:19:09 +00001766 return false;
1767
1768 CallSite CS(CI);
1769 CallingConv::ID CalleeCC = CS.getCallingConv();
1770 if (!IsTailCallConvention(CalleeCC) && CalleeCC != CallingConv::C)
1771 return false;
1772
1773 return true;
1774}
1775
Evan Cheng0c439eb2010-01-27 00:07:07 +00001776/// FuncIsMadeTailCallSafe - Return true if the function is being made into
1777/// a tailcall target by changing its ABI.
Nick Lewycky8a8d4792011-12-02 22:16:29 +00001778static bool FuncIsMadeTailCallSafe(CallingConv::ID CC,
1779 bool GuaranteedTailCallOpt) {
Chris Lattner29689432010-03-11 00:22:57 +00001780 return GuaranteedTailCallOpt && IsTailCallConvention(CC);
Evan Cheng0c439eb2010-01-27 00:07:07 +00001781}
1782
Dan Gohman98ca4f22009-08-05 01:29:28 +00001783SDValue
1784X86TargetLowering::LowerMemArgument(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001785 CallingConv::ID CallConv,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001786 const SmallVectorImpl<ISD::InputArg> &Ins,
1787 DebugLoc dl, SelectionDAG &DAG,
1788 const CCValAssign &VA,
1789 MachineFrameInfo *MFI,
Dan Gohmand858e902010-04-17 15:26:15 +00001790 unsigned i) const {
Rafael Espindola7effac52007-09-14 15:48:13 +00001791 // Create the nodes corresponding to a load from this parameter slot.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001792 ISD::ArgFlagsTy Flags = Ins[i].Flags;
Nick Lewycky8a8d4792011-12-02 22:16:29 +00001793 bool AlwaysUseMutable = FuncIsMadeTailCallSafe(CallConv,
1794 getTargetMachine().Options.GuaranteedTailCallOpt);
Duncan Sands276dcbd2008-03-21 09:14:45 +00001795 bool isImmutable = !AlwaysUseMutable && !Flags.isByVal();
Anton Korobeynikov22472762009-08-14 18:19:10 +00001796 EVT ValVT;
1797
1798 // If value is passed by pointer we have address passed instead of the value
1799 // itself.
1800 if (VA.getLocInfo() == CCValAssign::Indirect)
1801 ValVT = VA.getLocVT();
1802 else
1803 ValVT = VA.getValVT();
Evan Chenge70bb592008-01-10 02:24:25 +00001804
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001805 // FIXME: For now, all byval parameter objects are marked mutable. This can be
Scott Michelfdc40a02009-02-17 22:15:04 +00001806 // changed with more analysis.
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001807 // In case of tail call optimization mark all arguments mutable. Since they
1808 // could be overwritten by lowering of arguments in case of a tail call.
Evan Cheng90567c32010-02-02 23:58:13 +00001809 if (Flags.isByVal()) {
Evan Chengee2e0e32011-03-30 23:44:13 +00001810 unsigned Bytes = Flags.getByValSize();
1811 if (Bytes == 0) Bytes = 1; // Don't create zero-sized stack objects.
1812 int FI = MFI->CreateFixedObject(Bytes, VA.getLocMemOffset(), isImmutable);
Evan Cheng90567c32010-02-02 23:58:13 +00001813 return DAG.getFrameIndex(FI, getPointerTy());
1814 } else {
1815 int FI = MFI->CreateFixedObject(ValVT.getSizeInBits()/8,
Evan Chenged2ae132010-07-03 00:40:23 +00001816 VA.getLocMemOffset(), isImmutable);
Evan Cheng90567c32010-02-02 23:58:13 +00001817 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
1818 return DAG.getLoad(ValVT, dl, Chain, FIN,
Chris Lattnere8639032010-09-21 06:22:23 +00001819 MachinePointerInfo::getFixedStack(FI),
Pete Cooperd752e0f2011-11-08 18:42:53 +00001820 false, false, false, 0);
Evan Cheng90567c32010-02-02 23:58:13 +00001821 }
Rafael Espindola7effac52007-09-14 15:48:13 +00001822}
1823
Dan Gohman475871a2008-07-27 21:46:04 +00001824SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00001825X86TargetLowering::LowerFormalArguments(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001826 CallingConv::ID CallConv,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001827 bool isVarArg,
1828 const SmallVectorImpl<ISD::InputArg> &Ins,
1829 DebugLoc dl,
1830 SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001831 SmallVectorImpl<SDValue> &InVals)
1832 const {
Evan Cheng1bc78042006-04-26 01:20:17 +00001833 MachineFunction &MF = DAG.getMachineFunction();
Gordon Henriksen86737662008-01-05 16:56:59 +00001834 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
Scott Michelfdc40a02009-02-17 22:15:04 +00001835
Gordon Henriksen86737662008-01-05 16:56:59 +00001836 const Function* Fn = MF.getFunction();
1837 if (Fn->hasExternalLinkage() &&
1838 Subtarget->isTargetCygMing() &&
1839 Fn->getName() == "main")
1840 FuncInfo->setForceFramePointer(true);
1841
Evan Cheng1bc78042006-04-26 01:20:17 +00001842 MachineFrameInfo *MFI = MF.getFrameInfo();
Gordon Henriksen86737662008-01-05 16:56:59 +00001843 bool Is64Bit = Subtarget->is64Bit();
Eli Friedman9a2478a2012-01-20 00:05:46 +00001844 bool IsWindows = Subtarget->isTargetWindows();
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001845 bool IsWin64 = Subtarget->isTargetWin64();
Gordon Henriksenae636f82008-01-03 16:47:34 +00001846
Chris Lattner29689432010-03-11 00:22:57 +00001847 assert(!(isVarArg && IsTailCallConvention(CallConv)) &&
1848 "Var args not supported with calling convention fastcc or ghc");
Gordon Henriksenae636f82008-01-03 16:47:34 +00001849
Chris Lattner638402b2007-02-28 07:00:42 +00001850 // Assign locations to all of the incoming arguments.
Chris Lattnerf39f7712007-02-28 05:46:49 +00001851 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00001852 CCState CCInfo(CallConv, isVarArg, MF, getTargetMachine(),
Dan Gohman98ca4f22009-08-05 01:29:28 +00001853 ArgLocs, *DAG.getContext());
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00001854
1855 // Allocate shadow area for Win64
1856 if (IsWin64) {
1857 CCInfo.AllocateStack(32, 8);
1858 }
1859
Duncan Sands45907662010-10-31 13:21:44 +00001860 CCInfo.AnalyzeFormalArguments(Ins, CC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001861
Chris Lattnerf39f7712007-02-28 05:46:49 +00001862 unsigned LastVal = ~0U;
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001863 SDValue ArgValue;
Chris Lattnerf39f7712007-02-28 05:46:49 +00001864 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1865 CCValAssign &VA = ArgLocs[i];
1866 // TODO: If an arg is passed in two places (e.g. reg and stack), skip later
1867 // places.
1868 assert(VA.getValNo() != LastVal &&
1869 "Don't support value assigned to multiple locs yet");
Duncan Sands17001ce2011-10-18 12:44:00 +00001870 (void)LastVal;
Chris Lattnerf39f7712007-02-28 05:46:49 +00001871 LastVal = VA.getValNo();
Scott Michelfdc40a02009-02-17 22:15:04 +00001872
Chris Lattnerf39f7712007-02-28 05:46:49 +00001873 if (VA.isRegLoc()) {
Owen Andersone50ed302009-08-10 22:56:29 +00001874 EVT RegVT = VA.getLocVT();
Craig Topper44d23822012-02-22 05:59:10 +00001875 const TargetRegisterClass *RC;
Owen Anderson825b72b2009-08-11 20:47:22 +00001876 if (RegVT == MVT::i32)
Craig Topperc9099502012-04-20 06:31:50 +00001877 RC = &X86::GR32RegClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001878 else if (Is64Bit && RegVT == MVT::i64)
Craig Topperc9099502012-04-20 06:31:50 +00001879 RC = &X86::GR64RegClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001880 else if (RegVT == MVT::f32)
Craig Topperc9099502012-04-20 06:31:50 +00001881 RC = &X86::FR32RegClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001882 else if (RegVT == MVT::f64)
Craig Topperc9099502012-04-20 06:31:50 +00001883 RC = &X86::FR64RegClass;
Craig Topper7a9a28b2012-08-12 02:23:29 +00001884 else if (RegVT.is256BitVector())
Craig Topperc9099502012-04-20 06:31:50 +00001885 RC = &X86::VR256RegClass;
Craig Topper7a9a28b2012-08-12 02:23:29 +00001886 else if (RegVT.is128BitVector())
Craig Topperc9099502012-04-20 06:31:50 +00001887 RC = &X86::VR128RegClass;
Dale Johannesen0488fb62010-09-30 23:57:10 +00001888 else if (RegVT == MVT::x86mmx)
Craig Topperc9099502012-04-20 06:31:50 +00001889 RC = &X86::VR64RegClass;
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001890 else
Torok Edwinc23197a2009-07-14 16:55:14 +00001891 llvm_unreachable("Unknown argument type!");
Gordon Henriksenae636f82008-01-03 16:47:34 +00001892
Devang Patel68e6bee2011-02-21 23:21:26 +00001893 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001894 ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT);
Scott Michelfdc40a02009-02-17 22:15:04 +00001895
Chris Lattnerf39f7712007-02-28 05:46:49 +00001896 // If this is an 8 or 16-bit value, it is really passed promoted to 32
1897 // bits. Insert an assert[sz]ext to capture this, then truncate to the
1898 // right size.
1899 if (VA.getLocInfo() == CCValAssign::SExt)
Dale Johannesenace16102009-02-03 19:33:06 +00001900 ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue,
Chris Lattnerf39f7712007-02-28 05:46:49 +00001901 DAG.getValueType(VA.getValVT()));
1902 else if (VA.getLocInfo() == CCValAssign::ZExt)
Dale Johannesenace16102009-02-03 19:33:06 +00001903 ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue,
Chris Lattnerf39f7712007-02-28 05:46:49 +00001904 DAG.getValueType(VA.getValVT()));
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001905 else if (VA.getLocInfo() == CCValAssign::BCvt)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001906 ArgValue = DAG.getNode(ISD::BITCAST, dl, VA.getValVT(), ArgValue);
Scott Michelfdc40a02009-02-17 22:15:04 +00001907
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001908 if (VA.isExtInLoc()) {
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001909 // Handle MMX values passed in XMM regs.
1910 if (RegVT.isVector()) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00001911 ArgValue = DAG.getNode(X86ISD::MOVDQ2Q, dl, VA.getValVT(),
1912 ArgValue);
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001913 } else
1914 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
Evan Cheng44c0fd12008-04-25 20:13:28 +00001915 }
Chris Lattnerf39f7712007-02-28 05:46:49 +00001916 } else {
1917 assert(VA.isMemLoc());
Dan Gohman98ca4f22009-08-05 01:29:28 +00001918 ArgValue = LowerMemArgument(Chain, CallConv, Ins, dl, DAG, VA, MFI, i);
Evan Cheng1bc78042006-04-26 01:20:17 +00001919 }
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001920
1921 // If value is passed via pointer - do a load.
1922 if (VA.getLocInfo() == CCValAssign::Indirect)
Chris Lattner51abfe42010-09-21 06:02:19 +00001923 ArgValue = DAG.getLoad(VA.getValVT(), dl, Chain, ArgValue,
Pete Cooperd752e0f2011-11-08 18:42:53 +00001924 MachinePointerInfo(), false, false, false, 0);
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001925
Dan Gohman98ca4f22009-08-05 01:29:28 +00001926 InVals.push_back(ArgValue);
Evan Cheng1bc78042006-04-26 01:20:17 +00001927 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001928
Dan Gohman61a92132008-04-21 23:59:07 +00001929 // The x86-64 ABI for returning structs by value requires that we copy
1930 // the sret argument into %rax for the return. Save the argument into
1931 // a virtual register so that we can access it from the return points.
Dan Gohman7e77b0f2009-08-01 19:14:37 +00001932 if (Is64Bit && MF.getFunction()->hasStructRetAttr()) {
Dan Gohman61a92132008-04-21 23:59:07 +00001933 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1934 unsigned Reg = FuncInfo->getSRetReturnReg();
1935 if (!Reg) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001936 Reg = MF.getRegInfo().createVirtualRegister(getRegClassFor(MVT::i64));
Dan Gohman61a92132008-04-21 23:59:07 +00001937 FuncInfo->setSRetReturnReg(Reg);
1938 }
Dan Gohman98ca4f22009-08-05 01:29:28 +00001939 SDValue Copy = DAG.getCopyToReg(DAG.getEntryNode(), dl, Reg, InVals[0]);
Owen Anderson825b72b2009-08-11 20:47:22 +00001940 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Copy, Chain);
Dan Gohman61a92132008-04-21 23:59:07 +00001941 }
1942
Chris Lattnerf39f7712007-02-28 05:46:49 +00001943 unsigned StackSize = CCInfo.getNextStackOffset();
Evan Cheng0c439eb2010-01-27 00:07:07 +00001944 // Align stack specially for tail calls.
Nick Lewycky8a8d4792011-12-02 22:16:29 +00001945 if (FuncIsMadeTailCallSafe(CallConv,
1946 MF.getTarget().Options.GuaranteedTailCallOpt))
Gordon Henriksenae636f82008-01-03 16:47:34 +00001947 StackSize = GetAlignedArgumentStackSize(StackSize, DAG);
Evan Cheng25caf632006-05-23 21:06:34 +00001948
Evan Cheng1bc78042006-04-26 01:20:17 +00001949 // If the function takes variable number of arguments, make a frame index for
1950 // the start of the first vararg value... for expansion of llvm.va_start.
Gordon Henriksenae636f82008-01-03 16:47:34 +00001951 if (isVarArg) {
NAKAMURA Takumi3ca99432011-03-09 11:33:15 +00001952 if (Is64Bit || (CallConv != CallingConv::X86_FastCall &&
1953 CallConv != CallingConv::X86_ThisCall)) {
Jakob Stoklund Olesenb2eeed72010-07-29 17:42:27 +00001954 FuncInfo->setVarArgsFrameIndex(MFI->CreateFixedObject(1, StackSize,true));
Gordon Henriksen86737662008-01-05 16:56:59 +00001955 }
1956 if (Is64Bit) {
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001957 unsigned TotalNumIntRegs = 0, TotalNumXMMRegs = 0;
1958
1959 // FIXME: We should really autogenerate these arrays
Craig Topperc5eaae42012-03-11 07:57:25 +00001960 static const uint16_t GPR64ArgRegsWin64[] = {
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001961 X86::RCX, X86::RDX, X86::R8, X86::R9
Gordon Henriksen86737662008-01-05 16:56:59 +00001962 };
Craig Topperc5eaae42012-03-11 07:57:25 +00001963 static const uint16_t GPR64ArgRegs64Bit[] = {
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001964 X86::RDI, X86::RSI, X86::RDX, X86::RCX, X86::R8, X86::R9
1965 };
Craig Topperc5eaae42012-03-11 07:57:25 +00001966 static const uint16_t XMMArgRegs64Bit[] = {
Gordon Henriksen86737662008-01-05 16:56:59 +00001967 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1968 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1969 };
Craig Topperc5eaae42012-03-11 07:57:25 +00001970 const uint16_t *GPR64ArgRegs;
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001971 unsigned NumXMMRegs = 0;
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001972
1973 if (IsWin64) {
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001974 // The XMM registers which might contain var arg parameters are shadowed
1975 // in their paired GPR. So we only need to save the GPR to their home
1976 // slots.
1977 TotalNumIntRegs = 4;
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001978 GPR64ArgRegs = GPR64ArgRegsWin64;
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001979 } else {
1980 TotalNumIntRegs = 6; TotalNumXMMRegs = 8;
1981 GPR64ArgRegs = GPR64ArgRegs64Bit;
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001982
Chad Rosier30450e82011-12-22 22:35:21 +00001983 NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs64Bit,
1984 TotalNumXMMRegs);
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001985 }
1986 unsigned NumIntRegs = CCInfo.getFirstUnallocated(GPR64ArgRegs,
1987 TotalNumIntRegs);
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001988
Devang Patel578efa92009-06-05 21:57:13 +00001989 bool NoImplicitFloatOps = Fn->hasFnAttr(Attribute::NoImplicitFloat);
Craig Topper1accb7e2012-01-10 06:54:16 +00001990 assert(!(NumXMMRegs && !Subtarget->hasSSE1()) &&
Torok Edwin3f142c32009-02-01 18:15:56 +00001991 "SSE register cannot be used when SSE is disabled!");
Nick Lewycky8a8d4792011-12-02 22:16:29 +00001992 assert(!(NumXMMRegs && MF.getTarget().Options.UseSoftFloat &&
1993 NoImplicitFloatOps) &&
Evan Chengc7ce29b2009-02-13 22:36:38 +00001994 "SSE register cannot be used when SSE is disabled!");
Nick Lewycky8a8d4792011-12-02 22:16:29 +00001995 if (MF.getTarget().Options.UseSoftFloat || NoImplicitFloatOps ||
Craig Topper1accb7e2012-01-10 06:54:16 +00001996 !Subtarget->hasSSE1())
Torok Edwin3f142c32009-02-01 18:15:56 +00001997 // Kernel mode asks for SSE to be disabled, so don't push them
1998 // on the stack.
1999 TotalNumXMMRegs = 0;
Bill Wendlingf9abd7e2009-03-11 22:30:01 +00002000
Anton Korobeynikove7beda12010-10-03 22:52:07 +00002001 if (IsWin64) {
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00002002 const TargetFrameLowering &TFI = *getTargetMachine().getFrameLowering();
Cameron Esfahaniec37b002010-10-08 19:24:18 +00002003 // Get to the caller-allocated home save location. Add 8 to account
2004 // for the return address.
2005 int HomeOffset = TFI.getOffsetOfLocalArea() + 8;
Anton Korobeynikove7beda12010-10-03 22:52:07 +00002006 FuncInfo->setRegSaveFrameIndex(
Cameron Esfahaniec37b002010-10-08 19:24:18 +00002007 MFI->CreateFixedObject(1, NumIntRegs * 8 + HomeOffset, false));
NAKAMURA Takumi3ca99432011-03-09 11:33:15 +00002008 // Fixup to set vararg frame on shadow area (4 x i64).
2009 if (NumIntRegs < 4)
2010 FuncInfo->setVarArgsFrameIndex(FuncInfo->getRegSaveFrameIndex());
Anton Korobeynikove7beda12010-10-03 22:52:07 +00002011 } else {
2012 // For X86-64, if there are vararg parameters that are passed via
Chad Rosier30450e82011-12-22 22:35:21 +00002013 // registers, then we must store them to their spots on the stack so
2014 // they may be loaded by deferencing the result of va_next.
Anton Korobeynikove7beda12010-10-03 22:52:07 +00002015 FuncInfo->setVarArgsGPOffset(NumIntRegs * 8);
2016 FuncInfo->setVarArgsFPOffset(TotalNumIntRegs * 8 + NumXMMRegs * 16);
2017 FuncInfo->setRegSaveFrameIndex(
2018 MFI->CreateStackObject(TotalNumIntRegs * 8 + TotalNumXMMRegs * 16, 16,
Dan Gohman1e93df62010-04-17 14:41:14 +00002019 false));
Anton Korobeynikove7beda12010-10-03 22:52:07 +00002020 }
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002021
Gordon Henriksen86737662008-01-05 16:56:59 +00002022 // Store the integer parameter registers.
Dan Gohman475871a2008-07-27 21:46:04 +00002023 SmallVector<SDValue, 8> MemOps;
Dan Gohman1e93df62010-04-17 14:41:14 +00002024 SDValue RSFIN = DAG.getFrameIndex(FuncInfo->getRegSaveFrameIndex(),
2025 getPointerTy());
2026 unsigned Offset = FuncInfo->getVarArgsGPOffset();
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002027 for (; NumIntRegs != TotalNumIntRegs; ++NumIntRegs) {
Dan Gohmand6708ea2009-08-15 01:38:56 +00002028 SDValue FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), RSFIN,
2029 DAG.getIntPtrConstant(Offset));
Bob Wilson998e1252009-04-20 18:36:57 +00002030 unsigned VReg = MF.addLiveIn(GPR64ArgRegs[NumIntRegs],
Craig Topperc9099502012-04-20 06:31:50 +00002031 &X86::GR64RegClass);
Owen Anderson825b72b2009-08-11 20:47:22 +00002032 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i64);
Dan Gohman475871a2008-07-27 21:46:04 +00002033 SDValue Store =
Dale Johannesenace16102009-02-03 19:33:06 +00002034 DAG.getStore(Val.getValue(1), dl, Val, FIN,
Chris Lattnere8639032010-09-21 06:22:23 +00002035 MachinePointerInfo::getFixedStack(
2036 FuncInfo->getRegSaveFrameIndex(), Offset),
2037 false, false, 0);
Gordon Henriksen86737662008-01-05 16:56:59 +00002038 MemOps.push_back(Store);
Dan Gohmand6708ea2009-08-15 01:38:56 +00002039 Offset += 8;
Gordon Henriksen86737662008-01-05 16:56:59 +00002040 }
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002041
Dan Gohmanface41a2009-08-16 21:24:25 +00002042 if (TotalNumXMMRegs != 0 && NumXMMRegs != TotalNumXMMRegs) {
2043 // Now store the XMM (fp + vector) parameter registers.
2044 SmallVector<SDValue, 11> SaveXMMOps;
2045 SaveXMMOps.push_back(Chain);
Dan Gohmand6708ea2009-08-15 01:38:56 +00002046
Craig Topperc9099502012-04-20 06:31:50 +00002047 unsigned AL = MF.addLiveIn(X86::AL, &X86::GR8RegClass);
Dan Gohmanface41a2009-08-16 21:24:25 +00002048 SDValue ALVal = DAG.getCopyFromReg(DAG.getEntryNode(), dl, AL, MVT::i8);
2049 SaveXMMOps.push_back(ALVal);
Dan Gohmand6708ea2009-08-15 01:38:56 +00002050
Dan Gohman1e93df62010-04-17 14:41:14 +00002051 SaveXMMOps.push_back(DAG.getIntPtrConstant(
2052 FuncInfo->getRegSaveFrameIndex()));
2053 SaveXMMOps.push_back(DAG.getIntPtrConstant(
2054 FuncInfo->getVarArgsFPOffset()));
Dan Gohmand6708ea2009-08-15 01:38:56 +00002055
Dan Gohmanface41a2009-08-16 21:24:25 +00002056 for (; NumXMMRegs != TotalNumXMMRegs; ++NumXMMRegs) {
Anton Korobeynikove7beda12010-10-03 22:52:07 +00002057 unsigned VReg = MF.addLiveIn(XMMArgRegs64Bit[NumXMMRegs],
Craig Topperc9099502012-04-20 06:31:50 +00002058 &X86::VR128RegClass);
Dan Gohmanface41a2009-08-16 21:24:25 +00002059 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::v4f32);
2060 SaveXMMOps.push_back(Val);
2061 }
2062 MemOps.push_back(DAG.getNode(X86ISD::VASTART_SAVE_XMM_REGS, dl,
2063 MVT::Other,
2064 &SaveXMMOps[0], SaveXMMOps.size()));
Gordon Henriksen86737662008-01-05 16:56:59 +00002065 }
Dan Gohmanface41a2009-08-16 21:24:25 +00002066
2067 if (!MemOps.empty())
2068 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
2069 &MemOps[0], MemOps.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00002070 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00002071 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002072
Gordon Henriksen86737662008-01-05 16:56:59 +00002073 // Some CCs need callee pop.
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002074 if (X86::isCalleePop(CallConv, Is64Bit, isVarArg,
2075 MF.getTarget().Options.GuaranteedTailCallOpt)) {
Dan Gohman1e93df62010-04-17 14:41:14 +00002076 FuncInfo->setBytesToPopOnReturn(StackSize); // Callee pops everything.
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00002077 } else {
Dan Gohman1e93df62010-04-17 14:41:14 +00002078 FuncInfo->setBytesToPopOnReturn(0); // Callee pops nothing.
Chris Lattnerf39f7712007-02-28 05:46:49 +00002079 // If this is an sret function, the return should pop the hidden pointer.
Eli Friedman9a2478a2012-01-20 00:05:46 +00002080 if (!Is64Bit && !IsTailCallConvention(CallConv) && !IsWindows &&
Rafael Espindola1cee7102012-07-25 13:41:10 +00002081 argsAreStructReturn(Ins) == StackStructReturn)
Dan Gohman1e93df62010-04-17 14:41:14 +00002082 FuncInfo->setBytesToPopOnReturn(4);
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00002083 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00002084
Gordon Henriksen86737662008-01-05 16:56:59 +00002085 if (!Is64Bit) {
Dan Gohman1e93df62010-04-17 14:41:14 +00002086 // RegSaveFrameIndex is X86-64 only.
2087 FuncInfo->setRegSaveFrameIndex(0xAAAAAAA);
Anton Korobeynikovded05e32010-05-16 09:08:45 +00002088 if (CallConv == CallingConv::X86_FastCall ||
2089 CallConv == CallingConv::X86_ThisCall)
Dan Gohman1e93df62010-04-17 14:41:14 +00002090 // fastcc functions can't have varargs.
2091 FuncInfo->setVarArgsFrameIndex(0xAAAAAAA);
Gordon Henriksen86737662008-01-05 16:56:59 +00002092 }
Evan Cheng25caf632006-05-23 21:06:34 +00002093
Rafael Espindola76927d752011-08-30 19:39:58 +00002094 FuncInfo->setArgumentStackSize(StackSize);
2095
Dan Gohman98ca4f22009-08-05 01:29:28 +00002096 return Chain;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002097}
2098
Dan Gohman475871a2008-07-27 21:46:04 +00002099SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00002100X86TargetLowering::LowerMemOpCallTo(SDValue Chain,
2101 SDValue StackPtr, SDValue Arg,
2102 DebugLoc dl, SelectionDAG &DAG,
Evan Chengdffbd832008-01-10 00:09:10 +00002103 const CCValAssign &VA,
Dan Gohmand858e902010-04-17 15:26:15 +00002104 ISD::ArgFlagsTy Flags) const {
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00002105 unsigned LocMemOffset = VA.getLocMemOffset();
Dan Gohman475871a2008-07-27 21:46:04 +00002106 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset);
Dale Johannesenace16102009-02-03 19:33:06 +00002107 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, PtrOff);
Chris Lattnerfc448ff2010-09-21 18:51:21 +00002108 if (Flags.isByVal())
Dale Johannesendd64c412009-02-04 00:33:20 +00002109 return CreateCopyOfByValArgument(Arg, PtrOff, Chain, Flags, DAG, dl);
Chris Lattnerfc448ff2010-09-21 18:51:21 +00002110
2111 return DAG.getStore(Chain, dl, Arg, PtrOff,
2112 MachinePointerInfo::getStack(LocMemOffset),
David Greene67c9d422010-02-15 16:53:33 +00002113 false, false, 0);
Evan Chengdffbd832008-01-10 00:09:10 +00002114}
2115
Bill Wendling64e87322009-01-16 19:25:27 +00002116/// EmitTailCallLoadRetAddr - Emit a load of return address if tail call
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002117/// optimization is performed and it is required.
Scott Michelfdc40a02009-02-17 22:15:04 +00002118SDValue
2119X86TargetLowering::EmitTailCallLoadRetAddr(SelectionDAG &DAG,
Evan Chengddc419c2010-01-26 19:04:47 +00002120 SDValue &OutRetAddr, SDValue Chain,
2121 bool IsTailCall, bool Is64Bit,
Dan Gohmand858e902010-04-17 15:26:15 +00002122 int FPDiff, DebugLoc dl) const {
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002123 // Adjust the Return address stack slot.
Owen Andersone50ed302009-08-10 22:56:29 +00002124 EVT VT = getPointerTy();
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002125 OutRetAddr = getReturnAddressFrameIndex(DAG);
Bill Wendling64e87322009-01-16 19:25:27 +00002126
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002127 // Load the "old" Return address.
Chris Lattner51abfe42010-09-21 06:02:19 +00002128 OutRetAddr = DAG.getLoad(VT, dl, Chain, OutRetAddr, MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002129 false, false, false, 0);
Gabor Greifba36cb52008-08-28 21:40:38 +00002130 return SDValue(OutRetAddr.getNode(), 1);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002131}
2132
Chris Lattner7a2bdde2011-04-15 05:18:47 +00002133/// EmitTailCallStoreRetAddr - Emit a store of the return address if tail call
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002134/// optimization is performed and it is required (FPDiff!=0).
Scott Michelfdc40a02009-02-17 22:15:04 +00002135static SDValue
2136EmitTailCallStoreRetAddr(SelectionDAG & DAG, MachineFunction &MF,
Dan Gohman475871a2008-07-27 21:46:04 +00002137 SDValue Chain, SDValue RetAddrFrIdx,
Dale Johannesenace16102009-02-03 19:33:06 +00002138 bool Is64Bit, int FPDiff, DebugLoc dl) {
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002139 // Store the return address to the appropriate stack slot.
2140 if (!FPDiff) return Chain;
2141 // Calculate the new stack slot for the return address.
2142 int SlotSize = Is64Bit ? 8 : 4;
Scott Michelfdc40a02009-02-17 22:15:04 +00002143 int NewReturnAddrFI =
Evan Chenged2ae132010-07-03 00:40:23 +00002144 MF.getFrameInfo()->CreateFixedObject(SlotSize, FPDiff-SlotSize, false);
Owen Anderson825b72b2009-08-11 20:47:22 +00002145 EVT VT = Is64Bit ? MVT::i64 : MVT::i32;
Dan Gohman475871a2008-07-27 21:46:04 +00002146 SDValue NewRetAddrFrIdx = DAG.getFrameIndex(NewReturnAddrFI, VT);
Scott Michelfdc40a02009-02-17 22:15:04 +00002147 Chain = DAG.getStore(Chain, dl, RetAddrFrIdx, NewRetAddrFrIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00002148 MachinePointerInfo::getFixedStack(NewReturnAddrFI),
David Greene67c9d422010-02-15 16:53:33 +00002149 false, false, 0);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002150 return Chain;
2151}
2152
Dan Gohman98ca4f22009-08-05 01:29:28 +00002153SDValue
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00002154X86TargetLowering::LowerCall(TargetLowering::CallLoweringInfo &CLI,
Dan Gohmand858e902010-04-17 15:26:15 +00002155 SmallVectorImpl<SDValue> &InVals) const {
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00002156 SelectionDAG &DAG = CLI.DAG;
2157 DebugLoc &dl = CLI.DL;
2158 SmallVector<ISD::OutputArg, 32> &Outs = CLI.Outs;
2159 SmallVector<SDValue, 32> &OutVals = CLI.OutVals;
2160 SmallVector<ISD::InputArg, 32> &Ins = CLI.Ins;
2161 SDValue Chain = CLI.Chain;
2162 SDValue Callee = CLI.Callee;
2163 CallingConv::ID CallConv = CLI.CallConv;
2164 bool &isTailCall = CLI.IsTailCall;
2165 bool isVarArg = CLI.IsVarArg;
2166
Dan Gohman98ca4f22009-08-05 01:29:28 +00002167 MachineFunction &MF = DAG.getMachineFunction();
2168 bool Is64Bit = Subtarget->is64Bit();
NAKAMURA Takumifb840c92011-02-05 15:11:13 +00002169 bool IsWin64 = Subtarget->isTargetWin64();
Eli Friedman9a2478a2012-01-20 00:05:46 +00002170 bool IsWindows = Subtarget->isTargetWindows();
Rafael Espindola1cee7102012-07-25 13:41:10 +00002171 StructReturnType SR = callIsStructReturn(Outs);
Evan Cheng5f941932010-02-05 02:21:12 +00002172 bool IsSibcall = false;
Dan Gohman98ca4f22009-08-05 01:29:28 +00002173
Nick Lewycky22de16d2012-01-19 00:34:10 +00002174 if (MF.getTarget().Options.DisableTailCalls)
2175 isTailCall = false;
2176
Evan Cheng5f941932010-02-05 02:21:12 +00002177 if (isTailCall) {
Evan Cheng0c439eb2010-01-27 00:07:07 +00002178 // Check if it's really possible to do a tail call.
Evan Chenga375d472010-03-15 18:54:48 +00002179 isTailCall = IsEligibleForTailCallOptimization(Callee, CallConv,
Rafael Espindola1cee7102012-07-25 13:41:10 +00002180 isVarArg, SR != NotStructReturn,
2181 MF.getFunction()->hasStructRetAttr(),
2182 Outs, OutVals, Ins, DAG);
Evan Chengf22f9b32010-02-06 03:28:46 +00002183
2184 // Sibcalls are automatically detected tailcalls which do not require
2185 // ABI changes.
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002186 if (!MF.getTarget().Options.GuaranteedTailCallOpt && isTailCall)
Evan Cheng5f941932010-02-05 02:21:12 +00002187 IsSibcall = true;
Evan Chengf22f9b32010-02-06 03:28:46 +00002188
2189 if (isTailCall)
2190 ++NumTailCalls;
Evan Cheng5f941932010-02-05 02:21:12 +00002191 }
Evan Cheng0c439eb2010-01-27 00:07:07 +00002192
Chris Lattner29689432010-03-11 00:22:57 +00002193 assert(!(isVarArg && IsTailCallConvention(CallConv)) &&
2194 "Var args not supported with calling convention fastcc or ghc");
Gordon Henriksenae636f82008-01-03 16:47:34 +00002195
Chris Lattner638402b2007-02-28 07:00:42 +00002196 // Analyze operands of the call, assigning locations to each operand.
Chris Lattner423c5f42007-02-28 05:31:48 +00002197 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002198 CCState CCInfo(CallConv, isVarArg, MF, getTargetMachine(),
Dan Gohman98ca4f22009-08-05 01:29:28 +00002199 ArgLocs, *DAG.getContext());
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00002200
2201 // Allocate shadow area for Win64
2202 if (IsWin64) {
2203 CCInfo.AllocateStack(32, 8);
2204 }
2205
Duncan Sands45907662010-10-31 13:21:44 +00002206 CCInfo.AnalyzeCallOperands(Outs, CC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00002207
Chris Lattner423c5f42007-02-28 05:31:48 +00002208 // Get a count of how many bytes are to be pushed on the stack.
2209 unsigned NumBytes = CCInfo.getNextStackOffset();
Evan Chengf22f9b32010-02-06 03:28:46 +00002210 if (IsSibcall)
Evan Chengb2c92902010-02-02 02:22:50 +00002211 // This is a sibcall. The memory operands are available in caller's
2212 // own caller's stack.
2213 NumBytes = 0;
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002214 else if (getTargetMachine().Options.GuaranteedTailCallOpt &&
2215 IsTailCallConvention(CallConv))
Evan Chengf22f9b32010-02-06 03:28:46 +00002216 NumBytes = GetAlignedArgumentStackSize(NumBytes, DAG);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002217
Gordon Henriksen86737662008-01-05 16:56:59 +00002218 int FPDiff = 0;
Evan Chengf22f9b32010-02-06 03:28:46 +00002219 if (isTailCall && !IsSibcall) {
Gordon Henriksen86737662008-01-05 16:56:59 +00002220 // Lower arguments at fp - stackoffset + fpdiff.
Scott Michelfdc40a02009-02-17 22:15:04 +00002221 unsigned NumBytesCallerPushed =
Gordon Henriksen86737662008-01-05 16:56:59 +00002222 MF.getInfo<X86MachineFunctionInfo>()->getBytesToPopOnReturn();
2223 FPDiff = NumBytesCallerPushed - NumBytes;
2224
2225 // Set the delta of movement of the returnaddr stackslot.
2226 // But only set if delta is greater than previous delta.
2227 if (FPDiff < (MF.getInfo<X86MachineFunctionInfo>()->getTCReturnAddrDelta()))
2228 MF.getInfo<X86MachineFunctionInfo>()->setTCReturnAddrDelta(FPDiff);
2229 }
2230
Evan Chengf22f9b32010-02-06 03:28:46 +00002231 if (!IsSibcall)
2232 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002233
Dan Gohman475871a2008-07-27 21:46:04 +00002234 SDValue RetAddrFrIdx;
Chris Lattner7a2bdde2011-04-15 05:18:47 +00002235 // Load return address for tail calls.
Evan Chengf22f9b32010-02-06 03:28:46 +00002236 if (isTailCall && FPDiff)
2237 Chain = EmitTailCallLoadRetAddr(DAG, RetAddrFrIdx, Chain, isTailCall,
2238 Is64Bit, FPDiff, dl);
Gordon Henriksen86737662008-01-05 16:56:59 +00002239
Dan Gohman475871a2008-07-27 21:46:04 +00002240 SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass;
2241 SmallVector<SDValue, 8> MemOpChains;
2242 SDValue StackPtr;
Chris Lattner423c5f42007-02-28 05:31:48 +00002243
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002244 // Walk the register/memloc assignments, inserting copies/loads. In the case
2245 // of tail call optimization arguments are handle later.
Chris Lattner423c5f42007-02-28 05:31:48 +00002246 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2247 CCValAssign &VA = ArgLocs[i];
Owen Andersone50ed302009-08-10 22:56:29 +00002248 EVT RegVT = VA.getLocVT();
Dan Gohmanc9403652010-07-07 15:54:55 +00002249 SDValue Arg = OutVals[i];
Dan Gohman98ca4f22009-08-05 01:29:28 +00002250 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Dan Gohman095cc292008-09-13 01:54:27 +00002251 bool isByVal = Flags.isByVal();
Scott Michelfdc40a02009-02-17 22:15:04 +00002252
Chris Lattner423c5f42007-02-28 05:31:48 +00002253 // Promote the value if needed.
2254 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002255 default: llvm_unreachable("Unknown loc info!");
Chris Lattner423c5f42007-02-28 05:31:48 +00002256 case CCValAssign::Full: break;
2257 case CCValAssign::SExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002258 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00002259 break;
2260 case CCValAssign::ZExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002261 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00002262 break;
2263 case CCValAssign::AExt:
Craig Topper7a9a28b2012-08-12 02:23:29 +00002264 if (RegVT.is128BitVector()) {
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002265 // Special case: passing MMX values in XMM registers.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002266 Arg = DAG.getNode(ISD::BITCAST, dl, MVT::i64, Arg);
Owen Anderson825b72b2009-08-11 20:47:22 +00002267 Arg = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i64, Arg);
2268 Arg = getMOVL(DAG, dl, MVT::v2i64, DAG.getUNDEF(MVT::v2i64), Arg);
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002269 } else
2270 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, RegVT, Arg);
2271 break;
2272 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002273 Arg = DAG.getNode(ISD::BITCAST, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00002274 break;
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00002275 case CCValAssign::Indirect: {
2276 // Store the argument.
2277 SDValue SpillSlot = DAG.CreateStackTemporary(VA.getValVT());
Evan Chengff89dcb2009-10-18 18:16:27 +00002278 int FI = cast<FrameIndexSDNode>(SpillSlot)->getIndex();
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00002279 Chain = DAG.getStore(Chain, dl, Arg, SpillSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00002280 MachinePointerInfo::getFixedStack(FI),
David Greene67c9d422010-02-15 16:53:33 +00002281 false, false, 0);
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00002282 Arg = SpillSlot;
2283 break;
2284 }
Evan Cheng6b5783d2006-05-25 18:56:34 +00002285 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002286
Chris Lattner423c5f42007-02-28 05:31:48 +00002287 if (VA.isRegLoc()) {
Stuart Hastings2aa0f232011-05-26 04:09:49 +00002288 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
2289 if (isVarArg && IsWin64) {
2290 // Win64 ABI requires argument XMM reg to be copied to the corresponding
2291 // shadow reg if callee is a varargs function.
2292 unsigned ShadowReg = 0;
2293 switch (VA.getLocReg()) {
2294 case X86::XMM0: ShadowReg = X86::RCX; break;
2295 case X86::XMM1: ShadowReg = X86::RDX; break;
2296 case X86::XMM2: ShadowReg = X86::R8; break;
2297 case X86::XMM3: ShadowReg = X86::R9; break;
Anton Korobeynikovc52bedb2010-08-27 14:43:06 +00002298 }
Stuart Hastings2aa0f232011-05-26 04:09:49 +00002299 if (ShadowReg)
2300 RegsToPass.push_back(std::make_pair(ShadowReg, Arg));
Anton Korobeynikovc52bedb2010-08-27 14:43:06 +00002301 }
Evan Chengf22f9b32010-02-06 03:28:46 +00002302 } else if (!IsSibcall && (!isTailCall || isByVal)) {
Evan Cheng5f941932010-02-05 02:21:12 +00002303 assert(VA.isMemLoc());
2304 if (StackPtr.getNode() == 0)
2305 StackPtr = DAG.getCopyFromReg(Chain, dl, X86StackPtr, getPointerTy());
2306 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Arg,
2307 dl, DAG, VA, Flags));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002308 }
Stuart Hastings2aa0f232011-05-26 04:09:49 +00002309 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002310
Evan Cheng32fe1032006-05-25 00:59:30 +00002311 if (!MemOpChains.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00002312 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002313 &MemOpChains[0], MemOpChains.size());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002314
Chris Lattner88e1fd52009-07-09 04:24:46 +00002315 if (Subtarget->isPICStyleGOT()) {
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002316 // ELF / PIC requires GOT in the EBX register before function calls via PLT
2317 // GOT pointer.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002318 if (!isTailCall) {
Jakob Stoklund Olesenb8720782012-07-04 19:28:31 +00002319 RegsToPass.push_back(std::make_pair(unsigned(X86::EBX),
2320 DAG.getNode(X86ISD::GlobalBaseReg, DebugLoc(), getPointerTy())));
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002321 } else {
2322 // If we are tail calling and generating PIC/GOT style code load the
2323 // address of the callee into ECX. The value in ecx is used as target of
2324 // the tail jump. This is done to circumvent the ebx/callee-saved problem
2325 // for tail calls on PIC/GOT architectures. Normally we would just put the
2326 // address of GOT into ebx and then call target@PLT. But for tail calls
2327 // ebx would be restored (since ebx is callee saved) before jumping to the
2328 // target@PLT.
2329
2330 // Note: The actual moving to ECX is done further down.
2331 GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee);
2332 if (G && !G->getGlobal()->hasHiddenVisibility() &&
2333 !G->getGlobal()->hasProtectedVisibility())
2334 Callee = LowerGlobalAddress(Callee, DAG);
2335 else if (isa<ExternalSymbolSDNode>(Callee))
Chris Lattner15a380a2009-07-09 04:39:06 +00002336 Callee = LowerExternalSymbol(Callee, DAG);
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002337 }
Anton Korobeynikov7f705592007-01-12 19:20:47 +00002338 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00002339
NAKAMURA Takumifb840c92011-02-05 15:11:13 +00002340 if (Is64Bit && isVarArg && !IsWin64) {
Gordon Henriksen86737662008-01-05 16:56:59 +00002341 // From AMD64 ABI document:
2342 // For calls that may call functions that use varargs or stdargs
2343 // (prototype-less calls or calls to functions containing ellipsis (...) in
2344 // the declaration) %al is used as hidden argument to specify the number
2345 // of SSE registers used. The contents of %al do not need to match exactly
2346 // the number of registers, but must be an ubound on the number of SSE
2347 // registers used and is in the range 0 - 8 inclusive.
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002348
Gordon Henriksen86737662008-01-05 16:56:59 +00002349 // Count the number of XMM registers allocated.
Craig Topperc5eaae42012-03-11 07:57:25 +00002350 static const uint16_t XMMArgRegs[] = {
Gordon Henriksen86737662008-01-05 16:56:59 +00002351 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
2352 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
2353 };
2354 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs, 8);
Craig Topper1accb7e2012-01-10 06:54:16 +00002355 assert((Subtarget->hasSSE1() || !NumXMMRegs)
Torok Edwin3f142c32009-02-01 18:15:56 +00002356 && "SSE registers cannot be used when SSE is disabled");
Scott Michelfdc40a02009-02-17 22:15:04 +00002357
Jakob Stoklund Olesenb8720782012-07-04 19:28:31 +00002358 RegsToPass.push_back(std::make_pair(unsigned(X86::AL),
2359 DAG.getConstant(NumXMMRegs, MVT::i8)));
Gordon Henriksen86737662008-01-05 16:56:59 +00002360 }
2361
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00002362 // For tail calls lower the arguments to the 'real' stack slot.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002363 if (isTailCall) {
2364 // Force all the incoming stack arguments to be loaded from the stack
2365 // before any new outgoing arguments are stored to the stack, because the
2366 // outgoing stack slots may alias the incoming argument stack slots, and
2367 // the alias isn't otherwise explicit. This is slightly more conservative
2368 // than necessary, because it means that each store effectively depends
2369 // on every argument instead of just those arguments it would clobber.
2370 SDValue ArgChain = DAG.getStackArgumentTokenFactor(Chain);
2371
Dan Gohman475871a2008-07-27 21:46:04 +00002372 SmallVector<SDValue, 8> MemOpChains2;
2373 SDValue FIN;
Gordon Henriksen86737662008-01-05 16:56:59 +00002374 int FI = 0;
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002375 if (getTargetMachine().Options.GuaranteedTailCallOpt) {
Evan Chengb2c92902010-02-02 02:22:50 +00002376 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2377 CCValAssign &VA = ArgLocs[i];
2378 if (VA.isRegLoc())
2379 continue;
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00002380 assert(VA.isMemLoc());
Dan Gohmanc9403652010-07-07 15:54:55 +00002381 SDValue Arg = OutVals[i];
Dan Gohman98ca4f22009-08-05 01:29:28 +00002382 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Gordon Henriksen86737662008-01-05 16:56:59 +00002383 // Create frame index.
2384 int32_t Offset = VA.getLocMemOffset()+FPDiff;
Duncan Sands83ec4b62008-06-06 12:08:01 +00002385 uint32_t OpSize = (VA.getLocVT().getSizeInBits()+7)/8;
Evan Chenged2ae132010-07-03 00:40:23 +00002386 FI = MF.getFrameInfo()->CreateFixedObject(OpSize, Offset, true);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002387 FIN = DAG.getFrameIndex(FI, getPointerTy());
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00002388
Duncan Sands276dcbd2008-03-21 09:14:45 +00002389 if (Flags.isByVal()) {
Evan Cheng8e5712b2008-01-12 01:08:07 +00002390 // Copy relative to framepointer.
Dan Gohman475871a2008-07-27 21:46:04 +00002391 SDValue Source = DAG.getIntPtrConstant(VA.getLocMemOffset());
Gabor Greifba36cb52008-08-28 21:40:38 +00002392 if (StackPtr.getNode() == 0)
Scott Michelfdc40a02009-02-17 22:15:04 +00002393 StackPtr = DAG.getCopyFromReg(Chain, dl, X86StackPtr,
Dale Johannesendd64c412009-02-04 00:33:20 +00002394 getPointerTy());
Dale Johannesenace16102009-02-03 19:33:06 +00002395 Source = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, Source);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002396
Dan Gohman98ca4f22009-08-05 01:29:28 +00002397 MemOpChains2.push_back(CreateCopyOfByValArgument(Source, FIN,
2398 ArgChain,
Dale Johannesendd64c412009-02-04 00:33:20 +00002399 Flags, DAG, dl));
Gordon Henriksen86737662008-01-05 16:56:59 +00002400 } else {
Evan Cheng8e5712b2008-01-12 01:08:07 +00002401 // Store relative to framepointer.
Dan Gohman69de1932008-02-06 22:27:42 +00002402 MemOpChains2.push_back(
Dan Gohman98ca4f22009-08-05 01:29:28 +00002403 DAG.getStore(ArgChain, dl, Arg, FIN,
Chris Lattnere8639032010-09-21 06:22:23 +00002404 MachinePointerInfo::getFixedStack(FI),
David Greene67c9d422010-02-15 16:53:33 +00002405 false, false, 0));
Scott Michelfdc40a02009-02-17 22:15:04 +00002406 }
Gordon Henriksen86737662008-01-05 16:56:59 +00002407 }
2408 }
2409
2410 if (!MemOpChains2.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00002411 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Arnold Schwaighofer719eb022008-01-11 14:34:56 +00002412 &MemOpChains2[0], MemOpChains2.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00002413
2414 // Store the return address to the appropriate stack slot.
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002415 Chain = EmitTailCallStoreRetAddr(DAG, MF, Chain, RetAddrFrIdx, Is64Bit,
Dale Johannesenace16102009-02-03 19:33:06 +00002416 FPDiff, dl);
Gordon Henriksen86737662008-01-05 16:56:59 +00002417 }
2418
Jakob Stoklund Olesenb8720782012-07-04 19:28:31 +00002419 // Build a sequence of copy-to-reg nodes chained together with token chain
2420 // and flag operands which copy the outgoing args into registers.
2421 SDValue InFlag;
2422 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
2423 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
2424 RegsToPass[i].second, InFlag);
2425 InFlag = Chain.getValue(1);
2426 }
2427
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002428 if (getTargetMachine().getCodeModel() == CodeModel::Large) {
2429 assert(Is64Bit && "Large code model is only legal in 64-bit mode.");
2430 // In the 64-bit large code model, we have to make all calls
2431 // through a register, since the call instruction's 32-bit
2432 // pc-relative offset may not be large enough to hold the whole
2433 // address.
2434 } else if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002435 // If the callee is a GlobalAddress node (quite common, every direct call
2436 // is) turn it into a TargetGlobalAddress node so that legalize doesn't hack
2437 // it.
2438
Anton Korobeynikov2b2bc682006-12-22 22:29:05 +00002439 // We should use extra load for direct calls to dllimported functions in
2440 // non-JIT mode.
Dan Gohman46510a72010-04-15 01:51:59 +00002441 const GlobalValue *GV = G->getGlobal();
Chris Lattner754b7652009-07-10 05:48:03 +00002442 if (!GV->hasDLLImportLinkage()) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002443 unsigned char OpFlags = 0;
John McCall3a3465b2011-06-15 20:36:13 +00002444 bool ExtraLoad = false;
2445 unsigned WrapperKind = ISD::DELETED_NODE;
Eric Christopherfd179292009-08-27 18:07:15 +00002446
Chris Lattner48a7d022009-07-09 05:02:21 +00002447 // On ELF targets, in both X86-64 and X86-32 mode, direct calls to
2448 // external symbols most go through the PLT in PIC mode. If the symbol
2449 // has hidden or protected visibility, or if it is static or local, then
2450 // we don't need to use the PLT - we can directly call it.
2451 if (Subtarget->isTargetELF() &&
2452 getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
Chris Lattner74e726e2009-07-09 05:27:35 +00002453 GV->hasDefaultVisibility() && !GV->hasLocalLinkage()) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002454 OpFlags = X86II::MO_PLT;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00002455 } else if (Subtarget->isPICStyleStubAny() &&
Chris Lattner80945782010-09-27 06:34:01 +00002456 (GV->isDeclaration() || GV->isWeakForLinker()) &&
Daniel Dunbar558692f2011-04-20 00:14:25 +00002457 (!Subtarget->getTargetTriple().isMacOSX() ||
2458 Subtarget->getTargetTriple().isMacOSXVersionLT(10, 5))) {
Chris Lattner74e726e2009-07-09 05:27:35 +00002459 // PC-relative references to external symbols should go through $stub,
2460 // unless we're building with the leopard linker or later, which
2461 // automatically synthesizes these stubs.
2462 OpFlags = X86II::MO_DARWIN_STUB;
John McCall3a3465b2011-06-15 20:36:13 +00002463 } else if (Subtarget->isPICStyleRIPRel() &&
2464 isa<Function>(GV) &&
2465 cast<Function>(GV)->hasFnAttr(Attribute::NonLazyBind)) {
2466 // If the function is marked as non-lazy, generate an indirect call
2467 // which loads from the GOT directly. This avoids runtime overhead
2468 // at the cost of eager binding (and one extra byte of encoding).
2469 OpFlags = X86II::MO_GOTPCREL;
2470 WrapperKind = X86ISD::WrapperRIP;
2471 ExtraLoad = true;
Chris Lattner74e726e2009-07-09 05:27:35 +00002472 }
Chris Lattner48a7d022009-07-09 05:02:21 +00002473
Devang Patel0d881da2010-07-06 22:08:15 +00002474 Callee = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(),
Chris Lattner48a7d022009-07-09 05:02:21 +00002475 G->getOffset(), OpFlags);
John McCall3a3465b2011-06-15 20:36:13 +00002476
2477 // Add a wrapper if needed.
2478 if (WrapperKind != ISD::DELETED_NODE)
2479 Callee = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Callee);
2480 // Add extra indirection if needed.
2481 if (ExtraLoad)
2482 Callee = DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(), Callee,
2483 MachinePointerInfo::getGOT(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002484 false, false, false, 0);
Chris Lattner48a7d022009-07-09 05:02:21 +00002485 }
Bill Wendling056292f2008-09-16 21:48:12 +00002486 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002487 unsigned char OpFlags = 0;
2488
Evan Cheng1bf891a2010-12-01 22:59:46 +00002489 // On ELF targets, in either X86-64 or X86-32 mode, direct calls to
2490 // external symbols should go through the PLT.
2491 if (Subtarget->isTargetELF() &&
2492 getTargetMachine().getRelocationModel() == Reloc::PIC_) {
2493 OpFlags = X86II::MO_PLT;
2494 } else if (Subtarget->isPICStyleStubAny() &&
Daniel Dunbar558692f2011-04-20 00:14:25 +00002495 (!Subtarget->getTargetTriple().isMacOSX() ||
2496 Subtarget->getTargetTriple().isMacOSXVersionLT(10, 5))) {
Evan Cheng1bf891a2010-12-01 22:59:46 +00002497 // PC-relative references to external symbols should go through $stub,
2498 // unless we're building with the leopard linker or later, which
2499 // automatically synthesizes these stubs.
2500 OpFlags = X86II::MO_DARWIN_STUB;
Chris Lattner74e726e2009-07-09 05:27:35 +00002501 }
Eric Christopherfd179292009-08-27 18:07:15 +00002502
Chris Lattner48a7d022009-07-09 05:02:21 +00002503 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy(),
2504 OpFlags);
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002505 }
2506
Chris Lattnerd96d0722007-02-25 06:40:16 +00002507 // Returns a chain & a flag for retval copy to use.
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002508 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Dan Gohman475871a2008-07-27 21:46:04 +00002509 SmallVector<SDValue, 8> Ops;
Gordon Henriksen86737662008-01-05 16:56:59 +00002510
Evan Chengf22f9b32010-02-06 03:28:46 +00002511 if (!IsSibcall && isTailCall) {
Dale Johannesene8d72302009-02-06 23:05:02 +00002512 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
2513 DAG.getIntPtrConstant(0, true), InFlag);
Gordon Henriksen86737662008-01-05 16:56:59 +00002514 InFlag = Chain.getValue(1);
Gordon Henriksen86737662008-01-05 16:56:59 +00002515 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002516
Nate Begeman4c5dcf52006-02-17 00:03:04 +00002517 Ops.push_back(Chain);
2518 Ops.push_back(Callee);
Evan Chengb69d1132006-06-14 18:17:40 +00002519
Dan Gohman98ca4f22009-08-05 01:29:28 +00002520 if (isTailCall)
Owen Anderson825b72b2009-08-11 20:47:22 +00002521 Ops.push_back(DAG.getConstant(FPDiff, MVT::i32));
Evan Chengf4684712007-02-21 21:18:14 +00002522
Gordon Henriksen86737662008-01-05 16:56:59 +00002523 // Add argument registers to the end of the list so that they are known live
2524 // into the call.
Evan Cheng9b449442008-01-07 23:08:23 +00002525 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
2526 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
2527 RegsToPass[i].second.getValueType()));
Scott Michelfdc40a02009-02-17 22:15:04 +00002528
Jakob Stoklund Olesen8bcde2a2012-02-16 00:02:50 +00002529 // Add a register mask operand representing the call-preserved registers.
2530 const TargetRegisterInfo *TRI = getTargetMachine().getRegisterInfo();
2531 const uint32_t *Mask = TRI->getCallPreservedMask(CallConv);
2532 assert(Mask && "Missing call preserved mask for calling convention");
2533 Ops.push_back(DAG.getRegisterMask(Mask));
Jakob Stoklund Olesenc38c4562012-01-18 23:52:22 +00002534
Gabor Greifba36cb52008-08-28 21:40:38 +00002535 if (InFlag.getNode())
Evan Cheng347d5f72006-04-28 21:29:37 +00002536 Ops.push_back(InFlag);
Gordon Henriksenae636f82008-01-03 16:47:34 +00002537
Dan Gohman98ca4f22009-08-05 01:29:28 +00002538 if (isTailCall) {
Dale Johannesen88004c22010-06-05 00:30:45 +00002539 // We used to do:
2540 //// If this is the first return lowered for this function, add the regs
2541 //// to the liveout set for the function.
2542 // This isn't right, although it's probably harmless on x86; liveouts
2543 // should be computed from returns not tail calls. Consider a void
2544 // function making a tail call to a function returning int.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002545 return DAG.getNode(X86ISD::TC_RETURN, dl,
2546 NodeTys, &Ops[0], Ops.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00002547 }
2548
Dale Johannesenace16102009-02-03 19:33:06 +00002549 Chain = DAG.getNode(X86ISD::CALL, dl, NodeTys, &Ops[0], Ops.size());
Evan Cheng347d5f72006-04-28 21:29:37 +00002550 InFlag = Chain.getValue(1);
Evan Chengd90eb7f2006-01-05 00:27:02 +00002551
Chris Lattner2d297092006-05-23 18:50:38 +00002552 // Create the CALLSEQ_END node.
Gordon Henriksen86737662008-01-05 16:56:59 +00002553 unsigned NumBytesForCalleeToPush;
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002554 if (X86::isCalleePop(CallConv, Is64Bit, isVarArg,
2555 getTargetMachine().Options.GuaranteedTailCallOpt))
Gordon Henriksen86737662008-01-05 16:56:59 +00002556 NumBytesForCalleeToPush = NumBytes; // Callee pops everything
Eli Friedman9a2478a2012-01-20 00:05:46 +00002557 else if (!Is64Bit && !IsTailCallConvention(CallConv) && !IsWindows &&
Rafael Espindola1cee7102012-07-25 13:41:10 +00002558 SR == StackStructReturn)
Dan Gohmanf451cb82010-02-10 16:03:48 +00002559 // If this is a call to a struct-return function, the callee
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00002560 // pops the hidden struct pointer, so we have to push it back.
2561 // This is common for Darwin/X86, Linux & Mingw32 targets.
Eli Friedman9a2478a2012-01-20 00:05:46 +00002562 // For MSVC Win32 targets, the caller pops the hidden struct pointer.
Gordon Henriksenae636f82008-01-03 16:47:34 +00002563 NumBytesForCalleeToPush = 4;
Gordon Henriksen86737662008-01-05 16:56:59 +00002564 else
Gordon Henriksenae636f82008-01-03 16:47:34 +00002565 NumBytesForCalleeToPush = 0; // Callee pops nothing.
Scott Michelfdc40a02009-02-17 22:15:04 +00002566
Gordon Henriksenae636f82008-01-03 16:47:34 +00002567 // Returns a flag for retval copy to use.
Evan Chengf22f9b32010-02-06 03:28:46 +00002568 if (!IsSibcall) {
2569 Chain = DAG.getCALLSEQ_END(Chain,
2570 DAG.getIntPtrConstant(NumBytes, true),
2571 DAG.getIntPtrConstant(NumBytesForCalleeToPush,
2572 true),
2573 InFlag);
2574 InFlag = Chain.getValue(1);
2575 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00002576
Chris Lattner3085e152007-02-25 08:59:22 +00002577 // Handle result values, copying them out of physregs into vregs that we
2578 // return.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002579 return LowerCallResult(Chain, InFlag, CallConv, isVarArg,
2580 Ins, dl, DAG, InVals);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002581}
2582
Evan Cheng25ab6902006-09-08 06:48:29 +00002583
2584//===----------------------------------------------------------------------===//
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002585// Fast Calling Convention (tail call) implementation
2586//===----------------------------------------------------------------------===//
2587
2588// Like std call, callee cleans arguments, convention except that ECX is
2589// reserved for storing the tail called function address. Only 2 registers are
2590// free for argument passing (inreg). Tail call optimization is performed
2591// provided:
2592// * tailcallopt is enabled
2593// * caller/callee are fastcc
Arnold Schwaighofera2a4b472008-02-26 10:21:54 +00002594// On X86_64 architecture with GOT-style position independent code only local
2595// (within module) calls are supported at the moment.
Arnold Schwaighofer48abc5c2007-10-12 21:30:57 +00002596// To keep the stack aligned according to platform abi the function
2597// GetAlignedArgumentStackSize ensures that argument delta is always multiples
2598// of stack alignment. (Dynamic linkers need this - darwin's dyld for example)
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002599// If a tail called function callee has more arguments than the caller the
2600// caller needs to make sure that there is room to move the RETADDR to. This is
Arnold Schwaighofer48abc5c2007-10-12 21:30:57 +00002601// achieved by reserving an area the size of the argument delta right after the
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002602// original REtADDR, but before the saved framepointer or the spilled registers
2603// e.g. caller(arg1, arg2) calls callee(arg1, arg2,arg3,arg4)
2604// stack layout:
2605// arg1
2606// arg2
2607// RETADDR
Scott Michelfdc40a02009-02-17 22:15:04 +00002608// [ new RETADDR
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002609// move area ]
2610// (possible EBP)
2611// ESI
2612// EDI
2613// local1 ..
2614
2615/// GetAlignedArgumentStackSize - Make the stack size align e.g 16n + 12 aligned
2616/// for a 16 byte align requirement.
Dan Gohmand858e902010-04-17 15:26:15 +00002617unsigned
2618X86TargetLowering::GetAlignedArgumentStackSize(unsigned StackSize,
2619 SelectionDAG& DAG) const {
Evan Chenge9ac9e62008-09-07 09:07:23 +00002620 MachineFunction &MF = DAG.getMachineFunction();
2621 const TargetMachine &TM = MF.getTarget();
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00002622 const TargetFrameLowering &TFI = *TM.getFrameLowering();
Evan Chenge9ac9e62008-09-07 09:07:23 +00002623 unsigned StackAlignment = TFI.getStackAlignment();
Scott Michelfdc40a02009-02-17 22:15:04 +00002624 uint64_t AlignMask = StackAlignment - 1;
Evan Chenge9ac9e62008-09-07 09:07:23 +00002625 int64_t Offset = StackSize;
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00002626 uint64_t SlotSize = TD->getPointerSize();
Evan Chenge9ac9e62008-09-07 09:07:23 +00002627 if ( (Offset & AlignMask) <= (StackAlignment - SlotSize) ) {
2628 // Number smaller than 12 so just add the difference.
2629 Offset += ((StackAlignment - SlotSize) - (Offset & AlignMask));
2630 } else {
2631 // Mask out lower bits, add stackalignment once plus the 12 bytes.
Scott Michelfdc40a02009-02-17 22:15:04 +00002632 Offset = ((~AlignMask) & Offset) + StackAlignment +
Evan Chenge9ac9e62008-09-07 09:07:23 +00002633 (StackAlignment-SlotSize);
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002634 }
Evan Chenge9ac9e62008-09-07 09:07:23 +00002635 return Offset;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002636}
2637
Evan Cheng5f941932010-02-05 02:21:12 +00002638/// MatchingStackOffset - Return true if the given stack call argument is
2639/// already available in the same position (relatively) of the caller's
2640/// incoming argument stack.
2641static
2642bool MatchingStackOffset(SDValue Arg, unsigned Offset, ISD::ArgFlagsTy Flags,
2643 MachineFrameInfo *MFI, const MachineRegisterInfo *MRI,
2644 const X86InstrInfo *TII) {
Evan Cheng4cae1332010-03-05 08:38:04 +00002645 unsigned Bytes = Arg.getValueType().getSizeInBits() / 8;
2646 int FI = INT_MAX;
Evan Cheng5f941932010-02-05 02:21:12 +00002647 if (Arg.getOpcode() == ISD::CopyFromReg) {
2648 unsigned VR = cast<RegisterSDNode>(Arg.getOperand(1))->getReg();
Jakob Stoklund Olesenc9df0252011-01-10 02:58:51 +00002649 if (!TargetRegisterInfo::isVirtualRegister(VR))
Evan Cheng5f941932010-02-05 02:21:12 +00002650 return false;
2651 MachineInstr *Def = MRI->getVRegDef(VR);
2652 if (!Def)
2653 return false;
2654 if (!Flags.isByVal()) {
2655 if (!TII->isLoadFromStackSlot(Def, FI))
2656 return false;
2657 } else {
2658 unsigned Opcode = Def->getOpcode();
2659 if ((Opcode == X86::LEA32r || Opcode == X86::LEA64r) &&
2660 Def->getOperand(1).isFI()) {
2661 FI = Def->getOperand(1).getIndex();
Evan Cheng4cae1332010-03-05 08:38:04 +00002662 Bytes = Flags.getByValSize();
Evan Cheng5f941932010-02-05 02:21:12 +00002663 } else
2664 return false;
2665 }
Evan Cheng4cae1332010-03-05 08:38:04 +00002666 } else if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Arg)) {
2667 if (Flags.isByVal())
2668 // ByVal argument is passed in as a pointer but it's now being
Evan Cheng10718492010-03-05 19:55:55 +00002669 // dereferenced. e.g.
Evan Cheng4cae1332010-03-05 08:38:04 +00002670 // define @foo(%struct.X* %A) {
2671 // tail call @bar(%struct.X* byval %A)
2672 // }
Evan Cheng5f941932010-02-05 02:21:12 +00002673 return false;
2674 SDValue Ptr = Ld->getBasePtr();
2675 FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr);
2676 if (!FINode)
2677 return false;
2678 FI = FINode->getIndex();
Chad Rosierdf78fcd2011-06-25 02:04:56 +00002679 } else if (Arg.getOpcode() == ISD::FrameIndex && Flags.isByVal()) {
Chad Rosier14d71aa2011-06-25 18:51:28 +00002680 FrameIndexSDNode *FINode = cast<FrameIndexSDNode>(Arg);
Chad Rosierdf78fcd2011-06-25 02:04:56 +00002681 FI = FINode->getIndex();
2682 Bytes = Flags.getByValSize();
Evan Cheng4cae1332010-03-05 08:38:04 +00002683 } else
2684 return false;
Evan Cheng5f941932010-02-05 02:21:12 +00002685
Evan Cheng4cae1332010-03-05 08:38:04 +00002686 assert(FI != INT_MAX);
Evan Cheng5f941932010-02-05 02:21:12 +00002687 if (!MFI->isFixedObjectIndex(FI))
2688 return false;
Evan Cheng4cae1332010-03-05 08:38:04 +00002689 return Offset == MFI->getObjectOffset(FI) && Bytes == MFI->getObjectSize(FI);
Evan Cheng5f941932010-02-05 02:21:12 +00002690}
2691
Dan Gohman98ca4f22009-08-05 01:29:28 +00002692/// IsEligibleForTailCallOptimization - Check whether the call is eligible
2693/// for tail call optimization. Targets which want to do tail call
2694/// optimization should implement this function.
2695bool
2696X86TargetLowering::IsEligibleForTailCallOptimization(SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00002697 CallingConv::ID CalleeCC,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002698 bool isVarArg,
Evan Chenga375d472010-03-15 18:54:48 +00002699 bool isCalleeStructRet,
2700 bool isCallerStructRet,
Evan Chengb1712452010-01-27 06:25:16 +00002701 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00002702 const SmallVectorImpl<SDValue> &OutVals,
Evan Chengb1712452010-01-27 06:25:16 +00002703 const SmallVectorImpl<ISD::InputArg> &Ins,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002704 SelectionDAG& DAG) const {
Chris Lattner29689432010-03-11 00:22:57 +00002705 if (!IsTailCallConvention(CalleeCC) &&
Evan Chengb1712452010-01-27 06:25:16 +00002706 CalleeCC != CallingConv::C)
2707 return false;
2708
Evan Cheng7096ae42010-01-29 06:45:59 +00002709 // If -tailcallopt is specified, make fastcc functions tail-callable.
Evan Cheng2c12cb42010-03-26 16:26:03 +00002710 const MachineFunction &MF = DAG.getMachineFunction();
Evan Cheng7096ae42010-01-29 06:45:59 +00002711 const Function *CallerF = DAG.getMachineFunction().getFunction();
Evan Cheng13617962010-04-30 01:12:32 +00002712 CallingConv::ID CallerCC = CallerF->getCallingConv();
2713 bool CCMatch = CallerCC == CalleeCC;
2714
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002715 if (getTargetMachine().Options.GuaranteedTailCallOpt) {
Evan Cheng13617962010-04-30 01:12:32 +00002716 if (IsTailCallConvention(CalleeCC) && CCMatch)
Evan Cheng843bd692010-01-31 06:44:49 +00002717 return true;
2718 return false;
2719 }
2720
Dale Johannesen2f05cc02010-05-28 23:24:28 +00002721 // Look for obvious safe cases to perform tail call optimization that do not
2722 // require ABI changes. This is what gcc calls sibcall.
Evan Chengb2c92902010-02-02 02:22:50 +00002723
Evan Cheng2c12cb42010-03-26 16:26:03 +00002724 // Can't do sibcall if stack needs to be dynamically re-aligned. PEI needs to
2725 // emit a special epilogue.
2726 if (RegInfo->needsStackRealignment(MF))
2727 return false;
2728
Evan Chenga375d472010-03-15 18:54:48 +00002729 // Also avoid sibcall optimization if either caller or callee uses struct
2730 // return semantics.
2731 if (isCalleeStructRet || isCallerStructRet)
2732 return false;
2733
Chad Rosier2416da32011-06-24 21:15:36 +00002734 // An stdcall caller is expected to clean up its arguments; the callee
2735 // isn't going to do that.
2736 if (!CCMatch && CallerCC==CallingConv::X86_StdCall)
2737 return false;
2738
Chad Rosier871f6642011-05-18 19:59:50 +00002739 // Do not sibcall optimize vararg calls unless all arguments are passed via
Chad Rosiera1660892011-05-20 00:59:28 +00002740 // registers.
Chad Rosier871f6642011-05-18 19:59:50 +00002741 if (isVarArg && !Outs.empty()) {
Chad Rosiera1660892011-05-20 00:59:28 +00002742
2743 // Optimizing for varargs on Win64 is unlikely to be safe without
2744 // additional testing.
2745 if (Subtarget->isTargetWin64())
2746 return false;
2747
Chad Rosier871f6642011-05-18 19:59:50 +00002748 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002749 CCState CCInfo(CalleeCC, isVarArg, DAG.getMachineFunction(),
Craig Topper0fbf3642012-04-23 03:28:34 +00002750 getTargetMachine(), ArgLocs, *DAG.getContext());
Chad Rosier871f6642011-05-18 19:59:50 +00002751
Chad Rosier871f6642011-05-18 19:59:50 +00002752 CCInfo.AnalyzeCallOperands(Outs, CC_X86);
2753 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i)
2754 if (!ArgLocs[i].isRegLoc())
2755 return false;
2756 }
2757
Chad Rosier30450e82011-12-22 22:35:21 +00002758 // If the call result is in ST0 / ST1, it needs to be popped off the x87
2759 // stack. Therefore, if it's not used by the call it is not safe to optimize
2760 // this into a sibcall.
Evan Chengf5b9d6c2010-03-20 02:58:15 +00002761 bool Unused = false;
2762 for (unsigned i = 0, e = Ins.size(); i != e; ++i) {
2763 if (!Ins[i].Used) {
2764 Unused = true;
2765 break;
2766 }
2767 }
2768 if (Unused) {
2769 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002770 CCState CCInfo(CalleeCC, false, DAG.getMachineFunction(),
Craig Topper0fbf3642012-04-23 03:28:34 +00002771 getTargetMachine(), RVLocs, *DAG.getContext());
Evan Chengf5b9d6c2010-03-20 02:58:15 +00002772 CCInfo.AnalyzeCallResult(Ins, RetCC_X86);
Evan Cheng13617962010-04-30 01:12:32 +00002773 for (unsigned i = 0, e = RVLocs.size(); i != e; ++i) {
Evan Chengf5b9d6c2010-03-20 02:58:15 +00002774 CCValAssign &VA = RVLocs[i];
2775 if (VA.getLocReg() == X86::ST0 || VA.getLocReg() == X86::ST1)
2776 return false;
2777 }
2778 }
2779
Evan Cheng13617962010-04-30 01:12:32 +00002780 // If the calling conventions do not match, then we'd better make sure the
2781 // results are returned in the same way as what the caller expects.
2782 if (!CCMatch) {
2783 SmallVector<CCValAssign, 16> RVLocs1;
Eric Christopher471e4222011-06-08 23:55:35 +00002784 CCState CCInfo1(CalleeCC, false, DAG.getMachineFunction(),
Craig Topper0fbf3642012-04-23 03:28:34 +00002785 getTargetMachine(), RVLocs1, *DAG.getContext());
Evan Cheng13617962010-04-30 01:12:32 +00002786 CCInfo1.AnalyzeCallResult(Ins, RetCC_X86);
2787
2788 SmallVector<CCValAssign, 16> RVLocs2;
Eric Christopher471e4222011-06-08 23:55:35 +00002789 CCState CCInfo2(CallerCC, false, DAG.getMachineFunction(),
Craig Topper0fbf3642012-04-23 03:28:34 +00002790 getTargetMachine(), RVLocs2, *DAG.getContext());
Evan Cheng13617962010-04-30 01:12:32 +00002791 CCInfo2.AnalyzeCallResult(Ins, RetCC_X86);
2792
2793 if (RVLocs1.size() != RVLocs2.size())
2794 return false;
2795 for (unsigned i = 0, e = RVLocs1.size(); i != e; ++i) {
2796 if (RVLocs1[i].isRegLoc() != RVLocs2[i].isRegLoc())
2797 return false;
2798 if (RVLocs1[i].getLocInfo() != RVLocs2[i].getLocInfo())
2799 return false;
2800 if (RVLocs1[i].isRegLoc()) {
2801 if (RVLocs1[i].getLocReg() != RVLocs2[i].getLocReg())
2802 return false;
2803 } else {
2804 if (RVLocs1[i].getLocMemOffset() != RVLocs2[i].getLocMemOffset())
2805 return false;
2806 }
2807 }
2808 }
2809
Evan Chenga6bff982010-01-30 01:22:00 +00002810 // If the callee takes no arguments then go on to check the results of the
2811 // call.
2812 if (!Outs.empty()) {
2813 // Check if stack adjustment is needed. For now, do not do this if any
2814 // argument is passed on the stack.
2815 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002816 CCState CCInfo(CalleeCC, isVarArg, DAG.getMachineFunction(),
Craig Topper0fbf3642012-04-23 03:28:34 +00002817 getTargetMachine(), ArgLocs, *DAG.getContext());
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00002818
2819 // Allocate shadow area for Win64
2820 if (Subtarget->isTargetWin64()) {
2821 CCInfo.AllocateStack(32, 8);
2822 }
2823
Duncan Sands45907662010-10-31 13:21:44 +00002824 CCInfo.AnalyzeCallOperands(Outs, CC_X86);
Stuart Hastings6db2c2f2011-05-17 16:59:46 +00002825 if (CCInfo.getNextStackOffset()) {
Evan Chengb2c92902010-02-02 02:22:50 +00002826 MachineFunction &MF = DAG.getMachineFunction();
2827 if (MF.getInfo<X86MachineFunctionInfo>()->getBytesToPopOnReturn())
2828 return false;
Evan Chengb2c92902010-02-02 02:22:50 +00002829
2830 // Check if the arguments are already laid out in the right way as
2831 // the caller's fixed stack objects.
2832 MachineFrameInfo *MFI = MF.getFrameInfo();
Evan Cheng5f941932010-02-05 02:21:12 +00002833 const MachineRegisterInfo *MRI = &MF.getRegInfo();
2834 const X86InstrInfo *TII =
2835 ((X86TargetMachine&)getTargetMachine()).getInstrInfo();
Evan Chengb2c92902010-02-02 02:22:50 +00002836 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2837 CCValAssign &VA = ArgLocs[i];
Dan Gohmanc9403652010-07-07 15:54:55 +00002838 SDValue Arg = OutVals[i];
Evan Chengb2c92902010-02-02 02:22:50 +00002839 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Evan Chengb2c92902010-02-02 02:22:50 +00002840 if (VA.getLocInfo() == CCValAssign::Indirect)
2841 return false;
2842 if (!VA.isRegLoc()) {
Evan Cheng5f941932010-02-05 02:21:12 +00002843 if (!MatchingStackOffset(Arg, VA.getLocMemOffset(), Flags,
2844 MFI, MRI, TII))
Evan Chengb2c92902010-02-02 02:22:50 +00002845 return false;
2846 }
2847 }
2848 }
Evan Cheng9c044672010-05-29 01:35:22 +00002849
2850 // If the tailcall address may be in a register, then make sure it's
2851 // possible to register allocate for it. In 32-bit, the call address can
2852 // only target EAX, EDX, or ECX since the tail call must be scheduled after
Evan Chengdedd9742010-07-14 06:44:01 +00002853 // callee-saved registers are restored. These happen to be the same
2854 // registers used to pass 'inreg' arguments so watch out for those.
2855 if (!Subtarget->is64Bit() &&
2856 !isa<GlobalAddressSDNode>(Callee) &&
Evan Cheng9c044672010-05-29 01:35:22 +00002857 !isa<ExternalSymbolSDNode>(Callee)) {
Evan Cheng9c044672010-05-29 01:35:22 +00002858 unsigned NumInRegs = 0;
2859 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2860 CCValAssign &VA = ArgLocs[i];
Evan Chengdedd9742010-07-14 06:44:01 +00002861 if (!VA.isRegLoc())
2862 continue;
2863 unsigned Reg = VA.getLocReg();
2864 switch (Reg) {
2865 default: break;
2866 case X86::EAX: case X86::EDX: case X86::ECX:
2867 if (++NumInRegs == 3)
Evan Cheng9c044672010-05-29 01:35:22 +00002868 return false;
Evan Chengdedd9742010-07-14 06:44:01 +00002869 break;
Evan Cheng9c044672010-05-29 01:35:22 +00002870 }
2871 }
2872 }
Evan Chenga6bff982010-01-30 01:22:00 +00002873 }
Evan Chengb1712452010-01-27 06:25:16 +00002874
Evan Cheng86809cc2010-02-03 03:28:02 +00002875 return true;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002876}
2877
Dan Gohman3df24e62008-09-03 23:12:08 +00002878FastISel *
Bob Wilsond49edb72012-08-03 04:06:28 +00002879X86TargetLowering::createFastISel(FunctionLoweringInfo &funcInfo,
2880 const TargetLibraryInfo *libInfo) const {
2881 return X86::createFastISel(funcInfo, libInfo);
Dan Gohmand9f3c482008-08-19 21:32:53 +00002882}
2883
2884
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00002885//===----------------------------------------------------------------------===//
2886// Other Lowering Hooks
2887//===----------------------------------------------------------------------===//
2888
Bruno Cardoso Lopese654b562010-09-01 00:51:36 +00002889static bool MayFoldLoad(SDValue Op) {
2890 return Op.hasOneUse() && ISD::isNormalLoad(Op.getNode());
2891}
2892
2893static bool MayFoldIntoStore(SDValue Op) {
2894 return Op.hasOneUse() && ISD::isNormalStore(*Op.getNode()->use_begin());
2895}
2896
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002897static bool isTargetShuffle(unsigned Opcode) {
2898 switch(Opcode) {
2899 default: return false;
2900 case X86ISD::PSHUFD:
2901 case X86ISD::PSHUFHW:
2902 case X86ISD::PSHUFLW:
Craig Topperb3982da2011-12-31 23:50:21 +00002903 case X86ISD::SHUFP:
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00002904 case X86ISD::PALIGN:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002905 case X86ISD::MOVLHPS:
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00002906 case X86ISD::MOVLHPD:
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00002907 case X86ISD::MOVHLPS:
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00002908 case X86ISD::MOVLPS:
2909 case X86ISD::MOVLPD:
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00002910 case X86ISD::MOVSHDUP:
Bruno Cardoso Lopes013bb3d2010-08-31 22:35:05 +00002911 case X86ISD::MOVSLDUP:
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00002912 case X86ISD::MOVDDUP:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002913 case X86ISD::MOVSS:
2914 case X86ISD::MOVSD:
Craig Topper34671b82011-12-06 08:21:25 +00002915 case X86ISD::UNPCKL:
2916 case X86ISD::UNPCKH:
Craig Topper316cd2a2011-11-30 06:25:25 +00002917 case X86ISD::VPERMILP:
Craig Topperec24e612011-11-30 07:47:51 +00002918 case X86ISD::VPERM2X128:
Craig Topperbdcbcb32012-05-06 18:54:26 +00002919 case X86ISD::VPERMI:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002920 return true;
2921 }
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002922}
2923
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00002924static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
Craig Topper3d092db2012-03-21 02:14:01 +00002925 SDValue V1, SelectionDAG &DAG) {
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00002926 switch(Opc) {
2927 default: llvm_unreachable("Unknown x86 shuffle node");
2928 case X86ISD::MOVSHDUP:
Bruno Cardoso Lopes013bb3d2010-08-31 22:35:05 +00002929 case X86ISD::MOVSLDUP:
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00002930 case X86ISD::MOVDDUP:
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00002931 return DAG.getNode(Opc, dl, VT, V1);
2932 }
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00002933}
2934
2935static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
Craig Topper3d092db2012-03-21 02:14:01 +00002936 SDValue V1, unsigned TargetMask,
2937 SelectionDAG &DAG) {
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00002938 switch(Opc) {
2939 default: llvm_unreachable("Unknown x86 shuffle node");
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002940 case X86ISD::PSHUFD:
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00002941 case X86ISD::PSHUFHW:
2942 case X86ISD::PSHUFLW:
Craig Topper316cd2a2011-11-30 06:25:25 +00002943 case X86ISD::VPERMILP:
Craig Topper8325c112012-04-16 00:41:45 +00002944 case X86ISD::VPERMI:
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00002945 return DAG.getNode(Opc, dl, VT, V1, DAG.getConstant(TargetMask, MVT::i8));
2946 }
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00002947}
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00002948
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002949static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
Craig Topper3d092db2012-03-21 02:14:01 +00002950 SDValue V1, SDValue V2, unsigned TargetMask,
2951 SelectionDAG &DAG) {
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002952 switch(Opc) {
2953 default: llvm_unreachable("Unknown x86 shuffle node");
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00002954 case X86ISD::PALIGN:
Craig Topperb3982da2011-12-31 23:50:21 +00002955 case X86ISD::SHUFP:
Craig Topperec24e612011-11-30 07:47:51 +00002956 case X86ISD::VPERM2X128:
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002957 return DAG.getNode(Opc, dl, VT, V1, V2,
2958 DAG.getConstant(TargetMask, MVT::i8));
2959 }
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002960}
2961
2962static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
2963 SDValue V1, SDValue V2, SelectionDAG &DAG) {
2964 switch(Opc) {
2965 default: llvm_unreachable("Unknown x86 shuffle node");
2966 case X86ISD::MOVLHPS:
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00002967 case X86ISD::MOVLHPD:
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00002968 case X86ISD::MOVHLPS:
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00002969 case X86ISD::MOVLPS:
2970 case X86ISD::MOVLPD:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002971 case X86ISD::MOVSS:
2972 case X86ISD::MOVSD:
Craig Topper34671b82011-12-06 08:21:25 +00002973 case X86ISD::UNPCKL:
2974 case X86ISD::UNPCKH:
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002975 return DAG.getNode(Opc, dl, VT, V1, V2);
2976 }
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002977}
2978
Dan Gohmand858e902010-04-17 15:26:15 +00002979SDValue X86TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) const {
Anton Korobeynikova2780e12007-08-15 17:12:32 +00002980 MachineFunction &MF = DAG.getMachineFunction();
2981 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
2982 int ReturnAddrIndex = FuncInfo->getRAIndex();
2983
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002984 if (ReturnAddrIndex == 0) {
2985 // Set up a frame object for the return address.
Bill Wendling64e87322009-01-16 19:25:27 +00002986 uint64_t SlotSize = TD->getPointerSize();
David Greene3f2bf852009-11-12 20:49:22 +00002987 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(SlotSize, -SlotSize,
Evan Chenged2ae132010-07-03 00:40:23 +00002988 false);
Anton Korobeynikova2780e12007-08-15 17:12:32 +00002989 FuncInfo->setRAIndex(ReturnAddrIndex);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002990 }
2991
Evan Cheng25ab6902006-09-08 06:48:29 +00002992 return DAG.getFrameIndex(ReturnAddrIndex, getPointerTy());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002993}
2994
2995
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00002996bool X86::isOffsetSuitableForCodeModel(int64_t Offset, CodeModel::Model M,
2997 bool hasSymbolicDisplacement) {
2998 // Offset should fit into 32 bit immediate field.
Benjamin Kramer34247a02010-03-29 21:13:41 +00002999 if (!isInt<32>(Offset))
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00003000 return false;
3001
3002 // If we don't have a symbolic displacement - we don't have any extra
3003 // restrictions.
3004 if (!hasSymbolicDisplacement)
3005 return true;
3006
3007 // FIXME: Some tweaks might be needed for medium code model.
3008 if (M != CodeModel::Small && M != CodeModel::Kernel)
3009 return false;
3010
3011 // For small code model we assume that latest object is 16MB before end of 31
3012 // bits boundary. We may also accept pretty large negative constants knowing
3013 // that all objects are in the positive half of address space.
3014 if (M == CodeModel::Small && Offset < 16*1024*1024)
3015 return true;
3016
3017 // For kernel code model we know that all object resist in the negative half
3018 // of 32bits address space. We may not accept negative offsets, since they may
3019 // be just off and we may accept pretty large positive ones.
3020 if (M == CodeModel::Kernel && Offset > 0)
3021 return true;
3022
3023 return false;
3024}
3025
Evan Chengef41ff62011-06-23 17:54:54 +00003026/// isCalleePop - Determines whether the callee is required to pop its
3027/// own arguments. Callee pop is necessary to support tail calls.
3028bool X86::isCalleePop(CallingConv::ID CallingConv,
3029 bool is64Bit, bool IsVarArg, bool TailCallOpt) {
3030 if (IsVarArg)
3031 return false;
3032
3033 switch (CallingConv) {
3034 default:
3035 return false;
3036 case CallingConv::X86_StdCall:
3037 return !is64Bit;
3038 case CallingConv::X86_FastCall:
3039 return !is64Bit;
3040 case CallingConv::X86_ThisCall:
3041 return !is64Bit;
3042 case CallingConv::Fast:
3043 return TailCallOpt;
3044 case CallingConv::GHC:
3045 return TailCallOpt;
3046 }
3047}
3048
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003049/// TranslateX86CC - do a one to one translation of a ISD::CondCode to the X86
3050/// specific condition code, returning the condition code and the LHS/RHS of the
3051/// comparison to make.
3052static unsigned TranslateX86CC(ISD::CondCode SetCCOpcode, bool isFP,
3053 SDValue &LHS, SDValue &RHS, SelectionDAG &DAG) {
Evan Chengd9558e02006-01-06 00:43:03 +00003054 if (!isFP) {
Chris Lattnerbfd68a72006-09-13 17:04:54 +00003055 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS)) {
3056 if (SetCCOpcode == ISD::SETGT && RHSC->isAllOnesValue()) {
3057 // X > -1 -> X == 0, jump !sign.
3058 RHS = DAG.getConstant(0, RHS.getValueType());
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003059 return X86::COND_NS;
Craig Topper69947b92012-04-23 06:57:04 +00003060 }
3061 if (SetCCOpcode == ISD::SETLT && RHSC->isNullValue()) {
Chris Lattnerbfd68a72006-09-13 17:04:54 +00003062 // X < 0 -> X == 0, jump on sign.
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003063 return X86::COND_S;
Craig Topper69947b92012-04-23 06:57:04 +00003064 }
3065 if (SetCCOpcode == ISD::SETLT && RHSC->getZExtValue() == 1) {
Dan Gohman5f6913c2007-09-17 14:49:27 +00003066 // X < 1 -> X <= 0
3067 RHS = DAG.getConstant(0, RHS.getValueType());
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003068 return X86::COND_LE;
Chris Lattnerbfd68a72006-09-13 17:04:54 +00003069 }
Chris Lattnerf9570512006-09-13 03:22:10 +00003070 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00003071
Evan Chengd9558e02006-01-06 00:43:03 +00003072 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00003073 default: llvm_unreachable("Invalid integer condition!");
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003074 case ISD::SETEQ: return X86::COND_E;
3075 case ISD::SETGT: return X86::COND_G;
3076 case ISD::SETGE: return X86::COND_GE;
3077 case ISD::SETLT: return X86::COND_L;
3078 case ISD::SETLE: return X86::COND_LE;
3079 case ISD::SETNE: return X86::COND_NE;
3080 case ISD::SETULT: return X86::COND_B;
3081 case ISD::SETUGT: return X86::COND_A;
3082 case ISD::SETULE: return X86::COND_BE;
3083 case ISD::SETUGE: return X86::COND_AE;
Evan Chengd9558e02006-01-06 00:43:03 +00003084 }
Chris Lattner4c78e022008-12-23 23:42:27 +00003085 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003086
Chris Lattner4c78e022008-12-23 23:42:27 +00003087 // First determine if it is required or is profitable to flip the operands.
Duncan Sands4047f4a2008-10-24 13:03:10 +00003088
Chris Lattner4c78e022008-12-23 23:42:27 +00003089 // If LHS is a foldable load, but RHS is not, flip the condition.
Rafael Espindolaf297c932011-02-03 03:58:05 +00003090 if (ISD::isNON_EXTLoad(LHS.getNode()) &&
3091 !ISD::isNON_EXTLoad(RHS.getNode())) {
Chris Lattner4c78e022008-12-23 23:42:27 +00003092 SetCCOpcode = getSetCCSwappedOperands(SetCCOpcode);
3093 std::swap(LHS, RHS);
Evan Cheng4d46d0a2008-08-28 23:48:31 +00003094 }
3095
Chris Lattner4c78e022008-12-23 23:42:27 +00003096 switch (SetCCOpcode) {
3097 default: break;
3098 case ISD::SETOLT:
3099 case ISD::SETOLE:
3100 case ISD::SETUGT:
3101 case ISD::SETUGE:
3102 std::swap(LHS, RHS);
3103 break;
3104 }
3105
3106 // On a floating point condition, the flags are set as follows:
3107 // ZF PF CF op
3108 // 0 | 0 | 0 | X > Y
3109 // 0 | 0 | 1 | X < Y
3110 // 1 | 0 | 0 | X == Y
3111 // 1 | 1 | 1 | unordered
3112 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00003113 default: llvm_unreachable("Condcode should be pre-legalized away");
Chris Lattner4c78e022008-12-23 23:42:27 +00003114 case ISD::SETUEQ:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003115 case ISD::SETEQ: return X86::COND_E;
Chris Lattner4c78e022008-12-23 23:42:27 +00003116 case ISD::SETOLT: // flipped
3117 case ISD::SETOGT:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003118 case ISD::SETGT: return X86::COND_A;
Chris Lattner4c78e022008-12-23 23:42:27 +00003119 case ISD::SETOLE: // flipped
3120 case ISD::SETOGE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003121 case ISD::SETGE: return X86::COND_AE;
Chris Lattner4c78e022008-12-23 23:42:27 +00003122 case ISD::SETUGT: // flipped
3123 case ISD::SETULT:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003124 case ISD::SETLT: return X86::COND_B;
Chris Lattner4c78e022008-12-23 23:42:27 +00003125 case ISD::SETUGE: // flipped
3126 case ISD::SETULE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003127 case ISD::SETLE: return X86::COND_BE;
Chris Lattner4c78e022008-12-23 23:42:27 +00003128 case ISD::SETONE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003129 case ISD::SETNE: return X86::COND_NE;
3130 case ISD::SETUO: return X86::COND_P;
3131 case ISD::SETO: return X86::COND_NP;
Dan Gohman1a492952009-10-20 16:22:37 +00003132 case ISD::SETOEQ:
3133 case ISD::SETUNE: return X86::COND_INVALID;
Chris Lattner4c78e022008-12-23 23:42:27 +00003134 }
Evan Chengd9558e02006-01-06 00:43:03 +00003135}
3136
Evan Cheng4a460802006-01-11 00:33:36 +00003137/// hasFPCMov - is there a floating point cmov for the specific X86 condition
3138/// code. Current x86 isa includes the following FP cmov instructions:
Evan Chengaaca22c2006-01-10 20:26:56 +00003139/// fcmovb, fcomvbe, fcomve, fcmovu, fcmovae, fcmova, fcmovne, fcmovnu.
Evan Cheng4a460802006-01-11 00:33:36 +00003140static bool hasFPCMov(unsigned X86CC) {
Evan Chengaaca22c2006-01-10 20:26:56 +00003141 switch (X86CC) {
3142 default:
3143 return false;
Chris Lattner7fbe9722006-10-20 17:42:20 +00003144 case X86::COND_B:
3145 case X86::COND_BE:
3146 case X86::COND_E:
3147 case X86::COND_P:
3148 case X86::COND_A:
3149 case X86::COND_AE:
3150 case X86::COND_NE:
3151 case X86::COND_NP:
Evan Chengaaca22c2006-01-10 20:26:56 +00003152 return true;
3153 }
3154}
3155
Evan Chengeb2f9692009-10-27 19:56:55 +00003156/// isFPImmLegal - Returns true if the target can instruction select the
3157/// specified FP immediate natively. If false, the legalizer will
3158/// materialize the FP immediate as a load from a constant pool.
Evan Chenga1eaa3c2009-10-28 01:43:28 +00003159bool X86TargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
Evan Chengeb2f9692009-10-27 19:56:55 +00003160 for (unsigned i = 0, e = LegalFPImmediates.size(); i != e; ++i) {
3161 if (Imm.bitwiseIsEqual(LegalFPImmediates[i]))
3162 return true;
3163 }
3164 return false;
3165}
3166
Nate Begeman9008ca62009-04-27 18:41:29 +00003167/// isUndefOrInRange - Return true if Val is undef or if its value falls within
3168/// the specified range (L, H].
3169static bool isUndefOrInRange(int Val, int Low, int Hi) {
3170 return (Val < 0) || (Val >= Low && Val < Hi);
3171}
3172
3173/// isUndefOrEqual - Val is either less than zero (undef) or equal to the
3174/// specified value.
3175static bool isUndefOrEqual(int Val, int CmpVal) {
3176 if (Val < 0 || Val == CmpVal)
Evan Cheng5ced1d82006-04-06 23:23:56 +00003177 return true;
Nate Begeman9008ca62009-04-27 18:41:29 +00003178 return false;
Evan Chengc5cdff22006-04-07 21:53:05 +00003179}
3180
Benjamin Kramerd9b0b022012-06-02 10:20:22 +00003181/// isSequentialOrUndefInRange - Return true if every element in Mask, beginning
Bruno Cardoso Lopes4002d7e2011-08-12 21:54:42 +00003182/// from position Pos and ending in Pos+Size, falls within the specified
3183/// sequential range (L, L+Pos]. or is undef.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003184static bool isSequentialOrUndefInRange(ArrayRef<int> Mask,
Craig Topperb6072642012-05-03 07:26:59 +00003185 unsigned Pos, unsigned Size, int Low) {
3186 for (unsigned i = Pos, e = Pos+Size; i != e; ++i, ++Low)
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003187 if (!isUndefOrEqual(Mask[i], Low))
3188 return false;
3189 return true;
3190}
3191
Nate Begeman9008ca62009-04-27 18:41:29 +00003192/// isPSHUFDMask - Return true if the node specifies a shuffle of elements that
3193/// is suitable for input to PSHUFD or PSHUFW. That is, it doesn't reference
3194/// the second operand.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003195static bool isPSHUFDMask(ArrayRef<int> Mask, EVT VT) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00003196 if (VT == MVT::v4f32 || VT == MVT::v4i32 )
Nate Begeman9008ca62009-04-27 18:41:29 +00003197 return (Mask[0] < 4 && Mask[1] < 4 && Mask[2] < 4 && Mask[3] < 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00003198 if (VT == MVT::v2f64 || VT == MVT::v2i64)
Nate Begeman9008ca62009-04-27 18:41:29 +00003199 return (Mask[0] < 2 && Mask[1] < 2);
3200 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003201}
3202
Nate Begeman9008ca62009-04-27 18:41:29 +00003203/// isPSHUFHWMask - Return true if the node specifies a shuffle of elements that
3204/// is suitable for input to PSHUFHW.
Craig Toppera9a568a2012-05-02 08:03:44 +00003205static bool isPSHUFHWMask(ArrayRef<int> Mask, EVT VT, bool HasAVX2) {
3206 if (VT != MVT::v8i16 && (!HasAVX2 || VT != MVT::v16i16))
Evan Cheng0188ecb2006-03-22 18:59:22 +00003207 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003208
Nate Begeman9008ca62009-04-27 18:41:29 +00003209 // Lower quadword copied in order or undef.
Craig Topperc612d792012-01-02 09:17:37 +00003210 if (!isSequentialOrUndefInRange(Mask, 0, 4, 0))
3211 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003212
Evan Cheng506d3df2006-03-29 23:07:14 +00003213 // Upper quadword shuffled.
Craig Topperc612d792012-01-02 09:17:37 +00003214 for (unsigned i = 4; i != 8; ++i)
Craig Toppera9a568a2012-05-02 08:03:44 +00003215 if (!isUndefOrInRange(Mask[i], 4, 8))
Evan Cheng506d3df2006-03-29 23:07:14 +00003216 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003217
Craig Toppera9a568a2012-05-02 08:03:44 +00003218 if (VT == MVT::v16i16) {
3219 // Lower quadword copied in order or undef.
3220 if (!isSequentialOrUndefInRange(Mask, 8, 4, 8))
3221 return false;
3222
3223 // Upper quadword shuffled.
3224 for (unsigned i = 12; i != 16; ++i)
3225 if (!isUndefOrInRange(Mask[i], 12, 16))
3226 return false;
3227 }
3228
Evan Cheng506d3df2006-03-29 23:07:14 +00003229 return true;
3230}
3231
Nate Begeman9008ca62009-04-27 18:41:29 +00003232/// isPSHUFLWMask - Return true if the node specifies a shuffle of elements that
3233/// is suitable for input to PSHUFLW.
Craig Toppera9a568a2012-05-02 08:03:44 +00003234static bool isPSHUFLWMask(ArrayRef<int> Mask, EVT VT, bool HasAVX2) {
3235 if (VT != MVT::v8i16 && (!HasAVX2 || VT != MVT::v16i16))
Evan Cheng506d3df2006-03-29 23:07:14 +00003236 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003237
Rafael Espindola15684b22009-04-24 12:40:33 +00003238 // Upper quadword copied in order.
Craig Topperc612d792012-01-02 09:17:37 +00003239 if (!isSequentialOrUndefInRange(Mask, 4, 4, 4))
3240 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003241
Rafael Espindola15684b22009-04-24 12:40:33 +00003242 // Lower quadword shuffled.
Craig Topperc612d792012-01-02 09:17:37 +00003243 for (unsigned i = 0; i != 4; ++i)
Craig Toppera9a568a2012-05-02 08:03:44 +00003244 if (!isUndefOrInRange(Mask[i], 0, 4))
Rafael Espindola15684b22009-04-24 12:40:33 +00003245 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003246
Craig Toppera9a568a2012-05-02 08:03:44 +00003247 if (VT == MVT::v16i16) {
3248 // Upper quadword copied in order.
3249 if (!isSequentialOrUndefInRange(Mask, 12, 4, 12))
3250 return false;
3251
3252 // Lower quadword shuffled.
3253 for (unsigned i = 8; i != 12; ++i)
3254 if (!isUndefOrInRange(Mask[i], 8, 12))
3255 return false;
3256 }
3257
Rafael Espindola15684b22009-04-24 12:40:33 +00003258 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00003259}
3260
Nate Begemana09008b2009-10-19 02:17:23 +00003261/// isPALIGNRMask - Return true if the node specifies a shuffle of elements that
3262/// is suitable for input to PALIGNR.
Craig Topper0e2037b2012-01-20 05:53:00 +00003263static bool isPALIGNRMask(ArrayRef<int> Mask, EVT VT,
3264 const X86Subtarget *Subtarget) {
3265 if ((VT.getSizeInBits() == 128 && !Subtarget->hasSSSE3()) ||
3266 (VT.getSizeInBits() == 256 && !Subtarget->hasAVX2()))
Bruno Cardoso Lopes9065d4b2011-07-29 01:30:59 +00003267 return false;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003268
Craig Topper0e2037b2012-01-20 05:53:00 +00003269 unsigned NumElts = VT.getVectorNumElements();
3270 unsigned NumLanes = VT.getSizeInBits()/128;
3271 unsigned NumLaneElts = NumElts/NumLanes;
3272
3273 // Do not handle 64-bit element shuffles with palignr.
3274 if (NumLaneElts == 2)
Nate Begemana09008b2009-10-19 02:17:23 +00003275 return false;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003276
Craig Topper0e2037b2012-01-20 05:53:00 +00003277 for (unsigned l = 0; l != NumElts; l+=NumLaneElts) {
3278 unsigned i;
3279 for (i = 0; i != NumLaneElts; ++i) {
3280 if (Mask[i+l] >= 0)
3281 break;
3282 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00003283
Craig Topper0e2037b2012-01-20 05:53:00 +00003284 // Lane is all undef, go to next lane
3285 if (i == NumLaneElts)
3286 continue;
Nate Begemana09008b2009-10-19 02:17:23 +00003287
Craig Topper0e2037b2012-01-20 05:53:00 +00003288 int Start = Mask[i+l];
Nate Begemana09008b2009-10-19 02:17:23 +00003289
Craig Topper0e2037b2012-01-20 05:53:00 +00003290 // Make sure its in this lane in one of the sources
3291 if (!isUndefOrInRange(Start, l, l+NumLaneElts) &&
3292 !isUndefOrInRange(Start, l+NumElts, l+NumElts+NumLaneElts))
Nate Begemana09008b2009-10-19 02:17:23 +00003293 return false;
Craig Topper0e2037b2012-01-20 05:53:00 +00003294
3295 // If not lane 0, then we must match lane 0
3296 if (l != 0 && Mask[i] >= 0 && !isUndefOrEqual(Start, Mask[i]+l))
3297 return false;
3298
3299 // Correct second source to be contiguous with first source
3300 if (Start >= (int)NumElts)
3301 Start -= NumElts - NumLaneElts;
3302
3303 // Make sure we're shifting in the right direction.
3304 if (Start <= (int)(i+l))
3305 return false;
3306
3307 Start -= i;
3308
3309 // Check the rest of the elements to see if they are consecutive.
3310 for (++i; i != NumLaneElts; ++i) {
3311 int Idx = Mask[i+l];
3312
3313 // Make sure its in this lane
3314 if (!isUndefOrInRange(Idx, l, l+NumLaneElts) &&
3315 !isUndefOrInRange(Idx, l+NumElts, l+NumElts+NumLaneElts))
3316 return false;
3317
3318 // If not lane 0, then we must match lane 0
3319 if (l != 0 && Mask[i] >= 0 && !isUndefOrEqual(Idx, Mask[i]+l))
3320 return false;
3321
3322 if (Idx >= (int)NumElts)
3323 Idx -= NumElts - NumLaneElts;
3324
3325 if (!isUndefOrEqual(Idx, Start+i))
3326 return false;
3327
3328 }
Nate Begemana09008b2009-10-19 02:17:23 +00003329 }
Craig Topper0e2037b2012-01-20 05:53:00 +00003330
Nate Begemana09008b2009-10-19 02:17:23 +00003331 return true;
3332}
3333
Craig Topper1a7700a2012-01-19 08:19:12 +00003334/// CommuteVectorShuffleMask - Change values in a shuffle permute mask assuming
3335/// the two vector operands have swapped position.
3336static void CommuteVectorShuffleMask(SmallVectorImpl<int> &Mask,
3337 unsigned NumElems) {
3338 for (unsigned i = 0; i != NumElems; ++i) {
3339 int idx = Mask[i];
3340 if (idx < 0)
3341 continue;
3342 else if (idx < (int)NumElems)
3343 Mask[i] = idx + NumElems;
3344 else
3345 Mask[i] = idx - NumElems;
3346 }
3347}
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00003348
Craig Topper1a7700a2012-01-19 08:19:12 +00003349/// isSHUFPMask - Return true if the specified VECTOR_SHUFFLE operand
3350/// specifies a shuffle of elements that is suitable for input to 128/256-bit
3351/// SHUFPS and SHUFPD. If Commuted is true, then it checks for sources to be
3352/// reverse of what x86 shuffles want.
3353static bool isSHUFPMask(ArrayRef<int> Mask, EVT VT, bool HasAVX,
3354 bool Commuted = false) {
3355 if (!HasAVX && VT.getSizeInBits() == 256)
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00003356 return false;
3357
Craig Topper1a7700a2012-01-19 08:19:12 +00003358 unsigned NumElems = VT.getVectorNumElements();
3359 unsigned NumLanes = VT.getSizeInBits()/128;
3360 unsigned NumLaneElems = NumElems/NumLanes;
3361
3362 if (NumLaneElems != 2 && NumLaneElems != 4)
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00003363 return false;
3364
3365 // VSHUFPSY divides the resulting vector into 4 chunks.
3366 // The sources are also splitted into 4 chunks, and each destination
3367 // chunk must come from a different source chunk.
3368 //
3369 // SRC1 => X7 X6 X5 X4 X3 X2 X1 X0
3370 // SRC2 => Y7 Y6 Y5 Y4 Y3 Y2 Y1 Y9
3371 //
3372 // DST => Y7..Y4, Y7..Y4, X7..X4, X7..X4,
3373 // Y3..Y0, Y3..Y0, X3..X0, X3..X0
3374 //
Craig Topper9d7025b2011-11-27 21:41:12 +00003375 // VSHUFPDY divides the resulting vector into 4 chunks.
3376 // The sources are also splitted into 4 chunks, and each destination
3377 // chunk must come from a different source chunk.
3378 //
3379 // SRC1 => X3 X2 X1 X0
3380 // SRC2 => Y3 Y2 Y1 Y0
3381 //
3382 // DST => Y3..Y2, X3..X2, Y1..Y0, X1..X0
3383 //
Craig Topper1a7700a2012-01-19 08:19:12 +00003384 unsigned HalfLaneElems = NumLaneElems/2;
3385 for (unsigned l = 0; l != NumElems; l += NumLaneElems) {
3386 for (unsigned i = 0; i != NumLaneElems; ++i) {
3387 int Idx = Mask[i+l];
3388 unsigned RngStart = l + ((Commuted == (i<HalfLaneElems)) ? NumElems : 0);
3389 if (!isUndefOrInRange(Idx, RngStart, RngStart+NumLaneElems))
3390 return false;
3391 // For VSHUFPSY, the mask of the second half must be the same as the
3392 // first but with the appropriate offsets. This works in the same way as
3393 // VPERMILPS works with masks.
3394 if (NumElems != 8 || l == 0 || Mask[i] < 0)
3395 continue;
3396 if (!isUndefOrEqual(Idx, Mask[i]+l))
3397 return false;
Craig Topper1ff73d72011-12-06 04:59:07 +00003398 }
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00003399 }
3400
3401 return true;
3402}
3403
Evan Cheng2c0dbd02006-03-24 02:58:06 +00003404/// isMOVHLPSMask - Return true if the specified VECTOR_SHUFFLE operand
3405/// specifies a shuffle of elements that is suitable for input to MOVHLPS.
Craig Topperdd637ae2012-02-19 05:41:45 +00003406static bool isMOVHLPSMask(ArrayRef<int> Mask, EVT VT) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00003407 if (!VT.is128BitVector())
Bruno Cardoso Lopes61260052011-07-29 02:05:28 +00003408 return false;
3409
Craig Topper7a9a28b2012-08-12 02:23:29 +00003410 unsigned NumElems = VT.getVectorNumElements();
3411
Bruno Cardoso Lopes61260052011-07-29 02:05:28 +00003412 if (NumElems != 4)
Evan Cheng2c0dbd02006-03-24 02:58:06 +00003413 return false;
3414
Evan Cheng2064a2b2006-03-28 06:50:32 +00003415 // Expect bit0 == 6, bit1 == 7, bit2 == 2, bit3 == 3
Craig Topperdd637ae2012-02-19 05:41:45 +00003416 return isUndefOrEqual(Mask[0], 6) &&
3417 isUndefOrEqual(Mask[1], 7) &&
3418 isUndefOrEqual(Mask[2], 2) &&
3419 isUndefOrEqual(Mask[3], 3);
Evan Cheng6e56e2c2006-11-07 22:14:24 +00003420}
3421
Nate Begeman0b10b912009-11-07 23:17:15 +00003422/// isMOVHLPS_v_undef_Mask - Special case of isMOVHLPSMask for canonical form
3423/// of vector_shuffle v, v, <2, 3, 2, 3>, i.e. vector_shuffle v, undef,
3424/// <2, 3, 2, 3>
Craig Topperdd637ae2012-02-19 05:41:45 +00003425static bool isMOVHLPS_v_undef_Mask(ArrayRef<int> Mask, EVT VT) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00003426 if (!VT.is128BitVector())
Bruno Cardoso Lopes61260052011-07-29 02:05:28 +00003427 return false;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003428
Craig Topper7a9a28b2012-08-12 02:23:29 +00003429 unsigned NumElems = VT.getVectorNumElements();
3430
Nate Begeman0b10b912009-11-07 23:17:15 +00003431 if (NumElems != 4)
3432 return false;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003433
Craig Topperdd637ae2012-02-19 05:41:45 +00003434 return isUndefOrEqual(Mask[0], 2) &&
3435 isUndefOrEqual(Mask[1], 3) &&
3436 isUndefOrEqual(Mask[2], 2) &&
3437 isUndefOrEqual(Mask[3], 3);
Nate Begeman0b10b912009-11-07 23:17:15 +00003438}
3439
Evan Cheng5ced1d82006-04-06 23:23:56 +00003440/// isMOVLPMask - Return true if the specified VECTOR_SHUFFLE operand
3441/// specifies a shuffle of elements that is suitable for input to MOVLP{S|D}.
Craig Topperdd637ae2012-02-19 05:41:45 +00003442static bool isMOVLPMask(ArrayRef<int> Mask, EVT VT) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00003443 if (!VT.is128BitVector())
Elena Demikhovsky021c0a22011-12-28 08:14:01 +00003444 return false;
3445
Craig Topperdd637ae2012-02-19 05:41:45 +00003446 unsigned NumElems = VT.getVectorNumElements();
Evan Cheng5ced1d82006-04-06 23:23:56 +00003447
Evan Cheng5ced1d82006-04-06 23:23:56 +00003448 if (NumElems != 2 && NumElems != 4)
3449 return false;
3450
Chad Rosier238ae312012-04-30 17:47:15 +00003451 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
Craig Topperdd637ae2012-02-19 05:41:45 +00003452 if (!isUndefOrEqual(Mask[i], i + NumElems))
Evan Chengc5cdff22006-04-07 21:53:05 +00003453 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003454
Chad Rosier238ae312012-04-30 17:47:15 +00003455 for (unsigned i = NumElems/2, e = NumElems; i != e; ++i)
Craig Topperdd637ae2012-02-19 05:41:45 +00003456 if (!isUndefOrEqual(Mask[i], i))
Evan Chengc5cdff22006-04-07 21:53:05 +00003457 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003458
3459 return true;
3460}
3461
Nate Begeman0b10b912009-11-07 23:17:15 +00003462/// isMOVLHPSMask - Return true if the specified VECTOR_SHUFFLE operand
3463/// specifies a shuffle of elements that is suitable for input to MOVLHPS.
Craig Topperdd637ae2012-02-19 05:41:45 +00003464static bool isMOVLHPSMask(ArrayRef<int> Mask, EVT VT) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00003465 if (!VT.is128BitVector())
3466 return false;
3467
Craig Topperdd637ae2012-02-19 05:41:45 +00003468 unsigned NumElems = VT.getVectorNumElements();
Evan Cheng5ced1d82006-04-06 23:23:56 +00003469
Craig Topper7a9a28b2012-08-12 02:23:29 +00003470 if (NumElems != 2 && NumElems != 4)
Evan Cheng5ced1d82006-04-06 23:23:56 +00003471 return false;
3472
Chad Rosier238ae312012-04-30 17:47:15 +00003473 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
Craig Topperdd637ae2012-02-19 05:41:45 +00003474 if (!isUndefOrEqual(Mask[i], i))
Evan Chengc5cdff22006-04-07 21:53:05 +00003475 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003476
Chad Rosier238ae312012-04-30 17:47:15 +00003477 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
3478 if (!isUndefOrEqual(Mask[i + e], i + NumElems))
Evan Chengc5cdff22006-04-07 21:53:05 +00003479 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003480
3481 return true;
3482}
3483
Elena Demikhovsky15963732012-06-26 08:04:10 +00003484//
3485// Some special combinations that can be optimized.
3486//
3487static
3488SDValue Compact8x32ShuffleNode(ShuffleVectorSDNode *SVOp,
3489 SelectionDAG &DAG) {
3490 EVT VT = SVOp->getValueType(0);
Elena Demikhovsky15963732012-06-26 08:04:10 +00003491 DebugLoc dl = SVOp->getDebugLoc();
3492
3493 if (VT != MVT::v8i32 && VT != MVT::v8f32)
3494 return SDValue();
3495
3496 ArrayRef<int> Mask = SVOp->getMask();
3497
3498 // These are the special masks that may be optimized.
3499 static const int MaskToOptimizeEven[] = {0, 8, 2, 10, 4, 12, 6, 14};
3500 static const int MaskToOptimizeOdd[] = {1, 9, 3, 11, 5, 13, 7, 15};
3501 bool MatchEvenMask = true;
3502 bool MatchOddMask = true;
3503 for (int i=0; i<8; ++i) {
3504 if (!isUndefOrEqual(Mask[i], MaskToOptimizeEven[i]))
3505 MatchEvenMask = false;
3506 if (!isUndefOrEqual(Mask[i], MaskToOptimizeOdd[i]))
3507 MatchOddMask = false;
3508 }
3509 static const int CompactionMaskEven[] = {0, 2, -1, -1, 4, 6, -1, -1};
3510 static const int CompactionMaskOdd [] = {1, 3, -1, -1, 5, 7, -1, -1};
3511
3512 const int *CompactionMask;
3513 if (MatchEvenMask)
3514 CompactionMask = CompactionMaskEven;
3515 else if (MatchOddMask)
3516 CompactionMask = CompactionMaskOdd;
3517 else
3518 return SDValue();
3519
3520 SDValue UndefNode = DAG.getNode(ISD::UNDEF, dl, VT);
3521
3522 SDValue Op0 = DAG.getVectorShuffle(VT, dl, SVOp->getOperand(0),
3523 UndefNode, CompactionMask);
3524 SDValue Op1 = DAG.getVectorShuffle(VT, dl, SVOp->getOperand(1),
3525 UndefNode, CompactionMask);
3526 static const int UnpackMask[] = {0, 8, 1, 9, 4, 12, 5, 13};
3527 return DAG.getVectorShuffle(VT, dl, Op0, Op1, UnpackMask);
3528}
3529
Evan Cheng0038e592006-03-28 00:39:58 +00003530/// isUNPCKLMask - Return true if the specified VECTOR_SHUFFLE operand
3531/// specifies a shuffle of elements that is suitable for input to UNPCKL.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003532static bool isUNPCKLMask(ArrayRef<int> Mask, EVT VT,
Craig Topper6347e862011-11-21 06:57:39 +00003533 bool HasAVX2, bool V2IsSplat = false) {
Craig Topper94438ba2011-12-16 08:06:31 +00003534 unsigned NumElts = VT.getVectorNumElements();
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003535
3536 assert((VT.is128BitVector() || VT.is256BitVector()) &&
3537 "Unsupported vector type for unpckh");
3538
Craig Topper6347e862011-11-21 06:57:39 +00003539 if (VT.getSizeInBits() == 256 && NumElts != 4 && NumElts != 8 &&
Craig Topper6fa583d2011-11-21 08:26:50 +00003540 (!HasAVX2 || (NumElts != 16 && NumElts != 32)))
Evan Cheng0038e592006-03-28 00:39:58 +00003541 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003542
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003543 // Handle 128 and 256-bit vector lengths. AVX defines UNPCK* to operate
3544 // independently on 128-bit lanes.
3545 unsigned NumLanes = VT.getSizeInBits()/128;
3546 unsigned NumLaneElts = NumElts/NumLanes;
David Greenea20244d2011-03-02 17:23:43 +00003547
Craig Topper94438ba2011-12-16 08:06:31 +00003548 for (unsigned l = 0; l != NumLanes; ++l) {
3549 for (unsigned i = l*NumLaneElts, j = l*NumLaneElts;
3550 i != (l+1)*NumLaneElts;
David Greenea20244d2011-03-02 17:23:43 +00003551 i += 2, ++j) {
3552 int BitI = Mask[i];
3553 int BitI1 = Mask[i+1];
3554 if (!isUndefOrEqual(BitI, j))
Evan Cheng39623da2006-04-20 08:58:49 +00003555 return false;
David Greenea20244d2011-03-02 17:23:43 +00003556 if (V2IsSplat) {
3557 if (!isUndefOrEqual(BitI1, NumElts))
3558 return false;
3559 } else {
3560 if (!isUndefOrEqual(BitI1, j + NumElts))
3561 return false;
3562 }
Evan Cheng39623da2006-04-20 08:58:49 +00003563 }
Evan Cheng0038e592006-03-28 00:39:58 +00003564 }
David Greenea20244d2011-03-02 17:23:43 +00003565
Evan Cheng0038e592006-03-28 00:39:58 +00003566 return true;
3567}
3568
Evan Cheng4fcb9222006-03-28 02:43:26 +00003569/// isUNPCKHMask - Return true if the specified VECTOR_SHUFFLE operand
3570/// specifies a shuffle of elements that is suitable for input to UNPCKH.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003571static bool isUNPCKHMask(ArrayRef<int> Mask, EVT VT,
Craig Topper6347e862011-11-21 06:57:39 +00003572 bool HasAVX2, bool V2IsSplat = false) {
Craig Topper94438ba2011-12-16 08:06:31 +00003573 unsigned NumElts = VT.getVectorNumElements();
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003574
3575 assert((VT.is128BitVector() || VT.is256BitVector()) &&
3576 "Unsupported vector type for unpckh");
3577
Craig Topper6347e862011-11-21 06:57:39 +00003578 if (VT.getSizeInBits() == 256 && NumElts != 4 && NumElts != 8 &&
Craig Topper6fa583d2011-11-21 08:26:50 +00003579 (!HasAVX2 || (NumElts != 16 && NumElts != 32)))
Evan Cheng4fcb9222006-03-28 02:43:26 +00003580 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003581
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003582 // Handle 128 and 256-bit vector lengths. AVX defines UNPCK* to operate
3583 // independently on 128-bit lanes.
3584 unsigned NumLanes = VT.getSizeInBits()/128;
3585 unsigned NumLaneElts = NumElts/NumLanes;
3586
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003587 for (unsigned l = 0; l != NumLanes; ++l) {
Craig Topper94438ba2011-12-16 08:06:31 +00003588 for (unsigned i = l*NumLaneElts, j = (l*NumLaneElts)+NumLaneElts/2;
3589 i != (l+1)*NumLaneElts; i += 2, ++j) {
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003590 int BitI = Mask[i];
3591 int BitI1 = Mask[i+1];
3592 if (!isUndefOrEqual(BitI, j))
Evan Cheng39623da2006-04-20 08:58:49 +00003593 return false;
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003594 if (V2IsSplat) {
3595 if (isUndefOrEqual(BitI1, NumElts))
3596 return false;
3597 } else {
3598 if (!isUndefOrEqual(BitI1, j+NumElts))
3599 return false;
3600 }
Evan Cheng39623da2006-04-20 08:58:49 +00003601 }
Evan Cheng4fcb9222006-03-28 02:43:26 +00003602 }
Evan Cheng4fcb9222006-03-28 02:43:26 +00003603 return true;
3604}
3605
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003606/// isUNPCKL_v_undef_Mask - Special case of isUNPCKLMask for canonical form
3607/// of vector_shuffle v, v, <0, 4, 1, 5>, i.e. vector_shuffle v, undef,
3608/// <0, 0, 1, 1>
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003609static bool isUNPCKL_v_undef_Mask(ArrayRef<int> Mask, EVT VT,
Craig Topper94438ba2011-12-16 08:06:31 +00003610 bool HasAVX2) {
3611 unsigned NumElts = VT.getVectorNumElements();
3612
3613 assert((VT.is128BitVector() || VT.is256BitVector()) &&
3614 "Unsupported vector type for unpckh");
3615
3616 if (VT.getSizeInBits() == 256 && NumElts != 4 && NumElts != 8 &&
3617 (!HasAVX2 || (NumElts != 16 && NumElts != 32)))
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003618 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003619
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00003620 // For 256-bit i64/f64, use MOVDDUPY instead, so reject the matching pattern
3621 // FIXME: Need a better way to get rid of this, there's no latency difference
3622 // between UNPCKLPD and MOVDDUP, the later should always be checked first and
3623 // the former later. We should also remove the "_undef" special mask.
Craig Topper94438ba2011-12-16 08:06:31 +00003624 if (NumElts == 4 && VT.getSizeInBits() == 256)
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00003625 return false;
3626
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003627 // Handle 128 and 256-bit vector lengths. AVX defines UNPCK* to operate
3628 // independently on 128-bit lanes.
Craig Topper94438ba2011-12-16 08:06:31 +00003629 unsigned NumLanes = VT.getSizeInBits()/128;
3630 unsigned NumLaneElts = NumElts/NumLanes;
David Greenea20244d2011-03-02 17:23:43 +00003631
Craig Topper94438ba2011-12-16 08:06:31 +00003632 for (unsigned l = 0; l != NumLanes; ++l) {
3633 for (unsigned i = l*NumLaneElts, j = l*NumLaneElts;
3634 i != (l+1)*NumLaneElts;
David Greenea20244d2011-03-02 17:23:43 +00003635 i += 2, ++j) {
3636 int BitI = Mask[i];
3637 int BitI1 = Mask[i+1];
3638
3639 if (!isUndefOrEqual(BitI, j))
3640 return false;
3641 if (!isUndefOrEqual(BitI1, j))
3642 return false;
3643 }
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003644 }
David Greenea20244d2011-03-02 17:23:43 +00003645
Rafael Espindola15684b22009-04-24 12:40:33 +00003646 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00003647}
3648
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003649/// isUNPCKH_v_undef_Mask - Special case of isUNPCKHMask for canonical form
3650/// of vector_shuffle v, v, <2, 6, 3, 7>, i.e. vector_shuffle v, undef,
3651/// <2, 2, 3, 3>
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003652static bool isUNPCKH_v_undef_Mask(ArrayRef<int> Mask, EVT VT, bool HasAVX2) {
Craig Topper94438ba2011-12-16 08:06:31 +00003653 unsigned NumElts = VT.getVectorNumElements();
3654
3655 assert((VT.is128BitVector() || VT.is256BitVector()) &&
3656 "Unsupported vector type for unpckh");
3657
3658 if (VT.getSizeInBits() == 256 && NumElts != 4 && NumElts != 8 &&
3659 (!HasAVX2 || (NumElts != 16 && NumElts != 32)))
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003660 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003661
Craig Topper94438ba2011-12-16 08:06:31 +00003662 // Handle 128 and 256-bit vector lengths. AVX defines UNPCK* to operate
3663 // independently on 128-bit lanes.
3664 unsigned NumLanes = VT.getSizeInBits()/128;
3665 unsigned NumLaneElts = NumElts/NumLanes;
3666
3667 for (unsigned l = 0; l != NumLanes; ++l) {
3668 for (unsigned i = l*NumLaneElts, j = (l*NumLaneElts)+NumLaneElts/2;
3669 i != (l+1)*NumLaneElts; i += 2, ++j) {
3670 int BitI = Mask[i];
3671 int BitI1 = Mask[i+1];
3672 if (!isUndefOrEqual(BitI, j))
3673 return false;
3674 if (!isUndefOrEqual(BitI1, j))
3675 return false;
3676 }
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003677 }
Rafael Espindola15684b22009-04-24 12:40:33 +00003678 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00003679}
3680
Evan Cheng017dcc62006-04-21 01:05:10 +00003681/// isMOVLMask - Return true if the specified VECTOR_SHUFFLE operand
3682/// specifies a shuffle of elements that is suitable for input to MOVSS,
3683/// MOVSD, and MOVD, i.e. setting the lowest element.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003684static bool isMOVLMask(ArrayRef<int> Mask, EVT VT) {
Eli Friedman10415532009-06-06 06:05:10 +00003685 if (VT.getVectorElementType().getSizeInBits() < 32)
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003686 return false;
Craig Topper7a9a28b2012-08-12 02:23:29 +00003687 if (!VT.is128BitVector())
Elena Demikhovsky021c0a22011-12-28 08:14:01 +00003688 return false;
Eli Friedman10415532009-06-06 06:05:10 +00003689
Craig Topperc612d792012-01-02 09:17:37 +00003690 unsigned NumElts = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00003691
Nate Begeman9008ca62009-04-27 18:41:29 +00003692 if (!isUndefOrEqual(Mask[0], NumElts))
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003693 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003694
Craig Topperc612d792012-01-02 09:17:37 +00003695 for (unsigned i = 1; i != NumElts; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003696 if (!isUndefOrEqual(Mask[i], i))
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003697 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003698
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003699 return true;
3700}
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003701
Craig Topper70b883b2011-11-28 10:14:51 +00003702/// isVPERM2X128Mask - Match 256-bit shuffles where the elements are considered
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003703/// as permutations between 128-bit chunks or halves. As an example: this
3704/// shuffle bellow:
3705/// vector_shuffle <4, 5, 6, 7, 12, 13, 14, 15>
3706/// The first half comes from the second half of V1 and the second half from the
3707/// the second half of V2.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003708static bool isVPERM2X128Mask(ArrayRef<int> Mask, EVT VT, bool HasAVX) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00003709 if (!HasAVX || !VT.is256BitVector())
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003710 return false;
3711
3712 // The shuffle result is divided into half A and half B. In total the two
3713 // sources have 4 halves, namely: C, D, E, F. The final values of A and
3714 // B must come from C, D, E or F.
Craig Topperc612d792012-01-02 09:17:37 +00003715 unsigned HalfSize = VT.getVectorNumElements()/2;
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003716 bool MatchA = false, MatchB = false;
3717
3718 // Check if A comes from one of C, D, E, F.
Craig Topperc612d792012-01-02 09:17:37 +00003719 for (unsigned Half = 0; Half != 4; ++Half) {
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003720 if (isSequentialOrUndefInRange(Mask, 0, HalfSize, Half*HalfSize)) {
3721 MatchA = true;
3722 break;
3723 }
3724 }
3725
3726 // Check if B comes from one of C, D, E, F.
Craig Topperc612d792012-01-02 09:17:37 +00003727 for (unsigned Half = 0; Half != 4; ++Half) {
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003728 if (isSequentialOrUndefInRange(Mask, HalfSize, HalfSize, Half*HalfSize)) {
3729 MatchB = true;
3730 break;
3731 }
3732 }
3733
3734 return MatchA && MatchB;
3735}
3736
Craig Topper70b883b2011-11-28 10:14:51 +00003737/// getShuffleVPERM2X128Immediate - Return the appropriate immediate to shuffle
3738/// the specified VECTOR_MASK mask with VPERM2F128/VPERM2I128 instructions.
Craig Topperd93e4c32011-12-11 19:12:35 +00003739static unsigned getShuffleVPERM2X128Immediate(ShuffleVectorSDNode *SVOp) {
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003740 EVT VT = SVOp->getValueType(0);
3741
Craig Topperc612d792012-01-02 09:17:37 +00003742 unsigned HalfSize = VT.getVectorNumElements()/2;
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003743
Craig Topperc612d792012-01-02 09:17:37 +00003744 unsigned FstHalf = 0, SndHalf = 0;
3745 for (unsigned i = 0; i < HalfSize; ++i) {
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003746 if (SVOp->getMaskElt(i) > 0) {
3747 FstHalf = SVOp->getMaskElt(i)/HalfSize;
3748 break;
3749 }
3750 }
Craig Topperc612d792012-01-02 09:17:37 +00003751 for (unsigned i = HalfSize; i < HalfSize*2; ++i) {
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003752 if (SVOp->getMaskElt(i) > 0) {
3753 SndHalf = SVOp->getMaskElt(i)/HalfSize;
3754 break;
3755 }
3756 }
3757
3758 return (FstHalf | (SndHalf << 4));
3759}
3760
Craig Topper70b883b2011-11-28 10:14:51 +00003761/// isVPERMILPMask - Return true if the specified VECTOR_SHUFFLE operand
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00003762/// specifies a shuffle of elements that is suitable for input to VPERMILPD*.
3763/// Note that VPERMIL mask matching is different depending whether theunderlying
3764/// type is 32 or 64. In the VPERMILPS the high half of the mask should point
3765/// to the same elements of the low, but to the higher half of the source.
3766/// In VPERMILPD the two lanes could be shuffled independently of each other
Craig Topperdbd98a42012-02-07 06:28:42 +00003767/// with the same restriction that lanes can't be crossed. Also handles PSHUFDY.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003768static bool isVPERMILPMask(ArrayRef<int> Mask, EVT VT, bool HasAVX) {
Craig Topper70b883b2011-11-28 10:14:51 +00003769 if (!HasAVX)
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00003770 return false;
3771
Craig Topperc612d792012-01-02 09:17:37 +00003772 unsigned NumElts = VT.getVectorNumElements();
Craig Topper70b883b2011-11-28 10:14:51 +00003773 // Only match 256-bit with 32/64-bit types
3774 if (VT.getSizeInBits() != 256 || (NumElts != 4 && NumElts != 8))
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00003775 return false;
3776
Craig Topperc612d792012-01-02 09:17:37 +00003777 unsigned NumLanes = VT.getSizeInBits()/128;
3778 unsigned LaneSize = NumElts/NumLanes;
Craig Topper1a7700a2012-01-19 08:19:12 +00003779 for (unsigned l = 0; l != NumElts; l += LaneSize) {
Craig Topperc612d792012-01-02 09:17:37 +00003780 for (unsigned i = 0; i != LaneSize; ++i) {
Craig Topper1a7700a2012-01-19 08:19:12 +00003781 if (!isUndefOrInRange(Mask[i+l], l, l+LaneSize))
Craig Topper70b883b2011-11-28 10:14:51 +00003782 return false;
Craig Topper1a7700a2012-01-19 08:19:12 +00003783 if (NumElts != 8 || l == 0)
Craig Topper70b883b2011-11-28 10:14:51 +00003784 continue;
3785 // VPERMILPS handling
3786 if (Mask[i] < 0)
3787 continue;
Craig Topper1a7700a2012-01-19 08:19:12 +00003788 if (!isUndefOrEqual(Mask[i+l], Mask[i]+l))
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00003789 return false;
3790 }
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00003791 }
3792
3793 return true;
3794}
3795
Craig Topper5aaffa82012-02-19 02:53:47 +00003796/// isCommutedMOVLMask - Returns true if the shuffle mask is except the reverse
Evan Cheng017dcc62006-04-21 01:05:10 +00003797/// of what x86 movss want. X86 movs requires the lowest element to be lowest
Evan Cheng39623da2006-04-20 08:58:49 +00003798/// element of vector 2 and the other elements to come from vector 1 in order.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003799static bool isCommutedMOVLMask(ArrayRef<int> Mask, EVT VT,
Nate Begeman9008ca62009-04-27 18:41:29 +00003800 bool V2IsSplat = false, bool V2IsUndef = false) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00003801 if (!VT.is128BitVector())
Craig Topper97327dc2012-03-18 22:50:10 +00003802 return false;
Craig Topper7a9a28b2012-08-12 02:23:29 +00003803
3804 unsigned NumOps = VT.getVectorNumElements();
Chris Lattner5a88b832007-02-25 07:10:00 +00003805 if (NumOps != 2 && NumOps != 4 && NumOps != 8 && NumOps != 16)
Evan Cheng39623da2006-04-20 08:58:49 +00003806 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003807
Nate Begeman9008ca62009-04-27 18:41:29 +00003808 if (!isUndefOrEqual(Mask[0], 0))
Evan Cheng39623da2006-04-20 08:58:49 +00003809 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003810
Craig Topperc612d792012-01-02 09:17:37 +00003811 for (unsigned i = 1; i != NumOps; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003812 if (!(isUndefOrEqual(Mask[i], i+NumOps) ||
3813 (V2IsUndef && isUndefOrInRange(Mask[i], NumOps, NumOps*2)) ||
3814 (V2IsSplat && isUndefOrEqual(Mask[i], NumOps))))
Evan Cheng8cf723d2006-09-08 01:50:06 +00003815 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003816
Evan Cheng39623da2006-04-20 08:58:49 +00003817 return true;
3818}
3819
Evan Chengd9539472006-04-14 21:59:03 +00003820/// isMOVSHDUPMask - Return true if the specified VECTOR_SHUFFLE operand
3821/// specifies a shuffle of elements that is suitable for input to MOVSHDUP.
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003822/// Masks to match: <1, 1, 3, 3> or <1, 1, 3, 3, 5, 5, 7, 7>
Craig Topperdd637ae2012-02-19 05:41:45 +00003823static bool isMOVSHDUPMask(ArrayRef<int> Mask, EVT VT,
Craig Topper5aaffa82012-02-19 02:53:47 +00003824 const X86Subtarget *Subtarget) {
Craig Topperd0a31172012-01-10 06:37:29 +00003825 if (!Subtarget->hasSSE3())
Evan Chengd9539472006-04-14 21:59:03 +00003826 return false;
3827
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003828 unsigned NumElems = VT.getVectorNumElements();
3829
3830 if ((VT.getSizeInBits() == 128 && NumElems != 4) ||
3831 (VT.getSizeInBits() == 256 && NumElems != 8))
3832 return false;
3833
3834 // "i+1" is the value the indexed mask element must have
Craig Topperdd637ae2012-02-19 05:41:45 +00003835 for (unsigned i = 0; i != NumElems; i += 2)
3836 if (!isUndefOrEqual(Mask[i], i+1) ||
3837 !isUndefOrEqual(Mask[i+1], i+1))
Nate Begeman9008ca62009-04-27 18:41:29 +00003838 return false;
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003839
3840 return true;
Evan Chengd9539472006-04-14 21:59:03 +00003841}
3842
3843/// isMOVSLDUPMask - Return true if the specified VECTOR_SHUFFLE operand
3844/// specifies a shuffle of elements that is suitable for input to MOVSLDUP.
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003845/// Masks to match: <0, 0, 2, 2> or <0, 0, 2, 2, 4, 4, 6, 6>
Craig Topperdd637ae2012-02-19 05:41:45 +00003846static bool isMOVSLDUPMask(ArrayRef<int> Mask, EVT VT,
Craig Topper5aaffa82012-02-19 02:53:47 +00003847 const X86Subtarget *Subtarget) {
Craig Topperd0a31172012-01-10 06:37:29 +00003848 if (!Subtarget->hasSSE3())
Evan Chengd9539472006-04-14 21:59:03 +00003849 return false;
3850
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003851 unsigned NumElems = VT.getVectorNumElements();
3852
3853 if ((VT.getSizeInBits() == 128 && NumElems != 4) ||
3854 (VT.getSizeInBits() == 256 && NumElems != 8))
3855 return false;
3856
3857 // "i" is the value the indexed mask element must have
Craig Topperc612d792012-01-02 09:17:37 +00003858 for (unsigned i = 0; i != NumElems; i += 2)
Craig Topperdd637ae2012-02-19 05:41:45 +00003859 if (!isUndefOrEqual(Mask[i], i) ||
3860 !isUndefOrEqual(Mask[i+1], i))
Nate Begeman9008ca62009-04-27 18:41:29 +00003861 return false;
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003862
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003863 return true;
Evan Chengd9539472006-04-14 21:59:03 +00003864}
3865
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00003866/// isMOVDDUPYMask - Return true if the specified VECTOR_SHUFFLE operand
3867/// specifies a shuffle of elements that is suitable for input to 256-bit
3868/// version of MOVDDUP.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003869static bool isMOVDDUPYMask(ArrayRef<int> Mask, EVT VT, bool HasAVX) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00003870 if (!HasAVX || !VT.is256BitVector())
3871 return false;
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00003872
Craig Topper7a9a28b2012-08-12 02:23:29 +00003873 unsigned NumElts = VT.getVectorNumElements();
3874 if (NumElts != 4)
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00003875 return false;
3876
Craig Topperc612d792012-01-02 09:17:37 +00003877 for (unsigned i = 0; i != NumElts/2; ++i)
Craig Topperbeabc6c2011-12-05 06:56:46 +00003878 if (!isUndefOrEqual(Mask[i], 0))
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00003879 return false;
Craig Topperc612d792012-01-02 09:17:37 +00003880 for (unsigned i = NumElts/2; i != NumElts; ++i)
Craig Topperbeabc6c2011-12-05 06:56:46 +00003881 if (!isUndefOrEqual(Mask[i], NumElts/2))
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00003882 return false;
3883 return true;
3884}
3885
Evan Cheng0b457f02008-09-25 20:50:48 +00003886/// isMOVDDUPMask - Return true if the specified VECTOR_SHUFFLE operand
Bruno Cardoso Lopes06ef9232011-08-25 21:40:34 +00003887/// specifies a shuffle of elements that is suitable for input to 128-bit
3888/// version of MOVDDUP.
Craig Topperdd637ae2012-02-19 05:41:45 +00003889static bool isMOVDDUPMask(ArrayRef<int> Mask, EVT VT) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00003890 if (!VT.is128BitVector())
Bruno Cardoso Lopes06ef9232011-08-25 21:40:34 +00003891 return false;
3892
Craig Topperc612d792012-01-02 09:17:37 +00003893 unsigned e = VT.getVectorNumElements() / 2;
3894 for (unsigned i = 0; i != e; ++i)
Craig Topperdd637ae2012-02-19 05:41:45 +00003895 if (!isUndefOrEqual(Mask[i], i))
Evan Cheng0b457f02008-09-25 20:50:48 +00003896 return false;
Craig Topperc612d792012-01-02 09:17:37 +00003897 for (unsigned i = 0; i != e; ++i)
Craig Topperdd637ae2012-02-19 05:41:45 +00003898 if (!isUndefOrEqual(Mask[e+i], i))
Evan Cheng0b457f02008-09-25 20:50:48 +00003899 return false;
3900 return true;
3901}
3902
David Greenec38a03e2011-02-03 15:50:00 +00003903/// isVEXTRACTF128Index - Return true if the specified
3904/// EXTRACT_SUBVECTOR operand specifies a vector extract that is
3905/// suitable for input to VEXTRACTF128.
3906bool X86::isVEXTRACTF128Index(SDNode *N) {
3907 if (!isa<ConstantSDNode>(N->getOperand(1).getNode()))
3908 return false;
3909
3910 // The index should be aligned on a 128-bit boundary.
3911 uint64_t Index =
3912 cast<ConstantSDNode>(N->getOperand(1).getNode())->getZExtValue();
3913
3914 unsigned VL = N->getValueType(0).getVectorNumElements();
3915 unsigned VBits = N->getValueType(0).getSizeInBits();
3916 unsigned ElSize = VBits / VL;
3917 bool Result = (Index * ElSize) % 128 == 0;
3918
3919 return Result;
3920}
3921
David Greeneccacdc12011-02-04 16:08:29 +00003922/// isVINSERTF128Index - Return true if the specified INSERT_SUBVECTOR
3923/// operand specifies a subvector insert that is suitable for input to
3924/// VINSERTF128.
3925bool X86::isVINSERTF128Index(SDNode *N) {
3926 if (!isa<ConstantSDNode>(N->getOperand(2).getNode()))
3927 return false;
3928
3929 // The index should be aligned on a 128-bit boundary.
3930 uint64_t Index =
3931 cast<ConstantSDNode>(N->getOperand(2).getNode())->getZExtValue();
3932
3933 unsigned VL = N->getValueType(0).getVectorNumElements();
3934 unsigned VBits = N->getValueType(0).getSizeInBits();
3935 unsigned ElSize = VBits / VL;
3936 bool Result = (Index * ElSize) % 128 == 0;
3937
3938 return Result;
3939}
3940
Evan Cheng63d33002006-03-22 08:01:21 +00003941/// getShuffleSHUFImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00003942/// the specified VECTOR_SHUFFLE mask with PSHUF* and SHUFP* instructions.
Craig Topper1a7700a2012-01-19 08:19:12 +00003943/// Handles 128-bit and 256-bit.
Craig Topper5aaffa82012-02-19 02:53:47 +00003944static unsigned getShuffleSHUFImmediate(ShuffleVectorSDNode *N) {
Craig Topper1a7700a2012-01-19 08:19:12 +00003945 EVT VT = N->getValueType(0);
Nate Begeman9008ca62009-04-27 18:41:29 +00003946
Craig Topper1a7700a2012-01-19 08:19:12 +00003947 assert((VT.is128BitVector() || VT.is256BitVector()) &&
3948 "Unsupported vector type for PSHUF/SHUFP");
3949
3950 // Handle 128 and 256-bit vector lengths. AVX defines PSHUF/SHUFP to operate
3951 // independently on 128-bit lanes.
3952 unsigned NumElts = VT.getVectorNumElements();
3953 unsigned NumLanes = VT.getSizeInBits()/128;
3954 unsigned NumLaneElts = NumElts/NumLanes;
3955
3956 assert((NumLaneElts == 2 || NumLaneElts == 4) &&
3957 "Only supports 2 or 4 elements per lane");
3958
3959 unsigned Shift = (NumLaneElts == 4) ? 1 : 0;
Evan Chengb9df0ca2006-03-22 02:53:00 +00003960 unsigned Mask = 0;
Craig Topper1a7700a2012-01-19 08:19:12 +00003961 for (unsigned i = 0; i != NumElts; ++i) {
3962 int Elt = N->getMaskElt(i);
3963 if (Elt < 0) continue;
Craig Topper6b28d352012-05-03 07:12:59 +00003964 Elt &= NumLaneElts - 1;
3965 unsigned ShAmt = (i << Shift) % 8;
Craig Topper1a7700a2012-01-19 08:19:12 +00003966 Mask |= Elt << ShAmt;
Evan Cheng36b27f32006-03-28 23:41:33 +00003967 }
Craig Topper1a7700a2012-01-19 08:19:12 +00003968
Evan Cheng63d33002006-03-22 08:01:21 +00003969 return Mask;
3970}
3971
Evan Cheng506d3df2006-03-29 23:07:14 +00003972/// getShufflePSHUFHWImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00003973/// the specified VECTOR_SHUFFLE mask with the PSHUFHW instruction.
Craig Topperdd637ae2012-02-19 05:41:45 +00003974static unsigned getShufflePSHUFHWImmediate(ShuffleVectorSDNode *N) {
Craig Topper6b28d352012-05-03 07:12:59 +00003975 EVT VT = N->getValueType(0);
3976
3977 assert((VT == MVT::v8i16 || VT == MVT::v16i16) &&
3978 "Unsupported vector type for PSHUFHW");
3979
3980 unsigned NumElts = VT.getVectorNumElements();
3981
Evan Cheng506d3df2006-03-29 23:07:14 +00003982 unsigned Mask = 0;
Craig Topper6b28d352012-05-03 07:12:59 +00003983 for (unsigned l = 0; l != NumElts; l += 8) {
3984 // 8 nodes per lane, but we only care about the last 4.
3985 for (unsigned i = 0; i < 4; ++i) {
3986 int Elt = N->getMaskElt(l+i+4);
3987 if (Elt < 0) continue;
3988 Elt &= 0x3; // only 2-bits.
3989 Mask |= Elt << (i * 2);
3990 }
Evan Cheng506d3df2006-03-29 23:07:14 +00003991 }
Craig Topper6b28d352012-05-03 07:12:59 +00003992
Evan Cheng506d3df2006-03-29 23:07:14 +00003993 return Mask;
3994}
3995
3996/// getShufflePSHUFLWImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00003997/// the specified VECTOR_SHUFFLE mask with the PSHUFLW instruction.
Craig Topperdd637ae2012-02-19 05:41:45 +00003998static unsigned getShufflePSHUFLWImmediate(ShuffleVectorSDNode *N) {
Craig Topper6b28d352012-05-03 07:12:59 +00003999 EVT VT = N->getValueType(0);
4000
4001 assert((VT == MVT::v8i16 || VT == MVT::v16i16) &&
4002 "Unsupported vector type for PSHUFHW");
4003
4004 unsigned NumElts = VT.getVectorNumElements();
4005
Evan Cheng506d3df2006-03-29 23:07:14 +00004006 unsigned Mask = 0;
Craig Topper6b28d352012-05-03 07:12:59 +00004007 for (unsigned l = 0; l != NumElts; l += 8) {
4008 // 8 nodes per lane, but we only care about the first 4.
4009 for (unsigned i = 0; i < 4; ++i) {
4010 int Elt = N->getMaskElt(l+i);
4011 if (Elt < 0) continue;
4012 Elt &= 0x3; // only 2-bits
4013 Mask |= Elt << (i * 2);
4014 }
Evan Cheng506d3df2006-03-29 23:07:14 +00004015 }
Craig Topper6b28d352012-05-03 07:12:59 +00004016
Evan Cheng506d3df2006-03-29 23:07:14 +00004017 return Mask;
4018}
4019
Nate Begemana09008b2009-10-19 02:17:23 +00004020/// getShufflePALIGNRImmediate - Return the appropriate immediate to shuffle
4021/// the specified VECTOR_SHUFFLE mask with the PALIGNR instruction.
Craig Topperd93e4c32011-12-11 19:12:35 +00004022static unsigned getShufflePALIGNRImmediate(ShuffleVectorSDNode *SVOp) {
4023 EVT VT = SVOp->getValueType(0);
4024 unsigned EltSize = VT.getVectorElementType().getSizeInBits() >> 3;
Nate Begemana09008b2009-10-19 02:17:23 +00004025
Craig Topper0e2037b2012-01-20 05:53:00 +00004026 unsigned NumElts = VT.getVectorNumElements();
4027 unsigned NumLanes = VT.getSizeInBits()/128;
4028 unsigned NumLaneElts = NumElts/NumLanes;
4029
4030 int Val = 0;
4031 unsigned i;
4032 for (i = 0; i != NumElts; ++i) {
Nate Begemana09008b2009-10-19 02:17:23 +00004033 Val = SVOp->getMaskElt(i);
4034 if (Val >= 0)
4035 break;
4036 }
Craig Topper0e2037b2012-01-20 05:53:00 +00004037 if (Val >= (int)NumElts)
4038 Val -= NumElts - NumLaneElts;
4039
Eli Friedman63f8dde2011-07-25 21:36:45 +00004040 assert(Val - i > 0 && "PALIGNR imm should be positive");
Nate Begemana09008b2009-10-19 02:17:23 +00004041 return (Val - i) * EltSize;
4042}
4043
David Greenec38a03e2011-02-03 15:50:00 +00004044/// getExtractVEXTRACTF128Immediate - Return the appropriate immediate
4045/// to extract the specified EXTRACT_SUBVECTOR index with VEXTRACTF128
4046/// instructions.
4047unsigned X86::getExtractVEXTRACTF128Immediate(SDNode *N) {
4048 if (!isa<ConstantSDNode>(N->getOperand(1).getNode()))
4049 llvm_unreachable("Illegal extract subvector for VEXTRACTF128");
4050
4051 uint64_t Index =
4052 cast<ConstantSDNode>(N->getOperand(1).getNode())->getZExtValue();
4053
4054 EVT VecVT = N->getOperand(0).getValueType();
4055 EVT ElVT = VecVT.getVectorElementType();
4056
4057 unsigned NumElemsPerChunk = 128 / ElVT.getSizeInBits();
David Greenec38a03e2011-02-03 15:50:00 +00004058 return Index / NumElemsPerChunk;
4059}
4060
David Greeneccacdc12011-02-04 16:08:29 +00004061/// getInsertVINSERTF128Immediate - Return the appropriate immediate
4062/// to insert at the specified INSERT_SUBVECTOR index with VINSERTF128
4063/// instructions.
4064unsigned X86::getInsertVINSERTF128Immediate(SDNode *N) {
4065 if (!isa<ConstantSDNode>(N->getOperand(2).getNode()))
4066 llvm_unreachable("Illegal insert subvector for VINSERTF128");
4067
4068 uint64_t Index =
NAKAMURA Takumi27635382011-02-05 15:10:54 +00004069 cast<ConstantSDNode>(N->getOperand(2).getNode())->getZExtValue();
David Greeneccacdc12011-02-04 16:08:29 +00004070
4071 EVT VecVT = N->getValueType(0);
4072 EVT ElVT = VecVT.getVectorElementType();
4073
4074 unsigned NumElemsPerChunk = 128 / ElVT.getSizeInBits();
David Greeneccacdc12011-02-04 16:08:29 +00004075 return Index / NumElemsPerChunk;
4076}
4077
Elena Demikhovsky73c504a2012-04-15 11:18:59 +00004078/// getShuffleCLImmediate - Return the appropriate immediate to shuffle
4079/// the specified VECTOR_SHUFFLE mask with VPERMQ and VPERMPD instructions.
4080/// Handles 256-bit.
4081static unsigned getShuffleCLImmediate(ShuffleVectorSDNode *N) {
4082 EVT VT = N->getValueType(0);
4083
Elena Demikhovsky73c504a2012-04-15 11:18:59 +00004084 unsigned NumElts = VT.getVectorNumElements();
4085
Craig Topper095c5282012-04-15 23:48:57 +00004086 assert((VT.is256BitVector() && NumElts == 4) &&
4087 "Unsupported vector type for VPERMQ/VPERMPD");
4088
Elena Demikhovsky73c504a2012-04-15 11:18:59 +00004089 unsigned Mask = 0;
4090 for (unsigned i = 0; i != NumElts; ++i) {
4091 int Elt = N->getMaskElt(i);
Craig Topper095c5282012-04-15 23:48:57 +00004092 if (Elt < 0)
Elena Demikhovsky73c504a2012-04-15 11:18:59 +00004093 continue;
4094 Mask |= Elt << (i*2);
4095 }
4096
4097 return Mask;
4098}
Evan Cheng37b73872009-07-30 08:33:02 +00004099/// isZeroNode - Returns true if Elt is a constant zero or a floating point
4100/// constant +0.0.
4101bool X86::isZeroNode(SDValue Elt) {
4102 return ((isa<ConstantSDNode>(Elt) &&
Dan Gohmane368b462010-06-18 14:22:04 +00004103 cast<ConstantSDNode>(Elt)->isNullValue()) ||
Evan Cheng37b73872009-07-30 08:33:02 +00004104 (isa<ConstantFPSDNode>(Elt) &&
4105 cast<ConstantFPSDNode>(Elt)->getValueAPF().isPosZero()));
4106}
4107
Nate Begeman9008ca62009-04-27 18:41:29 +00004108/// CommuteVectorShuffle - Swap vector_shuffle operands as well as values in
4109/// their permute mask.
4110static SDValue CommuteVectorShuffle(ShuffleVectorSDNode *SVOp,
4111 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00004112 EVT VT = SVOp->getValueType(0);
Nate Begeman5a5ca152009-04-29 05:20:52 +00004113 unsigned NumElems = VT.getVectorNumElements();
Nate Begeman9008ca62009-04-27 18:41:29 +00004114 SmallVector<int, 8> MaskVec;
Eric Christopherfd179292009-08-27 18:07:15 +00004115
Nate Begeman5a5ca152009-04-29 05:20:52 +00004116 for (unsigned i = 0; i != NumElems; ++i) {
Craig Topper8ae97ba2012-05-21 06:40:16 +00004117 int Idx = SVOp->getMaskElt(i);
4118 if (Idx >= 0) {
4119 if (Idx < (int)NumElems)
4120 Idx += NumElems;
4121 else
4122 Idx -= NumElems;
4123 }
4124 MaskVec.push_back(Idx);
Evan Cheng5ced1d82006-04-06 23:23:56 +00004125 }
Nate Begeman9008ca62009-04-27 18:41:29 +00004126 return DAG.getVectorShuffle(VT, SVOp->getDebugLoc(), SVOp->getOperand(1),
4127 SVOp->getOperand(0), &MaskVec[0]);
Evan Cheng5ced1d82006-04-06 23:23:56 +00004128}
4129
Evan Cheng533a0aa2006-04-19 20:35:22 +00004130/// ShouldXformToMOVHLPS - Return true if the node should be transformed to
4131/// match movhlps. The lower half elements should come from upper half of
4132/// V1 (and in order), and the upper half elements should come from the upper
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00004133/// half of V2 (and in order).
Craig Topperdd637ae2012-02-19 05:41:45 +00004134static bool ShouldXformToMOVHLPS(ArrayRef<int> Mask, EVT VT) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00004135 if (!VT.is128BitVector())
Bruno Cardoso Lopes59353b42011-08-11 18:59:13 +00004136 return false;
4137 if (VT.getVectorNumElements() != 4)
Evan Cheng533a0aa2006-04-19 20:35:22 +00004138 return false;
4139 for (unsigned i = 0, e = 2; i != e; ++i)
Craig Topperdd637ae2012-02-19 05:41:45 +00004140 if (!isUndefOrEqual(Mask[i], i+2))
Evan Cheng533a0aa2006-04-19 20:35:22 +00004141 return false;
4142 for (unsigned i = 2; i != 4; ++i)
Craig Topperdd637ae2012-02-19 05:41:45 +00004143 if (!isUndefOrEqual(Mask[i], i+4))
Evan Cheng533a0aa2006-04-19 20:35:22 +00004144 return false;
4145 return true;
4146}
4147
Evan Cheng5ced1d82006-04-06 23:23:56 +00004148/// isScalarLoadToVector - Returns true if the node is a scalar load that
Evan Cheng7e2ff772008-05-08 00:57:18 +00004149/// is promoted to a vector. It also returns the LoadSDNode by reference if
4150/// required.
4151static bool isScalarLoadToVector(SDNode *N, LoadSDNode **LD = NULL) {
Evan Cheng0b457f02008-09-25 20:50:48 +00004152 if (N->getOpcode() != ISD::SCALAR_TO_VECTOR)
4153 return false;
4154 N = N->getOperand(0).getNode();
4155 if (!ISD::isNON_EXTLoad(N))
4156 return false;
4157 if (LD)
4158 *LD = cast<LoadSDNode>(N);
4159 return true;
Evan Cheng5ced1d82006-04-06 23:23:56 +00004160}
4161
Dan Gohman65fd6562011-11-03 21:49:52 +00004162// Test whether the given value is a vector value which will be legalized
4163// into a load.
4164static bool WillBeConstantPoolLoad(SDNode *N) {
4165 if (N->getOpcode() != ISD::BUILD_VECTOR)
4166 return false;
4167
4168 // Check for any non-constant elements.
4169 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
4170 switch (N->getOperand(i).getNode()->getOpcode()) {
4171 case ISD::UNDEF:
4172 case ISD::ConstantFP:
4173 case ISD::Constant:
4174 break;
4175 default:
4176 return false;
4177 }
4178
4179 // Vectors of all-zeros and all-ones are materialized with special
4180 // instructions rather than being loaded.
4181 return !ISD::isBuildVectorAllZeros(N) &&
4182 !ISD::isBuildVectorAllOnes(N);
4183}
4184
Evan Cheng533a0aa2006-04-19 20:35:22 +00004185/// ShouldXformToMOVLP{S|D} - Return true if the node should be transformed to
4186/// match movlp{s|d}. The lower half elements should come from lower half of
4187/// V1 (and in order), and the upper half elements should come from the upper
4188/// half of V2 (and in order). And since V1 will become the source of the
4189/// MOVLP, it must be either a vector load or a scalar load to vector.
Nate Begeman9008ca62009-04-27 18:41:29 +00004190static bool ShouldXformToMOVLP(SDNode *V1, SDNode *V2,
Craig Topperdd637ae2012-02-19 05:41:45 +00004191 ArrayRef<int> Mask, EVT VT) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00004192 if (!VT.is128BitVector())
Bruno Cardoso Lopes59353b42011-08-11 18:59:13 +00004193 return false;
4194
Evan Cheng466685d2006-10-09 20:57:25 +00004195 if (!ISD::isNON_EXTLoad(V1) && !isScalarLoadToVector(V1))
Evan Cheng533a0aa2006-04-19 20:35:22 +00004196 return false;
Evan Cheng23425f52006-10-09 21:39:25 +00004197 // Is V2 is a vector load, don't do this transformation. We will try to use
4198 // load folding shufps op.
Dan Gohman65fd6562011-11-03 21:49:52 +00004199 if (ISD::isNON_EXTLoad(V2) || WillBeConstantPoolLoad(V2))
Evan Cheng23425f52006-10-09 21:39:25 +00004200 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00004201
Bruno Cardoso Lopes59353b42011-08-11 18:59:13 +00004202 unsigned NumElems = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00004203
Evan Cheng533a0aa2006-04-19 20:35:22 +00004204 if (NumElems != 2 && NumElems != 4)
4205 return false;
Nate Begeman5a5ca152009-04-29 05:20:52 +00004206 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
Craig Topperdd637ae2012-02-19 05:41:45 +00004207 if (!isUndefOrEqual(Mask[i], i))
Evan Cheng533a0aa2006-04-19 20:35:22 +00004208 return false;
Chad Rosier238ae312012-04-30 17:47:15 +00004209 for (unsigned i = NumElems/2, e = NumElems; i != e; ++i)
Craig Topperdd637ae2012-02-19 05:41:45 +00004210 if (!isUndefOrEqual(Mask[i], i+NumElems))
Evan Cheng533a0aa2006-04-19 20:35:22 +00004211 return false;
4212 return true;
Evan Cheng5ced1d82006-04-06 23:23:56 +00004213}
4214
Evan Cheng39623da2006-04-20 08:58:49 +00004215/// isSplatVector - Returns true if N is a BUILD_VECTOR node whose elements are
4216/// all the same.
4217static bool isSplatVector(SDNode *N) {
4218 if (N->getOpcode() != ISD::BUILD_VECTOR)
4219 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00004220
Dan Gohman475871a2008-07-27 21:46:04 +00004221 SDValue SplatValue = N->getOperand(0);
Evan Cheng39623da2006-04-20 08:58:49 +00004222 for (unsigned i = 1, e = N->getNumOperands(); i != e; ++i)
4223 if (N->getOperand(i) != SplatValue)
Evan Cheng5ced1d82006-04-06 23:23:56 +00004224 return false;
4225 return true;
4226}
4227
Evan Cheng213d2cf2007-05-17 18:45:50 +00004228/// isZeroShuffle - Returns true if N is a VECTOR_SHUFFLE that can be resolved
Eric Christopherfd179292009-08-27 18:07:15 +00004229/// to an zero vector.
Nate Begeman5a5ca152009-04-29 05:20:52 +00004230/// FIXME: move to dag combiner / method on ShuffleVectorSDNode
Nate Begeman9008ca62009-04-27 18:41:29 +00004231static bool isZeroShuffle(ShuffleVectorSDNode *N) {
Dan Gohman475871a2008-07-27 21:46:04 +00004232 SDValue V1 = N->getOperand(0);
4233 SDValue V2 = N->getOperand(1);
Nate Begeman5a5ca152009-04-29 05:20:52 +00004234 unsigned NumElems = N->getValueType(0).getVectorNumElements();
4235 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004236 int Idx = N->getMaskElt(i);
Nate Begeman5a5ca152009-04-29 05:20:52 +00004237 if (Idx >= (int)NumElems) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004238 unsigned Opc = V2.getOpcode();
Rafael Espindola15684b22009-04-24 12:40:33 +00004239 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V2.getNode()))
4240 continue;
Evan Cheng37b73872009-07-30 08:33:02 +00004241 if (Opc != ISD::BUILD_VECTOR ||
4242 !X86::isZeroNode(V2.getOperand(Idx-NumElems)))
Nate Begeman9008ca62009-04-27 18:41:29 +00004243 return false;
4244 } else if (Idx >= 0) {
4245 unsigned Opc = V1.getOpcode();
4246 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V1.getNode()))
4247 continue;
Evan Cheng37b73872009-07-30 08:33:02 +00004248 if (Opc != ISD::BUILD_VECTOR ||
4249 !X86::isZeroNode(V1.getOperand(Idx)))
Chris Lattner8a594482007-11-25 00:24:49 +00004250 return false;
Evan Cheng213d2cf2007-05-17 18:45:50 +00004251 }
4252 }
4253 return true;
4254}
4255
4256/// getZeroVector - Returns a vector of specified type with all zero elements.
4257///
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00004258static SDValue getZeroVector(EVT VT, const X86Subtarget *Subtarget,
Craig Topper12216172012-01-13 08:12:35 +00004259 SelectionDAG &DAG, DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004260 assert(VT.isVector() && "Expected a vector type");
Craig Topper9d352402012-04-23 07:24:41 +00004261 unsigned Size = VT.getSizeInBits();
Scott Michelfdc40a02009-02-17 22:15:04 +00004262
Dale Johannesen0488fb62010-09-30 23:57:10 +00004263 // Always build SSE zero vectors as <4 x i32> bitcasted
Bruno Cardoso Lopes8c05a852010-08-12 02:06:36 +00004264 // to their dest type. This ensures they get CSE'd.
Dan Gohman475871a2008-07-27 21:46:04 +00004265 SDValue Vec;
Craig Topper9d352402012-04-23 07:24:41 +00004266 if (Size == 128) { // SSE
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00004267 if (Subtarget->hasSSE2()) { // SSE2
Bruno Cardoso Lopes8c05a852010-08-12 02:06:36 +00004268 SDValue Cst = DAG.getTargetConstant(0, MVT::i32);
4269 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
4270 } else { // SSE1
4271 SDValue Cst = DAG.getTargetConstantFP(+0.0, MVT::f32);
4272 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4f32, Cst, Cst, Cst, Cst);
4273 }
Craig Topper9d352402012-04-23 07:24:41 +00004274 } else if (Size == 256) { // AVX
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00004275 if (Subtarget->hasAVX2()) { // AVX2
Craig Topper12216172012-01-13 08:12:35 +00004276 SDValue Cst = DAG.getTargetConstant(0, MVT::i32);
4277 SDValue Ops[] = { Cst, Cst, Cst, Cst, Cst, Cst, Cst, Cst };
4278 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v8i32, Ops, 8);
4279 } else {
4280 // 256-bit logic and arithmetic instructions in AVX are all
4281 // floating-point, no support for integer ops. Emit fp zeroed vectors.
4282 SDValue Cst = DAG.getTargetConstantFP(+0.0, MVT::f32);
4283 SDValue Ops[] = { Cst, Cst, Cst, Cst, Cst, Cst, Cst, Cst };
4284 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v8f32, Ops, 8);
4285 }
Craig Topper9d352402012-04-23 07:24:41 +00004286 } else
4287 llvm_unreachable("Unexpected vector type");
4288
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004289 return DAG.getNode(ISD::BITCAST, dl, VT, Vec);
Evan Cheng213d2cf2007-05-17 18:45:50 +00004290}
4291
Chris Lattner8a594482007-11-25 00:24:49 +00004292/// getOnesVector - Returns a vector of specified type with all bits set.
Craig Topper745a86b2011-11-19 22:34:59 +00004293/// Always build ones vectors as <4 x i32> or <8 x i32>. For 256-bit types with
4294/// no AVX2 supprt, use two <4 x i32> inserted in a <8 x i32> appropriately.
4295/// Then bitcast to their original type, ensuring they get CSE'd.
4296static SDValue getOnesVector(EVT VT, bool HasAVX2, SelectionDAG &DAG,
4297 DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004298 assert(VT.isVector() && "Expected a vector type");
Craig Topper9d352402012-04-23 07:24:41 +00004299 unsigned Size = VT.getSizeInBits();
Scott Michelfdc40a02009-02-17 22:15:04 +00004300
Owen Anderson825b72b2009-08-11 20:47:22 +00004301 SDValue Cst = DAG.getTargetConstant(~0U, MVT::i32);
Craig Topper745a86b2011-11-19 22:34:59 +00004302 SDValue Vec;
Craig Topper9d352402012-04-23 07:24:41 +00004303 if (Size == 256) {
Craig Topper745a86b2011-11-19 22:34:59 +00004304 if (HasAVX2) { // AVX2
4305 SDValue Ops[] = { Cst, Cst, Cst, Cst, Cst, Cst, Cst, Cst };
4306 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v8i32, Ops, 8);
4307 } else { // AVX
4308 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
Craig Topper4c7972d2012-04-22 18:15:59 +00004309 Vec = Concat128BitVectors(Vec, Vec, MVT::v8i32, 8, DAG, dl);
Craig Topper745a86b2011-11-19 22:34:59 +00004310 }
Craig Topper9d352402012-04-23 07:24:41 +00004311 } else if (Size == 128) {
Craig Topper745a86b2011-11-19 22:34:59 +00004312 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
Craig Topper9d352402012-04-23 07:24:41 +00004313 } else
4314 llvm_unreachable("Unexpected vector type");
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +00004315
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004316 return DAG.getNode(ISD::BITCAST, dl, VT, Vec);
Chris Lattner8a594482007-11-25 00:24:49 +00004317}
4318
Evan Cheng39623da2006-04-20 08:58:49 +00004319/// NormalizeMask - V2 is a splat, modify the mask (if needed) so all elements
4320/// that point to V2 points to its first element.
Craig Topper39a9e482012-02-11 06:24:48 +00004321static void NormalizeMask(SmallVectorImpl<int> &Mask, unsigned NumElems) {
Nate Begeman5a5ca152009-04-29 05:20:52 +00004322 for (unsigned i = 0; i != NumElems; ++i) {
Craig Topper39a9e482012-02-11 06:24:48 +00004323 if (Mask[i] > (int)NumElems) {
4324 Mask[i] = NumElems;
Evan Cheng39623da2006-04-20 08:58:49 +00004325 }
Evan Cheng39623da2006-04-20 08:58:49 +00004326 }
Evan Cheng39623da2006-04-20 08:58:49 +00004327}
4328
Evan Cheng017dcc62006-04-21 01:05:10 +00004329/// getMOVLMask - Returns a vector_shuffle mask for an movs{s|d}, movd
4330/// operation of specified width.
Owen Andersone50ed302009-08-10 22:56:29 +00004331static SDValue getMOVL(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00004332 SDValue V2) {
4333 unsigned NumElems = VT.getVectorNumElements();
4334 SmallVector<int, 8> Mask;
4335 Mask.push_back(NumElems);
Evan Cheng39623da2006-04-20 08:58:49 +00004336 for (unsigned i = 1; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004337 Mask.push_back(i);
4338 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Evan Cheng39623da2006-04-20 08:58:49 +00004339}
4340
Nate Begeman9008ca62009-04-27 18:41:29 +00004341/// getUnpackl - Returns a vector_shuffle node for an unpackl operation.
Owen Andersone50ed302009-08-10 22:56:29 +00004342static SDValue getUnpackl(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00004343 SDValue V2) {
4344 unsigned NumElems = VT.getVectorNumElements();
4345 SmallVector<int, 8> Mask;
Evan Chengc575ca22006-04-17 20:43:08 +00004346 for (unsigned i = 0, e = NumElems/2; i != e; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004347 Mask.push_back(i);
4348 Mask.push_back(i + NumElems);
Evan Chengc575ca22006-04-17 20:43:08 +00004349 }
Nate Begeman9008ca62009-04-27 18:41:29 +00004350 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Evan Chengc575ca22006-04-17 20:43:08 +00004351}
4352
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004353/// getUnpackh - Returns a vector_shuffle node for an unpackh operation.
Owen Andersone50ed302009-08-10 22:56:29 +00004354static SDValue getUnpackh(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00004355 SDValue V2) {
4356 unsigned NumElems = VT.getVectorNumElements();
Nate Begeman9008ca62009-04-27 18:41:29 +00004357 SmallVector<int, 8> Mask;
Chad Rosier238ae312012-04-30 17:47:15 +00004358 for (unsigned i = 0, Half = NumElems/2; i != Half; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004359 Mask.push_back(i + Half);
4360 Mask.push_back(i + NumElems + Half);
Evan Cheng39623da2006-04-20 08:58:49 +00004361 }
Nate Begeman9008ca62009-04-27 18:41:29 +00004362 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Chris Lattner62098042008-03-09 01:05:04 +00004363}
4364
Bruno Cardoso Lopes5f1d8ab2011-08-11 02:49:44 +00004365// PromoteSplati8i16 - All i16 and i8 vector types can't be used directly by
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004366// a generic shuffle instruction because the target has no such instructions.
4367// Generate shuffles which repeat i16 and i8 several times until they can be
4368// represented by v4f32 and then be manipulated by target suported shuffles.
Bruno Cardoso Lopes5f1d8ab2011-08-11 02:49:44 +00004369static SDValue PromoteSplati8i16(SDValue V, SelectionDAG &DAG, int &EltNo) {
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004370 EVT VT = V.getValueType();
Nate Begeman9008ca62009-04-27 18:41:29 +00004371 int NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004372 DebugLoc dl = V.getDebugLoc();
Rafael Espindola15684b22009-04-24 12:40:33 +00004373
Nate Begeman9008ca62009-04-27 18:41:29 +00004374 while (NumElems > 4) {
4375 if (EltNo < NumElems/2) {
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004376 V = getUnpackl(DAG, dl, VT, V, V);
Nate Begeman9008ca62009-04-27 18:41:29 +00004377 } else {
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004378 V = getUnpackh(DAG, dl, VT, V, V);
Nate Begeman9008ca62009-04-27 18:41:29 +00004379 EltNo -= NumElems/2;
4380 }
4381 NumElems >>= 1;
4382 }
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004383 return V;
4384}
Eric Christopherfd179292009-08-27 18:07:15 +00004385
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004386/// getLegalSplat - Generate a legal splat with supported x86 shuffles
4387static SDValue getLegalSplat(SelectionDAG &DAG, SDValue V, int EltNo) {
4388 EVT VT = V.getValueType();
4389 DebugLoc dl = V.getDebugLoc();
Craig Topper9d352402012-04-23 07:24:41 +00004390 unsigned Size = VT.getSizeInBits();
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004391
Craig Topper9d352402012-04-23 07:24:41 +00004392 if (Size == 128) {
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004393 V = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, V);
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004394 int SplatMask[4] = { EltNo, EltNo, EltNo, EltNo };
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004395 V = DAG.getVectorShuffle(MVT::v4f32, dl, V, DAG.getUNDEF(MVT::v4f32),
4396 &SplatMask[0]);
Craig Topper9d352402012-04-23 07:24:41 +00004397 } else if (Size == 256) {
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004398 // To use VPERMILPS to splat scalars, the second half of indicies must
4399 // refer to the higher part, which is a duplication of the lower one,
4400 // because VPERMILPS can only handle in-lane permutations.
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004401 int SplatMask[8] = { EltNo, EltNo, EltNo, EltNo,
4402 EltNo+4, EltNo+4, EltNo+4, EltNo+4 };
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004403
4404 V = DAG.getNode(ISD::BITCAST, dl, MVT::v8f32, V);
4405 V = DAG.getVectorShuffle(MVT::v8f32, dl, V, DAG.getUNDEF(MVT::v8f32),
4406 &SplatMask[0]);
Craig Topper9d352402012-04-23 07:24:41 +00004407 } else
4408 llvm_unreachable("Vector size not supported");
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004409
4410 return DAG.getNode(ISD::BITCAST, dl, VT, V);
4411}
4412
Bruno Cardoso Lopes8a5b2622011-08-17 02:29:13 +00004413/// PromoteSplat - Splat is promoted to target supported vector shuffles.
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004414static SDValue PromoteSplat(ShuffleVectorSDNode *SV, SelectionDAG &DAG) {
4415 EVT SrcVT = SV->getValueType(0);
4416 SDValue V1 = SV->getOperand(0);
4417 DebugLoc dl = SV->getDebugLoc();
4418
4419 int EltNo = SV->getSplatIndex();
4420 int NumElems = SrcVT.getVectorNumElements();
4421 unsigned Size = SrcVT.getSizeInBits();
4422
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004423 assert(((Size == 128 && NumElems > 4) || Size == 256) &&
4424 "Unknown how to promote splat for type");
4425
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004426 // Extract the 128-bit part containing the splat element and update
4427 // the splat element index when it refers to the higher register.
4428 if (Size == 256) {
Craig Topper7d1e3dc2012-04-30 05:17:10 +00004429 V1 = Extract128BitVector(V1, EltNo, DAG, dl);
4430 if (EltNo >= NumElems/2)
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004431 EltNo -= NumElems/2;
4432 }
4433
Bruno Cardoso Lopes8a5b2622011-08-17 02:29:13 +00004434 // All i16 and i8 vector types can't be used directly by a generic shuffle
4435 // instruction because the target has no such instruction. Generate shuffles
4436 // which repeat i16 and i8 several times until they fit in i32, and then can
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004437 // be manipulated by target suported shuffles.
Bruno Cardoso Lopes5f1d8ab2011-08-11 02:49:44 +00004438 EVT EltVT = SrcVT.getVectorElementType();
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004439 if (EltVT == MVT::i8 || EltVT == MVT::i16)
Bruno Cardoso Lopes5f1d8ab2011-08-11 02:49:44 +00004440 V1 = PromoteSplati8i16(V1, DAG, EltNo);
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004441
4442 // Recreate the 256-bit vector and place the same 128-bit vector
4443 // into the low and high part. This is necessary because we want
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004444 // to use VPERM* to shuffle the vectors
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004445 if (Size == 256) {
Craig Topper4c7972d2012-04-22 18:15:59 +00004446 V1 = DAG.getNode(ISD::CONCAT_VECTORS, dl, SrcVT, V1, V1);
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004447 }
4448
4449 return getLegalSplat(DAG, V1, EltNo);
Evan Chengc575ca22006-04-17 20:43:08 +00004450}
4451
Evan Chengba05f722006-04-21 23:03:30 +00004452/// getShuffleVectorZeroOrUndef - Return a vector_shuffle of the specified
Chris Lattner8a594482007-11-25 00:24:49 +00004453/// vector of zero or undef vector. This produces a shuffle where the low
4454/// element of V2 is swizzled into the zero/undef vector, landing at element
4455/// 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 +00004456static SDValue getShuffleVectorZeroOrUndef(SDValue V2, unsigned Idx,
Craig Topper12216172012-01-13 08:12:35 +00004457 bool IsZero,
4458 const X86Subtarget *Subtarget,
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00004459 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00004460 EVT VT = V2.getValueType();
Craig Topper12216172012-01-13 08:12:35 +00004461 SDValue V1 = IsZero
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00004462 ? getZeroVector(VT, Subtarget, DAG, V2.getDebugLoc()) : DAG.getUNDEF(VT);
Nate Begeman9008ca62009-04-27 18:41:29 +00004463 unsigned NumElems = VT.getVectorNumElements();
4464 SmallVector<int, 16> MaskVec;
Chris Lattner8a594482007-11-25 00:24:49 +00004465 for (unsigned i = 0; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004466 // If this is the insertion idx, put the low elt of V2 here.
4467 MaskVec.push_back(i == Idx ? NumElems : i);
4468 return DAG.getVectorShuffle(VT, V2.getDebugLoc(), V1, V2, &MaskVec[0]);
Evan Cheng017dcc62006-04-21 01:05:10 +00004469}
4470
Craig Toppera1ffc682012-03-20 06:42:26 +00004471/// getTargetShuffleMask - Calculates the shuffle mask corresponding to the
4472/// target specific opcode. Returns true if the Mask could be calculated.
Craig Topper89f4e662012-03-20 07:17:59 +00004473/// Sets IsUnary to true if only uses one source.
Craig Topperd978c542012-05-06 19:46:21 +00004474static bool getTargetShuffleMask(SDNode *N, MVT VT,
Craig Topper89f4e662012-03-20 07:17:59 +00004475 SmallVectorImpl<int> &Mask, bool &IsUnary) {
Craig Toppera1ffc682012-03-20 06:42:26 +00004476 unsigned NumElems = VT.getVectorNumElements();
4477 SDValue ImmN;
4478
Craig Topper89f4e662012-03-20 07:17:59 +00004479 IsUnary = false;
Craig Toppera1ffc682012-03-20 06:42:26 +00004480 switch(N->getOpcode()) {
4481 case X86ISD::SHUFP:
4482 ImmN = N->getOperand(N->getNumOperands()-1);
4483 DecodeSHUFPMask(VT, cast<ConstantSDNode>(ImmN)->getZExtValue(), Mask);
4484 break;
4485 case X86ISD::UNPCKH:
4486 DecodeUNPCKHMask(VT, Mask);
4487 break;
4488 case X86ISD::UNPCKL:
4489 DecodeUNPCKLMask(VT, Mask);
4490 break;
4491 case X86ISD::MOVHLPS:
4492 DecodeMOVHLPSMask(NumElems, Mask);
4493 break;
4494 case X86ISD::MOVLHPS:
4495 DecodeMOVLHPSMask(NumElems, Mask);
4496 break;
4497 case X86ISD::PSHUFD:
4498 case X86ISD::VPERMILP:
4499 ImmN = N->getOperand(N->getNumOperands()-1);
4500 DecodePSHUFMask(VT, cast<ConstantSDNode>(ImmN)->getZExtValue(), Mask);
Craig Topper89f4e662012-03-20 07:17:59 +00004501 IsUnary = true;
Craig Toppera1ffc682012-03-20 06:42:26 +00004502 break;
4503 case X86ISD::PSHUFHW:
4504 ImmN = N->getOperand(N->getNumOperands()-1);
Craig Toppera9a568a2012-05-02 08:03:44 +00004505 DecodePSHUFHWMask(VT, cast<ConstantSDNode>(ImmN)->getZExtValue(), Mask);
Craig Topper89f4e662012-03-20 07:17:59 +00004506 IsUnary = true;
Craig Toppera1ffc682012-03-20 06:42:26 +00004507 break;
4508 case X86ISD::PSHUFLW:
4509 ImmN = N->getOperand(N->getNumOperands()-1);
Craig Toppera9a568a2012-05-02 08:03:44 +00004510 DecodePSHUFLWMask(VT, cast<ConstantSDNode>(ImmN)->getZExtValue(), Mask);
Craig Topper89f4e662012-03-20 07:17:59 +00004511 IsUnary = true;
Craig Toppera1ffc682012-03-20 06:42:26 +00004512 break;
Craig Topperbdcbcb32012-05-06 18:54:26 +00004513 case X86ISD::VPERMI:
4514 ImmN = N->getOperand(N->getNumOperands()-1);
4515 DecodeVPERMMask(cast<ConstantSDNode>(ImmN)->getZExtValue(), Mask);
4516 IsUnary = true;
4517 break;
Craig Toppera1ffc682012-03-20 06:42:26 +00004518 case X86ISD::MOVSS:
4519 case X86ISD::MOVSD: {
4520 // The index 0 always comes from the first element of the second source,
4521 // this is why MOVSS and MOVSD are used in the first place. The other
4522 // elements come from the other positions of the first source vector
4523 Mask.push_back(NumElems);
4524 for (unsigned i = 1; i != NumElems; ++i) {
4525 Mask.push_back(i);
4526 }
4527 break;
4528 }
4529 case X86ISD::VPERM2X128:
4530 ImmN = N->getOperand(N->getNumOperands()-1);
4531 DecodeVPERM2X128Mask(VT, cast<ConstantSDNode>(ImmN)->getZExtValue(), Mask);
Craig Topper2091df32012-04-17 05:54:54 +00004532 if (Mask.empty()) return false;
Craig Toppera1ffc682012-03-20 06:42:26 +00004533 break;
4534 case X86ISD::MOVDDUP:
4535 case X86ISD::MOVLHPD:
4536 case X86ISD::MOVLPD:
4537 case X86ISD::MOVLPS:
4538 case X86ISD::MOVSHDUP:
4539 case X86ISD::MOVSLDUP:
4540 case X86ISD::PALIGN:
4541 // Not yet implemented
4542 return false;
4543 default: llvm_unreachable("unknown target shuffle node");
4544 }
4545
4546 return true;
4547}
4548
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004549/// getShuffleScalarElt - Returns the scalar element that will make up the ith
4550/// element of the result of the vector shuffle.
Craig Topper3d092db2012-03-21 02:14:01 +00004551static SDValue getShuffleScalarElt(SDNode *N, unsigned Index, SelectionDAG &DAG,
Benjamin Kramer050db522011-03-26 12:38:19 +00004552 unsigned Depth) {
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004553 if (Depth == 6)
4554 return SDValue(); // Limit search depth.
4555
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004556 SDValue V = SDValue(N, 0);
4557 EVT VT = V.getValueType();
4558 unsigned Opcode = V.getOpcode();
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004559
4560 // Recurse into ISD::VECTOR_SHUFFLE node to find scalars.
4561 if (const ShuffleVectorSDNode *SV = dyn_cast<ShuffleVectorSDNode>(N)) {
Craig Topper3d092db2012-03-21 02:14:01 +00004562 int Elt = SV->getMaskElt(Index);
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004563
Craig Topper3d092db2012-03-21 02:14:01 +00004564 if (Elt < 0)
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004565 return DAG.getUNDEF(VT.getVectorElementType());
4566
Craig Topperd156dc12012-02-06 07:17:51 +00004567 unsigned NumElems = VT.getVectorNumElements();
Craig Topper3d092db2012-03-21 02:14:01 +00004568 SDValue NewV = (Elt < (int)NumElems) ? SV->getOperand(0)
4569 : SV->getOperand(1);
4570 return getShuffleScalarElt(NewV.getNode(), Elt % NumElems, DAG, Depth+1);
Evan Chengf26ffe92008-05-29 08:22:04 +00004571 }
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004572
4573 // Recurse into target specific vector shuffles to find scalars.
4574 if (isTargetShuffle(Opcode)) {
Craig Topperd978c542012-05-06 19:46:21 +00004575 MVT ShufVT = V.getValueType().getSimpleVT();
4576 unsigned NumElems = ShufVT.getVectorNumElements();
Craig Toppera1ffc682012-03-20 06:42:26 +00004577 SmallVector<int, 16> ShuffleMask;
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004578 SDValue ImmN;
Craig Topper89f4e662012-03-20 07:17:59 +00004579 bool IsUnary;
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004580
Craig Topperd978c542012-05-06 19:46:21 +00004581 if (!getTargetShuffleMask(N, ShufVT, ShuffleMask, IsUnary))
Craig Toppera1ffc682012-03-20 06:42:26 +00004582 return SDValue();
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004583
Craig Topper3d092db2012-03-21 02:14:01 +00004584 int Elt = ShuffleMask[Index];
4585 if (Elt < 0)
Craig Topperd978c542012-05-06 19:46:21 +00004586 return DAG.getUNDEF(ShufVT.getVectorElementType());
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004587
Craig Topper3d092db2012-03-21 02:14:01 +00004588 SDValue NewV = (Elt < (int)NumElems) ? N->getOperand(0)
Craig Topperd978c542012-05-06 19:46:21 +00004589 : N->getOperand(1);
Craig Topper3d092db2012-03-21 02:14:01 +00004590 return getShuffleScalarElt(NewV.getNode(), Elt % NumElems, DAG,
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004591 Depth+1);
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004592 }
4593
4594 // Actual nodes that may contain scalar elements
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004595 if (Opcode == ISD::BITCAST) {
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004596 V = V.getOperand(0);
4597 EVT SrcVT = V.getValueType();
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00004598 unsigned NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004599
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00004600 if (!SrcVT.isVector() || SrcVT.getVectorNumElements() != NumElems)
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004601 return SDValue();
4602 }
4603
4604 if (V.getOpcode() == ISD::SCALAR_TO_VECTOR)
4605 return (Index == 0) ? V.getOperand(0)
Craig Topper3d092db2012-03-21 02:14:01 +00004606 : DAG.getUNDEF(VT.getVectorElementType());
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004607
4608 if (V.getOpcode() == ISD::BUILD_VECTOR)
4609 return V.getOperand(Index);
4610
4611 return SDValue();
4612}
4613
4614/// getNumOfConsecutiveZeros - Return the number of elements of a vector
4615/// shuffle operation which come from a consecutively from a zero. The
Chris Lattner7a2bdde2011-04-15 05:18:47 +00004616/// search can start in two different directions, from left or right.
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004617static
Craig Topper3d092db2012-03-21 02:14:01 +00004618unsigned getNumOfConsecutiveZeros(ShuffleVectorSDNode *SVOp, unsigned NumElems,
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004619 bool ZerosFromLeft, SelectionDAG &DAG) {
Craig Topper3d092db2012-03-21 02:14:01 +00004620 unsigned i;
4621 for (i = 0; i != NumElems; ++i) {
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004622 unsigned Index = ZerosFromLeft ? i : NumElems-i-1;
Craig Topper3d092db2012-03-21 02:14:01 +00004623 SDValue Elt = getShuffleScalarElt(SVOp, Index, DAG, 0);
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004624 if (!(Elt.getNode() &&
4625 (Elt.getOpcode() == ISD::UNDEF || X86::isZeroNode(Elt))))
4626 break;
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004627 }
4628
4629 return i;
4630}
4631
Craig Topper3d092db2012-03-21 02:14:01 +00004632/// isShuffleMaskConsecutive - Check if the shuffle mask indicies [MaskI, MaskE)
4633/// correspond consecutively to elements from one of the vector operands,
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004634/// starting from its index OpIdx. Also tell OpNum which source vector operand.
4635static
Craig Topper3d092db2012-03-21 02:14:01 +00004636bool isShuffleMaskConsecutive(ShuffleVectorSDNode *SVOp,
4637 unsigned MaskI, unsigned MaskE, unsigned OpIdx,
4638 unsigned NumElems, unsigned &OpNum) {
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004639 bool SeenV1 = false;
4640 bool SeenV2 = false;
4641
Craig Topper3d092db2012-03-21 02:14:01 +00004642 for (unsigned i = MaskI; i != MaskE; ++i, ++OpIdx) {
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004643 int Idx = SVOp->getMaskElt(i);
4644 // Ignore undef indicies
4645 if (Idx < 0)
4646 continue;
4647
Craig Topper3d092db2012-03-21 02:14:01 +00004648 if (Idx < (int)NumElems)
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004649 SeenV1 = true;
4650 else
4651 SeenV2 = true;
4652
4653 // Only accept consecutive elements from the same vector
4654 if ((Idx % NumElems != OpIdx) || (SeenV1 && SeenV2))
4655 return false;
4656 }
4657
4658 OpNum = SeenV1 ? 0 : 1;
4659 return true;
4660}
4661
4662/// isVectorShiftRight - Returns true if the shuffle can be implemented as a
4663/// logical left shift of a vector.
4664static bool isVectorShiftRight(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
4665 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
4666 unsigned NumElems = SVOp->getValueType(0).getVectorNumElements();
4667 unsigned NumZeros = getNumOfConsecutiveZeros(SVOp, NumElems,
4668 false /* check zeros from right */, DAG);
4669 unsigned OpSrc;
4670
4671 if (!NumZeros)
4672 return false;
4673
4674 // Considering the elements in the mask that are not consecutive zeros,
4675 // check if they consecutively come from only one of the source vectors.
4676 //
4677 // V1 = {X, A, B, C} 0
4678 // \ \ \ /
4679 // vector_shuffle V1, V2 <1, 2, 3, X>
4680 //
4681 if (!isShuffleMaskConsecutive(SVOp,
4682 0, // Mask Start Index
Craig Topper3d092db2012-03-21 02:14:01 +00004683 NumElems-NumZeros, // Mask End Index(exclusive)
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004684 NumZeros, // Where to start looking in the src vector
4685 NumElems, // Number of elements in vector
4686 OpSrc)) // Which source operand ?
4687 return false;
4688
4689 isLeft = false;
4690 ShAmt = NumZeros;
4691 ShVal = SVOp->getOperand(OpSrc);
4692 return true;
4693}
4694
4695/// isVectorShiftLeft - Returns true if the shuffle can be implemented as a
4696/// logical left shift of a vector.
4697static bool isVectorShiftLeft(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
4698 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
4699 unsigned NumElems = SVOp->getValueType(0).getVectorNumElements();
4700 unsigned NumZeros = getNumOfConsecutiveZeros(SVOp, NumElems,
4701 true /* check zeros from left */, DAG);
4702 unsigned OpSrc;
4703
4704 if (!NumZeros)
4705 return false;
4706
4707 // Considering the elements in the mask that are not consecutive zeros,
4708 // check if they consecutively come from only one of the source vectors.
4709 //
4710 // 0 { A, B, X, X } = V2
4711 // / \ / /
4712 // vector_shuffle V1, V2 <X, X, 4, 5>
4713 //
4714 if (!isShuffleMaskConsecutive(SVOp,
4715 NumZeros, // Mask Start Index
Craig Topper3d092db2012-03-21 02:14:01 +00004716 NumElems, // Mask End Index(exclusive)
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004717 0, // Where to start looking in the src vector
4718 NumElems, // Number of elements in vector
4719 OpSrc)) // Which source operand ?
4720 return false;
4721
4722 isLeft = true;
4723 ShAmt = NumZeros;
4724 ShVal = SVOp->getOperand(OpSrc);
4725 return true;
Evan Chengf26ffe92008-05-29 08:22:04 +00004726}
4727
4728/// isVectorShift - Returns true if the shuffle can be implemented as a
4729/// logical left or right shift of a vector.
Nate Begeman9008ca62009-04-27 18:41:29 +00004730static bool isVectorShift(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
Dan Gohman475871a2008-07-27 21:46:04 +00004731 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00004732 // Although the logic below support any bitwidth size, there are no
4733 // shift instructions which handle more than 128-bit vectors.
Craig Topper7a9a28b2012-08-12 02:23:29 +00004734 if (!SVOp->getValueType(0).is128BitVector())
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00004735 return false;
4736
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004737 if (isVectorShiftLeft(SVOp, DAG, isLeft, ShVal, ShAmt) ||
4738 isVectorShiftRight(SVOp, DAG, isLeft, ShVal, ShAmt))
4739 return true;
Evan Chengf26ffe92008-05-29 08:22:04 +00004740
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004741 return false;
Evan Chengf26ffe92008-05-29 08:22:04 +00004742}
4743
Evan Chengc78d3b42006-04-24 18:01:45 +00004744/// LowerBuildVectorv16i8 - Custom lower build_vector of v16i8.
4745///
Dan Gohman475871a2008-07-27 21:46:04 +00004746static SDValue LowerBuildVectorv16i8(SDValue Op, unsigned NonZeros,
Evan Chengc78d3b42006-04-24 18:01:45 +00004747 unsigned NumNonZero, unsigned NumZero,
Dan Gohmand858e902010-04-17 15:26:15 +00004748 SelectionDAG &DAG,
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00004749 const X86Subtarget* Subtarget,
Dan Gohmand858e902010-04-17 15:26:15 +00004750 const TargetLowering &TLI) {
Evan Chengc78d3b42006-04-24 18:01:45 +00004751 if (NumNonZero > 8)
Dan Gohman475871a2008-07-27 21:46:04 +00004752 return SDValue();
Evan Chengc78d3b42006-04-24 18:01:45 +00004753
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004754 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00004755 SDValue V(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00004756 bool First = true;
4757 for (unsigned i = 0; i < 16; ++i) {
4758 bool ThisIsNonZero = (NonZeros & (1 << i)) != 0;
4759 if (ThisIsNonZero && First) {
4760 if (NumZero)
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00004761 V = getZeroVector(MVT::v8i16, Subtarget, DAG, dl);
Evan Chengc78d3b42006-04-24 18:01:45 +00004762 else
Owen Anderson825b72b2009-08-11 20:47:22 +00004763 V = DAG.getUNDEF(MVT::v8i16);
Evan Chengc78d3b42006-04-24 18:01:45 +00004764 First = false;
4765 }
4766
4767 if ((i & 1) != 0) {
Dan Gohman475871a2008-07-27 21:46:04 +00004768 SDValue ThisElt(0, 0), LastElt(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00004769 bool LastIsNonZero = (NonZeros & (1 << (i-1))) != 0;
4770 if (LastIsNonZero) {
Scott Michelfdc40a02009-02-17 22:15:04 +00004771 LastElt = DAG.getNode(ISD::ZERO_EXTEND, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004772 MVT::i16, Op.getOperand(i-1));
Evan Chengc78d3b42006-04-24 18:01:45 +00004773 }
4774 if (ThisIsNonZero) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004775 ThisElt = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i16, Op.getOperand(i));
4776 ThisElt = DAG.getNode(ISD::SHL, dl, MVT::i16,
4777 ThisElt, DAG.getConstant(8, MVT::i8));
Evan Chengc78d3b42006-04-24 18:01:45 +00004778 if (LastIsNonZero)
Owen Anderson825b72b2009-08-11 20:47:22 +00004779 ThisElt = DAG.getNode(ISD::OR, dl, MVT::i16, ThisElt, LastElt);
Evan Chengc78d3b42006-04-24 18:01:45 +00004780 } else
4781 ThisElt = LastElt;
4782
Gabor Greifba36cb52008-08-28 21:40:38 +00004783 if (ThisElt.getNode())
Owen Anderson825b72b2009-08-11 20:47:22 +00004784 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, V, ThisElt,
Chris Lattner0bd48932008-01-17 07:00:52 +00004785 DAG.getIntPtrConstant(i/2));
Evan Chengc78d3b42006-04-24 18:01:45 +00004786 }
4787 }
4788
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004789 return DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, V);
Evan Chengc78d3b42006-04-24 18:01:45 +00004790}
4791
Bill Wendlinga348c562007-03-22 18:42:45 +00004792/// LowerBuildVectorv8i16 - Custom lower build_vector of v8i16.
Evan Chengc78d3b42006-04-24 18:01:45 +00004793///
Dan Gohman475871a2008-07-27 21:46:04 +00004794static SDValue LowerBuildVectorv8i16(SDValue Op, unsigned NonZeros,
Dan Gohmand858e902010-04-17 15:26:15 +00004795 unsigned NumNonZero, unsigned NumZero,
4796 SelectionDAG &DAG,
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00004797 const X86Subtarget* Subtarget,
Dan Gohmand858e902010-04-17 15:26:15 +00004798 const TargetLowering &TLI) {
Evan Chengc78d3b42006-04-24 18:01:45 +00004799 if (NumNonZero > 4)
Dan Gohman475871a2008-07-27 21:46:04 +00004800 return SDValue();
Evan Chengc78d3b42006-04-24 18:01:45 +00004801
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004802 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00004803 SDValue V(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00004804 bool First = true;
4805 for (unsigned i = 0; i < 8; ++i) {
4806 bool isNonZero = (NonZeros & (1 << i)) != 0;
4807 if (isNonZero) {
4808 if (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 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004815 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004816 MVT::v8i16, V, Op.getOperand(i),
Chris Lattner0bd48932008-01-17 07:00:52 +00004817 DAG.getIntPtrConstant(i));
Evan Chengc78d3b42006-04-24 18:01:45 +00004818 }
4819 }
4820
4821 return V;
4822}
4823
Evan Chengf26ffe92008-05-29 08:22:04 +00004824/// getVShift - Return a vector logical shift node.
4825///
Owen Andersone50ed302009-08-10 22:56:29 +00004826static SDValue getVShift(bool isLeft, EVT VT, SDValue SrcOp,
Nate Begeman9008ca62009-04-27 18:41:29 +00004827 unsigned NumBits, SelectionDAG &DAG,
4828 const TargetLowering &TLI, DebugLoc dl) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00004829 assert(VT.is128BitVector() && "Unknown type for VShift");
Dale Johannesen0488fb62010-09-30 23:57:10 +00004830 EVT ShVT = MVT::v2i64;
Craig Toppered2e13d2012-01-22 19:15:14 +00004831 unsigned Opc = isLeft ? X86ISD::VSHLDQ : X86ISD::VSRLDQ;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004832 SrcOp = DAG.getNode(ISD::BITCAST, dl, ShVT, SrcOp);
4833 return DAG.getNode(ISD::BITCAST, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00004834 DAG.getNode(Opc, dl, ShVT, SrcOp,
Owen Anderson95771af2011-02-25 21:41:48 +00004835 DAG.getConstant(NumBits,
4836 TLI.getShiftAmountTy(SrcOp.getValueType()))));
Evan Chengf26ffe92008-05-29 08:22:04 +00004837}
4838
Dan Gohman475871a2008-07-27 21:46:04 +00004839SDValue
Evan Chengc3630942009-12-09 21:00:30 +00004840X86TargetLowering::LowerAsSplatVectorLoad(SDValue SrcOp, EVT VT, DebugLoc dl,
Dan Gohmand858e902010-04-17 15:26:15 +00004841 SelectionDAG &DAG) const {
Michael J. Spencerec38de22010-10-10 22:04:20 +00004842
Evan Chengc3630942009-12-09 21:00:30 +00004843 // Check if the scalar load can be widened into a vector load. And if
4844 // the address is "base + cst" see if the cst can be "absorbed" into
4845 // the shuffle mask.
4846 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(SrcOp)) {
4847 SDValue Ptr = LD->getBasePtr();
4848 if (!ISD::isNormalLoad(LD) || LD->isVolatile())
4849 return SDValue();
4850 EVT PVT = LD->getValueType(0);
4851 if (PVT != MVT::i32 && PVT != MVT::f32)
4852 return SDValue();
4853
4854 int FI = -1;
4855 int64_t Offset = 0;
4856 if (FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr)) {
4857 FI = FINode->getIndex();
4858 Offset = 0;
Chris Lattner0a9481f2011-02-13 22:25:43 +00004859 } else if (DAG.isBaseWithConstantOffset(Ptr) &&
Evan Chengc3630942009-12-09 21:00:30 +00004860 isa<FrameIndexSDNode>(Ptr.getOperand(0))) {
4861 FI = cast<FrameIndexSDNode>(Ptr.getOperand(0))->getIndex();
4862 Offset = Ptr.getConstantOperandVal(1);
4863 Ptr = Ptr.getOperand(0);
4864 } else {
4865 return SDValue();
4866 }
4867
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004868 // FIXME: 256-bit vector instructions don't require a strict alignment,
4869 // improve this code to support it better.
4870 unsigned RequiredAlign = VT.getSizeInBits()/8;
Evan Chengc3630942009-12-09 21:00:30 +00004871 SDValue Chain = LD->getChain();
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004872 // Make sure the stack object alignment is at least 16 or 32.
Evan Chengc3630942009-12-09 21:00:30 +00004873 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004874 if (DAG.InferPtrAlignment(Ptr) < RequiredAlign) {
Evan Chengc3630942009-12-09 21:00:30 +00004875 if (MFI->isFixedObjectIndex(FI)) {
Eric Christophere9625cf2010-01-23 06:02:43 +00004876 // Can't change the alignment. FIXME: It's possible to compute
4877 // the exact stack offset and reference FI + adjust offset instead.
4878 // If someone *really* cares about this. That's the way to implement it.
4879 return SDValue();
Evan Chengc3630942009-12-09 21:00:30 +00004880 } else {
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004881 MFI->setObjectAlignment(FI, RequiredAlign);
Evan Chengc3630942009-12-09 21:00:30 +00004882 }
4883 }
4884
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004885 // (Offset % 16 or 32) must be multiple of 4. Then address is then
Evan Chengc3630942009-12-09 21:00:30 +00004886 // Ptr + (Offset & ~15).
4887 if (Offset < 0)
4888 return SDValue();
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004889 if ((Offset % RequiredAlign) & 3)
Evan Chengc3630942009-12-09 21:00:30 +00004890 return SDValue();
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004891 int64_t StartOffset = Offset & ~(RequiredAlign-1);
Evan Chengc3630942009-12-09 21:00:30 +00004892 if (StartOffset)
4893 Ptr = DAG.getNode(ISD::ADD, Ptr.getDebugLoc(), Ptr.getValueType(),
4894 Ptr,DAG.getConstant(StartOffset, Ptr.getValueType()));
4895
4896 int EltNo = (Offset - StartOffset) >> 2;
Craig Topper66ddd152012-04-27 22:54:43 +00004897 unsigned NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004898
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004899 EVT NVT = EVT::getVectorVT(*DAG.getContext(), PVT, NumElems);
4900 SDValue V1 = DAG.getLoad(NVT, dl, Chain, Ptr,
Chris Lattner51abfe42010-09-21 06:02:19 +00004901 LD->getPointerInfo().getWithOffset(StartOffset),
Pete Cooperd752e0f2011-11-08 18:42:53 +00004902 false, false, false, 0);
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004903
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004904 SmallVector<int, 8> Mask;
Craig Topper66ddd152012-04-27 22:54:43 +00004905 for (unsigned i = 0; i != NumElems; ++i)
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004906 Mask.push_back(EltNo);
4907
Craig Toppercc3000632012-01-30 07:50:31 +00004908 return DAG.getVectorShuffle(NVT, dl, V1, DAG.getUNDEF(NVT), &Mask[0]);
Evan Chengc3630942009-12-09 21:00:30 +00004909 }
4910
4911 return SDValue();
4912}
4913
Michael J. Spencerec38de22010-10-10 22:04:20 +00004914/// EltsFromConsecutiveLoads - Given the initializing elements 'Elts' of a
4915/// vector of type 'VT', see if the elements can be replaced by a single large
Nate Begeman1449f292010-03-24 22:19:06 +00004916/// load which has the same value as a build_vector whose operands are 'elts'.
4917///
4918/// Example: <load i32 *a, load i32 *a+4, undef, undef> -> zextload a
Michael J. Spencerec38de22010-10-10 22:04:20 +00004919///
Nate Begeman1449f292010-03-24 22:19:06 +00004920/// FIXME: we'd also like to handle the case where the last elements are zero
4921/// rather than undef via VZEXT_LOAD, but we do not detect that case today.
4922/// There's even a handy isZeroNode for that purpose.
Nate Begemanfdea31a2010-03-24 20:49:50 +00004923static SDValue EltsFromConsecutiveLoads(EVT VT, SmallVectorImpl<SDValue> &Elts,
Chris Lattner88641552010-09-22 00:34:38 +00004924 DebugLoc &DL, SelectionDAG &DAG) {
Nate Begemanfdea31a2010-03-24 20:49:50 +00004925 EVT EltVT = VT.getVectorElementType();
4926 unsigned NumElems = Elts.size();
Michael J. Spencerec38de22010-10-10 22:04:20 +00004927
Nate Begemanfdea31a2010-03-24 20:49:50 +00004928 LoadSDNode *LDBase = NULL;
4929 unsigned LastLoadedElt = -1U;
Michael J. Spencerec38de22010-10-10 22:04:20 +00004930
Nate Begeman1449f292010-03-24 22:19:06 +00004931 // For each element in the initializer, see if we've found a load or an undef.
Michael J. Spencerec38de22010-10-10 22:04:20 +00004932 // If we don't find an initial load element, or later load elements are
Nate Begeman1449f292010-03-24 22:19:06 +00004933 // non-consecutive, bail out.
Nate Begemanfdea31a2010-03-24 20:49:50 +00004934 for (unsigned i = 0; i < NumElems; ++i) {
4935 SDValue Elt = Elts[i];
Michael J. Spencerec38de22010-10-10 22:04:20 +00004936
Nate Begemanfdea31a2010-03-24 20:49:50 +00004937 if (!Elt.getNode() ||
4938 (Elt.getOpcode() != ISD::UNDEF && !ISD::isNON_EXTLoad(Elt.getNode())))
4939 return SDValue();
4940 if (!LDBase) {
4941 if (Elt.getNode()->getOpcode() == ISD::UNDEF)
4942 return SDValue();
4943 LDBase = cast<LoadSDNode>(Elt.getNode());
4944 LastLoadedElt = i;
4945 continue;
4946 }
4947 if (Elt.getOpcode() == ISD::UNDEF)
4948 continue;
4949
4950 LoadSDNode *LD = cast<LoadSDNode>(Elt);
4951 if (!DAG.isConsecutiveLoad(LD, LDBase, EltVT.getSizeInBits()/8, i))
4952 return SDValue();
4953 LastLoadedElt = i;
4954 }
Nate Begeman1449f292010-03-24 22:19:06 +00004955
4956 // If we have found an entire vector of loads and undefs, then return a large
4957 // load of the entire vector width starting at the base pointer. If we found
4958 // consecutive loads for the low half, generate a vzext_load node.
Nate Begemanfdea31a2010-03-24 20:49:50 +00004959 if (LastLoadedElt == NumElems - 1) {
4960 if (DAG.InferPtrAlignment(LDBase->getBasePtr()) >= 16)
Chris Lattner88641552010-09-22 00:34:38 +00004961 return DAG.getLoad(VT, DL, LDBase->getChain(), LDBase->getBasePtr(),
Chris Lattner51abfe42010-09-21 06:02:19 +00004962 LDBase->getPointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00004963 LDBase->isVolatile(), LDBase->isNonTemporal(),
4964 LDBase->isInvariant(), 0);
Chris Lattner88641552010-09-22 00:34:38 +00004965 return DAG.getLoad(VT, DL, LDBase->getChain(), LDBase->getBasePtr(),
Chris Lattner51abfe42010-09-21 06:02:19 +00004966 LDBase->getPointerInfo(),
Nate Begemanfdea31a2010-03-24 20:49:50 +00004967 LDBase->isVolatile(), LDBase->isNonTemporal(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00004968 LDBase->isInvariant(), LDBase->getAlignment());
Craig Topper69947b92012-04-23 06:57:04 +00004969 }
4970 if (NumElems == 4 && LastLoadedElt == 1 &&
4971 DAG.getTargetLoweringInfo().isTypeLegal(MVT::v2i64)) {
Nate Begemanfdea31a2010-03-24 20:49:50 +00004972 SDVTList Tys = DAG.getVTList(MVT::v2i64, MVT::Other);
4973 SDValue Ops[] = { LDBase->getChain(), LDBase->getBasePtr() };
Eli Friedman322ea082011-09-14 23:42:45 +00004974 SDValue ResNode =
4975 DAG.getMemIntrinsicNode(X86ISD::VZEXT_LOAD, DL, Tys, Ops, 2, MVT::i64,
4976 LDBase->getPointerInfo(),
4977 LDBase->getAlignment(),
4978 false/*isVolatile*/, true/*ReadMem*/,
4979 false/*WriteMem*/);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004980 return DAG.getNode(ISD::BITCAST, DL, VT, ResNode);
Nate Begemanfdea31a2010-03-24 20:49:50 +00004981 }
4982 return SDValue();
4983}
4984
Nadav Rotem9d68b062012-04-08 12:54:54 +00004985/// LowerVectorBroadcast - Attempt to use the vbroadcast instruction
4986/// to generate a splat value for the following cases:
4987/// 1. A splat BUILD_VECTOR which uses a single scalar load, or a constant.
Nadav Rotemf8c10e52011-11-15 22:50:37 +00004988/// 2. A splat shuffle which uses a scalar_to_vector node which comes from
Nadav Rotem9d68b062012-04-08 12:54:54 +00004989/// a scalar load, or a constant.
4990/// The VBROADCAST node is returned when a pattern is found,
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00004991/// or SDValue() otherwise.
Nadav Rotem154819d2012-04-09 07:45:58 +00004992SDValue
4993X86TargetLowering::LowerVectorBroadcast(SDValue &Op, SelectionDAG &DAG) const {
Craig Toppera9376332012-01-10 08:23:59 +00004994 if (!Subtarget->hasAVX())
4995 return SDValue();
4996
Nadav Rotemf8c10e52011-11-15 22:50:37 +00004997 EVT VT = Op.getValueType();
Nadav Rotem154819d2012-04-09 07:45:58 +00004998 DebugLoc dl = Op.getDebugLoc();
Nadav Rotemf8c10e52011-11-15 22:50:37 +00004999
Craig Topper5da8a802012-05-04 05:49:51 +00005000 assert((VT.is128BitVector() || VT.is256BitVector()) &&
5001 "Unsupported vector type for broadcast.");
5002
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005003 SDValue Ld;
Nadav Rotem9d68b062012-04-08 12:54:54 +00005004 bool ConstSplatVal;
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005005
Nadav Rotem9d68b062012-04-08 12:54:54 +00005006 switch (Op.getOpcode()) {
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005007 default:
5008 // Unknown pattern found.
5009 return SDValue();
5010
5011 case ISD::BUILD_VECTOR: {
5012 // The BUILD_VECTOR node must be a splat.
Nadav Rotem9d68b062012-04-08 12:54:54 +00005013 if (!isSplatVector(Op.getNode()))
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005014 return SDValue();
5015
Nadav Rotem9d68b062012-04-08 12:54:54 +00005016 Ld = Op.getOperand(0);
5017 ConstSplatVal = (Ld.getOpcode() == ISD::Constant ||
5018 Ld.getOpcode() == ISD::ConstantFP);
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00005019
5020 // The suspected load node has several users. Make sure that all
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005021 // of its users are from the BUILD_VECTOR node.
Nadav Rotem9d68b062012-04-08 12:54:54 +00005022 // Constants may have multiple users.
5023 if (!ConstSplatVal && !Ld->hasNUsesOfValue(VT.getVectorNumElements(), 0))
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005024 return SDValue();
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00005025 break;
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005026 }
5027
5028 case ISD::VECTOR_SHUFFLE: {
5029 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
5030
5031 // Shuffles must have a splat mask where the first element is
5032 // broadcasted.
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00005033 if ((!SVOp->isSplat()) || SVOp->getMaskElt(0) != 0)
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005034 return SDValue();
5035
5036 SDValue Sc = Op.getOperand(0);
Nadav Rotemb88e8dd2012-05-10 12:50:02 +00005037 if (Sc.getOpcode() != ISD::SCALAR_TO_VECTOR &&
Elena Demikhovsky8f40f7b2012-07-01 06:12:26 +00005038 Sc.getOpcode() != ISD::BUILD_VECTOR) {
5039
5040 if (!Subtarget->hasAVX2())
5041 return SDValue();
5042
5043 // Use the register form of the broadcast instruction available on AVX2.
5044 if (VT.is256BitVector())
5045 Sc = Extract128BitVector(Sc, 0, DAG, dl);
5046 return DAG.getNode(X86ISD::VBROADCAST, dl, VT, Sc);
5047 }
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005048
5049 Ld = Sc.getOperand(0);
Nadav Rotem9d68b062012-04-08 12:54:54 +00005050 ConstSplatVal = (Ld.getOpcode() == ISD::Constant ||
Nadav Rotem154819d2012-04-09 07:45:58 +00005051 Ld.getOpcode() == ISD::ConstantFP);
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005052
5053 // The scalar_to_vector node and the suspected
5054 // load node must have exactly one user.
Nadav Rotem9d68b062012-04-08 12:54:54 +00005055 // Constants may have multiple users.
5056 if (!ConstSplatVal && (!Sc.hasOneUse() || !Ld.hasOneUse()))
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005057 return SDValue();
5058 break;
5059 }
5060 }
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00005061
Craig Topper7a9a28b2012-08-12 02:23:29 +00005062 bool Is256 = VT.is256BitVector();
Nadav Rotem9d68b062012-04-08 12:54:54 +00005063
5064 // Handle the broadcasting a single constant scalar from the constant pool
5065 // into a vector. On Sandybridge it is still better to load a constant vector
5066 // from the constant pool and not to broadcast it from a scalar.
5067 if (ConstSplatVal && Subtarget->hasAVX2()) {
5068 EVT CVT = Ld.getValueType();
5069 assert(!CVT.isVector() && "Must not broadcast a vector type");
5070 unsigned ScalarSize = CVT.getSizeInBits();
5071
Craig Topper5da8a802012-05-04 05:49:51 +00005072 if (ScalarSize == 32 || (Is256 && ScalarSize == 64)) {
Nadav Rotem9d68b062012-04-08 12:54:54 +00005073 const Constant *C = 0;
5074 if (ConstantSDNode *CI = dyn_cast<ConstantSDNode>(Ld))
5075 C = CI->getConstantIntValue();
5076 else if (ConstantFPSDNode *CF = dyn_cast<ConstantFPSDNode>(Ld))
5077 C = CF->getConstantFPValue();
5078
5079 assert(C && "Invalid constant type");
5080
Nadav Rotem154819d2012-04-09 07:45:58 +00005081 SDValue CP = DAG.getConstantPool(C, getPointerTy());
Nadav Rotem9d68b062012-04-08 12:54:54 +00005082 unsigned Alignment = cast<ConstantPoolSDNode>(CP)->getAlignment();
Nadav Rotem154819d2012-04-09 07:45:58 +00005083 Ld = DAG.getLoad(CVT, dl, DAG.getEntryNode(), CP,
Craig Topper6643d9c2012-05-04 06:18:33 +00005084 MachinePointerInfo::getConstantPool(),
5085 false, false, false, Alignment);
Nadav Rotem9d68b062012-04-08 12:54:54 +00005086
Nadav Rotem9d68b062012-04-08 12:54:54 +00005087 return DAG.getNode(X86ISD::VBROADCAST, dl, VT, Ld);
5088 }
5089 }
5090
Nadav Rotem4fc8a5d2012-05-19 19:57:37 +00005091 bool IsLoad = ISD::isNormalLoad(Ld.getNode());
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005092 unsigned ScalarSize = Ld.getValueType().getSizeInBits();
5093
Nadav Rotem4fc8a5d2012-05-19 19:57:37 +00005094 // Handle AVX2 in-register broadcasts.
5095 if (!IsLoad && Subtarget->hasAVX2() &&
5096 (ScalarSize == 32 || (Is256 && ScalarSize == 64)))
5097 return DAG.getNode(X86ISD::VBROADCAST, dl, VT, Ld);
5098
5099 // The scalar source must be a normal load.
5100 if (!IsLoad)
5101 return SDValue();
5102
Craig Topper5da8a802012-05-04 05:49:51 +00005103 if (ScalarSize == 32 || (Is256 && ScalarSize == 64))
Nadav Rotem9d68b062012-04-08 12:54:54 +00005104 return DAG.getNode(X86ISD::VBROADCAST, dl, VT, Ld);
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005105
Craig Toppera9376332012-01-10 08:23:59 +00005106 // The integer check is needed for the 64-bit into 128-bit so it doesn't match
Craig Topper5da8a802012-05-04 05:49:51 +00005107 // double since there is no vbroadcastsd xmm
Craig Toppera9376332012-01-10 08:23:59 +00005108 if (Subtarget->hasAVX2() && Ld.getValueType().isInteger()) {
Craig Topper5da8a802012-05-04 05:49:51 +00005109 if (ScalarSize == 8 || ScalarSize == 16 || ScalarSize == 64)
Nadav Rotem9d68b062012-04-08 12:54:54 +00005110 return DAG.getNode(X86ISD::VBROADCAST, dl, VT, Ld);
Craig Toppera9376332012-01-10 08:23:59 +00005111 }
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00005112
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005113 // Unsupported broadcast.
5114 return SDValue();
5115}
5116
Michael Liao7091b242012-08-14 21:24:47 +00005117// LowerVectorFpExtend - Recognize the scalarized FP_EXTEND from v2f32 to v2f64
5118// and convert it into X86ISD::VFPEXT due to the current ISD::FP_EXTEND has the
5119// constraint of matching input/output vector elements.
5120SDValue
5121X86TargetLowering::LowerVectorFpExtend(SDValue &Op, SelectionDAG &DAG) const {
5122 DebugLoc DL = Op.getDebugLoc();
5123 SDNode *N = Op.getNode();
5124 EVT VT = Op.getValueType();
5125 unsigned NumElts = Op.getNumOperands();
5126
5127 // Check supported types and sub-targets.
5128 //
5129 // Only v2f32 -> v2f64 needs special handling.
5130 if (VT != MVT::v2f64 || !Subtarget->hasSSE2())
5131 return SDValue();
5132
5133 SDValue VecIn;
5134 EVT VecInVT;
5135 SmallVector<int, 8> Mask;
5136 EVT SrcVT = MVT::Other;
5137
5138 // Check the patterns could be translated into X86vfpext.
5139 for (unsigned i = 0; i < NumElts; ++i) {
5140 SDValue In = N->getOperand(i);
5141 unsigned Opcode = In.getOpcode();
5142
5143 // Skip if the element is undefined.
5144 if (Opcode == ISD::UNDEF) {
5145 Mask.push_back(-1);
5146 continue;
5147 }
5148
5149 // Quit if one of the elements is not defined from 'fpext'.
5150 if (Opcode != ISD::FP_EXTEND)
5151 return SDValue();
5152
5153 // Check how the source of 'fpext' is defined.
5154 SDValue L2In = In.getOperand(0);
5155 EVT L2InVT = L2In.getValueType();
5156
5157 // Check the original type
5158 if (SrcVT == MVT::Other)
5159 SrcVT = L2InVT;
5160 else if (SrcVT != L2InVT) // Quit if non-homogenous typed.
5161 return SDValue();
5162
5163 // Check whether the value being 'fpext'ed is extracted from the same
5164 // source.
5165 Opcode = L2In.getOpcode();
5166
5167 // Quit if it's not extracted with a constant index.
5168 if (Opcode != ISD::EXTRACT_VECTOR_ELT ||
5169 !isa<ConstantSDNode>(L2In.getOperand(1)))
5170 return SDValue();
5171
5172 SDValue ExtractedFromVec = L2In.getOperand(0);
5173
5174 if (VecIn.getNode() == 0) {
5175 VecIn = ExtractedFromVec;
5176 VecInVT = ExtractedFromVec.getValueType();
5177 } else if (VecIn != ExtractedFromVec) // Quit if built from more than 1 vec.
5178 return SDValue();
5179
5180 Mask.push_back(cast<ConstantSDNode>(L2In.getOperand(1))->getZExtValue());
5181 }
5182
5183 // Fill the remaining mask as undef.
5184 for (unsigned i = NumElts; i < VecInVT.getVectorNumElements(); ++i)
5185 Mask.push_back(-1);
5186
5187 return DAG.getNode(X86ISD::VFPEXT, DL, VT,
5188 DAG.getVectorShuffle(VecInVT, DL,
5189 VecIn, DAG.getUNDEF(VecInVT),
5190 &Mask[0]));
5191}
5192
Evan Chengc3630942009-12-09 21:00:30 +00005193SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00005194X86TargetLowering::LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005195 DebugLoc dl = Op.getDebugLoc();
David Greenea5f26012011-02-07 19:36:54 +00005196
David Greenef125a292011-02-08 19:04:41 +00005197 EVT VT = Op.getValueType();
5198 EVT ExtVT = VT.getVectorElementType();
David Greenef125a292011-02-08 19:04:41 +00005199 unsigned NumElems = Op.getNumOperands();
5200
Bruno Cardoso Lopes531f19f2011-08-01 19:51:53 +00005201 // Vectors containing all zeros can be matched by pxor and xorps later
5202 if (ISD::isBuildVectorAllZeros(Op.getNode())) {
5203 // Canonicalize this to <4 x i32> to 1) ensure the zero vectors are CSE'd
5204 // and 2) ensure that i64 scalars are eliminated on x86-32 hosts.
Craig Topper07a27622012-01-22 03:07:48 +00005205 if (VT == MVT::v4i32 || VT == MVT::v8i32)
Chris Lattner8a594482007-11-25 00:24:49 +00005206 return Op;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005207
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00005208 return getZeroVector(VT, Subtarget, DAG, dl);
Chris Lattner8a594482007-11-25 00:24:49 +00005209 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00005210
Bruno Cardoso Lopes531f19f2011-08-01 19:51:53 +00005211 // Vectors containing all ones can be matched by pcmpeqd on 128-bit width
Craig Topper745a86b2011-11-19 22:34:59 +00005212 // vectors or broken into v4i32 operations on 256-bit vectors. AVX2 can use
5213 // vpcmpeqd on 256-bit vectors.
Bruno Cardoso Lopes531f19f2011-08-01 19:51:53 +00005214 if (ISD::isBuildVectorAllOnes(Op.getNode())) {
Craig Topper07a27622012-01-22 03:07:48 +00005215 if (VT == MVT::v4i32 || (VT == MVT::v8i32 && Subtarget->hasAVX2()))
Bruno Cardoso Lopes531f19f2011-08-01 19:51:53 +00005216 return Op;
5217
Craig Topper07a27622012-01-22 03:07:48 +00005218 return getOnesVector(VT, Subtarget->hasAVX2(), DAG, dl);
Bruno Cardoso Lopes531f19f2011-08-01 19:51:53 +00005219 }
5220
Nadav Rotem154819d2012-04-09 07:45:58 +00005221 SDValue Broadcast = LowerVectorBroadcast(Op, DAG);
Nadav Rotem9d68b062012-04-08 12:54:54 +00005222 if (Broadcast.getNode())
5223 return Broadcast;
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005224
Michael Liao7091b242012-08-14 21:24:47 +00005225 SDValue FpExt = LowerVectorFpExtend(Op, DAG);
5226 if (FpExt.getNode())
5227 return FpExt;
5228
Owen Andersone50ed302009-08-10 22:56:29 +00005229 unsigned EVTBits = ExtVT.getSizeInBits();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005230
Evan Cheng0db9fe62006-04-25 20:13:52 +00005231 unsigned NumZero = 0;
5232 unsigned NumNonZero = 0;
5233 unsigned NonZeros = 0;
Chris Lattnerc9517fb2008-03-08 22:48:29 +00005234 bool IsAllConstants = true;
Dan Gohman475871a2008-07-27 21:46:04 +00005235 SmallSet<SDValue, 8> Values;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005236 for (unsigned i = 0; i < NumElems; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00005237 SDValue Elt = Op.getOperand(i);
Evan Chengdb2d5242007-12-12 06:45:40 +00005238 if (Elt.getOpcode() == ISD::UNDEF)
5239 continue;
5240 Values.insert(Elt);
5241 if (Elt.getOpcode() != ISD::Constant &&
5242 Elt.getOpcode() != ISD::ConstantFP)
Chris Lattnerc9517fb2008-03-08 22:48:29 +00005243 IsAllConstants = false;
Evan Cheng37b73872009-07-30 08:33:02 +00005244 if (X86::isZeroNode(Elt))
Evan Chengdb2d5242007-12-12 06:45:40 +00005245 NumZero++;
5246 else {
5247 NonZeros |= (1 << i);
5248 NumNonZero++;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005249 }
5250 }
5251
Chris Lattner97a2a562010-08-26 05:24:29 +00005252 // All undef vector. Return an UNDEF. All zero vectors were handled above.
5253 if (NumNonZero == 0)
Dale Johannesene8d72302009-02-06 23:05:02 +00005254 return DAG.getUNDEF(VT);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005255
Chris Lattner67f453a2008-03-09 05:42:06 +00005256 // Special case for single non-zero, non-undef, element.
Eli Friedman10415532009-06-06 06:05:10 +00005257 if (NumNonZero == 1) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00005258 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dan Gohman475871a2008-07-27 21:46:04 +00005259 SDValue Item = Op.getOperand(Idx);
Scott Michelfdc40a02009-02-17 22:15:04 +00005260
Chris Lattner62098042008-03-09 01:05:04 +00005261 // If this is an insertion of an i64 value on x86-32, and if the top bits of
5262 // the value are obviously zero, truncate the value to i32 and do the
5263 // insertion that way. Only do this if the value is non-constant or if the
5264 // value is a constant being inserted into element 0. It is cheaper to do
5265 // a constant pool load than it is to do a movd + shuffle.
Owen Anderson825b72b2009-08-11 20:47:22 +00005266 if (ExtVT == MVT::i64 && !Subtarget->is64Bit() &&
Chris Lattner62098042008-03-09 01:05:04 +00005267 (!IsAllConstants || Idx == 0)) {
5268 if (DAG.MaskedValueIsZero(Item, APInt::getBitsSet(64, 32, 64))) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00005269 // Handle SSE only.
5270 assert(VT == MVT::v2i64 && "Expected an SSE value type!");
5271 EVT VecVT = MVT::v4i32;
5272 unsigned VecElts = 4;
Scott Michelfdc40a02009-02-17 22:15:04 +00005273
Chris Lattner62098042008-03-09 01:05:04 +00005274 // Truncate the value (which may itself be a constant) to i32, and
5275 // convert it to a vector with movd (S2V+shuffle to zero extend).
Owen Anderson825b72b2009-08-11 20:47:22 +00005276 Item = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Item);
Dale Johannesenace16102009-02-03 19:33:06 +00005277 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VecVT, Item);
Craig Topper12216172012-01-13 08:12:35 +00005278 Item = getShuffleVectorZeroOrUndef(Item, 0, true, Subtarget, DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +00005279
Chris Lattner62098042008-03-09 01:05:04 +00005280 // Now we have our 32-bit value zero extended in the low element of
5281 // a vector. If Idx != 0, swizzle it into place.
5282 if (Idx != 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005283 SmallVector<int, 4> Mask;
5284 Mask.push_back(Idx);
5285 for (unsigned i = 1; i != VecElts; ++i)
5286 Mask.push_back(i);
Craig Topperdf966f62012-04-22 19:17:57 +00005287 Item = DAG.getVectorShuffle(VecVT, dl, Item, DAG.getUNDEF(VecVT),
Nate Begeman9008ca62009-04-27 18:41:29 +00005288 &Mask[0]);
Chris Lattner62098042008-03-09 01:05:04 +00005289 }
Craig Topper07a27622012-01-22 03:07:48 +00005290 return DAG.getNode(ISD::BITCAST, dl, VT, Item);
Chris Lattner62098042008-03-09 01:05:04 +00005291 }
5292 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005293
Chris Lattner19f79692008-03-08 22:59:52 +00005294 // If we have a constant or non-constant insertion into the low element of
5295 // a vector, we can do this with SCALAR_TO_VECTOR + shuffle of zero into
5296 // the rest of the elements. This will be matched as movd/movq/movss/movsd
Eli Friedman10415532009-06-06 06:05:10 +00005297 // depending on what the source datatype is.
5298 if (Idx == 0) {
Craig Topperd62c16e2011-12-29 03:20:51 +00005299 if (NumZero == 0)
Eli Friedman10415532009-06-06 06:05:10 +00005300 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
Craig Topperd62c16e2011-12-29 03:20:51 +00005301
5302 if (ExtVT == MVT::i32 || ExtVT == MVT::f32 || ExtVT == MVT::f64 ||
Owen Anderson825b72b2009-08-11 20:47:22 +00005303 (ExtVT == MVT::i64 && Subtarget->is64Bit())) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00005304 if (VT.is256BitVector()) {
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00005305 SDValue ZeroVec = getZeroVector(VT, Subtarget, DAG, dl);
Nadav Rotem394a1f52012-01-11 14:07:51 +00005306 return DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, ZeroVec,
5307 Item, DAG.getIntPtrConstant(0));
Elena Demikhovsky021c0a22011-12-28 08:14:01 +00005308 }
Craig Topper7a9a28b2012-08-12 02:23:29 +00005309 assert(VT.is128BitVector() && "Expected an SSE value type!");
Eli Friedman10415532009-06-06 06:05:10 +00005310 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
5311 // Turn it into a MOVL (i.e. movss, movsd, or movd) to a zero vector.
Craig Topper12216172012-01-13 08:12:35 +00005312 return getShuffleVectorZeroOrUndef(Item, 0, true, Subtarget, DAG);
Craig Topperd62c16e2011-12-29 03:20:51 +00005313 }
5314
5315 if (ExtVT == MVT::i16 || ExtVT == MVT::i8) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005316 Item = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, Item);
Craig Topper3224e6b2011-12-29 03:09:33 +00005317 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32, Item);
Craig Topper7a9a28b2012-08-12 02:23:29 +00005318 if (VT.is256BitVector()) {
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00005319 SDValue ZeroVec = getZeroVector(MVT::v8i32, Subtarget, DAG, dl);
Craig Topperb14940a2012-04-22 20:55:18 +00005320 Item = Insert128BitVector(ZeroVec, Item, 0, DAG, dl);
Craig Topper19ec2a92011-12-29 03:34:54 +00005321 } else {
Craig Topper7a9a28b2012-08-12 02:23:29 +00005322 assert(VT.is128BitVector() && "Expected an SSE value type!");
Craig Topper12216172012-01-13 08:12:35 +00005323 Item = getShuffleVectorZeroOrUndef(Item, 0, true, Subtarget, DAG);
Craig Topper19ec2a92011-12-29 03:34:54 +00005324 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005325 return DAG.getNode(ISD::BITCAST, dl, VT, Item);
Eli Friedman10415532009-06-06 06:05:10 +00005326 }
Chris Lattnerc9517fb2008-03-08 22:48:29 +00005327 }
Evan Chengf26ffe92008-05-29 08:22:04 +00005328
5329 // Is it a vector logical left shift?
5330 if (NumElems == 2 && Idx == 1 &&
Evan Cheng37b73872009-07-30 08:33:02 +00005331 X86::isZeroNode(Op.getOperand(0)) &&
5332 !X86::isZeroNode(Op.getOperand(1))) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00005333 unsigned NumBits = VT.getSizeInBits();
Evan Chengf26ffe92008-05-29 08:22:04 +00005334 return getVShift(true, VT,
Scott Michelfdc40a02009-02-17 22:15:04 +00005335 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Dale Johannesenb300d2a2009-02-07 00:55:49 +00005336 VT, Op.getOperand(1)),
Dale Johannesenace16102009-02-03 19:33:06 +00005337 NumBits/2, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00005338 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005339
Chris Lattnerc9517fb2008-03-08 22:48:29 +00005340 if (IsAllConstants) // Otherwise, it's better to do a constpool load.
Dan Gohman475871a2008-07-27 21:46:04 +00005341 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005342
Chris Lattner19f79692008-03-08 22:59:52 +00005343 // Otherwise, if this is a vector with i32 or f32 elements, and the element
5344 // is a non-constant being inserted into an element other than the low one,
5345 // we can't use a constant pool load. Instead, use SCALAR_TO_VECTOR (aka
5346 // movd/movss) to move this into the low element, then shuffle it into
5347 // place.
Evan Cheng0db9fe62006-04-25 20:13:52 +00005348 if (EVTBits == 32) {
Dale Johannesenace16102009-02-03 19:33:06 +00005349 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
Scott Michelfdc40a02009-02-17 22:15:04 +00005350
Evan Cheng0db9fe62006-04-25 20:13:52 +00005351 // Turn it into a shuffle of zero and zero-extended scalar to vector.
Craig Topper12216172012-01-13 08:12:35 +00005352 Item = getShuffleVectorZeroOrUndef(Item, 0, NumZero > 0, Subtarget, DAG);
Nate Begeman9008ca62009-04-27 18:41:29 +00005353 SmallVector<int, 8> MaskVec;
Craig Topper31a207a2012-05-04 06:39:13 +00005354 for (unsigned i = 0; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00005355 MaskVec.push_back(i == Idx ? 0 : 1);
5356 return DAG.getVectorShuffle(VT, dl, Item, DAG.getUNDEF(VT), &MaskVec[0]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005357 }
5358 }
5359
Chris Lattner67f453a2008-03-09 05:42:06 +00005360 // Splat is obviously ok. Let legalizer expand it to a shuffle.
Evan Chengc3630942009-12-09 21:00:30 +00005361 if (Values.size() == 1) {
5362 if (EVTBits == 32) {
5363 // Instead of a shuffle like this:
5364 // shuffle (scalar_to_vector (load (ptr + 4))), undef, <0, 0, 0, 0>
5365 // Check if it's possible to issue this instead.
5366 // shuffle (vload ptr)), undef, <1, 1, 1, 1>
5367 unsigned Idx = CountTrailingZeros_32(NonZeros);
5368 SDValue Item = Op.getOperand(Idx);
5369 if (Op.getNode()->isOnlyUserOf(Item.getNode()))
5370 return LowerAsSplatVectorLoad(Item, VT, dl, DAG);
5371 }
Dan Gohman475871a2008-07-27 21:46:04 +00005372 return SDValue();
Evan Chengc3630942009-12-09 21:00:30 +00005373 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005374
Dan Gohmana3941172007-07-24 22:55:08 +00005375 // A vector full of immediates; various special cases are already
5376 // handled, so this is best done with a single constant-pool load.
Chris Lattnerc9517fb2008-03-08 22:48:29 +00005377 if (IsAllConstants)
Dan Gohman475871a2008-07-27 21:46:04 +00005378 return SDValue();
Dan Gohmana3941172007-07-24 22:55:08 +00005379
Bruno Cardoso Lopes6683efb2011-07-22 00:15:07 +00005380 // For AVX-length vectors, build the individual 128-bit pieces and use
5381 // shuffles to put them in place.
Craig Topper7a9a28b2012-08-12 02:23:29 +00005382 if (VT.is256BitVector()) {
Bruno Cardoso Lopes6683efb2011-07-22 00:15:07 +00005383 SmallVector<SDValue, 32> V;
Craig Topperfa5b70e2012-02-03 06:32:21 +00005384 for (unsigned i = 0; i != NumElems; ++i)
Bruno Cardoso Lopes6683efb2011-07-22 00:15:07 +00005385 V.push_back(Op.getOperand(i));
5386
5387 EVT HVT = EVT::getVectorVT(*DAG.getContext(), ExtVT, NumElems/2);
5388
5389 // Build both the lower and upper subvector.
5390 SDValue Lower = DAG.getNode(ISD::BUILD_VECTOR, dl, HVT, &V[0], NumElems/2);
5391 SDValue Upper = DAG.getNode(ISD::BUILD_VECTOR, dl, HVT, &V[NumElems / 2],
5392 NumElems/2);
5393
5394 // Recreate the wider vector with the lower and upper part.
Craig Topper4c7972d2012-04-22 18:15:59 +00005395 return Concat128BitVectors(Lower, Upper, VT, NumElems, DAG, dl);
Bruno Cardoso Lopes6683efb2011-07-22 00:15:07 +00005396 }
5397
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00005398 // Let legalizer expand 2-wide build_vectors.
Evan Cheng7e2ff772008-05-08 00:57:18 +00005399 if (EVTBits == 64) {
5400 if (NumNonZero == 1) {
5401 // One half is zero or undef.
5402 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dale Johannesenace16102009-02-03 19:33:06 +00005403 SDValue V2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT,
Evan Cheng7e2ff772008-05-08 00:57:18 +00005404 Op.getOperand(Idx));
Craig Topper12216172012-01-13 08:12:35 +00005405 return getShuffleVectorZeroOrUndef(V2, Idx, true, Subtarget, DAG);
Evan Cheng7e2ff772008-05-08 00:57:18 +00005406 }
Dan Gohman475871a2008-07-27 21:46:04 +00005407 return SDValue();
Evan Cheng7e2ff772008-05-08 00:57:18 +00005408 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00005409
5410 // If element VT is < 32 bits, convert it to inserts into a zero vector.
Bill Wendling826f36f2007-03-28 00:57:11 +00005411 if (EVTBits == 8 && NumElems == 16) {
Dan Gohman475871a2008-07-27 21:46:04 +00005412 SDValue V = LowerBuildVectorv16i8(Op, NonZeros,NumNonZero,NumZero, DAG,
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00005413 Subtarget, *this);
Gabor Greifba36cb52008-08-28 21:40:38 +00005414 if (V.getNode()) return V;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005415 }
5416
Bill Wendling826f36f2007-03-28 00:57:11 +00005417 if (EVTBits == 16 && NumElems == 8) {
Dan Gohman475871a2008-07-27 21:46:04 +00005418 SDValue V = LowerBuildVectorv8i16(Op, NonZeros,NumNonZero,NumZero, DAG,
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00005419 Subtarget, *this);
Gabor Greifba36cb52008-08-28 21:40:38 +00005420 if (V.getNode()) return V;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005421 }
5422
5423 // If element VT is == 32 bits, turn it into a number of shuffles.
Benjamin Kramer9c683542012-01-30 15:16:21 +00005424 SmallVector<SDValue, 8> V(NumElems);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005425 if (NumElems == 4 && NumZero > 0) {
5426 for (unsigned i = 0; i < 4; ++i) {
5427 bool isZero = !(NonZeros & (1 << i));
5428 if (isZero)
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00005429 V[i] = getZeroVector(VT, Subtarget, DAG, dl);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005430 else
Dale Johannesenace16102009-02-03 19:33:06 +00005431 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(i));
Evan Cheng0db9fe62006-04-25 20:13:52 +00005432 }
5433
5434 for (unsigned i = 0; i < 2; ++i) {
5435 switch ((NonZeros & (0x3 << i*2)) >> (i*2)) {
5436 default: break;
5437 case 0:
5438 V[i] = V[i*2]; // Must be a zero vector.
5439 break;
5440 case 1:
Nate Begeman9008ca62009-04-27 18:41:29 +00005441 V[i] = getMOVL(DAG, dl, VT, V[i*2+1], V[i*2]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005442 break;
5443 case 2:
Nate Begeman9008ca62009-04-27 18:41:29 +00005444 V[i] = getMOVL(DAG, dl, VT, V[i*2], V[i*2+1]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005445 break;
5446 case 3:
Nate Begeman9008ca62009-04-27 18:41:29 +00005447 V[i] = getUnpackl(DAG, dl, VT, V[i*2], V[i*2+1]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005448 break;
5449 }
5450 }
5451
Benjamin Kramer9c683542012-01-30 15:16:21 +00005452 bool Reverse1 = (NonZeros & 0x3) == 2;
5453 bool Reverse2 = ((NonZeros & (0x3 << 2)) >> 2) == 2;
5454 int MaskVec[] = {
5455 Reverse1 ? 1 : 0,
5456 Reverse1 ? 0 : 1,
Benjamin Kramer630ecf02012-01-30 20:01:35 +00005457 static_cast<int>(Reverse2 ? NumElems+1 : NumElems),
5458 static_cast<int>(Reverse2 ? NumElems : NumElems+1)
Benjamin Kramer9c683542012-01-30 15:16:21 +00005459 };
Nate Begeman9008ca62009-04-27 18:41:29 +00005460 return DAG.getVectorShuffle(VT, dl, V[0], V[1], &MaskVec[0]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005461 }
5462
Craig Topper7a9a28b2012-08-12 02:23:29 +00005463 if (Values.size() > 1 && VT.is128BitVector()) {
Nate Begemanfdea31a2010-03-24 20:49:50 +00005464 // Check for a build vector of consecutive loads.
5465 for (unsigned i = 0; i < NumElems; ++i)
5466 V[i] = Op.getOperand(i);
Michael J. Spencerec38de22010-10-10 22:04:20 +00005467
Nate Begemanfdea31a2010-03-24 20:49:50 +00005468 // Check for elements which are consecutive loads.
5469 SDValue LD = EltsFromConsecutiveLoads(VT, V, dl, DAG);
5470 if (LD.getNode())
5471 return LD;
Michael J. Spencerec38de22010-10-10 22:04:20 +00005472
5473 // For SSE 4.1, use insertps to put the high elements into the low element.
Craig Topperd0a31172012-01-10 06:37:29 +00005474 if (getSubtarget()->hasSSE41()) {
Chris Lattner24faf612010-08-28 17:59:08 +00005475 SDValue Result;
5476 if (Op.getOperand(0).getOpcode() != ISD::UNDEF)
5477 Result = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(0));
5478 else
5479 Result = DAG.getUNDEF(VT);
Michael J. Spencerec38de22010-10-10 22:04:20 +00005480
Chris Lattner24faf612010-08-28 17:59:08 +00005481 for (unsigned i = 1; i < NumElems; ++i) {
5482 if (Op.getOperand(i).getOpcode() == ISD::UNDEF) continue;
5483 Result = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, Result,
Nate Begeman9008ca62009-04-27 18:41:29 +00005484 Op.getOperand(i), DAG.getIntPtrConstant(i));
Chris Lattner24faf612010-08-28 17:59:08 +00005485 }
5486 return Result;
Nate Begeman9008ca62009-04-27 18:41:29 +00005487 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00005488
Chris Lattner6e80e442010-08-28 17:15:43 +00005489 // Otherwise, expand into a number of unpckl*, start by extending each of
5490 // our (non-undef) elements to the full vector width with the element in the
5491 // bottom slot of the vector (which generates no code for SSE).
5492 for (unsigned i = 0; i < NumElems; ++i) {
5493 if (Op.getOperand(i).getOpcode() != ISD::UNDEF)
5494 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(i));
5495 else
5496 V[i] = DAG.getUNDEF(VT);
5497 }
5498
5499 // Next, we iteratively mix elements, e.g. for v4f32:
Evan Cheng0db9fe62006-04-25 20:13:52 +00005500 // Step 1: unpcklps 0, 2 ==> X: <?, ?, 2, 0>
5501 // : unpcklps 1, 3 ==> Y: <?, ?, 3, 1>
5502 // Step 2: unpcklps X, Y ==> <3, 2, 1, 0>
Chris Lattner6e80e442010-08-28 17:15:43 +00005503 unsigned EltStride = NumElems >> 1;
5504 while (EltStride != 0) {
Chris Lattner3ddcc432010-08-28 17:28:30 +00005505 for (unsigned i = 0; i < EltStride; ++i) {
5506 // If V[i+EltStride] is undef and this is the first round of mixing,
5507 // then it is safe to just drop this shuffle: V[i] is already in the
5508 // right place, the one element (since it's the first round) being
5509 // inserted as undef can be dropped. This isn't safe for successive
5510 // rounds because they will permute elements within both vectors.
5511 if (V[i+EltStride].getOpcode() == ISD::UNDEF &&
5512 EltStride == NumElems/2)
5513 continue;
Michael J. Spencerec38de22010-10-10 22:04:20 +00005514
Chris Lattner6e80e442010-08-28 17:15:43 +00005515 V[i] = getUnpackl(DAG, dl, VT, V[i], V[i + EltStride]);
Chris Lattner3ddcc432010-08-28 17:28:30 +00005516 }
Chris Lattner6e80e442010-08-28 17:15:43 +00005517 EltStride >>= 1;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005518 }
5519 return V[0];
5520 }
Dan Gohman475871a2008-07-27 21:46:04 +00005521 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005522}
5523
Bruno Cardoso Lopes8af24512011-08-01 21:54:02 +00005524// LowerAVXCONCAT_VECTORS - 256-bit AVX can use the vinsertf128 instruction
5525// to create 256-bit vectors from two other 128-bit ones.
5526static SDValue LowerAVXCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) {
5527 DebugLoc dl = Op.getDebugLoc();
5528 EVT ResVT = Op.getValueType();
5529
Craig Topper7a9a28b2012-08-12 02:23:29 +00005530 assert(ResVT.is256BitVector() && "Value type must be 256-bit wide");
Bruno Cardoso Lopes8af24512011-08-01 21:54:02 +00005531
5532 SDValue V1 = Op.getOperand(0);
5533 SDValue V2 = Op.getOperand(1);
5534 unsigned NumElems = ResVT.getVectorNumElements();
5535
Craig Topper4c7972d2012-04-22 18:15:59 +00005536 return Concat128BitVectors(V1, V2, ResVT, NumElems, DAG, dl);
Bruno Cardoso Lopes8af24512011-08-01 21:54:02 +00005537}
5538
5539SDValue
5540X86TargetLowering::LowerCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) const {
Bruno Cardoso Lopes8af24512011-08-01 21:54:02 +00005541 assert(Op.getNumOperands() == 2);
Bruno Cardoso Lopes8af24512011-08-01 21:54:02 +00005542
5543 // 256-bit AVX can use the vinsertf128 instruction to create 256-bit vectors
5544 // from two other 128-bit ones.
5545 return LowerAVXCONCAT_VECTORS(Op, DAG);
5546}
5547
Nadav Roteme80aa7c2012-04-09 08:33:21 +00005548// Try to lower a shuffle node into a simple blend instruction.
Craig Topper1842ba02012-04-23 06:38:28 +00005549static SDValue LowerVECTOR_SHUFFLEtoBlend(ShuffleVectorSDNode *SVOp,
Nadav Roteme80aa7c2012-04-09 08:33:21 +00005550 const X86Subtarget *Subtarget,
Nadav Rotem91794872012-04-11 11:05:21 +00005551 SelectionDAG &DAG) {
Nadav Roteme80aa7c2012-04-09 08:33:21 +00005552 SDValue V1 = SVOp->getOperand(0);
5553 SDValue V2 = SVOp->getOperand(1);
5554 DebugLoc dl = SVOp->getDebugLoc();
Craig Topper708e44f2012-04-23 07:36:33 +00005555 MVT VT = SVOp->getValueType(0).getSimpleVT();
Craig Topper1842ba02012-04-23 06:38:28 +00005556 unsigned NumElems = VT.getVectorNumElements();
Nadav Roteme80aa7c2012-04-09 08:33:21 +00005557
Nadav Roteme6113782012-04-11 06:40:27 +00005558 if (!Subtarget->hasSSE41())
Nadav Roteme80aa7c2012-04-09 08:33:21 +00005559 return SDValue();
5560
Craig Topper1842ba02012-04-23 06:38:28 +00005561 unsigned ISDNo = 0;
Nadav Roteme6113782012-04-11 06:40:27 +00005562 MVT OpTy;
5563
Craig Topper708e44f2012-04-23 07:36:33 +00005564 switch (VT.SimpleTy) {
Nadav Roteme6113782012-04-11 06:40:27 +00005565 default: return SDValue();
5566 case MVT::v8i16:
Craig Topper1842ba02012-04-23 06:38:28 +00005567 ISDNo = X86ISD::BLENDPW;
5568 OpTy = MVT::v8i16;
5569 break;
Nadav Roteme6113782012-04-11 06:40:27 +00005570 case MVT::v4i32:
5571 case MVT::v4f32:
Craig Topper1842ba02012-04-23 06:38:28 +00005572 ISDNo = X86ISD::BLENDPS;
5573 OpTy = MVT::v4f32;
5574 break;
Nadav Roteme6113782012-04-11 06:40:27 +00005575 case MVT::v2i64:
5576 case MVT::v2f64:
Craig Topper1842ba02012-04-23 06:38:28 +00005577 ISDNo = X86ISD::BLENDPD;
5578 OpTy = MVT::v2f64;
5579 break;
Nadav Roteme6113782012-04-11 06:40:27 +00005580 case MVT::v8i32:
5581 case MVT::v8f32:
Craig Topper1842ba02012-04-23 06:38:28 +00005582 if (!Subtarget->hasAVX())
5583 return SDValue();
5584 ISDNo = X86ISD::BLENDPS;
5585 OpTy = MVT::v8f32;
5586 break;
Nadav Roteme6113782012-04-11 06:40:27 +00005587 case MVT::v4i64:
5588 case MVT::v4f64:
Craig Topper1842ba02012-04-23 06:38:28 +00005589 if (!Subtarget->hasAVX())
5590 return SDValue();
5591 ISDNo = X86ISD::BLENDPD;
5592 OpTy = MVT::v4f64;
5593 break;
Nadav Roteme6113782012-04-11 06:40:27 +00005594 }
5595 assert(ISDNo && "Invalid Op Number");
5596
5597 unsigned MaskVals = 0;
Nadav Roteme80aa7c2012-04-09 08:33:21 +00005598
Craig Topper1842ba02012-04-23 06:38:28 +00005599 for (unsigned i = 0; i != NumElems; ++i) {
Nadav Roteme80aa7c2012-04-09 08:33:21 +00005600 int EltIdx = SVOp->getMaskElt(i);
Craig Topper1842ba02012-04-23 06:38:28 +00005601 if (EltIdx == (int)i || EltIdx < 0)
Nadav Roteme6113782012-04-11 06:40:27 +00005602 MaskVals |= (1<<i);
Craig Topper1842ba02012-04-23 06:38:28 +00005603 else if (EltIdx == (int)(i + NumElems))
Nadav Roteme6113782012-04-11 06:40:27 +00005604 continue; // Bit is set to zero;
Craig Topper1842ba02012-04-23 06:38:28 +00005605 else
5606 return SDValue();
Nadav Roteme80aa7c2012-04-09 08:33:21 +00005607 }
5608
Nadav Roteme6113782012-04-11 06:40:27 +00005609 V1 = DAG.getNode(ISD::BITCAST, dl, OpTy, V1);
5610 V2 = DAG.getNode(ISD::BITCAST, dl, OpTy, V2);
5611 SDValue Ret = DAG.getNode(ISDNo, dl, OpTy, V1, V2,
5612 DAG.getConstant(MaskVals, MVT::i32));
5613 return DAG.getNode(ISD::BITCAST, dl, VT, Ret);
Nadav Roteme80aa7c2012-04-09 08:33:21 +00005614}
5615
Nate Begemanb9a47b82009-02-23 08:49:38 +00005616// v8i16 shuffles - Prefer shuffles in the following order:
5617// 1. [all] pshuflw, pshufhw, optional move
5618// 2. [ssse3] 1 x pshufb
5619// 3. [ssse3] 2 x pshufb + 1 x por
5620// 4. [all] mov + pshuflw + pshufhw + N x (pextrw + pinsrw)
Bruno Cardoso Lopesbf8154a2010-08-21 01:32:18 +00005621SDValue
5622X86TargetLowering::LowerVECTOR_SHUFFLEv8i16(SDValue Op,
5623 SelectionDAG &DAG) const {
5624 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Nate Begeman9008ca62009-04-27 18:41:29 +00005625 SDValue V1 = SVOp->getOperand(0);
5626 SDValue V2 = SVOp->getOperand(1);
5627 DebugLoc dl = SVOp->getDebugLoc();
Nate Begemanb9a47b82009-02-23 08:49:38 +00005628 SmallVector<int, 8> MaskVals;
Evan Cheng14b32e12007-12-11 01:46:18 +00005629
Nate Begemanb9a47b82009-02-23 08:49:38 +00005630 // Determine if more than 1 of the words in each of the low and high quadwords
5631 // of the result come from the same quadword of one of the two inputs. Undef
5632 // mask values count as coming from any quadword, for better codegen.
Benjamin Kramer003fad92011-10-15 13:28:31 +00005633 unsigned LoQuad[] = { 0, 0, 0, 0 };
5634 unsigned HiQuad[] = { 0, 0, 0, 0 };
Benjamin Kramer699ddcb2012-02-06 12:06:18 +00005635 std::bitset<4> InputQuads;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005636 for (unsigned i = 0; i < 8; ++i) {
Benjamin Kramer003fad92011-10-15 13:28:31 +00005637 unsigned *Quad = i < 4 ? LoQuad : HiQuad;
Nate Begeman9008ca62009-04-27 18:41:29 +00005638 int EltIdx = SVOp->getMaskElt(i);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005639 MaskVals.push_back(EltIdx);
5640 if (EltIdx < 0) {
5641 ++Quad[0];
5642 ++Quad[1];
5643 ++Quad[2];
5644 ++Quad[3];
Evan Cheng14b32e12007-12-11 01:46:18 +00005645 continue;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005646 }
5647 ++Quad[EltIdx / 4];
5648 InputQuads.set(EltIdx / 4);
Evan Cheng14b32e12007-12-11 01:46:18 +00005649 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00005650
Nate Begemanb9a47b82009-02-23 08:49:38 +00005651 int BestLoQuad = -1;
Evan Cheng14b32e12007-12-11 01:46:18 +00005652 unsigned MaxQuad = 1;
5653 for (unsigned i = 0; i < 4; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005654 if (LoQuad[i] > MaxQuad) {
5655 BestLoQuad = i;
5656 MaxQuad = LoQuad[i];
Evan Cheng14b32e12007-12-11 01:46:18 +00005657 }
Evan Cheng8a86c3f2007-12-07 08:07:39 +00005658 }
5659
Nate Begemanb9a47b82009-02-23 08:49:38 +00005660 int BestHiQuad = -1;
Evan Cheng14b32e12007-12-11 01:46:18 +00005661 MaxQuad = 1;
5662 for (unsigned i = 0; i < 4; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005663 if (HiQuad[i] > MaxQuad) {
5664 BestHiQuad = i;
5665 MaxQuad = HiQuad[i];
Evan Cheng14b32e12007-12-11 01:46:18 +00005666 }
5667 }
5668
Nate Begemanb9a47b82009-02-23 08:49:38 +00005669 // For SSSE3, If all 8 words of the result come from only 1 quadword of each
Eric Christopherfd179292009-08-27 18:07:15 +00005670 // of the two input vectors, shuffle them into one input vector so only a
Nate Begemanb9a47b82009-02-23 08:49:38 +00005671 // single pshufb instruction is necessary. If There are more than 2 input
5672 // quads, disable the next transformation since it does not help SSSE3.
5673 bool V1Used = InputQuads[0] || InputQuads[1];
5674 bool V2Used = InputQuads[2] || InputQuads[3];
Craig Topperd0a31172012-01-10 06:37:29 +00005675 if (Subtarget->hasSSSE3()) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005676 if (InputQuads.count() == 2 && V1Used && V2Used) {
Benjamin Kramer699ddcb2012-02-06 12:06:18 +00005677 BestLoQuad = InputQuads[0] ? 0 : 1;
5678 BestHiQuad = InputQuads[2] ? 2 : 3;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005679 }
5680 if (InputQuads.count() > 2) {
5681 BestLoQuad = -1;
5682 BestHiQuad = -1;
5683 }
5684 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00005685
Nate Begemanb9a47b82009-02-23 08:49:38 +00005686 // If BestLoQuad or BestHiQuad are set, shuffle the quads together and update
5687 // the shuffle mask. If a quad is scored as -1, that means that it contains
5688 // words from all 4 input quadwords.
5689 SDValue NewV;
5690 if (BestLoQuad >= 0 || BestHiQuad >= 0) {
Benjamin Kramer9c683542012-01-30 15:16:21 +00005691 int MaskV[] = {
5692 BestLoQuad < 0 ? 0 : BestLoQuad,
5693 BestHiQuad < 0 ? 1 : BestHiQuad
5694 };
Eric Christopherfd179292009-08-27 18:07:15 +00005695 NewV = DAG.getVectorShuffle(MVT::v2i64, dl,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005696 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64, V1),
5697 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64, V2), &MaskV[0]);
5698 NewV = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, NewV);
Evan Cheng14b32e12007-12-11 01:46:18 +00005699
Nate Begemanb9a47b82009-02-23 08:49:38 +00005700 // Rewrite the MaskVals and assign NewV to V1 if NewV now contains all the
5701 // source words for the shuffle, to aid later transformations.
5702 bool AllWordsInNewV = true;
Mon P Wang37b9a192009-03-11 06:35:11 +00005703 bool InOrder[2] = { true, true };
Evan Cheng14b32e12007-12-11 01:46:18 +00005704 for (unsigned i = 0; i != 8; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005705 int idx = MaskVals[i];
Mon P Wang37b9a192009-03-11 06:35:11 +00005706 if (idx != (int)i)
5707 InOrder[i/4] = false;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005708 if (idx < 0 || (idx/4) == BestLoQuad || (idx/4) == BestHiQuad)
Evan Cheng14b32e12007-12-11 01:46:18 +00005709 continue;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005710 AllWordsInNewV = false;
5711 break;
Evan Cheng14b32e12007-12-11 01:46:18 +00005712 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00005713
Nate Begemanb9a47b82009-02-23 08:49:38 +00005714 bool pshuflw = AllWordsInNewV, pshufhw = AllWordsInNewV;
5715 if (AllWordsInNewV) {
5716 for (int i = 0; i != 8; ++i) {
5717 int idx = MaskVals[i];
5718 if (idx < 0)
Evan Cheng14b32e12007-12-11 01:46:18 +00005719 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00005720 idx = MaskVals[i] = (idx / 4) == BestLoQuad ? (idx & 3) : (idx & 3) + 4;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005721 if ((idx != i) && idx < 4)
5722 pshufhw = false;
5723 if ((idx != i) && idx > 3)
5724 pshuflw = false;
Evan Cheng14b32e12007-12-11 01:46:18 +00005725 }
Nate Begemanb9a47b82009-02-23 08:49:38 +00005726 V1 = NewV;
5727 V2Used = false;
5728 BestLoQuad = 0;
5729 BestHiQuad = 1;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00005730 }
Evan Cheng14b32e12007-12-11 01:46:18 +00005731
Nate Begemanb9a47b82009-02-23 08:49:38 +00005732 // If we've eliminated the use of V2, and the new mask is a pshuflw or
5733 // pshufhw, that's as cheap as it gets. Return the new shuffle.
Mon P Wang37b9a192009-03-11 06:35:11 +00005734 if ((pshufhw && InOrder[0]) || (pshuflw && InOrder[1])) {
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00005735 unsigned Opc = pshufhw ? X86ISD::PSHUFHW : X86ISD::PSHUFLW;
5736 unsigned TargetMask = 0;
5737 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV,
Owen Anderson825b72b2009-08-11 20:47:22 +00005738 DAG.getUNDEF(MVT::v8i16), &MaskVals[0]);
Craig Topperdd637ae2012-02-19 05:41:45 +00005739 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(NewV.getNode());
5740 TargetMask = pshufhw ? getShufflePSHUFHWImmediate(SVOp):
5741 getShufflePSHUFLWImmediate(SVOp);
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00005742 V1 = NewV.getOperand(0);
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00005743 return getTargetShuffleNode(Opc, dl, MVT::v8i16, V1, TargetMask, DAG);
Evan Cheng14b32e12007-12-11 01:46:18 +00005744 }
Evan Cheng14b32e12007-12-11 01:46:18 +00005745 }
Eric Christopherfd179292009-08-27 18:07:15 +00005746
Nate Begemanb9a47b82009-02-23 08:49:38 +00005747 // If we have SSSE3, and all words of the result are from 1 input vector,
5748 // case 2 is generated, otherwise case 3 is generated. If no SSSE3
5749 // is present, fall back to case 4.
Craig Topperd0a31172012-01-10 06:37:29 +00005750 if (Subtarget->hasSSSE3()) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005751 SmallVector<SDValue,16> pshufbMask;
Eric Christopherfd179292009-08-27 18:07:15 +00005752
Nate Begemanb9a47b82009-02-23 08:49:38 +00005753 // If we have elements from both input vectors, set the high bit of the
Eric Christopherfd179292009-08-27 18:07:15 +00005754 // shuffle mask element to zero out elements that come from V2 in the V1
Nate Begemanb9a47b82009-02-23 08:49:38 +00005755 // mask, and elements that come from V1 in the V2 mask, so that the two
5756 // results can be OR'd together.
5757 bool TwoInputs = V1Used && V2Used;
5758 for (unsigned i = 0; i != 8; ++i) {
5759 int EltIdx = MaskVals[i] * 2;
Craig Topperbe97ae92012-05-18 07:07:36 +00005760 int Idx0 = (TwoInputs && (EltIdx >= 16)) ? 0x80 : EltIdx;
5761 int Idx1 = (TwoInputs && (EltIdx >= 16)) ? 0x80 : EltIdx+1;
5762 pshufbMask.push_back(DAG.getConstant(Idx0, MVT::i8));
5763 pshufbMask.push_back(DAG.getConstant(Idx1, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005764 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005765 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, V1);
Eric Christopherfd179292009-08-27 18:07:15 +00005766 V1 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V1,
Evan Chenga87008d2009-02-25 22:49:59 +00005767 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005768 MVT::v16i8, &pshufbMask[0], 16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005769 if (!TwoInputs)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005770 return DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V1);
Eric Christopherfd179292009-08-27 18:07:15 +00005771
Nate Begemanb9a47b82009-02-23 08:49:38 +00005772 // Calculate the shuffle mask for the second input, shuffle it, and
5773 // OR it with the first shuffled input.
5774 pshufbMask.clear();
5775 for (unsigned i = 0; i != 8; ++i) {
5776 int EltIdx = MaskVals[i] * 2;
Craig Topperbe97ae92012-05-18 07:07:36 +00005777 int Idx0 = (EltIdx < 16) ? 0x80 : EltIdx - 16;
5778 int Idx1 = (EltIdx < 16) ? 0x80 : EltIdx - 15;
5779 pshufbMask.push_back(DAG.getConstant(Idx0, MVT::i8));
5780 pshufbMask.push_back(DAG.getConstant(Idx1, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005781 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005782 V2 = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, V2);
Eric Christopherfd179292009-08-27 18:07:15 +00005783 V2 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V2,
Evan Chenga87008d2009-02-25 22:49:59 +00005784 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005785 MVT::v16i8, &pshufbMask[0], 16));
5786 V1 = DAG.getNode(ISD::OR, dl, MVT::v16i8, V1, V2);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005787 return DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005788 }
5789
5790 // If BestLoQuad >= 0, generate a pshuflw to put the low elements in order,
5791 // and update MaskVals with new element order.
Benjamin Kramer9c683542012-01-30 15:16:21 +00005792 std::bitset<8> InOrder;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005793 if (BestLoQuad >= 0) {
Benjamin Kramer9c683542012-01-30 15:16:21 +00005794 int MaskV[] = { -1, -1, -1, -1, 4, 5, 6, 7 };
Nate Begemanb9a47b82009-02-23 08:49:38 +00005795 for (int i = 0; i != 4; ++i) {
5796 int idx = MaskVals[i];
5797 if (idx < 0) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005798 InOrder.set(i);
5799 } else if ((idx / 4) == BestLoQuad) {
Benjamin Kramer9c683542012-01-30 15:16:21 +00005800 MaskV[i] = idx & 3;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005801 InOrder.set(i);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005802 }
5803 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005804 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV, DAG.getUNDEF(MVT::v8i16),
Nate Begeman9008ca62009-04-27 18:41:29 +00005805 &MaskV[0]);
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00005806
Craig Topperdd637ae2012-02-19 05:41:45 +00005807 if (NewV.getOpcode() == ISD::VECTOR_SHUFFLE && Subtarget->hasSSSE3()) {
5808 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(NewV.getNode());
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00005809 NewV = getTargetShuffleNode(X86ISD::PSHUFLW, dl, MVT::v8i16,
Craig Topperdd637ae2012-02-19 05:41:45 +00005810 NewV.getOperand(0),
5811 getShufflePSHUFLWImmediate(SVOp), DAG);
5812 }
Nate Begemanb9a47b82009-02-23 08:49:38 +00005813 }
Eric Christopherfd179292009-08-27 18:07:15 +00005814
Nate Begemanb9a47b82009-02-23 08:49:38 +00005815 // If BestHi >= 0, generate a pshufhw to put the high elements in order,
5816 // and update MaskVals with the new element order.
5817 if (BestHiQuad >= 0) {
Benjamin Kramer9c683542012-01-30 15:16:21 +00005818 int MaskV[] = { 0, 1, 2, 3, -1, -1, -1, -1 };
Nate Begemanb9a47b82009-02-23 08:49:38 +00005819 for (unsigned i = 4; i != 8; ++i) {
5820 int idx = MaskVals[i];
5821 if (idx < 0) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005822 InOrder.set(i);
5823 } else if ((idx / 4) == BestHiQuad) {
Benjamin Kramer9c683542012-01-30 15:16:21 +00005824 MaskV[i] = (idx & 3) + 4;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005825 InOrder.set(i);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005826 }
5827 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005828 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV, DAG.getUNDEF(MVT::v8i16),
Nate Begeman9008ca62009-04-27 18:41:29 +00005829 &MaskV[0]);
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00005830
Craig Topperdd637ae2012-02-19 05:41:45 +00005831 if (NewV.getOpcode() == ISD::VECTOR_SHUFFLE && Subtarget->hasSSSE3()) {
5832 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(NewV.getNode());
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00005833 NewV = getTargetShuffleNode(X86ISD::PSHUFHW, dl, MVT::v8i16,
Craig Topperdd637ae2012-02-19 05:41:45 +00005834 NewV.getOperand(0),
5835 getShufflePSHUFHWImmediate(SVOp), DAG);
5836 }
Nate Begemanb9a47b82009-02-23 08:49:38 +00005837 }
Eric Christopherfd179292009-08-27 18:07:15 +00005838
Nate Begemanb9a47b82009-02-23 08:49:38 +00005839 // In case BestHi & BestLo were both -1, which means each quadword has a word
5840 // from each of the four input quadwords, calculate the InOrder bitvector now
5841 // before falling through to the insert/extract cleanup.
5842 if (BestLoQuad == -1 && BestHiQuad == -1) {
5843 NewV = V1;
5844 for (int i = 0; i != 8; ++i)
5845 if (MaskVals[i] < 0 || MaskVals[i] == i)
5846 InOrder.set(i);
5847 }
Eric Christopherfd179292009-08-27 18:07:15 +00005848
Nate Begemanb9a47b82009-02-23 08:49:38 +00005849 // The other elements are put in the right place using pextrw and pinsrw.
5850 for (unsigned i = 0; i != 8; ++i) {
5851 if (InOrder[i])
5852 continue;
5853 int EltIdx = MaskVals[i];
5854 if (EltIdx < 0)
5855 continue;
Craig Topper6643d9c2012-05-04 06:18:33 +00005856 SDValue ExtOp = (EltIdx < 8) ?
5857 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, V1,
5858 DAG.getIntPtrConstant(EltIdx)) :
5859 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, V2,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005860 DAG.getIntPtrConstant(EltIdx - 8));
Owen Anderson825b72b2009-08-11 20:47:22 +00005861 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, ExtOp,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005862 DAG.getIntPtrConstant(i));
5863 }
5864 return NewV;
5865}
5866
5867// v16i8 shuffles - Prefer shuffles in the following order:
5868// 1. [ssse3] 1 x pshufb
5869// 2. [ssse3] 2 x pshufb + 1 x por
5870// 3. [all] v8i16 shuffle + N x pextrw + rotate + pinsrw
5871static
Nate Begeman9008ca62009-04-27 18:41:29 +00005872SDValue LowerVECTOR_SHUFFLEv16i8(ShuffleVectorSDNode *SVOp,
Dan Gohmand858e902010-04-17 15:26:15 +00005873 SelectionDAG &DAG,
5874 const X86TargetLowering &TLI) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005875 SDValue V1 = SVOp->getOperand(0);
5876 SDValue V2 = SVOp->getOperand(1);
5877 DebugLoc dl = SVOp->getDebugLoc();
Benjamin Kramered4c8c62012-01-15 13:16:05 +00005878 ArrayRef<int> MaskVals = SVOp->getMask();
Eric Christopherfd179292009-08-27 18:07:15 +00005879
Craig Topperb82b5ab2012-05-18 06:42:06 +00005880 bool V2IsUndef = V2.getOpcode() == ISD::UNDEF;
5881
Nate Begemanb9a47b82009-02-23 08:49:38 +00005882 // If we have SSSE3, case 1 is generated when all result bytes come from
Eric Christopherfd179292009-08-27 18:07:15 +00005883 // one of the inputs. Otherwise, case 2 is generated. If no SSSE3 is
Nate Begemanb9a47b82009-02-23 08:49:38 +00005884 // present, fall back to case 3.
Eric Christopherfd179292009-08-27 18:07:15 +00005885
Nate Begemanb9a47b82009-02-23 08:49:38 +00005886 // If SSSE3, use 1 pshufb instruction per vector with elements in the result.
Craig Topperd0a31172012-01-10 06:37:29 +00005887 if (TLI.getSubtarget()->hasSSSE3()) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005888 SmallVector<SDValue,16> pshufbMask;
Eric Christopherfd179292009-08-27 18:07:15 +00005889
Nate Begemanb9a47b82009-02-23 08:49:38 +00005890 // If all result elements are from one input vector, then only translate
Eric Christopherfd179292009-08-27 18:07:15 +00005891 // undef mask values to 0x80 (zero out result) in the pshufb mask.
Nate Begemanb9a47b82009-02-23 08:49:38 +00005892 //
5893 // Otherwise, we have elements from both input vectors, and must zero out
5894 // elements that come from V2 in the first mask, and V1 in the second mask
5895 // so that we can OR them together.
Nate Begemanb9a47b82009-02-23 08:49:38 +00005896 for (unsigned i = 0; i != 16; ++i) {
5897 int EltIdx = MaskVals[i];
Craig Topperb82b5ab2012-05-18 06:42:06 +00005898 if (EltIdx < 0 || EltIdx >= 16)
5899 EltIdx = 0x80;
Owen Anderson825b72b2009-08-11 20:47:22 +00005900 pshufbMask.push_back(DAG.getConstant(EltIdx, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005901 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005902 V1 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V1,
Evan Chenga87008d2009-02-25 22:49:59 +00005903 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005904 MVT::v16i8, &pshufbMask[0], 16));
Craig Topperb82b5ab2012-05-18 06:42:06 +00005905 if (V2IsUndef)
Nate Begemanb9a47b82009-02-23 08:49:38 +00005906 return V1;
Eric Christopherfd179292009-08-27 18:07:15 +00005907
Nate Begemanb9a47b82009-02-23 08:49:38 +00005908 // Calculate the shuffle mask for the second input, shuffle it, and
5909 // OR it with the first shuffled input.
5910 pshufbMask.clear();
5911 for (unsigned i = 0; i != 16; ++i) {
5912 int EltIdx = MaskVals[i];
Craig Topperb82b5ab2012-05-18 06:42:06 +00005913 EltIdx = (EltIdx < 16) ? 0x80 : EltIdx - 16;
Craig Topper85b9e562012-05-22 06:09:38 +00005914 pshufbMask.push_back(DAG.getConstant(EltIdx, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005915 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005916 V2 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V2,
Evan Chenga87008d2009-02-25 22:49:59 +00005917 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005918 MVT::v16i8, &pshufbMask[0], 16));
5919 return DAG.getNode(ISD::OR, dl, MVT::v16i8, V1, V2);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005920 }
Eric Christopherfd179292009-08-27 18:07:15 +00005921
Nate Begemanb9a47b82009-02-23 08:49:38 +00005922 // No SSSE3 - Calculate in place words and then fix all out of place words
5923 // With 0-16 extracts & inserts. Worst case is 16 bytes out of order from
5924 // the 16 different words that comprise the two doublequadword input vectors.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005925 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V1);
5926 V2 = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V2);
Craig Topperb82b5ab2012-05-18 06:42:06 +00005927 SDValue NewV = V1;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005928 for (int i = 0; i != 8; ++i) {
5929 int Elt0 = MaskVals[i*2];
5930 int Elt1 = MaskVals[i*2+1];
Eric Christopherfd179292009-08-27 18:07:15 +00005931
Nate Begemanb9a47b82009-02-23 08:49:38 +00005932 // This word of the result is all undef, skip it.
5933 if (Elt0 < 0 && Elt1 < 0)
5934 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00005935
Nate Begemanb9a47b82009-02-23 08:49:38 +00005936 // This word of the result is already in the correct place, skip it.
Craig Topperb82b5ab2012-05-18 06:42:06 +00005937 if ((Elt0 == i*2) && (Elt1 == i*2+1))
Nate Begemanb9a47b82009-02-23 08:49:38 +00005938 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00005939
Nate Begemanb9a47b82009-02-23 08:49:38 +00005940 SDValue Elt0Src = Elt0 < 16 ? V1 : V2;
5941 SDValue Elt1Src = Elt1 < 16 ? V1 : V2;
5942 SDValue InsElt;
Mon P Wang6b3ef692009-03-11 18:47:57 +00005943
5944 // If Elt0 and Elt1 are defined, are consecutive, and can be load
5945 // using a single extract together, load it and store it.
5946 if ((Elt0 >= 0) && ((Elt0 + 1) == Elt1) && ((Elt0 & 1) == 0)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005947 InsElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, Elt1Src,
Mon P Wang6b3ef692009-03-11 18:47:57 +00005948 DAG.getIntPtrConstant(Elt1 / 2));
Owen Anderson825b72b2009-08-11 20:47:22 +00005949 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, InsElt,
Mon P Wang6b3ef692009-03-11 18:47:57 +00005950 DAG.getIntPtrConstant(i));
5951 continue;
5952 }
5953
Nate Begemanb9a47b82009-02-23 08:49:38 +00005954 // If Elt1 is defined, extract it from the appropriate source. If the
Mon P Wang6b3ef692009-03-11 18:47:57 +00005955 // source byte is not also odd, shift the extracted word left 8 bits
5956 // otherwise clear the bottom 8 bits if we need to do an or.
Nate Begemanb9a47b82009-02-23 08:49:38 +00005957 if (Elt1 >= 0) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005958 InsElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, Elt1Src,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005959 DAG.getIntPtrConstant(Elt1 / 2));
5960 if ((Elt1 & 1) == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00005961 InsElt = DAG.getNode(ISD::SHL, dl, MVT::i16, InsElt,
Owen Anderson95771af2011-02-25 21:41:48 +00005962 DAG.getConstant(8,
5963 TLI.getShiftAmountTy(InsElt.getValueType())));
Mon P Wang6b3ef692009-03-11 18:47:57 +00005964 else if (Elt0 >= 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00005965 InsElt = DAG.getNode(ISD::AND, dl, MVT::i16, InsElt,
5966 DAG.getConstant(0xFF00, MVT::i16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005967 }
5968 // If Elt0 is defined, extract it from the appropriate source. If the
5969 // source byte is not also even, shift the extracted word right 8 bits. If
5970 // Elt1 was also defined, OR the extracted values together before
5971 // inserting them in the result.
5972 if (Elt0 >= 0) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005973 SDValue InsElt0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005974 Elt0Src, DAG.getIntPtrConstant(Elt0 / 2));
5975 if ((Elt0 & 1) != 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00005976 InsElt0 = DAG.getNode(ISD::SRL, dl, MVT::i16, InsElt0,
Owen Anderson95771af2011-02-25 21:41:48 +00005977 DAG.getConstant(8,
5978 TLI.getShiftAmountTy(InsElt0.getValueType())));
Mon P Wang6b3ef692009-03-11 18:47:57 +00005979 else if (Elt1 >= 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00005980 InsElt0 = DAG.getNode(ISD::AND, dl, MVT::i16, InsElt0,
5981 DAG.getConstant(0x00FF, MVT::i16));
5982 InsElt = Elt1 >= 0 ? DAG.getNode(ISD::OR, dl, MVT::i16, InsElt, InsElt0)
Nate Begemanb9a47b82009-02-23 08:49:38 +00005983 : InsElt0;
5984 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005985 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, InsElt,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005986 DAG.getIntPtrConstant(i));
5987 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005988 return DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, NewV);
Evan Cheng14b32e12007-12-11 01:46:18 +00005989}
5990
Evan Cheng7a831ce2007-12-15 03:00:47 +00005991/// RewriteAsNarrowerShuffle - Try rewriting v8i16 and v16i8 shuffles as 4 wide
Bruno Cardoso Lopes0a7dd4f2010-09-08 18:12:31 +00005992/// ones, or rewriting v4i32 / v4f32 as 2 wide ones if possible. This can be
Evan Cheng7a831ce2007-12-15 03:00:47 +00005993/// done when every pair / quad of shuffle mask elements point to elements in
5994/// the right sequence. e.g.
Bruno Cardoso Lopes0a7dd4f2010-09-08 18:12:31 +00005995/// vector_shuffle X, Y, <2, 3, | 10, 11, | 0, 1, | 14, 15>
Evan Cheng14b32e12007-12-11 01:46:18 +00005996static
Nate Begeman9008ca62009-04-27 18:41:29 +00005997SDValue RewriteAsNarrowerShuffle(ShuffleVectorSDNode *SVOp,
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005998 SelectionDAG &DAG, DebugLoc dl) {
Craig Topper11ac1f82012-05-04 04:08:44 +00005999 MVT VT = SVOp->getValueType(0).getSimpleVT();
Nate Begeman9008ca62009-04-27 18:41:29 +00006000 unsigned NumElems = VT.getVectorNumElements();
Craig Topper11ac1f82012-05-04 04:08:44 +00006001 MVT NewVT;
6002 unsigned Scale;
6003 switch (VT.SimpleTy) {
Craig Topperabb94d02012-02-05 03:43:23 +00006004 default: llvm_unreachable("Unexpected!");
Craig Topperf3640d72012-05-04 04:44:49 +00006005 case MVT::v4f32: NewVT = MVT::v2f64; Scale = 2; break;
6006 case MVT::v4i32: NewVT = MVT::v2i64; Scale = 2; break;
6007 case MVT::v8i16: NewVT = MVT::v4i32; Scale = 2; break;
6008 case MVT::v16i8: NewVT = MVT::v4i32; Scale = 4; break;
6009 case MVT::v16i16: NewVT = MVT::v8i32; Scale = 2; break;
6010 case MVT::v32i8: NewVT = MVT::v8i32; Scale = 4; break;
Evan Cheng7a831ce2007-12-15 03:00:47 +00006011 }
6012
Nate Begeman9008ca62009-04-27 18:41:29 +00006013 SmallVector<int, 8> MaskVec;
Craig Topper11ac1f82012-05-04 04:08:44 +00006014 for (unsigned i = 0; i != NumElems; i += Scale) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006015 int StartIdx = -1;
Craig Topper11ac1f82012-05-04 04:08:44 +00006016 for (unsigned j = 0; j != Scale; ++j) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006017 int EltIdx = SVOp->getMaskElt(i+j);
6018 if (EltIdx < 0)
Evan Cheng14b32e12007-12-11 01:46:18 +00006019 continue;
Craig Topper11ac1f82012-05-04 04:08:44 +00006020 if (StartIdx < 0)
6021 StartIdx = (EltIdx / Scale);
6022 if (EltIdx != (int)(StartIdx*Scale + j))
Dan Gohman475871a2008-07-27 21:46:04 +00006023 return SDValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00006024 }
Craig Topper11ac1f82012-05-04 04:08:44 +00006025 MaskVec.push_back(StartIdx);
Evan Cheng8a86c3f2007-12-07 08:07:39 +00006026 }
6027
Craig Topper11ac1f82012-05-04 04:08:44 +00006028 SDValue V1 = DAG.getNode(ISD::BITCAST, dl, NewVT, SVOp->getOperand(0));
6029 SDValue V2 = DAG.getNode(ISD::BITCAST, dl, NewVT, SVOp->getOperand(1));
Nate Begeman9008ca62009-04-27 18:41:29 +00006030 return DAG.getVectorShuffle(NewVT, dl, V1, V2, &MaskVec[0]);
Evan Cheng8a86c3f2007-12-07 08:07:39 +00006031}
6032
Evan Chengd880b972008-05-09 21:53:03 +00006033/// getVZextMovL - Return a zero-extending vector move low node.
Evan Cheng7e2ff772008-05-08 00:57:18 +00006034///
Owen Andersone50ed302009-08-10 22:56:29 +00006035static SDValue getVZextMovL(EVT VT, EVT OpVT,
Nate Begeman9008ca62009-04-27 18:41:29 +00006036 SDValue SrcOp, SelectionDAG &DAG,
6037 const X86Subtarget *Subtarget, DebugLoc dl) {
Owen Anderson825b72b2009-08-11 20:47:22 +00006038 if (VT == MVT::v2f64 || VT == MVT::v4f32) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00006039 LoadSDNode *LD = NULL;
Gabor Greifba36cb52008-08-28 21:40:38 +00006040 if (!isScalarLoadToVector(SrcOp.getNode(), &LD))
Evan Cheng7e2ff772008-05-08 00:57:18 +00006041 LD = dyn_cast<LoadSDNode>(SrcOp);
6042 if (!LD) {
6043 // movssrr and movsdrr do not clear top bits. Try to use movd, movq
6044 // instead.
Owen Anderson766b5ef2009-08-11 21:59:30 +00006045 MVT ExtVT = (OpVT == MVT::v2f64) ? MVT::i64 : MVT::i32;
Duncan Sandscdfad362010-11-03 12:17:33 +00006046 if ((ExtVT != MVT::i64 || Subtarget->is64Bit()) &&
Evan Cheng7e2ff772008-05-08 00:57:18 +00006047 SrcOp.getOpcode() == ISD::SCALAR_TO_VECTOR &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006048 SrcOp.getOperand(0).getOpcode() == ISD::BITCAST &&
Owen Anderson766b5ef2009-08-11 21:59:30 +00006049 SrcOp.getOperand(0).getOperand(0).getValueType() == ExtVT) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00006050 // PR2108
Owen Anderson825b72b2009-08-11 20:47:22 +00006051 OpVT = (OpVT == MVT::v2f64) ? MVT::v2i64 : MVT::v4i32;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006052 return DAG.getNode(ISD::BITCAST, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00006053 DAG.getNode(X86ISD::VZEXT_MOVL, dl, OpVT,
6054 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
6055 OpVT,
Gabor Greif327ef032008-08-28 23:19:51 +00006056 SrcOp.getOperand(0)
6057 .getOperand(0))));
Evan Cheng7e2ff772008-05-08 00:57:18 +00006058 }
6059 }
6060 }
6061
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006062 return DAG.getNode(ISD::BITCAST, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00006063 DAG.getNode(X86ISD::VZEXT_MOVL, dl, OpVT,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006064 DAG.getNode(ISD::BITCAST, dl,
Dale Johannesenace16102009-02-03 19:33:06 +00006065 OpVT, SrcOp)));
Evan Cheng7e2ff772008-05-08 00:57:18 +00006066}
6067
Bruno Cardoso Lopesd0888342011-07-22 00:14:56 +00006068/// LowerVECTOR_SHUFFLE_256 - Handle all 256-bit wide vectors shuffles
6069/// which could not be matched by any known target speficic shuffle
6070static SDValue
6071LowerVECTOR_SHUFFLE_256(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG) {
Elena Demikhovsky15963732012-06-26 08:04:10 +00006072
6073 SDValue NewOp = Compact8x32ShuffleNode(SVOp, DAG);
6074 if (NewOp.getNode())
6075 return NewOp;
6076
Craig Topper8f35c132012-01-20 09:29:03 +00006077 EVT VT = SVOp->getValueType(0);
Bruno Cardoso Lopes3b865982011-08-16 18:21:54 +00006078
Craig Topper8f35c132012-01-20 09:29:03 +00006079 unsigned NumElems = VT.getVectorNumElements();
6080 unsigned NumLaneElems = NumElems / 2;
6081
Craig Topper8f35c132012-01-20 09:29:03 +00006082 DebugLoc dl = SVOp->getDebugLoc();
6083 MVT EltVT = VT.getVectorElementType().getSimpleVT();
Craig Topper9a2b6e12012-04-06 07:45:23 +00006084 EVT NVT = MVT::getVectorVT(EltVT, NumLaneElems);
Craig Topper8ae97ba2012-05-21 06:40:16 +00006085 SDValue Output[2];
Craig Topper8f35c132012-01-20 09:29:03 +00006086
Craig Topper9a2b6e12012-04-06 07:45:23 +00006087 SmallVector<int, 16> Mask;
Craig Topper8f35c132012-01-20 09:29:03 +00006088 for (unsigned l = 0; l < 2; ++l) {
Craig Topper9a2b6e12012-04-06 07:45:23 +00006089 // Build a shuffle mask for the output, discovering on the fly which
6090 // input vectors to use as shuffle operands (recorded in InputUsed).
6091 // If building a suitable shuffle vector proves too hard, then bail
Craig Topper8ae97ba2012-05-21 06:40:16 +00006092 // out with UseBuildVector set.
6093 bool UseBuildVector = false;
Benjamin Kramer9e5512a2012-04-06 13:33:52 +00006094 int InputUsed[2] = { -1, -1 }; // Not yet discovered.
Craig Topper9a2b6e12012-04-06 07:45:23 +00006095 unsigned LaneStart = l * NumLaneElems;
6096 for (unsigned i = 0; i != NumLaneElems; ++i) {
6097 // The mask element. This indexes into the input.
6098 int Idx = SVOp->getMaskElt(i+LaneStart);
6099 if (Idx < 0) {
6100 // the mask element does not index into any input vector.
6101 Mask.push_back(-1);
6102 continue;
6103 }
Craig Topper8f35c132012-01-20 09:29:03 +00006104
Craig Topper9a2b6e12012-04-06 07:45:23 +00006105 // The input vector this mask element indexes into.
6106 int Input = Idx / NumLaneElems;
Craig Topper8f35c132012-01-20 09:29:03 +00006107
Craig Topper9a2b6e12012-04-06 07:45:23 +00006108 // Turn the index into an offset from the start of the input vector.
6109 Idx -= Input * NumLaneElems;
6110
6111 // Find or create a shuffle vector operand to hold this input.
6112 unsigned OpNo;
6113 for (OpNo = 0; OpNo < array_lengthof(InputUsed); ++OpNo) {
6114 if (InputUsed[OpNo] == Input)
6115 // This input vector is already an operand.
6116 break;
6117 if (InputUsed[OpNo] < 0) {
6118 // Create a new operand for this input vector.
6119 InputUsed[OpNo] = Input;
6120 break;
6121 }
6122 }
6123
6124 if (OpNo >= array_lengthof(InputUsed)) {
Craig Topper8ae97ba2012-05-21 06:40:16 +00006125 // More than two input vectors used! Give up on trying to create a
6126 // shuffle vector. Insert all elements into a BUILD_VECTOR instead.
6127 UseBuildVector = true;
6128 break;
Craig Topper9a2b6e12012-04-06 07:45:23 +00006129 }
6130
6131 // Add the mask index for the new shuffle vector.
6132 Mask.push_back(Idx + OpNo * NumLaneElems);
6133 }
6134
Craig Topper8ae97ba2012-05-21 06:40:16 +00006135 if (UseBuildVector) {
6136 SmallVector<SDValue, 16> SVOps;
6137 for (unsigned i = 0; i != NumLaneElems; ++i) {
6138 // The mask element. This indexes into the input.
6139 int Idx = SVOp->getMaskElt(i+LaneStart);
6140 if (Idx < 0) {
6141 SVOps.push_back(DAG.getUNDEF(EltVT));
6142 continue;
6143 }
6144
6145 // The input vector this mask element indexes into.
6146 int Input = Idx / NumElems;
6147
6148 // Turn the index into an offset from the start of the input vector.
6149 Idx -= Input * NumElems;
6150
6151 // Extract the vector element by hand.
6152 SVOps.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EltVT,
6153 SVOp->getOperand(Input),
6154 DAG.getIntPtrConstant(Idx)));
6155 }
6156
6157 // Construct the output using a BUILD_VECTOR.
6158 Output[l] = DAG.getNode(ISD::BUILD_VECTOR, dl, NVT, &SVOps[0],
6159 SVOps.size());
6160 } else if (InputUsed[0] < 0) {
Craig Topper9a2b6e12012-04-06 07:45:23 +00006161 // No input vectors were used! The result is undefined.
Craig Topper8ae97ba2012-05-21 06:40:16 +00006162 Output[l] = DAG.getUNDEF(NVT);
Craig Topper9a2b6e12012-04-06 07:45:23 +00006163 } else {
6164 SDValue Op0 = Extract128BitVector(SVOp->getOperand(InputUsed[0] / 2),
Craig Topperb14940a2012-04-22 20:55:18 +00006165 (InputUsed[0] % 2) * NumLaneElems,
6166 DAG, dl);
Craig Topper9a2b6e12012-04-06 07:45:23 +00006167 // If only one input was used, use an undefined vector for the other.
6168 SDValue Op1 = (InputUsed[1] < 0) ? DAG.getUNDEF(NVT) :
6169 Extract128BitVector(SVOp->getOperand(InputUsed[1] / 2),
Craig Topperb14940a2012-04-22 20:55:18 +00006170 (InputUsed[1] % 2) * NumLaneElems, DAG, dl);
Craig Topper9a2b6e12012-04-06 07:45:23 +00006171 // At least one input vector was used. Create a new shuffle vector.
Craig Topper8ae97ba2012-05-21 06:40:16 +00006172 Output[l] = DAG.getVectorShuffle(NVT, dl, Op0, Op1, &Mask[0]);
Craig Topper9a2b6e12012-04-06 07:45:23 +00006173 }
6174
6175 Mask.clear();
6176 }
Craig Topper8f35c132012-01-20 09:29:03 +00006177
6178 // Concatenate the result back
Craig Topper8ae97ba2012-05-21 06:40:16 +00006179 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT, Output[0], Output[1]);
Bruno Cardoso Lopesd0888342011-07-22 00:14:56 +00006180}
6181
Bruno Cardoso Lopes589b8972011-07-22 00:14:53 +00006182/// LowerVECTOR_SHUFFLE_128v4 - Handle all 128-bit wide vectors with
6183/// 4 elements, and match them with several different shuffle types.
Dan Gohman475871a2008-07-27 21:46:04 +00006184static SDValue
Bruno Cardoso Lopes589b8972011-07-22 00:14:53 +00006185LowerVECTOR_SHUFFLE_128v4(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006186 SDValue V1 = SVOp->getOperand(0);
6187 SDValue V2 = SVOp->getOperand(1);
6188 DebugLoc dl = SVOp->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00006189 EVT VT = SVOp->getValueType(0);
Eric Christopherfd179292009-08-27 18:07:15 +00006190
Craig Topper7a9a28b2012-08-12 02:23:29 +00006191 assert(VT.is128BitVector() && "Unsupported vector size");
Bruno Cardoso Lopes589b8972011-07-22 00:14:53 +00006192
Benjamin Kramer9c683542012-01-30 15:16:21 +00006193 std::pair<int, int> Locs[4];
6194 int Mask1[] = { -1, -1, -1, -1 };
Benjamin Kramered4c8c62012-01-15 13:16:05 +00006195 SmallVector<int, 8> PermMask(SVOp->getMask().begin(), SVOp->getMask().end());
Nate Begeman9008ca62009-04-27 18:41:29 +00006196
Evan Chengace3c172008-07-22 21:13:36 +00006197 unsigned NumHi = 0;
6198 unsigned NumLo = 0;
Evan Chengace3c172008-07-22 21:13:36 +00006199 for (unsigned i = 0; i != 4; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006200 int Idx = PermMask[i];
6201 if (Idx < 0) {
Evan Chengace3c172008-07-22 21:13:36 +00006202 Locs[i] = std::make_pair(-1, -1);
6203 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00006204 assert(Idx < 8 && "Invalid VECTOR_SHUFFLE index!");
6205 if (Idx < 4) {
Evan Chengace3c172008-07-22 21:13:36 +00006206 Locs[i] = std::make_pair(0, NumLo);
Nate Begeman9008ca62009-04-27 18:41:29 +00006207 Mask1[NumLo] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00006208 NumLo++;
6209 } else {
6210 Locs[i] = std::make_pair(1, NumHi);
6211 if (2+NumHi < 4)
Nate Begeman9008ca62009-04-27 18:41:29 +00006212 Mask1[2+NumHi] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00006213 NumHi++;
6214 }
6215 }
6216 }
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006217
Evan Chengace3c172008-07-22 21:13:36 +00006218 if (NumLo <= 2 && NumHi <= 2) {
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006219 // If no more than two elements come from either vector. This can be
6220 // implemented with two shuffles. First shuffle gather the elements.
6221 // The second shuffle, which takes the first shuffle as both of its
6222 // vector operands, put the elements into the right order.
Nate Begeman9008ca62009-04-27 18:41:29 +00006223 V1 = DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006224
Benjamin Kramer9c683542012-01-30 15:16:21 +00006225 int Mask2[] = { -1, -1, -1, -1 };
Eric Christopherfd179292009-08-27 18:07:15 +00006226
Benjamin Kramer9c683542012-01-30 15:16:21 +00006227 for (unsigned i = 0; i != 4; ++i)
6228 if (Locs[i].first != -1) {
Evan Chengace3c172008-07-22 21:13:36 +00006229 unsigned Idx = (i < 2) ? 0 : 4;
6230 Idx += Locs[i].first * 2 + Locs[i].second;
Nate Begeman9008ca62009-04-27 18:41:29 +00006231 Mask2[i] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00006232 }
Evan Chengace3c172008-07-22 21:13:36 +00006233
Nate Begeman9008ca62009-04-27 18:41:29 +00006234 return DAG.getVectorShuffle(VT, dl, V1, V1, &Mask2[0]);
Craig Topper69947b92012-04-23 06:57:04 +00006235 }
6236
6237 if (NumLo == 3 || NumHi == 3) {
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006238 // Otherwise, we must have three elements from one vector, call it X, and
6239 // one element from the other, call it Y. First, use a shufps to build an
6240 // intermediate vector with the one element from Y and the element from X
6241 // that will be in the same half in the final destination (the indexes don't
6242 // matter). Then, use a shufps to build the final vector, taking the half
6243 // containing the element from Y from the intermediate, and the other half
6244 // from X.
6245 if (NumHi == 3) {
6246 // Normalize it so the 3 elements come from V1.
Craig Topperbeabc6c2011-12-05 06:56:46 +00006247 CommuteVectorShuffleMask(PermMask, 4);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006248 std::swap(V1, V2);
6249 }
6250
6251 // Find the element from V2.
6252 unsigned HiIndex;
6253 for (HiIndex = 0; HiIndex < 3; ++HiIndex) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006254 int Val = PermMask[HiIndex];
6255 if (Val < 0)
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006256 continue;
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006257 if (Val >= 4)
6258 break;
6259 }
6260
Nate Begeman9008ca62009-04-27 18:41:29 +00006261 Mask1[0] = PermMask[HiIndex];
6262 Mask1[1] = -1;
6263 Mask1[2] = PermMask[HiIndex^1];
6264 Mask1[3] = -1;
6265 V2 = DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006266
6267 if (HiIndex >= 2) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006268 Mask1[0] = PermMask[0];
6269 Mask1[1] = PermMask[1];
6270 Mask1[2] = HiIndex & 1 ? 6 : 4;
6271 Mask1[3] = HiIndex & 1 ? 4 : 6;
6272 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006273 }
Craig Topper69947b92012-04-23 06:57:04 +00006274
6275 Mask1[0] = HiIndex & 1 ? 2 : 0;
6276 Mask1[1] = HiIndex & 1 ? 0 : 2;
6277 Mask1[2] = PermMask[2];
6278 Mask1[3] = PermMask[3];
6279 if (Mask1[2] >= 0)
6280 Mask1[2] += 4;
6281 if (Mask1[3] >= 0)
6282 Mask1[3] += 4;
6283 return DAG.getVectorShuffle(VT, dl, V2, V1, &Mask1[0]);
Evan Chengace3c172008-07-22 21:13:36 +00006284 }
6285
6286 // Break it into (shuffle shuffle_hi, shuffle_lo).
Benjamin Kramer9c683542012-01-30 15:16:21 +00006287 int LoMask[] = { -1, -1, -1, -1 };
6288 int HiMask[] = { -1, -1, -1, -1 };
Nate Begeman9008ca62009-04-27 18:41:29 +00006289
Benjamin Kramer9c683542012-01-30 15:16:21 +00006290 int *MaskPtr = LoMask;
Evan Chengace3c172008-07-22 21:13:36 +00006291 unsigned MaskIdx = 0;
6292 unsigned LoIdx = 0;
6293 unsigned HiIdx = 2;
6294 for (unsigned i = 0; i != 4; ++i) {
6295 if (i == 2) {
Benjamin Kramer9c683542012-01-30 15:16:21 +00006296 MaskPtr = HiMask;
Evan Chengace3c172008-07-22 21:13:36 +00006297 MaskIdx = 1;
6298 LoIdx = 0;
6299 HiIdx = 2;
6300 }
Nate Begeman9008ca62009-04-27 18:41:29 +00006301 int Idx = PermMask[i];
6302 if (Idx < 0) {
Evan Chengace3c172008-07-22 21:13:36 +00006303 Locs[i] = std::make_pair(-1, -1);
Nate Begeman9008ca62009-04-27 18:41:29 +00006304 } else if (Idx < 4) {
Evan Chengace3c172008-07-22 21:13:36 +00006305 Locs[i] = std::make_pair(MaskIdx, LoIdx);
Benjamin Kramer9c683542012-01-30 15:16:21 +00006306 MaskPtr[LoIdx] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00006307 LoIdx++;
6308 } else {
6309 Locs[i] = std::make_pair(MaskIdx, HiIdx);
Benjamin Kramer9c683542012-01-30 15:16:21 +00006310 MaskPtr[HiIdx] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00006311 HiIdx++;
6312 }
6313 }
6314
Nate Begeman9008ca62009-04-27 18:41:29 +00006315 SDValue LoShuffle = DAG.getVectorShuffle(VT, dl, V1, V2, &LoMask[0]);
6316 SDValue HiShuffle = DAG.getVectorShuffle(VT, dl, V1, V2, &HiMask[0]);
Benjamin Kramer9c683542012-01-30 15:16:21 +00006317 int MaskOps[] = { -1, -1, -1, -1 };
6318 for (unsigned i = 0; i != 4; ++i)
6319 if (Locs[i].first != -1)
6320 MaskOps[i] = Locs[i].first * 4 + Locs[i].second;
Nate Begeman9008ca62009-04-27 18:41:29 +00006321 return DAG.getVectorShuffle(VT, dl, LoShuffle, HiShuffle, &MaskOps[0]);
Evan Chengace3c172008-07-22 21:13:36 +00006322}
6323
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00006324static bool MayFoldVectorLoad(SDValue V) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006325 if (V.hasOneUse() && V.getOpcode() == ISD::BITCAST)
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00006326 V = V.getOperand(0);
6327 if (V.hasOneUse() && V.getOpcode() == ISD::SCALAR_TO_VECTOR)
6328 V = V.getOperand(0);
Evan Cheng7bc389b2011-11-08 00:31:58 +00006329 if (V.hasOneUse() && V.getOpcode() == ISD::BUILD_VECTOR &&
6330 V.getNumOperands() == 2 && V.getOperand(1).getOpcode() == ISD::UNDEF)
6331 // BUILD_VECTOR (load), undef
6332 V = V.getOperand(0);
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00006333 if (MayFoldLoad(V))
6334 return true;
6335 return false;
6336}
6337
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006338// FIXME: the version above should always be used. Since there's
6339// a bug where several vector shuffles can't be folded because the
6340// DAG is not updated during lowering and a node claims to have two
6341// uses while it only has one, use this version, and let isel match
6342// another instruction if the load really happens to have more than
6343// one use. Remove this version after this bug get fixed.
Evan Cheng835580f2010-10-07 20:50:20 +00006344// rdar://8434668, PR8156
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006345static bool RelaxedMayFoldVectorLoad(SDValue V) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006346 if (V.hasOneUse() && V.getOpcode() == ISD::BITCAST)
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006347 V = V.getOperand(0);
6348 if (V.hasOneUse() && V.getOpcode() == ISD::SCALAR_TO_VECTOR)
6349 V = V.getOperand(0);
6350 if (ISD::isNormalLoad(V.getNode()))
6351 return true;
6352 return false;
6353}
6354
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006355static
Evan Cheng835580f2010-10-07 20:50:20 +00006356SDValue getMOVDDup(SDValue &Op, DebugLoc &dl, SDValue V1, SelectionDAG &DAG) {
6357 EVT VT = Op.getValueType();
6358
6359 // Canonizalize to v2f64.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006360 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, V1);
6361 return DAG.getNode(ISD::BITCAST, dl, VT,
Evan Cheng835580f2010-10-07 20:50:20 +00006362 getTargetShuffleNode(X86ISD::MOVDDUP, dl, MVT::v2f64,
6363 V1, DAG));
6364}
6365
6366static
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006367SDValue getMOVLowToHigh(SDValue &Op, DebugLoc &dl, SelectionDAG &DAG,
Craig Topper1accb7e2012-01-10 06:54:16 +00006368 bool HasSSE2) {
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006369 SDValue V1 = Op.getOperand(0);
6370 SDValue V2 = Op.getOperand(1);
6371 EVT VT = Op.getValueType();
6372
6373 assert(VT != MVT::v2i64 && "unsupported shuffle type");
6374
Craig Topper1accb7e2012-01-10 06:54:16 +00006375 if (HasSSE2 && VT == MVT::v2f64)
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006376 return getTargetShuffleNode(X86ISD::MOVLHPD, dl, VT, V1, V2, DAG);
6377
Evan Cheng0899f5c2011-08-31 02:05:24 +00006378 // v4f32 or v4i32: canonizalized to v4f32 (which is legal for SSE1)
6379 return DAG.getNode(ISD::BITCAST, dl, VT,
6380 getTargetShuffleNode(X86ISD::MOVLHPS, dl, MVT::v4f32,
6381 DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, V1),
6382 DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, V2), DAG));
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006383}
6384
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00006385static
6386SDValue getMOVHighToLow(SDValue &Op, DebugLoc &dl, SelectionDAG &DAG) {
6387 SDValue V1 = Op.getOperand(0);
6388 SDValue V2 = Op.getOperand(1);
6389 EVT VT = Op.getValueType();
6390
6391 assert((VT == MVT::v4i32 || VT == MVT::v4f32) &&
6392 "unsupported shuffle type");
6393
6394 if (V2.getOpcode() == ISD::UNDEF)
6395 V2 = V1;
6396
6397 // v4i32 or v4f32
6398 return getTargetShuffleNode(X86ISD::MOVHLPS, dl, VT, V1, V2, DAG);
6399}
6400
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006401static
Craig Topper1accb7e2012-01-10 06:54:16 +00006402SDValue getMOVLP(SDValue &Op, DebugLoc &dl, SelectionDAG &DAG, bool HasSSE2) {
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006403 SDValue V1 = Op.getOperand(0);
6404 SDValue V2 = Op.getOperand(1);
6405 EVT VT = Op.getValueType();
6406 unsigned NumElems = VT.getVectorNumElements();
6407
6408 // Use MOVLPS and MOVLPD in case V1 or V2 are loads. During isel, the second
6409 // operand of these instructions is only memory, so check if there's a
6410 // potencial load folding here, otherwise use SHUFPS or MOVSD to match the
6411 // same masks.
6412 bool CanFoldLoad = false;
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006413
Bruno Cardoso Lopesd00bfe12010-09-02 02:35:51 +00006414 // Trivial case, when V2 comes from a load.
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00006415 if (MayFoldVectorLoad(V2))
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006416 CanFoldLoad = true;
6417
6418 // When V1 is a load, it can be folded later into a store in isel, example:
6419 // (store (v4f32 (X86Movlps (load addr:$src1), VR128:$src2)), addr:$src1)
6420 // turns into:
6421 // (MOVLPSmr addr:$src1, VR128:$src2)
6422 // So, recognize this potential and also use MOVLPS or MOVLPD
Evan Cheng7bc389b2011-11-08 00:31:58 +00006423 else if (MayFoldVectorLoad(V1) && MayFoldIntoStore(Op))
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006424 CanFoldLoad = true;
6425
Dan Gohman65fd6562011-11-03 21:49:52 +00006426 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006427 if (CanFoldLoad) {
Craig Topper1accb7e2012-01-10 06:54:16 +00006428 if (HasSSE2 && NumElems == 2)
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006429 return getTargetShuffleNode(X86ISD::MOVLPD, dl, VT, V1, V2, DAG);
6430
6431 if (NumElems == 4)
Benjamin Kramerd9b0b022012-06-02 10:20:22 +00006432 // If we don't care about the second element, proceed to use movss.
Dan Gohman65fd6562011-11-03 21:49:52 +00006433 if (SVOp->getMaskElt(1) != -1)
6434 return getTargetShuffleNode(X86ISD::MOVLPS, dl, VT, V1, V2, DAG);
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006435 }
6436
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006437 // movl and movlp will both match v2i64, but v2i64 is never matched by
6438 // movl earlier because we make it strict to avoid messing with the movlp load
6439 // folding logic (see the code above getMOVLP call). Match it here then,
6440 // this is horrible, but will stay like this until we move all shuffle
6441 // matching to x86 specific nodes. Note that for the 1st condition all
6442 // types are matched with movsd.
Craig Topper1accb7e2012-01-10 06:54:16 +00006443 if (HasSSE2) {
Bruno Cardoso Lopes5ca0d142011-09-14 02:36:14 +00006444 // FIXME: isMOVLMask should be checked and matched before getMOVLP,
6445 // as to remove this logic from here, as much as possible
Craig Topper5aaffa82012-02-19 02:53:47 +00006446 if (NumElems == 2 || !isMOVLMask(SVOp->getMask(), VT))
Bruno Cardoso Lopes57d6a5e2011-08-31 03:04:20 +00006447 return getTargetShuffleNode(X86ISD::MOVSD, dl, VT, V1, V2, DAG);
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006448 return getTargetShuffleNode(X86ISD::MOVSS, dl, VT, V1, V2, DAG);
Bruno Cardoso Lopes57d6a5e2011-08-31 03:04:20 +00006449 }
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006450
6451 assert(VT != MVT::v4i32 && "unsupported shuffle type");
6452
6453 // Invert the operand order and use SHUFPS to match it.
Craig Topperb3982da2011-12-31 23:50:21 +00006454 return getTargetShuffleNode(X86ISD::SHUFP, dl, VT, V2, V1,
Craig Topper5aaffa82012-02-19 02:53:47 +00006455 getShuffleSHUFImmediate(SVOp), DAG);
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006456}
6457
Nadav Rotem154819d2012-04-09 07:45:58 +00006458SDValue
6459X86TargetLowering::NormalizeVectorShuffle(SDValue Op, SelectionDAG &DAG) const {
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006460 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
6461 EVT VT = Op.getValueType();
6462 DebugLoc dl = Op.getDebugLoc();
6463 SDValue V1 = Op.getOperand(0);
6464 SDValue V2 = Op.getOperand(1);
6465
6466 if (isZeroShuffle(SVOp))
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00006467 return getZeroVector(VT, Subtarget, DAG, dl);
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006468
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006469 // Handle splat operations
6470 if (SVOp->isSplat()) {
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00006471 unsigned NumElem = VT.getVectorNumElements();
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00006472 int Size = VT.getSizeInBits();
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006473
Bruno Cardoso Lopes0e6d2302011-08-17 02:29:19 +00006474 // Use vbroadcast whenever the splat comes from a foldable load
Nadav Rotem154819d2012-04-09 07:45:58 +00006475 SDValue Broadcast = LowerVectorBroadcast(Op, DAG);
Nadav Rotem9d68b062012-04-08 12:54:54 +00006476 if (Broadcast.getNode())
6477 return Broadcast;
Bruno Cardoso Lopes0e6d2302011-08-17 02:29:19 +00006478
Bruno Cardoso Lopes6a32adc2011-07-25 23:05:25 +00006479 // Handle splats by matching through known shuffle masks
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00006480 if ((Size == 128 && NumElem <= 4) ||
6481 (Size == 256 && NumElem < 8))
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006482 return SDValue();
6483
Bruno Cardoso Lopes8a5b2622011-08-17 02:29:13 +00006484 // All remaning splats are promoted to target supported vector shuffles.
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006485 return PromoteSplat(SVOp, DAG);
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006486 }
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006487
6488 // If the shuffle can be profitably rewritten as a narrower shuffle, then
6489 // do it!
Craig Topperf3640d72012-05-04 04:44:49 +00006490 if (VT == MVT::v8i16 || VT == MVT::v16i8 ||
6491 VT == MVT::v16i16 || VT == MVT::v32i8) {
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006492 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, dl);
6493 if (NewOp.getNode())
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006494 return DAG.getNode(ISD::BITCAST, dl, VT, NewOp);
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006495 } else if ((VT == MVT::v4i32 ||
Craig Topper1accb7e2012-01-10 06:54:16 +00006496 (VT == MVT::v4f32 && Subtarget->hasSSE2()))) {
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006497 // FIXME: Figure out a cleaner way to do this.
6498 // Try to make use of movq to zero out the top part.
6499 if (ISD::isBuildVectorAllZeros(V2.getNode())) {
6500 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, dl);
6501 if (NewOp.getNode()) {
Craig Topper5aaffa82012-02-19 02:53:47 +00006502 EVT NewVT = NewOp.getValueType();
6503 if (isCommutedMOVLMask(cast<ShuffleVectorSDNode>(NewOp)->getMask(),
6504 NewVT, true, false))
6505 return getVZextMovL(VT, NewVT, NewOp.getOperand(0),
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006506 DAG, Subtarget, dl);
6507 }
6508 } else if (ISD::isBuildVectorAllZeros(V1.getNode())) {
6509 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, dl);
Craig Topper5aaffa82012-02-19 02:53:47 +00006510 if (NewOp.getNode()) {
6511 EVT NewVT = NewOp.getValueType();
6512 if (isMOVLMask(cast<ShuffleVectorSDNode>(NewOp)->getMask(), NewVT))
6513 return getVZextMovL(VT, NewVT, NewOp.getOperand(1),
6514 DAG, Subtarget, dl);
6515 }
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006516 }
6517 }
6518 return SDValue();
6519}
6520
Dan Gohman475871a2008-07-27 21:46:04 +00006521SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006522X86TargetLowering::LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) const {
Nate Begeman9008ca62009-04-27 18:41:29 +00006523 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Dan Gohman475871a2008-07-27 21:46:04 +00006524 SDValue V1 = Op.getOperand(0);
6525 SDValue V2 = Op.getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +00006526 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006527 DebugLoc dl = Op.getDebugLoc();
Nate Begeman9008ca62009-04-27 18:41:29 +00006528 unsigned NumElems = VT.getVectorNumElements();
Elena Demikhovsky16db7102012-01-12 20:33:10 +00006529 bool V1IsUndef = V1.getOpcode() == ISD::UNDEF;
Evan Cheng0db9fe62006-04-25 20:13:52 +00006530 bool V2IsUndef = V2.getOpcode() == ISD::UNDEF;
Evan Chengd9b8e402006-10-16 06:36:00 +00006531 bool V1IsSplat = false;
6532 bool V2IsSplat = false;
Craig Topper1accb7e2012-01-10 06:54:16 +00006533 bool HasSSE2 = Subtarget->hasSSE2();
Craig Topperbeabc6c2011-12-05 06:56:46 +00006534 bool HasAVX = Subtarget->hasAVX();
Craig Topper6347e862011-11-21 06:57:39 +00006535 bool HasAVX2 = Subtarget->hasAVX2();
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006536 MachineFunction &MF = DAG.getMachineFunction();
6537 bool OptForSize = MF.getFunction()->hasFnAttr(Attribute::OptimizeForSize);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006538
Craig Topper3426a3e2011-11-14 06:46:21 +00006539 assert(VT.getSizeInBits() != 64 && "Can't lower MMX shuffles");
Bruno Cardoso Lopes58277b12010-09-07 18:41:45 +00006540
Elena Demikhovsky16db7102012-01-12 20:33:10 +00006541 if (V1IsUndef && V2IsUndef)
6542 return DAG.getUNDEF(VT);
6543
6544 assert(!V1IsUndef && "Op 1 of shuffle should not be undef");
Craig Topper38034c52011-11-26 22:55:48 +00006545
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006546 // Vector shuffle lowering takes 3 steps:
6547 //
6548 // 1) Normalize the input vectors. Here splats, zeroed vectors, profitable
6549 // narrowing and commutation of operands should be handled.
6550 // 2) Matching of shuffles with known shuffle masks to x86 target specific
6551 // shuffle nodes.
6552 // 3) Rewriting of unmatched masks into new generic shuffle operations,
6553 // so the shuffle can be broken into other shuffles and the legalizer can
6554 // try the lowering again.
6555 //
Craig Topper3426a3e2011-11-14 06:46:21 +00006556 // The general idea is that no vector_shuffle operation should be left to
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006557 // be matched during isel, all of them must be converted to a target specific
6558 // node here.
Bruno Cardoso Lopes0d1340b2010-09-07 20:20:27 +00006559
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006560 // Normalize the input vectors. Here splats, zeroed vectors, profitable
6561 // narrowing and commutation of operands should be handled. The actual code
6562 // doesn't include all of those, work in progress...
Nadav Rotem154819d2012-04-09 07:45:58 +00006563 SDValue NewOp = NormalizeVectorShuffle(Op, DAG);
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006564 if (NewOp.getNode())
6565 return NewOp;
Eric Christopherfd179292009-08-27 18:07:15 +00006566
Craig Topper5aaffa82012-02-19 02:53:47 +00006567 SmallVector<int, 8> M(SVOp->getMask().begin(), SVOp->getMask().end());
6568
Bruno Cardoso Lopesa22c8452010-09-04 00:39:43 +00006569 // NOTE: isPSHUFDMask can also match both masks below (unpckl_undef and
6570 // unpckh_undef). Only use pshufd if speed is more important than size.
Craig Topper5aaffa82012-02-19 02:53:47 +00006571 if (OptForSize && isUNPCKL_v_undef_Mask(M, VT, HasAVX2))
Craig Topper34671b82011-12-06 08:21:25 +00006572 return getTargetShuffleNode(X86ISD::UNPCKL, dl, VT, V1, V1, DAG);
Craig Topper5aaffa82012-02-19 02:53:47 +00006573 if (OptForSize && isUNPCKH_v_undef_Mask(M, VT, HasAVX2))
Craig Topper34671b82011-12-06 08:21:25 +00006574 return getTargetShuffleNode(X86ISD::UNPCKH, dl, VT, V1, V1, DAG);
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00006575
Craig Topperdd637ae2012-02-19 05:41:45 +00006576 if (isMOVDDUPMask(M, VT) && Subtarget->hasSSE3() &&
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006577 V2IsUndef && RelaxedMayFoldVectorLoad(V1))
Evan Cheng835580f2010-10-07 20:50:20 +00006578 return getMOVDDup(Op, dl, V1, DAG);
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006579
Craig Topperdd637ae2012-02-19 05:41:45 +00006580 if (isMOVHLPS_v_undef_Mask(M, VT))
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006581 return getMOVHighToLow(Op, dl, DAG);
6582
6583 // Use to match splats
Craig Topper5aaffa82012-02-19 02:53:47 +00006584 if (HasSSE2 && isUNPCKHMask(M, VT, HasAVX2) && V2IsUndef &&
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006585 (VT == MVT::v2f64 || VT == MVT::v2i64))
Craig Topper34671b82011-12-06 08:21:25 +00006586 return getTargetShuffleNode(X86ISD::UNPCKH, dl, VT, V1, V1, DAG);
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006587
Craig Topper5aaffa82012-02-19 02:53:47 +00006588 if (isPSHUFDMask(M, VT)) {
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006589 // The actual implementation will match the mask in the if above and then
6590 // during isel it can match several different instructions, not only pshufd
6591 // as its name says, sad but true, emulate the behavior for now...
Craig Topperdd637ae2012-02-19 05:41:45 +00006592 if (isMOVDDUPMask(M, VT) && ((VT == MVT::v4f32 || VT == MVT::v2i64)))
6593 return getTargetShuffleNode(X86ISD::MOVLHPS, dl, VT, V1, V1, DAG);
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006594
Craig Topper5aaffa82012-02-19 02:53:47 +00006595 unsigned TargetMask = getShuffleSHUFImmediate(SVOp);
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006596
Craig Topperdbd98a42012-02-07 06:28:42 +00006597 if (HasAVX && (VT == MVT::v4f32 || VT == MVT::v2f64))
6598 return getTargetShuffleNode(X86ISD::VPERMILP, dl, VT, V1, TargetMask, DAG);
6599
Craig Topper1accb7e2012-01-10 06:54:16 +00006600 if (HasSSE2 && (VT == MVT::v4f32 || VT == MVT::v4i32))
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006601 return getTargetShuffleNode(X86ISD::PSHUFD, dl, VT, V1, TargetMask, DAG);
6602
Craig Topperb3982da2011-12-31 23:50:21 +00006603 return getTargetShuffleNode(X86ISD::SHUFP, dl, VT, V1, V1,
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00006604 TargetMask, DAG);
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006605 }
Eric Christopherfd179292009-08-27 18:07:15 +00006606
Evan Chengf26ffe92008-05-29 08:22:04 +00006607 // Check if this can be converted into a logical shift.
6608 bool isLeft = false;
6609 unsigned ShAmt = 0;
Dan Gohman475871a2008-07-27 21:46:04 +00006610 SDValue ShVal;
Craig Topper1accb7e2012-01-10 06:54:16 +00006611 bool isShift = HasSSE2 && isVectorShift(SVOp, DAG, isLeft, ShVal, ShAmt);
Evan Chengf26ffe92008-05-29 08:22:04 +00006612 if (isShift && ShVal.hasOneUse()) {
Scott Michelfdc40a02009-02-17 22:15:04 +00006613 // If the shifted value has multiple uses, it may be cheaper to use
Evan Chengf26ffe92008-05-29 08:22:04 +00006614 // v_set0 + movlhps or movhlps, etc.
Dan Gohman8a55ce42009-09-23 21:02:20 +00006615 EVT EltVT = VT.getVectorElementType();
6616 ShAmt *= EltVT.getSizeInBits();
Dale Johannesenace16102009-02-03 19:33:06 +00006617 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00006618 }
Eric Christopherfd179292009-08-27 18:07:15 +00006619
Craig Topper5aaffa82012-02-19 02:53:47 +00006620 if (isMOVLMask(M, VT)) {
Gabor Greifba36cb52008-08-28 21:40:38 +00006621 if (ISD::isBuildVectorAllZeros(V1.getNode()))
Dale Johannesenace16102009-02-03 19:33:06 +00006622 return getVZextMovL(VT, VT, V2, DAG, Subtarget, dl);
Craig Topperdd637ae2012-02-19 05:41:45 +00006623 if (!isMOVLPMask(M, VT)) {
Craig Topper1accb7e2012-01-10 06:54:16 +00006624 if (HasSSE2 && (VT == MVT::v2i64 || VT == MVT::v2f64))
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00006625 return getTargetShuffleNode(X86ISD::MOVSD, dl, VT, V1, V2, DAG);
6626
Bruno Cardoso Lopes4783a3e2010-09-01 22:59:03 +00006627 if (VT == MVT::v4i32 || VT == MVT::v4f32)
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00006628 return getTargetShuffleNode(X86ISD::MOVSS, dl, VT, V1, V2, DAG);
6629 }
Evan Cheng7e2ff772008-05-08 00:57:18 +00006630 }
Eric Christopherfd179292009-08-27 18:07:15 +00006631
Nate Begeman9008ca62009-04-27 18:41:29 +00006632 // FIXME: fold these into legal mask.
Craig Topperdd637ae2012-02-19 05:41:45 +00006633 if (isMOVLHPSMask(M, VT) && !isUNPCKLMask(M, VT, HasAVX2))
Craig Topper1accb7e2012-01-10 06:54:16 +00006634 return getMOVLowToHigh(Op, dl, DAG, HasSSE2);
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006635
Craig Topperdd637ae2012-02-19 05:41:45 +00006636 if (isMOVHLPSMask(M, VT))
Dale Johannesen0488fb62010-09-30 23:57:10 +00006637 return getMOVHighToLow(Op, dl, DAG);
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00006638
Craig Topperdd637ae2012-02-19 05:41:45 +00006639 if (V2IsUndef && isMOVSHDUPMask(M, VT, Subtarget))
Dale Johannesen0488fb62010-09-30 23:57:10 +00006640 return getTargetShuffleNode(X86ISD::MOVSHDUP, dl, VT, V1, DAG);
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00006641
Craig Topperdd637ae2012-02-19 05:41:45 +00006642 if (V2IsUndef && isMOVSLDUPMask(M, VT, Subtarget))
Dale Johannesen0488fb62010-09-30 23:57:10 +00006643 return getTargetShuffleNode(X86ISD::MOVSLDUP, dl, VT, V1, DAG);
Bruno Cardoso Lopes013bb3d2010-08-31 22:35:05 +00006644
Craig Topperdd637ae2012-02-19 05:41:45 +00006645 if (isMOVLPMask(M, VT))
Craig Topper1accb7e2012-01-10 06:54:16 +00006646 return getMOVLP(Op, dl, DAG, HasSSE2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006647
Craig Topperdd637ae2012-02-19 05:41:45 +00006648 if (ShouldXformToMOVHLPS(M, VT) ||
6649 ShouldXformToMOVLP(V1.getNode(), V2.getNode(), M, VT))
Nate Begeman9008ca62009-04-27 18:41:29 +00006650 return CommuteVectorShuffle(SVOp, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006651
Evan Chengf26ffe92008-05-29 08:22:04 +00006652 if (isShift) {
Craig Toppered2e13d2012-01-22 19:15:14 +00006653 // No better options. Use a vshldq / vsrldq.
Dan Gohman8a55ce42009-09-23 21:02:20 +00006654 EVT EltVT = VT.getVectorElementType();
6655 ShAmt *= EltVT.getSizeInBits();
Dale Johannesenace16102009-02-03 19:33:06 +00006656 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00006657 }
Eric Christopherfd179292009-08-27 18:07:15 +00006658
Evan Cheng9eca5e82006-10-25 21:49:50 +00006659 bool Commuted = false;
Chris Lattner8a594482007-11-25 00:24:49 +00006660 // FIXME: This should also accept a bitcast of a splat? Be careful, not
6661 // 1,1,1,1 -> v8i16 though.
Gabor Greifba36cb52008-08-28 21:40:38 +00006662 V1IsSplat = isSplatVector(V1.getNode());
6663 V2IsSplat = isSplatVector(V2.getNode());
Scott Michelfdc40a02009-02-17 22:15:04 +00006664
Chris Lattner8a594482007-11-25 00:24:49 +00006665 // Canonicalize the splat or undef, if present, to be on the RHS.
Craig Topper39a9e482012-02-11 06:24:48 +00006666 if (!V2IsUndef && V1IsSplat && !V2IsSplat) {
6667 CommuteVectorShuffleMask(M, NumElems);
6668 std::swap(V1, V2);
Evan Cheng9bbbb982006-10-25 20:48:19 +00006669 std::swap(V1IsSplat, V2IsSplat);
Evan Cheng9eca5e82006-10-25 21:49:50 +00006670 Commuted = true;
Evan Cheng9bbbb982006-10-25 20:48:19 +00006671 }
6672
Craig Topperbeabc6c2011-12-05 06:56:46 +00006673 if (isCommutedMOVLMask(M, VT, V2IsSplat, V2IsUndef)) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006674 // Shuffling low element of v1 into undef, just return v1.
Eric Christopherfd179292009-08-27 18:07:15 +00006675 if (V2IsUndef)
Nate Begeman9008ca62009-04-27 18:41:29 +00006676 return V1;
6677 // If V2 is a splat, the mask may be malformed such as <4,3,3,3>, which
6678 // the instruction selector will not match, so get a canonical MOVL with
6679 // swapped operands to undo the commute.
6680 return getMOVL(DAG, dl, VT, V2, V1);
Evan Chengd9b8e402006-10-16 06:36:00 +00006681 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00006682
Craig Topperbeabc6c2011-12-05 06:56:46 +00006683 if (isUNPCKLMask(M, VT, HasAVX2))
Craig Topper34671b82011-12-06 08:21:25 +00006684 return getTargetShuffleNode(X86ISD::UNPCKL, dl, VT, V1, V2, DAG);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00006685
Craig Topperbeabc6c2011-12-05 06:56:46 +00006686 if (isUNPCKHMask(M, VT, HasAVX2))
Craig Topper34671b82011-12-06 08:21:25 +00006687 return getTargetShuffleNode(X86ISD::UNPCKH, dl, VT, V1, V2, DAG);
Evan Chenge1113032006-10-04 18:33:38 +00006688
Evan Cheng9bbbb982006-10-25 20:48:19 +00006689 if (V2IsSplat) {
6690 // Normalize mask so all entries that point to V2 points to its first
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00006691 // element then try to match unpck{h|l} again. If match, return a
Craig Topper39a9e482012-02-11 06:24:48 +00006692 // new vector_shuffle with the corrected mask.p
6693 SmallVector<int, 8> NewMask(M.begin(), M.end());
6694 NormalizeMask(NewMask, NumElems);
Craig Topper69947b92012-04-23 06:57:04 +00006695 if (isUNPCKLMask(NewMask, VT, HasAVX2, true))
Craig Topper39a9e482012-02-11 06:24:48 +00006696 return getTargetShuffleNode(X86ISD::UNPCKL, dl, VT, V1, V2, DAG);
Craig Topper69947b92012-04-23 06:57:04 +00006697 if (isUNPCKHMask(NewMask, VT, HasAVX2, true))
Craig Topper39a9e482012-02-11 06:24:48 +00006698 return getTargetShuffleNode(X86ISD::UNPCKH, dl, VT, V1, V2, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006699 }
6700
Evan Cheng9eca5e82006-10-25 21:49:50 +00006701 if (Commuted) {
6702 // Commute is back and try unpck* again.
Nate Begeman9008ca62009-04-27 18:41:29 +00006703 // FIXME: this seems wrong.
Craig Topper39a9e482012-02-11 06:24:48 +00006704 CommuteVectorShuffleMask(M, NumElems);
6705 std::swap(V1, V2);
6706 std::swap(V1IsSplat, V2IsSplat);
6707 Commuted = false;
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00006708
Craig Topper39a9e482012-02-11 06:24:48 +00006709 if (isUNPCKLMask(M, VT, HasAVX2))
6710 return getTargetShuffleNode(X86ISD::UNPCKL, dl, VT, V1, V2, DAG);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00006711
Craig Topper39a9e482012-02-11 06:24:48 +00006712 if (isUNPCKHMask(M, VT, HasAVX2))
6713 return getTargetShuffleNode(X86ISD::UNPCKH, dl, VT, V1, V2, DAG);
Evan Cheng9eca5e82006-10-25 21:49:50 +00006714 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00006715
Nate Begeman9008ca62009-04-27 18:41:29 +00006716 // Normalize the node to match x86 shuffle ops if needed
Craig Topper1a7700a2012-01-19 08:19:12 +00006717 if (!V2IsUndef && (isSHUFPMask(M, VT, HasAVX, /* Commuted */ true)))
Nate Begeman9008ca62009-04-27 18:41:29 +00006718 return CommuteVectorShuffle(SVOp, DAG);
6719
Bruno Cardoso Lopes7256e222010-09-03 23:24:06 +00006720 // The checks below are all present in isShuffleMaskLegal, but they are
6721 // inlined here right now to enable us to directly emit target specific
6722 // nodes, and remove one by one until they don't return Op anymore.
Bruno Cardoso Lopes7256e222010-09-03 23:24:06 +00006723
Craig Topper0e2037b2012-01-20 05:53:00 +00006724 if (isPALIGNRMask(M, VT, Subtarget))
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00006725 return getTargetShuffleNode(X86ISD::PALIGN, dl, VT, V1, V2,
Craig Topperd93e4c32011-12-11 19:12:35 +00006726 getShufflePALIGNRImmediate(SVOp),
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00006727 DAG);
6728
Bruno Cardoso Lopesc800c0d2010-09-04 02:02:14 +00006729 if (ShuffleVectorSDNode::isSplatMask(&M[0], VT) &&
6730 SVOp->getSplatIndex() == 0 && V2IsUndef) {
Craig Topperbeabc6c2011-12-05 06:56:46 +00006731 if (VT == MVT::v2f64 || VT == MVT::v2i64)
Craig Topper34671b82011-12-06 08:21:25 +00006732 return getTargetShuffleNode(X86ISD::UNPCKL, dl, VT, V1, V1, DAG);
Bruno Cardoso Lopesc800c0d2010-09-04 02:02:14 +00006733 }
6734
Craig Toppera9a568a2012-05-02 08:03:44 +00006735 if (isPSHUFHWMask(M, VT, HasAVX2))
Bruno Cardoso Lopesbbfc3102010-09-04 01:36:45 +00006736 return getTargetShuffleNode(X86ISD::PSHUFHW, dl, VT, V1,
Craig Topper5aaffa82012-02-19 02:53:47 +00006737 getShufflePSHUFHWImmediate(SVOp),
Bruno Cardoso Lopesbbfc3102010-09-04 01:36:45 +00006738 DAG);
6739
Craig Toppera9a568a2012-05-02 08:03:44 +00006740 if (isPSHUFLWMask(M, VT, HasAVX2))
Bruno Cardoso Lopesbbfc3102010-09-04 01:36:45 +00006741 return getTargetShuffleNode(X86ISD::PSHUFLW, dl, VT, V1,
Craig Topper5aaffa82012-02-19 02:53:47 +00006742 getShufflePSHUFLWImmediate(SVOp),
Bruno Cardoso Lopesbbfc3102010-09-04 01:36:45 +00006743 DAG);
6744
Craig Topper1a7700a2012-01-19 08:19:12 +00006745 if (isSHUFPMask(M, VT, HasAVX))
Craig Topperb3982da2011-12-31 23:50:21 +00006746 return getTargetShuffleNode(X86ISD::SHUFP, dl, VT, V1, V2,
Craig Topper5aaffa82012-02-19 02:53:47 +00006747 getShuffleSHUFImmediate(SVOp), DAG);
Bruno Cardoso Lopes4c827f52010-09-04 01:22:57 +00006748
Craig Topper94438ba2011-12-16 08:06:31 +00006749 if (isUNPCKL_v_undef_Mask(M, VT, HasAVX2))
Craig Topper34671b82011-12-06 08:21:25 +00006750 return getTargetShuffleNode(X86ISD::UNPCKL, dl, VT, V1, V1, DAG);
Craig Topper94438ba2011-12-16 08:06:31 +00006751 if (isUNPCKH_v_undef_Mask(M, VT, HasAVX2))
Craig Topper34671b82011-12-06 08:21:25 +00006752 return getTargetShuffleNode(X86ISD::UNPCKH, dl, VT, V1, V1, DAG);
Bruno Cardoso Lopesa22c8452010-09-04 00:39:43 +00006753
Bruno Cardoso Lopesd0888342011-07-22 00:14:56 +00006754 //===--------------------------------------------------------------------===//
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00006755 // Generate target specific nodes for 128 or 256-bit shuffles only
6756 // supported in the AVX instruction set.
6757 //
Bruno Cardoso Lopesd0888342011-07-22 00:14:56 +00006758
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00006759 // Handle VMOVDDUPY permutations
Craig Topperbeabc6c2011-12-05 06:56:46 +00006760 if (V2IsUndef && isMOVDDUPYMask(M, VT, HasAVX))
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00006761 return getTargetShuffleNode(X86ISD::MOVDDUP, dl, VT, V1, DAG);
6762
Craig Topper70b883b2011-11-28 10:14:51 +00006763 // Handle VPERMILPS/D* permutations
Craig Topperdbd98a42012-02-07 06:28:42 +00006764 if (isVPERMILPMask(M, VT, HasAVX)) {
6765 if (HasAVX2 && VT == MVT::v8i32)
6766 return getTargetShuffleNode(X86ISD::PSHUFD, dl, VT, V1,
Craig Topper5aaffa82012-02-19 02:53:47 +00006767 getShuffleSHUFImmediate(SVOp), DAG);
Craig Topper316cd2a2011-11-30 06:25:25 +00006768 return getTargetShuffleNode(X86ISD::VPERMILP, dl, VT, V1,
Craig Topper5aaffa82012-02-19 02:53:47 +00006769 getShuffleSHUFImmediate(SVOp), DAG);
Craig Topperdbd98a42012-02-07 06:28:42 +00006770 }
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00006771
Craig Topper70b883b2011-11-28 10:14:51 +00006772 // Handle VPERM2F128/VPERM2I128 permutations
Craig Topperbeabc6c2011-12-05 06:56:46 +00006773 if (isVPERM2X128Mask(M, VT, HasAVX))
Craig Topperec24e612011-11-30 07:47:51 +00006774 return getTargetShuffleNode(X86ISD::VPERM2X128, dl, VT, V1,
Craig Topper70b883b2011-11-28 10:14:51 +00006775 V2, getShuffleVPERM2X128Immediate(SVOp), DAG);
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00006776
Craig Topper1842ba02012-04-23 06:38:28 +00006777 SDValue BlendOp = LowerVECTOR_SHUFFLEtoBlend(SVOp, Subtarget, DAG);
Nadav Roteme80aa7c2012-04-09 08:33:21 +00006778 if (BlendOp.getNode())
6779 return BlendOp;
Craig Topper095c5282012-04-15 23:48:57 +00006780
Elena Demikhovsky73c504a2012-04-15 11:18:59 +00006781 if (V2IsUndef && HasAVX2 && (VT == MVT::v8i32 || VT == MVT::v8f32)) {
Craig Topper095c5282012-04-15 23:48:57 +00006782 SmallVector<SDValue, 8> permclMask;
Elena Demikhovsky73c504a2012-04-15 11:18:59 +00006783 for (unsigned i = 0; i != 8; ++i) {
Craig Topper095c5282012-04-15 23:48:57 +00006784 permclMask.push_back(DAG.getConstant((M[i]>=0) ? M[i] : 0, MVT::i32));
Elena Demikhovsky73c504a2012-04-15 11:18:59 +00006785 }
Craig Topper92040742012-04-16 06:43:40 +00006786 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v8i32,
6787 &permclMask[0], 8);
6788 // Bitcast is for VPERMPS since mask is v8i32 but node takes v8f32
Craig Topper8325c112012-04-16 00:41:45 +00006789 return DAG.getNode(X86ISD::VPERMV, dl, VT,
Craig Topper92040742012-04-16 06:43:40 +00006790 DAG.getNode(ISD::BITCAST, dl, VT, Mask), V1);
Elena Demikhovsky73c504a2012-04-15 11:18:59 +00006791 }
Craig Topper095c5282012-04-15 23:48:57 +00006792
Craig Topper8325c112012-04-16 00:41:45 +00006793 if (V2IsUndef && HasAVX2 && (VT == MVT::v4i64 || VT == MVT::v4f64))
6794 return getTargetShuffleNode(X86ISD::VPERMI, dl, VT, V1,
Elena Demikhovsky73c504a2012-04-15 11:18:59 +00006795 getShuffleCLImmediate(SVOp), DAG);
6796
Nadav Roteme80aa7c2012-04-09 08:33:21 +00006797
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00006798 //===--------------------------------------------------------------------===//
6799 // Since no target specific shuffle was selected for this generic one,
6800 // lower it into other known shuffles. FIXME: this isn't true yet, but
6801 // this is the plan.
6802 //
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00006803
Bruno Cardoso Lopes9b4ad122011-07-27 00:56:37 +00006804 // Handle v8i16 specifically since SSE can do byte extraction and insertion.
6805 if (VT == MVT::v8i16) {
6806 SDValue NewOp = LowerVECTOR_SHUFFLEv8i16(Op, DAG);
6807 if (NewOp.getNode())
6808 return NewOp;
6809 }
6810
6811 if (VT == MVT::v16i8) {
6812 SDValue NewOp = LowerVECTOR_SHUFFLEv16i8(SVOp, DAG, *this);
6813 if (NewOp.getNode())
6814 return NewOp;
6815 }
6816
6817 // Handle all 128-bit wide vectors with 4 elements, and match them with
6818 // several different shuffle types.
Craig Topper7a9a28b2012-08-12 02:23:29 +00006819 if (NumElems == 4 && VT.is128BitVector())
Bruno Cardoso Lopes9b4ad122011-07-27 00:56:37 +00006820 return LowerVECTOR_SHUFFLE_128v4(SVOp, DAG);
6821
Bruno Cardoso Lopesd0888342011-07-22 00:14:56 +00006822 // Handle general 256-bit shuffles
6823 if (VT.is256BitVector())
6824 return LowerVECTOR_SHUFFLE_256(SVOp, DAG);
6825
Dan Gohman475871a2008-07-27 21:46:04 +00006826 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006827}
6828
Dan Gohman475871a2008-07-27 21:46:04 +00006829SDValue
6830X86TargetLowering::LowerEXTRACT_VECTOR_ELT_SSE4(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00006831 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00006832 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006833 DebugLoc dl = Op.getDebugLoc();
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00006834
Craig Topper7a9a28b2012-08-12 02:23:29 +00006835 if (!Op.getOperand(0).getValueType().is128BitVector())
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00006836 return SDValue();
6837
Duncan Sands83ec4b62008-06-06 12:08:01 +00006838 if (VT.getSizeInBits() == 8) {
Owen Anderson825b72b2009-08-11 20:47:22 +00006839 SDValue Extract = DAG.getNode(X86ISD::PEXTRB, dl, MVT::i32,
Nate Begeman14d12ca2008-02-11 04:19:36 +00006840 Op.getOperand(0), Op.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00006841 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Extract,
Nate Begeman14d12ca2008-02-11 04:19:36 +00006842 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00006843 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Craig Topper69947b92012-04-23 06:57:04 +00006844 }
6845
6846 if (VT.getSizeInBits() == 16) {
Evan Cheng52ceafa2009-01-02 05:29:08 +00006847 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
6848 // If Idx is 0, it's cheaper to do a move instead of a pextrw.
6849 if (Idx == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00006850 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i16,
6851 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006852 DAG.getNode(ISD::BITCAST, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00006853 MVT::v4i32,
Evan Cheng52ceafa2009-01-02 05:29:08 +00006854 Op.getOperand(0)),
6855 Op.getOperand(1)));
Owen Anderson825b72b2009-08-11 20:47:22 +00006856 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, dl, MVT::i32,
Nate Begeman14d12ca2008-02-11 04:19:36 +00006857 Op.getOperand(0), Op.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00006858 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Extract,
Nate Begeman14d12ca2008-02-11 04:19:36 +00006859 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00006860 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Craig Topper69947b92012-04-23 06:57:04 +00006861 }
6862
6863 if (VT == MVT::f32) {
Evan Cheng62a3f152008-03-24 21:52:23 +00006864 // EXTRACTPS outputs to a GPR32 register which will require a movd to copy
6865 // the result back to FR32 register. It's only worth matching if the
Dan Gohmand17cfbe2008-10-31 00:57:24 +00006866 // result has a single use which is a store or a bitcast to i32. And in
6867 // the case of a store, it's not worth it if the index is a constant 0,
6868 // because a MOVSSmr can be used instead, which is smaller and faster.
Evan Cheng62a3f152008-03-24 21:52:23 +00006869 if (!Op.hasOneUse())
Dan Gohman475871a2008-07-27 21:46:04 +00006870 return SDValue();
Gabor Greifba36cb52008-08-28 21:40:38 +00006871 SDNode *User = *Op.getNode()->use_begin();
Dan Gohmand17cfbe2008-10-31 00:57:24 +00006872 if ((User->getOpcode() != ISD::STORE ||
6873 (isa<ConstantSDNode>(Op.getOperand(1)) &&
6874 cast<ConstantSDNode>(Op.getOperand(1))->isNullValue())) &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006875 (User->getOpcode() != ISD::BITCAST ||
Owen Anderson825b72b2009-08-11 20:47:22 +00006876 User->getValueType(0) != MVT::i32))
Dan Gohman475871a2008-07-27 21:46:04 +00006877 return SDValue();
Owen Anderson825b72b2009-08-11 20:47:22 +00006878 SDValue Extract = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006879 DAG.getNode(ISD::BITCAST, dl, MVT::v4i32,
Dale Johannesenace16102009-02-03 19:33:06 +00006880 Op.getOperand(0)),
6881 Op.getOperand(1));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006882 return DAG.getNode(ISD::BITCAST, dl, MVT::f32, Extract);
Craig Topper69947b92012-04-23 06:57:04 +00006883 }
6884
6885 if (VT == MVT::i32 || VT == MVT::i64) {
Pete Coopera77214a2011-11-14 19:38:42 +00006886 // ExtractPS/pextrq works with constant index.
Mon P Wangf0fcdd82009-01-15 21:10:20 +00006887 if (isa<ConstantSDNode>(Op.getOperand(1)))
6888 return Op;
Nate Begeman14d12ca2008-02-11 04:19:36 +00006889 }
Dan Gohman475871a2008-07-27 21:46:04 +00006890 return SDValue();
Nate Begeman14d12ca2008-02-11 04:19:36 +00006891}
6892
6893
Dan Gohman475871a2008-07-27 21:46:04 +00006894SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006895X86TargetLowering::LowerEXTRACT_VECTOR_ELT(SDValue Op,
6896 SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00006897 if (!isa<ConstantSDNode>(Op.getOperand(1)))
Dan Gohman475871a2008-07-27 21:46:04 +00006898 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006899
David Greene74a579d2011-02-10 16:57:36 +00006900 SDValue Vec = Op.getOperand(0);
6901 EVT VecVT = Vec.getValueType();
6902
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00006903 // If this is a 256-bit vector result, first extract the 128-bit vector and
6904 // then extract the element from the 128-bit vector.
Craig Topper7a9a28b2012-08-12 02:23:29 +00006905 if (VecVT.is256BitVector()) {
David Greene74a579d2011-02-10 16:57:36 +00006906 DebugLoc dl = Op.getNode()->getDebugLoc();
6907 unsigned NumElems = VecVT.getVectorNumElements();
6908 SDValue Idx = Op.getOperand(1);
David Greene74a579d2011-02-10 16:57:36 +00006909 unsigned IdxVal = cast<ConstantSDNode>(Idx)->getZExtValue();
6910
6911 // Get the 128-bit vector.
Craig Topper7d1e3dc2012-04-30 05:17:10 +00006912 Vec = Extract128BitVector(Vec, IdxVal, DAG, dl);
David Greene74a579d2011-02-10 16:57:36 +00006913
Craig Topper7d1e3dc2012-04-30 05:17:10 +00006914 if (IdxVal >= NumElems/2)
6915 IdxVal -= NumElems/2;
David Greene74a579d2011-02-10 16:57:36 +00006916 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, Op.getValueType(), Vec,
Craig Topper7d1e3dc2012-04-30 05:17:10 +00006917 DAG.getConstant(IdxVal, MVT::i32));
David Greene74a579d2011-02-10 16:57:36 +00006918 }
6919
Craig Topper7a9a28b2012-08-12 02:23:29 +00006920 assert(VecVT.is128BitVector() && "Unexpected vector length");
David Greene74a579d2011-02-10 16:57:36 +00006921
Craig Topperd0a31172012-01-10 06:37:29 +00006922 if (Subtarget->hasSSE41()) {
Dan Gohman475871a2008-07-27 21:46:04 +00006923 SDValue Res = LowerEXTRACT_VECTOR_ELT_SSE4(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00006924 if (Res.getNode())
Evan Cheng62a3f152008-03-24 21:52:23 +00006925 return Res;
6926 }
Nate Begeman14d12ca2008-02-11 04:19:36 +00006927
Owen Andersone50ed302009-08-10 22:56:29 +00006928 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006929 DebugLoc dl = Op.getDebugLoc();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006930 // TODO: handle v16i8.
Duncan Sands83ec4b62008-06-06 12:08:01 +00006931 if (VT.getSizeInBits() == 16) {
Dan Gohman475871a2008-07-27 21:46:04 +00006932 SDValue Vec = Op.getOperand(0);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006933 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00006934 if (Idx == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00006935 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i16,
6936 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006937 DAG.getNode(ISD::BITCAST, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00006938 MVT::v4i32, Vec),
Evan Cheng14b32e12007-12-11 01:46:18 +00006939 Op.getOperand(1)));
Evan Cheng0db9fe62006-04-25 20:13:52 +00006940 // Transform it so it match pextrw which produces a 32-bit result.
Ken Dyck70d0ef12009-12-17 15:31:52 +00006941 EVT EltVT = MVT::i32;
Dan Gohman8a55ce42009-09-23 21:02:20 +00006942 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, dl, EltVT,
Evan Cheng0db9fe62006-04-25 20:13:52 +00006943 Op.getOperand(0), Op.getOperand(1));
Dan Gohman8a55ce42009-09-23 21:02:20 +00006944 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, EltVT, Extract,
Evan Cheng0db9fe62006-04-25 20:13:52 +00006945 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00006946 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Craig Topper69947b92012-04-23 06:57:04 +00006947 }
6948
6949 if (VT.getSizeInBits() == 32) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006950 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006951 if (Idx == 0)
6952 return Op;
Eric Christopherfd179292009-08-27 18:07:15 +00006953
Evan Cheng0db9fe62006-04-25 20:13:52 +00006954 // SHUFPS the element to the lowest double word, then movss.
Jeffrey Yasskina44defe2011-07-27 06:22:51 +00006955 int Mask[4] = { static_cast<int>(Idx), -1, -1, -1 };
Owen Andersone50ed302009-08-10 22:56:29 +00006956 EVT VVT = Op.getOperand(0).getValueType();
Eric Christopherfd179292009-08-27 18:07:15 +00006957 SDValue Vec = DAG.getVectorShuffle(VVT, dl, Op.getOperand(0),
Nate Begeman9008ca62009-04-27 18:41:29 +00006958 DAG.getUNDEF(VVT), Mask);
Dale Johannesenace16102009-02-03 19:33:06 +00006959 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, Vec,
Chris Lattner0bd48932008-01-17 07:00:52 +00006960 DAG.getIntPtrConstant(0));
Craig Topper69947b92012-04-23 06:57:04 +00006961 }
6962
6963 if (VT.getSizeInBits() == 64) {
Nate Begeman14d12ca2008-02-11 04:19:36 +00006964 // FIXME: .td only matches this for <2 x f64>, not <2 x i64> on 32b
6965 // FIXME: seems like this should be unnecessary if mov{h,l}pd were taught
6966 // to match extract_elt for f64.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006967 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006968 if (Idx == 0)
6969 return Op;
6970
6971 // UNPCKHPD the element to the lowest double word, then movsd.
6972 // Note if the lower 64 bits of the result of the UNPCKHPD is then stored
6973 // to a f64mem, the whole operation is folded into a single MOVHPDmr.
Nate Begeman9008ca62009-04-27 18:41:29 +00006974 int Mask[2] = { 1, -1 };
Owen Andersone50ed302009-08-10 22:56:29 +00006975 EVT VVT = Op.getOperand(0).getValueType();
Eric Christopherfd179292009-08-27 18:07:15 +00006976 SDValue Vec = DAG.getVectorShuffle(VVT, dl, Op.getOperand(0),
Nate Begeman9008ca62009-04-27 18:41:29 +00006977 DAG.getUNDEF(VVT), Mask);
Dale Johannesenace16102009-02-03 19:33:06 +00006978 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, Vec,
Chris Lattner0bd48932008-01-17 07:00:52 +00006979 DAG.getIntPtrConstant(0));
Evan Cheng0db9fe62006-04-25 20:13:52 +00006980 }
6981
Dan Gohman475871a2008-07-27 21:46:04 +00006982 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006983}
6984
Dan Gohman475871a2008-07-27 21:46:04 +00006985SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006986X86TargetLowering::LowerINSERT_VECTOR_ELT_SSE4(SDValue Op,
6987 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00006988 EVT VT = Op.getValueType();
Dan Gohman8a55ce42009-09-23 21:02:20 +00006989 EVT EltVT = VT.getVectorElementType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006990 DebugLoc dl = Op.getDebugLoc();
Nate Begeman14d12ca2008-02-11 04:19:36 +00006991
Dan Gohman475871a2008-07-27 21:46:04 +00006992 SDValue N0 = Op.getOperand(0);
6993 SDValue N1 = Op.getOperand(1);
6994 SDValue N2 = Op.getOperand(2);
Nate Begeman14d12ca2008-02-11 04:19:36 +00006995
Craig Topper7a9a28b2012-08-12 02:23:29 +00006996 if (!VT.is128BitVector())
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00006997 return SDValue();
6998
Dan Gohman8a55ce42009-09-23 21:02:20 +00006999 if ((EltVT.getSizeInBits() == 8 || EltVT.getSizeInBits() == 16) &&
Dan Gohmanef521f12008-08-14 22:53:18 +00007000 isa<ConstantSDNode>(N2)) {
Chris Lattner8f2b4cc2010-02-23 02:07:48 +00007001 unsigned Opc;
7002 if (VT == MVT::v8i16)
7003 Opc = X86ISD::PINSRW;
Chris Lattner8f2b4cc2010-02-23 02:07:48 +00007004 else if (VT == MVT::v16i8)
7005 Opc = X86ISD::PINSRB;
7006 else
7007 Opc = X86ISD::PINSRB;
7008
Nate Begeman14d12ca2008-02-11 04:19:36 +00007009 // Transform it so it match pinsr{b,w} which expects a GR32 as its second
7010 // argument.
Owen Anderson825b72b2009-08-11 20:47:22 +00007011 if (N1.getValueType() != MVT::i32)
7012 N1 = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, N1);
7013 if (N2.getValueType() != MVT::i32)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007014 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Dale Johannesenace16102009-02-03 19:33:06 +00007015 return DAG.getNode(Opc, dl, VT, N0, N1, N2);
Craig Topper69947b92012-04-23 06:57:04 +00007016 }
7017
7018 if (EltVT == MVT::f32 && isa<ConstantSDNode>(N2)) {
Nate Begeman14d12ca2008-02-11 04:19:36 +00007019 // Bits [7:6] of the constant are the source select. This will always be
7020 // zero here. The DAG Combiner may combine an extract_elt index into these
7021 // bits. For example (insert (extract, 3), 2) could be matched by putting
7022 // the '3' into bits [7:6] of X86ISD::INSERTPS.
Scott Michelfdc40a02009-02-17 22:15:04 +00007023 // Bits [5:4] of the constant are the destination select. This is the
Nate Begeman14d12ca2008-02-11 04:19:36 +00007024 // value of the incoming immediate.
Scott Michelfdc40a02009-02-17 22:15:04 +00007025 // Bits [3:0] of the constant are the zero mask. The DAG Combiner may
Nate Begeman14d12ca2008-02-11 04:19:36 +00007026 // combine either bitwise AND or insert of float 0.0 to set these bits.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007027 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue() << 4);
Eric Christopherfbd66872009-07-24 00:33:09 +00007028 // Create this as a scalar to vector..
Owen Anderson825b72b2009-08-11 20:47:22 +00007029 N1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4f32, N1);
Dale Johannesenace16102009-02-03 19:33:06 +00007030 return DAG.getNode(X86ISD::INSERTPS, dl, VT, N0, N1, N2);
Craig Topper69947b92012-04-23 06:57:04 +00007031 }
7032
7033 if ((EltVT == MVT::i32 || EltVT == MVT::i64) && isa<ConstantSDNode>(N2)) {
Eric Christopherfbd66872009-07-24 00:33:09 +00007034 // PINSR* works with constant index.
7035 return Op;
Nate Begeman14d12ca2008-02-11 04:19:36 +00007036 }
Dan Gohman475871a2008-07-27 21:46:04 +00007037 return SDValue();
Nate Begeman14d12ca2008-02-11 04:19:36 +00007038}
7039
Dan Gohman475871a2008-07-27 21:46:04 +00007040SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007041X86TargetLowering::LowerINSERT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00007042 EVT VT = Op.getValueType();
Dan Gohman8a55ce42009-09-23 21:02:20 +00007043 EVT EltVT = VT.getVectorElementType();
Nate Begeman14d12ca2008-02-11 04:19:36 +00007044
David Greene6b381262011-02-09 15:32:06 +00007045 DebugLoc dl = Op.getDebugLoc();
7046 SDValue N0 = Op.getOperand(0);
7047 SDValue N1 = Op.getOperand(1);
7048 SDValue N2 = Op.getOperand(2);
7049
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007050 // If this is a 256-bit vector result, first extract the 128-bit vector,
7051 // insert the element into the extracted half and then place it back.
Craig Topper7a9a28b2012-08-12 02:23:29 +00007052 if (VT.is256BitVector()) {
David Greene6b381262011-02-09 15:32:06 +00007053 if (!isa<ConstantSDNode>(N2))
7054 return SDValue();
7055
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007056 // Get the desired 128-bit vector half.
David Greene6b381262011-02-09 15:32:06 +00007057 unsigned NumElems = VT.getVectorNumElements();
7058 unsigned IdxVal = cast<ConstantSDNode>(N2)->getZExtValue();
Craig Topper7d1e3dc2012-04-30 05:17:10 +00007059 SDValue V = Extract128BitVector(N0, IdxVal, DAG, dl);
David Greene6b381262011-02-09 15:32:06 +00007060
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007061 // Insert the element into the desired half.
Craig Topper7d1e3dc2012-04-30 05:17:10 +00007062 bool Upper = IdxVal >= NumElems/2;
7063 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, V.getValueType(), V, N1,
7064 DAG.getConstant(Upper ? IdxVal-NumElems/2 : IdxVal, MVT::i32));
David Greene6b381262011-02-09 15:32:06 +00007065
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007066 // Insert the changed part back to the 256-bit vector
Craig Topper7d1e3dc2012-04-30 05:17:10 +00007067 return Insert128BitVector(N0, V, IdxVal, DAG, dl);
David Greene6b381262011-02-09 15:32:06 +00007068 }
7069
Craig Topperd0a31172012-01-10 06:37:29 +00007070 if (Subtarget->hasSSE41())
Nate Begeman14d12ca2008-02-11 04:19:36 +00007071 return LowerINSERT_VECTOR_ELT_SSE4(Op, DAG);
7072
Dan Gohman8a55ce42009-09-23 21:02:20 +00007073 if (EltVT == MVT::i8)
Dan Gohman475871a2008-07-27 21:46:04 +00007074 return SDValue();
Evan Cheng794405e2007-12-12 07:55:34 +00007075
Dan Gohman8a55ce42009-09-23 21:02:20 +00007076 if (EltVT.getSizeInBits() == 16 && isa<ConstantSDNode>(N2)) {
Evan Cheng794405e2007-12-12 07:55:34 +00007077 // Transform it so it match pinsrw which expects a 16-bit value in a GR32
7078 // as its second argument.
Owen Anderson825b72b2009-08-11 20:47:22 +00007079 if (N1.getValueType() != MVT::i32)
7080 N1 = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, N1);
7081 if (N2.getValueType() != MVT::i32)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007082 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Dale Johannesen0488fb62010-09-30 23:57:10 +00007083 return DAG.getNode(X86ISD::PINSRW, dl, VT, N0, N1, N2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007084 }
Dan Gohman475871a2008-07-27 21:46:04 +00007085 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007086}
7087
Dan Gohman475871a2008-07-27 21:46:04 +00007088SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007089X86TargetLowering::LowerSCALAR_TO_VECTOR(SDValue Op, SelectionDAG &DAG) const {
Bruno Cardoso Lopes233fa392011-07-25 23:05:16 +00007090 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007091 DebugLoc dl = Op.getDebugLoc();
David Greene2fcdfb42011-02-10 23:11:29 +00007092 EVT OpVT = Op.getValueType();
7093
Bruno Cardoso Lopes233fa392011-07-25 23:05:16 +00007094 // If this is a 256-bit vector result, first insert into a 128-bit
7095 // vector and then insert into the 256-bit vector.
Craig Topper7a9a28b2012-08-12 02:23:29 +00007096 if (!OpVT.is128BitVector()) {
Bruno Cardoso Lopes233fa392011-07-25 23:05:16 +00007097 // Insert into a 128-bit vector.
7098 EVT VT128 = EVT::getVectorVT(*Context,
7099 OpVT.getVectorElementType(),
7100 OpVT.getVectorNumElements() / 2);
7101
7102 Op = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT128, Op.getOperand(0));
7103
7104 // Insert the 128-bit vector.
Craig Topperb14940a2012-04-22 20:55:18 +00007105 return Insert128BitVector(DAG.getUNDEF(OpVT), Op, 0, DAG, dl);
Bruno Cardoso Lopes233fa392011-07-25 23:05:16 +00007106 }
7107
Craig Topperd77d2fe2012-04-29 20:22:05 +00007108 if (OpVT == MVT::v1i64 &&
Chris Lattnerf172ecd2010-07-04 23:07:25 +00007109 Op.getOperand(0).getValueType() == MVT::i64)
Owen Anderson825b72b2009-08-11 20:47:22 +00007110 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v1i64, Op.getOperand(0));
Rafael Espindoladef390a2009-08-03 02:45:34 +00007111
Owen Anderson825b72b2009-08-11 20:47:22 +00007112 SDValue AnyExt = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, Op.getOperand(0));
Craig Topper7a9a28b2012-08-12 02:23:29 +00007113 assert(OpVT.is128BitVector() && "Expected an SSE type!");
Craig Topperd77d2fe2012-04-29 20:22:05 +00007114 return DAG.getNode(ISD::BITCAST, dl, OpVT,
Dale Johannesen0488fb62010-09-30 23:57:10 +00007115 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,AnyExt));
Evan Cheng0db9fe62006-04-25 20:13:52 +00007116}
7117
David Greene91585092011-01-26 15:38:49 +00007118// Lower a node with an EXTRACT_SUBVECTOR opcode. This may result in
7119// a simple subregister reference or explicit instructions to grab
7120// upper bits of a vector.
7121SDValue
7122X86TargetLowering::LowerEXTRACT_SUBVECTOR(SDValue Op, SelectionDAG &DAG) const {
7123 if (Subtarget->hasAVX()) {
David Greenea5f26012011-02-07 19:36:54 +00007124 DebugLoc dl = Op.getNode()->getDebugLoc();
7125 SDValue Vec = Op.getNode()->getOperand(0);
7126 SDValue Idx = Op.getNode()->getOperand(1);
7127
Craig Topper7a9a28b2012-08-12 02:23:29 +00007128 if (Op.getNode()->getValueType(0).is128BitVector() &&
7129 Vec.getNode()->getValueType(0).is256BitVector() &&
Craig Topperb14940a2012-04-22 20:55:18 +00007130 isa<ConstantSDNode>(Idx)) {
7131 unsigned IdxVal = cast<ConstantSDNode>(Idx)->getZExtValue();
7132 return Extract128BitVector(Vec, IdxVal, DAG, dl);
David Greenea5f26012011-02-07 19:36:54 +00007133 }
David Greene91585092011-01-26 15:38:49 +00007134 }
7135 return SDValue();
7136}
7137
David Greenecfe33c42011-01-26 19:13:22 +00007138// Lower a node with an INSERT_SUBVECTOR opcode. This may result in a
7139// simple superregister reference or explicit instructions to insert
7140// the upper bits of a vector.
7141SDValue
7142X86TargetLowering::LowerINSERT_SUBVECTOR(SDValue Op, SelectionDAG &DAG) const {
7143 if (Subtarget->hasAVX()) {
7144 DebugLoc dl = Op.getNode()->getDebugLoc();
7145 SDValue Vec = Op.getNode()->getOperand(0);
7146 SDValue SubVec = Op.getNode()->getOperand(1);
7147 SDValue Idx = Op.getNode()->getOperand(2);
7148
Craig Topper7a9a28b2012-08-12 02:23:29 +00007149 if (Op.getNode()->getValueType(0).is256BitVector() &&
7150 SubVec.getNode()->getValueType(0).is128BitVector() &&
Craig Topperb14940a2012-04-22 20:55:18 +00007151 isa<ConstantSDNode>(Idx)) {
7152 unsigned IdxVal = cast<ConstantSDNode>(Idx)->getZExtValue();
7153 return Insert128BitVector(Vec, SubVec, IdxVal, DAG, dl);
David Greenecfe33c42011-01-26 19:13:22 +00007154 }
7155 }
7156 return SDValue();
7157}
7158
Bill Wendling056292f2008-09-16 21:48:12 +00007159// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
7160// their target countpart wrapped in the X86ISD::Wrapper node. Suppose N is
7161// one of the above mentioned nodes. It has to be wrapped because otherwise
7162// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
7163// be used to form addressing mode. These wrapped nodes will be selected
7164// into MOV32ri.
Dan Gohman475871a2008-07-27 21:46:04 +00007165SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007166X86TargetLowering::LowerConstantPool(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00007167 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Eric Christopherfd179292009-08-27 18:07:15 +00007168
Chris Lattner41621a22009-06-26 19:22:52 +00007169 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
7170 // global base reg.
7171 unsigned char OpFlag = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00007172 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007173 CodeModel::Model M = getTargetMachine().getCodeModel();
7174
Chris Lattner4f066492009-07-11 20:29:19 +00007175 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007176 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattnere4df7562009-07-09 03:15:51 +00007177 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00007178 else if (Subtarget->isPICStyleGOT())
Chris Lattner88e1fd52009-07-09 04:24:46 +00007179 OpFlag = X86II::MO_GOTOFF;
Chris Lattnere2c92082009-07-10 21:00:45 +00007180 else if (Subtarget->isPICStyleStubPIC())
Chris Lattner88e1fd52009-07-09 04:24:46 +00007181 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Eric Christopherfd179292009-08-27 18:07:15 +00007182
Evan Cheng1606e8e2009-03-13 07:51:59 +00007183 SDValue Result = DAG.getTargetConstantPool(CP->getConstVal(), getPointerTy(),
Chris Lattner41621a22009-06-26 19:22:52 +00007184 CP->getAlignment(),
7185 CP->getOffset(), OpFlag);
7186 DebugLoc DL = CP->getDebugLoc();
Chris Lattner18c59872009-06-27 04:16:01 +00007187 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00007188 // With PIC, the address is actually $g + Offset.
Chris Lattner41621a22009-06-26 19:22:52 +00007189 if (OpFlag) {
7190 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesenb300d2a2009-02-07 00:55:49 +00007191 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00007192 DebugLoc(), getPointerTy()),
Anton Korobeynikov7f705592007-01-12 19:20:47 +00007193 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007194 }
7195
7196 return Result;
7197}
7198
Dan Gohmand858e902010-04-17 15:26:15 +00007199SDValue X86TargetLowering::LowerJumpTable(SDValue Op, SelectionDAG &DAG) const {
Chris Lattner18c59872009-06-27 04:16:01 +00007200 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
Eric Christopherfd179292009-08-27 18:07:15 +00007201
Chris Lattner18c59872009-06-27 04:16:01 +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;
7205 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
Chris Lattner18c59872009-06-27 04:16:01 +00007216 SDValue Result = DAG.getTargetJumpTable(JT->getIndex(), getPointerTy(),
7217 OpFlag);
7218 DebugLoc DL = JT->getDebugLoc();
7219 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Eric Christopherfd179292009-08-27 18:07:15 +00007220
Chris Lattner18c59872009-06-27 04:16:01 +00007221 // With PIC, the address is actually $g + Offset.
Chris Lattner1e61e692010-11-15 02:46:57 +00007222 if (OpFlag)
Chris Lattner18c59872009-06-27 04:16:01 +00007223 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
7224 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00007225 DebugLoc(), getPointerTy()),
Chris Lattner18c59872009-06-27 04:16:01 +00007226 Result);
Eric Christopherfd179292009-08-27 18:07:15 +00007227
Chris Lattner18c59872009-06-27 04:16:01 +00007228 return Result;
7229}
7230
7231SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007232X86TargetLowering::LowerExternalSymbol(SDValue Op, SelectionDAG &DAG) const {
Chris Lattner18c59872009-06-27 04:16:01 +00007233 const char *Sym = cast<ExternalSymbolSDNode>(Op)->getSymbol();
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() &&
Eli Friedman586272d2011-08-11 01:48:05 +00007242 (M == CodeModel::Small || M == CodeModel::Kernel)) {
7243 if (Subtarget->isTargetDarwin() || Subtarget->isTargetELF())
7244 OpFlag = X86II::MO_GOTPCREL;
Chris Lattnere4df7562009-07-09 03:15:51 +00007245 WrapperKind = X86ISD::WrapperRIP;
Eli Friedman586272d2011-08-11 01:48:05 +00007246 } else if (Subtarget->isPICStyleGOT()) {
7247 OpFlag = X86II::MO_GOT;
7248 } else if (Subtarget->isPICStyleStubPIC()) {
7249 OpFlag = X86II::MO_DARWIN_NONLAZY_PIC_BASE;
7250 } else if (Subtarget->isPICStyleStubNoDynamic()) {
7251 OpFlag = X86II::MO_DARWIN_NONLAZY;
7252 }
Eric Christopherfd179292009-08-27 18:07:15 +00007253
Chris Lattner18c59872009-06-27 04:16:01 +00007254 SDValue Result = DAG.getTargetExternalSymbol(Sym, getPointerTy(), OpFlag);
Eric Christopherfd179292009-08-27 18:07:15 +00007255
Chris Lattner18c59872009-06-27 04:16:01 +00007256 DebugLoc DL = Op.getDebugLoc();
7257 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Eric Christopherfd179292009-08-27 18:07:15 +00007258
7259
Chris Lattner18c59872009-06-27 04:16:01 +00007260 // With PIC, the address is actually $g + Offset.
7261 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
Chris Lattnere4df7562009-07-09 03:15:51 +00007262 !Subtarget->is64Bit()) {
Chris Lattner18c59872009-06-27 04:16:01 +00007263 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
7264 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00007265 DebugLoc(), getPointerTy()),
Chris Lattner18c59872009-06-27 04:16:01 +00007266 Result);
7267 }
Eric Christopherfd179292009-08-27 18:07:15 +00007268
Eli Friedman586272d2011-08-11 01:48:05 +00007269 // For symbols that require a load from a stub to get the address, emit the
7270 // load.
7271 if (isGlobalStubReference(OpFlag))
7272 Result = DAG.getLoad(getPointerTy(), DL, DAG.getEntryNode(), Result,
Pete Cooperd752e0f2011-11-08 18:42:53 +00007273 MachinePointerInfo::getGOT(), false, false, false, 0);
Eli Friedman586272d2011-08-11 01:48:05 +00007274
Chris Lattner18c59872009-06-27 04:16:01 +00007275 return Result;
7276}
7277
Dan Gohman475871a2008-07-27 21:46:04 +00007278SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007279X86TargetLowering::LowerBlockAddress(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman29cbade2009-11-20 23:18:13 +00007280 // Create the TargetBlockAddressAddress node.
7281 unsigned char OpFlags =
7282 Subtarget->ClassifyBlockAddressReference();
Dan Gohmanf705adb2009-10-30 01:28:02 +00007283 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman46510a72010-04-15 01:51:59 +00007284 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
Dan Gohman29cbade2009-11-20 23:18:13 +00007285 DebugLoc dl = Op.getDebugLoc();
7286 SDValue Result = DAG.getBlockAddress(BA, getPointerTy(),
7287 /*isTarget=*/true, OpFlags);
7288
Dan Gohmanf705adb2009-10-30 01:28:02 +00007289 if (Subtarget->isPICStyleRIPRel() &&
7290 (M == CodeModel::Small || M == CodeModel::Kernel))
Dan Gohman29cbade2009-11-20 23:18:13 +00007291 Result = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Result);
7292 else
7293 Result = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), Result);
Dan Gohmanf705adb2009-10-30 01:28:02 +00007294
Dan Gohman29cbade2009-11-20 23:18:13 +00007295 // With PIC, the address is actually $g + Offset.
7296 if (isGlobalRelativeToPICBase(OpFlags)) {
7297 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(),
7298 DAG.getNode(X86ISD::GlobalBaseReg, dl, getPointerTy()),
7299 Result);
7300 }
Dan Gohmanf705adb2009-10-30 01:28:02 +00007301
7302 return Result;
7303}
7304
7305SDValue
Dale Johannesen33c960f2009-02-04 20:06:27 +00007306X86TargetLowering::LowerGlobalAddress(const GlobalValue *GV, DebugLoc dl,
Dan Gohman6520e202008-10-18 02:06:02 +00007307 int64_t Offset,
Evan Chengda43bcf2008-09-24 00:05:32 +00007308 SelectionDAG &DAG) const {
Dan Gohman6520e202008-10-18 02:06:02 +00007309 // Create the TargetGlobalAddress node, folding in the constant
7310 // offset if it is legal.
Chris Lattnerd392bd92009-07-10 07:20:05 +00007311 unsigned char OpFlags =
7312 Subtarget->ClassifyGlobalReference(GV, getTargetMachine());
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007313 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman6520e202008-10-18 02:06:02 +00007314 SDValue Result;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007315 if (OpFlags == X86II::MO_NO_FLAG &&
7316 X86::isOffsetSuitableForCodeModel(Offset, M)) {
Chris Lattner4aa21aa2009-07-09 00:58:53 +00007317 // A direct static reference to a global.
Devang Patel0d881da2010-07-06 22:08:15 +00007318 Result = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(), Offset);
Dan Gohman6520e202008-10-18 02:06:02 +00007319 Offset = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00007320 } else {
Devang Patel0d881da2010-07-06 22:08:15 +00007321 Result = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(), 0, OpFlags);
Chris Lattner18c59872009-06-27 04:16:01 +00007322 }
Eric Christopherfd179292009-08-27 18:07:15 +00007323
Chris Lattner4f066492009-07-11 20:29:19 +00007324 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007325 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattner18c59872009-06-27 04:16:01 +00007326 Result = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Result);
7327 else
7328 Result = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), Result);
Dan Gohman6520e202008-10-18 02:06:02 +00007329
Anton Korobeynikov7f705592007-01-12 19:20:47 +00007330 // With PIC, the address is actually $g + Offset.
Chris Lattner36c25012009-07-10 07:34:39 +00007331 if (isGlobalRelativeToPICBase(OpFlags)) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00007332 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(),
7333 DAG.getNode(X86ISD::GlobalBaseReg, dl, getPointerTy()),
Anton Korobeynikov7f705592007-01-12 19:20:47 +00007334 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007335 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007336
Chris Lattner36c25012009-07-10 07:34:39 +00007337 // For globals that require a load from a stub to get the address, emit the
7338 // load.
7339 if (isGlobalStubReference(OpFlags))
Dale Johannesen33c960f2009-02-04 20:06:27 +00007340 Result = DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(), Result,
Pete Cooperd752e0f2011-11-08 18:42:53 +00007341 MachinePointerInfo::getGOT(), false, false, false, 0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007342
Dan Gohman6520e202008-10-18 02:06:02 +00007343 // If there was a non-zero offset that we didn't fold, create an explicit
7344 // addition for it.
7345 if (Offset != 0)
Dale Johannesen33c960f2009-02-04 20:06:27 +00007346 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(), Result,
Dan Gohman6520e202008-10-18 02:06:02 +00007347 DAG.getConstant(Offset, getPointerTy()));
7348
Evan Cheng0db9fe62006-04-25 20:13:52 +00007349 return Result;
7350}
7351
Evan Chengda43bcf2008-09-24 00:05:32 +00007352SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007353X86TargetLowering::LowerGlobalAddress(SDValue Op, SelectionDAG &DAG) const {
Evan Chengda43bcf2008-09-24 00:05:32 +00007354 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Dan Gohman6520e202008-10-18 02:06:02 +00007355 int64_t Offset = cast<GlobalAddressSDNode>(Op)->getOffset();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007356 return LowerGlobalAddress(GV, Op.getDebugLoc(), Offset, DAG);
Evan Chengda43bcf2008-09-24 00:05:32 +00007357}
7358
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007359static SDValue
7360GetTLSADDR(SelectionDAG &DAG, SDValue Chain, GlobalAddressSDNode *GA,
Owen Andersone50ed302009-08-10 22:56:29 +00007361 SDValue *InFlag, const EVT PtrVT, unsigned ReturnReg,
Hans Wennborgf0234fc2012-06-01 16:27:21 +00007362 unsigned char OperandFlags, bool LocalDynamic = false) {
Anton Korobeynikov817a4642009-12-11 19:39:55 +00007363 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00007364 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007365 DebugLoc dl = GA->getDebugLoc();
Devang Patel0d881da2010-07-06 22:08:15 +00007366 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl,
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007367 GA->getValueType(0),
Chris Lattnerb903bed2009-06-26 21:20:29 +00007368 GA->getOffset(),
7369 OperandFlags);
Hans Wennborgf0234fc2012-06-01 16:27:21 +00007370
7371 X86ISD::NodeType CallType = LocalDynamic ? X86ISD::TLSBASEADDR
7372 : X86ISD::TLSADDR;
7373
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007374 if (InFlag) {
7375 SDValue Ops[] = { Chain, TGA, *InFlag };
Hans Wennborgf0234fc2012-06-01 16:27:21 +00007376 Chain = DAG.getNode(CallType, dl, NodeTys, Ops, 3);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007377 } else {
7378 SDValue Ops[] = { Chain, TGA };
Hans Wennborgf0234fc2012-06-01 16:27:21 +00007379 Chain = DAG.getNode(CallType, dl, NodeTys, Ops, 2);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007380 }
Anton Korobeynikov817a4642009-12-11 19:39:55 +00007381
7382 // TLSADDR will be codegen'ed as call. Inform MFI that function has calls.
Bill Wendlingb92187a2010-05-14 21:14:32 +00007383 MFI->setAdjustsStack(true);
Anton Korobeynikov817a4642009-12-11 19:39:55 +00007384
Rafael Espindola15f1b662009-04-24 12:59:40 +00007385 SDValue Flag = Chain.getValue(1);
7386 return DAG.getCopyFromReg(Chain, dl, ReturnReg, PtrVT, Flag);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007387}
7388
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007389// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 32 bit
Dan Gohman475871a2008-07-27 21:46:04 +00007390static SDValue
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007391LowerToTLSGeneralDynamicModel32(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00007392 const EVT PtrVT) {
Dan Gohman475871a2008-07-27 21:46:04 +00007393 SDValue InFlag;
Dale Johannesendd64c412009-02-04 00:33:20 +00007394 DebugLoc dl = GA->getDebugLoc(); // ? function entry point might be better
7395 SDValue Chain = DAG.getCopyToReg(DAG.getEntryNode(), dl, X86::EBX,
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007396 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00007397 DebugLoc(), PtrVT), InFlag);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007398 InFlag = Chain.getValue(1);
7399
Chris Lattnerb903bed2009-06-26 21:20:29 +00007400 return GetTLSADDR(DAG, Chain, GA, &InFlag, PtrVT, X86::EAX, X86II::MO_TLSGD);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007401}
7402
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007403// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 64 bit
Dan Gohman475871a2008-07-27 21:46:04 +00007404static SDValue
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007405LowerToTLSGeneralDynamicModel64(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00007406 const EVT PtrVT) {
Chris Lattnerb903bed2009-06-26 21:20:29 +00007407 return GetTLSADDR(DAG, DAG.getEntryNode(), GA, NULL, PtrVT,
7408 X86::RAX, X86II::MO_TLSGD);
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007409}
7410
Hans Wennborgf0234fc2012-06-01 16:27:21 +00007411static SDValue LowerToTLSLocalDynamicModel(GlobalAddressSDNode *GA,
7412 SelectionDAG &DAG,
7413 const EVT PtrVT,
7414 bool is64Bit) {
7415 DebugLoc dl = GA->getDebugLoc();
7416
7417 // Get the start address of the TLS block for this module.
7418 X86MachineFunctionInfo* MFI = DAG.getMachineFunction()
7419 .getInfo<X86MachineFunctionInfo>();
7420 MFI->incNumLocalDynamicTLSAccesses();
7421
7422 SDValue Base;
7423 if (is64Bit) {
7424 Base = GetTLSADDR(DAG, DAG.getEntryNode(), GA, NULL, PtrVT, X86::RAX,
7425 X86II::MO_TLSLD, /*LocalDynamic=*/true);
7426 } else {
7427 SDValue InFlag;
7428 SDValue Chain = DAG.getCopyToReg(DAG.getEntryNode(), dl, X86::EBX,
7429 DAG.getNode(X86ISD::GlobalBaseReg, DebugLoc(), PtrVT), InFlag);
7430 InFlag = Chain.getValue(1);
7431 Base = GetTLSADDR(DAG, Chain, GA, &InFlag, PtrVT, X86::EAX,
7432 X86II::MO_TLSLDM, /*LocalDynamic=*/true);
7433 }
7434
7435 // Note: the CleanupLocalDynamicTLSPass will remove redundant computations
7436 // of Base.
7437
7438 // Build x@dtpoff.
7439 unsigned char OperandFlags = X86II::MO_DTPOFF;
7440 unsigned WrapperKind = X86ISD::Wrapper;
7441 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl,
7442 GA->getValueType(0),
7443 GA->getOffset(), OperandFlags);
7444 SDValue Offset = DAG.getNode(WrapperKind, dl, PtrVT, TGA);
7445
7446 // Add x@dtpoff with the base.
7447 return DAG.getNode(ISD::ADD, dl, PtrVT, Offset, Base);
7448}
7449
Hans Wennborg228756c2012-05-11 10:11:01 +00007450// Lower ISD::GlobalTLSAddress using the "initial exec" or "local exec" model.
Dan Gohman475871a2008-07-27 21:46:04 +00007451static SDValue LowerToTLSExecModel(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00007452 const EVT PtrVT, TLSModel::Model model,
Hans Wennborg228756c2012-05-11 10:11:01 +00007453 bool is64Bit, bool isPIC) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00007454 DebugLoc dl = GA->getDebugLoc();
Michael J. Spencerec38de22010-10-10 22:04:20 +00007455
Chris Lattnerf93b90c2010-09-22 04:39:11 +00007456 // Get the Thread Pointer, which is %gs:0 (32-bit) or %fs:0 (64-bit).
7457 Value *Ptr = Constant::getNullValue(Type::getInt8PtrTy(*DAG.getContext(),
7458 is64Bit ? 257 : 256));
Rafael Espindola094fad32009-04-08 21:14:34 +00007459
Michael J. Spencerec38de22010-10-10 22:04:20 +00007460 SDValue ThreadPointer = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(),
Chris Lattnerf93b90c2010-09-22 04:39:11 +00007461 DAG.getIntPtrConstant(0),
Pete Cooperd752e0f2011-11-08 18:42:53 +00007462 MachinePointerInfo(Ptr),
7463 false, false, false, 0);
Rafael Espindola094fad32009-04-08 21:14:34 +00007464
Chris Lattnerb903bed2009-06-26 21:20:29 +00007465 unsigned char OperandFlags = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00007466 // Most TLS accesses are not RIP relative, even on x86-64. One exception is
7467 // initialexec.
7468 unsigned WrapperKind = X86ISD::Wrapper;
7469 if (model == TLSModel::LocalExec) {
Chris Lattnerb903bed2009-06-26 21:20:29 +00007470 OperandFlags = is64Bit ? X86II::MO_TPOFF : X86II::MO_NTPOFF;
Hans Wennborg228756c2012-05-11 10:11:01 +00007471 } else if (model == TLSModel::InitialExec) {
7472 if (is64Bit) {
7473 OperandFlags = X86II::MO_GOTTPOFF;
7474 WrapperKind = X86ISD::WrapperRIP;
7475 } else {
7476 OperandFlags = isPIC ? X86II::MO_GOTNTPOFF : X86II::MO_INDNTPOFF;
7477 }
Chris Lattner18c59872009-06-27 04:16:01 +00007478 } else {
Hans Wennborg228756c2012-05-11 10:11:01 +00007479 llvm_unreachable("Unexpected model");
Chris Lattnerb903bed2009-06-26 21:20:29 +00007480 }
Eric Christopherfd179292009-08-27 18:07:15 +00007481
Hans Wennborg228756c2012-05-11 10:11:01 +00007482 // emit "addl x@ntpoff,%eax" (local exec)
7483 // or "addl x@indntpoff,%eax" (initial exec)
7484 // or "addl x@gotntpoff(%ebx) ,%eax" (initial exec, 32-bit pic)
Michael J. Spencerec38de22010-10-10 22:04:20 +00007485 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl,
Devang Patel0d881da2010-07-06 22:08:15 +00007486 GA->getValueType(0),
Chris Lattnerb903bed2009-06-26 21:20:29 +00007487 GA->getOffset(), OperandFlags);
Chris Lattner18c59872009-06-27 04:16:01 +00007488 SDValue Offset = DAG.getNode(WrapperKind, dl, PtrVT, TGA);
Lauro Ramos Venancio7d2cc2b2007-04-22 22:50:52 +00007489
Hans Wennborg228756c2012-05-11 10:11:01 +00007490 if (model == TLSModel::InitialExec) {
7491 if (isPIC && !is64Bit) {
7492 Offset = DAG.getNode(ISD::ADD, dl, PtrVT,
7493 DAG.getNode(X86ISD::GlobalBaseReg, DebugLoc(), PtrVT),
7494 Offset);
Hans Wennborg228756c2012-05-11 10:11:01 +00007495 }
Rafael Espindola94e3b382012-06-29 04:22:35 +00007496
7497 Offset = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Offset,
7498 MachinePointerInfo::getGOT(), false, false, false,
7499 0);
Hans Wennborg228756c2012-05-11 10:11:01 +00007500 }
Lauro Ramos Venancio7d2cc2b2007-04-22 22:50:52 +00007501
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007502 // The address of the thread local variable is the add of the thread
7503 // pointer with the offset of the variable.
Dale Johannesen33c960f2009-02-04 20:06:27 +00007504 return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007505}
7506
Dan Gohman475871a2008-07-27 21:46:04 +00007507SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007508X86TargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const {
Michael J. Spencerec38de22010-10-10 22:04:20 +00007509
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007510 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
Chris Lattnerb903bed2009-06-26 21:20:29 +00007511 const GlobalValue *GV = GA->getGlobal();
Eric Christopherfd179292009-08-27 18:07:15 +00007512
Eric Christopher30ef0e52010-06-03 04:07:48 +00007513 if (Subtarget->isTargetELF()) {
Chandler Carruth34797132012-04-08 17:20:55 +00007514 TLSModel::Model model = getTargetMachine().getTLSModel(GV);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007515
Eric Christopher30ef0e52010-06-03 04:07:48 +00007516 switch (model) {
7517 case TLSModel::GeneralDynamic:
Eric Christopher30ef0e52010-06-03 04:07:48 +00007518 if (Subtarget->is64Bit())
7519 return LowerToTLSGeneralDynamicModel64(GA, DAG, getPointerTy());
7520 return LowerToTLSGeneralDynamicModel32(GA, DAG, getPointerTy());
Hans Wennborgf0234fc2012-06-01 16:27:21 +00007521 case TLSModel::LocalDynamic:
7522 return LowerToTLSLocalDynamicModel(GA, DAG, getPointerTy(),
7523 Subtarget->is64Bit());
Eric Christopher30ef0e52010-06-03 04:07:48 +00007524 case TLSModel::InitialExec:
7525 case TLSModel::LocalExec:
7526 return LowerToTLSExecModel(GA, DAG, getPointerTy(), model,
Hans Wennborg228756c2012-05-11 10:11:01 +00007527 Subtarget->is64Bit(),
7528 getTargetMachine().getRelocationModel() == Reloc::PIC_);
Eric Christopher30ef0e52010-06-03 04:07:48 +00007529 }
Craig Toppere8eb1162012-04-23 03:26:18 +00007530 llvm_unreachable("Unknown TLS model.");
7531 }
7532
7533 if (Subtarget->isTargetDarwin()) {
Eric Christopher30ef0e52010-06-03 04:07:48 +00007534 // Darwin only has one model of TLS. Lower to that.
7535 unsigned char OpFlag = 0;
7536 unsigned WrapperKind = Subtarget->isPICStyleRIPRel() ?
7537 X86ISD::WrapperRIP : X86ISD::Wrapper;
Michael J. Spencerec38de22010-10-10 22:04:20 +00007538
Eric Christopher30ef0e52010-06-03 04:07:48 +00007539 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
7540 // global base reg.
7541 bool PIC32 = (getTargetMachine().getRelocationModel() == Reloc::PIC_) &&
7542 !Subtarget->is64Bit();
7543 if (PIC32)
7544 OpFlag = X86II::MO_TLVP_PIC_BASE;
7545 else
7546 OpFlag = X86II::MO_TLVP;
Michael J. Spencerec38de22010-10-10 22:04:20 +00007547 DebugLoc DL = Op.getDebugLoc();
Devang Patel0d881da2010-07-06 22:08:15 +00007548 SDValue Result = DAG.getTargetGlobalAddress(GA->getGlobal(), DL,
Eric Christopherd8c05362010-12-09 06:25:53 +00007549 GA->getValueType(0),
Eric Christopher30ef0e52010-06-03 04:07:48 +00007550 GA->getOffset(), OpFlag);
Eric Christopher30ef0e52010-06-03 04:07:48 +00007551 SDValue Offset = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007552
Eric Christopher30ef0e52010-06-03 04:07:48 +00007553 // With PIC32, the address is actually $g + Offset.
7554 if (PIC32)
7555 Offset = DAG.getNode(ISD::ADD, DL, getPointerTy(),
7556 DAG.getNode(X86ISD::GlobalBaseReg,
7557 DebugLoc(), getPointerTy()),
7558 Offset);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007559
Eric Christopher30ef0e52010-06-03 04:07:48 +00007560 // Lowering the machine isd will make sure everything is in the right
7561 // location.
Eric Christopherd8c05362010-12-09 06:25:53 +00007562 SDValue Chain = DAG.getEntryNode();
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00007563 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Eric Christopherd8c05362010-12-09 06:25:53 +00007564 SDValue Args[] = { Chain, Offset };
7565 Chain = DAG.getNode(X86ISD::TLSCALL, DL, NodeTys, Args, 2);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007566
Eric Christopher30ef0e52010-06-03 04:07:48 +00007567 // TLSCALL will be codegen'ed as call. Inform MFI that function has calls.
7568 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
7569 MFI->setAdjustsStack(true);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00007570
Eric Christopher30ef0e52010-06-03 04:07:48 +00007571 // And our return value (tls address) is in the standard call return value
7572 // location.
Eric Christopherd8c05362010-12-09 06:25:53 +00007573 unsigned Reg = Subtarget->is64Bit() ? X86::RAX : X86::EAX;
Evan Chengfd230df2011-10-19 22:22:54 +00007574 return DAG.getCopyFromReg(Chain, DL, Reg, getPointerTy(),
7575 Chain.getValue(1));
Craig Toppere8eb1162012-04-23 03:26:18 +00007576 }
7577
7578 if (Subtarget->isTargetWindows()) {
Anton Korobeynikovd4a19b62012-02-11 17:26:53 +00007579 // Just use the implicit TLS architecture
7580 // Need to generate someting similar to:
7581 // mov rdx, qword [gs:abs 58H]; Load pointer to ThreadLocalStorage
7582 // ; from TEB
7583 // mov ecx, dword [rel _tls_index]: Load index (from C runtime)
7584 // mov rcx, qword [rdx+rcx*8]
7585 // mov eax, .tls$:tlsvar
7586 // [rax+rcx] contains the address
7587 // Windows 64bit: gs:0x58
7588 // Windows 32bit: fs:__tls_array
7589
7590 // If GV is an alias then use the aliasee for determining
7591 // thread-localness.
7592 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV))
7593 GV = GA->resolveAliasedGlobal(false);
7594 DebugLoc dl = GA->getDebugLoc();
7595 SDValue Chain = DAG.getEntryNode();
7596
7597 // Get the Thread Pointer, which is %fs:__tls_array (32-bit) or
7598 // %gs:0x58 (64-bit).
7599 Value *Ptr = Constant::getNullValue(Subtarget->is64Bit()
7600 ? Type::getInt8PtrTy(*DAG.getContext(),
7601 256)
7602 : Type::getInt32PtrTy(*DAG.getContext(),
7603 257));
7604
7605 SDValue ThreadPointer = DAG.getLoad(getPointerTy(), dl, Chain,
7606 Subtarget->is64Bit()
7607 ? DAG.getIntPtrConstant(0x58)
7608 : DAG.getExternalSymbol("_tls_array",
7609 getPointerTy()),
7610 MachinePointerInfo(Ptr),
7611 false, false, false, 0);
7612
7613 // Load the _tls_index variable
7614 SDValue IDX = DAG.getExternalSymbol("_tls_index", getPointerTy());
7615 if (Subtarget->is64Bit())
7616 IDX = DAG.getExtLoad(ISD::ZEXTLOAD, dl, getPointerTy(), Chain,
7617 IDX, MachinePointerInfo(), MVT::i32,
7618 false, false, 0);
7619 else
7620 IDX = DAG.getLoad(getPointerTy(), dl, Chain, IDX, MachinePointerInfo(),
7621 false, false, false, 0);
7622
7623 SDValue Scale = DAG.getConstant(Log2_64_Ceil(TD->getPointerSize()),
Craig Topper0fbf3642012-04-23 03:28:34 +00007624 getPointerTy());
Anton Korobeynikovd4a19b62012-02-11 17:26:53 +00007625 IDX = DAG.getNode(ISD::SHL, dl, getPointerTy(), IDX, Scale);
7626
7627 SDValue res = DAG.getNode(ISD::ADD, dl, getPointerTy(), ThreadPointer, IDX);
7628 res = DAG.getLoad(getPointerTy(), dl, Chain, res, MachinePointerInfo(),
7629 false, false, false, 0);
7630
7631 // Get the offset of start of .tls section
7632 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl,
7633 GA->getValueType(0),
7634 GA->getOffset(), X86II::MO_SECREL);
7635 SDValue Offset = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), TGA);
7636
7637 // The address of the thread local variable is the add of the thread
7638 // pointer with the offset of the variable.
7639 return DAG.getNode(ISD::ADD, dl, getPointerTy(), res, Offset);
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007640 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00007641
David Blaikie4d6ccb52012-01-20 21:51:11 +00007642 llvm_unreachable("TLS not implemented for this target.");
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007643}
7644
Evan Cheng0db9fe62006-04-25 20:13:52 +00007645
Chad Rosierb90d2a92012-01-03 23:19:12 +00007646/// LowerShiftParts - Lower SRA_PARTS and friends, which return two i32 values
7647/// and take a 2 x i32 value to shift plus a shift amount.
7648SDValue X86TargetLowering::LowerShiftParts(SDValue Op, SelectionDAG &DAG) const{
Dan Gohman4c1fa612008-03-03 22:22:09 +00007649 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
Owen Andersone50ed302009-08-10 22:56:29 +00007650 EVT VT = Op.getValueType();
Duncan Sands83ec4b62008-06-06 12:08:01 +00007651 unsigned VTBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007652 DebugLoc dl = Op.getDebugLoc();
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007653 bool isSRA = Op.getOpcode() == ISD::SRA_PARTS;
Dan Gohman475871a2008-07-27 21:46:04 +00007654 SDValue ShOpLo = Op.getOperand(0);
7655 SDValue ShOpHi = Op.getOperand(1);
7656 SDValue ShAmt = Op.getOperand(2);
Chris Lattner31dcfe62009-07-29 05:48:09 +00007657 SDValue Tmp1 = isSRA ? DAG.getNode(ISD::SRA, dl, VT, ShOpHi,
Owen Anderson825b72b2009-08-11 20:47:22 +00007658 DAG.getConstant(VTBits - 1, MVT::i8))
Chris Lattner31dcfe62009-07-29 05:48:09 +00007659 : DAG.getConstant(0, VT);
Evan Chenge3413162006-01-09 18:33:28 +00007660
Dan Gohman475871a2008-07-27 21:46:04 +00007661 SDValue Tmp2, Tmp3;
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007662 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dale Johannesenace16102009-02-03 19:33:06 +00007663 Tmp2 = DAG.getNode(X86ISD::SHLD, dl, VT, ShOpHi, ShOpLo, ShAmt);
7664 Tmp3 = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ShAmt);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007665 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00007666 Tmp2 = DAG.getNode(X86ISD::SHRD, dl, VT, ShOpLo, ShOpHi, ShAmt);
7667 Tmp3 = DAG.getNode(isSRA ? ISD::SRA : ISD::SRL, dl, VT, ShOpHi, ShAmt);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007668 }
Evan Chenge3413162006-01-09 18:33:28 +00007669
Owen Anderson825b72b2009-08-11 20:47:22 +00007670 SDValue AndNode = DAG.getNode(ISD::AND, dl, MVT::i8, ShAmt,
7671 DAG.getConstant(VTBits, MVT::i8));
Chris Lattnerccfea352010-02-22 00:28:59 +00007672 SDValue Cond = DAG.getNode(X86ISD::CMP, dl, MVT::i32,
Owen Anderson825b72b2009-08-11 20:47:22 +00007673 AndNode, DAG.getConstant(0, MVT::i8));
Evan Chenge3413162006-01-09 18:33:28 +00007674
Dan Gohman475871a2008-07-27 21:46:04 +00007675 SDValue Hi, Lo;
Owen Anderson825b72b2009-08-11 20:47:22 +00007676 SDValue CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Dan Gohman475871a2008-07-27 21:46:04 +00007677 SDValue Ops0[4] = { Tmp2, Tmp3, CC, Cond };
7678 SDValue Ops1[4] = { Tmp3, Tmp1, CC, Cond };
Duncan Sandsf9516202008-06-30 10:19:09 +00007679
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007680 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dale Johannesenace16102009-02-03 19:33:06 +00007681 Hi = DAG.getNode(X86ISD::CMOV, dl, VT, Ops0, 4);
7682 Lo = DAG.getNode(X86ISD::CMOV, dl, VT, Ops1, 4);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007683 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00007684 Lo = DAG.getNode(X86ISD::CMOV, dl, VT, Ops0, 4);
7685 Hi = DAG.getNode(X86ISD::CMOV, dl, VT, Ops1, 4);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007686 }
7687
Dan Gohman475871a2008-07-27 21:46:04 +00007688 SDValue Ops[2] = { Lo, Hi };
Dale Johannesenace16102009-02-03 19:33:06 +00007689 return DAG.getMergeValues(Ops, 2, dl);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007690}
Evan Chenga3195e82006-01-12 22:54:21 +00007691
Dan Gohmand858e902010-04-17 15:26:15 +00007692SDValue X86TargetLowering::LowerSINT_TO_FP(SDValue Op,
7693 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00007694 EVT SrcVT = Op.getOperand(0).getValueType();
Eli Friedman23ef1052009-06-06 03:57:58 +00007695
Dale Johannesen0488fb62010-09-30 23:57:10 +00007696 if (SrcVT.isVector())
Eli Friedman23ef1052009-06-06 03:57:58 +00007697 return SDValue();
Eli Friedman23ef1052009-06-06 03:57:58 +00007698
Owen Anderson825b72b2009-08-11 20:47:22 +00007699 assert(SrcVT.getSimpleVT() <= MVT::i64 && SrcVT.getSimpleVT() >= MVT::i16 &&
Chris Lattnerb09916b2008-02-27 05:57:41 +00007700 "Unknown SINT_TO_FP to lower!");
Scott Michelfdc40a02009-02-17 22:15:04 +00007701
Eli Friedman36df4992009-05-27 00:47:34 +00007702 // These are really Legal; return the operand so the caller accepts it as
7703 // Legal.
Owen Anderson825b72b2009-08-11 20:47:22 +00007704 if (SrcVT == MVT::i32 && isScalarFPTypeInSSEReg(Op.getValueType()))
Eli Friedman36df4992009-05-27 00:47:34 +00007705 return Op;
Owen Anderson825b72b2009-08-11 20:47:22 +00007706 if (SrcVT == MVT::i64 && isScalarFPTypeInSSEReg(Op.getValueType()) &&
Eli Friedman36df4992009-05-27 00:47:34 +00007707 Subtarget->is64Bit()) {
7708 return Op;
7709 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007710
Bruno Cardoso Lopesa511b8e2011-08-09 17:39:01 +00007711 DebugLoc dl = Op.getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +00007712 unsigned Size = SrcVT.getSizeInBits()/8;
Evan Cheng0db9fe62006-04-25 20:13:52 +00007713 MachineFunction &MF = DAG.getMachineFunction();
David Greene3f2bf852009-11-12 20:49:22 +00007714 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size, false);
Dan Gohman475871a2008-07-27 21:46:04 +00007715 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Dale Johannesenace16102009-02-03 19:33:06 +00007716 SDValue Chain = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Bill Wendling105be5a2009-03-13 08:41:47 +00007717 StackSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00007718 MachinePointerInfo::getFixedStack(SSFI),
David Greene67c9d422010-02-15 16:53:33 +00007719 false, false, 0);
Eli Friedman948e95a2009-05-23 09:59:16 +00007720 return BuildFILD(Op, SrcVT, Chain, StackSlot, DAG);
7721}
Evan Cheng0db9fe62006-04-25 20:13:52 +00007722
Owen Andersone50ed302009-08-10 22:56:29 +00007723SDValue X86TargetLowering::BuildFILD(SDValue Op, EVT SrcVT, SDValue Chain,
Michael J. Spencerec38de22010-10-10 22:04:20 +00007724 SDValue StackSlot,
Dan Gohmand858e902010-04-17 15:26:15 +00007725 SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00007726 // Build the FILD
Chris Lattner492a43e2010-09-22 01:28:21 +00007727 DebugLoc DL = Op.getDebugLoc();
Chris Lattner5a88b832007-02-25 07:10:00 +00007728 SDVTList Tys;
Chris Lattner78631162008-01-16 06:24:21 +00007729 bool useSSE = isScalarFPTypeInSSEReg(Op.getValueType());
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00007730 if (useSSE)
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00007731 Tys = DAG.getVTList(MVT::f64, MVT::Other, MVT::Glue);
Chris Lattner5a88b832007-02-25 07:10:00 +00007732 else
Owen Anderson825b72b2009-08-11 20:47:22 +00007733 Tys = DAG.getVTList(Op.getValueType(), MVT::Other);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007734
Chris Lattner492a43e2010-09-22 01:28:21 +00007735 unsigned ByteSize = SrcVT.getSizeInBits()/8;
Michael J. Spencerec38de22010-10-10 22:04:20 +00007736
Stuart Hastings84be9582011-06-02 15:57:11 +00007737 FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(StackSlot);
7738 MachineMemOperand *MMO;
7739 if (FI) {
7740 int SSFI = FI->getIndex();
7741 MMO =
7742 DAG.getMachineFunction()
7743 .getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
7744 MachineMemOperand::MOLoad, ByteSize, ByteSize);
7745 } else {
7746 MMO = cast<LoadSDNode>(StackSlot)->getMemOperand();
7747 StackSlot = StackSlot.getOperand(1);
7748 }
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00007749 SDValue Ops[] = { Chain, StackSlot, DAG.getValueType(SrcVT) };
Chris Lattner492a43e2010-09-22 01:28:21 +00007750 SDValue Result = DAG.getMemIntrinsicNode(useSSE ? X86ISD::FILD_FLAG :
7751 X86ISD::FILD, DL,
7752 Tys, Ops, array_lengthof(Ops),
7753 SrcVT, MMO);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007754
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00007755 if (useSSE) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00007756 Chain = Result.getValue(1);
Dan Gohman475871a2008-07-27 21:46:04 +00007757 SDValue InFlag = Result.getValue(2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007758
7759 // FIXME: Currently the FST is flagged to the FILD_FLAG. This
7760 // shouldn't be necessary except that RFP cannot be live across
7761 // multiple blocks. When stackifier is fixed, they can be uncoupled.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00007762 MachineFunction &MF = DAG.getMachineFunction();
Bob Wilsoneafca4e2010-09-22 17:35:14 +00007763 unsigned SSFISize = Op.getValueType().getSizeInBits()/8;
7764 int SSFI = MF.getFrameInfo()->CreateStackObject(SSFISize, SSFISize, false);
Dan Gohman475871a2008-07-27 21:46:04 +00007765 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Owen Anderson825b72b2009-08-11 20:47:22 +00007766 Tys = DAG.getVTList(MVT::Other);
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00007767 SDValue Ops[] = {
7768 Chain, Result, StackSlot, DAG.getValueType(Op.getValueType()), InFlag
7769 };
Chris Lattner492a43e2010-09-22 01:28:21 +00007770 MachineMemOperand *MMO =
7771 DAG.getMachineFunction()
7772 .getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
Bob Wilsoneafca4e2010-09-22 17:35:14 +00007773 MachineMemOperand::MOStore, SSFISize, SSFISize);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007774
Chris Lattner492a43e2010-09-22 01:28:21 +00007775 Chain = DAG.getMemIntrinsicNode(X86ISD::FST, DL, Tys,
7776 Ops, array_lengthof(Ops),
7777 Op.getValueType(), MMO);
7778 Result = DAG.getLoad(Op.getValueType(), DL, Chain, StackSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00007779 MachinePointerInfo::getFixedStack(SSFI),
Pete Cooperd752e0f2011-11-08 18:42:53 +00007780 false, false, false, 0);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00007781 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00007782
Evan Cheng0db9fe62006-04-25 20:13:52 +00007783 return Result;
7784}
7785
Bill Wendling8b8a6362009-01-17 03:56:04 +00007786// LowerUINT_TO_FP_i64 - 64-bit unsigned integer to double expansion.
Dan Gohmand858e902010-04-17 15:26:15 +00007787SDValue X86TargetLowering::LowerUINT_TO_FP_i64(SDValue Op,
7788 SelectionDAG &DAG) const {
Bill Wendling397ae212012-01-05 02:13:20 +00007789 // This algorithm is not obvious. Here it is what we're trying to output:
Bill Wendling8b8a6362009-01-17 03:56:04 +00007790 /*
Bill Wendling397ae212012-01-05 02:13:20 +00007791 movq %rax, %xmm0
7792 punpckldq (c0), %xmm0 // c0: (uint4){ 0x43300000U, 0x45300000U, 0U, 0U }
7793 subpd (c1), %xmm0 // c1: (double2){ 0x1.0p52, 0x1.0p52 * 0x1.0p32 }
7794 #ifdef __SSE3__
Chad Rosiera20e1e72012-08-01 18:39:17 +00007795 haddpd %xmm0, %xmm0
Bill Wendling397ae212012-01-05 02:13:20 +00007796 #else
Chad Rosiera20e1e72012-08-01 18:39:17 +00007797 pshufd $0x4e, %xmm0, %xmm1
Bill Wendling397ae212012-01-05 02:13:20 +00007798 addpd %xmm1, %xmm0
7799 #endif
Bill Wendling8b8a6362009-01-17 03:56:04 +00007800 */
Dale Johannesen040225f2008-10-21 23:07:49 +00007801
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007802 DebugLoc dl = Op.getDebugLoc();
Owen Andersona90b3dc2009-07-15 21:51:10 +00007803 LLVMContext *Context = DAG.getContext();
Dale Johannesenace16102009-02-03 19:33:06 +00007804
Dale Johannesen1c15bf52008-10-21 20:50:01 +00007805 // Build some magic constants.
Chris Lattner7302d802012-02-06 21:56:39 +00007806 const uint32_t CV0[] = { 0x43300000, 0x45300000, 0, 0 };
7807 Constant *C0 = ConstantDataVector::get(*Context, CV0);
Evan Cheng1606e8e2009-03-13 07:51:59 +00007808 SDValue CPIdx0 = DAG.getConstantPool(C0, getPointerTy(), 16);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00007809
Chris Lattner97484792012-01-25 09:56:22 +00007810 SmallVector<Constant*,2> CV1;
7811 CV1.push_back(
Chris Lattner4ca829e2012-01-25 06:02:56 +00007812 ConstantFP::get(*Context, APFloat(APInt(64, 0x4330000000000000ULL))));
Chris Lattner97484792012-01-25 09:56:22 +00007813 CV1.push_back(
7814 ConstantFP::get(*Context, APFloat(APInt(64, 0x4530000000000000ULL))));
7815 Constant *C1 = ConstantVector::get(CV1);
Evan Cheng1606e8e2009-03-13 07:51:59 +00007816 SDValue CPIdx1 = DAG.getConstantPool(C1, getPointerTy(), 16);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00007817
Bill Wendling397ae212012-01-05 02:13:20 +00007818 // Load the 64-bit value into an XMM register.
7819 SDValue XR1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i64,
7820 Op.getOperand(0));
Owen Anderson825b72b2009-08-11 20:47:22 +00007821 SDValue CLod0 = DAG.getLoad(MVT::v4i32, dl, DAG.getEntryNode(), CPIdx0,
Chris Lattnere8639032010-09-21 06:22:23 +00007822 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00007823 false, false, false, 16);
Bill Wendling397ae212012-01-05 02:13:20 +00007824 SDValue Unpck1 = getUnpackl(DAG, dl, MVT::v4i32,
7825 DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, XR1),
7826 CLod0);
7827
Owen Anderson825b72b2009-08-11 20:47:22 +00007828 SDValue CLod1 = DAG.getLoad(MVT::v2f64, dl, CLod0.getValue(1), CPIdx1,
Chris Lattnere8639032010-09-21 06:22:23 +00007829 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00007830 false, false, false, 16);
Bill Wendling397ae212012-01-05 02:13:20 +00007831 SDValue XR2F = DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Unpck1);
Owen Anderson825b72b2009-08-11 20:47:22 +00007832 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::v2f64, XR2F, CLod1);
Bill Wendling397ae212012-01-05 02:13:20 +00007833 SDValue Result;
Bill Wendling8b8a6362009-01-17 03:56:04 +00007834
Craig Topperd0a31172012-01-10 06:37:29 +00007835 if (Subtarget->hasSSE3()) {
Bill Wendling397ae212012-01-05 02:13:20 +00007836 // FIXME: The 'haddpd' instruction may be slower than 'movhlps + addsd'.
7837 Result = DAG.getNode(X86ISD::FHADD, dl, MVT::v2f64, Sub, Sub);
7838 } else {
7839 SDValue S2F = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, Sub);
7840 SDValue Shuffle = getTargetShuffleNode(X86ISD::PSHUFD, dl, MVT::v4i32,
7841 S2F, 0x4E, DAG);
7842 Result = DAG.getNode(ISD::FADD, dl, MVT::v2f64,
7843 DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Shuffle),
7844 Sub);
7845 }
7846
7847 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Result,
Dale Johannesen1c15bf52008-10-21 20:50:01 +00007848 DAG.getIntPtrConstant(0));
7849}
7850
Bill Wendling8b8a6362009-01-17 03:56:04 +00007851// LowerUINT_TO_FP_i32 - 32-bit unsigned integer to float expansion.
Dan Gohmand858e902010-04-17 15:26:15 +00007852SDValue X86TargetLowering::LowerUINT_TO_FP_i32(SDValue Op,
7853 SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007854 DebugLoc dl = Op.getDebugLoc();
Bill Wendling8b8a6362009-01-17 03:56:04 +00007855 // FP constant to bias correct the final result.
7856 SDValue Bias = DAG.getConstantFP(BitsToDouble(0x4330000000000000ULL),
Owen Anderson825b72b2009-08-11 20:47:22 +00007857 MVT::f64);
Bill Wendling8b8a6362009-01-17 03:56:04 +00007858
7859 // Load the 32-bit value into an XMM register.
Owen Anderson825b72b2009-08-11 20:47:22 +00007860 SDValue Load = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
Eli Friedman6cdc1f42011-08-02 18:38:35 +00007861 Op.getOperand(0));
Bill Wendling8b8a6362009-01-17 03:56:04 +00007862
Eli Friedmanf3704762011-08-29 21:15:46 +00007863 // Zero out the upper parts of the register.
Craig Topper12216172012-01-13 08:12:35 +00007864 Load = getShuffleVectorZeroOrUndef(Load, 0, true, Subtarget, DAG);
Eli Friedmanf3704762011-08-29 21:15:46 +00007865
Owen Anderson825b72b2009-08-11 20:47:22 +00007866 Load = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007867 DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Load),
Bill Wendling8b8a6362009-01-17 03:56:04 +00007868 DAG.getIntPtrConstant(0));
7869
7870 // Or the load with the bias.
Owen Anderson825b72b2009-08-11 20:47:22 +00007871 SDValue Or = DAG.getNode(ISD::OR, dl, MVT::v2i64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007872 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00007873 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00007874 MVT::v2f64, Load)),
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007875 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00007876 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00007877 MVT::v2f64, Bias)));
7878 Or = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007879 DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Or),
Bill Wendling8b8a6362009-01-17 03:56:04 +00007880 DAG.getIntPtrConstant(0));
7881
7882 // Subtract the bias.
Owen Anderson825b72b2009-08-11 20:47:22 +00007883 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::f64, Or, Bias);
Bill Wendling8b8a6362009-01-17 03:56:04 +00007884
7885 // Handle final rounding.
Owen Andersone50ed302009-08-10 22:56:29 +00007886 EVT DestVT = Op.getValueType();
Bill Wendling030939c2009-01-17 07:40:19 +00007887
Craig Topper69947b92012-04-23 06:57:04 +00007888 if (DestVT.bitsLT(MVT::f64))
Dale Johannesenace16102009-02-03 19:33:06 +00007889 return DAG.getNode(ISD::FP_ROUND, dl, DestVT, Sub,
Bill Wendling030939c2009-01-17 07:40:19 +00007890 DAG.getIntPtrConstant(0));
Craig Topper69947b92012-04-23 06:57:04 +00007891 if (DestVT.bitsGT(MVT::f64))
Dale Johannesenace16102009-02-03 19:33:06 +00007892 return DAG.getNode(ISD::FP_EXTEND, dl, DestVT, Sub);
Bill Wendling030939c2009-01-17 07:40:19 +00007893
7894 // Handle final rounding.
7895 return Sub;
Bill Wendling8b8a6362009-01-17 03:56:04 +00007896}
7897
Dan Gohmand858e902010-04-17 15:26:15 +00007898SDValue X86TargetLowering::LowerUINT_TO_FP(SDValue Op,
7899 SelectionDAG &DAG) const {
Evan Chenga06ec9e2009-01-19 08:08:22 +00007900 SDValue N0 = Op.getOperand(0);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007901 DebugLoc dl = Op.getDebugLoc();
Bill Wendling8b8a6362009-01-17 03:56:04 +00007902
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007903 // Since UINT_TO_FP is legal (it's marked custom), dag combiner won't
Evan Chenga06ec9e2009-01-19 08:08:22 +00007904 // optimize it to a SINT_TO_FP when the sign bit is known zero. Perform
7905 // the optimization here.
7906 if (DAG.SignBitIsZero(N0))
Dale Johannesenace16102009-02-03 19:33:06 +00007907 return DAG.getNode(ISD::SINT_TO_FP, dl, Op.getValueType(), N0);
Evan Chenga06ec9e2009-01-19 08:08:22 +00007908
Owen Andersone50ed302009-08-10 22:56:29 +00007909 EVT SrcVT = N0.getValueType();
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007910 EVT DstVT = Op.getValueType();
7911 if (SrcVT == MVT::i64 && DstVT == MVT::f64 && X86ScalarSSEf64)
Bill Wendling8b8a6362009-01-17 03:56:04 +00007912 return LowerUINT_TO_FP_i64(Op, DAG);
Craig Topper69947b92012-04-23 06:57:04 +00007913 if (SrcVT == MVT::i32 && X86ScalarSSEf64)
Bill Wendling8b8a6362009-01-17 03:56:04 +00007914 return LowerUINT_TO_FP_i32(Op, DAG);
Craig Topper69947b92012-04-23 06:57:04 +00007915 if (Subtarget->is64Bit() && SrcVT == MVT::i64 && DstVT == MVT::f32)
Bill Wendling397ae212012-01-05 02:13:20 +00007916 return SDValue();
Eli Friedman948e95a2009-05-23 09:59:16 +00007917
7918 // Make a 64-bit buffer, and use it to build an FILD.
Owen Anderson825b72b2009-08-11 20:47:22 +00007919 SDValue StackSlot = DAG.CreateStackTemporary(MVT::i64);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007920 if (SrcVT == MVT::i32) {
7921 SDValue WordOff = DAG.getConstant(4, getPointerTy());
7922 SDValue OffsetSlot = DAG.getNode(ISD::ADD, dl,
7923 getPointerTy(), StackSlot, WordOff);
7924 SDValue Store1 = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Chris Lattner8026a9d2010-09-21 17:50:43 +00007925 StackSlot, MachinePointerInfo(),
7926 false, false, 0);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007927 SDValue Store2 = DAG.getStore(Store1, dl, DAG.getConstant(0, MVT::i32),
Chris Lattner8026a9d2010-09-21 17:50:43 +00007928 OffsetSlot, MachinePointerInfo(),
7929 false, false, 0);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007930 SDValue Fild = BuildFILD(Op, MVT::i64, Store2, StackSlot, DAG);
7931 return Fild;
7932 }
7933
7934 assert(SrcVT == MVT::i64 && "Unexpected type in UINT_TO_FP");
7935 SDValue Store = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Bill Wendlingf6c07472012-01-10 19:41:30 +00007936 StackSlot, MachinePointerInfo(),
Chris Lattner8026a9d2010-09-21 17:50:43 +00007937 false, false, 0);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007938 // For i64 source, we need to add the appropriate power of 2 if the input
7939 // was negative. This is the same as the optimization in
7940 // DAGTypeLegalizer::ExpandIntOp_UNIT_TO_FP, and for it to be safe here,
7941 // we must be careful to do the computation in x87 extended precision, not
7942 // in SSE. (The generic code can't know it's OK to do this, or how to.)
Chris Lattner492a43e2010-09-22 01:28:21 +00007943 int SSFI = cast<FrameIndexSDNode>(StackSlot)->getIndex();
7944 MachineMemOperand *MMO =
7945 DAG.getMachineFunction()
7946 .getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
7947 MachineMemOperand::MOLoad, 8, 8);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007948
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007949 SDVTList Tys = DAG.getVTList(MVT::f80, MVT::Other);
7950 SDValue Ops[] = { Store, StackSlot, DAG.getValueType(MVT::i64) };
Chris Lattner492a43e2010-09-22 01:28:21 +00007951 SDValue Fild = DAG.getMemIntrinsicNode(X86ISD::FILD, dl, Tys, Ops, 3,
7952 MVT::i64, MMO);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007953
7954 APInt FF(32, 0x5F800000ULL);
7955
7956 // Check whether the sign bit is set.
7957 SDValue SignSet = DAG.getSetCC(dl, getSetCCResultType(MVT::i64),
7958 Op.getOperand(0), DAG.getConstant(0, MVT::i64),
7959 ISD::SETLT);
7960
7961 // Build a 64 bit pair (0, FF) in the constant pool, with FF in the lo bits.
7962 SDValue FudgePtr = DAG.getConstantPool(
7963 ConstantInt::get(*DAG.getContext(), FF.zext(64)),
7964 getPointerTy());
7965
7966 // Get a pointer to FF if the sign bit was set, or to 0 otherwise.
7967 SDValue Zero = DAG.getIntPtrConstant(0);
7968 SDValue Four = DAG.getIntPtrConstant(4);
7969 SDValue Offset = DAG.getNode(ISD::SELECT, dl, Zero.getValueType(), SignSet,
7970 Zero, Four);
7971 FudgePtr = DAG.getNode(ISD::ADD, dl, getPointerTy(), FudgePtr, Offset);
7972
7973 // Load the value out, extending it from f32 to f80.
7974 // FIXME: Avoid the extend by constructing the right constant pool?
Stuart Hastingsa9011292011-02-16 16:23:55 +00007975 SDValue Fudge = DAG.getExtLoad(ISD::EXTLOAD, dl, MVT::f80, DAG.getEntryNode(),
Chris Lattnere8639032010-09-21 06:22:23 +00007976 FudgePtr, MachinePointerInfo::getConstantPool(),
7977 MVT::f32, false, false, 4);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007978 // Extend everything to 80 bits to force it to be done on x87.
7979 SDValue Add = DAG.getNode(ISD::FADD, dl, MVT::f80, Fild, Fudge);
7980 return DAG.getNode(ISD::FP_ROUND, dl, DstVT, Add, DAG.getIntPtrConstant(0));
Bill Wendling8b8a6362009-01-17 03:56:04 +00007981}
7982
Dan Gohman475871a2008-07-27 21:46:04 +00007983std::pair<SDValue,SDValue> X86TargetLowering::
NAKAMURA Takumi9a68fdc2012-02-25 03:37:25 +00007984FP_TO_INTHelper(SDValue Op, SelectionDAG &DAG, bool IsSigned, bool IsReplace) const {
Chris Lattner07290932010-09-22 01:05:16 +00007985 DebugLoc DL = Op.getDebugLoc();
Eli Friedman948e95a2009-05-23 09:59:16 +00007986
Owen Andersone50ed302009-08-10 22:56:29 +00007987 EVT DstTy = Op.getValueType();
Eli Friedman948e95a2009-05-23 09:59:16 +00007988
Michael J. Spencer1a2d0612012-02-24 19:01:22 +00007989 if (!IsSigned && !isIntegerTypeFTOL(DstTy)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00007990 assert(DstTy == MVT::i32 && "Unexpected FP_TO_UINT");
7991 DstTy = MVT::i64;
Eli Friedman948e95a2009-05-23 09:59:16 +00007992 }
7993
Owen Anderson825b72b2009-08-11 20:47:22 +00007994 assert(DstTy.getSimpleVT() <= MVT::i64 &&
7995 DstTy.getSimpleVT() >= MVT::i16 &&
Michael J. Spencer1a2d0612012-02-24 19:01:22 +00007996 "Unknown FP_TO_INT to lower!");
Evan Cheng0db9fe62006-04-25 20:13:52 +00007997
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00007998 // These are really Legal.
Owen Anderson825b72b2009-08-11 20:47:22 +00007999 if (DstTy == MVT::i32 &&
Chris Lattner78631162008-01-16 06:24:21 +00008000 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Dan Gohman475871a2008-07-27 21:46:04 +00008001 return std::make_pair(SDValue(), SDValue());
Dale Johannesen73328d12007-09-19 23:55:34 +00008002 if (Subtarget->is64Bit() &&
Owen Anderson825b72b2009-08-11 20:47:22 +00008003 DstTy == MVT::i64 &&
Eli Friedman36df4992009-05-27 00:47:34 +00008004 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Dan Gohman475871a2008-07-27 21:46:04 +00008005 return std::make_pair(SDValue(), SDValue());
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00008006
Michael J. Spencer1a2d0612012-02-24 19:01:22 +00008007 // We lower FP->int64 either into FISTP64 followed by a load from a temporary
8008 // stack slot, or into the FTOL runtime function.
Evan Cheng87c89352007-10-15 20:11:21 +00008009 MachineFunction &MF = DAG.getMachineFunction();
Eli Friedman948e95a2009-05-23 09:59:16 +00008010 unsigned MemSize = DstTy.getSizeInBits()/8;
David Greene3f2bf852009-11-12 20:49:22 +00008011 int SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize, false);
Dan Gohman475871a2008-07-27 21:46:04 +00008012 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Eric Christopherfd179292009-08-27 18:07:15 +00008013
Evan Cheng0db9fe62006-04-25 20:13:52 +00008014 unsigned Opc;
Michael J. Spencer1a2d0612012-02-24 19:01:22 +00008015 if (!IsSigned && isIntegerTypeFTOL(DstTy))
8016 Opc = X86ISD::WIN_FTOL;
8017 else
8018 switch (DstTy.getSimpleVT().SimpleTy) {
8019 default: llvm_unreachable("Invalid FP_TO_SINT to lower!");
8020 case MVT::i16: Opc = X86ISD::FP_TO_INT16_IN_MEM; break;
8021 case MVT::i32: Opc = X86ISD::FP_TO_INT32_IN_MEM; break;
8022 case MVT::i64: Opc = X86ISD::FP_TO_INT64_IN_MEM; break;
8023 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00008024
Dan Gohman475871a2008-07-27 21:46:04 +00008025 SDValue Chain = DAG.getEntryNode();
8026 SDValue Value = Op.getOperand(0);
Chris Lattner492a43e2010-09-22 01:28:21 +00008027 EVT TheVT = Op.getOperand(0).getValueType();
Michael J. Spencer1a2d0612012-02-24 19:01:22 +00008028 // FIXME This causes a redundant load/store if the SSE-class value is already
8029 // in memory, such as if it is on the callstack.
Chris Lattner492a43e2010-09-22 01:28:21 +00008030 if (isScalarFPTypeInSSEReg(TheVT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00008031 assert(DstTy == MVT::i64 && "Invalid FP_TO_SINT to lower!");
Chris Lattner07290932010-09-22 01:05:16 +00008032 Chain = DAG.getStore(Chain, DL, Value, StackSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00008033 MachinePointerInfo::getFixedStack(SSFI),
David Greene67c9d422010-02-15 16:53:33 +00008034 false, false, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +00008035 SDVTList Tys = DAG.getVTList(Op.getOperand(0).getValueType(), MVT::Other);
Dan Gohman475871a2008-07-27 21:46:04 +00008036 SDValue Ops[] = {
Chris Lattner492a43e2010-09-22 01:28:21 +00008037 Chain, StackSlot, DAG.getValueType(TheVT)
Chris Lattner5a88b832007-02-25 07:10:00 +00008038 };
Michael J. Spencerec38de22010-10-10 22:04:20 +00008039
Chris Lattner492a43e2010-09-22 01:28:21 +00008040 MachineMemOperand *MMO =
8041 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
8042 MachineMemOperand::MOLoad, MemSize, MemSize);
8043 Value = DAG.getMemIntrinsicNode(X86ISD::FLD, DL, Tys, Ops, 3,
8044 DstTy, MMO);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008045 Chain = Value.getValue(1);
David Greene3f2bf852009-11-12 20:49:22 +00008046 SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize, false);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008047 StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
8048 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00008049
Chris Lattner07290932010-09-22 01:05:16 +00008050 MachineMemOperand *MMO =
8051 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
8052 MachineMemOperand::MOStore, MemSize, MemSize);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00008053
Michael J. Spencer1a2d0612012-02-24 19:01:22 +00008054 if (Opc != X86ISD::WIN_FTOL) {
8055 // Build the FP_TO_INT*_IN_MEM
8056 SDValue Ops[] = { Chain, Value, StackSlot };
8057 SDValue FIST = DAG.getMemIntrinsicNode(Opc, DL, DAG.getVTList(MVT::Other),
8058 Ops, 3, DstTy, MMO);
8059 return std::make_pair(FIST, StackSlot);
8060 } else {
8061 SDValue ftol = DAG.getNode(X86ISD::WIN_FTOL, DL,
8062 DAG.getVTList(MVT::Other, MVT::Glue),
8063 Chain, Value);
8064 SDValue eax = DAG.getCopyFromReg(ftol, DL, X86::EAX,
8065 MVT::i32, ftol.getValue(1));
8066 SDValue edx = DAG.getCopyFromReg(eax.getValue(1), DL, X86::EDX,
8067 MVT::i32, eax.getValue(2));
NAKAMURA Takumi9a68fdc2012-02-25 03:37:25 +00008068 SDValue Ops[] = { eax, edx };
8069 SDValue pair = IsReplace
8070 ? DAG.getNode(ISD::BUILD_PAIR, DL, MVT::i64, Ops, 2)
8071 : DAG.getMergeValues(Ops, 2, DL);
Michael J. Spencer1a2d0612012-02-24 19:01:22 +00008072 return std::make_pair(pair, SDValue());
8073 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00008074}
8075
Dan Gohmand858e902010-04-17 15:26:15 +00008076SDValue X86TargetLowering::LowerFP_TO_SINT(SDValue Op,
8077 SelectionDAG &DAG) const {
Dale Johannesen0488fb62010-09-30 23:57:10 +00008078 if (Op.getValueType().isVector())
Eli Friedman23ef1052009-06-06 03:57:58 +00008079 return SDValue();
Eli Friedman23ef1052009-06-06 03:57:58 +00008080
NAKAMURA Takumi9a68fdc2012-02-25 03:37:25 +00008081 std::pair<SDValue,SDValue> Vals = FP_TO_INTHelper(Op, DAG,
8082 /*IsSigned=*/ true, /*IsReplace=*/ false);
Dan Gohman475871a2008-07-27 21:46:04 +00008083 SDValue FIST = Vals.first, StackSlot = Vals.second;
Eli Friedman36df4992009-05-27 00:47:34 +00008084 // If FP_TO_INTHelper failed, the node is actually supposed to be Legal.
8085 if (FIST.getNode() == 0) return Op;
Scott Michelfdc40a02009-02-17 22:15:04 +00008086
Michael J. Spencer1a2d0612012-02-24 19:01:22 +00008087 if (StackSlot.getNode())
8088 // Load the result.
8089 return DAG.getLoad(Op.getValueType(), Op.getDebugLoc(),
8090 FIST, StackSlot, MachinePointerInfo(),
8091 false, false, false, 0);
Craig Topper69947b92012-04-23 06:57:04 +00008092
8093 // The node is the result.
8094 return FIST;
Chris Lattner27a6c732007-11-24 07:07:01 +00008095}
8096
Dan Gohmand858e902010-04-17 15:26:15 +00008097SDValue X86TargetLowering::LowerFP_TO_UINT(SDValue Op,
8098 SelectionDAG &DAG) const {
NAKAMURA Takumi9a68fdc2012-02-25 03:37:25 +00008099 std::pair<SDValue,SDValue> Vals = FP_TO_INTHelper(Op, DAG,
8100 /*IsSigned=*/ false, /*IsReplace=*/ false);
Eli Friedman948e95a2009-05-23 09:59:16 +00008101 SDValue FIST = Vals.first, StackSlot = Vals.second;
8102 assert(FIST.getNode() && "Unexpected failure");
8103
NAKAMURA Takumi9a68fdc2012-02-25 03:37:25 +00008104 if (StackSlot.getNode())
8105 // Load the result.
8106 return DAG.getLoad(Op.getValueType(), Op.getDebugLoc(),
8107 FIST, StackSlot, MachinePointerInfo(),
8108 false, false, false, 0);
Craig Topper69947b92012-04-23 06:57:04 +00008109
8110 // The node is the result.
8111 return FIST;
Eli Friedman948e95a2009-05-23 09:59:16 +00008112}
8113
Dan Gohmand858e902010-04-17 15:26:15 +00008114SDValue X86TargetLowering::LowerFABS(SDValue Op,
8115 SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00008116 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008117 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00008118 EVT VT = Op.getValueType();
8119 EVT EltVT = VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +00008120 if (VT.isVector())
8121 EltVT = VT.getVectorElementType();
Chris Lattner4ca829e2012-01-25 06:02:56 +00008122 Constant *C;
Owen Anderson825b72b2009-08-11 20:47:22 +00008123 if (EltVT == MVT::f64) {
Chad Rosiera20e1e72012-08-01 18:39:17 +00008124 C = ConstantVector::getSplat(2,
Chris Lattner4ca829e2012-01-25 06:02:56 +00008125 ConstantFP::get(*Context, APFloat(APInt(64, ~(1ULL << 63)))));
Evan Cheng0db9fe62006-04-25 20:13:52 +00008126 } else {
Chris Lattner4ca829e2012-01-25 06:02:56 +00008127 C = ConstantVector::getSplat(4,
8128 ConstantFP::get(*Context, APFloat(APInt(32, ~(1U << 31)))));
Evan Cheng0db9fe62006-04-25 20:13:52 +00008129 }
Evan Cheng1606e8e2009-03-13 07:51:59 +00008130 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00008131 SDValue Mask = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00008132 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00008133 false, false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00008134 return DAG.getNode(X86ISD::FAND, dl, VT, Op.getOperand(0), Mask);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008135}
8136
Dan Gohmand858e902010-04-17 15:26:15 +00008137SDValue X86TargetLowering::LowerFNEG(SDValue Op, SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00008138 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008139 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00008140 EVT VT = Op.getValueType();
8141 EVT EltVT = VT;
Chad Rosiera860b182011-12-15 01:02:25 +00008142 unsigned NumElts = VT == MVT::f64 ? 2 : 4;
8143 if (VT.isVector()) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00008144 EltVT = VT.getVectorElementType();
Chad Rosiera860b182011-12-15 01:02:25 +00008145 NumElts = VT.getVectorNumElements();
8146 }
Chris Lattner4ca829e2012-01-25 06:02:56 +00008147 Constant *C;
8148 if (EltVT == MVT::f64)
8149 C = ConstantFP::get(*Context, APFloat(APInt(64, 1ULL << 63)));
8150 else
8151 C = ConstantFP::get(*Context, APFloat(APInt(32, 1U << 31)));
8152 C = ConstantVector::getSplat(NumElts, C);
Evan Cheng1606e8e2009-03-13 07:51:59 +00008153 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00008154 SDValue Mask = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00008155 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00008156 false, false, false, 16);
Duncan Sands83ec4b62008-06-06 12:08:01 +00008157 if (VT.isVector()) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00008158 MVT XORVT = VT.is128BitVector() ? MVT::v2i64 : MVT::v4i64;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008159 return DAG.getNode(ISD::BITCAST, dl, VT,
Chad Rosiera860b182011-12-15 01:02:25 +00008160 DAG.getNode(ISD::XOR, dl, XORVT,
Craig Topper69947b92012-04-23 06:57:04 +00008161 DAG.getNode(ISD::BITCAST, dl, XORVT,
8162 Op.getOperand(0)),
8163 DAG.getNode(ISD::BITCAST, dl, XORVT, Mask)));
Evan Chengd4d01b72007-07-19 23:36:01 +00008164 }
Craig Topper69947b92012-04-23 06:57:04 +00008165
8166 return DAG.getNode(X86ISD::FXOR, dl, VT, Op.getOperand(0), Mask);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008167}
8168
Dan Gohmand858e902010-04-17 15:26:15 +00008169SDValue X86TargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00008170 LLVMContext *Context = DAG.getContext();
Dan Gohman475871a2008-07-27 21:46:04 +00008171 SDValue Op0 = Op.getOperand(0);
8172 SDValue Op1 = Op.getOperand(1);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008173 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00008174 EVT VT = Op.getValueType();
8175 EVT SrcVT = Op1.getValueType();
Evan Cheng73d6cf12007-01-05 21:37:56 +00008176
8177 // If second operand is smaller, extend it first.
Duncan Sands8e4eb092008-06-08 20:54:56 +00008178 if (SrcVT.bitsLT(VT)) {
Dale Johannesenace16102009-02-03 19:33:06 +00008179 Op1 = DAG.getNode(ISD::FP_EXTEND, dl, VT, Op1);
Evan Cheng73d6cf12007-01-05 21:37:56 +00008180 SrcVT = VT;
8181 }
Dale Johannesen61c7ef32007-10-21 01:07:44 +00008182 // And if it is bigger, shrink it first.
Duncan Sands8e4eb092008-06-08 20:54:56 +00008183 if (SrcVT.bitsGT(VT)) {
Dale Johannesenace16102009-02-03 19:33:06 +00008184 Op1 = DAG.getNode(ISD::FP_ROUND, dl, VT, Op1, DAG.getIntPtrConstant(1));
Dale Johannesen61c7ef32007-10-21 01:07:44 +00008185 SrcVT = VT;
Dale Johannesen61c7ef32007-10-21 01:07:44 +00008186 }
8187
8188 // At this point the operands and the result should have the same
8189 // type, and that won't be f80 since that is not custom lowered.
Evan Cheng73d6cf12007-01-05 21:37:56 +00008190
Evan Cheng68c47cb2007-01-05 07:55:56 +00008191 // First get the sign bit of second operand.
Chad Rosier01d426e2011-12-15 01:16:09 +00008192 SmallVector<Constant*,4> CV;
Owen Anderson825b72b2009-08-11 20:47:22 +00008193 if (SrcVT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00008194 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 1ULL << 63))));
8195 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 0))));
Evan Cheng68c47cb2007-01-05 07:55:56 +00008196 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00008197 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 1U << 31))));
8198 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
8199 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
8200 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
Evan Cheng68c47cb2007-01-05 07:55:56 +00008201 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00008202 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00008203 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00008204 SDValue Mask1 = DAG.getLoad(SrcVT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00008205 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00008206 false, false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00008207 SDValue SignBit = DAG.getNode(X86ISD::FAND, dl, SrcVT, Op1, Mask1);
Evan Cheng68c47cb2007-01-05 07:55:56 +00008208
8209 // Shift sign bit right or left if the two operands have different types.
Duncan Sands8e4eb092008-06-08 20:54:56 +00008210 if (SrcVT.bitsGT(VT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00008211 // Op0 is MVT::f32, Op1 is MVT::f64.
8212 SignBit = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f64, SignBit);
8213 SignBit = DAG.getNode(X86ISD::FSRL, dl, MVT::v2f64, SignBit,
8214 DAG.getConstant(32, MVT::i32));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008215 SignBit = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, SignBit);
Owen Anderson825b72b2009-08-11 20:47:22 +00008216 SignBit = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f32, SignBit,
Chris Lattner0bd48932008-01-17 07:00:52 +00008217 DAG.getIntPtrConstant(0));
Evan Cheng68c47cb2007-01-05 07:55:56 +00008218 }
8219
Evan Cheng73d6cf12007-01-05 21:37:56 +00008220 // Clear first operand sign bit.
8221 CV.clear();
Owen Anderson825b72b2009-08-11 20:47:22 +00008222 if (VT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00008223 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, ~(1ULL << 63)))));
8224 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 0))));
Evan Cheng73d6cf12007-01-05 21:37:56 +00008225 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00008226 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, ~(1U << 31)))));
8227 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
8228 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
8229 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
Evan Cheng73d6cf12007-01-05 21:37:56 +00008230 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00008231 C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00008232 CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00008233 SDValue Mask2 = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00008234 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00008235 false, false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00008236 SDValue Val = DAG.getNode(X86ISD::FAND, dl, VT, Op0, Mask2);
Evan Cheng73d6cf12007-01-05 21:37:56 +00008237
8238 // Or the value with the sign bit.
Dale Johannesenace16102009-02-03 19:33:06 +00008239 return DAG.getNode(X86ISD::FOR, dl, VT, Val, SignBit);
Evan Cheng68c47cb2007-01-05 07:55:56 +00008240}
8241
Stuart Hastings4fd0dee2011-06-01 04:39:42 +00008242SDValue X86TargetLowering::LowerFGETSIGN(SDValue Op, SelectionDAG &DAG) const {
8243 SDValue N0 = Op.getOperand(0);
8244 DebugLoc dl = Op.getDebugLoc();
8245 EVT VT = Op.getValueType();
8246
8247 // Lower ISD::FGETSIGN to (AND (X86ISD::FGETSIGNx86 ...) 1).
8248 SDValue xFGETSIGN = DAG.getNode(X86ISD::FGETSIGNx86, dl, VT, N0,
8249 DAG.getConstant(1, VT));
8250 return DAG.getNode(ISD::AND, dl, VT, xFGETSIGN, DAG.getConstant(1, VT));
8251}
8252
Dan Gohman076aee32009-03-04 19:44:21 +00008253/// Emit nodes that will be selected as "test Op0,Op0", or something
8254/// equivalent.
Dan Gohman31125812009-03-07 01:58:32 +00008255SDValue X86TargetLowering::EmitTest(SDValue Op, unsigned X86CC,
Evan Cheng552f09a2010-04-26 19:06:11 +00008256 SelectionDAG &DAG) const {
Dan Gohman076aee32009-03-04 19:44:21 +00008257 DebugLoc dl = Op.getDebugLoc();
8258
Dan Gohman31125812009-03-07 01:58:32 +00008259 // CF and OF aren't always set the way we want. Determine which
8260 // of these we need.
8261 bool NeedCF = false;
8262 bool NeedOF = false;
8263 switch (X86CC) {
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008264 default: break;
Dan Gohman31125812009-03-07 01:58:32 +00008265 case X86::COND_A: case X86::COND_AE:
8266 case X86::COND_B: case X86::COND_BE:
8267 NeedCF = true;
8268 break;
8269 case X86::COND_G: case X86::COND_GE:
8270 case X86::COND_L: case X86::COND_LE:
8271 case X86::COND_O: case X86::COND_NO:
8272 NeedOF = true;
8273 break;
Dan Gohman31125812009-03-07 01:58:32 +00008274 }
8275
Dan Gohman076aee32009-03-04 19:44:21 +00008276 // See if we can use the EFLAGS value from the operand instead of
Dan Gohman31125812009-03-07 01:58:32 +00008277 // doing a separate TEST. TEST always sets OF and CF to 0, so unless
8278 // we prove that the arithmetic won't overflow, we can't use OF or CF.
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008279 if (Op.getResNo() != 0 || NeedOF || NeedCF)
8280 // Emit a CMP with 0, which is the TEST pattern.
8281 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op,
8282 DAG.getConstant(0, Op.getValueType()));
8283
8284 unsigned Opcode = 0;
8285 unsigned NumOperands = 0;
Nadav Rotemd5c66a02012-08-18 05:02:36 +00008286 switch (Op.getNode()->getOpcode()) {
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008287 case ISD::ADD:
8288 // Due to an isel shortcoming, be conservative if this add is likely to be
8289 // selected as part of a load-modify-store instruction. When the root node
8290 // in a match is a store, isel doesn't know how to remap non-chain non-flag
8291 // uses of other nodes in the match, such as the ADD in this case. This
8292 // leads to the ADD being left around and reselected, with the result being
8293 // two adds in the output. Alas, even if none our users are stores, that
8294 // doesn't prove we're O.K. Ergo, if we have any parents that aren't
8295 // CopyToReg or SETCC, eschew INC/DEC. A better fix seems to require
8296 // climbing the DAG back to the root, and it doesn't seem to be worth the
8297 // effort.
8298 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
Pete Cooper2d496892011-11-15 21:57:53 +00008299 UE = Op.getNode()->use_end(); UI != UE; ++UI)
8300 if (UI->getOpcode() != ISD::CopyToReg &&
8301 UI->getOpcode() != ISD::SETCC &&
8302 UI->getOpcode() != ISD::STORE)
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008303 goto default_case;
8304
8305 if (ConstantSDNode *C =
8306 dyn_cast<ConstantSDNode>(Op.getNode()->getOperand(1))) {
8307 // An add of one will be selected as an INC.
8308 if (C->getAPIntValue() == 1) {
8309 Opcode = X86ISD::INC;
8310 NumOperands = 1;
8311 break;
Dan Gohmane220c4b2009-09-18 19:59:53 +00008312 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008313
8314 // An add of negative one (subtract of one) will be selected as a DEC.
8315 if (C->getAPIntValue().isAllOnesValue()) {
8316 Opcode = X86ISD::DEC;
8317 NumOperands = 1;
8318 break;
8319 }
Dan Gohman076aee32009-03-04 19:44:21 +00008320 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008321
8322 // Otherwise use a regular EFLAGS-setting add.
8323 Opcode = X86ISD::ADD;
8324 NumOperands = 2;
8325 break;
8326 case ISD::AND: {
8327 // If the primary and result isn't used, don't bother using X86ISD::AND,
8328 // because a TEST instruction will be better.
8329 bool NonFlagUse = false;
8330 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
8331 UE = Op.getNode()->use_end(); UI != UE; ++UI) {
8332 SDNode *User = *UI;
8333 unsigned UOpNo = UI.getOperandNo();
8334 if (User->getOpcode() == ISD::TRUNCATE && User->hasOneUse()) {
8335 // Look pass truncate.
8336 UOpNo = User->use_begin().getOperandNo();
8337 User = *User->use_begin();
8338 }
8339
8340 if (User->getOpcode() != ISD::BRCOND &&
8341 User->getOpcode() != ISD::SETCC &&
Nadav Rotemd5c66a02012-08-18 05:02:36 +00008342 (User->getOpcode() != ISD::SELECT || UOpNo != 0)) {
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008343 NonFlagUse = true;
8344 break;
8345 }
Dan Gohman076aee32009-03-04 19:44:21 +00008346 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008347
8348 if (!NonFlagUse)
8349 break;
8350 }
8351 // FALL THROUGH
8352 case ISD::SUB:
8353 case ISD::OR:
8354 case ISD::XOR:
8355 // Due to the ISEL shortcoming noted above, be conservative if this op is
8356 // likely to be selected as part of a load-modify-store instruction.
8357 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
8358 UE = Op.getNode()->use_end(); UI != UE; ++UI)
8359 if (UI->getOpcode() == ISD::STORE)
8360 goto default_case;
8361
8362 // Otherwise use a regular EFLAGS-setting instruction.
Nadav Rotemd5c66a02012-08-18 05:02:36 +00008363 switch (Op.getNode()->getOpcode()) {
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008364 default: llvm_unreachable("unexpected operator!");
Nadav Rotemd5c66a02012-08-18 05:02:36 +00008365 case ISD::SUB:
8366 Opcode = X86ISD::SUB;
8367 break;
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008368 case ISD::OR: Opcode = X86ISD::OR; break;
8369 case ISD::XOR: Opcode = X86ISD::XOR; break;
8370 case ISD::AND: Opcode = X86ISD::AND; break;
8371 }
8372
8373 NumOperands = 2;
8374 break;
8375 case X86ISD::ADD:
8376 case X86ISD::SUB:
8377 case X86ISD::INC:
8378 case X86ISD::DEC:
8379 case X86ISD::OR:
8380 case X86ISD::XOR:
8381 case X86ISD::AND:
8382 return SDValue(Op.getNode(), 1);
8383 default:
8384 default_case:
8385 break;
Dan Gohman076aee32009-03-04 19:44:21 +00008386 }
8387
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008388 if (Opcode == 0)
8389 // Emit a CMP with 0, which is the TEST pattern.
8390 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op,
8391 DAG.getConstant(0, Op.getValueType()));
8392
Manman Ren87253c22012-06-07 00:42:47 +00008393 if (Opcode == X86ISD::CMP) {
8394 SDValue New = DAG.getNode(Opcode, dl, MVT::i32, Op.getOperand(0),
8395 Op.getOperand(1));
Manman Rene6fc9d42012-06-07 19:27:33 +00008396 // We can't replace usage of SUB with CMP.
8397 // The SUB node will be removed later because there is no use of it.
Manman Ren87253c22012-06-07 00:42:47 +00008398 return SDValue(New.getNode(), 0);
8399 }
8400
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008401 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::i32);
8402 SmallVector<SDValue, 4> Ops;
8403 for (unsigned i = 0; i != NumOperands; ++i)
8404 Ops.push_back(Op.getOperand(i));
8405
8406 SDValue New = DAG.getNode(Opcode, dl, VTs, &Ops[0], NumOperands);
8407 DAG.ReplaceAllUsesWith(Op, New);
8408 return SDValue(New.getNode(), 1);
Dan Gohman076aee32009-03-04 19:44:21 +00008409}
8410
8411/// Emit nodes that will be selected as "cmp Op0,Op1", or something
8412/// equivalent.
Dan Gohman31125812009-03-07 01:58:32 +00008413SDValue X86TargetLowering::EmitCmp(SDValue Op0, SDValue Op1, unsigned X86CC,
Evan Cheng552f09a2010-04-26 19:06:11 +00008414 SelectionDAG &DAG) const {
Dan Gohman076aee32009-03-04 19:44:21 +00008415 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op1))
8416 if (C->getAPIntValue() == 0)
Evan Cheng552f09a2010-04-26 19:06:11 +00008417 return EmitTest(Op0, X86CC, DAG);
Dan Gohman076aee32009-03-04 19:44:21 +00008418
8419 DebugLoc dl = Op0.getDebugLoc();
Manman Ren39ad5682012-08-08 00:51:41 +00008420 if ((Op0.getValueType() == MVT::i8 || Op0.getValueType() == MVT::i16 ||
8421 Op0.getValueType() == MVT::i32 || Op0.getValueType() == MVT::i64)) {
8422 // Use SUB instead of CMP to enable CSE between SUB and CMP.
8423 SDVTList VTs = DAG.getVTList(Op0.getValueType(), MVT::i32);
8424 SDValue Sub = DAG.getNode(X86ISD::SUB, dl, VTs,
8425 Op0, Op1);
8426 return SDValue(Sub.getNode(), 1);
8427 }
Owen Anderson825b72b2009-08-11 20:47:22 +00008428 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op0, Op1);
Dan Gohman076aee32009-03-04 19:44:21 +00008429}
8430
Benjamin Kramer17c836c2012-04-27 12:07:43 +00008431/// Convert a comparison if required by the subtarget.
8432SDValue X86TargetLowering::ConvertCmpIfNecessary(SDValue Cmp,
8433 SelectionDAG &DAG) const {
8434 // If the subtarget does not support the FUCOMI instruction, floating-point
8435 // comparisons have to be converted.
8436 if (Subtarget->hasCMov() ||
8437 Cmp.getOpcode() != X86ISD::CMP ||
8438 !Cmp.getOperand(0).getValueType().isFloatingPoint() ||
8439 !Cmp.getOperand(1).getValueType().isFloatingPoint())
8440 return Cmp;
8441
8442 // The instruction selector will select an FUCOM instruction instead of
8443 // FUCOMI, which writes the comparison result to FPSW instead of EFLAGS. Hence
8444 // build an SDNode sequence that transfers the result from FPSW into EFLAGS:
8445 // (X86sahf (trunc (srl (X86fp_stsw (trunc (X86cmp ...)), 8))))
8446 DebugLoc dl = Cmp.getDebugLoc();
8447 SDValue TruncFPSW = DAG.getNode(ISD::TRUNCATE, dl, MVT::i16, Cmp);
8448 SDValue FNStSW = DAG.getNode(X86ISD::FNSTSW16r, dl, MVT::i16, TruncFPSW);
8449 SDValue Srl = DAG.getNode(ISD::SRL, dl, MVT::i16, FNStSW,
8450 DAG.getConstant(8, MVT::i8));
8451 SDValue TruncSrl = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Srl);
8452 return DAG.getNode(X86ISD::SAHF, dl, MVT::i32, TruncSrl);
8453}
8454
Evan Chengd40d03e2010-01-06 19:38:29 +00008455/// LowerToBT - Result of 'and' is compared against zero. Turn it into a BT node
8456/// if it's possible.
Evan Cheng5528e7b2010-04-21 01:47:12 +00008457SDValue X86TargetLowering::LowerToBT(SDValue And, ISD::CondCode CC,
8458 DebugLoc dl, SelectionDAG &DAG) const {
Evan Cheng2c755ba2010-02-27 07:36:59 +00008459 SDValue Op0 = And.getOperand(0);
8460 SDValue Op1 = And.getOperand(1);
8461 if (Op0.getOpcode() == ISD::TRUNCATE)
8462 Op0 = Op0.getOperand(0);
8463 if (Op1.getOpcode() == ISD::TRUNCATE)
8464 Op1 = Op1.getOperand(0);
8465
Evan Chengd40d03e2010-01-06 19:38:29 +00008466 SDValue LHS, RHS;
Dan Gohman6b13cbc2010-06-24 02:07:59 +00008467 if (Op1.getOpcode() == ISD::SHL)
8468 std::swap(Op0, Op1);
8469 if (Op0.getOpcode() == ISD::SHL) {
Evan Cheng2c755ba2010-02-27 07:36:59 +00008470 if (ConstantSDNode *And00C = dyn_cast<ConstantSDNode>(Op0.getOperand(0)))
8471 if (And00C->getZExtValue() == 1) {
Dan Gohman6b13cbc2010-06-24 02:07:59 +00008472 // If we looked past a truncate, check that it's only truncating away
8473 // known zeros.
8474 unsigned BitWidth = Op0.getValueSizeInBits();
8475 unsigned AndBitWidth = And.getValueSizeInBits();
8476 if (BitWidth > AndBitWidth) {
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00008477 APInt Zeros, Ones;
8478 DAG.ComputeMaskedBits(Op0, Zeros, Ones);
Dan Gohman6b13cbc2010-06-24 02:07:59 +00008479 if (Zeros.countLeadingOnes() < BitWidth - AndBitWidth)
8480 return SDValue();
8481 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00008482 LHS = Op1;
8483 RHS = Op0.getOperand(1);
Evan Chengd40d03e2010-01-06 19:38:29 +00008484 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00008485 } else if (Op1.getOpcode() == ISD::Constant) {
8486 ConstantSDNode *AndRHS = cast<ConstantSDNode>(Op1);
Benjamin Kramerf238f502011-11-23 13:54:17 +00008487 uint64_t AndRHSVal = AndRHS->getZExtValue();
Evan Cheng2c755ba2010-02-27 07:36:59 +00008488 SDValue AndLHS = Op0;
Benjamin Kramerf238f502011-11-23 13:54:17 +00008489
8490 if (AndRHSVal == 1 && AndLHS.getOpcode() == ISD::SRL) {
Evan Chengd40d03e2010-01-06 19:38:29 +00008491 LHS = AndLHS.getOperand(0);
8492 RHS = AndLHS.getOperand(1);
Dan Gohmane5af2d32009-01-29 01:59:02 +00008493 }
Benjamin Kramerf238f502011-11-23 13:54:17 +00008494
8495 // Use BT if the immediate can't be encoded in a TEST instruction.
8496 if (!isUInt<32>(AndRHSVal) && isPowerOf2_64(AndRHSVal)) {
8497 LHS = AndLHS;
8498 RHS = DAG.getConstant(Log2_64_Ceil(AndRHSVal), LHS.getValueType());
8499 }
Evan Chengd40d03e2010-01-06 19:38:29 +00008500 }
Evan Cheng0488db92007-09-25 01:57:46 +00008501
Evan Chengd40d03e2010-01-06 19:38:29 +00008502 if (LHS.getNode()) {
Evan Chenge5b51ac2010-04-17 06:13:15 +00008503 // If LHS is i8, promote it to i32 with any_extend. There is no i8 BT
Evan Chengd40d03e2010-01-06 19:38:29 +00008504 // instruction. Since the shift amount is in-range-or-undefined, we know
Evan Chenge5b51ac2010-04-17 06:13:15 +00008505 // that doing a bittest on the i32 value is ok. We extend to i32 because
Evan Chengd40d03e2010-01-06 19:38:29 +00008506 // the encoding for the i16 version is larger than the i32 version.
Evan Chenge5b51ac2010-04-17 06:13:15 +00008507 // Also promote i16 to i32 for performance / code size reason.
8508 if (LHS.getValueType() == MVT::i8 ||
Evan Cheng2bce5f4b2010-04-28 08:30:49 +00008509 LHS.getValueType() == MVT::i16)
Evan Chengd40d03e2010-01-06 19:38:29 +00008510 LHS = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, LHS);
Chris Lattnere55484e2008-12-25 05:34:37 +00008511
Evan Chengd40d03e2010-01-06 19:38:29 +00008512 // If the operand types disagree, extend the shift amount to match. Since
8513 // BT ignores high bits (like shifts) we can use anyextend.
8514 if (LHS.getValueType() != RHS.getValueType())
8515 RHS = DAG.getNode(ISD::ANY_EXTEND, dl, LHS.getValueType(), RHS);
Dan Gohmane5af2d32009-01-29 01:59:02 +00008516
Evan Chengd40d03e2010-01-06 19:38:29 +00008517 SDValue BT = DAG.getNode(X86ISD::BT, dl, MVT::i32, LHS, RHS);
8518 unsigned Cond = CC == ISD::SETEQ ? X86::COND_AE : X86::COND_B;
8519 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
8520 DAG.getConstant(Cond, MVT::i8), BT);
Chris Lattnere55484e2008-12-25 05:34:37 +00008521 }
8522
Evan Cheng54de3ea2010-01-05 06:52:31 +00008523 return SDValue();
8524}
8525
Dan Gohmand858e902010-04-17 15:26:15 +00008526SDValue X86TargetLowering::LowerSETCC(SDValue Op, SelectionDAG &DAG) const {
Duncan Sands28b77e92011-09-06 19:07:46 +00008527
8528 if (Op.getValueType().isVector()) return LowerVSETCC(Op, DAG);
8529
Evan Cheng54de3ea2010-01-05 06:52:31 +00008530 assert(Op.getValueType() == MVT::i8 && "SetCC type must be 8-bit integer");
8531 SDValue Op0 = Op.getOperand(0);
8532 SDValue Op1 = Op.getOperand(1);
8533 DebugLoc dl = Op.getDebugLoc();
8534 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
8535
8536 // Optimize to BT if possible.
Evan Chengd40d03e2010-01-06 19:38:29 +00008537 // Lower (X & (1 << N)) == 0 to BT(X, N).
8538 // Lower ((X >>u N) & 1) != 0 to BT(X, N).
8539 // Lower ((X >>s N) & 1) != 0 to BT(X, N).
Andrew Trickf6c39412011-03-23 23:11:02 +00008540 if (Op0.getOpcode() == ISD::AND && Op0.hasOneUse() &&
Evan Chengd40d03e2010-01-06 19:38:29 +00008541 Op1.getOpcode() == ISD::Constant &&
Dan Gohmane368b462010-06-18 14:22:04 +00008542 cast<ConstantSDNode>(Op1)->isNullValue() &&
Evan Chengd40d03e2010-01-06 19:38:29 +00008543 (CC == ISD::SETEQ || CC == ISD::SETNE)) {
8544 SDValue NewSetCC = LowerToBT(Op0, CC, dl, DAG);
8545 if (NewSetCC.getNode())
8546 return NewSetCC;
8547 }
Evan Cheng54de3ea2010-01-05 06:52:31 +00008548
Chris Lattner481eebc2010-12-19 21:23:48 +00008549 // Look for X == 0, X == 1, X != 0, or X != 1. We can simplify some forms of
8550 // these.
8551 if (Op1.getOpcode() == ISD::Constant &&
Andrew Trickf6c39412011-03-23 23:11:02 +00008552 (cast<ConstantSDNode>(Op1)->getZExtValue() == 1 ||
Evan Cheng2c755ba2010-02-27 07:36:59 +00008553 cast<ConstantSDNode>(Op1)->isNullValue()) &&
8554 (CC == ISD::SETEQ || CC == ISD::SETNE)) {
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008555
Chris Lattner481eebc2010-12-19 21:23:48 +00008556 // If the input is a setcc, then reuse the input setcc or use a new one with
8557 // the inverted condition.
8558 if (Op0.getOpcode() == X86ISD::SETCC) {
8559 X86::CondCode CCode = (X86::CondCode)Op0.getConstantOperandVal(0);
8560 bool Invert = (CC == ISD::SETNE) ^
8561 cast<ConstantSDNode>(Op1)->isNullValue();
8562 if (!Invert) return Op0;
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008563
Evan Cheng2c755ba2010-02-27 07:36:59 +00008564 CCode = X86::GetOppositeBranchCondition(CCode);
Chris Lattner481eebc2010-12-19 21:23:48 +00008565 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
8566 DAG.getConstant(CCode, MVT::i8), Op0.getOperand(1));
8567 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00008568 }
8569
Evan Chenge5b51ac2010-04-17 06:13:15 +00008570 bool isFP = Op1.getValueType().isFloatingPoint();
Chris Lattnere55484e2008-12-25 05:34:37 +00008571 unsigned X86CC = TranslateX86CC(CC, isFP, Op0, Op1, DAG);
Dan Gohman1a492952009-10-20 16:22:37 +00008572 if (X86CC == X86::COND_INVALID)
8573 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00008574
Chris Lattnerc19d1c32010-12-19 22:08:31 +00008575 SDValue EFLAGS = EmitCmp(Op0, Op1, X86CC, DAG);
Benjamin Kramer17c836c2012-04-27 12:07:43 +00008576 EFLAGS = ConvertCmpIfNecessary(EFLAGS, DAG);
Owen Anderson825b72b2009-08-11 20:47:22 +00008577 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
Chris Lattnerc19d1c32010-12-19 22:08:31 +00008578 DAG.getConstant(X86CC, MVT::i8), EFLAGS);
Evan Cheng0488db92007-09-25 01:57:46 +00008579}
8580
Craig Topper89af15e2011-09-18 08:03:58 +00008581// Lower256IntVSETCC - Break a VSETCC 256-bit integer VSETCC into two new 128
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00008582// ones, and then concatenate the result back.
Craig Topper89af15e2011-09-18 08:03:58 +00008583static SDValue Lower256IntVSETCC(SDValue Op, SelectionDAG &DAG) {
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00008584 EVT VT = Op.getValueType();
8585
Craig Topper7a9a28b2012-08-12 02:23:29 +00008586 assert(VT.is256BitVector() && Op.getOpcode() == ISD::SETCC &&
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00008587 "Unsupported value type for operation");
8588
Craig Topper66ddd152012-04-27 22:54:43 +00008589 unsigned NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00008590 DebugLoc dl = Op.getDebugLoc();
8591 SDValue CC = Op.getOperand(2);
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00008592
8593 // Extract the LHS vectors
8594 SDValue LHS = Op.getOperand(0);
Craig Topperb14940a2012-04-22 20:55:18 +00008595 SDValue LHS1 = Extract128BitVector(LHS, 0, DAG, dl);
8596 SDValue LHS2 = Extract128BitVector(LHS, NumElems/2, DAG, dl);
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00008597
8598 // Extract the RHS vectors
8599 SDValue RHS = Op.getOperand(1);
Craig Topperb14940a2012-04-22 20:55:18 +00008600 SDValue RHS1 = Extract128BitVector(RHS, 0, DAG, dl);
8601 SDValue RHS2 = Extract128BitVector(RHS, NumElems/2, DAG, dl);
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00008602
8603 // Issue the operation on the smaller types and concatenate the result back
8604 MVT EltVT = VT.getVectorElementType().getSimpleVT();
8605 EVT NewVT = MVT::getVectorVT(EltVT, NumElems/2);
8606 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT,
8607 DAG.getNode(Op.getOpcode(), dl, NewVT, LHS1, RHS1, CC),
8608 DAG.getNode(Op.getOpcode(), dl, NewVT, LHS2, RHS2, CC));
8609}
8610
8611
Dan Gohmand858e902010-04-17 15:26:15 +00008612SDValue X86TargetLowering::LowerVSETCC(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +00008613 SDValue Cond;
8614 SDValue Op0 = Op.getOperand(0);
8615 SDValue Op1 = Op.getOperand(1);
8616 SDValue CC = Op.getOperand(2);
Owen Andersone50ed302009-08-10 22:56:29 +00008617 EVT VT = Op.getValueType();
Nate Begeman30a0de92008-07-17 16:51:19 +00008618 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
8619 bool isFP = Op.getOperand(1).getValueType().isFloatingPoint();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008620 DebugLoc dl = Op.getDebugLoc();
Nate Begeman30a0de92008-07-17 16:51:19 +00008621
8622 if (isFP) {
Craig Topper523908d2012-08-13 02:34:03 +00008623#ifndef NDEBUG
Bruno Cardoso Lopes0f0e0a02011-08-09 00:46:57 +00008624 EVT EltVT = Op0.getValueType().getVectorElementType();
Craig Topper523908d2012-08-13 02:34:03 +00008625 assert(EltVT == MVT::f32 || EltVT == MVT::f64);
8626#endif
Bruno Cardoso Lopes0f0e0a02011-08-09 00:46:57 +00008627
Craig Topper523908d2012-08-13 02:34:03 +00008628 unsigned SSECC;
Nate Begeman30a0de92008-07-17 16:51:19 +00008629 bool Swap = false;
8630
Bruno Cardoso Lopes8e03a822011-09-12 19:30:40 +00008631 // SSE Condition code mapping:
8632 // 0 - EQ
8633 // 1 - LT
8634 // 2 - LE
8635 // 3 - UNORD
8636 // 4 - NEQ
8637 // 5 - NLT
8638 // 6 - NLE
8639 // 7 - ORD
Nate Begeman30a0de92008-07-17 16:51:19 +00008640 switch (SetCCOpcode) {
Craig Topper2f1b2ec2012-08-13 03:42:38 +00008641 default: llvm_unreachable("Unexpected SETCC condition");
Nate Begemanfb8ead02008-07-25 19:05:58 +00008642 case ISD::SETOEQ:
Nate Begeman30a0de92008-07-17 16:51:19 +00008643 case ISD::SETEQ: SSECC = 0; break;
Bruno Cardoso Lopes8e03a822011-09-12 19:30:40 +00008644 case ISD::SETOGT:
8645 case ISD::SETGT: Swap = true; // Fallthrough
Bruno Cardoso Lopes457d53d2011-09-12 21:24:07 +00008646 case ISD::SETLT:
8647 case ISD::SETOLT: SSECC = 1; break;
8648 case ISD::SETOGE:
8649 case ISD::SETGE: Swap = true; // Fallthrough
Nate Begeman30a0de92008-07-17 16:51:19 +00008650 case ISD::SETLE:
8651 case ISD::SETOLE: SSECC = 2; break;
8652 case ISD::SETUO: SSECC = 3; break;
Nate Begemanfb8ead02008-07-25 19:05:58 +00008653 case ISD::SETUNE:
Nate Begeman30a0de92008-07-17 16:51:19 +00008654 case ISD::SETNE: SSECC = 4; break;
Craig Topper523908d2012-08-13 02:34:03 +00008655 case ISD::SETULE: Swap = true; // Fallthrough
Nate Begeman30a0de92008-07-17 16:51:19 +00008656 case ISD::SETUGE: SSECC = 5; break;
Craig Topper523908d2012-08-13 02:34:03 +00008657 case ISD::SETULT: Swap = true; // Fallthrough
Nate Begeman30a0de92008-07-17 16:51:19 +00008658 case ISD::SETUGT: SSECC = 6; break;
8659 case ISD::SETO: SSECC = 7; break;
Craig Topper523908d2012-08-13 02:34:03 +00008660 case ISD::SETUEQ:
8661 case ISD::SETONE: SSECC = 8; break;
Nate Begeman30a0de92008-07-17 16:51:19 +00008662 }
8663 if (Swap)
8664 std::swap(Op0, Op1);
8665
Nate Begemanfb8ead02008-07-25 19:05:58 +00008666 // In the two special cases we can't handle, emit two comparisons.
Nate Begeman30a0de92008-07-17 16:51:19 +00008667 if (SSECC == 8) {
Craig Topper523908d2012-08-13 02:34:03 +00008668 unsigned CC0, CC1;
8669 unsigned CombineOpc;
Nate Begemanfb8ead02008-07-25 19:05:58 +00008670 if (SetCCOpcode == ISD::SETUEQ) {
Craig Topper523908d2012-08-13 02:34:03 +00008671 CC0 = 3; CC1 = 0; CombineOpc = ISD::OR;
8672 } else {
8673 assert(SetCCOpcode == ISD::SETONE);
8674 CC0 = 7; CC1 = 4; CombineOpc = ISD::AND;
Craig Topper69947b92012-04-23 06:57:04 +00008675 }
Craig Topper523908d2012-08-13 02:34:03 +00008676
8677 SDValue Cmp0 = DAG.getNode(X86ISD::CMPP, dl, VT, Op0, Op1,
8678 DAG.getConstant(CC0, MVT::i8));
8679 SDValue Cmp1 = DAG.getNode(X86ISD::CMPP, dl, VT, Op0, Op1,
8680 DAG.getConstant(CC1, MVT::i8));
8681 return DAG.getNode(CombineOpc, dl, VT, Cmp0, Cmp1);
Nate Begeman30a0de92008-07-17 16:51:19 +00008682 }
8683 // Handle all other FP comparisons here.
Craig Topper1906d322012-01-22 23:36:02 +00008684 return DAG.getNode(X86ISD::CMPP, dl, VT, Op0, Op1,
8685 DAG.getConstant(SSECC, MVT::i8));
Nate Begeman30a0de92008-07-17 16:51:19 +00008686 }
Scott Michelfdc40a02009-02-17 22:15:04 +00008687
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00008688 // Break 256-bit integer vector compare into smaller ones.
Craig Topper7a9a28b2012-08-12 02:23:29 +00008689 if (VT.is256BitVector() && !Subtarget->hasAVX2())
Craig Topper89af15e2011-09-18 08:03:58 +00008690 return Lower256IntVSETCC(Op, DAG);
Bruno Cardoso Lopes0f0e0a02011-08-09 00:46:57 +00008691
Nate Begeman30a0de92008-07-17 16:51:19 +00008692 // We are handling one of the integer comparisons here. Since SSE only has
8693 // GT and EQ comparisons for integer, swapping operands and multiple
8694 // operations may be required for some comparisons.
Craig Topper2f1b2ec2012-08-13 03:42:38 +00008695 unsigned Opc;
Nate Begeman30a0de92008-07-17 16:51:19 +00008696 bool Swap = false, Invert = false, FlipSigns = false;
Scott Michelfdc40a02009-02-17 22:15:04 +00008697
Nate Begeman30a0de92008-07-17 16:51:19 +00008698 switch (SetCCOpcode) {
Craig Topper2f1b2ec2012-08-13 03:42:38 +00008699 default: llvm_unreachable("Unexpected SETCC condition");
Nate Begeman30a0de92008-07-17 16:51:19 +00008700 case ISD::SETNE: Invert = true;
Craig Topper67609fd2012-01-22 22:42:16 +00008701 case ISD::SETEQ: Opc = X86ISD::PCMPEQ; break;
Nate Begeman30a0de92008-07-17 16:51:19 +00008702 case ISD::SETLT: Swap = true;
Craig Topper67609fd2012-01-22 22:42:16 +00008703 case ISD::SETGT: Opc = X86ISD::PCMPGT; break;
Nate Begeman30a0de92008-07-17 16:51:19 +00008704 case ISD::SETGE: Swap = true;
Craig Topper67609fd2012-01-22 22:42:16 +00008705 case ISD::SETLE: Opc = X86ISD::PCMPGT; Invert = true; break;
Nate Begeman30a0de92008-07-17 16:51:19 +00008706 case ISD::SETULT: Swap = true;
Craig Topper67609fd2012-01-22 22:42:16 +00008707 case ISD::SETUGT: Opc = X86ISD::PCMPGT; FlipSigns = true; break;
Nate Begeman30a0de92008-07-17 16:51:19 +00008708 case ISD::SETUGE: Swap = true;
Craig Topper67609fd2012-01-22 22:42:16 +00008709 case ISD::SETULE: Opc = X86ISD::PCMPGT; FlipSigns = true; Invert = true; break;
Nate Begeman30a0de92008-07-17 16:51:19 +00008710 }
8711 if (Swap)
8712 std::swap(Op0, Op1);
Scott Michelfdc40a02009-02-17 22:15:04 +00008713
Eli Friedman7d3e2b72011-09-28 21:00:25 +00008714 // Check that the operation in question is available (most are plain SSE2,
8715 // but PCMPGTQ and PCMPEQQ have different requirements).
Craig Topper2f1b2ec2012-08-13 03:42:38 +00008716 if (VT == MVT::v2i64) {
8717 if (Opc == X86ISD::PCMPGT && !Subtarget->hasSSE42())
8718 return SDValue();
8719 if (Opc == X86ISD::PCMPEQ && !Subtarget->hasSSE41())
8720 return SDValue();
8721 }
Eli Friedman7d3e2b72011-09-28 21:00:25 +00008722
Nate Begeman30a0de92008-07-17 16:51:19 +00008723 // Since SSE has no unsigned integer comparisons, we need to flip the sign
8724 // bits of the inputs before performing those operations.
8725 if (FlipSigns) {
Owen Andersone50ed302009-08-10 22:56:29 +00008726 EVT EltVT = VT.getVectorElementType();
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00008727 SDValue SignBit = DAG.getConstant(APInt::getSignBit(EltVT.getSizeInBits()),
8728 EltVT);
Dan Gohman475871a2008-07-27 21:46:04 +00008729 std::vector<SDValue> SignBits(VT.getVectorNumElements(), SignBit);
Evan Chenga87008d2009-02-25 22:49:59 +00008730 SDValue SignVec = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &SignBits[0],
8731 SignBits.size());
Dale Johannesenace16102009-02-03 19:33:06 +00008732 Op0 = DAG.getNode(ISD::XOR, dl, VT, Op0, SignVec);
8733 Op1 = DAG.getNode(ISD::XOR, dl, VT, Op1, SignVec);
Nate Begeman30a0de92008-07-17 16:51:19 +00008734 }
Scott Michelfdc40a02009-02-17 22:15:04 +00008735
Dale Johannesenace16102009-02-03 19:33:06 +00008736 SDValue Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
Nate Begeman30a0de92008-07-17 16:51:19 +00008737
8738 // If the logical-not of the result is required, perform that now.
Bob Wilson4c245462009-01-22 17:39:32 +00008739 if (Invert)
Dale Johannesenace16102009-02-03 19:33:06 +00008740 Result = DAG.getNOT(dl, Result, VT);
Bob Wilson4c245462009-01-22 17:39:32 +00008741
Nate Begeman30a0de92008-07-17 16:51:19 +00008742 return Result;
8743}
Evan Cheng0488db92007-09-25 01:57:46 +00008744
Evan Cheng370e5342008-12-03 08:38:43 +00008745// isX86LogicalCmp - Return true if opcode is a X86 logical comparison.
Dan Gohman076aee32009-03-04 19:44:21 +00008746static bool isX86LogicalCmp(SDValue Op) {
8747 unsigned Opc = Op.getNode()->getOpcode();
Benjamin Kramer17c836c2012-04-27 12:07:43 +00008748 if (Opc == X86ISD::CMP || Opc == X86ISD::COMI || Opc == X86ISD::UCOMI ||
8749 Opc == X86ISD::SAHF)
Dan Gohman076aee32009-03-04 19:44:21 +00008750 return true;
8751 if (Op.getResNo() == 1 &&
8752 (Opc == X86ISD::ADD ||
8753 Opc == X86ISD::SUB ||
Chris Lattner5b856542010-12-20 00:59:46 +00008754 Opc == X86ISD::ADC ||
8755 Opc == X86ISD::SBB ||
Dan Gohman076aee32009-03-04 19:44:21 +00008756 Opc == X86ISD::SMUL ||
8757 Opc == X86ISD::UMUL ||
8758 Opc == X86ISD::INC ||
Dan Gohmane220c4b2009-09-18 19:59:53 +00008759 Opc == X86ISD::DEC ||
8760 Opc == X86ISD::OR ||
8761 Opc == X86ISD::XOR ||
8762 Opc == X86ISD::AND))
Dan Gohman076aee32009-03-04 19:44:21 +00008763 return true;
8764
Chris Lattner9637d5b2010-12-05 07:49:54 +00008765 if (Op.getResNo() == 2 && Opc == X86ISD::UMUL)
8766 return true;
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008767
Dan Gohman076aee32009-03-04 19:44:21 +00008768 return false;
Evan Cheng370e5342008-12-03 08:38:43 +00008769}
8770
Chris Lattnera2b56002010-12-05 01:23:24 +00008771static bool isZero(SDValue V) {
8772 ConstantSDNode *C = dyn_cast<ConstantSDNode>(V);
8773 return C && C->isNullValue();
8774}
8775
Chris Lattner96908b12010-12-05 02:00:51 +00008776static bool isAllOnes(SDValue V) {
8777 ConstantSDNode *C = dyn_cast<ConstantSDNode>(V);
8778 return C && C->isAllOnesValue();
8779}
8780
Evan Chengb64dd5f2012-08-07 22:21:00 +00008781static bool isTruncWithZeroHighBitsInput(SDValue V, SelectionDAG &DAG) {
8782 if (V.getOpcode() != ISD::TRUNCATE)
8783 return false;
8784
8785 SDValue VOp0 = V.getOperand(0);
8786 unsigned InBits = VOp0.getValueSizeInBits();
8787 unsigned Bits = V.getValueSizeInBits();
8788 return DAG.MaskedValueIsZero(VOp0, APInt::getHighBitsSet(InBits,InBits-Bits));
8789}
8790
Dan Gohmand858e902010-04-17 15:26:15 +00008791SDValue X86TargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng734503b2006-09-11 02:19:56 +00008792 bool addTest = true;
Dan Gohman475871a2008-07-27 21:46:04 +00008793 SDValue Cond = Op.getOperand(0);
Chris Lattnera2b56002010-12-05 01:23:24 +00008794 SDValue Op1 = Op.getOperand(1);
8795 SDValue Op2 = Op.getOperand(2);
8796 DebugLoc DL = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00008797 SDValue CC;
Evan Cheng9bba8942006-01-26 02:13:10 +00008798
Dan Gohman1a492952009-10-20 16:22:37 +00008799 if (Cond.getOpcode() == ISD::SETCC) {
8800 SDValue NewCond = LowerSETCC(Cond, DAG);
8801 if (NewCond.getNode())
8802 Cond = NewCond;
8803 }
Evan Cheng734503b2006-09-11 02:19:56 +00008804
Chris Lattnera2b56002010-12-05 01:23:24 +00008805 // (select (x == 0), -1, y) -> (sign_bit (x - 1)) | y
Chris Lattner96908b12010-12-05 02:00:51 +00008806 // (select (x == 0), y, -1) -> ~(sign_bit (x - 1)) | y
Chris Lattnera2b56002010-12-05 01:23:24 +00008807 // (select (x != 0), y, -1) -> (sign_bit (x - 1)) | y
Chris Lattner96908b12010-12-05 02:00:51 +00008808 // (select (x != 0), -1, y) -> ~(sign_bit (x - 1)) | y
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00008809 if (Cond.getOpcode() == X86ISD::SETCC &&
Chris Lattner96908b12010-12-05 02:00:51 +00008810 Cond.getOperand(1).getOpcode() == X86ISD::CMP &&
8811 isZero(Cond.getOperand(1).getOperand(1))) {
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00008812 SDValue Cmp = Cond.getOperand(1);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008813
Chris Lattnera2b56002010-12-05 01:23:24 +00008814 unsigned CondCode =cast<ConstantSDNode>(Cond.getOperand(0))->getZExtValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008815
8816 if ((isAllOnes(Op1) || isAllOnes(Op2)) &&
Chris Lattner96908b12010-12-05 02:00:51 +00008817 (CondCode == X86::COND_E || CondCode == X86::COND_NE)) {
8818 SDValue Y = isAllOnes(Op2) ? Op1 : Op2;
Chris Lattnera2b56002010-12-05 01:23:24 +00008819
8820 SDValue CmpOp0 = Cmp.getOperand(0);
Manman Rened579842012-05-07 18:06:23 +00008821 // Apply further optimizations for special cases
8822 // (select (x != 0), -1, 0) -> neg & sbb
8823 // (select (x == 0), 0, -1) -> neg & sbb
8824 if (ConstantSDNode *YC = dyn_cast<ConstantSDNode>(Y))
Chad Rosiera20e1e72012-08-01 18:39:17 +00008825 if (YC->isNullValue() &&
Manman Rened579842012-05-07 18:06:23 +00008826 (isAllOnes(Op1) == (CondCode == X86::COND_NE))) {
8827 SDVTList VTs = DAG.getVTList(CmpOp0.getValueType(), MVT::i32);
Chad Rosiera20e1e72012-08-01 18:39:17 +00008828 SDValue Neg = DAG.getNode(X86ISD::SUB, DL, VTs,
8829 DAG.getConstant(0, CmpOp0.getValueType()),
Manman Rened579842012-05-07 18:06:23 +00008830 CmpOp0);
8831 SDValue Res = DAG.getNode(X86ISD::SETCC_CARRY, DL, Op.getValueType(),
8832 DAG.getConstant(X86::COND_B, MVT::i8),
8833 SDValue(Neg.getNode(), 1));
8834 return Res;
8835 }
8836
Chris Lattnera2b56002010-12-05 01:23:24 +00008837 Cmp = DAG.getNode(X86ISD::CMP, DL, MVT::i32,
8838 CmpOp0, DAG.getConstant(1, CmpOp0.getValueType()));
Benjamin Kramer17c836c2012-04-27 12:07:43 +00008839 Cmp = ConvertCmpIfNecessary(Cmp, DAG);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008840
Chris Lattner96908b12010-12-05 02:00:51 +00008841 SDValue Res = // Res = 0 or -1.
Chris Lattnera2b56002010-12-05 01:23:24 +00008842 DAG.getNode(X86ISD::SETCC_CARRY, DL, Op.getValueType(),
8843 DAG.getConstant(X86::COND_B, MVT::i8), Cmp);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008844
Chris Lattner96908b12010-12-05 02:00:51 +00008845 if (isAllOnes(Op1) != (CondCode == X86::COND_E))
8846 Res = DAG.getNOT(DL, Res, Res.getValueType());
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008847
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00008848 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(Op2);
Chris Lattnera2b56002010-12-05 01:23:24 +00008849 if (N2C == 0 || !N2C->isNullValue())
8850 Res = DAG.getNode(ISD::OR, DL, Res.getValueType(), Res, Y);
8851 return Res;
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00008852 }
8853 }
8854
Chris Lattnera2b56002010-12-05 01:23:24 +00008855 // Look past (and (setcc_carry (cmp ...)), 1).
Evan Chengad9c0a32009-12-15 00:53:42 +00008856 if (Cond.getOpcode() == ISD::AND &&
8857 Cond.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY) {
8858 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Cond.getOperand(1));
Michael J. Spencerec38de22010-10-10 22:04:20 +00008859 if (C && C->getAPIntValue() == 1)
Evan Chengad9c0a32009-12-15 00:53:42 +00008860 Cond = Cond.getOperand(0);
8861 }
8862
Evan Cheng3f41d662007-10-08 22:16:29 +00008863 // If condition flag is set by a X86ISD::CMP, then use it as the condition
8864 // setting operand in place of the X86ISD::SETCC.
Dan Gohman65fd6562011-11-03 21:49:52 +00008865 unsigned CondOpcode = Cond.getOpcode();
8866 if (CondOpcode == X86ISD::SETCC ||
8867 CondOpcode == X86ISD::SETCC_CARRY) {
Evan Cheng734503b2006-09-11 02:19:56 +00008868 CC = Cond.getOperand(0);
8869
Dan Gohman475871a2008-07-27 21:46:04 +00008870 SDValue Cmp = Cond.getOperand(1);
Evan Cheng734503b2006-09-11 02:19:56 +00008871 unsigned Opc = Cmp.getOpcode();
Owen Andersone50ed302009-08-10 22:56:29 +00008872 EVT VT = Op.getValueType();
Scott Michelfdc40a02009-02-17 22:15:04 +00008873
Evan Cheng3f41d662007-10-08 22:16:29 +00008874 bool IllegalFPCMov = false;
Duncan Sands83ec4b62008-06-06 12:08:01 +00008875 if (VT.isFloatingPoint() && !VT.isVector() &&
Chris Lattner78631162008-01-16 06:24:21 +00008876 !isScalarFPTypeInSSEReg(VT)) // FPStack?
Dan Gohman7810bfe2008-09-26 21:54:37 +00008877 IllegalFPCMov = !hasFPCMov(cast<ConstantSDNode>(CC)->getSExtValue());
Scott Michelfdc40a02009-02-17 22:15:04 +00008878
Chris Lattnerd1980a52009-03-12 06:52:53 +00008879 if ((isX86LogicalCmp(Cmp) && !IllegalFPCMov) ||
8880 Opc == X86ISD::BT) { // FIXME
Evan Cheng3f41d662007-10-08 22:16:29 +00008881 Cond = Cmp;
Evan Cheng0488db92007-09-25 01:57:46 +00008882 addTest = false;
8883 }
Dan Gohman65fd6562011-11-03 21:49:52 +00008884 } else if (CondOpcode == ISD::USUBO || CondOpcode == ISD::SSUBO ||
8885 CondOpcode == ISD::UADDO || CondOpcode == ISD::SADDO ||
8886 ((CondOpcode == ISD::UMULO || CondOpcode == ISD::SMULO) &&
8887 Cond.getOperand(0).getValueType() != MVT::i8)) {
8888 SDValue LHS = Cond.getOperand(0);
8889 SDValue RHS = Cond.getOperand(1);
8890 unsigned X86Opcode;
8891 unsigned X86Cond;
8892 SDVTList VTs;
8893 switch (CondOpcode) {
8894 case ISD::UADDO: X86Opcode = X86ISD::ADD; X86Cond = X86::COND_B; break;
8895 case ISD::SADDO: X86Opcode = X86ISD::ADD; X86Cond = X86::COND_O; break;
8896 case ISD::USUBO: X86Opcode = X86ISD::SUB; X86Cond = X86::COND_B; break;
8897 case ISD::SSUBO: X86Opcode = X86ISD::SUB; X86Cond = X86::COND_O; break;
8898 case ISD::UMULO: X86Opcode = X86ISD::UMUL; X86Cond = X86::COND_O; break;
8899 case ISD::SMULO: X86Opcode = X86ISD::SMUL; X86Cond = X86::COND_O; break;
8900 default: llvm_unreachable("unexpected overflowing operator");
8901 }
8902 if (CondOpcode == ISD::UMULO)
8903 VTs = DAG.getVTList(LHS.getValueType(), LHS.getValueType(),
8904 MVT::i32);
8905 else
8906 VTs = DAG.getVTList(LHS.getValueType(), MVT::i32);
8907
8908 SDValue X86Op = DAG.getNode(X86Opcode, DL, VTs, LHS, RHS);
8909
8910 if (CondOpcode == ISD::UMULO)
8911 Cond = X86Op.getValue(2);
8912 else
8913 Cond = X86Op.getValue(1);
8914
8915 CC = DAG.getConstant(X86Cond, MVT::i8);
8916 addTest = false;
Evan Cheng0488db92007-09-25 01:57:46 +00008917 }
8918
8919 if (addTest) {
Evan Chengb64dd5f2012-08-07 22:21:00 +00008920 // Look pass the truncate if the high bits are known zero.
8921 if (isTruncWithZeroHighBitsInput(Cond, DAG))
8922 Cond = Cond.getOperand(0);
Evan Chengd40d03e2010-01-06 19:38:29 +00008923
8924 // We know the result of AND is compared against zero. Try to match
8925 // it to BT.
Michael J. Spencerec38de22010-10-10 22:04:20 +00008926 if (Cond.getOpcode() == ISD::AND && Cond.hasOneUse()) {
Chris Lattnera2b56002010-12-05 01:23:24 +00008927 SDValue NewSetCC = LowerToBT(Cond, ISD::SETNE, DL, DAG);
Evan Chengd40d03e2010-01-06 19:38:29 +00008928 if (NewSetCC.getNode()) {
8929 CC = NewSetCC.getOperand(0);
8930 Cond = NewSetCC.getOperand(1);
8931 addTest = false;
8932 }
8933 }
8934 }
8935
8936 if (addTest) {
Owen Anderson825b72b2009-08-11 20:47:22 +00008937 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng552f09a2010-04-26 19:06:11 +00008938 Cond = EmitTest(Cond, X86::COND_NE, DAG);
Evan Cheng0488db92007-09-25 01:57:46 +00008939 }
8940
Benjamin Kramere915ff32010-12-22 23:09:28 +00008941 // a < b ? -1 : 0 -> RES = ~setcc_carry
8942 // a < b ? 0 : -1 -> RES = setcc_carry
8943 // a >= b ? -1 : 0 -> RES = setcc_carry
8944 // a >= b ? 0 : -1 -> RES = ~setcc_carry
Manman Ren39ad5682012-08-08 00:51:41 +00008945 if (Cond.getOpcode() == X86ISD::SUB) {
Benjamin Kramer17c836c2012-04-27 12:07:43 +00008946 Cond = ConvertCmpIfNecessary(Cond, DAG);
Benjamin Kramere915ff32010-12-22 23:09:28 +00008947 unsigned CondCode = cast<ConstantSDNode>(CC)->getZExtValue();
8948
8949 if ((CondCode == X86::COND_AE || CondCode == X86::COND_B) &&
8950 (isAllOnes(Op1) || isAllOnes(Op2)) && (isZero(Op1) || isZero(Op2))) {
8951 SDValue Res = DAG.getNode(X86ISD::SETCC_CARRY, DL, Op.getValueType(),
8952 DAG.getConstant(X86::COND_B, MVT::i8), Cond);
8953 if (isAllOnes(Op1) != (CondCode == X86::COND_B))
8954 return DAG.getNOT(DL, Res, Res.getValueType());
8955 return Res;
8956 }
8957 }
8958
Evan Cheng0488db92007-09-25 01:57:46 +00008959 // X86ISD::CMOV means set the result (which is operand 1) to the RHS if
8960 // condition is true.
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00008961 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::Glue);
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00008962 SDValue Ops[] = { Op2, Op1, CC, Cond };
Chris Lattnera2b56002010-12-05 01:23:24 +00008963 return DAG.getNode(X86ISD::CMOV, DL, VTs, Ops, array_lengthof(Ops));
Evan Cheng0488db92007-09-25 01:57:46 +00008964}
8965
Evan Cheng370e5342008-12-03 08:38:43 +00008966// isAndOrOfSingleUseSetCCs - Return true if node is an ISD::AND or
8967// ISD::OR of two X86ISD::SETCC nodes each of which has no other use apart
8968// from the AND / OR.
8969static bool isAndOrOfSetCCs(SDValue Op, unsigned &Opc) {
8970 Opc = Op.getOpcode();
8971 if (Opc != ISD::OR && Opc != ISD::AND)
8972 return false;
8973 return (Op.getOperand(0).getOpcode() == X86ISD::SETCC &&
8974 Op.getOperand(0).hasOneUse() &&
8975 Op.getOperand(1).getOpcode() == X86ISD::SETCC &&
8976 Op.getOperand(1).hasOneUse());
8977}
8978
Evan Cheng961d6d42009-02-02 08:19:07 +00008979// isXor1OfSetCC - Return true if node is an ISD::XOR of a X86ISD::SETCC and
8980// 1 and that the SETCC node has a single use.
Evan Cheng67ad9db2009-02-02 08:07:36 +00008981static bool isXor1OfSetCC(SDValue Op) {
8982 if (Op.getOpcode() != ISD::XOR)
8983 return false;
8984 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
8985 if (N1C && N1C->getAPIntValue() == 1) {
8986 return Op.getOperand(0).getOpcode() == X86ISD::SETCC &&
8987 Op.getOperand(0).hasOneUse();
8988 }
8989 return false;
8990}
8991
Dan Gohmand858e902010-04-17 15:26:15 +00008992SDValue X86TargetLowering::LowerBRCOND(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng734503b2006-09-11 02:19:56 +00008993 bool addTest = true;
Dan Gohman475871a2008-07-27 21:46:04 +00008994 SDValue Chain = Op.getOperand(0);
8995 SDValue Cond = Op.getOperand(1);
8996 SDValue Dest = Op.getOperand(2);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008997 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00008998 SDValue CC;
Dan Gohman65fd6562011-11-03 21:49:52 +00008999 bool Inverted = false;
Evan Cheng734503b2006-09-11 02:19:56 +00009000
Dan Gohman1a492952009-10-20 16:22:37 +00009001 if (Cond.getOpcode() == ISD::SETCC) {
Dan Gohman65fd6562011-11-03 21:49:52 +00009002 // Check for setcc([su]{add,sub,mul}o == 0).
9003 if (cast<CondCodeSDNode>(Cond.getOperand(2))->get() == ISD::SETEQ &&
9004 isa<ConstantSDNode>(Cond.getOperand(1)) &&
9005 cast<ConstantSDNode>(Cond.getOperand(1))->isNullValue() &&
9006 Cond.getOperand(0).getResNo() == 1 &&
9007 (Cond.getOperand(0).getOpcode() == ISD::SADDO ||
9008 Cond.getOperand(0).getOpcode() == ISD::UADDO ||
9009 Cond.getOperand(0).getOpcode() == ISD::SSUBO ||
9010 Cond.getOperand(0).getOpcode() == ISD::USUBO ||
9011 Cond.getOperand(0).getOpcode() == ISD::SMULO ||
9012 Cond.getOperand(0).getOpcode() == ISD::UMULO)) {
9013 Inverted = true;
9014 Cond = Cond.getOperand(0);
9015 } else {
9016 SDValue NewCond = LowerSETCC(Cond, DAG);
9017 if (NewCond.getNode())
9018 Cond = NewCond;
9019 }
Dan Gohman1a492952009-10-20 16:22:37 +00009020 }
Chris Lattnere55484e2008-12-25 05:34:37 +00009021#if 0
9022 // FIXME: LowerXALUO doesn't handle these!!
Bill Wendlingd350e022008-12-12 21:15:41 +00009023 else if (Cond.getOpcode() == X86ISD::ADD ||
9024 Cond.getOpcode() == X86ISD::SUB ||
9025 Cond.getOpcode() == X86ISD::SMUL ||
9026 Cond.getOpcode() == X86ISD::UMUL)
Bill Wendling74c37652008-12-09 22:08:41 +00009027 Cond = LowerXALUO(Cond, DAG);
Chris Lattnere55484e2008-12-25 05:34:37 +00009028#endif
Scott Michelfdc40a02009-02-17 22:15:04 +00009029
Evan Chengad9c0a32009-12-15 00:53:42 +00009030 // Look pass (and (setcc_carry (cmp ...)), 1).
9031 if (Cond.getOpcode() == ISD::AND &&
9032 Cond.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY) {
9033 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Cond.getOperand(1));
Michael J. Spencerec38de22010-10-10 22:04:20 +00009034 if (C && C->getAPIntValue() == 1)
Evan Chengad9c0a32009-12-15 00:53:42 +00009035 Cond = Cond.getOperand(0);
9036 }
9037
Evan Cheng3f41d662007-10-08 22:16:29 +00009038 // If condition flag is set by a X86ISD::CMP, then use it as the condition
9039 // setting operand in place of the X86ISD::SETCC.
Dan Gohman65fd6562011-11-03 21:49:52 +00009040 unsigned CondOpcode = Cond.getOpcode();
9041 if (CondOpcode == X86ISD::SETCC ||
9042 CondOpcode == X86ISD::SETCC_CARRY) {
Evan Cheng734503b2006-09-11 02:19:56 +00009043 CC = Cond.getOperand(0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00009044
Dan Gohman475871a2008-07-27 21:46:04 +00009045 SDValue Cmp = Cond.getOperand(1);
Evan Cheng734503b2006-09-11 02:19:56 +00009046 unsigned Opc = Cmp.getOpcode();
Chris Lattnere55484e2008-12-25 05:34:37 +00009047 // FIXME: WHY THE SPECIAL CASING OF LogicalCmp??
Dan Gohman076aee32009-03-04 19:44:21 +00009048 if (isX86LogicalCmp(Cmp) || Opc == X86ISD::BT) {
Evan Cheng3f41d662007-10-08 22:16:29 +00009049 Cond = Cmp;
Evan Cheng0488db92007-09-25 01:57:46 +00009050 addTest = false;
Bill Wendling61edeb52008-12-02 01:06:39 +00009051 } else {
Evan Cheng370e5342008-12-03 08:38:43 +00009052 switch (cast<ConstantSDNode>(CC)->getZExtValue()) {
Bill Wendling0ea25cb2008-12-03 08:32:02 +00009053 default: break;
9054 case X86::COND_O:
Dan Gohman653456c2009-01-07 00:15:08 +00009055 case X86::COND_B:
Chris Lattnere55484e2008-12-25 05:34:37 +00009056 // These can only come from an arithmetic instruction with overflow,
9057 // e.g. SADDO, UADDO.
Bill Wendling0ea25cb2008-12-03 08:32:02 +00009058 Cond = Cond.getNode()->getOperand(1);
9059 addTest = false;
9060 break;
Bill Wendling61edeb52008-12-02 01:06:39 +00009061 }
Evan Cheng0488db92007-09-25 01:57:46 +00009062 }
Dan Gohman65fd6562011-11-03 21:49:52 +00009063 }
9064 CondOpcode = Cond.getOpcode();
9065 if (CondOpcode == ISD::UADDO || CondOpcode == ISD::SADDO ||
9066 CondOpcode == ISD::USUBO || CondOpcode == ISD::SSUBO ||
9067 ((CondOpcode == ISD::UMULO || CondOpcode == ISD::SMULO) &&
9068 Cond.getOperand(0).getValueType() != MVT::i8)) {
9069 SDValue LHS = Cond.getOperand(0);
9070 SDValue RHS = Cond.getOperand(1);
9071 unsigned X86Opcode;
9072 unsigned X86Cond;
9073 SDVTList VTs;
9074 switch (CondOpcode) {
9075 case ISD::UADDO: X86Opcode = X86ISD::ADD; X86Cond = X86::COND_B; break;
9076 case ISD::SADDO: X86Opcode = X86ISD::ADD; X86Cond = X86::COND_O; break;
9077 case ISD::USUBO: X86Opcode = X86ISD::SUB; X86Cond = X86::COND_B; break;
9078 case ISD::SSUBO: X86Opcode = X86ISD::SUB; X86Cond = X86::COND_O; break;
9079 case ISD::UMULO: X86Opcode = X86ISD::UMUL; X86Cond = X86::COND_O; break;
9080 case ISD::SMULO: X86Opcode = X86ISD::SMUL; X86Cond = X86::COND_O; break;
9081 default: llvm_unreachable("unexpected overflowing operator");
9082 }
9083 if (Inverted)
9084 X86Cond = X86::GetOppositeBranchCondition((X86::CondCode)X86Cond);
9085 if (CondOpcode == ISD::UMULO)
9086 VTs = DAG.getVTList(LHS.getValueType(), LHS.getValueType(),
9087 MVT::i32);
9088 else
9089 VTs = DAG.getVTList(LHS.getValueType(), MVT::i32);
9090
9091 SDValue X86Op = DAG.getNode(X86Opcode, dl, VTs, LHS, RHS);
9092
9093 if (CondOpcode == ISD::UMULO)
9094 Cond = X86Op.getValue(2);
9095 else
9096 Cond = X86Op.getValue(1);
9097
9098 CC = DAG.getConstant(X86Cond, MVT::i8);
9099 addTest = false;
Evan Cheng370e5342008-12-03 08:38:43 +00009100 } else {
9101 unsigned CondOpc;
9102 if (Cond.hasOneUse() && isAndOrOfSetCCs(Cond, CondOpc)) {
9103 SDValue Cmp = Cond.getOperand(0).getOperand(1);
Evan Cheng370e5342008-12-03 08:38:43 +00009104 if (CondOpc == ISD::OR) {
9105 // Also, recognize the pattern generated by an FCMP_UNE. We can emit
9106 // two branches instead of an explicit OR instruction with a
9107 // separate test.
9108 if (Cmp == Cond.getOperand(1).getOperand(1) &&
Dan Gohman076aee32009-03-04 19:44:21 +00009109 isX86LogicalCmp(Cmp)) {
Evan Cheng370e5342008-12-03 08:38:43 +00009110 CC = Cond.getOperand(0).getOperand(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009111 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Evan Cheng370e5342008-12-03 08:38:43 +00009112 Chain, Dest, CC, Cmp);
9113 CC = Cond.getOperand(1).getOperand(0);
9114 Cond = Cmp;
9115 addTest = false;
9116 }
9117 } else { // ISD::AND
9118 // Also, recognize the pattern generated by an FCMP_OEQ. We can emit
9119 // two branches instead of an explicit AND instruction with a
9120 // separate test. However, we only do this if this block doesn't
9121 // have a fall-through edge, because this requires an explicit
9122 // jmp when the condition is false.
9123 if (Cmp == Cond.getOperand(1).getOperand(1) &&
Dan Gohman076aee32009-03-04 19:44:21 +00009124 isX86LogicalCmp(Cmp) &&
Evan Cheng370e5342008-12-03 08:38:43 +00009125 Op.getNode()->hasOneUse()) {
9126 X86::CondCode CCode =
9127 (X86::CondCode)Cond.getOperand(0).getConstantOperandVal(0);
9128 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00009129 CC = DAG.getConstant(CCode, MVT::i8);
Dan Gohman027657d2010-06-18 15:30:29 +00009130 SDNode *User = *Op.getNode()->use_begin();
Evan Cheng370e5342008-12-03 08:38:43 +00009131 // Look for an unconditional branch following this conditional branch.
9132 // We need this because we need to reverse the successors in order
9133 // to implement FCMP_OEQ.
Dan Gohman027657d2010-06-18 15:30:29 +00009134 if (User->getOpcode() == ISD::BR) {
9135 SDValue FalseBB = User->getOperand(1);
9136 SDNode *NewBR =
9137 DAG.UpdateNodeOperands(User, User->getOperand(0), Dest);
Evan Cheng370e5342008-12-03 08:38:43 +00009138 assert(NewBR == User);
Nick Lewycky2a3ee5e2010-06-20 20:27:42 +00009139 (void)NewBR;
Evan Cheng370e5342008-12-03 08:38:43 +00009140 Dest = FalseBB;
Dan Gohman279c22e2008-10-21 03:29:32 +00009141
Dale Johannesene4d209d2009-02-03 20:21:25 +00009142 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Evan Cheng370e5342008-12-03 08:38:43 +00009143 Chain, Dest, CC, Cmp);
9144 X86::CondCode CCode =
9145 (X86::CondCode)Cond.getOperand(1).getConstantOperandVal(0);
9146 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00009147 CC = DAG.getConstant(CCode, MVT::i8);
Evan Cheng370e5342008-12-03 08:38:43 +00009148 Cond = Cmp;
9149 addTest = false;
9150 }
9151 }
Dan Gohman279c22e2008-10-21 03:29:32 +00009152 }
Evan Cheng67ad9db2009-02-02 08:07:36 +00009153 } else if (Cond.hasOneUse() && isXor1OfSetCC(Cond)) {
9154 // Recognize for xorb (setcc), 1 patterns. The xor inverts the condition.
9155 // It should be transformed during dag combiner except when the condition
9156 // is set by a arithmetics with overflow node.
9157 X86::CondCode CCode =
9158 (X86::CondCode)Cond.getOperand(0).getConstantOperandVal(0);
9159 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00009160 CC = DAG.getConstant(CCode, MVT::i8);
Evan Cheng67ad9db2009-02-02 08:07:36 +00009161 Cond = Cond.getOperand(0).getOperand(1);
9162 addTest = false;
Dan Gohman65fd6562011-11-03 21:49:52 +00009163 } else if (Cond.getOpcode() == ISD::SETCC &&
9164 cast<CondCodeSDNode>(Cond.getOperand(2))->get() == ISD::SETOEQ) {
9165 // For FCMP_OEQ, we can emit
9166 // two branches instead of an explicit AND instruction with a
9167 // separate test. However, we only do this if this block doesn't
9168 // have a fall-through edge, because this requires an explicit
9169 // jmp when the condition is false.
9170 if (Op.getNode()->hasOneUse()) {
9171 SDNode *User = *Op.getNode()->use_begin();
9172 // Look for an unconditional branch following this conditional branch.
9173 // We need this because we need to reverse the successors in order
9174 // to implement FCMP_OEQ.
9175 if (User->getOpcode() == ISD::BR) {
9176 SDValue FalseBB = User->getOperand(1);
9177 SDNode *NewBR =
9178 DAG.UpdateNodeOperands(User, User->getOperand(0), Dest);
9179 assert(NewBR == User);
9180 (void)NewBR;
9181 Dest = FalseBB;
9182
9183 SDValue Cmp = DAG.getNode(X86ISD::CMP, dl, MVT::i32,
9184 Cond.getOperand(0), Cond.getOperand(1));
Benjamin Kramer17c836c2012-04-27 12:07:43 +00009185 Cmp = ConvertCmpIfNecessary(Cmp, DAG);
Dan Gohman65fd6562011-11-03 21:49:52 +00009186 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
9187 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
9188 Chain, Dest, CC, Cmp);
9189 CC = DAG.getConstant(X86::COND_P, MVT::i8);
9190 Cond = Cmp;
9191 addTest = false;
9192 }
9193 }
9194 } else if (Cond.getOpcode() == ISD::SETCC &&
9195 cast<CondCodeSDNode>(Cond.getOperand(2))->get() == ISD::SETUNE) {
9196 // For FCMP_UNE, we can emit
9197 // two branches instead of an explicit AND instruction with a
9198 // separate test. However, we only do this if this block doesn't
9199 // have a fall-through edge, because this requires an explicit
9200 // jmp when the condition is false.
9201 if (Op.getNode()->hasOneUse()) {
9202 SDNode *User = *Op.getNode()->use_begin();
9203 // Look for an unconditional branch following this conditional branch.
9204 // We need this because we need to reverse the successors in order
9205 // to implement FCMP_UNE.
9206 if (User->getOpcode() == ISD::BR) {
9207 SDValue FalseBB = User->getOperand(1);
9208 SDNode *NewBR =
9209 DAG.UpdateNodeOperands(User, User->getOperand(0), Dest);
9210 assert(NewBR == User);
9211 (void)NewBR;
9212
9213 SDValue Cmp = DAG.getNode(X86ISD::CMP, dl, MVT::i32,
9214 Cond.getOperand(0), Cond.getOperand(1));
Benjamin Kramer17c836c2012-04-27 12:07:43 +00009215 Cmp = ConvertCmpIfNecessary(Cmp, DAG);
Dan Gohman65fd6562011-11-03 21:49:52 +00009216 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
9217 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
9218 Chain, Dest, CC, Cmp);
9219 CC = DAG.getConstant(X86::COND_NP, MVT::i8);
9220 Cond = Cmp;
9221 addTest = false;
9222 Dest = FalseBB;
9223 }
9224 }
Dan Gohman279c22e2008-10-21 03:29:32 +00009225 }
Evan Cheng0488db92007-09-25 01:57:46 +00009226 }
9227
9228 if (addTest) {
Evan Chengb64dd5f2012-08-07 22:21:00 +00009229 // Look pass the truncate if the high bits are known zero.
9230 if (isTruncWithZeroHighBitsInput(Cond, DAG))
9231 Cond = Cond.getOperand(0);
Evan Chengd40d03e2010-01-06 19:38:29 +00009232
9233 // We know the result of AND is compared against zero. Try to match
9234 // it to BT.
Michael J. Spencerec38de22010-10-10 22:04:20 +00009235 if (Cond.getOpcode() == ISD::AND && Cond.hasOneUse()) {
Evan Chengd40d03e2010-01-06 19:38:29 +00009236 SDValue NewSetCC = LowerToBT(Cond, ISD::SETNE, dl, DAG);
9237 if (NewSetCC.getNode()) {
9238 CC = NewSetCC.getOperand(0);
9239 Cond = NewSetCC.getOperand(1);
9240 addTest = false;
9241 }
9242 }
9243 }
9244
9245 if (addTest) {
Owen Anderson825b72b2009-08-11 20:47:22 +00009246 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng552f09a2010-04-26 19:06:11 +00009247 Cond = EmitTest(Cond, X86::COND_NE, DAG);
Evan Cheng0488db92007-09-25 01:57:46 +00009248 }
Benjamin Kramer17c836c2012-04-27 12:07:43 +00009249 Cond = ConvertCmpIfNecessary(Cond, DAG);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009250 return DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Dan Gohman279c22e2008-10-21 03:29:32 +00009251 Chain, Dest, CC, Cond);
Evan Cheng0488db92007-09-25 01:57:46 +00009252}
9253
Anton Korobeynikove060b532007-04-17 19:34:00 +00009254
9255// Lower dynamic stack allocation to _alloca call for Cygwin/Mingw targets.
9256// Calls to _alloca is needed to probe the stack when allocating more than 4k
9257// bytes in one go. Touching the stack at 4K increments is necessary to ensure
9258// that the guard pages used by the OS virtual memory manager are allocated in
9259// correct sequence.
Dan Gohman475871a2008-07-27 21:46:04 +00009260SDValue
9261X86TargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00009262 SelectionDAG &DAG) const {
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009263 assert((Subtarget->isTargetCygMing() || Subtarget->isTargetWindows() ||
Nick Lewycky8a8d4792011-12-02 22:16:29 +00009264 getTargetMachine().Options.EnableSegmentedStacks) &&
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009265 "This should be used only on Windows targets or when segmented stacks "
Rafael Espindola96428ce2011-09-06 18:43:08 +00009266 "are being used");
9267 assert(!Subtarget->isTargetEnvMacho() && "Not implemented");
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009268 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov096b4612008-06-11 20:16:42 +00009269
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00009270 // Get the inputs.
Dan Gohman475871a2008-07-27 21:46:04 +00009271 SDValue Chain = Op.getOperand(0);
9272 SDValue Size = Op.getOperand(1);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00009273 // FIXME: Ensure alignment here
9274
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009275 bool Is64Bit = Subtarget->is64Bit();
9276 EVT SPTy = Is64Bit ? MVT::i64 : MVT::i32;
Anton Korobeynikov096b4612008-06-11 20:16:42 +00009277
Nick Lewycky8a8d4792011-12-02 22:16:29 +00009278 if (getTargetMachine().Options.EnableSegmentedStacks) {
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009279 MachineFunction &MF = DAG.getMachineFunction();
9280 MachineRegisterInfo &MRI = MF.getRegInfo();
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00009281
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009282 if (Is64Bit) {
9283 // The 64 bit implementation of segmented stacks needs to clobber both r10
Rafael Espindola96428ce2011-09-06 18:43:08 +00009284 // r11. This makes it impossible to use it along with nested parameters.
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009285 const Function *F = MF.getFunction();
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00009286
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009287 for (Function::const_arg_iterator I = F->arg_begin(), E = F->arg_end();
Craig Topper31a207a2012-05-04 06:39:13 +00009288 I != E; ++I)
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009289 if (I->hasNestAttr())
9290 report_fatal_error("Cannot use segmented stacks with functions that "
9291 "have nested arguments.");
9292 }
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00009293
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009294 const TargetRegisterClass *AddrRegClass =
9295 getRegClassFor(Subtarget->is64Bit() ? MVT::i64:MVT::i32);
9296 unsigned Vreg = MRI.createVirtualRegister(AddrRegClass);
9297 Chain = DAG.getCopyToReg(Chain, dl, Vreg, Size);
9298 SDValue Value = DAG.getNode(X86ISD::SEG_ALLOCA, dl, SPTy, Chain,
9299 DAG.getRegister(Vreg, SPTy));
9300 SDValue Ops1[2] = { Value, Chain };
9301 return DAG.getMergeValues(Ops1, 2, dl);
9302 } else {
9303 SDValue Flag;
9304 unsigned Reg = (Subtarget->is64Bit() ? X86::RAX : X86::EAX);
Anton Korobeynikov096b4612008-06-11 20:16:42 +00009305
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009306 Chain = DAG.getCopyToReg(Chain, dl, Reg, Size, Flag);
9307 Flag = Chain.getValue(1);
9308 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Anton Korobeynikov096b4612008-06-11 20:16:42 +00009309
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009310 Chain = DAG.getNode(X86ISD::WIN_ALLOCA, dl, NodeTys, Chain, Flag);
9311 Flag = Chain.getValue(1);
9312
9313 Chain = DAG.getCopyFromReg(Chain, dl, X86StackPtr, SPTy).getValue(1);
9314
9315 SDValue Ops1[2] = { Chain.getValue(0), Chain };
9316 return DAG.getMergeValues(Ops1, 2, dl);
9317 }
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00009318}
9319
Dan Gohmand858e902010-04-17 15:26:15 +00009320SDValue X86TargetLowering::LowerVASTART(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman1e93df62010-04-17 14:41:14 +00009321 MachineFunction &MF = DAG.getMachineFunction();
9322 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
9323
Dan Gohman69de1932008-02-06 22:27:42 +00009324 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Chris Lattner8026a9d2010-09-21 17:50:43 +00009325 DebugLoc DL = Op.getDebugLoc();
Evan Cheng8b2794a2006-10-13 21:14:26 +00009326
Anton Korobeynikove7beda12010-10-03 22:52:07 +00009327 if (!Subtarget->is64Bit() || Subtarget->isTargetWin64()) {
Evan Cheng25ab6902006-09-08 06:48:29 +00009328 // vastart just stores the address of the VarArgsFrameIndex slot into the
9329 // memory location argument.
Dan Gohman1e93df62010-04-17 14:41:14 +00009330 SDValue FR = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
9331 getPointerTy());
Chris Lattner8026a9d2010-09-21 17:50:43 +00009332 return DAG.getStore(Op.getOperand(0), DL, FR, Op.getOperand(1),
9333 MachinePointerInfo(SV), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00009334 }
9335
9336 // __va_list_tag:
9337 // gp_offset (0 - 6 * 8)
9338 // fp_offset (48 - 48 + 8 * 16)
9339 // overflow_arg_area (point to parameters coming in memory).
9340 // reg_save_area
Dan Gohman475871a2008-07-27 21:46:04 +00009341 SmallVector<SDValue, 8> MemOps;
9342 SDValue FIN = Op.getOperand(1);
Evan Cheng25ab6902006-09-08 06:48:29 +00009343 // Store gp_offset
Chris Lattner8026a9d2010-09-21 17:50:43 +00009344 SDValue Store = DAG.getStore(Op.getOperand(0), DL,
Dan Gohman1e93df62010-04-17 14:41:14 +00009345 DAG.getConstant(FuncInfo->getVarArgsGPOffset(),
9346 MVT::i32),
Chris Lattner8026a9d2010-09-21 17:50:43 +00009347 FIN, MachinePointerInfo(SV), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00009348 MemOps.push_back(Store);
9349
9350 // Store fp_offset
Chris Lattner8026a9d2010-09-21 17:50:43 +00009351 FIN = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00009352 FIN, DAG.getIntPtrConstant(4));
Chris Lattner8026a9d2010-09-21 17:50:43 +00009353 Store = DAG.getStore(Op.getOperand(0), DL,
Dan Gohman1e93df62010-04-17 14:41:14 +00009354 DAG.getConstant(FuncInfo->getVarArgsFPOffset(),
9355 MVT::i32),
Chris Lattner8026a9d2010-09-21 17:50:43 +00009356 FIN, MachinePointerInfo(SV, 4), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00009357 MemOps.push_back(Store);
9358
9359 // Store ptr to overflow_arg_area
Chris Lattner8026a9d2010-09-21 17:50:43 +00009360 FIN = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00009361 FIN, DAG.getIntPtrConstant(4));
Dan Gohman1e93df62010-04-17 14:41:14 +00009362 SDValue OVFIN = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
9363 getPointerTy());
Chris Lattner8026a9d2010-09-21 17:50:43 +00009364 Store = DAG.getStore(Op.getOperand(0), DL, OVFIN, FIN,
9365 MachinePointerInfo(SV, 8),
David Greene67c9d422010-02-15 16:53:33 +00009366 false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00009367 MemOps.push_back(Store);
9368
9369 // Store ptr to reg_save_area.
Chris Lattner8026a9d2010-09-21 17:50:43 +00009370 FIN = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00009371 FIN, DAG.getIntPtrConstant(8));
Dan Gohman1e93df62010-04-17 14:41:14 +00009372 SDValue RSFIN = DAG.getFrameIndex(FuncInfo->getRegSaveFrameIndex(),
9373 getPointerTy());
Chris Lattner8026a9d2010-09-21 17:50:43 +00009374 Store = DAG.getStore(Op.getOperand(0), DL, RSFIN, FIN,
9375 MachinePointerInfo(SV, 16), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00009376 MemOps.push_back(Store);
Chris Lattner8026a9d2010-09-21 17:50:43 +00009377 return DAG.getNode(ISD::TokenFactor, DL, MVT::Other,
Dale Johannesene4d209d2009-02-03 20:21:25 +00009378 &MemOps[0], MemOps.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00009379}
9380
Dan Gohmand858e902010-04-17 15:26:15 +00009381SDValue X86TargetLowering::LowerVAARG(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman320afb82010-10-12 18:00:49 +00009382 assert(Subtarget->is64Bit() &&
9383 "LowerVAARG only handles 64-bit va_arg!");
9384 assert((Subtarget->isTargetLinux() ||
9385 Subtarget->isTargetDarwin()) &&
9386 "Unhandled target in LowerVAARG");
9387 assert(Op.getNode()->getNumOperands() == 4);
9388 SDValue Chain = Op.getOperand(0);
9389 SDValue SrcPtr = Op.getOperand(1);
9390 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
9391 unsigned Align = Op.getConstantOperandVal(3);
9392 DebugLoc dl = Op.getDebugLoc();
Dan Gohman9018e832008-05-10 01:26:14 +00009393
Dan Gohman320afb82010-10-12 18:00:49 +00009394 EVT ArgVT = Op.getNode()->getValueType(0);
Chris Lattnerdb125cf2011-07-18 04:54:35 +00009395 Type *ArgTy = ArgVT.getTypeForEVT(*DAG.getContext());
Dan Gohman320afb82010-10-12 18:00:49 +00009396 uint32_t ArgSize = getTargetData()->getTypeAllocSize(ArgTy);
9397 uint8_t ArgMode;
9398
9399 // Decide which area this value should be read from.
9400 // TODO: Implement the AMD64 ABI in its entirety. This simple
9401 // selection mechanism works only for the basic types.
9402 if (ArgVT == MVT::f80) {
9403 llvm_unreachable("va_arg for f80 not yet implemented");
9404 } else if (ArgVT.isFloatingPoint() && ArgSize <= 16 /*bytes*/) {
9405 ArgMode = 2; // Argument passed in XMM register. Use fp_offset.
9406 } else if (ArgVT.isInteger() && ArgSize <= 32 /*bytes*/) {
9407 ArgMode = 1; // Argument passed in GPR64 register(s). Use gp_offset.
9408 } else {
9409 llvm_unreachable("Unhandled argument type in LowerVAARG");
9410 }
9411
9412 if (ArgMode == 2) {
9413 // Sanity Check: Make sure using fp_offset makes sense.
Nick Lewycky8a8d4792011-12-02 22:16:29 +00009414 assert(!getTargetMachine().Options.UseSoftFloat &&
Eric Christopher52b45052010-10-12 19:44:17 +00009415 !(DAG.getMachineFunction()
9416 .getFunction()->hasFnAttr(Attribute::NoImplicitFloat)) &&
Craig Topper1accb7e2012-01-10 06:54:16 +00009417 Subtarget->hasSSE1());
Dan Gohman320afb82010-10-12 18:00:49 +00009418 }
9419
9420 // Insert VAARG_64 node into the DAG
9421 // VAARG_64 returns two values: Variable Argument Address, Chain
9422 SmallVector<SDValue, 11> InstOps;
9423 InstOps.push_back(Chain);
9424 InstOps.push_back(SrcPtr);
9425 InstOps.push_back(DAG.getConstant(ArgSize, MVT::i32));
9426 InstOps.push_back(DAG.getConstant(ArgMode, MVT::i8));
9427 InstOps.push_back(DAG.getConstant(Align, MVT::i32));
9428 SDVTList VTs = DAG.getVTList(getPointerTy(), MVT::Other);
9429 SDValue VAARG = DAG.getMemIntrinsicNode(X86ISD::VAARG_64, dl,
9430 VTs, &InstOps[0], InstOps.size(),
9431 MVT::i64,
9432 MachinePointerInfo(SV),
9433 /*Align=*/0,
9434 /*Volatile=*/false,
9435 /*ReadMem=*/true,
9436 /*WriteMem=*/true);
9437 Chain = VAARG.getValue(1);
9438
9439 // Load the next argument and return it
9440 return DAG.getLoad(ArgVT, dl,
9441 Chain,
9442 VAARG,
9443 MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00009444 false, false, false, 0);
Dan Gohman9018e832008-05-10 01:26:14 +00009445}
9446
Dan Gohmand858e902010-04-17 15:26:15 +00009447SDValue X86TargetLowering::LowerVACOPY(SDValue Op, SelectionDAG &DAG) const {
Evan Chengae642192007-03-02 23:16:35 +00009448 // X86-64 va_list is a struct { i32, i32, i8*, i8* }.
Dan Gohman28269132008-04-18 20:55:41 +00009449 assert(Subtarget->is64Bit() && "This code only handles 64-bit va_copy!");
Dan Gohman475871a2008-07-27 21:46:04 +00009450 SDValue Chain = Op.getOperand(0);
9451 SDValue DstPtr = Op.getOperand(1);
9452 SDValue SrcPtr = Op.getOperand(2);
Dan Gohman69de1932008-02-06 22:27:42 +00009453 const Value *DstSV = cast<SrcValueSDNode>(Op.getOperand(3))->getValue();
9454 const Value *SrcSV = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Chris Lattnere72f2022010-09-21 05:40:29 +00009455 DebugLoc DL = Op.getDebugLoc();
Evan Chengae642192007-03-02 23:16:35 +00009456
Chris Lattnere72f2022010-09-21 05:40:29 +00009457 return DAG.getMemcpy(Chain, DL, DstPtr, SrcPtr,
Mon P Wang20adc9d2010-04-04 03:10:48 +00009458 DAG.getIntPtrConstant(24), 8, /*isVolatile*/false,
Michael J. Spencerec38de22010-10-10 22:04:20 +00009459 false,
Chris Lattnere72f2022010-09-21 05:40:29 +00009460 MachinePointerInfo(DstSV), MachinePointerInfo(SrcSV));
Evan Chengae642192007-03-02 23:16:35 +00009461}
9462
Craig Topper80e46362012-01-23 06:16:53 +00009463// getTargetVShiftNOde - Handle vector element shifts where the shift amount
9464// may or may not be a constant. Takes immediate version of shift as input.
9465static SDValue getTargetVShiftNode(unsigned Opc, DebugLoc dl, EVT VT,
9466 SDValue SrcOp, SDValue ShAmt,
9467 SelectionDAG &DAG) {
9468 assert(ShAmt.getValueType() == MVT::i32 && "ShAmt is not i32");
9469
9470 if (isa<ConstantSDNode>(ShAmt)) {
Nadav Rotemd896e242012-07-15 20:27:43 +00009471 // Constant may be a TargetConstant. Use a regular constant.
9472 uint32_t ShiftAmt = cast<ConstantSDNode>(ShAmt)->getZExtValue();
Craig Topper80e46362012-01-23 06:16:53 +00009473 switch (Opc) {
9474 default: llvm_unreachable("Unknown target vector shift node");
9475 case X86ISD::VSHLI:
9476 case X86ISD::VSRLI:
9477 case X86ISD::VSRAI:
Nadav Rotemd896e242012-07-15 20:27:43 +00009478 return DAG.getNode(Opc, dl, VT, SrcOp,
9479 DAG.getConstant(ShiftAmt, MVT::i32));
Craig Topper80e46362012-01-23 06:16:53 +00009480 }
9481 }
9482
9483 // Change opcode to non-immediate version
9484 switch (Opc) {
9485 default: llvm_unreachable("Unknown target vector shift node");
9486 case X86ISD::VSHLI: Opc = X86ISD::VSHL; break;
9487 case X86ISD::VSRLI: Opc = X86ISD::VSRL; break;
9488 case X86ISD::VSRAI: Opc = X86ISD::VSRA; break;
9489 }
9490
9491 // Need to build a vector containing shift amount
9492 // Shift amount is 32-bits, but SSE instructions read 64-bit, so fill with 0
9493 SDValue ShOps[4];
9494 ShOps[0] = ShAmt;
9495 ShOps[1] = DAG.getConstant(0, MVT::i32);
Craig Topper6d688152012-08-14 07:43:25 +00009496 ShOps[2] = ShOps[3] = DAG.getUNDEF(MVT::i32);
Craig Topper80e46362012-01-23 06:16:53 +00009497 ShAmt = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, &ShOps[0], 4);
Nadav Rotem65f489f2012-07-14 22:26:05 +00009498
9499 // The return type has to be a 128-bit type with the same element
9500 // type as the input type.
9501 MVT EltVT = VT.getVectorElementType().getSimpleVT();
9502 EVT ShVT = MVT::getVectorVT(EltVT, 128/EltVT.getSizeInBits());
9503
9504 ShAmt = DAG.getNode(ISD::BITCAST, dl, ShVT, ShAmt);
Craig Topper80e46362012-01-23 06:16:53 +00009505 return DAG.getNode(Opc, dl, VT, SrcOp, ShAmt);
9506}
9507
Dan Gohman475871a2008-07-27 21:46:04 +00009508SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00009509X86TargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009510 DebugLoc dl = Op.getDebugLoc();
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00009511 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00009512 switch (IntNo) {
Dan Gohman475871a2008-07-27 21:46:04 +00009513 default: return SDValue(); // Don't custom lower most intrinsics.
Evan Cheng5759f972008-05-04 09:15:50 +00009514 // Comparison intrinsics.
Evan Cheng0db9fe62006-04-25 20:13:52 +00009515 case Intrinsic::x86_sse_comieq_ss:
9516 case Intrinsic::x86_sse_comilt_ss:
9517 case Intrinsic::x86_sse_comile_ss:
9518 case Intrinsic::x86_sse_comigt_ss:
9519 case Intrinsic::x86_sse_comige_ss:
9520 case Intrinsic::x86_sse_comineq_ss:
9521 case Intrinsic::x86_sse_ucomieq_ss:
9522 case Intrinsic::x86_sse_ucomilt_ss:
9523 case Intrinsic::x86_sse_ucomile_ss:
9524 case Intrinsic::x86_sse_ucomigt_ss:
9525 case Intrinsic::x86_sse_ucomige_ss:
9526 case Intrinsic::x86_sse_ucomineq_ss:
9527 case Intrinsic::x86_sse2_comieq_sd:
9528 case Intrinsic::x86_sse2_comilt_sd:
9529 case Intrinsic::x86_sse2_comile_sd:
9530 case Intrinsic::x86_sse2_comigt_sd:
9531 case Intrinsic::x86_sse2_comige_sd:
9532 case Intrinsic::x86_sse2_comineq_sd:
9533 case Intrinsic::x86_sse2_ucomieq_sd:
9534 case Intrinsic::x86_sse2_ucomilt_sd:
9535 case Intrinsic::x86_sse2_ucomile_sd:
9536 case Intrinsic::x86_sse2_ucomigt_sd:
9537 case Intrinsic::x86_sse2_ucomige_sd:
9538 case Intrinsic::x86_sse2_ucomineq_sd: {
Craig Topper6d688152012-08-14 07:43:25 +00009539 unsigned Opc;
9540 ISD::CondCode CC;
Evan Cheng0db9fe62006-04-25 20:13:52 +00009541 switch (IntNo) {
Craig Topper86c7c582012-01-30 01:10:15 +00009542 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00009543 case Intrinsic::x86_sse_comieq_ss:
9544 case Intrinsic::x86_sse2_comieq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009545 Opc = X86ISD::COMI;
9546 CC = ISD::SETEQ;
9547 break;
Evan Cheng6be2c582006-04-05 23:38:46 +00009548 case Intrinsic::x86_sse_comilt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009549 case Intrinsic::x86_sse2_comilt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009550 Opc = X86ISD::COMI;
9551 CC = ISD::SETLT;
9552 break;
9553 case Intrinsic::x86_sse_comile_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009554 case Intrinsic::x86_sse2_comile_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009555 Opc = X86ISD::COMI;
9556 CC = ISD::SETLE;
9557 break;
9558 case Intrinsic::x86_sse_comigt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009559 case Intrinsic::x86_sse2_comigt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009560 Opc = X86ISD::COMI;
9561 CC = ISD::SETGT;
9562 break;
9563 case Intrinsic::x86_sse_comige_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009564 case Intrinsic::x86_sse2_comige_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009565 Opc = X86ISD::COMI;
9566 CC = ISD::SETGE;
9567 break;
9568 case Intrinsic::x86_sse_comineq_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009569 case Intrinsic::x86_sse2_comineq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009570 Opc = X86ISD::COMI;
9571 CC = ISD::SETNE;
9572 break;
9573 case Intrinsic::x86_sse_ucomieq_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009574 case Intrinsic::x86_sse2_ucomieq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009575 Opc = X86ISD::UCOMI;
9576 CC = ISD::SETEQ;
9577 break;
9578 case Intrinsic::x86_sse_ucomilt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009579 case Intrinsic::x86_sse2_ucomilt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009580 Opc = X86ISD::UCOMI;
9581 CC = ISD::SETLT;
9582 break;
9583 case Intrinsic::x86_sse_ucomile_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009584 case Intrinsic::x86_sse2_ucomile_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009585 Opc = X86ISD::UCOMI;
9586 CC = ISD::SETLE;
9587 break;
9588 case Intrinsic::x86_sse_ucomigt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009589 case Intrinsic::x86_sse2_ucomigt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009590 Opc = X86ISD::UCOMI;
9591 CC = ISD::SETGT;
9592 break;
9593 case Intrinsic::x86_sse_ucomige_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009594 case Intrinsic::x86_sse2_ucomige_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009595 Opc = X86ISD::UCOMI;
9596 CC = ISD::SETGE;
9597 break;
9598 case Intrinsic::x86_sse_ucomineq_ss:
9599 case Intrinsic::x86_sse2_ucomineq_sd:
9600 Opc = X86ISD::UCOMI;
9601 CC = ISD::SETNE;
9602 break;
Evan Cheng6be2c582006-04-05 23:38:46 +00009603 }
Evan Cheng734503b2006-09-11 02:19:56 +00009604
Dan Gohman475871a2008-07-27 21:46:04 +00009605 SDValue LHS = Op.getOperand(1);
9606 SDValue RHS = Op.getOperand(2);
Chris Lattner1c39d4c2008-12-24 23:53:05 +00009607 unsigned X86CC = TranslateX86CC(CC, true, LHS, RHS, DAG);
Dan Gohman1a492952009-10-20 16:22:37 +00009608 assert(X86CC != X86::COND_INVALID && "Unexpected illegal condition!");
Owen Anderson825b72b2009-08-11 20:47:22 +00009609 SDValue Cond = DAG.getNode(Opc, dl, MVT::i32, LHS, RHS);
9610 SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
9611 DAG.getConstant(X86CC, MVT::i8), Cond);
9612 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC);
Evan Cheng6be2c582006-04-05 23:38:46 +00009613 }
Craig Topper6d688152012-08-14 07:43:25 +00009614
Duncan Sands04aa4ae2011-09-23 16:10:22 +00009615 // Arithmetic intrinsics.
Craig Topper5b209e82012-02-05 03:14:49 +00009616 case Intrinsic::x86_sse2_pmulu_dq:
9617 case Intrinsic::x86_avx2_pmulu_dq:
9618 return DAG.getNode(X86ISD::PMULUDQ, dl, Op.getValueType(),
9619 Op.getOperand(1), Op.getOperand(2));
Craig Topper6d688152012-08-14 07:43:25 +00009620
9621 // SSE3/AVX horizontal add/sub intrinsics
Duncan Sands04aa4ae2011-09-23 16:10:22 +00009622 case Intrinsic::x86_sse3_hadd_ps:
9623 case Intrinsic::x86_sse3_hadd_pd:
9624 case Intrinsic::x86_avx_hadd_ps_256:
9625 case Intrinsic::x86_avx_hadd_pd_256:
Duncan Sands04aa4ae2011-09-23 16:10:22 +00009626 case Intrinsic::x86_sse3_hsub_ps:
9627 case Intrinsic::x86_sse3_hsub_pd:
9628 case Intrinsic::x86_avx_hsub_ps_256:
9629 case Intrinsic::x86_avx_hsub_pd_256:
Craig Topper4bb3f342012-01-25 05:37:32 +00009630 case Intrinsic::x86_ssse3_phadd_w_128:
9631 case Intrinsic::x86_ssse3_phadd_d_128:
9632 case Intrinsic::x86_avx2_phadd_w:
9633 case Intrinsic::x86_avx2_phadd_d:
Craig Topper4bb3f342012-01-25 05:37:32 +00009634 case Intrinsic::x86_ssse3_phsub_w_128:
9635 case Intrinsic::x86_ssse3_phsub_d_128:
9636 case Intrinsic::x86_avx2_phsub_w:
Craig Topper6d688152012-08-14 07:43:25 +00009637 case Intrinsic::x86_avx2_phsub_d: {
9638 unsigned Opcode;
9639 switch (IntNo) {
9640 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
9641 case Intrinsic::x86_sse3_hadd_ps:
9642 case Intrinsic::x86_sse3_hadd_pd:
9643 case Intrinsic::x86_avx_hadd_ps_256:
9644 case Intrinsic::x86_avx_hadd_pd_256:
9645 Opcode = X86ISD::FHADD;
9646 break;
9647 case Intrinsic::x86_sse3_hsub_ps:
9648 case Intrinsic::x86_sse3_hsub_pd:
9649 case Intrinsic::x86_avx_hsub_ps_256:
9650 case Intrinsic::x86_avx_hsub_pd_256:
9651 Opcode = X86ISD::FHSUB;
9652 break;
9653 case Intrinsic::x86_ssse3_phadd_w_128:
9654 case Intrinsic::x86_ssse3_phadd_d_128:
9655 case Intrinsic::x86_avx2_phadd_w:
9656 case Intrinsic::x86_avx2_phadd_d:
9657 Opcode = X86ISD::HADD;
9658 break;
9659 case Intrinsic::x86_ssse3_phsub_w_128:
9660 case Intrinsic::x86_ssse3_phsub_d_128:
9661 case Intrinsic::x86_avx2_phsub_w:
9662 case Intrinsic::x86_avx2_phsub_d:
9663 Opcode = X86ISD::HSUB;
9664 break;
9665 }
9666 return DAG.getNode(Opcode, dl, Op.getValueType(),
Craig Topper4bb3f342012-01-25 05:37:32 +00009667 Op.getOperand(1), Op.getOperand(2));
Craig Topper6d688152012-08-14 07:43:25 +00009668 }
9669
9670 // AVX2 variable shift intrinsics
Craig Topper98fc7292011-11-19 17:46:46 +00009671 case Intrinsic::x86_avx2_psllv_d:
9672 case Intrinsic::x86_avx2_psllv_q:
9673 case Intrinsic::x86_avx2_psllv_d_256:
9674 case Intrinsic::x86_avx2_psllv_q_256:
Craig Topper98fc7292011-11-19 17:46:46 +00009675 case Intrinsic::x86_avx2_psrlv_d:
9676 case Intrinsic::x86_avx2_psrlv_q:
9677 case Intrinsic::x86_avx2_psrlv_d_256:
9678 case Intrinsic::x86_avx2_psrlv_q_256:
Craig Topper98fc7292011-11-19 17:46:46 +00009679 case Intrinsic::x86_avx2_psrav_d:
Craig Topper6d688152012-08-14 07:43:25 +00009680 case Intrinsic::x86_avx2_psrav_d_256: {
9681 unsigned Opcode;
9682 switch (IntNo) {
9683 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
9684 case Intrinsic::x86_avx2_psllv_d:
9685 case Intrinsic::x86_avx2_psllv_q:
9686 case Intrinsic::x86_avx2_psllv_d_256:
9687 case Intrinsic::x86_avx2_psllv_q_256:
9688 Opcode = ISD::SHL;
9689 break;
9690 case Intrinsic::x86_avx2_psrlv_d:
9691 case Intrinsic::x86_avx2_psrlv_q:
9692 case Intrinsic::x86_avx2_psrlv_d_256:
9693 case Intrinsic::x86_avx2_psrlv_q_256:
9694 Opcode = ISD::SRL;
9695 break;
9696 case Intrinsic::x86_avx2_psrav_d:
9697 case Intrinsic::x86_avx2_psrav_d_256:
9698 Opcode = ISD::SRA;
9699 break;
9700 }
9701 return DAG.getNode(Opcode, dl, Op.getValueType(),
9702 Op.getOperand(1), Op.getOperand(2));
9703 }
9704
Craig Topper969ba282012-01-25 06:43:11 +00009705 case Intrinsic::x86_ssse3_pshuf_b_128:
9706 case Intrinsic::x86_avx2_pshuf_b:
9707 return DAG.getNode(X86ISD::PSHUFB, dl, Op.getValueType(),
9708 Op.getOperand(1), Op.getOperand(2));
Craig Topper6d688152012-08-14 07:43:25 +00009709
Craig Topper969ba282012-01-25 06:43:11 +00009710 case Intrinsic::x86_ssse3_psign_b_128:
9711 case Intrinsic::x86_ssse3_psign_w_128:
9712 case Intrinsic::x86_ssse3_psign_d_128:
9713 case Intrinsic::x86_avx2_psign_b:
9714 case Intrinsic::x86_avx2_psign_w:
9715 case Intrinsic::x86_avx2_psign_d:
9716 return DAG.getNode(X86ISD::PSIGN, dl, Op.getValueType(),
9717 Op.getOperand(1), Op.getOperand(2));
Craig Topper6d688152012-08-14 07:43:25 +00009718
Craig Toppere566cd02012-01-26 07:18:03 +00009719 case Intrinsic::x86_sse41_insertps:
9720 return DAG.getNode(X86ISD::INSERTPS, dl, Op.getValueType(),
9721 Op.getOperand(1), Op.getOperand(2), Op.getOperand(3));
Craig Topper6d688152012-08-14 07:43:25 +00009722
Craig Toppere566cd02012-01-26 07:18:03 +00009723 case Intrinsic::x86_avx_vperm2f128_ps_256:
9724 case Intrinsic::x86_avx_vperm2f128_pd_256:
9725 case Intrinsic::x86_avx_vperm2f128_si_256:
9726 case Intrinsic::x86_avx2_vperm2i128:
9727 return DAG.getNode(X86ISD::VPERM2X128, dl, Op.getValueType(),
9728 Op.getOperand(1), Op.getOperand(2), Op.getOperand(3));
Craig Topper6d688152012-08-14 07:43:25 +00009729
Craig Topperffa6c402012-04-16 07:13:00 +00009730 case Intrinsic::x86_avx2_permd:
9731 case Intrinsic::x86_avx2_permps:
9732 // Operands intentionally swapped. Mask is last operand to intrinsic,
9733 // but second operand for node/intruction.
9734 return DAG.getNode(X86ISD::VPERMV, dl, Op.getValueType(),
9735 Op.getOperand(2), Op.getOperand(1));
Craig Topper98fc7292011-11-19 17:46:46 +00009736
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009737 // ptest and testp intrinsics. The intrinsic these come from are designed to
9738 // return an integer value, not just an instruction so lower it to the ptest
9739 // or testp pattern and a setcc for the result.
Eric Christopher71c67532009-07-29 00:28:05 +00009740 case Intrinsic::x86_sse41_ptestz:
9741 case Intrinsic::x86_sse41_ptestc:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009742 case Intrinsic::x86_sse41_ptestnzc:
9743 case Intrinsic::x86_avx_ptestz_256:
9744 case Intrinsic::x86_avx_ptestc_256:
9745 case Intrinsic::x86_avx_ptestnzc_256:
9746 case Intrinsic::x86_avx_vtestz_ps:
9747 case Intrinsic::x86_avx_vtestc_ps:
9748 case Intrinsic::x86_avx_vtestnzc_ps:
9749 case Intrinsic::x86_avx_vtestz_pd:
9750 case Intrinsic::x86_avx_vtestc_pd:
9751 case Intrinsic::x86_avx_vtestnzc_pd:
9752 case Intrinsic::x86_avx_vtestz_ps_256:
9753 case Intrinsic::x86_avx_vtestc_ps_256:
9754 case Intrinsic::x86_avx_vtestnzc_ps_256:
9755 case Intrinsic::x86_avx_vtestz_pd_256:
9756 case Intrinsic::x86_avx_vtestc_pd_256:
9757 case Intrinsic::x86_avx_vtestnzc_pd_256: {
9758 bool IsTestPacked = false;
Craig Topper6d688152012-08-14 07:43:25 +00009759 unsigned X86CC;
Eric Christopher71c67532009-07-29 00:28:05 +00009760 switch (IntNo) {
Eric Christopher978dae32009-07-29 18:14:04 +00009761 default: llvm_unreachable("Bad fallthrough in Intrinsic lowering.");
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009762 case Intrinsic::x86_avx_vtestz_ps:
9763 case Intrinsic::x86_avx_vtestz_pd:
9764 case Intrinsic::x86_avx_vtestz_ps_256:
9765 case Intrinsic::x86_avx_vtestz_pd_256:
9766 IsTestPacked = true; // Fallthrough
Eric Christopher71c67532009-07-29 00:28:05 +00009767 case Intrinsic::x86_sse41_ptestz:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009768 case Intrinsic::x86_avx_ptestz_256:
Eric Christopher71c67532009-07-29 00:28:05 +00009769 // ZF = 1
9770 X86CC = X86::COND_E;
9771 break;
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009772 case Intrinsic::x86_avx_vtestc_ps:
9773 case Intrinsic::x86_avx_vtestc_pd:
9774 case Intrinsic::x86_avx_vtestc_ps_256:
9775 case Intrinsic::x86_avx_vtestc_pd_256:
9776 IsTestPacked = true; // Fallthrough
Eric Christopher71c67532009-07-29 00:28:05 +00009777 case Intrinsic::x86_sse41_ptestc:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009778 case Intrinsic::x86_avx_ptestc_256:
Eric Christopher71c67532009-07-29 00:28:05 +00009779 // CF = 1
9780 X86CC = X86::COND_B;
9781 break;
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009782 case Intrinsic::x86_avx_vtestnzc_ps:
9783 case Intrinsic::x86_avx_vtestnzc_pd:
9784 case Intrinsic::x86_avx_vtestnzc_ps_256:
9785 case Intrinsic::x86_avx_vtestnzc_pd_256:
9786 IsTestPacked = true; // Fallthrough
Eric Christopherfd179292009-08-27 18:07:15 +00009787 case Intrinsic::x86_sse41_ptestnzc:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009788 case Intrinsic::x86_avx_ptestnzc_256:
Eric Christopher71c67532009-07-29 00:28:05 +00009789 // ZF and CF = 0
9790 X86CC = X86::COND_A;
9791 break;
9792 }
Eric Christopherfd179292009-08-27 18:07:15 +00009793
Eric Christopher71c67532009-07-29 00:28:05 +00009794 SDValue LHS = Op.getOperand(1);
9795 SDValue RHS = Op.getOperand(2);
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009796 unsigned TestOpc = IsTestPacked ? X86ISD::TESTP : X86ISD::PTEST;
9797 SDValue Test = DAG.getNode(TestOpc, dl, MVT::i32, LHS, RHS);
Owen Anderson825b72b2009-08-11 20:47:22 +00009798 SDValue CC = DAG.getConstant(X86CC, MVT::i8);
9799 SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8, CC, Test);
9800 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC);
Eric Christopher71c67532009-07-29 00:28:05 +00009801 }
Evan Cheng5759f972008-05-04 09:15:50 +00009802
Craig Topper80e46362012-01-23 06:16:53 +00009803 // SSE/AVX shift intrinsics
9804 case Intrinsic::x86_sse2_psll_w:
9805 case Intrinsic::x86_sse2_psll_d:
9806 case Intrinsic::x86_sse2_psll_q:
9807 case Intrinsic::x86_avx2_psll_w:
9808 case Intrinsic::x86_avx2_psll_d:
9809 case Intrinsic::x86_avx2_psll_q:
Craig Topper80e46362012-01-23 06:16:53 +00009810 case Intrinsic::x86_sse2_psrl_w:
9811 case Intrinsic::x86_sse2_psrl_d:
9812 case Intrinsic::x86_sse2_psrl_q:
9813 case Intrinsic::x86_avx2_psrl_w:
9814 case Intrinsic::x86_avx2_psrl_d:
9815 case Intrinsic::x86_avx2_psrl_q:
Craig Topper80e46362012-01-23 06:16:53 +00009816 case Intrinsic::x86_sse2_psra_w:
9817 case Intrinsic::x86_sse2_psra_d:
9818 case Intrinsic::x86_avx2_psra_w:
Craig Topper6d688152012-08-14 07:43:25 +00009819 case Intrinsic::x86_avx2_psra_d: {
9820 unsigned Opcode;
9821 switch (IntNo) {
9822 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
9823 case Intrinsic::x86_sse2_psll_w:
9824 case Intrinsic::x86_sse2_psll_d:
9825 case Intrinsic::x86_sse2_psll_q:
9826 case Intrinsic::x86_avx2_psll_w:
9827 case Intrinsic::x86_avx2_psll_d:
9828 case Intrinsic::x86_avx2_psll_q:
9829 Opcode = X86ISD::VSHL;
9830 break;
9831 case Intrinsic::x86_sse2_psrl_w:
9832 case Intrinsic::x86_sse2_psrl_d:
9833 case Intrinsic::x86_sse2_psrl_q:
9834 case Intrinsic::x86_avx2_psrl_w:
9835 case Intrinsic::x86_avx2_psrl_d:
9836 case Intrinsic::x86_avx2_psrl_q:
9837 Opcode = X86ISD::VSRL;
9838 break;
9839 case Intrinsic::x86_sse2_psra_w:
9840 case Intrinsic::x86_sse2_psra_d:
9841 case Intrinsic::x86_avx2_psra_w:
9842 case Intrinsic::x86_avx2_psra_d:
9843 Opcode = X86ISD::VSRA;
9844 break;
9845 }
9846 return DAG.getNode(Opcode, dl, Op.getValueType(),
Craig Topper80e46362012-01-23 06:16:53 +00009847 Op.getOperand(1), Op.getOperand(2));
Craig Topper6d688152012-08-14 07:43:25 +00009848 }
9849
9850 // SSE/AVX immediate shift intrinsics
Evan Cheng5759f972008-05-04 09:15:50 +00009851 case Intrinsic::x86_sse2_pslli_w:
9852 case Intrinsic::x86_sse2_pslli_d:
9853 case Intrinsic::x86_sse2_pslli_q:
Craig Topper80e46362012-01-23 06:16:53 +00009854 case Intrinsic::x86_avx2_pslli_w:
9855 case Intrinsic::x86_avx2_pslli_d:
9856 case Intrinsic::x86_avx2_pslli_q:
Evan Cheng5759f972008-05-04 09:15:50 +00009857 case Intrinsic::x86_sse2_psrli_w:
9858 case Intrinsic::x86_sse2_psrli_d:
9859 case Intrinsic::x86_sse2_psrli_q:
Craig Topper80e46362012-01-23 06:16:53 +00009860 case Intrinsic::x86_avx2_psrli_w:
9861 case Intrinsic::x86_avx2_psrli_d:
9862 case Intrinsic::x86_avx2_psrli_q:
Evan Cheng5759f972008-05-04 09:15:50 +00009863 case Intrinsic::x86_sse2_psrai_w:
9864 case Intrinsic::x86_sse2_psrai_d:
Craig Topper80e46362012-01-23 06:16:53 +00009865 case Intrinsic::x86_avx2_psrai_w:
Craig Topper6d688152012-08-14 07:43:25 +00009866 case Intrinsic::x86_avx2_psrai_d: {
9867 unsigned Opcode;
9868 switch (IntNo) {
9869 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
9870 case Intrinsic::x86_sse2_pslli_w:
9871 case Intrinsic::x86_sse2_pslli_d:
9872 case Intrinsic::x86_sse2_pslli_q:
9873 case Intrinsic::x86_avx2_pslli_w:
9874 case Intrinsic::x86_avx2_pslli_d:
9875 case Intrinsic::x86_avx2_pslli_q:
9876 Opcode = X86ISD::VSHLI;
9877 break;
9878 case Intrinsic::x86_sse2_psrli_w:
9879 case Intrinsic::x86_sse2_psrli_d:
9880 case Intrinsic::x86_sse2_psrli_q:
9881 case Intrinsic::x86_avx2_psrli_w:
9882 case Intrinsic::x86_avx2_psrli_d:
9883 case Intrinsic::x86_avx2_psrli_q:
9884 Opcode = X86ISD::VSRLI;
9885 break;
9886 case Intrinsic::x86_sse2_psrai_w:
9887 case Intrinsic::x86_sse2_psrai_d:
9888 case Intrinsic::x86_avx2_psrai_w:
9889 case Intrinsic::x86_avx2_psrai_d:
9890 Opcode = X86ISD::VSRAI;
9891 break;
9892 }
9893 return getTargetVShiftNode(Opcode, dl, Op.getValueType(),
Craig Topper80e46362012-01-23 06:16:53 +00009894 Op.getOperand(1), Op.getOperand(2), DAG);
Craig Topper6d688152012-08-14 07:43:25 +00009895 }
9896
Craig Topper80e46362012-01-23 06:16:53 +00009897 // Fix vector shift instructions where the last operand is a non-immediate
9898 // i32 value.
Evan Cheng5759f972008-05-04 09:15:50 +00009899 case Intrinsic::x86_mmx_pslli_w:
9900 case Intrinsic::x86_mmx_pslli_d:
9901 case Intrinsic::x86_mmx_pslli_q:
9902 case Intrinsic::x86_mmx_psrli_w:
9903 case Intrinsic::x86_mmx_psrli_d:
9904 case Intrinsic::x86_mmx_psrli_q:
9905 case Intrinsic::x86_mmx_psrai_w:
9906 case Intrinsic::x86_mmx_psrai_d: {
Dan Gohman475871a2008-07-27 21:46:04 +00009907 SDValue ShAmt = Op.getOperand(2);
Evan Cheng5759f972008-05-04 09:15:50 +00009908 if (isa<ConstantSDNode>(ShAmt))
Dan Gohman475871a2008-07-27 21:46:04 +00009909 return SDValue();
Evan Cheng5759f972008-05-04 09:15:50 +00009910
Craig Topper6d688152012-08-14 07:43:25 +00009911 unsigned NewIntNo;
Evan Cheng5759f972008-05-04 09:15:50 +00009912 switch (IntNo) {
Craig Topper6d688152012-08-14 07:43:25 +00009913 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
Craig Topper80e46362012-01-23 06:16:53 +00009914 case Intrinsic::x86_mmx_pslli_w:
9915 NewIntNo = Intrinsic::x86_mmx_psll_w;
Evan Cheng5759f972008-05-04 09:15:50 +00009916 break;
Craig Topper80e46362012-01-23 06:16:53 +00009917 case Intrinsic::x86_mmx_pslli_d:
9918 NewIntNo = Intrinsic::x86_mmx_psll_d;
Evan Cheng5759f972008-05-04 09:15:50 +00009919 break;
Craig Topper80e46362012-01-23 06:16:53 +00009920 case Intrinsic::x86_mmx_pslli_q:
9921 NewIntNo = Intrinsic::x86_mmx_psll_q;
Evan Cheng5759f972008-05-04 09:15:50 +00009922 break;
Craig Topper80e46362012-01-23 06:16:53 +00009923 case Intrinsic::x86_mmx_psrli_w:
9924 NewIntNo = Intrinsic::x86_mmx_psrl_w;
Evan Cheng5759f972008-05-04 09:15:50 +00009925 break;
Craig Topper80e46362012-01-23 06:16:53 +00009926 case Intrinsic::x86_mmx_psrli_d:
9927 NewIntNo = Intrinsic::x86_mmx_psrl_d;
Evan Cheng5759f972008-05-04 09:15:50 +00009928 break;
Craig Topper80e46362012-01-23 06:16:53 +00009929 case Intrinsic::x86_mmx_psrli_q:
9930 NewIntNo = Intrinsic::x86_mmx_psrl_q;
Evan Cheng5759f972008-05-04 09:15:50 +00009931 break;
Craig Topper80e46362012-01-23 06:16:53 +00009932 case Intrinsic::x86_mmx_psrai_w:
9933 NewIntNo = Intrinsic::x86_mmx_psra_w;
Evan Cheng5759f972008-05-04 09:15:50 +00009934 break;
Craig Topper80e46362012-01-23 06:16:53 +00009935 case Intrinsic::x86_mmx_psrai_d:
9936 NewIntNo = Intrinsic::x86_mmx_psra_d;
Evan Cheng5759f972008-05-04 09:15:50 +00009937 break;
Evan Cheng5759f972008-05-04 09:15:50 +00009938 }
Mon P Wangefa42202009-09-03 19:56:25 +00009939
9940 // The vector shift intrinsics with scalars uses 32b shift amounts but
9941 // the sse2/mmx shift instructions reads 64 bits. Set the upper 32 bits
9942 // to be zero.
Craig Topper80e46362012-01-23 06:16:53 +00009943 ShAmt = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v2i32, ShAmt,
9944 DAG.getConstant(0, MVT::i32));
Dale Johannesen0488fb62010-09-30 23:57:10 +00009945// FIXME this must be lowered to get rid of the invalid type.
Mon P Wangefa42202009-09-03 19:56:25 +00009946
Owen Andersone50ed302009-08-10 22:56:29 +00009947 EVT VT = Op.getValueType();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00009948 ShAmt = DAG.getNode(ISD::BITCAST, dl, VT, ShAmt);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009949 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009950 DAG.getConstant(NewIntNo, MVT::i32),
Evan Cheng5759f972008-05-04 09:15:50 +00009951 Op.getOperand(1), ShAmt);
9952 }
Craig Topper4feb6472012-08-06 06:22:36 +00009953 case Intrinsic::x86_sse42_pcmpistria128:
9954 case Intrinsic::x86_sse42_pcmpestria128:
9955 case Intrinsic::x86_sse42_pcmpistric128:
9956 case Intrinsic::x86_sse42_pcmpestric128:
9957 case Intrinsic::x86_sse42_pcmpistrio128:
9958 case Intrinsic::x86_sse42_pcmpestrio128:
9959 case Intrinsic::x86_sse42_pcmpistris128:
9960 case Intrinsic::x86_sse42_pcmpestris128:
9961 case Intrinsic::x86_sse42_pcmpistriz128:
9962 case Intrinsic::x86_sse42_pcmpestriz128: {
9963 unsigned Opcode;
9964 unsigned X86CC;
9965 switch (IntNo) {
9966 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
9967 case Intrinsic::x86_sse42_pcmpistria128:
9968 Opcode = X86ISD::PCMPISTRI;
9969 X86CC = X86::COND_A;
9970 break;
9971 case Intrinsic::x86_sse42_pcmpestria128:
9972 Opcode = X86ISD::PCMPESTRI;
9973 X86CC = X86::COND_A;
9974 break;
9975 case Intrinsic::x86_sse42_pcmpistric128:
9976 Opcode = X86ISD::PCMPISTRI;
9977 X86CC = X86::COND_B;
9978 break;
9979 case Intrinsic::x86_sse42_pcmpestric128:
9980 Opcode = X86ISD::PCMPESTRI;
9981 X86CC = X86::COND_B;
9982 break;
9983 case Intrinsic::x86_sse42_pcmpistrio128:
9984 Opcode = X86ISD::PCMPISTRI;
9985 X86CC = X86::COND_O;
9986 break;
9987 case Intrinsic::x86_sse42_pcmpestrio128:
9988 Opcode = X86ISD::PCMPESTRI;
9989 X86CC = X86::COND_O;
9990 break;
9991 case Intrinsic::x86_sse42_pcmpistris128:
9992 Opcode = X86ISD::PCMPISTRI;
9993 X86CC = X86::COND_S;
9994 break;
9995 case Intrinsic::x86_sse42_pcmpestris128:
9996 Opcode = X86ISD::PCMPESTRI;
9997 X86CC = X86::COND_S;
9998 break;
9999 case Intrinsic::x86_sse42_pcmpistriz128:
10000 Opcode = X86ISD::PCMPISTRI;
10001 X86CC = X86::COND_E;
10002 break;
10003 case Intrinsic::x86_sse42_pcmpestriz128:
10004 Opcode = X86ISD::PCMPESTRI;
10005 X86CC = X86::COND_E;
10006 break;
10007 }
10008 SmallVector<SDValue, 5> NewOps;
10009 NewOps.append(Op->op_begin()+1, Op->op_end());
10010 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::i32);
10011 SDValue PCMP = DAG.getNode(Opcode, dl, VTs, NewOps.data(), NewOps.size());
10012 SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
10013 DAG.getConstant(X86CC, MVT::i8),
10014 SDValue(PCMP.getNode(), 1));
10015 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC);
10016 }
Craig Topper6d688152012-08-14 07:43:25 +000010017
Craig Topper4feb6472012-08-06 06:22:36 +000010018 case Intrinsic::x86_sse42_pcmpistri128:
10019 case Intrinsic::x86_sse42_pcmpestri128: {
10020 unsigned Opcode;
10021 if (IntNo == Intrinsic::x86_sse42_pcmpistri128)
10022 Opcode = X86ISD::PCMPISTRI;
10023 else
10024 Opcode = X86ISD::PCMPESTRI;
10025
10026 SmallVector<SDValue, 5> NewOps;
10027 NewOps.append(Op->op_begin()+1, Op->op_end());
10028 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::i32);
10029 return DAG.getNode(Opcode, dl, VTs, NewOps.data(), NewOps.size());
10030 }
Evan Cheng38bcbaf2005-12-23 07:31:11 +000010031 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000010032}
Evan Cheng72261582005-12-20 06:22:03 +000010033
Benjamin Kramerb9bee042012-07-12 09:31:43 +000010034SDValue
10035X86TargetLowering::LowerINTRINSIC_W_CHAIN(SDValue Op, SelectionDAG &DAG) const {
10036 DebugLoc dl = Op.getDebugLoc();
10037 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
10038 switch (IntNo) {
10039 default: return SDValue(); // Don't custom lower most intrinsics.
10040
10041 // RDRAND intrinsics.
10042 case Intrinsic::x86_rdrand_16:
10043 case Intrinsic::x86_rdrand_32:
10044 case Intrinsic::x86_rdrand_64: {
10045 // Emit the node with the right value type.
Benjamin Kramerfeae00a2012-07-12 18:14:57 +000010046 SDVTList VTs = DAG.getVTList(Op->getValueType(0), MVT::Glue, MVT::Other);
10047 SDValue Result = DAG.getNode(X86ISD::RDRAND, dl, VTs, Op.getOperand(0));
Benjamin Kramerb9bee042012-07-12 09:31:43 +000010048
10049 // If the value returned by RDRAND was valid (CF=1), return 1. Otherwise
10050 // return the value from Rand, which is always 0, casted to i32.
10051 SDValue Ops[] = { DAG.getZExtOrTrunc(Result, dl, Op->getValueType(1)),
10052 DAG.getConstant(1, Op->getValueType(1)),
10053 DAG.getConstant(X86::COND_B, MVT::i32),
10054 SDValue(Result.getNode(), 1) };
10055 SDValue isValid = DAG.getNode(X86ISD::CMOV, dl,
10056 DAG.getVTList(Op->getValueType(1), MVT::Glue),
10057 Ops, 4);
10058
10059 // Return { result, isValid, chain }.
10060 return DAG.getNode(ISD::MERGE_VALUES, dl, Op->getVTList(), Result, isValid,
Benjamin Kramerfeae00a2012-07-12 18:14:57 +000010061 SDValue(Result.getNode(), 2));
Benjamin Kramerb9bee042012-07-12 09:31:43 +000010062 }
10063 }
10064}
10065
Dan Gohmand858e902010-04-17 15:26:15 +000010066SDValue X86TargetLowering::LowerRETURNADDR(SDValue Op,
10067 SelectionDAG &DAG) const {
Evan Cheng2457f2c2010-05-22 01:47:14 +000010068 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
10069 MFI->setReturnAddressIsTaken(true);
10070
Bill Wendling64e87322009-01-16 19:25:27 +000010071 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Dale Johannesen6f38cb62009-02-07 19:59:05 +000010072 DebugLoc dl = Op.getDebugLoc();
Bill Wendling64e87322009-01-16 19:25:27 +000010073
10074 if (Depth > 0) {
10075 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
10076 SDValue Offset =
10077 DAG.getConstant(TD->getPointerSize(),
Owen Anderson825b72b2009-08-11 20:47:22 +000010078 Subtarget->is64Bit() ? MVT::i64 : MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +000010079 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
Scott Michelfdc40a02009-02-17 22:15:04 +000010080 DAG.getNode(ISD::ADD, dl, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +000010081 FrameAddr, Offset),
Pete Cooperd752e0f2011-11-08 18:42:53 +000010082 MachinePointerInfo(), false, false, false, 0);
Bill Wendling64e87322009-01-16 19:25:27 +000010083 }
10084
10085 // Just load the return address.
Dan Gohman475871a2008-07-27 21:46:04 +000010086 SDValue RetAddrFI = getReturnAddressFrameIndex(DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +000010087 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000010088 RetAddrFI, MachinePointerInfo(), false, false, false, 0);
Nate Begemanbcc5f362007-01-29 22:58:52 +000010089}
10090
Dan Gohmand858e902010-04-17 15:26:15 +000010091SDValue X86TargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng184793f2008-09-27 01:56:22 +000010092 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
10093 MFI->setFrameAddressIsTaken(true);
Evan Cheng2457f2c2010-05-22 01:47:14 +000010094
Owen Andersone50ed302009-08-10 22:56:29 +000010095 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +000010096 DebugLoc dl = Op.getDebugLoc(); // FIXME probably not meaningful
Evan Cheng184793f2008-09-27 01:56:22 +000010097 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
10098 unsigned FrameReg = Subtarget->is64Bit() ? X86::RBP : X86::EBP;
Dale Johannesendd64c412009-02-04 00:33:20 +000010099 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, VT);
Evan Cheng184793f2008-09-27 01:56:22 +000010100 while (Depth--)
Chris Lattner51abfe42010-09-21 06:02:19 +000010101 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr,
10102 MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000010103 false, false, false, 0);
Evan Cheng184793f2008-09-27 01:56:22 +000010104 return FrameAddr;
Nate Begemanbcc5f362007-01-29 22:58:52 +000010105}
10106
Dan Gohman475871a2008-07-27 21:46:04 +000010107SDValue X86TargetLowering::LowerFRAME_TO_ARGS_OFFSET(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +000010108 SelectionDAG &DAG) const {
Anton Korobeynikovbff66b02008-09-09 18:22:57 +000010109 return DAG.getIntPtrConstant(2*TD->getPointerSize());
Anton Korobeynikov2365f512007-07-14 14:06:15 +000010110}
10111
Dan Gohmand858e902010-04-17 15:26:15 +000010112SDValue X86TargetLowering::LowerEH_RETURN(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +000010113 SDValue Chain = Op.getOperand(0);
10114 SDValue Offset = Op.getOperand(1);
10115 SDValue Handler = Op.getOperand(2);
Dale Johannesen6f38cb62009-02-07 19:59:05 +000010116 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov2365f512007-07-14 14:06:15 +000010117
Dan Gohmand8816272010-08-11 18:14:00 +000010118 SDValue Frame = DAG.getCopyFromReg(DAG.getEntryNode(), dl,
10119 Subtarget->is64Bit() ? X86::RBP : X86::EBP,
10120 getPointerTy());
Anton Korobeynikovb84c1672008-09-08 21:12:47 +000010121 unsigned StoreAddrReg = (Subtarget->is64Bit() ? X86::RCX : X86::ECX);
Anton Korobeynikov2365f512007-07-14 14:06:15 +000010122
Dan Gohmand8816272010-08-11 18:14:00 +000010123 SDValue StoreAddr = DAG.getNode(ISD::ADD, dl, getPointerTy(), Frame,
10124 DAG.getIntPtrConstant(TD->getPointerSize()));
Dale Johannesene4d209d2009-02-03 20:21:25 +000010125 StoreAddr = DAG.getNode(ISD::ADD, dl, getPointerTy(), StoreAddr, Offset);
Chris Lattner8026a9d2010-09-21 17:50:43 +000010126 Chain = DAG.getStore(Chain, dl, Handler, StoreAddr, MachinePointerInfo(),
10127 false, false, 0);
Dale Johannesendd64c412009-02-04 00:33:20 +000010128 Chain = DAG.getCopyToReg(Chain, dl, StoreAddrReg, StoreAddr);
Anton Korobeynikov2365f512007-07-14 14:06:15 +000010129
Dale Johannesene4d209d2009-02-03 20:21:25 +000010130 return DAG.getNode(X86ISD::EH_RETURN, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +000010131 MVT::Other,
Anton Korobeynikovb84c1672008-09-08 21:12:47 +000010132 Chain, DAG.getRegister(StoreAddrReg, getPointerTy()));
Anton Korobeynikov2365f512007-07-14 14:06:15 +000010133}
10134
Duncan Sands4a544a72011-09-06 13:37:06 +000010135SDValue X86TargetLowering::LowerADJUST_TRAMPOLINE(SDValue Op,
10136 SelectionDAG &DAG) const {
10137 return Op.getOperand(0);
10138}
10139
10140SDValue X86TargetLowering::LowerINIT_TRAMPOLINE(SDValue Op,
10141 SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +000010142 SDValue Root = Op.getOperand(0);
10143 SDValue Trmp = Op.getOperand(1); // trampoline
10144 SDValue FPtr = Op.getOperand(2); // nested function
10145 SDValue Nest = Op.getOperand(3); // 'nest' parameter value
Dale Johannesen6f38cb62009-02-07 19:59:05 +000010146 DebugLoc dl = Op.getDebugLoc();
Duncan Sandsb116fac2007-07-27 20:02:49 +000010147
Dan Gohman69de1932008-02-06 22:27:42 +000010148 const Value *TrmpAddr = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Duncan Sandsb116fac2007-07-27 20:02:49 +000010149
10150 if (Subtarget->is64Bit()) {
Dan Gohman475871a2008-07-27 21:46:04 +000010151 SDValue OutChains[6];
Duncan Sands339e14f2008-01-16 22:55:25 +000010152
10153 // Large code-model.
Chris Lattnera62fe662010-02-05 19:20:30 +000010154 const unsigned char JMP64r = 0xFF; // 64-bit jmp through register opcode.
10155 const unsigned char MOV64ri = 0xB8; // X86::MOV64ri opcode.
Duncan Sands339e14f2008-01-16 22:55:25 +000010156
Evan Cheng0e6a0522011-07-18 20:57:22 +000010157 const unsigned char N86R10 = X86_MC::getX86RegNum(X86::R10);
10158 const unsigned char N86R11 = X86_MC::getX86RegNum(X86::R11);
Duncan Sands339e14f2008-01-16 22:55:25 +000010159
10160 const unsigned char REX_WB = 0x40 | 0x08 | 0x01; // REX prefix
10161
10162 // Load the pointer to the nested function into R11.
10163 unsigned OpCode = ((MOV64ri | N86R11) << 8) | REX_WB; // movabsq r11
Dan Gohman475871a2008-07-27 21:46:04 +000010164 SDValue Addr = Trmp;
Owen Anderson825b72b2009-08-11 20:47:22 +000010165 OutChains[0] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Chris Lattner8026a9d2010-09-21 17:50:43 +000010166 Addr, MachinePointerInfo(TrmpAddr),
10167 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +000010168
Owen Anderson825b72b2009-08-11 20:47:22 +000010169 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
10170 DAG.getConstant(2, MVT::i64));
Chris Lattner8026a9d2010-09-21 17:50:43 +000010171 OutChains[1] = DAG.getStore(Root, dl, FPtr, Addr,
10172 MachinePointerInfo(TrmpAddr, 2),
David Greene67c9d422010-02-15 16:53:33 +000010173 false, false, 2);
Duncan Sands339e14f2008-01-16 22:55:25 +000010174
10175 // Load the 'nest' parameter value into R10.
10176 // R10 is specified in X86CallingConv.td
10177 OpCode = ((MOV64ri | N86R10) << 8) | REX_WB; // movabsq r10
Owen Anderson825b72b2009-08-11 20:47:22 +000010178 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
10179 DAG.getConstant(10, MVT::i64));
10180 OutChains[2] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Chris Lattner8026a9d2010-09-21 17:50:43 +000010181 Addr, MachinePointerInfo(TrmpAddr, 10),
10182 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +000010183
Owen Anderson825b72b2009-08-11 20:47:22 +000010184 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
10185 DAG.getConstant(12, MVT::i64));
Chris Lattner8026a9d2010-09-21 17:50:43 +000010186 OutChains[3] = DAG.getStore(Root, dl, Nest, Addr,
10187 MachinePointerInfo(TrmpAddr, 12),
David Greene67c9d422010-02-15 16:53:33 +000010188 false, false, 2);
Duncan Sands339e14f2008-01-16 22:55:25 +000010189
10190 // Jump to the nested function.
10191 OpCode = (JMP64r << 8) | REX_WB; // jmpq *...
Owen Anderson825b72b2009-08-11 20:47:22 +000010192 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
10193 DAG.getConstant(20, MVT::i64));
10194 OutChains[4] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Chris Lattner8026a9d2010-09-21 17:50:43 +000010195 Addr, MachinePointerInfo(TrmpAddr, 20),
10196 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +000010197
10198 unsigned char ModRM = N86R11 | (4 << 3) | (3 << 6); // ...r11
Owen Anderson825b72b2009-08-11 20:47:22 +000010199 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
10200 DAG.getConstant(22, MVT::i64));
10201 OutChains[5] = DAG.getStore(Root, dl, DAG.getConstant(ModRM, MVT::i8), Addr,
Chris Lattner8026a9d2010-09-21 17:50:43 +000010202 MachinePointerInfo(TrmpAddr, 22),
10203 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +000010204
Duncan Sands4a544a72011-09-06 13:37:06 +000010205 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains, 6);
Duncan Sandsb116fac2007-07-27 20:02:49 +000010206 } else {
Dan Gohmanbbfb9c52008-01-31 01:01:48 +000010207 const Function *Func =
Duncan Sandsb116fac2007-07-27 20:02:49 +000010208 cast<Function>(cast<SrcValueSDNode>(Op.getOperand(5))->getValue());
Sandeep Patel65c3c8f2009-09-02 08:44:58 +000010209 CallingConv::ID CC = Func->getCallingConv();
Duncan Sandsee465742007-08-29 19:01:20 +000010210 unsigned NestReg;
Duncan Sandsb116fac2007-07-27 20:02:49 +000010211
10212 switch (CC) {
10213 default:
Torok Edwinc23197a2009-07-14 16:55:14 +000010214 llvm_unreachable("Unsupported calling convention");
Duncan Sandsb116fac2007-07-27 20:02:49 +000010215 case CallingConv::C:
Duncan Sandsb116fac2007-07-27 20:02:49 +000010216 case CallingConv::X86_StdCall: {
10217 // Pass 'nest' parameter in ECX.
10218 // Must be kept in sync with X86CallingConv.td
Duncan Sandsee465742007-08-29 19:01:20 +000010219 NestReg = X86::ECX;
Duncan Sandsb116fac2007-07-27 20:02:49 +000010220
10221 // Check that ECX wasn't needed by an 'inreg' parameter.
Chris Lattnerdb125cf2011-07-18 04:54:35 +000010222 FunctionType *FTy = Func->getFunctionType();
Devang Patel05988662008-09-25 21:00:45 +000010223 const AttrListPtr &Attrs = Func->getAttributes();
Duncan Sandsb116fac2007-07-27 20:02:49 +000010224
Chris Lattner58d74912008-03-12 17:45:29 +000010225 if (!Attrs.isEmpty() && !Func->isVarArg()) {
Duncan Sandsb116fac2007-07-27 20:02:49 +000010226 unsigned InRegCount = 0;
10227 unsigned Idx = 1;
10228
10229 for (FunctionType::param_iterator I = FTy->param_begin(),
10230 E = FTy->param_end(); I != E; ++I, ++Idx)
Devang Patel05988662008-09-25 21:00:45 +000010231 if (Attrs.paramHasAttr(Idx, Attribute::InReg))
Duncan Sandsb116fac2007-07-27 20:02:49 +000010232 // FIXME: should only count parameters that are lowered to integers.
Anton Korobeynikovbff66b02008-09-09 18:22:57 +000010233 InRegCount += (TD->getTypeSizeInBits(*I) + 31) / 32;
Duncan Sandsb116fac2007-07-27 20:02:49 +000010234
10235 if (InRegCount > 2) {
Eric Christopher90eb4022010-07-22 00:26:08 +000010236 report_fatal_error("Nest register in use - reduce number of inreg"
10237 " parameters!");
Duncan Sandsb116fac2007-07-27 20:02:49 +000010238 }
10239 }
10240 break;
10241 }
10242 case CallingConv::X86_FastCall:
Anton Korobeynikovded05e32010-05-16 09:08:45 +000010243 case CallingConv::X86_ThisCall:
Duncan Sandsbf53c292008-09-10 13:22:10 +000010244 case CallingConv::Fast:
Duncan Sandsb116fac2007-07-27 20:02:49 +000010245 // Pass 'nest' parameter in EAX.
10246 // Must be kept in sync with X86CallingConv.td
Duncan Sandsee465742007-08-29 19:01:20 +000010247 NestReg = X86::EAX;
Duncan Sandsb116fac2007-07-27 20:02:49 +000010248 break;
10249 }
10250
Dan Gohman475871a2008-07-27 21:46:04 +000010251 SDValue OutChains[4];
10252 SDValue Addr, Disp;
Duncan Sandsb116fac2007-07-27 20:02:49 +000010253
Owen Anderson825b72b2009-08-11 20:47:22 +000010254 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
10255 DAG.getConstant(10, MVT::i32));
10256 Disp = DAG.getNode(ISD::SUB, dl, MVT::i32, FPtr, Addr);
Duncan Sandsb116fac2007-07-27 20:02:49 +000010257
Chris Lattnera62fe662010-02-05 19:20:30 +000010258 // This is storing the opcode for MOV32ri.
10259 const unsigned char MOV32ri = 0xB8; // X86::MOV32ri's opcode byte.
Evan Cheng0e6a0522011-07-18 20:57:22 +000010260 const unsigned char N86Reg = X86_MC::getX86RegNum(NestReg);
Scott Michelfdc40a02009-02-17 22:15:04 +000010261 OutChains[0] = DAG.getStore(Root, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +000010262 DAG.getConstant(MOV32ri|N86Reg, MVT::i8),
Chris Lattner8026a9d2010-09-21 17:50:43 +000010263 Trmp, MachinePointerInfo(TrmpAddr),
10264 false, false, 0);
Duncan Sandsb116fac2007-07-27 20:02:49 +000010265
Owen Anderson825b72b2009-08-11 20:47:22 +000010266 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
10267 DAG.getConstant(1, MVT::i32));
Chris Lattner8026a9d2010-09-21 17:50:43 +000010268 OutChains[1] = DAG.getStore(Root, dl, Nest, Addr,
10269 MachinePointerInfo(TrmpAddr, 1),
David Greene67c9d422010-02-15 16:53:33 +000010270 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +000010271
Chris Lattnera62fe662010-02-05 19:20:30 +000010272 const unsigned char JMP = 0xE9; // jmp <32bit dst> opcode.
Owen Anderson825b72b2009-08-11 20:47:22 +000010273 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
10274 DAG.getConstant(5, MVT::i32));
10275 OutChains[2] = DAG.getStore(Root, dl, DAG.getConstant(JMP, MVT::i8), Addr,
Chris Lattner8026a9d2010-09-21 17:50:43 +000010276 MachinePointerInfo(TrmpAddr, 5),
10277 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +000010278
Owen Anderson825b72b2009-08-11 20:47:22 +000010279 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
10280 DAG.getConstant(6, MVT::i32));
Chris Lattner8026a9d2010-09-21 17:50:43 +000010281 OutChains[3] = DAG.getStore(Root, dl, Disp, Addr,
10282 MachinePointerInfo(TrmpAddr, 6),
David Greene67c9d422010-02-15 16:53:33 +000010283 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +000010284
Duncan Sands4a544a72011-09-06 13:37:06 +000010285 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains, 4);
Duncan Sandsb116fac2007-07-27 20:02:49 +000010286 }
10287}
10288
Dan Gohmand858e902010-04-17 15:26:15 +000010289SDValue X86TargetLowering::LowerFLT_ROUNDS_(SDValue Op,
10290 SelectionDAG &DAG) const {
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000010291 /*
10292 The rounding mode is in bits 11:10 of FPSR, and has the following
10293 settings:
10294 00 Round to nearest
10295 01 Round to -inf
10296 10 Round to +inf
10297 11 Round to 0
10298
10299 FLT_ROUNDS, on the other hand, expects the following:
10300 -1 Undefined
10301 0 Round to 0
10302 1 Round to nearest
10303 2 Round to +inf
10304 3 Round to -inf
10305
10306 To perform the conversion, we do:
10307 (((((FPSR & 0x800) >> 11) | ((FPSR & 0x400) >> 9)) + 1) & 3)
10308 */
10309
10310 MachineFunction &MF = DAG.getMachineFunction();
10311 const TargetMachine &TM = MF.getTarget();
Anton Korobeynikov16c29b52011-01-10 12:39:04 +000010312 const TargetFrameLowering &TFI = *TM.getFrameLowering();
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000010313 unsigned StackAlignment = TFI.getStackAlignment();
Owen Andersone50ed302009-08-10 22:56:29 +000010314 EVT VT = Op.getValueType();
Chris Lattner2156b792010-09-22 01:11:26 +000010315 DebugLoc DL = Op.getDebugLoc();
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000010316
10317 // Save FP Control Word to stack slot
David Greene3f2bf852009-11-12 20:49:22 +000010318 int SSFI = MF.getFrameInfo()->CreateStackObject(2, StackAlignment, false);
Dan Gohman475871a2008-07-27 21:46:04 +000010319 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000010320
Michael J. Spencerec38de22010-10-10 22:04:20 +000010321
Chris Lattner2156b792010-09-22 01:11:26 +000010322 MachineMemOperand *MMO =
10323 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
10324 MachineMemOperand::MOStore, 2, 2);
Michael J. Spencerec38de22010-10-10 22:04:20 +000010325
Chris Lattner2156b792010-09-22 01:11:26 +000010326 SDValue Ops[] = { DAG.getEntryNode(), StackSlot };
10327 SDValue Chain = DAG.getMemIntrinsicNode(X86ISD::FNSTCW16m, DL,
10328 DAG.getVTList(MVT::Other),
10329 Ops, 2, MVT::i16, MMO);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000010330
10331 // Load FP Control Word from stack slot
Chris Lattner2156b792010-09-22 01:11:26 +000010332 SDValue CWD = DAG.getLoad(MVT::i16, DL, Chain, StackSlot,
Pete Cooperd752e0f2011-11-08 18:42:53 +000010333 MachinePointerInfo(), false, false, false, 0);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000010334
10335 // Transform as necessary
Dan Gohman475871a2008-07-27 21:46:04 +000010336 SDValue CWD1 =
Chris Lattner2156b792010-09-22 01:11:26 +000010337 DAG.getNode(ISD::SRL, DL, MVT::i16,
10338 DAG.getNode(ISD::AND, DL, MVT::i16,
Owen Anderson825b72b2009-08-11 20:47:22 +000010339 CWD, DAG.getConstant(0x800, MVT::i16)),
10340 DAG.getConstant(11, MVT::i8));
Dan Gohman475871a2008-07-27 21:46:04 +000010341 SDValue CWD2 =
Chris Lattner2156b792010-09-22 01:11:26 +000010342 DAG.getNode(ISD::SRL, DL, MVT::i16,
10343 DAG.getNode(ISD::AND, DL, MVT::i16,
Owen Anderson825b72b2009-08-11 20:47:22 +000010344 CWD, DAG.getConstant(0x400, MVT::i16)),
10345 DAG.getConstant(9, MVT::i8));
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000010346
Dan Gohman475871a2008-07-27 21:46:04 +000010347 SDValue RetVal =
Chris Lattner2156b792010-09-22 01:11:26 +000010348 DAG.getNode(ISD::AND, DL, MVT::i16,
10349 DAG.getNode(ISD::ADD, DL, MVT::i16,
10350 DAG.getNode(ISD::OR, DL, MVT::i16, CWD1, CWD2),
Owen Anderson825b72b2009-08-11 20:47:22 +000010351 DAG.getConstant(1, MVT::i16)),
10352 DAG.getConstant(3, MVT::i16));
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000010353
10354
Duncan Sands83ec4b62008-06-06 12:08:01 +000010355 return DAG.getNode((VT.getSizeInBits() < 16 ?
Chris Lattner2156b792010-09-22 01:11:26 +000010356 ISD::TRUNCATE : ISD::ZERO_EXTEND), DL, VT, RetVal);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000010357}
10358
Dan Gohmand858e902010-04-17 15:26:15 +000010359SDValue X86TargetLowering::LowerCTLZ(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +000010360 EVT VT = Op.getValueType();
10361 EVT OpVT = VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +000010362 unsigned NumBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +000010363 DebugLoc dl = Op.getDebugLoc();
Evan Cheng18efe262007-12-14 02:13:44 +000010364
10365 Op = Op.getOperand(0);
Owen Anderson825b72b2009-08-11 20:47:22 +000010366 if (VT == MVT::i8) {
Evan Cheng152804e2007-12-14 08:30:15 +000010367 // Zero extend to i32 since there is not an i8 bsr.
Owen Anderson825b72b2009-08-11 20:47:22 +000010368 OpVT = MVT::i32;
Dale Johannesene4d209d2009-02-03 20:21:25 +000010369 Op = DAG.getNode(ISD::ZERO_EXTEND, dl, OpVT, Op);
Evan Cheng18efe262007-12-14 02:13:44 +000010370 }
Evan Cheng18efe262007-12-14 02:13:44 +000010371
Evan Cheng152804e2007-12-14 08:30:15 +000010372 // Issue a bsr (scan bits in reverse) which also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +000010373 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +000010374 Op = DAG.getNode(X86ISD::BSR, dl, VTs, Op);
Evan Cheng152804e2007-12-14 08:30:15 +000010375
10376 // If src is zero (i.e. bsr sets ZF), returns NumBits.
Benjamin Kramer7f1a5602009-12-29 16:57:26 +000010377 SDValue Ops[] = {
10378 Op,
10379 DAG.getConstant(NumBits+NumBits-1, OpVT),
10380 DAG.getConstant(X86::COND_E, MVT::i8),
10381 Op.getValue(1)
10382 };
10383 Op = DAG.getNode(X86ISD::CMOV, dl, OpVT, Ops, array_lengthof(Ops));
Evan Cheng152804e2007-12-14 08:30:15 +000010384
10385 // Finally xor with NumBits-1.
Dale Johannesene4d209d2009-02-03 20:21:25 +000010386 Op = DAG.getNode(ISD::XOR, dl, OpVT, Op, DAG.getConstant(NumBits-1, OpVT));
Evan Cheng152804e2007-12-14 08:30:15 +000010387
Owen Anderson825b72b2009-08-11 20:47:22 +000010388 if (VT == MVT::i8)
10389 Op = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Op);
Evan Cheng18efe262007-12-14 02:13:44 +000010390 return Op;
10391}
10392
Chandler Carruthacc068e2011-12-24 10:55:54 +000010393SDValue X86TargetLowering::LowerCTLZ_ZERO_UNDEF(SDValue Op,
10394 SelectionDAG &DAG) const {
10395 EVT VT = Op.getValueType();
10396 EVT OpVT = VT;
10397 unsigned NumBits = VT.getSizeInBits();
10398 DebugLoc dl = Op.getDebugLoc();
10399
10400 Op = Op.getOperand(0);
10401 if (VT == MVT::i8) {
10402 // Zero extend to i32 since there is not an i8 bsr.
10403 OpVT = MVT::i32;
10404 Op = DAG.getNode(ISD::ZERO_EXTEND, dl, OpVT, Op);
10405 }
10406
10407 // Issue a bsr (scan bits in reverse).
10408 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
10409 Op = DAG.getNode(X86ISD::BSR, dl, VTs, Op);
10410
10411 // And xor with NumBits-1.
10412 Op = DAG.getNode(ISD::XOR, dl, OpVT, Op, DAG.getConstant(NumBits-1, OpVT));
10413
10414 if (VT == MVT::i8)
10415 Op = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Op);
10416 return Op;
10417}
10418
Dan Gohmand858e902010-04-17 15:26:15 +000010419SDValue X86TargetLowering::LowerCTTZ(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +000010420 EVT VT = Op.getValueType();
Duncan Sands83ec4b62008-06-06 12:08:01 +000010421 unsigned NumBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +000010422 DebugLoc dl = Op.getDebugLoc();
Evan Cheng18efe262007-12-14 02:13:44 +000010423 Op = Op.getOperand(0);
Evan Cheng152804e2007-12-14 08:30:15 +000010424
10425 // Issue a bsf (scan bits forward) which also sets EFLAGS.
Chandler Carruth77821022011-12-24 12:12:34 +000010426 SDVTList VTs = DAG.getVTList(VT, MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +000010427 Op = DAG.getNode(X86ISD::BSF, dl, VTs, Op);
Evan Cheng152804e2007-12-14 08:30:15 +000010428
10429 // If src is zero (i.e. bsf sets ZF), returns NumBits.
Benjamin Kramer7f1a5602009-12-29 16:57:26 +000010430 SDValue Ops[] = {
10431 Op,
Chandler Carruth77821022011-12-24 12:12:34 +000010432 DAG.getConstant(NumBits, VT),
Benjamin Kramer7f1a5602009-12-29 16:57:26 +000010433 DAG.getConstant(X86::COND_E, MVT::i8),
10434 Op.getValue(1)
10435 };
Chandler Carruth77821022011-12-24 12:12:34 +000010436 return DAG.getNode(X86ISD::CMOV, dl, VT, Ops, array_lengthof(Ops));
Evan Cheng18efe262007-12-14 02:13:44 +000010437}
10438
Craig Topper13894fa2011-08-24 06:14:18 +000010439// Lower256IntArith - Break a 256-bit integer operation into two new 128-bit
10440// ones, and then concatenate the result back.
10441static SDValue Lower256IntArith(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +000010442 EVT VT = Op.getValueType();
Craig Topper13894fa2011-08-24 06:14:18 +000010443
Craig Topper7a9a28b2012-08-12 02:23:29 +000010444 assert(VT.is256BitVector() && VT.isInteger() &&
Craig Topper13894fa2011-08-24 06:14:18 +000010445 "Unsupported value type for operation");
10446
Craig Topper66ddd152012-04-27 22:54:43 +000010447 unsigned NumElems = VT.getVectorNumElements();
Craig Topper13894fa2011-08-24 06:14:18 +000010448 DebugLoc dl = Op.getDebugLoc();
Craig Topper13894fa2011-08-24 06:14:18 +000010449
10450 // Extract the LHS vectors
10451 SDValue LHS = Op.getOperand(0);
Craig Topperb14940a2012-04-22 20:55:18 +000010452 SDValue LHS1 = Extract128BitVector(LHS, 0, DAG, dl);
10453 SDValue LHS2 = Extract128BitVector(LHS, NumElems/2, DAG, dl);
Craig Topper13894fa2011-08-24 06:14:18 +000010454
10455 // Extract the RHS vectors
10456 SDValue RHS = Op.getOperand(1);
Craig Topperb14940a2012-04-22 20:55:18 +000010457 SDValue RHS1 = Extract128BitVector(RHS, 0, DAG, dl);
10458 SDValue RHS2 = Extract128BitVector(RHS, NumElems/2, DAG, dl);
Craig Topper13894fa2011-08-24 06:14:18 +000010459
10460 MVT EltVT = VT.getVectorElementType().getSimpleVT();
10461 EVT NewVT = MVT::getVectorVT(EltVT, NumElems/2);
10462
10463 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT,
10464 DAG.getNode(Op.getOpcode(), dl, NewVT, LHS1, RHS1),
10465 DAG.getNode(Op.getOpcode(), dl, NewVT, LHS2, RHS2));
10466}
10467
10468SDValue X86TargetLowering::LowerADD(SDValue Op, SelectionDAG &DAG) const {
Craig Topper7a9a28b2012-08-12 02:23:29 +000010469 assert(Op.getValueType().is256BitVector() &&
Craig Topper13894fa2011-08-24 06:14:18 +000010470 Op.getValueType().isInteger() &&
10471 "Only handle AVX 256-bit vector integer operation");
10472 return Lower256IntArith(Op, DAG);
10473}
10474
10475SDValue X86TargetLowering::LowerSUB(SDValue Op, SelectionDAG &DAG) const {
Craig Topper7a9a28b2012-08-12 02:23:29 +000010476 assert(Op.getValueType().is256BitVector() &&
Craig Topper13894fa2011-08-24 06:14:18 +000010477 Op.getValueType().isInteger() &&
10478 "Only handle AVX 256-bit vector integer operation");
10479 return Lower256IntArith(Op, DAG);
10480}
10481
10482SDValue X86TargetLowering::LowerMUL(SDValue Op, SelectionDAG &DAG) const {
10483 EVT VT = Op.getValueType();
10484
10485 // Decompose 256-bit ops into smaller 128-bit ops.
Craig Topper7a9a28b2012-08-12 02:23:29 +000010486 if (VT.is256BitVector() && !Subtarget->hasAVX2())
Craig Topper13894fa2011-08-24 06:14:18 +000010487 return Lower256IntArith(Op, DAG);
10488
Craig Topper5b209e82012-02-05 03:14:49 +000010489 assert((VT == MVT::v2i64 || VT == MVT::v4i64) &&
10490 "Only know how to lower V2I64/V4I64 multiply");
10491
Dale Johannesen6f38cb62009-02-07 19:59:05 +000010492 DebugLoc dl = Op.getDebugLoc();
Scott Michelfdc40a02009-02-17 22:15:04 +000010493
Craig Topper5b209e82012-02-05 03:14:49 +000010494 // Ahi = psrlqi(a, 32);
10495 // Bhi = psrlqi(b, 32);
10496 //
10497 // AloBlo = pmuludq(a, b);
10498 // AloBhi = pmuludq(a, Bhi);
10499 // AhiBlo = pmuludq(Ahi, b);
10500
10501 // AloBhi = psllqi(AloBhi, 32);
10502 // AhiBlo = psllqi(AhiBlo, 32);
10503 // return AloBlo + AloBhi + AhiBlo;
10504
Craig Topperaaa643c2011-11-09 07:28:55 +000010505 SDValue A = Op.getOperand(0);
10506 SDValue B = Op.getOperand(1);
10507
Craig Topper5b209e82012-02-05 03:14:49 +000010508 SDValue ShAmt = DAG.getConstant(32, MVT::i32);
Craig Topperaaa643c2011-11-09 07:28:55 +000010509
Craig Topper5b209e82012-02-05 03:14:49 +000010510 SDValue Ahi = DAG.getNode(X86ISD::VSRLI, dl, VT, A, ShAmt);
10511 SDValue Bhi = DAG.getNode(X86ISD::VSRLI, dl, VT, B, ShAmt);
Craig Topperaaa643c2011-11-09 07:28:55 +000010512
Craig Topper5b209e82012-02-05 03:14:49 +000010513 // Bit cast to 32-bit vectors for MULUDQ
10514 EVT MulVT = (VT == MVT::v2i64) ? MVT::v4i32 : MVT::v8i32;
10515 A = DAG.getNode(ISD::BITCAST, dl, MulVT, A);
10516 B = DAG.getNode(ISD::BITCAST, dl, MulVT, B);
10517 Ahi = DAG.getNode(ISD::BITCAST, dl, MulVT, Ahi);
10518 Bhi = DAG.getNode(ISD::BITCAST, dl, MulVT, Bhi);
Craig Topperaaa643c2011-11-09 07:28:55 +000010519
Craig Topper5b209e82012-02-05 03:14:49 +000010520 SDValue AloBlo = DAG.getNode(X86ISD::PMULUDQ, dl, VT, A, B);
10521 SDValue AloBhi = DAG.getNode(X86ISD::PMULUDQ, dl, VT, A, Bhi);
10522 SDValue AhiBlo = DAG.getNode(X86ISD::PMULUDQ, dl, VT, Ahi, B);
Craig Topperaaa643c2011-11-09 07:28:55 +000010523
Craig Topper5b209e82012-02-05 03:14:49 +000010524 AloBhi = DAG.getNode(X86ISD::VSHLI, dl, VT, AloBhi, ShAmt);
10525 AhiBlo = DAG.getNode(X86ISD::VSHLI, dl, VT, AhiBlo, ShAmt);
Mon P Wangaf9b9522008-12-18 21:42:19 +000010526
Dale Johannesene4d209d2009-02-03 20:21:25 +000010527 SDValue Res = DAG.getNode(ISD::ADD, dl, VT, AloBlo, AloBhi);
Craig Topper5b209e82012-02-05 03:14:49 +000010528 return DAG.getNode(ISD::ADD, dl, VT, Res, AhiBlo);
Mon P Wangaf9b9522008-12-18 21:42:19 +000010529}
10530
Nadav Rotem43012222011-05-11 08:12:09 +000010531SDValue X86TargetLowering::LowerShift(SDValue Op, SelectionDAG &DAG) const {
10532
Nate Begemanbdcb5af2010-07-27 22:37:06 +000010533 EVT VT = Op.getValueType();
10534 DebugLoc dl = Op.getDebugLoc();
10535 SDValue R = Op.getOperand(0);
Nadav Rotem43012222011-05-11 08:12:09 +000010536 SDValue Amt = Op.getOperand(1);
Nate Begemanbdcb5af2010-07-27 22:37:06 +000010537 LLVMContext *Context = DAG.getContext();
Nate Begemanbdcb5af2010-07-27 22:37:06 +000010538
Craig Topper1accb7e2012-01-10 06:54:16 +000010539 if (!Subtarget->hasSSE2())
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +000010540 return SDValue();
10541
Nadav Rotem43012222011-05-11 08:12:09 +000010542 // Optimize shl/srl/sra with constant shift amount.
10543 if (isSplatVector(Amt.getNode())) {
10544 SDValue SclrAmt = Amt->getOperand(0);
10545 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(SclrAmt)) {
10546 uint64_t ShiftAmt = C->getZExtValue();
10547
Craig Toppered2e13d2012-01-22 19:15:14 +000010548 if (VT == MVT::v2i64 || VT == MVT::v4i32 || VT == MVT::v8i16 ||
10549 (Subtarget->hasAVX2() &&
10550 (VT == MVT::v4i64 || VT == MVT::v8i32 || VT == MVT::v16i16))) {
10551 if (Op.getOpcode() == ISD::SHL)
10552 return DAG.getNode(X86ISD::VSHLI, dl, VT, R,
10553 DAG.getConstant(ShiftAmt, MVT::i32));
10554 if (Op.getOpcode() == ISD::SRL)
10555 return DAG.getNode(X86ISD::VSRLI, dl, VT, R,
10556 DAG.getConstant(ShiftAmt, MVT::i32));
10557 if (Op.getOpcode() == ISD::SRA && VT != MVT::v2i64 && VT != MVT::v4i64)
10558 return DAG.getNode(X86ISD::VSRAI, dl, VT, R,
10559 DAG.getConstant(ShiftAmt, MVT::i32));
Benjamin Kramerdade3c12011-10-30 17:31:21 +000010560 }
10561
Craig Toppered2e13d2012-01-22 19:15:14 +000010562 if (VT == MVT::v16i8) {
10563 if (Op.getOpcode() == ISD::SHL) {
10564 // Make a large shift.
10565 SDValue SHL = DAG.getNode(X86ISD::VSHLI, dl, MVT::v8i16, R,
10566 DAG.getConstant(ShiftAmt, MVT::i32));
10567 SHL = DAG.getNode(ISD::BITCAST, dl, VT, SHL);
10568 // Zero out the rightmost bits.
10569 SmallVector<SDValue, 16> V(16,
10570 DAG.getConstant(uint8_t(-1U << ShiftAmt),
10571 MVT::i8));
10572 return DAG.getNode(ISD::AND, dl, VT, SHL,
10573 DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &V[0], 16));
Eli Friedmanf6aa6b12011-11-01 21:18:39 +000010574 }
Craig Toppered2e13d2012-01-22 19:15:14 +000010575 if (Op.getOpcode() == ISD::SRL) {
10576 // Make a large shift.
10577 SDValue SRL = DAG.getNode(X86ISD::VSRLI, dl, MVT::v8i16, R,
10578 DAG.getConstant(ShiftAmt, MVT::i32));
10579 SRL = DAG.getNode(ISD::BITCAST, dl, VT, SRL);
10580 // Zero out the leftmost bits.
10581 SmallVector<SDValue, 16> V(16,
10582 DAG.getConstant(uint8_t(-1U) >> ShiftAmt,
10583 MVT::i8));
10584 return DAG.getNode(ISD::AND, dl, VT, SRL,
10585 DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &V[0], 16));
10586 }
10587 if (Op.getOpcode() == ISD::SRA) {
10588 if (ShiftAmt == 7) {
10589 // R s>> 7 === R s< 0
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +000010590 SDValue Zeros = getZeroVector(VT, Subtarget, DAG, dl);
Craig Topper67609fd2012-01-22 22:42:16 +000010591 return DAG.getNode(X86ISD::PCMPGT, dl, VT, Zeros, R);
Craig Toppered2e13d2012-01-22 19:15:14 +000010592 }
Eli Friedmanf6aa6b12011-11-01 21:18:39 +000010593
Craig Toppered2e13d2012-01-22 19:15:14 +000010594 // R s>> a === ((R u>> a) ^ m) - m
10595 SDValue Res = DAG.getNode(ISD::SRL, dl, VT, R, Amt);
10596 SmallVector<SDValue, 16> V(16, DAG.getConstant(128 >> ShiftAmt,
10597 MVT::i8));
10598 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &V[0], 16);
10599 Res = DAG.getNode(ISD::XOR, dl, VT, Res, Mask);
10600 Res = DAG.getNode(ISD::SUB, dl, VT, Res, Mask);
10601 return Res;
10602 }
Craig Topper731dfd02012-04-23 03:42:40 +000010603 llvm_unreachable("Unknown shift opcode.");
Eli Friedmanf6aa6b12011-11-01 21:18:39 +000010604 }
Craig Topper46154eb2011-11-11 07:39:23 +000010605
Craig Topper0d86d462011-11-20 00:12:05 +000010606 if (Subtarget->hasAVX2() && VT == MVT::v32i8) {
10607 if (Op.getOpcode() == ISD::SHL) {
10608 // Make a large shift.
Craig Toppered2e13d2012-01-22 19:15:14 +000010609 SDValue SHL = DAG.getNode(X86ISD::VSHLI, dl, MVT::v16i16, R,
10610 DAG.getConstant(ShiftAmt, MVT::i32));
10611 SHL = DAG.getNode(ISD::BITCAST, dl, VT, SHL);
Craig Topper0d86d462011-11-20 00:12:05 +000010612 // Zero out the rightmost bits.
Craig Toppered2e13d2012-01-22 19:15:14 +000010613 SmallVector<SDValue, 32> V(32,
10614 DAG.getConstant(uint8_t(-1U << ShiftAmt),
10615 MVT::i8));
Craig Topper0d86d462011-11-20 00:12:05 +000010616 return DAG.getNode(ISD::AND, dl, VT, SHL,
10617 DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &V[0], 32));
Craig Topper46154eb2011-11-11 07:39:23 +000010618 }
Craig Topper0d86d462011-11-20 00:12:05 +000010619 if (Op.getOpcode() == ISD::SRL) {
10620 // Make a large shift.
Craig Toppered2e13d2012-01-22 19:15:14 +000010621 SDValue SRL = DAG.getNode(X86ISD::VSRLI, dl, MVT::v16i16, R,
10622 DAG.getConstant(ShiftAmt, MVT::i32));
10623 SRL = DAG.getNode(ISD::BITCAST, dl, VT, SRL);
Craig Topper0d86d462011-11-20 00:12:05 +000010624 // Zero out the leftmost bits.
Craig Toppered2e13d2012-01-22 19:15:14 +000010625 SmallVector<SDValue, 32> V(32,
10626 DAG.getConstant(uint8_t(-1U) >> ShiftAmt,
10627 MVT::i8));
Craig Topper0d86d462011-11-20 00:12:05 +000010628 return DAG.getNode(ISD::AND, dl, VT, SRL,
10629 DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &V[0], 32));
10630 }
10631 if (Op.getOpcode() == ISD::SRA) {
10632 if (ShiftAmt == 7) {
10633 // R s>> 7 === R s< 0
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +000010634 SDValue Zeros = getZeroVector(VT, Subtarget, DAG, dl);
Craig Topper67609fd2012-01-22 22:42:16 +000010635 return DAG.getNode(X86ISD::PCMPGT, dl, VT, Zeros, R);
Craig Topper0d86d462011-11-20 00:12:05 +000010636 }
10637
10638 // R s>> a === ((R u>> a) ^ m) - m
10639 SDValue Res = DAG.getNode(ISD::SRL, dl, VT, R, Amt);
10640 SmallVector<SDValue, 32> V(32, DAG.getConstant(128 >> ShiftAmt,
10641 MVT::i8));
10642 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &V[0], 32);
10643 Res = DAG.getNode(ISD::XOR, dl, VT, Res, Mask);
10644 Res = DAG.getNode(ISD::SUB, dl, VT, Res, Mask);
10645 return Res;
10646 }
Craig Topper731dfd02012-04-23 03:42:40 +000010647 llvm_unreachable("Unknown shift opcode.");
Craig Topper0d86d462011-11-20 00:12:05 +000010648 }
Nadav Rotem43012222011-05-11 08:12:09 +000010649 }
10650 }
10651
10652 // Lower SHL with variable shift amount.
Nadav Rotem43012222011-05-11 08:12:09 +000010653 if (VT == MVT::v4i32 && Op->getOpcode() == ISD::SHL) {
Craig Toppered2e13d2012-01-22 19:15:14 +000010654 Op = DAG.getNode(X86ISD::VSHLI, dl, VT, Op.getOperand(1),
10655 DAG.getConstant(23, MVT::i32));
Nate Begeman51409212010-07-28 00:21:48 +000010656
Chris Lattner7302d802012-02-06 21:56:39 +000010657 const uint32_t CV[] = { 0x3f800000U, 0x3f800000U, 0x3f800000U, 0x3f800000U};
10658 Constant *C = ConstantDataVector::get(*Context, CV);
Nate Begeman51409212010-07-28 00:21:48 +000010659 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
10660 SDValue Addend = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +000010661 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000010662 false, false, false, 16);
Nate Begeman51409212010-07-28 00:21:48 +000010663
10664 Op = DAG.getNode(ISD::ADD, dl, VT, Op, Addend);
Wesley Peckbf17cfa2010-11-23 03:31:01 +000010665 Op = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, Op);
Nate Begeman51409212010-07-28 00:21:48 +000010666 Op = DAG.getNode(ISD::FP_TO_SINT, dl, VT, Op);
10667 return DAG.getNode(ISD::MUL, dl, VT, Op, R);
10668 }
Nadav Rotem43012222011-05-11 08:12:09 +000010669 if (VT == MVT::v16i8 && Op->getOpcode() == ISD::SHL) {
Craig Topper8b5a6b62012-01-17 08:23:44 +000010670 assert(Subtarget->hasSSE2() && "Need SSE2 for pslli/pcmpeq.");
Lang Hames8b99c1e2011-12-17 01:08:46 +000010671
Nate Begeman51409212010-07-28 00:21:48 +000010672 // a = a << 5;
Craig Toppered2e13d2012-01-22 19:15:14 +000010673 Op = DAG.getNode(X86ISD::VSHLI, dl, MVT::v8i16, Op.getOperand(1),
10674 DAG.getConstant(5, MVT::i32));
10675 Op = DAG.getNode(ISD::BITCAST, dl, VT, Op);
Nate Begeman51409212010-07-28 00:21:48 +000010676
Lang Hames8b99c1e2011-12-17 01:08:46 +000010677 // Turn 'a' into a mask suitable for VSELECT
10678 SDValue VSelM = DAG.getConstant(0x80, VT);
10679 SDValue OpVSel = DAG.getNode(ISD::AND, dl, VT, VSelM, Op);
Craig Topper67609fd2012-01-22 22:42:16 +000010680 OpVSel = DAG.getNode(X86ISD::PCMPEQ, dl, VT, OpVSel, VSelM);
Nate Begeman51409212010-07-28 00:21:48 +000010681
Lang Hames8b99c1e2011-12-17 01:08:46 +000010682 SDValue CM1 = DAG.getConstant(0x0f, VT);
10683 SDValue CM2 = DAG.getConstant(0x3f, VT);
Nate Begeman51409212010-07-28 00:21:48 +000010684
Lang Hames8b99c1e2011-12-17 01:08:46 +000010685 // r = VSELECT(r, psllw(r & (char16)15, 4), a);
10686 SDValue M = DAG.getNode(ISD::AND, dl, VT, R, CM1);
Craig Toppered2e13d2012-01-22 19:15:14 +000010687 M = getTargetVShiftNode(X86ISD::VSHLI, dl, MVT::v8i16, M,
10688 DAG.getConstant(4, MVT::i32), DAG);
10689 M = DAG.getNode(ISD::BITCAST, dl, VT, M);
Lang Hames8b99c1e2011-12-17 01:08:46 +000010690 R = DAG.getNode(ISD::VSELECT, dl, VT, OpVSel, M, R);
10691
Nate Begeman51409212010-07-28 00:21:48 +000010692 // a += a
10693 Op = DAG.getNode(ISD::ADD, dl, VT, Op, Op);
Lang Hames8b99c1e2011-12-17 01:08:46 +000010694 OpVSel = DAG.getNode(ISD::AND, dl, VT, VSelM, Op);
Craig Topper67609fd2012-01-22 22:42:16 +000010695 OpVSel = DAG.getNode(X86ISD::PCMPEQ, dl, VT, OpVSel, VSelM);
Michael J. Spencerec38de22010-10-10 22:04:20 +000010696
Lang Hames8b99c1e2011-12-17 01:08:46 +000010697 // r = VSELECT(r, psllw(r & (char16)63, 2), a);
10698 M = DAG.getNode(ISD::AND, dl, VT, R, CM2);
Craig Toppered2e13d2012-01-22 19:15:14 +000010699 M = getTargetVShiftNode(X86ISD::VSHLI, dl, MVT::v8i16, M,
10700 DAG.getConstant(2, MVT::i32), DAG);
10701 M = DAG.getNode(ISD::BITCAST, dl, VT, M);
Lang Hames8b99c1e2011-12-17 01:08:46 +000010702 R = DAG.getNode(ISD::VSELECT, dl, VT, OpVSel, M, R);
10703
Nate Begeman51409212010-07-28 00:21:48 +000010704 // a += a
10705 Op = DAG.getNode(ISD::ADD, dl, VT, Op, Op);
Lang Hames8b99c1e2011-12-17 01:08:46 +000010706 OpVSel = DAG.getNode(ISD::AND, dl, VT, VSelM, Op);
Craig Topper67609fd2012-01-22 22:42:16 +000010707 OpVSel = DAG.getNode(X86ISD::PCMPEQ, dl, VT, OpVSel, VSelM);
Michael J. Spencerec38de22010-10-10 22:04:20 +000010708
Lang Hames8b99c1e2011-12-17 01:08:46 +000010709 // return VSELECT(r, r+r, a);
10710 R = DAG.getNode(ISD::VSELECT, dl, VT, OpVSel,
Lang Hamesa0a25132011-12-15 18:57:27 +000010711 DAG.getNode(ISD::ADD, dl, VT, R, R), R);
Nate Begeman51409212010-07-28 00:21:48 +000010712 return R;
10713 }
Craig Topper46154eb2011-11-11 07:39:23 +000010714
10715 // Decompose 256-bit shifts into smaller 128-bit shifts.
Craig Topper7a9a28b2012-08-12 02:23:29 +000010716 if (VT.is256BitVector()) {
Craig Toppered2e13d2012-01-22 19:15:14 +000010717 unsigned NumElems = VT.getVectorNumElements();
Craig Topper46154eb2011-11-11 07:39:23 +000010718 MVT EltVT = VT.getVectorElementType().getSimpleVT();
10719 EVT NewVT = MVT::getVectorVT(EltVT, NumElems/2);
10720
10721 // Extract the two vectors
Craig Topperb14940a2012-04-22 20:55:18 +000010722 SDValue V1 = Extract128BitVector(R, 0, DAG, dl);
10723 SDValue V2 = Extract128BitVector(R, NumElems/2, DAG, dl);
Craig Topper46154eb2011-11-11 07:39:23 +000010724
10725 // Recreate the shift amount vectors
10726 SDValue Amt1, Amt2;
10727 if (Amt.getOpcode() == ISD::BUILD_VECTOR) {
10728 // Constant shift amount
10729 SmallVector<SDValue, 4> Amt1Csts;
10730 SmallVector<SDValue, 4> Amt2Csts;
Craig Toppered2e13d2012-01-22 19:15:14 +000010731 for (unsigned i = 0; i != NumElems/2; ++i)
Craig Topper46154eb2011-11-11 07:39:23 +000010732 Amt1Csts.push_back(Amt->getOperand(i));
Craig Toppered2e13d2012-01-22 19:15:14 +000010733 for (unsigned i = NumElems/2; i != NumElems; ++i)
Craig Topper46154eb2011-11-11 07:39:23 +000010734 Amt2Csts.push_back(Amt->getOperand(i));
10735
10736 Amt1 = DAG.getNode(ISD::BUILD_VECTOR, dl, NewVT,
10737 &Amt1Csts[0], NumElems/2);
10738 Amt2 = DAG.getNode(ISD::BUILD_VECTOR, dl, NewVT,
10739 &Amt2Csts[0], NumElems/2);
10740 } else {
10741 // Variable shift amount
Craig Topperb14940a2012-04-22 20:55:18 +000010742 Amt1 = Extract128BitVector(Amt, 0, DAG, dl);
10743 Amt2 = Extract128BitVector(Amt, NumElems/2, DAG, dl);
Craig Topper46154eb2011-11-11 07:39:23 +000010744 }
10745
10746 // Issue new vector shifts for the smaller types
10747 V1 = DAG.getNode(Op.getOpcode(), dl, NewVT, V1, Amt1);
10748 V2 = DAG.getNode(Op.getOpcode(), dl, NewVT, V2, Amt2);
10749
10750 // Concatenate the result back
10751 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT, V1, V2);
10752 }
10753
Nate Begeman51409212010-07-28 00:21:48 +000010754 return SDValue();
Nate Begemanbdcb5af2010-07-27 22:37:06 +000010755}
Mon P Wangaf9b9522008-12-18 21:42:19 +000010756
Dan Gohmand858e902010-04-17 15:26:15 +000010757SDValue X86TargetLowering::LowerXALUO(SDValue Op, SelectionDAG &DAG) const {
Bill Wendling74c37652008-12-09 22:08:41 +000010758 // Lower the "add/sub/mul with overflow" instruction into a regular ins plus
10759 // a "setcc" instruction that checks the overflow flag. The "brcond" lowering
Bill Wendling61edeb52008-12-02 01:06:39 +000010760 // looks for this combo and may remove the "setcc" instruction if the "setcc"
10761 // has only one use.
Bill Wendling3fafd932008-11-26 22:37:40 +000010762 SDNode *N = Op.getNode();
Bill Wendling61edeb52008-12-02 01:06:39 +000010763 SDValue LHS = N->getOperand(0);
10764 SDValue RHS = N->getOperand(1);
Bill Wendling74c37652008-12-09 22:08:41 +000010765 unsigned BaseOp = 0;
10766 unsigned Cond = 0;
Chris Lattnerb20e0b12010-12-05 07:30:36 +000010767 DebugLoc DL = Op.getDebugLoc();
Bill Wendling74c37652008-12-09 22:08:41 +000010768 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +000010769 default: llvm_unreachable("Unknown ovf instruction!");
Bill Wendling74c37652008-12-09 22:08:41 +000010770 case ISD::SADDO:
Dan Gohman076aee32009-03-04 19:44:21 +000010771 // A subtract of one will be selected as a INC. Note that INC doesn't
10772 // set CF, so we can't do this for UADDO.
Benjamin Kramerc175a4b2011-03-08 15:20:20 +000010773 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(RHS))
10774 if (C->isOne()) {
Dan Gohman076aee32009-03-04 19:44:21 +000010775 BaseOp = X86ISD::INC;
10776 Cond = X86::COND_O;
10777 break;
10778 }
Bill Wendlingab55ebd2008-12-12 00:56:36 +000010779 BaseOp = X86ISD::ADD;
Bill Wendling74c37652008-12-09 22:08:41 +000010780 Cond = X86::COND_O;
10781 break;
10782 case ISD::UADDO:
Bill Wendlingab55ebd2008-12-12 00:56:36 +000010783 BaseOp = X86ISD::ADD;
Dan Gohman653456c2009-01-07 00:15:08 +000010784 Cond = X86::COND_B;
Bill Wendling74c37652008-12-09 22:08:41 +000010785 break;
10786 case ISD::SSUBO:
Dan Gohman076aee32009-03-04 19:44:21 +000010787 // A subtract of one will be selected as a DEC. Note that DEC doesn't
10788 // set CF, so we can't do this for USUBO.
Benjamin Kramerc175a4b2011-03-08 15:20:20 +000010789 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(RHS))
10790 if (C->isOne()) {
Dan Gohman076aee32009-03-04 19:44:21 +000010791 BaseOp = X86ISD::DEC;
10792 Cond = X86::COND_O;
10793 break;
10794 }
Bill Wendlingab55ebd2008-12-12 00:56:36 +000010795 BaseOp = X86ISD::SUB;
Bill Wendling74c37652008-12-09 22:08:41 +000010796 Cond = X86::COND_O;
10797 break;
10798 case ISD::USUBO:
Bill Wendlingab55ebd2008-12-12 00:56:36 +000010799 BaseOp = X86ISD::SUB;
Dan Gohman653456c2009-01-07 00:15:08 +000010800 Cond = X86::COND_B;
Bill Wendling74c37652008-12-09 22:08:41 +000010801 break;
10802 case ISD::SMULO:
Bill Wendlingd350e022008-12-12 21:15:41 +000010803 BaseOp = X86ISD::SMUL;
Bill Wendling74c37652008-12-09 22:08:41 +000010804 Cond = X86::COND_O;
10805 break;
Chris Lattnerb20e0b12010-12-05 07:30:36 +000010806 case ISD::UMULO: { // i64, i8 = umulo lhs, rhs --> i64, i64, i32 umul lhs,rhs
10807 SDVTList VTs = DAG.getVTList(N->getValueType(0), N->getValueType(0),
10808 MVT::i32);
10809 SDValue Sum = DAG.getNode(X86ISD::UMUL, DL, VTs, LHS, RHS);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000010810
Chris Lattnerb20e0b12010-12-05 07:30:36 +000010811 SDValue SetCC =
10812 DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
10813 DAG.getConstant(X86::COND_O, MVT::i32),
10814 SDValue(Sum.getNode(), 2));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000010815
Dan Gohman6e5fda22011-07-22 18:45:15 +000010816 return DAG.getNode(ISD::MERGE_VALUES, DL, N->getVTList(), Sum, SetCC);
Chris Lattnerb20e0b12010-12-05 07:30:36 +000010817 }
Bill Wendling74c37652008-12-09 22:08:41 +000010818 }
Bill Wendling3fafd932008-11-26 22:37:40 +000010819
Bill Wendling61edeb52008-12-02 01:06:39 +000010820 // Also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +000010821 SDVTList VTs = DAG.getVTList(N->getValueType(0), MVT::i32);
Chris Lattnerb20e0b12010-12-05 07:30:36 +000010822 SDValue Sum = DAG.getNode(BaseOp, DL, VTs, LHS, RHS);
Bill Wendling3fafd932008-11-26 22:37:40 +000010823
Bill Wendling61edeb52008-12-02 01:06:39 +000010824 SDValue SetCC =
Chris Lattnerb20e0b12010-12-05 07:30:36 +000010825 DAG.getNode(X86ISD::SETCC, DL, N->getValueType(1),
10826 DAG.getConstant(Cond, MVT::i32),
10827 SDValue(Sum.getNode(), 1));
Bill Wendling3fafd932008-11-26 22:37:40 +000010828
Dan Gohman6e5fda22011-07-22 18:45:15 +000010829 return DAG.getNode(ISD::MERGE_VALUES, DL, N->getVTList(), Sum, SetCC);
Bill Wendling41ea7e72008-11-24 19:21:46 +000010830}
10831
Chad Rosier30450e82011-12-22 22:35:21 +000010832SDValue X86TargetLowering::LowerSIGN_EXTEND_INREG(SDValue Op,
10833 SelectionDAG &DAG) const {
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000010834 DebugLoc dl = Op.getDebugLoc();
Craig Toppera124f942011-11-21 01:12:36 +000010835 EVT ExtraVT = cast<VTSDNode>(Op.getOperand(1))->getVT();
10836 EVT VT = Op.getValueType();
10837
Craig Toppered2e13d2012-01-22 19:15:14 +000010838 if (!Subtarget->hasSSE2() || !VT.isVector())
10839 return SDValue();
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000010840
Craig Toppered2e13d2012-01-22 19:15:14 +000010841 unsigned BitsDiff = VT.getScalarType().getSizeInBits() -
10842 ExtraVT.getScalarType().getSizeInBits();
10843 SDValue ShAmt = DAG.getConstant(BitsDiff, MVT::i32);
10844
10845 switch (VT.getSimpleVT().SimpleTy) {
10846 default: return SDValue();
10847 case MVT::v8i32:
10848 case MVT::v16i16:
10849 if (!Subtarget->hasAVX())
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000010850 return SDValue();
Craig Toppered2e13d2012-01-22 19:15:14 +000010851 if (!Subtarget->hasAVX2()) {
10852 // needs to be split
Craig Topper66ddd152012-04-27 22:54:43 +000010853 unsigned NumElems = VT.getVectorNumElements();
Craig Toppera124f942011-11-21 01:12:36 +000010854
Craig Toppered2e13d2012-01-22 19:15:14 +000010855 // Extract the LHS vectors
10856 SDValue LHS = Op.getOperand(0);
Craig Topperb14940a2012-04-22 20:55:18 +000010857 SDValue LHS1 = Extract128BitVector(LHS, 0, DAG, dl);
10858 SDValue LHS2 = Extract128BitVector(LHS, NumElems/2, DAG, dl);
Craig Toppera124f942011-11-21 01:12:36 +000010859
Craig Toppered2e13d2012-01-22 19:15:14 +000010860 MVT EltVT = VT.getVectorElementType().getSimpleVT();
10861 EVT NewVT = MVT::getVectorVT(EltVT, NumElems/2);
Craig Toppera124f942011-11-21 01:12:36 +000010862
Craig Toppered2e13d2012-01-22 19:15:14 +000010863 EVT ExtraEltVT = ExtraVT.getVectorElementType();
Craig Topperb6072642012-05-03 07:26:59 +000010864 unsigned ExtraNumElems = ExtraVT.getVectorNumElements();
Craig Toppered2e13d2012-01-22 19:15:14 +000010865 ExtraVT = EVT::getVectorVT(*DAG.getContext(), ExtraEltVT,
10866 ExtraNumElems/2);
10867 SDValue Extra = DAG.getValueType(ExtraVT);
Craig Toppera124f942011-11-21 01:12:36 +000010868
Craig Toppered2e13d2012-01-22 19:15:14 +000010869 LHS1 = DAG.getNode(Op.getOpcode(), dl, NewVT, LHS1, Extra);
10870 LHS2 = DAG.getNode(Op.getOpcode(), dl, NewVT, LHS2, Extra);
Craig Toppera124f942011-11-21 01:12:36 +000010871
Craig Toppered2e13d2012-01-22 19:15:14 +000010872 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT, LHS1, LHS2);;
10873 }
10874 // fall through
10875 case MVT::v4i32:
10876 case MVT::v8i16: {
10877 SDValue Tmp1 = getTargetVShiftNode(X86ISD::VSHLI, dl, VT,
10878 Op.getOperand(0), ShAmt, DAG);
10879 return getTargetVShiftNode(X86ISD::VSRAI, dl, VT, Tmp1, ShAmt, DAG);
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000010880 }
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000010881 }
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000010882}
10883
10884
Eric Christopher9a9d2752010-07-22 02:48:34 +000010885SDValue X86TargetLowering::LowerMEMBARRIER(SDValue Op, SelectionDAG &DAG) const{
10886 DebugLoc dl = Op.getDebugLoc();
Michael J. Spencerec38de22010-10-10 22:04:20 +000010887
Eric Christopher77ed1352011-07-08 00:04:56 +000010888 // Go ahead and emit the fence on x86-64 even if we asked for no-sse2.
10889 // There isn't any reason to disable it if the target processor supports it.
Craig Topper1accb7e2012-01-10 06:54:16 +000010890 if (!Subtarget->hasSSE2() && !Subtarget->is64Bit()) {
Eric Christopherc0b2a202010-08-14 21:51:50 +000010891 SDValue Chain = Op.getOperand(0);
Eric Christopher77ed1352011-07-08 00:04:56 +000010892 SDValue Zero = DAG.getConstant(0, MVT::i32);
Eric Christopherc0b2a202010-08-14 21:51:50 +000010893 SDValue Ops[] = {
10894 DAG.getRegister(X86::ESP, MVT::i32), // Base
10895 DAG.getTargetConstant(1, MVT::i8), // Scale
10896 DAG.getRegister(0, MVT::i32), // Index
10897 DAG.getTargetConstant(0, MVT::i32), // Disp
10898 DAG.getRegister(0, MVT::i32), // Segment.
10899 Zero,
10900 Chain
10901 };
Michael J. Spencerec38de22010-10-10 22:04:20 +000010902 SDNode *Res =
Eric Christopherc0b2a202010-08-14 21:51:50 +000010903 DAG.getMachineNode(X86::OR32mrLocked, dl, MVT::Other, Ops,
10904 array_lengthof(Ops));
10905 return SDValue(Res, 0);
Eric Christopherb6729dc2010-08-04 23:03:04 +000010906 }
Michael J. Spencerec38de22010-10-10 22:04:20 +000010907
Eric Christopher9a9d2752010-07-22 02:48:34 +000010908 unsigned isDev = cast<ConstantSDNode>(Op.getOperand(5))->getZExtValue();
Chris Lattner132929a2010-08-14 17:26:09 +000010909 if (!isDev)
Eric Christopher9a9d2752010-07-22 02:48:34 +000010910 return DAG.getNode(X86ISD::MEMBARRIER, dl, MVT::Other, Op.getOperand(0));
Michael J. Spencerec38de22010-10-10 22:04:20 +000010911
Chris Lattner132929a2010-08-14 17:26:09 +000010912 unsigned Op1 = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
10913 unsigned Op2 = cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue();
10914 unsigned Op3 = cast<ConstantSDNode>(Op.getOperand(3))->getZExtValue();
10915 unsigned Op4 = cast<ConstantSDNode>(Op.getOperand(4))->getZExtValue();
Michael J. Spencerec38de22010-10-10 22:04:20 +000010916
Chris Lattner132929a2010-08-14 17:26:09 +000010917 // def : Pat<(membarrier (i8 0), (i8 0), (i8 0), (i8 1), (i8 1)), (SFENCE)>;
10918 if (!Op1 && !Op2 && !Op3 && Op4)
10919 return DAG.getNode(X86ISD::SFENCE, dl, MVT::Other, Op.getOperand(0));
Michael J. Spencerec38de22010-10-10 22:04:20 +000010920
Chris Lattner132929a2010-08-14 17:26:09 +000010921 // def : Pat<(membarrier (i8 1), (i8 0), (i8 0), (i8 0), (i8 1)), (LFENCE)>;
10922 if (Op1 && !Op2 && !Op3 && !Op4)
10923 return DAG.getNode(X86ISD::LFENCE, dl, MVT::Other, Op.getOperand(0));
Michael J. Spencerec38de22010-10-10 22:04:20 +000010924
10925 // def : Pat<(membarrier (i8 imm), (i8 imm), (i8 imm), (i8 imm), (i8 1)),
Chris Lattner132929a2010-08-14 17:26:09 +000010926 // (MFENCE)>;
10927 return DAG.getNode(X86ISD::MFENCE, dl, MVT::Other, Op.getOperand(0));
Eric Christopher9a9d2752010-07-22 02:48:34 +000010928}
10929
Eli Friedman14648462011-07-27 22:21:52 +000010930SDValue X86TargetLowering::LowerATOMIC_FENCE(SDValue Op,
10931 SelectionDAG &DAG) const {
10932 DebugLoc dl = Op.getDebugLoc();
10933 AtomicOrdering FenceOrdering = static_cast<AtomicOrdering>(
10934 cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue());
10935 SynchronizationScope FenceScope = static_cast<SynchronizationScope>(
10936 cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue());
10937
10938 // The only fence that needs an instruction is a sequentially-consistent
10939 // cross-thread fence.
10940 if (FenceOrdering == SequentiallyConsistent && FenceScope == CrossThread) {
10941 // Use mfence if we have SSE2 or we're on x86-64 (even if we asked for
10942 // no-sse2). There isn't any reason to disable it if the target processor
10943 // supports it.
Craig Topper1accb7e2012-01-10 06:54:16 +000010944 if (Subtarget->hasSSE2() || Subtarget->is64Bit())
Eli Friedman14648462011-07-27 22:21:52 +000010945 return DAG.getNode(X86ISD::MFENCE, dl, MVT::Other, Op.getOperand(0));
10946
10947 SDValue Chain = Op.getOperand(0);
10948 SDValue Zero = DAG.getConstant(0, MVT::i32);
10949 SDValue Ops[] = {
10950 DAG.getRegister(X86::ESP, MVT::i32), // Base
10951 DAG.getTargetConstant(1, MVT::i8), // Scale
10952 DAG.getRegister(0, MVT::i32), // Index
10953 DAG.getTargetConstant(0, MVT::i32), // Disp
10954 DAG.getRegister(0, MVT::i32), // Segment.
10955 Zero,
10956 Chain
10957 };
10958 SDNode *Res =
10959 DAG.getMachineNode(X86::OR32mrLocked, dl, MVT::Other, Ops,
10960 array_lengthof(Ops));
10961 return SDValue(Res, 0);
10962 }
10963
10964 // MEMBARRIER is a compiler barrier; it codegens to a no-op.
10965 return DAG.getNode(X86ISD::MEMBARRIER, dl, MVT::Other, Op.getOperand(0));
10966}
10967
10968
Dan Gohmand858e902010-04-17 15:26:15 +000010969SDValue X86TargetLowering::LowerCMP_SWAP(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +000010970 EVT T = Op.getValueType();
Chris Lattner93c4a5b2010-09-21 23:59:42 +000010971 DebugLoc DL = Op.getDebugLoc();
Andrew Lenhartha76e2f02008-03-04 21:13:33 +000010972 unsigned Reg = 0;
10973 unsigned size = 0;
Owen Anderson825b72b2009-08-11 20:47:22 +000010974 switch(T.getSimpleVT().SimpleTy) {
Craig Topperabb94d02012-02-05 03:43:23 +000010975 default: llvm_unreachable("Invalid value type!");
Owen Anderson825b72b2009-08-11 20:47:22 +000010976 case MVT::i8: Reg = X86::AL; size = 1; break;
10977 case MVT::i16: Reg = X86::AX; size = 2; break;
10978 case MVT::i32: Reg = X86::EAX; size = 4; break;
10979 case MVT::i64:
Duncan Sands1607f052008-12-01 11:39:25 +000010980 assert(Subtarget->is64Bit() && "Node not type legal!");
10981 Reg = X86::RAX; size = 8;
Andrew Lenharthd19189e2008-03-05 01:15:49 +000010982 break;
Bill Wendling61edeb52008-12-02 01:06:39 +000010983 }
Chris Lattner93c4a5b2010-09-21 23:59:42 +000010984 SDValue cpIn = DAG.getCopyToReg(Op.getOperand(0), DL, Reg,
Dale Johannesend18a4622008-09-11 03:12:59 +000010985 Op.getOperand(2), SDValue());
Dan Gohman475871a2008-07-27 21:46:04 +000010986 SDValue Ops[] = { cpIn.getValue(0),
Evan Cheng8a186ae2008-09-24 23:26:36 +000010987 Op.getOperand(1),
10988 Op.getOperand(3),
Owen Anderson825b72b2009-08-11 20:47:22 +000010989 DAG.getTargetConstant(size, MVT::i8),
Evan Cheng8a186ae2008-09-24 23:26:36 +000010990 cpIn.getValue(1) };
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +000010991 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Chris Lattner93c4a5b2010-09-21 23:59:42 +000010992 MachineMemOperand *MMO = cast<AtomicSDNode>(Op)->getMemOperand();
10993 SDValue Result = DAG.getMemIntrinsicNode(X86ISD::LCMPXCHG_DAG, DL, Tys,
10994 Ops, 5, T, MMO);
Scott Michelfdc40a02009-02-17 22:15:04 +000010995 SDValue cpOut =
Chris Lattner93c4a5b2010-09-21 23:59:42 +000010996 DAG.getCopyFromReg(Result.getValue(0), DL, Reg, T, Result.getValue(1));
Andrew Lenharth26ed8692008-03-01 21:52:34 +000010997 return cpOut;
10998}
10999
Duncan Sands1607f052008-12-01 11:39:25 +000011000SDValue X86TargetLowering::LowerREADCYCLECOUNTER(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +000011001 SelectionDAG &DAG) const {
Duncan Sands1607f052008-12-01 11:39:25 +000011002 assert(Subtarget->is64Bit() && "Result not type legalized?");
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +000011003 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Duncan Sands1607f052008-12-01 11:39:25 +000011004 SDValue TheChain = Op.getOperand(0);
Dale Johannesen6f38cb62009-02-07 19:59:05 +000011005 DebugLoc dl = Op.getDebugLoc();
Dale Johannesene4d209d2009-02-03 20:21:25 +000011006 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, dl, Tys, &TheChain, 1);
Owen Anderson825b72b2009-08-11 20:47:22 +000011007 SDValue rax = DAG.getCopyFromReg(rd, dl, X86::RAX, MVT::i64, rd.getValue(1));
11008 SDValue rdx = DAG.getCopyFromReg(rax.getValue(1), dl, X86::RDX, MVT::i64,
Duncan Sands1607f052008-12-01 11:39:25 +000011009 rax.getValue(2));
Owen Anderson825b72b2009-08-11 20:47:22 +000011010 SDValue Tmp = DAG.getNode(ISD::SHL, dl, MVT::i64, rdx,
11011 DAG.getConstant(32, MVT::i8));
Duncan Sands1607f052008-12-01 11:39:25 +000011012 SDValue Ops[] = {
Owen Anderson825b72b2009-08-11 20:47:22 +000011013 DAG.getNode(ISD::OR, dl, MVT::i64, rax, Tmp),
Duncan Sands1607f052008-12-01 11:39:25 +000011014 rdx.getValue(1)
11015 };
Dale Johannesene4d209d2009-02-03 20:21:25 +000011016 return DAG.getMergeValues(Ops, 2, dl);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011017}
11018
Wesley Peckbf17cfa2010-11-23 03:31:01 +000011019SDValue X86TargetLowering::LowerBITCAST(SDValue Op,
Dale Johannesen7d07b482010-05-21 00:52:33 +000011020 SelectionDAG &DAG) const {
11021 EVT SrcVT = Op.getOperand(0).getValueType();
11022 EVT DstVT = Op.getValueType();
Craig Topper1accb7e2012-01-10 06:54:16 +000011023 assert(Subtarget->is64Bit() && !Subtarget->hasSSE2() &&
Chris Lattner2a786eb2010-12-19 20:19:20 +000011024 Subtarget->hasMMX() && "Unexpected custom BITCAST");
Michael J. Spencerec38de22010-10-10 22:04:20 +000011025 assert((DstVT == MVT::i64 ||
Dale Johannesen7d07b482010-05-21 00:52:33 +000011026 (DstVT.isVector() && DstVT.getSizeInBits()==64)) &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +000011027 "Unexpected custom BITCAST");
Dale Johannesen7d07b482010-05-21 00:52:33 +000011028 // i64 <=> MMX conversions are Legal.
11029 if (SrcVT==MVT::i64 && DstVT.isVector())
11030 return Op;
11031 if (DstVT==MVT::i64 && SrcVT.isVector())
11032 return Op;
Dale Johannesene39859a2010-05-21 18:40:15 +000011033 // MMX <=> MMX conversions are Legal.
11034 if (SrcVT.isVector() && DstVT.isVector())
11035 return Op;
Dale Johannesen7d07b482010-05-21 00:52:33 +000011036 // All other conversions need to be expanded.
11037 return SDValue();
11038}
Chris Lattner5b856542010-12-20 00:59:46 +000011039
Dan Gohmand858e902010-04-17 15:26:15 +000011040SDValue X86TargetLowering::LowerLOAD_SUB(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen71d1bf52008-09-29 22:25:26 +000011041 SDNode *Node = Op.getNode();
Dale Johannesene4d209d2009-02-03 20:21:25 +000011042 DebugLoc dl = Node->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +000011043 EVT T = Node->getValueType(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +000011044 SDValue negOp = DAG.getNode(ISD::SUB, dl, T,
Evan Cheng242b38b2009-02-23 09:03:22 +000011045 DAG.getConstant(0, T), Node->getOperand(2));
Dale Johannesene4d209d2009-02-03 20:21:25 +000011046 return DAG.getAtomic(ISD::ATOMIC_LOAD_ADD, dl,
Dan Gohman0b1d4a72008-12-23 21:37:04 +000011047 cast<AtomicSDNode>(Node)->getMemoryVT(),
Dale Johannesen71d1bf52008-09-29 22:25:26 +000011048 Node->getOperand(0),
11049 Node->getOperand(1), negOp,
11050 cast<AtomicSDNode>(Node)->getSrcValue(),
Eli Friedman55ba8162011-07-29 03:05:32 +000011051 cast<AtomicSDNode>(Node)->getAlignment(),
11052 cast<AtomicSDNode>(Node)->getOrdering(),
11053 cast<AtomicSDNode>(Node)->getSynchScope());
Mon P Wang63307c32008-05-05 19:05:59 +000011054}
11055
Eli Friedman327236c2011-08-24 20:50:09 +000011056static SDValue LowerATOMIC_STORE(SDValue Op, SelectionDAG &DAG) {
11057 SDNode *Node = Op.getNode();
11058 DebugLoc dl = Node->getDebugLoc();
Eli Friedmanf8f90f02011-08-24 22:33:28 +000011059 EVT VT = cast<AtomicSDNode>(Node)->getMemoryVT();
Eli Friedman327236c2011-08-24 20:50:09 +000011060
11061 // Convert seq_cst store -> xchg
Eli Friedmanf8f90f02011-08-24 22:33:28 +000011062 // Convert wide store -> swap (-> cmpxchg8b/cmpxchg16b)
11063 // FIXME: On 32-bit, store -> fist or movq would be more efficient
11064 // (The only way to get a 16-byte store is cmpxchg16b)
11065 // FIXME: 16-byte ATOMIC_SWAP isn't actually hooked up at the moment.
11066 if (cast<AtomicSDNode>(Node)->getOrdering() == SequentiallyConsistent ||
11067 !DAG.getTargetLoweringInfo().isTypeLegal(VT)) {
Eli Friedman4317fe12011-08-24 21:17:30 +000011068 SDValue Swap = DAG.getAtomic(ISD::ATOMIC_SWAP, dl,
11069 cast<AtomicSDNode>(Node)->getMemoryVT(),
11070 Node->getOperand(0),
11071 Node->getOperand(1), Node->getOperand(2),
Eli Friedmanf8f90f02011-08-24 22:33:28 +000011072 cast<AtomicSDNode>(Node)->getMemOperand(),
Eli Friedman4317fe12011-08-24 21:17:30 +000011073 cast<AtomicSDNode>(Node)->getOrdering(),
11074 cast<AtomicSDNode>(Node)->getSynchScope());
Eli Friedman327236c2011-08-24 20:50:09 +000011075 return Swap.getValue(1);
11076 }
11077 // Other atomic stores have a simple pattern.
11078 return Op;
11079}
11080
Chris Lattner5b856542010-12-20 00:59:46 +000011081static SDValue LowerADDC_ADDE_SUBC_SUBE(SDValue Op, SelectionDAG &DAG) {
11082 EVT VT = Op.getNode()->getValueType(0);
11083
11084 // Let legalize expand this if it isn't a legal type yet.
11085 if (!DAG.getTargetLoweringInfo().isTypeLegal(VT))
11086 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011087
Chris Lattner5b856542010-12-20 00:59:46 +000011088 SDVTList VTs = DAG.getVTList(VT, MVT::i32);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011089
Chris Lattner5b856542010-12-20 00:59:46 +000011090 unsigned Opc;
11091 bool ExtraOp = false;
11092 switch (Op.getOpcode()) {
Craig Topperabb94d02012-02-05 03:43:23 +000011093 default: llvm_unreachable("Invalid code");
Chris Lattner5b856542010-12-20 00:59:46 +000011094 case ISD::ADDC: Opc = X86ISD::ADD; break;
11095 case ISD::ADDE: Opc = X86ISD::ADC; ExtraOp = true; break;
11096 case ISD::SUBC: Opc = X86ISD::SUB; break;
11097 case ISD::SUBE: Opc = X86ISD::SBB; ExtraOp = true; break;
11098 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011099
Chris Lattner5b856542010-12-20 00:59:46 +000011100 if (!ExtraOp)
11101 return DAG.getNode(Opc, Op->getDebugLoc(), VTs, Op.getOperand(0),
11102 Op.getOperand(1));
11103 return DAG.getNode(Opc, Op->getDebugLoc(), VTs, Op.getOperand(0),
11104 Op.getOperand(1), Op.getOperand(2));
11105}
11106
Evan Cheng0db9fe62006-04-25 20:13:52 +000011107/// LowerOperation - Provide custom lowering hooks for some operations.
11108///
Dan Gohmand858e902010-04-17 15:26:15 +000011109SDValue X86TargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +000011110 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +000011111 default: llvm_unreachable("Should not custom lower this!");
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000011112 case ISD::SIGN_EXTEND_INREG: return LowerSIGN_EXTEND_INREG(Op,DAG);
Eric Christopher9a9d2752010-07-22 02:48:34 +000011113 case ISD::MEMBARRIER: return LowerMEMBARRIER(Op,DAG);
Eli Friedman14648462011-07-27 22:21:52 +000011114 case ISD::ATOMIC_FENCE: return LowerATOMIC_FENCE(Op,DAG);
Dan Gohman0b1d4a72008-12-23 21:37:04 +000011115 case ISD::ATOMIC_CMP_SWAP: return LowerCMP_SWAP(Op,DAG);
11116 case ISD::ATOMIC_LOAD_SUB: return LowerLOAD_SUB(Op,DAG);
Eli Friedman327236c2011-08-24 20:50:09 +000011117 case ISD::ATOMIC_STORE: return LowerATOMIC_STORE(Op,DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000011118 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
Mon P Wangeb38ebf2010-01-24 00:05:03 +000011119 case ISD::CONCAT_VECTORS: return LowerCONCAT_VECTORS(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000011120 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
11121 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
11122 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
David Greene91585092011-01-26 15:38:49 +000011123 case ISD::EXTRACT_SUBVECTOR: return LowerEXTRACT_SUBVECTOR(Op, DAG);
David Greenecfe33c42011-01-26 19:13:22 +000011124 case ISD::INSERT_SUBVECTOR: return LowerINSERT_SUBVECTOR(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000011125 case ISD::SCALAR_TO_VECTOR: return LowerSCALAR_TO_VECTOR(Op, DAG);
11126 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
11127 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +000011128 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Bill Wendling056292f2008-09-16 21:48:12 +000011129 case ISD::ExternalSymbol: return LowerExternalSymbol(Op, DAG);
Dan Gohmanf705adb2009-10-30 01:28:02 +000011130 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000011131 case ISD::SHL_PARTS:
11132 case ISD::SRA_PARTS:
Nadav Rotem43012222011-05-11 08:12:09 +000011133 case ISD::SRL_PARTS: return LowerShiftParts(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000011134 case ISD::SINT_TO_FP: return LowerSINT_TO_FP(Op, DAG);
Dale Johannesen1c15bf52008-10-21 20:50:01 +000011135 case ISD::UINT_TO_FP: return LowerUINT_TO_FP(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000011136 case ISD::FP_TO_SINT: return LowerFP_TO_SINT(Op, DAG);
Eli Friedman948e95a2009-05-23 09:59:16 +000011137 case ISD::FP_TO_UINT: return LowerFP_TO_UINT(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000011138 case ISD::FABS: return LowerFABS(Op, DAG);
11139 case ISD::FNEG: return LowerFNEG(Op, DAG);
Evan Cheng68c47cb2007-01-05 07:55:56 +000011140 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Stuart Hastings4fd0dee2011-06-01 04:39:42 +000011141 case ISD::FGETSIGN: return LowerFGETSIGN(Op, DAG);
Evan Chenge5f62042007-09-29 00:00:36 +000011142 case ISD::SETCC: return LowerSETCC(Op, DAG);
11143 case ISD::SELECT: return LowerSELECT(Op, DAG);
11144 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000011145 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000011146 case ISD::VASTART: return LowerVASTART(Op, DAG);
Dan Gohman9018e832008-05-10 01:26:14 +000011147 case ISD::VAARG: return LowerVAARG(Op, DAG);
Evan Chengae642192007-03-02 23:16:35 +000011148 case ISD::VACOPY: return LowerVACOPY(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000011149 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
Benjamin Kramerb9bee042012-07-12 09:31:43 +000011150 case ISD::INTRINSIC_W_CHAIN: return LowerINTRINSIC_W_CHAIN(Op, DAG);
Nate Begemanbcc5f362007-01-29 22:58:52 +000011151 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
11152 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Anton Korobeynikov2365f512007-07-14 14:06:15 +000011153 case ISD::FRAME_TO_ARGS_OFFSET:
11154 return LowerFRAME_TO_ARGS_OFFSET(Op, DAG);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +000011155 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
Anton Korobeynikov2365f512007-07-14 14:06:15 +000011156 case ISD::EH_RETURN: return LowerEH_RETURN(Op, DAG);
Duncan Sands4a544a72011-09-06 13:37:06 +000011157 case ISD::INIT_TRAMPOLINE: return LowerINIT_TRAMPOLINE(Op, DAG);
11158 case ISD::ADJUST_TRAMPOLINE: return LowerADJUST_TRAMPOLINE(Op, DAG);
Dan Gohman1a024862008-01-31 00:41:03 +000011159 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
Evan Cheng18efe262007-12-14 02:13:44 +000011160 case ISD::CTLZ: return LowerCTLZ(Op, DAG);
Chandler Carruthacc068e2011-12-24 10:55:54 +000011161 case ISD::CTLZ_ZERO_UNDEF: return LowerCTLZ_ZERO_UNDEF(Op, DAG);
Evan Cheng18efe262007-12-14 02:13:44 +000011162 case ISD::CTTZ: return LowerCTTZ(Op, DAG);
Craig Topper13894fa2011-08-24 06:14:18 +000011163 case ISD::MUL: return LowerMUL(Op, DAG);
Nadav Rotem43012222011-05-11 08:12:09 +000011164 case ISD::SRA:
11165 case ISD::SRL:
11166 case ISD::SHL: return LowerShift(Op, DAG);
Bill Wendling74c37652008-12-09 22:08:41 +000011167 case ISD::SADDO:
11168 case ISD::UADDO:
11169 case ISD::SSUBO:
11170 case ISD::USUBO:
11171 case ISD::SMULO:
11172 case ISD::UMULO: return LowerXALUO(Op, DAG);
Duncan Sands1607f052008-12-01 11:39:25 +000011173 case ISD::READCYCLECOUNTER: return LowerREADCYCLECOUNTER(Op, DAG);
Wesley Peckbf17cfa2010-11-23 03:31:01 +000011174 case ISD::BITCAST: return LowerBITCAST(Op, DAG);
Chris Lattner5b856542010-12-20 00:59:46 +000011175 case ISD::ADDC:
11176 case ISD::ADDE:
11177 case ISD::SUBC:
11178 case ISD::SUBE: return LowerADDC_ADDE_SUBC_SUBE(Op, DAG);
Craig Topper13894fa2011-08-24 06:14:18 +000011179 case ISD::ADD: return LowerADD(Op, DAG);
11180 case ISD::SUB: return LowerSUB(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000011181 }
Chris Lattner27a6c732007-11-24 07:07:01 +000011182}
11183
Eli Friedmanf8f90f02011-08-24 22:33:28 +000011184static void ReplaceATOMIC_LOAD(SDNode *Node,
11185 SmallVectorImpl<SDValue> &Results,
11186 SelectionDAG &DAG) {
11187 DebugLoc dl = Node->getDebugLoc();
11188 EVT VT = cast<AtomicSDNode>(Node)->getMemoryVT();
11189
11190 // Convert wide load -> cmpxchg8b/cmpxchg16b
11191 // FIXME: On 32-bit, load -> fild or movq would be more efficient
11192 // (The only way to get a 16-byte load is cmpxchg16b)
11193 // FIXME: 16-byte ATOMIC_CMP_SWAP isn't actually hooked up at the moment.
Benjamin Kramer2753ae32011-08-27 17:36:14 +000011194 SDValue Zero = DAG.getConstant(0, VT);
11195 SDValue Swap = DAG.getAtomic(ISD::ATOMIC_CMP_SWAP, dl, VT,
Eli Friedmanf8f90f02011-08-24 22:33:28 +000011196 Node->getOperand(0),
11197 Node->getOperand(1), Zero, Zero,
11198 cast<AtomicSDNode>(Node)->getMemOperand(),
11199 cast<AtomicSDNode>(Node)->getOrdering(),
11200 cast<AtomicSDNode>(Node)->getSynchScope());
11201 Results.push_back(Swap.getValue(0));
11202 Results.push_back(Swap.getValue(1));
11203}
11204
Craig Topperc0878702012-08-17 06:55:11 +000011205static void
Duncan Sands1607f052008-12-01 11:39:25 +000011206ReplaceATOMIC_BINARY_64(SDNode *Node, SmallVectorImpl<SDValue>&Results,
Craig Topperc0878702012-08-17 06:55:11 +000011207 SelectionDAG &DAG, unsigned NewOp) {
Dale Johannesene4d209d2009-02-03 20:21:25 +000011208 DebugLoc dl = Node->getDebugLoc();
Duncan Sands17001ce2011-10-18 12:44:00 +000011209 assert (Node->getValueType(0) == MVT::i64 &&
11210 "Only know how to expand i64 atomics");
Duncan Sands1607f052008-12-01 11:39:25 +000011211
11212 SDValue Chain = Node->getOperand(0);
11213 SDValue In1 = Node->getOperand(1);
Owen Anderson825b72b2009-08-11 20:47:22 +000011214 SDValue In2L = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +000011215 Node->getOperand(2), DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +000011216 SDValue In2H = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +000011217 Node->getOperand(2), DAG.getIntPtrConstant(1));
Dan Gohmanc76909a2009-09-25 20:36:54 +000011218 SDValue Ops[] = { Chain, In1, In2L, In2H };
Owen Anderson825b72b2009-08-11 20:47:22 +000011219 SDVTList Tys = DAG.getVTList(MVT::i32, MVT::i32, MVT::Other);
Dan Gohmanc76909a2009-09-25 20:36:54 +000011220 SDValue Result =
11221 DAG.getMemIntrinsicNode(NewOp, dl, Tys, Ops, 4, MVT::i64,
11222 cast<MemSDNode>(Node)->getMemOperand());
Duncan Sands1607f052008-12-01 11:39:25 +000011223 SDValue OpsF[] = { Result.getValue(0), Result.getValue(1)};
Owen Anderson825b72b2009-08-11 20:47:22 +000011224 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, OpsF, 2));
Duncan Sands1607f052008-12-01 11:39:25 +000011225 Results.push_back(Result.getValue(2));
11226}
11227
Duncan Sands126d9072008-07-04 11:47:58 +000011228/// ReplaceNodeResults - Replace a node with an illegal result type
11229/// with a new node built out of custom code.
Duncan Sands1607f052008-12-01 11:39:25 +000011230void X86TargetLowering::ReplaceNodeResults(SDNode *N,
11231 SmallVectorImpl<SDValue>&Results,
Dan Gohmand858e902010-04-17 15:26:15 +000011232 SelectionDAG &DAG) const {
Dale Johannesene4d209d2009-02-03 20:21:25 +000011233 DebugLoc dl = N->getDebugLoc();
Chris Lattner27a6c732007-11-24 07:07:01 +000011234 switch (N->getOpcode()) {
Duncan Sandsed294c42008-10-20 15:56:33 +000011235 default:
Craig Topperabb94d02012-02-05 03:43:23 +000011236 llvm_unreachable("Do not know how to custom type legalize this operation!");
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000011237 case ISD::SIGN_EXTEND_INREG:
Chris Lattner5b856542010-12-20 00:59:46 +000011238 case ISD::ADDC:
11239 case ISD::ADDE:
11240 case ISD::SUBC:
11241 case ISD::SUBE:
11242 // We don't want to expand or promote these.
11243 return;
Michael J. Spencer1a2d0612012-02-24 19:01:22 +000011244 case ISD::FP_TO_SINT:
11245 case ISD::FP_TO_UINT: {
11246 bool IsSigned = N->getOpcode() == ISD::FP_TO_SINT;
11247
11248 if (!IsSigned && !isIntegerTypeFTOL(SDValue(N, 0).getValueType()))
11249 return;
11250
Eli Friedman948e95a2009-05-23 09:59:16 +000011251 std::pair<SDValue,SDValue> Vals =
NAKAMURA Takumi9a68fdc2012-02-25 03:37:25 +000011252 FP_TO_INTHelper(SDValue(N, 0), DAG, IsSigned, /*IsReplace=*/ true);
Duncan Sands1607f052008-12-01 11:39:25 +000011253 SDValue FIST = Vals.first, StackSlot = Vals.second;
11254 if (FIST.getNode() != 0) {
Owen Andersone50ed302009-08-10 22:56:29 +000011255 EVT VT = N->getValueType(0);
Duncan Sands1607f052008-12-01 11:39:25 +000011256 // Return a load from the stack slot.
Michael J. Spencer1a2d0612012-02-24 19:01:22 +000011257 if (StackSlot.getNode() != 0)
11258 Results.push_back(DAG.getLoad(VT, dl, FIST, StackSlot,
11259 MachinePointerInfo(),
11260 false, false, false, 0));
11261 else
11262 Results.push_back(FIST);
Duncan Sands1607f052008-12-01 11:39:25 +000011263 }
11264 return;
11265 }
11266 case ISD::READCYCLECOUNTER: {
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +000011267 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Duncan Sands1607f052008-12-01 11:39:25 +000011268 SDValue TheChain = N->getOperand(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +000011269 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, dl, Tys, &TheChain, 1);
Owen Anderson825b72b2009-08-11 20:47:22 +000011270 SDValue eax = DAG.getCopyFromReg(rd, dl, X86::EAX, MVT::i32,
Dale Johannesendd64c412009-02-04 00:33:20 +000011271 rd.getValue(1));
Owen Anderson825b72b2009-08-11 20:47:22 +000011272 SDValue edx = DAG.getCopyFromReg(eax.getValue(1), dl, X86::EDX, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +000011273 eax.getValue(2));
11274 // Use a buildpair to merge the two 32-bit values into a 64-bit one.
11275 SDValue Ops[] = { eax, edx };
Owen Anderson825b72b2009-08-11 20:47:22 +000011276 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Ops, 2));
Duncan Sands1607f052008-12-01 11:39:25 +000011277 Results.push_back(edx.getValue(1));
11278 return;
11279 }
Dan Gohman0b1d4a72008-12-23 21:37:04 +000011280 case ISD::ATOMIC_CMP_SWAP: {
Owen Andersone50ed302009-08-10 22:56:29 +000011281 EVT T = N->getValueType(0);
Benjamin Kramer2753ae32011-08-27 17:36:14 +000011282 assert((T == MVT::i64 || T == MVT::i128) && "can only expand cmpxchg pair");
Eli Friedman43f51ae2011-08-26 21:21:21 +000011283 bool Regs64bit = T == MVT::i128;
11284 EVT HalfT = Regs64bit ? MVT::i64 : MVT::i32;
Duncan Sands1607f052008-12-01 11:39:25 +000011285 SDValue cpInL, cpInH;
Eli Friedman43f51ae2011-08-26 21:21:21 +000011286 cpInL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, HalfT, N->getOperand(2),
11287 DAG.getConstant(0, HalfT));
11288 cpInH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, HalfT, N->getOperand(2),
11289 DAG.getConstant(1, HalfT));
11290 cpInL = DAG.getCopyToReg(N->getOperand(0), dl,
11291 Regs64bit ? X86::RAX : X86::EAX,
11292 cpInL, SDValue());
11293 cpInH = DAG.getCopyToReg(cpInL.getValue(0), dl,
11294 Regs64bit ? X86::RDX : X86::EDX,
11295 cpInH, cpInL.getValue(1));
Duncan Sands1607f052008-12-01 11:39:25 +000011296 SDValue swapInL, swapInH;
Eli Friedman43f51ae2011-08-26 21:21:21 +000011297 swapInL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, HalfT, N->getOperand(3),
11298 DAG.getConstant(0, HalfT));
11299 swapInH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, HalfT, N->getOperand(3),
11300 DAG.getConstant(1, HalfT));
11301 swapInL = DAG.getCopyToReg(cpInH.getValue(0), dl,
11302 Regs64bit ? X86::RBX : X86::EBX,
11303 swapInL, cpInH.getValue(1));
11304 swapInH = DAG.getCopyToReg(swapInL.getValue(0), dl,
Chad Rosiera20e1e72012-08-01 18:39:17 +000011305 Regs64bit ? X86::RCX : X86::ECX,
Eli Friedman43f51ae2011-08-26 21:21:21 +000011306 swapInH, swapInL.getValue(1));
Duncan Sands1607f052008-12-01 11:39:25 +000011307 SDValue Ops[] = { swapInH.getValue(0),
11308 N->getOperand(1),
11309 swapInH.getValue(1) };
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +000011310 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Andrew Trick1a2cf3b2010-10-11 19:02:04 +000011311 MachineMemOperand *MMO = cast<AtomicSDNode>(N)->getMemOperand();
Eli Friedman43f51ae2011-08-26 21:21:21 +000011312 unsigned Opcode = Regs64bit ? X86ISD::LCMPXCHG16_DAG :
11313 X86ISD::LCMPXCHG8_DAG;
11314 SDValue Result = DAG.getMemIntrinsicNode(Opcode, dl, Tys,
Andrew Trick1a2cf3b2010-10-11 19:02:04 +000011315 Ops, 3, T, MMO);
Eli Friedman43f51ae2011-08-26 21:21:21 +000011316 SDValue cpOutL = DAG.getCopyFromReg(Result.getValue(0), dl,
11317 Regs64bit ? X86::RAX : X86::EAX,
11318 HalfT, Result.getValue(1));
11319 SDValue cpOutH = DAG.getCopyFromReg(cpOutL.getValue(1), dl,
11320 Regs64bit ? X86::RDX : X86::EDX,
11321 HalfT, cpOutL.getValue(2));
Duncan Sands1607f052008-12-01 11:39:25 +000011322 SDValue OpsF[] = { cpOutL.getValue(0), cpOutH.getValue(0)};
Eli Friedman43f51ae2011-08-26 21:21:21 +000011323 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, T, OpsF, 2));
Duncan Sands1607f052008-12-01 11:39:25 +000011324 Results.push_back(cpOutH.getValue(1));
11325 return;
11326 }
Dan Gohman0b1d4a72008-12-23 21:37:04 +000011327 case ISD::ATOMIC_LOAD_ADD:
Dan Gohman0b1d4a72008-12-23 21:37:04 +000011328 case ISD::ATOMIC_LOAD_AND:
Dan Gohman0b1d4a72008-12-23 21:37:04 +000011329 case ISD::ATOMIC_LOAD_NAND:
Dan Gohman0b1d4a72008-12-23 21:37:04 +000011330 case ISD::ATOMIC_LOAD_OR:
Dan Gohman0b1d4a72008-12-23 21:37:04 +000011331 case ISD::ATOMIC_LOAD_SUB:
Dan Gohman0b1d4a72008-12-23 21:37:04 +000011332 case ISD::ATOMIC_LOAD_XOR:
Craig Topperc0878702012-08-17 06:55:11 +000011333 case ISD::ATOMIC_SWAP: {
11334 unsigned Opc;
11335 switch (N->getOpcode()) {
11336 default: llvm_unreachable("Unexpected opcode");
11337 case ISD::ATOMIC_LOAD_ADD:
11338 Opc = X86ISD::ATOMADD64_DAG;
11339 break;
11340 case ISD::ATOMIC_LOAD_AND:
11341 Opc = X86ISD::ATOMAND64_DAG;
11342 break;
11343 case ISD::ATOMIC_LOAD_NAND:
11344 Opc = X86ISD::ATOMNAND64_DAG;
11345 break;
11346 case ISD::ATOMIC_LOAD_OR:
11347 Opc = X86ISD::ATOMOR64_DAG;
11348 break;
11349 case ISD::ATOMIC_LOAD_SUB:
11350 Opc = X86ISD::ATOMSUB64_DAG;
11351 break;
11352 case ISD::ATOMIC_LOAD_XOR:
11353 Opc = X86ISD::ATOMXOR64_DAG;
11354 break;
11355 case ISD::ATOMIC_SWAP:
11356 Opc = X86ISD::ATOMSWAP64_DAG;
11357 break;
11358 }
11359 ReplaceATOMIC_BINARY_64(N, Results, DAG, Opc);
Duncan Sands1607f052008-12-01 11:39:25 +000011360 return;
Craig Topperc0878702012-08-17 06:55:11 +000011361 }
Eli Friedmanf8f90f02011-08-24 22:33:28 +000011362 case ISD::ATOMIC_LOAD:
11363 ReplaceATOMIC_LOAD(N, Results, DAG);
Chris Lattner27a6c732007-11-24 07:07:01 +000011364 }
Evan Cheng0db9fe62006-04-25 20:13:52 +000011365}
11366
Evan Cheng72261582005-12-20 06:22:03 +000011367const char *X86TargetLowering::getTargetNodeName(unsigned Opcode) const {
11368 switch (Opcode) {
11369 default: return NULL;
Evan Cheng18efe262007-12-14 02:13:44 +000011370 case X86ISD::BSF: return "X86ISD::BSF";
11371 case X86ISD::BSR: return "X86ISD::BSR";
Evan Chenge3413162006-01-09 18:33:28 +000011372 case X86ISD::SHLD: return "X86ISD::SHLD";
11373 case X86ISD::SHRD: return "X86ISD::SHRD";
Evan Chengef6ffb12006-01-31 03:14:29 +000011374 case X86ISD::FAND: return "X86ISD::FAND";
Evan Cheng68c47cb2007-01-05 07:55:56 +000011375 case X86ISD::FOR: return "X86ISD::FOR";
Evan Cheng223547a2006-01-31 22:28:30 +000011376 case X86ISD::FXOR: return "X86ISD::FXOR";
Evan Cheng68c47cb2007-01-05 07:55:56 +000011377 case X86ISD::FSRL: return "X86ISD::FSRL";
Evan Chenga3195e82006-01-12 22:54:21 +000011378 case X86ISD::FILD: return "X86ISD::FILD";
Evan Chenge3de85b2006-02-04 02:20:30 +000011379 case X86ISD::FILD_FLAG: return "X86ISD::FILD_FLAG";
Evan Cheng72261582005-12-20 06:22:03 +000011380 case X86ISD::FP_TO_INT16_IN_MEM: return "X86ISD::FP_TO_INT16_IN_MEM";
11381 case X86ISD::FP_TO_INT32_IN_MEM: return "X86ISD::FP_TO_INT32_IN_MEM";
11382 case X86ISD::FP_TO_INT64_IN_MEM: return "X86ISD::FP_TO_INT64_IN_MEM";
Evan Chengb077b842005-12-21 02:39:21 +000011383 case X86ISD::FLD: return "X86ISD::FLD";
Evan Chengd90eb7f2006-01-05 00:27:02 +000011384 case X86ISD::FST: return "X86ISD::FST";
Evan Cheng72261582005-12-20 06:22:03 +000011385 case X86ISD::CALL: return "X86ISD::CALL";
Evan Cheng72261582005-12-20 06:22:03 +000011386 case X86ISD::RDTSC_DAG: return "X86ISD::RDTSC_DAG";
Dan Gohmanc7a37d42008-12-23 22:45:23 +000011387 case X86ISD::BT: return "X86ISD::BT";
Evan Cheng72261582005-12-20 06:22:03 +000011388 case X86ISD::CMP: return "X86ISD::CMP";
Evan Cheng6be2c582006-04-05 23:38:46 +000011389 case X86ISD::COMI: return "X86ISD::COMI";
11390 case X86ISD::UCOMI: return "X86ISD::UCOMI";
Evan Chengd5781fc2005-12-21 20:21:51 +000011391 case X86ISD::SETCC: return "X86ISD::SETCC";
Evan Chengad9c0a32009-12-15 00:53:42 +000011392 case X86ISD::SETCC_CARRY: return "X86ISD::SETCC_CARRY";
Stuart Hastings865f0932011-06-03 23:53:54 +000011393 case X86ISD::FSETCCsd: return "X86ISD::FSETCCsd";
11394 case X86ISD::FSETCCss: return "X86ISD::FSETCCss";
Evan Cheng72261582005-12-20 06:22:03 +000011395 case X86ISD::CMOV: return "X86ISD::CMOV";
11396 case X86ISD::BRCOND: return "X86ISD::BRCOND";
Evan Chengb077b842005-12-21 02:39:21 +000011397 case X86ISD::RET_FLAG: return "X86ISD::RET_FLAG";
Evan Cheng8df346b2006-03-04 01:12:00 +000011398 case X86ISD::REP_STOS: return "X86ISD::REP_STOS";
11399 case X86ISD::REP_MOVS: return "X86ISD::REP_MOVS";
Evan Cheng7ccced62006-02-18 00:15:05 +000011400 case X86ISD::GlobalBaseReg: return "X86ISD::GlobalBaseReg";
Evan Cheng020d2e82006-02-23 20:41:18 +000011401 case X86ISD::Wrapper: return "X86ISD::Wrapper";
Chris Lattner18c59872009-06-27 04:16:01 +000011402 case X86ISD::WrapperRIP: return "X86ISD::WrapperRIP";
Nate Begeman14d12ca2008-02-11 04:19:36 +000011403 case X86ISD::PEXTRB: return "X86ISD::PEXTRB";
Evan Chengb067a1e2006-03-31 19:22:53 +000011404 case X86ISD::PEXTRW: return "X86ISD::PEXTRW";
Nate Begeman14d12ca2008-02-11 04:19:36 +000011405 case X86ISD::INSERTPS: return "X86ISD::INSERTPS";
11406 case X86ISD::PINSRB: return "X86ISD::PINSRB";
Evan Cheng653159f2006-03-31 21:55:24 +000011407 case X86ISD::PINSRW: return "X86ISD::PINSRW";
Nate Begemanb9a47b82009-02-23 08:49:38 +000011408 case X86ISD::PSHUFB: return "X86ISD::PSHUFB";
Bruno Cardoso Lopesc1af4772011-07-13 21:36:47 +000011409 case X86ISD::ANDNP: return "X86ISD::ANDNP";
Craig Topper31133842011-11-19 07:33:10 +000011410 case X86ISD::PSIGN: return "X86ISD::PSIGN";
Craig Toppere6a62772011-11-13 17:31:07 +000011411 case X86ISD::BLENDV: return "X86ISD::BLENDV";
Nadav Roteme6113782012-04-11 06:40:27 +000011412 case X86ISD::BLENDPW: return "X86ISD::BLENDPW";
11413 case X86ISD::BLENDPS: return "X86ISD::BLENDPS";
11414 case X86ISD::BLENDPD: return "X86ISD::BLENDPD";
Craig Topperfe033152011-12-06 09:31:36 +000011415 case X86ISD::HADD: return "X86ISD::HADD";
11416 case X86ISD::HSUB: return "X86ISD::HSUB";
Craig Toppere6a62772011-11-13 17:31:07 +000011417 case X86ISD::FHADD: return "X86ISD::FHADD";
11418 case X86ISD::FHSUB: return "X86ISD::FHSUB";
Evan Cheng8ca29322006-11-10 21:43:37 +000011419 case X86ISD::FMAX: return "X86ISD::FMAX";
11420 case X86ISD::FMIN: return "X86ISD::FMIN";
Dan Gohman20382522007-07-10 00:05:58 +000011421 case X86ISD::FRSQRT: return "X86ISD::FRSQRT";
11422 case X86ISD::FRCP: return "X86ISD::FRCP";
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +000011423 case X86ISD::TLSADDR: return "X86ISD::TLSADDR";
Hans Wennborgf0234fc2012-06-01 16:27:21 +000011424 case X86ISD::TLSBASEADDR: return "X86ISD::TLSBASEADDR";
Eric Christopher30ef0e52010-06-03 04:07:48 +000011425 case X86ISD::TLSCALL: return "X86ISD::TLSCALL";
Anton Korobeynikov2365f512007-07-14 14:06:15 +000011426 case X86ISD::EH_RETURN: return "X86ISD::EH_RETURN";
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +000011427 case X86ISD::TC_RETURN: return "X86ISD::TC_RETURN";
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000011428 case X86ISD::FNSTCW16m: return "X86ISD::FNSTCW16m";
Benjamin Kramer17c836c2012-04-27 12:07:43 +000011429 case X86ISD::FNSTSW16r: return "X86ISD::FNSTSW16r";
Evan Cheng7e2ff772008-05-08 00:57:18 +000011430 case X86ISD::LCMPXCHG_DAG: return "X86ISD::LCMPXCHG_DAG";
11431 case X86ISD::LCMPXCHG8_DAG: return "X86ISD::LCMPXCHG8_DAG";
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011432 case X86ISD::ATOMADD64_DAG: return "X86ISD::ATOMADD64_DAG";
11433 case X86ISD::ATOMSUB64_DAG: return "X86ISD::ATOMSUB64_DAG";
11434 case X86ISD::ATOMOR64_DAG: return "X86ISD::ATOMOR64_DAG";
11435 case X86ISD::ATOMXOR64_DAG: return "X86ISD::ATOMXOR64_DAG";
11436 case X86ISD::ATOMAND64_DAG: return "X86ISD::ATOMAND64_DAG";
11437 case X86ISD::ATOMNAND64_DAG: return "X86ISD::ATOMNAND64_DAG";
Evan Chengd880b972008-05-09 21:53:03 +000011438 case X86ISD::VZEXT_MOVL: return "X86ISD::VZEXT_MOVL";
Michael Liaob7bf7262012-08-14 22:53:17 +000011439 case X86ISD::VSEXT_MOVL: return "X86ISD::VSEXT_MOVL";
Evan Chengd880b972008-05-09 21:53:03 +000011440 case X86ISD::VZEXT_LOAD: return "X86ISD::VZEXT_LOAD";
Michael Liao7091b242012-08-14 21:24:47 +000011441 case X86ISD::VFPEXT: return "X86ISD::VFPEXT";
Craig Toppered2e13d2012-01-22 19:15:14 +000011442 case X86ISD::VSHLDQ: return "X86ISD::VSHLDQ";
11443 case X86ISD::VSRLDQ: return "X86ISD::VSRLDQ";
Evan Chengf26ffe92008-05-29 08:22:04 +000011444 case X86ISD::VSHL: return "X86ISD::VSHL";
11445 case X86ISD::VSRL: return "X86ISD::VSRL";
Craig Toppered2e13d2012-01-22 19:15:14 +000011446 case X86ISD::VSRA: return "X86ISD::VSRA";
11447 case X86ISD::VSHLI: return "X86ISD::VSHLI";
11448 case X86ISD::VSRLI: return "X86ISD::VSRLI";
11449 case X86ISD::VSRAI: return "X86ISD::VSRAI";
Craig Topper1906d322012-01-22 23:36:02 +000011450 case X86ISD::CMPP: return "X86ISD::CMPP";
Craig Topper67609fd2012-01-22 22:42:16 +000011451 case X86ISD::PCMPEQ: return "X86ISD::PCMPEQ";
11452 case X86ISD::PCMPGT: return "X86ISD::PCMPGT";
Bill Wendlingab55ebd2008-12-12 00:56:36 +000011453 case X86ISD::ADD: return "X86ISD::ADD";
11454 case X86ISD::SUB: return "X86ISD::SUB";
Chris Lattner5b856542010-12-20 00:59:46 +000011455 case X86ISD::ADC: return "X86ISD::ADC";
11456 case X86ISD::SBB: return "X86ISD::SBB";
Bill Wendlingd350e022008-12-12 21:15:41 +000011457 case X86ISD::SMUL: return "X86ISD::SMUL";
11458 case X86ISD::UMUL: return "X86ISD::UMUL";
Dan Gohman076aee32009-03-04 19:44:21 +000011459 case X86ISD::INC: return "X86ISD::INC";
11460 case X86ISD::DEC: return "X86ISD::DEC";
Dan Gohmane220c4b2009-09-18 19:59:53 +000011461 case X86ISD::OR: return "X86ISD::OR";
11462 case X86ISD::XOR: return "X86ISD::XOR";
11463 case X86ISD::AND: return "X86ISD::AND";
Craig Topper54a11172011-10-14 07:06:56 +000011464 case X86ISD::ANDN: return "X86ISD::ANDN";
Craig Toppere6a62772011-11-13 17:31:07 +000011465 case X86ISD::BLSI: return "X86ISD::BLSI";
11466 case X86ISD::BLSMSK: return "X86ISD::BLSMSK";
11467 case X86ISD::BLSR: return "X86ISD::BLSR";
Evan Cheng73f24c92009-03-30 21:36:47 +000011468 case X86ISD::MUL_IMM: return "X86ISD::MUL_IMM";
Eric Christopher71c67532009-07-29 00:28:05 +000011469 case X86ISD::PTEST: return "X86ISD::PTEST";
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +000011470 case X86ISD::TESTP: return "X86ISD::TESTP";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000011471 case X86ISD::PALIGN: return "X86ISD::PALIGN";
11472 case X86ISD::PSHUFD: return "X86ISD::PSHUFD";
11473 case X86ISD::PSHUFHW: return "X86ISD::PSHUFHW";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000011474 case X86ISD::PSHUFLW: return "X86ISD::PSHUFLW";
Craig Topperb3982da2011-12-31 23:50:21 +000011475 case X86ISD::SHUFP: return "X86ISD::SHUFP";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000011476 case X86ISD::MOVLHPS: return "X86ISD::MOVLHPS";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000011477 case X86ISD::MOVLHPD: return "X86ISD::MOVLHPD";
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +000011478 case X86ISD::MOVHLPS: return "X86ISD::MOVHLPS";
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +000011479 case X86ISD::MOVLPS: return "X86ISD::MOVLPS";
11480 case X86ISD::MOVLPD: return "X86ISD::MOVLPD";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000011481 case X86ISD::MOVDDUP: return "X86ISD::MOVDDUP";
11482 case X86ISD::MOVSHDUP: return "X86ISD::MOVSHDUP";
11483 case X86ISD::MOVSLDUP: return "X86ISD::MOVSLDUP";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000011484 case X86ISD::MOVSD: return "X86ISD::MOVSD";
11485 case X86ISD::MOVSS: return "X86ISD::MOVSS";
Craig Topper34671b82011-12-06 08:21:25 +000011486 case X86ISD::UNPCKL: return "X86ISD::UNPCKL";
11487 case X86ISD::UNPCKH: return "X86ISD::UNPCKH";
Bruno Cardoso Lopes0e6d2302011-08-17 02:29:19 +000011488 case X86ISD::VBROADCAST: return "X86ISD::VBROADCAST";
Craig Topper316cd2a2011-11-30 06:25:25 +000011489 case X86ISD::VPERMILP: return "X86ISD::VPERMILP";
Craig Topperec24e612011-11-30 07:47:51 +000011490 case X86ISD::VPERM2X128: return "X86ISD::VPERM2X128";
Craig Topper8325c112012-04-16 00:41:45 +000011491 case X86ISD::VPERMV: return "X86ISD::VPERMV";
11492 case X86ISD::VPERMI: return "X86ISD::VPERMI";
Craig Topper5b209e82012-02-05 03:14:49 +000011493 case X86ISD::PMULUDQ: return "X86ISD::PMULUDQ";
Dan Gohmand6708ea2009-08-15 01:38:56 +000011494 case X86ISD::VASTART_SAVE_XMM_REGS: return "X86ISD::VASTART_SAVE_XMM_REGS";
Dan Gohman320afb82010-10-12 18:00:49 +000011495 case X86ISD::VAARG_64: return "X86ISD::VAARG_64";
Michael J. Spencere9c253e2010-10-21 01:41:01 +000011496 case X86ISD::WIN_ALLOCA: return "X86ISD::WIN_ALLOCA";
Eli Friedman14648462011-07-27 22:21:52 +000011497 case X86ISD::MEMBARRIER: return "X86ISD::MEMBARRIER";
Rafael Espindolad07b7ec2011-08-30 19:43:21 +000011498 case X86ISD::SEG_ALLOCA: return "X86ISD::SEG_ALLOCA";
Michael J. Spencer1a2d0612012-02-24 19:01:22 +000011499 case X86ISD::WIN_FTOL: return "X86ISD::WIN_FTOL";
Benjamin Kramer17c836c2012-04-27 12:07:43 +000011500 case X86ISD::SAHF: return "X86ISD::SAHF";
Benjamin Kramerb9bee042012-07-12 09:31:43 +000011501 case X86ISD::RDRAND: return "X86ISD::RDRAND";
Elena Demikhovsky1503aba2012-08-01 12:06:00 +000011502 case X86ISD::FMADD: return "X86ISD::FMADD";
11503 case X86ISD::FMSUB: return "X86ISD::FMSUB";
11504 case X86ISD::FNMADD: return "X86ISD::FNMADD";
11505 case X86ISD::FNMSUB: return "X86ISD::FNMSUB";
11506 case X86ISD::FMADDSUB: return "X86ISD::FMADDSUB";
11507 case X86ISD::FMSUBADD: return "X86ISD::FMSUBADD";
Evan Cheng72261582005-12-20 06:22:03 +000011508 }
11509}
Evan Cheng3a03ebb2005-12-21 23:05:39 +000011510
Chris Lattnerc9addb72007-03-30 23:15:24 +000011511// isLegalAddressingMode - Return true if the addressing mode represented
11512// by AM is legal for this target, for a load/store of the specified type.
Scott Michelfdc40a02009-02-17 22:15:04 +000011513bool X86TargetLowering::isLegalAddressingMode(const AddrMode &AM,
Chris Lattnerdb125cf2011-07-18 04:54:35 +000011514 Type *Ty) const {
Chris Lattnerc9addb72007-03-30 23:15:24 +000011515 // X86 supports extremely general addressing modes.
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000011516 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman92b651f2010-08-24 15:55:12 +000011517 Reloc::Model R = getTargetMachine().getRelocationModel();
Scott Michelfdc40a02009-02-17 22:15:04 +000011518
Chris Lattnerc9addb72007-03-30 23:15:24 +000011519 // X86 allows a sign-extended 32-bit immediate field as a displacement.
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000011520 if (!X86::isOffsetSuitableForCodeModel(AM.BaseOffs, M, AM.BaseGV != NULL))
Chris Lattnerc9addb72007-03-30 23:15:24 +000011521 return false;
Scott Michelfdc40a02009-02-17 22:15:04 +000011522
Chris Lattnerc9addb72007-03-30 23:15:24 +000011523 if (AM.BaseGV) {
Chris Lattnerdfed4132009-07-10 07:38:24 +000011524 unsigned GVFlags =
11525 Subtarget->ClassifyGlobalReference(AM.BaseGV, getTargetMachine());
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000011526
Chris Lattnerdfed4132009-07-10 07:38:24 +000011527 // If a reference to this global requires an extra load, we can't fold it.
11528 if (isGlobalStubReference(GVFlags))
Chris Lattnerc9addb72007-03-30 23:15:24 +000011529 return false;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000011530
Chris Lattnerdfed4132009-07-10 07:38:24 +000011531 // If BaseGV requires a register for the PIC base, we cannot also have a
11532 // BaseReg specified.
11533 if (AM.HasBaseReg && isGlobalRelativeToPICBase(GVFlags))
Dale Johannesen203af582008-12-05 21:47:27 +000011534 return false;
Evan Cheng52787842007-08-01 23:46:47 +000011535
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000011536 // If lower 4G is not available, then we must use rip-relative addressing.
Dan Gohman92b651f2010-08-24 15:55:12 +000011537 if ((M != CodeModel::Small || R != Reloc::Static) &&
11538 Subtarget->is64Bit() && (AM.BaseOffs || AM.Scale > 1))
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000011539 return false;
Chris Lattnerc9addb72007-03-30 23:15:24 +000011540 }
Scott Michelfdc40a02009-02-17 22:15:04 +000011541
Chris Lattnerc9addb72007-03-30 23:15:24 +000011542 switch (AM.Scale) {
11543 case 0:
11544 case 1:
11545 case 2:
11546 case 4:
11547 case 8:
11548 // These scales always work.
11549 break;
11550 case 3:
11551 case 5:
11552 case 9:
11553 // These scales are formed with basereg+scalereg. Only accept if there is
11554 // no basereg yet.
11555 if (AM.HasBaseReg)
11556 return false;
11557 break;
11558 default: // Other stuff never works.
11559 return false;
11560 }
Scott Michelfdc40a02009-02-17 22:15:04 +000011561
Chris Lattnerc9addb72007-03-30 23:15:24 +000011562 return true;
11563}
11564
11565
Chris Lattnerdb125cf2011-07-18 04:54:35 +000011566bool X86TargetLowering::isTruncateFree(Type *Ty1, Type *Ty2) const {
Duncan Sandsb0bc6c32010-02-15 16:12:20 +000011567 if (!Ty1->isIntegerTy() || !Ty2->isIntegerTy())
Evan Cheng2bd122c2007-10-26 01:56:11 +000011568 return false;
Evan Chenge127a732007-10-29 07:57:50 +000011569 unsigned NumBits1 = Ty1->getPrimitiveSizeInBits();
11570 unsigned NumBits2 = Ty2->getPrimitiveSizeInBits();
Evan Cheng260e07e2008-03-20 02:18:41 +000011571 if (NumBits1 <= NumBits2)
Evan Chenge127a732007-10-29 07:57:50 +000011572 return false;
Dan Gohman377fbc02010-02-25 03:04:36 +000011573 return true;
Evan Cheng2bd122c2007-10-26 01:56:11 +000011574}
11575
Evan Cheng70e10d32012-07-17 06:53:39 +000011576bool X86TargetLowering::isLegalICmpImmediate(int64_t Imm) const {
11577 return Imm == (int32_t)Imm;
11578}
11579
11580bool X86TargetLowering::isLegalAddImmediate(int64_t Imm) const {
Evan Chenga9e13ba2012-07-17 18:54:11 +000011581 // Can also use sub to handle negated immediates.
Evan Cheng70e10d32012-07-17 06:53:39 +000011582 return Imm == (int32_t)Imm;
11583}
11584
Owen Andersone50ed302009-08-10 22:56:29 +000011585bool X86TargetLowering::isTruncateFree(EVT VT1, EVT VT2) const {
Duncan Sands83ec4b62008-06-06 12:08:01 +000011586 if (!VT1.isInteger() || !VT2.isInteger())
Evan Cheng3c3ddb32007-10-29 19:58:20 +000011587 return false;
Duncan Sands83ec4b62008-06-06 12:08:01 +000011588 unsigned NumBits1 = VT1.getSizeInBits();
11589 unsigned NumBits2 = VT2.getSizeInBits();
Evan Cheng260e07e2008-03-20 02:18:41 +000011590 if (NumBits1 <= NumBits2)
Evan Cheng3c3ddb32007-10-29 19:58:20 +000011591 return false;
Dan Gohman377fbc02010-02-25 03:04:36 +000011592 return true;
Evan Cheng3c3ddb32007-10-29 19:58:20 +000011593}
Evan Cheng2bd122c2007-10-26 01:56:11 +000011594
Chris Lattnerdb125cf2011-07-18 04:54:35 +000011595bool X86TargetLowering::isZExtFree(Type *Ty1, Type *Ty2) const {
Dan Gohman349ba492009-04-09 02:06:09 +000011596 // x86-64 implicitly zero-extends 32-bit results in 64-bit registers.
Duncan Sandsb0bc6c32010-02-15 16:12:20 +000011597 return Ty1->isIntegerTy(32) && Ty2->isIntegerTy(64) && Subtarget->is64Bit();
Dan Gohman97121ba2009-04-08 00:15:30 +000011598}
11599
Owen Andersone50ed302009-08-10 22:56:29 +000011600bool X86TargetLowering::isZExtFree(EVT VT1, EVT VT2) const {
Dan Gohman349ba492009-04-09 02:06:09 +000011601 // x86-64 implicitly zero-extends 32-bit results in 64-bit registers.
Owen Anderson825b72b2009-08-11 20:47:22 +000011602 return VT1 == MVT::i32 && VT2 == MVT::i64 && Subtarget->is64Bit();
Dan Gohman97121ba2009-04-08 00:15:30 +000011603}
11604
Owen Andersone50ed302009-08-10 22:56:29 +000011605bool X86TargetLowering::isNarrowingProfitable(EVT VT1, EVT VT2) const {
Evan Cheng8b944d32009-05-28 00:35:15 +000011606 // i16 instructions are longer (0x66 prefix) and potentially slower.
Owen Anderson825b72b2009-08-11 20:47:22 +000011607 return !(VT1 == MVT::i32 && VT2 == MVT::i16);
Evan Cheng8b944d32009-05-28 00:35:15 +000011608}
11609
Evan Cheng60c07e12006-07-05 22:17:51 +000011610/// isShuffleMaskLegal - Targets can use this to indicate that they only
11611/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
11612/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
11613/// are assumed to be legal.
11614bool
Eric Christopherfd179292009-08-27 18:07:15 +000011615X86TargetLowering::isShuffleMaskLegal(const SmallVectorImpl<int> &M,
Owen Andersone50ed302009-08-10 22:56:29 +000011616 EVT VT) const {
Eric Christophercff6f852010-04-15 01:40:20 +000011617 // Very little shuffling can be done for 64-bit vectors right now.
Nate Begeman9008ca62009-04-27 18:41:29 +000011618 if (VT.getSizeInBits() == 64)
Craig Topper1dc0fbc2011-12-05 07:27:14 +000011619 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +000011620
Nate Begemana09008b2009-10-19 02:17:23 +000011621 // FIXME: pshufb, blends, shifts.
Nate Begeman9008ca62009-04-27 18:41:29 +000011622 return (VT.getVectorNumElements() == 2 ||
11623 ShuffleVectorSDNode::isSplatMask(&M[0], VT) ||
11624 isMOVLMask(M, VT) ||
Craig Topper1a7700a2012-01-19 08:19:12 +000011625 isSHUFPMask(M, VT, Subtarget->hasAVX()) ||
Nate Begeman9008ca62009-04-27 18:41:29 +000011626 isPSHUFDMask(M, VT) ||
Craig Toppera9a568a2012-05-02 08:03:44 +000011627 isPSHUFHWMask(M, VT, Subtarget->hasAVX2()) ||
11628 isPSHUFLWMask(M, VT, Subtarget->hasAVX2()) ||
Craig Topper0e2037b2012-01-20 05:53:00 +000011629 isPALIGNRMask(M, VT, Subtarget) ||
Craig Topper6347e862011-11-21 06:57:39 +000011630 isUNPCKLMask(M, VT, Subtarget->hasAVX2()) ||
11631 isUNPCKHMask(M, VT, Subtarget->hasAVX2()) ||
Craig Topper94438ba2011-12-16 08:06:31 +000011632 isUNPCKL_v_undef_Mask(M, VT, Subtarget->hasAVX2()) ||
11633 isUNPCKH_v_undef_Mask(M, VT, Subtarget->hasAVX2()));
Evan Cheng60c07e12006-07-05 22:17:51 +000011634}
11635
Dan Gohman7d8143f2008-04-09 20:09:42 +000011636bool
Nate Begeman5a5ca152009-04-29 05:20:52 +000011637X86TargetLowering::isVectorClearMaskLegal(const SmallVectorImpl<int> &Mask,
Owen Andersone50ed302009-08-10 22:56:29 +000011638 EVT VT) const {
Nate Begeman9008ca62009-04-27 18:41:29 +000011639 unsigned NumElts = VT.getVectorNumElements();
11640 // FIXME: This collection of masks seems suspect.
11641 if (NumElts == 2)
11642 return true;
Craig Topper7a9a28b2012-08-12 02:23:29 +000011643 if (NumElts == 4 && VT.is128BitVector()) {
Nate Begeman9008ca62009-04-27 18:41:29 +000011644 return (isMOVLMask(Mask, VT) ||
11645 isCommutedMOVLMask(Mask, VT, true) ||
Craig Topper1a7700a2012-01-19 08:19:12 +000011646 isSHUFPMask(Mask, VT, Subtarget->hasAVX()) ||
11647 isSHUFPMask(Mask, VT, Subtarget->hasAVX(), /* Commuted */ true));
Evan Cheng60c07e12006-07-05 22:17:51 +000011648 }
11649 return false;
11650}
11651
11652//===----------------------------------------------------------------------===//
11653// X86 Scheduler Hooks
11654//===----------------------------------------------------------------------===//
11655
Mon P Wang63307c32008-05-05 19:05:59 +000011656// private utility function
11657MachineBasicBlock *
11658X86TargetLowering::EmitAtomicBitwiseWithCustomInserter(MachineInstr *bInstr,
11659 MachineBasicBlock *MBB,
11660 unsigned regOpc,
Andrew Lenharth507a58a2008-06-14 05:48:15 +000011661 unsigned immOpc,
Dale Johannesen140be2d2008-08-19 18:47:28 +000011662 unsigned LoadOpc,
11663 unsigned CXchgOpc,
Dale Johannesen140be2d2008-08-19 18:47:28 +000011664 unsigned notOpc,
11665 unsigned EAXreg,
Craig Topper44d23822012-02-22 05:59:10 +000011666 const TargetRegisterClass *RC,
Richard Smith42fc29e2012-04-13 22:47:00 +000011667 bool Invert) const {
Mon P Wang63307c32008-05-05 19:05:59 +000011668 // For the atomic bitwise operator, we generate
11669 // thisMBB:
11670 // newMBB:
Mon P Wangab3e7472008-05-05 22:56:23 +000011671 // ld t1 = [bitinstr.addr]
11672 // op t2 = t1, [bitinstr.val]
Richard Smith42fc29e2012-04-13 22:47:00 +000011673 // not t3 = t2 (if Invert)
Mon P Wangab3e7472008-05-05 22:56:23 +000011674 // mov EAX = t1
Richard Smith42fc29e2012-04-13 22:47:00 +000011675 // lcs dest = [bitinstr.addr], t3 [EAX is implicit]
Mon P Wang63307c32008-05-05 19:05:59 +000011676 // bz newMBB
11677 // fallthrough -->nextMBB
11678 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
11679 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +000011680 MachineFunction::iterator MBBIter = MBB;
Mon P Wang63307c32008-05-05 19:05:59 +000011681 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +000011682
Mon P Wang63307c32008-05-05 19:05:59 +000011683 /// First build the CFG
11684 MachineFunction *F = MBB->getParent();
11685 MachineBasicBlock *thisMBB = MBB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +000011686 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
11687 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
11688 F->insert(MBBIter, newMBB);
11689 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011690
Dan Gohman14152b42010-07-06 20:24:04 +000011691 // Transfer the remainder of thisMBB and its successor edges to nextMBB.
11692 nextMBB->splice(nextMBB->begin(), thisMBB,
11693 llvm::next(MachineBasicBlock::iterator(bInstr)),
11694 thisMBB->end());
11695 nextMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011696
Mon P Wang63307c32008-05-05 19:05:59 +000011697 // Update thisMBB to fall through to newMBB
11698 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011699
Mon P Wang63307c32008-05-05 19:05:59 +000011700 // newMBB jumps to itself and fall through to nextMBB
11701 newMBB->addSuccessor(nextMBB);
11702 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011703
Mon P Wang63307c32008-05-05 19:05:59 +000011704 // Insert instructions into newMBB based on incoming instruction
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000011705 assert(bInstr->getNumOperands() < X86::AddrNumOperands + 4 &&
Bill Wendling51b16f42009-05-30 01:09:53 +000011706 "unexpected number of operands");
Dale Johannesene4d209d2009-02-03 20:21:25 +000011707 DebugLoc dl = bInstr->getDebugLoc();
Mon P Wang63307c32008-05-05 19:05:59 +000011708 MachineOperand& destOper = bInstr->getOperand(0);
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000011709 MachineOperand* argOpers[2 + X86::AddrNumOperands];
Mon P Wang63307c32008-05-05 19:05:59 +000011710 int numArgs = bInstr->getNumOperands() - 1;
11711 for (int i=0; i < numArgs; ++i)
11712 argOpers[i] = &bInstr->getOperand(i+1);
11713
11714 // x86 address has 4 operands: base, index, scale, and displacement
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000011715 int lastAddrIndx = X86::AddrNumOperands - 1; // [0,3]
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000011716 int valArgIndx = lastAddrIndx + 1;
Scott Michelfdc40a02009-02-17 22:15:04 +000011717
Dale Johannesen140be2d2008-08-19 18:47:28 +000011718 unsigned t1 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +000011719 MachineInstrBuilder MIB = BuildMI(newMBB, dl, TII->get(LoadOpc), t1);
Mon P Wang63307c32008-05-05 19:05:59 +000011720 for (int i=0; i <= lastAddrIndx; ++i)
11721 (*MIB).addOperand(*argOpers[i]);
Andrew Lenharth507a58a2008-06-14 05:48:15 +000011722
Dale Johannesen140be2d2008-08-19 18:47:28 +000011723 unsigned t2 = F->getRegInfo().createVirtualRegister(RC);
Dan Gohmand735b802008-10-03 15:45:36 +000011724 assert((argOpers[valArgIndx]->isReg() ||
11725 argOpers[valArgIndx]->isImm()) &&
Dan Gohman014278e2008-09-13 17:58:21 +000011726 "invalid operand");
Dan Gohmand735b802008-10-03 15:45:36 +000011727 if (argOpers[valArgIndx]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +000011728 MIB = BuildMI(newMBB, dl, TII->get(regOpc), t2);
Mon P Wang63307c32008-05-05 19:05:59 +000011729 else
Dale Johannesene4d209d2009-02-03 20:21:25 +000011730 MIB = BuildMI(newMBB, dl, TII->get(immOpc), t2);
Richard Smith42fc29e2012-04-13 22:47:00 +000011731 MIB.addReg(t1);
Mon P Wang63307c32008-05-05 19:05:59 +000011732 (*MIB).addOperand(*argOpers[valArgIndx]);
Andrew Lenharth507a58a2008-06-14 05:48:15 +000011733
Richard Smith42fc29e2012-04-13 22:47:00 +000011734 unsigned t3 = F->getRegInfo().createVirtualRegister(RC);
11735 if (Invert) {
11736 MIB = BuildMI(newMBB, dl, TII->get(notOpc), t3).addReg(t2);
11737 }
11738 else
11739 t3 = t2;
11740
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011741 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), EAXreg);
Richard Smith2c651fe2012-04-16 18:43:53 +000011742 MIB.addReg(t1);
Scott Michelfdc40a02009-02-17 22:15:04 +000011743
Dale Johannesene4d209d2009-02-03 20:21:25 +000011744 MIB = BuildMI(newMBB, dl, TII->get(CXchgOpc));
Mon P Wang63307c32008-05-05 19:05:59 +000011745 for (int i=0; i <= lastAddrIndx; ++i)
11746 (*MIB).addOperand(*argOpers[i]);
Richard Smith42fc29e2012-04-13 22:47:00 +000011747 MIB.addReg(t3);
Mon P Wangf5952662008-07-17 04:54:06 +000011748 assert(bInstr->hasOneMemOperand() && "Unexpected number of memoperand");
Dan Gohmanc76909a2009-09-25 20:36:54 +000011749 (*MIB).setMemRefs(bInstr->memoperands_begin(),
11750 bInstr->memoperands_end());
Mon P Wangf5952662008-07-17 04:54:06 +000011751
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011752 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), destOper.getReg());
Dale Johannesen140be2d2008-08-19 18:47:28 +000011753 MIB.addReg(EAXreg);
Scott Michelfdc40a02009-02-17 22:15:04 +000011754
Mon P Wang63307c32008-05-05 19:05:59 +000011755 // insert branch
Chris Lattnerbd13fb62010-02-11 19:25:55 +000011756 BuildMI(newMBB, dl, TII->get(X86::JNE_4)).addMBB(newMBB);
Mon P Wang63307c32008-05-05 19:05:59 +000011757
Dan Gohman14152b42010-07-06 20:24:04 +000011758 bInstr->eraseFromParent(); // The pseudo instruction is gone now.
Mon P Wang63307c32008-05-05 19:05:59 +000011759 return nextMBB;
11760}
11761
Dale Johannesen1b54c7f2008-10-03 19:41:08 +000011762// private utility function: 64 bit atomics on 32 bit host.
Mon P Wang63307c32008-05-05 19:05:59 +000011763MachineBasicBlock *
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011764X86TargetLowering::EmitAtomicBit6432WithCustomInserter(MachineInstr *bInstr,
11765 MachineBasicBlock *MBB,
11766 unsigned regOpcL,
11767 unsigned regOpcH,
11768 unsigned immOpcL,
11769 unsigned immOpcH,
Richard Smith42fc29e2012-04-13 22:47:00 +000011770 bool Invert) const {
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011771 // For the atomic bitwise operator, we generate
11772 // thisMBB (instructions are in pairs, except cmpxchg8b)
11773 // ld t1,t2 = [bitinstr.addr]
11774 // newMBB:
11775 // out1, out2 = phi (thisMBB, t1/t2) (newMBB, t3/t4)
11776 // op t5, t6 <- out1, out2, [bitinstr.val]
Dale Johannesen880ae362008-10-03 22:25:52 +000011777 // (for SWAP, substitute: mov t5, t6 <- [bitinstr.val])
Richard Smith42fc29e2012-04-13 22:47:00 +000011778 // neg t7, t8 < t5, t6 (if Invert)
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011779 // mov ECX, EBX <- t5, t6
11780 // mov EAX, EDX <- t1, t2
11781 // cmpxchg8b [bitinstr.addr] [EAX, EDX, EBX, ECX implicit]
11782 // mov t3, t4 <- EAX, EDX
11783 // bz newMBB
11784 // result in out1, out2
11785 // fallthrough -->nextMBB
11786
Craig Topperc9099502012-04-20 06:31:50 +000011787 const TargetRegisterClass *RC = &X86::GR32RegClass;
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011788 const unsigned LoadOpc = X86::MOV32rm;
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011789 const unsigned NotOpc = X86::NOT32r;
11790 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
11791 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
11792 MachineFunction::iterator MBBIter = MBB;
11793 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +000011794
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011795 /// First build the CFG
11796 MachineFunction *F = MBB->getParent();
11797 MachineBasicBlock *thisMBB = MBB;
11798 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
11799 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
11800 F->insert(MBBIter, newMBB);
11801 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011802
Dan Gohman14152b42010-07-06 20:24:04 +000011803 // Transfer the remainder of thisMBB and its successor edges to nextMBB.
11804 nextMBB->splice(nextMBB->begin(), thisMBB,
11805 llvm::next(MachineBasicBlock::iterator(bInstr)),
11806 thisMBB->end());
11807 nextMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011808
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011809 // Update thisMBB to fall through to newMBB
11810 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011811
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011812 // newMBB jumps to itself and fall through to nextMBB
11813 newMBB->addSuccessor(nextMBB);
11814 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011815
Dale Johannesene4d209d2009-02-03 20:21:25 +000011816 DebugLoc dl = bInstr->getDebugLoc();
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011817 // Insert instructions into newMBB based on incoming instruction
11818 // There are 8 "real" operands plus 9 implicit def/uses, ignored here.
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000011819 assert(bInstr->getNumOperands() < X86::AddrNumOperands + 14 &&
Bill Wendling51b16f42009-05-30 01:09:53 +000011820 "unexpected number of operands");
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011821 MachineOperand& dest1Oper = bInstr->getOperand(0);
11822 MachineOperand& dest2Oper = bInstr->getOperand(1);
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000011823 MachineOperand* argOpers[2 + X86::AddrNumOperands];
11824 for (int i=0; i < 2 + X86::AddrNumOperands; ++i) {
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011825 argOpers[i] = &bInstr->getOperand(i+2);
11826
Dan Gohman71ea4e52010-05-14 21:01:44 +000011827 // We use some of the operands multiple times, so conservatively just
11828 // clear any kill flags that might be present.
11829 if (argOpers[i]->isReg() && argOpers[i]->isUse())
11830 argOpers[i]->setIsKill(false);
11831 }
11832
Evan Chengad5b52f2010-01-08 19:14:57 +000011833 // x86 address has 5 operands: base, index, scale, displacement, and segment.
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000011834 int lastAddrIndx = X86::AddrNumOperands - 1; // [0,3]
Scott Michelfdc40a02009-02-17 22:15:04 +000011835
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011836 unsigned t1 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +000011837 MachineInstrBuilder MIB = BuildMI(thisMBB, dl, TII->get(LoadOpc), t1);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011838 for (int i=0; i <= lastAddrIndx; ++i)
11839 (*MIB).addOperand(*argOpers[i]);
11840 unsigned t2 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +000011841 MIB = BuildMI(thisMBB, dl, TII->get(LoadOpc), t2);
Dale Johannesen880ae362008-10-03 22:25:52 +000011842 // add 4 to displacement.
Rafael Espindola094fad32009-04-08 21:14:34 +000011843 for (int i=0; i <= lastAddrIndx-2; ++i)
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011844 (*MIB).addOperand(*argOpers[i]);
Dale Johannesen880ae362008-10-03 22:25:52 +000011845 MachineOperand newOp3 = *(argOpers[3]);
11846 if (newOp3.isImm())
11847 newOp3.setImm(newOp3.getImm()+4);
11848 else
11849 newOp3.setOffset(newOp3.getOffset()+4);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011850 (*MIB).addOperand(newOp3);
Rafael Espindola094fad32009-04-08 21:14:34 +000011851 (*MIB).addOperand(*argOpers[lastAddrIndx]);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011852
11853 // t3/4 are defined later, at the bottom of the loop
11854 unsigned t3 = F->getRegInfo().createVirtualRegister(RC);
11855 unsigned t4 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +000011856 BuildMI(newMBB, dl, TII->get(X86::PHI), dest1Oper.getReg())
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011857 .addReg(t1).addMBB(thisMBB).addReg(t3).addMBB(newMBB);
Dale Johannesene4d209d2009-02-03 20:21:25 +000011858 BuildMI(newMBB, dl, TII->get(X86::PHI), dest2Oper.getReg())
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011859 .addReg(t2).addMBB(thisMBB).addReg(t4).addMBB(newMBB);
11860
Evan Cheng306b4ca2010-01-08 23:41:50 +000011861 // The subsequent operations should be using the destination registers of
Richard Smith42fc29e2012-04-13 22:47:00 +000011862 // the PHI instructions.
11863 t1 = dest1Oper.getReg();
11864 t2 = dest2Oper.getReg();
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011865
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000011866 int valArgIndx = lastAddrIndx + 1;
11867 assert((argOpers[valArgIndx]->isReg() ||
Bill Wendling51b16f42009-05-30 01:09:53 +000011868 argOpers[valArgIndx]->isImm()) &&
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011869 "invalid operand");
11870 unsigned t5 = F->getRegInfo().createVirtualRegister(RC);
11871 unsigned t6 = F->getRegInfo().createVirtualRegister(RC);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000011872 if (argOpers[valArgIndx]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +000011873 MIB = BuildMI(newMBB, dl, TII->get(regOpcL), t5);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011874 else
Dale Johannesene4d209d2009-02-03 20:21:25 +000011875 MIB = BuildMI(newMBB, dl, TII->get(immOpcL), t5);
Dale Johannesen880ae362008-10-03 22:25:52 +000011876 if (regOpcL != X86::MOV32rr)
Evan Cheng306b4ca2010-01-08 23:41:50 +000011877 MIB.addReg(t1);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000011878 (*MIB).addOperand(*argOpers[valArgIndx]);
11879 assert(argOpers[valArgIndx + 1]->isReg() ==
Bill Wendling51b16f42009-05-30 01:09:53 +000011880 argOpers[valArgIndx]->isReg());
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000011881 assert(argOpers[valArgIndx + 1]->isImm() ==
Bill Wendling51b16f42009-05-30 01:09:53 +000011882 argOpers[valArgIndx]->isImm());
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000011883 if (argOpers[valArgIndx + 1]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +000011884 MIB = BuildMI(newMBB, dl, TII->get(regOpcH), t6);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011885 else
Dale Johannesene4d209d2009-02-03 20:21:25 +000011886 MIB = BuildMI(newMBB, dl, TII->get(immOpcH), t6);
Dale Johannesen880ae362008-10-03 22:25:52 +000011887 if (regOpcH != X86::MOV32rr)
Evan Cheng306b4ca2010-01-08 23:41:50 +000011888 MIB.addReg(t2);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000011889 (*MIB).addOperand(*argOpers[valArgIndx + 1]);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011890
Richard Smith42fc29e2012-04-13 22:47:00 +000011891 unsigned t7, t8;
11892 if (Invert) {
11893 t7 = F->getRegInfo().createVirtualRegister(RC);
11894 t8 = F->getRegInfo().createVirtualRegister(RC);
11895 MIB = BuildMI(newMBB, dl, TII->get(NotOpc), t7).addReg(t5);
11896 MIB = BuildMI(newMBB, dl, TII->get(NotOpc), t8).addReg(t6);
11897 } else {
11898 t7 = t5;
11899 t8 = t6;
11900 }
11901
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011902 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::EAX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011903 MIB.addReg(t1);
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011904 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::EDX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011905 MIB.addReg(t2);
11906
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011907 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::EBX);
Richard Smith42fc29e2012-04-13 22:47:00 +000011908 MIB.addReg(t7);
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011909 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::ECX);
Richard Smith42fc29e2012-04-13 22:47:00 +000011910 MIB.addReg(t8);
Scott Michelfdc40a02009-02-17 22:15:04 +000011911
Dale Johannesene4d209d2009-02-03 20:21:25 +000011912 MIB = BuildMI(newMBB, dl, TII->get(X86::LCMPXCHG8B));
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011913 for (int i=0; i <= lastAddrIndx; ++i)
11914 (*MIB).addOperand(*argOpers[i]);
11915
11916 assert(bInstr->hasOneMemOperand() && "Unexpected number of memoperand");
Dan Gohmanc76909a2009-09-25 20:36:54 +000011917 (*MIB).setMemRefs(bInstr->memoperands_begin(),
11918 bInstr->memoperands_end());
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011919
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011920 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), t3);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011921 MIB.addReg(X86::EAX);
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011922 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), t4);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011923 MIB.addReg(X86::EDX);
Scott Michelfdc40a02009-02-17 22:15:04 +000011924
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011925 // insert branch
Chris Lattnerbd13fb62010-02-11 19:25:55 +000011926 BuildMI(newMBB, dl, TII->get(X86::JNE_4)).addMBB(newMBB);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011927
Dan Gohman14152b42010-07-06 20:24:04 +000011928 bInstr->eraseFromParent(); // The pseudo instruction is gone now.
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011929 return nextMBB;
11930}
11931
11932// private utility function
11933MachineBasicBlock *
Mon P Wang63307c32008-05-05 19:05:59 +000011934X86TargetLowering::EmitAtomicMinMaxWithCustomInserter(MachineInstr *mInstr,
11935 MachineBasicBlock *MBB,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +000011936 unsigned cmovOpc) const {
Mon P Wang63307c32008-05-05 19:05:59 +000011937 // For the atomic min/max operator, we generate
11938 // thisMBB:
11939 // newMBB:
Mon P Wangab3e7472008-05-05 22:56:23 +000011940 // ld t1 = [min/max.addr]
Scott Michelfdc40a02009-02-17 22:15:04 +000011941 // mov t2 = [min/max.val]
Mon P Wang63307c32008-05-05 19:05:59 +000011942 // cmp t1, t2
11943 // cmov[cond] t2 = t1
Mon P Wangab3e7472008-05-05 22:56:23 +000011944 // mov EAX = t1
Mon P Wang63307c32008-05-05 19:05:59 +000011945 // lcs dest = [bitinstr.addr], t2 [EAX is implicit]
11946 // bz newMBB
11947 // fallthrough -->nextMBB
11948 //
11949 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
11950 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +000011951 MachineFunction::iterator MBBIter = MBB;
Mon P Wang63307c32008-05-05 19:05:59 +000011952 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +000011953
Mon P Wang63307c32008-05-05 19:05:59 +000011954 /// First build the CFG
11955 MachineFunction *F = MBB->getParent();
11956 MachineBasicBlock *thisMBB = MBB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +000011957 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
11958 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
11959 F->insert(MBBIter, newMBB);
11960 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011961
Dan Gohman14152b42010-07-06 20:24:04 +000011962 // Transfer the remainder of thisMBB and its successor edges to nextMBB.
11963 nextMBB->splice(nextMBB->begin(), thisMBB,
11964 llvm::next(MachineBasicBlock::iterator(mInstr)),
11965 thisMBB->end());
11966 nextMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011967
Mon P Wang63307c32008-05-05 19:05:59 +000011968 // Update thisMBB to fall through to newMBB
11969 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011970
Mon P Wang63307c32008-05-05 19:05:59 +000011971 // newMBB jumps to newMBB and fall through to nextMBB
11972 newMBB->addSuccessor(nextMBB);
11973 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011974
Dale Johannesene4d209d2009-02-03 20:21:25 +000011975 DebugLoc dl = mInstr->getDebugLoc();
Mon P Wang63307c32008-05-05 19:05:59 +000011976 // Insert instructions into newMBB based on incoming instruction
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000011977 assert(mInstr->getNumOperands() < X86::AddrNumOperands + 4 &&
Bill Wendling51b16f42009-05-30 01:09:53 +000011978 "unexpected number of operands");
Mon P Wang63307c32008-05-05 19:05:59 +000011979 MachineOperand& destOper = mInstr->getOperand(0);
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000011980 MachineOperand* argOpers[2 + X86::AddrNumOperands];
Mon P Wang63307c32008-05-05 19:05:59 +000011981 int numArgs = mInstr->getNumOperands() - 1;
11982 for (int i=0; i < numArgs; ++i)
11983 argOpers[i] = &mInstr->getOperand(i+1);
Scott Michelfdc40a02009-02-17 22:15:04 +000011984
Mon P Wang63307c32008-05-05 19:05:59 +000011985 // x86 address has 4 operands: base, index, scale, and displacement
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000011986 int lastAddrIndx = X86::AddrNumOperands - 1; // [0,3]
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000011987 int valArgIndx = lastAddrIndx + 1;
Scott Michelfdc40a02009-02-17 22:15:04 +000011988
Craig Topperc9099502012-04-20 06:31:50 +000011989 unsigned t1 = F->getRegInfo().createVirtualRegister(&X86::GR32RegClass);
Dale Johannesene4d209d2009-02-03 20:21:25 +000011990 MachineInstrBuilder MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rm), t1);
Mon P Wang63307c32008-05-05 19:05:59 +000011991 for (int i=0; i <= lastAddrIndx; ++i)
11992 (*MIB).addOperand(*argOpers[i]);
Mon P Wangab3e7472008-05-05 22:56:23 +000011993
Mon P Wang63307c32008-05-05 19:05:59 +000011994 // We only support register and immediate values
Dan Gohmand735b802008-10-03 15:45:36 +000011995 assert((argOpers[valArgIndx]->isReg() ||
11996 argOpers[valArgIndx]->isImm()) &&
Dan Gohman014278e2008-09-13 17:58:21 +000011997 "invalid operand");
Scott Michelfdc40a02009-02-17 22:15:04 +000011998
Craig Topperc9099502012-04-20 06:31:50 +000011999 unsigned t2 = F->getRegInfo().createVirtualRegister(&X86::GR32RegClass);
Dan Gohmand735b802008-10-03 15:45:36 +000012000 if (argOpers[valArgIndx]->isReg())
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012001 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), t2);
Scott Michelfdc40a02009-02-17 22:15:04 +000012002 else
Dale Johannesene4d209d2009-02-03 20:21:25 +000012003 MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rr), t2);
Mon P Wang63307c32008-05-05 19:05:59 +000012004 (*MIB).addOperand(*argOpers[valArgIndx]);
12005
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012006 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::EAX);
Mon P Wangab3e7472008-05-05 22:56:23 +000012007 MIB.addReg(t1);
12008
Dale Johannesene4d209d2009-02-03 20:21:25 +000012009 MIB = BuildMI(newMBB, dl, TII->get(X86::CMP32rr));
Mon P Wang63307c32008-05-05 19:05:59 +000012010 MIB.addReg(t1);
12011 MIB.addReg(t2);
12012
12013 // Generate movc
Craig Topperc9099502012-04-20 06:31:50 +000012014 unsigned t3 = F->getRegInfo().createVirtualRegister(&X86::GR32RegClass);
Dale Johannesene4d209d2009-02-03 20:21:25 +000012015 MIB = BuildMI(newMBB, dl, TII->get(cmovOpc),t3);
Mon P Wang63307c32008-05-05 19:05:59 +000012016 MIB.addReg(t2);
12017 MIB.addReg(t1);
12018
12019 // Cmp and exchange if none has modified the memory location
Dale Johannesene4d209d2009-02-03 20:21:25 +000012020 MIB = BuildMI(newMBB, dl, TII->get(X86::LCMPXCHG32));
Mon P Wang63307c32008-05-05 19:05:59 +000012021 for (int i=0; i <= lastAddrIndx; ++i)
12022 (*MIB).addOperand(*argOpers[i]);
12023 MIB.addReg(t3);
Mon P Wangf5952662008-07-17 04:54:06 +000012024 assert(mInstr->hasOneMemOperand() && "Unexpected number of memoperand");
Dan Gohmanc76909a2009-09-25 20:36:54 +000012025 (*MIB).setMemRefs(mInstr->memoperands_begin(),
12026 mInstr->memoperands_end());
Scott Michelfdc40a02009-02-17 22:15:04 +000012027
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012028 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), destOper.getReg());
Mon P Wang63307c32008-05-05 19:05:59 +000012029 MIB.addReg(X86::EAX);
Scott Michelfdc40a02009-02-17 22:15:04 +000012030
Mon P Wang63307c32008-05-05 19:05:59 +000012031 // insert branch
Chris Lattnerbd13fb62010-02-11 19:25:55 +000012032 BuildMI(newMBB, dl, TII->get(X86::JNE_4)).addMBB(newMBB);
Mon P Wang63307c32008-05-05 19:05:59 +000012033
Dan Gohman14152b42010-07-06 20:24:04 +000012034 mInstr->eraseFromParent(); // The pseudo instruction is gone now.
Mon P Wang63307c32008-05-05 19:05:59 +000012035 return nextMBB;
12036}
12037
Eric Christopherf83a5de2009-08-27 18:08:16 +000012038// FIXME: When we get size specific XMM0 registers, i.e. XMM0_V16I8
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000012039// or XMM0_V32I8 in AVX all of this code can be replaced with that
12040// in the .td file.
Dan Gohmand6708ea2009-08-15 01:38:56 +000012041MachineBasicBlock *
Eric Christopherb120ab42009-08-18 22:50:32 +000012042X86TargetLowering::EmitPCMP(MachineInstr *MI, MachineBasicBlock *BB,
Daniel Dunbara279bc32009-09-20 02:20:51 +000012043 unsigned numArgs, bool memArg) const {
Craig Topperd0a31172012-01-10 06:37:29 +000012044 assert(Subtarget->hasSSE42() &&
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000012045 "Target must have SSE4.2 or AVX features enabled");
12046
Eric Christopherb120ab42009-08-18 22:50:32 +000012047 DebugLoc dl = MI->getDebugLoc();
12048 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Eric Christopherb120ab42009-08-18 22:50:32 +000012049 unsigned Opc;
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000012050 if (!Subtarget->hasAVX()) {
12051 if (memArg)
12052 Opc = numArgs == 3 ? X86::PCMPISTRM128rm : X86::PCMPESTRM128rm;
12053 else
12054 Opc = numArgs == 3 ? X86::PCMPISTRM128rr : X86::PCMPESTRM128rr;
12055 } else {
12056 if (memArg)
12057 Opc = numArgs == 3 ? X86::VPCMPISTRM128rm : X86::VPCMPESTRM128rm;
12058 else
12059 Opc = numArgs == 3 ? X86::VPCMPISTRM128rr : X86::VPCMPESTRM128rr;
12060 }
Eric Christopherb120ab42009-08-18 22:50:32 +000012061
Eric Christopher41c902f2010-11-30 08:20:21 +000012062 MachineInstrBuilder MIB = BuildMI(*BB, MI, dl, TII->get(Opc));
Eric Christopherb120ab42009-08-18 22:50:32 +000012063 for (unsigned i = 0; i < numArgs; ++i) {
12064 MachineOperand &Op = MI->getOperand(i+1);
Eric Christopherb120ab42009-08-18 22:50:32 +000012065 if (!(Op.isReg() && Op.isImplicit()))
12066 MIB.addOperand(Op);
12067 }
Bruno Cardoso Lopes5affa512011-08-31 03:04:09 +000012068 BuildMI(*BB, MI, dl,
Craig Topper638aa682012-08-05 00:17:48 +000012069 TII->get(TargetOpcode::COPY), MI->getOperand(0).getReg())
Eric Christopherb120ab42009-08-18 22:50:32 +000012070 .addReg(X86::XMM0);
12071
Dan Gohman14152b42010-07-06 20:24:04 +000012072 MI->eraseFromParent();
Eric Christopherb120ab42009-08-18 22:50:32 +000012073 return BB;
12074}
12075
12076MachineBasicBlock *
Eric Christopher228232b2010-11-30 07:20:12 +000012077X86TargetLowering::EmitMonitor(MachineInstr *MI, MachineBasicBlock *BB) const {
Eric Christopher228232b2010-11-30 07:20:12 +000012078 DebugLoc dl = MI->getDebugLoc();
12079 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000012080
Eric Christopher228232b2010-11-30 07:20:12 +000012081 // Address into RAX/EAX, other two args into ECX, EDX.
12082 unsigned MemOpc = Subtarget->is64Bit() ? X86::LEA64r : X86::LEA32r;
12083 unsigned MemReg = Subtarget->is64Bit() ? X86::RAX : X86::EAX;
12084 MachineInstrBuilder MIB = BuildMI(*BB, MI, dl, TII->get(MemOpc), MemReg);
12085 for (int i = 0; i < X86::AddrNumOperands; ++i)
Eric Christopher82be2202010-11-30 08:10:28 +000012086 MIB.addOperand(MI->getOperand(i));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000012087
Eric Christopher228232b2010-11-30 07:20:12 +000012088 unsigned ValOps = X86::AddrNumOperands;
12089 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::ECX)
12090 .addReg(MI->getOperand(ValOps).getReg());
12091 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::EDX)
12092 .addReg(MI->getOperand(ValOps+1).getReg());
12093
12094 // The instruction doesn't actually take any operands though.
12095 BuildMI(*BB, MI, dl, TII->get(X86::MONITORrrr));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000012096
Eric Christopher228232b2010-11-30 07:20:12 +000012097 MI->eraseFromParent(); // The pseudo is gone now.
12098 return BB;
12099}
12100
12101MachineBasicBlock *
Dan Gohman320afb82010-10-12 18:00:49 +000012102X86TargetLowering::EmitVAARG64WithCustomInserter(
12103 MachineInstr *MI,
12104 MachineBasicBlock *MBB) const {
12105 // Emit va_arg instruction on X86-64.
12106
12107 // Operands to this pseudo-instruction:
12108 // 0 ) Output : destination address (reg)
12109 // 1-5) Input : va_list address (addr, i64mem)
12110 // 6 ) ArgSize : Size (in bytes) of vararg type
12111 // 7 ) ArgMode : 0=overflow only, 1=use gp_offset, 2=use fp_offset
12112 // 8 ) Align : Alignment of type
12113 // 9 ) EFLAGS (implicit-def)
12114
12115 assert(MI->getNumOperands() == 10 && "VAARG_64 should have 10 operands!");
12116 assert(X86::AddrNumOperands == 5 && "VAARG_64 assumes 5 address operands");
12117
12118 unsigned DestReg = MI->getOperand(0).getReg();
12119 MachineOperand &Base = MI->getOperand(1);
12120 MachineOperand &Scale = MI->getOperand(2);
12121 MachineOperand &Index = MI->getOperand(3);
12122 MachineOperand &Disp = MI->getOperand(4);
12123 MachineOperand &Segment = MI->getOperand(5);
12124 unsigned ArgSize = MI->getOperand(6).getImm();
12125 unsigned ArgMode = MI->getOperand(7).getImm();
12126 unsigned Align = MI->getOperand(8).getImm();
12127
12128 // Memory Reference
12129 assert(MI->hasOneMemOperand() && "Expected VAARG_64 to have one memoperand");
12130 MachineInstr::mmo_iterator MMOBegin = MI->memoperands_begin();
12131 MachineInstr::mmo_iterator MMOEnd = MI->memoperands_end();
12132
12133 // Machine Information
12134 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
12135 MachineRegisterInfo &MRI = MBB->getParent()->getRegInfo();
12136 const TargetRegisterClass *AddrRegClass = getRegClassFor(MVT::i64);
12137 const TargetRegisterClass *OffsetRegClass = getRegClassFor(MVT::i32);
12138 DebugLoc DL = MI->getDebugLoc();
12139
12140 // struct va_list {
12141 // i32 gp_offset
12142 // i32 fp_offset
12143 // i64 overflow_area (address)
12144 // i64 reg_save_area (address)
12145 // }
12146 // sizeof(va_list) = 24
12147 // alignment(va_list) = 8
12148
12149 unsigned TotalNumIntRegs = 6;
12150 unsigned TotalNumXMMRegs = 8;
12151 bool UseGPOffset = (ArgMode == 1);
12152 bool UseFPOffset = (ArgMode == 2);
12153 unsigned MaxOffset = TotalNumIntRegs * 8 +
12154 (UseFPOffset ? TotalNumXMMRegs * 16 : 0);
12155
12156 /* Align ArgSize to a multiple of 8 */
12157 unsigned ArgSizeA8 = (ArgSize + 7) & ~7;
12158 bool NeedsAlign = (Align > 8);
12159
12160 MachineBasicBlock *thisMBB = MBB;
12161 MachineBasicBlock *overflowMBB;
12162 MachineBasicBlock *offsetMBB;
12163 MachineBasicBlock *endMBB;
12164
12165 unsigned OffsetDestReg = 0; // Argument address computed by offsetMBB
12166 unsigned OverflowDestReg = 0; // Argument address computed by overflowMBB
12167 unsigned OffsetReg = 0;
12168
12169 if (!UseGPOffset && !UseFPOffset) {
12170 // If we only pull from the overflow region, we don't create a branch.
12171 // We don't need to alter control flow.
12172 OffsetDestReg = 0; // unused
12173 OverflowDestReg = DestReg;
12174
12175 offsetMBB = NULL;
12176 overflowMBB = thisMBB;
12177 endMBB = thisMBB;
12178 } else {
12179 // First emit code to check if gp_offset (or fp_offset) is below the bound.
12180 // If so, pull the argument from reg_save_area. (branch to offsetMBB)
12181 // If not, pull from overflow_area. (branch to overflowMBB)
12182 //
12183 // thisMBB
12184 // | .
12185 // | .
12186 // offsetMBB overflowMBB
12187 // | .
12188 // | .
12189 // endMBB
12190
12191 // Registers for the PHI in endMBB
12192 OffsetDestReg = MRI.createVirtualRegister(AddrRegClass);
12193 OverflowDestReg = MRI.createVirtualRegister(AddrRegClass);
12194
12195 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
12196 MachineFunction *MF = MBB->getParent();
12197 overflowMBB = MF->CreateMachineBasicBlock(LLVM_BB);
12198 offsetMBB = MF->CreateMachineBasicBlock(LLVM_BB);
12199 endMBB = MF->CreateMachineBasicBlock(LLVM_BB);
12200
12201 MachineFunction::iterator MBBIter = MBB;
12202 ++MBBIter;
12203
12204 // Insert the new basic blocks
12205 MF->insert(MBBIter, offsetMBB);
12206 MF->insert(MBBIter, overflowMBB);
12207 MF->insert(MBBIter, endMBB);
12208
12209 // Transfer the remainder of MBB and its successor edges to endMBB.
12210 endMBB->splice(endMBB->begin(), thisMBB,
12211 llvm::next(MachineBasicBlock::iterator(MI)),
12212 thisMBB->end());
12213 endMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
12214
12215 // Make offsetMBB and overflowMBB successors of thisMBB
12216 thisMBB->addSuccessor(offsetMBB);
12217 thisMBB->addSuccessor(overflowMBB);
12218
12219 // endMBB is a successor of both offsetMBB and overflowMBB
12220 offsetMBB->addSuccessor(endMBB);
12221 overflowMBB->addSuccessor(endMBB);
12222
12223 // Load the offset value into a register
12224 OffsetReg = MRI.createVirtualRegister(OffsetRegClass);
12225 BuildMI(thisMBB, DL, TII->get(X86::MOV32rm), OffsetReg)
12226 .addOperand(Base)
12227 .addOperand(Scale)
12228 .addOperand(Index)
12229 .addDisp(Disp, UseFPOffset ? 4 : 0)
12230 .addOperand(Segment)
12231 .setMemRefs(MMOBegin, MMOEnd);
12232
12233 // Check if there is enough room left to pull this argument.
12234 BuildMI(thisMBB, DL, TII->get(X86::CMP32ri))
12235 .addReg(OffsetReg)
12236 .addImm(MaxOffset + 8 - ArgSizeA8);
12237
12238 // Branch to "overflowMBB" if offset >= max
12239 // Fall through to "offsetMBB" otherwise
12240 BuildMI(thisMBB, DL, TII->get(X86::GetCondBranchFromCond(X86::COND_AE)))
12241 .addMBB(overflowMBB);
12242 }
12243
12244 // In offsetMBB, emit code to use the reg_save_area.
12245 if (offsetMBB) {
12246 assert(OffsetReg != 0);
12247
12248 // Read the reg_save_area address.
12249 unsigned RegSaveReg = MRI.createVirtualRegister(AddrRegClass);
12250 BuildMI(offsetMBB, DL, TII->get(X86::MOV64rm), RegSaveReg)
12251 .addOperand(Base)
12252 .addOperand(Scale)
12253 .addOperand(Index)
12254 .addDisp(Disp, 16)
12255 .addOperand(Segment)
12256 .setMemRefs(MMOBegin, MMOEnd);
12257
12258 // Zero-extend the offset
12259 unsigned OffsetReg64 = MRI.createVirtualRegister(AddrRegClass);
12260 BuildMI(offsetMBB, DL, TII->get(X86::SUBREG_TO_REG), OffsetReg64)
12261 .addImm(0)
12262 .addReg(OffsetReg)
12263 .addImm(X86::sub_32bit);
12264
12265 // Add the offset to the reg_save_area to get the final address.
12266 BuildMI(offsetMBB, DL, TII->get(X86::ADD64rr), OffsetDestReg)
12267 .addReg(OffsetReg64)
12268 .addReg(RegSaveReg);
12269
12270 // Compute the offset for the next argument
12271 unsigned NextOffsetReg = MRI.createVirtualRegister(OffsetRegClass);
12272 BuildMI(offsetMBB, DL, TII->get(X86::ADD32ri), NextOffsetReg)
12273 .addReg(OffsetReg)
12274 .addImm(UseFPOffset ? 16 : 8);
12275
12276 // Store it back into the va_list.
12277 BuildMI(offsetMBB, DL, TII->get(X86::MOV32mr))
12278 .addOperand(Base)
12279 .addOperand(Scale)
12280 .addOperand(Index)
12281 .addDisp(Disp, UseFPOffset ? 4 : 0)
12282 .addOperand(Segment)
12283 .addReg(NextOffsetReg)
12284 .setMemRefs(MMOBegin, MMOEnd);
12285
12286 // Jump to endMBB
12287 BuildMI(offsetMBB, DL, TII->get(X86::JMP_4))
12288 .addMBB(endMBB);
12289 }
12290
12291 //
12292 // Emit code to use overflow area
12293 //
12294
12295 // Load the overflow_area address into a register.
12296 unsigned OverflowAddrReg = MRI.createVirtualRegister(AddrRegClass);
12297 BuildMI(overflowMBB, DL, TII->get(X86::MOV64rm), OverflowAddrReg)
12298 .addOperand(Base)
12299 .addOperand(Scale)
12300 .addOperand(Index)
12301 .addDisp(Disp, 8)
12302 .addOperand(Segment)
12303 .setMemRefs(MMOBegin, MMOEnd);
12304
12305 // If we need to align it, do so. Otherwise, just copy the address
12306 // to OverflowDestReg.
12307 if (NeedsAlign) {
12308 // Align the overflow address
12309 assert((Align & (Align-1)) == 0 && "Alignment must be a power of 2");
12310 unsigned TmpReg = MRI.createVirtualRegister(AddrRegClass);
12311
12312 // aligned_addr = (addr + (align-1)) & ~(align-1)
12313 BuildMI(overflowMBB, DL, TII->get(X86::ADD64ri32), TmpReg)
12314 .addReg(OverflowAddrReg)
12315 .addImm(Align-1);
12316
12317 BuildMI(overflowMBB, DL, TII->get(X86::AND64ri32), OverflowDestReg)
12318 .addReg(TmpReg)
12319 .addImm(~(uint64_t)(Align-1));
12320 } else {
12321 BuildMI(overflowMBB, DL, TII->get(TargetOpcode::COPY), OverflowDestReg)
12322 .addReg(OverflowAddrReg);
12323 }
12324
12325 // Compute the next overflow address after this argument.
12326 // (the overflow address should be kept 8-byte aligned)
12327 unsigned NextAddrReg = MRI.createVirtualRegister(AddrRegClass);
12328 BuildMI(overflowMBB, DL, TII->get(X86::ADD64ri32), NextAddrReg)
12329 .addReg(OverflowDestReg)
12330 .addImm(ArgSizeA8);
12331
12332 // Store the new overflow address.
12333 BuildMI(overflowMBB, DL, TII->get(X86::MOV64mr))
12334 .addOperand(Base)
12335 .addOperand(Scale)
12336 .addOperand(Index)
12337 .addDisp(Disp, 8)
12338 .addOperand(Segment)
12339 .addReg(NextAddrReg)
12340 .setMemRefs(MMOBegin, MMOEnd);
12341
12342 // If we branched, emit the PHI to the front of endMBB.
12343 if (offsetMBB) {
12344 BuildMI(*endMBB, endMBB->begin(), DL,
12345 TII->get(X86::PHI), DestReg)
12346 .addReg(OffsetDestReg).addMBB(offsetMBB)
12347 .addReg(OverflowDestReg).addMBB(overflowMBB);
12348 }
12349
12350 // Erase the pseudo instruction
12351 MI->eraseFromParent();
12352
12353 return endMBB;
12354}
12355
12356MachineBasicBlock *
Dan Gohmand6708ea2009-08-15 01:38:56 +000012357X86TargetLowering::EmitVAStartSaveXMMRegsWithCustomInserter(
12358 MachineInstr *MI,
12359 MachineBasicBlock *MBB) const {
12360 // Emit code to save XMM registers to the stack. The ABI says that the
12361 // number of registers to save is given in %al, so it's theoretically
12362 // possible to do an indirect jump trick to avoid saving all of them,
12363 // however this code takes a simpler approach and just executes all
12364 // of the stores if %al is non-zero. It's less code, and it's probably
12365 // easier on the hardware branch predictor, and stores aren't all that
12366 // expensive anyway.
12367
12368 // Create the new basic blocks. One block contains all the XMM stores,
12369 // and one block is the final destination regardless of whether any
12370 // stores were performed.
12371 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
12372 MachineFunction *F = MBB->getParent();
12373 MachineFunction::iterator MBBIter = MBB;
12374 ++MBBIter;
12375 MachineBasicBlock *XMMSaveMBB = F->CreateMachineBasicBlock(LLVM_BB);
12376 MachineBasicBlock *EndMBB = F->CreateMachineBasicBlock(LLVM_BB);
12377 F->insert(MBBIter, XMMSaveMBB);
12378 F->insert(MBBIter, EndMBB);
12379
Dan Gohman14152b42010-07-06 20:24:04 +000012380 // Transfer the remainder of MBB and its successor edges to EndMBB.
12381 EndMBB->splice(EndMBB->begin(), MBB,
12382 llvm::next(MachineBasicBlock::iterator(MI)),
12383 MBB->end());
12384 EndMBB->transferSuccessorsAndUpdatePHIs(MBB);
12385
Dan Gohmand6708ea2009-08-15 01:38:56 +000012386 // The original block will now fall through to the XMM save block.
12387 MBB->addSuccessor(XMMSaveMBB);
12388 // The XMMSaveMBB will fall through to the end block.
12389 XMMSaveMBB->addSuccessor(EndMBB);
12390
12391 // Now add the instructions.
12392 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
12393 DebugLoc DL = MI->getDebugLoc();
12394
12395 unsigned CountReg = MI->getOperand(0).getReg();
12396 int64_t RegSaveFrameIndex = MI->getOperand(1).getImm();
12397 int64_t VarArgsFPOffset = MI->getOperand(2).getImm();
12398
12399 if (!Subtarget->isTargetWin64()) {
12400 // If %al is 0, branch around the XMM save block.
12401 BuildMI(MBB, DL, TII->get(X86::TEST8rr)).addReg(CountReg).addReg(CountReg);
Chris Lattnerbd13fb62010-02-11 19:25:55 +000012402 BuildMI(MBB, DL, TII->get(X86::JE_4)).addMBB(EndMBB);
Dan Gohmand6708ea2009-08-15 01:38:56 +000012403 MBB->addSuccessor(EndMBB);
12404 }
12405
Bruno Cardoso Lopes5affa512011-08-31 03:04:09 +000012406 unsigned MOVOpc = Subtarget->hasAVX() ? X86::VMOVAPSmr : X86::MOVAPSmr;
Dan Gohmand6708ea2009-08-15 01:38:56 +000012407 // In the XMM save block, save all the XMM argument registers.
12408 for (int i = 3, e = MI->getNumOperands(); i != e; ++i) {
12409 int64_t Offset = (i - 3) * 16 + VarArgsFPOffset;
Dan Gohmanc76909a2009-09-25 20:36:54 +000012410 MachineMemOperand *MMO =
Evan Chengff89dcb2009-10-18 18:16:27 +000012411 F->getMachineMemOperand(
Chris Lattnere8639032010-09-21 06:22:23 +000012412 MachinePointerInfo::getFixedStack(RegSaveFrameIndex, Offset),
Chris Lattner59db5492010-09-21 04:39:43 +000012413 MachineMemOperand::MOStore,
Evan Chengff89dcb2009-10-18 18:16:27 +000012414 /*Size=*/16, /*Align=*/16);
Bruno Cardoso Lopes5affa512011-08-31 03:04:09 +000012415 BuildMI(XMMSaveMBB, DL, TII->get(MOVOpc))
Dan Gohmand6708ea2009-08-15 01:38:56 +000012416 .addFrameIndex(RegSaveFrameIndex)
12417 .addImm(/*Scale=*/1)
12418 .addReg(/*IndexReg=*/0)
12419 .addImm(/*Disp=*/Offset)
12420 .addReg(/*Segment=*/0)
12421 .addReg(MI->getOperand(i).getReg())
Dan Gohmanc76909a2009-09-25 20:36:54 +000012422 .addMemOperand(MMO);
Dan Gohmand6708ea2009-08-15 01:38:56 +000012423 }
12424
Dan Gohman14152b42010-07-06 20:24:04 +000012425 MI->eraseFromParent(); // The pseudo instruction is gone now.
Dan Gohmand6708ea2009-08-15 01:38:56 +000012426
12427 return EndMBB;
12428}
Mon P Wang63307c32008-05-05 19:05:59 +000012429
Lang Hames6e3f7e42012-02-03 01:13:49 +000012430// The EFLAGS operand of SelectItr might be missing a kill marker
12431// because there were multiple uses of EFLAGS, and ISel didn't know
12432// which to mark. Figure out whether SelectItr should have had a
12433// kill marker, and set it if it should. Returns the correct kill
12434// marker value.
12435static bool checkAndUpdateEFLAGSKill(MachineBasicBlock::iterator SelectItr,
12436 MachineBasicBlock* BB,
12437 const TargetRegisterInfo* TRI) {
12438 // Scan forward through BB for a use/def of EFLAGS.
12439 MachineBasicBlock::iterator miI(llvm::next(SelectItr));
12440 for (MachineBasicBlock::iterator miE = BB->end(); miI != miE; ++miI) {
Lang Hames50a36f72012-02-02 07:48:37 +000012441 const MachineInstr& mi = *miI;
Lang Hames6e3f7e42012-02-03 01:13:49 +000012442 if (mi.readsRegister(X86::EFLAGS))
Lang Hames50a36f72012-02-02 07:48:37 +000012443 return false;
Lang Hames6e3f7e42012-02-03 01:13:49 +000012444 if (mi.definesRegister(X86::EFLAGS))
12445 break; // Should have kill-flag - update below.
12446 }
12447
12448 // If we hit the end of the block, check whether EFLAGS is live into a
12449 // successor.
12450 if (miI == BB->end()) {
12451 for (MachineBasicBlock::succ_iterator sItr = BB->succ_begin(),
12452 sEnd = BB->succ_end();
12453 sItr != sEnd; ++sItr) {
12454 MachineBasicBlock* succ = *sItr;
12455 if (succ->isLiveIn(X86::EFLAGS))
12456 return false;
Lang Hames50a36f72012-02-02 07:48:37 +000012457 }
12458 }
12459
Lang Hames6e3f7e42012-02-03 01:13:49 +000012460 // We found a def, or hit the end of the basic block and EFLAGS wasn't live
12461 // out. SelectMI should have a kill flag on EFLAGS.
12462 SelectItr->addRegisterKilled(X86::EFLAGS, TRI);
Lang Hames50a36f72012-02-02 07:48:37 +000012463 return true;
12464}
12465
Evan Cheng60c07e12006-07-05 22:17:51 +000012466MachineBasicBlock *
Chris Lattner52600972009-09-02 05:57:00 +000012467X86TargetLowering::EmitLoweredSelect(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000012468 MachineBasicBlock *BB) const {
Chris Lattner52600972009-09-02 05:57:00 +000012469 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
12470 DebugLoc DL = MI->getDebugLoc();
Daniel Dunbara279bc32009-09-20 02:20:51 +000012471
Chris Lattner52600972009-09-02 05:57:00 +000012472 // To "insert" a SELECT_CC instruction, we actually have to insert the
12473 // diamond control-flow pattern. The incoming instruction knows the
12474 // destination vreg to set, the condition code register to branch on, the
12475 // true/false values to select between, and a branch opcode to use.
12476 const BasicBlock *LLVM_BB = BB->getBasicBlock();
12477 MachineFunction::iterator It = BB;
12478 ++It;
Daniel Dunbara279bc32009-09-20 02:20:51 +000012479
Chris Lattner52600972009-09-02 05:57:00 +000012480 // thisMBB:
12481 // ...
12482 // TrueVal = ...
12483 // cmpTY ccX, r1, r2
12484 // bCC copy1MBB
12485 // fallthrough --> copy0MBB
12486 MachineBasicBlock *thisMBB = BB;
12487 MachineFunction *F = BB->getParent();
12488 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
12489 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Chris Lattner52600972009-09-02 05:57:00 +000012490 F->insert(It, copy0MBB);
12491 F->insert(It, sinkMBB);
Bill Wendling730c07e2010-06-25 20:48:10 +000012492
Bill Wendling730c07e2010-06-25 20:48:10 +000012493 // If the EFLAGS register isn't dead in the terminator, then claim that it's
12494 // live into the sink and copy blocks.
Lang Hames6e3f7e42012-02-03 01:13:49 +000012495 const TargetRegisterInfo* TRI = getTargetMachine().getRegisterInfo();
12496 if (!MI->killsRegister(X86::EFLAGS) &&
12497 !checkAndUpdateEFLAGSKill(MI, BB, TRI)) {
12498 copy0MBB->addLiveIn(X86::EFLAGS);
12499 sinkMBB->addLiveIn(X86::EFLAGS);
Bill Wendling730c07e2010-06-25 20:48:10 +000012500 }
12501
Dan Gohman14152b42010-07-06 20:24:04 +000012502 // Transfer the remainder of BB and its successor edges to sinkMBB.
12503 sinkMBB->splice(sinkMBB->begin(), BB,
12504 llvm::next(MachineBasicBlock::iterator(MI)),
12505 BB->end());
12506 sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
12507
12508 // Add the true and fallthrough blocks as its successors.
12509 BB->addSuccessor(copy0MBB);
12510 BB->addSuccessor(sinkMBB);
12511
12512 // Create the conditional branch instruction.
12513 unsigned Opc =
12514 X86::GetCondBranchFromCond((X86::CondCode)MI->getOperand(3).getImm());
12515 BuildMI(BB, DL, TII->get(Opc)).addMBB(sinkMBB);
12516
Chris Lattner52600972009-09-02 05:57:00 +000012517 // copy0MBB:
12518 // %FalseValue = ...
12519 // # fallthrough to sinkMBB
Dan Gohman3335a222010-04-30 20:14:26 +000012520 copy0MBB->addSuccessor(sinkMBB);
Daniel Dunbara279bc32009-09-20 02:20:51 +000012521
Chris Lattner52600972009-09-02 05:57:00 +000012522 // sinkMBB:
12523 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
12524 // ...
Dan Gohman14152b42010-07-06 20:24:04 +000012525 BuildMI(*sinkMBB, sinkMBB->begin(), DL,
12526 TII->get(X86::PHI), MI->getOperand(0).getReg())
Chris Lattner52600972009-09-02 05:57:00 +000012527 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
12528 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
12529
Dan Gohman14152b42010-07-06 20:24:04 +000012530 MI->eraseFromParent(); // The pseudo instruction is gone now.
Dan Gohman3335a222010-04-30 20:14:26 +000012531 return sinkMBB;
Chris Lattner52600972009-09-02 05:57:00 +000012532}
12533
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000012534MachineBasicBlock *
Rafael Espindola151ab3e2011-08-30 19:47:04 +000012535X86TargetLowering::EmitLoweredSegAlloca(MachineInstr *MI, MachineBasicBlock *BB,
12536 bool Is64Bit) const {
12537 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
12538 DebugLoc DL = MI->getDebugLoc();
12539 MachineFunction *MF = BB->getParent();
12540 const BasicBlock *LLVM_BB = BB->getBasicBlock();
12541
Nick Lewycky8a8d4792011-12-02 22:16:29 +000012542 assert(getTargetMachine().Options.EnableSegmentedStacks);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000012543
12544 unsigned TlsReg = Is64Bit ? X86::FS : X86::GS;
12545 unsigned TlsOffset = Is64Bit ? 0x70 : 0x30;
12546
12547 // BB:
12548 // ... [Till the alloca]
12549 // If stacklet is not large enough, jump to mallocMBB
12550 //
12551 // bumpMBB:
12552 // Allocate by subtracting from RSP
12553 // Jump to continueMBB
12554 //
12555 // mallocMBB:
12556 // Allocate by call to runtime
12557 //
12558 // continueMBB:
12559 // ...
12560 // [rest of original BB]
12561 //
12562
12563 MachineBasicBlock *mallocMBB = MF->CreateMachineBasicBlock(LLVM_BB);
12564 MachineBasicBlock *bumpMBB = MF->CreateMachineBasicBlock(LLVM_BB);
12565 MachineBasicBlock *continueMBB = MF->CreateMachineBasicBlock(LLVM_BB);
12566
12567 MachineRegisterInfo &MRI = MF->getRegInfo();
12568 const TargetRegisterClass *AddrRegClass =
12569 getRegClassFor(Is64Bit ? MVT::i64:MVT::i32);
12570
12571 unsigned mallocPtrVReg = MRI.createVirtualRegister(AddrRegClass),
12572 bumpSPPtrVReg = MRI.createVirtualRegister(AddrRegClass),
12573 tmpSPVReg = MRI.createVirtualRegister(AddrRegClass),
Rafael Espindola66bf7432011-10-26 21:16:41 +000012574 SPLimitVReg = MRI.createVirtualRegister(AddrRegClass),
Rafael Espindola151ab3e2011-08-30 19:47:04 +000012575 sizeVReg = MI->getOperand(1).getReg(),
12576 physSPReg = Is64Bit ? X86::RSP : X86::ESP;
12577
12578 MachineFunction::iterator MBBIter = BB;
12579 ++MBBIter;
12580
12581 MF->insert(MBBIter, bumpMBB);
12582 MF->insert(MBBIter, mallocMBB);
12583 MF->insert(MBBIter, continueMBB);
12584
12585 continueMBB->splice(continueMBB->begin(), BB, llvm::next
12586 (MachineBasicBlock::iterator(MI)), BB->end());
12587 continueMBB->transferSuccessorsAndUpdatePHIs(BB);
12588
12589 // Add code to the main basic block to check if the stack limit has been hit,
12590 // and if so, jump to mallocMBB otherwise to bumpMBB.
12591 BuildMI(BB, DL, TII->get(TargetOpcode::COPY), tmpSPVReg).addReg(physSPReg);
Rafael Espindola66bf7432011-10-26 21:16:41 +000012592 BuildMI(BB, DL, TII->get(Is64Bit ? X86::SUB64rr:X86::SUB32rr), SPLimitVReg)
Rafael Espindola151ab3e2011-08-30 19:47:04 +000012593 .addReg(tmpSPVReg).addReg(sizeVReg);
12594 BuildMI(BB, DL, TII->get(Is64Bit ? X86::CMP64mr:X86::CMP32mr))
Rafael Espindola014f7a32012-01-11 18:14:03 +000012595 .addReg(0).addImm(1).addReg(0).addImm(TlsOffset).addReg(TlsReg)
Rafael Espindola66bf7432011-10-26 21:16:41 +000012596 .addReg(SPLimitVReg);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000012597 BuildMI(BB, DL, TII->get(X86::JG_4)).addMBB(mallocMBB);
12598
12599 // bumpMBB simply decreases the stack pointer, since we know the current
12600 // stacklet has enough space.
12601 BuildMI(bumpMBB, DL, TII->get(TargetOpcode::COPY), physSPReg)
Rafael Espindola66bf7432011-10-26 21:16:41 +000012602 .addReg(SPLimitVReg);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000012603 BuildMI(bumpMBB, DL, TII->get(TargetOpcode::COPY), bumpSPPtrVReg)
Rafael Espindola66bf7432011-10-26 21:16:41 +000012604 .addReg(SPLimitVReg);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000012605 BuildMI(bumpMBB, DL, TII->get(X86::JMP_4)).addMBB(continueMBB);
12606
12607 // Calls into a routine in libgcc to allocate more space from the heap.
Jakob Stoklund Olesen8bcde2a2012-02-16 00:02:50 +000012608 const uint32_t *RegMask =
12609 getTargetMachine().getRegisterInfo()->getCallPreservedMask(CallingConv::C);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000012610 if (Is64Bit) {
12611 BuildMI(mallocMBB, DL, TII->get(X86::MOV64rr), X86::RDI)
12612 .addReg(sizeVReg);
12613 BuildMI(mallocMBB, DL, TII->get(X86::CALL64pcrel32))
Jakob Stoklund Olesen85dccf12012-07-04 23:53:27 +000012614 .addExternalSymbol("__morestack_allocate_stack_space")
Jakob Stoklund Olesen8bcde2a2012-02-16 00:02:50 +000012615 .addRegMask(RegMask)
Jakob Stoklund Olesen85dccf12012-07-04 23:53:27 +000012616 .addReg(X86::RDI, RegState::Implicit)
Jakob Stoklund Olesen8bcde2a2012-02-16 00:02:50 +000012617 .addReg(X86::RAX, RegState::ImplicitDefine);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000012618 } else {
12619 BuildMI(mallocMBB, DL, TII->get(X86::SUB32ri), physSPReg).addReg(physSPReg)
12620 .addImm(12);
12621 BuildMI(mallocMBB, DL, TII->get(X86::PUSH32r)).addReg(sizeVReg);
12622 BuildMI(mallocMBB, DL, TII->get(X86::CALLpcrel32))
Jakob Stoklund Olesen8bcde2a2012-02-16 00:02:50 +000012623 .addExternalSymbol("__morestack_allocate_stack_space")
12624 .addRegMask(RegMask)
12625 .addReg(X86::EAX, RegState::ImplicitDefine);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000012626 }
12627
12628 if (!Is64Bit)
12629 BuildMI(mallocMBB, DL, TII->get(X86::ADD32ri), physSPReg).addReg(physSPReg)
12630 .addImm(16);
12631
12632 BuildMI(mallocMBB, DL, TII->get(TargetOpcode::COPY), mallocPtrVReg)
12633 .addReg(Is64Bit ? X86::RAX : X86::EAX);
12634 BuildMI(mallocMBB, DL, TII->get(X86::JMP_4)).addMBB(continueMBB);
12635
12636 // Set up the CFG correctly.
12637 BB->addSuccessor(bumpMBB);
12638 BB->addSuccessor(mallocMBB);
12639 mallocMBB->addSuccessor(continueMBB);
12640 bumpMBB->addSuccessor(continueMBB);
12641
12642 // Take care of the PHI nodes.
12643 BuildMI(*continueMBB, continueMBB->begin(), DL, TII->get(X86::PHI),
12644 MI->getOperand(0).getReg())
12645 .addReg(mallocPtrVReg).addMBB(mallocMBB)
12646 .addReg(bumpSPPtrVReg).addMBB(bumpMBB);
12647
12648 // Delete the original pseudo instruction.
12649 MI->eraseFromParent();
12650
12651 // And we're done.
12652 return continueMBB;
12653}
12654
12655MachineBasicBlock *
Michael J. Spencere9c253e2010-10-21 01:41:01 +000012656X86TargetLowering::EmitLoweredWinAlloca(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000012657 MachineBasicBlock *BB) const {
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000012658 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
12659 DebugLoc DL = MI->getDebugLoc();
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000012660
NAKAMURA Takumia2e07622011-03-24 07:07:00 +000012661 assert(!Subtarget->isTargetEnvMacho());
12662
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000012663 // The lowering is pretty easy: we're just emitting the call to _alloca. The
12664 // non-trivial part is impdef of ESP.
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000012665
NAKAMURA Takumia2e07622011-03-24 07:07:00 +000012666 if (Subtarget->isTargetWin64()) {
12667 if (Subtarget->isTargetCygMing()) {
12668 // ___chkstk(Mingw64):
12669 // Clobbers R10, R11, RAX and EFLAGS.
12670 // Updates RSP.
12671 BuildMI(*BB, MI, DL, TII->get(X86::W64ALLOCA))
12672 .addExternalSymbol("___chkstk")
12673 .addReg(X86::RAX, RegState::Implicit)
12674 .addReg(X86::RSP, RegState::Implicit)
12675 .addReg(X86::RAX, RegState::Define | RegState::Implicit)
12676 .addReg(X86::RSP, RegState::Define | RegState::Implicit)
12677 .addReg(X86::EFLAGS, RegState::Define | RegState::Implicit);
12678 } else {
12679 // __chkstk(MSVCRT): does not update stack pointer.
12680 // Clobbers R10, R11 and EFLAGS.
12681 // FIXME: RAX(allocated size) might be reused and not killed.
12682 BuildMI(*BB, MI, DL, TII->get(X86::W64ALLOCA))
12683 .addExternalSymbol("__chkstk")
12684 .addReg(X86::RAX, RegState::Implicit)
12685 .addReg(X86::EFLAGS, RegState::Define | RegState::Implicit);
12686 // RAX has the offset to subtracted from RSP.
12687 BuildMI(*BB, MI, DL, TII->get(X86::SUB64rr), X86::RSP)
12688 .addReg(X86::RSP)
12689 .addReg(X86::RAX);
12690 }
12691 } else {
12692 const char *StackProbeSymbol =
Michael J. Spencere9c253e2010-10-21 01:41:01 +000012693 Subtarget->isTargetWindows() ? "_chkstk" : "_alloca";
12694
NAKAMURA Takumia2e07622011-03-24 07:07:00 +000012695 BuildMI(*BB, MI, DL, TII->get(X86::CALLpcrel32))
12696 .addExternalSymbol(StackProbeSymbol)
12697 .addReg(X86::EAX, RegState::Implicit)
12698 .addReg(X86::ESP, RegState::Implicit)
12699 .addReg(X86::EAX, RegState::Define | RegState::Implicit)
12700 .addReg(X86::ESP, RegState::Define | RegState::Implicit)
12701 .addReg(X86::EFLAGS, RegState::Define | RegState::Implicit);
12702 }
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000012703
Dan Gohman14152b42010-07-06 20:24:04 +000012704 MI->eraseFromParent(); // The pseudo instruction is gone now.
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000012705 return BB;
12706}
Chris Lattner52600972009-09-02 05:57:00 +000012707
12708MachineBasicBlock *
Eric Christopher30ef0e52010-06-03 04:07:48 +000012709X86TargetLowering::EmitLoweredTLSCall(MachineInstr *MI,
12710 MachineBasicBlock *BB) const {
12711 // This is pretty easy. We're taking the value that we received from
12712 // our load from the relocation, sticking it in either RDI (x86-64)
12713 // or EAX and doing an indirect call. The return value will then
12714 // be in the normal return register.
Michael J. Spencerec38de22010-10-10 22:04:20 +000012715 const X86InstrInfo *TII
Eric Christopher54415362010-06-08 22:04:25 +000012716 = static_cast<const X86InstrInfo*>(getTargetMachine().getInstrInfo());
Eric Christopher30ef0e52010-06-03 04:07:48 +000012717 DebugLoc DL = MI->getDebugLoc();
12718 MachineFunction *F = BB->getParent();
Eric Christopher722d3152010-09-27 06:01:51 +000012719
12720 assert(Subtarget->isTargetDarwin() && "Darwin only instr emitted?");
Eric Christopher54415362010-06-08 22:04:25 +000012721 assert(MI->getOperand(3).isGlobal() && "This should be a global");
Michael J. Spencerec38de22010-10-10 22:04:20 +000012722
Jakob Stoklund Olesen8bcde2a2012-02-16 00:02:50 +000012723 // Get a register mask for the lowered call.
12724 // FIXME: The 32-bit calls have non-standard calling conventions. Use a
12725 // proper register mask.
12726 const uint32_t *RegMask =
12727 getTargetMachine().getRegisterInfo()->getCallPreservedMask(CallingConv::C);
Eric Christopher30ef0e52010-06-03 04:07:48 +000012728 if (Subtarget->is64Bit()) {
Dan Gohman14152b42010-07-06 20:24:04 +000012729 MachineInstrBuilder MIB = BuildMI(*BB, MI, DL,
12730 TII->get(X86::MOV64rm), X86::RDI)
Eric Christopher54415362010-06-08 22:04:25 +000012731 .addReg(X86::RIP)
12732 .addImm(0).addReg(0)
Michael J. Spencerec38de22010-10-10 22:04:20 +000012733 .addGlobalAddress(MI->getOperand(3).getGlobal(), 0,
Eric Christopher54415362010-06-08 22:04:25 +000012734 MI->getOperand(3).getTargetFlags())
12735 .addReg(0);
Eric Christopher722d3152010-09-27 06:01:51 +000012736 MIB = BuildMI(*BB, MI, DL, TII->get(X86::CALL64m));
Chris Lattner599b5312010-07-08 23:46:44 +000012737 addDirectMem(MIB, X86::RDI);
Jakob Stoklund Olesen8bcde2a2012-02-16 00:02:50 +000012738 MIB.addReg(X86::RAX, RegState::ImplicitDefine).addRegMask(RegMask);
Eric Christopher61025492010-06-15 23:08:42 +000012739 } else if (getTargetMachine().getRelocationModel() != Reloc::PIC_) {
Dan Gohman14152b42010-07-06 20:24:04 +000012740 MachineInstrBuilder MIB = BuildMI(*BB, MI, DL,
12741 TII->get(X86::MOV32rm), X86::EAX)
Eric Christopher61025492010-06-15 23:08:42 +000012742 .addReg(0)
12743 .addImm(0).addReg(0)
Michael J. Spencerec38de22010-10-10 22:04:20 +000012744 .addGlobalAddress(MI->getOperand(3).getGlobal(), 0,
Eric Christopher61025492010-06-15 23:08:42 +000012745 MI->getOperand(3).getTargetFlags())
12746 .addReg(0);
Dan Gohman14152b42010-07-06 20:24:04 +000012747 MIB = BuildMI(*BB, MI, DL, TII->get(X86::CALL32m));
Chris Lattner599b5312010-07-08 23:46:44 +000012748 addDirectMem(MIB, X86::EAX);
Jakob Stoklund Olesen8bcde2a2012-02-16 00:02:50 +000012749 MIB.addReg(X86::EAX, RegState::ImplicitDefine).addRegMask(RegMask);
Eric Christopher30ef0e52010-06-03 04:07:48 +000012750 } else {
Dan Gohman14152b42010-07-06 20:24:04 +000012751 MachineInstrBuilder MIB = BuildMI(*BB, MI, DL,
12752 TII->get(X86::MOV32rm), X86::EAX)
Eric Christopher54415362010-06-08 22:04:25 +000012753 .addReg(TII->getGlobalBaseReg(F))
12754 .addImm(0).addReg(0)
Michael J. Spencerec38de22010-10-10 22:04:20 +000012755 .addGlobalAddress(MI->getOperand(3).getGlobal(), 0,
Eric Christopher54415362010-06-08 22:04:25 +000012756 MI->getOperand(3).getTargetFlags())
12757 .addReg(0);
Dan Gohman14152b42010-07-06 20:24:04 +000012758 MIB = BuildMI(*BB, MI, DL, TII->get(X86::CALL32m));
Chris Lattner599b5312010-07-08 23:46:44 +000012759 addDirectMem(MIB, X86::EAX);
Jakob Stoklund Olesen8bcde2a2012-02-16 00:02:50 +000012760 MIB.addReg(X86::EAX, RegState::ImplicitDefine).addRegMask(RegMask);
Eric Christopher30ef0e52010-06-03 04:07:48 +000012761 }
Michael J. Spencerec38de22010-10-10 22:04:20 +000012762
Dan Gohman14152b42010-07-06 20:24:04 +000012763 MI->eraseFromParent(); // The pseudo instruction is gone now.
Eric Christopher30ef0e52010-06-03 04:07:48 +000012764 return BB;
12765}
12766
12767MachineBasicBlock *
Evan Chengff9b3732008-01-30 18:18:23 +000012768X86TargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000012769 MachineBasicBlock *BB) const {
Evan Cheng60c07e12006-07-05 22:17:51 +000012770 switch (MI->getOpcode()) {
Craig Topperabb94d02012-02-05 03:43:23 +000012771 default: llvm_unreachable("Unexpected instr type to insert");
NAKAMURA Takumi7754f852011-01-26 02:04:09 +000012772 case X86::TAILJMPd64:
12773 case X86::TAILJMPr64:
12774 case X86::TAILJMPm64:
Craig Topper6d1263a2012-02-05 05:38:58 +000012775 llvm_unreachable("TAILJMP64 would not be touched here.");
NAKAMURA Takumi7754f852011-01-26 02:04:09 +000012776 case X86::TCRETURNdi64:
12777 case X86::TCRETURNri64:
12778 case X86::TCRETURNmi64:
NAKAMURA Takumi7754f852011-01-26 02:04:09 +000012779 return BB;
Michael J. Spencere9c253e2010-10-21 01:41:01 +000012780 case X86::WIN_ALLOCA:
12781 return EmitLoweredWinAlloca(MI, BB);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000012782 case X86::SEG_ALLOCA_32:
12783 return EmitLoweredSegAlloca(MI, BB, false);
12784 case X86::SEG_ALLOCA_64:
12785 return EmitLoweredSegAlloca(MI, BB, true);
Eric Christopher30ef0e52010-06-03 04:07:48 +000012786 case X86::TLSCall_32:
12787 case X86::TLSCall_64:
12788 return EmitLoweredTLSCall(MI, BB);
Dan Gohmancbbea0f2009-08-27 00:14:12 +000012789 case X86::CMOV_GR8:
Evan Cheng60c07e12006-07-05 22:17:51 +000012790 case X86::CMOV_FR32:
12791 case X86::CMOV_FR64:
12792 case X86::CMOV_V4F32:
12793 case X86::CMOV_V2F64:
Chris Lattner52600972009-09-02 05:57:00 +000012794 case X86::CMOV_V2I64:
Bruno Cardoso Lopesd40aa242011-08-09 23:27:13 +000012795 case X86::CMOV_V8F32:
12796 case X86::CMOV_V4F64:
12797 case X86::CMOV_V4I64:
Chris Lattner314a1132010-03-14 18:31:44 +000012798 case X86::CMOV_GR16:
12799 case X86::CMOV_GR32:
12800 case X86::CMOV_RFP32:
12801 case X86::CMOV_RFP64:
12802 case X86::CMOV_RFP80:
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000012803 return EmitLoweredSelect(MI, BB);
Evan Cheng60c07e12006-07-05 22:17:51 +000012804
Dale Johannesen849f2142007-07-03 00:53:03 +000012805 case X86::FP32_TO_INT16_IN_MEM:
12806 case X86::FP32_TO_INT32_IN_MEM:
12807 case X86::FP32_TO_INT64_IN_MEM:
12808 case X86::FP64_TO_INT16_IN_MEM:
12809 case X86::FP64_TO_INT32_IN_MEM:
Dale Johannesena996d522007-08-07 01:17:37 +000012810 case X86::FP64_TO_INT64_IN_MEM:
12811 case X86::FP80_TO_INT16_IN_MEM:
12812 case X86::FP80_TO_INT32_IN_MEM:
12813 case X86::FP80_TO_INT64_IN_MEM: {
Chris Lattner52600972009-09-02 05:57:00 +000012814 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
12815 DebugLoc DL = MI->getDebugLoc();
12816
Evan Cheng60c07e12006-07-05 22:17:51 +000012817 // Change the floating point control register to use "round towards zero"
12818 // mode when truncating to an integer value.
12819 MachineFunction *F = BB->getParent();
David Greene3f2bf852009-11-12 20:49:22 +000012820 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2, false);
Dan Gohman14152b42010-07-06 20:24:04 +000012821 addFrameReference(BuildMI(*BB, MI, DL,
12822 TII->get(X86::FNSTCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000012823
12824 // Load the old value of the high byte of the control word...
12825 unsigned OldCW =
Craig Topperc9099502012-04-20 06:31:50 +000012826 F->getRegInfo().createVirtualRegister(&X86::GR16RegClass);
Dan Gohman14152b42010-07-06 20:24:04 +000012827 addFrameReference(BuildMI(*BB, MI, DL, TII->get(X86::MOV16rm), OldCW),
Dale Johannesene4d209d2009-02-03 20:21:25 +000012828 CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000012829
12830 // Set the high part to be round to zero...
Dan Gohman14152b42010-07-06 20:24:04 +000012831 addFrameReference(BuildMI(*BB, MI, DL, TII->get(X86::MOV16mi)), CWFrameIdx)
Evan Chengc0f64ff2006-11-27 23:37:22 +000012832 .addImm(0xC7F);
Evan Cheng60c07e12006-07-05 22:17:51 +000012833
12834 // Reload the modified control word now...
Dan Gohman14152b42010-07-06 20:24:04 +000012835 addFrameReference(BuildMI(*BB, MI, DL,
12836 TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000012837
12838 // Restore the memory image of control word to original value
Dan Gohman14152b42010-07-06 20:24:04 +000012839 addFrameReference(BuildMI(*BB, MI, DL, TII->get(X86::MOV16mr)), CWFrameIdx)
Evan Chengc0f64ff2006-11-27 23:37:22 +000012840 .addReg(OldCW);
Evan Cheng60c07e12006-07-05 22:17:51 +000012841
12842 // Get the X86 opcode to use.
12843 unsigned Opc;
12844 switch (MI->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +000012845 default: llvm_unreachable("illegal opcode!");
Dale Johannesene377d4d2007-07-04 21:07:47 +000012846 case X86::FP32_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m32; break;
12847 case X86::FP32_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m32; break;
12848 case X86::FP32_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m32; break;
12849 case X86::FP64_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m64; break;
12850 case X86::FP64_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m64; break;
12851 case X86::FP64_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m64; break;
Dale Johannesena996d522007-08-07 01:17:37 +000012852 case X86::FP80_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m80; break;
12853 case X86::FP80_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m80; break;
12854 case X86::FP80_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m80; break;
Evan Cheng60c07e12006-07-05 22:17:51 +000012855 }
12856
12857 X86AddressMode AM;
12858 MachineOperand &Op = MI->getOperand(0);
Dan Gohmand735b802008-10-03 15:45:36 +000012859 if (Op.isReg()) {
Evan Cheng60c07e12006-07-05 22:17:51 +000012860 AM.BaseType = X86AddressMode::RegBase;
12861 AM.Base.Reg = Op.getReg();
12862 } else {
12863 AM.BaseType = X86AddressMode::FrameIndexBase;
Chris Lattner8aa797a2007-12-30 23:10:15 +000012864 AM.Base.FrameIndex = Op.getIndex();
Evan Cheng60c07e12006-07-05 22:17:51 +000012865 }
12866 Op = MI->getOperand(1);
Dan Gohmand735b802008-10-03 15:45:36 +000012867 if (Op.isImm())
Chris Lattner7fbe9722006-10-20 17:42:20 +000012868 AM.Scale = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +000012869 Op = MI->getOperand(2);
Dan Gohmand735b802008-10-03 15:45:36 +000012870 if (Op.isImm())
Chris Lattner7fbe9722006-10-20 17:42:20 +000012871 AM.IndexReg = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +000012872 Op = MI->getOperand(3);
Dan Gohmand735b802008-10-03 15:45:36 +000012873 if (Op.isGlobal()) {
Evan Cheng60c07e12006-07-05 22:17:51 +000012874 AM.GV = Op.getGlobal();
12875 } else {
Chris Lattner7fbe9722006-10-20 17:42:20 +000012876 AM.Disp = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +000012877 }
Dan Gohman14152b42010-07-06 20:24:04 +000012878 addFullAddress(BuildMI(*BB, MI, DL, TII->get(Opc)), AM)
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000012879 .addReg(MI->getOperand(X86::AddrNumOperands).getReg());
Evan Cheng60c07e12006-07-05 22:17:51 +000012880
12881 // Reload the original control word now.
Dan Gohman14152b42010-07-06 20:24:04 +000012882 addFrameReference(BuildMI(*BB, MI, DL,
12883 TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000012884
Dan Gohman14152b42010-07-06 20:24:04 +000012885 MI->eraseFromParent(); // The pseudo instruction is gone now.
Evan Cheng60c07e12006-07-05 22:17:51 +000012886 return BB;
12887 }
Eric Christopherb120ab42009-08-18 22:50:32 +000012888 // String/text processing lowering.
12889 case X86::PCMPISTRM128REG:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000012890 case X86::VPCMPISTRM128REG:
Eric Christopherb120ab42009-08-18 22:50:32 +000012891 case X86::PCMPISTRM128MEM:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000012892 case X86::VPCMPISTRM128MEM:
Eric Christopherb120ab42009-08-18 22:50:32 +000012893 case X86::PCMPESTRM128REG:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000012894 case X86::VPCMPESTRM128REG:
Eric Christopherb120ab42009-08-18 22:50:32 +000012895 case X86::PCMPESTRM128MEM:
Craig Topper63a99ff2012-08-17 07:15:56 +000012896 case X86::VPCMPESTRM128MEM: {
12897 unsigned NumArgs;
12898 bool MemArg;
12899 switch (MI->getOpcode()) {
12900 default: llvm_unreachable("illegal opcode!");
12901 case X86::PCMPISTRM128REG:
12902 case X86::VPCMPISTRM128REG:
12903 NumArgs = 3; MemArg = false; break;
12904 case X86::PCMPISTRM128MEM:
12905 case X86::VPCMPISTRM128MEM:
12906 NumArgs = 3; MemArg = true; break;
12907 case X86::PCMPESTRM128REG:
12908 case X86::VPCMPESTRM128REG:
12909 NumArgs = 5; MemArg = false; break;
12910 case X86::PCMPESTRM128MEM:
12911 case X86::VPCMPESTRM128MEM:
12912 NumArgs = 5; MemArg = true; break;
12913 }
12914 return EmitPCMP(MI, BB, NumArgs, MemArg);
12915 }
Eric Christopherb120ab42009-08-18 22:50:32 +000012916
Eric Christopher228232b2010-11-30 07:20:12 +000012917 // Thread synchronization.
12918 case X86::MONITOR:
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000012919 return EmitMonitor(MI, BB);
Eric Christopher228232b2010-11-30 07:20:12 +000012920
Eric Christopherb120ab42009-08-18 22:50:32 +000012921 // Atomic Lowering.
Mon P Wang63307c32008-05-05 19:05:59 +000012922 case X86::ATOMAND32:
12923 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND32rr,
Scott Michelfdc40a02009-02-17 22:15:04 +000012924 X86::AND32ri, X86::MOV32rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012925 X86::LCMPXCHG32,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012926 X86::NOT32r, X86::EAX,
Craig Topperc9099502012-04-20 06:31:50 +000012927 &X86::GR32RegClass);
Mon P Wang63307c32008-05-05 19:05:59 +000012928 case X86::ATOMOR32:
Scott Michelfdc40a02009-02-17 22:15:04 +000012929 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR32rr,
12930 X86::OR32ri, X86::MOV32rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012931 X86::LCMPXCHG32,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012932 X86::NOT32r, X86::EAX,
Craig Topperc9099502012-04-20 06:31:50 +000012933 &X86::GR32RegClass);
Mon P Wang63307c32008-05-05 19:05:59 +000012934 case X86::ATOMXOR32:
12935 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR32rr,
Scott Michelfdc40a02009-02-17 22:15:04 +000012936 X86::XOR32ri, X86::MOV32rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012937 X86::LCMPXCHG32,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012938 X86::NOT32r, X86::EAX,
Craig Topperc9099502012-04-20 06:31:50 +000012939 &X86::GR32RegClass);
Andrew Lenharth507a58a2008-06-14 05:48:15 +000012940 case X86::ATOMNAND32:
12941 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND32rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012942 X86::AND32ri, X86::MOV32rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012943 X86::LCMPXCHG32,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012944 X86::NOT32r, X86::EAX,
Craig Topperc9099502012-04-20 06:31:50 +000012945 &X86::GR32RegClass, true);
Mon P Wang63307c32008-05-05 19:05:59 +000012946 case X86::ATOMMIN32:
12947 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL32rr);
12948 case X86::ATOMMAX32:
12949 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG32rr);
12950 case X86::ATOMUMIN32:
12951 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB32rr);
12952 case X86::ATOMUMAX32:
12953 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA32rr);
Dale Johannesen140be2d2008-08-19 18:47:28 +000012954
12955 case X86::ATOMAND16:
12956 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND16rr,
12957 X86::AND16ri, X86::MOV16rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012958 X86::LCMPXCHG16,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012959 X86::NOT16r, X86::AX,
Craig Topperc9099502012-04-20 06:31:50 +000012960 &X86::GR16RegClass);
Dale Johannesen140be2d2008-08-19 18:47:28 +000012961 case X86::ATOMOR16:
Scott Michelfdc40a02009-02-17 22:15:04 +000012962 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR16rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012963 X86::OR16ri, X86::MOV16rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012964 X86::LCMPXCHG16,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012965 X86::NOT16r, X86::AX,
Craig Topperc9099502012-04-20 06:31:50 +000012966 &X86::GR16RegClass);
Dale Johannesen140be2d2008-08-19 18:47:28 +000012967 case X86::ATOMXOR16:
12968 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR16rr,
12969 X86::XOR16ri, X86::MOV16rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012970 X86::LCMPXCHG16,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012971 X86::NOT16r, X86::AX,
Craig Topperc9099502012-04-20 06:31:50 +000012972 &X86::GR16RegClass);
Dale Johannesen140be2d2008-08-19 18:47:28 +000012973 case X86::ATOMNAND16:
12974 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND16rr,
12975 X86::AND16ri, X86::MOV16rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012976 X86::LCMPXCHG16,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012977 X86::NOT16r, X86::AX,
Craig Topperc9099502012-04-20 06:31:50 +000012978 &X86::GR16RegClass, true);
Dale Johannesen140be2d2008-08-19 18:47:28 +000012979 case X86::ATOMMIN16:
12980 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL16rr);
12981 case X86::ATOMMAX16:
12982 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG16rr);
12983 case X86::ATOMUMIN16:
12984 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB16rr);
12985 case X86::ATOMUMAX16:
12986 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA16rr);
12987
12988 case X86::ATOMAND8:
12989 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND8rr,
12990 X86::AND8ri, X86::MOV8rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012991 X86::LCMPXCHG8,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012992 X86::NOT8r, X86::AL,
Craig Topperc9099502012-04-20 06:31:50 +000012993 &X86::GR8RegClass);
Dale Johannesen140be2d2008-08-19 18:47:28 +000012994 case X86::ATOMOR8:
Scott Michelfdc40a02009-02-17 22:15:04 +000012995 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR8rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012996 X86::OR8ri, X86::MOV8rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012997 X86::LCMPXCHG8,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012998 X86::NOT8r, X86::AL,
Craig Topperc9099502012-04-20 06:31:50 +000012999 &X86::GR8RegClass);
Dale Johannesen140be2d2008-08-19 18:47:28 +000013000 case X86::ATOMXOR8:
13001 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR8rr,
13002 X86::XOR8ri, X86::MOV8rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000013003 X86::LCMPXCHG8,
Dale Johannesen140be2d2008-08-19 18:47:28 +000013004 X86::NOT8r, X86::AL,
Craig Topperc9099502012-04-20 06:31:50 +000013005 &X86::GR8RegClass);
Dale Johannesen140be2d2008-08-19 18:47:28 +000013006 case X86::ATOMNAND8:
13007 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND8rr,
13008 X86::AND8ri, X86::MOV8rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000013009 X86::LCMPXCHG8,
Dale Johannesen140be2d2008-08-19 18:47:28 +000013010 X86::NOT8r, X86::AL,
Craig Topperc9099502012-04-20 06:31:50 +000013011 &X86::GR8RegClass, true);
Dale Johannesen140be2d2008-08-19 18:47:28 +000013012 // FIXME: There are no CMOV8 instructions; MIN/MAX need some other way.
Dale Johannesen48c1bc22008-10-02 18:53:47 +000013013 // This group is for 64-bit host.
Dale Johannesena99e3842008-08-20 00:48:50 +000013014 case X86::ATOMAND64:
13015 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND64rr,
Scott Michelfdc40a02009-02-17 22:15:04 +000013016 X86::AND64ri32, X86::MOV64rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000013017 X86::LCMPXCHG64,
Dale Johannesena99e3842008-08-20 00:48:50 +000013018 X86::NOT64r, X86::RAX,
Craig Topperc9099502012-04-20 06:31:50 +000013019 &X86::GR64RegClass);
Dale Johannesena99e3842008-08-20 00:48:50 +000013020 case X86::ATOMOR64:
Scott Michelfdc40a02009-02-17 22:15:04 +000013021 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR64rr,
13022 X86::OR64ri32, X86::MOV64rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000013023 X86::LCMPXCHG64,
Dale Johannesena99e3842008-08-20 00:48:50 +000013024 X86::NOT64r, X86::RAX,
Craig Topperc9099502012-04-20 06:31:50 +000013025 &X86::GR64RegClass);
Dale Johannesena99e3842008-08-20 00:48:50 +000013026 case X86::ATOMXOR64:
13027 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR64rr,
Scott Michelfdc40a02009-02-17 22:15:04 +000013028 X86::XOR64ri32, X86::MOV64rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000013029 X86::LCMPXCHG64,
Dale Johannesena99e3842008-08-20 00:48:50 +000013030 X86::NOT64r, X86::RAX,
Craig Topperc9099502012-04-20 06:31:50 +000013031 &X86::GR64RegClass);
Dale Johannesena99e3842008-08-20 00:48:50 +000013032 case X86::ATOMNAND64:
13033 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND64rr,
13034 X86::AND64ri32, X86::MOV64rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000013035 X86::LCMPXCHG64,
Dale Johannesena99e3842008-08-20 00:48:50 +000013036 X86::NOT64r, X86::RAX,
Craig Topperc9099502012-04-20 06:31:50 +000013037 &X86::GR64RegClass, true);
Dale Johannesena99e3842008-08-20 00:48:50 +000013038 case X86::ATOMMIN64:
13039 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL64rr);
13040 case X86::ATOMMAX64:
13041 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG64rr);
13042 case X86::ATOMUMIN64:
13043 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB64rr);
13044 case X86::ATOMUMAX64:
13045 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA64rr);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000013046
13047 // This group does 64-bit operations on a 32-bit host.
13048 case X86::ATOMAND6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000013049 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000013050 X86::AND32rr, X86::AND32rr,
13051 X86::AND32ri, X86::AND32ri,
13052 false);
13053 case X86::ATOMOR6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000013054 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000013055 X86::OR32rr, X86::OR32rr,
13056 X86::OR32ri, X86::OR32ri,
13057 false);
13058 case X86::ATOMXOR6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000013059 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000013060 X86::XOR32rr, X86::XOR32rr,
13061 X86::XOR32ri, X86::XOR32ri,
13062 false);
13063 case X86::ATOMNAND6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000013064 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000013065 X86::AND32rr, X86::AND32rr,
13066 X86::AND32ri, X86::AND32ri,
13067 true);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000013068 case X86::ATOMADD6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000013069 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000013070 X86::ADD32rr, X86::ADC32rr,
13071 X86::ADD32ri, X86::ADC32ri,
13072 false);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000013073 case X86::ATOMSUB6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000013074 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000013075 X86::SUB32rr, X86::SBB32rr,
13076 X86::SUB32ri, X86::SBB32ri,
13077 false);
Dale Johannesen880ae362008-10-03 22:25:52 +000013078 case X86::ATOMSWAP6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000013079 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen880ae362008-10-03 22:25:52 +000013080 X86::MOV32rr, X86::MOV32rr,
13081 X86::MOV32ri, X86::MOV32ri,
13082 false);
Dan Gohmand6708ea2009-08-15 01:38:56 +000013083 case X86::VASTART_SAVE_XMM_REGS:
13084 return EmitVAStartSaveXMMRegsWithCustomInserter(MI, BB);
Dan Gohman320afb82010-10-12 18:00:49 +000013085
13086 case X86::VAARG_64:
13087 return EmitVAARG64WithCustomInserter(MI, BB);
Evan Cheng60c07e12006-07-05 22:17:51 +000013088 }
13089}
13090
13091//===----------------------------------------------------------------------===//
13092// X86 Optimization Hooks
13093//===----------------------------------------------------------------------===//
13094
Dan Gohman475871a2008-07-27 21:46:04 +000013095void X86TargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
Dan Gohmanfd29e0e2008-02-13 00:35:47 +000013096 APInt &KnownZero,
13097 APInt &KnownOne,
Dan Gohmanea859be2007-06-22 14:59:07 +000013098 const SelectionDAG &DAG,
Nate Begeman368e18d2006-02-16 21:11:51 +000013099 unsigned Depth) const {
Rafael Espindola26c8dcc2012-04-04 12:51:34 +000013100 unsigned BitWidth = KnownZero.getBitWidth();
Evan Cheng3a03ebb2005-12-21 23:05:39 +000013101 unsigned Opc = Op.getOpcode();
Evan Cheng865f0602006-04-05 06:11:20 +000013102 assert((Opc >= ISD::BUILTIN_OP_END ||
13103 Opc == ISD::INTRINSIC_WO_CHAIN ||
13104 Opc == ISD::INTRINSIC_W_CHAIN ||
13105 Opc == ISD::INTRINSIC_VOID) &&
13106 "Should use MaskedValueIsZero if you don't know whether Op"
13107 " is a target node!");
Evan Cheng3a03ebb2005-12-21 23:05:39 +000013108
Rafael Espindola26c8dcc2012-04-04 12:51:34 +000013109 KnownZero = KnownOne = APInt(BitWidth, 0); // Don't know anything.
Evan Cheng3a03ebb2005-12-21 23:05:39 +000013110 switch (Opc) {
Evan Cheng865f0602006-04-05 06:11:20 +000013111 default: break;
Evan Cheng97d0e0e2009-02-02 09:15:04 +000013112 case X86ISD::ADD:
13113 case X86ISD::SUB:
Chris Lattner5b856542010-12-20 00:59:46 +000013114 case X86ISD::ADC:
13115 case X86ISD::SBB:
Evan Cheng97d0e0e2009-02-02 09:15:04 +000013116 case X86ISD::SMUL:
13117 case X86ISD::UMUL:
Dan Gohman076aee32009-03-04 19:44:21 +000013118 case X86ISD::INC:
13119 case X86ISD::DEC:
Dan Gohmane220c4b2009-09-18 19:59:53 +000013120 case X86ISD::OR:
13121 case X86ISD::XOR:
13122 case X86ISD::AND:
Evan Cheng97d0e0e2009-02-02 09:15:04 +000013123 // These nodes' second result is a boolean.
13124 if (Op.getResNo() == 0)
13125 break;
13126 // Fallthrough
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000013127 case X86ISD::SETCC:
Rafael Espindola26c8dcc2012-04-04 12:51:34 +000013128 KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - 1);
Nate Begeman368e18d2006-02-16 21:11:51 +000013129 break;
Evan Cheng7c1780c2011-10-07 17:21:44 +000013130 case ISD::INTRINSIC_WO_CHAIN: {
13131 unsigned IntId = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
13132 unsigned NumLoBits = 0;
13133 switch (IntId) {
13134 default: break;
13135 case Intrinsic::x86_sse_movmsk_ps:
13136 case Intrinsic::x86_avx_movmsk_ps_256:
13137 case Intrinsic::x86_sse2_movmsk_pd:
13138 case Intrinsic::x86_avx_movmsk_pd_256:
13139 case Intrinsic::x86_mmx_pmovmskb:
Craig Topper3738ccd2011-12-27 06:27:23 +000013140 case Intrinsic::x86_sse2_pmovmskb_128:
13141 case Intrinsic::x86_avx2_pmovmskb: {
Evan Cheng7c1780c2011-10-07 17:21:44 +000013142 // High bits of movmskp{s|d}, pmovmskb are known zero.
13143 switch (IntId) {
Craig Topperabb94d02012-02-05 03:43:23 +000013144 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
Evan Cheng7c1780c2011-10-07 17:21:44 +000013145 case Intrinsic::x86_sse_movmsk_ps: NumLoBits = 4; break;
13146 case Intrinsic::x86_avx_movmsk_ps_256: NumLoBits = 8; break;
13147 case Intrinsic::x86_sse2_movmsk_pd: NumLoBits = 2; break;
13148 case Intrinsic::x86_avx_movmsk_pd_256: NumLoBits = 4; break;
13149 case Intrinsic::x86_mmx_pmovmskb: NumLoBits = 8; break;
13150 case Intrinsic::x86_sse2_pmovmskb_128: NumLoBits = 16; break;
Craig Topper3738ccd2011-12-27 06:27:23 +000013151 case Intrinsic::x86_avx2_pmovmskb: NumLoBits = 32; break;
Evan Cheng7c1780c2011-10-07 17:21:44 +000013152 }
Rafael Espindola26c8dcc2012-04-04 12:51:34 +000013153 KnownZero = APInt::getHighBitsSet(BitWidth, BitWidth - NumLoBits);
Evan Cheng7c1780c2011-10-07 17:21:44 +000013154 break;
13155 }
13156 }
13157 break;
13158 }
Evan Cheng3a03ebb2005-12-21 23:05:39 +000013159 }
Evan Cheng3a03ebb2005-12-21 23:05:39 +000013160}
Chris Lattner259e97c2006-01-31 19:43:35 +000013161
Owen Andersonbc146b02010-09-21 20:42:50 +000013162unsigned X86TargetLowering::ComputeNumSignBitsForTargetNode(SDValue Op,
13163 unsigned Depth) const {
13164 // SETCC_CARRY sets the dest to ~0 for true or 0 for false.
13165 if (Op.getOpcode() == X86ISD::SETCC_CARRY)
13166 return Op.getValueType().getScalarType().getSizeInBits();
Michael J. Spencerec38de22010-10-10 22:04:20 +000013167
Owen Andersonbc146b02010-09-21 20:42:50 +000013168 // Fallback case.
13169 return 1;
13170}
13171
Evan Cheng206ee9d2006-07-07 08:33:52 +000013172/// isGAPlusOffset - Returns true (and the GlobalValue and the offset) if the
Evan Chengad4196b2008-05-12 19:56:52 +000013173/// node is a GlobalAddress + offset.
13174bool X86TargetLowering::isGAPlusOffset(SDNode *N,
Dan Gohman46510a72010-04-15 01:51:59 +000013175 const GlobalValue* &GA,
13176 int64_t &Offset) const {
Evan Chengad4196b2008-05-12 19:56:52 +000013177 if (N->getOpcode() == X86ISD::Wrapper) {
13178 if (isa<GlobalAddressSDNode>(N->getOperand(0))) {
Evan Cheng206ee9d2006-07-07 08:33:52 +000013179 GA = cast<GlobalAddressSDNode>(N->getOperand(0))->getGlobal();
Dan Gohman6520e202008-10-18 02:06:02 +000013180 Offset = cast<GlobalAddressSDNode>(N->getOperand(0))->getOffset();
Evan Cheng206ee9d2006-07-07 08:33:52 +000013181 return true;
13182 }
Evan Cheng206ee9d2006-07-07 08:33:52 +000013183 }
Evan Chengad4196b2008-05-12 19:56:52 +000013184 return TargetLowering::isGAPlusOffset(N, GA, Offset);
Evan Cheng206ee9d2006-07-07 08:33:52 +000013185}
13186
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000013187/// isShuffleHigh128VectorInsertLow - Checks whether the shuffle node is the
13188/// same as extracting the high 128-bit part of 256-bit vector and then
13189/// inserting the result into the low part of a new 256-bit vector
13190static bool isShuffleHigh128VectorInsertLow(ShuffleVectorSDNode *SVOp) {
13191 EVT VT = SVOp->getValueType(0);
Craig Topper66ddd152012-04-27 22:54:43 +000013192 unsigned NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000013193
13194 // vector_shuffle <4, 5, 6, 7, u, u, u, u> or <2, 3, u, u>
Craig Topper66ddd152012-04-27 22:54:43 +000013195 for (unsigned i = 0, j = NumElems/2; i != NumElems/2; ++i, ++j)
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000013196 if (!isUndefOrEqual(SVOp->getMaskElt(i), j) ||
13197 SVOp->getMaskElt(j) >= 0)
13198 return false;
13199
13200 return true;
13201}
13202
13203/// isShuffleLow128VectorInsertHigh - Checks whether the shuffle node is the
13204/// same as extracting the low 128-bit part of 256-bit vector and then
13205/// inserting the result into the high part of a new 256-bit vector
13206static bool isShuffleLow128VectorInsertHigh(ShuffleVectorSDNode *SVOp) {
13207 EVT VT = SVOp->getValueType(0);
Craig Topper66ddd152012-04-27 22:54:43 +000013208 unsigned NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000013209
13210 // vector_shuffle <u, u, u, u, 0, 1, 2, 3> or <u, u, 0, 1>
Craig Topper66ddd152012-04-27 22:54:43 +000013211 for (unsigned i = NumElems/2, j = 0; i != NumElems; ++i, ++j)
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000013212 if (!isUndefOrEqual(SVOp->getMaskElt(i), j) ||
13213 SVOp->getMaskElt(j) >= 0)
13214 return false;
13215
13216 return true;
13217}
13218
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000013219/// PerformShuffleCombine256 - Performs shuffle combines for 256-bit vectors.
13220static SDValue PerformShuffleCombine256(SDNode *N, SelectionDAG &DAG,
Craig Topper12216172012-01-13 08:12:35 +000013221 TargetLowering::DAGCombinerInfo &DCI,
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +000013222 const X86Subtarget* Subtarget) {
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000013223 DebugLoc dl = N->getDebugLoc();
13224 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
13225 SDValue V1 = SVOp->getOperand(0);
13226 SDValue V2 = SVOp->getOperand(1);
13227 EVT VT = SVOp->getValueType(0);
Craig Topper66ddd152012-04-27 22:54:43 +000013228 unsigned NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000013229
13230 if (V1.getOpcode() == ISD::CONCAT_VECTORS &&
13231 V2.getOpcode() == ISD::CONCAT_VECTORS) {
13232 //
13233 // 0,0,0,...
Benjamin Kramer558cc5a2011-07-22 01:02:57 +000013234 // |
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000013235 // V UNDEF BUILD_VECTOR UNDEF
13236 // \ / \ /
13237 // CONCAT_VECTOR CONCAT_VECTOR
13238 // \ /
13239 // \ /
13240 // RESULT: V + zero extended
13241 //
13242 if (V2.getOperand(0).getOpcode() != ISD::BUILD_VECTOR ||
13243 V2.getOperand(1).getOpcode() != ISD::UNDEF ||
13244 V1.getOperand(1).getOpcode() != ISD::UNDEF)
13245 return SDValue();
13246
13247 if (!ISD::isBuildVectorAllZeros(V2.getOperand(0).getNode()))
13248 return SDValue();
13249
13250 // To match the shuffle mask, the first half of the mask should
13251 // be exactly the first vector, and all the rest a splat with the
13252 // first element of the second one.
Craig Topper66ddd152012-04-27 22:54:43 +000013253 for (unsigned i = 0; i != NumElems/2; ++i)
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000013254 if (!isUndefOrEqual(SVOp->getMaskElt(i), i) ||
13255 !isUndefOrEqual(SVOp->getMaskElt(i+NumElems/2), NumElems))
13256 return SDValue();
13257
Chad Rosier3d1161e2012-01-03 21:05:52 +000013258 // If V1 is coming from a vector load then just fold to a VZEXT_LOAD.
13259 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(V1.getOperand(0))) {
Chad Rosier42726832012-05-07 18:47:44 +000013260 if (Ld->hasNUsesOfValue(1, 0)) {
13261 SDVTList Tys = DAG.getVTList(MVT::v4i64, MVT::Other);
13262 SDValue Ops[] = { Ld->getChain(), Ld->getBasePtr() };
13263 SDValue ResNode =
13264 DAG.getMemIntrinsicNode(X86ISD::VZEXT_LOAD, dl, Tys, Ops, 2,
13265 Ld->getMemoryVT(),
13266 Ld->getPointerInfo(),
13267 Ld->getAlignment(),
13268 false/*isVolatile*/, true/*ReadMem*/,
13269 false/*WriteMem*/);
13270 return DAG.getNode(ISD::BITCAST, dl, VT, ResNode);
13271 }
Chad Rosiera20e1e72012-08-01 18:39:17 +000013272 }
Chad Rosier3d1161e2012-01-03 21:05:52 +000013273
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000013274 // Emit a zeroed vector and insert the desired subvector on its
13275 // first half.
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +000013276 SDValue Zeros = getZeroVector(VT, Subtarget, DAG, dl);
Craig Topperb14940a2012-04-22 20:55:18 +000013277 SDValue InsV = Insert128BitVector(Zeros, V1.getOperand(0), 0, DAG, dl);
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000013278 return DCI.CombineTo(N, InsV);
13279 }
13280
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000013281 //===--------------------------------------------------------------------===//
13282 // Combine some shuffles into subvector extracts and inserts:
13283 //
13284
13285 // vector_shuffle <4, 5, 6, 7, u, u, u, u> or <2, 3, u, u>
13286 if (isShuffleHigh128VectorInsertLow(SVOp)) {
Craig Topperb14940a2012-04-22 20:55:18 +000013287 SDValue V = Extract128BitVector(V1, NumElems/2, DAG, dl);
13288 SDValue InsV = Insert128BitVector(DAG.getUNDEF(VT), V, 0, DAG, dl);
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000013289 return DCI.CombineTo(N, InsV);
13290 }
13291
13292 // vector_shuffle <u, u, u, u, 0, 1, 2, 3> or <u, u, 0, 1>
13293 if (isShuffleLow128VectorInsertHigh(SVOp)) {
Craig Topperb14940a2012-04-22 20:55:18 +000013294 SDValue V = Extract128BitVector(V1, 0, DAG, dl);
13295 SDValue InsV = Insert128BitVector(DAG.getUNDEF(VT), V, NumElems/2, DAG, dl);
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000013296 return DCI.CombineTo(N, InsV);
13297 }
13298
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000013299 return SDValue();
13300}
13301
13302/// PerformShuffleCombine - Performs several different shuffle combines.
Dan Gohman475871a2008-07-27 21:46:04 +000013303static SDValue PerformShuffleCombine(SDNode *N, SelectionDAG &DAG,
Bruno Cardoso Lopes50b37c72011-08-15 21:45:54 +000013304 TargetLowering::DAGCombinerInfo &DCI,
13305 const X86Subtarget *Subtarget) {
Dale Johannesene4d209d2009-02-03 20:21:25 +000013306 DebugLoc dl = N->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +000013307 EVT VT = N->getValueType(0);
Mon P Wang1e955802009-04-03 02:43:30 +000013308
Mon P Wanga0fd0d52010-12-19 23:55:53 +000013309 // Don't create instructions with illegal types after legalize types has run.
13310 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
13311 if (!DCI.isBeforeLegalize() && !TLI.isTypeLegal(VT.getVectorElementType()))
13312 return SDValue();
13313
Bruno Cardoso Lopes50b37c72011-08-15 21:45:54 +000013314 // Combine 256-bit vector shuffles. This is only profitable when in AVX mode
Craig Topper7a9a28b2012-08-12 02:23:29 +000013315 if (Subtarget->hasAVX() && VT.is256BitVector() &&
Bruno Cardoso Lopes50b37c72011-08-15 21:45:54 +000013316 N->getOpcode() == ISD::VECTOR_SHUFFLE)
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +000013317 return PerformShuffleCombine256(N, DAG, DCI, Subtarget);
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000013318
13319 // Only handle 128 wide vector from here on.
Craig Topper7a9a28b2012-08-12 02:23:29 +000013320 if (!VT.is128BitVector())
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000013321 return SDValue();
13322
13323 // Combine a vector_shuffle that is equal to build_vector load1, load2, load3,
13324 // load4, <0, 1, 2, 3> into a 128-bit load if the load addresses are
13325 // consecutive, non-overlapping, and in the right order.
Nate Begemanfdea31a2010-03-24 20:49:50 +000013326 SmallVector<SDValue, 16> Elts;
13327 for (unsigned i = 0, e = VT.getVectorNumElements(); i != e; ++i)
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000013328 Elts.push_back(getShuffleScalarElt(N, i, DAG, 0));
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +000013329
Nate Begemanfdea31a2010-03-24 20:49:50 +000013330 return EltsFromConsecutiveLoads(VT, Elts, dl, DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +000013331}
Evan Chengd880b972008-05-09 21:53:03 +000013332
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000013333
Craig Topperc16f8512012-04-25 06:39:39 +000013334/// DCI, PerformTruncateCombine - Converts truncate operation to
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000013335/// a sequence of vector shuffle operations.
13336/// It is possible when we truncate 256-bit vector to 128-bit vector
13337
Chad Rosiera20e1e72012-08-01 18:39:17 +000013338SDValue X86TargetLowering::PerformTruncateCombine(SDNode *N, SelectionDAG &DAG,
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000013339 DAGCombinerInfo &DCI) const {
13340 if (!DCI.isBeforeLegalizeOps())
13341 return SDValue();
13342
Craig Topper3ef43cf2012-04-24 06:36:35 +000013343 if (!Subtarget->hasAVX())
13344 return SDValue();
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000013345
13346 EVT VT = N->getValueType(0);
13347 SDValue Op = N->getOperand(0);
13348 EVT OpVT = Op.getValueType();
13349 DebugLoc dl = N->getDebugLoc();
13350
13351 if ((VT == MVT::v4i32) && (OpVT == MVT::v4i64)) {
13352
Elena Demikhovsky1da58672012-04-22 09:39:03 +000013353 if (Subtarget->hasAVX2()) {
13354 // AVX2: v4i64 -> v4i32
13355
13356 // VPERMD
13357 static const int ShufMask[] = {0, 2, 4, 6, -1, -1, -1, -1};
13358
13359 Op = DAG.getNode(ISD::BITCAST, dl, MVT::v8i32, Op);
13360 Op = DAG.getVectorShuffle(MVT::v8i32, dl, Op, DAG.getUNDEF(MVT::v8i32),
13361 ShufMask);
13362
Craig Topperd63fa652012-04-22 18:51:37 +000013363 return DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT, Op,
13364 DAG.getIntPtrConstant(0));
Elena Demikhovsky1da58672012-04-22 09:39:03 +000013365 }
13366
13367 // AVX: v4i64 -> v4i32
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000013368 SDValue OpLo = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v2i64, Op,
Craig Topperd63fa652012-04-22 18:51:37 +000013369 DAG.getIntPtrConstant(0));
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000013370
13371 SDValue OpHi = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v2i64, Op,
Craig Topperd63fa652012-04-22 18:51:37 +000013372 DAG.getIntPtrConstant(2));
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000013373
13374 OpLo = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, OpLo);
13375 OpHi = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, OpHi);
13376
13377 // PSHUFD
Craig Topper9e401f22012-04-21 18:58:38 +000013378 static const int ShufMask1[] = {0, 2, 0, 0};
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000013379
Craig Toppercacafd42012-08-14 08:18:43 +000013380 SDValue Undef = DAG.getUNDEF(VT);
13381 OpLo = DAG.getVectorShuffle(VT, dl, OpLo, Undef, ShufMask1);
13382 OpHi = DAG.getVectorShuffle(VT, dl, OpHi, Undef, ShufMask1);
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000013383
13384 // MOVLHPS
Craig Topper9e401f22012-04-21 18:58:38 +000013385 static const int ShufMask2[] = {0, 1, 4, 5};
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000013386
Elena Demikhovsky73252572012-02-01 10:33:05 +000013387 return DAG.getVectorShuffle(VT, dl, OpLo, OpHi, ShufMask2);
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000013388 }
Craig Topperd63fa652012-04-22 18:51:37 +000013389
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000013390 if ((VT == MVT::v8i16) && (OpVT == MVT::v8i32)) {
13391
Elena Demikhovsky1da58672012-04-22 09:39:03 +000013392 if (Subtarget->hasAVX2()) {
13393 // AVX2: v8i32 -> v8i16
13394
13395 Op = DAG.getNode(ISD::BITCAST, dl, MVT::v32i8, Op);
Craig Topperd63fa652012-04-22 18:51:37 +000013396
Elena Demikhovsky1da58672012-04-22 09:39:03 +000013397 // PSHUFB
13398 SmallVector<SDValue,32> pshufbMask;
13399 for (unsigned i = 0; i < 2; ++i) {
13400 pshufbMask.push_back(DAG.getConstant(0x0, MVT::i8));
13401 pshufbMask.push_back(DAG.getConstant(0x1, MVT::i8));
13402 pshufbMask.push_back(DAG.getConstant(0x4, MVT::i8));
13403 pshufbMask.push_back(DAG.getConstant(0x5, MVT::i8));
13404 pshufbMask.push_back(DAG.getConstant(0x8, MVT::i8));
13405 pshufbMask.push_back(DAG.getConstant(0x9, MVT::i8));
13406 pshufbMask.push_back(DAG.getConstant(0xc, MVT::i8));
13407 pshufbMask.push_back(DAG.getConstant(0xd, MVT::i8));
13408 for (unsigned j = 0; j < 8; ++j)
13409 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
13410 }
Craig Topperd63fa652012-04-22 18:51:37 +000013411 SDValue BV = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v32i8,
13412 &pshufbMask[0], 32);
Elena Demikhovsky1da58672012-04-22 09:39:03 +000013413 Op = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v32i8, Op, BV);
13414
13415 Op = DAG.getNode(ISD::BITCAST, dl, MVT::v4i64, Op);
13416
13417 static const int ShufMask[] = {0, 2, -1, -1};
Craig Topperd63fa652012-04-22 18:51:37 +000013418 Op = DAG.getVectorShuffle(MVT::v4i64, dl, Op, DAG.getUNDEF(MVT::v4i64),
Elena Demikhovsky1da58672012-04-22 09:39:03 +000013419 &ShufMask[0]);
13420
Craig Topperd63fa652012-04-22 18:51:37 +000013421 Op = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v2i64, Op,
13422 DAG.getIntPtrConstant(0));
Elena Demikhovsky1da58672012-04-22 09:39:03 +000013423
13424 return DAG.getNode(ISD::BITCAST, dl, VT, Op);
13425 }
13426
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000013427 SDValue OpLo = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i32, Op,
Craig Topperd63fa652012-04-22 18:51:37 +000013428 DAG.getIntPtrConstant(0));
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000013429
13430 SDValue OpHi = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i32, Op,
Craig Topperd63fa652012-04-22 18:51:37 +000013431 DAG.getIntPtrConstant(4));
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000013432
13433 OpLo = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, OpLo);
13434 OpHi = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, OpHi);
13435
13436 // PSHUFB
Craig Topper9e401f22012-04-21 18:58:38 +000013437 static const int ShufMask1[] = {0, 1, 4, 5, 8, 9, 12, 13,
13438 -1, -1, -1, -1, -1, -1, -1, -1};
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000013439
Craig Toppercacafd42012-08-14 08:18:43 +000013440 SDValue Undef = DAG.getUNDEF(MVT::v16i8);
13441 OpLo = DAG.getVectorShuffle(MVT::v16i8, dl, OpLo, Undef, ShufMask1);
13442 OpHi = DAG.getVectorShuffle(MVT::v16i8, dl, OpHi, Undef, ShufMask1);
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000013443
13444 OpLo = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, OpLo);
13445 OpHi = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, OpHi);
13446
13447 // MOVLHPS
Craig Topper9e401f22012-04-21 18:58:38 +000013448 static const int ShufMask2[] = {0, 1, 4, 5};
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000013449
Elena Demikhovsky73252572012-02-01 10:33:05 +000013450 SDValue res = DAG.getVectorShuffle(MVT::v4i32, dl, OpLo, OpHi, ShufMask2);
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000013451 return DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, res);
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000013452 }
13453
13454 return SDValue();
13455}
13456
Craig Topper89f4e662012-03-20 07:17:59 +000013457/// XFormVExtractWithShuffleIntoLoad - Check if a vector extract from a target
13458/// specific shuffle of a load can be folded into a single element load.
13459/// Similar handling for VECTOR_SHUFFLE is performed by DAGCombiner, but
13460/// shuffles have been customed lowered so we need to handle those here.
13461static SDValue XFormVExtractWithShuffleIntoLoad(SDNode *N, SelectionDAG &DAG,
13462 TargetLowering::DAGCombinerInfo &DCI) {
13463 if (DCI.isBeforeLegalizeOps())
13464 return SDValue();
13465
13466 SDValue InVec = N->getOperand(0);
13467 SDValue EltNo = N->getOperand(1);
13468
13469 if (!isa<ConstantSDNode>(EltNo))
13470 return SDValue();
13471
13472 EVT VT = InVec.getValueType();
13473
13474 bool HasShuffleIntoBitcast = false;
13475 if (InVec.getOpcode() == ISD::BITCAST) {
13476 // Don't duplicate a load with other uses.
13477 if (!InVec.hasOneUse())
13478 return SDValue();
13479 EVT BCVT = InVec.getOperand(0).getValueType();
13480 if (BCVT.getVectorNumElements() != VT.getVectorNumElements())
13481 return SDValue();
13482 InVec = InVec.getOperand(0);
13483 HasShuffleIntoBitcast = true;
13484 }
13485
13486 if (!isTargetShuffle(InVec.getOpcode()))
13487 return SDValue();
13488
13489 // Don't duplicate a load with other uses.
13490 if (!InVec.hasOneUse())
13491 return SDValue();
13492
13493 SmallVector<int, 16> ShuffleMask;
13494 bool UnaryShuffle;
Craig Topperd978c542012-05-06 19:46:21 +000013495 if (!getTargetShuffleMask(InVec.getNode(), VT.getSimpleVT(), ShuffleMask,
13496 UnaryShuffle))
Craig Topper89f4e662012-03-20 07:17:59 +000013497 return SDValue();
13498
13499 // Select the input vector, guarding against out of range extract vector.
13500 unsigned NumElems = VT.getVectorNumElements();
13501 int Elt = cast<ConstantSDNode>(EltNo)->getZExtValue();
13502 int Idx = (Elt > (int)NumElems) ? -1 : ShuffleMask[Elt];
13503 SDValue LdNode = (Idx < (int)NumElems) ? InVec.getOperand(0)
13504 : InVec.getOperand(1);
13505
13506 // If inputs to shuffle are the same for both ops, then allow 2 uses
13507 unsigned AllowedUses = InVec.getOperand(0) == InVec.getOperand(1) ? 2 : 1;
13508
13509 if (LdNode.getOpcode() == ISD::BITCAST) {
13510 // Don't duplicate a load with other uses.
13511 if (!LdNode.getNode()->hasNUsesOfValue(AllowedUses, 0))
13512 return SDValue();
13513
13514 AllowedUses = 1; // only allow 1 load use if we have a bitcast
13515 LdNode = LdNode.getOperand(0);
13516 }
13517
13518 if (!ISD::isNormalLoad(LdNode.getNode()))
13519 return SDValue();
13520
13521 LoadSDNode *LN0 = cast<LoadSDNode>(LdNode);
13522
13523 if (!LN0 ||!LN0->hasNUsesOfValue(AllowedUses, 0) || LN0->isVolatile())
13524 return SDValue();
13525
13526 if (HasShuffleIntoBitcast) {
13527 // If there's a bitcast before the shuffle, check if the load type and
13528 // alignment is valid.
13529 unsigned Align = LN0->getAlignment();
13530 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
13531 unsigned NewAlign = TLI.getTargetData()->
13532 getABITypeAlignment(VT.getTypeForEVT(*DAG.getContext()));
13533
13534 if (NewAlign > Align || !TLI.isOperationLegalOrCustom(ISD::LOAD, VT))
13535 return SDValue();
13536 }
13537
13538 // All checks match so transform back to vector_shuffle so that DAG combiner
13539 // can finish the job
13540 DebugLoc dl = N->getDebugLoc();
13541
13542 // Create shuffle node taking into account the case that its a unary shuffle
13543 SDValue Shuffle = (UnaryShuffle) ? DAG.getUNDEF(VT) : InVec.getOperand(1);
13544 Shuffle = DAG.getVectorShuffle(InVec.getValueType(), dl,
13545 InVec.getOperand(0), Shuffle,
13546 &ShuffleMask[0]);
13547 Shuffle = DAG.getNode(ISD::BITCAST, dl, VT, Shuffle);
13548 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, N->getValueType(0), Shuffle,
13549 EltNo);
13550}
13551
Bruno Cardoso Lopesb3e06692010-09-03 19:55:05 +000013552/// PerformEXTRACT_VECTOR_ELTCombine - Detect vector gather/scatter index
13553/// generation and convert it from being a bunch of shuffles and extracts
13554/// to a simple store and scalar loads to extract the elements.
Dan Gohman1bbf72b2010-03-15 23:23:03 +000013555static SDValue PerformEXTRACT_VECTOR_ELTCombine(SDNode *N, SelectionDAG &DAG,
Craig Topper89f4e662012-03-20 07:17:59 +000013556 TargetLowering::DAGCombinerInfo &DCI) {
13557 SDValue NewOp = XFormVExtractWithShuffleIntoLoad(N, DAG, DCI);
13558 if (NewOp.getNode())
13559 return NewOp;
13560
Dan Gohman1bbf72b2010-03-15 23:23:03 +000013561 SDValue InputVector = N->getOperand(0);
13562
13563 // Only operate on vectors of 4 elements, where the alternative shuffling
13564 // gets to be more expensive.
13565 if (InputVector.getValueType() != MVT::v4i32)
13566 return SDValue();
13567
13568 // Check whether every use of InputVector is an EXTRACT_VECTOR_ELT with a
13569 // single use which is a sign-extend or zero-extend, and all elements are
13570 // used.
13571 SmallVector<SDNode *, 4> Uses;
13572 unsigned ExtractedElements = 0;
13573 for (SDNode::use_iterator UI = InputVector.getNode()->use_begin(),
13574 UE = InputVector.getNode()->use_end(); UI != UE; ++UI) {
13575 if (UI.getUse().getResNo() != InputVector.getResNo())
13576 return SDValue();
13577
13578 SDNode *Extract = *UI;
13579 if (Extract->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
13580 return SDValue();
13581
13582 if (Extract->getValueType(0) != MVT::i32)
13583 return SDValue();
13584 if (!Extract->hasOneUse())
13585 return SDValue();
13586 if (Extract->use_begin()->getOpcode() != ISD::SIGN_EXTEND &&
13587 Extract->use_begin()->getOpcode() != ISD::ZERO_EXTEND)
13588 return SDValue();
13589 if (!isa<ConstantSDNode>(Extract->getOperand(1)))
13590 return SDValue();
13591
13592 // Record which element was extracted.
13593 ExtractedElements |=
13594 1 << cast<ConstantSDNode>(Extract->getOperand(1))->getZExtValue();
13595
13596 Uses.push_back(Extract);
13597 }
13598
13599 // If not all the elements were used, this may not be worthwhile.
13600 if (ExtractedElements != 15)
13601 return SDValue();
13602
13603 // Ok, we've now decided to do the transformation.
13604 DebugLoc dl = InputVector.getDebugLoc();
13605
13606 // Store the value to a temporary stack slot.
13607 SDValue StackPtr = DAG.CreateStackTemporary(InputVector.getValueType());
Chris Lattner8026a9d2010-09-21 17:50:43 +000013608 SDValue Ch = DAG.getStore(DAG.getEntryNode(), dl, InputVector, StackPtr,
13609 MachinePointerInfo(), false, false, 0);
Dan Gohman1bbf72b2010-03-15 23:23:03 +000013610
13611 // Replace each use (extract) with a load of the appropriate element.
13612 for (SmallVectorImpl<SDNode *>::iterator UI = Uses.begin(),
13613 UE = Uses.end(); UI != UE; ++UI) {
13614 SDNode *Extract = *UI;
13615
Nadav Rotem86694292011-05-17 08:31:57 +000013616 // cOMpute the element's address.
Dan Gohman1bbf72b2010-03-15 23:23:03 +000013617 SDValue Idx = Extract->getOperand(1);
13618 unsigned EltSize =
13619 InputVector.getValueType().getVectorElementType().getSizeInBits()/8;
13620 uint64_t Offset = EltSize * cast<ConstantSDNode>(Idx)->getZExtValue();
Craig Topper89f4e662012-03-20 07:17:59 +000013621 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Dan Gohman1bbf72b2010-03-15 23:23:03 +000013622 SDValue OffsetVal = DAG.getConstant(Offset, TLI.getPointerTy());
13623
Nadav Rotem86694292011-05-17 08:31:57 +000013624 SDValue ScalarAddr = DAG.getNode(ISD::ADD, dl, TLI.getPointerTy(),
Chris Lattner51abfe42010-09-21 06:02:19 +000013625 StackPtr, OffsetVal);
Dan Gohman1bbf72b2010-03-15 23:23:03 +000013626
13627 // Load the scalar.
Eric Christopher90eb4022010-07-22 00:26:08 +000013628 SDValue LoadScalar = DAG.getLoad(Extract->getValueType(0), dl, Ch,
Chris Lattner51abfe42010-09-21 06:02:19 +000013629 ScalarAddr, MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000013630 false, false, false, 0);
Dan Gohman1bbf72b2010-03-15 23:23:03 +000013631
13632 // Replace the exact with the load.
13633 DAG.ReplaceAllUsesOfValueWith(SDValue(Extract, 0), LoadScalar);
13634 }
13635
13636 // The replacement was made in place; don't return anything.
13637 return SDValue();
13638}
13639
Duncan Sands6bcd2192011-09-17 16:49:39 +000013640/// PerformSELECTCombine - Do target-specific dag combines on SELECT and VSELECT
13641/// nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000013642static SDValue PerformSELECTCombine(SDNode *N, SelectionDAG &DAG,
Nadav Rotemcc616562012-01-15 19:27:55 +000013643 TargetLowering::DAGCombinerInfo &DCI,
Chris Lattner47b4ce82009-03-11 05:48:52 +000013644 const X86Subtarget *Subtarget) {
13645 DebugLoc DL = N->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +000013646 SDValue Cond = N->getOperand(0);
Chris Lattner47b4ce82009-03-11 05:48:52 +000013647 // Get the LHS/RHS of the select.
13648 SDValue LHS = N->getOperand(1);
13649 SDValue RHS = N->getOperand(2);
Bruno Cardoso Lopes149f29f2011-09-20 22:34:45 +000013650 EVT VT = LHS.getValueType();
Eric Christopherfd179292009-08-27 18:07:15 +000013651
Dan Gohman670e5392009-09-21 18:03:22 +000013652 // If we have SSE[12] support, try to form min/max nodes. SSE min/max
Dan Gohman8ce05da2010-02-22 04:03:39 +000013653 // instructions match the semantics of the common C idiom x<y?x:y but not
13654 // x<=y?x:y, because of how they handle negative zero (which can be
13655 // ignored in unsafe-math mode).
Benjamin Kramer2c2ccbf2011-09-22 03:27:22 +000013656 if (Cond.getOpcode() == ISD::SETCC && VT.isFloatingPoint() &&
13657 VT != MVT::f80 && DAG.getTargetLoweringInfo().isTypeLegal(VT) &&
Craig Topper1accb7e2012-01-10 06:54:16 +000013658 (Subtarget->hasSSE2() ||
13659 (Subtarget->hasSSE1() && VT.getScalarType() == MVT::f32))) {
Chris Lattner47b4ce82009-03-11 05:48:52 +000013660 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000013661
Chris Lattner47b4ce82009-03-11 05:48:52 +000013662 unsigned Opcode = 0;
Dan Gohman670e5392009-09-21 18:03:22 +000013663 // Check for x CC y ? x : y.
Dan Gohmane8326932010-02-24 06:52:40 +000013664 if (DAG.isEqualTo(LHS, Cond.getOperand(0)) &&
13665 DAG.isEqualTo(RHS, Cond.getOperand(1))) {
Chris Lattner47b4ce82009-03-11 05:48:52 +000013666 switch (CC) {
13667 default: break;
Dan Gohman670e5392009-09-21 18:03:22 +000013668 case ISD::SETULT:
Dan Gohmane8326932010-02-24 06:52:40 +000013669 // Converting this to a min would handle NaNs incorrectly, and swapping
13670 // the operands would cause it to handle comparisons between positive
13671 // and negative zero incorrectly.
Evan Cheng60108e92010-07-15 22:07:12 +000013672 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)) {
Nick Lewycky8a8d4792011-12-02 22:16:29 +000013673 if (!DAG.getTarget().Options.UnsafeFPMath &&
Dan Gohmane8326932010-02-24 06:52:40 +000013674 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
13675 break;
13676 std::swap(LHS, RHS);
13677 }
Dan Gohman670e5392009-09-21 18:03:22 +000013678 Opcode = X86ISD::FMIN;
13679 break;
13680 case ISD::SETOLE:
Dan Gohmane8326932010-02-24 06:52:40 +000013681 // Converting this to a min would handle comparisons between positive
13682 // and negative zero incorrectly.
Nick Lewycky8a8d4792011-12-02 22:16:29 +000013683 if (!DAG.getTarget().Options.UnsafeFPMath &&
Dan Gohmane8326932010-02-24 06:52:40 +000013684 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(RHS))
13685 break;
Dan Gohman670e5392009-09-21 18:03:22 +000013686 Opcode = X86ISD::FMIN;
13687 break;
Chris Lattner47b4ce82009-03-11 05:48:52 +000013688 case ISD::SETULE:
Dan Gohmane8326932010-02-24 06:52:40 +000013689 // Converting this to a min would handle both negative zeros and NaNs
13690 // incorrectly, but we can swap the operands to fix both.
13691 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000013692 case ISD::SETOLT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000013693 case ISD::SETLT:
Dan Gohman670e5392009-09-21 18:03:22 +000013694 case ISD::SETLE:
Chris Lattner47b4ce82009-03-11 05:48:52 +000013695 Opcode = X86ISD::FMIN;
13696 break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000013697
Dan Gohman670e5392009-09-21 18:03:22 +000013698 case ISD::SETOGE:
Dan Gohmane8326932010-02-24 06:52:40 +000013699 // Converting this to a max would handle comparisons between positive
13700 // and negative zero incorrectly.
Nick Lewycky8a8d4792011-12-02 22:16:29 +000013701 if (!DAG.getTarget().Options.UnsafeFPMath &&
Evan Chengdd5663c2011-08-04 18:38:15 +000013702 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000013703 break;
Dan Gohman670e5392009-09-21 18:03:22 +000013704 Opcode = X86ISD::FMAX;
13705 break;
Chris Lattner47b4ce82009-03-11 05:48:52 +000013706 case ISD::SETUGT:
Dan Gohmane8326932010-02-24 06:52:40 +000013707 // Converting this to a max would handle NaNs incorrectly, and swapping
13708 // the operands would cause it to handle comparisons between positive
13709 // and negative zero incorrectly.
Evan Cheng60108e92010-07-15 22:07:12 +000013710 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)) {
Nick Lewycky8a8d4792011-12-02 22:16:29 +000013711 if (!DAG.getTarget().Options.UnsafeFPMath &&
Dan Gohmane8326932010-02-24 06:52:40 +000013712 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
13713 break;
13714 std::swap(LHS, RHS);
13715 }
Dan Gohman670e5392009-09-21 18:03:22 +000013716 Opcode = X86ISD::FMAX;
13717 break;
13718 case ISD::SETUGE:
Dan Gohmane8326932010-02-24 06:52:40 +000013719 // Converting this to a max would handle both negative zeros and NaNs
13720 // incorrectly, but we can swap the operands to fix both.
13721 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000013722 case ISD::SETOGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000013723 case ISD::SETGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000013724 case ISD::SETGE:
13725 Opcode = X86ISD::FMAX;
13726 break;
Chris Lattner83e6c992006-10-04 06:57:07 +000013727 }
Dan Gohman670e5392009-09-21 18:03:22 +000013728 // Check for x CC y ? y : x -- a min/max with reversed arms.
Dan Gohmane8326932010-02-24 06:52:40 +000013729 } else if (DAG.isEqualTo(LHS, Cond.getOperand(1)) &&
13730 DAG.isEqualTo(RHS, Cond.getOperand(0))) {
Chris Lattner47b4ce82009-03-11 05:48:52 +000013731 switch (CC) {
13732 default: break;
Dan Gohman670e5392009-09-21 18:03:22 +000013733 case ISD::SETOGE:
Dan Gohmane8326932010-02-24 06:52:40 +000013734 // Converting this to a min would handle comparisons between positive
13735 // and negative zero incorrectly, and swapping the operands would
13736 // cause it to handle NaNs incorrectly.
Nick Lewycky8a8d4792011-12-02 22:16:29 +000013737 if (!DAG.getTarget().Options.UnsafeFPMath &&
Dan Gohmane8326932010-02-24 06:52:40 +000013738 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS))) {
Evan Cheng60108e92010-07-15 22:07:12 +000013739 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000013740 break;
13741 std::swap(LHS, RHS);
13742 }
Dan Gohman670e5392009-09-21 18:03:22 +000013743 Opcode = X86ISD::FMIN;
Dan Gohman8d44b282009-09-03 20:34:31 +000013744 break;
Dan Gohman670e5392009-09-21 18:03:22 +000013745 case ISD::SETUGT:
Dan Gohmane8326932010-02-24 06:52:40 +000013746 // Converting this to a min would handle NaNs incorrectly.
Nick Lewycky8a8d4792011-12-02 22:16:29 +000013747 if (!DAG.getTarget().Options.UnsafeFPMath &&
Dan Gohmane8326932010-02-24 06:52:40 +000013748 (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)))
13749 break;
Dan Gohman670e5392009-09-21 18:03:22 +000013750 Opcode = X86ISD::FMIN;
13751 break;
13752 case ISD::SETUGE:
Dan Gohmane8326932010-02-24 06:52:40 +000013753 // Converting this to a min would handle both negative zeros and NaNs
13754 // incorrectly, but we can swap the operands to fix both.
13755 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000013756 case ISD::SETOGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000013757 case ISD::SETGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000013758 case ISD::SETGE:
13759 Opcode = X86ISD::FMIN;
13760 break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000013761
Dan Gohman670e5392009-09-21 18:03:22 +000013762 case ISD::SETULT:
Dan Gohmane8326932010-02-24 06:52:40 +000013763 // Converting this to a max would handle NaNs incorrectly.
Evan Cheng60108e92010-07-15 22:07:12 +000013764 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000013765 break;
Dan Gohman670e5392009-09-21 18:03:22 +000013766 Opcode = X86ISD::FMAX;
Dan Gohman8d44b282009-09-03 20:34:31 +000013767 break;
Dan Gohman670e5392009-09-21 18:03:22 +000013768 case ISD::SETOLE:
Dan Gohmane8326932010-02-24 06:52:40 +000013769 // Converting this to a max would handle comparisons between positive
13770 // and negative zero incorrectly, and swapping the operands would
13771 // cause it to handle NaNs incorrectly.
Nick Lewycky8a8d4792011-12-02 22:16:29 +000013772 if (!DAG.getTarget().Options.UnsafeFPMath &&
Dan Gohmane8326932010-02-24 06:52:40 +000013773 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(RHS)) {
Evan Cheng60108e92010-07-15 22:07:12 +000013774 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000013775 break;
13776 std::swap(LHS, RHS);
13777 }
Dan Gohman670e5392009-09-21 18:03:22 +000013778 Opcode = X86ISD::FMAX;
13779 break;
13780 case ISD::SETULE:
Dan Gohmane8326932010-02-24 06:52:40 +000013781 // Converting this to a max would handle both negative zeros and NaNs
13782 // incorrectly, but we can swap the operands to fix both.
13783 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000013784 case ISD::SETOLT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000013785 case ISD::SETLT:
Dan Gohman670e5392009-09-21 18:03:22 +000013786 case ISD::SETLE:
Chris Lattner47b4ce82009-03-11 05:48:52 +000013787 Opcode = X86ISD::FMAX;
13788 break;
13789 }
Chris Lattner83e6c992006-10-04 06:57:07 +000013790 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000013791
Chris Lattner47b4ce82009-03-11 05:48:52 +000013792 if (Opcode)
13793 return DAG.getNode(Opcode, DL, N->getValueType(0), LHS, RHS);
Chris Lattner83e6c992006-10-04 06:57:07 +000013794 }
Eric Christopherfd179292009-08-27 18:07:15 +000013795
Chris Lattnerd1980a52009-03-12 06:52:53 +000013796 // If this is a select between two integer constants, try to do some
13797 // optimizations.
Chris Lattnercee56e72009-03-13 05:53:31 +000013798 if (ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(LHS)) {
13799 if (ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(RHS))
Chris Lattnerd1980a52009-03-12 06:52:53 +000013800 // Don't do this for crazy integer types.
13801 if (DAG.getTargetLoweringInfo().isTypeLegal(LHS.getValueType())) {
13802 // If this is efficiently invertible, canonicalize the LHSC/RHSC values
Chris Lattnercee56e72009-03-13 05:53:31 +000013803 // so that TrueC (the true value) is larger than FalseC.
Chris Lattnerd1980a52009-03-12 06:52:53 +000013804 bool NeedsCondInvert = false;
Eric Christopherfd179292009-08-27 18:07:15 +000013805
Chris Lattnercee56e72009-03-13 05:53:31 +000013806 if (TrueC->getAPIntValue().ult(FalseC->getAPIntValue()) &&
Chris Lattnerd1980a52009-03-12 06:52:53 +000013807 // Efficiently invertible.
13808 (Cond.getOpcode() == ISD::SETCC || // setcc -> invertible.
13809 (Cond.getOpcode() == ISD::XOR && // xor(X, C) -> invertible.
13810 isa<ConstantSDNode>(Cond.getOperand(1))))) {
13811 NeedsCondInvert = true;
Chris Lattnercee56e72009-03-13 05:53:31 +000013812 std::swap(TrueC, FalseC);
Chris Lattnerd1980a52009-03-12 06:52:53 +000013813 }
Eric Christopherfd179292009-08-27 18:07:15 +000013814
Chris Lattnerd1980a52009-03-12 06:52:53 +000013815 // Optimize C ? 8 : 0 -> zext(C) << 3. Likewise for any pow2/0.
Chris Lattnercee56e72009-03-13 05:53:31 +000013816 if (FalseC->getAPIntValue() == 0 &&
13817 TrueC->getAPIntValue().isPowerOf2()) {
Chris Lattnerd1980a52009-03-12 06:52:53 +000013818 if (NeedsCondInvert) // Invert the condition if needed.
13819 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
13820 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000013821
Chris Lattnerd1980a52009-03-12 06:52:53 +000013822 // Zero extend the condition if needed.
13823 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, LHS.getValueType(), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000013824
Chris Lattnercee56e72009-03-13 05:53:31 +000013825 unsigned ShAmt = TrueC->getAPIntValue().logBase2();
Chris Lattnerd1980a52009-03-12 06:52:53 +000013826 return DAG.getNode(ISD::SHL, DL, LHS.getValueType(), Cond,
Owen Anderson825b72b2009-08-11 20:47:22 +000013827 DAG.getConstant(ShAmt, MVT::i8));
Chris Lattnerd1980a52009-03-12 06:52:53 +000013828 }
Eric Christopherfd179292009-08-27 18:07:15 +000013829
Chris Lattner97a29a52009-03-13 05:22:11 +000013830 // Optimize Cond ? cst+1 : cst -> zext(setcc(C)+cst.
Chris Lattnercee56e72009-03-13 05:53:31 +000013831 if (FalseC->getAPIntValue()+1 == TrueC->getAPIntValue()) {
Chris Lattner97a29a52009-03-13 05:22:11 +000013832 if (NeedsCondInvert) // Invert the condition if needed.
13833 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
13834 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000013835
Chris Lattner97a29a52009-03-13 05:22:11 +000013836 // Zero extend the condition if needed.
Chris Lattnercee56e72009-03-13 05:53:31 +000013837 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL,
13838 FalseC->getValueType(0), Cond);
Chris Lattner97a29a52009-03-13 05:22:11 +000013839 return DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
Chris Lattnercee56e72009-03-13 05:53:31 +000013840 SDValue(FalseC, 0));
Chris Lattner97a29a52009-03-13 05:22:11 +000013841 }
Eric Christopherfd179292009-08-27 18:07:15 +000013842
Chris Lattnercee56e72009-03-13 05:53:31 +000013843 // Optimize cases that will turn into an LEA instruction. This requires
13844 // an i32 or i64 and an efficient multiplier (1, 2, 3, 4, 5, 8, 9).
Owen Anderson825b72b2009-08-11 20:47:22 +000013845 if (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i64) {
Chris Lattnercee56e72009-03-13 05:53:31 +000013846 uint64_t Diff = TrueC->getZExtValue()-FalseC->getZExtValue();
Owen Anderson825b72b2009-08-11 20:47:22 +000013847 if (N->getValueType(0) == MVT::i32) Diff = (unsigned)Diff;
Eric Christopherfd179292009-08-27 18:07:15 +000013848
Chris Lattnercee56e72009-03-13 05:53:31 +000013849 bool isFastMultiplier = false;
13850 if (Diff < 10) {
13851 switch ((unsigned char)Diff) {
13852 default: break;
13853 case 1: // result = add base, cond
13854 case 2: // result = lea base( , cond*2)
13855 case 3: // result = lea base(cond, cond*2)
13856 case 4: // result = lea base( , cond*4)
13857 case 5: // result = lea base(cond, cond*4)
13858 case 8: // result = lea base( , cond*8)
13859 case 9: // result = lea base(cond, cond*8)
13860 isFastMultiplier = true;
13861 break;
13862 }
13863 }
Eric Christopherfd179292009-08-27 18:07:15 +000013864
Chris Lattnercee56e72009-03-13 05:53:31 +000013865 if (isFastMultiplier) {
13866 APInt Diff = TrueC->getAPIntValue()-FalseC->getAPIntValue();
13867 if (NeedsCondInvert) // Invert the condition if needed.
13868 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
13869 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000013870
Chris Lattnercee56e72009-03-13 05:53:31 +000013871 // Zero extend the condition if needed.
13872 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, FalseC->getValueType(0),
13873 Cond);
13874 // Scale the condition by the difference.
13875 if (Diff != 1)
13876 Cond = DAG.getNode(ISD::MUL, DL, Cond.getValueType(), Cond,
13877 DAG.getConstant(Diff, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000013878
Chris Lattnercee56e72009-03-13 05:53:31 +000013879 // Add the base if non-zero.
13880 if (FalseC->getAPIntValue() != 0)
13881 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
13882 SDValue(FalseC, 0));
13883 return Cond;
13884 }
Eric Christopherfd179292009-08-27 18:07:15 +000013885 }
Chris Lattnerd1980a52009-03-12 06:52:53 +000013886 }
13887 }
Eric Christopherfd179292009-08-27 18:07:15 +000013888
Evan Cheng56f582d2012-01-04 01:41:39 +000013889 // Canonicalize max and min:
13890 // (x > y) ? x : y -> (x >= y) ? x : y
13891 // (x < y) ? x : y -> (x <= y) ? x : y
13892 // This allows use of COND_S / COND_NS (see TranslateX86CC) which eliminates
13893 // the need for an extra compare
13894 // against zero. e.g.
13895 // (x - y) > 0 : (x - y) ? 0 -> (x - y) >= 0 : (x - y) ? 0
13896 // subl %esi, %edi
13897 // testl %edi, %edi
13898 // movl $0, %eax
13899 // cmovgl %edi, %eax
13900 // =>
13901 // xorl %eax, %eax
13902 // subl %esi, $edi
13903 // cmovsl %eax, %edi
13904 if (N->getOpcode() == ISD::SELECT && Cond.getOpcode() == ISD::SETCC &&
13905 DAG.isEqualTo(LHS, Cond.getOperand(0)) &&
13906 DAG.isEqualTo(RHS, Cond.getOperand(1))) {
13907 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
13908 switch (CC) {
13909 default: break;
13910 case ISD::SETLT:
13911 case ISD::SETGT: {
13912 ISD::CondCode NewCC = (CC == ISD::SETLT) ? ISD::SETLE : ISD::SETGE;
13913 Cond = DAG.getSetCC(Cond.getDebugLoc(), Cond.getValueType(),
13914 Cond.getOperand(0), Cond.getOperand(1), NewCC);
13915 return DAG.getNode(ISD::SELECT, DL, VT, Cond, LHS, RHS);
13916 }
13917 }
13918 }
13919
Nadav Rotemcc616562012-01-15 19:27:55 +000013920 // If we know that this node is legal then we know that it is going to be
13921 // matched by one of the SSE/AVX BLEND instructions. These instructions only
13922 // depend on the highest bit in each word. Try to use SimplifyDemandedBits
13923 // to simplify previous instructions.
13924 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
13925 if (N->getOpcode() == ISD::VSELECT && DCI.isBeforeLegalizeOps() &&
Nadav Rotembdcae382012-06-07 20:53:48 +000013926 !DCI.isBeforeLegalize() && TLI.isOperationLegal(ISD::VSELECT, VT)) {
Nadav Rotemcc616562012-01-15 19:27:55 +000013927 unsigned BitWidth = Cond.getValueType().getScalarType().getSizeInBits();
Nadav Rotembdcae382012-06-07 20:53:48 +000013928
13929 // Don't optimize vector selects that map to mask-registers.
13930 if (BitWidth == 1)
13931 return SDValue();
13932
Nadav Rotemcc616562012-01-15 19:27:55 +000013933 assert(BitWidth >= 8 && BitWidth <= 64 && "Invalid mask size");
13934 APInt DemandedMask = APInt::getHighBitsSet(BitWidth, 1);
13935
13936 APInt KnownZero, KnownOne;
13937 TargetLowering::TargetLoweringOpt TLO(DAG, DCI.isBeforeLegalize(),
13938 DCI.isBeforeLegalizeOps());
13939 if (TLO.ShrinkDemandedConstant(Cond, DemandedMask) ||
13940 TLI.SimplifyDemandedBits(Cond, DemandedMask, KnownZero, KnownOne, TLO))
13941 DCI.CommitTargetLoweringOpt(TLO);
13942 }
13943
Dan Gohman475871a2008-07-27 21:46:04 +000013944 return SDValue();
Chris Lattner83e6c992006-10-04 06:57:07 +000013945}
13946
Michael Liao2a33cec2012-08-10 19:58:13 +000013947// Check whether a boolean test is testing a boolean value generated by
13948// X86ISD::SETCC. If so, return the operand of that SETCC and proper condition
13949// code.
13950//
13951// Simplify the following patterns:
13952// (Op (CMP (SETCC Cond EFLAGS) 1) EQ) or
13953// (Op (CMP (SETCC Cond EFLAGS) 0) NEQ)
13954// to (Op EFLAGS Cond)
13955//
13956// (Op (CMP (SETCC Cond EFLAGS) 0) EQ) or
13957// (Op (CMP (SETCC Cond EFLAGS) 1) NEQ)
13958// to (Op EFLAGS !Cond)
13959//
13960// where Op could be BRCOND or CMOV.
13961//
13962static SDValue BoolTestSetCCCombine(SDValue Cmp, X86::CondCode &CC) {
13963 // Quit if not CMP and SUB with its value result used.
13964 if (Cmp.getOpcode() != X86ISD::CMP &&
13965 (Cmp.getOpcode() != X86ISD::SUB || Cmp.getNode()->hasAnyUseOfValue(0)))
13966 return SDValue();
13967
13968 // Quit if not used as a boolean value.
13969 if (CC != X86::COND_E && CC != X86::COND_NE)
13970 return SDValue();
13971
13972 // Check CMP operands. One of them should be 0 or 1 and the other should be
13973 // an SetCC or extended from it.
13974 SDValue Op1 = Cmp.getOperand(0);
13975 SDValue Op2 = Cmp.getOperand(1);
13976
13977 SDValue SetCC;
13978 const ConstantSDNode* C = 0;
13979 bool needOppositeCond = (CC == X86::COND_E);
13980
13981 if ((C = dyn_cast<ConstantSDNode>(Op1)))
13982 SetCC = Op2;
13983 else if ((C = dyn_cast<ConstantSDNode>(Op2)))
13984 SetCC = Op1;
13985 else // Quit if all operands are not constants.
13986 return SDValue();
13987
13988 if (C->getZExtValue() == 1)
13989 needOppositeCond = !needOppositeCond;
13990 else if (C->getZExtValue() != 0)
13991 // Quit if the constant is neither 0 or 1.
13992 return SDValue();
13993
13994 // Skip 'zext' node.
13995 if (SetCC.getOpcode() == ISD::ZERO_EXTEND)
13996 SetCC = SetCC.getOperand(0);
13997
13998 // Quit if not SETCC.
13999 // FIXME: So far we only handle the boolean value generated from SETCC. If
14000 // there is other ways to generate boolean values, we need handle them here
14001 // as well.
14002 if (SetCC.getOpcode() != X86ISD::SETCC)
14003 return SDValue();
14004
14005 // Set the condition code or opposite one if necessary.
14006 CC = X86::CondCode(SetCC.getConstantOperandVal(0));
14007 if (needOppositeCond)
14008 CC = X86::GetOppositeBranchCondition(CC);
14009
14010 return SetCC.getOperand(1);
14011}
14012
Michael Liao9eac20a2012-08-11 23:47:06 +000014013static bool IsValidFCMOVCondition(X86::CondCode CC) {
14014 switch (CC) {
14015 default:
14016 return false;
14017 case X86::COND_B:
14018 case X86::COND_BE:
14019 case X86::COND_E:
14020 case X86::COND_P:
14021 case X86::COND_AE:
14022 case X86::COND_A:
14023 case X86::COND_NE:
14024 case X86::COND_NP:
14025 return true;
14026 }
14027}
14028
Chris Lattnerd1980a52009-03-12 06:52:53 +000014029/// Optimize X86ISD::CMOV [LHS, RHS, CONDCODE (e.g. X86::COND_NE), CONDVAL]
14030static SDValue PerformCMOVCombine(SDNode *N, SelectionDAG &DAG,
14031 TargetLowering::DAGCombinerInfo &DCI) {
14032 DebugLoc DL = N->getDebugLoc();
Eric Christopherfd179292009-08-27 18:07:15 +000014033
Chris Lattnerd1980a52009-03-12 06:52:53 +000014034 // If the flag operand isn't dead, don't touch this CMOV.
14035 if (N->getNumValues() == 2 && !SDValue(N, 1).use_empty())
14036 return SDValue();
Eric Christopherfd179292009-08-27 18:07:15 +000014037
Evan Chengb5a55d92011-05-24 01:48:22 +000014038 SDValue FalseOp = N->getOperand(0);
14039 SDValue TrueOp = N->getOperand(1);
14040 X86::CondCode CC = (X86::CondCode)N->getConstantOperandVal(2);
14041 SDValue Cond = N->getOperand(3);
Michael Liao2a33cec2012-08-10 19:58:13 +000014042
Evan Chengb5a55d92011-05-24 01:48:22 +000014043 if (CC == X86::COND_E || CC == X86::COND_NE) {
14044 switch (Cond.getOpcode()) {
14045 default: break;
14046 case X86ISD::BSR:
14047 case X86ISD::BSF:
14048 // If operand of BSR / BSF are proven never zero, then ZF cannot be set.
14049 if (DAG.isKnownNeverZero(Cond.getOperand(0)))
14050 return (CC == X86::COND_E) ? FalseOp : TrueOp;
14051 }
14052 }
14053
Michael Liao2a33cec2012-08-10 19:58:13 +000014054 SDValue Flags;
14055
14056 Flags = BoolTestSetCCCombine(Cond, CC);
Michael Liao9eac20a2012-08-11 23:47:06 +000014057 if (Flags.getNode() &&
14058 // Extra check as FCMOV only supports a subset of X86 cond.
14059 (FalseOp.getValueType() != MVT::f80 || IsValidFCMOVCondition(CC))) {
Michael Liao2a33cec2012-08-10 19:58:13 +000014060 SDValue Ops[] = { FalseOp, TrueOp,
14061 DAG.getConstant(CC, MVT::i8), Flags };
14062 return DAG.getNode(X86ISD::CMOV, DL, N->getVTList(),
14063 Ops, array_lengthof(Ops));
14064 }
14065
Chris Lattnerd1980a52009-03-12 06:52:53 +000014066 // If this is a select between two integer constants, try to do some
14067 // optimizations. Note that the operands are ordered the opposite of SELECT
14068 // operands.
Evan Chengb5a55d92011-05-24 01:48:22 +000014069 if (ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(TrueOp)) {
14070 if (ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(FalseOp)) {
Chris Lattnerd1980a52009-03-12 06:52:53 +000014071 // Canonicalize the TrueC/FalseC values so that TrueC (the true value) is
14072 // larger than FalseC (the false value).
Chris Lattnerd1980a52009-03-12 06:52:53 +000014073 if (TrueC->getAPIntValue().ult(FalseC->getAPIntValue())) {
14074 CC = X86::GetOppositeBranchCondition(CC);
14075 std::swap(TrueC, FalseC);
14076 }
Eric Christopherfd179292009-08-27 18:07:15 +000014077
Chris Lattnerd1980a52009-03-12 06:52:53 +000014078 // Optimize C ? 8 : 0 -> zext(setcc(C)) << 3. Likewise for any pow2/0.
Chris Lattnercee56e72009-03-13 05:53:31 +000014079 // This is efficient for any integer data type (including i8/i16) and
14080 // shift amount.
Chris Lattnerd1980a52009-03-12 06:52:53 +000014081 if (FalseC->getAPIntValue() == 0 && TrueC->getAPIntValue().isPowerOf2()) {
Owen Anderson825b72b2009-08-11 20:47:22 +000014082 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
14083 DAG.getConstant(CC, MVT::i8), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000014084
Chris Lattnerd1980a52009-03-12 06:52:53 +000014085 // Zero extend the condition if needed.
14086 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, TrueC->getValueType(0), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000014087
Chris Lattnerd1980a52009-03-12 06:52:53 +000014088 unsigned ShAmt = TrueC->getAPIntValue().logBase2();
14089 Cond = DAG.getNode(ISD::SHL, DL, Cond.getValueType(), Cond,
Owen Anderson825b72b2009-08-11 20:47:22 +000014090 DAG.getConstant(ShAmt, MVT::i8));
Chris Lattnerd1980a52009-03-12 06:52:53 +000014091 if (N->getNumValues() == 2) // Dead flag value?
14092 return DCI.CombineTo(N, Cond, SDValue());
14093 return Cond;
14094 }
Eric Christopherfd179292009-08-27 18:07:15 +000014095
Chris Lattnercee56e72009-03-13 05:53:31 +000014096 // Optimize Cond ? cst+1 : cst -> zext(setcc(C)+cst. This is efficient
14097 // for any integer data type, including i8/i16.
Chris Lattner97a29a52009-03-13 05:22:11 +000014098 if (FalseC->getAPIntValue()+1 == TrueC->getAPIntValue()) {
Owen Anderson825b72b2009-08-11 20:47:22 +000014099 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
14100 DAG.getConstant(CC, MVT::i8), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000014101
Chris Lattner97a29a52009-03-13 05:22:11 +000014102 // Zero extend the condition if needed.
Chris Lattnercee56e72009-03-13 05:53:31 +000014103 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL,
14104 FalseC->getValueType(0), Cond);
Chris Lattner97a29a52009-03-13 05:22:11 +000014105 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
14106 SDValue(FalseC, 0));
Eric Christopherfd179292009-08-27 18:07:15 +000014107
Chris Lattner97a29a52009-03-13 05:22:11 +000014108 if (N->getNumValues() == 2) // Dead flag value?
14109 return DCI.CombineTo(N, Cond, SDValue());
14110 return Cond;
14111 }
Eric Christopherfd179292009-08-27 18:07:15 +000014112
Chris Lattnercee56e72009-03-13 05:53:31 +000014113 // Optimize cases that will turn into an LEA instruction. This requires
14114 // an i32 or i64 and an efficient multiplier (1, 2, 3, 4, 5, 8, 9).
Owen Anderson825b72b2009-08-11 20:47:22 +000014115 if (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i64) {
Chris Lattnercee56e72009-03-13 05:53:31 +000014116 uint64_t Diff = TrueC->getZExtValue()-FalseC->getZExtValue();
Owen Anderson825b72b2009-08-11 20:47:22 +000014117 if (N->getValueType(0) == MVT::i32) Diff = (unsigned)Diff;
Eric Christopherfd179292009-08-27 18:07:15 +000014118
Chris Lattnercee56e72009-03-13 05:53:31 +000014119 bool isFastMultiplier = false;
14120 if (Diff < 10) {
14121 switch ((unsigned char)Diff) {
14122 default: break;
14123 case 1: // result = add base, cond
14124 case 2: // result = lea base( , cond*2)
14125 case 3: // result = lea base(cond, cond*2)
14126 case 4: // result = lea base( , cond*4)
14127 case 5: // result = lea base(cond, cond*4)
14128 case 8: // result = lea base( , cond*8)
14129 case 9: // result = lea base(cond, cond*8)
14130 isFastMultiplier = true;
14131 break;
14132 }
14133 }
Eric Christopherfd179292009-08-27 18:07:15 +000014134
Chris Lattnercee56e72009-03-13 05:53:31 +000014135 if (isFastMultiplier) {
14136 APInt Diff = TrueC->getAPIntValue()-FalseC->getAPIntValue();
Owen Anderson825b72b2009-08-11 20:47:22 +000014137 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
14138 DAG.getConstant(CC, MVT::i8), Cond);
Chris Lattnercee56e72009-03-13 05:53:31 +000014139 // Zero extend the condition if needed.
14140 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, FalseC->getValueType(0),
14141 Cond);
14142 // Scale the condition by the difference.
14143 if (Diff != 1)
14144 Cond = DAG.getNode(ISD::MUL, DL, Cond.getValueType(), Cond,
14145 DAG.getConstant(Diff, Cond.getValueType()));
14146
14147 // Add the base if non-zero.
14148 if (FalseC->getAPIntValue() != 0)
14149 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
14150 SDValue(FalseC, 0));
14151 if (N->getNumValues() == 2) // Dead flag value?
14152 return DCI.CombineTo(N, Cond, SDValue());
14153 return Cond;
14154 }
Eric Christopherfd179292009-08-27 18:07:15 +000014155 }
Chris Lattnerd1980a52009-03-12 06:52:53 +000014156 }
14157 }
14158 return SDValue();
14159}
14160
14161
Evan Cheng0b0cd912009-03-28 05:57:29 +000014162/// PerformMulCombine - Optimize a single multiply with constant into two
14163/// in order to implement it with two cheaper instructions, e.g.
14164/// LEA + SHL, LEA + LEA.
14165static SDValue PerformMulCombine(SDNode *N, SelectionDAG &DAG,
14166 TargetLowering::DAGCombinerInfo &DCI) {
Evan Cheng0b0cd912009-03-28 05:57:29 +000014167 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
14168 return SDValue();
14169
Owen Andersone50ed302009-08-10 22:56:29 +000014170 EVT VT = N->getValueType(0);
Owen Anderson825b72b2009-08-11 20:47:22 +000014171 if (VT != MVT::i64)
Evan Cheng0b0cd912009-03-28 05:57:29 +000014172 return SDValue();
14173
14174 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1));
14175 if (!C)
14176 return SDValue();
14177 uint64_t MulAmt = C->getZExtValue();
14178 if (isPowerOf2_64(MulAmt) || MulAmt == 3 || MulAmt == 5 || MulAmt == 9)
14179 return SDValue();
14180
14181 uint64_t MulAmt1 = 0;
14182 uint64_t MulAmt2 = 0;
14183 if ((MulAmt % 9) == 0) {
14184 MulAmt1 = 9;
14185 MulAmt2 = MulAmt / 9;
14186 } else if ((MulAmt % 5) == 0) {
14187 MulAmt1 = 5;
14188 MulAmt2 = MulAmt / 5;
14189 } else if ((MulAmt % 3) == 0) {
14190 MulAmt1 = 3;
14191 MulAmt2 = MulAmt / 3;
14192 }
14193 if (MulAmt2 &&
14194 (isPowerOf2_64(MulAmt2) || MulAmt2 == 3 || MulAmt2 == 5 || MulAmt2 == 9)){
14195 DebugLoc DL = N->getDebugLoc();
14196
14197 if (isPowerOf2_64(MulAmt2) &&
14198 !(N->hasOneUse() && N->use_begin()->getOpcode() == ISD::ADD))
14199 // If second multiplifer is pow2, issue it first. We want the multiply by
14200 // 3, 5, or 9 to be folded into the addressing mode unless the lone use
14201 // is an add.
14202 std::swap(MulAmt1, MulAmt2);
14203
14204 SDValue NewMul;
Eric Christopherfd179292009-08-27 18:07:15 +000014205 if (isPowerOf2_64(MulAmt1))
Evan Cheng0b0cd912009-03-28 05:57:29 +000014206 NewMul = DAG.getNode(ISD::SHL, DL, VT, N->getOperand(0),
Owen Anderson825b72b2009-08-11 20:47:22 +000014207 DAG.getConstant(Log2_64(MulAmt1), MVT::i8));
Evan Cheng0b0cd912009-03-28 05:57:29 +000014208 else
Evan Cheng73f24c92009-03-30 21:36:47 +000014209 NewMul = DAG.getNode(X86ISD::MUL_IMM, DL, VT, N->getOperand(0),
Evan Cheng0b0cd912009-03-28 05:57:29 +000014210 DAG.getConstant(MulAmt1, VT));
14211
Eric Christopherfd179292009-08-27 18:07:15 +000014212 if (isPowerOf2_64(MulAmt2))
Evan Cheng0b0cd912009-03-28 05:57:29 +000014213 NewMul = DAG.getNode(ISD::SHL, DL, VT, NewMul,
Owen Anderson825b72b2009-08-11 20:47:22 +000014214 DAG.getConstant(Log2_64(MulAmt2), MVT::i8));
Eric Christopherfd179292009-08-27 18:07:15 +000014215 else
Evan Cheng73f24c92009-03-30 21:36:47 +000014216 NewMul = DAG.getNode(X86ISD::MUL_IMM, DL, VT, NewMul,
Evan Cheng0b0cd912009-03-28 05:57:29 +000014217 DAG.getConstant(MulAmt2, VT));
14218
14219 // Do not add new nodes to DAG combiner worklist.
14220 DCI.CombineTo(N, NewMul, false);
14221 }
14222 return SDValue();
14223}
14224
Evan Chengad9c0a32009-12-15 00:53:42 +000014225static SDValue PerformSHLCombine(SDNode *N, SelectionDAG &DAG) {
14226 SDValue N0 = N->getOperand(0);
14227 SDValue N1 = N->getOperand(1);
14228 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
14229 EVT VT = N0.getValueType();
14230
14231 // fold (shl (and (setcc_c), c1), c2) -> (and setcc_c, (c1 << c2))
14232 // since the result of setcc_c is all zero's or all ones.
Nadav Rotemfb0dfbb2011-10-30 13:24:22 +000014233 if (VT.isInteger() && !VT.isVector() &&
14234 N1C && N0.getOpcode() == ISD::AND &&
Evan Chengad9c0a32009-12-15 00:53:42 +000014235 N0.getOperand(1).getOpcode() == ISD::Constant) {
14236 SDValue N00 = N0.getOperand(0);
14237 if (N00.getOpcode() == X86ISD::SETCC_CARRY ||
14238 ((N00.getOpcode() == ISD::ANY_EXTEND ||
14239 N00.getOpcode() == ISD::ZERO_EXTEND) &&
14240 N00.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY)) {
14241 APInt Mask = cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
14242 APInt ShAmt = N1C->getAPIntValue();
14243 Mask = Mask.shl(ShAmt);
14244 if (Mask != 0)
14245 return DAG.getNode(ISD::AND, N->getDebugLoc(), VT,
14246 N00, DAG.getConstant(Mask, VT));
14247 }
14248 }
14249
Nadav Rotemfb0dfbb2011-10-30 13:24:22 +000014250
14251 // Hardware support for vector shifts is sparse which makes us scalarize the
14252 // vector operations in many cases. Also, on sandybridge ADD is faster than
14253 // shl.
14254 // (shl V, 1) -> add V,V
14255 if (isSplatVector(N1.getNode())) {
14256 assert(N0.getValueType().isVector() && "Invalid vector shift type");
14257 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1->getOperand(0));
14258 // We shift all of the values by one. In many cases we do not have
14259 // hardware support for this operation. This is better expressed as an ADD
14260 // of two values.
14261 if (N1C && (1 == N1C->getZExtValue())) {
14262 return DAG.getNode(ISD::ADD, N->getDebugLoc(), VT, N0, N0);
14263 }
14264 }
14265
Evan Chengad9c0a32009-12-15 00:53:42 +000014266 return SDValue();
14267}
Evan Cheng0b0cd912009-03-28 05:57:29 +000014268
Nate Begeman740ab032009-01-26 00:52:55 +000014269/// PerformShiftCombine - Transforms vector shift nodes to use vector shifts
14270/// when possible.
14271static SDValue PerformShiftCombine(SDNode* N, SelectionDAG &DAG,
Mon P Wang845b1892012-02-01 22:15:20 +000014272 TargetLowering::DAGCombinerInfo &DCI,
Nate Begeman740ab032009-01-26 00:52:55 +000014273 const X86Subtarget *Subtarget) {
Evan Chengad9c0a32009-12-15 00:53:42 +000014274 EVT VT = N->getValueType(0);
Nadav Rotemfb0dfbb2011-10-30 13:24:22 +000014275 if (N->getOpcode() == ISD::SHL) {
14276 SDValue V = PerformSHLCombine(N, DAG);
14277 if (V.getNode()) return V;
14278 }
Evan Chengad9c0a32009-12-15 00:53:42 +000014279
Nate Begeman740ab032009-01-26 00:52:55 +000014280 // On X86 with SSE2 support, we can transform this to a vector shift if
14281 // all elements are shifted by the same amount. We can't do this in legalize
14282 // because the a constant vector is typically transformed to a constant pool
14283 // so we have no knowledge of the shift amount.
Craig Topper1accb7e2012-01-10 06:54:16 +000014284 if (!Subtarget->hasSSE2())
Nate Begemanc2fd67f2009-01-26 03:15:31 +000014285 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +000014286
Craig Topper7be5dfd2011-11-12 09:58:49 +000014287 if (VT != MVT::v2i64 && VT != MVT::v4i32 && VT != MVT::v8i16 &&
14288 (!Subtarget->hasAVX2() ||
14289 (VT != MVT::v4i64 && VT != MVT::v8i32 && VT != MVT::v16i16)))
Nate Begemanc2fd67f2009-01-26 03:15:31 +000014290 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +000014291
Mon P Wang3becd092009-01-28 08:12:05 +000014292 SDValue ShAmtOp = N->getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +000014293 EVT EltVT = VT.getVectorElementType();
Chris Lattner47b4ce82009-03-11 05:48:52 +000014294 DebugLoc DL = N->getDebugLoc();
Mon P Wangefa42202009-09-03 19:56:25 +000014295 SDValue BaseShAmt = SDValue();
Mon P Wang3becd092009-01-28 08:12:05 +000014296 if (ShAmtOp.getOpcode() == ISD::BUILD_VECTOR) {
14297 unsigned NumElts = VT.getVectorNumElements();
14298 unsigned i = 0;
14299 for (; i != NumElts; ++i) {
14300 SDValue Arg = ShAmtOp.getOperand(i);
14301 if (Arg.getOpcode() == ISD::UNDEF) continue;
14302 BaseShAmt = Arg;
14303 break;
14304 }
Craig Topper37c26772012-01-17 04:44:50 +000014305 // Handle the case where the build_vector is all undef
14306 // FIXME: Should DAG allow this?
14307 if (i == NumElts)
14308 return SDValue();
14309
Mon P Wang3becd092009-01-28 08:12:05 +000014310 for (; i != NumElts; ++i) {
14311 SDValue Arg = ShAmtOp.getOperand(i);
14312 if (Arg.getOpcode() == ISD::UNDEF) continue;
14313 if (Arg != BaseShAmt) {
14314 return SDValue();
14315 }
14316 }
14317 } else if (ShAmtOp.getOpcode() == ISD::VECTOR_SHUFFLE &&
Nate Begeman9008ca62009-04-27 18:41:29 +000014318 cast<ShuffleVectorSDNode>(ShAmtOp)->isSplat()) {
Mon P Wangefa42202009-09-03 19:56:25 +000014319 SDValue InVec = ShAmtOp.getOperand(0);
14320 if (InVec.getOpcode() == ISD::BUILD_VECTOR) {
14321 unsigned NumElts = InVec.getValueType().getVectorNumElements();
14322 unsigned i = 0;
14323 for (; i != NumElts; ++i) {
14324 SDValue Arg = InVec.getOperand(i);
14325 if (Arg.getOpcode() == ISD::UNDEF) continue;
14326 BaseShAmt = Arg;
14327 break;
14328 }
14329 } else if (InVec.getOpcode() == ISD::INSERT_VECTOR_ELT) {
14330 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(InVec.getOperand(2))) {
Evan Chengae3ecf92010-02-16 21:09:44 +000014331 unsigned SplatIdx= cast<ShuffleVectorSDNode>(ShAmtOp)->getSplatIndex();
Mon P Wangefa42202009-09-03 19:56:25 +000014332 if (C->getZExtValue() == SplatIdx)
14333 BaseShAmt = InVec.getOperand(1);
14334 }
14335 }
Mon P Wang845b1892012-02-01 22:15:20 +000014336 if (BaseShAmt.getNode() == 0) {
14337 // Don't create instructions with illegal types after legalize
14338 // types has run.
14339 if (!DAG.getTargetLoweringInfo().isTypeLegal(EltVT) &&
14340 !DCI.isBeforeLegalize())
14341 return SDValue();
14342
Mon P Wangefa42202009-09-03 19:56:25 +000014343 BaseShAmt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, EltVT, ShAmtOp,
14344 DAG.getIntPtrConstant(0));
Mon P Wang845b1892012-02-01 22:15:20 +000014345 }
Mon P Wang3becd092009-01-28 08:12:05 +000014346 } else
Nate Begemanc2fd67f2009-01-26 03:15:31 +000014347 return SDValue();
Nate Begeman740ab032009-01-26 00:52:55 +000014348
Mon P Wangefa42202009-09-03 19:56:25 +000014349 // The shift amount is an i32.
Owen Anderson825b72b2009-08-11 20:47:22 +000014350 if (EltVT.bitsGT(MVT::i32))
14351 BaseShAmt = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, BaseShAmt);
14352 else if (EltVT.bitsLT(MVT::i32))
Mon P Wangefa42202009-09-03 19:56:25 +000014353 BaseShAmt = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::i32, BaseShAmt);
Nate Begeman740ab032009-01-26 00:52:55 +000014354
Nate Begemanc2fd67f2009-01-26 03:15:31 +000014355 // The shift amount is identical so we can do a vector shift.
14356 SDValue ValOp = N->getOperand(0);
14357 switch (N->getOpcode()) {
14358 default:
Torok Edwinc23197a2009-07-14 16:55:14 +000014359 llvm_unreachable("Unknown shift opcode!");
Nate Begemanc2fd67f2009-01-26 03:15:31 +000014360 case ISD::SHL:
Craig Toppered2e13d2012-01-22 19:15:14 +000014361 switch (VT.getSimpleVT().SimpleTy) {
14362 default: return SDValue();
14363 case MVT::v2i64:
14364 case MVT::v4i32:
14365 case MVT::v8i16:
14366 case MVT::v4i64:
14367 case MVT::v8i32:
14368 case MVT::v16i16:
14369 return getTargetVShiftNode(X86ISD::VSHLI, DL, VT, ValOp, BaseShAmt, DAG);
14370 }
Nate Begemanc2fd67f2009-01-26 03:15:31 +000014371 case ISD::SRA:
Craig Toppered2e13d2012-01-22 19:15:14 +000014372 switch (VT.getSimpleVT().SimpleTy) {
14373 default: return SDValue();
14374 case MVT::v4i32:
14375 case MVT::v8i16:
14376 case MVT::v8i32:
14377 case MVT::v16i16:
14378 return getTargetVShiftNode(X86ISD::VSRAI, DL, VT, ValOp, BaseShAmt, DAG);
14379 }
Nate Begemanc2fd67f2009-01-26 03:15:31 +000014380 case ISD::SRL:
Craig Toppered2e13d2012-01-22 19:15:14 +000014381 switch (VT.getSimpleVT().SimpleTy) {
14382 default: return SDValue();
14383 case MVT::v2i64:
14384 case MVT::v4i32:
14385 case MVT::v8i16:
14386 case MVT::v4i64:
14387 case MVT::v8i32:
14388 case MVT::v16i16:
14389 return getTargetVShiftNode(X86ISD::VSRLI, DL, VT, ValOp, BaseShAmt, DAG);
14390 }
Nate Begeman740ab032009-01-26 00:52:55 +000014391 }
Nate Begeman740ab032009-01-26 00:52:55 +000014392}
14393
Nate Begemanb65c1752010-12-17 22:55:37 +000014394
Stuart Hastings865f0932011-06-03 23:53:54 +000014395// CMPEQCombine - Recognize the distinctive (AND (setcc ...) (setcc ..))
14396// where both setccs reference the same FP CMP, and rewrite for CMPEQSS
14397// and friends. Likewise for OR -> CMPNEQSS.
14398static SDValue CMPEQCombine(SDNode *N, SelectionDAG &DAG,
14399 TargetLowering::DAGCombinerInfo &DCI,
14400 const X86Subtarget *Subtarget) {
14401 unsigned opcode;
14402
14403 // SSE1 supports CMP{eq|ne}SS, and SSE2 added CMP{eq|ne}SD, but
14404 // we're requiring SSE2 for both.
Craig Topper1accb7e2012-01-10 06:54:16 +000014405 if (Subtarget->hasSSE2() && isAndOrOfSetCCs(SDValue(N, 0U), opcode)) {
Stuart Hastings865f0932011-06-03 23:53:54 +000014406 SDValue N0 = N->getOperand(0);
14407 SDValue N1 = N->getOperand(1);
14408 SDValue CMP0 = N0->getOperand(1);
14409 SDValue CMP1 = N1->getOperand(1);
14410 DebugLoc DL = N->getDebugLoc();
14411
14412 // The SETCCs should both refer to the same CMP.
14413 if (CMP0.getOpcode() != X86ISD::CMP || CMP0 != CMP1)
14414 return SDValue();
14415
14416 SDValue CMP00 = CMP0->getOperand(0);
14417 SDValue CMP01 = CMP0->getOperand(1);
14418 EVT VT = CMP00.getValueType();
14419
14420 if (VT == MVT::f32 || VT == MVT::f64) {
14421 bool ExpectingFlags = false;
14422 // Check for any users that want flags:
14423 for (SDNode::use_iterator UI = N->use_begin(),
14424 UE = N->use_end();
14425 !ExpectingFlags && UI != UE; ++UI)
14426 switch (UI->getOpcode()) {
14427 default:
14428 case ISD::BR_CC:
14429 case ISD::BRCOND:
14430 case ISD::SELECT:
14431 ExpectingFlags = true;
14432 break;
14433 case ISD::CopyToReg:
14434 case ISD::SIGN_EXTEND:
14435 case ISD::ZERO_EXTEND:
14436 case ISD::ANY_EXTEND:
14437 break;
14438 }
14439
14440 if (!ExpectingFlags) {
14441 enum X86::CondCode cc0 = (enum X86::CondCode)N0.getConstantOperandVal(0);
14442 enum X86::CondCode cc1 = (enum X86::CondCode)N1.getConstantOperandVal(0);
14443
14444 if (cc1 == X86::COND_E || cc1 == X86::COND_NE) {
14445 X86::CondCode tmp = cc0;
14446 cc0 = cc1;
14447 cc1 = tmp;
14448 }
14449
14450 if ((cc0 == X86::COND_E && cc1 == X86::COND_NP) ||
14451 (cc0 == X86::COND_NE && cc1 == X86::COND_P)) {
14452 bool is64BitFP = (CMP00.getValueType() == MVT::f64);
14453 X86ISD::NodeType NTOperator = is64BitFP ?
14454 X86ISD::FSETCCsd : X86ISD::FSETCCss;
14455 // FIXME: need symbolic constants for these magic numbers.
14456 // See X86ATTInstPrinter.cpp:printSSECC().
14457 unsigned x86cc = (cc0 == X86::COND_E) ? 0 : 4;
14458 SDValue OnesOrZeroesF = DAG.getNode(NTOperator, DL, MVT::f32, CMP00, CMP01,
14459 DAG.getConstant(x86cc, MVT::i8));
14460 SDValue OnesOrZeroesI = DAG.getNode(ISD::BITCAST, DL, MVT::i32,
14461 OnesOrZeroesF);
14462 SDValue ANDed = DAG.getNode(ISD::AND, DL, MVT::i32, OnesOrZeroesI,
14463 DAG.getConstant(1, MVT::i32));
14464 SDValue OneBitOfTruth = DAG.getNode(ISD::TRUNCATE, DL, MVT::i8, ANDed);
14465 return OneBitOfTruth;
14466 }
14467 }
14468 }
14469 }
14470 return SDValue();
14471}
14472
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +000014473/// CanFoldXORWithAllOnes - Test whether the XOR operand is a AllOnes vector
14474/// so it can be folded inside ANDNP.
14475static bool CanFoldXORWithAllOnes(const SDNode *N) {
14476 EVT VT = N->getValueType(0);
14477
14478 // Match direct AllOnes for 128 and 256-bit vectors
14479 if (ISD::isBuildVectorAllOnes(N))
14480 return true;
14481
14482 // Look through a bit convert.
14483 if (N->getOpcode() == ISD::BITCAST)
14484 N = N->getOperand(0).getNode();
14485
14486 // Sometimes the operand may come from a insert_subvector building a 256-bit
14487 // allones vector
Craig Topper7a9a28b2012-08-12 02:23:29 +000014488 if (VT.is256BitVector() &&
Bill Wendling456a9252011-08-04 00:32:58 +000014489 N->getOpcode() == ISD::INSERT_SUBVECTOR) {
14490 SDValue V1 = N->getOperand(0);
14491 SDValue V2 = N->getOperand(1);
14492
14493 if (V1.getOpcode() == ISD::INSERT_SUBVECTOR &&
14494 V1.getOperand(0).getOpcode() == ISD::UNDEF &&
14495 ISD::isBuildVectorAllOnes(V1.getOperand(1).getNode()) &&
14496 ISD::isBuildVectorAllOnes(V2.getNode()))
14497 return true;
14498 }
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +000014499
14500 return false;
14501}
14502
Nate Begemanb65c1752010-12-17 22:55:37 +000014503static SDValue PerformAndCombine(SDNode *N, SelectionDAG &DAG,
14504 TargetLowering::DAGCombinerInfo &DCI,
14505 const X86Subtarget *Subtarget) {
14506 if (DCI.isBeforeLegalizeOps())
14507 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000014508
Stuart Hastings865f0932011-06-03 23:53:54 +000014509 SDValue R = CMPEQCombine(N, DAG, DCI, Subtarget);
14510 if (R.getNode())
14511 return R;
14512
Craig Topper54a11172011-10-14 07:06:56 +000014513 EVT VT = N->getValueType(0);
14514
Craig Topperb4c94572011-10-21 06:55:01 +000014515 // Create ANDN, BLSI, and BLSR instructions
14516 // BLSI is X & (-X)
14517 // BLSR is X & (X-1)
Craig Topper54a11172011-10-14 07:06:56 +000014518 if (Subtarget->hasBMI() && (VT == MVT::i32 || VT == MVT::i64)) {
14519 SDValue N0 = N->getOperand(0);
14520 SDValue N1 = N->getOperand(1);
14521 DebugLoc DL = N->getDebugLoc();
14522
14523 // Check LHS for not
14524 if (N0.getOpcode() == ISD::XOR && isAllOnes(N0.getOperand(1)))
14525 return DAG.getNode(X86ISD::ANDN, DL, VT, N0.getOperand(0), N1);
14526 // Check RHS for not
14527 if (N1.getOpcode() == ISD::XOR && isAllOnes(N1.getOperand(1)))
14528 return DAG.getNode(X86ISD::ANDN, DL, VT, N1.getOperand(0), N0);
14529
Craig Topperb4c94572011-10-21 06:55:01 +000014530 // Check LHS for neg
14531 if (N0.getOpcode() == ISD::SUB && N0.getOperand(1) == N1 &&
14532 isZero(N0.getOperand(0)))
14533 return DAG.getNode(X86ISD::BLSI, DL, VT, N1);
14534
14535 // Check RHS for neg
14536 if (N1.getOpcode() == ISD::SUB && N1.getOperand(1) == N0 &&
14537 isZero(N1.getOperand(0)))
14538 return DAG.getNode(X86ISD::BLSI, DL, VT, N0);
14539
14540 // Check LHS for X-1
14541 if (N0.getOpcode() == ISD::ADD && N0.getOperand(0) == N1 &&
14542 isAllOnes(N0.getOperand(1)))
14543 return DAG.getNode(X86ISD::BLSR, DL, VT, N1);
14544
14545 // Check RHS for X-1
14546 if (N1.getOpcode() == ISD::ADD && N1.getOperand(0) == N0 &&
14547 isAllOnes(N1.getOperand(1)))
14548 return DAG.getNode(X86ISD::BLSR, DL, VT, N0);
14549
Craig Topper54a11172011-10-14 07:06:56 +000014550 return SDValue();
14551 }
14552
Bruno Cardoso Lopes466b0222011-07-13 21:36:51 +000014553 // Want to form ANDNP nodes:
14554 // 1) In the hopes of then easily combining them with OR and AND nodes
14555 // to form PBLEND/PSIGN.
14556 // 2) To match ANDN packed intrinsics
Bruno Cardoso Lopes466b0222011-07-13 21:36:51 +000014557 if (VT != MVT::v2i64 && VT != MVT::v4i64)
Nate Begemanb65c1752010-12-17 22:55:37 +000014558 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000014559
Nate Begemanb65c1752010-12-17 22:55:37 +000014560 SDValue N0 = N->getOperand(0);
14561 SDValue N1 = N->getOperand(1);
14562 DebugLoc DL = N->getDebugLoc();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000014563
Nate Begemanb65c1752010-12-17 22:55:37 +000014564 // Check LHS for vnot
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000014565 if (N0.getOpcode() == ISD::XOR &&
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +000014566 //ISD::isBuildVectorAllOnes(N0.getOperand(1).getNode()))
14567 CanFoldXORWithAllOnes(N0.getOperand(1).getNode()))
Bruno Cardoso Lopesc1af4772011-07-13 21:36:47 +000014568 return DAG.getNode(X86ISD::ANDNP, DL, VT, N0.getOperand(0), N1);
Nate Begemanb65c1752010-12-17 22:55:37 +000014569
14570 // Check RHS for vnot
14571 if (N1.getOpcode() == ISD::XOR &&
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +000014572 //ISD::isBuildVectorAllOnes(N1.getOperand(1).getNode()))
14573 CanFoldXORWithAllOnes(N1.getOperand(1).getNode()))
Bruno Cardoso Lopesc1af4772011-07-13 21:36:47 +000014574 return DAG.getNode(X86ISD::ANDNP, DL, VT, N1.getOperand(0), N0);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000014575
Nate Begemanb65c1752010-12-17 22:55:37 +000014576 return SDValue();
14577}
14578
Evan Cheng760d1942010-01-04 21:22:48 +000014579static SDValue PerformOrCombine(SDNode *N, SelectionDAG &DAG,
Evan Cheng8b1190a2010-04-28 01:18:01 +000014580 TargetLowering::DAGCombinerInfo &DCI,
Evan Cheng760d1942010-01-04 21:22:48 +000014581 const X86Subtarget *Subtarget) {
Evan Cheng39cfeec2010-04-28 02:25:18 +000014582 if (DCI.isBeforeLegalizeOps())
Evan Cheng8b1190a2010-04-28 01:18:01 +000014583 return SDValue();
14584
Stuart Hastings865f0932011-06-03 23:53:54 +000014585 SDValue R = CMPEQCombine(N, DAG, DCI, Subtarget);
14586 if (R.getNode())
14587 return R;
14588
Evan Cheng760d1942010-01-04 21:22:48 +000014589 EVT VT = N->getValueType(0);
Evan Cheng760d1942010-01-04 21:22:48 +000014590
Evan Cheng760d1942010-01-04 21:22:48 +000014591 SDValue N0 = N->getOperand(0);
14592 SDValue N1 = N->getOperand(1);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000014593
Nate Begemanb65c1752010-12-17 22:55:37 +000014594 // look for psign/blend
Craig Topper1666cb62011-11-19 07:07:26 +000014595 if (VT == MVT::v2i64 || VT == MVT::v4i64) {
Craig Topperd0a31172012-01-10 06:37:29 +000014596 if (!Subtarget->hasSSSE3() ||
Craig Topper1666cb62011-11-19 07:07:26 +000014597 (VT == MVT::v4i64 && !Subtarget->hasAVX2()))
14598 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000014599
Craig Topper1666cb62011-11-19 07:07:26 +000014600 // Canonicalize pandn to RHS
14601 if (N0.getOpcode() == X86ISD::ANDNP)
14602 std::swap(N0, N1);
Lang Hames9ffaa6a2012-01-10 22:53:20 +000014603 // or (and (m, y), (pandn m, x))
Craig Topper1666cb62011-11-19 07:07:26 +000014604 if (N0.getOpcode() == ISD::AND && N1.getOpcode() == X86ISD::ANDNP) {
14605 SDValue Mask = N1.getOperand(0);
14606 SDValue X = N1.getOperand(1);
14607 SDValue Y;
14608 if (N0.getOperand(0) == Mask)
14609 Y = N0.getOperand(1);
14610 if (N0.getOperand(1) == Mask)
14611 Y = N0.getOperand(0);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000014612
Craig Topper1666cb62011-11-19 07:07:26 +000014613 // Check to see if the mask appeared in both the AND and ANDNP and
14614 if (!Y.getNode())
14615 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000014616
Craig Topper1666cb62011-11-19 07:07:26 +000014617 // Validate that X, Y, and Mask are BIT_CONVERTS, and see through them.
Craig Topper1666cb62011-11-19 07:07:26 +000014618 // Look through mask bitcast.
Nadav Rotem4ac90812012-04-01 19:31:22 +000014619 if (Mask.getOpcode() == ISD::BITCAST)
14620 Mask = Mask.getOperand(0);
14621 if (X.getOpcode() == ISD::BITCAST)
14622 X = X.getOperand(0);
14623 if (Y.getOpcode() == ISD::BITCAST)
14624 Y = Y.getOperand(0);
14625
Craig Topper1666cb62011-11-19 07:07:26 +000014626 EVT MaskVT = Mask.getValueType();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000014627
Craig Toppered2e13d2012-01-22 19:15:14 +000014628 // Validate that the Mask operand is a vector sra node.
Craig Topper1666cb62011-11-19 07:07:26 +000014629 // FIXME: what to do for bytes, since there is a psignb/pblendvb, but
14630 // there is no psrai.b
Craig Topper7fb8b0c2012-01-23 06:46:22 +000014631 if (Mask.getOpcode() != X86ISD::VSRAI)
Craig Toppered2e13d2012-01-22 19:15:14 +000014632 return SDValue();
Craig Topper1666cb62011-11-19 07:07:26 +000014633
14634 // Check that the SRA is all signbits.
Craig Topper7fb8b0c2012-01-23 06:46:22 +000014635 SDValue SraC = Mask.getOperand(1);
Craig Topper1666cb62011-11-19 07:07:26 +000014636 unsigned SraAmt = cast<ConstantSDNode>(SraC)->getZExtValue();
14637 unsigned EltBits = MaskVT.getVectorElementType().getSizeInBits();
14638 if ((SraAmt + 1) != EltBits)
14639 return SDValue();
14640
14641 DebugLoc DL = N->getDebugLoc();
14642
14643 // Now we know we at least have a plendvb with the mask val. See if
14644 // we can form a psignb/w/d.
14645 // psign = x.type == y.type == mask.type && y = sub(0, x);
Craig Topper1666cb62011-11-19 07:07:26 +000014646 if (Y.getOpcode() == ISD::SUB && Y.getOperand(1) == X &&
14647 ISD::isBuildVectorAllZeros(Y.getOperand(0).getNode()) &&
Craig Toppered2e13d2012-01-22 19:15:14 +000014648 X.getValueType() == MaskVT && Y.getValueType() == MaskVT) {
14649 assert((EltBits == 8 || EltBits == 16 || EltBits == 32) &&
14650 "Unsupported VT for PSIGN");
Craig Topper7fb8b0c2012-01-23 06:46:22 +000014651 Mask = DAG.getNode(X86ISD::PSIGN, DL, MaskVT, X, Mask.getOperand(0));
Craig Toppered2e13d2012-01-22 19:15:14 +000014652 return DAG.getNode(ISD::BITCAST, DL, VT, Mask);
Craig Topper1666cb62011-11-19 07:07:26 +000014653 }
14654 // PBLENDVB only available on SSE 4.1
Craig Topperd0a31172012-01-10 06:37:29 +000014655 if (!Subtarget->hasSSE41())
Craig Topper1666cb62011-11-19 07:07:26 +000014656 return SDValue();
14657
14658 EVT BlendVT = (VT == MVT::v4i64) ? MVT::v32i8 : MVT::v16i8;
14659
14660 X = DAG.getNode(ISD::BITCAST, DL, BlendVT, X);
14661 Y = DAG.getNode(ISD::BITCAST, DL, BlendVT, Y);
14662 Mask = DAG.getNode(ISD::BITCAST, DL, BlendVT, Mask);
Nadav Rotem18197d72011-11-30 10:13:37 +000014663 Mask = DAG.getNode(ISD::VSELECT, DL, BlendVT, Mask, Y, X);
Craig Topper1666cb62011-11-19 07:07:26 +000014664 return DAG.getNode(ISD::BITCAST, DL, VT, Mask);
Nate Begemanb65c1752010-12-17 22:55:37 +000014665 }
14666 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000014667
Craig Topper1666cb62011-11-19 07:07:26 +000014668 if (VT != MVT::i16 && VT != MVT::i32 && VT != MVT::i64)
14669 return SDValue();
14670
Nate Begemanb65c1752010-12-17 22:55:37 +000014671 // fold (or (x << c) | (y >> (64 - c))) ==> (shld64 x, y, c)
Evan Cheng760d1942010-01-04 21:22:48 +000014672 if (N0.getOpcode() == ISD::SRL && N1.getOpcode() == ISD::SHL)
14673 std::swap(N0, N1);
14674 if (N0.getOpcode() != ISD::SHL || N1.getOpcode() != ISD::SRL)
14675 return SDValue();
Evan Cheng8b1190a2010-04-28 01:18:01 +000014676 if (!N0.hasOneUse() || !N1.hasOneUse())
14677 return SDValue();
Evan Cheng760d1942010-01-04 21:22:48 +000014678
14679 SDValue ShAmt0 = N0.getOperand(1);
14680 if (ShAmt0.getValueType() != MVT::i8)
14681 return SDValue();
14682 SDValue ShAmt1 = N1.getOperand(1);
14683 if (ShAmt1.getValueType() != MVT::i8)
14684 return SDValue();
14685 if (ShAmt0.getOpcode() == ISD::TRUNCATE)
14686 ShAmt0 = ShAmt0.getOperand(0);
14687 if (ShAmt1.getOpcode() == ISD::TRUNCATE)
14688 ShAmt1 = ShAmt1.getOperand(0);
14689
14690 DebugLoc DL = N->getDebugLoc();
14691 unsigned Opc = X86ISD::SHLD;
14692 SDValue Op0 = N0.getOperand(0);
14693 SDValue Op1 = N1.getOperand(0);
14694 if (ShAmt0.getOpcode() == ISD::SUB) {
14695 Opc = X86ISD::SHRD;
14696 std::swap(Op0, Op1);
14697 std::swap(ShAmt0, ShAmt1);
14698 }
14699
Evan Cheng8b1190a2010-04-28 01:18:01 +000014700 unsigned Bits = VT.getSizeInBits();
Evan Cheng760d1942010-01-04 21:22:48 +000014701 if (ShAmt1.getOpcode() == ISD::SUB) {
14702 SDValue Sum = ShAmt1.getOperand(0);
14703 if (ConstantSDNode *SumC = dyn_cast<ConstantSDNode>(Sum)) {
Dan Gohman4e39e9d2010-06-24 14:30:44 +000014704 SDValue ShAmt1Op1 = ShAmt1.getOperand(1);
14705 if (ShAmt1Op1.getNode()->getOpcode() == ISD::TRUNCATE)
14706 ShAmt1Op1 = ShAmt1Op1.getOperand(0);
14707 if (SumC->getSExtValue() == Bits && ShAmt1Op1 == ShAmt0)
Evan Cheng760d1942010-01-04 21:22:48 +000014708 return DAG.getNode(Opc, DL, VT,
14709 Op0, Op1,
14710 DAG.getNode(ISD::TRUNCATE, DL,
14711 MVT::i8, ShAmt0));
14712 }
14713 } else if (ConstantSDNode *ShAmt1C = dyn_cast<ConstantSDNode>(ShAmt1)) {
14714 ConstantSDNode *ShAmt0C = dyn_cast<ConstantSDNode>(ShAmt0);
14715 if (ShAmt0C &&
Evan Cheng8b1190a2010-04-28 01:18:01 +000014716 ShAmt0C->getSExtValue() + ShAmt1C->getSExtValue() == Bits)
Evan Cheng760d1942010-01-04 21:22:48 +000014717 return DAG.getNode(Opc, DL, VT,
14718 N0.getOperand(0), N1.getOperand(0),
14719 DAG.getNode(ISD::TRUNCATE, DL,
14720 MVT::i8, ShAmt0));
14721 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000014722
Evan Cheng760d1942010-01-04 21:22:48 +000014723 return SDValue();
14724}
14725
Manman Ren92363622012-06-07 22:39:10 +000014726// Generate NEG and CMOV for integer abs.
14727static SDValue performIntegerAbsCombine(SDNode *N, SelectionDAG &DAG) {
14728 EVT VT = N->getValueType(0);
14729
14730 // Since X86 does not have CMOV for 8-bit integer, we don't convert
14731 // 8-bit integer abs to NEG and CMOV.
14732 if (VT.isInteger() && VT.getSizeInBits() == 8)
14733 return SDValue();
14734
14735 SDValue N0 = N->getOperand(0);
14736 SDValue N1 = N->getOperand(1);
14737 DebugLoc DL = N->getDebugLoc();
14738
14739 // Check pattern of XOR(ADD(X,Y), Y) where Y is SRA(X, size(X)-1)
14740 // and change it to SUB and CMOV.
14741 if (VT.isInteger() && N->getOpcode() == ISD::XOR &&
14742 N0.getOpcode() == ISD::ADD &&
14743 N0.getOperand(1) == N1 &&
14744 N1.getOpcode() == ISD::SRA &&
14745 N1.getOperand(0) == N0.getOperand(0))
14746 if (ConstantSDNode *Y1C = dyn_cast<ConstantSDNode>(N1.getOperand(1)))
14747 if (Y1C->getAPIntValue() == VT.getSizeInBits()-1) {
14748 // Generate SUB & CMOV.
14749 SDValue Neg = DAG.getNode(X86ISD::SUB, DL, DAG.getVTList(VT, MVT::i32),
14750 DAG.getConstant(0, VT), N0.getOperand(0));
14751
14752 SDValue Ops[] = { N0.getOperand(0), Neg,
14753 DAG.getConstant(X86::COND_GE, MVT::i8),
14754 SDValue(Neg.getNode(), 1) };
14755 return DAG.getNode(X86ISD::CMOV, DL, DAG.getVTList(VT, MVT::Glue),
14756 Ops, array_lengthof(Ops));
14757 }
14758 return SDValue();
14759}
14760
Craig Topper3738ccd2011-12-27 06:27:23 +000014761// PerformXorCombine - Attempts to turn XOR nodes into BLSMSK nodes
Craig Topperb4c94572011-10-21 06:55:01 +000014762static SDValue PerformXorCombine(SDNode *N, SelectionDAG &DAG,
14763 TargetLowering::DAGCombinerInfo &DCI,
14764 const X86Subtarget *Subtarget) {
14765 if (DCI.isBeforeLegalizeOps())
14766 return SDValue();
14767
Manman Ren45d53b82012-06-08 18:58:26 +000014768 if (Subtarget->hasCMov()) {
14769 SDValue RV = performIntegerAbsCombine(N, DAG);
14770 if (RV.getNode())
14771 return RV;
14772 }
Manman Ren92363622012-06-07 22:39:10 +000014773
14774 // Try forming BMI if it is available.
14775 if (!Subtarget->hasBMI())
14776 return SDValue();
14777
Craig Topperb4c94572011-10-21 06:55:01 +000014778 EVT VT = N->getValueType(0);
14779
14780 if (VT != MVT::i32 && VT != MVT::i64)
14781 return SDValue();
14782
Craig Topper3738ccd2011-12-27 06:27:23 +000014783 assert(Subtarget->hasBMI() && "Creating BLSMSK requires BMI instructions");
14784
Craig Topperb4c94572011-10-21 06:55:01 +000014785 // Create BLSMSK instructions by finding X ^ (X-1)
14786 SDValue N0 = N->getOperand(0);
14787 SDValue N1 = N->getOperand(1);
14788 DebugLoc DL = N->getDebugLoc();
14789
14790 if (N0.getOpcode() == ISD::ADD && N0.getOperand(0) == N1 &&
14791 isAllOnes(N0.getOperand(1)))
14792 return DAG.getNode(X86ISD::BLSMSK, DL, VT, N1);
14793
14794 if (N1.getOpcode() == ISD::ADD && N1.getOperand(0) == N0 &&
14795 isAllOnes(N1.getOperand(1)))
14796 return DAG.getNode(X86ISD::BLSMSK, DL, VT, N0);
14797
14798 return SDValue();
14799}
14800
Nadav Rotem91e43fd2011-09-18 10:39:32 +000014801/// PerformLOADCombine - Do target-specific dag combines on LOAD nodes.
14802static SDValue PerformLOADCombine(SDNode *N, SelectionDAG &DAG,
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000014803 TargetLowering::DAGCombinerInfo &DCI,
14804 const X86Subtarget *Subtarget) {
Nadav Rotem91e43fd2011-09-18 10:39:32 +000014805 LoadSDNode *Ld = cast<LoadSDNode>(N);
14806 EVT RegVT = Ld->getValueType(0);
14807 EVT MemVT = Ld->getMemoryVT();
14808 DebugLoc dl = Ld->getDebugLoc();
14809 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
14810
14811 ISD::LoadExtType Ext = Ld->getExtensionType();
14812
Nadav Rotemca6f2962011-09-18 19:00:23 +000014813 // If this is a vector EXT Load then attempt to optimize it using a
Nadav Rotem91e43fd2011-09-18 10:39:32 +000014814 // shuffle. We need SSE4 for the shuffles.
14815 // TODO: It is possible to support ZExt by zeroing the undef values
14816 // during the shuffle phase or after the shuffle.
Eli Friedmanda813f42011-12-28 21:24:44 +000014817 if (RegVT.isVector() && RegVT.isInteger() &&
14818 Ext == ISD::EXTLOAD && Subtarget->hasSSE41()) {
Nadav Rotem91e43fd2011-09-18 10:39:32 +000014819 assert(MemVT != RegVT && "Cannot extend to the same type");
14820 assert(MemVT.isVector() && "Must load a vector from memory");
14821
14822 unsigned NumElems = RegVT.getVectorNumElements();
14823 unsigned RegSz = RegVT.getSizeInBits();
14824 unsigned MemSz = MemVT.getSizeInBits();
14825 assert(RegSz > MemSz && "Register size must be greater than the mem size");
Nadav Rotem91e43fd2011-09-18 10:39:32 +000014826
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000014827 // All sizes must be a power of two.
14828 if (!isPowerOf2_32(RegSz * MemSz * NumElems))
14829 return SDValue();
14830
14831 // Attempt to load the original value using scalar loads.
14832 // Find the largest scalar type that divides the total loaded size.
Nadav Rotem91e43fd2011-09-18 10:39:32 +000014833 MVT SclrLoadTy = MVT::i8;
14834 for (unsigned tp = MVT::FIRST_INTEGER_VALUETYPE;
14835 tp < MVT::LAST_INTEGER_VALUETYPE; ++tp) {
14836 MVT Tp = (MVT::SimpleValueType)tp;
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000014837 if (TLI.isTypeLegal(Tp) && ((MemSz % Tp.getSizeInBits()) == 0)) {
Nadav Rotem91e43fd2011-09-18 10:39:32 +000014838 SclrLoadTy = Tp;
Nadav Rotem91e43fd2011-09-18 10:39:32 +000014839 }
14840 }
14841
Nadav Rotem5cd95e12012-07-11 13:27:05 +000014842 // On 32bit systems, we can't save 64bit integers. Try bitcasting to F64.
14843 if (TLI.isTypeLegal(MVT::f64) && SclrLoadTy.getSizeInBits() < 64 &&
14844 (64 <= MemSz))
14845 SclrLoadTy = MVT::f64;
14846
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000014847 // Calculate the number of scalar loads that we need to perform
14848 // in order to load our vector from memory.
14849 unsigned NumLoads = MemSz / SclrLoadTy.getSizeInBits();
Nadav Rotem91e43fd2011-09-18 10:39:32 +000014850
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000014851 // Represent our vector as a sequence of elements which are the
14852 // largest scalar that we can load.
Nadav Rotem91e43fd2011-09-18 10:39:32 +000014853 EVT LoadUnitVecVT = EVT::getVectorVT(*DAG.getContext(), SclrLoadTy,
14854 RegSz/SclrLoadTy.getSizeInBits());
14855
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000014856 // Represent the data using the same element type that is stored in
14857 // memory. In practice, we ''widen'' MemVT.
Nadav Rotem91e43fd2011-09-18 10:39:32 +000014858 EVT WideVecVT = EVT::getVectorVT(*DAG.getContext(), MemVT.getScalarType(),
14859 RegSz/MemVT.getScalarType().getSizeInBits());
Nadav Rotem91e43fd2011-09-18 10:39:32 +000014860
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000014861 assert(WideVecVT.getSizeInBits() == LoadUnitVecVT.getSizeInBits() &&
14862 "Invalid vector type");
Nadav Rotem91e43fd2011-09-18 10:39:32 +000014863
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000014864 // We can't shuffle using an illegal type.
14865 if (!TLI.isTypeLegal(WideVecVT))
14866 return SDValue();
14867
14868 SmallVector<SDValue, 8> Chains;
14869 SDValue Ptr = Ld->getBasePtr();
14870 SDValue Increment = DAG.getConstant(SclrLoadTy.getSizeInBits()/8,
14871 TLI.getPointerTy());
14872 SDValue Res = DAG.getUNDEF(LoadUnitVecVT);
14873
14874 for (unsigned i = 0; i < NumLoads; ++i) {
14875 // Perform a single load.
14876 SDValue ScalarLoad = DAG.getLoad(SclrLoadTy, dl, Ld->getChain(),
14877 Ptr, Ld->getPointerInfo(),
14878 Ld->isVolatile(), Ld->isNonTemporal(),
14879 Ld->isInvariant(), Ld->getAlignment());
14880 Chains.push_back(ScalarLoad.getValue(1));
14881 // Create the first element type using SCALAR_TO_VECTOR in order to avoid
14882 // another round of DAGCombining.
14883 if (i == 0)
14884 Res = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, LoadUnitVecVT, ScalarLoad);
14885 else
14886 Res = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, LoadUnitVecVT, Res,
14887 ScalarLoad, DAG.getIntPtrConstant(i));
14888
14889 Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr, Increment);
14890 }
14891
14892 SDValue TF = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &Chains[0],
14893 Chains.size());
Nadav Rotem91e43fd2011-09-18 10:39:32 +000014894
14895 // Bitcast the loaded value to a vector of the original element type, in
14896 // the size of the target vector type.
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000014897 SDValue SlicedVec = DAG.getNode(ISD::BITCAST, dl, WideVecVT, Res);
Nadav Rotem91e43fd2011-09-18 10:39:32 +000014898 unsigned SizeRatio = RegSz/MemSz;
14899
14900 // Redistribute the loaded elements into the different locations.
14901 SmallVector<int, 8> ShuffleVec(NumElems * SizeRatio, -1);
Craig Topper31a207a2012-05-04 06:39:13 +000014902 for (unsigned i = 0; i != NumElems; ++i)
14903 ShuffleVec[i*SizeRatio] = i;
Nadav Rotem91e43fd2011-09-18 10:39:32 +000014904
14905 SDValue Shuff = DAG.getVectorShuffle(WideVecVT, dl, SlicedVec,
Craig Topperdf966f62012-04-22 19:17:57 +000014906 DAG.getUNDEF(WideVecVT),
14907 &ShuffleVec[0]);
Nadav Rotem91e43fd2011-09-18 10:39:32 +000014908
14909 // Bitcast to the requested type.
14910 Shuff = DAG.getNode(ISD::BITCAST, dl, RegVT, Shuff);
14911 // Replace the original load with the new sequence
14912 // and return the new chain.
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000014913 return DCI.CombineTo(N, Shuff, TF, true);
Nadav Rotem91e43fd2011-09-18 10:39:32 +000014914 }
14915
14916 return SDValue();
14917}
14918
Chris Lattner149a4e52008-02-22 02:09:43 +000014919/// PerformSTORECombine - Do target-specific dag combines on STORE nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000014920static SDValue PerformSTORECombine(SDNode *N, SelectionDAG &DAG,
Evan Cheng536e6672009-03-12 05:59:15 +000014921 const X86Subtarget *Subtarget) {
Nadav Rotem614061b2011-08-10 19:30:14 +000014922 StoreSDNode *St = cast<StoreSDNode>(N);
14923 EVT VT = St->getValue().getValueType();
14924 EVT StVT = St->getMemoryVT();
14925 DebugLoc dl = St->getDebugLoc();
Nadav Rotem5e742a32011-08-11 16:41:21 +000014926 SDValue StoredVal = St->getOperand(1);
14927 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
14928
Nick Lewycky8a8d4792011-12-02 22:16:29 +000014929 // If we are saving a concatenation of two XMM registers, perform two stores.
Nadav Rotem87d35e82012-05-19 20:30:08 +000014930 // On Sandy Bridge, 256-bit memory operations are executed by two
14931 // 128-bit ports. However, on Haswell it is better to issue a single 256-bit
14932 // memory operation.
Craig Topper7a9a28b2012-08-12 02:23:29 +000014933 if (VT.is256BitVector() && !Subtarget->hasAVX2() &&
Craig Topperb4a8aef2012-04-27 21:05:09 +000014934 StoredVal.getNode()->getOpcode() == ISD::CONCAT_VECTORS &&
14935 StoredVal.getNumOperands() == 2) {
Nadav Rotem5e742a32011-08-11 16:41:21 +000014936 SDValue Value0 = StoredVal.getOperand(0);
14937 SDValue Value1 = StoredVal.getOperand(1);
14938
14939 SDValue Stride = DAG.getConstant(16, TLI.getPointerTy());
14940 SDValue Ptr0 = St->getBasePtr();
14941 SDValue Ptr1 = DAG.getNode(ISD::ADD, dl, Ptr0.getValueType(), Ptr0, Stride);
14942
14943 SDValue Ch0 = DAG.getStore(St->getChain(), dl, Value0, Ptr0,
14944 St->getPointerInfo(), St->isVolatile(),
14945 St->isNonTemporal(), St->getAlignment());
14946 SDValue Ch1 = DAG.getStore(St->getChain(), dl, Value1, Ptr1,
14947 St->getPointerInfo(), St->isVolatile(),
14948 St->isNonTemporal(), St->getAlignment());
14949 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Ch0, Ch1);
14950 }
Nadav Rotem614061b2011-08-10 19:30:14 +000014951
14952 // Optimize trunc store (of multiple scalars) to shuffle and store.
14953 // First, pack all of the elements in one place. Next, store to memory
14954 // in fewer chunks.
14955 if (St->isTruncatingStore() && VT.isVector()) {
14956 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
14957 unsigned NumElems = VT.getVectorNumElements();
14958 assert(StVT != VT && "Cannot truncate to the same type");
14959 unsigned FromSz = VT.getVectorElementType().getSizeInBits();
14960 unsigned ToSz = StVT.getVectorElementType().getSizeInBits();
14961
14962 // From, To sizes and ElemCount must be pow of two
14963 if (!isPowerOf2_32(NumElems * FromSz * ToSz)) return SDValue();
Nadav Rotem9c6cdf42011-09-21 08:45:10 +000014964 // We are going to use the original vector elt for storing.
Nadav Rotem64ac73b2011-09-21 17:14:40 +000014965 // Accumulated smaller vector elements must be a multiple of the store size.
Nadav Rotem9c6cdf42011-09-21 08:45:10 +000014966 if (0 != (NumElems * FromSz) % ToSz) return SDValue();
Nadav Rotem91e43fd2011-09-18 10:39:32 +000014967
Nadav Rotem614061b2011-08-10 19:30:14 +000014968 unsigned SizeRatio = FromSz / ToSz;
14969
14970 assert(SizeRatio * NumElems * ToSz == VT.getSizeInBits());
14971
14972 // Create a type on which we perform the shuffle
14973 EVT WideVecVT = EVT::getVectorVT(*DAG.getContext(),
14974 StVT.getScalarType(), NumElems*SizeRatio);
14975
14976 assert(WideVecVT.getSizeInBits() == VT.getSizeInBits());
14977
14978 SDValue WideVec = DAG.getNode(ISD::BITCAST, dl, WideVecVT, St->getValue());
14979 SmallVector<int, 8> ShuffleVec(NumElems * SizeRatio, -1);
Craig Topper31a207a2012-05-04 06:39:13 +000014980 for (unsigned i = 0; i != NumElems; ++i)
14981 ShuffleVec[i] = i * SizeRatio;
Nadav Rotem614061b2011-08-10 19:30:14 +000014982
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000014983 // Can't shuffle using an illegal type.
14984 if (!TLI.isTypeLegal(WideVecVT))
14985 return SDValue();
Nadav Rotem614061b2011-08-10 19:30:14 +000014986
14987 SDValue Shuff = DAG.getVectorShuffle(WideVecVT, dl, WideVec,
Craig Topperdf966f62012-04-22 19:17:57 +000014988 DAG.getUNDEF(WideVecVT),
14989 &ShuffleVec[0]);
Nadav Rotem614061b2011-08-10 19:30:14 +000014990 // At this point all of the data is stored at the bottom of the
14991 // register. We now need to save it to mem.
14992
14993 // Find the largest store unit
14994 MVT StoreType = MVT::i8;
14995 for (unsigned tp = MVT::FIRST_INTEGER_VALUETYPE;
14996 tp < MVT::LAST_INTEGER_VALUETYPE; ++tp) {
14997 MVT Tp = (MVT::SimpleValueType)tp;
Nadav Rotem5cd95e12012-07-11 13:27:05 +000014998 if (TLI.isTypeLegal(Tp) && Tp.getSizeInBits() <= NumElems * ToSz)
Nadav Rotem614061b2011-08-10 19:30:14 +000014999 StoreType = Tp;
15000 }
15001
Nadav Rotem5cd95e12012-07-11 13:27:05 +000015002 // On 32bit systems, we can't save 64bit integers. Try bitcasting to F64.
15003 if (TLI.isTypeLegal(MVT::f64) && StoreType.getSizeInBits() < 64 &&
15004 (64 <= NumElems * ToSz))
15005 StoreType = MVT::f64;
15006
Nadav Rotem614061b2011-08-10 19:30:14 +000015007 // Bitcast the original vector into a vector of store-size units
15008 EVT StoreVecVT = EVT::getVectorVT(*DAG.getContext(),
Nadav Rotem5cd95e12012-07-11 13:27:05 +000015009 StoreType, VT.getSizeInBits()/StoreType.getSizeInBits());
Nadav Rotem614061b2011-08-10 19:30:14 +000015010 assert(StoreVecVT.getSizeInBits() == VT.getSizeInBits());
15011 SDValue ShuffWide = DAG.getNode(ISD::BITCAST, dl, StoreVecVT, Shuff);
15012 SmallVector<SDValue, 8> Chains;
15013 SDValue Increment = DAG.getConstant(StoreType.getSizeInBits()/8,
15014 TLI.getPointerTy());
15015 SDValue Ptr = St->getBasePtr();
15016
15017 // Perform one or more big stores into memory.
Craig Topper31a207a2012-05-04 06:39:13 +000015018 for (unsigned i=0, e=(ToSz*NumElems)/StoreType.getSizeInBits(); i!=e; ++i) {
Nadav Rotem614061b2011-08-10 19:30:14 +000015019 SDValue SubVec = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl,
15020 StoreType, ShuffWide,
15021 DAG.getIntPtrConstant(i));
15022 SDValue Ch = DAG.getStore(St->getChain(), dl, SubVec, Ptr,
15023 St->getPointerInfo(), St->isVolatile(),
15024 St->isNonTemporal(), St->getAlignment());
15025 Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr, Increment);
15026 Chains.push_back(Ch);
15027 }
15028
15029 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &Chains[0],
15030 Chains.size());
15031 }
15032
15033
Chris Lattner149a4e52008-02-22 02:09:43 +000015034 // Turn load->store of MMX types into GPR load/stores. This avoids clobbering
15035 // the FP state in cases where an emms may be missing.
Dale Johannesen079f2a62008-02-25 19:20:14 +000015036 // A preferable solution to the general problem is to figure out the right
15037 // places to insert EMMS. This qualifies as a quick hack.
Evan Cheng536e6672009-03-12 05:59:15 +000015038
15039 // Similarly, turn load->store of i64 into double load/stores in 32-bit mode.
Evan Cheng536e6672009-03-12 05:59:15 +000015040 if (VT.getSizeInBits() != 64)
15041 return SDValue();
15042
Devang Patel578efa92009-06-05 21:57:13 +000015043 const Function *F = DAG.getMachineFunction().getFunction();
15044 bool NoImplicitFloatOps = F->hasFnAttr(Attribute::NoImplicitFloat);
Nick Lewycky8a8d4792011-12-02 22:16:29 +000015045 bool F64IsLegal = !DAG.getTarget().Options.UseSoftFloat && !NoImplicitFloatOps
Craig Topper1accb7e2012-01-10 06:54:16 +000015046 && Subtarget->hasSSE2();
Evan Cheng536e6672009-03-12 05:59:15 +000015047 if ((VT.isVector() ||
Owen Anderson825b72b2009-08-11 20:47:22 +000015048 (VT == MVT::i64 && F64IsLegal && !Subtarget->is64Bit())) &&
Dale Johannesen079f2a62008-02-25 19:20:14 +000015049 isa<LoadSDNode>(St->getValue()) &&
15050 !cast<LoadSDNode>(St->getValue())->isVolatile() &&
15051 St->getChain().hasOneUse() && !St->isVolatile()) {
Gabor Greifba36cb52008-08-28 21:40:38 +000015052 SDNode* LdVal = St->getValue().getNode();
Dale Johannesen079f2a62008-02-25 19:20:14 +000015053 LoadSDNode *Ld = 0;
15054 int TokenFactorIndex = -1;
Dan Gohman475871a2008-07-27 21:46:04 +000015055 SmallVector<SDValue, 8> Ops;
Gabor Greifba36cb52008-08-28 21:40:38 +000015056 SDNode* ChainVal = St->getChain().getNode();
Dale Johannesen079f2a62008-02-25 19:20:14 +000015057 // Must be a store of a load. We currently handle two cases: the load
15058 // is a direct child, and it's under an intervening TokenFactor. It is
15059 // possible to dig deeper under nested TokenFactors.
Dale Johannesen14e2ea92008-02-25 22:29:22 +000015060 if (ChainVal == LdVal)
Dale Johannesen079f2a62008-02-25 19:20:14 +000015061 Ld = cast<LoadSDNode>(St->getChain());
15062 else if (St->getValue().hasOneUse() &&
15063 ChainVal->getOpcode() == ISD::TokenFactor) {
Chad Rosierc2348d52012-02-01 18:45:51 +000015064 for (unsigned i = 0, e = ChainVal->getNumOperands(); i != e; ++i) {
Gabor Greifba36cb52008-08-28 21:40:38 +000015065 if (ChainVal->getOperand(i).getNode() == LdVal) {
Dale Johannesen079f2a62008-02-25 19:20:14 +000015066 TokenFactorIndex = i;
15067 Ld = cast<LoadSDNode>(St->getValue());
15068 } else
15069 Ops.push_back(ChainVal->getOperand(i));
15070 }
15071 }
Dale Johannesen079f2a62008-02-25 19:20:14 +000015072
Evan Cheng536e6672009-03-12 05:59:15 +000015073 if (!Ld || !ISD::isNormalLoad(Ld))
15074 return SDValue();
Dale Johannesen079f2a62008-02-25 19:20:14 +000015075
Evan Cheng536e6672009-03-12 05:59:15 +000015076 // If this is not the MMX case, i.e. we are just turning i64 load/store
15077 // into f64 load/store, avoid the transformation if there are multiple
15078 // uses of the loaded value.
15079 if (!VT.isVector() && !Ld->hasNUsesOfValue(1, 0))
15080 return SDValue();
Dale Johannesen079f2a62008-02-25 19:20:14 +000015081
Evan Cheng536e6672009-03-12 05:59:15 +000015082 DebugLoc LdDL = Ld->getDebugLoc();
15083 DebugLoc StDL = N->getDebugLoc();
15084 // If we are a 64-bit capable x86, lower to a single movq load/store pair.
15085 // Otherwise, if it's legal to use f64 SSE instructions, use f64 load/store
15086 // pair instead.
15087 if (Subtarget->is64Bit() || F64IsLegal) {
Owen Anderson825b72b2009-08-11 20:47:22 +000015088 EVT LdVT = Subtarget->is64Bit() ? MVT::i64 : MVT::f64;
Chris Lattner51abfe42010-09-21 06:02:19 +000015089 SDValue NewLd = DAG.getLoad(LdVT, LdDL, Ld->getChain(), Ld->getBasePtr(),
15090 Ld->getPointerInfo(), Ld->isVolatile(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000015091 Ld->isNonTemporal(), Ld->isInvariant(),
15092 Ld->getAlignment());
Evan Cheng536e6672009-03-12 05:59:15 +000015093 SDValue NewChain = NewLd.getValue(1);
Dale Johannesen079f2a62008-02-25 19:20:14 +000015094 if (TokenFactorIndex != -1) {
Evan Cheng536e6672009-03-12 05:59:15 +000015095 Ops.push_back(NewChain);
Owen Anderson825b72b2009-08-11 20:47:22 +000015096 NewChain = DAG.getNode(ISD::TokenFactor, LdDL, MVT::Other, &Ops[0],
Dale Johannesen079f2a62008-02-25 19:20:14 +000015097 Ops.size());
15098 }
Evan Cheng536e6672009-03-12 05:59:15 +000015099 return DAG.getStore(NewChain, StDL, NewLd, St->getBasePtr(),
Chris Lattner51abfe42010-09-21 06:02:19 +000015100 St->getPointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +000015101 St->isVolatile(), St->isNonTemporal(),
15102 St->getAlignment());
Chris Lattner149a4e52008-02-22 02:09:43 +000015103 }
Evan Cheng536e6672009-03-12 05:59:15 +000015104
15105 // Otherwise, lower to two pairs of 32-bit loads / stores.
15106 SDValue LoAddr = Ld->getBasePtr();
Owen Anderson825b72b2009-08-11 20:47:22 +000015107 SDValue HiAddr = DAG.getNode(ISD::ADD, LdDL, MVT::i32, LoAddr,
15108 DAG.getConstant(4, MVT::i32));
Evan Cheng536e6672009-03-12 05:59:15 +000015109
Owen Anderson825b72b2009-08-11 20:47:22 +000015110 SDValue LoLd = DAG.getLoad(MVT::i32, LdDL, Ld->getChain(), LoAddr,
Chris Lattner51abfe42010-09-21 06:02:19 +000015111 Ld->getPointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +000015112 Ld->isVolatile(), Ld->isNonTemporal(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000015113 Ld->isInvariant(), Ld->getAlignment());
Owen Anderson825b72b2009-08-11 20:47:22 +000015114 SDValue HiLd = DAG.getLoad(MVT::i32, LdDL, Ld->getChain(), HiAddr,
Chris Lattner51abfe42010-09-21 06:02:19 +000015115 Ld->getPointerInfo().getWithOffset(4),
David Greene67c9d422010-02-15 16:53:33 +000015116 Ld->isVolatile(), Ld->isNonTemporal(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000015117 Ld->isInvariant(),
Evan Cheng536e6672009-03-12 05:59:15 +000015118 MinAlign(Ld->getAlignment(), 4));
15119
15120 SDValue NewChain = LoLd.getValue(1);
15121 if (TokenFactorIndex != -1) {
15122 Ops.push_back(LoLd);
15123 Ops.push_back(HiLd);
Owen Anderson825b72b2009-08-11 20:47:22 +000015124 NewChain = DAG.getNode(ISD::TokenFactor, LdDL, MVT::Other, &Ops[0],
Evan Cheng536e6672009-03-12 05:59:15 +000015125 Ops.size());
15126 }
15127
15128 LoAddr = St->getBasePtr();
Owen Anderson825b72b2009-08-11 20:47:22 +000015129 HiAddr = DAG.getNode(ISD::ADD, StDL, MVT::i32, LoAddr,
15130 DAG.getConstant(4, MVT::i32));
Evan Cheng536e6672009-03-12 05:59:15 +000015131
15132 SDValue LoSt = DAG.getStore(NewChain, StDL, LoLd, LoAddr,
Chris Lattner8026a9d2010-09-21 17:50:43 +000015133 St->getPointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +000015134 St->isVolatile(), St->isNonTemporal(),
15135 St->getAlignment());
Evan Cheng536e6672009-03-12 05:59:15 +000015136 SDValue HiSt = DAG.getStore(NewChain, StDL, HiLd, HiAddr,
Chris Lattner8026a9d2010-09-21 17:50:43 +000015137 St->getPointerInfo().getWithOffset(4),
Evan Cheng536e6672009-03-12 05:59:15 +000015138 St->isVolatile(),
David Greene67c9d422010-02-15 16:53:33 +000015139 St->isNonTemporal(),
Evan Cheng536e6672009-03-12 05:59:15 +000015140 MinAlign(St->getAlignment(), 4));
Owen Anderson825b72b2009-08-11 20:47:22 +000015141 return DAG.getNode(ISD::TokenFactor, StDL, MVT::Other, LoSt, HiSt);
Chris Lattner149a4e52008-02-22 02:09:43 +000015142 }
Dan Gohman475871a2008-07-27 21:46:04 +000015143 return SDValue();
Chris Lattner149a4e52008-02-22 02:09:43 +000015144}
15145
Duncan Sands17470be2011-09-22 20:15:48 +000015146/// isHorizontalBinOp - Return 'true' if this vector operation is "horizontal"
15147/// and return the operands for the horizontal operation in LHS and RHS. A
15148/// horizontal operation performs the binary operation on successive elements
15149/// of its first operand, then on successive elements of its second operand,
15150/// returning the resulting values in a vector. For example, if
15151/// A = < float a0, float a1, float a2, float a3 >
15152/// and
15153/// B = < float b0, float b1, float b2, float b3 >
15154/// then the result of doing a horizontal operation on A and B is
15155/// A horizontal-op B = < a0 op a1, a2 op a3, b0 op b1, b2 op b3 >.
15156/// In short, LHS and RHS are inspected to see if LHS op RHS is of the form
15157/// A horizontal-op B, for some already available A and B, and if so then LHS is
15158/// set to A, RHS to B, and the routine returns 'true'.
15159/// Note that the binary operation should have the property that if one of the
15160/// operands is UNDEF then the result is UNDEF.
Craig Topperbeabc6c2011-12-05 06:56:46 +000015161static bool isHorizontalBinOp(SDValue &LHS, SDValue &RHS, bool IsCommutative) {
Duncan Sands17470be2011-09-22 20:15:48 +000015162 // Look for the following pattern: if
15163 // A = < float a0, float a1, float a2, float a3 >
15164 // B = < float b0, float b1, float b2, float b3 >
15165 // and
15166 // LHS = VECTOR_SHUFFLE A, B, <0, 2, 4, 6>
15167 // RHS = VECTOR_SHUFFLE A, B, <1, 3, 5, 7>
15168 // then LHS op RHS = < a0 op a1, a2 op a3, b0 op b1, b2 op b3 >
15169 // which is A horizontal-op B.
15170
15171 // At least one of the operands should be a vector shuffle.
15172 if (LHS.getOpcode() != ISD::VECTOR_SHUFFLE &&
15173 RHS.getOpcode() != ISD::VECTOR_SHUFFLE)
15174 return false;
15175
15176 EVT VT = LHS.getValueType();
Craig Topperf8363302011-12-02 08:18:41 +000015177
15178 assert((VT.is128BitVector() || VT.is256BitVector()) &&
15179 "Unsupported vector type for horizontal add/sub");
15180
15181 // Handle 128 and 256-bit vector lengths. AVX defines horizontal add/sub to
15182 // operate independently on 128-bit lanes.
Craig Topperb72039c2011-11-30 09:10:50 +000015183 unsigned NumElts = VT.getVectorNumElements();
15184 unsigned NumLanes = VT.getSizeInBits()/128;
15185 unsigned NumLaneElts = NumElts / NumLanes;
Craig Topperf8363302011-12-02 08:18:41 +000015186 assert((NumLaneElts % 2 == 0) &&
15187 "Vector type should have an even number of elements in each lane");
15188 unsigned HalfLaneElts = NumLaneElts/2;
Duncan Sands17470be2011-09-22 20:15:48 +000015189
15190 // View LHS in the form
15191 // LHS = VECTOR_SHUFFLE A, B, LMask
15192 // If LHS is not a shuffle then pretend it is the shuffle
15193 // LHS = VECTOR_SHUFFLE LHS, undef, <0, 1, ..., N-1>
15194 // NOTE: in what follows a default initialized SDValue represents an UNDEF of
15195 // type VT.
15196 SDValue A, B;
Craig Topperb72039c2011-11-30 09:10:50 +000015197 SmallVector<int, 16> LMask(NumElts);
Duncan Sands17470be2011-09-22 20:15:48 +000015198 if (LHS.getOpcode() == ISD::VECTOR_SHUFFLE) {
15199 if (LHS.getOperand(0).getOpcode() != ISD::UNDEF)
15200 A = LHS.getOperand(0);
15201 if (LHS.getOperand(1).getOpcode() != ISD::UNDEF)
15202 B = LHS.getOperand(1);
Benjamin Kramered4c8c62012-01-15 13:16:05 +000015203 ArrayRef<int> Mask = cast<ShuffleVectorSDNode>(LHS.getNode())->getMask();
15204 std::copy(Mask.begin(), Mask.end(), LMask.begin());
Duncan Sands17470be2011-09-22 20:15:48 +000015205 } else {
15206 if (LHS.getOpcode() != ISD::UNDEF)
15207 A = LHS;
Craig Topperb72039c2011-11-30 09:10:50 +000015208 for (unsigned i = 0; i != NumElts; ++i)
Duncan Sands17470be2011-09-22 20:15:48 +000015209 LMask[i] = i;
15210 }
15211
15212 // Likewise, view RHS in the form
15213 // RHS = VECTOR_SHUFFLE C, D, RMask
15214 SDValue C, D;
Craig Topperb72039c2011-11-30 09:10:50 +000015215 SmallVector<int, 16> RMask(NumElts);
Duncan Sands17470be2011-09-22 20:15:48 +000015216 if (RHS.getOpcode() == ISD::VECTOR_SHUFFLE) {
15217 if (RHS.getOperand(0).getOpcode() != ISD::UNDEF)
15218 C = RHS.getOperand(0);
15219 if (RHS.getOperand(1).getOpcode() != ISD::UNDEF)
15220 D = RHS.getOperand(1);
Benjamin Kramered4c8c62012-01-15 13:16:05 +000015221 ArrayRef<int> Mask = cast<ShuffleVectorSDNode>(RHS.getNode())->getMask();
15222 std::copy(Mask.begin(), Mask.end(), RMask.begin());
Duncan Sands17470be2011-09-22 20:15:48 +000015223 } else {
15224 if (RHS.getOpcode() != ISD::UNDEF)
15225 C = RHS;
Craig Topperb72039c2011-11-30 09:10:50 +000015226 for (unsigned i = 0; i != NumElts; ++i)
Duncan Sands17470be2011-09-22 20:15:48 +000015227 RMask[i] = i;
15228 }
15229
15230 // Check that the shuffles are both shuffling the same vectors.
15231 if (!(A == C && B == D) && !(A == D && B == C))
15232 return false;
15233
15234 // If everything is UNDEF then bail out: it would be better to fold to UNDEF.
15235 if (!A.getNode() && !B.getNode())
15236 return false;
15237
15238 // If A and B occur in reverse order in RHS, then "swap" them (which means
15239 // rewriting the mask).
15240 if (A != C)
Craig Topperbeabc6c2011-12-05 06:56:46 +000015241 CommuteVectorShuffleMask(RMask, NumElts);
Duncan Sands17470be2011-09-22 20:15:48 +000015242
15243 // At this point LHS and RHS are equivalent to
15244 // LHS = VECTOR_SHUFFLE A, B, LMask
15245 // RHS = VECTOR_SHUFFLE A, B, RMask
15246 // Check that the masks correspond to performing a horizontal operation.
Craig Topperf8363302011-12-02 08:18:41 +000015247 for (unsigned i = 0; i != NumElts; ++i) {
Craig Topperbeabc6c2011-12-05 06:56:46 +000015248 int LIdx = LMask[i], RIdx = RMask[i];
Duncan Sands17470be2011-09-22 20:15:48 +000015249
Craig Topperf8363302011-12-02 08:18:41 +000015250 // Ignore any UNDEF components.
Craig Topperbeabc6c2011-12-05 06:56:46 +000015251 if (LIdx < 0 || RIdx < 0 ||
15252 (!A.getNode() && (LIdx < (int)NumElts || RIdx < (int)NumElts)) ||
15253 (!B.getNode() && (LIdx >= (int)NumElts || RIdx >= (int)NumElts)))
Craig Topperf8363302011-12-02 08:18:41 +000015254 continue;
Duncan Sands17470be2011-09-22 20:15:48 +000015255
Craig Topperf8363302011-12-02 08:18:41 +000015256 // Check that successive elements are being operated on. If not, this is
15257 // not a horizontal operation.
15258 unsigned Src = (i/HalfLaneElts) % 2; // each lane is split between srcs
15259 unsigned LaneStart = (i/NumLaneElts) * NumLaneElts;
Craig Topperbeabc6c2011-12-05 06:56:46 +000015260 int Index = 2*(i%HalfLaneElts) + NumElts*Src + LaneStart;
Craig Topperf8363302011-12-02 08:18:41 +000015261 if (!(LIdx == Index && RIdx == Index + 1) &&
Craig Topperbeabc6c2011-12-05 06:56:46 +000015262 !(IsCommutative && LIdx == Index + 1 && RIdx == Index))
Craig Topperf8363302011-12-02 08:18:41 +000015263 return false;
Duncan Sands17470be2011-09-22 20:15:48 +000015264 }
15265
15266 LHS = A.getNode() ? A : B; // If A is 'UNDEF', use B for it.
15267 RHS = B.getNode() ? B : A; // If B is 'UNDEF', use A for it.
15268 return true;
15269}
15270
15271/// PerformFADDCombine - Do target-specific dag combines on floating point adds.
15272static SDValue PerformFADDCombine(SDNode *N, SelectionDAG &DAG,
15273 const X86Subtarget *Subtarget) {
15274 EVT VT = N->getValueType(0);
15275 SDValue LHS = N->getOperand(0);
15276 SDValue RHS = N->getOperand(1);
15277
15278 // Try to synthesize horizontal adds from adds of shuffles.
Craig Topperd0a31172012-01-10 06:37:29 +000015279 if (((Subtarget->hasSSE3() && (VT == MVT::v4f32 || VT == MVT::v2f64)) ||
Craig Topper138a5c62011-12-02 07:16:01 +000015280 (Subtarget->hasAVX() && (VT == MVT::v8f32 || VT == MVT::v4f64))) &&
Duncan Sands17470be2011-09-22 20:15:48 +000015281 isHorizontalBinOp(LHS, RHS, true))
15282 return DAG.getNode(X86ISD::FHADD, N->getDebugLoc(), VT, LHS, RHS);
15283 return SDValue();
15284}
15285
15286/// PerformFSUBCombine - Do target-specific dag combines on floating point subs.
15287static SDValue PerformFSUBCombine(SDNode *N, SelectionDAG &DAG,
15288 const X86Subtarget *Subtarget) {
15289 EVT VT = N->getValueType(0);
15290 SDValue LHS = N->getOperand(0);
15291 SDValue RHS = N->getOperand(1);
15292
15293 // Try to synthesize horizontal subs from subs of shuffles.
Craig Topperd0a31172012-01-10 06:37:29 +000015294 if (((Subtarget->hasSSE3() && (VT == MVT::v4f32 || VT == MVT::v2f64)) ||
Craig Topper138a5c62011-12-02 07:16:01 +000015295 (Subtarget->hasAVX() && (VT == MVT::v8f32 || VT == MVT::v4f64))) &&
Duncan Sands17470be2011-09-22 20:15:48 +000015296 isHorizontalBinOp(LHS, RHS, false))
15297 return DAG.getNode(X86ISD::FHSUB, N->getDebugLoc(), VT, LHS, RHS);
15298 return SDValue();
15299}
15300
Chris Lattner6cf73262008-01-25 06:14:17 +000015301/// PerformFORCombine - Do target-specific dag combines on X86ISD::FOR and
15302/// X86ISD::FXOR nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000015303static SDValue PerformFORCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattner6cf73262008-01-25 06:14:17 +000015304 assert(N->getOpcode() == X86ISD::FOR || N->getOpcode() == X86ISD::FXOR);
15305 // F[X]OR(0.0, x) -> x
15306 // F[X]OR(x, 0.0) -> x
Chris Lattneraf723b92008-01-25 05:46:26 +000015307 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
15308 if (C->getValueAPF().isPosZero())
15309 return N->getOperand(1);
15310 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
15311 if (C->getValueAPF().isPosZero())
15312 return N->getOperand(0);
Dan Gohman475871a2008-07-27 21:46:04 +000015313 return SDValue();
Chris Lattneraf723b92008-01-25 05:46:26 +000015314}
15315
15316/// PerformFANDCombine - Do target-specific dag combines on X86ISD::FAND nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000015317static SDValue PerformFANDCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattneraf723b92008-01-25 05:46:26 +000015318 // FAND(0.0, x) -> 0.0
15319 // FAND(x, 0.0) -> 0.0
15320 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
15321 if (C->getValueAPF().isPosZero())
15322 return N->getOperand(0);
15323 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
15324 if (C->getValueAPF().isPosZero())
15325 return N->getOperand(1);
Dan Gohman475871a2008-07-27 21:46:04 +000015326 return SDValue();
Chris Lattneraf723b92008-01-25 05:46:26 +000015327}
15328
Dan Gohmane5af2d32009-01-29 01:59:02 +000015329static SDValue PerformBTCombine(SDNode *N,
15330 SelectionDAG &DAG,
15331 TargetLowering::DAGCombinerInfo &DCI) {
15332 // BT ignores high bits in the bit index operand.
15333 SDValue Op1 = N->getOperand(1);
15334 if (Op1.hasOneUse()) {
15335 unsigned BitWidth = Op1.getValueSizeInBits();
15336 APInt DemandedMask = APInt::getLowBitsSet(BitWidth, Log2_32(BitWidth));
15337 APInt KnownZero, KnownOne;
Evan Chenge5b51ac2010-04-17 06:13:15 +000015338 TargetLowering::TargetLoweringOpt TLO(DAG, !DCI.isBeforeLegalize(),
15339 !DCI.isBeforeLegalizeOps());
Dan Gohmand858e902010-04-17 15:26:15 +000015340 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Dan Gohmane5af2d32009-01-29 01:59:02 +000015341 if (TLO.ShrinkDemandedConstant(Op1, DemandedMask) ||
15342 TLI.SimplifyDemandedBits(Op1, DemandedMask, KnownZero, KnownOne, TLO))
15343 DCI.CommitTargetLoweringOpt(TLO);
15344 }
15345 return SDValue();
15346}
Chris Lattner83e6c992006-10-04 06:57:07 +000015347
Eli Friedman7a5e5552009-06-07 06:52:44 +000015348static SDValue PerformVZEXT_MOVLCombine(SDNode *N, SelectionDAG &DAG) {
15349 SDValue Op = N->getOperand(0);
Wesley Peckbf17cfa2010-11-23 03:31:01 +000015350 if (Op.getOpcode() == ISD::BITCAST)
Eli Friedman7a5e5552009-06-07 06:52:44 +000015351 Op = Op.getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +000015352 EVT VT = N->getValueType(0), OpVT = Op.getValueType();
Eli Friedman7a5e5552009-06-07 06:52:44 +000015353 if (Op.getOpcode() == X86ISD::VZEXT_LOAD &&
Eric Christopherfd179292009-08-27 18:07:15 +000015354 VT.getVectorElementType().getSizeInBits() ==
Eli Friedman7a5e5552009-06-07 06:52:44 +000015355 OpVT.getVectorElementType().getSizeInBits()) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +000015356 return DAG.getNode(ISD::BITCAST, N->getDebugLoc(), VT, Op);
Eli Friedman7a5e5552009-06-07 06:52:44 +000015357 }
15358 return SDValue();
15359}
15360
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +000015361static SDValue PerformSExtCombine(SDNode *N, SelectionDAG &DAG,
15362 TargetLowering::DAGCombinerInfo &DCI,
15363 const X86Subtarget *Subtarget) {
15364 if (!DCI.isBeforeLegalizeOps())
15365 return SDValue();
15366
Craig Topper3ef43cf2012-04-24 06:36:35 +000015367 if (!Subtarget->hasAVX())
Elena Demikhovskyf6020402012-02-08 08:37:26 +000015368 return SDValue();
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +000015369
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +000015370 EVT VT = N->getValueType(0);
15371 SDValue Op = N->getOperand(0);
15372 EVT OpVT = Op.getValueType();
15373 DebugLoc dl = N->getDebugLoc();
15374
Elena Demikhovskyf6020402012-02-08 08:37:26 +000015375 if ((VT == MVT::v4i64 && OpVT == MVT::v4i32) ||
15376 (VT == MVT::v8i32 && OpVT == MVT::v8i16)) {
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +000015377
Craig Topper3ef43cf2012-04-24 06:36:35 +000015378 if (Subtarget->hasAVX2())
Elena Demikhovsky1da58672012-04-22 09:39:03 +000015379 return DAG.getNode(X86ISD::VSEXT_MOVL, dl, VT, Op);
Elena Demikhovsky1da58672012-04-22 09:39:03 +000015380
15381 // Optimize vectors in AVX mode
15382 // Sign extend v8i16 to v8i32 and
15383 // v4i32 to v4i64
15384 //
15385 // Divide input vector into two parts
15386 // for v4i32 the shuffle mask will be { 0, 1, -1, -1} {2, 3, -1, -1}
15387 // use vpmovsx instruction to extend v4i32 -> v2i64; v8i16 -> v4i32
15388 // concat the vectors to original VT
15389
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +000015390 unsigned NumElems = OpVT.getVectorNumElements();
Craig Toppercacafd42012-08-14 08:18:43 +000015391 SDValue Undef = DAG.getUNDEF(OpVT);
15392
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +000015393 SmallVector<int,8> ShufMask1(NumElems, -1);
Craig Topper3ef43cf2012-04-24 06:36:35 +000015394 for (unsigned i = 0; i != NumElems/2; ++i)
15395 ShufMask1[i] = i;
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +000015396
Craig Toppercacafd42012-08-14 08:18:43 +000015397 SDValue OpLo = DAG.getVectorShuffle(OpVT, dl, Op, Undef, &ShufMask1[0]);
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +000015398
15399 SmallVector<int,8> ShufMask2(NumElems, -1);
Craig Topper3ef43cf2012-04-24 06:36:35 +000015400 for (unsigned i = 0; i != NumElems/2; ++i)
15401 ShufMask2[i] = i + NumElems/2;
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +000015402
Craig Toppercacafd42012-08-14 08:18:43 +000015403 SDValue OpHi = DAG.getVectorShuffle(OpVT, dl, Op, Undef, &ShufMask2[0]);
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +000015404
Craig Topper3ef43cf2012-04-24 06:36:35 +000015405 EVT HalfVT = EVT::getVectorVT(*DAG.getContext(), VT.getScalarType(),
Elena Demikhovskyf6020402012-02-08 08:37:26 +000015406 VT.getVectorNumElements()/2);
15407
Craig Topper3ef43cf2012-04-24 06:36:35 +000015408 OpLo = DAG.getNode(X86ISD::VSEXT_MOVL, dl, HalfVT, OpLo);
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +000015409 OpHi = DAG.getNode(X86ISD::VSEXT_MOVL, dl, HalfVT, OpHi);
15410
15411 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT, OpLo, OpHi);
15412 }
15413 return SDValue();
15414}
15415
Michael Liaof6c24ee2012-08-10 14:39:24 +000015416static SDValue PerformFMACombine(SDNode *N, SelectionDAG &DAG,
Elena Demikhovsky1503aba2012-08-01 12:06:00 +000015417 const X86Subtarget* Subtarget) {
15418 DebugLoc dl = N->getDebugLoc();
15419 EVT VT = N->getValueType(0);
15420
15421 EVT ScalarVT = VT.getScalarType();
15422 if ((ScalarVT != MVT::f32 && ScalarVT != MVT::f64) || !Subtarget->hasFMA())
15423 return SDValue();
15424
15425 SDValue A = N->getOperand(0);
15426 SDValue B = N->getOperand(1);
15427 SDValue C = N->getOperand(2);
15428
15429 bool NegA = (A.getOpcode() == ISD::FNEG);
15430 bool NegB = (B.getOpcode() == ISD::FNEG);
15431 bool NegC = (C.getOpcode() == ISD::FNEG);
15432
Michael Liaof6c24ee2012-08-10 14:39:24 +000015433 // Negative multiplication when NegA xor NegB
15434 bool NegMul = (NegA != NegB);
Elena Demikhovsky1503aba2012-08-01 12:06:00 +000015435 if (NegA)
15436 A = A.getOperand(0);
15437 if (NegB)
15438 B = B.getOperand(0);
15439 if (NegC)
15440 C = C.getOperand(0);
15441
15442 unsigned Opcode;
15443 if (!NegMul)
15444 Opcode = (!NegC)? X86ISD::FMADD : X86ISD::FMSUB;
15445 else
15446 Opcode = (!NegC)? X86ISD::FNMADD : X86ISD::FNMSUB;
15447 return DAG.getNode(Opcode, dl, VT, A, B, C);
15448}
15449
Elena Demikhovsky28d7e712012-01-24 13:54:13 +000015450static SDValue PerformZExtCombine(SDNode *N, SelectionDAG &DAG,
Craig Topperc16f8512012-04-25 06:39:39 +000015451 TargetLowering::DAGCombinerInfo &DCI,
Elena Demikhovsky28d7e712012-01-24 13:54:13 +000015452 const X86Subtarget *Subtarget) {
Evan Cheng2e489c42009-12-16 00:53:11 +000015453 // (i32 zext (and (i8 x86isd::setcc_carry), 1)) ->
15454 // (and (i32 x86isd::setcc_carry), 1)
15455 // This eliminates the zext. This transformation is necessary because
15456 // ISD::SETCC is always legalized to i8.
15457 DebugLoc dl = N->getDebugLoc();
15458 SDValue N0 = N->getOperand(0);
15459 EVT VT = N->getValueType(0);
Elena Demikhovsky28d7e712012-01-24 13:54:13 +000015460 EVT OpVT = N0.getValueType();
15461
Evan Cheng2e489c42009-12-16 00:53:11 +000015462 if (N0.getOpcode() == ISD::AND &&
15463 N0.hasOneUse() &&
15464 N0.getOperand(0).hasOneUse()) {
15465 SDValue N00 = N0.getOperand(0);
15466 if (N00.getOpcode() != X86ISD::SETCC_CARRY)
15467 return SDValue();
15468 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
15469 if (!C || C->getZExtValue() != 1)
15470 return SDValue();
15471 return DAG.getNode(ISD::AND, dl, VT,
15472 DAG.getNode(X86ISD::SETCC_CARRY, dl, VT,
15473 N00.getOperand(0), N00.getOperand(1)),
15474 DAG.getConstant(1, VT));
15475 }
Craig Topperd0cf5652012-04-21 18:13:35 +000015476
Elena Demikhovsky28d7e712012-01-24 13:54:13 +000015477 // Optimize vectors in AVX mode:
15478 //
15479 // v8i16 -> v8i32
15480 // Use vpunpcklwd for 4 lower elements v8i16 -> v4i32.
15481 // Use vpunpckhwd for 4 upper elements v8i16 -> v4i32.
15482 // Concat upper and lower parts.
15483 //
15484 // v4i32 -> v4i64
15485 // Use vpunpckldq for 4 lower elements v4i32 -> v2i64.
15486 // Use vpunpckhdq for 4 upper elements v4i32 -> v2i64.
15487 // Concat upper and lower parts.
15488 //
Craig Topperc16f8512012-04-25 06:39:39 +000015489 if (!DCI.isBeforeLegalizeOps())
15490 return SDValue();
Elena Demikhovsky28d7e712012-01-24 13:54:13 +000015491
Craig Topperc16f8512012-04-25 06:39:39 +000015492 if (!Subtarget->hasAVX())
15493 return SDValue();
Elena Demikhovsky28d7e712012-01-24 13:54:13 +000015494
Craig Topperc16f8512012-04-25 06:39:39 +000015495 if (((VT == MVT::v8i32) && (OpVT == MVT::v8i16)) ||
15496 ((VT == MVT::v4i64) && (OpVT == MVT::v4i32))) {
Elena Demikhovsky1da58672012-04-22 09:39:03 +000015497
Craig Topperc16f8512012-04-25 06:39:39 +000015498 if (Subtarget->hasAVX2())
15499 return DAG.getNode(X86ISD::VZEXT_MOVL, dl, VT, N0);
Elena Demikhovsky28d7e712012-01-24 13:54:13 +000015500
Craig Topperc16f8512012-04-25 06:39:39 +000015501 SDValue ZeroVec = getZeroVector(OpVT, Subtarget, DAG, dl);
15502 SDValue OpLo = getUnpackl(DAG, dl, OpVT, N0, ZeroVec);
15503 SDValue OpHi = getUnpackh(DAG, dl, OpVT, N0, ZeroVec);
Elena Demikhovsky28d7e712012-01-24 13:54:13 +000015504
Craig Topperc16f8512012-04-25 06:39:39 +000015505 EVT HVT = EVT::getVectorVT(*DAG.getContext(), VT.getVectorElementType(),
15506 VT.getVectorNumElements()/2);
Elena Demikhovsky28d7e712012-01-24 13:54:13 +000015507
Craig Topperc16f8512012-04-25 06:39:39 +000015508 OpLo = DAG.getNode(ISD::BITCAST, dl, HVT, OpLo);
15509 OpHi = DAG.getNode(ISD::BITCAST, dl, HVT, OpHi);
15510
15511 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT, OpLo, OpHi);
Elena Demikhovsky28d7e712012-01-24 13:54:13 +000015512 }
15513
Evan Cheng2e489c42009-12-16 00:53:11 +000015514 return SDValue();
15515}
15516
Chad Rosiera73b6fc2012-04-27 22:33:25 +000015517// Optimize x == -y --> x+y == 0
15518// x != -y --> x+y != 0
15519static SDValue PerformISDSETCCCombine(SDNode *N, SelectionDAG &DAG) {
15520 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(2))->get();
15521 SDValue LHS = N->getOperand(0);
Chad Rosiera20e1e72012-08-01 18:39:17 +000015522 SDValue RHS = N->getOperand(1);
Chad Rosiera73b6fc2012-04-27 22:33:25 +000015523
15524 if ((CC == ISD::SETNE || CC == ISD::SETEQ) && LHS.getOpcode() == ISD::SUB)
15525 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(LHS.getOperand(0)))
15526 if (C->getAPIntValue() == 0 && LHS.hasOneUse()) {
15527 SDValue addV = DAG.getNode(ISD::ADD, N->getDebugLoc(),
15528 LHS.getValueType(), RHS, LHS.getOperand(1));
15529 return DAG.getSetCC(N->getDebugLoc(), N->getValueType(0),
15530 addV, DAG.getConstant(0, addV.getValueType()), CC);
15531 }
15532 if ((CC == ISD::SETNE || CC == ISD::SETEQ) && RHS.getOpcode() == ISD::SUB)
15533 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(RHS.getOperand(0)))
15534 if (C->getAPIntValue() == 0 && RHS.hasOneUse()) {
15535 SDValue addV = DAG.getNode(ISD::ADD, N->getDebugLoc(),
15536 RHS.getValueType(), LHS, RHS.getOperand(1));
15537 return DAG.getSetCC(N->getDebugLoc(), N->getValueType(0),
15538 addV, DAG.getConstant(0, addV.getValueType()), CC);
15539 }
15540 return SDValue();
15541}
15542
Chris Lattnerc19d1c32010-12-19 22:08:31 +000015543// Optimize RES = X86ISD::SETCC CONDCODE, EFLAG_INPUT
15544static SDValue PerformSETCCCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattnerc19d1c32010-12-19 22:08:31 +000015545 DebugLoc DL = N->getDebugLoc();
Michael Liao2a33cec2012-08-10 19:58:13 +000015546 X86::CondCode CC = X86::CondCode(N->getConstantOperandVal(0));
15547 SDValue EFLAGS = N->getOperand(1);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000015548
Chris Lattnerc19d1c32010-12-19 22:08:31 +000015549 // Materialize "setb reg" as "sbb reg,reg", since it can be extended without
15550 // a zext and produces an all-ones bit which is more useful than 0/1 in some
15551 // cases.
Michael Liao2a33cec2012-08-10 19:58:13 +000015552 if (CC == X86::COND_B)
Chris Lattnerc19d1c32010-12-19 22:08:31 +000015553 return DAG.getNode(ISD::AND, DL, MVT::i8,
15554 DAG.getNode(X86ISD::SETCC_CARRY, DL, MVT::i8,
Michael Liao2a33cec2012-08-10 19:58:13 +000015555 DAG.getConstant(CC, MVT::i8), EFLAGS),
Chris Lattnerc19d1c32010-12-19 22:08:31 +000015556 DAG.getConstant(1, MVT::i8));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000015557
Michael Liao2a33cec2012-08-10 19:58:13 +000015558 SDValue Flags;
15559
15560 Flags = BoolTestSetCCCombine(EFLAGS, CC);
15561 if (Flags.getNode()) {
15562 SDValue Cond = DAG.getConstant(CC, MVT::i8);
15563 return DAG.getNode(X86ISD::SETCC, DL, N->getVTList(), Cond, Flags);
15564 }
15565
15566 return SDValue();
15567}
15568
15569// Optimize branch condition evaluation.
15570//
15571static SDValue PerformBrCondCombine(SDNode *N, SelectionDAG &DAG,
15572 TargetLowering::DAGCombinerInfo &DCI,
15573 const X86Subtarget *Subtarget) {
15574 DebugLoc DL = N->getDebugLoc();
15575 SDValue Chain = N->getOperand(0);
15576 SDValue Dest = N->getOperand(1);
15577 SDValue EFLAGS = N->getOperand(3);
15578 X86::CondCode CC = X86::CondCode(N->getConstantOperandVal(2));
15579
15580 SDValue Flags;
15581
15582 Flags = BoolTestSetCCCombine(EFLAGS, CC);
15583 if (Flags.getNode()) {
15584 SDValue Cond = DAG.getConstant(CC, MVT::i8);
15585 return DAG.getNode(X86ISD::BRCOND, DL, N->getVTList(), Chain, Dest, Cond,
15586 Flags);
15587 }
15588
Chris Lattnerc19d1c32010-12-19 22:08:31 +000015589 return SDValue();
15590}
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000015591
Craig Topper7fd5e162012-04-24 06:02:29 +000015592static SDValue PerformUINT_TO_FPCombine(SDNode *N, SelectionDAG &DAG) {
Nadav Rotema3540772012-04-23 21:53:37 +000015593 SDValue Op0 = N->getOperand(0);
15594 EVT InVT = Op0->getValueType(0);
Nadav Rotema3540772012-04-23 21:53:37 +000015595
15596 // UINT_TO_FP(v4i8) -> SINT_TO_FP(ZEXT(v4i8 to v4i32))
Craig Topper7fd5e162012-04-24 06:02:29 +000015597 if (InVT == MVT::v8i8 || InVT == MVT::v4i8) {
Nadav Rotema3540772012-04-23 21:53:37 +000015598 DebugLoc dl = N->getDebugLoc();
Craig Topper7fd5e162012-04-24 06:02:29 +000015599 MVT DstVT = InVT == MVT::v4i8 ? MVT::v4i32 : MVT::v8i32;
Nadav Rotema3540772012-04-23 21:53:37 +000015600 SDValue P = DAG.getNode(ISD::ZERO_EXTEND, dl, DstVT, Op0);
15601 // Notice that we use SINT_TO_FP because we know that the high bits
15602 // are zero and SINT_TO_FP is better supported by the hardware.
15603 return DAG.getNode(ISD::SINT_TO_FP, dl, N->getValueType(0), P);
15604 }
15605
15606 return SDValue();
15607}
15608
Benjamin Kramer1396c402011-06-18 11:09:41 +000015609static SDValue PerformSINT_TO_FPCombine(SDNode *N, SelectionDAG &DAG,
15610 const X86TargetLowering *XTLI) {
Stuart Hastingsf99a4b82011-06-06 23:15:58 +000015611 SDValue Op0 = N->getOperand(0);
Nadav Rotema3540772012-04-23 21:53:37 +000015612 EVT InVT = Op0->getValueType(0);
Nadav Rotema3540772012-04-23 21:53:37 +000015613
15614 // SINT_TO_FP(v4i8) -> SINT_TO_FP(SEXT(v4i8 to v4i32))
Craig Topper7fd5e162012-04-24 06:02:29 +000015615 if (InVT == MVT::v8i8 || InVT == MVT::v4i8) {
Nadav Rotema3540772012-04-23 21:53:37 +000015616 DebugLoc dl = N->getDebugLoc();
Craig Topper7fd5e162012-04-24 06:02:29 +000015617 MVT DstVT = InVT == MVT::v4i8 ? MVT::v4i32 : MVT::v8i32;
Nadav Rotema3540772012-04-23 21:53:37 +000015618 SDValue P = DAG.getNode(ISD::SIGN_EXTEND, dl, DstVT, Op0);
15619 return DAG.getNode(ISD::SINT_TO_FP, dl, N->getValueType(0), P);
15620 }
15621
Stuart Hastingsf99a4b82011-06-06 23:15:58 +000015622 // Transform (SINT_TO_FP (i64 ...)) into an x87 operation if we have
15623 // a 32-bit target where SSE doesn't support i64->FP operations.
15624 if (Op0.getOpcode() == ISD::LOAD) {
15625 LoadSDNode *Ld = cast<LoadSDNode>(Op0.getNode());
15626 EVT VT = Ld->getValueType(0);
15627 if (!Ld->isVolatile() && !N->getValueType(0).isVector() &&
15628 ISD::isNON_EXTLoad(Op0.getNode()) && Op0.hasOneUse() &&
15629 !XTLI->getSubtarget()->is64Bit() &&
15630 !DAG.getTargetLoweringInfo().isTypeLegal(VT)) {
Benjamin Kramer1396c402011-06-18 11:09:41 +000015631 SDValue FILDChain = XTLI->BuildFILD(SDValue(N, 0), Ld->getValueType(0),
15632 Ld->getChain(), Op0, DAG);
Stuart Hastingsf99a4b82011-06-06 23:15:58 +000015633 DAG.ReplaceAllUsesOfValueWith(Op0.getValue(1), FILDChain.getValue(1));
15634 return FILDChain;
15635 }
15636 }
15637 return SDValue();
15638}
15639
Craig Topper7fd5e162012-04-24 06:02:29 +000015640static SDValue PerformFP_TO_SINTCombine(SDNode *N, SelectionDAG &DAG) {
15641 EVT VT = N->getValueType(0);
Nadav Rotema3540772012-04-23 21:53:37 +000015642
15643 // v4i8 = FP_TO_SINT() -> v4i8 = TRUNCATE (V4i32 = FP_TO_SINT()
Nadav Rotema3540772012-04-23 21:53:37 +000015644 if (VT == MVT::v8i8 || VT == MVT::v4i8) {
15645 DebugLoc dl = N->getDebugLoc();
Craig Topper7fd5e162012-04-24 06:02:29 +000015646 MVT DstVT = VT == MVT::v4i8 ? MVT::v4i32 : MVT::v8i32;
Nadav Rotema3540772012-04-23 21:53:37 +000015647 SDValue I = DAG.getNode(ISD::FP_TO_SINT, dl, DstVT, N->getOperand(0));
15648 return DAG.getNode(ISD::TRUNCATE, dl, VT, I);
15649 }
15650
15651 return SDValue();
15652}
15653
Chris Lattner23a01992010-12-20 01:37:09 +000015654// Optimize RES, EFLAGS = X86ISD::ADC LHS, RHS, EFLAGS
15655static SDValue PerformADCCombine(SDNode *N, SelectionDAG &DAG,
15656 X86TargetLowering::DAGCombinerInfo &DCI) {
15657 // If the LHS and RHS of the ADC node are zero, then it can't overflow and
15658 // the result is either zero or one (depending on the input carry bit).
15659 // Strength reduce this down to a "set on carry" aka SETCC_CARRY&1.
15660 if (X86::isZeroNode(N->getOperand(0)) &&
15661 X86::isZeroNode(N->getOperand(1)) &&
15662 // We don't have a good way to replace an EFLAGS use, so only do this when
15663 // dead right now.
15664 SDValue(N, 1).use_empty()) {
15665 DebugLoc DL = N->getDebugLoc();
15666 EVT VT = N->getValueType(0);
15667 SDValue CarryOut = DAG.getConstant(0, N->getValueType(1));
15668 SDValue Res1 = DAG.getNode(ISD::AND, DL, VT,
15669 DAG.getNode(X86ISD::SETCC_CARRY, DL, VT,
15670 DAG.getConstant(X86::COND_B,MVT::i8),
15671 N->getOperand(2)),
15672 DAG.getConstant(1, VT));
15673 return DCI.CombineTo(N, Res1, CarryOut);
15674 }
15675
15676 return SDValue();
15677}
15678
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000015679// fold (add Y, (sete X, 0)) -> adc 0, Y
15680// (add Y, (setne X, 0)) -> sbb -1, Y
15681// (sub (sete X, 0), Y) -> sbb 0, Y
15682// (sub (setne X, 0), Y) -> adc -1, Y
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000015683static SDValue OptimizeConditionalInDecrement(SDNode *N, SelectionDAG &DAG) {
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000015684 DebugLoc DL = N->getDebugLoc();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000015685
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000015686 // Look through ZExts.
15687 SDValue Ext = N->getOperand(N->getOpcode() == ISD::SUB ? 1 : 0);
15688 if (Ext.getOpcode() != ISD::ZERO_EXTEND || !Ext.hasOneUse())
15689 return SDValue();
15690
15691 SDValue SetCC = Ext.getOperand(0);
15692 if (SetCC.getOpcode() != X86ISD::SETCC || !SetCC.hasOneUse())
15693 return SDValue();
15694
15695 X86::CondCode CC = (X86::CondCode)SetCC.getConstantOperandVal(0);
15696 if (CC != X86::COND_E && CC != X86::COND_NE)
15697 return SDValue();
15698
15699 SDValue Cmp = SetCC.getOperand(1);
15700 if (Cmp.getOpcode() != X86ISD::CMP || !Cmp.hasOneUse() ||
Chris Lattner9cd3da42011-01-16 02:56:53 +000015701 !X86::isZeroNode(Cmp.getOperand(1)) ||
15702 !Cmp.getOperand(0).getValueType().isInteger())
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000015703 return SDValue();
15704
15705 SDValue CmpOp0 = Cmp.getOperand(0);
15706 SDValue NewCmp = DAG.getNode(X86ISD::CMP, DL, MVT::i32, CmpOp0,
15707 DAG.getConstant(1, CmpOp0.getValueType()));
15708
15709 SDValue OtherVal = N->getOperand(N->getOpcode() == ISD::SUB ? 0 : 1);
15710 if (CC == X86::COND_NE)
15711 return DAG.getNode(N->getOpcode() == ISD::SUB ? X86ISD::ADC : X86ISD::SBB,
15712 DL, OtherVal.getValueType(), OtherVal,
15713 DAG.getConstant(-1ULL, OtherVal.getValueType()), NewCmp);
15714 return DAG.getNode(N->getOpcode() == ISD::SUB ? X86ISD::SBB : X86ISD::ADC,
15715 DL, OtherVal.getValueType(), OtherVal,
15716 DAG.getConstant(0, OtherVal.getValueType()), NewCmp);
15717}
Chris Lattnerc19d1c32010-12-19 22:08:31 +000015718
Craig Topper54f952a2011-11-19 09:02:40 +000015719/// PerformADDCombine - Do target-specific dag combines on integer adds.
15720static SDValue PerformAddCombine(SDNode *N, SelectionDAG &DAG,
15721 const X86Subtarget *Subtarget) {
15722 EVT VT = N->getValueType(0);
15723 SDValue Op0 = N->getOperand(0);
15724 SDValue Op1 = N->getOperand(1);
15725
15726 // Try to synthesize horizontal adds from adds of shuffles.
Craig Topperd0a31172012-01-10 06:37:29 +000015727 if (((Subtarget->hasSSSE3() && (VT == MVT::v8i16 || VT == MVT::v4i32)) ||
Craig Toppere6cf4a02012-01-13 05:04:25 +000015728 (Subtarget->hasAVX2() && (VT == MVT::v16i16 || VT == MVT::v8i32))) &&
Craig Topper54f952a2011-11-19 09:02:40 +000015729 isHorizontalBinOp(Op0, Op1, true))
15730 return DAG.getNode(X86ISD::HADD, N->getDebugLoc(), VT, Op0, Op1);
15731
15732 return OptimizeConditionalInDecrement(N, DAG);
15733}
15734
15735static SDValue PerformSubCombine(SDNode *N, SelectionDAG &DAG,
15736 const X86Subtarget *Subtarget) {
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000015737 SDValue Op0 = N->getOperand(0);
15738 SDValue Op1 = N->getOperand(1);
15739
15740 // X86 can't encode an immediate LHS of a sub. See if we can push the
15741 // negation into a preceding instruction.
15742 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op0)) {
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000015743 // If the RHS of the sub is a XOR with one use and a constant, invert the
15744 // immediate. Then add one to the LHS of the sub so we can turn
15745 // X-Y -> X+~Y+1, saving one register.
15746 if (Op1->hasOneUse() && Op1.getOpcode() == ISD::XOR &&
15747 isa<ConstantSDNode>(Op1.getOperand(1))) {
Nick Lewycky726ebd62011-08-23 19:01:24 +000015748 APInt XorC = cast<ConstantSDNode>(Op1.getOperand(1))->getAPIntValue();
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000015749 EVT VT = Op0.getValueType();
15750 SDValue NewXor = DAG.getNode(ISD::XOR, Op1.getDebugLoc(), VT,
15751 Op1.getOperand(0),
15752 DAG.getConstant(~XorC, VT));
15753 return DAG.getNode(ISD::ADD, N->getDebugLoc(), VT, NewXor,
Nick Lewycky726ebd62011-08-23 19:01:24 +000015754 DAG.getConstant(C->getAPIntValue()+1, VT));
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000015755 }
15756 }
15757
Craig Topper54f952a2011-11-19 09:02:40 +000015758 // Try to synthesize horizontal adds from adds of shuffles.
15759 EVT VT = N->getValueType(0);
Craig Topperd0a31172012-01-10 06:37:29 +000015760 if (((Subtarget->hasSSSE3() && (VT == MVT::v8i16 || VT == MVT::v4i32)) ||
Craig Topperb72039c2011-11-30 09:10:50 +000015761 (Subtarget->hasAVX2() && (VT == MVT::v16i16 || VT == MVT::v8i32))) &&
15762 isHorizontalBinOp(Op0, Op1, true))
Craig Topper54f952a2011-11-19 09:02:40 +000015763 return DAG.getNode(X86ISD::HSUB, N->getDebugLoc(), VT, Op0, Op1);
15764
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000015765 return OptimizeConditionalInDecrement(N, DAG);
15766}
15767
Dan Gohman475871a2008-07-27 21:46:04 +000015768SDValue X86TargetLowering::PerformDAGCombine(SDNode *N,
Evan Cheng9dd93b32008-11-05 06:03:38 +000015769 DAGCombinerInfo &DCI) const {
Evan Cheng206ee9d2006-07-07 08:33:52 +000015770 SelectionDAG &DAG = DCI.DAG;
15771 switch (N->getOpcode()) {
15772 default: break;
Dan Gohman1bbf72b2010-03-15 23:23:03 +000015773 case ISD::EXTRACT_VECTOR_ELT:
Craig Topper89f4e662012-03-20 07:17:59 +000015774 return PerformEXTRACT_VECTOR_ELTCombine(N, DAG, DCI);
Duncan Sands6bcd2192011-09-17 16:49:39 +000015775 case ISD::VSELECT:
Nadav Rotemcc616562012-01-15 19:27:55 +000015776 case ISD::SELECT: return PerformSELECTCombine(N, DAG, DCI, Subtarget);
Chris Lattnerd1980a52009-03-12 06:52:53 +000015777 case X86ISD::CMOV: return PerformCMOVCombine(N, DAG, DCI);
Craig Topper54f952a2011-11-19 09:02:40 +000015778 case ISD::ADD: return PerformAddCombine(N, DAG, Subtarget);
15779 case ISD::SUB: return PerformSubCombine(N, DAG, Subtarget);
Chris Lattner23a01992010-12-20 01:37:09 +000015780 case X86ISD::ADC: return PerformADCCombine(N, DAG, DCI);
Evan Cheng0b0cd912009-03-28 05:57:29 +000015781 case ISD::MUL: return PerformMulCombine(N, DAG, DCI);
Nate Begeman740ab032009-01-26 00:52:55 +000015782 case ISD::SHL:
15783 case ISD::SRA:
Mon P Wang845b1892012-02-01 22:15:20 +000015784 case ISD::SRL: return PerformShiftCombine(N, DAG, DCI, Subtarget);
Nate Begemanb65c1752010-12-17 22:55:37 +000015785 case ISD::AND: return PerformAndCombine(N, DAG, DCI, Subtarget);
Evan Cheng8b1190a2010-04-28 01:18:01 +000015786 case ISD::OR: return PerformOrCombine(N, DAG, DCI, Subtarget);
Craig Topperb4c94572011-10-21 06:55:01 +000015787 case ISD::XOR: return PerformXorCombine(N, DAG, DCI, Subtarget);
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000015788 case ISD::LOAD: return PerformLOADCombine(N, DAG, DCI, Subtarget);
Evan Cheng7e2ff772008-05-08 00:57:18 +000015789 case ISD::STORE: return PerformSTORECombine(N, DAG, Subtarget);
Craig Topper7fd5e162012-04-24 06:02:29 +000015790 case ISD::UINT_TO_FP: return PerformUINT_TO_FPCombine(N, DAG);
Stuart Hastingsf99a4b82011-06-06 23:15:58 +000015791 case ISD::SINT_TO_FP: return PerformSINT_TO_FPCombine(N, DAG, this);
Craig Topper7fd5e162012-04-24 06:02:29 +000015792 case ISD::FP_TO_SINT: return PerformFP_TO_SINTCombine(N, DAG);
Duncan Sands17470be2011-09-22 20:15:48 +000015793 case ISD::FADD: return PerformFADDCombine(N, DAG, Subtarget);
15794 case ISD::FSUB: return PerformFSUBCombine(N, DAG, Subtarget);
Chris Lattner6cf73262008-01-25 06:14:17 +000015795 case X86ISD::FXOR:
Chris Lattneraf723b92008-01-25 05:46:26 +000015796 case X86ISD::FOR: return PerformFORCombine(N, DAG);
15797 case X86ISD::FAND: return PerformFANDCombine(N, DAG);
Dan Gohmane5af2d32009-01-29 01:59:02 +000015798 case X86ISD::BT: return PerformBTCombine(N, DAG, DCI);
Eli Friedman7a5e5552009-06-07 06:52:44 +000015799 case X86ISD::VZEXT_MOVL: return PerformVZEXT_MOVLCombine(N, DAG);
Elena Demikhovsky1da58672012-04-22 09:39:03 +000015800 case ISD::ANY_EXTEND:
Craig Topperc16f8512012-04-25 06:39:39 +000015801 case ISD::ZERO_EXTEND: return PerformZExtCombine(N, DAG, DCI, Subtarget);
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +000015802 case ISD::SIGN_EXTEND: return PerformSExtCombine(N, DAG, DCI, Subtarget);
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000015803 case ISD::TRUNCATE: return PerformTruncateCombine(N, DAG, DCI);
Chad Rosiera73b6fc2012-04-27 22:33:25 +000015804 case ISD::SETCC: return PerformISDSETCCCombine(N, DAG);
Chris Lattnerc19d1c32010-12-19 22:08:31 +000015805 case X86ISD::SETCC: return PerformSETCCCombine(N, DAG);
Michael Liao2a33cec2012-08-10 19:58:13 +000015806 case X86ISD::BRCOND: return PerformBrCondCombine(N, DAG, DCI, Subtarget);
Craig Topperb3982da2011-12-31 23:50:21 +000015807 case X86ISD::SHUFP: // Handle all target specific shuffles
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +000015808 case X86ISD::PALIGN:
Craig Topper34671b82011-12-06 08:21:25 +000015809 case X86ISD::UNPCKH:
15810 case X86ISD::UNPCKL:
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000015811 case X86ISD::MOVHLPS:
15812 case X86ISD::MOVLHPS:
15813 case X86ISD::PSHUFD:
15814 case X86ISD::PSHUFHW:
15815 case X86ISD::PSHUFLW:
15816 case X86ISD::MOVSS:
15817 case X86ISD::MOVSD:
Craig Topper316cd2a2011-11-30 06:25:25 +000015818 case X86ISD::VPERMILP:
Craig Topperec24e612011-11-30 07:47:51 +000015819 case X86ISD::VPERM2X128:
Bruno Cardoso Lopes50b37c72011-08-15 21:45:54 +000015820 case ISD::VECTOR_SHUFFLE: return PerformShuffleCombine(N, DAG, DCI,Subtarget);
Elena Demikhovsky1503aba2012-08-01 12:06:00 +000015821 case ISD::FMA: return PerformFMACombine(N, DAG, Subtarget);
Evan Cheng206ee9d2006-07-07 08:33:52 +000015822 }
15823
Dan Gohman475871a2008-07-27 21:46:04 +000015824 return SDValue();
Evan Cheng206ee9d2006-07-07 08:33:52 +000015825}
15826
Evan Chenge5b51ac2010-04-17 06:13:15 +000015827/// isTypeDesirableForOp - Return true if the target has native support for
15828/// the specified value type and it is 'desirable' to use the type for the
15829/// given node type. e.g. On x86 i16 is legal, but undesirable since i16
15830/// instruction encodings are longer and some i16 instructions are slow.
15831bool X86TargetLowering::isTypeDesirableForOp(unsigned Opc, EVT VT) const {
15832 if (!isTypeLegal(VT))
15833 return false;
Evan Cheng2bce5f4b2010-04-28 08:30:49 +000015834 if (VT != MVT::i16)
Evan Chenge5b51ac2010-04-17 06:13:15 +000015835 return true;
15836
15837 switch (Opc) {
15838 default:
15839 return true;
Evan Cheng4c26e932010-04-19 19:29:22 +000015840 case ISD::LOAD:
15841 case ISD::SIGN_EXTEND:
15842 case ISD::ZERO_EXTEND:
15843 case ISD::ANY_EXTEND:
Evan Chenge5b51ac2010-04-17 06:13:15 +000015844 case ISD::SHL:
Evan Chenge5b51ac2010-04-17 06:13:15 +000015845 case ISD::SRL:
15846 case ISD::SUB:
15847 case ISD::ADD:
15848 case ISD::MUL:
15849 case ISD::AND:
15850 case ISD::OR:
15851 case ISD::XOR:
15852 return false;
15853 }
15854}
15855
15856/// IsDesirableToPromoteOp - This method query the target whether it is
Evan Cheng64b7bf72010-04-16 06:14:10 +000015857/// beneficial for dag combiner to promote the specified node. If true, it
15858/// should return the desired promotion type by reference.
Evan Chenge5b51ac2010-04-17 06:13:15 +000015859bool X86TargetLowering::IsDesirableToPromoteOp(SDValue Op, EVT &PVT) const {
Evan Cheng64b7bf72010-04-16 06:14:10 +000015860 EVT VT = Op.getValueType();
15861 if (VT != MVT::i16)
15862 return false;
15863
Evan Cheng4c26e932010-04-19 19:29:22 +000015864 bool Promote = false;
15865 bool Commute = false;
Evan Cheng64b7bf72010-04-16 06:14:10 +000015866 switch (Op.getOpcode()) {
Evan Cheng4c26e932010-04-19 19:29:22 +000015867 default: break;
15868 case ISD::LOAD: {
15869 LoadSDNode *LD = cast<LoadSDNode>(Op);
15870 // If the non-extending load has a single use and it's not live out, then it
15871 // might be folded.
Evan Cheng2bce5f4b2010-04-28 08:30:49 +000015872 if (LD->getExtensionType() == ISD::NON_EXTLOAD /*&&
15873 Op.hasOneUse()*/) {
15874 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
15875 UE = Op.getNode()->use_end(); UI != UE; ++UI) {
15876 // The only case where we'd want to promote LOAD (rather then it being
15877 // promoted as an operand is when it's only use is liveout.
15878 if (UI->getOpcode() != ISD::CopyToReg)
15879 return false;
15880 }
15881 }
Evan Cheng4c26e932010-04-19 19:29:22 +000015882 Promote = true;
15883 break;
15884 }
15885 case ISD::SIGN_EXTEND:
15886 case ISD::ZERO_EXTEND:
15887 case ISD::ANY_EXTEND:
15888 Promote = true;
15889 break;
Evan Chenge5b51ac2010-04-17 06:13:15 +000015890 case ISD::SHL:
Evan Cheng2bce5f4b2010-04-28 08:30:49 +000015891 case ISD::SRL: {
Evan Chenge5b51ac2010-04-17 06:13:15 +000015892 SDValue N0 = Op.getOperand(0);
15893 // Look out for (store (shl (load), x)).
Evan Chengc82c20b2010-04-24 04:44:57 +000015894 if (MayFoldLoad(N0) && MayFoldIntoStore(Op))
Evan Chenge5b51ac2010-04-17 06:13:15 +000015895 return false;
Evan Cheng4c26e932010-04-19 19:29:22 +000015896 Promote = true;
Evan Chenge5b51ac2010-04-17 06:13:15 +000015897 break;
15898 }
Evan Cheng64b7bf72010-04-16 06:14:10 +000015899 case ISD::ADD:
15900 case ISD::MUL:
15901 case ISD::AND:
15902 case ISD::OR:
Evan Cheng4c26e932010-04-19 19:29:22 +000015903 case ISD::XOR:
15904 Commute = true;
15905 // fallthrough
15906 case ISD::SUB: {
Evan Cheng64b7bf72010-04-16 06:14:10 +000015907 SDValue N0 = Op.getOperand(0);
15908 SDValue N1 = Op.getOperand(1);
Evan Chengc82c20b2010-04-24 04:44:57 +000015909 if (!Commute && MayFoldLoad(N1))
Evan Cheng64b7bf72010-04-16 06:14:10 +000015910 return false;
15911 // Avoid disabling potential load folding opportunities.
Evan Chengc82c20b2010-04-24 04:44:57 +000015912 if (MayFoldLoad(N0) && (!isa<ConstantSDNode>(N1) || MayFoldIntoStore(Op)))
Evan Cheng64b7bf72010-04-16 06:14:10 +000015913 return false;
Evan Chengc82c20b2010-04-24 04:44:57 +000015914 if (MayFoldLoad(N1) && (!isa<ConstantSDNode>(N0) || MayFoldIntoStore(Op)))
Evan Cheng64b7bf72010-04-16 06:14:10 +000015915 return false;
Evan Cheng4c26e932010-04-19 19:29:22 +000015916 Promote = true;
Evan Cheng64b7bf72010-04-16 06:14:10 +000015917 }
15918 }
15919
15920 PVT = MVT::i32;
Evan Cheng4c26e932010-04-19 19:29:22 +000015921 return Promote;
Evan Cheng64b7bf72010-04-16 06:14:10 +000015922}
15923
Evan Cheng60c07e12006-07-05 22:17:51 +000015924//===----------------------------------------------------------------------===//
15925// X86 Inline Assembly Support
15926//===----------------------------------------------------------------------===//
15927
Benjamin Kramer2ea4cdb2011-12-18 19:59:20 +000015928namespace {
15929 // Helper to match a string separated by whitespace.
Benjamin Kramer0581ed72011-12-18 20:51:31 +000015930 bool matchAsmImpl(StringRef s, ArrayRef<const StringRef *> args) {
Benjamin Kramer2ea4cdb2011-12-18 19:59:20 +000015931 s = s.substr(s.find_first_not_of(" \t")); // Skip leading whitespace.
Benjamin Kramere6cddb72011-12-17 14:36:05 +000015932
Benjamin Kramer0581ed72011-12-18 20:51:31 +000015933 for (unsigned i = 0, e = args.size(); i != e; ++i) {
Benjamin Kramer2ea4cdb2011-12-18 19:59:20 +000015934 StringRef piece(*args[i]);
15935 if (!s.startswith(piece)) // Check if the piece matches.
15936 return false;
15937
15938 s = s.substr(piece.size());
15939 StringRef::size_type pos = s.find_first_not_of(" \t");
15940 if (pos == 0) // We matched a prefix.
15941 return false;
15942
15943 s = s.substr(pos);
Benjamin Kramere6cddb72011-12-17 14:36:05 +000015944 }
15945
Benjamin Kramer2ea4cdb2011-12-18 19:59:20 +000015946 return s.empty();
Benjamin Kramere6cddb72011-12-17 14:36:05 +000015947 }
Benjamin Kramer0581ed72011-12-18 20:51:31 +000015948 const VariadicFunction1<bool, StringRef, StringRef, matchAsmImpl> matchAsm={};
Benjamin Kramere6cddb72011-12-17 14:36:05 +000015949}
15950
Chris Lattnerb8105652009-07-20 17:51:36 +000015951bool X86TargetLowering::ExpandInlineAsm(CallInst *CI) const {
15952 InlineAsm *IA = cast<InlineAsm>(CI->getCalledValue());
Chris Lattnerb8105652009-07-20 17:51:36 +000015953
15954 std::string AsmStr = IA->getAsmString();
15955
Benjamin Kramere6cddb72011-12-17 14:36:05 +000015956 IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
15957 if (!Ty || Ty->getBitWidth() % 16 != 0)
15958 return false;
15959
Chris Lattnerb8105652009-07-20 17:51:36 +000015960 // TODO: should remove alternatives from the asmstring: "foo {a|b}" -> "foo a"
Benjamin Kramerd4f19592010-01-11 18:03:24 +000015961 SmallVector<StringRef, 4> AsmPieces;
Peter Collingbourne98361182010-11-13 19:54:23 +000015962 SplitString(AsmStr, AsmPieces, ";\n");
Chris Lattnerb8105652009-07-20 17:51:36 +000015963
15964 switch (AsmPieces.size()) {
15965 default: return false;
15966 case 1:
Chris Lattner7a2bdde2011-04-15 05:18:47 +000015967 // FIXME: this should verify that we are targeting a 486 or better. If not,
Benjamin Kramere6cddb72011-12-17 14:36:05 +000015968 // we will turn this bswap into something that will be lowered to logical
15969 // ops instead of emitting the bswap asm. For now, we don't support 486 or
15970 // lower so don't worry about this.
Chris Lattnerb8105652009-07-20 17:51:36 +000015971 // bswap $0
Benjamin Kramer2ea4cdb2011-12-18 19:59:20 +000015972 if (matchAsm(AsmPieces[0], "bswap", "$0") ||
15973 matchAsm(AsmPieces[0], "bswapl", "$0") ||
15974 matchAsm(AsmPieces[0], "bswapq", "$0") ||
15975 matchAsm(AsmPieces[0], "bswap", "${0:q}") ||
15976 matchAsm(AsmPieces[0], "bswapl", "${0:q}") ||
15977 matchAsm(AsmPieces[0], "bswapq", "${0:q}")) {
Chris Lattnerb8105652009-07-20 17:51:36 +000015978 // No need to check constraints, nothing other than the equivalent of
15979 // "=r,0" would be valid here.
Evan Cheng55d42002011-01-08 01:24:27 +000015980 return IntrinsicLowering::LowerToByteSwap(CI);
Chris Lattnerb8105652009-07-20 17:51:36 +000015981 }
Benjamin Kramere6cddb72011-12-17 14:36:05 +000015982
Chris Lattnerb8105652009-07-20 17:51:36 +000015983 // rorw $$8, ${0:w} --> llvm.bswap.i16
Duncan Sandsb0bc6c32010-02-15 16:12:20 +000015984 if (CI->getType()->isIntegerTy(16) &&
Benjamin Kramere6cddb72011-12-17 14:36:05 +000015985 IA->getConstraintString().compare(0, 5, "=r,0,") == 0 &&
Benjamin Kramer2ea4cdb2011-12-18 19:59:20 +000015986 (matchAsm(AsmPieces[0], "rorw", "$$8,", "${0:w}") ||
15987 matchAsm(AsmPieces[0], "rolw", "$$8,", "${0:w}"))) {
Dan Gohman0ef701e2010-03-04 19:58:08 +000015988 AsmPieces.clear();
Evan Cheng55d42002011-01-08 01:24:27 +000015989 const std::string &ConstraintsStr = IA->getConstraintString();
15990 SplitString(StringRef(ConstraintsStr).substr(5), AsmPieces, ",");
Dan Gohman0ef701e2010-03-04 19:58:08 +000015991 std::sort(AsmPieces.begin(), AsmPieces.end());
15992 if (AsmPieces.size() == 4 &&
15993 AsmPieces[0] == "~{cc}" &&
15994 AsmPieces[1] == "~{dirflag}" &&
15995 AsmPieces[2] == "~{flags}" &&
Benjamin Kramere6cddb72011-12-17 14:36:05 +000015996 AsmPieces[3] == "~{fpsr}")
15997 return IntrinsicLowering::LowerToByteSwap(CI);
Chris Lattnerb8105652009-07-20 17:51:36 +000015998 }
15999 break;
16000 case 3:
Peter Collingbourne948cf022010-11-13 19:54:30 +000016001 if (CI->getType()->isIntegerTy(32) &&
Benjamin Kramere6cddb72011-12-17 14:36:05 +000016002 IA->getConstraintString().compare(0, 5, "=r,0,") == 0 &&
Benjamin Kramer2ea4cdb2011-12-18 19:59:20 +000016003 matchAsm(AsmPieces[0], "rorw", "$$8,", "${0:w}") &&
16004 matchAsm(AsmPieces[1], "rorl", "$$16,", "$0") &&
16005 matchAsm(AsmPieces[2], "rorw", "$$8,", "${0:w}")) {
Benjamin Kramere6cddb72011-12-17 14:36:05 +000016006 AsmPieces.clear();
16007 const std::string &ConstraintsStr = IA->getConstraintString();
16008 SplitString(StringRef(ConstraintsStr).substr(5), AsmPieces, ",");
16009 std::sort(AsmPieces.begin(), AsmPieces.end());
16010 if (AsmPieces.size() == 4 &&
16011 AsmPieces[0] == "~{cc}" &&
16012 AsmPieces[1] == "~{dirflag}" &&
16013 AsmPieces[2] == "~{flags}" &&
16014 AsmPieces[3] == "~{fpsr}")
16015 return IntrinsicLowering::LowerToByteSwap(CI);
Peter Collingbourne948cf022010-11-13 19:54:30 +000016016 }
Evan Cheng55d42002011-01-08 01:24:27 +000016017
16018 if (CI->getType()->isIntegerTy(64)) {
16019 InlineAsm::ConstraintInfoVector Constraints = IA->ParseConstraints();
16020 if (Constraints.size() >= 2 &&
16021 Constraints[0].Codes.size() == 1 && Constraints[0].Codes[0] == "A" &&
16022 Constraints[1].Codes.size() == 1 && Constraints[1].Codes[0] == "0") {
16023 // bswap %eax / bswap %edx / xchgl %eax, %edx -> llvm.bswap.i64
Benjamin Kramer2ea4cdb2011-12-18 19:59:20 +000016024 if (matchAsm(AsmPieces[0], "bswap", "%eax") &&
16025 matchAsm(AsmPieces[1], "bswap", "%edx") &&
16026 matchAsm(AsmPieces[2], "xchgl", "%eax,", "%edx"))
Benjamin Kramere6cddb72011-12-17 14:36:05 +000016027 return IntrinsicLowering::LowerToByteSwap(CI);
Chris Lattnerb8105652009-07-20 17:51:36 +000016028 }
16029 }
16030 break;
16031 }
16032 return false;
16033}
16034
16035
16036
Chris Lattnerf4dff842006-07-11 02:54:03 +000016037/// getConstraintType - Given a constraint letter, return the type of
16038/// constraint it is for this target.
16039X86TargetLowering::ConstraintType
Chris Lattner4234f572007-03-25 02:14:49 +000016040X86TargetLowering::getConstraintType(const std::string &Constraint) const {
16041 if (Constraint.size() == 1) {
16042 switch (Constraint[0]) {
Chris Lattner4234f572007-03-25 02:14:49 +000016043 case 'R':
Chris Lattner4234f572007-03-25 02:14:49 +000016044 case 'q':
16045 case 'Q':
John Thompson44ab89e2010-10-29 17:29:13 +000016046 case 'f':
16047 case 't':
16048 case 'u':
Dale Johannesen2ffbcac2008-04-01 00:57:48 +000016049 case 'y':
John Thompson44ab89e2010-10-29 17:29:13 +000016050 case 'x':
Chris Lattner4234f572007-03-25 02:14:49 +000016051 case 'Y':
Eric Christopher31b5f002011-07-07 22:29:07 +000016052 case 'l':
Chris Lattner4234f572007-03-25 02:14:49 +000016053 return C_RegisterClass;
John Thompson44ab89e2010-10-29 17:29:13 +000016054 case 'a':
16055 case 'b':
16056 case 'c':
16057 case 'd':
16058 case 'S':
16059 case 'D':
16060 case 'A':
16061 return C_Register;
16062 case 'I':
16063 case 'J':
16064 case 'K':
16065 case 'L':
16066 case 'M':
16067 case 'N':
16068 case 'G':
16069 case 'C':
Dale Johannesen78e3e522009-02-12 20:58:09 +000016070 case 'e':
16071 case 'Z':
16072 return C_Other;
Chris Lattner4234f572007-03-25 02:14:49 +000016073 default:
16074 break;
16075 }
Chris Lattnerf4dff842006-07-11 02:54:03 +000016076 }
Chris Lattner4234f572007-03-25 02:14:49 +000016077 return TargetLowering::getConstraintType(Constraint);
Chris Lattnerf4dff842006-07-11 02:54:03 +000016078}
16079
John Thompson44ab89e2010-10-29 17:29:13 +000016080/// Examine constraint type and operand type and determine a weight value.
John Thompsoneac6e1d2010-09-13 18:15:37 +000016081/// This object must already have been set up with the operand type
16082/// and the current alternative constraint selected.
John Thompson44ab89e2010-10-29 17:29:13 +000016083TargetLowering::ConstraintWeight
16084 X86TargetLowering::getSingleConstraintMatchWeight(
John Thompsoneac6e1d2010-09-13 18:15:37 +000016085 AsmOperandInfo &info, const char *constraint) const {
John Thompson44ab89e2010-10-29 17:29:13 +000016086 ConstraintWeight weight = CW_Invalid;
John Thompsoneac6e1d2010-09-13 18:15:37 +000016087 Value *CallOperandVal = info.CallOperandVal;
16088 // If we don't have a value, we can't do a match,
16089 // but allow it at the lowest weight.
16090 if (CallOperandVal == NULL)
John Thompson44ab89e2010-10-29 17:29:13 +000016091 return CW_Default;
Chris Lattnerdb125cf2011-07-18 04:54:35 +000016092 Type *type = CallOperandVal->getType();
John Thompsoneac6e1d2010-09-13 18:15:37 +000016093 // Look at the constraint type.
16094 switch (*constraint) {
16095 default:
John Thompson44ab89e2010-10-29 17:29:13 +000016096 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
16097 case 'R':
16098 case 'q':
16099 case 'Q':
16100 case 'a':
16101 case 'b':
16102 case 'c':
16103 case 'd':
16104 case 'S':
16105 case 'D':
16106 case 'A':
16107 if (CallOperandVal->getType()->isIntegerTy())
16108 weight = CW_SpecificReg;
16109 break;
16110 case 'f':
16111 case 't':
16112 case 'u':
16113 if (type->isFloatingPointTy())
16114 weight = CW_SpecificReg;
16115 break;
16116 case 'y':
Chris Lattner2a786eb2010-12-19 20:19:20 +000016117 if (type->isX86_MMXTy() && Subtarget->hasMMX())
John Thompson44ab89e2010-10-29 17:29:13 +000016118 weight = CW_SpecificReg;
16119 break;
16120 case 'x':
16121 case 'Y':
Craig Topper1accb7e2012-01-10 06:54:16 +000016122 if (((type->getPrimitiveSizeInBits() == 128) && Subtarget->hasSSE1()) ||
Eric Christopher55487552012-01-07 01:02:09 +000016123 ((type->getPrimitiveSizeInBits() == 256) && Subtarget->hasAVX()))
John Thompson44ab89e2010-10-29 17:29:13 +000016124 weight = CW_Register;
John Thompsoneac6e1d2010-09-13 18:15:37 +000016125 break;
16126 case 'I':
16127 if (ConstantInt *C = dyn_cast<ConstantInt>(info.CallOperandVal)) {
16128 if (C->getZExtValue() <= 31)
John Thompson44ab89e2010-10-29 17:29:13 +000016129 weight = CW_Constant;
John Thompsoneac6e1d2010-09-13 18:15:37 +000016130 }
16131 break;
John Thompson44ab89e2010-10-29 17:29:13 +000016132 case 'J':
16133 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
16134 if (C->getZExtValue() <= 63)
16135 weight = CW_Constant;
16136 }
16137 break;
16138 case 'K':
16139 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
16140 if ((C->getSExtValue() >= -0x80) && (C->getSExtValue() <= 0x7f))
16141 weight = CW_Constant;
16142 }
16143 break;
16144 case 'L':
16145 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
16146 if ((C->getZExtValue() == 0xff) || (C->getZExtValue() == 0xffff))
16147 weight = CW_Constant;
16148 }
16149 break;
16150 case 'M':
16151 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
16152 if (C->getZExtValue() <= 3)
16153 weight = CW_Constant;
16154 }
16155 break;
16156 case 'N':
16157 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
16158 if (C->getZExtValue() <= 0xff)
16159 weight = CW_Constant;
16160 }
16161 break;
16162 case 'G':
16163 case 'C':
16164 if (dyn_cast<ConstantFP>(CallOperandVal)) {
16165 weight = CW_Constant;
16166 }
16167 break;
16168 case 'e':
16169 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
16170 if ((C->getSExtValue() >= -0x80000000LL) &&
16171 (C->getSExtValue() <= 0x7fffffffLL))
16172 weight = CW_Constant;
16173 }
16174 break;
16175 case 'Z':
16176 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
16177 if (C->getZExtValue() <= 0xffffffff)
16178 weight = CW_Constant;
16179 }
16180 break;
John Thompsoneac6e1d2010-09-13 18:15:37 +000016181 }
16182 return weight;
16183}
16184
Dale Johannesenba2a0b92008-01-29 02:21:21 +000016185/// LowerXConstraint - try to replace an X constraint, which matches anything,
16186/// with another that has more specific requirements based on the type of the
16187/// corresponding operand.
Chris Lattner5e764232008-04-26 23:02:14 +000016188const char *X86TargetLowering::
Owen Andersone50ed302009-08-10 22:56:29 +000016189LowerXConstraint(EVT ConstraintVT) const {
Chris Lattner5e764232008-04-26 23:02:14 +000016190 // FP X constraints get lowered to SSE1/2 registers if available, otherwise
16191 // 'f' like normal targets.
Duncan Sands83ec4b62008-06-06 12:08:01 +000016192 if (ConstraintVT.isFloatingPoint()) {
Craig Topper1accb7e2012-01-10 06:54:16 +000016193 if (Subtarget->hasSSE2())
Chris Lattner5e764232008-04-26 23:02:14 +000016194 return "Y";
Craig Topper1accb7e2012-01-10 06:54:16 +000016195 if (Subtarget->hasSSE1())
Chris Lattner5e764232008-04-26 23:02:14 +000016196 return "x";
16197 }
Scott Michelfdc40a02009-02-17 22:15:04 +000016198
Chris Lattner5e764232008-04-26 23:02:14 +000016199 return TargetLowering::LowerXConstraint(ConstraintVT);
Dale Johannesenba2a0b92008-01-29 02:21:21 +000016200}
16201
Chris Lattner48884cd2007-08-25 00:47:38 +000016202/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
16203/// vector. If it is invalid, don't add anything to Ops.
Dan Gohman475871a2008-07-27 21:46:04 +000016204void X86TargetLowering::LowerAsmOperandForConstraint(SDValue Op,
Eric Christopher100c8332011-06-02 23:16:42 +000016205 std::string &Constraint,
Dan Gohman475871a2008-07-27 21:46:04 +000016206 std::vector<SDValue>&Ops,
Chris Lattner5e764232008-04-26 23:02:14 +000016207 SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +000016208 SDValue Result(0, 0);
Scott Michelfdc40a02009-02-17 22:15:04 +000016209
Eric Christopher100c8332011-06-02 23:16:42 +000016210 // Only support length 1 constraints for now.
16211 if (Constraint.length() > 1) return;
Eric Christopher471e4222011-06-08 23:55:35 +000016212
Eric Christopher100c8332011-06-02 23:16:42 +000016213 char ConstraintLetter = Constraint[0];
16214 switch (ConstraintLetter) {
Chris Lattner22aaf1d2006-10-31 20:13:11 +000016215 default: break;
Devang Patel84f7fd22007-03-17 00:13:28 +000016216 case 'I':
Chris Lattner188b9fe2007-03-25 01:57:35 +000016217 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +000016218 if (C->getZExtValue() <= 31) {
16219 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattner48884cd2007-08-25 00:47:38 +000016220 break;
16221 }
Devang Patel84f7fd22007-03-17 00:13:28 +000016222 }
Chris Lattner48884cd2007-08-25 00:47:38 +000016223 return;
Evan Cheng364091e2008-09-22 23:57:37 +000016224 case 'J':
16225 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattner2e06dd22009-06-15 04:39:05 +000016226 if (C->getZExtValue() <= 63) {
Chris Lattnere4935152009-06-15 04:01:39 +000016227 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
16228 break;
16229 }
16230 }
16231 return;
16232 case 'K':
16233 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattner2e06dd22009-06-15 04:39:05 +000016234 if ((int8_t)C->getSExtValue() == C->getSExtValue()) {
Evan Cheng364091e2008-09-22 23:57:37 +000016235 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
16236 break;
16237 }
16238 }
16239 return;
Chris Lattner188b9fe2007-03-25 01:57:35 +000016240 case 'N':
16241 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +000016242 if (C->getZExtValue() <= 255) {
16243 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattner48884cd2007-08-25 00:47:38 +000016244 break;
16245 }
Chris Lattner188b9fe2007-03-25 01:57:35 +000016246 }
Chris Lattner48884cd2007-08-25 00:47:38 +000016247 return;
Dale Johannesen78e3e522009-02-12 20:58:09 +000016248 case 'e': {
16249 // 32-bit signed value
16250 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohman7720cb32010-06-18 14:01:07 +000016251 if (ConstantInt::isValueValidForType(Type::getInt32Ty(*DAG.getContext()),
16252 C->getSExtValue())) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000016253 // Widen to 64 bits here to get it sign extended.
Owen Anderson825b72b2009-08-11 20:47:22 +000016254 Result = DAG.getTargetConstant(C->getSExtValue(), MVT::i64);
Dale Johannesen78e3e522009-02-12 20:58:09 +000016255 break;
16256 }
16257 // FIXME gcc accepts some relocatable values here too, but only in certain
16258 // memory models; it's complicated.
16259 }
16260 return;
16261 }
16262 case 'Z': {
16263 // 32-bit unsigned value
16264 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohman7720cb32010-06-18 14:01:07 +000016265 if (ConstantInt::isValueValidForType(Type::getInt32Ty(*DAG.getContext()),
16266 C->getZExtValue())) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000016267 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
16268 break;
16269 }
16270 }
16271 // FIXME gcc accepts some relocatable values here too, but only in certain
16272 // memory models; it's complicated.
16273 return;
16274 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000016275 case 'i': {
Chris Lattner22aaf1d2006-10-31 20:13:11 +000016276 // Literal immediates are always ok.
Chris Lattner48884cd2007-08-25 00:47:38 +000016277 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Op)) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000016278 // Widen to 64 bits here to get it sign extended.
Owen Anderson825b72b2009-08-11 20:47:22 +000016279 Result = DAG.getTargetConstant(CST->getSExtValue(), MVT::i64);
Chris Lattner48884cd2007-08-25 00:47:38 +000016280 break;
16281 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000016282
Dale Johannesene5ff9ef2010-06-24 20:14:51 +000016283 // In any sort of PIC mode addresses need to be computed at runtime by
16284 // adding in a register or some sort of table lookup. These can't
16285 // be used as immediates.
Dale Johannesene2b448c2010-07-06 23:27:00 +000016286 if (Subtarget->isPICStyleGOT() || Subtarget->isPICStyleStubPIC())
Dale Johannesene5ff9ef2010-06-24 20:14:51 +000016287 return;
16288
Chris Lattnerdc43a882007-05-03 16:52:29 +000016289 // If we are in non-pic codegen mode, we allow the address of a global (with
16290 // an optional displacement) to be used with 'i'.
Chris Lattner49921962009-05-08 18:23:14 +000016291 GlobalAddressSDNode *GA = 0;
Chris Lattnerdc43a882007-05-03 16:52:29 +000016292 int64_t Offset = 0;
Scott Michelfdc40a02009-02-17 22:15:04 +000016293
Chris Lattner49921962009-05-08 18:23:14 +000016294 // Match either (GA), (GA+C), (GA+C1+C2), etc.
16295 while (1) {
16296 if ((GA = dyn_cast<GlobalAddressSDNode>(Op))) {
16297 Offset += GA->getOffset();
16298 break;
16299 } else if (Op.getOpcode() == ISD::ADD) {
16300 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
16301 Offset += C->getZExtValue();
16302 Op = Op.getOperand(0);
16303 continue;
16304 }
16305 } else if (Op.getOpcode() == ISD::SUB) {
16306 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
16307 Offset += -C->getZExtValue();
16308 Op = Op.getOperand(0);
16309 continue;
16310 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000016311 }
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000016312
Chris Lattner49921962009-05-08 18:23:14 +000016313 // Otherwise, this isn't something we can handle, reject it.
16314 return;
Chris Lattnerdc43a882007-05-03 16:52:29 +000016315 }
Eric Christopherfd179292009-08-27 18:07:15 +000016316
Dan Gohman46510a72010-04-15 01:51:59 +000016317 const GlobalValue *GV = GA->getGlobal();
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000016318 // If we require an extra load to get this address, as in PIC mode, we
16319 // can't accept it.
Chris Lattner36c25012009-07-10 07:34:39 +000016320 if (isGlobalStubReference(Subtarget->ClassifyGlobalReference(GV,
16321 getTargetMachine())))
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000016322 return;
Scott Michelfdc40a02009-02-17 22:15:04 +000016323
Devang Patel0d881da2010-07-06 22:08:15 +000016324 Result = DAG.getTargetGlobalAddress(GV, Op.getDebugLoc(),
16325 GA->getValueType(0), Offset);
Chris Lattner49921962009-05-08 18:23:14 +000016326 break;
Chris Lattner22aaf1d2006-10-31 20:13:11 +000016327 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000016328 }
Scott Michelfdc40a02009-02-17 22:15:04 +000016329
Gabor Greifba36cb52008-08-28 21:40:38 +000016330 if (Result.getNode()) {
Chris Lattner48884cd2007-08-25 00:47:38 +000016331 Ops.push_back(Result);
16332 return;
16333 }
Dale Johannesen1784d162010-06-25 21:55:36 +000016334 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
Chris Lattner22aaf1d2006-10-31 20:13:11 +000016335}
16336
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000016337std::pair<unsigned, const TargetRegisterClass*>
Chris Lattnerf76d1802006-07-31 23:26:50 +000016338X86TargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +000016339 EVT VT) const {
Chris Lattnerad043e82007-04-09 05:11:28 +000016340 // First, see if this is a constraint that directly corresponds to an LLVM
16341 // register class.
16342 if (Constraint.size() == 1) {
16343 // GCC Constraint Letters
16344 switch (Constraint[0]) {
16345 default: break;
Eric Christopherd176af82011-06-29 17:23:50 +000016346 // TODO: Slight differences here in allocation order and leaving
16347 // RIP in the class. Do they matter any more here than they do
16348 // in the normal allocation?
16349 case 'q': // GENERAL_REGS in 64-bit mode, Q_REGS in 32-bit mode.
16350 if (Subtarget->is64Bit()) {
Craig Topperc9099502012-04-20 06:31:50 +000016351 if (VT == MVT::i32 || VT == MVT::f32)
16352 return std::make_pair(0U, &X86::GR32RegClass);
16353 if (VT == MVT::i16)
16354 return std::make_pair(0U, &X86::GR16RegClass);
16355 if (VT == MVT::i8 || VT == MVT::i1)
16356 return std::make_pair(0U, &X86::GR8RegClass);
16357 if (VT == MVT::i64 || VT == MVT::f64)
16358 return std::make_pair(0U, &X86::GR64RegClass);
16359 break;
Eric Christopherd176af82011-06-29 17:23:50 +000016360 }
16361 // 32-bit fallthrough
16362 case 'Q': // Q_REGS
Nick Lewycky9bf45d02011-07-08 00:19:27 +000016363 if (VT == MVT::i32 || VT == MVT::f32)
Craig Topperc9099502012-04-20 06:31:50 +000016364 return std::make_pair(0U, &X86::GR32_ABCDRegClass);
16365 if (VT == MVT::i16)
16366 return std::make_pair(0U, &X86::GR16_ABCDRegClass);
16367 if (VT == MVT::i8 || VT == MVT::i1)
16368 return std::make_pair(0U, &X86::GR8_ABCD_LRegClass);
16369 if (VT == MVT::i64)
16370 return std::make_pair(0U, &X86::GR64_ABCDRegClass);
Eric Christopherd176af82011-06-29 17:23:50 +000016371 break;
Chris Lattner0f65cad2007-04-09 05:49:22 +000016372 case 'r': // GENERAL_REGS
Chris Lattner0f65cad2007-04-09 05:49:22 +000016373 case 'l': // INDEX_REGS
Eric Christopher5427ede2011-07-14 20:13:52 +000016374 if (VT == MVT::i8 || VT == MVT::i1)
Craig Topperc9099502012-04-20 06:31:50 +000016375 return std::make_pair(0U, &X86::GR8RegClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000016376 if (VT == MVT::i16)
Craig Topperc9099502012-04-20 06:31:50 +000016377 return std::make_pair(0U, &X86::GR16RegClass);
Eric Christopher2bbecd82011-05-19 21:33:47 +000016378 if (VT == MVT::i32 || VT == MVT::f32 || !Subtarget->is64Bit())
Craig Topperc9099502012-04-20 06:31:50 +000016379 return std::make_pair(0U, &X86::GR32RegClass);
16380 return std::make_pair(0U, &X86::GR64RegClass);
Dale Johannesen5f3663e2009-10-07 22:47:20 +000016381 case 'R': // LEGACY_REGS
Eric Christopher5427ede2011-07-14 20:13:52 +000016382 if (VT == MVT::i8 || VT == MVT::i1)
Craig Topperc9099502012-04-20 06:31:50 +000016383 return std::make_pair(0U, &X86::GR8_NOREXRegClass);
Dale Johannesen5f3663e2009-10-07 22:47:20 +000016384 if (VT == MVT::i16)
Craig Topperc9099502012-04-20 06:31:50 +000016385 return std::make_pair(0U, &X86::GR16_NOREXRegClass);
Dale Johannesen5f3663e2009-10-07 22:47:20 +000016386 if (VT == MVT::i32 || !Subtarget->is64Bit())
Craig Topperc9099502012-04-20 06:31:50 +000016387 return std::make_pair(0U, &X86::GR32_NOREXRegClass);
16388 return std::make_pair(0U, &X86::GR64_NOREXRegClass);
Chris Lattnerfce84ac2008-03-11 19:06:29 +000016389 case 'f': // FP Stack registers.
16390 // If SSE is enabled for this VT, use f80 to ensure the isel moves the
16391 // value to the correct fpstack register class.
Owen Anderson825b72b2009-08-11 20:47:22 +000016392 if (VT == MVT::f32 && !isScalarFPTypeInSSEReg(VT))
Craig Topperc9099502012-04-20 06:31:50 +000016393 return std::make_pair(0U, &X86::RFP32RegClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000016394 if (VT == MVT::f64 && !isScalarFPTypeInSSEReg(VT))
Craig Topperc9099502012-04-20 06:31:50 +000016395 return std::make_pair(0U, &X86::RFP64RegClass);
16396 return std::make_pair(0U, &X86::RFP80RegClass);
Chris Lattner6c284d72007-04-12 04:14:49 +000016397 case 'y': // MMX_REGS if MMX allowed.
16398 if (!Subtarget->hasMMX()) break;
Craig Topperc9099502012-04-20 06:31:50 +000016399 return std::make_pair(0U, &X86::VR64RegClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +000016400 case 'Y': // SSE_REGS if SSE2 allowed
Craig Topper1accb7e2012-01-10 06:54:16 +000016401 if (!Subtarget->hasSSE2()) break;
Chris Lattner0f65cad2007-04-09 05:49:22 +000016402 // FALL THROUGH.
Eric Christopher55487552012-01-07 01:02:09 +000016403 case 'x': // SSE_REGS if SSE1 allowed or AVX_REGS if AVX allowed
Craig Topper1accb7e2012-01-10 06:54:16 +000016404 if (!Subtarget->hasSSE1()) break;
Duncan Sands83ec4b62008-06-06 12:08:01 +000016405
Owen Anderson825b72b2009-08-11 20:47:22 +000016406 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner0f65cad2007-04-09 05:49:22 +000016407 default: break;
16408 // Scalar SSE types.
Owen Anderson825b72b2009-08-11 20:47:22 +000016409 case MVT::f32:
16410 case MVT::i32:
Craig Topperc9099502012-04-20 06:31:50 +000016411 return std::make_pair(0U, &X86::FR32RegClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000016412 case MVT::f64:
16413 case MVT::i64:
Craig Topperc9099502012-04-20 06:31:50 +000016414 return std::make_pair(0U, &X86::FR64RegClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +000016415 // Vector types.
Owen Anderson825b72b2009-08-11 20:47:22 +000016416 case MVT::v16i8:
16417 case MVT::v8i16:
16418 case MVT::v4i32:
16419 case MVT::v2i64:
16420 case MVT::v4f32:
16421 case MVT::v2f64:
Craig Topperc9099502012-04-20 06:31:50 +000016422 return std::make_pair(0U, &X86::VR128RegClass);
Eric Christopher55487552012-01-07 01:02:09 +000016423 // AVX types.
16424 case MVT::v32i8:
16425 case MVT::v16i16:
16426 case MVT::v8i32:
16427 case MVT::v4i64:
16428 case MVT::v8f32:
16429 case MVT::v4f64:
Craig Topperc9099502012-04-20 06:31:50 +000016430 return std::make_pair(0U, &X86::VR256RegClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +000016431 }
Chris Lattnerad043e82007-04-09 05:11:28 +000016432 break;
16433 }
16434 }
Scott Michelfdc40a02009-02-17 22:15:04 +000016435
Chris Lattnerf76d1802006-07-31 23:26:50 +000016436 // Use the default implementation in TargetLowering to convert the register
16437 // constraint into a member of a register class.
16438 std::pair<unsigned, const TargetRegisterClass*> Res;
16439 Res = TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
Chris Lattner1a60aa72006-10-31 19:42:44 +000016440
16441 // Not found as a standard register?
16442 if (Res.second == 0) {
Chris Lattner56d77c72009-09-13 22:41:48 +000016443 // Map st(0) -> st(7) -> ST0
16444 if (Constraint.size() == 7 && Constraint[0] == '{' &&
16445 tolower(Constraint[1]) == 's' &&
16446 tolower(Constraint[2]) == 't' &&
16447 Constraint[3] == '(' &&
16448 (Constraint[4] >= '0' && Constraint[4] <= '7') &&
16449 Constraint[5] == ')' &&
16450 Constraint[6] == '}') {
Daniel Dunbara279bc32009-09-20 02:20:51 +000016451
Chris Lattner56d77c72009-09-13 22:41:48 +000016452 Res.first = X86::ST0+Constraint[4]-'0';
Craig Topperc9099502012-04-20 06:31:50 +000016453 Res.second = &X86::RFP80RegClass;
Chris Lattner56d77c72009-09-13 22:41:48 +000016454 return Res;
16455 }
Daniel Dunbara279bc32009-09-20 02:20:51 +000016456
Chris Lattner56d77c72009-09-13 22:41:48 +000016457 // GCC allows "st(0)" to be called just plain "st".
Benjamin Kramer05872ea2009-11-12 20:36:59 +000016458 if (StringRef("{st}").equals_lower(Constraint)) {
Chris Lattner1a60aa72006-10-31 19:42:44 +000016459 Res.first = X86::ST0;
Craig Topperc9099502012-04-20 06:31:50 +000016460 Res.second = &X86::RFP80RegClass;
Chris Lattner56d77c72009-09-13 22:41:48 +000016461 return Res;
Chris Lattner1a60aa72006-10-31 19:42:44 +000016462 }
Chris Lattner56d77c72009-09-13 22:41:48 +000016463
16464 // flags -> EFLAGS
Benjamin Kramer05872ea2009-11-12 20:36:59 +000016465 if (StringRef("{flags}").equals_lower(Constraint)) {
Chris Lattner56d77c72009-09-13 22:41:48 +000016466 Res.first = X86::EFLAGS;
Craig Topperc9099502012-04-20 06:31:50 +000016467 Res.second = &X86::CCRRegClass;
Chris Lattner56d77c72009-09-13 22:41:48 +000016468 return Res;
16469 }
Daniel Dunbara279bc32009-09-20 02:20:51 +000016470
Dale Johannesen330169f2008-11-13 21:52:36 +000016471 // 'A' means EAX + EDX.
16472 if (Constraint == "A") {
16473 Res.first = X86::EAX;
Craig Topperc9099502012-04-20 06:31:50 +000016474 Res.second = &X86::GR32_ADRegClass;
Chris Lattner56d77c72009-09-13 22:41:48 +000016475 return Res;
Dale Johannesen330169f2008-11-13 21:52:36 +000016476 }
Chris Lattner1a60aa72006-10-31 19:42:44 +000016477 return Res;
16478 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000016479
Chris Lattnerf76d1802006-07-31 23:26:50 +000016480 // Otherwise, check to see if this is a register class of the wrong value
16481 // type. For example, we want to map "{ax},i32" -> {eax}, we don't want it to
16482 // turn into {ax},{dx}.
16483 if (Res.second->hasType(VT))
16484 return Res; // Correct type already, nothing to do.
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000016485
Chris Lattnerf76d1802006-07-31 23:26:50 +000016486 // All of the single-register GCC register classes map their values onto
16487 // 16-bit register pieces "ax","dx","cx","bx","si","di","bp","sp". If we
16488 // really want an 8-bit or 32-bit register, map to the appropriate register
16489 // class and return the appropriate register.
Craig Topperc9099502012-04-20 06:31:50 +000016490 if (Res.second == &X86::GR16RegClass) {
Owen Anderson825b72b2009-08-11 20:47:22 +000016491 if (VT == MVT::i8) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000016492 unsigned DestReg = 0;
16493 switch (Res.first) {
16494 default: break;
16495 case X86::AX: DestReg = X86::AL; break;
16496 case X86::DX: DestReg = X86::DL; break;
16497 case X86::CX: DestReg = X86::CL; break;
16498 case X86::BX: DestReg = X86::BL; break;
16499 }
16500 if (DestReg) {
16501 Res.first = DestReg;
Craig Topperc9099502012-04-20 06:31:50 +000016502 Res.second = &X86::GR8RegClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000016503 }
Owen Anderson825b72b2009-08-11 20:47:22 +000016504 } else if (VT == MVT::i32) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000016505 unsigned DestReg = 0;
16506 switch (Res.first) {
16507 default: break;
16508 case X86::AX: DestReg = X86::EAX; break;
16509 case X86::DX: DestReg = X86::EDX; break;
16510 case X86::CX: DestReg = X86::ECX; break;
16511 case X86::BX: DestReg = X86::EBX; break;
16512 case X86::SI: DestReg = X86::ESI; break;
16513 case X86::DI: DestReg = X86::EDI; break;
16514 case X86::BP: DestReg = X86::EBP; break;
16515 case X86::SP: DestReg = X86::ESP; break;
16516 }
16517 if (DestReg) {
16518 Res.first = DestReg;
Craig Topperc9099502012-04-20 06:31:50 +000016519 Res.second = &X86::GR32RegClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000016520 }
Owen Anderson825b72b2009-08-11 20:47:22 +000016521 } else if (VT == MVT::i64) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000016522 unsigned DestReg = 0;
16523 switch (Res.first) {
16524 default: break;
16525 case X86::AX: DestReg = X86::RAX; break;
16526 case X86::DX: DestReg = X86::RDX; break;
16527 case X86::CX: DestReg = X86::RCX; break;
16528 case X86::BX: DestReg = X86::RBX; break;
16529 case X86::SI: DestReg = X86::RSI; break;
16530 case X86::DI: DestReg = X86::RDI; break;
16531 case X86::BP: DestReg = X86::RBP; break;
16532 case X86::SP: DestReg = X86::RSP; break;
16533 }
16534 if (DestReg) {
16535 Res.first = DestReg;
Craig Topperc9099502012-04-20 06:31:50 +000016536 Res.second = &X86::GR64RegClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000016537 }
Chris Lattnerf76d1802006-07-31 23:26:50 +000016538 }
Craig Topperc9099502012-04-20 06:31:50 +000016539 } else if (Res.second == &X86::FR32RegClass ||
16540 Res.second == &X86::FR64RegClass ||
16541 Res.second == &X86::VR128RegClass) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000016542 // Handle references to XMM physical registers that got mapped into the
16543 // wrong class. This can happen with constraints like {xmm0} where the
16544 // target independent register mapper will just pick the first match it can
16545 // find, ignoring the required type.
Eli Friedman52d418d2012-06-25 23:42:33 +000016546
16547 if (VT == MVT::f32 || VT == MVT::i32)
Craig Topperc9099502012-04-20 06:31:50 +000016548 Res.second = &X86::FR32RegClass;
Eli Friedman52d418d2012-06-25 23:42:33 +000016549 else if (VT == MVT::f64 || VT == MVT::i64)
Craig Topperc9099502012-04-20 06:31:50 +000016550 Res.second = &X86::FR64RegClass;
16551 else if (X86::VR128RegClass.hasType(VT))
16552 Res.second = &X86::VR128RegClass;
Eli Friedman52d418d2012-06-25 23:42:33 +000016553 else if (X86::VR256RegClass.hasType(VT))
16554 Res.second = &X86::VR256RegClass;
Chris Lattnerf76d1802006-07-31 23:26:50 +000016555 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000016556
Chris Lattnerf76d1802006-07-31 23:26:50 +000016557 return Res;
16558}