blob: 80dd9ef613bd15addbad7cb4dcc95032418cc045 [file] [log] [blame]
Arnold Schwaighofer92226dd2007-10-12 21:53:12 +00001//===-- X86ISelLowering.cpp - X86 DAG Lowering Implementation -------------===//
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file defines the interfaces that X86 uses to lower LLVM code into a
11// selection DAG.
12//
13//===----------------------------------------------------------------------===//
14
Evan Chengb1712452010-01-27 06:25:16 +000015#define DEBUG_TYPE "x86-isel"
Craig Topper1bf724b2012-02-19 07:15:48 +000016#include "X86ISelLowering.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000017#include "X86.h"
Evan Cheng0cc39452006-01-16 21:21:29 +000018#include "X86InstrBuilder.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000019#include "X86TargetMachine.h"
Chris Lattner8c6ed052009-09-16 01:46:41 +000020#include "X86TargetObjectFile.h"
David Greene583b68f2011-02-17 19:18:59 +000021#include "Utils/X86ShuffleDecode.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000022#include "llvm/CallingConv.h"
Evan Cheng223547a2006-01-31 22:28:30 +000023#include "llvm/Constants.h"
Evan Cheng347d5f72006-04-28 21:29:37 +000024#include "llvm/DerivedTypes.h"
Chris Lattnerb903bed2009-06-26 21:20:29 +000025#include "llvm/GlobalAlias.h"
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +000026#include "llvm/GlobalVariable.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000027#include "llvm/Function.h"
Chris Lattnerb8105652009-07-20 17:51:36 +000028#include "llvm/Instructions.h"
Evan Cheng6be2c582006-04-05 23:38:46 +000029#include "llvm/Intrinsics.h"
Owen Andersona90b3dc2009-07-15 21:51:10 +000030#include "llvm/LLVMContext.h"
Evan Cheng55d42002011-01-08 01:24:27 +000031#include "llvm/CodeGen/IntrinsicLowering.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000032#include "llvm/CodeGen/MachineFrameInfo.h"
Evan Cheng4a460802006-01-11 00:33:36 +000033#include "llvm/CodeGen/MachineFunction.h"
34#include "llvm/CodeGen/MachineInstrBuilder.h"
Chris Lattner5e1df8d2010-01-25 23:38:14 +000035#include "llvm/CodeGen/MachineJumpTableInfo.h"
Evan Chenga844bde2008-02-02 04:07:54 +000036#include "llvm/CodeGen/MachineModuleInfo.h"
Chris Lattner84bc5422007-12-31 04:13:23 +000037#include "llvm/CodeGen/MachineRegisterInfo.h"
Chris Lattner589c6f62010-01-26 06:28:43 +000038#include "llvm/MC/MCAsmInfo.h"
Chris Lattnerc64daab2010-01-26 05:02:42 +000039#include "llvm/MC/MCContext.h"
Daniel Dunbar4e815f82010-03-15 23:51:06 +000040#include "llvm/MC/MCExpr.h"
Chris Lattnerc64daab2010-01-26 05:02:42 +000041#include "llvm/MC/MCSymbol.h"
Evan Cheng14b32e12007-12-11 01:46:18 +000042#include "llvm/ADT/SmallSet.h"
Evan Chengb1712452010-01-27 06:25:16 +000043#include "llvm/ADT/Statistic.h"
Chris Lattner1a60aa72006-10-31 19:42:44 +000044#include "llvm/ADT/StringExtras.h"
Benjamin Kramer2ea4cdb2011-12-18 19:59:20 +000045#include "llvm/ADT/VariadicFunction.h"
Evan Cheng485fafc2011-03-21 01:19:09 +000046#include "llvm/Support/CallSite.h"
Chris Lattnerc64daab2010-01-26 05:02:42 +000047#include "llvm/Support/Debug.h"
48#include "llvm/Support/ErrorHandling.h"
49#include "llvm/Support/MathExtras.h"
Rafael Espindola151ab3e2011-08-30 19:47:04 +000050#include "llvm/Target/TargetOptions.h"
Benjamin Kramer9c683542012-01-30 15:16:21 +000051#include <bitset>
Joerg Sonnenberger78cab942012-08-10 10:53:56 +000052#include <cctype>
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000053using namespace llvm;
54
Evan Chengb1712452010-01-27 06:25:16 +000055STATISTIC(NumTailCalls, "Number of tail calls");
56
Evan Cheng10e86422008-04-25 19:11:04 +000057// Forward declarations.
Owen Andersone50ed302009-08-10 22:56:29 +000058static SDValue getMOVL(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +000059 SDValue V2);
Evan Cheng10e86422008-04-25 19:11:04 +000060
David Greenea5f26012011-02-07 19:36:54 +000061/// Generate a DAG to grab 128-bits from a vector > 128 bits. This
62/// sets things up to match to an AVX VEXTRACTF128 instruction or a
David Greene74a579d2011-02-10 16:57:36 +000063/// simple subregister reference. Idx is an index in the 128 bits we
64/// want. It need not be aligned to a 128-bit bounday. That makes
65/// lowering EXTRACT_VECTOR_ELT operations easier.
Craig Topperb14940a2012-04-22 20:55:18 +000066static SDValue Extract128BitVector(SDValue Vec, unsigned IdxVal,
67 SelectionDAG &DAG, DebugLoc dl) {
David Greenea5f26012011-02-07 19:36:54 +000068 EVT VT = Vec.getValueType();
Craig Topper7a9a28b2012-08-12 02:23:29 +000069 assert(VT.is256BitVector() && "Unexpected vector size!");
David Greenea5f26012011-02-07 19:36:54 +000070 EVT ElVT = VT.getVectorElementType();
Craig Topper66ddd152012-04-27 22:54:43 +000071 unsigned Factor = VT.getSizeInBits()/128;
Bruno Cardoso Lopes67727ca2011-07-21 01:55:27 +000072 EVT ResultVT = EVT::getVectorVT(*DAG.getContext(), ElVT,
73 VT.getVectorNumElements()/Factor);
David Greenea5f26012011-02-07 19:36:54 +000074
75 // Extract from UNDEF is UNDEF.
76 if (Vec.getOpcode() == ISD::UNDEF)
Craig Topper767b4f62012-04-22 19:29:34 +000077 return DAG.getUNDEF(ResultVT);
David Greenea5f26012011-02-07 19:36:54 +000078
Craig Topperb14940a2012-04-22 20:55:18 +000079 // Extract the relevant 128 bits. Generate an EXTRACT_SUBVECTOR
80 // we can match to VEXTRACTF128.
81 unsigned ElemsPerChunk = 128 / ElVT.getSizeInBits();
David Greenea5f26012011-02-07 19:36:54 +000082
Craig Topperb14940a2012-04-22 20:55:18 +000083 // This is the index of the first element of the 128-bit chunk
84 // we want.
85 unsigned NormalizedIdxVal = (((IdxVal * ElVT.getSizeInBits()) / 128)
86 * ElemsPerChunk);
David Greenea5f26012011-02-07 19:36:54 +000087
Craig Topperb8d9da12012-09-06 06:09:01 +000088 SDValue VecIdx = DAG.getIntPtrConstant(NormalizedIdxVal);
Craig Topperb14940a2012-04-22 20:55:18 +000089 SDValue Result = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, ResultVT, Vec,
90 VecIdx);
David Greenea5f26012011-02-07 19:36:54 +000091
Craig Topperb14940a2012-04-22 20:55:18 +000092 return Result;
David Greenea5f26012011-02-07 19:36:54 +000093}
94
95/// Generate a DAG to put 128-bits into a vector > 128 bits. This
96/// sets things up to match to an AVX VINSERTF128 instruction or a
David Greene6b381262011-02-09 15:32:06 +000097/// simple superregister reference. Idx is an index in the 128 bits
98/// we want. It need not be aligned to a 128-bit bounday. That makes
99/// lowering INSERT_VECTOR_ELT operations easier.
Craig Topperb14940a2012-04-22 20:55:18 +0000100static SDValue Insert128BitVector(SDValue Result, SDValue Vec,
101 unsigned IdxVal, SelectionDAG &DAG,
David Greenea5f26012011-02-07 19:36:54 +0000102 DebugLoc dl) {
Craig Topper703c38b2012-06-20 05:39:26 +0000103 // Inserting UNDEF is Result
104 if (Vec.getOpcode() == ISD::UNDEF)
105 return Result;
106
Craig Topperb14940a2012-04-22 20:55:18 +0000107 EVT VT = Vec.getValueType();
Craig Topper7a9a28b2012-08-12 02:23:29 +0000108 assert(VT.is128BitVector() && "Unexpected vector size!");
David Greenea5f26012011-02-07 19:36:54 +0000109
Craig Topperb14940a2012-04-22 20:55:18 +0000110 EVT ElVT = VT.getVectorElementType();
111 EVT ResultVT = Result.getValueType();
David Greenea5f26012011-02-07 19:36:54 +0000112
Craig Topperb14940a2012-04-22 20:55:18 +0000113 // Insert the relevant 128 bits.
114 unsigned ElemsPerChunk = 128/ElVT.getSizeInBits();
David Greenea5f26012011-02-07 19:36:54 +0000115
Craig Topperb14940a2012-04-22 20:55:18 +0000116 // This is the index of the first element of the 128-bit chunk
117 // we want.
118 unsigned NormalizedIdxVal = (((IdxVal * ElVT.getSizeInBits())/128)
119 * ElemsPerChunk);
David Greenea5f26012011-02-07 19:36:54 +0000120
Craig Topperb8d9da12012-09-06 06:09:01 +0000121 SDValue VecIdx = DAG.getIntPtrConstant(NormalizedIdxVal);
Craig Topper703c38b2012-06-20 05:39:26 +0000122 return DAG.getNode(ISD::INSERT_SUBVECTOR, dl, ResultVT, Result, Vec,
123 VecIdx);
David Greenea5f26012011-02-07 19:36:54 +0000124}
125
Craig Topper4c7972d2012-04-22 18:15:59 +0000126/// Concat two 128-bit vectors into a 256 bit vector using VINSERTF128
127/// instructions. This is used because creating CONCAT_VECTOR nodes of
128/// BUILD_VECTORS returns a larger BUILD_VECTOR while we're trying to lower
129/// large BUILD_VECTORS.
130static SDValue Concat128BitVectors(SDValue V1, SDValue V2, EVT VT,
131 unsigned NumElems, SelectionDAG &DAG,
132 DebugLoc dl) {
Craig Topperb14940a2012-04-22 20:55:18 +0000133 SDValue V = Insert128BitVector(DAG.getUNDEF(VT), V1, 0, DAG, dl);
134 return Insert128BitVector(V, V2, NumElems/2, DAG, dl);
Craig Topper4c7972d2012-04-22 18:15:59 +0000135}
136
Chris Lattnerf0144122009-07-28 03:13:23 +0000137static TargetLoweringObjectFile *createTLOF(X86TargetMachine &TM) {
Evan Cheng2bffee22011-02-01 01:14:13 +0000138 const X86Subtarget *Subtarget = &TM.getSubtarget<X86Subtarget>();
139 bool is64Bit = Subtarget->is64Bit();
NAKAMURA Takumi27635382011-02-05 15:10:54 +0000140
Evan Cheng2bffee22011-02-01 01:14:13 +0000141 if (Subtarget->isTargetEnvMacho()) {
Chris Lattnere019ec12010-12-19 20:07:10 +0000142 if (is64Bit)
Bill Wendlinga44489d2012-06-26 10:05:06 +0000143 return new X86_64MachoTargetObjectFile();
Anton Korobeynikov293d5922010-02-21 20:28:15 +0000144 return new TargetLoweringObjectFileMachO();
Michael J. Spencerec38de22010-10-10 22:04:20 +0000145 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000146
Rafael Espindolad6b43a32012-06-19 00:48:28 +0000147 if (Subtarget->isTargetLinux())
148 return new X86LinuxTargetObjectFile();
Evan Cheng203576a2011-07-20 19:50:42 +0000149 if (Subtarget->isTargetELF())
150 return new TargetLoweringObjectFileELF();
Evan Cheng2bffee22011-02-01 01:14:13 +0000151 if (Subtarget->isTargetCOFF() && !Subtarget->isTargetEnvMacho())
Chris Lattnere019ec12010-12-19 20:07:10 +0000152 return new TargetLoweringObjectFileCOFF();
Eric Christopher62f35a22010-07-05 19:26:33 +0000153 llvm_unreachable("unknown subtarget type");
Chris Lattnerf0144122009-07-28 03:13:23 +0000154}
155
Dan Gohmanc9f5f3f2008-05-14 01:58:56 +0000156X86TargetLowering::X86TargetLowering(X86TargetMachine &TM)
Chris Lattnerf0144122009-07-28 03:13:23 +0000157 : TargetLowering(TM, createTLOF(TM)) {
Evan Cheng559806f2006-01-27 08:10:46 +0000158 Subtarget = &TM.getSubtarget<X86Subtarget>();
Craig Topper1accb7e2012-01-10 06:54:16 +0000159 X86ScalarSSEf64 = Subtarget->hasSSE2();
160 X86ScalarSSEf32 = Subtarget->hasSSE1();
Anton Korobeynikovbff66b02008-09-09 18:22:57 +0000161
Anton Korobeynikov2365f512007-07-14 14:06:15 +0000162 RegInfo = TM.getRegisterInfo();
Micah Villmow3574eca2012-10-08 16:38:25 +0000163 TD = getDataLayout();
Anton Korobeynikov2365f512007-07-14 14:06:15 +0000164
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000165 // Set up the TargetLowering object.
Craig Topper9e401f22012-04-21 18:58:38 +0000166 static const MVT IntVTs[] = { MVT::i8, MVT::i16, MVT::i32, MVT::i64 };
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000167
168 // X86 is weird, it always uses i8 for shift amounts and setcc results.
Duncan Sands03228082008-11-23 15:47:28 +0000169 setBooleanContents(ZeroOrOneBooleanContent);
Duncan Sands28b77e92011-09-06 19:07:46 +0000170 // X86-SSE is even stranger. It uses -1 or 0 for vector masks.
171 setBooleanVectorContents(ZeroOrNegativeOneBooleanContent);
Eric Christopher471e4222011-06-08 23:55:35 +0000172
Eric Christopherde5e1012011-03-11 01:05:58 +0000173 // For 64-bit since we have so many registers use the ILP scheduler, for
174 // 32-bit code use the register pressure specific scheduling.
Preston Gurdc0f0a932012-05-02 22:02:02 +0000175 // For Atom, always use ILP scheduling.
Chad Rosiera20e1e72012-08-01 18:39:17 +0000176 if (Subtarget->isAtom())
Eric Christopherde5e1012011-03-11 01:05:58 +0000177 setSchedulingPreference(Sched::ILP);
Preston Gurdc0f0a932012-05-02 22:02:02 +0000178 else if (Subtarget->is64Bit())
179 setSchedulingPreference(Sched::ILP);
Eric Christopherde5e1012011-03-11 01:05:58 +0000180 else
181 setSchedulingPreference(Sched::RegPressure);
Michael Liaoc5c970e2012-10-31 04:14:09 +0000182 setStackPointerRegisterToSaveRestore(RegInfo->getStackRegister());
Evan Cheng714554d2006-03-16 21:47:42 +0000183
Preston Gurd2e2efd92012-09-04 18:22:17 +0000184 // Bypass i32 with i8 on Atom when compiling with O2
185 if (Subtarget->hasSlowDivide() && TM.getOptLevel() >= CodeGenOpt::Default)
Preston Gurd8d662b52012-10-04 21:33:40 +0000186 addBypassSlowDiv(32, 8);
Preston Gurd2e2efd92012-09-04 18:22:17 +0000187
Michael J. Spencer92bf38c2010-10-10 23:11:06 +0000188 if (Subtarget->isTargetWindows() && !Subtarget->isTargetCygMing()) {
Michael J. Spencer1802a9f2010-10-10 22:04:34 +0000189 // Setup Windows compiler runtime calls.
190 setLibcallName(RTLIB::SDIV_I64, "_alldiv");
Michael J. Spencer335b8062010-10-11 05:29:15 +0000191 setLibcallName(RTLIB::UDIV_I64, "_aulldiv");
Julien Lerougef2960822011-07-08 21:40:25 +0000192 setLibcallName(RTLIB::SREM_I64, "_allrem");
193 setLibcallName(RTLIB::UREM_I64, "_aullrem");
194 setLibcallName(RTLIB::MUL_I64, "_allmul");
Michael J. Spencer1802a9f2010-10-10 22:04:34 +0000195 setLibcallCallingConv(RTLIB::SDIV_I64, CallingConv::X86_StdCall);
Michael J. Spencer335b8062010-10-11 05:29:15 +0000196 setLibcallCallingConv(RTLIB::UDIV_I64, CallingConv::X86_StdCall);
Julien Lerougef2960822011-07-08 21:40:25 +0000197 setLibcallCallingConv(RTLIB::SREM_I64, CallingConv::X86_StdCall);
198 setLibcallCallingConv(RTLIB::UREM_I64, CallingConv::X86_StdCall);
199 setLibcallCallingConv(RTLIB::MUL_I64, CallingConv::X86_StdCall);
Michael J. Spencer1a2d0612012-02-24 19:01:22 +0000200
201 // The _ftol2 runtime function has an unusual calling conv, which
202 // is modeled by a special pseudo-instruction.
203 setLibcallName(RTLIB::FPTOUINT_F64_I64, 0);
204 setLibcallName(RTLIB::FPTOUINT_F32_I64, 0);
205 setLibcallName(RTLIB::FPTOUINT_F64_I32, 0);
206 setLibcallName(RTLIB::FPTOUINT_F32_I32, 0);
Michael J. Spencer1802a9f2010-10-10 22:04:34 +0000207 }
208
Anton Korobeynikovd27a2582006-12-10 23:12:42 +0000209 if (Subtarget->isTargetDarwin()) {
Evan Chengdf57fa02006-03-17 20:31:41 +0000210 // Darwin should use _setjmp/_longjmp instead of setjmp/longjmp.
Anton Korobeynikovd27a2582006-12-10 23:12:42 +0000211 setUseUnderscoreSetJmp(false);
212 setUseUnderscoreLongJmp(false);
Anton Korobeynikov317848f2007-01-03 11:43:14 +0000213 } else if (Subtarget->isTargetMingw()) {
Anton Korobeynikovd27a2582006-12-10 23:12:42 +0000214 // MS runtime is weird: it exports _setjmp, but longjmp!
215 setUseUnderscoreSetJmp(true);
216 setUseUnderscoreLongJmp(false);
217 } else {
218 setUseUnderscoreSetJmp(true);
219 setUseUnderscoreLongJmp(true);
220 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000221
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000222 // Set up the register classes.
Craig Topperc9099502012-04-20 06:31:50 +0000223 addRegisterClass(MVT::i8, &X86::GR8RegClass);
224 addRegisterClass(MVT::i16, &X86::GR16RegClass);
225 addRegisterClass(MVT::i32, &X86::GR32RegClass);
Evan Cheng25ab6902006-09-08 06:48:29 +0000226 if (Subtarget->is64Bit())
Craig Topperc9099502012-04-20 06:31:50 +0000227 addRegisterClass(MVT::i64, &X86::GR64RegClass);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000228
Owen Anderson825b72b2009-08-11 20:47:22 +0000229 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
Evan Chengc5484282006-10-04 00:56:09 +0000230
Scott Michelfdc40a02009-02-17 22:15:04 +0000231 // We don't accept any truncstore of integer registers.
Owen Anderson825b72b2009-08-11 20:47:22 +0000232 setTruncStoreAction(MVT::i64, MVT::i32, Expand);
Dan Gohman71edb242010-04-30 18:30:26 +0000233 setTruncStoreAction(MVT::i64, MVT::i16, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000234 setTruncStoreAction(MVT::i64, MVT::i8 , Expand);
Dan Gohman71edb242010-04-30 18:30:26 +0000235 setTruncStoreAction(MVT::i32, MVT::i16, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000236 setTruncStoreAction(MVT::i32, MVT::i8 , Expand);
237 setTruncStoreAction(MVT::i16, MVT::i8, Expand);
Evan Cheng7f042682008-10-15 02:05:31 +0000238
239 // SETOEQ and SETUNE require checking two conditions.
Owen Anderson825b72b2009-08-11 20:47:22 +0000240 setCondCodeAction(ISD::SETOEQ, MVT::f32, Expand);
241 setCondCodeAction(ISD::SETOEQ, MVT::f64, Expand);
242 setCondCodeAction(ISD::SETOEQ, MVT::f80, Expand);
243 setCondCodeAction(ISD::SETUNE, MVT::f32, Expand);
244 setCondCodeAction(ISD::SETUNE, MVT::f64, Expand);
245 setCondCodeAction(ISD::SETUNE, MVT::f80, Expand);
Chris Lattnerddf89562008-01-17 19:59:44 +0000246
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000247 // Promote all UINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have this
248 // operation.
Owen Anderson825b72b2009-08-11 20:47:22 +0000249 setOperationAction(ISD::UINT_TO_FP , MVT::i1 , Promote);
250 setOperationAction(ISD::UINT_TO_FP , MVT::i8 , Promote);
251 setOperationAction(ISD::UINT_TO_FP , MVT::i16 , Promote);
Evan Cheng6892f282006-01-17 02:32:49 +0000252
Evan Cheng25ab6902006-09-08 06:48:29 +0000253 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000254 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Promote);
Bill Wendling397ae212012-01-05 02:13:20 +0000255 setOperationAction(ISD::UINT_TO_FP , MVT::i64 , Custom);
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000256 } else if (!TM.Options.UseSoftFloat) {
Dale Johannesen8d908eb2010-05-15 18:51:12 +0000257 // We have an algorithm for SSE2->double, and we turn this into a
258 // 64-bit FILD followed by conditional FADD for other targets.
259 setOperationAction(ISD::UINT_TO_FP , MVT::i64 , Custom);
Eli Friedman948e95a2009-05-23 09:59:16 +0000260 // We have an algorithm for SSE2, and we turn this into a 64-bit
261 // FILD for other targets.
Dale Johannesen8d908eb2010-05-15 18:51:12 +0000262 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000263 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000264
265 // Promote i1/i8 SINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have
266 // this operation.
Owen Anderson825b72b2009-08-11 20:47:22 +0000267 setOperationAction(ISD::SINT_TO_FP , MVT::i1 , Promote);
268 setOperationAction(ISD::SINT_TO_FP , MVT::i8 , Promote);
Bill Wendling105be5a2009-03-13 08:41:47 +0000269
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000270 if (!TM.Options.UseSoftFloat) {
Bill Wendling105be5a2009-03-13 08:41:47 +0000271 // SSE has no i16 to fp conversion, only i32
272 if (X86ScalarSSEf32) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000273 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Promote);
Bill Wendling105be5a2009-03-13 08:41:47 +0000274 // f32 and f64 cases are Legal, f80 case is not
Owen Anderson825b72b2009-08-11 20:47:22 +0000275 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Custom);
Bill Wendling105be5a2009-03-13 08:41:47 +0000276 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000277 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Custom);
278 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Custom);
Bill Wendling105be5a2009-03-13 08:41:47 +0000279 }
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000280 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000281 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Promote);
282 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Promote);
Evan Cheng5298bcc2006-02-17 07:01:52 +0000283 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000284
Dale Johannesen73328d12007-09-19 23:55:34 +0000285 // In 32-bit mode these are custom lowered. In 64-bit mode F32 and F64
286 // are Legal, f80 is custom lowered.
Owen Anderson825b72b2009-08-11 20:47:22 +0000287 setOperationAction(ISD::FP_TO_SINT , MVT::i64 , Custom);
288 setOperationAction(ISD::SINT_TO_FP , MVT::i64 , Custom);
Evan Cheng6dab0532006-01-30 08:02:57 +0000289
Evan Cheng02568ff2006-01-30 22:13:22 +0000290 // Promote i1/i8 FP_TO_SINT to larger FP_TO_SINTS's, as X86 doesn't have
291 // this operation.
Owen Anderson825b72b2009-08-11 20:47:22 +0000292 setOperationAction(ISD::FP_TO_SINT , MVT::i1 , Promote);
293 setOperationAction(ISD::FP_TO_SINT , MVT::i8 , Promote);
Evan Cheng02568ff2006-01-30 22:13:22 +0000294
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000295 if (X86ScalarSSEf32) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000296 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Promote);
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000297 // f32 and f64 cases are Legal, f80 case is not
Owen Anderson825b72b2009-08-11 20:47:22 +0000298 setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom);
Evan Cheng02568ff2006-01-30 22:13:22 +0000299 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000300 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Custom);
301 setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000302 }
303
304 // Handle FP_TO_UINT by promoting the destination to a larger signed
305 // conversion.
Owen Anderson825b72b2009-08-11 20:47:22 +0000306 setOperationAction(ISD::FP_TO_UINT , MVT::i1 , Promote);
307 setOperationAction(ISD::FP_TO_UINT , MVT::i8 , Promote);
308 setOperationAction(ISD::FP_TO_UINT , MVT::i16 , Promote);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000309
Evan Cheng25ab6902006-09-08 06:48:29 +0000310 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000311 setOperationAction(ISD::FP_TO_UINT , MVT::i64 , Expand);
312 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Promote);
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000313 } else if (!TM.Options.UseSoftFloat) {
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +0000314 // Since AVX is a superset of SSE3, only check for SSE here.
315 if (Subtarget->hasSSE1() && !Subtarget->hasSSE3())
Evan Cheng25ab6902006-09-08 06:48:29 +0000316 // Expand FP_TO_UINT into a select.
317 // FIXME: We would like to use a Custom expander here eventually to do
318 // the optimal thing for SSE vs. the default expansion in the legalizer.
Owen Anderson825b72b2009-08-11 20:47:22 +0000319 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Expand);
Evan Cheng25ab6902006-09-08 06:48:29 +0000320 else
Eli Friedman948e95a2009-05-23 09:59:16 +0000321 // With SSE3 we can use fisttpll to convert to a signed i64; without
322 // SSE, we're stuck with a fistpll.
Owen Anderson825b72b2009-08-11 20:47:22 +0000323 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000324 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000325
Michael J. Spencer1a2d0612012-02-24 19:01:22 +0000326 if (isTargetFTOL()) {
327 // Use the _ftol2 runtime function, which has a pseudo-instruction
328 // to handle its weird calling convention.
329 setOperationAction(ISD::FP_TO_UINT , MVT::i64 , Custom);
330 }
331
Chris Lattner399610a2006-12-05 18:22:22 +0000332 // TODO: when we have SSE, these could be more efficient, by using movd/movq.
Michael J. Spencerec38de22010-10-10 22:04:20 +0000333 if (!X86ScalarSSEf64) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000334 setOperationAction(ISD::BITCAST , MVT::f32 , Expand);
335 setOperationAction(ISD::BITCAST , MVT::i32 , Expand);
Dale Johannesene39859a2010-05-21 18:40:15 +0000336 if (Subtarget->is64Bit()) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000337 setOperationAction(ISD::BITCAST , MVT::f64 , Expand);
Dale Johannesen0488fb62010-09-30 23:57:10 +0000338 // Without SSE, i64->f64 goes through memory.
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000339 setOperationAction(ISD::BITCAST , MVT::i64 , Expand);
Dale Johannesen7d07b482010-05-21 00:52:33 +0000340 }
Chris Lattnerf3597a12006-12-05 18:45:06 +0000341 }
Chris Lattner21f66852005-12-23 05:15:23 +0000342
Dan Gohmanb00ee212008-02-18 19:34:53 +0000343 // Scalar integer divide and remainder are lowered to use operations that
344 // produce two results, to match the available instructions. This exposes
345 // the two-result form to trivial CSE, which is able to combine x/y and x%y
346 // into a single instruction.
347 //
348 // Scalar integer multiply-high is also lowered to use two-result
349 // operations, to match the available instructions. However, plain multiply
350 // (low) operations are left as Legal, as there are single-result
351 // instructions for this in x86. Using the two-result multiply instructions
352 // when both high and low results are needed must be arranged by dagcombine.
Craig Topper9e401f22012-04-21 18:58:38 +0000353 for (unsigned i = 0; i != array_lengthof(IntVTs); ++i) {
Chris Lattnere019ec12010-12-19 20:07:10 +0000354 MVT VT = IntVTs[i];
355 setOperationAction(ISD::MULHS, VT, Expand);
356 setOperationAction(ISD::MULHU, VT, Expand);
357 setOperationAction(ISD::SDIV, VT, Expand);
358 setOperationAction(ISD::UDIV, VT, Expand);
359 setOperationAction(ISD::SREM, VT, Expand);
360 setOperationAction(ISD::UREM, VT, Expand);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000361
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +0000362 // Add/Sub overflow ops with MVT::Glues are lowered to EFLAGS dependences.
Chris Lattnerd8ff7ec2010-12-20 01:03:27 +0000363 setOperationAction(ISD::ADDC, VT, Custom);
364 setOperationAction(ISD::ADDE, VT, Custom);
365 setOperationAction(ISD::SUBC, VT, Custom);
366 setOperationAction(ISD::SUBE, VT, Custom);
Chris Lattnere019ec12010-12-19 20:07:10 +0000367 }
Dan Gohmana37c9f72007-09-25 18:23:27 +0000368
Owen Anderson825b72b2009-08-11 20:47:22 +0000369 setOperationAction(ISD::BR_JT , MVT::Other, Expand);
370 setOperationAction(ISD::BRCOND , MVT::Other, Custom);
371 setOperationAction(ISD::BR_CC , MVT::Other, Expand);
372 setOperationAction(ISD::SELECT_CC , MVT::Other, Expand);
Evan Cheng25ab6902006-09-08 06:48:29 +0000373 if (Subtarget->is64Bit())
Owen Anderson825b72b2009-08-11 20:47:22 +0000374 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i32, Legal);
375 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16 , Legal);
376 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8 , Legal);
377 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1 , Expand);
378 setOperationAction(ISD::FP_ROUND_INREG , MVT::f32 , Expand);
379 setOperationAction(ISD::FREM , MVT::f32 , Expand);
380 setOperationAction(ISD::FREM , MVT::f64 , Expand);
381 setOperationAction(ISD::FREM , MVT::f80 , Expand);
382 setOperationAction(ISD::FLT_ROUNDS_ , MVT::i32 , Custom);
Scott Michelfdc40a02009-02-17 22:15:04 +0000383
Chandler Carruth77821022011-12-24 12:12:34 +0000384 // Promote the i8 variants and force them on up to i32 which has a shorter
385 // encoding.
386 setOperationAction(ISD::CTTZ , MVT::i8 , Promote);
387 AddPromotedToType (ISD::CTTZ , MVT::i8 , MVT::i32);
388 setOperationAction(ISD::CTTZ_ZERO_UNDEF , MVT::i8 , Promote);
389 AddPromotedToType (ISD::CTTZ_ZERO_UNDEF , MVT::i8 , MVT::i32);
Craig Topper909652f2011-10-14 03:21:46 +0000390 if (Subtarget->hasBMI()) {
Chandler Carruthd873a4b2011-12-24 11:11:38 +0000391 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::i16 , Expand);
392 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::i32 , Expand);
393 if (Subtarget->is64Bit())
394 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::i64, Expand);
Craig Topper909652f2011-10-14 03:21:46 +0000395 } else {
Craig Topper909652f2011-10-14 03:21:46 +0000396 setOperationAction(ISD::CTTZ , MVT::i16 , Custom);
397 setOperationAction(ISD::CTTZ , MVT::i32 , Custom);
398 if (Subtarget->is64Bit())
399 setOperationAction(ISD::CTTZ , MVT::i64 , Custom);
400 }
Craig Topper37f21672011-10-11 06:44:02 +0000401
402 if (Subtarget->hasLZCNT()) {
Chandler Carruth77821022011-12-24 12:12:34 +0000403 // When promoting the i8 variants, force them to i32 for a shorter
404 // encoding.
Craig Topper37f21672011-10-11 06:44:02 +0000405 setOperationAction(ISD::CTLZ , MVT::i8 , Promote);
Chandler Carruth77821022011-12-24 12:12:34 +0000406 AddPromotedToType (ISD::CTLZ , MVT::i8 , MVT::i32);
407 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i8 , Promote);
408 AddPromotedToType (ISD::CTLZ_ZERO_UNDEF, MVT::i8 , MVT::i32);
Chandler Carruthacc068e2011-12-24 10:55:54 +0000409 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i16 , Expand);
410 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i32 , Expand);
411 if (Subtarget->is64Bit())
412 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i64, Expand);
Craig Topper37f21672011-10-11 06:44:02 +0000413 } else {
414 setOperationAction(ISD::CTLZ , MVT::i8 , Custom);
415 setOperationAction(ISD::CTLZ , MVT::i16 , Custom);
416 setOperationAction(ISD::CTLZ , MVT::i32 , Custom);
Chandler Carruthacc068e2011-12-24 10:55:54 +0000417 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i8 , Custom);
418 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i16 , Custom);
419 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i32 , Custom);
420 if (Subtarget->is64Bit()) {
Craig Topper37f21672011-10-11 06:44:02 +0000421 setOperationAction(ISD::CTLZ , MVT::i64 , Custom);
Chandler Carruthacc068e2011-12-24 10:55:54 +0000422 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i64, Custom);
423 }
Evan Cheng25ab6902006-09-08 06:48:29 +0000424 }
425
Benjamin Kramer1292c222010-12-04 20:32:23 +0000426 if (Subtarget->hasPOPCNT()) {
427 setOperationAction(ISD::CTPOP , MVT::i8 , Promote);
428 } else {
429 setOperationAction(ISD::CTPOP , MVT::i8 , Expand);
430 setOperationAction(ISD::CTPOP , MVT::i16 , Expand);
431 setOperationAction(ISD::CTPOP , MVT::i32 , Expand);
432 if (Subtarget->is64Bit())
433 setOperationAction(ISD::CTPOP , MVT::i64 , Expand);
434 }
435
Owen Anderson825b72b2009-08-11 20:47:22 +0000436 setOperationAction(ISD::READCYCLECOUNTER , MVT::i64 , Custom);
437 setOperationAction(ISD::BSWAP , MVT::i16 , Expand);
Nate Begeman35ef9132006-01-11 21:21:00 +0000438
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000439 // These should be promoted to a larger select which is supported.
Dan Gohmancbbea0f2009-08-27 00:14:12 +0000440 setOperationAction(ISD::SELECT , MVT::i1 , Promote);
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000441 // X86 wants to expand cmov itself.
Dan Gohmancbbea0f2009-08-27 00:14:12 +0000442 setOperationAction(ISD::SELECT , MVT::i8 , Custom);
Chris Lattnere019ec12010-12-19 20:07:10 +0000443 setOperationAction(ISD::SELECT , MVT::i16 , Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000444 setOperationAction(ISD::SELECT , MVT::i32 , Custom);
445 setOperationAction(ISD::SELECT , MVT::f32 , Custom);
446 setOperationAction(ISD::SELECT , MVT::f64 , Custom);
447 setOperationAction(ISD::SELECT , MVT::f80 , Custom);
448 setOperationAction(ISD::SETCC , MVT::i8 , Custom);
Dan Gohman71edb242010-04-30 18:30:26 +0000449 setOperationAction(ISD::SETCC , MVT::i16 , Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000450 setOperationAction(ISD::SETCC , MVT::i32 , Custom);
451 setOperationAction(ISD::SETCC , MVT::f32 , Custom);
452 setOperationAction(ISD::SETCC , MVT::f64 , Custom);
453 setOperationAction(ISD::SETCC , MVT::f80 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000454 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000455 setOperationAction(ISD::SELECT , MVT::i64 , Custom);
Andrew Trickf6c39412011-03-23 23:11:02 +0000456 setOperationAction(ISD::SETCC , MVT::i64 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000457 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000458 setOperationAction(ISD::EH_RETURN , MVT::Other, Custom);
Michael Liao6c0e04c2012-10-15 22:39:43 +0000459 // NOTE: EH_SJLJ_SETJMP/_LONGJMP supported here is NOT intened to support
460 // SjLj exception handling but a light-weight setjmp/longjmp replacement to
Michael Liao281ae5a2012-10-17 02:22:27 +0000461 // support continuation, user-level threading, and etc.. As a result, no
Michael Liao6c0e04c2012-10-15 22:39:43 +0000462 // other SjLj exception interfaces are implemented and please don't build
463 // your own exception handling based on them.
464 // LLVM/Clang supports zero-cost DWARF exception handling.
465 setOperationAction(ISD::EH_SJLJ_SETJMP, MVT::i32, Custom);
466 setOperationAction(ISD::EH_SJLJ_LONGJMP, MVT::Other, Custom);
Anton Korobeynikov2365f512007-07-14 14:06:15 +0000467
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000468 // Darwin ABI issue.
Owen Anderson825b72b2009-08-11 20:47:22 +0000469 setOperationAction(ISD::ConstantPool , MVT::i32 , Custom);
470 setOperationAction(ISD::JumpTable , MVT::i32 , Custom);
471 setOperationAction(ISD::GlobalAddress , MVT::i32 , Custom);
472 setOperationAction(ISD::GlobalTLSAddress, MVT::i32 , Custom);
Anton Korobeynikov6625eff2008-05-04 21:36:32 +0000473 if (Subtarget->is64Bit())
Owen Anderson825b72b2009-08-11 20:47:22 +0000474 setOperationAction(ISD::GlobalTLSAddress, MVT::i64, Custom);
475 setOperationAction(ISD::ExternalSymbol , MVT::i32 , Custom);
Dan Gohmanf705adb2009-10-30 01:28:02 +0000476 setOperationAction(ISD::BlockAddress , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000477 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000478 setOperationAction(ISD::ConstantPool , MVT::i64 , Custom);
479 setOperationAction(ISD::JumpTable , MVT::i64 , Custom);
480 setOperationAction(ISD::GlobalAddress , MVT::i64 , Custom);
481 setOperationAction(ISD::ExternalSymbol, MVT::i64 , Custom);
Dan Gohmanf705adb2009-10-30 01:28:02 +0000482 setOperationAction(ISD::BlockAddress , MVT::i64 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000483 }
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000484 // 64-bit addm sub, shl, sra, srl (iff 32-bit x86)
Owen Anderson825b72b2009-08-11 20:47:22 +0000485 setOperationAction(ISD::SHL_PARTS , MVT::i32 , Custom);
486 setOperationAction(ISD::SRA_PARTS , MVT::i32 , Custom);
487 setOperationAction(ISD::SRL_PARTS , MVT::i32 , Custom);
Dan Gohman4c1fa612008-03-03 22:22:09 +0000488 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000489 setOperationAction(ISD::SHL_PARTS , MVT::i64 , Custom);
490 setOperationAction(ISD::SRA_PARTS , MVT::i64 , Custom);
491 setOperationAction(ISD::SRL_PARTS , MVT::i64 , Custom);
Dan Gohman4c1fa612008-03-03 22:22:09 +0000492 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000493
Craig Topper1accb7e2012-01-10 06:54:16 +0000494 if (Subtarget->hasSSE1())
Owen Anderson825b72b2009-08-11 20:47:22 +0000495 setOperationAction(ISD::PREFETCH , MVT::Other, Legal);
Evan Cheng27b7db52008-03-08 00:58:38 +0000496
Eric Christopher9a9d2752010-07-22 02:48:34 +0000497 setOperationAction(ISD::MEMBARRIER , MVT::Other, Custom);
Eli Friedman14648462011-07-27 22:21:52 +0000498 setOperationAction(ISD::ATOMIC_FENCE , MVT::Other, Custom);
Michael J. Spencerec38de22010-10-10 22:04:20 +0000499
Jim Grosbachf1ab49e2010-06-23 16:25:07 +0000500 // On X86 and X86-64, atomic operations are lowered to locked instructions.
501 // Locked instructions, in turn, have implicit fence semantics (all memory
502 // operations are flushed before issuing the locked instruction, and they
503 // are not buffered), so we can fold away the common pattern of
504 // fence-atomic-fence.
505 setShouldFoldAtomicFences(true);
Andrew Lenharthd497d9f2008-02-16 14:46:26 +0000506
Mon P Wang63307c32008-05-05 19:05:59 +0000507 // Expand certain atomics
Craig Topper9e401f22012-04-21 18:58:38 +0000508 for (unsigned i = 0; i != array_lengthof(IntVTs); ++i) {
Chris Lattnere019ec12010-12-19 20:07:10 +0000509 MVT VT = IntVTs[i];
510 setOperationAction(ISD::ATOMIC_CMP_SWAP, VT, Custom);
511 setOperationAction(ISD::ATOMIC_LOAD_SUB, VT, Custom);
Eli Friedman327236c2011-08-24 20:50:09 +0000512 setOperationAction(ISD::ATOMIC_STORE, VT, Custom);
Chris Lattnere019ec12010-12-19 20:07:10 +0000513 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000514
Dale Johannesen48c1bc22008-10-02 18:53:47 +0000515 if (!Subtarget->is64Bit()) {
Eli Friedmanf8f90f02011-08-24 22:33:28 +0000516 setOperationAction(ISD::ATOMIC_LOAD, MVT::i64, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000517 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i64, Custom);
518 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i64, Custom);
519 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i64, Custom);
520 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i64, Custom);
521 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i64, Custom);
522 setOperationAction(ISD::ATOMIC_LOAD_NAND, MVT::i64, Custom);
523 setOperationAction(ISD::ATOMIC_SWAP, MVT::i64, Custom);
Michael Liaoe5e8f762012-09-25 18:08:13 +0000524 setOperationAction(ISD::ATOMIC_LOAD_MAX, MVT::i64, Custom);
525 setOperationAction(ISD::ATOMIC_LOAD_MIN, MVT::i64, Custom);
526 setOperationAction(ISD::ATOMIC_LOAD_UMAX, MVT::i64, Custom);
527 setOperationAction(ISD::ATOMIC_LOAD_UMIN, MVT::i64, Custom);
Dale Johannesen48c1bc22008-10-02 18:53:47 +0000528 }
529
Eli Friedman43f51ae2011-08-26 21:21:21 +0000530 if (Subtarget->hasCmpxchg16b()) {
531 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i128, Custom);
532 }
533
Evan Cheng3c992d22006-03-07 02:02:57 +0000534 // FIXME - use subtarget debug flags
Anton Korobeynikovab4022f2006-10-31 08:31:24 +0000535 if (!Subtarget->isTargetDarwin() &&
536 !Subtarget->isTargetELF() &&
Dan Gohman44066042008-07-01 00:05:16 +0000537 !Subtarget->isTargetCygMing()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000538 setOperationAction(ISD::EH_LABEL, MVT::Other, Expand);
Dan Gohman44066042008-07-01 00:05:16 +0000539 }
Chris Lattnerf73bae12005-11-29 06:16:21 +0000540
Owen Anderson825b72b2009-08-11 20:47:22 +0000541 setOperationAction(ISD::EXCEPTIONADDR, MVT::i64, Expand);
542 setOperationAction(ISD::EHSELECTION, MVT::i64, Expand);
543 setOperationAction(ISD::EXCEPTIONADDR, MVT::i32, Expand);
544 setOperationAction(ISD::EHSELECTION, MVT::i32, Expand);
Anton Korobeynikovce3b4652007-05-02 19:53:33 +0000545 if (Subtarget->is64Bit()) {
Anton Korobeynikovce3b4652007-05-02 19:53:33 +0000546 setExceptionPointerRegister(X86::RAX);
547 setExceptionSelectorRegister(X86::RDX);
548 } else {
549 setExceptionPointerRegister(X86::EAX);
550 setExceptionSelectorRegister(X86::EDX);
551 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000552 setOperationAction(ISD::FRAME_TO_ARGS_OFFSET, MVT::i32, Custom);
553 setOperationAction(ISD::FRAME_TO_ARGS_OFFSET, MVT::i64, Custom);
Anton Korobeynikov260a6b82008-09-08 21:12:11 +0000554
Duncan Sands4a544a72011-09-06 13:37:06 +0000555 setOperationAction(ISD::INIT_TRAMPOLINE, MVT::Other, Custom);
556 setOperationAction(ISD::ADJUST_TRAMPOLINE, MVT::Other, Custom);
Duncan Sandsb116fac2007-07-27 20:02:49 +0000557
Owen Anderson825b72b2009-08-11 20:47:22 +0000558 setOperationAction(ISD::TRAP, MVT::Other, Legal);
Shuxin Yang970755e2012-10-19 20:11:16 +0000559 setOperationAction(ISD::DEBUGTRAP, MVT::Other, Legal);
Anton Korobeynikov66fac792008-01-15 07:02:33 +0000560
Nate Begemanacc398c2006-01-25 18:21:52 +0000561 // VASTART needs to be custom lowered to use the VarArgsFrameIndex
Owen Anderson825b72b2009-08-11 20:47:22 +0000562 setOperationAction(ISD::VASTART , MVT::Other, Custom);
563 setOperationAction(ISD::VAEND , MVT::Other, Expand);
Dan Gohman9018e832008-05-10 01:26:14 +0000564 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000565 setOperationAction(ISD::VAARG , MVT::Other, Custom);
566 setOperationAction(ISD::VACOPY , MVT::Other, Custom);
Dan Gohman9018e832008-05-10 01:26:14 +0000567 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000568 setOperationAction(ISD::VAARG , MVT::Other, Expand);
569 setOperationAction(ISD::VACOPY , MVT::Other, Expand);
Dan Gohman9018e832008-05-10 01:26:14 +0000570 }
Evan Chengae642192007-03-02 23:16:35 +0000571
Owen Anderson825b72b2009-08-11 20:47:22 +0000572 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
573 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
Eric Christopherc967ad82011-08-31 04:17:21 +0000574
575 if (Subtarget->isTargetCOFF() && !Subtarget->isTargetEnvMacho())
576 setOperationAction(ISD::DYNAMIC_STACKALLOC, Subtarget->is64Bit() ?
577 MVT::i64 : MVT::i32, Custom);
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000578 else if (TM.Options.EnableSegmentedStacks)
Eric Christopherc967ad82011-08-31 04:17:21 +0000579 setOperationAction(ISD::DYNAMIC_STACKALLOC, Subtarget->is64Bit() ?
580 MVT::i64 : MVT::i32, Custom);
581 else
582 setOperationAction(ISD::DYNAMIC_STACKALLOC, Subtarget->is64Bit() ?
583 MVT::i64 : MVT::i32, Expand);
Chris Lattnerb99329e2006-01-13 02:42:53 +0000584
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000585 if (!TM.Options.UseSoftFloat && X86ScalarSSEf64) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000586 // f32 and f64 use SSE.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000587 // Set up the FP register classes.
Craig Topperc9099502012-04-20 06:31:50 +0000588 addRegisterClass(MVT::f32, &X86::FR32RegClass);
589 addRegisterClass(MVT::f64, &X86::FR64RegClass);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000590
Evan Cheng223547a2006-01-31 22:28:30 +0000591 // Use ANDPD to simulate FABS.
Owen Anderson825b72b2009-08-11 20:47:22 +0000592 setOperationAction(ISD::FABS , MVT::f64, Custom);
593 setOperationAction(ISD::FABS , MVT::f32, Custom);
Evan Cheng223547a2006-01-31 22:28:30 +0000594
595 // Use XORP to simulate FNEG.
Owen Anderson825b72b2009-08-11 20:47:22 +0000596 setOperationAction(ISD::FNEG , MVT::f64, Custom);
597 setOperationAction(ISD::FNEG , MVT::f32, Custom);
Evan Cheng223547a2006-01-31 22:28:30 +0000598
Evan Cheng68c47cb2007-01-05 07:55:56 +0000599 // Use ANDPD and ORPD to simulate FCOPYSIGN.
Owen Anderson825b72b2009-08-11 20:47:22 +0000600 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
601 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
Evan Cheng68c47cb2007-01-05 07:55:56 +0000602
Stuart Hastings4fd0dee2011-06-01 04:39:42 +0000603 // Lower this to FGETSIGNx86 plus an AND.
604 setOperationAction(ISD::FGETSIGN, MVT::i64, Custom);
605 setOperationAction(ISD::FGETSIGN, MVT::i32, Custom);
606
Evan Chengd25e9e82006-02-02 00:28:23 +0000607 // We don't support sin/cos/fmod
Owen Anderson825b72b2009-08-11 20:47:22 +0000608 setOperationAction(ISD::FSIN , MVT::f64, Expand);
609 setOperationAction(ISD::FCOS , MVT::f64, Expand);
610 setOperationAction(ISD::FSIN , MVT::f32, Expand);
611 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000612
Chris Lattnera54aa942006-01-29 06:26:08 +0000613 // Expand FP immediates into loads from the stack, except for the special
614 // cases we handle.
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000615 addLegalFPImmediate(APFloat(+0.0)); // xorpd
616 addLegalFPImmediate(APFloat(+0.0f)); // xorps
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000617 } else if (!TM.Options.UseSoftFloat && X86ScalarSSEf32) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000618 // Use SSE for f32, x87 for f64.
619 // Set up the FP register classes.
Craig Topperc9099502012-04-20 06:31:50 +0000620 addRegisterClass(MVT::f32, &X86::FR32RegClass);
621 addRegisterClass(MVT::f64, &X86::RFP64RegClass);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000622
623 // Use ANDPS to simulate FABS.
Owen Anderson825b72b2009-08-11 20:47:22 +0000624 setOperationAction(ISD::FABS , MVT::f32, Custom);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000625
626 // Use XORP to simulate FNEG.
Owen Anderson825b72b2009-08-11 20:47:22 +0000627 setOperationAction(ISD::FNEG , MVT::f32, Custom);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000628
Owen Anderson825b72b2009-08-11 20:47:22 +0000629 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000630
631 // Use ANDPS and ORPS to simulate FCOPYSIGN.
Owen Anderson825b72b2009-08-11 20:47:22 +0000632 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
633 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000634
635 // We don't support sin/cos/fmod
Owen Anderson825b72b2009-08-11 20:47:22 +0000636 setOperationAction(ISD::FSIN , MVT::f32, Expand);
637 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000638
Nate Begemane1795842008-02-14 08:57:00 +0000639 // Special cases we handle for FP constants.
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000640 addLegalFPImmediate(APFloat(+0.0f)); // xorps
641 addLegalFPImmediate(APFloat(+0.0)); // FLD0
642 addLegalFPImmediate(APFloat(+1.0)); // FLD1
643 addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS
644 addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS
645
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000646 if (!TM.Options.UnsafeFPMath) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000647 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
648 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000649 }
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000650 } else if (!TM.Options.UseSoftFloat) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000651 // f32 and f64 in x87.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000652 // Set up the FP register classes.
Craig Topperc9099502012-04-20 06:31:50 +0000653 addRegisterClass(MVT::f64, &X86::RFP64RegClass);
654 addRegisterClass(MVT::f32, &X86::RFP32RegClass);
Anton Korobeynikov12c49af2006-11-21 00:01:06 +0000655
Owen Anderson825b72b2009-08-11 20:47:22 +0000656 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
657 setOperationAction(ISD::UNDEF, MVT::f32, Expand);
658 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
659 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Expand);
Dale Johannesen5411a392007-08-09 01:04:01 +0000660
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000661 if (!TM.Options.UnsafeFPMath) {
Benjamin Kramer562b2402012-09-15 12:44:27 +0000662 setOperationAction(ISD::FSIN , MVT::f32 , Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000663 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
Benjamin Kramer562b2402012-09-15 12:44:27 +0000664 setOperationAction(ISD::FCOS , MVT::f32 , Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000665 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000666 }
Dale Johannesenf04afdb2007-08-30 00:23:21 +0000667 addLegalFPImmediate(APFloat(+0.0)); // FLD0
668 addLegalFPImmediate(APFloat(+1.0)); // FLD1
669 addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS
670 addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000671 addLegalFPImmediate(APFloat(+0.0f)); // FLD0
672 addLegalFPImmediate(APFloat(+1.0f)); // FLD1
673 addLegalFPImmediate(APFloat(-0.0f)); // FLD0/FCHS
674 addLegalFPImmediate(APFloat(-1.0f)); // FLD1/FCHS
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000675 }
Evan Cheng470a6ad2006-02-22 02:26:30 +0000676
Cameron Zwarich33390842011-07-08 21:39:21 +0000677 // We don't support FMA.
678 setOperationAction(ISD::FMA, MVT::f64, Expand);
679 setOperationAction(ISD::FMA, MVT::f32, Expand);
680
Dale Johannesen59a58732007-08-05 18:49:15 +0000681 // Long double always uses X87.
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000682 if (!TM.Options.UseSoftFloat) {
Craig Topperc9099502012-04-20 06:31:50 +0000683 addRegisterClass(MVT::f80, &X86::RFP80RegClass);
Owen Anderson825b72b2009-08-11 20:47:22 +0000684 setOperationAction(ISD::UNDEF, MVT::f80, Expand);
685 setOperationAction(ISD::FCOPYSIGN, MVT::f80, Expand);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000686 {
Benjamin Kramer98383962010-12-04 14:22:24 +0000687 APFloat TmpFlt = APFloat::getZero(APFloat::x87DoubleExtended);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000688 addLegalFPImmediate(TmpFlt); // FLD0
689 TmpFlt.changeSign();
690 addLegalFPImmediate(TmpFlt); // FLD0/FCHS
Benjamin Kramer98383962010-12-04 14:22:24 +0000691
692 bool ignored;
Evan Chengc7ce29b2009-02-13 22:36:38 +0000693 APFloat TmpFlt2(+1.0);
694 TmpFlt2.convert(APFloat::x87DoubleExtended, APFloat::rmNearestTiesToEven,
695 &ignored);
696 addLegalFPImmediate(TmpFlt2); // FLD1
697 TmpFlt2.changeSign();
698 addLegalFPImmediate(TmpFlt2); // FLD1/FCHS
699 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000700
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000701 if (!TM.Options.UnsafeFPMath) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000702 setOperationAction(ISD::FSIN , MVT::f80 , Expand);
703 setOperationAction(ISD::FCOS , MVT::f80 , Expand);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000704 }
Cameron Zwarich33390842011-07-08 21:39:21 +0000705
Owen Anderson4a4fdf32011-12-08 19:32:14 +0000706 setOperationAction(ISD::FFLOOR, MVT::f80, Expand);
707 setOperationAction(ISD::FCEIL, MVT::f80, Expand);
708 setOperationAction(ISD::FTRUNC, MVT::f80, Expand);
709 setOperationAction(ISD::FRINT, MVT::f80, Expand);
710 setOperationAction(ISD::FNEARBYINT, MVT::f80, Expand);
Cameron Zwarich33390842011-07-08 21:39:21 +0000711 setOperationAction(ISD::FMA, MVT::f80, Expand);
Dale Johannesen2f429012007-09-26 21:10:55 +0000712 }
Dale Johannesen59a58732007-08-05 18:49:15 +0000713
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000714 // Always use a library call for pow.
Owen Anderson825b72b2009-08-11 20:47:22 +0000715 setOperationAction(ISD::FPOW , MVT::f32 , Expand);
716 setOperationAction(ISD::FPOW , MVT::f64 , Expand);
717 setOperationAction(ISD::FPOW , MVT::f80 , Expand);
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000718
Owen Anderson825b72b2009-08-11 20:47:22 +0000719 setOperationAction(ISD::FLOG, MVT::f80, Expand);
720 setOperationAction(ISD::FLOG2, MVT::f80, Expand);
721 setOperationAction(ISD::FLOG10, MVT::f80, Expand);
722 setOperationAction(ISD::FEXP, MVT::f80, Expand);
723 setOperationAction(ISD::FEXP2, MVT::f80, Expand);
Dale Johannesen7794f2a2008-09-04 00:47:13 +0000724
Mon P Wangf007a8b2008-11-06 05:31:54 +0000725 // First set operation action for all vector types to either promote
Mon P Wang0c397192008-10-30 08:01:45 +0000726 // (for widening) or expand (for scalarization). Then we will selectively
727 // turn on ones that can be effectively codegen'd.
Craig Topper55de3392012-11-14 06:41:09 +0000728 for (int i = MVT::FIRST_VECTOR_VALUETYPE;
729 i <= MVT::LAST_VECTOR_VALUETYPE; ++i) {
Craig Topper49010472012-11-15 06:51:10 +0000730 MVT VT = (MVT::SimpleValueType)i;
Craig Topper55de3392012-11-14 06:41:09 +0000731 setOperationAction(ISD::ADD , VT, Expand);
732 setOperationAction(ISD::SUB , VT, Expand);
733 setOperationAction(ISD::FADD, VT, Expand);
734 setOperationAction(ISD::FNEG, VT, Expand);
735 setOperationAction(ISD::FSUB, VT, Expand);
736 setOperationAction(ISD::MUL , VT, Expand);
737 setOperationAction(ISD::FMUL, VT, Expand);
738 setOperationAction(ISD::SDIV, VT, Expand);
739 setOperationAction(ISD::UDIV, VT, Expand);
740 setOperationAction(ISD::FDIV, VT, Expand);
741 setOperationAction(ISD::SREM, VT, Expand);
742 setOperationAction(ISD::UREM, VT, Expand);
743 setOperationAction(ISD::LOAD, VT, Expand);
744 setOperationAction(ISD::VECTOR_SHUFFLE, VT, Expand);
745 setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT,Expand);
746 setOperationAction(ISD::INSERT_VECTOR_ELT, VT, Expand);
747 setOperationAction(ISD::EXTRACT_SUBVECTOR, VT,Expand);
748 setOperationAction(ISD::INSERT_SUBVECTOR, VT,Expand);
749 setOperationAction(ISD::FABS, VT, Expand);
750 setOperationAction(ISD::FSIN, VT, Expand);
751 setOperationAction(ISD::FCOS, VT, Expand);
752 setOperationAction(ISD::FREM, VT, Expand);
753 setOperationAction(ISD::FMA, VT, Expand);
754 setOperationAction(ISD::FPOWI, VT, Expand);
755 setOperationAction(ISD::FSQRT, VT, Expand);
756 setOperationAction(ISD::FCOPYSIGN, VT, Expand);
757 setOperationAction(ISD::FFLOOR, VT, Expand);
Craig Topper49010472012-11-15 06:51:10 +0000758 setOperationAction(ISD::FCEIL, VT, Expand);
759 setOperationAction(ISD::FTRUNC, VT, Expand);
760 setOperationAction(ISD::FRINT, VT, Expand);
761 setOperationAction(ISD::FNEARBYINT, VT, Expand);
Craig Topper55de3392012-11-14 06:41:09 +0000762 setOperationAction(ISD::SMUL_LOHI, VT, Expand);
763 setOperationAction(ISD::UMUL_LOHI, VT, Expand);
764 setOperationAction(ISD::SDIVREM, VT, Expand);
765 setOperationAction(ISD::UDIVREM, VT, Expand);
766 setOperationAction(ISD::FPOW, VT, Expand);
767 setOperationAction(ISD::CTPOP, VT, Expand);
768 setOperationAction(ISD::CTTZ, VT, Expand);
769 setOperationAction(ISD::CTTZ_ZERO_UNDEF, VT, Expand);
770 setOperationAction(ISD::CTLZ, VT, Expand);
771 setOperationAction(ISD::CTLZ_ZERO_UNDEF, VT, Expand);
772 setOperationAction(ISD::SHL, VT, Expand);
773 setOperationAction(ISD::SRA, VT, Expand);
774 setOperationAction(ISD::SRL, VT, Expand);
775 setOperationAction(ISD::ROTL, VT, Expand);
776 setOperationAction(ISD::ROTR, VT, Expand);
777 setOperationAction(ISD::BSWAP, VT, Expand);
778 setOperationAction(ISD::SETCC, VT, Expand);
779 setOperationAction(ISD::FLOG, VT, Expand);
780 setOperationAction(ISD::FLOG2, VT, Expand);
781 setOperationAction(ISD::FLOG10, VT, Expand);
782 setOperationAction(ISD::FEXP, VT, Expand);
783 setOperationAction(ISD::FEXP2, VT, Expand);
784 setOperationAction(ISD::FP_TO_UINT, VT, Expand);
785 setOperationAction(ISD::FP_TO_SINT, VT, Expand);
786 setOperationAction(ISD::UINT_TO_FP, VT, Expand);
787 setOperationAction(ISD::SINT_TO_FP, VT, Expand);
788 setOperationAction(ISD::SIGN_EXTEND_INREG, VT,Expand);
789 setOperationAction(ISD::TRUNCATE, VT, Expand);
790 setOperationAction(ISD::SIGN_EXTEND, VT, Expand);
791 setOperationAction(ISD::ZERO_EXTEND, VT, Expand);
792 setOperationAction(ISD::ANY_EXTEND, VT, Expand);
793 setOperationAction(ISD::VSELECT, VT, Expand);
Jakub Staszak6610b1d2012-04-29 20:52:53 +0000794 for (int InnerVT = MVT::FIRST_VECTOR_VALUETYPE;
795 InnerVT <= MVT::LAST_VECTOR_VALUETYPE; ++InnerVT)
Craig Topper55de3392012-11-14 06:41:09 +0000796 setTruncStoreAction(VT,
Dan Gohman2e141d72009-12-14 23:40:38 +0000797 (MVT::SimpleValueType)InnerVT, Expand);
Craig Topper55de3392012-11-14 06:41:09 +0000798 setLoadExtAction(ISD::SEXTLOAD, VT, Expand);
799 setLoadExtAction(ISD::ZEXTLOAD, VT, Expand);
800 setLoadExtAction(ISD::EXTLOAD, VT, Expand);
Evan Chengd30bf012006-03-01 01:11:20 +0000801 }
802
Evan Chengc7ce29b2009-02-13 22:36:38 +0000803 // FIXME: In order to prevent SSE instructions being expanded to MMX ones
804 // with -msoft-float, disable use of MMX as well.
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000805 if (!TM.Options.UseSoftFloat && Subtarget->hasMMX()) {
Craig Topperc9099502012-04-20 06:31:50 +0000806 addRegisterClass(MVT::x86mmx, &X86::VR64RegClass);
Dale Johannesen0488fb62010-09-30 23:57:10 +0000807 // No operations on x86mmx supported, everything uses intrinsics.
Evan Cheng470a6ad2006-02-22 02:26:30 +0000808 }
809
Dale Johannesen0488fb62010-09-30 23:57:10 +0000810 // MMX-sized vectors (other than x86mmx) are expected to be expanded
811 // into smaller operations.
812 setOperationAction(ISD::MULHS, MVT::v8i8, Expand);
813 setOperationAction(ISD::MULHS, MVT::v4i16, Expand);
814 setOperationAction(ISD::MULHS, MVT::v2i32, Expand);
815 setOperationAction(ISD::MULHS, MVT::v1i64, Expand);
816 setOperationAction(ISD::AND, MVT::v8i8, Expand);
817 setOperationAction(ISD::AND, MVT::v4i16, Expand);
818 setOperationAction(ISD::AND, MVT::v2i32, Expand);
819 setOperationAction(ISD::AND, MVT::v1i64, Expand);
820 setOperationAction(ISD::OR, MVT::v8i8, Expand);
821 setOperationAction(ISD::OR, MVT::v4i16, Expand);
822 setOperationAction(ISD::OR, MVT::v2i32, Expand);
823 setOperationAction(ISD::OR, MVT::v1i64, Expand);
824 setOperationAction(ISD::XOR, MVT::v8i8, Expand);
825 setOperationAction(ISD::XOR, MVT::v4i16, Expand);
826 setOperationAction(ISD::XOR, MVT::v2i32, Expand);
827 setOperationAction(ISD::XOR, MVT::v1i64, Expand);
828 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i8, Expand);
829 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4i16, Expand);
830 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v2i32, Expand);
831 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v1i64, Expand);
832 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v1i64, Expand);
833 setOperationAction(ISD::SELECT, MVT::v8i8, Expand);
834 setOperationAction(ISD::SELECT, MVT::v4i16, Expand);
835 setOperationAction(ISD::SELECT, MVT::v2i32, Expand);
836 setOperationAction(ISD::SELECT, MVT::v1i64, Expand);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000837 setOperationAction(ISD::BITCAST, MVT::v8i8, Expand);
838 setOperationAction(ISD::BITCAST, MVT::v4i16, Expand);
839 setOperationAction(ISD::BITCAST, MVT::v2i32, Expand);
840 setOperationAction(ISD::BITCAST, MVT::v1i64, Expand);
Dale Johannesen0488fb62010-09-30 23:57:10 +0000841
Craig Topper1accb7e2012-01-10 06:54:16 +0000842 if (!TM.Options.UseSoftFloat && Subtarget->hasSSE1()) {
Craig Topperc9099502012-04-20 06:31:50 +0000843 addRegisterClass(MVT::v4f32, &X86::VR128RegClass);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000844
Owen Anderson825b72b2009-08-11 20:47:22 +0000845 setOperationAction(ISD::FADD, MVT::v4f32, Legal);
846 setOperationAction(ISD::FSUB, MVT::v4f32, Legal);
847 setOperationAction(ISD::FMUL, MVT::v4f32, Legal);
848 setOperationAction(ISD::FDIV, MVT::v4f32, Legal);
849 setOperationAction(ISD::FSQRT, MVT::v4f32, Legal);
850 setOperationAction(ISD::FNEG, MVT::v4f32, Custom);
Craig Topper43620672012-09-08 07:31:51 +0000851 setOperationAction(ISD::FABS, MVT::v4f32, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000852 setOperationAction(ISD::LOAD, MVT::v4f32, Legal);
853 setOperationAction(ISD::BUILD_VECTOR, MVT::v4f32, Custom);
854 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4f32, Custom);
855 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
856 setOperationAction(ISD::SELECT, MVT::v4f32, Custom);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000857 }
858
Craig Topper1accb7e2012-01-10 06:54:16 +0000859 if (!TM.Options.UseSoftFloat && Subtarget->hasSSE2()) {
Craig Topperc9099502012-04-20 06:31:50 +0000860 addRegisterClass(MVT::v2f64, &X86::VR128RegClass);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000861
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000862 // FIXME: Unfortunately -soft-float and -no-implicit-float means XMM
863 // registers cannot be used even for integer operations.
Craig Topperc9099502012-04-20 06:31:50 +0000864 addRegisterClass(MVT::v16i8, &X86::VR128RegClass);
865 addRegisterClass(MVT::v8i16, &X86::VR128RegClass);
866 addRegisterClass(MVT::v4i32, &X86::VR128RegClass);
867 addRegisterClass(MVT::v2i64, &X86::VR128RegClass);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000868
Owen Anderson825b72b2009-08-11 20:47:22 +0000869 setOperationAction(ISD::ADD, MVT::v16i8, Legal);
870 setOperationAction(ISD::ADD, MVT::v8i16, Legal);
871 setOperationAction(ISD::ADD, MVT::v4i32, Legal);
872 setOperationAction(ISD::ADD, MVT::v2i64, Legal);
873 setOperationAction(ISD::MUL, MVT::v2i64, Custom);
874 setOperationAction(ISD::SUB, MVT::v16i8, Legal);
875 setOperationAction(ISD::SUB, MVT::v8i16, Legal);
876 setOperationAction(ISD::SUB, MVT::v4i32, Legal);
877 setOperationAction(ISD::SUB, MVT::v2i64, Legal);
878 setOperationAction(ISD::MUL, MVT::v8i16, Legal);
879 setOperationAction(ISD::FADD, MVT::v2f64, Legal);
880 setOperationAction(ISD::FSUB, MVT::v2f64, Legal);
881 setOperationAction(ISD::FMUL, MVT::v2f64, Legal);
882 setOperationAction(ISD::FDIV, MVT::v2f64, Legal);
883 setOperationAction(ISD::FSQRT, MVT::v2f64, Legal);
884 setOperationAction(ISD::FNEG, MVT::v2f64, Custom);
Craig Topper43620672012-09-08 07:31:51 +0000885 setOperationAction(ISD::FABS, MVT::v2f64, Custom);
Evan Cheng2c3ae372006-04-12 21:21:57 +0000886
Nadav Rotem354efd82011-09-18 14:57:03 +0000887 setOperationAction(ISD::SETCC, MVT::v2i64, Custom);
Duncan Sands28b77e92011-09-06 19:07:46 +0000888 setOperationAction(ISD::SETCC, MVT::v16i8, Custom);
889 setOperationAction(ISD::SETCC, MVT::v8i16, Custom);
890 setOperationAction(ISD::SETCC, MVT::v4i32, Custom);
Nate Begemanc2616e42008-05-12 20:34:32 +0000891
Owen Anderson825b72b2009-08-11 20:47:22 +0000892 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v16i8, Custom);
893 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i16, Custom);
894 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
895 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom);
896 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
Evan Chengf7c378e2006-04-10 07:23:14 +0000897
Evan Cheng2c3ae372006-04-12 21:21:57 +0000898 // Custom lower build_vector, vector_shuffle, and extract_vector_elt.
Jakub Staszak6610b1d2012-04-29 20:52:53 +0000899 for (int i = MVT::v16i8; i != MVT::v2i64; ++i) {
Craig Topper0d1f1762012-08-12 00:34:56 +0000900 MVT VT = (MVT::SimpleValueType)i;
Nate Begeman844e0f92007-12-11 01:41:33 +0000901 // Do not attempt to custom lower non-power-of-2 vectors
Duncan Sands83ec4b62008-06-06 12:08:01 +0000902 if (!isPowerOf2_32(VT.getVectorNumElements()))
Nate Begeman844e0f92007-12-11 01:41:33 +0000903 continue;
David Greene9b9838d2009-06-29 16:47:10 +0000904 // Do not attempt to custom lower non-128-bit vectors
905 if (!VT.is128BitVector())
906 continue;
Craig Topper0d1f1762012-08-12 00:34:56 +0000907 setOperationAction(ISD::BUILD_VECTOR, VT, Custom);
908 setOperationAction(ISD::VECTOR_SHUFFLE, VT, Custom);
909 setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT, Custom);
Evan Cheng2c3ae372006-04-12 21:21:57 +0000910 }
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000911
Owen Anderson825b72b2009-08-11 20:47:22 +0000912 setOperationAction(ISD::BUILD_VECTOR, MVT::v2f64, Custom);
913 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i64, Custom);
914 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2f64, Custom);
915 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i64, Custom);
916 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2f64, Custom);
917 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2f64, Custom);
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000918
Nate Begemancdd1eec2008-02-12 22:51:28 +0000919 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000920 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i64, Custom);
921 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Custom);
Nate Begemancdd1eec2008-02-12 22:51:28 +0000922 }
Evan Cheng2c3ae372006-04-12 21:21:57 +0000923
Anton Korobeynikov12c49af2006-11-21 00:01:06 +0000924 // Promote v16i8, v8i16, v4i32 load, select, and, or, xor to v2i64.
Craig Topper31a207a2012-05-04 06:39:13 +0000925 for (int i = MVT::v16i8; i != MVT::v2i64; ++i) {
Craig Topper0d1f1762012-08-12 00:34:56 +0000926 MVT VT = (MVT::SimpleValueType)i;
David Greene9b9838d2009-06-29 16:47:10 +0000927
928 // Do not attempt to promote non-128-bit vectors
Chris Lattner32b4b5a2010-07-05 05:53:14 +0000929 if (!VT.is128BitVector())
David Greene9b9838d2009-06-29 16:47:10 +0000930 continue;
Michael J. Spencerec38de22010-10-10 22:04:20 +0000931
Craig Topper0d1f1762012-08-12 00:34:56 +0000932 setOperationAction(ISD::AND, VT, Promote);
933 AddPromotedToType (ISD::AND, VT, MVT::v2i64);
934 setOperationAction(ISD::OR, VT, Promote);
935 AddPromotedToType (ISD::OR, VT, MVT::v2i64);
936 setOperationAction(ISD::XOR, VT, Promote);
937 AddPromotedToType (ISD::XOR, VT, MVT::v2i64);
938 setOperationAction(ISD::LOAD, VT, Promote);
939 AddPromotedToType (ISD::LOAD, VT, MVT::v2i64);
940 setOperationAction(ISD::SELECT, VT, Promote);
941 AddPromotedToType (ISD::SELECT, VT, MVT::v2i64);
Evan Chengf7c378e2006-04-10 07:23:14 +0000942 }
Evan Cheng2c3ae372006-04-12 21:21:57 +0000943
Owen Anderson825b72b2009-08-11 20:47:22 +0000944 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000945
Evan Cheng2c3ae372006-04-12 21:21:57 +0000946 // Custom lower v2i64 and v2f64 selects.
Owen Anderson825b72b2009-08-11 20:47:22 +0000947 setOperationAction(ISD::LOAD, MVT::v2f64, Legal);
948 setOperationAction(ISD::LOAD, MVT::v2i64, Legal);
949 setOperationAction(ISD::SELECT, MVT::v2f64, Custom);
950 setOperationAction(ISD::SELECT, MVT::v2i64, Custom);
Scott Michelfdc40a02009-02-17 22:15:04 +0000951
Owen Anderson825b72b2009-08-11 20:47:22 +0000952 setOperationAction(ISD::FP_TO_SINT, MVT::v4i32, Legal);
953 setOperationAction(ISD::SINT_TO_FP, MVT::v4i32, Legal);
Michael Liaob8150d82012-09-10 18:33:51 +0000954
Michael Liaoa7554632012-10-23 17:36:08 +0000955 setOperationAction(ISD::UINT_TO_FP, MVT::v4i8, Custom);
956 setOperationAction(ISD::UINT_TO_FP, MVT::v4i16, Custom);
Michael Liao991b6a22012-10-24 04:09:32 +0000957 // As there is no 64-bit GPR available, we need build a special custom
958 // sequence to convert from v2i32 to v2f32.
959 if (!Subtarget->is64Bit())
960 setOperationAction(ISD::UINT_TO_FP, MVT::v2f32, Custom);
Michael Liaoa7554632012-10-23 17:36:08 +0000961
Michael Liao9d796db2012-10-10 16:32:15 +0000962 setOperationAction(ISD::FP_EXTEND, MVT::v2f32, Custom);
Michael Liao44c2d612012-10-10 16:53:28 +0000963 setOperationAction(ISD::FP_ROUND, MVT::v2f32, Custom);
Michael Liao9d796db2012-10-10 16:32:15 +0000964
Michael Liaob8150d82012-09-10 18:33:51 +0000965 setLoadExtAction(ISD::EXTLOAD, MVT::v2f32, Legal);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000966 }
Evan Chengc7ce29b2009-02-13 22:36:38 +0000967
Craig Topperd0a31172012-01-10 06:37:29 +0000968 if (Subtarget->hasSSE41()) {
Benjamin Kramerb6533972011-12-09 15:44:03 +0000969 setOperationAction(ISD::FFLOOR, MVT::f32, Legal);
970 setOperationAction(ISD::FCEIL, MVT::f32, Legal);
971 setOperationAction(ISD::FTRUNC, MVT::f32, Legal);
972 setOperationAction(ISD::FRINT, MVT::f32, Legal);
973 setOperationAction(ISD::FNEARBYINT, MVT::f32, Legal);
974 setOperationAction(ISD::FFLOOR, MVT::f64, Legal);
975 setOperationAction(ISD::FCEIL, MVT::f64, Legal);
976 setOperationAction(ISD::FTRUNC, MVT::f64, Legal);
977 setOperationAction(ISD::FRINT, MVT::f64, Legal);
978 setOperationAction(ISD::FNEARBYINT, MVT::f64, Legal);
979
Craig Topper12fb5c62012-09-08 17:42:27 +0000980 setOperationAction(ISD::FFLOOR, MVT::v4f32, Legal);
Craig Topperd5775522012-11-16 06:37:56 +0000981 setOperationAction(ISD::FCEIL, MVT::v4f32, Legal);
982 setOperationAction(ISD::FTRUNC, MVT::v4f32, Legal);
983 setOperationAction(ISD::FRINT, MVT::v4f32, Legal);
984 setOperationAction(ISD::FNEARBYINT, MVT::v4f32, Legal);
Craig Topper12fb5c62012-09-08 17:42:27 +0000985 setOperationAction(ISD::FFLOOR, MVT::v2f64, Legal);
Craig Topperd5775522012-11-16 06:37:56 +0000986 setOperationAction(ISD::FCEIL, MVT::v2f64, Legal);
987 setOperationAction(ISD::FTRUNC, MVT::v2f64, Legal);
988 setOperationAction(ISD::FRINT, MVT::v2f64, Legal);
989 setOperationAction(ISD::FNEARBYINT, MVT::v2f64, Legal);
Craig Topper12fb5c62012-09-08 17:42:27 +0000990
Nate Begeman14d12ca2008-02-11 04:19:36 +0000991 // FIXME: Do we need to handle scalar-to-vector here?
Owen Anderson825b72b2009-08-11 20:47:22 +0000992 setOperationAction(ISD::MUL, MVT::v4i32, Legal);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000993
Nadav Rotemfbad25e2011-09-11 15:02:23 +0000994 setOperationAction(ISD::VSELECT, MVT::v2f64, Legal);
995 setOperationAction(ISD::VSELECT, MVT::v2i64, Legal);
996 setOperationAction(ISD::VSELECT, MVT::v16i8, Legal);
997 setOperationAction(ISD::VSELECT, MVT::v4i32, Legal);
998 setOperationAction(ISD::VSELECT, MVT::v4f32, Legal);
Nadav Rotemffe3e7d2011-09-08 08:11:19 +0000999
Nate Begeman14d12ca2008-02-11 04:19:36 +00001000 // i8 and i16 vectors are custom , because the source register and source
1001 // source memory operand types are not the same width. f32 vectors are
1002 // custom since the immediate controlling the insert encodes additional
1003 // information.
Owen Anderson825b72b2009-08-11 20:47:22 +00001004 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v16i8, Custom);
1005 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
1006 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom);
1007 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
Nate Begeman14d12ca2008-02-11 04:19:36 +00001008
Owen Anderson825b72b2009-08-11 20:47:22 +00001009 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v16i8, Custom);
1010 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v8i16, Custom);
1011 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4i32, Custom);
1012 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
Nate Begeman14d12ca2008-02-11 04:19:36 +00001013
Pete Coopera77214a2011-11-14 19:38:42 +00001014 // FIXME: these should be Legal but thats only for the case where
Chad Rosier30450e82011-12-22 22:35:21 +00001015 // the index is constant. For now custom expand to deal with that.
Nate Begeman14d12ca2008-02-11 04:19:36 +00001016 if (Subtarget->is64Bit()) {
Pete Coopera77214a2011-11-14 19:38:42 +00001017 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i64, Custom);
1018 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Custom);
Nate Begeman14d12ca2008-02-11 04:19:36 +00001019 }
1020 }
Evan Cheng470a6ad2006-02-22 02:26:30 +00001021
Craig Topper1accb7e2012-01-10 06:54:16 +00001022 if (Subtarget->hasSSE2()) {
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +00001023 setOperationAction(ISD::SRL, MVT::v8i16, Custom);
Craig Topper7be5dfd2011-11-12 09:58:49 +00001024 setOperationAction(ISD::SRL, MVT::v16i8, Custom);
Nadav Rotem43012222011-05-11 08:12:09 +00001025
Nadav Rotem43012222011-05-11 08:12:09 +00001026 setOperationAction(ISD::SHL, MVT::v8i16, Custom);
Craig Topper7be5dfd2011-11-12 09:58:49 +00001027 setOperationAction(ISD::SHL, MVT::v16i8, Custom);
Nadav Rotem43012222011-05-11 08:12:09 +00001028
Nadav Rotem43012222011-05-11 08:12:09 +00001029 setOperationAction(ISD::SRA, MVT::v8i16, Custom);
Eli Friedmanf6aa6b12011-11-01 21:18:39 +00001030 setOperationAction(ISD::SRA, MVT::v16i8, Custom);
Craig Topper7be5dfd2011-11-12 09:58:49 +00001031
1032 if (Subtarget->hasAVX2()) {
1033 setOperationAction(ISD::SRL, MVT::v2i64, Legal);
1034 setOperationAction(ISD::SRL, MVT::v4i32, Legal);
1035
1036 setOperationAction(ISD::SHL, MVT::v2i64, Legal);
1037 setOperationAction(ISD::SHL, MVT::v4i32, Legal);
1038
1039 setOperationAction(ISD::SRA, MVT::v4i32, Legal);
1040 } else {
1041 setOperationAction(ISD::SRL, MVT::v2i64, Custom);
1042 setOperationAction(ISD::SRL, MVT::v4i32, Custom);
1043
1044 setOperationAction(ISD::SHL, MVT::v2i64, Custom);
1045 setOperationAction(ISD::SHL, MVT::v4i32, Custom);
1046
1047 setOperationAction(ISD::SRA, MVT::v4i32, Custom);
1048 }
Nadav Rotem43012222011-05-11 08:12:09 +00001049 }
1050
Nick Lewycky8a8d4792011-12-02 22:16:29 +00001051 if (!TM.Options.UseSoftFloat && Subtarget->hasAVX()) {
Craig Topperc9099502012-04-20 06:31:50 +00001052 addRegisterClass(MVT::v32i8, &X86::VR256RegClass);
1053 addRegisterClass(MVT::v16i16, &X86::VR256RegClass);
1054 addRegisterClass(MVT::v8i32, &X86::VR256RegClass);
1055 addRegisterClass(MVT::v8f32, &X86::VR256RegClass);
1056 addRegisterClass(MVT::v4i64, &X86::VR256RegClass);
1057 addRegisterClass(MVT::v4f64, &X86::VR256RegClass);
David Greened94c1012009-06-29 22:50:51 +00001058
Owen Anderson825b72b2009-08-11 20:47:22 +00001059 setOperationAction(ISD::LOAD, MVT::v8f32, Legal);
Owen Anderson825b72b2009-08-11 20:47:22 +00001060 setOperationAction(ISD::LOAD, MVT::v4f64, Legal);
1061 setOperationAction(ISD::LOAD, MVT::v4i64, Legal);
David Greene54d8eba2011-01-27 22:38:56 +00001062
Owen Anderson825b72b2009-08-11 20:47:22 +00001063 setOperationAction(ISD::FADD, MVT::v8f32, Legal);
1064 setOperationAction(ISD::FSUB, MVT::v8f32, Legal);
1065 setOperationAction(ISD::FMUL, MVT::v8f32, Legal);
1066 setOperationAction(ISD::FDIV, MVT::v8f32, Legal);
1067 setOperationAction(ISD::FSQRT, MVT::v8f32, Legal);
Craig Topper12fb5c62012-09-08 17:42:27 +00001068 setOperationAction(ISD::FFLOOR, MVT::v8f32, Legal);
Craig Topperd5775522012-11-16 06:37:56 +00001069 setOperationAction(ISD::FCEIL, MVT::v8f32, Legal);
1070 setOperationAction(ISD::FTRUNC, MVT::v8f32, Legal);
1071 setOperationAction(ISD::FRINT, MVT::v8f32, Legal);
1072 setOperationAction(ISD::FNEARBYINT, MVT::v8f32, Legal);
Owen Anderson825b72b2009-08-11 20:47:22 +00001073 setOperationAction(ISD::FNEG, MVT::v8f32, Custom);
Craig Topper43620672012-09-08 07:31:51 +00001074 setOperationAction(ISD::FABS, MVT::v8f32, Custom);
David Greene9b9838d2009-06-29 16:47:10 +00001075
Owen Anderson825b72b2009-08-11 20:47:22 +00001076 setOperationAction(ISD::FADD, MVT::v4f64, Legal);
1077 setOperationAction(ISD::FSUB, MVT::v4f64, Legal);
1078 setOperationAction(ISD::FMUL, MVT::v4f64, Legal);
1079 setOperationAction(ISD::FDIV, MVT::v4f64, Legal);
1080 setOperationAction(ISD::FSQRT, MVT::v4f64, Legal);
Craig Topper12fb5c62012-09-08 17:42:27 +00001081 setOperationAction(ISD::FFLOOR, MVT::v4f64, Legal);
Craig Topperd5775522012-11-16 06:37:56 +00001082 setOperationAction(ISD::FCEIL, MVT::v4f64, Legal);
1083 setOperationAction(ISD::FTRUNC, MVT::v4f64, Legal);
1084 setOperationAction(ISD::FRINT, MVT::v4f64, Legal);
1085 setOperationAction(ISD::FNEARBYINT, MVT::v4f64, Legal);
Owen Anderson825b72b2009-08-11 20:47:22 +00001086 setOperationAction(ISD::FNEG, MVT::v4f64, Custom);
Craig Topper43620672012-09-08 07:31:51 +00001087 setOperationAction(ISD::FABS, MVT::v4f64, Custom);
David Greene9b9838d2009-06-29 16:47:10 +00001088
Michael Liaobedcbd42012-10-16 18:14:11 +00001089 setOperationAction(ISD::TRUNCATE, MVT::v8i16, Custom);
1090
1091 setOperationAction(ISD::FP_TO_SINT, MVT::v8i16, Custom);
1092
Bruno Cardoso Lopes2e64ae42011-07-28 01:26:39 +00001093 setOperationAction(ISD::FP_TO_SINT, MVT::v8i32, Legal);
1094 setOperationAction(ISD::SINT_TO_FP, MVT::v8i32, Legal);
Bruno Cardoso Lopes55244ce2011-08-01 21:54:09 +00001095 setOperationAction(ISD::FP_ROUND, MVT::v4f32, Legal);
Bruno Cardoso Lopes2e64ae42011-07-28 01:26:39 +00001096
Michael Liaoa7554632012-10-23 17:36:08 +00001097 setOperationAction(ISD::ZERO_EXTEND, MVT::v8i32, Custom);
1098 setOperationAction(ISD::UINT_TO_FP, MVT::v8i8, Custom);
1099 setOperationAction(ISD::UINT_TO_FP, MVT::v8i16, Custom);
1100
Michael Liaob8150d82012-09-10 18:33:51 +00001101 setLoadExtAction(ISD::EXTLOAD, MVT::v4f32, Legal);
1102
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +00001103 setOperationAction(ISD::SRL, MVT::v16i16, Custom);
1104 setOperationAction(ISD::SRL, MVT::v32i8, Custom);
1105
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +00001106 setOperationAction(ISD::SHL, MVT::v16i16, Custom);
1107 setOperationAction(ISD::SHL, MVT::v32i8, Custom);
1108
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +00001109 setOperationAction(ISD::SRA, MVT::v16i16, Custom);
Craig Topper7be5dfd2011-11-12 09:58:49 +00001110 setOperationAction(ISD::SRA, MVT::v32i8, Custom);
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +00001111
Duncan Sands28b77e92011-09-06 19:07:46 +00001112 setOperationAction(ISD::SETCC, MVT::v32i8, Custom);
1113 setOperationAction(ISD::SETCC, MVT::v16i16, Custom);
1114 setOperationAction(ISD::SETCC, MVT::v8i32, Custom);
1115 setOperationAction(ISD::SETCC, MVT::v4i64, Custom);
Bruno Cardoso Lopes0f0e0a02011-08-09 00:46:57 +00001116
Bruno Cardoso Lopesd40aa242011-08-09 23:27:13 +00001117 setOperationAction(ISD::SELECT, MVT::v4f64, Custom);
1118 setOperationAction(ISD::SELECT, MVT::v4i64, Custom);
1119 setOperationAction(ISD::SELECT, MVT::v8f32, Custom);
1120
Craig Topperaaa643c2011-11-09 07:28:55 +00001121 setOperationAction(ISD::VSELECT, MVT::v4f64, Legal);
1122 setOperationAction(ISD::VSELECT, MVT::v4i64, Legal);
1123 setOperationAction(ISD::VSELECT, MVT::v8i32, Legal);
1124 setOperationAction(ISD::VSELECT, MVT::v8f32, Legal);
Nadav Rotem8ffad562011-09-09 20:29:17 +00001125
Craig Topperbf404372012-08-31 15:40:30 +00001126 if (Subtarget->hasFMA() || Subtarget->hasFMA4()) {
Elena Demikhovsky1503aba2012-08-01 12:06:00 +00001127 setOperationAction(ISD::FMA, MVT::v8f32, Custom);
1128 setOperationAction(ISD::FMA, MVT::v4f64, Custom);
1129 setOperationAction(ISD::FMA, MVT::v4f32, Custom);
1130 setOperationAction(ISD::FMA, MVT::v2f64, Custom);
1131 setOperationAction(ISD::FMA, MVT::f32, Custom);
1132 setOperationAction(ISD::FMA, MVT::f64, Custom);
1133 }
Craig Topper880ef452012-08-11 22:34:26 +00001134
Craig Topperaaa643c2011-11-09 07:28:55 +00001135 if (Subtarget->hasAVX2()) {
1136 setOperationAction(ISD::ADD, MVT::v4i64, Legal);
1137 setOperationAction(ISD::ADD, MVT::v8i32, Legal);
1138 setOperationAction(ISD::ADD, MVT::v16i16, Legal);
1139 setOperationAction(ISD::ADD, MVT::v32i8, Legal);
Craig Topper13894fa2011-08-24 06:14:18 +00001140
Craig Topperaaa643c2011-11-09 07:28:55 +00001141 setOperationAction(ISD::SUB, MVT::v4i64, Legal);
1142 setOperationAction(ISD::SUB, MVT::v8i32, Legal);
1143 setOperationAction(ISD::SUB, MVT::v16i16, Legal);
1144 setOperationAction(ISD::SUB, MVT::v32i8, Legal);
Craig Topper13894fa2011-08-24 06:14:18 +00001145
Craig Topperaaa643c2011-11-09 07:28:55 +00001146 setOperationAction(ISD::MUL, MVT::v4i64, Custom);
1147 setOperationAction(ISD::MUL, MVT::v8i32, Legal);
1148 setOperationAction(ISD::MUL, MVT::v16i16, Legal);
Craig Topper46154eb2011-11-11 07:39:23 +00001149 // Don't lower v32i8 because there is no 128-bit byte mul
Nadav Rotembb539bf2011-11-09 13:21:28 +00001150
1151 setOperationAction(ISD::VSELECT, MVT::v32i8, Legal);
Craig Topper7be5dfd2011-11-12 09:58:49 +00001152
1153 setOperationAction(ISD::SRL, MVT::v4i64, Legal);
1154 setOperationAction(ISD::SRL, MVT::v8i32, Legal);
1155
1156 setOperationAction(ISD::SHL, MVT::v4i64, Legal);
1157 setOperationAction(ISD::SHL, MVT::v8i32, Legal);
1158
1159 setOperationAction(ISD::SRA, MVT::v8i32, Legal);
Craig Topperaaa643c2011-11-09 07:28:55 +00001160 } else {
1161 setOperationAction(ISD::ADD, MVT::v4i64, Custom);
1162 setOperationAction(ISD::ADD, MVT::v8i32, Custom);
1163 setOperationAction(ISD::ADD, MVT::v16i16, Custom);
1164 setOperationAction(ISD::ADD, MVT::v32i8, Custom);
1165
1166 setOperationAction(ISD::SUB, MVT::v4i64, Custom);
1167 setOperationAction(ISD::SUB, MVT::v8i32, Custom);
1168 setOperationAction(ISD::SUB, MVT::v16i16, Custom);
1169 setOperationAction(ISD::SUB, MVT::v32i8, Custom);
1170
1171 setOperationAction(ISD::MUL, MVT::v4i64, Custom);
1172 setOperationAction(ISD::MUL, MVT::v8i32, Custom);
1173 setOperationAction(ISD::MUL, MVT::v16i16, Custom);
1174 // Don't lower v32i8 because there is no 128-bit byte mul
Craig Topper7be5dfd2011-11-12 09:58:49 +00001175
1176 setOperationAction(ISD::SRL, MVT::v4i64, Custom);
1177 setOperationAction(ISD::SRL, MVT::v8i32, Custom);
1178
1179 setOperationAction(ISD::SHL, MVT::v4i64, Custom);
1180 setOperationAction(ISD::SHL, MVT::v8i32, Custom);
1181
1182 setOperationAction(ISD::SRA, MVT::v8i32, Custom);
Craig Topperaaa643c2011-11-09 07:28:55 +00001183 }
Craig Topper13894fa2011-08-24 06:14:18 +00001184
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001185 // Custom lower several nodes for 256-bit types.
Jakub Staszak6610b1d2012-04-29 20:52:53 +00001186 for (int i = MVT::FIRST_VECTOR_VALUETYPE;
1187 i <= MVT::LAST_VECTOR_VALUETYPE; ++i) {
Craig Topper0d1f1762012-08-12 00:34:56 +00001188 MVT VT = (MVT::SimpleValueType)i;
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001189
1190 // Extract subvector is special because the value type
1191 // (result) is 128-bit but the source is 256-bit wide.
1192 if (VT.is128BitVector())
Craig Topper0d1f1762012-08-12 00:34:56 +00001193 setOperationAction(ISD::EXTRACT_SUBVECTOR, VT, Custom);
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001194
1195 // Do not attempt to custom lower other non-256-bit vectors
1196 if (!VT.is256BitVector())
David Greene9b9838d2009-06-29 16:47:10 +00001197 continue;
David Greene54d8eba2011-01-27 22:38:56 +00001198
Craig Topper0d1f1762012-08-12 00:34:56 +00001199 setOperationAction(ISD::BUILD_VECTOR, VT, Custom);
1200 setOperationAction(ISD::VECTOR_SHUFFLE, VT, Custom);
1201 setOperationAction(ISD::INSERT_VECTOR_ELT, VT, Custom);
1202 setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT, Custom);
1203 setOperationAction(ISD::SCALAR_TO_VECTOR, VT, Custom);
1204 setOperationAction(ISD::INSERT_SUBVECTOR, VT, Custom);
1205 setOperationAction(ISD::CONCAT_VECTORS, VT, Custom);
David Greene9b9838d2009-06-29 16:47:10 +00001206 }
1207
David Greene54d8eba2011-01-27 22:38:56 +00001208 // Promote v32i8, v16i16, v8i32 select, and, or, xor to v4i64.
Jakub Staszak6610b1d2012-04-29 20:52:53 +00001209 for (int i = MVT::v32i8; i != MVT::v4i64; ++i) {
Craig Topper0d1f1762012-08-12 00:34:56 +00001210 MVT VT = (MVT::SimpleValueType)i;
David Greene54d8eba2011-01-27 22:38:56 +00001211
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001212 // Do not attempt to promote non-256-bit vectors
1213 if (!VT.is256BitVector())
David Greene54d8eba2011-01-27 22:38:56 +00001214 continue;
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001215
Craig Topper0d1f1762012-08-12 00:34:56 +00001216 setOperationAction(ISD::AND, VT, Promote);
1217 AddPromotedToType (ISD::AND, VT, MVT::v4i64);
1218 setOperationAction(ISD::OR, VT, Promote);
1219 AddPromotedToType (ISD::OR, VT, MVT::v4i64);
1220 setOperationAction(ISD::XOR, VT, Promote);
1221 AddPromotedToType (ISD::XOR, VT, MVT::v4i64);
1222 setOperationAction(ISD::LOAD, VT, Promote);
1223 AddPromotedToType (ISD::LOAD, VT, MVT::v4i64);
1224 setOperationAction(ISD::SELECT, VT, Promote);
1225 AddPromotedToType (ISD::SELECT, VT, MVT::v4i64);
David Greene54d8eba2011-01-27 22:38:56 +00001226 }
David Greene9b9838d2009-06-29 16:47:10 +00001227 }
1228
Nadav Rotemd0f3ef82011-07-14 11:11:14 +00001229 // SIGN_EXTEND_INREGs are evaluated by the extend type. Handle the expansion
1230 // of this type with custom code.
Jakub Staszak6610b1d2012-04-29 20:52:53 +00001231 for (int VT = MVT::FIRST_VECTOR_VALUETYPE;
1232 VT != MVT::LAST_VECTOR_VALUETYPE; VT++) {
Chad Rosier30450e82011-12-22 22:35:21 +00001233 setOperationAction(ISD::SIGN_EXTEND_INREG, (MVT::SimpleValueType)VT,
1234 Custom);
Nadav Rotemd0f3ef82011-07-14 11:11:14 +00001235 }
1236
Evan Cheng6be2c582006-04-05 23:38:46 +00001237 // We want to custom lower some of our intrinsics.
Owen Anderson825b72b2009-08-11 20:47:22 +00001238 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
Benjamin Kramerb9bee042012-07-12 09:31:43 +00001239 setOperationAction(ISD::INTRINSIC_W_CHAIN, MVT::Other, Custom);
Evan Cheng6be2c582006-04-05 23:38:46 +00001240
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00001241
Eli Friedman962f5492010-06-02 19:35:46 +00001242 // Only custom-lower 64-bit SADDO and friends on 64-bit because we don't
1243 // handle type legalization for these operations here.
Dan Gohman71c62a22010-06-02 19:13:40 +00001244 //
Eli Friedman962f5492010-06-02 19:35:46 +00001245 // FIXME: We really should do custom legalization for addition and
1246 // subtraction on x86-32 once PR3203 is fixed. We really can't do much better
1247 // than generic legalization for 64-bit multiplication-with-overflow, though.
Chris Lattnera34b3cf2010-12-19 20:03:11 +00001248 for (unsigned i = 0, e = 3+Subtarget->is64Bit(); i != e; ++i) {
1249 // Add/Sub/Mul with overflow operations are custom lowered.
1250 MVT VT = IntVTs[i];
1251 setOperationAction(ISD::SADDO, VT, Custom);
1252 setOperationAction(ISD::UADDO, VT, Custom);
1253 setOperationAction(ISD::SSUBO, VT, Custom);
1254 setOperationAction(ISD::USUBO, VT, Custom);
1255 setOperationAction(ISD::SMULO, VT, Custom);
1256 setOperationAction(ISD::UMULO, VT, Custom);
Eli Friedmana993f0a2010-06-02 00:27:18 +00001257 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00001258
Chris Lattnera34b3cf2010-12-19 20:03:11 +00001259 // There are no 8-bit 3-address imul/mul instructions
1260 setOperationAction(ISD::SMULO, MVT::i8, Expand);
1261 setOperationAction(ISD::UMULO, MVT::i8, Expand);
Bill Wendling41ea7e72008-11-24 19:21:46 +00001262
Evan Chengd54f2d52009-03-31 19:38:51 +00001263 if (!Subtarget->is64Bit()) {
1264 // These libcalls are not available in 32-bit.
1265 setLibcallName(RTLIB::SHL_I128, 0);
1266 setLibcallName(RTLIB::SRL_I128, 0);
1267 setLibcallName(RTLIB::SRA_I128, 0);
1268 }
1269
Evan Cheng206ee9d2006-07-07 08:33:52 +00001270 // We have target-specific dag combine patterns for the following nodes:
1271 setTargetDAGCombine(ISD::VECTOR_SHUFFLE);
Dan Gohman1bbf72b2010-03-15 23:23:03 +00001272 setTargetDAGCombine(ISD::EXTRACT_VECTOR_ELT);
Duncan Sands6bcd2192011-09-17 16:49:39 +00001273 setTargetDAGCombine(ISD::VSELECT);
Chris Lattner83e6c992006-10-04 06:57:07 +00001274 setTargetDAGCombine(ISD::SELECT);
Nate Begeman740ab032009-01-26 00:52:55 +00001275 setTargetDAGCombine(ISD::SHL);
1276 setTargetDAGCombine(ISD::SRA);
1277 setTargetDAGCombine(ISD::SRL);
Evan Cheng760d1942010-01-04 21:22:48 +00001278 setTargetDAGCombine(ISD::OR);
Nate Begemanb65c1752010-12-17 22:55:37 +00001279 setTargetDAGCombine(ISD::AND);
Benjamin Kramer7d6fe132010-12-21 21:41:44 +00001280 setTargetDAGCombine(ISD::ADD);
Duncan Sands17470be2011-09-22 20:15:48 +00001281 setTargetDAGCombine(ISD::FADD);
1282 setTargetDAGCombine(ISD::FSUB);
Elena Demikhovsky1503aba2012-08-01 12:06:00 +00001283 setTargetDAGCombine(ISD::FMA);
Benjamin Kramer7d6fe132010-12-21 21:41:44 +00001284 setTargetDAGCombine(ISD::SUB);
Nadav Rotem91e43fd2011-09-18 10:39:32 +00001285 setTargetDAGCombine(ISD::LOAD);
Chris Lattner149a4e52008-02-22 02:09:43 +00001286 setTargetDAGCombine(ISD::STORE);
Evan Cheng2e489c42009-12-16 00:53:11 +00001287 setTargetDAGCombine(ISD::ZERO_EXTEND);
Elena Demikhovsky1da58672012-04-22 09:39:03 +00001288 setTargetDAGCombine(ISD::ANY_EXTEND);
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +00001289 setTargetDAGCombine(ISD::SIGN_EXTEND);
Elena Demikhovsky3ae98152012-02-01 07:56:44 +00001290 setTargetDAGCombine(ISD::TRUNCATE);
Stuart Hastingsf99a4b82011-06-06 23:15:58 +00001291 setTargetDAGCombine(ISD::SINT_TO_FP);
Chad Rosiera73b6fc2012-04-27 22:33:25 +00001292 setTargetDAGCombine(ISD::SETCC);
Evan Cheng0b0cd912009-03-28 05:57:29 +00001293 if (Subtarget->is64Bit())
1294 setTargetDAGCombine(ISD::MUL);
Manman Ren92363622012-06-07 22:39:10 +00001295 setTargetDAGCombine(ISD::XOR);
Evan Cheng206ee9d2006-07-07 08:33:52 +00001296
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001297 computeRegisterProperties();
1298
Evan Cheng05219282011-01-06 06:52:41 +00001299 // On Darwin, -Os means optimize for size without hurting performance,
1300 // do not reduce the limit.
Dan Gohman87060f52008-06-30 21:00:56 +00001301 maxStoresPerMemset = 16; // For @llvm.memset -> sequence of stores
Evan Cheng05219282011-01-06 06:52:41 +00001302 maxStoresPerMemsetOptSize = Subtarget->isTargetDarwin() ? 16 : 8;
Evan Cheng255f20f2010-04-01 06:04:33 +00001303 maxStoresPerMemcpy = 8; // For @llvm.memcpy -> sequence of stores
Evan Cheng05219282011-01-06 06:52:41 +00001304 maxStoresPerMemcpyOptSize = Subtarget->isTargetDarwin() ? 8 : 4;
1305 maxStoresPerMemmove = 8; // For @llvm.memmove -> sequence of stores
1306 maxStoresPerMemmoveOptSize = Subtarget->isTargetDarwin() ? 8 : 4;
Jakob Stoklund Olesen8c741b82011-12-06 01:26:19 +00001307 setPrefLoopAlignment(4); // 2^4 bytes.
Evan Cheng6ebf7bc2009-05-13 21:42:09 +00001308 benefitFromCodePlacementOpt = true;
Eli Friedmanfc5d3052011-05-06 20:34:06 +00001309
Benjamin Krameraaf723d2012-05-05 12:49:14 +00001310 // Predictable cmov don't hurt on atom because it's in-order.
1311 predictableSelectIsExpensive = !Subtarget->isAtom();
1312
Jakob Stoklund Olesen8c741b82011-12-06 01:26:19 +00001313 setPrefFunctionAlignment(4); // 2^4 bytes.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001314}
1315
Scott Michel5b8f82e2008-03-10 15:42:14 +00001316
Duncan Sands28b77e92011-09-06 19:07:46 +00001317EVT X86TargetLowering::getSetCCResultType(EVT VT) const {
1318 if (!VT.isVector()) return MVT::i8;
1319 return VT.changeVectorElementTypeToInteger();
Scott Michel5b8f82e2008-03-10 15:42:14 +00001320}
1321
1322
Evan Cheng29286502008-01-23 23:17:41 +00001323/// getMaxByValAlign - Helper for getByValTypeAlignment to determine
1324/// the desired ByVal argument alignment.
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001325static void getMaxByValAlign(Type *Ty, unsigned &MaxAlign) {
Evan Cheng29286502008-01-23 23:17:41 +00001326 if (MaxAlign == 16)
1327 return;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001328 if (VectorType *VTy = dyn_cast<VectorType>(Ty)) {
Evan Cheng29286502008-01-23 23:17:41 +00001329 if (VTy->getBitWidth() == 128)
1330 MaxAlign = 16;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001331 } else if (ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
Evan Cheng29286502008-01-23 23:17:41 +00001332 unsigned EltAlign = 0;
1333 getMaxByValAlign(ATy->getElementType(), EltAlign);
1334 if (EltAlign > MaxAlign)
1335 MaxAlign = EltAlign;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001336 } else if (StructType *STy = dyn_cast<StructType>(Ty)) {
Evan Cheng29286502008-01-23 23:17:41 +00001337 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
1338 unsigned EltAlign = 0;
1339 getMaxByValAlign(STy->getElementType(i), EltAlign);
1340 if (EltAlign > MaxAlign)
1341 MaxAlign = EltAlign;
1342 if (MaxAlign == 16)
1343 break;
1344 }
1345 }
Evan Cheng29286502008-01-23 23:17:41 +00001346}
1347
1348/// getByValTypeAlignment - Return the desired alignment for ByVal aggregate
1349/// function arguments in the caller parameter area. For X86, aggregates
Dale Johannesen0c191872008-02-08 19:48:20 +00001350/// that contain SSE vectors are placed at 16-byte boundaries while the rest
1351/// are at 4-byte boundaries.
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001352unsigned X86TargetLowering::getByValTypeAlignment(Type *Ty) const {
Evan Cheng1887c1c2008-08-21 21:00:15 +00001353 if (Subtarget->is64Bit()) {
1354 // Max of 8 and alignment of type.
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00001355 unsigned TyAlign = TD->getABITypeAlignment(Ty);
Evan Cheng1887c1c2008-08-21 21:00:15 +00001356 if (TyAlign > 8)
1357 return TyAlign;
1358 return 8;
1359 }
1360
Evan Cheng29286502008-01-23 23:17:41 +00001361 unsigned Align = 4;
Craig Topper1accb7e2012-01-10 06:54:16 +00001362 if (Subtarget->hasSSE1())
Dale Johannesen0c191872008-02-08 19:48:20 +00001363 getMaxByValAlign(Ty, Align);
Evan Cheng29286502008-01-23 23:17:41 +00001364 return Align;
1365}
Chris Lattner2b02a442007-02-25 08:29:00 +00001366
Evan Chengf0df0312008-05-15 08:39:06 +00001367/// getOptimalMemOpType - Returns the target specific optimal type for load
Evan Chengc3b0c342010-04-08 07:37:57 +00001368/// and store operations as a result of memset, memcpy, and memmove
1369/// lowering. If DstAlign is zero that means it's safe to destination
1370/// alignment can satisfy any constraint. Similarly if SrcAlign is zero it
1371/// means there isn't a need to check it against alignment requirement,
1372/// probably because the source does not need to be loaded. If
Lang Hames15701f82011-10-26 23:50:43 +00001373/// 'IsZeroVal' is true, that means it's safe to return a
Evan Chengc3b0c342010-04-08 07:37:57 +00001374/// non-scalar-integer type, e.g. empty string source, constant, or loaded
1375/// from memory. 'MemcpyStrSrc' indicates whether the memcpy source is
1376/// constant so it does not need to be loaded.
Dan Gohman37f32ee2010-04-16 20:11:05 +00001377/// It returns EVT::Other if the type should be determined using generic
1378/// target-independent logic.
Owen Andersone50ed302009-08-10 22:56:29 +00001379EVT
Evan Cheng255f20f2010-04-01 06:04:33 +00001380X86TargetLowering::getOptimalMemOpType(uint64_t Size,
1381 unsigned DstAlign, unsigned SrcAlign,
Lang Hames15701f82011-10-26 23:50:43 +00001382 bool IsZeroVal,
Evan Chengc3b0c342010-04-08 07:37:57 +00001383 bool MemcpyStrSrc,
Dan Gohman37f32ee2010-04-16 20:11:05 +00001384 MachineFunction &MF) const {
Dan Gohman37f32ee2010-04-16 20:11:05 +00001385 const Function *F = MF.getFunction();
Lang Hames15701f82011-10-26 23:50:43 +00001386 if (IsZeroVal &&
Bill Wendling67658342012-10-09 07:45:08 +00001387 !F->getFnAttributes().hasAttribute(Attributes::NoImplicitFloat)) {
Evan Cheng255f20f2010-04-01 06:04:33 +00001388 if (Size >= 16 &&
Evan Chenga5e13622011-01-07 19:35:30 +00001389 (Subtarget->isUnalignedMemAccessFast() ||
1390 ((DstAlign == 0 || DstAlign >= 16) &&
Benjamin Kramer2dbe9292012-11-14 20:08:40 +00001391 (SrcAlign == 0 || SrcAlign >= 16)))) {
1392 if (Size >= 32) {
Craig Topper562659f2012-01-13 08:32:21 +00001393 if (Subtarget->hasAVX2())
1394 return MVT::v8i32;
1395 if (Subtarget->hasAVX())
1396 return MVT::v8f32;
1397 }
Craig Topper1accb7e2012-01-10 06:54:16 +00001398 if (Subtarget->hasSSE2())
Evan Cheng255f20f2010-04-01 06:04:33 +00001399 return MVT::v4i32;
Craig Topper1accb7e2012-01-10 06:54:16 +00001400 if (Subtarget->hasSSE1())
Evan Cheng255f20f2010-04-01 06:04:33 +00001401 return MVT::v4f32;
Evan Chengc3b0c342010-04-08 07:37:57 +00001402 } else if (!MemcpyStrSrc && Size >= 8 &&
Evan Cheng3ea97552010-04-01 20:27:45 +00001403 !Subtarget->is64Bit() &&
Craig Topper1accb7e2012-01-10 06:54:16 +00001404 Subtarget->hasSSE2()) {
Evan Chengc3b0c342010-04-08 07:37:57 +00001405 // Do not use f64 to lower memcpy if source is string constant. It's
1406 // better to use i32 to avoid the loads.
Evan Cheng255f20f2010-04-01 06:04:33 +00001407 return MVT::f64;
Evan Chengc3b0c342010-04-08 07:37:57 +00001408 }
Chris Lattner4002a1b2008-10-28 05:49:35 +00001409 }
Evan Chengf0df0312008-05-15 08:39:06 +00001410 if (Subtarget->is64Bit() && Size >= 8)
Owen Anderson825b72b2009-08-11 20:47:22 +00001411 return MVT::i64;
1412 return MVT::i32;
Evan Chengf0df0312008-05-15 08:39:06 +00001413}
1414
Chris Lattner5e1df8d2010-01-25 23:38:14 +00001415/// getJumpTableEncoding - Return the entry encoding for a jump table in the
1416/// current function. The returned value is a member of the
1417/// MachineJumpTableInfo::JTEntryKind enum.
1418unsigned X86TargetLowering::getJumpTableEncoding() const {
1419 // In GOT pic mode, each entry in the jump table is emitted as a @GOTOFF
1420 // symbol.
1421 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1422 Subtarget->isPICStyleGOT())
Chris Lattnerc64daab2010-01-26 05:02:42 +00001423 return MachineJumpTableInfo::EK_Custom32;
Michael J. Spencerec38de22010-10-10 22:04:20 +00001424
Chris Lattner5e1df8d2010-01-25 23:38:14 +00001425 // Otherwise, use the normal jump table encoding heuristics.
1426 return TargetLowering::getJumpTableEncoding();
1427}
1428
Chris Lattnerc64daab2010-01-26 05:02:42 +00001429const MCExpr *
1430X86TargetLowering::LowerCustomJumpTableEntry(const MachineJumpTableInfo *MJTI,
1431 const MachineBasicBlock *MBB,
1432 unsigned uid,MCContext &Ctx) const{
1433 assert(getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1434 Subtarget->isPICStyleGOT());
1435 // In 32-bit ELF systems, our jump table entries are formed with @GOTOFF
1436 // entries.
Daniel Dunbar4e815f82010-03-15 23:51:06 +00001437 return MCSymbolRefExpr::Create(MBB->getSymbol(),
1438 MCSymbolRefExpr::VK_GOTOFF, Ctx);
Chris Lattnerc64daab2010-01-26 05:02:42 +00001439}
1440
Evan Chengcc415862007-11-09 01:32:10 +00001441/// getPICJumpTableRelocaBase - Returns relocation base for the given PIC
1442/// jumptable.
Dan Gohman475871a2008-07-27 21:46:04 +00001443SDValue X86TargetLowering::getPICJumpTableRelocBase(SDValue Table,
Chris Lattner589c6f62010-01-26 06:28:43 +00001444 SelectionDAG &DAG) const {
Chris Lattnere4df7562009-07-09 03:15:51 +00001445 if (!Subtarget->is64Bit())
Dale Johannesenb300d2a2009-02-07 00:55:49 +00001446 // This doesn't have DebugLoc associated with it, but is not really the
1447 // same as a Register.
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00001448 return DAG.getNode(X86ISD::GlobalBaseReg, DebugLoc(), getPointerTy());
Evan Chengcc415862007-11-09 01:32:10 +00001449 return Table;
1450}
1451
Chris Lattner589c6f62010-01-26 06:28:43 +00001452/// getPICJumpTableRelocBaseExpr - This returns the relocation base for the
1453/// given PIC jumptable, the same as getPICJumpTableRelocBase, but as an
1454/// MCExpr.
1455const MCExpr *X86TargetLowering::
1456getPICJumpTableRelocBaseExpr(const MachineFunction *MF, unsigned JTI,
1457 MCContext &Ctx) const {
1458 // X86-64 uses RIP relative addressing based on the jump table label.
1459 if (Subtarget->isPICStyleRIPRel())
1460 return TargetLowering::getPICJumpTableRelocBaseExpr(MF, JTI, Ctx);
1461
1462 // Otherwise, the reference is relative to the PIC base.
Chris Lattner142b5312010-11-14 22:48:15 +00001463 return MCSymbolRefExpr::Create(MF->getPICBaseSymbol(), Ctx);
Chris Lattner589c6f62010-01-26 06:28:43 +00001464}
1465
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00001466// FIXME: Why this routine is here? Move to RegInfo!
Evan Chengdee81012010-07-26 21:50:05 +00001467std::pair<const TargetRegisterClass*, uint8_t>
1468X86TargetLowering::findRepresentativeClass(EVT VT) const{
1469 const TargetRegisterClass *RRC = 0;
1470 uint8_t Cost = 1;
1471 switch (VT.getSimpleVT().SimpleTy) {
1472 default:
1473 return TargetLowering::findRepresentativeClass(VT);
1474 case MVT::i8: case MVT::i16: case MVT::i32: case MVT::i64:
Craig Topperc9099502012-04-20 06:31:50 +00001475 RRC = Subtarget->is64Bit() ?
1476 (const TargetRegisterClass*)&X86::GR64RegClass :
1477 (const TargetRegisterClass*)&X86::GR32RegClass;
Evan Chengdee81012010-07-26 21:50:05 +00001478 break;
Dale Johannesen0488fb62010-09-30 23:57:10 +00001479 case MVT::x86mmx:
Craig Topperc9099502012-04-20 06:31:50 +00001480 RRC = &X86::VR64RegClass;
Evan Chengdee81012010-07-26 21:50:05 +00001481 break;
1482 case MVT::f32: case MVT::f64:
1483 case MVT::v16i8: case MVT::v8i16: case MVT::v4i32: case MVT::v2i64:
1484 case MVT::v4f32: case MVT::v2f64:
1485 case MVT::v32i8: case MVT::v8i32: case MVT::v4i64: case MVT::v8f32:
1486 case MVT::v4f64:
Craig Topperc9099502012-04-20 06:31:50 +00001487 RRC = &X86::VR128RegClass;
Evan Chengdee81012010-07-26 21:50:05 +00001488 break;
1489 }
1490 return std::make_pair(RRC, Cost);
1491}
1492
Eric Christopherf7a0c7b2010-07-06 05:18:56 +00001493bool X86TargetLowering::getStackCookieLocation(unsigned &AddressSpace,
1494 unsigned &Offset) const {
1495 if (!Subtarget->isTargetLinux())
1496 return false;
1497
1498 if (Subtarget->is64Bit()) {
1499 // %fs:0x28, unless we're using a Kernel code model, in which case it's %gs:
1500 Offset = 0x28;
1501 if (getTargetMachine().getCodeModel() == CodeModel::Kernel)
1502 AddressSpace = 256;
1503 else
1504 AddressSpace = 257;
1505 } else {
1506 // %gs:0x14 on i386
1507 Offset = 0x14;
1508 AddressSpace = 256;
1509 }
1510 return true;
1511}
1512
1513
Chris Lattner2b02a442007-02-25 08:29:00 +00001514//===----------------------------------------------------------------------===//
1515// Return Value Calling Convention Implementation
1516//===----------------------------------------------------------------------===//
1517
Chris Lattner59ed56b2007-02-28 04:55:35 +00001518#include "X86GenCallingConv.inc"
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001519
Michael J. Spencerec38de22010-10-10 22:04:20 +00001520bool
Eric Christopher471e4222011-06-08 23:55:35 +00001521X86TargetLowering::CanLowerReturn(CallingConv::ID CallConv,
Craig Topper0fbf3642012-04-23 03:28:34 +00001522 MachineFunction &MF, bool isVarArg,
Dan Gohman84023e02010-07-10 09:00:22 +00001523 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9af33c2010-07-06 22:19:37 +00001524 LLVMContext &Context) const {
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +00001525 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00001526 CCState CCInfo(CallConv, isVarArg, MF, getTargetMachine(),
Dan Gohmanc9af33c2010-07-06 22:19:37 +00001527 RVLocs, Context);
Dan Gohman84023e02010-07-10 09:00:22 +00001528 return CCInfo.CheckReturn(Outs, RetCC_X86);
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +00001529}
1530
Dan Gohman98ca4f22009-08-05 01:29:28 +00001531SDValue
1532X86TargetLowering::LowerReturn(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001533 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001534 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001535 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohmand858e902010-04-17 15:26:15 +00001536 DebugLoc dl, SelectionDAG &DAG) const {
Dan Gohman1e93df62010-04-17 14:41:14 +00001537 MachineFunction &MF = DAG.getMachineFunction();
1538 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
Scott Michelfdc40a02009-02-17 22:15:04 +00001539
Chris Lattner9774c912007-02-27 05:28:59 +00001540 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00001541 CCState CCInfo(CallConv, isVarArg, MF, getTargetMachine(),
Dan Gohman98ca4f22009-08-05 01:29:28 +00001542 RVLocs, *DAG.getContext());
1543 CCInfo.AnalyzeReturn(Outs, RetCC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001544
Evan Chengdcea1632010-02-04 02:40:39 +00001545 // Add the regs to the liveout set for the function.
1546 MachineRegisterInfo &MRI = DAG.getMachineFunction().getRegInfo();
1547 for (unsigned i = 0; i != RVLocs.size(); ++i)
1548 if (RVLocs[i].isRegLoc() && !MRI.isLiveOut(RVLocs[i].getLocReg()))
1549 MRI.addLiveOut(RVLocs[i].getLocReg());
Scott Michelfdc40a02009-02-17 22:15:04 +00001550
Dan Gohman475871a2008-07-27 21:46:04 +00001551 SDValue Flag;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001552
Dan Gohman475871a2008-07-27 21:46:04 +00001553 SmallVector<SDValue, 6> RetOps;
Chris Lattner447ff682008-03-11 03:23:40 +00001554 RetOps.push_back(Chain); // Operand #0 = Chain (updated below)
1555 // Operand #1 = Bytes To Pop
Dan Gohman1e93df62010-04-17 14:41:14 +00001556 RetOps.push_back(DAG.getTargetConstant(FuncInfo->getBytesToPopOnReturn(),
1557 MVT::i16));
Scott Michelfdc40a02009-02-17 22:15:04 +00001558
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001559 // Copy the result values into the output registers.
Chris Lattner8e6da152008-03-10 21:08:41 +00001560 for (unsigned i = 0; i != RVLocs.size(); ++i) {
1561 CCValAssign &VA = RVLocs[i];
1562 assert(VA.isRegLoc() && "Can only return in registers!");
Dan Gohmanc9403652010-07-07 15:54:55 +00001563 SDValue ValToCopy = OutVals[i];
Dale Johannesenc76d23f2010-07-23 00:30:35 +00001564 EVT ValVT = ValToCopy.getValueType();
1565
Jakob Stoklund Olesenee66b412012-05-31 17:28:20 +00001566 // Promote values to the appropriate types
1567 if (VA.getLocInfo() == CCValAssign::SExt)
1568 ValToCopy = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), ValToCopy);
1569 else if (VA.getLocInfo() == CCValAssign::ZExt)
1570 ValToCopy = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), ValToCopy);
1571 else if (VA.getLocInfo() == CCValAssign::AExt)
1572 ValToCopy = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), ValToCopy);
1573 else if (VA.getLocInfo() == CCValAssign::BCvt)
1574 ValToCopy = DAG.getNode(ISD::BITCAST, dl, VA.getLocVT(), ValToCopy);
1575
Dale Johannesenc4510512010-09-24 19:05:48 +00001576 // If this is x86-64, and we disabled SSE, we can't return FP values,
1577 // or SSE or MMX vectors.
1578 if ((ValVT == MVT::f32 || ValVT == MVT::f64 ||
1579 VA.getLocReg() == X86::XMM0 || VA.getLocReg() == X86::XMM1) &&
Craig Topper1accb7e2012-01-10 06:54:16 +00001580 (Subtarget->is64Bit() && !Subtarget->hasSSE1())) {
Dale Johannesenc76d23f2010-07-23 00:30:35 +00001581 report_fatal_error("SSE register return with SSE disabled");
1582 }
1583 // Likewise we can't return F64 values with SSE1 only. gcc does so, but
1584 // llvm-gcc has never done it right and no one has noticed, so this
1585 // should be OK for now.
1586 if (ValVT == MVT::f64 &&
Craig Topper1accb7e2012-01-10 06:54:16 +00001587 (Subtarget->is64Bit() && !Subtarget->hasSSE2()))
Dale Johannesenc76d23f2010-07-23 00:30:35 +00001588 report_fatal_error("SSE2 register return with SSE2 disabled");
Scott Michelfdc40a02009-02-17 22:15:04 +00001589
Chris Lattner447ff682008-03-11 03:23:40 +00001590 // Returns in ST0/ST1 are handled specially: these are pushed as operands to
1591 // the RET instruction and handled by the FP Stackifier.
Dan Gohman37eed792009-02-04 17:28:58 +00001592 if (VA.getLocReg() == X86::ST0 ||
1593 VA.getLocReg() == X86::ST1) {
Chris Lattner447ff682008-03-11 03:23:40 +00001594 // If this is a copy from an xmm register to ST(0), use an FPExtend to
1595 // change the value to the FP stack register class.
Dan Gohman37eed792009-02-04 17:28:58 +00001596 if (isScalarFPTypeInSSEReg(VA.getValVT()))
Owen Anderson825b72b2009-08-11 20:47:22 +00001597 ValToCopy = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f80, ValToCopy);
Chris Lattner447ff682008-03-11 03:23:40 +00001598 RetOps.push_back(ValToCopy);
1599 // Don't emit a copytoreg.
1600 continue;
1601 }
Dale Johannesena68f9012008-06-24 22:01:44 +00001602
Evan Cheng242b38b2009-02-23 09:03:22 +00001603 // 64-bit vector (MMX) values are returned in XMM0 / XMM1 except for v1i64
1604 // which is returned in RAX / RDX.
Evan Cheng6140a8b2009-02-22 08:05:12 +00001605 if (Subtarget->is64Bit()) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00001606 if (ValVT == MVT::x86mmx) {
Chris Lattner97a2a562010-08-26 05:24:29 +00001607 if (VA.getLocReg() == X86::XMM0 || VA.getLocReg() == X86::XMM1) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001608 ValToCopy = DAG.getNode(ISD::BITCAST, dl, MVT::i64, ValToCopy);
Eric Christopher90eb4022010-07-22 00:26:08 +00001609 ValToCopy = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i64,
1610 ValToCopy);
Chris Lattner97a2a562010-08-26 05:24:29 +00001611 // If we don't have SSE2 available, convert to v4f32 so the generated
1612 // register is legal.
Craig Topper1accb7e2012-01-10 06:54:16 +00001613 if (!Subtarget->hasSSE2())
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001614 ValToCopy = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32,ValToCopy);
Chris Lattner97a2a562010-08-26 05:24:29 +00001615 }
Evan Cheng242b38b2009-02-23 09:03:22 +00001616 }
Evan Cheng6140a8b2009-02-22 08:05:12 +00001617 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00001618
Dale Johannesendd64c412009-02-04 00:33:20 +00001619 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), ValToCopy, Flag);
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001620 Flag = Chain.getValue(1);
1621 }
Dan Gohman61a92132008-04-21 23:59:07 +00001622
1623 // The x86-64 ABI for returning structs by value requires that we copy
1624 // the sret argument into %rax for the return. We saved the argument into
1625 // a virtual register in the entry block, so now we copy the value out
1626 // and into %rax.
1627 if (Subtarget->is64Bit() &&
1628 DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
1629 MachineFunction &MF = DAG.getMachineFunction();
1630 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1631 unsigned Reg = FuncInfo->getSRetReturnReg();
Michael J. Spencerec38de22010-10-10 22:04:20 +00001632 assert(Reg &&
Zhongxing Xuc2798a12010-05-26 08:10:02 +00001633 "SRetReturnReg should have been set in LowerFormalArguments().");
Dale Johannesendd64c412009-02-04 00:33:20 +00001634 SDValue Val = DAG.getCopyFromReg(Chain, dl, Reg, getPointerTy());
Dan Gohman61a92132008-04-21 23:59:07 +00001635
Dale Johannesendd64c412009-02-04 00:33:20 +00001636 Chain = DAG.getCopyToReg(Chain, dl, X86::RAX, Val, Flag);
Dan Gohman61a92132008-04-21 23:59:07 +00001637 Flag = Chain.getValue(1);
Dan Gohman00326812009-10-12 16:36:12 +00001638
1639 // RAX now acts like a return value.
Evan Chengdcea1632010-02-04 02:40:39 +00001640 MRI.addLiveOut(X86::RAX);
Dan Gohman61a92132008-04-21 23:59:07 +00001641 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001642
Chris Lattner447ff682008-03-11 03:23:40 +00001643 RetOps[0] = Chain; // Update chain.
1644
1645 // Add the flag if we have it.
Gabor Greifba36cb52008-08-28 21:40:38 +00001646 if (Flag.getNode())
Chris Lattner447ff682008-03-11 03:23:40 +00001647 RetOps.push_back(Flag);
Scott Michelfdc40a02009-02-17 22:15:04 +00001648
1649 return DAG.getNode(X86ISD::RET_FLAG, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001650 MVT::Other, &RetOps[0], RetOps.size());
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001651}
1652
Evan Chengbf010eb2012-04-10 01:51:00 +00001653bool X86TargetLowering::isUsedByReturnOnly(SDNode *N, SDValue &Chain) const {
Evan Cheng3d2125c2010-11-30 23:55:39 +00001654 if (N->getNumValues() != 1)
1655 return false;
1656 if (!N->hasNUsesOfValue(1, 0))
1657 return false;
1658
Evan Chengbf010eb2012-04-10 01:51:00 +00001659 SDValue TCChain = Chain;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001660 SDNode *Copy = *N->use_begin();
Chad Rosierc8d7eea2012-03-05 19:27:12 +00001661 if (Copy->getOpcode() == ISD::CopyToReg) {
1662 // If the copy has a glue operand, we conservatively assume it isn't safe to
1663 // perform a tail call.
1664 if (Copy->getOperand(Copy->getNumOperands()-1).getValueType() == MVT::Glue)
1665 return false;
Evan Chengbf010eb2012-04-10 01:51:00 +00001666 TCChain = Copy->getOperand(0);
Chad Rosierc8d7eea2012-03-05 19:27:12 +00001667 } else if (Copy->getOpcode() != ISD::FP_EXTEND)
Chad Rosier74bab7f2012-03-02 02:50:46 +00001668 return false;
1669
Evan Cheng1bf891a2010-12-01 22:59:46 +00001670 bool HasRet = false;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001671 for (SDNode::use_iterator UI = Copy->use_begin(), UE = Copy->use_end();
Evan Cheng1bf891a2010-12-01 22:59:46 +00001672 UI != UE; ++UI) {
Evan Cheng3d2125c2010-11-30 23:55:39 +00001673 if (UI->getOpcode() != X86ISD::RET_FLAG)
1674 return false;
Evan Cheng1bf891a2010-12-01 22:59:46 +00001675 HasRet = true;
1676 }
Evan Cheng3d2125c2010-11-30 23:55:39 +00001677
Evan Chengbf010eb2012-04-10 01:51:00 +00001678 if (!HasRet)
1679 return false;
1680
1681 Chain = TCChain;
1682 return true;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001683}
1684
Cameron Zwarich7bbf0ee2011-03-17 14:53:37 +00001685EVT
1686X86TargetLowering::getTypeForExtArgOrReturn(LLVMContext &Context, EVT VT,
Cameron Zwarich44579682011-03-17 14:21:56 +00001687 ISD::NodeType ExtendKind) const {
Cameron Zwarich7bbf0ee2011-03-17 14:53:37 +00001688 MVT ReturnMVT;
Cameron Zwarichebe81732011-03-16 22:20:18 +00001689 // TODO: Is this also valid on 32-bit?
1690 if (Subtarget->is64Bit() && VT == MVT::i1 && ExtendKind == ISD::ZERO_EXTEND)
Cameron Zwarich7bbf0ee2011-03-17 14:53:37 +00001691 ReturnMVT = MVT::i8;
1692 else
1693 ReturnMVT = MVT::i32;
1694
1695 EVT MinVT = getRegisterType(Context, ReturnMVT);
1696 return VT.bitsLT(MinVT) ? MinVT : VT;
Cameron Zwarichebe81732011-03-16 22:20:18 +00001697}
1698
Dan Gohman98ca4f22009-08-05 01:29:28 +00001699/// LowerCallResult - Lower the result values of a call into the
1700/// appropriate copies out of appropriate physical registers.
1701///
1702SDValue
1703X86TargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001704 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001705 const SmallVectorImpl<ISD::InputArg> &Ins,
1706 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001707 SmallVectorImpl<SDValue> &InVals) const {
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001708
Chris Lattnere32bbf62007-02-28 07:09:55 +00001709 // Assign locations to each value returned by this call.
Chris Lattner9774c912007-02-27 05:28:59 +00001710 SmallVector<CCValAssign, 16> RVLocs;
Torok Edwin3f142c32009-02-01 18:15:56 +00001711 bool Is64Bit = Subtarget->is64Bit();
Eric Christopher471e4222011-06-08 23:55:35 +00001712 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
Craig Topper0fbf3642012-04-23 03:28:34 +00001713 getTargetMachine(), RVLocs, *DAG.getContext());
Dan Gohman98ca4f22009-08-05 01:29:28 +00001714 CCInfo.AnalyzeCallResult(Ins, RetCC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001715
Chris Lattner3085e152007-02-25 08:59:22 +00001716 // Copy all of the result registers out of their specified physreg.
Chris Lattner8e6da152008-03-10 21:08:41 +00001717 for (unsigned i = 0; i != RVLocs.size(); ++i) {
Dan Gohman37eed792009-02-04 17:28:58 +00001718 CCValAssign &VA = RVLocs[i];
Owen Andersone50ed302009-08-10 22:56:29 +00001719 EVT CopyVT = VA.getValVT();
Scott Michelfdc40a02009-02-17 22:15:04 +00001720
Torok Edwin3f142c32009-02-01 18:15:56 +00001721 // If this is x86-64, and we disabled SSE, we can't return FP values
Owen Anderson825b72b2009-08-11 20:47:22 +00001722 if ((CopyVT == MVT::f32 || CopyVT == MVT::f64) &&
Craig Topper1accb7e2012-01-10 06:54:16 +00001723 ((Is64Bit || Ins[i].Flags.isInReg()) && !Subtarget->hasSSE1())) {
Chris Lattner75361b62010-04-07 22:58:41 +00001724 report_fatal_error("SSE register return with SSE disabled");
Torok Edwin3f142c32009-02-01 18:15:56 +00001725 }
1726
Evan Cheng79fb3b42009-02-20 20:43:02 +00001727 SDValue Val;
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001728
1729 // If this is a call to a function that returns an fp value on the floating
Sylvestre Ledruc8e41c52012-07-23 08:51:15 +00001730 // point stack, we must guarantee the value is popped from the stack, so
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001731 // a CopyFromReg is not good enough - the copy instruction may be eliminated
Jakob Stoklund Olesen9bbe4d62011-06-28 18:32:28 +00001732 // if the return value is not used. We use the FpPOP_RETVAL instruction
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001733 // instead.
1734 if (VA.getLocReg() == X86::ST0 || VA.getLocReg() == X86::ST1) {
1735 // If we prefer to use the value in xmm registers, copy it out as f80 and
1736 // use a truncate to move it from fp stack reg to xmm reg.
1737 if (isScalarFPTypeInSSEReg(VA.getValVT())) CopyVT = MVT::f80;
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001738 SDValue Ops[] = { Chain, InFlag };
Jakob Stoklund Olesen9bbe4d62011-06-28 18:32:28 +00001739 Chain = SDValue(DAG.getMachineNode(X86::FpPOP_RETVAL, dl, CopyVT,
1740 MVT::Other, MVT::Glue, Ops, 2), 1);
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001741 Val = Chain.getValue(0);
1742
1743 // Round the f80 to the right size, which also moves it to the appropriate
1744 // xmm register.
1745 if (CopyVT != VA.getValVT())
1746 Val = DAG.getNode(ISD::FP_ROUND, dl, VA.getValVT(), Val,
1747 // This truncation won't change the value.
1748 DAG.getIntPtrConstant(1));
Evan Cheng79fb3b42009-02-20 20:43:02 +00001749 } else {
1750 Chain = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(),
1751 CopyVT, InFlag).getValue(1);
1752 Val = Chain.getValue(0);
1753 }
Chris Lattner8e6da152008-03-10 21:08:41 +00001754 InFlag = Chain.getValue(2);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001755 InVals.push_back(Val);
Chris Lattner3085e152007-02-25 08:59:22 +00001756 }
Duncan Sands4bdcb612008-07-02 17:40:58 +00001757
Dan Gohman98ca4f22009-08-05 01:29:28 +00001758 return Chain;
Chris Lattner2b02a442007-02-25 08:29:00 +00001759}
1760
1761
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001762//===----------------------------------------------------------------------===//
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001763// C & StdCall & Fast Calling Convention implementation
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001764//===----------------------------------------------------------------------===//
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001765// StdCall calling convention seems to be standard for many Windows' API
1766// routines and around. It differs from C calling convention just a little:
1767// callee should clean up the stack, not caller. Symbols should be also
1768// decorated in some fancy way :) It doesn't support any vector arguments.
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001769// For info on fast calling convention see Fast Calling Convention (tail call)
1770// implementation LowerX86_32FastCCCallTo.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001771
Dan Gohman98ca4f22009-08-05 01:29:28 +00001772/// CallIsStructReturn - Determines whether a call uses struct return
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001773/// semantics.
Rafael Espindola1cee7102012-07-25 13:41:10 +00001774enum StructReturnType {
1775 NotStructReturn,
1776 RegStructReturn,
1777 StackStructReturn
1778};
1779static StructReturnType
1780callIsStructReturn(const SmallVectorImpl<ISD::OutputArg> &Outs) {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001781 if (Outs.empty())
Rafael Espindola1cee7102012-07-25 13:41:10 +00001782 return NotStructReturn;
Duncan Sands276dcbd2008-03-21 09:14:45 +00001783
Rafael Espindola1cee7102012-07-25 13:41:10 +00001784 const ISD::ArgFlagsTy &Flags = Outs[0].Flags;
1785 if (!Flags.isSRet())
1786 return NotStructReturn;
1787 if (Flags.isInReg())
1788 return RegStructReturn;
1789 return StackStructReturn;
Gordon Henriksen86737662008-01-05 16:56:59 +00001790}
1791
Dan Gohman7e77b0f2009-08-01 19:14:37 +00001792/// ArgsAreStructReturn - Determines whether a function uses struct
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001793/// return semantics.
Rafael Espindola1cee7102012-07-25 13:41:10 +00001794static StructReturnType
1795argsAreStructReturn(const SmallVectorImpl<ISD::InputArg> &Ins) {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001796 if (Ins.empty())
Rafael Espindola1cee7102012-07-25 13:41:10 +00001797 return NotStructReturn;
Duncan Sands276dcbd2008-03-21 09:14:45 +00001798
Rafael Espindola1cee7102012-07-25 13:41:10 +00001799 const ISD::ArgFlagsTy &Flags = Ins[0].Flags;
1800 if (!Flags.isSRet())
1801 return NotStructReturn;
1802 if (Flags.isInReg())
1803 return RegStructReturn;
1804 return StackStructReturn;
Gordon Henriksen86737662008-01-05 16:56:59 +00001805}
1806
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001807/// CreateCopyOfByValArgument - Make a copy of an aggregate at address specified
1808/// by "Src" to address "Dst" with size and alignment information specified by
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001809/// the specific parameter attribute. The copy will be passed as a byval
1810/// function parameter.
Scott Michelfdc40a02009-02-17 22:15:04 +00001811static SDValue
Dan Gohman475871a2008-07-27 21:46:04 +00001812CreateCopyOfByValArgument(SDValue Src, SDValue Dst, SDValue Chain,
Dale Johannesendd64c412009-02-04 00:33:20 +00001813 ISD::ArgFlagsTy Flags, SelectionDAG &DAG,
1814 DebugLoc dl) {
Chris Lattnere72f2022010-09-21 05:40:29 +00001815 SDValue SizeNode = DAG.getConstant(Flags.getByValSize(), MVT::i32);
Michael J. Spencerec38de22010-10-10 22:04:20 +00001816
Dale Johannesendd64c412009-02-04 00:33:20 +00001817 return DAG.getMemcpy(Chain, dl, Dst, Src, SizeNode, Flags.getByValAlign(),
Stuart Hastings03d58262011-03-10 00:25:53 +00001818 /*isVolatile*/false, /*AlwaysInline=*/true,
Chris Lattnerfc448ff2010-09-21 18:51:21 +00001819 MachinePointerInfo(), MachinePointerInfo());
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001820}
1821
Chris Lattner29689432010-03-11 00:22:57 +00001822/// IsTailCallConvention - Return true if the calling convention is one that
1823/// supports tail call optimization.
1824static bool IsTailCallConvention(CallingConv::ID CC) {
1825 return (CC == CallingConv::Fast || CC == CallingConv::GHC);
1826}
1827
Evan Cheng485fafc2011-03-21 01:19:09 +00001828bool X86TargetLowering::mayBeEmittedAsTailCall(CallInst *CI) const {
Nick Lewycky22de16d2012-01-19 00:34:10 +00001829 if (!CI->isTailCall() || getTargetMachine().Options.DisableTailCalls)
Evan Cheng485fafc2011-03-21 01:19:09 +00001830 return false;
1831
1832 CallSite CS(CI);
1833 CallingConv::ID CalleeCC = CS.getCallingConv();
1834 if (!IsTailCallConvention(CalleeCC) && CalleeCC != CallingConv::C)
1835 return false;
1836
1837 return true;
1838}
1839
Evan Cheng0c439eb2010-01-27 00:07:07 +00001840/// FuncIsMadeTailCallSafe - Return true if the function is being made into
1841/// a tailcall target by changing its ABI.
Nick Lewycky8a8d4792011-12-02 22:16:29 +00001842static bool FuncIsMadeTailCallSafe(CallingConv::ID CC,
1843 bool GuaranteedTailCallOpt) {
Chris Lattner29689432010-03-11 00:22:57 +00001844 return GuaranteedTailCallOpt && IsTailCallConvention(CC);
Evan Cheng0c439eb2010-01-27 00:07:07 +00001845}
1846
Dan Gohman98ca4f22009-08-05 01:29:28 +00001847SDValue
1848X86TargetLowering::LowerMemArgument(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001849 CallingConv::ID CallConv,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001850 const SmallVectorImpl<ISD::InputArg> &Ins,
1851 DebugLoc dl, SelectionDAG &DAG,
1852 const CCValAssign &VA,
1853 MachineFrameInfo *MFI,
Dan Gohmand858e902010-04-17 15:26:15 +00001854 unsigned i) const {
Rafael Espindola7effac52007-09-14 15:48:13 +00001855 // Create the nodes corresponding to a load from this parameter slot.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001856 ISD::ArgFlagsTy Flags = Ins[i].Flags;
Nick Lewycky8a8d4792011-12-02 22:16:29 +00001857 bool AlwaysUseMutable = FuncIsMadeTailCallSafe(CallConv,
1858 getTargetMachine().Options.GuaranteedTailCallOpt);
Duncan Sands276dcbd2008-03-21 09:14:45 +00001859 bool isImmutable = !AlwaysUseMutable && !Flags.isByVal();
Anton Korobeynikov22472762009-08-14 18:19:10 +00001860 EVT ValVT;
1861
1862 // If value is passed by pointer we have address passed instead of the value
1863 // itself.
1864 if (VA.getLocInfo() == CCValAssign::Indirect)
1865 ValVT = VA.getLocVT();
1866 else
1867 ValVT = VA.getValVT();
Evan Chenge70bb592008-01-10 02:24:25 +00001868
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001869 // FIXME: For now, all byval parameter objects are marked mutable. This can be
Scott Michelfdc40a02009-02-17 22:15:04 +00001870 // changed with more analysis.
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001871 // In case of tail call optimization mark all arguments mutable. Since they
1872 // could be overwritten by lowering of arguments in case of a tail call.
Evan Cheng90567c32010-02-02 23:58:13 +00001873 if (Flags.isByVal()) {
Evan Chengee2e0e32011-03-30 23:44:13 +00001874 unsigned Bytes = Flags.getByValSize();
1875 if (Bytes == 0) Bytes = 1; // Don't create zero-sized stack objects.
1876 int FI = MFI->CreateFixedObject(Bytes, VA.getLocMemOffset(), isImmutable);
Evan Cheng90567c32010-02-02 23:58:13 +00001877 return DAG.getFrameIndex(FI, getPointerTy());
1878 } else {
1879 int FI = MFI->CreateFixedObject(ValVT.getSizeInBits()/8,
Evan Chenged2ae132010-07-03 00:40:23 +00001880 VA.getLocMemOffset(), isImmutable);
Evan Cheng90567c32010-02-02 23:58:13 +00001881 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
1882 return DAG.getLoad(ValVT, dl, Chain, FIN,
Chris Lattnere8639032010-09-21 06:22:23 +00001883 MachinePointerInfo::getFixedStack(FI),
Pete Cooperd752e0f2011-11-08 18:42:53 +00001884 false, false, false, 0);
Evan Cheng90567c32010-02-02 23:58:13 +00001885 }
Rafael Espindola7effac52007-09-14 15:48:13 +00001886}
1887
Dan Gohman475871a2008-07-27 21:46:04 +00001888SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00001889X86TargetLowering::LowerFormalArguments(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001890 CallingConv::ID CallConv,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001891 bool isVarArg,
1892 const SmallVectorImpl<ISD::InputArg> &Ins,
1893 DebugLoc dl,
1894 SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001895 SmallVectorImpl<SDValue> &InVals)
1896 const {
Evan Cheng1bc78042006-04-26 01:20:17 +00001897 MachineFunction &MF = DAG.getMachineFunction();
Gordon Henriksen86737662008-01-05 16:56:59 +00001898 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
Scott Michelfdc40a02009-02-17 22:15:04 +00001899
Gordon Henriksen86737662008-01-05 16:56:59 +00001900 const Function* Fn = MF.getFunction();
1901 if (Fn->hasExternalLinkage() &&
1902 Subtarget->isTargetCygMing() &&
1903 Fn->getName() == "main")
1904 FuncInfo->setForceFramePointer(true);
1905
Evan Cheng1bc78042006-04-26 01:20:17 +00001906 MachineFrameInfo *MFI = MF.getFrameInfo();
Gordon Henriksen86737662008-01-05 16:56:59 +00001907 bool Is64Bit = Subtarget->is64Bit();
Eli Friedman9a2478a2012-01-20 00:05:46 +00001908 bool IsWindows = Subtarget->isTargetWindows();
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001909 bool IsWin64 = Subtarget->isTargetWin64();
Gordon Henriksenae636f82008-01-03 16:47:34 +00001910
Chris Lattner29689432010-03-11 00:22:57 +00001911 assert(!(isVarArg && IsTailCallConvention(CallConv)) &&
1912 "Var args not supported with calling convention fastcc or ghc");
Gordon Henriksenae636f82008-01-03 16:47:34 +00001913
Chris Lattner638402b2007-02-28 07:00:42 +00001914 // Assign locations to all of the incoming arguments.
Chris Lattnerf39f7712007-02-28 05:46:49 +00001915 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00001916 CCState CCInfo(CallConv, isVarArg, MF, getTargetMachine(),
Dan Gohman98ca4f22009-08-05 01:29:28 +00001917 ArgLocs, *DAG.getContext());
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00001918
1919 // Allocate shadow area for Win64
1920 if (IsWin64) {
1921 CCInfo.AllocateStack(32, 8);
1922 }
1923
Duncan Sands45907662010-10-31 13:21:44 +00001924 CCInfo.AnalyzeFormalArguments(Ins, CC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001925
Chris Lattnerf39f7712007-02-28 05:46:49 +00001926 unsigned LastVal = ~0U;
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001927 SDValue ArgValue;
Chris Lattnerf39f7712007-02-28 05:46:49 +00001928 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1929 CCValAssign &VA = ArgLocs[i];
1930 // TODO: If an arg is passed in two places (e.g. reg and stack), skip later
1931 // places.
1932 assert(VA.getValNo() != LastVal &&
1933 "Don't support value assigned to multiple locs yet");
Duncan Sands17001ce2011-10-18 12:44:00 +00001934 (void)LastVal;
Chris Lattnerf39f7712007-02-28 05:46:49 +00001935 LastVal = VA.getValNo();
Scott Michelfdc40a02009-02-17 22:15:04 +00001936
Chris Lattnerf39f7712007-02-28 05:46:49 +00001937 if (VA.isRegLoc()) {
Owen Andersone50ed302009-08-10 22:56:29 +00001938 EVT RegVT = VA.getLocVT();
Craig Topper44d23822012-02-22 05:59:10 +00001939 const TargetRegisterClass *RC;
Owen Anderson825b72b2009-08-11 20:47:22 +00001940 if (RegVT == MVT::i32)
Craig Topperc9099502012-04-20 06:31:50 +00001941 RC = &X86::GR32RegClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001942 else if (Is64Bit && RegVT == MVT::i64)
Craig Topperc9099502012-04-20 06:31:50 +00001943 RC = &X86::GR64RegClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001944 else if (RegVT == MVT::f32)
Craig Topperc9099502012-04-20 06:31:50 +00001945 RC = &X86::FR32RegClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001946 else if (RegVT == MVT::f64)
Craig Topperc9099502012-04-20 06:31:50 +00001947 RC = &X86::FR64RegClass;
Craig Topper7a9a28b2012-08-12 02:23:29 +00001948 else if (RegVT.is256BitVector())
Craig Topperc9099502012-04-20 06:31:50 +00001949 RC = &X86::VR256RegClass;
Craig Topper7a9a28b2012-08-12 02:23:29 +00001950 else if (RegVT.is128BitVector())
Craig Topperc9099502012-04-20 06:31:50 +00001951 RC = &X86::VR128RegClass;
Dale Johannesen0488fb62010-09-30 23:57:10 +00001952 else if (RegVT == MVT::x86mmx)
Craig Topperc9099502012-04-20 06:31:50 +00001953 RC = &X86::VR64RegClass;
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001954 else
Torok Edwinc23197a2009-07-14 16:55:14 +00001955 llvm_unreachable("Unknown argument type!");
Gordon Henriksenae636f82008-01-03 16:47:34 +00001956
Devang Patel68e6bee2011-02-21 23:21:26 +00001957 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001958 ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT);
Scott Michelfdc40a02009-02-17 22:15:04 +00001959
Chris Lattnerf39f7712007-02-28 05:46:49 +00001960 // If this is an 8 or 16-bit value, it is really passed promoted to 32
1961 // bits. Insert an assert[sz]ext to capture this, then truncate to the
1962 // right size.
1963 if (VA.getLocInfo() == CCValAssign::SExt)
Dale Johannesenace16102009-02-03 19:33:06 +00001964 ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue,
Chris Lattnerf39f7712007-02-28 05:46:49 +00001965 DAG.getValueType(VA.getValVT()));
1966 else if (VA.getLocInfo() == CCValAssign::ZExt)
Dale Johannesenace16102009-02-03 19:33:06 +00001967 ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue,
Chris Lattnerf39f7712007-02-28 05:46:49 +00001968 DAG.getValueType(VA.getValVT()));
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001969 else if (VA.getLocInfo() == CCValAssign::BCvt)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001970 ArgValue = DAG.getNode(ISD::BITCAST, dl, VA.getValVT(), ArgValue);
Scott Michelfdc40a02009-02-17 22:15:04 +00001971
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001972 if (VA.isExtInLoc()) {
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001973 // Handle MMX values passed in XMM regs.
1974 if (RegVT.isVector()) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00001975 ArgValue = DAG.getNode(X86ISD::MOVDQ2Q, dl, VA.getValVT(),
1976 ArgValue);
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001977 } else
1978 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
Evan Cheng44c0fd12008-04-25 20:13:28 +00001979 }
Chris Lattnerf39f7712007-02-28 05:46:49 +00001980 } else {
1981 assert(VA.isMemLoc());
Dan Gohman98ca4f22009-08-05 01:29:28 +00001982 ArgValue = LowerMemArgument(Chain, CallConv, Ins, dl, DAG, VA, MFI, i);
Evan Cheng1bc78042006-04-26 01:20:17 +00001983 }
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001984
1985 // If value is passed via pointer - do a load.
1986 if (VA.getLocInfo() == CCValAssign::Indirect)
Chris Lattner51abfe42010-09-21 06:02:19 +00001987 ArgValue = DAG.getLoad(VA.getValVT(), dl, Chain, ArgValue,
Pete Cooperd752e0f2011-11-08 18:42:53 +00001988 MachinePointerInfo(), false, false, false, 0);
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001989
Dan Gohman98ca4f22009-08-05 01:29:28 +00001990 InVals.push_back(ArgValue);
Evan Cheng1bc78042006-04-26 01:20:17 +00001991 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001992
Dan Gohman61a92132008-04-21 23:59:07 +00001993 // The x86-64 ABI for returning structs by value requires that we copy
1994 // the sret argument into %rax for the return. Save the argument into
1995 // a virtual register so that we can access it from the return points.
Dan Gohman7e77b0f2009-08-01 19:14:37 +00001996 if (Is64Bit && MF.getFunction()->hasStructRetAttr()) {
Dan Gohman61a92132008-04-21 23:59:07 +00001997 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1998 unsigned Reg = FuncInfo->getSRetReturnReg();
1999 if (!Reg) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002000 Reg = MF.getRegInfo().createVirtualRegister(getRegClassFor(MVT::i64));
Dan Gohman61a92132008-04-21 23:59:07 +00002001 FuncInfo->setSRetReturnReg(Reg);
2002 }
Dan Gohman98ca4f22009-08-05 01:29:28 +00002003 SDValue Copy = DAG.getCopyToReg(DAG.getEntryNode(), dl, Reg, InVals[0]);
Owen Anderson825b72b2009-08-11 20:47:22 +00002004 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Copy, Chain);
Dan Gohman61a92132008-04-21 23:59:07 +00002005 }
2006
Chris Lattnerf39f7712007-02-28 05:46:49 +00002007 unsigned StackSize = CCInfo.getNextStackOffset();
Evan Cheng0c439eb2010-01-27 00:07:07 +00002008 // Align stack specially for tail calls.
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002009 if (FuncIsMadeTailCallSafe(CallConv,
2010 MF.getTarget().Options.GuaranteedTailCallOpt))
Gordon Henriksenae636f82008-01-03 16:47:34 +00002011 StackSize = GetAlignedArgumentStackSize(StackSize, DAG);
Evan Cheng25caf632006-05-23 21:06:34 +00002012
Evan Cheng1bc78042006-04-26 01:20:17 +00002013 // If the function takes variable number of arguments, make a frame index for
2014 // the start of the first vararg value... for expansion of llvm.va_start.
Gordon Henriksenae636f82008-01-03 16:47:34 +00002015 if (isVarArg) {
NAKAMURA Takumi3ca99432011-03-09 11:33:15 +00002016 if (Is64Bit || (CallConv != CallingConv::X86_FastCall &&
2017 CallConv != CallingConv::X86_ThisCall)) {
Jakob Stoklund Olesenb2eeed72010-07-29 17:42:27 +00002018 FuncInfo->setVarArgsFrameIndex(MFI->CreateFixedObject(1, StackSize,true));
Gordon Henriksen86737662008-01-05 16:56:59 +00002019 }
2020 if (Is64Bit) {
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002021 unsigned TotalNumIntRegs = 0, TotalNumXMMRegs = 0;
2022
2023 // FIXME: We should really autogenerate these arrays
Craig Topperc5eaae42012-03-11 07:57:25 +00002024 static const uint16_t GPR64ArgRegsWin64[] = {
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002025 X86::RCX, X86::RDX, X86::R8, X86::R9
Gordon Henriksen86737662008-01-05 16:56:59 +00002026 };
Craig Topperc5eaae42012-03-11 07:57:25 +00002027 static const uint16_t GPR64ArgRegs64Bit[] = {
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002028 X86::RDI, X86::RSI, X86::RDX, X86::RCX, X86::R8, X86::R9
2029 };
Craig Topperc5eaae42012-03-11 07:57:25 +00002030 static const uint16_t XMMArgRegs64Bit[] = {
Gordon Henriksen86737662008-01-05 16:56:59 +00002031 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
2032 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
2033 };
Craig Topperc5eaae42012-03-11 07:57:25 +00002034 const uint16_t *GPR64ArgRegs;
Anton Korobeynikove7beda12010-10-03 22:52:07 +00002035 unsigned NumXMMRegs = 0;
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002036
2037 if (IsWin64) {
Anton Korobeynikove7beda12010-10-03 22:52:07 +00002038 // The XMM registers which might contain var arg parameters are shadowed
2039 // in their paired GPR. So we only need to save the GPR to their home
2040 // slots.
2041 TotalNumIntRegs = 4;
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002042 GPR64ArgRegs = GPR64ArgRegsWin64;
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002043 } else {
2044 TotalNumIntRegs = 6; TotalNumXMMRegs = 8;
2045 GPR64ArgRegs = GPR64ArgRegs64Bit;
Anton Korobeynikove7beda12010-10-03 22:52:07 +00002046
Chad Rosier30450e82011-12-22 22:35:21 +00002047 NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs64Bit,
2048 TotalNumXMMRegs);
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002049 }
2050 unsigned NumIntRegs = CCInfo.getFirstUnallocated(GPR64ArgRegs,
2051 TotalNumIntRegs);
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002052
Bill Wendling67658342012-10-09 07:45:08 +00002053 bool NoImplicitFloatOps = Fn->getFnAttributes().
2054 hasAttribute(Attributes::NoImplicitFloat);
Craig Topper1accb7e2012-01-10 06:54:16 +00002055 assert(!(NumXMMRegs && !Subtarget->hasSSE1()) &&
Torok Edwin3f142c32009-02-01 18:15:56 +00002056 "SSE register cannot be used when SSE is disabled!");
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002057 assert(!(NumXMMRegs && MF.getTarget().Options.UseSoftFloat &&
2058 NoImplicitFloatOps) &&
Evan Chengc7ce29b2009-02-13 22:36:38 +00002059 "SSE register cannot be used when SSE is disabled!");
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002060 if (MF.getTarget().Options.UseSoftFloat || NoImplicitFloatOps ||
Craig Topper1accb7e2012-01-10 06:54:16 +00002061 !Subtarget->hasSSE1())
Torok Edwin3f142c32009-02-01 18:15:56 +00002062 // Kernel mode asks for SSE to be disabled, so don't push them
2063 // on the stack.
2064 TotalNumXMMRegs = 0;
Bill Wendlingf9abd7e2009-03-11 22:30:01 +00002065
Anton Korobeynikove7beda12010-10-03 22:52:07 +00002066 if (IsWin64) {
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00002067 const TargetFrameLowering &TFI = *getTargetMachine().getFrameLowering();
Cameron Esfahaniec37b002010-10-08 19:24:18 +00002068 // Get to the caller-allocated home save location. Add 8 to account
2069 // for the return address.
2070 int HomeOffset = TFI.getOffsetOfLocalArea() + 8;
Anton Korobeynikove7beda12010-10-03 22:52:07 +00002071 FuncInfo->setRegSaveFrameIndex(
Cameron Esfahaniec37b002010-10-08 19:24:18 +00002072 MFI->CreateFixedObject(1, NumIntRegs * 8 + HomeOffset, false));
NAKAMURA Takumi3ca99432011-03-09 11:33:15 +00002073 // Fixup to set vararg frame on shadow area (4 x i64).
2074 if (NumIntRegs < 4)
2075 FuncInfo->setVarArgsFrameIndex(FuncInfo->getRegSaveFrameIndex());
Anton Korobeynikove7beda12010-10-03 22:52:07 +00002076 } else {
2077 // For X86-64, if there are vararg parameters that are passed via
Chad Rosier30450e82011-12-22 22:35:21 +00002078 // registers, then we must store them to their spots on the stack so
2079 // they may be loaded by deferencing the result of va_next.
Anton Korobeynikove7beda12010-10-03 22:52:07 +00002080 FuncInfo->setVarArgsGPOffset(NumIntRegs * 8);
2081 FuncInfo->setVarArgsFPOffset(TotalNumIntRegs * 8 + NumXMMRegs * 16);
2082 FuncInfo->setRegSaveFrameIndex(
2083 MFI->CreateStackObject(TotalNumIntRegs * 8 + TotalNumXMMRegs * 16, 16,
Dan Gohman1e93df62010-04-17 14:41:14 +00002084 false));
Anton Korobeynikove7beda12010-10-03 22:52:07 +00002085 }
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002086
Gordon Henriksen86737662008-01-05 16:56:59 +00002087 // Store the integer parameter registers.
Dan Gohman475871a2008-07-27 21:46:04 +00002088 SmallVector<SDValue, 8> MemOps;
Dan Gohman1e93df62010-04-17 14:41:14 +00002089 SDValue RSFIN = DAG.getFrameIndex(FuncInfo->getRegSaveFrameIndex(),
2090 getPointerTy());
2091 unsigned Offset = FuncInfo->getVarArgsGPOffset();
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002092 for (; NumIntRegs != TotalNumIntRegs; ++NumIntRegs) {
Dan Gohmand6708ea2009-08-15 01:38:56 +00002093 SDValue FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), RSFIN,
2094 DAG.getIntPtrConstant(Offset));
Bob Wilson998e1252009-04-20 18:36:57 +00002095 unsigned VReg = MF.addLiveIn(GPR64ArgRegs[NumIntRegs],
Craig Topperc9099502012-04-20 06:31:50 +00002096 &X86::GR64RegClass);
Owen Anderson825b72b2009-08-11 20:47:22 +00002097 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i64);
Dan Gohman475871a2008-07-27 21:46:04 +00002098 SDValue Store =
Dale Johannesenace16102009-02-03 19:33:06 +00002099 DAG.getStore(Val.getValue(1), dl, Val, FIN,
Chris Lattnere8639032010-09-21 06:22:23 +00002100 MachinePointerInfo::getFixedStack(
2101 FuncInfo->getRegSaveFrameIndex(), Offset),
2102 false, false, 0);
Gordon Henriksen86737662008-01-05 16:56:59 +00002103 MemOps.push_back(Store);
Dan Gohmand6708ea2009-08-15 01:38:56 +00002104 Offset += 8;
Gordon Henriksen86737662008-01-05 16:56:59 +00002105 }
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002106
Dan Gohmanface41a2009-08-16 21:24:25 +00002107 if (TotalNumXMMRegs != 0 && NumXMMRegs != TotalNumXMMRegs) {
2108 // Now store the XMM (fp + vector) parameter registers.
2109 SmallVector<SDValue, 11> SaveXMMOps;
2110 SaveXMMOps.push_back(Chain);
Dan Gohmand6708ea2009-08-15 01:38:56 +00002111
Craig Topperc9099502012-04-20 06:31:50 +00002112 unsigned AL = MF.addLiveIn(X86::AL, &X86::GR8RegClass);
Dan Gohmanface41a2009-08-16 21:24:25 +00002113 SDValue ALVal = DAG.getCopyFromReg(DAG.getEntryNode(), dl, AL, MVT::i8);
2114 SaveXMMOps.push_back(ALVal);
Dan Gohmand6708ea2009-08-15 01:38:56 +00002115
Dan Gohman1e93df62010-04-17 14:41:14 +00002116 SaveXMMOps.push_back(DAG.getIntPtrConstant(
2117 FuncInfo->getRegSaveFrameIndex()));
2118 SaveXMMOps.push_back(DAG.getIntPtrConstant(
2119 FuncInfo->getVarArgsFPOffset()));
Dan Gohmand6708ea2009-08-15 01:38:56 +00002120
Dan Gohmanface41a2009-08-16 21:24:25 +00002121 for (; NumXMMRegs != TotalNumXMMRegs; ++NumXMMRegs) {
Anton Korobeynikove7beda12010-10-03 22:52:07 +00002122 unsigned VReg = MF.addLiveIn(XMMArgRegs64Bit[NumXMMRegs],
Craig Topperc9099502012-04-20 06:31:50 +00002123 &X86::VR128RegClass);
Dan Gohmanface41a2009-08-16 21:24:25 +00002124 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::v4f32);
2125 SaveXMMOps.push_back(Val);
2126 }
2127 MemOps.push_back(DAG.getNode(X86ISD::VASTART_SAVE_XMM_REGS, dl,
2128 MVT::Other,
2129 &SaveXMMOps[0], SaveXMMOps.size()));
Gordon Henriksen86737662008-01-05 16:56:59 +00002130 }
Dan Gohmanface41a2009-08-16 21:24:25 +00002131
2132 if (!MemOps.empty())
2133 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
2134 &MemOps[0], MemOps.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00002135 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00002136 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002137
Gordon Henriksen86737662008-01-05 16:56:59 +00002138 // Some CCs need callee pop.
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002139 if (X86::isCalleePop(CallConv, Is64Bit, isVarArg,
2140 MF.getTarget().Options.GuaranteedTailCallOpt)) {
Dan Gohman1e93df62010-04-17 14:41:14 +00002141 FuncInfo->setBytesToPopOnReturn(StackSize); // Callee pops everything.
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00002142 } else {
Dan Gohman1e93df62010-04-17 14:41:14 +00002143 FuncInfo->setBytesToPopOnReturn(0); // Callee pops nothing.
Chris Lattnerf39f7712007-02-28 05:46:49 +00002144 // If this is an sret function, the return should pop the hidden pointer.
Eli Friedman9a2478a2012-01-20 00:05:46 +00002145 if (!Is64Bit && !IsTailCallConvention(CallConv) && !IsWindows &&
Rafael Espindola1cee7102012-07-25 13:41:10 +00002146 argsAreStructReturn(Ins) == StackStructReturn)
Dan Gohman1e93df62010-04-17 14:41:14 +00002147 FuncInfo->setBytesToPopOnReturn(4);
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00002148 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00002149
Gordon Henriksen86737662008-01-05 16:56:59 +00002150 if (!Is64Bit) {
Dan Gohman1e93df62010-04-17 14:41:14 +00002151 // RegSaveFrameIndex is X86-64 only.
2152 FuncInfo->setRegSaveFrameIndex(0xAAAAAAA);
Anton Korobeynikovded05e32010-05-16 09:08:45 +00002153 if (CallConv == CallingConv::X86_FastCall ||
2154 CallConv == CallingConv::X86_ThisCall)
Dan Gohman1e93df62010-04-17 14:41:14 +00002155 // fastcc functions can't have varargs.
2156 FuncInfo->setVarArgsFrameIndex(0xAAAAAAA);
Gordon Henriksen86737662008-01-05 16:56:59 +00002157 }
Evan Cheng25caf632006-05-23 21:06:34 +00002158
Rafael Espindola76927d752011-08-30 19:39:58 +00002159 FuncInfo->setArgumentStackSize(StackSize);
2160
Dan Gohman98ca4f22009-08-05 01:29:28 +00002161 return Chain;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002162}
2163
Dan Gohman475871a2008-07-27 21:46:04 +00002164SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00002165X86TargetLowering::LowerMemOpCallTo(SDValue Chain,
2166 SDValue StackPtr, SDValue Arg,
2167 DebugLoc dl, SelectionDAG &DAG,
Evan Chengdffbd832008-01-10 00:09:10 +00002168 const CCValAssign &VA,
Dan Gohmand858e902010-04-17 15:26:15 +00002169 ISD::ArgFlagsTy Flags) const {
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00002170 unsigned LocMemOffset = VA.getLocMemOffset();
Dan Gohman475871a2008-07-27 21:46:04 +00002171 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset);
Dale Johannesenace16102009-02-03 19:33:06 +00002172 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, PtrOff);
Chris Lattnerfc448ff2010-09-21 18:51:21 +00002173 if (Flags.isByVal())
Dale Johannesendd64c412009-02-04 00:33:20 +00002174 return CreateCopyOfByValArgument(Arg, PtrOff, Chain, Flags, DAG, dl);
Chris Lattnerfc448ff2010-09-21 18:51:21 +00002175
2176 return DAG.getStore(Chain, dl, Arg, PtrOff,
2177 MachinePointerInfo::getStack(LocMemOffset),
David Greene67c9d422010-02-15 16:53:33 +00002178 false, false, 0);
Evan Chengdffbd832008-01-10 00:09:10 +00002179}
2180
Bill Wendling64e87322009-01-16 19:25:27 +00002181/// EmitTailCallLoadRetAddr - Emit a load of return address if tail call
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002182/// optimization is performed and it is required.
Scott Michelfdc40a02009-02-17 22:15:04 +00002183SDValue
2184X86TargetLowering::EmitTailCallLoadRetAddr(SelectionDAG &DAG,
Evan Chengddc419c2010-01-26 19:04:47 +00002185 SDValue &OutRetAddr, SDValue Chain,
2186 bool IsTailCall, bool Is64Bit,
Dan Gohmand858e902010-04-17 15:26:15 +00002187 int FPDiff, DebugLoc dl) const {
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002188 // Adjust the Return address stack slot.
Owen Andersone50ed302009-08-10 22:56:29 +00002189 EVT VT = getPointerTy();
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002190 OutRetAddr = getReturnAddressFrameIndex(DAG);
Bill Wendling64e87322009-01-16 19:25:27 +00002191
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002192 // Load the "old" Return address.
Chris Lattner51abfe42010-09-21 06:02:19 +00002193 OutRetAddr = DAG.getLoad(VT, dl, Chain, OutRetAddr, MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002194 false, false, false, 0);
Gabor Greifba36cb52008-08-28 21:40:38 +00002195 return SDValue(OutRetAddr.getNode(), 1);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002196}
2197
Chris Lattner7a2bdde2011-04-15 05:18:47 +00002198/// EmitTailCallStoreRetAddr - Emit a store of the return address if tail call
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002199/// optimization is performed and it is required (FPDiff!=0).
Scott Michelfdc40a02009-02-17 22:15:04 +00002200static SDValue
2201EmitTailCallStoreRetAddr(SelectionDAG & DAG, MachineFunction &MF,
Michael Liaoaa3c2c02012-10-25 06:29:14 +00002202 SDValue Chain, SDValue RetAddrFrIdx, EVT PtrVT,
2203 unsigned SlotSize, int FPDiff, DebugLoc dl) {
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002204 // Store the return address to the appropriate stack slot.
2205 if (!FPDiff) return Chain;
2206 // Calculate the new stack slot for the return address.
Scott Michelfdc40a02009-02-17 22:15:04 +00002207 int NewReturnAddrFI =
Evan Chenged2ae132010-07-03 00:40:23 +00002208 MF.getFrameInfo()->CreateFixedObject(SlotSize, FPDiff-SlotSize, false);
Michael Liaoaa3c2c02012-10-25 06:29:14 +00002209 SDValue NewRetAddrFrIdx = DAG.getFrameIndex(NewReturnAddrFI, PtrVT);
Scott Michelfdc40a02009-02-17 22:15:04 +00002210 Chain = DAG.getStore(Chain, dl, RetAddrFrIdx, NewRetAddrFrIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00002211 MachinePointerInfo::getFixedStack(NewReturnAddrFI),
David Greene67c9d422010-02-15 16:53:33 +00002212 false, false, 0);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002213 return Chain;
2214}
2215
Dan Gohman98ca4f22009-08-05 01:29:28 +00002216SDValue
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00002217X86TargetLowering::LowerCall(TargetLowering::CallLoweringInfo &CLI,
Dan Gohmand858e902010-04-17 15:26:15 +00002218 SmallVectorImpl<SDValue> &InVals) const {
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00002219 SelectionDAG &DAG = CLI.DAG;
2220 DebugLoc &dl = CLI.DL;
2221 SmallVector<ISD::OutputArg, 32> &Outs = CLI.Outs;
2222 SmallVector<SDValue, 32> &OutVals = CLI.OutVals;
2223 SmallVector<ISD::InputArg, 32> &Ins = CLI.Ins;
2224 SDValue Chain = CLI.Chain;
2225 SDValue Callee = CLI.Callee;
2226 CallingConv::ID CallConv = CLI.CallConv;
2227 bool &isTailCall = CLI.IsTailCall;
2228 bool isVarArg = CLI.IsVarArg;
2229
Dan Gohman98ca4f22009-08-05 01:29:28 +00002230 MachineFunction &MF = DAG.getMachineFunction();
2231 bool Is64Bit = Subtarget->is64Bit();
NAKAMURA Takumifb840c92011-02-05 15:11:13 +00002232 bool IsWin64 = Subtarget->isTargetWin64();
Eli Friedman9a2478a2012-01-20 00:05:46 +00002233 bool IsWindows = Subtarget->isTargetWindows();
Rafael Espindola1cee7102012-07-25 13:41:10 +00002234 StructReturnType SR = callIsStructReturn(Outs);
Evan Cheng5f941932010-02-05 02:21:12 +00002235 bool IsSibcall = false;
Dan Gohman98ca4f22009-08-05 01:29:28 +00002236
Nick Lewycky22de16d2012-01-19 00:34:10 +00002237 if (MF.getTarget().Options.DisableTailCalls)
2238 isTailCall = false;
2239
Evan Cheng5f941932010-02-05 02:21:12 +00002240 if (isTailCall) {
Evan Cheng0c439eb2010-01-27 00:07:07 +00002241 // Check if it's really possible to do a tail call.
Evan Chenga375d472010-03-15 18:54:48 +00002242 isTailCall = IsEligibleForTailCallOptimization(Callee, CallConv,
Rafael Espindola1cee7102012-07-25 13:41:10 +00002243 isVarArg, SR != NotStructReturn,
Evan Chengb1cacc72012-09-25 05:32:34 +00002244 MF.getFunction()->hasStructRetAttr(), CLI.RetTy,
Rafael Espindola1cee7102012-07-25 13:41:10 +00002245 Outs, OutVals, Ins, DAG);
Evan Chengf22f9b32010-02-06 03:28:46 +00002246
2247 // Sibcalls are automatically detected tailcalls which do not require
2248 // ABI changes.
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002249 if (!MF.getTarget().Options.GuaranteedTailCallOpt && isTailCall)
Evan Cheng5f941932010-02-05 02:21:12 +00002250 IsSibcall = true;
Evan Chengf22f9b32010-02-06 03:28:46 +00002251
2252 if (isTailCall)
2253 ++NumTailCalls;
Evan Cheng5f941932010-02-05 02:21:12 +00002254 }
Evan Cheng0c439eb2010-01-27 00:07:07 +00002255
Chris Lattner29689432010-03-11 00:22:57 +00002256 assert(!(isVarArg && IsTailCallConvention(CallConv)) &&
2257 "Var args not supported with calling convention fastcc or ghc");
Gordon Henriksenae636f82008-01-03 16:47:34 +00002258
Chris Lattner638402b2007-02-28 07:00:42 +00002259 // Analyze operands of the call, assigning locations to each operand.
Chris Lattner423c5f42007-02-28 05:31:48 +00002260 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002261 CCState CCInfo(CallConv, isVarArg, MF, getTargetMachine(),
Dan Gohman98ca4f22009-08-05 01:29:28 +00002262 ArgLocs, *DAG.getContext());
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00002263
2264 // Allocate shadow area for Win64
2265 if (IsWin64) {
2266 CCInfo.AllocateStack(32, 8);
2267 }
2268
Duncan Sands45907662010-10-31 13:21:44 +00002269 CCInfo.AnalyzeCallOperands(Outs, CC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00002270
Chris Lattner423c5f42007-02-28 05:31:48 +00002271 // Get a count of how many bytes are to be pushed on the stack.
2272 unsigned NumBytes = CCInfo.getNextStackOffset();
Evan Chengf22f9b32010-02-06 03:28:46 +00002273 if (IsSibcall)
Evan Chengb2c92902010-02-02 02:22:50 +00002274 // This is a sibcall. The memory operands are available in caller's
2275 // own caller's stack.
2276 NumBytes = 0;
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002277 else if (getTargetMachine().Options.GuaranteedTailCallOpt &&
2278 IsTailCallConvention(CallConv))
Evan Chengf22f9b32010-02-06 03:28:46 +00002279 NumBytes = GetAlignedArgumentStackSize(NumBytes, DAG);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002280
Gordon Henriksen86737662008-01-05 16:56:59 +00002281 int FPDiff = 0;
Evan Chengf22f9b32010-02-06 03:28:46 +00002282 if (isTailCall && !IsSibcall) {
Gordon Henriksen86737662008-01-05 16:56:59 +00002283 // Lower arguments at fp - stackoffset + fpdiff.
Jakub Staszak96df4372012-10-29 22:02:26 +00002284 X86MachineFunctionInfo *X86Info = MF.getInfo<X86MachineFunctionInfo>();
2285 unsigned NumBytesCallerPushed = X86Info->getBytesToPopOnReturn();
2286
Gordon Henriksen86737662008-01-05 16:56:59 +00002287 FPDiff = NumBytesCallerPushed - NumBytes;
2288
2289 // Set the delta of movement of the returnaddr stackslot.
2290 // But only set if delta is greater than previous delta.
Jakub Staszak96df4372012-10-29 22:02:26 +00002291 if (FPDiff < X86Info->getTCReturnAddrDelta())
2292 X86Info->setTCReturnAddrDelta(FPDiff);
Gordon Henriksen86737662008-01-05 16:56:59 +00002293 }
2294
Evan Chengf22f9b32010-02-06 03:28:46 +00002295 if (!IsSibcall)
2296 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002297
Dan Gohman475871a2008-07-27 21:46:04 +00002298 SDValue RetAddrFrIdx;
Chris Lattner7a2bdde2011-04-15 05:18:47 +00002299 // Load return address for tail calls.
Evan Chengf22f9b32010-02-06 03:28:46 +00002300 if (isTailCall && FPDiff)
2301 Chain = EmitTailCallLoadRetAddr(DAG, RetAddrFrIdx, Chain, isTailCall,
2302 Is64Bit, FPDiff, dl);
Gordon Henriksen86737662008-01-05 16:56:59 +00002303
Dan Gohman475871a2008-07-27 21:46:04 +00002304 SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass;
2305 SmallVector<SDValue, 8> MemOpChains;
2306 SDValue StackPtr;
Chris Lattner423c5f42007-02-28 05:31:48 +00002307
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002308 // Walk the register/memloc assignments, inserting copies/loads. In the case
2309 // of tail call optimization arguments are handle later.
Chris Lattner423c5f42007-02-28 05:31:48 +00002310 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2311 CCValAssign &VA = ArgLocs[i];
Owen Andersone50ed302009-08-10 22:56:29 +00002312 EVT RegVT = VA.getLocVT();
Dan Gohmanc9403652010-07-07 15:54:55 +00002313 SDValue Arg = OutVals[i];
Dan Gohman98ca4f22009-08-05 01:29:28 +00002314 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Dan Gohman095cc292008-09-13 01:54:27 +00002315 bool isByVal = Flags.isByVal();
Scott Michelfdc40a02009-02-17 22:15:04 +00002316
Chris Lattner423c5f42007-02-28 05:31:48 +00002317 // Promote the value if needed.
2318 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002319 default: llvm_unreachable("Unknown loc info!");
Chris Lattner423c5f42007-02-28 05:31:48 +00002320 case CCValAssign::Full: break;
2321 case CCValAssign::SExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002322 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00002323 break;
2324 case CCValAssign::ZExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002325 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00002326 break;
2327 case CCValAssign::AExt:
Craig Topper7a9a28b2012-08-12 02:23:29 +00002328 if (RegVT.is128BitVector()) {
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002329 // Special case: passing MMX values in XMM registers.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002330 Arg = DAG.getNode(ISD::BITCAST, dl, MVT::i64, Arg);
Owen Anderson825b72b2009-08-11 20:47:22 +00002331 Arg = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i64, Arg);
2332 Arg = getMOVL(DAG, dl, MVT::v2i64, DAG.getUNDEF(MVT::v2i64), Arg);
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002333 } else
2334 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, RegVT, Arg);
2335 break;
2336 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002337 Arg = DAG.getNode(ISD::BITCAST, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00002338 break;
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00002339 case CCValAssign::Indirect: {
2340 // Store the argument.
2341 SDValue SpillSlot = DAG.CreateStackTemporary(VA.getValVT());
Evan Chengff89dcb2009-10-18 18:16:27 +00002342 int FI = cast<FrameIndexSDNode>(SpillSlot)->getIndex();
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00002343 Chain = DAG.getStore(Chain, dl, Arg, SpillSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00002344 MachinePointerInfo::getFixedStack(FI),
David Greene67c9d422010-02-15 16:53:33 +00002345 false, false, 0);
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00002346 Arg = SpillSlot;
2347 break;
2348 }
Evan Cheng6b5783d2006-05-25 18:56:34 +00002349 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002350
Chris Lattner423c5f42007-02-28 05:31:48 +00002351 if (VA.isRegLoc()) {
Stuart Hastings2aa0f232011-05-26 04:09:49 +00002352 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
2353 if (isVarArg && IsWin64) {
2354 // Win64 ABI requires argument XMM reg to be copied to the corresponding
2355 // shadow reg if callee is a varargs function.
2356 unsigned ShadowReg = 0;
2357 switch (VA.getLocReg()) {
2358 case X86::XMM0: ShadowReg = X86::RCX; break;
2359 case X86::XMM1: ShadowReg = X86::RDX; break;
2360 case X86::XMM2: ShadowReg = X86::R8; break;
2361 case X86::XMM3: ShadowReg = X86::R9; break;
Anton Korobeynikovc52bedb2010-08-27 14:43:06 +00002362 }
Stuart Hastings2aa0f232011-05-26 04:09:49 +00002363 if (ShadowReg)
2364 RegsToPass.push_back(std::make_pair(ShadowReg, Arg));
Anton Korobeynikovc52bedb2010-08-27 14:43:06 +00002365 }
Evan Chengf22f9b32010-02-06 03:28:46 +00002366 } else if (!IsSibcall && (!isTailCall || isByVal)) {
Evan Cheng5f941932010-02-05 02:21:12 +00002367 assert(VA.isMemLoc());
2368 if (StackPtr.getNode() == 0)
Michael Liaoc5c970e2012-10-31 04:14:09 +00002369 StackPtr = DAG.getCopyFromReg(Chain, dl, RegInfo->getStackRegister(),
2370 getPointerTy());
Evan Cheng5f941932010-02-05 02:21:12 +00002371 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Arg,
2372 dl, DAG, VA, Flags));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002373 }
Stuart Hastings2aa0f232011-05-26 04:09:49 +00002374 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002375
Evan Cheng32fe1032006-05-25 00:59:30 +00002376 if (!MemOpChains.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00002377 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002378 &MemOpChains[0], MemOpChains.size());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002379
Chris Lattner88e1fd52009-07-09 04:24:46 +00002380 if (Subtarget->isPICStyleGOT()) {
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002381 // ELF / PIC requires GOT in the EBX register before function calls via PLT
2382 // GOT pointer.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002383 if (!isTailCall) {
Jakob Stoklund Olesenb8720782012-07-04 19:28:31 +00002384 RegsToPass.push_back(std::make_pair(unsigned(X86::EBX),
2385 DAG.getNode(X86ISD::GlobalBaseReg, DebugLoc(), getPointerTy())));
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002386 } else {
2387 // If we are tail calling and generating PIC/GOT style code load the
2388 // address of the callee into ECX. The value in ecx is used as target of
2389 // the tail jump. This is done to circumvent the ebx/callee-saved problem
2390 // for tail calls on PIC/GOT architectures. Normally we would just put the
2391 // address of GOT into ebx and then call target@PLT. But for tail calls
2392 // ebx would be restored (since ebx is callee saved) before jumping to the
2393 // target@PLT.
2394
2395 // Note: The actual moving to ECX is done further down.
2396 GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee);
2397 if (G && !G->getGlobal()->hasHiddenVisibility() &&
2398 !G->getGlobal()->hasProtectedVisibility())
2399 Callee = LowerGlobalAddress(Callee, DAG);
2400 else if (isa<ExternalSymbolSDNode>(Callee))
Chris Lattner15a380a2009-07-09 04:39:06 +00002401 Callee = LowerExternalSymbol(Callee, DAG);
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002402 }
Anton Korobeynikov7f705592007-01-12 19:20:47 +00002403 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00002404
NAKAMURA Takumifb840c92011-02-05 15:11:13 +00002405 if (Is64Bit && isVarArg && !IsWin64) {
Gordon Henriksen86737662008-01-05 16:56:59 +00002406 // From AMD64 ABI document:
2407 // For calls that may call functions that use varargs or stdargs
2408 // (prototype-less calls or calls to functions containing ellipsis (...) in
2409 // the declaration) %al is used as hidden argument to specify the number
2410 // of SSE registers used. The contents of %al do not need to match exactly
2411 // the number of registers, but must be an ubound on the number of SSE
2412 // registers used and is in the range 0 - 8 inclusive.
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002413
Gordon Henriksen86737662008-01-05 16:56:59 +00002414 // Count the number of XMM registers allocated.
Craig Topperc5eaae42012-03-11 07:57:25 +00002415 static const uint16_t XMMArgRegs[] = {
Gordon Henriksen86737662008-01-05 16:56:59 +00002416 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
2417 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
2418 };
2419 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs, 8);
Craig Topper1accb7e2012-01-10 06:54:16 +00002420 assert((Subtarget->hasSSE1() || !NumXMMRegs)
Torok Edwin3f142c32009-02-01 18:15:56 +00002421 && "SSE registers cannot be used when SSE is disabled");
Scott Michelfdc40a02009-02-17 22:15:04 +00002422
Jakob Stoklund Olesenb8720782012-07-04 19:28:31 +00002423 RegsToPass.push_back(std::make_pair(unsigned(X86::AL),
2424 DAG.getConstant(NumXMMRegs, MVT::i8)));
Gordon Henriksen86737662008-01-05 16:56:59 +00002425 }
2426
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00002427 // For tail calls lower the arguments to the 'real' stack slot.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002428 if (isTailCall) {
2429 // Force all the incoming stack arguments to be loaded from the stack
2430 // before any new outgoing arguments are stored to the stack, because the
2431 // outgoing stack slots may alias the incoming argument stack slots, and
2432 // the alias isn't otherwise explicit. This is slightly more conservative
2433 // than necessary, because it means that each store effectively depends
2434 // on every argument instead of just those arguments it would clobber.
2435 SDValue ArgChain = DAG.getStackArgumentTokenFactor(Chain);
2436
Dan Gohman475871a2008-07-27 21:46:04 +00002437 SmallVector<SDValue, 8> MemOpChains2;
2438 SDValue FIN;
Gordon Henriksen86737662008-01-05 16:56:59 +00002439 int FI = 0;
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002440 if (getTargetMachine().Options.GuaranteedTailCallOpt) {
Evan Chengb2c92902010-02-02 02:22:50 +00002441 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2442 CCValAssign &VA = ArgLocs[i];
2443 if (VA.isRegLoc())
2444 continue;
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00002445 assert(VA.isMemLoc());
Dan Gohmanc9403652010-07-07 15:54:55 +00002446 SDValue Arg = OutVals[i];
Dan Gohman98ca4f22009-08-05 01:29:28 +00002447 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Gordon Henriksen86737662008-01-05 16:56:59 +00002448 // Create frame index.
2449 int32_t Offset = VA.getLocMemOffset()+FPDiff;
Duncan Sands83ec4b62008-06-06 12:08:01 +00002450 uint32_t OpSize = (VA.getLocVT().getSizeInBits()+7)/8;
Evan Chenged2ae132010-07-03 00:40:23 +00002451 FI = MF.getFrameInfo()->CreateFixedObject(OpSize, Offset, true);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002452 FIN = DAG.getFrameIndex(FI, getPointerTy());
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00002453
Duncan Sands276dcbd2008-03-21 09:14:45 +00002454 if (Flags.isByVal()) {
Evan Cheng8e5712b2008-01-12 01:08:07 +00002455 // Copy relative to framepointer.
Dan Gohman475871a2008-07-27 21:46:04 +00002456 SDValue Source = DAG.getIntPtrConstant(VA.getLocMemOffset());
Gabor Greifba36cb52008-08-28 21:40:38 +00002457 if (StackPtr.getNode() == 0)
Michael Liaoc5c970e2012-10-31 04:14:09 +00002458 StackPtr = DAG.getCopyFromReg(Chain, dl,
2459 RegInfo->getStackRegister(),
Dale Johannesendd64c412009-02-04 00:33:20 +00002460 getPointerTy());
Dale Johannesenace16102009-02-03 19:33:06 +00002461 Source = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, Source);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002462
Dan Gohman98ca4f22009-08-05 01:29:28 +00002463 MemOpChains2.push_back(CreateCopyOfByValArgument(Source, FIN,
2464 ArgChain,
Dale Johannesendd64c412009-02-04 00:33:20 +00002465 Flags, DAG, dl));
Gordon Henriksen86737662008-01-05 16:56:59 +00002466 } else {
Evan Cheng8e5712b2008-01-12 01:08:07 +00002467 // Store relative to framepointer.
Dan Gohman69de1932008-02-06 22:27:42 +00002468 MemOpChains2.push_back(
Dan Gohman98ca4f22009-08-05 01:29:28 +00002469 DAG.getStore(ArgChain, dl, Arg, FIN,
Chris Lattnere8639032010-09-21 06:22:23 +00002470 MachinePointerInfo::getFixedStack(FI),
David Greene67c9d422010-02-15 16:53:33 +00002471 false, false, 0));
Scott Michelfdc40a02009-02-17 22:15:04 +00002472 }
Gordon Henriksen86737662008-01-05 16:56:59 +00002473 }
2474 }
2475
2476 if (!MemOpChains2.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00002477 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Arnold Schwaighofer719eb022008-01-11 14:34:56 +00002478 &MemOpChains2[0], MemOpChains2.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00002479
2480 // Store the return address to the appropriate stack slot.
Michael Liaoaa3c2c02012-10-25 06:29:14 +00002481 Chain = EmitTailCallStoreRetAddr(DAG, MF, Chain, RetAddrFrIdx,
2482 getPointerTy(), RegInfo->getSlotSize(),
Dale Johannesenace16102009-02-03 19:33:06 +00002483 FPDiff, dl);
Gordon Henriksen86737662008-01-05 16:56:59 +00002484 }
2485
Jakob Stoklund Olesenb8720782012-07-04 19:28:31 +00002486 // Build a sequence of copy-to-reg nodes chained together with token chain
2487 // and flag operands which copy the outgoing args into registers.
2488 SDValue InFlag;
2489 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
2490 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
2491 RegsToPass[i].second, InFlag);
2492 InFlag = Chain.getValue(1);
2493 }
2494
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002495 if (getTargetMachine().getCodeModel() == CodeModel::Large) {
2496 assert(Is64Bit && "Large code model is only legal in 64-bit mode.");
2497 // In the 64-bit large code model, we have to make all calls
2498 // through a register, since the call instruction's 32-bit
2499 // pc-relative offset may not be large enough to hold the whole
2500 // address.
2501 } else if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002502 // If the callee is a GlobalAddress node (quite common, every direct call
2503 // is) turn it into a TargetGlobalAddress node so that legalize doesn't hack
2504 // it.
2505
Anton Korobeynikov2b2bc682006-12-22 22:29:05 +00002506 // We should use extra load for direct calls to dllimported functions in
2507 // non-JIT mode.
Dan Gohman46510a72010-04-15 01:51:59 +00002508 const GlobalValue *GV = G->getGlobal();
Chris Lattner754b7652009-07-10 05:48:03 +00002509 if (!GV->hasDLLImportLinkage()) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002510 unsigned char OpFlags = 0;
John McCall3a3465b2011-06-15 20:36:13 +00002511 bool ExtraLoad = false;
2512 unsigned WrapperKind = ISD::DELETED_NODE;
Eric Christopherfd179292009-08-27 18:07:15 +00002513
Chris Lattner48a7d022009-07-09 05:02:21 +00002514 // On ELF targets, in both X86-64 and X86-32 mode, direct calls to
2515 // external symbols most go through the PLT in PIC mode. If the symbol
2516 // has hidden or protected visibility, or if it is static or local, then
2517 // we don't need to use the PLT - we can directly call it.
2518 if (Subtarget->isTargetELF() &&
2519 getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
Chris Lattner74e726e2009-07-09 05:27:35 +00002520 GV->hasDefaultVisibility() && !GV->hasLocalLinkage()) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002521 OpFlags = X86II::MO_PLT;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00002522 } else if (Subtarget->isPICStyleStubAny() &&
Chris Lattner80945782010-09-27 06:34:01 +00002523 (GV->isDeclaration() || GV->isWeakForLinker()) &&
Daniel Dunbar558692f2011-04-20 00:14:25 +00002524 (!Subtarget->getTargetTriple().isMacOSX() ||
2525 Subtarget->getTargetTriple().isMacOSXVersionLT(10, 5))) {
Chris Lattner74e726e2009-07-09 05:27:35 +00002526 // PC-relative references to external symbols should go through $stub,
2527 // unless we're building with the leopard linker or later, which
2528 // automatically synthesizes these stubs.
2529 OpFlags = X86II::MO_DARWIN_STUB;
John McCall3a3465b2011-06-15 20:36:13 +00002530 } else if (Subtarget->isPICStyleRIPRel() &&
2531 isa<Function>(GV) &&
Bill Wendling67658342012-10-09 07:45:08 +00002532 cast<Function>(GV)->getFnAttributes().
2533 hasAttribute(Attributes::NonLazyBind)) {
John McCall3a3465b2011-06-15 20:36:13 +00002534 // If the function is marked as non-lazy, generate an indirect call
2535 // which loads from the GOT directly. This avoids runtime overhead
2536 // at the cost of eager binding (and one extra byte of encoding).
2537 OpFlags = X86II::MO_GOTPCREL;
2538 WrapperKind = X86ISD::WrapperRIP;
2539 ExtraLoad = true;
Chris Lattner74e726e2009-07-09 05:27:35 +00002540 }
Chris Lattner48a7d022009-07-09 05:02:21 +00002541
Devang Patel0d881da2010-07-06 22:08:15 +00002542 Callee = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(),
Chris Lattner48a7d022009-07-09 05:02:21 +00002543 G->getOffset(), OpFlags);
John McCall3a3465b2011-06-15 20:36:13 +00002544
2545 // Add a wrapper if needed.
2546 if (WrapperKind != ISD::DELETED_NODE)
2547 Callee = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Callee);
2548 // Add extra indirection if needed.
2549 if (ExtraLoad)
2550 Callee = DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(), Callee,
2551 MachinePointerInfo::getGOT(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002552 false, false, false, 0);
Chris Lattner48a7d022009-07-09 05:02:21 +00002553 }
Bill Wendling056292f2008-09-16 21:48:12 +00002554 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002555 unsigned char OpFlags = 0;
2556
Evan Cheng1bf891a2010-12-01 22:59:46 +00002557 // On ELF targets, in either X86-64 or X86-32 mode, direct calls to
2558 // external symbols should go through the PLT.
2559 if (Subtarget->isTargetELF() &&
2560 getTargetMachine().getRelocationModel() == Reloc::PIC_) {
2561 OpFlags = X86II::MO_PLT;
2562 } else if (Subtarget->isPICStyleStubAny() &&
Daniel Dunbar558692f2011-04-20 00:14:25 +00002563 (!Subtarget->getTargetTriple().isMacOSX() ||
2564 Subtarget->getTargetTriple().isMacOSXVersionLT(10, 5))) {
Evan Cheng1bf891a2010-12-01 22:59:46 +00002565 // PC-relative references to external symbols should go through $stub,
2566 // unless we're building with the leopard linker or later, which
2567 // automatically synthesizes these stubs.
2568 OpFlags = X86II::MO_DARWIN_STUB;
Chris Lattner74e726e2009-07-09 05:27:35 +00002569 }
Eric Christopherfd179292009-08-27 18:07:15 +00002570
Chris Lattner48a7d022009-07-09 05:02:21 +00002571 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy(),
2572 OpFlags);
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002573 }
2574
Chris Lattnerd96d0722007-02-25 06:40:16 +00002575 // Returns a chain & a flag for retval copy to use.
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002576 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Dan Gohman475871a2008-07-27 21:46:04 +00002577 SmallVector<SDValue, 8> Ops;
Gordon Henriksen86737662008-01-05 16:56:59 +00002578
Evan Chengf22f9b32010-02-06 03:28:46 +00002579 if (!IsSibcall && isTailCall) {
Dale Johannesene8d72302009-02-06 23:05:02 +00002580 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
2581 DAG.getIntPtrConstant(0, true), InFlag);
Gordon Henriksen86737662008-01-05 16:56:59 +00002582 InFlag = Chain.getValue(1);
Gordon Henriksen86737662008-01-05 16:56:59 +00002583 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002584
Nate Begeman4c5dcf52006-02-17 00:03:04 +00002585 Ops.push_back(Chain);
2586 Ops.push_back(Callee);
Evan Chengb69d1132006-06-14 18:17:40 +00002587
Dan Gohman98ca4f22009-08-05 01:29:28 +00002588 if (isTailCall)
Owen Anderson825b72b2009-08-11 20:47:22 +00002589 Ops.push_back(DAG.getConstant(FPDiff, MVT::i32));
Evan Chengf4684712007-02-21 21:18:14 +00002590
Gordon Henriksen86737662008-01-05 16:56:59 +00002591 // Add argument registers to the end of the list so that they are known live
2592 // into the call.
Evan Cheng9b449442008-01-07 23:08:23 +00002593 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
2594 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
2595 RegsToPass[i].second.getValueType()));
Scott Michelfdc40a02009-02-17 22:15:04 +00002596
Jakob Stoklund Olesen8bcde2a2012-02-16 00:02:50 +00002597 // Add a register mask operand representing the call-preserved registers.
2598 const TargetRegisterInfo *TRI = getTargetMachine().getRegisterInfo();
2599 const uint32_t *Mask = TRI->getCallPreservedMask(CallConv);
2600 assert(Mask && "Missing call preserved mask for calling convention");
2601 Ops.push_back(DAG.getRegisterMask(Mask));
Jakob Stoklund Olesenc38c4562012-01-18 23:52:22 +00002602
Gabor Greifba36cb52008-08-28 21:40:38 +00002603 if (InFlag.getNode())
Evan Cheng347d5f72006-04-28 21:29:37 +00002604 Ops.push_back(InFlag);
Gordon Henriksenae636f82008-01-03 16:47:34 +00002605
Dan Gohman98ca4f22009-08-05 01:29:28 +00002606 if (isTailCall) {
Dale Johannesen88004c22010-06-05 00:30:45 +00002607 // We used to do:
2608 //// If this is the first return lowered for this function, add the regs
2609 //// to the liveout set for the function.
2610 // This isn't right, although it's probably harmless on x86; liveouts
2611 // should be computed from returns not tail calls. Consider a void
2612 // function making a tail call to a function returning int.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002613 return DAG.getNode(X86ISD::TC_RETURN, dl,
2614 NodeTys, &Ops[0], Ops.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00002615 }
2616
Dale Johannesenace16102009-02-03 19:33:06 +00002617 Chain = DAG.getNode(X86ISD::CALL, dl, NodeTys, &Ops[0], Ops.size());
Evan Cheng347d5f72006-04-28 21:29:37 +00002618 InFlag = Chain.getValue(1);
Evan Chengd90eb7f2006-01-05 00:27:02 +00002619
Chris Lattner2d297092006-05-23 18:50:38 +00002620 // Create the CALLSEQ_END node.
Gordon Henriksen86737662008-01-05 16:56:59 +00002621 unsigned NumBytesForCalleeToPush;
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002622 if (X86::isCalleePop(CallConv, Is64Bit, isVarArg,
2623 getTargetMachine().Options.GuaranteedTailCallOpt))
Gordon Henriksen86737662008-01-05 16:56:59 +00002624 NumBytesForCalleeToPush = NumBytes; // Callee pops everything
Eli Friedman9a2478a2012-01-20 00:05:46 +00002625 else if (!Is64Bit && !IsTailCallConvention(CallConv) && !IsWindows &&
Rafael Espindola1cee7102012-07-25 13:41:10 +00002626 SR == StackStructReturn)
Dan Gohmanf451cb82010-02-10 16:03:48 +00002627 // If this is a call to a struct-return function, the callee
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00002628 // pops the hidden struct pointer, so we have to push it back.
2629 // This is common for Darwin/X86, Linux & Mingw32 targets.
Eli Friedman9a2478a2012-01-20 00:05:46 +00002630 // For MSVC Win32 targets, the caller pops the hidden struct pointer.
Gordon Henriksenae636f82008-01-03 16:47:34 +00002631 NumBytesForCalleeToPush = 4;
Gordon Henriksen86737662008-01-05 16:56:59 +00002632 else
Gordon Henriksenae636f82008-01-03 16:47:34 +00002633 NumBytesForCalleeToPush = 0; // Callee pops nothing.
Scott Michelfdc40a02009-02-17 22:15:04 +00002634
Gordon Henriksenae636f82008-01-03 16:47:34 +00002635 // Returns a flag for retval copy to use.
Evan Chengf22f9b32010-02-06 03:28:46 +00002636 if (!IsSibcall) {
2637 Chain = DAG.getCALLSEQ_END(Chain,
2638 DAG.getIntPtrConstant(NumBytes, true),
2639 DAG.getIntPtrConstant(NumBytesForCalleeToPush,
2640 true),
2641 InFlag);
2642 InFlag = Chain.getValue(1);
2643 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00002644
Chris Lattner3085e152007-02-25 08:59:22 +00002645 // Handle result values, copying them out of physregs into vregs that we
2646 // return.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002647 return LowerCallResult(Chain, InFlag, CallConv, isVarArg,
2648 Ins, dl, DAG, InVals);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002649}
2650
Evan Cheng25ab6902006-09-08 06:48:29 +00002651
2652//===----------------------------------------------------------------------===//
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002653// Fast Calling Convention (tail call) implementation
2654//===----------------------------------------------------------------------===//
2655
2656// Like std call, callee cleans arguments, convention except that ECX is
2657// reserved for storing the tail called function address. Only 2 registers are
2658// free for argument passing (inreg). Tail call optimization is performed
2659// provided:
2660// * tailcallopt is enabled
2661// * caller/callee are fastcc
Arnold Schwaighofera2a4b472008-02-26 10:21:54 +00002662// On X86_64 architecture with GOT-style position independent code only local
2663// (within module) calls are supported at the moment.
Arnold Schwaighofer48abc5c2007-10-12 21:30:57 +00002664// To keep the stack aligned according to platform abi the function
2665// GetAlignedArgumentStackSize ensures that argument delta is always multiples
2666// of stack alignment. (Dynamic linkers need this - darwin's dyld for example)
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002667// If a tail called function callee has more arguments than the caller the
2668// caller needs to make sure that there is room to move the RETADDR to. This is
Arnold Schwaighofer48abc5c2007-10-12 21:30:57 +00002669// achieved by reserving an area the size of the argument delta right after the
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002670// original REtADDR, but before the saved framepointer or the spilled registers
2671// e.g. caller(arg1, arg2) calls callee(arg1, arg2,arg3,arg4)
2672// stack layout:
2673// arg1
2674// arg2
2675// RETADDR
Scott Michelfdc40a02009-02-17 22:15:04 +00002676// [ new RETADDR
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002677// move area ]
2678// (possible EBP)
2679// ESI
2680// EDI
2681// local1 ..
2682
2683/// GetAlignedArgumentStackSize - Make the stack size align e.g 16n + 12 aligned
2684/// for a 16 byte align requirement.
Dan Gohmand858e902010-04-17 15:26:15 +00002685unsigned
2686X86TargetLowering::GetAlignedArgumentStackSize(unsigned StackSize,
2687 SelectionDAG& DAG) const {
Evan Chenge9ac9e62008-09-07 09:07:23 +00002688 MachineFunction &MF = DAG.getMachineFunction();
2689 const TargetMachine &TM = MF.getTarget();
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00002690 const TargetFrameLowering &TFI = *TM.getFrameLowering();
Evan Chenge9ac9e62008-09-07 09:07:23 +00002691 unsigned StackAlignment = TFI.getStackAlignment();
Scott Michelfdc40a02009-02-17 22:15:04 +00002692 uint64_t AlignMask = StackAlignment - 1;
Evan Chenge9ac9e62008-09-07 09:07:23 +00002693 int64_t Offset = StackSize;
Michael Liaoaa3c2c02012-10-25 06:29:14 +00002694 unsigned SlotSize = RegInfo->getSlotSize();
Evan Chenge9ac9e62008-09-07 09:07:23 +00002695 if ( (Offset & AlignMask) <= (StackAlignment - SlotSize) ) {
2696 // Number smaller than 12 so just add the difference.
2697 Offset += ((StackAlignment - SlotSize) - (Offset & AlignMask));
2698 } else {
2699 // Mask out lower bits, add stackalignment once plus the 12 bytes.
Scott Michelfdc40a02009-02-17 22:15:04 +00002700 Offset = ((~AlignMask) & Offset) + StackAlignment +
Evan Chenge9ac9e62008-09-07 09:07:23 +00002701 (StackAlignment-SlotSize);
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002702 }
Evan Chenge9ac9e62008-09-07 09:07:23 +00002703 return Offset;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002704}
2705
Evan Cheng5f941932010-02-05 02:21:12 +00002706/// MatchingStackOffset - Return true if the given stack call argument is
2707/// already available in the same position (relatively) of the caller's
2708/// incoming argument stack.
2709static
2710bool MatchingStackOffset(SDValue Arg, unsigned Offset, ISD::ArgFlagsTy Flags,
2711 MachineFrameInfo *MFI, const MachineRegisterInfo *MRI,
2712 const X86InstrInfo *TII) {
Evan Cheng4cae1332010-03-05 08:38:04 +00002713 unsigned Bytes = Arg.getValueType().getSizeInBits() / 8;
2714 int FI = INT_MAX;
Evan Cheng5f941932010-02-05 02:21:12 +00002715 if (Arg.getOpcode() == ISD::CopyFromReg) {
2716 unsigned VR = cast<RegisterSDNode>(Arg.getOperand(1))->getReg();
Jakob Stoklund Olesenc9df0252011-01-10 02:58:51 +00002717 if (!TargetRegisterInfo::isVirtualRegister(VR))
Evan Cheng5f941932010-02-05 02:21:12 +00002718 return false;
2719 MachineInstr *Def = MRI->getVRegDef(VR);
2720 if (!Def)
2721 return false;
2722 if (!Flags.isByVal()) {
2723 if (!TII->isLoadFromStackSlot(Def, FI))
2724 return false;
2725 } else {
2726 unsigned Opcode = Def->getOpcode();
2727 if ((Opcode == X86::LEA32r || Opcode == X86::LEA64r) &&
2728 Def->getOperand(1).isFI()) {
2729 FI = Def->getOperand(1).getIndex();
Evan Cheng4cae1332010-03-05 08:38:04 +00002730 Bytes = Flags.getByValSize();
Evan Cheng5f941932010-02-05 02:21:12 +00002731 } else
2732 return false;
2733 }
Evan Cheng4cae1332010-03-05 08:38:04 +00002734 } else if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Arg)) {
2735 if (Flags.isByVal())
2736 // ByVal argument is passed in as a pointer but it's now being
Evan Cheng10718492010-03-05 19:55:55 +00002737 // dereferenced. e.g.
Evan Cheng4cae1332010-03-05 08:38:04 +00002738 // define @foo(%struct.X* %A) {
2739 // tail call @bar(%struct.X* byval %A)
2740 // }
Evan Cheng5f941932010-02-05 02:21:12 +00002741 return false;
2742 SDValue Ptr = Ld->getBasePtr();
2743 FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr);
2744 if (!FINode)
2745 return false;
2746 FI = FINode->getIndex();
Chad Rosierdf78fcd2011-06-25 02:04:56 +00002747 } else if (Arg.getOpcode() == ISD::FrameIndex && Flags.isByVal()) {
Chad Rosier14d71aa2011-06-25 18:51:28 +00002748 FrameIndexSDNode *FINode = cast<FrameIndexSDNode>(Arg);
Chad Rosierdf78fcd2011-06-25 02:04:56 +00002749 FI = FINode->getIndex();
2750 Bytes = Flags.getByValSize();
Evan Cheng4cae1332010-03-05 08:38:04 +00002751 } else
2752 return false;
Evan Cheng5f941932010-02-05 02:21:12 +00002753
Evan Cheng4cae1332010-03-05 08:38:04 +00002754 assert(FI != INT_MAX);
Evan Cheng5f941932010-02-05 02:21:12 +00002755 if (!MFI->isFixedObjectIndex(FI))
2756 return false;
Evan Cheng4cae1332010-03-05 08:38:04 +00002757 return Offset == MFI->getObjectOffset(FI) && Bytes == MFI->getObjectSize(FI);
Evan Cheng5f941932010-02-05 02:21:12 +00002758}
2759
Dan Gohman98ca4f22009-08-05 01:29:28 +00002760/// IsEligibleForTailCallOptimization - Check whether the call is eligible
2761/// for tail call optimization. Targets which want to do tail call
2762/// optimization should implement this function.
2763bool
2764X86TargetLowering::IsEligibleForTailCallOptimization(SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00002765 CallingConv::ID CalleeCC,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002766 bool isVarArg,
Evan Chenga375d472010-03-15 18:54:48 +00002767 bool isCalleeStructRet,
2768 bool isCallerStructRet,
Evan Chengb1cacc72012-09-25 05:32:34 +00002769 Type *RetTy,
Evan Chengb1712452010-01-27 06:25:16 +00002770 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00002771 const SmallVectorImpl<SDValue> &OutVals,
Evan Chengb1712452010-01-27 06:25:16 +00002772 const SmallVectorImpl<ISD::InputArg> &Ins,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002773 SelectionDAG& DAG) const {
Chris Lattner29689432010-03-11 00:22:57 +00002774 if (!IsTailCallConvention(CalleeCC) &&
Evan Chengb1712452010-01-27 06:25:16 +00002775 CalleeCC != CallingConv::C)
2776 return false;
2777
Evan Cheng7096ae42010-01-29 06:45:59 +00002778 // If -tailcallopt is specified, make fastcc functions tail-callable.
Evan Cheng2c12cb42010-03-26 16:26:03 +00002779 const MachineFunction &MF = DAG.getMachineFunction();
Evan Cheng7096ae42010-01-29 06:45:59 +00002780 const Function *CallerF = DAG.getMachineFunction().getFunction();
Evan Chengb1cacc72012-09-25 05:32:34 +00002781
2782 // If the function return type is x86_fp80 and the callee return type is not,
2783 // then the FP_EXTEND of the call result is not a nop. It's not safe to
2784 // perform a tailcall optimization here.
2785 if (CallerF->getReturnType()->isX86_FP80Ty() && !RetTy->isX86_FP80Ty())
2786 return false;
2787
Evan Cheng13617962010-04-30 01:12:32 +00002788 CallingConv::ID CallerCC = CallerF->getCallingConv();
2789 bool CCMatch = CallerCC == CalleeCC;
2790
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002791 if (getTargetMachine().Options.GuaranteedTailCallOpt) {
Evan Cheng13617962010-04-30 01:12:32 +00002792 if (IsTailCallConvention(CalleeCC) && CCMatch)
Evan Cheng843bd692010-01-31 06:44:49 +00002793 return true;
2794 return false;
2795 }
2796
Dale Johannesen2f05cc02010-05-28 23:24:28 +00002797 // Look for obvious safe cases to perform tail call optimization that do not
2798 // require ABI changes. This is what gcc calls sibcall.
Evan Chengb2c92902010-02-02 02:22:50 +00002799
Evan Cheng2c12cb42010-03-26 16:26:03 +00002800 // Can't do sibcall if stack needs to be dynamically re-aligned. PEI needs to
2801 // emit a special epilogue.
2802 if (RegInfo->needsStackRealignment(MF))
2803 return false;
2804
Evan Chenga375d472010-03-15 18:54:48 +00002805 // Also avoid sibcall optimization if either caller or callee uses struct
2806 // return semantics.
2807 if (isCalleeStructRet || isCallerStructRet)
2808 return false;
2809
Chad Rosier2416da32011-06-24 21:15:36 +00002810 // An stdcall caller is expected to clean up its arguments; the callee
2811 // isn't going to do that.
2812 if (!CCMatch && CallerCC==CallingConv::X86_StdCall)
2813 return false;
2814
Chad Rosier871f6642011-05-18 19:59:50 +00002815 // Do not sibcall optimize vararg calls unless all arguments are passed via
Chad Rosiera1660892011-05-20 00:59:28 +00002816 // registers.
Chad Rosier871f6642011-05-18 19:59:50 +00002817 if (isVarArg && !Outs.empty()) {
Chad Rosiera1660892011-05-20 00:59:28 +00002818
2819 // Optimizing for varargs on Win64 is unlikely to be safe without
2820 // additional testing.
2821 if (Subtarget->isTargetWin64())
2822 return false;
2823
Chad Rosier871f6642011-05-18 19:59:50 +00002824 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002825 CCState CCInfo(CalleeCC, isVarArg, DAG.getMachineFunction(),
Craig Topper0fbf3642012-04-23 03:28:34 +00002826 getTargetMachine(), ArgLocs, *DAG.getContext());
Chad Rosier871f6642011-05-18 19:59:50 +00002827
Chad Rosier871f6642011-05-18 19:59:50 +00002828 CCInfo.AnalyzeCallOperands(Outs, CC_X86);
2829 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i)
2830 if (!ArgLocs[i].isRegLoc())
2831 return false;
2832 }
2833
Chad Rosier30450e82011-12-22 22:35:21 +00002834 // If the call result is in ST0 / ST1, it needs to be popped off the x87
2835 // stack. Therefore, if it's not used by the call it is not safe to optimize
2836 // this into a sibcall.
Evan Chengf5b9d6c2010-03-20 02:58:15 +00002837 bool Unused = false;
2838 for (unsigned i = 0, e = Ins.size(); i != e; ++i) {
2839 if (!Ins[i].Used) {
2840 Unused = true;
2841 break;
2842 }
2843 }
2844 if (Unused) {
2845 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002846 CCState CCInfo(CalleeCC, false, DAG.getMachineFunction(),
Craig Topper0fbf3642012-04-23 03:28:34 +00002847 getTargetMachine(), RVLocs, *DAG.getContext());
Evan Chengf5b9d6c2010-03-20 02:58:15 +00002848 CCInfo.AnalyzeCallResult(Ins, RetCC_X86);
Evan Cheng13617962010-04-30 01:12:32 +00002849 for (unsigned i = 0, e = RVLocs.size(); i != e; ++i) {
Evan Chengf5b9d6c2010-03-20 02:58:15 +00002850 CCValAssign &VA = RVLocs[i];
2851 if (VA.getLocReg() == X86::ST0 || VA.getLocReg() == X86::ST1)
2852 return false;
2853 }
2854 }
2855
Evan Cheng13617962010-04-30 01:12:32 +00002856 // If the calling conventions do not match, then we'd better make sure the
2857 // results are returned in the same way as what the caller expects.
2858 if (!CCMatch) {
2859 SmallVector<CCValAssign, 16> RVLocs1;
Eric Christopher471e4222011-06-08 23:55:35 +00002860 CCState CCInfo1(CalleeCC, false, DAG.getMachineFunction(),
Craig Topper0fbf3642012-04-23 03:28:34 +00002861 getTargetMachine(), RVLocs1, *DAG.getContext());
Evan Cheng13617962010-04-30 01:12:32 +00002862 CCInfo1.AnalyzeCallResult(Ins, RetCC_X86);
2863
2864 SmallVector<CCValAssign, 16> RVLocs2;
Eric Christopher471e4222011-06-08 23:55:35 +00002865 CCState CCInfo2(CallerCC, false, DAG.getMachineFunction(),
Craig Topper0fbf3642012-04-23 03:28:34 +00002866 getTargetMachine(), RVLocs2, *DAG.getContext());
Evan Cheng13617962010-04-30 01:12:32 +00002867 CCInfo2.AnalyzeCallResult(Ins, RetCC_X86);
2868
2869 if (RVLocs1.size() != RVLocs2.size())
2870 return false;
2871 for (unsigned i = 0, e = RVLocs1.size(); i != e; ++i) {
2872 if (RVLocs1[i].isRegLoc() != RVLocs2[i].isRegLoc())
2873 return false;
2874 if (RVLocs1[i].getLocInfo() != RVLocs2[i].getLocInfo())
2875 return false;
2876 if (RVLocs1[i].isRegLoc()) {
2877 if (RVLocs1[i].getLocReg() != RVLocs2[i].getLocReg())
2878 return false;
2879 } else {
2880 if (RVLocs1[i].getLocMemOffset() != RVLocs2[i].getLocMemOffset())
2881 return false;
2882 }
2883 }
2884 }
2885
Evan Chenga6bff982010-01-30 01:22:00 +00002886 // If the callee takes no arguments then go on to check the results of the
2887 // call.
2888 if (!Outs.empty()) {
2889 // Check if stack adjustment is needed. For now, do not do this if any
2890 // argument is passed on the stack.
2891 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002892 CCState CCInfo(CalleeCC, isVarArg, DAG.getMachineFunction(),
Craig Topper0fbf3642012-04-23 03:28:34 +00002893 getTargetMachine(), ArgLocs, *DAG.getContext());
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00002894
2895 // Allocate shadow area for Win64
2896 if (Subtarget->isTargetWin64()) {
2897 CCInfo.AllocateStack(32, 8);
2898 }
2899
Duncan Sands45907662010-10-31 13:21:44 +00002900 CCInfo.AnalyzeCallOperands(Outs, CC_X86);
Stuart Hastings6db2c2f2011-05-17 16:59:46 +00002901 if (CCInfo.getNextStackOffset()) {
Evan Chengb2c92902010-02-02 02:22:50 +00002902 MachineFunction &MF = DAG.getMachineFunction();
2903 if (MF.getInfo<X86MachineFunctionInfo>()->getBytesToPopOnReturn())
2904 return false;
Evan Chengb2c92902010-02-02 02:22:50 +00002905
2906 // Check if the arguments are already laid out in the right way as
2907 // the caller's fixed stack objects.
2908 MachineFrameInfo *MFI = MF.getFrameInfo();
Evan Cheng5f941932010-02-05 02:21:12 +00002909 const MachineRegisterInfo *MRI = &MF.getRegInfo();
2910 const X86InstrInfo *TII =
Roman Divacky59324292012-09-05 22:26:57 +00002911 ((const X86TargetMachine&)getTargetMachine()).getInstrInfo();
Evan Chengb2c92902010-02-02 02:22:50 +00002912 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2913 CCValAssign &VA = ArgLocs[i];
Dan Gohmanc9403652010-07-07 15:54:55 +00002914 SDValue Arg = OutVals[i];
Evan Chengb2c92902010-02-02 02:22:50 +00002915 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Evan Chengb2c92902010-02-02 02:22:50 +00002916 if (VA.getLocInfo() == CCValAssign::Indirect)
2917 return false;
2918 if (!VA.isRegLoc()) {
Evan Cheng5f941932010-02-05 02:21:12 +00002919 if (!MatchingStackOffset(Arg, VA.getLocMemOffset(), Flags,
2920 MFI, MRI, TII))
Evan Chengb2c92902010-02-02 02:22:50 +00002921 return false;
2922 }
2923 }
2924 }
Evan Cheng9c044672010-05-29 01:35:22 +00002925
2926 // If the tailcall address may be in a register, then make sure it's
2927 // possible to register allocate for it. In 32-bit, the call address can
2928 // only target EAX, EDX, or ECX since the tail call must be scheduled after
Evan Chengdedd9742010-07-14 06:44:01 +00002929 // callee-saved registers are restored. These happen to be the same
2930 // registers used to pass 'inreg' arguments so watch out for those.
2931 if (!Subtarget->is64Bit() &&
2932 !isa<GlobalAddressSDNode>(Callee) &&
Evan Cheng9c044672010-05-29 01:35:22 +00002933 !isa<ExternalSymbolSDNode>(Callee)) {
Evan Cheng9c044672010-05-29 01:35:22 +00002934 unsigned NumInRegs = 0;
2935 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2936 CCValAssign &VA = ArgLocs[i];
Evan Chengdedd9742010-07-14 06:44:01 +00002937 if (!VA.isRegLoc())
2938 continue;
2939 unsigned Reg = VA.getLocReg();
2940 switch (Reg) {
2941 default: break;
2942 case X86::EAX: case X86::EDX: case X86::ECX:
2943 if (++NumInRegs == 3)
Evan Cheng9c044672010-05-29 01:35:22 +00002944 return false;
Evan Chengdedd9742010-07-14 06:44:01 +00002945 break;
Evan Cheng9c044672010-05-29 01:35:22 +00002946 }
2947 }
2948 }
Evan Chenga6bff982010-01-30 01:22:00 +00002949 }
Evan Chengb1712452010-01-27 06:25:16 +00002950
Evan Cheng86809cc2010-02-03 03:28:02 +00002951 return true;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002952}
2953
Dan Gohman3df24e62008-09-03 23:12:08 +00002954FastISel *
Bob Wilsond49edb72012-08-03 04:06:28 +00002955X86TargetLowering::createFastISel(FunctionLoweringInfo &funcInfo,
2956 const TargetLibraryInfo *libInfo) const {
2957 return X86::createFastISel(funcInfo, libInfo);
Dan Gohmand9f3c482008-08-19 21:32:53 +00002958}
2959
2960
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00002961//===----------------------------------------------------------------------===//
2962// Other Lowering Hooks
2963//===----------------------------------------------------------------------===//
2964
Bruno Cardoso Lopese654b562010-09-01 00:51:36 +00002965static bool MayFoldLoad(SDValue Op) {
2966 return Op.hasOneUse() && ISD::isNormalLoad(Op.getNode());
2967}
2968
2969static bool MayFoldIntoStore(SDValue Op) {
2970 return Op.hasOneUse() && ISD::isNormalStore(*Op.getNode()->use_begin());
2971}
2972
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002973static bool isTargetShuffle(unsigned Opcode) {
2974 switch(Opcode) {
2975 default: return false;
2976 case X86ISD::PSHUFD:
2977 case X86ISD::PSHUFHW:
2978 case X86ISD::PSHUFLW:
Craig Topperb3982da2011-12-31 23:50:21 +00002979 case X86ISD::SHUFP:
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00002980 case X86ISD::PALIGN:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002981 case X86ISD::MOVLHPS:
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00002982 case X86ISD::MOVLHPD:
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00002983 case X86ISD::MOVHLPS:
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00002984 case X86ISD::MOVLPS:
2985 case X86ISD::MOVLPD:
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00002986 case X86ISD::MOVSHDUP:
Bruno Cardoso Lopes013bb3d2010-08-31 22:35:05 +00002987 case X86ISD::MOVSLDUP:
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00002988 case X86ISD::MOVDDUP:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002989 case X86ISD::MOVSS:
2990 case X86ISD::MOVSD:
Craig Topper34671b82011-12-06 08:21:25 +00002991 case X86ISD::UNPCKL:
2992 case X86ISD::UNPCKH:
Craig Topper316cd2a2011-11-30 06:25:25 +00002993 case X86ISD::VPERMILP:
Craig Topperec24e612011-11-30 07:47:51 +00002994 case X86ISD::VPERM2X128:
Craig Topperbdcbcb32012-05-06 18:54:26 +00002995 case X86ISD::VPERMI:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002996 return true;
2997 }
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002998}
2999
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00003000static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
Craig Topper3d092db2012-03-21 02:14:01 +00003001 SDValue V1, SelectionDAG &DAG) {
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00003002 switch(Opc) {
3003 default: llvm_unreachable("Unknown x86 shuffle node");
3004 case X86ISD::MOVSHDUP:
Bruno Cardoso Lopes013bb3d2010-08-31 22:35:05 +00003005 case X86ISD::MOVSLDUP:
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00003006 case X86ISD::MOVDDUP:
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00003007 return DAG.getNode(Opc, dl, VT, V1);
3008 }
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00003009}
3010
3011static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
Craig Topper3d092db2012-03-21 02:14:01 +00003012 SDValue V1, unsigned TargetMask,
3013 SelectionDAG &DAG) {
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00003014 switch(Opc) {
3015 default: llvm_unreachable("Unknown x86 shuffle node");
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00003016 case X86ISD::PSHUFD:
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00003017 case X86ISD::PSHUFHW:
3018 case X86ISD::PSHUFLW:
Craig Topper316cd2a2011-11-30 06:25:25 +00003019 case X86ISD::VPERMILP:
Craig Topper8325c112012-04-16 00:41:45 +00003020 case X86ISD::VPERMI:
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00003021 return DAG.getNode(Opc, dl, VT, V1, DAG.getConstant(TargetMask, MVT::i8));
3022 }
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00003023}
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00003024
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00003025static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
Craig Topper3d092db2012-03-21 02:14:01 +00003026 SDValue V1, SDValue V2, unsigned TargetMask,
3027 SelectionDAG &DAG) {
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00003028 switch(Opc) {
3029 default: llvm_unreachable("Unknown x86 shuffle node");
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00003030 case X86ISD::PALIGN:
Craig Topperb3982da2011-12-31 23:50:21 +00003031 case X86ISD::SHUFP:
Craig Topperec24e612011-11-30 07:47:51 +00003032 case X86ISD::VPERM2X128:
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00003033 return DAG.getNode(Opc, dl, VT, V1, V2,
3034 DAG.getConstant(TargetMask, MVT::i8));
3035 }
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00003036}
3037
3038static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
3039 SDValue V1, SDValue V2, SelectionDAG &DAG) {
3040 switch(Opc) {
3041 default: llvm_unreachable("Unknown x86 shuffle node");
3042 case X86ISD::MOVLHPS:
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00003043 case X86ISD::MOVLHPD:
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00003044 case X86ISD::MOVHLPS:
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00003045 case X86ISD::MOVLPS:
3046 case X86ISD::MOVLPD:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003047 case X86ISD::MOVSS:
3048 case X86ISD::MOVSD:
Craig Topper34671b82011-12-06 08:21:25 +00003049 case X86ISD::UNPCKL:
3050 case X86ISD::UNPCKH:
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00003051 return DAG.getNode(Opc, dl, VT, V1, V2);
3052 }
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00003053}
3054
Dan Gohmand858e902010-04-17 15:26:15 +00003055SDValue X86TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) const {
Anton Korobeynikova2780e12007-08-15 17:12:32 +00003056 MachineFunction &MF = DAG.getMachineFunction();
3057 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
3058 int ReturnAddrIndex = FuncInfo->getRAIndex();
3059
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00003060 if (ReturnAddrIndex == 0) {
3061 // Set up a frame object for the return address.
Michael Liaoaa3c2c02012-10-25 06:29:14 +00003062 unsigned SlotSize = RegInfo->getSlotSize();
David Greene3f2bf852009-11-12 20:49:22 +00003063 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(SlotSize, -SlotSize,
Evan Chenged2ae132010-07-03 00:40:23 +00003064 false);
Anton Korobeynikova2780e12007-08-15 17:12:32 +00003065 FuncInfo->setRAIndex(ReturnAddrIndex);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00003066 }
3067
Evan Cheng25ab6902006-09-08 06:48:29 +00003068 return DAG.getFrameIndex(ReturnAddrIndex, getPointerTy());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00003069}
3070
3071
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00003072bool X86::isOffsetSuitableForCodeModel(int64_t Offset, CodeModel::Model M,
3073 bool hasSymbolicDisplacement) {
3074 // Offset should fit into 32 bit immediate field.
Benjamin Kramer34247a02010-03-29 21:13:41 +00003075 if (!isInt<32>(Offset))
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00003076 return false;
3077
3078 // If we don't have a symbolic displacement - we don't have any extra
3079 // restrictions.
3080 if (!hasSymbolicDisplacement)
3081 return true;
3082
3083 // FIXME: Some tweaks might be needed for medium code model.
3084 if (M != CodeModel::Small && M != CodeModel::Kernel)
3085 return false;
3086
3087 // For small code model we assume that latest object is 16MB before end of 31
3088 // bits boundary. We may also accept pretty large negative constants knowing
3089 // that all objects are in the positive half of address space.
3090 if (M == CodeModel::Small && Offset < 16*1024*1024)
3091 return true;
3092
3093 // For kernel code model we know that all object resist in the negative half
3094 // of 32bits address space. We may not accept negative offsets, since they may
3095 // be just off and we may accept pretty large positive ones.
3096 if (M == CodeModel::Kernel && Offset > 0)
3097 return true;
3098
3099 return false;
3100}
3101
Evan Chengef41ff62011-06-23 17:54:54 +00003102/// isCalleePop - Determines whether the callee is required to pop its
3103/// own arguments. Callee pop is necessary to support tail calls.
3104bool X86::isCalleePop(CallingConv::ID CallingConv,
3105 bool is64Bit, bool IsVarArg, bool TailCallOpt) {
3106 if (IsVarArg)
3107 return false;
3108
3109 switch (CallingConv) {
3110 default:
3111 return false;
3112 case CallingConv::X86_StdCall:
3113 return !is64Bit;
3114 case CallingConv::X86_FastCall:
3115 return !is64Bit;
3116 case CallingConv::X86_ThisCall:
3117 return !is64Bit;
3118 case CallingConv::Fast:
3119 return TailCallOpt;
3120 case CallingConv::GHC:
3121 return TailCallOpt;
3122 }
3123}
3124
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003125/// TranslateX86CC - do a one to one translation of a ISD::CondCode to the X86
3126/// specific condition code, returning the condition code and the LHS/RHS of the
3127/// comparison to make.
3128static unsigned TranslateX86CC(ISD::CondCode SetCCOpcode, bool isFP,
3129 SDValue &LHS, SDValue &RHS, SelectionDAG &DAG) {
Evan Chengd9558e02006-01-06 00:43:03 +00003130 if (!isFP) {
Chris Lattnerbfd68a72006-09-13 17:04:54 +00003131 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS)) {
3132 if (SetCCOpcode == ISD::SETGT && RHSC->isAllOnesValue()) {
3133 // X > -1 -> X == 0, jump !sign.
3134 RHS = DAG.getConstant(0, RHS.getValueType());
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003135 return X86::COND_NS;
Craig Topper69947b92012-04-23 06:57:04 +00003136 }
3137 if (SetCCOpcode == ISD::SETLT && RHSC->isNullValue()) {
Chris Lattnerbfd68a72006-09-13 17:04:54 +00003138 // X < 0 -> X == 0, jump on sign.
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003139 return X86::COND_S;
Craig Topper69947b92012-04-23 06:57:04 +00003140 }
3141 if (SetCCOpcode == ISD::SETLT && RHSC->getZExtValue() == 1) {
Dan Gohman5f6913c2007-09-17 14:49:27 +00003142 // X < 1 -> X <= 0
3143 RHS = DAG.getConstant(0, RHS.getValueType());
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003144 return X86::COND_LE;
Chris Lattnerbfd68a72006-09-13 17:04:54 +00003145 }
Chris Lattnerf9570512006-09-13 03:22:10 +00003146 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00003147
Evan Chengd9558e02006-01-06 00:43:03 +00003148 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00003149 default: llvm_unreachable("Invalid integer condition!");
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003150 case ISD::SETEQ: return X86::COND_E;
3151 case ISD::SETGT: return X86::COND_G;
3152 case ISD::SETGE: return X86::COND_GE;
3153 case ISD::SETLT: return X86::COND_L;
3154 case ISD::SETLE: return X86::COND_LE;
3155 case ISD::SETNE: return X86::COND_NE;
3156 case ISD::SETULT: return X86::COND_B;
3157 case ISD::SETUGT: return X86::COND_A;
3158 case ISD::SETULE: return X86::COND_BE;
3159 case ISD::SETUGE: return X86::COND_AE;
Evan Chengd9558e02006-01-06 00:43:03 +00003160 }
Chris Lattner4c78e022008-12-23 23:42:27 +00003161 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003162
Chris Lattner4c78e022008-12-23 23:42:27 +00003163 // First determine if it is required or is profitable to flip the operands.
Duncan Sands4047f4a2008-10-24 13:03:10 +00003164
Chris Lattner4c78e022008-12-23 23:42:27 +00003165 // If LHS is a foldable load, but RHS is not, flip the condition.
Rafael Espindolaf297c932011-02-03 03:58:05 +00003166 if (ISD::isNON_EXTLoad(LHS.getNode()) &&
3167 !ISD::isNON_EXTLoad(RHS.getNode())) {
Chris Lattner4c78e022008-12-23 23:42:27 +00003168 SetCCOpcode = getSetCCSwappedOperands(SetCCOpcode);
3169 std::swap(LHS, RHS);
Evan Cheng4d46d0a2008-08-28 23:48:31 +00003170 }
3171
Chris Lattner4c78e022008-12-23 23:42:27 +00003172 switch (SetCCOpcode) {
3173 default: break;
3174 case ISD::SETOLT:
3175 case ISD::SETOLE:
3176 case ISD::SETUGT:
3177 case ISD::SETUGE:
3178 std::swap(LHS, RHS);
3179 break;
3180 }
3181
3182 // On a floating point condition, the flags are set as follows:
3183 // ZF PF CF op
3184 // 0 | 0 | 0 | X > Y
3185 // 0 | 0 | 1 | X < Y
3186 // 1 | 0 | 0 | X == Y
3187 // 1 | 1 | 1 | unordered
3188 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00003189 default: llvm_unreachable("Condcode should be pre-legalized away");
Chris Lattner4c78e022008-12-23 23:42:27 +00003190 case ISD::SETUEQ:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003191 case ISD::SETEQ: return X86::COND_E;
Chris Lattner4c78e022008-12-23 23:42:27 +00003192 case ISD::SETOLT: // flipped
3193 case ISD::SETOGT:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003194 case ISD::SETGT: return X86::COND_A;
Chris Lattner4c78e022008-12-23 23:42:27 +00003195 case ISD::SETOLE: // flipped
3196 case ISD::SETOGE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003197 case ISD::SETGE: return X86::COND_AE;
Chris Lattner4c78e022008-12-23 23:42:27 +00003198 case ISD::SETUGT: // flipped
3199 case ISD::SETULT:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003200 case ISD::SETLT: return X86::COND_B;
Chris Lattner4c78e022008-12-23 23:42:27 +00003201 case ISD::SETUGE: // flipped
3202 case ISD::SETULE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003203 case ISD::SETLE: return X86::COND_BE;
Chris Lattner4c78e022008-12-23 23:42:27 +00003204 case ISD::SETONE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003205 case ISD::SETNE: return X86::COND_NE;
3206 case ISD::SETUO: return X86::COND_P;
3207 case ISD::SETO: return X86::COND_NP;
Dan Gohman1a492952009-10-20 16:22:37 +00003208 case ISD::SETOEQ:
3209 case ISD::SETUNE: return X86::COND_INVALID;
Chris Lattner4c78e022008-12-23 23:42:27 +00003210 }
Evan Chengd9558e02006-01-06 00:43:03 +00003211}
3212
Evan Cheng4a460802006-01-11 00:33:36 +00003213/// hasFPCMov - is there a floating point cmov for the specific X86 condition
3214/// code. Current x86 isa includes the following FP cmov instructions:
Evan Chengaaca22c2006-01-10 20:26:56 +00003215/// fcmovb, fcomvbe, fcomve, fcmovu, fcmovae, fcmova, fcmovne, fcmovnu.
Evan Cheng4a460802006-01-11 00:33:36 +00003216static bool hasFPCMov(unsigned X86CC) {
Evan Chengaaca22c2006-01-10 20:26:56 +00003217 switch (X86CC) {
3218 default:
3219 return false;
Chris Lattner7fbe9722006-10-20 17:42:20 +00003220 case X86::COND_B:
3221 case X86::COND_BE:
3222 case X86::COND_E:
3223 case X86::COND_P:
3224 case X86::COND_A:
3225 case X86::COND_AE:
3226 case X86::COND_NE:
3227 case X86::COND_NP:
Evan Chengaaca22c2006-01-10 20:26:56 +00003228 return true;
3229 }
3230}
3231
Evan Chengeb2f9692009-10-27 19:56:55 +00003232/// isFPImmLegal - Returns true if the target can instruction select the
3233/// specified FP immediate natively. If false, the legalizer will
3234/// materialize the FP immediate as a load from a constant pool.
Evan Chenga1eaa3c2009-10-28 01:43:28 +00003235bool X86TargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
Evan Chengeb2f9692009-10-27 19:56:55 +00003236 for (unsigned i = 0, e = LegalFPImmediates.size(); i != e; ++i) {
3237 if (Imm.bitwiseIsEqual(LegalFPImmediates[i]))
3238 return true;
3239 }
3240 return false;
3241}
3242
Nate Begeman9008ca62009-04-27 18:41:29 +00003243/// isUndefOrInRange - Return true if Val is undef or if its value falls within
3244/// the specified range (L, H].
3245static bool isUndefOrInRange(int Val, int Low, int Hi) {
3246 return (Val < 0) || (Val >= Low && Val < Hi);
3247}
3248
3249/// isUndefOrEqual - Val is either less than zero (undef) or equal to the
3250/// specified value.
3251static bool isUndefOrEqual(int Val, int CmpVal) {
3252 if (Val < 0 || Val == CmpVal)
Evan Cheng5ced1d82006-04-06 23:23:56 +00003253 return true;
Nate Begeman9008ca62009-04-27 18:41:29 +00003254 return false;
Evan Chengc5cdff22006-04-07 21:53:05 +00003255}
3256
Benjamin Kramerd9b0b022012-06-02 10:20:22 +00003257/// isSequentialOrUndefInRange - Return true if every element in Mask, beginning
Bruno Cardoso Lopes4002d7e2011-08-12 21:54:42 +00003258/// from position Pos and ending in Pos+Size, falls within the specified
3259/// sequential range (L, L+Pos]. or is undef.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003260static bool isSequentialOrUndefInRange(ArrayRef<int> Mask,
Craig Topperb6072642012-05-03 07:26:59 +00003261 unsigned Pos, unsigned Size, int Low) {
3262 for (unsigned i = Pos, e = Pos+Size; i != e; ++i, ++Low)
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003263 if (!isUndefOrEqual(Mask[i], Low))
3264 return false;
3265 return true;
3266}
3267
Nate Begeman9008ca62009-04-27 18:41:29 +00003268/// isPSHUFDMask - Return true if the node specifies a shuffle of elements that
3269/// is suitable for input to PSHUFD or PSHUFW. That is, it doesn't reference
3270/// the second operand.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003271static bool isPSHUFDMask(ArrayRef<int> Mask, EVT VT) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00003272 if (VT == MVT::v4f32 || VT == MVT::v4i32 )
Nate Begeman9008ca62009-04-27 18:41:29 +00003273 return (Mask[0] < 4 && Mask[1] < 4 && Mask[2] < 4 && Mask[3] < 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00003274 if (VT == MVT::v2f64 || VT == MVT::v2i64)
Nate Begeman9008ca62009-04-27 18:41:29 +00003275 return (Mask[0] < 2 && Mask[1] < 2);
3276 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003277}
3278
Nate Begeman9008ca62009-04-27 18:41:29 +00003279/// isPSHUFHWMask - Return true if the node specifies a shuffle of elements that
3280/// is suitable for input to PSHUFHW.
Craig Toppera9a568a2012-05-02 08:03:44 +00003281static bool isPSHUFHWMask(ArrayRef<int> Mask, EVT VT, bool HasAVX2) {
3282 if (VT != MVT::v8i16 && (!HasAVX2 || VT != MVT::v16i16))
Evan Cheng0188ecb2006-03-22 18:59:22 +00003283 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003284
Nate Begeman9008ca62009-04-27 18:41:29 +00003285 // Lower quadword copied in order or undef.
Craig Topperc612d792012-01-02 09:17:37 +00003286 if (!isSequentialOrUndefInRange(Mask, 0, 4, 0))
3287 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003288
Evan Cheng506d3df2006-03-29 23:07:14 +00003289 // Upper quadword shuffled.
Craig Topperc612d792012-01-02 09:17:37 +00003290 for (unsigned i = 4; i != 8; ++i)
Craig Toppera9a568a2012-05-02 08:03:44 +00003291 if (!isUndefOrInRange(Mask[i], 4, 8))
Evan Cheng506d3df2006-03-29 23:07:14 +00003292 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003293
Craig Toppera9a568a2012-05-02 08:03:44 +00003294 if (VT == MVT::v16i16) {
3295 // Lower quadword copied in order or undef.
3296 if (!isSequentialOrUndefInRange(Mask, 8, 4, 8))
3297 return false;
3298
3299 // Upper quadword shuffled.
3300 for (unsigned i = 12; i != 16; ++i)
3301 if (!isUndefOrInRange(Mask[i], 12, 16))
3302 return false;
3303 }
3304
Evan Cheng506d3df2006-03-29 23:07:14 +00003305 return true;
3306}
3307
Nate Begeman9008ca62009-04-27 18:41:29 +00003308/// isPSHUFLWMask - Return true if the node specifies a shuffle of elements that
3309/// is suitable for input to PSHUFLW.
Craig Toppera9a568a2012-05-02 08:03:44 +00003310static bool isPSHUFLWMask(ArrayRef<int> Mask, EVT VT, bool HasAVX2) {
3311 if (VT != MVT::v8i16 && (!HasAVX2 || VT != MVT::v16i16))
Evan Cheng506d3df2006-03-29 23:07:14 +00003312 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003313
Rafael Espindola15684b22009-04-24 12:40:33 +00003314 // Upper quadword copied in order.
Craig Topperc612d792012-01-02 09:17:37 +00003315 if (!isSequentialOrUndefInRange(Mask, 4, 4, 4))
3316 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003317
Rafael Espindola15684b22009-04-24 12:40:33 +00003318 // Lower quadword shuffled.
Craig Topperc612d792012-01-02 09:17:37 +00003319 for (unsigned i = 0; i != 4; ++i)
Craig Toppera9a568a2012-05-02 08:03:44 +00003320 if (!isUndefOrInRange(Mask[i], 0, 4))
Rafael Espindola15684b22009-04-24 12:40:33 +00003321 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003322
Craig Toppera9a568a2012-05-02 08:03:44 +00003323 if (VT == MVT::v16i16) {
3324 // Upper quadword copied in order.
3325 if (!isSequentialOrUndefInRange(Mask, 12, 4, 12))
3326 return false;
3327
3328 // Lower quadword shuffled.
3329 for (unsigned i = 8; i != 12; ++i)
3330 if (!isUndefOrInRange(Mask[i], 8, 12))
3331 return false;
3332 }
3333
Rafael Espindola15684b22009-04-24 12:40:33 +00003334 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00003335}
3336
Nate Begemana09008b2009-10-19 02:17:23 +00003337/// isPALIGNRMask - Return true if the node specifies a shuffle of elements that
3338/// is suitable for input to PALIGNR.
Craig Topper0e2037b2012-01-20 05:53:00 +00003339static bool isPALIGNRMask(ArrayRef<int> Mask, EVT VT,
3340 const X86Subtarget *Subtarget) {
3341 if ((VT.getSizeInBits() == 128 && !Subtarget->hasSSSE3()) ||
3342 (VT.getSizeInBits() == 256 && !Subtarget->hasAVX2()))
Bruno Cardoso Lopes9065d4b2011-07-29 01:30:59 +00003343 return false;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003344
Craig Topper0e2037b2012-01-20 05:53:00 +00003345 unsigned NumElts = VT.getVectorNumElements();
3346 unsigned NumLanes = VT.getSizeInBits()/128;
3347 unsigned NumLaneElts = NumElts/NumLanes;
3348
3349 // Do not handle 64-bit element shuffles with palignr.
3350 if (NumLaneElts == 2)
Nate Begemana09008b2009-10-19 02:17:23 +00003351 return false;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003352
Craig Topper0e2037b2012-01-20 05:53:00 +00003353 for (unsigned l = 0; l != NumElts; l+=NumLaneElts) {
3354 unsigned i;
3355 for (i = 0; i != NumLaneElts; ++i) {
3356 if (Mask[i+l] >= 0)
3357 break;
3358 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00003359
Craig Topper0e2037b2012-01-20 05:53:00 +00003360 // Lane is all undef, go to next lane
3361 if (i == NumLaneElts)
3362 continue;
Nate Begemana09008b2009-10-19 02:17:23 +00003363
Craig Topper0e2037b2012-01-20 05:53:00 +00003364 int Start = Mask[i+l];
Nate Begemana09008b2009-10-19 02:17:23 +00003365
Craig Topper0e2037b2012-01-20 05:53:00 +00003366 // Make sure its in this lane in one of the sources
3367 if (!isUndefOrInRange(Start, l, l+NumLaneElts) &&
3368 !isUndefOrInRange(Start, l+NumElts, l+NumElts+NumLaneElts))
Nate Begemana09008b2009-10-19 02:17:23 +00003369 return false;
Craig Topper0e2037b2012-01-20 05:53:00 +00003370
3371 // If not lane 0, then we must match lane 0
3372 if (l != 0 && Mask[i] >= 0 && !isUndefOrEqual(Start, Mask[i]+l))
3373 return false;
3374
3375 // Correct second source to be contiguous with first source
3376 if (Start >= (int)NumElts)
3377 Start -= NumElts - NumLaneElts;
3378
3379 // Make sure we're shifting in the right direction.
3380 if (Start <= (int)(i+l))
3381 return false;
3382
3383 Start -= i;
3384
3385 // Check the rest of the elements to see if they are consecutive.
3386 for (++i; i != NumLaneElts; ++i) {
3387 int Idx = Mask[i+l];
3388
3389 // Make sure its in this lane
3390 if (!isUndefOrInRange(Idx, l, l+NumLaneElts) &&
3391 !isUndefOrInRange(Idx, l+NumElts, l+NumElts+NumLaneElts))
3392 return false;
3393
3394 // If not lane 0, then we must match lane 0
3395 if (l != 0 && Mask[i] >= 0 && !isUndefOrEqual(Idx, Mask[i]+l))
3396 return false;
3397
3398 if (Idx >= (int)NumElts)
3399 Idx -= NumElts - NumLaneElts;
3400
3401 if (!isUndefOrEqual(Idx, Start+i))
3402 return false;
3403
3404 }
Nate Begemana09008b2009-10-19 02:17:23 +00003405 }
Craig Topper0e2037b2012-01-20 05:53:00 +00003406
Nate Begemana09008b2009-10-19 02:17:23 +00003407 return true;
3408}
3409
Craig Topper1a7700a2012-01-19 08:19:12 +00003410/// CommuteVectorShuffleMask - Change values in a shuffle permute mask assuming
3411/// the two vector operands have swapped position.
3412static void CommuteVectorShuffleMask(SmallVectorImpl<int> &Mask,
3413 unsigned NumElems) {
3414 for (unsigned i = 0; i != NumElems; ++i) {
3415 int idx = Mask[i];
3416 if (idx < 0)
3417 continue;
3418 else if (idx < (int)NumElems)
3419 Mask[i] = idx + NumElems;
3420 else
3421 Mask[i] = idx - NumElems;
3422 }
3423}
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00003424
Craig Topper1a7700a2012-01-19 08:19:12 +00003425/// isSHUFPMask - Return true if the specified VECTOR_SHUFFLE operand
3426/// specifies a shuffle of elements that is suitable for input to 128/256-bit
3427/// SHUFPS and SHUFPD. If Commuted is true, then it checks for sources to be
3428/// reverse of what x86 shuffles want.
3429static bool isSHUFPMask(ArrayRef<int> Mask, EVT VT, bool HasAVX,
3430 bool Commuted = false) {
3431 if (!HasAVX && VT.getSizeInBits() == 256)
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00003432 return false;
3433
Craig Topper1a7700a2012-01-19 08:19:12 +00003434 unsigned NumElems = VT.getVectorNumElements();
3435 unsigned NumLanes = VT.getSizeInBits()/128;
3436 unsigned NumLaneElems = NumElems/NumLanes;
3437
3438 if (NumLaneElems != 2 && NumLaneElems != 4)
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00003439 return false;
3440
3441 // VSHUFPSY divides the resulting vector into 4 chunks.
3442 // The sources are also splitted into 4 chunks, and each destination
3443 // chunk must come from a different source chunk.
3444 //
3445 // SRC1 => X7 X6 X5 X4 X3 X2 X1 X0
3446 // SRC2 => Y7 Y6 Y5 Y4 Y3 Y2 Y1 Y9
3447 //
3448 // DST => Y7..Y4, Y7..Y4, X7..X4, X7..X4,
3449 // Y3..Y0, Y3..Y0, X3..X0, X3..X0
3450 //
Craig Topper9d7025b2011-11-27 21:41:12 +00003451 // VSHUFPDY divides the resulting vector into 4 chunks.
3452 // The sources are also splitted into 4 chunks, and each destination
3453 // chunk must come from a different source chunk.
3454 //
3455 // SRC1 => X3 X2 X1 X0
3456 // SRC2 => Y3 Y2 Y1 Y0
3457 //
3458 // DST => Y3..Y2, X3..X2, Y1..Y0, X1..X0
3459 //
Craig Topper1a7700a2012-01-19 08:19:12 +00003460 unsigned HalfLaneElems = NumLaneElems/2;
3461 for (unsigned l = 0; l != NumElems; l += NumLaneElems) {
3462 for (unsigned i = 0; i != NumLaneElems; ++i) {
3463 int Idx = Mask[i+l];
3464 unsigned RngStart = l + ((Commuted == (i<HalfLaneElems)) ? NumElems : 0);
3465 if (!isUndefOrInRange(Idx, RngStart, RngStart+NumLaneElems))
3466 return false;
3467 // For VSHUFPSY, the mask of the second half must be the same as the
3468 // first but with the appropriate offsets. This works in the same way as
3469 // VPERMILPS works with masks.
3470 if (NumElems != 8 || l == 0 || Mask[i] < 0)
3471 continue;
3472 if (!isUndefOrEqual(Idx, Mask[i]+l))
3473 return false;
Craig Topper1ff73d72011-12-06 04:59:07 +00003474 }
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00003475 }
3476
3477 return true;
3478}
3479
Evan Cheng2c0dbd02006-03-24 02:58:06 +00003480/// isMOVHLPSMask - Return true if the specified VECTOR_SHUFFLE operand
3481/// specifies a shuffle of elements that is suitable for input to MOVHLPS.
Craig Topperdd637ae2012-02-19 05:41:45 +00003482static bool isMOVHLPSMask(ArrayRef<int> Mask, EVT VT) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00003483 if (!VT.is128BitVector())
Bruno Cardoso Lopes61260052011-07-29 02:05:28 +00003484 return false;
3485
Craig Topper7a9a28b2012-08-12 02:23:29 +00003486 unsigned NumElems = VT.getVectorNumElements();
3487
Bruno Cardoso Lopes61260052011-07-29 02:05:28 +00003488 if (NumElems != 4)
Evan Cheng2c0dbd02006-03-24 02:58:06 +00003489 return false;
3490
Evan Cheng2064a2b2006-03-28 06:50:32 +00003491 // Expect bit0 == 6, bit1 == 7, bit2 == 2, bit3 == 3
Craig Topperdd637ae2012-02-19 05:41:45 +00003492 return isUndefOrEqual(Mask[0], 6) &&
3493 isUndefOrEqual(Mask[1], 7) &&
3494 isUndefOrEqual(Mask[2], 2) &&
3495 isUndefOrEqual(Mask[3], 3);
Evan Cheng6e56e2c2006-11-07 22:14:24 +00003496}
3497
Nate Begeman0b10b912009-11-07 23:17:15 +00003498/// isMOVHLPS_v_undef_Mask - Special case of isMOVHLPSMask for canonical form
3499/// of vector_shuffle v, v, <2, 3, 2, 3>, i.e. vector_shuffle v, undef,
3500/// <2, 3, 2, 3>
Craig Topperdd637ae2012-02-19 05:41:45 +00003501static bool isMOVHLPS_v_undef_Mask(ArrayRef<int> Mask, EVT VT) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00003502 if (!VT.is128BitVector())
Bruno Cardoso Lopes61260052011-07-29 02:05:28 +00003503 return false;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003504
Craig Topper7a9a28b2012-08-12 02:23:29 +00003505 unsigned NumElems = VT.getVectorNumElements();
3506
Nate Begeman0b10b912009-11-07 23:17:15 +00003507 if (NumElems != 4)
3508 return false;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003509
Craig Topperdd637ae2012-02-19 05:41:45 +00003510 return isUndefOrEqual(Mask[0], 2) &&
3511 isUndefOrEqual(Mask[1], 3) &&
3512 isUndefOrEqual(Mask[2], 2) &&
3513 isUndefOrEqual(Mask[3], 3);
Nate Begeman0b10b912009-11-07 23:17:15 +00003514}
3515
Evan Cheng5ced1d82006-04-06 23:23:56 +00003516/// isMOVLPMask - Return true if the specified VECTOR_SHUFFLE operand
3517/// specifies a shuffle of elements that is suitable for input to MOVLP{S|D}.
Craig Topperdd637ae2012-02-19 05:41:45 +00003518static bool isMOVLPMask(ArrayRef<int> Mask, EVT VT) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00003519 if (!VT.is128BitVector())
Elena Demikhovsky021c0a22011-12-28 08:14:01 +00003520 return false;
3521
Craig Topperdd637ae2012-02-19 05:41:45 +00003522 unsigned NumElems = VT.getVectorNumElements();
Evan Cheng5ced1d82006-04-06 23:23:56 +00003523
Evan Cheng5ced1d82006-04-06 23:23:56 +00003524 if (NumElems != 2 && NumElems != 4)
3525 return false;
3526
Chad Rosier238ae312012-04-30 17:47:15 +00003527 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
Craig Topperdd637ae2012-02-19 05:41:45 +00003528 if (!isUndefOrEqual(Mask[i], i + NumElems))
Evan Chengc5cdff22006-04-07 21:53:05 +00003529 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003530
Chad Rosier238ae312012-04-30 17:47:15 +00003531 for (unsigned i = NumElems/2, e = NumElems; i != e; ++i)
Craig Topperdd637ae2012-02-19 05:41:45 +00003532 if (!isUndefOrEqual(Mask[i], i))
Evan Chengc5cdff22006-04-07 21:53:05 +00003533 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003534
3535 return true;
3536}
3537
Nate Begeman0b10b912009-11-07 23:17:15 +00003538/// isMOVLHPSMask - Return true if the specified VECTOR_SHUFFLE operand
3539/// specifies a shuffle of elements that is suitable for input to MOVLHPS.
Craig Topperdd637ae2012-02-19 05:41:45 +00003540static bool isMOVLHPSMask(ArrayRef<int> Mask, EVT VT) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00003541 if (!VT.is128BitVector())
3542 return false;
3543
Craig Topperdd637ae2012-02-19 05:41:45 +00003544 unsigned NumElems = VT.getVectorNumElements();
Evan Cheng5ced1d82006-04-06 23:23:56 +00003545
Craig Topper7a9a28b2012-08-12 02:23:29 +00003546 if (NumElems != 2 && NumElems != 4)
Evan Cheng5ced1d82006-04-06 23:23:56 +00003547 return false;
3548
Chad Rosier238ae312012-04-30 17:47:15 +00003549 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
Craig Topperdd637ae2012-02-19 05:41:45 +00003550 if (!isUndefOrEqual(Mask[i], i))
Evan Chengc5cdff22006-04-07 21:53:05 +00003551 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003552
Chad Rosier238ae312012-04-30 17:47:15 +00003553 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
3554 if (!isUndefOrEqual(Mask[i + e], i + NumElems))
Evan Chengc5cdff22006-04-07 21:53:05 +00003555 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003556
3557 return true;
3558}
3559
Elena Demikhovsky15963732012-06-26 08:04:10 +00003560//
3561// Some special combinations that can be optimized.
3562//
3563static
3564SDValue Compact8x32ShuffleNode(ShuffleVectorSDNode *SVOp,
3565 SelectionDAG &DAG) {
3566 EVT VT = SVOp->getValueType(0);
Elena Demikhovsky15963732012-06-26 08:04:10 +00003567 DebugLoc dl = SVOp->getDebugLoc();
3568
3569 if (VT != MVT::v8i32 && VT != MVT::v8f32)
3570 return SDValue();
3571
3572 ArrayRef<int> Mask = SVOp->getMask();
3573
3574 // These are the special masks that may be optimized.
3575 static const int MaskToOptimizeEven[] = {0, 8, 2, 10, 4, 12, 6, 14};
3576 static const int MaskToOptimizeOdd[] = {1, 9, 3, 11, 5, 13, 7, 15};
3577 bool MatchEvenMask = true;
3578 bool MatchOddMask = true;
3579 for (int i=0; i<8; ++i) {
3580 if (!isUndefOrEqual(Mask[i], MaskToOptimizeEven[i]))
3581 MatchEvenMask = false;
3582 if (!isUndefOrEqual(Mask[i], MaskToOptimizeOdd[i]))
3583 MatchOddMask = false;
3584 }
Elena Demikhovsky15963732012-06-26 08:04:10 +00003585
Elena Demikhovsky32510202012-09-04 12:49:02 +00003586 if (!MatchEvenMask && !MatchOddMask)
Elena Demikhovsky15963732012-06-26 08:04:10 +00003587 return SDValue();
Michael Liao471b9172012-10-03 23:43:52 +00003588
Elena Demikhovsky15963732012-06-26 08:04:10 +00003589 SDValue UndefNode = DAG.getNode(ISD::UNDEF, dl, VT);
3590
Elena Demikhovsky32510202012-09-04 12:49:02 +00003591 SDValue Op0 = SVOp->getOperand(0);
3592 SDValue Op1 = SVOp->getOperand(1);
3593
3594 if (MatchEvenMask) {
3595 // Shift the second operand right to 32 bits.
3596 static const int ShiftRightMask[] = {-1, 0, -1, 2, -1, 4, -1, 6 };
3597 Op1 = DAG.getVectorShuffle(VT, dl, Op1, UndefNode, ShiftRightMask);
3598 } else {
3599 // Shift the first operand left to 32 bits.
3600 static const int ShiftLeftMask[] = {1, -1, 3, -1, 5, -1, 7, -1 };
3601 Op0 = DAG.getVectorShuffle(VT, dl, Op0, UndefNode, ShiftLeftMask);
3602 }
3603 static const int BlendMask[] = {0, 9, 2, 11, 4, 13, 6, 15};
3604 return DAG.getVectorShuffle(VT, dl, Op0, Op1, BlendMask);
Elena Demikhovsky15963732012-06-26 08:04:10 +00003605}
3606
Evan Cheng0038e592006-03-28 00:39:58 +00003607/// isUNPCKLMask - Return true if the specified VECTOR_SHUFFLE operand
3608/// specifies a shuffle of elements that is suitable for input to UNPCKL.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003609static bool isUNPCKLMask(ArrayRef<int> Mask, EVT VT,
Craig Topper6347e862011-11-21 06:57:39 +00003610 bool HasAVX2, bool V2IsSplat = false) {
Craig Topper94438ba2011-12-16 08:06:31 +00003611 unsigned NumElts = VT.getVectorNumElements();
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003612
3613 assert((VT.is128BitVector() || VT.is256BitVector()) &&
3614 "Unsupported vector type for unpckh");
3615
Craig Topper6347e862011-11-21 06:57:39 +00003616 if (VT.getSizeInBits() == 256 && NumElts != 4 && NumElts != 8 &&
Craig Topper6fa583d2011-11-21 08:26:50 +00003617 (!HasAVX2 || (NumElts != 16 && NumElts != 32)))
Evan Cheng0038e592006-03-28 00:39:58 +00003618 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003619
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003620 // Handle 128 and 256-bit vector lengths. AVX defines UNPCK* to operate
3621 // independently on 128-bit lanes.
3622 unsigned NumLanes = VT.getSizeInBits()/128;
3623 unsigned NumLaneElts = NumElts/NumLanes;
David Greenea20244d2011-03-02 17:23:43 +00003624
Craig Topper94438ba2011-12-16 08:06:31 +00003625 for (unsigned l = 0; l != NumLanes; ++l) {
3626 for (unsigned i = l*NumLaneElts, j = l*NumLaneElts;
3627 i != (l+1)*NumLaneElts;
David Greenea20244d2011-03-02 17:23:43 +00003628 i += 2, ++j) {
3629 int BitI = Mask[i];
3630 int BitI1 = Mask[i+1];
3631 if (!isUndefOrEqual(BitI, j))
Evan Cheng39623da2006-04-20 08:58:49 +00003632 return false;
David Greenea20244d2011-03-02 17:23:43 +00003633 if (V2IsSplat) {
3634 if (!isUndefOrEqual(BitI1, NumElts))
3635 return false;
3636 } else {
3637 if (!isUndefOrEqual(BitI1, j + NumElts))
3638 return false;
3639 }
Evan Cheng39623da2006-04-20 08:58:49 +00003640 }
Evan Cheng0038e592006-03-28 00:39:58 +00003641 }
David Greenea20244d2011-03-02 17:23:43 +00003642
Evan Cheng0038e592006-03-28 00:39:58 +00003643 return true;
3644}
3645
Evan Cheng4fcb9222006-03-28 02:43:26 +00003646/// isUNPCKHMask - Return true if the specified VECTOR_SHUFFLE operand
3647/// specifies a shuffle of elements that is suitable for input to UNPCKH.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003648static bool isUNPCKHMask(ArrayRef<int> Mask, EVT VT,
Craig Topper6347e862011-11-21 06:57:39 +00003649 bool HasAVX2, bool V2IsSplat = false) {
Craig Topper94438ba2011-12-16 08:06:31 +00003650 unsigned NumElts = VT.getVectorNumElements();
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003651
3652 assert((VT.is128BitVector() || VT.is256BitVector()) &&
3653 "Unsupported vector type for unpckh");
3654
Craig Topper6347e862011-11-21 06:57:39 +00003655 if (VT.getSizeInBits() == 256 && NumElts != 4 && NumElts != 8 &&
Craig Topper6fa583d2011-11-21 08:26:50 +00003656 (!HasAVX2 || (NumElts != 16 && NumElts != 32)))
Evan Cheng4fcb9222006-03-28 02:43:26 +00003657 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003658
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003659 // Handle 128 and 256-bit vector lengths. AVX defines UNPCK* to operate
3660 // independently on 128-bit lanes.
3661 unsigned NumLanes = VT.getSizeInBits()/128;
3662 unsigned NumLaneElts = NumElts/NumLanes;
3663
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003664 for (unsigned l = 0; l != NumLanes; ++l) {
Craig Topper94438ba2011-12-16 08:06:31 +00003665 for (unsigned i = l*NumLaneElts, j = (l*NumLaneElts)+NumLaneElts/2;
3666 i != (l+1)*NumLaneElts; i += 2, ++j) {
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003667 int BitI = Mask[i];
3668 int BitI1 = Mask[i+1];
3669 if (!isUndefOrEqual(BitI, j))
Evan Cheng39623da2006-04-20 08:58:49 +00003670 return false;
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003671 if (V2IsSplat) {
3672 if (isUndefOrEqual(BitI1, NumElts))
3673 return false;
3674 } else {
3675 if (!isUndefOrEqual(BitI1, j+NumElts))
3676 return false;
3677 }
Evan Cheng39623da2006-04-20 08:58:49 +00003678 }
Evan Cheng4fcb9222006-03-28 02:43:26 +00003679 }
Evan Cheng4fcb9222006-03-28 02:43:26 +00003680 return true;
3681}
3682
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003683/// isUNPCKL_v_undef_Mask - Special case of isUNPCKLMask for canonical form
3684/// of vector_shuffle v, v, <0, 4, 1, 5>, i.e. vector_shuffle v, undef,
3685/// <0, 0, 1, 1>
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003686static bool isUNPCKL_v_undef_Mask(ArrayRef<int> Mask, EVT VT,
Craig Topper94438ba2011-12-16 08:06:31 +00003687 bool HasAVX2) {
3688 unsigned NumElts = VT.getVectorNumElements();
3689
3690 assert((VT.is128BitVector() || VT.is256BitVector()) &&
3691 "Unsupported vector type for unpckh");
3692
3693 if (VT.getSizeInBits() == 256 && NumElts != 4 && NumElts != 8 &&
3694 (!HasAVX2 || (NumElts != 16 && NumElts != 32)))
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003695 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003696
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00003697 // For 256-bit i64/f64, use MOVDDUPY instead, so reject the matching pattern
3698 // FIXME: Need a better way to get rid of this, there's no latency difference
3699 // between UNPCKLPD and MOVDDUP, the later should always be checked first and
3700 // the former later. We should also remove the "_undef" special mask.
Craig Topper94438ba2011-12-16 08:06:31 +00003701 if (NumElts == 4 && VT.getSizeInBits() == 256)
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00003702 return false;
3703
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003704 // Handle 128 and 256-bit vector lengths. AVX defines UNPCK* to operate
3705 // independently on 128-bit lanes.
Craig Topper94438ba2011-12-16 08:06:31 +00003706 unsigned NumLanes = VT.getSizeInBits()/128;
3707 unsigned NumLaneElts = NumElts/NumLanes;
David Greenea20244d2011-03-02 17:23:43 +00003708
Craig Topper94438ba2011-12-16 08:06:31 +00003709 for (unsigned l = 0; l != NumLanes; ++l) {
3710 for (unsigned i = l*NumLaneElts, j = l*NumLaneElts;
3711 i != (l+1)*NumLaneElts;
David Greenea20244d2011-03-02 17:23:43 +00003712 i += 2, ++j) {
3713 int BitI = Mask[i];
3714 int BitI1 = Mask[i+1];
3715
3716 if (!isUndefOrEqual(BitI, j))
3717 return false;
3718 if (!isUndefOrEqual(BitI1, j))
3719 return false;
3720 }
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003721 }
David Greenea20244d2011-03-02 17:23:43 +00003722
Rafael Espindola15684b22009-04-24 12:40:33 +00003723 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00003724}
3725
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003726/// isUNPCKH_v_undef_Mask - Special case of isUNPCKHMask for canonical form
3727/// of vector_shuffle v, v, <2, 6, 3, 7>, i.e. vector_shuffle v, undef,
3728/// <2, 2, 3, 3>
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003729static bool isUNPCKH_v_undef_Mask(ArrayRef<int> Mask, EVT VT, bool HasAVX2) {
Craig Topper94438ba2011-12-16 08:06:31 +00003730 unsigned NumElts = VT.getVectorNumElements();
3731
3732 assert((VT.is128BitVector() || VT.is256BitVector()) &&
3733 "Unsupported vector type for unpckh");
3734
3735 if (VT.getSizeInBits() == 256 && NumElts != 4 && NumElts != 8 &&
3736 (!HasAVX2 || (NumElts != 16 && NumElts != 32)))
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003737 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003738
Craig Topper94438ba2011-12-16 08:06:31 +00003739 // Handle 128 and 256-bit vector lengths. AVX defines UNPCK* to operate
3740 // independently on 128-bit lanes.
3741 unsigned NumLanes = VT.getSizeInBits()/128;
3742 unsigned NumLaneElts = NumElts/NumLanes;
3743
3744 for (unsigned l = 0; l != NumLanes; ++l) {
3745 for (unsigned i = l*NumLaneElts, j = (l*NumLaneElts)+NumLaneElts/2;
3746 i != (l+1)*NumLaneElts; i += 2, ++j) {
3747 int BitI = Mask[i];
3748 int BitI1 = Mask[i+1];
3749 if (!isUndefOrEqual(BitI, j))
3750 return false;
3751 if (!isUndefOrEqual(BitI1, j))
3752 return false;
3753 }
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003754 }
Rafael Espindola15684b22009-04-24 12:40:33 +00003755 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00003756}
3757
Evan Cheng017dcc62006-04-21 01:05:10 +00003758/// isMOVLMask - Return true if the specified VECTOR_SHUFFLE operand
3759/// specifies a shuffle of elements that is suitable for input to MOVSS,
3760/// MOVSD, and MOVD, i.e. setting the lowest element.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003761static bool isMOVLMask(ArrayRef<int> Mask, EVT VT) {
Eli Friedman10415532009-06-06 06:05:10 +00003762 if (VT.getVectorElementType().getSizeInBits() < 32)
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003763 return false;
Craig Topper7a9a28b2012-08-12 02:23:29 +00003764 if (!VT.is128BitVector())
Elena Demikhovsky021c0a22011-12-28 08:14:01 +00003765 return false;
Eli Friedman10415532009-06-06 06:05:10 +00003766
Craig Topperc612d792012-01-02 09:17:37 +00003767 unsigned NumElts = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00003768
Nate Begeman9008ca62009-04-27 18:41:29 +00003769 if (!isUndefOrEqual(Mask[0], NumElts))
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003770 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003771
Craig Topperc612d792012-01-02 09:17:37 +00003772 for (unsigned i = 1; i != NumElts; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003773 if (!isUndefOrEqual(Mask[i], i))
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003774 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003775
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003776 return true;
3777}
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003778
Craig Topper70b883b2011-11-28 10:14:51 +00003779/// isVPERM2X128Mask - Match 256-bit shuffles where the elements are considered
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003780/// as permutations between 128-bit chunks or halves. As an example: this
3781/// shuffle bellow:
3782/// vector_shuffle <4, 5, 6, 7, 12, 13, 14, 15>
3783/// The first half comes from the second half of V1 and the second half from the
3784/// the second half of V2.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003785static bool isVPERM2X128Mask(ArrayRef<int> Mask, EVT VT, bool HasAVX) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00003786 if (!HasAVX || !VT.is256BitVector())
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003787 return false;
3788
3789 // The shuffle result is divided into half A and half B. In total the two
3790 // sources have 4 halves, namely: C, D, E, F. The final values of A and
3791 // B must come from C, D, E or F.
Craig Topperc612d792012-01-02 09:17:37 +00003792 unsigned HalfSize = VT.getVectorNumElements()/2;
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003793 bool MatchA = false, MatchB = false;
3794
3795 // Check if A comes from one of C, D, E, F.
Craig Topperc612d792012-01-02 09:17:37 +00003796 for (unsigned Half = 0; Half != 4; ++Half) {
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003797 if (isSequentialOrUndefInRange(Mask, 0, HalfSize, Half*HalfSize)) {
3798 MatchA = true;
3799 break;
3800 }
3801 }
3802
3803 // Check if B comes from one of C, D, E, F.
Craig Topperc612d792012-01-02 09:17:37 +00003804 for (unsigned Half = 0; Half != 4; ++Half) {
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003805 if (isSequentialOrUndefInRange(Mask, HalfSize, HalfSize, Half*HalfSize)) {
3806 MatchB = true;
3807 break;
3808 }
3809 }
3810
3811 return MatchA && MatchB;
3812}
3813
Craig Topper70b883b2011-11-28 10:14:51 +00003814/// getShuffleVPERM2X128Immediate - Return the appropriate immediate to shuffle
3815/// the specified VECTOR_MASK mask with VPERM2F128/VPERM2I128 instructions.
Craig Topperd93e4c32011-12-11 19:12:35 +00003816static unsigned getShuffleVPERM2X128Immediate(ShuffleVectorSDNode *SVOp) {
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003817 EVT VT = SVOp->getValueType(0);
3818
Craig Topperc612d792012-01-02 09:17:37 +00003819 unsigned HalfSize = VT.getVectorNumElements()/2;
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003820
Craig Topperc612d792012-01-02 09:17:37 +00003821 unsigned FstHalf = 0, SndHalf = 0;
3822 for (unsigned i = 0; i < HalfSize; ++i) {
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003823 if (SVOp->getMaskElt(i) > 0) {
3824 FstHalf = SVOp->getMaskElt(i)/HalfSize;
3825 break;
3826 }
3827 }
Craig Topperc612d792012-01-02 09:17:37 +00003828 for (unsigned i = HalfSize; i < HalfSize*2; ++i) {
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003829 if (SVOp->getMaskElt(i) > 0) {
3830 SndHalf = SVOp->getMaskElt(i)/HalfSize;
3831 break;
3832 }
3833 }
3834
3835 return (FstHalf | (SndHalf << 4));
3836}
3837
Craig Topper70b883b2011-11-28 10:14:51 +00003838/// isVPERMILPMask - Return true if the specified VECTOR_SHUFFLE operand
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00003839/// specifies a shuffle of elements that is suitable for input to VPERMILPD*.
3840/// Note that VPERMIL mask matching is different depending whether theunderlying
3841/// type is 32 or 64. In the VPERMILPS the high half of the mask should point
3842/// to the same elements of the low, but to the higher half of the source.
3843/// In VPERMILPD the two lanes could be shuffled independently of each other
Craig Topperdbd98a42012-02-07 06:28:42 +00003844/// with the same restriction that lanes can't be crossed. Also handles PSHUFDY.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003845static bool isVPERMILPMask(ArrayRef<int> Mask, EVT VT, bool HasAVX) {
Craig Topper70b883b2011-11-28 10:14:51 +00003846 if (!HasAVX)
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00003847 return false;
3848
Craig Topperc612d792012-01-02 09:17:37 +00003849 unsigned NumElts = VT.getVectorNumElements();
Craig Topper70b883b2011-11-28 10:14:51 +00003850 // Only match 256-bit with 32/64-bit types
3851 if (VT.getSizeInBits() != 256 || (NumElts != 4 && NumElts != 8))
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00003852 return false;
3853
Craig Topperc612d792012-01-02 09:17:37 +00003854 unsigned NumLanes = VT.getSizeInBits()/128;
3855 unsigned LaneSize = NumElts/NumLanes;
Craig Topper1a7700a2012-01-19 08:19:12 +00003856 for (unsigned l = 0; l != NumElts; l += LaneSize) {
Craig Topperc612d792012-01-02 09:17:37 +00003857 for (unsigned i = 0; i != LaneSize; ++i) {
Craig Topper1a7700a2012-01-19 08:19:12 +00003858 if (!isUndefOrInRange(Mask[i+l], l, l+LaneSize))
Craig Topper70b883b2011-11-28 10:14:51 +00003859 return false;
Craig Topper1a7700a2012-01-19 08:19:12 +00003860 if (NumElts != 8 || l == 0)
Craig Topper70b883b2011-11-28 10:14:51 +00003861 continue;
3862 // VPERMILPS handling
3863 if (Mask[i] < 0)
3864 continue;
Craig Topper1a7700a2012-01-19 08:19:12 +00003865 if (!isUndefOrEqual(Mask[i+l], Mask[i]+l))
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00003866 return false;
3867 }
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00003868 }
3869
3870 return true;
3871}
3872
Craig Topper5aaffa82012-02-19 02:53:47 +00003873/// isCommutedMOVLMask - Returns true if the shuffle mask is except the reverse
Evan Cheng017dcc62006-04-21 01:05:10 +00003874/// of what x86 movss want. X86 movs requires the lowest element to be lowest
Evan Cheng39623da2006-04-20 08:58:49 +00003875/// element of vector 2 and the other elements to come from vector 1 in order.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003876static bool isCommutedMOVLMask(ArrayRef<int> Mask, EVT VT,
Nate Begeman9008ca62009-04-27 18:41:29 +00003877 bool V2IsSplat = false, bool V2IsUndef = false) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00003878 if (!VT.is128BitVector())
Craig Topper97327dc2012-03-18 22:50:10 +00003879 return false;
Craig Topper7a9a28b2012-08-12 02:23:29 +00003880
3881 unsigned NumOps = VT.getVectorNumElements();
Chris Lattner5a88b832007-02-25 07:10:00 +00003882 if (NumOps != 2 && NumOps != 4 && NumOps != 8 && NumOps != 16)
Evan Cheng39623da2006-04-20 08:58:49 +00003883 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003884
Nate Begeman9008ca62009-04-27 18:41:29 +00003885 if (!isUndefOrEqual(Mask[0], 0))
Evan Cheng39623da2006-04-20 08:58:49 +00003886 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003887
Craig Topperc612d792012-01-02 09:17:37 +00003888 for (unsigned i = 1; i != NumOps; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003889 if (!(isUndefOrEqual(Mask[i], i+NumOps) ||
3890 (V2IsUndef && isUndefOrInRange(Mask[i], NumOps, NumOps*2)) ||
3891 (V2IsSplat && isUndefOrEqual(Mask[i], NumOps))))
Evan Cheng8cf723d2006-09-08 01:50:06 +00003892 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003893
Evan Cheng39623da2006-04-20 08:58:49 +00003894 return true;
3895}
3896
Evan Chengd9539472006-04-14 21:59:03 +00003897/// isMOVSHDUPMask - Return true if the specified VECTOR_SHUFFLE operand
3898/// specifies a shuffle of elements that is suitable for input to MOVSHDUP.
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003899/// Masks to match: <1, 1, 3, 3> or <1, 1, 3, 3, 5, 5, 7, 7>
Craig Topperdd637ae2012-02-19 05:41:45 +00003900static bool isMOVSHDUPMask(ArrayRef<int> Mask, EVT VT,
Craig Topper5aaffa82012-02-19 02:53:47 +00003901 const X86Subtarget *Subtarget) {
Craig Topperd0a31172012-01-10 06:37:29 +00003902 if (!Subtarget->hasSSE3())
Evan Chengd9539472006-04-14 21:59:03 +00003903 return false;
3904
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003905 unsigned NumElems = VT.getVectorNumElements();
3906
3907 if ((VT.getSizeInBits() == 128 && NumElems != 4) ||
3908 (VT.getSizeInBits() == 256 && NumElems != 8))
3909 return false;
3910
3911 // "i+1" is the value the indexed mask element must have
Craig Topperdd637ae2012-02-19 05:41:45 +00003912 for (unsigned i = 0; i != NumElems; i += 2)
3913 if (!isUndefOrEqual(Mask[i], i+1) ||
3914 !isUndefOrEqual(Mask[i+1], i+1))
Nate Begeman9008ca62009-04-27 18:41:29 +00003915 return false;
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003916
3917 return true;
Evan Chengd9539472006-04-14 21:59:03 +00003918}
3919
3920/// isMOVSLDUPMask - Return true if the specified VECTOR_SHUFFLE operand
3921/// specifies a shuffle of elements that is suitable for input to MOVSLDUP.
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003922/// Masks to match: <0, 0, 2, 2> or <0, 0, 2, 2, 4, 4, 6, 6>
Craig Topperdd637ae2012-02-19 05:41:45 +00003923static bool isMOVSLDUPMask(ArrayRef<int> Mask, EVT VT,
Craig Topper5aaffa82012-02-19 02:53:47 +00003924 const X86Subtarget *Subtarget) {
Craig Topperd0a31172012-01-10 06:37:29 +00003925 if (!Subtarget->hasSSE3())
Evan Chengd9539472006-04-14 21:59:03 +00003926 return false;
3927
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003928 unsigned NumElems = VT.getVectorNumElements();
3929
3930 if ((VT.getSizeInBits() == 128 && NumElems != 4) ||
3931 (VT.getSizeInBits() == 256 && NumElems != 8))
3932 return false;
3933
3934 // "i" is the value the indexed mask element must have
Craig Topperc612d792012-01-02 09:17:37 +00003935 for (unsigned i = 0; i != NumElems; i += 2)
Craig Topperdd637ae2012-02-19 05:41:45 +00003936 if (!isUndefOrEqual(Mask[i], i) ||
3937 !isUndefOrEqual(Mask[i+1], i))
Nate Begeman9008ca62009-04-27 18:41:29 +00003938 return false;
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003939
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003940 return true;
Evan Chengd9539472006-04-14 21:59:03 +00003941}
3942
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00003943/// isMOVDDUPYMask - Return true if the specified VECTOR_SHUFFLE operand
3944/// specifies a shuffle of elements that is suitable for input to 256-bit
3945/// version of MOVDDUP.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003946static bool isMOVDDUPYMask(ArrayRef<int> Mask, EVT VT, bool HasAVX) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00003947 if (!HasAVX || !VT.is256BitVector())
3948 return false;
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00003949
Craig Topper7a9a28b2012-08-12 02:23:29 +00003950 unsigned NumElts = VT.getVectorNumElements();
3951 if (NumElts != 4)
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00003952 return false;
3953
Craig Topperc612d792012-01-02 09:17:37 +00003954 for (unsigned i = 0; i != NumElts/2; ++i)
Craig Topperbeabc6c2011-12-05 06:56:46 +00003955 if (!isUndefOrEqual(Mask[i], 0))
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00003956 return false;
Craig Topperc612d792012-01-02 09:17:37 +00003957 for (unsigned i = NumElts/2; i != NumElts; ++i)
Craig Topperbeabc6c2011-12-05 06:56:46 +00003958 if (!isUndefOrEqual(Mask[i], NumElts/2))
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00003959 return false;
3960 return true;
3961}
3962
Evan Cheng0b457f02008-09-25 20:50:48 +00003963/// isMOVDDUPMask - Return true if the specified VECTOR_SHUFFLE operand
Bruno Cardoso Lopes06ef9232011-08-25 21:40:34 +00003964/// specifies a shuffle of elements that is suitable for input to 128-bit
3965/// version of MOVDDUP.
Craig Topperdd637ae2012-02-19 05:41:45 +00003966static bool isMOVDDUPMask(ArrayRef<int> Mask, EVT VT) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00003967 if (!VT.is128BitVector())
Bruno Cardoso Lopes06ef9232011-08-25 21:40:34 +00003968 return false;
3969
Craig Topperc612d792012-01-02 09:17:37 +00003970 unsigned e = VT.getVectorNumElements() / 2;
3971 for (unsigned i = 0; i != e; ++i)
Craig Topperdd637ae2012-02-19 05:41:45 +00003972 if (!isUndefOrEqual(Mask[i], i))
Evan Cheng0b457f02008-09-25 20:50:48 +00003973 return false;
Craig Topperc612d792012-01-02 09:17:37 +00003974 for (unsigned i = 0; i != e; ++i)
Craig Topperdd637ae2012-02-19 05:41:45 +00003975 if (!isUndefOrEqual(Mask[e+i], i))
Evan Cheng0b457f02008-09-25 20:50:48 +00003976 return false;
3977 return true;
3978}
3979
David Greenec38a03e2011-02-03 15:50:00 +00003980/// isVEXTRACTF128Index - Return true if the specified
3981/// EXTRACT_SUBVECTOR operand specifies a vector extract that is
3982/// suitable for input to VEXTRACTF128.
3983bool X86::isVEXTRACTF128Index(SDNode *N) {
3984 if (!isa<ConstantSDNode>(N->getOperand(1).getNode()))
3985 return false;
3986
3987 // The index should be aligned on a 128-bit boundary.
3988 uint64_t Index =
3989 cast<ConstantSDNode>(N->getOperand(1).getNode())->getZExtValue();
3990
3991 unsigned VL = N->getValueType(0).getVectorNumElements();
3992 unsigned VBits = N->getValueType(0).getSizeInBits();
3993 unsigned ElSize = VBits / VL;
3994 bool Result = (Index * ElSize) % 128 == 0;
3995
3996 return Result;
3997}
3998
David Greeneccacdc12011-02-04 16:08:29 +00003999/// isVINSERTF128Index - Return true if the specified INSERT_SUBVECTOR
4000/// operand specifies a subvector insert that is suitable for input to
4001/// VINSERTF128.
4002bool X86::isVINSERTF128Index(SDNode *N) {
4003 if (!isa<ConstantSDNode>(N->getOperand(2).getNode()))
4004 return false;
4005
4006 // The index should be aligned on a 128-bit boundary.
4007 uint64_t Index =
4008 cast<ConstantSDNode>(N->getOperand(2).getNode())->getZExtValue();
4009
4010 unsigned VL = N->getValueType(0).getVectorNumElements();
4011 unsigned VBits = N->getValueType(0).getSizeInBits();
4012 unsigned ElSize = VBits / VL;
4013 bool Result = (Index * ElSize) % 128 == 0;
4014
4015 return Result;
4016}
4017
Evan Cheng63d33002006-03-22 08:01:21 +00004018/// getShuffleSHUFImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00004019/// the specified VECTOR_SHUFFLE mask with PSHUF* and SHUFP* instructions.
Craig Topper1a7700a2012-01-19 08:19:12 +00004020/// Handles 128-bit and 256-bit.
Craig Topper5aaffa82012-02-19 02:53:47 +00004021static unsigned getShuffleSHUFImmediate(ShuffleVectorSDNode *N) {
Craig Topper1a7700a2012-01-19 08:19:12 +00004022 EVT VT = N->getValueType(0);
Nate Begeman9008ca62009-04-27 18:41:29 +00004023
Craig Topper1a7700a2012-01-19 08:19:12 +00004024 assert((VT.is128BitVector() || VT.is256BitVector()) &&
4025 "Unsupported vector type for PSHUF/SHUFP");
4026
4027 // Handle 128 and 256-bit vector lengths. AVX defines PSHUF/SHUFP to operate
4028 // independently on 128-bit lanes.
4029 unsigned NumElts = VT.getVectorNumElements();
4030 unsigned NumLanes = VT.getSizeInBits()/128;
4031 unsigned NumLaneElts = NumElts/NumLanes;
4032
4033 assert((NumLaneElts == 2 || NumLaneElts == 4) &&
4034 "Only supports 2 or 4 elements per lane");
4035
4036 unsigned Shift = (NumLaneElts == 4) ? 1 : 0;
Evan Chengb9df0ca2006-03-22 02:53:00 +00004037 unsigned Mask = 0;
Craig Topper1a7700a2012-01-19 08:19:12 +00004038 for (unsigned i = 0; i != NumElts; ++i) {
4039 int Elt = N->getMaskElt(i);
4040 if (Elt < 0) continue;
Craig Topper6b28d352012-05-03 07:12:59 +00004041 Elt &= NumLaneElts - 1;
4042 unsigned ShAmt = (i << Shift) % 8;
Craig Topper1a7700a2012-01-19 08:19:12 +00004043 Mask |= Elt << ShAmt;
Evan Cheng36b27f32006-03-28 23:41:33 +00004044 }
Craig Topper1a7700a2012-01-19 08:19:12 +00004045
Evan Cheng63d33002006-03-22 08:01:21 +00004046 return Mask;
4047}
4048
Evan Cheng506d3df2006-03-29 23:07:14 +00004049/// getShufflePSHUFHWImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00004050/// the specified VECTOR_SHUFFLE mask with the PSHUFHW instruction.
Craig Topperdd637ae2012-02-19 05:41:45 +00004051static unsigned getShufflePSHUFHWImmediate(ShuffleVectorSDNode *N) {
Craig Topper6b28d352012-05-03 07:12:59 +00004052 EVT VT = N->getValueType(0);
4053
4054 assert((VT == MVT::v8i16 || VT == MVT::v16i16) &&
4055 "Unsupported vector type for PSHUFHW");
4056
4057 unsigned NumElts = VT.getVectorNumElements();
4058
Evan Cheng506d3df2006-03-29 23:07:14 +00004059 unsigned Mask = 0;
Craig Topper6b28d352012-05-03 07:12:59 +00004060 for (unsigned l = 0; l != NumElts; l += 8) {
4061 // 8 nodes per lane, but we only care about the last 4.
4062 for (unsigned i = 0; i < 4; ++i) {
4063 int Elt = N->getMaskElt(l+i+4);
4064 if (Elt < 0) continue;
4065 Elt &= 0x3; // only 2-bits.
4066 Mask |= Elt << (i * 2);
4067 }
Evan Cheng506d3df2006-03-29 23:07:14 +00004068 }
Craig Topper6b28d352012-05-03 07:12:59 +00004069
Evan Cheng506d3df2006-03-29 23:07:14 +00004070 return Mask;
4071}
4072
4073/// getShufflePSHUFLWImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00004074/// the specified VECTOR_SHUFFLE mask with the PSHUFLW instruction.
Craig Topperdd637ae2012-02-19 05:41:45 +00004075static unsigned getShufflePSHUFLWImmediate(ShuffleVectorSDNode *N) {
Craig Topper6b28d352012-05-03 07:12:59 +00004076 EVT VT = N->getValueType(0);
4077
4078 assert((VT == MVT::v8i16 || VT == MVT::v16i16) &&
4079 "Unsupported vector type for PSHUFHW");
4080
4081 unsigned NumElts = VT.getVectorNumElements();
4082
Evan Cheng506d3df2006-03-29 23:07:14 +00004083 unsigned Mask = 0;
Craig Topper6b28d352012-05-03 07:12:59 +00004084 for (unsigned l = 0; l != NumElts; l += 8) {
4085 // 8 nodes per lane, but we only care about the first 4.
4086 for (unsigned i = 0; i < 4; ++i) {
4087 int Elt = N->getMaskElt(l+i);
4088 if (Elt < 0) continue;
4089 Elt &= 0x3; // only 2-bits
4090 Mask |= Elt << (i * 2);
4091 }
Evan Cheng506d3df2006-03-29 23:07:14 +00004092 }
Craig Topper6b28d352012-05-03 07:12:59 +00004093
Evan Cheng506d3df2006-03-29 23:07:14 +00004094 return Mask;
4095}
4096
Nate Begemana09008b2009-10-19 02:17:23 +00004097/// getShufflePALIGNRImmediate - Return the appropriate immediate to shuffle
4098/// the specified VECTOR_SHUFFLE mask with the PALIGNR instruction.
Craig Topperd93e4c32011-12-11 19:12:35 +00004099static unsigned getShufflePALIGNRImmediate(ShuffleVectorSDNode *SVOp) {
4100 EVT VT = SVOp->getValueType(0);
4101 unsigned EltSize = VT.getVectorElementType().getSizeInBits() >> 3;
Nate Begemana09008b2009-10-19 02:17:23 +00004102
Craig Topper0e2037b2012-01-20 05:53:00 +00004103 unsigned NumElts = VT.getVectorNumElements();
4104 unsigned NumLanes = VT.getSizeInBits()/128;
4105 unsigned NumLaneElts = NumElts/NumLanes;
4106
4107 int Val = 0;
4108 unsigned i;
4109 for (i = 0; i != NumElts; ++i) {
Nate Begemana09008b2009-10-19 02:17:23 +00004110 Val = SVOp->getMaskElt(i);
4111 if (Val >= 0)
4112 break;
4113 }
Craig Topper0e2037b2012-01-20 05:53:00 +00004114 if (Val >= (int)NumElts)
4115 Val -= NumElts - NumLaneElts;
4116
Eli Friedman63f8dde2011-07-25 21:36:45 +00004117 assert(Val - i > 0 && "PALIGNR imm should be positive");
Nate Begemana09008b2009-10-19 02:17:23 +00004118 return (Val - i) * EltSize;
4119}
4120
David Greenec38a03e2011-02-03 15:50:00 +00004121/// getExtractVEXTRACTF128Immediate - Return the appropriate immediate
4122/// to extract the specified EXTRACT_SUBVECTOR index with VEXTRACTF128
4123/// instructions.
4124unsigned X86::getExtractVEXTRACTF128Immediate(SDNode *N) {
4125 if (!isa<ConstantSDNode>(N->getOperand(1).getNode()))
4126 llvm_unreachable("Illegal extract subvector for VEXTRACTF128");
4127
4128 uint64_t Index =
4129 cast<ConstantSDNode>(N->getOperand(1).getNode())->getZExtValue();
4130
4131 EVT VecVT = N->getOperand(0).getValueType();
4132 EVT ElVT = VecVT.getVectorElementType();
4133
4134 unsigned NumElemsPerChunk = 128 / ElVT.getSizeInBits();
David Greenec38a03e2011-02-03 15:50:00 +00004135 return Index / NumElemsPerChunk;
4136}
4137
David Greeneccacdc12011-02-04 16:08:29 +00004138/// getInsertVINSERTF128Immediate - Return the appropriate immediate
4139/// to insert at the specified INSERT_SUBVECTOR index with VINSERTF128
4140/// instructions.
4141unsigned X86::getInsertVINSERTF128Immediate(SDNode *N) {
4142 if (!isa<ConstantSDNode>(N->getOperand(2).getNode()))
4143 llvm_unreachable("Illegal insert subvector for VINSERTF128");
4144
4145 uint64_t Index =
NAKAMURA Takumi27635382011-02-05 15:10:54 +00004146 cast<ConstantSDNode>(N->getOperand(2).getNode())->getZExtValue();
David Greeneccacdc12011-02-04 16:08:29 +00004147
4148 EVT VecVT = N->getValueType(0);
4149 EVT ElVT = VecVT.getVectorElementType();
4150
4151 unsigned NumElemsPerChunk = 128 / ElVT.getSizeInBits();
David Greeneccacdc12011-02-04 16:08:29 +00004152 return Index / NumElemsPerChunk;
4153}
4154
Elena Demikhovsky73c504a2012-04-15 11:18:59 +00004155/// getShuffleCLImmediate - Return the appropriate immediate to shuffle
4156/// the specified VECTOR_SHUFFLE mask with VPERMQ and VPERMPD instructions.
4157/// Handles 256-bit.
4158static unsigned getShuffleCLImmediate(ShuffleVectorSDNode *N) {
4159 EVT VT = N->getValueType(0);
4160
Elena Demikhovsky73c504a2012-04-15 11:18:59 +00004161 unsigned NumElts = VT.getVectorNumElements();
4162
Craig Topper095c5282012-04-15 23:48:57 +00004163 assert((VT.is256BitVector() && NumElts == 4) &&
4164 "Unsupported vector type for VPERMQ/VPERMPD");
4165
Elena Demikhovsky73c504a2012-04-15 11:18:59 +00004166 unsigned Mask = 0;
4167 for (unsigned i = 0; i != NumElts; ++i) {
4168 int Elt = N->getMaskElt(i);
Craig Topper095c5282012-04-15 23:48:57 +00004169 if (Elt < 0)
Elena Demikhovsky73c504a2012-04-15 11:18:59 +00004170 continue;
4171 Mask |= Elt << (i*2);
4172 }
4173
4174 return Mask;
4175}
Evan Cheng37b73872009-07-30 08:33:02 +00004176/// isZeroNode - Returns true if Elt is a constant zero or a floating point
4177/// constant +0.0.
4178bool X86::isZeroNode(SDValue Elt) {
4179 return ((isa<ConstantSDNode>(Elt) &&
Dan Gohmane368b462010-06-18 14:22:04 +00004180 cast<ConstantSDNode>(Elt)->isNullValue()) ||
Evan Cheng37b73872009-07-30 08:33:02 +00004181 (isa<ConstantFPSDNode>(Elt) &&
4182 cast<ConstantFPSDNode>(Elt)->getValueAPF().isPosZero()));
4183}
4184
Nate Begeman9008ca62009-04-27 18:41:29 +00004185/// CommuteVectorShuffle - Swap vector_shuffle operands as well as values in
4186/// their permute mask.
4187static SDValue CommuteVectorShuffle(ShuffleVectorSDNode *SVOp,
4188 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00004189 EVT VT = SVOp->getValueType(0);
Nate Begeman5a5ca152009-04-29 05:20:52 +00004190 unsigned NumElems = VT.getVectorNumElements();
Nate Begeman9008ca62009-04-27 18:41:29 +00004191 SmallVector<int, 8> MaskVec;
Eric Christopherfd179292009-08-27 18:07:15 +00004192
Nate Begeman5a5ca152009-04-29 05:20:52 +00004193 for (unsigned i = 0; i != NumElems; ++i) {
Craig Topper8ae97ba2012-05-21 06:40:16 +00004194 int Idx = SVOp->getMaskElt(i);
4195 if (Idx >= 0) {
4196 if (Idx < (int)NumElems)
4197 Idx += NumElems;
4198 else
4199 Idx -= NumElems;
4200 }
4201 MaskVec.push_back(Idx);
Evan Cheng5ced1d82006-04-06 23:23:56 +00004202 }
Nate Begeman9008ca62009-04-27 18:41:29 +00004203 return DAG.getVectorShuffle(VT, SVOp->getDebugLoc(), SVOp->getOperand(1),
4204 SVOp->getOperand(0), &MaskVec[0]);
Evan Cheng5ced1d82006-04-06 23:23:56 +00004205}
4206
Evan Cheng533a0aa2006-04-19 20:35:22 +00004207/// ShouldXformToMOVHLPS - Return true if the node should be transformed to
4208/// match movhlps. The lower half elements should come from upper half of
4209/// V1 (and in order), and the upper half elements should come from the upper
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00004210/// half of V2 (and in order).
Craig Topperdd637ae2012-02-19 05:41:45 +00004211static bool ShouldXformToMOVHLPS(ArrayRef<int> Mask, EVT VT) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00004212 if (!VT.is128BitVector())
Bruno Cardoso Lopes59353b42011-08-11 18:59:13 +00004213 return false;
4214 if (VT.getVectorNumElements() != 4)
Evan Cheng533a0aa2006-04-19 20:35:22 +00004215 return false;
4216 for (unsigned i = 0, e = 2; i != e; ++i)
Craig Topperdd637ae2012-02-19 05:41:45 +00004217 if (!isUndefOrEqual(Mask[i], i+2))
Evan Cheng533a0aa2006-04-19 20:35:22 +00004218 return false;
4219 for (unsigned i = 2; i != 4; ++i)
Craig Topperdd637ae2012-02-19 05:41:45 +00004220 if (!isUndefOrEqual(Mask[i], i+4))
Evan Cheng533a0aa2006-04-19 20:35:22 +00004221 return false;
4222 return true;
4223}
4224
Evan Cheng5ced1d82006-04-06 23:23:56 +00004225/// isScalarLoadToVector - Returns true if the node is a scalar load that
Evan Cheng7e2ff772008-05-08 00:57:18 +00004226/// is promoted to a vector. It also returns the LoadSDNode by reference if
4227/// required.
4228static bool isScalarLoadToVector(SDNode *N, LoadSDNode **LD = NULL) {
Evan Cheng0b457f02008-09-25 20:50:48 +00004229 if (N->getOpcode() != ISD::SCALAR_TO_VECTOR)
4230 return false;
4231 N = N->getOperand(0).getNode();
4232 if (!ISD::isNON_EXTLoad(N))
4233 return false;
4234 if (LD)
4235 *LD = cast<LoadSDNode>(N);
4236 return true;
Evan Cheng5ced1d82006-04-06 23:23:56 +00004237}
4238
Dan Gohman65fd6562011-11-03 21:49:52 +00004239// Test whether the given value is a vector value which will be legalized
4240// into a load.
4241static bool WillBeConstantPoolLoad(SDNode *N) {
4242 if (N->getOpcode() != ISD::BUILD_VECTOR)
4243 return false;
4244
4245 // Check for any non-constant elements.
4246 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
4247 switch (N->getOperand(i).getNode()->getOpcode()) {
4248 case ISD::UNDEF:
4249 case ISD::ConstantFP:
4250 case ISD::Constant:
4251 break;
4252 default:
4253 return false;
4254 }
4255
4256 // Vectors of all-zeros and all-ones are materialized with special
4257 // instructions rather than being loaded.
4258 return !ISD::isBuildVectorAllZeros(N) &&
4259 !ISD::isBuildVectorAllOnes(N);
4260}
4261
Evan Cheng533a0aa2006-04-19 20:35:22 +00004262/// ShouldXformToMOVLP{S|D} - Return true if the node should be transformed to
4263/// match movlp{s|d}. The lower half elements should come from lower half of
4264/// V1 (and in order), and the upper half elements should come from the upper
4265/// half of V2 (and in order). And since V1 will become the source of the
4266/// MOVLP, it must be either a vector load or a scalar load to vector.
Nate Begeman9008ca62009-04-27 18:41:29 +00004267static bool ShouldXformToMOVLP(SDNode *V1, SDNode *V2,
Craig Topperdd637ae2012-02-19 05:41:45 +00004268 ArrayRef<int> Mask, EVT VT) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00004269 if (!VT.is128BitVector())
Bruno Cardoso Lopes59353b42011-08-11 18:59:13 +00004270 return false;
4271
Evan Cheng466685d2006-10-09 20:57:25 +00004272 if (!ISD::isNON_EXTLoad(V1) && !isScalarLoadToVector(V1))
Evan Cheng533a0aa2006-04-19 20:35:22 +00004273 return false;
Evan Cheng23425f52006-10-09 21:39:25 +00004274 // Is V2 is a vector load, don't do this transformation. We will try to use
4275 // load folding shufps op.
Dan Gohman65fd6562011-11-03 21:49:52 +00004276 if (ISD::isNON_EXTLoad(V2) || WillBeConstantPoolLoad(V2))
Evan Cheng23425f52006-10-09 21:39:25 +00004277 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00004278
Bruno Cardoso Lopes59353b42011-08-11 18:59:13 +00004279 unsigned NumElems = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00004280
Evan Cheng533a0aa2006-04-19 20:35:22 +00004281 if (NumElems != 2 && NumElems != 4)
4282 return false;
Nate Begeman5a5ca152009-04-29 05:20:52 +00004283 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
Craig Topperdd637ae2012-02-19 05:41:45 +00004284 if (!isUndefOrEqual(Mask[i], i))
Evan Cheng533a0aa2006-04-19 20:35:22 +00004285 return false;
Chad Rosier238ae312012-04-30 17:47:15 +00004286 for (unsigned i = NumElems/2, e = NumElems; i != e; ++i)
Craig Topperdd637ae2012-02-19 05:41:45 +00004287 if (!isUndefOrEqual(Mask[i], i+NumElems))
Evan Cheng533a0aa2006-04-19 20:35:22 +00004288 return false;
4289 return true;
Evan Cheng5ced1d82006-04-06 23:23:56 +00004290}
4291
Evan Cheng39623da2006-04-20 08:58:49 +00004292/// isSplatVector - Returns true if N is a BUILD_VECTOR node whose elements are
4293/// all the same.
4294static bool isSplatVector(SDNode *N) {
4295 if (N->getOpcode() != ISD::BUILD_VECTOR)
4296 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00004297
Dan Gohman475871a2008-07-27 21:46:04 +00004298 SDValue SplatValue = N->getOperand(0);
Evan Cheng39623da2006-04-20 08:58:49 +00004299 for (unsigned i = 1, e = N->getNumOperands(); i != e; ++i)
4300 if (N->getOperand(i) != SplatValue)
Evan Cheng5ced1d82006-04-06 23:23:56 +00004301 return false;
4302 return true;
4303}
4304
Evan Cheng213d2cf2007-05-17 18:45:50 +00004305/// isZeroShuffle - Returns true if N is a VECTOR_SHUFFLE that can be resolved
Eric Christopherfd179292009-08-27 18:07:15 +00004306/// to an zero vector.
Nate Begeman5a5ca152009-04-29 05:20:52 +00004307/// FIXME: move to dag combiner / method on ShuffleVectorSDNode
Nate Begeman9008ca62009-04-27 18:41:29 +00004308static bool isZeroShuffle(ShuffleVectorSDNode *N) {
Dan Gohman475871a2008-07-27 21:46:04 +00004309 SDValue V1 = N->getOperand(0);
4310 SDValue V2 = N->getOperand(1);
Nate Begeman5a5ca152009-04-29 05:20:52 +00004311 unsigned NumElems = N->getValueType(0).getVectorNumElements();
4312 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004313 int Idx = N->getMaskElt(i);
Nate Begeman5a5ca152009-04-29 05:20:52 +00004314 if (Idx >= (int)NumElems) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004315 unsigned Opc = V2.getOpcode();
Rafael Espindola15684b22009-04-24 12:40:33 +00004316 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V2.getNode()))
4317 continue;
Evan Cheng37b73872009-07-30 08:33:02 +00004318 if (Opc != ISD::BUILD_VECTOR ||
4319 !X86::isZeroNode(V2.getOperand(Idx-NumElems)))
Nate Begeman9008ca62009-04-27 18:41:29 +00004320 return false;
4321 } else if (Idx >= 0) {
4322 unsigned Opc = V1.getOpcode();
4323 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V1.getNode()))
4324 continue;
Evan Cheng37b73872009-07-30 08:33:02 +00004325 if (Opc != ISD::BUILD_VECTOR ||
4326 !X86::isZeroNode(V1.getOperand(Idx)))
Chris Lattner8a594482007-11-25 00:24:49 +00004327 return false;
Evan Cheng213d2cf2007-05-17 18:45:50 +00004328 }
4329 }
4330 return true;
4331}
4332
4333/// getZeroVector - Returns a vector of specified type with all zero elements.
4334///
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00004335static SDValue getZeroVector(EVT VT, const X86Subtarget *Subtarget,
Craig Topper12216172012-01-13 08:12:35 +00004336 SelectionDAG &DAG, DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004337 assert(VT.isVector() && "Expected a vector type");
Craig Topper9d352402012-04-23 07:24:41 +00004338 unsigned Size = VT.getSizeInBits();
Scott Michelfdc40a02009-02-17 22:15:04 +00004339
Dale Johannesen0488fb62010-09-30 23:57:10 +00004340 // Always build SSE zero vectors as <4 x i32> bitcasted
Bruno Cardoso Lopes8c05a852010-08-12 02:06:36 +00004341 // to their dest type. This ensures they get CSE'd.
Dan Gohman475871a2008-07-27 21:46:04 +00004342 SDValue Vec;
Craig Topper9d352402012-04-23 07:24:41 +00004343 if (Size == 128) { // SSE
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00004344 if (Subtarget->hasSSE2()) { // SSE2
Bruno Cardoso Lopes8c05a852010-08-12 02:06:36 +00004345 SDValue Cst = DAG.getTargetConstant(0, MVT::i32);
4346 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
4347 } else { // SSE1
4348 SDValue Cst = DAG.getTargetConstantFP(+0.0, MVT::f32);
4349 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4f32, Cst, Cst, Cst, Cst);
4350 }
Craig Topper9d352402012-04-23 07:24:41 +00004351 } else if (Size == 256) { // AVX
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00004352 if (Subtarget->hasAVX2()) { // AVX2
Craig Topper12216172012-01-13 08:12:35 +00004353 SDValue Cst = DAG.getTargetConstant(0, MVT::i32);
4354 SDValue Ops[] = { Cst, Cst, Cst, Cst, Cst, Cst, Cst, Cst };
4355 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v8i32, Ops, 8);
4356 } else {
4357 // 256-bit logic and arithmetic instructions in AVX are all
4358 // floating-point, no support for integer ops. Emit fp zeroed vectors.
4359 SDValue Cst = DAG.getTargetConstantFP(+0.0, MVT::f32);
4360 SDValue Ops[] = { Cst, Cst, Cst, Cst, Cst, Cst, Cst, Cst };
4361 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v8f32, Ops, 8);
4362 }
Craig Topper9d352402012-04-23 07:24:41 +00004363 } else
4364 llvm_unreachable("Unexpected vector type");
4365
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004366 return DAG.getNode(ISD::BITCAST, dl, VT, Vec);
Evan Cheng213d2cf2007-05-17 18:45:50 +00004367}
4368
Chris Lattner8a594482007-11-25 00:24:49 +00004369/// getOnesVector - Returns a vector of specified type with all bits set.
Craig Topper745a86b2011-11-19 22:34:59 +00004370/// Always build ones vectors as <4 x i32> or <8 x i32>. For 256-bit types with
4371/// no AVX2 supprt, use two <4 x i32> inserted in a <8 x i32> appropriately.
4372/// Then bitcast to their original type, ensuring they get CSE'd.
4373static SDValue getOnesVector(EVT VT, bool HasAVX2, SelectionDAG &DAG,
4374 DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004375 assert(VT.isVector() && "Expected a vector type");
Craig Topper9d352402012-04-23 07:24:41 +00004376 unsigned Size = VT.getSizeInBits();
Scott Michelfdc40a02009-02-17 22:15:04 +00004377
Owen Anderson825b72b2009-08-11 20:47:22 +00004378 SDValue Cst = DAG.getTargetConstant(~0U, MVT::i32);
Craig Topper745a86b2011-11-19 22:34:59 +00004379 SDValue Vec;
Craig Topper9d352402012-04-23 07:24:41 +00004380 if (Size == 256) {
Craig Topper745a86b2011-11-19 22:34:59 +00004381 if (HasAVX2) { // AVX2
4382 SDValue Ops[] = { Cst, Cst, Cst, Cst, Cst, Cst, Cst, Cst };
4383 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v8i32, Ops, 8);
4384 } else { // AVX
4385 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
Craig Topper4c7972d2012-04-22 18:15:59 +00004386 Vec = Concat128BitVectors(Vec, Vec, MVT::v8i32, 8, DAG, dl);
Craig Topper745a86b2011-11-19 22:34:59 +00004387 }
Craig Topper9d352402012-04-23 07:24:41 +00004388 } else if (Size == 128) {
Craig Topper745a86b2011-11-19 22:34:59 +00004389 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
Craig Topper9d352402012-04-23 07:24:41 +00004390 } else
4391 llvm_unreachable("Unexpected vector type");
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +00004392
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004393 return DAG.getNode(ISD::BITCAST, dl, VT, Vec);
Chris Lattner8a594482007-11-25 00:24:49 +00004394}
4395
Evan Cheng39623da2006-04-20 08:58:49 +00004396/// NormalizeMask - V2 is a splat, modify the mask (if needed) so all elements
4397/// that point to V2 points to its first element.
Craig Topper39a9e482012-02-11 06:24:48 +00004398static void NormalizeMask(SmallVectorImpl<int> &Mask, unsigned NumElems) {
Nate Begeman5a5ca152009-04-29 05:20:52 +00004399 for (unsigned i = 0; i != NumElems; ++i) {
Craig Topper39a9e482012-02-11 06:24:48 +00004400 if (Mask[i] > (int)NumElems) {
4401 Mask[i] = NumElems;
Evan Cheng39623da2006-04-20 08:58:49 +00004402 }
Evan Cheng39623da2006-04-20 08:58:49 +00004403 }
Evan Cheng39623da2006-04-20 08:58:49 +00004404}
4405
Evan Cheng017dcc62006-04-21 01:05:10 +00004406/// getMOVLMask - Returns a vector_shuffle mask for an movs{s|d}, movd
4407/// operation of specified width.
Owen Andersone50ed302009-08-10 22:56:29 +00004408static SDValue getMOVL(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00004409 SDValue V2) {
4410 unsigned NumElems = VT.getVectorNumElements();
4411 SmallVector<int, 8> Mask;
4412 Mask.push_back(NumElems);
Evan Cheng39623da2006-04-20 08:58:49 +00004413 for (unsigned i = 1; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004414 Mask.push_back(i);
4415 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Evan Cheng39623da2006-04-20 08:58:49 +00004416}
4417
Nate Begeman9008ca62009-04-27 18:41:29 +00004418/// getUnpackl - Returns a vector_shuffle node for an unpackl operation.
Owen Andersone50ed302009-08-10 22:56:29 +00004419static SDValue getUnpackl(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00004420 SDValue V2) {
4421 unsigned NumElems = VT.getVectorNumElements();
4422 SmallVector<int, 8> Mask;
Evan Chengc575ca22006-04-17 20:43:08 +00004423 for (unsigned i = 0, e = NumElems/2; i != e; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004424 Mask.push_back(i);
4425 Mask.push_back(i + NumElems);
Evan Chengc575ca22006-04-17 20:43:08 +00004426 }
Nate Begeman9008ca62009-04-27 18:41:29 +00004427 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Evan Chengc575ca22006-04-17 20:43:08 +00004428}
4429
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004430/// getUnpackh - Returns a vector_shuffle node for an unpackh operation.
Owen Andersone50ed302009-08-10 22:56:29 +00004431static SDValue getUnpackh(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00004432 SDValue V2) {
4433 unsigned NumElems = VT.getVectorNumElements();
Nate Begeman9008ca62009-04-27 18:41:29 +00004434 SmallVector<int, 8> Mask;
Chad Rosier238ae312012-04-30 17:47:15 +00004435 for (unsigned i = 0, Half = NumElems/2; i != Half; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004436 Mask.push_back(i + Half);
4437 Mask.push_back(i + NumElems + Half);
Evan Cheng39623da2006-04-20 08:58:49 +00004438 }
Nate Begeman9008ca62009-04-27 18:41:29 +00004439 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Chris Lattner62098042008-03-09 01:05:04 +00004440}
4441
Bruno Cardoso Lopes5f1d8ab2011-08-11 02:49:44 +00004442// PromoteSplati8i16 - All i16 and i8 vector types can't be used directly by
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004443// a generic shuffle instruction because the target has no such instructions.
4444// Generate shuffles which repeat i16 and i8 several times until they can be
4445// represented by v4f32 and then be manipulated by target suported shuffles.
Bruno Cardoso Lopes5f1d8ab2011-08-11 02:49:44 +00004446static SDValue PromoteSplati8i16(SDValue V, SelectionDAG &DAG, int &EltNo) {
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004447 EVT VT = V.getValueType();
Nate Begeman9008ca62009-04-27 18:41:29 +00004448 int NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004449 DebugLoc dl = V.getDebugLoc();
Rafael Espindola15684b22009-04-24 12:40:33 +00004450
Nate Begeman9008ca62009-04-27 18:41:29 +00004451 while (NumElems > 4) {
4452 if (EltNo < NumElems/2) {
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004453 V = getUnpackl(DAG, dl, VT, V, V);
Nate Begeman9008ca62009-04-27 18:41:29 +00004454 } else {
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004455 V = getUnpackh(DAG, dl, VT, V, V);
Nate Begeman9008ca62009-04-27 18:41:29 +00004456 EltNo -= NumElems/2;
4457 }
4458 NumElems >>= 1;
4459 }
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004460 return V;
4461}
Eric Christopherfd179292009-08-27 18:07:15 +00004462
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004463/// getLegalSplat - Generate a legal splat with supported x86 shuffles
4464static SDValue getLegalSplat(SelectionDAG &DAG, SDValue V, int EltNo) {
4465 EVT VT = V.getValueType();
4466 DebugLoc dl = V.getDebugLoc();
Craig Topper9d352402012-04-23 07:24:41 +00004467 unsigned Size = VT.getSizeInBits();
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004468
Craig Topper9d352402012-04-23 07:24:41 +00004469 if (Size == 128) {
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004470 V = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, V);
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004471 int SplatMask[4] = { EltNo, EltNo, EltNo, EltNo };
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004472 V = DAG.getVectorShuffle(MVT::v4f32, dl, V, DAG.getUNDEF(MVT::v4f32),
4473 &SplatMask[0]);
Craig Topper9d352402012-04-23 07:24:41 +00004474 } else if (Size == 256) {
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004475 // To use VPERMILPS to splat scalars, the second half of indicies must
4476 // refer to the higher part, which is a duplication of the lower one,
4477 // because VPERMILPS can only handle in-lane permutations.
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004478 int SplatMask[8] = { EltNo, EltNo, EltNo, EltNo,
4479 EltNo+4, EltNo+4, EltNo+4, EltNo+4 };
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004480
4481 V = DAG.getNode(ISD::BITCAST, dl, MVT::v8f32, V);
4482 V = DAG.getVectorShuffle(MVT::v8f32, dl, V, DAG.getUNDEF(MVT::v8f32),
4483 &SplatMask[0]);
Craig Topper9d352402012-04-23 07:24:41 +00004484 } else
4485 llvm_unreachable("Vector size not supported");
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004486
4487 return DAG.getNode(ISD::BITCAST, dl, VT, V);
4488}
4489
Bruno Cardoso Lopes8a5b2622011-08-17 02:29:13 +00004490/// PromoteSplat - Splat is promoted to target supported vector shuffles.
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004491static SDValue PromoteSplat(ShuffleVectorSDNode *SV, SelectionDAG &DAG) {
4492 EVT SrcVT = SV->getValueType(0);
4493 SDValue V1 = SV->getOperand(0);
4494 DebugLoc dl = SV->getDebugLoc();
4495
4496 int EltNo = SV->getSplatIndex();
4497 int NumElems = SrcVT.getVectorNumElements();
4498 unsigned Size = SrcVT.getSizeInBits();
4499
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004500 assert(((Size == 128 && NumElems > 4) || Size == 256) &&
4501 "Unknown how to promote splat for type");
4502
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004503 // Extract the 128-bit part containing the splat element and update
4504 // the splat element index when it refers to the higher register.
4505 if (Size == 256) {
Craig Topper7d1e3dc2012-04-30 05:17:10 +00004506 V1 = Extract128BitVector(V1, EltNo, DAG, dl);
4507 if (EltNo >= NumElems/2)
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004508 EltNo -= NumElems/2;
4509 }
4510
Bruno Cardoso Lopes8a5b2622011-08-17 02:29:13 +00004511 // All i16 and i8 vector types can't be used directly by a generic shuffle
4512 // instruction because the target has no such instruction. Generate shuffles
4513 // which repeat i16 and i8 several times until they fit in i32, and then can
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004514 // be manipulated by target suported shuffles.
Bruno Cardoso Lopes5f1d8ab2011-08-11 02:49:44 +00004515 EVT EltVT = SrcVT.getVectorElementType();
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004516 if (EltVT == MVT::i8 || EltVT == MVT::i16)
Bruno Cardoso Lopes5f1d8ab2011-08-11 02:49:44 +00004517 V1 = PromoteSplati8i16(V1, DAG, EltNo);
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004518
4519 // Recreate the 256-bit vector and place the same 128-bit vector
4520 // into the low and high part. This is necessary because we want
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004521 // to use VPERM* to shuffle the vectors
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004522 if (Size == 256) {
Craig Topper4c7972d2012-04-22 18:15:59 +00004523 V1 = DAG.getNode(ISD::CONCAT_VECTORS, dl, SrcVT, V1, V1);
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004524 }
4525
4526 return getLegalSplat(DAG, V1, EltNo);
Evan Chengc575ca22006-04-17 20:43:08 +00004527}
4528
Evan Chengba05f722006-04-21 23:03:30 +00004529/// getShuffleVectorZeroOrUndef - Return a vector_shuffle of the specified
Chris Lattner8a594482007-11-25 00:24:49 +00004530/// vector of zero or undef vector. This produces a shuffle where the low
4531/// element of V2 is swizzled into the zero/undef vector, landing at element
4532/// 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 +00004533static SDValue getShuffleVectorZeroOrUndef(SDValue V2, unsigned Idx,
Craig Topper12216172012-01-13 08:12:35 +00004534 bool IsZero,
4535 const X86Subtarget *Subtarget,
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00004536 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00004537 EVT VT = V2.getValueType();
Craig Topper12216172012-01-13 08:12:35 +00004538 SDValue V1 = IsZero
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00004539 ? getZeroVector(VT, Subtarget, DAG, V2.getDebugLoc()) : DAG.getUNDEF(VT);
Nate Begeman9008ca62009-04-27 18:41:29 +00004540 unsigned NumElems = VT.getVectorNumElements();
4541 SmallVector<int, 16> MaskVec;
Chris Lattner8a594482007-11-25 00:24:49 +00004542 for (unsigned i = 0; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004543 // If this is the insertion idx, put the low elt of V2 here.
4544 MaskVec.push_back(i == Idx ? NumElems : i);
4545 return DAG.getVectorShuffle(VT, V2.getDebugLoc(), V1, V2, &MaskVec[0]);
Evan Cheng017dcc62006-04-21 01:05:10 +00004546}
4547
Craig Toppera1ffc682012-03-20 06:42:26 +00004548/// getTargetShuffleMask - Calculates the shuffle mask corresponding to the
4549/// target specific opcode. Returns true if the Mask could be calculated.
Craig Topper89f4e662012-03-20 07:17:59 +00004550/// Sets IsUnary to true if only uses one source.
Craig Topperd978c542012-05-06 19:46:21 +00004551static bool getTargetShuffleMask(SDNode *N, MVT VT,
Craig Topper89f4e662012-03-20 07:17:59 +00004552 SmallVectorImpl<int> &Mask, bool &IsUnary) {
Craig Toppera1ffc682012-03-20 06:42:26 +00004553 unsigned NumElems = VT.getVectorNumElements();
4554 SDValue ImmN;
4555
Craig Topper89f4e662012-03-20 07:17:59 +00004556 IsUnary = false;
Craig Toppera1ffc682012-03-20 06:42:26 +00004557 switch(N->getOpcode()) {
4558 case X86ISD::SHUFP:
4559 ImmN = N->getOperand(N->getNumOperands()-1);
4560 DecodeSHUFPMask(VT, cast<ConstantSDNode>(ImmN)->getZExtValue(), Mask);
4561 break;
4562 case X86ISD::UNPCKH:
4563 DecodeUNPCKHMask(VT, Mask);
4564 break;
4565 case X86ISD::UNPCKL:
4566 DecodeUNPCKLMask(VT, Mask);
4567 break;
4568 case X86ISD::MOVHLPS:
4569 DecodeMOVHLPSMask(NumElems, Mask);
4570 break;
4571 case X86ISD::MOVLHPS:
4572 DecodeMOVLHPSMask(NumElems, Mask);
4573 break;
4574 case X86ISD::PSHUFD:
4575 case X86ISD::VPERMILP:
4576 ImmN = N->getOperand(N->getNumOperands()-1);
4577 DecodePSHUFMask(VT, cast<ConstantSDNode>(ImmN)->getZExtValue(), Mask);
Craig Topper89f4e662012-03-20 07:17:59 +00004578 IsUnary = true;
Craig Toppera1ffc682012-03-20 06:42:26 +00004579 break;
4580 case X86ISD::PSHUFHW:
4581 ImmN = N->getOperand(N->getNumOperands()-1);
Craig Toppera9a568a2012-05-02 08:03:44 +00004582 DecodePSHUFHWMask(VT, cast<ConstantSDNode>(ImmN)->getZExtValue(), Mask);
Craig Topper89f4e662012-03-20 07:17:59 +00004583 IsUnary = true;
Craig Toppera1ffc682012-03-20 06:42:26 +00004584 break;
4585 case X86ISD::PSHUFLW:
4586 ImmN = N->getOperand(N->getNumOperands()-1);
Craig Toppera9a568a2012-05-02 08:03:44 +00004587 DecodePSHUFLWMask(VT, cast<ConstantSDNode>(ImmN)->getZExtValue(), Mask);
Craig Topper89f4e662012-03-20 07:17:59 +00004588 IsUnary = true;
Craig Toppera1ffc682012-03-20 06:42:26 +00004589 break;
Craig Topperbdcbcb32012-05-06 18:54:26 +00004590 case X86ISD::VPERMI:
4591 ImmN = N->getOperand(N->getNumOperands()-1);
4592 DecodeVPERMMask(cast<ConstantSDNode>(ImmN)->getZExtValue(), Mask);
4593 IsUnary = true;
4594 break;
Craig Toppera1ffc682012-03-20 06:42:26 +00004595 case X86ISD::MOVSS:
4596 case X86ISD::MOVSD: {
4597 // The index 0 always comes from the first element of the second source,
4598 // this is why MOVSS and MOVSD are used in the first place. The other
4599 // elements come from the other positions of the first source vector
4600 Mask.push_back(NumElems);
4601 for (unsigned i = 1; i != NumElems; ++i) {
4602 Mask.push_back(i);
4603 }
4604 break;
4605 }
4606 case X86ISD::VPERM2X128:
4607 ImmN = N->getOperand(N->getNumOperands()-1);
4608 DecodeVPERM2X128Mask(VT, cast<ConstantSDNode>(ImmN)->getZExtValue(), Mask);
Craig Topper2091df32012-04-17 05:54:54 +00004609 if (Mask.empty()) return false;
Craig Toppera1ffc682012-03-20 06:42:26 +00004610 break;
4611 case X86ISD::MOVDDUP:
4612 case X86ISD::MOVLHPD:
4613 case X86ISD::MOVLPD:
4614 case X86ISD::MOVLPS:
4615 case X86ISD::MOVSHDUP:
4616 case X86ISD::MOVSLDUP:
4617 case X86ISD::PALIGN:
4618 // Not yet implemented
4619 return false;
4620 default: llvm_unreachable("unknown target shuffle node");
4621 }
4622
4623 return true;
4624}
4625
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004626/// getShuffleScalarElt - Returns the scalar element that will make up the ith
4627/// element of the result of the vector shuffle.
Craig Topper3d092db2012-03-21 02:14:01 +00004628static SDValue getShuffleScalarElt(SDNode *N, unsigned Index, SelectionDAG &DAG,
Benjamin Kramer050db522011-03-26 12:38:19 +00004629 unsigned Depth) {
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004630 if (Depth == 6)
4631 return SDValue(); // Limit search depth.
4632
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004633 SDValue V = SDValue(N, 0);
4634 EVT VT = V.getValueType();
4635 unsigned Opcode = V.getOpcode();
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004636
4637 // Recurse into ISD::VECTOR_SHUFFLE node to find scalars.
4638 if (const ShuffleVectorSDNode *SV = dyn_cast<ShuffleVectorSDNode>(N)) {
Craig Topper3d092db2012-03-21 02:14:01 +00004639 int Elt = SV->getMaskElt(Index);
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004640
Craig Topper3d092db2012-03-21 02:14:01 +00004641 if (Elt < 0)
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004642 return DAG.getUNDEF(VT.getVectorElementType());
4643
Craig Topperd156dc12012-02-06 07:17:51 +00004644 unsigned NumElems = VT.getVectorNumElements();
Craig Topper3d092db2012-03-21 02:14:01 +00004645 SDValue NewV = (Elt < (int)NumElems) ? SV->getOperand(0)
4646 : SV->getOperand(1);
4647 return getShuffleScalarElt(NewV.getNode(), Elt % NumElems, DAG, Depth+1);
Evan Chengf26ffe92008-05-29 08:22:04 +00004648 }
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004649
4650 // Recurse into target specific vector shuffles to find scalars.
4651 if (isTargetShuffle(Opcode)) {
Craig Topperd978c542012-05-06 19:46:21 +00004652 MVT ShufVT = V.getValueType().getSimpleVT();
4653 unsigned NumElems = ShufVT.getVectorNumElements();
Craig Toppera1ffc682012-03-20 06:42:26 +00004654 SmallVector<int, 16> ShuffleMask;
Craig Topper89f4e662012-03-20 07:17:59 +00004655 bool IsUnary;
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004656
Craig Topperd978c542012-05-06 19:46:21 +00004657 if (!getTargetShuffleMask(N, ShufVT, ShuffleMask, IsUnary))
Craig Toppera1ffc682012-03-20 06:42:26 +00004658 return SDValue();
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004659
Craig Topper3d092db2012-03-21 02:14:01 +00004660 int Elt = ShuffleMask[Index];
4661 if (Elt < 0)
Craig Topperd978c542012-05-06 19:46:21 +00004662 return DAG.getUNDEF(ShufVT.getVectorElementType());
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004663
Craig Topper3d092db2012-03-21 02:14:01 +00004664 SDValue NewV = (Elt < (int)NumElems) ? N->getOperand(0)
Craig Topperd978c542012-05-06 19:46:21 +00004665 : N->getOperand(1);
Craig Topper3d092db2012-03-21 02:14:01 +00004666 return getShuffleScalarElt(NewV.getNode(), Elt % NumElems, DAG,
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004667 Depth+1);
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004668 }
4669
4670 // Actual nodes that may contain scalar elements
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004671 if (Opcode == ISD::BITCAST) {
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004672 V = V.getOperand(0);
4673 EVT SrcVT = V.getValueType();
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00004674 unsigned NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004675
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00004676 if (!SrcVT.isVector() || SrcVT.getVectorNumElements() != NumElems)
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004677 return SDValue();
4678 }
4679
4680 if (V.getOpcode() == ISD::SCALAR_TO_VECTOR)
4681 return (Index == 0) ? V.getOperand(0)
Craig Topper3d092db2012-03-21 02:14:01 +00004682 : DAG.getUNDEF(VT.getVectorElementType());
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004683
4684 if (V.getOpcode() == ISD::BUILD_VECTOR)
4685 return V.getOperand(Index);
4686
4687 return SDValue();
4688}
4689
4690/// getNumOfConsecutiveZeros - Return the number of elements of a vector
4691/// shuffle operation which come from a consecutively from a zero. The
Chris Lattner7a2bdde2011-04-15 05:18:47 +00004692/// search can start in two different directions, from left or right.
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004693static
Craig Topper3d092db2012-03-21 02:14:01 +00004694unsigned getNumOfConsecutiveZeros(ShuffleVectorSDNode *SVOp, unsigned NumElems,
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004695 bool ZerosFromLeft, SelectionDAG &DAG) {
Craig Topper3d092db2012-03-21 02:14:01 +00004696 unsigned i;
4697 for (i = 0; i != NumElems; ++i) {
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004698 unsigned Index = ZerosFromLeft ? i : NumElems-i-1;
Craig Topper3d092db2012-03-21 02:14:01 +00004699 SDValue Elt = getShuffleScalarElt(SVOp, Index, DAG, 0);
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004700 if (!(Elt.getNode() &&
4701 (Elt.getOpcode() == ISD::UNDEF || X86::isZeroNode(Elt))))
4702 break;
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004703 }
4704
4705 return i;
4706}
4707
Craig Topper3d092db2012-03-21 02:14:01 +00004708/// isShuffleMaskConsecutive - Check if the shuffle mask indicies [MaskI, MaskE)
4709/// correspond consecutively to elements from one of the vector operands,
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004710/// starting from its index OpIdx. Also tell OpNum which source vector operand.
4711static
Craig Topper3d092db2012-03-21 02:14:01 +00004712bool isShuffleMaskConsecutive(ShuffleVectorSDNode *SVOp,
4713 unsigned MaskI, unsigned MaskE, unsigned OpIdx,
4714 unsigned NumElems, unsigned &OpNum) {
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004715 bool SeenV1 = false;
4716 bool SeenV2 = false;
4717
Craig Topper3d092db2012-03-21 02:14:01 +00004718 for (unsigned i = MaskI; i != MaskE; ++i, ++OpIdx) {
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004719 int Idx = SVOp->getMaskElt(i);
4720 // Ignore undef indicies
4721 if (Idx < 0)
4722 continue;
4723
Craig Topper3d092db2012-03-21 02:14:01 +00004724 if (Idx < (int)NumElems)
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004725 SeenV1 = true;
4726 else
4727 SeenV2 = true;
4728
4729 // Only accept consecutive elements from the same vector
4730 if ((Idx % NumElems != OpIdx) || (SeenV1 && SeenV2))
4731 return false;
4732 }
4733
4734 OpNum = SeenV1 ? 0 : 1;
4735 return true;
4736}
4737
4738/// isVectorShiftRight - Returns true if the shuffle can be implemented as a
4739/// logical left shift of a vector.
4740static bool isVectorShiftRight(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
4741 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
4742 unsigned NumElems = SVOp->getValueType(0).getVectorNumElements();
4743 unsigned NumZeros = getNumOfConsecutiveZeros(SVOp, NumElems,
4744 false /* check zeros from right */, DAG);
4745 unsigned OpSrc;
4746
4747 if (!NumZeros)
4748 return false;
4749
4750 // Considering the elements in the mask that are not consecutive zeros,
4751 // check if they consecutively come from only one of the source vectors.
4752 //
4753 // V1 = {X, A, B, C} 0
4754 // \ \ \ /
4755 // vector_shuffle V1, V2 <1, 2, 3, X>
4756 //
4757 if (!isShuffleMaskConsecutive(SVOp,
4758 0, // Mask Start Index
Craig Topper3d092db2012-03-21 02:14:01 +00004759 NumElems-NumZeros, // Mask End Index(exclusive)
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004760 NumZeros, // Where to start looking in the src vector
4761 NumElems, // Number of elements in vector
4762 OpSrc)) // Which source operand ?
4763 return false;
4764
4765 isLeft = false;
4766 ShAmt = NumZeros;
4767 ShVal = SVOp->getOperand(OpSrc);
4768 return true;
4769}
4770
4771/// isVectorShiftLeft - Returns true if the shuffle can be implemented as a
4772/// logical left shift of a vector.
4773static bool isVectorShiftLeft(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
4774 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
4775 unsigned NumElems = SVOp->getValueType(0).getVectorNumElements();
4776 unsigned NumZeros = getNumOfConsecutiveZeros(SVOp, NumElems,
4777 true /* check zeros from left */, DAG);
4778 unsigned OpSrc;
4779
4780 if (!NumZeros)
4781 return false;
4782
4783 // Considering the elements in the mask that are not consecutive zeros,
4784 // check if they consecutively come from only one of the source vectors.
4785 //
4786 // 0 { A, B, X, X } = V2
4787 // / \ / /
4788 // vector_shuffle V1, V2 <X, X, 4, 5>
4789 //
4790 if (!isShuffleMaskConsecutive(SVOp,
4791 NumZeros, // Mask Start Index
Craig Topper3d092db2012-03-21 02:14:01 +00004792 NumElems, // Mask End Index(exclusive)
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004793 0, // Where to start looking in the src vector
4794 NumElems, // Number of elements in vector
4795 OpSrc)) // Which source operand ?
4796 return false;
4797
4798 isLeft = true;
4799 ShAmt = NumZeros;
4800 ShVal = SVOp->getOperand(OpSrc);
4801 return true;
Evan Chengf26ffe92008-05-29 08:22:04 +00004802}
4803
4804/// isVectorShift - Returns true if the shuffle can be implemented as a
4805/// logical left or right shift of a vector.
Nate Begeman9008ca62009-04-27 18:41:29 +00004806static bool isVectorShift(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
Dan Gohman475871a2008-07-27 21:46:04 +00004807 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00004808 // Although the logic below support any bitwidth size, there are no
4809 // shift instructions which handle more than 128-bit vectors.
Craig Topper7a9a28b2012-08-12 02:23:29 +00004810 if (!SVOp->getValueType(0).is128BitVector())
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00004811 return false;
4812
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004813 if (isVectorShiftLeft(SVOp, DAG, isLeft, ShVal, ShAmt) ||
4814 isVectorShiftRight(SVOp, DAG, isLeft, ShVal, ShAmt))
4815 return true;
Evan Chengf26ffe92008-05-29 08:22:04 +00004816
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004817 return false;
Evan Chengf26ffe92008-05-29 08:22:04 +00004818}
4819
Evan Chengc78d3b42006-04-24 18:01:45 +00004820/// LowerBuildVectorv16i8 - Custom lower build_vector of v16i8.
4821///
Dan Gohman475871a2008-07-27 21:46:04 +00004822static SDValue LowerBuildVectorv16i8(SDValue Op, unsigned NonZeros,
Evan Chengc78d3b42006-04-24 18:01:45 +00004823 unsigned NumNonZero, unsigned NumZero,
Dan Gohmand858e902010-04-17 15:26:15 +00004824 SelectionDAG &DAG,
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00004825 const X86Subtarget* Subtarget,
Dan Gohmand858e902010-04-17 15:26:15 +00004826 const TargetLowering &TLI) {
Evan Chengc78d3b42006-04-24 18:01:45 +00004827 if (NumNonZero > 8)
Dan Gohman475871a2008-07-27 21:46:04 +00004828 return SDValue();
Evan Chengc78d3b42006-04-24 18:01:45 +00004829
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004830 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00004831 SDValue V(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00004832 bool First = true;
4833 for (unsigned i = 0; i < 16; ++i) {
4834 bool ThisIsNonZero = (NonZeros & (1 << i)) != 0;
4835 if (ThisIsNonZero && First) {
4836 if (NumZero)
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00004837 V = getZeroVector(MVT::v8i16, Subtarget, DAG, dl);
Evan Chengc78d3b42006-04-24 18:01:45 +00004838 else
Owen Anderson825b72b2009-08-11 20:47:22 +00004839 V = DAG.getUNDEF(MVT::v8i16);
Evan Chengc78d3b42006-04-24 18:01:45 +00004840 First = false;
4841 }
4842
4843 if ((i & 1) != 0) {
Dan Gohman475871a2008-07-27 21:46:04 +00004844 SDValue ThisElt(0, 0), LastElt(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00004845 bool LastIsNonZero = (NonZeros & (1 << (i-1))) != 0;
4846 if (LastIsNonZero) {
Scott Michelfdc40a02009-02-17 22:15:04 +00004847 LastElt = DAG.getNode(ISD::ZERO_EXTEND, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004848 MVT::i16, Op.getOperand(i-1));
Evan Chengc78d3b42006-04-24 18:01:45 +00004849 }
4850 if (ThisIsNonZero) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004851 ThisElt = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i16, Op.getOperand(i));
4852 ThisElt = DAG.getNode(ISD::SHL, dl, MVT::i16,
4853 ThisElt, DAG.getConstant(8, MVT::i8));
Evan Chengc78d3b42006-04-24 18:01:45 +00004854 if (LastIsNonZero)
Owen Anderson825b72b2009-08-11 20:47:22 +00004855 ThisElt = DAG.getNode(ISD::OR, dl, MVT::i16, ThisElt, LastElt);
Evan Chengc78d3b42006-04-24 18:01:45 +00004856 } else
4857 ThisElt = LastElt;
4858
Gabor Greifba36cb52008-08-28 21:40:38 +00004859 if (ThisElt.getNode())
Owen Anderson825b72b2009-08-11 20:47:22 +00004860 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, V, ThisElt,
Chris Lattner0bd48932008-01-17 07:00:52 +00004861 DAG.getIntPtrConstant(i/2));
Evan Chengc78d3b42006-04-24 18:01:45 +00004862 }
4863 }
4864
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004865 return DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, V);
Evan Chengc78d3b42006-04-24 18:01:45 +00004866}
4867
Bill Wendlinga348c562007-03-22 18:42:45 +00004868/// LowerBuildVectorv8i16 - Custom lower build_vector of v8i16.
Evan Chengc78d3b42006-04-24 18:01:45 +00004869///
Dan Gohman475871a2008-07-27 21:46:04 +00004870static SDValue LowerBuildVectorv8i16(SDValue Op, unsigned NonZeros,
Dan Gohmand858e902010-04-17 15:26:15 +00004871 unsigned NumNonZero, unsigned NumZero,
4872 SelectionDAG &DAG,
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00004873 const X86Subtarget* Subtarget,
Dan Gohmand858e902010-04-17 15:26:15 +00004874 const TargetLowering &TLI) {
Evan Chengc78d3b42006-04-24 18:01:45 +00004875 if (NumNonZero > 4)
Dan Gohman475871a2008-07-27 21:46:04 +00004876 return SDValue();
Evan Chengc78d3b42006-04-24 18:01:45 +00004877
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004878 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00004879 SDValue V(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00004880 bool First = true;
4881 for (unsigned i = 0; i < 8; ++i) {
4882 bool isNonZero = (NonZeros & (1 << i)) != 0;
4883 if (isNonZero) {
4884 if (First) {
4885 if (NumZero)
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00004886 V = getZeroVector(MVT::v8i16, Subtarget, DAG, dl);
Evan Chengc78d3b42006-04-24 18:01:45 +00004887 else
Owen Anderson825b72b2009-08-11 20:47:22 +00004888 V = DAG.getUNDEF(MVT::v8i16);
Evan Chengc78d3b42006-04-24 18:01:45 +00004889 First = false;
4890 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004891 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004892 MVT::v8i16, V, Op.getOperand(i),
Chris Lattner0bd48932008-01-17 07:00:52 +00004893 DAG.getIntPtrConstant(i));
Evan Chengc78d3b42006-04-24 18:01:45 +00004894 }
4895 }
4896
4897 return V;
4898}
4899
Evan Chengf26ffe92008-05-29 08:22:04 +00004900/// getVShift - Return a vector logical shift node.
4901///
Owen Andersone50ed302009-08-10 22:56:29 +00004902static SDValue getVShift(bool isLeft, EVT VT, SDValue SrcOp,
Nate Begeman9008ca62009-04-27 18:41:29 +00004903 unsigned NumBits, SelectionDAG &DAG,
4904 const TargetLowering &TLI, DebugLoc dl) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00004905 assert(VT.is128BitVector() && "Unknown type for VShift");
Dale Johannesen0488fb62010-09-30 23:57:10 +00004906 EVT ShVT = MVT::v2i64;
Craig Toppered2e13d2012-01-22 19:15:14 +00004907 unsigned Opc = isLeft ? X86ISD::VSHLDQ : X86ISD::VSRLDQ;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004908 SrcOp = DAG.getNode(ISD::BITCAST, dl, ShVT, SrcOp);
4909 return DAG.getNode(ISD::BITCAST, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00004910 DAG.getNode(Opc, dl, ShVT, SrcOp,
Owen Anderson95771af2011-02-25 21:41:48 +00004911 DAG.getConstant(NumBits,
4912 TLI.getShiftAmountTy(SrcOp.getValueType()))));
Evan Chengf26ffe92008-05-29 08:22:04 +00004913}
4914
Dan Gohman475871a2008-07-27 21:46:04 +00004915SDValue
Evan Chengc3630942009-12-09 21:00:30 +00004916X86TargetLowering::LowerAsSplatVectorLoad(SDValue SrcOp, EVT VT, DebugLoc dl,
Dan Gohmand858e902010-04-17 15:26:15 +00004917 SelectionDAG &DAG) const {
Michael J. Spencerec38de22010-10-10 22:04:20 +00004918
Evan Chengc3630942009-12-09 21:00:30 +00004919 // Check if the scalar load can be widened into a vector load. And if
4920 // the address is "base + cst" see if the cst can be "absorbed" into
4921 // the shuffle mask.
4922 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(SrcOp)) {
4923 SDValue Ptr = LD->getBasePtr();
4924 if (!ISD::isNormalLoad(LD) || LD->isVolatile())
4925 return SDValue();
4926 EVT PVT = LD->getValueType(0);
4927 if (PVT != MVT::i32 && PVT != MVT::f32)
4928 return SDValue();
4929
4930 int FI = -1;
4931 int64_t Offset = 0;
4932 if (FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr)) {
4933 FI = FINode->getIndex();
4934 Offset = 0;
Chris Lattner0a9481f2011-02-13 22:25:43 +00004935 } else if (DAG.isBaseWithConstantOffset(Ptr) &&
Evan Chengc3630942009-12-09 21:00:30 +00004936 isa<FrameIndexSDNode>(Ptr.getOperand(0))) {
4937 FI = cast<FrameIndexSDNode>(Ptr.getOperand(0))->getIndex();
4938 Offset = Ptr.getConstantOperandVal(1);
4939 Ptr = Ptr.getOperand(0);
4940 } else {
4941 return SDValue();
4942 }
4943
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004944 // FIXME: 256-bit vector instructions don't require a strict alignment,
4945 // improve this code to support it better.
4946 unsigned RequiredAlign = VT.getSizeInBits()/8;
Evan Chengc3630942009-12-09 21:00:30 +00004947 SDValue Chain = LD->getChain();
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004948 // Make sure the stack object alignment is at least 16 or 32.
Evan Chengc3630942009-12-09 21:00:30 +00004949 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004950 if (DAG.InferPtrAlignment(Ptr) < RequiredAlign) {
Evan Chengc3630942009-12-09 21:00:30 +00004951 if (MFI->isFixedObjectIndex(FI)) {
Eric Christophere9625cf2010-01-23 06:02:43 +00004952 // Can't change the alignment. FIXME: It's possible to compute
4953 // the exact stack offset and reference FI + adjust offset instead.
4954 // If someone *really* cares about this. That's the way to implement it.
4955 return SDValue();
Evan Chengc3630942009-12-09 21:00:30 +00004956 } else {
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004957 MFI->setObjectAlignment(FI, RequiredAlign);
Evan Chengc3630942009-12-09 21:00:30 +00004958 }
4959 }
4960
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004961 // (Offset % 16 or 32) must be multiple of 4. Then address is then
Evan Chengc3630942009-12-09 21:00:30 +00004962 // Ptr + (Offset & ~15).
4963 if (Offset < 0)
4964 return SDValue();
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004965 if ((Offset % RequiredAlign) & 3)
Evan Chengc3630942009-12-09 21:00:30 +00004966 return SDValue();
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004967 int64_t StartOffset = Offset & ~(RequiredAlign-1);
Evan Chengc3630942009-12-09 21:00:30 +00004968 if (StartOffset)
4969 Ptr = DAG.getNode(ISD::ADD, Ptr.getDebugLoc(), Ptr.getValueType(),
4970 Ptr,DAG.getConstant(StartOffset, Ptr.getValueType()));
4971
4972 int EltNo = (Offset - StartOffset) >> 2;
Craig Topper66ddd152012-04-27 22:54:43 +00004973 unsigned NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004974
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004975 EVT NVT = EVT::getVectorVT(*DAG.getContext(), PVT, NumElems);
4976 SDValue V1 = DAG.getLoad(NVT, dl, Chain, Ptr,
Chris Lattner51abfe42010-09-21 06:02:19 +00004977 LD->getPointerInfo().getWithOffset(StartOffset),
Pete Cooperd752e0f2011-11-08 18:42:53 +00004978 false, false, false, 0);
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004979
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004980 SmallVector<int, 8> Mask;
Craig Topper66ddd152012-04-27 22:54:43 +00004981 for (unsigned i = 0; i != NumElems; ++i)
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004982 Mask.push_back(EltNo);
4983
Craig Toppercc3000632012-01-30 07:50:31 +00004984 return DAG.getVectorShuffle(NVT, dl, V1, DAG.getUNDEF(NVT), &Mask[0]);
Evan Chengc3630942009-12-09 21:00:30 +00004985 }
4986
4987 return SDValue();
4988}
4989
Michael J. Spencerec38de22010-10-10 22:04:20 +00004990/// EltsFromConsecutiveLoads - Given the initializing elements 'Elts' of a
4991/// vector of type 'VT', see if the elements can be replaced by a single large
Nate Begeman1449f292010-03-24 22:19:06 +00004992/// load which has the same value as a build_vector whose operands are 'elts'.
4993///
4994/// Example: <load i32 *a, load i32 *a+4, undef, undef> -> zextload a
Michael J. Spencerec38de22010-10-10 22:04:20 +00004995///
Nate Begeman1449f292010-03-24 22:19:06 +00004996/// FIXME: we'd also like to handle the case where the last elements are zero
4997/// rather than undef via VZEXT_LOAD, but we do not detect that case today.
4998/// There's even a handy isZeroNode for that purpose.
Nate Begemanfdea31a2010-03-24 20:49:50 +00004999static SDValue EltsFromConsecutiveLoads(EVT VT, SmallVectorImpl<SDValue> &Elts,
Chris Lattner88641552010-09-22 00:34:38 +00005000 DebugLoc &DL, SelectionDAG &DAG) {
Nate Begemanfdea31a2010-03-24 20:49:50 +00005001 EVT EltVT = VT.getVectorElementType();
5002 unsigned NumElems = Elts.size();
Michael J. Spencerec38de22010-10-10 22:04:20 +00005003
Nate Begemanfdea31a2010-03-24 20:49:50 +00005004 LoadSDNode *LDBase = NULL;
5005 unsigned LastLoadedElt = -1U;
Michael J. Spencerec38de22010-10-10 22:04:20 +00005006
Nate Begeman1449f292010-03-24 22:19:06 +00005007 // For each element in the initializer, see if we've found a load or an undef.
Michael J. Spencerec38de22010-10-10 22:04:20 +00005008 // If we don't find an initial load element, or later load elements are
Nate Begeman1449f292010-03-24 22:19:06 +00005009 // non-consecutive, bail out.
Nate Begemanfdea31a2010-03-24 20:49:50 +00005010 for (unsigned i = 0; i < NumElems; ++i) {
5011 SDValue Elt = Elts[i];
Michael J. Spencerec38de22010-10-10 22:04:20 +00005012
Nate Begemanfdea31a2010-03-24 20:49:50 +00005013 if (!Elt.getNode() ||
5014 (Elt.getOpcode() != ISD::UNDEF && !ISD::isNON_EXTLoad(Elt.getNode())))
5015 return SDValue();
5016 if (!LDBase) {
5017 if (Elt.getNode()->getOpcode() == ISD::UNDEF)
5018 return SDValue();
5019 LDBase = cast<LoadSDNode>(Elt.getNode());
5020 LastLoadedElt = i;
5021 continue;
5022 }
5023 if (Elt.getOpcode() == ISD::UNDEF)
5024 continue;
5025
5026 LoadSDNode *LD = cast<LoadSDNode>(Elt);
5027 if (!DAG.isConsecutiveLoad(LD, LDBase, EltVT.getSizeInBits()/8, i))
5028 return SDValue();
5029 LastLoadedElt = i;
5030 }
Nate Begeman1449f292010-03-24 22:19:06 +00005031
5032 // If we have found an entire vector of loads and undefs, then return a large
5033 // load of the entire vector width starting at the base pointer. If we found
5034 // consecutive loads for the low half, generate a vzext_load node.
Nate Begemanfdea31a2010-03-24 20:49:50 +00005035 if (LastLoadedElt == NumElems - 1) {
5036 if (DAG.InferPtrAlignment(LDBase->getBasePtr()) >= 16)
Chris Lattner88641552010-09-22 00:34:38 +00005037 return DAG.getLoad(VT, DL, LDBase->getChain(), LDBase->getBasePtr(),
Chris Lattner51abfe42010-09-21 06:02:19 +00005038 LDBase->getPointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00005039 LDBase->isVolatile(), LDBase->isNonTemporal(),
5040 LDBase->isInvariant(), 0);
Chris Lattner88641552010-09-22 00:34:38 +00005041 return DAG.getLoad(VT, DL, LDBase->getChain(), LDBase->getBasePtr(),
Chris Lattner51abfe42010-09-21 06:02:19 +00005042 LDBase->getPointerInfo(),
Nate Begemanfdea31a2010-03-24 20:49:50 +00005043 LDBase->isVolatile(), LDBase->isNonTemporal(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00005044 LDBase->isInvariant(), LDBase->getAlignment());
Craig Topper69947b92012-04-23 06:57:04 +00005045 }
5046 if (NumElems == 4 && LastLoadedElt == 1 &&
5047 DAG.getTargetLoweringInfo().isTypeLegal(MVT::v2i64)) {
Nate Begemanfdea31a2010-03-24 20:49:50 +00005048 SDVTList Tys = DAG.getVTList(MVT::v2i64, MVT::Other);
5049 SDValue Ops[] = { LDBase->getChain(), LDBase->getBasePtr() };
Eli Friedman322ea082011-09-14 23:42:45 +00005050 SDValue ResNode =
5051 DAG.getMemIntrinsicNode(X86ISD::VZEXT_LOAD, DL, Tys, Ops, 2, MVT::i64,
5052 LDBase->getPointerInfo(),
5053 LDBase->getAlignment(),
5054 false/*isVolatile*/, true/*ReadMem*/,
5055 false/*WriteMem*/);
Manman Ren2b7a2e82012-08-31 23:16:57 +00005056
5057 // Make sure the newly-created LOAD is in the same position as LDBase in
5058 // terms of dependency. We create a TokenFactor for LDBase and ResNode, and
5059 // update uses of LDBase's output chain to use the TokenFactor.
5060 if (LDBase->hasAnyUseOfValue(1)) {
5061 SDValue NewChain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other,
5062 SDValue(LDBase, 1), SDValue(ResNode.getNode(), 1));
5063 DAG.ReplaceAllUsesOfValueWith(SDValue(LDBase, 1), NewChain);
5064 DAG.UpdateNodeOperands(NewChain.getNode(), SDValue(LDBase, 1),
5065 SDValue(ResNode.getNode(), 1));
5066 }
5067
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005068 return DAG.getNode(ISD::BITCAST, DL, VT, ResNode);
Nate Begemanfdea31a2010-03-24 20:49:50 +00005069 }
5070 return SDValue();
5071}
5072
Nadav Rotem9d68b062012-04-08 12:54:54 +00005073/// LowerVectorBroadcast - Attempt to use the vbroadcast instruction
5074/// to generate a splat value for the following cases:
5075/// 1. A splat BUILD_VECTOR which uses a single scalar load, or a constant.
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005076/// 2. A splat shuffle which uses a scalar_to_vector node which comes from
Nadav Rotem9d68b062012-04-08 12:54:54 +00005077/// a scalar load, or a constant.
5078/// The VBROADCAST node is returned when a pattern is found,
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00005079/// or SDValue() otherwise.
Nadav Rotem154819d2012-04-09 07:45:58 +00005080SDValue
Craig Topper55b24052012-09-11 06:15:32 +00005081X86TargetLowering::LowerVectorBroadcast(SDValue Op, SelectionDAG &DAG) const {
Craig Toppera9376332012-01-10 08:23:59 +00005082 if (!Subtarget->hasAVX())
5083 return SDValue();
5084
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005085 EVT VT = Op.getValueType();
Nadav Rotem154819d2012-04-09 07:45:58 +00005086 DebugLoc dl = Op.getDebugLoc();
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005087
Craig Topper5da8a802012-05-04 05:49:51 +00005088 assert((VT.is128BitVector() || VT.is256BitVector()) &&
5089 "Unsupported vector type for broadcast.");
5090
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005091 SDValue Ld;
Nadav Rotem9d68b062012-04-08 12:54:54 +00005092 bool ConstSplatVal;
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005093
Nadav Rotem9d68b062012-04-08 12:54:54 +00005094 switch (Op.getOpcode()) {
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005095 default:
5096 // Unknown pattern found.
5097 return SDValue();
5098
5099 case ISD::BUILD_VECTOR: {
5100 // The BUILD_VECTOR node must be a splat.
Nadav Rotem9d68b062012-04-08 12:54:54 +00005101 if (!isSplatVector(Op.getNode()))
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005102 return SDValue();
5103
Nadav Rotem9d68b062012-04-08 12:54:54 +00005104 Ld = Op.getOperand(0);
5105 ConstSplatVal = (Ld.getOpcode() == ISD::Constant ||
5106 Ld.getOpcode() == ISD::ConstantFP);
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00005107
5108 // The suspected load node has several users. Make sure that all
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005109 // of its users are from the BUILD_VECTOR node.
Nadav Rotem9d68b062012-04-08 12:54:54 +00005110 // Constants may have multiple users.
5111 if (!ConstSplatVal && !Ld->hasNUsesOfValue(VT.getVectorNumElements(), 0))
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005112 return SDValue();
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00005113 break;
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005114 }
5115
5116 case ISD::VECTOR_SHUFFLE: {
5117 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
5118
5119 // Shuffles must have a splat mask where the first element is
5120 // broadcasted.
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00005121 if ((!SVOp->isSplat()) || SVOp->getMaskElt(0) != 0)
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005122 return SDValue();
5123
5124 SDValue Sc = Op.getOperand(0);
Nadav Rotemb88e8dd2012-05-10 12:50:02 +00005125 if (Sc.getOpcode() != ISD::SCALAR_TO_VECTOR &&
Elena Demikhovsky8f40f7b2012-07-01 06:12:26 +00005126 Sc.getOpcode() != ISD::BUILD_VECTOR) {
5127
5128 if (!Subtarget->hasAVX2())
5129 return SDValue();
5130
5131 // Use the register form of the broadcast instruction available on AVX2.
5132 if (VT.is256BitVector())
5133 Sc = Extract128BitVector(Sc, 0, DAG, dl);
5134 return DAG.getNode(X86ISD::VBROADCAST, dl, VT, Sc);
5135 }
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005136
5137 Ld = Sc.getOperand(0);
Nadav Rotem9d68b062012-04-08 12:54:54 +00005138 ConstSplatVal = (Ld.getOpcode() == ISD::Constant ||
Nadav Rotem154819d2012-04-09 07:45:58 +00005139 Ld.getOpcode() == ISD::ConstantFP);
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005140
5141 // The scalar_to_vector node and the suspected
5142 // load node must have exactly one user.
Nadav Rotem9d68b062012-04-08 12:54:54 +00005143 // Constants may have multiple users.
5144 if (!ConstSplatVal && (!Sc.hasOneUse() || !Ld.hasOneUse()))
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005145 return SDValue();
5146 break;
5147 }
5148 }
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00005149
Craig Topper7a9a28b2012-08-12 02:23:29 +00005150 bool Is256 = VT.is256BitVector();
Nadav Rotem9d68b062012-04-08 12:54:54 +00005151
5152 // Handle the broadcasting a single constant scalar from the constant pool
5153 // into a vector. On Sandybridge it is still better to load a constant vector
5154 // from the constant pool and not to broadcast it from a scalar.
5155 if (ConstSplatVal && Subtarget->hasAVX2()) {
5156 EVT CVT = Ld.getValueType();
5157 assert(!CVT.isVector() && "Must not broadcast a vector type");
5158 unsigned ScalarSize = CVT.getSizeInBits();
5159
Craig Topper5da8a802012-05-04 05:49:51 +00005160 if (ScalarSize == 32 || (Is256 && ScalarSize == 64)) {
Nadav Rotem9d68b062012-04-08 12:54:54 +00005161 const Constant *C = 0;
5162 if (ConstantSDNode *CI = dyn_cast<ConstantSDNode>(Ld))
5163 C = CI->getConstantIntValue();
5164 else if (ConstantFPSDNode *CF = dyn_cast<ConstantFPSDNode>(Ld))
5165 C = CF->getConstantFPValue();
5166
5167 assert(C && "Invalid constant type");
5168
Nadav Rotem154819d2012-04-09 07:45:58 +00005169 SDValue CP = DAG.getConstantPool(C, getPointerTy());
Nadav Rotem9d68b062012-04-08 12:54:54 +00005170 unsigned Alignment = cast<ConstantPoolSDNode>(CP)->getAlignment();
Nadav Rotem154819d2012-04-09 07:45:58 +00005171 Ld = DAG.getLoad(CVT, dl, DAG.getEntryNode(), CP,
Craig Topper6643d9c2012-05-04 06:18:33 +00005172 MachinePointerInfo::getConstantPool(),
5173 false, false, false, Alignment);
Nadav Rotem9d68b062012-04-08 12:54:54 +00005174
Nadav Rotem9d68b062012-04-08 12:54:54 +00005175 return DAG.getNode(X86ISD::VBROADCAST, dl, VT, Ld);
5176 }
5177 }
5178
Nadav Rotem4fc8a5d2012-05-19 19:57:37 +00005179 bool IsLoad = ISD::isNormalLoad(Ld.getNode());
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005180 unsigned ScalarSize = Ld.getValueType().getSizeInBits();
5181
Nadav Rotem4fc8a5d2012-05-19 19:57:37 +00005182 // Handle AVX2 in-register broadcasts.
5183 if (!IsLoad && Subtarget->hasAVX2() &&
5184 (ScalarSize == 32 || (Is256 && ScalarSize == 64)))
5185 return DAG.getNode(X86ISD::VBROADCAST, dl, VT, Ld);
5186
5187 // The scalar source must be a normal load.
5188 if (!IsLoad)
5189 return SDValue();
5190
Craig Topper5da8a802012-05-04 05:49:51 +00005191 if (ScalarSize == 32 || (Is256 && ScalarSize == 64))
Nadav Rotem9d68b062012-04-08 12:54:54 +00005192 return DAG.getNode(X86ISD::VBROADCAST, dl, VT, Ld);
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005193
Craig Toppera9376332012-01-10 08:23:59 +00005194 // The integer check is needed for the 64-bit into 128-bit so it doesn't match
Craig Topper5da8a802012-05-04 05:49:51 +00005195 // double since there is no vbroadcastsd xmm
Craig Toppera9376332012-01-10 08:23:59 +00005196 if (Subtarget->hasAVX2() && Ld.getValueType().isInteger()) {
Craig Topper5da8a802012-05-04 05:49:51 +00005197 if (ScalarSize == 8 || ScalarSize == 16 || ScalarSize == 64)
Nadav Rotem9d68b062012-04-08 12:54:54 +00005198 return DAG.getNode(X86ISD::VBROADCAST, dl, VT, Ld);
Craig Toppera9376332012-01-10 08:23:59 +00005199 }
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00005200
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005201 // Unsupported broadcast.
5202 return SDValue();
5203}
5204
Evan Chengc3630942009-12-09 21:00:30 +00005205SDValue
Michael Liaofacace82012-10-19 17:15:18 +00005206X86TargetLowering::buildFromShuffleMostly(SDValue Op, SelectionDAG &DAG) const {
5207 EVT VT = Op.getValueType();
5208
5209 // Skip if insert_vec_elt is not supported.
5210 if (!isOperationLegalOrCustom(ISD::INSERT_VECTOR_ELT, VT))
5211 return SDValue();
5212
5213 DebugLoc DL = Op.getDebugLoc();
5214 unsigned NumElems = Op.getNumOperands();
5215
5216 SDValue VecIn1;
5217 SDValue VecIn2;
5218 SmallVector<unsigned, 4> InsertIndices;
5219 SmallVector<int, 8> Mask(NumElems, -1);
5220
5221 for (unsigned i = 0; i != NumElems; ++i) {
5222 unsigned Opc = Op.getOperand(i).getOpcode();
5223
5224 if (Opc == ISD::UNDEF)
5225 continue;
5226
5227 if (Opc != ISD::EXTRACT_VECTOR_ELT) {
5228 // Quit if more than 1 elements need inserting.
5229 if (InsertIndices.size() > 1)
5230 return SDValue();
5231
5232 InsertIndices.push_back(i);
5233 continue;
5234 }
5235
5236 SDValue ExtractedFromVec = Op.getOperand(i).getOperand(0);
5237 SDValue ExtIdx = Op.getOperand(i).getOperand(1);
5238
5239 // Quit if extracted from vector of different type.
5240 if (ExtractedFromVec.getValueType() != VT)
5241 return SDValue();
5242
5243 // Quit if non-constant index.
5244 if (!isa<ConstantSDNode>(ExtIdx))
5245 return SDValue();
5246
5247 if (VecIn1.getNode() == 0)
5248 VecIn1 = ExtractedFromVec;
5249 else if (VecIn1 != ExtractedFromVec) {
5250 if (VecIn2.getNode() == 0)
5251 VecIn2 = ExtractedFromVec;
5252 else if (VecIn2 != ExtractedFromVec)
5253 // Quit if more than 2 vectors to shuffle
5254 return SDValue();
5255 }
5256
5257 unsigned Idx = cast<ConstantSDNode>(ExtIdx)->getZExtValue();
5258
5259 if (ExtractedFromVec == VecIn1)
5260 Mask[i] = Idx;
5261 else if (ExtractedFromVec == VecIn2)
5262 Mask[i] = Idx + NumElems;
5263 }
5264
5265 if (VecIn1.getNode() == 0)
5266 return SDValue();
5267
5268 VecIn2 = VecIn2.getNode() ? VecIn2 : DAG.getUNDEF(VT);
5269 SDValue NV = DAG.getVectorShuffle(VT, DL, VecIn1, VecIn2, &Mask[0]);
5270 for (unsigned i = 0, e = InsertIndices.size(); i != e; ++i) {
5271 unsigned Idx = InsertIndices[i];
5272 NV = DAG.getNode(ISD::INSERT_VECTOR_ELT, DL, VT, NV, Op.getOperand(Idx),
5273 DAG.getIntPtrConstant(Idx));
5274 }
5275
5276 return NV;
5277}
5278
5279SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00005280X86TargetLowering::LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005281 DebugLoc dl = Op.getDebugLoc();
David Greenea5f26012011-02-07 19:36:54 +00005282
David Greenef125a292011-02-08 19:04:41 +00005283 EVT VT = Op.getValueType();
5284 EVT ExtVT = VT.getVectorElementType();
David Greenef125a292011-02-08 19:04:41 +00005285 unsigned NumElems = Op.getNumOperands();
5286
Bruno Cardoso Lopes531f19f2011-08-01 19:51:53 +00005287 // Vectors containing all zeros can be matched by pxor and xorps later
5288 if (ISD::isBuildVectorAllZeros(Op.getNode())) {
5289 // Canonicalize this to <4 x i32> to 1) ensure the zero vectors are CSE'd
5290 // and 2) ensure that i64 scalars are eliminated on x86-32 hosts.
Craig Topper07a27622012-01-22 03:07:48 +00005291 if (VT == MVT::v4i32 || VT == MVT::v8i32)
Chris Lattner8a594482007-11-25 00:24:49 +00005292 return Op;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005293
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00005294 return getZeroVector(VT, Subtarget, DAG, dl);
Chris Lattner8a594482007-11-25 00:24:49 +00005295 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00005296
Bruno Cardoso Lopes531f19f2011-08-01 19:51:53 +00005297 // Vectors containing all ones can be matched by pcmpeqd on 128-bit width
Craig Topper745a86b2011-11-19 22:34:59 +00005298 // vectors or broken into v4i32 operations on 256-bit vectors. AVX2 can use
5299 // vpcmpeqd on 256-bit vectors.
Bruno Cardoso Lopes531f19f2011-08-01 19:51:53 +00005300 if (ISD::isBuildVectorAllOnes(Op.getNode())) {
Craig Topper07a27622012-01-22 03:07:48 +00005301 if (VT == MVT::v4i32 || (VT == MVT::v8i32 && Subtarget->hasAVX2()))
Bruno Cardoso Lopes531f19f2011-08-01 19:51:53 +00005302 return Op;
5303
Craig Topper07a27622012-01-22 03:07:48 +00005304 return getOnesVector(VT, Subtarget->hasAVX2(), DAG, dl);
Bruno Cardoso Lopes531f19f2011-08-01 19:51:53 +00005305 }
5306
Nadav Rotem154819d2012-04-09 07:45:58 +00005307 SDValue Broadcast = LowerVectorBroadcast(Op, DAG);
Nadav Rotem9d68b062012-04-08 12:54:54 +00005308 if (Broadcast.getNode())
5309 return Broadcast;
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005310
Owen Andersone50ed302009-08-10 22:56:29 +00005311 unsigned EVTBits = ExtVT.getSizeInBits();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005312
Evan Cheng0db9fe62006-04-25 20:13:52 +00005313 unsigned NumZero = 0;
5314 unsigned NumNonZero = 0;
5315 unsigned NonZeros = 0;
Chris Lattnerc9517fb2008-03-08 22:48:29 +00005316 bool IsAllConstants = true;
Dan Gohman475871a2008-07-27 21:46:04 +00005317 SmallSet<SDValue, 8> Values;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005318 for (unsigned i = 0; i < NumElems; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00005319 SDValue Elt = Op.getOperand(i);
Evan Chengdb2d5242007-12-12 06:45:40 +00005320 if (Elt.getOpcode() == ISD::UNDEF)
5321 continue;
5322 Values.insert(Elt);
5323 if (Elt.getOpcode() != ISD::Constant &&
5324 Elt.getOpcode() != ISD::ConstantFP)
Chris Lattnerc9517fb2008-03-08 22:48:29 +00005325 IsAllConstants = false;
Evan Cheng37b73872009-07-30 08:33:02 +00005326 if (X86::isZeroNode(Elt))
Evan Chengdb2d5242007-12-12 06:45:40 +00005327 NumZero++;
5328 else {
5329 NonZeros |= (1 << i);
5330 NumNonZero++;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005331 }
5332 }
5333
Chris Lattner97a2a562010-08-26 05:24:29 +00005334 // All undef vector. Return an UNDEF. All zero vectors were handled above.
5335 if (NumNonZero == 0)
Dale Johannesene8d72302009-02-06 23:05:02 +00005336 return DAG.getUNDEF(VT);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005337
Chris Lattner67f453a2008-03-09 05:42:06 +00005338 // Special case for single non-zero, non-undef, element.
Eli Friedman10415532009-06-06 06:05:10 +00005339 if (NumNonZero == 1) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00005340 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dan Gohman475871a2008-07-27 21:46:04 +00005341 SDValue Item = Op.getOperand(Idx);
Scott Michelfdc40a02009-02-17 22:15:04 +00005342
Chris Lattner62098042008-03-09 01:05:04 +00005343 // If this is an insertion of an i64 value on x86-32, and if the top bits of
5344 // the value are obviously zero, truncate the value to i32 and do the
5345 // insertion that way. Only do this if the value is non-constant or if the
5346 // value is a constant being inserted into element 0. It is cheaper to do
5347 // a constant pool load than it is to do a movd + shuffle.
Owen Anderson825b72b2009-08-11 20:47:22 +00005348 if (ExtVT == MVT::i64 && !Subtarget->is64Bit() &&
Chris Lattner62098042008-03-09 01:05:04 +00005349 (!IsAllConstants || Idx == 0)) {
5350 if (DAG.MaskedValueIsZero(Item, APInt::getBitsSet(64, 32, 64))) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00005351 // Handle SSE only.
5352 assert(VT == MVT::v2i64 && "Expected an SSE value type!");
5353 EVT VecVT = MVT::v4i32;
5354 unsigned VecElts = 4;
Scott Michelfdc40a02009-02-17 22:15:04 +00005355
Chris Lattner62098042008-03-09 01:05:04 +00005356 // Truncate the value (which may itself be a constant) to i32, and
5357 // convert it to a vector with movd (S2V+shuffle to zero extend).
Owen Anderson825b72b2009-08-11 20:47:22 +00005358 Item = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Item);
Dale Johannesenace16102009-02-03 19:33:06 +00005359 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VecVT, Item);
Craig Topper12216172012-01-13 08:12:35 +00005360 Item = getShuffleVectorZeroOrUndef(Item, 0, true, Subtarget, DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +00005361
Chris Lattner62098042008-03-09 01:05:04 +00005362 // Now we have our 32-bit value zero extended in the low element of
5363 // a vector. If Idx != 0, swizzle it into place.
5364 if (Idx != 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005365 SmallVector<int, 4> Mask;
5366 Mask.push_back(Idx);
5367 for (unsigned i = 1; i != VecElts; ++i)
5368 Mask.push_back(i);
Craig Topperdf966f62012-04-22 19:17:57 +00005369 Item = DAG.getVectorShuffle(VecVT, dl, Item, DAG.getUNDEF(VecVT),
Nate Begeman9008ca62009-04-27 18:41:29 +00005370 &Mask[0]);
Chris Lattner62098042008-03-09 01:05:04 +00005371 }
Craig Topper07a27622012-01-22 03:07:48 +00005372 return DAG.getNode(ISD::BITCAST, dl, VT, Item);
Chris Lattner62098042008-03-09 01:05:04 +00005373 }
5374 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005375
Chris Lattner19f79692008-03-08 22:59:52 +00005376 // If we have a constant or non-constant insertion into the low element of
5377 // a vector, we can do this with SCALAR_TO_VECTOR + shuffle of zero into
5378 // the rest of the elements. This will be matched as movd/movq/movss/movsd
Eli Friedman10415532009-06-06 06:05:10 +00005379 // depending on what the source datatype is.
5380 if (Idx == 0) {
Craig Topperd62c16e2011-12-29 03:20:51 +00005381 if (NumZero == 0)
Eli Friedman10415532009-06-06 06:05:10 +00005382 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
Craig Topperd62c16e2011-12-29 03:20:51 +00005383
5384 if (ExtVT == MVT::i32 || ExtVT == MVT::f32 || ExtVT == MVT::f64 ||
Owen Anderson825b72b2009-08-11 20:47:22 +00005385 (ExtVT == MVT::i64 && Subtarget->is64Bit())) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00005386 if (VT.is256BitVector()) {
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00005387 SDValue ZeroVec = getZeroVector(VT, Subtarget, DAG, dl);
Nadav Rotem394a1f52012-01-11 14:07:51 +00005388 return DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, ZeroVec,
5389 Item, DAG.getIntPtrConstant(0));
Elena Demikhovsky021c0a22011-12-28 08:14:01 +00005390 }
Craig Topper7a9a28b2012-08-12 02:23:29 +00005391 assert(VT.is128BitVector() && "Expected an SSE value type!");
Eli Friedman10415532009-06-06 06:05:10 +00005392 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
5393 // Turn it into a MOVL (i.e. movss, movsd, or movd) to a zero vector.
Craig Topper12216172012-01-13 08:12:35 +00005394 return getShuffleVectorZeroOrUndef(Item, 0, true, Subtarget, DAG);
Craig Topperd62c16e2011-12-29 03:20:51 +00005395 }
5396
5397 if (ExtVT == MVT::i16 || ExtVT == MVT::i8) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005398 Item = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, Item);
Craig Topper3224e6b2011-12-29 03:09:33 +00005399 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32, Item);
Craig Topper7a9a28b2012-08-12 02:23:29 +00005400 if (VT.is256BitVector()) {
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00005401 SDValue ZeroVec = getZeroVector(MVT::v8i32, Subtarget, DAG, dl);
Craig Topperb14940a2012-04-22 20:55:18 +00005402 Item = Insert128BitVector(ZeroVec, Item, 0, DAG, dl);
Craig Topper19ec2a92011-12-29 03:34:54 +00005403 } else {
Craig Topper7a9a28b2012-08-12 02:23:29 +00005404 assert(VT.is128BitVector() && "Expected an SSE value type!");
Craig Topper12216172012-01-13 08:12:35 +00005405 Item = getShuffleVectorZeroOrUndef(Item, 0, true, Subtarget, DAG);
Craig Topper19ec2a92011-12-29 03:34:54 +00005406 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005407 return DAG.getNode(ISD::BITCAST, dl, VT, Item);
Eli Friedman10415532009-06-06 06:05:10 +00005408 }
Chris Lattnerc9517fb2008-03-08 22:48:29 +00005409 }
Evan Chengf26ffe92008-05-29 08:22:04 +00005410
5411 // Is it a vector logical left shift?
5412 if (NumElems == 2 && Idx == 1 &&
Evan Cheng37b73872009-07-30 08:33:02 +00005413 X86::isZeroNode(Op.getOperand(0)) &&
5414 !X86::isZeroNode(Op.getOperand(1))) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00005415 unsigned NumBits = VT.getSizeInBits();
Evan Chengf26ffe92008-05-29 08:22:04 +00005416 return getVShift(true, VT,
Scott Michelfdc40a02009-02-17 22:15:04 +00005417 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Dale Johannesenb300d2a2009-02-07 00:55:49 +00005418 VT, Op.getOperand(1)),
Dale Johannesenace16102009-02-03 19:33:06 +00005419 NumBits/2, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00005420 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005421
Chris Lattnerc9517fb2008-03-08 22:48:29 +00005422 if (IsAllConstants) // Otherwise, it's better to do a constpool load.
Dan Gohman475871a2008-07-27 21:46:04 +00005423 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005424
Chris Lattner19f79692008-03-08 22:59:52 +00005425 // Otherwise, if this is a vector with i32 or f32 elements, and the element
5426 // is a non-constant being inserted into an element other than the low one,
5427 // we can't use a constant pool load. Instead, use SCALAR_TO_VECTOR (aka
5428 // movd/movss) to move this into the low element, then shuffle it into
5429 // place.
Evan Cheng0db9fe62006-04-25 20:13:52 +00005430 if (EVTBits == 32) {
Dale Johannesenace16102009-02-03 19:33:06 +00005431 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
Scott Michelfdc40a02009-02-17 22:15:04 +00005432
Evan Cheng0db9fe62006-04-25 20:13:52 +00005433 // Turn it into a shuffle of zero and zero-extended scalar to vector.
Craig Topper12216172012-01-13 08:12:35 +00005434 Item = getShuffleVectorZeroOrUndef(Item, 0, NumZero > 0, Subtarget, DAG);
Nate Begeman9008ca62009-04-27 18:41:29 +00005435 SmallVector<int, 8> MaskVec;
Craig Topper31a207a2012-05-04 06:39:13 +00005436 for (unsigned i = 0; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00005437 MaskVec.push_back(i == Idx ? 0 : 1);
5438 return DAG.getVectorShuffle(VT, dl, Item, DAG.getUNDEF(VT), &MaskVec[0]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005439 }
5440 }
5441
Chris Lattner67f453a2008-03-09 05:42:06 +00005442 // Splat is obviously ok. Let legalizer expand it to a shuffle.
Evan Chengc3630942009-12-09 21:00:30 +00005443 if (Values.size() == 1) {
5444 if (EVTBits == 32) {
5445 // Instead of a shuffle like this:
5446 // shuffle (scalar_to_vector (load (ptr + 4))), undef, <0, 0, 0, 0>
5447 // Check if it's possible to issue this instead.
5448 // shuffle (vload ptr)), undef, <1, 1, 1, 1>
5449 unsigned Idx = CountTrailingZeros_32(NonZeros);
5450 SDValue Item = Op.getOperand(Idx);
5451 if (Op.getNode()->isOnlyUserOf(Item.getNode()))
5452 return LowerAsSplatVectorLoad(Item, VT, dl, DAG);
5453 }
Dan Gohman475871a2008-07-27 21:46:04 +00005454 return SDValue();
Evan Chengc3630942009-12-09 21:00:30 +00005455 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005456
Dan Gohmana3941172007-07-24 22:55:08 +00005457 // A vector full of immediates; various special cases are already
5458 // handled, so this is best done with a single constant-pool load.
Chris Lattnerc9517fb2008-03-08 22:48:29 +00005459 if (IsAllConstants)
Dan Gohman475871a2008-07-27 21:46:04 +00005460 return SDValue();
Dan Gohmana3941172007-07-24 22:55:08 +00005461
Bruno Cardoso Lopes6683efb2011-07-22 00:15:07 +00005462 // For AVX-length vectors, build the individual 128-bit pieces and use
5463 // shuffles to put them in place.
Craig Topper7a9a28b2012-08-12 02:23:29 +00005464 if (VT.is256BitVector()) {
Bruno Cardoso Lopes6683efb2011-07-22 00:15:07 +00005465 SmallVector<SDValue, 32> V;
Craig Topperfa5b70e2012-02-03 06:32:21 +00005466 for (unsigned i = 0; i != NumElems; ++i)
Bruno Cardoso Lopes6683efb2011-07-22 00:15:07 +00005467 V.push_back(Op.getOperand(i));
5468
5469 EVT HVT = EVT::getVectorVT(*DAG.getContext(), ExtVT, NumElems/2);
5470
5471 // Build both the lower and upper subvector.
5472 SDValue Lower = DAG.getNode(ISD::BUILD_VECTOR, dl, HVT, &V[0], NumElems/2);
5473 SDValue Upper = DAG.getNode(ISD::BUILD_VECTOR, dl, HVT, &V[NumElems / 2],
5474 NumElems/2);
5475
5476 // Recreate the wider vector with the lower and upper part.
Craig Topper4c7972d2012-04-22 18:15:59 +00005477 return Concat128BitVectors(Lower, Upper, VT, NumElems, DAG, dl);
Bruno Cardoso Lopes6683efb2011-07-22 00:15:07 +00005478 }
5479
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00005480 // Let legalizer expand 2-wide build_vectors.
Evan Cheng7e2ff772008-05-08 00:57:18 +00005481 if (EVTBits == 64) {
5482 if (NumNonZero == 1) {
5483 // One half is zero or undef.
5484 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dale Johannesenace16102009-02-03 19:33:06 +00005485 SDValue V2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT,
Evan Cheng7e2ff772008-05-08 00:57:18 +00005486 Op.getOperand(Idx));
Craig Topper12216172012-01-13 08:12:35 +00005487 return getShuffleVectorZeroOrUndef(V2, Idx, true, Subtarget, DAG);
Evan Cheng7e2ff772008-05-08 00:57:18 +00005488 }
Dan Gohman475871a2008-07-27 21:46:04 +00005489 return SDValue();
Evan Cheng7e2ff772008-05-08 00:57:18 +00005490 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00005491
5492 // If element VT is < 32 bits, convert it to inserts into a zero vector.
Bill Wendling826f36f2007-03-28 00:57:11 +00005493 if (EVTBits == 8 && NumElems == 16) {
Dan Gohman475871a2008-07-27 21:46:04 +00005494 SDValue V = LowerBuildVectorv16i8(Op, NonZeros,NumNonZero,NumZero, DAG,
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00005495 Subtarget, *this);
Gabor Greifba36cb52008-08-28 21:40:38 +00005496 if (V.getNode()) return V;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005497 }
5498
Bill Wendling826f36f2007-03-28 00:57:11 +00005499 if (EVTBits == 16 && NumElems == 8) {
Dan Gohman475871a2008-07-27 21:46:04 +00005500 SDValue V = LowerBuildVectorv8i16(Op, NonZeros,NumNonZero,NumZero, DAG,
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00005501 Subtarget, *this);
Gabor Greifba36cb52008-08-28 21:40:38 +00005502 if (V.getNode()) return V;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005503 }
5504
5505 // If element VT is == 32 bits, turn it into a number of shuffles.
Benjamin Kramer9c683542012-01-30 15:16:21 +00005506 SmallVector<SDValue, 8> V(NumElems);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005507 if (NumElems == 4 && NumZero > 0) {
5508 for (unsigned i = 0; i < 4; ++i) {
5509 bool isZero = !(NonZeros & (1 << i));
5510 if (isZero)
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00005511 V[i] = getZeroVector(VT, Subtarget, DAG, dl);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005512 else
Dale Johannesenace16102009-02-03 19:33:06 +00005513 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(i));
Evan Cheng0db9fe62006-04-25 20:13:52 +00005514 }
5515
5516 for (unsigned i = 0; i < 2; ++i) {
5517 switch ((NonZeros & (0x3 << i*2)) >> (i*2)) {
5518 default: break;
5519 case 0:
5520 V[i] = V[i*2]; // Must be a zero vector.
5521 break;
5522 case 1:
Nate Begeman9008ca62009-04-27 18:41:29 +00005523 V[i] = getMOVL(DAG, dl, VT, V[i*2+1], V[i*2]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005524 break;
5525 case 2:
Nate Begeman9008ca62009-04-27 18:41:29 +00005526 V[i] = getMOVL(DAG, dl, VT, V[i*2], V[i*2+1]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005527 break;
5528 case 3:
Nate Begeman9008ca62009-04-27 18:41:29 +00005529 V[i] = getUnpackl(DAG, dl, VT, V[i*2], V[i*2+1]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005530 break;
5531 }
5532 }
5533
Benjamin Kramer9c683542012-01-30 15:16:21 +00005534 bool Reverse1 = (NonZeros & 0x3) == 2;
5535 bool Reverse2 = ((NonZeros & (0x3 << 2)) >> 2) == 2;
5536 int MaskVec[] = {
5537 Reverse1 ? 1 : 0,
5538 Reverse1 ? 0 : 1,
Benjamin Kramer630ecf02012-01-30 20:01:35 +00005539 static_cast<int>(Reverse2 ? NumElems+1 : NumElems),
5540 static_cast<int>(Reverse2 ? NumElems : NumElems+1)
Benjamin Kramer9c683542012-01-30 15:16:21 +00005541 };
Nate Begeman9008ca62009-04-27 18:41:29 +00005542 return DAG.getVectorShuffle(VT, dl, V[0], V[1], &MaskVec[0]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005543 }
5544
Craig Topper7a9a28b2012-08-12 02:23:29 +00005545 if (Values.size() > 1 && VT.is128BitVector()) {
Nate Begemanfdea31a2010-03-24 20:49:50 +00005546 // Check for a build vector of consecutive loads.
5547 for (unsigned i = 0; i < NumElems; ++i)
5548 V[i] = Op.getOperand(i);
Michael J. Spencerec38de22010-10-10 22:04:20 +00005549
Nate Begemanfdea31a2010-03-24 20:49:50 +00005550 // Check for elements which are consecutive loads.
5551 SDValue LD = EltsFromConsecutiveLoads(VT, V, dl, DAG);
5552 if (LD.getNode())
5553 return LD;
Michael J. Spencerec38de22010-10-10 22:04:20 +00005554
Michael Liaofacace82012-10-19 17:15:18 +00005555 // Check for a build vector from mostly shuffle plus few inserting.
5556 SDValue Sh = buildFromShuffleMostly(Op, DAG);
5557 if (Sh.getNode())
5558 return Sh;
5559
Michael J. Spencerec38de22010-10-10 22:04:20 +00005560 // For SSE 4.1, use insertps to put the high elements into the low element.
Craig Topperd0a31172012-01-10 06:37:29 +00005561 if (getSubtarget()->hasSSE41()) {
Chris Lattner24faf612010-08-28 17:59:08 +00005562 SDValue Result;
5563 if (Op.getOperand(0).getOpcode() != ISD::UNDEF)
5564 Result = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(0));
5565 else
5566 Result = DAG.getUNDEF(VT);
Michael J. Spencerec38de22010-10-10 22:04:20 +00005567
Chris Lattner24faf612010-08-28 17:59:08 +00005568 for (unsigned i = 1; i < NumElems; ++i) {
5569 if (Op.getOperand(i).getOpcode() == ISD::UNDEF) continue;
5570 Result = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, Result,
Nate Begeman9008ca62009-04-27 18:41:29 +00005571 Op.getOperand(i), DAG.getIntPtrConstant(i));
Chris Lattner24faf612010-08-28 17:59:08 +00005572 }
5573 return Result;
Nate Begeman9008ca62009-04-27 18:41:29 +00005574 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00005575
Chris Lattner6e80e442010-08-28 17:15:43 +00005576 // Otherwise, expand into a number of unpckl*, start by extending each of
5577 // our (non-undef) elements to the full vector width with the element in the
5578 // bottom slot of the vector (which generates no code for SSE).
5579 for (unsigned i = 0; i < NumElems; ++i) {
5580 if (Op.getOperand(i).getOpcode() != ISD::UNDEF)
5581 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(i));
5582 else
5583 V[i] = DAG.getUNDEF(VT);
5584 }
5585
5586 // Next, we iteratively mix elements, e.g. for v4f32:
Evan Cheng0db9fe62006-04-25 20:13:52 +00005587 // Step 1: unpcklps 0, 2 ==> X: <?, ?, 2, 0>
5588 // : unpcklps 1, 3 ==> Y: <?, ?, 3, 1>
5589 // Step 2: unpcklps X, Y ==> <3, 2, 1, 0>
Chris Lattner6e80e442010-08-28 17:15:43 +00005590 unsigned EltStride = NumElems >> 1;
5591 while (EltStride != 0) {
Chris Lattner3ddcc432010-08-28 17:28:30 +00005592 for (unsigned i = 0; i < EltStride; ++i) {
5593 // If V[i+EltStride] is undef and this is the first round of mixing,
5594 // then it is safe to just drop this shuffle: V[i] is already in the
5595 // right place, the one element (since it's the first round) being
5596 // inserted as undef can be dropped. This isn't safe for successive
5597 // rounds because they will permute elements within both vectors.
5598 if (V[i+EltStride].getOpcode() == ISD::UNDEF &&
5599 EltStride == NumElems/2)
5600 continue;
Michael J. Spencerec38de22010-10-10 22:04:20 +00005601
Chris Lattner6e80e442010-08-28 17:15:43 +00005602 V[i] = getUnpackl(DAG, dl, VT, V[i], V[i + EltStride]);
Chris Lattner3ddcc432010-08-28 17:28:30 +00005603 }
Chris Lattner6e80e442010-08-28 17:15:43 +00005604 EltStride >>= 1;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005605 }
5606 return V[0];
5607 }
Dan Gohman475871a2008-07-27 21:46:04 +00005608 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005609}
5610
Bruno Cardoso Lopes8af24512011-08-01 21:54:02 +00005611// LowerAVXCONCAT_VECTORS - 256-bit AVX can use the vinsertf128 instruction
5612// to create 256-bit vectors from two other 128-bit ones.
5613static SDValue LowerAVXCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) {
5614 DebugLoc dl = Op.getDebugLoc();
5615 EVT ResVT = Op.getValueType();
5616
Craig Topper7a9a28b2012-08-12 02:23:29 +00005617 assert(ResVT.is256BitVector() && "Value type must be 256-bit wide");
Bruno Cardoso Lopes8af24512011-08-01 21:54:02 +00005618
5619 SDValue V1 = Op.getOperand(0);
5620 SDValue V2 = Op.getOperand(1);
5621 unsigned NumElems = ResVT.getVectorNumElements();
5622
Craig Topper4c7972d2012-04-22 18:15:59 +00005623 return Concat128BitVectors(V1, V2, ResVT, NumElems, DAG, dl);
Bruno Cardoso Lopes8af24512011-08-01 21:54:02 +00005624}
5625
Craig Topper55b24052012-09-11 06:15:32 +00005626static SDValue LowerCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) {
Bruno Cardoso Lopes8af24512011-08-01 21:54:02 +00005627 assert(Op.getNumOperands() == 2);
Bruno Cardoso Lopes8af24512011-08-01 21:54:02 +00005628
5629 // 256-bit AVX can use the vinsertf128 instruction to create 256-bit vectors
5630 // from two other 128-bit ones.
5631 return LowerAVXCONCAT_VECTORS(Op, DAG);
5632}
5633
Nadav Roteme80aa7c2012-04-09 08:33:21 +00005634// Try to lower a shuffle node into a simple blend instruction.
Craig Topper55b24052012-09-11 06:15:32 +00005635static SDValue
5636LowerVECTOR_SHUFFLEtoBlend(ShuffleVectorSDNode *SVOp,
5637 const X86Subtarget *Subtarget, SelectionDAG &DAG) {
Nadav Roteme80aa7c2012-04-09 08:33:21 +00005638 SDValue V1 = SVOp->getOperand(0);
5639 SDValue V2 = SVOp->getOperand(1);
5640 DebugLoc dl = SVOp->getDebugLoc();
Craig Topper708e44f2012-04-23 07:36:33 +00005641 MVT VT = SVOp->getValueType(0).getSimpleVT();
Craig Topper1842ba02012-04-23 06:38:28 +00005642 unsigned NumElems = VT.getVectorNumElements();
Nadav Roteme80aa7c2012-04-09 08:33:21 +00005643
Nadav Roteme6113782012-04-11 06:40:27 +00005644 if (!Subtarget->hasSSE41())
Nadav Roteme80aa7c2012-04-09 08:33:21 +00005645 return SDValue();
5646
Craig Topper1842ba02012-04-23 06:38:28 +00005647 unsigned ISDNo = 0;
Nadav Roteme6113782012-04-11 06:40:27 +00005648 MVT OpTy;
5649
Craig Topper708e44f2012-04-23 07:36:33 +00005650 switch (VT.SimpleTy) {
Nadav Roteme6113782012-04-11 06:40:27 +00005651 default: return SDValue();
5652 case MVT::v8i16:
Craig Topper1842ba02012-04-23 06:38:28 +00005653 ISDNo = X86ISD::BLENDPW;
5654 OpTy = MVT::v8i16;
5655 break;
Nadav Roteme6113782012-04-11 06:40:27 +00005656 case MVT::v4i32:
5657 case MVT::v4f32:
Craig Topper1842ba02012-04-23 06:38:28 +00005658 ISDNo = X86ISD::BLENDPS;
5659 OpTy = MVT::v4f32;
5660 break;
Nadav Roteme6113782012-04-11 06:40:27 +00005661 case MVT::v2i64:
5662 case MVT::v2f64:
Craig Topper1842ba02012-04-23 06:38:28 +00005663 ISDNo = X86ISD::BLENDPD;
5664 OpTy = MVT::v2f64;
5665 break;
Nadav Roteme6113782012-04-11 06:40:27 +00005666 case MVT::v8i32:
5667 case MVT::v8f32:
Craig Topper1842ba02012-04-23 06:38:28 +00005668 if (!Subtarget->hasAVX())
5669 return SDValue();
5670 ISDNo = X86ISD::BLENDPS;
5671 OpTy = MVT::v8f32;
5672 break;
Nadav Roteme6113782012-04-11 06:40:27 +00005673 case MVT::v4i64:
5674 case MVT::v4f64:
Craig Topper1842ba02012-04-23 06:38:28 +00005675 if (!Subtarget->hasAVX())
5676 return SDValue();
5677 ISDNo = X86ISD::BLENDPD;
5678 OpTy = MVT::v4f64;
5679 break;
Nadav Roteme6113782012-04-11 06:40:27 +00005680 }
5681 assert(ISDNo && "Invalid Op Number");
5682
5683 unsigned MaskVals = 0;
Nadav Roteme80aa7c2012-04-09 08:33:21 +00005684
Craig Topper1842ba02012-04-23 06:38:28 +00005685 for (unsigned i = 0; i != NumElems; ++i) {
Nadav Roteme80aa7c2012-04-09 08:33:21 +00005686 int EltIdx = SVOp->getMaskElt(i);
Craig Topper1842ba02012-04-23 06:38:28 +00005687 if (EltIdx == (int)i || EltIdx < 0)
Nadav Roteme6113782012-04-11 06:40:27 +00005688 MaskVals |= (1<<i);
Craig Topper1842ba02012-04-23 06:38:28 +00005689 else if (EltIdx == (int)(i + NumElems))
Nadav Roteme6113782012-04-11 06:40:27 +00005690 continue; // Bit is set to zero;
Craig Topper1842ba02012-04-23 06:38:28 +00005691 else
5692 return SDValue();
Nadav Roteme80aa7c2012-04-09 08:33:21 +00005693 }
5694
Nadav Roteme6113782012-04-11 06:40:27 +00005695 V1 = DAG.getNode(ISD::BITCAST, dl, OpTy, V1);
5696 V2 = DAG.getNode(ISD::BITCAST, dl, OpTy, V2);
5697 SDValue Ret = DAG.getNode(ISDNo, dl, OpTy, V1, V2,
5698 DAG.getConstant(MaskVals, MVT::i32));
5699 return DAG.getNode(ISD::BITCAST, dl, VT, Ret);
Nadav Roteme80aa7c2012-04-09 08:33:21 +00005700}
5701
Nate Begemanb9a47b82009-02-23 08:49:38 +00005702// v8i16 shuffles - Prefer shuffles in the following order:
5703// 1. [all] pshuflw, pshufhw, optional move
5704// 2. [ssse3] 1 x pshufb
5705// 3. [ssse3] 2 x pshufb + 1 x por
5706// 4. [all] mov + pshuflw + pshufhw + N x (pextrw + pinsrw)
Craig Topper55b24052012-09-11 06:15:32 +00005707static SDValue
5708LowerVECTOR_SHUFFLEv8i16(SDValue Op, const X86Subtarget *Subtarget,
5709 SelectionDAG &DAG) {
Bruno Cardoso Lopesbf8154a2010-08-21 01:32:18 +00005710 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Nate Begeman9008ca62009-04-27 18:41:29 +00005711 SDValue V1 = SVOp->getOperand(0);
5712 SDValue V2 = SVOp->getOperand(1);
5713 DebugLoc dl = SVOp->getDebugLoc();
Nate Begemanb9a47b82009-02-23 08:49:38 +00005714 SmallVector<int, 8> MaskVals;
Evan Cheng14b32e12007-12-11 01:46:18 +00005715
Nate Begemanb9a47b82009-02-23 08:49:38 +00005716 // Determine if more than 1 of the words in each of the low and high quadwords
5717 // of the result come from the same quadword of one of the two inputs. Undef
5718 // mask values count as coming from any quadword, for better codegen.
Benjamin Kramer003fad92011-10-15 13:28:31 +00005719 unsigned LoQuad[] = { 0, 0, 0, 0 };
5720 unsigned HiQuad[] = { 0, 0, 0, 0 };
Benjamin Kramer699ddcb2012-02-06 12:06:18 +00005721 std::bitset<4> InputQuads;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005722 for (unsigned i = 0; i < 8; ++i) {
Benjamin Kramer003fad92011-10-15 13:28:31 +00005723 unsigned *Quad = i < 4 ? LoQuad : HiQuad;
Nate Begeman9008ca62009-04-27 18:41:29 +00005724 int EltIdx = SVOp->getMaskElt(i);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005725 MaskVals.push_back(EltIdx);
5726 if (EltIdx < 0) {
5727 ++Quad[0];
5728 ++Quad[1];
5729 ++Quad[2];
5730 ++Quad[3];
Evan Cheng14b32e12007-12-11 01:46:18 +00005731 continue;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005732 }
5733 ++Quad[EltIdx / 4];
5734 InputQuads.set(EltIdx / 4);
Evan Cheng14b32e12007-12-11 01:46:18 +00005735 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00005736
Nate Begemanb9a47b82009-02-23 08:49:38 +00005737 int BestLoQuad = -1;
Evan Cheng14b32e12007-12-11 01:46:18 +00005738 unsigned MaxQuad = 1;
5739 for (unsigned i = 0; i < 4; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005740 if (LoQuad[i] > MaxQuad) {
5741 BestLoQuad = i;
5742 MaxQuad = LoQuad[i];
Evan Cheng14b32e12007-12-11 01:46:18 +00005743 }
Evan Cheng8a86c3f2007-12-07 08:07:39 +00005744 }
5745
Nate Begemanb9a47b82009-02-23 08:49:38 +00005746 int BestHiQuad = -1;
Evan Cheng14b32e12007-12-11 01:46:18 +00005747 MaxQuad = 1;
5748 for (unsigned i = 0; i < 4; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005749 if (HiQuad[i] > MaxQuad) {
5750 BestHiQuad = i;
5751 MaxQuad = HiQuad[i];
Evan Cheng14b32e12007-12-11 01:46:18 +00005752 }
5753 }
5754
Nate Begemanb9a47b82009-02-23 08:49:38 +00005755 // For SSSE3, If all 8 words of the result come from only 1 quadword of each
Eric Christopherfd179292009-08-27 18:07:15 +00005756 // of the two input vectors, shuffle them into one input vector so only a
Nate Begemanb9a47b82009-02-23 08:49:38 +00005757 // single pshufb instruction is necessary. If There are more than 2 input
5758 // quads, disable the next transformation since it does not help SSSE3.
5759 bool V1Used = InputQuads[0] || InputQuads[1];
5760 bool V2Used = InputQuads[2] || InputQuads[3];
Craig Topperd0a31172012-01-10 06:37:29 +00005761 if (Subtarget->hasSSSE3()) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005762 if (InputQuads.count() == 2 && V1Used && V2Used) {
Benjamin Kramer699ddcb2012-02-06 12:06:18 +00005763 BestLoQuad = InputQuads[0] ? 0 : 1;
5764 BestHiQuad = InputQuads[2] ? 2 : 3;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005765 }
5766 if (InputQuads.count() > 2) {
5767 BestLoQuad = -1;
5768 BestHiQuad = -1;
5769 }
5770 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00005771
Nate Begemanb9a47b82009-02-23 08:49:38 +00005772 // If BestLoQuad or BestHiQuad are set, shuffle the quads together and update
5773 // the shuffle mask. If a quad is scored as -1, that means that it contains
5774 // words from all 4 input quadwords.
5775 SDValue NewV;
5776 if (BestLoQuad >= 0 || BestHiQuad >= 0) {
Benjamin Kramer9c683542012-01-30 15:16:21 +00005777 int MaskV[] = {
5778 BestLoQuad < 0 ? 0 : BestLoQuad,
5779 BestHiQuad < 0 ? 1 : BestHiQuad
5780 };
Eric Christopherfd179292009-08-27 18:07:15 +00005781 NewV = DAG.getVectorShuffle(MVT::v2i64, dl,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005782 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64, V1),
5783 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64, V2), &MaskV[0]);
5784 NewV = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, NewV);
Evan Cheng14b32e12007-12-11 01:46:18 +00005785
Nate Begemanb9a47b82009-02-23 08:49:38 +00005786 // Rewrite the MaskVals and assign NewV to V1 if NewV now contains all the
5787 // source words for the shuffle, to aid later transformations.
5788 bool AllWordsInNewV = true;
Mon P Wang37b9a192009-03-11 06:35:11 +00005789 bool InOrder[2] = { true, true };
Evan Cheng14b32e12007-12-11 01:46:18 +00005790 for (unsigned i = 0; i != 8; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005791 int idx = MaskVals[i];
Mon P Wang37b9a192009-03-11 06:35:11 +00005792 if (idx != (int)i)
5793 InOrder[i/4] = false;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005794 if (idx < 0 || (idx/4) == BestLoQuad || (idx/4) == BestHiQuad)
Evan Cheng14b32e12007-12-11 01:46:18 +00005795 continue;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005796 AllWordsInNewV = false;
5797 break;
Evan Cheng14b32e12007-12-11 01:46:18 +00005798 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00005799
Nate Begemanb9a47b82009-02-23 08:49:38 +00005800 bool pshuflw = AllWordsInNewV, pshufhw = AllWordsInNewV;
5801 if (AllWordsInNewV) {
5802 for (int i = 0; i != 8; ++i) {
5803 int idx = MaskVals[i];
5804 if (idx < 0)
Evan Cheng14b32e12007-12-11 01:46:18 +00005805 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00005806 idx = MaskVals[i] = (idx / 4) == BestLoQuad ? (idx & 3) : (idx & 3) + 4;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005807 if ((idx != i) && idx < 4)
5808 pshufhw = false;
5809 if ((idx != i) && idx > 3)
5810 pshuflw = false;
Evan Cheng14b32e12007-12-11 01:46:18 +00005811 }
Nate Begemanb9a47b82009-02-23 08:49:38 +00005812 V1 = NewV;
5813 V2Used = false;
5814 BestLoQuad = 0;
5815 BestHiQuad = 1;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00005816 }
Evan Cheng14b32e12007-12-11 01:46:18 +00005817
Nate Begemanb9a47b82009-02-23 08:49:38 +00005818 // If we've eliminated the use of V2, and the new mask is a pshuflw or
5819 // pshufhw, that's as cheap as it gets. Return the new shuffle.
Mon P Wang37b9a192009-03-11 06:35:11 +00005820 if ((pshufhw && InOrder[0]) || (pshuflw && InOrder[1])) {
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00005821 unsigned Opc = pshufhw ? X86ISD::PSHUFHW : X86ISD::PSHUFLW;
5822 unsigned TargetMask = 0;
5823 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV,
Owen Anderson825b72b2009-08-11 20:47:22 +00005824 DAG.getUNDEF(MVT::v8i16), &MaskVals[0]);
Craig Topperdd637ae2012-02-19 05:41:45 +00005825 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(NewV.getNode());
5826 TargetMask = pshufhw ? getShufflePSHUFHWImmediate(SVOp):
5827 getShufflePSHUFLWImmediate(SVOp);
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00005828 V1 = NewV.getOperand(0);
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00005829 return getTargetShuffleNode(Opc, dl, MVT::v8i16, V1, TargetMask, DAG);
Evan Cheng14b32e12007-12-11 01:46:18 +00005830 }
Evan Cheng14b32e12007-12-11 01:46:18 +00005831 }
Eric Christopherfd179292009-08-27 18:07:15 +00005832
Nate Begemanb9a47b82009-02-23 08:49:38 +00005833 // If we have SSSE3, and all words of the result are from 1 input vector,
5834 // case 2 is generated, otherwise case 3 is generated. If no SSSE3
5835 // is present, fall back to case 4.
Craig Topperd0a31172012-01-10 06:37:29 +00005836 if (Subtarget->hasSSSE3()) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005837 SmallVector<SDValue,16> pshufbMask;
Eric Christopherfd179292009-08-27 18:07:15 +00005838
Nate Begemanb9a47b82009-02-23 08:49:38 +00005839 // If we have elements from both input vectors, set the high bit of the
Eric Christopherfd179292009-08-27 18:07:15 +00005840 // shuffle mask element to zero out elements that come from V2 in the V1
Nate Begemanb9a47b82009-02-23 08:49:38 +00005841 // mask, and elements that come from V1 in the V2 mask, so that the two
5842 // results can be OR'd together.
5843 bool TwoInputs = V1Used && V2Used;
5844 for (unsigned i = 0; i != 8; ++i) {
5845 int EltIdx = MaskVals[i] * 2;
Craig Topperbe97ae92012-05-18 07:07:36 +00005846 int Idx0 = (TwoInputs && (EltIdx >= 16)) ? 0x80 : EltIdx;
5847 int Idx1 = (TwoInputs && (EltIdx >= 16)) ? 0x80 : EltIdx+1;
5848 pshufbMask.push_back(DAG.getConstant(Idx0, MVT::i8));
5849 pshufbMask.push_back(DAG.getConstant(Idx1, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005850 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005851 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, V1);
Eric Christopherfd179292009-08-27 18:07:15 +00005852 V1 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V1,
Evan Chenga87008d2009-02-25 22:49:59 +00005853 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005854 MVT::v16i8, &pshufbMask[0], 16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005855 if (!TwoInputs)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005856 return DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V1);
Eric Christopherfd179292009-08-27 18:07:15 +00005857
Nate Begemanb9a47b82009-02-23 08:49:38 +00005858 // Calculate the shuffle mask for the second input, shuffle it, and
5859 // OR it with the first shuffled input.
5860 pshufbMask.clear();
5861 for (unsigned i = 0; i != 8; ++i) {
5862 int EltIdx = MaskVals[i] * 2;
Craig Topperbe97ae92012-05-18 07:07:36 +00005863 int Idx0 = (EltIdx < 16) ? 0x80 : EltIdx - 16;
5864 int Idx1 = (EltIdx < 16) ? 0x80 : EltIdx - 15;
5865 pshufbMask.push_back(DAG.getConstant(Idx0, MVT::i8));
5866 pshufbMask.push_back(DAG.getConstant(Idx1, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005867 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005868 V2 = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, V2);
Eric Christopherfd179292009-08-27 18:07:15 +00005869 V2 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V2,
Evan Chenga87008d2009-02-25 22:49:59 +00005870 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005871 MVT::v16i8, &pshufbMask[0], 16));
5872 V1 = DAG.getNode(ISD::OR, dl, MVT::v16i8, V1, V2);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005873 return DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005874 }
5875
5876 // If BestLoQuad >= 0, generate a pshuflw to put the low elements in order,
5877 // and update MaskVals with new element order.
Benjamin Kramer9c683542012-01-30 15:16:21 +00005878 std::bitset<8> InOrder;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005879 if (BestLoQuad >= 0) {
Benjamin Kramer9c683542012-01-30 15:16:21 +00005880 int MaskV[] = { -1, -1, -1, -1, 4, 5, 6, 7 };
Nate Begemanb9a47b82009-02-23 08:49:38 +00005881 for (int i = 0; i != 4; ++i) {
5882 int idx = MaskVals[i];
5883 if (idx < 0) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005884 InOrder.set(i);
5885 } else if ((idx / 4) == BestLoQuad) {
Benjamin Kramer9c683542012-01-30 15:16:21 +00005886 MaskV[i] = idx & 3;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005887 InOrder.set(i);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005888 }
5889 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005890 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV, DAG.getUNDEF(MVT::v8i16),
Nate Begeman9008ca62009-04-27 18:41:29 +00005891 &MaskV[0]);
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00005892
Craig Topperdd637ae2012-02-19 05:41:45 +00005893 if (NewV.getOpcode() == ISD::VECTOR_SHUFFLE && Subtarget->hasSSSE3()) {
5894 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(NewV.getNode());
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00005895 NewV = getTargetShuffleNode(X86ISD::PSHUFLW, dl, MVT::v8i16,
Craig Topperdd637ae2012-02-19 05:41:45 +00005896 NewV.getOperand(0),
5897 getShufflePSHUFLWImmediate(SVOp), DAG);
5898 }
Nate Begemanb9a47b82009-02-23 08:49:38 +00005899 }
Eric Christopherfd179292009-08-27 18:07:15 +00005900
Nate Begemanb9a47b82009-02-23 08:49:38 +00005901 // If BestHi >= 0, generate a pshufhw to put the high elements in order,
5902 // and update MaskVals with the new element order.
5903 if (BestHiQuad >= 0) {
Benjamin Kramer9c683542012-01-30 15:16:21 +00005904 int MaskV[] = { 0, 1, 2, 3, -1, -1, -1, -1 };
Nate Begemanb9a47b82009-02-23 08:49:38 +00005905 for (unsigned i = 4; i != 8; ++i) {
5906 int idx = MaskVals[i];
5907 if (idx < 0) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005908 InOrder.set(i);
5909 } else if ((idx / 4) == BestHiQuad) {
Benjamin Kramer9c683542012-01-30 15:16:21 +00005910 MaskV[i] = (idx & 3) + 4;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005911 InOrder.set(i);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005912 }
5913 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005914 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV, DAG.getUNDEF(MVT::v8i16),
Nate Begeman9008ca62009-04-27 18:41:29 +00005915 &MaskV[0]);
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00005916
Craig Topperdd637ae2012-02-19 05:41:45 +00005917 if (NewV.getOpcode() == ISD::VECTOR_SHUFFLE && Subtarget->hasSSSE3()) {
5918 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(NewV.getNode());
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00005919 NewV = getTargetShuffleNode(X86ISD::PSHUFHW, dl, MVT::v8i16,
Craig Topperdd637ae2012-02-19 05:41:45 +00005920 NewV.getOperand(0),
5921 getShufflePSHUFHWImmediate(SVOp), DAG);
5922 }
Nate Begemanb9a47b82009-02-23 08:49:38 +00005923 }
Eric Christopherfd179292009-08-27 18:07:15 +00005924
Nate Begemanb9a47b82009-02-23 08:49:38 +00005925 // In case BestHi & BestLo were both -1, which means each quadword has a word
5926 // from each of the four input quadwords, calculate the InOrder bitvector now
5927 // before falling through to the insert/extract cleanup.
5928 if (BestLoQuad == -1 && BestHiQuad == -1) {
5929 NewV = V1;
5930 for (int i = 0; i != 8; ++i)
5931 if (MaskVals[i] < 0 || MaskVals[i] == i)
5932 InOrder.set(i);
5933 }
Eric Christopherfd179292009-08-27 18:07:15 +00005934
Nate Begemanb9a47b82009-02-23 08:49:38 +00005935 // The other elements are put in the right place using pextrw and pinsrw.
5936 for (unsigned i = 0; i != 8; ++i) {
5937 if (InOrder[i])
5938 continue;
5939 int EltIdx = MaskVals[i];
5940 if (EltIdx < 0)
5941 continue;
Craig Topper6643d9c2012-05-04 06:18:33 +00005942 SDValue ExtOp = (EltIdx < 8) ?
5943 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, V1,
5944 DAG.getIntPtrConstant(EltIdx)) :
5945 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, V2,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005946 DAG.getIntPtrConstant(EltIdx - 8));
Owen Anderson825b72b2009-08-11 20:47:22 +00005947 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, ExtOp,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005948 DAG.getIntPtrConstant(i));
5949 }
5950 return NewV;
5951}
5952
5953// v16i8 shuffles - Prefer shuffles in the following order:
5954// 1. [ssse3] 1 x pshufb
5955// 2. [ssse3] 2 x pshufb + 1 x por
5956// 3. [all] v8i16 shuffle + N x pextrw + rotate + pinsrw
5957static
Nate Begeman9008ca62009-04-27 18:41:29 +00005958SDValue LowerVECTOR_SHUFFLEv16i8(ShuffleVectorSDNode *SVOp,
Dan Gohmand858e902010-04-17 15:26:15 +00005959 SelectionDAG &DAG,
5960 const X86TargetLowering &TLI) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005961 SDValue V1 = SVOp->getOperand(0);
5962 SDValue V2 = SVOp->getOperand(1);
5963 DebugLoc dl = SVOp->getDebugLoc();
Benjamin Kramered4c8c62012-01-15 13:16:05 +00005964 ArrayRef<int> MaskVals = SVOp->getMask();
Eric Christopherfd179292009-08-27 18:07:15 +00005965
Nate Begemanb9a47b82009-02-23 08:49:38 +00005966 // If we have SSSE3, case 1 is generated when all result bytes come from
Eric Christopherfd179292009-08-27 18:07:15 +00005967 // one of the inputs. Otherwise, case 2 is generated. If no SSSE3 is
Nate Begemanb9a47b82009-02-23 08:49:38 +00005968 // present, fall back to case 3.
Eric Christopherfd179292009-08-27 18:07:15 +00005969
Nate Begemanb9a47b82009-02-23 08:49:38 +00005970 // If SSSE3, use 1 pshufb instruction per vector with elements in the result.
Craig Topperd0a31172012-01-10 06:37:29 +00005971 if (TLI.getSubtarget()->hasSSSE3()) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005972 SmallVector<SDValue,16> pshufbMask;
Eric Christopherfd179292009-08-27 18:07:15 +00005973
Nate Begemanb9a47b82009-02-23 08:49:38 +00005974 // If all result elements are from one input vector, then only translate
Eric Christopherfd179292009-08-27 18:07:15 +00005975 // undef mask values to 0x80 (zero out result) in the pshufb mask.
Nate Begemanb9a47b82009-02-23 08:49:38 +00005976 //
5977 // Otherwise, we have elements from both input vectors, and must zero out
5978 // elements that come from V2 in the first mask, and V1 in the second mask
5979 // so that we can OR them together.
Nate Begemanb9a47b82009-02-23 08:49:38 +00005980 for (unsigned i = 0; i != 16; ++i) {
5981 int EltIdx = MaskVals[i];
Craig Topperb82b5ab2012-05-18 06:42:06 +00005982 if (EltIdx < 0 || EltIdx >= 16)
5983 EltIdx = 0x80;
Owen Anderson825b72b2009-08-11 20:47:22 +00005984 pshufbMask.push_back(DAG.getConstant(EltIdx, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005985 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005986 V1 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V1,
Evan Chenga87008d2009-02-25 22:49:59 +00005987 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005988 MVT::v16i8, &pshufbMask[0], 16));
Michael Liao265bcb12012-08-31 20:12:31 +00005989
5990 // As PSHUFB will zero elements with negative indices, it's safe to ignore
5991 // the 2nd operand if it's undefined or zero.
5992 if (V2.getOpcode() == ISD::UNDEF ||
5993 ISD::isBuildVectorAllZeros(V2.getNode()))
Nate Begemanb9a47b82009-02-23 08:49:38 +00005994 return V1;
Eric Christopherfd179292009-08-27 18:07:15 +00005995
Nate Begemanb9a47b82009-02-23 08:49:38 +00005996 // Calculate the shuffle mask for the second input, shuffle it, and
5997 // OR it with the first shuffled input.
5998 pshufbMask.clear();
5999 for (unsigned i = 0; i != 16; ++i) {
6000 int EltIdx = MaskVals[i];
Craig Topperb82b5ab2012-05-18 06:42:06 +00006001 EltIdx = (EltIdx < 16) ? 0x80 : EltIdx - 16;
Craig Topper85b9e562012-05-22 06:09:38 +00006002 pshufbMask.push_back(DAG.getConstant(EltIdx, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00006003 }
Owen Anderson825b72b2009-08-11 20:47:22 +00006004 V2 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V2,
Evan Chenga87008d2009-02-25 22:49:59 +00006005 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00006006 MVT::v16i8, &pshufbMask[0], 16));
6007 return DAG.getNode(ISD::OR, dl, MVT::v16i8, V1, V2);
Nate Begemanb9a47b82009-02-23 08:49:38 +00006008 }
Eric Christopherfd179292009-08-27 18:07:15 +00006009
Nate Begemanb9a47b82009-02-23 08:49:38 +00006010 // No SSSE3 - Calculate in place words and then fix all out of place words
6011 // With 0-16 extracts & inserts. Worst case is 16 bytes out of order from
6012 // the 16 different words that comprise the two doublequadword input vectors.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006013 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V1);
6014 V2 = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V2);
Craig Topperb82b5ab2012-05-18 06:42:06 +00006015 SDValue NewV = V1;
Nate Begemanb9a47b82009-02-23 08:49:38 +00006016 for (int i = 0; i != 8; ++i) {
6017 int Elt0 = MaskVals[i*2];
6018 int Elt1 = MaskVals[i*2+1];
Eric Christopherfd179292009-08-27 18:07:15 +00006019
Nate Begemanb9a47b82009-02-23 08:49:38 +00006020 // This word of the result is all undef, skip it.
6021 if (Elt0 < 0 && Elt1 < 0)
6022 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00006023
Nate Begemanb9a47b82009-02-23 08:49:38 +00006024 // This word of the result is already in the correct place, skip it.
Craig Topperb82b5ab2012-05-18 06:42:06 +00006025 if ((Elt0 == i*2) && (Elt1 == i*2+1))
Nate Begemanb9a47b82009-02-23 08:49:38 +00006026 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00006027
Nate Begemanb9a47b82009-02-23 08:49:38 +00006028 SDValue Elt0Src = Elt0 < 16 ? V1 : V2;
6029 SDValue Elt1Src = Elt1 < 16 ? V1 : V2;
6030 SDValue InsElt;
Mon P Wang6b3ef692009-03-11 18:47:57 +00006031
6032 // If Elt0 and Elt1 are defined, are consecutive, and can be load
6033 // using a single extract together, load it and store it.
6034 if ((Elt0 >= 0) && ((Elt0 + 1) == Elt1) && ((Elt0 & 1) == 0)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00006035 InsElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, Elt1Src,
Mon P Wang6b3ef692009-03-11 18:47:57 +00006036 DAG.getIntPtrConstant(Elt1 / 2));
Owen Anderson825b72b2009-08-11 20:47:22 +00006037 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, InsElt,
Mon P Wang6b3ef692009-03-11 18:47:57 +00006038 DAG.getIntPtrConstant(i));
6039 continue;
6040 }
6041
Nate Begemanb9a47b82009-02-23 08:49:38 +00006042 // If Elt1 is defined, extract it from the appropriate source. If the
Mon P Wang6b3ef692009-03-11 18:47:57 +00006043 // source byte is not also odd, shift the extracted word left 8 bits
6044 // otherwise clear the bottom 8 bits if we need to do an or.
Nate Begemanb9a47b82009-02-23 08:49:38 +00006045 if (Elt1 >= 0) {
Owen Anderson825b72b2009-08-11 20:47:22 +00006046 InsElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, Elt1Src,
Nate Begemanb9a47b82009-02-23 08:49:38 +00006047 DAG.getIntPtrConstant(Elt1 / 2));
6048 if ((Elt1 & 1) == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00006049 InsElt = DAG.getNode(ISD::SHL, dl, MVT::i16, InsElt,
Owen Anderson95771af2011-02-25 21:41:48 +00006050 DAG.getConstant(8,
6051 TLI.getShiftAmountTy(InsElt.getValueType())));
Mon P Wang6b3ef692009-03-11 18:47:57 +00006052 else if (Elt0 >= 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00006053 InsElt = DAG.getNode(ISD::AND, dl, MVT::i16, InsElt,
6054 DAG.getConstant(0xFF00, MVT::i16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00006055 }
6056 // If Elt0 is defined, extract it from the appropriate source. If the
6057 // source byte is not also even, shift the extracted word right 8 bits. If
6058 // Elt1 was also defined, OR the extracted values together before
6059 // inserting them in the result.
6060 if (Elt0 >= 0) {
Owen Anderson825b72b2009-08-11 20:47:22 +00006061 SDValue InsElt0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16,
Nate Begemanb9a47b82009-02-23 08:49:38 +00006062 Elt0Src, DAG.getIntPtrConstant(Elt0 / 2));
6063 if ((Elt0 & 1) != 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00006064 InsElt0 = DAG.getNode(ISD::SRL, dl, MVT::i16, InsElt0,
Owen Anderson95771af2011-02-25 21:41:48 +00006065 DAG.getConstant(8,
6066 TLI.getShiftAmountTy(InsElt0.getValueType())));
Mon P Wang6b3ef692009-03-11 18:47:57 +00006067 else if (Elt1 >= 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00006068 InsElt0 = DAG.getNode(ISD::AND, dl, MVT::i16, InsElt0,
6069 DAG.getConstant(0x00FF, MVT::i16));
6070 InsElt = Elt1 >= 0 ? DAG.getNode(ISD::OR, dl, MVT::i16, InsElt, InsElt0)
Nate Begemanb9a47b82009-02-23 08:49:38 +00006071 : InsElt0;
6072 }
Owen Anderson825b72b2009-08-11 20:47:22 +00006073 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, InsElt,
Nate Begemanb9a47b82009-02-23 08:49:38 +00006074 DAG.getIntPtrConstant(i));
6075 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006076 return DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, NewV);
Evan Cheng14b32e12007-12-11 01:46:18 +00006077}
6078
Elena Demikhovsky41789462012-09-06 12:42:01 +00006079// v32i8 shuffles - Translate to VPSHUFB if possible.
6080static
6081SDValue LowerVECTOR_SHUFFLEv32i8(ShuffleVectorSDNode *SVOp,
Craig Topper55b24052012-09-11 06:15:32 +00006082 const X86Subtarget *Subtarget,
6083 SelectionDAG &DAG) {
Elena Demikhovsky41789462012-09-06 12:42:01 +00006084 EVT VT = SVOp->getValueType(0);
6085 SDValue V1 = SVOp->getOperand(0);
6086 SDValue V2 = SVOp->getOperand(1);
6087 DebugLoc dl = SVOp->getDebugLoc();
Elena Demikhovsky8100d242012-09-10 12:13:11 +00006088 SmallVector<int, 32> MaskVals(SVOp->getMask().begin(), SVOp->getMask().end());
Elena Demikhovsky41789462012-09-06 12:42:01 +00006089
6090 bool V2IsUndef = V2.getOpcode() == ISD::UNDEF;
Elena Demikhovsky8100d242012-09-10 12:13:11 +00006091 bool V1IsAllZero = ISD::isBuildVectorAllZeros(V1.getNode());
6092 bool V2IsAllZero = ISD::isBuildVectorAllZeros(V2.getNode());
Elena Demikhovsky41789462012-09-06 12:42:01 +00006093
Michael Liao471b9172012-10-03 23:43:52 +00006094 // VPSHUFB may be generated if
Elena Demikhovsky8100d242012-09-10 12:13:11 +00006095 // (1) one of input vector is undefined or zeroinitializer.
6096 // The mask value 0x80 puts 0 in the corresponding slot of the vector.
6097 // And (2) the mask indexes don't cross the 128-bit lane.
Craig Topper55b24052012-09-11 06:15:32 +00006098 if (VT != MVT::v32i8 || !Subtarget->hasAVX2() ||
Elena Demikhovsky8100d242012-09-10 12:13:11 +00006099 (!V2IsUndef && !V2IsAllZero && !V1IsAllZero))
Elena Demikhovsky41789462012-09-06 12:42:01 +00006100 return SDValue();
6101
Elena Demikhovsky8100d242012-09-10 12:13:11 +00006102 if (V1IsAllZero && !V2IsAllZero) {
6103 CommuteVectorShuffleMask(MaskVals, 32);
6104 V1 = V2;
6105 }
6106 SmallVector<SDValue, 32> pshufbMask;
Elena Demikhovsky41789462012-09-06 12:42:01 +00006107 for (unsigned i = 0; i != 32; i++) {
6108 int EltIdx = MaskVals[i];
6109 if (EltIdx < 0 || EltIdx >= 32)
6110 EltIdx = 0x80;
6111 else {
6112 if ((EltIdx >= 16 && i < 16) || (EltIdx < 16 && i >= 16))
6113 // Cross lane is not allowed.
6114 return SDValue();
6115 EltIdx &= 0xf;
6116 }
6117 pshufbMask.push_back(DAG.getConstant(EltIdx, MVT::i8));
6118 }
6119 return DAG.getNode(X86ISD::PSHUFB, dl, MVT::v32i8, V1,
6120 DAG.getNode(ISD::BUILD_VECTOR, dl,
6121 MVT::v32i8, &pshufbMask[0], 32));
6122}
6123
Evan Cheng7a831ce2007-12-15 03:00:47 +00006124/// RewriteAsNarrowerShuffle - Try rewriting v8i16 and v16i8 shuffles as 4 wide
Bruno Cardoso Lopes0a7dd4f2010-09-08 18:12:31 +00006125/// ones, or rewriting v4i32 / v4f32 as 2 wide ones if possible. This can be
Evan Cheng7a831ce2007-12-15 03:00:47 +00006126/// done when every pair / quad of shuffle mask elements point to elements in
6127/// the right sequence. e.g.
Bruno Cardoso Lopes0a7dd4f2010-09-08 18:12:31 +00006128/// vector_shuffle X, Y, <2, 3, | 10, 11, | 0, 1, | 14, 15>
Evan Cheng14b32e12007-12-11 01:46:18 +00006129static
Nate Begeman9008ca62009-04-27 18:41:29 +00006130SDValue RewriteAsNarrowerShuffle(ShuffleVectorSDNode *SVOp,
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006131 SelectionDAG &DAG, DebugLoc dl) {
Craig Topper11ac1f82012-05-04 04:08:44 +00006132 MVT VT = SVOp->getValueType(0).getSimpleVT();
Nate Begeman9008ca62009-04-27 18:41:29 +00006133 unsigned NumElems = VT.getVectorNumElements();
Craig Topper11ac1f82012-05-04 04:08:44 +00006134 MVT NewVT;
6135 unsigned Scale;
6136 switch (VT.SimpleTy) {
Craig Topperabb94d02012-02-05 03:43:23 +00006137 default: llvm_unreachable("Unexpected!");
Craig Topperf3640d72012-05-04 04:44:49 +00006138 case MVT::v4f32: NewVT = MVT::v2f64; Scale = 2; break;
6139 case MVT::v4i32: NewVT = MVT::v2i64; Scale = 2; break;
6140 case MVT::v8i16: NewVT = MVT::v4i32; Scale = 2; break;
6141 case MVT::v16i8: NewVT = MVT::v4i32; Scale = 4; break;
6142 case MVT::v16i16: NewVT = MVT::v8i32; Scale = 2; break;
6143 case MVT::v32i8: NewVT = MVT::v8i32; Scale = 4; break;
Evan Cheng7a831ce2007-12-15 03:00:47 +00006144 }
6145
Nate Begeman9008ca62009-04-27 18:41:29 +00006146 SmallVector<int, 8> MaskVec;
Craig Topper11ac1f82012-05-04 04:08:44 +00006147 for (unsigned i = 0; i != NumElems; i += Scale) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006148 int StartIdx = -1;
Craig Topper11ac1f82012-05-04 04:08:44 +00006149 for (unsigned j = 0; j != Scale; ++j) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006150 int EltIdx = SVOp->getMaskElt(i+j);
6151 if (EltIdx < 0)
Evan Cheng14b32e12007-12-11 01:46:18 +00006152 continue;
Craig Topper11ac1f82012-05-04 04:08:44 +00006153 if (StartIdx < 0)
6154 StartIdx = (EltIdx / Scale);
6155 if (EltIdx != (int)(StartIdx*Scale + j))
Dan Gohman475871a2008-07-27 21:46:04 +00006156 return SDValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00006157 }
Craig Topper11ac1f82012-05-04 04:08:44 +00006158 MaskVec.push_back(StartIdx);
Evan Cheng8a86c3f2007-12-07 08:07:39 +00006159 }
6160
Craig Topper11ac1f82012-05-04 04:08:44 +00006161 SDValue V1 = DAG.getNode(ISD::BITCAST, dl, NewVT, SVOp->getOperand(0));
6162 SDValue V2 = DAG.getNode(ISD::BITCAST, dl, NewVT, SVOp->getOperand(1));
Nate Begeman9008ca62009-04-27 18:41:29 +00006163 return DAG.getVectorShuffle(NewVT, dl, V1, V2, &MaskVec[0]);
Evan Cheng8a86c3f2007-12-07 08:07:39 +00006164}
6165
Evan Chengd880b972008-05-09 21:53:03 +00006166/// getVZextMovL - Return a zero-extending vector move low node.
Evan Cheng7e2ff772008-05-08 00:57:18 +00006167///
Owen Andersone50ed302009-08-10 22:56:29 +00006168static SDValue getVZextMovL(EVT VT, EVT OpVT,
Nate Begeman9008ca62009-04-27 18:41:29 +00006169 SDValue SrcOp, SelectionDAG &DAG,
6170 const X86Subtarget *Subtarget, DebugLoc dl) {
Owen Anderson825b72b2009-08-11 20:47:22 +00006171 if (VT == MVT::v2f64 || VT == MVT::v4f32) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00006172 LoadSDNode *LD = NULL;
Gabor Greifba36cb52008-08-28 21:40:38 +00006173 if (!isScalarLoadToVector(SrcOp.getNode(), &LD))
Evan Cheng7e2ff772008-05-08 00:57:18 +00006174 LD = dyn_cast<LoadSDNode>(SrcOp);
6175 if (!LD) {
6176 // movssrr and movsdrr do not clear top bits. Try to use movd, movq
6177 // instead.
Owen Anderson766b5ef2009-08-11 21:59:30 +00006178 MVT ExtVT = (OpVT == MVT::v2f64) ? MVT::i64 : MVT::i32;
Duncan Sandscdfad362010-11-03 12:17:33 +00006179 if ((ExtVT != MVT::i64 || Subtarget->is64Bit()) &&
Evan Cheng7e2ff772008-05-08 00:57:18 +00006180 SrcOp.getOpcode() == ISD::SCALAR_TO_VECTOR &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006181 SrcOp.getOperand(0).getOpcode() == ISD::BITCAST &&
Owen Anderson766b5ef2009-08-11 21:59:30 +00006182 SrcOp.getOperand(0).getOperand(0).getValueType() == ExtVT) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00006183 // PR2108
Owen Anderson825b72b2009-08-11 20:47:22 +00006184 OpVT = (OpVT == MVT::v2f64) ? MVT::v2i64 : MVT::v4i32;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006185 return DAG.getNode(ISD::BITCAST, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00006186 DAG.getNode(X86ISD::VZEXT_MOVL, dl, OpVT,
6187 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
6188 OpVT,
Gabor Greif327ef032008-08-28 23:19:51 +00006189 SrcOp.getOperand(0)
6190 .getOperand(0))));
Evan Cheng7e2ff772008-05-08 00:57:18 +00006191 }
6192 }
6193 }
6194
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006195 return DAG.getNode(ISD::BITCAST, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00006196 DAG.getNode(X86ISD::VZEXT_MOVL, dl, OpVT,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006197 DAG.getNode(ISD::BITCAST, dl,
Dale Johannesenace16102009-02-03 19:33:06 +00006198 OpVT, SrcOp)));
Evan Cheng7e2ff772008-05-08 00:57:18 +00006199}
6200
Bruno Cardoso Lopesd0888342011-07-22 00:14:56 +00006201/// LowerVECTOR_SHUFFLE_256 - Handle all 256-bit wide vectors shuffles
6202/// which could not be matched by any known target speficic shuffle
6203static SDValue
6204LowerVECTOR_SHUFFLE_256(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG) {
Elena Demikhovsky15963732012-06-26 08:04:10 +00006205
6206 SDValue NewOp = Compact8x32ShuffleNode(SVOp, DAG);
6207 if (NewOp.getNode())
6208 return NewOp;
6209
Craig Topper8f35c132012-01-20 09:29:03 +00006210 EVT VT = SVOp->getValueType(0);
Bruno Cardoso Lopes3b865982011-08-16 18:21:54 +00006211
Craig Topper8f35c132012-01-20 09:29:03 +00006212 unsigned NumElems = VT.getVectorNumElements();
6213 unsigned NumLaneElems = NumElems / 2;
6214
Craig Topper8f35c132012-01-20 09:29:03 +00006215 DebugLoc dl = SVOp->getDebugLoc();
6216 MVT EltVT = VT.getVectorElementType().getSimpleVT();
Craig Topper9a2b6e12012-04-06 07:45:23 +00006217 EVT NVT = MVT::getVectorVT(EltVT, NumLaneElems);
Craig Topper8ae97ba2012-05-21 06:40:16 +00006218 SDValue Output[2];
Craig Topper8f35c132012-01-20 09:29:03 +00006219
Craig Topper9a2b6e12012-04-06 07:45:23 +00006220 SmallVector<int, 16> Mask;
Craig Topper8f35c132012-01-20 09:29:03 +00006221 for (unsigned l = 0; l < 2; ++l) {
Craig Topper9a2b6e12012-04-06 07:45:23 +00006222 // Build a shuffle mask for the output, discovering on the fly which
6223 // input vectors to use as shuffle operands (recorded in InputUsed).
6224 // If building a suitable shuffle vector proves too hard, then bail
Craig Topper8ae97ba2012-05-21 06:40:16 +00006225 // out with UseBuildVector set.
6226 bool UseBuildVector = false;
Benjamin Kramer9e5512a2012-04-06 13:33:52 +00006227 int InputUsed[2] = { -1, -1 }; // Not yet discovered.
Craig Topper9a2b6e12012-04-06 07:45:23 +00006228 unsigned LaneStart = l * NumLaneElems;
6229 for (unsigned i = 0; i != NumLaneElems; ++i) {
6230 // The mask element. This indexes into the input.
6231 int Idx = SVOp->getMaskElt(i+LaneStart);
6232 if (Idx < 0) {
6233 // the mask element does not index into any input vector.
6234 Mask.push_back(-1);
6235 continue;
6236 }
Craig Topper8f35c132012-01-20 09:29:03 +00006237
Craig Topper9a2b6e12012-04-06 07:45:23 +00006238 // The input vector this mask element indexes into.
6239 int Input = Idx / NumLaneElems;
Craig Topper8f35c132012-01-20 09:29:03 +00006240
Craig Topper9a2b6e12012-04-06 07:45:23 +00006241 // Turn the index into an offset from the start of the input vector.
6242 Idx -= Input * NumLaneElems;
6243
6244 // Find or create a shuffle vector operand to hold this input.
6245 unsigned OpNo;
6246 for (OpNo = 0; OpNo < array_lengthof(InputUsed); ++OpNo) {
6247 if (InputUsed[OpNo] == Input)
6248 // This input vector is already an operand.
6249 break;
6250 if (InputUsed[OpNo] < 0) {
6251 // Create a new operand for this input vector.
6252 InputUsed[OpNo] = Input;
6253 break;
6254 }
6255 }
6256
6257 if (OpNo >= array_lengthof(InputUsed)) {
Craig Topper8ae97ba2012-05-21 06:40:16 +00006258 // More than two input vectors used! Give up on trying to create a
6259 // shuffle vector. Insert all elements into a BUILD_VECTOR instead.
6260 UseBuildVector = true;
6261 break;
Craig Topper9a2b6e12012-04-06 07:45:23 +00006262 }
6263
6264 // Add the mask index for the new shuffle vector.
6265 Mask.push_back(Idx + OpNo * NumLaneElems);
6266 }
6267
Craig Topper8ae97ba2012-05-21 06:40:16 +00006268 if (UseBuildVector) {
6269 SmallVector<SDValue, 16> SVOps;
6270 for (unsigned i = 0; i != NumLaneElems; ++i) {
6271 // The mask element. This indexes into the input.
6272 int Idx = SVOp->getMaskElt(i+LaneStart);
6273 if (Idx < 0) {
6274 SVOps.push_back(DAG.getUNDEF(EltVT));
6275 continue;
6276 }
6277
6278 // The input vector this mask element indexes into.
6279 int Input = Idx / NumElems;
6280
6281 // Turn the index into an offset from the start of the input vector.
6282 Idx -= Input * NumElems;
6283
6284 // Extract the vector element by hand.
6285 SVOps.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EltVT,
6286 SVOp->getOperand(Input),
6287 DAG.getIntPtrConstant(Idx)));
6288 }
6289
6290 // Construct the output using a BUILD_VECTOR.
6291 Output[l] = DAG.getNode(ISD::BUILD_VECTOR, dl, NVT, &SVOps[0],
6292 SVOps.size());
6293 } else if (InputUsed[0] < 0) {
Craig Topper9a2b6e12012-04-06 07:45:23 +00006294 // No input vectors were used! The result is undefined.
Craig Topper8ae97ba2012-05-21 06:40:16 +00006295 Output[l] = DAG.getUNDEF(NVT);
Craig Topper9a2b6e12012-04-06 07:45:23 +00006296 } else {
6297 SDValue Op0 = Extract128BitVector(SVOp->getOperand(InputUsed[0] / 2),
Craig Topperb14940a2012-04-22 20:55:18 +00006298 (InputUsed[0] % 2) * NumLaneElems,
6299 DAG, dl);
Craig Topper9a2b6e12012-04-06 07:45:23 +00006300 // If only one input was used, use an undefined vector for the other.
6301 SDValue Op1 = (InputUsed[1] < 0) ? DAG.getUNDEF(NVT) :
6302 Extract128BitVector(SVOp->getOperand(InputUsed[1] / 2),
Craig Topperb14940a2012-04-22 20:55:18 +00006303 (InputUsed[1] % 2) * NumLaneElems, DAG, dl);
Craig Topper9a2b6e12012-04-06 07:45:23 +00006304 // At least one input vector was used. Create a new shuffle vector.
Craig Topper8ae97ba2012-05-21 06:40:16 +00006305 Output[l] = DAG.getVectorShuffle(NVT, dl, Op0, Op1, &Mask[0]);
Craig Topper9a2b6e12012-04-06 07:45:23 +00006306 }
6307
6308 Mask.clear();
6309 }
Craig Topper8f35c132012-01-20 09:29:03 +00006310
6311 // Concatenate the result back
Craig Topper8ae97ba2012-05-21 06:40:16 +00006312 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT, Output[0], Output[1]);
Bruno Cardoso Lopesd0888342011-07-22 00:14:56 +00006313}
6314
Bruno Cardoso Lopes589b8972011-07-22 00:14:53 +00006315/// LowerVECTOR_SHUFFLE_128v4 - Handle all 128-bit wide vectors with
6316/// 4 elements, and match them with several different shuffle types.
Dan Gohman475871a2008-07-27 21:46:04 +00006317static SDValue
Bruno Cardoso Lopes589b8972011-07-22 00:14:53 +00006318LowerVECTOR_SHUFFLE_128v4(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006319 SDValue V1 = SVOp->getOperand(0);
6320 SDValue V2 = SVOp->getOperand(1);
6321 DebugLoc dl = SVOp->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00006322 EVT VT = SVOp->getValueType(0);
Eric Christopherfd179292009-08-27 18:07:15 +00006323
Craig Topper7a9a28b2012-08-12 02:23:29 +00006324 assert(VT.is128BitVector() && "Unsupported vector size");
Bruno Cardoso Lopes589b8972011-07-22 00:14:53 +00006325
Benjamin Kramer9c683542012-01-30 15:16:21 +00006326 std::pair<int, int> Locs[4];
6327 int Mask1[] = { -1, -1, -1, -1 };
Benjamin Kramered4c8c62012-01-15 13:16:05 +00006328 SmallVector<int, 8> PermMask(SVOp->getMask().begin(), SVOp->getMask().end());
Nate Begeman9008ca62009-04-27 18:41:29 +00006329
Evan Chengace3c172008-07-22 21:13:36 +00006330 unsigned NumHi = 0;
6331 unsigned NumLo = 0;
Evan Chengace3c172008-07-22 21:13:36 +00006332 for (unsigned i = 0; i != 4; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006333 int Idx = PermMask[i];
6334 if (Idx < 0) {
Evan Chengace3c172008-07-22 21:13:36 +00006335 Locs[i] = std::make_pair(-1, -1);
6336 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00006337 assert(Idx < 8 && "Invalid VECTOR_SHUFFLE index!");
6338 if (Idx < 4) {
Evan Chengace3c172008-07-22 21:13:36 +00006339 Locs[i] = std::make_pair(0, NumLo);
Nate Begeman9008ca62009-04-27 18:41:29 +00006340 Mask1[NumLo] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00006341 NumLo++;
6342 } else {
6343 Locs[i] = std::make_pair(1, NumHi);
6344 if (2+NumHi < 4)
Nate Begeman9008ca62009-04-27 18:41:29 +00006345 Mask1[2+NumHi] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00006346 NumHi++;
6347 }
6348 }
6349 }
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006350
Evan Chengace3c172008-07-22 21:13:36 +00006351 if (NumLo <= 2 && NumHi <= 2) {
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006352 // If no more than two elements come from either vector. This can be
6353 // implemented with two shuffles. First shuffle gather the elements.
6354 // The second shuffle, which takes the first shuffle as both of its
6355 // vector operands, put the elements into the right order.
Nate Begeman9008ca62009-04-27 18:41:29 +00006356 V1 = DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006357
Benjamin Kramer9c683542012-01-30 15:16:21 +00006358 int Mask2[] = { -1, -1, -1, -1 };
Eric Christopherfd179292009-08-27 18:07:15 +00006359
Benjamin Kramer9c683542012-01-30 15:16:21 +00006360 for (unsigned i = 0; i != 4; ++i)
6361 if (Locs[i].first != -1) {
Evan Chengace3c172008-07-22 21:13:36 +00006362 unsigned Idx = (i < 2) ? 0 : 4;
6363 Idx += Locs[i].first * 2 + Locs[i].second;
Nate Begeman9008ca62009-04-27 18:41:29 +00006364 Mask2[i] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00006365 }
Evan Chengace3c172008-07-22 21:13:36 +00006366
Nate Begeman9008ca62009-04-27 18:41:29 +00006367 return DAG.getVectorShuffle(VT, dl, V1, V1, &Mask2[0]);
Craig Topper69947b92012-04-23 06:57:04 +00006368 }
6369
6370 if (NumLo == 3 || NumHi == 3) {
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006371 // Otherwise, we must have three elements from one vector, call it X, and
6372 // one element from the other, call it Y. First, use a shufps to build an
6373 // intermediate vector with the one element from Y and the element from X
6374 // that will be in the same half in the final destination (the indexes don't
6375 // matter). Then, use a shufps to build the final vector, taking the half
6376 // containing the element from Y from the intermediate, and the other half
6377 // from X.
6378 if (NumHi == 3) {
6379 // Normalize it so the 3 elements come from V1.
Craig Topperbeabc6c2011-12-05 06:56:46 +00006380 CommuteVectorShuffleMask(PermMask, 4);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006381 std::swap(V1, V2);
6382 }
6383
6384 // Find the element from V2.
6385 unsigned HiIndex;
6386 for (HiIndex = 0; HiIndex < 3; ++HiIndex) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006387 int Val = PermMask[HiIndex];
6388 if (Val < 0)
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006389 continue;
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006390 if (Val >= 4)
6391 break;
6392 }
6393
Nate Begeman9008ca62009-04-27 18:41:29 +00006394 Mask1[0] = PermMask[HiIndex];
6395 Mask1[1] = -1;
6396 Mask1[2] = PermMask[HiIndex^1];
6397 Mask1[3] = -1;
6398 V2 = DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006399
6400 if (HiIndex >= 2) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006401 Mask1[0] = PermMask[0];
6402 Mask1[1] = PermMask[1];
6403 Mask1[2] = HiIndex & 1 ? 6 : 4;
6404 Mask1[3] = HiIndex & 1 ? 4 : 6;
6405 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006406 }
Craig Topper69947b92012-04-23 06:57:04 +00006407
6408 Mask1[0] = HiIndex & 1 ? 2 : 0;
6409 Mask1[1] = HiIndex & 1 ? 0 : 2;
6410 Mask1[2] = PermMask[2];
6411 Mask1[3] = PermMask[3];
6412 if (Mask1[2] >= 0)
6413 Mask1[2] += 4;
6414 if (Mask1[3] >= 0)
6415 Mask1[3] += 4;
6416 return DAG.getVectorShuffle(VT, dl, V2, V1, &Mask1[0]);
Evan Chengace3c172008-07-22 21:13:36 +00006417 }
6418
6419 // Break it into (shuffle shuffle_hi, shuffle_lo).
Benjamin Kramer9c683542012-01-30 15:16:21 +00006420 int LoMask[] = { -1, -1, -1, -1 };
6421 int HiMask[] = { -1, -1, -1, -1 };
Nate Begeman9008ca62009-04-27 18:41:29 +00006422
Benjamin Kramer9c683542012-01-30 15:16:21 +00006423 int *MaskPtr = LoMask;
Evan Chengace3c172008-07-22 21:13:36 +00006424 unsigned MaskIdx = 0;
6425 unsigned LoIdx = 0;
6426 unsigned HiIdx = 2;
6427 for (unsigned i = 0; i != 4; ++i) {
6428 if (i == 2) {
Benjamin Kramer9c683542012-01-30 15:16:21 +00006429 MaskPtr = HiMask;
Evan Chengace3c172008-07-22 21:13:36 +00006430 MaskIdx = 1;
6431 LoIdx = 0;
6432 HiIdx = 2;
6433 }
Nate Begeman9008ca62009-04-27 18:41:29 +00006434 int Idx = PermMask[i];
6435 if (Idx < 0) {
Evan Chengace3c172008-07-22 21:13:36 +00006436 Locs[i] = std::make_pair(-1, -1);
Nate Begeman9008ca62009-04-27 18:41:29 +00006437 } else if (Idx < 4) {
Evan Chengace3c172008-07-22 21:13:36 +00006438 Locs[i] = std::make_pair(MaskIdx, LoIdx);
Benjamin Kramer9c683542012-01-30 15:16:21 +00006439 MaskPtr[LoIdx] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00006440 LoIdx++;
6441 } else {
6442 Locs[i] = std::make_pair(MaskIdx, HiIdx);
Benjamin Kramer9c683542012-01-30 15:16:21 +00006443 MaskPtr[HiIdx] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00006444 HiIdx++;
6445 }
6446 }
6447
Nate Begeman9008ca62009-04-27 18:41:29 +00006448 SDValue LoShuffle = DAG.getVectorShuffle(VT, dl, V1, V2, &LoMask[0]);
6449 SDValue HiShuffle = DAG.getVectorShuffle(VT, dl, V1, V2, &HiMask[0]);
Benjamin Kramer9c683542012-01-30 15:16:21 +00006450 int MaskOps[] = { -1, -1, -1, -1 };
6451 for (unsigned i = 0; i != 4; ++i)
6452 if (Locs[i].first != -1)
6453 MaskOps[i] = Locs[i].first * 4 + Locs[i].second;
Nate Begeman9008ca62009-04-27 18:41:29 +00006454 return DAG.getVectorShuffle(VT, dl, LoShuffle, HiShuffle, &MaskOps[0]);
Evan Chengace3c172008-07-22 21:13:36 +00006455}
6456
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00006457static bool MayFoldVectorLoad(SDValue V) {
Jakub Staszaka24262a2012-10-30 00:01:57 +00006458 while (V.hasOneUse() && V.getOpcode() == ISD::BITCAST)
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00006459 V = V.getOperand(0);
Jakub Staszaka24262a2012-10-30 00:01:57 +00006460
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00006461 if (V.hasOneUse() && V.getOpcode() == ISD::SCALAR_TO_VECTOR)
6462 V = V.getOperand(0);
Evan Cheng7bc389b2011-11-08 00:31:58 +00006463 if (V.hasOneUse() && V.getOpcode() == ISD::BUILD_VECTOR &&
6464 V.getNumOperands() == 2 && V.getOperand(1).getOpcode() == ISD::UNDEF)
6465 // BUILD_VECTOR (load), undef
6466 V = V.getOperand(0);
Jakub Staszaka24262a2012-10-30 00:01:57 +00006467
6468 return MayFoldLoad(V);
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00006469}
6470
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006471// FIXME: the version above should always be used. Since there's
6472// a bug where several vector shuffles can't be folded because the
6473// DAG is not updated during lowering and a node claims to have two
6474// uses while it only has one, use this version, and let isel match
6475// another instruction if the load really happens to have more than
6476// one use. Remove this version after this bug get fixed.
Evan Cheng835580f2010-10-07 20:50:20 +00006477// rdar://8434668, PR8156
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006478static bool RelaxedMayFoldVectorLoad(SDValue V) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006479 if (V.hasOneUse() && V.getOpcode() == ISD::BITCAST)
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006480 V = V.getOperand(0);
6481 if (V.hasOneUse() && V.getOpcode() == ISD::SCALAR_TO_VECTOR)
6482 V = V.getOperand(0);
6483 if (ISD::isNormalLoad(V.getNode()))
6484 return true;
6485 return false;
6486}
6487
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006488static
Evan Cheng835580f2010-10-07 20:50:20 +00006489SDValue getMOVDDup(SDValue &Op, DebugLoc &dl, SDValue V1, SelectionDAG &DAG) {
6490 EVT VT = Op.getValueType();
6491
6492 // Canonizalize to v2f64.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006493 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, V1);
6494 return DAG.getNode(ISD::BITCAST, dl, VT,
Evan Cheng835580f2010-10-07 20:50:20 +00006495 getTargetShuffleNode(X86ISD::MOVDDUP, dl, MVT::v2f64,
6496 V1, DAG));
6497}
6498
6499static
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006500SDValue getMOVLowToHigh(SDValue &Op, DebugLoc &dl, SelectionDAG &DAG,
Craig Topper1accb7e2012-01-10 06:54:16 +00006501 bool HasSSE2) {
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006502 SDValue V1 = Op.getOperand(0);
6503 SDValue V2 = Op.getOperand(1);
6504 EVT VT = Op.getValueType();
6505
6506 assert(VT != MVT::v2i64 && "unsupported shuffle type");
6507
Craig Topper1accb7e2012-01-10 06:54:16 +00006508 if (HasSSE2 && VT == MVT::v2f64)
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006509 return getTargetShuffleNode(X86ISD::MOVLHPD, dl, VT, V1, V2, DAG);
6510
Evan Cheng0899f5c2011-08-31 02:05:24 +00006511 // v4f32 or v4i32: canonizalized to v4f32 (which is legal for SSE1)
6512 return DAG.getNode(ISD::BITCAST, dl, VT,
6513 getTargetShuffleNode(X86ISD::MOVLHPS, dl, MVT::v4f32,
6514 DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, V1),
6515 DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, V2), DAG));
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006516}
6517
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00006518static
6519SDValue getMOVHighToLow(SDValue &Op, DebugLoc &dl, SelectionDAG &DAG) {
6520 SDValue V1 = Op.getOperand(0);
6521 SDValue V2 = Op.getOperand(1);
6522 EVT VT = Op.getValueType();
6523
6524 assert((VT == MVT::v4i32 || VT == MVT::v4f32) &&
6525 "unsupported shuffle type");
6526
6527 if (V2.getOpcode() == ISD::UNDEF)
6528 V2 = V1;
6529
6530 // v4i32 or v4f32
6531 return getTargetShuffleNode(X86ISD::MOVHLPS, dl, VT, V1, V2, DAG);
6532}
6533
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006534static
Craig Topper1accb7e2012-01-10 06:54:16 +00006535SDValue getMOVLP(SDValue &Op, DebugLoc &dl, SelectionDAG &DAG, bool HasSSE2) {
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006536 SDValue V1 = Op.getOperand(0);
6537 SDValue V2 = Op.getOperand(1);
6538 EVT VT = Op.getValueType();
6539 unsigned NumElems = VT.getVectorNumElements();
6540
6541 // Use MOVLPS and MOVLPD in case V1 or V2 are loads. During isel, the second
6542 // operand of these instructions is only memory, so check if there's a
6543 // potencial load folding here, otherwise use SHUFPS or MOVSD to match the
6544 // same masks.
6545 bool CanFoldLoad = false;
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006546
Bruno Cardoso Lopesd00bfe12010-09-02 02:35:51 +00006547 // Trivial case, when V2 comes from a load.
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00006548 if (MayFoldVectorLoad(V2))
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006549 CanFoldLoad = true;
6550
6551 // When V1 is a load, it can be folded later into a store in isel, example:
6552 // (store (v4f32 (X86Movlps (load addr:$src1), VR128:$src2)), addr:$src1)
6553 // turns into:
6554 // (MOVLPSmr addr:$src1, VR128:$src2)
6555 // So, recognize this potential and also use MOVLPS or MOVLPD
Evan Cheng7bc389b2011-11-08 00:31:58 +00006556 else if (MayFoldVectorLoad(V1) && MayFoldIntoStore(Op))
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006557 CanFoldLoad = true;
6558
Dan Gohman65fd6562011-11-03 21:49:52 +00006559 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006560 if (CanFoldLoad) {
Craig Topper1accb7e2012-01-10 06:54:16 +00006561 if (HasSSE2 && NumElems == 2)
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006562 return getTargetShuffleNode(X86ISD::MOVLPD, dl, VT, V1, V2, DAG);
6563
6564 if (NumElems == 4)
Benjamin Kramerd9b0b022012-06-02 10:20:22 +00006565 // If we don't care about the second element, proceed to use movss.
Dan Gohman65fd6562011-11-03 21:49:52 +00006566 if (SVOp->getMaskElt(1) != -1)
6567 return getTargetShuffleNode(X86ISD::MOVLPS, dl, VT, V1, V2, DAG);
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006568 }
6569
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006570 // movl and movlp will both match v2i64, but v2i64 is never matched by
6571 // movl earlier because we make it strict to avoid messing with the movlp load
6572 // folding logic (see the code above getMOVLP call). Match it here then,
6573 // this is horrible, but will stay like this until we move all shuffle
6574 // matching to x86 specific nodes. Note that for the 1st condition all
6575 // types are matched with movsd.
Craig Topper1accb7e2012-01-10 06:54:16 +00006576 if (HasSSE2) {
Bruno Cardoso Lopes5ca0d142011-09-14 02:36:14 +00006577 // FIXME: isMOVLMask should be checked and matched before getMOVLP,
6578 // as to remove this logic from here, as much as possible
Craig Topper5aaffa82012-02-19 02:53:47 +00006579 if (NumElems == 2 || !isMOVLMask(SVOp->getMask(), VT))
Bruno Cardoso Lopes57d6a5e2011-08-31 03:04:20 +00006580 return getTargetShuffleNode(X86ISD::MOVSD, dl, VT, V1, V2, DAG);
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006581 return getTargetShuffleNode(X86ISD::MOVSS, dl, VT, V1, V2, DAG);
Bruno Cardoso Lopes57d6a5e2011-08-31 03:04:20 +00006582 }
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006583
6584 assert(VT != MVT::v4i32 && "unsupported shuffle type");
6585
6586 // Invert the operand order and use SHUFPS to match it.
Craig Topperb3982da2011-12-31 23:50:21 +00006587 return getTargetShuffleNode(X86ISD::SHUFP, dl, VT, V2, V1,
Craig Topper5aaffa82012-02-19 02:53:47 +00006588 getShuffleSHUFImmediate(SVOp), DAG);
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006589}
6590
Michael Liaod9d09602012-10-23 17:34:00 +00006591// Reduce a vector shuffle to zext.
6592SDValue
6593X86TargetLowering::lowerVectorIntExtend(SDValue Op, SelectionDAG &DAG) const {
6594 // PMOVZX is only available from SSE41.
6595 if (!Subtarget->hasSSE41())
6596 return SDValue();
6597
6598 EVT VT = Op.getValueType();
6599
6600 // Only AVX2 support 256-bit vector integer extending.
6601 if (!Subtarget->hasAVX2() && VT.is256BitVector())
6602 return SDValue();
6603
6604 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
6605 DebugLoc DL = Op.getDebugLoc();
6606 SDValue V1 = Op.getOperand(0);
6607 SDValue V2 = Op.getOperand(1);
6608 unsigned NumElems = VT.getVectorNumElements();
6609
6610 // Extending is an unary operation and the element type of the source vector
6611 // won't be equal to or larger than i64.
6612 if (V2.getOpcode() != ISD::UNDEF || !VT.isInteger() ||
6613 VT.getVectorElementType() == MVT::i64)
6614 return SDValue();
6615
6616 // Find the expansion ratio, e.g. expanding from i8 to i32 has a ratio of 4.
6617 unsigned Shift = 1; // Start from 2, i.e. 1 << 1.
Duncan Sands34739052012-10-29 11:29:53 +00006618 while ((1U << Shift) < NumElems) {
6619 if (SVOp->getMaskElt(1U << Shift) == 1)
Michael Liaod9d09602012-10-23 17:34:00 +00006620 break;
6621 Shift += 1;
6622 // The maximal ratio is 8, i.e. from i8 to i64.
6623 if (Shift > 3)
6624 return SDValue();
6625 }
6626
6627 // Check the shuffle mask.
6628 unsigned Mask = (1U << Shift) - 1;
6629 for (unsigned i = 0; i != NumElems; ++i) {
6630 int EltIdx = SVOp->getMaskElt(i);
6631 if ((i & Mask) != 0 && EltIdx != -1)
6632 return SDValue();
Matt Beaumont-Gaya999de02012-10-23 19:46:36 +00006633 if ((i & Mask) == 0 && (unsigned)EltIdx != (i >> Shift))
Michael Liaod9d09602012-10-23 17:34:00 +00006634 return SDValue();
6635 }
6636
6637 unsigned NBits = VT.getVectorElementType().getSizeInBits() << Shift;
6638 EVT NeVT = EVT::getIntegerVT(*DAG.getContext(), NBits);
6639 EVT NVT = EVT::getVectorVT(*DAG.getContext(), NeVT, NumElems >> Shift);
6640
6641 if (!isTypeLegal(NVT))
6642 return SDValue();
6643
6644 // Simplify the operand as it's prepared to be fed into shuffle.
6645 unsigned SignificantBits = NVT.getSizeInBits() >> Shift;
6646 if (V1.getOpcode() == ISD::BITCAST &&
6647 V1.getOperand(0).getOpcode() == ISD::SCALAR_TO_VECTOR &&
6648 V1.getOperand(0).getOperand(0).getOpcode() == ISD::EXTRACT_VECTOR_ELT &&
6649 V1.getOperand(0)
6650 .getOperand(0).getValueType().getSizeInBits() == SignificantBits) {
6651 // (bitcast (sclr2vec (ext_vec_elt x))) -> (bitcast x)
6652 SDValue V = V1.getOperand(0).getOperand(0).getOperand(0);
Michael Liao07872742012-10-23 21:40:15 +00006653 ConstantSDNode *CIdx =
6654 dyn_cast<ConstantSDNode>(V1.getOperand(0).getOperand(0).getOperand(1));
Michael Liaod9d09602012-10-23 17:34:00 +00006655 // If it's foldable, i.e. normal load with single use, we will let code
6656 // selection to fold it. Otherwise, we will short the conversion sequence.
Michael Liao07872742012-10-23 21:40:15 +00006657 if (CIdx && CIdx->getZExtValue() == 0 &&
6658 (!ISD::isNormalLoad(V.getNode()) || !V.hasOneUse()))
Michael Liaod9d09602012-10-23 17:34:00 +00006659 V1 = DAG.getNode(ISD::BITCAST, DL, V1.getValueType(), V);
6660 }
6661
6662 return DAG.getNode(ISD::BITCAST, DL, VT,
6663 DAG.getNode(X86ISD::VZEXT, DL, NVT, V1));
6664}
6665
Nadav Rotem154819d2012-04-09 07:45:58 +00006666SDValue
6667X86TargetLowering::NormalizeVectorShuffle(SDValue Op, SelectionDAG &DAG) const {
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006668 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
6669 EVT VT = Op.getValueType();
6670 DebugLoc dl = Op.getDebugLoc();
6671 SDValue V1 = Op.getOperand(0);
6672 SDValue V2 = Op.getOperand(1);
6673
6674 if (isZeroShuffle(SVOp))
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00006675 return getZeroVector(VT, Subtarget, DAG, dl);
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006676
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006677 // Handle splat operations
6678 if (SVOp->isSplat()) {
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00006679 unsigned NumElem = VT.getVectorNumElements();
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00006680 int Size = VT.getSizeInBits();
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006681
Bruno Cardoso Lopes0e6d2302011-08-17 02:29:19 +00006682 // Use vbroadcast whenever the splat comes from a foldable load
Nadav Rotem154819d2012-04-09 07:45:58 +00006683 SDValue Broadcast = LowerVectorBroadcast(Op, DAG);
Nadav Rotem9d68b062012-04-08 12:54:54 +00006684 if (Broadcast.getNode())
6685 return Broadcast;
Bruno Cardoso Lopes0e6d2302011-08-17 02:29:19 +00006686
Bruno Cardoso Lopes6a32adc2011-07-25 23:05:25 +00006687 // Handle splats by matching through known shuffle masks
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00006688 if ((Size == 128 && NumElem <= 4) ||
6689 (Size == 256 && NumElem < 8))
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006690 return SDValue();
6691
Bruno Cardoso Lopes8a5b2622011-08-17 02:29:13 +00006692 // All remaning splats are promoted to target supported vector shuffles.
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006693 return PromoteSplat(SVOp, DAG);
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006694 }
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006695
Michael Liaod9d09602012-10-23 17:34:00 +00006696 // Check integer expanding shuffles.
6697 SDValue NewOp = lowerVectorIntExtend(Op, DAG);
6698 if (NewOp.getNode())
6699 return NewOp;
6700
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006701 // If the shuffle can be profitably rewritten as a narrower shuffle, then
6702 // do it!
Craig Topperf3640d72012-05-04 04:44:49 +00006703 if (VT == MVT::v8i16 || VT == MVT::v16i8 ||
6704 VT == MVT::v16i16 || VT == MVT::v32i8) {
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006705 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, dl);
6706 if (NewOp.getNode())
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006707 return DAG.getNode(ISD::BITCAST, dl, VT, NewOp);
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006708 } else if ((VT == MVT::v4i32 ||
Craig Topper1accb7e2012-01-10 06:54:16 +00006709 (VT == MVT::v4f32 && Subtarget->hasSSE2()))) {
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006710 // FIXME: Figure out a cleaner way to do this.
6711 // Try to make use of movq to zero out the top part.
6712 if (ISD::isBuildVectorAllZeros(V2.getNode())) {
6713 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, dl);
6714 if (NewOp.getNode()) {
Craig Topper5aaffa82012-02-19 02:53:47 +00006715 EVT NewVT = NewOp.getValueType();
6716 if (isCommutedMOVLMask(cast<ShuffleVectorSDNode>(NewOp)->getMask(),
6717 NewVT, true, false))
6718 return getVZextMovL(VT, NewVT, NewOp.getOperand(0),
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006719 DAG, Subtarget, dl);
6720 }
6721 } else if (ISD::isBuildVectorAllZeros(V1.getNode())) {
6722 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, dl);
Craig Topper5aaffa82012-02-19 02:53:47 +00006723 if (NewOp.getNode()) {
6724 EVT NewVT = NewOp.getValueType();
6725 if (isMOVLMask(cast<ShuffleVectorSDNode>(NewOp)->getMask(), NewVT))
6726 return getVZextMovL(VT, NewVT, NewOp.getOperand(1),
6727 DAG, Subtarget, dl);
6728 }
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006729 }
6730 }
6731 return SDValue();
6732}
6733
Dan Gohman475871a2008-07-27 21:46:04 +00006734SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006735X86TargetLowering::LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) const {
Nate Begeman9008ca62009-04-27 18:41:29 +00006736 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Dan Gohman475871a2008-07-27 21:46:04 +00006737 SDValue V1 = Op.getOperand(0);
6738 SDValue V2 = Op.getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +00006739 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006740 DebugLoc dl = Op.getDebugLoc();
Nate Begeman9008ca62009-04-27 18:41:29 +00006741 unsigned NumElems = VT.getVectorNumElements();
Elena Demikhovsky16db7102012-01-12 20:33:10 +00006742 bool V1IsUndef = V1.getOpcode() == ISD::UNDEF;
Evan Cheng0db9fe62006-04-25 20:13:52 +00006743 bool V2IsUndef = V2.getOpcode() == ISD::UNDEF;
Evan Chengd9b8e402006-10-16 06:36:00 +00006744 bool V1IsSplat = false;
6745 bool V2IsSplat = false;
Craig Topper1accb7e2012-01-10 06:54:16 +00006746 bool HasSSE2 = Subtarget->hasSSE2();
Craig Topperbeabc6c2011-12-05 06:56:46 +00006747 bool HasAVX = Subtarget->hasAVX();
Craig Topper6347e862011-11-21 06:57:39 +00006748 bool HasAVX2 = Subtarget->hasAVX2();
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006749 MachineFunction &MF = DAG.getMachineFunction();
Bill Wendling67658342012-10-09 07:45:08 +00006750 bool OptForSize = MF.getFunction()->getFnAttributes().
6751 hasAttribute(Attributes::OptimizeForSize);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006752
Craig Topper3426a3e2011-11-14 06:46:21 +00006753 assert(VT.getSizeInBits() != 64 && "Can't lower MMX shuffles");
Bruno Cardoso Lopes58277b12010-09-07 18:41:45 +00006754
Elena Demikhovsky16db7102012-01-12 20:33:10 +00006755 if (V1IsUndef && V2IsUndef)
6756 return DAG.getUNDEF(VT);
6757
6758 assert(!V1IsUndef && "Op 1 of shuffle should not be undef");
Craig Topper38034c52011-11-26 22:55:48 +00006759
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006760 // Vector shuffle lowering takes 3 steps:
6761 //
6762 // 1) Normalize the input vectors. Here splats, zeroed vectors, profitable
6763 // narrowing and commutation of operands should be handled.
6764 // 2) Matching of shuffles with known shuffle masks to x86 target specific
6765 // shuffle nodes.
6766 // 3) Rewriting of unmatched masks into new generic shuffle operations,
6767 // so the shuffle can be broken into other shuffles and the legalizer can
6768 // try the lowering again.
6769 //
Craig Topper3426a3e2011-11-14 06:46:21 +00006770 // The general idea is that no vector_shuffle operation should be left to
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006771 // be matched during isel, all of them must be converted to a target specific
6772 // node here.
Bruno Cardoso Lopes0d1340b2010-09-07 20:20:27 +00006773
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006774 // Normalize the input vectors. Here splats, zeroed vectors, profitable
6775 // narrowing and commutation of operands should be handled. The actual code
6776 // doesn't include all of those, work in progress...
Nadav Rotem154819d2012-04-09 07:45:58 +00006777 SDValue NewOp = NormalizeVectorShuffle(Op, DAG);
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006778 if (NewOp.getNode())
6779 return NewOp;
Eric Christopherfd179292009-08-27 18:07:15 +00006780
Craig Topper5aaffa82012-02-19 02:53:47 +00006781 SmallVector<int, 8> M(SVOp->getMask().begin(), SVOp->getMask().end());
6782
Bruno Cardoso Lopesa22c8452010-09-04 00:39:43 +00006783 // NOTE: isPSHUFDMask can also match both masks below (unpckl_undef and
6784 // unpckh_undef). Only use pshufd if speed is more important than size.
Craig Topper5aaffa82012-02-19 02:53:47 +00006785 if (OptForSize && isUNPCKL_v_undef_Mask(M, VT, HasAVX2))
Craig Topper34671b82011-12-06 08:21:25 +00006786 return getTargetShuffleNode(X86ISD::UNPCKL, dl, VT, V1, V1, DAG);
Craig Topper5aaffa82012-02-19 02:53:47 +00006787 if (OptForSize && isUNPCKH_v_undef_Mask(M, VT, HasAVX2))
Craig Topper34671b82011-12-06 08:21:25 +00006788 return getTargetShuffleNode(X86ISD::UNPCKH, dl, VT, V1, V1, DAG);
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00006789
Craig Topperdd637ae2012-02-19 05:41:45 +00006790 if (isMOVDDUPMask(M, VT) && Subtarget->hasSSE3() &&
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006791 V2IsUndef && RelaxedMayFoldVectorLoad(V1))
Evan Cheng835580f2010-10-07 20:50:20 +00006792 return getMOVDDup(Op, dl, V1, DAG);
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006793
Craig Topperdd637ae2012-02-19 05:41:45 +00006794 if (isMOVHLPS_v_undef_Mask(M, VT))
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006795 return getMOVHighToLow(Op, dl, DAG);
6796
6797 // Use to match splats
Craig Topper5aaffa82012-02-19 02:53:47 +00006798 if (HasSSE2 && isUNPCKHMask(M, VT, HasAVX2) && V2IsUndef &&
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006799 (VT == MVT::v2f64 || VT == MVT::v2i64))
Craig Topper34671b82011-12-06 08:21:25 +00006800 return getTargetShuffleNode(X86ISD::UNPCKH, dl, VT, V1, V1, DAG);
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006801
Craig Topper5aaffa82012-02-19 02:53:47 +00006802 if (isPSHUFDMask(M, VT)) {
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006803 // The actual implementation will match the mask in the if above and then
6804 // during isel it can match several different instructions, not only pshufd
6805 // as its name says, sad but true, emulate the behavior for now...
Craig Topperdd637ae2012-02-19 05:41:45 +00006806 if (isMOVDDUPMask(M, VT) && ((VT == MVT::v4f32 || VT == MVT::v2i64)))
6807 return getTargetShuffleNode(X86ISD::MOVLHPS, dl, VT, V1, V1, DAG);
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006808
Craig Topper5aaffa82012-02-19 02:53:47 +00006809 unsigned TargetMask = getShuffleSHUFImmediate(SVOp);
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006810
Craig Topperdbd98a42012-02-07 06:28:42 +00006811 if (HasAVX && (VT == MVT::v4f32 || VT == MVT::v2f64))
6812 return getTargetShuffleNode(X86ISD::VPERMILP, dl, VT, V1, TargetMask, DAG);
6813
Craig Topper1accb7e2012-01-10 06:54:16 +00006814 if (HasSSE2 && (VT == MVT::v4f32 || VT == MVT::v4i32))
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006815 return getTargetShuffleNode(X86ISD::PSHUFD, dl, VT, V1, TargetMask, DAG);
6816
Craig Topperb3982da2011-12-31 23:50:21 +00006817 return getTargetShuffleNode(X86ISD::SHUFP, dl, VT, V1, V1,
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00006818 TargetMask, DAG);
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006819 }
Eric Christopherfd179292009-08-27 18:07:15 +00006820
Evan Chengf26ffe92008-05-29 08:22:04 +00006821 // Check if this can be converted into a logical shift.
6822 bool isLeft = false;
6823 unsigned ShAmt = 0;
Dan Gohman475871a2008-07-27 21:46:04 +00006824 SDValue ShVal;
Craig Topper1accb7e2012-01-10 06:54:16 +00006825 bool isShift = HasSSE2 && isVectorShift(SVOp, DAG, isLeft, ShVal, ShAmt);
Evan Chengf26ffe92008-05-29 08:22:04 +00006826 if (isShift && ShVal.hasOneUse()) {
Scott Michelfdc40a02009-02-17 22:15:04 +00006827 // If the shifted value has multiple uses, it may be cheaper to use
Evan Chengf26ffe92008-05-29 08:22:04 +00006828 // v_set0 + movlhps or movhlps, etc.
Dan Gohman8a55ce42009-09-23 21:02:20 +00006829 EVT EltVT = VT.getVectorElementType();
6830 ShAmt *= EltVT.getSizeInBits();
Dale Johannesenace16102009-02-03 19:33:06 +00006831 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00006832 }
Eric Christopherfd179292009-08-27 18:07:15 +00006833
Craig Topper5aaffa82012-02-19 02:53:47 +00006834 if (isMOVLMask(M, VT)) {
Gabor Greifba36cb52008-08-28 21:40:38 +00006835 if (ISD::isBuildVectorAllZeros(V1.getNode()))
Dale Johannesenace16102009-02-03 19:33:06 +00006836 return getVZextMovL(VT, VT, V2, DAG, Subtarget, dl);
Craig Topperdd637ae2012-02-19 05:41:45 +00006837 if (!isMOVLPMask(M, VT)) {
Craig Topper1accb7e2012-01-10 06:54:16 +00006838 if (HasSSE2 && (VT == MVT::v2i64 || VT == MVT::v2f64))
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00006839 return getTargetShuffleNode(X86ISD::MOVSD, dl, VT, V1, V2, DAG);
6840
Bruno Cardoso Lopes4783a3e2010-09-01 22:59:03 +00006841 if (VT == MVT::v4i32 || VT == MVT::v4f32)
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00006842 return getTargetShuffleNode(X86ISD::MOVSS, dl, VT, V1, V2, DAG);
6843 }
Evan Cheng7e2ff772008-05-08 00:57:18 +00006844 }
Eric Christopherfd179292009-08-27 18:07:15 +00006845
Nate Begeman9008ca62009-04-27 18:41:29 +00006846 // FIXME: fold these into legal mask.
Craig Topperdd637ae2012-02-19 05:41:45 +00006847 if (isMOVLHPSMask(M, VT) && !isUNPCKLMask(M, VT, HasAVX2))
Craig Topper1accb7e2012-01-10 06:54:16 +00006848 return getMOVLowToHigh(Op, dl, DAG, HasSSE2);
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006849
Craig Topperdd637ae2012-02-19 05:41:45 +00006850 if (isMOVHLPSMask(M, VT))
Dale Johannesen0488fb62010-09-30 23:57:10 +00006851 return getMOVHighToLow(Op, dl, DAG);
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00006852
Craig Topperdd637ae2012-02-19 05:41:45 +00006853 if (V2IsUndef && isMOVSHDUPMask(M, VT, Subtarget))
Dale Johannesen0488fb62010-09-30 23:57:10 +00006854 return getTargetShuffleNode(X86ISD::MOVSHDUP, dl, VT, V1, DAG);
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00006855
Craig Topperdd637ae2012-02-19 05:41:45 +00006856 if (V2IsUndef && isMOVSLDUPMask(M, VT, Subtarget))
Dale Johannesen0488fb62010-09-30 23:57:10 +00006857 return getTargetShuffleNode(X86ISD::MOVSLDUP, dl, VT, V1, DAG);
Bruno Cardoso Lopes013bb3d2010-08-31 22:35:05 +00006858
Craig Topperdd637ae2012-02-19 05:41:45 +00006859 if (isMOVLPMask(M, VT))
Craig Topper1accb7e2012-01-10 06:54:16 +00006860 return getMOVLP(Op, dl, DAG, HasSSE2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006861
Craig Topperdd637ae2012-02-19 05:41:45 +00006862 if (ShouldXformToMOVHLPS(M, VT) ||
6863 ShouldXformToMOVLP(V1.getNode(), V2.getNode(), M, VT))
Nate Begeman9008ca62009-04-27 18:41:29 +00006864 return CommuteVectorShuffle(SVOp, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006865
Evan Chengf26ffe92008-05-29 08:22:04 +00006866 if (isShift) {
Craig Toppered2e13d2012-01-22 19:15:14 +00006867 // No better options. Use a vshldq / vsrldq.
Dan Gohman8a55ce42009-09-23 21:02:20 +00006868 EVT EltVT = VT.getVectorElementType();
6869 ShAmt *= EltVT.getSizeInBits();
Dale Johannesenace16102009-02-03 19:33:06 +00006870 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00006871 }
Eric Christopherfd179292009-08-27 18:07:15 +00006872
Evan Cheng9eca5e82006-10-25 21:49:50 +00006873 bool Commuted = false;
Chris Lattner8a594482007-11-25 00:24:49 +00006874 // FIXME: This should also accept a bitcast of a splat? Be careful, not
6875 // 1,1,1,1 -> v8i16 though.
Gabor Greifba36cb52008-08-28 21:40:38 +00006876 V1IsSplat = isSplatVector(V1.getNode());
6877 V2IsSplat = isSplatVector(V2.getNode());
Scott Michelfdc40a02009-02-17 22:15:04 +00006878
Chris Lattner8a594482007-11-25 00:24:49 +00006879 // Canonicalize the splat or undef, if present, to be on the RHS.
Craig Topper39a9e482012-02-11 06:24:48 +00006880 if (!V2IsUndef && V1IsSplat && !V2IsSplat) {
6881 CommuteVectorShuffleMask(M, NumElems);
6882 std::swap(V1, V2);
Evan Cheng9bbbb982006-10-25 20:48:19 +00006883 std::swap(V1IsSplat, V2IsSplat);
Evan Cheng9eca5e82006-10-25 21:49:50 +00006884 Commuted = true;
Evan Cheng9bbbb982006-10-25 20:48:19 +00006885 }
6886
Craig Topperbeabc6c2011-12-05 06:56:46 +00006887 if (isCommutedMOVLMask(M, VT, V2IsSplat, V2IsUndef)) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006888 // Shuffling low element of v1 into undef, just return v1.
Eric Christopherfd179292009-08-27 18:07:15 +00006889 if (V2IsUndef)
Nate Begeman9008ca62009-04-27 18:41:29 +00006890 return V1;
6891 // If V2 is a splat, the mask may be malformed such as <4,3,3,3>, which
6892 // the instruction selector will not match, so get a canonical MOVL with
6893 // swapped operands to undo the commute.
6894 return getMOVL(DAG, dl, VT, V2, V1);
Evan Chengd9b8e402006-10-16 06:36:00 +00006895 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00006896
Craig Topperbeabc6c2011-12-05 06:56:46 +00006897 if (isUNPCKLMask(M, VT, HasAVX2))
Craig Topper34671b82011-12-06 08:21:25 +00006898 return getTargetShuffleNode(X86ISD::UNPCKL, dl, VT, V1, V2, DAG);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00006899
Craig Topperbeabc6c2011-12-05 06:56:46 +00006900 if (isUNPCKHMask(M, VT, HasAVX2))
Craig Topper34671b82011-12-06 08:21:25 +00006901 return getTargetShuffleNode(X86ISD::UNPCKH, dl, VT, V1, V2, DAG);
Evan Chenge1113032006-10-04 18:33:38 +00006902
Evan Cheng9bbbb982006-10-25 20:48:19 +00006903 if (V2IsSplat) {
6904 // Normalize mask so all entries that point to V2 points to its first
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00006905 // element then try to match unpck{h|l} again. If match, return a
Craig Topper39a9e482012-02-11 06:24:48 +00006906 // new vector_shuffle with the corrected mask.p
6907 SmallVector<int, 8> NewMask(M.begin(), M.end());
6908 NormalizeMask(NewMask, NumElems);
Craig Topper69947b92012-04-23 06:57:04 +00006909 if (isUNPCKLMask(NewMask, VT, HasAVX2, true))
Craig Topper39a9e482012-02-11 06:24:48 +00006910 return getTargetShuffleNode(X86ISD::UNPCKL, dl, VT, V1, V2, DAG);
Craig Topper69947b92012-04-23 06:57:04 +00006911 if (isUNPCKHMask(NewMask, VT, HasAVX2, true))
Craig Topper39a9e482012-02-11 06:24:48 +00006912 return getTargetShuffleNode(X86ISD::UNPCKH, dl, VT, V1, V2, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006913 }
6914
Evan Cheng9eca5e82006-10-25 21:49:50 +00006915 if (Commuted) {
6916 // Commute is back and try unpck* again.
Nate Begeman9008ca62009-04-27 18:41:29 +00006917 // FIXME: this seems wrong.
Craig Topper39a9e482012-02-11 06:24:48 +00006918 CommuteVectorShuffleMask(M, NumElems);
6919 std::swap(V1, V2);
6920 std::swap(V1IsSplat, V2IsSplat);
6921 Commuted = false;
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00006922
Craig Topper39a9e482012-02-11 06:24:48 +00006923 if (isUNPCKLMask(M, VT, HasAVX2))
6924 return getTargetShuffleNode(X86ISD::UNPCKL, dl, VT, V1, V2, DAG);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00006925
Craig Topper39a9e482012-02-11 06:24:48 +00006926 if (isUNPCKHMask(M, VT, HasAVX2))
6927 return getTargetShuffleNode(X86ISD::UNPCKH, dl, VT, V1, V2, DAG);
Evan Cheng9eca5e82006-10-25 21:49:50 +00006928 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00006929
Nate Begeman9008ca62009-04-27 18:41:29 +00006930 // Normalize the node to match x86 shuffle ops if needed
Craig Topper1a7700a2012-01-19 08:19:12 +00006931 if (!V2IsUndef && (isSHUFPMask(M, VT, HasAVX, /* Commuted */ true)))
Nate Begeman9008ca62009-04-27 18:41:29 +00006932 return CommuteVectorShuffle(SVOp, DAG);
6933
Bruno Cardoso Lopes7256e222010-09-03 23:24:06 +00006934 // The checks below are all present in isShuffleMaskLegal, but they are
6935 // inlined here right now to enable us to directly emit target specific
6936 // nodes, and remove one by one until they don't return Op anymore.
Bruno Cardoso Lopes7256e222010-09-03 23:24:06 +00006937
Craig Topper0e2037b2012-01-20 05:53:00 +00006938 if (isPALIGNRMask(M, VT, Subtarget))
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00006939 return getTargetShuffleNode(X86ISD::PALIGN, dl, VT, V1, V2,
Craig Topperd93e4c32011-12-11 19:12:35 +00006940 getShufflePALIGNRImmediate(SVOp),
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00006941 DAG);
6942
Bruno Cardoso Lopesc800c0d2010-09-04 02:02:14 +00006943 if (ShuffleVectorSDNode::isSplatMask(&M[0], VT) &&
6944 SVOp->getSplatIndex() == 0 && V2IsUndef) {
Craig Topperbeabc6c2011-12-05 06:56:46 +00006945 if (VT == MVT::v2f64 || VT == MVT::v2i64)
Craig Topper34671b82011-12-06 08:21:25 +00006946 return getTargetShuffleNode(X86ISD::UNPCKL, dl, VT, V1, V1, DAG);
Bruno Cardoso Lopesc800c0d2010-09-04 02:02:14 +00006947 }
6948
Craig Toppera9a568a2012-05-02 08:03:44 +00006949 if (isPSHUFHWMask(M, VT, HasAVX2))
Bruno Cardoso Lopesbbfc3102010-09-04 01:36:45 +00006950 return getTargetShuffleNode(X86ISD::PSHUFHW, dl, VT, V1,
Craig Topper5aaffa82012-02-19 02:53:47 +00006951 getShufflePSHUFHWImmediate(SVOp),
Bruno Cardoso Lopesbbfc3102010-09-04 01:36:45 +00006952 DAG);
6953
Craig Toppera9a568a2012-05-02 08:03:44 +00006954 if (isPSHUFLWMask(M, VT, HasAVX2))
Bruno Cardoso Lopesbbfc3102010-09-04 01:36:45 +00006955 return getTargetShuffleNode(X86ISD::PSHUFLW, dl, VT, V1,
Craig Topper5aaffa82012-02-19 02:53:47 +00006956 getShufflePSHUFLWImmediate(SVOp),
Bruno Cardoso Lopesbbfc3102010-09-04 01:36:45 +00006957 DAG);
6958
Craig Topper1a7700a2012-01-19 08:19:12 +00006959 if (isSHUFPMask(M, VT, HasAVX))
Craig Topperb3982da2011-12-31 23:50:21 +00006960 return getTargetShuffleNode(X86ISD::SHUFP, dl, VT, V1, V2,
Craig Topper5aaffa82012-02-19 02:53:47 +00006961 getShuffleSHUFImmediate(SVOp), DAG);
Bruno Cardoso Lopes4c827f52010-09-04 01:22:57 +00006962
Craig Topper94438ba2011-12-16 08:06:31 +00006963 if (isUNPCKL_v_undef_Mask(M, VT, HasAVX2))
Craig Topper34671b82011-12-06 08:21:25 +00006964 return getTargetShuffleNode(X86ISD::UNPCKL, dl, VT, V1, V1, DAG);
Craig Topper94438ba2011-12-16 08:06:31 +00006965 if (isUNPCKH_v_undef_Mask(M, VT, HasAVX2))
Craig Topper34671b82011-12-06 08:21:25 +00006966 return getTargetShuffleNode(X86ISD::UNPCKH, dl, VT, V1, V1, DAG);
Bruno Cardoso Lopesa22c8452010-09-04 00:39:43 +00006967
Bruno Cardoso Lopesd0888342011-07-22 00:14:56 +00006968 //===--------------------------------------------------------------------===//
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00006969 // Generate target specific nodes for 128 or 256-bit shuffles only
6970 // supported in the AVX instruction set.
6971 //
Bruno Cardoso Lopesd0888342011-07-22 00:14:56 +00006972
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00006973 // Handle VMOVDDUPY permutations
Craig Topperbeabc6c2011-12-05 06:56:46 +00006974 if (V2IsUndef && isMOVDDUPYMask(M, VT, HasAVX))
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00006975 return getTargetShuffleNode(X86ISD::MOVDDUP, dl, VT, V1, DAG);
6976
Craig Topper70b883b2011-11-28 10:14:51 +00006977 // Handle VPERMILPS/D* permutations
Craig Topperdbd98a42012-02-07 06:28:42 +00006978 if (isVPERMILPMask(M, VT, HasAVX)) {
6979 if (HasAVX2 && VT == MVT::v8i32)
6980 return getTargetShuffleNode(X86ISD::PSHUFD, dl, VT, V1,
Craig Topper5aaffa82012-02-19 02:53:47 +00006981 getShuffleSHUFImmediate(SVOp), DAG);
Craig Topper316cd2a2011-11-30 06:25:25 +00006982 return getTargetShuffleNode(X86ISD::VPERMILP, dl, VT, V1,
Craig Topper5aaffa82012-02-19 02:53:47 +00006983 getShuffleSHUFImmediate(SVOp), DAG);
Craig Topperdbd98a42012-02-07 06:28:42 +00006984 }
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00006985
Craig Topper70b883b2011-11-28 10:14:51 +00006986 // Handle VPERM2F128/VPERM2I128 permutations
Craig Topperbeabc6c2011-12-05 06:56:46 +00006987 if (isVPERM2X128Mask(M, VT, HasAVX))
Craig Topperec24e612011-11-30 07:47:51 +00006988 return getTargetShuffleNode(X86ISD::VPERM2X128, dl, VT, V1,
Craig Topper70b883b2011-11-28 10:14:51 +00006989 V2, getShuffleVPERM2X128Immediate(SVOp), DAG);
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00006990
Craig Topper1842ba02012-04-23 06:38:28 +00006991 SDValue BlendOp = LowerVECTOR_SHUFFLEtoBlend(SVOp, Subtarget, DAG);
Nadav Roteme80aa7c2012-04-09 08:33:21 +00006992 if (BlendOp.getNode())
6993 return BlendOp;
Craig Topper095c5282012-04-15 23:48:57 +00006994
Elena Demikhovsky73c504a2012-04-15 11:18:59 +00006995 if (V2IsUndef && HasAVX2 && (VT == MVT::v8i32 || VT == MVT::v8f32)) {
Craig Topper095c5282012-04-15 23:48:57 +00006996 SmallVector<SDValue, 8> permclMask;
Elena Demikhovsky73c504a2012-04-15 11:18:59 +00006997 for (unsigned i = 0; i != 8; ++i) {
Craig Topper095c5282012-04-15 23:48:57 +00006998 permclMask.push_back(DAG.getConstant((M[i]>=0) ? M[i] : 0, MVT::i32));
Elena Demikhovsky73c504a2012-04-15 11:18:59 +00006999 }
Craig Topper92040742012-04-16 06:43:40 +00007000 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v8i32,
7001 &permclMask[0], 8);
7002 // Bitcast is for VPERMPS since mask is v8i32 but node takes v8f32
Craig Topper8325c112012-04-16 00:41:45 +00007003 return DAG.getNode(X86ISD::VPERMV, dl, VT,
Craig Topper92040742012-04-16 06:43:40 +00007004 DAG.getNode(ISD::BITCAST, dl, VT, Mask), V1);
Elena Demikhovsky73c504a2012-04-15 11:18:59 +00007005 }
Craig Topper095c5282012-04-15 23:48:57 +00007006
Craig Topper8325c112012-04-16 00:41:45 +00007007 if (V2IsUndef && HasAVX2 && (VT == MVT::v4i64 || VT == MVT::v4f64))
7008 return getTargetShuffleNode(X86ISD::VPERMI, dl, VT, V1,
Elena Demikhovsky73c504a2012-04-15 11:18:59 +00007009 getShuffleCLImmediate(SVOp), DAG);
7010
Nadav Roteme80aa7c2012-04-09 08:33:21 +00007011
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00007012 //===--------------------------------------------------------------------===//
7013 // Since no target specific shuffle was selected for this generic one,
7014 // lower it into other known shuffles. FIXME: this isn't true yet, but
7015 // this is the plan.
7016 //
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00007017
Bruno Cardoso Lopes9b4ad122011-07-27 00:56:37 +00007018 // Handle v8i16 specifically since SSE can do byte extraction and insertion.
7019 if (VT == MVT::v8i16) {
Craig Topper55b24052012-09-11 06:15:32 +00007020 SDValue NewOp = LowerVECTOR_SHUFFLEv8i16(Op, Subtarget, DAG);
Bruno Cardoso Lopes9b4ad122011-07-27 00:56:37 +00007021 if (NewOp.getNode())
7022 return NewOp;
7023 }
7024
7025 if (VT == MVT::v16i8) {
7026 SDValue NewOp = LowerVECTOR_SHUFFLEv16i8(SVOp, DAG, *this);
7027 if (NewOp.getNode())
7028 return NewOp;
7029 }
7030
Elena Demikhovsky41789462012-09-06 12:42:01 +00007031 if (VT == MVT::v32i8) {
Craig Topper55b24052012-09-11 06:15:32 +00007032 SDValue NewOp = LowerVECTOR_SHUFFLEv32i8(SVOp, Subtarget, DAG);
Elena Demikhovsky41789462012-09-06 12:42:01 +00007033 if (NewOp.getNode())
7034 return NewOp;
7035 }
7036
Bruno Cardoso Lopes9b4ad122011-07-27 00:56:37 +00007037 // Handle all 128-bit wide vectors with 4 elements, and match them with
7038 // several different shuffle types.
Craig Topper7a9a28b2012-08-12 02:23:29 +00007039 if (NumElems == 4 && VT.is128BitVector())
Bruno Cardoso Lopes9b4ad122011-07-27 00:56:37 +00007040 return LowerVECTOR_SHUFFLE_128v4(SVOp, DAG);
7041
Bruno Cardoso Lopesd0888342011-07-22 00:14:56 +00007042 // Handle general 256-bit shuffles
7043 if (VT.is256BitVector())
7044 return LowerVECTOR_SHUFFLE_256(SVOp, DAG);
7045
Dan Gohman475871a2008-07-27 21:46:04 +00007046 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007047}
7048
Dan Gohman475871a2008-07-27 21:46:04 +00007049SDValue
7050X86TargetLowering::LowerEXTRACT_VECTOR_ELT_SSE4(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00007051 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00007052 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007053 DebugLoc dl = Op.getDebugLoc();
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007054
Craig Topper7a9a28b2012-08-12 02:23:29 +00007055 if (!Op.getOperand(0).getValueType().is128BitVector())
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007056 return SDValue();
7057
Duncan Sands83ec4b62008-06-06 12:08:01 +00007058 if (VT.getSizeInBits() == 8) {
Owen Anderson825b72b2009-08-11 20:47:22 +00007059 SDValue Extract = DAG.getNode(X86ISD::PEXTRB, dl, MVT::i32,
Craig Topper7c022842012-09-12 06:20:41 +00007060 Op.getOperand(0), Op.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00007061 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Extract,
Craig Topper7c022842012-09-12 06:20:41 +00007062 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00007063 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Craig Topper69947b92012-04-23 06:57:04 +00007064 }
7065
7066 if (VT.getSizeInBits() == 16) {
Evan Cheng52ceafa2009-01-02 05:29:08 +00007067 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
7068 // If Idx is 0, it's cheaper to do a move instead of a pextrw.
7069 if (Idx == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00007070 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i16,
7071 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007072 DAG.getNode(ISD::BITCAST, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00007073 MVT::v4i32,
Evan Cheng52ceafa2009-01-02 05:29:08 +00007074 Op.getOperand(0)),
7075 Op.getOperand(1)));
Owen Anderson825b72b2009-08-11 20:47:22 +00007076 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, dl, MVT::i32,
Craig Topper7c022842012-09-12 06:20:41 +00007077 Op.getOperand(0), Op.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00007078 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Extract,
Craig Topper7c022842012-09-12 06:20:41 +00007079 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00007080 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Craig Topper69947b92012-04-23 06:57:04 +00007081 }
7082
7083 if (VT == MVT::f32) {
Evan Cheng62a3f152008-03-24 21:52:23 +00007084 // EXTRACTPS outputs to a GPR32 register which will require a movd to copy
7085 // the result back to FR32 register. It's only worth matching if the
Dan Gohmand17cfbe2008-10-31 00:57:24 +00007086 // result has a single use which is a store or a bitcast to i32. And in
7087 // the case of a store, it's not worth it if the index is a constant 0,
7088 // because a MOVSSmr can be used instead, which is smaller and faster.
Evan Cheng62a3f152008-03-24 21:52:23 +00007089 if (!Op.hasOneUse())
Dan Gohman475871a2008-07-27 21:46:04 +00007090 return SDValue();
Gabor Greifba36cb52008-08-28 21:40:38 +00007091 SDNode *User = *Op.getNode()->use_begin();
Dan Gohmand17cfbe2008-10-31 00:57:24 +00007092 if ((User->getOpcode() != ISD::STORE ||
7093 (isa<ConstantSDNode>(Op.getOperand(1)) &&
7094 cast<ConstantSDNode>(Op.getOperand(1))->isNullValue())) &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007095 (User->getOpcode() != ISD::BITCAST ||
Owen Anderson825b72b2009-08-11 20:47:22 +00007096 User->getValueType(0) != MVT::i32))
Dan Gohman475871a2008-07-27 21:46:04 +00007097 return SDValue();
Owen Anderson825b72b2009-08-11 20:47:22 +00007098 SDValue Extract = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007099 DAG.getNode(ISD::BITCAST, dl, MVT::v4i32,
Dale Johannesenace16102009-02-03 19:33:06 +00007100 Op.getOperand(0)),
7101 Op.getOperand(1));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007102 return DAG.getNode(ISD::BITCAST, dl, MVT::f32, Extract);
Craig Topper69947b92012-04-23 06:57:04 +00007103 }
7104
7105 if (VT == MVT::i32 || VT == MVT::i64) {
Pete Coopera77214a2011-11-14 19:38:42 +00007106 // ExtractPS/pextrq works with constant index.
Mon P Wangf0fcdd82009-01-15 21:10:20 +00007107 if (isa<ConstantSDNode>(Op.getOperand(1)))
7108 return Op;
Nate Begeman14d12ca2008-02-11 04:19:36 +00007109 }
Dan Gohman475871a2008-07-27 21:46:04 +00007110 return SDValue();
Nate Begeman14d12ca2008-02-11 04:19:36 +00007111}
7112
7113
Dan Gohman475871a2008-07-27 21:46:04 +00007114SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007115X86TargetLowering::LowerEXTRACT_VECTOR_ELT(SDValue Op,
7116 SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00007117 if (!isa<ConstantSDNode>(Op.getOperand(1)))
Dan Gohman475871a2008-07-27 21:46:04 +00007118 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007119
David Greene74a579d2011-02-10 16:57:36 +00007120 SDValue Vec = Op.getOperand(0);
7121 EVT VecVT = Vec.getValueType();
7122
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007123 // If this is a 256-bit vector result, first extract the 128-bit vector and
7124 // then extract the element from the 128-bit vector.
Craig Topper7a9a28b2012-08-12 02:23:29 +00007125 if (VecVT.is256BitVector()) {
David Greene74a579d2011-02-10 16:57:36 +00007126 DebugLoc dl = Op.getNode()->getDebugLoc();
7127 unsigned NumElems = VecVT.getVectorNumElements();
7128 SDValue Idx = Op.getOperand(1);
David Greene74a579d2011-02-10 16:57:36 +00007129 unsigned IdxVal = cast<ConstantSDNode>(Idx)->getZExtValue();
7130
7131 // Get the 128-bit vector.
Craig Topper7d1e3dc2012-04-30 05:17:10 +00007132 Vec = Extract128BitVector(Vec, IdxVal, DAG, dl);
David Greene74a579d2011-02-10 16:57:36 +00007133
Craig Topper7d1e3dc2012-04-30 05:17:10 +00007134 if (IdxVal >= NumElems/2)
7135 IdxVal -= NumElems/2;
David Greene74a579d2011-02-10 16:57:36 +00007136 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, Op.getValueType(), Vec,
Craig Topper7d1e3dc2012-04-30 05:17:10 +00007137 DAG.getConstant(IdxVal, MVT::i32));
David Greene74a579d2011-02-10 16:57:36 +00007138 }
7139
Craig Topper7a9a28b2012-08-12 02:23:29 +00007140 assert(VecVT.is128BitVector() && "Unexpected vector length");
David Greene74a579d2011-02-10 16:57:36 +00007141
Craig Topperd0a31172012-01-10 06:37:29 +00007142 if (Subtarget->hasSSE41()) {
Dan Gohman475871a2008-07-27 21:46:04 +00007143 SDValue Res = LowerEXTRACT_VECTOR_ELT_SSE4(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00007144 if (Res.getNode())
Evan Cheng62a3f152008-03-24 21:52:23 +00007145 return Res;
7146 }
Nate Begeman14d12ca2008-02-11 04:19:36 +00007147
Owen Andersone50ed302009-08-10 22:56:29 +00007148 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007149 DebugLoc dl = Op.getDebugLoc();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007150 // TODO: handle v16i8.
Duncan Sands83ec4b62008-06-06 12:08:01 +00007151 if (VT.getSizeInBits() == 16) {
Dan Gohman475871a2008-07-27 21:46:04 +00007152 SDValue Vec = Op.getOperand(0);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007153 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00007154 if (Idx == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00007155 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i16,
7156 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007157 DAG.getNode(ISD::BITCAST, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00007158 MVT::v4i32, Vec),
Evan Cheng14b32e12007-12-11 01:46:18 +00007159 Op.getOperand(1)));
Evan Cheng0db9fe62006-04-25 20:13:52 +00007160 // Transform it so it match pextrw which produces a 32-bit result.
Ken Dyck70d0ef12009-12-17 15:31:52 +00007161 EVT EltVT = MVT::i32;
Dan Gohman8a55ce42009-09-23 21:02:20 +00007162 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, dl, EltVT,
Craig Topper7c022842012-09-12 06:20:41 +00007163 Op.getOperand(0), Op.getOperand(1));
Dan Gohman8a55ce42009-09-23 21:02:20 +00007164 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, EltVT, Extract,
Craig Topper7c022842012-09-12 06:20:41 +00007165 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00007166 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Craig Topper69947b92012-04-23 06:57:04 +00007167 }
7168
7169 if (VT.getSizeInBits() == 32) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007170 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007171 if (Idx == 0)
7172 return Op;
Eric Christopherfd179292009-08-27 18:07:15 +00007173
Evan Cheng0db9fe62006-04-25 20:13:52 +00007174 // SHUFPS the element to the lowest double word, then movss.
Jeffrey Yasskina44defe2011-07-27 06:22:51 +00007175 int Mask[4] = { static_cast<int>(Idx), -1, -1, -1 };
Owen Andersone50ed302009-08-10 22:56:29 +00007176 EVT VVT = Op.getOperand(0).getValueType();
Eric Christopherfd179292009-08-27 18:07:15 +00007177 SDValue Vec = DAG.getVectorShuffle(VVT, dl, Op.getOperand(0),
Nate Begeman9008ca62009-04-27 18:41:29 +00007178 DAG.getUNDEF(VVT), Mask);
Dale Johannesenace16102009-02-03 19:33:06 +00007179 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, Vec,
Chris Lattner0bd48932008-01-17 07:00:52 +00007180 DAG.getIntPtrConstant(0));
Craig Topper69947b92012-04-23 06:57:04 +00007181 }
7182
7183 if (VT.getSizeInBits() == 64) {
Nate Begeman14d12ca2008-02-11 04:19:36 +00007184 // FIXME: .td only matches this for <2 x f64>, not <2 x i64> on 32b
7185 // FIXME: seems like this should be unnecessary if mov{h,l}pd were taught
7186 // to match extract_elt for f64.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007187 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007188 if (Idx == 0)
7189 return Op;
7190
7191 // UNPCKHPD the element to the lowest double word, then movsd.
7192 // Note if the lower 64 bits of the result of the UNPCKHPD is then stored
7193 // to a f64mem, the whole operation is folded into a single MOVHPDmr.
Nate Begeman9008ca62009-04-27 18:41:29 +00007194 int Mask[2] = { 1, -1 };
Owen Andersone50ed302009-08-10 22:56:29 +00007195 EVT VVT = Op.getOperand(0).getValueType();
Eric Christopherfd179292009-08-27 18:07:15 +00007196 SDValue Vec = DAG.getVectorShuffle(VVT, dl, Op.getOperand(0),
Nate Begeman9008ca62009-04-27 18:41:29 +00007197 DAG.getUNDEF(VVT), Mask);
Dale Johannesenace16102009-02-03 19:33:06 +00007198 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, Vec,
Chris Lattner0bd48932008-01-17 07:00:52 +00007199 DAG.getIntPtrConstant(0));
Evan Cheng0db9fe62006-04-25 20:13:52 +00007200 }
7201
Dan Gohman475871a2008-07-27 21:46:04 +00007202 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007203}
7204
Dan Gohman475871a2008-07-27 21:46:04 +00007205SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007206X86TargetLowering::LowerINSERT_VECTOR_ELT_SSE4(SDValue Op,
7207 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00007208 EVT VT = Op.getValueType();
Dan Gohman8a55ce42009-09-23 21:02:20 +00007209 EVT EltVT = VT.getVectorElementType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007210 DebugLoc dl = Op.getDebugLoc();
Nate Begeman14d12ca2008-02-11 04:19:36 +00007211
Dan Gohman475871a2008-07-27 21:46:04 +00007212 SDValue N0 = Op.getOperand(0);
7213 SDValue N1 = Op.getOperand(1);
7214 SDValue N2 = Op.getOperand(2);
Nate Begeman14d12ca2008-02-11 04:19:36 +00007215
Craig Topper7a9a28b2012-08-12 02:23:29 +00007216 if (!VT.is128BitVector())
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007217 return SDValue();
7218
Dan Gohman8a55ce42009-09-23 21:02:20 +00007219 if ((EltVT.getSizeInBits() == 8 || EltVT.getSizeInBits() == 16) &&
Dan Gohmanef521f12008-08-14 22:53:18 +00007220 isa<ConstantSDNode>(N2)) {
Chris Lattner8f2b4cc2010-02-23 02:07:48 +00007221 unsigned Opc;
7222 if (VT == MVT::v8i16)
7223 Opc = X86ISD::PINSRW;
Chris Lattner8f2b4cc2010-02-23 02:07:48 +00007224 else if (VT == MVT::v16i8)
7225 Opc = X86ISD::PINSRB;
7226 else
7227 Opc = X86ISD::PINSRB;
7228
Nate Begeman14d12ca2008-02-11 04:19:36 +00007229 // Transform it so it match pinsr{b,w} which expects a GR32 as its second
7230 // argument.
Owen Anderson825b72b2009-08-11 20:47:22 +00007231 if (N1.getValueType() != MVT::i32)
7232 N1 = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, N1);
7233 if (N2.getValueType() != MVT::i32)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007234 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Dale Johannesenace16102009-02-03 19:33:06 +00007235 return DAG.getNode(Opc, dl, VT, N0, N1, N2);
Craig Topper69947b92012-04-23 06:57:04 +00007236 }
7237
7238 if (EltVT == MVT::f32 && isa<ConstantSDNode>(N2)) {
Nate Begeman14d12ca2008-02-11 04:19:36 +00007239 // Bits [7:6] of the constant are the source select. This will always be
7240 // zero here. The DAG Combiner may combine an extract_elt index into these
7241 // bits. For example (insert (extract, 3), 2) could be matched by putting
7242 // the '3' into bits [7:6] of X86ISD::INSERTPS.
Scott Michelfdc40a02009-02-17 22:15:04 +00007243 // Bits [5:4] of the constant are the destination select. This is the
Nate Begeman14d12ca2008-02-11 04:19:36 +00007244 // value of the incoming immediate.
Scott Michelfdc40a02009-02-17 22:15:04 +00007245 // Bits [3:0] of the constant are the zero mask. The DAG Combiner may
Nate Begeman14d12ca2008-02-11 04:19:36 +00007246 // combine either bitwise AND or insert of float 0.0 to set these bits.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007247 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue() << 4);
Eric Christopherfbd66872009-07-24 00:33:09 +00007248 // Create this as a scalar to vector..
Owen Anderson825b72b2009-08-11 20:47:22 +00007249 N1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4f32, N1);
Dale Johannesenace16102009-02-03 19:33:06 +00007250 return DAG.getNode(X86ISD::INSERTPS, dl, VT, N0, N1, N2);
Craig Topper69947b92012-04-23 06:57:04 +00007251 }
7252
7253 if ((EltVT == MVT::i32 || EltVT == MVT::i64) && isa<ConstantSDNode>(N2)) {
Eric Christopherfbd66872009-07-24 00:33:09 +00007254 // PINSR* works with constant index.
7255 return Op;
Nate Begeman14d12ca2008-02-11 04:19:36 +00007256 }
Dan Gohman475871a2008-07-27 21:46:04 +00007257 return SDValue();
Nate Begeman14d12ca2008-02-11 04:19:36 +00007258}
7259
Dan Gohman475871a2008-07-27 21:46:04 +00007260SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007261X86TargetLowering::LowerINSERT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00007262 EVT VT = Op.getValueType();
Dan Gohman8a55ce42009-09-23 21:02:20 +00007263 EVT EltVT = VT.getVectorElementType();
Nate Begeman14d12ca2008-02-11 04:19:36 +00007264
David Greene6b381262011-02-09 15:32:06 +00007265 DebugLoc dl = Op.getDebugLoc();
7266 SDValue N0 = Op.getOperand(0);
7267 SDValue N1 = Op.getOperand(1);
7268 SDValue N2 = Op.getOperand(2);
7269
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007270 // If this is a 256-bit vector result, first extract the 128-bit vector,
7271 // insert the element into the extracted half and then place it back.
Craig Topper7a9a28b2012-08-12 02:23:29 +00007272 if (VT.is256BitVector()) {
David Greene6b381262011-02-09 15:32:06 +00007273 if (!isa<ConstantSDNode>(N2))
7274 return SDValue();
7275
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007276 // Get the desired 128-bit vector half.
David Greene6b381262011-02-09 15:32:06 +00007277 unsigned NumElems = VT.getVectorNumElements();
7278 unsigned IdxVal = cast<ConstantSDNode>(N2)->getZExtValue();
Craig Topper7d1e3dc2012-04-30 05:17:10 +00007279 SDValue V = Extract128BitVector(N0, IdxVal, DAG, dl);
David Greene6b381262011-02-09 15:32:06 +00007280
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007281 // Insert the element into the desired half.
Craig Topper7d1e3dc2012-04-30 05:17:10 +00007282 bool Upper = IdxVal >= NumElems/2;
7283 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, V.getValueType(), V, N1,
7284 DAG.getConstant(Upper ? IdxVal-NumElems/2 : IdxVal, MVT::i32));
David Greene6b381262011-02-09 15:32:06 +00007285
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007286 // Insert the changed part back to the 256-bit vector
Craig Topper7d1e3dc2012-04-30 05:17:10 +00007287 return Insert128BitVector(N0, V, IdxVal, DAG, dl);
David Greene6b381262011-02-09 15:32:06 +00007288 }
7289
Craig Topperd0a31172012-01-10 06:37:29 +00007290 if (Subtarget->hasSSE41())
Nate Begeman14d12ca2008-02-11 04:19:36 +00007291 return LowerINSERT_VECTOR_ELT_SSE4(Op, DAG);
7292
Dan Gohman8a55ce42009-09-23 21:02:20 +00007293 if (EltVT == MVT::i8)
Dan Gohman475871a2008-07-27 21:46:04 +00007294 return SDValue();
Evan Cheng794405e2007-12-12 07:55:34 +00007295
Dan Gohman8a55ce42009-09-23 21:02:20 +00007296 if (EltVT.getSizeInBits() == 16 && isa<ConstantSDNode>(N2)) {
Evan Cheng794405e2007-12-12 07:55:34 +00007297 // Transform it so it match pinsrw which expects a 16-bit value in a GR32
7298 // as its second argument.
Owen Anderson825b72b2009-08-11 20:47:22 +00007299 if (N1.getValueType() != MVT::i32)
7300 N1 = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, N1);
7301 if (N2.getValueType() != MVT::i32)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007302 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Dale Johannesen0488fb62010-09-30 23:57:10 +00007303 return DAG.getNode(X86ISD::PINSRW, dl, VT, N0, N1, N2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007304 }
Dan Gohman475871a2008-07-27 21:46:04 +00007305 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007306}
7307
Craig Topper55b24052012-09-11 06:15:32 +00007308static SDValue LowerSCALAR_TO_VECTOR(SDValue Op, SelectionDAG &DAG) {
Bruno Cardoso Lopes233fa392011-07-25 23:05:16 +00007309 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007310 DebugLoc dl = Op.getDebugLoc();
David Greene2fcdfb42011-02-10 23:11:29 +00007311 EVT OpVT = Op.getValueType();
7312
Bruno Cardoso Lopes233fa392011-07-25 23:05:16 +00007313 // If this is a 256-bit vector result, first insert into a 128-bit
7314 // vector and then insert into the 256-bit vector.
Craig Topper7a9a28b2012-08-12 02:23:29 +00007315 if (!OpVT.is128BitVector()) {
Bruno Cardoso Lopes233fa392011-07-25 23:05:16 +00007316 // Insert into a 128-bit vector.
7317 EVT VT128 = EVT::getVectorVT(*Context,
7318 OpVT.getVectorElementType(),
7319 OpVT.getVectorNumElements() / 2);
7320
7321 Op = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT128, Op.getOperand(0));
7322
7323 // Insert the 128-bit vector.
Craig Topperb14940a2012-04-22 20:55:18 +00007324 return Insert128BitVector(DAG.getUNDEF(OpVT), Op, 0, DAG, dl);
Bruno Cardoso Lopes233fa392011-07-25 23:05:16 +00007325 }
7326
Craig Topperd77d2fe2012-04-29 20:22:05 +00007327 if (OpVT == MVT::v1i64 &&
Chris Lattnerf172ecd2010-07-04 23:07:25 +00007328 Op.getOperand(0).getValueType() == MVT::i64)
Owen Anderson825b72b2009-08-11 20:47:22 +00007329 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v1i64, Op.getOperand(0));
Rafael Espindoladef390a2009-08-03 02:45:34 +00007330
Owen Anderson825b72b2009-08-11 20:47:22 +00007331 SDValue AnyExt = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, Op.getOperand(0));
Craig Topper7a9a28b2012-08-12 02:23:29 +00007332 assert(OpVT.is128BitVector() && "Expected an SSE type!");
Craig Topperd77d2fe2012-04-29 20:22:05 +00007333 return DAG.getNode(ISD::BITCAST, dl, OpVT,
Dale Johannesen0488fb62010-09-30 23:57:10 +00007334 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,AnyExt));
Evan Cheng0db9fe62006-04-25 20:13:52 +00007335}
7336
David Greene91585092011-01-26 15:38:49 +00007337// Lower a node with an EXTRACT_SUBVECTOR opcode. This may result in
7338// a simple subregister reference or explicit instructions to grab
7339// upper bits of a vector.
Craig Topper55b24052012-09-11 06:15:32 +00007340static SDValue LowerEXTRACT_SUBVECTOR(SDValue Op, const X86Subtarget *Subtarget,
7341 SelectionDAG &DAG) {
David Greene91585092011-01-26 15:38:49 +00007342 if (Subtarget->hasAVX()) {
David Greenea5f26012011-02-07 19:36:54 +00007343 DebugLoc dl = Op.getNode()->getDebugLoc();
7344 SDValue Vec = Op.getNode()->getOperand(0);
7345 SDValue Idx = Op.getNode()->getOperand(1);
7346
Craig Topper7a9a28b2012-08-12 02:23:29 +00007347 if (Op.getNode()->getValueType(0).is128BitVector() &&
7348 Vec.getNode()->getValueType(0).is256BitVector() &&
Craig Topperb14940a2012-04-22 20:55:18 +00007349 isa<ConstantSDNode>(Idx)) {
7350 unsigned IdxVal = cast<ConstantSDNode>(Idx)->getZExtValue();
7351 return Extract128BitVector(Vec, IdxVal, DAG, dl);
David Greenea5f26012011-02-07 19:36:54 +00007352 }
David Greene91585092011-01-26 15:38:49 +00007353 }
7354 return SDValue();
7355}
7356
David Greenecfe33c42011-01-26 19:13:22 +00007357// Lower a node with an INSERT_SUBVECTOR opcode. This may result in a
7358// simple superregister reference or explicit instructions to insert
7359// the upper bits of a vector.
Craig Topper55b24052012-09-11 06:15:32 +00007360static SDValue LowerINSERT_SUBVECTOR(SDValue Op, const X86Subtarget *Subtarget,
7361 SelectionDAG &DAG) {
David Greenecfe33c42011-01-26 19:13:22 +00007362 if (Subtarget->hasAVX()) {
7363 DebugLoc dl = Op.getNode()->getDebugLoc();
7364 SDValue Vec = Op.getNode()->getOperand(0);
7365 SDValue SubVec = Op.getNode()->getOperand(1);
7366 SDValue Idx = Op.getNode()->getOperand(2);
7367
Craig Topper7a9a28b2012-08-12 02:23:29 +00007368 if (Op.getNode()->getValueType(0).is256BitVector() &&
7369 SubVec.getNode()->getValueType(0).is128BitVector() &&
Craig Topperb14940a2012-04-22 20:55:18 +00007370 isa<ConstantSDNode>(Idx)) {
7371 unsigned IdxVal = cast<ConstantSDNode>(Idx)->getZExtValue();
7372 return Insert128BitVector(Vec, SubVec, IdxVal, DAG, dl);
David Greenecfe33c42011-01-26 19:13:22 +00007373 }
7374 }
7375 return SDValue();
7376}
7377
Bill Wendling056292f2008-09-16 21:48:12 +00007378// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
7379// their target countpart wrapped in the X86ISD::Wrapper node. Suppose N is
7380// one of the above mentioned nodes. It has to be wrapped because otherwise
7381// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
7382// be used to form addressing mode. These wrapped nodes will be selected
7383// into MOV32ri.
Dan Gohman475871a2008-07-27 21:46:04 +00007384SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007385X86TargetLowering::LowerConstantPool(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00007386 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Eric Christopherfd179292009-08-27 18:07:15 +00007387
Chris Lattner41621a22009-06-26 19:22:52 +00007388 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
7389 // global base reg.
7390 unsigned char OpFlag = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00007391 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007392 CodeModel::Model M = getTargetMachine().getCodeModel();
7393
Chris Lattner4f066492009-07-11 20:29:19 +00007394 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007395 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattnere4df7562009-07-09 03:15:51 +00007396 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00007397 else if (Subtarget->isPICStyleGOT())
Chris Lattner88e1fd52009-07-09 04:24:46 +00007398 OpFlag = X86II::MO_GOTOFF;
Chris Lattnere2c92082009-07-10 21:00:45 +00007399 else if (Subtarget->isPICStyleStubPIC())
Chris Lattner88e1fd52009-07-09 04:24:46 +00007400 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Eric Christopherfd179292009-08-27 18:07:15 +00007401
Evan Cheng1606e8e2009-03-13 07:51:59 +00007402 SDValue Result = DAG.getTargetConstantPool(CP->getConstVal(), getPointerTy(),
Chris Lattner41621a22009-06-26 19:22:52 +00007403 CP->getAlignment(),
7404 CP->getOffset(), OpFlag);
7405 DebugLoc DL = CP->getDebugLoc();
Chris Lattner18c59872009-06-27 04:16:01 +00007406 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00007407 // With PIC, the address is actually $g + Offset.
Chris Lattner41621a22009-06-26 19:22:52 +00007408 if (OpFlag) {
7409 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesenb300d2a2009-02-07 00:55:49 +00007410 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00007411 DebugLoc(), getPointerTy()),
Anton Korobeynikov7f705592007-01-12 19:20:47 +00007412 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007413 }
7414
7415 return Result;
7416}
7417
Dan Gohmand858e902010-04-17 15:26:15 +00007418SDValue X86TargetLowering::LowerJumpTable(SDValue Op, SelectionDAG &DAG) const {
Chris Lattner18c59872009-06-27 04:16:01 +00007419 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
Eric Christopherfd179292009-08-27 18:07:15 +00007420
Chris Lattner18c59872009-06-27 04:16:01 +00007421 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
7422 // global base reg.
7423 unsigned char OpFlag = 0;
7424 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007425 CodeModel::Model M = getTargetMachine().getCodeModel();
7426
Chris Lattner4f066492009-07-11 20:29:19 +00007427 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007428 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattnere4df7562009-07-09 03:15:51 +00007429 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00007430 else if (Subtarget->isPICStyleGOT())
Chris Lattner88e1fd52009-07-09 04:24:46 +00007431 OpFlag = X86II::MO_GOTOFF;
Chris Lattnere2c92082009-07-10 21:00:45 +00007432 else if (Subtarget->isPICStyleStubPIC())
Chris Lattner88e1fd52009-07-09 04:24:46 +00007433 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Eric Christopherfd179292009-08-27 18:07:15 +00007434
Chris Lattner18c59872009-06-27 04:16:01 +00007435 SDValue Result = DAG.getTargetJumpTable(JT->getIndex(), getPointerTy(),
7436 OpFlag);
7437 DebugLoc DL = JT->getDebugLoc();
7438 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Eric Christopherfd179292009-08-27 18:07:15 +00007439
Chris Lattner18c59872009-06-27 04:16:01 +00007440 // With PIC, the address is actually $g + Offset.
Chris Lattner1e61e692010-11-15 02:46:57 +00007441 if (OpFlag)
Chris Lattner18c59872009-06-27 04:16:01 +00007442 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
7443 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00007444 DebugLoc(), getPointerTy()),
Chris Lattner18c59872009-06-27 04:16:01 +00007445 Result);
Eric Christopherfd179292009-08-27 18:07:15 +00007446
Chris Lattner18c59872009-06-27 04:16:01 +00007447 return Result;
7448}
7449
7450SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007451X86TargetLowering::LowerExternalSymbol(SDValue Op, SelectionDAG &DAG) const {
Chris Lattner18c59872009-06-27 04:16:01 +00007452 const char *Sym = cast<ExternalSymbolSDNode>(Op)->getSymbol();
Eric Christopherfd179292009-08-27 18:07:15 +00007453
Chris Lattner18c59872009-06-27 04:16:01 +00007454 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
7455 // global base reg.
7456 unsigned char OpFlag = 0;
7457 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007458 CodeModel::Model M = getTargetMachine().getCodeModel();
7459
Chris Lattner4f066492009-07-11 20:29:19 +00007460 if (Subtarget->isPICStyleRIPRel() &&
Eli Friedman586272d2011-08-11 01:48:05 +00007461 (M == CodeModel::Small || M == CodeModel::Kernel)) {
7462 if (Subtarget->isTargetDarwin() || Subtarget->isTargetELF())
7463 OpFlag = X86II::MO_GOTPCREL;
Chris Lattnere4df7562009-07-09 03:15:51 +00007464 WrapperKind = X86ISD::WrapperRIP;
Eli Friedman586272d2011-08-11 01:48:05 +00007465 } else if (Subtarget->isPICStyleGOT()) {
7466 OpFlag = X86II::MO_GOT;
7467 } else if (Subtarget->isPICStyleStubPIC()) {
7468 OpFlag = X86II::MO_DARWIN_NONLAZY_PIC_BASE;
7469 } else if (Subtarget->isPICStyleStubNoDynamic()) {
7470 OpFlag = X86II::MO_DARWIN_NONLAZY;
7471 }
Eric Christopherfd179292009-08-27 18:07:15 +00007472
Chris Lattner18c59872009-06-27 04:16:01 +00007473 SDValue Result = DAG.getTargetExternalSymbol(Sym, getPointerTy(), OpFlag);
Eric Christopherfd179292009-08-27 18:07:15 +00007474
Chris Lattner18c59872009-06-27 04:16:01 +00007475 DebugLoc DL = Op.getDebugLoc();
7476 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Eric Christopherfd179292009-08-27 18:07:15 +00007477
7478
Chris Lattner18c59872009-06-27 04:16:01 +00007479 // With PIC, the address is actually $g + Offset.
7480 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
Chris Lattnere4df7562009-07-09 03:15:51 +00007481 !Subtarget->is64Bit()) {
Chris Lattner18c59872009-06-27 04:16:01 +00007482 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
7483 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00007484 DebugLoc(), getPointerTy()),
Chris Lattner18c59872009-06-27 04:16:01 +00007485 Result);
7486 }
Eric Christopherfd179292009-08-27 18:07:15 +00007487
Eli Friedman586272d2011-08-11 01:48:05 +00007488 // For symbols that require a load from a stub to get the address, emit the
7489 // load.
7490 if (isGlobalStubReference(OpFlag))
7491 Result = DAG.getLoad(getPointerTy(), DL, DAG.getEntryNode(), Result,
Pete Cooperd752e0f2011-11-08 18:42:53 +00007492 MachinePointerInfo::getGOT(), false, false, false, 0);
Eli Friedman586272d2011-08-11 01:48:05 +00007493
Chris Lattner18c59872009-06-27 04:16:01 +00007494 return Result;
7495}
7496
Dan Gohman475871a2008-07-27 21:46:04 +00007497SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007498X86TargetLowering::LowerBlockAddress(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman29cbade2009-11-20 23:18:13 +00007499 // Create the TargetBlockAddressAddress node.
7500 unsigned char OpFlags =
7501 Subtarget->ClassifyBlockAddressReference();
Dan Gohmanf705adb2009-10-30 01:28:02 +00007502 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman46510a72010-04-15 01:51:59 +00007503 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
Michael Liao6c7ccaa2012-09-12 21:43:09 +00007504 int64_t Offset = cast<BlockAddressSDNode>(Op)->getOffset();
Dan Gohman29cbade2009-11-20 23:18:13 +00007505 DebugLoc dl = Op.getDebugLoc();
Michael Liao6c7ccaa2012-09-12 21:43:09 +00007506 SDValue Result = DAG.getTargetBlockAddress(BA, getPointerTy(), Offset,
7507 OpFlags);
Dan Gohman29cbade2009-11-20 23:18:13 +00007508
Dan Gohmanf705adb2009-10-30 01:28:02 +00007509 if (Subtarget->isPICStyleRIPRel() &&
7510 (M == CodeModel::Small || M == CodeModel::Kernel))
Dan Gohman29cbade2009-11-20 23:18:13 +00007511 Result = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Result);
7512 else
7513 Result = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), Result);
Dan Gohmanf705adb2009-10-30 01:28:02 +00007514
Dan Gohman29cbade2009-11-20 23:18:13 +00007515 // With PIC, the address is actually $g + Offset.
7516 if (isGlobalRelativeToPICBase(OpFlags)) {
7517 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(),
7518 DAG.getNode(X86ISD::GlobalBaseReg, dl, getPointerTy()),
7519 Result);
7520 }
Dan Gohmanf705adb2009-10-30 01:28:02 +00007521
7522 return Result;
7523}
7524
7525SDValue
Dale Johannesen33c960f2009-02-04 20:06:27 +00007526X86TargetLowering::LowerGlobalAddress(const GlobalValue *GV, DebugLoc dl,
Dan Gohman6520e202008-10-18 02:06:02 +00007527 int64_t Offset,
Evan Chengda43bcf2008-09-24 00:05:32 +00007528 SelectionDAG &DAG) const {
Dan Gohman6520e202008-10-18 02:06:02 +00007529 // Create the TargetGlobalAddress node, folding in the constant
7530 // offset if it is legal.
Chris Lattnerd392bd92009-07-10 07:20:05 +00007531 unsigned char OpFlags =
7532 Subtarget->ClassifyGlobalReference(GV, getTargetMachine());
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007533 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman6520e202008-10-18 02:06:02 +00007534 SDValue Result;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007535 if (OpFlags == X86II::MO_NO_FLAG &&
7536 X86::isOffsetSuitableForCodeModel(Offset, M)) {
Chris Lattner4aa21aa2009-07-09 00:58:53 +00007537 // A direct static reference to a global.
Devang Patel0d881da2010-07-06 22:08:15 +00007538 Result = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(), Offset);
Dan Gohman6520e202008-10-18 02:06:02 +00007539 Offset = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00007540 } else {
Devang Patel0d881da2010-07-06 22:08:15 +00007541 Result = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(), 0, OpFlags);
Chris Lattner18c59872009-06-27 04:16:01 +00007542 }
Eric Christopherfd179292009-08-27 18:07:15 +00007543
Chris Lattner4f066492009-07-11 20:29:19 +00007544 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007545 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattner18c59872009-06-27 04:16:01 +00007546 Result = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Result);
7547 else
7548 Result = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), Result);
Dan Gohman6520e202008-10-18 02:06:02 +00007549
Anton Korobeynikov7f705592007-01-12 19:20:47 +00007550 // With PIC, the address is actually $g + Offset.
Chris Lattner36c25012009-07-10 07:34:39 +00007551 if (isGlobalRelativeToPICBase(OpFlags)) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00007552 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(),
7553 DAG.getNode(X86ISD::GlobalBaseReg, dl, getPointerTy()),
Anton Korobeynikov7f705592007-01-12 19:20:47 +00007554 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007555 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007556
Chris Lattner36c25012009-07-10 07:34:39 +00007557 // For globals that require a load from a stub to get the address, emit the
7558 // load.
7559 if (isGlobalStubReference(OpFlags))
Dale Johannesen33c960f2009-02-04 20:06:27 +00007560 Result = DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(), Result,
Pete Cooperd752e0f2011-11-08 18:42:53 +00007561 MachinePointerInfo::getGOT(), false, false, false, 0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007562
Dan Gohman6520e202008-10-18 02:06:02 +00007563 // If there was a non-zero offset that we didn't fold, create an explicit
7564 // addition for it.
7565 if (Offset != 0)
Dale Johannesen33c960f2009-02-04 20:06:27 +00007566 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(), Result,
Dan Gohman6520e202008-10-18 02:06:02 +00007567 DAG.getConstant(Offset, getPointerTy()));
7568
Evan Cheng0db9fe62006-04-25 20:13:52 +00007569 return Result;
7570}
7571
Evan Chengda43bcf2008-09-24 00:05:32 +00007572SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007573X86TargetLowering::LowerGlobalAddress(SDValue Op, SelectionDAG &DAG) const {
Evan Chengda43bcf2008-09-24 00:05:32 +00007574 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Dan Gohman6520e202008-10-18 02:06:02 +00007575 int64_t Offset = cast<GlobalAddressSDNode>(Op)->getOffset();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007576 return LowerGlobalAddress(GV, Op.getDebugLoc(), Offset, DAG);
Evan Chengda43bcf2008-09-24 00:05:32 +00007577}
7578
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007579static SDValue
7580GetTLSADDR(SelectionDAG &DAG, SDValue Chain, GlobalAddressSDNode *GA,
Owen Andersone50ed302009-08-10 22:56:29 +00007581 SDValue *InFlag, const EVT PtrVT, unsigned ReturnReg,
Hans Wennborgf0234fc2012-06-01 16:27:21 +00007582 unsigned char OperandFlags, bool LocalDynamic = false) {
Anton Korobeynikov817a4642009-12-11 19:39:55 +00007583 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00007584 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007585 DebugLoc dl = GA->getDebugLoc();
Devang Patel0d881da2010-07-06 22:08:15 +00007586 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl,
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007587 GA->getValueType(0),
Chris Lattnerb903bed2009-06-26 21:20:29 +00007588 GA->getOffset(),
7589 OperandFlags);
Hans Wennborgf0234fc2012-06-01 16:27:21 +00007590
7591 X86ISD::NodeType CallType = LocalDynamic ? X86ISD::TLSBASEADDR
7592 : X86ISD::TLSADDR;
7593
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007594 if (InFlag) {
7595 SDValue Ops[] = { Chain, TGA, *InFlag };
Hans Wennborgf0234fc2012-06-01 16:27:21 +00007596 Chain = DAG.getNode(CallType, dl, NodeTys, Ops, 3);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007597 } else {
7598 SDValue Ops[] = { Chain, TGA };
Hans Wennborgf0234fc2012-06-01 16:27:21 +00007599 Chain = DAG.getNode(CallType, dl, NodeTys, Ops, 2);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007600 }
Anton Korobeynikov817a4642009-12-11 19:39:55 +00007601
7602 // TLSADDR will be codegen'ed as call. Inform MFI that function has calls.
Bill Wendlingb92187a2010-05-14 21:14:32 +00007603 MFI->setAdjustsStack(true);
Anton Korobeynikov817a4642009-12-11 19:39:55 +00007604
Rafael Espindola15f1b662009-04-24 12:59:40 +00007605 SDValue Flag = Chain.getValue(1);
7606 return DAG.getCopyFromReg(Chain, dl, ReturnReg, PtrVT, Flag);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007607}
7608
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007609// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 32 bit
Dan Gohman475871a2008-07-27 21:46:04 +00007610static SDValue
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007611LowerToTLSGeneralDynamicModel32(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00007612 const EVT PtrVT) {
Dan Gohman475871a2008-07-27 21:46:04 +00007613 SDValue InFlag;
Dale Johannesendd64c412009-02-04 00:33:20 +00007614 DebugLoc dl = GA->getDebugLoc(); // ? function entry point might be better
7615 SDValue Chain = DAG.getCopyToReg(DAG.getEntryNode(), dl, X86::EBX,
Craig Topper7c022842012-09-12 06:20:41 +00007616 DAG.getNode(X86ISD::GlobalBaseReg,
7617 DebugLoc(), PtrVT), InFlag);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007618 InFlag = Chain.getValue(1);
7619
Chris Lattnerb903bed2009-06-26 21:20:29 +00007620 return GetTLSADDR(DAG, Chain, GA, &InFlag, PtrVT, X86::EAX, X86II::MO_TLSGD);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007621}
7622
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007623// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 64 bit
Dan Gohman475871a2008-07-27 21:46:04 +00007624static SDValue
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007625LowerToTLSGeneralDynamicModel64(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00007626 const EVT PtrVT) {
Chris Lattnerb903bed2009-06-26 21:20:29 +00007627 return GetTLSADDR(DAG, DAG.getEntryNode(), GA, NULL, PtrVT,
7628 X86::RAX, X86II::MO_TLSGD);
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007629}
7630
Hans Wennborgf0234fc2012-06-01 16:27:21 +00007631static SDValue LowerToTLSLocalDynamicModel(GlobalAddressSDNode *GA,
7632 SelectionDAG &DAG,
7633 const EVT PtrVT,
7634 bool is64Bit) {
7635 DebugLoc dl = GA->getDebugLoc();
7636
7637 // Get the start address of the TLS block for this module.
7638 X86MachineFunctionInfo* MFI = DAG.getMachineFunction()
7639 .getInfo<X86MachineFunctionInfo>();
7640 MFI->incNumLocalDynamicTLSAccesses();
7641
7642 SDValue Base;
7643 if (is64Bit) {
7644 Base = GetTLSADDR(DAG, DAG.getEntryNode(), GA, NULL, PtrVT, X86::RAX,
7645 X86II::MO_TLSLD, /*LocalDynamic=*/true);
7646 } else {
7647 SDValue InFlag;
7648 SDValue Chain = DAG.getCopyToReg(DAG.getEntryNode(), dl, X86::EBX,
7649 DAG.getNode(X86ISD::GlobalBaseReg, DebugLoc(), PtrVT), InFlag);
7650 InFlag = Chain.getValue(1);
7651 Base = GetTLSADDR(DAG, Chain, GA, &InFlag, PtrVT, X86::EAX,
7652 X86II::MO_TLSLDM, /*LocalDynamic=*/true);
7653 }
7654
7655 // Note: the CleanupLocalDynamicTLSPass will remove redundant computations
7656 // of Base.
7657
7658 // Build x@dtpoff.
7659 unsigned char OperandFlags = X86II::MO_DTPOFF;
7660 unsigned WrapperKind = X86ISD::Wrapper;
7661 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl,
7662 GA->getValueType(0),
7663 GA->getOffset(), OperandFlags);
7664 SDValue Offset = DAG.getNode(WrapperKind, dl, PtrVT, TGA);
7665
7666 // Add x@dtpoff with the base.
7667 return DAG.getNode(ISD::ADD, dl, PtrVT, Offset, Base);
7668}
7669
Hans Wennborg228756c2012-05-11 10:11:01 +00007670// Lower ISD::GlobalTLSAddress using the "initial exec" or "local exec" model.
Dan Gohman475871a2008-07-27 21:46:04 +00007671static SDValue LowerToTLSExecModel(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00007672 const EVT PtrVT, TLSModel::Model model,
Hans Wennborg228756c2012-05-11 10:11:01 +00007673 bool is64Bit, bool isPIC) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00007674 DebugLoc dl = GA->getDebugLoc();
Michael J. Spencerec38de22010-10-10 22:04:20 +00007675
Chris Lattnerf93b90c2010-09-22 04:39:11 +00007676 // Get the Thread Pointer, which is %gs:0 (32-bit) or %fs:0 (64-bit).
7677 Value *Ptr = Constant::getNullValue(Type::getInt8PtrTy(*DAG.getContext(),
7678 is64Bit ? 257 : 256));
Rafael Espindola094fad32009-04-08 21:14:34 +00007679
Michael J. Spencerec38de22010-10-10 22:04:20 +00007680 SDValue ThreadPointer = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(),
Chris Lattnerf93b90c2010-09-22 04:39:11 +00007681 DAG.getIntPtrConstant(0),
Pete Cooperd752e0f2011-11-08 18:42:53 +00007682 MachinePointerInfo(Ptr),
7683 false, false, false, 0);
Rafael Espindola094fad32009-04-08 21:14:34 +00007684
Chris Lattnerb903bed2009-06-26 21:20:29 +00007685 unsigned char OperandFlags = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00007686 // Most TLS accesses are not RIP relative, even on x86-64. One exception is
7687 // initialexec.
7688 unsigned WrapperKind = X86ISD::Wrapper;
7689 if (model == TLSModel::LocalExec) {
Chris Lattnerb903bed2009-06-26 21:20:29 +00007690 OperandFlags = is64Bit ? X86II::MO_TPOFF : X86II::MO_NTPOFF;
Hans Wennborg228756c2012-05-11 10:11:01 +00007691 } else if (model == TLSModel::InitialExec) {
7692 if (is64Bit) {
7693 OperandFlags = X86II::MO_GOTTPOFF;
7694 WrapperKind = X86ISD::WrapperRIP;
7695 } else {
7696 OperandFlags = isPIC ? X86II::MO_GOTNTPOFF : X86II::MO_INDNTPOFF;
7697 }
Chris Lattner18c59872009-06-27 04:16:01 +00007698 } else {
Hans Wennborg228756c2012-05-11 10:11:01 +00007699 llvm_unreachable("Unexpected model");
Chris Lattnerb903bed2009-06-26 21:20:29 +00007700 }
Eric Christopherfd179292009-08-27 18:07:15 +00007701
Hans Wennborg228756c2012-05-11 10:11:01 +00007702 // emit "addl x@ntpoff,%eax" (local exec)
7703 // or "addl x@indntpoff,%eax" (initial exec)
7704 // or "addl x@gotntpoff(%ebx) ,%eax" (initial exec, 32-bit pic)
Michael J. Spencerec38de22010-10-10 22:04:20 +00007705 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl,
Devang Patel0d881da2010-07-06 22:08:15 +00007706 GA->getValueType(0),
Chris Lattnerb903bed2009-06-26 21:20:29 +00007707 GA->getOffset(), OperandFlags);
Chris Lattner18c59872009-06-27 04:16:01 +00007708 SDValue Offset = DAG.getNode(WrapperKind, dl, PtrVT, TGA);
Lauro Ramos Venancio7d2cc2b2007-04-22 22:50:52 +00007709
Hans Wennborg228756c2012-05-11 10:11:01 +00007710 if (model == TLSModel::InitialExec) {
7711 if (isPIC && !is64Bit) {
7712 Offset = DAG.getNode(ISD::ADD, dl, PtrVT,
7713 DAG.getNode(X86ISD::GlobalBaseReg, DebugLoc(), PtrVT),
7714 Offset);
Hans Wennborg228756c2012-05-11 10:11:01 +00007715 }
Rafael Espindola94e3b382012-06-29 04:22:35 +00007716
7717 Offset = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Offset,
7718 MachinePointerInfo::getGOT(), false, false, false,
7719 0);
Hans Wennborg228756c2012-05-11 10:11:01 +00007720 }
Lauro Ramos Venancio7d2cc2b2007-04-22 22:50:52 +00007721
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007722 // The address of the thread local variable is the add of the thread
7723 // pointer with the offset of the variable.
Dale Johannesen33c960f2009-02-04 20:06:27 +00007724 return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007725}
7726
Dan Gohman475871a2008-07-27 21:46:04 +00007727SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007728X86TargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const {
Michael J. Spencerec38de22010-10-10 22:04:20 +00007729
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007730 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
Chris Lattnerb903bed2009-06-26 21:20:29 +00007731 const GlobalValue *GV = GA->getGlobal();
Eric Christopherfd179292009-08-27 18:07:15 +00007732
Eric Christopher30ef0e52010-06-03 04:07:48 +00007733 if (Subtarget->isTargetELF()) {
Chandler Carruth34797132012-04-08 17:20:55 +00007734 TLSModel::Model model = getTargetMachine().getTLSModel(GV);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007735
Eric Christopher30ef0e52010-06-03 04:07:48 +00007736 switch (model) {
7737 case TLSModel::GeneralDynamic:
Eric Christopher30ef0e52010-06-03 04:07:48 +00007738 if (Subtarget->is64Bit())
7739 return LowerToTLSGeneralDynamicModel64(GA, DAG, getPointerTy());
7740 return LowerToTLSGeneralDynamicModel32(GA, DAG, getPointerTy());
Hans Wennborgf0234fc2012-06-01 16:27:21 +00007741 case TLSModel::LocalDynamic:
7742 return LowerToTLSLocalDynamicModel(GA, DAG, getPointerTy(),
7743 Subtarget->is64Bit());
Eric Christopher30ef0e52010-06-03 04:07:48 +00007744 case TLSModel::InitialExec:
7745 case TLSModel::LocalExec:
7746 return LowerToTLSExecModel(GA, DAG, getPointerTy(), model,
Hans Wennborg228756c2012-05-11 10:11:01 +00007747 Subtarget->is64Bit(),
7748 getTargetMachine().getRelocationModel() == Reloc::PIC_);
Eric Christopher30ef0e52010-06-03 04:07:48 +00007749 }
Craig Toppere8eb1162012-04-23 03:26:18 +00007750 llvm_unreachable("Unknown TLS model.");
7751 }
7752
7753 if (Subtarget->isTargetDarwin()) {
Eric Christopher30ef0e52010-06-03 04:07:48 +00007754 // Darwin only has one model of TLS. Lower to that.
7755 unsigned char OpFlag = 0;
7756 unsigned WrapperKind = Subtarget->isPICStyleRIPRel() ?
7757 X86ISD::WrapperRIP : X86ISD::Wrapper;
Michael J. Spencerec38de22010-10-10 22:04:20 +00007758
Eric Christopher30ef0e52010-06-03 04:07:48 +00007759 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
7760 // global base reg.
7761 bool PIC32 = (getTargetMachine().getRelocationModel() == Reloc::PIC_) &&
7762 !Subtarget->is64Bit();
7763 if (PIC32)
7764 OpFlag = X86II::MO_TLVP_PIC_BASE;
7765 else
7766 OpFlag = X86II::MO_TLVP;
Michael J. Spencerec38de22010-10-10 22:04:20 +00007767 DebugLoc DL = Op.getDebugLoc();
Devang Patel0d881da2010-07-06 22:08:15 +00007768 SDValue Result = DAG.getTargetGlobalAddress(GA->getGlobal(), DL,
Eric Christopherd8c05362010-12-09 06:25:53 +00007769 GA->getValueType(0),
Eric Christopher30ef0e52010-06-03 04:07:48 +00007770 GA->getOffset(), OpFlag);
Eric Christopher30ef0e52010-06-03 04:07:48 +00007771 SDValue Offset = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007772
Eric Christopher30ef0e52010-06-03 04:07:48 +00007773 // With PIC32, the address is actually $g + Offset.
7774 if (PIC32)
7775 Offset = DAG.getNode(ISD::ADD, DL, getPointerTy(),
7776 DAG.getNode(X86ISD::GlobalBaseReg,
7777 DebugLoc(), getPointerTy()),
7778 Offset);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007779
Eric Christopher30ef0e52010-06-03 04:07:48 +00007780 // Lowering the machine isd will make sure everything is in the right
7781 // location.
Eric Christopherd8c05362010-12-09 06:25:53 +00007782 SDValue Chain = DAG.getEntryNode();
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00007783 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Eric Christopherd8c05362010-12-09 06:25:53 +00007784 SDValue Args[] = { Chain, Offset };
7785 Chain = DAG.getNode(X86ISD::TLSCALL, DL, NodeTys, Args, 2);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007786
Eric Christopher30ef0e52010-06-03 04:07:48 +00007787 // TLSCALL will be codegen'ed as call. Inform MFI that function has calls.
7788 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
7789 MFI->setAdjustsStack(true);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00007790
Eric Christopher30ef0e52010-06-03 04:07:48 +00007791 // And our return value (tls address) is in the standard call return value
7792 // location.
Eric Christopherd8c05362010-12-09 06:25:53 +00007793 unsigned Reg = Subtarget->is64Bit() ? X86::RAX : X86::EAX;
Evan Chengfd230df2011-10-19 22:22:54 +00007794 return DAG.getCopyFromReg(Chain, DL, Reg, getPointerTy(),
7795 Chain.getValue(1));
Craig Toppere8eb1162012-04-23 03:26:18 +00007796 }
7797
7798 if (Subtarget->isTargetWindows()) {
Anton Korobeynikovd4a19b62012-02-11 17:26:53 +00007799 // Just use the implicit TLS architecture
7800 // Need to generate someting similar to:
7801 // mov rdx, qword [gs:abs 58H]; Load pointer to ThreadLocalStorage
7802 // ; from TEB
7803 // mov ecx, dword [rel _tls_index]: Load index (from C runtime)
7804 // mov rcx, qword [rdx+rcx*8]
7805 // mov eax, .tls$:tlsvar
7806 // [rax+rcx] contains the address
7807 // Windows 64bit: gs:0x58
7808 // Windows 32bit: fs:__tls_array
7809
7810 // If GV is an alias then use the aliasee for determining
7811 // thread-localness.
7812 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV))
7813 GV = GA->resolveAliasedGlobal(false);
7814 DebugLoc dl = GA->getDebugLoc();
7815 SDValue Chain = DAG.getEntryNode();
7816
7817 // Get the Thread Pointer, which is %fs:__tls_array (32-bit) or
7818 // %gs:0x58 (64-bit).
7819 Value *Ptr = Constant::getNullValue(Subtarget->is64Bit()
7820 ? Type::getInt8PtrTy(*DAG.getContext(),
7821 256)
7822 : Type::getInt32PtrTy(*DAG.getContext(),
7823 257));
7824
7825 SDValue ThreadPointer = DAG.getLoad(getPointerTy(), dl, Chain,
7826 Subtarget->is64Bit()
7827 ? DAG.getIntPtrConstant(0x58)
7828 : DAG.getExternalSymbol("_tls_array",
7829 getPointerTy()),
7830 MachinePointerInfo(Ptr),
7831 false, false, false, 0);
7832
7833 // Load the _tls_index variable
7834 SDValue IDX = DAG.getExternalSymbol("_tls_index", getPointerTy());
7835 if (Subtarget->is64Bit())
7836 IDX = DAG.getExtLoad(ISD::ZEXTLOAD, dl, getPointerTy(), Chain,
7837 IDX, MachinePointerInfo(), MVT::i32,
7838 false, false, 0);
7839 else
7840 IDX = DAG.getLoad(getPointerTy(), dl, Chain, IDX, MachinePointerInfo(),
7841 false, false, false, 0);
7842
Chandler Carruth426c2bf2012-11-01 09:14:31 +00007843 SDValue Scale = DAG.getConstant(Log2_64_Ceil(TD->getPointerSize()),
Craig Topper0fbf3642012-04-23 03:28:34 +00007844 getPointerTy());
Anton Korobeynikovd4a19b62012-02-11 17:26:53 +00007845 IDX = DAG.getNode(ISD::SHL, dl, getPointerTy(), IDX, Scale);
7846
7847 SDValue res = DAG.getNode(ISD::ADD, dl, getPointerTy(), ThreadPointer, IDX);
7848 res = DAG.getLoad(getPointerTy(), dl, Chain, res, MachinePointerInfo(),
7849 false, false, false, 0);
7850
7851 // Get the offset of start of .tls section
7852 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl,
7853 GA->getValueType(0),
7854 GA->getOffset(), X86II::MO_SECREL);
7855 SDValue Offset = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), TGA);
7856
7857 // The address of the thread local variable is the add of the thread
7858 // pointer with the offset of the variable.
7859 return DAG.getNode(ISD::ADD, dl, getPointerTy(), res, Offset);
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007860 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00007861
David Blaikie4d6ccb52012-01-20 21:51:11 +00007862 llvm_unreachable("TLS not implemented for this target.");
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007863}
7864
Evan Cheng0db9fe62006-04-25 20:13:52 +00007865
Chad Rosierb90d2a92012-01-03 23:19:12 +00007866/// LowerShiftParts - Lower SRA_PARTS and friends, which return two i32 values
7867/// and take a 2 x i32 value to shift plus a shift amount.
7868SDValue X86TargetLowering::LowerShiftParts(SDValue Op, SelectionDAG &DAG) const{
Dan Gohman4c1fa612008-03-03 22:22:09 +00007869 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
Owen Andersone50ed302009-08-10 22:56:29 +00007870 EVT VT = Op.getValueType();
Duncan Sands83ec4b62008-06-06 12:08:01 +00007871 unsigned VTBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007872 DebugLoc dl = Op.getDebugLoc();
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007873 bool isSRA = Op.getOpcode() == ISD::SRA_PARTS;
Dan Gohman475871a2008-07-27 21:46:04 +00007874 SDValue ShOpLo = Op.getOperand(0);
7875 SDValue ShOpHi = Op.getOperand(1);
7876 SDValue ShAmt = Op.getOperand(2);
Chris Lattner31dcfe62009-07-29 05:48:09 +00007877 SDValue Tmp1 = isSRA ? DAG.getNode(ISD::SRA, dl, VT, ShOpHi,
Owen Anderson825b72b2009-08-11 20:47:22 +00007878 DAG.getConstant(VTBits - 1, MVT::i8))
Chris Lattner31dcfe62009-07-29 05:48:09 +00007879 : DAG.getConstant(0, VT);
Evan Chenge3413162006-01-09 18:33:28 +00007880
Dan Gohman475871a2008-07-27 21:46:04 +00007881 SDValue Tmp2, Tmp3;
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007882 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dale Johannesenace16102009-02-03 19:33:06 +00007883 Tmp2 = DAG.getNode(X86ISD::SHLD, dl, VT, ShOpHi, ShOpLo, ShAmt);
7884 Tmp3 = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ShAmt);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007885 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00007886 Tmp2 = DAG.getNode(X86ISD::SHRD, dl, VT, ShOpLo, ShOpHi, ShAmt);
7887 Tmp3 = DAG.getNode(isSRA ? ISD::SRA : ISD::SRL, dl, VT, ShOpHi, ShAmt);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007888 }
Evan Chenge3413162006-01-09 18:33:28 +00007889
Owen Anderson825b72b2009-08-11 20:47:22 +00007890 SDValue AndNode = DAG.getNode(ISD::AND, dl, MVT::i8, ShAmt,
7891 DAG.getConstant(VTBits, MVT::i8));
Chris Lattnerccfea352010-02-22 00:28:59 +00007892 SDValue Cond = DAG.getNode(X86ISD::CMP, dl, MVT::i32,
Owen Anderson825b72b2009-08-11 20:47:22 +00007893 AndNode, DAG.getConstant(0, MVT::i8));
Evan Chenge3413162006-01-09 18:33:28 +00007894
Dan Gohman475871a2008-07-27 21:46:04 +00007895 SDValue Hi, Lo;
Owen Anderson825b72b2009-08-11 20:47:22 +00007896 SDValue CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Dan Gohman475871a2008-07-27 21:46:04 +00007897 SDValue Ops0[4] = { Tmp2, Tmp3, CC, Cond };
7898 SDValue Ops1[4] = { Tmp3, Tmp1, CC, Cond };
Duncan Sandsf9516202008-06-30 10:19:09 +00007899
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007900 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dale Johannesenace16102009-02-03 19:33:06 +00007901 Hi = DAG.getNode(X86ISD::CMOV, dl, VT, Ops0, 4);
7902 Lo = DAG.getNode(X86ISD::CMOV, dl, VT, Ops1, 4);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007903 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00007904 Lo = DAG.getNode(X86ISD::CMOV, dl, VT, Ops0, 4);
7905 Hi = DAG.getNode(X86ISD::CMOV, dl, VT, Ops1, 4);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007906 }
7907
Dan Gohman475871a2008-07-27 21:46:04 +00007908 SDValue Ops[2] = { Lo, Hi };
Dale Johannesenace16102009-02-03 19:33:06 +00007909 return DAG.getMergeValues(Ops, 2, dl);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007910}
Evan Chenga3195e82006-01-12 22:54:21 +00007911
Dan Gohmand858e902010-04-17 15:26:15 +00007912SDValue X86TargetLowering::LowerSINT_TO_FP(SDValue Op,
7913 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00007914 EVT SrcVT = Op.getOperand(0).getValueType();
Eli Friedman23ef1052009-06-06 03:57:58 +00007915
Dale Johannesen0488fb62010-09-30 23:57:10 +00007916 if (SrcVT.isVector())
Eli Friedman23ef1052009-06-06 03:57:58 +00007917 return SDValue();
Eli Friedman23ef1052009-06-06 03:57:58 +00007918
Owen Anderson825b72b2009-08-11 20:47:22 +00007919 assert(SrcVT.getSimpleVT() <= MVT::i64 && SrcVT.getSimpleVT() >= MVT::i16 &&
Chris Lattnerb09916b2008-02-27 05:57:41 +00007920 "Unknown SINT_TO_FP to lower!");
Scott Michelfdc40a02009-02-17 22:15:04 +00007921
Eli Friedman36df4992009-05-27 00:47:34 +00007922 // These are really Legal; return the operand so the caller accepts it as
7923 // Legal.
Owen Anderson825b72b2009-08-11 20:47:22 +00007924 if (SrcVT == MVT::i32 && isScalarFPTypeInSSEReg(Op.getValueType()))
Eli Friedman36df4992009-05-27 00:47:34 +00007925 return Op;
Owen Anderson825b72b2009-08-11 20:47:22 +00007926 if (SrcVT == MVT::i64 && isScalarFPTypeInSSEReg(Op.getValueType()) &&
Eli Friedman36df4992009-05-27 00:47:34 +00007927 Subtarget->is64Bit()) {
7928 return Op;
7929 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007930
Bruno Cardoso Lopesa511b8e2011-08-09 17:39:01 +00007931 DebugLoc dl = Op.getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +00007932 unsigned Size = SrcVT.getSizeInBits()/8;
Evan Cheng0db9fe62006-04-25 20:13:52 +00007933 MachineFunction &MF = DAG.getMachineFunction();
David Greene3f2bf852009-11-12 20:49:22 +00007934 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size, false);
Dan Gohman475871a2008-07-27 21:46:04 +00007935 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Dale Johannesenace16102009-02-03 19:33:06 +00007936 SDValue Chain = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Bill Wendling105be5a2009-03-13 08:41:47 +00007937 StackSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00007938 MachinePointerInfo::getFixedStack(SSFI),
David Greene67c9d422010-02-15 16:53:33 +00007939 false, false, 0);
Eli Friedman948e95a2009-05-23 09:59:16 +00007940 return BuildFILD(Op, SrcVT, Chain, StackSlot, DAG);
7941}
Evan Cheng0db9fe62006-04-25 20:13:52 +00007942
Owen Andersone50ed302009-08-10 22:56:29 +00007943SDValue X86TargetLowering::BuildFILD(SDValue Op, EVT SrcVT, SDValue Chain,
Michael J. Spencerec38de22010-10-10 22:04:20 +00007944 SDValue StackSlot,
Dan Gohmand858e902010-04-17 15:26:15 +00007945 SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00007946 // Build the FILD
Chris Lattner492a43e2010-09-22 01:28:21 +00007947 DebugLoc DL = Op.getDebugLoc();
Chris Lattner5a88b832007-02-25 07:10:00 +00007948 SDVTList Tys;
Chris Lattner78631162008-01-16 06:24:21 +00007949 bool useSSE = isScalarFPTypeInSSEReg(Op.getValueType());
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00007950 if (useSSE)
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00007951 Tys = DAG.getVTList(MVT::f64, MVT::Other, MVT::Glue);
Chris Lattner5a88b832007-02-25 07:10:00 +00007952 else
Owen Anderson825b72b2009-08-11 20:47:22 +00007953 Tys = DAG.getVTList(Op.getValueType(), MVT::Other);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007954
Chris Lattner492a43e2010-09-22 01:28:21 +00007955 unsigned ByteSize = SrcVT.getSizeInBits()/8;
Michael J. Spencerec38de22010-10-10 22:04:20 +00007956
Stuart Hastings84be9582011-06-02 15:57:11 +00007957 FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(StackSlot);
7958 MachineMemOperand *MMO;
7959 if (FI) {
7960 int SSFI = FI->getIndex();
7961 MMO =
7962 DAG.getMachineFunction()
7963 .getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
7964 MachineMemOperand::MOLoad, ByteSize, ByteSize);
7965 } else {
7966 MMO = cast<LoadSDNode>(StackSlot)->getMemOperand();
7967 StackSlot = StackSlot.getOperand(1);
7968 }
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00007969 SDValue Ops[] = { Chain, StackSlot, DAG.getValueType(SrcVT) };
Chris Lattner492a43e2010-09-22 01:28:21 +00007970 SDValue Result = DAG.getMemIntrinsicNode(useSSE ? X86ISD::FILD_FLAG :
7971 X86ISD::FILD, DL,
7972 Tys, Ops, array_lengthof(Ops),
7973 SrcVT, MMO);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007974
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00007975 if (useSSE) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00007976 Chain = Result.getValue(1);
Dan Gohman475871a2008-07-27 21:46:04 +00007977 SDValue InFlag = Result.getValue(2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007978
7979 // FIXME: Currently the FST is flagged to the FILD_FLAG. This
7980 // shouldn't be necessary except that RFP cannot be live across
7981 // multiple blocks. When stackifier is fixed, they can be uncoupled.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00007982 MachineFunction &MF = DAG.getMachineFunction();
Bob Wilsoneafca4e2010-09-22 17:35:14 +00007983 unsigned SSFISize = Op.getValueType().getSizeInBits()/8;
7984 int SSFI = MF.getFrameInfo()->CreateStackObject(SSFISize, SSFISize, false);
Dan Gohman475871a2008-07-27 21:46:04 +00007985 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Owen Anderson825b72b2009-08-11 20:47:22 +00007986 Tys = DAG.getVTList(MVT::Other);
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00007987 SDValue Ops[] = {
7988 Chain, Result, StackSlot, DAG.getValueType(Op.getValueType()), InFlag
7989 };
Chris Lattner492a43e2010-09-22 01:28:21 +00007990 MachineMemOperand *MMO =
7991 DAG.getMachineFunction()
7992 .getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
Bob Wilsoneafca4e2010-09-22 17:35:14 +00007993 MachineMemOperand::MOStore, SSFISize, SSFISize);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007994
Chris Lattner492a43e2010-09-22 01:28:21 +00007995 Chain = DAG.getMemIntrinsicNode(X86ISD::FST, DL, Tys,
7996 Ops, array_lengthof(Ops),
7997 Op.getValueType(), MMO);
7998 Result = DAG.getLoad(Op.getValueType(), DL, Chain, StackSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00007999 MachinePointerInfo::getFixedStack(SSFI),
Pete Cooperd752e0f2011-11-08 18:42:53 +00008000 false, false, false, 0);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00008001 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00008002
Evan Cheng0db9fe62006-04-25 20:13:52 +00008003 return Result;
8004}
8005
Bill Wendling8b8a6362009-01-17 03:56:04 +00008006// LowerUINT_TO_FP_i64 - 64-bit unsigned integer to double expansion.
Dan Gohmand858e902010-04-17 15:26:15 +00008007SDValue X86TargetLowering::LowerUINT_TO_FP_i64(SDValue Op,
8008 SelectionDAG &DAG) const {
Bill Wendling397ae212012-01-05 02:13:20 +00008009 // This algorithm is not obvious. Here it is what we're trying to output:
Bill Wendling8b8a6362009-01-17 03:56:04 +00008010 /*
Bill Wendling397ae212012-01-05 02:13:20 +00008011 movq %rax, %xmm0
8012 punpckldq (c0), %xmm0 // c0: (uint4){ 0x43300000U, 0x45300000U, 0U, 0U }
8013 subpd (c1), %xmm0 // c1: (double2){ 0x1.0p52, 0x1.0p52 * 0x1.0p32 }
8014 #ifdef __SSE3__
Chad Rosiera20e1e72012-08-01 18:39:17 +00008015 haddpd %xmm0, %xmm0
Bill Wendling397ae212012-01-05 02:13:20 +00008016 #else
Chad Rosiera20e1e72012-08-01 18:39:17 +00008017 pshufd $0x4e, %xmm0, %xmm1
Bill Wendling397ae212012-01-05 02:13:20 +00008018 addpd %xmm1, %xmm0
8019 #endif
Bill Wendling8b8a6362009-01-17 03:56:04 +00008020 */
Dale Johannesen040225f2008-10-21 23:07:49 +00008021
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008022 DebugLoc dl = Op.getDebugLoc();
Owen Andersona90b3dc2009-07-15 21:51:10 +00008023 LLVMContext *Context = DAG.getContext();
Dale Johannesenace16102009-02-03 19:33:06 +00008024
Dale Johannesen1c15bf52008-10-21 20:50:01 +00008025 // Build some magic constants.
Chris Lattner7302d802012-02-06 21:56:39 +00008026 const uint32_t CV0[] = { 0x43300000, 0x45300000, 0, 0 };
8027 Constant *C0 = ConstantDataVector::get(*Context, CV0);
Evan Cheng1606e8e2009-03-13 07:51:59 +00008028 SDValue CPIdx0 = DAG.getConstantPool(C0, getPointerTy(), 16);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00008029
Chris Lattner97484792012-01-25 09:56:22 +00008030 SmallVector<Constant*,2> CV1;
8031 CV1.push_back(
Chris Lattner4ca829e2012-01-25 06:02:56 +00008032 ConstantFP::get(*Context, APFloat(APInt(64, 0x4330000000000000ULL))));
Chris Lattner97484792012-01-25 09:56:22 +00008033 CV1.push_back(
8034 ConstantFP::get(*Context, APFloat(APInt(64, 0x4530000000000000ULL))));
8035 Constant *C1 = ConstantVector::get(CV1);
Evan Cheng1606e8e2009-03-13 07:51:59 +00008036 SDValue CPIdx1 = DAG.getConstantPool(C1, getPointerTy(), 16);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00008037
Bill Wendling397ae212012-01-05 02:13:20 +00008038 // Load the 64-bit value into an XMM register.
8039 SDValue XR1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i64,
8040 Op.getOperand(0));
Owen Anderson825b72b2009-08-11 20:47:22 +00008041 SDValue CLod0 = DAG.getLoad(MVT::v4i32, dl, DAG.getEntryNode(), CPIdx0,
Chris Lattnere8639032010-09-21 06:22:23 +00008042 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00008043 false, false, false, 16);
Bill Wendling397ae212012-01-05 02:13:20 +00008044 SDValue Unpck1 = getUnpackl(DAG, dl, MVT::v4i32,
8045 DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, XR1),
8046 CLod0);
8047
Owen Anderson825b72b2009-08-11 20:47:22 +00008048 SDValue CLod1 = DAG.getLoad(MVT::v2f64, dl, CLod0.getValue(1), CPIdx1,
Chris Lattnere8639032010-09-21 06:22:23 +00008049 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00008050 false, false, false, 16);
Bill Wendling397ae212012-01-05 02:13:20 +00008051 SDValue XR2F = DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Unpck1);
Owen Anderson825b72b2009-08-11 20:47:22 +00008052 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::v2f64, XR2F, CLod1);
Bill Wendling397ae212012-01-05 02:13:20 +00008053 SDValue Result;
Bill Wendling8b8a6362009-01-17 03:56:04 +00008054
Craig Topperd0a31172012-01-10 06:37:29 +00008055 if (Subtarget->hasSSE3()) {
Bill Wendling397ae212012-01-05 02:13:20 +00008056 // FIXME: The 'haddpd' instruction may be slower than 'movhlps + addsd'.
8057 Result = DAG.getNode(X86ISD::FHADD, dl, MVT::v2f64, Sub, Sub);
8058 } else {
8059 SDValue S2F = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, Sub);
8060 SDValue Shuffle = getTargetShuffleNode(X86ISD::PSHUFD, dl, MVT::v4i32,
8061 S2F, 0x4E, DAG);
8062 Result = DAG.getNode(ISD::FADD, dl, MVT::v2f64,
8063 DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Shuffle),
8064 Sub);
8065 }
8066
8067 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Result,
Dale Johannesen1c15bf52008-10-21 20:50:01 +00008068 DAG.getIntPtrConstant(0));
8069}
8070
Bill Wendling8b8a6362009-01-17 03:56:04 +00008071// LowerUINT_TO_FP_i32 - 32-bit unsigned integer to float expansion.
Dan Gohmand858e902010-04-17 15:26:15 +00008072SDValue X86TargetLowering::LowerUINT_TO_FP_i32(SDValue Op,
8073 SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008074 DebugLoc dl = Op.getDebugLoc();
Bill Wendling8b8a6362009-01-17 03:56:04 +00008075 // FP constant to bias correct the final result.
8076 SDValue Bias = DAG.getConstantFP(BitsToDouble(0x4330000000000000ULL),
Owen Anderson825b72b2009-08-11 20:47:22 +00008077 MVT::f64);
Bill Wendling8b8a6362009-01-17 03:56:04 +00008078
8079 // Load the 32-bit value into an XMM register.
Owen Anderson825b72b2009-08-11 20:47:22 +00008080 SDValue Load = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
Eli Friedman6cdc1f42011-08-02 18:38:35 +00008081 Op.getOperand(0));
Bill Wendling8b8a6362009-01-17 03:56:04 +00008082
Eli Friedmanf3704762011-08-29 21:15:46 +00008083 // Zero out the upper parts of the register.
Craig Topper12216172012-01-13 08:12:35 +00008084 Load = getShuffleVectorZeroOrUndef(Load, 0, true, Subtarget, DAG);
Eli Friedmanf3704762011-08-29 21:15:46 +00008085
Owen Anderson825b72b2009-08-11 20:47:22 +00008086 Load = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008087 DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Load),
Bill Wendling8b8a6362009-01-17 03:56:04 +00008088 DAG.getIntPtrConstant(0));
8089
8090 // Or the load with the bias.
Owen Anderson825b72b2009-08-11 20:47:22 +00008091 SDValue Or = DAG.getNode(ISD::OR, dl, MVT::v2i64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008092 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00008093 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00008094 MVT::v2f64, Load)),
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008095 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00008096 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00008097 MVT::v2f64, Bias)));
8098 Or = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008099 DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Or),
Bill Wendling8b8a6362009-01-17 03:56:04 +00008100 DAG.getIntPtrConstant(0));
8101
8102 // Subtract the bias.
Owen Anderson825b72b2009-08-11 20:47:22 +00008103 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::f64, Or, Bias);
Bill Wendling8b8a6362009-01-17 03:56:04 +00008104
8105 // Handle final rounding.
Owen Andersone50ed302009-08-10 22:56:29 +00008106 EVT DestVT = Op.getValueType();
Bill Wendling030939c2009-01-17 07:40:19 +00008107
Craig Topper69947b92012-04-23 06:57:04 +00008108 if (DestVT.bitsLT(MVT::f64))
Dale Johannesenace16102009-02-03 19:33:06 +00008109 return DAG.getNode(ISD::FP_ROUND, dl, DestVT, Sub,
Bill Wendling030939c2009-01-17 07:40:19 +00008110 DAG.getIntPtrConstant(0));
Craig Topper69947b92012-04-23 06:57:04 +00008111 if (DestVT.bitsGT(MVT::f64))
Dale Johannesenace16102009-02-03 19:33:06 +00008112 return DAG.getNode(ISD::FP_EXTEND, dl, DestVT, Sub);
Bill Wendling030939c2009-01-17 07:40:19 +00008113
8114 // Handle final rounding.
8115 return Sub;
Bill Wendling8b8a6362009-01-17 03:56:04 +00008116}
8117
Michael Liaoa7554632012-10-23 17:36:08 +00008118SDValue X86TargetLowering::lowerUINT_TO_FP_vec(SDValue Op,
8119 SelectionDAG &DAG) const {
8120 SDValue N0 = Op.getOperand(0);
8121 EVT SVT = N0.getValueType();
8122 DebugLoc dl = Op.getDebugLoc();
8123
8124 assert((SVT == MVT::v4i8 || SVT == MVT::v4i16 ||
8125 SVT == MVT::v8i8 || SVT == MVT::v8i16) &&
8126 "Custom UINT_TO_FP is not supported!");
8127
8128 EVT NVT = EVT::getVectorVT(*DAG.getContext(), MVT::i32, SVT.getVectorNumElements());
8129 return DAG.getNode(ISD::SINT_TO_FP, dl, Op.getValueType(),
8130 DAG.getNode(ISD::ZERO_EXTEND, dl, NVT, N0));
8131}
8132
Dan Gohmand858e902010-04-17 15:26:15 +00008133SDValue X86TargetLowering::LowerUINT_TO_FP(SDValue Op,
8134 SelectionDAG &DAG) const {
Evan Chenga06ec9e2009-01-19 08:08:22 +00008135 SDValue N0 = Op.getOperand(0);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008136 DebugLoc dl = Op.getDebugLoc();
Bill Wendling8b8a6362009-01-17 03:56:04 +00008137
Michael Liaoa7554632012-10-23 17:36:08 +00008138 if (Op.getValueType().isVector())
8139 return lowerUINT_TO_FP_vec(Op, DAG);
8140
Dale Johannesen8d908eb2010-05-15 18:51:12 +00008141 // Since UINT_TO_FP is legal (it's marked custom), dag combiner won't
Evan Chenga06ec9e2009-01-19 08:08:22 +00008142 // optimize it to a SINT_TO_FP when the sign bit is known zero. Perform
8143 // the optimization here.
8144 if (DAG.SignBitIsZero(N0))
Dale Johannesenace16102009-02-03 19:33:06 +00008145 return DAG.getNode(ISD::SINT_TO_FP, dl, Op.getValueType(), N0);
Evan Chenga06ec9e2009-01-19 08:08:22 +00008146
Owen Andersone50ed302009-08-10 22:56:29 +00008147 EVT SrcVT = N0.getValueType();
Dale Johannesen8d908eb2010-05-15 18:51:12 +00008148 EVT DstVT = Op.getValueType();
8149 if (SrcVT == MVT::i64 && DstVT == MVT::f64 && X86ScalarSSEf64)
Bill Wendling8b8a6362009-01-17 03:56:04 +00008150 return LowerUINT_TO_FP_i64(Op, DAG);
Craig Topper69947b92012-04-23 06:57:04 +00008151 if (SrcVT == MVT::i32 && X86ScalarSSEf64)
Bill Wendling8b8a6362009-01-17 03:56:04 +00008152 return LowerUINT_TO_FP_i32(Op, DAG);
Craig Topper69947b92012-04-23 06:57:04 +00008153 if (Subtarget->is64Bit() && SrcVT == MVT::i64 && DstVT == MVT::f32)
Bill Wendling397ae212012-01-05 02:13:20 +00008154 return SDValue();
Eli Friedman948e95a2009-05-23 09:59:16 +00008155
8156 // Make a 64-bit buffer, and use it to build an FILD.
Owen Anderson825b72b2009-08-11 20:47:22 +00008157 SDValue StackSlot = DAG.CreateStackTemporary(MVT::i64);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00008158 if (SrcVT == MVT::i32) {
8159 SDValue WordOff = DAG.getConstant(4, getPointerTy());
8160 SDValue OffsetSlot = DAG.getNode(ISD::ADD, dl,
8161 getPointerTy(), StackSlot, WordOff);
8162 SDValue Store1 = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Chris Lattner8026a9d2010-09-21 17:50:43 +00008163 StackSlot, MachinePointerInfo(),
8164 false, false, 0);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00008165 SDValue Store2 = DAG.getStore(Store1, dl, DAG.getConstant(0, MVT::i32),
Chris Lattner8026a9d2010-09-21 17:50:43 +00008166 OffsetSlot, MachinePointerInfo(),
8167 false, false, 0);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00008168 SDValue Fild = BuildFILD(Op, MVT::i64, Store2, StackSlot, DAG);
8169 return Fild;
8170 }
8171
8172 assert(SrcVT == MVT::i64 && "Unexpected type in UINT_TO_FP");
8173 SDValue Store = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Bill Wendlingf6c07472012-01-10 19:41:30 +00008174 StackSlot, MachinePointerInfo(),
Chris Lattner8026a9d2010-09-21 17:50:43 +00008175 false, false, 0);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00008176 // For i64 source, we need to add the appropriate power of 2 if the input
8177 // was negative. This is the same as the optimization in
8178 // DAGTypeLegalizer::ExpandIntOp_UNIT_TO_FP, and for it to be safe here,
8179 // we must be careful to do the computation in x87 extended precision, not
8180 // in SSE. (The generic code can't know it's OK to do this, or how to.)
Chris Lattner492a43e2010-09-22 01:28:21 +00008181 int SSFI = cast<FrameIndexSDNode>(StackSlot)->getIndex();
8182 MachineMemOperand *MMO =
8183 DAG.getMachineFunction()
8184 .getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
8185 MachineMemOperand::MOLoad, 8, 8);
Michael J. Spencerec38de22010-10-10 22:04:20 +00008186
Dale Johannesen8d908eb2010-05-15 18:51:12 +00008187 SDVTList Tys = DAG.getVTList(MVT::f80, MVT::Other);
8188 SDValue Ops[] = { Store, StackSlot, DAG.getValueType(MVT::i64) };
Chris Lattner492a43e2010-09-22 01:28:21 +00008189 SDValue Fild = DAG.getMemIntrinsicNode(X86ISD::FILD, dl, Tys, Ops, 3,
8190 MVT::i64, MMO);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00008191
8192 APInt FF(32, 0x5F800000ULL);
8193
8194 // Check whether the sign bit is set.
8195 SDValue SignSet = DAG.getSetCC(dl, getSetCCResultType(MVT::i64),
8196 Op.getOperand(0), DAG.getConstant(0, MVT::i64),
8197 ISD::SETLT);
8198
8199 // Build a 64 bit pair (0, FF) in the constant pool, with FF in the lo bits.
8200 SDValue FudgePtr = DAG.getConstantPool(
8201 ConstantInt::get(*DAG.getContext(), FF.zext(64)),
8202 getPointerTy());
8203
8204 // Get a pointer to FF if the sign bit was set, or to 0 otherwise.
8205 SDValue Zero = DAG.getIntPtrConstant(0);
8206 SDValue Four = DAG.getIntPtrConstant(4);
8207 SDValue Offset = DAG.getNode(ISD::SELECT, dl, Zero.getValueType(), SignSet,
8208 Zero, Four);
8209 FudgePtr = DAG.getNode(ISD::ADD, dl, getPointerTy(), FudgePtr, Offset);
8210
8211 // Load the value out, extending it from f32 to f80.
8212 // FIXME: Avoid the extend by constructing the right constant pool?
Stuart Hastingsa9011292011-02-16 16:23:55 +00008213 SDValue Fudge = DAG.getExtLoad(ISD::EXTLOAD, dl, MVT::f80, DAG.getEntryNode(),
Chris Lattnere8639032010-09-21 06:22:23 +00008214 FudgePtr, MachinePointerInfo::getConstantPool(),
8215 MVT::f32, false, false, 4);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00008216 // Extend everything to 80 bits to force it to be done on x87.
8217 SDValue Add = DAG.getNode(ISD::FADD, dl, MVT::f80, Fild, Fudge);
8218 return DAG.getNode(ISD::FP_ROUND, dl, DstVT, Add, DAG.getIntPtrConstant(0));
Bill Wendling8b8a6362009-01-17 03:56:04 +00008219}
8220
Dan Gohman475871a2008-07-27 21:46:04 +00008221std::pair<SDValue,SDValue> X86TargetLowering::
NAKAMURA Takumi9a68fdc2012-02-25 03:37:25 +00008222FP_TO_INTHelper(SDValue Op, SelectionDAG &DAG, bool IsSigned, bool IsReplace) const {
Chris Lattner07290932010-09-22 01:05:16 +00008223 DebugLoc DL = Op.getDebugLoc();
Eli Friedman948e95a2009-05-23 09:59:16 +00008224
Owen Andersone50ed302009-08-10 22:56:29 +00008225 EVT DstTy = Op.getValueType();
Eli Friedman948e95a2009-05-23 09:59:16 +00008226
Michael J. Spencer1a2d0612012-02-24 19:01:22 +00008227 if (!IsSigned && !isIntegerTypeFTOL(DstTy)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00008228 assert(DstTy == MVT::i32 && "Unexpected FP_TO_UINT");
8229 DstTy = MVT::i64;
Eli Friedman948e95a2009-05-23 09:59:16 +00008230 }
8231
Owen Anderson825b72b2009-08-11 20:47:22 +00008232 assert(DstTy.getSimpleVT() <= MVT::i64 &&
8233 DstTy.getSimpleVT() >= MVT::i16 &&
Michael J. Spencer1a2d0612012-02-24 19:01:22 +00008234 "Unknown FP_TO_INT to lower!");
Evan Cheng0db9fe62006-04-25 20:13:52 +00008235
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00008236 // These are really Legal.
Owen Anderson825b72b2009-08-11 20:47:22 +00008237 if (DstTy == MVT::i32 &&
Chris Lattner78631162008-01-16 06:24:21 +00008238 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Dan Gohman475871a2008-07-27 21:46:04 +00008239 return std::make_pair(SDValue(), SDValue());
Dale Johannesen73328d12007-09-19 23:55:34 +00008240 if (Subtarget->is64Bit() &&
Owen Anderson825b72b2009-08-11 20:47:22 +00008241 DstTy == MVT::i64 &&
Eli Friedman36df4992009-05-27 00:47:34 +00008242 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Dan Gohman475871a2008-07-27 21:46:04 +00008243 return std::make_pair(SDValue(), SDValue());
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00008244
Michael J. Spencer1a2d0612012-02-24 19:01:22 +00008245 // We lower FP->int64 either into FISTP64 followed by a load from a temporary
8246 // stack slot, or into the FTOL runtime function.
Evan Cheng87c89352007-10-15 20:11:21 +00008247 MachineFunction &MF = DAG.getMachineFunction();
Eli Friedman948e95a2009-05-23 09:59:16 +00008248 unsigned MemSize = DstTy.getSizeInBits()/8;
David Greene3f2bf852009-11-12 20:49:22 +00008249 int SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize, false);
Dan Gohman475871a2008-07-27 21:46:04 +00008250 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Eric Christopherfd179292009-08-27 18:07:15 +00008251
Evan Cheng0db9fe62006-04-25 20:13:52 +00008252 unsigned Opc;
Michael J. Spencer1a2d0612012-02-24 19:01:22 +00008253 if (!IsSigned && isIntegerTypeFTOL(DstTy))
8254 Opc = X86ISD::WIN_FTOL;
8255 else
8256 switch (DstTy.getSimpleVT().SimpleTy) {
8257 default: llvm_unreachable("Invalid FP_TO_SINT to lower!");
8258 case MVT::i16: Opc = X86ISD::FP_TO_INT16_IN_MEM; break;
8259 case MVT::i32: Opc = X86ISD::FP_TO_INT32_IN_MEM; break;
8260 case MVT::i64: Opc = X86ISD::FP_TO_INT64_IN_MEM; break;
8261 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00008262
Dan Gohman475871a2008-07-27 21:46:04 +00008263 SDValue Chain = DAG.getEntryNode();
8264 SDValue Value = Op.getOperand(0);
Chris Lattner492a43e2010-09-22 01:28:21 +00008265 EVT TheVT = Op.getOperand(0).getValueType();
Michael J. Spencer1a2d0612012-02-24 19:01:22 +00008266 // FIXME This causes a redundant load/store if the SSE-class value is already
8267 // in memory, such as if it is on the callstack.
Chris Lattner492a43e2010-09-22 01:28:21 +00008268 if (isScalarFPTypeInSSEReg(TheVT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00008269 assert(DstTy == MVT::i64 && "Invalid FP_TO_SINT to lower!");
Chris Lattner07290932010-09-22 01:05:16 +00008270 Chain = DAG.getStore(Chain, DL, Value, StackSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00008271 MachinePointerInfo::getFixedStack(SSFI),
David Greene67c9d422010-02-15 16:53:33 +00008272 false, false, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +00008273 SDVTList Tys = DAG.getVTList(Op.getOperand(0).getValueType(), MVT::Other);
Dan Gohman475871a2008-07-27 21:46:04 +00008274 SDValue Ops[] = {
Chris Lattner492a43e2010-09-22 01:28:21 +00008275 Chain, StackSlot, DAG.getValueType(TheVT)
Chris Lattner5a88b832007-02-25 07:10:00 +00008276 };
Michael J. Spencerec38de22010-10-10 22:04:20 +00008277
Chris Lattner492a43e2010-09-22 01:28:21 +00008278 MachineMemOperand *MMO =
8279 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
8280 MachineMemOperand::MOLoad, MemSize, MemSize);
8281 Value = DAG.getMemIntrinsicNode(X86ISD::FLD, DL, Tys, Ops, 3,
8282 DstTy, MMO);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008283 Chain = Value.getValue(1);
David Greene3f2bf852009-11-12 20:49:22 +00008284 SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize, false);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008285 StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
8286 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00008287
Chris Lattner07290932010-09-22 01:05:16 +00008288 MachineMemOperand *MMO =
8289 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
8290 MachineMemOperand::MOStore, MemSize, MemSize);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00008291
Michael J. Spencer1a2d0612012-02-24 19:01:22 +00008292 if (Opc != X86ISD::WIN_FTOL) {
8293 // Build the FP_TO_INT*_IN_MEM
8294 SDValue Ops[] = { Chain, Value, StackSlot };
8295 SDValue FIST = DAG.getMemIntrinsicNode(Opc, DL, DAG.getVTList(MVT::Other),
8296 Ops, 3, DstTy, MMO);
8297 return std::make_pair(FIST, StackSlot);
8298 } else {
8299 SDValue ftol = DAG.getNode(X86ISD::WIN_FTOL, DL,
8300 DAG.getVTList(MVT::Other, MVT::Glue),
8301 Chain, Value);
8302 SDValue eax = DAG.getCopyFromReg(ftol, DL, X86::EAX,
8303 MVT::i32, ftol.getValue(1));
8304 SDValue edx = DAG.getCopyFromReg(eax.getValue(1), DL, X86::EDX,
8305 MVT::i32, eax.getValue(2));
NAKAMURA Takumi9a68fdc2012-02-25 03:37:25 +00008306 SDValue Ops[] = { eax, edx };
8307 SDValue pair = IsReplace
8308 ? DAG.getNode(ISD::BUILD_PAIR, DL, MVT::i64, Ops, 2)
8309 : DAG.getMergeValues(Ops, 2, DL);
Michael J. Spencer1a2d0612012-02-24 19:01:22 +00008310 return std::make_pair(pair, SDValue());
8311 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00008312}
8313
Michael Liaoa7554632012-10-23 17:36:08 +00008314SDValue X86TargetLowering::lowerZERO_EXTEND(SDValue Op, SelectionDAG &DAG) const {
8315 DebugLoc DL = Op.getDebugLoc();
8316 EVT VT = Op.getValueType();
8317 SDValue In = Op.getOperand(0);
8318 EVT SVT = In.getValueType();
8319
8320 if (!VT.is256BitVector() || !SVT.is128BitVector() ||
8321 VT.getVectorNumElements() != SVT.getVectorNumElements())
8322 return SDValue();
8323
8324 assert(Subtarget->hasAVX() && "256-bit vector is observed without AVX!");
8325
8326 // AVX2 has better support of integer extending.
8327 if (Subtarget->hasAVX2())
8328 return DAG.getNode(X86ISD::VZEXT, DL, VT, In);
8329
8330 SDValue Lo = DAG.getNode(X86ISD::VZEXT, DL, MVT::v4i32, In);
8331 static const int Mask[] = {4, 5, 6, 7, -1, -1, -1, -1};
8332 SDValue Hi = DAG.getNode(X86ISD::VZEXT, DL, MVT::v4i32,
8333 DAG.getVectorShuffle(MVT::v8i16, DL, In, DAG.getUNDEF(MVT::v8i16), &Mask[0]));
8334
8335 return DAG.getNode(ISD::CONCAT_VECTORS, DL, MVT::v8i32, Lo, Hi);
8336}
8337
Michael Liaobedcbd42012-10-16 18:14:11 +00008338SDValue X86TargetLowering::lowerTRUNCATE(SDValue Op, SelectionDAG &DAG) const {
8339 DebugLoc DL = Op.getDebugLoc();
8340 EVT VT = Op.getValueType();
8341 EVT SVT = Op.getOperand(0).getValueType();
8342
8343 if (!VT.is128BitVector() || !SVT.is256BitVector() ||
8344 VT.getVectorNumElements() != SVT.getVectorNumElements())
8345 return SDValue();
8346
8347 assert(Subtarget->hasAVX() && "256-bit vector is observed without AVX!");
8348
8349 unsigned NumElems = VT.getVectorNumElements();
8350 EVT NVT = EVT::getVectorVT(*DAG.getContext(), VT.getVectorElementType(),
8351 NumElems * 2);
8352
8353 SDValue In = Op.getOperand(0);
8354 SmallVector<int, 16> MaskVec(NumElems * 2, -1);
8355 // Prepare truncation shuffle mask
8356 for (unsigned i = 0; i != NumElems; ++i)
8357 MaskVec[i] = i * 2;
8358 SDValue V = DAG.getVectorShuffle(NVT, DL,
8359 DAG.getNode(ISD::BITCAST, DL, NVT, In),
8360 DAG.getUNDEF(NVT), &MaskVec[0]);
8361 return DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, VT, V,
8362 DAG.getIntPtrConstant(0));
8363}
8364
Dan Gohmand858e902010-04-17 15:26:15 +00008365SDValue X86TargetLowering::LowerFP_TO_SINT(SDValue Op,
8366 SelectionDAG &DAG) const {
Michael Liaobedcbd42012-10-16 18:14:11 +00008367 if (Op.getValueType().isVector()) {
8368 if (Op.getValueType() == MVT::v8i16)
8369 return DAG.getNode(ISD::TRUNCATE, Op.getDebugLoc(), Op.getValueType(),
8370 DAG.getNode(ISD::FP_TO_SINT, Op.getDebugLoc(),
8371 MVT::v8i32, Op.getOperand(0)));
Eli Friedman23ef1052009-06-06 03:57:58 +00008372 return SDValue();
Michael Liaobedcbd42012-10-16 18:14:11 +00008373 }
Eli Friedman23ef1052009-06-06 03:57:58 +00008374
NAKAMURA Takumi9a68fdc2012-02-25 03:37:25 +00008375 std::pair<SDValue,SDValue> Vals = FP_TO_INTHelper(Op, DAG,
8376 /*IsSigned=*/ true, /*IsReplace=*/ false);
Dan Gohman475871a2008-07-27 21:46:04 +00008377 SDValue FIST = Vals.first, StackSlot = Vals.second;
Eli Friedman36df4992009-05-27 00:47:34 +00008378 // If FP_TO_INTHelper failed, the node is actually supposed to be Legal.
8379 if (FIST.getNode() == 0) return Op;
Scott Michelfdc40a02009-02-17 22:15:04 +00008380
Michael J. Spencer1a2d0612012-02-24 19:01:22 +00008381 if (StackSlot.getNode())
8382 // Load the result.
8383 return DAG.getLoad(Op.getValueType(), Op.getDebugLoc(),
8384 FIST, StackSlot, MachinePointerInfo(),
8385 false, false, false, 0);
Craig Topper69947b92012-04-23 06:57:04 +00008386
8387 // The node is the result.
8388 return FIST;
Chris Lattner27a6c732007-11-24 07:07:01 +00008389}
8390
Dan Gohmand858e902010-04-17 15:26:15 +00008391SDValue X86TargetLowering::LowerFP_TO_UINT(SDValue Op,
8392 SelectionDAG &DAG) const {
NAKAMURA Takumi9a68fdc2012-02-25 03:37:25 +00008393 std::pair<SDValue,SDValue> Vals = FP_TO_INTHelper(Op, DAG,
8394 /*IsSigned=*/ false, /*IsReplace=*/ false);
Eli Friedman948e95a2009-05-23 09:59:16 +00008395 SDValue FIST = Vals.first, StackSlot = Vals.second;
8396 assert(FIST.getNode() && "Unexpected failure");
8397
NAKAMURA Takumi9a68fdc2012-02-25 03:37:25 +00008398 if (StackSlot.getNode())
8399 // Load the result.
8400 return DAG.getLoad(Op.getValueType(), Op.getDebugLoc(),
8401 FIST, StackSlot, MachinePointerInfo(),
8402 false, false, false, 0);
Craig Topper69947b92012-04-23 06:57:04 +00008403
8404 // The node is the result.
8405 return FIST;
Eli Friedman948e95a2009-05-23 09:59:16 +00008406}
8407
Michael Liao9d796db2012-10-10 16:32:15 +00008408SDValue X86TargetLowering::lowerFP_EXTEND(SDValue Op,
8409 SelectionDAG &DAG) const {
8410 DebugLoc DL = Op.getDebugLoc();
8411 EVT VT = Op.getValueType();
8412 SDValue In = Op.getOperand(0);
8413 EVT SVT = In.getValueType();
8414
8415 assert(SVT == MVT::v2f32 && "Only customize MVT::v2f32 type legalization!");
8416
8417 return DAG.getNode(X86ISD::VFPEXT, DL, VT,
8418 DAG.getNode(ISD::CONCAT_VECTORS, DL, MVT::v4f32,
8419 In, DAG.getUNDEF(SVT)));
8420}
8421
Craig Topper43620672012-09-08 07:31:51 +00008422SDValue X86TargetLowering::LowerFABS(SDValue Op, SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00008423 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008424 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00008425 EVT VT = Op.getValueType();
8426 EVT EltVT = VT;
Craig Topper43620672012-09-08 07:31:51 +00008427 unsigned NumElts = VT == MVT::f64 ? 2 : 4;
8428 if (VT.isVector()) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00008429 EltVT = VT.getVectorElementType();
Craig Topper43620672012-09-08 07:31:51 +00008430 NumElts = VT.getVectorNumElements();
Evan Cheng0db9fe62006-04-25 20:13:52 +00008431 }
Craig Topper43620672012-09-08 07:31:51 +00008432 Constant *C;
8433 if (EltVT == MVT::f64)
8434 C = ConstantFP::get(*Context, APFloat(APInt(64, ~(1ULL << 63))));
8435 else
8436 C = ConstantFP::get(*Context, APFloat(APInt(32, ~(1U << 31))));
8437 C = ConstantVector::getSplat(NumElts, C);
8438 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy());
8439 unsigned Alignment = cast<ConstantPoolSDNode>(CPIdx)->getAlignment();
Dale Johannesenace16102009-02-03 19:33:06 +00008440 SDValue Mask = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00008441 MachinePointerInfo::getConstantPool(),
Craig Topper43620672012-09-08 07:31:51 +00008442 false, false, false, Alignment);
8443 if (VT.isVector()) {
8444 MVT ANDVT = VT.is128BitVector() ? MVT::v2i64 : MVT::v4i64;
8445 return DAG.getNode(ISD::BITCAST, dl, VT,
8446 DAG.getNode(ISD::AND, dl, ANDVT,
8447 DAG.getNode(ISD::BITCAST, dl, ANDVT,
8448 Op.getOperand(0)),
8449 DAG.getNode(ISD::BITCAST, dl, ANDVT, Mask)));
8450 }
Dale Johannesenace16102009-02-03 19:33:06 +00008451 return DAG.getNode(X86ISD::FAND, dl, VT, Op.getOperand(0), Mask);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008452}
8453
Dan Gohmand858e902010-04-17 15:26:15 +00008454SDValue X86TargetLowering::LowerFNEG(SDValue Op, SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00008455 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008456 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00008457 EVT VT = Op.getValueType();
8458 EVT EltVT = VT;
Chad Rosiera860b182011-12-15 01:02:25 +00008459 unsigned NumElts = VT == MVT::f64 ? 2 : 4;
8460 if (VT.isVector()) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00008461 EltVT = VT.getVectorElementType();
Chad Rosiera860b182011-12-15 01:02:25 +00008462 NumElts = VT.getVectorNumElements();
8463 }
Chris Lattner4ca829e2012-01-25 06:02:56 +00008464 Constant *C;
8465 if (EltVT == MVT::f64)
8466 C = ConstantFP::get(*Context, APFloat(APInt(64, 1ULL << 63)));
8467 else
8468 C = ConstantFP::get(*Context, APFloat(APInt(32, 1U << 31)));
8469 C = ConstantVector::getSplat(NumElts, C);
Craig Toppercacd9d62012-09-08 07:46:05 +00008470 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy());
8471 unsigned Alignment = cast<ConstantPoolSDNode>(CPIdx)->getAlignment();
Dale Johannesenace16102009-02-03 19:33:06 +00008472 SDValue Mask = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00008473 MachinePointerInfo::getConstantPool(),
Craig Toppercacd9d62012-09-08 07:46:05 +00008474 false, false, false, Alignment);
Duncan Sands83ec4b62008-06-06 12:08:01 +00008475 if (VT.isVector()) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00008476 MVT XORVT = VT.is128BitVector() ? MVT::v2i64 : MVT::v4i64;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008477 return DAG.getNode(ISD::BITCAST, dl, VT,
Chad Rosiera860b182011-12-15 01:02:25 +00008478 DAG.getNode(ISD::XOR, dl, XORVT,
Craig Topper69947b92012-04-23 06:57:04 +00008479 DAG.getNode(ISD::BITCAST, dl, XORVT,
8480 Op.getOperand(0)),
8481 DAG.getNode(ISD::BITCAST, dl, XORVT, Mask)));
Evan Chengd4d01b72007-07-19 23:36:01 +00008482 }
Craig Topper69947b92012-04-23 06:57:04 +00008483
8484 return DAG.getNode(X86ISD::FXOR, dl, VT, Op.getOperand(0), Mask);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008485}
8486
Dan Gohmand858e902010-04-17 15:26:15 +00008487SDValue X86TargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00008488 LLVMContext *Context = DAG.getContext();
Dan Gohman475871a2008-07-27 21:46:04 +00008489 SDValue Op0 = Op.getOperand(0);
8490 SDValue Op1 = Op.getOperand(1);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008491 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00008492 EVT VT = Op.getValueType();
8493 EVT SrcVT = Op1.getValueType();
Evan Cheng73d6cf12007-01-05 21:37:56 +00008494
8495 // If second operand is smaller, extend it first.
Duncan Sands8e4eb092008-06-08 20:54:56 +00008496 if (SrcVT.bitsLT(VT)) {
Dale Johannesenace16102009-02-03 19:33:06 +00008497 Op1 = DAG.getNode(ISD::FP_EXTEND, dl, VT, Op1);
Evan Cheng73d6cf12007-01-05 21:37:56 +00008498 SrcVT = VT;
8499 }
Dale Johannesen61c7ef32007-10-21 01:07:44 +00008500 // And if it is bigger, shrink it first.
Duncan Sands8e4eb092008-06-08 20:54:56 +00008501 if (SrcVT.bitsGT(VT)) {
Dale Johannesenace16102009-02-03 19:33:06 +00008502 Op1 = DAG.getNode(ISD::FP_ROUND, dl, VT, Op1, DAG.getIntPtrConstant(1));
Dale Johannesen61c7ef32007-10-21 01:07:44 +00008503 SrcVT = VT;
Dale Johannesen61c7ef32007-10-21 01:07:44 +00008504 }
8505
8506 // At this point the operands and the result should have the same
8507 // type, and that won't be f80 since that is not custom lowered.
Evan Cheng73d6cf12007-01-05 21:37:56 +00008508
Evan Cheng68c47cb2007-01-05 07:55:56 +00008509 // First get the sign bit of second operand.
Chad Rosier01d426e2011-12-15 01:16:09 +00008510 SmallVector<Constant*,4> CV;
Owen Anderson825b72b2009-08-11 20:47:22 +00008511 if (SrcVT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00008512 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 1ULL << 63))));
8513 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 0))));
Evan Cheng68c47cb2007-01-05 07:55:56 +00008514 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00008515 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 1U << 31))));
8516 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
8517 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
8518 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
Evan Cheng68c47cb2007-01-05 07:55:56 +00008519 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00008520 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00008521 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00008522 SDValue Mask1 = DAG.getLoad(SrcVT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00008523 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00008524 false, false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00008525 SDValue SignBit = DAG.getNode(X86ISD::FAND, dl, SrcVT, Op1, Mask1);
Evan Cheng68c47cb2007-01-05 07:55:56 +00008526
8527 // Shift sign bit right or left if the two operands have different types.
Duncan Sands8e4eb092008-06-08 20:54:56 +00008528 if (SrcVT.bitsGT(VT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00008529 // Op0 is MVT::f32, Op1 is MVT::f64.
8530 SignBit = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f64, SignBit);
8531 SignBit = DAG.getNode(X86ISD::FSRL, dl, MVT::v2f64, SignBit,
8532 DAG.getConstant(32, MVT::i32));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008533 SignBit = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, SignBit);
Owen Anderson825b72b2009-08-11 20:47:22 +00008534 SignBit = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f32, SignBit,
Chris Lattner0bd48932008-01-17 07:00:52 +00008535 DAG.getIntPtrConstant(0));
Evan Cheng68c47cb2007-01-05 07:55:56 +00008536 }
8537
Evan Cheng73d6cf12007-01-05 21:37:56 +00008538 // Clear first operand sign bit.
8539 CV.clear();
Owen Anderson825b72b2009-08-11 20:47:22 +00008540 if (VT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00008541 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, ~(1ULL << 63)))));
8542 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 0))));
Evan Cheng73d6cf12007-01-05 21:37:56 +00008543 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00008544 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, ~(1U << 31)))));
8545 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
8546 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
8547 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
Evan Cheng73d6cf12007-01-05 21:37:56 +00008548 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00008549 C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00008550 CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00008551 SDValue Mask2 = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00008552 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00008553 false, false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00008554 SDValue Val = DAG.getNode(X86ISD::FAND, dl, VT, Op0, Mask2);
Evan Cheng73d6cf12007-01-05 21:37:56 +00008555
8556 // Or the value with the sign bit.
Dale Johannesenace16102009-02-03 19:33:06 +00008557 return DAG.getNode(X86ISD::FOR, dl, VT, Val, SignBit);
Evan Cheng68c47cb2007-01-05 07:55:56 +00008558}
8559
Craig Topper55b24052012-09-11 06:15:32 +00008560static SDValue LowerFGETSIGN(SDValue Op, SelectionDAG &DAG) {
Stuart Hastings4fd0dee2011-06-01 04:39:42 +00008561 SDValue N0 = Op.getOperand(0);
8562 DebugLoc dl = Op.getDebugLoc();
8563 EVT VT = Op.getValueType();
8564
8565 // Lower ISD::FGETSIGN to (AND (X86ISD::FGETSIGNx86 ...) 1).
8566 SDValue xFGETSIGN = DAG.getNode(X86ISD::FGETSIGNx86, dl, VT, N0,
8567 DAG.getConstant(1, VT));
8568 return DAG.getNode(ISD::AND, dl, VT, xFGETSIGN, DAG.getConstant(1, VT));
8569}
8570
Michael Liaof966e4e2012-09-13 20:24:54 +00008571// LowerVectorAllZeroTest - Check whether an OR'd tree is PTEST-able.
8572//
8573SDValue X86TargetLowering::LowerVectorAllZeroTest(SDValue Op, SelectionDAG &DAG) const {
8574 assert(Op.getOpcode() == ISD::OR && "Only check OR'd tree.");
8575
8576 if (!Subtarget->hasSSE41())
8577 return SDValue();
8578
8579 if (!Op->hasOneUse())
8580 return SDValue();
8581
8582 SDNode *N = Op.getNode();
8583 DebugLoc DL = N->getDebugLoc();
8584
8585 SmallVector<SDValue, 8> Opnds;
8586 DenseMap<SDValue, unsigned> VecInMap;
8587 EVT VT = MVT::Other;
8588
8589 // Recognize a special case where a vector is casted into wide integer to
8590 // test all 0s.
8591 Opnds.push_back(N->getOperand(0));
8592 Opnds.push_back(N->getOperand(1));
8593
8594 for (unsigned Slot = 0, e = Opnds.size(); Slot < e; ++Slot) {
8595 SmallVector<SDValue, 8>::const_iterator I = Opnds.begin() + Slot;
8596 // BFS traverse all OR'd operands.
8597 if (I->getOpcode() == ISD::OR) {
8598 Opnds.push_back(I->getOperand(0));
8599 Opnds.push_back(I->getOperand(1));
8600 // Re-evaluate the number of nodes to be traversed.
8601 e += 2; // 2 more nodes (LHS and RHS) are pushed.
8602 continue;
8603 }
8604
8605 // Quit if a non-EXTRACT_VECTOR_ELT
8606 if (I->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
8607 return SDValue();
8608
8609 // Quit if without a constant index.
8610 SDValue Idx = I->getOperand(1);
8611 if (!isa<ConstantSDNode>(Idx))
8612 return SDValue();
8613
8614 SDValue ExtractedFromVec = I->getOperand(0);
8615 DenseMap<SDValue, unsigned>::iterator M = VecInMap.find(ExtractedFromVec);
8616 if (M == VecInMap.end()) {
8617 VT = ExtractedFromVec.getValueType();
8618 // Quit if not 128/256-bit vector.
8619 if (!VT.is128BitVector() && !VT.is256BitVector())
8620 return SDValue();
8621 // Quit if not the same type.
8622 if (VecInMap.begin() != VecInMap.end() &&
8623 VT != VecInMap.begin()->first.getValueType())
8624 return SDValue();
8625 M = VecInMap.insert(std::make_pair(ExtractedFromVec, 0)).first;
8626 }
8627 M->second |= 1U << cast<ConstantSDNode>(Idx)->getZExtValue();
8628 }
8629
8630 assert((VT.is128BitVector() || VT.is256BitVector()) &&
Michael Liao9aba7ea2012-09-13 20:30:16 +00008631 "Not extracted from 128-/256-bit vector.");
Michael Liaof966e4e2012-09-13 20:24:54 +00008632
8633 unsigned FullMask = (1U << VT.getVectorNumElements()) - 1U;
8634 SmallVector<SDValue, 8> VecIns;
8635
8636 for (DenseMap<SDValue, unsigned>::const_iterator
8637 I = VecInMap.begin(), E = VecInMap.end(); I != E; ++I) {
8638 // Quit if not all elements are used.
8639 if (I->second != FullMask)
8640 return SDValue();
8641 VecIns.push_back(I->first);
8642 }
8643
8644 EVT TestVT = VT.is128BitVector() ? MVT::v2i64 : MVT::v4i64;
8645
8646 // Cast all vectors into TestVT for PTEST.
8647 for (unsigned i = 0, e = VecIns.size(); i < e; ++i)
8648 VecIns[i] = DAG.getNode(ISD::BITCAST, DL, TestVT, VecIns[i]);
8649
8650 // If more than one full vectors are evaluated, OR them first before PTEST.
8651 for (unsigned Slot = 0, e = VecIns.size(); e - Slot > 1; Slot += 2, e += 1) {
8652 // Each iteration will OR 2 nodes and append the result until there is only
8653 // 1 node left, i.e. the final OR'd value of all vectors.
8654 SDValue LHS = VecIns[Slot];
8655 SDValue RHS = VecIns[Slot + 1];
8656 VecIns.push_back(DAG.getNode(ISD::OR, DL, TestVT, LHS, RHS));
8657 }
8658
8659 return DAG.getNode(X86ISD::PTEST, DL, MVT::i32,
8660 VecIns.back(), VecIns.back());
8661}
8662
Dan Gohman076aee32009-03-04 19:44:21 +00008663/// Emit nodes that will be selected as "test Op0,Op0", or something
8664/// equivalent.
Dan Gohman31125812009-03-07 01:58:32 +00008665SDValue X86TargetLowering::EmitTest(SDValue Op, unsigned X86CC,
Evan Cheng552f09a2010-04-26 19:06:11 +00008666 SelectionDAG &DAG) const {
Dan Gohman076aee32009-03-04 19:44:21 +00008667 DebugLoc dl = Op.getDebugLoc();
8668
Dan Gohman31125812009-03-07 01:58:32 +00008669 // CF and OF aren't always set the way we want. Determine which
8670 // of these we need.
8671 bool NeedCF = false;
8672 bool NeedOF = false;
8673 switch (X86CC) {
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008674 default: break;
Dan Gohman31125812009-03-07 01:58:32 +00008675 case X86::COND_A: case X86::COND_AE:
8676 case X86::COND_B: case X86::COND_BE:
8677 NeedCF = true;
8678 break;
8679 case X86::COND_G: case X86::COND_GE:
8680 case X86::COND_L: case X86::COND_LE:
8681 case X86::COND_O: case X86::COND_NO:
8682 NeedOF = true;
8683 break;
Dan Gohman31125812009-03-07 01:58:32 +00008684 }
8685
Dan Gohman076aee32009-03-04 19:44:21 +00008686 // See if we can use the EFLAGS value from the operand instead of
Dan Gohman31125812009-03-07 01:58:32 +00008687 // doing a separate TEST. TEST always sets OF and CF to 0, so unless
8688 // we prove that the arithmetic won't overflow, we can't use OF or CF.
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008689 if (Op.getResNo() != 0 || NeedOF || NeedCF)
8690 // Emit a CMP with 0, which is the TEST pattern.
8691 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op,
8692 DAG.getConstant(0, Op.getValueType()));
8693
8694 unsigned Opcode = 0;
8695 unsigned NumOperands = 0;
Nadav Rotemb9d6b842012-08-18 17:53:03 +00008696
8697 // Truncate operations may prevent the merge of the SETCC instruction
8698 // and the arithmetic intruction before it. Attempt to truncate the operands
8699 // of the arithmetic instruction and use a reduced bit-width instruction.
8700 bool NeedTruncation = false;
8701 SDValue ArithOp = Op;
8702 if (Op->getOpcode() == ISD::TRUNCATE && Op->hasOneUse()) {
8703 SDValue Arith = Op->getOperand(0);
8704 // Both the trunc and the arithmetic op need to have one user each.
8705 if (Arith->hasOneUse())
8706 switch (Arith.getOpcode()) {
8707 default: break;
8708 case ISD::ADD:
8709 case ISD::SUB:
8710 case ISD::AND:
8711 case ISD::OR:
8712 case ISD::XOR: {
8713 NeedTruncation = true;
8714 ArithOp = Arith;
8715 }
8716 }
8717 }
8718
8719 // NOTICE: In the code below we use ArithOp to hold the arithmetic operation
8720 // which may be the result of a CAST. We use the variable 'Op', which is the
8721 // non-casted variable when we check for possible users.
8722 switch (ArithOp.getOpcode()) {
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008723 case ISD::ADD:
8724 // Due to an isel shortcoming, be conservative if this add is likely to be
8725 // selected as part of a load-modify-store instruction. When the root node
8726 // in a match is a store, isel doesn't know how to remap non-chain non-flag
8727 // uses of other nodes in the match, such as the ADD in this case. This
8728 // leads to the ADD being left around and reselected, with the result being
8729 // two adds in the output. Alas, even if none our users are stores, that
8730 // doesn't prove we're O.K. Ergo, if we have any parents that aren't
8731 // CopyToReg or SETCC, eschew INC/DEC. A better fix seems to require
8732 // climbing the DAG back to the root, and it doesn't seem to be worth the
8733 // effort.
8734 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
Pete Cooper2d496892011-11-15 21:57:53 +00008735 UE = Op.getNode()->use_end(); UI != UE; ++UI)
8736 if (UI->getOpcode() != ISD::CopyToReg &&
8737 UI->getOpcode() != ISD::SETCC &&
8738 UI->getOpcode() != ISD::STORE)
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008739 goto default_case;
8740
8741 if (ConstantSDNode *C =
Nadav Rotemb9d6b842012-08-18 17:53:03 +00008742 dyn_cast<ConstantSDNode>(ArithOp.getNode()->getOperand(1))) {
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008743 // An add of one will be selected as an INC.
8744 if (C->getAPIntValue() == 1) {
8745 Opcode = X86ISD::INC;
8746 NumOperands = 1;
8747 break;
Dan Gohmane220c4b2009-09-18 19:59:53 +00008748 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008749
8750 // An add of negative one (subtract of one) will be selected as a DEC.
8751 if (C->getAPIntValue().isAllOnesValue()) {
8752 Opcode = X86ISD::DEC;
8753 NumOperands = 1;
8754 break;
8755 }
Dan Gohman076aee32009-03-04 19:44:21 +00008756 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008757
8758 // Otherwise use a regular EFLAGS-setting add.
8759 Opcode = X86ISD::ADD;
8760 NumOperands = 2;
8761 break;
8762 case ISD::AND: {
8763 // If the primary and result isn't used, don't bother using X86ISD::AND,
8764 // because a TEST instruction will be better.
8765 bool NonFlagUse = false;
8766 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
8767 UE = Op.getNode()->use_end(); UI != UE; ++UI) {
8768 SDNode *User = *UI;
8769 unsigned UOpNo = UI.getOperandNo();
8770 if (User->getOpcode() == ISD::TRUNCATE && User->hasOneUse()) {
8771 // Look pass truncate.
8772 UOpNo = User->use_begin().getOperandNo();
8773 User = *User->use_begin();
8774 }
8775
8776 if (User->getOpcode() != ISD::BRCOND &&
8777 User->getOpcode() != ISD::SETCC &&
Nadav Rotemb9d6b842012-08-18 17:53:03 +00008778 !(User->getOpcode() == ISD::SELECT && UOpNo == 0)) {
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008779 NonFlagUse = true;
8780 break;
8781 }
Dan Gohman076aee32009-03-04 19:44:21 +00008782 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008783
8784 if (!NonFlagUse)
8785 break;
8786 }
8787 // FALL THROUGH
8788 case ISD::SUB:
8789 case ISD::OR:
8790 case ISD::XOR:
8791 // Due to the ISEL shortcoming noted above, be conservative if this op is
8792 // likely to be selected as part of a load-modify-store instruction.
8793 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
8794 UE = Op.getNode()->use_end(); UI != UE; ++UI)
8795 if (UI->getOpcode() == ISD::STORE)
8796 goto default_case;
8797
8798 // Otherwise use a regular EFLAGS-setting instruction.
Nadav Rotemb9d6b842012-08-18 17:53:03 +00008799 switch (ArithOp.getOpcode()) {
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008800 default: llvm_unreachable("unexpected operator!");
Nadav Rotemb9d6b842012-08-18 17:53:03 +00008801 case ISD::SUB: Opcode = X86ISD::SUB; break;
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008802 case ISD::XOR: Opcode = X86ISD::XOR; break;
8803 case ISD::AND: Opcode = X86ISD::AND; break;
Michael Liaof966e4e2012-09-13 20:24:54 +00008804 case ISD::OR: {
8805 if (!NeedTruncation && (X86CC == X86::COND_E || X86CC == X86::COND_NE)) {
8806 SDValue EFLAGS = LowerVectorAllZeroTest(Op, DAG);
8807 if (EFLAGS.getNode())
8808 return EFLAGS;
8809 }
8810 Opcode = X86ISD::OR;
8811 break;
8812 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008813 }
8814
8815 NumOperands = 2;
8816 break;
8817 case X86ISD::ADD:
8818 case X86ISD::SUB:
8819 case X86ISD::INC:
8820 case X86ISD::DEC:
8821 case X86ISD::OR:
8822 case X86ISD::XOR:
8823 case X86ISD::AND:
8824 return SDValue(Op.getNode(), 1);
8825 default:
8826 default_case:
8827 break;
Dan Gohman076aee32009-03-04 19:44:21 +00008828 }
8829
Nadav Rotemb9d6b842012-08-18 17:53:03 +00008830 // If we found that truncation is beneficial, perform the truncation and
8831 // update 'Op'.
8832 if (NeedTruncation) {
8833 EVT VT = Op.getValueType();
8834 SDValue WideVal = Op->getOperand(0);
8835 EVT WideVT = WideVal.getValueType();
8836 unsigned ConvertedOp = 0;
8837 // Use a target machine opcode to prevent further DAGCombine
8838 // optimizations that may separate the arithmetic operations
8839 // from the setcc node.
8840 switch (WideVal.getOpcode()) {
8841 default: break;
8842 case ISD::ADD: ConvertedOp = X86ISD::ADD; break;
8843 case ISD::SUB: ConvertedOp = X86ISD::SUB; break;
8844 case ISD::AND: ConvertedOp = X86ISD::AND; break;
8845 case ISD::OR: ConvertedOp = X86ISD::OR; break;
8846 case ISD::XOR: ConvertedOp = X86ISD::XOR; break;
8847 }
8848
8849 if (ConvertedOp) {
8850 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
8851 if (TLI.isOperationLegal(WideVal.getOpcode(), WideVT)) {
8852 SDValue V0 = DAG.getNode(ISD::TRUNCATE, dl, VT, WideVal.getOperand(0));
8853 SDValue V1 = DAG.getNode(ISD::TRUNCATE, dl, VT, WideVal.getOperand(1));
8854 Op = DAG.getNode(ConvertedOp, dl, VT, V0, V1);
8855 }
8856 }
8857 }
8858
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008859 if (Opcode == 0)
8860 // Emit a CMP with 0, which is the TEST pattern.
8861 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op,
8862 DAG.getConstant(0, Op.getValueType()));
8863
8864 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::i32);
8865 SmallVector<SDValue, 4> Ops;
8866 for (unsigned i = 0; i != NumOperands; ++i)
8867 Ops.push_back(Op.getOperand(i));
8868
8869 SDValue New = DAG.getNode(Opcode, dl, VTs, &Ops[0], NumOperands);
8870 DAG.ReplaceAllUsesWith(Op, New);
8871 return SDValue(New.getNode(), 1);
Dan Gohman076aee32009-03-04 19:44:21 +00008872}
8873
8874/// Emit nodes that will be selected as "cmp Op0,Op1", or something
8875/// equivalent.
Dan Gohman31125812009-03-07 01:58:32 +00008876SDValue X86TargetLowering::EmitCmp(SDValue Op0, SDValue Op1, unsigned X86CC,
Evan Cheng552f09a2010-04-26 19:06:11 +00008877 SelectionDAG &DAG) const {
Dan Gohman076aee32009-03-04 19:44:21 +00008878 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op1))
8879 if (C->getAPIntValue() == 0)
Evan Cheng552f09a2010-04-26 19:06:11 +00008880 return EmitTest(Op0, X86CC, DAG);
Dan Gohman076aee32009-03-04 19:44:21 +00008881
8882 DebugLoc dl = Op0.getDebugLoc();
Manman Ren39ad5682012-08-08 00:51:41 +00008883 if ((Op0.getValueType() == MVT::i8 || Op0.getValueType() == MVT::i16 ||
8884 Op0.getValueType() == MVT::i32 || Op0.getValueType() == MVT::i64)) {
8885 // Use SUB instead of CMP to enable CSE between SUB and CMP.
8886 SDVTList VTs = DAG.getVTList(Op0.getValueType(), MVT::i32);
8887 SDValue Sub = DAG.getNode(X86ISD::SUB, dl, VTs,
8888 Op0, Op1);
8889 return SDValue(Sub.getNode(), 1);
8890 }
Owen Anderson825b72b2009-08-11 20:47:22 +00008891 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op0, Op1);
Dan Gohman076aee32009-03-04 19:44:21 +00008892}
8893
Benjamin Kramer17c836c2012-04-27 12:07:43 +00008894/// Convert a comparison if required by the subtarget.
8895SDValue X86TargetLowering::ConvertCmpIfNecessary(SDValue Cmp,
8896 SelectionDAG &DAG) const {
8897 // If the subtarget does not support the FUCOMI instruction, floating-point
8898 // comparisons have to be converted.
8899 if (Subtarget->hasCMov() ||
8900 Cmp.getOpcode() != X86ISD::CMP ||
8901 !Cmp.getOperand(0).getValueType().isFloatingPoint() ||
8902 !Cmp.getOperand(1).getValueType().isFloatingPoint())
8903 return Cmp;
8904
8905 // The instruction selector will select an FUCOM instruction instead of
8906 // FUCOMI, which writes the comparison result to FPSW instead of EFLAGS. Hence
8907 // build an SDNode sequence that transfers the result from FPSW into EFLAGS:
8908 // (X86sahf (trunc (srl (X86fp_stsw (trunc (X86cmp ...)), 8))))
8909 DebugLoc dl = Cmp.getDebugLoc();
8910 SDValue TruncFPSW = DAG.getNode(ISD::TRUNCATE, dl, MVT::i16, Cmp);
8911 SDValue FNStSW = DAG.getNode(X86ISD::FNSTSW16r, dl, MVT::i16, TruncFPSW);
8912 SDValue Srl = DAG.getNode(ISD::SRL, dl, MVT::i16, FNStSW,
8913 DAG.getConstant(8, MVT::i8));
8914 SDValue TruncSrl = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Srl);
8915 return DAG.getNode(X86ISD::SAHF, dl, MVT::i32, TruncSrl);
8916}
8917
Evan Chengd40d03e2010-01-06 19:38:29 +00008918/// LowerToBT - Result of 'and' is compared against zero. Turn it into a BT node
8919/// if it's possible.
Evan Cheng5528e7b2010-04-21 01:47:12 +00008920SDValue X86TargetLowering::LowerToBT(SDValue And, ISD::CondCode CC,
8921 DebugLoc dl, SelectionDAG &DAG) const {
Evan Cheng2c755ba2010-02-27 07:36:59 +00008922 SDValue Op0 = And.getOperand(0);
8923 SDValue Op1 = And.getOperand(1);
8924 if (Op0.getOpcode() == ISD::TRUNCATE)
8925 Op0 = Op0.getOperand(0);
8926 if (Op1.getOpcode() == ISD::TRUNCATE)
8927 Op1 = Op1.getOperand(0);
8928
Evan Chengd40d03e2010-01-06 19:38:29 +00008929 SDValue LHS, RHS;
Dan Gohman6b13cbc2010-06-24 02:07:59 +00008930 if (Op1.getOpcode() == ISD::SHL)
8931 std::swap(Op0, Op1);
8932 if (Op0.getOpcode() == ISD::SHL) {
Evan Cheng2c755ba2010-02-27 07:36:59 +00008933 if (ConstantSDNode *And00C = dyn_cast<ConstantSDNode>(Op0.getOperand(0)))
8934 if (And00C->getZExtValue() == 1) {
Dan Gohman6b13cbc2010-06-24 02:07:59 +00008935 // If we looked past a truncate, check that it's only truncating away
8936 // known zeros.
8937 unsigned BitWidth = Op0.getValueSizeInBits();
8938 unsigned AndBitWidth = And.getValueSizeInBits();
8939 if (BitWidth > AndBitWidth) {
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00008940 APInt Zeros, Ones;
8941 DAG.ComputeMaskedBits(Op0, Zeros, Ones);
Dan Gohman6b13cbc2010-06-24 02:07:59 +00008942 if (Zeros.countLeadingOnes() < BitWidth - AndBitWidth)
8943 return SDValue();
8944 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00008945 LHS = Op1;
8946 RHS = Op0.getOperand(1);
Evan Chengd40d03e2010-01-06 19:38:29 +00008947 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00008948 } else if (Op1.getOpcode() == ISD::Constant) {
8949 ConstantSDNode *AndRHS = cast<ConstantSDNode>(Op1);
Benjamin Kramerf238f502011-11-23 13:54:17 +00008950 uint64_t AndRHSVal = AndRHS->getZExtValue();
Evan Cheng2c755ba2010-02-27 07:36:59 +00008951 SDValue AndLHS = Op0;
Benjamin Kramerf238f502011-11-23 13:54:17 +00008952
8953 if (AndRHSVal == 1 && AndLHS.getOpcode() == ISD::SRL) {
Evan Chengd40d03e2010-01-06 19:38:29 +00008954 LHS = AndLHS.getOperand(0);
8955 RHS = AndLHS.getOperand(1);
Dan Gohmane5af2d32009-01-29 01:59:02 +00008956 }
Benjamin Kramerf238f502011-11-23 13:54:17 +00008957
8958 // Use BT if the immediate can't be encoded in a TEST instruction.
8959 if (!isUInt<32>(AndRHSVal) && isPowerOf2_64(AndRHSVal)) {
8960 LHS = AndLHS;
8961 RHS = DAG.getConstant(Log2_64_Ceil(AndRHSVal), LHS.getValueType());
8962 }
Evan Chengd40d03e2010-01-06 19:38:29 +00008963 }
Evan Cheng0488db92007-09-25 01:57:46 +00008964
Evan Chengd40d03e2010-01-06 19:38:29 +00008965 if (LHS.getNode()) {
Evan Chenge5b51ac2010-04-17 06:13:15 +00008966 // If LHS is i8, promote it to i32 with any_extend. There is no i8 BT
Evan Chengd40d03e2010-01-06 19:38:29 +00008967 // instruction. Since the shift amount is in-range-or-undefined, we know
Evan Chenge5b51ac2010-04-17 06:13:15 +00008968 // that doing a bittest on the i32 value is ok. We extend to i32 because
Evan Chengd40d03e2010-01-06 19:38:29 +00008969 // the encoding for the i16 version is larger than the i32 version.
Evan Chenge5b51ac2010-04-17 06:13:15 +00008970 // Also promote i16 to i32 for performance / code size reason.
8971 if (LHS.getValueType() == MVT::i8 ||
Evan Cheng2bce5f4b2010-04-28 08:30:49 +00008972 LHS.getValueType() == MVT::i16)
Evan Chengd40d03e2010-01-06 19:38:29 +00008973 LHS = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, LHS);
Chris Lattnere55484e2008-12-25 05:34:37 +00008974
Evan Chengd40d03e2010-01-06 19:38:29 +00008975 // If the operand types disagree, extend the shift amount to match. Since
8976 // BT ignores high bits (like shifts) we can use anyextend.
8977 if (LHS.getValueType() != RHS.getValueType())
8978 RHS = DAG.getNode(ISD::ANY_EXTEND, dl, LHS.getValueType(), RHS);
Dan Gohmane5af2d32009-01-29 01:59:02 +00008979
Evan Chengd40d03e2010-01-06 19:38:29 +00008980 SDValue BT = DAG.getNode(X86ISD::BT, dl, MVT::i32, LHS, RHS);
8981 unsigned Cond = CC == ISD::SETEQ ? X86::COND_AE : X86::COND_B;
8982 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
8983 DAG.getConstant(Cond, MVT::i8), BT);
Chris Lattnere55484e2008-12-25 05:34:37 +00008984 }
8985
Evan Cheng54de3ea2010-01-05 06:52:31 +00008986 return SDValue();
8987}
8988
Dan Gohmand858e902010-04-17 15:26:15 +00008989SDValue X86TargetLowering::LowerSETCC(SDValue Op, SelectionDAG &DAG) const {
Duncan Sands28b77e92011-09-06 19:07:46 +00008990
8991 if (Op.getValueType().isVector()) return LowerVSETCC(Op, DAG);
8992
Evan Cheng54de3ea2010-01-05 06:52:31 +00008993 assert(Op.getValueType() == MVT::i8 && "SetCC type must be 8-bit integer");
8994 SDValue Op0 = Op.getOperand(0);
8995 SDValue Op1 = Op.getOperand(1);
8996 DebugLoc dl = Op.getDebugLoc();
8997 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
8998
8999 // Optimize to BT if possible.
Evan Chengd40d03e2010-01-06 19:38:29 +00009000 // Lower (X & (1 << N)) == 0 to BT(X, N).
9001 // Lower ((X >>u N) & 1) != 0 to BT(X, N).
9002 // Lower ((X >>s N) & 1) != 0 to BT(X, N).
Andrew Trickf6c39412011-03-23 23:11:02 +00009003 if (Op0.getOpcode() == ISD::AND && Op0.hasOneUse() &&
Evan Chengd40d03e2010-01-06 19:38:29 +00009004 Op1.getOpcode() == ISD::Constant &&
Dan Gohmane368b462010-06-18 14:22:04 +00009005 cast<ConstantSDNode>(Op1)->isNullValue() &&
Evan Chengd40d03e2010-01-06 19:38:29 +00009006 (CC == ISD::SETEQ || CC == ISD::SETNE)) {
9007 SDValue NewSetCC = LowerToBT(Op0, CC, dl, DAG);
9008 if (NewSetCC.getNode())
9009 return NewSetCC;
9010 }
Evan Cheng54de3ea2010-01-05 06:52:31 +00009011
Chris Lattner481eebc2010-12-19 21:23:48 +00009012 // Look for X == 0, X == 1, X != 0, or X != 1. We can simplify some forms of
9013 // these.
9014 if (Op1.getOpcode() == ISD::Constant &&
Andrew Trickf6c39412011-03-23 23:11:02 +00009015 (cast<ConstantSDNode>(Op1)->getZExtValue() == 1 ||
Evan Cheng2c755ba2010-02-27 07:36:59 +00009016 cast<ConstantSDNode>(Op1)->isNullValue()) &&
9017 (CC == ISD::SETEQ || CC == ISD::SETNE)) {
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009018
Chris Lattner481eebc2010-12-19 21:23:48 +00009019 // If the input is a setcc, then reuse the input setcc or use a new one with
9020 // the inverted condition.
9021 if (Op0.getOpcode() == X86ISD::SETCC) {
9022 X86::CondCode CCode = (X86::CondCode)Op0.getConstantOperandVal(0);
9023 bool Invert = (CC == ISD::SETNE) ^
9024 cast<ConstantSDNode>(Op1)->isNullValue();
9025 if (!Invert) return Op0;
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009026
Evan Cheng2c755ba2010-02-27 07:36:59 +00009027 CCode = X86::GetOppositeBranchCondition(CCode);
Chris Lattner481eebc2010-12-19 21:23:48 +00009028 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
9029 DAG.getConstant(CCode, MVT::i8), Op0.getOperand(1));
9030 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00009031 }
9032
Evan Chenge5b51ac2010-04-17 06:13:15 +00009033 bool isFP = Op1.getValueType().isFloatingPoint();
Chris Lattnere55484e2008-12-25 05:34:37 +00009034 unsigned X86CC = TranslateX86CC(CC, isFP, Op0, Op1, DAG);
Dan Gohman1a492952009-10-20 16:22:37 +00009035 if (X86CC == X86::COND_INVALID)
9036 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00009037
Chris Lattnerc19d1c32010-12-19 22:08:31 +00009038 SDValue EFLAGS = EmitCmp(Op0, Op1, X86CC, DAG);
Benjamin Kramer17c836c2012-04-27 12:07:43 +00009039 EFLAGS = ConvertCmpIfNecessary(EFLAGS, DAG);
Owen Anderson825b72b2009-08-11 20:47:22 +00009040 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
Chris Lattnerc19d1c32010-12-19 22:08:31 +00009041 DAG.getConstant(X86CC, MVT::i8), EFLAGS);
Evan Cheng0488db92007-09-25 01:57:46 +00009042}
9043
Craig Topper89af15e2011-09-18 08:03:58 +00009044// Lower256IntVSETCC - Break a VSETCC 256-bit integer VSETCC into two new 128
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00009045// ones, and then concatenate the result back.
Craig Topper89af15e2011-09-18 08:03:58 +00009046static SDValue Lower256IntVSETCC(SDValue Op, SelectionDAG &DAG) {
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00009047 EVT VT = Op.getValueType();
9048
Craig Topper7a9a28b2012-08-12 02:23:29 +00009049 assert(VT.is256BitVector() && Op.getOpcode() == ISD::SETCC &&
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00009050 "Unsupported value type for operation");
9051
Craig Topper66ddd152012-04-27 22:54:43 +00009052 unsigned NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00009053 DebugLoc dl = Op.getDebugLoc();
9054 SDValue CC = Op.getOperand(2);
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00009055
9056 // Extract the LHS vectors
9057 SDValue LHS = Op.getOperand(0);
Craig Topperb14940a2012-04-22 20:55:18 +00009058 SDValue LHS1 = Extract128BitVector(LHS, 0, DAG, dl);
9059 SDValue LHS2 = Extract128BitVector(LHS, NumElems/2, DAG, dl);
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00009060
9061 // Extract the RHS vectors
9062 SDValue RHS = Op.getOperand(1);
Craig Topperb14940a2012-04-22 20:55:18 +00009063 SDValue RHS1 = Extract128BitVector(RHS, 0, DAG, dl);
9064 SDValue RHS2 = Extract128BitVector(RHS, NumElems/2, DAG, dl);
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00009065
9066 // Issue the operation on the smaller types and concatenate the result back
9067 MVT EltVT = VT.getVectorElementType().getSimpleVT();
9068 EVT NewVT = MVT::getVectorVT(EltVT, NumElems/2);
9069 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT,
9070 DAG.getNode(Op.getOpcode(), dl, NewVT, LHS1, RHS1, CC),
9071 DAG.getNode(Op.getOpcode(), dl, NewVT, LHS2, RHS2, CC));
9072}
9073
9074
Dan Gohmand858e902010-04-17 15:26:15 +00009075SDValue X86TargetLowering::LowerVSETCC(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +00009076 SDValue Cond;
9077 SDValue Op0 = Op.getOperand(0);
9078 SDValue Op1 = Op.getOperand(1);
9079 SDValue CC = Op.getOperand(2);
Owen Andersone50ed302009-08-10 22:56:29 +00009080 EVT VT = Op.getValueType();
Nate Begeman30a0de92008-07-17 16:51:19 +00009081 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
9082 bool isFP = Op.getOperand(1).getValueType().isFloatingPoint();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009083 DebugLoc dl = Op.getDebugLoc();
Nate Begeman30a0de92008-07-17 16:51:19 +00009084
9085 if (isFP) {
Craig Topper523908d2012-08-13 02:34:03 +00009086#ifndef NDEBUG
Bruno Cardoso Lopes0f0e0a02011-08-09 00:46:57 +00009087 EVT EltVT = Op0.getValueType().getVectorElementType();
Craig Topper523908d2012-08-13 02:34:03 +00009088 assert(EltVT == MVT::f32 || EltVT == MVT::f64);
9089#endif
Bruno Cardoso Lopes0f0e0a02011-08-09 00:46:57 +00009090
Craig Topper523908d2012-08-13 02:34:03 +00009091 unsigned SSECC;
Nate Begeman30a0de92008-07-17 16:51:19 +00009092 bool Swap = false;
9093
Bruno Cardoso Lopes8e03a822011-09-12 19:30:40 +00009094 // SSE Condition code mapping:
9095 // 0 - EQ
9096 // 1 - LT
9097 // 2 - LE
9098 // 3 - UNORD
9099 // 4 - NEQ
9100 // 5 - NLT
9101 // 6 - NLE
9102 // 7 - ORD
Nate Begeman30a0de92008-07-17 16:51:19 +00009103 switch (SetCCOpcode) {
Craig Topper2f1b2ec2012-08-13 03:42:38 +00009104 default: llvm_unreachable("Unexpected SETCC condition");
Nate Begemanfb8ead02008-07-25 19:05:58 +00009105 case ISD::SETOEQ:
Nate Begeman30a0de92008-07-17 16:51:19 +00009106 case ISD::SETEQ: SSECC = 0; break;
Bruno Cardoso Lopes8e03a822011-09-12 19:30:40 +00009107 case ISD::SETOGT:
9108 case ISD::SETGT: Swap = true; // Fallthrough
Bruno Cardoso Lopes457d53d2011-09-12 21:24:07 +00009109 case ISD::SETLT:
9110 case ISD::SETOLT: SSECC = 1; break;
9111 case ISD::SETOGE:
9112 case ISD::SETGE: Swap = true; // Fallthrough
Nate Begeman30a0de92008-07-17 16:51:19 +00009113 case ISD::SETLE:
9114 case ISD::SETOLE: SSECC = 2; break;
9115 case ISD::SETUO: SSECC = 3; break;
Nate Begemanfb8ead02008-07-25 19:05:58 +00009116 case ISD::SETUNE:
Nate Begeman30a0de92008-07-17 16:51:19 +00009117 case ISD::SETNE: SSECC = 4; break;
Craig Topper523908d2012-08-13 02:34:03 +00009118 case ISD::SETULE: Swap = true; // Fallthrough
Nate Begeman30a0de92008-07-17 16:51:19 +00009119 case ISD::SETUGE: SSECC = 5; break;
Craig Topper523908d2012-08-13 02:34:03 +00009120 case ISD::SETULT: Swap = true; // Fallthrough
Nate Begeman30a0de92008-07-17 16:51:19 +00009121 case ISD::SETUGT: SSECC = 6; break;
9122 case ISD::SETO: SSECC = 7; break;
Craig Topper523908d2012-08-13 02:34:03 +00009123 case ISD::SETUEQ:
9124 case ISD::SETONE: SSECC = 8; break;
Nate Begeman30a0de92008-07-17 16:51:19 +00009125 }
9126 if (Swap)
9127 std::swap(Op0, Op1);
9128
Nate Begemanfb8ead02008-07-25 19:05:58 +00009129 // In the two special cases we can't handle, emit two comparisons.
Nate Begeman30a0de92008-07-17 16:51:19 +00009130 if (SSECC == 8) {
Craig Topper523908d2012-08-13 02:34:03 +00009131 unsigned CC0, CC1;
9132 unsigned CombineOpc;
Nate Begemanfb8ead02008-07-25 19:05:58 +00009133 if (SetCCOpcode == ISD::SETUEQ) {
Craig Topper523908d2012-08-13 02:34:03 +00009134 CC0 = 3; CC1 = 0; CombineOpc = ISD::OR;
9135 } else {
9136 assert(SetCCOpcode == ISD::SETONE);
9137 CC0 = 7; CC1 = 4; CombineOpc = ISD::AND;
Craig Topper69947b92012-04-23 06:57:04 +00009138 }
Craig Topper523908d2012-08-13 02:34:03 +00009139
9140 SDValue Cmp0 = DAG.getNode(X86ISD::CMPP, dl, VT, Op0, Op1,
9141 DAG.getConstant(CC0, MVT::i8));
9142 SDValue Cmp1 = DAG.getNode(X86ISD::CMPP, dl, VT, Op0, Op1,
9143 DAG.getConstant(CC1, MVT::i8));
9144 return DAG.getNode(CombineOpc, dl, VT, Cmp0, Cmp1);
Nate Begeman30a0de92008-07-17 16:51:19 +00009145 }
9146 // Handle all other FP comparisons here.
Craig Topper1906d322012-01-22 23:36:02 +00009147 return DAG.getNode(X86ISD::CMPP, dl, VT, Op0, Op1,
9148 DAG.getConstant(SSECC, MVT::i8));
Nate Begeman30a0de92008-07-17 16:51:19 +00009149 }
Scott Michelfdc40a02009-02-17 22:15:04 +00009150
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00009151 // Break 256-bit integer vector compare into smaller ones.
Craig Topper7a9a28b2012-08-12 02:23:29 +00009152 if (VT.is256BitVector() && !Subtarget->hasAVX2())
Craig Topper89af15e2011-09-18 08:03:58 +00009153 return Lower256IntVSETCC(Op, DAG);
Bruno Cardoso Lopes0f0e0a02011-08-09 00:46:57 +00009154
Nate Begeman30a0de92008-07-17 16:51:19 +00009155 // We are handling one of the integer comparisons here. Since SSE only has
9156 // GT and EQ comparisons for integer, swapping operands and multiple
9157 // operations may be required for some comparisons.
Craig Topper2f1b2ec2012-08-13 03:42:38 +00009158 unsigned Opc;
Nate Begeman30a0de92008-07-17 16:51:19 +00009159 bool Swap = false, Invert = false, FlipSigns = false;
Scott Michelfdc40a02009-02-17 22:15:04 +00009160
Nate Begeman30a0de92008-07-17 16:51:19 +00009161 switch (SetCCOpcode) {
Craig Topper2f1b2ec2012-08-13 03:42:38 +00009162 default: llvm_unreachable("Unexpected SETCC condition");
Nate Begeman30a0de92008-07-17 16:51:19 +00009163 case ISD::SETNE: Invert = true;
Craig Topper67609fd2012-01-22 22:42:16 +00009164 case ISD::SETEQ: Opc = X86ISD::PCMPEQ; break;
Nate Begeman30a0de92008-07-17 16:51:19 +00009165 case ISD::SETLT: Swap = true;
Craig Topper67609fd2012-01-22 22:42:16 +00009166 case ISD::SETGT: Opc = X86ISD::PCMPGT; break;
Nate Begeman30a0de92008-07-17 16:51:19 +00009167 case ISD::SETGE: Swap = true;
Craig Topper67609fd2012-01-22 22:42:16 +00009168 case ISD::SETLE: Opc = X86ISD::PCMPGT; Invert = true; break;
Nate Begeman30a0de92008-07-17 16:51:19 +00009169 case ISD::SETULT: Swap = true;
Craig Topper67609fd2012-01-22 22:42:16 +00009170 case ISD::SETUGT: Opc = X86ISD::PCMPGT; FlipSigns = true; break;
Nate Begeman30a0de92008-07-17 16:51:19 +00009171 case ISD::SETUGE: Swap = true;
Craig Topper67609fd2012-01-22 22:42:16 +00009172 case ISD::SETULE: Opc = X86ISD::PCMPGT; FlipSigns = true; Invert = true; break;
Nate Begeman30a0de92008-07-17 16:51:19 +00009173 }
9174 if (Swap)
9175 std::swap(Op0, Op1);
Scott Michelfdc40a02009-02-17 22:15:04 +00009176
Eli Friedman7d3e2b72011-09-28 21:00:25 +00009177 // Check that the operation in question is available (most are plain SSE2,
9178 // but PCMPGTQ and PCMPEQQ have different requirements).
Craig Topper2f1b2ec2012-08-13 03:42:38 +00009179 if (VT == MVT::v2i64) {
9180 if (Opc == X86ISD::PCMPGT && !Subtarget->hasSSE42())
9181 return SDValue();
9182 if (Opc == X86ISD::PCMPEQ && !Subtarget->hasSSE41())
9183 return SDValue();
9184 }
Eli Friedman7d3e2b72011-09-28 21:00:25 +00009185
Nate Begeman30a0de92008-07-17 16:51:19 +00009186 // Since SSE has no unsigned integer comparisons, we need to flip the sign
9187 // bits of the inputs before performing those operations.
9188 if (FlipSigns) {
Owen Andersone50ed302009-08-10 22:56:29 +00009189 EVT EltVT = VT.getVectorElementType();
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00009190 SDValue SignBit = DAG.getConstant(APInt::getSignBit(EltVT.getSizeInBits()),
9191 EltVT);
Dan Gohman475871a2008-07-27 21:46:04 +00009192 std::vector<SDValue> SignBits(VT.getVectorNumElements(), SignBit);
Evan Chenga87008d2009-02-25 22:49:59 +00009193 SDValue SignVec = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &SignBits[0],
9194 SignBits.size());
Dale Johannesenace16102009-02-03 19:33:06 +00009195 Op0 = DAG.getNode(ISD::XOR, dl, VT, Op0, SignVec);
9196 Op1 = DAG.getNode(ISD::XOR, dl, VT, Op1, SignVec);
Nate Begeman30a0de92008-07-17 16:51:19 +00009197 }
Scott Michelfdc40a02009-02-17 22:15:04 +00009198
Dale Johannesenace16102009-02-03 19:33:06 +00009199 SDValue Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
Nate Begeman30a0de92008-07-17 16:51:19 +00009200
9201 // If the logical-not of the result is required, perform that now.
Bob Wilson4c245462009-01-22 17:39:32 +00009202 if (Invert)
Dale Johannesenace16102009-02-03 19:33:06 +00009203 Result = DAG.getNOT(dl, Result, VT);
Bob Wilson4c245462009-01-22 17:39:32 +00009204
Nate Begeman30a0de92008-07-17 16:51:19 +00009205 return Result;
9206}
Evan Cheng0488db92007-09-25 01:57:46 +00009207
Evan Cheng370e5342008-12-03 08:38:43 +00009208// isX86LogicalCmp - Return true if opcode is a X86 logical comparison.
Dan Gohman076aee32009-03-04 19:44:21 +00009209static bool isX86LogicalCmp(SDValue Op) {
9210 unsigned Opc = Op.getNode()->getOpcode();
Benjamin Kramer17c836c2012-04-27 12:07:43 +00009211 if (Opc == X86ISD::CMP || Opc == X86ISD::COMI || Opc == X86ISD::UCOMI ||
9212 Opc == X86ISD::SAHF)
Dan Gohman076aee32009-03-04 19:44:21 +00009213 return true;
9214 if (Op.getResNo() == 1 &&
9215 (Opc == X86ISD::ADD ||
9216 Opc == X86ISD::SUB ||
Chris Lattner5b856542010-12-20 00:59:46 +00009217 Opc == X86ISD::ADC ||
9218 Opc == X86ISD::SBB ||
Dan Gohman076aee32009-03-04 19:44:21 +00009219 Opc == X86ISD::SMUL ||
9220 Opc == X86ISD::UMUL ||
9221 Opc == X86ISD::INC ||
Dan Gohmane220c4b2009-09-18 19:59:53 +00009222 Opc == X86ISD::DEC ||
9223 Opc == X86ISD::OR ||
9224 Opc == X86ISD::XOR ||
9225 Opc == X86ISD::AND))
Dan Gohman076aee32009-03-04 19:44:21 +00009226 return true;
9227
Chris Lattner9637d5b2010-12-05 07:49:54 +00009228 if (Op.getResNo() == 2 && Opc == X86ISD::UMUL)
9229 return true;
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009230
Dan Gohman076aee32009-03-04 19:44:21 +00009231 return false;
Evan Cheng370e5342008-12-03 08:38:43 +00009232}
9233
Chris Lattnera2b56002010-12-05 01:23:24 +00009234static bool isZero(SDValue V) {
9235 ConstantSDNode *C = dyn_cast<ConstantSDNode>(V);
9236 return C && C->isNullValue();
9237}
9238
Chris Lattner96908b12010-12-05 02:00:51 +00009239static bool isAllOnes(SDValue V) {
9240 ConstantSDNode *C = dyn_cast<ConstantSDNode>(V);
9241 return C && C->isAllOnesValue();
9242}
9243
Evan Chengb64dd5f2012-08-07 22:21:00 +00009244static bool isTruncWithZeroHighBitsInput(SDValue V, SelectionDAG &DAG) {
9245 if (V.getOpcode() != ISD::TRUNCATE)
9246 return false;
9247
9248 SDValue VOp0 = V.getOperand(0);
9249 unsigned InBits = VOp0.getValueSizeInBits();
9250 unsigned Bits = V.getValueSizeInBits();
9251 return DAG.MaskedValueIsZero(VOp0, APInt::getHighBitsSet(InBits,InBits-Bits));
9252}
9253
Dan Gohmand858e902010-04-17 15:26:15 +00009254SDValue X86TargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng734503b2006-09-11 02:19:56 +00009255 bool addTest = true;
Dan Gohman475871a2008-07-27 21:46:04 +00009256 SDValue Cond = Op.getOperand(0);
Chris Lattnera2b56002010-12-05 01:23:24 +00009257 SDValue Op1 = Op.getOperand(1);
9258 SDValue Op2 = Op.getOperand(2);
9259 DebugLoc DL = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00009260 SDValue CC;
Evan Cheng9bba8942006-01-26 02:13:10 +00009261
Dan Gohman1a492952009-10-20 16:22:37 +00009262 if (Cond.getOpcode() == ISD::SETCC) {
9263 SDValue NewCond = LowerSETCC(Cond, DAG);
9264 if (NewCond.getNode())
9265 Cond = NewCond;
9266 }
Evan Cheng734503b2006-09-11 02:19:56 +00009267
Chris Lattnera2b56002010-12-05 01:23:24 +00009268 // (select (x == 0), -1, y) -> (sign_bit (x - 1)) | y
Chris Lattner96908b12010-12-05 02:00:51 +00009269 // (select (x == 0), y, -1) -> ~(sign_bit (x - 1)) | y
Chris Lattnera2b56002010-12-05 01:23:24 +00009270 // (select (x != 0), y, -1) -> (sign_bit (x - 1)) | y
Chris Lattner96908b12010-12-05 02:00:51 +00009271 // (select (x != 0), -1, y) -> ~(sign_bit (x - 1)) | y
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00009272 if (Cond.getOpcode() == X86ISD::SETCC &&
Chris Lattner96908b12010-12-05 02:00:51 +00009273 Cond.getOperand(1).getOpcode() == X86ISD::CMP &&
9274 isZero(Cond.getOperand(1).getOperand(1))) {
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00009275 SDValue Cmp = Cond.getOperand(1);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009276
Chris Lattnera2b56002010-12-05 01:23:24 +00009277 unsigned CondCode =cast<ConstantSDNode>(Cond.getOperand(0))->getZExtValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009278
9279 if ((isAllOnes(Op1) || isAllOnes(Op2)) &&
Chris Lattner96908b12010-12-05 02:00:51 +00009280 (CondCode == X86::COND_E || CondCode == X86::COND_NE)) {
9281 SDValue Y = isAllOnes(Op2) ? Op1 : Op2;
Chris Lattnera2b56002010-12-05 01:23:24 +00009282
9283 SDValue CmpOp0 = Cmp.getOperand(0);
Manman Rened579842012-05-07 18:06:23 +00009284 // Apply further optimizations for special cases
9285 // (select (x != 0), -1, 0) -> neg & sbb
9286 // (select (x == 0), 0, -1) -> neg & sbb
9287 if (ConstantSDNode *YC = dyn_cast<ConstantSDNode>(Y))
Chad Rosiera20e1e72012-08-01 18:39:17 +00009288 if (YC->isNullValue() &&
Manman Rened579842012-05-07 18:06:23 +00009289 (isAllOnes(Op1) == (CondCode == X86::COND_NE))) {
9290 SDVTList VTs = DAG.getVTList(CmpOp0.getValueType(), MVT::i32);
Chad Rosiera20e1e72012-08-01 18:39:17 +00009291 SDValue Neg = DAG.getNode(X86ISD::SUB, DL, VTs,
9292 DAG.getConstant(0, CmpOp0.getValueType()),
Manman Rened579842012-05-07 18:06:23 +00009293 CmpOp0);
9294 SDValue Res = DAG.getNode(X86ISD::SETCC_CARRY, DL, Op.getValueType(),
9295 DAG.getConstant(X86::COND_B, MVT::i8),
9296 SDValue(Neg.getNode(), 1));
9297 return Res;
9298 }
9299
Chris Lattnera2b56002010-12-05 01:23:24 +00009300 Cmp = DAG.getNode(X86ISD::CMP, DL, MVT::i32,
9301 CmpOp0, DAG.getConstant(1, CmpOp0.getValueType()));
Benjamin Kramer17c836c2012-04-27 12:07:43 +00009302 Cmp = ConvertCmpIfNecessary(Cmp, DAG);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009303
Chris Lattner96908b12010-12-05 02:00:51 +00009304 SDValue Res = // Res = 0 or -1.
Chris Lattnera2b56002010-12-05 01:23:24 +00009305 DAG.getNode(X86ISD::SETCC_CARRY, DL, Op.getValueType(),
9306 DAG.getConstant(X86::COND_B, MVT::i8), Cmp);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009307
Chris Lattner96908b12010-12-05 02:00:51 +00009308 if (isAllOnes(Op1) != (CondCode == X86::COND_E))
9309 Res = DAG.getNOT(DL, Res, Res.getValueType());
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009310
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00009311 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(Op2);
Chris Lattnera2b56002010-12-05 01:23:24 +00009312 if (N2C == 0 || !N2C->isNullValue())
9313 Res = DAG.getNode(ISD::OR, DL, Res.getValueType(), Res, Y);
9314 return Res;
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00009315 }
9316 }
9317
Chris Lattnera2b56002010-12-05 01:23:24 +00009318 // Look past (and (setcc_carry (cmp ...)), 1).
Evan Chengad9c0a32009-12-15 00:53:42 +00009319 if (Cond.getOpcode() == ISD::AND &&
9320 Cond.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY) {
9321 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Cond.getOperand(1));
Michael J. Spencerec38de22010-10-10 22:04:20 +00009322 if (C && C->getAPIntValue() == 1)
Evan Chengad9c0a32009-12-15 00:53:42 +00009323 Cond = Cond.getOperand(0);
9324 }
9325
Evan Cheng3f41d662007-10-08 22:16:29 +00009326 // If condition flag is set by a X86ISD::CMP, then use it as the condition
9327 // setting operand in place of the X86ISD::SETCC.
Dan Gohman65fd6562011-11-03 21:49:52 +00009328 unsigned CondOpcode = Cond.getOpcode();
9329 if (CondOpcode == X86ISD::SETCC ||
9330 CondOpcode == X86ISD::SETCC_CARRY) {
Evan Cheng734503b2006-09-11 02:19:56 +00009331 CC = Cond.getOperand(0);
9332
Dan Gohman475871a2008-07-27 21:46:04 +00009333 SDValue Cmp = Cond.getOperand(1);
Evan Cheng734503b2006-09-11 02:19:56 +00009334 unsigned Opc = Cmp.getOpcode();
Owen Andersone50ed302009-08-10 22:56:29 +00009335 EVT VT = Op.getValueType();
Scott Michelfdc40a02009-02-17 22:15:04 +00009336
Evan Cheng3f41d662007-10-08 22:16:29 +00009337 bool IllegalFPCMov = false;
Duncan Sands83ec4b62008-06-06 12:08:01 +00009338 if (VT.isFloatingPoint() && !VT.isVector() &&
Chris Lattner78631162008-01-16 06:24:21 +00009339 !isScalarFPTypeInSSEReg(VT)) // FPStack?
Dan Gohman7810bfe2008-09-26 21:54:37 +00009340 IllegalFPCMov = !hasFPCMov(cast<ConstantSDNode>(CC)->getSExtValue());
Scott Michelfdc40a02009-02-17 22:15:04 +00009341
Chris Lattnerd1980a52009-03-12 06:52:53 +00009342 if ((isX86LogicalCmp(Cmp) && !IllegalFPCMov) ||
9343 Opc == X86ISD::BT) { // FIXME
Evan Cheng3f41d662007-10-08 22:16:29 +00009344 Cond = Cmp;
Evan Cheng0488db92007-09-25 01:57:46 +00009345 addTest = false;
9346 }
Dan Gohman65fd6562011-11-03 21:49:52 +00009347 } else if (CondOpcode == ISD::USUBO || CondOpcode == ISD::SSUBO ||
9348 CondOpcode == ISD::UADDO || CondOpcode == ISD::SADDO ||
9349 ((CondOpcode == ISD::UMULO || CondOpcode == ISD::SMULO) &&
9350 Cond.getOperand(0).getValueType() != MVT::i8)) {
9351 SDValue LHS = Cond.getOperand(0);
9352 SDValue RHS = Cond.getOperand(1);
9353 unsigned X86Opcode;
9354 unsigned X86Cond;
9355 SDVTList VTs;
9356 switch (CondOpcode) {
9357 case ISD::UADDO: X86Opcode = X86ISD::ADD; X86Cond = X86::COND_B; break;
9358 case ISD::SADDO: X86Opcode = X86ISD::ADD; X86Cond = X86::COND_O; break;
9359 case ISD::USUBO: X86Opcode = X86ISD::SUB; X86Cond = X86::COND_B; break;
9360 case ISD::SSUBO: X86Opcode = X86ISD::SUB; X86Cond = X86::COND_O; break;
9361 case ISD::UMULO: X86Opcode = X86ISD::UMUL; X86Cond = X86::COND_O; break;
9362 case ISD::SMULO: X86Opcode = X86ISD::SMUL; X86Cond = X86::COND_O; break;
9363 default: llvm_unreachable("unexpected overflowing operator");
9364 }
9365 if (CondOpcode == ISD::UMULO)
9366 VTs = DAG.getVTList(LHS.getValueType(), LHS.getValueType(),
9367 MVT::i32);
9368 else
9369 VTs = DAG.getVTList(LHS.getValueType(), MVT::i32);
9370
9371 SDValue X86Op = DAG.getNode(X86Opcode, DL, VTs, LHS, RHS);
9372
9373 if (CondOpcode == ISD::UMULO)
9374 Cond = X86Op.getValue(2);
9375 else
9376 Cond = X86Op.getValue(1);
9377
9378 CC = DAG.getConstant(X86Cond, MVT::i8);
9379 addTest = false;
Evan Cheng0488db92007-09-25 01:57:46 +00009380 }
9381
9382 if (addTest) {
Evan Chengb64dd5f2012-08-07 22:21:00 +00009383 // Look pass the truncate if the high bits are known zero.
9384 if (isTruncWithZeroHighBitsInput(Cond, DAG))
9385 Cond = Cond.getOperand(0);
Evan Chengd40d03e2010-01-06 19:38:29 +00009386
9387 // We know the result of AND is compared against zero. Try to match
9388 // it to BT.
Michael J. Spencerec38de22010-10-10 22:04:20 +00009389 if (Cond.getOpcode() == ISD::AND && Cond.hasOneUse()) {
Chris Lattnera2b56002010-12-05 01:23:24 +00009390 SDValue NewSetCC = LowerToBT(Cond, ISD::SETNE, DL, DAG);
Evan Chengd40d03e2010-01-06 19:38:29 +00009391 if (NewSetCC.getNode()) {
9392 CC = NewSetCC.getOperand(0);
9393 Cond = NewSetCC.getOperand(1);
9394 addTest = false;
9395 }
9396 }
9397 }
9398
9399 if (addTest) {
Owen Anderson825b72b2009-08-11 20:47:22 +00009400 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng552f09a2010-04-26 19:06:11 +00009401 Cond = EmitTest(Cond, X86::COND_NE, DAG);
Evan Cheng0488db92007-09-25 01:57:46 +00009402 }
9403
Benjamin Kramere915ff32010-12-22 23:09:28 +00009404 // a < b ? -1 : 0 -> RES = ~setcc_carry
9405 // a < b ? 0 : -1 -> RES = setcc_carry
9406 // a >= b ? -1 : 0 -> RES = setcc_carry
9407 // a >= b ? 0 : -1 -> RES = ~setcc_carry
Manman Ren39ad5682012-08-08 00:51:41 +00009408 if (Cond.getOpcode() == X86ISD::SUB) {
Benjamin Kramer17c836c2012-04-27 12:07:43 +00009409 Cond = ConvertCmpIfNecessary(Cond, DAG);
Benjamin Kramere915ff32010-12-22 23:09:28 +00009410 unsigned CondCode = cast<ConstantSDNode>(CC)->getZExtValue();
9411
9412 if ((CondCode == X86::COND_AE || CondCode == X86::COND_B) &&
9413 (isAllOnes(Op1) || isAllOnes(Op2)) && (isZero(Op1) || isZero(Op2))) {
9414 SDValue Res = DAG.getNode(X86ISD::SETCC_CARRY, DL, Op.getValueType(),
9415 DAG.getConstant(X86::COND_B, MVT::i8), Cond);
9416 if (isAllOnes(Op1) != (CondCode == X86::COND_B))
9417 return DAG.getNOT(DL, Res, Res.getValueType());
9418 return Res;
9419 }
9420 }
9421
Benjamin Kramer444dcce2012-10-13 10:39:49 +00009422 // X86 doesn't have an i8 cmov. If both operands are the result of a truncate
9423 // widen the cmov and push the truncate through. This avoids introducing a new
9424 // branch during isel and doesn't add any extensions.
9425 if (Op.getValueType() == MVT::i8 &&
9426 Op1.getOpcode() == ISD::TRUNCATE && Op2.getOpcode() == ISD::TRUNCATE) {
9427 SDValue T1 = Op1.getOperand(0), T2 = Op2.getOperand(0);
9428 if (T1.getValueType() == T2.getValueType() &&
9429 // Blacklist CopyFromReg to avoid partial register stalls.
9430 T1.getOpcode() != ISD::CopyFromReg && T2.getOpcode()!=ISD::CopyFromReg){
9431 SDVTList VTs = DAG.getVTList(T1.getValueType(), MVT::Glue);
Benjamin Kramerf8b65aa2012-10-13 12:50:19 +00009432 SDValue Cmov = DAG.getNode(X86ISD::CMOV, DL, VTs, T2, T1, CC, Cond);
Benjamin Kramer444dcce2012-10-13 10:39:49 +00009433 return DAG.getNode(ISD::TRUNCATE, DL, Op.getValueType(), Cmov);
9434 }
9435 }
9436
Evan Cheng0488db92007-09-25 01:57:46 +00009437 // X86ISD::CMOV means set the result (which is operand 1) to the RHS if
9438 // condition is true.
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00009439 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::Glue);
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00009440 SDValue Ops[] = { Op2, Op1, CC, Cond };
Chris Lattnera2b56002010-12-05 01:23:24 +00009441 return DAG.getNode(X86ISD::CMOV, DL, VTs, Ops, array_lengthof(Ops));
Evan Cheng0488db92007-09-25 01:57:46 +00009442}
9443
Evan Cheng370e5342008-12-03 08:38:43 +00009444// isAndOrOfSingleUseSetCCs - Return true if node is an ISD::AND or
9445// ISD::OR of two X86ISD::SETCC nodes each of which has no other use apart
9446// from the AND / OR.
9447static bool isAndOrOfSetCCs(SDValue Op, unsigned &Opc) {
9448 Opc = Op.getOpcode();
9449 if (Opc != ISD::OR && Opc != ISD::AND)
9450 return false;
9451 return (Op.getOperand(0).getOpcode() == X86ISD::SETCC &&
9452 Op.getOperand(0).hasOneUse() &&
9453 Op.getOperand(1).getOpcode() == X86ISD::SETCC &&
9454 Op.getOperand(1).hasOneUse());
9455}
9456
Evan Cheng961d6d42009-02-02 08:19:07 +00009457// isXor1OfSetCC - Return true if node is an ISD::XOR of a X86ISD::SETCC and
9458// 1 and that the SETCC node has a single use.
Evan Cheng67ad9db2009-02-02 08:07:36 +00009459static bool isXor1OfSetCC(SDValue Op) {
9460 if (Op.getOpcode() != ISD::XOR)
9461 return false;
9462 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
9463 if (N1C && N1C->getAPIntValue() == 1) {
9464 return Op.getOperand(0).getOpcode() == X86ISD::SETCC &&
9465 Op.getOperand(0).hasOneUse();
9466 }
9467 return false;
9468}
9469
Dan Gohmand858e902010-04-17 15:26:15 +00009470SDValue X86TargetLowering::LowerBRCOND(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng734503b2006-09-11 02:19:56 +00009471 bool addTest = true;
Dan Gohman475871a2008-07-27 21:46:04 +00009472 SDValue Chain = Op.getOperand(0);
9473 SDValue Cond = Op.getOperand(1);
9474 SDValue Dest = Op.getOperand(2);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009475 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00009476 SDValue CC;
Dan Gohman65fd6562011-11-03 21:49:52 +00009477 bool Inverted = false;
Evan Cheng734503b2006-09-11 02:19:56 +00009478
Dan Gohman1a492952009-10-20 16:22:37 +00009479 if (Cond.getOpcode() == ISD::SETCC) {
Dan Gohman65fd6562011-11-03 21:49:52 +00009480 // Check for setcc([su]{add,sub,mul}o == 0).
9481 if (cast<CondCodeSDNode>(Cond.getOperand(2))->get() == ISD::SETEQ &&
9482 isa<ConstantSDNode>(Cond.getOperand(1)) &&
9483 cast<ConstantSDNode>(Cond.getOperand(1))->isNullValue() &&
9484 Cond.getOperand(0).getResNo() == 1 &&
9485 (Cond.getOperand(0).getOpcode() == ISD::SADDO ||
9486 Cond.getOperand(0).getOpcode() == ISD::UADDO ||
9487 Cond.getOperand(0).getOpcode() == ISD::SSUBO ||
9488 Cond.getOperand(0).getOpcode() == ISD::USUBO ||
9489 Cond.getOperand(0).getOpcode() == ISD::SMULO ||
9490 Cond.getOperand(0).getOpcode() == ISD::UMULO)) {
9491 Inverted = true;
9492 Cond = Cond.getOperand(0);
9493 } else {
9494 SDValue NewCond = LowerSETCC(Cond, DAG);
9495 if (NewCond.getNode())
9496 Cond = NewCond;
9497 }
Dan Gohman1a492952009-10-20 16:22:37 +00009498 }
Chris Lattnere55484e2008-12-25 05:34:37 +00009499#if 0
9500 // FIXME: LowerXALUO doesn't handle these!!
Bill Wendlingd350e022008-12-12 21:15:41 +00009501 else if (Cond.getOpcode() == X86ISD::ADD ||
9502 Cond.getOpcode() == X86ISD::SUB ||
9503 Cond.getOpcode() == X86ISD::SMUL ||
9504 Cond.getOpcode() == X86ISD::UMUL)
Bill Wendling74c37652008-12-09 22:08:41 +00009505 Cond = LowerXALUO(Cond, DAG);
Chris Lattnere55484e2008-12-25 05:34:37 +00009506#endif
Scott Michelfdc40a02009-02-17 22:15:04 +00009507
Evan Chengad9c0a32009-12-15 00:53:42 +00009508 // Look pass (and (setcc_carry (cmp ...)), 1).
9509 if (Cond.getOpcode() == ISD::AND &&
9510 Cond.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY) {
9511 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Cond.getOperand(1));
Michael J. Spencerec38de22010-10-10 22:04:20 +00009512 if (C && C->getAPIntValue() == 1)
Evan Chengad9c0a32009-12-15 00:53:42 +00009513 Cond = Cond.getOperand(0);
9514 }
9515
Evan Cheng3f41d662007-10-08 22:16:29 +00009516 // If condition flag is set by a X86ISD::CMP, then use it as the condition
9517 // setting operand in place of the X86ISD::SETCC.
Dan Gohman65fd6562011-11-03 21:49:52 +00009518 unsigned CondOpcode = Cond.getOpcode();
9519 if (CondOpcode == X86ISD::SETCC ||
9520 CondOpcode == X86ISD::SETCC_CARRY) {
Evan Cheng734503b2006-09-11 02:19:56 +00009521 CC = Cond.getOperand(0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00009522
Dan Gohman475871a2008-07-27 21:46:04 +00009523 SDValue Cmp = Cond.getOperand(1);
Evan Cheng734503b2006-09-11 02:19:56 +00009524 unsigned Opc = Cmp.getOpcode();
Chris Lattnere55484e2008-12-25 05:34:37 +00009525 // FIXME: WHY THE SPECIAL CASING OF LogicalCmp??
Dan Gohman076aee32009-03-04 19:44:21 +00009526 if (isX86LogicalCmp(Cmp) || Opc == X86ISD::BT) {
Evan Cheng3f41d662007-10-08 22:16:29 +00009527 Cond = Cmp;
Evan Cheng0488db92007-09-25 01:57:46 +00009528 addTest = false;
Bill Wendling61edeb52008-12-02 01:06:39 +00009529 } else {
Evan Cheng370e5342008-12-03 08:38:43 +00009530 switch (cast<ConstantSDNode>(CC)->getZExtValue()) {
Bill Wendling0ea25cb2008-12-03 08:32:02 +00009531 default: break;
9532 case X86::COND_O:
Dan Gohman653456c2009-01-07 00:15:08 +00009533 case X86::COND_B:
Chris Lattnere55484e2008-12-25 05:34:37 +00009534 // These can only come from an arithmetic instruction with overflow,
9535 // e.g. SADDO, UADDO.
Bill Wendling0ea25cb2008-12-03 08:32:02 +00009536 Cond = Cond.getNode()->getOperand(1);
9537 addTest = false;
9538 break;
Bill Wendling61edeb52008-12-02 01:06:39 +00009539 }
Evan Cheng0488db92007-09-25 01:57:46 +00009540 }
Dan Gohman65fd6562011-11-03 21:49:52 +00009541 }
9542 CondOpcode = Cond.getOpcode();
9543 if (CondOpcode == ISD::UADDO || CondOpcode == ISD::SADDO ||
9544 CondOpcode == ISD::USUBO || CondOpcode == ISD::SSUBO ||
9545 ((CondOpcode == ISD::UMULO || CondOpcode == ISD::SMULO) &&
9546 Cond.getOperand(0).getValueType() != MVT::i8)) {
9547 SDValue LHS = Cond.getOperand(0);
9548 SDValue RHS = Cond.getOperand(1);
9549 unsigned X86Opcode;
9550 unsigned X86Cond;
9551 SDVTList VTs;
9552 switch (CondOpcode) {
9553 case ISD::UADDO: X86Opcode = X86ISD::ADD; X86Cond = X86::COND_B; break;
9554 case ISD::SADDO: X86Opcode = X86ISD::ADD; X86Cond = X86::COND_O; break;
9555 case ISD::USUBO: X86Opcode = X86ISD::SUB; X86Cond = X86::COND_B; break;
9556 case ISD::SSUBO: X86Opcode = X86ISD::SUB; X86Cond = X86::COND_O; break;
9557 case ISD::UMULO: X86Opcode = X86ISD::UMUL; X86Cond = X86::COND_O; break;
9558 case ISD::SMULO: X86Opcode = X86ISD::SMUL; X86Cond = X86::COND_O; break;
9559 default: llvm_unreachable("unexpected overflowing operator");
9560 }
9561 if (Inverted)
9562 X86Cond = X86::GetOppositeBranchCondition((X86::CondCode)X86Cond);
9563 if (CondOpcode == ISD::UMULO)
9564 VTs = DAG.getVTList(LHS.getValueType(), LHS.getValueType(),
9565 MVT::i32);
9566 else
9567 VTs = DAG.getVTList(LHS.getValueType(), MVT::i32);
9568
9569 SDValue X86Op = DAG.getNode(X86Opcode, dl, VTs, LHS, RHS);
9570
9571 if (CondOpcode == ISD::UMULO)
9572 Cond = X86Op.getValue(2);
9573 else
9574 Cond = X86Op.getValue(1);
9575
9576 CC = DAG.getConstant(X86Cond, MVT::i8);
9577 addTest = false;
Evan Cheng370e5342008-12-03 08:38:43 +00009578 } else {
9579 unsigned CondOpc;
9580 if (Cond.hasOneUse() && isAndOrOfSetCCs(Cond, CondOpc)) {
9581 SDValue Cmp = Cond.getOperand(0).getOperand(1);
Evan Cheng370e5342008-12-03 08:38:43 +00009582 if (CondOpc == ISD::OR) {
9583 // Also, recognize the pattern generated by an FCMP_UNE. We can emit
9584 // two branches instead of an explicit OR instruction with a
9585 // separate test.
9586 if (Cmp == Cond.getOperand(1).getOperand(1) &&
Dan Gohman076aee32009-03-04 19:44:21 +00009587 isX86LogicalCmp(Cmp)) {
Evan Cheng370e5342008-12-03 08:38:43 +00009588 CC = Cond.getOperand(0).getOperand(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009589 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Evan Cheng370e5342008-12-03 08:38:43 +00009590 Chain, Dest, CC, Cmp);
9591 CC = Cond.getOperand(1).getOperand(0);
9592 Cond = Cmp;
9593 addTest = false;
9594 }
9595 } else { // ISD::AND
9596 // Also, recognize the pattern generated by an FCMP_OEQ. We can emit
9597 // two branches instead of an explicit AND instruction with a
9598 // separate test. However, we only do this if this block doesn't
9599 // have a fall-through edge, because this requires an explicit
9600 // jmp when the condition is false.
9601 if (Cmp == Cond.getOperand(1).getOperand(1) &&
Dan Gohman076aee32009-03-04 19:44:21 +00009602 isX86LogicalCmp(Cmp) &&
Evan Cheng370e5342008-12-03 08:38:43 +00009603 Op.getNode()->hasOneUse()) {
9604 X86::CondCode CCode =
9605 (X86::CondCode)Cond.getOperand(0).getConstantOperandVal(0);
9606 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00009607 CC = DAG.getConstant(CCode, MVT::i8);
Dan Gohman027657d2010-06-18 15:30:29 +00009608 SDNode *User = *Op.getNode()->use_begin();
Evan Cheng370e5342008-12-03 08:38:43 +00009609 // Look for an unconditional branch following this conditional branch.
9610 // We need this because we need to reverse the successors in order
9611 // to implement FCMP_OEQ.
Dan Gohman027657d2010-06-18 15:30:29 +00009612 if (User->getOpcode() == ISD::BR) {
9613 SDValue FalseBB = User->getOperand(1);
9614 SDNode *NewBR =
9615 DAG.UpdateNodeOperands(User, User->getOperand(0), Dest);
Evan Cheng370e5342008-12-03 08:38:43 +00009616 assert(NewBR == User);
Nick Lewycky2a3ee5e2010-06-20 20:27:42 +00009617 (void)NewBR;
Evan Cheng370e5342008-12-03 08:38:43 +00009618 Dest = FalseBB;
Dan Gohman279c22e2008-10-21 03:29:32 +00009619
Dale Johannesene4d209d2009-02-03 20:21:25 +00009620 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Evan Cheng370e5342008-12-03 08:38:43 +00009621 Chain, Dest, CC, Cmp);
9622 X86::CondCode CCode =
9623 (X86::CondCode)Cond.getOperand(1).getConstantOperandVal(0);
9624 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00009625 CC = DAG.getConstant(CCode, MVT::i8);
Evan Cheng370e5342008-12-03 08:38:43 +00009626 Cond = Cmp;
9627 addTest = false;
9628 }
9629 }
Dan Gohman279c22e2008-10-21 03:29:32 +00009630 }
Evan Cheng67ad9db2009-02-02 08:07:36 +00009631 } else if (Cond.hasOneUse() && isXor1OfSetCC(Cond)) {
9632 // Recognize for xorb (setcc), 1 patterns. The xor inverts the condition.
9633 // It should be transformed during dag combiner except when the condition
9634 // is set by a arithmetics with overflow node.
9635 X86::CondCode CCode =
9636 (X86::CondCode)Cond.getOperand(0).getConstantOperandVal(0);
9637 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00009638 CC = DAG.getConstant(CCode, MVT::i8);
Evan Cheng67ad9db2009-02-02 08:07:36 +00009639 Cond = Cond.getOperand(0).getOperand(1);
9640 addTest = false;
Dan Gohman65fd6562011-11-03 21:49:52 +00009641 } else if (Cond.getOpcode() == ISD::SETCC &&
9642 cast<CondCodeSDNode>(Cond.getOperand(2))->get() == ISD::SETOEQ) {
9643 // For FCMP_OEQ, we can emit
9644 // two branches instead of an explicit AND instruction with a
9645 // separate test. However, we only do this if this block doesn't
9646 // have a fall-through edge, because this requires an explicit
9647 // jmp when the condition is false.
9648 if (Op.getNode()->hasOneUse()) {
9649 SDNode *User = *Op.getNode()->use_begin();
9650 // Look for an unconditional branch following this conditional branch.
9651 // We need this because we need to reverse the successors in order
9652 // to implement FCMP_OEQ.
9653 if (User->getOpcode() == ISD::BR) {
9654 SDValue FalseBB = User->getOperand(1);
9655 SDNode *NewBR =
9656 DAG.UpdateNodeOperands(User, User->getOperand(0), Dest);
9657 assert(NewBR == User);
9658 (void)NewBR;
9659 Dest = FalseBB;
9660
9661 SDValue Cmp = DAG.getNode(X86ISD::CMP, dl, MVT::i32,
9662 Cond.getOperand(0), Cond.getOperand(1));
Benjamin Kramer17c836c2012-04-27 12:07:43 +00009663 Cmp = ConvertCmpIfNecessary(Cmp, DAG);
Dan Gohman65fd6562011-11-03 21:49:52 +00009664 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
9665 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
9666 Chain, Dest, CC, Cmp);
9667 CC = DAG.getConstant(X86::COND_P, MVT::i8);
9668 Cond = Cmp;
9669 addTest = false;
9670 }
9671 }
9672 } else if (Cond.getOpcode() == ISD::SETCC &&
9673 cast<CondCodeSDNode>(Cond.getOperand(2))->get() == ISD::SETUNE) {
9674 // For FCMP_UNE, we can emit
9675 // two branches instead of an explicit AND instruction with a
9676 // separate test. However, we only do this if this block doesn't
9677 // have a fall-through edge, because this requires an explicit
9678 // jmp when the condition is false.
9679 if (Op.getNode()->hasOneUse()) {
9680 SDNode *User = *Op.getNode()->use_begin();
9681 // Look for an unconditional branch following this conditional branch.
9682 // We need this because we need to reverse the successors in order
9683 // to implement FCMP_UNE.
9684 if (User->getOpcode() == ISD::BR) {
9685 SDValue FalseBB = User->getOperand(1);
9686 SDNode *NewBR =
9687 DAG.UpdateNodeOperands(User, User->getOperand(0), Dest);
9688 assert(NewBR == User);
9689 (void)NewBR;
9690
9691 SDValue Cmp = DAG.getNode(X86ISD::CMP, dl, MVT::i32,
9692 Cond.getOperand(0), Cond.getOperand(1));
Benjamin Kramer17c836c2012-04-27 12:07:43 +00009693 Cmp = ConvertCmpIfNecessary(Cmp, DAG);
Dan Gohman65fd6562011-11-03 21:49:52 +00009694 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
9695 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
9696 Chain, Dest, CC, Cmp);
9697 CC = DAG.getConstant(X86::COND_NP, MVT::i8);
9698 Cond = Cmp;
9699 addTest = false;
9700 Dest = FalseBB;
9701 }
9702 }
Dan Gohman279c22e2008-10-21 03:29:32 +00009703 }
Evan Cheng0488db92007-09-25 01:57:46 +00009704 }
9705
9706 if (addTest) {
Evan Chengb64dd5f2012-08-07 22:21:00 +00009707 // Look pass the truncate if the high bits are known zero.
9708 if (isTruncWithZeroHighBitsInput(Cond, DAG))
9709 Cond = Cond.getOperand(0);
Evan Chengd40d03e2010-01-06 19:38:29 +00009710
9711 // We know the result of AND is compared against zero. Try to match
9712 // it to BT.
Michael J. Spencerec38de22010-10-10 22:04:20 +00009713 if (Cond.getOpcode() == ISD::AND && Cond.hasOneUse()) {
Evan Chengd40d03e2010-01-06 19:38:29 +00009714 SDValue NewSetCC = LowerToBT(Cond, ISD::SETNE, dl, DAG);
9715 if (NewSetCC.getNode()) {
9716 CC = NewSetCC.getOperand(0);
9717 Cond = NewSetCC.getOperand(1);
9718 addTest = false;
9719 }
9720 }
9721 }
9722
9723 if (addTest) {
Owen Anderson825b72b2009-08-11 20:47:22 +00009724 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng552f09a2010-04-26 19:06:11 +00009725 Cond = EmitTest(Cond, X86::COND_NE, DAG);
Evan Cheng0488db92007-09-25 01:57:46 +00009726 }
Benjamin Kramer17c836c2012-04-27 12:07:43 +00009727 Cond = ConvertCmpIfNecessary(Cond, DAG);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009728 return DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Dan Gohman279c22e2008-10-21 03:29:32 +00009729 Chain, Dest, CC, Cond);
Evan Cheng0488db92007-09-25 01:57:46 +00009730}
9731
Anton Korobeynikove060b532007-04-17 19:34:00 +00009732
9733// Lower dynamic stack allocation to _alloca call for Cygwin/Mingw targets.
9734// Calls to _alloca is needed to probe the stack when allocating more than 4k
9735// bytes in one go. Touching the stack at 4K increments is necessary to ensure
9736// that the guard pages used by the OS virtual memory manager are allocated in
9737// correct sequence.
Dan Gohman475871a2008-07-27 21:46:04 +00009738SDValue
9739X86TargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00009740 SelectionDAG &DAG) const {
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009741 assert((Subtarget->isTargetCygMing() || Subtarget->isTargetWindows() ||
Nick Lewycky8a8d4792011-12-02 22:16:29 +00009742 getTargetMachine().Options.EnableSegmentedStacks) &&
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009743 "This should be used only on Windows targets or when segmented stacks "
Rafael Espindola96428ce2011-09-06 18:43:08 +00009744 "are being used");
9745 assert(!Subtarget->isTargetEnvMacho() && "Not implemented");
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009746 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov096b4612008-06-11 20:16:42 +00009747
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00009748 // Get the inputs.
Dan Gohman475871a2008-07-27 21:46:04 +00009749 SDValue Chain = Op.getOperand(0);
9750 SDValue Size = Op.getOperand(1);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00009751 // FIXME: Ensure alignment here
9752
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009753 bool Is64Bit = Subtarget->is64Bit();
9754 EVT SPTy = Is64Bit ? MVT::i64 : MVT::i32;
Anton Korobeynikov096b4612008-06-11 20:16:42 +00009755
Nick Lewycky8a8d4792011-12-02 22:16:29 +00009756 if (getTargetMachine().Options.EnableSegmentedStacks) {
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009757 MachineFunction &MF = DAG.getMachineFunction();
9758 MachineRegisterInfo &MRI = MF.getRegInfo();
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00009759
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009760 if (Is64Bit) {
9761 // The 64 bit implementation of segmented stacks needs to clobber both r10
Rafael Espindola96428ce2011-09-06 18:43:08 +00009762 // r11. This makes it impossible to use it along with nested parameters.
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009763 const Function *F = MF.getFunction();
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00009764
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009765 for (Function::const_arg_iterator I = F->arg_begin(), E = F->arg_end();
Craig Topper31a207a2012-05-04 06:39:13 +00009766 I != E; ++I)
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009767 if (I->hasNestAttr())
9768 report_fatal_error("Cannot use segmented stacks with functions that "
9769 "have nested arguments.");
9770 }
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00009771
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009772 const TargetRegisterClass *AddrRegClass =
9773 getRegClassFor(Subtarget->is64Bit() ? MVT::i64:MVT::i32);
9774 unsigned Vreg = MRI.createVirtualRegister(AddrRegClass);
9775 Chain = DAG.getCopyToReg(Chain, dl, Vreg, Size);
9776 SDValue Value = DAG.getNode(X86ISD::SEG_ALLOCA, dl, SPTy, Chain,
9777 DAG.getRegister(Vreg, SPTy));
9778 SDValue Ops1[2] = { Value, Chain };
9779 return DAG.getMergeValues(Ops1, 2, dl);
9780 } else {
9781 SDValue Flag;
9782 unsigned Reg = (Subtarget->is64Bit() ? X86::RAX : X86::EAX);
Anton Korobeynikov096b4612008-06-11 20:16:42 +00009783
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009784 Chain = DAG.getCopyToReg(Chain, dl, Reg, Size, Flag);
9785 Flag = Chain.getValue(1);
9786 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Anton Korobeynikov096b4612008-06-11 20:16:42 +00009787
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009788 Chain = DAG.getNode(X86ISD::WIN_ALLOCA, dl, NodeTys, Chain, Flag);
9789 Flag = Chain.getValue(1);
9790
Michael Liaoc5c970e2012-10-31 04:14:09 +00009791 Chain = DAG.getCopyFromReg(Chain, dl, RegInfo->getStackRegister(),
9792 SPTy).getValue(1);
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009793
9794 SDValue Ops1[2] = { Chain.getValue(0), Chain };
9795 return DAG.getMergeValues(Ops1, 2, dl);
9796 }
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00009797}
9798
Dan Gohmand858e902010-04-17 15:26:15 +00009799SDValue X86TargetLowering::LowerVASTART(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman1e93df62010-04-17 14:41:14 +00009800 MachineFunction &MF = DAG.getMachineFunction();
9801 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
9802
Dan Gohman69de1932008-02-06 22:27:42 +00009803 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Chris Lattner8026a9d2010-09-21 17:50:43 +00009804 DebugLoc DL = Op.getDebugLoc();
Evan Cheng8b2794a2006-10-13 21:14:26 +00009805
Anton Korobeynikove7beda12010-10-03 22:52:07 +00009806 if (!Subtarget->is64Bit() || Subtarget->isTargetWin64()) {
Evan Cheng25ab6902006-09-08 06:48:29 +00009807 // vastart just stores the address of the VarArgsFrameIndex slot into the
9808 // memory location argument.
Dan Gohman1e93df62010-04-17 14:41:14 +00009809 SDValue FR = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
9810 getPointerTy());
Chris Lattner8026a9d2010-09-21 17:50:43 +00009811 return DAG.getStore(Op.getOperand(0), DL, FR, Op.getOperand(1),
9812 MachinePointerInfo(SV), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00009813 }
9814
9815 // __va_list_tag:
9816 // gp_offset (0 - 6 * 8)
9817 // fp_offset (48 - 48 + 8 * 16)
9818 // overflow_arg_area (point to parameters coming in memory).
9819 // reg_save_area
Dan Gohman475871a2008-07-27 21:46:04 +00009820 SmallVector<SDValue, 8> MemOps;
9821 SDValue FIN = Op.getOperand(1);
Evan Cheng25ab6902006-09-08 06:48:29 +00009822 // Store gp_offset
Chris Lattner8026a9d2010-09-21 17:50:43 +00009823 SDValue Store = DAG.getStore(Op.getOperand(0), DL,
Dan Gohman1e93df62010-04-17 14:41:14 +00009824 DAG.getConstant(FuncInfo->getVarArgsGPOffset(),
9825 MVT::i32),
Chris Lattner8026a9d2010-09-21 17:50:43 +00009826 FIN, MachinePointerInfo(SV), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00009827 MemOps.push_back(Store);
9828
9829 // Store fp_offset
Chris Lattner8026a9d2010-09-21 17:50:43 +00009830 FIN = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00009831 FIN, DAG.getIntPtrConstant(4));
Chris Lattner8026a9d2010-09-21 17:50:43 +00009832 Store = DAG.getStore(Op.getOperand(0), DL,
Dan Gohman1e93df62010-04-17 14:41:14 +00009833 DAG.getConstant(FuncInfo->getVarArgsFPOffset(),
9834 MVT::i32),
Chris Lattner8026a9d2010-09-21 17:50:43 +00009835 FIN, MachinePointerInfo(SV, 4), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00009836 MemOps.push_back(Store);
9837
9838 // Store ptr to overflow_arg_area
Chris Lattner8026a9d2010-09-21 17:50:43 +00009839 FIN = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00009840 FIN, DAG.getIntPtrConstant(4));
Dan Gohman1e93df62010-04-17 14:41:14 +00009841 SDValue OVFIN = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
9842 getPointerTy());
Chris Lattner8026a9d2010-09-21 17:50:43 +00009843 Store = DAG.getStore(Op.getOperand(0), DL, OVFIN, FIN,
9844 MachinePointerInfo(SV, 8),
David Greene67c9d422010-02-15 16:53:33 +00009845 false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00009846 MemOps.push_back(Store);
9847
9848 // Store ptr to reg_save_area.
Chris Lattner8026a9d2010-09-21 17:50:43 +00009849 FIN = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00009850 FIN, DAG.getIntPtrConstant(8));
Dan Gohman1e93df62010-04-17 14:41:14 +00009851 SDValue RSFIN = DAG.getFrameIndex(FuncInfo->getRegSaveFrameIndex(),
9852 getPointerTy());
Chris Lattner8026a9d2010-09-21 17:50:43 +00009853 Store = DAG.getStore(Op.getOperand(0), DL, RSFIN, FIN,
9854 MachinePointerInfo(SV, 16), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00009855 MemOps.push_back(Store);
Chris Lattner8026a9d2010-09-21 17:50:43 +00009856 return DAG.getNode(ISD::TokenFactor, DL, MVT::Other,
Dale Johannesene4d209d2009-02-03 20:21:25 +00009857 &MemOps[0], MemOps.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00009858}
9859
Dan Gohmand858e902010-04-17 15:26:15 +00009860SDValue X86TargetLowering::LowerVAARG(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman320afb82010-10-12 18:00:49 +00009861 assert(Subtarget->is64Bit() &&
9862 "LowerVAARG only handles 64-bit va_arg!");
9863 assert((Subtarget->isTargetLinux() ||
9864 Subtarget->isTargetDarwin()) &&
9865 "Unhandled target in LowerVAARG");
9866 assert(Op.getNode()->getNumOperands() == 4);
9867 SDValue Chain = Op.getOperand(0);
9868 SDValue SrcPtr = Op.getOperand(1);
9869 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
9870 unsigned Align = Op.getConstantOperandVal(3);
9871 DebugLoc dl = Op.getDebugLoc();
Dan Gohman9018e832008-05-10 01:26:14 +00009872
Dan Gohman320afb82010-10-12 18:00:49 +00009873 EVT ArgVT = Op.getNode()->getValueType(0);
Chris Lattnerdb125cf2011-07-18 04:54:35 +00009874 Type *ArgTy = ArgVT.getTypeForEVT(*DAG.getContext());
Micah Villmow3574eca2012-10-08 16:38:25 +00009875 uint32_t ArgSize = getDataLayout()->getTypeAllocSize(ArgTy);
Dan Gohman320afb82010-10-12 18:00:49 +00009876 uint8_t ArgMode;
9877
9878 // Decide which area this value should be read from.
9879 // TODO: Implement the AMD64 ABI in its entirety. This simple
9880 // selection mechanism works only for the basic types.
9881 if (ArgVT == MVT::f80) {
9882 llvm_unreachable("va_arg for f80 not yet implemented");
9883 } else if (ArgVT.isFloatingPoint() && ArgSize <= 16 /*bytes*/) {
9884 ArgMode = 2; // Argument passed in XMM register. Use fp_offset.
9885 } else if (ArgVT.isInteger() && ArgSize <= 32 /*bytes*/) {
9886 ArgMode = 1; // Argument passed in GPR64 register(s). Use gp_offset.
9887 } else {
9888 llvm_unreachable("Unhandled argument type in LowerVAARG");
9889 }
9890
9891 if (ArgMode == 2) {
9892 // Sanity Check: Make sure using fp_offset makes sense.
Nick Lewycky8a8d4792011-12-02 22:16:29 +00009893 assert(!getTargetMachine().Options.UseSoftFloat &&
Eric Christopher52b45052010-10-12 19:44:17 +00009894 !(DAG.getMachineFunction()
Bill Wendling67658342012-10-09 07:45:08 +00009895 .getFunction()->getFnAttributes()
9896 .hasAttribute(Attributes::NoImplicitFloat)) &&
Craig Topper1accb7e2012-01-10 06:54:16 +00009897 Subtarget->hasSSE1());
Dan Gohman320afb82010-10-12 18:00:49 +00009898 }
9899
9900 // Insert VAARG_64 node into the DAG
9901 // VAARG_64 returns two values: Variable Argument Address, Chain
9902 SmallVector<SDValue, 11> InstOps;
9903 InstOps.push_back(Chain);
9904 InstOps.push_back(SrcPtr);
9905 InstOps.push_back(DAG.getConstant(ArgSize, MVT::i32));
9906 InstOps.push_back(DAG.getConstant(ArgMode, MVT::i8));
9907 InstOps.push_back(DAG.getConstant(Align, MVT::i32));
9908 SDVTList VTs = DAG.getVTList(getPointerTy(), MVT::Other);
9909 SDValue VAARG = DAG.getMemIntrinsicNode(X86ISD::VAARG_64, dl,
9910 VTs, &InstOps[0], InstOps.size(),
9911 MVT::i64,
9912 MachinePointerInfo(SV),
9913 /*Align=*/0,
9914 /*Volatile=*/false,
9915 /*ReadMem=*/true,
9916 /*WriteMem=*/true);
9917 Chain = VAARG.getValue(1);
9918
9919 // Load the next argument and return it
9920 return DAG.getLoad(ArgVT, dl,
9921 Chain,
9922 VAARG,
9923 MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00009924 false, false, false, 0);
Dan Gohman9018e832008-05-10 01:26:14 +00009925}
9926
Craig Topper55b24052012-09-11 06:15:32 +00009927static SDValue LowerVACOPY(SDValue Op, const X86Subtarget *Subtarget,
9928 SelectionDAG &DAG) {
Evan Chengae642192007-03-02 23:16:35 +00009929 // X86-64 va_list is a struct { i32, i32, i8*, i8* }.
Dan Gohman28269132008-04-18 20:55:41 +00009930 assert(Subtarget->is64Bit() && "This code only handles 64-bit va_copy!");
Dan Gohman475871a2008-07-27 21:46:04 +00009931 SDValue Chain = Op.getOperand(0);
9932 SDValue DstPtr = Op.getOperand(1);
9933 SDValue SrcPtr = Op.getOperand(2);
Dan Gohman69de1932008-02-06 22:27:42 +00009934 const Value *DstSV = cast<SrcValueSDNode>(Op.getOperand(3))->getValue();
9935 const Value *SrcSV = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Chris Lattnere72f2022010-09-21 05:40:29 +00009936 DebugLoc DL = Op.getDebugLoc();
Evan Chengae642192007-03-02 23:16:35 +00009937
Chris Lattnere72f2022010-09-21 05:40:29 +00009938 return DAG.getMemcpy(Chain, DL, DstPtr, SrcPtr,
Mon P Wang20adc9d2010-04-04 03:10:48 +00009939 DAG.getIntPtrConstant(24), 8, /*isVolatile*/false,
Michael J. Spencerec38de22010-10-10 22:04:20 +00009940 false,
Chris Lattnere72f2022010-09-21 05:40:29 +00009941 MachinePointerInfo(DstSV), MachinePointerInfo(SrcSV));
Evan Chengae642192007-03-02 23:16:35 +00009942}
9943
Craig Topper80e46362012-01-23 06:16:53 +00009944// getTargetVShiftNOde - Handle vector element shifts where the shift amount
9945// may or may not be a constant. Takes immediate version of shift as input.
9946static SDValue getTargetVShiftNode(unsigned Opc, DebugLoc dl, EVT VT,
9947 SDValue SrcOp, SDValue ShAmt,
9948 SelectionDAG &DAG) {
9949 assert(ShAmt.getValueType() == MVT::i32 && "ShAmt is not i32");
9950
9951 if (isa<ConstantSDNode>(ShAmt)) {
Nadav Rotemd896e242012-07-15 20:27:43 +00009952 // Constant may be a TargetConstant. Use a regular constant.
9953 uint32_t ShiftAmt = cast<ConstantSDNode>(ShAmt)->getZExtValue();
Craig Topper80e46362012-01-23 06:16:53 +00009954 switch (Opc) {
9955 default: llvm_unreachable("Unknown target vector shift node");
9956 case X86ISD::VSHLI:
9957 case X86ISD::VSRLI:
9958 case X86ISD::VSRAI:
Nadav Rotemd896e242012-07-15 20:27:43 +00009959 return DAG.getNode(Opc, dl, VT, SrcOp,
9960 DAG.getConstant(ShiftAmt, MVT::i32));
Craig Topper80e46362012-01-23 06:16:53 +00009961 }
9962 }
9963
9964 // Change opcode to non-immediate version
9965 switch (Opc) {
9966 default: llvm_unreachable("Unknown target vector shift node");
9967 case X86ISD::VSHLI: Opc = X86ISD::VSHL; break;
9968 case X86ISD::VSRLI: Opc = X86ISD::VSRL; break;
9969 case X86ISD::VSRAI: Opc = X86ISD::VSRA; break;
9970 }
9971
9972 // Need to build a vector containing shift amount
9973 // Shift amount is 32-bits, but SSE instructions read 64-bit, so fill with 0
9974 SDValue ShOps[4];
9975 ShOps[0] = ShAmt;
9976 ShOps[1] = DAG.getConstant(0, MVT::i32);
Craig Topper6d688152012-08-14 07:43:25 +00009977 ShOps[2] = ShOps[3] = DAG.getUNDEF(MVT::i32);
Craig Topper80e46362012-01-23 06:16:53 +00009978 ShAmt = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, &ShOps[0], 4);
Nadav Rotem65f489f2012-07-14 22:26:05 +00009979
9980 // The return type has to be a 128-bit type with the same element
9981 // type as the input type.
9982 MVT EltVT = VT.getVectorElementType().getSimpleVT();
9983 EVT ShVT = MVT::getVectorVT(EltVT, 128/EltVT.getSizeInBits());
9984
9985 ShAmt = DAG.getNode(ISD::BITCAST, dl, ShVT, ShAmt);
Craig Topper80e46362012-01-23 06:16:53 +00009986 return DAG.getNode(Opc, dl, VT, SrcOp, ShAmt);
9987}
9988
Craig Topper55b24052012-09-11 06:15:32 +00009989static SDValue LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG) {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009990 DebugLoc dl = Op.getDebugLoc();
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00009991 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00009992 switch (IntNo) {
Dan Gohman475871a2008-07-27 21:46:04 +00009993 default: return SDValue(); // Don't custom lower most intrinsics.
Evan Cheng5759f972008-05-04 09:15:50 +00009994 // Comparison intrinsics.
Evan Cheng0db9fe62006-04-25 20:13:52 +00009995 case Intrinsic::x86_sse_comieq_ss:
9996 case Intrinsic::x86_sse_comilt_ss:
9997 case Intrinsic::x86_sse_comile_ss:
9998 case Intrinsic::x86_sse_comigt_ss:
9999 case Intrinsic::x86_sse_comige_ss:
10000 case Intrinsic::x86_sse_comineq_ss:
10001 case Intrinsic::x86_sse_ucomieq_ss:
10002 case Intrinsic::x86_sse_ucomilt_ss:
10003 case Intrinsic::x86_sse_ucomile_ss:
10004 case Intrinsic::x86_sse_ucomigt_ss:
10005 case Intrinsic::x86_sse_ucomige_ss:
10006 case Intrinsic::x86_sse_ucomineq_ss:
10007 case Intrinsic::x86_sse2_comieq_sd:
10008 case Intrinsic::x86_sse2_comilt_sd:
10009 case Intrinsic::x86_sse2_comile_sd:
10010 case Intrinsic::x86_sse2_comigt_sd:
10011 case Intrinsic::x86_sse2_comige_sd:
10012 case Intrinsic::x86_sse2_comineq_sd:
10013 case Intrinsic::x86_sse2_ucomieq_sd:
10014 case Intrinsic::x86_sse2_ucomilt_sd:
10015 case Intrinsic::x86_sse2_ucomile_sd:
10016 case Intrinsic::x86_sse2_ucomigt_sd:
10017 case Intrinsic::x86_sse2_ucomige_sd:
10018 case Intrinsic::x86_sse2_ucomineq_sd: {
Craig Topper6d688152012-08-14 07:43:25 +000010019 unsigned Opc;
10020 ISD::CondCode CC;
Evan Cheng0db9fe62006-04-25 20:13:52 +000010021 switch (IntNo) {
Craig Topper86c7c582012-01-30 01:10:15 +000010022 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000010023 case Intrinsic::x86_sse_comieq_ss:
10024 case Intrinsic::x86_sse2_comieq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +000010025 Opc = X86ISD::COMI;
10026 CC = ISD::SETEQ;
10027 break;
Evan Cheng6be2c582006-04-05 23:38:46 +000010028 case Intrinsic::x86_sse_comilt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +000010029 case Intrinsic::x86_sse2_comilt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +000010030 Opc = X86ISD::COMI;
10031 CC = ISD::SETLT;
10032 break;
10033 case Intrinsic::x86_sse_comile_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +000010034 case Intrinsic::x86_sse2_comile_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +000010035 Opc = X86ISD::COMI;
10036 CC = ISD::SETLE;
10037 break;
10038 case Intrinsic::x86_sse_comigt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +000010039 case Intrinsic::x86_sse2_comigt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +000010040 Opc = X86ISD::COMI;
10041 CC = ISD::SETGT;
10042 break;
10043 case Intrinsic::x86_sse_comige_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +000010044 case Intrinsic::x86_sse2_comige_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +000010045 Opc = X86ISD::COMI;
10046 CC = ISD::SETGE;
10047 break;
10048 case Intrinsic::x86_sse_comineq_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +000010049 case Intrinsic::x86_sse2_comineq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +000010050 Opc = X86ISD::COMI;
10051 CC = ISD::SETNE;
10052 break;
10053 case Intrinsic::x86_sse_ucomieq_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +000010054 case Intrinsic::x86_sse2_ucomieq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +000010055 Opc = X86ISD::UCOMI;
10056 CC = ISD::SETEQ;
10057 break;
10058 case Intrinsic::x86_sse_ucomilt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +000010059 case Intrinsic::x86_sse2_ucomilt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +000010060 Opc = X86ISD::UCOMI;
10061 CC = ISD::SETLT;
10062 break;
10063 case Intrinsic::x86_sse_ucomile_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +000010064 case Intrinsic::x86_sse2_ucomile_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +000010065 Opc = X86ISD::UCOMI;
10066 CC = ISD::SETLE;
10067 break;
10068 case Intrinsic::x86_sse_ucomigt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +000010069 case Intrinsic::x86_sse2_ucomigt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +000010070 Opc = X86ISD::UCOMI;
10071 CC = ISD::SETGT;
10072 break;
10073 case Intrinsic::x86_sse_ucomige_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +000010074 case Intrinsic::x86_sse2_ucomige_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +000010075 Opc = X86ISD::UCOMI;
10076 CC = ISD::SETGE;
10077 break;
10078 case Intrinsic::x86_sse_ucomineq_ss:
10079 case Intrinsic::x86_sse2_ucomineq_sd:
10080 Opc = X86ISD::UCOMI;
10081 CC = ISD::SETNE;
10082 break;
Evan Cheng6be2c582006-04-05 23:38:46 +000010083 }
Evan Cheng734503b2006-09-11 02:19:56 +000010084
Dan Gohman475871a2008-07-27 21:46:04 +000010085 SDValue LHS = Op.getOperand(1);
10086 SDValue RHS = Op.getOperand(2);
Chris Lattner1c39d4c2008-12-24 23:53:05 +000010087 unsigned X86CC = TranslateX86CC(CC, true, LHS, RHS, DAG);
Dan Gohman1a492952009-10-20 16:22:37 +000010088 assert(X86CC != X86::COND_INVALID && "Unexpected illegal condition!");
Owen Anderson825b72b2009-08-11 20:47:22 +000010089 SDValue Cond = DAG.getNode(Opc, dl, MVT::i32, LHS, RHS);
10090 SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
10091 DAG.getConstant(X86CC, MVT::i8), Cond);
10092 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC);
Evan Cheng6be2c582006-04-05 23:38:46 +000010093 }
Craig Topper6d688152012-08-14 07:43:25 +000010094
Duncan Sands04aa4ae2011-09-23 16:10:22 +000010095 // Arithmetic intrinsics.
Craig Topper5b209e82012-02-05 03:14:49 +000010096 case Intrinsic::x86_sse2_pmulu_dq:
10097 case Intrinsic::x86_avx2_pmulu_dq:
10098 return DAG.getNode(X86ISD::PMULUDQ, dl, Op.getValueType(),
10099 Op.getOperand(1), Op.getOperand(2));
Craig Topper6d688152012-08-14 07:43:25 +000010100
10101 // SSE3/AVX horizontal add/sub intrinsics
Duncan Sands04aa4ae2011-09-23 16:10:22 +000010102 case Intrinsic::x86_sse3_hadd_ps:
10103 case Intrinsic::x86_sse3_hadd_pd:
10104 case Intrinsic::x86_avx_hadd_ps_256:
10105 case Intrinsic::x86_avx_hadd_pd_256:
Duncan Sands04aa4ae2011-09-23 16:10:22 +000010106 case Intrinsic::x86_sse3_hsub_ps:
10107 case Intrinsic::x86_sse3_hsub_pd:
10108 case Intrinsic::x86_avx_hsub_ps_256:
10109 case Intrinsic::x86_avx_hsub_pd_256:
Craig Topper4bb3f342012-01-25 05:37:32 +000010110 case Intrinsic::x86_ssse3_phadd_w_128:
10111 case Intrinsic::x86_ssse3_phadd_d_128:
10112 case Intrinsic::x86_avx2_phadd_w:
10113 case Intrinsic::x86_avx2_phadd_d:
Craig Topper4bb3f342012-01-25 05:37:32 +000010114 case Intrinsic::x86_ssse3_phsub_w_128:
10115 case Intrinsic::x86_ssse3_phsub_d_128:
10116 case Intrinsic::x86_avx2_phsub_w:
Craig Topper6d688152012-08-14 07:43:25 +000010117 case Intrinsic::x86_avx2_phsub_d: {
10118 unsigned Opcode;
10119 switch (IntNo) {
10120 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
10121 case Intrinsic::x86_sse3_hadd_ps:
10122 case Intrinsic::x86_sse3_hadd_pd:
10123 case Intrinsic::x86_avx_hadd_ps_256:
10124 case Intrinsic::x86_avx_hadd_pd_256:
10125 Opcode = X86ISD::FHADD;
10126 break;
10127 case Intrinsic::x86_sse3_hsub_ps:
10128 case Intrinsic::x86_sse3_hsub_pd:
10129 case Intrinsic::x86_avx_hsub_ps_256:
10130 case Intrinsic::x86_avx_hsub_pd_256:
10131 Opcode = X86ISD::FHSUB;
10132 break;
10133 case Intrinsic::x86_ssse3_phadd_w_128:
10134 case Intrinsic::x86_ssse3_phadd_d_128:
10135 case Intrinsic::x86_avx2_phadd_w:
10136 case Intrinsic::x86_avx2_phadd_d:
10137 Opcode = X86ISD::HADD;
10138 break;
10139 case Intrinsic::x86_ssse3_phsub_w_128:
10140 case Intrinsic::x86_ssse3_phsub_d_128:
10141 case Intrinsic::x86_avx2_phsub_w:
10142 case Intrinsic::x86_avx2_phsub_d:
10143 Opcode = X86ISD::HSUB;
10144 break;
10145 }
10146 return DAG.getNode(Opcode, dl, Op.getValueType(),
Craig Topper4bb3f342012-01-25 05:37:32 +000010147 Op.getOperand(1), Op.getOperand(2));
Craig Topper6d688152012-08-14 07:43:25 +000010148 }
10149
10150 // AVX2 variable shift intrinsics
Craig Topper98fc7292011-11-19 17:46:46 +000010151 case Intrinsic::x86_avx2_psllv_d:
10152 case Intrinsic::x86_avx2_psllv_q:
10153 case Intrinsic::x86_avx2_psllv_d_256:
10154 case Intrinsic::x86_avx2_psllv_q_256:
Craig Topper98fc7292011-11-19 17:46:46 +000010155 case Intrinsic::x86_avx2_psrlv_d:
10156 case Intrinsic::x86_avx2_psrlv_q:
10157 case Intrinsic::x86_avx2_psrlv_d_256:
10158 case Intrinsic::x86_avx2_psrlv_q_256:
Craig Topper98fc7292011-11-19 17:46:46 +000010159 case Intrinsic::x86_avx2_psrav_d:
Craig Topper6d688152012-08-14 07:43:25 +000010160 case Intrinsic::x86_avx2_psrav_d_256: {
10161 unsigned Opcode;
10162 switch (IntNo) {
10163 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
10164 case Intrinsic::x86_avx2_psllv_d:
10165 case Intrinsic::x86_avx2_psllv_q:
10166 case Intrinsic::x86_avx2_psllv_d_256:
10167 case Intrinsic::x86_avx2_psllv_q_256:
10168 Opcode = ISD::SHL;
10169 break;
10170 case Intrinsic::x86_avx2_psrlv_d:
10171 case Intrinsic::x86_avx2_psrlv_q:
10172 case Intrinsic::x86_avx2_psrlv_d_256:
10173 case Intrinsic::x86_avx2_psrlv_q_256:
10174 Opcode = ISD::SRL;
10175 break;
10176 case Intrinsic::x86_avx2_psrav_d:
10177 case Intrinsic::x86_avx2_psrav_d_256:
10178 Opcode = ISD::SRA;
10179 break;
10180 }
10181 return DAG.getNode(Opcode, dl, Op.getValueType(),
10182 Op.getOperand(1), Op.getOperand(2));
10183 }
10184
Craig Topper969ba282012-01-25 06:43:11 +000010185 case Intrinsic::x86_ssse3_pshuf_b_128:
10186 case Intrinsic::x86_avx2_pshuf_b:
10187 return DAG.getNode(X86ISD::PSHUFB, dl, Op.getValueType(),
10188 Op.getOperand(1), Op.getOperand(2));
Craig Topper6d688152012-08-14 07:43:25 +000010189
Craig Topper969ba282012-01-25 06:43:11 +000010190 case Intrinsic::x86_ssse3_psign_b_128:
10191 case Intrinsic::x86_ssse3_psign_w_128:
10192 case Intrinsic::x86_ssse3_psign_d_128:
10193 case Intrinsic::x86_avx2_psign_b:
10194 case Intrinsic::x86_avx2_psign_w:
10195 case Intrinsic::x86_avx2_psign_d:
10196 return DAG.getNode(X86ISD::PSIGN, dl, Op.getValueType(),
10197 Op.getOperand(1), Op.getOperand(2));
Craig Topper6d688152012-08-14 07:43:25 +000010198
Craig Toppere566cd02012-01-26 07:18:03 +000010199 case Intrinsic::x86_sse41_insertps:
10200 return DAG.getNode(X86ISD::INSERTPS, dl, Op.getValueType(),
10201 Op.getOperand(1), Op.getOperand(2), Op.getOperand(3));
Craig Topper6d688152012-08-14 07:43:25 +000010202
Craig Toppere566cd02012-01-26 07:18:03 +000010203 case Intrinsic::x86_avx_vperm2f128_ps_256:
10204 case Intrinsic::x86_avx_vperm2f128_pd_256:
10205 case Intrinsic::x86_avx_vperm2f128_si_256:
10206 case Intrinsic::x86_avx2_vperm2i128:
10207 return DAG.getNode(X86ISD::VPERM2X128, dl, Op.getValueType(),
10208 Op.getOperand(1), Op.getOperand(2), Op.getOperand(3));
Craig Topper6d688152012-08-14 07:43:25 +000010209
Craig Topperffa6c402012-04-16 07:13:00 +000010210 case Intrinsic::x86_avx2_permd:
10211 case Intrinsic::x86_avx2_permps:
10212 // Operands intentionally swapped. Mask is last operand to intrinsic,
10213 // but second operand for node/intruction.
10214 return DAG.getNode(X86ISD::VPERMV, dl, Op.getValueType(),
10215 Op.getOperand(2), Op.getOperand(1));
Craig Topper98fc7292011-11-19 17:46:46 +000010216
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +000010217 // ptest and testp intrinsics. The intrinsic these come from are designed to
10218 // return an integer value, not just an instruction so lower it to the ptest
10219 // or testp pattern and a setcc for the result.
Eric Christopher71c67532009-07-29 00:28:05 +000010220 case Intrinsic::x86_sse41_ptestz:
10221 case Intrinsic::x86_sse41_ptestc:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +000010222 case Intrinsic::x86_sse41_ptestnzc:
10223 case Intrinsic::x86_avx_ptestz_256:
10224 case Intrinsic::x86_avx_ptestc_256:
10225 case Intrinsic::x86_avx_ptestnzc_256:
10226 case Intrinsic::x86_avx_vtestz_ps:
10227 case Intrinsic::x86_avx_vtestc_ps:
10228 case Intrinsic::x86_avx_vtestnzc_ps:
10229 case Intrinsic::x86_avx_vtestz_pd:
10230 case Intrinsic::x86_avx_vtestc_pd:
10231 case Intrinsic::x86_avx_vtestnzc_pd:
10232 case Intrinsic::x86_avx_vtestz_ps_256:
10233 case Intrinsic::x86_avx_vtestc_ps_256:
10234 case Intrinsic::x86_avx_vtestnzc_ps_256:
10235 case Intrinsic::x86_avx_vtestz_pd_256:
10236 case Intrinsic::x86_avx_vtestc_pd_256:
10237 case Intrinsic::x86_avx_vtestnzc_pd_256: {
10238 bool IsTestPacked = false;
Craig Topper6d688152012-08-14 07:43:25 +000010239 unsigned X86CC;
Eric Christopher71c67532009-07-29 00:28:05 +000010240 switch (IntNo) {
Eric Christopher978dae32009-07-29 18:14:04 +000010241 default: llvm_unreachable("Bad fallthrough in Intrinsic lowering.");
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +000010242 case Intrinsic::x86_avx_vtestz_ps:
10243 case Intrinsic::x86_avx_vtestz_pd:
10244 case Intrinsic::x86_avx_vtestz_ps_256:
10245 case Intrinsic::x86_avx_vtestz_pd_256:
10246 IsTestPacked = true; // Fallthrough
Eric Christopher71c67532009-07-29 00:28:05 +000010247 case Intrinsic::x86_sse41_ptestz:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +000010248 case Intrinsic::x86_avx_ptestz_256:
Eric Christopher71c67532009-07-29 00:28:05 +000010249 // ZF = 1
10250 X86CC = X86::COND_E;
10251 break;
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +000010252 case Intrinsic::x86_avx_vtestc_ps:
10253 case Intrinsic::x86_avx_vtestc_pd:
10254 case Intrinsic::x86_avx_vtestc_ps_256:
10255 case Intrinsic::x86_avx_vtestc_pd_256:
10256 IsTestPacked = true; // Fallthrough
Eric Christopher71c67532009-07-29 00:28:05 +000010257 case Intrinsic::x86_sse41_ptestc:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +000010258 case Intrinsic::x86_avx_ptestc_256:
Eric Christopher71c67532009-07-29 00:28:05 +000010259 // CF = 1
10260 X86CC = X86::COND_B;
10261 break;
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +000010262 case Intrinsic::x86_avx_vtestnzc_ps:
10263 case Intrinsic::x86_avx_vtestnzc_pd:
10264 case Intrinsic::x86_avx_vtestnzc_ps_256:
10265 case Intrinsic::x86_avx_vtestnzc_pd_256:
10266 IsTestPacked = true; // Fallthrough
Eric Christopherfd179292009-08-27 18:07:15 +000010267 case Intrinsic::x86_sse41_ptestnzc:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +000010268 case Intrinsic::x86_avx_ptestnzc_256:
Eric Christopher71c67532009-07-29 00:28:05 +000010269 // ZF and CF = 0
10270 X86CC = X86::COND_A;
10271 break;
10272 }
Eric Christopherfd179292009-08-27 18:07:15 +000010273
Eric Christopher71c67532009-07-29 00:28:05 +000010274 SDValue LHS = Op.getOperand(1);
10275 SDValue RHS = Op.getOperand(2);
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +000010276 unsigned TestOpc = IsTestPacked ? X86ISD::TESTP : X86ISD::PTEST;
10277 SDValue Test = DAG.getNode(TestOpc, dl, MVT::i32, LHS, RHS);
Owen Anderson825b72b2009-08-11 20:47:22 +000010278 SDValue CC = DAG.getConstant(X86CC, MVT::i8);
10279 SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8, CC, Test);
10280 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC);
Eric Christopher71c67532009-07-29 00:28:05 +000010281 }
Evan Cheng5759f972008-05-04 09:15:50 +000010282
Craig Topper80e46362012-01-23 06:16:53 +000010283 // SSE/AVX shift intrinsics
10284 case Intrinsic::x86_sse2_psll_w:
10285 case Intrinsic::x86_sse2_psll_d:
10286 case Intrinsic::x86_sse2_psll_q:
10287 case Intrinsic::x86_avx2_psll_w:
10288 case Intrinsic::x86_avx2_psll_d:
10289 case Intrinsic::x86_avx2_psll_q:
Craig Topper80e46362012-01-23 06:16:53 +000010290 case Intrinsic::x86_sse2_psrl_w:
10291 case Intrinsic::x86_sse2_psrl_d:
10292 case Intrinsic::x86_sse2_psrl_q:
10293 case Intrinsic::x86_avx2_psrl_w:
10294 case Intrinsic::x86_avx2_psrl_d:
10295 case Intrinsic::x86_avx2_psrl_q:
Craig Topper80e46362012-01-23 06:16:53 +000010296 case Intrinsic::x86_sse2_psra_w:
10297 case Intrinsic::x86_sse2_psra_d:
10298 case Intrinsic::x86_avx2_psra_w:
Craig Topper6d688152012-08-14 07:43:25 +000010299 case Intrinsic::x86_avx2_psra_d: {
10300 unsigned Opcode;
10301 switch (IntNo) {
10302 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
10303 case Intrinsic::x86_sse2_psll_w:
10304 case Intrinsic::x86_sse2_psll_d:
10305 case Intrinsic::x86_sse2_psll_q:
10306 case Intrinsic::x86_avx2_psll_w:
10307 case Intrinsic::x86_avx2_psll_d:
10308 case Intrinsic::x86_avx2_psll_q:
10309 Opcode = X86ISD::VSHL;
10310 break;
10311 case Intrinsic::x86_sse2_psrl_w:
10312 case Intrinsic::x86_sse2_psrl_d:
10313 case Intrinsic::x86_sse2_psrl_q:
10314 case Intrinsic::x86_avx2_psrl_w:
10315 case Intrinsic::x86_avx2_psrl_d:
10316 case Intrinsic::x86_avx2_psrl_q:
10317 Opcode = X86ISD::VSRL;
10318 break;
10319 case Intrinsic::x86_sse2_psra_w:
10320 case Intrinsic::x86_sse2_psra_d:
10321 case Intrinsic::x86_avx2_psra_w:
10322 case Intrinsic::x86_avx2_psra_d:
10323 Opcode = X86ISD::VSRA;
10324 break;
10325 }
10326 return DAG.getNode(Opcode, dl, Op.getValueType(),
Craig Topper80e46362012-01-23 06:16:53 +000010327 Op.getOperand(1), Op.getOperand(2));
Craig Topper6d688152012-08-14 07:43:25 +000010328 }
10329
10330 // SSE/AVX immediate shift intrinsics
Evan Cheng5759f972008-05-04 09:15:50 +000010331 case Intrinsic::x86_sse2_pslli_w:
10332 case Intrinsic::x86_sse2_pslli_d:
10333 case Intrinsic::x86_sse2_pslli_q:
Craig Topper80e46362012-01-23 06:16:53 +000010334 case Intrinsic::x86_avx2_pslli_w:
10335 case Intrinsic::x86_avx2_pslli_d:
10336 case Intrinsic::x86_avx2_pslli_q:
Evan Cheng5759f972008-05-04 09:15:50 +000010337 case Intrinsic::x86_sse2_psrli_w:
10338 case Intrinsic::x86_sse2_psrli_d:
10339 case Intrinsic::x86_sse2_psrli_q:
Craig Topper80e46362012-01-23 06:16:53 +000010340 case Intrinsic::x86_avx2_psrli_w:
10341 case Intrinsic::x86_avx2_psrli_d:
10342 case Intrinsic::x86_avx2_psrli_q:
Evan Cheng5759f972008-05-04 09:15:50 +000010343 case Intrinsic::x86_sse2_psrai_w:
10344 case Intrinsic::x86_sse2_psrai_d:
Craig Topper80e46362012-01-23 06:16:53 +000010345 case Intrinsic::x86_avx2_psrai_w:
Craig Topper6d688152012-08-14 07:43:25 +000010346 case Intrinsic::x86_avx2_psrai_d: {
10347 unsigned Opcode;
10348 switch (IntNo) {
10349 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
10350 case Intrinsic::x86_sse2_pslli_w:
10351 case Intrinsic::x86_sse2_pslli_d:
10352 case Intrinsic::x86_sse2_pslli_q:
10353 case Intrinsic::x86_avx2_pslli_w:
10354 case Intrinsic::x86_avx2_pslli_d:
10355 case Intrinsic::x86_avx2_pslli_q:
10356 Opcode = X86ISD::VSHLI;
10357 break;
10358 case Intrinsic::x86_sse2_psrli_w:
10359 case Intrinsic::x86_sse2_psrli_d:
10360 case Intrinsic::x86_sse2_psrli_q:
10361 case Intrinsic::x86_avx2_psrli_w:
10362 case Intrinsic::x86_avx2_psrli_d:
10363 case Intrinsic::x86_avx2_psrli_q:
10364 Opcode = X86ISD::VSRLI;
10365 break;
10366 case Intrinsic::x86_sse2_psrai_w:
10367 case Intrinsic::x86_sse2_psrai_d:
10368 case Intrinsic::x86_avx2_psrai_w:
10369 case Intrinsic::x86_avx2_psrai_d:
10370 Opcode = X86ISD::VSRAI;
10371 break;
10372 }
10373 return getTargetVShiftNode(Opcode, dl, Op.getValueType(),
Craig Topper80e46362012-01-23 06:16:53 +000010374 Op.getOperand(1), Op.getOperand(2), DAG);
Craig Topper6d688152012-08-14 07:43:25 +000010375 }
10376
Craig Topper4feb6472012-08-06 06:22:36 +000010377 case Intrinsic::x86_sse42_pcmpistria128:
10378 case Intrinsic::x86_sse42_pcmpestria128:
10379 case Intrinsic::x86_sse42_pcmpistric128:
10380 case Intrinsic::x86_sse42_pcmpestric128:
10381 case Intrinsic::x86_sse42_pcmpistrio128:
10382 case Intrinsic::x86_sse42_pcmpestrio128:
10383 case Intrinsic::x86_sse42_pcmpistris128:
10384 case Intrinsic::x86_sse42_pcmpestris128:
10385 case Intrinsic::x86_sse42_pcmpistriz128:
10386 case Intrinsic::x86_sse42_pcmpestriz128: {
10387 unsigned Opcode;
10388 unsigned X86CC;
10389 switch (IntNo) {
10390 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
10391 case Intrinsic::x86_sse42_pcmpistria128:
10392 Opcode = X86ISD::PCMPISTRI;
10393 X86CC = X86::COND_A;
10394 break;
10395 case Intrinsic::x86_sse42_pcmpestria128:
10396 Opcode = X86ISD::PCMPESTRI;
10397 X86CC = X86::COND_A;
10398 break;
10399 case Intrinsic::x86_sse42_pcmpistric128:
10400 Opcode = X86ISD::PCMPISTRI;
10401 X86CC = X86::COND_B;
10402 break;
10403 case Intrinsic::x86_sse42_pcmpestric128:
10404 Opcode = X86ISD::PCMPESTRI;
10405 X86CC = X86::COND_B;
10406 break;
10407 case Intrinsic::x86_sse42_pcmpistrio128:
10408 Opcode = X86ISD::PCMPISTRI;
10409 X86CC = X86::COND_O;
10410 break;
10411 case Intrinsic::x86_sse42_pcmpestrio128:
10412 Opcode = X86ISD::PCMPESTRI;
10413 X86CC = X86::COND_O;
10414 break;
10415 case Intrinsic::x86_sse42_pcmpistris128:
10416 Opcode = X86ISD::PCMPISTRI;
10417 X86CC = X86::COND_S;
10418 break;
10419 case Intrinsic::x86_sse42_pcmpestris128:
10420 Opcode = X86ISD::PCMPESTRI;
10421 X86CC = X86::COND_S;
10422 break;
10423 case Intrinsic::x86_sse42_pcmpistriz128:
10424 Opcode = X86ISD::PCMPISTRI;
10425 X86CC = X86::COND_E;
10426 break;
10427 case Intrinsic::x86_sse42_pcmpestriz128:
10428 Opcode = X86ISD::PCMPESTRI;
10429 X86CC = X86::COND_E;
10430 break;
10431 }
10432 SmallVector<SDValue, 5> NewOps;
10433 NewOps.append(Op->op_begin()+1, Op->op_end());
10434 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::i32);
10435 SDValue PCMP = DAG.getNode(Opcode, dl, VTs, NewOps.data(), NewOps.size());
10436 SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
10437 DAG.getConstant(X86CC, MVT::i8),
10438 SDValue(PCMP.getNode(), 1));
10439 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC);
10440 }
Craig Topper6d688152012-08-14 07:43:25 +000010441
Craig Topper4feb6472012-08-06 06:22:36 +000010442 case Intrinsic::x86_sse42_pcmpistri128:
10443 case Intrinsic::x86_sse42_pcmpestri128: {
10444 unsigned Opcode;
10445 if (IntNo == Intrinsic::x86_sse42_pcmpistri128)
10446 Opcode = X86ISD::PCMPISTRI;
10447 else
10448 Opcode = X86ISD::PCMPESTRI;
10449
10450 SmallVector<SDValue, 5> NewOps;
10451 NewOps.append(Op->op_begin()+1, Op->op_end());
10452 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::i32);
10453 return DAG.getNode(Opcode, dl, VTs, NewOps.data(), NewOps.size());
10454 }
Craig Topper0e292372012-08-24 04:03:22 +000010455 case Intrinsic::x86_fma_vfmadd_ps:
10456 case Intrinsic::x86_fma_vfmadd_pd:
10457 case Intrinsic::x86_fma_vfmsub_ps:
10458 case Intrinsic::x86_fma_vfmsub_pd:
10459 case Intrinsic::x86_fma_vfnmadd_ps:
10460 case Intrinsic::x86_fma_vfnmadd_pd:
10461 case Intrinsic::x86_fma_vfnmsub_ps:
10462 case Intrinsic::x86_fma_vfnmsub_pd:
10463 case Intrinsic::x86_fma_vfmaddsub_ps:
10464 case Intrinsic::x86_fma_vfmaddsub_pd:
10465 case Intrinsic::x86_fma_vfmsubadd_ps:
10466 case Intrinsic::x86_fma_vfmsubadd_pd:
10467 case Intrinsic::x86_fma_vfmadd_ps_256:
10468 case Intrinsic::x86_fma_vfmadd_pd_256:
10469 case Intrinsic::x86_fma_vfmsub_ps_256:
10470 case Intrinsic::x86_fma_vfmsub_pd_256:
10471 case Intrinsic::x86_fma_vfnmadd_ps_256:
10472 case Intrinsic::x86_fma_vfnmadd_pd_256:
10473 case Intrinsic::x86_fma_vfnmsub_ps_256:
10474 case Intrinsic::x86_fma_vfnmsub_pd_256:
10475 case Intrinsic::x86_fma_vfmaddsub_ps_256:
10476 case Intrinsic::x86_fma_vfmaddsub_pd_256:
10477 case Intrinsic::x86_fma_vfmsubadd_ps_256:
10478 case Intrinsic::x86_fma_vfmsubadd_pd_256: {
Craig Topper0e292372012-08-24 04:03:22 +000010479 unsigned Opc;
10480 switch (IntNo) {
10481 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
10482 case Intrinsic::x86_fma_vfmadd_ps:
10483 case Intrinsic::x86_fma_vfmadd_pd:
10484 case Intrinsic::x86_fma_vfmadd_ps_256:
10485 case Intrinsic::x86_fma_vfmadd_pd_256:
10486 Opc = X86ISD::FMADD;
10487 break;
10488 case Intrinsic::x86_fma_vfmsub_ps:
10489 case Intrinsic::x86_fma_vfmsub_pd:
10490 case Intrinsic::x86_fma_vfmsub_ps_256:
10491 case Intrinsic::x86_fma_vfmsub_pd_256:
10492 Opc = X86ISD::FMSUB;
10493 break;
10494 case Intrinsic::x86_fma_vfnmadd_ps:
10495 case Intrinsic::x86_fma_vfnmadd_pd:
10496 case Intrinsic::x86_fma_vfnmadd_ps_256:
10497 case Intrinsic::x86_fma_vfnmadd_pd_256:
10498 Opc = X86ISD::FNMADD;
10499 break;
10500 case Intrinsic::x86_fma_vfnmsub_ps:
10501 case Intrinsic::x86_fma_vfnmsub_pd:
10502 case Intrinsic::x86_fma_vfnmsub_ps_256:
10503 case Intrinsic::x86_fma_vfnmsub_pd_256:
10504 Opc = X86ISD::FNMSUB;
10505 break;
10506 case Intrinsic::x86_fma_vfmaddsub_ps:
10507 case Intrinsic::x86_fma_vfmaddsub_pd:
10508 case Intrinsic::x86_fma_vfmaddsub_ps_256:
10509 case Intrinsic::x86_fma_vfmaddsub_pd_256:
10510 Opc = X86ISD::FMADDSUB;
10511 break;
10512 case Intrinsic::x86_fma_vfmsubadd_ps:
10513 case Intrinsic::x86_fma_vfmsubadd_pd:
10514 case Intrinsic::x86_fma_vfmsubadd_ps_256:
10515 case Intrinsic::x86_fma_vfmsubadd_pd_256:
10516 Opc = X86ISD::FMSUBADD;
10517 break;
10518 }
10519
10520 return DAG.getNode(Opc, dl, Op.getValueType(), Op.getOperand(1),
10521 Op.getOperand(2), Op.getOperand(3));
10522 }
Evan Cheng38bcbaf2005-12-23 07:31:11 +000010523 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000010524}
Evan Cheng72261582005-12-20 06:22:03 +000010525
Craig Topper55b24052012-09-11 06:15:32 +000010526static SDValue LowerINTRINSIC_W_CHAIN(SDValue Op, SelectionDAG &DAG) {
Benjamin Kramerb9bee042012-07-12 09:31:43 +000010527 DebugLoc dl = Op.getDebugLoc();
10528 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
10529 switch (IntNo) {
10530 default: return SDValue(); // Don't custom lower most intrinsics.
10531
10532 // RDRAND intrinsics.
10533 case Intrinsic::x86_rdrand_16:
10534 case Intrinsic::x86_rdrand_32:
10535 case Intrinsic::x86_rdrand_64: {
10536 // Emit the node with the right value type.
Benjamin Kramerfeae00a2012-07-12 18:14:57 +000010537 SDVTList VTs = DAG.getVTList(Op->getValueType(0), MVT::Glue, MVT::Other);
10538 SDValue Result = DAG.getNode(X86ISD::RDRAND, dl, VTs, Op.getOperand(0));
Benjamin Kramerb9bee042012-07-12 09:31:43 +000010539
10540 // If the value returned by RDRAND was valid (CF=1), return 1. Otherwise
10541 // return the value from Rand, which is always 0, casted to i32.
10542 SDValue Ops[] = { DAG.getZExtOrTrunc(Result, dl, Op->getValueType(1)),
10543 DAG.getConstant(1, Op->getValueType(1)),
10544 DAG.getConstant(X86::COND_B, MVT::i32),
10545 SDValue(Result.getNode(), 1) };
10546 SDValue isValid = DAG.getNode(X86ISD::CMOV, dl,
10547 DAG.getVTList(Op->getValueType(1), MVT::Glue),
10548 Ops, 4);
10549
10550 // Return { result, isValid, chain }.
10551 return DAG.getNode(ISD::MERGE_VALUES, dl, Op->getVTList(), Result, isValid,
Benjamin Kramerfeae00a2012-07-12 18:14:57 +000010552 SDValue(Result.getNode(), 2));
Benjamin Kramerb9bee042012-07-12 09:31:43 +000010553 }
10554 }
10555}
10556
Dan Gohmand858e902010-04-17 15:26:15 +000010557SDValue X86TargetLowering::LowerRETURNADDR(SDValue Op,
10558 SelectionDAG &DAG) const {
Evan Cheng2457f2c2010-05-22 01:47:14 +000010559 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
10560 MFI->setReturnAddressIsTaken(true);
10561
Bill Wendling64e87322009-01-16 19:25:27 +000010562 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Dale Johannesen6f38cb62009-02-07 19:59:05 +000010563 DebugLoc dl = Op.getDebugLoc();
Michael Liaoaa3c2c02012-10-25 06:29:14 +000010564 EVT PtrVT = getPointerTy();
Bill Wendling64e87322009-01-16 19:25:27 +000010565
10566 if (Depth > 0) {
10567 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
10568 SDValue Offset =
Michael Liaoaa3c2c02012-10-25 06:29:14 +000010569 DAG.getConstant(RegInfo->getSlotSize(), PtrVT);
10570 return DAG.getLoad(PtrVT, dl, DAG.getEntryNode(),
10571 DAG.getNode(ISD::ADD, dl, PtrVT,
Dale Johannesene4d209d2009-02-03 20:21:25 +000010572 FrameAddr, Offset),
Pete Cooperd752e0f2011-11-08 18:42:53 +000010573 MachinePointerInfo(), false, false, false, 0);
Bill Wendling64e87322009-01-16 19:25:27 +000010574 }
10575
10576 // Just load the return address.
Dan Gohman475871a2008-07-27 21:46:04 +000010577 SDValue RetAddrFI = getReturnAddressFrameIndex(DAG);
Michael Liaoaa3c2c02012-10-25 06:29:14 +000010578 return DAG.getLoad(PtrVT, dl, DAG.getEntryNode(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000010579 RetAddrFI, MachinePointerInfo(), false, false, false, 0);
Nate Begemanbcc5f362007-01-29 22:58:52 +000010580}
10581
Dan Gohmand858e902010-04-17 15:26:15 +000010582SDValue X86TargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng184793f2008-09-27 01:56:22 +000010583 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
10584 MFI->setFrameAddressIsTaken(true);
Evan Cheng2457f2c2010-05-22 01:47:14 +000010585
Owen Andersone50ed302009-08-10 22:56:29 +000010586 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +000010587 DebugLoc dl = Op.getDebugLoc(); // FIXME probably not meaningful
Evan Cheng184793f2008-09-27 01:56:22 +000010588 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
10589 unsigned FrameReg = Subtarget->is64Bit() ? X86::RBP : X86::EBP;
Dale Johannesendd64c412009-02-04 00:33:20 +000010590 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, VT);
Evan Cheng184793f2008-09-27 01:56:22 +000010591 while (Depth--)
Chris Lattner51abfe42010-09-21 06:02:19 +000010592 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr,
10593 MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000010594 false, false, false, 0);
Evan Cheng184793f2008-09-27 01:56:22 +000010595 return FrameAddr;
Nate Begemanbcc5f362007-01-29 22:58:52 +000010596}
10597
Dan Gohman475871a2008-07-27 21:46:04 +000010598SDValue X86TargetLowering::LowerFRAME_TO_ARGS_OFFSET(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +000010599 SelectionDAG &DAG) const {
Michael Liaoaa3c2c02012-10-25 06:29:14 +000010600 return DAG.getIntPtrConstant(2 * RegInfo->getSlotSize());
Anton Korobeynikov2365f512007-07-14 14:06:15 +000010601}
10602
Dan Gohmand858e902010-04-17 15:26:15 +000010603SDValue X86TargetLowering::LowerEH_RETURN(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +000010604 SDValue Chain = Op.getOperand(0);
10605 SDValue Offset = Op.getOperand(1);
10606 SDValue Handler = Op.getOperand(2);
Dale Johannesen6f38cb62009-02-07 19:59:05 +000010607 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov2365f512007-07-14 14:06:15 +000010608
Dan Gohmand8816272010-08-11 18:14:00 +000010609 SDValue Frame = DAG.getCopyFromReg(DAG.getEntryNode(), dl,
10610 Subtarget->is64Bit() ? X86::RBP : X86::EBP,
10611 getPointerTy());
Anton Korobeynikovb84c1672008-09-08 21:12:47 +000010612 unsigned StoreAddrReg = (Subtarget->is64Bit() ? X86::RCX : X86::ECX);
Anton Korobeynikov2365f512007-07-14 14:06:15 +000010613
Dan Gohmand8816272010-08-11 18:14:00 +000010614 SDValue StoreAddr = DAG.getNode(ISD::ADD, dl, getPointerTy(), Frame,
Michael Liaoaa3c2c02012-10-25 06:29:14 +000010615 DAG.getIntPtrConstant(RegInfo->getSlotSize()));
Dale Johannesene4d209d2009-02-03 20:21:25 +000010616 StoreAddr = DAG.getNode(ISD::ADD, dl, getPointerTy(), StoreAddr, Offset);
Chris Lattner8026a9d2010-09-21 17:50:43 +000010617 Chain = DAG.getStore(Chain, dl, Handler, StoreAddr, MachinePointerInfo(),
10618 false, false, 0);
Dale Johannesendd64c412009-02-04 00:33:20 +000010619 Chain = DAG.getCopyToReg(Chain, dl, StoreAddrReg, StoreAddr);
Anton Korobeynikov2365f512007-07-14 14:06:15 +000010620
Dale Johannesene4d209d2009-02-03 20:21:25 +000010621 return DAG.getNode(X86ISD::EH_RETURN, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +000010622 MVT::Other,
Anton Korobeynikovb84c1672008-09-08 21:12:47 +000010623 Chain, DAG.getRegister(StoreAddrReg, getPointerTy()));
Anton Korobeynikov2365f512007-07-14 14:06:15 +000010624}
10625
Michael Liao6c0e04c2012-10-15 22:39:43 +000010626SDValue X86TargetLowering::lowerEH_SJLJ_SETJMP(SDValue Op,
10627 SelectionDAG &DAG) const {
10628 DebugLoc DL = Op.getDebugLoc();
10629 return DAG.getNode(X86ISD::EH_SJLJ_SETJMP, DL,
10630 DAG.getVTList(MVT::i32, MVT::Other),
10631 Op.getOperand(0), Op.getOperand(1));
10632}
10633
10634SDValue X86TargetLowering::lowerEH_SJLJ_LONGJMP(SDValue Op,
10635 SelectionDAG &DAG) const {
10636 DebugLoc DL = Op.getDebugLoc();
10637 return DAG.getNode(X86ISD::EH_SJLJ_LONGJMP, DL, MVT::Other,
10638 Op.getOperand(0), Op.getOperand(1));
10639}
10640
Craig Topper55b24052012-09-11 06:15:32 +000010641static SDValue LowerADJUST_TRAMPOLINE(SDValue Op, SelectionDAG &DAG) {
Duncan Sands4a544a72011-09-06 13:37:06 +000010642 return Op.getOperand(0);
10643}
10644
10645SDValue X86TargetLowering::LowerINIT_TRAMPOLINE(SDValue Op,
10646 SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +000010647 SDValue Root = Op.getOperand(0);
10648 SDValue Trmp = Op.getOperand(1); // trampoline
10649 SDValue FPtr = Op.getOperand(2); // nested function
10650 SDValue Nest = Op.getOperand(3); // 'nest' parameter value
Dale Johannesen6f38cb62009-02-07 19:59:05 +000010651 DebugLoc dl = Op.getDebugLoc();
Duncan Sandsb116fac2007-07-27 20:02:49 +000010652
Dan Gohman69de1932008-02-06 22:27:42 +000010653 const Value *TrmpAddr = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Michael Liao7abf67a2012-10-04 19:50:43 +000010654 const TargetRegisterInfo* TRI = getTargetMachine().getRegisterInfo();
Duncan Sandsb116fac2007-07-27 20:02:49 +000010655
10656 if (Subtarget->is64Bit()) {
Dan Gohman475871a2008-07-27 21:46:04 +000010657 SDValue OutChains[6];
Duncan Sands339e14f2008-01-16 22:55:25 +000010658
10659 // Large code-model.
Chris Lattnera62fe662010-02-05 19:20:30 +000010660 const unsigned char JMP64r = 0xFF; // 64-bit jmp through register opcode.
10661 const unsigned char MOV64ri = 0xB8; // X86::MOV64ri opcode.
Duncan Sands339e14f2008-01-16 22:55:25 +000010662
Michael Liao7abf67a2012-10-04 19:50:43 +000010663 const unsigned char N86R10 = TRI->getEncodingValue(X86::R10) & 0x7;
10664 const unsigned char N86R11 = TRI->getEncodingValue(X86::R11) & 0x7;
Duncan Sands339e14f2008-01-16 22:55:25 +000010665
10666 const unsigned char REX_WB = 0x40 | 0x08 | 0x01; // REX prefix
10667
10668 // Load the pointer to the nested function into R11.
10669 unsigned OpCode = ((MOV64ri | N86R11) << 8) | REX_WB; // movabsq r11
Dan Gohman475871a2008-07-27 21:46:04 +000010670 SDValue Addr = Trmp;
Owen Anderson825b72b2009-08-11 20:47:22 +000010671 OutChains[0] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Chris Lattner8026a9d2010-09-21 17:50:43 +000010672 Addr, MachinePointerInfo(TrmpAddr),
10673 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +000010674
Owen Anderson825b72b2009-08-11 20:47:22 +000010675 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
10676 DAG.getConstant(2, MVT::i64));
Chris Lattner8026a9d2010-09-21 17:50:43 +000010677 OutChains[1] = DAG.getStore(Root, dl, FPtr, Addr,
10678 MachinePointerInfo(TrmpAddr, 2),
David Greene67c9d422010-02-15 16:53:33 +000010679 false, false, 2);
Duncan Sands339e14f2008-01-16 22:55:25 +000010680
10681 // Load the 'nest' parameter value into R10.
10682 // R10 is specified in X86CallingConv.td
10683 OpCode = ((MOV64ri | N86R10) << 8) | REX_WB; // movabsq r10
Owen Anderson825b72b2009-08-11 20:47:22 +000010684 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
10685 DAG.getConstant(10, MVT::i64));
10686 OutChains[2] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Chris Lattner8026a9d2010-09-21 17:50:43 +000010687 Addr, MachinePointerInfo(TrmpAddr, 10),
10688 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +000010689
Owen Anderson825b72b2009-08-11 20:47:22 +000010690 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
10691 DAG.getConstant(12, MVT::i64));
Chris Lattner8026a9d2010-09-21 17:50:43 +000010692 OutChains[3] = DAG.getStore(Root, dl, Nest, Addr,
10693 MachinePointerInfo(TrmpAddr, 12),
David Greene67c9d422010-02-15 16:53:33 +000010694 false, false, 2);
Duncan Sands339e14f2008-01-16 22:55:25 +000010695
10696 // Jump to the nested function.
10697 OpCode = (JMP64r << 8) | REX_WB; // jmpq *...
Owen Anderson825b72b2009-08-11 20:47:22 +000010698 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
10699 DAG.getConstant(20, MVT::i64));
10700 OutChains[4] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Chris Lattner8026a9d2010-09-21 17:50:43 +000010701 Addr, MachinePointerInfo(TrmpAddr, 20),
10702 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +000010703
10704 unsigned char ModRM = N86R11 | (4 << 3) | (3 << 6); // ...r11
Owen Anderson825b72b2009-08-11 20:47:22 +000010705 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
10706 DAG.getConstant(22, MVT::i64));
10707 OutChains[5] = DAG.getStore(Root, dl, DAG.getConstant(ModRM, MVT::i8), Addr,
Chris Lattner8026a9d2010-09-21 17:50:43 +000010708 MachinePointerInfo(TrmpAddr, 22),
10709 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +000010710
Duncan Sands4a544a72011-09-06 13:37:06 +000010711 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains, 6);
Duncan Sandsb116fac2007-07-27 20:02:49 +000010712 } else {
Dan Gohmanbbfb9c52008-01-31 01:01:48 +000010713 const Function *Func =
Duncan Sandsb116fac2007-07-27 20:02:49 +000010714 cast<Function>(cast<SrcValueSDNode>(Op.getOperand(5))->getValue());
Sandeep Patel65c3c8f2009-09-02 08:44:58 +000010715 CallingConv::ID CC = Func->getCallingConv();
Duncan Sandsee465742007-08-29 19:01:20 +000010716 unsigned NestReg;
Duncan Sandsb116fac2007-07-27 20:02:49 +000010717
10718 switch (CC) {
10719 default:
Torok Edwinc23197a2009-07-14 16:55:14 +000010720 llvm_unreachable("Unsupported calling convention");
Duncan Sandsb116fac2007-07-27 20:02:49 +000010721 case CallingConv::C:
Duncan Sandsb116fac2007-07-27 20:02:49 +000010722 case CallingConv::X86_StdCall: {
10723 // Pass 'nest' parameter in ECX.
10724 // Must be kept in sync with X86CallingConv.td
Duncan Sandsee465742007-08-29 19:01:20 +000010725 NestReg = X86::ECX;
Duncan Sandsb116fac2007-07-27 20:02:49 +000010726
10727 // Check that ECX wasn't needed by an 'inreg' parameter.
Chris Lattnerdb125cf2011-07-18 04:54:35 +000010728 FunctionType *FTy = Func->getFunctionType();
Devang Patel05988662008-09-25 21:00:45 +000010729 const AttrListPtr &Attrs = Func->getAttributes();
Duncan Sandsb116fac2007-07-27 20:02:49 +000010730
Chris Lattner58d74912008-03-12 17:45:29 +000010731 if (!Attrs.isEmpty() && !Func->isVarArg()) {
Duncan Sandsb116fac2007-07-27 20:02:49 +000010732 unsigned InRegCount = 0;
10733 unsigned Idx = 1;
10734
10735 for (FunctionType::param_iterator I = FTy->param_begin(),
10736 E = FTy->param_end(); I != E; ++I, ++Idx)
Bill Wendling67658342012-10-09 07:45:08 +000010737 if (Attrs.getParamAttributes(Idx).hasAttribute(Attributes::InReg))
Duncan Sandsb116fac2007-07-27 20:02:49 +000010738 // FIXME: should only count parameters that are lowered to integers.
Anton Korobeynikovbff66b02008-09-09 18:22:57 +000010739 InRegCount += (TD->getTypeSizeInBits(*I) + 31) / 32;
Duncan Sandsb116fac2007-07-27 20:02:49 +000010740
10741 if (InRegCount > 2) {
Eric Christopher90eb4022010-07-22 00:26:08 +000010742 report_fatal_error("Nest register in use - reduce number of inreg"
10743 " parameters!");
Duncan Sandsb116fac2007-07-27 20:02:49 +000010744 }
10745 }
10746 break;
10747 }
10748 case CallingConv::X86_FastCall:
Anton Korobeynikovded05e32010-05-16 09:08:45 +000010749 case CallingConv::X86_ThisCall:
Duncan Sandsbf53c292008-09-10 13:22:10 +000010750 case CallingConv::Fast:
Duncan Sandsb116fac2007-07-27 20:02:49 +000010751 // Pass 'nest' parameter in EAX.
10752 // Must be kept in sync with X86CallingConv.td
Duncan Sandsee465742007-08-29 19:01:20 +000010753 NestReg = X86::EAX;
Duncan Sandsb116fac2007-07-27 20:02:49 +000010754 break;
10755 }
10756
Dan Gohman475871a2008-07-27 21:46:04 +000010757 SDValue OutChains[4];
10758 SDValue Addr, Disp;
Duncan Sandsb116fac2007-07-27 20:02:49 +000010759
Owen Anderson825b72b2009-08-11 20:47:22 +000010760 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
10761 DAG.getConstant(10, MVT::i32));
10762 Disp = DAG.getNode(ISD::SUB, dl, MVT::i32, FPtr, Addr);
Duncan Sandsb116fac2007-07-27 20:02:49 +000010763
Chris Lattnera62fe662010-02-05 19:20:30 +000010764 // This is storing the opcode for MOV32ri.
10765 const unsigned char MOV32ri = 0xB8; // X86::MOV32ri's opcode byte.
Michael Liao7abf67a2012-10-04 19:50:43 +000010766 const unsigned char N86Reg = TRI->getEncodingValue(NestReg) & 0x7;
Scott Michelfdc40a02009-02-17 22:15:04 +000010767 OutChains[0] = DAG.getStore(Root, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +000010768 DAG.getConstant(MOV32ri|N86Reg, MVT::i8),
Chris Lattner8026a9d2010-09-21 17:50:43 +000010769 Trmp, MachinePointerInfo(TrmpAddr),
10770 false, false, 0);
Duncan Sandsb116fac2007-07-27 20:02:49 +000010771
Owen Anderson825b72b2009-08-11 20:47:22 +000010772 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
10773 DAG.getConstant(1, MVT::i32));
Chris Lattner8026a9d2010-09-21 17:50:43 +000010774 OutChains[1] = DAG.getStore(Root, dl, Nest, Addr,
10775 MachinePointerInfo(TrmpAddr, 1),
David Greene67c9d422010-02-15 16:53:33 +000010776 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +000010777
Chris Lattnera62fe662010-02-05 19:20:30 +000010778 const unsigned char JMP = 0xE9; // jmp <32bit dst> opcode.
Owen Anderson825b72b2009-08-11 20:47:22 +000010779 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
10780 DAG.getConstant(5, MVT::i32));
10781 OutChains[2] = DAG.getStore(Root, dl, DAG.getConstant(JMP, MVT::i8), Addr,
Chris Lattner8026a9d2010-09-21 17:50:43 +000010782 MachinePointerInfo(TrmpAddr, 5),
10783 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +000010784
Owen Anderson825b72b2009-08-11 20:47:22 +000010785 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
10786 DAG.getConstant(6, MVT::i32));
Chris Lattner8026a9d2010-09-21 17:50:43 +000010787 OutChains[3] = DAG.getStore(Root, dl, Disp, Addr,
10788 MachinePointerInfo(TrmpAddr, 6),
David Greene67c9d422010-02-15 16:53:33 +000010789 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +000010790
Duncan Sands4a544a72011-09-06 13:37:06 +000010791 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains, 4);
Duncan Sandsb116fac2007-07-27 20:02:49 +000010792 }
10793}
10794
Dan Gohmand858e902010-04-17 15:26:15 +000010795SDValue X86TargetLowering::LowerFLT_ROUNDS_(SDValue Op,
10796 SelectionDAG &DAG) const {
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000010797 /*
10798 The rounding mode is in bits 11:10 of FPSR, and has the following
10799 settings:
10800 00 Round to nearest
10801 01 Round to -inf
10802 10 Round to +inf
10803 11 Round to 0
10804
10805 FLT_ROUNDS, on the other hand, expects the following:
10806 -1 Undefined
10807 0 Round to 0
10808 1 Round to nearest
10809 2 Round to +inf
10810 3 Round to -inf
10811
10812 To perform the conversion, we do:
10813 (((((FPSR & 0x800) >> 11) | ((FPSR & 0x400) >> 9)) + 1) & 3)
10814 */
10815
10816 MachineFunction &MF = DAG.getMachineFunction();
10817 const TargetMachine &TM = MF.getTarget();
Anton Korobeynikov16c29b52011-01-10 12:39:04 +000010818 const TargetFrameLowering &TFI = *TM.getFrameLowering();
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000010819 unsigned StackAlignment = TFI.getStackAlignment();
Owen Andersone50ed302009-08-10 22:56:29 +000010820 EVT VT = Op.getValueType();
Chris Lattner2156b792010-09-22 01:11:26 +000010821 DebugLoc DL = Op.getDebugLoc();
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000010822
10823 // Save FP Control Word to stack slot
David Greene3f2bf852009-11-12 20:49:22 +000010824 int SSFI = MF.getFrameInfo()->CreateStackObject(2, StackAlignment, false);
Dan Gohman475871a2008-07-27 21:46:04 +000010825 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000010826
Michael J. Spencerec38de22010-10-10 22:04:20 +000010827
Chris Lattner2156b792010-09-22 01:11:26 +000010828 MachineMemOperand *MMO =
10829 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
10830 MachineMemOperand::MOStore, 2, 2);
Michael J. Spencerec38de22010-10-10 22:04:20 +000010831
Chris Lattner2156b792010-09-22 01:11:26 +000010832 SDValue Ops[] = { DAG.getEntryNode(), StackSlot };
10833 SDValue Chain = DAG.getMemIntrinsicNode(X86ISD::FNSTCW16m, DL,
10834 DAG.getVTList(MVT::Other),
10835 Ops, 2, MVT::i16, MMO);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000010836
10837 // Load FP Control Word from stack slot
Chris Lattner2156b792010-09-22 01:11:26 +000010838 SDValue CWD = DAG.getLoad(MVT::i16, DL, Chain, StackSlot,
Pete Cooperd752e0f2011-11-08 18:42:53 +000010839 MachinePointerInfo(), false, false, false, 0);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000010840
10841 // Transform as necessary
Dan Gohman475871a2008-07-27 21:46:04 +000010842 SDValue CWD1 =
Chris Lattner2156b792010-09-22 01:11:26 +000010843 DAG.getNode(ISD::SRL, DL, MVT::i16,
10844 DAG.getNode(ISD::AND, DL, MVT::i16,
Owen Anderson825b72b2009-08-11 20:47:22 +000010845 CWD, DAG.getConstant(0x800, MVT::i16)),
10846 DAG.getConstant(11, MVT::i8));
Dan Gohman475871a2008-07-27 21:46:04 +000010847 SDValue CWD2 =
Chris Lattner2156b792010-09-22 01:11:26 +000010848 DAG.getNode(ISD::SRL, DL, MVT::i16,
10849 DAG.getNode(ISD::AND, DL, MVT::i16,
Owen Anderson825b72b2009-08-11 20:47:22 +000010850 CWD, DAG.getConstant(0x400, MVT::i16)),
10851 DAG.getConstant(9, MVT::i8));
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000010852
Dan Gohman475871a2008-07-27 21:46:04 +000010853 SDValue RetVal =
Chris Lattner2156b792010-09-22 01:11:26 +000010854 DAG.getNode(ISD::AND, DL, MVT::i16,
10855 DAG.getNode(ISD::ADD, DL, MVT::i16,
10856 DAG.getNode(ISD::OR, DL, MVT::i16, CWD1, CWD2),
Owen Anderson825b72b2009-08-11 20:47:22 +000010857 DAG.getConstant(1, MVT::i16)),
10858 DAG.getConstant(3, MVT::i16));
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000010859
10860
Duncan Sands83ec4b62008-06-06 12:08:01 +000010861 return DAG.getNode((VT.getSizeInBits() < 16 ?
Chris Lattner2156b792010-09-22 01:11:26 +000010862 ISD::TRUNCATE : ISD::ZERO_EXTEND), DL, VT, RetVal);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000010863}
10864
Craig Topper55b24052012-09-11 06:15:32 +000010865static SDValue LowerCTLZ(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +000010866 EVT VT = Op.getValueType();
10867 EVT OpVT = VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +000010868 unsigned NumBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +000010869 DebugLoc dl = Op.getDebugLoc();
Evan Cheng18efe262007-12-14 02:13:44 +000010870
10871 Op = Op.getOperand(0);
Owen Anderson825b72b2009-08-11 20:47:22 +000010872 if (VT == MVT::i8) {
Evan Cheng152804e2007-12-14 08:30:15 +000010873 // Zero extend to i32 since there is not an i8 bsr.
Owen Anderson825b72b2009-08-11 20:47:22 +000010874 OpVT = MVT::i32;
Dale Johannesene4d209d2009-02-03 20:21:25 +000010875 Op = DAG.getNode(ISD::ZERO_EXTEND, dl, OpVT, Op);
Evan Cheng18efe262007-12-14 02:13:44 +000010876 }
Evan Cheng18efe262007-12-14 02:13:44 +000010877
Evan Cheng152804e2007-12-14 08:30:15 +000010878 // Issue a bsr (scan bits in reverse) which also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +000010879 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +000010880 Op = DAG.getNode(X86ISD::BSR, dl, VTs, Op);
Evan Cheng152804e2007-12-14 08:30:15 +000010881
10882 // If src is zero (i.e. bsr sets ZF), returns NumBits.
Benjamin Kramer7f1a5602009-12-29 16:57:26 +000010883 SDValue Ops[] = {
10884 Op,
10885 DAG.getConstant(NumBits+NumBits-1, OpVT),
10886 DAG.getConstant(X86::COND_E, MVT::i8),
10887 Op.getValue(1)
10888 };
10889 Op = DAG.getNode(X86ISD::CMOV, dl, OpVT, Ops, array_lengthof(Ops));
Evan Cheng152804e2007-12-14 08:30:15 +000010890
10891 // Finally xor with NumBits-1.
Dale Johannesene4d209d2009-02-03 20:21:25 +000010892 Op = DAG.getNode(ISD::XOR, dl, OpVT, Op, DAG.getConstant(NumBits-1, OpVT));
Evan Cheng152804e2007-12-14 08:30:15 +000010893
Owen Anderson825b72b2009-08-11 20:47:22 +000010894 if (VT == MVT::i8)
10895 Op = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Op);
Evan Cheng18efe262007-12-14 02:13:44 +000010896 return Op;
10897}
10898
Craig Topper55b24052012-09-11 06:15:32 +000010899static SDValue LowerCTLZ_ZERO_UNDEF(SDValue Op, SelectionDAG &DAG) {
Chandler Carruthacc068e2011-12-24 10:55:54 +000010900 EVT VT = Op.getValueType();
10901 EVT OpVT = VT;
10902 unsigned NumBits = VT.getSizeInBits();
10903 DebugLoc dl = Op.getDebugLoc();
10904
10905 Op = Op.getOperand(0);
10906 if (VT == MVT::i8) {
10907 // Zero extend to i32 since there is not an i8 bsr.
10908 OpVT = MVT::i32;
10909 Op = DAG.getNode(ISD::ZERO_EXTEND, dl, OpVT, Op);
10910 }
10911
10912 // Issue a bsr (scan bits in reverse).
10913 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
10914 Op = DAG.getNode(X86ISD::BSR, dl, VTs, Op);
10915
10916 // And xor with NumBits-1.
10917 Op = DAG.getNode(ISD::XOR, dl, OpVT, Op, DAG.getConstant(NumBits-1, OpVT));
10918
10919 if (VT == MVT::i8)
10920 Op = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Op);
10921 return Op;
10922}
10923
Craig Topper55b24052012-09-11 06:15:32 +000010924static SDValue LowerCTTZ(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +000010925 EVT VT = Op.getValueType();
Duncan Sands83ec4b62008-06-06 12:08:01 +000010926 unsigned NumBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +000010927 DebugLoc dl = Op.getDebugLoc();
Evan Cheng18efe262007-12-14 02:13:44 +000010928 Op = Op.getOperand(0);
Evan Cheng152804e2007-12-14 08:30:15 +000010929
10930 // Issue a bsf (scan bits forward) which also sets EFLAGS.
Chandler Carruth77821022011-12-24 12:12:34 +000010931 SDVTList VTs = DAG.getVTList(VT, MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +000010932 Op = DAG.getNode(X86ISD::BSF, dl, VTs, Op);
Evan Cheng152804e2007-12-14 08:30:15 +000010933
10934 // If src is zero (i.e. bsf sets ZF), returns NumBits.
Benjamin Kramer7f1a5602009-12-29 16:57:26 +000010935 SDValue Ops[] = {
10936 Op,
Chandler Carruth77821022011-12-24 12:12:34 +000010937 DAG.getConstant(NumBits, VT),
Benjamin Kramer7f1a5602009-12-29 16:57:26 +000010938 DAG.getConstant(X86::COND_E, MVT::i8),
10939 Op.getValue(1)
10940 };
Chandler Carruth77821022011-12-24 12:12:34 +000010941 return DAG.getNode(X86ISD::CMOV, dl, VT, Ops, array_lengthof(Ops));
Evan Cheng18efe262007-12-14 02:13:44 +000010942}
10943
Craig Topper13894fa2011-08-24 06:14:18 +000010944// Lower256IntArith - Break a 256-bit integer operation into two new 128-bit
10945// ones, and then concatenate the result back.
10946static SDValue Lower256IntArith(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +000010947 EVT VT = Op.getValueType();
Craig Topper13894fa2011-08-24 06:14:18 +000010948
Craig Topper7a9a28b2012-08-12 02:23:29 +000010949 assert(VT.is256BitVector() && VT.isInteger() &&
Craig Topper13894fa2011-08-24 06:14:18 +000010950 "Unsupported value type for operation");
10951
Craig Topper66ddd152012-04-27 22:54:43 +000010952 unsigned NumElems = VT.getVectorNumElements();
Craig Topper13894fa2011-08-24 06:14:18 +000010953 DebugLoc dl = Op.getDebugLoc();
Craig Topper13894fa2011-08-24 06:14:18 +000010954
10955 // Extract the LHS vectors
10956 SDValue LHS = Op.getOperand(0);
Craig Topperb14940a2012-04-22 20:55:18 +000010957 SDValue LHS1 = Extract128BitVector(LHS, 0, DAG, dl);
10958 SDValue LHS2 = Extract128BitVector(LHS, NumElems/2, DAG, dl);
Craig Topper13894fa2011-08-24 06:14:18 +000010959
10960 // Extract the RHS vectors
10961 SDValue RHS = Op.getOperand(1);
Craig Topperb14940a2012-04-22 20:55:18 +000010962 SDValue RHS1 = Extract128BitVector(RHS, 0, DAG, dl);
10963 SDValue RHS2 = Extract128BitVector(RHS, NumElems/2, DAG, dl);
Craig Topper13894fa2011-08-24 06:14:18 +000010964
10965 MVT EltVT = VT.getVectorElementType().getSimpleVT();
10966 EVT NewVT = MVT::getVectorVT(EltVT, NumElems/2);
10967
10968 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT,
10969 DAG.getNode(Op.getOpcode(), dl, NewVT, LHS1, RHS1),
10970 DAG.getNode(Op.getOpcode(), dl, NewVT, LHS2, RHS2));
10971}
10972
Craig Topper55b24052012-09-11 06:15:32 +000010973static SDValue LowerADD(SDValue Op, SelectionDAG &DAG) {
Craig Topper7a9a28b2012-08-12 02:23:29 +000010974 assert(Op.getValueType().is256BitVector() &&
Craig Topper13894fa2011-08-24 06:14:18 +000010975 Op.getValueType().isInteger() &&
10976 "Only handle AVX 256-bit vector integer operation");
10977 return Lower256IntArith(Op, DAG);
10978}
10979
Craig Topper55b24052012-09-11 06:15:32 +000010980static SDValue LowerSUB(SDValue Op, SelectionDAG &DAG) {
Craig Topper7a9a28b2012-08-12 02:23:29 +000010981 assert(Op.getValueType().is256BitVector() &&
Craig Topper13894fa2011-08-24 06:14:18 +000010982 Op.getValueType().isInteger() &&
10983 "Only handle AVX 256-bit vector integer operation");
10984 return Lower256IntArith(Op, DAG);
10985}
10986
Craig Topper55b24052012-09-11 06:15:32 +000010987static SDValue LowerMUL(SDValue Op, const X86Subtarget *Subtarget,
10988 SelectionDAG &DAG) {
Craig Topper13894fa2011-08-24 06:14:18 +000010989 EVT VT = Op.getValueType();
10990
10991 // Decompose 256-bit ops into smaller 128-bit ops.
Craig Topper7a9a28b2012-08-12 02:23:29 +000010992 if (VT.is256BitVector() && !Subtarget->hasAVX2())
Craig Topper13894fa2011-08-24 06:14:18 +000010993 return Lower256IntArith(Op, DAG);
10994
Craig Topper5b209e82012-02-05 03:14:49 +000010995 assert((VT == MVT::v2i64 || VT == MVT::v4i64) &&
10996 "Only know how to lower V2I64/V4I64 multiply");
10997
Dale Johannesen6f38cb62009-02-07 19:59:05 +000010998 DebugLoc dl = Op.getDebugLoc();
Scott Michelfdc40a02009-02-17 22:15:04 +000010999
Craig Topper5b209e82012-02-05 03:14:49 +000011000 // Ahi = psrlqi(a, 32);
11001 // Bhi = psrlqi(b, 32);
11002 //
11003 // AloBlo = pmuludq(a, b);
11004 // AloBhi = pmuludq(a, Bhi);
11005 // AhiBlo = pmuludq(Ahi, b);
11006
11007 // AloBhi = psllqi(AloBhi, 32);
11008 // AhiBlo = psllqi(AhiBlo, 32);
11009 // return AloBlo + AloBhi + AhiBlo;
11010
Craig Topperaaa643c2011-11-09 07:28:55 +000011011 SDValue A = Op.getOperand(0);
11012 SDValue B = Op.getOperand(1);
11013
Craig Topper5b209e82012-02-05 03:14:49 +000011014 SDValue ShAmt = DAG.getConstant(32, MVT::i32);
Craig Topperaaa643c2011-11-09 07:28:55 +000011015
Craig Topper5b209e82012-02-05 03:14:49 +000011016 SDValue Ahi = DAG.getNode(X86ISD::VSRLI, dl, VT, A, ShAmt);
11017 SDValue Bhi = DAG.getNode(X86ISD::VSRLI, dl, VT, B, ShAmt);
Craig Topperaaa643c2011-11-09 07:28:55 +000011018
Craig Topper5b209e82012-02-05 03:14:49 +000011019 // Bit cast to 32-bit vectors for MULUDQ
11020 EVT MulVT = (VT == MVT::v2i64) ? MVT::v4i32 : MVT::v8i32;
11021 A = DAG.getNode(ISD::BITCAST, dl, MulVT, A);
11022 B = DAG.getNode(ISD::BITCAST, dl, MulVT, B);
11023 Ahi = DAG.getNode(ISD::BITCAST, dl, MulVT, Ahi);
11024 Bhi = DAG.getNode(ISD::BITCAST, dl, MulVT, Bhi);
Craig Topperaaa643c2011-11-09 07:28:55 +000011025
Craig Topper5b209e82012-02-05 03:14:49 +000011026 SDValue AloBlo = DAG.getNode(X86ISD::PMULUDQ, dl, VT, A, B);
11027 SDValue AloBhi = DAG.getNode(X86ISD::PMULUDQ, dl, VT, A, Bhi);
11028 SDValue AhiBlo = DAG.getNode(X86ISD::PMULUDQ, dl, VT, Ahi, B);
Craig Topperaaa643c2011-11-09 07:28:55 +000011029
Craig Topper5b209e82012-02-05 03:14:49 +000011030 AloBhi = DAG.getNode(X86ISD::VSHLI, dl, VT, AloBhi, ShAmt);
11031 AhiBlo = DAG.getNode(X86ISD::VSHLI, dl, VT, AhiBlo, ShAmt);
Mon P Wangaf9b9522008-12-18 21:42:19 +000011032
Dale Johannesene4d209d2009-02-03 20:21:25 +000011033 SDValue Res = DAG.getNode(ISD::ADD, dl, VT, AloBlo, AloBhi);
Craig Topper5b209e82012-02-05 03:14:49 +000011034 return DAG.getNode(ISD::ADD, dl, VT, Res, AhiBlo);
Mon P Wangaf9b9522008-12-18 21:42:19 +000011035}
11036
Nadav Rotem43012222011-05-11 08:12:09 +000011037SDValue X86TargetLowering::LowerShift(SDValue Op, SelectionDAG &DAG) const {
11038
Nate Begemanbdcb5af2010-07-27 22:37:06 +000011039 EVT VT = Op.getValueType();
11040 DebugLoc dl = Op.getDebugLoc();
11041 SDValue R = Op.getOperand(0);
Nadav Rotem43012222011-05-11 08:12:09 +000011042 SDValue Amt = Op.getOperand(1);
Nate Begemanbdcb5af2010-07-27 22:37:06 +000011043 LLVMContext *Context = DAG.getContext();
Nate Begemanbdcb5af2010-07-27 22:37:06 +000011044
Craig Topper1accb7e2012-01-10 06:54:16 +000011045 if (!Subtarget->hasSSE2())
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +000011046 return SDValue();
11047
Nadav Rotem43012222011-05-11 08:12:09 +000011048 // Optimize shl/srl/sra with constant shift amount.
11049 if (isSplatVector(Amt.getNode())) {
11050 SDValue SclrAmt = Amt->getOperand(0);
11051 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(SclrAmt)) {
11052 uint64_t ShiftAmt = C->getZExtValue();
11053
Craig Toppered2e13d2012-01-22 19:15:14 +000011054 if (VT == MVT::v2i64 || VT == MVT::v4i32 || VT == MVT::v8i16 ||
11055 (Subtarget->hasAVX2() &&
11056 (VT == MVT::v4i64 || VT == MVT::v8i32 || VT == MVT::v16i16))) {
11057 if (Op.getOpcode() == ISD::SHL)
11058 return DAG.getNode(X86ISD::VSHLI, dl, VT, R,
11059 DAG.getConstant(ShiftAmt, MVT::i32));
11060 if (Op.getOpcode() == ISD::SRL)
11061 return DAG.getNode(X86ISD::VSRLI, dl, VT, R,
11062 DAG.getConstant(ShiftAmt, MVT::i32));
11063 if (Op.getOpcode() == ISD::SRA && VT != MVT::v2i64 && VT != MVT::v4i64)
11064 return DAG.getNode(X86ISD::VSRAI, dl, VT, R,
11065 DAG.getConstant(ShiftAmt, MVT::i32));
Benjamin Kramerdade3c12011-10-30 17:31:21 +000011066 }
11067
Craig Toppered2e13d2012-01-22 19:15:14 +000011068 if (VT == MVT::v16i8) {
11069 if (Op.getOpcode() == ISD::SHL) {
11070 // Make a large shift.
11071 SDValue SHL = DAG.getNode(X86ISD::VSHLI, dl, MVT::v8i16, R,
11072 DAG.getConstant(ShiftAmt, MVT::i32));
11073 SHL = DAG.getNode(ISD::BITCAST, dl, VT, SHL);
11074 // Zero out the rightmost bits.
11075 SmallVector<SDValue, 16> V(16,
11076 DAG.getConstant(uint8_t(-1U << ShiftAmt),
11077 MVT::i8));
11078 return DAG.getNode(ISD::AND, dl, VT, SHL,
11079 DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &V[0], 16));
Eli Friedmanf6aa6b12011-11-01 21:18:39 +000011080 }
Craig Toppered2e13d2012-01-22 19:15:14 +000011081 if (Op.getOpcode() == ISD::SRL) {
11082 // Make a large shift.
11083 SDValue SRL = DAG.getNode(X86ISD::VSRLI, dl, MVT::v8i16, R,
11084 DAG.getConstant(ShiftAmt, MVT::i32));
11085 SRL = DAG.getNode(ISD::BITCAST, dl, VT, SRL);
11086 // Zero out the leftmost bits.
11087 SmallVector<SDValue, 16> V(16,
11088 DAG.getConstant(uint8_t(-1U) >> ShiftAmt,
11089 MVT::i8));
11090 return DAG.getNode(ISD::AND, dl, VT, SRL,
11091 DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &V[0], 16));
11092 }
11093 if (Op.getOpcode() == ISD::SRA) {
11094 if (ShiftAmt == 7) {
11095 // R s>> 7 === R s< 0
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +000011096 SDValue Zeros = getZeroVector(VT, Subtarget, DAG, dl);
Craig Topper67609fd2012-01-22 22:42:16 +000011097 return DAG.getNode(X86ISD::PCMPGT, dl, VT, Zeros, R);
Craig Toppered2e13d2012-01-22 19:15:14 +000011098 }
Eli Friedmanf6aa6b12011-11-01 21:18:39 +000011099
Craig Toppered2e13d2012-01-22 19:15:14 +000011100 // R s>> a === ((R u>> a) ^ m) - m
11101 SDValue Res = DAG.getNode(ISD::SRL, dl, VT, R, Amt);
11102 SmallVector<SDValue, 16> V(16, DAG.getConstant(128 >> ShiftAmt,
11103 MVT::i8));
11104 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &V[0], 16);
11105 Res = DAG.getNode(ISD::XOR, dl, VT, Res, Mask);
11106 Res = DAG.getNode(ISD::SUB, dl, VT, Res, Mask);
11107 return Res;
11108 }
Craig Topper731dfd02012-04-23 03:42:40 +000011109 llvm_unreachable("Unknown shift opcode.");
Eli Friedmanf6aa6b12011-11-01 21:18:39 +000011110 }
Craig Topper46154eb2011-11-11 07:39:23 +000011111
Craig Topper0d86d462011-11-20 00:12:05 +000011112 if (Subtarget->hasAVX2() && VT == MVT::v32i8) {
11113 if (Op.getOpcode() == ISD::SHL) {
11114 // Make a large shift.
Craig Toppered2e13d2012-01-22 19:15:14 +000011115 SDValue SHL = DAG.getNode(X86ISD::VSHLI, dl, MVT::v16i16, R,
11116 DAG.getConstant(ShiftAmt, MVT::i32));
11117 SHL = DAG.getNode(ISD::BITCAST, dl, VT, SHL);
Craig Topper0d86d462011-11-20 00:12:05 +000011118 // Zero out the rightmost bits.
Craig Toppered2e13d2012-01-22 19:15:14 +000011119 SmallVector<SDValue, 32> V(32,
11120 DAG.getConstant(uint8_t(-1U << ShiftAmt),
11121 MVT::i8));
Craig Topper0d86d462011-11-20 00:12:05 +000011122 return DAG.getNode(ISD::AND, dl, VT, SHL,
11123 DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &V[0], 32));
Craig Topper46154eb2011-11-11 07:39:23 +000011124 }
Craig Topper0d86d462011-11-20 00:12:05 +000011125 if (Op.getOpcode() == ISD::SRL) {
11126 // Make a large shift.
Craig Toppered2e13d2012-01-22 19:15:14 +000011127 SDValue SRL = DAG.getNode(X86ISD::VSRLI, dl, MVT::v16i16, R,
11128 DAG.getConstant(ShiftAmt, MVT::i32));
11129 SRL = DAG.getNode(ISD::BITCAST, dl, VT, SRL);
Craig Topper0d86d462011-11-20 00:12:05 +000011130 // Zero out the leftmost bits.
Craig Toppered2e13d2012-01-22 19:15:14 +000011131 SmallVector<SDValue, 32> V(32,
11132 DAG.getConstant(uint8_t(-1U) >> ShiftAmt,
11133 MVT::i8));
Craig Topper0d86d462011-11-20 00:12:05 +000011134 return DAG.getNode(ISD::AND, dl, VT, SRL,
11135 DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &V[0], 32));
11136 }
11137 if (Op.getOpcode() == ISD::SRA) {
11138 if (ShiftAmt == 7) {
11139 // R s>> 7 === R s< 0
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +000011140 SDValue Zeros = getZeroVector(VT, Subtarget, DAG, dl);
Craig Topper67609fd2012-01-22 22:42:16 +000011141 return DAG.getNode(X86ISD::PCMPGT, dl, VT, Zeros, R);
Craig Topper0d86d462011-11-20 00:12:05 +000011142 }
11143
11144 // R s>> a === ((R u>> a) ^ m) - m
11145 SDValue Res = DAG.getNode(ISD::SRL, dl, VT, R, Amt);
11146 SmallVector<SDValue, 32> V(32, DAG.getConstant(128 >> ShiftAmt,
11147 MVT::i8));
11148 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &V[0], 32);
11149 Res = DAG.getNode(ISD::XOR, dl, VT, Res, Mask);
11150 Res = DAG.getNode(ISD::SUB, dl, VT, Res, Mask);
11151 return Res;
11152 }
Craig Topper731dfd02012-04-23 03:42:40 +000011153 llvm_unreachable("Unknown shift opcode.");
Craig Topper0d86d462011-11-20 00:12:05 +000011154 }
Nadav Rotem43012222011-05-11 08:12:09 +000011155 }
11156 }
11157
11158 // Lower SHL with variable shift amount.
Nadav Rotem43012222011-05-11 08:12:09 +000011159 if (VT == MVT::v4i32 && Op->getOpcode() == ISD::SHL) {
Craig Toppered2e13d2012-01-22 19:15:14 +000011160 Op = DAG.getNode(X86ISD::VSHLI, dl, VT, Op.getOperand(1),
11161 DAG.getConstant(23, MVT::i32));
Nate Begeman51409212010-07-28 00:21:48 +000011162
Chris Lattner7302d802012-02-06 21:56:39 +000011163 const uint32_t CV[] = { 0x3f800000U, 0x3f800000U, 0x3f800000U, 0x3f800000U};
11164 Constant *C = ConstantDataVector::get(*Context, CV);
Nate Begeman51409212010-07-28 00:21:48 +000011165 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
11166 SDValue Addend = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +000011167 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000011168 false, false, false, 16);
Nate Begeman51409212010-07-28 00:21:48 +000011169
11170 Op = DAG.getNode(ISD::ADD, dl, VT, Op, Addend);
Wesley Peckbf17cfa2010-11-23 03:31:01 +000011171 Op = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, Op);
Nate Begeman51409212010-07-28 00:21:48 +000011172 Op = DAG.getNode(ISD::FP_TO_SINT, dl, VT, Op);
11173 return DAG.getNode(ISD::MUL, dl, VT, Op, R);
11174 }
Nadav Rotem43012222011-05-11 08:12:09 +000011175 if (VT == MVT::v16i8 && Op->getOpcode() == ISD::SHL) {
Craig Topper8b5a6b62012-01-17 08:23:44 +000011176 assert(Subtarget->hasSSE2() && "Need SSE2 for pslli/pcmpeq.");
Lang Hames8b99c1e2011-12-17 01:08:46 +000011177
Nate Begeman51409212010-07-28 00:21:48 +000011178 // a = a << 5;
Craig Toppered2e13d2012-01-22 19:15:14 +000011179 Op = DAG.getNode(X86ISD::VSHLI, dl, MVT::v8i16, Op.getOperand(1),
11180 DAG.getConstant(5, MVT::i32));
11181 Op = DAG.getNode(ISD::BITCAST, dl, VT, Op);
Nate Begeman51409212010-07-28 00:21:48 +000011182
Lang Hames8b99c1e2011-12-17 01:08:46 +000011183 // Turn 'a' into a mask suitable for VSELECT
11184 SDValue VSelM = DAG.getConstant(0x80, VT);
11185 SDValue OpVSel = DAG.getNode(ISD::AND, dl, VT, VSelM, Op);
Craig Topper67609fd2012-01-22 22:42:16 +000011186 OpVSel = DAG.getNode(X86ISD::PCMPEQ, dl, VT, OpVSel, VSelM);
Nate Begeman51409212010-07-28 00:21:48 +000011187
Lang Hames8b99c1e2011-12-17 01:08:46 +000011188 SDValue CM1 = DAG.getConstant(0x0f, VT);
11189 SDValue CM2 = DAG.getConstant(0x3f, VT);
Nate Begeman51409212010-07-28 00:21:48 +000011190
Lang Hames8b99c1e2011-12-17 01:08:46 +000011191 // r = VSELECT(r, psllw(r & (char16)15, 4), a);
11192 SDValue M = DAG.getNode(ISD::AND, dl, VT, R, CM1);
Craig Toppered2e13d2012-01-22 19:15:14 +000011193 M = getTargetVShiftNode(X86ISD::VSHLI, dl, MVT::v8i16, M,
11194 DAG.getConstant(4, MVT::i32), DAG);
11195 M = DAG.getNode(ISD::BITCAST, dl, VT, M);
Lang Hames8b99c1e2011-12-17 01:08:46 +000011196 R = DAG.getNode(ISD::VSELECT, dl, VT, OpVSel, M, R);
11197
Nate Begeman51409212010-07-28 00:21:48 +000011198 // a += a
11199 Op = DAG.getNode(ISD::ADD, dl, VT, Op, Op);
Lang Hames8b99c1e2011-12-17 01:08:46 +000011200 OpVSel = DAG.getNode(ISD::AND, dl, VT, VSelM, Op);
Craig Topper67609fd2012-01-22 22:42:16 +000011201 OpVSel = DAG.getNode(X86ISD::PCMPEQ, dl, VT, OpVSel, VSelM);
Michael J. Spencerec38de22010-10-10 22:04:20 +000011202
Lang Hames8b99c1e2011-12-17 01:08:46 +000011203 // r = VSELECT(r, psllw(r & (char16)63, 2), a);
11204 M = DAG.getNode(ISD::AND, dl, VT, R, CM2);
Craig Toppered2e13d2012-01-22 19:15:14 +000011205 M = getTargetVShiftNode(X86ISD::VSHLI, dl, MVT::v8i16, M,
11206 DAG.getConstant(2, MVT::i32), DAG);
11207 M = DAG.getNode(ISD::BITCAST, dl, VT, M);
Lang Hames8b99c1e2011-12-17 01:08:46 +000011208 R = DAG.getNode(ISD::VSELECT, dl, VT, OpVSel, M, R);
11209
Nate Begeman51409212010-07-28 00:21:48 +000011210 // a += a
11211 Op = DAG.getNode(ISD::ADD, dl, VT, Op, Op);
Lang Hames8b99c1e2011-12-17 01:08:46 +000011212 OpVSel = DAG.getNode(ISD::AND, dl, VT, VSelM, Op);
Craig Topper67609fd2012-01-22 22:42:16 +000011213 OpVSel = DAG.getNode(X86ISD::PCMPEQ, dl, VT, OpVSel, VSelM);
Michael J. Spencerec38de22010-10-10 22:04:20 +000011214
Lang Hames8b99c1e2011-12-17 01:08:46 +000011215 // return VSELECT(r, r+r, a);
11216 R = DAG.getNode(ISD::VSELECT, dl, VT, OpVSel,
Lang Hamesa0a25132011-12-15 18:57:27 +000011217 DAG.getNode(ISD::ADD, dl, VT, R, R), R);
Nate Begeman51409212010-07-28 00:21:48 +000011218 return R;
11219 }
Craig Topper46154eb2011-11-11 07:39:23 +000011220
11221 // Decompose 256-bit shifts into smaller 128-bit shifts.
Craig Topper7a9a28b2012-08-12 02:23:29 +000011222 if (VT.is256BitVector()) {
Craig Toppered2e13d2012-01-22 19:15:14 +000011223 unsigned NumElems = VT.getVectorNumElements();
Craig Topper46154eb2011-11-11 07:39:23 +000011224 MVT EltVT = VT.getVectorElementType().getSimpleVT();
11225 EVT NewVT = MVT::getVectorVT(EltVT, NumElems/2);
11226
11227 // Extract the two vectors
Craig Topperb14940a2012-04-22 20:55:18 +000011228 SDValue V1 = Extract128BitVector(R, 0, DAG, dl);
11229 SDValue V2 = Extract128BitVector(R, NumElems/2, DAG, dl);
Craig Topper46154eb2011-11-11 07:39:23 +000011230
11231 // Recreate the shift amount vectors
11232 SDValue Amt1, Amt2;
11233 if (Amt.getOpcode() == ISD::BUILD_VECTOR) {
11234 // Constant shift amount
11235 SmallVector<SDValue, 4> Amt1Csts;
11236 SmallVector<SDValue, 4> Amt2Csts;
Craig Toppered2e13d2012-01-22 19:15:14 +000011237 for (unsigned i = 0; i != NumElems/2; ++i)
Craig Topper46154eb2011-11-11 07:39:23 +000011238 Amt1Csts.push_back(Amt->getOperand(i));
Craig Toppered2e13d2012-01-22 19:15:14 +000011239 for (unsigned i = NumElems/2; i != NumElems; ++i)
Craig Topper46154eb2011-11-11 07:39:23 +000011240 Amt2Csts.push_back(Amt->getOperand(i));
11241
11242 Amt1 = DAG.getNode(ISD::BUILD_VECTOR, dl, NewVT,
11243 &Amt1Csts[0], NumElems/2);
11244 Amt2 = DAG.getNode(ISD::BUILD_VECTOR, dl, NewVT,
11245 &Amt2Csts[0], NumElems/2);
11246 } else {
11247 // Variable shift amount
Craig Topperb14940a2012-04-22 20:55:18 +000011248 Amt1 = Extract128BitVector(Amt, 0, DAG, dl);
11249 Amt2 = Extract128BitVector(Amt, NumElems/2, DAG, dl);
Craig Topper46154eb2011-11-11 07:39:23 +000011250 }
11251
11252 // Issue new vector shifts for the smaller types
11253 V1 = DAG.getNode(Op.getOpcode(), dl, NewVT, V1, Amt1);
11254 V2 = DAG.getNode(Op.getOpcode(), dl, NewVT, V2, Amt2);
11255
11256 // Concatenate the result back
11257 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT, V1, V2);
11258 }
11259
Nate Begeman51409212010-07-28 00:21:48 +000011260 return SDValue();
Nate Begemanbdcb5af2010-07-27 22:37:06 +000011261}
Mon P Wangaf9b9522008-12-18 21:42:19 +000011262
Craig Topper55b24052012-09-11 06:15:32 +000011263static SDValue LowerXALUO(SDValue Op, SelectionDAG &DAG) {
Bill Wendling74c37652008-12-09 22:08:41 +000011264 // Lower the "add/sub/mul with overflow" instruction into a regular ins plus
11265 // a "setcc" instruction that checks the overflow flag. The "brcond" lowering
Bill Wendling61edeb52008-12-02 01:06:39 +000011266 // looks for this combo and may remove the "setcc" instruction if the "setcc"
11267 // has only one use.
Bill Wendling3fafd932008-11-26 22:37:40 +000011268 SDNode *N = Op.getNode();
Bill Wendling61edeb52008-12-02 01:06:39 +000011269 SDValue LHS = N->getOperand(0);
11270 SDValue RHS = N->getOperand(1);
Bill Wendling74c37652008-12-09 22:08:41 +000011271 unsigned BaseOp = 0;
11272 unsigned Cond = 0;
Chris Lattnerb20e0b12010-12-05 07:30:36 +000011273 DebugLoc DL = Op.getDebugLoc();
Bill Wendling74c37652008-12-09 22:08:41 +000011274 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +000011275 default: llvm_unreachable("Unknown ovf instruction!");
Bill Wendling74c37652008-12-09 22:08:41 +000011276 case ISD::SADDO:
Dan Gohman076aee32009-03-04 19:44:21 +000011277 // A subtract of one will be selected as a INC. Note that INC doesn't
11278 // set CF, so we can't do this for UADDO.
Benjamin Kramerc175a4b2011-03-08 15:20:20 +000011279 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(RHS))
11280 if (C->isOne()) {
Dan Gohman076aee32009-03-04 19:44:21 +000011281 BaseOp = X86ISD::INC;
11282 Cond = X86::COND_O;
11283 break;
11284 }
Bill Wendlingab55ebd2008-12-12 00:56:36 +000011285 BaseOp = X86ISD::ADD;
Bill Wendling74c37652008-12-09 22:08:41 +000011286 Cond = X86::COND_O;
11287 break;
11288 case ISD::UADDO:
Bill Wendlingab55ebd2008-12-12 00:56:36 +000011289 BaseOp = X86ISD::ADD;
Dan Gohman653456c2009-01-07 00:15:08 +000011290 Cond = X86::COND_B;
Bill Wendling74c37652008-12-09 22:08:41 +000011291 break;
11292 case ISD::SSUBO:
Dan Gohman076aee32009-03-04 19:44:21 +000011293 // A subtract of one will be selected as a DEC. Note that DEC doesn't
11294 // set CF, so we can't do this for USUBO.
Benjamin Kramerc175a4b2011-03-08 15:20:20 +000011295 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(RHS))
11296 if (C->isOne()) {
Dan Gohman076aee32009-03-04 19:44:21 +000011297 BaseOp = X86ISD::DEC;
11298 Cond = X86::COND_O;
11299 break;
11300 }
Bill Wendlingab55ebd2008-12-12 00:56:36 +000011301 BaseOp = X86ISD::SUB;
Bill Wendling74c37652008-12-09 22:08:41 +000011302 Cond = X86::COND_O;
11303 break;
11304 case ISD::USUBO:
Bill Wendlingab55ebd2008-12-12 00:56:36 +000011305 BaseOp = X86ISD::SUB;
Dan Gohman653456c2009-01-07 00:15:08 +000011306 Cond = X86::COND_B;
Bill Wendling74c37652008-12-09 22:08:41 +000011307 break;
11308 case ISD::SMULO:
Bill Wendlingd350e022008-12-12 21:15:41 +000011309 BaseOp = X86ISD::SMUL;
Bill Wendling74c37652008-12-09 22:08:41 +000011310 Cond = X86::COND_O;
11311 break;
Chris Lattnerb20e0b12010-12-05 07:30:36 +000011312 case ISD::UMULO: { // i64, i8 = umulo lhs, rhs --> i64, i64, i32 umul lhs,rhs
11313 SDVTList VTs = DAG.getVTList(N->getValueType(0), N->getValueType(0),
11314 MVT::i32);
11315 SDValue Sum = DAG.getNode(X86ISD::UMUL, DL, VTs, LHS, RHS);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011316
Chris Lattnerb20e0b12010-12-05 07:30:36 +000011317 SDValue SetCC =
11318 DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
11319 DAG.getConstant(X86::COND_O, MVT::i32),
11320 SDValue(Sum.getNode(), 2));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011321
Dan Gohman6e5fda22011-07-22 18:45:15 +000011322 return DAG.getNode(ISD::MERGE_VALUES, DL, N->getVTList(), Sum, SetCC);
Chris Lattnerb20e0b12010-12-05 07:30:36 +000011323 }
Bill Wendling74c37652008-12-09 22:08:41 +000011324 }
Bill Wendling3fafd932008-11-26 22:37:40 +000011325
Bill Wendling61edeb52008-12-02 01:06:39 +000011326 // Also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +000011327 SDVTList VTs = DAG.getVTList(N->getValueType(0), MVT::i32);
Chris Lattnerb20e0b12010-12-05 07:30:36 +000011328 SDValue Sum = DAG.getNode(BaseOp, DL, VTs, LHS, RHS);
Bill Wendling3fafd932008-11-26 22:37:40 +000011329
Bill Wendling61edeb52008-12-02 01:06:39 +000011330 SDValue SetCC =
Chris Lattnerb20e0b12010-12-05 07:30:36 +000011331 DAG.getNode(X86ISD::SETCC, DL, N->getValueType(1),
11332 DAG.getConstant(Cond, MVT::i32),
11333 SDValue(Sum.getNode(), 1));
Bill Wendling3fafd932008-11-26 22:37:40 +000011334
Dan Gohman6e5fda22011-07-22 18:45:15 +000011335 return DAG.getNode(ISD::MERGE_VALUES, DL, N->getVTList(), Sum, SetCC);
Bill Wendling41ea7e72008-11-24 19:21:46 +000011336}
11337
Chad Rosier30450e82011-12-22 22:35:21 +000011338SDValue X86TargetLowering::LowerSIGN_EXTEND_INREG(SDValue Op,
11339 SelectionDAG &DAG) const {
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000011340 DebugLoc dl = Op.getDebugLoc();
Craig Toppera124f942011-11-21 01:12:36 +000011341 EVT ExtraVT = cast<VTSDNode>(Op.getOperand(1))->getVT();
11342 EVT VT = Op.getValueType();
11343
Craig Toppered2e13d2012-01-22 19:15:14 +000011344 if (!Subtarget->hasSSE2() || !VT.isVector())
11345 return SDValue();
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000011346
Craig Toppered2e13d2012-01-22 19:15:14 +000011347 unsigned BitsDiff = VT.getScalarType().getSizeInBits() -
11348 ExtraVT.getScalarType().getSizeInBits();
11349 SDValue ShAmt = DAG.getConstant(BitsDiff, MVT::i32);
11350
11351 switch (VT.getSimpleVT().SimpleTy) {
11352 default: return SDValue();
11353 case MVT::v8i32:
11354 case MVT::v16i16:
11355 if (!Subtarget->hasAVX())
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000011356 return SDValue();
Craig Toppered2e13d2012-01-22 19:15:14 +000011357 if (!Subtarget->hasAVX2()) {
11358 // needs to be split
Craig Topper66ddd152012-04-27 22:54:43 +000011359 unsigned NumElems = VT.getVectorNumElements();
Craig Toppera124f942011-11-21 01:12:36 +000011360
Craig Toppered2e13d2012-01-22 19:15:14 +000011361 // Extract the LHS vectors
11362 SDValue LHS = Op.getOperand(0);
Craig Topperb14940a2012-04-22 20:55:18 +000011363 SDValue LHS1 = Extract128BitVector(LHS, 0, DAG, dl);
11364 SDValue LHS2 = Extract128BitVector(LHS, NumElems/2, DAG, dl);
Craig Toppera124f942011-11-21 01:12:36 +000011365
Craig Toppered2e13d2012-01-22 19:15:14 +000011366 MVT EltVT = VT.getVectorElementType().getSimpleVT();
11367 EVT NewVT = MVT::getVectorVT(EltVT, NumElems/2);
Craig Toppera124f942011-11-21 01:12:36 +000011368
Craig Toppered2e13d2012-01-22 19:15:14 +000011369 EVT ExtraEltVT = ExtraVT.getVectorElementType();
Craig Topperb6072642012-05-03 07:26:59 +000011370 unsigned ExtraNumElems = ExtraVT.getVectorNumElements();
Craig Toppered2e13d2012-01-22 19:15:14 +000011371 ExtraVT = EVT::getVectorVT(*DAG.getContext(), ExtraEltVT,
11372 ExtraNumElems/2);
11373 SDValue Extra = DAG.getValueType(ExtraVT);
Craig Toppera124f942011-11-21 01:12:36 +000011374
Craig Toppered2e13d2012-01-22 19:15:14 +000011375 LHS1 = DAG.getNode(Op.getOpcode(), dl, NewVT, LHS1, Extra);
11376 LHS2 = DAG.getNode(Op.getOpcode(), dl, NewVT, LHS2, Extra);
Craig Toppera124f942011-11-21 01:12:36 +000011377
Dmitri Gribenko2de05722012-09-10 21:26:47 +000011378 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT, LHS1, LHS2);
Craig Toppered2e13d2012-01-22 19:15:14 +000011379 }
11380 // fall through
11381 case MVT::v4i32:
11382 case MVT::v8i16: {
11383 SDValue Tmp1 = getTargetVShiftNode(X86ISD::VSHLI, dl, VT,
11384 Op.getOperand(0), ShAmt, DAG);
11385 return getTargetVShiftNode(X86ISD::VSRAI, dl, VT, Tmp1, ShAmt, DAG);
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000011386 }
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000011387 }
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000011388}
11389
11390
Craig Topper55b24052012-09-11 06:15:32 +000011391static SDValue LowerMEMBARRIER(SDValue Op, const X86Subtarget *Subtarget,
11392 SelectionDAG &DAG) {
Eric Christopher9a9d2752010-07-22 02:48:34 +000011393 DebugLoc dl = Op.getDebugLoc();
Michael J. Spencerec38de22010-10-10 22:04:20 +000011394
Eric Christopher77ed1352011-07-08 00:04:56 +000011395 // Go ahead and emit the fence on x86-64 even if we asked for no-sse2.
11396 // There isn't any reason to disable it if the target processor supports it.
Craig Topper1accb7e2012-01-10 06:54:16 +000011397 if (!Subtarget->hasSSE2() && !Subtarget->is64Bit()) {
Eric Christopherc0b2a202010-08-14 21:51:50 +000011398 SDValue Chain = Op.getOperand(0);
Eric Christopher77ed1352011-07-08 00:04:56 +000011399 SDValue Zero = DAG.getConstant(0, MVT::i32);
Eric Christopherc0b2a202010-08-14 21:51:50 +000011400 SDValue Ops[] = {
11401 DAG.getRegister(X86::ESP, MVT::i32), // Base
11402 DAG.getTargetConstant(1, MVT::i8), // Scale
11403 DAG.getRegister(0, MVT::i32), // Index
11404 DAG.getTargetConstant(0, MVT::i32), // Disp
11405 DAG.getRegister(0, MVT::i32), // Segment.
11406 Zero,
11407 Chain
11408 };
Michael J. Spencerec38de22010-10-10 22:04:20 +000011409 SDNode *Res =
Eric Christopherc0b2a202010-08-14 21:51:50 +000011410 DAG.getMachineNode(X86::OR32mrLocked, dl, MVT::Other, Ops,
11411 array_lengthof(Ops));
11412 return SDValue(Res, 0);
Eric Christopherb6729dc2010-08-04 23:03:04 +000011413 }
Michael J. Spencerec38de22010-10-10 22:04:20 +000011414
Eric Christopher9a9d2752010-07-22 02:48:34 +000011415 unsigned isDev = cast<ConstantSDNode>(Op.getOperand(5))->getZExtValue();
Chris Lattner132929a2010-08-14 17:26:09 +000011416 if (!isDev)
Eric Christopher9a9d2752010-07-22 02:48:34 +000011417 return DAG.getNode(X86ISD::MEMBARRIER, dl, MVT::Other, Op.getOperand(0));
Michael J. Spencerec38de22010-10-10 22:04:20 +000011418
Chris Lattner132929a2010-08-14 17:26:09 +000011419 unsigned Op1 = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
11420 unsigned Op2 = cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue();
11421 unsigned Op3 = cast<ConstantSDNode>(Op.getOperand(3))->getZExtValue();
11422 unsigned Op4 = cast<ConstantSDNode>(Op.getOperand(4))->getZExtValue();
Michael J. Spencerec38de22010-10-10 22:04:20 +000011423
Chris Lattner132929a2010-08-14 17:26:09 +000011424 // def : Pat<(membarrier (i8 0), (i8 0), (i8 0), (i8 1), (i8 1)), (SFENCE)>;
11425 if (!Op1 && !Op2 && !Op3 && Op4)
11426 return DAG.getNode(X86ISD::SFENCE, dl, MVT::Other, Op.getOperand(0));
Michael J. Spencerec38de22010-10-10 22:04:20 +000011427
Chris Lattner132929a2010-08-14 17:26:09 +000011428 // def : Pat<(membarrier (i8 1), (i8 0), (i8 0), (i8 0), (i8 1)), (LFENCE)>;
11429 if (Op1 && !Op2 && !Op3 && !Op4)
11430 return DAG.getNode(X86ISD::LFENCE, dl, MVT::Other, Op.getOperand(0));
Michael J. Spencerec38de22010-10-10 22:04:20 +000011431
11432 // def : Pat<(membarrier (i8 imm), (i8 imm), (i8 imm), (i8 imm), (i8 1)),
Chris Lattner132929a2010-08-14 17:26:09 +000011433 // (MFENCE)>;
11434 return DAG.getNode(X86ISD::MFENCE, dl, MVT::Other, Op.getOperand(0));
Eric Christopher9a9d2752010-07-22 02:48:34 +000011435}
11436
Craig Topper55b24052012-09-11 06:15:32 +000011437static SDValue LowerATOMIC_FENCE(SDValue Op, const X86Subtarget *Subtarget,
11438 SelectionDAG &DAG) {
Eli Friedman14648462011-07-27 22:21:52 +000011439 DebugLoc dl = Op.getDebugLoc();
11440 AtomicOrdering FenceOrdering = static_cast<AtomicOrdering>(
11441 cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue());
11442 SynchronizationScope FenceScope = static_cast<SynchronizationScope>(
11443 cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue());
11444
11445 // The only fence that needs an instruction is a sequentially-consistent
11446 // cross-thread fence.
11447 if (FenceOrdering == SequentiallyConsistent && FenceScope == CrossThread) {
11448 // Use mfence if we have SSE2 or we're on x86-64 (even if we asked for
11449 // no-sse2). There isn't any reason to disable it if the target processor
11450 // supports it.
Craig Topper1accb7e2012-01-10 06:54:16 +000011451 if (Subtarget->hasSSE2() || Subtarget->is64Bit())
Eli Friedman14648462011-07-27 22:21:52 +000011452 return DAG.getNode(X86ISD::MFENCE, dl, MVT::Other, Op.getOperand(0));
11453
11454 SDValue Chain = Op.getOperand(0);
11455 SDValue Zero = DAG.getConstant(0, MVT::i32);
11456 SDValue Ops[] = {
11457 DAG.getRegister(X86::ESP, MVT::i32), // Base
11458 DAG.getTargetConstant(1, MVT::i8), // Scale
11459 DAG.getRegister(0, MVT::i32), // Index
11460 DAG.getTargetConstant(0, MVT::i32), // Disp
11461 DAG.getRegister(0, MVT::i32), // Segment.
11462 Zero,
11463 Chain
11464 };
11465 SDNode *Res =
11466 DAG.getMachineNode(X86::OR32mrLocked, dl, MVT::Other, Ops,
11467 array_lengthof(Ops));
11468 return SDValue(Res, 0);
11469 }
11470
11471 // MEMBARRIER is a compiler barrier; it codegens to a no-op.
11472 return DAG.getNode(X86ISD::MEMBARRIER, dl, MVT::Other, Op.getOperand(0));
11473}
11474
11475
Craig Topper55b24052012-09-11 06:15:32 +000011476static SDValue LowerCMP_SWAP(SDValue Op, const X86Subtarget *Subtarget,
11477 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +000011478 EVT T = Op.getValueType();
Chris Lattner93c4a5b2010-09-21 23:59:42 +000011479 DebugLoc DL = Op.getDebugLoc();
Andrew Lenhartha76e2f02008-03-04 21:13:33 +000011480 unsigned Reg = 0;
11481 unsigned size = 0;
Owen Anderson825b72b2009-08-11 20:47:22 +000011482 switch(T.getSimpleVT().SimpleTy) {
Craig Topperabb94d02012-02-05 03:43:23 +000011483 default: llvm_unreachable("Invalid value type!");
Owen Anderson825b72b2009-08-11 20:47:22 +000011484 case MVT::i8: Reg = X86::AL; size = 1; break;
11485 case MVT::i16: Reg = X86::AX; size = 2; break;
11486 case MVT::i32: Reg = X86::EAX; size = 4; break;
11487 case MVT::i64:
Duncan Sands1607f052008-12-01 11:39:25 +000011488 assert(Subtarget->is64Bit() && "Node not type legal!");
11489 Reg = X86::RAX; size = 8;
Andrew Lenharthd19189e2008-03-05 01:15:49 +000011490 break;
Bill Wendling61edeb52008-12-02 01:06:39 +000011491 }
Chris Lattner93c4a5b2010-09-21 23:59:42 +000011492 SDValue cpIn = DAG.getCopyToReg(Op.getOperand(0), DL, Reg,
Dale Johannesend18a4622008-09-11 03:12:59 +000011493 Op.getOperand(2), SDValue());
Dan Gohman475871a2008-07-27 21:46:04 +000011494 SDValue Ops[] = { cpIn.getValue(0),
Evan Cheng8a186ae2008-09-24 23:26:36 +000011495 Op.getOperand(1),
11496 Op.getOperand(3),
Owen Anderson825b72b2009-08-11 20:47:22 +000011497 DAG.getTargetConstant(size, MVT::i8),
Evan Cheng8a186ae2008-09-24 23:26:36 +000011498 cpIn.getValue(1) };
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +000011499 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Chris Lattner93c4a5b2010-09-21 23:59:42 +000011500 MachineMemOperand *MMO = cast<AtomicSDNode>(Op)->getMemOperand();
11501 SDValue Result = DAG.getMemIntrinsicNode(X86ISD::LCMPXCHG_DAG, DL, Tys,
11502 Ops, 5, T, MMO);
Scott Michelfdc40a02009-02-17 22:15:04 +000011503 SDValue cpOut =
Chris Lattner93c4a5b2010-09-21 23:59:42 +000011504 DAG.getCopyFromReg(Result.getValue(0), DL, Reg, T, Result.getValue(1));
Andrew Lenharth26ed8692008-03-01 21:52:34 +000011505 return cpOut;
11506}
11507
Craig Topper55b24052012-09-11 06:15:32 +000011508static SDValue LowerREADCYCLECOUNTER(SDValue Op, const X86Subtarget *Subtarget,
11509 SelectionDAG &DAG) {
Duncan Sands1607f052008-12-01 11:39:25 +000011510 assert(Subtarget->is64Bit() && "Result not type legalized?");
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +000011511 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Duncan Sands1607f052008-12-01 11:39:25 +000011512 SDValue TheChain = Op.getOperand(0);
Dale Johannesen6f38cb62009-02-07 19:59:05 +000011513 DebugLoc dl = Op.getDebugLoc();
Dale Johannesene4d209d2009-02-03 20:21:25 +000011514 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, dl, Tys, &TheChain, 1);
Owen Anderson825b72b2009-08-11 20:47:22 +000011515 SDValue rax = DAG.getCopyFromReg(rd, dl, X86::RAX, MVT::i64, rd.getValue(1));
11516 SDValue rdx = DAG.getCopyFromReg(rax.getValue(1), dl, X86::RDX, MVT::i64,
Duncan Sands1607f052008-12-01 11:39:25 +000011517 rax.getValue(2));
Owen Anderson825b72b2009-08-11 20:47:22 +000011518 SDValue Tmp = DAG.getNode(ISD::SHL, dl, MVT::i64, rdx,
11519 DAG.getConstant(32, MVT::i8));
Duncan Sands1607f052008-12-01 11:39:25 +000011520 SDValue Ops[] = {
Owen Anderson825b72b2009-08-11 20:47:22 +000011521 DAG.getNode(ISD::OR, dl, MVT::i64, rax, Tmp),
Duncan Sands1607f052008-12-01 11:39:25 +000011522 rdx.getValue(1)
11523 };
Dale Johannesene4d209d2009-02-03 20:21:25 +000011524 return DAG.getMergeValues(Ops, 2, dl);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011525}
11526
Craig Topper55b24052012-09-11 06:15:32 +000011527SDValue X86TargetLowering::LowerBITCAST(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen7d07b482010-05-21 00:52:33 +000011528 EVT SrcVT = Op.getOperand(0).getValueType();
11529 EVT DstVT = Op.getValueType();
Craig Topper1accb7e2012-01-10 06:54:16 +000011530 assert(Subtarget->is64Bit() && !Subtarget->hasSSE2() &&
Chris Lattner2a786eb2010-12-19 20:19:20 +000011531 Subtarget->hasMMX() && "Unexpected custom BITCAST");
Michael J. Spencerec38de22010-10-10 22:04:20 +000011532 assert((DstVT == MVT::i64 ||
Dale Johannesen7d07b482010-05-21 00:52:33 +000011533 (DstVT.isVector() && DstVT.getSizeInBits()==64)) &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +000011534 "Unexpected custom BITCAST");
Dale Johannesen7d07b482010-05-21 00:52:33 +000011535 // i64 <=> MMX conversions are Legal.
11536 if (SrcVT==MVT::i64 && DstVT.isVector())
11537 return Op;
11538 if (DstVT==MVT::i64 && SrcVT.isVector())
11539 return Op;
Dale Johannesene39859a2010-05-21 18:40:15 +000011540 // MMX <=> MMX conversions are Legal.
11541 if (SrcVT.isVector() && DstVT.isVector())
11542 return Op;
Dale Johannesen7d07b482010-05-21 00:52:33 +000011543 // All other conversions need to be expanded.
11544 return SDValue();
11545}
Chris Lattner5b856542010-12-20 00:59:46 +000011546
Craig Topper55b24052012-09-11 06:15:32 +000011547static SDValue LowerLOAD_SUB(SDValue Op, SelectionDAG &DAG) {
Dale Johannesen71d1bf52008-09-29 22:25:26 +000011548 SDNode *Node = Op.getNode();
Dale Johannesene4d209d2009-02-03 20:21:25 +000011549 DebugLoc dl = Node->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +000011550 EVT T = Node->getValueType(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +000011551 SDValue negOp = DAG.getNode(ISD::SUB, dl, T,
Evan Cheng242b38b2009-02-23 09:03:22 +000011552 DAG.getConstant(0, T), Node->getOperand(2));
Dale Johannesene4d209d2009-02-03 20:21:25 +000011553 return DAG.getAtomic(ISD::ATOMIC_LOAD_ADD, dl,
Dan Gohman0b1d4a72008-12-23 21:37:04 +000011554 cast<AtomicSDNode>(Node)->getMemoryVT(),
Dale Johannesen71d1bf52008-09-29 22:25:26 +000011555 Node->getOperand(0),
11556 Node->getOperand(1), negOp,
11557 cast<AtomicSDNode>(Node)->getSrcValue(),
Eli Friedman55ba8162011-07-29 03:05:32 +000011558 cast<AtomicSDNode>(Node)->getAlignment(),
11559 cast<AtomicSDNode>(Node)->getOrdering(),
11560 cast<AtomicSDNode>(Node)->getSynchScope());
Mon P Wang63307c32008-05-05 19:05:59 +000011561}
11562
Eli Friedman327236c2011-08-24 20:50:09 +000011563static SDValue LowerATOMIC_STORE(SDValue Op, SelectionDAG &DAG) {
11564 SDNode *Node = Op.getNode();
11565 DebugLoc dl = Node->getDebugLoc();
Eli Friedmanf8f90f02011-08-24 22:33:28 +000011566 EVT VT = cast<AtomicSDNode>(Node)->getMemoryVT();
Eli Friedman327236c2011-08-24 20:50:09 +000011567
11568 // Convert seq_cst store -> xchg
Eli Friedmanf8f90f02011-08-24 22:33:28 +000011569 // Convert wide store -> swap (-> cmpxchg8b/cmpxchg16b)
11570 // FIXME: On 32-bit, store -> fist or movq would be more efficient
11571 // (The only way to get a 16-byte store is cmpxchg16b)
11572 // FIXME: 16-byte ATOMIC_SWAP isn't actually hooked up at the moment.
11573 if (cast<AtomicSDNode>(Node)->getOrdering() == SequentiallyConsistent ||
11574 !DAG.getTargetLoweringInfo().isTypeLegal(VT)) {
Eli Friedman4317fe12011-08-24 21:17:30 +000011575 SDValue Swap = DAG.getAtomic(ISD::ATOMIC_SWAP, dl,
11576 cast<AtomicSDNode>(Node)->getMemoryVT(),
11577 Node->getOperand(0),
11578 Node->getOperand(1), Node->getOperand(2),
Eli Friedmanf8f90f02011-08-24 22:33:28 +000011579 cast<AtomicSDNode>(Node)->getMemOperand(),
Eli Friedman4317fe12011-08-24 21:17:30 +000011580 cast<AtomicSDNode>(Node)->getOrdering(),
11581 cast<AtomicSDNode>(Node)->getSynchScope());
Eli Friedman327236c2011-08-24 20:50:09 +000011582 return Swap.getValue(1);
11583 }
11584 // Other atomic stores have a simple pattern.
11585 return Op;
11586}
11587
Chris Lattner5b856542010-12-20 00:59:46 +000011588static SDValue LowerADDC_ADDE_SUBC_SUBE(SDValue Op, SelectionDAG &DAG) {
11589 EVT VT = Op.getNode()->getValueType(0);
11590
11591 // Let legalize expand this if it isn't a legal type yet.
11592 if (!DAG.getTargetLoweringInfo().isTypeLegal(VT))
11593 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011594
Chris Lattner5b856542010-12-20 00:59:46 +000011595 SDVTList VTs = DAG.getVTList(VT, MVT::i32);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011596
Chris Lattner5b856542010-12-20 00:59:46 +000011597 unsigned Opc;
11598 bool ExtraOp = false;
11599 switch (Op.getOpcode()) {
Craig Topperabb94d02012-02-05 03:43:23 +000011600 default: llvm_unreachable("Invalid code");
Chris Lattner5b856542010-12-20 00:59:46 +000011601 case ISD::ADDC: Opc = X86ISD::ADD; break;
11602 case ISD::ADDE: Opc = X86ISD::ADC; ExtraOp = true; break;
11603 case ISD::SUBC: Opc = X86ISD::SUB; break;
11604 case ISD::SUBE: Opc = X86ISD::SBB; ExtraOp = true; break;
11605 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011606
Chris Lattner5b856542010-12-20 00:59:46 +000011607 if (!ExtraOp)
11608 return DAG.getNode(Opc, Op->getDebugLoc(), VTs, Op.getOperand(0),
11609 Op.getOperand(1));
11610 return DAG.getNode(Opc, Op->getDebugLoc(), VTs, Op.getOperand(0),
11611 Op.getOperand(1), Op.getOperand(2));
11612}
11613
Evan Cheng0db9fe62006-04-25 20:13:52 +000011614/// LowerOperation - Provide custom lowering hooks for some operations.
11615///
Dan Gohmand858e902010-04-17 15:26:15 +000011616SDValue X86TargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +000011617 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +000011618 default: llvm_unreachable("Should not custom lower this!");
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000011619 case ISD::SIGN_EXTEND_INREG: return LowerSIGN_EXTEND_INREG(Op,DAG);
Craig Topper55b24052012-09-11 06:15:32 +000011620 case ISD::MEMBARRIER: return LowerMEMBARRIER(Op, Subtarget, DAG);
11621 case ISD::ATOMIC_FENCE: return LowerATOMIC_FENCE(Op, Subtarget, DAG);
11622 case ISD::ATOMIC_CMP_SWAP: return LowerCMP_SWAP(Op, Subtarget, DAG);
Dan Gohman0b1d4a72008-12-23 21:37:04 +000011623 case ISD::ATOMIC_LOAD_SUB: return LowerLOAD_SUB(Op,DAG);
Eli Friedman327236c2011-08-24 20:50:09 +000011624 case ISD::ATOMIC_STORE: return LowerATOMIC_STORE(Op,DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000011625 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
Mon P Wangeb38ebf2010-01-24 00:05:03 +000011626 case ISD::CONCAT_VECTORS: return LowerCONCAT_VECTORS(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000011627 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
11628 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
11629 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
Craig Topper55b24052012-09-11 06:15:32 +000011630 case ISD::EXTRACT_SUBVECTOR: return LowerEXTRACT_SUBVECTOR(Op,Subtarget,DAG);
11631 case ISD::INSERT_SUBVECTOR: return LowerINSERT_SUBVECTOR(Op, Subtarget,DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000011632 case ISD::SCALAR_TO_VECTOR: return LowerSCALAR_TO_VECTOR(Op, DAG);
11633 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
11634 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +000011635 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Bill Wendling056292f2008-09-16 21:48:12 +000011636 case ISD::ExternalSymbol: return LowerExternalSymbol(Op, DAG);
Dan Gohmanf705adb2009-10-30 01:28:02 +000011637 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000011638 case ISD::SHL_PARTS:
11639 case ISD::SRA_PARTS:
Nadav Rotem43012222011-05-11 08:12:09 +000011640 case ISD::SRL_PARTS: return LowerShiftParts(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000011641 case ISD::SINT_TO_FP: return LowerSINT_TO_FP(Op, DAG);
Dale Johannesen1c15bf52008-10-21 20:50:01 +000011642 case ISD::UINT_TO_FP: return LowerUINT_TO_FP(Op, DAG);
Michael Liaobedcbd42012-10-16 18:14:11 +000011643 case ISD::TRUNCATE: return lowerTRUNCATE(Op, DAG);
Michael Liaoa7554632012-10-23 17:36:08 +000011644 case ISD::ZERO_EXTEND: return lowerZERO_EXTEND(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000011645 case ISD::FP_TO_SINT: return LowerFP_TO_SINT(Op, DAG);
Eli Friedman948e95a2009-05-23 09:59:16 +000011646 case ISD::FP_TO_UINT: return LowerFP_TO_UINT(Op, DAG);
Michael Liao9d796db2012-10-10 16:32:15 +000011647 case ISD::FP_EXTEND: return lowerFP_EXTEND(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000011648 case ISD::FABS: return LowerFABS(Op, DAG);
11649 case ISD::FNEG: return LowerFNEG(Op, DAG);
Evan Cheng68c47cb2007-01-05 07:55:56 +000011650 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Stuart Hastings4fd0dee2011-06-01 04:39:42 +000011651 case ISD::FGETSIGN: return LowerFGETSIGN(Op, DAG);
Evan Chenge5f62042007-09-29 00:00:36 +000011652 case ISD::SETCC: return LowerSETCC(Op, DAG);
11653 case ISD::SELECT: return LowerSELECT(Op, DAG);
11654 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000011655 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000011656 case ISD::VASTART: return LowerVASTART(Op, DAG);
Dan Gohman9018e832008-05-10 01:26:14 +000011657 case ISD::VAARG: return LowerVAARG(Op, DAG);
Craig Topper55b24052012-09-11 06:15:32 +000011658 case ISD::VACOPY: return LowerVACOPY(Op, Subtarget, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000011659 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
Benjamin Kramerb9bee042012-07-12 09:31:43 +000011660 case ISD::INTRINSIC_W_CHAIN: return LowerINTRINSIC_W_CHAIN(Op, DAG);
Nate Begemanbcc5f362007-01-29 22:58:52 +000011661 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
11662 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Anton Korobeynikov2365f512007-07-14 14:06:15 +000011663 case ISD::FRAME_TO_ARGS_OFFSET:
11664 return LowerFRAME_TO_ARGS_OFFSET(Op, DAG);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +000011665 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
Anton Korobeynikov2365f512007-07-14 14:06:15 +000011666 case ISD::EH_RETURN: return LowerEH_RETURN(Op, DAG);
Michael Liao6c0e04c2012-10-15 22:39:43 +000011667 case ISD::EH_SJLJ_SETJMP: return lowerEH_SJLJ_SETJMP(Op, DAG);
11668 case ISD::EH_SJLJ_LONGJMP: return lowerEH_SJLJ_LONGJMP(Op, DAG);
Duncan Sands4a544a72011-09-06 13:37:06 +000011669 case ISD::INIT_TRAMPOLINE: return LowerINIT_TRAMPOLINE(Op, DAG);
11670 case ISD::ADJUST_TRAMPOLINE: return LowerADJUST_TRAMPOLINE(Op, DAG);
Dan Gohman1a024862008-01-31 00:41:03 +000011671 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
Evan Cheng18efe262007-12-14 02:13:44 +000011672 case ISD::CTLZ: return LowerCTLZ(Op, DAG);
Chandler Carruthacc068e2011-12-24 10:55:54 +000011673 case ISD::CTLZ_ZERO_UNDEF: return LowerCTLZ_ZERO_UNDEF(Op, DAG);
Evan Cheng18efe262007-12-14 02:13:44 +000011674 case ISD::CTTZ: return LowerCTTZ(Op, DAG);
Craig Topper55b24052012-09-11 06:15:32 +000011675 case ISD::MUL: return LowerMUL(Op, Subtarget, DAG);
Nadav Rotem43012222011-05-11 08:12:09 +000011676 case ISD::SRA:
11677 case ISD::SRL:
11678 case ISD::SHL: return LowerShift(Op, DAG);
Bill Wendling74c37652008-12-09 22:08:41 +000011679 case ISD::SADDO:
11680 case ISD::UADDO:
11681 case ISD::SSUBO:
11682 case ISD::USUBO:
11683 case ISD::SMULO:
11684 case ISD::UMULO: return LowerXALUO(Op, DAG);
Craig Topper55b24052012-09-11 06:15:32 +000011685 case ISD::READCYCLECOUNTER: return LowerREADCYCLECOUNTER(Op, Subtarget,DAG);
Wesley Peckbf17cfa2010-11-23 03:31:01 +000011686 case ISD::BITCAST: return LowerBITCAST(Op, DAG);
Chris Lattner5b856542010-12-20 00:59:46 +000011687 case ISD::ADDC:
11688 case ISD::ADDE:
11689 case ISD::SUBC:
11690 case ISD::SUBE: return LowerADDC_ADDE_SUBC_SUBE(Op, DAG);
Craig Topper13894fa2011-08-24 06:14:18 +000011691 case ISD::ADD: return LowerADD(Op, DAG);
11692 case ISD::SUB: return LowerSUB(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000011693 }
Chris Lattner27a6c732007-11-24 07:07:01 +000011694}
11695
Eli Friedmanf8f90f02011-08-24 22:33:28 +000011696static void ReplaceATOMIC_LOAD(SDNode *Node,
11697 SmallVectorImpl<SDValue> &Results,
11698 SelectionDAG &DAG) {
11699 DebugLoc dl = Node->getDebugLoc();
11700 EVT VT = cast<AtomicSDNode>(Node)->getMemoryVT();
11701
11702 // Convert wide load -> cmpxchg8b/cmpxchg16b
11703 // FIXME: On 32-bit, load -> fild or movq would be more efficient
11704 // (The only way to get a 16-byte load is cmpxchg16b)
11705 // FIXME: 16-byte ATOMIC_CMP_SWAP isn't actually hooked up at the moment.
Benjamin Kramer2753ae32011-08-27 17:36:14 +000011706 SDValue Zero = DAG.getConstant(0, VT);
11707 SDValue Swap = DAG.getAtomic(ISD::ATOMIC_CMP_SWAP, dl, VT,
Eli Friedmanf8f90f02011-08-24 22:33:28 +000011708 Node->getOperand(0),
11709 Node->getOperand(1), Zero, Zero,
11710 cast<AtomicSDNode>(Node)->getMemOperand(),
11711 cast<AtomicSDNode>(Node)->getOrdering(),
11712 cast<AtomicSDNode>(Node)->getSynchScope());
11713 Results.push_back(Swap.getValue(0));
11714 Results.push_back(Swap.getValue(1));
11715}
11716
Craig Topperc0878702012-08-17 06:55:11 +000011717static void
Duncan Sands1607f052008-12-01 11:39:25 +000011718ReplaceATOMIC_BINARY_64(SDNode *Node, SmallVectorImpl<SDValue>&Results,
Craig Topperc0878702012-08-17 06:55:11 +000011719 SelectionDAG &DAG, unsigned NewOp) {
Dale Johannesene4d209d2009-02-03 20:21:25 +000011720 DebugLoc dl = Node->getDebugLoc();
Duncan Sands17001ce2011-10-18 12:44:00 +000011721 assert (Node->getValueType(0) == MVT::i64 &&
11722 "Only know how to expand i64 atomics");
Duncan Sands1607f052008-12-01 11:39:25 +000011723
11724 SDValue Chain = Node->getOperand(0);
11725 SDValue In1 = Node->getOperand(1);
Owen Anderson825b72b2009-08-11 20:47:22 +000011726 SDValue In2L = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +000011727 Node->getOperand(2), DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +000011728 SDValue In2H = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +000011729 Node->getOperand(2), DAG.getIntPtrConstant(1));
Dan Gohmanc76909a2009-09-25 20:36:54 +000011730 SDValue Ops[] = { Chain, In1, In2L, In2H };
Owen Anderson825b72b2009-08-11 20:47:22 +000011731 SDVTList Tys = DAG.getVTList(MVT::i32, MVT::i32, MVT::Other);
Dan Gohmanc76909a2009-09-25 20:36:54 +000011732 SDValue Result =
11733 DAG.getMemIntrinsicNode(NewOp, dl, Tys, Ops, 4, MVT::i64,
11734 cast<MemSDNode>(Node)->getMemOperand());
Duncan Sands1607f052008-12-01 11:39:25 +000011735 SDValue OpsF[] = { Result.getValue(0), Result.getValue(1)};
Owen Anderson825b72b2009-08-11 20:47:22 +000011736 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, OpsF, 2));
Duncan Sands1607f052008-12-01 11:39:25 +000011737 Results.push_back(Result.getValue(2));
11738}
11739
Duncan Sands126d9072008-07-04 11:47:58 +000011740/// ReplaceNodeResults - Replace a node with an illegal result type
11741/// with a new node built out of custom code.
Duncan Sands1607f052008-12-01 11:39:25 +000011742void X86TargetLowering::ReplaceNodeResults(SDNode *N,
11743 SmallVectorImpl<SDValue>&Results,
Dan Gohmand858e902010-04-17 15:26:15 +000011744 SelectionDAG &DAG) const {
Dale Johannesene4d209d2009-02-03 20:21:25 +000011745 DebugLoc dl = N->getDebugLoc();
Chris Lattner27a6c732007-11-24 07:07:01 +000011746 switch (N->getOpcode()) {
Duncan Sandsed294c42008-10-20 15:56:33 +000011747 default:
Craig Topperabb94d02012-02-05 03:43:23 +000011748 llvm_unreachable("Do not know how to custom type legalize this operation!");
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000011749 case ISD::SIGN_EXTEND_INREG:
Chris Lattner5b856542010-12-20 00:59:46 +000011750 case ISD::ADDC:
11751 case ISD::ADDE:
11752 case ISD::SUBC:
11753 case ISD::SUBE:
11754 // We don't want to expand or promote these.
11755 return;
Michael J. Spencer1a2d0612012-02-24 19:01:22 +000011756 case ISD::FP_TO_SINT:
11757 case ISD::FP_TO_UINT: {
11758 bool IsSigned = N->getOpcode() == ISD::FP_TO_SINT;
11759
11760 if (!IsSigned && !isIntegerTypeFTOL(SDValue(N, 0).getValueType()))
11761 return;
11762
Eli Friedman948e95a2009-05-23 09:59:16 +000011763 std::pair<SDValue,SDValue> Vals =
NAKAMURA Takumi9a68fdc2012-02-25 03:37:25 +000011764 FP_TO_INTHelper(SDValue(N, 0), DAG, IsSigned, /*IsReplace=*/ true);
Duncan Sands1607f052008-12-01 11:39:25 +000011765 SDValue FIST = Vals.first, StackSlot = Vals.second;
11766 if (FIST.getNode() != 0) {
Owen Andersone50ed302009-08-10 22:56:29 +000011767 EVT VT = N->getValueType(0);
Duncan Sands1607f052008-12-01 11:39:25 +000011768 // Return a load from the stack slot.
Michael J. Spencer1a2d0612012-02-24 19:01:22 +000011769 if (StackSlot.getNode() != 0)
11770 Results.push_back(DAG.getLoad(VT, dl, FIST, StackSlot,
11771 MachinePointerInfo(),
11772 false, false, false, 0));
11773 else
11774 Results.push_back(FIST);
Duncan Sands1607f052008-12-01 11:39:25 +000011775 }
11776 return;
11777 }
Michael Liao991b6a22012-10-24 04:09:32 +000011778 case ISD::UINT_TO_FP: {
11779 if (N->getOperand(0).getValueType() != MVT::v2i32 &&
11780 N->getValueType(0) != MVT::v2f32)
11781 return;
11782 SDValue ZExtIn = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v2i64,
11783 N->getOperand(0));
11784 SDValue Bias = DAG.getConstantFP(BitsToDouble(0x4330000000000000ULL),
11785 MVT::f64);
11786 SDValue VBias = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v2f64, Bias, Bias);
11787 SDValue Or = DAG.getNode(ISD::OR, dl, MVT::v2i64, ZExtIn,
11788 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64, VBias));
11789 Or = DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Or);
11790 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::v2f64, Or, VBias);
11791 Results.push_back(DAG.getNode(X86ISD::VFPROUND, dl, MVT::v4f32, Sub));
11792 return;
11793 }
Michael Liao44c2d612012-10-10 16:53:28 +000011794 case ISD::FP_ROUND: {
11795 SDValue V = DAG.getNode(X86ISD::VFPROUND, dl, MVT::v4f32, N->getOperand(0));
11796 Results.push_back(V);
11797 return;
11798 }
Duncan Sands1607f052008-12-01 11:39:25 +000011799 case ISD::READCYCLECOUNTER: {
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +000011800 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Duncan Sands1607f052008-12-01 11:39:25 +000011801 SDValue TheChain = N->getOperand(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +000011802 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, dl, Tys, &TheChain, 1);
Owen Anderson825b72b2009-08-11 20:47:22 +000011803 SDValue eax = DAG.getCopyFromReg(rd, dl, X86::EAX, MVT::i32,
Dale Johannesendd64c412009-02-04 00:33:20 +000011804 rd.getValue(1));
Owen Anderson825b72b2009-08-11 20:47:22 +000011805 SDValue edx = DAG.getCopyFromReg(eax.getValue(1), dl, X86::EDX, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +000011806 eax.getValue(2));
11807 // Use a buildpair to merge the two 32-bit values into a 64-bit one.
11808 SDValue Ops[] = { eax, edx };
Owen Anderson825b72b2009-08-11 20:47:22 +000011809 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Ops, 2));
Duncan Sands1607f052008-12-01 11:39:25 +000011810 Results.push_back(edx.getValue(1));
11811 return;
11812 }
Dan Gohman0b1d4a72008-12-23 21:37:04 +000011813 case ISD::ATOMIC_CMP_SWAP: {
Owen Andersone50ed302009-08-10 22:56:29 +000011814 EVT T = N->getValueType(0);
Benjamin Kramer2753ae32011-08-27 17:36:14 +000011815 assert((T == MVT::i64 || T == MVT::i128) && "can only expand cmpxchg pair");
Eli Friedman43f51ae2011-08-26 21:21:21 +000011816 bool Regs64bit = T == MVT::i128;
11817 EVT HalfT = Regs64bit ? MVT::i64 : MVT::i32;
Duncan Sands1607f052008-12-01 11:39:25 +000011818 SDValue cpInL, cpInH;
Eli Friedman43f51ae2011-08-26 21:21:21 +000011819 cpInL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, HalfT, N->getOperand(2),
11820 DAG.getConstant(0, HalfT));
11821 cpInH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, HalfT, N->getOperand(2),
11822 DAG.getConstant(1, HalfT));
11823 cpInL = DAG.getCopyToReg(N->getOperand(0), dl,
11824 Regs64bit ? X86::RAX : X86::EAX,
11825 cpInL, SDValue());
11826 cpInH = DAG.getCopyToReg(cpInL.getValue(0), dl,
11827 Regs64bit ? X86::RDX : X86::EDX,
11828 cpInH, cpInL.getValue(1));
Duncan Sands1607f052008-12-01 11:39:25 +000011829 SDValue swapInL, swapInH;
Eli Friedman43f51ae2011-08-26 21:21:21 +000011830 swapInL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, HalfT, N->getOperand(3),
11831 DAG.getConstant(0, HalfT));
11832 swapInH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, HalfT, N->getOperand(3),
11833 DAG.getConstant(1, HalfT));
11834 swapInL = DAG.getCopyToReg(cpInH.getValue(0), dl,
11835 Regs64bit ? X86::RBX : X86::EBX,
11836 swapInL, cpInH.getValue(1));
11837 swapInH = DAG.getCopyToReg(swapInL.getValue(0), dl,
Chad Rosiera20e1e72012-08-01 18:39:17 +000011838 Regs64bit ? X86::RCX : X86::ECX,
Eli Friedman43f51ae2011-08-26 21:21:21 +000011839 swapInH, swapInL.getValue(1));
Duncan Sands1607f052008-12-01 11:39:25 +000011840 SDValue Ops[] = { swapInH.getValue(0),
11841 N->getOperand(1),
11842 swapInH.getValue(1) };
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +000011843 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Andrew Trick1a2cf3b2010-10-11 19:02:04 +000011844 MachineMemOperand *MMO = cast<AtomicSDNode>(N)->getMemOperand();
Eli Friedman43f51ae2011-08-26 21:21:21 +000011845 unsigned Opcode = Regs64bit ? X86ISD::LCMPXCHG16_DAG :
11846 X86ISD::LCMPXCHG8_DAG;
11847 SDValue Result = DAG.getMemIntrinsicNode(Opcode, dl, Tys,
Andrew Trick1a2cf3b2010-10-11 19:02:04 +000011848 Ops, 3, T, MMO);
Eli Friedman43f51ae2011-08-26 21:21:21 +000011849 SDValue cpOutL = DAG.getCopyFromReg(Result.getValue(0), dl,
11850 Regs64bit ? X86::RAX : X86::EAX,
11851 HalfT, Result.getValue(1));
11852 SDValue cpOutH = DAG.getCopyFromReg(cpOutL.getValue(1), dl,
11853 Regs64bit ? X86::RDX : X86::EDX,
11854 HalfT, cpOutL.getValue(2));
Duncan Sands1607f052008-12-01 11:39:25 +000011855 SDValue OpsF[] = { cpOutL.getValue(0), cpOutH.getValue(0)};
Eli Friedman43f51ae2011-08-26 21:21:21 +000011856 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, T, OpsF, 2));
Duncan Sands1607f052008-12-01 11:39:25 +000011857 Results.push_back(cpOutH.getValue(1));
11858 return;
11859 }
Dan Gohman0b1d4a72008-12-23 21:37:04 +000011860 case ISD::ATOMIC_LOAD_ADD:
Dan Gohman0b1d4a72008-12-23 21:37:04 +000011861 case ISD::ATOMIC_LOAD_AND:
Dan Gohman0b1d4a72008-12-23 21:37:04 +000011862 case ISD::ATOMIC_LOAD_NAND:
Dan Gohman0b1d4a72008-12-23 21:37:04 +000011863 case ISD::ATOMIC_LOAD_OR:
Dan Gohman0b1d4a72008-12-23 21:37:04 +000011864 case ISD::ATOMIC_LOAD_SUB:
Dan Gohman0b1d4a72008-12-23 21:37:04 +000011865 case ISD::ATOMIC_LOAD_XOR:
Michael Liaoe5e8f762012-09-25 18:08:13 +000011866 case ISD::ATOMIC_LOAD_MAX:
11867 case ISD::ATOMIC_LOAD_MIN:
11868 case ISD::ATOMIC_LOAD_UMAX:
11869 case ISD::ATOMIC_LOAD_UMIN:
Craig Topperc0878702012-08-17 06:55:11 +000011870 case ISD::ATOMIC_SWAP: {
11871 unsigned Opc;
11872 switch (N->getOpcode()) {
11873 default: llvm_unreachable("Unexpected opcode");
11874 case ISD::ATOMIC_LOAD_ADD:
11875 Opc = X86ISD::ATOMADD64_DAG;
11876 break;
11877 case ISD::ATOMIC_LOAD_AND:
11878 Opc = X86ISD::ATOMAND64_DAG;
11879 break;
11880 case ISD::ATOMIC_LOAD_NAND:
11881 Opc = X86ISD::ATOMNAND64_DAG;
11882 break;
11883 case ISD::ATOMIC_LOAD_OR:
11884 Opc = X86ISD::ATOMOR64_DAG;
11885 break;
11886 case ISD::ATOMIC_LOAD_SUB:
11887 Opc = X86ISD::ATOMSUB64_DAG;
11888 break;
11889 case ISD::ATOMIC_LOAD_XOR:
11890 Opc = X86ISD::ATOMXOR64_DAG;
11891 break;
Michael Liaoe5e8f762012-09-25 18:08:13 +000011892 case ISD::ATOMIC_LOAD_MAX:
11893 Opc = X86ISD::ATOMMAX64_DAG;
11894 break;
11895 case ISD::ATOMIC_LOAD_MIN:
11896 Opc = X86ISD::ATOMMIN64_DAG;
11897 break;
11898 case ISD::ATOMIC_LOAD_UMAX:
11899 Opc = X86ISD::ATOMUMAX64_DAG;
11900 break;
11901 case ISD::ATOMIC_LOAD_UMIN:
11902 Opc = X86ISD::ATOMUMIN64_DAG;
11903 break;
Craig Topperc0878702012-08-17 06:55:11 +000011904 case ISD::ATOMIC_SWAP:
11905 Opc = X86ISD::ATOMSWAP64_DAG;
11906 break;
11907 }
11908 ReplaceATOMIC_BINARY_64(N, Results, DAG, Opc);
Duncan Sands1607f052008-12-01 11:39:25 +000011909 return;
Craig Topperc0878702012-08-17 06:55:11 +000011910 }
Eli Friedmanf8f90f02011-08-24 22:33:28 +000011911 case ISD::ATOMIC_LOAD:
11912 ReplaceATOMIC_LOAD(N, Results, DAG);
Chris Lattner27a6c732007-11-24 07:07:01 +000011913 }
Evan Cheng0db9fe62006-04-25 20:13:52 +000011914}
11915
Evan Cheng72261582005-12-20 06:22:03 +000011916const char *X86TargetLowering::getTargetNodeName(unsigned Opcode) const {
11917 switch (Opcode) {
11918 default: return NULL;
Evan Cheng18efe262007-12-14 02:13:44 +000011919 case X86ISD::BSF: return "X86ISD::BSF";
11920 case X86ISD::BSR: return "X86ISD::BSR";
Evan Chenge3413162006-01-09 18:33:28 +000011921 case X86ISD::SHLD: return "X86ISD::SHLD";
11922 case X86ISD::SHRD: return "X86ISD::SHRD";
Evan Chengef6ffb12006-01-31 03:14:29 +000011923 case X86ISD::FAND: return "X86ISD::FAND";
Evan Cheng68c47cb2007-01-05 07:55:56 +000011924 case X86ISD::FOR: return "X86ISD::FOR";
Evan Cheng223547a2006-01-31 22:28:30 +000011925 case X86ISD::FXOR: return "X86ISD::FXOR";
Evan Cheng68c47cb2007-01-05 07:55:56 +000011926 case X86ISD::FSRL: return "X86ISD::FSRL";
Evan Chenga3195e82006-01-12 22:54:21 +000011927 case X86ISD::FILD: return "X86ISD::FILD";
Evan Chenge3de85b2006-02-04 02:20:30 +000011928 case X86ISD::FILD_FLAG: return "X86ISD::FILD_FLAG";
Evan Cheng72261582005-12-20 06:22:03 +000011929 case X86ISD::FP_TO_INT16_IN_MEM: return "X86ISD::FP_TO_INT16_IN_MEM";
11930 case X86ISD::FP_TO_INT32_IN_MEM: return "X86ISD::FP_TO_INT32_IN_MEM";
11931 case X86ISD::FP_TO_INT64_IN_MEM: return "X86ISD::FP_TO_INT64_IN_MEM";
Evan Chengb077b842005-12-21 02:39:21 +000011932 case X86ISD::FLD: return "X86ISD::FLD";
Evan Chengd90eb7f2006-01-05 00:27:02 +000011933 case X86ISD::FST: return "X86ISD::FST";
Evan Cheng72261582005-12-20 06:22:03 +000011934 case X86ISD::CALL: return "X86ISD::CALL";
Evan Cheng72261582005-12-20 06:22:03 +000011935 case X86ISD::RDTSC_DAG: return "X86ISD::RDTSC_DAG";
Dan Gohmanc7a37d42008-12-23 22:45:23 +000011936 case X86ISD::BT: return "X86ISD::BT";
Evan Cheng72261582005-12-20 06:22:03 +000011937 case X86ISD::CMP: return "X86ISD::CMP";
Evan Cheng6be2c582006-04-05 23:38:46 +000011938 case X86ISD::COMI: return "X86ISD::COMI";
11939 case X86ISD::UCOMI: return "X86ISD::UCOMI";
Evan Chengd5781fc2005-12-21 20:21:51 +000011940 case X86ISD::SETCC: return "X86ISD::SETCC";
Evan Chengad9c0a32009-12-15 00:53:42 +000011941 case X86ISD::SETCC_CARRY: return "X86ISD::SETCC_CARRY";
Stuart Hastings865f0932011-06-03 23:53:54 +000011942 case X86ISD::FSETCCsd: return "X86ISD::FSETCCsd";
11943 case X86ISD::FSETCCss: return "X86ISD::FSETCCss";
Evan Cheng72261582005-12-20 06:22:03 +000011944 case X86ISD::CMOV: return "X86ISD::CMOV";
11945 case X86ISD::BRCOND: return "X86ISD::BRCOND";
Evan Chengb077b842005-12-21 02:39:21 +000011946 case X86ISD::RET_FLAG: return "X86ISD::RET_FLAG";
Evan Cheng8df346b2006-03-04 01:12:00 +000011947 case X86ISD::REP_STOS: return "X86ISD::REP_STOS";
11948 case X86ISD::REP_MOVS: return "X86ISD::REP_MOVS";
Evan Cheng7ccced62006-02-18 00:15:05 +000011949 case X86ISD::GlobalBaseReg: return "X86ISD::GlobalBaseReg";
Evan Cheng020d2e82006-02-23 20:41:18 +000011950 case X86ISD::Wrapper: return "X86ISD::Wrapper";
Chris Lattner18c59872009-06-27 04:16:01 +000011951 case X86ISD::WrapperRIP: return "X86ISD::WrapperRIP";
Nate Begeman14d12ca2008-02-11 04:19:36 +000011952 case X86ISD::PEXTRB: return "X86ISD::PEXTRB";
Evan Chengb067a1e2006-03-31 19:22:53 +000011953 case X86ISD::PEXTRW: return "X86ISD::PEXTRW";
Nate Begeman14d12ca2008-02-11 04:19:36 +000011954 case X86ISD::INSERTPS: return "X86ISD::INSERTPS";
11955 case X86ISD::PINSRB: return "X86ISD::PINSRB";
Evan Cheng653159f2006-03-31 21:55:24 +000011956 case X86ISD::PINSRW: return "X86ISD::PINSRW";
Nate Begemanb9a47b82009-02-23 08:49:38 +000011957 case X86ISD::PSHUFB: return "X86ISD::PSHUFB";
Bruno Cardoso Lopesc1af4772011-07-13 21:36:47 +000011958 case X86ISD::ANDNP: return "X86ISD::ANDNP";
Craig Topper31133842011-11-19 07:33:10 +000011959 case X86ISD::PSIGN: return "X86ISD::PSIGN";
Craig Toppere6a62772011-11-13 17:31:07 +000011960 case X86ISD::BLENDV: return "X86ISD::BLENDV";
Nadav Roteme6113782012-04-11 06:40:27 +000011961 case X86ISD::BLENDPW: return "X86ISD::BLENDPW";
11962 case X86ISD::BLENDPS: return "X86ISD::BLENDPS";
11963 case X86ISD::BLENDPD: return "X86ISD::BLENDPD";
Craig Topperfe033152011-12-06 09:31:36 +000011964 case X86ISD::HADD: return "X86ISD::HADD";
11965 case X86ISD::HSUB: return "X86ISD::HSUB";
Craig Toppere6a62772011-11-13 17:31:07 +000011966 case X86ISD::FHADD: return "X86ISD::FHADD";
11967 case X86ISD::FHSUB: return "X86ISD::FHSUB";
Evan Cheng8ca29322006-11-10 21:43:37 +000011968 case X86ISD::FMAX: return "X86ISD::FMAX";
11969 case X86ISD::FMIN: return "X86ISD::FMIN";
Nadav Rotemd60cb112012-08-19 13:06:16 +000011970 case X86ISD::FMAXC: return "X86ISD::FMAXC";
11971 case X86ISD::FMINC: return "X86ISD::FMINC";
Dan Gohman20382522007-07-10 00:05:58 +000011972 case X86ISD::FRSQRT: return "X86ISD::FRSQRT";
11973 case X86ISD::FRCP: return "X86ISD::FRCP";
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +000011974 case X86ISD::TLSADDR: return "X86ISD::TLSADDR";
Hans Wennborgf0234fc2012-06-01 16:27:21 +000011975 case X86ISD::TLSBASEADDR: return "X86ISD::TLSBASEADDR";
Eric Christopher30ef0e52010-06-03 04:07:48 +000011976 case X86ISD::TLSCALL: return "X86ISD::TLSCALL";
Michael Liao6c0e04c2012-10-15 22:39:43 +000011977 case X86ISD::EH_SJLJ_SETJMP: return "X86ISD::EH_SJLJ_SETJMP";
11978 case X86ISD::EH_SJLJ_LONGJMP: return "X86ISD::EH_SJLJ_LONGJMP";
Anton Korobeynikov2365f512007-07-14 14:06:15 +000011979 case X86ISD::EH_RETURN: return "X86ISD::EH_RETURN";
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +000011980 case X86ISD::TC_RETURN: return "X86ISD::TC_RETURN";
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000011981 case X86ISD::FNSTCW16m: return "X86ISD::FNSTCW16m";
Benjamin Kramer17c836c2012-04-27 12:07:43 +000011982 case X86ISD::FNSTSW16r: return "X86ISD::FNSTSW16r";
Evan Cheng7e2ff772008-05-08 00:57:18 +000011983 case X86ISD::LCMPXCHG_DAG: return "X86ISD::LCMPXCHG_DAG";
11984 case X86ISD::LCMPXCHG8_DAG: return "X86ISD::LCMPXCHG8_DAG";
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011985 case X86ISD::ATOMADD64_DAG: return "X86ISD::ATOMADD64_DAG";
11986 case X86ISD::ATOMSUB64_DAG: return "X86ISD::ATOMSUB64_DAG";
11987 case X86ISD::ATOMOR64_DAG: return "X86ISD::ATOMOR64_DAG";
11988 case X86ISD::ATOMXOR64_DAG: return "X86ISD::ATOMXOR64_DAG";
11989 case X86ISD::ATOMAND64_DAG: return "X86ISD::ATOMAND64_DAG";
11990 case X86ISD::ATOMNAND64_DAG: return "X86ISD::ATOMNAND64_DAG";
Evan Chengd880b972008-05-09 21:53:03 +000011991 case X86ISD::VZEXT_MOVL: return "X86ISD::VZEXT_MOVL";
Michael Liaob7bf7262012-08-14 22:53:17 +000011992 case X86ISD::VSEXT_MOVL: return "X86ISD::VSEXT_MOVL";
Evan Chengd880b972008-05-09 21:53:03 +000011993 case X86ISD::VZEXT_LOAD: return "X86ISD::VZEXT_LOAD";
Michael Liaod9d09602012-10-23 17:34:00 +000011994 case X86ISD::VZEXT: return "X86ISD::VZEXT";
11995 case X86ISD::VSEXT: return "X86ISD::VSEXT";
Michael Liao7091b242012-08-14 21:24:47 +000011996 case X86ISD::VFPEXT: return "X86ISD::VFPEXT";
Michael Liao44c2d612012-10-10 16:53:28 +000011997 case X86ISD::VFPROUND: return "X86ISD::VFPROUND";
Craig Toppered2e13d2012-01-22 19:15:14 +000011998 case X86ISD::VSHLDQ: return "X86ISD::VSHLDQ";
11999 case X86ISD::VSRLDQ: return "X86ISD::VSRLDQ";
Evan Chengf26ffe92008-05-29 08:22:04 +000012000 case X86ISD::VSHL: return "X86ISD::VSHL";
12001 case X86ISD::VSRL: return "X86ISD::VSRL";
Craig Toppered2e13d2012-01-22 19:15:14 +000012002 case X86ISD::VSRA: return "X86ISD::VSRA";
12003 case X86ISD::VSHLI: return "X86ISD::VSHLI";
12004 case X86ISD::VSRLI: return "X86ISD::VSRLI";
12005 case X86ISD::VSRAI: return "X86ISD::VSRAI";
Craig Topper1906d322012-01-22 23:36:02 +000012006 case X86ISD::CMPP: return "X86ISD::CMPP";
Craig Topper67609fd2012-01-22 22:42:16 +000012007 case X86ISD::PCMPEQ: return "X86ISD::PCMPEQ";
12008 case X86ISD::PCMPGT: return "X86ISD::PCMPGT";
Bill Wendlingab55ebd2008-12-12 00:56:36 +000012009 case X86ISD::ADD: return "X86ISD::ADD";
12010 case X86ISD::SUB: return "X86ISD::SUB";
Chris Lattner5b856542010-12-20 00:59:46 +000012011 case X86ISD::ADC: return "X86ISD::ADC";
12012 case X86ISD::SBB: return "X86ISD::SBB";
Bill Wendlingd350e022008-12-12 21:15:41 +000012013 case X86ISD::SMUL: return "X86ISD::SMUL";
12014 case X86ISD::UMUL: return "X86ISD::UMUL";
Dan Gohman076aee32009-03-04 19:44:21 +000012015 case X86ISD::INC: return "X86ISD::INC";
12016 case X86ISD::DEC: return "X86ISD::DEC";
Dan Gohmane220c4b2009-09-18 19:59:53 +000012017 case X86ISD::OR: return "X86ISD::OR";
12018 case X86ISD::XOR: return "X86ISD::XOR";
12019 case X86ISD::AND: return "X86ISD::AND";
Craig Topper54a11172011-10-14 07:06:56 +000012020 case X86ISD::ANDN: return "X86ISD::ANDN";
Craig Toppere6a62772011-11-13 17:31:07 +000012021 case X86ISD::BLSI: return "X86ISD::BLSI";
12022 case X86ISD::BLSMSK: return "X86ISD::BLSMSK";
12023 case X86ISD::BLSR: return "X86ISD::BLSR";
Evan Cheng73f24c92009-03-30 21:36:47 +000012024 case X86ISD::MUL_IMM: return "X86ISD::MUL_IMM";
Eric Christopher71c67532009-07-29 00:28:05 +000012025 case X86ISD::PTEST: return "X86ISD::PTEST";
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +000012026 case X86ISD::TESTP: return "X86ISD::TESTP";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000012027 case X86ISD::PALIGN: return "X86ISD::PALIGN";
12028 case X86ISD::PSHUFD: return "X86ISD::PSHUFD";
12029 case X86ISD::PSHUFHW: return "X86ISD::PSHUFHW";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000012030 case X86ISD::PSHUFLW: return "X86ISD::PSHUFLW";
Craig Topperb3982da2011-12-31 23:50:21 +000012031 case X86ISD::SHUFP: return "X86ISD::SHUFP";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000012032 case X86ISD::MOVLHPS: return "X86ISD::MOVLHPS";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000012033 case X86ISD::MOVLHPD: return "X86ISD::MOVLHPD";
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +000012034 case X86ISD::MOVHLPS: return "X86ISD::MOVHLPS";
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +000012035 case X86ISD::MOVLPS: return "X86ISD::MOVLPS";
12036 case X86ISD::MOVLPD: return "X86ISD::MOVLPD";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000012037 case X86ISD::MOVDDUP: return "X86ISD::MOVDDUP";
12038 case X86ISD::MOVSHDUP: return "X86ISD::MOVSHDUP";
12039 case X86ISD::MOVSLDUP: return "X86ISD::MOVSLDUP";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000012040 case X86ISD::MOVSD: return "X86ISD::MOVSD";
12041 case X86ISD::MOVSS: return "X86ISD::MOVSS";
Craig Topper34671b82011-12-06 08:21:25 +000012042 case X86ISD::UNPCKL: return "X86ISD::UNPCKL";
12043 case X86ISD::UNPCKH: return "X86ISD::UNPCKH";
Bruno Cardoso Lopes0e6d2302011-08-17 02:29:19 +000012044 case X86ISD::VBROADCAST: return "X86ISD::VBROADCAST";
Craig Topper316cd2a2011-11-30 06:25:25 +000012045 case X86ISD::VPERMILP: return "X86ISD::VPERMILP";
Craig Topperec24e612011-11-30 07:47:51 +000012046 case X86ISD::VPERM2X128: return "X86ISD::VPERM2X128";
Craig Topper8325c112012-04-16 00:41:45 +000012047 case X86ISD::VPERMV: return "X86ISD::VPERMV";
12048 case X86ISD::VPERMI: return "X86ISD::VPERMI";
Craig Topper5b209e82012-02-05 03:14:49 +000012049 case X86ISD::PMULUDQ: return "X86ISD::PMULUDQ";
Dan Gohmand6708ea2009-08-15 01:38:56 +000012050 case X86ISD::VASTART_SAVE_XMM_REGS: return "X86ISD::VASTART_SAVE_XMM_REGS";
Dan Gohman320afb82010-10-12 18:00:49 +000012051 case X86ISD::VAARG_64: return "X86ISD::VAARG_64";
Michael J. Spencere9c253e2010-10-21 01:41:01 +000012052 case X86ISD::WIN_ALLOCA: return "X86ISD::WIN_ALLOCA";
Eli Friedman14648462011-07-27 22:21:52 +000012053 case X86ISD::MEMBARRIER: return "X86ISD::MEMBARRIER";
Rafael Espindolad07b7ec2011-08-30 19:43:21 +000012054 case X86ISD::SEG_ALLOCA: return "X86ISD::SEG_ALLOCA";
Michael J. Spencer1a2d0612012-02-24 19:01:22 +000012055 case X86ISD::WIN_FTOL: return "X86ISD::WIN_FTOL";
Benjamin Kramer17c836c2012-04-27 12:07:43 +000012056 case X86ISD::SAHF: return "X86ISD::SAHF";
Benjamin Kramerb9bee042012-07-12 09:31:43 +000012057 case X86ISD::RDRAND: return "X86ISD::RDRAND";
Elena Demikhovsky1503aba2012-08-01 12:06:00 +000012058 case X86ISD::FMADD: return "X86ISD::FMADD";
12059 case X86ISD::FMSUB: return "X86ISD::FMSUB";
12060 case X86ISD::FNMADD: return "X86ISD::FNMADD";
12061 case X86ISD::FNMSUB: return "X86ISD::FNMSUB";
12062 case X86ISD::FMADDSUB: return "X86ISD::FMADDSUB";
12063 case X86ISD::FMSUBADD: return "X86ISD::FMSUBADD";
Craig Topper9c7ae012012-11-10 01:23:36 +000012064 case X86ISD::PCMPESTRI: return "X86ISD::PCMPESTRI";
12065 case X86ISD::PCMPISTRI: return "X86ISD::PCMPISTRI";
Evan Cheng72261582005-12-20 06:22:03 +000012066 }
12067}
Evan Cheng3a03ebb2005-12-21 23:05:39 +000012068
Chris Lattnerc9addb72007-03-30 23:15:24 +000012069// isLegalAddressingMode - Return true if the addressing mode represented
12070// by AM is legal for this target, for a load/store of the specified type.
Scott Michelfdc40a02009-02-17 22:15:04 +000012071bool X86TargetLowering::isLegalAddressingMode(const AddrMode &AM,
Chris Lattnerdb125cf2011-07-18 04:54:35 +000012072 Type *Ty) const {
Chris Lattnerc9addb72007-03-30 23:15:24 +000012073 // X86 supports extremely general addressing modes.
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000012074 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman92b651f2010-08-24 15:55:12 +000012075 Reloc::Model R = getTargetMachine().getRelocationModel();
Scott Michelfdc40a02009-02-17 22:15:04 +000012076
Chris Lattnerc9addb72007-03-30 23:15:24 +000012077 // X86 allows a sign-extended 32-bit immediate field as a displacement.
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000012078 if (!X86::isOffsetSuitableForCodeModel(AM.BaseOffs, M, AM.BaseGV != NULL))
Chris Lattnerc9addb72007-03-30 23:15:24 +000012079 return false;
Scott Michelfdc40a02009-02-17 22:15:04 +000012080
Chris Lattnerc9addb72007-03-30 23:15:24 +000012081 if (AM.BaseGV) {
Chris Lattnerdfed4132009-07-10 07:38:24 +000012082 unsigned GVFlags =
12083 Subtarget->ClassifyGlobalReference(AM.BaseGV, getTargetMachine());
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000012084
Chris Lattnerdfed4132009-07-10 07:38:24 +000012085 // If a reference to this global requires an extra load, we can't fold it.
12086 if (isGlobalStubReference(GVFlags))
Chris Lattnerc9addb72007-03-30 23:15:24 +000012087 return false;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000012088
Chris Lattnerdfed4132009-07-10 07:38:24 +000012089 // If BaseGV requires a register for the PIC base, we cannot also have a
12090 // BaseReg specified.
12091 if (AM.HasBaseReg && isGlobalRelativeToPICBase(GVFlags))
Dale Johannesen203af582008-12-05 21:47:27 +000012092 return false;
Evan Cheng52787842007-08-01 23:46:47 +000012093
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000012094 // If lower 4G is not available, then we must use rip-relative addressing.
Dan Gohman92b651f2010-08-24 15:55:12 +000012095 if ((M != CodeModel::Small || R != Reloc::Static) &&
12096 Subtarget->is64Bit() && (AM.BaseOffs || AM.Scale > 1))
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000012097 return false;
Chris Lattnerc9addb72007-03-30 23:15:24 +000012098 }
Scott Michelfdc40a02009-02-17 22:15:04 +000012099
Chris Lattnerc9addb72007-03-30 23:15:24 +000012100 switch (AM.Scale) {
12101 case 0:
12102 case 1:
12103 case 2:
12104 case 4:
12105 case 8:
12106 // These scales always work.
12107 break;
12108 case 3:
12109 case 5:
12110 case 9:
12111 // These scales are formed with basereg+scalereg. Only accept if there is
12112 // no basereg yet.
12113 if (AM.HasBaseReg)
12114 return false;
12115 break;
12116 default: // Other stuff never works.
12117 return false;
12118 }
Scott Michelfdc40a02009-02-17 22:15:04 +000012119
Chris Lattnerc9addb72007-03-30 23:15:24 +000012120 return true;
12121}
12122
12123
Chris Lattnerdb125cf2011-07-18 04:54:35 +000012124bool X86TargetLowering::isTruncateFree(Type *Ty1, Type *Ty2) const {
Duncan Sandsb0bc6c32010-02-15 16:12:20 +000012125 if (!Ty1->isIntegerTy() || !Ty2->isIntegerTy())
Evan Cheng2bd122c2007-10-26 01:56:11 +000012126 return false;
Evan Chenge127a732007-10-29 07:57:50 +000012127 unsigned NumBits1 = Ty1->getPrimitiveSizeInBits();
12128 unsigned NumBits2 = Ty2->getPrimitiveSizeInBits();
Evan Cheng260e07e2008-03-20 02:18:41 +000012129 if (NumBits1 <= NumBits2)
Evan Chenge127a732007-10-29 07:57:50 +000012130 return false;
Dan Gohman377fbc02010-02-25 03:04:36 +000012131 return true;
Evan Cheng2bd122c2007-10-26 01:56:11 +000012132}
12133
Evan Cheng70e10d32012-07-17 06:53:39 +000012134bool X86TargetLowering::isLegalICmpImmediate(int64_t Imm) const {
12135 return Imm == (int32_t)Imm;
12136}
12137
12138bool X86TargetLowering::isLegalAddImmediate(int64_t Imm) const {
Evan Chenga9e13ba2012-07-17 18:54:11 +000012139 // Can also use sub to handle negated immediates.
Evan Cheng70e10d32012-07-17 06:53:39 +000012140 return Imm == (int32_t)Imm;
12141}
12142
Owen Andersone50ed302009-08-10 22:56:29 +000012143bool X86TargetLowering::isTruncateFree(EVT VT1, EVT VT2) const {
Duncan Sands83ec4b62008-06-06 12:08:01 +000012144 if (!VT1.isInteger() || !VT2.isInteger())
Evan Cheng3c3ddb32007-10-29 19:58:20 +000012145 return false;
Duncan Sands83ec4b62008-06-06 12:08:01 +000012146 unsigned NumBits1 = VT1.getSizeInBits();
12147 unsigned NumBits2 = VT2.getSizeInBits();
Evan Cheng260e07e2008-03-20 02:18:41 +000012148 if (NumBits1 <= NumBits2)
Evan Cheng3c3ddb32007-10-29 19:58:20 +000012149 return false;
Dan Gohman377fbc02010-02-25 03:04:36 +000012150 return true;
Evan Cheng3c3ddb32007-10-29 19:58:20 +000012151}
Evan Cheng2bd122c2007-10-26 01:56:11 +000012152
Chris Lattnerdb125cf2011-07-18 04:54:35 +000012153bool X86TargetLowering::isZExtFree(Type *Ty1, Type *Ty2) const {
Dan Gohman349ba492009-04-09 02:06:09 +000012154 // x86-64 implicitly zero-extends 32-bit results in 64-bit registers.
Duncan Sandsb0bc6c32010-02-15 16:12:20 +000012155 return Ty1->isIntegerTy(32) && Ty2->isIntegerTy(64) && Subtarget->is64Bit();
Dan Gohman97121ba2009-04-08 00:15:30 +000012156}
12157
Owen Andersone50ed302009-08-10 22:56:29 +000012158bool X86TargetLowering::isZExtFree(EVT VT1, EVT VT2) const {
Dan Gohman349ba492009-04-09 02:06:09 +000012159 // x86-64 implicitly zero-extends 32-bit results in 64-bit registers.
Owen Anderson825b72b2009-08-11 20:47:22 +000012160 return VT1 == MVT::i32 && VT2 == MVT::i64 && Subtarget->is64Bit();
Dan Gohman97121ba2009-04-08 00:15:30 +000012161}
12162
Owen Andersone50ed302009-08-10 22:56:29 +000012163bool X86TargetLowering::isNarrowingProfitable(EVT VT1, EVT VT2) const {
Evan Cheng8b944d32009-05-28 00:35:15 +000012164 // i16 instructions are longer (0x66 prefix) and potentially slower.
Owen Anderson825b72b2009-08-11 20:47:22 +000012165 return !(VT1 == MVT::i32 && VT2 == MVT::i16);
Evan Cheng8b944d32009-05-28 00:35:15 +000012166}
12167
Evan Cheng60c07e12006-07-05 22:17:51 +000012168/// isShuffleMaskLegal - Targets can use this to indicate that they only
12169/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
12170/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
12171/// are assumed to be legal.
12172bool
Eric Christopherfd179292009-08-27 18:07:15 +000012173X86TargetLowering::isShuffleMaskLegal(const SmallVectorImpl<int> &M,
Owen Andersone50ed302009-08-10 22:56:29 +000012174 EVT VT) const {
Eric Christophercff6f852010-04-15 01:40:20 +000012175 // Very little shuffling can be done for 64-bit vectors right now.
Nate Begeman9008ca62009-04-27 18:41:29 +000012176 if (VT.getSizeInBits() == 64)
Craig Topper1dc0fbc2011-12-05 07:27:14 +000012177 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +000012178
Nate Begemana09008b2009-10-19 02:17:23 +000012179 // FIXME: pshufb, blends, shifts.
Nate Begeman9008ca62009-04-27 18:41:29 +000012180 return (VT.getVectorNumElements() == 2 ||
12181 ShuffleVectorSDNode::isSplatMask(&M[0], VT) ||
12182 isMOVLMask(M, VT) ||
Craig Topper1a7700a2012-01-19 08:19:12 +000012183 isSHUFPMask(M, VT, Subtarget->hasAVX()) ||
Nate Begeman9008ca62009-04-27 18:41:29 +000012184 isPSHUFDMask(M, VT) ||
Craig Toppera9a568a2012-05-02 08:03:44 +000012185 isPSHUFHWMask(M, VT, Subtarget->hasAVX2()) ||
12186 isPSHUFLWMask(M, VT, Subtarget->hasAVX2()) ||
Craig Topper0e2037b2012-01-20 05:53:00 +000012187 isPALIGNRMask(M, VT, Subtarget) ||
Craig Topper6347e862011-11-21 06:57:39 +000012188 isUNPCKLMask(M, VT, Subtarget->hasAVX2()) ||
12189 isUNPCKHMask(M, VT, Subtarget->hasAVX2()) ||
Craig Topper94438ba2011-12-16 08:06:31 +000012190 isUNPCKL_v_undef_Mask(M, VT, Subtarget->hasAVX2()) ||
12191 isUNPCKH_v_undef_Mask(M, VT, Subtarget->hasAVX2()));
Evan Cheng60c07e12006-07-05 22:17:51 +000012192}
12193
Dan Gohman7d8143f2008-04-09 20:09:42 +000012194bool
Nate Begeman5a5ca152009-04-29 05:20:52 +000012195X86TargetLowering::isVectorClearMaskLegal(const SmallVectorImpl<int> &Mask,
Owen Andersone50ed302009-08-10 22:56:29 +000012196 EVT VT) const {
Nate Begeman9008ca62009-04-27 18:41:29 +000012197 unsigned NumElts = VT.getVectorNumElements();
12198 // FIXME: This collection of masks seems suspect.
12199 if (NumElts == 2)
12200 return true;
Craig Topper7a9a28b2012-08-12 02:23:29 +000012201 if (NumElts == 4 && VT.is128BitVector()) {
Nate Begeman9008ca62009-04-27 18:41:29 +000012202 return (isMOVLMask(Mask, VT) ||
12203 isCommutedMOVLMask(Mask, VT, true) ||
Craig Topper1a7700a2012-01-19 08:19:12 +000012204 isSHUFPMask(Mask, VT, Subtarget->hasAVX()) ||
12205 isSHUFPMask(Mask, VT, Subtarget->hasAVX(), /* Commuted */ true));
Evan Cheng60c07e12006-07-05 22:17:51 +000012206 }
12207 return false;
12208}
12209
12210//===----------------------------------------------------------------------===//
12211// X86 Scheduler Hooks
12212//===----------------------------------------------------------------------===//
12213
Michael Liaobe02a902012-11-08 07:28:54 +000012214/// Utility function to emit xbegin specifying the start of an RTM region.
Craig Topper2da36912012-11-11 22:45:02 +000012215static MachineBasicBlock *EmitXBegin(MachineInstr *MI, MachineBasicBlock *MBB,
12216 const TargetInstrInfo *TII) {
Michael Liaobe02a902012-11-08 07:28:54 +000012217 DebugLoc DL = MI->getDebugLoc();
Michael Liaobe02a902012-11-08 07:28:54 +000012218
12219 const BasicBlock *BB = MBB->getBasicBlock();
12220 MachineFunction::iterator I = MBB;
12221 ++I;
12222
12223 // For the v = xbegin(), we generate
12224 //
12225 // thisMBB:
12226 // xbegin sinkMBB
12227 //
12228 // mainMBB:
12229 // eax = -1
12230 //
12231 // sinkMBB:
12232 // v = eax
12233
12234 MachineBasicBlock *thisMBB = MBB;
12235 MachineFunction *MF = MBB->getParent();
12236 MachineBasicBlock *mainMBB = MF->CreateMachineBasicBlock(BB);
12237 MachineBasicBlock *sinkMBB = MF->CreateMachineBasicBlock(BB);
12238 MF->insert(I, mainMBB);
12239 MF->insert(I, sinkMBB);
12240
12241 // Transfer the remainder of BB and its successor edges to sinkMBB.
12242 sinkMBB->splice(sinkMBB->begin(), MBB,
12243 llvm::next(MachineBasicBlock::iterator(MI)), MBB->end());
12244 sinkMBB->transferSuccessorsAndUpdatePHIs(MBB);
12245
12246 // thisMBB:
12247 // xbegin sinkMBB
12248 // # fallthrough to mainMBB
12249 // # abortion to sinkMBB
12250 BuildMI(thisMBB, DL, TII->get(X86::XBEGIN_4)).addMBB(sinkMBB);
12251 thisMBB->addSuccessor(mainMBB);
12252 thisMBB->addSuccessor(sinkMBB);
12253
12254 // mainMBB:
12255 // EAX = -1
12256 BuildMI(mainMBB, DL, TII->get(X86::MOV32ri), X86::EAX).addImm(-1);
12257 mainMBB->addSuccessor(sinkMBB);
12258
12259 // sinkMBB:
12260 // EAX is live into the sinkMBB
12261 sinkMBB->addLiveIn(X86::EAX);
12262 BuildMI(*sinkMBB, sinkMBB->begin(), DL,
12263 TII->get(TargetOpcode::COPY), MI->getOperand(0).getReg())
12264 .addReg(X86::EAX);
12265
12266 MI->eraseFromParent();
12267 return sinkMBB;
12268}
12269
Michael Liaob118a072012-09-20 03:06:15 +000012270// Get CMPXCHG opcode for the specified data type.
12271static unsigned getCmpXChgOpcode(EVT VT) {
12272 switch (VT.getSimpleVT().SimpleTy) {
12273 case MVT::i8: return X86::LCMPXCHG8;
12274 case MVT::i16: return X86::LCMPXCHG16;
12275 case MVT::i32: return X86::LCMPXCHG32;
12276 case MVT::i64: return X86::LCMPXCHG64;
12277 default:
12278 break;
Richard Smith42fc29e2012-04-13 22:47:00 +000012279 }
Michael Liaob118a072012-09-20 03:06:15 +000012280 llvm_unreachable("Invalid operand size!");
Mon P Wang63307c32008-05-05 19:05:59 +000012281}
12282
Michael Liaob118a072012-09-20 03:06:15 +000012283// Get LOAD opcode for the specified data type.
12284static unsigned getLoadOpcode(EVT VT) {
12285 switch (VT.getSimpleVT().SimpleTy) {
12286 case MVT::i8: return X86::MOV8rm;
12287 case MVT::i16: return X86::MOV16rm;
12288 case MVT::i32: return X86::MOV32rm;
12289 case MVT::i64: return X86::MOV64rm;
12290 default:
12291 break;
12292 }
12293 llvm_unreachable("Invalid operand size!");
12294}
12295
12296// Get opcode of the non-atomic one from the specified atomic instruction.
12297static unsigned getNonAtomicOpcode(unsigned Opc) {
12298 switch (Opc) {
12299 case X86::ATOMAND8: return X86::AND8rr;
12300 case X86::ATOMAND16: return X86::AND16rr;
12301 case X86::ATOMAND32: return X86::AND32rr;
12302 case X86::ATOMAND64: return X86::AND64rr;
12303 case X86::ATOMOR8: return X86::OR8rr;
12304 case X86::ATOMOR16: return X86::OR16rr;
12305 case X86::ATOMOR32: return X86::OR32rr;
12306 case X86::ATOMOR64: return X86::OR64rr;
12307 case X86::ATOMXOR8: return X86::XOR8rr;
12308 case X86::ATOMXOR16: return X86::XOR16rr;
12309 case X86::ATOMXOR32: return X86::XOR32rr;
12310 case X86::ATOMXOR64: return X86::XOR64rr;
12311 }
12312 llvm_unreachable("Unhandled atomic-load-op opcode!");
12313}
12314
12315// Get opcode of the non-atomic one from the specified atomic instruction with
12316// extra opcode.
12317static unsigned getNonAtomicOpcodeWithExtraOpc(unsigned Opc,
12318 unsigned &ExtraOpc) {
12319 switch (Opc) {
12320 case X86::ATOMNAND8: ExtraOpc = X86::NOT8r; return X86::AND8rr;
12321 case X86::ATOMNAND16: ExtraOpc = X86::NOT16r; return X86::AND16rr;
12322 case X86::ATOMNAND32: ExtraOpc = X86::NOT32r; return X86::AND32rr;
12323 case X86::ATOMNAND64: ExtraOpc = X86::NOT64r; return X86::AND64rr;
Michael Liaofe87c302012-09-21 03:18:52 +000012324 case X86::ATOMMAX8: ExtraOpc = X86::CMP8rr; return X86::CMOVL32rr;
Michael Liaob118a072012-09-20 03:06:15 +000012325 case X86::ATOMMAX16: ExtraOpc = X86::CMP16rr; return X86::CMOVL16rr;
12326 case X86::ATOMMAX32: ExtraOpc = X86::CMP32rr; return X86::CMOVL32rr;
12327 case X86::ATOMMAX64: ExtraOpc = X86::CMP64rr; return X86::CMOVL64rr;
Michael Liaofe87c302012-09-21 03:18:52 +000012328 case X86::ATOMMIN8: ExtraOpc = X86::CMP8rr; return X86::CMOVG32rr;
Michael Liaob118a072012-09-20 03:06:15 +000012329 case X86::ATOMMIN16: ExtraOpc = X86::CMP16rr; return X86::CMOVG16rr;
12330 case X86::ATOMMIN32: ExtraOpc = X86::CMP32rr; return X86::CMOVG32rr;
12331 case X86::ATOMMIN64: ExtraOpc = X86::CMP64rr; return X86::CMOVG64rr;
Michael Liaofe87c302012-09-21 03:18:52 +000012332 case X86::ATOMUMAX8: ExtraOpc = X86::CMP8rr; return X86::CMOVB32rr;
Michael Liaob118a072012-09-20 03:06:15 +000012333 case X86::ATOMUMAX16: ExtraOpc = X86::CMP16rr; return X86::CMOVB16rr;
12334 case X86::ATOMUMAX32: ExtraOpc = X86::CMP32rr; return X86::CMOVB32rr;
12335 case X86::ATOMUMAX64: ExtraOpc = X86::CMP64rr; return X86::CMOVB64rr;
Michael Liaofe87c302012-09-21 03:18:52 +000012336 case X86::ATOMUMIN8: ExtraOpc = X86::CMP8rr; return X86::CMOVA32rr;
Michael Liaob118a072012-09-20 03:06:15 +000012337 case X86::ATOMUMIN16: ExtraOpc = X86::CMP16rr; return X86::CMOVA16rr;
12338 case X86::ATOMUMIN32: ExtraOpc = X86::CMP32rr; return X86::CMOVA32rr;
12339 case X86::ATOMUMIN64: ExtraOpc = X86::CMP64rr; return X86::CMOVA64rr;
12340 }
12341 llvm_unreachable("Unhandled atomic-load-op opcode!");
12342}
12343
12344// Get opcode of the non-atomic one from the specified atomic instruction for
12345// 64-bit data type on 32-bit target.
12346static unsigned getNonAtomic6432Opcode(unsigned Opc, unsigned &HiOpc) {
12347 switch (Opc) {
12348 case X86::ATOMAND6432: HiOpc = X86::AND32rr; return X86::AND32rr;
12349 case X86::ATOMOR6432: HiOpc = X86::OR32rr; return X86::OR32rr;
12350 case X86::ATOMXOR6432: HiOpc = X86::XOR32rr; return X86::XOR32rr;
12351 case X86::ATOMADD6432: HiOpc = X86::ADC32rr; return X86::ADD32rr;
12352 case X86::ATOMSUB6432: HiOpc = X86::SBB32rr; return X86::SUB32rr;
12353 case X86::ATOMSWAP6432: HiOpc = X86::MOV32rr; return X86::MOV32rr;
Michael Liaoe5e8f762012-09-25 18:08:13 +000012354 case X86::ATOMMAX6432: HiOpc = X86::SETLr; return X86::SETLr;
12355 case X86::ATOMMIN6432: HiOpc = X86::SETGr; return X86::SETGr;
12356 case X86::ATOMUMAX6432: HiOpc = X86::SETBr; return X86::SETBr;
12357 case X86::ATOMUMIN6432: HiOpc = X86::SETAr; return X86::SETAr;
Michael Liaob118a072012-09-20 03:06:15 +000012358 }
12359 llvm_unreachable("Unhandled atomic-load-op opcode!");
12360}
12361
12362// Get opcode of the non-atomic one from the specified atomic instruction for
12363// 64-bit data type on 32-bit target with extra opcode.
12364static unsigned getNonAtomic6432OpcodeWithExtraOpc(unsigned Opc,
12365 unsigned &HiOpc,
12366 unsigned &ExtraOpc) {
12367 switch (Opc) {
12368 case X86::ATOMNAND6432:
12369 ExtraOpc = X86::NOT32r;
12370 HiOpc = X86::AND32rr;
12371 return X86::AND32rr;
12372 }
12373 llvm_unreachable("Unhandled atomic-load-op opcode!");
12374}
12375
12376// Get pseudo CMOV opcode from the specified data type.
12377static unsigned getPseudoCMOVOpc(EVT VT) {
12378 switch (VT.getSimpleVT().SimpleTy) {
Michael Liaofe87c302012-09-21 03:18:52 +000012379 case MVT::i8: return X86::CMOV_GR8;
Michael Liaob118a072012-09-20 03:06:15 +000012380 case MVT::i16: return X86::CMOV_GR16;
12381 case MVT::i32: return X86::CMOV_GR32;
12382 default:
12383 break;
12384 }
12385 llvm_unreachable("Unknown CMOV opcode!");
12386}
12387
12388// EmitAtomicLoadArith - emit the code sequence for pseudo atomic instructions.
12389// They will be translated into a spin-loop or compare-exchange loop from
12390//
12391// ...
12392// dst = atomic-fetch-op MI.addr, MI.val
12393// ...
12394//
12395// to
12396//
12397// ...
12398// EAX = LOAD MI.addr
12399// loop:
12400// t1 = OP MI.val, EAX
12401// LCMPXCHG [MI.addr], t1, [EAX is implicitly used & defined]
12402// JNE loop
12403// sink:
12404// dst = EAX
12405// ...
Mon P Wang63307c32008-05-05 19:05:59 +000012406MachineBasicBlock *
Michael Liaob118a072012-09-20 03:06:15 +000012407X86TargetLowering::EmitAtomicLoadArith(MachineInstr *MI,
12408 MachineBasicBlock *MBB) const {
12409 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
12410 DebugLoc DL = MI->getDebugLoc();
12411
12412 MachineFunction *MF = MBB->getParent();
12413 MachineRegisterInfo &MRI = MF->getRegInfo();
12414
12415 const BasicBlock *BB = MBB->getBasicBlock();
12416 MachineFunction::iterator I = MBB;
12417 ++I;
12418
12419 assert(MI->getNumOperands() <= X86::AddrNumOperands + 2 &&
12420 "Unexpected number of operands");
12421
12422 assert(MI->hasOneMemOperand() &&
12423 "Expected atomic-load-op to have one memoperand");
12424
12425 // Memory Reference
12426 MachineInstr::mmo_iterator MMOBegin = MI->memoperands_begin();
12427 MachineInstr::mmo_iterator MMOEnd = MI->memoperands_end();
12428
12429 unsigned DstReg, SrcReg;
12430 unsigned MemOpndSlot;
12431
12432 unsigned CurOp = 0;
12433
12434 DstReg = MI->getOperand(CurOp++).getReg();
12435 MemOpndSlot = CurOp;
12436 CurOp += X86::AddrNumOperands;
12437 SrcReg = MI->getOperand(CurOp++).getReg();
12438
12439 const TargetRegisterClass *RC = MRI.getRegClass(DstReg);
Craig Topperf4d25a22012-09-30 19:49:56 +000012440 MVT::SimpleValueType VT = *RC->vt_begin();
Michael Liaob118a072012-09-20 03:06:15 +000012441 unsigned AccPhyReg = getX86SubSuperRegister(X86::EAX, VT);
12442
12443 unsigned LCMPXCHGOpc = getCmpXChgOpcode(VT);
12444 unsigned LOADOpc = getLoadOpcode(VT);
12445
12446 // For the atomic load-arith operator, we generate
12447 //
12448 // thisMBB:
12449 // EAX = LOAD [MI.addr]
12450 // mainMBB:
12451 // t1 = OP MI.val, EAX
12452 // LCMPXCHG [MI.addr], t1, [EAX is implicitly used & defined]
12453 // JNE mainMBB
12454 // sinkMBB:
12455
12456 MachineBasicBlock *thisMBB = MBB;
12457 MachineBasicBlock *mainMBB = MF->CreateMachineBasicBlock(BB);
12458 MachineBasicBlock *sinkMBB = MF->CreateMachineBasicBlock(BB);
12459 MF->insert(I, mainMBB);
12460 MF->insert(I, sinkMBB);
12461
12462 MachineInstrBuilder MIB;
12463
12464 // Transfer the remainder of BB and its successor edges to sinkMBB.
12465 sinkMBB->splice(sinkMBB->begin(), MBB,
12466 llvm::next(MachineBasicBlock::iterator(MI)), MBB->end());
12467 sinkMBB->transferSuccessorsAndUpdatePHIs(MBB);
12468
12469 // thisMBB:
12470 MIB = BuildMI(thisMBB, DL, TII->get(LOADOpc), AccPhyReg);
12471 for (unsigned i = 0; i < X86::AddrNumOperands; ++i)
12472 MIB.addOperand(MI->getOperand(MemOpndSlot + i));
12473 MIB.setMemRefs(MMOBegin, MMOEnd);
12474
12475 thisMBB->addSuccessor(mainMBB);
12476
12477 // mainMBB:
12478 MachineBasicBlock *origMainMBB = mainMBB;
12479 mainMBB->addLiveIn(AccPhyReg);
12480
12481 // Copy AccPhyReg as it is used more than once.
12482 unsigned AccReg = MRI.createVirtualRegister(RC);
12483 BuildMI(mainMBB, DL, TII->get(TargetOpcode::COPY), AccReg)
12484 .addReg(AccPhyReg);
12485
12486 unsigned t1 = MRI.createVirtualRegister(RC);
12487 unsigned Opc = MI->getOpcode();
12488 switch (Opc) {
12489 default:
12490 llvm_unreachable("Unhandled atomic-load-op opcode!");
12491 case X86::ATOMAND8:
12492 case X86::ATOMAND16:
12493 case X86::ATOMAND32:
12494 case X86::ATOMAND64:
12495 case X86::ATOMOR8:
12496 case X86::ATOMOR16:
12497 case X86::ATOMOR32:
12498 case X86::ATOMOR64:
12499 case X86::ATOMXOR8:
12500 case X86::ATOMXOR16:
12501 case X86::ATOMXOR32:
12502 case X86::ATOMXOR64: {
12503 unsigned ARITHOpc = getNonAtomicOpcode(Opc);
12504 BuildMI(mainMBB, DL, TII->get(ARITHOpc), t1).addReg(SrcReg)
12505 .addReg(AccReg);
12506 break;
12507 }
12508 case X86::ATOMNAND8:
12509 case X86::ATOMNAND16:
12510 case X86::ATOMNAND32:
12511 case X86::ATOMNAND64: {
12512 unsigned t2 = MRI.createVirtualRegister(RC);
12513 unsigned NOTOpc;
12514 unsigned ANDOpc = getNonAtomicOpcodeWithExtraOpc(Opc, NOTOpc);
12515 BuildMI(mainMBB, DL, TII->get(ANDOpc), t2).addReg(SrcReg)
12516 .addReg(AccReg);
12517 BuildMI(mainMBB, DL, TII->get(NOTOpc), t1).addReg(t2);
12518 break;
12519 }
Michael Liao08382492012-09-21 03:00:17 +000012520 case X86::ATOMMAX8:
Michael Liaob118a072012-09-20 03:06:15 +000012521 case X86::ATOMMAX16:
12522 case X86::ATOMMAX32:
12523 case X86::ATOMMAX64:
Michael Liaofe87c302012-09-21 03:18:52 +000012524 case X86::ATOMMIN8:
Michael Liaob118a072012-09-20 03:06:15 +000012525 case X86::ATOMMIN16:
12526 case X86::ATOMMIN32:
12527 case X86::ATOMMIN64:
Michael Liaofe87c302012-09-21 03:18:52 +000012528 case X86::ATOMUMAX8:
Michael Liaob118a072012-09-20 03:06:15 +000012529 case X86::ATOMUMAX16:
12530 case X86::ATOMUMAX32:
12531 case X86::ATOMUMAX64:
Michael Liaofe87c302012-09-21 03:18:52 +000012532 case X86::ATOMUMIN8:
Michael Liaob118a072012-09-20 03:06:15 +000012533 case X86::ATOMUMIN16:
12534 case X86::ATOMUMIN32:
12535 case X86::ATOMUMIN64: {
12536 unsigned CMPOpc;
12537 unsigned CMOVOpc = getNonAtomicOpcodeWithExtraOpc(Opc, CMPOpc);
12538
12539 BuildMI(mainMBB, DL, TII->get(CMPOpc))
12540 .addReg(SrcReg)
12541 .addReg(AccReg);
12542
12543 if (Subtarget->hasCMov()) {
Michael Liaofe87c302012-09-21 03:18:52 +000012544 if (VT != MVT::i8) {
12545 // Native support
12546 BuildMI(mainMBB, DL, TII->get(CMOVOpc), t1)
12547 .addReg(SrcReg)
12548 .addReg(AccReg);
12549 } else {
12550 // Promote i8 to i32 to use CMOV32
12551 const TargetRegisterClass *RC32 = getRegClassFor(MVT::i32);
12552 unsigned SrcReg32 = MRI.createVirtualRegister(RC32);
12553 unsigned AccReg32 = MRI.createVirtualRegister(RC32);
12554 unsigned t2 = MRI.createVirtualRegister(RC32);
12555
12556 unsigned Undef = MRI.createVirtualRegister(RC32);
12557 BuildMI(mainMBB, DL, TII->get(TargetOpcode::IMPLICIT_DEF), Undef);
12558
12559 BuildMI(mainMBB, DL, TII->get(TargetOpcode::INSERT_SUBREG), SrcReg32)
12560 .addReg(Undef)
12561 .addReg(SrcReg)
12562 .addImm(X86::sub_8bit);
12563 BuildMI(mainMBB, DL, TII->get(TargetOpcode::INSERT_SUBREG), AccReg32)
12564 .addReg(Undef)
12565 .addReg(AccReg)
12566 .addImm(X86::sub_8bit);
12567
12568 BuildMI(mainMBB, DL, TII->get(CMOVOpc), t2)
12569 .addReg(SrcReg32)
12570 .addReg(AccReg32);
12571
12572 BuildMI(mainMBB, DL, TII->get(TargetOpcode::COPY), t1)
12573 .addReg(t2, 0, X86::sub_8bit);
12574 }
Michael Liaob118a072012-09-20 03:06:15 +000012575 } else {
12576 // Use pseudo select and lower them.
Michael Liaofe87c302012-09-21 03:18:52 +000012577 assert((VT == MVT::i8 || VT == MVT::i16 || VT == MVT::i32) &&
Michael Liaob118a072012-09-20 03:06:15 +000012578 "Invalid atomic-load-op transformation!");
12579 unsigned SelOpc = getPseudoCMOVOpc(VT);
12580 X86::CondCode CC = X86::getCondFromCMovOpc(CMOVOpc);
12581 assert(CC != X86::COND_INVALID && "Invalid atomic-load-op transformation!");
12582 MIB = BuildMI(mainMBB, DL, TII->get(SelOpc), t1)
12583 .addReg(SrcReg).addReg(AccReg)
12584 .addImm(CC);
12585 mainMBB = EmitLoweredSelect(MIB, mainMBB);
12586 }
12587 break;
12588 }
12589 }
12590
12591 // Copy AccPhyReg back from virtual register.
12592 BuildMI(mainMBB, DL, TII->get(TargetOpcode::COPY), AccPhyReg)
12593 .addReg(AccReg);
12594
12595 MIB = BuildMI(mainMBB, DL, TII->get(LCMPXCHGOpc));
12596 for (unsigned i = 0; i < X86::AddrNumOperands; ++i)
12597 MIB.addOperand(MI->getOperand(MemOpndSlot + i));
12598 MIB.addReg(t1);
12599 MIB.setMemRefs(MMOBegin, MMOEnd);
12600
12601 BuildMI(mainMBB, DL, TII->get(X86::JNE_4)).addMBB(origMainMBB);
12602
12603 mainMBB->addSuccessor(origMainMBB);
12604 mainMBB->addSuccessor(sinkMBB);
12605
12606 // sinkMBB:
12607 sinkMBB->addLiveIn(AccPhyReg);
12608
12609 BuildMI(*sinkMBB, sinkMBB->begin(), DL,
12610 TII->get(TargetOpcode::COPY), DstReg)
12611 .addReg(AccPhyReg);
12612
12613 MI->eraseFromParent();
12614 return sinkMBB;
12615}
12616
12617// EmitAtomicLoadArith6432 - emit the code sequence for pseudo atomic
12618// instructions. They will be translated into a spin-loop or compare-exchange
12619// loop from
12620//
12621// ...
12622// dst = atomic-fetch-op MI.addr, MI.val
12623// ...
12624//
12625// to
12626//
12627// ...
12628// EAX = LOAD [MI.addr + 0]
12629// EDX = LOAD [MI.addr + 4]
12630// loop:
12631// EBX = OP MI.val.lo, EAX
12632// ECX = OP MI.val.hi, EDX
12633// LCMPXCHG8B [MI.addr], [ECX:EBX & EDX:EAX are implicitly used and EDX:EAX is implicitly defined]
12634// JNE loop
12635// sink:
12636// dst = EDX:EAX
12637// ...
12638MachineBasicBlock *
12639X86TargetLowering::EmitAtomicLoadArith6432(MachineInstr *MI,
12640 MachineBasicBlock *MBB) const {
12641 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
12642 DebugLoc DL = MI->getDebugLoc();
12643
12644 MachineFunction *MF = MBB->getParent();
12645 MachineRegisterInfo &MRI = MF->getRegInfo();
12646
12647 const BasicBlock *BB = MBB->getBasicBlock();
12648 MachineFunction::iterator I = MBB;
12649 ++I;
12650
12651 assert(MI->getNumOperands() <= X86::AddrNumOperands + 4 &&
12652 "Unexpected number of operands");
12653
12654 assert(MI->hasOneMemOperand() &&
12655 "Expected atomic-load-op32 to have one memoperand");
12656
12657 // Memory Reference
12658 MachineInstr::mmo_iterator MMOBegin = MI->memoperands_begin();
12659 MachineInstr::mmo_iterator MMOEnd = MI->memoperands_end();
12660
12661 unsigned DstLoReg, DstHiReg;
12662 unsigned SrcLoReg, SrcHiReg;
12663 unsigned MemOpndSlot;
12664
12665 unsigned CurOp = 0;
12666
12667 DstLoReg = MI->getOperand(CurOp++).getReg();
12668 DstHiReg = MI->getOperand(CurOp++).getReg();
12669 MemOpndSlot = CurOp;
12670 CurOp += X86::AddrNumOperands;
12671 SrcLoReg = MI->getOperand(CurOp++).getReg();
12672 SrcHiReg = MI->getOperand(CurOp++).getReg();
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012673
Craig Topperc9099502012-04-20 06:31:50 +000012674 const TargetRegisterClass *RC = &X86::GR32RegClass;
Michael Liaoe5e8f762012-09-25 18:08:13 +000012675 const TargetRegisterClass *RC8 = &X86::GR8RegClass;
Scott Michelfdc40a02009-02-17 22:15:04 +000012676
Michael Liaob118a072012-09-20 03:06:15 +000012677 unsigned LCMPXCHGOpc = X86::LCMPXCHG8B;
12678 unsigned LOADOpc = X86::MOV32rm;
Scott Michelfdc40a02009-02-17 22:15:04 +000012679
Michael Liaob118a072012-09-20 03:06:15 +000012680 // For the atomic load-arith operator, we generate
Mon P Wang63307c32008-05-05 19:05:59 +000012681 //
Michael Liaob118a072012-09-20 03:06:15 +000012682 // thisMBB:
12683 // EAX = LOAD [MI.addr + 0]
12684 // EDX = LOAD [MI.addr + 4]
12685 // mainMBB:
12686 // EBX = OP MI.vallo, EAX
12687 // ECX = OP MI.valhi, EDX
12688 // LCMPXCHG8B [MI.addr], [ECX:EBX & EDX:EAX are implicitly used and EDX:EAX is implicitly defined]
12689 // JNE mainMBB
12690 // sinkMBB:
Scott Michelfdc40a02009-02-17 22:15:04 +000012691
Mon P Wang63307c32008-05-05 19:05:59 +000012692 MachineBasicBlock *thisMBB = MBB;
Michael Liaob118a072012-09-20 03:06:15 +000012693 MachineBasicBlock *mainMBB = MF->CreateMachineBasicBlock(BB);
12694 MachineBasicBlock *sinkMBB = MF->CreateMachineBasicBlock(BB);
12695 MF->insert(I, mainMBB);
12696 MF->insert(I, sinkMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000012697
Michael Liaob118a072012-09-20 03:06:15 +000012698 MachineInstrBuilder MIB;
Scott Michelfdc40a02009-02-17 22:15:04 +000012699
Michael Liaob118a072012-09-20 03:06:15 +000012700 // Transfer the remainder of BB and its successor edges to sinkMBB.
12701 sinkMBB->splice(sinkMBB->begin(), MBB,
12702 llvm::next(MachineBasicBlock::iterator(MI)), MBB->end());
12703 sinkMBB->transferSuccessorsAndUpdatePHIs(MBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000012704
Michael Liaob118a072012-09-20 03:06:15 +000012705 // thisMBB:
12706 // Lo
12707 MIB = BuildMI(thisMBB, DL, TII->get(LOADOpc), X86::EAX);
12708 for (unsigned i = 0; i < X86::AddrNumOperands; ++i)
12709 MIB.addOperand(MI->getOperand(MemOpndSlot + i));
12710 MIB.setMemRefs(MMOBegin, MMOEnd);
12711 // Hi
12712 MIB = BuildMI(thisMBB, DL, TII->get(LOADOpc), X86::EDX);
12713 for (unsigned i = 0; i < X86::AddrNumOperands; ++i) {
Evan Chenga395f4d2012-10-11 00:15:48 +000012714 if (i == X86::AddrDisp)
Michael Liaob118a072012-09-20 03:06:15 +000012715 MIB.addDisp(MI->getOperand(MemOpndSlot + i), 4); // 4 == sizeof(i32)
Evan Chenga395f4d2012-10-11 00:15:48 +000012716 else
Michael Liaob118a072012-09-20 03:06:15 +000012717 MIB.addOperand(MI->getOperand(MemOpndSlot + i));
12718 }
12719 MIB.setMemRefs(MMOBegin, MMOEnd);
Scott Michelfdc40a02009-02-17 22:15:04 +000012720
Michael Liaob118a072012-09-20 03:06:15 +000012721 thisMBB->addSuccessor(mainMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000012722
Michael Liaob118a072012-09-20 03:06:15 +000012723 // mainMBB:
12724 MachineBasicBlock *origMainMBB = mainMBB;
12725 mainMBB->addLiveIn(X86::EAX);
12726 mainMBB->addLiveIn(X86::EDX);
Scott Michelfdc40a02009-02-17 22:15:04 +000012727
Michael Liaob118a072012-09-20 03:06:15 +000012728 // Copy EDX:EAX as they are used more than once.
12729 unsigned LoReg = MRI.createVirtualRegister(RC);
12730 unsigned HiReg = MRI.createVirtualRegister(RC);
12731 BuildMI(mainMBB, DL, TII->get(TargetOpcode::COPY), LoReg).addReg(X86::EAX);
12732 BuildMI(mainMBB, DL, TII->get(TargetOpcode::COPY), HiReg).addReg(X86::EDX);
Mon P Wangab3e7472008-05-05 22:56:23 +000012733
Michael Liaob118a072012-09-20 03:06:15 +000012734 unsigned t1L = MRI.createVirtualRegister(RC);
12735 unsigned t1H = MRI.createVirtualRegister(RC);
Scott Michelfdc40a02009-02-17 22:15:04 +000012736
Michael Liaob118a072012-09-20 03:06:15 +000012737 unsigned Opc = MI->getOpcode();
12738 switch (Opc) {
12739 default:
12740 llvm_unreachable("Unhandled atomic-load-op6432 opcode!");
12741 case X86::ATOMAND6432:
12742 case X86::ATOMOR6432:
12743 case X86::ATOMXOR6432:
12744 case X86::ATOMADD6432:
12745 case X86::ATOMSUB6432: {
12746 unsigned HiOpc;
12747 unsigned LoOpc = getNonAtomic6432Opcode(Opc, HiOpc);
Michael Liaodd3383f2012-11-12 06:49:17 +000012748 BuildMI(mainMBB, DL, TII->get(LoOpc), t1L).addReg(LoReg).addReg(SrcLoReg);
12749 BuildMI(mainMBB, DL, TII->get(HiOpc), t1H).addReg(HiReg).addReg(SrcHiReg);
Michael Liaob118a072012-09-20 03:06:15 +000012750 break;
12751 }
12752 case X86::ATOMNAND6432: {
12753 unsigned HiOpc, NOTOpc;
12754 unsigned LoOpc = getNonAtomic6432OpcodeWithExtraOpc(Opc, HiOpc, NOTOpc);
12755 unsigned t2L = MRI.createVirtualRegister(RC);
12756 unsigned t2H = MRI.createVirtualRegister(RC);
12757 BuildMI(mainMBB, DL, TII->get(LoOpc), t2L).addReg(SrcLoReg).addReg(LoReg);
12758 BuildMI(mainMBB, DL, TII->get(HiOpc), t2H).addReg(SrcHiReg).addReg(HiReg);
12759 BuildMI(mainMBB, DL, TII->get(NOTOpc), t1L).addReg(t2L);
12760 BuildMI(mainMBB, DL, TII->get(NOTOpc), t1H).addReg(t2H);
12761 break;
12762 }
Michael Liaoe5e8f762012-09-25 18:08:13 +000012763 case X86::ATOMMAX6432:
12764 case X86::ATOMMIN6432:
12765 case X86::ATOMUMAX6432:
12766 case X86::ATOMUMIN6432: {
12767 unsigned HiOpc;
12768 unsigned LoOpc = getNonAtomic6432Opcode(Opc, HiOpc);
12769 unsigned cL = MRI.createVirtualRegister(RC8);
12770 unsigned cH = MRI.createVirtualRegister(RC8);
12771 unsigned cL32 = MRI.createVirtualRegister(RC);
12772 unsigned cH32 = MRI.createVirtualRegister(RC);
12773 unsigned cc = MRI.createVirtualRegister(RC);
12774 // cl := cmp src_lo, lo
12775 BuildMI(mainMBB, DL, TII->get(X86::CMP32rr))
12776 .addReg(SrcLoReg).addReg(LoReg);
12777 BuildMI(mainMBB, DL, TII->get(LoOpc), cL);
12778 BuildMI(mainMBB, DL, TII->get(X86::MOVZX32rr8), cL32).addReg(cL);
12779 // ch := cmp src_hi, hi
12780 BuildMI(mainMBB, DL, TII->get(X86::CMP32rr))
12781 .addReg(SrcHiReg).addReg(HiReg);
12782 BuildMI(mainMBB, DL, TII->get(HiOpc), cH);
12783 BuildMI(mainMBB, DL, TII->get(X86::MOVZX32rr8), cH32).addReg(cH);
12784 // cc := if (src_hi == hi) ? cl : ch;
12785 if (Subtarget->hasCMov()) {
12786 BuildMI(mainMBB, DL, TII->get(X86::CMOVE32rr), cc)
12787 .addReg(cH32).addReg(cL32);
12788 } else {
12789 MIB = BuildMI(mainMBB, DL, TII->get(X86::CMOV_GR32), cc)
12790 .addReg(cH32).addReg(cL32)
12791 .addImm(X86::COND_E);
12792 mainMBB = EmitLoweredSelect(MIB, mainMBB);
12793 }
12794 BuildMI(mainMBB, DL, TII->get(X86::TEST32rr)).addReg(cc).addReg(cc);
12795 if (Subtarget->hasCMov()) {
12796 BuildMI(mainMBB, DL, TII->get(X86::CMOVNE32rr), t1L)
12797 .addReg(SrcLoReg).addReg(LoReg);
12798 BuildMI(mainMBB, DL, TII->get(X86::CMOVNE32rr), t1H)
12799 .addReg(SrcHiReg).addReg(HiReg);
12800 } else {
12801 MIB = BuildMI(mainMBB, DL, TII->get(X86::CMOV_GR32), t1L)
12802 .addReg(SrcLoReg).addReg(LoReg)
12803 .addImm(X86::COND_NE);
12804 mainMBB = EmitLoweredSelect(MIB, mainMBB);
12805 MIB = BuildMI(mainMBB, DL, TII->get(X86::CMOV_GR32), t1H)
12806 .addReg(SrcHiReg).addReg(HiReg)
12807 .addImm(X86::COND_NE);
12808 mainMBB = EmitLoweredSelect(MIB, mainMBB);
12809 }
12810 break;
12811 }
Michael Liaob118a072012-09-20 03:06:15 +000012812 case X86::ATOMSWAP6432: {
12813 unsigned HiOpc;
12814 unsigned LoOpc = getNonAtomic6432Opcode(Opc, HiOpc);
12815 BuildMI(mainMBB, DL, TII->get(LoOpc), t1L).addReg(SrcLoReg);
12816 BuildMI(mainMBB, DL, TII->get(HiOpc), t1H).addReg(SrcHiReg);
12817 break;
12818 }
12819 }
Mon P Wang63307c32008-05-05 19:05:59 +000012820
Michael Liaob118a072012-09-20 03:06:15 +000012821 // Copy EDX:EAX back from HiReg:LoReg
12822 BuildMI(mainMBB, DL, TII->get(TargetOpcode::COPY), X86::EAX).addReg(LoReg);
12823 BuildMI(mainMBB, DL, TII->get(TargetOpcode::COPY), X86::EDX).addReg(HiReg);
12824 // Copy ECX:EBX from t1H:t1L
12825 BuildMI(mainMBB, DL, TII->get(TargetOpcode::COPY), X86::EBX).addReg(t1L);
12826 BuildMI(mainMBB, DL, TII->get(TargetOpcode::COPY), X86::ECX).addReg(t1H);
Mon P Wangab3e7472008-05-05 22:56:23 +000012827
Michael Liaob118a072012-09-20 03:06:15 +000012828 MIB = BuildMI(mainMBB, DL, TII->get(LCMPXCHGOpc));
12829 for (unsigned i = 0; i < X86::AddrNumOperands; ++i)
12830 MIB.addOperand(MI->getOperand(MemOpndSlot + i));
12831 MIB.setMemRefs(MMOBegin, MMOEnd);
Mon P Wang63307c32008-05-05 19:05:59 +000012832
Michael Liaob118a072012-09-20 03:06:15 +000012833 BuildMI(mainMBB, DL, TII->get(X86::JNE_4)).addMBB(origMainMBB);
Mon P Wang63307c32008-05-05 19:05:59 +000012834
Michael Liaob118a072012-09-20 03:06:15 +000012835 mainMBB->addSuccessor(origMainMBB);
12836 mainMBB->addSuccessor(sinkMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000012837
Michael Liaob118a072012-09-20 03:06:15 +000012838 // sinkMBB:
12839 sinkMBB->addLiveIn(X86::EAX);
12840 sinkMBB->addLiveIn(X86::EDX);
Scott Michelfdc40a02009-02-17 22:15:04 +000012841
Michael Liaob118a072012-09-20 03:06:15 +000012842 BuildMI(*sinkMBB, sinkMBB->begin(), DL,
12843 TII->get(TargetOpcode::COPY), DstLoReg)
12844 .addReg(X86::EAX);
12845 BuildMI(*sinkMBB, sinkMBB->begin(), DL,
12846 TII->get(TargetOpcode::COPY), DstHiReg)
12847 .addReg(X86::EDX);
Mon P Wang63307c32008-05-05 19:05:59 +000012848
Michael Liaob118a072012-09-20 03:06:15 +000012849 MI->eraseFromParent();
12850 return sinkMBB;
Mon P Wang63307c32008-05-05 19:05:59 +000012851}
12852
Eric Christopherf83a5de2009-08-27 18:08:16 +000012853// FIXME: When we get size specific XMM0 registers, i.e. XMM0_V16I8
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000012854// or XMM0_V32I8 in AVX all of this code can be replaced with that
12855// in the .td file.
Craig Topper8cb8c812012-11-10 09:02:47 +000012856static MachineBasicBlock *EmitPCMPSTRM(MachineInstr *MI, MachineBasicBlock *BB,
12857 const TargetInstrInfo *TII) {
Eric Christopherb120ab42009-08-18 22:50:32 +000012858 unsigned Opc;
Craig Topper8aae8dd2012-11-10 08:57:41 +000012859 switch (MI->getOpcode()) {
12860 default: llvm_unreachable("illegal opcode!");
12861 case X86::PCMPISTRM128REG: Opc = X86::PCMPISTRM128rr; break;
12862 case X86::VPCMPISTRM128REG: Opc = X86::VPCMPISTRM128rr; break;
12863 case X86::PCMPISTRM128MEM: Opc = X86::PCMPISTRM128rm; break;
12864 case X86::VPCMPISTRM128MEM: Opc = X86::VPCMPISTRM128rm; break;
12865 case X86::PCMPESTRM128REG: Opc = X86::PCMPESTRM128rr; break;
12866 case X86::VPCMPESTRM128REG: Opc = X86::VPCMPESTRM128rr; break;
12867 case X86::PCMPESTRM128MEM: Opc = X86::PCMPESTRM128rm; break;
12868 case X86::VPCMPESTRM128MEM: Opc = X86::VPCMPESTRM128rm; break;
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000012869 }
Eric Christopherb120ab42009-08-18 22:50:32 +000012870
Craig Topper8aae8dd2012-11-10 08:57:41 +000012871 DebugLoc dl = MI->getDebugLoc();
Eric Christopher41c902f2010-11-30 08:20:21 +000012872 MachineInstrBuilder MIB = BuildMI(*BB, MI, dl, TII->get(Opc));
Craig Topper8aae8dd2012-11-10 08:57:41 +000012873
Craig Topper52ea2452012-11-10 09:25:36 +000012874 unsigned NumArgs = MI->getNumOperands();
12875 for (unsigned i = 1; i < NumArgs; ++i) {
12876 MachineOperand &Op = MI->getOperand(i);
Eric Christopherb120ab42009-08-18 22:50:32 +000012877 if (!(Op.isReg() && Op.isImplicit()))
12878 MIB.addOperand(Op);
12879 }
Craig Topper8aae8dd2012-11-10 08:57:41 +000012880 if (MI->hasOneMemOperand())
Craig Topper9c7ae012012-11-10 01:23:36 +000012881 MIB->setMemRefs(MI->memoperands_begin(), MI->memoperands_end());
12882
Bruno Cardoso Lopes5affa512011-08-31 03:04:09 +000012883 BuildMI(*BB, MI, dl,
Craig Topper638aa682012-08-05 00:17:48 +000012884 TII->get(TargetOpcode::COPY), MI->getOperand(0).getReg())
Eric Christopherb120ab42009-08-18 22:50:32 +000012885 .addReg(X86::XMM0);
12886
Dan Gohman14152b42010-07-06 20:24:04 +000012887 MI->eraseFromParent();
Eric Christopherb120ab42009-08-18 22:50:32 +000012888 return BB;
12889}
12890
Craig Topper9c7ae012012-11-10 01:23:36 +000012891// FIXME: Custom handling because TableGen doesn't support multiple implicit
12892// defs in an instruction pattern
Craig Topper8cb8c812012-11-10 09:02:47 +000012893static MachineBasicBlock *EmitPCMPSTRI(MachineInstr *MI, MachineBasicBlock *BB,
12894 const TargetInstrInfo *TII) {
Craig Topper9c7ae012012-11-10 01:23:36 +000012895 unsigned Opc;
Craig Topper8aae8dd2012-11-10 08:57:41 +000012896 switch (MI->getOpcode()) {
12897 default: llvm_unreachable("illegal opcode!");
12898 case X86::PCMPISTRIREG: Opc = X86::PCMPISTRIrr; break;
12899 case X86::VPCMPISTRIREG: Opc = X86::VPCMPISTRIrr; break;
12900 case X86::PCMPISTRIMEM: Opc = X86::PCMPISTRIrm; break;
12901 case X86::VPCMPISTRIMEM: Opc = X86::VPCMPISTRIrm; break;
12902 case X86::PCMPESTRIREG: Opc = X86::PCMPESTRIrr; break;
12903 case X86::VPCMPESTRIREG: Opc = X86::VPCMPESTRIrr; break;
12904 case X86::PCMPESTRIMEM: Opc = X86::PCMPESTRIrm; break;
12905 case X86::VPCMPESTRIMEM: Opc = X86::VPCMPESTRIrm; break;
Craig Topper9c7ae012012-11-10 01:23:36 +000012906 }
12907
Craig Topper8aae8dd2012-11-10 08:57:41 +000012908 DebugLoc dl = MI->getDebugLoc();
Craig Topper9c7ae012012-11-10 01:23:36 +000012909 MachineInstrBuilder MIB = BuildMI(*BB, MI, dl, TII->get(Opc));
Craig Topper8aae8dd2012-11-10 08:57:41 +000012910
Craig Topper52ea2452012-11-10 09:25:36 +000012911 unsigned NumArgs = MI->getNumOperands(); // remove the results
12912 for (unsigned i = 1; i < NumArgs; ++i) {
12913 MachineOperand &Op = MI->getOperand(i);
Craig Topper9c7ae012012-11-10 01:23:36 +000012914 if (!(Op.isReg() && Op.isImplicit()))
12915 MIB.addOperand(Op);
12916 }
Craig Topper8aae8dd2012-11-10 08:57:41 +000012917 if (MI->hasOneMemOperand())
Craig Topper9c7ae012012-11-10 01:23:36 +000012918 MIB->setMemRefs(MI->memoperands_begin(), MI->memoperands_end());
12919
12920 BuildMI(*BB, MI, dl,
12921 TII->get(TargetOpcode::COPY), MI->getOperand(0).getReg())
12922 .addReg(X86::ECX);
12923
12924 MI->eraseFromParent();
12925 return BB;
12926}
12927
Craig Topper2da36912012-11-11 22:45:02 +000012928static MachineBasicBlock * EmitMonitor(MachineInstr *MI, MachineBasicBlock *BB,
12929 const TargetInstrInfo *TII,
12930 const X86Subtarget* Subtarget) {
Eric Christopher228232b2010-11-30 07:20:12 +000012931 DebugLoc dl = MI->getDebugLoc();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000012932
Eric Christopher228232b2010-11-30 07:20:12 +000012933 // Address into RAX/EAX, other two args into ECX, EDX.
12934 unsigned MemOpc = Subtarget->is64Bit() ? X86::LEA64r : X86::LEA32r;
12935 unsigned MemReg = Subtarget->is64Bit() ? X86::RAX : X86::EAX;
12936 MachineInstrBuilder MIB = BuildMI(*BB, MI, dl, TII->get(MemOpc), MemReg);
12937 for (int i = 0; i < X86::AddrNumOperands; ++i)
Eric Christopher82be2202010-11-30 08:10:28 +000012938 MIB.addOperand(MI->getOperand(i));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000012939
Eric Christopher228232b2010-11-30 07:20:12 +000012940 unsigned ValOps = X86::AddrNumOperands;
12941 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::ECX)
12942 .addReg(MI->getOperand(ValOps).getReg());
12943 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::EDX)
12944 .addReg(MI->getOperand(ValOps+1).getReg());
12945
12946 // The instruction doesn't actually take any operands though.
12947 BuildMI(*BB, MI, dl, TII->get(X86::MONITORrrr));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000012948
Eric Christopher228232b2010-11-30 07:20:12 +000012949 MI->eraseFromParent(); // The pseudo is gone now.
12950 return BB;
12951}
12952
12953MachineBasicBlock *
Dan Gohman320afb82010-10-12 18:00:49 +000012954X86TargetLowering::EmitVAARG64WithCustomInserter(
12955 MachineInstr *MI,
12956 MachineBasicBlock *MBB) const {
12957 // Emit va_arg instruction on X86-64.
12958
12959 // Operands to this pseudo-instruction:
12960 // 0 ) Output : destination address (reg)
12961 // 1-5) Input : va_list address (addr, i64mem)
12962 // 6 ) ArgSize : Size (in bytes) of vararg type
12963 // 7 ) ArgMode : 0=overflow only, 1=use gp_offset, 2=use fp_offset
12964 // 8 ) Align : Alignment of type
12965 // 9 ) EFLAGS (implicit-def)
12966
12967 assert(MI->getNumOperands() == 10 && "VAARG_64 should have 10 operands!");
12968 assert(X86::AddrNumOperands == 5 && "VAARG_64 assumes 5 address operands");
12969
12970 unsigned DestReg = MI->getOperand(0).getReg();
12971 MachineOperand &Base = MI->getOperand(1);
12972 MachineOperand &Scale = MI->getOperand(2);
12973 MachineOperand &Index = MI->getOperand(3);
12974 MachineOperand &Disp = MI->getOperand(4);
12975 MachineOperand &Segment = MI->getOperand(5);
12976 unsigned ArgSize = MI->getOperand(6).getImm();
12977 unsigned ArgMode = MI->getOperand(7).getImm();
12978 unsigned Align = MI->getOperand(8).getImm();
12979
12980 // Memory Reference
12981 assert(MI->hasOneMemOperand() && "Expected VAARG_64 to have one memoperand");
12982 MachineInstr::mmo_iterator MMOBegin = MI->memoperands_begin();
12983 MachineInstr::mmo_iterator MMOEnd = MI->memoperands_end();
12984
12985 // Machine Information
12986 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
12987 MachineRegisterInfo &MRI = MBB->getParent()->getRegInfo();
12988 const TargetRegisterClass *AddrRegClass = getRegClassFor(MVT::i64);
12989 const TargetRegisterClass *OffsetRegClass = getRegClassFor(MVT::i32);
12990 DebugLoc DL = MI->getDebugLoc();
12991
12992 // struct va_list {
12993 // i32 gp_offset
12994 // i32 fp_offset
12995 // i64 overflow_area (address)
12996 // i64 reg_save_area (address)
12997 // }
12998 // sizeof(va_list) = 24
12999 // alignment(va_list) = 8
13000
13001 unsigned TotalNumIntRegs = 6;
13002 unsigned TotalNumXMMRegs = 8;
13003 bool UseGPOffset = (ArgMode == 1);
13004 bool UseFPOffset = (ArgMode == 2);
13005 unsigned MaxOffset = TotalNumIntRegs * 8 +
13006 (UseFPOffset ? TotalNumXMMRegs * 16 : 0);
13007
13008 /* Align ArgSize to a multiple of 8 */
13009 unsigned ArgSizeA8 = (ArgSize + 7) & ~7;
13010 bool NeedsAlign = (Align > 8);
13011
13012 MachineBasicBlock *thisMBB = MBB;
13013 MachineBasicBlock *overflowMBB;
13014 MachineBasicBlock *offsetMBB;
13015 MachineBasicBlock *endMBB;
13016
13017 unsigned OffsetDestReg = 0; // Argument address computed by offsetMBB
13018 unsigned OverflowDestReg = 0; // Argument address computed by overflowMBB
13019 unsigned OffsetReg = 0;
13020
13021 if (!UseGPOffset && !UseFPOffset) {
13022 // If we only pull from the overflow region, we don't create a branch.
13023 // We don't need to alter control flow.
13024 OffsetDestReg = 0; // unused
13025 OverflowDestReg = DestReg;
13026
13027 offsetMBB = NULL;
13028 overflowMBB = thisMBB;
13029 endMBB = thisMBB;
13030 } else {
13031 // First emit code to check if gp_offset (or fp_offset) is below the bound.
13032 // If so, pull the argument from reg_save_area. (branch to offsetMBB)
13033 // If not, pull from overflow_area. (branch to overflowMBB)
13034 //
13035 // thisMBB
13036 // | .
13037 // | .
13038 // offsetMBB overflowMBB
13039 // | .
13040 // | .
13041 // endMBB
13042
13043 // Registers for the PHI in endMBB
13044 OffsetDestReg = MRI.createVirtualRegister(AddrRegClass);
13045 OverflowDestReg = MRI.createVirtualRegister(AddrRegClass);
13046
13047 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
13048 MachineFunction *MF = MBB->getParent();
13049 overflowMBB = MF->CreateMachineBasicBlock(LLVM_BB);
13050 offsetMBB = MF->CreateMachineBasicBlock(LLVM_BB);
13051 endMBB = MF->CreateMachineBasicBlock(LLVM_BB);
13052
13053 MachineFunction::iterator MBBIter = MBB;
13054 ++MBBIter;
13055
13056 // Insert the new basic blocks
13057 MF->insert(MBBIter, offsetMBB);
13058 MF->insert(MBBIter, overflowMBB);
13059 MF->insert(MBBIter, endMBB);
13060
13061 // Transfer the remainder of MBB and its successor edges to endMBB.
13062 endMBB->splice(endMBB->begin(), thisMBB,
13063 llvm::next(MachineBasicBlock::iterator(MI)),
13064 thisMBB->end());
13065 endMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
13066
13067 // Make offsetMBB and overflowMBB successors of thisMBB
13068 thisMBB->addSuccessor(offsetMBB);
13069 thisMBB->addSuccessor(overflowMBB);
13070
13071 // endMBB is a successor of both offsetMBB and overflowMBB
13072 offsetMBB->addSuccessor(endMBB);
13073 overflowMBB->addSuccessor(endMBB);
13074
13075 // Load the offset value into a register
13076 OffsetReg = MRI.createVirtualRegister(OffsetRegClass);
13077 BuildMI(thisMBB, DL, TII->get(X86::MOV32rm), OffsetReg)
13078 .addOperand(Base)
13079 .addOperand(Scale)
13080 .addOperand(Index)
13081 .addDisp(Disp, UseFPOffset ? 4 : 0)
13082 .addOperand(Segment)
13083 .setMemRefs(MMOBegin, MMOEnd);
13084
13085 // Check if there is enough room left to pull this argument.
13086 BuildMI(thisMBB, DL, TII->get(X86::CMP32ri))
13087 .addReg(OffsetReg)
13088 .addImm(MaxOffset + 8 - ArgSizeA8);
13089
13090 // Branch to "overflowMBB" if offset >= max
13091 // Fall through to "offsetMBB" otherwise
13092 BuildMI(thisMBB, DL, TII->get(X86::GetCondBranchFromCond(X86::COND_AE)))
13093 .addMBB(overflowMBB);
13094 }
13095
13096 // In offsetMBB, emit code to use the reg_save_area.
13097 if (offsetMBB) {
13098 assert(OffsetReg != 0);
13099
13100 // Read the reg_save_area address.
13101 unsigned RegSaveReg = MRI.createVirtualRegister(AddrRegClass);
13102 BuildMI(offsetMBB, DL, TII->get(X86::MOV64rm), RegSaveReg)
13103 .addOperand(Base)
13104 .addOperand(Scale)
13105 .addOperand(Index)
13106 .addDisp(Disp, 16)
13107 .addOperand(Segment)
13108 .setMemRefs(MMOBegin, MMOEnd);
13109
13110 // Zero-extend the offset
13111 unsigned OffsetReg64 = MRI.createVirtualRegister(AddrRegClass);
13112 BuildMI(offsetMBB, DL, TII->get(X86::SUBREG_TO_REG), OffsetReg64)
13113 .addImm(0)
13114 .addReg(OffsetReg)
13115 .addImm(X86::sub_32bit);
13116
13117 // Add the offset to the reg_save_area to get the final address.
13118 BuildMI(offsetMBB, DL, TII->get(X86::ADD64rr), OffsetDestReg)
13119 .addReg(OffsetReg64)
13120 .addReg(RegSaveReg);
13121
13122 // Compute the offset for the next argument
13123 unsigned NextOffsetReg = MRI.createVirtualRegister(OffsetRegClass);
13124 BuildMI(offsetMBB, DL, TII->get(X86::ADD32ri), NextOffsetReg)
13125 .addReg(OffsetReg)
13126 .addImm(UseFPOffset ? 16 : 8);
13127
13128 // Store it back into the va_list.
13129 BuildMI(offsetMBB, DL, TII->get(X86::MOV32mr))
13130 .addOperand(Base)
13131 .addOperand(Scale)
13132 .addOperand(Index)
13133 .addDisp(Disp, UseFPOffset ? 4 : 0)
13134 .addOperand(Segment)
13135 .addReg(NextOffsetReg)
13136 .setMemRefs(MMOBegin, MMOEnd);
13137
13138 // Jump to endMBB
13139 BuildMI(offsetMBB, DL, TII->get(X86::JMP_4))
13140 .addMBB(endMBB);
13141 }
13142
13143 //
13144 // Emit code to use overflow area
13145 //
13146
13147 // Load the overflow_area address into a register.
13148 unsigned OverflowAddrReg = MRI.createVirtualRegister(AddrRegClass);
13149 BuildMI(overflowMBB, DL, TII->get(X86::MOV64rm), OverflowAddrReg)
13150 .addOperand(Base)
13151 .addOperand(Scale)
13152 .addOperand(Index)
13153 .addDisp(Disp, 8)
13154 .addOperand(Segment)
13155 .setMemRefs(MMOBegin, MMOEnd);
13156
13157 // If we need to align it, do so. Otherwise, just copy the address
13158 // to OverflowDestReg.
13159 if (NeedsAlign) {
13160 // Align the overflow address
13161 assert((Align & (Align-1)) == 0 && "Alignment must be a power of 2");
13162 unsigned TmpReg = MRI.createVirtualRegister(AddrRegClass);
13163
13164 // aligned_addr = (addr + (align-1)) & ~(align-1)
13165 BuildMI(overflowMBB, DL, TII->get(X86::ADD64ri32), TmpReg)
13166 .addReg(OverflowAddrReg)
13167 .addImm(Align-1);
13168
13169 BuildMI(overflowMBB, DL, TII->get(X86::AND64ri32), OverflowDestReg)
13170 .addReg(TmpReg)
13171 .addImm(~(uint64_t)(Align-1));
13172 } else {
13173 BuildMI(overflowMBB, DL, TII->get(TargetOpcode::COPY), OverflowDestReg)
13174 .addReg(OverflowAddrReg);
13175 }
13176
13177 // Compute the next overflow address after this argument.
13178 // (the overflow address should be kept 8-byte aligned)
13179 unsigned NextAddrReg = MRI.createVirtualRegister(AddrRegClass);
13180 BuildMI(overflowMBB, DL, TII->get(X86::ADD64ri32), NextAddrReg)
13181 .addReg(OverflowDestReg)
13182 .addImm(ArgSizeA8);
13183
13184 // Store the new overflow address.
13185 BuildMI(overflowMBB, DL, TII->get(X86::MOV64mr))
13186 .addOperand(Base)
13187 .addOperand(Scale)
13188 .addOperand(Index)
13189 .addDisp(Disp, 8)
13190 .addOperand(Segment)
13191 .addReg(NextAddrReg)
13192 .setMemRefs(MMOBegin, MMOEnd);
13193
13194 // If we branched, emit the PHI to the front of endMBB.
13195 if (offsetMBB) {
13196 BuildMI(*endMBB, endMBB->begin(), DL,
13197 TII->get(X86::PHI), DestReg)
13198 .addReg(OffsetDestReg).addMBB(offsetMBB)
13199 .addReg(OverflowDestReg).addMBB(overflowMBB);
13200 }
13201
13202 // Erase the pseudo instruction
13203 MI->eraseFromParent();
13204
13205 return endMBB;
13206}
13207
13208MachineBasicBlock *
Dan Gohmand6708ea2009-08-15 01:38:56 +000013209X86TargetLowering::EmitVAStartSaveXMMRegsWithCustomInserter(
13210 MachineInstr *MI,
13211 MachineBasicBlock *MBB) const {
13212 // Emit code to save XMM registers to the stack. The ABI says that the
13213 // number of registers to save is given in %al, so it's theoretically
13214 // possible to do an indirect jump trick to avoid saving all of them,
13215 // however this code takes a simpler approach and just executes all
13216 // of the stores if %al is non-zero. It's less code, and it's probably
13217 // easier on the hardware branch predictor, and stores aren't all that
13218 // expensive anyway.
13219
13220 // Create the new basic blocks. One block contains all the XMM stores,
13221 // and one block is the final destination regardless of whether any
13222 // stores were performed.
13223 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
13224 MachineFunction *F = MBB->getParent();
13225 MachineFunction::iterator MBBIter = MBB;
13226 ++MBBIter;
13227 MachineBasicBlock *XMMSaveMBB = F->CreateMachineBasicBlock(LLVM_BB);
13228 MachineBasicBlock *EndMBB = F->CreateMachineBasicBlock(LLVM_BB);
13229 F->insert(MBBIter, XMMSaveMBB);
13230 F->insert(MBBIter, EndMBB);
13231
Dan Gohman14152b42010-07-06 20:24:04 +000013232 // Transfer the remainder of MBB and its successor edges to EndMBB.
13233 EndMBB->splice(EndMBB->begin(), MBB,
13234 llvm::next(MachineBasicBlock::iterator(MI)),
13235 MBB->end());
13236 EndMBB->transferSuccessorsAndUpdatePHIs(MBB);
13237
Dan Gohmand6708ea2009-08-15 01:38:56 +000013238 // The original block will now fall through to the XMM save block.
13239 MBB->addSuccessor(XMMSaveMBB);
13240 // The XMMSaveMBB will fall through to the end block.
13241 XMMSaveMBB->addSuccessor(EndMBB);
13242
13243 // Now add the instructions.
13244 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
13245 DebugLoc DL = MI->getDebugLoc();
13246
13247 unsigned CountReg = MI->getOperand(0).getReg();
13248 int64_t RegSaveFrameIndex = MI->getOperand(1).getImm();
13249 int64_t VarArgsFPOffset = MI->getOperand(2).getImm();
13250
13251 if (!Subtarget->isTargetWin64()) {
13252 // If %al is 0, branch around the XMM save block.
13253 BuildMI(MBB, DL, TII->get(X86::TEST8rr)).addReg(CountReg).addReg(CountReg);
Chris Lattnerbd13fb62010-02-11 19:25:55 +000013254 BuildMI(MBB, DL, TII->get(X86::JE_4)).addMBB(EndMBB);
Dan Gohmand6708ea2009-08-15 01:38:56 +000013255 MBB->addSuccessor(EndMBB);
13256 }
13257
Bruno Cardoso Lopes5affa512011-08-31 03:04:09 +000013258 unsigned MOVOpc = Subtarget->hasAVX() ? X86::VMOVAPSmr : X86::MOVAPSmr;
Dan Gohmand6708ea2009-08-15 01:38:56 +000013259 // In the XMM save block, save all the XMM argument registers.
13260 for (int i = 3, e = MI->getNumOperands(); i != e; ++i) {
13261 int64_t Offset = (i - 3) * 16 + VarArgsFPOffset;
Dan Gohmanc76909a2009-09-25 20:36:54 +000013262 MachineMemOperand *MMO =
Evan Chengff89dcb2009-10-18 18:16:27 +000013263 F->getMachineMemOperand(
Chris Lattnere8639032010-09-21 06:22:23 +000013264 MachinePointerInfo::getFixedStack(RegSaveFrameIndex, Offset),
Chris Lattner59db5492010-09-21 04:39:43 +000013265 MachineMemOperand::MOStore,
Evan Chengff89dcb2009-10-18 18:16:27 +000013266 /*Size=*/16, /*Align=*/16);
Bruno Cardoso Lopes5affa512011-08-31 03:04:09 +000013267 BuildMI(XMMSaveMBB, DL, TII->get(MOVOpc))
Dan Gohmand6708ea2009-08-15 01:38:56 +000013268 .addFrameIndex(RegSaveFrameIndex)
13269 .addImm(/*Scale=*/1)
13270 .addReg(/*IndexReg=*/0)
13271 .addImm(/*Disp=*/Offset)
13272 .addReg(/*Segment=*/0)
13273 .addReg(MI->getOperand(i).getReg())
Dan Gohmanc76909a2009-09-25 20:36:54 +000013274 .addMemOperand(MMO);
Dan Gohmand6708ea2009-08-15 01:38:56 +000013275 }
13276
Dan Gohman14152b42010-07-06 20:24:04 +000013277 MI->eraseFromParent(); // The pseudo instruction is gone now.
Dan Gohmand6708ea2009-08-15 01:38:56 +000013278
13279 return EndMBB;
13280}
Mon P Wang63307c32008-05-05 19:05:59 +000013281
Lang Hames6e3f7e42012-02-03 01:13:49 +000013282// The EFLAGS operand of SelectItr might be missing a kill marker
13283// because there were multiple uses of EFLAGS, and ISel didn't know
13284// which to mark. Figure out whether SelectItr should have had a
13285// kill marker, and set it if it should. Returns the correct kill
13286// marker value.
13287static bool checkAndUpdateEFLAGSKill(MachineBasicBlock::iterator SelectItr,
13288 MachineBasicBlock* BB,
13289 const TargetRegisterInfo* TRI) {
13290 // Scan forward through BB for a use/def of EFLAGS.
13291 MachineBasicBlock::iterator miI(llvm::next(SelectItr));
13292 for (MachineBasicBlock::iterator miE = BB->end(); miI != miE; ++miI) {
Lang Hames50a36f72012-02-02 07:48:37 +000013293 const MachineInstr& mi = *miI;
Lang Hames6e3f7e42012-02-03 01:13:49 +000013294 if (mi.readsRegister(X86::EFLAGS))
Lang Hames50a36f72012-02-02 07:48:37 +000013295 return false;
Lang Hames6e3f7e42012-02-03 01:13:49 +000013296 if (mi.definesRegister(X86::EFLAGS))
13297 break; // Should have kill-flag - update below.
13298 }
13299
13300 // If we hit the end of the block, check whether EFLAGS is live into a
13301 // successor.
13302 if (miI == BB->end()) {
13303 for (MachineBasicBlock::succ_iterator sItr = BB->succ_begin(),
13304 sEnd = BB->succ_end();
13305 sItr != sEnd; ++sItr) {
13306 MachineBasicBlock* succ = *sItr;
13307 if (succ->isLiveIn(X86::EFLAGS))
13308 return false;
Lang Hames50a36f72012-02-02 07:48:37 +000013309 }
13310 }
13311
Lang Hames6e3f7e42012-02-03 01:13:49 +000013312 // We found a def, or hit the end of the basic block and EFLAGS wasn't live
13313 // out. SelectMI should have a kill flag on EFLAGS.
13314 SelectItr->addRegisterKilled(X86::EFLAGS, TRI);
Lang Hames50a36f72012-02-02 07:48:37 +000013315 return true;
13316}
13317
Evan Cheng60c07e12006-07-05 22:17:51 +000013318MachineBasicBlock *
Chris Lattner52600972009-09-02 05:57:00 +000013319X86TargetLowering::EmitLoweredSelect(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000013320 MachineBasicBlock *BB) const {
Chris Lattner52600972009-09-02 05:57:00 +000013321 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
13322 DebugLoc DL = MI->getDebugLoc();
Daniel Dunbara279bc32009-09-20 02:20:51 +000013323
Chris Lattner52600972009-09-02 05:57:00 +000013324 // To "insert" a SELECT_CC instruction, we actually have to insert the
13325 // diamond control-flow pattern. The incoming instruction knows the
13326 // destination vreg to set, the condition code register to branch on, the
13327 // true/false values to select between, and a branch opcode to use.
13328 const BasicBlock *LLVM_BB = BB->getBasicBlock();
13329 MachineFunction::iterator It = BB;
13330 ++It;
Daniel Dunbara279bc32009-09-20 02:20:51 +000013331
Chris Lattner52600972009-09-02 05:57:00 +000013332 // thisMBB:
13333 // ...
13334 // TrueVal = ...
13335 // cmpTY ccX, r1, r2
13336 // bCC copy1MBB
13337 // fallthrough --> copy0MBB
13338 MachineBasicBlock *thisMBB = BB;
13339 MachineFunction *F = BB->getParent();
13340 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
13341 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Chris Lattner52600972009-09-02 05:57:00 +000013342 F->insert(It, copy0MBB);
13343 F->insert(It, sinkMBB);
Bill Wendling730c07e2010-06-25 20:48:10 +000013344
Bill Wendling730c07e2010-06-25 20:48:10 +000013345 // If the EFLAGS register isn't dead in the terminator, then claim that it's
13346 // live into the sink and copy blocks.
Lang Hames6e3f7e42012-02-03 01:13:49 +000013347 const TargetRegisterInfo* TRI = getTargetMachine().getRegisterInfo();
13348 if (!MI->killsRegister(X86::EFLAGS) &&
13349 !checkAndUpdateEFLAGSKill(MI, BB, TRI)) {
13350 copy0MBB->addLiveIn(X86::EFLAGS);
13351 sinkMBB->addLiveIn(X86::EFLAGS);
Bill Wendling730c07e2010-06-25 20:48:10 +000013352 }
13353
Dan Gohman14152b42010-07-06 20:24:04 +000013354 // Transfer the remainder of BB and its successor edges to sinkMBB.
13355 sinkMBB->splice(sinkMBB->begin(), BB,
13356 llvm::next(MachineBasicBlock::iterator(MI)),
13357 BB->end());
13358 sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
13359
13360 // Add the true and fallthrough blocks as its successors.
13361 BB->addSuccessor(copy0MBB);
13362 BB->addSuccessor(sinkMBB);
13363
13364 // Create the conditional branch instruction.
13365 unsigned Opc =
13366 X86::GetCondBranchFromCond((X86::CondCode)MI->getOperand(3).getImm());
13367 BuildMI(BB, DL, TII->get(Opc)).addMBB(sinkMBB);
13368
Chris Lattner52600972009-09-02 05:57:00 +000013369 // copy0MBB:
13370 // %FalseValue = ...
13371 // # fallthrough to sinkMBB
Dan Gohman3335a222010-04-30 20:14:26 +000013372 copy0MBB->addSuccessor(sinkMBB);
Daniel Dunbara279bc32009-09-20 02:20:51 +000013373
Chris Lattner52600972009-09-02 05:57:00 +000013374 // sinkMBB:
13375 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
13376 // ...
Dan Gohman14152b42010-07-06 20:24:04 +000013377 BuildMI(*sinkMBB, sinkMBB->begin(), DL,
13378 TII->get(X86::PHI), MI->getOperand(0).getReg())
Chris Lattner52600972009-09-02 05:57:00 +000013379 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
13380 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
13381
Dan Gohman14152b42010-07-06 20:24:04 +000013382 MI->eraseFromParent(); // The pseudo instruction is gone now.
Dan Gohman3335a222010-04-30 20:14:26 +000013383 return sinkMBB;
Chris Lattner52600972009-09-02 05:57:00 +000013384}
13385
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000013386MachineBasicBlock *
Rafael Espindola151ab3e2011-08-30 19:47:04 +000013387X86TargetLowering::EmitLoweredSegAlloca(MachineInstr *MI, MachineBasicBlock *BB,
13388 bool Is64Bit) const {
13389 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
13390 DebugLoc DL = MI->getDebugLoc();
13391 MachineFunction *MF = BB->getParent();
13392 const BasicBlock *LLVM_BB = BB->getBasicBlock();
13393
Nick Lewycky8a8d4792011-12-02 22:16:29 +000013394 assert(getTargetMachine().Options.EnableSegmentedStacks);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000013395
13396 unsigned TlsReg = Is64Bit ? X86::FS : X86::GS;
13397 unsigned TlsOffset = Is64Bit ? 0x70 : 0x30;
13398
13399 // BB:
13400 // ... [Till the alloca]
13401 // If stacklet is not large enough, jump to mallocMBB
13402 //
13403 // bumpMBB:
13404 // Allocate by subtracting from RSP
13405 // Jump to continueMBB
13406 //
13407 // mallocMBB:
13408 // Allocate by call to runtime
13409 //
13410 // continueMBB:
13411 // ...
13412 // [rest of original BB]
13413 //
13414
13415 MachineBasicBlock *mallocMBB = MF->CreateMachineBasicBlock(LLVM_BB);
13416 MachineBasicBlock *bumpMBB = MF->CreateMachineBasicBlock(LLVM_BB);
13417 MachineBasicBlock *continueMBB = MF->CreateMachineBasicBlock(LLVM_BB);
13418
13419 MachineRegisterInfo &MRI = MF->getRegInfo();
13420 const TargetRegisterClass *AddrRegClass =
13421 getRegClassFor(Is64Bit ? MVT::i64:MVT::i32);
13422
13423 unsigned mallocPtrVReg = MRI.createVirtualRegister(AddrRegClass),
13424 bumpSPPtrVReg = MRI.createVirtualRegister(AddrRegClass),
13425 tmpSPVReg = MRI.createVirtualRegister(AddrRegClass),
Rafael Espindola66bf7432011-10-26 21:16:41 +000013426 SPLimitVReg = MRI.createVirtualRegister(AddrRegClass),
Rafael Espindola151ab3e2011-08-30 19:47:04 +000013427 sizeVReg = MI->getOperand(1).getReg(),
13428 physSPReg = Is64Bit ? X86::RSP : X86::ESP;
13429
13430 MachineFunction::iterator MBBIter = BB;
13431 ++MBBIter;
13432
13433 MF->insert(MBBIter, bumpMBB);
13434 MF->insert(MBBIter, mallocMBB);
13435 MF->insert(MBBIter, continueMBB);
13436
13437 continueMBB->splice(continueMBB->begin(), BB, llvm::next
13438 (MachineBasicBlock::iterator(MI)), BB->end());
13439 continueMBB->transferSuccessorsAndUpdatePHIs(BB);
13440
13441 // Add code to the main basic block to check if the stack limit has been hit,
13442 // and if so, jump to mallocMBB otherwise to bumpMBB.
13443 BuildMI(BB, DL, TII->get(TargetOpcode::COPY), tmpSPVReg).addReg(physSPReg);
Rafael Espindola66bf7432011-10-26 21:16:41 +000013444 BuildMI(BB, DL, TII->get(Is64Bit ? X86::SUB64rr:X86::SUB32rr), SPLimitVReg)
Rafael Espindola151ab3e2011-08-30 19:47:04 +000013445 .addReg(tmpSPVReg).addReg(sizeVReg);
13446 BuildMI(BB, DL, TII->get(Is64Bit ? X86::CMP64mr:X86::CMP32mr))
Rafael Espindola014f7a32012-01-11 18:14:03 +000013447 .addReg(0).addImm(1).addReg(0).addImm(TlsOffset).addReg(TlsReg)
Rafael Espindola66bf7432011-10-26 21:16:41 +000013448 .addReg(SPLimitVReg);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000013449 BuildMI(BB, DL, TII->get(X86::JG_4)).addMBB(mallocMBB);
13450
13451 // bumpMBB simply decreases the stack pointer, since we know the current
13452 // stacklet has enough space.
13453 BuildMI(bumpMBB, DL, TII->get(TargetOpcode::COPY), physSPReg)
Rafael Espindola66bf7432011-10-26 21:16:41 +000013454 .addReg(SPLimitVReg);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000013455 BuildMI(bumpMBB, DL, TII->get(TargetOpcode::COPY), bumpSPPtrVReg)
Rafael Espindola66bf7432011-10-26 21:16:41 +000013456 .addReg(SPLimitVReg);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000013457 BuildMI(bumpMBB, DL, TII->get(X86::JMP_4)).addMBB(continueMBB);
13458
13459 // Calls into a routine in libgcc to allocate more space from the heap.
Jakob Stoklund Olesen8bcde2a2012-02-16 00:02:50 +000013460 const uint32_t *RegMask =
13461 getTargetMachine().getRegisterInfo()->getCallPreservedMask(CallingConv::C);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000013462 if (Is64Bit) {
13463 BuildMI(mallocMBB, DL, TII->get(X86::MOV64rr), X86::RDI)
13464 .addReg(sizeVReg);
13465 BuildMI(mallocMBB, DL, TII->get(X86::CALL64pcrel32))
Jakob Stoklund Olesen85dccf12012-07-04 23:53:27 +000013466 .addExternalSymbol("__morestack_allocate_stack_space")
Jakob Stoklund Olesen8bcde2a2012-02-16 00:02:50 +000013467 .addRegMask(RegMask)
Jakob Stoklund Olesen85dccf12012-07-04 23:53:27 +000013468 .addReg(X86::RDI, RegState::Implicit)
Jakob Stoklund Olesen8bcde2a2012-02-16 00:02:50 +000013469 .addReg(X86::RAX, RegState::ImplicitDefine);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000013470 } else {
13471 BuildMI(mallocMBB, DL, TII->get(X86::SUB32ri), physSPReg).addReg(physSPReg)
13472 .addImm(12);
13473 BuildMI(mallocMBB, DL, TII->get(X86::PUSH32r)).addReg(sizeVReg);
13474 BuildMI(mallocMBB, DL, TII->get(X86::CALLpcrel32))
Jakob Stoklund Olesen8bcde2a2012-02-16 00:02:50 +000013475 .addExternalSymbol("__morestack_allocate_stack_space")
13476 .addRegMask(RegMask)
13477 .addReg(X86::EAX, RegState::ImplicitDefine);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000013478 }
13479
13480 if (!Is64Bit)
13481 BuildMI(mallocMBB, DL, TII->get(X86::ADD32ri), physSPReg).addReg(physSPReg)
13482 .addImm(16);
13483
13484 BuildMI(mallocMBB, DL, TII->get(TargetOpcode::COPY), mallocPtrVReg)
13485 .addReg(Is64Bit ? X86::RAX : X86::EAX);
13486 BuildMI(mallocMBB, DL, TII->get(X86::JMP_4)).addMBB(continueMBB);
13487
13488 // Set up the CFG correctly.
13489 BB->addSuccessor(bumpMBB);
13490 BB->addSuccessor(mallocMBB);
13491 mallocMBB->addSuccessor(continueMBB);
13492 bumpMBB->addSuccessor(continueMBB);
13493
13494 // Take care of the PHI nodes.
13495 BuildMI(*continueMBB, continueMBB->begin(), DL, TII->get(X86::PHI),
13496 MI->getOperand(0).getReg())
13497 .addReg(mallocPtrVReg).addMBB(mallocMBB)
13498 .addReg(bumpSPPtrVReg).addMBB(bumpMBB);
13499
13500 // Delete the original pseudo instruction.
13501 MI->eraseFromParent();
13502
13503 // And we're done.
13504 return continueMBB;
13505}
13506
13507MachineBasicBlock *
Michael J. Spencere9c253e2010-10-21 01:41:01 +000013508X86TargetLowering::EmitLoweredWinAlloca(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000013509 MachineBasicBlock *BB) const {
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000013510 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
13511 DebugLoc DL = MI->getDebugLoc();
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000013512
NAKAMURA Takumia2e07622011-03-24 07:07:00 +000013513 assert(!Subtarget->isTargetEnvMacho());
13514
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000013515 // The lowering is pretty easy: we're just emitting the call to _alloca. The
13516 // non-trivial part is impdef of ESP.
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000013517
NAKAMURA Takumia2e07622011-03-24 07:07:00 +000013518 if (Subtarget->isTargetWin64()) {
13519 if (Subtarget->isTargetCygMing()) {
13520 // ___chkstk(Mingw64):
13521 // Clobbers R10, R11, RAX and EFLAGS.
13522 // Updates RSP.
13523 BuildMI(*BB, MI, DL, TII->get(X86::W64ALLOCA))
13524 .addExternalSymbol("___chkstk")
13525 .addReg(X86::RAX, RegState::Implicit)
13526 .addReg(X86::RSP, RegState::Implicit)
13527 .addReg(X86::RAX, RegState::Define | RegState::Implicit)
13528 .addReg(X86::RSP, RegState::Define | RegState::Implicit)
13529 .addReg(X86::EFLAGS, RegState::Define | RegState::Implicit);
13530 } else {
13531 // __chkstk(MSVCRT): does not update stack pointer.
13532 // Clobbers R10, R11 and EFLAGS.
13533 // FIXME: RAX(allocated size) might be reused and not killed.
13534 BuildMI(*BB, MI, DL, TII->get(X86::W64ALLOCA))
13535 .addExternalSymbol("__chkstk")
13536 .addReg(X86::RAX, RegState::Implicit)
13537 .addReg(X86::EFLAGS, RegState::Define | RegState::Implicit);
13538 // RAX has the offset to subtracted from RSP.
13539 BuildMI(*BB, MI, DL, TII->get(X86::SUB64rr), X86::RSP)
13540 .addReg(X86::RSP)
13541 .addReg(X86::RAX);
13542 }
13543 } else {
13544 const char *StackProbeSymbol =
Michael J. Spencere9c253e2010-10-21 01:41:01 +000013545 Subtarget->isTargetWindows() ? "_chkstk" : "_alloca";
13546
NAKAMURA Takumia2e07622011-03-24 07:07:00 +000013547 BuildMI(*BB, MI, DL, TII->get(X86::CALLpcrel32))
13548 .addExternalSymbol(StackProbeSymbol)
13549 .addReg(X86::EAX, RegState::Implicit)
13550 .addReg(X86::ESP, RegState::Implicit)
13551 .addReg(X86::EAX, RegState::Define | RegState::Implicit)
13552 .addReg(X86::ESP, RegState::Define | RegState::Implicit)
13553 .addReg(X86::EFLAGS, RegState::Define | RegState::Implicit);
13554 }
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000013555
Dan Gohman14152b42010-07-06 20:24:04 +000013556 MI->eraseFromParent(); // The pseudo instruction is gone now.
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000013557 return BB;
13558}
Chris Lattner52600972009-09-02 05:57:00 +000013559
13560MachineBasicBlock *
Eric Christopher30ef0e52010-06-03 04:07:48 +000013561X86TargetLowering::EmitLoweredTLSCall(MachineInstr *MI,
13562 MachineBasicBlock *BB) const {
13563 // This is pretty easy. We're taking the value that we received from
13564 // our load from the relocation, sticking it in either RDI (x86-64)
13565 // or EAX and doing an indirect call. The return value will then
13566 // be in the normal return register.
Michael J. Spencerec38de22010-10-10 22:04:20 +000013567 const X86InstrInfo *TII
Eric Christopher54415362010-06-08 22:04:25 +000013568 = static_cast<const X86InstrInfo*>(getTargetMachine().getInstrInfo());
Eric Christopher30ef0e52010-06-03 04:07:48 +000013569 DebugLoc DL = MI->getDebugLoc();
13570 MachineFunction *F = BB->getParent();
Eric Christopher722d3152010-09-27 06:01:51 +000013571
13572 assert(Subtarget->isTargetDarwin() && "Darwin only instr emitted?");
Eric Christopher54415362010-06-08 22:04:25 +000013573 assert(MI->getOperand(3).isGlobal() && "This should be a global");
Michael J. Spencerec38de22010-10-10 22:04:20 +000013574
Jakob Stoklund Olesen8bcde2a2012-02-16 00:02:50 +000013575 // Get a register mask for the lowered call.
13576 // FIXME: The 32-bit calls have non-standard calling conventions. Use a
13577 // proper register mask.
13578 const uint32_t *RegMask =
13579 getTargetMachine().getRegisterInfo()->getCallPreservedMask(CallingConv::C);
Eric Christopher30ef0e52010-06-03 04:07:48 +000013580 if (Subtarget->is64Bit()) {
Dan Gohman14152b42010-07-06 20:24:04 +000013581 MachineInstrBuilder MIB = BuildMI(*BB, MI, DL,
13582 TII->get(X86::MOV64rm), X86::RDI)
Eric Christopher54415362010-06-08 22:04:25 +000013583 .addReg(X86::RIP)
13584 .addImm(0).addReg(0)
Michael J. Spencerec38de22010-10-10 22:04:20 +000013585 .addGlobalAddress(MI->getOperand(3).getGlobal(), 0,
Eric Christopher54415362010-06-08 22:04:25 +000013586 MI->getOperand(3).getTargetFlags())
13587 .addReg(0);
Eric Christopher722d3152010-09-27 06:01:51 +000013588 MIB = BuildMI(*BB, MI, DL, TII->get(X86::CALL64m));
Chris Lattner599b5312010-07-08 23:46:44 +000013589 addDirectMem(MIB, X86::RDI);
Jakob Stoklund Olesen8bcde2a2012-02-16 00:02:50 +000013590 MIB.addReg(X86::RAX, RegState::ImplicitDefine).addRegMask(RegMask);
Eric Christopher61025492010-06-15 23:08:42 +000013591 } else if (getTargetMachine().getRelocationModel() != Reloc::PIC_) {
Dan Gohman14152b42010-07-06 20:24:04 +000013592 MachineInstrBuilder MIB = BuildMI(*BB, MI, DL,
13593 TII->get(X86::MOV32rm), X86::EAX)
Eric Christopher61025492010-06-15 23:08:42 +000013594 .addReg(0)
13595 .addImm(0).addReg(0)
Michael J. Spencerec38de22010-10-10 22:04:20 +000013596 .addGlobalAddress(MI->getOperand(3).getGlobal(), 0,
Eric Christopher61025492010-06-15 23:08:42 +000013597 MI->getOperand(3).getTargetFlags())
13598 .addReg(0);
Dan Gohman14152b42010-07-06 20:24:04 +000013599 MIB = BuildMI(*BB, MI, DL, TII->get(X86::CALL32m));
Chris Lattner599b5312010-07-08 23:46:44 +000013600 addDirectMem(MIB, X86::EAX);
Jakob Stoklund Olesen8bcde2a2012-02-16 00:02:50 +000013601 MIB.addReg(X86::EAX, RegState::ImplicitDefine).addRegMask(RegMask);
Eric Christopher30ef0e52010-06-03 04:07:48 +000013602 } else {
Dan Gohman14152b42010-07-06 20:24:04 +000013603 MachineInstrBuilder MIB = BuildMI(*BB, MI, DL,
13604 TII->get(X86::MOV32rm), X86::EAX)
Eric Christopher54415362010-06-08 22:04:25 +000013605 .addReg(TII->getGlobalBaseReg(F))
13606 .addImm(0).addReg(0)
Michael J. Spencerec38de22010-10-10 22:04:20 +000013607 .addGlobalAddress(MI->getOperand(3).getGlobal(), 0,
Eric Christopher54415362010-06-08 22:04:25 +000013608 MI->getOperand(3).getTargetFlags())
13609 .addReg(0);
Dan Gohman14152b42010-07-06 20:24:04 +000013610 MIB = BuildMI(*BB, MI, DL, TII->get(X86::CALL32m));
Chris Lattner599b5312010-07-08 23:46:44 +000013611 addDirectMem(MIB, X86::EAX);
Jakob Stoklund Olesen8bcde2a2012-02-16 00:02:50 +000013612 MIB.addReg(X86::EAX, RegState::ImplicitDefine).addRegMask(RegMask);
Eric Christopher30ef0e52010-06-03 04:07:48 +000013613 }
Michael J. Spencerec38de22010-10-10 22:04:20 +000013614
Dan Gohman14152b42010-07-06 20:24:04 +000013615 MI->eraseFromParent(); // The pseudo instruction is gone now.
Eric Christopher30ef0e52010-06-03 04:07:48 +000013616 return BB;
13617}
13618
13619MachineBasicBlock *
Michael Liao6c0e04c2012-10-15 22:39:43 +000013620X86TargetLowering::emitEHSjLjSetJmp(MachineInstr *MI,
13621 MachineBasicBlock *MBB) const {
13622 DebugLoc DL = MI->getDebugLoc();
13623 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
13624
13625 MachineFunction *MF = MBB->getParent();
13626 MachineRegisterInfo &MRI = MF->getRegInfo();
13627
13628 const BasicBlock *BB = MBB->getBasicBlock();
13629 MachineFunction::iterator I = MBB;
13630 ++I;
13631
13632 // Memory Reference
13633 MachineInstr::mmo_iterator MMOBegin = MI->memoperands_begin();
13634 MachineInstr::mmo_iterator MMOEnd = MI->memoperands_end();
13635
13636 unsigned DstReg;
13637 unsigned MemOpndSlot = 0;
13638
13639 unsigned CurOp = 0;
13640
13641 DstReg = MI->getOperand(CurOp++).getReg();
13642 const TargetRegisterClass *RC = MRI.getRegClass(DstReg);
13643 assert(RC->hasType(MVT::i32) && "Invalid destination!");
13644 unsigned mainDstReg = MRI.createVirtualRegister(RC);
13645 unsigned restoreDstReg = MRI.createVirtualRegister(RC);
13646
13647 MemOpndSlot = CurOp;
13648
13649 MVT PVT = getPointerTy();
13650 assert((PVT == MVT::i64 || PVT == MVT::i32) &&
13651 "Invalid Pointer Size!");
13652
13653 // For v = setjmp(buf), we generate
13654 //
13655 // thisMBB:
Michael Liao281ae5a2012-10-17 02:22:27 +000013656 // buf[LabelOffset] = restoreMBB
Michael Liao6c0e04c2012-10-15 22:39:43 +000013657 // SjLjSetup restoreMBB
13658 //
13659 // mainMBB:
13660 // v_main = 0
13661 //
13662 // sinkMBB:
13663 // v = phi(main, restore)
13664 //
13665 // restoreMBB:
13666 // v_restore = 1
13667
13668 MachineBasicBlock *thisMBB = MBB;
13669 MachineBasicBlock *mainMBB = MF->CreateMachineBasicBlock(BB);
13670 MachineBasicBlock *sinkMBB = MF->CreateMachineBasicBlock(BB);
13671 MachineBasicBlock *restoreMBB = MF->CreateMachineBasicBlock(BB);
13672 MF->insert(I, mainMBB);
13673 MF->insert(I, sinkMBB);
13674 MF->push_back(restoreMBB);
13675
13676 MachineInstrBuilder MIB;
13677
13678 // Transfer the remainder of BB and its successor edges to sinkMBB.
13679 sinkMBB->splice(sinkMBB->begin(), MBB,
13680 llvm::next(MachineBasicBlock::iterator(MI)), MBB->end());
13681 sinkMBB->transferSuccessorsAndUpdatePHIs(MBB);
13682
13683 // thisMBB:
Michael Liao281ae5a2012-10-17 02:22:27 +000013684 unsigned PtrStoreOpc = 0;
13685 unsigned LabelReg = 0;
13686 const int64_t LabelOffset = 1 * PVT.getStoreSize();
13687 Reloc::Model RM = getTargetMachine().getRelocationModel();
13688 bool UseImmLabel = (getTargetMachine().getCodeModel() == CodeModel::Small) &&
13689 (RM == Reloc::Static || RM == Reloc::DynamicNoPIC);
Michael Liao6c0e04c2012-10-15 22:39:43 +000013690
Michael Liao281ae5a2012-10-17 02:22:27 +000013691 // Prepare IP either in reg or imm.
13692 if (!UseImmLabel) {
13693 PtrStoreOpc = (PVT == MVT::i64) ? X86::MOV64mr : X86::MOV32mr;
13694 const TargetRegisterClass *PtrRC = getRegClassFor(PVT);
13695 LabelReg = MRI.createVirtualRegister(PtrRC);
13696 if (Subtarget->is64Bit()) {
13697 MIB = BuildMI(*thisMBB, MI, DL, TII->get(X86::LEA64r), LabelReg)
13698 .addReg(X86::RIP)
13699 .addImm(0)
13700 .addReg(0)
13701 .addMBB(restoreMBB)
13702 .addReg(0);
13703 } else {
13704 const X86InstrInfo *XII = static_cast<const X86InstrInfo*>(TII);
13705 MIB = BuildMI(*thisMBB, MI, DL, TII->get(X86::LEA32r), LabelReg)
13706 .addReg(XII->getGlobalBaseReg(MF))
13707 .addImm(0)
13708 .addReg(0)
13709 .addMBB(restoreMBB, Subtarget->ClassifyBlockAddressReference())
13710 .addReg(0);
13711 }
13712 } else
13713 PtrStoreOpc = (PVT == MVT::i64) ? X86::MOV64mi32 : X86::MOV32mi;
Michael Liao6c0e04c2012-10-15 22:39:43 +000013714 // Store IP
Michael Liao281ae5a2012-10-17 02:22:27 +000013715 MIB = BuildMI(*thisMBB, MI, DL, TII->get(PtrStoreOpc));
Michael Liao6c0e04c2012-10-15 22:39:43 +000013716 for (unsigned i = 0; i < X86::AddrNumOperands; ++i) {
13717 if (i == X86::AddrDisp)
Michael Liao281ae5a2012-10-17 02:22:27 +000013718 MIB.addDisp(MI->getOperand(MemOpndSlot + i), LabelOffset);
Michael Liao6c0e04c2012-10-15 22:39:43 +000013719 else
13720 MIB.addOperand(MI->getOperand(MemOpndSlot + i));
13721 }
Michael Liao281ae5a2012-10-17 02:22:27 +000013722 if (!UseImmLabel)
13723 MIB.addReg(LabelReg);
13724 else
13725 MIB.addMBB(restoreMBB);
Michael Liao6c0e04c2012-10-15 22:39:43 +000013726 MIB.setMemRefs(MMOBegin, MMOEnd);
13727 // Setup
13728 MIB = BuildMI(*thisMBB, MI, DL, TII->get(X86::EH_SjLj_Setup))
13729 .addMBB(restoreMBB);
13730 MIB.addRegMask(RegInfo->getNoPreservedMask());
13731 thisMBB->addSuccessor(mainMBB);
13732 thisMBB->addSuccessor(restoreMBB);
13733
13734 // mainMBB:
13735 // EAX = 0
13736 BuildMI(mainMBB, DL, TII->get(X86::MOV32r0), mainDstReg);
13737 mainMBB->addSuccessor(sinkMBB);
13738
13739 // sinkMBB:
13740 BuildMI(*sinkMBB, sinkMBB->begin(), DL,
13741 TII->get(X86::PHI), DstReg)
13742 .addReg(mainDstReg).addMBB(mainMBB)
13743 .addReg(restoreDstReg).addMBB(restoreMBB);
13744
13745 // restoreMBB:
13746 BuildMI(restoreMBB, DL, TII->get(X86::MOV32ri), restoreDstReg).addImm(1);
13747 BuildMI(restoreMBB, DL, TII->get(X86::JMP_4)).addMBB(sinkMBB);
13748 restoreMBB->addSuccessor(sinkMBB);
13749
13750 MI->eraseFromParent();
13751 return sinkMBB;
13752}
13753
13754MachineBasicBlock *
13755X86TargetLowering::emitEHSjLjLongJmp(MachineInstr *MI,
13756 MachineBasicBlock *MBB) const {
13757 DebugLoc DL = MI->getDebugLoc();
13758 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
13759
13760 MachineFunction *MF = MBB->getParent();
13761 MachineRegisterInfo &MRI = MF->getRegInfo();
13762
13763 // Memory Reference
13764 MachineInstr::mmo_iterator MMOBegin = MI->memoperands_begin();
13765 MachineInstr::mmo_iterator MMOEnd = MI->memoperands_end();
13766
13767 MVT PVT = getPointerTy();
13768 assert((PVT == MVT::i64 || PVT == MVT::i32) &&
13769 "Invalid Pointer Size!");
13770
13771 const TargetRegisterClass *RC =
13772 (PVT == MVT::i64) ? &X86::GR64RegClass : &X86::GR32RegClass;
13773 unsigned Tmp = MRI.createVirtualRegister(RC);
13774 // Since FP is only updated here but NOT referenced, it's treated as GPR.
13775 unsigned FP = (PVT == MVT::i64) ? X86::RBP : X86::EBP;
13776 unsigned SP = RegInfo->getStackRegister();
13777
13778 MachineInstrBuilder MIB;
13779
Michael Liao281ae5a2012-10-17 02:22:27 +000013780 const int64_t LabelOffset = 1 * PVT.getStoreSize();
13781 const int64_t SPOffset = 2 * PVT.getStoreSize();
Michael Liao6c0e04c2012-10-15 22:39:43 +000013782
13783 unsigned PtrLoadOpc = (PVT == MVT::i64) ? X86::MOV64rm : X86::MOV32rm;
13784 unsigned IJmpOpc = (PVT == MVT::i64) ? X86::JMP64r : X86::JMP32r;
13785
13786 // Reload FP
13787 MIB = BuildMI(*MBB, MI, DL, TII->get(PtrLoadOpc), FP);
13788 for (unsigned i = 0; i < X86::AddrNumOperands; ++i)
13789 MIB.addOperand(MI->getOperand(i));
13790 MIB.setMemRefs(MMOBegin, MMOEnd);
13791 // Reload IP
13792 MIB = BuildMI(*MBB, MI, DL, TII->get(PtrLoadOpc), Tmp);
13793 for (unsigned i = 0; i < X86::AddrNumOperands; ++i) {
13794 if (i == X86::AddrDisp)
Michael Liao281ae5a2012-10-17 02:22:27 +000013795 MIB.addDisp(MI->getOperand(i), LabelOffset);
Michael Liao6c0e04c2012-10-15 22:39:43 +000013796 else
13797 MIB.addOperand(MI->getOperand(i));
13798 }
13799 MIB.setMemRefs(MMOBegin, MMOEnd);
13800 // Reload SP
13801 MIB = BuildMI(*MBB, MI, DL, TII->get(PtrLoadOpc), SP);
13802 for (unsigned i = 0; i < X86::AddrNumOperands; ++i) {
13803 if (i == X86::AddrDisp)
Michael Liao281ae5a2012-10-17 02:22:27 +000013804 MIB.addDisp(MI->getOperand(i), SPOffset);
Michael Liao6c0e04c2012-10-15 22:39:43 +000013805 else
13806 MIB.addOperand(MI->getOperand(i));
13807 }
13808 MIB.setMemRefs(MMOBegin, MMOEnd);
13809 // Jump
13810 BuildMI(*MBB, MI, DL, TII->get(IJmpOpc)).addReg(Tmp);
13811
13812 MI->eraseFromParent();
13813 return MBB;
13814}
13815
13816MachineBasicBlock *
Evan Chengff9b3732008-01-30 18:18:23 +000013817X86TargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000013818 MachineBasicBlock *BB) const {
Evan Cheng60c07e12006-07-05 22:17:51 +000013819 switch (MI->getOpcode()) {
Craig Topperabb94d02012-02-05 03:43:23 +000013820 default: llvm_unreachable("Unexpected instr type to insert");
NAKAMURA Takumi7754f852011-01-26 02:04:09 +000013821 case X86::TAILJMPd64:
13822 case X86::TAILJMPr64:
13823 case X86::TAILJMPm64:
Craig Topper6d1263a2012-02-05 05:38:58 +000013824 llvm_unreachable("TAILJMP64 would not be touched here.");
NAKAMURA Takumi7754f852011-01-26 02:04:09 +000013825 case X86::TCRETURNdi64:
13826 case X86::TCRETURNri64:
13827 case X86::TCRETURNmi64:
NAKAMURA Takumi7754f852011-01-26 02:04:09 +000013828 return BB;
Michael J. Spencere9c253e2010-10-21 01:41:01 +000013829 case X86::WIN_ALLOCA:
13830 return EmitLoweredWinAlloca(MI, BB);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000013831 case X86::SEG_ALLOCA_32:
13832 return EmitLoweredSegAlloca(MI, BB, false);
13833 case X86::SEG_ALLOCA_64:
13834 return EmitLoweredSegAlloca(MI, BB, true);
Eric Christopher30ef0e52010-06-03 04:07:48 +000013835 case X86::TLSCall_32:
13836 case X86::TLSCall_64:
13837 return EmitLoweredTLSCall(MI, BB);
Dan Gohmancbbea0f2009-08-27 00:14:12 +000013838 case X86::CMOV_GR8:
Evan Cheng60c07e12006-07-05 22:17:51 +000013839 case X86::CMOV_FR32:
13840 case X86::CMOV_FR64:
13841 case X86::CMOV_V4F32:
13842 case X86::CMOV_V2F64:
Chris Lattner52600972009-09-02 05:57:00 +000013843 case X86::CMOV_V2I64:
Bruno Cardoso Lopesd40aa242011-08-09 23:27:13 +000013844 case X86::CMOV_V8F32:
13845 case X86::CMOV_V4F64:
13846 case X86::CMOV_V4I64:
Chris Lattner314a1132010-03-14 18:31:44 +000013847 case X86::CMOV_GR16:
13848 case X86::CMOV_GR32:
13849 case X86::CMOV_RFP32:
13850 case X86::CMOV_RFP64:
13851 case X86::CMOV_RFP80:
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000013852 return EmitLoweredSelect(MI, BB);
Evan Cheng60c07e12006-07-05 22:17:51 +000013853
Dale Johannesen849f2142007-07-03 00:53:03 +000013854 case X86::FP32_TO_INT16_IN_MEM:
13855 case X86::FP32_TO_INT32_IN_MEM:
13856 case X86::FP32_TO_INT64_IN_MEM:
13857 case X86::FP64_TO_INT16_IN_MEM:
13858 case X86::FP64_TO_INT32_IN_MEM:
Dale Johannesena996d522007-08-07 01:17:37 +000013859 case X86::FP64_TO_INT64_IN_MEM:
13860 case X86::FP80_TO_INT16_IN_MEM:
13861 case X86::FP80_TO_INT32_IN_MEM:
13862 case X86::FP80_TO_INT64_IN_MEM: {
Chris Lattner52600972009-09-02 05:57:00 +000013863 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
13864 DebugLoc DL = MI->getDebugLoc();
13865
Evan Cheng60c07e12006-07-05 22:17:51 +000013866 // Change the floating point control register to use "round towards zero"
13867 // mode when truncating to an integer value.
13868 MachineFunction *F = BB->getParent();
David Greene3f2bf852009-11-12 20:49:22 +000013869 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2, false);
Dan Gohman14152b42010-07-06 20:24:04 +000013870 addFrameReference(BuildMI(*BB, MI, DL,
13871 TII->get(X86::FNSTCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000013872
13873 // Load the old value of the high byte of the control word...
13874 unsigned OldCW =
Craig Topperc9099502012-04-20 06:31:50 +000013875 F->getRegInfo().createVirtualRegister(&X86::GR16RegClass);
Dan Gohman14152b42010-07-06 20:24:04 +000013876 addFrameReference(BuildMI(*BB, MI, DL, TII->get(X86::MOV16rm), OldCW),
Dale Johannesene4d209d2009-02-03 20:21:25 +000013877 CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000013878
13879 // Set the high part to be round to zero...
Dan Gohman14152b42010-07-06 20:24:04 +000013880 addFrameReference(BuildMI(*BB, MI, DL, TII->get(X86::MOV16mi)), CWFrameIdx)
Evan Chengc0f64ff2006-11-27 23:37:22 +000013881 .addImm(0xC7F);
Evan Cheng60c07e12006-07-05 22:17:51 +000013882
13883 // Reload the modified control word now...
Dan Gohman14152b42010-07-06 20:24:04 +000013884 addFrameReference(BuildMI(*BB, MI, DL,
13885 TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000013886
13887 // Restore the memory image of control word to original value
Dan Gohman14152b42010-07-06 20:24:04 +000013888 addFrameReference(BuildMI(*BB, MI, DL, TII->get(X86::MOV16mr)), CWFrameIdx)
Evan Chengc0f64ff2006-11-27 23:37:22 +000013889 .addReg(OldCW);
Evan Cheng60c07e12006-07-05 22:17:51 +000013890
13891 // Get the X86 opcode to use.
13892 unsigned Opc;
13893 switch (MI->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +000013894 default: llvm_unreachable("illegal opcode!");
Dale Johannesene377d4d2007-07-04 21:07:47 +000013895 case X86::FP32_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m32; break;
13896 case X86::FP32_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m32; break;
13897 case X86::FP32_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m32; break;
13898 case X86::FP64_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m64; break;
13899 case X86::FP64_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m64; break;
13900 case X86::FP64_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m64; break;
Dale Johannesena996d522007-08-07 01:17:37 +000013901 case X86::FP80_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m80; break;
13902 case X86::FP80_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m80; break;
13903 case X86::FP80_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m80; break;
Evan Cheng60c07e12006-07-05 22:17:51 +000013904 }
13905
13906 X86AddressMode AM;
13907 MachineOperand &Op = MI->getOperand(0);
Dan Gohmand735b802008-10-03 15:45:36 +000013908 if (Op.isReg()) {
Evan Cheng60c07e12006-07-05 22:17:51 +000013909 AM.BaseType = X86AddressMode::RegBase;
13910 AM.Base.Reg = Op.getReg();
13911 } else {
13912 AM.BaseType = X86AddressMode::FrameIndexBase;
Chris Lattner8aa797a2007-12-30 23:10:15 +000013913 AM.Base.FrameIndex = Op.getIndex();
Evan Cheng60c07e12006-07-05 22:17:51 +000013914 }
13915 Op = MI->getOperand(1);
Dan Gohmand735b802008-10-03 15:45:36 +000013916 if (Op.isImm())
Chris Lattner7fbe9722006-10-20 17:42:20 +000013917 AM.Scale = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +000013918 Op = MI->getOperand(2);
Dan Gohmand735b802008-10-03 15:45:36 +000013919 if (Op.isImm())
Chris Lattner7fbe9722006-10-20 17:42:20 +000013920 AM.IndexReg = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +000013921 Op = MI->getOperand(3);
Dan Gohmand735b802008-10-03 15:45:36 +000013922 if (Op.isGlobal()) {
Evan Cheng60c07e12006-07-05 22:17:51 +000013923 AM.GV = Op.getGlobal();
13924 } else {
Chris Lattner7fbe9722006-10-20 17:42:20 +000013925 AM.Disp = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +000013926 }
Dan Gohman14152b42010-07-06 20:24:04 +000013927 addFullAddress(BuildMI(*BB, MI, DL, TII->get(Opc)), AM)
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000013928 .addReg(MI->getOperand(X86::AddrNumOperands).getReg());
Evan Cheng60c07e12006-07-05 22:17:51 +000013929
13930 // Reload the original control word now.
Dan Gohman14152b42010-07-06 20:24:04 +000013931 addFrameReference(BuildMI(*BB, MI, DL,
13932 TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000013933
Dan Gohman14152b42010-07-06 20:24:04 +000013934 MI->eraseFromParent(); // The pseudo instruction is gone now.
Evan Cheng60c07e12006-07-05 22:17:51 +000013935 return BB;
13936 }
Eric Christopherb120ab42009-08-18 22:50:32 +000013937 // String/text processing lowering.
13938 case X86::PCMPISTRM128REG:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000013939 case X86::VPCMPISTRM128REG:
Eric Christopherb120ab42009-08-18 22:50:32 +000013940 case X86::PCMPISTRM128MEM:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000013941 case X86::VPCMPISTRM128MEM:
Eric Christopherb120ab42009-08-18 22:50:32 +000013942 case X86::PCMPESTRM128REG:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000013943 case X86::VPCMPESTRM128REG:
Eric Christopherb120ab42009-08-18 22:50:32 +000013944 case X86::PCMPESTRM128MEM:
Craig Topper8aae8dd2012-11-10 08:57:41 +000013945 case X86::VPCMPESTRM128MEM:
13946 assert(Subtarget->hasSSE42() &&
13947 "Target must have SSE4.2 or AVX features enabled");
13948 return EmitPCMPSTRM(MI, BB, getTargetMachine().getInstrInfo());
Craig Topper9c7ae012012-11-10 01:23:36 +000013949
13950 // String/text processing lowering.
13951 case X86::PCMPISTRIREG:
13952 case X86::VPCMPISTRIREG:
13953 case X86::PCMPISTRIMEM:
13954 case X86::VPCMPISTRIMEM:
13955 case X86::PCMPESTRIREG:
13956 case X86::VPCMPESTRIREG:
13957 case X86::PCMPESTRIMEM:
Craig Topper8aae8dd2012-11-10 08:57:41 +000013958 case X86::VPCMPESTRIMEM:
13959 assert(Subtarget->hasSSE42() &&
13960 "Target must have SSE4.2 or AVX features enabled");
13961 return EmitPCMPSTRI(MI, BB, getTargetMachine().getInstrInfo());
Eric Christopherb120ab42009-08-18 22:50:32 +000013962
Craig Topper8aae8dd2012-11-10 08:57:41 +000013963 // Thread synchronization.
Eric Christopher228232b2010-11-30 07:20:12 +000013964 case X86::MONITOR:
Craig Topper2da36912012-11-11 22:45:02 +000013965 return EmitMonitor(MI, BB, getTargetMachine().getInstrInfo(), Subtarget);
Eric Christopher228232b2010-11-30 07:20:12 +000013966
Michael Liaobe02a902012-11-08 07:28:54 +000013967 // xbegin
13968 case X86::XBEGIN:
Craig Topper2da36912012-11-11 22:45:02 +000013969 return EmitXBegin(MI, BB, getTargetMachine().getInstrInfo());
Michael Liaobe02a902012-11-08 07:28:54 +000013970
Craig Topper8aae8dd2012-11-10 08:57:41 +000013971 // Atomic Lowering.
Dale Johannesen140be2d2008-08-19 18:47:28 +000013972 case X86::ATOMAND8:
Michael Liaob118a072012-09-20 03:06:15 +000013973 case X86::ATOMAND16:
13974 case X86::ATOMAND32:
Dale Johannesena99e3842008-08-20 00:48:50 +000013975 case X86::ATOMAND64:
Michael Liaob118a072012-09-20 03:06:15 +000013976 // Fall through
13977 case X86::ATOMOR8:
13978 case X86::ATOMOR16:
13979 case X86::ATOMOR32:
Dale Johannesena99e3842008-08-20 00:48:50 +000013980 case X86::ATOMOR64:
Michael Liaob118a072012-09-20 03:06:15 +000013981 // Fall through
13982 case X86::ATOMXOR16:
13983 case X86::ATOMXOR8:
13984 case X86::ATOMXOR32:
Dale Johannesena99e3842008-08-20 00:48:50 +000013985 case X86::ATOMXOR64:
Michael Liaob118a072012-09-20 03:06:15 +000013986 // Fall through
13987 case X86::ATOMNAND8:
13988 case X86::ATOMNAND16:
13989 case X86::ATOMNAND32:
13990 case X86::ATOMNAND64:
13991 // Fall through
Michael Liaofe87c302012-09-21 03:18:52 +000013992 case X86::ATOMMAX8:
Michael Liaob118a072012-09-20 03:06:15 +000013993 case X86::ATOMMAX16:
13994 case X86::ATOMMAX32:
13995 case X86::ATOMMAX64:
13996 // Fall through
Michael Liaofe87c302012-09-21 03:18:52 +000013997 case X86::ATOMMIN8:
Michael Liaob118a072012-09-20 03:06:15 +000013998 case X86::ATOMMIN16:
13999 case X86::ATOMMIN32:
14000 case X86::ATOMMIN64:
14001 // Fall through
Michael Liaofe87c302012-09-21 03:18:52 +000014002 case X86::ATOMUMAX8:
Michael Liaob118a072012-09-20 03:06:15 +000014003 case X86::ATOMUMAX16:
14004 case X86::ATOMUMAX32:
14005 case X86::ATOMUMAX64:
14006 // Fall through
Michael Liaofe87c302012-09-21 03:18:52 +000014007 case X86::ATOMUMIN8:
Michael Liaob118a072012-09-20 03:06:15 +000014008 case X86::ATOMUMIN16:
14009 case X86::ATOMUMIN32:
14010 case X86::ATOMUMIN64:
14011 return EmitAtomicLoadArith(MI, BB);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000014012
14013 // This group does 64-bit operations on a 32-bit host.
14014 case X86::ATOMAND6432:
Dale Johannesen48c1bc22008-10-02 18:53:47 +000014015 case X86::ATOMOR6432:
Dale Johannesen48c1bc22008-10-02 18:53:47 +000014016 case X86::ATOMXOR6432:
Dale Johannesen48c1bc22008-10-02 18:53:47 +000014017 case X86::ATOMNAND6432:
Dale Johannesen48c1bc22008-10-02 18:53:47 +000014018 case X86::ATOMADD6432:
Dale Johannesen48c1bc22008-10-02 18:53:47 +000014019 case X86::ATOMSUB6432:
Michael Liaoe5e8f762012-09-25 18:08:13 +000014020 case X86::ATOMMAX6432:
14021 case X86::ATOMMIN6432:
14022 case X86::ATOMUMAX6432:
14023 case X86::ATOMUMIN6432:
Michael Liaob118a072012-09-20 03:06:15 +000014024 case X86::ATOMSWAP6432:
14025 return EmitAtomicLoadArith6432(MI, BB);
Craig Topperacaaa6f2012-08-18 06:39:34 +000014026
Dan Gohmand6708ea2009-08-15 01:38:56 +000014027 case X86::VASTART_SAVE_XMM_REGS:
14028 return EmitVAStartSaveXMMRegsWithCustomInserter(MI, BB);
Dan Gohman320afb82010-10-12 18:00:49 +000014029
14030 case X86::VAARG_64:
14031 return EmitVAARG64WithCustomInserter(MI, BB);
Michael Liao6c0e04c2012-10-15 22:39:43 +000014032
14033 case X86::EH_SjLj_SetJmp32:
14034 case X86::EH_SjLj_SetJmp64:
14035 return emitEHSjLjSetJmp(MI, BB);
14036
14037 case X86::EH_SjLj_LongJmp32:
14038 case X86::EH_SjLj_LongJmp64:
14039 return emitEHSjLjLongJmp(MI, BB);
Evan Cheng60c07e12006-07-05 22:17:51 +000014040 }
14041}
14042
14043//===----------------------------------------------------------------------===//
14044// X86 Optimization Hooks
14045//===----------------------------------------------------------------------===//
14046
Dan Gohman475871a2008-07-27 21:46:04 +000014047void X86TargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
Dan Gohmanfd29e0e2008-02-13 00:35:47 +000014048 APInt &KnownZero,
14049 APInt &KnownOne,
Dan Gohmanea859be2007-06-22 14:59:07 +000014050 const SelectionDAG &DAG,
Nate Begeman368e18d2006-02-16 21:11:51 +000014051 unsigned Depth) const {
Rafael Espindola26c8dcc2012-04-04 12:51:34 +000014052 unsigned BitWidth = KnownZero.getBitWidth();
Evan Cheng3a03ebb2005-12-21 23:05:39 +000014053 unsigned Opc = Op.getOpcode();
Evan Cheng865f0602006-04-05 06:11:20 +000014054 assert((Opc >= ISD::BUILTIN_OP_END ||
14055 Opc == ISD::INTRINSIC_WO_CHAIN ||
14056 Opc == ISD::INTRINSIC_W_CHAIN ||
14057 Opc == ISD::INTRINSIC_VOID) &&
14058 "Should use MaskedValueIsZero if you don't know whether Op"
14059 " is a target node!");
Evan Cheng3a03ebb2005-12-21 23:05:39 +000014060
Rafael Espindola26c8dcc2012-04-04 12:51:34 +000014061 KnownZero = KnownOne = APInt(BitWidth, 0); // Don't know anything.
Evan Cheng3a03ebb2005-12-21 23:05:39 +000014062 switch (Opc) {
Evan Cheng865f0602006-04-05 06:11:20 +000014063 default: break;
Evan Cheng97d0e0e2009-02-02 09:15:04 +000014064 case X86ISD::ADD:
14065 case X86ISD::SUB:
Chris Lattner5b856542010-12-20 00:59:46 +000014066 case X86ISD::ADC:
14067 case X86ISD::SBB:
Evan Cheng97d0e0e2009-02-02 09:15:04 +000014068 case X86ISD::SMUL:
14069 case X86ISD::UMUL:
Dan Gohman076aee32009-03-04 19:44:21 +000014070 case X86ISD::INC:
14071 case X86ISD::DEC:
Dan Gohmane220c4b2009-09-18 19:59:53 +000014072 case X86ISD::OR:
14073 case X86ISD::XOR:
14074 case X86ISD::AND:
Evan Cheng97d0e0e2009-02-02 09:15:04 +000014075 // These nodes' second result is a boolean.
14076 if (Op.getResNo() == 0)
14077 break;
14078 // Fallthrough
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000014079 case X86ISD::SETCC:
Rafael Espindola26c8dcc2012-04-04 12:51:34 +000014080 KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - 1);
Nate Begeman368e18d2006-02-16 21:11:51 +000014081 break;
Evan Cheng7c1780c2011-10-07 17:21:44 +000014082 case ISD::INTRINSIC_WO_CHAIN: {
14083 unsigned IntId = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
14084 unsigned NumLoBits = 0;
14085 switch (IntId) {
14086 default: break;
14087 case Intrinsic::x86_sse_movmsk_ps:
14088 case Intrinsic::x86_avx_movmsk_ps_256:
14089 case Intrinsic::x86_sse2_movmsk_pd:
14090 case Intrinsic::x86_avx_movmsk_pd_256:
14091 case Intrinsic::x86_mmx_pmovmskb:
Craig Topper3738ccd2011-12-27 06:27:23 +000014092 case Intrinsic::x86_sse2_pmovmskb_128:
14093 case Intrinsic::x86_avx2_pmovmskb: {
Evan Cheng7c1780c2011-10-07 17:21:44 +000014094 // High bits of movmskp{s|d}, pmovmskb are known zero.
14095 switch (IntId) {
Craig Topperabb94d02012-02-05 03:43:23 +000014096 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
Evan Cheng7c1780c2011-10-07 17:21:44 +000014097 case Intrinsic::x86_sse_movmsk_ps: NumLoBits = 4; break;
14098 case Intrinsic::x86_avx_movmsk_ps_256: NumLoBits = 8; break;
14099 case Intrinsic::x86_sse2_movmsk_pd: NumLoBits = 2; break;
14100 case Intrinsic::x86_avx_movmsk_pd_256: NumLoBits = 4; break;
14101 case Intrinsic::x86_mmx_pmovmskb: NumLoBits = 8; break;
14102 case Intrinsic::x86_sse2_pmovmskb_128: NumLoBits = 16; break;
Craig Topper3738ccd2011-12-27 06:27:23 +000014103 case Intrinsic::x86_avx2_pmovmskb: NumLoBits = 32; break;
Evan Cheng7c1780c2011-10-07 17:21:44 +000014104 }
Rafael Espindola26c8dcc2012-04-04 12:51:34 +000014105 KnownZero = APInt::getHighBitsSet(BitWidth, BitWidth - NumLoBits);
Evan Cheng7c1780c2011-10-07 17:21:44 +000014106 break;
14107 }
14108 }
14109 break;
14110 }
Evan Cheng3a03ebb2005-12-21 23:05:39 +000014111 }
Evan Cheng3a03ebb2005-12-21 23:05:39 +000014112}
Chris Lattner259e97c2006-01-31 19:43:35 +000014113
Owen Andersonbc146b02010-09-21 20:42:50 +000014114unsigned X86TargetLowering::ComputeNumSignBitsForTargetNode(SDValue Op,
14115 unsigned Depth) const {
14116 // SETCC_CARRY sets the dest to ~0 for true or 0 for false.
14117 if (Op.getOpcode() == X86ISD::SETCC_CARRY)
14118 return Op.getValueType().getScalarType().getSizeInBits();
Michael J. Spencerec38de22010-10-10 22:04:20 +000014119
Owen Andersonbc146b02010-09-21 20:42:50 +000014120 // Fallback case.
14121 return 1;
14122}
14123
Evan Cheng206ee9d2006-07-07 08:33:52 +000014124/// isGAPlusOffset - Returns true (and the GlobalValue and the offset) if the
Evan Chengad4196b2008-05-12 19:56:52 +000014125/// node is a GlobalAddress + offset.
14126bool X86TargetLowering::isGAPlusOffset(SDNode *N,
Dan Gohman46510a72010-04-15 01:51:59 +000014127 const GlobalValue* &GA,
14128 int64_t &Offset) const {
Evan Chengad4196b2008-05-12 19:56:52 +000014129 if (N->getOpcode() == X86ISD::Wrapper) {
14130 if (isa<GlobalAddressSDNode>(N->getOperand(0))) {
Evan Cheng206ee9d2006-07-07 08:33:52 +000014131 GA = cast<GlobalAddressSDNode>(N->getOperand(0))->getGlobal();
Dan Gohman6520e202008-10-18 02:06:02 +000014132 Offset = cast<GlobalAddressSDNode>(N->getOperand(0))->getOffset();
Evan Cheng206ee9d2006-07-07 08:33:52 +000014133 return true;
14134 }
Evan Cheng206ee9d2006-07-07 08:33:52 +000014135 }
Evan Chengad4196b2008-05-12 19:56:52 +000014136 return TargetLowering::isGAPlusOffset(N, GA, Offset);
Evan Cheng206ee9d2006-07-07 08:33:52 +000014137}
14138
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000014139/// isShuffleHigh128VectorInsertLow - Checks whether the shuffle node is the
14140/// same as extracting the high 128-bit part of 256-bit vector and then
14141/// inserting the result into the low part of a new 256-bit vector
14142static bool isShuffleHigh128VectorInsertLow(ShuffleVectorSDNode *SVOp) {
14143 EVT VT = SVOp->getValueType(0);
Craig Topper66ddd152012-04-27 22:54:43 +000014144 unsigned NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000014145
14146 // vector_shuffle <4, 5, 6, 7, u, u, u, u> or <2, 3, u, u>
Craig Topper66ddd152012-04-27 22:54:43 +000014147 for (unsigned i = 0, j = NumElems/2; i != NumElems/2; ++i, ++j)
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000014148 if (!isUndefOrEqual(SVOp->getMaskElt(i), j) ||
14149 SVOp->getMaskElt(j) >= 0)
14150 return false;
14151
14152 return true;
14153}
14154
14155/// isShuffleLow128VectorInsertHigh - Checks whether the shuffle node is the
14156/// same as extracting the low 128-bit part of 256-bit vector and then
14157/// inserting the result into the high part of a new 256-bit vector
14158static bool isShuffleLow128VectorInsertHigh(ShuffleVectorSDNode *SVOp) {
14159 EVT VT = SVOp->getValueType(0);
Craig Topper66ddd152012-04-27 22:54:43 +000014160 unsigned NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000014161
14162 // vector_shuffle <u, u, u, u, 0, 1, 2, 3> or <u, u, 0, 1>
Craig Topper66ddd152012-04-27 22:54:43 +000014163 for (unsigned i = NumElems/2, j = 0; i != NumElems; ++i, ++j)
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000014164 if (!isUndefOrEqual(SVOp->getMaskElt(i), j) ||
14165 SVOp->getMaskElt(j) >= 0)
14166 return false;
14167
14168 return true;
14169}
14170
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000014171/// PerformShuffleCombine256 - Performs shuffle combines for 256-bit vectors.
14172static SDValue PerformShuffleCombine256(SDNode *N, SelectionDAG &DAG,
Craig Topper12216172012-01-13 08:12:35 +000014173 TargetLowering::DAGCombinerInfo &DCI,
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +000014174 const X86Subtarget* Subtarget) {
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000014175 DebugLoc dl = N->getDebugLoc();
14176 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
14177 SDValue V1 = SVOp->getOperand(0);
14178 SDValue V2 = SVOp->getOperand(1);
14179 EVT VT = SVOp->getValueType(0);
Craig Topper66ddd152012-04-27 22:54:43 +000014180 unsigned NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000014181
14182 if (V1.getOpcode() == ISD::CONCAT_VECTORS &&
14183 V2.getOpcode() == ISD::CONCAT_VECTORS) {
14184 //
14185 // 0,0,0,...
Benjamin Kramer558cc5a2011-07-22 01:02:57 +000014186 // |
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000014187 // V UNDEF BUILD_VECTOR UNDEF
14188 // \ / \ /
14189 // CONCAT_VECTOR CONCAT_VECTOR
14190 // \ /
14191 // \ /
14192 // RESULT: V + zero extended
14193 //
14194 if (V2.getOperand(0).getOpcode() != ISD::BUILD_VECTOR ||
14195 V2.getOperand(1).getOpcode() != ISD::UNDEF ||
14196 V1.getOperand(1).getOpcode() != ISD::UNDEF)
14197 return SDValue();
14198
14199 if (!ISD::isBuildVectorAllZeros(V2.getOperand(0).getNode()))
14200 return SDValue();
14201
14202 // To match the shuffle mask, the first half of the mask should
14203 // be exactly the first vector, and all the rest a splat with the
14204 // first element of the second one.
Craig Topper66ddd152012-04-27 22:54:43 +000014205 for (unsigned i = 0; i != NumElems/2; ++i)
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000014206 if (!isUndefOrEqual(SVOp->getMaskElt(i), i) ||
14207 !isUndefOrEqual(SVOp->getMaskElt(i+NumElems/2), NumElems))
14208 return SDValue();
14209
Chad Rosier3d1161e2012-01-03 21:05:52 +000014210 // If V1 is coming from a vector load then just fold to a VZEXT_LOAD.
14211 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(V1.getOperand(0))) {
Chad Rosier42726832012-05-07 18:47:44 +000014212 if (Ld->hasNUsesOfValue(1, 0)) {
14213 SDVTList Tys = DAG.getVTList(MVT::v4i64, MVT::Other);
14214 SDValue Ops[] = { Ld->getChain(), Ld->getBasePtr() };
14215 SDValue ResNode =
14216 DAG.getMemIntrinsicNode(X86ISD::VZEXT_LOAD, dl, Tys, Ops, 2,
14217 Ld->getMemoryVT(),
14218 Ld->getPointerInfo(),
14219 Ld->getAlignment(),
14220 false/*isVolatile*/, true/*ReadMem*/,
14221 false/*WriteMem*/);
Manman Ren2adc5032012-11-13 19:13:05 +000014222
14223 // Make sure the newly-created LOAD is in the same position as Ld in
14224 // terms of dependency. We create a TokenFactor for Ld and ResNode,
14225 // and update uses of Ld's output chain to use the TokenFactor.
14226 if (Ld->hasAnyUseOfValue(1)) {
14227 SDValue NewChain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
14228 SDValue(Ld, 1), SDValue(ResNode.getNode(), 1));
14229 DAG.ReplaceAllUsesOfValueWith(SDValue(Ld, 1), NewChain);
14230 DAG.UpdateNodeOperands(NewChain.getNode(), SDValue(Ld, 1),
14231 SDValue(ResNode.getNode(), 1));
14232 }
14233
Chad Rosier42726832012-05-07 18:47:44 +000014234 return DAG.getNode(ISD::BITCAST, dl, VT, ResNode);
14235 }
Chad Rosiera20e1e72012-08-01 18:39:17 +000014236 }
Chad Rosier3d1161e2012-01-03 21:05:52 +000014237
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000014238 // Emit a zeroed vector and insert the desired subvector on its
14239 // first half.
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +000014240 SDValue Zeros = getZeroVector(VT, Subtarget, DAG, dl);
Craig Topperb14940a2012-04-22 20:55:18 +000014241 SDValue InsV = Insert128BitVector(Zeros, V1.getOperand(0), 0, DAG, dl);
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000014242 return DCI.CombineTo(N, InsV);
14243 }
14244
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000014245 //===--------------------------------------------------------------------===//
14246 // Combine some shuffles into subvector extracts and inserts:
14247 //
14248
14249 // vector_shuffle <4, 5, 6, 7, u, u, u, u> or <2, 3, u, u>
14250 if (isShuffleHigh128VectorInsertLow(SVOp)) {
Craig Topperb14940a2012-04-22 20:55:18 +000014251 SDValue V = Extract128BitVector(V1, NumElems/2, DAG, dl);
14252 SDValue InsV = Insert128BitVector(DAG.getUNDEF(VT), V, 0, DAG, dl);
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000014253 return DCI.CombineTo(N, InsV);
14254 }
14255
14256 // vector_shuffle <u, u, u, u, 0, 1, 2, 3> or <u, u, 0, 1>
14257 if (isShuffleLow128VectorInsertHigh(SVOp)) {
Craig Topperb14940a2012-04-22 20:55:18 +000014258 SDValue V = Extract128BitVector(V1, 0, DAG, dl);
14259 SDValue InsV = Insert128BitVector(DAG.getUNDEF(VT), V, NumElems/2, DAG, dl);
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000014260 return DCI.CombineTo(N, InsV);
14261 }
14262
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000014263 return SDValue();
14264}
14265
14266/// PerformShuffleCombine - Performs several different shuffle combines.
Dan Gohman475871a2008-07-27 21:46:04 +000014267static SDValue PerformShuffleCombine(SDNode *N, SelectionDAG &DAG,
Bruno Cardoso Lopes50b37c72011-08-15 21:45:54 +000014268 TargetLowering::DAGCombinerInfo &DCI,
14269 const X86Subtarget *Subtarget) {
Dale Johannesene4d209d2009-02-03 20:21:25 +000014270 DebugLoc dl = N->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +000014271 EVT VT = N->getValueType(0);
Mon P Wang1e955802009-04-03 02:43:30 +000014272
Mon P Wanga0fd0d52010-12-19 23:55:53 +000014273 // Don't create instructions with illegal types after legalize types has run.
14274 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
14275 if (!DCI.isBeforeLegalize() && !TLI.isTypeLegal(VT.getVectorElementType()))
14276 return SDValue();
14277
Bruno Cardoso Lopes50b37c72011-08-15 21:45:54 +000014278 // Combine 256-bit vector shuffles. This is only profitable when in AVX mode
Craig Topper7a9a28b2012-08-12 02:23:29 +000014279 if (Subtarget->hasAVX() && VT.is256BitVector() &&
Bruno Cardoso Lopes50b37c72011-08-15 21:45:54 +000014280 N->getOpcode() == ISD::VECTOR_SHUFFLE)
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +000014281 return PerformShuffleCombine256(N, DAG, DCI, Subtarget);
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000014282
14283 // Only handle 128 wide vector from here on.
Craig Topper7a9a28b2012-08-12 02:23:29 +000014284 if (!VT.is128BitVector())
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000014285 return SDValue();
14286
14287 // Combine a vector_shuffle that is equal to build_vector load1, load2, load3,
14288 // load4, <0, 1, 2, 3> into a 128-bit load if the load addresses are
14289 // consecutive, non-overlapping, and in the right order.
Nate Begemanfdea31a2010-03-24 20:49:50 +000014290 SmallVector<SDValue, 16> Elts;
14291 for (unsigned i = 0, e = VT.getVectorNumElements(); i != e; ++i)
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000014292 Elts.push_back(getShuffleScalarElt(N, i, DAG, 0));
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +000014293
Nate Begemanfdea31a2010-03-24 20:49:50 +000014294 return EltsFromConsecutiveLoads(VT, Elts, dl, DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +000014295}
Evan Chengd880b972008-05-09 21:53:03 +000014296
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000014297
Craig Topper55b24052012-09-11 06:15:32 +000014298/// PerformTruncateCombine - Converts truncate operation to
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000014299/// a sequence of vector shuffle operations.
14300/// It is possible when we truncate 256-bit vector to 128-bit vector
Craig Topper55b24052012-09-11 06:15:32 +000014301static SDValue PerformTruncateCombine(SDNode *N, SelectionDAG &DAG,
14302 TargetLowering::DAGCombinerInfo &DCI,
14303 const X86Subtarget *Subtarget) {
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000014304 if (!DCI.isBeforeLegalizeOps())
14305 return SDValue();
14306
Craig Topper3ef43cf2012-04-24 06:36:35 +000014307 if (!Subtarget->hasAVX())
14308 return SDValue();
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000014309
14310 EVT VT = N->getValueType(0);
14311 SDValue Op = N->getOperand(0);
14312 EVT OpVT = Op.getValueType();
14313 DebugLoc dl = N->getDebugLoc();
14314
14315 if ((VT == MVT::v4i32) && (OpVT == MVT::v4i64)) {
14316
Elena Demikhovsky1da58672012-04-22 09:39:03 +000014317 if (Subtarget->hasAVX2()) {
14318 // AVX2: v4i64 -> v4i32
14319
14320 // VPERMD
14321 static const int ShufMask[] = {0, 2, 4, 6, -1, -1, -1, -1};
14322
14323 Op = DAG.getNode(ISD::BITCAST, dl, MVT::v8i32, Op);
14324 Op = DAG.getVectorShuffle(MVT::v8i32, dl, Op, DAG.getUNDEF(MVT::v8i32),
14325 ShufMask);
14326
Craig Topperd63fa652012-04-22 18:51:37 +000014327 return DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT, Op,
14328 DAG.getIntPtrConstant(0));
Elena Demikhovsky1da58672012-04-22 09:39:03 +000014329 }
14330
14331 // AVX: v4i64 -> v4i32
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000014332 SDValue OpLo = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v2i64, Op,
Craig Topperd63fa652012-04-22 18:51:37 +000014333 DAG.getIntPtrConstant(0));
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000014334
14335 SDValue OpHi = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v2i64, Op,
Craig Topperd63fa652012-04-22 18:51:37 +000014336 DAG.getIntPtrConstant(2));
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000014337
14338 OpLo = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, OpLo);
14339 OpHi = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, OpHi);
14340
14341 // PSHUFD
Craig Topper9e401f22012-04-21 18:58:38 +000014342 static const int ShufMask1[] = {0, 2, 0, 0};
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000014343
Craig Toppercacafd42012-08-14 08:18:43 +000014344 SDValue Undef = DAG.getUNDEF(VT);
14345 OpLo = DAG.getVectorShuffle(VT, dl, OpLo, Undef, ShufMask1);
14346 OpHi = DAG.getVectorShuffle(VT, dl, OpHi, Undef, ShufMask1);
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000014347
14348 // MOVLHPS
Craig Topper9e401f22012-04-21 18:58:38 +000014349 static const int ShufMask2[] = {0, 1, 4, 5};
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000014350
Elena Demikhovsky73252572012-02-01 10:33:05 +000014351 return DAG.getVectorShuffle(VT, dl, OpLo, OpHi, ShufMask2);
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000014352 }
Craig Topperd63fa652012-04-22 18:51:37 +000014353
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000014354 if ((VT == MVT::v8i16) && (OpVT == MVT::v8i32)) {
14355
Elena Demikhovsky1da58672012-04-22 09:39:03 +000014356 if (Subtarget->hasAVX2()) {
14357 // AVX2: v8i32 -> v8i16
14358
14359 Op = DAG.getNode(ISD::BITCAST, dl, MVT::v32i8, Op);
Craig Topperd63fa652012-04-22 18:51:37 +000014360
Elena Demikhovsky1da58672012-04-22 09:39:03 +000014361 // PSHUFB
14362 SmallVector<SDValue,32> pshufbMask;
14363 for (unsigned i = 0; i < 2; ++i) {
14364 pshufbMask.push_back(DAG.getConstant(0x0, MVT::i8));
14365 pshufbMask.push_back(DAG.getConstant(0x1, MVT::i8));
14366 pshufbMask.push_back(DAG.getConstant(0x4, MVT::i8));
14367 pshufbMask.push_back(DAG.getConstant(0x5, MVT::i8));
14368 pshufbMask.push_back(DAG.getConstant(0x8, MVT::i8));
14369 pshufbMask.push_back(DAG.getConstant(0x9, MVT::i8));
14370 pshufbMask.push_back(DAG.getConstant(0xc, MVT::i8));
14371 pshufbMask.push_back(DAG.getConstant(0xd, MVT::i8));
14372 for (unsigned j = 0; j < 8; ++j)
14373 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
14374 }
Craig Topperd63fa652012-04-22 18:51:37 +000014375 SDValue BV = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v32i8,
14376 &pshufbMask[0], 32);
Elena Demikhovsky1da58672012-04-22 09:39:03 +000014377 Op = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v32i8, Op, BV);
14378
14379 Op = DAG.getNode(ISD::BITCAST, dl, MVT::v4i64, Op);
14380
14381 static const int ShufMask[] = {0, 2, -1, -1};
Craig Topperd63fa652012-04-22 18:51:37 +000014382 Op = DAG.getVectorShuffle(MVT::v4i64, dl, Op, DAG.getUNDEF(MVT::v4i64),
Elena Demikhovsky1da58672012-04-22 09:39:03 +000014383 &ShufMask[0]);
14384
Craig Topperd63fa652012-04-22 18:51:37 +000014385 Op = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v2i64, Op,
14386 DAG.getIntPtrConstant(0));
Elena Demikhovsky1da58672012-04-22 09:39:03 +000014387
14388 return DAG.getNode(ISD::BITCAST, dl, VT, Op);
14389 }
14390
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000014391 SDValue OpLo = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i32, Op,
Craig Topperd63fa652012-04-22 18:51:37 +000014392 DAG.getIntPtrConstant(0));
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000014393
14394 SDValue OpHi = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i32, Op,
Craig Topperd63fa652012-04-22 18:51:37 +000014395 DAG.getIntPtrConstant(4));
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000014396
14397 OpLo = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, OpLo);
14398 OpHi = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, OpHi);
14399
14400 // PSHUFB
Craig Topper9e401f22012-04-21 18:58:38 +000014401 static const int ShufMask1[] = {0, 1, 4, 5, 8, 9, 12, 13,
14402 -1, -1, -1, -1, -1, -1, -1, -1};
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000014403
Craig Toppercacafd42012-08-14 08:18:43 +000014404 SDValue Undef = DAG.getUNDEF(MVT::v16i8);
14405 OpLo = DAG.getVectorShuffle(MVT::v16i8, dl, OpLo, Undef, ShufMask1);
14406 OpHi = DAG.getVectorShuffle(MVT::v16i8, dl, OpHi, Undef, ShufMask1);
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000014407
14408 OpLo = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, OpLo);
14409 OpHi = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, OpHi);
14410
14411 // MOVLHPS
Craig Topper9e401f22012-04-21 18:58:38 +000014412 static const int ShufMask2[] = {0, 1, 4, 5};
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000014413
Elena Demikhovsky73252572012-02-01 10:33:05 +000014414 SDValue res = DAG.getVectorShuffle(MVT::v4i32, dl, OpLo, OpHi, ShufMask2);
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000014415 return DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, res);
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000014416 }
14417
14418 return SDValue();
14419}
14420
Craig Topper89f4e662012-03-20 07:17:59 +000014421/// XFormVExtractWithShuffleIntoLoad - Check if a vector extract from a target
14422/// specific shuffle of a load can be folded into a single element load.
14423/// Similar handling for VECTOR_SHUFFLE is performed by DAGCombiner, but
14424/// shuffles have been customed lowered so we need to handle those here.
14425static SDValue XFormVExtractWithShuffleIntoLoad(SDNode *N, SelectionDAG &DAG,
14426 TargetLowering::DAGCombinerInfo &DCI) {
14427 if (DCI.isBeforeLegalizeOps())
14428 return SDValue();
14429
14430 SDValue InVec = N->getOperand(0);
14431 SDValue EltNo = N->getOperand(1);
14432
14433 if (!isa<ConstantSDNode>(EltNo))
14434 return SDValue();
14435
14436 EVT VT = InVec.getValueType();
14437
14438 bool HasShuffleIntoBitcast = false;
14439 if (InVec.getOpcode() == ISD::BITCAST) {
14440 // Don't duplicate a load with other uses.
14441 if (!InVec.hasOneUse())
14442 return SDValue();
14443 EVT BCVT = InVec.getOperand(0).getValueType();
14444 if (BCVT.getVectorNumElements() != VT.getVectorNumElements())
14445 return SDValue();
14446 InVec = InVec.getOperand(0);
14447 HasShuffleIntoBitcast = true;
14448 }
14449
14450 if (!isTargetShuffle(InVec.getOpcode()))
14451 return SDValue();
14452
14453 // Don't duplicate a load with other uses.
14454 if (!InVec.hasOneUse())
14455 return SDValue();
14456
14457 SmallVector<int, 16> ShuffleMask;
14458 bool UnaryShuffle;
Craig Topperd978c542012-05-06 19:46:21 +000014459 if (!getTargetShuffleMask(InVec.getNode(), VT.getSimpleVT(), ShuffleMask,
14460 UnaryShuffle))
Craig Topper89f4e662012-03-20 07:17:59 +000014461 return SDValue();
14462
14463 // Select the input vector, guarding against out of range extract vector.
14464 unsigned NumElems = VT.getVectorNumElements();
14465 int Elt = cast<ConstantSDNode>(EltNo)->getZExtValue();
14466 int Idx = (Elt > (int)NumElems) ? -1 : ShuffleMask[Elt];
14467 SDValue LdNode = (Idx < (int)NumElems) ? InVec.getOperand(0)
14468 : InVec.getOperand(1);
14469
14470 // If inputs to shuffle are the same for both ops, then allow 2 uses
14471 unsigned AllowedUses = InVec.getOperand(0) == InVec.getOperand(1) ? 2 : 1;
14472
14473 if (LdNode.getOpcode() == ISD::BITCAST) {
14474 // Don't duplicate a load with other uses.
14475 if (!LdNode.getNode()->hasNUsesOfValue(AllowedUses, 0))
14476 return SDValue();
14477
14478 AllowedUses = 1; // only allow 1 load use if we have a bitcast
14479 LdNode = LdNode.getOperand(0);
14480 }
14481
14482 if (!ISD::isNormalLoad(LdNode.getNode()))
14483 return SDValue();
14484
14485 LoadSDNode *LN0 = cast<LoadSDNode>(LdNode);
14486
14487 if (!LN0 ||!LN0->hasNUsesOfValue(AllowedUses, 0) || LN0->isVolatile())
14488 return SDValue();
14489
14490 if (HasShuffleIntoBitcast) {
14491 // If there's a bitcast before the shuffle, check if the load type and
14492 // alignment is valid.
14493 unsigned Align = LN0->getAlignment();
14494 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Micah Villmow3574eca2012-10-08 16:38:25 +000014495 unsigned NewAlign = TLI.getDataLayout()->
Craig Topper89f4e662012-03-20 07:17:59 +000014496 getABITypeAlignment(VT.getTypeForEVT(*DAG.getContext()));
14497
14498 if (NewAlign > Align || !TLI.isOperationLegalOrCustom(ISD::LOAD, VT))
14499 return SDValue();
14500 }
14501
14502 // All checks match so transform back to vector_shuffle so that DAG combiner
14503 // can finish the job
14504 DebugLoc dl = N->getDebugLoc();
14505
14506 // Create shuffle node taking into account the case that its a unary shuffle
14507 SDValue Shuffle = (UnaryShuffle) ? DAG.getUNDEF(VT) : InVec.getOperand(1);
14508 Shuffle = DAG.getVectorShuffle(InVec.getValueType(), dl,
14509 InVec.getOperand(0), Shuffle,
14510 &ShuffleMask[0]);
14511 Shuffle = DAG.getNode(ISD::BITCAST, dl, VT, Shuffle);
14512 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, N->getValueType(0), Shuffle,
14513 EltNo);
14514}
14515
Bruno Cardoso Lopesb3e06692010-09-03 19:55:05 +000014516/// PerformEXTRACT_VECTOR_ELTCombine - Detect vector gather/scatter index
14517/// generation and convert it from being a bunch of shuffles and extracts
14518/// to a simple store and scalar loads to extract the elements.
Dan Gohman1bbf72b2010-03-15 23:23:03 +000014519static SDValue PerformEXTRACT_VECTOR_ELTCombine(SDNode *N, SelectionDAG &DAG,
Craig Topper89f4e662012-03-20 07:17:59 +000014520 TargetLowering::DAGCombinerInfo &DCI) {
14521 SDValue NewOp = XFormVExtractWithShuffleIntoLoad(N, DAG, DCI);
14522 if (NewOp.getNode())
14523 return NewOp;
14524
Dan Gohman1bbf72b2010-03-15 23:23:03 +000014525 SDValue InputVector = N->getOperand(0);
Manman Ren4c74a952012-10-30 22:15:38 +000014526 // Detect whether we are trying to convert from mmx to i32 and the bitcast
14527 // from mmx to v2i32 has a single usage.
14528 if (InputVector.getNode()->getOpcode() == llvm::ISD::BITCAST &&
14529 InputVector.getNode()->getOperand(0).getValueType() == MVT::x86mmx &&
14530 InputVector.hasOneUse() && N->getValueType(0) == MVT::i32)
14531 return DAG.getNode(X86ISD::MMX_MOVD2W, InputVector.getDebugLoc(),
14532 N->getValueType(0),
14533 InputVector.getNode()->getOperand(0));
Dan Gohman1bbf72b2010-03-15 23:23:03 +000014534
14535 // Only operate on vectors of 4 elements, where the alternative shuffling
14536 // gets to be more expensive.
14537 if (InputVector.getValueType() != MVT::v4i32)
14538 return SDValue();
14539
14540 // Check whether every use of InputVector is an EXTRACT_VECTOR_ELT with a
14541 // single use which is a sign-extend or zero-extend, and all elements are
14542 // used.
14543 SmallVector<SDNode *, 4> Uses;
14544 unsigned ExtractedElements = 0;
14545 for (SDNode::use_iterator UI = InputVector.getNode()->use_begin(),
14546 UE = InputVector.getNode()->use_end(); UI != UE; ++UI) {
14547 if (UI.getUse().getResNo() != InputVector.getResNo())
14548 return SDValue();
14549
14550 SDNode *Extract = *UI;
14551 if (Extract->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
14552 return SDValue();
14553
14554 if (Extract->getValueType(0) != MVT::i32)
14555 return SDValue();
14556 if (!Extract->hasOneUse())
14557 return SDValue();
14558 if (Extract->use_begin()->getOpcode() != ISD::SIGN_EXTEND &&
14559 Extract->use_begin()->getOpcode() != ISD::ZERO_EXTEND)
14560 return SDValue();
14561 if (!isa<ConstantSDNode>(Extract->getOperand(1)))
14562 return SDValue();
14563
14564 // Record which element was extracted.
14565 ExtractedElements |=
14566 1 << cast<ConstantSDNode>(Extract->getOperand(1))->getZExtValue();
14567
14568 Uses.push_back(Extract);
14569 }
14570
14571 // If not all the elements were used, this may not be worthwhile.
14572 if (ExtractedElements != 15)
14573 return SDValue();
14574
14575 // Ok, we've now decided to do the transformation.
14576 DebugLoc dl = InputVector.getDebugLoc();
14577
14578 // Store the value to a temporary stack slot.
14579 SDValue StackPtr = DAG.CreateStackTemporary(InputVector.getValueType());
Chris Lattner8026a9d2010-09-21 17:50:43 +000014580 SDValue Ch = DAG.getStore(DAG.getEntryNode(), dl, InputVector, StackPtr,
14581 MachinePointerInfo(), false, false, 0);
Dan Gohman1bbf72b2010-03-15 23:23:03 +000014582
14583 // Replace each use (extract) with a load of the appropriate element.
14584 for (SmallVectorImpl<SDNode *>::iterator UI = Uses.begin(),
14585 UE = Uses.end(); UI != UE; ++UI) {
14586 SDNode *Extract = *UI;
14587
Nadav Rotem86694292011-05-17 08:31:57 +000014588 // cOMpute the element's address.
Dan Gohman1bbf72b2010-03-15 23:23:03 +000014589 SDValue Idx = Extract->getOperand(1);
14590 unsigned EltSize =
14591 InputVector.getValueType().getVectorElementType().getSizeInBits()/8;
14592 uint64_t Offset = EltSize * cast<ConstantSDNode>(Idx)->getZExtValue();
Craig Topper89f4e662012-03-20 07:17:59 +000014593 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Dan Gohman1bbf72b2010-03-15 23:23:03 +000014594 SDValue OffsetVal = DAG.getConstant(Offset, TLI.getPointerTy());
14595
Nadav Rotem86694292011-05-17 08:31:57 +000014596 SDValue ScalarAddr = DAG.getNode(ISD::ADD, dl, TLI.getPointerTy(),
Chris Lattner51abfe42010-09-21 06:02:19 +000014597 StackPtr, OffsetVal);
Dan Gohman1bbf72b2010-03-15 23:23:03 +000014598
14599 // Load the scalar.
Eric Christopher90eb4022010-07-22 00:26:08 +000014600 SDValue LoadScalar = DAG.getLoad(Extract->getValueType(0), dl, Ch,
Chris Lattner51abfe42010-09-21 06:02:19 +000014601 ScalarAddr, MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000014602 false, false, false, 0);
Dan Gohman1bbf72b2010-03-15 23:23:03 +000014603
14604 // Replace the exact with the load.
14605 DAG.ReplaceAllUsesOfValueWith(SDValue(Extract, 0), LoadScalar);
14606 }
14607
14608 // The replacement was made in place; don't return anything.
14609 return SDValue();
14610}
14611
Duncan Sands6bcd2192011-09-17 16:49:39 +000014612/// PerformSELECTCombine - Do target-specific dag combines on SELECT and VSELECT
14613/// nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000014614static SDValue PerformSELECTCombine(SDNode *N, SelectionDAG &DAG,
Nadav Rotemcc616562012-01-15 19:27:55 +000014615 TargetLowering::DAGCombinerInfo &DCI,
Chris Lattner47b4ce82009-03-11 05:48:52 +000014616 const X86Subtarget *Subtarget) {
14617 DebugLoc DL = N->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +000014618 SDValue Cond = N->getOperand(0);
Chris Lattner47b4ce82009-03-11 05:48:52 +000014619 // Get the LHS/RHS of the select.
14620 SDValue LHS = N->getOperand(1);
14621 SDValue RHS = N->getOperand(2);
Bruno Cardoso Lopes149f29f2011-09-20 22:34:45 +000014622 EVT VT = LHS.getValueType();
Eric Christopherfd179292009-08-27 18:07:15 +000014623
Dan Gohman670e5392009-09-21 18:03:22 +000014624 // If we have SSE[12] support, try to form min/max nodes. SSE min/max
Dan Gohman8ce05da2010-02-22 04:03:39 +000014625 // instructions match the semantics of the common C idiom x<y?x:y but not
14626 // x<=y?x:y, because of how they handle negative zero (which can be
14627 // ignored in unsafe-math mode).
Benjamin Kramer2c2ccbf2011-09-22 03:27:22 +000014628 if (Cond.getOpcode() == ISD::SETCC && VT.isFloatingPoint() &&
14629 VT != MVT::f80 && DAG.getTargetLoweringInfo().isTypeLegal(VT) &&
Craig Topper1accb7e2012-01-10 06:54:16 +000014630 (Subtarget->hasSSE2() ||
14631 (Subtarget->hasSSE1() && VT.getScalarType() == MVT::f32))) {
Chris Lattner47b4ce82009-03-11 05:48:52 +000014632 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000014633
Chris Lattner47b4ce82009-03-11 05:48:52 +000014634 unsigned Opcode = 0;
Dan Gohman670e5392009-09-21 18:03:22 +000014635 // Check for x CC y ? x : y.
Dan Gohmane8326932010-02-24 06:52:40 +000014636 if (DAG.isEqualTo(LHS, Cond.getOperand(0)) &&
14637 DAG.isEqualTo(RHS, Cond.getOperand(1))) {
Chris Lattner47b4ce82009-03-11 05:48:52 +000014638 switch (CC) {
14639 default: break;
Dan Gohman670e5392009-09-21 18:03:22 +000014640 case ISD::SETULT:
Dan Gohmane8326932010-02-24 06:52:40 +000014641 // Converting this to a min would handle NaNs incorrectly, and swapping
14642 // the operands would cause it to handle comparisons between positive
14643 // and negative zero incorrectly.
Evan Cheng60108e92010-07-15 22:07:12 +000014644 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)) {
Nick Lewycky8a8d4792011-12-02 22:16:29 +000014645 if (!DAG.getTarget().Options.UnsafeFPMath &&
Dan Gohmane8326932010-02-24 06:52:40 +000014646 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
14647 break;
14648 std::swap(LHS, RHS);
14649 }
Dan Gohman670e5392009-09-21 18:03:22 +000014650 Opcode = X86ISD::FMIN;
14651 break;
14652 case ISD::SETOLE:
Dan Gohmane8326932010-02-24 06:52:40 +000014653 // Converting this to a min would handle comparisons between positive
14654 // and negative zero incorrectly.
Nick Lewycky8a8d4792011-12-02 22:16:29 +000014655 if (!DAG.getTarget().Options.UnsafeFPMath &&
Dan Gohmane8326932010-02-24 06:52:40 +000014656 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(RHS))
14657 break;
Dan Gohman670e5392009-09-21 18:03:22 +000014658 Opcode = X86ISD::FMIN;
14659 break;
Chris Lattner47b4ce82009-03-11 05:48:52 +000014660 case ISD::SETULE:
Dan Gohmane8326932010-02-24 06:52:40 +000014661 // Converting this to a min would handle both negative zeros and NaNs
14662 // incorrectly, but we can swap the operands to fix both.
14663 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000014664 case ISD::SETOLT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000014665 case ISD::SETLT:
Dan Gohman670e5392009-09-21 18:03:22 +000014666 case ISD::SETLE:
Chris Lattner47b4ce82009-03-11 05:48:52 +000014667 Opcode = X86ISD::FMIN;
14668 break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000014669
Dan Gohman670e5392009-09-21 18:03:22 +000014670 case ISD::SETOGE:
Dan Gohmane8326932010-02-24 06:52:40 +000014671 // Converting this to a max would handle comparisons between positive
14672 // and negative zero incorrectly.
Nick Lewycky8a8d4792011-12-02 22:16:29 +000014673 if (!DAG.getTarget().Options.UnsafeFPMath &&
Evan Chengdd5663c2011-08-04 18:38:15 +000014674 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000014675 break;
Dan Gohman670e5392009-09-21 18:03:22 +000014676 Opcode = X86ISD::FMAX;
14677 break;
Chris Lattner47b4ce82009-03-11 05:48:52 +000014678 case ISD::SETUGT:
Dan Gohmane8326932010-02-24 06:52:40 +000014679 // Converting this to a max would handle NaNs incorrectly, and swapping
14680 // the operands would cause it to handle comparisons between positive
14681 // and negative zero incorrectly.
Evan Cheng60108e92010-07-15 22:07:12 +000014682 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)) {
Nick Lewycky8a8d4792011-12-02 22:16:29 +000014683 if (!DAG.getTarget().Options.UnsafeFPMath &&
Dan Gohmane8326932010-02-24 06:52:40 +000014684 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
14685 break;
14686 std::swap(LHS, RHS);
14687 }
Dan Gohman670e5392009-09-21 18:03:22 +000014688 Opcode = X86ISD::FMAX;
14689 break;
14690 case ISD::SETUGE:
Dan Gohmane8326932010-02-24 06:52:40 +000014691 // Converting this to a max would handle both negative zeros and NaNs
14692 // incorrectly, but we can swap the operands to fix both.
14693 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000014694 case ISD::SETOGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000014695 case ISD::SETGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000014696 case ISD::SETGE:
14697 Opcode = X86ISD::FMAX;
14698 break;
Chris Lattner83e6c992006-10-04 06:57:07 +000014699 }
Dan Gohman670e5392009-09-21 18:03:22 +000014700 // Check for x CC y ? y : x -- a min/max with reversed arms.
Dan Gohmane8326932010-02-24 06:52:40 +000014701 } else if (DAG.isEqualTo(LHS, Cond.getOperand(1)) &&
14702 DAG.isEqualTo(RHS, Cond.getOperand(0))) {
Chris Lattner47b4ce82009-03-11 05:48:52 +000014703 switch (CC) {
14704 default: break;
Dan Gohman670e5392009-09-21 18:03:22 +000014705 case ISD::SETOGE:
Dan Gohmane8326932010-02-24 06:52:40 +000014706 // Converting this to a min would handle comparisons between positive
14707 // and negative zero incorrectly, and swapping the operands would
14708 // cause it to handle NaNs incorrectly.
Nick Lewycky8a8d4792011-12-02 22:16:29 +000014709 if (!DAG.getTarget().Options.UnsafeFPMath &&
Dan Gohmane8326932010-02-24 06:52:40 +000014710 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS))) {
Evan Cheng60108e92010-07-15 22:07:12 +000014711 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000014712 break;
14713 std::swap(LHS, RHS);
14714 }
Dan Gohman670e5392009-09-21 18:03:22 +000014715 Opcode = X86ISD::FMIN;
Dan Gohman8d44b282009-09-03 20:34:31 +000014716 break;
Dan Gohman670e5392009-09-21 18:03:22 +000014717 case ISD::SETUGT:
Dan Gohmane8326932010-02-24 06:52:40 +000014718 // Converting this to a min would handle NaNs incorrectly.
Nick Lewycky8a8d4792011-12-02 22:16:29 +000014719 if (!DAG.getTarget().Options.UnsafeFPMath &&
Dan Gohmane8326932010-02-24 06:52:40 +000014720 (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)))
14721 break;
Dan Gohman670e5392009-09-21 18:03:22 +000014722 Opcode = X86ISD::FMIN;
14723 break;
14724 case ISD::SETUGE:
Dan Gohmane8326932010-02-24 06:52:40 +000014725 // Converting this to a min would handle both negative zeros and NaNs
14726 // incorrectly, but we can swap the operands to fix both.
14727 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000014728 case ISD::SETOGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000014729 case ISD::SETGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000014730 case ISD::SETGE:
14731 Opcode = X86ISD::FMIN;
14732 break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000014733
Dan Gohman670e5392009-09-21 18:03:22 +000014734 case ISD::SETULT:
Dan Gohmane8326932010-02-24 06:52:40 +000014735 // Converting this to a max would handle NaNs incorrectly.
Evan Cheng60108e92010-07-15 22:07:12 +000014736 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000014737 break;
Dan Gohman670e5392009-09-21 18:03:22 +000014738 Opcode = X86ISD::FMAX;
Dan Gohman8d44b282009-09-03 20:34:31 +000014739 break;
Dan Gohman670e5392009-09-21 18:03:22 +000014740 case ISD::SETOLE:
Dan Gohmane8326932010-02-24 06:52:40 +000014741 // Converting this to a max would handle comparisons between positive
14742 // and negative zero incorrectly, and swapping the operands would
14743 // cause it to handle NaNs incorrectly.
Nick Lewycky8a8d4792011-12-02 22:16:29 +000014744 if (!DAG.getTarget().Options.UnsafeFPMath &&
Dan Gohmane8326932010-02-24 06:52:40 +000014745 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(RHS)) {
Evan Cheng60108e92010-07-15 22:07:12 +000014746 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000014747 break;
14748 std::swap(LHS, RHS);
14749 }
Dan Gohman670e5392009-09-21 18:03:22 +000014750 Opcode = X86ISD::FMAX;
14751 break;
14752 case ISD::SETULE:
Dan Gohmane8326932010-02-24 06:52:40 +000014753 // Converting this to a max would handle both negative zeros and NaNs
14754 // incorrectly, but we can swap the operands to fix both.
14755 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000014756 case ISD::SETOLT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000014757 case ISD::SETLT:
Dan Gohman670e5392009-09-21 18:03:22 +000014758 case ISD::SETLE:
Chris Lattner47b4ce82009-03-11 05:48:52 +000014759 Opcode = X86ISD::FMAX;
14760 break;
14761 }
Chris Lattner83e6c992006-10-04 06:57:07 +000014762 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000014763
Chris Lattner47b4ce82009-03-11 05:48:52 +000014764 if (Opcode)
14765 return DAG.getNode(Opcode, DL, N->getValueType(0), LHS, RHS);
Chris Lattner83e6c992006-10-04 06:57:07 +000014766 }
Eric Christopherfd179292009-08-27 18:07:15 +000014767
Chris Lattnerd1980a52009-03-12 06:52:53 +000014768 // If this is a select between two integer constants, try to do some
14769 // optimizations.
Chris Lattnercee56e72009-03-13 05:53:31 +000014770 if (ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(LHS)) {
14771 if (ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(RHS))
Chris Lattnerd1980a52009-03-12 06:52:53 +000014772 // Don't do this for crazy integer types.
14773 if (DAG.getTargetLoweringInfo().isTypeLegal(LHS.getValueType())) {
14774 // If this is efficiently invertible, canonicalize the LHSC/RHSC values
Chris Lattnercee56e72009-03-13 05:53:31 +000014775 // so that TrueC (the true value) is larger than FalseC.
Chris Lattnerd1980a52009-03-12 06:52:53 +000014776 bool NeedsCondInvert = false;
Eric Christopherfd179292009-08-27 18:07:15 +000014777
Chris Lattnercee56e72009-03-13 05:53:31 +000014778 if (TrueC->getAPIntValue().ult(FalseC->getAPIntValue()) &&
Chris Lattnerd1980a52009-03-12 06:52:53 +000014779 // Efficiently invertible.
14780 (Cond.getOpcode() == ISD::SETCC || // setcc -> invertible.
14781 (Cond.getOpcode() == ISD::XOR && // xor(X, C) -> invertible.
14782 isa<ConstantSDNode>(Cond.getOperand(1))))) {
14783 NeedsCondInvert = true;
Chris Lattnercee56e72009-03-13 05:53:31 +000014784 std::swap(TrueC, FalseC);
Chris Lattnerd1980a52009-03-12 06:52:53 +000014785 }
Eric Christopherfd179292009-08-27 18:07:15 +000014786
Chris Lattnerd1980a52009-03-12 06:52:53 +000014787 // Optimize C ? 8 : 0 -> zext(C) << 3. Likewise for any pow2/0.
Chris Lattnercee56e72009-03-13 05:53:31 +000014788 if (FalseC->getAPIntValue() == 0 &&
14789 TrueC->getAPIntValue().isPowerOf2()) {
Chris Lattnerd1980a52009-03-12 06:52:53 +000014790 if (NeedsCondInvert) // Invert the condition if needed.
14791 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
14792 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000014793
Chris Lattnerd1980a52009-03-12 06:52:53 +000014794 // Zero extend the condition if needed.
14795 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, LHS.getValueType(), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000014796
Chris Lattnercee56e72009-03-13 05:53:31 +000014797 unsigned ShAmt = TrueC->getAPIntValue().logBase2();
Chris Lattnerd1980a52009-03-12 06:52:53 +000014798 return DAG.getNode(ISD::SHL, DL, LHS.getValueType(), Cond,
Owen Anderson825b72b2009-08-11 20:47:22 +000014799 DAG.getConstant(ShAmt, MVT::i8));
Chris Lattnerd1980a52009-03-12 06:52:53 +000014800 }
Eric Christopherfd179292009-08-27 18:07:15 +000014801
Chris Lattner97a29a52009-03-13 05:22:11 +000014802 // Optimize Cond ? cst+1 : cst -> zext(setcc(C)+cst.
Chris Lattnercee56e72009-03-13 05:53:31 +000014803 if (FalseC->getAPIntValue()+1 == TrueC->getAPIntValue()) {
Chris Lattner97a29a52009-03-13 05:22:11 +000014804 if (NeedsCondInvert) // Invert the condition if needed.
14805 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
14806 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000014807
Chris Lattner97a29a52009-03-13 05:22:11 +000014808 // Zero extend the condition if needed.
Chris Lattnercee56e72009-03-13 05:53:31 +000014809 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL,
14810 FalseC->getValueType(0), Cond);
Chris Lattner97a29a52009-03-13 05:22:11 +000014811 return DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
Chris Lattnercee56e72009-03-13 05:53:31 +000014812 SDValue(FalseC, 0));
Chris Lattner97a29a52009-03-13 05:22:11 +000014813 }
Eric Christopherfd179292009-08-27 18:07:15 +000014814
Chris Lattnercee56e72009-03-13 05:53:31 +000014815 // Optimize cases that will turn into an LEA instruction. This requires
14816 // an i32 or i64 and an efficient multiplier (1, 2, 3, 4, 5, 8, 9).
Owen Anderson825b72b2009-08-11 20:47:22 +000014817 if (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i64) {
Chris Lattnercee56e72009-03-13 05:53:31 +000014818 uint64_t Diff = TrueC->getZExtValue()-FalseC->getZExtValue();
Owen Anderson825b72b2009-08-11 20:47:22 +000014819 if (N->getValueType(0) == MVT::i32) Diff = (unsigned)Diff;
Eric Christopherfd179292009-08-27 18:07:15 +000014820
Chris Lattnercee56e72009-03-13 05:53:31 +000014821 bool isFastMultiplier = false;
14822 if (Diff < 10) {
14823 switch ((unsigned char)Diff) {
14824 default: break;
14825 case 1: // result = add base, cond
14826 case 2: // result = lea base( , cond*2)
14827 case 3: // result = lea base(cond, cond*2)
14828 case 4: // result = lea base( , cond*4)
14829 case 5: // result = lea base(cond, cond*4)
14830 case 8: // result = lea base( , cond*8)
14831 case 9: // result = lea base(cond, cond*8)
14832 isFastMultiplier = true;
14833 break;
14834 }
14835 }
Eric Christopherfd179292009-08-27 18:07:15 +000014836
Chris Lattnercee56e72009-03-13 05:53:31 +000014837 if (isFastMultiplier) {
14838 APInt Diff = TrueC->getAPIntValue()-FalseC->getAPIntValue();
14839 if (NeedsCondInvert) // Invert the condition if needed.
14840 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
14841 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000014842
Chris Lattnercee56e72009-03-13 05:53:31 +000014843 // Zero extend the condition if needed.
14844 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, FalseC->getValueType(0),
14845 Cond);
14846 // Scale the condition by the difference.
14847 if (Diff != 1)
14848 Cond = DAG.getNode(ISD::MUL, DL, Cond.getValueType(), Cond,
14849 DAG.getConstant(Diff, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000014850
Chris Lattnercee56e72009-03-13 05:53:31 +000014851 // Add the base if non-zero.
14852 if (FalseC->getAPIntValue() != 0)
14853 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
14854 SDValue(FalseC, 0));
14855 return Cond;
14856 }
Eric Christopherfd179292009-08-27 18:07:15 +000014857 }
Chris Lattnerd1980a52009-03-12 06:52:53 +000014858 }
14859 }
Eric Christopherfd179292009-08-27 18:07:15 +000014860
Evan Cheng56f582d2012-01-04 01:41:39 +000014861 // Canonicalize max and min:
14862 // (x > y) ? x : y -> (x >= y) ? x : y
14863 // (x < y) ? x : y -> (x <= y) ? x : y
14864 // This allows use of COND_S / COND_NS (see TranslateX86CC) which eliminates
14865 // the need for an extra compare
14866 // against zero. e.g.
14867 // (x - y) > 0 : (x - y) ? 0 -> (x - y) >= 0 : (x - y) ? 0
14868 // subl %esi, %edi
14869 // testl %edi, %edi
14870 // movl $0, %eax
14871 // cmovgl %edi, %eax
14872 // =>
14873 // xorl %eax, %eax
14874 // subl %esi, $edi
14875 // cmovsl %eax, %edi
14876 if (N->getOpcode() == ISD::SELECT && Cond.getOpcode() == ISD::SETCC &&
14877 DAG.isEqualTo(LHS, Cond.getOperand(0)) &&
14878 DAG.isEqualTo(RHS, Cond.getOperand(1))) {
14879 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
14880 switch (CC) {
14881 default: break;
14882 case ISD::SETLT:
14883 case ISD::SETGT: {
14884 ISD::CondCode NewCC = (CC == ISD::SETLT) ? ISD::SETLE : ISD::SETGE;
14885 Cond = DAG.getSetCC(Cond.getDebugLoc(), Cond.getValueType(),
14886 Cond.getOperand(0), Cond.getOperand(1), NewCC);
14887 return DAG.getNode(ISD::SELECT, DL, VT, Cond, LHS, RHS);
14888 }
14889 }
14890 }
14891
Nadav Rotemcc616562012-01-15 19:27:55 +000014892 // If we know that this node is legal then we know that it is going to be
14893 // matched by one of the SSE/AVX BLEND instructions. These instructions only
14894 // depend on the highest bit in each word. Try to use SimplifyDemandedBits
14895 // to simplify previous instructions.
14896 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
14897 if (N->getOpcode() == ISD::VSELECT && DCI.isBeforeLegalizeOps() &&
Nadav Rotembdcae382012-06-07 20:53:48 +000014898 !DCI.isBeforeLegalize() && TLI.isOperationLegal(ISD::VSELECT, VT)) {
Nadav Rotemcc616562012-01-15 19:27:55 +000014899 unsigned BitWidth = Cond.getValueType().getScalarType().getSizeInBits();
Nadav Rotembdcae382012-06-07 20:53:48 +000014900
14901 // Don't optimize vector selects that map to mask-registers.
14902 if (BitWidth == 1)
14903 return SDValue();
14904
Nadav Rotemcc616562012-01-15 19:27:55 +000014905 assert(BitWidth >= 8 && BitWidth <= 64 && "Invalid mask size");
14906 APInt DemandedMask = APInt::getHighBitsSet(BitWidth, 1);
14907
14908 APInt KnownZero, KnownOne;
14909 TargetLowering::TargetLoweringOpt TLO(DAG, DCI.isBeforeLegalize(),
14910 DCI.isBeforeLegalizeOps());
14911 if (TLO.ShrinkDemandedConstant(Cond, DemandedMask) ||
14912 TLI.SimplifyDemandedBits(Cond, DemandedMask, KnownZero, KnownOne, TLO))
14913 DCI.CommitTargetLoweringOpt(TLO);
14914 }
14915
Dan Gohman475871a2008-07-27 21:46:04 +000014916 return SDValue();
Chris Lattner83e6c992006-10-04 06:57:07 +000014917}
14918
Michael Liao2a33cec2012-08-10 19:58:13 +000014919// Check whether a boolean test is testing a boolean value generated by
14920// X86ISD::SETCC. If so, return the operand of that SETCC and proper condition
14921// code.
14922//
14923// Simplify the following patterns:
14924// (Op (CMP (SETCC Cond EFLAGS) 1) EQ) or
14925// (Op (CMP (SETCC Cond EFLAGS) 0) NEQ)
14926// to (Op EFLAGS Cond)
14927//
14928// (Op (CMP (SETCC Cond EFLAGS) 0) EQ) or
14929// (Op (CMP (SETCC Cond EFLAGS) 1) NEQ)
14930// to (Op EFLAGS !Cond)
14931//
14932// where Op could be BRCOND or CMOV.
14933//
Michael Liaodbf8b5b2012-08-28 03:34:40 +000014934static SDValue checkBoolTestSetCCCombine(SDValue Cmp, X86::CondCode &CC) {
Michael Liao2a33cec2012-08-10 19:58:13 +000014935 // Quit if not CMP and SUB with its value result used.
14936 if (Cmp.getOpcode() != X86ISD::CMP &&
14937 (Cmp.getOpcode() != X86ISD::SUB || Cmp.getNode()->hasAnyUseOfValue(0)))
14938 return SDValue();
14939
14940 // Quit if not used as a boolean value.
14941 if (CC != X86::COND_E && CC != X86::COND_NE)
14942 return SDValue();
14943
14944 // Check CMP operands. One of them should be 0 or 1 and the other should be
14945 // an SetCC or extended from it.
14946 SDValue Op1 = Cmp.getOperand(0);
14947 SDValue Op2 = Cmp.getOperand(1);
14948
14949 SDValue SetCC;
14950 const ConstantSDNode* C = 0;
14951 bool needOppositeCond = (CC == X86::COND_E);
14952
14953 if ((C = dyn_cast<ConstantSDNode>(Op1)))
14954 SetCC = Op2;
14955 else if ((C = dyn_cast<ConstantSDNode>(Op2)))
14956 SetCC = Op1;
14957 else // Quit if all operands are not constants.
14958 return SDValue();
14959
14960 if (C->getZExtValue() == 1)
14961 needOppositeCond = !needOppositeCond;
14962 else if (C->getZExtValue() != 0)
14963 // Quit if the constant is neither 0 or 1.
14964 return SDValue();
14965
14966 // Skip 'zext' node.
14967 if (SetCC.getOpcode() == ISD::ZERO_EXTEND)
14968 SetCC = SetCC.getOperand(0);
14969
Michael Liao7fdc66b2012-09-10 16:36:16 +000014970 switch (SetCC.getOpcode()) {
14971 case X86ISD::SETCC:
14972 // Set the condition code or opposite one if necessary.
14973 CC = X86::CondCode(SetCC.getConstantOperandVal(0));
14974 if (needOppositeCond)
14975 CC = X86::GetOppositeBranchCondition(CC);
14976 return SetCC.getOperand(1);
14977 case X86ISD::CMOV: {
14978 // Check whether false/true value has canonical one, i.e. 0 or 1.
14979 ConstantSDNode *FVal = dyn_cast<ConstantSDNode>(SetCC.getOperand(0));
14980 ConstantSDNode *TVal = dyn_cast<ConstantSDNode>(SetCC.getOperand(1));
14981 // Quit if true value is not a constant.
14982 if (!TVal)
14983 return SDValue();
14984 // Quit if false value is not a constant.
14985 if (!FVal) {
14986 // A special case for rdrand, where 0 is set if false cond is found.
14987 SDValue Op = SetCC.getOperand(0);
14988 if (Op.getOpcode() != X86ISD::RDRAND)
14989 return SDValue();
14990 }
14991 // Quit if false value is not the constant 0 or 1.
14992 bool FValIsFalse = true;
14993 if (FVal && FVal->getZExtValue() != 0) {
14994 if (FVal->getZExtValue() != 1)
14995 return SDValue();
14996 // If FVal is 1, opposite cond is needed.
14997 needOppositeCond = !needOppositeCond;
14998 FValIsFalse = false;
14999 }
15000 // Quit if TVal is not the constant opposite of FVal.
15001 if (FValIsFalse && TVal->getZExtValue() != 1)
15002 return SDValue();
15003 if (!FValIsFalse && TVal->getZExtValue() != 0)
15004 return SDValue();
15005 CC = X86::CondCode(SetCC.getConstantOperandVal(2));
15006 if (needOppositeCond)
15007 CC = X86::GetOppositeBranchCondition(CC);
15008 return SetCC.getOperand(3);
15009 }
15010 }
Michael Liao2a33cec2012-08-10 19:58:13 +000015011
Michael Liao7fdc66b2012-09-10 16:36:16 +000015012 return SDValue();
Michael Liao2a33cec2012-08-10 19:58:13 +000015013}
15014
Chris Lattnerd1980a52009-03-12 06:52:53 +000015015/// Optimize X86ISD::CMOV [LHS, RHS, CONDCODE (e.g. X86::COND_NE), CONDVAL]
15016static SDValue PerformCMOVCombine(SDNode *N, SelectionDAG &DAG,
Michael Liaodbf8b5b2012-08-28 03:34:40 +000015017 TargetLowering::DAGCombinerInfo &DCI,
15018 const X86Subtarget *Subtarget) {
Chris Lattnerd1980a52009-03-12 06:52:53 +000015019 DebugLoc DL = N->getDebugLoc();
Eric Christopherfd179292009-08-27 18:07:15 +000015020
Chris Lattnerd1980a52009-03-12 06:52:53 +000015021 // If the flag operand isn't dead, don't touch this CMOV.
15022 if (N->getNumValues() == 2 && !SDValue(N, 1).use_empty())
15023 return SDValue();
Eric Christopherfd179292009-08-27 18:07:15 +000015024
Evan Chengb5a55d92011-05-24 01:48:22 +000015025 SDValue FalseOp = N->getOperand(0);
15026 SDValue TrueOp = N->getOperand(1);
15027 X86::CondCode CC = (X86::CondCode)N->getConstantOperandVal(2);
15028 SDValue Cond = N->getOperand(3);
Michael Liao2a33cec2012-08-10 19:58:13 +000015029
Evan Chengb5a55d92011-05-24 01:48:22 +000015030 if (CC == X86::COND_E || CC == X86::COND_NE) {
15031 switch (Cond.getOpcode()) {
15032 default: break;
15033 case X86ISD::BSR:
15034 case X86ISD::BSF:
15035 // If operand of BSR / BSF are proven never zero, then ZF cannot be set.
15036 if (DAG.isKnownNeverZero(Cond.getOperand(0)))
15037 return (CC == X86::COND_E) ? FalseOp : TrueOp;
15038 }
15039 }
15040
Michael Liao2a33cec2012-08-10 19:58:13 +000015041 SDValue Flags;
15042
Michael Liaodbf8b5b2012-08-28 03:34:40 +000015043 Flags = checkBoolTestSetCCCombine(Cond, CC);
Michael Liao9eac20a2012-08-11 23:47:06 +000015044 if (Flags.getNode() &&
15045 // Extra check as FCMOV only supports a subset of X86 cond.
Michael Liao7859f432012-09-06 07:11:22 +000015046 (FalseOp.getValueType() != MVT::f80 || hasFPCMov(CC))) {
Michael Liaodbf8b5b2012-08-28 03:34:40 +000015047 SDValue Ops[] = { FalseOp, TrueOp,
15048 DAG.getConstant(CC, MVT::i8), Flags };
15049 return DAG.getNode(X86ISD::CMOV, DL, N->getVTList(),
15050 Ops, array_lengthof(Ops));
15051 }
15052
Chris Lattnerd1980a52009-03-12 06:52:53 +000015053 // If this is a select between two integer constants, try to do some
15054 // optimizations. Note that the operands are ordered the opposite of SELECT
15055 // operands.
Evan Chengb5a55d92011-05-24 01:48:22 +000015056 if (ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(TrueOp)) {
15057 if (ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(FalseOp)) {
Chris Lattnerd1980a52009-03-12 06:52:53 +000015058 // Canonicalize the TrueC/FalseC values so that TrueC (the true value) is
15059 // larger than FalseC (the false value).
Chris Lattnerd1980a52009-03-12 06:52:53 +000015060 if (TrueC->getAPIntValue().ult(FalseC->getAPIntValue())) {
15061 CC = X86::GetOppositeBranchCondition(CC);
15062 std::swap(TrueC, FalseC);
NAKAMURA Takumie2687452012-10-16 06:28:34 +000015063 std::swap(TrueOp, FalseOp);
Chris Lattnerd1980a52009-03-12 06:52:53 +000015064 }
Eric Christopherfd179292009-08-27 18:07:15 +000015065
Chris Lattnerd1980a52009-03-12 06:52:53 +000015066 // Optimize C ? 8 : 0 -> zext(setcc(C)) << 3. Likewise for any pow2/0.
Chris Lattnercee56e72009-03-13 05:53:31 +000015067 // This is efficient for any integer data type (including i8/i16) and
15068 // shift amount.
Chris Lattnerd1980a52009-03-12 06:52:53 +000015069 if (FalseC->getAPIntValue() == 0 && TrueC->getAPIntValue().isPowerOf2()) {
Owen Anderson825b72b2009-08-11 20:47:22 +000015070 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
15071 DAG.getConstant(CC, MVT::i8), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000015072
Chris Lattnerd1980a52009-03-12 06:52:53 +000015073 // Zero extend the condition if needed.
15074 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, TrueC->getValueType(0), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000015075
Chris Lattnerd1980a52009-03-12 06:52:53 +000015076 unsigned ShAmt = TrueC->getAPIntValue().logBase2();
15077 Cond = DAG.getNode(ISD::SHL, DL, Cond.getValueType(), Cond,
Owen Anderson825b72b2009-08-11 20:47:22 +000015078 DAG.getConstant(ShAmt, MVT::i8));
Chris Lattnerd1980a52009-03-12 06:52:53 +000015079 if (N->getNumValues() == 2) // Dead flag value?
15080 return DCI.CombineTo(N, Cond, SDValue());
15081 return Cond;
15082 }
Eric Christopherfd179292009-08-27 18:07:15 +000015083
Chris Lattnercee56e72009-03-13 05:53:31 +000015084 // Optimize Cond ? cst+1 : cst -> zext(setcc(C)+cst. This is efficient
15085 // for any integer data type, including i8/i16.
Chris Lattner97a29a52009-03-13 05:22:11 +000015086 if (FalseC->getAPIntValue()+1 == TrueC->getAPIntValue()) {
Owen Anderson825b72b2009-08-11 20:47:22 +000015087 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
15088 DAG.getConstant(CC, MVT::i8), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000015089
Chris Lattner97a29a52009-03-13 05:22:11 +000015090 // Zero extend the condition if needed.
Chris Lattnercee56e72009-03-13 05:53:31 +000015091 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL,
15092 FalseC->getValueType(0), Cond);
Chris Lattner97a29a52009-03-13 05:22:11 +000015093 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
15094 SDValue(FalseC, 0));
Eric Christopherfd179292009-08-27 18:07:15 +000015095
Chris Lattner97a29a52009-03-13 05:22:11 +000015096 if (N->getNumValues() == 2) // Dead flag value?
15097 return DCI.CombineTo(N, Cond, SDValue());
15098 return Cond;
15099 }
Eric Christopherfd179292009-08-27 18:07:15 +000015100
Chris Lattnercee56e72009-03-13 05:53:31 +000015101 // Optimize cases that will turn into an LEA instruction. This requires
15102 // an i32 or i64 and an efficient multiplier (1, 2, 3, 4, 5, 8, 9).
Owen Anderson825b72b2009-08-11 20:47:22 +000015103 if (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i64) {
Chris Lattnercee56e72009-03-13 05:53:31 +000015104 uint64_t Diff = TrueC->getZExtValue()-FalseC->getZExtValue();
Owen Anderson825b72b2009-08-11 20:47:22 +000015105 if (N->getValueType(0) == MVT::i32) Diff = (unsigned)Diff;
Eric Christopherfd179292009-08-27 18:07:15 +000015106
Chris Lattnercee56e72009-03-13 05:53:31 +000015107 bool isFastMultiplier = false;
15108 if (Diff < 10) {
15109 switch ((unsigned char)Diff) {
15110 default: break;
15111 case 1: // result = add base, cond
15112 case 2: // result = lea base( , cond*2)
15113 case 3: // result = lea base(cond, cond*2)
15114 case 4: // result = lea base( , cond*4)
15115 case 5: // result = lea base(cond, cond*4)
15116 case 8: // result = lea base( , cond*8)
15117 case 9: // result = lea base(cond, cond*8)
15118 isFastMultiplier = true;
15119 break;
15120 }
15121 }
Eric Christopherfd179292009-08-27 18:07:15 +000015122
Chris Lattnercee56e72009-03-13 05:53:31 +000015123 if (isFastMultiplier) {
15124 APInt Diff = TrueC->getAPIntValue()-FalseC->getAPIntValue();
Owen Anderson825b72b2009-08-11 20:47:22 +000015125 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
15126 DAG.getConstant(CC, MVT::i8), Cond);
Chris Lattnercee56e72009-03-13 05:53:31 +000015127 // Zero extend the condition if needed.
15128 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, FalseC->getValueType(0),
15129 Cond);
15130 // Scale the condition by the difference.
15131 if (Diff != 1)
15132 Cond = DAG.getNode(ISD::MUL, DL, Cond.getValueType(), Cond,
15133 DAG.getConstant(Diff, Cond.getValueType()));
15134
15135 // Add the base if non-zero.
15136 if (FalseC->getAPIntValue() != 0)
15137 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
15138 SDValue(FalseC, 0));
15139 if (N->getNumValues() == 2) // Dead flag value?
15140 return DCI.CombineTo(N, Cond, SDValue());
15141 return Cond;
15142 }
Eric Christopherfd179292009-08-27 18:07:15 +000015143 }
Chris Lattnerd1980a52009-03-12 06:52:53 +000015144 }
15145 }
NAKAMURA Takumie2687452012-10-16 06:28:34 +000015146
15147 // Handle these cases:
15148 // (select (x != c), e, c) -> select (x != c), e, x),
15149 // (select (x == c), c, e) -> select (x == c), x, e)
15150 // where the c is an integer constant, and the "select" is the combination
15151 // of CMOV and CMP.
15152 //
15153 // The rationale for this change is that the conditional-move from a constant
15154 // needs two instructions, however, conditional-move from a register needs
15155 // only one instruction.
15156 //
15157 // CAVEAT: By replacing a constant with a symbolic value, it may obscure
15158 // some instruction-combining opportunities. This opt needs to be
15159 // postponed as late as possible.
15160 //
15161 if (!DCI.isBeforeLegalize() && !DCI.isBeforeLegalizeOps()) {
15162 // the DCI.xxxx conditions are provided to postpone the optimization as
15163 // late as possible.
15164
15165 ConstantSDNode *CmpAgainst = 0;
15166 if ((Cond.getOpcode() == X86ISD::CMP || Cond.getOpcode() == X86ISD::SUB) &&
15167 (CmpAgainst = dyn_cast<ConstantSDNode>(Cond.getOperand(1))) &&
15168 dyn_cast<ConstantSDNode>(Cond.getOperand(0)) == 0) {
15169
15170 if (CC == X86::COND_NE &&
15171 CmpAgainst == dyn_cast<ConstantSDNode>(FalseOp)) {
15172 CC = X86::GetOppositeBranchCondition(CC);
15173 std::swap(TrueOp, FalseOp);
15174 }
15175
15176 if (CC == X86::COND_E &&
15177 CmpAgainst == dyn_cast<ConstantSDNode>(TrueOp)) {
15178 SDValue Ops[] = { FalseOp, Cond.getOperand(0),
15179 DAG.getConstant(CC, MVT::i8), Cond };
15180 return DAG.getNode(X86ISD::CMOV, DL, N->getVTList (), Ops,
15181 array_lengthof(Ops));
15182 }
15183 }
15184 }
15185
Chris Lattnerd1980a52009-03-12 06:52:53 +000015186 return SDValue();
15187}
15188
15189
Evan Cheng0b0cd912009-03-28 05:57:29 +000015190/// PerformMulCombine - Optimize a single multiply with constant into two
15191/// in order to implement it with two cheaper instructions, e.g.
15192/// LEA + SHL, LEA + LEA.
15193static SDValue PerformMulCombine(SDNode *N, SelectionDAG &DAG,
15194 TargetLowering::DAGCombinerInfo &DCI) {
Evan Cheng0b0cd912009-03-28 05:57:29 +000015195 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
15196 return SDValue();
15197
Owen Andersone50ed302009-08-10 22:56:29 +000015198 EVT VT = N->getValueType(0);
Owen Anderson825b72b2009-08-11 20:47:22 +000015199 if (VT != MVT::i64)
Evan Cheng0b0cd912009-03-28 05:57:29 +000015200 return SDValue();
15201
15202 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1));
15203 if (!C)
15204 return SDValue();
15205 uint64_t MulAmt = C->getZExtValue();
15206 if (isPowerOf2_64(MulAmt) || MulAmt == 3 || MulAmt == 5 || MulAmt == 9)
15207 return SDValue();
15208
15209 uint64_t MulAmt1 = 0;
15210 uint64_t MulAmt2 = 0;
15211 if ((MulAmt % 9) == 0) {
15212 MulAmt1 = 9;
15213 MulAmt2 = MulAmt / 9;
15214 } else if ((MulAmt % 5) == 0) {
15215 MulAmt1 = 5;
15216 MulAmt2 = MulAmt / 5;
15217 } else if ((MulAmt % 3) == 0) {
15218 MulAmt1 = 3;
15219 MulAmt2 = MulAmt / 3;
15220 }
15221 if (MulAmt2 &&
15222 (isPowerOf2_64(MulAmt2) || MulAmt2 == 3 || MulAmt2 == 5 || MulAmt2 == 9)){
15223 DebugLoc DL = N->getDebugLoc();
15224
15225 if (isPowerOf2_64(MulAmt2) &&
15226 !(N->hasOneUse() && N->use_begin()->getOpcode() == ISD::ADD))
15227 // If second multiplifer is pow2, issue it first. We want the multiply by
15228 // 3, 5, or 9 to be folded into the addressing mode unless the lone use
15229 // is an add.
15230 std::swap(MulAmt1, MulAmt2);
15231
15232 SDValue NewMul;
Eric Christopherfd179292009-08-27 18:07:15 +000015233 if (isPowerOf2_64(MulAmt1))
Evan Cheng0b0cd912009-03-28 05:57:29 +000015234 NewMul = DAG.getNode(ISD::SHL, DL, VT, N->getOperand(0),
Owen Anderson825b72b2009-08-11 20:47:22 +000015235 DAG.getConstant(Log2_64(MulAmt1), MVT::i8));
Evan Cheng0b0cd912009-03-28 05:57:29 +000015236 else
Evan Cheng73f24c92009-03-30 21:36:47 +000015237 NewMul = DAG.getNode(X86ISD::MUL_IMM, DL, VT, N->getOperand(0),
Evan Cheng0b0cd912009-03-28 05:57:29 +000015238 DAG.getConstant(MulAmt1, VT));
15239
Eric Christopherfd179292009-08-27 18:07:15 +000015240 if (isPowerOf2_64(MulAmt2))
Evan Cheng0b0cd912009-03-28 05:57:29 +000015241 NewMul = DAG.getNode(ISD::SHL, DL, VT, NewMul,
Owen Anderson825b72b2009-08-11 20:47:22 +000015242 DAG.getConstant(Log2_64(MulAmt2), MVT::i8));
Eric Christopherfd179292009-08-27 18:07:15 +000015243 else
Evan Cheng73f24c92009-03-30 21:36:47 +000015244 NewMul = DAG.getNode(X86ISD::MUL_IMM, DL, VT, NewMul,
Evan Cheng0b0cd912009-03-28 05:57:29 +000015245 DAG.getConstant(MulAmt2, VT));
15246
15247 // Do not add new nodes to DAG combiner worklist.
15248 DCI.CombineTo(N, NewMul, false);
15249 }
15250 return SDValue();
15251}
15252
Evan Chengad9c0a32009-12-15 00:53:42 +000015253static SDValue PerformSHLCombine(SDNode *N, SelectionDAG &DAG) {
15254 SDValue N0 = N->getOperand(0);
15255 SDValue N1 = N->getOperand(1);
15256 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
15257 EVT VT = N0.getValueType();
15258
15259 // fold (shl (and (setcc_c), c1), c2) -> (and setcc_c, (c1 << c2))
15260 // since the result of setcc_c is all zero's or all ones.
Nadav Rotemfb0dfbb2011-10-30 13:24:22 +000015261 if (VT.isInteger() && !VT.isVector() &&
15262 N1C && N0.getOpcode() == ISD::AND &&
Evan Chengad9c0a32009-12-15 00:53:42 +000015263 N0.getOperand(1).getOpcode() == ISD::Constant) {
15264 SDValue N00 = N0.getOperand(0);
15265 if (N00.getOpcode() == X86ISD::SETCC_CARRY ||
15266 ((N00.getOpcode() == ISD::ANY_EXTEND ||
15267 N00.getOpcode() == ISD::ZERO_EXTEND) &&
15268 N00.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY)) {
15269 APInt Mask = cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
15270 APInt ShAmt = N1C->getAPIntValue();
15271 Mask = Mask.shl(ShAmt);
15272 if (Mask != 0)
15273 return DAG.getNode(ISD::AND, N->getDebugLoc(), VT,
15274 N00, DAG.getConstant(Mask, VT));
15275 }
15276 }
15277
Nadav Rotemfb0dfbb2011-10-30 13:24:22 +000015278
15279 // Hardware support for vector shifts is sparse which makes us scalarize the
15280 // vector operations in many cases. Also, on sandybridge ADD is faster than
15281 // shl.
15282 // (shl V, 1) -> add V,V
15283 if (isSplatVector(N1.getNode())) {
15284 assert(N0.getValueType().isVector() && "Invalid vector shift type");
15285 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1->getOperand(0));
15286 // We shift all of the values by one. In many cases we do not have
15287 // hardware support for this operation. This is better expressed as an ADD
15288 // of two values.
15289 if (N1C && (1 == N1C->getZExtValue())) {
15290 return DAG.getNode(ISD::ADD, N->getDebugLoc(), VT, N0, N0);
15291 }
15292 }
15293
Evan Chengad9c0a32009-12-15 00:53:42 +000015294 return SDValue();
15295}
Evan Cheng0b0cd912009-03-28 05:57:29 +000015296
Nate Begeman740ab032009-01-26 00:52:55 +000015297/// PerformShiftCombine - Transforms vector shift nodes to use vector shifts
15298/// when possible.
15299static SDValue PerformShiftCombine(SDNode* N, SelectionDAG &DAG,
Mon P Wang845b1892012-02-01 22:15:20 +000015300 TargetLowering::DAGCombinerInfo &DCI,
Nate Begeman740ab032009-01-26 00:52:55 +000015301 const X86Subtarget *Subtarget) {
Evan Chengad9c0a32009-12-15 00:53:42 +000015302 EVT VT = N->getValueType(0);
Nadav Rotemfb0dfbb2011-10-30 13:24:22 +000015303 if (N->getOpcode() == ISD::SHL) {
15304 SDValue V = PerformSHLCombine(N, DAG);
15305 if (V.getNode()) return V;
15306 }
Evan Chengad9c0a32009-12-15 00:53:42 +000015307
Nate Begeman740ab032009-01-26 00:52:55 +000015308 // On X86 with SSE2 support, we can transform this to a vector shift if
15309 // all elements are shifted by the same amount. We can't do this in legalize
15310 // because the a constant vector is typically transformed to a constant pool
15311 // so we have no knowledge of the shift amount.
Craig Topper1accb7e2012-01-10 06:54:16 +000015312 if (!Subtarget->hasSSE2())
Nate Begemanc2fd67f2009-01-26 03:15:31 +000015313 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +000015314
Craig Topper7be5dfd2011-11-12 09:58:49 +000015315 if (VT != MVT::v2i64 && VT != MVT::v4i32 && VT != MVT::v8i16 &&
15316 (!Subtarget->hasAVX2() ||
15317 (VT != MVT::v4i64 && VT != MVT::v8i32 && VT != MVT::v16i16)))
Nate Begemanc2fd67f2009-01-26 03:15:31 +000015318 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +000015319
Mon P Wang3becd092009-01-28 08:12:05 +000015320 SDValue ShAmtOp = N->getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +000015321 EVT EltVT = VT.getVectorElementType();
Chris Lattner47b4ce82009-03-11 05:48:52 +000015322 DebugLoc DL = N->getDebugLoc();
Mon P Wangefa42202009-09-03 19:56:25 +000015323 SDValue BaseShAmt = SDValue();
Mon P Wang3becd092009-01-28 08:12:05 +000015324 if (ShAmtOp.getOpcode() == ISD::BUILD_VECTOR) {
15325 unsigned NumElts = VT.getVectorNumElements();
15326 unsigned i = 0;
15327 for (; i != NumElts; ++i) {
15328 SDValue Arg = ShAmtOp.getOperand(i);
15329 if (Arg.getOpcode() == ISD::UNDEF) continue;
15330 BaseShAmt = Arg;
15331 break;
15332 }
Craig Topper37c26772012-01-17 04:44:50 +000015333 // Handle the case where the build_vector is all undef
15334 // FIXME: Should DAG allow this?
15335 if (i == NumElts)
15336 return SDValue();
15337
Mon P Wang3becd092009-01-28 08:12:05 +000015338 for (; i != NumElts; ++i) {
15339 SDValue Arg = ShAmtOp.getOperand(i);
15340 if (Arg.getOpcode() == ISD::UNDEF) continue;
15341 if (Arg != BaseShAmt) {
15342 return SDValue();
15343 }
15344 }
15345 } else if (ShAmtOp.getOpcode() == ISD::VECTOR_SHUFFLE &&
Nate Begeman9008ca62009-04-27 18:41:29 +000015346 cast<ShuffleVectorSDNode>(ShAmtOp)->isSplat()) {
Mon P Wangefa42202009-09-03 19:56:25 +000015347 SDValue InVec = ShAmtOp.getOperand(0);
15348 if (InVec.getOpcode() == ISD::BUILD_VECTOR) {
15349 unsigned NumElts = InVec.getValueType().getVectorNumElements();
15350 unsigned i = 0;
15351 for (; i != NumElts; ++i) {
15352 SDValue Arg = InVec.getOperand(i);
15353 if (Arg.getOpcode() == ISD::UNDEF) continue;
15354 BaseShAmt = Arg;
15355 break;
15356 }
15357 } else if (InVec.getOpcode() == ISD::INSERT_VECTOR_ELT) {
15358 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(InVec.getOperand(2))) {
Evan Chengae3ecf92010-02-16 21:09:44 +000015359 unsigned SplatIdx= cast<ShuffleVectorSDNode>(ShAmtOp)->getSplatIndex();
Mon P Wangefa42202009-09-03 19:56:25 +000015360 if (C->getZExtValue() == SplatIdx)
15361 BaseShAmt = InVec.getOperand(1);
15362 }
15363 }
Mon P Wang845b1892012-02-01 22:15:20 +000015364 if (BaseShAmt.getNode() == 0) {
15365 // Don't create instructions with illegal types after legalize
15366 // types has run.
15367 if (!DAG.getTargetLoweringInfo().isTypeLegal(EltVT) &&
15368 !DCI.isBeforeLegalize())
15369 return SDValue();
15370
Mon P Wangefa42202009-09-03 19:56:25 +000015371 BaseShAmt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, EltVT, ShAmtOp,
15372 DAG.getIntPtrConstant(0));
Mon P Wang845b1892012-02-01 22:15:20 +000015373 }
Mon P Wang3becd092009-01-28 08:12:05 +000015374 } else
Nate Begemanc2fd67f2009-01-26 03:15:31 +000015375 return SDValue();
Nate Begeman740ab032009-01-26 00:52:55 +000015376
Mon P Wangefa42202009-09-03 19:56:25 +000015377 // The shift amount is an i32.
Owen Anderson825b72b2009-08-11 20:47:22 +000015378 if (EltVT.bitsGT(MVT::i32))
15379 BaseShAmt = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, BaseShAmt);
15380 else if (EltVT.bitsLT(MVT::i32))
Mon P Wangefa42202009-09-03 19:56:25 +000015381 BaseShAmt = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::i32, BaseShAmt);
Nate Begeman740ab032009-01-26 00:52:55 +000015382
Nate Begemanc2fd67f2009-01-26 03:15:31 +000015383 // The shift amount is identical so we can do a vector shift.
15384 SDValue ValOp = N->getOperand(0);
15385 switch (N->getOpcode()) {
15386 default:
Torok Edwinc23197a2009-07-14 16:55:14 +000015387 llvm_unreachable("Unknown shift opcode!");
Nate Begemanc2fd67f2009-01-26 03:15:31 +000015388 case ISD::SHL:
Craig Toppered2e13d2012-01-22 19:15:14 +000015389 switch (VT.getSimpleVT().SimpleTy) {
15390 default: return SDValue();
15391 case MVT::v2i64:
15392 case MVT::v4i32:
15393 case MVT::v8i16:
15394 case MVT::v4i64:
15395 case MVT::v8i32:
15396 case MVT::v16i16:
15397 return getTargetVShiftNode(X86ISD::VSHLI, DL, VT, ValOp, BaseShAmt, DAG);
15398 }
Nate Begemanc2fd67f2009-01-26 03:15:31 +000015399 case ISD::SRA:
Craig Toppered2e13d2012-01-22 19:15:14 +000015400 switch (VT.getSimpleVT().SimpleTy) {
15401 default: return SDValue();
15402 case MVT::v4i32:
15403 case MVT::v8i16:
15404 case MVT::v8i32:
15405 case MVT::v16i16:
15406 return getTargetVShiftNode(X86ISD::VSRAI, DL, VT, ValOp, BaseShAmt, DAG);
15407 }
Nate Begemanc2fd67f2009-01-26 03:15:31 +000015408 case ISD::SRL:
Craig Toppered2e13d2012-01-22 19:15:14 +000015409 switch (VT.getSimpleVT().SimpleTy) {
15410 default: return SDValue();
15411 case MVT::v2i64:
15412 case MVT::v4i32:
15413 case MVT::v8i16:
15414 case MVT::v4i64:
15415 case MVT::v8i32:
15416 case MVT::v16i16:
15417 return getTargetVShiftNode(X86ISD::VSRLI, DL, VT, ValOp, BaseShAmt, DAG);
15418 }
Nate Begeman740ab032009-01-26 00:52:55 +000015419 }
Nate Begeman740ab032009-01-26 00:52:55 +000015420}
15421
Nate Begemanb65c1752010-12-17 22:55:37 +000015422
Stuart Hastings865f0932011-06-03 23:53:54 +000015423// CMPEQCombine - Recognize the distinctive (AND (setcc ...) (setcc ..))
15424// where both setccs reference the same FP CMP, and rewrite for CMPEQSS
15425// and friends. Likewise for OR -> CMPNEQSS.
15426static SDValue CMPEQCombine(SDNode *N, SelectionDAG &DAG,
15427 TargetLowering::DAGCombinerInfo &DCI,
15428 const X86Subtarget *Subtarget) {
15429 unsigned opcode;
15430
15431 // SSE1 supports CMP{eq|ne}SS, and SSE2 added CMP{eq|ne}SD, but
15432 // we're requiring SSE2 for both.
Craig Topper1accb7e2012-01-10 06:54:16 +000015433 if (Subtarget->hasSSE2() && isAndOrOfSetCCs(SDValue(N, 0U), opcode)) {
Stuart Hastings865f0932011-06-03 23:53:54 +000015434 SDValue N0 = N->getOperand(0);
15435 SDValue N1 = N->getOperand(1);
15436 SDValue CMP0 = N0->getOperand(1);
15437 SDValue CMP1 = N1->getOperand(1);
15438 DebugLoc DL = N->getDebugLoc();
15439
15440 // The SETCCs should both refer to the same CMP.
15441 if (CMP0.getOpcode() != X86ISD::CMP || CMP0 != CMP1)
15442 return SDValue();
15443
15444 SDValue CMP00 = CMP0->getOperand(0);
15445 SDValue CMP01 = CMP0->getOperand(1);
15446 EVT VT = CMP00.getValueType();
15447
15448 if (VT == MVT::f32 || VT == MVT::f64) {
15449 bool ExpectingFlags = false;
15450 // Check for any users that want flags:
15451 for (SDNode::use_iterator UI = N->use_begin(),
15452 UE = N->use_end();
15453 !ExpectingFlags && UI != UE; ++UI)
15454 switch (UI->getOpcode()) {
15455 default:
15456 case ISD::BR_CC:
15457 case ISD::BRCOND:
15458 case ISD::SELECT:
15459 ExpectingFlags = true;
15460 break;
15461 case ISD::CopyToReg:
15462 case ISD::SIGN_EXTEND:
15463 case ISD::ZERO_EXTEND:
15464 case ISD::ANY_EXTEND:
15465 break;
15466 }
15467
15468 if (!ExpectingFlags) {
15469 enum X86::CondCode cc0 = (enum X86::CondCode)N0.getConstantOperandVal(0);
15470 enum X86::CondCode cc1 = (enum X86::CondCode)N1.getConstantOperandVal(0);
15471
15472 if (cc1 == X86::COND_E || cc1 == X86::COND_NE) {
15473 X86::CondCode tmp = cc0;
15474 cc0 = cc1;
15475 cc1 = tmp;
15476 }
15477
15478 if ((cc0 == X86::COND_E && cc1 == X86::COND_NP) ||
15479 (cc0 == X86::COND_NE && cc1 == X86::COND_P)) {
15480 bool is64BitFP = (CMP00.getValueType() == MVT::f64);
15481 X86ISD::NodeType NTOperator = is64BitFP ?
15482 X86ISD::FSETCCsd : X86ISD::FSETCCss;
15483 // FIXME: need symbolic constants for these magic numbers.
15484 // See X86ATTInstPrinter.cpp:printSSECC().
15485 unsigned x86cc = (cc0 == X86::COND_E) ? 0 : 4;
15486 SDValue OnesOrZeroesF = DAG.getNode(NTOperator, DL, MVT::f32, CMP00, CMP01,
15487 DAG.getConstant(x86cc, MVT::i8));
15488 SDValue OnesOrZeroesI = DAG.getNode(ISD::BITCAST, DL, MVT::i32,
15489 OnesOrZeroesF);
15490 SDValue ANDed = DAG.getNode(ISD::AND, DL, MVT::i32, OnesOrZeroesI,
15491 DAG.getConstant(1, MVT::i32));
15492 SDValue OneBitOfTruth = DAG.getNode(ISD::TRUNCATE, DL, MVT::i8, ANDed);
15493 return OneBitOfTruth;
15494 }
15495 }
15496 }
15497 }
15498 return SDValue();
15499}
15500
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +000015501/// CanFoldXORWithAllOnes - Test whether the XOR operand is a AllOnes vector
15502/// so it can be folded inside ANDNP.
15503static bool CanFoldXORWithAllOnes(const SDNode *N) {
15504 EVT VT = N->getValueType(0);
15505
15506 // Match direct AllOnes for 128 and 256-bit vectors
15507 if (ISD::isBuildVectorAllOnes(N))
15508 return true;
15509
15510 // Look through a bit convert.
15511 if (N->getOpcode() == ISD::BITCAST)
15512 N = N->getOperand(0).getNode();
15513
15514 // Sometimes the operand may come from a insert_subvector building a 256-bit
15515 // allones vector
Craig Topper7a9a28b2012-08-12 02:23:29 +000015516 if (VT.is256BitVector() &&
Bill Wendling456a9252011-08-04 00:32:58 +000015517 N->getOpcode() == ISD::INSERT_SUBVECTOR) {
15518 SDValue V1 = N->getOperand(0);
15519 SDValue V2 = N->getOperand(1);
15520
15521 if (V1.getOpcode() == ISD::INSERT_SUBVECTOR &&
15522 V1.getOperand(0).getOpcode() == ISD::UNDEF &&
15523 ISD::isBuildVectorAllOnes(V1.getOperand(1).getNode()) &&
15524 ISD::isBuildVectorAllOnes(V2.getNode()))
15525 return true;
15526 }
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +000015527
15528 return false;
15529}
15530
Nate Begemanb65c1752010-12-17 22:55:37 +000015531static SDValue PerformAndCombine(SDNode *N, SelectionDAG &DAG,
15532 TargetLowering::DAGCombinerInfo &DCI,
15533 const X86Subtarget *Subtarget) {
15534 if (DCI.isBeforeLegalizeOps())
15535 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000015536
Stuart Hastings865f0932011-06-03 23:53:54 +000015537 SDValue R = CMPEQCombine(N, DAG, DCI, Subtarget);
15538 if (R.getNode())
15539 return R;
15540
Craig Topper54a11172011-10-14 07:06:56 +000015541 EVT VT = N->getValueType(0);
15542
Craig Topperb4c94572011-10-21 06:55:01 +000015543 // Create ANDN, BLSI, and BLSR instructions
15544 // BLSI is X & (-X)
15545 // BLSR is X & (X-1)
Craig Topper54a11172011-10-14 07:06:56 +000015546 if (Subtarget->hasBMI() && (VT == MVT::i32 || VT == MVT::i64)) {
15547 SDValue N0 = N->getOperand(0);
15548 SDValue N1 = N->getOperand(1);
15549 DebugLoc DL = N->getDebugLoc();
15550
15551 // Check LHS for not
15552 if (N0.getOpcode() == ISD::XOR && isAllOnes(N0.getOperand(1)))
15553 return DAG.getNode(X86ISD::ANDN, DL, VT, N0.getOperand(0), N1);
15554 // Check RHS for not
15555 if (N1.getOpcode() == ISD::XOR && isAllOnes(N1.getOperand(1)))
15556 return DAG.getNode(X86ISD::ANDN, DL, VT, N1.getOperand(0), N0);
15557
Craig Topperb4c94572011-10-21 06:55:01 +000015558 // Check LHS for neg
15559 if (N0.getOpcode() == ISD::SUB && N0.getOperand(1) == N1 &&
15560 isZero(N0.getOperand(0)))
15561 return DAG.getNode(X86ISD::BLSI, DL, VT, N1);
15562
15563 // Check RHS for neg
15564 if (N1.getOpcode() == ISD::SUB && N1.getOperand(1) == N0 &&
15565 isZero(N1.getOperand(0)))
15566 return DAG.getNode(X86ISD::BLSI, DL, VT, N0);
15567
15568 // Check LHS for X-1
15569 if (N0.getOpcode() == ISD::ADD && N0.getOperand(0) == N1 &&
15570 isAllOnes(N0.getOperand(1)))
15571 return DAG.getNode(X86ISD::BLSR, DL, VT, N1);
15572
15573 // Check RHS for X-1
15574 if (N1.getOpcode() == ISD::ADD && N1.getOperand(0) == N0 &&
15575 isAllOnes(N1.getOperand(1)))
15576 return DAG.getNode(X86ISD::BLSR, DL, VT, N0);
15577
Craig Topper54a11172011-10-14 07:06:56 +000015578 return SDValue();
15579 }
15580
Bruno Cardoso Lopes466b0222011-07-13 21:36:51 +000015581 // Want to form ANDNP nodes:
15582 // 1) In the hopes of then easily combining them with OR and AND nodes
15583 // to form PBLEND/PSIGN.
15584 // 2) To match ANDN packed intrinsics
Bruno Cardoso Lopes466b0222011-07-13 21:36:51 +000015585 if (VT != MVT::v2i64 && VT != MVT::v4i64)
Nate Begemanb65c1752010-12-17 22:55:37 +000015586 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000015587
Nate Begemanb65c1752010-12-17 22:55:37 +000015588 SDValue N0 = N->getOperand(0);
15589 SDValue N1 = N->getOperand(1);
15590 DebugLoc DL = N->getDebugLoc();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000015591
Nate Begemanb65c1752010-12-17 22:55:37 +000015592 // Check LHS for vnot
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000015593 if (N0.getOpcode() == ISD::XOR &&
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +000015594 //ISD::isBuildVectorAllOnes(N0.getOperand(1).getNode()))
15595 CanFoldXORWithAllOnes(N0.getOperand(1).getNode()))
Bruno Cardoso Lopesc1af4772011-07-13 21:36:47 +000015596 return DAG.getNode(X86ISD::ANDNP, DL, VT, N0.getOperand(0), N1);
Nate Begemanb65c1752010-12-17 22:55:37 +000015597
15598 // Check RHS for vnot
15599 if (N1.getOpcode() == ISD::XOR &&
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +000015600 //ISD::isBuildVectorAllOnes(N1.getOperand(1).getNode()))
15601 CanFoldXORWithAllOnes(N1.getOperand(1).getNode()))
Bruno Cardoso Lopesc1af4772011-07-13 21:36:47 +000015602 return DAG.getNode(X86ISD::ANDNP, DL, VT, N1.getOperand(0), N0);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000015603
Nate Begemanb65c1752010-12-17 22:55:37 +000015604 return SDValue();
15605}
15606
Evan Cheng760d1942010-01-04 21:22:48 +000015607static SDValue PerformOrCombine(SDNode *N, SelectionDAG &DAG,
Evan Cheng8b1190a2010-04-28 01:18:01 +000015608 TargetLowering::DAGCombinerInfo &DCI,
Evan Cheng760d1942010-01-04 21:22:48 +000015609 const X86Subtarget *Subtarget) {
Evan Cheng39cfeec2010-04-28 02:25:18 +000015610 if (DCI.isBeforeLegalizeOps())
Evan Cheng8b1190a2010-04-28 01:18:01 +000015611 return SDValue();
15612
Stuart Hastings865f0932011-06-03 23:53:54 +000015613 SDValue R = CMPEQCombine(N, DAG, DCI, Subtarget);
15614 if (R.getNode())
15615 return R;
15616
Evan Cheng760d1942010-01-04 21:22:48 +000015617 EVT VT = N->getValueType(0);
Evan Cheng760d1942010-01-04 21:22:48 +000015618
Evan Cheng760d1942010-01-04 21:22:48 +000015619 SDValue N0 = N->getOperand(0);
15620 SDValue N1 = N->getOperand(1);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000015621
Nate Begemanb65c1752010-12-17 22:55:37 +000015622 // look for psign/blend
Craig Topper1666cb62011-11-19 07:07:26 +000015623 if (VT == MVT::v2i64 || VT == MVT::v4i64) {
Craig Topperd0a31172012-01-10 06:37:29 +000015624 if (!Subtarget->hasSSSE3() ||
Craig Topper1666cb62011-11-19 07:07:26 +000015625 (VT == MVT::v4i64 && !Subtarget->hasAVX2()))
15626 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000015627
Craig Topper1666cb62011-11-19 07:07:26 +000015628 // Canonicalize pandn to RHS
15629 if (N0.getOpcode() == X86ISD::ANDNP)
15630 std::swap(N0, N1);
Lang Hames9ffaa6a2012-01-10 22:53:20 +000015631 // or (and (m, y), (pandn m, x))
Craig Topper1666cb62011-11-19 07:07:26 +000015632 if (N0.getOpcode() == ISD::AND && N1.getOpcode() == X86ISD::ANDNP) {
15633 SDValue Mask = N1.getOperand(0);
15634 SDValue X = N1.getOperand(1);
15635 SDValue Y;
15636 if (N0.getOperand(0) == Mask)
15637 Y = N0.getOperand(1);
15638 if (N0.getOperand(1) == Mask)
15639 Y = N0.getOperand(0);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000015640
Craig Topper1666cb62011-11-19 07:07:26 +000015641 // Check to see if the mask appeared in both the AND and ANDNP and
15642 if (!Y.getNode())
15643 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000015644
Craig Topper1666cb62011-11-19 07:07:26 +000015645 // Validate that X, Y, and Mask are BIT_CONVERTS, and see through them.
Craig Topper1666cb62011-11-19 07:07:26 +000015646 // Look through mask bitcast.
Nadav Rotem4ac90812012-04-01 19:31:22 +000015647 if (Mask.getOpcode() == ISD::BITCAST)
15648 Mask = Mask.getOperand(0);
15649 if (X.getOpcode() == ISD::BITCAST)
15650 X = X.getOperand(0);
15651 if (Y.getOpcode() == ISD::BITCAST)
15652 Y = Y.getOperand(0);
15653
Craig Topper1666cb62011-11-19 07:07:26 +000015654 EVT MaskVT = Mask.getValueType();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000015655
Craig Toppered2e13d2012-01-22 19:15:14 +000015656 // Validate that the Mask operand is a vector sra node.
Craig Topper1666cb62011-11-19 07:07:26 +000015657 // FIXME: what to do for bytes, since there is a psignb/pblendvb, but
15658 // there is no psrai.b
Craig Topper7fb8b0c2012-01-23 06:46:22 +000015659 if (Mask.getOpcode() != X86ISD::VSRAI)
Craig Toppered2e13d2012-01-22 19:15:14 +000015660 return SDValue();
Craig Topper1666cb62011-11-19 07:07:26 +000015661
15662 // Check that the SRA is all signbits.
Craig Topper7fb8b0c2012-01-23 06:46:22 +000015663 SDValue SraC = Mask.getOperand(1);
Craig Topper1666cb62011-11-19 07:07:26 +000015664 unsigned SraAmt = cast<ConstantSDNode>(SraC)->getZExtValue();
15665 unsigned EltBits = MaskVT.getVectorElementType().getSizeInBits();
15666 if ((SraAmt + 1) != EltBits)
15667 return SDValue();
15668
15669 DebugLoc DL = N->getDebugLoc();
15670
15671 // Now we know we at least have a plendvb with the mask val. See if
15672 // we can form a psignb/w/d.
15673 // psign = x.type == y.type == mask.type && y = sub(0, x);
Craig Topper1666cb62011-11-19 07:07:26 +000015674 if (Y.getOpcode() == ISD::SUB && Y.getOperand(1) == X &&
15675 ISD::isBuildVectorAllZeros(Y.getOperand(0).getNode()) &&
Craig Toppered2e13d2012-01-22 19:15:14 +000015676 X.getValueType() == MaskVT && Y.getValueType() == MaskVT) {
15677 assert((EltBits == 8 || EltBits == 16 || EltBits == 32) &&
15678 "Unsupported VT for PSIGN");
Craig Topper7fb8b0c2012-01-23 06:46:22 +000015679 Mask = DAG.getNode(X86ISD::PSIGN, DL, MaskVT, X, Mask.getOperand(0));
Craig Toppered2e13d2012-01-22 19:15:14 +000015680 return DAG.getNode(ISD::BITCAST, DL, VT, Mask);
Craig Topper1666cb62011-11-19 07:07:26 +000015681 }
15682 // PBLENDVB only available on SSE 4.1
Craig Topperd0a31172012-01-10 06:37:29 +000015683 if (!Subtarget->hasSSE41())
Craig Topper1666cb62011-11-19 07:07:26 +000015684 return SDValue();
15685
15686 EVT BlendVT = (VT == MVT::v4i64) ? MVT::v32i8 : MVT::v16i8;
15687
15688 X = DAG.getNode(ISD::BITCAST, DL, BlendVT, X);
15689 Y = DAG.getNode(ISD::BITCAST, DL, BlendVT, Y);
15690 Mask = DAG.getNode(ISD::BITCAST, DL, BlendVT, Mask);
Nadav Rotem18197d72011-11-30 10:13:37 +000015691 Mask = DAG.getNode(ISD::VSELECT, DL, BlendVT, Mask, Y, X);
Craig Topper1666cb62011-11-19 07:07:26 +000015692 return DAG.getNode(ISD::BITCAST, DL, VT, Mask);
Nate Begemanb65c1752010-12-17 22:55:37 +000015693 }
15694 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000015695
Craig Topper1666cb62011-11-19 07:07:26 +000015696 if (VT != MVT::i16 && VT != MVT::i32 && VT != MVT::i64)
15697 return SDValue();
15698
Nate Begemanb65c1752010-12-17 22:55:37 +000015699 // fold (or (x << c) | (y >> (64 - c))) ==> (shld64 x, y, c)
Evan Cheng760d1942010-01-04 21:22:48 +000015700 if (N0.getOpcode() == ISD::SRL && N1.getOpcode() == ISD::SHL)
15701 std::swap(N0, N1);
15702 if (N0.getOpcode() != ISD::SHL || N1.getOpcode() != ISD::SRL)
15703 return SDValue();
Evan Cheng8b1190a2010-04-28 01:18:01 +000015704 if (!N0.hasOneUse() || !N1.hasOneUse())
15705 return SDValue();
Evan Cheng760d1942010-01-04 21:22:48 +000015706
15707 SDValue ShAmt0 = N0.getOperand(1);
15708 if (ShAmt0.getValueType() != MVT::i8)
15709 return SDValue();
15710 SDValue ShAmt1 = N1.getOperand(1);
15711 if (ShAmt1.getValueType() != MVT::i8)
15712 return SDValue();
15713 if (ShAmt0.getOpcode() == ISD::TRUNCATE)
15714 ShAmt0 = ShAmt0.getOperand(0);
15715 if (ShAmt1.getOpcode() == ISD::TRUNCATE)
15716 ShAmt1 = ShAmt1.getOperand(0);
15717
15718 DebugLoc DL = N->getDebugLoc();
15719 unsigned Opc = X86ISD::SHLD;
15720 SDValue Op0 = N0.getOperand(0);
15721 SDValue Op1 = N1.getOperand(0);
15722 if (ShAmt0.getOpcode() == ISD::SUB) {
15723 Opc = X86ISD::SHRD;
15724 std::swap(Op0, Op1);
15725 std::swap(ShAmt0, ShAmt1);
15726 }
15727
Evan Cheng8b1190a2010-04-28 01:18:01 +000015728 unsigned Bits = VT.getSizeInBits();
Evan Cheng760d1942010-01-04 21:22:48 +000015729 if (ShAmt1.getOpcode() == ISD::SUB) {
15730 SDValue Sum = ShAmt1.getOperand(0);
15731 if (ConstantSDNode *SumC = dyn_cast<ConstantSDNode>(Sum)) {
Dan Gohman4e39e9d2010-06-24 14:30:44 +000015732 SDValue ShAmt1Op1 = ShAmt1.getOperand(1);
15733 if (ShAmt1Op1.getNode()->getOpcode() == ISD::TRUNCATE)
15734 ShAmt1Op1 = ShAmt1Op1.getOperand(0);
15735 if (SumC->getSExtValue() == Bits && ShAmt1Op1 == ShAmt0)
Evan Cheng760d1942010-01-04 21:22:48 +000015736 return DAG.getNode(Opc, DL, VT,
15737 Op0, Op1,
15738 DAG.getNode(ISD::TRUNCATE, DL,
15739 MVT::i8, ShAmt0));
15740 }
15741 } else if (ConstantSDNode *ShAmt1C = dyn_cast<ConstantSDNode>(ShAmt1)) {
15742 ConstantSDNode *ShAmt0C = dyn_cast<ConstantSDNode>(ShAmt0);
15743 if (ShAmt0C &&
Evan Cheng8b1190a2010-04-28 01:18:01 +000015744 ShAmt0C->getSExtValue() + ShAmt1C->getSExtValue() == Bits)
Evan Cheng760d1942010-01-04 21:22:48 +000015745 return DAG.getNode(Opc, DL, VT,
15746 N0.getOperand(0), N1.getOperand(0),
15747 DAG.getNode(ISD::TRUNCATE, DL,
15748 MVT::i8, ShAmt0));
15749 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000015750
Evan Cheng760d1942010-01-04 21:22:48 +000015751 return SDValue();
15752}
15753
Manman Ren92363622012-06-07 22:39:10 +000015754// Generate NEG and CMOV for integer abs.
15755static SDValue performIntegerAbsCombine(SDNode *N, SelectionDAG &DAG) {
15756 EVT VT = N->getValueType(0);
15757
15758 // Since X86 does not have CMOV for 8-bit integer, we don't convert
15759 // 8-bit integer abs to NEG and CMOV.
15760 if (VT.isInteger() && VT.getSizeInBits() == 8)
15761 return SDValue();
15762
15763 SDValue N0 = N->getOperand(0);
15764 SDValue N1 = N->getOperand(1);
15765 DebugLoc DL = N->getDebugLoc();
15766
15767 // Check pattern of XOR(ADD(X,Y), Y) where Y is SRA(X, size(X)-1)
15768 // and change it to SUB and CMOV.
15769 if (VT.isInteger() && N->getOpcode() == ISD::XOR &&
15770 N0.getOpcode() == ISD::ADD &&
15771 N0.getOperand(1) == N1 &&
15772 N1.getOpcode() == ISD::SRA &&
15773 N1.getOperand(0) == N0.getOperand(0))
15774 if (ConstantSDNode *Y1C = dyn_cast<ConstantSDNode>(N1.getOperand(1)))
15775 if (Y1C->getAPIntValue() == VT.getSizeInBits()-1) {
15776 // Generate SUB & CMOV.
15777 SDValue Neg = DAG.getNode(X86ISD::SUB, DL, DAG.getVTList(VT, MVT::i32),
15778 DAG.getConstant(0, VT), N0.getOperand(0));
15779
15780 SDValue Ops[] = { N0.getOperand(0), Neg,
15781 DAG.getConstant(X86::COND_GE, MVT::i8),
15782 SDValue(Neg.getNode(), 1) };
15783 return DAG.getNode(X86ISD::CMOV, DL, DAG.getVTList(VT, MVT::Glue),
15784 Ops, array_lengthof(Ops));
15785 }
15786 return SDValue();
15787}
15788
Craig Topper3738ccd2011-12-27 06:27:23 +000015789// PerformXorCombine - Attempts to turn XOR nodes into BLSMSK nodes
Craig Topperb4c94572011-10-21 06:55:01 +000015790static SDValue PerformXorCombine(SDNode *N, SelectionDAG &DAG,
15791 TargetLowering::DAGCombinerInfo &DCI,
15792 const X86Subtarget *Subtarget) {
15793 if (DCI.isBeforeLegalizeOps())
15794 return SDValue();
15795
Manman Ren45d53b82012-06-08 18:58:26 +000015796 if (Subtarget->hasCMov()) {
15797 SDValue RV = performIntegerAbsCombine(N, DAG);
15798 if (RV.getNode())
15799 return RV;
15800 }
Manman Ren92363622012-06-07 22:39:10 +000015801
15802 // Try forming BMI if it is available.
15803 if (!Subtarget->hasBMI())
15804 return SDValue();
15805
Craig Topperb4c94572011-10-21 06:55:01 +000015806 EVT VT = N->getValueType(0);
15807
15808 if (VT != MVT::i32 && VT != MVT::i64)
15809 return SDValue();
15810
Craig Topper3738ccd2011-12-27 06:27:23 +000015811 assert(Subtarget->hasBMI() && "Creating BLSMSK requires BMI instructions");
15812
Craig Topperb4c94572011-10-21 06:55:01 +000015813 // Create BLSMSK instructions by finding X ^ (X-1)
15814 SDValue N0 = N->getOperand(0);
15815 SDValue N1 = N->getOperand(1);
15816 DebugLoc DL = N->getDebugLoc();
15817
15818 if (N0.getOpcode() == ISD::ADD && N0.getOperand(0) == N1 &&
15819 isAllOnes(N0.getOperand(1)))
15820 return DAG.getNode(X86ISD::BLSMSK, DL, VT, N1);
15821
15822 if (N1.getOpcode() == ISD::ADD && N1.getOperand(0) == N0 &&
15823 isAllOnes(N1.getOperand(1)))
15824 return DAG.getNode(X86ISD::BLSMSK, DL, VT, N0);
15825
15826 return SDValue();
15827}
15828
Nadav Rotem91e43fd2011-09-18 10:39:32 +000015829/// PerformLOADCombine - Do target-specific dag combines on LOAD nodes.
15830static SDValue PerformLOADCombine(SDNode *N, SelectionDAG &DAG,
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000015831 TargetLowering::DAGCombinerInfo &DCI,
15832 const X86Subtarget *Subtarget) {
Nadav Rotem91e43fd2011-09-18 10:39:32 +000015833 LoadSDNode *Ld = cast<LoadSDNode>(N);
15834 EVT RegVT = Ld->getValueType(0);
15835 EVT MemVT = Ld->getMemoryVT();
15836 DebugLoc dl = Ld->getDebugLoc();
15837 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
15838
15839 ISD::LoadExtType Ext = Ld->getExtensionType();
15840
Nadav Rotemca6f2962011-09-18 19:00:23 +000015841 // If this is a vector EXT Load then attempt to optimize it using a
Michael Liao35a56402012-10-17 03:59:18 +000015842 // shuffle. We need SSSE3 shuffles.
Nadav Rotem91e43fd2011-09-18 10:39:32 +000015843 // TODO: It is possible to support ZExt by zeroing the undef values
15844 // during the shuffle phase or after the shuffle.
Eli Friedmanda813f42011-12-28 21:24:44 +000015845 if (RegVT.isVector() && RegVT.isInteger() &&
Michael Liao35a56402012-10-17 03:59:18 +000015846 Ext == ISD::EXTLOAD && Subtarget->hasSSSE3()) {
Nadav Rotem91e43fd2011-09-18 10:39:32 +000015847 assert(MemVT != RegVT && "Cannot extend to the same type");
15848 assert(MemVT.isVector() && "Must load a vector from memory");
15849
15850 unsigned NumElems = RegVT.getVectorNumElements();
15851 unsigned RegSz = RegVT.getSizeInBits();
15852 unsigned MemSz = MemVT.getSizeInBits();
15853 assert(RegSz > MemSz && "Register size must be greater than the mem size");
Nadav Rotem91e43fd2011-09-18 10:39:32 +000015854
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000015855 // All sizes must be a power of two.
15856 if (!isPowerOf2_32(RegSz * MemSz * NumElems))
15857 return SDValue();
15858
15859 // Attempt to load the original value using scalar loads.
15860 // Find the largest scalar type that divides the total loaded size.
Nadav Rotem91e43fd2011-09-18 10:39:32 +000015861 MVT SclrLoadTy = MVT::i8;
15862 for (unsigned tp = MVT::FIRST_INTEGER_VALUETYPE;
15863 tp < MVT::LAST_INTEGER_VALUETYPE; ++tp) {
15864 MVT Tp = (MVT::SimpleValueType)tp;
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000015865 if (TLI.isTypeLegal(Tp) && ((MemSz % Tp.getSizeInBits()) == 0)) {
Nadav Rotem91e43fd2011-09-18 10:39:32 +000015866 SclrLoadTy = Tp;
Nadav Rotem91e43fd2011-09-18 10:39:32 +000015867 }
15868 }
15869
Nadav Rotem5cd95e12012-07-11 13:27:05 +000015870 // On 32bit systems, we can't save 64bit integers. Try bitcasting to F64.
15871 if (TLI.isTypeLegal(MVT::f64) && SclrLoadTy.getSizeInBits() < 64 &&
15872 (64 <= MemSz))
15873 SclrLoadTy = MVT::f64;
15874
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000015875 // Calculate the number of scalar loads that we need to perform
15876 // in order to load our vector from memory.
15877 unsigned NumLoads = MemSz / SclrLoadTy.getSizeInBits();
Nadav Rotem91e43fd2011-09-18 10:39:32 +000015878
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000015879 // Represent our vector as a sequence of elements which are the
15880 // largest scalar that we can load.
Nadav Rotem91e43fd2011-09-18 10:39:32 +000015881 EVT LoadUnitVecVT = EVT::getVectorVT(*DAG.getContext(), SclrLoadTy,
15882 RegSz/SclrLoadTy.getSizeInBits());
15883
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000015884 // Represent the data using the same element type that is stored in
15885 // memory. In practice, we ''widen'' MemVT.
Nadav Rotem91e43fd2011-09-18 10:39:32 +000015886 EVT WideVecVT = EVT::getVectorVT(*DAG.getContext(), MemVT.getScalarType(),
15887 RegSz/MemVT.getScalarType().getSizeInBits());
Nadav Rotem91e43fd2011-09-18 10:39:32 +000015888
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000015889 assert(WideVecVT.getSizeInBits() == LoadUnitVecVT.getSizeInBits() &&
15890 "Invalid vector type");
Nadav Rotem91e43fd2011-09-18 10:39:32 +000015891
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000015892 // We can't shuffle using an illegal type.
15893 if (!TLI.isTypeLegal(WideVecVT))
15894 return SDValue();
15895
15896 SmallVector<SDValue, 8> Chains;
15897 SDValue Ptr = Ld->getBasePtr();
15898 SDValue Increment = DAG.getConstant(SclrLoadTy.getSizeInBits()/8,
15899 TLI.getPointerTy());
15900 SDValue Res = DAG.getUNDEF(LoadUnitVecVT);
15901
15902 for (unsigned i = 0; i < NumLoads; ++i) {
15903 // Perform a single load.
15904 SDValue ScalarLoad = DAG.getLoad(SclrLoadTy, dl, Ld->getChain(),
15905 Ptr, Ld->getPointerInfo(),
15906 Ld->isVolatile(), Ld->isNonTemporal(),
15907 Ld->isInvariant(), Ld->getAlignment());
15908 Chains.push_back(ScalarLoad.getValue(1));
15909 // Create the first element type using SCALAR_TO_VECTOR in order to avoid
15910 // another round of DAGCombining.
15911 if (i == 0)
15912 Res = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, LoadUnitVecVT, ScalarLoad);
15913 else
15914 Res = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, LoadUnitVecVT, Res,
15915 ScalarLoad, DAG.getIntPtrConstant(i));
15916
15917 Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr, Increment);
15918 }
15919
15920 SDValue TF = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &Chains[0],
15921 Chains.size());
Nadav Rotem91e43fd2011-09-18 10:39:32 +000015922
15923 // Bitcast the loaded value to a vector of the original element type, in
15924 // the size of the target vector type.
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000015925 SDValue SlicedVec = DAG.getNode(ISD::BITCAST, dl, WideVecVT, Res);
Nadav Rotem91e43fd2011-09-18 10:39:32 +000015926 unsigned SizeRatio = RegSz/MemSz;
15927
15928 // Redistribute the loaded elements into the different locations.
15929 SmallVector<int, 8> ShuffleVec(NumElems * SizeRatio, -1);
Craig Topper31a207a2012-05-04 06:39:13 +000015930 for (unsigned i = 0; i != NumElems; ++i)
15931 ShuffleVec[i*SizeRatio] = i;
Nadav Rotem91e43fd2011-09-18 10:39:32 +000015932
15933 SDValue Shuff = DAG.getVectorShuffle(WideVecVT, dl, SlicedVec,
Craig Topperdf966f62012-04-22 19:17:57 +000015934 DAG.getUNDEF(WideVecVT),
15935 &ShuffleVec[0]);
Nadav Rotem91e43fd2011-09-18 10:39:32 +000015936
15937 // Bitcast to the requested type.
15938 Shuff = DAG.getNode(ISD::BITCAST, dl, RegVT, Shuff);
15939 // Replace the original load with the new sequence
15940 // and return the new chain.
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000015941 return DCI.CombineTo(N, Shuff, TF, true);
Nadav Rotem91e43fd2011-09-18 10:39:32 +000015942 }
15943
15944 return SDValue();
15945}
15946
Chris Lattner149a4e52008-02-22 02:09:43 +000015947/// PerformSTORECombine - Do target-specific dag combines on STORE nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000015948static SDValue PerformSTORECombine(SDNode *N, SelectionDAG &DAG,
Evan Cheng536e6672009-03-12 05:59:15 +000015949 const X86Subtarget *Subtarget) {
Nadav Rotem614061b2011-08-10 19:30:14 +000015950 StoreSDNode *St = cast<StoreSDNode>(N);
15951 EVT VT = St->getValue().getValueType();
15952 EVT StVT = St->getMemoryVT();
15953 DebugLoc dl = St->getDebugLoc();
Nadav Rotem5e742a32011-08-11 16:41:21 +000015954 SDValue StoredVal = St->getOperand(1);
15955 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
15956
Nick Lewycky8a8d4792011-12-02 22:16:29 +000015957 // If we are saving a concatenation of two XMM registers, perform two stores.
Nadav Rotem87d35e82012-05-19 20:30:08 +000015958 // On Sandy Bridge, 256-bit memory operations are executed by two
15959 // 128-bit ports. However, on Haswell it is better to issue a single 256-bit
15960 // memory operation.
Craig Topper7a9a28b2012-08-12 02:23:29 +000015961 if (VT.is256BitVector() && !Subtarget->hasAVX2() &&
Craig Topperb4a8aef2012-04-27 21:05:09 +000015962 StoredVal.getNode()->getOpcode() == ISD::CONCAT_VECTORS &&
15963 StoredVal.getNumOperands() == 2) {
Nadav Rotem5e742a32011-08-11 16:41:21 +000015964 SDValue Value0 = StoredVal.getOperand(0);
15965 SDValue Value1 = StoredVal.getOperand(1);
15966
15967 SDValue Stride = DAG.getConstant(16, TLI.getPointerTy());
15968 SDValue Ptr0 = St->getBasePtr();
15969 SDValue Ptr1 = DAG.getNode(ISD::ADD, dl, Ptr0.getValueType(), Ptr0, Stride);
15970
15971 SDValue Ch0 = DAG.getStore(St->getChain(), dl, Value0, Ptr0,
15972 St->getPointerInfo(), St->isVolatile(),
15973 St->isNonTemporal(), St->getAlignment());
15974 SDValue Ch1 = DAG.getStore(St->getChain(), dl, Value1, Ptr1,
15975 St->getPointerInfo(), St->isVolatile(),
15976 St->isNonTemporal(), St->getAlignment());
15977 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Ch0, Ch1);
15978 }
Nadav Rotem614061b2011-08-10 19:30:14 +000015979
15980 // Optimize trunc store (of multiple scalars) to shuffle and store.
15981 // First, pack all of the elements in one place. Next, store to memory
15982 // in fewer chunks.
15983 if (St->isTruncatingStore() && VT.isVector()) {
15984 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
15985 unsigned NumElems = VT.getVectorNumElements();
15986 assert(StVT != VT && "Cannot truncate to the same type");
15987 unsigned FromSz = VT.getVectorElementType().getSizeInBits();
15988 unsigned ToSz = StVT.getVectorElementType().getSizeInBits();
15989
15990 // From, To sizes and ElemCount must be pow of two
15991 if (!isPowerOf2_32(NumElems * FromSz * ToSz)) return SDValue();
Nadav Rotem9c6cdf42011-09-21 08:45:10 +000015992 // We are going to use the original vector elt for storing.
Nadav Rotem64ac73b2011-09-21 17:14:40 +000015993 // Accumulated smaller vector elements must be a multiple of the store size.
Nadav Rotem9c6cdf42011-09-21 08:45:10 +000015994 if (0 != (NumElems * FromSz) % ToSz) return SDValue();
Nadav Rotem91e43fd2011-09-18 10:39:32 +000015995
Nadav Rotem614061b2011-08-10 19:30:14 +000015996 unsigned SizeRatio = FromSz / ToSz;
15997
15998 assert(SizeRatio * NumElems * ToSz == VT.getSizeInBits());
15999
16000 // Create a type on which we perform the shuffle
16001 EVT WideVecVT = EVT::getVectorVT(*DAG.getContext(),
16002 StVT.getScalarType(), NumElems*SizeRatio);
16003
16004 assert(WideVecVT.getSizeInBits() == VT.getSizeInBits());
16005
16006 SDValue WideVec = DAG.getNode(ISD::BITCAST, dl, WideVecVT, St->getValue());
16007 SmallVector<int, 8> ShuffleVec(NumElems * SizeRatio, -1);
Craig Topper31a207a2012-05-04 06:39:13 +000016008 for (unsigned i = 0; i != NumElems; ++i)
16009 ShuffleVec[i] = i * SizeRatio;
Nadav Rotem614061b2011-08-10 19:30:14 +000016010
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000016011 // Can't shuffle using an illegal type.
16012 if (!TLI.isTypeLegal(WideVecVT))
16013 return SDValue();
Nadav Rotem614061b2011-08-10 19:30:14 +000016014
16015 SDValue Shuff = DAG.getVectorShuffle(WideVecVT, dl, WideVec,
Craig Topperdf966f62012-04-22 19:17:57 +000016016 DAG.getUNDEF(WideVecVT),
16017 &ShuffleVec[0]);
Nadav Rotem614061b2011-08-10 19:30:14 +000016018 // At this point all of the data is stored at the bottom of the
16019 // register. We now need to save it to mem.
16020
16021 // Find the largest store unit
16022 MVT StoreType = MVT::i8;
16023 for (unsigned tp = MVT::FIRST_INTEGER_VALUETYPE;
16024 tp < MVT::LAST_INTEGER_VALUETYPE; ++tp) {
16025 MVT Tp = (MVT::SimpleValueType)tp;
Nadav Rotem5cd95e12012-07-11 13:27:05 +000016026 if (TLI.isTypeLegal(Tp) && Tp.getSizeInBits() <= NumElems * ToSz)
Nadav Rotem614061b2011-08-10 19:30:14 +000016027 StoreType = Tp;
16028 }
16029
Nadav Rotem5cd95e12012-07-11 13:27:05 +000016030 // On 32bit systems, we can't save 64bit integers. Try bitcasting to F64.
16031 if (TLI.isTypeLegal(MVT::f64) && StoreType.getSizeInBits() < 64 &&
16032 (64 <= NumElems * ToSz))
16033 StoreType = MVT::f64;
16034
Nadav Rotem614061b2011-08-10 19:30:14 +000016035 // Bitcast the original vector into a vector of store-size units
16036 EVT StoreVecVT = EVT::getVectorVT(*DAG.getContext(),
Nadav Rotem5cd95e12012-07-11 13:27:05 +000016037 StoreType, VT.getSizeInBits()/StoreType.getSizeInBits());
Nadav Rotem614061b2011-08-10 19:30:14 +000016038 assert(StoreVecVT.getSizeInBits() == VT.getSizeInBits());
16039 SDValue ShuffWide = DAG.getNode(ISD::BITCAST, dl, StoreVecVT, Shuff);
16040 SmallVector<SDValue, 8> Chains;
16041 SDValue Increment = DAG.getConstant(StoreType.getSizeInBits()/8,
16042 TLI.getPointerTy());
16043 SDValue Ptr = St->getBasePtr();
16044
16045 // Perform one or more big stores into memory.
Craig Topper31a207a2012-05-04 06:39:13 +000016046 for (unsigned i=0, e=(ToSz*NumElems)/StoreType.getSizeInBits(); i!=e; ++i) {
Nadav Rotem614061b2011-08-10 19:30:14 +000016047 SDValue SubVec = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl,
16048 StoreType, ShuffWide,
16049 DAG.getIntPtrConstant(i));
16050 SDValue Ch = DAG.getStore(St->getChain(), dl, SubVec, Ptr,
16051 St->getPointerInfo(), St->isVolatile(),
16052 St->isNonTemporal(), St->getAlignment());
16053 Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr, Increment);
16054 Chains.push_back(Ch);
16055 }
16056
16057 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &Chains[0],
16058 Chains.size());
16059 }
16060
16061
Chris Lattner149a4e52008-02-22 02:09:43 +000016062 // Turn load->store of MMX types into GPR load/stores. This avoids clobbering
16063 // the FP state in cases where an emms may be missing.
Dale Johannesen079f2a62008-02-25 19:20:14 +000016064 // A preferable solution to the general problem is to figure out the right
16065 // places to insert EMMS. This qualifies as a quick hack.
Evan Cheng536e6672009-03-12 05:59:15 +000016066
16067 // Similarly, turn load->store of i64 into double load/stores in 32-bit mode.
Evan Cheng536e6672009-03-12 05:59:15 +000016068 if (VT.getSizeInBits() != 64)
16069 return SDValue();
16070
Devang Patel578efa92009-06-05 21:57:13 +000016071 const Function *F = DAG.getMachineFunction().getFunction();
Bill Wendling67658342012-10-09 07:45:08 +000016072 bool NoImplicitFloatOps = F->getFnAttributes().
16073 hasAttribute(Attributes::NoImplicitFloat);
Nick Lewycky8a8d4792011-12-02 22:16:29 +000016074 bool F64IsLegal = !DAG.getTarget().Options.UseSoftFloat && !NoImplicitFloatOps
Craig Topper1accb7e2012-01-10 06:54:16 +000016075 && Subtarget->hasSSE2();
Evan Cheng536e6672009-03-12 05:59:15 +000016076 if ((VT.isVector() ||
Owen Anderson825b72b2009-08-11 20:47:22 +000016077 (VT == MVT::i64 && F64IsLegal && !Subtarget->is64Bit())) &&
Dale Johannesen079f2a62008-02-25 19:20:14 +000016078 isa<LoadSDNode>(St->getValue()) &&
16079 !cast<LoadSDNode>(St->getValue())->isVolatile() &&
16080 St->getChain().hasOneUse() && !St->isVolatile()) {
Gabor Greifba36cb52008-08-28 21:40:38 +000016081 SDNode* LdVal = St->getValue().getNode();
Dale Johannesen079f2a62008-02-25 19:20:14 +000016082 LoadSDNode *Ld = 0;
16083 int TokenFactorIndex = -1;
Dan Gohman475871a2008-07-27 21:46:04 +000016084 SmallVector<SDValue, 8> Ops;
Gabor Greifba36cb52008-08-28 21:40:38 +000016085 SDNode* ChainVal = St->getChain().getNode();
Dale Johannesen079f2a62008-02-25 19:20:14 +000016086 // Must be a store of a load. We currently handle two cases: the load
16087 // is a direct child, and it's under an intervening TokenFactor. It is
16088 // possible to dig deeper under nested TokenFactors.
Dale Johannesen14e2ea92008-02-25 22:29:22 +000016089 if (ChainVal == LdVal)
Dale Johannesen079f2a62008-02-25 19:20:14 +000016090 Ld = cast<LoadSDNode>(St->getChain());
16091 else if (St->getValue().hasOneUse() &&
16092 ChainVal->getOpcode() == ISD::TokenFactor) {
Chad Rosierc2348d52012-02-01 18:45:51 +000016093 for (unsigned i = 0, e = ChainVal->getNumOperands(); i != e; ++i) {
Gabor Greifba36cb52008-08-28 21:40:38 +000016094 if (ChainVal->getOperand(i).getNode() == LdVal) {
Dale Johannesen079f2a62008-02-25 19:20:14 +000016095 TokenFactorIndex = i;
16096 Ld = cast<LoadSDNode>(St->getValue());
16097 } else
16098 Ops.push_back(ChainVal->getOperand(i));
16099 }
16100 }
Dale Johannesen079f2a62008-02-25 19:20:14 +000016101
Evan Cheng536e6672009-03-12 05:59:15 +000016102 if (!Ld || !ISD::isNormalLoad(Ld))
16103 return SDValue();
Dale Johannesen079f2a62008-02-25 19:20:14 +000016104
Evan Cheng536e6672009-03-12 05:59:15 +000016105 // If this is not the MMX case, i.e. we are just turning i64 load/store
16106 // into f64 load/store, avoid the transformation if there are multiple
16107 // uses of the loaded value.
16108 if (!VT.isVector() && !Ld->hasNUsesOfValue(1, 0))
16109 return SDValue();
Dale Johannesen079f2a62008-02-25 19:20:14 +000016110
Evan Cheng536e6672009-03-12 05:59:15 +000016111 DebugLoc LdDL = Ld->getDebugLoc();
16112 DebugLoc StDL = N->getDebugLoc();
16113 // If we are a 64-bit capable x86, lower to a single movq load/store pair.
16114 // Otherwise, if it's legal to use f64 SSE instructions, use f64 load/store
16115 // pair instead.
16116 if (Subtarget->is64Bit() || F64IsLegal) {
Owen Anderson825b72b2009-08-11 20:47:22 +000016117 EVT LdVT = Subtarget->is64Bit() ? MVT::i64 : MVT::f64;
Chris Lattner51abfe42010-09-21 06:02:19 +000016118 SDValue NewLd = DAG.getLoad(LdVT, LdDL, Ld->getChain(), Ld->getBasePtr(),
16119 Ld->getPointerInfo(), Ld->isVolatile(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000016120 Ld->isNonTemporal(), Ld->isInvariant(),
16121 Ld->getAlignment());
Evan Cheng536e6672009-03-12 05:59:15 +000016122 SDValue NewChain = NewLd.getValue(1);
Dale Johannesen079f2a62008-02-25 19:20:14 +000016123 if (TokenFactorIndex != -1) {
Evan Cheng536e6672009-03-12 05:59:15 +000016124 Ops.push_back(NewChain);
Owen Anderson825b72b2009-08-11 20:47:22 +000016125 NewChain = DAG.getNode(ISD::TokenFactor, LdDL, MVT::Other, &Ops[0],
Dale Johannesen079f2a62008-02-25 19:20:14 +000016126 Ops.size());
16127 }
Evan Cheng536e6672009-03-12 05:59:15 +000016128 return DAG.getStore(NewChain, StDL, NewLd, St->getBasePtr(),
Chris Lattner51abfe42010-09-21 06:02:19 +000016129 St->getPointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +000016130 St->isVolatile(), St->isNonTemporal(),
16131 St->getAlignment());
Chris Lattner149a4e52008-02-22 02:09:43 +000016132 }
Evan Cheng536e6672009-03-12 05:59:15 +000016133
16134 // Otherwise, lower to two pairs of 32-bit loads / stores.
16135 SDValue LoAddr = Ld->getBasePtr();
Owen Anderson825b72b2009-08-11 20:47:22 +000016136 SDValue HiAddr = DAG.getNode(ISD::ADD, LdDL, MVT::i32, LoAddr,
16137 DAG.getConstant(4, MVT::i32));
Evan Cheng536e6672009-03-12 05:59:15 +000016138
Owen Anderson825b72b2009-08-11 20:47:22 +000016139 SDValue LoLd = DAG.getLoad(MVT::i32, LdDL, Ld->getChain(), LoAddr,
Chris Lattner51abfe42010-09-21 06:02:19 +000016140 Ld->getPointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +000016141 Ld->isVolatile(), Ld->isNonTemporal(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000016142 Ld->isInvariant(), Ld->getAlignment());
Owen Anderson825b72b2009-08-11 20:47:22 +000016143 SDValue HiLd = DAG.getLoad(MVT::i32, LdDL, Ld->getChain(), HiAddr,
Chris Lattner51abfe42010-09-21 06:02:19 +000016144 Ld->getPointerInfo().getWithOffset(4),
David Greene67c9d422010-02-15 16:53:33 +000016145 Ld->isVolatile(), Ld->isNonTemporal(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000016146 Ld->isInvariant(),
Evan Cheng536e6672009-03-12 05:59:15 +000016147 MinAlign(Ld->getAlignment(), 4));
16148
16149 SDValue NewChain = LoLd.getValue(1);
16150 if (TokenFactorIndex != -1) {
16151 Ops.push_back(LoLd);
16152 Ops.push_back(HiLd);
Owen Anderson825b72b2009-08-11 20:47:22 +000016153 NewChain = DAG.getNode(ISD::TokenFactor, LdDL, MVT::Other, &Ops[0],
Evan Cheng536e6672009-03-12 05:59:15 +000016154 Ops.size());
16155 }
16156
16157 LoAddr = St->getBasePtr();
Owen Anderson825b72b2009-08-11 20:47:22 +000016158 HiAddr = DAG.getNode(ISD::ADD, StDL, MVT::i32, LoAddr,
16159 DAG.getConstant(4, MVT::i32));
Evan Cheng536e6672009-03-12 05:59:15 +000016160
16161 SDValue LoSt = DAG.getStore(NewChain, StDL, LoLd, LoAddr,
Chris Lattner8026a9d2010-09-21 17:50:43 +000016162 St->getPointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +000016163 St->isVolatile(), St->isNonTemporal(),
16164 St->getAlignment());
Evan Cheng536e6672009-03-12 05:59:15 +000016165 SDValue HiSt = DAG.getStore(NewChain, StDL, HiLd, HiAddr,
Chris Lattner8026a9d2010-09-21 17:50:43 +000016166 St->getPointerInfo().getWithOffset(4),
Evan Cheng536e6672009-03-12 05:59:15 +000016167 St->isVolatile(),
David Greene67c9d422010-02-15 16:53:33 +000016168 St->isNonTemporal(),
Evan Cheng536e6672009-03-12 05:59:15 +000016169 MinAlign(St->getAlignment(), 4));
Owen Anderson825b72b2009-08-11 20:47:22 +000016170 return DAG.getNode(ISD::TokenFactor, StDL, MVT::Other, LoSt, HiSt);
Chris Lattner149a4e52008-02-22 02:09:43 +000016171 }
Dan Gohman475871a2008-07-27 21:46:04 +000016172 return SDValue();
Chris Lattner149a4e52008-02-22 02:09:43 +000016173}
16174
Duncan Sands17470be2011-09-22 20:15:48 +000016175/// isHorizontalBinOp - Return 'true' if this vector operation is "horizontal"
16176/// and return the operands for the horizontal operation in LHS and RHS. A
16177/// horizontal operation performs the binary operation on successive elements
16178/// of its first operand, then on successive elements of its second operand,
16179/// returning the resulting values in a vector. For example, if
16180/// A = < float a0, float a1, float a2, float a3 >
16181/// and
16182/// B = < float b0, float b1, float b2, float b3 >
16183/// then the result of doing a horizontal operation on A and B is
16184/// A horizontal-op B = < a0 op a1, a2 op a3, b0 op b1, b2 op b3 >.
16185/// In short, LHS and RHS are inspected to see if LHS op RHS is of the form
16186/// A horizontal-op B, for some already available A and B, and if so then LHS is
16187/// set to A, RHS to B, and the routine returns 'true'.
16188/// Note that the binary operation should have the property that if one of the
16189/// operands is UNDEF then the result is UNDEF.
Craig Topperbeabc6c2011-12-05 06:56:46 +000016190static bool isHorizontalBinOp(SDValue &LHS, SDValue &RHS, bool IsCommutative) {
Duncan Sands17470be2011-09-22 20:15:48 +000016191 // Look for the following pattern: if
16192 // A = < float a0, float a1, float a2, float a3 >
16193 // B = < float b0, float b1, float b2, float b3 >
16194 // and
16195 // LHS = VECTOR_SHUFFLE A, B, <0, 2, 4, 6>
16196 // RHS = VECTOR_SHUFFLE A, B, <1, 3, 5, 7>
16197 // then LHS op RHS = < a0 op a1, a2 op a3, b0 op b1, b2 op b3 >
16198 // which is A horizontal-op B.
16199
16200 // At least one of the operands should be a vector shuffle.
16201 if (LHS.getOpcode() != ISD::VECTOR_SHUFFLE &&
16202 RHS.getOpcode() != ISD::VECTOR_SHUFFLE)
16203 return false;
16204
16205 EVT VT = LHS.getValueType();
Craig Topperf8363302011-12-02 08:18:41 +000016206
16207 assert((VT.is128BitVector() || VT.is256BitVector()) &&
16208 "Unsupported vector type for horizontal add/sub");
16209
16210 // Handle 128 and 256-bit vector lengths. AVX defines horizontal add/sub to
16211 // operate independently on 128-bit lanes.
Craig Topperb72039c2011-11-30 09:10:50 +000016212 unsigned NumElts = VT.getVectorNumElements();
16213 unsigned NumLanes = VT.getSizeInBits()/128;
16214 unsigned NumLaneElts = NumElts / NumLanes;
Craig Topperf8363302011-12-02 08:18:41 +000016215 assert((NumLaneElts % 2 == 0) &&
16216 "Vector type should have an even number of elements in each lane");
16217 unsigned HalfLaneElts = NumLaneElts/2;
Duncan Sands17470be2011-09-22 20:15:48 +000016218
16219 // View LHS in the form
16220 // LHS = VECTOR_SHUFFLE A, B, LMask
16221 // If LHS is not a shuffle then pretend it is the shuffle
16222 // LHS = VECTOR_SHUFFLE LHS, undef, <0, 1, ..., N-1>
16223 // NOTE: in what follows a default initialized SDValue represents an UNDEF of
16224 // type VT.
16225 SDValue A, B;
Craig Topperb72039c2011-11-30 09:10:50 +000016226 SmallVector<int, 16> LMask(NumElts);
Duncan Sands17470be2011-09-22 20:15:48 +000016227 if (LHS.getOpcode() == ISD::VECTOR_SHUFFLE) {
16228 if (LHS.getOperand(0).getOpcode() != ISD::UNDEF)
16229 A = LHS.getOperand(0);
16230 if (LHS.getOperand(1).getOpcode() != ISD::UNDEF)
16231 B = LHS.getOperand(1);
Benjamin Kramered4c8c62012-01-15 13:16:05 +000016232 ArrayRef<int> Mask = cast<ShuffleVectorSDNode>(LHS.getNode())->getMask();
16233 std::copy(Mask.begin(), Mask.end(), LMask.begin());
Duncan Sands17470be2011-09-22 20:15:48 +000016234 } else {
16235 if (LHS.getOpcode() != ISD::UNDEF)
16236 A = LHS;
Craig Topperb72039c2011-11-30 09:10:50 +000016237 for (unsigned i = 0; i != NumElts; ++i)
Duncan Sands17470be2011-09-22 20:15:48 +000016238 LMask[i] = i;
16239 }
16240
16241 // Likewise, view RHS in the form
16242 // RHS = VECTOR_SHUFFLE C, D, RMask
16243 SDValue C, D;
Craig Topperb72039c2011-11-30 09:10:50 +000016244 SmallVector<int, 16> RMask(NumElts);
Duncan Sands17470be2011-09-22 20:15:48 +000016245 if (RHS.getOpcode() == ISD::VECTOR_SHUFFLE) {
16246 if (RHS.getOperand(0).getOpcode() != ISD::UNDEF)
16247 C = RHS.getOperand(0);
16248 if (RHS.getOperand(1).getOpcode() != ISD::UNDEF)
16249 D = RHS.getOperand(1);
Benjamin Kramered4c8c62012-01-15 13:16:05 +000016250 ArrayRef<int> Mask = cast<ShuffleVectorSDNode>(RHS.getNode())->getMask();
16251 std::copy(Mask.begin(), Mask.end(), RMask.begin());
Duncan Sands17470be2011-09-22 20:15:48 +000016252 } else {
16253 if (RHS.getOpcode() != ISD::UNDEF)
16254 C = RHS;
Craig Topperb72039c2011-11-30 09:10:50 +000016255 for (unsigned i = 0; i != NumElts; ++i)
Duncan Sands17470be2011-09-22 20:15:48 +000016256 RMask[i] = i;
16257 }
16258
16259 // Check that the shuffles are both shuffling the same vectors.
16260 if (!(A == C && B == D) && !(A == D && B == C))
16261 return false;
16262
16263 // If everything is UNDEF then bail out: it would be better to fold to UNDEF.
16264 if (!A.getNode() && !B.getNode())
16265 return false;
16266
16267 // If A and B occur in reverse order in RHS, then "swap" them (which means
16268 // rewriting the mask).
16269 if (A != C)
Craig Topperbeabc6c2011-12-05 06:56:46 +000016270 CommuteVectorShuffleMask(RMask, NumElts);
Duncan Sands17470be2011-09-22 20:15:48 +000016271
16272 // At this point LHS and RHS are equivalent to
16273 // LHS = VECTOR_SHUFFLE A, B, LMask
16274 // RHS = VECTOR_SHUFFLE A, B, RMask
16275 // Check that the masks correspond to performing a horizontal operation.
Craig Topperf8363302011-12-02 08:18:41 +000016276 for (unsigned i = 0; i != NumElts; ++i) {
Craig Topperbeabc6c2011-12-05 06:56:46 +000016277 int LIdx = LMask[i], RIdx = RMask[i];
Duncan Sands17470be2011-09-22 20:15:48 +000016278
Craig Topperf8363302011-12-02 08:18:41 +000016279 // Ignore any UNDEF components.
Craig Topperbeabc6c2011-12-05 06:56:46 +000016280 if (LIdx < 0 || RIdx < 0 ||
16281 (!A.getNode() && (LIdx < (int)NumElts || RIdx < (int)NumElts)) ||
16282 (!B.getNode() && (LIdx >= (int)NumElts || RIdx >= (int)NumElts)))
Craig Topperf8363302011-12-02 08:18:41 +000016283 continue;
Duncan Sands17470be2011-09-22 20:15:48 +000016284
Craig Topperf8363302011-12-02 08:18:41 +000016285 // Check that successive elements are being operated on. If not, this is
16286 // not a horizontal operation.
16287 unsigned Src = (i/HalfLaneElts) % 2; // each lane is split between srcs
16288 unsigned LaneStart = (i/NumLaneElts) * NumLaneElts;
Craig Topperbeabc6c2011-12-05 06:56:46 +000016289 int Index = 2*(i%HalfLaneElts) + NumElts*Src + LaneStart;
Craig Topperf8363302011-12-02 08:18:41 +000016290 if (!(LIdx == Index && RIdx == Index + 1) &&
Craig Topperbeabc6c2011-12-05 06:56:46 +000016291 !(IsCommutative && LIdx == Index + 1 && RIdx == Index))
Craig Topperf8363302011-12-02 08:18:41 +000016292 return false;
Duncan Sands17470be2011-09-22 20:15:48 +000016293 }
16294
16295 LHS = A.getNode() ? A : B; // If A is 'UNDEF', use B for it.
16296 RHS = B.getNode() ? B : A; // If B is 'UNDEF', use A for it.
16297 return true;
16298}
16299
16300/// PerformFADDCombine - Do target-specific dag combines on floating point adds.
16301static SDValue PerformFADDCombine(SDNode *N, SelectionDAG &DAG,
16302 const X86Subtarget *Subtarget) {
16303 EVT VT = N->getValueType(0);
16304 SDValue LHS = N->getOperand(0);
16305 SDValue RHS = N->getOperand(1);
16306
16307 // Try to synthesize horizontal adds from adds of shuffles.
Craig Topperd0a31172012-01-10 06:37:29 +000016308 if (((Subtarget->hasSSE3() && (VT == MVT::v4f32 || VT == MVT::v2f64)) ||
Craig Topper138a5c62011-12-02 07:16:01 +000016309 (Subtarget->hasAVX() && (VT == MVT::v8f32 || VT == MVT::v4f64))) &&
Duncan Sands17470be2011-09-22 20:15:48 +000016310 isHorizontalBinOp(LHS, RHS, true))
16311 return DAG.getNode(X86ISD::FHADD, N->getDebugLoc(), VT, LHS, RHS);
16312 return SDValue();
16313}
16314
16315/// PerformFSUBCombine - Do target-specific dag combines on floating point subs.
16316static SDValue PerformFSUBCombine(SDNode *N, SelectionDAG &DAG,
16317 const X86Subtarget *Subtarget) {
16318 EVT VT = N->getValueType(0);
16319 SDValue LHS = N->getOperand(0);
16320 SDValue RHS = N->getOperand(1);
16321
16322 // Try to synthesize horizontal subs from subs of shuffles.
Craig Topperd0a31172012-01-10 06:37:29 +000016323 if (((Subtarget->hasSSE3() && (VT == MVT::v4f32 || VT == MVT::v2f64)) ||
Craig Topper138a5c62011-12-02 07:16:01 +000016324 (Subtarget->hasAVX() && (VT == MVT::v8f32 || VT == MVT::v4f64))) &&
Duncan Sands17470be2011-09-22 20:15:48 +000016325 isHorizontalBinOp(LHS, RHS, false))
16326 return DAG.getNode(X86ISD::FHSUB, N->getDebugLoc(), VT, LHS, RHS);
16327 return SDValue();
16328}
16329
Chris Lattner6cf73262008-01-25 06:14:17 +000016330/// PerformFORCombine - Do target-specific dag combines on X86ISD::FOR and
16331/// X86ISD::FXOR nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000016332static SDValue PerformFORCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattner6cf73262008-01-25 06:14:17 +000016333 assert(N->getOpcode() == X86ISD::FOR || N->getOpcode() == X86ISD::FXOR);
16334 // F[X]OR(0.0, x) -> x
16335 // F[X]OR(x, 0.0) -> x
Chris Lattneraf723b92008-01-25 05:46:26 +000016336 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
16337 if (C->getValueAPF().isPosZero())
16338 return N->getOperand(1);
16339 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
16340 if (C->getValueAPF().isPosZero())
16341 return N->getOperand(0);
Dan Gohman475871a2008-07-27 21:46:04 +000016342 return SDValue();
Chris Lattneraf723b92008-01-25 05:46:26 +000016343}
16344
Nadav Rotemd60cb112012-08-19 13:06:16 +000016345/// PerformFMinFMaxCombine - Do target-specific dag combines on X86ISD::FMIN and
16346/// X86ISD::FMAX nodes.
16347static SDValue PerformFMinFMaxCombine(SDNode *N, SelectionDAG &DAG) {
16348 assert(N->getOpcode() == X86ISD::FMIN || N->getOpcode() == X86ISD::FMAX);
16349
16350 // Only perform optimizations if UnsafeMath is used.
16351 if (!DAG.getTarget().Options.UnsafeFPMath)
16352 return SDValue();
16353
16354 // If we run in unsafe-math mode, then convert the FMAX and FMIN nodes
Craig Topper8365e9b2012-09-01 06:33:50 +000016355 // into FMINC and FMAXC, which are Commutative operations.
Nadav Rotemd60cb112012-08-19 13:06:16 +000016356 unsigned NewOp = 0;
16357 switch (N->getOpcode()) {
16358 default: llvm_unreachable("unknown opcode");
16359 case X86ISD::FMIN: NewOp = X86ISD::FMINC; break;
16360 case X86ISD::FMAX: NewOp = X86ISD::FMAXC; break;
16361 }
16362
16363 return DAG.getNode(NewOp, N->getDebugLoc(), N->getValueType(0),
16364 N->getOperand(0), N->getOperand(1));
16365}
16366
16367
Chris Lattneraf723b92008-01-25 05:46:26 +000016368/// PerformFANDCombine - Do target-specific dag combines on X86ISD::FAND nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000016369static SDValue PerformFANDCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattneraf723b92008-01-25 05:46:26 +000016370 // FAND(0.0, x) -> 0.0
16371 // FAND(x, 0.0) -> 0.0
16372 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
16373 if (C->getValueAPF().isPosZero())
16374 return N->getOperand(0);
16375 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
16376 if (C->getValueAPF().isPosZero())
16377 return N->getOperand(1);
Dan Gohman475871a2008-07-27 21:46:04 +000016378 return SDValue();
Chris Lattneraf723b92008-01-25 05:46:26 +000016379}
16380
Dan Gohmane5af2d32009-01-29 01:59:02 +000016381static SDValue PerformBTCombine(SDNode *N,
16382 SelectionDAG &DAG,
16383 TargetLowering::DAGCombinerInfo &DCI) {
16384 // BT ignores high bits in the bit index operand.
16385 SDValue Op1 = N->getOperand(1);
16386 if (Op1.hasOneUse()) {
16387 unsigned BitWidth = Op1.getValueSizeInBits();
16388 APInt DemandedMask = APInt::getLowBitsSet(BitWidth, Log2_32(BitWidth));
16389 APInt KnownZero, KnownOne;
Evan Chenge5b51ac2010-04-17 06:13:15 +000016390 TargetLowering::TargetLoweringOpt TLO(DAG, !DCI.isBeforeLegalize(),
16391 !DCI.isBeforeLegalizeOps());
Dan Gohmand858e902010-04-17 15:26:15 +000016392 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Dan Gohmane5af2d32009-01-29 01:59:02 +000016393 if (TLO.ShrinkDemandedConstant(Op1, DemandedMask) ||
16394 TLI.SimplifyDemandedBits(Op1, DemandedMask, KnownZero, KnownOne, TLO))
16395 DCI.CommitTargetLoweringOpt(TLO);
16396 }
16397 return SDValue();
16398}
Chris Lattner83e6c992006-10-04 06:57:07 +000016399
Eli Friedman7a5e5552009-06-07 06:52:44 +000016400static SDValue PerformVZEXT_MOVLCombine(SDNode *N, SelectionDAG &DAG) {
16401 SDValue Op = N->getOperand(0);
Wesley Peckbf17cfa2010-11-23 03:31:01 +000016402 if (Op.getOpcode() == ISD::BITCAST)
Eli Friedman7a5e5552009-06-07 06:52:44 +000016403 Op = Op.getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +000016404 EVT VT = N->getValueType(0), OpVT = Op.getValueType();
Eli Friedman7a5e5552009-06-07 06:52:44 +000016405 if (Op.getOpcode() == X86ISD::VZEXT_LOAD &&
Eric Christopherfd179292009-08-27 18:07:15 +000016406 VT.getVectorElementType().getSizeInBits() ==
Eli Friedman7a5e5552009-06-07 06:52:44 +000016407 OpVT.getVectorElementType().getSizeInBits()) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +000016408 return DAG.getNode(ISD::BITCAST, N->getDebugLoc(), VT, Op);
Eli Friedman7a5e5552009-06-07 06:52:44 +000016409 }
16410 return SDValue();
16411}
16412
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +000016413static SDValue PerformSExtCombine(SDNode *N, SelectionDAG &DAG,
16414 TargetLowering::DAGCombinerInfo &DCI,
16415 const X86Subtarget *Subtarget) {
16416 if (!DCI.isBeforeLegalizeOps())
16417 return SDValue();
16418
Craig Topper3ef43cf2012-04-24 06:36:35 +000016419 if (!Subtarget->hasAVX())
Elena Demikhovskyf6020402012-02-08 08:37:26 +000016420 return SDValue();
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +000016421
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +000016422 EVT VT = N->getValueType(0);
16423 SDValue Op = N->getOperand(0);
16424 EVT OpVT = Op.getValueType();
16425 DebugLoc dl = N->getDebugLoc();
16426
Elena Demikhovskyf6020402012-02-08 08:37:26 +000016427 if ((VT == MVT::v4i64 && OpVT == MVT::v4i32) ||
16428 (VT == MVT::v8i32 && OpVT == MVT::v8i16)) {
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +000016429
Craig Topper3ef43cf2012-04-24 06:36:35 +000016430 if (Subtarget->hasAVX2())
Elena Demikhovsky1da58672012-04-22 09:39:03 +000016431 return DAG.getNode(X86ISD::VSEXT_MOVL, dl, VT, Op);
Elena Demikhovsky1da58672012-04-22 09:39:03 +000016432
16433 // Optimize vectors in AVX mode
16434 // Sign extend v8i16 to v8i32 and
16435 // v4i32 to v4i64
16436 //
16437 // Divide input vector into two parts
16438 // for v4i32 the shuffle mask will be { 0, 1, -1, -1} {2, 3, -1, -1}
16439 // use vpmovsx instruction to extend v4i32 -> v2i64; v8i16 -> v4i32
16440 // concat the vectors to original VT
16441
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +000016442 unsigned NumElems = OpVT.getVectorNumElements();
Craig Toppercacafd42012-08-14 08:18:43 +000016443 SDValue Undef = DAG.getUNDEF(OpVT);
16444
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +000016445 SmallVector<int,8> ShufMask1(NumElems, -1);
Craig Topper3ef43cf2012-04-24 06:36:35 +000016446 for (unsigned i = 0; i != NumElems/2; ++i)
16447 ShufMask1[i] = i;
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +000016448
Craig Toppercacafd42012-08-14 08:18:43 +000016449 SDValue OpLo = DAG.getVectorShuffle(OpVT, dl, Op, Undef, &ShufMask1[0]);
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +000016450
16451 SmallVector<int,8> ShufMask2(NumElems, -1);
Craig Topper3ef43cf2012-04-24 06:36:35 +000016452 for (unsigned i = 0; i != NumElems/2; ++i)
16453 ShufMask2[i] = i + NumElems/2;
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +000016454
Craig Toppercacafd42012-08-14 08:18:43 +000016455 SDValue OpHi = DAG.getVectorShuffle(OpVT, dl, Op, Undef, &ShufMask2[0]);
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +000016456
Craig Topper3ef43cf2012-04-24 06:36:35 +000016457 EVT HalfVT = EVT::getVectorVT(*DAG.getContext(), VT.getScalarType(),
Elena Demikhovskyf6020402012-02-08 08:37:26 +000016458 VT.getVectorNumElements()/2);
16459
Craig Topper3ef43cf2012-04-24 06:36:35 +000016460 OpLo = DAG.getNode(X86ISD::VSEXT_MOVL, dl, HalfVT, OpLo);
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +000016461 OpHi = DAG.getNode(X86ISD::VSEXT_MOVL, dl, HalfVT, OpHi);
16462
16463 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT, OpLo, OpHi);
16464 }
16465 return SDValue();
16466}
16467
Michael Liaof6c24ee2012-08-10 14:39:24 +000016468static SDValue PerformFMACombine(SDNode *N, SelectionDAG &DAG,
Elena Demikhovsky1503aba2012-08-01 12:06:00 +000016469 const X86Subtarget* Subtarget) {
16470 DebugLoc dl = N->getDebugLoc();
16471 EVT VT = N->getValueType(0);
16472
Craig Topperb1bdd7d2012-08-30 06:56:15 +000016473 // Let legalize expand this if it isn't a legal type yet.
16474 if (!DAG.getTargetLoweringInfo().isTypeLegal(VT))
16475 return SDValue();
16476
Elena Demikhovsky1503aba2012-08-01 12:06:00 +000016477 EVT ScalarVT = VT.getScalarType();
Craig Topperbf404372012-08-31 15:40:30 +000016478 if ((ScalarVT != MVT::f32 && ScalarVT != MVT::f64) ||
16479 (!Subtarget->hasFMA() && !Subtarget->hasFMA4()))
Elena Demikhovsky1503aba2012-08-01 12:06:00 +000016480 return SDValue();
16481
16482 SDValue A = N->getOperand(0);
16483 SDValue B = N->getOperand(1);
16484 SDValue C = N->getOperand(2);
16485
16486 bool NegA = (A.getOpcode() == ISD::FNEG);
16487 bool NegB = (B.getOpcode() == ISD::FNEG);
16488 bool NegC = (C.getOpcode() == ISD::FNEG);
16489
Michael Liaof6c24ee2012-08-10 14:39:24 +000016490 // Negative multiplication when NegA xor NegB
16491 bool NegMul = (NegA != NegB);
Elena Demikhovsky1503aba2012-08-01 12:06:00 +000016492 if (NegA)
16493 A = A.getOperand(0);
16494 if (NegB)
16495 B = B.getOperand(0);
16496 if (NegC)
16497 C = C.getOperand(0);
16498
16499 unsigned Opcode;
16500 if (!NegMul)
Craig Topperbf404372012-08-31 15:40:30 +000016501 Opcode = (!NegC) ? X86ISD::FMADD : X86ISD::FMSUB;
Elena Demikhovsky1503aba2012-08-01 12:06:00 +000016502 else
Craig Topperbf404372012-08-31 15:40:30 +000016503 Opcode = (!NegC) ? X86ISD::FNMADD : X86ISD::FNMSUB;
16504
Elena Demikhovsky1503aba2012-08-01 12:06:00 +000016505 return DAG.getNode(Opcode, dl, VT, A, B, C);
16506}
16507
Elena Demikhovsky28d7e712012-01-24 13:54:13 +000016508static SDValue PerformZExtCombine(SDNode *N, SelectionDAG &DAG,
Craig Topperc16f8512012-04-25 06:39:39 +000016509 TargetLowering::DAGCombinerInfo &DCI,
Elena Demikhovsky28d7e712012-01-24 13:54:13 +000016510 const X86Subtarget *Subtarget) {
Evan Cheng2e489c42009-12-16 00:53:11 +000016511 // (i32 zext (and (i8 x86isd::setcc_carry), 1)) ->
16512 // (and (i32 x86isd::setcc_carry), 1)
16513 // This eliminates the zext. This transformation is necessary because
16514 // ISD::SETCC is always legalized to i8.
16515 DebugLoc dl = N->getDebugLoc();
16516 SDValue N0 = N->getOperand(0);
16517 EVT VT = N->getValueType(0);
Elena Demikhovsky28d7e712012-01-24 13:54:13 +000016518 EVT OpVT = N0.getValueType();
16519
Evan Cheng2e489c42009-12-16 00:53:11 +000016520 if (N0.getOpcode() == ISD::AND &&
16521 N0.hasOneUse() &&
16522 N0.getOperand(0).hasOneUse()) {
16523 SDValue N00 = N0.getOperand(0);
16524 if (N00.getOpcode() != X86ISD::SETCC_CARRY)
16525 return SDValue();
16526 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
16527 if (!C || C->getZExtValue() != 1)
16528 return SDValue();
16529 return DAG.getNode(ISD::AND, dl, VT,
16530 DAG.getNode(X86ISD::SETCC_CARRY, dl, VT,
16531 N00.getOperand(0), N00.getOperand(1)),
16532 DAG.getConstant(1, VT));
16533 }
Craig Topperd0cf5652012-04-21 18:13:35 +000016534
Elena Demikhovsky28d7e712012-01-24 13:54:13 +000016535 // Optimize vectors in AVX mode:
16536 //
16537 // v8i16 -> v8i32
16538 // Use vpunpcklwd for 4 lower elements v8i16 -> v4i32.
16539 // Use vpunpckhwd for 4 upper elements v8i16 -> v4i32.
16540 // Concat upper and lower parts.
16541 //
16542 // v4i32 -> v4i64
16543 // Use vpunpckldq for 4 lower elements v4i32 -> v2i64.
16544 // Use vpunpckhdq for 4 upper elements v4i32 -> v2i64.
16545 // Concat upper and lower parts.
16546 //
Craig Topperc16f8512012-04-25 06:39:39 +000016547 if (!DCI.isBeforeLegalizeOps())
16548 return SDValue();
Elena Demikhovsky28d7e712012-01-24 13:54:13 +000016549
Craig Topperc16f8512012-04-25 06:39:39 +000016550 if (!Subtarget->hasAVX())
16551 return SDValue();
Elena Demikhovsky28d7e712012-01-24 13:54:13 +000016552
Craig Topperc16f8512012-04-25 06:39:39 +000016553 if (((VT == MVT::v8i32) && (OpVT == MVT::v8i16)) ||
16554 ((VT == MVT::v4i64) && (OpVT == MVT::v4i32))) {
Elena Demikhovsky1da58672012-04-22 09:39:03 +000016555
Craig Topperc16f8512012-04-25 06:39:39 +000016556 if (Subtarget->hasAVX2())
16557 return DAG.getNode(X86ISD::VZEXT_MOVL, dl, VT, N0);
Elena Demikhovsky28d7e712012-01-24 13:54:13 +000016558
Craig Topperc16f8512012-04-25 06:39:39 +000016559 SDValue ZeroVec = getZeroVector(OpVT, Subtarget, DAG, dl);
16560 SDValue OpLo = getUnpackl(DAG, dl, OpVT, N0, ZeroVec);
16561 SDValue OpHi = getUnpackh(DAG, dl, OpVT, N0, ZeroVec);
Elena Demikhovsky28d7e712012-01-24 13:54:13 +000016562
Craig Topperc16f8512012-04-25 06:39:39 +000016563 EVT HVT = EVT::getVectorVT(*DAG.getContext(), VT.getVectorElementType(),
16564 VT.getVectorNumElements()/2);
Elena Demikhovsky28d7e712012-01-24 13:54:13 +000016565
Craig Topperc16f8512012-04-25 06:39:39 +000016566 OpLo = DAG.getNode(ISD::BITCAST, dl, HVT, OpLo);
16567 OpHi = DAG.getNode(ISD::BITCAST, dl, HVT, OpHi);
16568
16569 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT, OpLo, OpHi);
Elena Demikhovsky28d7e712012-01-24 13:54:13 +000016570 }
16571
Evan Cheng2e489c42009-12-16 00:53:11 +000016572 return SDValue();
16573}
16574
Chad Rosiera73b6fc2012-04-27 22:33:25 +000016575// Optimize x == -y --> x+y == 0
16576// x != -y --> x+y != 0
16577static SDValue PerformISDSETCCCombine(SDNode *N, SelectionDAG &DAG) {
16578 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(2))->get();
16579 SDValue LHS = N->getOperand(0);
Chad Rosiera20e1e72012-08-01 18:39:17 +000016580 SDValue RHS = N->getOperand(1);
Chad Rosiera73b6fc2012-04-27 22:33:25 +000016581
16582 if ((CC == ISD::SETNE || CC == ISD::SETEQ) && LHS.getOpcode() == ISD::SUB)
16583 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(LHS.getOperand(0)))
16584 if (C->getAPIntValue() == 0 && LHS.hasOneUse()) {
16585 SDValue addV = DAG.getNode(ISD::ADD, N->getDebugLoc(),
16586 LHS.getValueType(), RHS, LHS.getOperand(1));
16587 return DAG.getSetCC(N->getDebugLoc(), N->getValueType(0),
16588 addV, DAG.getConstant(0, addV.getValueType()), CC);
16589 }
16590 if ((CC == ISD::SETNE || CC == ISD::SETEQ) && RHS.getOpcode() == ISD::SUB)
16591 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(RHS.getOperand(0)))
16592 if (C->getAPIntValue() == 0 && RHS.hasOneUse()) {
16593 SDValue addV = DAG.getNode(ISD::ADD, N->getDebugLoc(),
16594 RHS.getValueType(), LHS, RHS.getOperand(1));
16595 return DAG.getSetCC(N->getDebugLoc(), N->getValueType(0),
16596 addV, DAG.getConstant(0, addV.getValueType()), CC);
16597 }
16598 return SDValue();
16599}
16600
Shuxin Yanga5526a92012-10-31 23:11:48 +000016601// Helper function of PerformSETCCCombine. It is to materialize "setb reg"
16602// as "sbb reg,reg", since it can be extended without zext and produces
16603// an all-ones bit which is more useful than 0/1 in some cases.
16604static SDValue MaterializeSETB(DebugLoc DL, SDValue EFLAGS, SelectionDAG &DAG) {
16605 return DAG.getNode(ISD::AND, DL, MVT::i8,
16606 DAG.getNode(X86ISD::SETCC_CARRY, DL, MVT::i8,
16607 DAG.getConstant(X86::COND_B, MVT::i8), EFLAGS),
16608 DAG.getConstant(1, MVT::i8));
16609}
16610
Chris Lattnerc19d1c32010-12-19 22:08:31 +000016611// Optimize RES = X86ISD::SETCC CONDCODE, EFLAG_INPUT
Michael Liaodbf8b5b2012-08-28 03:34:40 +000016612static SDValue PerformSETCCCombine(SDNode *N, SelectionDAG &DAG,
16613 TargetLowering::DAGCombinerInfo &DCI,
16614 const X86Subtarget *Subtarget) {
Chris Lattnerc19d1c32010-12-19 22:08:31 +000016615 DebugLoc DL = N->getDebugLoc();
Michael Liao2a33cec2012-08-10 19:58:13 +000016616 X86::CondCode CC = X86::CondCode(N->getConstantOperandVal(0));
16617 SDValue EFLAGS = N->getOperand(1);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000016618
Shuxin Yanga5526a92012-10-31 23:11:48 +000016619 if (CC == X86::COND_A) {
16620 // Try to convert COND_A into COND_B in an attempt to facilitate
16621 // materializing "setb reg".
16622 //
16623 // Do not flip "e > c", where "c" is a constant, because Cmp instruction
16624 // cannot take an immediate as its first operand.
16625 //
16626 if (EFLAGS.getOpcode() == X86ISD::SUB && EFLAGS.hasOneUse() &&
16627 EFLAGS.getValueType().isInteger() &&
16628 !isa<ConstantSDNode>(EFLAGS.getOperand(1))) {
16629 SDValue NewSub = DAG.getNode(X86ISD::SUB, EFLAGS.getDebugLoc(),
16630 EFLAGS.getNode()->getVTList(),
16631 EFLAGS.getOperand(1), EFLAGS.getOperand(0));
16632 SDValue NewEFLAGS = SDValue(NewSub.getNode(), EFLAGS.getResNo());
16633 return MaterializeSETB(DL, NewEFLAGS, DAG);
16634 }
16635 }
16636
Chris Lattnerc19d1c32010-12-19 22:08:31 +000016637 // Materialize "setb reg" as "sbb reg,reg", since it can be extended without
16638 // a zext and produces an all-ones bit which is more useful than 0/1 in some
16639 // cases.
Michael Liao2a33cec2012-08-10 19:58:13 +000016640 if (CC == X86::COND_B)
Shuxin Yanga5526a92012-10-31 23:11:48 +000016641 return MaterializeSETB(DL, EFLAGS, DAG);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000016642
Michael Liao2a33cec2012-08-10 19:58:13 +000016643 SDValue Flags;
16644
Michael Liaodbf8b5b2012-08-28 03:34:40 +000016645 Flags = checkBoolTestSetCCCombine(EFLAGS, CC);
16646 if (Flags.getNode()) {
16647 SDValue Cond = DAG.getConstant(CC, MVT::i8);
16648 return DAG.getNode(X86ISD::SETCC, DL, N->getVTList(), Cond, Flags);
16649 }
16650
Michael Liao2a33cec2012-08-10 19:58:13 +000016651 return SDValue();
16652}
16653
16654// Optimize branch condition evaluation.
16655//
16656static SDValue PerformBrCondCombine(SDNode *N, SelectionDAG &DAG,
16657 TargetLowering::DAGCombinerInfo &DCI,
16658 const X86Subtarget *Subtarget) {
16659 DebugLoc DL = N->getDebugLoc();
16660 SDValue Chain = N->getOperand(0);
16661 SDValue Dest = N->getOperand(1);
16662 SDValue EFLAGS = N->getOperand(3);
16663 X86::CondCode CC = X86::CondCode(N->getConstantOperandVal(2));
16664
16665 SDValue Flags;
16666
Michael Liaodbf8b5b2012-08-28 03:34:40 +000016667 Flags = checkBoolTestSetCCCombine(EFLAGS, CC);
16668 if (Flags.getNode()) {
16669 SDValue Cond = DAG.getConstant(CC, MVT::i8);
16670 return DAG.getNode(X86ISD::BRCOND, DL, N->getVTList(), Chain, Dest, Cond,
16671 Flags);
16672 }
16673
Chris Lattnerc19d1c32010-12-19 22:08:31 +000016674 return SDValue();
16675}
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000016676
Benjamin Kramer1396c402011-06-18 11:09:41 +000016677static SDValue PerformSINT_TO_FPCombine(SDNode *N, SelectionDAG &DAG,
16678 const X86TargetLowering *XTLI) {
Stuart Hastingsf99a4b82011-06-06 23:15:58 +000016679 SDValue Op0 = N->getOperand(0);
Nadav Rotema3540772012-04-23 21:53:37 +000016680 EVT InVT = Op0->getValueType(0);
Nadav Rotema3540772012-04-23 21:53:37 +000016681
16682 // SINT_TO_FP(v4i8) -> SINT_TO_FP(SEXT(v4i8 to v4i32))
Craig Topper7fd5e162012-04-24 06:02:29 +000016683 if (InVT == MVT::v8i8 || InVT == MVT::v4i8) {
Nadav Rotema3540772012-04-23 21:53:37 +000016684 DebugLoc dl = N->getDebugLoc();
Craig Topper7fd5e162012-04-24 06:02:29 +000016685 MVT DstVT = InVT == MVT::v4i8 ? MVT::v4i32 : MVT::v8i32;
Nadav Rotema3540772012-04-23 21:53:37 +000016686 SDValue P = DAG.getNode(ISD::SIGN_EXTEND, dl, DstVT, Op0);
16687 return DAG.getNode(ISD::SINT_TO_FP, dl, N->getValueType(0), P);
16688 }
16689
Stuart Hastingsf99a4b82011-06-06 23:15:58 +000016690 // Transform (SINT_TO_FP (i64 ...)) into an x87 operation if we have
16691 // a 32-bit target where SSE doesn't support i64->FP operations.
16692 if (Op0.getOpcode() == ISD::LOAD) {
16693 LoadSDNode *Ld = cast<LoadSDNode>(Op0.getNode());
16694 EVT VT = Ld->getValueType(0);
16695 if (!Ld->isVolatile() && !N->getValueType(0).isVector() &&
16696 ISD::isNON_EXTLoad(Op0.getNode()) && Op0.hasOneUse() &&
16697 !XTLI->getSubtarget()->is64Bit() &&
16698 !DAG.getTargetLoweringInfo().isTypeLegal(VT)) {
Benjamin Kramer1396c402011-06-18 11:09:41 +000016699 SDValue FILDChain = XTLI->BuildFILD(SDValue(N, 0), Ld->getValueType(0),
16700 Ld->getChain(), Op0, DAG);
Stuart Hastingsf99a4b82011-06-06 23:15:58 +000016701 DAG.ReplaceAllUsesOfValueWith(Op0.getValue(1), FILDChain.getValue(1));
16702 return FILDChain;
16703 }
16704 }
16705 return SDValue();
16706}
16707
Chris Lattner23a01992010-12-20 01:37:09 +000016708// Optimize RES, EFLAGS = X86ISD::ADC LHS, RHS, EFLAGS
16709static SDValue PerformADCCombine(SDNode *N, SelectionDAG &DAG,
16710 X86TargetLowering::DAGCombinerInfo &DCI) {
16711 // If the LHS and RHS of the ADC node are zero, then it can't overflow and
16712 // the result is either zero or one (depending on the input carry bit).
16713 // Strength reduce this down to a "set on carry" aka SETCC_CARRY&1.
16714 if (X86::isZeroNode(N->getOperand(0)) &&
16715 X86::isZeroNode(N->getOperand(1)) &&
16716 // We don't have a good way to replace an EFLAGS use, so only do this when
16717 // dead right now.
16718 SDValue(N, 1).use_empty()) {
16719 DebugLoc DL = N->getDebugLoc();
16720 EVT VT = N->getValueType(0);
16721 SDValue CarryOut = DAG.getConstant(0, N->getValueType(1));
16722 SDValue Res1 = DAG.getNode(ISD::AND, DL, VT,
16723 DAG.getNode(X86ISD::SETCC_CARRY, DL, VT,
16724 DAG.getConstant(X86::COND_B,MVT::i8),
16725 N->getOperand(2)),
16726 DAG.getConstant(1, VT));
16727 return DCI.CombineTo(N, Res1, CarryOut);
16728 }
16729
16730 return SDValue();
16731}
16732
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000016733// fold (add Y, (sete X, 0)) -> adc 0, Y
16734// (add Y, (setne X, 0)) -> sbb -1, Y
16735// (sub (sete X, 0), Y) -> sbb 0, Y
16736// (sub (setne X, 0), Y) -> adc -1, Y
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000016737static SDValue OptimizeConditionalInDecrement(SDNode *N, SelectionDAG &DAG) {
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000016738 DebugLoc DL = N->getDebugLoc();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000016739
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000016740 // Look through ZExts.
16741 SDValue Ext = N->getOperand(N->getOpcode() == ISD::SUB ? 1 : 0);
16742 if (Ext.getOpcode() != ISD::ZERO_EXTEND || !Ext.hasOneUse())
16743 return SDValue();
16744
16745 SDValue SetCC = Ext.getOperand(0);
16746 if (SetCC.getOpcode() != X86ISD::SETCC || !SetCC.hasOneUse())
16747 return SDValue();
16748
16749 X86::CondCode CC = (X86::CondCode)SetCC.getConstantOperandVal(0);
16750 if (CC != X86::COND_E && CC != X86::COND_NE)
16751 return SDValue();
16752
16753 SDValue Cmp = SetCC.getOperand(1);
16754 if (Cmp.getOpcode() != X86ISD::CMP || !Cmp.hasOneUse() ||
Chris Lattner9cd3da42011-01-16 02:56:53 +000016755 !X86::isZeroNode(Cmp.getOperand(1)) ||
16756 !Cmp.getOperand(0).getValueType().isInteger())
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000016757 return SDValue();
16758
16759 SDValue CmpOp0 = Cmp.getOperand(0);
16760 SDValue NewCmp = DAG.getNode(X86ISD::CMP, DL, MVT::i32, CmpOp0,
16761 DAG.getConstant(1, CmpOp0.getValueType()));
16762
16763 SDValue OtherVal = N->getOperand(N->getOpcode() == ISD::SUB ? 0 : 1);
16764 if (CC == X86::COND_NE)
16765 return DAG.getNode(N->getOpcode() == ISD::SUB ? X86ISD::ADC : X86ISD::SBB,
16766 DL, OtherVal.getValueType(), OtherVal,
16767 DAG.getConstant(-1ULL, OtherVal.getValueType()), NewCmp);
16768 return DAG.getNode(N->getOpcode() == ISD::SUB ? X86ISD::SBB : X86ISD::ADC,
16769 DL, OtherVal.getValueType(), OtherVal,
16770 DAG.getConstant(0, OtherVal.getValueType()), NewCmp);
16771}
Chris Lattnerc19d1c32010-12-19 22:08:31 +000016772
Craig Topper54f952a2011-11-19 09:02:40 +000016773/// PerformADDCombine - Do target-specific dag combines on integer adds.
16774static SDValue PerformAddCombine(SDNode *N, SelectionDAG &DAG,
16775 const X86Subtarget *Subtarget) {
16776 EVT VT = N->getValueType(0);
16777 SDValue Op0 = N->getOperand(0);
16778 SDValue Op1 = N->getOperand(1);
16779
16780 // Try to synthesize horizontal adds from adds of shuffles.
Craig Topperd0a31172012-01-10 06:37:29 +000016781 if (((Subtarget->hasSSSE3() && (VT == MVT::v8i16 || VT == MVT::v4i32)) ||
Craig Toppere6cf4a02012-01-13 05:04:25 +000016782 (Subtarget->hasAVX2() && (VT == MVT::v16i16 || VT == MVT::v8i32))) &&
Craig Topper54f952a2011-11-19 09:02:40 +000016783 isHorizontalBinOp(Op0, Op1, true))
16784 return DAG.getNode(X86ISD::HADD, N->getDebugLoc(), VT, Op0, Op1);
16785
16786 return OptimizeConditionalInDecrement(N, DAG);
16787}
16788
16789static SDValue PerformSubCombine(SDNode *N, SelectionDAG &DAG,
16790 const X86Subtarget *Subtarget) {
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000016791 SDValue Op0 = N->getOperand(0);
16792 SDValue Op1 = N->getOperand(1);
16793
16794 // X86 can't encode an immediate LHS of a sub. See if we can push the
16795 // negation into a preceding instruction.
16796 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op0)) {
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000016797 // If the RHS of the sub is a XOR with one use and a constant, invert the
16798 // immediate. Then add one to the LHS of the sub so we can turn
16799 // X-Y -> X+~Y+1, saving one register.
16800 if (Op1->hasOneUse() && Op1.getOpcode() == ISD::XOR &&
16801 isa<ConstantSDNode>(Op1.getOperand(1))) {
Nick Lewycky726ebd62011-08-23 19:01:24 +000016802 APInt XorC = cast<ConstantSDNode>(Op1.getOperand(1))->getAPIntValue();
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000016803 EVT VT = Op0.getValueType();
16804 SDValue NewXor = DAG.getNode(ISD::XOR, Op1.getDebugLoc(), VT,
16805 Op1.getOperand(0),
16806 DAG.getConstant(~XorC, VT));
16807 return DAG.getNode(ISD::ADD, N->getDebugLoc(), VT, NewXor,
Nick Lewycky726ebd62011-08-23 19:01:24 +000016808 DAG.getConstant(C->getAPIntValue()+1, VT));
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000016809 }
16810 }
16811
Craig Topper54f952a2011-11-19 09:02:40 +000016812 // Try to synthesize horizontal adds from adds of shuffles.
16813 EVT VT = N->getValueType(0);
Craig Topperd0a31172012-01-10 06:37:29 +000016814 if (((Subtarget->hasSSSE3() && (VT == MVT::v8i16 || VT == MVT::v4i32)) ||
Craig Topperb72039c2011-11-30 09:10:50 +000016815 (Subtarget->hasAVX2() && (VT == MVT::v16i16 || VT == MVT::v8i32))) &&
16816 isHorizontalBinOp(Op0, Op1, true))
Craig Topper54f952a2011-11-19 09:02:40 +000016817 return DAG.getNode(X86ISD::HSUB, N->getDebugLoc(), VT, Op0, Op1);
16818
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000016819 return OptimizeConditionalInDecrement(N, DAG);
16820}
16821
Michael Liaod9d09602012-10-23 17:34:00 +000016822/// performVZEXTCombine - Performs build vector combines
16823static SDValue performVZEXTCombine(SDNode *N, SelectionDAG &DAG,
16824 TargetLowering::DAGCombinerInfo &DCI,
16825 const X86Subtarget *Subtarget) {
16826 // (vzext (bitcast (vzext (x)) -> (vzext x)
16827 SDValue In = N->getOperand(0);
16828 while (In.getOpcode() == ISD::BITCAST)
16829 In = In.getOperand(0);
16830
16831 if (In.getOpcode() != X86ISD::VZEXT)
16832 return SDValue();
16833
16834 return DAG.getNode(X86ISD::VZEXT, N->getDebugLoc(), N->getValueType(0), In.getOperand(0));
16835}
16836
Dan Gohman475871a2008-07-27 21:46:04 +000016837SDValue X86TargetLowering::PerformDAGCombine(SDNode *N,
Evan Cheng9dd93b32008-11-05 06:03:38 +000016838 DAGCombinerInfo &DCI) const {
Evan Cheng206ee9d2006-07-07 08:33:52 +000016839 SelectionDAG &DAG = DCI.DAG;
16840 switch (N->getOpcode()) {
16841 default: break;
Dan Gohman1bbf72b2010-03-15 23:23:03 +000016842 case ISD::EXTRACT_VECTOR_ELT:
Craig Topper89f4e662012-03-20 07:17:59 +000016843 return PerformEXTRACT_VECTOR_ELTCombine(N, DAG, DCI);
Duncan Sands6bcd2192011-09-17 16:49:39 +000016844 case ISD::VSELECT:
Nadav Rotemcc616562012-01-15 19:27:55 +000016845 case ISD::SELECT: return PerformSELECTCombine(N, DAG, DCI, Subtarget);
Michael Liaodbf8b5b2012-08-28 03:34:40 +000016846 case X86ISD::CMOV: return PerformCMOVCombine(N, DAG, DCI, Subtarget);
Craig Topper54f952a2011-11-19 09:02:40 +000016847 case ISD::ADD: return PerformAddCombine(N, DAG, Subtarget);
16848 case ISD::SUB: return PerformSubCombine(N, DAG, Subtarget);
Chris Lattner23a01992010-12-20 01:37:09 +000016849 case X86ISD::ADC: return PerformADCCombine(N, DAG, DCI);
Evan Cheng0b0cd912009-03-28 05:57:29 +000016850 case ISD::MUL: return PerformMulCombine(N, DAG, DCI);
Nate Begeman740ab032009-01-26 00:52:55 +000016851 case ISD::SHL:
16852 case ISD::SRA:
Mon P Wang845b1892012-02-01 22:15:20 +000016853 case ISD::SRL: return PerformShiftCombine(N, DAG, DCI, Subtarget);
Nate Begemanb65c1752010-12-17 22:55:37 +000016854 case ISD::AND: return PerformAndCombine(N, DAG, DCI, Subtarget);
Evan Cheng8b1190a2010-04-28 01:18:01 +000016855 case ISD::OR: return PerformOrCombine(N, DAG, DCI, Subtarget);
Craig Topperb4c94572011-10-21 06:55:01 +000016856 case ISD::XOR: return PerformXorCombine(N, DAG, DCI, Subtarget);
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000016857 case ISD::LOAD: return PerformLOADCombine(N, DAG, DCI, Subtarget);
Evan Cheng7e2ff772008-05-08 00:57:18 +000016858 case ISD::STORE: return PerformSTORECombine(N, DAG, Subtarget);
Stuart Hastingsf99a4b82011-06-06 23:15:58 +000016859 case ISD::SINT_TO_FP: return PerformSINT_TO_FPCombine(N, DAG, this);
Duncan Sands17470be2011-09-22 20:15:48 +000016860 case ISD::FADD: return PerformFADDCombine(N, DAG, Subtarget);
16861 case ISD::FSUB: return PerformFSUBCombine(N, DAG, Subtarget);
Chris Lattner6cf73262008-01-25 06:14:17 +000016862 case X86ISD::FXOR:
Chris Lattneraf723b92008-01-25 05:46:26 +000016863 case X86ISD::FOR: return PerformFORCombine(N, DAG);
Nadav Rotemd60cb112012-08-19 13:06:16 +000016864 case X86ISD::FMIN:
16865 case X86ISD::FMAX: return PerformFMinFMaxCombine(N, DAG);
Chris Lattneraf723b92008-01-25 05:46:26 +000016866 case X86ISD::FAND: return PerformFANDCombine(N, DAG);
Dan Gohmane5af2d32009-01-29 01:59:02 +000016867 case X86ISD::BT: return PerformBTCombine(N, DAG, DCI);
Eli Friedman7a5e5552009-06-07 06:52:44 +000016868 case X86ISD::VZEXT_MOVL: return PerformVZEXT_MOVLCombine(N, DAG);
Elena Demikhovsky1da58672012-04-22 09:39:03 +000016869 case ISD::ANY_EXTEND:
Craig Topperc16f8512012-04-25 06:39:39 +000016870 case ISD::ZERO_EXTEND: return PerformZExtCombine(N, DAG, DCI, Subtarget);
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +000016871 case ISD::SIGN_EXTEND: return PerformSExtCombine(N, DAG, DCI, Subtarget);
Craig Topper55b24052012-09-11 06:15:32 +000016872 case ISD::TRUNCATE: return PerformTruncateCombine(N, DAG,DCI,Subtarget);
Chad Rosiera73b6fc2012-04-27 22:33:25 +000016873 case ISD::SETCC: return PerformISDSETCCCombine(N, DAG);
Michael Liaodbf8b5b2012-08-28 03:34:40 +000016874 case X86ISD::SETCC: return PerformSETCCCombine(N, DAG, DCI, Subtarget);
Michael Liao2a33cec2012-08-10 19:58:13 +000016875 case X86ISD::BRCOND: return PerformBrCondCombine(N, DAG, DCI, Subtarget);
Michael Liaod9d09602012-10-23 17:34:00 +000016876 case X86ISD::VZEXT: return performVZEXTCombine(N, DAG, DCI, Subtarget);
Craig Topperb3982da2011-12-31 23:50:21 +000016877 case X86ISD::SHUFP: // Handle all target specific shuffles
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +000016878 case X86ISD::PALIGN:
Craig Topper34671b82011-12-06 08:21:25 +000016879 case X86ISD::UNPCKH:
16880 case X86ISD::UNPCKL:
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000016881 case X86ISD::MOVHLPS:
16882 case X86ISD::MOVLHPS:
16883 case X86ISD::PSHUFD:
16884 case X86ISD::PSHUFHW:
16885 case X86ISD::PSHUFLW:
16886 case X86ISD::MOVSS:
16887 case X86ISD::MOVSD:
Craig Topper316cd2a2011-11-30 06:25:25 +000016888 case X86ISD::VPERMILP:
Craig Topperec24e612011-11-30 07:47:51 +000016889 case X86ISD::VPERM2X128:
Bruno Cardoso Lopes50b37c72011-08-15 21:45:54 +000016890 case ISD::VECTOR_SHUFFLE: return PerformShuffleCombine(N, DAG, DCI,Subtarget);
Elena Demikhovsky1503aba2012-08-01 12:06:00 +000016891 case ISD::FMA: return PerformFMACombine(N, DAG, Subtarget);
Evan Cheng206ee9d2006-07-07 08:33:52 +000016892 }
16893
Dan Gohman475871a2008-07-27 21:46:04 +000016894 return SDValue();
Evan Cheng206ee9d2006-07-07 08:33:52 +000016895}
16896
Evan Chenge5b51ac2010-04-17 06:13:15 +000016897/// isTypeDesirableForOp - Return true if the target has native support for
16898/// the specified value type and it is 'desirable' to use the type for the
16899/// given node type. e.g. On x86 i16 is legal, but undesirable since i16
16900/// instruction encodings are longer and some i16 instructions are slow.
16901bool X86TargetLowering::isTypeDesirableForOp(unsigned Opc, EVT VT) const {
16902 if (!isTypeLegal(VT))
16903 return false;
Evan Cheng2bce5f4b2010-04-28 08:30:49 +000016904 if (VT != MVT::i16)
Evan Chenge5b51ac2010-04-17 06:13:15 +000016905 return true;
16906
16907 switch (Opc) {
16908 default:
16909 return true;
Evan Cheng4c26e932010-04-19 19:29:22 +000016910 case ISD::LOAD:
16911 case ISD::SIGN_EXTEND:
16912 case ISD::ZERO_EXTEND:
16913 case ISD::ANY_EXTEND:
Evan Chenge5b51ac2010-04-17 06:13:15 +000016914 case ISD::SHL:
Evan Chenge5b51ac2010-04-17 06:13:15 +000016915 case ISD::SRL:
16916 case ISD::SUB:
16917 case ISD::ADD:
16918 case ISD::MUL:
16919 case ISD::AND:
16920 case ISD::OR:
16921 case ISD::XOR:
16922 return false;
16923 }
16924}
16925
16926/// IsDesirableToPromoteOp - This method query the target whether it is
Evan Cheng64b7bf72010-04-16 06:14:10 +000016927/// beneficial for dag combiner to promote the specified node. If true, it
16928/// should return the desired promotion type by reference.
Evan Chenge5b51ac2010-04-17 06:13:15 +000016929bool X86TargetLowering::IsDesirableToPromoteOp(SDValue Op, EVT &PVT) const {
Evan Cheng64b7bf72010-04-16 06:14:10 +000016930 EVT VT = Op.getValueType();
16931 if (VT != MVT::i16)
16932 return false;
16933
Evan Cheng4c26e932010-04-19 19:29:22 +000016934 bool Promote = false;
16935 bool Commute = false;
Evan Cheng64b7bf72010-04-16 06:14:10 +000016936 switch (Op.getOpcode()) {
Evan Cheng4c26e932010-04-19 19:29:22 +000016937 default: break;
16938 case ISD::LOAD: {
16939 LoadSDNode *LD = cast<LoadSDNode>(Op);
16940 // If the non-extending load has a single use and it's not live out, then it
16941 // might be folded.
Evan Cheng2bce5f4b2010-04-28 08:30:49 +000016942 if (LD->getExtensionType() == ISD::NON_EXTLOAD /*&&
16943 Op.hasOneUse()*/) {
16944 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
16945 UE = Op.getNode()->use_end(); UI != UE; ++UI) {
16946 // The only case where we'd want to promote LOAD (rather then it being
16947 // promoted as an operand is when it's only use is liveout.
16948 if (UI->getOpcode() != ISD::CopyToReg)
16949 return false;
16950 }
16951 }
Evan Cheng4c26e932010-04-19 19:29:22 +000016952 Promote = true;
16953 break;
16954 }
16955 case ISD::SIGN_EXTEND:
16956 case ISD::ZERO_EXTEND:
16957 case ISD::ANY_EXTEND:
16958 Promote = true;
16959 break;
Evan Chenge5b51ac2010-04-17 06:13:15 +000016960 case ISD::SHL:
Evan Cheng2bce5f4b2010-04-28 08:30:49 +000016961 case ISD::SRL: {
Evan Chenge5b51ac2010-04-17 06:13:15 +000016962 SDValue N0 = Op.getOperand(0);
16963 // Look out for (store (shl (load), x)).
Evan Chengc82c20b2010-04-24 04:44:57 +000016964 if (MayFoldLoad(N0) && MayFoldIntoStore(Op))
Evan Chenge5b51ac2010-04-17 06:13:15 +000016965 return false;
Evan Cheng4c26e932010-04-19 19:29:22 +000016966 Promote = true;
Evan Chenge5b51ac2010-04-17 06:13:15 +000016967 break;
16968 }
Evan Cheng64b7bf72010-04-16 06:14:10 +000016969 case ISD::ADD:
16970 case ISD::MUL:
16971 case ISD::AND:
16972 case ISD::OR:
Evan Cheng4c26e932010-04-19 19:29:22 +000016973 case ISD::XOR:
16974 Commute = true;
16975 // fallthrough
16976 case ISD::SUB: {
Evan Cheng64b7bf72010-04-16 06:14:10 +000016977 SDValue N0 = Op.getOperand(0);
16978 SDValue N1 = Op.getOperand(1);
Evan Chengc82c20b2010-04-24 04:44:57 +000016979 if (!Commute && MayFoldLoad(N1))
Evan Cheng64b7bf72010-04-16 06:14:10 +000016980 return false;
16981 // Avoid disabling potential load folding opportunities.
Evan Chengc82c20b2010-04-24 04:44:57 +000016982 if (MayFoldLoad(N0) && (!isa<ConstantSDNode>(N1) || MayFoldIntoStore(Op)))
Evan Cheng64b7bf72010-04-16 06:14:10 +000016983 return false;
Evan Chengc82c20b2010-04-24 04:44:57 +000016984 if (MayFoldLoad(N1) && (!isa<ConstantSDNode>(N0) || MayFoldIntoStore(Op)))
Evan Cheng64b7bf72010-04-16 06:14:10 +000016985 return false;
Evan Cheng4c26e932010-04-19 19:29:22 +000016986 Promote = true;
Evan Cheng64b7bf72010-04-16 06:14:10 +000016987 }
16988 }
16989
16990 PVT = MVT::i32;
Evan Cheng4c26e932010-04-19 19:29:22 +000016991 return Promote;
Evan Cheng64b7bf72010-04-16 06:14:10 +000016992}
16993
Evan Cheng60c07e12006-07-05 22:17:51 +000016994//===----------------------------------------------------------------------===//
16995// X86 Inline Assembly Support
16996//===----------------------------------------------------------------------===//
16997
Benjamin Kramer2ea4cdb2011-12-18 19:59:20 +000016998namespace {
16999 // Helper to match a string separated by whitespace.
Benjamin Kramer0581ed72011-12-18 20:51:31 +000017000 bool matchAsmImpl(StringRef s, ArrayRef<const StringRef *> args) {
Benjamin Kramer2ea4cdb2011-12-18 19:59:20 +000017001 s = s.substr(s.find_first_not_of(" \t")); // Skip leading whitespace.
Benjamin Kramere6cddb72011-12-17 14:36:05 +000017002
Benjamin Kramer0581ed72011-12-18 20:51:31 +000017003 for (unsigned i = 0, e = args.size(); i != e; ++i) {
Benjamin Kramer2ea4cdb2011-12-18 19:59:20 +000017004 StringRef piece(*args[i]);
17005 if (!s.startswith(piece)) // Check if the piece matches.
17006 return false;
17007
17008 s = s.substr(piece.size());
17009 StringRef::size_type pos = s.find_first_not_of(" \t");
17010 if (pos == 0) // We matched a prefix.
17011 return false;
17012
17013 s = s.substr(pos);
Benjamin Kramere6cddb72011-12-17 14:36:05 +000017014 }
17015
Benjamin Kramer2ea4cdb2011-12-18 19:59:20 +000017016 return s.empty();
Benjamin Kramere6cddb72011-12-17 14:36:05 +000017017 }
Benjamin Kramer0581ed72011-12-18 20:51:31 +000017018 const VariadicFunction1<bool, StringRef, StringRef, matchAsmImpl> matchAsm={};
Benjamin Kramere6cddb72011-12-17 14:36:05 +000017019}
17020
Chris Lattnerb8105652009-07-20 17:51:36 +000017021bool X86TargetLowering::ExpandInlineAsm(CallInst *CI) const {
17022 InlineAsm *IA = cast<InlineAsm>(CI->getCalledValue());
Chris Lattnerb8105652009-07-20 17:51:36 +000017023
17024 std::string AsmStr = IA->getAsmString();
17025
Benjamin Kramere6cddb72011-12-17 14:36:05 +000017026 IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
17027 if (!Ty || Ty->getBitWidth() % 16 != 0)
17028 return false;
17029
Chris Lattnerb8105652009-07-20 17:51:36 +000017030 // TODO: should remove alternatives from the asmstring: "foo {a|b}" -> "foo a"
Benjamin Kramerd4f19592010-01-11 18:03:24 +000017031 SmallVector<StringRef, 4> AsmPieces;
Peter Collingbourne98361182010-11-13 19:54:23 +000017032 SplitString(AsmStr, AsmPieces, ";\n");
Chris Lattnerb8105652009-07-20 17:51:36 +000017033
17034 switch (AsmPieces.size()) {
17035 default: return false;
17036 case 1:
Chris Lattner7a2bdde2011-04-15 05:18:47 +000017037 // FIXME: this should verify that we are targeting a 486 or better. If not,
Benjamin Kramere6cddb72011-12-17 14:36:05 +000017038 // we will turn this bswap into something that will be lowered to logical
17039 // ops instead of emitting the bswap asm. For now, we don't support 486 or
17040 // lower so don't worry about this.
Chris Lattnerb8105652009-07-20 17:51:36 +000017041 // bswap $0
Benjamin Kramer2ea4cdb2011-12-18 19:59:20 +000017042 if (matchAsm(AsmPieces[0], "bswap", "$0") ||
17043 matchAsm(AsmPieces[0], "bswapl", "$0") ||
17044 matchAsm(AsmPieces[0], "bswapq", "$0") ||
17045 matchAsm(AsmPieces[0], "bswap", "${0:q}") ||
17046 matchAsm(AsmPieces[0], "bswapl", "${0:q}") ||
17047 matchAsm(AsmPieces[0], "bswapq", "${0:q}")) {
Chris Lattnerb8105652009-07-20 17:51:36 +000017048 // No need to check constraints, nothing other than the equivalent of
17049 // "=r,0" would be valid here.
Evan Cheng55d42002011-01-08 01:24:27 +000017050 return IntrinsicLowering::LowerToByteSwap(CI);
Chris Lattnerb8105652009-07-20 17:51:36 +000017051 }
Benjamin Kramere6cddb72011-12-17 14:36:05 +000017052
Chris Lattnerb8105652009-07-20 17:51:36 +000017053 // rorw $$8, ${0:w} --> llvm.bswap.i16
Duncan Sandsb0bc6c32010-02-15 16:12:20 +000017054 if (CI->getType()->isIntegerTy(16) &&
Benjamin Kramere6cddb72011-12-17 14:36:05 +000017055 IA->getConstraintString().compare(0, 5, "=r,0,") == 0 &&
Benjamin Kramer2ea4cdb2011-12-18 19:59:20 +000017056 (matchAsm(AsmPieces[0], "rorw", "$$8,", "${0:w}") ||
17057 matchAsm(AsmPieces[0], "rolw", "$$8,", "${0:w}"))) {
Dan Gohman0ef701e2010-03-04 19:58:08 +000017058 AsmPieces.clear();
Evan Cheng55d42002011-01-08 01:24:27 +000017059 const std::string &ConstraintsStr = IA->getConstraintString();
17060 SplitString(StringRef(ConstraintsStr).substr(5), AsmPieces, ",");
Dan Gohman0ef701e2010-03-04 19:58:08 +000017061 std::sort(AsmPieces.begin(), AsmPieces.end());
17062 if (AsmPieces.size() == 4 &&
17063 AsmPieces[0] == "~{cc}" &&
17064 AsmPieces[1] == "~{dirflag}" &&
17065 AsmPieces[2] == "~{flags}" &&
Benjamin Kramere6cddb72011-12-17 14:36:05 +000017066 AsmPieces[3] == "~{fpsr}")
17067 return IntrinsicLowering::LowerToByteSwap(CI);
Chris Lattnerb8105652009-07-20 17:51:36 +000017068 }
17069 break;
17070 case 3:
Peter Collingbourne948cf022010-11-13 19:54:30 +000017071 if (CI->getType()->isIntegerTy(32) &&
Benjamin Kramere6cddb72011-12-17 14:36:05 +000017072 IA->getConstraintString().compare(0, 5, "=r,0,") == 0 &&
Benjamin Kramer2ea4cdb2011-12-18 19:59:20 +000017073 matchAsm(AsmPieces[0], "rorw", "$$8,", "${0:w}") &&
17074 matchAsm(AsmPieces[1], "rorl", "$$16,", "$0") &&
17075 matchAsm(AsmPieces[2], "rorw", "$$8,", "${0:w}")) {
Benjamin Kramere6cddb72011-12-17 14:36:05 +000017076 AsmPieces.clear();
17077 const std::string &ConstraintsStr = IA->getConstraintString();
17078 SplitString(StringRef(ConstraintsStr).substr(5), AsmPieces, ",");
17079 std::sort(AsmPieces.begin(), AsmPieces.end());
17080 if (AsmPieces.size() == 4 &&
17081 AsmPieces[0] == "~{cc}" &&
17082 AsmPieces[1] == "~{dirflag}" &&
17083 AsmPieces[2] == "~{flags}" &&
17084 AsmPieces[3] == "~{fpsr}")
17085 return IntrinsicLowering::LowerToByteSwap(CI);
Peter Collingbourne948cf022010-11-13 19:54:30 +000017086 }
Evan Cheng55d42002011-01-08 01:24:27 +000017087
17088 if (CI->getType()->isIntegerTy(64)) {
17089 InlineAsm::ConstraintInfoVector Constraints = IA->ParseConstraints();
17090 if (Constraints.size() >= 2 &&
17091 Constraints[0].Codes.size() == 1 && Constraints[0].Codes[0] == "A" &&
17092 Constraints[1].Codes.size() == 1 && Constraints[1].Codes[0] == "0") {
17093 // bswap %eax / bswap %edx / xchgl %eax, %edx -> llvm.bswap.i64
Benjamin Kramer2ea4cdb2011-12-18 19:59:20 +000017094 if (matchAsm(AsmPieces[0], "bswap", "%eax") &&
17095 matchAsm(AsmPieces[1], "bswap", "%edx") &&
17096 matchAsm(AsmPieces[2], "xchgl", "%eax,", "%edx"))
Benjamin Kramere6cddb72011-12-17 14:36:05 +000017097 return IntrinsicLowering::LowerToByteSwap(CI);
Chris Lattnerb8105652009-07-20 17:51:36 +000017098 }
17099 }
17100 break;
17101 }
17102 return false;
17103}
17104
17105
17106
Chris Lattnerf4dff842006-07-11 02:54:03 +000017107/// getConstraintType - Given a constraint letter, return the type of
17108/// constraint it is for this target.
17109X86TargetLowering::ConstraintType
Chris Lattner4234f572007-03-25 02:14:49 +000017110X86TargetLowering::getConstraintType(const std::string &Constraint) const {
17111 if (Constraint.size() == 1) {
17112 switch (Constraint[0]) {
Chris Lattner4234f572007-03-25 02:14:49 +000017113 case 'R':
Chris Lattner4234f572007-03-25 02:14:49 +000017114 case 'q':
17115 case 'Q':
John Thompson44ab89e2010-10-29 17:29:13 +000017116 case 'f':
17117 case 't':
17118 case 'u':
Dale Johannesen2ffbcac2008-04-01 00:57:48 +000017119 case 'y':
John Thompson44ab89e2010-10-29 17:29:13 +000017120 case 'x':
Chris Lattner4234f572007-03-25 02:14:49 +000017121 case 'Y':
Eric Christopher31b5f002011-07-07 22:29:07 +000017122 case 'l':
Chris Lattner4234f572007-03-25 02:14:49 +000017123 return C_RegisterClass;
John Thompson44ab89e2010-10-29 17:29:13 +000017124 case 'a':
17125 case 'b':
17126 case 'c':
17127 case 'd':
17128 case 'S':
17129 case 'D':
17130 case 'A':
17131 return C_Register;
17132 case 'I':
17133 case 'J':
17134 case 'K':
17135 case 'L':
17136 case 'M':
17137 case 'N':
17138 case 'G':
17139 case 'C':
Dale Johannesen78e3e522009-02-12 20:58:09 +000017140 case 'e':
17141 case 'Z':
17142 return C_Other;
Chris Lattner4234f572007-03-25 02:14:49 +000017143 default:
17144 break;
17145 }
Chris Lattnerf4dff842006-07-11 02:54:03 +000017146 }
Chris Lattner4234f572007-03-25 02:14:49 +000017147 return TargetLowering::getConstraintType(Constraint);
Chris Lattnerf4dff842006-07-11 02:54:03 +000017148}
17149
John Thompson44ab89e2010-10-29 17:29:13 +000017150/// Examine constraint type and operand type and determine a weight value.
John Thompsoneac6e1d2010-09-13 18:15:37 +000017151/// This object must already have been set up with the operand type
17152/// and the current alternative constraint selected.
John Thompson44ab89e2010-10-29 17:29:13 +000017153TargetLowering::ConstraintWeight
17154 X86TargetLowering::getSingleConstraintMatchWeight(
John Thompsoneac6e1d2010-09-13 18:15:37 +000017155 AsmOperandInfo &info, const char *constraint) const {
John Thompson44ab89e2010-10-29 17:29:13 +000017156 ConstraintWeight weight = CW_Invalid;
John Thompsoneac6e1d2010-09-13 18:15:37 +000017157 Value *CallOperandVal = info.CallOperandVal;
17158 // If we don't have a value, we can't do a match,
17159 // but allow it at the lowest weight.
17160 if (CallOperandVal == NULL)
John Thompson44ab89e2010-10-29 17:29:13 +000017161 return CW_Default;
Chris Lattnerdb125cf2011-07-18 04:54:35 +000017162 Type *type = CallOperandVal->getType();
John Thompsoneac6e1d2010-09-13 18:15:37 +000017163 // Look at the constraint type.
17164 switch (*constraint) {
17165 default:
John Thompson44ab89e2010-10-29 17:29:13 +000017166 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
17167 case 'R':
17168 case 'q':
17169 case 'Q':
17170 case 'a':
17171 case 'b':
17172 case 'c':
17173 case 'd':
17174 case 'S':
17175 case 'D':
17176 case 'A':
17177 if (CallOperandVal->getType()->isIntegerTy())
17178 weight = CW_SpecificReg;
17179 break;
17180 case 'f':
17181 case 't':
17182 case 'u':
17183 if (type->isFloatingPointTy())
17184 weight = CW_SpecificReg;
17185 break;
17186 case 'y':
Chris Lattner2a786eb2010-12-19 20:19:20 +000017187 if (type->isX86_MMXTy() && Subtarget->hasMMX())
John Thompson44ab89e2010-10-29 17:29:13 +000017188 weight = CW_SpecificReg;
17189 break;
17190 case 'x':
17191 case 'Y':
Craig Topper1accb7e2012-01-10 06:54:16 +000017192 if (((type->getPrimitiveSizeInBits() == 128) && Subtarget->hasSSE1()) ||
Eric Christopher55487552012-01-07 01:02:09 +000017193 ((type->getPrimitiveSizeInBits() == 256) && Subtarget->hasAVX()))
John Thompson44ab89e2010-10-29 17:29:13 +000017194 weight = CW_Register;
John Thompsoneac6e1d2010-09-13 18:15:37 +000017195 break;
17196 case 'I':
17197 if (ConstantInt *C = dyn_cast<ConstantInt>(info.CallOperandVal)) {
17198 if (C->getZExtValue() <= 31)
John Thompson44ab89e2010-10-29 17:29:13 +000017199 weight = CW_Constant;
John Thompsoneac6e1d2010-09-13 18:15:37 +000017200 }
17201 break;
John Thompson44ab89e2010-10-29 17:29:13 +000017202 case 'J':
17203 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
17204 if (C->getZExtValue() <= 63)
17205 weight = CW_Constant;
17206 }
17207 break;
17208 case 'K':
17209 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
17210 if ((C->getSExtValue() >= -0x80) && (C->getSExtValue() <= 0x7f))
17211 weight = CW_Constant;
17212 }
17213 break;
17214 case 'L':
17215 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
17216 if ((C->getZExtValue() == 0xff) || (C->getZExtValue() == 0xffff))
17217 weight = CW_Constant;
17218 }
17219 break;
17220 case 'M':
17221 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
17222 if (C->getZExtValue() <= 3)
17223 weight = CW_Constant;
17224 }
17225 break;
17226 case 'N':
17227 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
17228 if (C->getZExtValue() <= 0xff)
17229 weight = CW_Constant;
17230 }
17231 break;
17232 case 'G':
17233 case 'C':
17234 if (dyn_cast<ConstantFP>(CallOperandVal)) {
17235 weight = CW_Constant;
17236 }
17237 break;
17238 case 'e':
17239 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
17240 if ((C->getSExtValue() >= -0x80000000LL) &&
17241 (C->getSExtValue() <= 0x7fffffffLL))
17242 weight = CW_Constant;
17243 }
17244 break;
17245 case 'Z':
17246 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
17247 if (C->getZExtValue() <= 0xffffffff)
17248 weight = CW_Constant;
17249 }
17250 break;
John Thompsoneac6e1d2010-09-13 18:15:37 +000017251 }
17252 return weight;
17253}
17254
Dale Johannesenba2a0b92008-01-29 02:21:21 +000017255/// LowerXConstraint - try to replace an X constraint, which matches anything,
17256/// with another that has more specific requirements based on the type of the
17257/// corresponding operand.
Chris Lattner5e764232008-04-26 23:02:14 +000017258const char *X86TargetLowering::
Owen Andersone50ed302009-08-10 22:56:29 +000017259LowerXConstraint(EVT ConstraintVT) const {
Chris Lattner5e764232008-04-26 23:02:14 +000017260 // FP X constraints get lowered to SSE1/2 registers if available, otherwise
17261 // 'f' like normal targets.
Duncan Sands83ec4b62008-06-06 12:08:01 +000017262 if (ConstraintVT.isFloatingPoint()) {
Craig Topper1accb7e2012-01-10 06:54:16 +000017263 if (Subtarget->hasSSE2())
Chris Lattner5e764232008-04-26 23:02:14 +000017264 return "Y";
Craig Topper1accb7e2012-01-10 06:54:16 +000017265 if (Subtarget->hasSSE1())
Chris Lattner5e764232008-04-26 23:02:14 +000017266 return "x";
17267 }
Scott Michelfdc40a02009-02-17 22:15:04 +000017268
Chris Lattner5e764232008-04-26 23:02:14 +000017269 return TargetLowering::LowerXConstraint(ConstraintVT);
Dale Johannesenba2a0b92008-01-29 02:21:21 +000017270}
17271
Chris Lattner48884cd2007-08-25 00:47:38 +000017272/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
17273/// vector. If it is invalid, don't add anything to Ops.
Dan Gohman475871a2008-07-27 21:46:04 +000017274void X86TargetLowering::LowerAsmOperandForConstraint(SDValue Op,
Eric Christopher100c8332011-06-02 23:16:42 +000017275 std::string &Constraint,
Dan Gohman475871a2008-07-27 21:46:04 +000017276 std::vector<SDValue>&Ops,
Chris Lattner5e764232008-04-26 23:02:14 +000017277 SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +000017278 SDValue Result(0, 0);
Scott Michelfdc40a02009-02-17 22:15:04 +000017279
Eric Christopher100c8332011-06-02 23:16:42 +000017280 // Only support length 1 constraints for now.
17281 if (Constraint.length() > 1) return;
Eric Christopher471e4222011-06-08 23:55:35 +000017282
Eric Christopher100c8332011-06-02 23:16:42 +000017283 char ConstraintLetter = Constraint[0];
17284 switch (ConstraintLetter) {
Chris Lattner22aaf1d2006-10-31 20:13:11 +000017285 default: break;
Devang Patel84f7fd22007-03-17 00:13:28 +000017286 case 'I':
Chris Lattner188b9fe2007-03-25 01:57:35 +000017287 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +000017288 if (C->getZExtValue() <= 31) {
17289 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattner48884cd2007-08-25 00:47:38 +000017290 break;
17291 }
Devang Patel84f7fd22007-03-17 00:13:28 +000017292 }
Chris Lattner48884cd2007-08-25 00:47:38 +000017293 return;
Evan Cheng364091e2008-09-22 23:57:37 +000017294 case 'J':
17295 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattner2e06dd22009-06-15 04:39:05 +000017296 if (C->getZExtValue() <= 63) {
Chris Lattnere4935152009-06-15 04:01:39 +000017297 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
17298 break;
17299 }
17300 }
17301 return;
17302 case 'K':
17303 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Jakub Staszakdccd7f92012-11-06 23:52:19 +000017304 if (isInt<8>(C->getSExtValue())) {
Evan Cheng364091e2008-09-22 23:57:37 +000017305 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
17306 break;
17307 }
17308 }
17309 return;
Chris Lattner188b9fe2007-03-25 01:57:35 +000017310 case 'N':
17311 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +000017312 if (C->getZExtValue() <= 255) {
17313 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattner48884cd2007-08-25 00:47:38 +000017314 break;
17315 }
Chris Lattner188b9fe2007-03-25 01:57:35 +000017316 }
Chris Lattner48884cd2007-08-25 00:47:38 +000017317 return;
Dale Johannesen78e3e522009-02-12 20:58:09 +000017318 case 'e': {
17319 // 32-bit signed value
17320 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohman7720cb32010-06-18 14:01:07 +000017321 if (ConstantInt::isValueValidForType(Type::getInt32Ty(*DAG.getContext()),
17322 C->getSExtValue())) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000017323 // Widen to 64 bits here to get it sign extended.
Owen Anderson825b72b2009-08-11 20:47:22 +000017324 Result = DAG.getTargetConstant(C->getSExtValue(), MVT::i64);
Dale Johannesen78e3e522009-02-12 20:58:09 +000017325 break;
17326 }
17327 // FIXME gcc accepts some relocatable values here too, but only in certain
17328 // memory models; it's complicated.
17329 }
17330 return;
17331 }
17332 case 'Z': {
17333 // 32-bit unsigned value
17334 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohman7720cb32010-06-18 14:01:07 +000017335 if (ConstantInt::isValueValidForType(Type::getInt32Ty(*DAG.getContext()),
17336 C->getZExtValue())) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000017337 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
17338 break;
17339 }
17340 }
17341 // FIXME gcc accepts some relocatable values here too, but only in certain
17342 // memory models; it's complicated.
17343 return;
17344 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000017345 case 'i': {
Chris Lattner22aaf1d2006-10-31 20:13:11 +000017346 // Literal immediates are always ok.
Chris Lattner48884cd2007-08-25 00:47:38 +000017347 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Op)) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000017348 // Widen to 64 bits here to get it sign extended.
Owen Anderson825b72b2009-08-11 20:47:22 +000017349 Result = DAG.getTargetConstant(CST->getSExtValue(), MVT::i64);
Chris Lattner48884cd2007-08-25 00:47:38 +000017350 break;
17351 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000017352
Dale Johannesene5ff9ef2010-06-24 20:14:51 +000017353 // In any sort of PIC mode addresses need to be computed at runtime by
17354 // adding in a register or some sort of table lookup. These can't
17355 // be used as immediates.
Dale Johannesene2b448c2010-07-06 23:27:00 +000017356 if (Subtarget->isPICStyleGOT() || Subtarget->isPICStyleStubPIC())
Dale Johannesene5ff9ef2010-06-24 20:14:51 +000017357 return;
17358
Chris Lattnerdc43a882007-05-03 16:52:29 +000017359 // If we are in non-pic codegen mode, we allow the address of a global (with
17360 // an optional displacement) to be used with 'i'.
Chris Lattner49921962009-05-08 18:23:14 +000017361 GlobalAddressSDNode *GA = 0;
Chris Lattnerdc43a882007-05-03 16:52:29 +000017362 int64_t Offset = 0;
Scott Michelfdc40a02009-02-17 22:15:04 +000017363
Chris Lattner49921962009-05-08 18:23:14 +000017364 // Match either (GA), (GA+C), (GA+C1+C2), etc.
17365 while (1) {
17366 if ((GA = dyn_cast<GlobalAddressSDNode>(Op))) {
17367 Offset += GA->getOffset();
17368 break;
17369 } else if (Op.getOpcode() == ISD::ADD) {
17370 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
17371 Offset += C->getZExtValue();
17372 Op = Op.getOperand(0);
17373 continue;
17374 }
17375 } else if (Op.getOpcode() == ISD::SUB) {
17376 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
17377 Offset += -C->getZExtValue();
17378 Op = Op.getOperand(0);
17379 continue;
17380 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000017381 }
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000017382
Chris Lattner49921962009-05-08 18:23:14 +000017383 // Otherwise, this isn't something we can handle, reject it.
17384 return;
Chris Lattnerdc43a882007-05-03 16:52:29 +000017385 }
Eric Christopherfd179292009-08-27 18:07:15 +000017386
Dan Gohman46510a72010-04-15 01:51:59 +000017387 const GlobalValue *GV = GA->getGlobal();
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000017388 // If we require an extra load to get this address, as in PIC mode, we
17389 // can't accept it.
Chris Lattner36c25012009-07-10 07:34:39 +000017390 if (isGlobalStubReference(Subtarget->ClassifyGlobalReference(GV,
17391 getTargetMachine())))
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000017392 return;
Scott Michelfdc40a02009-02-17 22:15:04 +000017393
Devang Patel0d881da2010-07-06 22:08:15 +000017394 Result = DAG.getTargetGlobalAddress(GV, Op.getDebugLoc(),
17395 GA->getValueType(0), Offset);
Chris Lattner49921962009-05-08 18:23:14 +000017396 break;
Chris Lattner22aaf1d2006-10-31 20:13:11 +000017397 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000017398 }
Scott Michelfdc40a02009-02-17 22:15:04 +000017399
Gabor Greifba36cb52008-08-28 21:40:38 +000017400 if (Result.getNode()) {
Chris Lattner48884cd2007-08-25 00:47:38 +000017401 Ops.push_back(Result);
17402 return;
17403 }
Dale Johannesen1784d162010-06-25 21:55:36 +000017404 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
Chris Lattner22aaf1d2006-10-31 20:13:11 +000017405}
17406
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000017407std::pair<unsigned, const TargetRegisterClass*>
Chris Lattnerf76d1802006-07-31 23:26:50 +000017408X86TargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +000017409 EVT VT) const {
Chris Lattnerad043e82007-04-09 05:11:28 +000017410 // First, see if this is a constraint that directly corresponds to an LLVM
17411 // register class.
17412 if (Constraint.size() == 1) {
17413 // GCC Constraint Letters
17414 switch (Constraint[0]) {
17415 default: break;
Eric Christopherd176af82011-06-29 17:23:50 +000017416 // TODO: Slight differences here in allocation order and leaving
17417 // RIP in the class. Do they matter any more here than they do
17418 // in the normal allocation?
17419 case 'q': // GENERAL_REGS in 64-bit mode, Q_REGS in 32-bit mode.
17420 if (Subtarget->is64Bit()) {
Craig Topperc9099502012-04-20 06:31:50 +000017421 if (VT == MVT::i32 || VT == MVT::f32)
17422 return std::make_pair(0U, &X86::GR32RegClass);
17423 if (VT == MVT::i16)
17424 return std::make_pair(0U, &X86::GR16RegClass);
17425 if (VT == MVT::i8 || VT == MVT::i1)
17426 return std::make_pair(0U, &X86::GR8RegClass);
17427 if (VT == MVT::i64 || VT == MVT::f64)
17428 return std::make_pair(0U, &X86::GR64RegClass);
17429 break;
Eric Christopherd176af82011-06-29 17:23:50 +000017430 }
17431 // 32-bit fallthrough
17432 case 'Q': // Q_REGS
Nick Lewycky9bf45d02011-07-08 00:19:27 +000017433 if (VT == MVT::i32 || VT == MVT::f32)
Craig Topperc9099502012-04-20 06:31:50 +000017434 return std::make_pair(0U, &X86::GR32_ABCDRegClass);
17435 if (VT == MVT::i16)
17436 return std::make_pair(0U, &X86::GR16_ABCDRegClass);
17437 if (VT == MVT::i8 || VT == MVT::i1)
17438 return std::make_pair(0U, &X86::GR8_ABCD_LRegClass);
17439 if (VT == MVT::i64)
17440 return std::make_pair(0U, &X86::GR64_ABCDRegClass);
Eric Christopherd176af82011-06-29 17:23:50 +000017441 break;
Chris Lattner0f65cad2007-04-09 05:49:22 +000017442 case 'r': // GENERAL_REGS
Chris Lattner0f65cad2007-04-09 05:49:22 +000017443 case 'l': // INDEX_REGS
Eric Christopher5427ede2011-07-14 20:13:52 +000017444 if (VT == MVT::i8 || VT == MVT::i1)
Craig Topperc9099502012-04-20 06:31:50 +000017445 return std::make_pair(0U, &X86::GR8RegClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000017446 if (VT == MVT::i16)
Craig Topperc9099502012-04-20 06:31:50 +000017447 return std::make_pair(0U, &X86::GR16RegClass);
Eric Christopher2bbecd82011-05-19 21:33:47 +000017448 if (VT == MVT::i32 || VT == MVT::f32 || !Subtarget->is64Bit())
Craig Topperc9099502012-04-20 06:31:50 +000017449 return std::make_pair(0U, &X86::GR32RegClass);
17450 return std::make_pair(0U, &X86::GR64RegClass);
Dale Johannesen5f3663e2009-10-07 22:47:20 +000017451 case 'R': // LEGACY_REGS
Eric Christopher5427ede2011-07-14 20:13:52 +000017452 if (VT == MVT::i8 || VT == MVT::i1)
Craig Topperc9099502012-04-20 06:31:50 +000017453 return std::make_pair(0U, &X86::GR8_NOREXRegClass);
Dale Johannesen5f3663e2009-10-07 22:47:20 +000017454 if (VT == MVT::i16)
Craig Topperc9099502012-04-20 06:31:50 +000017455 return std::make_pair(0U, &X86::GR16_NOREXRegClass);
Dale Johannesen5f3663e2009-10-07 22:47:20 +000017456 if (VT == MVT::i32 || !Subtarget->is64Bit())
Craig Topperc9099502012-04-20 06:31:50 +000017457 return std::make_pair(0U, &X86::GR32_NOREXRegClass);
17458 return std::make_pair(0U, &X86::GR64_NOREXRegClass);
Chris Lattnerfce84ac2008-03-11 19:06:29 +000017459 case 'f': // FP Stack registers.
17460 // If SSE is enabled for this VT, use f80 to ensure the isel moves the
17461 // value to the correct fpstack register class.
Owen Anderson825b72b2009-08-11 20:47:22 +000017462 if (VT == MVT::f32 && !isScalarFPTypeInSSEReg(VT))
Craig Topperc9099502012-04-20 06:31:50 +000017463 return std::make_pair(0U, &X86::RFP32RegClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000017464 if (VT == MVT::f64 && !isScalarFPTypeInSSEReg(VT))
Craig Topperc9099502012-04-20 06:31:50 +000017465 return std::make_pair(0U, &X86::RFP64RegClass);
17466 return std::make_pair(0U, &X86::RFP80RegClass);
Chris Lattner6c284d72007-04-12 04:14:49 +000017467 case 'y': // MMX_REGS if MMX allowed.
17468 if (!Subtarget->hasMMX()) break;
Craig Topperc9099502012-04-20 06:31:50 +000017469 return std::make_pair(0U, &X86::VR64RegClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +000017470 case 'Y': // SSE_REGS if SSE2 allowed
Craig Topper1accb7e2012-01-10 06:54:16 +000017471 if (!Subtarget->hasSSE2()) break;
Chris Lattner0f65cad2007-04-09 05:49:22 +000017472 // FALL THROUGH.
Eric Christopher55487552012-01-07 01:02:09 +000017473 case 'x': // SSE_REGS if SSE1 allowed or AVX_REGS if AVX allowed
Craig Topper1accb7e2012-01-10 06:54:16 +000017474 if (!Subtarget->hasSSE1()) break;
Duncan Sands83ec4b62008-06-06 12:08:01 +000017475
Owen Anderson825b72b2009-08-11 20:47:22 +000017476 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner0f65cad2007-04-09 05:49:22 +000017477 default: break;
17478 // Scalar SSE types.
Owen Anderson825b72b2009-08-11 20:47:22 +000017479 case MVT::f32:
17480 case MVT::i32:
Craig Topperc9099502012-04-20 06:31:50 +000017481 return std::make_pair(0U, &X86::FR32RegClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000017482 case MVT::f64:
17483 case MVT::i64:
Craig Topperc9099502012-04-20 06:31:50 +000017484 return std::make_pair(0U, &X86::FR64RegClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +000017485 // Vector types.
Owen Anderson825b72b2009-08-11 20:47:22 +000017486 case MVT::v16i8:
17487 case MVT::v8i16:
17488 case MVT::v4i32:
17489 case MVT::v2i64:
17490 case MVT::v4f32:
17491 case MVT::v2f64:
Craig Topperc9099502012-04-20 06:31:50 +000017492 return std::make_pair(0U, &X86::VR128RegClass);
Eric Christopher55487552012-01-07 01:02:09 +000017493 // AVX types.
17494 case MVT::v32i8:
17495 case MVT::v16i16:
17496 case MVT::v8i32:
17497 case MVT::v4i64:
17498 case MVT::v8f32:
17499 case MVT::v4f64:
Craig Topperc9099502012-04-20 06:31:50 +000017500 return std::make_pair(0U, &X86::VR256RegClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +000017501 }
Chris Lattnerad043e82007-04-09 05:11:28 +000017502 break;
17503 }
17504 }
Scott Michelfdc40a02009-02-17 22:15:04 +000017505
Chris Lattnerf76d1802006-07-31 23:26:50 +000017506 // Use the default implementation in TargetLowering to convert the register
17507 // constraint into a member of a register class.
17508 std::pair<unsigned, const TargetRegisterClass*> Res;
17509 Res = TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
Chris Lattner1a60aa72006-10-31 19:42:44 +000017510
17511 // Not found as a standard register?
17512 if (Res.second == 0) {
Chris Lattner56d77c72009-09-13 22:41:48 +000017513 // Map st(0) -> st(7) -> ST0
17514 if (Constraint.size() == 7 && Constraint[0] == '{' &&
17515 tolower(Constraint[1]) == 's' &&
17516 tolower(Constraint[2]) == 't' &&
17517 Constraint[3] == '(' &&
17518 (Constraint[4] >= '0' && Constraint[4] <= '7') &&
17519 Constraint[5] == ')' &&
17520 Constraint[6] == '}') {
Daniel Dunbara279bc32009-09-20 02:20:51 +000017521
Chris Lattner56d77c72009-09-13 22:41:48 +000017522 Res.first = X86::ST0+Constraint[4]-'0';
Craig Topperc9099502012-04-20 06:31:50 +000017523 Res.second = &X86::RFP80RegClass;
Chris Lattner56d77c72009-09-13 22:41:48 +000017524 return Res;
17525 }
Daniel Dunbara279bc32009-09-20 02:20:51 +000017526
Chris Lattner56d77c72009-09-13 22:41:48 +000017527 // GCC allows "st(0)" to be called just plain "st".
Benjamin Kramer05872ea2009-11-12 20:36:59 +000017528 if (StringRef("{st}").equals_lower(Constraint)) {
Chris Lattner1a60aa72006-10-31 19:42:44 +000017529 Res.first = X86::ST0;
Craig Topperc9099502012-04-20 06:31:50 +000017530 Res.second = &X86::RFP80RegClass;
Chris Lattner56d77c72009-09-13 22:41:48 +000017531 return Res;
Chris Lattner1a60aa72006-10-31 19:42:44 +000017532 }
Chris Lattner56d77c72009-09-13 22:41:48 +000017533
17534 // flags -> EFLAGS
Benjamin Kramer05872ea2009-11-12 20:36:59 +000017535 if (StringRef("{flags}").equals_lower(Constraint)) {
Chris Lattner56d77c72009-09-13 22:41:48 +000017536 Res.first = X86::EFLAGS;
Craig Topperc9099502012-04-20 06:31:50 +000017537 Res.second = &X86::CCRRegClass;
Chris Lattner56d77c72009-09-13 22:41:48 +000017538 return Res;
17539 }
Daniel Dunbara279bc32009-09-20 02:20:51 +000017540
Dale Johannesen330169f2008-11-13 21:52:36 +000017541 // 'A' means EAX + EDX.
17542 if (Constraint == "A") {
17543 Res.first = X86::EAX;
Craig Topperc9099502012-04-20 06:31:50 +000017544 Res.second = &X86::GR32_ADRegClass;
Chris Lattner56d77c72009-09-13 22:41:48 +000017545 return Res;
Dale Johannesen330169f2008-11-13 21:52:36 +000017546 }
Chris Lattner1a60aa72006-10-31 19:42:44 +000017547 return Res;
17548 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000017549
Chris Lattnerf76d1802006-07-31 23:26:50 +000017550 // Otherwise, check to see if this is a register class of the wrong value
17551 // type. For example, we want to map "{ax},i32" -> {eax}, we don't want it to
17552 // turn into {ax},{dx}.
17553 if (Res.second->hasType(VT))
17554 return Res; // Correct type already, nothing to do.
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000017555
Chris Lattnerf76d1802006-07-31 23:26:50 +000017556 // All of the single-register GCC register classes map their values onto
17557 // 16-bit register pieces "ax","dx","cx","bx","si","di","bp","sp". If we
17558 // really want an 8-bit or 32-bit register, map to the appropriate register
17559 // class and return the appropriate register.
Craig Topperc9099502012-04-20 06:31:50 +000017560 if (Res.second == &X86::GR16RegClass) {
Owen Anderson825b72b2009-08-11 20:47:22 +000017561 if (VT == MVT::i8) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000017562 unsigned DestReg = 0;
17563 switch (Res.first) {
17564 default: break;
17565 case X86::AX: DestReg = X86::AL; break;
17566 case X86::DX: DestReg = X86::DL; break;
17567 case X86::CX: DestReg = X86::CL; break;
17568 case X86::BX: DestReg = X86::BL; break;
17569 }
17570 if (DestReg) {
17571 Res.first = DestReg;
Craig Topperc9099502012-04-20 06:31:50 +000017572 Res.second = &X86::GR8RegClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000017573 }
Owen Anderson825b72b2009-08-11 20:47:22 +000017574 } else if (VT == MVT::i32) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000017575 unsigned DestReg = 0;
17576 switch (Res.first) {
17577 default: break;
17578 case X86::AX: DestReg = X86::EAX; break;
17579 case X86::DX: DestReg = X86::EDX; break;
17580 case X86::CX: DestReg = X86::ECX; break;
17581 case X86::BX: DestReg = X86::EBX; break;
17582 case X86::SI: DestReg = X86::ESI; break;
17583 case X86::DI: DestReg = X86::EDI; break;
17584 case X86::BP: DestReg = X86::EBP; break;
17585 case X86::SP: DestReg = X86::ESP; break;
17586 }
17587 if (DestReg) {
17588 Res.first = DestReg;
Craig Topperc9099502012-04-20 06:31:50 +000017589 Res.second = &X86::GR32RegClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000017590 }
Owen Anderson825b72b2009-08-11 20:47:22 +000017591 } else if (VT == MVT::i64) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000017592 unsigned DestReg = 0;
17593 switch (Res.first) {
17594 default: break;
17595 case X86::AX: DestReg = X86::RAX; break;
17596 case X86::DX: DestReg = X86::RDX; break;
17597 case X86::CX: DestReg = X86::RCX; break;
17598 case X86::BX: DestReg = X86::RBX; break;
17599 case X86::SI: DestReg = X86::RSI; break;
17600 case X86::DI: DestReg = X86::RDI; break;
17601 case X86::BP: DestReg = X86::RBP; break;
17602 case X86::SP: DestReg = X86::RSP; break;
17603 }
17604 if (DestReg) {
17605 Res.first = DestReg;
Craig Topperc9099502012-04-20 06:31:50 +000017606 Res.second = &X86::GR64RegClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000017607 }
Chris Lattnerf76d1802006-07-31 23:26:50 +000017608 }
Craig Topperc9099502012-04-20 06:31:50 +000017609 } else if (Res.second == &X86::FR32RegClass ||
17610 Res.second == &X86::FR64RegClass ||
17611 Res.second == &X86::VR128RegClass) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000017612 // Handle references to XMM physical registers that got mapped into the
17613 // wrong class. This can happen with constraints like {xmm0} where the
17614 // target independent register mapper will just pick the first match it can
17615 // find, ignoring the required type.
Eli Friedman52d418d2012-06-25 23:42:33 +000017616
17617 if (VT == MVT::f32 || VT == MVT::i32)
Craig Topperc9099502012-04-20 06:31:50 +000017618 Res.second = &X86::FR32RegClass;
Eli Friedman52d418d2012-06-25 23:42:33 +000017619 else if (VT == MVT::f64 || VT == MVT::i64)
Craig Topperc9099502012-04-20 06:31:50 +000017620 Res.second = &X86::FR64RegClass;
17621 else if (X86::VR128RegClass.hasType(VT))
17622 Res.second = &X86::VR128RegClass;
Eli Friedman52d418d2012-06-25 23:42:33 +000017623 else if (X86::VR256RegClass.hasType(VT))
17624 Res.second = &X86::VR256RegClass;
Chris Lattnerf76d1802006-07-31 23:26:50 +000017625 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000017626
Chris Lattnerf76d1802006-07-31 23:26:50 +000017627 return Res;
17628}
Nadav Rotemb4b04c32012-11-03 00:39:56 +000017629
Nadav Roteme6237022012-11-05 19:32:46 +000017630//===----------------------------------------------------------------------===//
17631//
17632// X86 cost model.
17633//
17634//===----------------------------------------------------------------------===//
17635
17636struct X86CostTblEntry {
17637 int ISD;
17638 MVT Type;
17639 unsigned Cost;
17640};
17641
Nadav Rotemd8eae8b2012-11-06 23:36:00 +000017642static int
17643FindInTable(const X86CostTblEntry *Tbl, unsigned len, int ISD, MVT Ty) {
Nadav Rotem7ae3bcc2012-11-05 23:48:20 +000017644 for (unsigned int i = 0; i < len; ++i)
17645 if (Tbl[i].ISD == ISD && Tbl[i].Type == Ty)
17646 return i;
17647
17648 // Could not find an entry.
17649 return -1;
17650}
17651
Nadav Rotemb0428682012-11-06 19:33:53 +000017652struct X86TypeConversionCostTblEntry {
17653 int ISD;
17654 MVT Dst;
17655 MVT Src;
17656 unsigned Cost;
17657};
17658
Nadav Rotemd8eae8b2012-11-06 23:36:00 +000017659static int
17660FindInConvertTable(const X86TypeConversionCostTblEntry *Tbl, unsigned len,
17661 int ISD, MVT Dst, MVT Src) {
Nadav Rotemb0428682012-11-06 19:33:53 +000017662 for (unsigned int i = 0; i < len; ++i)
17663 if (Tbl[i].ISD == ISD && Tbl[i].Src == Src && Tbl[i].Dst == Dst)
17664 return i;
17665
17666 // Could not find an entry.
17667 return -1;
17668}
17669
Nadav Rotemb4b04c32012-11-03 00:39:56 +000017670unsigned
17671X86VectorTargetTransformInfo::getArithmeticInstrCost(unsigned Opcode,
17672 Type *Ty) const {
Nadav Roteme6237022012-11-05 19:32:46 +000017673 // Legalize the type.
Nadav Rotem887c1fe2012-11-05 23:57:45 +000017674 std::pair<unsigned, MVT> LT = getTypeLegalizationCost(Ty);
Nadav Roteme6237022012-11-05 19:32:46 +000017675
17676 int ISD = InstructionOpcodeToISD(Opcode);
17677 assert(ISD && "Invalid opcode");
17678
Nadav Rotemb0428682012-11-06 19:33:53 +000017679 const X86Subtarget &ST = TLI->getTargetMachine().getSubtarget<X86Subtarget>();
Nadav Rotemb4b04c32012-11-03 00:39:56 +000017680
Nadav Roteme6237022012-11-05 19:32:46 +000017681 static const X86CostTblEntry AVX1CostTable[] = {
17682 // We don't have to scalarize unsupported ops. We can issue two half-sized
17683 // operations and we only need to extract the upper YMM half.
17684 // Two ops + 1 extract + 1 insert = 4.
17685 { ISD::MUL, MVT::v8i32, 4 },
17686 { ISD::SUB, MVT::v8i32, 4 },
17687 { ISD::ADD, MVT::v8i32, 4 },
17688 { ISD::MUL, MVT::v4i64, 4 },
17689 { ISD::SUB, MVT::v4i64, 4 },
17690 { ISD::ADD, MVT::v4i64, 4 },
17691 };
Nadav Rotemb4b04c32012-11-03 00:39:56 +000017692
Nadav Roteme6237022012-11-05 19:32:46 +000017693 // Look for AVX1 lowering tricks.
Nadav Rotem7ae3bcc2012-11-05 23:48:20 +000017694 if (ST.hasAVX()) {
17695 int Idx = FindInTable(AVX1CostTable, array_lengthof(AVX1CostTable), ISD,
17696 LT.second);
17697 if (Idx != -1)
17698 return LT.first * AVX1CostTable[Idx].Cost;
17699 }
Nadav Roteme6237022012-11-05 19:32:46 +000017700 // Fallback to the default implementation.
Nadav Rotemb4b04c32012-11-03 00:39:56 +000017701 return VectorTargetTransformImpl::getArithmeticInstrCost(Opcode, Ty);
17702}
17703
17704unsigned
17705X86VectorTargetTransformInfo::getVectorInstrCost(unsigned Opcode, Type *Val,
Richard Smithe010eb32012-11-05 22:01:44 +000017706 unsigned Index) const {
Nadav Rotema4ab5292012-11-05 21:12:13 +000017707 assert(Val->isVectorTy() && "This must be a vector type");
17708
Nadav Rotem7ae3bcc2012-11-05 23:48:20 +000017709 if (Index != -1U) {
Nadav Rotema4ab5292012-11-05 21:12:13 +000017710 // Legalize the type.
Nadav Rotem887c1fe2012-11-05 23:57:45 +000017711 std::pair<unsigned, MVT> LT = getTypeLegalizationCost(Val);
Nadav Rotema4ab5292012-11-05 21:12:13 +000017712
17713 // This type is legalized to a scalar type.
17714 if (!LT.second.isVector())
17715 return 0;
17716
17717 // The type may be split. Normalize the index to the new type.
17718 unsigned Width = LT.second.getVectorNumElements();
17719 Index = Index % Width;
17720
17721 // Floating point scalars are already located in index #0.
17722 if (Val->getScalarType()->isFloatingPointTy() && Index == 0)
17723 return 0;
17724 }
17725
Nadav Rotemb4b04c32012-11-03 00:39:56 +000017726 return VectorTargetTransformImpl::getVectorInstrCost(Opcode, Val, Index);
17727}
17728
Nadav Rotem7ae3bcc2012-11-05 23:48:20 +000017729unsigned X86VectorTargetTransformInfo::getCmpSelInstrCost(unsigned Opcode,
17730 Type *ValTy,
17731 Type *CondTy) const {
17732 // Legalize the type.
Nadav Rotem887c1fe2012-11-05 23:57:45 +000017733 std::pair<unsigned, MVT> LT = getTypeLegalizationCost(ValTy);
Nadav Rotem7ae3bcc2012-11-05 23:48:20 +000017734
17735 MVT MTy = LT.second;
17736
17737 int ISD = InstructionOpcodeToISD(Opcode);
17738 assert(ISD && "Invalid opcode");
17739
17740 const X86Subtarget &ST =
17741 TLI->getTargetMachine().getSubtarget<X86Subtarget>();
17742
17743 static const X86CostTblEntry SSE42CostTbl[] = {
17744 { ISD::SETCC, MVT::v2f64, 1 },
17745 { ISD::SETCC, MVT::v4f32, 1 },
17746 { ISD::SETCC, MVT::v2i64, 1 },
17747 { ISD::SETCC, MVT::v4i32, 1 },
17748 { ISD::SETCC, MVT::v8i16, 1 },
17749 { ISD::SETCC, MVT::v16i8, 1 },
17750 };
17751
17752 static const X86CostTblEntry AVX1CostTbl[] = {
17753 { ISD::SETCC, MVT::v4f64, 1 },
17754 { ISD::SETCC, MVT::v8f32, 1 },
17755 // AVX1 does not support 8-wide integer compare.
17756 { ISD::SETCC, MVT::v4i64, 4 },
17757 { ISD::SETCC, MVT::v8i32, 4 },
17758 { ISD::SETCC, MVT::v16i16, 4 },
17759 { ISD::SETCC, MVT::v32i8, 4 },
17760 };
17761
17762 static const X86CostTblEntry AVX2CostTbl[] = {
17763 { ISD::SETCC, MVT::v4i64, 1 },
17764 { ISD::SETCC, MVT::v8i32, 1 },
17765 { ISD::SETCC, MVT::v16i16, 1 },
17766 { ISD::SETCC, MVT::v32i8, 1 },
17767 };
17768
17769 if (ST.hasSSE42()) {
17770 int Idx = FindInTable(SSE42CostTbl, array_lengthof(SSE42CostTbl), ISD, MTy);
17771 if (Idx != -1)
17772 return LT.first * SSE42CostTbl[Idx].Cost;
17773 }
17774
17775 if (ST.hasAVX()) {
17776 int Idx = FindInTable(AVX1CostTbl, array_lengthof(AVX1CostTbl), ISD, MTy);
17777 if (Idx != -1)
17778 return LT.first * AVX1CostTbl[Idx].Cost;
17779 }
17780
17781 if (ST.hasAVX2()) {
17782 int Idx = FindInTable(AVX2CostTbl, array_lengthof(AVX2CostTbl), ISD, MTy);
17783 if (Idx != -1)
17784 return LT.first * AVX2CostTbl[Idx].Cost;
17785 }
17786
17787 return VectorTargetTransformImpl::getCmpSelInstrCost(Opcode, ValTy, CondTy);
17788}
17789
Nadav Rotemb0428682012-11-06 19:33:53 +000017790unsigned X86VectorTargetTransformInfo::getCastInstrCost(unsigned Opcode,
17791 Type *Dst,
17792 Type *Src) const {
17793 int ISD = InstructionOpcodeToISD(Opcode);
17794 assert(ISD && "Invalid opcode");
Nadav Rotem7ae3bcc2012-11-05 23:48:20 +000017795
Nadav Rotemb0428682012-11-06 19:33:53 +000017796 EVT SrcTy = TLI->getValueType(Src);
17797 EVT DstTy = TLI->getValueType(Dst);
17798
17799 if (!SrcTy.isSimple() || !DstTy.isSimple())
17800 return VectorTargetTransformImpl::getCastInstrCost(Opcode, Dst, Src);
17801
17802 const X86Subtarget &ST = TLI->getTargetMachine().getSubtarget<X86Subtarget>();
17803
17804 static const X86TypeConversionCostTblEntry AVXConversionTbl[] = {
17805 { ISD::SIGN_EXTEND, MVT::v8i32, MVT::v8i16, 1 },
17806 { ISD::ZERO_EXTEND, MVT::v8i32, MVT::v8i16, 1 },
17807 { ISD::SIGN_EXTEND, MVT::v4i64, MVT::v4i32, 1 },
17808 { ISD::ZERO_EXTEND, MVT::v4i64, MVT::v4i32, 1 },
17809 { ISD::TRUNCATE, MVT::v4i32, MVT::v4i64, 1 },
17810 { ISD::TRUNCATE, MVT::v8i16, MVT::v8i32, 1 },
17811 { ISD::SINT_TO_FP, MVT::v8f32, MVT::v8i8, 1 },
17812 { ISD::SINT_TO_FP, MVT::v4f32, MVT::v4i8, 1 },
17813 { ISD::UINT_TO_FP, MVT::v8f32, MVT::v8i8, 1 },
17814 { ISD::UINT_TO_FP, MVT::v4f32, MVT::v4i8, 1 },
Nadav Rotemb14a5f52012-11-09 07:02:24 +000017815 { ISD::FP_TO_SINT, MVT::v8i8, MVT::v8f32, 1 },
17816 { ISD::FP_TO_SINT, MVT::v4i8, MVT::v4f32, 1 },
Nadav Rotemb0428682012-11-06 19:33:53 +000017817 { ISD::ZERO_EXTEND, MVT::v8i32, MVT::v8i1, 6 },
17818 { ISD::SIGN_EXTEND, MVT::v8i32, MVT::v8i1, 9 },
Nadav Rotema6fb97a2012-11-06 21:17:17 +000017819 { ISD::TRUNCATE, MVT::v8i32, MVT::v8i64, 3 },
Nadav Rotemb0428682012-11-06 19:33:53 +000017820 };
17821
17822 if (ST.hasAVX()) {
17823 int Idx = FindInConvertTable(AVXConversionTbl,
17824 array_lengthof(AVXConversionTbl),
17825 ISD, DstTy.getSimpleVT(), SrcTy.getSimpleVT());
17826 if (Idx != -1)
17827 return AVXConversionTbl[Idx].Cost;
17828 }
17829
17830 return VectorTargetTransformImpl::getCastInstrCost(Opcode, Dst, Src);
17831}
Nadav Rotem7ae3bcc2012-11-05 23:48:20 +000017832