blob: 2b48d50f92396d2783689fbdaaa7c0524fd6f5a3 [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"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000017#include "Utils/X86ShuffleDecode.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000018#include "X86.h"
Evan Cheng0cc39452006-01-16 21:21:29 +000019#include "X86InstrBuilder.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000020#include "X86TargetMachine.h"
Chris Lattner8c6ed052009-09-16 01:46:41 +000021#include "X86TargetObjectFile.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000022#include "llvm/ADT/SmallSet.h"
23#include "llvm/ADT/Statistic.h"
24#include "llvm/ADT/StringExtras.h"
25#include "llvm/ADT/VariadicFunction.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000026#include "llvm/CallingConv.h"
Evan Cheng55d42002011-01-08 01:24:27 +000027#include "llvm/CodeGen/IntrinsicLowering.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000028#include "llvm/CodeGen/MachineFrameInfo.h"
Evan Cheng4a460802006-01-11 00:33:36 +000029#include "llvm/CodeGen/MachineFunction.h"
30#include "llvm/CodeGen/MachineInstrBuilder.h"
Chris Lattner5e1df8d2010-01-25 23:38:14 +000031#include "llvm/CodeGen/MachineJumpTableInfo.h"
Evan Chenga844bde2008-02-02 04:07:54 +000032#include "llvm/CodeGen/MachineModuleInfo.h"
Chris Lattner84bc5422007-12-31 04:13:23 +000033#include "llvm/CodeGen/MachineRegisterInfo.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000034#include "llvm/Constants.h"
35#include "llvm/DerivedTypes.h"
36#include "llvm/Function.h"
37#include "llvm/GlobalAlias.h"
38#include "llvm/GlobalVariable.h"
39#include "llvm/Instructions.h"
40#include "llvm/Intrinsics.h"
41#include "llvm/LLVMContext.h"
Chris Lattner589c6f62010-01-26 06:28:43 +000042#include "llvm/MC/MCAsmInfo.h"
Chris Lattnerc64daab2010-01-26 05:02:42 +000043#include "llvm/MC/MCContext.h"
Daniel Dunbar4e815f82010-03-15 23:51:06 +000044#include "llvm/MC/MCExpr.h"
Chris Lattnerc64daab2010-01-26 05:02:42 +000045#include "llvm/MC/MCSymbol.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);
Benjamin Kramer2f8a6cd2012-12-22 16:07:56 +0000873 setOperationAction(ISD::MUL, MVT::v4i32, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000874 setOperationAction(ISD::MUL, MVT::v2i64, Custom);
875 setOperationAction(ISD::SUB, MVT::v16i8, Legal);
876 setOperationAction(ISD::SUB, MVT::v8i16, Legal);
877 setOperationAction(ISD::SUB, MVT::v4i32, Legal);
878 setOperationAction(ISD::SUB, MVT::v2i64, Legal);
879 setOperationAction(ISD::MUL, MVT::v8i16, Legal);
880 setOperationAction(ISD::FADD, MVT::v2f64, Legal);
881 setOperationAction(ISD::FSUB, MVT::v2f64, Legal);
882 setOperationAction(ISD::FMUL, MVT::v2f64, Legal);
883 setOperationAction(ISD::FDIV, MVT::v2f64, Legal);
884 setOperationAction(ISD::FSQRT, MVT::v2f64, Legal);
885 setOperationAction(ISD::FNEG, MVT::v2f64, Custom);
Craig Topper43620672012-09-08 07:31:51 +0000886 setOperationAction(ISD::FABS, MVT::v2f64, Custom);
Evan Cheng2c3ae372006-04-12 21:21:57 +0000887
Nadav Rotem354efd82011-09-18 14:57:03 +0000888 setOperationAction(ISD::SETCC, MVT::v2i64, Custom);
Duncan Sands28b77e92011-09-06 19:07:46 +0000889 setOperationAction(ISD::SETCC, MVT::v16i8, Custom);
890 setOperationAction(ISD::SETCC, MVT::v8i16, Custom);
891 setOperationAction(ISD::SETCC, MVT::v4i32, Custom);
Nate Begemanc2616e42008-05-12 20:34:32 +0000892
Owen Anderson825b72b2009-08-11 20:47:22 +0000893 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v16i8, Custom);
894 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i16, Custom);
895 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
896 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom);
897 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
Evan Chengf7c378e2006-04-10 07:23:14 +0000898
Evan Cheng2c3ae372006-04-12 21:21:57 +0000899 // Custom lower build_vector, vector_shuffle, and extract_vector_elt.
Jakub Staszak6610b1d2012-04-29 20:52:53 +0000900 for (int i = MVT::v16i8; i != MVT::v2i64; ++i) {
Craig Topper0d1f1762012-08-12 00:34:56 +0000901 MVT VT = (MVT::SimpleValueType)i;
Nate Begeman844e0f92007-12-11 01:41:33 +0000902 // Do not attempt to custom lower non-power-of-2 vectors
Duncan Sands83ec4b62008-06-06 12:08:01 +0000903 if (!isPowerOf2_32(VT.getVectorNumElements()))
Nate Begeman844e0f92007-12-11 01:41:33 +0000904 continue;
David Greene9b9838d2009-06-29 16:47:10 +0000905 // Do not attempt to custom lower non-128-bit vectors
906 if (!VT.is128BitVector())
907 continue;
Craig Topper0d1f1762012-08-12 00:34:56 +0000908 setOperationAction(ISD::BUILD_VECTOR, VT, Custom);
909 setOperationAction(ISD::VECTOR_SHUFFLE, VT, Custom);
910 setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT, Custom);
Evan Cheng2c3ae372006-04-12 21:21:57 +0000911 }
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000912
Owen Anderson825b72b2009-08-11 20:47:22 +0000913 setOperationAction(ISD::BUILD_VECTOR, MVT::v2f64, Custom);
914 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i64, Custom);
915 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2f64, Custom);
916 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i64, Custom);
917 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2f64, Custom);
918 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2f64, Custom);
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000919
Nate Begemancdd1eec2008-02-12 22:51:28 +0000920 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000921 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i64, Custom);
922 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Custom);
Nate Begemancdd1eec2008-02-12 22:51:28 +0000923 }
Evan Cheng2c3ae372006-04-12 21:21:57 +0000924
Anton Korobeynikov12c49af2006-11-21 00:01:06 +0000925 // Promote v16i8, v8i16, v4i32 load, select, and, or, xor to v2i64.
Craig Topper31a207a2012-05-04 06:39:13 +0000926 for (int i = MVT::v16i8; i != MVT::v2i64; ++i) {
Craig Topper0d1f1762012-08-12 00:34:56 +0000927 MVT VT = (MVT::SimpleValueType)i;
David Greene9b9838d2009-06-29 16:47:10 +0000928
929 // Do not attempt to promote non-128-bit vectors
Chris Lattner32b4b5a2010-07-05 05:53:14 +0000930 if (!VT.is128BitVector())
David Greene9b9838d2009-06-29 16:47:10 +0000931 continue;
Michael J. Spencerec38de22010-10-10 22:04:20 +0000932
Craig Topper0d1f1762012-08-12 00:34:56 +0000933 setOperationAction(ISD::AND, VT, Promote);
934 AddPromotedToType (ISD::AND, VT, MVT::v2i64);
935 setOperationAction(ISD::OR, VT, Promote);
936 AddPromotedToType (ISD::OR, VT, MVT::v2i64);
937 setOperationAction(ISD::XOR, VT, Promote);
938 AddPromotedToType (ISD::XOR, VT, MVT::v2i64);
939 setOperationAction(ISD::LOAD, VT, Promote);
940 AddPromotedToType (ISD::LOAD, VT, MVT::v2i64);
941 setOperationAction(ISD::SELECT, VT, Promote);
942 AddPromotedToType (ISD::SELECT, VT, MVT::v2i64);
Evan Chengf7c378e2006-04-10 07:23:14 +0000943 }
Evan Cheng2c3ae372006-04-12 21:21:57 +0000944
Owen Anderson825b72b2009-08-11 20:47:22 +0000945 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000946
Evan Cheng2c3ae372006-04-12 21:21:57 +0000947 // Custom lower v2i64 and v2f64 selects.
Owen Anderson825b72b2009-08-11 20:47:22 +0000948 setOperationAction(ISD::LOAD, MVT::v2f64, Legal);
949 setOperationAction(ISD::LOAD, MVT::v2i64, Legal);
950 setOperationAction(ISD::SELECT, MVT::v2f64, Custom);
951 setOperationAction(ISD::SELECT, MVT::v2i64, Custom);
Scott Michelfdc40a02009-02-17 22:15:04 +0000952
Owen Anderson825b72b2009-08-11 20:47:22 +0000953 setOperationAction(ISD::FP_TO_SINT, MVT::v4i32, Legal);
954 setOperationAction(ISD::SINT_TO_FP, MVT::v4i32, Legal);
Michael Liaob8150d82012-09-10 18:33:51 +0000955
Michael Liaoa7554632012-10-23 17:36:08 +0000956 setOperationAction(ISD::UINT_TO_FP, MVT::v4i8, Custom);
957 setOperationAction(ISD::UINT_TO_FP, MVT::v4i16, Custom);
Michael Liao991b6a22012-10-24 04:09:32 +0000958 // As there is no 64-bit GPR available, we need build a special custom
959 // sequence to convert from v2i32 to v2f32.
960 if (!Subtarget->is64Bit())
961 setOperationAction(ISD::UINT_TO_FP, MVT::v2f32, Custom);
Michael Liaoa7554632012-10-23 17:36:08 +0000962
Michael Liao9d796db2012-10-10 16:32:15 +0000963 setOperationAction(ISD::FP_EXTEND, MVT::v2f32, Custom);
Michael Liao44c2d612012-10-10 16:53:28 +0000964 setOperationAction(ISD::FP_ROUND, MVT::v2f32, Custom);
Michael Liao9d796db2012-10-10 16:32:15 +0000965
Michael Liaob8150d82012-09-10 18:33:51 +0000966 setLoadExtAction(ISD::EXTLOAD, MVT::v2f32, Legal);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000967 }
Evan Chengc7ce29b2009-02-13 22:36:38 +0000968
Craig Topperd0a31172012-01-10 06:37:29 +0000969 if (Subtarget->hasSSE41()) {
Benjamin Kramerb6533972011-12-09 15:44:03 +0000970 setOperationAction(ISD::FFLOOR, MVT::f32, Legal);
971 setOperationAction(ISD::FCEIL, MVT::f32, Legal);
972 setOperationAction(ISD::FTRUNC, MVT::f32, Legal);
973 setOperationAction(ISD::FRINT, MVT::f32, Legal);
974 setOperationAction(ISD::FNEARBYINT, MVT::f32, Legal);
975 setOperationAction(ISD::FFLOOR, MVT::f64, Legal);
976 setOperationAction(ISD::FCEIL, MVT::f64, Legal);
977 setOperationAction(ISD::FTRUNC, MVT::f64, Legal);
978 setOperationAction(ISD::FRINT, MVT::f64, Legal);
979 setOperationAction(ISD::FNEARBYINT, MVT::f64, Legal);
980
Craig Topper12fb5c62012-09-08 17:42:27 +0000981 setOperationAction(ISD::FFLOOR, MVT::v4f32, Legal);
Craig Topperd5775522012-11-16 06:37:56 +0000982 setOperationAction(ISD::FCEIL, MVT::v4f32, Legal);
983 setOperationAction(ISD::FTRUNC, MVT::v4f32, Legal);
984 setOperationAction(ISD::FRINT, MVT::v4f32, Legal);
985 setOperationAction(ISD::FNEARBYINT, MVT::v4f32, Legal);
Craig Topper12fb5c62012-09-08 17:42:27 +0000986 setOperationAction(ISD::FFLOOR, MVT::v2f64, Legal);
Craig Topperd5775522012-11-16 06:37:56 +0000987 setOperationAction(ISD::FCEIL, MVT::v2f64, Legal);
988 setOperationAction(ISD::FTRUNC, MVT::v2f64, Legal);
989 setOperationAction(ISD::FRINT, MVT::v2f64, Legal);
990 setOperationAction(ISD::FNEARBYINT, MVT::v2f64, Legal);
Craig Topper12fb5c62012-09-08 17:42:27 +0000991
Nate Begeman14d12ca2008-02-11 04:19:36 +0000992 // FIXME: Do we need to handle scalar-to-vector here?
Owen Anderson825b72b2009-08-11 20:47:22 +0000993 setOperationAction(ISD::MUL, MVT::v4i32, Legal);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000994
Nadav Rotemfbad25e2011-09-11 15:02:23 +0000995 setOperationAction(ISD::VSELECT, MVT::v2f64, Legal);
996 setOperationAction(ISD::VSELECT, MVT::v2i64, Legal);
997 setOperationAction(ISD::VSELECT, MVT::v16i8, Legal);
998 setOperationAction(ISD::VSELECT, MVT::v4i32, Legal);
999 setOperationAction(ISD::VSELECT, MVT::v4f32, Legal);
Nadav Rotemffe3e7d2011-09-08 08:11:19 +00001000
Nate Begeman14d12ca2008-02-11 04:19:36 +00001001 // i8 and i16 vectors are custom , because the source register and source
1002 // source memory operand types are not the same width. f32 vectors are
1003 // custom since the immediate controlling the insert encodes additional
1004 // information.
Owen Anderson825b72b2009-08-11 20:47:22 +00001005 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v16i8, Custom);
1006 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
1007 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom);
1008 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
Nate Begeman14d12ca2008-02-11 04:19:36 +00001009
Owen Anderson825b72b2009-08-11 20:47:22 +00001010 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v16i8, Custom);
1011 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v8i16, Custom);
1012 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4i32, Custom);
1013 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
Nate Begeman14d12ca2008-02-11 04:19:36 +00001014
Pete Coopera77214a2011-11-14 19:38:42 +00001015 // FIXME: these should be Legal but thats only for the case where
Chad Rosier30450e82011-12-22 22:35:21 +00001016 // the index is constant. For now custom expand to deal with that.
Nate Begeman14d12ca2008-02-11 04:19:36 +00001017 if (Subtarget->is64Bit()) {
Pete Coopera77214a2011-11-14 19:38:42 +00001018 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i64, Custom);
1019 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Custom);
Nate Begeman14d12ca2008-02-11 04:19:36 +00001020 }
1021 }
Evan Cheng470a6ad2006-02-22 02:26:30 +00001022
Craig Topper1accb7e2012-01-10 06:54:16 +00001023 if (Subtarget->hasSSE2()) {
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +00001024 setOperationAction(ISD::SRL, MVT::v8i16, Custom);
Craig Topper7be5dfd2011-11-12 09:58:49 +00001025 setOperationAction(ISD::SRL, MVT::v16i8, Custom);
Nadav Rotem43012222011-05-11 08:12:09 +00001026
Nadav Rotem43012222011-05-11 08:12:09 +00001027 setOperationAction(ISD::SHL, MVT::v8i16, Custom);
Craig Topper7be5dfd2011-11-12 09:58:49 +00001028 setOperationAction(ISD::SHL, MVT::v16i8, Custom);
Nadav Rotem43012222011-05-11 08:12:09 +00001029
Nadav Rotem43012222011-05-11 08:12:09 +00001030 setOperationAction(ISD::SRA, MVT::v8i16, Custom);
Eli Friedmanf6aa6b12011-11-01 21:18:39 +00001031 setOperationAction(ISD::SRA, MVT::v16i8, Custom);
Craig Topper7be5dfd2011-11-12 09:58:49 +00001032
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00001033 if (Subtarget->hasInt256()) {
Craig Topper7be5dfd2011-11-12 09:58:49 +00001034 setOperationAction(ISD::SRL, MVT::v2i64, Legal);
1035 setOperationAction(ISD::SRL, MVT::v4i32, Legal);
1036
1037 setOperationAction(ISD::SHL, MVT::v2i64, Legal);
1038 setOperationAction(ISD::SHL, MVT::v4i32, Legal);
1039
1040 setOperationAction(ISD::SRA, MVT::v4i32, Legal);
1041 } else {
1042 setOperationAction(ISD::SRL, MVT::v2i64, Custom);
1043 setOperationAction(ISD::SRL, MVT::v4i32, Custom);
1044
1045 setOperationAction(ISD::SHL, MVT::v2i64, Custom);
1046 setOperationAction(ISD::SHL, MVT::v4i32, Custom);
1047
1048 setOperationAction(ISD::SRA, MVT::v4i32, Custom);
1049 }
Nadav Rotem43012222011-05-11 08:12:09 +00001050 }
1051
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00001052 if (!TM.Options.UseSoftFloat && Subtarget->hasFp256()) {
Craig Topperc9099502012-04-20 06:31:50 +00001053 addRegisterClass(MVT::v32i8, &X86::VR256RegClass);
1054 addRegisterClass(MVT::v16i16, &X86::VR256RegClass);
1055 addRegisterClass(MVT::v8i32, &X86::VR256RegClass);
1056 addRegisterClass(MVT::v8f32, &X86::VR256RegClass);
1057 addRegisterClass(MVT::v4i64, &X86::VR256RegClass);
1058 addRegisterClass(MVT::v4f64, &X86::VR256RegClass);
David Greened94c1012009-06-29 22:50:51 +00001059
Owen Anderson825b72b2009-08-11 20:47:22 +00001060 setOperationAction(ISD::LOAD, MVT::v8f32, Legal);
Owen Anderson825b72b2009-08-11 20:47:22 +00001061 setOperationAction(ISD::LOAD, MVT::v4f64, Legal);
1062 setOperationAction(ISD::LOAD, MVT::v4i64, Legal);
David Greene54d8eba2011-01-27 22:38:56 +00001063
Owen Anderson825b72b2009-08-11 20:47:22 +00001064 setOperationAction(ISD::FADD, MVT::v8f32, Legal);
1065 setOperationAction(ISD::FSUB, MVT::v8f32, Legal);
1066 setOperationAction(ISD::FMUL, MVT::v8f32, Legal);
1067 setOperationAction(ISD::FDIV, MVT::v8f32, Legal);
1068 setOperationAction(ISD::FSQRT, MVT::v8f32, Legal);
Craig Topper12fb5c62012-09-08 17:42:27 +00001069 setOperationAction(ISD::FFLOOR, MVT::v8f32, Legal);
Craig Topperd5775522012-11-16 06:37:56 +00001070 setOperationAction(ISD::FCEIL, MVT::v8f32, Legal);
1071 setOperationAction(ISD::FTRUNC, MVT::v8f32, Legal);
1072 setOperationAction(ISD::FRINT, MVT::v8f32, Legal);
1073 setOperationAction(ISD::FNEARBYINT, MVT::v8f32, Legal);
Owen Anderson825b72b2009-08-11 20:47:22 +00001074 setOperationAction(ISD::FNEG, MVT::v8f32, Custom);
Craig Topper43620672012-09-08 07:31:51 +00001075 setOperationAction(ISD::FABS, MVT::v8f32, Custom);
David Greene9b9838d2009-06-29 16:47:10 +00001076
Owen Anderson825b72b2009-08-11 20:47:22 +00001077 setOperationAction(ISD::FADD, MVT::v4f64, Legal);
1078 setOperationAction(ISD::FSUB, MVT::v4f64, Legal);
1079 setOperationAction(ISD::FMUL, MVT::v4f64, Legal);
1080 setOperationAction(ISD::FDIV, MVT::v4f64, Legal);
1081 setOperationAction(ISD::FSQRT, MVT::v4f64, Legal);
Craig Topper12fb5c62012-09-08 17:42:27 +00001082 setOperationAction(ISD::FFLOOR, MVT::v4f64, Legal);
Craig Topperd5775522012-11-16 06:37:56 +00001083 setOperationAction(ISD::FCEIL, MVT::v4f64, Legal);
1084 setOperationAction(ISD::FTRUNC, MVT::v4f64, Legal);
1085 setOperationAction(ISD::FRINT, MVT::v4f64, Legal);
1086 setOperationAction(ISD::FNEARBYINT, MVT::v4f64, Legal);
Owen Anderson825b72b2009-08-11 20:47:22 +00001087 setOperationAction(ISD::FNEG, MVT::v4f64, Custom);
Craig Topper43620672012-09-08 07:31:51 +00001088 setOperationAction(ISD::FABS, MVT::v4f64, Custom);
David Greene9b9838d2009-06-29 16:47:10 +00001089
Michael Liaobedcbd42012-10-16 18:14:11 +00001090 setOperationAction(ISD::TRUNCATE, MVT::v8i16, Custom);
Nadav Rotem3c22a442012-12-27 07:45:10 +00001091 setOperationAction(ISD::TRUNCATE, MVT::v4i32, Custom);
Michael Liaobedcbd42012-10-16 18:14:11 +00001092
1093 setOperationAction(ISD::FP_TO_SINT, MVT::v8i16, Custom);
1094
Bruno Cardoso Lopes2e64ae42011-07-28 01:26:39 +00001095 setOperationAction(ISD::FP_TO_SINT, MVT::v8i32, Legal);
1096 setOperationAction(ISD::SINT_TO_FP, MVT::v8i32, Legal);
Bruno Cardoso Lopes55244ce2011-08-01 21:54:09 +00001097 setOperationAction(ISD::FP_ROUND, MVT::v4f32, Legal);
Bruno Cardoso Lopes2e64ae42011-07-28 01:26:39 +00001098
Michael Liaoa7554632012-10-23 17:36:08 +00001099 setOperationAction(ISD::ZERO_EXTEND, MVT::v8i32, Custom);
1100 setOperationAction(ISD::UINT_TO_FP, MVT::v8i8, Custom);
1101 setOperationAction(ISD::UINT_TO_FP, MVT::v8i16, Custom);
1102
Michael Liaob8150d82012-09-10 18:33:51 +00001103 setLoadExtAction(ISD::EXTLOAD, MVT::v4f32, Legal);
1104
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +00001105 setOperationAction(ISD::SRL, MVT::v16i16, Custom);
1106 setOperationAction(ISD::SRL, MVT::v32i8, Custom);
1107
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +00001108 setOperationAction(ISD::SHL, MVT::v16i16, Custom);
1109 setOperationAction(ISD::SHL, MVT::v32i8, Custom);
1110
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +00001111 setOperationAction(ISD::SRA, MVT::v16i16, Custom);
Craig Topper7be5dfd2011-11-12 09:58:49 +00001112 setOperationAction(ISD::SRA, MVT::v32i8, Custom);
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +00001113
Duncan Sands28b77e92011-09-06 19:07:46 +00001114 setOperationAction(ISD::SETCC, MVT::v32i8, Custom);
1115 setOperationAction(ISD::SETCC, MVT::v16i16, Custom);
1116 setOperationAction(ISD::SETCC, MVT::v8i32, Custom);
1117 setOperationAction(ISD::SETCC, MVT::v4i64, Custom);
Bruno Cardoso Lopes0f0e0a02011-08-09 00:46:57 +00001118
Bruno Cardoso Lopesd40aa242011-08-09 23:27:13 +00001119 setOperationAction(ISD::SELECT, MVT::v4f64, Custom);
1120 setOperationAction(ISD::SELECT, MVT::v4i64, Custom);
1121 setOperationAction(ISD::SELECT, MVT::v8f32, Custom);
1122
Craig Topperaaa643c2011-11-09 07:28:55 +00001123 setOperationAction(ISD::VSELECT, MVT::v4f64, Legal);
1124 setOperationAction(ISD::VSELECT, MVT::v4i64, Legal);
1125 setOperationAction(ISD::VSELECT, MVT::v8i32, Legal);
1126 setOperationAction(ISD::VSELECT, MVT::v8f32, Legal);
Nadav Rotem8ffad562011-09-09 20:29:17 +00001127
Nadav Rotem0509db22012-12-28 05:45:24 +00001128 setOperationAction(ISD::SIGN_EXTEND, MVT::v4i64, Custom);
1129 setOperationAction(ISD::SIGN_EXTEND, MVT::v8i32, Custom);
1130 setOperationAction(ISD::ZERO_EXTEND, MVT::v4i64, Custom);
1131 setOperationAction(ISD::ZERO_EXTEND, MVT::v8i32, Custom);
1132 setOperationAction(ISD::ANY_EXTEND, MVT::v4i64, Custom);
1133 setOperationAction(ISD::ANY_EXTEND, MVT::v8i32, Custom);
Nadav Rotem1a330af2012-12-27 22:47:16 +00001134
Craig Topperbf404372012-08-31 15:40:30 +00001135 if (Subtarget->hasFMA() || Subtarget->hasFMA4()) {
Craig Topper3dcefc82012-11-21 05:36:24 +00001136 setOperationAction(ISD::FMA, MVT::v8f32, Legal);
1137 setOperationAction(ISD::FMA, MVT::v4f64, Legal);
1138 setOperationAction(ISD::FMA, MVT::v4f32, Legal);
1139 setOperationAction(ISD::FMA, MVT::v2f64, Legal);
1140 setOperationAction(ISD::FMA, MVT::f32, Legal);
1141 setOperationAction(ISD::FMA, MVT::f64, Legal);
Elena Demikhovsky1503aba2012-08-01 12:06:00 +00001142 }
Craig Topper880ef452012-08-11 22:34:26 +00001143
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00001144 if (Subtarget->hasInt256()) {
Craig Topperaaa643c2011-11-09 07:28:55 +00001145 setOperationAction(ISD::ADD, MVT::v4i64, Legal);
1146 setOperationAction(ISD::ADD, MVT::v8i32, Legal);
1147 setOperationAction(ISD::ADD, MVT::v16i16, Legal);
1148 setOperationAction(ISD::ADD, MVT::v32i8, Legal);
Craig Topper13894fa2011-08-24 06:14:18 +00001149
Craig Topperaaa643c2011-11-09 07:28:55 +00001150 setOperationAction(ISD::SUB, MVT::v4i64, Legal);
1151 setOperationAction(ISD::SUB, MVT::v8i32, Legal);
1152 setOperationAction(ISD::SUB, MVT::v16i16, Legal);
1153 setOperationAction(ISD::SUB, MVT::v32i8, Legal);
Craig Topper13894fa2011-08-24 06:14:18 +00001154
Craig Topperaaa643c2011-11-09 07:28:55 +00001155 setOperationAction(ISD::MUL, MVT::v4i64, Custom);
1156 setOperationAction(ISD::MUL, MVT::v8i32, Legal);
1157 setOperationAction(ISD::MUL, MVT::v16i16, Legal);
Craig Topper46154eb2011-11-11 07:39:23 +00001158 // Don't lower v32i8 because there is no 128-bit byte mul
Nadav Rotembb539bf2011-11-09 13:21:28 +00001159
1160 setOperationAction(ISD::VSELECT, MVT::v32i8, Legal);
Craig Topper7be5dfd2011-11-12 09:58:49 +00001161
1162 setOperationAction(ISD::SRL, MVT::v4i64, Legal);
1163 setOperationAction(ISD::SRL, MVT::v8i32, Legal);
1164
1165 setOperationAction(ISD::SHL, MVT::v4i64, Legal);
1166 setOperationAction(ISD::SHL, MVT::v8i32, Legal);
1167
1168 setOperationAction(ISD::SRA, MVT::v8i32, Legal);
Craig Topperaaa643c2011-11-09 07:28:55 +00001169 } else {
1170 setOperationAction(ISD::ADD, MVT::v4i64, Custom);
1171 setOperationAction(ISD::ADD, MVT::v8i32, Custom);
1172 setOperationAction(ISD::ADD, MVT::v16i16, Custom);
1173 setOperationAction(ISD::ADD, MVT::v32i8, Custom);
1174
1175 setOperationAction(ISD::SUB, MVT::v4i64, Custom);
1176 setOperationAction(ISD::SUB, MVT::v8i32, Custom);
1177 setOperationAction(ISD::SUB, MVT::v16i16, Custom);
1178 setOperationAction(ISD::SUB, MVT::v32i8, Custom);
1179
1180 setOperationAction(ISD::MUL, MVT::v4i64, Custom);
1181 setOperationAction(ISD::MUL, MVT::v8i32, Custom);
1182 setOperationAction(ISD::MUL, MVT::v16i16, Custom);
1183 // Don't lower v32i8 because there is no 128-bit byte mul
Craig Topper7be5dfd2011-11-12 09:58:49 +00001184
1185 setOperationAction(ISD::SRL, MVT::v4i64, Custom);
1186 setOperationAction(ISD::SRL, MVT::v8i32, Custom);
1187
1188 setOperationAction(ISD::SHL, MVT::v4i64, Custom);
1189 setOperationAction(ISD::SHL, MVT::v8i32, Custom);
1190
1191 setOperationAction(ISD::SRA, MVT::v8i32, Custom);
Craig Topperaaa643c2011-11-09 07:28:55 +00001192 }
Craig Topper13894fa2011-08-24 06:14:18 +00001193
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001194 // Custom lower several nodes for 256-bit types.
Jakub Staszak6610b1d2012-04-29 20:52:53 +00001195 for (int i = MVT::FIRST_VECTOR_VALUETYPE;
1196 i <= MVT::LAST_VECTOR_VALUETYPE; ++i) {
Craig Topper0d1f1762012-08-12 00:34:56 +00001197 MVT VT = (MVT::SimpleValueType)i;
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001198
1199 // Extract subvector is special because the value type
1200 // (result) is 128-bit but the source is 256-bit wide.
1201 if (VT.is128BitVector())
Craig Topper0d1f1762012-08-12 00:34:56 +00001202 setOperationAction(ISD::EXTRACT_SUBVECTOR, VT, Custom);
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001203
1204 // Do not attempt to custom lower other non-256-bit vectors
1205 if (!VT.is256BitVector())
David Greene9b9838d2009-06-29 16:47:10 +00001206 continue;
David Greene54d8eba2011-01-27 22:38:56 +00001207
Craig Topper0d1f1762012-08-12 00:34:56 +00001208 setOperationAction(ISD::BUILD_VECTOR, VT, Custom);
1209 setOperationAction(ISD::VECTOR_SHUFFLE, VT, Custom);
1210 setOperationAction(ISD::INSERT_VECTOR_ELT, VT, Custom);
1211 setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT, Custom);
1212 setOperationAction(ISD::SCALAR_TO_VECTOR, VT, Custom);
1213 setOperationAction(ISD::INSERT_SUBVECTOR, VT, Custom);
1214 setOperationAction(ISD::CONCAT_VECTORS, VT, Custom);
David Greene9b9838d2009-06-29 16:47:10 +00001215 }
1216
David Greene54d8eba2011-01-27 22:38:56 +00001217 // Promote v32i8, v16i16, v8i32 select, and, or, xor to v4i64.
Jakub Staszak6610b1d2012-04-29 20:52:53 +00001218 for (int i = MVT::v32i8; i != MVT::v4i64; ++i) {
Craig Topper0d1f1762012-08-12 00:34:56 +00001219 MVT VT = (MVT::SimpleValueType)i;
David Greene54d8eba2011-01-27 22:38:56 +00001220
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001221 // Do not attempt to promote non-256-bit vectors
1222 if (!VT.is256BitVector())
David Greene54d8eba2011-01-27 22:38:56 +00001223 continue;
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001224
Craig Topper0d1f1762012-08-12 00:34:56 +00001225 setOperationAction(ISD::AND, VT, Promote);
1226 AddPromotedToType (ISD::AND, VT, MVT::v4i64);
1227 setOperationAction(ISD::OR, VT, Promote);
1228 AddPromotedToType (ISD::OR, VT, MVT::v4i64);
1229 setOperationAction(ISD::XOR, VT, Promote);
1230 AddPromotedToType (ISD::XOR, VT, MVT::v4i64);
1231 setOperationAction(ISD::LOAD, VT, Promote);
1232 AddPromotedToType (ISD::LOAD, VT, MVT::v4i64);
1233 setOperationAction(ISD::SELECT, VT, Promote);
1234 AddPromotedToType (ISD::SELECT, VT, MVT::v4i64);
David Greene54d8eba2011-01-27 22:38:56 +00001235 }
David Greene9b9838d2009-06-29 16:47:10 +00001236 }
1237
Nadav Rotemd0f3ef82011-07-14 11:11:14 +00001238 // SIGN_EXTEND_INREGs are evaluated by the extend type. Handle the expansion
1239 // of this type with custom code.
Jakub Staszak6610b1d2012-04-29 20:52:53 +00001240 for (int VT = MVT::FIRST_VECTOR_VALUETYPE;
1241 VT != MVT::LAST_VECTOR_VALUETYPE; VT++) {
Chad Rosier30450e82011-12-22 22:35:21 +00001242 setOperationAction(ISD::SIGN_EXTEND_INREG, (MVT::SimpleValueType)VT,
1243 Custom);
Nadav Rotemd0f3ef82011-07-14 11:11:14 +00001244 }
1245
Evan Cheng6be2c582006-04-05 23:38:46 +00001246 // We want to custom lower some of our intrinsics.
Owen Anderson825b72b2009-08-11 20:47:22 +00001247 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
Benjamin Kramerb9bee042012-07-12 09:31:43 +00001248 setOperationAction(ISD::INTRINSIC_W_CHAIN, MVT::Other, Custom);
Evan Cheng6be2c582006-04-05 23:38:46 +00001249
Eli Friedman962f5492010-06-02 19:35:46 +00001250 // Only custom-lower 64-bit SADDO and friends on 64-bit because we don't
1251 // handle type legalization for these operations here.
Dan Gohman71c62a22010-06-02 19:13:40 +00001252 //
Eli Friedman962f5492010-06-02 19:35:46 +00001253 // FIXME: We really should do custom legalization for addition and
1254 // subtraction on x86-32 once PR3203 is fixed. We really can't do much better
1255 // than generic legalization for 64-bit multiplication-with-overflow, though.
Chris Lattnera34b3cf2010-12-19 20:03:11 +00001256 for (unsigned i = 0, e = 3+Subtarget->is64Bit(); i != e; ++i) {
1257 // Add/Sub/Mul with overflow operations are custom lowered.
1258 MVT VT = IntVTs[i];
1259 setOperationAction(ISD::SADDO, VT, Custom);
1260 setOperationAction(ISD::UADDO, VT, Custom);
1261 setOperationAction(ISD::SSUBO, VT, Custom);
1262 setOperationAction(ISD::USUBO, VT, Custom);
1263 setOperationAction(ISD::SMULO, VT, Custom);
1264 setOperationAction(ISD::UMULO, VT, Custom);
Eli Friedmana993f0a2010-06-02 00:27:18 +00001265 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00001266
Chris Lattnera34b3cf2010-12-19 20:03:11 +00001267 // There are no 8-bit 3-address imul/mul instructions
1268 setOperationAction(ISD::SMULO, MVT::i8, Expand);
1269 setOperationAction(ISD::UMULO, MVT::i8, Expand);
Bill Wendling41ea7e72008-11-24 19:21:46 +00001270
Evan Chengd54f2d52009-03-31 19:38:51 +00001271 if (!Subtarget->is64Bit()) {
1272 // These libcalls are not available in 32-bit.
1273 setLibcallName(RTLIB::SHL_I128, 0);
1274 setLibcallName(RTLIB::SRL_I128, 0);
1275 setLibcallName(RTLIB::SRA_I128, 0);
1276 }
1277
Evan Cheng206ee9d2006-07-07 08:33:52 +00001278 // We have target-specific dag combine patterns for the following nodes:
1279 setTargetDAGCombine(ISD::VECTOR_SHUFFLE);
Dan Gohman1bbf72b2010-03-15 23:23:03 +00001280 setTargetDAGCombine(ISD::EXTRACT_VECTOR_ELT);
Duncan Sands6bcd2192011-09-17 16:49:39 +00001281 setTargetDAGCombine(ISD::VSELECT);
Chris Lattner83e6c992006-10-04 06:57:07 +00001282 setTargetDAGCombine(ISD::SELECT);
Nate Begeman740ab032009-01-26 00:52:55 +00001283 setTargetDAGCombine(ISD::SHL);
1284 setTargetDAGCombine(ISD::SRA);
1285 setTargetDAGCombine(ISD::SRL);
Evan Cheng760d1942010-01-04 21:22:48 +00001286 setTargetDAGCombine(ISD::OR);
Nate Begemanb65c1752010-12-17 22:55:37 +00001287 setTargetDAGCombine(ISD::AND);
Benjamin Kramer7d6fe132010-12-21 21:41:44 +00001288 setTargetDAGCombine(ISD::ADD);
Duncan Sands17470be2011-09-22 20:15:48 +00001289 setTargetDAGCombine(ISD::FADD);
1290 setTargetDAGCombine(ISD::FSUB);
Elena Demikhovsky1503aba2012-08-01 12:06:00 +00001291 setTargetDAGCombine(ISD::FMA);
Benjamin Kramer7d6fe132010-12-21 21:41:44 +00001292 setTargetDAGCombine(ISD::SUB);
Nadav Rotem91e43fd2011-09-18 10:39:32 +00001293 setTargetDAGCombine(ISD::LOAD);
Chris Lattner149a4e52008-02-22 02:09:43 +00001294 setTargetDAGCombine(ISD::STORE);
Evan Cheng2e489c42009-12-16 00:53:11 +00001295 setTargetDAGCombine(ISD::ZERO_EXTEND);
Elena Demikhovsky1da58672012-04-22 09:39:03 +00001296 setTargetDAGCombine(ISD::ANY_EXTEND);
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +00001297 setTargetDAGCombine(ISD::SIGN_EXTEND);
Elena Demikhovsky3ae98152012-02-01 07:56:44 +00001298 setTargetDAGCombine(ISD::TRUNCATE);
Stuart Hastingsf99a4b82011-06-06 23:15:58 +00001299 setTargetDAGCombine(ISD::SINT_TO_FP);
Chad Rosiera73b6fc2012-04-27 22:33:25 +00001300 setTargetDAGCombine(ISD::SETCC);
Evan Cheng0b0cd912009-03-28 05:57:29 +00001301 if (Subtarget->is64Bit())
1302 setTargetDAGCombine(ISD::MUL);
Manman Ren92363622012-06-07 22:39:10 +00001303 setTargetDAGCombine(ISD::XOR);
Evan Cheng206ee9d2006-07-07 08:33:52 +00001304
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001305 computeRegisterProperties();
1306
Evan Cheng05219282011-01-06 06:52:41 +00001307 // On Darwin, -Os means optimize for size without hurting performance,
1308 // do not reduce the limit.
Dan Gohman87060f52008-06-30 21:00:56 +00001309 maxStoresPerMemset = 16; // For @llvm.memset -> sequence of stores
Evan Cheng05219282011-01-06 06:52:41 +00001310 maxStoresPerMemsetOptSize = Subtarget->isTargetDarwin() ? 16 : 8;
Evan Cheng255f20f2010-04-01 06:04:33 +00001311 maxStoresPerMemcpy = 8; // For @llvm.memcpy -> sequence of stores
Evan Cheng05219282011-01-06 06:52:41 +00001312 maxStoresPerMemcpyOptSize = Subtarget->isTargetDarwin() ? 8 : 4;
1313 maxStoresPerMemmove = 8; // For @llvm.memmove -> sequence of stores
1314 maxStoresPerMemmoveOptSize = Subtarget->isTargetDarwin() ? 8 : 4;
Jakob Stoklund Olesen8c741b82011-12-06 01:26:19 +00001315 setPrefLoopAlignment(4); // 2^4 bytes.
Evan Cheng6ebf7bc2009-05-13 21:42:09 +00001316 benefitFromCodePlacementOpt = true;
Eli Friedmanfc5d3052011-05-06 20:34:06 +00001317
Benjamin Krameraaf723d2012-05-05 12:49:14 +00001318 // Predictable cmov don't hurt on atom because it's in-order.
1319 predictableSelectIsExpensive = !Subtarget->isAtom();
1320
Jakob Stoklund Olesen8c741b82011-12-06 01:26:19 +00001321 setPrefFunctionAlignment(4); // 2^4 bytes.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001322}
1323
Duncan Sands28b77e92011-09-06 19:07:46 +00001324EVT X86TargetLowering::getSetCCResultType(EVT VT) const {
1325 if (!VT.isVector()) return MVT::i8;
1326 return VT.changeVectorElementTypeToInteger();
Scott Michel5b8f82e2008-03-10 15:42:14 +00001327}
1328
Evan Cheng29286502008-01-23 23:17:41 +00001329/// getMaxByValAlign - Helper for getByValTypeAlignment to determine
1330/// the desired ByVal argument alignment.
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001331static void getMaxByValAlign(Type *Ty, unsigned &MaxAlign) {
Evan Cheng29286502008-01-23 23:17:41 +00001332 if (MaxAlign == 16)
1333 return;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001334 if (VectorType *VTy = dyn_cast<VectorType>(Ty)) {
Evan Cheng29286502008-01-23 23:17:41 +00001335 if (VTy->getBitWidth() == 128)
1336 MaxAlign = 16;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001337 } else if (ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
Evan Cheng29286502008-01-23 23:17:41 +00001338 unsigned EltAlign = 0;
1339 getMaxByValAlign(ATy->getElementType(), EltAlign);
1340 if (EltAlign > MaxAlign)
1341 MaxAlign = EltAlign;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001342 } else if (StructType *STy = dyn_cast<StructType>(Ty)) {
Evan Cheng29286502008-01-23 23:17:41 +00001343 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
1344 unsigned EltAlign = 0;
1345 getMaxByValAlign(STy->getElementType(i), EltAlign);
1346 if (EltAlign > MaxAlign)
1347 MaxAlign = EltAlign;
1348 if (MaxAlign == 16)
1349 break;
1350 }
1351 }
Evan Cheng29286502008-01-23 23:17:41 +00001352}
1353
1354/// getByValTypeAlignment - Return the desired alignment for ByVal aggregate
1355/// function arguments in the caller parameter area. For X86, aggregates
Dale Johannesen0c191872008-02-08 19:48:20 +00001356/// that contain SSE vectors are placed at 16-byte boundaries while the rest
1357/// are at 4-byte boundaries.
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001358unsigned X86TargetLowering::getByValTypeAlignment(Type *Ty) const {
Evan Cheng1887c1c2008-08-21 21:00:15 +00001359 if (Subtarget->is64Bit()) {
1360 // Max of 8 and alignment of type.
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00001361 unsigned TyAlign = TD->getABITypeAlignment(Ty);
Evan Cheng1887c1c2008-08-21 21:00:15 +00001362 if (TyAlign > 8)
1363 return TyAlign;
1364 return 8;
1365 }
1366
Evan Cheng29286502008-01-23 23:17:41 +00001367 unsigned Align = 4;
Craig Topper1accb7e2012-01-10 06:54:16 +00001368 if (Subtarget->hasSSE1())
Dale Johannesen0c191872008-02-08 19:48:20 +00001369 getMaxByValAlign(Ty, Align);
Evan Cheng29286502008-01-23 23:17:41 +00001370 return Align;
1371}
Chris Lattner2b02a442007-02-25 08:29:00 +00001372
Evan Chengf0df0312008-05-15 08:39:06 +00001373/// getOptimalMemOpType - Returns the target specific optimal type for load
Evan Chengc3b0c342010-04-08 07:37:57 +00001374/// and store operations as a result of memset, memcpy, and memmove
1375/// lowering. If DstAlign is zero that means it's safe to destination
1376/// alignment can satisfy any constraint. Similarly if SrcAlign is zero it
1377/// means there isn't a need to check it against alignment requirement,
Evan Cheng946a3a92012-12-12 02:34:41 +00001378/// probably because the source does not need to be loaded. If 'IsMemset' is
1379/// true, that means it's expanding a memset. If 'ZeroMemset' is true, that
1380/// means it's a memset of zero. 'MemcpyStrSrc' indicates whether the memcpy
1381/// source is constant so it does not need to be loaded.
Dan Gohman37f32ee2010-04-16 20:11:05 +00001382/// It returns EVT::Other if the type should be determined using generic
1383/// target-independent logic.
Owen Andersone50ed302009-08-10 22:56:29 +00001384EVT
Evan Cheng255f20f2010-04-01 06:04:33 +00001385X86TargetLowering::getOptimalMemOpType(uint64_t Size,
1386 unsigned DstAlign, unsigned SrcAlign,
Evan Cheng946a3a92012-12-12 02:34:41 +00001387 bool IsMemset, bool ZeroMemset,
Evan Chengc3b0c342010-04-08 07:37:57 +00001388 bool MemcpyStrSrc,
Dan Gohman37f32ee2010-04-16 20:11:05 +00001389 MachineFunction &MF) const {
Dan Gohman37f32ee2010-04-16 20:11:05 +00001390 const Function *F = MF.getFunction();
Evan Cheng946a3a92012-12-12 02:34:41 +00001391 if ((!IsMemset || ZeroMemset) &&
Bill Wendling034b94b2012-12-19 07:18:57 +00001392 !F->getFnAttributes().hasAttribute(Attribute::NoImplicitFloat)) {
Evan Cheng255f20f2010-04-01 06:04:33 +00001393 if (Size >= 16 &&
Evan Chenga5e13622011-01-07 19:35:30 +00001394 (Subtarget->isUnalignedMemAccessFast() ||
1395 ((DstAlign == 0 || DstAlign >= 16) &&
Benjamin Kramer2dbe9292012-11-14 20:08:40 +00001396 (SrcAlign == 0 || SrcAlign >= 16)))) {
1397 if (Size >= 32) {
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00001398 if (Subtarget->hasInt256())
Craig Topper562659f2012-01-13 08:32:21 +00001399 return MVT::v8i32;
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00001400 if (Subtarget->hasFp256())
Craig Topper562659f2012-01-13 08:32:21 +00001401 return MVT::v8f32;
1402 }
Craig Topper1accb7e2012-01-10 06:54:16 +00001403 if (Subtarget->hasSSE2())
Evan Cheng255f20f2010-04-01 06:04:33 +00001404 return MVT::v4i32;
Craig Topper1accb7e2012-01-10 06:54:16 +00001405 if (Subtarget->hasSSE1())
Evan Cheng255f20f2010-04-01 06:04:33 +00001406 return MVT::v4f32;
Evan Chengc3b0c342010-04-08 07:37:57 +00001407 } else if (!MemcpyStrSrc && Size >= 8 &&
Evan Cheng3ea97552010-04-01 20:27:45 +00001408 !Subtarget->is64Bit() &&
Craig Topper1accb7e2012-01-10 06:54:16 +00001409 Subtarget->hasSSE2()) {
Evan Chengc3b0c342010-04-08 07:37:57 +00001410 // Do not use f64 to lower memcpy if source is string constant. It's
1411 // better to use i32 to avoid the loads.
Evan Cheng255f20f2010-04-01 06:04:33 +00001412 return MVT::f64;
Evan Chengc3b0c342010-04-08 07:37:57 +00001413 }
Chris Lattner4002a1b2008-10-28 05:49:35 +00001414 }
Evan Chengf0df0312008-05-15 08:39:06 +00001415 if (Subtarget->is64Bit() && Size >= 8)
Owen Anderson825b72b2009-08-11 20:47:22 +00001416 return MVT::i64;
1417 return MVT::i32;
Evan Chengf0df0312008-05-15 08:39:06 +00001418}
1419
Evan Cheng7d342672012-12-12 01:32:07 +00001420bool X86TargetLowering::isSafeMemOpType(MVT VT) const {
Evan Cheng61f4dfe2012-12-12 00:42:09 +00001421 if (VT == MVT::f32)
1422 return X86ScalarSSEf32;
1423 else if (VT == MVT::f64)
1424 return X86ScalarSSEf64;
Evan Cheng7d342672012-12-12 01:32:07 +00001425 return true;
Evan Cheng61f4dfe2012-12-12 00:42:09 +00001426}
1427
Evan Cheng376642e2012-12-10 23:21:26 +00001428bool
1429X86TargetLowering::allowsUnalignedMemoryAccesses(EVT VT, bool *Fast) const {
1430 if (Fast)
1431 *Fast = Subtarget->isUnalignedMemAccessFast();
1432 return true;
1433}
1434
Chris Lattner5e1df8d2010-01-25 23:38:14 +00001435/// getJumpTableEncoding - Return the entry encoding for a jump table in the
1436/// current function. The returned value is a member of the
1437/// MachineJumpTableInfo::JTEntryKind enum.
1438unsigned X86TargetLowering::getJumpTableEncoding() const {
1439 // In GOT pic mode, each entry in the jump table is emitted as a @GOTOFF
1440 // symbol.
1441 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1442 Subtarget->isPICStyleGOT())
Chris Lattnerc64daab2010-01-26 05:02:42 +00001443 return MachineJumpTableInfo::EK_Custom32;
Michael J. Spencerec38de22010-10-10 22:04:20 +00001444
Chris Lattner5e1df8d2010-01-25 23:38:14 +00001445 // Otherwise, use the normal jump table encoding heuristics.
1446 return TargetLowering::getJumpTableEncoding();
1447}
1448
Chris Lattnerc64daab2010-01-26 05:02:42 +00001449const MCExpr *
1450X86TargetLowering::LowerCustomJumpTableEntry(const MachineJumpTableInfo *MJTI,
1451 const MachineBasicBlock *MBB,
1452 unsigned uid,MCContext &Ctx) const{
1453 assert(getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1454 Subtarget->isPICStyleGOT());
1455 // In 32-bit ELF systems, our jump table entries are formed with @GOTOFF
1456 // entries.
Daniel Dunbar4e815f82010-03-15 23:51:06 +00001457 return MCSymbolRefExpr::Create(MBB->getSymbol(),
1458 MCSymbolRefExpr::VK_GOTOFF, Ctx);
Chris Lattnerc64daab2010-01-26 05:02:42 +00001459}
1460
Evan Chengcc415862007-11-09 01:32:10 +00001461/// getPICJumpTableRelocaBase - Returns relocation base for the given PIC
1462/// jumptable.
Dan Gohman475871a2008-07-27 21:46:04 +00001463SDValue X86TargetLowering::getPICJumpTableRelocBase(SDValue Table,
Chris Lattner589c6f62010-01-26 06:28:43 +00001464 SelectionDAG &DAG) const {
Chris Lattnere4df7562009-07-09 03:15:51 +00001465 if (!Subtarget->is64Bit())
Dale Johannesenb300d2a2009-02-07 00:55:49 +00001466 // This doesn't have DebugLoc associated with it, but is not really the
1467 // same as a Register.
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00001468 return DAG.getNode(X86ISD::GlobalBaseReg, DebugLoc(), getPointerTy());
Evan Chengcc415862007-11-09 01:32:10 +00001469 return Table;
1470}
1471
Chris Lattner589c6f62010-01-26 06:28:43 +00001472/// getPICJumpTableRelocBaseExpr - This returns the relocation base for the
1473/// given PIC jumptable, the same as getPICJumpTableRelocBase, but as an
1474/// MCExpr.
1475const MCExpr *X86TargetLowering::
1476getPICJumpTableRelocBaseExpr(const MachineFunction *MF, unsigned JTI,
1477 MCContext &Ctx) const {
1478 // X86-64 uses RIP relative addressing based on the jump table label.
1479 if (Subtarget->isPICStyleRIPRel())
1480 return TargetLowering::getPICJumpTableRelocBaseExpr(MF, JTI, Ctx);
1481
1482 // Otherwise, the reference is relative to the PIC base.
Chris Lattner142b5312010-11-14 22:48:15 +00001483 return MCSymbolRefExpr::Create(MF->getPICBaseSymbol(), Ctx);
Chris Lattner589c6f62010-01-26 06:28:43 +00001484}
1485
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00001486// FIXME: Why this routine is here? Move to RegInfo!
Evan Chengdee81012010-07-26 21:50:05 +00001487std::pair<const TargetRegisterClass*, uint8_t>
Patrik Hagglund03405572012-12-19 11:30:36 +00001488X86TargetLowering::findRepresentativeClass(MVT VT) const{
Evan Chengdee81012010-07-26 21:50:05 +00001489 const TargetRegisterClass *RRC = 0;
1490 uint8_t Cost = 1;
Patrik Hagglund03405572012-12-19 11:30:36 +00001491 switch (VT.SimpleTy) {
Evan Chengdee81012010-07-26 21:50:05 +00001492 default:
1493 return TargetLowering::findRepresentativeClass(VT);
1494 case MVT::i8: case MVT::i16: case MVT::i32: case MVT::i64:
Craig Topperc9099502012-04-20 06:31:50 +00001495 RRC = Subtarget->is64Bit() ?
1496 (const TargetRegisterClass*)&X86::GR64RegClass :
1497 (const TargetRegisterClass*)&X86::GR32RegClass;
Evan Chengdee81012010-07-26 21:50:05 +00001498 break;
Dale Johannesen0488fb62010-09-30 23:57:10 +00001499 case MVT::x86mmx:
Craig Topperc9099502012-04-20 06:31:50 +00001500 RRC = &X86::VR64RegClass;
Evan Chengdee81012010-07-26 21:50:05 +00001501 break;
1502 case MVT::f32: case MVT::f64:
1503 case MVT::v16i8: case MVT::v8i16: case MVT::v4i32: case MVT::v2i64:
1504 case MVT::v4f32: case MVT::v2f64:
1505 case MVT::v32i8: case MVT::v8i32: case MVT::v4i64: case MVT::v8f32:
1506 case MVT::v4f64:
Craig Topperc9099502012-04-20 06:31:50 +00001507 RRC = &X86::VR128RegClass;
Evan Chengdee81012010-07-26 21:50:05 +00001508 break;
1509 }
1510 return std::make_pair(RRC, Cost);
1511}
1512
Eric Christopherf7a0c7b2010-07-06 05:18:56 +00001513bool X86TargetLowering::getStackCookieLocation(unsigned &AddressSpace,
1514 unsigned &Offset) const {
1515 if (!Subtarget->isTargetLinux())
1516 return false;
1517
1518 if (Subtarget->is64Bit()) {
1519 // %fs:0x28, unless we're using a Kernel code model, in which case it's %gs:
1520 Offset = 0x28;
1521 if (getTargetMachine().getCodeModel() == CodeModel::Kernel)
1522 AddressSpace = 256;
1523 else
1524 AddressSpace = 257;
1525 } else {
1526 // %gs:0x14 on i386
1527 Offset = 0x14;
1528 AddressSpace = 256;
1529 }
1530 return true;
1531}
1532
Chris Lattner2b02a442007-02-25 08:29:00 +00001533//===----------------------------------------------------------------------===//
1534// Return Value Calling Convention Implementation
1535//===----------------------------------------------------------------------===//
1536
Chris Lattner59ed56b2007-02-28 04:55:35 +00001537#include "X86GenCallingConv.inc"
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001538
Michael J. Spencerec38de22010-10-10 22:04:20 +00001539bool
Eric Christopher471e4222011-06-08 23:55:35 +00001540X86TargetLowering::CanLowerReturn(CallingConv::ID CallConv,
Craig Topper0fbf3642012-04-23 03:28:34 +00001541 MachineFunction &MF, bool isVarArg,
Dan Gohman84023e02010-07-10 09:00:22 +00001542 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9af33c2010-07-06 22:19:37 +00001543 LLVMContext &Context) const {
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +00001544 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00001545 CCState CCInfo(CallConv, isVarArg, MF, getTargetMachine(),
Dan Gohmanc9af33c2010-07-06 22:19:37 +00001546 RVLocs, Context);
Dan Gohman84023e02010-07-10 09:00:22 +00001547 return CCInfo.CheckReturn(Outs, RetCC_X86);
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +00001548}
1549
Dan Gohman98ca4f22009-08-05 01:29:28 +00001550SDValue
1551X86TargetLowering::LowerReturn(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001552 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001553 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001554 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohmand858e902010-04-17 15:26:15 +00001555 DebugLoc dl, SelectionDAG &DAG) const {
Dan Gohman1e93df62010-04-17 14:41:14 +00001556 MachineFunction &MF = DAG.getMachineFunction();
1557 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
Scott Michelfdc40a02009-02-17 22:15:04 +00001558
Chris Lattner9774c912007-02-27 05:28:59 +00001559 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00001560 CCState CCInfo(CallConv, isVarArg, MF, getTargetMachine(),
Dan Gohman98ca4f22009-08-05 01:29:28 +00001561 RVLocs, *DAG.getContext());
1562 CCInfo.AnalyzeReturn(Outs, RetCC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001563
Evan Chengdcea1632010-02-04 02:40:39 +00001564 // Add the regs to the liveout set for the function.
1565 MachineRegisterInfo &MRI = DAG.getMachineFunction().getRegInfo();
1566 for (unsigned i = 0; i != RVLocs.size(); ++i)
1567 if (RVLocs[i].isRegLoc() && !MRI.isLiveOut(RVLocs[i].getLocReg()))
1568 MRI.addLiveOut(RVLocs[i].getLocReg());
Scott Michelfdc40a02009-02-17 22:15:04 +00001569
Dan Gohman475871a2008-07-27 21:46:04 +00001570 SDValue Flag;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001571
Dan Gohman475871a2008-07-27 21:46:04 +00001572 SmallVector<SDValue, 6> RetOps;
Chris Lattner447ff682008-03-11 03:23:40 +00001573 RetOps.push_back(Chain); // Operand #0 = Chain (updated below)
1574 // Operand #1 = Bytes To Pop
Dan Gohman1e93df62010-04-17 14:41:14 +00001575 RetOps.push_back(DAG.getTargetConstant(FuncInfo->getBytesToPopOnReturn(),
1576 MVT::i16));
Scott Michelfdc40a02009-02-17 22:15:04 +00001577
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001578 // Copy the result values into the output registers.
Chris Lattner8e6da152008-03-10 21:08:41 +00001579 for (unsigned i = 0; i != RVLocs.size(); ++i) {
1580 CCValAssign &VA = RVLocs[i];
1581 assert(VA.isRegLoc() && "Can only return in registers!");
Dan Gohmanc9403652010-07-07 15:54:55 +00001582 SDValue ValToCopy = OutVals[i];
Dale Johannesenc76d23f2010-07-23 00:30:35 +00001583 EVT ValVT = ValToCopy.getValueType();
1584
Jakob Stoklund Olesenee66b412012-05-31 17:28:20 +00001585 // Promote values to the appropriate types
1586 if (VA.getLocInfo() == CCValAssign::SExt)
1587 ValToCopy = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), ValToCopy);
1588 else if (VA.getLocInfo() == CCValAssign::ZExt)
1589 ValToCopy = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), ValToCopy);
1590 else if (VA.getLocInfo() == CCValAssign::AExt)
1591 ValToCopy = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), ValToCopy);
1592 else if (VA.getLocInfo() == CCValAssign::BCvt)
1593 ValToCopy = DAG.getNode(ISD::BITCAST, dl, VA.getLocVT(), ValToCopy);
1594
Dale Johannesenc4510512010-09-24 19:05:48 +00001595 // If this is x86-64, and we disabled SSE, we can't return FP values,
1596 // or SSE or MMX vectors.
1597 if ((ValVT == MVT::f32 || ValVT == MVT::f64 ||
1598 VA.getLocReg() == X86::XMM0 || VA.getLocReg() == X86::XMM1) &&
Craig Topper1accb7e2012-01-10 06:54:16 +00001599 (Subtarget->is64Bit() && !Subtarget->hasSSE1())) {
Dale Johannesenc76d23f2010-07-23 00:30:35 +00001600 report_fatal_error("SSE register return with SSE disabled");
1601 }
1602 // Likewise we can't return F64 values with SSE1 only. gcc does so, but
1603 // llvm-gcc has never done it right and no one has noticed, so this
1604 // should be OK for now.
1605 if (ValVT == MVT::f64 &&
Craig Topper1accb7e2012-01-10 06:54:16 +00001606 (Subtarget->is64Bit() && !Subtarget->hasSSE2()))
Dale Johannesenc76d23f2010-07-23 00:30:35 +00001607 report_fatal_error("SSE2 register return with SSE2 disabled");
Scott Michelfdc40a02009-02-17 22:15:04 +00001608
Chris Lattner447ff682008-03-11 03:23:40 +00001609 // Returns in ST0/ST1 are handled specially: these are pushed as operands to
1610 // the RET instruction and handled by the FP Stackifier.
Dan Gohman37eed792009-02-04 17:28:58 +00001611 if (VA.getLocReg() == X86::ST0 ||
1612 VA.getLocReg() == X86::ST1) {
Chris Lattner447ff682008-03-11 03:23:40 +00001613 // If this is a copy from an xmm register to ST(0), use an FPExtend to
1614 // change the value to the FP stack register class.
Dan Gohman37eed792009-02-04 17:28:58 +00001615 if (isScalarFPTypeInSSEReg(VA.getValVT()))
Owen Anderson825b72b2009-08-11 20:47:22 +00001616 ValToCopy = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f80, ValToCopy);
Chris Lattner447ff682008-03-11 03:23:40 +00001617 RetOps.push_back(ValToCopy);
1618 // Don't emit a copytoreg.
1619 continue;
1620 }
Dale Johannesena68f9012008-06-24 22:01:44 +00001621
Evan Cheng242b38b2009-02-23 09:03:22 +00001622 // 64-bit vector (MMX) values are returned in XMM0 / XMM1 except for v1i64
1623 // which is returned in RAX / RDX.
Evan Cheng6140a8b2009-02-22 08:05:12 +00001624 if (Subtarget->is64Bit()) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00001625 if (ValVT == MVT::x86mmx) {
Chris Lattner97a2a562010-08-26 05:24:29 +00001626 if (VA.getLocReg() == X86::XMM0 || VA.getLocReg() == X86::XMM1) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001627 ValToCopy = DAG.getNode(ISD::BITCAST, dl, MVT::i64, ValToCopy);
Eric Christopher90eb4022010-07-22 00:26:08 +00001628 ValToCopy = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i64,
1629 ValToCopy);
Chris Lattner97a2a562010-08-26 05:24:29 +00001630 // If we don't have SSE2 available, convert to v4f32 so the generated
1631 // register is legal.
Craig Topper1accb7e2012-01-10 06:54:16 +00001632 if (!Subtarget->hasSSE2())
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001633 ValToCopy = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32,ValToCopy);
Chris Lattner97a2a562010-08-26 05:24:29 +00001634 }
Evan Cheng242b38b2009-02-23 09:03:22 +00001635 }
Evan Cheng6140a8b2009-02-22 08:05:12 +00001636 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00001637
Dale Johannesendd64c412009-02-04 00:33:20 +00001638 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), ValToCopy, Flag);
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001639 Flag = Chain.getValue(1);
1640 }
Dan Gohman61a92132008-04-21 23:59:07 +00001641
1642 // The x86-64 ABI for returning structs by value requires that we copy
1643 // the sret argument into %rax for the return. We saved the argument into
1644 // a virtual register in the entry block, so now we copy the value out
1645 // and into %rax.
1646 if (Subtarget->is64Bit() &&
1647 DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
1648 MachineFunction &MF = DAG.getMachineFunction();
1649 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1650 unsigned Reg = FuncInfo->getSRetReturnReg();
Michael J. Spencerec38de22010-10-10 22:04:20 +00001651 assert(Reg &&
Zhongxing Xuc2798a12010-05-26 08:10:02 +00001652 "SRetReturnReg should have been set in LowerFormalArguments().");
Dale Johannesendd64c412009-02-04 00:33:20 +00001653 SDValue Val = DAG.getCopyFromReg(Chain, dl, Reg, getPointerTy());
Dan Gohman61a92132008-04-21 23:59:07 +00001654
Dale Johannesendd64c412009-02-04 00:33:20 +00001655 Chain = DAG.getCopyToReg(Chain, dl, X86::RAX, Val, Flag);
Dan Gohman61a92132008-04-21 23:59:07 +00001656 Flag = Chain.getValue(1);
Dan Gohman00326812009-10-12 16:36:12 +00001657
1658 // RAX now acts like a return value.
Evan Chengdcea1632010-02-04 02:40:39 +00001659 MRI.addLiveOut(X86::RAX);
Dan Gohman61a92132008-04-21 23:59:07 +00001660 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001661
Chris Lattner447ff682008-03-11 03:23:40 +00001662 RetOps[0] = Chain; // Update chain.
1663
1664 // Add the flag if we have it.
Gabor Greifba36cb52008-08-28 21:40:38 +00001665 if (Flag.getNode())
Chris Lattner447ff682008-03-11 03:23:40 +00001666 RetOps.push_back(Flag);
Scott Michelfdc40a02009-02-17 22:15:04 +00001667
1668 return DAG.getNode(X86ISD::RET_FLAG, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001669 MVT::Other, &RetOps[0], RetOps.size());
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001670}
1671
Evan Chengbf010eb2012-04-10 01:51:00 +00001672bool X86TargetLowering::isUsedByReturnOnly(SDNode *N, SDValue &Chain) const {
Evan Cheng3d2125c2010-11-30 23:55:39 +00001673 if (N->getNumValues() != 1)
1674 return false;
1675 if (!N->hasNUsesOfValue(1, 0))
1676 return false;
1677
Evan Chengbf010eb2012-04-10 01:51:00 +00001678 SDValue TCChain = Chain;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001679 SDNode *Copy = *N->use_begin();
Chad Rosierc8d7eea2012-03-05 19:27:12 +00001680 if (Copy->getOpcode() == ISD::CopyToReg) {
1681 // If the copy has a glue operand, we conservatively assume it isn't safe to
1682 // perform a tail call.
1683 if (Copy->getOperand(Copy->getNumOperands()-1).getValueType() == MVT::Glue)
1684 return false;
Evan Chengbf010eb2012-04-10 01:51:00 +00001685 TCChain = Copy->getOperand(0);
Chad Rosierc8d7eea2012-03-05 19:27:12 +00001686 } else if (Copy->getOpcode() != ISD::FP_EXTEND)
Chad Rosier74bab7f2012-03-02 02:50:46 +00001687 return false;
1688
Evan Cheng1bf891a2010-12-01 22:59:46 +00001689 bool HasRet = false;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001690 for (SDNode::use_iterator UI = Copy->use_begin(), UE = Copy->use_end();
Evan Cheng1bf891a2010-12-01 22:59:46 +00001691 UI != UE; ++UI) {
Evan Cheng3d2125c2010-11-30 23:55:39 +00001692 if (UI->getOpcode() != X86ISD::RET_FLAG)
1693 return false;
Evan Cheng1bf891a2010-12-01 22:59:46 +00001694 HasRet = true;
1695 }
Evan Cheng3d2125c2010-11-30 23:55:39 +00001696
Evan Chengbf010eb2012-04-10 01:51:00 +00001697 if (!HasRet)
1698 return false;
1699
1700 Chain = TCChain;
1701 return true;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001702}
1703
Patrik Hagglunde5c65912012-12-19 12:02:25 +00001704MVT
1705X86TargetLowering::getTypeForExtArgOrReturn(MVT VT,
Cameron Zwarich44579682011-03-17 14:21:56 +00001706 ISD::NodeType ExtendKind) const {
Cameron Zwarich7bbf0ee2011-03-17 14:53:37 +00001707 MVT ReturnMVT;
Cameron Zwarichebe81732011-03-16 22:20:18 +00001708 // TODO: Is this also valid on 32-bit?
1709 if (Subtarget->is64Bit() && VT == MVT::i1 && ExtendKind == ISD::ZERO_EXTEND)
Cameron Zwarich7bbf0ee2011-03-17 14:53:37 +00001710 ReturnMVT = MVT::i8;
1711 else
1712 ReturnMVT = MVT::i32;
1713
Patrik Hagglunde5c65912012-12-19 12:02:25 +00001714 MVT MinVT = getRegisterType(ReturnMVT);
Cameron Zwarich7bbf0ee2011-03-17 14:53:37 +00001715 return VT.bitsLT(MinVT) ? MinVT : VT;
Cameron Zwarichebe81732011-03-16 22:20:18 +00001716}
1717
Dan Gohman98ca4f22009-08-05 01:29:28 +00001718/// LowerCallResult - Lower the result values of a call into the
1719/// appropriate copies out of appropriate physical registers.
1720///
1721SDValue
1722X86TargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001723 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001724 const SmallVectorImpl<ISD::InputArg> &Ins,
1725 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001726 SmallVectorImpl<SDValue> &InVals) const {
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001727
Chris Lattnere32bbf62007-02-28 07:09:55 +00001728 // Assign locations to each value returned by this call.
Chris Lattner9774c912007-02-27 05:28:59 +00001729 SmallVector<CCValAssign, 16> RVLocs;
Torok Edwin3f142c32009-02-01 18:15:56 +00001730 bool Is64Bit = Subtarget->is64Bit();
Eric Christopher471e4222011-06-08 23:55:35 +00001731 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
Craig Topper0fbf3642012-04-23 03:28:34 +00001732 getTargetMachine(), RVLocs, *DAG.getContext());
Dan Gohman98ca4f22009-08-05 01:29:28 +00001733 CCInfo.AnalyzeCallResult(Ins, RetCC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001734
Chris Lattner3085e152007-02-25 08:59:22 +00001735 // Copy all of the result registers out of their specified physreg.
Jakub Staszakc20323a2012-12-29 15:57:26 +00001736 for (unsigned i = 0, e = RVLocs.size(); i != e; ++i) {
Dan Gohman37eed792009-02-04 17:28:58 +00001737 CCValAssign &VA = RVLocs[i];
Owen Andersone50ed302009-08-10 22:56:29 +00001738 EVT CopyVT = VA.getValVT();
Scott Michelfdc40a02009-02-17 22:15:04 +00001739
Torok Edwin3f142c32009-02-01 18:15:56 +00001740 // If this is x86-64, and we disabled SSE, we can't return FP values
Owen Anderson825b72b2009-08-11 20:47:22 +00001741 if ((CopyVT == MVT::f32 || CopyVT == MVT::f64) &&
Craig Topper1accb7e2012-01-10 06:54:16 +00001742 ((Is64Bit || Ins[i].Flags.isInReg()) && !Subtarget->hasSSE1())) {
Chris Lattner75361b62010-04-07 22:58:41 +00001743 report_fatal_error("SSE register return with SSE disabled");
Torok Edwin3f142c32009-02-01 18:15:56 +00001744 }
1745
Evan Cheng79fb3b42009-02-20 20:43:02 +00001746 SDValue Val;
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001747
1748 // If this is a call to a function that returns an fp value on the floating
Sylvestre Ledruc8e41c52012-07-23 08:51:15 +00001749 // point stack, we must guarantee the value is popped from the stack, so
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001750 // a CopyFromReg is not good enough - the copy instruction may be eliminated
Jakob Stoklund Olesen9bbe4d62011-06-28 18:32:28 +00001751 // if the return value is not used. We use the FpPOP_RETVAL instruction
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001752 // instead.
1753 if (VA.getLocReg() == X86::ST0 || VA.getLocReg() == X86::ST1) {
1754 // If we prefer to use the value in xmm registers, copy it out as f80 and
1755 // use a truncate to move it from fp stack reg to xmm reg.
1756 if (isScalarFPTypeInSSEReg(VA.getValVT())) CopyVT = MVT::f80;
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001757 SDValue Ops[] = { Chain, InFlag };
Jakob Stoklund Olesen9bbe4d62011-06-28 18:32:28 +00001758 Chain = SDValue(DAG.getMachineNode(X86::FpPOP_RETVAL, dl, CopyVT,
1759 MVT::Other, MVT::Glue, Ops, 2), 1);
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001760 Val = Chain.getValue(0);
1761
1762 // Round the f80 to the right size, which also moves it to the appropriate
1763 // xmm register.
1764 if (CopyVT != VA.getValVT())
1765 Val = DAG.getNode(ISD::FP_ROUND, dl, VA.getValVT(), Val,
1766 // This truncation won't change the value.
1767 DAG.getIntPtrConstant(1));
Evan Cheng79fb3b42009-02-20 20:43:02 +00001768 } else {
1769 Chain = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(),
1770 CopyVT, InFlag).getValue(1);
1771 Val = Chain.getValue(0);
1772 }
Chris Lattner8e6da152008-03-10 21:08:41 +00001773 InFlag = Chain.getValue(2);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001774 InVals.push_back(Val);
Chris Lattner3085e152007-02-25 08:59:22 +00001775 }
Duncan Sands4bdcb612008-07-02 17:40:58 +00001776
Dan Gohman98ca4f22009-08-05 01:29:28 +00001777 return Chain;
Chris Lattner2b02a442007-02-25 08:29:00 +00001778}
1779
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001780//===----------------------------------------------------------------------===//
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001781// C & StdCall & Fast Calling Convention implementation
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001782//===----------------------------------------------------------------------===//
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001783// StdCall calling convention seems to be standard for many Windows' API
1784// routines and around. It differs from C calling convention just a little:
1785// callee should clean up the stack, not caller. Symbols should be also
1786// decorated in some fancy way :) It doesn't support any vector arguments.
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001787// For info on fast calling convention see Fast Calling Convention (tail call)
1788// implementation LowerX86_32FastCCCallTo.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001789
Dan Gohman98ca4f22009-08-05 01:29:28 +00001790/// CallIsStructReturn - Determines whether a call uses struct return
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001791/// semantics.
Rafael Espindola1cee7102012-07-25 13:41:10 +00001792enum StructReturnType {
1793 NotStructReturn,
1794 RegStructReturn,
1795 StackStructReturn
1796};
1797static StructReturnType
1798callIsStructReturn(const SmallVectorImpl<ISD::OutputArg> &Outs) {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001799 if (Outs.empty())
Rafael Espindola1cee7102012-07-25 13:41:10 +00001800 return NotStructReturn;
Duncan Sands276dcbd2008-03-21 09:14:45 +00001801
Rafael Espindola1cee7102012-07-25 13:41:10 +00001802 const ISD::ArgFlagsTy &Flags = Outs[0].Flags;
1803 if (!Flags.isSRet())
1804 return NotStructReturn;
1805 if (Flags.isInReg())
1806 return RegStructReturn;
1807 return StackStructReturn;
Gordon Henriksen86737662008-01-05 16:56:59 +00001808}
1809
Dan Gohman7e77b0f2009-08-01 19:14:37 +00001810/// ArgsAreStructReturn - Determines whether a function uses struct
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001811/// return semantics.
Rafael Espindola1cee7102012-07-25 13:41:10 +00001812static StructReturnType
1813argsAreStructReturn(const SmallVectorImpl<ISD::InputArg> &Ins) {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001814 if (Ins.empty())
Rafael Espindola1cee7102012-07-25 13:41:10 +00001815 return NotStructReturn;
Duncan Sands276dcbd2008-03-21 09:14:45 +00001816
Rafael Espindola1cee7102012-07-25 13:41:10 +00001817 const ISD::ArgFlagsTy &Flags = Ins[0].Flags;
1818 if (!Flags.isSRet())
1819 return NotStructReturn;
1820 if (Flags.isInReg())
1821 return RegStructReturn;
1822 return StackStructReturn;
Gordon Henriksen86737662008-01-05 16:56:59 +00001823}
1824
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001825/// CreateCopyOfByValArgument - Make a copy of an aggregate at address specified
1826/// by "Src" to address "Dst" with size and alignment information specified by
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001827/// the specific parameter attribute. The copy will be passed as a byval
1828/// function parameter.
Scott Michelfdc40a02009-02-17 22:15:04 +00001829static SDValue
Dan Gohman475871a2008-07-27 21:46:04 +00001830CreateCopyOfByValArgument(SDValue Src, SDValue Dst, SDValue Chain,
Dale Johannesendd64c412009-02-04 00:33:20 +00001831 ISD::ArgFlagsTy Flags, SelectionDAG &DAG,
1832 DebugLoc dl) {
Chris Lattnere72f2022010-09-21 05:40:29 +00001833 SDValue SizeNode = DAG.getConstant(Flags.getByValSize(), MVT::i32);
Michael J. Spencerec38de22010-10-10 22:04:20 +00001834
Dale Johannesendd64c412009-02-04 00:33:20 +00001835 return DAG.getMemcpy(Chain, dl, Dst, Src, SizeNode, Flags.getByValAlign(),
Stuart Hastings03d58262011-03-10 00:25:53 +00001836 /*isVolatile*/false, /*AlwaysInline=*/true,
Chris Lattnerfc448ff2010-09-21 18:51:21 +00001837 MachinePointerInfo(), MachinePointerInfo());
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001838}
1839
Chris Lattner29689432010-03-11 00:22:57 +00001840/// IsTailCallConvention - Return true if the calling convention is one that
1841/// supports tail call optimization.
1842static bool IsTailCallConvention(CallingConv::ID CC) {
Duncan Sandsdc7f1742012-11-16 12:36:39 +00001843 return (CC == CallingConv::Fast || CC == CallingConv::GHC ||
1844 CC == CallingConv::HiPE);
Chris Lattner29689432010-03-11 00:22:57 +00001845}
1846
Evan Cheng485fafc2011-03-21 01:19:09 +00001847bool X86TargetLowering::mayBeEmittedAsTailCall(CallInst *CI) const {
Nick Lewycky22de16d2012-01-19 00:34:10 +00001848 if (!CI->isTailCall() || getTargetMachine().Options.DisableTailCalls)
Evan Cheng485fafc2011-03-21 01:19:09 +00001849 return false;
1850
1851 CallSite CS(CI);
1852 CallingConv::ID CalleeCC = CS.getCallingConv();
1853 if (!IsTailCallConvention(CalleeCC) && CalleeCC != CallingConv::C)
1854 return false;
1855
1856 return true;
1857}
1858
Evan Cheng0c439eb2010-01-27 00:07:07 +00001859/// FuncIsMadeTailCallSafe - Return true if the function is being made into
1860/// a tailcall target by changing its ABI.
Nick Lewycky8a8d4792011-12-02 22:16:29 +00001861static bool FuncIsMadeTailCallSafe(CallingConv::ID CC,
1862 bool GuaranteedTailCallOpt) {
Chris Lattner29689432010-03-11 00:22:57 +00001863 return GuaranteedTailCallOpt && IsTailCallConvention(CC);
Evan Cheng0c439eb2010-01-27 00:07:07 +00001864}
1865
Dan Gohman98ca4f22009-08-05 01:29:28 +00001866SDValue
1867X86TargetLowering::LowerMemArgument(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001868 CallingConv::ID CallConv,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001869 const SmallVectorImpl<ISD::InputArg> &Ins,
1870 DebugLoc dl, SelectionDAG &DAG,
1871 const CCValAssign &VA,
1872 MachineFrameInfo *MFI,
Dan Gohmand858e902010-04-17 15:26:15 +00001873 unsigned i) const {
Rafael Espindola7effac52007-09-14 15:48:13 +00001874 // Create the nodes corresponding to a load from this parameter slot.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001875 ISD::ArgFlagsTy Flags = Ins[i].Flags;
Nick Lewycky8a8d4792011-12-02 22:16:29 +00001876 bool AlwaysUseMutable = FuncIsMadeTailCallSafe(CallConv,
1877 getTargetMachine().Options.GuaranteedTailCallOpt);
Duncan Sands276dcbd2008-03-21 09:14:45 +00001878 bool isImmutable = !AlwaysUseMutable && !Flags.isByVal();
Anton Korobeynikov22472762009-08-14 18:19:10 +00001879 EVT ValVT;
1880
1881 // If value is passed by pointer we have address passed instead of the value
1882 // itself.
1883 if (VA.getLocInfo() == CCValAssign::Indirect)
1884 ValVT = VA.getLocVT();
1885 else
1886 ValVT = VA.getValVT();
Evan Chenge70bb592008-01-10 02:24:25 +00001887
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001888 // FIXME: For now, all byval parameter objects are marked mutable. This can be
Scott Michelfdc40a02009-02-17 22:15:04 +00001889 // changed with more analysis.
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001890 // In case of tail call optimization mark all arguments mutable. Since they
1891 // could be overwritten by lowering of arguments in case of a tail call.
Evan Cheng90567c32010-02-02 23:58:13 +00001892 if (Flags.isByVal()) {
Evan Chengee2e0e32011-03-30 23:44:13 +00001893 unsigned Bytes = Flags.getByValSize();
1894 if (Bytes == 0) Bytes = 1; // Don't create zero-sized stack objects.
1895 int FI = MFI->CreateFixedObject(Bytes, VA.getLocMemOffset(), isImmutable);
Evan Cheng90567c32010-02-02 23:58:13 +00001896 return DAG.getFrameIndex(FI, getPointerTy());
1897 } else {
1898 int FI = MFI->CreateFixedObject(ValVT.getSizeInBits()/8,
Evan Chenged2ae132010-07-03 00:40:23 +00001899 VA.getLocMemOffset(), isImmutable);
Evan Cheng90567c32010-02-02 23:58:13 +00001900 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
1901 return DAG.getLoad(ValVT, dl, Chain, FIN,
Chris Lattnere8639032010-09-21 06:22:23 +00001902 MachinePointerInfo::getFixedStack(FI),
Pete Cooperd752e0f2011-11-08 18:42:53 +00001903 false, false, false, 0);
Evan Cheng90567c32010-02-02 23:58:13 +00001904 }
Rafael Espindola7effac52007-09-14 15:48:13 +00001905}
1906
Dan Gohman475871a2008-07-27 21:46:04 +00001907SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00001908X86TargetLowering::LowerFormalArguments(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001909 CallingConv::ID CallConv,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001910 bool isVarArg,
1911 const SmallVectorImpl<ISD::InputArg> &Ins,
1912 DebugLoc dl,
1913 SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001914 SmallVectorImpl<SDValue> &InVals)
1915 const {
Evan Cheng1bc78042006-04-26 01:20:17 +00001916 MachineFunction &MF = DAG.getMachineFunction();
Gordon Henriksen86737662008-01-05 16:56:59 +00001917 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
Scott Michelfdc40a02009-02-17 22:15:04 +00001918
Gordon Henriksen86737662008-01-05 16:56:59 +00001919 const Function* Fn = MF.getFunction();
1920 if (Fn->hasExternalLinkage() &&
1921 Subtarget->isTargetCygMing() &&
1922 Fn->getName() == "main")
1923 FuncInfo->setForceFramePointer(true);
1924
Evan Cheng1bc78042006-04-26 01:20:17 +00001925 MachineFrameInfo *MFI = MF.getFrameInfo();
Gordon Henriksen86737662008-01-05 16:56:59 +00001926 bool Is64Bit = Subtarget->is64Bit();
Eli Friedman9a2478a2012-01-20 00:05:46 +00001927 bool IsWindows = Subtarget->isTargetWindows();
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001928 bool IsWin64 = Subtarget->isTargetWin64();
Gordon Henriksenae636f82008-01-03 16:47:34 +00001929
Chris Lattner29689432010-03-11 00:22:57 +00001930 assert(!(isVarArg && IsTailCallConvention(CallConv)) &&
Duncan Sandsdc7f1742012-11-16 12:36:39 +00001931 "Var args not supported with calling convention fastcc, ghc or hipe");
Gordon Henriksenae636f82008-01-03 16:47:34 +00001932
Chris Lattner638402b2007-02-28 07:00:42 +00001933 // Assign locations to all of the incoming arguments.
Chris Lattnerf39f7712007-02-28 05:46:49 +00001934 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00001935 CCState CCInfo(CallConv, isVarArg, MF, getTargetMachine(),
Dan Gohman98ca4f22009-08-05 01:29:28 +00001936 ArgLocs, *DAG.getContext());
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00001937
1938 // Allocate shadow area for Win64
1939 if (IsWin64) {
1940 CCInfo.AllocateStack(32, 8);
1941 }
1942
Duncan Sands45907662010-10-31 13:21:44 +00001943 CCInfo.AnalyzeFormalArguments(Ins, CC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001944
Chris Lattnerf39f7712007-02-28 05:46:49 +00001945 unsigned LastVal = ~0U;
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001946 SDValue ArgValue;
Chris Lattnerf39f7712007-02-28 05:46:49 +00001947 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1948 CCValAssign &VA = ArgLocs[i];
1949 // TODO: If an arg is passed in two places (e.g. reg and stack), skip later
1950 // places.
1951 assert(VA.getValNo() != LastVal &&
1952 "Don't support value assigned to multiple locs yet");
Duncan Sands17001ce2011-10-18 12:44:00 +00001953 (void)LastVal;
Chris Lattnerf39f7712007-02-28 05:46:49 +00001954 LastVal = VA.getValNo();
Scott Michelfdc40a02009-02-17 22:15:04 +00001955
Chris Lattnerf39f7712007-02-28 05:46:49 +00001956 if (VA.isRegLoc()) {
Owen Andersone50ed302009-08-10 22:56:29 +00001957 EVT RegVT = VA.getLocVT();
Craig Topper44d23822012-02-22 05:59:10 +00001958 const TargetRegisterClass *RC;
Owen Anderson825b72b2009-08-11 20:47:22 +00001959 if (RegVT == MVT::i32)
Craig Topperc9099502012-04-20 06:31:50 +00001960 RC = &X86::GR32RegClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001961 else if (Is64Bit && RegVT == MVT::i64)
Craig Topperc9099502012-04-20 06:31:50 +00001962 RC = &X86::GR64RegClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001963 else if (RegVT == MVT::f32)
Craig Topperc9099502012-04-20 06:31:50 +00001964 RC = &X86::FR32RegClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001965 else if (RegVT == MVT::f64)
Craig Topperc9099502012-04-20 06:31:50 +00001966 RC = &X86::FR64RegClass;
Craig Topper7a9a28b2012-08-12 02:23:29 +00001967 else if (RegVT.is256BitVector())
Craig Topperc9099502012-04-20 06:31:50 +00001968 RC = &X86::VR256RegClass;
Craig Topper7a9a28b2012-08-12 02:23:29 +00001969 else if (RegVT.is128BitVector())
Craig Topperc9099502012-04-20 06:31:50 +00001970 RC = &X86::VR128RegClass;
Dale Johannesen0488fb62010-09-30 23:57:10 +00001971 else if (RegVT == MVT::x86mmx)
Craig Topperc9099502012-04-20 06:31:50 +00001972 RC = &X86::VR64RegClass;
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001973 else
Torok Edwinc23197a2009-07-14 16:55:14 +00001974 llvm_unreachable("Unknown argument type!");
Gordon Henriksenae636f82008-01-03 16:47:34 +00001975
Devang Patel68e6bee2011-02-21 23:21:26 +00001976 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001977 ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT);
Scott Michelfdc40a02009-02-17 22:15:04 +00001978
Chris Lattnerf39f7712007-02-28 05:46:49 +00001979 // If this is an 8 or 16-bit value, it is really passed promoted to 32
1980 // bits. Insert an assert[sz]ext to capture this, then truncate to the
1981 // right size.
1982 if (VA.getLocInfo() == CCValAssign::SExt)
Dale Johannesenace16102009-02-03 19:33:06 +00001983 ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue,
Chris Lattnerf39f7712007-02-28 05:46:49 +00001984 DAG.getValueType(VA.getValVT()));
1985 else if (VA.getLocInfo() == CCValAssign::ZExt)
Dale Johannesenace16102009-02-03 19:33:06 +00001986 ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue,
Chris Lattnerf39f7712007-02-28 05:46:49 +00001987 DAG.getValueType(VA.getValVT()));
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001988 else if (VA.getLocInfo() == CCValAssign::BCvt)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001989 ArgValue = DAG.getNode(ISD::BITCAST, dl, VA.getValVT(), ArgValue);
Scott Michelfdc40a02009-02-17 22:15:04 +00001990
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001991 if (VA.isExtInLoc()) {
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001992 // Handle MMX values passed in XMM regs.
Jakub Staszakc20323a2012-12-29 15:57:26 +00001993 if (RegVT.isVector())
1994 ArgValue = DAG.getNode(X86ISD::MOVDQ2Q, dl, VA.getValVT(), ArgValue);
1995 else
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001996 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
Evan Cheng44c0fd12008-04-25 20:13:28 +00001997 }
Chris Lattnerf39f7712007-02-28 05:46:49 +00001998 } else {
1999 assert(VA.isMemLoc());
Dan Gohman98ca4f22009-08-05 01:29:28 +00002000 ArgValue = LowerMemArgument(Chain, CallConv, Ins, dl, DAG, VA, MFI, i);
Evan Cheng1bc78042006-04-26 01:20:17 +00002001 }
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00002002
2003 // If value is passed via pointer - do a load.
2004 if (VA.getLocInfo() == CCValAssign::Indirect)
Chris Lattner51abfe42010-09-21 06:02:19 +00002005 ArgValue = DAG.getLoad(VA.getValVT(), dl, Chain, ArgValue,
Pete Cooperd752e0f2011-11-08 18:42:53 +00002006 MachinePointerInfo(), false, false, false, 0);
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00002007
Dan Gohman98ca4f22009-08-05 01:29:28 +00002008 InVals.push_back(ArgValue);
Evan Cheng1bc78042006-04-26 01:20:17 +00002009 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00002010
Dan Gohman61a92132008-04-21 23:59:07 +00002011 // The x86-64 ABI for returning structs by value requires that we copy
2012 // the sret argument into %rax for the return. Save the argument into
2013 // a virtual register so that we can access it from the return points.
Dan Gohman7e77b0f2009-08-01 19:14:37 +00002014 if (Is64Bit && MF.getFunction()->hasStructRetAttr()) {
Dan Gohman61a92132008-04-21 23:59:07 +00002015 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
2016 unsigned Reg = FuncInfo->getSRetReturnReg();
2017 if (!Reg) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002018 Reg = MF.getRegInfo().createVirtualRegister(getRegClassFor(MVT::i64));
Dan Gohman61a92132008-04-21 23:59:07 +00002019 FuncInfo->setSRetReturnReg(Reg);
2020 }
Dan Gohman98ca4f22009-08-05 01:29:28 +00002021 SDValue Copy = DAG.getCopyToReg(DAG.getEntryNode(), dl, Reg, InVals[0]);
Owen Anderson825b72b2009-08-11 20:47:22 +00002022 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Copy, Chain);
Dan Gohman61a92132008-04-21 23:59:07 +00002023 }
2024
Chris Lattnerf39f7712007-02-28 05:46:49 +00002025 unsigned StackSize = CCInfo.getNextStackOffset();
Evan Cheng0c439eb2010-01-27 00:07:07 +00002026 // Align stack specially for tail calls.
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002027 if (FuncIsMadeTailCallSafe(CallConv,
2028 MF.getTarget().Options.GuaranteedTailCallOpt))
Gordon Henriksenae636f82008-01-03 16:47:34 +00002029 StackSize = GetAlignedArgumentStackSize(StackSize, DAG);
Evan Cheng25caf632006-05-23 21:06:34 +00002030
Evan Cheng1bc78042006-04-26 01:20:17 +00002031 // If the function takes variable number of arguments, make a frame index for
2032 // the start of the first vararg value... for expansion of llvm.va_start.
Gordon Henriksenae636f82008-01-03 16:47:34 +00002033 if (isVarArg) {
NAKAMURA Takumi3ca99432011-03-09 11:33:15 +00002034 if (Is64Bit || (CallConv != CallingConv::X86_FastCall &&
2035 CallConv != CallingConv::X86_ThisCall)) {
Jakob Stoklund Olesenb2eeed72010-07-29 17:42:27 +00002036 FuncInfo->setVarArgsFrameIndex(MFI->CreateFixedObject(1, StackSize,true));
Gordon Henriksen86737662008-01-05 16:56:59 +00002037 }
2038 if (Is64Bit) {
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002039 unsigned TotalNumIntRegs = 0, TotalNumXMMRegs = 0;
2040
2041 // FIXME: We should really autogenerate these arrays
Craig Topperc5eaae42012-03-11 07:57:25 +00002042 static const uint16_t GPR64ArgRegsWin64[] = {
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002043 X86::RCX, X86::RDX, X86::R8, X86::R9
Gordon Henriksen86737662008-01-05 16:56:59 +00002044 };
Craig Topperc5eaae42012-03-11 07:57:25 +00002045 static const uint16_t GPR64ArgRegs64Bit[] = {
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002046 X86::RDI, X86::RSI, X86::RDX, X86::RCX, X86::R8, X86::R9
2047 };
Craig Topperc5eaae42012-03-11 07:57:25 +00002048 static const uint16_t XMMArgRegs64Bit[] = {
Gordon Henriksen86737662008-01-05 16:56:59 +00002049 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
2050 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
2051 };
Craig Topperc5eaae42012-03-11 07:57:25 +00002052 const uint16_t *GPR64ArgRegs;
Anton Korobeynikove7beda12010-10-03 22:52:07 +00002053 unsigned NumXMMRegs = 0;
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002054
2055 if (IsWin64) {
Anton Korobeynikove7beda12010-10-03 22:52:07 +00002056 // The XMM registers which might contain var arg parameters are shadowed
2057 // in their paired GPR. So we only need to save the GPR to their home
2058 // slots.
2059 TotalNumIntRegs = 4;
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002060 GPR64ArgRegs = GPR64ArgRegsWin64;
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002061 } else {
2062 TotalNumIntRegs = 6; TotalNumXMMRegs = 8;
2063 GPR64ArgRegs = GPR64ArgRegs64Bit;
Anton Korobeynikove7beda12010-10-03 22:52:07 +00002064
Chad Rosier30450e82011-12-22 22:35:21 +00002065 NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs64Bit,
2066 TotalNumXMMRegs);
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002067 }
2068 unsigned NumIntRegs = CCInfo.getFirstUnallocated(GPR64ArgRegs,
2069 TotalNumIntRegs);
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002070
Bill Wendling67658342012-10-09 07:45:08 +00002071 bool NoImplicitFloatOps = Fn->getFnAttributes().
Bill Wendling034b94b2012-12-19 07:18:57 +00002072 hasAttribute(Attribute::NoImplicitFloat);
Craig Topper1accb7e2012-01-10 06:54:16 +00002073 assert(!(NumXMMRegs && !Subtarget->hasSSE1()) &&
Torok Edwin3f142c32009-02-01 18:15:56 +00002074 "SSE register cannot be used when SSE is disabled!");
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002075 assert(!(NumXMMRegs && MF.getTarget().Options.UseSoftFloat &&
2076 NoImplicitFloatOps) &&
Evan Chengc7ce29b2009-02-13 22:36:38 +00002077 "SSE register cannot be used when SSE is disabled!");
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002078 if (MF.getTarget().Options.UseSoftFloat || NoImplicitFloatOps ||
Craig Topper1accb7e2012-01-10 06:54:16 +00002079 !Subtarget->hasSSE1())
Torok Edwin3f142c32009-02-01 18:15:56 +00002080 // Kernel mode asks for SSE to be disabled, so don't push them
2081 // on the stack.
2082 TotalNumXMMRegs = 0;
Bill Wendlingf9abd7e2009-03-11 22:30:01 +00002083
Anton Korobeynikove7beda12010-10-03 22:52:07 +00002084 if (IsWin64) {
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00002085 const TargetFrameLowering &TFI = *getTargetMachine().getFrameLowering();
Cameron Esfahaniec37b002010-10-08 19:24:18 +00002086 // Get to the caller-allocated home save location. Add 8 to account
2087 // for the return address.
2088 int HomeOffset = TFI.getOffsetOfLocalArea() + 8;
Anton Korobeynikove7beda12010-10-03 22:52:07 +00002089 FuncInfo->setRegSaveFrameIndex(
Cameron Esfahaniec37b002010-10-08 19:24:18 +00002090 MFI->CreateFixedObject(1, NumIntRegs * 8 + HomeOffset, false));
NAKAMURA Takumi3ca99432011-03-09 11:33:15 +00002091 // Fixup to set vararg frame on shadow area (4 x i64).
2092 if (NumIntRegs < 4)
2093 FuncInfo->setVarArgsFrameIndex(FuncInfo->getRegSaveFrameIndex());
Anton Korobeynikove7beda12010-10-03 22:52:07 +00002094 } else {
2095 // For X86-64, if there are vararg parameters that are passed via
Chad Rosier30450e82011-12-22 22:35:21 +00002096 // registers, then we must store them to their spots on the stack so
2097 // they may be loaded by deferencing the result of va_next.
Anton Korobeynikove7beda12010-10-03 22:52:07 +00002098 FuncInfo->setVarArgsGPOffset(NumIntRegs * 8);
2099 FuncInfo->setVarArgsFPOffset(TotalNumIntRegs * 8 + NumXMMRegs * 16);
2100 FuncInfo->setRegSaveFrameIndex(
2101 MFI->CreateStackObject(TotalNumIntRegs * 8 + TotalNumXMMRegs * 16, 16,
Dan Gohman1e93df62010-04-17 14:41:14 +00002102 false));
Anton Korobeynikove7beda12010-10-03 22:52:07 +00002103 }
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002104
Gordon Henriksen86737662008-01-05 16:56:59 +00002105 // Store the integer parameter registers.
Dan Gohman475871a2008-07-27 21:46:04 +00002106 SmallVector<SDValue, 8> MemOps;
Dan Gohman1e93df62010-04-17 14:41:14 +00002107 SDValue RSFIN = DAG.getFrameIndex(FuncInfo->getRegSaveFrameIndex(),
2108 getPointerTy());
2109 unsigned Offset = FuncInfo->getVarArgsGPOffset();
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002110 for (; NumIntRegs != TotalNumIntRegs; ++NumIntRegs) {
Dan Gohmand6708ea2009-08-15 01:38:56 +00002111 SDValue FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), RSFIN,
2112 DAG.getIntPtrConstant(Offset));
Bob Wilson998e1252009-04-20 18:36:57 +00002113 unsigned VReg = MF.addLiveIn(GPR64ArgRegs[NumIntRegs],
Craig Topperc9099502012-04-20 06:31:50 +00002114 &X86::GR64RegClass);
Owen Anderson825b72b2009-08-11 20:47:22 +00002115 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i64);
Dan Gohman475871a2008-07-27 21:46:04 +00002116 SDValue Store =
Dale Johannesenace16102009-02-03 19:33:06 +00002117 DAG.getStore(Val.getValue(1), dl, Val, FIN,
Chris Lattnere8639032010-09-21 06:22:23 +00002118 MachinePointerInfo::getFixedStack(
2119 FuncInfo->getRegSaveFrameIndex(), Offset),
2120 false, false, 0);
Gordon Henriksen86737662008-01-05 16:56:59 +00002121 MemOps.push_back(Store);
Dan Gohmand6708ea2009-08-15 01:38:56 +00002122 Offset += 8;
Gordon Henriksen86737662008-01-05 16:56:59 +00002123 }
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002124
Dan Gohmanface41a2009-08-16 21:24:25 +00002125 if (TotalNumXMMRegs != 0 && NumXMMRegs != TotalNumXMMRegs) {
2126 // Now store the XMM (fp + vector) parameter registers.
2127 SmallVector<SDValue, 11> SaveXMMOps;
2128 SaveXMMOps.push_back(Chain);
Dan Gohmand6708ea2009-08-15 01:38:56 +00002129
Craig Topperc9099502012-04-20 06:31:50 +00002130 unsigned AL = MF.addLiveIn(X86::AL, &X86::GR8RegClass);
Dan Gohmanface41a2009-08-16 21:24:25 +00002131 SDValue ALVal = DAG.getCopyFromReg(DAG.getEntryNode(), dl, AL, MVT::i8);
2132 SaveXMMOps.push_back(ALVal);
Dan Gohmand6708ea2009-08-15 01:38:56 +00002133
Dan Gohman1e93df62010-04-17 14:41:14 +00002134 SaveXMMOps.push_back(DAG.getIntPtrConstant(
2135 FuncInfo->getRegSaveFrameIndex()));
2136 SaveXMMOps.push_back(DAG.getIntPtrConstant(
2137 FuncInfo->getVarArgsFPOffset()));
Dan Gohmand6708ea2009-08-15 01:38:56 +00002138
Dan Gohmanface41a2009-08-16 21:24:25 +00002139 for (; NumXMMRegs != TotalNumXMMRegs; ++NumXMMRegs) {
Anton Korobeynikove7beda12010-10-03 22:52:07 +00002140 unsigned VReg = MF.addLiveIn(XMMArgRegs64Bit[NumXMMRegs],
Craig Topperc9099502012-04-20 06:31:50 +00002141 &X86::VR128RegClass);
Dan Gohmanface41a2009-08-16 21:24:25 +00002142 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::v4f32);
2143 SaveXMMOps.push_back(Val);
2144 }
2145 MemOps.push_back(DAG.getNode(X86ISD::VASTART_SAVE_XMM_REGS, dl,
2146 MVT::Other,
2147 &SaveXMMOps[0], SaveXMMOps.size()));
Gordon Henriksen86737662008-01-05 16:56:59 +00002148 }
Dan Gohmanface41a2009-08-16 21:24:25 +00002149
2150 if (!MemOps.empty())
2151 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
2152 &MemOps[0], MemOps.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00002153 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00002154 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002155
Gordon Henriksen86737662008-01-05 16:56:59 +00002156 // Some CCs need callee pop.
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002157 if (X86::isCalleePop(CallConv, Is64Bit, isVarArg,
2158 MF.getTarget().Options.GuaranteedTailCallOpt)) {
Dan Gohman1e93df62010-04-17 14:41:14 +00002159 FuncInfo->setBytesToPopOnReturn(StackSize); // Callee pops everything.
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00002160 } else {
Dan Gohman1e93df62010-04-17 14:41:14 +00002161 FuncInfo->setBytesToPopOnReturn(0); // Callee pops nothing.
Chris Lattnerf39f7712007-02-28 05:46:49 +00002162 // If this is an sret function, the return should pop the hidden pointer.
Eli Friedman9a2478a2012-01-20 00:05:46 +00002163 if (!Is64Bit && !IsTailCallConvention(CallConv) && !IsWindows &&
Rafael Espindola1cee7102012-07-25 13:41:10 +00002164 argsAreStructReturn(Ins) == StackStructReturn)
Dan Gohman1e93df62010-04-17 14:41:14 +00002165 FuncInfo->setBytesToPopOnReturn(4);
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00002166 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00002167
Gordon Henriksen86737662008-01-05 16:56:59 +00002168 if (!Is64Bit) {
Dan Gohman1e93df62010-04-17 14:41:14 +00002169 // RegSaveFrameIndex is X86-64 only.
2170 FuncInfo->setRegSaveFrameIndex(0xAAAAAAA);
Anton Korobeynikovded05e32010-05-16 09:08:45 +00002171 if (CallConv == CallingConv::X86_FastCall ||
2172 CallConv == CallingConv::X86_ThisCall)
Dan Gohman1e93df62010-04-17 14:41:14 +00002173 // fastcc functions can't have varargs.
2174 FuncInfo->setVarArgsFrameIndex(0xAAAAAAA);
Gordon Henriksen86737662008-01-05 16:56:59 +00002175 }
Evan Cheng25caf632006-05-23 21:06:34 +00002176
Rafael Espindola76927d752011-08-30 19:39:58 +00002177 FuncInfo->setArgumentStackSize(StackSize);
2178
Dan Gohman98ca4f22009-08-05 01:29:28 +00002179 return Chain;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002180}
2181
Dan Gohman475871a2008-07-27 21:46:04 +00002182SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00002183X86TargetLowering::LowerMemOpCallTo(SDValue Chain,
2184 SDValue StackPtr, SDValue Arg,
2185 DebugLoc dl, SelectionDAG &DAG,
Evan Chengdffbd832008-01-10 00:09:10 +00002186 const CCValAssign &VA,
Dan Gohmand858e902010-04-17 15:26:15 +00002187 ISD::ArgFlagsTy Flags) const {
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00002188 unsigned LocMemOffset = VA.getLocMemOffset();
Dan Gohman475871a2008-07-27 21:46:04 +00002189 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset);
Dale Johannesenace16102009-02-03 19:33:06 +00002190 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, PtrOff);
Chris Lattnerfc448ff2010-09-21 18:51:21 +00002191 if (Flags.isByVal())
Dale Johannesendd64c412009-02-04 00:33:20 +00002192 return CreateCopyOfByValArgument(Arg, PtrOff, Chain, Flags, DAG, dl);
Chris Lattnerfc448ff2010-09-21 18:51:21 +00002193
2194 return DAG.getStore(Chain, dl, Arg, PtrOff,
2195 MachinePointerInfo::getStack(LocMemOffset),
David Greene67c9d422010-02-15 16:53:33 +00002196 false, false, 0);
Evan Chengdffbd832008-01-10 00:09:10 +00002197}
2198
Bill Wendling64e87322009-01-16 19:25:27 +00002199/// EmitTailCallLoadRetAddr - Emit a load of return address if tail call
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002200/// optimization is performed and it is required.
Scott Michelfdc40a02009-02-17 22:15:04 +00002201SDValue
2202X86TargetLowering::EmitTailCallLoadRetAddr(SelectionDAG &DAG,
Evan Chengddc419c2010-01-26 19:04:47 +00002203 SDValue &OutRetAddr, SDValue Chain,
2204 bool IsTailCall, bool Is64Bit,
Dan Gohmand858e902010-04-17 15:26:15 +00002205 int FPDiff, DebugLoc dl) const {
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002206 // Adjust the Return address stack slot.
Owen Andersone50ed302009-08-10 22:56:29 +00002207 EVT VT = getPointerTy();
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002208 OutRetAddr = getReturnAddressFrameIndex(DAG);
Bill Wendling64e87322009-01-16 19:25:27 +00002209
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002210 // Load the "old" Return address.
Chris Lattner51abfe42010-09-21 06:02:19 +00002211 OutRetAddr = DAG.getLoad(VT, dl, Chain, OutRetAddr, MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002212 false, false, false, 0);
Gabor Greifba36cb52008-08-28 21:40:38 +00002213 return SDValue(OutRetAddr.getNode(), 1);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002214}
2215
Chris Lattner7a2bdde2011-04-15 05:18:47 +00002216/// EmitTailCallStoreRetAddr - Emit a store of the return address if tail call
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002217/// optimization is performed and it is required (FPDiff!=0).
Scott Michelfdc40a02009-02-17 22:15:04 +00002218static SDValue
2219EmitTailCallStoreRetAddr(SelectionDAG & DAG, MachineFunction &MF,
Michael Liaoaa3c2c02012-10-25 06:29:14 +00002220 SDValue Chain, SDValue RetAddrFrIdx, EVT PtrVT,
2221 unsigned SlotSize, int FPDiff, DebugLoc dl) {
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002222 // Store the return address to the appropriate stack slot.
2223 if (!FPDiff) return Chain;
2224 // Calculate the new stack slot for the return address.
Scott Michelfdc40a02009-02-17 22:15:04 +00002225 int NewReturnAddrFI =
Evan Chenged2ae132010-07-03 00:40:23 +00002226 MF.getFrameInfo()->CreateFixedObject(SlotSize, FPDiff-SlotSize, false);
Michael Liaoaa3c2c02012-10-25 06:29:14 +00002227 SDValue NewRetAddrFrIdx = DAG.getFrameIndex(NewReturnAddrFI, PtrVT);
Scott Michelfdc40a02009-02-17 22:15:04 +00002228 Chain = DAG.getStore(Chain, dl, RetAddrFrIdx, NewRetAddrFrIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00002229 MachinePointerInfo::getFixedStack(NewReturnAddrFI),
David Greene67c9d422010-02-15 16:53:33 +00002230 false, false, 0);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002231 return Chain;
2232}
2233
Dan Gohman98ca4f22009-08-05 01:29:28 +00002234SDValue
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00002235X86TargetLowering::LowerCall(TargetLowering::CallLoweringInfo &CLI,
Dan Gohmand858e902010-04-17 15:26:15 +00002236 SmallVectorImpl<SDValue> &InVals) const {
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00002237 SelectionDAG &DAG = CLI.DAG;
2238 DebugLoc &dl = CLI.DL;
2239 SmallVector<ISD::OutputArg, 32> &Outs = CLI.Outs;
2240 SmallVector<SDValue, 32> &OutVals = CLI.OutVals;
2241 SmallVector<ISD::InputArg, 32> &Ins = CLI.Ins;
2242 SDValue Chain = CLI.Chain;
2243 SDValue Callee = CLI.Callee;
2244 CallingConv::ID CallConv = CLI.CallConv;
2245 bool &isTailCall = CLI.IsTailCall;
2246 bool isVarArg = CLI.IsVarArg;
2247
Dan Gohman98ca4f22009-08-05 01:29:28 +00002248 MachineFunction &MF = DAG.getMachineFunction();
2249 bool Is64Bit = Subtarget->is64Bit();
NAKAMURA Takumifb840c92011-02-05 15:11:13 +00002250 bool IsWin64 = Subtarget->isTargetWin64();
Eli Friedman9a2478a2012-01-20 00:05:46 +00002251 bool IsWindows = Subtarget->isTargetWindows();
Rafael Espindola1cee7102012-07-25 13:41:10 +00002252 StructReturnType SR = callIsStructReturn(Outs);
Evan Cheng5f941932010-02-05 02:21:12 +00002253 bool IsSibcall = false;
Dan Gohman98ca4f22009-08-05 01:29:28 +00002254
Nick Lewycky22de16d2012-01-19 00:34:10 +00002255 if (MF.getTarget().Options.DisableTailCalls)
2256 isTailCall = false;
2257
Evan Cheng5f941932010-02-05 02:21:12 +00002258 if (isTailCall) {
Evan Cheng0c439eb2010-01-27 00:07:07 +00002259 // Check if it's really possible to do a tail call.
Evan Chenga375d472010-03-15 18:54:48 +00002260 isTailCall = IsEligibleForTailCallOptimization(Callee, CallConv,
Rafael Espindola1cee7102012-07-25 13:41:10 +00002261 isVarArg, SR != NotStructReturn,
Evan Chengb1cacc72012-09-25 05:32:34 +00002262 MF.getFunction()->hasStructRetAttr(), CLI.RetTy,
Rafael Espindola1cee7102012-07-25 13:41:10 +00002263 Outs, OutVals, Ins, DAG);
Evan Chengf22f9b32010-02-06 03:28:46 +00002264
2265 // Sibcalls are automatically detected tailcalls which do not require
2266 // ABI changes.
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002267 if (!MF.getTarget().Options.GuaranteedTailCallOpt && isTailCall)
Evan Cheng5f941932010-02-05 02:21:12 +00002268 IsSibcall = true;
Evan Chengf22f9b32010-02-06 03:28:46 +00002269
2270 if (isTailCall)
2271 ++NumTailCalls;
Evan Cheng5f941932010-02-05 02:21:12 +00002272 }
Evan Cheng0c439eb2010-01-27 00:07:07 +00002273
Chris Lattner29689432010-03-11 00:22:57 +00002274 assert(!(isVarArg && IsTailCallConvention(CallConv)) &&
Duncan Sandsdc7f1742012-11-16 12:36:39 +00002275 "Var args not supported with calling convention fastcc, ghc or hipe");
Gordon Henriksenae636f82008-01-03 16:47:34 +00002276
Chris Lattner638402b2007-02-28 07:00:42 +00002277 // Analyze operands of the call, assigning locations to each operand.
Chris Lattner423c5f42007-02-28 05:31:48 +00002278 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002279 CCState CCInfo(CallConv, isVarArg, MF, getTargetMachine(),
Dan Gohman98ca4f22009-08-05 01:29:28 +00002280 ArgLocs, *DAG.getContext());
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00002281
2282 // Allocate shadow area for Win64
2283 if (IsWin64) {
2284 CCInfo.AllocateStack(32, 8);
2285 }
2286
Duncan Sands45907662010-10-31 13:21:44 +00002287 CCInfo.AnalyzeCallOperands(Outs, CC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00002288
Chris Lattner423c5f42007-02-28 05:31:48 +00002289 // Get a count of how many bytes are to be pushed on the stack.
2290 unsigned NumBytes = CCInfo.getNextStackOffset();
Evan Chengf22f9b32010-02-06 03:28:46 +00002291 if (IsSibcall)
Evan Chengb2c92902010-02-02 02:22:50 +00002292 // This is a sibcall. The memory operands are available in caller's
2293 // own caller's stack.
2294 NumBytes = 0;
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002295 else if (getTargetMachine().Options.GuaranteedTailCallOpt &&
2296 IsTailCallConvention(CallConv))
Evan Chengf22f9b32010-02-06 03:28:46 +00002297 NumBytes = GetAlignedArgumentStackSize(NumBytes, DAG);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002298
Gordon Henriksen86737662008-01-05 16:56:59 +00002299 int FPDiff = 0;
Evan Chengf22f9b32010-02-06 03:28:46 +00002300 if (isTailCall && !IsSibcall) {
Gordon Henriksen86737662008-01-05 16:56:59 +00002301 // Lower arguments at fp - stackoffset + fpdiff.
Jakub Staszak96df4372012-10-29 22:02:26 +00002302 X86MachineFunctionInfo *X86Info = MF.getInfo<X86MachineFunctionInfo>();
2303 unsigned NumBytesCallerPushed = X86Info->getBytesToPopOnReturn();
2304
Gordon Henriksen86737662008-01-05 16:56:59 +00002305 FPDiff = NumBytesCallerPushed - NumBytes;
2306
2307 // Set the delta of movement of the returnaddr stackslot.
2308 // But only set if delta is greater than previous delta.
Jakub Staszak96df4372012-10-29 22:02:26 +00002309 if (FPDiff < X86Info->getTCReturnAddrDelta())
2310 X86Info->setTCReturnAddrDelta(FPDiff);
Gordon Henriksen86737662008-01-05 16:56:59 +00002311 }
2312
Evan Chengf22f9b32010-02-06 03:28:46 +00002313 if (!IsSibcall)
2314 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002315
Dan Gohman475871a2008-07-27 21:46:04 +00002316 SDValue RetAddrFrIdx;
Chris Lattner7a2bdde2011-04-15 05:18:47 +00002317 // Load return address for tail calls.
Evan Chengf22f9b32010-02-06 03:28:46 +00002318 if (isTailCall && FPDiff)
2319 Chain = EmitTailCallLoadRetAddr(DAG, RetAddrFrIdx, Chain, isTailCall,
2320 Is64Bit, FPDiff, dl);
Gordon Henriksen86737662008-01-05 16:56:59 +00002321
Dan Gohman475871a2008-07-27 21:46:04 +00002322 SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass;
2323 SmallVector<SDValue, 8> MemOpChains;
2324 SDValue StackPtr;
Chris Lattner423c5f42007-02-28 05:31:48 +00002325
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002326 // Walk the register/memloc assignments, inserting copies/loads. In the case
2327 // of tail call optimization arguments are handle later.
Chris Lattner423c5f42007-02-28 05:31:48 +00002328 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2329 CCValAssign &VA = ArgLocs[i];
Owen Andersone50ed302009-08-10 22:56:29 +00002330 EVT RegVT = VA.getLocVT();
Dan Gohmanc9403652010-07-07 15:54:55 +00002331 SDValue Arg = OutVals[i];
Dan Gohman98ca4f22009-08-05 01:29:28 +00002332 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Dan Gohman095cc292008-09-13 01:54:27 +00002333 bool isByVal = Flags.isByVal();
Scott Michelfdc40a02009-02-17 22:15:04 +00002334
Chris Lattner423c5f42007-02-28 05:31:48 +00002335 // Promote the value if needed.
2336 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002337 default: llvm_unreachable("Unknown loc info!");
Chris Lattner423c5f42007-02-28 05:31:48 +00002338 case CCValAssign::Full: break;
2339 case CCValAssign::SExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002340 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00002341 break;
2342 case CCValAssign::ZExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002343 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00002344 break;
2345 case CCValAssign::AExt:
Craig Topper7a9a28b2012-08-12 02:23:29 +00002346 if (RegVT.is128BitVector()) {
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002347 // Special case: passing MMX values in XMM registers.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002348 Arg = DAG.getNode(ISD::BITCAST, dl, MVT::i64, Arg);
Owen Anderson825b72b2009-08-11 20:47:22 +00002349 Arg = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i64, Arg);
2350 Arg = getMOVL(DAG, dl, MVT::v2i64, DAG.getUNDEF(MVT::v2i64), Arg);
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002351 } else
2352 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, RegVT, Arg);
2353 break;
2354 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002355 Arg = DAG.getNode(ISD::BITCAST, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00002356 break;
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00002357 case CCValAssign::Indirect: {
2358 // Store the argument.
2359 SDValue SpillSlot = DAG.CreateStackTemporary(VA.getValVT());
Evan Chengff89dcb2009-10-18 18:16:27 +00002360 int FI = cast<FrameIndexSDNode>(SpillSlot)->getIndex();
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00002361 Chain = DAG.getStore(Chain, dl, Arg, SpillSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00002362 MachinePointerInfo::getFixedStack(FI),
David Greene67c9d422010-02-15 16:53:33 +00002363 false, false, 0);
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00002364 Arg = SpillSlot;
2365 break;
2366 }
Evan Cheng6b5783d2006-05-25 18:56:34 +00002367 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002368
Chris Lattner423c5f42007-02-28 05:31:48 +00002369 if (VA.isRegLoc()) {
Stuart Hastings2aa0f232011-05-26 04:09:49 +00002370 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
2371 if (isVarArg && IsWin64) {
2372 // Win64 ABI requires argument XMM reg to be copied to the corresponding
2373 // shadow reg if callee is a varargs function.
2374 unsigned ShadowReg = 0;
2375 switch (VA.getLocReg()) {
2376 case X86::XMM0: ShadowReg = X86::RCX; break;
2377 case X86::XMM1: ShadowReg = X86::RDX; break;
2378 case X86::XMM2: ShadowReg = X86::R8; break;
2379 case X86::XMM3: ShadowReg = X86::R9; break;
Anton Korobeynikovc52bedb2010-08-27 14:43:06 +00002380 }
Stuart Hastings2aa0f232011-05-26 04:09:49 +00002381 if (ShadowReg)
2382 RegsToPass.push_back(std::make_pair(ShadowReg, Arg));
Anton Korobeynikovc52bedb2010-08-27 14:43:06 +00002383 }
Evan Chengf22f9b32010-02-06 03:28:46 +00002384 } else if (!IsSibcall && (!isTailCall || isByVal)) {
Evan Cheng5f941932010-02-05 02:21:12 +00002385 assert(VA.isMemLoc());
2386 if (StackPtr.getNode() == 0)
Michael Liaoc5c970e2012-10-31 04:14:09 +00002387 StackPtr = DAG.getCopyFromReg(Chain, dl, RegInfo->getStackRegister(),
2388 getPointerTy());
Evan Cheng5f941932010-02-05 02:21:12 +00002389 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Arg,
2390 dl, DAG, VA, Flags));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002391 }
Stuart Hastings2aa0f232011-05-26 04:09:49 +00002392 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002393
Evan Cheng32fe1032006-05-25 00:59:30 +00002394 if (!MemOpChains.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00002395 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002396 &MemOpChains[0], MemOpChains.size());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002397
Chris Lattner88e1fd52009-07-09 04:24:46 +00002398 if (Subtarget->isPICStyleGOT()) {
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002399 // ELF / PIC requires GOT in the EBX register before function calls via PLT
2400 // GOT pointer.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002401 if (!isTailCall) {
Jakob Stoklund Olesenb8720782012-07-04 19:28:31 +00002402 RegsToPass.push_back(std::make_pair(unsigned(X86::EBX),
2403 DAG.getNode(X86ISD::GlobalBaseReg, DebugLoc(), getPointerTy())));
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002404 } else {
2405 // If we are tail calling and generating PIC/GOT style code load the
2406 // address of the callee into ECX. The value in ecx is used as target of
2407 // the tail jump. This is done to circumvent the ebx/callee-saved problem
2408 // for tail calls on PIC/GOT architectures. Normally we would just put the
2409 // address of GOT into ebx and then call target@PLT. But for tail calls
2410 // ebx would be restored (since ebx is callee saved) before jumping to the
2411 // target@PLT.
2412
2413 // Note: The actual moving to ECX is done further down.
2414 GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee);
2415 if (G && !G->getGlobal()->hasHiddenVisibility() &&
2416 !G->getGlobal()->hasProtectedVisibility())
2417 Callee = LowerGlobalAddress(Callee, DAG);
2418 else if (isa<ExternalSymbolSDNode>(Callee))
Chris Lattner15a380a2009-07-09 04:39:06 +00002419 Callee = LowerExternalSymbol(Callee, DAG);
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002420 }
Anton Korobeynikov7f705592007-01-12 19:20:47 +00002421 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00002422
NAKAMURA Takumifb840c92011-02-05 15:11:13 +00002423 if (Is64Bit && isVarArg && !IsWin64) {
Gordon Henriksen86737662008-01-05 16:56:59 +00002424 // From AMD64 ABI document:
2425 // For calls that may call functions that use varargs or stdargs
2426 // (prototype-less calls or calls to functions containing ellipsis (...) in
2427 // the declaration) %al is used as hidden argument to specify the number
2428 // of SSE registers used. The contents of %al do not need to match exactly
2429 // the number of registers, but must be an ubound on the number of SSE
2430 // registers used and is in the range 0 - 8 inclusive.
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002431
Gordon Henriksen86737662008-01-05 16:56:59 +00002432 // Count the number of XMM registers allocated.
Craig Topperc5eaae42012-03-11 07:57:25 +00002433 static const uint16_t XMMArgRegs[] = {
Gordon Henriksen86737662008-01-05 16:56:59 +00002434 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
2435 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
2436 };
2437 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs, 8);
Craig Topper1accb7e2012-01-10 06:54:16 +00002438 assert((Subtarget->hasSSE1() || !NumXMMRegs)
Torok Edwin3f142c32009-02-01 18:15:56 +00002439 && "SSE registers cannot be used when SSE is disabled");
Scott Michelfdc40a02009-02-17 22:15:04 +00002440
Jakob Stoklund Olesenb8720782012-07-04 19:28:31 +00002441 RegsToPass.push_back(std::make_pair(unsigned(X86::AL),
2442 DAG.getConstant(NumXMMRegs, MVT::i8)));
Gordon Henriksen86737662008-01-05 16:56:59 +00002443 }
2444
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00002445 // For tail calls lower the arguments to the 'real' stack slot.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002446 if (isTailCall) {
2447 // Force all the incoming stack arguments to be loaded from the stack
2448 // before any new outgoing arguments are stored to the stack, because the
2449 // outgoing stack slots may alias the incoming argument stack slots, and
2450 // the alias isn't otherwise explicit. This is slightly more conservative
2451 // than necessary, because it means that each store effectively depends
2452 // on every argument instead of just those arguments it would clobber.
2453 SDValue ArgChain = DAG.getStackArgumentTokenFactor(Chain);
2454
Dan Gohman475871a2008-07-27 21:46:04 +00002455 SmallVector<SDValue, 8> MemOpChains2;
2456 SDValue FIN;
Gordon Henriksen86737662008-01-05 16:56:59 +00002457 int FI = 0;
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002458 if (getTargetMachine().Options.GuaranteedTailCallOpt) {
Evan Chengb2c92902010-02-02 02:22:50 +00002459 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2460 CCValAssign &VA = ArgLocs[i];
2461 if (VA.isRegLoc())
2462 continue;
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00002463 assert(VA.isMemLoc());
Dan Gohmanc9403652010-07-07 15:54:55 +00002464 SDValue Arg = OutVals[i];
Dan Gohman98ca4f22009-08-05 01:29:28 +00002465 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Gordon Henriksen86737662008-01-05 16:56:59 +00002466 // Create frame index.
2467 int32_t Offset = VA.getLocMemOffset()+FPDiff;
Duncan Sands83ec4b62008-06-06 12:08:01 +00002468 uint32_t OpSize = (VA.getLocVT().getSizeInBits()+7)/8;
Evan Chenged2ae132010-07-03 00:40:23 +00002469 FI = MF.getFrameInfo()->CreateFixedObject(OpSize, Offset, true);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002470 FIN = DAG.getFrameIndex(FI, getPointerTy());
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00002471
Duncan Sands276dcbd2008-03-21 09:14:45 +00002472 if (Flags.isByVal()) {
Evan Cheng8e5712b2008-01-12 01:08:07 +00002473 // Copy relative to framepointer.
Dan Gohman475871a2008-07-27 21:46:04 +00002474 SDValue Source = DAG.getIntPtrConstant(VA.getLocMemOffset());
Gabor Greifba36cb52008-08-28 21:40:38 +00002475 if (StackPtr.getNode() == 0)
Michael Liaoc5c970e2012-10-31 04:14:09 +00002476 StackPtr = DAG.getCopyFromReg(Chain, dl,
2477 RegInfo->getStackRegister(),
Dale Johannesendd64c412009-02-04 00:33:20 +00002478 getPointerTy());
Dale Johannesenace16102009-02-03 19:33:06 +00002479 Source = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, Source);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002480
Dan Gohman98ca4f22009-08-05 01:29:28 +00002481 MemOpChains2.push_back(CreateCopyOfByValArgument(Source, FIN,
2482 ArgChain,
Dale Johannesendd64c412009-02-04 00:33:20 +00002483 Flags, DAG, dl));
Gordon Henriksen86737662008-01-05 16:56:59 +00002484 } else {
Evan Cheng8e5712b2008-01-12 01:08:07 +00002485 // Store relative to framepointer.
Dan Gohman69de1932008-02-06 22:27:42 +00002486 MemOpChains2.push_back(
Dan Gohman98ca4f22009-08-05 01:29:28 +00002487 DAG.getStore(ArgChain, dl, Arg, FIN,
Chris Lattnere8639032010-09-21 06:22:23 +00002488 MachinePointerInfo::getFixedStack(FI),
David Greene67c9d422010-02-15 16:53:33 +00002489 false, false, 0));
Scott Michelfdc40a02009-02-17 22:15:04 +00002490 }
Gordon Henriksen86737662008-01-05 16:56:59 +00002491 }
2492 }
2493
2494 if (!MemOpChains2.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00002495 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Arnold Schwaighofer719eb022008-01-11 14:34:56 +00002496 &MemOpChains2[0], MemOpChains2.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00002497
2498 // Store the return address to the appropriate stack slot.
Michael Liaoaa3c2c02012-10-25 06:29:14 +00002499 Chain = EmitTailCallStoreRetAddr(DAG, MF, Chain, RetAddrFrIdx,
2500 getPointerTy(), RegInfo->getSlotSize(),
Dale Johannesenace16102009-02-03 19:33:06 +00002501 FPDiff, dl);
Gordon Henriksen86737662008-01-05 16:56:59 +00002502 }
2503
Jakob Stoklund Olesenb8720782012-07-04 19:28:31 +00002504 // Build a sequence of copy-to-reg nodes chained together with token chain
2505 // and flag operands which copy the outgoing args into registers.
2506 SDValue InFlag;
2507 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
2508 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
2509 RegsToPass[i].second, InFlag);
2510 InFlag = Chain.getValue(1);
2511 }
2512
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002513 if (getTargetMachine().getCodeModel() == CodeModel::Large) {
2514 assert(Is64Bit && "Large code model is only legal in 64-bit mode.");
2515 // In the 64-bit large code model, we have to make all calls
2516 // through a register, since the call instruction's 32-bit
2517 // pc-relative offset may not be large enough to hold the whole
2518 // address.
2519 } else if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002520 // If the callee is a GlobalAddress node (quite common, every direct call
2521 // is) turn it into a TargetGlobalAddress node so that legalize doesn't hack
2522 // it.
2523
Anton Korobeynikov2b2bc682006-12-22 22:29:05 +00002524 // We should use extra load for direct calls to dllimported functions in
2525 // non-JIT mode.
Dan Gohman46510a72010-04-15 01:51:59 +00002526 const GlobalValue *GV = G->getGlobal();
Chris Lattner754b7652009-07-10 05:48:03 +00002527 if (!GV->hasDLLImportLinkage()) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002528 unsigned char OpFlags = 0;
John McCall3a3465b2011-06-15 20:36:13 +00002529 bool ExtraLoad = false;
2530 unsigned WrapperKind = ISD::DELETED_NODE;
Eric Christopherfd179292009-08-27 18:07:15 +00002531
Chris Lattner48a7d022009-07-09 05:02:21 +00002532 // On ELF targets, in both X86-64 and X86-32 mode, direct calls to
2533 // external symbols most go through the PLT in PIC mode. If the symbol
2534 // has hidden or protected visibility, or if it is static or local, then
2535 // we don't need to use the PLT - we can directly call it.
2536 if (Subtarget->isTargetELF() &&
2537 getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
Chris Lattner74e726e2009-07-09 05:27:35 +00002538 GV->hasDefaultVisibility() && !GV->hasLocalLinkage()) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002539 OpFlags = X86II::MO_PLT;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00002540 } else if (Subtarget->isPICStyleStubAny() &&
Chris Lattner80945782010-09-27 06:34:01 +00002541 (GV->isDeclaration() || GV->isWeakForLinker()) &&
Daniel Dunbar558692f2011-04-20 00:14:25 +00002542 (!Subtarget->getTargetTriple().isMacOSX() ||
2543 Subtarget->getTargetTriple().isMacOSXVersionLT(10, 5))) {
Chris Lattner74e726e2009-07-09 05:27:35 +00002544 // PC-relative references to external symbols should go through $stub,
2545 // unless we're building with the leopard linker or later, which
2546 // automatically synthesizes these stubs.
2547 OpFlags = X86II::MO_DARWIN_STUB;
John McCall3a3465b2011-06-15 20:36:13 +00002548 } else if (Subtarget->isPICStyleRIPRel() &&
2549 isa<Function>(GV) &&
Bill Wendling67658342012-10-09 07:45:08 +00002550 cast<Function>(GV)->getFnAttributes().
Bill Wendling034b94b2012-12-19 07:18:57 +00002551 hasAttribute(Attribute::NonLazyBind)) {
John McCall3a3465b2011-06-15 20:36:13 +00002552 // If the function is marked as non-lazy, generate an indirect call
2553 // which loads from the GOT directly. This avoids runtime overhead
2554 // at the cost of eager binding (and one extra byte of encoding).
2555 OpFlags = X86II::MO_GOTPCREL;
2556 WrapperKind = X86ISD::WrapperRIP;
2557 ExtraLoad = true;
Chris Lattner74e726e2009-07-09 05:27:35 +00002558 }
Chris Lattner48a7d022009-07-09 05:02:21 +00002559
Devang Patel0d881da2010-07-06 22:08:15 +00002560 Callee = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(),
Chris Lattner48a7d022009-07-09 05:02:21 +00002561 G->getOffset(), OpFlags);
John McCall3a3465b2011-06-15 20:36:13 +00002562
2563 // Add a wrapper if needed.
2564 if (WrapperKind != ISD::DELETED_NODE)
2565 Callee = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Callee);
2566 // Add extra indirection if needed.
2567 if (ExtraLoad)
2568 Callee = DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(), Callee,
2569 MachinePointerInfo::getGOT(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002570 false, false, false, 0);
Chris Lattner48a7d022009-07-09 05:02:21 +00002571 }
Bill Wendling056292f2008-09-16 21:48:12 +00002572 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002573 unsigned char OpFlags = 0;
2574
Evan Cheng1bf891a2010-12-01 22:59:46 +00002575 // On ELF targets, in either X86-64 or X86-32 mode, direct calls to
2576 // external symbols should go through the PLT.
2577 if (Subtarget->isTargetELF() &&
2578 getTargetMachine().getRelocationModel() == Reloc::PIC_) {
2579 OpFlags = X86II::MO_PLT;
2580 } else if (Subtarget->isPICStyleStubAny() &&
Daniel Dunbar558692f2011-04-20 00:14:25 +00002581 (!Subtarget->getTargetTriple().isMacOSX() ||
2582 Subtarget->getTargetTriple().isMacOSXVersionLT(10, 5))) {
Evan Cheng1bf891a2010-12-01 22:59:46 +00002583 // PC-relative references to external symbols should go through $stub,
2584 // unless we're building with the leopard linker or later, which
2585 // automatically synthesizes these stubs.
2586 OpFlags = X86II::MO_DARWIN_STUB;
Chris Lattner74e726e2009-07-09 05:27:35 +00002587 }
Eric Christopherfd179292009-08-27 18:07:15 +00002588
Chris Lattner48a7d022009-07-09 05:02:21 +00002589 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy(),
2590 OpFlags);
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002591 }
2592
Chris Lattnerd96d0722007-02-25 06:40:16 +00002593 // Returns a chain & a flag for retval copy to use.
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002594 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Dan Gohman475871a2008-07-27 21:46:04 +00002595 SmallVector<SDValue, 8> Ops;
Gordon Henriksen86737662008-01-05 16:56:59 +00002596
Evan Chengf22f9b32010-02-06 03:28:46 +00002597 if (!IsSibcall && isTailCall) {
Dale Johannesene8d72302009-02-06 23:05:02 +00002598 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
2599 DAG.getIntPtrConstant(0, true), InFlag);
Gordon Henriksen86737662008-01-05 16:56:59 +00002600 InFlag = Chain.getValue(1);
Gordon Henriksen86737662008-01-05 16:56:59 +00002601 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002602
Nate Begeman4c5dcf52006-02-17 00:03:04 +00002603 Ops.push_back(Chain);
2604 Ops.push_back(Callee);
Evan Chengb69d1132006-06-14 18:17:40 +00002605
Dan Gohman98ca4f22009-08-05 01:29:28 +00002606 if (isTailCall)
Owen Anderson825b72b2009-08-11 20:47:22 +00002607 Ops.push_back(DAG.getConstant(FPDiff, MVT::i32));
Evan Chengf4684712007-02-21 21:18:14 +00002608
Gordon Henriksen86737662008-01-05 16:56:59 +00002609 // Add argument registers to the end of the list so that they are known live
2610 // into the call.
Evan Cheng9b449442008-01-07 23:08:23 +00002611 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
2612 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
2613 RegsToPass[i].second.getValueType()));
Scott Michelfdc40a02009-02-17 22:15:04 +00002614
Jakob Stoklund Olesen8bcde2a2012-02-16 00:02:50 +00002615 // Add a register mask operand representing the call-preserved registers.
2616 const TargetRegisterInfo *TRI = getTargetMachine().getRegisterInfo();
2617 const uint32_t *Mask = TRI->getCallPreservedMask(CallConv);
2618 assert(Mask && "Missing call preserved mask for calling convention");
2619 Ops.push_back(DAG.getRegisterMask(Mask));
Jakob Stoklund Olesenc38c4562012-01-18 23:52:22 +00002620
Gabor Greifba36cb52008-08-28 21:40:38 +00002621 if (InFlag.getNode())
Evan Cheng347d5f72006-04-28 21:29:37 +00002622 Ops.push_back(InFlag);
Gordon Henriksenae636f82008-01-03 16:47:34 +00002623
Dan Gohman98ca4f22009-08-05 01:29:28 +00002624 if (isTailCall) {
Dale Johannesen88004c22010-06-05 00:30:45 +00002625 // We used to do:
2626 //// If this is the first return lowered for this function, add the regs
2627 //// to the liveout set for the function.
2628 // This isn't right, although it's probably harmless on x86; liveouts
2629 // should be computed from returns not tail calls. Consider a void
2630 // function making a tail call to a function returning int.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002631 return DAG.getNode(X86ISD::TC_RETURN, dl,
2632 NodeTys, &Ops[0], Ops.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00002633 }
2634
Dale Johannesenace16102009-02-03 19:33:06 +00002635 Chain = DAG.getNode(X86ISD::CALL, dl, NodeTys, &Ops[0], Ops.size());
Evan Cheng347d5f72006-04-28 21:29:37 +00002636 InFlag = Chain.getValue(1);
Evan Chengd90eb7f2006-01-05 00:27:02 +00002637
Chris Lattner2d297092006-05-23 18:50:38 +00002638 // Create the CALLSEQ_END node.
Gordon Henriksen86737662008-01-05 16:56:59 +00002639 unsigned NumBytesForCalleeToPush;
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002640 if (X86::isCalleePop(CallConv, Is64Bit, isVarArg,
2641 getTargetMachine().Options.GuaranteedTailCallOpt))
Gordon Henriksen86737662008-01-05 16:56:59 +00002642 NumBytesForCalleeToPush = NumBytes; // Callee pops everything
Eli Friedman9a2478a2012-01-20 00:05:46 +00002643 else if (!Is64Bit && !IsTailCallConvention(CallConv) && !IsWindows &&
Rafael Espindola1cee7102012-07-25 13:41:10 +00002644 SR == StackStructReturn)
Dan Gohmanf451cb82010-02-10 16:03:48 +00002645 // If this is a call to a struct-return function, the callee
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00002646 // pops the hidden struct pointer, so we have to push it back.
2647 // This is common for Darwin/X86, Linux & Mingw32 targets.
Eli Friedman9a2478a2012-01-20 00:05:46 +00002648 // For MSVC Win32 targets, the caller pops the hidden struct pointer.
Gordon Henriksenae636f82008-01-03 16:47:34 +00002649 NumBytesForCalleeToPush = 4;
Gordon Henriksen86737662008-01-05 16:56:59 +00002650 else
Gordon Henriksenae636f82008-01-03 16:47:34 +00002651 NumBytesForCalleeToPush = 0; // Callee pops nothing.
Scott Michelfdc40a02009-02-17 22:15:04 +00002652
Gordon Henriksenae636f82008-01-03 16:47:34 +00002653 // Returns a flag for retval copy to use.
Evan Chengf22f9b32010-02-06 03:28:46 +00002654 if (!IsSibcall) {
2655 Chain = DAG.getCALLSEQ_END(Chain,
2656 DAG.getIntPtrConstant(NumBytes, true),
2657 DAG.getIntPtrConstant(NumBytesForCalleeToPush,
2658 true),
2659 InFlag);
2660 InFlag = Chain.getValue(1);
2661 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00002662
Chris Lattner3085e152007-02-25 08:59:22 +00002663 // Handle result values, copying them out of physregs into vregs that we
2664 // return.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002665 return LowerCallResult(Chain, InFlag, CallConv, isVarArg,
2666 Ins, dl, DAG, InVals);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002667}
2668
Evan Cheng25ab6902006-09-08 06:48:29 +00002669//===----------------------------------------------------------------------===//
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002670// Fast Calling Convention (tail call) implementation
2671//===----------------------------------------------------------------------===//
2672
2673// Like std call, callee cleans arguments, convention except that ECX is
2674// reserved for storing the tail called function address. Only 2 registers are
2675// free for argument passing (inreg). Tail call optimization is performed
2676// provided:
2677// * tailcallopt is enabled
2678// * caller/callee are fastcc
Arnold Schwaighofera2a4b472008-02-26 10:21:54 +00002679// On X86_64 architecture with GOT-style position independent code only local
2680// (within module) calls are supported at the moment.
Arnold Schwaighofer48abc5c2007-10-12 21:30:57 +00002681// To keep the stack aligned according to platform abi the function
2682// GetAlignedArgumentStackSize ensures that argument delta is always multiples
2683// of stack alignment. (Dynamic linkers need this - darwin's dyld for example)
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002684// If a tail called function callee has more arguments than the caller the
2685// caller needs to make sure that there is room to move the RETADDR to. This is
Arnold Schwaighofer48abc5c2007-10-12 21:30:57 +00002686// achieved by reserving an area the size of the argument delta right after the
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002687// original REtADDR, but before the saved framepointer or the spilled registers
2688// e.g. caller(arg1, arg2) calls callee(arg1, arg2,arg3,arg4)
2689// stack layout:
2690// arg1
2691// arg2
2692// RETADDR
Scott Michelfdc40a02009-02-17 22:15:04 +00002693// [ new RETADDR
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002694// move area ]
2695// (possible EBP)
2696// ESI
2697// EDI
2698// local1 ..
2699
2700/// GetAlignedArgumentStackSize - Make the stack size align e.g 16n + 12 aligned
2701/// for a 16 byte align requirement.
Dan Gohmand858e902010-04-17 15:26:15 +00002702unsigned
2703X86TargetLowering::GetAlignedArgumentStackSize(unsigned StackSize,
2704 SelectionDAG& DAG) const {
Evan Chenge9ac9e62008-09-07 09:07:23 +00002705 MachineFunction &MF = DAG.getMachineFunction();
2706 const TargetMachine &TM = MF.getTarget();
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00002707 const TargetFrameLowering &TFI = *TM.getFrameLowering();
Evan Chenge9ac9e62008-09-07 09:07:23 +00002708 unsigned StackAlignment = TFI.getStackAlignment();
Scott Michelfdc40a02009-02-17 22:15:04 +00002709 uint64_t AlignMask = StackAlignment - 1;
Evan Chenge9ac9e62008-09-07 09:07:23 +00002710 int64_t Offset = StackSize;
Michael Liaoaa3c2c02012-10-25 06:29:14 +00002711 unsigned SlotSize = RegInfo->getSlotSize();
Evan Chenge9ac9e62008-09-07 09:07:23 +00002712 if ( (Offset & AlignMask) <= (StackAlignment - SlotSize) ) {
2713 // Number smaller than 12 so just add the difference.
2714 Offset += ((StackAlignment - SlotSize) - (Offset & AlignMask));
2715 } else {
2716 // Mask out lower bits, add stackalignment once plus the 12 bytes.
Scott Michelfdc40a02009-02-17 22:15:04 +00002717 Offset = ((~AlignMask) & Offset) + StackAlignment +
Evan Chenge9ac9e62008-09-07 09:07:23 +00002718 (StackAlignment-SlotSize);
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002719 }
Evan Chenge9ac9e62008-09-07 09:07:23 +00002720 return Offset;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002721}
2722
Evan Cheng5f941932010-02-05 02:21:12 +00002723/// MatchingStackOffset - Return true if the given stack call argument is
2724/// already available in the same position (relatively) of the caller's
2725/// incoming argument stack.
2726static
2727bool MatchingStackOffset(SDValue Arg, unsigned Offset, ISD::ArgFlagsTy Flags,
2728 MachineFrameInfo *MFI, const MachineRegisterInfo *MRI,
2729 const X86InstrInfo *TII) {
Evan Cheng4cae1332010-03-05 08:38:04 +00002730 unsigned Bytes = Arg.getValueType().getSizeInBits() / 8;
2731 int FI = INT_MAX;
Evan Cheng5f941932010-02-05 02:21:12 +00002732 if (Arg.getOpcode() == ISD::CopyFromReg) {
2733 unsigned VR = cast<RegisterSDNode>(Arg.getOperand(1))->getReg();
Jakob Stoklund Olesenc9df0252011-01-10 02:58:51 +00002734 if (!TargetRegisterInfo::isVirtualRegister(VR))
Evan Cheng5f941932010-02-05 02:21:12 +00002735 return false;
2736 MachineInstr *Def = MRI->getVRegDef(VR);
2737 if (!Def)
2738 return false;
2739 if (!Flags.isByVal()) {
2740 if (!TII->isLoadFromStackSlot(Def, FI))
2741 return false;
2742 } else {
2743 unsigned Opcode = Def->getOpcode();
2744 if ((Opcode == X86::LEA32r || Opcode == X86::LEA64r) &&
2745 Def->getOperand(1).isFI()) {
2746 FI = Def->getOperand(1).getIndex();
Evan Cheng4cae1332010-03-05 08:38:04 +00002747 Bytes = Flags.getByValSize();
Evan Cheng5f941932010-02-05 02:21:12 +00002748 } else
2749 return false;
2750 }
Evan Cheng4cae1332010-03-05 08:38:04 +00002751 } else if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Arg)) {
2752 if (Flags.isByVal())
2753 // ByVal argument is passed in as a pointer but it's now being
Evan Cheng10718492010-03-05 19:55:55 +00002754 // dereferenced. e.g.
Evan Cheng4cae1332010-03-05 08:38:04 +00002755 // define @foo(%struct.X* %A) {
2756 // tail call @bar(%struct.X* byval %A)
2757 // }
Evan Cheng5f941932010-02-05 02:21:12 +00002758 return false;
2759 SDValue Ptr = Ld->getBasePtr();
2760 FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr);
2761 if (!FINode)
2762 return false;
2763 FI = FINode->getIndex();
Chad Rosierdf78fcd2011-06-25 02:04:56 +00002764 } else if (Arg.getOpcode() == ISD::FrameIndex && Flags.isByVal()) {
Chad Rosier14d71aa2011-06-25 18:51:28 +00002765 FrameIndexSDNode *FINode = cast<FrameIndexSDNode>(Arg);
Chad Rosierdf78fcd2011-06-25 02:04:56 +00002766 FI = FINode->getIndex();
2767 Bytes = Flags.getByValSize();
Evan Cheng4cae1332010-03-05 08:38:04 +00002768 } else
2769 return false;
Evan Cheng5f941932010-02-05 02:21:12 +00002770
Evan Cheng4cae1332010-03-05 08:38:04 +00002771 assert(FI != INT_MAX);
Evan Cheng5f941932010-02-05 02:21:12 +00002772 if (!MFI->isFixedObjectIndex(FI))
2773 return false;
Evan Cheng4cae1332010-03-05 08:38:04 +00002774 return Offset == MFI->getObjectOffset(FI) && Bytes == MFI->getObjectSize(FI);
Evan Cheng5f941932010-02-05 02:21:12 +00002775}
2776
Dan Gohman98ca4f22009-08-05 01:29:28 +00002777/// IsEligibleForTailCallOptimization - Check whether the call is eligible
2778/// for tail call optimization. Targets which want to do tail call
2779/// optimization should implement this function.
2780bool
2781X86TargetLowering::IsEligibleForTailCallOptimization(SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00002782 CallingConv::ID CalleeCC,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002783 bool isVarArg,
Evan Chenga375d472010-03-15 18:54:48 +00002784 bool isCalleeStructRet,
2785 bool isCallerStructRet,
Evan Chengb1cacc72012-09-25 05:32:34 +00002786 Type *RetTy,
Evan Chengb1712452010-01-27 06:25:16 +00002787 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00002788 const SmallVectorImpl<SDValue> &OutVals,
Evan Chengb1712452010-01-27 06:25:16 +00002789 const SmallVectorImpl<ISD::InputArg> &Ins,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002790 SelectionDAG& DAG) const {
Chris Lattner29689432010-03-11 00:22:57 +00002791 if (!IsTailCallConvention(CalleeCC) &&
Evan Chengb1712452010-01-27 06:25:16 +00002792 CalleeCC != CallingConv::C)
2793 return false;
2794
Evan Cheng7096ae42010-01-29 06:45:59 +00002795 // If -tailcallopt is specified, make fastcc functions tail-callable.
Evan Cheng2c12cb42010-03-26 16:26:03 +00002796 const MachineFunction &MF = DAG.getMachineFunction();
Evan Cheng7096ae42010-01-29 06:45:59 +00002797 const Function *CallerF = DAG.getMachineFunction().getFunction();
Evan Chengb1cacc72012-09-25 05:32:34 +00002798
2799 // If the function return type is x86_fp80 and the callee return type is not,
2800 // then the FP_EXTEND of the call result is not a nop. It's not safe to
2801 // perform a tailcall optimization here.
2802 if (CallerF->getReturnType()->isX86_FP80Ty() && !RetTy->isX86_FP80Ty())
2803 return false;
2804
Evan Cheng13617962010-04-30 01:12:32 +00002805 CallingConv::ID CallerCC = CallerF->getCallingConv();
2806 bool CCMatch = CallerCC == CalleeCC;
2807
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002808 if (getTargetMachine().Options.GuaranteedTailCallOpt) {
Evan Cheng13617962010-04-30 01:12:32 +00002809 if (IsTailCallConvention(CalleeCC) && CCMatch)
Evan Cheng843bd692010-01-31 06:44:49 +00002810 return true;
2811 return false;
2812 }
2813
Dale Johannesen2f05cc02010-05-28 23:24:28 +00002814 // Look for obvious safe cases to perform tail call optimization that do not
2815 // require ABI changes. This is what gcc calls sibcall.
Evan Chengb2c92902010-02-02 02:22:50 +00002816
Evan Cheng2c12cb42010-03-26 16:26:03 +00002817 // Can't do sibcall if stack needs to be dynamically re-aligned. PEI needs to
2818 // emit a special epilogue.
2819 if (RegInfo->needsStackRealignment(MF))
2820 return false;
2821
Evan Chenga375d472010-03-15 18:54:48 +00002822 // Also avoid sibcall optimization if either caller or callee uses struct
2823 // return semantics.
2824 if (isCalleeStructRet || isCallerStructRet)
2825 return false;
2826
Chad Rosier2416da32011-06-24 21:15:36 +00002827 // An stdcall caller is expected to clean up its arguments; the callee
2828 // isn't going to do that.
2829 if (!CCMatch && CallerCC==CallingConv::X86_StdCall)
2830 return false;
2831
Chad Rosier871f6642011-05-18 19:59:50 +00002832 // Do not sibcall optimize vararg calls unless all arguments are passed via
Chad Rosiera1660892011-05-20 00:59:28 +00002833 // registers.
Chad Rosier871f6642011-05-18 19:59:50 +00002834 if (isVarArg && !Outs.empty()) {
Chad Rosiera1660892011-05-20 00:59:28 +00002835
2836 // Optimizing for varargs on Win64 is unlikely to be safe without
2837 // additional testing.
2838 if (Subtarget->isTargetWin64())
2839 return false;
2840
Chad Rosier871f6642011-05-18 19:59:50 +00002841 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002842 CCState CCInfo(CalleeCC, isVarArg, DAG.getMachineFunction(),
Craig Topper0fbf3642012-04-23 03:28:34 +00002843 getTargetMachine(), ArgLocs, *DAG.getContext());
Chad Rosier871f6642011-05-18 19:59:50 +00002844
Chad Rosier871f6642011-05-18 19:59:50 +00002845 CCInfo.AnalyzeCallOperands(Outs, CC_X86);
2846 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i)
2847 if (!ArgLocs[i].isRegLoc())
2848 return false;
2849 }
2850
Chad Rosier30450e82011-12-22 22:35:21 +00002851 // If the call result is in ST0 / ST1, it needs to be popped off the x87
2852 // stack. Therefore, if it's not used by the call it is not safe to optimize
2853 // this into a sibcall.
Evan Chengf5b9d6c2010-03-20 02:58:15 +00002854 bool Unused = false;
2855 for (unsigned i = 0, e = Ins.size(); i != e; ++i) {
2856 if (!Ins[i].Used) {
2857 Unused = true;
2858 break;
2859 }
2860 }
2861 if (Unused) {
2862 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002863 CCState CCInfo(CalleeCC, false, DAG.getMachineFunction(),
Craig Topper0fbf3642012-04-23 03:28:34 +00002864 getTargetMachine(), RVLocs, *DAG.getContext());
Evan Chengf5b9d6c2010-03-20 02:58:15 +00002865 CCInfo.AnalyzeCallResult(Ins, RetCC_X86);
Evan Cheng13617962010-04-30 01:12:32 +00002866 for (unsigned i = 0, e = RVLocs.size(); i != e; ++i) {
Evan Chengf5b9d6c2010-03-20 02:58:15 +00002867 CCValAssign &VA = RVLocs[i];
2868 if (VA.getLocReg() == X86::ST0 || VA.getLocReg() == X86::ST1)
2869 return false;
2870 }
2871 }
2872
Evan Cheng13617962010-04-30 01:12:32 +00002873 // If the calling conventions do not match, then we'd better make sure the
2874 // results are returned in the same way as what the caller expects.
2875 if (!CCMatch) {
2876 SmallVector<CCValAssign, 16> RVLocs1;
Eric Christopher471e4222011-06-08 23:55:35 +00002877 CCState CCInfo1(CalleeCC, false, DAG.getMachineFunction(),
Craig Topper0fbf3642012-04-23 03:28:34 +00002878 getTargetMachine(), RVLocs1, *DAG.getContext());
Evan Cheng13617962010-04-30 01:12:32 +00002879 CCInfo1.AnalyzeCallResult(Ins, RetCC_X86);
2880
2881 SmallVector<CCValAssign, 16> RVLocs2;
Eric Christopher471e4222011-06-08 23:55:35 +00002882 CCState CCInfo2(CallerCC, false, DAG.getMachineFunction(),
Craig Topper0fbf3642012-04-23 03:28:34 +00002883 getTargetMachine(), RVLocs2, *DAG.getContext());
Evan Cheng13617962010-04-30 01:12:32 +00002884 CCInfo2.AnalyzeCallResult(Ins, RetCC_X86);
2885
2886 if (RVLocs1.size() != RVLocs2.size())
2887 return false;
2888 for (unsigned i = 0, e = RVLocs1.size(); i != e; ++i) {
2889 if (RVLocs1[i].isRegLoc() != RVLocs2[i].isRegLoc())
2890 return false;
2891 if (RVLocs1[i].getLocInfo() != RVLocs2[i].getLocInfo())
2892 return false;
2893 if (RVLocs1[i].isRegLoc()) {
2894 if (RVLocs1[i].getLocReg() != RVLocs2[i].getLocReg())
2895 return false;
2896 } else {
2897 if (RVLocs1[i].getLocMemOffset() != RVLocs2[i].getLocMemOffset())
2898 return false;
2899 }
2900 }
2901 }
2902
Evan Chenga6bff982010-01-30 01:22:00 +00002903 // If the callee takes no arguments then go on to check the results of the
2904 // call.
2905 if (!Outs.empty()) {
2906 // Check if stack adjustment is needed. For now, do not do this if any
2907 // argument is passed on the stack.
2908 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002909 CCState CCInfo(CalleeCC, isVarArg, DAG.getMachineFunction(),
Craig Topper0fbf3642012-04-23 03:28:34 +00002910 getTargetMachine(), ArgLocs, *DAG.getContext());
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00002911
2912 // Allocate shadow area for Win64
2913 if (Subtarget->isTargetWin64()) {
2914 CCInfo.AllocateStack(32, 8);
2915 }
2916
Duncan Sands45907662010-10-31 13:21:44 +00002917 CCInfo.AnalyzeCallOperands(Outs, CC_X86);
Stuart Hastings6db2c2f2011-05-17 16:59:46 +00002918 if (CCInfo.getNextStackOffset()) {
Evan Chengb2c92902010-02-02 02:22:50 +00002919 MachineFunction &MF = DAG.getMachineFunction();
2920 if (MF.getInfo<X86MachineFunctionInfo>()->getBytesToPopOnReturn())
2921 return false;
Evan Chengb2c92902010-02-02 02:22:50 +00002922
2923 // Check if the arguments are already laid out in the right way as
2924 // the caller's fixed stack objects.
2925 MachineFrameInfo *MFI = MF.getFrameInfo();
Evan Cheng5f941932010-02-05 02:21:12 +00002926 const MachineRegisterInfo *MRI = &MF.getRegInfo();
2927 const X86InstrInfo *TII =
Roman Divacky59324292012-09-05 22:26:57 +00002928 ((const X86TargetMachine&)getTargetMachine()).getInstrInfo();
Evan Chengb2c92902010-02-02 02:22:50 +00002929 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2930 CCValAssign &VA = ArgLocs[i];
Dan Gohmanc9403652010-07-07 15:54:55 +00002931 SDValue Arg = OutVals[i];
Evan Chengb2c92902010-02-02 02:22:50 +00002932 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Evan Chengb2c92902010-02-02 02:22:50 +00002933 if (VA.getLocInfo() == CCValAssign::Indirect)
2934 return false;
2935 if (!VA.isRegLoc()) {
Evan Cheng5f941932010-02-05 02:21:12 +00002936 if (!MatchingStackOffset(Arg, VA.getLocMemOffset(), Flags,
2937 MFI, MRI, TII))
Evan Chengb2c92902010-02-02 02:22:50 +00002938 return false;
2939 }
2940 }
2941 }
Evan Cheng9c044672010-05-29 01:35:22 +00002942
2943 // If the tailcall address may be in a register, then make sure it's
2944 // possible to register allocate for it. In 32-bit, the call address can
2945 // only target EAX, EDX, or ECX since the tail call must be scheduled after
Evan Chengdedd9742010-07-14 06:44:01 +00002946 // callee-saved registers are restored. These happen to be the same
2947 // registers used to pass 'inreg' arguments so watch out for those.
2948 if (!Subtarget->is64Bit() &&
2949 !isa<GlobalAddressSDNode>(Callee) &&
Evan Cheng9c044672010-05-29 01:35:22 +00002950 !isa<ExternalSymbolSDNode>(Callee)) {
Evan Cheng9c044672010-05-29 01:35:22 +00002951 unsigned NumInRegs = 0;
2952 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2953 CCValAssign &VA = ArgLocs[i];
Evan Chengdedd9742010-07-14 06:44:01 +00002954 if (!VA.isRegLoc())
2955 continue;
2956 unsigned Reg = VA.getLocReg();
2957 switch (Reg) {
2958 default: break;
2959 case X86::EAX: case X86::EDX: case X86::ECX:
2960 if (++NumInRegs == 3)
Evan Cheng9c044672010-05-29 01:35:22 +00002961 return false;
Evan Chengdedd9742010-07-14 06:44:01 +00002962 break;
Evan Cheng9c044672010-05-29 01:35:22 +00002963 }
2964 }
2965 }
Evan Chenga6bff982010-01-30 01:22:00 +00002966 }
Evan Chengb1712452010-01-27 06:25:16 +00002967
Evan Cheng86809cc2010-02-03 03:28:02 +00002968 return true;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002969}
2970
Dan Gohman3df24e62008-09-03 23:12:08 +00002971FastISel *
Bob Wilsond49edb72012-08-03 04:06:28 +00002972X86TargetLowering::createFastISel(FunctionLoweringInfo &funcInfo,
2973 const TargetLibraryInfo *libInfo) const {
2974 return X86::createFastISel(funcInfo, libInfo);
Dan Gohmand9f3c482008-08-19 21:32:53 +00002975}
2976
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00002977//===----------------------------------------------------------------------===//
2978// Other Lowering Hooks
2979//===----------------------------------------------------------------------===//
2980
Bruno Cardoso Lopese654b562010-09-01 00:51:36 +00002981static bool MayFoldLoad(SDValue Op) {
2982 return Op.hasOneUse() && ISD::isNormalLoad(Op.getNode());
2983}
2984
2985static bool MayFoldIntoStore(SDValue Op) {
2986 return Op.hasOneUse() && ISD::isNormalStore(*Op.getNode()->use_begin());
2987}
2988
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002989static bool isTargetShuffle(unsigned Opcode) {
2990 switch(Opcode) {
2991 default: return false;
2992 case X86ISD::PSHUFD:
2993 case X86ISD::PSHUFHW:
2994 case X86ISD::PSHUFLW:
Craig Topperb3982da2011-12-31 23:50:21 +00002995 case X86ISD::SHUFP:
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00002996 case X86ISD::PALIGN:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002997 case X86ISD::MOVLHPS:
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00002998 case X86ISD::MOVLHPD:
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00002999 case X86ISD::MOVHLPS:
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00003000 case X86ISD::MOVLPS:
3001 case X86ISD::MOVLPD:
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00003002 case X86ISD::MOVSHDUP:
Bruno Cardoso Lopes013bb3d2010-08-31 22:35:05 +00003003 case X86ISD::MOVSLDUP:
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00003004 case X86ISD::MOVDDUP:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003005 case X86ISD::MOVSS:
3006 case X86ISD::MOVSD:
Craig Topper34671b82011-12-06 08:21:25 +00003007 case X86ISD::UNPCKL:
3008 case X86ISD::UNPCKH:
Craig Topper316cd2a2011-11-30 06:25:25 +00003009 case X86ISD::VPERMILP:
Craig Topperec24e612011-11-30 07:47:51 +00003010 case X86ISD::VPERM2X128:
Craig Topperbdcbcb32012-05-06 18:54:26 +00003011 case X86ISD::VPERMI:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003012 return true;
3013 }
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003014}
3015
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00003016static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
Craig Topper3d092db2012-03-21 02:14:01 +00003017 SDValue V1, SelectionDAG &DAG) {
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00003018 switch(Opc) {
3019 default: llvm_unreachable("Unknown x86 shuffle node");
3020 case X86ISD::MOVSHDUP:
Bruno Cardoso Lopes013bb3d2010-08-31 22:35:05 +00003021 case X86ISD::MOVSLDUP:
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00003022 case X86ISD::MOVDDUP:
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00003023 return DAG.getNode(Opc, dl, VT, V1);
3024 }
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00003025}
3026
3027static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
Craig Topper3d092db2012-03-21 02:14:01 +00003028 SDValue V1, unsigned TargetMask,
3029 SelectionDAG &DAG) {
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00003030 switch(Opc) {
3031 default: llvm_unreachable("Unknown x86 shuffle node");
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00003032 case X86ISD::PSHUFD:
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00003033 case X86ISD::PSHUFHW:
3034 case X86ISD::PSHUFLW:
Craig Topper316cd2a2011-11-30 06:25:25 +00003035 case X86ISD::VPERMILP:
Craig Topper8325c112012-04-16 00:41:45 +00003036 case X86ISD::VPERMI:
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00003037 return DAG.getNode(Opc, dl, VT, V1, DAG.getConstant(TargetMask, MVT::i8));
3038 }
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00003039}
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00003040
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00003041static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
Craig Topper3d092db2012-03-21 02:14:01 +00003042 SDValue V1, SDValue V2, unsigned TargetMask,
3043 SelectionDAG &DAG) {
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00003044 switch(Opc) {
3045 default: llvm_unreachable("Unknown x86 shuffle node");
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00003046 case X86ISD::PALIGN:
Craig Topperb3982da2011-12-31 23:50:21 +00003047 case X86ISD::SHUFP:
Craig Topperec24e612011-11-30 07:47:51 +00003048 case X86ISD::VPERM2X128:
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00003049 return DAG.getNode(Opc, dl, VT, V1, V2,
3050 DAG.getConstant(TargetMask, MVT::i8));
3051 }
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00003052}
3053
3054static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
3055 SDValue V1, SDValue V2, SelectionDAG &DAG) {
3056 switch(Opc) {
3057 default: llvm_unreachable("Unknown x86 shuffle node");
3058 case X86ISD::MOVLHPS:
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00003059 case X86ISD::MOVLHPD:
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00003060 case X86ISD::MOVHLPS:
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00003061 case X86ISD::MOVLPS:
3062 case X86ISD::MOVLPD:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003063 case X86ISD::MOVSS:
3064 case X86ISD::MOVSD:
Craig Topper34671b82011-12-06 08:21:25 +00003065 case X86ISD::UNPCKL:
3066 case X86ISD::UNPCKH:
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00003067 return DAG.getNode(Opc, dl, VT, V1, V2);
3068 }
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00003069}
3070
Dan Gohmand858e902010-04-17 15:26:15 +00003071SDValue X86TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) const {
Anton Korobeynikova2780e12007-08-15 17:12:32 +00003072 MachineFunction &MF = DAG.getMachineFunction();
3073 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
3074 int ReturnAddrIndex = FuncInfo->getRAIndex();
3075
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00003076 if (ReturnAddrIndex == 0) {
3077 // Set up a frame object for the return address.
Michael Liaoaa3c2c02012-10-25 06:29:14 +00003078 unsigned SlotSize = RegInfo->getSlotSize();
David Greene3f2bf852009-11-12 20:49:22 +00003079 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(SlotSize, -SlotSize,
Evan Chenged2ae132010-07-03 00:40:23 +00003080 false);
Anton Korobeynikova2780e12007-08-15 17:12:32 +00003081 FuncInfo->setRAIndex(ReturnAddrIndex);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00003082 }
3083
Evan Cheng25ab6902006-09-08 06:48:29 +00003084 return DAG.getFrameIndex(ReturnAddrIndex, getPointerTy());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00003085}
3086
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00003087bool X86::isOffsetSuitableForCodeModel(int64_t Offset, CodeModel::Model M,
3088 bool hasSymbolicDisplacement) {
3089 // Offset should fit into 32 bit immediate field.
Benjamin Kramer34247a02010-03-29 21:13:41 +00003090 if (!isInt<32>(Offset))
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00003091 return false;
3092
3093 // If we don't have a symbolic displacement - we don't have any extra
3094 // restrictions.
3095 if (!hasSymbolicDisplacement)
3096 return true;
3097
3098 // FIXME: Some tweaks might be needed for medium code model.
3099 if (M != CodeModel::Small && M != CodeModel::Kernel)
3100 return false;
3101
3102 // For small code model we assume that latest object is 16MB before end of 31
3103 // bits boundary. We may also accept pretty large negative constants knowing
3104 // that all objects are in the positive half of address space.
3105 if (M == CodeModel::Small && Offset < 16*1024*1024)
3106 return true;
3107
3108 // For kernel code model we know that all object resist in the negative half
3109 // of 32bits address space. We may not accept negative offsets, since they may
3110 // be just off and we may accept pretty large positive ones.
3111 if (M == CodeModel::Kernel && Offset > 0)
3112 return true;
3113
3114 return false;
3115}
3116
Evan Chengef41ff62011-06-23 17:54:54 +00003117/// isCalleePop - Determines whether the callee is required to pop its
3118/// own arguments. Callee pop is necessary to support tail calls.
3119bool X86::isCalleePop(CallingConv::ID CallingConv,
3120 bool is64Bit, bool IsVarArg, bool TailCallOpt) {
3121 if (IsVarArg)
3122 return false;
3123
3124 switch (CallingConv) {
3125 default:
3126 return false;
3127 case CallingConv::X86_StdCall:
3128 return !is64Bit;
3129 case CallingConv::X86_FastCall:
3130 return !is64Bit;
3131 case CallingConv::X86_ThisCall:
3132 return !is64Bit;
3133 case CallingConv::Fast:
3134 return TailCallOpt;
3135 case CallingConv::GHC:
3136 return TailCallOpt;
Duncan Sandsdc7f1742012-11-16 12:36:39 +00003137 case CallingConv::HiPE:
3138 return TailCallOpt;
Evan Chengef41ff62011-06-23 17:54:54 +00003139 }
3140}
3141
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003142/// TranslateX86CC - do a one to one translation of a ISD::CondCode to the X86
3143/// specific condition code, returning the condition code and the LHS/RHS of the
3144/// comparison to make.
3145static unsigned TranslateX86CC(ISD::CondCode SetCCOpcode, bool isFP,
3146 SDValue &LHS, SDValue &RHS, SelectionDAG &DAG) {
Evan Chengd9558e02006-01-06 00:43:03 +00003147 if (!isFP) {
Chris Lattnerbfd68a72006-09-13 17:04:54 +00003148 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS)) {
3149 if (SetCCOpcode == ISD::SETGT && RHSC->isAllOnesValue()) {
3150 // X > -1 -> X == 0, jump !sign.
3151 RHS = DAG.getConstant(0, RHS.getValueType());
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003152 return X86::COND_NS;
Craig Topper69947b92012-04-23 06:57:04 +00003153 }
3154 if (SetCCOpcode == ISD::SETLT && RHSC->isNullValue()) {
Chris Lattnerbfd68a72006-09-13 17:04:54 +00003155 // X < 0 -> X == 0, jump on sign.
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003156 return X86::COND_S;
Craig Topper69947b92012-04-23 06:57:04 +00003157 }
3158 if (SetCCOpcode == ISD::SETLT && RHSC->getZExtValue() == 1) {
Dan Gohman5f6913c2007-09-17 14:49:27 +00003159 // X < 1 -> X <= 0
3160 RHS = DAG.getConstant(0, RHS.getValueType());
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003161 return X86::COND_LE;
Chris Lattnerbfd68a72006-09-13 17:04:54 +00003162 }
Chris Lattnerf9570512006-09-13 03:22:10 +00003163 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00003164
Evan Chengd9558e02006-01-06 00:43:03 +00003165 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00003166 default: llvm_unreachable("Invalid integer condition!");
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003167 case ISD::SETEQ: return X86::COND_E;
3168 case ISD::SETGT: return X86::COND_G;
3169 case ISD::SETGE: return X86::COND_GE;
3170 case ISD::SETLT: return X86::COND_L;
3171 case ISD::SETLE: return X86::COND_LE;
3172 case ISD::SETNE: return X86::COND_NE;
3173 case ISD::SETULT: return X86::COND_B;
3174 case ISD::SETUGT: return X86::COND_A;
3175 case ISD::SETULE: return X86::COND_BE;
3176 case ISD::SETUGE: return X86::COND_AE;
Evan Chengd9558e02006-01-06 00:43:03 +00003177 }
Chris Lattner4c78e022008-12-23 23:42:27 +00003178 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003179
Chris Lattner4c78e022008-12-23 23:42:27 +00003180 // First determine if it is required or is profitable to flip the operands.
Duncan Sands4047f4a2008-10-24 13:03:10 +00003181
Chris Lattner4c78e022008-12-23 23:42:27 +00003182 // If LHS is a foldable load, but RHS is not, flip the condition.
Rafael Espindolaf297c932011-02-03 03:58:05 +00003183 if (ISD::isNON_EXTLoad(LHS.getNode()) &&
3184 !ISD::isNON_EXTLoad(RHS.getNode())) {
Chris Lattner4c78e022008-12-23 23:42:27 +00003185 SetCCOpcode = getSetCCSwappedOperands(SetCCOpcode);
3186 std::swap(LHS, RHS);
Evan Cheng4d46d0a2008-08-28 23:48:31 +00003187 }
3188
Chris Lattner4c78e022008-12-23 23:42:27 +00003189 switch (SetCCOpcode) {
3190 default: break;
3191 case ISD::SETOLT:
3192 case ISD::SETOLE:
3193 case ISD::SETUGT:
3194 case ISD::SETUGE:
3195 std::swap(LHS, RHS);
3196 break;
3197 }
3198
3199 // On a floating point condition, the flags are set as follows:
3200 // ZF PF CF op
3201 // 0 | 0 | 0 | X > Y
3202 // 0 | 0 | 1 | X < Y
3203 // 1 | 0 | 0 | X == Y
3204 // 1 | 1 | 1 | unordered
3205 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00003206 default: llvm_unreachable("Condcode should be pre-legalized away");
Chris Lattner4c78e022008-12-23 23:42:27 +00003207 case ISD::SETUEQ:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003208 case ISD::SETEQ: return X86::COND_E;
Chris Lattner4c78e022008-12-23 23:42:27 +00003209 case ISD::SETOLT: // flipped
3210 case ISD::SETOGT:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003211 case ISD::SETGT: return X86::COND_A;
Chris Lattner4c78e022008-12-23 23:42:27 +00003212 case ISD::SETOLE: // flipped
3213 case ISD::SETOGE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003214 case ISD::SETGE: return X86::COND_AE;
Chris Lattner4c78e022008-12-23 23:42:27 +00003215 case ISD::SETUGT: // flipped
3216 case ISD::SETULT:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003217 case ISD::SETLT: return X86::COND_B;
Chris Lattner4c78e022008-12-23 23:42:27 +00003218 case ISD::SETUGE: // flipped
3219 case ISD::SETULE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003220 case ISD::SETLE: return X86::COND_BE;
Chris Lattner4c78e022008-12-23 23:42:27 +00003221 case ISD::SETONE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003222 case ISD::SETNE: return X86::COND_NE;
3223 case ISD::SETUO: return X86::COND_P;
3224 case ISD::SETO: return X86::COND_NP;
Dan Gohman1a492952009-10-20 16:22:37 +00003225 case ISD::SETOEQ:
3226 case ISD::SETUNE: return X86::COND_INVALID;
Chris Lattner4c78e022008-12-23 23:42:27 +00003227 }
Evan Chengd9558e02006-01-06 00:43:03 +00003228}
3229
Evan Cheng4a460802006-01-11 00:33:36 +00003230/// hasFPCMov - is there a floating point cmov for the specific X86 condition
3231/// code. Current x86 isa includes the following FP cmov instructions:
Evan Chengaaca22c2006-01-10 20:26:56 +00003232/// fcmovb, fcomvbe, fcomve, fcmovu, fcmovae, fcmova, fcmovne, fcmovnu.
Evan Cheng4a460802006-01-11 00:33:36 +00003233static bool hasFPCMov(unsigned X86CC) {
Evan Chengaaca22c2006-01-10 20:26:56 +00003234 switch (X86CC) {
3235 default:
3236 return false;
Chris Lattner7fbe9722006-10-20 17:42:20 +00003237 case X86::COND_B:
3238 case X86::COND_BE:
3239 case X86::COND_E:
3240 case X86::COND_P:
3241 case X86::COND_A:
3242 case X86::COND_AE:
3243 case X86::COND_NE:
3244 case X86::COND_NP:
Evan Chengaaca22c2006-01-10 20:26:56 +00003245 return true;
3246 }
3247}
3248
Evan Chengeb2f9692009-10-27 19:56:55 +00003249/// isFPImmLegal - Returns true if the target can instruction select the
3250/// specified FP immediate natively. If false, the legalizer will
3251/// materialize the FP immediate as a load from a constant pool.
Evan Chenga1eaa3c2009-10-28 01:43:28 +00003252bool X86TargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
Evan Chengeb2f9692009-10-27 19:56:55 +00003253 for (unsigned i = 0, e = LegalFPImmediates.size(); i != e; ++i) {
3254 if (Imm.bitwiseIsEqual(LegalFPImmediates[i]))
3255 return true;
3256 }
3257 return false;
3258}
3259
Nate Begeman9008ca62009-04-27 18:41:29 +00003260/// isUndefOrInRange - Return true if Val is undef or if its value falls within
3261/// the specified range (L, H].
3262static bool isUndefOrInRange(int Val, int Low, int Hi) {
3263 return (Val < 0) || (Val >= Low && Val < Hi);
3264}
3265
3266/// isUndefOrEqual - Val is either less than zero (undef) or equal to the
3267/// specified value.
3268static bool isUndefOrEqual(int Val, int CmpVal) {
Jakub Staszakb2af3a02012-12-06 18:22:59 +00003269 return (Val < 0 || Val == CmpVal);
Evan Chengc5cdff22006-04-07 21:53:05 +00003270}
3271
Benjamin Kramerd9b0b022012-06-02 10:20:22 +00003272/// isSequentialOrUndefInRange - Return true if every element in Mask, beginning
Bruno Cardoso Lopes4002d7e2011-08-12 21:54:42 +00003273/// from position Pos and ending in Pos+Size, falls within the specified
3274/// sequential range (L, L+Pos]. or is undef.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003275static bool isSequentialOrUndefInRange(ArrayRef<int> Mask,
Craig Topperb6072642012-05-03 07:26:59 +00003276 unsigned Pos, unsigned Size, int Low) {
3277 for (unsigned i = Pos, e = Pos+Size; i != e; ++i, ++Low)
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003278 if (!isUndefOrEqual(Mask[i], Low))
3279 return false;
3280 return true;
3281}
3282
Nate Begeman9008ca62009-04-27 18:41:29 +00003283/// isPSHUFDMask - Return true if the node specifies a shuffle of elements that
3284/// is suitable for input to PSHUFD or PSHUFW. That is, it doesn't reference
3285/// the second operand.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003286static bool isPSHUFDMask(ArrayRef<int> Mask, EVT VT) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00003287 if (VT == MVT::v4f32 || VT == MVT::v4i32 )
Nate Begeman9008ca62009-04-27 18:41:29 +00003288 return (Mask[0] < 4 && Mask[1] < 4 && Mask[2] < 4 && Mask[3] < 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00003289 if (VT == MVT::v2f64 || VT == MVT::v2i64)
Nate Begeman9008ca62009-04-27 18:41:29 +00003290 return (Mask[0] < 2 && Mask[1] < 2);
3291 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003292}
3293
Nate Begeman9008ca62009-04-27 18:41:29 +00003294/// isPSHUFHWMask - Return true if the node specifies a shuffle of elements that
3295/// is suitable for input to PSHUFHW.
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00003296static bool isPSHUFHWMask(ArrayRef<int> Mask, EVT VT, bool HasInt256) {
3297 if (VT != MVT::v8i16 && (!HasInt256 || VT != MVT::v16i16))
Evan Cheng0188ecb2006-03-22 18:59:22 +00003298 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003299
Nate Begeman9008ca62009-04-27 18:41:29 +00003300 // Lower quadword copied in order or undef.
Craig Topperc612d792012-01-02 09:17:37 +00003301 if (!isSequentialOrUndefInRange(Mask, 0, 4, 0))
3302 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003303
Evan Cheng506d3df2006-03-29 23:07:14 +00003304 // Upper quadword shuffled.
Craig Topperc612d792012-01-02 09:17:37 +00003305 for (unsigned i = 4; i != 8; ++i)
Craig Toppera9a568a2012-05-02 08:03:44 +00003306 if (!isUndefOrInRange(Mask[i], 4, 8))
Evan Cheng506d3df2006-03-29 23:07:14 +00003307 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003308
Craig Toppera9a568a2012-05-02 08:03:44 +00003309 if (VT == MVT::v16i16) {
3310 // Lower quadword copied in order or undef.
3311 if (!isSequentialOrUndefInRange(Mask, 8, 4, 8))
3312 return false;
3313
3314 // Upper quadword shuffled.
3315 for (unsigned i = 12; i != 16; ++i)
3316 if (!isUndefOrInRange(Mask[i], 12, 16))
3317 return false;
3318 }
3319
Evan Cheng506d3df2006-03-29 23:07:14 +00003320 return true;
3321}
3322
Nate Begeman9008ca62009-04-27 18:41:29 +00003323/// isPSHUFLWMask - Return true if the node specifies a shuffle of elements that
3324/// is suitable for input to PSHUFLW.
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00003325static bool isPSHUFLWMask(ArrayRef<int> Mask, EVT VT, bool HasInt256) {
3326 if (VT != MVT::v8i16 && (!HasInt256 || VT != MVT::v16i16))
Evan Cheng506d3df2006-03-29 23:07:14 +00003327 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003328
Rafael Espindola15684b22009-04-24 12:40:33 +00003329 // Upper quadword copied in order.
Craig Topperc612d792012-01-02 09:17:37 +00003330 if (!isSequentialOrUndefInRange(Mask, 4, 4, 4))
3331 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003332
Rafael Espindola15684b22009-04-24 12:40:33 +00003333 // Lower quadword shuffled.
Craig Topperc612d792012-01-02 09:17:37 +00003334 for (unsigned i = 0; i != 4; ++i)
Craig Toppera9a568a2012-05-02 08:03:44 +00003335 if (!isUndefOrInRange(Mask[i], 0, 4))
Rafael Espindola15684b22009-04-24 12:40:33 +00003336 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003337
Craig Toppera9a568a2012-05-02 08:03:44 +00003338 if (VT == MVT::v16i16) {
3339 // Upper quadword copied in order.
3340 if (!isSequentialOrUndefInRange(Mask, 12, 4, 12))
3341 return false;
3342
3343 // Lower quadword shuffled.
3344 for (unsigned i = 8; i != 12; ++i)
3345 if (!isUndefOrInRange(Mask[i], 8, 12))
3346 return false;
3347 }
3348
Rafael Espindola15684b22009-04-24 12:40:33 +00003349 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00003350}
3351
Nate Begemana09008b2009-10-19 02:17:23 +00003352/// isPALIGNRMask - Return true if the node specifies a shuffle of elements that
3353/// is suitable for input to PALIGNR.
Craig Topper0e2037b2012-01-20 05:53:00 +00003354static bool isPALIGNRMask(ArrayRef<int> Mask, EVT VT,
3355 const X86Subtarget *Subtarget) {
3356 if ((VT.getSizeInBits() == 128 && !Subtarget->hasSSSE3()) ||
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00003357 (VT.getSizeInBits() == 256 && !Subtarget->hasInt256()))
Bruno Cardoso Lopes9065d4b2011-07-29 01:30:59 +00003358 return false;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003359
Craig Topper0e2037b2012-01-20 05:53:00 +00003360 unsigned NumElts = VT.getVectorNumElements();
3361 unsigned NumLanes = VT.getSizeInBits()/128;
3362 unsigned NumLaneElts = NumElts/NumLanes;
3363
3364 // Do not handle 64-bit element shuffles with palignr.
3365 if (NumLaneElts == 2)
Nate Begemana09008b2009-10-19 02:17:23 +00003366 return false;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003367
Craig Topper0e2037b2012-01-20 05:53:00 +00003368 for (unsigned l = 0; l != NumElts; l+=NumLaneElts) {
3369 unsigned i;
3370 for (i = 0; i != NumLaneElts; ++i) {
3371 if (Mask[i+l] >= 0)
3372 break;
3373 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00003374
Craig Topper0e2037b2012-01-20 05:53:00 +00003375 // Lane is all undef, go to next lane
3376 if (i == NumLaneElts)
3377 continue;
Nate Begemana09008b2009-10-19 02:17:23 +00003378
Craig Topper0e2037b2012-01-20 05:53:00 +00003379 int Start = Mask[i+l];
Nate Begemana09008b2009-10-19 02:17:23 +00003380
Craig Topper0e2037b2012-01-20 05:53:00 +00003381 // Make sure its in this lane in one of the sources
3382 if (!isUndefOrInRange(Start, l, l+NumLaneElts) &&
3383 !isUndefOrInRange(Start, l+NumElts, l+NumElts+NumLaneElts))
Nate Begemana09008b2009-10-19 02:17:23 +00003384 return false;
Craig Topper0e2037b2012-01-20 05:53:00 +00003385
3386 // If not lane 0, then we must match lane 0
3387 if (l != 0 && Mask[i] >= 0 && !isUndefOrEqual(Start, Mask[i]+l))
3388 return false;
3389
3390 // Correct second source to be contiguous with first source
3391 if (Start >= (int)NumElts)
3392 Start -= NumElts - NumLaneElts;
3393
3394 // Make sure we're shifting in the right direction.
3395 if (Start <= (int)(i+l))
3396 return false;
3397
3398 Start -= i;
3399
3400 // Check the rest of the elements to see if they are consecutive.
3401 for (++i; i != NumLaneElts; ++i) {
3402 int Idx = Mask[i+l];
3403
3404 // Make sure its in this lane
3405 if (!isUndefOrInRange(Idx, l, l+NumLaneElts) &&
3406 !isUndefOrInRange(Idx, l+NumElts, l+NumElts+NumLaneElts))
3407 return false;
3408
3409 // If not lane 0, then we must match lane 0
3410 if (l != 0 && Mask[i] >= 0 && !isUndefOrEqual(Idx, Mask[i]+l))
3411 return false;
3412
3413 if (Idx >= (int)NumElts)
3414 Idx -= NumElts - NumLaneElts;
3415
3416 if (!isUndefOrEqual(Idx, Start+i))
3417 return false;
3418
3419 }
Nate Begemana09008b2009-10-19 02:17:23 +00003420 }
Craig Topper0e2037b2012-01-20 05:53:00 +00003421
Nate Begemana09008b2009-10-19 02:17:23 +00003422 return true;
3423}
3424
Craig Topper1a7700a2012-01-19 08:19:12 +00003425/// CommuteVectorShuffleMask - Change values in a shuffle permute mask assuming
3426/// the two vector operands have swapped position.
3427static void CommuteVectorShuffleMask(SmallVectorImpl<int> &Mask,
3428 unsigned NumElems) {
3429 for (unsigned i = 0; i != NumElems; ++i) {
3430 int idx = Mask[i];
3431 if (idx < 0)
3432 continue;
3433 else if (idx < (int)NumElems)
3434 Mask[i] = idx + NumElems;
3435 else
3436 Mask[i] = idx - NumElems;
3437 }
3438}
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00003439
Craig Topper1a7700a2012-01-19 08:19:12 +00003440/// isSHUFPMask - Return true if the specified VECTOR_SHUFFLE operand
3441/// specifies a shuffle of elements that is suitable for input to 128/256-bit
3442/// SHUFPS and SHUFPD. If Commuted is true, then it checks for sources to be
3443/// reverse of what x86 shuffles want.
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00003444static bool isSHUFPMask(ArrayRef<int> Mask, EVT VT, bool HasFp256,
Craig Topper1a7700a2012-01-19 08:19:12 +00003445 bool Commuted = false) {
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00003446 if (!HasFp256 && VT.getSizeInBits() == 256)
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00003447 return false;
3448
Craig Topper1a7700a2012-01-19 08:19:12 +00003449 unsigned NumElems = VT.getVectorNumElements();
3450 unsigned NumLanes = VT.getSizeInBits()/128;
3451 unsigned NumLaneElems = NumElems/NumLanes;
3452
3453 if (NumLaneElems != 2 && NumLaneElems != 4)
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00003454 return false;
3455
3456 // VSHUFPSY divides the resulting vector into 4 chunks.
3457 // The sources are also splitted into 4 chunks, and each destination
3458 // chunk must come from a different source chunk.
3459 //
3460 // SRC1 => X7 X6 X5 X4 X3 X2 X1 X0
3461 // SRC2 => Y7 Y6 Y5 Y4 Y3 Y2 Y1 Y9
3462 //
3463 // DST => Y7..Y4, Y7..Y4, X7..X4, X7..X4,
3464 // Y3..Y0, Y3..Y0, X3..X0, X3..X0
3465 //
Craig Topper9d7025b2011-11-27 21:41:12 +00003466 // VSHUFPDY divides the resulting vector into 4 chunks.
3467 // The sources are also splitted into 4 chunks, and each destination
3468 // chunk must come from a different source chunk.
3469 //
3470 // SRC1 => X3 X2 X1 X0
3471 // SRC2 => Y3 Y2 Y1 Y0
3472 //
3473 // DST => Y3..Y2, X3..X2, Y1..Y0, X1..X0
3474 //
Craig Topper1a7700a2012-01-19 08:19:12 +00003475 unsigned HalfLaneElems = NumLaneElems/2;
3476 for (unsigned l = 0; l != NumElems; l += NumLaneElems) {
3477 for (unsigned i = 0; i != NumLaneElems; ++i) {
3478 int Idx = Mask[i+l];
3479 unsigned RngStart = l + ((Commuted == (i<HalfLaneElems)) ? NumElems : 0);
3480 if (!isUndefOrInRange(Idx, RngStart, RngStart+NumLaneElems))
3481 return false;
3482 // For VSHUFPSY, the mask of the second half must be the same as the
3483 // first but with the appropriate offsets. This works in the same way as
3484 // VPERMILPS works with masks.
3485 if (NumElems != 8 || l == 0 || Mask[i] < 0)
3486 continue;
3487 if (!isUndefOrEqual(Idx, Mask[i]+l))
3488 return false;
Craig Topper1ff73d72011-12-06 04:59:07 +00003489 }
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00003490 }
3491
3492 return true;
3493}
3494
Evan Cheng2c0dbd02006-03-24 02:58:06 +00003495/// isMOVHLPSMask - Return true if the specified VECTOR_SHUFFLE operand
3496/// specifies a shuffle of elements that is suitable for input to MOVHLPS.
Craig Topperdd637ae2012-02-19 05:41:45 +00003497static bool isMOVHLPSMask(ArrayRef<int> Mask, EVT VT) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00003498 if (!VT.is128BitVector())
Bruno Cardoso Lopes61260052011-07-29 02:05:28 +00003499 return false;
3500
Craig Topper7a9a28b2012-08-12 02:23:29 +00003501 unsigned NumElems = VT.getVectorNumElements();
3502
Bruno Cardoso Lopes61260052011-07-29 02:05:28 +00003503 if (NumElems != 4)
Evan Cheng2c0dbd02006-03-24 02:58:06 +00003504 return false;
3505
Evan Cheng2064a2b2006-03-28 06:50:32 +00003506 // Expect bit0 == 6, bit1 == 7, bit2 == 2, bit3 == 3
Craig Topperdd637ae2012-02-19 05:41:45 +00003507 return isUndefOrEqual(Mask[0], 6) &&
3508 isUndefOrEqual(Mask[1], 7) &&
3509 isUndefOrEqual(Mask[2], 2) &&
3510 isUndefOrEqual(Mask[3], 3);
Evan Cheng6e56e2c2006-11-07 22:14:24 +00003511}
3512
Nate Begeman0b10b912009-11-07 23:17:15 +00003513/// isMOVHLPS_v_undef_Mask - Special case of isMOVHLPSMask for canonical form
3514/// of vector_shuffle v, v, <2, 3, 2, 3>, i.e. vector_shuffle v, undef,
3515/// <2, 3, 2, 3>
Craig Topperdd637ae2012-02-19 05:41:45 +00003516static bool isMOVHLPS_v_undef_Mask(ArrayRef<int> Mask, EVT VT) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00003517 if (!VT.is128BitVector())
Bruno Cardoso Lopes61260052011-07-29 02:05:28 +00003518 return false;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003519
Craig Topper7a9a28b2012-08-12 02:23:29 +00003520 unsigned NumElems = VT.getVectorNumElements();
3521
Nate Begeman0b10b912009-11-07 23:17:15 +00003522 if (NumElems != 4)
3523 return false;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003524
Craig Topperdd637ae2012-02-19 05:41:45 +00003525 return isUndefOrEqual(Mask[0], 2) &&
3526 isUndefOrEqual(Mask[1], 3) &&
3527 isUndefOrEqual(Mask[2], 2) &&
3528 isUndefOrEqual(Mask[3], 3);
Nate Begeman0b10b912009-11-07 23:17:15 +00003529}
3530
Evan Cheng5ced1d82006-04-06 23:23:56 +00003531/// isMOVLPMask - Return true if the specified VECTOR_SHUFFLE operand
3532/// specifies a shuffle of elements that is suitable for input to MOVLP{S|D}.
Craig Topperdd637ae2012-02-19 05:41:45 +00003533static bool isMOVLPMask(ArrayRef<int> Mask, EVT VT) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00003534 if (!VT.is128BitVector())
Elena Demikhovsky021c0a22011-12-28 08:14:01 +00003535 return false;
3536
Craig Topperdd637ae2012-02-19 05:41:45 +00003537 unsigned NumElems = VT.getVectorNumElements();
Evan Cheng5ced1d82006-04-06 23:23:56 +00003538
Evan Cheng5ced1d82006-04-06 23:23:56 +00003539 if (NumElems != 2 && NumElems != 4)
3540 return false;
3541
Chad Rosier238ae312012-04-30 17:47:15 +00003542 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
Craig Topperdd637ae2012-02-19 05:41:45 +00003543 if (!isUndefOrEqual(Mask[i], i + NumElems))
Evan Chengc5cdff22006-04-07 21:53:05 +00003544 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003545
Chad Rosier238ae312012-04-30 17:47:15 +00003546 for (unsigned i = NumElems/2, e = NumElems; i != e; ++i)
Craig Topperdd637ae2012-02-19 05:41:45 +00003547 if (!isUndefOrEqual(Mask[i], i))
Evan Chengc5cdff22006-04-07 21:53:05 +00003548 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003549
3550 return true;
3551}
3552
Nate Begeman0b10b912009-11-07 23:17:15 +00003553/// isMOVLHPSMask - Return true if the specified VECTOR_SHUFFLE operand
3554/// specifies a shuffle of elements that is suitable for input to MOVLHPS.
Craig Topperdd637ae2012-02-19 05:41:45 +00003555static bool isMOVLHPSMask(ArrayRef<int> Mask, EVT VT) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00003556 if (!VT.is128BitVector())
3557 return false;
3558
Craig Topperdd637ae2012-02-19 05:41:45 +00003559 unsigned NumElems = VT.getVectorNumElements();
Evan Cheng5ced1d82006-04-06 23:23:56 +00003560
Craig Topper7a9a28b2012-08-12 02:23:29 +00003561 if (NumElems != 2 && NumElems != 4)
Evan Cheng5ced1d82006-04-06 23:23:56 +00003562 return false;
3563
Chad Rosier238ae312012-04-30 17:47:15 +00003564 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
Craig Topperdd637ae2012-02-19 05:41:45 +00003565 if (!isUndefOrEqual(Mask[i], i))
Evan Chengc5cdff22006-04-07 21:53:05 +00003566 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003567
Chad Rosier238ae312012-04-30 17:47:15 +00003568 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
3569 if (!isUndefOrEqual(Mask[i + e], i + NumElems))
Evan Chengc5cdff22006-04-07 21:53:05 +00003570 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003571
3572 return true;
3573}
3574
Elena Demikhovsky15963732012-06-26 08:04:10 +00003575//
3576// Some special combinations that can be optimized.
3577//
3578static
3579SDValue Compact8x32ShuffleNode(ShuffleVectorSDNode *SVOp,
3580 SelectionDAG &DAG) {
3581 EVT VT = SVOp->getValueType(0);
Elena Demikhovsky15963732012-06-26 08:04:10 +00003582 DebugLoc dl = SVOp->getDebugLoc();
3583
3584 if (VT != MVT::v8i32 && VT != MVT::v8f32)
3585 return SDValue();
3586
3587 ArrayRef<int> Mask = SVOp->getMask();
3588
3589 // These are the special masks that may be optimized.
3590 static const int MaskToOptimizeEven[] = {0, 8, 2, 10, 4, 12, 6, 14};
3591 static const int MaskToOptimizeOdd[] = {1, 9, 3, 11, 5, 13, 7, 15};
3592 bool MatchEvenMask = true;
3593 bool MatchOddMask = true;
3594 for (int i=0; i<8; ++i) {
3595 if (!isUndefOrEqual(Mask[i], MaskToOptimizeEven[i]))
3596 MatchEvenMask = false;
3597 if (!isUndefOrEqual(Mask[i], MaskToOptimizeOdd[i]))
3598 MatchOddMask = false;
3599 }
Elena Demikhovsky15963732012-06-26 08:04:10 +00003600
Elena Demikhovsky32510202012-09-04 12:49:02 +00003601 if (!MatchEvenMask && !MatchOddMask)
Elena Demikhovsky15963732012-06-26 08:04:10 +00003602 return SDValue();
Michael Liao471b9172012-10-03 23:43:52 +00003603
Elena Demikhovsky15963732012-06-26 08:04:10 +00003604 SDValue UndefNode = DAG.getNode(ISD::UNDEF, dl, VT);
3605
Elena Demikhovsky32510202012-09-04 12:49:02 +00003606 SDValue Op0 = SVOp->getOperand(0);
3607 SDValue Op1 = SVOp->getOperand(1);
3608
3609 if (MatchEvenMask) {
3610 // Shift the second operand right to 32 bits.
3611 static const int ShiftRightMask[] = {-1, 0, -1, 2, -1, 4, -1, 6 };
3612 Op1 = DAG.getVectorShuffle(VT, dl, Op1, UndefNode, ShiftRightMask);
3613 } else {
3614 // Shift the first operand left to 32 bits.
3615 static const int ShiftLeftMask[] = {1, -1, 3, -1, 5, -1, 7, -1 };
3616 Op0 = DAG.getVectorShuffle(VT, dl, Op0, UndefNode, ShiftLeftMask);
3617 }
3618 static const int BlendMask[] = {0, 9, 2, 11, 4, 13, 6, 15};
3619 return DAG.getVectorShuffle(VT, dl, Op0, Op1, BlendMask);
Elena Demikhovsky15963732012-06-26 08:04:10 +00003620}
3621
Evan Cheng0038e592006-03-28 00:39:58 +00003622/// isUNPCKLMask - Return true if the specified VECTOR_SHUFFLE operand
3623/// specifies a shuffle of elements that is suitable for input to UNPCKL.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003624static bool isUNPCKLMask(ArrayRef<int> Mask, EVT VT,
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00003625 bool HasInt256, bool V2IsSplat = false) {
Craig Topper94438ba2011-12-16 08:06:31 +00003626 unsigned NumElts = VT.getVectorNumElements();
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003627
3628 assert((VT.is128BitVector() || VT.is256BitVector()) &&
3629 "Unsupported vector type for unpckh");
3630
Craig Topper6347e862011-11-21 06:57:39 +00003631 if (VT.getSizeInBits() == 256 && NumElts != 4 && NumElts != 8 &&
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00003632 (!HasInt256 || (NumElts != 16 && NumElts != 32)))
Evan Cheng0038e592006-03-28 00:39:58 +00003633 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003634
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003635 // Handle 128 and 256-bit vector lengths. AVX defines UNPCK* to operate
3636 // independently on 128-bit lanes.
3637 unsigned NumLanes = VT.getSizeInBits()/128;
3638 unsigned NumLaneElts = NumElts/NumLanes;
David Greenea20244d2011-03-02 17:23:43 +00003639
Craig Topper94438ba2011-12-16 08:06:31 +00003640 for (unsigned l = 0; l != NumLanes; ++l) {
3641 for (unsigned i = l*NumLaneElts, j = l*NumLaneElts;
3642 i != (l+1)*NumLaneElts;
David Greenea20244d2011-03-02 17:23:43 +00003643 i += 2, ++j) {
3644 int BitI = Mask[i];
3645 int BitI1 = Mask[i+1];
3646 if (!isUndefOrEqual(BitI, j))
Evan Cheng39623da2006-04-20 08:58:49 +00003647 return false;
David Greenea20244d2011-03-02 17:23:43 +00003648 if (V2IsSplat) {
3649 if (!isUndefOrEqual(BitI1, NumElts))
3650 return false;
3651 } else {
3652 if (!isUndefOrEqual(BitI1, j + NumElts))
3653 return false;
3654 }
Evan Cheng39623da2006-04-20 08:58:49 +00003655 }
Evan Cheng0038e592006-03-28 00:39:58 +00003656 }
David Greenea20244d2011-03-02 17:23:43 +00003657
Evan Cheng0038e592006-03-28 00:39:58 +00003658 return true;
3659}
3660
Evan Cheng4fcb9222006-03-28 02:43:26 +00003661/// isUNPCKHMask - Return true if the specified VECTOR_SHUFFLE operand
3662/// specifies a shuffle of elements that is suitable for input to UNPCKH.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003663static bool isUNPCKHMask(ArrayRef<int> Mask, EVT VT,
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00003664 bool HasInt256, bool V2IsSplat = false) {
Craig Topper94438ba2011-12-16 08:06:31 +00003665 unsigned NumElts = VT.getVectorNumElements();
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003666
3667 assert((VT.is128BitVector() || VT.is256BitVector()) &&
3668 "Unsupported vector type for unpckh");
3669
Craig Topper6347e862011-11-21 06:57:39 +00003670 if (VT.getSizeInBits() == 256 && NumElts != 4 && NumElts != 8 &&
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00003671 (!HasInt256 || (NumElts != 16 && NumElts != 32)))
Evan Cheng4fcb9222006-03-28 02:43:26 +00003672 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003673
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003674 // Handle 128 and 256-bit vector lengths. AVX defines UNPCK* to operate
3675 // independently on 128-bit lanes.
3676 unsigned NumLanes = VT.getSizeInBits()/128;
3677 unsigned NumLaneElts = NumElts/NumLanes;
3678
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003679 for (unsigned l = 0; l != NumLanes; ++l) {
Craig Topper94438ba2011-12-16 08:06:31 +00003680 for (unsigned i = l*NumLaneElts, j = (l*NumLaneElts)+NumLaneElts/2;
3681 i != (l+1)*NumLaneElts; i += 2, ++j) {
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003682 int BitI = Mask[i];
3683 int BitI1 = Mask[i+1];
3684 if (!isUndefOrEqual(BitI, j))
Evan Cheng39623da2006-04-20 08:58:49 +00003685 return false;
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003686 if (V2IsSplat) {
3687 if (isUndefOrEqual(BitI1, NumElts))
3688 return false;
3689 } else {
3690 if (!isUndefOrEqual(BitI1, j+NumElts))
3691 return false;
3692 }
Evan Cheng39623da2006-04-20 08:58:49 +00003693 }
Evan Cheng4fcb9222006-03-28 02:43:26 +00003694 }
Evan Cheng4fcb9222006-03-28 02:43:26 +00003695 return true;
3696}
3697
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003698/// isUNPCKL_v_undef_Mask - Special case of isUNPCKLMask for canonical form
3699/// of vector_shuffle v, v, <0, 4, 1, 5>, i.e. vector_shuffle v, undef,
3700/// <0, 0, 1, 1>
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003701static bool isUNPCKL_v_undef_Mask(ArrayRef<int> Mask, EVT VT,
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00003702 bool HasInt256) {
Craig Topper94438ba2011-12-16 08:06:31 +00003703 unsigned NumElts = VT.getVectorNumElements();
3704
3705 assert((VT.is128BitVector() || VT.is256BitVector()) &&
3706 "Unsupported vector type for unpckh");
3707
3708 if (VT.getSizeInBits() == 256 && NumElts != 4 && NumElts != 8 &&
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00003709 (!HasInt256 || (NumElts != 16 && NumElts != 32)))
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003710 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003711
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00003712 // For 256-bit i64/f64, use MOVDDUPY instead, so reject the matching pattern
3713 // FIXME: Need a better way to get rid of this, there's no latency difference
3714 // between UNPCKLPD and MOVDDUP, the later should always be checked first and
3715 // the former later. We should also remove the "_undef" special mask.
Craig Topper94438ba2011-12-16 08:06:31 +00003716 if (NumElts == 4 && VT.getSizeInBits() == 256)
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00003717 return false;
3718
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003719 // Handle 128 and 256-bit vector lengths. AVX defines UNPCK* to operate
3720 // independently on 128-bit lanes.
Craig Topper94438ba2011-12-16 08:06:31 +00003721 unsigned NumLanes = VT.getSizeInBits()/128;
3722 unsigned NumLaneElts = NumElts/NumLanes;
David Greenea20244d2011-03-02 17:23:43 +00003723
Craig Topper94438ba2011-12-16 08:06:31 +00003724 for (unsigned l = 0; l != NumLanes; ++l) {
3725 for (unsigned i = l*NumLaneElts, j = l*NumLaneElts;
3726 i != (l+1)*NumLaneElts;
David Greenea20244d2011-03-02 17:23:43 +00003727 i += 2, ++j) {
3728 int BitI = Mask[i];
3729 int BitI1 = Mask[i+1];
3730
3731 if (!isUndefOrEqual(BitI, j))
3732 return false;
3733 if (!isUndefOrEqual(BitI1, j))
3734 return false;
3735 }
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003736 }
David Greenea20244d2011-03-02 17:23:43 +00003737
Rafael Espindola15684b22009-04-24 12:40:33 +00003738 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00003739}
3740
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003741/// isUNPCKH_v_undef_Mask - Special case of isUNPCKHMask for canonical form
3742/// of vector_shuffle v, v, <2, 6, 3, 7>, i.e. vector_shuffle v, undef,
3743/// <2, 2, 3, 3>
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00003744static bool isUNPCKH_v_undef_Mask(ArrayRef<int> Mask, EVT VT, bool HasInt256) {
Craig Topper94438ba2011-12-16 08:06:31 +00003745 unsigned NumElts = VT.getVectorNumElements();
3746
3747 assert((VT.is128BitVector() || VT.is256BitVector()) &&
3748 "Unsupported vector type for unpckh");
3749
3750 if (VT.getSizeInBits() == 256 && NumElts != 4 && NumElts != 8 &&
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00003751 (!HasInt256 || (NumElts != 16 && NumElts != 32)))
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003752 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003753
Craig Topper94438ba2011-12-16 08:06:31 +00003754 // Handle 128 and 256-bit vector lengths. AVX defines UNPCK* to operate
3755 // independently on 128-bit lanes.
3756 unsigned NumLanes = VT.getSizeInBits()/128;
3757 unsigned NumLaneElts = NumElts/NumLanes;
3758
3759 for (unsigned l = 0; l != NumLanes; ++l) {
3760 for (unsigned i = l*NumLaneElts, j = (l*NumLaneElts)+NumLaneElts/2;
3761 i != (l+1)*NumLaneElts; i += 2, ++j) {
3762 int BitI = Mask[i];
3763 int BitI1 = Mask[i+1];
3764 if (!isUndefOrEqual(BitI, j))
3765 return false;
3766 if (!isUndefOrEqual(BitI1, j))
3767 return false;
3768 }
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003769 }
Rafael Espindola15684b22009-04-24 12:40:33 +00003770 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00003771}
3772
Evan Cheng017dcc62006-04-21 01:05:10 +00003773/// isMOVLMask - Return true if the specified VECTOR_SHUFFLE operand
3774/// specifies a shuffle of elements that is suitable for input to MOVSS,
3775/// MOVSD, and MOVD, i.e. setting the lowest element.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003776static bool isMOVLMask(ArrayRef<int> Mask, EVT VT) {
Eli Friedman10415532009-06-06 06:05:10 +00003777 if (VT.getVectorElementType().getSizeInBits() < 32)
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003778 return false;
Craig Topper7a9a28b2012-08-12 02:23:29 +00003779 if (!VT.is128BitVector())
Elena Demikhovsky021c0a22011-12-28 08:14:01 +00003780 return false;
Eli Friedman10415532009-06-06 06:05:10 +00003781
Craig Topperc612d792012-01-02 09:17:37 +00003782 unsigned NumElts = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00003783
Nate Begeman9008ca62009-04-27 18:41:29 +00003784 if (!isUndefOrEqual(Mask[0], NumElts))
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003785 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003786
Craig Topperc612d792012-01-02 09:17:37 +00003787 for (unsigned i = 1; i != NumElts; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003788 if (!isUndefOrEqual(Mask[i], i))
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003789 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003790
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003791 return true;
3792}
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003793
Craig Topper70b883b2011-11-28 10:14:51 +00003794/// isVPERM2X128Mask - Match 256-bit shuffles where the elements are considered
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003795/// as permutations between 128-bit chunks or halves. As an example: this
3796/// shuffle bellow:
3797/// vector_shuffle <4, 5, 6, 7, 12, 13, 14, 15>
3798/// The first half comes from the second half of V1 and the second half from the
3799/// the second half of V2.
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00003800static bool isVPERM2X128Mask(ArrayRef<int> Mask, EVT VT, bool HasFp256) {
3801 if (!HasFp256 || !VT.is256BitVector())
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003802 return false;
3803
3804 // The shuffle result is divided into half A and half B. In total the two
3805 // sources have 4 halves, namely: C, D, E, F. The final values of A and
3806 // B must come from C, D, E or F.
Craig Topperc612d792012-01-02 09:17:37 +00003807 unsigned HalfSize = VT.getVectorNumElements()/2;
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003808 bool MatchA = false, MatchB = false;
3809
3810 // Check if A comes from one of C, D, E, F.
Craig Topperc612d792012-01-02 09:17:37 +00003811 for (unsigned Half = 0; Half != 4; ++Half) {
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003812 if (isSequentialOrUndefInRange(Mask, 0, HalfSize, Half*HalfSize)) {
3813 MatchA = true;
3814 break;
3815 }
3816 }
3817
3818 // Check if B comes from one of C, D, E, F.
Craig Topperc612d792012-01-02 09:17:37 +00003819 for (unsigned Half = 0; Half != 4; ++Half) {
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003820 if (isSequentialOrUndefInRange(Mask, HalfSize, HalfSize, Half*HalfSize)) {
3821 MatchB = true;
3822 break;
3823 }
3824 }
3825
3826 return MatchA && MatchB;
3827}
3828
Craig Topper70b883b2011-11-28 10:14:51 +00003829/// getShuffleVPERM2X128Immediate - Return the appropriate immediate to shuffle
3830/// the specified VECTOR_MASK mask with VPERM2F128/VPERM2I128 instructions.
Craig Topperd93e4c32011-12-11 19:12:35 +00003831static unsigned getShuffleVPERM2X128Immediate(ShuffleVectorSDNode *SVOp) {
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003832 EVT VT = SVOp->getValueType(0);
3833
Craig Topperc612d792012-01-02 09:17:37 +00003834 unsigned HalfSize = VT.getVectorNumElements()/2;
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003835
Craig Topperc612d792012-01-02 09:17:37 +00003836 unsigned FstHalf = 0, SndHalf = 0;
3837 for (unsigned i = 0; i < HalfSize; ++i) {
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003838 if (SVOp->getMaskElt(i) > 0) {
3839 FstHalf = SVOp->getMaskElt(i)/HalfSize;
3840 break;
3841 }
3842 }
Craig Topperc612d792012-01-02 09:17:37 +00003843 for (unsigned i = HalfSize; i < HalfSize*2; ++i) {
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003844 if (SVOp->getMaskElt(i) > 0) {
3845 SndHalf = SVOp->getMaskElt(i)/HalfSize;
3846 break;
3847 }
3848 }
3849
3850 return (FstHalf | (SndHalf << 4));
3851}
3852
Craig Topper70b883b2011-11-28 10:14:51 +00003853/// isVPERMILPMask - Return true if the specified VECTOR_SHUFFLE operand
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00003854/// specifies a shuffle of elements that is suitable for input to VPERMILPD*.
3855/// Note that VPERMIL mask matching is different depending whether theunderlying
3856/// type is 32 or 64. In the VPERMILPS the high half of the mask should point
3857/// to the same elements of the low, but to the higher half of the source.
3858/// In VPERMILPD the two lanes could be shuffled independently of each other
Craig Topperdbd98a42012-02-07 06:28:42 +00003859/// with the same restriction that lanes can't be crossed. Also handles PSHUFDY.
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00003860static bool isVPERMILPMask(ArrayRef<int> Mask, EVT VT, bool HasFp256) {
3861 if (!HasFp256)
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00003862 return false;
3863
Craig Topperc612d792012-01-02 09:17:37 +00003864 unsigned NumElts = VT.getVectorNumElements();
Craig Topper70b883b2011-11-28 10:14:51 +00003865 // Only match 256-bit with 32/64-bit types
3866 if (VT.getSizeInBits() != 256 || (NumElts != 4 && NumElts != 8))
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00003867 return false;
3868
Craig Topperc612d792012-01-02 09:17:37 +00003869 unsigned NumLanes = VT.getSizeInBits()/128;
3870 unsigned LaneSize = NumElts/NumLanes;
Craig Topper1a7700a2012-01-19 08:19:12 +00003871 for (unsigned l = 0; l != NumElts; l += LaneSize) {
Craig Topperc612d792012-01-02 09:17:37 +00003872 for (unsigned i = 0; i != LaneSize; ++i) {
Craig Topper1a7700a2012-01-19 08:19:12 +00003873 if (!isUndefOrInRange(Mask[i+l], l, l+LaneSize))
Craig Topper70b883b2011-11-28 10:14:51 +00003874 return false;
Craig Topper1a7700a2012-01-19 08:19:12 +00003875 if (NumElts != 8 || l == 0)
Craig Topper70b883b2011-11-28 10:14:51 +00003876 continue;
3877 // VPERMILPS handling
3878 if (Mask[i] < 0)
3879 continue;
Craig Topper1a7700a2012-01-19 08:19:12 +00003880 if (!isUndefOrEqual(Mask[i+l], Mask[i]+l))
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00003881 return false;
3882 }
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00003883 }
3884
3885 return true;
3886}
3887
Craig Topper5aaffa82012-02-19 02:53:47 +00003888/// isCommutedMOVLMask - Returns true if the shuffle mask is except the reverse
Evan Cheng017dcc62006-04-21 01:05:10 +00003889/// of what x86 movss want. X86 movs requires the lowest element to be lowest
Evan Cheng39623da2006-04-20 08:58:49 +00003890/// element of vector 2 and the other elements to come from vector 1 in order.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003891static bool isCommutedMOVLMask(ArrayRef<int> Mask, EVT VT,
Nate Begeman9008ca62009-04-27 18:41:29 +00003892 bool V2IsSplat = false, bool V2IsUndef = false) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00003893 if (!VT.is128BitVector())
Craig Topper97327dc2012-03-18 22:50:10 +00003894 return false;
Craig Topper7a9a28b2012-08-12 02:23:29 +00003895
3896 unsigned NumOps = VT.getVectorNumElements();
Chris Lattner5a88b832007-02-25 07:10:00 +00003897 if (NumOps != 2 && NumOps != 4 && NumOps != 8 && NumOps != 16)
Evan Cheng39623da2006-04-20 08:58:49 +00003898 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003899
Nate Begeman9008ca62009-04-27 18:41:29 +00003900 if (!isUndefOrEqual(Mask[0], 0))
Evan Cheng39623da2006-04-20 08:58:49 +00003901 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003902
Craig Topperc612d792012-01-02 09:17:37 +00003903 for (unsigned i = 1; i != NumOps; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003904 if (!(isUndefOrEqual(Mask[i], i+NumOps) ||
3905 (V2IsUndef && isUndefOrInRange(Mask[i], NumOps, NumOps*2)) ||
3906 (V2IsSplat && isUndefOrEqual(Mask[i], NumOps))))
Evan Cheng8cf723d2006-09-08 01:50:06 +00003907 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003908
Evan Cheng39623da2006-04-20 08:58:49 +00003909 return true;
3910}
3911
Evan Chengd9539472006-04-14 21:59:03 +00003912/// isMOVSHDUPMask - Return true if the specified VECTOR_SHUFFLE operand
3913/// specifies a shuffle of elements that is suitable for input to MOVSHDUP.
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003914/// Masks to match: <1, 1, 3, 3> or <1, 1, 3, 3, 5, 5, 7, 7>
Craig Topperdd637ae2012-02-19 05:41:45 +00003915static bool isMOVSHDUPMask(ArrayRef<int> Mask, EVT VT,
Craig Topper5aaffa82012-02-19 02:53:47 +00003916 const X86Subtarget *Subtarget) {
Craig Topperd0a31172012-01-10 06:37:29 +00003917 if (!Subtarget->hasSSE3())
Evan Chengd9539472006-04-14 21:59:03 +00003918 return false;
3919
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003920 unsigned NumElems = VT.getVectorNumElements();
3921
3922 if ((VT.getSizeInBits() == 128 && NumElems != 4) ||
3923 (VT.getSizeInBits() == 256 && NumElems != 8))
3924 return false;
3925
3926 // "i+1" is the value the indexed mask element must have
Craig Topperdd637ae2012-02-19 05:41:45 +00003927 for (unsigned i = 0; i != NumElems; i += 2)
3928 if (!isUndefOrEqual(Mask[i], i+1) ||
3929 !isUndefOrEqual(Mask[i+1], i+1))
Nate Begeman9008ca62009-04-27 18:41:29 +00003930 return false;
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003931
3932 return true;
Evan Chengd9539472006-04-14 21:59:03 +00003933}
3934
3935/// isMOVSLDUPMask - Return true if the specified VECTOR_SHUFFLE operand
3936/// specifies a shuffle of elements that is suitable for input to MOVSLDUP.
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003937/// Masks to match: <0, 0, 2, 2> or <0, 0, 2, 2, 4, 4, 6, 6>
Craig Topperdd637ae2012-02-19 05:41:45 +00003938static bool isMOVSLDUPMask(ArrayRef<int> Mask, EVT VT,
Craig Topper5aaffa82012-02-19 02:53:47 +00003939 const X86Subtarget *Subtarget) {
Craig Topperd0a31172012-01-10 06:37:29 +00003940 if (!Subtarget->hasSSE3())
Evan Chengd9539472006-04-14 21:59:03 +00003941 return false;
3942
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003943 unsigned NumElems = VT.getVectorNumElements();
3944
3945 if ((VT.getSizeInBits() == 128 && NumElems != 4) ||
3946 (VT.getSizeInBits() == 256 && NumElems != 8))
3947 return false;
3948
3949 // "i" is the value the indexed mask element must have
Craig Topperc612d792012-01-02 09:17:37 +00003950 for (unsigned i = 0; i != NumElems; i += 2)
Craig Topperdd637ae2012-02-19 05:41:45 +00003951 if (!isUndefOrEqual(Mask[i], i) ||
3952 !isUndefOrEqual(Mask[i+1], i))
Nate Begeman9008ca62009-04-27 18:41:29 +00003953 return false;
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003954
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003955 return true;
Evan Chengd9539472006-04-14 21:59:03 +00003956}
3957
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00003958/// isMOVDDUPYMask - Return true if the specified VECTOR_SHUFFLE operand
3959/// specifies a shuffle of elements that is suitable for input to 256-bit
3960/// version of MOVDDUP.
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00003961static bool isMOVDDUPYMask(ArrayRef<int> Mask, EVT VT, bool HasFp256) {
3962 if (!HasFp256 || !VT.is256BitVector())
Craig Topper7a9a28b2012-08-12 02:23:29 +00003963 return false;
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00003964
Craig Topper7a9a28b2012-08-12 02:23:29 +00003965 unsigned NumElts = VT.getVectorNumElements();
3966 if (NumElts != 4)
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00003967 return false;
3968
Craig Topperc612d792012-01-02 09:17:37 +00003969 for (unsigned i = 0; i != NumElts/2; ++i)
Craig Topperbeabc6c2011-12-05 06:56:46 +00003970 if (!isUndefOrEqual(Mask[i], 0))
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00003971 return false;
Craig Topperc612d792012-01-02 09:17:37 +00003972 for (unsigned i = NumElts/2; i != NumElts; ++i)
Craig Topperbeabc6c2011-12-05 06:56:46 +00003973 if (!isUndefOrEqual(Mask[i], NumElts/2))
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00003974 return false;
3975 return true;
3976}
3977
Evan Cheng0b457f02008-09-25 20:50:48 +00003978/// isMOVDDUPMask - Return true if the specified VECTOR_SHUFFLE operand
Bruno Cardoso Lopes06ef9232011-08-25 21:40:34 +00003979/// specifies a shuffle of elements that is suitable for input to 128-bit
3980/// version of MOVDDUP.
Craig Topperdd637ae2012-02-19 05:41:45 +00003981static bool isMOVDDUPMask(ArrayRef<int> Mask, EVT VT) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00003982 if (!VT.is128BitVector())
Bruno Cardoso Lopes06ef9232011-08-25 21:40:34 +00003983 return false;
3984
Craig Topperc612d792012-01-02 09:17:37 +00003985 unsigned e = VT.getVectorNumElements() / 2;
3986 for (unsigned i = 0; i != e; ++i)
Craig Topperdd637ae2012-02-19 05:41:45 +00003987 if (!isUndefOrEqual(Mask[i], i))
Evan Cheng0b457f02008-09-25 20:50:48 +00003988 return false;
Craig Topperc612d792012-01-02 09:17:37 +00003989 for (unsigned i = 0; i != e; ++i)
Craig Topperdd637ae2012-02-19 05:41:45 +00003990 if (!isUndefOrEqual(Mask[e+i], i))
Evan Cheng0b457f02008-09-25 20:50:48 +00003991 return false;
3992 return true;
3993}
3994
David Greenec38a03e2011-02-03 15:50:00 +00003995/// isVEXTRACTF128Index - Return true if the specified
3996/// EXTRACT_SUBVECTOR operand specifies a vector extract that is
3997/// suitable for input to VEXTRACTF128.
3998bool X86::isVEXTRACTF128Index(SDNode *N) {
3999 if (!isa<ConstantSDNode>(N->getOperand(1).getNode()))
4000 return false;
4001
4002 // The index should be aligned on a 128-bit boundary.
4003 uint64_t Index =
4004 cast<ConstantSDNode>(N->getOperand(1).getNode())->getZExtValue();
4005
4006 unsigned VL = N->getValueType(0).getVectorNumElements();
4007 unsigned VBits = N->getValueType(0).getSizeInBits();
4008 unsigned ElSize = VBits / VL;
4009 bool Result = (Index * ElSize) % 128 == 0;
4010
4011 return Result;
4012}
4013
David Greeneccacdc12011-02-04 16:08:29 +00004014/// isVINSERTF128Index - Return true if the specified INSERT_SUBVECTOR
4015/// operand specifies a subvector insert that is suitable for input to
4016/// VINSERTF128.
4017bool X86::isVINSERTF128Index(SDNode *N) {
4018 if (!isa<ConstantSDNode>(N->getOperand(2).getNode()))
4019 return false;
4020
4021 // The index should be aligned on a 128-bit boundary.
4022 uint64_t Index =
4023 cast<ConstantSDNode>(N->getOperand(2).getNode())->getZExtValue();
4024
4025 unsigned VL = N->getValueType(0).getVectorNumElements();
4026 unsigned VBits = N->getValueType(0).getSizeInBits();
4027 unsigned ElSize = VBits / VL;
4028 bool Result = (Index * ElSize) % 128 == 0;
4029
4030 return Result;
4031}
4032
Evan Cheng63d33002006-03-22 08:01:21 +00004033/// getShuffleSHUFImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00004034/// the specified VECTOR_SHUFFLE mask with PSHUF* and SHUFP* instructions.
Craig Topper1a7700a2012-01-19 08:19:12 +00004035/// Handles 128-bit and 256-bit.
Craig Topper5aaffa82012-02-19 02:53:47 +00004036static unsigned getShuffleSHUFImmediate(ShuffleVectorSDNode *N) {
Craig Topper1a7700a2012-01-19 08:19:12 +00004037 EVT VT = N->getValueType(0);
Nate Begeman9008ca62009-04-27 18:41:29 +00004038
Craig Topper1a7700a2012-01-19 08:19:12 +00004039 assert((VT.is128BitVector() || VT.is256BitVector()) &&
4040 "Unsupported vector type for PSHUF/SHUFP");
4041
4042 // Handle 128 and 256-bit vector lengths. AVX defines PSHUF/SHUFP to operate
4043 // independently on 128-bit lanes.
4044 unsigned NumElts = VT.getVectorNumElements();
4045 unsigned NumLanes = VT.getSizeInBits()/128;
4046 unsigned NumLaneElts = NumElts/NumLanes;
4047
4048 assert((NumLaneElts == 2 || NumLaneElts == 4) &&
4049 "Only supports 2 or 4 elements per lane");
4050
4051 unsigned Shift = (NumLaneElts == 4) ? 1 : 0;
Evan Chengb9df0ca2006-03-22 02:53:00 +00004052 unsigned Mask = 0;
Craig Topper1a7700a2012-01-19 08:19:12 +00004053 for (unsigned i = 0; i != NumElts; ++i) {
4054 int Elt = N->getMaskElt(i);
4055 if (Elt < 0) continue;
Craig Topper6b28d352012-05-03 07:12:59 +00004056 Elt &= NumLaneElts - 1;
4057 unsigned ShAmt = (i << Shift) % 8;
Craig Topper1a7700a2012-01-19 08:19:12 +00004058 Mask |= Elt << ShAmt;
Evan Cheng36b27f32006-03-28 23:41:33 +00004059 }
Craig Topper1a7700a2012-01-19 08:19:12 +00004060
Evan Cheng63d33002006-03-22 08:01:21 +00004061 return Mask;
4062}
4063
Evan Cheng506d3df2006-03-29 23:07:14 +00004064/// getShufflePSHUFHWImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00004065/// the specified VECTOR_SHUFFLE mask with the PSHUFHW instruction.
Craig Topperdd637ae2012-02-19 05:41:45 +00004066static unsigned getShufflePSHUFHWImmediate(ShuffleVectorSDNode *N) {
Craig Topper6b28d352012-05-03 07:12:59 +00004067 EVT VT = N->getValueType(0);
4068
4069 assert((VT == MVT::v8i16 || VT == MVT::v16i16) &&
4070 "Unsupported vector type for PSHUFHW");
4071
4072 unsigned NumElts = VT.getVectorNumElements();
4073
Evan Cheng506d3df2006-03-29 23:07:14 +00004074 unsigned Mask = 0;
Craig Topper6b28d352012-05-03 07:12:59 +00004075 for (unsigned l = 0; l != NumElts; l += 8) {
4076 // 8 nodes per lane, but we only care about the last 4.
4077 for (unsigned i = 0; i < 4; ++i) {
4078 int Elt = N->getMaskElt(l+i+4);
4079 if (Elt < 0) continue;
4080 Elt &= 0x3; // only 2-bits.
4081 Mask |= Elt << (i * 2);
4082 }
Evan Cheng506d3df2006-03-29 23:07:14 +00004083 }
Craig Topper6b28d352012-05-03 07:12:59 +00004084
Evan Cheng506d3df2006-03-29 23:07:14 +00004085 return Mask;
4086}
4087
4088/// getShufflePSHUFLWImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00004089/// the specified VECTOR_SHUFFLE mask with the PSHUFLW instruction.
Craig Topperdd637ae2012-02-19 05:41:45 +00004090static unsigned getShufflePSHUFLWImmediate(ShuffleVectorSDNode *N) {
Craig Topper6b28d352012-05-03 07:12:59 +00004091 EVT VT = N->getValueType(0);
4092
4093 assert((VT == MVT::v8i16 || VT == MVT::v16i16) &&
4094 "Unsupported vector type for PSHUFHW");
4095
4096 unsigned NumElts = VT.getVectorNumElements();
4097
Evan Cheng506d3df2006-03-29 23:07:14 +00004098 unsigned Mask = 0;
Craig Topper6b28d352012-05-03 07:12:59 +00004099 for (unsigned l = 0; l != NumElts; l += 8) {
4100 // 8 nodes per lane, but we only care about the first 4.
4101 for (unsigned i = 0; i < 4; ++i) {
4102 int Elt = N->getMaskElt(l+i);
4103 if (Elt < 0) continue;
4104 Elt &= 0x3; // only 2-bits
4105 Mask |= Elt << (i * 2);
4106 }
Evan Cheng506d3df2006-03-29 23:07:14 +00004107 }
Craig Topper6b28d352012-05-03 07:12:59 +00004108
Evan Cheng506d3df2006-03-29 23:07:14 +00004109 return Mask;
4110}
4111
Nate Begemana09008b2009-10-19 02:17:23 +00004112/// getShufflePALIGNRImmediate - Return the appropriate immediate to shuffle
4113/// the specified VECTOR_SHUFFLE mask with the PALIGNR instruction.
Craig Topperd93e4c32011-12-11 19:12:35 +00004114static unsigned getShufflePALIGNRImmediate(ShuffleVectorSDNode *SVOp) {
4115 EVT VT = SVOp->getValueType(0);
4116 unsigned EltSize = VT.getVectorElementType().getSizeInBits() >> 3;
Nate Begemana09008b2009-10-19 02:17:23 +00004117
Craig Topper0e2037b2012-01-20 05:53:00 +00004118 unsigned NumElts = VT.getVectorNumElements();
4119 unsigned NumLanes = VT.getSizeInBits()/128;
4120 unsigned NumLaneElts = NumElts/NumLanes;
4121
4122 int Val = 0;
4123 unsigned i;
4124 for (i = 0; i != NumElts; ++i) {
Nate Begemana09008b2009-10-19 02:17:23 +00004125 Val = SVOp->getMaskElt(i);
4126 if (Val >= 0)
4127 break;
4128 }
Craig Topper0e2037b2012-01-20 05:53:00 +00004129 if (Val >= (int)NumElts)
4130 Val -= NumElts - NumLaneElts;
4131
Eli Friedman63f8dde2011-07-25 21:36:45 +00004132 assert(Val - i > 0 && "PALIGNR imm should be positive");
Nate Begemana09008b2009-10-19 02:17:23 +00004133 return (Val - i) * EltSize;
4134}
4135
David Greenec38a03e2011-02-03 15:50:00 +00004136/// getExtractVEXTRACTF128Immediate - Return the appropriate immediate
4137/// to extract the specified EXTRACT_SUBVECTOR index with VEXTRACTF128
4138/// instructions.
4139unsigned X86::getExtractVEXTRACTF128Immediate(SDNode *N) {
4140 if (!isa<ConstantSDNode>(N->getOperand(1).getNode()))
4141 llvm_unreachable("Illegal extract subvector for VEXTRACTF128");
4142
4143 uint64_t Index =
4144 cast<ConstantSDNode>(N->getOperand(1).getNode())->getZExtValue();
4145
4146 EVT VecVT = N->getOperand(0).getValueType();
4147 EVT ElVT = VecVT.getVectorElementType();
4148
4149 unsigned NumElemsPerChunk = 128 / ElVT.getSizeInBits();
David Greenec38a03e2011-02-03 15:50:00 +00004150 return Index / NumElemsPerChunk;
4151}
4152
David Greeneccacdc12011-02-04 16:08:29 +00004153/// getInsertVINSERTF128Immediate - Return the appropriate immediate
4154/// to insert at the specified INSERT_SUBVECTOR index with VINSERTF128
4155/// instructions.
4156unsigned X86::getInsertVINSERTF128Immediate(SDNode *N) {
4157 if (!isa<ConstantSDNode>(N->getOperand(2).getNode()))
4158 llvm_unreachable("Illegal insert subvector for VINSERTF128");
4159
4160 uint64_t Index =
NAKAMURA Takumi27635382011-02-05 15:10:54 +00004161 cast<ConstantSDNode>(N->getOperand(2).getNode())->getZExtValue();
David Greeneccacdc12011-02-04 16:08:29 +00004162
4163 EVT VecVT = N->getValueType(0);
4164 EVT ElVT = VecVT.getVectorElementType();
4165
4166 unsigned NumElemsPerChunk = 128 / ElVT.getSizeInBits();
David Greeneccacdc12011-02-04 16:08:29 +00004167 return Index / NumElemsPerChunk;
4168}
4169
Elena Demikhovsky73c504a2012-04-15 11:18:59 +00004170/// getShuffleCLImmediate - Return the appropriate immediate to shuffle
4171/// the specified VECTOR_SHUFFLE mask with VPERMQ and VPERMPD instructions.
4172/// Handles 256-bit.
4173static unsigned getShuffleCLImmediate(ShuffleVectorSDNode *N) {
4174 EVT VT = N->getValueType(0);
4175
Elena Demikhovsky73c504a2012-04-15 11:18:59 +00004176 unsigned NumElts = VT.getVectorNumElements();
4177
Craig Topper095c5282012-04-15 23:48:57 +00004178 assert((VT.is256BitVector() && NumElts == 4) &&
4179 "Unsupported vector type for VPERMQ/VPERMPD");
4180
Elena Demikhovsky73c504a2012-04-15 11:18:59 +00004181 unsigned Mask = 0;
4182 for (unsigned i = 0; i != NumElts; ++i) {
4183 int Elt = N->getMaskElt(i);
Craig Topper095c5282012-04-15 23:48:57 +00004184 if (Elt < 0)
Elena Demikhovsky73c504a2012-04-15 11:18:59 +00004185 continue;
4186 Mask |= Elt << (i*2);
4187 }
4188
4189 return Mask;
4190}
Evan Cheng37b73872009-07-30 08:33:02 +00004191/// isZeroNode - Returns true if Elt is a constant zero or a floating point
4192/// constant +0.0.
4193bool X86::isZeroNode(SDValue Elt) {
4194 return ((isa<ConstantSDNode>(Elt) &&
Dan Gohmane368b462010-06-18 14:22:04 +00004195 cast<ConstantSDNode>(Elt)->isNullValue()) ||
Evan Cheng37b73872009-07-30 08:33:02 +00004196 (isa<ConstantFPSDNode>(Elt) &&
4197 cast<ConstantFPSDNode>(Elt)->getValueAPF().isPosZero()));
4198}
4199
Nate Begeman9008ca62009-04-27 18:41:29 +00004200/// CommuteVectorShuffle - Swap vector_shuffle operands as well as values in
4201/// their permute mask.
4202static SDValue CommuteVectorShuffle(ShuffleVectorSDNode *SVOp,
4203 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00004204 EVT VT = SVOp->getValueType(0);
Nate Begeman5a5ca152009-04-29 05:20:52 +00004205 unsigned NumElems = VT.getVectorNumElements();
Nate Begeman9008ca62009-04-27 18:41:29 +00004206 SmallVector<int, 8> MaskVec;
Eric Christopherfd179292009-08-27 18:07:15 +00004207
Nate Begeman5a5ca152009-04-29 05:20:52 +00004208 for (unsigned i = 0; i != NumElems; ++i) {
Craig Topper8ae97ba2012-05-21 06:40:16 +00004209 int Idx = SVOp->getMaskElt(i);
4210 if (Idx >= 0) {
4211 if (Idx < (int)NumElems)
4212 Idx += NumElems;
4213 else
4214 Idx -= NumElems;
4215 }
4216 MaskVec.push_back(Idx);
Evan Cheng5ced1d82006-04-06 23:23:56 +00004217 }
Nate Begeman9008ca62009-04-27 18:41:29 +00004218 return DAG.getVectorShuffle(VT, SVOp->getDebugLoc(), SVOp->getOperand(1),
4219 SVOp->getOperand(0), &MaskVec[0]);
Evan Cheng5ced1d82006-04-06 23:23:56 +00004220}
4221
Evan Cheng533a0aa2006-04-19 20:35:22 +00004222/// ShouldXformToMOVHLPS - Return true if the node should be transformed to
4223/// match movhlps. The lower half elements should come from upper half of
4224/// V1 (and in order), and the upper half elements should come from the upper
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00004225/// half of V2 (and in order).
Craig Topperdd637ae2012-02-19 05:41:45 +00004226static bool ShouldXformToMOVHLPS(ArrayRef<int> Mask, EVT VT) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00004227 if (!VT.is128BitVector())
Bruno Cardoso Lopes59353b42011-08-11 18:59:13 +00004228 return false;
4229 if (VT.getVectorNumElements() != 4)
Evan Cheng533a0aa2006-04-19 20:35:22 +00004230 return false;
4231 for (unsigned i = 0, e = 2; i != e; ++i)
Craig Topperdd637ae2012-02-19 05:41:45 +00004232 if (!isUndefOrEqual(Mask[i], i+2))
Evan Cheng533a0aa2006-04-19 20:35:22 +00004233 return false;
4234 for (unsigned i = 2; i != 4; ++i)
Craig Topperdd637ae2012-02-19 05:41:45 +00004235 if (!isUndefOrEqual(Mask[i], i+4))
Evan Cheng533a0aa2006-04-19 20:35:22 +00004236 return false;
4237 return true;
4238}
4239
Evan Cheng5ced1d82006-04-06 23:23:56 +00004240/// isScalarLoadToVector - Returns true if the node is a scalar load that
Evan Cheng7e2ff772008-05-08 00:57:18 +00004241/// is promoted to a vector. It also returns the LoadSDNode by reference if
4242/// required.
4243static bool isScalarLoadToVector(SDNode *N, LoadSDNode **LD = NULL) {
Evan Cheng0b457f02008-09-25 20:50:48 +00004244 if (N->getOpcode() != ISD::SCALAR_TO_VECTOR)
4245 return false;
4246 N = N->getOperand(0).getNode();
4247 if (!ISD::isNON_EXTLoad(N))
4248 return false;
4249 if (LD)
4250 *LD = cast<LoadSDNode>(N);
4251 return true;
Evan Cheng5ced1d82006-04-06 23:23:56 +00004252}
4253
Dan Gohman65fd6562011-11-03 21:49:52 +00004254// Test whether the given value is a vector value which will be legalized
4255// into a load.
4256static bool WillBeConstantPoolLoad(SDNode *N) {
4257 if (N->getOpcode() != ISD::BUILD_VECTOR)
4258 return false;
4259
4260 // Check for any non-constant elements.
4261 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
4262 switch (N->getOperand(i).getNode()->getOpcode()) {
4263 case ISD::UNDEF:
4264 case ISD::ConstantFP:
4265 case ISD::Constant:
4266 break;
4267 default:
4268 return false;
4269 }
4270
4271 // Vectors of all-zeros and all-ones are materialized with special
4272 // instructions rather than being loaded.
4273 return !ISD::isBuildVectorAllZeros(N) &&
4274 !ISD::isBuildVectorAllOnes(N);
4275}
4276
Evan Cheng533a0aa2006-04-19 20:35:22 +00004277/// ShouldXformToMOVLP{S|D} - Return true if the node should be transformed to
4278/// match movlp{s|d}. The lower half elements should come from lower half of
4279/// V1 (and in order), and the upper half elements should come from the upper
4280/// half of V2 (and in order). And since V1 will become the source of the
4281/// MOVLP, it must be either a vector load or a scalar load to vector.
Nate Begeman9008ca62009-04-27 18:41:29 +00004282static bool ShouldXformToMOVLP(SDNode *V1, SDNode *V2,
Craig Topperdd637ae2012-02-19 05:41:45 +00004283 ArrayRef<int> Mask, EVT VT) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00004284 if (!VT.is128BitVector())
Bruno Cardoso Lopes59353b42011-08-11 18:59:13 +00004285 return false;
4286
Evan Cheng466685d2006-10-09 20:57:25 +00004287 if (!ISD::isNON_EXTLoad(V1) && !isScalarLoadToVector(V1))
Evan Cheng533a0aa2006-04-19 20:35:22 +00004288 return false;
Evan Cheng23425f52006-10-09 21:39:25 +00004289 // Is V2 is a vector load, don't do this transformation. We will try to use
4290 // load folding shufps op.
Dan Gohman65fd6562011-11-03 21:49:52 +00004291 if (ISD::isNON_EXTLoad(V2) || WillBeConstantPoolLoad(V2))
Evan Cheng23425f52006-10-09 21:39:25 +00004292 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00004293
Bruno Cardoso Lopes59353b42011-08-11 18:59:13 +00004294 unsigned NumElems = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00004295
Evan Cheng533a0aa2006-04-19 20:35:22 +00004296 if (NumElems != 2 && NumElems != 4)
4297 return false;
Nate Begeman5a5ca152009-04-29 05:20:52 +00004298 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
Craig Topperdd637ae2012-02-19 05:41:45 +00004299 if (!isUndefOrEqual(Mask[i], i))
Evan Cheng533a0aa2006-04-19 20:35:22 +00004300 return false;
Chad Rosier238ae312012-04-30 17:47:15 +00004301 for (unsigned i = NumElems/2, e = NumElems; i != e; ++i)
Craig Topperdd637ae2012-02-19 05:41:45 +00004302 if (!isUndefOrEqual(Mask[i], i+NumElems))
Evan Cheng533a0aa2006-04-19 20:35:22 +00004303 return false;
4304 return true;
Evan Cheng5ced1d82006-04-06 23:23:56 +00004305}
4306
Evan Cheng39623da2006-04-20 08:58:49 +00004307/// isSplatVector - Returns true if N is a BUILD_VECTOR node whose elements are
4308/// all the same.
4309static bool isSplatVector(SDNode *N) {
4310 if (N->getOpcode() != ISD::BUILD_VECTOR)
4311 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00004312
Dan Gohman475871a2008-07-27 21:46:04 +00004313 SDValue SplatValue = N->getOperand(0);
Evan Cheng39623da2006-04-20 08:58:49 +00004314 for (unsigned i = 1, e = N->getNumOperands(); i != e; ++i)
4315 if (N->getOperand(i) != SplatValue)
Evan Cheng5ced1d82006-04-06 23:23:56 +00004316 return false;
4317 return true;
4318}
4319
Evan Cheng213d2cf2007-05-17 18:45:50 +00004320/// isZeroShuffle - Returns true if N is a VECTOR_SHUFFLE that can be resolved
Eric Christopherfd179292009-08-27 18:07:15 +00004321/// to an zero vector.
Nate Begeman5a5ca152009-04-29 05:20:52 +00004322/// FIXME: move to dag combiner / method on ShuffleVectorSDNode
Nate Begeman9008ca62009-04-27 18:41:29 +00004323static bool isZeroShuffle(ShuffleVectorSDNode *N) {
Dan Gohman475871a2008-07-27 21:46:04 +00004324 SDValue V1 = N->getOperand(0);
4325 SDValue V2 = N->getOperand(1);
Nate Begeman5a5ca152009-04-29 05:20:52 +00004326 unsigned NumElems = N->getValueType(0).getVectorNumElements();
4327 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004328 int Idx = N->getMaskElt(i);
Nate Begeman5a5ca152009-04-29 05:20:52 +00004329 if (Idx >= (int)NumElems) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004330 unsigned Opc = V2.getOpcode();
Rafael Espindola15684b22009-04-24 12:40:33 +00004331 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V2.getNode()))
4332 continue;
Evan Cheng37b73872009-07-30 08:33:02 +00004333 if (Opc != ISD::BUILD_VECTOR ||
4334 !X86::isZeroNode(V2.getOperand(Idx-NumElems)))
Nate Begeman9008ca62009-04-27 18:41:29 +00004335 return false;
4336 } else if (Idx >= 0) {
4337 unsigned Opc = V1.getOpcode();
4338 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V1.getNode()))
4339 continue;
Evan Cheng37b73872009-07-30 08:33:02 +00004340 if (Opc != ISD::BUILD_VECTOR ||
4341 !X86::isZeroNode(V1.getOperand(Idx)))
Chris Lattner8a594482007-11-25 00:24:49 +00004342 return false;
Evan Cheng213d2cf2007-05-17 18:45:50 +00004343 }
4344 }
4345 return true;
4346}
4347
4348/// getZeroVector - Returns a vector of specified type with all zero elements.
4349///
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00004350static SDValue getZeroVector(EVT VT, const X86Subtarget *Subtarget,
Craig Topper12216172012-01-13 08:12:35 +00004351 SelectionDAG &DAG, DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004352 assert(VT.isVector() && "Expected a vector type");
Craig Topper9d352402012-04-23 07:24:41 +00004353 unsigned Size = VT.getSizeInBits();
Scott Michelfdc40a02009-02-17 22:15:04 +00004354
Dale Johannesen0488fb62010-09-30 23:57:10 +00004355 // Always build SSE zero vectors as <4 x i32> bitcasted
Bruno Cardoso Lopes8c05a852010-08-12 02:06:36 +00004356 // to their dest type. This ensures they get CSE'd.
Dan Gohman475871a2008-07-27 21:46:04 +00004357 SDValue Vec;
Craig Topper9d352402012-04-23 07:24:41 +00004358 if (Size == 128) { // SSE
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00004359 if (Subtarget->hasSSE2()) { // SSE2
Bruno Cardoso Lopes8c05a852010-08-12 02:06:36 +00004360 SDValue Cst = DAG.getTargetConstant(0, MVT::i32);
4361 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
4362 } else { // SSE1
4363 SDValue Cst = DAG.getTargetConstantFP(+0.0, MVT::f32);
4364 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4f32, Cst, Cst, Cst, Cst);
4365 }
Craig Topper9d352402012-04-23 07:24:41 +00004366 } else if (Size == 256) { // AVX
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00004367 if (Subtarget->hasInt256()) { // AVX2
Craig Topper12216172012-01-13 08:12:35 +00004368 SDValue Cst = DAG.getTargetConstant(0, MVT::i32);
4369 SDValue Ops[] = { Cst, Cst, Cst, Cst, Cst, Cst, Cst, Cst };
4370 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v8i32, Ops, 8);
4371 } else {
4372 // 256-bit logic and arithmetic instructions in AVX are all
4373 // floating-point, no support for integer ops. Emit fp zeroed vectors.
4374 SDValue Cst = DAG.getTargetConstantFP(+0.0, MVT::f32);
4375 SDValue Ops[] = { Cst, Cst, Cst, Cst, Cst, Cst, Cst, Cst };
4376 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v8f32, Ops, 8);
4377 }
Craig Topper9d352402012-04-23 07:24:41 +00004378 } else
4379 llvm_unreachable("Unexpected vector type");
4380
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004381 return DAG.getNode(ISD::BITCAST, dl, VT, Vec);
Evan Cheng213d2cf2007-05-17 18:45:50 +00004382}
4383
Chris Lattner8a594482007-11-25 00:24:49 +00004384/// getOnesVector - Returns a vector of specified type with all bits set.
Craig Topper745a86b2011-11-19 22:34:59 +00004385/// Always build ones vectors as <4 x i32> or <8 x i32>. For 256-bit types with
4386/// no AVX2 supprt, use two <4 x i32> inserted in a <8 x i32> appropriately.
4387/// Then bitcast to their original type, ensuring they get CSE'd.
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00004388static SDValue getOnesVector(EVT VT, bool HasInt256, SelectionDAG &DAG,
Craig Topper745a86b2011-11-19 22:34:59 +00004389 DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004390 assert(VT.isVector() && "Expected a vector type");
Craig Topper9d352402012-04-23 07:24:41 +00004391 unsigned Size = VT.getSizeInBits();
Scott Michelfdc40a02009-02-17 22:15:04 +00004392
Owen Anderson825b72b2009-08-11 20:47:22 +00004393 SDValue Cst = DAG.getTargetConstant(~0U, MVT::i32);
Craig Topper745a86b2011-11-19 22:34:59 +00004394 SDValue Vec;
Craig Topper9d352402012-04-23 07:24:41 +00004395 if (Size == 256) {
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00004396 if (HasInt256) { // AVX2
Craig Topper745a86b2011-11-19 22:34:59 +00004397 SDValue Ops[] = { Cst, Cst, Cst, Cst, Cst, Cst, Cst, Cst };
4398 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v8i32, Ops, 8);
4399 } else { // AVX
4400 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
Craig Topper4c7972d2012-04-22 18:15:59 +00004401 Vec = Concat128BitVectors(Vec, Vec, MVT::v8i32, 8, DAG, dl);
Craig Topper745a86b2011-11-19 22:34:59 +00004402 }
Craig Topper9d352402012-04-23 07:24:41 +00004403 } else if (Size == 128) {
Craig Topper745a86b2011-11-19 22:34:59 +00004404 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
Craig Topper9d352402012-04-23 07:24:41 +00004405 } else
4406 llvm_unreachable("Unexpected vector type");
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +00004407
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004408 return DAG.getNode(ISD::BITCAST, dl, VT, Vec);
Chris Lattner8a594482007-11-25 00:24:49 +00004409}
4410
Evan Cheng39623da2006-04-20 08:58:49 +00004411/// NormalizeMask - V2 is a splat, modify the mask (if needed) so all elements
4412/// that point to V2 points to its first element.
Craig Topper39a9e482012-02-11 06:24:48 +00004413static void NormalizeMask(SmallVectorImpl<int> &Mask, unsigned NumElems) {
Nate Begeman5a5ca152009-04-29 05:20:52 +00004414 for (unsigned i = 0; i != NumElems; ++i) {
Craig Topper39a9e482012-02-11 06:24:48 +00004415 if (Mask[i] > (int)NumElems) {
4416 Mask[i] = NumElems;
Evan Cheng39623da2006-04-20 08:58:49 +00004417 }
Evan Cheng39623da2006-04-20 08:58:49 +00004418 }
Evan Cheng39623da2006-04-20 08:58:49 +00004419}
4420
Evan Cheng017dcc62006-04-21 01:05:10 +00004421/// getMOVLMask - Returns a vector_shuffle mask for an movs{s|d}, movd
4422/// operation of specified width.
Owen Andersone50ed302009-08-10 22:56:29 +00004423static SDValue getMOVL(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00004424 SDValue V2) {
4425 unsigned NumElems = VT.getVectorNumElements();
4426 SmallVector<int, 8> Mask;
4427 Mask.push_back(NumElems);
Evan Cheng39623da2006-04-20 08:58:49 +00004428 for (unsigned i = 1; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004429 Mask.push_back(i);
4430 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Evan Cheng39623da2006-04-20 08:58:49 +00004431}
4432
Nate Begeman9008ca62009-04-27 18:41:29 +00004433/// getUnpackl - Returns a vector_shuffle node for an unpackl operation.
Owen Andersone50ed302009-08-10 22:56:29 +00004434static SDValue getUnpackl(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00004435 SDValue V2) {
4436 unsigned NumElems = VT.getVectorNumElements();
4437 SmallVector<int, 8> Mask;
Evan Chengc575ca22006-04-17 20:43:08 +00004438 for (unsigned i = 0, e = NumElems/2; i != e; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004439 Mask.push_back(i);
4440 Mask.push_back(i + NumElems);
Evan Chengc575ca22006-04-17 20:43:08 +00004441 }
Nate Begeman9008ca62009-04-27 18:41:29 +00004442 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Evan Chengc575ca22006-04-17 20:43:08 +00004443}
4444
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004445/// getUnpackh - Returns a vector_shuffle node for an unpackh operation.
Owen Andersone50ed302009-08-10 22:56:29 +00004446static SDValue getUnpackh(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00004447 SDValue V2) {
4448 unsigned NumElems = VT.getVectorNumElements();
Nate Begeman9008ca62009-04-27 18:41:29 +00004449 SmallVector<int, 8> Mask;
Chad Rosier238ae312012-04-30 17:47:15 +00004450 for (unsigned i = 0, Half = NumElems/2; i != Half; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004451 Mask.push_back(i + Half);
4452 Mask.push_back(i + NumElems + Half);
Evan Cheng39623da2006-04-20 08:58:49 +00004453 }
Nate Begeman9008ca62009-04-27 18:41:29 +00004454 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Chris Lattner62098042008-03-09 01:05:04 +00004455}
4456
Bruno Cardoso Lopes5f1d8ab2011-08-11 02:49:44 +00004457// PromoteSplati8i16 - All i16 and i8 vector types can't be used directly by
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004458// a generic shuffle instruction because the target has no such instructions.
4459// Generate shuffles which repeat i16 and i8 several times until they can be
4460// represented by v4f32 and then be manipulated by target suported shuffles.
Bruno Cardoso Lopes5f1d8ab2011-08-11 02:49:44 +00004461static SDValue PromoteSplati8i16(SDValue V, SelectionDAG &DAG, int &EltNo) {
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004462 EVT VT = V.getValueType();
Nate Begeman9008ca62009-04-27 18:41:29 +00004463 int NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004464 DebugLoc dl = V.getDebugLoc();
Rafael Espindola15684b22009-04-24 12:40:33 +00004465
Nate Begeman9008ca62009-04-27 18:41:29 +00004466 while (NumElems > 4) {
4467 if (EltNo < NumElems/2) {
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004468 V = getUnpackl(DAG, dl, VT, V, V);
Nate Begeman9008ca62009-04-27 18:41:29 +00004469 } else {
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004470 V = getUnpackh(DAG, dl, VT, V, V);
Nate Begeman9008ca62009-04-27 18:41:29 +00004471 EltNo -= NumElems/2;
4472 }
4473 NumElems >>= 1;
4474 }
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004475 return V;
4476}
Eric Christopherfd179292009-08-27 18:07:15 +00004477
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004478/// getLegalSplat - Generate a legal splat with supported x86 shuffles
4479static SDValue getLegalSplat(SelectionDAG &DAG, SDValue V, int EltNo) {
4480 EVT VT = V.getValueType();
4481 DebugLoc dl = V.getDebugLoc();
Craig Topper9d352402012-04-23 07:24:41 +00004482 unsigned Size = VT.getSizeInBits();
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004483
Craig Topper9d352402012-04-23 07:24:41 +00004484 if (Size == 128) {
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004485 V = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, V);
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004486 int SplatMask[4] = { EltNo, EltNo, EltNo, EltNo };
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004487 V = DAG.getVectorShuffle(MVT::v4f32, dl, V, DAG.getUNDEF(MVT::v4f32),
4488 &SplatMask[0]);
Craig Topper9d352402012-04-23 07:24:41 +00004489 } else if (Size == 256) {
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004490 // To use VPERMILPS to splat scalars, the second half of indicies must
4491 // refer to the higher part, which is a duplication of the lower one,
4492 // because VPERMILPS can only handle in-lane permutations.
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004493 int SplatMask[8] = { EltNo, EltNo, EltNo, EltNo,
4494 EltNo+4, EltNo+4, EltNo+4, EltNo+4 };
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004495
4496 V = DAG.getNode(ISD::BITCAST, dl, MVT::v8f32, V);
4497 V = DAG.getVectorShuffle(MVT::v8f32, dl, V, DAG.getUNDEF(MVT::v8f32),
4498 &SplatMask[0]);
Craig Topper9d352402012-04-23 07:24:41 +00004499 } else
4500 llvm_unreachable("Vector size not supported");
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004501
4502 return DAG.getNode(ISD::BITCAST, dl, VT, V);
4503}
4504
Bruno Cardoso Lopes8a5b2622011-08-17 02:29:13 +00004505/// PromoteSplat - Splat is promoted to target supported vector shuffles.
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004506static SDValue PromoteSplat(ShuffleVectorSDNode *SV, SelectionDAG &DAG) {
4507 EVT SrcVT = SV->getValueType(0);
4508 SDValue V1 = SV->getOperand(0);
4509 DebugLoc dl = SV->getDebugLoc();
4510
4511 int EltNo = SV->getSplatIndex();
4512 int NumElems = SrcVT.getVectorNumElements();
4513 unsigned Size = SrcVT.getSizeInBits();
4514
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004515 assert(((Size == 128 && NumElems > 4) || Size == 256) &&
4516 "Unknown how to promote splat for type");
4517
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004518 // Extract the 128-bit part containing the splat element and update
4519 // the splat element index when it refers to the higher register.
4520 if (Size == 256) {
Craig Topper7d1e3dc2012-04-30 05:17:10 +00004521 V1 = Extract128BitVector(V1, EltNo, DAG, dl);
4522 if (EltNo >= NumElems/2)
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004523 EltNo -= NumElems/2;
4524 }
4525
Bruno Cardoso Lopes8a5b2622011-08-17 02:29:13 +00004526 // All i16 and i8 vector types can't be used directly by a generic shuffle
4527 // instruction because the target has no such instruction. Generate shuffles
4528 // which repeat i16 and i8 several times until they fit in i32, and then can
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004529 // be manipulated by target suported shuffles.
Bruno Cardoso Lopes5f1d8ab2011-08-11 02:49:44 +00004530 EVT EltVT = SrcVT.getVectorElementType();
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004531 if (EltVT == MVT::i8 || EltVT == MVT::i16)
Bruno Cardoso Lopes5f1d8ab2011-08-11 02:49:44 +00004532 V1 = PromoteSplati8i16(V1, DAG, EltNo);
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004533
4534 // Recreate the 256-bit vector and place the same 128-bit vector
4535 // into the low and high part. This is necessary because we want
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004536 // to use VPERM* to shuffle the vectors
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004537 if (Size == 256) {
Craig Topper4c7972d2012-04-22 18:15:59 +00004538 V1 = DAG.getNode(ISD::CONCAT_VECTORS, dl, SrcVT, V1, V1);
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004539 }
4540
4541 return getLegalSplat(DAG, V1, EltNo);
Evan Chengc575ca22006-04-17 20:43:08 +00004542}
4543
Evan Chengba05f722006-04-21 23:03:30 +00004544/// getShuffleVectorZeroOrUndef - Return a vector_shuffle of the specified
Chris Lattner8a594482007-11-25 00:24:49 +00004545/// vector of zero or undef vector. This produces a shuffle where the low
4546/// element of V2 is swizzled into the zero/undef vector, landing at element
4547/// 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 +00004548static SDValue getShuffleVectorZeroOrUndef(SDValue V2, unsigned Idx,
Craig Topper12216172012-01-13 08:12:35 +00004549 bool IsZero,
4550 const X86Subtarget *Subtarget,
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00004551 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00004552 EVT VT = V2.getValueType();
Craig Topper12216172012-01-13 08:12:35 +00004553 SDValue V1 = IsZero
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00004554 ? getZeroVector(VT, Subtarget, DAG, V2.getDebugLoc()) : DAG.getUNDEF(VT);
Nate Begeman9008ca62009-04-27 18:41:29 +00004555 unsigned NumElems = VT.getVectorNumElements();
4556 SmallVector<int, 16> MaskVec;
Chris Lattner8a594482007-11-25 00:24:49 +00004557 for (unsigned i = 0; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004558 // If this is the insertion idx, put the low elt of V2 here.
4559 MaskVec.push_back(i == Idx ? NumElems : i);
4560 return DAG.getVectorShuffle(VT, V2.getDebugLoc(), V1, V2, &MaskVec[0]);
Evan Cheng017dcc62006-04-21 01:05:10 +00004561}
4562
Craig Toppera1ffc682012-03-20 06:42:26 +00004563/// getTargetShuffleMask - Calculates the shuffle mask corresponding to the
4564/// target specific opcode. Returns true if the Mask could be calculated.
Craig Topper89f4e662012-03-20 07:17:59 +00004565/// Sets IsUnary to true if only uses one source.
Craig Topperd978c542012-05-06 19:46:21 +00004566static bool getTargetShuffleMask(SDNode *N, MVT VT,
Craig Topper89f4e662012-03-20 07:17:59 +00004567 SmallVectorImpl<int> &Mask, bool &IsUnary) {
Craig Toppera1ffc682012-03-20 06:42:26 +00004568 unsigned NumElems = VT.getVectorNumElements();
4569 SDValue ImmN;
4570
Craig Topper89f4e662012-03-20 07:17:59 +00004571 IsUnary = false;
Craig Toppera1ffc682012-03-20 06:42:26 +00004572 switch(N->getOpcode()) {
4573 case X86ISD::SHUFP:
4574 ImmN = N->getOperand(N->getNumOperands()-1);
4575 DecodeSHUFPMask(VT, cast<ConstantSDNode>(ImmN)->getZExtValue(), Mask);
4576 break;
4577 case X86ISD::UNPCKH:
4578 DecodeUNPCKHMask(VT, Mask);
4579 break;
4580 case X86ISD::UNPCKL:
4581 DecodeUNPCKLMask(VT, Mask);
4582 break;
4583 case X86ISD::MOVHLPS:
4584 DecodeMOVHLPSMask(NumElems, Mask);
4585 break;
4586 case X86ISD::MOVLHPS:
4587 DecodeMOVLHPSMask(NumElems, Mask);
4588 break;
4589 case X86ISD::PSHUFD:
4590 case X86ISD::VPERMILP:
4591 ImmN = N->getOperand(N->getNumOperands()-1);
4592 DecodePSHUFMask(VT, cast<ConstantSDNode>(ImmN)->getZExtValue(), Mask);
Craig Topper89f4e662012-03-20 07:17:59 +00004593 IsUnary = true;
Craig Toppera1ffc682012-03-20 06:42:26 +00004594 break;
4595 case X86ISD::PSHUFHW:
4596 ImmN = N->getOperand(N->getNumOperands()-1);
Craig Toppera9a568a2012-05-02 08:03:44 +00004597 DecodePSHUFHWMask(VT, cast<ConstantSDNode>(ImmN)->getZExtValue(), Mask);
Craig Topper89f4e662012-03-20 07:17:59 +00004598 IsUnary = true;
Craig Toppera1ffc682012-03-20 06:42:26 +00004599 break;
4600 case X86ISD::PSHUFLW:
4601 ImmN = N->getOperand(N->getNumOperands()-1);
Craig Toppera9a568a2012-05-02 08:03:44 +00004602 DecodePSHUFLWMask(VT, cast<ConstantSDNode>(ImmN)->getZExtValue(), Mask);
Craig Topper89f4e662012-03-20 07:17:59 +00004603 IsUnary = true;
Craig Toppera1ffc682012-03-20 06:42:26 +00004604 break;
Craig Topperbdcbcb32012-05-06 18:54:26 +00004605 case X86ISD::VPERMI:
4606 ImmN = N->getOperand(N->getNumOperands()-1);
4607 DecodeVPERMMask(cast<ConstantSDNode>(ImmN)->getZExtValue(), Mask);
4608 IsUnary = true;
4609 break;
Craig Toppera1ffc682012-03-20 06:42:26 +00004610 case X86ISD::MOVSS:
4611 case X86ISD::MOVSD: {
4612 // The index 0 always comes from the first element of the second source,
4613 // this is why MOVSS and MOVSD are used in the first place. The other
4614 // elements come from the other positions of the first source vector
4615 Mask.push_back(NumElems);
4616 for (unsigned i = 1; i != NumElems; ++i) {
4617 Mask.push_back(i);
4618 }
4619 break;
4620 }
4621 case X86ISD::VPERM2X128:
4622 ImmN = N->getOperand(N->getNumOperands()-1);
4623 DecodeVPERM2X128Mask(VT, cast<ConstantSDNode>(ImmN)->getZExtValue(), Mask);
Craig Topper2091df32012-04-17 05:54:54 +00004624 if (Mask.empty()) return false;
Craig Toppera1ffc682012-03-20 06:42:26 +00004625 break;
4626 case X86ISD::MOVDDUP:
4627 case X86ISD::MOVLHPD:
4628 case X86ISD::MOVLPD:
4629 case X86ISD::MOVLPS:
4630 case X86ISD::MOVSHDUP:
4631 case X86ISD::MOVSLDUP:
4632 case X86ISD::PALIGN:
4633 // Not yet implemented
4634 return false;
4635 default: llvm_unreachable("unknown target shuffle node");
4636 }
4637
4638 return true;
4639}
4640
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004641/// getShuffleScalarElt - Returns the scalar element that will make up the ith
4642/// element of the result of the vector shuffle.
Craig Topper3d092db2012-03-21 02:14:01 +00004643static SDValue getShuffleScalarElt(SDNode *N, unsigned Index, SelectionDAG &DAG,
Benjamin Kramer050db522011-03-26 12:38:19 +00004644 unsigned Depth) {
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004645 if (Depth == 6)
4646 return SDValue(); // Limit search depth.
4647
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004648 SDValue V = SDValue(N, 0);
4649 EVT VT = V.getValueType();
4650 unsigned Opcode = V.getOpcode();
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004651
4652 // Recurse into ISD::VECTOR_SHUFFLE node to find scalars.
4653 if (const ShuffleVectorSDNode *SV = dyn_cast<ShuffleVectorSDNode>(N)) {
Craig Topper3d092db2012-03-21 02:14:01 +00004654 int Elt = SV->getMaskElt(Index);
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004655
Craig Topper3d092db2012-03-21 02:14:01 +00004656 if (Elt < 0)
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004657 return DAG.getUNDEF(VT.getVectorElementType());
4658
Craig Topperd156dc12012-02-06 07:17:51 +00004659 unsigned NumElems = VT.getVectorNumElements();
Craig Topper3d092db2012-03-21 02:14:01 +00004660 SDValue NewV = (Elt < (int)NumElems) ? SV->getOperand(0)
4661 : SV->getOperand(1);
4662 return getShuffleScalarElt(NewV.getNode(), Elt % NumElems, DAG, Depth+1);
Evan Chengf26ffe92008-05-29 08:22:04 +00004663 }
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004664
4665 // Recurse into target specific vector shuffles to find scalars.
4666 if (isTargetShuffle(Opcode)) {
Craig Topperd978c542012-05-06 19:46:21 +00004667 MVT ShufVT = V.getValueType().getSimpleVT();
4668 unsigned NumElems = ShufVT.getVectorNumElements();
Craig Toppera1ffc682012-03-20 06:42:26 +00004669 SmallVector<int, 16> ShuffleMask;
Craig Topper89f4e662012-03-20 07:17:59 +00004670 bool IsUnary;
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004671
Craig Topperd978c542012-05-06 19:46:21 +00004672 if (!getTargetShuffleMask(N, ShufVT, ShuffleMask, IsUnary))
Craig Toppera1ffc682012-03-20 06:42:26 +00004673 return SDValue();
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004674
Craig Topper3d092db2012-03-21 02:14:01 +00004675 int Elt = ShuffleMask[Index];
4676 if (Elt < 0)
Craig Topperd978c542012-05-06 19:46:21 +00004677 return DAG.getUNDEF(ShufVT.getVectorElementType());
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004678
Craig Topper3d092db2012-03-21 02:14:01 +00004679 SDValue NewV = (Elt < (int)NumElems) ? N->getOperand(0)
Craig Topperd978c542012-05-06 19:46:21 +00004680 : N->getOperand(1);
Craig Topper3d092db2012-03-21 02:14:01 +00004681 return getShuffleScalarElt(NewV.getNode(), Elt % NumElems, DAG,
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004682 Depth+1);
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004683 }
4684
4685 // Actual nodes that may contain scalar elements
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004686 if (Opcode == ISD::BITCAST) {
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004687 V = V.getOperand(0);
4688 EVT SrcVT = V.getValueType();
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00004689 unsigned NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004690
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00004691 if (!SrcVT.isVector() || SrcVT.getVectorNumElements() != NumElems)
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004692 return SDValue();
4693 }
4694
4695 if (V.getOpcode() == ISD::SCALAR_TO_VECTOR)
4696 return (Index == 0) ? V.getOperand(0)
Craig Topper3d092db2012-03-21 02:14:01 +00004697 : DAG.getUNDEF(VT.getVectorElementType());
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004698
4699 if (V.getOpcode() == ISD::BUILD_VECTOR)
4700 return V.getOperand(Index);
4701
4702 return SDValue();
4703}
4704
4705/// getNumOfConsecutiveZeros - Return the number of elements of a vector
4706/// shuffle operation which come from a consecutively from a zero. The
Chris Lattner7a2bdde2011-04-15 05:18:47 +00004707/// search can start in two different directions, from left or right.
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004708static
Craig Topper3d092db2012-03-21 02:14:01 +00004709unsigned getNumOfConsecutiveZeros(ShuffleVectorSDNode *SVOp, unsigned NumElems,
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004710 bool ZerosFromLeft, SelectionDAG &DAG) {
Craig Topper3d092db2012-03-21 02:14:01 +00004711 unsigned i;
4712 for (i = 0; i != NumElems; ++i) {
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004713 unsigned Index = ZerosFromLeft ? i : NumElems-i-1;
Craig Topper3d092db2012-03-21 02:14:01 +00004714 SDValue Elt = getShuffleScalarElt(SVOp, Index, DAG, 0);
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004715 if (!(Elt.getNode() &&
4716 (Elt.getOpcode() == ISD::UNDEF || X86::isZeroNode(Elt))))
4717 break;
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004718 }
4719
4720 return i;
4721}
4722
Craig Topper3d092db2012-03-21 02:14:01 +00004723/// isShuffleMaskConsecutive - Check if the shuffle mask indicies [MaskI, MaskE)
4724/// correspond consecutively to elements from one of the vector operands,
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004725/// starting from its index OpIdx. Also tell OpNum which source vector operand.
4726static
Craig Topper3d092db2012-03-21 02:14:01 +00004727bool isShuffleMaskConsecutive(ShuffleVectorSDNode *SVOp,
4728 unsigned MaskI, unsigned MaskE, unsigned OpIdx,
4729 unsigned NumElems, unsigned &OpNum) {
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004730 bool SeenV1 = false;
4731 bool SeenV2 = false;
4732
Craig Topper3d092db2012-03-21 02:14:01 +00004733 for (unsigned i = MaskI; i != MaskE; ++i, ++OpIdx) {
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004734 int Idx = SVOp->getMaskElt(i);
4735 // Ignore undef indicies
4736 if (Idx < 0)
4737 continue;
4738
Craig Topper3d092db2012-03-21 02:14:01 +00004739 if (Idx < (int)NumElems)
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004740 SeenV1 = true;
4741 else
4742 SeenV2 = true;
4743
4744 // Only accept consecutive elements from the same vector
4745 if ((Idx % NumElems != OpIdx) || (SeenV1 && SeenV2))
4746 return false;
4747 }
4748
4749 OpNum = SeenV1 ? 0 : 1;
4750 return true;
4751}
4752
4753/// isVectorShiftRight - Returns true if the shuffle can be implemented as a
4754/// logical left shift of a vector.
4755static bool isVectorShiftRight(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
4756 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
4757 unsigned NumElems = SVOp->getValueType(0).getVectorNumElements();
4758 unsigned NumZeros = getNumOfConsecutiveZeros(SVOp, NumElems,
4759 false /* check zeros from right */, DAG);
4760 unsigned OpSrc;
4761
4762 if (!NumZeros)
4763 return false;
4764
4765 // Considering the elements in the mask that are not consecutive zeros,
4766 // check if they consecutively come from only one of the source vectors.
4767 //
4768 // V1 = {X, A, B, C} 0
4769 // \ \ \ /
4770 // vector_shuffle V1, V2 <1, 2, 3, X>
4771 //
4772 if (!isShuffleMaskConsecutive(SVOp,
4773 0, // Mask Start Index
Craig Topper3d092db2012-03-21 02:14:01 +00004774 NumElems-NumZeros, // Mask End Index(exclusive)
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004775 NumZeros, // Where to start looking in the src vector
4776 NumElems, // Number of elements in vector
4777 OpSrc)) // Which source operand ?
4778 return false;
4779
4780 isLeft = false;
4781 ShAmt = NumZeros;
4782 ShVal = SVOp->getOperand(OpSrc);
4783 return true;
4784}
4785
4786/// isVectorShiftLeft - Returns true if the shuffle can be implemented as a
4787/// logical left shift of a vector.
4788static bool isVectorShiftLeft(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
4789 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
4790 unsigned NumElems = SVOp->getValueType(0).getVectorNumElements();
4791 unsigned NumZeros = getNumOfConsecutiveZeros(SVOp, NumElems,
4792 true /* check zeros from left */, DAG);
4793 unsigned OpSrc;
4794
4795 if (!NumZeros)
4796 return false;
4797
4798 // Considering the elements in the mask that are not consecutive zeros,
4799 // check if they consecutively come from only one of the source vectors.
4800 //
4801 // 0 { A, B, X, X } = V2
4802 // / \ / /
4803 // vector_shuffle V1, V2 <X, X, 4, 5>
4804 //
4805 if (!isShuffleMaskConsecutive(SVOp,
4806 NumZeros, // Mask Start Index
Craig Topper3d092db2012-03-21 02:14:01 +00004807 NumElems, // Mask End Index(exclusive)
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004808 0, // Where to start looking in the src vector
4809 NumElems, // Number of elements in vector
4810 OpSrc)) // Which source operand ?
4811 return false;
4812
4813 isLeft = true;
4814 ShAmt = NumZeros;
4815 ShVal = SVOp->getOperand(OpSrc);
4816 return true;
Evan Chengf26ffe92008-05-29 08:22:04 +00004817}
4818
4819/// isVectorShift - Returns true if the shuffle can be implemented as a
4820/// logical left or right shift of a vector.
Nate Begeman9008ca62009-04-27 18:41:29 +00004821static bool isVectorShift(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
Dan Gohman475871a2008-07-27 21:46:04 +00004822 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00004823 // Although the logic below support any bitwidth size, there are no
4824 // shift instructions which handle more than 128-bit vectors.
Craig Topper7a9a28b2012-08-12 02:23:29 +00004825 if (!SVOp->getValueType(0).is128BitVector())
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00004826 return false;
4827
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004828 if (isVectorShiftLeft(SVOp, DAG, isLeft, ShVal, ShAmt) ||
4829 isVectorShiftRight(SVOp, DAG, isLeft, ShVal, ShAmt))
4830 return true;
Evan Chengf26ffe92008-05-29 08:22:04 +00004831
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004832 return false;
Evan Chengf26ffe92008-05-29 08:22:04 +00004833}
4834
Evan Chengc78d3b42006-04-24 18:01:45 +00004835/// LowerBuildVectorv16i8 - Custom lower build_vector of v16i8.
4836///
Dan Gohman475871a2008-07-27 21:46:04 +00004837static SDValue LowerBuildVectorv16i8(SDValue Op, unsigned NonZeros,
Evan Chengc78d3b42006-04-24 18:01:45 +00004838 unsigned NumNonZero, unsigned NumZero,
Dan Gohmand858e902010-04-17 15:26:15 +00004839 SelectionDAG &DAG,
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00004840 const X86Subtarget* Subtarget,
Dan Gohmand858e902010-04-17 15:26:15 +00004841 const TargetLowering &TLI) {
Evan Chengc78d3b42006-04-24 18:01:45 +00004842 if (NumNonZero > 8)
Dan Gohman475871a2008-07-27 21:46:04 +00004843 return SDValue();
Evan Chengc78d3b42006-04-24 18:01:45 +00004844
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004845 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00004846 SDValue V(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00004847 bool First = true;
4848 for (unsigned i = 0; i < 16; ++i) {
4849 bool ThisIsNonZero = (NonZeros & (1 << i)) != 0;
4850 if (ThisIsNonZero && First) {
4851 if (NumZero)
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00004852 V = getZeroVector(MVT::v8i16, Subtarget, DAG, dl);
Evan Chengc78d3b42006-04-24 18:01:45 +00004853 else
Owen Anderson825b72b2009-08-11 20:47:22 +00004854 V = DAG.getUNDEF(MVT::v8i16);
Evan Chengc78d3b42006-04-24 18:01:45 +00004855 First = false;
4856 }
4857
4858 if ((i & 1) != 0) {
Dan Gohman475871a2008-07-27 21:46:04 +00004859 SDValue ThisElt(0, 0), LastElt(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00004860 bool LastIsNonZero = (NonZeros & (1 << (i-1))) != 0;
4861 if (LastIsNonZero) {
Scott Michelfdc40a02009-02-17 22:15:04 +00004862 LastElt = DAG.getNode(ISD::ZERO_EXTEND, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004863 MVT::i16, Op.getOperand(i-1));
Evan Chengc78d3b42006-04-24 18:01:45 +00004864 }
4865 if (ThisIsNonZero) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004866 ThisElt = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i16, Op.getOperand(i));
4867 ThisElt = DAG.getNode(ISD::SHL, dl, MVT::i16,
4868 ThisElt, DAG.getConstant(8, MVT::i8));
Evan Chengc78d3b42006-04-24 18:01:45 +00004869 if (LastIsNonZero)
Owen Anderson825b72b2009-08-11 20:47:22 +00004870 ThisElt = DAG.getNode(ISD::OR, dl, MVT::i16, ThisElt, LastElt);
Evan Chengc78d3b42006-04-24 18:01:45 +00004871 } else
4872 ThisElt = LastElt;
4873
Gabor Greifba36cb52008-08-28 21:40:38 +00004874 if (ThisElt.getNode())
Owen Anderson825b72b2009-08-11 20:47:22 +00004875 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, V, ThisElt,
Chris Lattner0bd48932008-01-17 07:00:52 +00004876 DAG.getIntPtrConstant(i/2));
Evan Chengc78d3b42006-04-24 18:01:45 +00004877 }
4878 }
4879
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004880 return DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, V);
Evan Chengc78d3b42006-04-24 18:01:45 +00004881}
4882
Bill Wendlinga348c562007-03-22 18:42:45 +00004883/// LowerBuildVectorv8i16 - Custom lower build_vector of v8i16.
Evan Chengc78d3b42006-04-24 18:01:45 +00004884///
Dan Gohman475871a2008-07-27 21:46:04 +00004885static SDValue LowerBuildVectorv8i16(SDValue Op, unsigned NonZeros,
Dan Gohmand858e902010-04-17 15:26:15 +00004886 unsigned NumNonZero, unsigned NumZero,
4887 SelectionDAG &DAG,
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00004888 const X86Subtarget* Subtarget,
Dan Gohmand858e902010-04-17 15:26:15 +00004889 const TargetLowering &TLI) {
Evan Chengc78d3b42006-04-24 18:01:45 +00004890 if (NumNonZero > 4)
Dan Gohman475871a2008-07-27 21:46:04 +00004891 return SDValue();
Evan Chengc78d3b42006-04-24 18:01:45 +00004892
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004893 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00004894 SDValue V(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00004895 bool First = true;
4896 for (unsigned i = 0; i < 8; ++i) {
4897 bool isNonZero = (NonZeros & (1 << i)) != 0;
4898 if (isNonZero) {
4899 if (First) {
4900 if (NumZero)
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00004901 V = getZeroVector(MVT::v8i16, Subtarget, DAG, dl);
Evan Chengc78d3b42006-04-24 18:01:45 +00004902 else
Owen Anderson825b72b2009-08-11 20:47:22 +00004903 V = DAG.getUNDEF(MVT::v8i16);
Evan Chengc78d3b42006-04-24 18:01:45 +00004904 First = false;
4905 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004906 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004907 MVT::v8i16, V, Op.getOperand(i),
Chris Lattner0bd48932008-01-17 07:00:52 +00004908 DAG.getIntPtrConstant(i));
Evan Chengc78d3b42006-04-24 18:01:45 +00004909 }
4910 }
4911
4912 return V;
4913}
4914
Evan Chengf26ffe92008-05-29 08:22:04 +00004915/// getVShift - Return a vector logical shift node.
4916///
Owen Andersone50ed302009-08-10 22:56:29 +00004917static SDValue getVShift(bool isLeft, EVT VT, SDValue SrcOp,
Nate Begeman9008ca62009-04-27 18:41:29 +00004918 unsigned NumBits, SelectionDAG &DAG,
4919 const TargetLowering &TLI, DebugLoc dl) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00004920 assert(VT.is128BitVector() && "Unknown type for VShift");
Dale Johannesen0488fb62010-09-30 23:57:10 +00004921 EVT ShVT = MVT::v2i64;
Craig Toppered2e13d2012-01-22 19:15:14 +00004922 unsigned Opc = isLeft ? X86ISD::VSHLDQ : X86ISD::VSRLDQ;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004923 SrcOp = DAG.getNode(ISD::BITCAST, dl, ShVT, SrcOp);
4924 return DAG.getNode(ISD::BITCAST, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00004925 DAG.getNode(Opc, dl, ShVT, SrcOp,
Owen Anderson95771af2011-02-25 21:41:48 +00004926 DAG.getConstant(NumBits,
4927 TLI.getShiftAmountTy(SrcOp.getValueType()))));
Evan Chengf26ffe92008-05-29 08:22:04 +00004928}
4929
Dan Gohman475871a2008-07-27 21:46:04 +00004930SDValue
Evan Chengc3630942009-12-09 21:00:30 +00004931X86TargetLowering::LowerAsSplatVectorLoad(SDValue SrcOp, EVT VT, DebugLoc dl,
Dan Gohmand858e902010-04-17 15:26:15 +00004932 SelectionDAG &DAG) const {
Michael J. Spencerec38de22010-10-10 22:04:20 +00004933
Evan Chengc3630942009-12-09 21:00:30 +00004934 // Check if the scalar load can be widened into a vector load. And if
4935 // the address is "base + cst" see if the cst can be "absorbed" into
4936 // the shuffle mask.
4937 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(SrcOp)) {
4938 SDValue Ptr = LD->getBasePtr();
4939 if (!ISD::isNormalLoad(LD) || LD->isVolatile())
4940 return SDValue();
4941 EVT PVT = LD->getValueType(0);
4942 if (PVT != MVT::i32 && PVT != MVT::f32)
4943 return SDValue();
4944
4945 int FI = -1;
4946 int64_t Offset = 0;
4947 if (FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr)) {
4948 FI = FINode->getIndex();
4949 Offset = 0;
Chris Lattner0a9481f2011-02-13 22:25:43 +00004950 } else if (DAG.isBaseWithConstantOffset(Ptr) &&
Evan Chengc3630942009-12-09 21:00:30 +00004951 isa<FrameIndexSDNode>(Ptr.getOperand(0))) {
4952 FI = cast<FrameIndexSDNode>(Ptr.getOperand(0))->getIndex();
4953 Offset = Ptr.getConstantOperandVal(1);
4954 Ptr = Ptr.getOperand(0);
4955 } else {
4956 return SDValue();
4957 }
4958
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004959 // FIXME: 256-bit vector instructions don't require a strict alignment,
4960 // improve this code to support it better.
4961 unsigned RequiredAlign = VT.getSizeInBits()/8;
Evan Chengc3630942009-12-09 21:00:30 +00004962 SDValue Chain = LD->getChain();
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004963 // Make sure the stack object alignment is at least 16 or 32.
Evan Chengc3630942009-12-09 21:00:30 +00004964 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004965 if (DAG.InferPtrAlignment(Ptr) < RequiredAlign) {
Evan Chengc3630942009-12-09 21:00:30 +00004966 if (MFI->isFixedObjectIndex(FI)) {
Eric Christophere9625cf2010-01-23 06:02:43 +00004967 // Can't change the alignment. FIXME: It's possible to compute
4968 // the exact stack offset and reference FI + adjust offset instead.
4969 // If someone *really* cares about this. That's the way to implement it.
4970 return SDValue();
Evan Chengc3630942009-12-09 21:00:30 +00004971 } else {
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004972 MFI->setObjectAlignment(FI, RequiredAlign);
Evan Chengc3630942009-12-09 21:00:30 +00004973 }
4974 }
4975
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004976 // (Offset % 16 or 32) must be multiple of 4. Then address is then
Evan Chengc3630942009-12-09 21:00:30 +00004977 // Ptr + (Offset & ~15).
4978 if (Offset < 0)
4979 return SDValue();
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004980 if ((Offset % RequiredAlign) & 3)
Evan Chengc3630942009-12-09 21:00:30 +00004981 return SDValue();
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004982 int64_t StartOffset = Offset & ~(RequiredAlign-1);
Evan Chengc3630942009-12-09 21:00:30 +00004983 if (StartOffset)
4984 Ptr = DAG.getNode(ISD::ADD, Ptr.getDebugLoc(), Ptr.getValueType(),
4985 Ptr,DAG.getConstant(StartOffset, Ptr.getValueType()));
4986
4987 int EltNo = (Offset - StartOffset) >> 2;
Craig Topper66ddd152012-04-27 22:54:43 +00004988 unsigned NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004989
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004990 EVT NVT = EVT::getVectorVT(*DAG.getContext(), PVT, NumElems);
4991 SDValue V1 = DAG.getLoad(NVT, dl, Chain, Ptr,
Chris Lattner51abfe42010-09-21 06:02:19 +00004992 LD->getPointerInfo().getWithOffset(StartOffset),
Pete Cooperd752e0f2011-11-08 18:42:53 +00004993 false, false, false, 0);
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004994
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004995 SmallVector<int, 8> Mask;
Craig Topper66ddd152012-04-27 22:54:43 +00004996 for (unsigned i = 0; i != NumElems; ++i)
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004997 Mask.push_back(EltNo);
4998
Craig Toppercc3000632012-01-30 07:50:31 +00004999 return DAG.getVectorShuffle(NVT, dl, V1, DAG.getUNDEF(NVT), &Mask[0]);
Evan Chengc3630942009-12-09 21:00:30 +00005000 }
5001
5002 return SDValue();
5003}
5004
Michael J. Spencerec38de22010-10-10 22:04:20 +00005005/// EltsFromConsecutiveLoads - Given the initializing elements 'Elts' of a
5006/// vector of type 'VT', see if the elements can be replaced by a single large
Nate Begeman1449f292010-03-24 22:19:06 +00005007/// load which has the same value as a build_vector whose operands are 'elts'.
5008///
5009/// Example: <load i32 *a, load i32 *a+4, undef, undef> -> zextload a
Michael J. Spencerec38de22010-10-10 22:04:20 +00005010///
Nate Begeman1449f292010-03-24 22:19:06 +00005011/// FIXME: we'd also like to handle the case where the last elements are zero
5012/// rather than undef via VZEXT_LOAD, but we do not detect that case today.
5013/// There's even a handy isZeroNode for that purpose.
Nate Begemanfdea31a2010-03-24 20:49:50 +00005014static SDValue EltsFromConsecutiveLoads(EVT VT, SmallVectorImpl<SDValue> &Elts,
Chris Lattner88641552010-09-22 00:34:38 +00005015 DebugLoc &DL, SelectionDAG &DAG) {
Nate Begemanfdea31a2010-03-24 20:49:50 +00005016 EVT EltVT = VT.getVectorElementType();
5017 unsigned NumElems = Elts.size();
Michael J. Spencerec38de22010-10-10 22:04:20 +00005018
Nate Begemanfdea31a2010-03-24 20:49:50 +00005019 LoadSDNode *LDBase = NULL;
5020 unsigned LastLoadedElt = -1U;
Michael J. Spencerec38de22010-10-10 22:04:20 +00005021
Nate Begeman1449f292010-03-24 22:19:06 +00005022 // For each element in the initializer, see if we've found a load or an undef.
Michael J. Spencerec38de22010-10-10 22:04:20 +00005023 // If we don't find an initial load element, or later load elements are
Nate Begeman1449f292010-03-24 22:19:06 +00005024 // non-consecutive, bail out.
Nate Begemanfdea31a2010-03-24 20:49:50 +00005025 for (unsigned i = 0; i < NumElems; ++i) {
5026 SDValue Elt = Elts[i];
Michael J. Spencerec38de22010-10-10 22:04:20 +00005027
Nate Begemanfdea31a2010-03-24 20:49:50 +00005028 if (!Elt.getNode() ||
5029 (Elt.getOpcode() != ISD::UNDEF && !ISD::isNON_EXTLoad(Elt.getNode())))
5030 return SDValue();
5031 if (!LDBase) {
5032 if (Elt.getNode()->getOpcode() == ISD::UNDEF)
5033 return SDValue();
5034 LDBase = cast<LoadSDNode>(Elt.getNode());
5035 LastLoadedElt = i;
5036 continue;
5037 }
5038 if (Elt.getOpcode() == ISD::UNDEF)
5039 continue;
5040
5041 LoadSDNode *LD = cast<LoadSDNode>(Elt);
5042 if (!DAG.isConsecutiveLoad(LD, LDBase, EltVT.getSizeInBits()/8, i))
5043 return SDValue();
5044 LastLoadedElt = i;
5045 }
Nate Begeman1449f292010-03-24 22:19:06 +00005046
5047 // If we have found an entire vector of loads and undefs, then return a large
5048 // load of the entire vector width starting at the base pointer. If we found
5049 // consecutive loads for the low half, generate a vzext_load node.
Nate Begemanfdea31a2010-03-24 20:49:50 +00005050 if (LastLoadedElt == NumElems - 1) {
5051 if (DAG.InferPtrAlignment(LDBase->getBasePtr()) >= 16)
Chris Lattner88641552010-09-22 00:34:38 +00005052 return DAG.getLoad(VT, DL, LDBase->getChain(), LDBase->getBasePtr(),
Chris Lattner51abfe42010-09-21 06:02:19 +00005053 LDBase->getPointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00005054 LDBase->isVolatile(), LDBase->isNonTemporal(),
5055 LDBase->isInvariant(), 0);
Chris Lattner88641552010-09-22 00:34:38 +00005056 return DAG.getLoad(VT, DL, LDBase->getChain(), LDBase->getBasePtr(),
Chris Lattner51abfe42010-09-21 06:02:19 +00005057 LDBase->getPointerInfo(),
Nate Begemanfdea31a2010-03-24 20:49:50 +00005058 LDBase->isVolatile(), LDBase->isNonTemporal(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00005059 LDBase->isInvariant(), LDBase->getAlignment());
Craig Topper69947b92012-04-23 06:57:04 +00005060 }
5061 if (NumElems == 4 && LastLoadedElt == 1 &&
5062 DAG.getTargetLoweringInfo().isTypeLegal(MVT::v2i64)) {
Nate Begemanfdea31a2010-03-24 20:49:50 +00005063 SDVTList Tys = DAG.getVTList(MVT::v2i64, MVT::Other);
5064 SDValue Ops[] = { LDBase->getChain(), LDBase->getBasePtr() };
Eli Friedman322ea082011-09-14 23:42:45 +00005065 SDValue ResNode =
5066 DAG.getMemIntrinsicNode(X86ISD::VZEXT_LOAD, DL, Tys, Ops, 2, MVT::i64,
5067 LDBase->getPointerInfo(),
5068 LDBase->getAlignment(),
5069 false/*isVolatile*/, true/*ReadMem*/,
5070 false/*WriteMem*/);
Manman Ren2b7a2e82012-08-31 23:16:57 +00005071
5072 // Make sure the newly-created LOAD is in the same position as LDBase in
5073 // terms of dependency. We create a TokenFactor for LDBase and ResNode, and
5074 // update uses of LDBase's output chain to use the TokenFactor.
5075 if (LDBase->hasAnyUseOfValue(1)) {
5076 SDValue NewChain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other,
5077 SDValue(LDBase, 1), SDValue(ResNode.getNode(), 1));
5078 DAG.ReplaceAllUsesOfValueWith(SDValue(LDBase, 1), NewChain);
5079 DAG.UpdateNodeOperands(NewChain.getNode(), SDValue(LDBase, 1),
5080 SDValue(ResNode.getNode(), 1));
5081 }
5082
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005083 return DAG.getNode(ISD::BITCAST, DL, VT, ResNode);
Nate Begemanfdea31a2010-03-24 20:49:50 +00005084 }
5085 return SDValue();
5086}
5087
Nadav Rotem9d68b062012-04-08 12:54:54 +00005088/// LowerVectorBroadcast - Attempt to use the vbroadcast instruction
5089/// to generate a splat value for the following cases:
5090/// 1. A splat BUILD_VECTOR which uses a single scalar load, or a constant.
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005091/// 2. A splat shuffle which uses a scalar_to_vector node which comes from
Nadav Rotem9d68b062012-04-08 12:54:54 +00005092/// a scalar load, or a constant.
5093/// The VBROADCAST node is returned when a pattern is found,
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00005094/// or SDValue() otherwise.
Nadav Rotem154819d2012-04-09 07:45:58 +00005095SDValue
Craig Topper55b24052012-09-11 06:15:32 +00005096X86TargetLowering::LowerVectorBroadcast(SDValue Op, SelectionDAG &DAG) const {
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00005097 if (!Subtarget->hasFp256())
Craig Toppera9376332012-01-10 08:23:59 +00005098 return SDValue();
5099
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005100 EVT VT = Op.getValueType();
Nadav Rotem154819d2012-04-09 07:45:58 +00005101 DebugLoc dl = Op.getDebugLoc();
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005102
Craig Topper5da8a802012-05-04 05:49:51 +00005103 assert((VT.is128BitVector() || VT.is256BitVector()) &&
5104 "Unsupported vector type for broadcast.");
5105
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005106 SDValue Ld;
Nadav Rotem9d68b062012-04-08 12:54:54 +00005107 bool ConstSplatVal;
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005108
Nadav Rotem9d68b062012-04-08 12:54:54 +00005109 switch (Op.getOpcode()) {
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005110 default:
5111 // Unknown pattern found.
5112 return SDValue();
5113
5114 case ISD::BUILD_VECTOR: {
5115 // The BUILD_VECTOR node must be a splat.
Nadav Rotem9d68b062012-04-08 12:54:54 +00005116 if (!isSplatVector(Op.getNode()))
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005117 return SDValue();
5118
Nadav Rotem9d68b062012-04-08 12:54:54 +00005119 Ld = Op.getOperand(0);
5120 ConstSplatVal = (Ld.getOpcode() == ISD::Constant ||
5121 Ld.getOpcode() == ISD::ConstantFP);
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00005122
5123 // The suspected load node has several users. Make sure that all
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005124 // of its users are from the BUILD_VECTOR node.
Nadav Rotem9d68b062012-04-08 12:54:54 +00005125 // Constants may have multiple users.
5126 if (!ConstSplatVal && !Ld->hasNUsesOfValue(VT.getVectorNumElements(), 0))
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005127 return SDValue();
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00005128 break;
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005129 }
5130
5131 case ISD::VECTOR_SHUFFLE: {
5132 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
5133
5134 // Shuffles must have a splat mask where the first element is
5135 // broadcasted.
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00005136 if ((!SVOp->isSplat()) || SVOp->getMaskElt(0) != 0)
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005137 return SDValue();
5138
5139 SDValue Sc = Op.getOperand(0);
Nadav Rotemb88e8dd2012-05-10 12:50:02 +00005140 if (Sc.getOpcode() != ISD::SCALAR_TO_VECTOR &&
Elena Demikhovsky8f40f7b2012-07-01 06:12:26 +00005141 Sc.getOpcode() != ISD::BUILD_VECTOR) {
5142
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00005143 if (!Subtarget->hasInt256())
Elena Demikhovsky8f40f7b2012-07-01 06:12:26 +00005144 return SDValue();
5145
5146 // Use the register form of the broadcast instruction available on AVX2.
5147 if (VT.is256BitVector())
5148 Sc = Extract128BitVector(Sc, 0, DAG, dl);
5149 return DAG.getNode(X86ISD::VBROADCAST, dl, VT, Sc);
5150 }
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005151
5152 Ld = Sc.getOperand(0);
Nadav Rotem9d68b062012-04-08 12:54:54 +00005153 ConstSplatVal = (Ld.getOpcode() == ISD::Constant ||
Nadav Rotem154819d2012-04-09 07:45:58 +00005154 Ld.getOpcode() == ISD::ConstantFP);
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005155
5156 // The scalar_to_vector node and the suspected
5157 // load node must have exactly one user.
Nadav Rotem9d68b062012-04-08 12:54:54 +00005158 // Constants may have multiple users.
5159 if (!ConstSplatVal && (!Sc.hasOneUse() || !Ld.hasOneUse()))
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005160 return SDValue();
5161 break;
5162 }
5163 }
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00005164
Craig Topper7a9a28b2012-08-12 02:23:29 +00005165 bool Is256 = VT.is256BitVector();
Nadav Rotem9d68b062012-04-08 12:54:54 +00005166
5167 // Handle the broadcasting a single constant scalar from the constant pool
5168 // into a vector. On Sandybridge it is still better to load a constant vector
5169 // from the constant pool and not to broadcast it from a scalar.
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00005170 if (ConstSplatVal && Subtarget->hasInt256()) {
Nadav Rotem9d68b062012-04-08 12:54:54 +00005171 EVT CVT = Ld.getValueType();
5172 assert(!CVT.isVector() && "Must not broadcast a vector type");
5173 unsigned ScalarSize = CVT.getSizeInBits();
5174
Craig Topper5da8a802012-05-04 05:49:51 +00005175 if (ScalarSize == 32 || (Is256 && ScalarSize == 64)) {
Nadav Rotem9d68b062012-04-08 12:54:54 +00005176 const Constant *C = 0;
5177 if (ConstantSDNode *CI = dyn_cast<ConstantSDNode>(Ld))
5178 C = CI->getConstantIntValue();
5179 else if (ConstantFPSDNode *CF = dyn_cast<ConstantFPSDNode>(Ld))
5180 C = CF->getConstantFPValue();
5181
5182 assert(C && "Invalid constant type");
5183
Nadav Rotem154819d2012-04-09 07:45:58 +00005184 SDValue CP = DAG.getConstantPool(C, getPointerTy());
Nadav Rotem9d68b062012-04-08 12:54:54 +00005185 unsigned Alignment = cast<ConstantPoolSDNode>(CP)->getAlignment();
Nadav Rotem154819d2012-04-09 07:45:58 +00005186 Ld = DAG.getLoad(CVT, dl, DAG.getEntryNode(), CP,
Craig Topper6643d9c2012-05-04 06:18:33 +00005187 MachinePointerInfo::getConstantPool(),
5188 false, false, false, Alignment);
Nadav Rotem9d68b062012-04-08 12:54:54 +00005189
Nadav Rotem9d68b062012-04-08 12:54:54 +00005190 return DAG.getNode(X86ISD::VBROADCAST, dl, VT, Ld);
5191 }
5192 }
5193
Nadav Rotem4fc8a5d2012-05-19 19:57:37 +00005194 bool IsLoad = ISD::isNormalLoad(Ld.getNode());
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005195 unsigned ScalarSize = Ld.getValueType().getSizeInBits();
5196
Nadav Rotem4fc8a5d2012-05-19 19:57:37 +00005197 // Handle AVX2 in-register broadcasts.
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00005198 if (!IsLoad && Subtarget->hasInt256() &&
Nadav Rotem4fc8a5d2012-05-19 19:57:37 +00005199 (ScalarSize == 32 || (Is256 && ScalarSize == 64)))
5200 return DAG.getNode(X86ISD::VBROADCAST, dl, VT, Ld);
5201
5202 // The scalar source must be a normal load.
5203 if (!IsLoad)
5204 return SDValue();
5205
Craig Topper5da8a802012-05-04 05:49:51 +00005206 if (ScalarSize == 32 || (Is256 && ScalarSize == 64))
Nadav Rotem9d68b062012-04-08 12:54:54 +00005207 return DAG.getNode(X86ISD::VBROADCAST, dl, VT, Ld);
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005208
Craig Toppera9376332012-01-10 08:23:59 +00005209 // The integer check is needed for the 64-bit into 128-bit so it doesn't match
Craig Topper5da8a802012-05-04 05:49:51 +00005210 // double since there is no vbroadcastsd xmm
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00005211 if (Subtarget->hasInt256() && Ld.getValueType().isInteger()) {
Craig Topper5da8a802012-05-04 05:49:51 +00005212 if (ScalarSize == 8 || ScalarSize == 16 || ScalarSize == 64)
Nadav Rotem9d68b062012-04-08 12:54:54 +00005213 return DAG.getNode(X86ISD::VBROADCAST, dl, VT, Ld);
Craig Toppera9376332012-01-10 08:23:59 +00005214 }
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00005215
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005216 // Unsupported broadcast.
5217 return SDValue();
5218}
5219
Evan Chengc3630942009-12-09 21:00:30 +00005220SDValue
Michael Liaofacace82012-10-19 17:15:18 +00005221X86TargetLowering::buildFromShuffleMostly(SDValue Op, SelectionDAG &DAG) const {
5222 EVT VT = Op.getValueType();
5223
5224 // Skip if insert_vec_elt is not supported.
5225 if (!isOperationLegalOrCustom(ISD::INSERT_VECTOR_ELT, VT))
5226 return SDValue();
5227
5228 DebugLoc DL = Op.getDebugLoc();
5229 unsigned NumElems = Op.getNumOperands();
5230
5231 SDValue VecIn1;
5232 SDValue VecIn2;
5233 SmallVector<unsigned, 4> InsertIndices;
5234 SmallVector<int, 8> Mask(NumElems, -1);
5235
5236 for (unsigned i = 0; i != NumElems; ++i) {
5237 unsigned Opc = Op.getOperand(i).getOpcode();
5238
5239 if (Opc == ISD::UNDEF)
5240 continue;
5241
5242 if (Opc != ISD::EXTRACT_VECTOR_ELT) {
5243 // Quit if more than 1 elements need inserting.
5244 if (InsertIndices.size() > 1)
5245 return SDValue();
5246
5247 InsertIndices.push_back(i);
5248 continue;
5249 }
5250
5251 SDValue ExtractedFromVec = Op.getOperand(i).getOperand(0);
5252 SDValue ExtIdx = Op.getOperand(i).getOperand(1);
5253
5254 // Quit if extracted from vector of different type.
5255 if (ExtractedFromVec.getValueType() != VT)
5256 return SDValue();
5257
5258 // Quit if non-constant index.
5259 if (!isa<ConstantSDNode>(ExtIdx))
5260 return SDValue();
5261
5262 if (VecIn1.getNode() == 0)
5263 VecIn1 = ExtractedFromVec;
5264 else if (VecIn1 != ExtractedFromVec) {
5265 if (VecIn2.getNode() == 0)
5266 VecIn2 = ExtractedFromVec;
5267 else if (VecIn2 != ExtractedFromVec)
5268 // Quit if more than 2 vectors to shuffle
5269 return SDValue();
5270 }
5271
5272 unsigned Idx = cast<ConstantSDNode>(ExtIdx)->getZExtValue();
5273
5274 if (ExtractedFromVec == VecIn1)
5275 Mask[i] = Idx;
5276 else if (ExtractedFromVec == VecIn2)
5277 Mask[i] = Idx + NumElems;
5278 }
5279
5280 if (VecIn1.getNode() == 0)
5281 return SDValue();
5282
5283 VecIn2 = VecIn2.getNode() ? VecIn2 : DAG.getUNDEF(VT);
5284 SDValue NV = DAG.getVectorShuffle(VT, DL, VecIn1, VecIn2, &Mask[0]);
5285 for (unsigned i = 0, e = InsertIndices.size(); i != e; ++i) {
5286 unsigned Idx = InsertIndices[i];
5287 NV = DAG.getNode(ISD::INSERT_VECTOR_ELT, DL, VT, NV, Op.getOperand(Idx),
5288 DAG.getIntPtrConstant(Idx));
5289 }
5290
5291 return NV;
5292}
5293
5294SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00005295X86TargetLowering::LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005296 DebugLoc dl = Op.getDebugLoc();
David Greenea5f26012011-02-07 19:36:54 +00005297
David Greenef125a292011-02-08 19:04:41 +00005298 EVT VT = Op.getValueType();
5299 EVT ExtVT = VT.getVectorElementType();
David Greenef125a292011-02-08 19:04:41 +00005300 unsigned NumElems = Op.getNumOperands();
5301
Bruno Cardoso Lopes531f19f2011-08-01 19:51:53 +00005302 // Vectors containing all zeros can be matched by pxor and xorps later
5303 if (ISD::isBuildVectorAllZeros(Op.getNode())) {
5304 // Canonicalize this to <4 x i32> to 1) ensure the zero vectors are CSE'd
5305 // and 2) ensure that i64 scalars are eliminated on x86-32 hosts.
Craig Topper07a27622012-01-22 03:07:48 +00005306 if (VT == MVT::v4i32 || VT == MVT::v8i32)
Chris Lattner8a594482007-11-25 00:24:49 +00005307 return Op;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005308
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00005309 return getZeroVector(VT, Subtarget, DAG, dl);
Chris Lattner8a594482007-11-25 00:24:49 +00005310 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00005311
Bruno Cardoso Lopes531f19f2011-08-01 19:51:53 +00005312 // Vectors containing all ones can be matched by pcmpeqd on 128-bit width
Craig Topper745a86b2011-11-19 22:34:59 +00005313 // vectors or broken into v4i32 operations on 256-bit vectors. AVX2 can use
5314 // vpcmpeqd on 256-bit vectors.
Bruno Cardoso Lopes531f19f2011-08-01 19:51:53 +00005315 if (ISD::isBuildVectorAllOnes(Op.getNode())) {
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00005316 if (VT == MVT::v4i32 || (VT == MVT::v8i32 && Subtarget->hasInt256()))
Bruno Cardoso Lopes531f19f2011-08-01 19:51:53 +00005317 return Op;
5318
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00005319 return getOnesVector(VT, Subtarget->hasInt256(), DAG, dl);
Bruno Cardoso Lopes531f19f2011-08-01 19:51:53 +00005320 }
5321
Nadav Rotem154819d2012-04-09 07:45:58 +00005322 SDValue Broadcast = LowerVectorBroadcast(Op, DAG);
Nadav Rotem9d68b062012-04-08 12:54:54 +00005323 if (Broadcast.getNode())
5324 return Broadcast;
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005325
Owen Andersone50ed302009-08-10 22:56:29 +00005326 unsigned EVTBits = ExtVT.getSizeInBits();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005327
Evan Cheng0db9fe62006-04-25 20:13:52 +00005328 unsigned NumZero = 0;
5329 unsigned NumNonZero = 0;
5330 unsigned NonZeros = 0;
Chris Lattnerc9517fb2008-03-08 22:48:29 +00005331 bool IsAllConstants = true;
Dan Gohman475871a2008-07-27 21:46:04 +00005332 SmallSet<SDValue, 8> Values;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005333 for (unsigned i = 0; i < NumElems; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00005334 SDValue Elt = Op.getOperand(i);
Evan Chengdb2d5242007-12-12 06:45:40 +00005335 if (Elt.getOpcode() == ISD::UNDEF)
5336 continue;
5337 Values.insert(Elt);
5338 if (Elt.getOpcode() != ISD::Constant &&
5339 Elt.getOpcode() != ISD::ConstantFP)
Chris Lattnerc9517fb2008-03-08 22:48:29 +00005340 IsAllConstants = false;
Evan Cheng37b73872009-07-30 08:33:02 +00005341 if (X86::isZeroNode(Elt))
Evan Chengdb2d5242007-12-12 06:45:40 +00005342 NumZero++;
5343 else {
5344 NonZeros |= (1 << i);
5345 NumNonZero++;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005346 }
5347 }
5348
Chris Lattner97a2a562010-08-26 05:24:29 +00005349 // All undef vector. Return an UNDEF. All zero vectors were handled above.
5350 if (NumNonZero == 0)
Dale Johannesene8d72302009-02-06 23:05:02 +00005351 return DAG.getUNDEF(VT);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005352
Chris Lattner67f453a2008-03-09 05:42:06 +00005353 // Special case for single non-zero, non-undef, element.
Eli Friedman10415532009-06-06 06:05:10 +00005354 if (NumNonZero == 1) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00005355 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dan Gohman475871a2008-07-27 21:46:04 +00005356 SDValue Item = Op.getOperand(Idx);
Scott Michelfdc40a02009-02-17 22:15:04 +00005357
Chris Lattner62098042008-03-09 01:05:04 +00005358 // If this is an insertion of an i64 value on x86-32, and if the top bits of
5359 // the value are obviously zero, truncate the value to i32 and do the
5360 // insertion that way. Only do this if the value is non-constant or if the
5361 // value is a constant being inserted into element 0. It is cheaper to do
5362 // a constant pool load than it is to do a movd + shuffle.
Owen Anderson825b72b2009-08-11 20:47:22 +00005363 if (ExtVT == MVT::i64 && !Subtarget->is64Bit() &&
Chris Lattner62098042008-03-09 01:05:04 +00005364 (!IsAllConstants || Idx == 0)) {
5365 if (DAG.MaskedValueIsZero(Item, APInt::getBitsSet(64, 32, 64))) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00005366 // Handle SSE only.
5367 assert(VT == MVT::v2i64 && "Expected an SSE value type!");
5368 EVT VecVT = MVT::v4i32;
5369 unsigned VecElts = 4;
Scott Michelfdc40a02009-02-17 22:15:04 +00005370
Chris Lattner62098042008-03-09 01:05:04 +00005371 // Truncate the value (which may itself be a constant) to i32, and
5372 // convert it to a vector with movd (S2V+shuffle to zero extend).
Owen Anderson825b72b2009-08-11 20:47:22 +00005373 Item = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Item);
Dale Johannesenace16102009-02-03 19:33:06 +00005374 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VecVT, Item);
Craig Topper12216172012-01-13 08:12:35 +00005375 Item = getShuffleVectorZeroOrUndef(Item, 0, true, Subtarget, DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +00005376
Chris Lattner62098042008-03-09 01:05:04 +00005377 // Now we have our 32-bit value zero extended in the low element of
5378 // a vector. If Idx != 0, swizzle it into place.
5379 if (Idx != 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005380 SmallVector<int, 4> Mask;
5381 Mask.push_back(Idx);
5382 for (unsigned i = 1; i != VecElts; ++i)
5383 Mask.push_back(i);
Craig Topperdf966f62012-04-22 19:17:57 +00005384 Item = DAG.getVectorShuffle(VecVT, dl, Item, DAG.getUNDEF(VecVT),
Nate Begeman9008ca62009-04-27 18:41:29 +00005385 &Mask[0]);
Chris Lattner62098042008-03-09 01:05:04 +00005386 }
Craig Topper07a27622012-01-22 03:07:48 +00005387 return DAG.getNode(ISD::BITCAST, dl, VT, Item);
Chris Lattner62098042008-03-09 01:05:04 +00005388 }
5389 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005390
Chris Lattner19f79692008-03-08 22:59:52 +00005391 // If we have a constant or non-constant insertion into the low element of
5392 // a vector, we can do this with SCALAR_TO_VECTOR + shuffle of zero into
5393 // the rest of the elements. This will be matched as movd/movq/movss/movsd
Eli Friedman10415532009-06-06 06:05:10 +00005394 // depending on what the source datatype is.
5395 if (Idx == 0) {
Craig Topperd62c16e2011-12-29 03:20:51 +00005396 if (NumZero == 0)
Eli Friedman10415532009-06-06 06:05:10 +00005397 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
Craig Topperd62c16e2011-12-29 03:20:51 +00005398
5399 if (ExtVT == MVT::i32 || ExtVT == MVT::f32 || ExtVT == MVT::f64 ||
Owen Anderson825b72b2009-08-11 20:47:22 +00005400 (ExtVT == MVT::i64 && Subtarget->is64Bit())) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00005401 if (VT.is256BitVector()) {
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00005402 SDValue ZeroVec = getZeroVector(VT, Subtarget, DAG, dl);
Nadav Rotem394a1f52012-01-11 14:07:51 +00005403 return DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, ZeroVec,
5404 Item, DAG.getIntPtrConstant(0));
Elena Demikhovsky021c0a22011-12-28 08:14:01 +00005405 }
Craig Topper7a9a28b2012-08-12 02:23:29 +00005406 assert(VT.is128BitVector() && "Expected an SSE value type!");
Eli Friedman10415532009-06-06 06:05:10 +00005407 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
5408 // Turn it into a MOVL (i.e. movss, movsd, or movd) to a zero vector.
Craig Topper12216172012-01-13 08:12:35 +00005409 return getShuffleVectorZeroOrUndef(Item, 0, true, Subtarget, DAG);
Craig Topperd62c16e2011-12-29 03:20:51 +00005410 }
5411
5412 if (ExtVT == MVT::i16 || ExtVT == MVT::i8) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005413 Item = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, Item);
Craig Topper3224e6b2011-12-29 03:09:33 +00005414 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32, Item);
Craig Topper7a9a28b2012-08-12 02:23:29 +00005415 if (VT.is256BitVector()) {
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00005416 SDValue ZeroVec = getZeroVector(MVT::v8i32, Subtarget, DAG, dl);
Craig Topperb14940a2012-04-22 20:55:18 +00005417 Item = Insert128BitVector(ZeroVec, Item, 0, DAG, dl);
Craig Topper19ec2a92011-12-29 03:34:54 +00005418 } else {
Craig Topper7a9a28b2012-08-12 02:23:29 +00005419 assert(VT.is128BitVector() && "Expected an SSE value type!");
Craig Topper12216172012-01-13 08:12:35 +00005420 Item = getShuffleVectorZeroOrUndef(Item, 0, true, Subtarget, DAG);
Craig Topper19ec2a92011-12-29 03:34:54 +00005421 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005422 return DAG.getNode(ISD::BITCAST, dl, VT, Item);
Eli Friedman10415532009-06-06 06:05:10 +00005423 }
Chris Lattnerc9517fb2008-03-08 22:48:29 +00005424 }
Evan Chengf26ffe92008-05-29 08:22:04 +00005425
5426 // Is it a vector logical left shift?
5427 if (NumElems == 2 && Idx == 1 &&
Evan Cheng37b73872009-07-30 08:33:02 +00005428 X86::isZeroNode(Op.getOperand(0)) &&
5429 !X86::isZeroNode(Op.getOperand(1))) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00005430 unsigned NumBits = VT.getSizeInBits();
Evan Chengf26ffe92008-05-29 08:22:04 +00005431 return getVShift(true, VT,
Scott Michelfdc40a02009-02-17 22:15:04 +00005432 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Dale Johannesenb300d2a2009-02-07 00:55:49 +00005433 VT, Op.getOperand(1)),
Dale Johannesenace16102009-02-03 19:33:06 +00005434 NumBits/2, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00005435 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005436
Chris Lattnerc9517fb2008-03-08 22:48:29 +00005437 if (IsAllConstants) // Otherwise, it's better to do a constpool load.
Dan Gohman475871a2008-07-27 21:46:04 +00005438 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005439
Chris Lattner19f79692008-03-08 22:59:52 +00005440 // Otherwise, if this is a vector with i32 or f32 elements, and the element
5441 // is a non-constant being inserted into an element other than the low one,
5442 // we can't use a constant pool load. Instead, use SCALAR_TO_VECTOR (aka
5443 // movd/movss) to move this into the low element, then shuffle it into
5444 // place.
Evan Cheng0db9fe62006-04-25 20:13:52 +00005445 if (EVTBits == 32) {
Dale Johannesenace16102009-02-03 19:33:06 +00005446 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
Scott Michelfdc40a02009-02-17 22:15:04 +00005447
Evan Cheng0db9fe62006-04-25 20:13:52 +00005448 // Turn it into a shuffle of zero and zero-extended scalar to vector.
Craig Topper12216172012-01-13 08:12:35 +00005449 Item = getShuffleVectorZeroOrUndef(Item, 0, NumZero > 0, Subtarget, DAG);
Nate Begeman9008ca62009-04-27 18:41:29 +00005450 SmallVector<int, 8> MaskVec;
Craig Topper31a207a2012-05-04 06:39:13 +00005451 for (unsigned i = 0; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00005452 MaskVec.push_back(i == Idx ? 0 : 1);
5453 return DAG.getVectorShuffle(VT, dl, Item, DAG.getUNDEF(VT), &MaskVec[0]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005454 }
5455 }
5456
Chris Lattner67f453a2008-03-09 05:42:06 +00005457 // Splat is obviously ok. Let legalizer expand it to a shuffle.
Evan Chengc3630942009-12-09 21:00:30 +00005458 if (Values.size() == 1) {
5459 if (EVTBits == 32) {
5460 // Instead of a shuffle like this:
5461 // shuffle (scalar_to_vector (load (ptr + 4))), undef, <0, 0, 0, 0>
5462 // Check if it's possible to issue this instead.
5463 // shuffle (vload ptr)), undef, <1, 1, 1, 1>
5464 unsigned Idx = CountTrailingZeros_32(NonZeros);
5465 SDValue Item = Op.getOperand(Idx);
5466 if (Op.getNode()->isOnlyUserOf(Item.getNode()))
5467 return LowerAsSplatVectorLoad(Item, VT, dl, DAG);
5468 }
Dan Gohman475871a2008-07-27 21:46:04 +00005469 return SDValue();
Evan Chengc3630942009-12-09 21:00:30 +00005470 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005471
Dan Gohmana3941172007-07-24 22:55:08 +00005472 // A vector full of immediates; various special cases are already
5473 // handled, so this is best done with a single constant-pool load.
Chris Lattnerc9517fb2008-03-08 22:48:29 +00005474 if (IsAllConstants)
Dan Gohman475871a2008-07-27 21:46:04 +00005475 return SDValue();
Dan Gohmana3941172007-07-24 22:55:08 +00005476
Bruno Cardoso Lopes6683efb2011-07-22 00:15:07 +00005477 // For AVX-length vectors, build the individual 128-bit pieces and use
5478 // shuffles to put them in place.
Craig Topper7a9a28b2012-08-12 02:23:29 +00005479 if (VT.is256BitVector()) {
Bruno Cardoso Lopes6683efb2011-07-22 00:15:07 +00005480 SmallVector<SDValue, 32> V;
Craig Topperfa5b70e2012-02-03 06:32:21 +00005481 for (unsigned i = 0; i != NumElems; ++i)
Bruno Cardoso Lopes6683efb2011-07-22 00:15:07 +00005482 V.push_back(Op.getOperand(i));
5483
5484 EVT HVT = EVT::getVectorVT(*DAG.getContext(), ExtVT, NumElems/2);
5485
5486 // Build both the lower and upper subvector.
5487 SDValue Lower = DAG.getNode(ISD::BUILD_VECTOR, dl, HVT, &V[0], NumElems/2);
5488 SDValue Upper = DAG.getNode(ISD::BUILD_VECTOR, dl, HVT, &V[NumElems / 2],
5489 NumElems/2);
5490
5491 // Recreate the wider vector with the lower and upper part.
Craig Topper4c7972d2012-04-22 18:15:59 +00005492 return Concat128BitVectors(Lower, Upper, VT, NumElems, DAG, dl);
Bruno Cardoso Lopes6683efb2011-07-22 00:15:07 +00005493 }
5494
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00005495 // Let legalizer expand 2-wide build_vectors.
Evan Cheng7e2ff772008-05-08 00:57:18 +00005496 if (EVTBits == 64) {
5497 if (NumNonZero == 1) {
5498 // One half is zero or undef.
5499 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dale Johannesenace16102009-02-03 19:33:06 +00005500 SDValue V2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT,
Evan Cheng7e2ff772008-05-08 00:57:18 +00005501 Op.getOperand(Idx));
Craig Topper12216172012-01-13 08:12:35 +00005502 return getShuffleVectorZeroOrUndef(V2, Idx, true, Subtarget, DAG);
Evan Cheng7e2ff772008-05-08 00:57:18 +00005503 }
Dan Gohman475871a2008-07-27 21:46:04 +00005504 return SDValue();
Evan Cheng7e2ff772008-05-08 00:57:18 +00005505 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00005506
5507 // If element VT is < 32 bits, convert it to inserts into a zero vector.
Bill Wendling826f36f2007-03-28 00:57:11 +00005508 if (EVTBits == 8 && NumElems == 16) {
Dan Gohman475871a2008-07-27 21:46:04 +00005509 SDValue V = LowerBuildVectorv16i8(Op, NonZeros,NumNonZero,NumZero, DAG,
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00005510 Subtarget, *this);
Gabor Greifba36cb52008-08-28 21:40:38 +00005511 if (V.getNode()) return V;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005512 }
5513
Bill Wendling826f36f2007-03-28 00:57:11 +00005514 if (EVTBits == 16 && NumElems == 8) {
Dan Gohman475871a2008-07-27 21:46:04 +00005515 SDValue V = LowerBuildVectorv8i16(Op, NonZeros,NumNonZero,NumZero, DAG,
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00005516 Subtarget, *this);
Gabor Greifba36cb52008-08-28 21:40:38 +00005517 if (V.getNode()) return V;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005518 }
5519
5520 // If element VT is == 32 bits, turn it into a number of shuffles.
Benjamin Kramer9c683542012-01-30 15:16:21 +00005521 SmallVector<SDValue, 8> V(NumElems);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005522 if (NumElems == 4 && NumZero > 0) {
5523 for (unsigned i = 0; i < 4; ++i) {
5524 bool isZero = !(NonZeros & (1 << i));
5525 if (isZero)
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00005526 V[i] = getZeroVector(VT, Subtarget, DAG, dl);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005527 else
Dale Johannesenace16102009-02-03 19:33:06 +00005528 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(i));
Evan Cheng0db9fe62006-04-25 20:13:52 +00005529 }
5530
5531 for (unsigned i = 0; i < 2; ++i) {
5532 switch ((NonZeros & (0x3 << i*2)) >> (i*2)) {
5533 default: break;
5534 case 0:
5535 V[i] = V[i*2]; // Must be a zero vector.
5536 break;
5537 case 1:
Nate Begeman9008ca62009-04-27 18:41:29 +00005538 V[i] = getMOVL(DAG, dl, VT, V[i*2+1], V[i*2]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005539 break;
5540 case 2:
Nate Begeman9008ca62009-04-27 18:41:29 +00005541 V[i] = getMOVL(DAG, dl, VT, V[i*2], V[i*2+1]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005542 break;
5543 case 3:
Nate Begeman9008ca62009-04-27 18:41:29 +00005544 V[i] = getUnpackl(DAG, dl, VT, V[i*2], V[i*2+1]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005545 break;
5546 }
5547 }
5548
Benjamin Kramer9c683542012-01-30 15:16:21 +00005549 bool Reverse1 = (NonZeros & 0x3) == 2;
5550 bool Reverse2 = ((NonZeros & (0x3 << 2)) >> 2) == 2;
5551 int MaskVec[] = {
5552 Reverse1 ? 1 : 0,
5553 Reverse1 ? 0 : 1,
Benjamin Kramer630ecf02012-01-30 20:01:35 +00005554 static_cast<int>(Reverse2 ? NumElems+1 : NumElems),
5555 static_cast<int>(Reverse2 ? NumElems : NumElems+1)
Benjamin Kramer9c683542012-01-30 15:16:21 +00005556 };
Nate Begeman9008ca62009-04-27 18:41:29 +00005557 return DAG.getVectorShuffle(VT, dl, V[0], V[1], &MaskVec[0]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005558 }
5559
Craig Topper7a9a28b2012-08-12 02:23:29 +00005560 if (Values.size() > 1 && VT.is128BitVector()) {
Nate Begemanfdea31a2010-03-24 20:49:50 +00005561 // Check for a build vector of consecutive loads.
5562 for (unsigned i = 0; i < NumElems; ++i)
5563 V[i] = Op.getOperand(i);
Michael J. Spencerec38de22010-10-10 22:04:20 +00005564
Nate Begemanfdea31a2010-03-24 20:49:50 +00005565 // Check for elements which are consecutive loads.
5566 SDValue LD = EltsFromConsecutiveLoads(VT, V, dl, DAG);
5567 if (LD.getNode())
5568 return LD;
Michael J. Spencerec38de22010-10-10 22:04:20 +00005569
Michael Liaofacace82012-10-19 17:15:18 +00005570 // Check for a build vector from mostly shuffle plus few inserting.
5571 SDValue Sh = buildFromShuffleMostly(Op, DAG);
5572 if (Sh.getNode())
5573 return Sh;
5574
Michael J. Spencerec38de22010-10-10 22:04:20 +00005575 // For SSE 4.1, use insertps to put the high elements into the low element.
Craig Topperd0a31172012-01-10 06:37:29 +00005576 if (getSubtarget()->hasSSE41()) {
Chris Lattner24faf612010-08-28 17:59:08 +00005577 SDValue Result;
5578 if (Op.getOperand(0).getOpcode() != ISD::UNDEF)
5579 Result = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(0));
5580 else
5581 Result = DAG.getUNDEF(VT);
Michael J. Spencerec38de22010-10-10 22:04:20 +00005582
Chris Lattner24faf612010-08-28 17:59:08 +00005583 for (unsigned i = 1; i < NumElems; ++i) {
5584 if (Op.getOperand(i).getOpcode() == ISD::UNDEF) continue;
5585 Result = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, Result,
Nate Begeman9008ca62009-04-27 18:41:29 +00005586 Op.getOperand(i), DAG.getIntPtrConstant(i));
Chris Lattner24faf612010-08-28 17:59:08 +00005587 }
5588 return Result;
Nate Begeman9008ca62009-04-27 18:41:29 +00005589 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00005590
Chris Lattner6e80e442010-08-28 17:15:43 +00005591 // Otherwise, expand into a number of unpckl*, start by extending each of
5592 // our (non-undef) elements to the full vector width with the element in the
5593 // bottom slot of the vector (which generates no code for SSE).
5594 for (unsigned i = 0; i < NumElems; ++i) {
5595 if (Op.getOperand(i).getOpcode() != ISD::UNDEF)
5596 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(i));
5597 else
5598 V[i] = DAG.getUNDEF(VT);
5599 }
5600
5601 // Next, we iteratively mix elements, e.g. for v4f32:
Evan Cheng0db9fe62006-04-25 20:13:52 +00005602 // Step 1: unpcklps 0, 2 ==> X: <?, ?, 2, 0>
5603 // : unpcklps 1, 3 ==> Y: <?, ?, 3, 1>
5604 // Step 2: unpcklps X, Y ==> <3, 2, 1, 0>
Chris Lattner6e80e442010-08-28 17:15:43 +00005605 unsigned EltStride = NumElems >> 1;
5606 while (EltStride != 0) {
Chris Lattner3ddcc432010-08-28 17:28:30 +00005607 for (unsigned i = 0; i < EltStride; ++i) {
5608 // If V[i+EltStride] is undef and this is the first round of mixing,
5609 // then it is safe to just drop this shuffle: V[i] is already in the
5610 // right place, the one element (since it's the first round) being
5611 // inserted as undef can be dropped. This isn't safe for successive
5612 // rounds because they will permute elements within both vectors.
5613 if (V[i+EltStride].getOpcode() == ISD::UNDEF &&
5614 EltStride == NumElems/2)
5615 continue;
Michael J. Spencerec38de22010-10-10 22:04:20 +00005616
Chris Lattner6e80e442010-08-28 17:15:43 +00005617 V[i] = getUnpackl(DAG, dl, VT, V[i], V[i + EltStride]);
Chris Lattner3ddcc432010-08-28 17:28:30 +00005618 }
Chris Lattner6e80e442010-08-28 17:15:43 +00005619 EltStride >>= 1;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005620 }
5621 return V[0];
5622 }
Dan Gohman475871a2008-07-27 21:46:04 +00005623 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005624}
5625
Bruno Cardoso Lopes8af24512011-08-01 21:54:02 +00005626// LowerAVXCONCAT_VECTORS - 256-bit AVX can use the vinsertf128 instruction
5627// to create 256-bit vectors from two other 128-bit ones.
5628static SDValue LowerAVXCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) {
5629 DebugLoc dl = Op.getDebugLoc();
5630 EVT ResVT = Op.getValueType();
5631
Craig Topper7a9a28b2012-08-12 02:23:29 +00005632 assert(ResVT.is256BitVector() && "Value type must be 256-bit wide");
Bruno Cardoso Lopes8af24512011-08-01 21:54:02 +00005633
5634 SDValue V1 = Op.getOperand(0);
5635 SDValue V2 = Op.getOperand(1);
5636 unsigned NumElems = ResVT.getVectorNumElements();
5637
Craig Topper4c7972d2012-04-22 18:15:59 +00005638 return Concat128BitVectors(V1, V2, ResVT, NumElems, DAG, dl);
Bruno Cardoso Lopes8af24512011-08-01 21:54:02 +00005639}
5640
Craig Topper55b24052012-09-11 06:15:32 +00005641static SDValue LowerCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) {
Bruno Cardoso Lopes8af24512011-08-01 21:54:02 +00005642 assert(Op.getNumOperands() == 2);
Bruno Cardoso Lopes8af24512011-08-01 21:54:02 +00005643
5644 // 256-bit AVX can use the vinsertf128 instruction to create 256-bit vectors
5645 // from two other 128-bit ones.
5646 return LowerAVXCONCAT_VECTORS(Op, DAG);
5647}
5648
Nadav Roteme80aa7c2012-04-09 08:33:21 +00005649// Try to lower a shuffle node into a simple blend instruction.
Craig Topper55b24052012-09-11 06:15:32 +00005650static SDValue
5651LowerVECTOR_SHUFFLEtoBlend(ShuffleVectorSDNode *SVOp,
5652 const X86Subtarget *Subtarget, SelectionDAG &DAG) {
Nadav Roteme80aa7c2012-04-09 08:33:21 +00005653 SDValue V1 = SVOp->getOperand(0);
5654 SDValue V2 = SVOp->getOperand(1);
5655 DebugLoc dl = SVOp->getDebugLoc();
Elena Demikhovsky226e0e62012-12-05 09:24:57 +00005656 EVT VT = SVOp->getValueType(0);
5657 EVT EltVT = VT.getVectorElementType();
Craig Topper1842ba02012-04-23 06:38:28 +00005658 unsigned NumElems = VT.getVectorNumElements();
Nadav Roteme80aa7c2012-04-09 08:33:21 +00005659
Elena Demikhovsky226e0e62012-12-05 09:24:57 +00005660 if (!Subtarget->hasSSE41() || EltVT == MVT::i8)
5661 return SDValue();
5662 if (!Subtarget->hasInt256() && VT == MVT::v16i16)
Nadav Roteme80aa7c2012-04-09 08:33:21 +00005663 return SDValue();
5664
Elena Demikhovsky226e0e62012-12-05 09:24:57 +00005665 // Check the mask for BLEND and build the value.
5666 unsigned MaskValue = 0;
5667 // There are 2 lanes if (NumElems > 8), and 1 lane otherwise.
5668 unsigned NumLanes = (NumElems-1)/8 + 1;
5669 unsigned NumElemsInLane = NumElems / NumLanes;
Nadav Roteme6113782012-04-11 06:40:27 +00005670
Elena Demikhovsky226e0e62012-12-05 09:24:57 +00005671 // Blend for v16i16 should be symetric for the both lanes.
5672 for (unsigned i = 0; i < NumElemsInLane; ++i) {
Nadav Roteme6113782012-04-11 06:40:27 +00005673
Elena Demikhovsky226e0e62012-12-05 09:24:57 +00005674 int SndLaneEltIdx = (NumLanes == 2) ?
5675 SVOp->getMaskElt(i + NumElemsInLane) : -1;
Nadav Roteme80aa7c2012-04-09 08:33:21 +00005676 int EltIdx = SVOp->getMaskElt(i);
Elena Demikhovsky226e0e62012-12-05 09:24:57 +00005677
5678 if ((EltIdx == -1 || EltIdx == (int)i) &&
5679 (SndLaneEltIdx == -1 || SndLaneEltIdx == (int)(i + NumElemsInLane)))
5680 continue;
5681
5682 if (((unsigned)EltIdx == (i + NumElems)) &&
5683 (SndLaneEltIdx == -1 ||
5684 (unsigned)SndLaneEltIdx == i + NumElems + NumElemsInLane))
5685 MaskValue |= (1<<i);
5686 else
Craig Topper1842ba02012-04-23 06:38:28 +00005687 return SDValue();
Nadav Roteme80aa7c2012-04-09 08:33:21 +00005688 }
5689
Elena Demikhovsky226e0e62012-12-05 09:24:57 +00005690 // Convert i32 vectors to floating point if it is not AVX2.
5691 // AVX2 introduced VPBLENDD instruction for 128 and 256-bit vectors.
5692 EVT BlendVT = VT;
5693 if (EltVT == MVT::i64 || (EltVT == MVT::i32 && !Subtarget->hasInt256())) {
5694 BlendVT = EVT::getVectorVT(*DAG.getContext(),
5695 EVT::getFloatingPointVT(EltVT.getSizeInBits()),
5696 NumElems);
5697 V1 = DAG.getNode(ISD::BITCAST, dl, VT, V1);
5698 V2 = DAG.getNode(ISD::BITCAST, dl, VT, V2);
5699 }
5700
5701 SDValue Ret = DAG.getNode(X86ISD::BLENDI, dl, BlendVT, V1, V2,
5702 DAG.getConstant(MaskValue, MVT::i32));
Nadav Roteme6113782012-04-11 06:40:27 +00005703 return DAG.getNode(ISD::BITCAST, dl, VT, Ret);
Nadav Roteme80aa7c2012-04-09 08:33:21 +00005704}
5705
Nate Begemanb9a47b82009-02-23 08:49:38 +00005706// v8i16 shuffles - Prefer shuffles in the following order:
5707// 1. [all] pshuflw, pshufhw, optional move
5708// 2. [ssse3] 1 x pshufb
5709// 3. [ssse3] 2 x pshufb + 1 x por
5710// 4. [all] mov + pshuflw + pshufhw + N x (pextrw + pinsrw)
Craig Topper55b24052012-09-11 06:15:32 +00005711static SDValue
5712LowerVECTOR_SHUFFLEv8i16(SDValue Op, const X86Subtarget *Subtarget,
5713 SelectionDAG &DAG) {
Bruno Cardoso Lopesbf8154a2010-08-21 01:32:18 +00005714 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Nate Begeman9008ca62009-04-27 18:41:29 +00005715 SDValue V1 = SVOp->getOperand(0);
5716 SDValue V2 = SVOp->getOperand(1);
5717 DebugLoc dl = SVOp->getDebugLoc();
Nate Begemanb9a47b82009-02-23 08:49:38 +00005718 SmallVector<int, 8> MaskVals;
Evan Cheng14b32e12007-12-11 01:46:18 +00005719
Nate Begemanb9a47b82009-02-23 08:49:38 +00005720 // Determine if more than 1 of the words in each of the low and high quadwords
5721 // of the result come from the same quadword of one of the two inputs. Undef
5722 // mask values count as coming from any quadword, for better codegen.
Benjamin Kramer003fad92011-10-15 13:28:31 +00005723 unsigned LoQuad[] = { 0, 0, 0, 0 };
5724 unsigned HiQuad[] = { 0, 0, 0, 0 };
Benjamin Kramer699ddcb2012-02-06 12:06:18 +00005725 std::bitset<4> InputQuads;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005726 for (unsigned i = 0; i < 8; ++i) {
Benjamin Kramer003fad92011-10-15 13:28:31 +00005727 unsigned *Quad = i < 4 ? LoQuad : HiQuad;
Nate Begeman9008ca62009-04-27 18:41:29 +00005728 int EltIdx = SVOp->getMaskElt(i);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005729 MaskVals.push_back(EltIdx);
5730 if (EltIdx < 0) {
5731 ++Quad[0];
5732 ++Quad[1];
5733 ++Quad[2];
5734 ++Quad[3];
Evan Cheng14b32e12007-12-11 01:46:18 +00005735 continue;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005736 }
5737 ++Quad[EltIdx / 4];
5738 InputQuads.set(EltIdx / 4);
Evan Cheng14b32e12007-12-11 01:46:18 +00005739 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00005740
Nate Begemanb9a47b82009-02-23 08:49:38 +00005741 int BestLoQuad = -1;
Evan Cheng14b32e12007-12-11 01:46:18 +00005742 unsigned MaxQuad = 1;
5743 for (unsigned i = 0; i < 4; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005744 if (LoQuad[i] > MaxQuad) {
5745 BestLoQuad = i;
5746 MaxQuad = LoQuad[i];
Evan Cheng14b32e12007-12-11 01:46:18 +00005747 }
Evan Cheng8a86c3f2007-12-07 08:07:39 +00005748 }
5749
Nate Begemanb9a47b82009-02-23 08:49:38 +00005750 int BestHiQuad = -1;
Evan Cheng14b32e12007-12-11 01:46:18 +00005751 MaxQuad = 1;
5752 for (unsigned i = 0; i < 4; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005753 if (HiQuad[i] > MaxQuad) {
5754 BestHiQuad = i;
5755 MaxQuad = HiQuad[i];
Evan Cheng14b32e12007-12-11 01:46:18 +00005756 }
5757 }
5758
Nate Begemanb9a47b82009-02-23 08:49:38 +00005759 // For SSSE3, If all 8 words of the result come from only 1 quadword of each
Eric Christopherfd179292009-08-27 18:07:15 +00005760 // of the two input vectors, shuffle them into one input vector so only a
Nate Begemanb9a47b82009-02-23 08:49:38 +00005761 // single pshufb instruction is necessary. If There are more than 2 input
5762 // quads, disable the next transformation since it does not help SSSE3.
5763 bool V1Used = InputQuads[0] || InputQuads[1];
5764 bool V2Used = InputQuads[2] || InputQuads[3];
Craig Topperd0a31172012-01-10 06:37:29 +00005765 if (Subtarget->hasSSSE3()) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005766 if (InputQuads.count() == 2 && V1Used && V2Used) {
Benjamin Kramer699ddcb2012-02-06 12:06:18 +00005767 BestLoQuad = InputQuads[0] ? 0 : 1;
5768 BestHiQuad = InputQuads[2] ? 2 : 3;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005769 }
5770 if (InputQuads.count() > 2) {
5771 BestLoQuad = -1;
5772 BestHiQuad = -1;
5773 }
5774 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00005775
Nate Begemanb9a47b82009-02-23 08:49:38 +00005776 // If BestLoQuad or BestHiQuad are set, shuffle the quads together and update
5777 // the shuffle mask. If a quad is scored as -1, that means that it contains
5778 // words from all 4 input quadwords.
5779 SDValue NewV;
5780 if (BestLoQuad >= 0 || BestHiQuad >= 0) {
Benjamin Kramer9c683542012-01-30 15:16:21 +00005781 int MaskV[] = {
5782 BestLoQuad < 0 ? 0 : BestLoQuad,
5783 BestHiQuad < 0 ? 1 : BestHiQuad
5784 };
Eric Christopherfd179292009-08-27 18:07:15 +00005785 NewV = DAG.getVectorShuffle(MVT::v2i64, dl,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005786 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64, V1),
5787 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64, V2), &MaskV[0]);
5788 NewV = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, NewV);
Evan Cheng14b32e12007-12-11 01:46:18 +00005789
Nate Begemanb9a47b82009-02-23 08:49:38 +00005790 // Rewrite the MaskVals and assign NewV to V1 if NewV now contains all the
5791 // source words for the shuffle, to aid later transformations.
5792 bool AllWordsInNewV = true;
Mon P Wang37b9a192009-03-11 06:35:11 +00005793 bool InOrder[2] = { true, true };
Evan Cheng14b32e12007-12-11 01:46:18 +00005794 for (unsigned i = 0; i != 8; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005795 int idx = MaskVals[i];
Mon P Wang37b9a192009-03-11 06:35:11 +00005796 if (idx != (int)i)
5797 InOrder[i/4] = false;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005798 if (idx < 0 || (idx/4) == BestLoQuad || (idx/4) == BestHiQuad)
Evan Cheng14b32e12007-12-11 01:46:18 +00005799 continue;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005800 AllWordsInNewV = false;
5801 break;
Evan Cheng14b32e12007-12-11 01:46:18 +00005802 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00005803
Nate Begemanb9a47b82009-02-23 08:49:38 +00005804 bool pshuflw = AllWordsInNewV, pshufhw = AllWordsInNewV;
5805 if (AllWordsInNewV) {
5806 for (int i = 0; i != 8; ++i) {
5807 int idx = MaskVals[i];
5808 if (idx < 0)
Evan Cheng14b32e12007-12-11 01:46:18 +00005809 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00005810 idx = MaskVals[i] = (idx / 4) == BestLoQuad ? (idx & 3) : (idx & 3) + 4;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005811 if ((idx != i) && idx < 4)
5812 pshufhw = false;
5813 if ((idx != i) && idx > 3)
5814 pshuflw = false;
Evan Cheng14b32e12007-12-11 01:46:18 +00005815 }
Nate Begemanb9a47b82009-02-23 08:49:38 +00005816 V1 = NewV;
5817 V2Used = false;
5818 BestLoQuad = 0;
5819 BestHiQuad = 1;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00005820 }
Evan Cheng14b32e12007-12-11 01:46:18 +00005821
Nate Begemanb9a47b82009-02-23 08:49:38 +00005822 // If we've eliminated the use of V2, and the new mask is a pshuflw or
5823 // pshufhw, that's as cheap as it gets. Return the new shuffle.
Mon P Wang37b9a192009-03-11 06:35:11 +00005824 if ((pshufhw && InOrder[0]) || (pshuflw && InOrder[1])) {
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00005825 unsigned Opc = pshufhw ? X86ISD::PSHUFHW : X86ISD::PSHUFLW;
5826 unsigned TargetMask = 0;
5827 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV,
Owen Anderson825b72b2009-08-11 20:47:22 +00005828 DAG.getUNDEF(MVT::v8i16), &MaskVals[0]);
Craig Topperdd637ae2012-02-19 05:41:45 +00005829 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(NewV.getNode());
5830 TargetMask = pshufhw ? getShufflePSHUFHWImmediate(SVOp):
5831 getShufflePSHUFLWImmediate(SVOp);
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00005832 V1 = NewV.getOperand(0);
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00005833 return getTargetShuffleNode(Opc, dl, MVT::v8i16, V1, TargetMask, DAG);
Evan Cheng14b32e12007-12-11 01:46:18 +00005834 }
Evan Cheng14b32e12007-12-11 01:46:18 +00005835 }
Eric Christopherfd179292009-08-27 18:07:15 +00005836
Nate Begemanb9a47b82009-02-23 08:49:38 +00005837 // If we have SSSE3, and all words of the result are from 1 input vector,
5838 // case 2 is generated, otherwise case 3 is generated. If no SSSE3
5839 // is present, fall back to case 4.
Craig Topperd0a31172012-01-10 06:37:29 +00005840 if (Subtarget->hasSSSE3()) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005841 SmallVector<SDValue,16> pshufbMask;
Eric Christopherfd179292009-08-27 18:07:15 +00005842
Nate Begemanb9a47b82009-02-23 08:49:38 +00005843 // If we have elements from both input vectors, set the high bit of the
Eric Christopherfd179292009-08-27 18:07:15 +00005844 // shuffle mask element to zero out elements that come from V2 in the V1
Nate Begemanb9a47b82009-02-23 08:49:38 +00005845 // mask, and elements that come from V1 in the V2 mask, so that the two
5846 // results can be OR'd together.
5847 bool TwoInputs = V1Used && V2Used;
5848 for (unsigned i = 0; i != 8; ++i) {
5849 int EltIdx = MaskVals[i] * 2;
Craig Topperbe97ae92012-05-18 07:07:36 +00005850 int Idx0 = (TwoInputs && (EltIdx >= 16)) ? 0x80 : EltIdx;
5851 int Idx1 = (TwoInputs && (EltIdx >= 16)) ? 0x80 : EltIdx+1;
5852 pshufbMask.push_back(DAG.getConstant(Idx0, MVT::i8));
5853 pshufbMask.push_back(DAG.getConstant(Idx1, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005854 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005855 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, V1);
Eric Christopherfd179292009-08-27 18:07:15 +00005856 V1 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V1,
Evan Chenga87008d2009-02-25 22:49:59 +00005857 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005858 MVT::v16i8, &pshufbMask[0], 16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005859 if (!TwoInputs)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005860 return DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V1);
Eric Christopherfd179292009-08-27 18:07:15 +00005861
Nate Begemanb9a47b82009-02-23 08:49:38 +00005862 // Calculate the shuffle mask for the second input, shuffle it, and
5863 // OR it with the first shuffled input.
5864 pshufbMask.clear();
5865 for (unsigned i = 0; i != 8; ++i) {
5866 int EltIdx = MaskVals[i] * 2;
Craig Topperbe97ae92012-05-18 07:07:36 +00005867 int Idx0 = (EltIdx < 16) ? 0x80 : EltIdx - 16;
5868 int Idx1 = (EltIdx < 16) ? 0x80 : EltIdx - 15;
5869 pshufbMask.push_back(DAG.getConstant(Idx0, MVT::i8));
5870 pshufbMask.push_back(DAG.getConstant(Idx1, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005871 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005872 V2 = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, V2);
Eric Christopherfd179292009-08-27 18:07:15 +00005873 V2 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V2,
Evan Chenga87008d2009-02-25 22:49:59 +00005874 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005875 MVT::v16i8, &pshufbMask[0], 16));
5876 V1 = DAG.getNode(ISD::OR, dl, MVT::v16i8, V1, V2);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005877 return DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005878 }
5879
5880 // If BestLoQuad >= 0, generate a pshuflw to put the low elements in order,
5881 // and update MaskVals with new element order.
Benjamin Kramer9c683542012-01-30 15:16:21 +00005882 std::bitset<8> InOrder;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005883 if (BestLoQuad >= 0) {
Benjamin Kramer9c683542012-01-30 15:16:21 +00005884 int MaskV[] = { -1, -1, -1, -1, 4, 5, 6, 7 };
Nate Begemanb9a47b82009-02-23 08:49:38 +00005885 for (int i = 0; i != 4; ++i) {
5886 int idx = MaskVals[i];
5887 if (idx < 0) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005888 InOrder.set(i);
5889 } else if ((idx / 4) == BestLoQuad) {
Benjamin Kramer9c683542012-01-30 15:16:21 +00005890 MaskV[i] = idx & 3;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005891 InOrder.set(i);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005892 }
5893 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005894 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV, DAG.getUNDEF(MVT::v8i16),
Nate Begeman9008ca62009-04-27 18:41:29 +00005895 &MaskV[0]);
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00005896
Craig Topperdd637ae2012-02-19 05:41:45 +00005897 if (NewV.getOpcode() == ISD::VECTOR_SHUFFLE && Subtarget->hasSSSE3()) {
5898 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(NewV.getNode());
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00005899 NewV = getTargetShuffleNode(X86ISD::PSHUFLW, dl, MVT::v8i16,
Craig Topperdd637ae2012-02-19 05:41:45 +00005900 NewV.getOperand(0),
5901 getShufflePSHUFLWImmediate(SVOp), DAG);
5902 }
Nate Begemanb9a47b82009-02-23 08:49:38 +00005903 }
Eric Christopherfd179292009-08-27 18:07:15 +00005904
Nate Begemanb9a47b82009-02-23 08:49:38 +00005905 // If BestHi >= 0, generate a pshufhw to put the high elements in order,
5906 // and update MaskVals with the new element order.
5907 if (BestHiQuad >= 0) {
Benjamin Kramer9c683542012-01-30 15:16:21 +00005908 int MaskV[] = { 0, 1, 2, 3, -1, -1, -1, -1 };
Nate Begemanb9a47b82009-02-23 08:49:38 +00005909 for (unsigned i = 4; i != 8; ++i) {
5910 int idx = MaskVals[i];
5911 if (idx < 0) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005912 InOrder.set(i);
5913 } else if ((idx / 4) == BestHiQuad) {
Benjamin Kramer9c683542012-01-30 15:16:21 +00005914 MaskV[i] = (idx & 3) + 4;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005915 InOrder.set(i);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005916 }
5917 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005918 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV, DAG.getUNDEF(MVT::v8i16),
Nate Begeman9008ca62009-04-27 18:41:29 +00005919 &MaskV[0]);
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00005920
Craig Topperdd637ae2012-02-19 05:41:45 +00005921 if (NewV.getOpcode() == ISD::VECTOR_SHUFFLE && Subtarget->hasSSSE3()) {
5922 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(NewV.getNode());
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00005923 NewV = getTargetShuffleNode(X86ISD::PSHUFHW, dl, MVT::v8i16,
Craig Topperdd637ae2012-02-19 05:41:45 +00005924 NewV.getOperand(0),
5925 getShufflePSHUFHWImmediate(SVOp), DAG);
5926 }
Nate Begemanb9a47b82009-02-23 08:49:38 +00005927 }
Eric Christopherfd179292009-08-27 18:07:15 +00005928
Nate Begemanb9a47b82009-02-23 08:49:38 +00005929 // In case BestHi & BestLo were both -1, which means each quadword has a word
5930 // from each of the four input quadwords, calculate the InOrder bitvector now
5931 // before falling through to the insert/extract cleanup.
5932 if (BestLoQuad == -1 && BestHiQuad == -1) {
5933 NewV = V1;
5934 for (int i = 0; i != 8; ++i)
5935 if (MaskVals[i] < 0 || MaskVals[i] == i)
5936 InOrder.set(i);
5937 }
Eric Christopherfd179292009-08-27 18:07:15 +00005938
Nate Begemanb9a47b82009-02-23 08:49:38 +00005939 // The other elements are put in the right place using pextrw and pinsrw.
5940 for (unsigned i = 0; i != 8; ++i) {
5941 if (InOrder[i])
5942 continue;
5943 int EltIdx = MaskVals[i];
5944 if (EltIdx < 0)
5945 continue;
Craig Topper6643d9c2012-05-04 06:18:33 +00005946 SDValue ExtOp = (EltIdx < 8) ?
5947 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, V1,
5948 DAG.getIntPtrConstant(EltIdx)) :
5949 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, V2,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005950 DAG.getIntPtrConstant(EltIdx - 8));
Owen Anderson825b72b2009-08-11 20:47:22 +00005951 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, ExtOp,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005952 DAG.getIntPtrConstant(i));
5953 }
5954 return NewV;
5955}
5956
5957// v16i8 shuffles - Prefer shuffles in the following order:
5958// 1. [ssse3] 1 x pshufb
5959// 2. [ssse3] 2 x pshufb + 1 x por
5960// 3. [all] v8i16 shuffle + N x pextrw + rotate + pinsrw
5961static
Nate Begeman9008ca62009-04-27 18:41:29 +00005962SDValue LowerVECTOR_SHUFFLEv16i8(ShuffleVectorSDNode *SVOp,
Dan Gohmand858e902010-04-17 15:26:15 +00005963 SelectionDAG &DAG,
5964 const X86TargetLowering &TLI) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005965 SDValue V1 = SVOp->getOperand(0);
5966 SDValue V2 = SVOp->getOperand(1);
5967 DebugLoc dl = SVOp->getDebugLoc();
Benjamin Kramered4c8c62012-01-15 13:16:05 +00005968 ArrayRef<int> MaskVals = SVOp->getMask();
Eric Christopherfd179292009-08-27 18:07:15 +00005969
Nate Begemanb9a47b82009-02-23 08:49:38 +00005970 // If we have SSSE3, case 1 is generated when all result bytes come from
Eric Christopherfd179292009-08-27 18:07:15 +00005971 // one of the inputs. Otherwise, case 2 is generated. If no SSSE3 is
Nate Begemanb9a47b82009-02-23 08:49:38 +00005972 // present, fall back to case 3.
Eric Christopherfd179292009-08-27 18:07:15 +00005973
Nate Begemanb9a47b82009-02-23 08:49:38 +00005974 // If SSSE3, use 1 pshufb instruction per vector with elements in the result.
Craig Topperd0a31172012-01-10 06:37:29 +00005975 if (TLI.getSubtarget()->hasSSSE3()) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005976 SmallVector<SDValue,16> pshufbMask;
Eric Christopherfd179292009-08-27 18:07:15 +00005977
Nate Begemanb9a47b82009-02-23 08:49:38 +00005978 // If all result elements are from one input vector, then only translate
Eric Christopherfd179292009-08-27 18:07:15 +00005979 // undef mask values to 0x80 (zero out result) in the pshufb mask.
Nate Begemanb9a47b82009-02-23 08:49:38 +00005980 //
5981 // Otherwise, we have elements from both input vectors, and must zero out
5982 // elements that come from V2 in the first mask, and V1 in the second mask
5983 // so that we can OR them together.
Nate Begemanb9a47b82009-02-23 08:49:38 +00005984 for (unsigned i = 0; i != 16; ++i) {
5985 int EltIdx = MaskVals[i];
Craig Topperb82b5ab2012-05-18 06:42:06 +00005986 if (EltIdx < 0 || EltIdx >= 16)
5987 EltIdx = 0x80;
Owen Anderson825b72b2009-08-11 20:47:22 +00005988 pshufbMask.push_back(DAG.getConstant(EltIdx, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005989 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005990 V1 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V1,
Evan Chenga87008d2009-02-25 22:49:59 +00005991 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005992 MVT::v16i8, &pshufbMask[0], 16));
Michael Liao265bcb12012-08-31 20:12:31 +00005993
5994 // As PSHUFB will zero elements with negative indices, it's safe to ignore
5995 // the 2nd operand if it's undefined or zero.
5996 if (V2.getOpcode() == ISD::UNDEF ||
5997 ISD::isBuildVectorAllZeros(V2.getNode()))
Nate Begemanb9a47b82009-02-23 08:49:38 +00005998 return V1;
Eric Christopherfd179292009-08-27 18:07:15 +00005999
Nate Begemanb9a47b82009-02-23 08:49:38 +00006000 // Calculate the shuffle mask for the second input, shuffle it, and
6001 // OR it with the first shuffled input.
6002 pshufbMask.clear();
6003 for (unsigned i = 0; i != 16; ++i) {
6004 int EltIdx = MaskVals[i];
Craig Topperb82b5ab2012-05-18 06:42:06 +00006005 EltIdx = (EltIdx < 16) ? 0x80 : EltIdx - 16;
Craig Topper85b9e562012-05-22 06:09:38 +00006006 pshufbMask.push_back(DAG.getConstant(EltIdx, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00006007 }
Owen Anderson825b72b2009-08-11 20:47:22 +00006008 V2 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V2,
Evan Chenga87008d2009-02-25 22:49:59 +00006009 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00006010 MVT::v16i8, &pshufbMask[0], 16));
6011 return DAG.getNode(ISD::OR, dl, MVT::v16i8, V1, V2);
Nate Begemanb9a47b82009-02-23 08:49:38 +00006012 }
Eric Christopherfd179292009-08-27 18:07:15 +00006013
Nate Begemanb9a47b82009-02-23 08:49:38 +00006014 // No SSSE3 - Calculate in place words and then fix all out of place words
6015 // With 0-16 extracts & inserts. Worst case is 16 bytes out of order from
6016 // the 16 different words that comprise the two doublequadword input vectors.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006017 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V1);
6018 V2 = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V2);
Craig Topperb82b5ab2012-05-18 06:42:06 +00006019 SDValue NewV = V1;
Nate Begemanb9a47b82009-02-23 08:49:38 +00006020 for (int i = 0; i != 8; ++i) {
6021 int Elt0 = MaskVals[i*2];
6022 int Elt1 = MaskVals[i*2+1];
Eric Christopherfd179292009-08-27 18:07:15 +00006023
Nate Begemanb9a47b82009-02-23 08:49:38 +00006024 // This word of the result is all undef, skip it.
6025 if (Elt0 < 0 && Elt1 < 0)
6026 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00006027
Nate Begemanb9a47b82009-02-23 08:49:38 +00006028 // This word of the result is already in the correct place, skip it.
Craig Topperb82b5ab2012-05-18 06:42:06 +00006029 if ((Elt0 == i*2) && (Elt1 == i*2+1))
Nate Begemanb9a47b82009-02-23 08:49:38 +00006030 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00006031
Nate Begemanb9a47b82009-02-23 08:49:38 +00006032 SDValue Elt0Src = Elt0 < 16 ? V1 : V2;
6033 SDValue Elt1Src = Elt1 < 16 ? V1 : V2;
6034 SDValue InsElt;
Mon P Wang6b3ef692009-03-11 18:47:57 +00006035
6036 // If Elt0 and Elt1 are defined, are consecutive, and can be load
6037 // using a single extract together, load it and store it.
6038 if ((Elt0 >= 0) && ((Elt0 + 1) == Elt1) && ((Elt0 & 1) == 0)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00006039 InsElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, Elt1Src,
Mon P Wang6b3ef692009-03-11 18:47:57 +00006040 DAG.getIntPtrConstant(Elt1 / 2));
Owen Anderson825b72b2009-08-11 20:47:22 +00006041 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, InsElt,
Mon P Wang6b3ef692009-03-11 18:47:57 +00006042 DAG.getIntPtrConstant(i));
6043 continue;
6044 }
6045
Nate Begemanb9a47b82009-02-23 08:49:38 +00006046 // If Elt1 is defined, extract it from the appropriate source. If the
Mon P Wang6b3ef692009-03-11 18:47:57 +00006047 // source byte is not also odd, shift the extracted word left 8 bits
6048 // otherwise clear the bottom 8 bits if we need to do an or.
Nate Begemanb9a47b82009-02-23 08:49:38 +00006049 if (Elt1 >= 0) {
Owen Anderson825b72b2009-08-11 20:47:22 +00006050 InsElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, Elt1Src,
Nate Begemanb9a47b82009-02-23 08:49:38 +00006051 DAG.getIntPtrConstant(Elt1 / 2));
6052 if ((Elt1 & 1) == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00006053 InsElt = DAG.getNode(ISD::SHL, dl, MVT::i16, InsElt,
Owen Anderson95771af2011-02-25 21:41:48 +00006054 DAG.getConstant(8,
6055 TLI.getShiftAmountTy(InsElt.getValueType())));
Mon P Wang6b3ef692009-03-11 18:47:57 +00006056 else if (Elt0 >= 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00006057 InsElt = DAG.getNode(ISD::AND, dl, MVT::i16, InsElt,
6058 DAG.getConstant(0xFF00, MVT::i16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00006059 }
6060 // If Elt0 is defined, extract it from the appropriate source. If the
6061 // source byte is not also even, shift the extracted word right 8 bits. If
6062 // Elt1 was also defined, OR the extracted values together before
6063 // inserting them in the result.
6064 if (Elt0 >= 0) {
Owen Anderson825b72b2009-08-11 20:47:22 +00006065 SDValue InsElt0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16,
Nate Begemanb9a47b82009-02-23 08:49:38 +00006066 Elt0Src, DAG.getIntPtrConstant(Elt0 / 2));
6067 if ((Elt0 & 1) != 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00006068 InsElt0 = DAG.getNode(ISD::SRL, dl, MVT::i16, InsElt0,
Owen Anderson95771af2011-02-25 21:41:48 +00006069 DAG.getConstant(8,
6070 TLI.getShiftAmountTy(InsElt0.getValueType())));
Mon P Wang6b3ef692009-03-11 18:47:57 +00006071 else if (Elt1 >= 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00006072 InsElt0 = DAG.getNode(ISD::AND, dl, MVT::i16, InsElt0,
6073 DAG.getConstant(0x00FF, MVT::i16));
6074 InsElt = Elt1 >= 0 ? DAG.getNode(ISD::OR, dl, MVT::i16, InsElt, InsElt0)
Nate Begemanb9a47b82009-02-23 08:49:38 +00006075 : InsElt0;
6076 }
Owen Anderson825b72b2009-08-11 20:47:22 +00006077 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, InsElt,
Nate Begemanb9a47b82009-02-23 08:49:38 +00006078 DAG.getIntPtrConstant(i));
6079 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006080 return DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, NewV);
Evan Cheng14b32e12007-12-11 01:46:18 +00006081}
6082
Elena Demikhovsky41789462012-09-06 12:42:01 +00006083// v32i8 shuffles - Translate to VPSHUFB if possible.
6084static
6085SDValue LowerVECTOR_SHUFFLEv32i8(ShuffleVectorSDNode *SVOp,
Craig Topper55b24052012-09-11 06:15:32 +00006086 const X86Subtarget *Subtarget,
6087 SelectionDAG &DAG) {
Elena Demikhovsky41789462012-09-06 12:42:01 +00006088 EVT VT = SVOp->getValueType(0);
6089 SDValue V1 = SVOp->getOperand(0);
6090 SDValue V2 = SVOp->getOperand(1);
6091 DebugLoc dl = SVOp->getDebugLoc();
Elena Demikhovsky8100d242012-09-10 12:13:11 +00006092 SmallVector<int, 32> MaskVals(SVOp->getMask().begin(), SVOp->getMask().end());
Elena Demikhovsky41789462012-09-06 12:42:01 +00006093
6094 bool V2IsUndef = V2.getOpcode() == ISD::UNDEF;
Elena Demikhovsky8100d242012-09-10 12:13:11 +00006095 bool V1IsAllZero = ISD::isBuildVectorAllZeros(V1.getNode());
6096 bool V2IsAllZero = ISD::isBuildVectorAllZeros(V2.getNode());
Elena Demikhovsky41789462012-09-06 12:42:01 +00006097
Michael Liao471b9172012-10-03 23:43:52 +00006098 // VPSHUFB may be generated if
Elena Demikhovsky8100d242012-09-10 12:13:11 +00006099 // (1) one of input vector is undefined or zeroinitializer.
6100 // The mask value 0x80 puts 0 in the corresponding slot of the vector.
6101 // And (2) the mask indexes don't cross the 128-bit lane.
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00006102 if (VT != MVT::v32i8 || !Subtarget->hasInt256() ||
Elena Demikhovsky8100d242012-09-10 12:13:11 +00006103 (!V2IsUndef && !V2IsAllZero && !V1IsAllZero))
Elena Demikhovsky41789462012-09-06 12:42:01 +00006104 return SDValue();
6105
Elena Demikhovsky8100d242012-09-10 12:13:11 +00006106 if (V1IsAllZero && !V2IsAllZero) {
6107 CommuteVectorShuffleMask(MaskVals, 32);
6108 V1 = V2;
6109 }
6110 SmallVector<SDValue, 32> pshufbMask;
Elena Demikhovsky41789462012-09-06 12:42:01 +00006111 for (unsigned i = 0; i != 32; i++) {
6112 int EltIdx = MaskVals[i];
6113 if (EltIdx < 0 || EltIdx >= 32)
6114 EltIdx = 0x80;
6115 else {
6116 if ((EltIdx >= 16 && i < 16) || (EltIdx < 16 && i >= 16))
6117 // Cross lane is not allowed.
6118 return SDValue();
6119 EltIdx &= 0xf;
6120 }
6121 pshufbMask.push_back(DAG.getConstant(EltIdx, MVT::i8));
6122 }
6123 return DAG.getNode(X86ISD::PSHUFB, dl, MVT::v32i8, V1,
6124 DAG.getNode(ISD::BUILD_VECTOR, dl,
6125 MVT::v32i8, &pshufbMask[0], 32));
6126}
6127
Evan Cheng7a831ce2007-12-15 03:00:47 +00006128/// RewriteAsNarrowerShuffle - Try rewriting v8i16 and v16i8 shuffles as 4 wide
Bruno Cardoso Lopes0a7dd4f2010-09-08 18:12:31 +00006129/// ones, or rewriting v4i32 / v4f32 as 2 wide ones if possible. This can be
Evan Cheng7a831ce2007-12-15 03:00:47 +00006130/// done when every pair / quad of shuffle mask elements point to elements in
6131/// the right sequence. e.g.
Bruno Cardoso Lopes0a7dd4f2010-09-08 18:12:31 +00006132/// vector_shuffle X, Y, <2, 3, | 10, 11, | 0, 1, | 14, 15>
Evan Cheng14b32e12007-12-11 01:46:18 +00006133static
Nate Begeman9008ca62009-04-27 18:41:29 +00006134SDValue RewriteAsNarrowerShuffle(ShuffleVectorSDNode *SVOp,
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006135 SelectionDAG &DAG, DebugLoc dl) {
Craig Topper11ac1f82012-05-04 04:08:44 +00006136 MVT VT = SVOp->getValueType(0).getSimpleVT();
Nate Begeman9008ca62009-04-27 18:41:29 +00006137 unsigned NumElems = VT.getVectorNumElements();
Craig Topper11ac1f82012-05-04 04:08:44 +00006138 MVT NewVT;
6139 unsigned Scale;
6140 switch (VT.SimpleTy) {
Craig Topperabb94d02012-02-05 03:43:23 +00006141 default: llvm_unreachable("Unexpected!");
Craig Topperf3640d72012-05-04 04:44:49 +00006142 case MVT::v4f32: NewVT = MVT::v2f64; Scale = 2; break;
6143 case MVT::v4i32: NewVT = MVT::v2i64; Scale = 2; break;
6144 case MVT::v8i16: NewVT = MVT::v4i32; Scale = 2; break;
6145 case MVT::v16i8: NewVT = MVT::v4i32; Scale = 4; break;
6146 case MVT::v16i16: NewVT = MVT::v8i32; Scale = 2; break;
6147 case MVT::v32i8: NewVT = MVT::v8i32; Scale = 4; break;
Evan Cheng7a831ce2007-12-15 03:00:47 +00006148 }
6149
Nate Begeman9008ca62009-04-27 18:41:29 +00006150 SmallVector<int, 8> MaskVec;
Craig Topper11ac1f82012-05-04 04:08:44 +00006151 for (unsigned i = 0; i != NumElems; i += Scale) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006152 int StartIdx = -1;
Craig Topper11ac1f82012-05-04 04:08:44 +00006153 for (unsigned j = 0; j != Scale; ++j) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006154 int EltIdx = SVOp->getMaskElt(i+j);
6155 if (EltIdx < 0)
Evan Cheng14b32e12007-12-11 01:46:18 +00006156 continue;
Craig Topper11ac1f82012-05-04 04:08:44 +00006157 if (StartIdx < 0)
6158 StartIdx = (EltIdx / Scale);
6159 if (EltIdx != (int)(StartIdx*Scale + j))
Dan Gohman475871a2008-07-27 21:46:04 +00006160 return SDValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00006161 }
Craig Topper11ac1f82012-05-04 04:08:44 +00006162 MaskVec.push_back(StartIdx);
Evan Cheng8a86c3f2007-12-07 08:07:39 +00006163 }
6164
Craig Topper11ac1f82012-05-04 04:08:44 +00006165 SDValue V1 = DAG.getNode(ISD::BITCAST, dl, NewVT, SVOp->getOperand(0));
6166 SDValue V2 = DAG.getNode(ISD::BITCAST, dl, NewVT, SVOp->getOperand(1));
Nate Begeman9008ca62009-04-27 18:41:29 +00006167 return DAG.getVectorShuffle(NewVT, dl, V1, V2, &MaskVec[0]);
Evan Cheng8a86c3f2007-12-07 08:07:39 +00006168}
6169
Evan Chengd880b972008-05-09 21:53:03 +00006170/// getVZextMovL - Return a zero-extending vector move low node.
Evan Cheng7e2ff772008-05-08 00:57:18 +00006171///
Owen Andersone50ed302009-08-10 22:56:29 +00006172static SDValue getVZextMovL(EVT VT, EVT OpVT,
Nate Begeman9008ca62009-04-27 18:41:29 +00006173 SDValue SrcOp, SelectionDAG &DAG,
6174 const X86Subtarget *Subtarget, DebugLoc dl) {
Owen Anderson825b72b2009-08-11 20:47:22 +00006175 if (VT == MVT::v2f64 || VT == MVT::v4f32) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00006176 LoadSDNode *LD = NULL;
Gabor Greifba36cb52008-08-28 21:40:38 +00006177 if (!isScalarLoadToVector(SrcOp.getNode(), &LD))
Evan Cheng7e2ff772008-05-08 00:57:18 +00006178 LD = dyn_cast<LoadSDNode>(SrcOp);
6179 if (!LD) {
6180 // movssrr and movsdrr do not clear top bits. Try to use movd, movq
6181 // instead.
Owen Anderson766b5ef2009-08-11 21:59:30 +00006182 MVT ExtVT = (OpVT == MVT::v2f64) ? MVT::i64 : MVT::i32;
Duncan Sandscdfad362010-11-03 12:17:33 +00006183 if ((ExtVT != MVT::i64 || Subtarget->is64Bit()) &&
Evan Cheng7e2ff772008-05-08 00:57:18 +00006184 SrcOp.getOpcode() == ISD::SCALAR_TO_VECTOR &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006185 SrcOp.getOperand(0).getOpcode() == ISD::BITCAST &&
Owen Anderson766b5ef2009-08-11 21:59:30 +00006186 SrcOp.getOperand(0).getOperand(0).getValueType() == ExtVT) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00006187 // PR2108
Owen Anderson825b72b2009-08-11 20:47:22 +00006188 OpVT = (OpVT == MVT::v2f64) ? MVT::v2i64 : MVT::v4i32;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006189 return DAG.getNode(ISD::BITCAST, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00006190 DAG.getNode(X86ISD::VZEXT_MOVL, dl, OpVT,
6191 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
6192 OpVT,
Gabor Greif327ef032008-08-28 23:19:51 +00006193 SrcOp.getOperand(0)
6194 .getOperand(0))));
Evan Cheng7e2ff772008-05-08 00:57:18 +00006195 }
6196 }
6197 }
6198
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006199 return DAG.getNode(ISD::BITCAST, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00006200 DAG.getNode(X86ISD::VZEXT_MOVL, dl, OpVT,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006201 DAG.getNode(ISD::BITCAST, dl,
Dale Johannesenace16102009-02-03 19:33:06 +00006202 OpVT, SrcOp)));
Evan Cheng7e2ff772008-05-08 00:57:18 +00006203}
6204
Bruno Cardoso Lopesd0888342011-07-22 00:14:56 +00006205/// LowerVECTOR_SHUFFLE_256 - Handle all 256-bit wide vectors shuffles
6206/// which could not be matched by any known target speficic shuffle
6207static SDValue
6208LowerVECTOR_SHUFFLE_256(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG) {
Elena Demikhovsky15963732012-06-26 08:04:10 +00006209
6210 SDValue NewOp = Compact8x32ShuffleNode(SVOp, DAG);
6211 if (NewOp.getNode())
6212 return NewOp;
6213
Craig Topper8f35c132012-01-20 09:29:03 +00006214 EVT VT = SVOp->getValueType(0);
Bruno Cardoso Lopes3b865982011-08-16 18:21:54 +00006215
Craig Topper8f35c132012-01-20 09:29:03 +00006216 unsigned NumElems = VT.getVectorNumElements();
6217 unsigned NumLaneElems = NumElems / 2;
6218
Craig Topper8f35c132012-01-20 09:29:03 +00006219 DebugLoc dl = SVOp->getDebugLoc();
6220 MVT EltVT = VT.getVectorElementType().getSimpleVT();
Craig Topper9a2b6e12012-04-06 07:45:23 +00006221 EVT NVT = MVT::getVectorVT(EltVT, NumLaneElems);
Craig Topper8ae97ba2012-05-21 06:40:16 +00006222 SDValue Output[2];
Craig Topper8f35c132012-01-20 09:29:03 +00006223
Craig Topper9a2b6e12012-04-06 07:45:23 +00006224 SmallVector<int, 16> Mask;
Craig Topper8f35c132012-01-20 09:29:03 +00006225 for (unsigned l = 0; l < 2; ++l) {
Craig Topper9a2b6e12012-04-06 07:45:23 +00006226 // Build a shuffle mask for the output, discovering on the fly which
6227 // input vectors to use as shuffle operands (recorded in InputUsed).
6228 // If building a suitable shuffle vector proves too hard, then bail
Craig Topper8ae97ba2012-05-21 06:40:16 +00006229 // out with UseBuildVector set.
6230 bool UseBuildVector = false;
Benjamin Kramer9e5512a2012-04-06 13:33:52 +00006231 int InputUsed[2] = { -1, -1 }; // Not yet discovered.
Craig Topper9a2b6e12012-04-06 07:45:23 +00006232 unsigned LaneStart = l * NumLaneElems;
6233 for (unsigned i = 0; i != NumLaneElems; ++i) {
6234 // The mask element. This indexes into the input.
6235 int Idx = SVOp->getMaskElt(i+LaneStart);
6236 if (Idx < 0) {
6237 // the mask element does not index into any input vector.
6238 Mask.push_back(-1);
6239 continue;
6240 }
Craig Topper8f35c132012-01-20 09:29:03 +00006241
Craig Topper9a2b6e12012-04-06 07:45:23 +00006242 // The input vector this mask element indexes into.
6243 int Input = Idx / NumLaneElems;
Craig Topper8f35c132012-01-20 09:29:03 +00006244
Craig Topper9a2b6e12012-04-06 07:45:23 +00006245 // Turn the index into an offset from the start of the input vector.
6246 Idx -= Input * NumLaneElems;
6247
6248 // Find or create a shuffle vector operand to hold this input.
6249 unsigned OpNo;
6250 for (OpNo = 0; OpNo < array_lengthof(InputUsed); ++OpNo) {
6251 if (InputUsed[OpNo] == Input)
6252 // This input vector is already an operand.
6253 break;
6254 if (InputUsed[OpNo] < 0) {
6255 // Create a new operand for this input vector.
6256 InputUsed[OpNo] = Input;
6257 break;
6258 }
6259 }
6260
6261 if (OpNo >= array_lengthof(InputUsed)) {
Craig Topper8ae97ba2012-05-21 06:40:16 +00006262 // More than two input vectors used! Give up on trying to create a
6263 // shuffle vector. Insert all elements into a BUILD_VECTOR instead.
6264 UseBuildVector = true;
6265 break;
Craig Topper9a2b6e12012-04-06 07:45:23 +00006266 }
6267
6268 // Add the mask index for the new shuffle vector.
6269 Mask.push_back(Idx + OpNo * NumLaneElems);
6270 }
6271
Craig Topper8ae97ba2012-05-21 06:40:16 +00006272 if (UseBuildVector) {
6273 SmallVector<SDValue, 16> SVOps;
6274 for (unsigned i = 0; i != NumLaneElems; ++i) {
6275 // The mask element. This indexes into the input.
6276 int Idx = SVOp->getMaskElt(i+LaneStart);
6277 if (Idx < 0) {
6278 SVOps.push_back(DAG.getUNDEF(EltVT));
6279 continue;
6280 }
6281
6282 // The input vector this mask element indexes into.
6283 int Input = Idx / NumElems;
6284
6285 // Turn the index into an offset from the start of the input vector.
6286 Idx -= Input * NumElems;
6287
6288 // Extract the vector element by hand.
6289 SVOps.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EltVT,
6290 SVOp->getOperand(Input),
6291 DAG.getIntPtrConstant(Idx)));
6292 }
6293
6294 // Construct the output using a BUILD_VECTOR.
6295 Output[l] = DAG.getNode(ISD::BUILD_VECTOR, dl, NVT, &SVOps[0],
6296 SVOps.size());
6297 } else if (InputUsed[0] < 0) {
Craig Topper9a2b6e12012-04-06 07:45:23 +00006298 // No input vectors were used! The result is undefined.
Craig Topper8ae97ba2012-05-21 06:40:16 +00006299 Output[l] = DAG.getUNDEF(NVT);
Craig Topper9a2b6e12012-04-06 07:45:23 +00006300 } else {
6301 SDValue Op0 = Extract128BitVector(SVOp->getOperand(InputUsed[0] / 2),
Craig Topperb14940a2012-04-22 20:55:18 +00006302 (InputUsed[0] % 2) * NumLaneElems,
6303 DAG, dl);
Craig Topper9a2b6e12012-04-06 07:45:23 +00006304 // If only one input was used, use an undefined vector for the other.
6305 SDValue Op1 = (InputUsed[1] < 0) ? DAG.getUNDEF(NVT) :
6306 Extract128BitVector(SVOp->getOperand(InputUsed[1] / 2),
Craig Topperb14940a2012-04-22 20:55:18 +00006307 (InputUsed[1] % 2) * NumLaneElems, DAG, dl);
Craig Topper9a2b6e12012-04-06 07:45:23 +00006308 // At least one input vector was used. Create a new shuffle vector.
Craig Topper8ae97ba2012-05-21 06:40:16 +00006309 Output[l] = DAG.getVectorShuffle(NVT, dl, Op0, Op1, &Mask[0]);
Craig Topper9a2b6e12012-04-06 07:45:23 +00006310 }
6311
6312 Mask.clear();
6313 }
Craig Topper8f35c132012-01-20 09:29:03 +00006314
6315 // Concatenate the result back
Craig Topper8ae97ba2012-05-21 06:40:16 +00006316 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT, Output[0], Output[1]);
Bruno Cardoso Lopesd0888342011-07-22 00:14:56 +00006317}
6318
Bruno Cardoso Lopes589b8972011-07-22 00:14:53 +00006319/// LowerVECTOR_SHUFFLE_128v4 - Handle all 128-bit wide vectors with
6320/// 4 elements, and match them with several different shuffle types.
Dan Gohman475871a2008-07-27 21:46:04 +00006321static SDValue
Bruno Cardoso Lopes589b8972011-07-22 00:14:53 +00006322LowerVECTOR_SHUFFLE_128v4(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006323 SDValue V1 = SVOp->getOperand(0);
6324 SDValue V2 = SVOp->getOperand(1);
6325 DebugLoc dl = SVOp->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00006326 EVT VT = SVOp->getValueType(0);
Eric Christopherfd179292009-08-27 18:07:15 +00006327
Craig Topper7a9a28b2012-08-12 02:23:29 +00006328 assert(VT.is128BitVector() && "Unsupported vector size");
Bruno Cardoso Lopes589b8972011-07-22 00:14:53 +00006329
Benjamin Kramer9c683542012-01-30 15:16:21 +00006330 std::pair<int, int> Locs[4];
6331 int Mask1[] = { -1, -1, -1, -1 };
Benjamin Kramered4c8c62012-01-15 13:16:05 +00006332 SmallVector<int, 8> PermMask(SVOp->getMask().begin(), SVOp->getMask().end());
Nate Begeman9008ca62009-04-27 18:41:29 +00006333
Evan Chengace3c172008-07-22 21:13:36 +00006334 unsigned NumHi = 0;
6335 unsigned NumLo = 0;
Evan Chengace3c172008-07-22 21:13:36 +00006336 for (unsigned i = 0; i != 4; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006337 int Idx = PermMask[i];
6338 if (Idx < 0) {
Evan Chengace3c172008-07-22 21:13:36 +00006339 Locs[i] = std::make_pair(-1, -1);
6340 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00006341 assert(Idx < 8 && "Invalid VECTOR_SHUFFLE index!");
6342 if (Idx < 4) {
Evan Chengace3c172008-07-22 21:13:36 +00006343 Locs[i] = std::make_pair(0, NumLo);
Nate Begeman9008ca62009-04-27 18:41:29 +00006344 Mask1[NumLo] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00006345 NumLo++;
6346 } else {
6347 Locs[i] = std::make_pair(1, NumHi);
6348 if (2+NumHi < 4)
Nate Begeman9008ca62009-04-27 18:41:29 +00006349 Mask1[2+NumHi] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00006350 NumHi++;
6351 }
6352 }
6353 }
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006354
Evan Chengace3c172008-07-22 21:13:36 +00006355 if (NumLo <= 2 && NumHi <= 2) {
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006356 // If no more than two elements come from either vector. This can be
6357 // implemented with two shuffles. First shuffle gather the elements.
6358 // The second shuffle, which takes the first shuffle as both of its
6359 // vector operands, put the elements into the right order.
Nate Begeman9008ca62009-04-27 18:41:29 +00006360 V1 = DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006361
Benjamin Kramer9c683542012-01-30 15:16:21 +00006362 int Mask2[] = { -1, -1, -1, -1 };
Eric Christopherfd179292009-08-27 18:07:15 +00006363
Benjamin Kramer9c683542012-01-30 15:16:21 +00006364 for (unsigned i = 0; i != 4; ++i)
6365 if (Locs[i].first != -1) {
Evan Chengace3c172008-07-22 21:13:36 +00006366 unsigned Idx = (i < 2) ? 0 : 4;
6367 Idx += Locs[i].first * 2 + Locs[i].second;
Nate Begeman9008ca62009-04-27 18:41:29 +00006368 Mask2[i] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00006369 }
Evan Chengace3c172008-07-22 21:13:36 +00006370
Nate Begeman9008ca62009-04-27 18:41:29 +00006371 return DAG.getVectorShuffle(VT, dl, V1, V1, &Mask2[0]);
Craig Topper69947b92012-04-23 06:57:04 +00006372 }
6373
6374 if (NumLo == 3 || NumHi == 3) {
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006375 // Otherwise, we must have three elements from one vector, call it X, and
6376 // one element from the other, call it Y. First, use a shufps to build an
6377 // intermediate vector with the one element from Y and the element from X
6378 // that will be in the same half in the final destination (the indexes don't
6379 // matter). Then, use a shufps to build the final vector, taking the half
6380 // containing the element from Y from the intermediate, and the other half
6381 // from X.
6382 if (NumHi == 3) {
6383 // Normalize it so the 3 elements come from V1.
Craig Topperbeabc6c2011-12-05 06:56:46 +00006384 CommuteVectorShuffleMask(PermMask, 4);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006385 std::swap(V1, V2);
6386 }
6387
6388 // Find the element from V2.
6389 unsigned HiIndex;
6390 for (HiIndex = 0; HiIndex < 3; ++HiIndex) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006391 int Val = PermMask[HiIndex];
6392 if (Val < 0)
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006393 continue;
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006394 if (Val >= 4)
6395 break;
6396 }
6397
Nate Begeman9008ca62009-04-27 18:41:29 +00006398 Mask1[0] = PermMask[HiIndex];
6399 Mask1[1] = -1;
6400 Mask1[2] = PermMask[HiIndex^1];
6401 Mask1[3] = -1;
6402 V2 = DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006403
6404 if (HiIndex >= 2) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006405 Mask1[0] = PermMask[0];
6406 Mask1[1] = PermMask[1];
6407 Mask1[2] = HiIndex & 1 ? 6 : 4;
6408 Mask1[3] = HiIndex & 1 ? 4 : 6;
6409 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006410 }
Craig Topper69947b92012-04-23 06:57:04 +00006411
6412 Mask1[0] = HiIndex & 1 ? 2 : 0;
6413 Mask1[1] = HiIndex & 1 ? 0 : 2;
6414 Mask1[2] = PermMask[2];
6415 Mask1[3] = PermMask[3];
6416 if (Mask1[2] >= 0)
6417 Mask1[2] += 4;
6418 if (Mask1[3] >= 0)
6419 Mask1[3] += 4;
6420 return DAG.getVectorShuffle(VT, dl, V2, V1, &Mask1[0]);
Evan Chengace3c172008-07-22 21:13:36 +00006421 }
6422
6423 // Break it into (shuffle shuffle_hi, shuffle_lo).
Benjamin Kramer9c683542012-01-30 15:16:21 +00006424 int LoMask[] = { -1, -1, -1, -1 };
6425 int HiMask[] = { -1, -1, -1, -1 };
Nate Begeman9008ca62009-04-27 18:41:29 +00006426
Benjamin Kramer9c683542012-01-30 15:16:21 +00006427 int *MaskPtr = LoMask;
Evan Chengace3c172008-07-22 21:13:36 +00006428 unsigned MaskIdx = 0;
6429 unsigned LoIdx = 0;
6430 unsigned HiIdx = 2;
6431 for (unsigned i = 0; i != 4; ++i) {
6432 if (i == 2) {
Benjamin Kramer9c683542012-01-30 15:16:21 +00006433 MaskPtr = HiMask;
Evan Chengace3c172008-07-22 21:13:36 +00006434 MaskIdx = 1;
6435 LoIdx = 0;
6436 HiIdx = 2;
6437 }
Nate Begeman9008ca62009-04-27 18:41:29 +00006438 int Idx = PermMask[i];
6439 if (Idx < 0) {
Evan Chengace3c172008-07-22 21:13:36 +00006440 Locs[i] = std::make_pair(-1, -1);
Nate Begeman9008ca62009-04-27 18:41:29 +00006441 } else if (Idx < 4) {
Evan Chengace3c172008-07-22 21:13:36 +00006442 Locs[i] = std::make_pair(MaskIdx, LoIdx);
Benjamin Kramer9c683542012-01-30 15:16:21 +00006443 MaskPtr[LoIdx] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00006444 LoIdx++;
6445 } else {
6446 Locs[i] = std::make_pair(MaskIdx, HiIdx);
Benjamin Kramer9c683542012-01-30 15:16:21 +00006447 MaskPtr[HiIdx] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00006448 HiIdx++;
6449 }
6450 }
6451
Nate Begeman9008ca62009-04-27 18:41:29 +00006452 SDValue LoShuffle = DAG.getVectorShuffle(VT, dl, V1, V2, &LoMask[0]);
6453 SDValue HiShuffle = DAG.getVectorShuffle(VT, dl, V1, V2, &HiMask[0]);
Benjamin Kramer9c683542012-01-30 15:16:21 +00006454 int MaskOps[] = { -1, -1, -1, -1 };
6455 for (unsigned i = 0; i != 4; ++i)
6456 if (Locs[i].first != -1)
6457 MaskOps[i] = Locs[i].first * 4 + Locs[i].second;
Nate Begeman9008ca62009-04-27 18:41:29 +00006458 return DAG.getVectorShuffle(VT, dl, LoShuffle, HiShuffle, &MaskOps[0]);
Evan Chengace3c172008-07-22 21:13:36 +00006459}
6460
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00006461static bool MayFoldVectorLoad(SDValue V) {
Jakub Staszaka24262a2012-10-30 00:01:57 +00006462 while (V.hasOneUse() && V.getOpcode() == ISD::BITCAST)
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00006463 V = V.getOperand(0);
Jakub Staszaka24262a2012-10-30 00:01:57 +00006464
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00006465 if (V.hasOneUse() && V.getOpcode() == ISD::SCALAR_TO_VECTOR)
6466 V = V.getOperand(0);
Evan Cheng7bc389b2011-11-08 00:31:58 +00006467 if (V.hasOneUse() && V.getOpcode() == ISD::BUILD_VECTOR &&
6468 V.getNumOperands() == 2 && V.getOperand(1).getOpcode() == ISD::UNDEF)
6469 // BUILD_VECTOR (load), undef
6470 V = V.getOperand(0);
Jakub Staszaka24262a2012-10-30 00:01:57 +00006471
6472 return MayFoldLoad(V);
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00006473}
6474
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006475static
Evan Cheng835580f2010-10-07 20:50:20 +00006476SDValue getMOVDDup(SDValue &Op, DebugLoc &dl, SDValue V1, SelectionDAG &DAG) {
6477 EVT VT = Op.getValueType();
6478
6479 // Canonizalize to v2f64.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006480 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, V1);
6481 return DAG.getNode(ISD::BITCAST, dl, VT,
Evan Cheng835580f2010-10-07 20:50:20 +00006482 getTargetShuffleNode(X86ISD::MOVDDUP, dl, MVT::v2f64,
6483 V1, DAG));
6484}
6485
6486static
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006487SDValue getMOVLowToHigh(SDValue &Op, DebugLoc &dl, SelectionDAG &DAG,
Craig Topper1accb7e2012-01-10 06:54:16 +00006488 bool HasSSE2) {
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006489 SDValue V1 = Op.getOperand(0);
6490 SDValue V2 = Op.getOperand(1);
6491 EVT VT = Op.getValueType();
6492
6493 assert(VT != MVT::v2i64 && "unsupported shuffle type");
6494
Craig Topper1accb7e2012-01-10 06:54:16 +00006495 if (HasSSE2 && VT == MVT::v2f64)
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006496 return getTargetShuffleNode(X86ISD::MOVLHPD, dl, VT, V1, V2, DAG);
6497
Evan Cheng0899f5c2011-08-31 02:05:24 +00006498 // v4f32 or v4i32: canonizalized to v4f32 (which is legal for SSE1)
6499 return DAG.getNode(ISD::BITCAST, dl, VT,
6500 getTargetShuffleNode(X86ISD::MOVLHPS, dl, MVT::v4f32,
6501 DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, V1),
6502 DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, V2), DAG));
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006503}
6504
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00006505static
6506SDValue getMOVHighToLow(SDValue &Op, DebugLoc &dl, SelectionDAG &DAG) {
6507 SDValue V1 = Op.getOperand(0);
6508 SDValue V2 = Op.getOperand(1);
6509 EVT VT = Op.getValueType();
6510
6511 assert((VT == MVT::v4i32 || VT == MVT::v4f32) &&
6512 "unsupported shuffle type");
6513
6514 if (V2.getOpcode() == ISD::UNDEF)
6515 V2 = V1;
6516
6517 // v4i32 or v4f32
6518 return getTargetShuffleNode(X86ISD::MOVHLPS, dl, VT, V1, V2, DAG);
6519}
6520
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006521static
Craig Topper1accb7e2012-01-10 06:54:16 +00006522SDValue getMOVLP(SDValue &Op, DebugLoc &dl, SelectionDAG &DAG, bool HasSSE2) {
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006523 SDValue V1 = Op.getOperand(0);
6524 SDValue V2 = Op.getOperand(1);
6525 EVT VT = Op.getValueType();
6526 unsigned NumElems = VT.getVectorNumElements();
6527
6528 // Use MOVLPS and MOVLPD in case V1 or V2 are loads. During isel, the second
6529 // operand of these instructions is only memory, so check if there's a
6530 // potencial load folding here, otherwise use SHUFPS or MOVSD to match the
6531 // same masks.
6532 bool CanFoldLoad = false;
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006533
Bruno Cardoso Lopesd00bfe12010-09-02 02:35:51 +00006534 // Trivial case, when V2 comes from a load.
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00006535 if (MayFoldVectorLoad(V2))
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006536 CanFoldLoad = true;
6537
6538 // When V1 is a load, it can be folded later into a store in isel, example:
6539 // (store (v4f32 (X86Movlps (load addr:$src1), VR128:$src2)), addr:$src1)
6540 // turns into:
6541 // (MOVLPSmr addr:$src1, VR128:$src2)
6542 // So, recognize this potential and also use MOVLPS or MOVLPD
Evan Cheng7bc389b2011-11-08 00:31:58 +00006543 else if (MayFoldVectorLoad(V1) && MayFoldIntoStore(Op))
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006544 CanFoldLoad = true;
6545
Dan Gohman65fd6562011-11-03 21:49:52 +00006546 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006547 if (CanFoldLoad) {
Craig Topper1accb7e2012-01-10 06:54:16 +00006548 if (HasSSE2 && NumElems == 2)
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006549 return getTargetShuffleNode(X86ISD::MOVLPD, dl, VT, V1, V2, DAG);
6550
6551 if (NumElems == 4)
Benjamin Kramerd9b0b022012-06-02 10:20:22 +00006552 // If we don't care about the second element, proceed to use movss.
Dan Gohman65fd6562011-11-03 21:49:52 +00006553 if (SVOp->getMaskElt(1) != -1)
6554 return getTargetShuffleNode(X86ISD::MOVLPS, dl, VT, V1, V2, DAG);
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006555 }
6556
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006557 // movl and movlp will both match v2i64, but v2i64 is never matched by
6558 // movl earlier because we make it strict to avoid messing with the movlp load
6559 // folding logic (see the code above getMOVLP call). Match it here then,
6560 // this is horrible, but will stay like this until we move all shuffle
6561 // matching to x86 specific nodes. Note that for the 1st condition all
6562 // types are matched with movsd.
Craig Topper1accb7e2012-01-10 06:54:16 +00006563 if (HasSSE2) {
Bruno Cardoso Lopes5ca0d142011-09-14 02:36:14 +00006564 // FIXME: isMOVLMask should be checked and matched before getMOVLP,
6565 // as to remove this logic from here, as much as possible
Craig Topper5aaffa82012-02-19 02:53:47 +00006566 if (NumElems == 2 || !isMOVLMask(SVOp->getMask(), VT))
Bruno Cardoso Lopes57d6a5e2011-08-31 03:04:20 +00006567 return getTargetShuffleNode(X86ISD::MOVSD, dl, VT, V1, V2, DAG);
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006568 return getTargetShuffleNode(X86ISD::MOVSS, dl, VT, V1, V2, DAG);
Bruno Cardoso Lopes57d6a5e2011-08-31 03:04:20 +00006569 }
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006570
6571 assert(VT != MVT::v4i32 && "unsupported shuffle type");
6572
6573 // Invert the operand order and use SHUFPS to match it.
Craig Topperb3982da2011-12-31 23:50:21 +00006574 return getTargetShuffleNode(X86ISD::SHUFP, dl, VT, V2, V1,
Craig Topper5aaffa82012-02-19 02:53:47 +00006575 getShuffleSHUFImmediate(SVOp), DAG);
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006576}
6577
Michael Liaod9d09602012-10-23 17:34:00 +00006578// Reduce a vector shuffle to zext.
6579SDValue
6580X86TargetLowering::lowerVectorIntExtend(SDValue Op, SelectionDAG &DAG) const {
6581 // PMOVZX is only available from SSE41.
6582 if (!Subtarget->hasSSE41())
6583 return SDValue();
6584
6585 EVT VT = Op.getValueType();
6586
6587 // Only AVX2 support 256-bit vector integer extending.
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00006588 if (!Subtarget->hasInt256() && VT.is256BitVector())
Michael Liaod9d09602012-10-23 17:34:00 +00006589 return SDValue();
6590
6591 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
6592 DebugLoc DL = Op.getDebugLoc();
6593 SDValue V1 = Op.getOperand(0);
6594 SDValue V2 = Op.getOperand(1);
6595 unsigned NumElems = VT.getVectorNumElements();
6596
6597 // Extending is an unary operation and the element type of the source vector
6598 // won't be equal to or larger than i64.
6599 if (V2.getOpcode() != ISD::UNDEF || !VT.isInteger() ||
6600 VT.getVectorElementType() == MVT::i64)
6601 return SDValue();
6602
6603 // Find the expansion ratio, e.g. expanding from i8 to i32 has a ratio of 4.
6604 unsigned Shift = 1; // Start from 2, i.e. 1 << 1.
Duncan Sands34739052012-10-29 11:29:53 +00006605 while ((1U << Shift) < NumElems) {
6606 if (SVOp->getMaskElt(1U << Shift) == 1)
Michael Liaod9d09602012-10-23 17:34:00 +00006607 break;
6608 Shift += 1;
6609 // The maximal ratio is 8, i.e. from i8 to i64.
6610 if (Shift > 3)
6611 return SDValue();
6612 }
6613
6614 // Check the shuffle mask.
6615 unsigned Mask = (1U << Shift) - 1;
6616 for (unsigned i = 0; i != NumElems; ++i) {
6617 int EltIdx = SVOp->getMaskElt(i);
6618 if ((i & Mask) != 0 && EltIdx != -1)
6619 return SDValue();
Matt Beaumont-Gaya999de02012-10-23 19:46:36 +00006620 if ((i & Mask) == 0 && (unsigned)EltIdx != (i >> Shift))
Michael Liaod9d09602012-10-23 17:34:00 +00006621 return SDValue();
6622 }
6623
6624 unsigned NBits = VT.getVectorElementType().getSizeInBits() << Shift;
6625 EVT NeVT = EVT::getIntegerVT(*DAG.getContext(), NBits);
6626 EVT NVT = EVT::getVectorVT(*DAG.getContext(), NeVT, NumElems >> Shift);
6627
6628 if (!isTypeLegal(NVT))
6629 return SDValue();
6630
6631 // Simplify the operand as it's prepared to be fed into shuffle.
6632 unsigned SignificantBits = NVT.getSizeInBits() >> Shift;
6633 if (V1.getOpcode() == ISD::BITCAST &&
6634 V1.getOperand(0).getOpcode() == ISD::SCALAR_TO_VECTOR &&
6635 V1.getOperand(0).getOperand(0).getOpcode() == ISD::EXTRACT_VECTOR_ELT &&
6636 V1.getOperand(0)
6637 .getOperand(0).getValueType().getSizeInBits() == SignificantBits) {
6638 // (bitcast (sclr2vec (ext_vec_elt x))) -> (bitcast x)
6639 SDValue V = V1.getOperand(0).getOperand(0).getOperand(0);
Michael Liao07872742012-10-23 21:40:15 +00006640 ConstantSDNode *CIdx =
6641 dyn_cast<ConstantSDNode>(V1.getOperand(0).getOperand(0).getOperand(1));
Michael Liaod9d09602012-10-23 17:34:00 +00006642 // If it's foldable, i.e. normal load with single use, we will let code
6643 // selection to fold it. Otherwise, we will short the conversion sequence.
Michael Liao07872742012-10-23 21:40:15 +00006644 if (CIdx && CIdx->getZExtValue() == 0 &&
6645 (!ISD::isNormalLoad(V.getNode()) || !V.hasOneUse()))
Michael Liaod9d09602012-10-23 17:34:00 +00006646 V1 = DAG.getNode(ISD::BITCAST, DL, V1.getValueType(), V);
6647 }
6648
6649 return DAG.getNode(ISD::BITCAST, DL, VT,
6650 DAG.getNode(X86ISD::VZEXT, DL, NVT, V1));
6651}
6652
Nadav Rotem154819d2012-04-09 07:45:58 +00006653SDValue
6654X86TargetLowering::NormalizeVectorShuffle(SDValue Op, SelectionDAG &DAG) const {
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006655 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
6656 EVT VT = Op.getValueType();
6657 DebugLoc dl = Op.getDebugLoc();
6658 SDValue V1 = Op.getOperand(0);
6659 SDValue V2 = Op.getOperand(1);
6660
6661 if (isZeroShuffle(SVOp))
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00006662 return getZeroVector(VT, Subtarget, DAG, dl);
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006663
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006664 // Handle splat operations
6665 if (SVOp->isSplat()) {
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00006666 unsigned NumElem = VT.getVectorNumElements();
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00006667 int Size = VT.getSizeInBits();
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006668
Bruno Cardoso Lopes0e6d2302011-08-17 02:29:19 +00006669 // Use vbroadcast whenever the splat comes from a foldable load
Nadav Rotem154819d2012-04-09 07:45:58 +00006670 SDValue Broadcast = LowerVectorBroadcast(Op, DAG);
Nadav Rotem9d68b062012-04-08 12:54:54 +00006671 if (Broadcast.getNode())
6672 return Broadcast;
Bruno Cardoso Lopes0e6d2302011-08-17 02:29:19 +00006673
Bruno Cardoso Lopes6a32adc2011-07-25 23:05:25 +00006674 // Handle splats by matching through known shuffle masks
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00006675 if ((Size == 128 && NumElem <= 4) ||
Jakub Staszakd642baf2012-11-26 19:24:31 +00006676 (Size == 256 && NumElem <= 8))
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006677 return SDValue();
6678
Bruno Cardoso Lopes8a5b2622011-08-17 02:29:13 +00006679 // All remaning splats are promoted to target supported vector shuffles.
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006680 return PromoteSplat(SVOp, DAG);
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006681 }
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006682
Michael Liaod9d09602012-10-23 17:34:00 +00006683 // Check integer expanding shuffles.
6684 SDValue NewOp = lowerVectorIntExtend(Op, DAG);
6685 if (NewOp.getNode())
6686 return NewOp;
6687
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006688 // If the shuffle can be profitably rewritten as a narrower shuffle, then
6689 // do it!
Craig Topperf3640d72012-05-04 04:44:49 +00006690 if (VT == MVT::v8i16 || VT == MVT::v16i8 ||
6691 VT == MVT::v16i16 || VT == MVT::v32i8) {
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006692 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, dl);
6693 if (NewOp.getNode())
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006694 return DAG.getNode(ISD::BITCAST, dl, VT, NewOp);
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006695 } else if ((VT == MVT::v4i32 ||
Craig Topper1accb7e2012-01-10 06:54:16 +00006696 (VT == MVT::v4f32 && Subtarget->hasSSE2()))) {
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006697 // FIXME: Figure out a cleaner way to do this.
6698 // Try to make use of movq to zero out the top part.
6699 if (ISD::isBuildVectorAllZeros(V2.getNode())) {
6700 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, dl);
6701 if (NewOp.getNode()) {
Craig Topper5aaffa82012-02-19 02:53:47 +00006702 EVT NewVT = NewOp.getValueType();
6703 if (isCommutedMOVLMask(cast<ShuffleVectorSDNode>(NewOp)->getMask(),
6704 NewVT, true, false))
6705 return getVZextMovL(VT, NewVT, NewOp.getOperand(0),
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006706 DAG, Subtarget, dl);
6707 }
6708 } else if (ISD::isBuildVectorAllZeros(V1.getNode())) {
6709 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, dl);
Craig Topper5aaffa82012-02-19 02:53:47 +00006710 if (NewOp.getNode()) {
6711 EVT NewVT = NewOp.getValueType();
6712 if (isMOVLMask(cast<ShuffleVectorSDNode>(NewOp)->getMask(), NewVT))
6713 return getVZextMovL(VT, NewVT, NewOp.getOperand(1),
6714 DAG, Subtarget, dl);
6715 }
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006716 }
6717 }
6718 return SDValue();
6719}
6720
Dan Gohman475871a2008-07-27 21:46:04 +00006721SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006722X86TargetLowering::LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) const {
Nate Begeman9008ca62009-04-27 18:41:29 +00006723 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Dan Gohman475871a2008-07-27 21:46:04 +00006724 SDValue V1 = Op.getOperand(0);
6725 SDValue V2 = Op.getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +00006726 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006727 DebugLoc dl = Op.getDebugLoc();
Nate Begeman9008ca62009-04-27 18:41:29 +00006728 unsigned NumElems = VT.getVectorNumElements();
Elena Demikhovsky16db7102012-01-12 20:33:10 +00006729 bool V1IsUndef = V1.getOpcode() == ISD::UNDEF;
Evan Cheng0db9fe62006-04-25 20:13:52 +00006730 bool V2IsUndef = V2.getOpcode() == ISD::UNDEF;
Evan Chengd9b8e402006-10-16 06:36:00 +00006731 bool V1IsSplat = false;
6732 bool V2IsSplat = false;
Craig Topper1accb7e2012-01-10 06:54:16 +00006733 bool HasSSE2 = Subtarget->hasSSE2();
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00006734 bool HasFp256 = Subtarget->hasFp256();
6735 bool HasInt256 = Subtarget->hasInt256();
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006736 MachineFunction &MF = DAG.getMachineFunction();
Bill Wendling67658342012-10-09 07:45:08 +00006737 bool OptForSize = MF.getFunction()->getFnAttributes().
Bill Wendling034b94b2012-12-19 07:18:57 +00006738 hasAttribute(Attribute::OptimizeForSize);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006739
Craig Topper3426a3e2011-11-14 06:46:21 +00006740 assert(VT.getSizeInBits() != 64 && "Can't lower MMX shuffles");
Bruno Cardoso Lopes58277b12010-09-07 18:41:45 +00006741
Elena Demikhovsky16db7102012-01-12 20:33:10 +00006742 if (V1IsUndef && V2IsUndef)
6743 return DAG.getUNDEF(VT);
6744
6745 assert(!V1IsUndef && "Op 1 of shuffle should not be undef");
Craig Topper38034c52011-11-26 22:55:48 +00006746
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006747 // Vector shuffle lowering takes 3 steps:
6748 //
6749 // 1) Normalize the input vectors. Here splats, zeroed vectors, profitable
6750 // narrowing and commutation of operands should be handled.
6751 // 2) Matching of shuffles with known shuffle masks to x86 target specific
6752 // shuffle nodes.
6753 // 3) Rewriting of unmatched masks into new generic shuffle operations,
6754 // so the shuffle can be broken into other shuffles and the legalizer can
6755 // try the lowering again.
6756 //
Craig Topper3426a3e2011-11-14 06:46:21 +00006757 // The general idea is that no vector_shuffle operation should be left to
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006758 // be matched during isel, all of them must be converted to a target specific
6759 // node here.
Bruno Cardoso Lopes0d1340b2010-09-07 20:20:27 +00006760
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006761 // Normalize the input vectors. Here splats, zeroed vectors, profitable
6762 // narrowing and commutation of operands should be handled. The actual code
6763 // doesn't include all of those, work in progress...
Nadav Rotem154819d2012-04-09 07:45:58 +00006764 SDValue NewOp = NormalizeVectorShuffle(Op, DAG);
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006765 if (NewOp.getNode())
6766 return NewOp;
Eric Christopherfd179292009-08-27 18:07:15 +00006767
Craig Topper5aaffa82012-02-19 02:53:47 +00006768 SmallVector<int, 8> M(SVOp->getMask().begin(), SVOp->getMask().end());
6769
Bruno Cardoso Lopesa22c8452010-09-04 00:39:43 +00006770 // NOTE: isPSHUFDMask can also match both masks below (unpckl_undef and
6771 // unpckh_undef). Only use pshufd if speed is more important than size.
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00006772 if (OptForSize && isUNPCKL_v_undef_Mask(M, VT, HasInt256))
Craig Topper34671b82011-12-06 08:21:25 +00006773 return getTargetShuffleNode(X86ISD::UNPCKL, dl, VT, V1, V1, DAG);
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00006774 if (OptForSize && isUNPCKH_v_undef_Mask(M, VT, HasInt256))
Craig Topper34671b82011-12-06 08:21:25 +00006775 return getTargetShuffleNode(X86ISD::UNPCKH, dl, VT, V1, V1, DAG);
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00006776
Craig Topperdd637ae2012-02-19 05:41:45 +00006777 if (isMOVDDUPMask(M, VT) && Subtarget->hasSSE3() &&
Jakub Staszakd3a05632012-12-06 19:05:46 +00006778 V2IsUndef && MayFoldVectorLoad(V1))
Evan Cheng835580f2010-10-07 20:50:20 +00006779 return getMOVDDup(Op, dl, V1, DAG);
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006780
Craig Topperdd637ae2012-02-19 05:41:45 +00006781 if (isMOVHLPS_v_undef_Mask(M, VT))
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006782 return getMOVHighToLow(Op, dl, DAG);
6783
6784 // Use to match splats
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00006785 if (HasSSE2 && isUNPCKHMask(M, VT, HasInt256) && V2IsUndef &&
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006786 (VT == MVT::v2f64 || VT == MVT::v2i64))
Craig Topper34671b82011-12-06 08:21:25 +00006787 return getTargetShuffleNode(X86ISD::UNPCKH, dl, VT, V1, V1, DAG);
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006788
Craig Topper5aaffa82012-02-19 02:53:47 +00006789 if (isPSHUFDMask(M, VT)) {
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006790 // The actual implementation will match the mask in the if above and then
6791 // during isel it can match several different instructions, not only pshufd
6792 // as its name says, sad but true, emulate the behavior for now...
Craig Topperdd637ae2012-02-19 05:41:45 +00006793 if (isMOVDDUPMask(M, VT) && ((VT == MVT::v4f32 || VT == MVT::v2i64)))
6794 return getTargetShuffleNode(X86ISD::MOVLHPS, dl, VT, V1, V1, DAG);
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006795
Craig Topper5aaffa82012-02-19 02:53:47 +00006796 unsigned TargetMask = getShuffleSHUFImmediate(SVOp);
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006797
Craig Topper1accb7e2012-01-10 06:54:16 +00006798 if (HasSSE2 && (VT == MVT::v4f32 || VT == MVT::v4i32))
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006799 return getTargetShuffleNode(X86ISD::PSHUFD, dl, VT, V1, TargetMask, DAG);
6800
Nadav Roteme4ccfef2012-12-07 19:01:13 +00006801 if (HasFp256 && (VT == MVT::v4f32 || VT == MVT::v2f64))
6802 return getTargetShuffleNode(X86ISD::VPERMILP, dl, VT, V1, TargetMask,
6803 DAG);
6804
Craig Topperb3982da2011-12-31 23:50:21 +00006805 return getTargetShuffleNode(X86ISD::SHUFP, dl, VT, V1, V1,
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00006806 TargetMask, DAG);
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006807 }
Eric Christopherfd179292009-08-27 18:07:15 +00006808
Evan Chengf26ffe92008-05-29 08:22:04 +00006809 // Check if this can be converted into a logical shift.
6810 bool isLeft = false;
6811 unsigned ShAmt = 0;
Dan Gohman475871a2008-07-27 21:46:04 +00006812 SDValue ShVal;
Craig Topper1accb7e2012-01-10 06:54:16 +00006813 bool isShift = HasSSE2 && isVectorShift(SVOp, DAG, isLeft, ShVal, ShAmt);
Evan Chengf26ffe92008-05-29 08:22:04 +00006814 if (isShift && ShVal.hasOneUse()) {
Scott Michelfdc40a02009-02-17 22:15:04 +00006815 // If the shifted value has multiple uses, it may be cheaper to use
Evan Chengf26ffe92008-05-29 08:22:04 +00006816 // v_set0 + movlhps or movhlps, etc.
Dan Gohman8a55ce42009-09-23 21:02:20 +00006817 EVT EltVT = VT.getVectorElementType();
6818 ShAmt *= EltVT.getSizeInBits();
Dale Johannesenace16102009-02-03 19:33:06 +00006819 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00006820 }
Eric Christopherfd179292009-08-27 18:07:15 +00006821
Craig Topper5aaffa82012-02-19 02:53:47 +00006822 if (isMOVLMask(M, VT)) {
Gabor Greifba36cb52008-08-28 21:40:38 +00006823 if (ISD::isBuildVectorAllZeros(V1.getNode()))
Dale Johannesenace16102009-02-03 19:33:06 +00006824 return getVZextMovL(VT, VT, V2, DAG, Subtarget, dl);
Craig Topperdd637ae2012-02-19 05:41:45 +00006825 if (!isMOVLPMask(M, VT)) {
Craig Topper1accb7e2012-01-10 06:54:16 +00006826 if (HasSSE2 && (VT == MVT::v2i64 || VT == MVT::v2f64))
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00006827 return getTargetShuffleNode(X86ISD::MOVSD, dl, VT, V1, V2, DAG);
6828
Bruno Cardoso Lopes4783a3e2010-09-01 22:59:03 +00006829 if (VT == MVT::v4i32 || VT == MVT::v4f32)
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00006830 return getTargetShuffleNode(X86ISD::MOVSS, dl, VT, V1, V2, DAG);
6831 }
Evan Cheng7e2ff772008-05-08 00:57:18 +00006832 }
Eric Christopherfd179292009-08-27 18:07:15 +00006833
Nate Begeman9008ca62009-04-27 18:41:29 +00006834 // FIXME: fold these into legal mask.
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00006835 if (isMOVLHPSMask(M, VT) && !isUNPCKLMask(M, VT, HasInt256))
Craig Topper1accb7e2012-01-10 06:54:16 +00006836 return getMOVLowToHigh(Op, dl, DAG, HasSSE2);
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006837
Craig Topperdd637ae2012-02-19 05:41:45 +00006838 if (isMOVHLPSMask(M, VT))
Dale Johannesen0488fb62010-09-30 23:57:10 +00006839 return getMOVHighToLow(Op, dl, DAG);
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00006840
Craig Topperdd637ae2012-02-19 05:41:45 +00006841 if (V2IsUndef && isMOVSHDUPMask(M, VT, Subtarget))
Dale Johannesen0488fb62010-09-30 23:57:10 +00006842 return getTargetShuffleNode(X86ISD::MOVSHDUP, dl, VT, V1, DAG);
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00006843
Craig Topperdd637ae2012-02-19 05:41:45 +00006844 if (V2IsUndef && isMOVSLDUPMask(M, VT, Subtarget))
Dale Johannesen0488fb62010-09-30 23:57:10 +00006845 return getTargetShuffleNode(X86ISD::MOVSLDUP, dl, VT, V1, DAG);
Bruno Cardoso Lopes013bb3d2010-08-31 22:35:05 +00006846
Craig Topperdd637ae2012-02-19 05:41:45 +00006847 if (isMOVLPMask(M, VT))
Craig Topper1accb7e2012-01-10 06:54:16 +00006848 return getMOVLP(Op, dl, DAG, HasSSE2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006849
Craig Topperdd637ae2012-02-19 05:41:45 +00006850 if (ShouldXformToMOVHLPS(M, VT) ||
6851 ShouldXformToMOVLP(V1.getNode(), V2.getNode(), M, VT))
Nate Begeman9008ca62009-04-27 18:41:29 +00006852 return CommuteVectorShuffle(SVOp, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006853
Evan Chengf26ffe92008-05-29 08:22:04 +00006854 if (isShift) {
Craig Toppered2e13d2012-01-22 19:15:14 +00006855 // No better options. Use a vshldq / vsrldq.
Dan Gohman8a55ce42009-09-23 21:02:20 +00006856 EVT EltVT = VT.getVectorElementType();
6857 ShAmt *= EltVT.getSizeInBits();
Dale Johannesenace16102009-02-03 19:33:06 +00006858 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00006859 }
Eric Christopherfd179292009-08-27 18:07:15 +00006860
Evan Cheng9eca5e82006-10-25 21:49:50 +00006861 bool Commuted = false;
Chris Lattner8a594482007-11-25 00:24:49 +00006862 // FIXME: This should also accept a bitcast of a splat? Be careful, not
6863 // 1,1,1,1 -> v8i16 though.
Gabor Greifba36cb52008-08-28 21:40:38 +00006864 V1IsSplat = isSplatVector(V1.getNode());
6865 V2IsSplat = isSplatVector(V2.getNode());
Scott Michelfdc40a02009-02-17 22:15:04 +00006866
Chris Lattner8a594482007-11-25 00:24:49 +00006867 // Canonicalize the splat or undef, if present, to be on the RHS.
Craig Topper39a9e482012-02-11 06:24:48 +00006868 if (!V2IsUndef && V1IsSplat && !V2IsSplat) {
6869 CommuteVectorShuffleMask(M, NumElems);
6870 std::swap(V1, V2);
Evan Cheng9bbbb982006-10-25 20:48:19 +00006871 std::swap(V1IsSplat, V2IsSplat);
Evan Cheng9eca5e82006-10-25 21:49:50 +00006872 Commuted = true;
Evan Cheng9bbbb982006-10-25 20:48:19 +00006873 }
6874
Craig Topperbeabc6c2011-12-05 06:56:46 +00006875 if (isCommutedMOVLMask(M, VT, V2IsSplat, V2IsUndef)) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006876 // Shuffling low element of v1 into undef, just return v1.
Eric Christopherfd179292009-08-27 18:07:15 +00006877 if (V2IsUndef)
Nate Begeman9008ca62009-04-27 18:41:29 +00006878 return V1;
6879 // If V2 is a splat, the mask may be malformed such as <4,3,3,3>, which
6880 // the instruction selector will not match, so get a canonical MOVL with
6881 // swapped operands to undo the commute.
6882 return getMOVL(DAG, dl, VT, V2, V1);
Evan Chengd9b8e402006-10-16 06:36:00 +00006883 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00006884
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00006885 if (isUNPCKLMask(M, VT, HasInt256))
Craig Topper34671b82011-12-06 08:21:25 +00006886 return getTargetShuffleNode(X86ISD::UNPCKL, dl, VT, V1, V2, DAG);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00006887
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00006888 if (isUNPCKHMask(M, VT, HasInt256))
Craig Topper34671b82011-12-06 08:21:25 +00006889 return getTargetShuffleNode(X86ISD::UNPCKH, dl, VT, V1, V2, DAG);
Evan Chenge1113032006-10-04 18:33:38 +00006890
Evan Cheng9bbbb982006-10-25 20:48:19 +00006891 if (V2IsSplat) {
6892 // Normalize mask so all entries that point to V2 points to its first
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00006893 // element then try to match unpck{h|l} again. If match, return a
Craig Topper39a9e482012-02-11 06:24:48 +00006894 // new vector_shuffle with the corrected mask.p
6895 SmallVector<int, 8> NewMask(M.begin(), M.end());
6896 NormalizeMask(NewMask, NumElems);
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00006897 if (isUNPCKLMask(NewMask, VT, HasInt256, true))
Craig Topper39a9e482012-02-11 06:24:48 +00006898 return getTargetShuffleNode(X86ISD::UNPCKL, dl, VT, V1, V2, DAG);
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00006899 if (isUNPCKHMask(NewMask, VT, HasInt256, true))
Craig Topper39a9e482012-02-11 06:24:48 +00006900 return getTargetShuffleNode(X86ISD::UNPCKH, dl, VT, V1, V2, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006901 }
6902
Evan Cheng9eca5e82006-10-25 21:49:50 +00006903 if (Commuted) {
6904 // Commute is back and try unpck* again.
Nate Begeman9008ca62009-04-27 18:41:29 +00006905 // FIXME: this seems wrong.
Craig Topper39a9e482012-02-11 06:24:48 +00006906 CommuteVectorShuffleMask(M, NumElems);
6907 std::swap(V1, V2);
6908 std::swap(V1IsSplat, V2IsSplat);
6909 Commuted = false;
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00006910
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00006911 if (isUNPCKLMask(M, VT, HasInt256))
Craig Topper39a9e482012-02-11 06:24:48 +00006912 return getTargetShuffleNode(X86ISD::UNPCKL, dl, VT, V1, V2, DAG);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00006913
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00006914 if (isUNPCKHMask(M, VT, HasInt256))
Craig Topper39a9e482012-02-11 06:24:48 +00006915 return getTargetShuffleNode(X86ISD::UNPCKH, dl, VT, V1, V2, DAG);
Evan Cheng9eca5e82006-10-25 21:49:50 +00006916 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00006917
Nate Begeman9008ca62009-04-27 18:41:29 +00006918 // Normalize the node to match x86 shuffle ops if needed
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00006919 if (!V2IsUndef && (isSHUFPMask(M, VT, HasFp256, /* Commuted */ true)))
Nate Begeman9008ca62009-04-27 18:41:29 +00006920 return CommuteVectorShuffle(SVOp, DAG);
6921
Bruno Cardoso Lopes7256e222010-09-03 23:24:06 +00006922 // The checks below are all present in isShuffleMaskLegal, but they are
6923 // inlined here right now to enable us to directly emit target specific
6924 // nodes, and remove one by one until they don't return Op anymore.
Bruno Cardoso Lopes7256e222010-09-03 23:24:06 +00006925
Craig Topper0e2037b2012-01-20 05:53:00 +00006926 if (isPALIGNRMask(M, VT, Subtarget))
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00006927 return getTargetShuffleNode(X86ISD::PALIGN, dl, VT, V1, V2,
Craig Topperd93e4c32011-12-11 19:12:35 +00006928 getShufflePALIGNRImmediate(SVOp),
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00006929 DAG);
6930
Bruno Cardoso Lopesc800c0d2010-09-04 02:02:14 +00006931 if (ShuffleVectorSDNode::isSplatMask(&M[0], VT) &&
6932 SVOp->getSplatIndex() == 0 && V2IsUndef) {
Craig Topperbeabc6c2011-12-05 06:56:46 +00006933 if (VT == MVT::v2f64 || VT == MVT::v2i64)
Craig Topper34671b82011-12-06 08:21:25 +00006934 return getTargetShuffleNode(X86ISD::UNPCKL, dl, VT, V1, V1, DAG);
Bruno Cardoso Lopesc800c0d2010-09-04 02:02:14 +00006935 }
6936
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00006937 if (isPSHUFHWMask(M, VT, HasInt256))
Bruno Cardoso Lopesbbfc3102010-09-04 01:36:45 +00006938 return getTargetShuffleNode(X86ISD::PSHUFHW, dl, VT, V1,
Craig Topper5aaffa82012-02-19 02:53:47 +00006939 getShufflePSHUFHWImmediate(SVOp),
Bruno Cardoso Lopesbbfc3102010-09-04 01:36:45 +00006940 DAG);
6941
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00006942 if (isPSHUFLWMask(M, VT, HasInt256))
Bruno Cardoso Lopesbbfc3102010-09-04 01:36:45 +00006943 return getTargetShuffleNode(X86ISD::PSHUFLW, dl, VT, V1,
Craig Topper5aaffa82012-02-19 02:53:47 +00006944 getShufflePSHUFLWImmediate(SVOp),
Bruno Cardoso Lopesbbfc3102010-09-04 01:36:45 +00006945 DAG);
6946
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00006947 if (isSHUFPMask(M, VT, HasFp256))
Craig Topperb3982da2011-12-31 23:50:21 +00006948 return getTargetShuffleNode(X86ISD::SHUFP, dl, VT, V1, V2,
Craig Topper5aaffa82012-02-19 02:53:47 +00006949 getShuffleSHUFImmediate(SVOp), DAG);
Bruno Cardoso Lopes4c827f52010-09-04 01:22:57 +00006950
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00006951 if (isUNPCKL_v_undef_Mask(M, VT, HasInt256))
Craig Topper34671b82011-12-06 08:21:25 +00006952 return getTargetShuffleNode(X86ISD::UNPCKL, dl, VT, V1, V1, DAG);
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00006953 if (isUNPCKH_v_undef_Mask(M, VT, HasInt256))
Craig Topper34671b82011-12-06 08:21:25 +00006954 return getTargetShuffleNode(X86ISD::UNPCKH, dl, VT, V1, V1, DAG);
Bruno Cardoso Lopesa22c8452010-09-04 00:39:43 +00006955
Bruno Cardoso Lopesd0888342011-07-22 00:14:56 +00006956 //===--------------------------------------------------------------------===//
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00006957 // Generate target specific nodes for 128 or 256-bit shuffles only
6958 // supported in the AVX instruction set.
6959 //
Bruno Cardoso Lopesd0888342011-07-22 00:14:56 +00006960
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00006961 // Handle VMOVDDUPY permutations
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00006962 if (V2IsUndef && isMOVDDUPYMask(M, VT, HasFp256))
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00006963 return getTargetShuffleNode(X86ISD::MOVDDUP, dl, VT, V1, DAG);
6964
Craig Topper70b883b2011-11-28 10:14:51 +00006965 // Handle VPERMILPS/D* permutations
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00006966 if (isVPERMILPMask(M, VT, HasFp256)) {
6967 if (HasInt256 && VT == MVT::v8i32)
Craig Topperdbd98a42012-02-07 06:28:42 +00006968 return getTargetShuffleNode(X86ISD::PSHUFD, dl, VT, V1,
Craig Topper5aaffa82012-02-19 02:53:47 +00006969 getShuffleSHUFImmediate(SVOp), DAG);
Craig Topper316cd2a2011-11-30 06:25:25 +00006970 return getTargetShuffleNode(X86ISD::VPERMILP, dl, VT, V1,
Craig Topper5aaffa82012-02-19 02:53:47 +00006971 getShuffleSHUFImmediate(SVOp), DAG);
Craig Topperdbd98a42012-02-07 06:28:42 +00006972 }
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00006973
Craig Topper70b883b2011-11-28 10:14:51 +00006974 // Handle VPERM2F128/VPERM2I128 permutations
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00006975 if (isVPERM2X128Mask(M, VT, HasFp256))
Craig Topperec24e612011-11-30 07:47:51 +00006976 return getTargetShuffleNode(X86ISD::VPERM2X128, dl, VT, V1,
Craig Topper70b883b2011-11-28 10:14:51 +00006977 V2, getShuffleVPERM2X128Immediate(SVOp), DAG);
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00006978
Craig Topper1842ba02012-04-23 06:38:28 +00006979 SDValue BlendOp = LowerVECTOR_SHUFFLEtoBlend(SVOp, Subtarget, DAG);
Nadav Roteme80aa7c2012-04-09 08:33:21 +00006980 if (BlendOp.getNode())
6981 return BlendOp;
Craig Topper095c5282012-04-15 23:48:57 +00006982
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00006983 if (V2IsUndef && HasInt256 && (VT == MVT::v8i32 || VT == MVT::v8f32)) {
Craig Topper095c5282012-04-15 23:48:57 +00006984 SmallVector<SDValue, 8> permclMask;
Elena Demikhovsky73c504a2012-04-15 11:18:59 +00006985 for (unsigned i = 0; i != 8; ++i) {
Craig Topper095c5282012-04-15 23:48:57 +00006986 permclMask.push_back(DAG.getConstant((M[i]>=0) ? M[i] : 0, MVT::i32));
Elena Demikhovsky73c504a2012-04-15 11:18:59 +00006987 }
Craig Topper92040742012-04-16 06:43:40 +00006988 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v8i32,
6989 &permclMask[0], 8);
6990 // Bitcast is for VPERMPS since mask is v8i32 but node takes v8f32
Craig Topper8325c112012-04-16 00:41:45 +00006991 return DAG.getNode(X86ISD::VPERMV, dl, VT,
Craig Topper92040742012-04-16 06:43:40 +00006992 DAG.getNode(ISD::BITCAST, dl, VT, Mask), V1);
Elena Demikhovsky73c504a2012-04-15 11:18:59 +00006993 }
Craig Topper095c5282012-04-15 23:48:57 +00006994
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00006995 if (V2IsUndef && HasInt256 && (VT == MVT::v4i64 || VT == MVT::v4f64))
Craig Topper8325c112012-04-16 00:41:45 +00006996 return getTargetShuffleNode(X86ISD::VPERMI, dl, VT, V1,
Elena Demikhovsky73c504a2012-04-15 11:18:59 +00006997 getShuffleCLImmediate(SVOp), DAG);
6998
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00006999 //===--------------------------------------------------------------------===//
7000 // Since no target specific shuffle was selected for this generic one,
7001 // lower it into other known shuffles. FIXME: this isn't true yet, but
7002 // this is the plan.
7003 //
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00007004
Bruno Cardoso Lopes9b4ad122011-07-27 00:56:37 +00007005 // Handle v8i16 specifically since SSE can do byte extraction and insertion.
7006 if (VT == MVT::v8i16) {
Craig Topper55b24052012-09-11 06:15:32 +00007007 SDValue NewOp = LowerVECTOR_SHUFFLEv8i16(Op, Subtarget, DAG);
Bruno Cardoso Lopes9b4ad122011-07-27 00:56:37 +00007008 if (NewOp.getNode())
7009 return NewOp;
7010 }
7011
7012 if (VT == MVT::v16i8) {
7013 SDValue NewOp = LowerVECTOR_SHUFFLEv16i8(SVOp, DAG, *this);
7014 if (NewOp.getNode())
7015 return NewOp;
7016 }
7017
Elena Demikhovsky41789462012-09-06 12:42:01 +00007018 if (VT == MVT::v32i8) {
Craig Topper55b24052012-09-11 06:15:32 +00007019 SDValue NewOp = LowerVECTOR_SHUFFLEv32i8(SVOp, Subtarget, DAG);
Elena Demikhovsky41789462012-09-06 12:42:01 +00007020 if (NewOp.getNode())
7021 return NewOp;
7022 }
7023
Bruno Cardoso Lopes9b4ad122011-07-27 00:56:37 +00007024 // Handle all 128-bit wide vectors with 4 elements, and match them with
7025 // several different shuffle types.
Craig Topper7a9a28b2012-08-12 02:23:29 +00007026 if (NumElems == 4 && VT.is128BitVector())
Bruno Cardoso Lopes9b4ad122011-07-27 00:56:37 +00007027 return LowerVECTOR_SHUFFLE_128v4(SVOp, DAG);
7028
Bruno Cardoso Lopesd0888342011-07-22 00:14:56 +00007029 // Handle general 256-bit shuffles
7030 if (VT.is256BitVector())
7031 return LowerVECTOR_SHUFFLE_256(SVOp, DAG);
7032
Dan Gohman475871a2008-07-27 21:46:04 +00007033 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007034}
7035
Dan Gohman475871a2008-07-27 21:46:04 +00007036SDValue
7037X86TargetLowering::LowerEXTRACT_VECTOR_ELT_SSE4(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00007038 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00007039 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007040 DebugLoc dl = Op.getDebugLoc();
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007041
Craig Topper7a9a28b2012-08-12 02:23:29 +00007042 if (!Op.getOperand(0).getValueType().is128BitVector())
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007043 return SDValue();
7044
Duncan Sands83ec4b62008-06-06 12:08:01 +00007045 if (VT.getSizeInBits() == 8) {
Owen Anderson825b72b2009-08-11 20:47:22 +00007046 SDValue Extract = DAG.getNode(X86ISD::PEXTRB, dl, MVT::i32,
Craig Topper7c022842012-09-12 06:20:41 +00007047 Op.getOperand(0), Op.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00007048 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Extract,
Craig Topper7c022842012-09-12 06:20:41 +00007049 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00007050 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Craig Topper69947b92012-04-23 06:57:04 +00007051 }
7052
7053 if (VT.getSizeInBits() == 16) {
Evan Cheng52ceafa2009-01-02 05:29:08 +00007054 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
7055 // If Idx is 0, it's cheaper to do a move instead of a pextrw.
7056 if (Idx == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00007057 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i16,
7058 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007059 DAG.getNode(ISD::BITCAST, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00007060 MVT::v4i32,
Evan Cheng52ceafa2009-01-02 05:29:08 +00007061 Op.getOperand(0)),
7062 Op.getOperand(1)));
Owen Anderson825b72b2009-08-11 20:47:22 +00007063 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, dl, MVT::i32,
Craig Topper7c022842012-09-12 06:20:41 +00007064 Op.getOperand(0), Op.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00007065 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Extract,
Craig Topper7c022842012-09-12 06:20:41 +00007066 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00007067 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Craig Topper69947b92012-04-23 06:57:04 +00007068 }
7069
7070 if (VT == MVT::f32) {
Evan Cheng62a3f152008-03-24 21:52:23 +00007071 // EXTRACTPS outputs to a GPR32 register which will require a movd to copy
7072 // the result back to FR32 register. It's only worth matching if the
Dan Gohmand17cfbe2008-10-31 00:57:24 +00007073 // result has a single use which is a store or a bitcast to i32. And in
7074 // the case of a store, it's not worth it if the index is a constant 0,
7075 // because a MOVSSmr can be used instead, which is smaller and faster.
Evan Cheng62a3f152008-03-24 21:52:23 +00007076 if (!Op.hasOneUse())
Dan Gohman475871a2008-07-27 21:46:04 +00007077 return SDValue();
Gabor Greifba36cb52008-08-28 21:40:38 +00007078 SDNode *User = *Op.getNode()->use_begin();
Dan Gohmand17cfbe2008-10-31 00:57:24 +00007079 if ((User->getOpcode() != ISD::STORE ||
7080 (isa<ConstantSDNode>(Op.getOperand(1)) &&
7081 cast<ConstantSDNode>(Op.getOperand(1))->isNullValue())) &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007082 (User->getOpcode() != ISD::BITCAST ||
Owen Anderson825b72b2009-08-11 20:47:22 +00007083 User->getValueType(0) != MVT::i32))
Dan Gohman475871a2008-07-27 21:46:04 +00007084 return SDValue();
Owen Anderson825b72b2009-08-11 20:47:22 +00007085 SDValue Extract = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007086 DAG.getNode(ISD::BITCAST, dl, MVT::v4i32,
Dale Johannesenace16102009-02-03 19:33:06 +00007087 Op.getOperand(0)),
7088 Op.getOperand(1));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007089 return DAG.getNode(ISD::BITCAST, dl, MVT::f32, Extract);
Craig Topper69947b92012-04-23 06:57:04 +00007090 }
7091
7092 if (VT == MVT::i32 || VT == MVT::i64) {
Pete Coopera77214a2011-11-14 19:38:42 +00007093 // ExtractPS/pextrq works with constant index.
Mon P Wangf0fcdd82009-01-15 21:10:20 +00007094 if (isa<ConstantSDNode>(Op.getOperand(1)))
7095 return Op;
Nate Begeman14d12ca2008-02-11 04:19:36 +00007096 }
Dan Gohman475871a2008-07-27 21:46:04 +00007097 return SDValue();
Nate Begeman14d12ca2008-02-11 04:19:36 +00007098}
7099
Dan Gohman475871a2008-07-27 21:46:04 +00007100SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007101X86TargetLowering::LowerEXTRACT_VECTOR_ELT(SDValue Op,
7102 SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00007103 if (!isa<ConstantSDNode>(Op.getOperand(1)))
Dan Gohman475871a2008-07-27 21:46:04 +00007104 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007105
David Greene74a579d2011-02-10 16:57:36 +00007106 SDValue Vec = Op.getOperand(0);
7107 EVT VecVT = Vec.getValueType();
7108
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007109 // If this is a 256-bit vector result, first extract the 128-bit vector and
7110 // then extract the element from the 128-bit vector.
Craig Topper7a9a28b2012-08-12 02:23:29 +00007111 if (VecVT.is256BitVector()) {
David Greene74a579d2011-02-10 16:57:36 +00007112 DebugLoc dl = Op.getNode()->getDebugLoc();
7113 unsigned NumElems = VecVT.getVectorNumElements();
7114 SDValue Idx = Op.getOperand(1);
David Greene74a579d2011-02-10 16:57:36 +00007115 unsigned IdxVal = cast<ConstantSDNode>(Idx)->getZExtValue();
7116
7117 // Get the 128-bit vector.
Craig Topper7d1e3dc2012-04-30 05:17:10 +00007118 Vec = Extract128BitVector(Vec, IdxVal, DAG, dl);
David Greene74a579d2011-02-10 16:57:36 +00007119
Craig Topper7d1e3dc2012-04-30 05:17:10 +00007120 if (IdxVal >= NumElems/2)
7121 IdxVal -= NumElems/2;
David Greene74a579d2011-02-10 16:57:36 +00007122 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, Op.getValueType(), Vec,
Craig Topper7d1e3dc2012-04-30 05:17:10 +00007123 DAG.getConstant(IdxVal, MVT::i32));
David Greene74a579d2011-02-10 16:57:36 +00007124 }
7125
Craig Topper7a9a28b2012-08-12 02:23:29 +00007126 assert(VecVT.is128BitVector() && "Unexpected vector length");
David Greene74a579d2011-02-10 16:57:36 +00007127
Craig Topperd0a31172012-01-10 06:37:29 +00007128 if (Subtarget->hasSSE41()) {
Dan Gohman475871a2008-07-27 21:46:04 +00007129 SDValue Res = LowerEXTRACT_VECTOR_ELT_SSE4(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00007130 if (Res.getNode())
Evan Cheng62a3f152008-03-24 21:52:23 +00007131 return Res;
7132 }
Nate Begeman14d12ca2008-02-11 04:19:36 +00007133
Owen Andersone50ed302009-08-10 22:56:29 +00007134 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007135 DebugLoc dl = Op.getDebugLoc();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007136 // TODO: handle v16i8.
Duncan Sands83ec4b62008-06-06 12:08:01 +00007137 if (VT.getSizeInBits() == 16) {
Dan Gohman475871a2008-07-27 21:46:04 +00007138 SDValue Vec = Op.getOperand(0);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007139 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00007140 if (Idx == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00007141 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i16,
7142 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007143 DAG.getNode(ISD::BITCAST, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00007144 MVT::v4i32, Vec),
Evan Cheng14b32e12007-12-11 01:46:18 +00007145 Op.getOperand(1)));
Evan Cheng0db9fe62006-04-25 20:13:52 +00007146 // Transform it so it match pextrw which produces a 32-bit result.
Ken Dyck70d0ef12009-12-17 15:31:52 +00007147 EVT EltVT = MVT::i32;
Dan Gohman8a55ce42009-09-23 21:02:20 +00007148 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, dl, EltVT,
Craig Topper7c022842012-09-12 06:20:41 +00007149 Op.getOperand(0), Op.getOperand(1));
Dan Gohman8a55ce42009-09-23 21:02:20 +00007150 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, EltVT, Extract,
Craig Topper7c022842012-09-12 06:20:41 +00007151 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00007152 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Craig Topper69947b92012-04-23 06:57:04 +00007153 }
7154
7155 if (VT.getSizeInBits() == 32) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007156 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007157 if (Idx == 0)
7158 return Op;
Eric Christopherfd179292009-08-27 18:07:15 +00007159
Evan Cheng0db9fe62006-04-25 20:13:52 +00007160 // SHUFPS the element to the lowest double word, then movss.
Jeffrey Yasskina44defe2011-07-27 06:22:51 +00007161 int Mask[4] = { static_cast<int>(Idx), -1, -1, -1 };
Owen Andersone50ed302009-08-10 22:56:29 +00007162 EVT VVT = Op.getOperand(0).getValueType();
Eric Christopherfd179292009-08-27 18:07:15 +00007163 SDValue Vec = DAG.getVectorShuffle(VVT, dl, Op.getOperand(0),
Nate Begeman9008ca62009-04-27 18:41:29 +00007164 DAG.getUNDEF(VVT), Mask);
Dale Johannesenace16102009-02-03 19:33:06 +00007165 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, Vec,
Chris Lattner0bd48932008-01-17 07:00:52 +00007166 DAG.getIntPtrConstant(0));
Craig Topper69947b92012-04-23 06:57:04 +00007167 }
7168
7169 if (VT.getSizeInBits() == 64) {
Nate Begeman14d12ca2008-02-11 04:19:36 +00007170 // FIXME: .td only matches this for <2 x f64>, not <2 x i64> on 32b
7171 // FIXME: seems like this should be unnecessary if mov{h,l}pd were taught
7172 // to match extract_elt for f64.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007173 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007174 if (Idx == 0)
7175 return Op;
7176
7177 // UNPCKHPD the element to the lowest double word, then movsd.
7178 // Note if the lower 64 bits of the result of the UNPCKHPD is then stored
7179 // to a f64mem, the whole operation is folded into a single MOVHPDmr.
Nate Begeman9008ca62009-04-27 18:41:29 +00007180 int Mask[2] = { 1, -1 };
Owen Andersone50ed302009-08-10 22:56:29 +00007181 EVT VVT = Op.getOperand(0).getValueType();
Eric Christopherfd179292009-08-27 18:07:15 +00007182 SDValue Vec = DAG.getVectorShuffle(VVT, dl, Op.getOperand(0),
Nate Begeman9008ca62009-04-27 18:41:29 +00007183 DAG.getUNDEF(VVT), Mask);
Dale Johannesenace16102009-02-03 19:33:06 +00007184 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, Vec,
Chris Lattner0bd48932008-01-17 07:00:52 +00007185 DAG.getIntPtrConstant(0));
Evan Cheng0db9fe62006-04-25 20:13:52 +00007186 }
7187
Dan Gohman475871a2008-07-27 21:46:04 +00007188 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007189}
7190
Dan Gohman475871a2008-07-27 21:46:04 +00007191SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007192X86TargetLowering::LowerINSERT_VECTOR_ELT_SSE4(SDValue Op,
7193 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00007194 EVT VT = Op.getValueType();
Dan Gohman8a55ce42009-09-23 21:02:20 +00007195 EVT EltVT = VT.getVectorElementType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007196 DebugLoc dl = Op.getDebugLoc();
Nate Begeman14d12ca2008-02-11 04:19:36 +00007197
Dan Gohman475871a2008-07-27 21:46:04 +00007198 SDValue N0 = Op.getOperand(0);
7199 SDValue N1 = Op.getOperand(1);
7200 SDValue N2 = Op.getOperand(2);
Nate Begeman14d12ca2008-02-11 04:19:36 +00007201
Craig Topper7a9a28b2012-08-12 02:23:29 +00007202 if (!VT.is128BitVector())
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007203 return SDValue();
7204
Dan Gohman8a55ce42009-09-23 21:02:20 +00007205 if ((EltVT.getSizeInBits() == 8 || EltVT.getSizeInBits() == 16) &&
Dan Gohmanef521f12008-08-14 22:53:18 +00007206 isa<ConstantSDNode>(N2)) {
Chris Lattner8f2b4cc2010-02-23 02:07:48 +00007207 unsigned Opc;
7208 if (VT == MVT::v8i16)
7209 Opc = X86ISD::PINSRW;
Chris Lattner8f2b4cc2010-02-23 02:07:48 +00007210 else if (VT == MVT::v16i8)
7211 Opc = X86ISD::PINSRB;
7212 else
7213 Opc = X86ISD::PINSRB;
7214
Nate Begeman14d12ca2008-02-11 04:19:36 +00007215 // Transform it so it match pinsr{b,w} which expects a GR32 as its second
7216 // argument.
Owen Anderson825b72b2009-08-11 20:47:22 +00007217 if (N1.getValueType() != MVT::i32)
7218 N1 = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, N1);
7219 if (N2.getValueType() != MVT::i32)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007220 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Dale Johannesenace16102009-02-03 19:33:06 +00007221 return DAG.getNode(Opc, dl, VT, N0, N1, N2);
Craig Topper69947b92012-04-23 06:57:04 +00007222 }
7223
7224 if (EltVT == MVT::f32 && isa<ConstantSDNode>(N2)) {
Nate Begeman14d12ca2008-02-11 04:19:36 +00007225 // Bits [7:6] of the constant are the source select. This will always be
7226 // zero here. The DAG Combiner may combine an extract_elt index into these
7227 // bits. For example (insert (extract, 3), 2) could be matched by putting
7228 // the '3' into bits [7:6] of X86ISD::INSERTPS.
Scott Michelfdc40a02009-02-17 22:15:04 +00007229 // Bits [5:4] of the constant are the destination select. This is the
Nate Begeman14d12ca2008-02-11 04:19:36 +00007230 // value of the incoming immediate.
Scott Michelfdc40a02009-02-17 22:15:04 +00007231 // Bits [3:0] of the constant are the zero mask. The DAG Combiner may
Nate Begeman14d12ca2008-02-11 04:19:36 +00007232 // combine either bitwise AND or insert of float 0.0 to set these bits.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007233 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue() << 4);
Eric Christopherfbd66872009-07-24 00:33:09 +00007234 // Create this as a scalar to vector..
Owen Anderson825b72b2009-08-11 20:47:22 +00007235 N1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4f32, N1);
Dale Johannesenace16102009-02-03 19:33:06 +00007236 return DAG.getNode(X86ISD::INSERTPS, dl, VT, N0, N1, N2);
Craig Topper69947b92012-04-23 06:57:04 +00007237 }
7238
7239 if ((EltVT == MVT::i32 || EltVT == MVT::i64) && isa<ConstantSDNode>(N2)) {
Eric Christopherfbd66872009-07-24 00:33:09 +00007240 // PINSR* works with constant index.
7241 return Op;
Nate Begeman14d12ca2008-02-11 04:19:36 +00007242 }
Dan Gohman475871a2008-07-27 21:46:04 +00007243 return SDValue();
Nate Begeman14d12ca2008-02-11 04:19:36 +00007244}
7245
Dan Gohman475871a2008-07-27 21:46:04 +00007246SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007247X86TargetLowering::LowerINSERT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00007248 EVT VT = Op.getValueType();
Dan Gohman8a55ce42009-09-23 21:02:20 +00007249 EVT EltVT = VT.getVectorElementType();
Nate Begeman14d12ca2008-02-11 04:19:36 +00007250
David Greene6b381262011-02-09 15:32:06 +00007251 DebugLoc dl = Op.getDebugLoc();
7252 SDValue N0 = Op.getOperand(0);
7253 SDValue N1 = Op.getOperand(1);
7254 SDValue N2 = Op.getOperand(2);
7255
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007256 // If this is a 256-bit vector result, first extract the 128-bit vector,
7257 // insert the element into the extracted half and then place it back.
Craig Topper7a9a28b2012-08-12 02:23:29 +00007258 if (VT.is256BitVector()) {
David Greene6b381262011-02-09 15:32:06 +00007259 if (!isa<ConstantSDNode>(N2))
7260 return SDValue();
7261
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007262 // Get the desired 128-bit vector half.
David Greene6b381262011-02-09 15:32:06 +00007263 unsigned NumElems = VT.getVectorNumElements();
7264 unsigned IdxVal = cast<ConstantSDNode>(N2)->getZExtValue();
Craig Topper7d1e3dc2012-04-30 05:17:10 +00007265 SDValue V = Extract128BitVector(N0, IdxVal, DAG, dl);
David Greene6b381262011-02-09 15:32:06 +00007266
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007267 // Insert the element into the desired half.
Craig Topper7d1e3dc2012-04-30 05:17:10 +00007268 bool Upper = IdxVal >= NumElems/2;
7269 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, V.getValueType(), V, N1,
7270 DAG.getConstant(Upper ? IdxVal-NumElems/2 : IdxVal, MVT::i32));
David Greene6b381262011-02-09 15:32:06 +00007271
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007272 // Insert the changed part back to the 256-bit vector
Craig Topper7d1e3dc2012-04-30 05:17:10 +00007273 return Insert128BitVector(N0, V, IdxVal, DAG, dl);
David Greene6b381262011-02-09 15:32:06 +00007274 }
7275
Craig Topperd0a31172012-01-10 06:37:29 +00007276 if (Subtarget->hasSSE41())
Nate Begeman14d12ca2008-02-11 04:19:36 +00007277 return LowerINSERT_VECTOR_ELT_SSE4(Op, DAG);
7278
Dan Gohman8a55ce42009-09-23 21:02:20 +00007279 if (EltVT == MVT::i8)
Dan Gohman475871a2008-07-27 21:46:04 +00007280 return SDValue();
Evan Cheng794405e2007-12-12 07:55:34 +00007281
Dan Gohman8a55ce42009-09-23 21:02:20 +00007282 if (EltVT.getSizeInBits() == 16 && isa<ConstantSDNode>(N2)) {
Evan Cheng794405e2007-12-12 07:55:34 +00007283 // Transform it so it match pinsrw which expects a 16-bit value in a GR32
7284 // as its second argument.
Owen Anderson825b72b2009-08-11 20:47:22 +00007285 if (N1.getValueType() != MVT::i32)
7286 N1 = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, N1);
7287 if (N2.getValueType() != MVT::i32)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007288 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Dale Johannesen0488fb62010-09-30 23:57:10 +00007289 return DAG.getNode(X86ISD::PINSRW, dl, VT, N0, N1, N2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007290 }
Dan Gohman475871a2008-07-27 21:46:04 +00007291 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007292}
7293
Craig Topper55b24052012-09-11 06:15:32 +00007294static SDValue LowerSCALAR_TO_VECTOR(SDValue Op, SelectionDAG &DAG) {
Bruno Cardoso Lopes233fa392011-07-25 23:05:16 +00007295 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007296 DebugLoc dl = Op.getDebugLoc();
David Greene2fcdfb42011-02-10 23:11:29 +00007297 EVT OpVT = Op.getValueType();
7298
Bruno Cardoso Lopes233fa392011-07-25 23:05:16 +00007299 // If this is a 256-bit vector result, first insert into a 128-bit
7300 // vector and then insert into the 256-bit vector.
Craig Topper7a9a28b2012-08-12 02:23:29 +00007301 if (!OpVT.is128BitVector()) {
Bruno Cardoso Lopes233fa392011-07-25 23:05:16 +00007302 // Insert into a 128-bit vector.
7303 EVT VT128 = EVT::getVectorVT(*Context,
7304 OpVT.getVectorElementType(),
7305 OpVT.getVectorNumElements() / 2);
7306
7307 Op = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT128, Op.getOperand(0));
7308
7309 // Insert the 128-bit vector.
Craig Topperb14940a2012-04-22 20:55:18 +00007310 return Insert128BitVector(DAG.getUNDEF(OpVT), Op, 0, DAG, dl);
Bruno Cardoso Lopes233fa392011-07-25 23:05:16 +00007311 }
7312
Craig Topperd77d2fe2012-04-29 20:22:05 +00007313 if (OpVT == MVT::v1i64 &&
Chris Lattnerf172ecd2010-07-04 23:07:25 +00007314 Op.getOperand(0).getValueType() == MVT::i64)
Owen Anderson825b72b2009-08-11 20:47:22 +00007315 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v1i64, Op.getOperand(0));
Rafael Espindoladef390a2009-08-03 02:45:34 +00007316
Owen Anderson825b72b2009-08-11 20:47:22 +00007317 SDValue AnyExt = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, Op.getOperand(0));
Craig Topper7a9a28b2012-08-12 02:23:29 +00007318 assert(OpVT.is128BitVector() && "Expected an SSE type!");
Craig Topperd77d2fe2012-04-29 20:22:05 +00007319 return DAG.getNode(ISD::BITCAST, dl, OpVT,
Dale Johannesen0488fb62010-09-30 23:57:10 +00007320 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,AnyExt));
Evan Cheng0db9fe62006-04-25 20:13:52 +00007321}
7322
David Greene91585092011-01-26 15:38:49 +00007323// Lower a node with an EXTRACT_SUBVECTOR opcode. This may result in
7324// a simple subregister reference or explicit instructions to grab
7325// upper bits of a vector.
Craig Topper55b24052012-09-11 06:15:32 +00007326static SDValue LowerEXTRACT_SUBVECTOR(SDValue Op, const X86Subtarget *Subtarget,
7327 SelectionDAG &DAG) {
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00007328 if (Subtarget->hasFp256()) {
David Greenea5f26012011-02-07 19:36:54 +00007329 DebugLoc dl = Op.getNode()->getDebugLoc();
7330 SDValue Vec = Op.getNode()->getOperand(0);
7331 SDValue Idx = Op.getNode()->getOperand(1);
7332
Craig Topper7a9a28b2012-08-12 02:23:29 +00007333 if (Op.getNode()->getValueType(0).is128BitVector() &&
7334 Vec.getNode()->getValueType(0).is256BitVector() &&
Craig Topperb14940a2012-04-22 20:55:18 +00007335 isa<ConstantSDNode>(Idx)) {
7336 unsigned IdxVal = cast<ConstantSDNode>(Idx)->getZExtValue();
7337 return Extract128BitVector(Vec, IdxVal, DAG, dl);
David Greenea5f26012011-02-07 19:36:54 +00007338 }
David Greene91585092011-01-26 15:38:49 +00007339 }
7340 return SDValue();
7341}
7342
David Greenecfe33c42011-01-26 19:13:22 +00007343// Lower a node with an INSERT_SUBVECTOR opcode. This may result in a
7344// simple superregister reference or explicit instructions to insert
7345// the upper bits of a vector.
Craig Topper55b24052012-09-11 06:15:32 +00007346static SDValue LowerINSERT_SUBVECTOR(SDValue Op, const X86Subtarget *Subtarget,
7347 SelectionDAG &DAG) {
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00007348 if (Subtarget->hasFp256()) {
David Greenecfe33c42011-01-26 19:13:22 +00007349 DebugLoc dl = Op.getNode()->getDebugLoc();
7350 SDValue Vec = Op.getNode()->getOperand(0);
7351 SDValue SubVec = Op.getNode()->getOperand(1);
7352 SDValue Idx = Op.getNode()->getOperand(2);
7353
Craig Topper7a9a28b2012-08-12 02:23:29 +00007354 if (Op.getNode()->getValueType(0).is256BitVector() &&
7355 SubVec.getNode()->getValueType(0).is128BitVector() &&
Craig Topperb14940a2012-04-22 20:55:18 +00007356 isa<ConstantSDNode>(Idx)) {
7357 unsigned IdxVal = cast<ConstantSDNode>(Idx)->getZExtValue();
7358 return Insert128BitVector(Vec, SubVec, IdxVal, DAG, dl);
David Greenecfe33c42011-01-26 19:13:22 +00007359 }
7360 }
7361 return SDValue();
7362}
7363
Bill Wendling056292f2008-09-16 21:48:12 +00007364// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
7365// their target countpart wrapped in the X86ISD::Wrapper node. Suppose N is
7366// one of the above mentioned nodes. It has to be wrapped because otherwise
7367// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
7368// be used to form addressing mode. These wrapped nodes will be selected
7369// into MOV32ri.
Dan Gohman475871a2008-07-27 21:46:04 +00007370SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007371X86TargetLowering::LowerConstantPool(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00007372 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Eric Christopherfd179292009-08-27 18:07:15 +00007373
Chris Lattner41621a22009-06-26 19:22:52 +00007374 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
7375 // global base reg.
7376 unsigned char OpFlag = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00007377 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007378 CodeModel::Model M = getTargetMachine().getCodeModel();
7379
Chris Lattner4f066492009-07-11 20:29:19 +00007380 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007381 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattnere4df7562009-07-09 03:15:51 +00007382 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00007383 else if (Subtarget->isPICStyleGOT())
Chris Lattner88e1fd52009-07-09 04:24:46 +00007384 OpFlag = X86II::MO_GOTOFF;
Chris Lattnere2c92082009-07-10 21:00:45 +00007385 else if (Subtarget->isPICStyleStubPIC())
Chris Lattner88e1fd52009-07-09 04:24:46 +00007386 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Eric Christopherfd179292009-08-27 18:07:15 +00007387
Evan Cheng1606e8e2009-03-13 07:51:59 +00007388 SDValue Result = DAG.getTargetConstantPool(CP->getConstVal(), getPointerTy(),
Chris Lattner41621a22009-06-26 19:22:52 +00007389 CP->getAlignment(),
7390 CP->getOffset(), OpFlag);
7391 DebugLoc DL = CP->getDebugLoc();
Chris Lattner18c59872009-06-27 04:16:01 +00007392 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00007393 // With PIC, the address is actually $g + Offset.
Chris Lattner41621a22009-06-26 19:22:52 +00007394 if (OpFlag) {
7395 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesenb300d2a2009-02-07 00:55:49 +00007396 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00007397 DebugLoc(), getPointerTy()),
Anton Korobeynikov7f705592007-01-12 19:20:47 +00007398 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007399 }
7400
7401 return Result;
7402}
7403
Dan Gohmand858e902010-04-17 15:26:15 +00007404SDValue X86TargetLowering::LowerJumpTable(SDValue Op, SelectionDAG &DAG) const {
Chris Lattner18c59872009-06-27 04:16:01 +00007405 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
Eric Christopherfd179292009-08-27 18:07:15 +00007406
Chris Lattner18c59872009-06-27 04:16:01 +00007407 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
7408 // global base reg.
7409 unsigned char OpFlag = 0;
7410 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007411 CodeModel::Model M = getTargetMachine().getCodeModel();
7412
Chris Lattner4f066492009-07-11 20:29:19 +00007413 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007414 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattnere4df7562009-07-09 03:15:51 +00007415 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00007416 else if (Subtarget->isPICStyleGOT())
Chris Lattner88e1fd52009-07-09 04:24:46 +00007417 OpFlag = X86II::MO_GOTOFF;
Chris Lattnere2c92082009-07-10 21:00:45 +00007418 else if (Subtarget->isPICStyleStubPIC())
Chris Lattner88e1fd52009-07-09 04:24:46 +00007419 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Eric Christopherfd179292009-08-27 18:07:15 +00007420
Chris Lattner18c59872009-06-27 04:16:01 +00007421 SDValue Result = DAG.getTargetJumpTable(JT->getIndex(), getPointerTy(),
7422 OpFlag);
7423 DebugLoc DL = JT->getDebugLoc();
7424 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Eric Christopherfd179292009-08-27 18:07:15 +00007425
Chris Lattner18c59872009-06-27 04:16:01 +00007426 // With PIC, the address is actually $g + Offset.
Chris Lattner1e61e692010-11-15 02:46:57 +00007427 if (OpFlag)
Chris Lattner18c59872009-06-27 04:16:01 +00007428 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
7429 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00007430 DebugLoc(), getPointerTy()),
Chris Lattner18c59872009-06-27 04:16:01 +00007431 Result);
Eric Christopherfd179292009-08-27 18:07:15 +00007432
Chris Lattner18c59872009-06-27 04:16:01 +00007433 return Result;
7434}
7435
7436SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007437X86TargetLowering::LowerExternalSymbol(SDValue Op, SelectionDAG &DAG) const {
Chris Lattner18c59872009-06-27 04:16:01 +00007438 const char *Sym = cast<ExternalSymbolSDNode>(Op)->getSymbol();
Eric Christopherfd179292009-08-27 18:07:15 +00007439
Chris Lattner18c59872009-06-27 04:16:01 +00007440 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
7441 // global base reg.
7442 unsigned char OpFlag = 0;
7443 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007444 CodeModel::Model M = getTargetMachine().getCodeModel();
7445
Chris Lattner4f066492009-07-11 20:29:19 +00007446 if (Subtarget->isPICStyleRIPRel() &&
Eli Friedman586272d2011-08-11 01:48:05 +00007447 (M == CodeModel::Small || M == CodeModel::Kernel)) {
7448 if (Subtarget->isTargetDarwin() || Subtarget->isTargetELF())
7449 OpFlag = X86II::MO_GOTPCREL;
Chris Lattnere4df7562009-07-09 03:15:51 +00007450 WrapperKind = X86ISD::WrapperRIP;
Eli Friedman586272d2011-08-11 01:48:05 +00007451 } else if (Subtarget->isPICStyleGOT()) {
7452 OpFlag = X86II::MO_GOT;
7453 } else if (Subtarget->isPICStyleStubPIC()) {
7454 OpFlag = X86II::MO_DARWIN_NONLAZY_PIC_BASE;
7455 } else if (Subtarget->isPICStyleStubNoDynamic()) {
7456 OpFlag = X86II::MO_DARWIN_NONLAZY;
7457 }
Eric Christopherfd179292009-08-27 18:07:15 +00007458
Chris Lattner18c59872009-06-27 04:16:01 +00007459 SDValue Result = DAG.getTargetExternalSymbol(Sym, getPointerTy(), OpFlag);
Eric Christopherfd179292009-08-27 18:07:15 +00007460
Chris Lattner18c59872009-06-27 04:16:01 +00007461 DebugLoc DL = Op.getDebugLoc();
7462 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Eric Christopherfd179292009-08-27 18:07:15 +00007463
Chris Lattner18c59872009-06-27 04:16:01 +00007464 // With PIC, the address is actually $g + Offset.
7465 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
Chris Lattnere4df7562009-07-09 03:15:51 +00007466 !Subtarget->is64Bit()) {
Chris Lattner18c59872009-06-27 04:16:01 +00007467 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
7468 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00007469 DebugLoc(), getPointerTy()),
Chris Lattner18c59872009-06-27 04:16:01 +00007470 Result);
7471 }
Eric Christopherfd179292009-08-27 18:07:15 +00007472
Eli Friedman586272d2011-08-11 01:48:05 +00007473 // For symbols that require a load from a stub to get the address, emit the
7474 // load.
7475 if (isGlobalStubReference(OpFlag))
7476 Result = DAG.getLoad(getPointerTy(), DL, DAG.getEntryNode(), Result,
Pete Cooperd752e0f2011-11-08 18:42:53 +00007477 MachinePointerInfo::getGOT(), false, false, false, 0);
Eli Friedman586272d2011-08-11 01:48:05 +00007478
Chris Lattner18c59872009-06-27 04:16:01 +00007479 return Result;
7480}
7481
Dan Gohman475871a2008-07-27 21:46:04 +00007482SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007483X86TargetLowering::LowerBlockAddress(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman29cbade2009-11-20 23:18:13 +00007484 // Create the TargetBlockAddressAddress node.
7485 unsigned char OpFlags =
7486 Subtarget->ClassifyBlockAddressReference();
Dan Gohmanf705adb2009-10-30 01:28:02 +00007487 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman46510a72010-04-15 01:51:59 +00007488 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
Michael Liao6c7ccaa2012-09-12 21:43:09 +00007489 int64_t Offset = cast<BlockAddressSDNode>(Op)->getOffset();
Dan Gohman29cbade2009-11-20 23:18:13 +00007490 DebugLoc dl = Op.getDebugLoc();
Michael Liao6c7ccaa2012-09-12 21:43:09 +00007491 SDValue Result = DAG.getTargetBlockAddress(BA, getPointerTy(), Offset,
7492 OpFlags);
Dan Gohman29cbade2009-11-20 23:18:13 +00007493
Dan Gohmanf705adb2009-10-30 01:28:02 +00007494 if (Subtarget->isPICStyleRIPRel() &&
7495 (M == CodeModel::Small || M == CodeModel::Kernel))
Dan Gohman29cbade2009-11-20 23:18:13 +00007496 Result = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Result);
7497 else
7498 Result = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), Result);
Dan Gohmanf705adb2009-10-30 01:28:02 +00007499
Dan Gohman29cbade2009-11-20 23:18:13 +00007500 // With PIC, the address is actually $g + Offset.
7501 if (isGlobalRelativeToPICBase(OpFlags)) {
7502 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(),
7503 DAG.getNode(X86ISD::GlobalBaseReg, dl, getPointerTy()),
7504 Result);
7505 }
Dan Gohmanf705adb2009-10-30 01:28:02 +00007506
7507 return Result;
7508}
7509
7510SDValue
Dale Johannesen33c960f2009-02-04 20:06:27 +00007511X86TargetLowering::LowerGlobalAddress(const GlobalValue *GV, DebugLoc dl,
Dan Gohman6520e202008-10-18 02:06:02 +00007512 int64_t Offset,
Evan Chengda43bcf2008-09-24 00:05:32 +00007513 SelectionDAG &DAG) const {
Dan Gohman6520e202008-10-18 02:06:02 +00007514 // Create the TargetGlobalAddress node, folding in the constant
7515 // offset if it is legal.
Chris Lattnerd392bd92009-07-10 07:20:05 +00007516 unsigned char OpFlags =
7517 Subtarget->ClassifyGlobalReference(GV, getTargetMachine());
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007518 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman6520e202008-10-18 02:06:02 +00007519 SDValue Result;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007520 if (OpFlags == X86II::MO_NO_FLAG &&
7521 X86::isOffsetSuitableForCodeModel(Offset, M)) {
Chris Lattner4aa21aa2009-07-09 00:58:53 +00007522 // A direct static reference to a global.
Devang Patel0d881da2010-07-06 22:08:15 +00007523 Result = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(), Offset);
Dan Gohman6520e202008-10-18 02:06:02 +00007524 Offset = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00007525 } else {
Devang Patel0d881da2010-07-06 22:08:15 +00007526 Result = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(), 0, OpFlags);
Chris Lattner18c59872009-06-27 04:16:01 +00007527 }
Eric Christopherfd179292009-08-27 18:07:15 +00007528
Chris Lattner4f066492009-07-11 20:29:19 +00007529 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007530 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattner18c59872009-06-27 04:16:01 +00007531 Result = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Result);
7532 else
7533 Result = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), Result);
Dan Gohman6520e202008-10-18 02:06:02 +00007534
Anton Korobeynikov7f705592007-01-12 19:20:47 +00007535 // With PIC, the address is actually $g + Offset.
Chris Lattner36c25012009-07-10 07:34:39 +00007536 if (isGlobalRelativeToPICBase(OpFlags)) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00007537 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(),
7538 DAG.getNode(X86ISD::GlobalBaseReg, dl, getPointerTy()),
Anton Korobeynikov7f705592007-01-12 19:20:47 +00007539 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007540 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007541
Chris Lattner36c25012009-07-10 07:34:39 +00007542 // For globals that require a load from a stub to get the address, emit the
7543 // load.
7544 if (isGlobalStubReference(OpFlags))
Dale Johannesen33c960f2009-02-04 20:06:27 +00007545 Result = DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(), Result,
Pete Cooperd752e0f2011-11-08 18:42:53 +00007546 MachinePointerInfo::getGOT(), false, false, false, 0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007547
Dan Gohman6520e202008-10-18 02:06:02 +00007548 // If there was a non-zero offset that we didn't fold, create an explicit
7549 // addition for it.
7550 if (Offset != 0)
Dale Johannesen33c960f2009-02-04 20:06:27 +00007551 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(), Result,
Dan Gohman6520e202008-10-18 02:06:02 +00007552 DAG.getConstant(Offset, getPointerTy()));
7553
Evan Cheng0db9fe62006-04-25 20:13:52 +00007554 return Result;
7555}
7556
Evan Chengda43bcf2008-09-24 00:05:32 +00007557SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007558X86TargetLowering::LowerGlobalAddress(SDValue Op, SelectionDAG &DAG) const {
Evan Chengda43bcf2008-09-24 00:05:32 +00007559 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Dan Gohman6520e202008-10-18 02:06:02 +00007560 int64_t Offset = cast<GlobalAddressSDNode>(Op)->getOffset();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007561 return LowerGlobalAddress(GV, Op.getDebugLoc(), Offset, DAG);
Evan Chengda43bcf2008-09-24 00:05:32 +00007562}
7563
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007564static SDValue
7565GetTLSADDR(SelectionDAG &DAG, SDValue Chain, GlobalAddressSDNode *GA,
Owen Andersone50ed302009-08-10 22:56:29 +00007566 SDValue *InFlag, const EVT PtrVT, unsigned ReturnReg,
Hans Wennborgf0234fc2012-06-01 16:27:21 +00007567 unsigned char OperandFlags, bool LocalDynamic = false) {
Anton Korobeynikov817a4642009-12-11 19:39:55 +00007568 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00007569 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007570 DebugLoc dl = GA->getDebugLoc();
Devang Patel0d881da2010-07-06 22:08:15 +00007571 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl,
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007572 GA->getValueType(0),
Chris Lattnerb903bed2009-06-26 21:20:29 +00007573 GA->getOffset(),
7574 OperandFlags);
Hans Wennborgf0234fc2012-06-01 16:27:21 +00007575
7576 X86ISD::NodeType CallType = LocalDynamic ? X86ISD::TLSBASEADDR
7577 : X86ISD::TLSADDR;
7578
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007579 if (InFlag) {
7580 SDValue Ops[] = { Chain, TGA, *InFlag };
Hans Wennborgf0234fc2012-06-01 16:27:21 +00007581 Chain = DAG.getNode(CallType, dl, NodeTys, Ops, 3);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007582 } else {
7583 SDValue Ops[] = { Chain, TGA };
Hans Wennborgf0234fc2012-06-01 16:27:21 +00007584 Chain = DAG.getNode(CallType, dl, NodeTys, Ops, 2);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007585 }
Anton Korobeynikov817a4642009-12-11 19:39:55 +00007586
7587 // TLSADDR will be codegen'ed as call. Inform MFI that function has calls.
Bill Wendlingb92187a2010-05-14 21:14:32 +00007588 MFI->setAdjustsStack(true);
Anton Korobeynikov817a4642009-12-11 19:39:55 +00007589
Rafael Espindola15f1b662009-04-24 12:59:40 +00007590 SDValue Flag = Chain.getValue(1);
7591 return DAG.getCopyFromReg(Chain, dl, ReturnReg, PtrVT, Flag);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007592}
7593
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007594// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 32 bit
Dan Gohman475871a2008-07-27 21:46:04 +00007595static SDValue
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007596LowerToTLSGeneralDynamicModel32(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00007597 const EVT PtrVT) {
Dan Gohman475871a2008-07-27 21:46:04 +00007598 SDValue InFlag;
Dale Johannesendd64c412009-02-04 00:33:20 +00007599 DebugLoc dl = GA->getDebugLoc(); // ? function entry point might be better
7600 SDValue Chain = DAG.getCopyToReg(DAG.getEntryNode(), dl, X86::EBX,
Craig Topper7c022842012-09-12 06:20:41 +00007601 DAG.getNode(X86ISD::GlobalBaseReg,
7602 DebugLoc(), PtrVT), InFlag);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007603 InFlag = Chain.getValue(1);
7604
Chris Lattnerb903bed2009-06-26 21:20:29 +00007605 return GetTLSADDR(DAG, Chain, GA, &InFlag, PtrVT, X86::EAX, X86II::MO_TLSGD);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007606}
7607
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007608// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 64 bit
Dan Gohman475871a2008-07-27 21:46:04 +00007609static SDValue
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007610LowerToTLSGeneralDynamicModel64(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00007611 const EVT PtrVT) {
Chris Lattnerb903bed2009-06-26 21:20:29 +00007612 return GetTLSADDR(DAG, DAG.getEntryNode(), GA, NULL, PtrVT,
7613 X86::RAX, X86II::MO_TLSGD);
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007614}
7615
Hans Wennborgf0234fc2012-06-01 16:27:21 +00007616static SDValue LowerToTLSLocalDynamicModel(GlobalAddressSDNode *GA,
7617 SelectionDAG &DAG,
7618 const EVT PtrVT,
7619 bool is64Bit) {
7620 DebugLoc dl = GA->getDebugLoc();
7621
7622 // Get the start address of the TLS block for this module.
7623 X86MachineFunctionInfo* MFI = DAG.getMachineFunction()
7624 .getInfo<X86MachineFunctionInfo>();
7625 MFI->incNumLocalDynamicTLSAccesses();
7626
7627 SDValue Base;
7628 if (is64Bit) {
7629 Base = GetTLSADDR(DAG, DAG.getEntryNode(), GA, NULL, PtrVT, X86::RAX,
7630 X86II::MO_TLSLD, /*LocalDynamic=*/true);
7631 } else {
7632 SDValue InFlag;
7633 SDValue Chain = DAG.getCopyToReg(DAG.getEntryNode(), dl, X86::EBX,
7634 DAG.getNode(X86ISD::GlobalBaseReg, DebugLoc(), PtrVT), InFlag);
7635 InFlag = Chain.getValue(1);
7636 Base = GetTLSADDR(DAG, Chain, GA, &InFlag, PtrVT, X86::EAX,
7637 X86II::MO_TLSLDM, /*LocalDynamic=*/true);
7638 }
7639
7640 // Note: the CleanupLocalDynamicTLSPass will remove redundant computations
7641 // of Base.
7642
7643 // Build x@dtpoff.
7644 unsigned char OperandFlags = X86II::MO_DTPOFF;
7645 unsigned WrapperKind = X86ISD::Wrapper;
7646 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl,
7647 GA->getValueType(0),
7648 GA->getOffset(), OperandFlags);
7649 SDValue Offset = DAG.getNode(WrapperKind, dl, PtrVT, TGA);
7650
7651 // Add x@dtpoff with the base.
7652 return DAG.getNode(ISD::ADD, dl, PtrVT, Offset, Base);
7653}
7654
Hans Wennborg228756c2012-05-11 10:11:01 +00007655// Lower ISD::GlobalTLSAddress using the "initial exec" or "local exec" model.
Dan Gohman475871a2008-07-27 21:46:04 +00007656static SDValue LowerToTLSExecModel(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00007657 const EVT PtrVT, TLSModel::Model model,
Hans Wennborg228756c2012-05-11 10:11:01 +00007658 bool is64Bit, bool isPIC) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00007659 DebugLoc dl = GA->getDebugLoc();
Michael J. Spencerec38de22010-10-10 22:04:20 +00007660
Chris Lattnerf93b90c2010-09-22 04:39:11 +00007661 // Get the Thread Pointer, which is %gs:0 (32-bit) or %fs:0 (64-bit).
7662 Value *Ptr = Constant::getNullValue(Type::getInt8PtrTy(*DAG.getContext(),
7663 is64Bit ? 257 : 256));
Rafael Espindola094fad32009-04-08 21:14:34 +00007664
Michael J. Spencerec38de22010-10-10 22:04:20 +00007665 SDValue ThreadPointer = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(),
Chris Lattnerf93b90c2010-09-22 04:39:11 +00007666 DAG.getIntPtrConstant(0),
Pete Cooperd752e0f2011-11-08 18:42:53 +00007667 MachinePointerInfo(Ptr),
7668 false, false, false, 0);
Rafael Espindola094fad32009-04-08 21:14:34 +00007669
Chris Lattnerb903bed2009-06-26 21:20:29 +00007670 unsigned char OperandFlags = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00007671 // Most TLS accesses are not RIP relative, even on x86-64. One exception is
7672 // initialexec.
7673 unsigned WrapperKind = X86ISD::Wrapper;
7674 if (model == TLSModel::LocalExec) {
Chris Lattnerb903bed2009-06-26 21:20:29 +00007675 OperandFlags = is64Bit ? X86II::MO_TPOFF : X86II::MO_NTPOFF;
Hans Wennborg228756c2012-05-11 10:11:01 +00007676 } else if (model == TLSModel::InitialExec) {
7677 if (is64Bit) {
7678 OperandFlags = X86II::MO_GOTTPOFF;
7679 WrapperKind = X86ISD::WrapperRIP;
7680 } else {
7681 OperandFlags = isPIC ? X86II::MO_GOTNTPOFF : X86II::MO_INDNTPOFF;
7682 }
Chris Lattner18c59872009-06-27 04:16:01 +00007683 } else {
Hans Wennborg228756c2012-05-11 10:11:01 +00007684 llvm_unreachable("Unexpected model");
Chris Lattnerb903bed2009-06-26 21:20:29 +00007685 }
Eric Christopherfd179292009-08-27 18:07:15 +00007686
Hans Wennborg228756c2012-05-11 10:11:01 +00007687 // emit "addl x@ntpoff,%eax" (local exec)
7688 // or "addl x@indntpoff,%eax" (initial exec)
7689 // or "addl x@gotntpoff(%ebx) ,%eax" (initial exec, 32-bit pic)
Michael J. Spencerec38de22010-10-10 22:04:20 +00007690 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl,
Devang Patel0d881da2010-07-06 22:08:15 +00007691 GA->getValueType(0),
Chris Lattnerb903bed2009-06-26 21:20:29 +00007692 GA->getOffset(), OperandFlags);
Chris Lattner18c59872009-06-27 04:16:01 +00007693 SDValue Offset = DAG.getNode(WrapperKind, dl, PtrVT, TGA);
Lauro Ramos Venancio7d2cc2b2007-04-22 22:50:52 +00007694
Hans Wennborg228756c2012-05-11 10:11:01 +00007695 if (model == TLSModel::InitialExec) {
7696 if (isPIC && !is64Bit) {
7697 Offset = DAG.getNode(ISD::ADD, dl, PtrVT,
7698 DAG.getNode(X86ISD::GlobalBaseReg, DebugLoc(), PtrVT),
7699 Offset);
Hans Wennborg228756c2012-05-11 10:11:01 +00007700 }
Rafael Espindola94e3b382012-06-29 04:22:35 +00007701
7702 Offset = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Offset,
7703 MachinePointerInfo::getGOT(), false, false, false,
7704 0);
Hans Wennborg228756c2012-05-11 10:11:01 +00007705 }
Lauro Ramos Venancio7d2cc2b2007-04-22 22:50:52 +00007706
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007707 // The address of the thread local variable is the add of the thread
7708 // pointer with the offset of the variable.
Dale Johannesen33c960f2009-02-04 20:06:27 +00007709 return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007710}
7711
Dan Gohman475871a2008-07-27 21:46:04 +00007712SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007713X86TargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const {
Michael J. Spencerec38de22010-10-10 22:04:20 +00007714
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007715 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
Chris Lattnerb903bed2009-06-26 21:20:29 +00007716 const GlobalValue *GV = GA->getGlobal();
Eric Christopherfd179292009-08-27 18:07:15 +00007717
Eric Christopher30ef0e52010-06-03 04:07:48 +00007718 if (Subtarget->isTargetELF()) {
Chandler Carruth34797132012-04-08 17:20:55 +00007719 TLSModel::Model model = getTargetMachine().getTLSModel(GV);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007720
Eric Christopher30ef0e52010-06-03 04:07:48 +00007721 switch (model) {
7722 case TLSModel::GeneralDynamic:
Eric Christopher30ef0e52010-06-03 04:07:48 +00007723 if (Subtarget->is64Bit())
7724 return LowerToTLSGeneralDynamicModel64(GA, DAG, getPointerTy());
7725 return LowerToTLSGeneralDynamicModel32(GA, DAG, getPointerTy());
Hans Wennborgf0234fc2012-06-01 16:27:21 +00007726 case TLSModel::LocalDynamic:
7727 return LowerToTLSLocalDynamicModel(GA, DAG, getPointerTy(),
7728 Subtarget->is64Bit());
Eric Christopher30ef0e52010-06-03 04:07:48 +00007729 case TLSModel::InitialExec:
7730 case TLSModel::LocalExec:
7731 return LowerToTLSExecModel(GA, DAG, getPointerTy(), model,
Hans Wennborg228756c2012-05-11 10:11:01 +00007732 Subtarget->is64Bit(),
7733 getTargetMachine().getRelocationModel() == Reloc::PIC_);
Eric Christopher30ef0e52010-06-03 04:07:48 +00007734 }
Craig Toppere8eb1162012-04-23 03:26:18 +00007735 llvm_unreachable("Unknown TLS model.");
7736 }
7737
7738 if (Subtarget->isTargetDarwin()) {
Eric Christopher30ef0e52010-06-03 04:07:48 +00007739 // Darwin only has one model of TLS. Lower to that.
7740 unsigned char OpFlag = 0;
7741 unsigned WrapperKind = Subtarget->isPICStyleRIPRel() ?
7742 X86ISD::WrapperRIP : X86ISD::Wrapper;
Michael J. Spencerec38de22010-10-10 22:04:20 +00007743
Eric Christopher30ef0e52010-06-03 04:07:48 +00007744 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
7745 // global base reg.
7746 bool PIC32 = (getTargetMachine().getRelocationModel() == Reloc::PIC_) &&
7747 !Subtarget->is64Bit();
7748 if (PIC32)
7749 OpFlag = X86II::MO_TLVP_PIC_BASE;
7750 else
7751 OpFlag = X86II::MO_TLVP;
Michael J. Spencerec38de22010-10-10 22:04:20 +00007752 DebugLoc DL = Op.getDebugLoc();
Devang Patel0d881da2010-07-06 22:08:15 +00007753 SDValue Result = DAG.getTargetGlobalAddress(GA->getGlobal(), DL,
Eric Christopherd8c05362010-12-09 06:25:53 +00007754 GA->getValueType(0),
Eric Christopher30ef0e52010-06-03 04:07:48 +00007755 GA->getOffset(), OpFlag);
Eric Christopher30ef0e52010-06-03 04:07:48 +00007756 SDValue Offset = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007757
Eric Christopher30ef0e52010-06-03 04:07:48 +00007758 // With PIC32, the address is actually $g + Offset.
7759 if (PIC32)
7760 Offset = DAG.getNode(ISD::ADD, DL, getPointerTy(),
7761 DAG.getNode(X86ISD::GlobalBaseReg,
7762 DebugLoc(), getPointerTy()),
7763 Offset);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007764
Eric Christopher30ef0e52010-06-03 04:07:48 +00007765 // Lowering the machine isd will make sure everything is in the right
7766 // location.
Eric Christopherd8c05362010-12-09 06:25:53 +00007767 SDValue Chain = DAG.getEntryNode();
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00007768 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Eric Christopherd8c05362010-12-09 06:25:53 +00007769 SDValue Args[] = { Chain, Offset };
7770 Chain = DAG.getNode(X86ISD::TLSCALL, DL, NodeTys, Args, 2);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007771
Eric Christopher30ef0e52010-06-03 04:07:48 +00007772 // TLSCALL will be codegen'ed as call. Inform MFI that function has calls.
7773 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
7774 MFI->setAdjustsStack(true);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00007775
Eric Christopher30ef0e52010-06-03 04:07:48 +00007776 // And our return value (tls address) is in the standard call return value
7777 // location.
Eric Christopherd8c05362010-12-09 06:25:53 +00007778 unsigned Reg = Subtarget->is64Bit() ? X86::RAX : X86::EAX;
Evan Chengfd230df2011-10-19 22:22:54 +00007779 return DAG.getCopyFromReg(Chain, DL, Reg, getPointerTy(),
7780 Chain.getValue(1));
Craig Toppere8eb1162012-04-23 03:26:18 +00007781 }
7782
7783 if (Subtarget->isTargetWindows()) {
Anton Korobeynikovd4a19b62012-02-11 17:26:53 +00007784 // Just use the implicit TLS architecture
7785 // Need to generate someting similar to:
7786 // mov rdx, qword [gs:abs 58H]; Load pointer to ThreadLocalStorage
7787 // ; from TEB
7788 // mov ecx, dword [rel _tls_index]: Load index (from C runtime)
7789 // mov rcx, qword [rdx+rcx*8]
7790 // mov eax, .tls$:tlsvar
7791 // [rax+rcx] contains the address
7792 // Windows 64bit: gs:0x58
7793 // Windows 32bit: fs:__tls_array
7794
7795 // If GV is an alias then use the aliasee for determining
7796 // thread-localness.
7797 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV))
7798 GV = GA->resolveAliasedGlobal(false);
7799 DebugLoc dl = GA->getDebugLoc();
7800 SDValue Chain = DAG.getEntryNode();
7801
7802 // Get the Thread Pointer, which is %fs:__tls_array (32-bit) or
7803 // %gs:0x58 (64-bit).
7804 Value *Ptr = Constant::getNullValue(Subtarget->is64Bit()
7805 ? Type::getInt8PtrTy(*DAG.getContext(),
7806 256)
7807 : Type::getInt32PtrTy(*DAG.getContext(),
7808 257));
7809
7810 SDValue ThreadPointer = DAG.getLoad(getPointerTy(), dl, Chain,
7811 Subtarget->is64Bit()
7812 ? DAG.getIntPtrConstant(0x58)
7813 : DAG.getExternalSymbol("_tls_array",
7814 getPointerTy()),
7815 MachinePointerInfo(Ptr),
7816 false, false, false, 0);
7817
7818 // Load the _tls_index variable
7819 SDValue IDX = DAG.getExternalSymbol("_tls_index", getPointerTy());
7820 if (Subtarget->is64Bit())
7821 IDX = DAG.getExtLoad(ISD::ZEXTLOAD, dl, getPointerTy(), Chain,
7822 IDX, MachinePointerInfo(), MVT::i32,
7823 false, false, 0);
7824 else
7825 IDX = DAG.getLoad(getPointerTy(), dl, Chain, IDX, MachinePointerInfo(),
7826 false, false, false, 0);
7827
Chandler Carruth426c2bf2012-11-01 09:14:31 +00007828 SDValue Scale = DAG.getConstant(Log2_64_Ceil(TD->getPointerSize()),
Craig Topper0fbf3642012-04-23 03:28:34 +00007829 getPointerTy());
Anton Korobeynikovd4a19b62012-02-11 17:26:53 +00007830 IDX = DAG.getNode(ISD::SHL, dl, getPointerTy(), IDX, Scale);
7831
7832 SDValue res = DAG.getNode(ISD::ADD, dl, getPointerTy(), ThreadPointer, IDX);
7833 res = DAG.getLoad(getPointerTy(), dl, Chain, res, MachinePointerInfo(),
7834 false, false, false, 0);
7835
7836 // Get the offset of start of .tls section
7837 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl,
7838 GA->getValueType(0),
7839 GA->getOffset(), X86II::MO_SECREL);
7840 SDValue Offset = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), TGA);
7841
7842 // The address of the thread local variable is the add of the thread
7843 // pointer with the offset of the variable.
7844 return DAG.getNode(ISD::ADD, dl, getPointerTy(), res, Offset);
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007845 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00007846
David Blaikie4d6ccb52012-01-20 21:51:11 +00007847 llvm_unreachable("TLS not implemented for this target.");
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007848}
7849
Chad Rosierb90d2a92012-01-03 23:19:12 +00007850/// LowerShiftParts - Lower SRA_PARTS and friends, which return two i32 values
7851/// and take a 2 x i32 value to shift plus a shift amount.
7852SDValue X86TargetLowering::LowerShiftParts(SDValue Op, SelectionDAG &DAG) const{
Dan Gohman4c1fa612008-03-03 22:22:09 +00007853 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
Owen Andersone50ed302009-08-10 22:56:29 +00007854 EVT VT = Op.getValueType();
Duncan Sands83ec4b62008-06-06 12:08:01 +00007855 unsigned VTBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007856 DebugLoc dl = Op.getDebugLoc();
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007857 bool isSRA = Op.getOpcode() == ISD::SRA_PARTS;
Dan Gohman475871a2008-07-27 21:46:04 +00007858 SDValue ShOpLo = Op.getOperand(0);
7859 SDValue ShOpHi = Op.getOperand(1);
7860 SDValue ShAmt = Op.getOperand(2);
Chris Lattner31dcfe62009-07-29 05:48:09 +00007861 SDValue Tmp1 = isSRA ? DAG.getNode(ISD::SRA, dl, VT, ShOpHi,
Owen Anderson825b72b2009-08-11 20:47:22 +00007862 DAG.getConstant(VTBits - 1, MVT::i8))
Chris Lattner31dcfe62009-07-29 05:48:09 +00007863 : DAG.getConstant(0, VT);
Evan Chenge3413162006-01-09 18:33:28 +00007864
Dan Gohman475871a2008-07-27 21:46:04 +00007865 SDValue Tmp2, Tmp3;
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007866 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dale Johannesenace16102009-02-03 19:33:06 +00007867 Tmp2 = DAG.getNode(X86ISD::SHLD, dl, VT, ShOpHi, ShOpLo, ShAmt);
7868 Tmp3 = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ShAmt);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007869 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00007870 Tmp2 = DAG.getNode(X86ISD::SHRD, dl, VT, ShOpLo, ShOpHi, ShAmt);
7871 Tmp3 = DAG.getNode(isSRA ? ISD::SRA : ISD::SRL, dl, VT, ShOpHi, ShAmt);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007872 }
Evan Chenge3413162006-01-09 18:33:28 +00007873
Owen Anderson825b72b2009-08-11 20:47:22 +00007874 SDValue AndNode = DAG.getNode(ISD::AND, dl, MVT::i8, ShAmt,
7875 DAG.getConstant(VTBits, MVT::i8));
Chris Lattnerccfea352010-02-22 00:28:59 +00007876 SDValue Cond = DAG.getNode(X86ISD::CMP, dl, MVT::i32,
Owen Anderson825b72b2009-08-11 20:47:22 +00007877 AndNode, DAG.getConstant(0, MVT::i8));
Evan Chenge3413162006-01-09 18:33:28 +00007878
Dan Gohman475871a2008-07-27 21:46:04 +00007879 SDValue Hi, Lo;
Owen Anderson825b72b2009-08-11 20:47:22 +00007880 SDValue CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Dan Gohman475871a2008-07-27 21:46:04 +00007881 SDValue Ops0[4] = { Tmp2, Tmp3, CC, Cond };
7882 SDValue Ops1[4] = { Tmp3, Tmp1, CC, Cond };
Duncan Sandsf9516202008-06-30 10:19:09 +00007883
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007884 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dale Johannesenace16102009-02-03 19:33:06 +00007885 Hi = DAG.getNode(X86ISD::CMOV, dl, VT, Ops0, 4);
7886 Lo = DAG.getNode(X86ISD::CMOV, dl, VT, Ops1, 4);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007887 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00007888 Lo = DAG.getNode(X86ISD::CMOV, dl, VT, Ops0, 4);
7889 Hi = DAG.getNode(X86ISD::CMOV, dl, VT, Ops1, 4);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007890 }
7891
Dan Gohman475871a2008-07-27 21:46:04 +00007892 SDValue Ops[2] = { Lo, Hi };
Dale Johannesenace16102009-02-03 19:33:06 +00007893 return DAG.getMergeValues(Ops, 2, dl);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007894}
Evan Chenga3195e82006-01-12 22:54:21 +00007895
Dan Gohmand858e902010-04-17 15:26:15 +00007896SDValue X86TargetLowering::LowerSINT_TO_FP(SDValue Op,
7897 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00007898 EVT SrcVT = Op.getOperand(0).getValueType();
Eli Friedman23ef1052009-06-06 03:57:58 +00007899
Dale Johannesen0488fb62010-09-30 23:57:10 +00007900 if (SrcVT.isVector())
Eli Friedman23ef1052009-06-06 03:57:58 +00007901 return SDValue();
Eli Friedman23ef1052009-06-06 03:57:58 +00007902
Owen Anderson825b72b2009-08-11 20:47:22 +00007903 assert(SrcVT.getSimpleVT() <= MVT::i64 && SrcVT.getSimpleVT() >= MVT::i16 &&
Chris Lattnerb09916b2008-02-27 05:57:41 +00007904 "Unknown SINT_TO_FP to lower!");
Scott Michelfdc40a02009-02-17 22:15:04 +00007905
Eli Friedman36df4992009-05-27 00:47:34 +00007906 // These are really Legal; return the operand so the caller accepts it as
7907 // Legal.
Owen Anderson825b72b2009-08-11 20:47:22 +00007908 if (SrcVT == MVT::i32 && isScalarFPTypeInSSEReg(Op.getValueType()))
Eli Friedman36df4992009-05-27 00:47:34 +00007909 return Op;
Owen Anderson825b72b2009-08-11 20:47:22 +00007910 if (SrcVT == MVT::i64 && isScalarFPTypeInSSEReg(Op.getValueType()) &&
Eli Friedman36df4992009-05-27 00:47:34 +00007911 Subtarget->is64Bit()) {
7912 return Op;
7913 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007914
Bruno Cardoso Lopesa511b8e2011-08-09 17:39:01 +00007915 DebugLoc dl = Op.getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +00007916 unsigned Size = SrcVT.getSizeInBits()/8;
Evan Cheng0db9fe62006-04-25 20:13:52 +00007917 MachineFunction &MF = DAG.getMachineFunction();
David Greene3f2bf852009-11-12 20:49:22 +00007918 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size, false);
Dan Gohman475871a2008-07-27 21:46:04 +00007919 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Dale Johannesenace16102009-02-03 19:33:06 +00007920 SDValue Chain = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Bill Wendling105be5a2009-03-13 08:41:47 +00007921 StackSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00007922 MachinePointerInfo::getFixedStack(SSFI),
David Greene67c9d422010-02-15 16:53:33 +00007923 false, false, 0);
Eli Friedman948e95a2009-05-23 09:59:16 +00007924 return BuildFILD(Op, SrcVT, Chain, StackSlot, DAG);
7925}
Evan Cheng0db9fe62006-04-25 20:13:52 +00007926
Owen Andersone50ed302009-08-10 22:56:29 +00007927SDValue X86TargetLowering::BuildFILD(SDValue Op, EVT SrcVT, SDValue Chain,
Michael J. Spencerec38de22010-10-10 22:04:20 +00007928 SDValue StackSlot,
Dan Gohmand858e902010-04-17 15:26:15 +00007929 SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00007930 // Build the FILD
Chris Lattner492a43e2010-09-22 01:28:21 +00007931 DebugLoc DL = Op.getDebugLoc();
Chris Lattner5a88b832007-02-25 07:10:00 +00007932 SDVTList Tys;
Chris Lattner78631162008-01-16 06:24:21 +00007933 bool useSSE = isScalarFPTypeInSSEReg(Op.getValueType());
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00007934 if (useSSE)
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00007935 Tys = DAG.getVTList(MVT::f64, MVT::Other, MVT::Glue);
Chris Lattner5a88b832007-02-25 07:10:00 +00007936 else
Owen Anderson825b72b2009-08-11 20:47:22 +00007937 Tys = DAG.getVTList(Op.getValueType(), MVT::Other);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007938
Chris Lattner492a43e2010-09-22 01:28:21 +00007939 unsigned ByteSize = SrcVT.getSizeInBits()/8;
Michael J. Spencerec38de22010-10-10 22:04:20 +00007940
Stuart Hastings84be9582011-06-02 15:57:11 +00007941 FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(StackSlot);
7942 MachineMemOperand *MMO;
7943 if (FI) {
7944 int SSFI = FI->getIndex();
7945 MMO =
7946 DAG.getMachineFunction()
7947 .getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
7948 MachineMemOperand::MOLoad, ByteSize, ByteSize);
7949 } else {
7950 MMO = cast<LoadSDNode>(StackSlot)->getMemOperand();
7951 StackSlot = StackSlot.getOperand(1);
7952 }
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00007953 SDValue Ops[] = { Chain, StackSlot, DAG.getValueType(SrcVT) };
Chris Lattner492a43e2010-09-22 01:28:21 +00007954 SDValue Result = DAG.getMemIntrinsicNode(useSSE ? X86ISD::FILD_FLAG :
7955 X86ISD::FILD, DL,
7956 Tys, Ops, array_lengthof(Ops),
7957 SrcVT, MMO);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007958
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00007959 if (useSSE) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00007960 Chain = Result.getValue(1);
Dan Gohman475871a2008-07-27 21:46:04 +00007961 SDValue InFlag = Result.getValue(2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007962
7963 // FIXME: Currently the FST is flagged to the FILD_FLAG. This
7964 // shouldn't be necessary except that RFP cannot be live across
7965 // multiple blocks. When stackifier is fixed, they can be uncoupled.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00007966 MachineFunction &MF = DAG.getMachineFunction();
Bob Wilsoneafca4e2010-09-22 17:35:14 +00007967 unsigned SSFISize = Op.getValueType().getSizeInBits()/8;
7968 int SSFI = MF.getFrameInfo()->CreateStackObject(SSFISize, SSFISize, false);
Dan Gohman475871a2008-07-27 21:46:04 +00007969 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Owen Anderson825b72b2009-08-11 20:47:22 +00007970 Tys = DAG.getVTList(MVT::Other);
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00007971 SDValue Ops[] = {
7972 Chain, Result, StackSlot, DAG.getValueType(Op.getValueType()), InFlag
7973 };
Chris Lattner492a43e2010-09-22 01:28:21 +00007974 MachineMemOperand *MMO =
7975 DAG.getMachineFunction()
7976 .getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
Bob Wilsoneafca4e2010-09-22 17:35:14 +00007977 MachineMemOperand::MOStore, SSFISize, SSFISize);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007978
Chris Lattner492a43e2010-09-22 01:28:21 +00007979 Chain = DAG.getMemIntrinsicNode(X86ISD::FST, DL, Tys,
7980 Ops, array_lengthof(Ops),
7981 Op.getValueType(), MMO);
7982 Result = DAG.getLoad(Op.getValueType(), DL, Chain, StackSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00007983 MachinePointerInfo::getFixedStack(SSFI),
Pete Cooperd752e0f2011-11-08 18:42:53 +00007984 false, false, false, 0);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00007985 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00007986
Evan Cheng0db9fe62006-04-25 20:13:52 +00007987 return Result;
7988}
7989
Bill Wendling8b8a6362009-01-17 03:56:04 +00007990// LowerUINT_TO_FP_i64 - 64-bit unsigned integer to double expansion.
Dan Gohmand858e902010-04-17 15:26:15 +00007991SDValue X86TargetLowering::LowerUINT_TO_FP_i64(SDValue Op,
7992 SelectionDAG &DAG) const {
Bill Wendling397ae212012-01-05 02:13:20 +00007993 // This algorithm is not obvious. Here it is what we're trying to output:
Bill Wendling8b8a6362009-01-17 03:56:04 +00007994 /*
Bill Wendling397ae212012-01-05 02:13:20 +00007995 movq %rax, %xmm0
7996 punpckldq (c0), %xmm0 // c0: (uint4){ 0x43300000U, 0x45300000U, 0U, 0U }
7997 subpd (c1), %xmm0 // c1: (double2){ 0x1.0p52, 0x1.0p52 * 0x1.0p32 }
7998 #ifdef __SSE3__
Chad Rosiera20e1e72012-08-01 18:39:17 +00007999 haddpd %xmm0, %xmm0
Bill Wendling397ae212012-01-05 02:13:20 +00008000 #else
Chad Rosiera20e1e72012-08-01 18:39:17 +00008001 pshufd $0x4e, %xmm0, %xmm1
Bill Wendling397ae212012-01-05 02:13:20 +00008002 addpd %xmm1, %xmm0
8003 #endif
Bill Wendling8b8a6362009-01-17 03:56:04 +00008004 */
Dale Johannesen040225f2008-10-21 23:07:49 +00008005
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008006 DebugLoc dl = Op.getDebugLoc();
Owen Andersona90b3dc2009-07-15 21:51:10 +00008007 LLVMContext *Context = DAG.getContext();
Dale Johannesenace16102009-02-03 19:33:06 +00008008
Dale Johannesen1c15bf52008-10-21 20:50:01 +00008009 // Build some magic constants.
Chris Lattner7302d802012-02-06 21:56:39 +00008010 const uint32_t CV0[] = { 0x43300000, 0x45300000, 0, 0 };
8011 Constant *C0 = ConstantDataVector::get(*Context, CV0);
Evan Cheng1606e8e2009-03-13 07:51:59 +00008012 SDValue CPIdx0 = DAG.getConstantPool(C0, getPointerTy(), 16);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00008013
Chris Lattner97484792012-01-25 09:56:22 +00008014 SmallVector<Constant*,2> CV1;
8015 CV1.push_back(
Chris Lattner4ca829e2012-01-25 06:02:56 +00008016 ConstantFP::get(*Context, APFloat(APInt(64, 0x4330000000000000ULL))));
Chris Lattner97484792012-01-25 09:56:22 +00008017 CV1.push_back(
8018 ConstantFP::get(*Context, APFloat(APInt(64, 0x4530000000000000ULL))));
8019 Constant *C1 = ConstantVector::get(CV1);
Evan Cheng1606e8e2009-03-13 07:51:59 +00008020 SDValue CPIdx1 = DAG.getConstantPool(C1, getPointerTy(), 16);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00008021
Bill Wendling397ae212012-01-05 02:13:20 +00008022 // Load the 64-bit value into an XMM register.
8023 SDValue XR1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i64,
8024 Op.getOperand(0));
Owen Anderson825b72b2009-08-11 20:47:22 +00008025 SDValue CLod0 = DAG.getLoad(MVT::v4i32, dl, DAG.getEntryNode(), CPIdx0,
Chris Lattnere8639032010-09-21 06:22:23 +00008026 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00008027 false, false, false, 16);
Bill Wendling397ae212012-01-05 02:13:20 +00008028 SDValue Unpck1 = getUnpackl(DAG, dl, MVT::v4i32,
8029 DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, XR1),
8030 CLod0);
8031
Owen Anderson825b72b2009-08-11 20:47:22 +00008032 SDValue CLod1 = DAG.getLoad(MVT::v2f64, dl, CLod0.getValue(1), CPIdx1,
Chris Lattnere8639032010-09-21 06:22:23 +00008033 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00008034 false, false, false, 16);
Bill Wendling397ae212012-01-05 02:13:20 +00008035 SDValue XR2F = DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Unpck1);
Owen Anderson825b72b2009-08-11 20:47:22 +00008036 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::v2f64, XR2F, CLod1);
Bill Wendling397ae212012-01-05 02:13:20 +00008037 SDValue Result;
Bill Wendling8b8a6362009-01-17 03:56:04 +00008038
Craig Topperd0a31172012-01-10 06:37:29 +00008039 if (Subtarget->hasSSE3()) {
Bill Wendling397ae212012-01-05 02:13:20 +00008040 // FIXME: The 'haddpd' instruction may be slower than 'movhlps + addsd'.
8041 Result = DAG.getNode(X86ISD::FHADD, dl, MVT::v2f64, Sub, Sub);
8042 } else {
8043 SDValue S2F = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, Sub);
8044 SDValue Shuffle = getTargetShuffleNode(X86ISD::PSHUFD, dl, MVT::v4i32,
8045 S2F, 0x4E, DAG);
8046 Result = DAG.getNode(ISD::FADD, dl, MVT::v2f64,
8047 DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Shuffle),
8048 Sub);
8049 }
8050
8051 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Result,
Dale Johannesen1c15bf52008-10-21 20:50:01 +00008052 DAG.getIntPtrConstant(0));
8053}
8054
Bill Wendling8b8a6362009-01-17 03:56:04 +00008055// LowerUINT_TO_FP_i32 - 32-bit unsigned integer to float expansion.
Dan Gohmand858e902010-04-17 15:26:15 +00008056SDValue X86TargetLowering::LowerUINT_TO_FP_i32(SDValue Op,
8057 SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008058 DebugLoc dl = Op.getDebugLoc();
Bill Wendling8b8a6362009-01-17 03:56:04 +00008059 // FP constant to bias correct the final result.
8060 SDValue Bias = DAG.getConstantFP(BitsToDouble(0x4330000000000000ULL),
Owen Anderson825b72b2009-08-11 20:47:22 +00008061 MVT::f64);
Bill Wendling8b8a6362009-01-17 03:56:04 +00008062
8063 // Load the 32-bit value into an XMM register.
Owen Anderson825b72b2009-08-11 20:47:22 +00008064 SDValue Load = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
Eli Friedman6cdc1f42011-08-02 18:38:35 +00008065 Op.getOperand(0));
Bill Wendling8b8a6362009-01-17 03:56:04 +00008066
Eli Friedmanf3704762011-08-29 21:15:46 +00008067 // Zero out the upper parts of the register.
Craig Topper12216172012-01-13 08:12:35 +00008068 Load = getShuffleVectorZeroOrUndef(Load, 0, true, Subtarget, DAG);
Eli Friedmanf3704762011-08-29 21:15:46 +00008069
Owen Anderson825b72b2009-08-11 20:47:22 +00008070 Load = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008071 DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Load),
Bill Wendling8b8a6362009-01-17 03:56:04 +00008072 DAG.getIntPtrConstant(0));
8073
8074 // Or the load with the bias.
Owen Anderson825b72b2009-08-11 20:47:22 +00008075 SDValue Or = DAG.getNode(ISD::OR, dl, MVT::v2i64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008076 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00008077 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00008078 MVT::v2f64, Load)),
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008079 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00008080 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00008081 MVT::v2f64, Bias)));
8082 Or = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008083 DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Or),
Bill Wendling8b8a6362009-01-17 03:56:04 +00008084 DAG.getIntPtrConstant(0));
8085
8086 // Subtract the bias.
Owen Anderson825b72b2009-08-11 20:47:22 +00008087 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::f64, Or, Bias);
Bill Wendling8b8a6362009-01-17 03:56:04 +00008088
8089 // Handle final rounding.
Owen Andersone50ed302009-08-10 22:56:29 +00008090 EVT DestVT = Op.getValueType();
Bill Wendling030939c2009-01-17 07:40:19 +00008091
Craig Topper69947b92012-04-23 06:57:04 +00008092 if (DestVT.bitsLT(MVT::f64))
Dale Johannesenace16102009-02-03 19:33:06 +00008093 return DAG.getNode(ISD::FP_ROUND, dl, DestVT, Sub,
Bill Wendling030939c2009-01-17 07:40:19 +00008094 DAG.getIntPtrConstant(0));
Craig Topper69947b92012-04-23 06:57:04 +00008095 if (DestVT.bitsGT(MVT::f64))
Dale Johannesenace16102009-02-03 19:33:06 +00008096 return DAG.getNode(ISD::FP_EXTEND, dl, DestVT, Sub);
Bill Wendling030939c2009-01-17 07:40:19 +00008097
8098 // Handle final rounding.
8099 return Sub;
Bill Wendling8b8a6362009-01-17 03:56:04 +00008100}
8101
Michael Liaoa7554632012-10-23 17:36:08 +00008102SDValue X86TargetLowering::lowerUINT_TO_FP_vec(SDValue Op,
8103 SelectionDAG &DAG) const {
8104 SDValue N0 = Op.getOperand(0);
8105 EVT SVT = N0.getValueType();
8106 DebugLoc dl = Op.getDebugLoc();
8107
8108 assert((SVT == MVT::v4i8 || SVT == MVT::v4i16 ||
8109 SVT == MVT::v8i8 || SVT == MVT::v8i16) &&
8110 "Custom UINT_TO_FP is not supported!");
8111
8112 EVT NVT = EVT::getVectorVT(*DAG.getContext(), MVT::i32, SVT.getVectorNumElements());
8113 return DAG.getNode(ISD::SINT_TO_FP, dl, Op.getValueType(),
8114 DAG.getNode(ISD::ZERO_EXTEND, dl, NVT, N0));
8115}
8116
Dan Gohmand858e902010-04-17 15:26:15 +00008117SDValue X86TargetLowering::LowerUINT_TO_FP(SDValue Op,
8118 SelectionDAG &DAG) const {
Evan Chenga06ec9e2009-01-19 08:08:22 +00008119 SDValue N0 = Op.getOperand(0);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008120 DebugLoc dl = Op.getDebugLoc();
Bill Wendling8b8a6362009-01-17 03:56:04 +00008121
Michael Liaoa7554632012-10-23 17:36:08 +00008122 if (Op.getValueType().isVector())
8123 return lowerUINT_TO_FP_vec(Op, DAG);
8124
Dale Johannesen8d908eb2010-05-15 18:51:12 +00008125 // Since UINT_TO_FP is legal (it's marked custom), dag combiner won't
Evan Chenga06ec9e2009-01-19 08:08:22 +00008126 // optimize it to a SINT_TO_FP when the sign bit is known zero. Perform
8127 // the optimization here.
8128 if (DAG.SignBitIsZero(N0))
Dale Johannesenace16102009-02-03 19:33:06 +00008129 return DAG.getNode(ISD::SINT_TO_FP, dl, Op.getValueType(), N0);
Evan Chenga06ec9e2009-01-19 08:08:22 +00008130
Owen Andersone50ed302009-08-10 22:56:29 +00008131 EVT SrcVT = N0.getValueType();
Dale Johannesen8d908eb2010-05-15 18:51:12 +00008132 EVT DstVT = Op.getValueType();
8133 if (SrcVT == MVT::i64 && DstVT == MVT::f64 && X86ScalarSSEf64)
Bill Wendling8b8a6362009-01-17 03:56:04 +00008134 return LowerUINT_TO_FP_i64(Op, DAG);
Craig Topper69947b92012-04-23 06:57:04 +00008135 if (SrcVT == MVT::i32 && X86ScalarSSEf64)
Bill Wendling8b8a6362009-01-17 03:56:04 +00008136 return LowerUINT_TO_FP_i32(Op, DAG);
Craig Topper69947b92012-04-23 06:57:04 +00008137 if (Subtarget->is64Bit() && SrcVT == MVT::i64 && DstVT == MVT::f32)
Bill Wendling397ae212012-01-05 02:13:20 +00008138 return SDValue();
Eli Friedman948e95a2009-05-23 09:59:16 +00008139
8140 // Make a 64-bit buffer, and use it to build an FILD.
Owen Anderson825b72b2009-08-11 20:47:22 +00008141 SDValue StackSlot = DAG.CreateStackTemporary(MVT::i64);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00008142 if (SrcVT == MVT::i32) {
8143 SDValue WordOff = DAG.getConstant(4, getPointerTy());
8144 SDValue OffsetSlot = DAG.getNode(ISD::ADD, dl,
8145 getPointerTy(), StackSlot, WordOff);
8146 SDValue Store1 = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Chris Lattner8026a9d2010-09-21 17:50:43 +00008147 StackSlot, MachinePointerInfo(),
8148 false, false, 0);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00008149 SDValue Store2 = DAG.getStore(Store1, dl, DAG.getConstant(0, MVT::i32),
Chris Lattner8026a9d2010-09-21 17:50:43 +00008150 OffsetSlot, MachinePointerInfo(),
8151 false, false, 0);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00008152 SDValue Fild = BuildFILD(Op, MVT::i64, Store2, StackSlot, DAG);
8153 return Fild;
8154 }
8155
8156 assert(SrcVT == MVT::i64 && "Unexpected type in UINT_TO_FP");
8157 SDValue Store = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Bill Wendlingf6c07472012-01-10 19:41:30 +00008158 StackSlot, MachinePointerInfo(),
Chris Lattner8026a9d2010-09-21 17:50:43 +00008159 false, false, 0);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00008160 // For i64 source, we need to add the appropriate power of 2 if the input
8161 // was negative. This is the same as the optimization in
8162 // DAGTypeLegalizer::ExpandIntOp_UNIT_TO_FP, and for it to be safe here,
8163 // we must be careful to do the computation in x87 extended precision, not
8164 // in SSE. (The generic code can't know it's OK to do this, or how to.)
Chris Lattner492a43e2010-09-22 01:28:21 +00008165 int SSFI = cast<FrameIndexSDNode>(StackSlot)->getIndex();
8166 MachineMemOperand *MMO =
8167 DAG.getMachineFunction()
8168 .getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
8169 MachineMemOperand::MOLoad, 8, 8);
Michael J. Spencerec38de22010-10-10 22:04:20 +00008170
Dale Johannesen8d908eb2010-05-15 18:51:12 +00008171 SDVTList Tys = DAG.getVTList(MVT::f80, MVT::Other);
8172 SDValue Ops[] = { Store, StackSlot, DAG.getValueType(MVT::i64) };
Chris Lattner492a43e2010-09-22 01:28:21 +00008173 SDValue Fild = DAG.getMemIntrinsicNode(X86ISD::FILD, dl, Tys, Ops, 3,
8174 MVT::i64, MMO);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00008175
8176 APInt FF(32, 0x5F800000ULL);
8177
8178 // Check whether the sign bit is set.
8179 SDValue SignSet = DAG.getSetCC(dl, getSetCCResultType(MVT::i64),
8180 Op.getOperand(0), DAG.getConstant(0, MVT::i64),
8181 ISD::SETLT);
8182
8183 // Build a 64 bit pair (0, FF) in the constant pool, with FF in the lo bits.
8184 SDValue FudgePtr = DAG.getConstantPool(
8185 ConstantInt::get(*DAG.getContext(), FF.zext(64)),
8186 getPointerTy());
8187
8188 // Get a pointer to FF if the sign bit was set, or to 0 otherwise.
8189 SDValue Zero = DAG.getIntPtrConstant(0);
8190 SDValue Four = DAG.getIntPtrConstant(4);
8191 SDValue Offset = DAG.getNode(ISD::SELECT, dl, Zero.getValueType(), SignSet,
8192 Zero, Four);
8193 FudgePtr = DAG.getNode(ISD::ADD, dl, getPointerTy(), FudgePtr, Offset);
8194
8195 // Load the value out, extending it from f32 to f80.
8196 // FIXME: Avoid the extend by constructing the right constant pool?
Stuart Hastingsa9011292011-02-16 16:23:55 +00008197 SDValue Fudge = DAG.getExtLoad(ISD::EXTLOAD, dl, MVT::f80, DAG.getEntryNode(),
Chris Lattnere8639032010-09-21 06:22:23 +00008198 FudgePtr, MachinePointerInfo::getConstantPool(),
8199 MVT::f32, false, false, 4);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00008200 // Extend everything to 80 bits to force it to be done on x87.
8201 SDValue Add = DAG.getNode(ISD::FADD, dl, MVT::f80, Fild, Fudge);
8202 return DAG.getNode(ISD::FP_ROUND, dl, DstVT, Add, DAG.getIntPtrConstant(0));
Bill Wendling8b8a6362009-01-17 03:56:04 +00008203}
8204
Dan Gohman475871a2008-07-27 21:46:04 +00008205std::pair<SDValue,SDValue> X86TargetLowering::
NAKAMURA Takumi9a68fdc2012-02-25 03:37:25 +00008206FP_TO_INTHelper(SDValue Op, SelectionDAG &DAG, bool IsSigned, bool IsReplace) const {
Chris Lattner07290932010-09-22 01:05:16 +00008207 DebugLoc DL = Op.getDebugLoc();
Eli Friedman948e95a2009-05-23 09:59:16 +00008208
Owen Andersone50ed302009-08-10 22:56:29 +00008209 EVT DstTy = Op.getValueType();
Eli Friedman948e95a2009-05-23 09:59:16 +00008210
Michael J. Spencer1a2d0612012-02-24 19:01:22 +00008211 if (!IsSigned && !isIntegerTypeFTOL(DstTy)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00008212 assert(DstTy == MVT::i32 && "Unexpected FP_TO_UINT");
8213 DstTy = MVT::i64;
Eli Friedman948e95a2009-05-23 09:59:16 +00008214 }
8215
Owen Anderson825b72b2009-08-11 20:47:22 +00008216 assert(DstTy.getSimpleVT() <= MVT::i64 &&
8217 DstTy.getSimpleVT() >= MVT::i16 &&
Michael J. Spencer1a2d0612012-02-24 19:01:22 +00008218 "Unknown FP_TO_INT to lower!");
Evan Cheng0db9fe62006-04-25 20:13:52 +00008219
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00008220 // These are really Legal.
Owen Anderson825b72b2009-08-11 20:47:22 +00008221 if (DstTy == MVT::i32 &&
Chris Lattner78631162008-01-16 06:24:21 +00008222 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Dan Gohman475871a2008-07-27 21:46:04 +00008223 return std::make_pair(SDValue(), SDValue());
Dale Johannesen73328d12007-09-19 23:55:34 +00008224 if (Subtarget->is64Bit() &&
Owen Anderson825b72b2009-08-11 20:47:22 +00008225 DstTy == MVT::i64 &&
Eli Friedman36df4992009-05-27 00:47:34 +00008226 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Dan Gohman475871a2008-07-27 21:46:04 +00008227 return std::make_pair(SDValue(), SDValue());
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00008228
Michael J. Spencer1a2d0612012-02-24 19:01:22 +00008229 // We lower FP->int64 either into FISTP64 followed by a load from a temporary
8230 // stack slot, or into the FTOL runtime function.
Evan Cheng87c89352007-10-15 20:11:21 +00008231 MachineFunction &MF = DAG.getMachineFunction();
Eli Friedman948e95a2009-05-23 09:59:16 +00008232 unsigned MemSize = DstTy.getSizeInBits()/8;
David Greene3f2bf852009-11-12 20:49:22 +00008233 int SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize, false);
Dan Gohman475871a2008-07-27 21:46:04 +00008234 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Eric Christopherfd179292009-08-27 18:07:15 +00008235
Evan Cheng0db9fe62006-04-25 20:13:52 +00008236 unsigned Opc;
Michael J. Spencer1a2d0612012-02-24 19:01:22 +00008237 if (!IsSigned && isIntegerTypeFTOL(DstTy))
8238 Opc = X86ISD::WIN_FTOL;
8239 else
8240 switch (DstTy.getSimpleVT().SimpleTy) {
8241 default: llvm_unreachable("Invalid FP_TO_SINT to lower!");
8242 case MVT::i16: Opc = X86ISD::FP_TO_INT16_IN_MEM; break;
8243 case MVT::i32: Opc = X86ISD::FP_TO_INT32_IN_MEM; break;
8244 case MVT::i64: Opc = X86ISD::FP_TO_INT64_IN_MEM; break;
8245 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00008246
Dan Gohman475871a2008-07-27 21:46:04 +00008247 SDValue Chain = DAG.getEntryNode();
8248 SDValue Value = Op.getOperand(0);
Chris Lattner492a43e2010-09-22 01:28:21 +00008249 EVT TheVT = Op.getOperand(0).getValueType();
Michael J. Spencer1a2d0612012-02-24 19:01:22 +00008250 // FIXME This causes a redundant load/store if the SSE-class value is already
8251 // in memory, such as if it is on the callstack.
Chris Lattner492a43e2010-09-22 01:28:21 +00008252 if (isScalarFPTypeInSSEReg(TheVT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00008253 assert(DstTy == MVT::i64 && "Invalid FP_TO_SINT to lower!");
Chris Lattner07290932010-09-22 01:05:16 +00008254 Chain = DAG.getStore(Chain, DL, Value, StackSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00008255 MachinePointerInfo::getFixedStack(SSFI),
David Greene67c9d422010-02-15 16:53:33 +00008256 false, false, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +00008257 SDVTList Tys = DAG.getVTList(Op.getOperand(0).getValueType(), MVT::Other);
Dan Gohman475871a2008-07-27 21:46:04 +00008258 SDValue Ops[] = {
Chris Lattner492a43e2010-09-22 01:28:21 +00008259 Chain, StackSlot, DAG.getValueType(TheVT)
Chris Lattner5a88b832007-02-25 07:10:00 +00008260 };
Michael J. Spencerec38de22010-10-10 22:04:20 +00008261
Chris Lattner492a43e2010-09-22 01:28:21 +00008262 MachineMemOperand *MMO =
8263 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
8264 MachineMemOperand::MOLoad, MemSize, MemSize);
8265 Value = DAG.getMemIntrinsicNode(X86ISD::FLD, DL, Tys, Ops, 3,
8266 DstTy, MMO);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008267 Chain = Value.getValue(1);
David Greene3f2bf852009-11-12 20:49:22 +00008268 SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize, false);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008269 StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
8270 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00008271
Chris Lattner07290932010-09-22 01:05:16 +00008272 MachineMemOperand *MMO =
8273 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
8274 MachineMemOperand::MOStore, MemSize, MemSize);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00008275
Michael J. Spencer1a2d0612012-02-24 19:01:22 +00008276 if (Opc != X86ISD::WIN_FTOL) {
8277 // Build the FP_TO_INT*_IN_MEM
8278 SDValue Ops[] = { Chain, Value, StackSlot };
8279 SDValue FIST = DAG.getMemIntrinsicNode(Opc, DL, DAG.getVTList(MVT::Other),
8280 Ops, 3, DstTy, MMO);
8281 return std::make_pair(FIST, StackSlot);
8282 } else {
8283 SDValue ftol = DAG.getNode(X86ISD::WIN_FTOL, DL,
8284 DAG.getVTList(MVT::Other, MVT::Glue),
8285 Chain, Value);
8286 SDValue eax = DAG.getCopyFromReg(ftol, DL, X86::EAX,
8287 MVT::i32, ftol.getValue(1));
8288 SDValue edx = DAG.getCopyFromReg(eax.getValue(1), DL, X86::EDX,
8289 MVT::i32, eax.getValue(2));
NAKAMURA Takumi9a68fdc2012-02-25 03:37:25 +00008290 SDValue Ops[] = { eax, edx };
8291 SDValue pair = IsReplace
8292 ? DAG.getNode(ISD::BUILD_PAIR, DL, MVT::i64, Ops, 2)
8293 : DAG.getMergeValues(Ops, 2, DL);
Michael J. Spencer1a2d0612012-02-24 19:01:22 +00008294 return std::make_pair(pair, SDValue());
8295 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00008296}
8297
Nadav Rotem0509db22012-12-28 05:45:24 +00008298static SDValue LowerAVXExtend(SDValue Op, SelectionDAG &DAG,
8299 const X86Subtarget *Subtarget) {
8300 EVT VT = Op->getValueType(0);
8301 SDValue In = Op->getOperand(0);
8302 EVT InVT = In.getValueType();
8303 DebugLoc dl = Op->getDebugLoc();
8304
8305 // Optimize vectors in AVX mode:
8306 //
8307 // v8i16 -> v8i32
8308 // Use vpunpcklwd for 4 lower elements v8i16 -> v4i32.
8309 // Use vpunpckhwd for 4 upper elements v8i16 -> v4i32.
8310 // Concat upper and lower parts.
8311 //
8312 // v4i32 -> v4i64
8313 // Use vpunpckldq for 4 lower elements v4i32 -> v2i64.
8314 // Use vpunpckhdq for 4 upper elements v4i32 -> v2i64.
8315 // Concat upper and lower parts.
8316 //
8317
8318 if (((VT != MVT::v8i32) || (InVT != MVT::v8i16)) &&
8319 ((VT != MVT::v4i64) || (InVT != MVT::v4i32)))
8320 return SDValue();
8321
8322 if (Subtarget->hasInt256())
8323 return DAG.getNode(X86ISD::VZEXT_MOVL, dl, VT, In);
8324
8325 SDValue ZeroVec = getZeroVector(InVT, Subtarget, DAG, dl);
8326 SDValue Undef = DAG.getUNDEF(InVT);
8327 bool NeedZero = Op.getOpcode() == ISD::ZERO_EXTEND;
8328 SDValue OpLo = getUnpackl(DAG, dl, InVT, In, NeedZero ? ZeroVec : Undef);
8329 SDValue OpHi = getUnpackh(DAG, dl, InVT, In, NeedZero ? ZeroVec : Undef);
8330
8331 EVT HVT = EVT::getVectorVT(*DAG.getContext(), VT.getVectorElementType(),
8332 VT.getVectorNumElements()/2);
8333
8334 OpLo = DAG.getNode(ISD::BITCAST, dl, HVT, OpLo);
8335 OpHi = DAG.getNode(ISD::BITCAST, dl, HVT, OpHi);
8336
8337 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT, OpLo, OpHi);
8338}
8339
8340SDValue X86TargetLowering::LowerANY_EXTEND(SDValue Op,
8341 SelectionDAG &DAG) const {
8342 if (Subtarget->hasFp256()) {
8343 SDValue Res = LowerAVXExtend(Op, DAG, Subtarget);
8344 if (Res.getNode())
8345 return Res;
8346 }
8347
8348 return SDValue();
8349}
Nadav Rotem40ef8b72012-12-28 07:28:43 +00008350SDValue X86TargetLowering::LowerZERO_EXTEND(SDValue Op,
8351 SelectionDAG &DAG) const {
Michael Liaoa7554632012-10-23 17:36:08 +00008352 DebugLoc DL = Op.getDebugLoc();
8353 EVT VT = Op.getValueType();
8354 SDValue In = Op.getOperand(0);
8355 EVT SVT = In.getValueType();
8356
Nadav Rotem0509db22012-12-28 05:45:24 +00008357 if (Subtarget->hasFp256()) {
8358 SDValue Res = LowerAVXExtend(Op, DAG, Subtarget);
8359 if (Res.getNode())
8360 return Res;
8361 }
8362
Michael Liaoa7554632012-10-23 17:36:08 +00008363 if (!VT.is256BitVector() || !SVT.is128BitVector() ||
8364 VT.getVectorNumElements() != SVT.getVectorNumElements())
8365 return SDValue();
8366
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00008367 assert(Subtarget->hasFp256() && "256-bit vector is observed without AVX!");
Michael Liaoa7554632012-10-23 17:36:08 +00008368
8369 // AVX2 has better support of integer extending.
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00008370 if (Subtarget->hasInt256())
Michael Liaoa7554632012-10-23 17:36:08 +00008371 return DAG.getNode(X86ISD::VZEXT, DL, VT, In);
8372
8373 SDValue Lo = DAG.getNode(X86ISD::VZEXT, DL, MVT::v4i32, In);
8374 static const int Mask[] = {4, 5, 6, 7, -1, -1, -1, -1};
8375 SDValue Hi = DAG.getNode(X86ISD::VZEXT, DL, MVT::v4i32,
Nadav Rotem40ef8b72012-12-28 07:28:43 +00008376 DAG.getVectorShuffle(MVT::v8i16, DL, In,
8377 DAG.getUNDEF(MVT::v8i16),
8378 &Mask[0]));
Michael Liaoa7554632012-10-23 17:36:08 +00008379
8380 return DAG.getNode(ISD::CONCAT_VECTORS, DL, MVT::v8i32, Lo, Hi);
8381}
8382
Michael Liaobedcbd42012-10-16 18:14:11 +00008383SDValue X86TargetLowering::lowerTRUNCATE(SDValue Op, SelectionDAG &DAG) const {
8384 DebugLoc DL = Op.getDebugLoc();
8385 EVT VT = Op.getValueType();
Nadav Rotem3c22a442012-12-27 07:45:10 +00008386 SDValue In = Op.getOperand(0);
8387 EVT SVT = In.getValueType();
Michael Liaobedcbd42012-10-16 18:14:11 +00008388
Nadav Rotem3c22a442012-12-27 07:45:10 +00008389 if ((VT == MVT::v4i32) && (SVT == MVT::v4i64)) {
8390 // On AVX2, v4i64 -> v4i32 becomes VPERMD.
8391 if (Subtarget->hasInt256()) {
8392 static const int ShufMask[] = {0, 2, 4, 6, -1, -1, -1, -1};
8393 In = DAG.getNode(ISD::BITCAST, DL, MVT::v8i32, In);
8394 In = DAG.getVectorShuffle(MVT::v8i32, DL, In, DAG.getUNDEF(MVT::v8i32),
8395 ShufMask);
8396 return DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, VT, In,
8397 DAG.getIntPtrConstant(0));
8398 }
8399
8400 // On AVX, v4i64 -> v4i32 becomes a sequence that uses PSHUFD and MOVLHPS.
8401 SDValue OpLo = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, MVT::v2i64, In,
8402 DAG.getIntPtrConstant(0));
8403 SDValue OpHi = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, MVT::v2i64, In,
8404 DAG.getIntPtrConstant(2));
8405
8406 OpLo = DAG.getNode(ISD::BITCAST, DL, MVT::v4i32, OpLo);
8407 OpHi = DAG.getNode(ISD::BITCAST, DL, MVT::v4i32, OpHi);
8408
8409 // The PSHUFD mask:
8410 static const int ShufMask1[] = {0, 2, 0, 0};
8411 SDValue Undef = DAG.getUNDEF(VT);
8412 OpLo = DAG.getVectorShuffle(VT, DL, OpLo, Undef, ShufMask1);
8413 OpHi = DAG.getVectorShuffle(VT, DL, OpHi, Undef, ShufMask1);
8414
8415 // The MOVLHPS mask:
8416 static const int ShufMask2[] = {0, 1, 4, 5};
8417 return DAG.getVectorShuffle(VT, DL, OpLo, OpHi, ShufMask2);
8418 }
8419
8420 if ((VT == MVT::v8i16) && (SVT == MVT::v8i32)) {
8421 // On AVX2, v8i32 -> v8i16 becomed PSHUFB.
8422 if (Subtarget->hasInt256()) {
8423 In = DAG.getNode(ISD::BITCAST, DL, MVT::v32i8, In);
8424
8425 SmallVector<SDValue,32> pshufbMask;
8426 for (unsigned i = 0; i < 2; ++i) {
8427 pshufbMask.push_back(DAG.getConstant(0x0, MVT::i8));
8428 pshufbMask.push_back(DAG.getConstant(0x1, MVT::i8));
8429 pshufbMask.push_back(DAG.getConstant(0x4, MVT::i8));
8430 pshufbMask.push_back(DAG.getConstant(0x5, MVT::i8));
8431 pshufbMask.push_back(DAG.getConstant(0x8, MVT::i8));
8432 pshufbMask.push_back(DAG.getConstant(0x9, MVT::i8));
8433 pshufbMask.push_back(DAG.getConstant(0xc, MVT::i8));
8434 pshufbMask.push_back(DAG.getConstant(0xd, MVT::i8));
8435 for (unsigned j = 0; j < 8; ++j)
8436 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
8437 }
8438 SDValue BV = DAG.getNode(ISD::BUILD_VECTOR, DL, MVT::v32i8,
8439 &pshufbMask[0], 32);
8440 In = DAG.getNode(X86ISD::PSHUFB, DL, MVT::v32i8, In, BV);
8441 In = DAG.getNode(ISD::BITCAST, DL, MVT::v4i64, In);
8442
8443 static const int ShufMask[] = {0, 2, -1, -1};
8444 In = DAG.getVectorShuffle(MVT::v4i64, DL, In, DAG.getUNDEF(MVT::v4i64),
8445 &ShufMask[0]);
8446 In = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, MVT::v2i64, In,
8447 DAG.getIntPtrConstant(0));
8448 return DAG.getNode(ISD::BITCAST, DL, VT, In);
8449 }
8450
8451 SDValue OpLo = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, MVT::v4i32, In,
8452 DAG.getIntPtrConstant(0));
8453
8454 SDValue OpHi = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, MVT::v4i32, In,
8455 DAG.getIntPtrConstant(4));
8456
8457 OpLo = DAG.getNode(ISD::BITCAST, DL, MVT::v16i8, OpLo);
8458 OpHi = DAG.getNode(ISD::BITCAST, DL, MVT::v16i8, OpHi);
8459
8460 // The PSHUFB mask:
8461 static const int ShufMask1[] = {0, 1, 4, 5, 8, 9, 12, 13,
8462 -1, -1, -1, -1, -1, -1, -1, -1};
8463
8464 SDValue Undef = DAG.getUNDEF(MVT::v16i8);
8465 OpLo = DAG.getVectorShuffle(MVT::v16i8, DL, OpLo, Undef, ShufMask1);
8466 OpHi = DAG.getVectorShuffle(MVT::v16i8, DL, OpHi, Undef, ShufMask1);
8467
8468 OpLo = DAG.getNode(ISD::BITCAST, DL, MVT::v4i32, OpLo);
8469 OpHi = DAG.getNode(ISD::BITCAST, DL, MVT::v4i32, OpHi);
8470
8471 // The MOVLHPS Mask:
8472 static const int ShufMask2[] = {0, 1, 4, 5};
8473 SDValue res = DAG.getVectorShuffle(MVT::v4i32, DL, OpLo, OpHi, ShufMask2);
8474 return DAG.getNode(ISD::BITCAST, DL, MVT::v8i16, res);
8475 }
8476
8477 // Handle truncation of V256 to V128 using shuffles.
8478 if (!VT.is128BitVector() || !SVT.is256BitVector())
Michael Liaobedcbd42012-10-16 18:14:11 +00008479 return SDValue();
8480
Nadav Rotem3c22a442012-12-27 07:45:10 +00008481 assert(VT.getVectorNumElements() != SVT.getVectorNumElements() &&
8482 "Invalid op");
8483 assert(Subtarget->hasFp256() && "256-bit vector without AVX!");
Michael Liaobedcbd42012-10-16 18:14:11 +00008484
8485 unsigned NumElems = VT.getVectorNumElements();
8486 EVT NVT = EVT::getVectorVT(*DAG.getContext(), VT.getVectorElementType(),
8487 NumElems * 2);
8488
Michael Liaobedcbd42012-10-16 18:14:11 +00008489 SmallVector<int, 16> MaskVec(NumElems * 2, -1);
8490 // Prepare truncation shuffle mask
8491 for (unsigned i = 0; i != NumElems; ++i)
8492 MaskVec[i] = i * 2;
8493 SDValue V = DAG.getVectorShuffle(NVT, DL,
8494 DAG.getNode(ISD::BITCAST, DL, NVT, In),
8495 DAG.getUNDEF(NVT), &MaskVec[0]);
8496 return DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, VT, V,
8497 DAG.getIntPtrConstant(0));
8498}
8499
Dan Gohmand858e902010-04-17 15:26:15 +00008500SDValue X86TargetLowering::LowerFP_TO_SINT(SDValue Op,
8501 SelectionDAG &DAG) const {
Michael Liaobedcbd42012-10-16 18:14:11 +00008502 if (Op.getValueType().isVector()) {
8503 if (Op.getValueType() == MVT::v8i16)
8504 return DAG.getNode(ISD::TRUNCATE, Op.getDebugLoc(), Op.getValueType(),
8505 DAG.getNode(ISD::FP_TO_SINT, Op.getDebugLoc(),
8506 MVT::v8i32, Op.getOperand(0)));
Eli Friedman23ef1052009-06-06 03:57:58 +00008507 return SDValue();
Michael Liaobedcbd42012-10-16 18:14:11 +00008508 }
Eli Friedman23ef1052009-06-06 03:57:58 +00008509
NAKAMURA Takumi9a68fdc2012-02-25 03:37:25 +00008510 std::pair<SDValue,SDValue> Vals = FP_TO_INTHelper(Op, DAG,
8511 /*IsSigned=*/ true, /*IsReplace=*/ false);
Dan Gohman475871a2008-07-27 21:46:04 +00008512 SDValue FIST = Vals.first, StackSlot = Vals.second;
Eli Friedman36df4992009-05-27 00:47:34 +00008513 // If FP_TO_INTHelper failed, the node is actually supposed to be Legal.
8514 if (FIST.getNode() == 0) return Op;
Scott Michelfdc40a02009-02-17 22:15:04 +00008515
Michael J. Spencer1a2d0612012-02-24 19:01:22 +00008516 if (StackSlot.getNode())
8517 // Load the result.
8518 return DAG.getLoad(Op.getValueType(), Op.getDebugLoc(),
8519 FIST, StackSlot, MachinePointerInfo(),
8520 false, false, false, 0);
Craig Topper69947b92012-04-23 06:57:04 +00008521
8522 // The node is the result.
8523 return FIST;
Chris Lattner27a6c732007-11-24 07:07:01 +00008524}
8525
Dan Gohmand858e902010-04-17 15:26:15 +00008526SDValue X86TargetLowering::LowerFP_TO_UINT(SDValue Op,
8527 SelectionDAG &DAG) const {
NAKAMURA Takumi9a68fdc2012-02-25 03:37:25 +00008528 std::pair<SDValue,SDValue> Vals = FP_TO_INTHelper(Op, DAG,
8529 /*IsSigned=*/ false, /*IsReplace=*/ false);
Eli Friedman948e95a2009-05-23 09:59:16 +00008530 SDValue FIST = Vals.first, StackSlot = Vals.second;
8531 assert(FIST.getNode() && "Unexpected failure");
8532
NAKAMURA Takumi9a68fdc2012-02-25 03:37:25 +00008533 if (StackSlot.getNode())
8534 // Load the result.
8535 return DAG.getLoad(Op.getValueType(), Op.getDebugLoc(),
8536 FIST, StackSlot, MachinePointerInfo(),
8537 false, false, false, 0);
Craig Topper69947b92012-04-23 06:57:04 +00008538
8539 // The node is the result.
8540 return FIST;
Eli Friedman948e95a2009-05-23 09:59:16 +00008541}
8542
Michael Liao9d796db2012-10-10 16:32:15 +00008543SDValue X86TargetLowering::lowerFP_EXTEND(SDValue Op,
8544 SelectionDAG &DAG) const {
8545 DebugLoc DL = Op.getDebugLoc();
8546 EVT VT = Op.getValueType();
8547 SDValue In = Op.getOperand(0);
8548 EVT SVT = In.getValueType();
8549
8550 assert(SVT == MVT::v2f32 && "Only customize MVT::v2f32 type legalization!");
8551
8552 return DAG.getNode(X86ISD::VFPEXT, DL, VT,
8553 DAG.getNode(ISD::CONCAT_VECTORS, DL, MVT::v4f32,
8554 In, DAG.getUNDEF(SVT)));
8555}
8556
Craig Topper43620672012-09-08 07:31:51 +00008557SDValue X86TargetLowering::LowerFABS(SDValue Op, SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00008558 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008559 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00008560 EVT VT = Op.getValueType();
8561 EVT EltVT = VT;
Craig Topper43620672012-09-08 07:31:51 +00008562 unsigned NumElts = VT == MVT::f64 ? 2 : 4;
8563 if (VT.isVector()) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00008564 EltVT = VT.getVectorElementType();
Craig Topper43620672012-09-08 07:31:51 +00008565 NumElts = VT.getVectorNumElements();
Evan Cheng0db9fe62006-04-25 20:13:52 +00008566 }
Craig Topper43620672012-09-08 07:31:51 +00008567 Constant *C;
8568 if (EltVT == MVT::f64)
8569 C = ConstantFP::get(*Context, APFloat(APInt(64, ~(1ULL << 63))));
8570 else
8571 C = ConstantFP::get(*Context, APFloat(APInt(32, ~(1U << 31))));
8572 C = ConstantVector::getSplat(NumElts, C);
8573 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy());
8574 unsigned Alignment = cast<ConstantPoolSDNode>(CPIdx)->getAlignment();
Dale Johannesenace16102009-02-03 19:33:06 +00008575 SDValue Mask = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00008576 MachinePointerInfo::getConstantPool(),
Craig Topper43620672012-09-08 07:31:51 +00008577 false, false, false, Alignment);
8578 if (VT.isVector()) {
8579 MVT ANDVT = VT.is128BitVector() ? MVT::v2i64 : MVT::v4i64;
8580 return DAG.getNode(ISD::BITCAST, dl, VT,
8581 DAG.getNode(ISD::AND, dl, ANDVT,
8582 DAG.getNode(ISD::BITCAST, dl, ANDVT,
8583 Op.getOperand(0)),
8584 DAG.getNode(ISD::BITCAST, dl, ANDVT, Mask)));
8585 }
Dale Johannesenace16102009-02-03 19:33:06 +00008586 return DAG.getNode(X86ISD::FAND, dl, VT, Op.getOperand(0), Mask);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008587}
8588
Dan Gohmand858e902010-04-17 15:26:15 +00008589SDValue X86TargetLowering::LowerFNEG(SDValue Op, SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00008590 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008591 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00008592 EVT VT = Op.getValueType();
8593 EVT EltVT = VT;
Chad Rosiera860b182011-12-15 01:02:25 +00008594 unsigned NumElts = VT == MVT::f64 ? 2 : 4;
8595 if (VT.isVector()) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00008596 EltVT = VT.getVectorElementType();
Chad Rosiera860b182011-12-15 01:02:25 +00008597 NumElts = VT.getVectorNumElements();
8598 }
Chris Lattner4ca829e2012-01-25 06:02:56 +00008599 Constant *C;
8600 if (EltVT == MVT::f64)
8601 C = ConstantFP::get(*Context, APFloat(APInt(64, 1ULL << 63)));
8602 else
8603 C = ConstantFP::get(*Context, APFloat(APInt(32, 1U << 31)));
8604 C = ConstantVector::getSplat(NumElts, C);
Craig Toppercacd9d62012-09-08 07:46:05 +00008605 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy());
8606 unsigned Alignment = cast<ConstantPoolSDNode>(CPIdx)->getAlignment();
Dale Johannesenace16102009-02-03 19:33:06 +00008607 SDValue Mask = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00008608 MachinePointerInfo::getConstantPool(),
Craig Toppercacd9d62012-09-08 07:46:05 +00008609 false, false, false, Alignment);
Duncan Sands83ec4b62008-06-06 12:08:01 +00008610 if (VT.isVector()) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00008611 MVT XORVT = VT.is128BitVector() ? MVT::v2i64 : MVT::v4i64;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008612 return DAG.getNode(ISD::BITCAST, dl, VT,
Chad Rosiera860b182011-12-15 01:02:25 +00008613 DAG.getNode(ISD::XOR, dl, XORVT,
Craig Topper69947b92012-04-23 06:57:04 +00008614 DAG.getNode(ISD::BITCAST, dl, XORVT,
8615 Op.getOperand(0)),
8616 DAG.getNode(ISD::BITCAST, dl, XORVT, Mask)));
Evan Chengd4d01b72007-07-19 23:36:01 +00008617 }
Craig Topper69947b92012-04-23 06:57:04 +00008618
8619 return DAG.getNode(X86ISD::FXOR, dl, VT, Op.getOperand(0), Mask);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008620}
8621
Dan Gohmand858e902010-04-17 15:26:15 +00008622SDValue X86TargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00008623 LLVMContext *Context = DAG.getContext();
Dan Gohman475871a2008-07-27 21:46:04 +00008624 SDValue Op0 = Op.getOperand(0);
8625 SDValue Op1 = Op.getOperand(1);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008626 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00008627 EVT VT = Op.getValueType();
8628 EVT SrcVT = Op1.getValueType();
Evan Cheng73d6cf12007-01-05 21:37:56 +00008629
8630 // If second operand is smaller, extend it first.
Duncan Sands8e4eb092008-06-08 20:54:56 +00008631 if (SrcVT.bitsLT(VT)) {
Dale Johannesenace16102009-02-03 19:33:06 +00008632 Op1 = DAG.getNode(ISD::FP_EXTEND, dl, VT, Op1);
Evan Cheng73d6cf12007-01-05 21:37:56 +00008633 SrcVT = VT;
8634 }
Dale Johannesen61c7ef32007-10-21 01:07:44 +00008635 // And if it is bigger, shrink it first.
Duncan Sands8e4eb092008-06-08 20:54:56 +00008636 if (SrcVT.bitsGT(VT)) {
Dale Johannesenace16102009-02-03 19:33:06 +00008637 Op1 = DAG.getNode(ISD::FP_ROUND, dl, VT, Op1, DAG.getIntPtrConstant(1));
Dale Johannesen61c7ef32007-10-21 01:07:44 +00008638 SrcVT = VT;
Dale Johannesen61c7ef32007-10-21 01:07:44 +00008639 }
8640
8641 // At this point the operands and the result should have the same
8642 // type, and that won't be f80 since that is not custom lowered.
Evan Cheng73d6cf12007-01-05 21:37:56 +00008643
Evan Cheng68c47cb2007-01-05 07:55:56 +00008644 // First get the sign bit of second operand.
Chad Rosier01d426e2011-12-15 01:16:09 +00008645 SmallVector<Constant*,4> CV;
Owen Anderson825b72b2009-08-11 20:47:22 +00008646 if (SrcVT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00008647 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 1ULL << 63))));
8648 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 0))));
Evan Cheng68c47cb2007-01-05 07:55:56 +00008649 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00008650 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 1U << 31))));
8651 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
8652 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
8653 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
Evan Cheng68c47cb2007-01-05 07:55:56 +00008654 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00008655 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00008656 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00008657 SDValue Mask1 = DAG.getLoad(SrcVT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00008658 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00008659 false, false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00008660 SDValue SignBit = DAG.getNode(X86ISD::FAND, dl, SrcVT, Op1, Mask1);
Evan Cheng68c47cb2007-01-05 07:55:56 +00008661
8662 // Shift sign bit right or left if the two operands have different types.
Duncan Sands8e4eb092008-06-08 20:54:56 +00008663 if (SrcVT.bitsGT(VT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00008664 // Op0 is MVT::f32, Op1 is MVT::f64.
8665 SignBit = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f64, SignBit);
8666 SignBit = DAG.getNode(X86ISD::FSRL, dl, MVT::v2f64, SignBit,
8667 DAG.getConstant(32, MVT::i32));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008668 SignBit = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, SignBit);
Owen Anderson825b72b2009-08-11 20:47:22 +00008669 SignBit = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f32, SignBit,
Chris Lattner0bd48932008-01-17 07:00:52 +00008670 DAG.getIntPtrConstant(0));
Evan Cheng68c47cb2007-01-05 07:55:56 +00008671 }
8672
Evan Cheng73d6cf12007-01-05 21:37:56 +00008673 // Clear first operand sign bit.
8674 CV.clear();
Owen Anderson825b72b2009-08-11 20:47:22 +00008675 if (VT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00008676 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, ~(1ULL << 63)))));
8677 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 0))));
Evan Cheng73d6cf12007-01-05 21:37:56 +00008678 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00008679 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, ~(1U << 31)))));
8680 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
8681 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
8682 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
Evan Cheng73d6cf12007-01-05 21:37:56 +00008683 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00008684 C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00008685 CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00008686 SDValue Mask2 = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00008687 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00008688 false, false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00008689 SDValue Val = DAG.getNode(X86ISD::FAND, dl, VT, Op0, Mask2);
Evan Cheng73d6cf12007-01-05 21:37:56 +00008690
8691 // Or the value with the sign bit.
Dale Johannesenace16102009-02-03 19:33:06 +00008692 return DAG.getNode(X86ISD::FOR, dl, VT, Val, SignBit);
Evan Cheng68c47cb2007-01-05 07:55:56 +00008693}
8694
Craig Topper55b24052012-09-11 06:15:32 +00008695static SDValue LowerFGETSIGN(SDValue Op, SelectionDAG &DAG) {
Stuart Hastings4fd0dee2011-06-01 04:39:42 +00008696 SDValue N0 = Op.getOperand(0);
8697 DebugLoc dl = Op.getDebugLoc();
8698 EVT VT = Op.getValueType();
8699
8700 // Lower ISD::FGETSIGN to (AND (X86ISD::FGETSIGNx86 ...) 1).
8701 SDValue xFGETSIGN = DAG.getNode(X86ISD::FGETSIGNx86, dl, VT, N0,
8702 DAG.getConstant(1, VT));
8703 return DAG.getNode(ISD::AND, dl, VT, xFGETSIGN, DAG.getConstant(1, VT));
8704}
8705
Michael Liaof966e4e2012-09-13 20:24:54 +00008706// LowerVectorAllZeroTest - Check whether an OR'd tree is PTEST-able.
8707//
8708SDValue X86TargetLowering::LowerVectorAllZeroTest(SDValue Op, SelectionDAG &DAG) const {
8709 assert(Op.getOpcode() == ISD::OR && "Only check OR'd tree.");
8710
8711 if (!Subtarget->hasSSE41())
8712 return SDValue();
8713
8714 if (!Op->hasOneUse())
8715 return SDValue();
8716
8717 SDNode *N = Op.getNode();
8718 DebugLoc DL = N->getDebugLoc();
8719
8720 SmallVector<SDValue, 8> Opnds;
8721 DenseMap<SDValue, unsigned> VecInMap;
8722 EVT VT = MVT::Other;
8723
8724 // Recognize a special case where a vector is casted into wide integer to
8725 // test all 0s.
8726 Opnds.push_back(N->getOperand(0));
8727 Opnds.push_back(N->getOperand(1));
8728
8729 for (unsigned Slot = 0, e = Opnds.size(); Slot < e; ++Slot) {
8730 SmallVector<SDValue, 8>::const_iterator I = Opnds.begin() + Slot;
8731 // BFS traverse all OR'd operands.
8732 if (I->getOpcode() == ISD::OR) {
8733 Opnds.push_back(I->getOperand(0));
8734 Opnds.push_back(I->getOperand(1));
8735 // Re-evaluate the number of nodes to be traversed.
8736 e += 2; // 2 more nodes (LHS and RHS) are pushed.
8737 continue;
8738 }
8739
8740 // Quit if a non-EXTRACT_VECTOR_ELT
8741 if (I->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
8742 return SDValue();
8743
8744 // Quit if without a constant index.
8745 SDValue Idx = I->getOperand(1);
8746 if (!isa<ConstantSDNode>(Idx))
8747 return SDValue();
8748
8749 SDValue ExtractedFromVec = I->getOperand(0);
8750 DenseMap<SDValue, unsigned>::iterator M = VecInMap.find(ExtractedFromVec);
8751 if (M == VecInMap.end()) {
8752 VT = ExtractedFromVec.getValueType();
8753 // Quit if not 128/256-bit vector.
8754 if (!VT.is128BitVector() && !VT.is256BitVector())
8755 return SDValue();
8756 // Quit if not the same type.
8757 if (VecInMap.begin() != VecInMap.end() &&
8758 VT != VecInMap.begin()->first.getValueType())
8759 return SDValue();
8760 M = VecInMap.insert(std::make_pair(ExtractedFromVec, 0)).first;
8761 }
8762 M->second |= 1U << cast<ConstantSDNode>(Idx)->getZExtValue();
8763 }
8764
8765 assert((VT.is128BitVector() || VT.is256BitVector()) &&
Michael Liao9aba7ea2012-09-13 20:30:16 +00008766 "Not extracted from 128-/256-bit vector.");
Michael Liaof966e4e2012-09-13 20:24:54 +00008767
8768 unsigned FullMask = (1U << VT.getVectorNumElements()) - 1U;
8769 SmallVector<SDValue, 8> VecIns;
8770
8771 for (DenseMap<SDValue, unsigned>::const_iterator
8772 I = VecInMap.begin(), E = VecInMap.end(); I != E; ++I) {
8773 // Quit if not all elements are used.
8774 if (I->second != FullMask)
8775 return SDValue();
8776 VecIns.push_back(I->first);
8777 }
8778
8779 EVT TestVT = VT.is128BitVector() ? MVT::v2i64 : MVT::v4i64;
8780
8781 // Cast all vectors into TestVT for PTEST.
8782 for (unsigned i = 0, e = VecIns.size(); i < e; ++i)
8783 VecIns[i] = DAG.getNode(ISD::BITCAST, DL, TestVT, VecIns[i]);
8784
8785 // If more than one full vectors are evaluated, OR them first before PTEST.
8786 for (unsigned Slot = 0, e = VecIns.size(); e - Slot > 1; Slot += 2, e += 1) {
8787 // Each iteration will OR 2 nodes and append the result until there is only
8788 // 1 node left, i.e. the final OR'd value of all vectors.
8789 SDValue LHS = VecIns[Slot];
8790 SDValue RHS = VecIns[Slot + 1];
8791 VecIns.push_back(DAG.getNode(ISD::OR, DL, TestVT, LHS, RHS));
8792 }
8793
8794 return DAG.getNode(X86ISD::PTEST, DL, MVT::i32,
8795 VecIns.back(), VecIns.back());
8796}
8797
Dan Gohman076aee32009-03-04 19:44:21 +00008798/// Emit nodes that will be selected as "test Op0,Op0", or something
8799/// equivalent.
Dan Gohman31125812009-03-07 01:58:32 +00008800SDValue X86TargetLowering::EmitTest(SDValue Op, unsigned X86CC,
Evan Cheng552f09a2010-04-26 19:06:11 +00008801 SelectionDAG &DAG) const {
Dan Gohman076aee32009-03-04 19:44:21 +00008802 DebugLoc dl = Op.getDebugLoc();
8803
Dan Gohman31125812009-03-07 01:58:32 +00008804 // CF and OF aren't always set the way we want. Determine which
8805 // of these we need.
8806 bool NeedCF = false;
8807 bool NeedOF = false;
8808 switch (X86CC) {
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008809 default: break;
Dan Gohman31125812009-03-07 01:58:32 +00008810 case X86::COND_A: case X86::COND_AE:
8811 case X86::COND_B: case X86::COND_BE:
8812 NeedCF = true;
8813 break;
8814 case X86::COND_G: case X86::COND_GE:
8815 case X86::COND_L: case X86::COND_LE:
8816 case X86::COND_O: case X86::COND_NO:
8817 NeedOF = true;
8818 break;
Dan Gohman31125812009-03-07 01:58:32 +00008819 }
8820
Dan Gohman076aee32009-03-04 19:44:21 +00008821 // See if we can use the EFLAGS value from the operand instead of
Dan Gohman31125812009-03-07 01:58:32 +00008822 // doing a separate TEST. TEST always sets OF and CF to 0, so unless
8823 // we prove that the arithmetic won't overflow, we can't use OF or CF.
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008824 if (Op.getResNo() != 0 || NeedOF || NeedCF)
8825 // Emit a CMP with 0, which is the TEST pattern.
8826 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op,
8827 DAG.getConstant(0, Op.getValueType()));
8828
8829 unsigned Opcode = 0;
8830 unsigned NumOperands = 0;
Nadav Rotemb9d6b842012-08-18 17:53:03 +00008831
8832 // Truncate operations may prevent the merge of the SETCC instruction
8833 // and the arithmetic intruction before it. Attempt to truncate the operands
8834 // of the arithmetic instruction and use a reduced bit-width instruction.
8835 bool NeedTruncation = false;
8836 SDValue ArithOp = Op;
8837 if (Op->getOpcode() == ISD::TRUNCATE && Op->hasOneUse()) {
8838 SDValue Arith = Op->getOperand(0);
8839 // Both the trunc and the arithmetic op need to have one user each.
8840 if (Arith->hasOneUse())
8841 switch (Arith.getOpcode()) {
8842 default: break;
8843 case ISD::ADD:
8844 case ISD::SUB:
8845 case ISD::AND:
8846 case ISD::OR:
8847 case ISD::XOR: {
8848 NeedTruncation = true;
8849 ArithOp = Arith;
8850 }
8851 }
8852 }
8853
8854 // NOTICE: In the code below we use ArithOp to hold the arithmetic operation
8855 // which may be the result of a CAST. We use the variable 'Op', which is the
8856 // non-casted variable when we check for possible users.
8857 switch (ArithOp.getOpcode()) {
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008858 case ISD::ADD:
8859 // Due to an isel shortcoming, be conservative if this add is likely to be
8860 // selected as part of a load-modify-store instruction. When the root node
8861 // in a match is a store, isel doesn't know how to remap non-chain non-flag
8862 // uses of other nodes in the match, such as the ADD in this case. This
8863 // leads to the ADD being left around and reselected, with the result being
8864 // two adds in the output. Alas, even if none our users are stores, that
8865 // doesn't prove we're O.K. Ergo, if we have any parents that aren't
8866 // CopyToReg or SETCC, eschew INC/DEC. A better fix seems to require
8867 // climbing the DAG back to the root, and it doesn't seem to be worth the
8868 // effort.
8869 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
Pete Cooper2d496892011-11-15 21:57:53 +00008870 UE = Op.getNode()->use_end(); UI != UE; ++UI)
8871 if (UI->getOpcode() != ISD::CopyToReg &&
8872 UI->getOpcode() != ISD::SETCC &&
8873 UI->getOpcode() != ISD::STORE)
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008874 goto default_case;
8875
8876 if (ConstantSDNode *C =
Nadav Rotemb9d6b842012-08-18 17:53:03 +00008877 dyn_cast<ConstantSDNode>(ArithOp.getNode()->getOperand(1))) {
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008878 // An add of one will be selected as an INC.
8879 if (C->getAPIntValue() == 1) {
8880 Opcode = X86ISD::INC;
8881 NumOperands = 1;
8882 break;
Dan Gohmane220c4b2009-09-18 19:59:53 +00008883 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008884
8885 // An add of negative one (subtract of one) will be selected as a DEC.
8886 if (C->getAPIntValue().isAllOnesValue()) {
8887 Opcode = X86ISD::DEC;
8888 NumOperands = 1;
8889 break;
8890 }
Dan Gohman076aee32009-03-04 19:44:21 +00008891 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008892
8893 // Otherwise use a regular EFLAGS-setting add.
8894 Opcode = X86ISD::ADD;
8895 NumOperands = 2;
8896 break;
8897 case ISD::AND: {
8898 // If the primary and result isn't used, don't bother using X86ISD::AND,
8899 // because a TEST instruction will be better.
8900 bool NonFlagUse = false;
8901 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
8902 UE = Op.getNode()->use_end(); UI != UE; ++UI) {
8903 SDNode *User = *UI;
8904 unsigned UOpNo = UI.getOperandNo();
8905 if (User->getOpcode() == ISD::TRUNCATE && User->hasOneUse()) {
8906 // Look pass truncate.
8907 UOpNo = User->use_begin().getOperandNo();
8908 User = *User->use_begin();
8909 }
8910
8911 if (User->getOpcode() != ISD::BRCOND &&
8912 User->getOpcode() != ISD::SETCC &&
Nadav Rotemb9d6b842012-08-18 17:53:03 +00008913 !(User->getOpcode() == ISD::SELECT && UOpNo == 0)) {
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008914 NonFlagUse = true;
8915 break;
8916 }
Dan Gohman076aee32009-03-04 19:44:21 +00008917 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008918
8919 if (!NonFlagUse)
8920 break;
8921 }
8922 // FALL THROUGH
8923 case ISD::SUB:
8924 case ISD::OR:
8925 case ISD::XOR:
8926 // Due to the ISEL shortcoming noted above, be conservative if this op is
8927 // likely to be selected as part of a load-modify-store instruction.
8928 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
8929 UE = Op.getNode()->use_end(); UI != UE; ++UI)
8930 if (UI->getOpcode() == ISD::STORE)
8931 goto default_case;
8932
8933 // Otherwise use a regular EFLAGS-setting instruction.
Nadav Rotemb9d6b842012-08-18 17:53:03 +00008934 switch (ArithOp.getOpcode()) {
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008935 default: llvm_unreachable("unexpected operator!");
Nadav Rotemb9d6b842012-08-18 17:53:03 +00008936 case ISD::SUB: Opcode = X86ISD::SUB; break;
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008937 case ISD::XOR: Opcode = X86ISD::XOR; break;
8938 case ISD::AND: Opcode = X86ISD::AND; break;
Michael Liaof966e4e2012-09-13 20:24:54 +00008939 case ISD::OR: {
8940 if (!NeedTruncation && (X86CC == X86::COND_E || X86CC == X86::COND_NE)) {
8941 SDValue EFLAGS = LowerVectorAllZeroTest(Op, DAG);
8942 if (EFLAGS.getNode())
8943 return EFLAGS;
8944 }
8945 Opcode = X86ISD::OR;
8946 break;
8947 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008948 }
8949
8950 NumOperands = 2;
8951 break;
8952 case X86ISD::ADD:
8953 case X86ISD::SUB:
8954 case X86ISD::INC:
8955 case X86ISD::DEC:
8956 case X86ISD::OR:
8957 case X86ISD::XOR:
8958 case X86ISD::AND:
8959 return SDValue(Op.getNode(), 1);
8960 default:
8961 default_case:
8962 break;
Dan Gohman076aee32009-03-04 19:44:21 +00008963 }
8964
Nadav Rotemb9d6b842012-08-18 17:53:03 +00008965 // If we found that truncation is beneficial, perform the truncation and
8966 // update 'Op'.
8967 if (NeedTruncation) {
8968 EVT VT = Op.getValueType();
8969 SDValue WideVal = Op->getOperand(0);
8970 EVT WideVT = WideVal.getValueType();
8971 unsigned ConvertedOp = 0;
8972 // Use a target machine opcode to prevent further DAGCombine
8973 // optimizations that may separate the arithmetic operations
8974 // from the setcc node.
8975 switch (WideVal.getOpcode()) {
8976 default: break;
8977 case ISD::ADD: ConvertedOp = X86ISD::ADD; break;
8978 case ISD::SUB: ConvertedOp = X86ISD::SUB; break;
8979 case ISD::AND: ConvertedOp = X86ISD::AND; break;
8980 case ISD::OR: ConvertedOp = X86ISD::OR; break;
8981 case ISD::XOR: ConvertedOp = X86ISD::XOR; break;
8982 }
8983
8984 if (ConvertedOp) {
8985 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
8986 if (TLI.isOperationLegal(WideVal.getOpcode(), WideVT)) {
8987 SDValue V0 = DAG.getNode(ISD::TRUNCATE, dl, VT, WideVal.getOperand(0));
8988 SDValue V1 = DAG.getNode(ISD::TRUNCATE, dl, VT, WideVal.getOperand(1));
8989 Op = DAG.getNode(ConvertedOp, dl, VT, V0, V1);
8990 }
8991 }
8992 }
8993
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008994 if (Opcode == 0)
8995 // Emit a CMP with 0, which is the TEST pattern.
8996 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op,
8997 DAG.getConstant(0, Op.getValueType()));
8998
8999 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::i32);
9000 SmallVector<SDValue, 4> Ops;
9001 for (unsigned i = 0; i != NumOperands; ++i)
9002 Ops.push_back(Op.getOperand(i));
9003
9004 SDValue New = DAG.getNode(Opcode, dl, VTs, &Ops[0], NumOperands);
9005 DAG.ReplaceAllUsesWith(Op, New);
9006 return SDValue(New.getNode(), 1);
Dan Gohman076aee32009-03-04 19:44:21 +00009007}
9008
9009/// Emit nodes that will be selected as "cmp Op0,Op1", or something
9010/// equivalent.
Dan Gohman31125812009-03-07 01:58:32 +00009011SDValue X86TargetLowering::EmitCmp(SDValue Op0, SDValue Op1, unsigned X86CC,
Evan Cheng552f09a2010-04-26 19:06:11 +00009012 SelectionDAG &DAG) const {
Dan Gohman076aee32009-03-04 19:44:21 +00009013 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op1))
9014 if (C->getAPIntValue() == 0)
Evan Cheng552f09a2010-04-26 19:06:11 +00009015 return EmitTest(Op0, X86CC, DAG);
Dan Gohman076aee32009-03-04 19:44:21 +00009016
9017 DebugLoc dl = Op0.getDebugLoc();
Manman Ren39ad5682012-08-08 00:51:41 +00009018 if ((Op0.getValueType() == MVT::i8 || Op0.getValueType() == MVT::i16 ||
9019 Op0.getValueType() == MVT::i32 || Op0.getValueType() == MVT::i64)) {
9020 // Use SUB instead of CMP to enable CSE between SUB and CMP.
9021 SDVTList VTs = DAG.getVTList(Op0.getValueType(), MVT::i32);
9022 SDValue Sub = DAG.getNode(X86ISD::SUB, dl, VTs,
9023 Op0, Op1);
9024 return SDValue(Sub.getNode(), 1);
9025 }
Owen Anderson825b72b2009-08-11 20:47:22 +00009026 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op0, Op1);
Dan Gohman076aee32009-03-04 19:44:21 +00009027}
9028
Benjamin Kramer17c836c2012-04-27 12:07:43 +00009029/// Convert a comparison if required by the subtarget.
9030SDValue X86TargetLowering::ConvertCmpIfNecessary(SDValue Cmp,
9031 SelectionDAG &DAG) const {
9032 // If the subtarget does not support the FUCOMI instruction, floating-point
9033 // comparisons have to be converted.
9034 if (Subtarget->hasCMov() ||
9035 Cmp.getOpcode() != X86ISD::CMP ||
9036 !Cmp.getOperand(0).getValueType().isFloatingPoint() ||
9037 !Cmp.getOperand(1).getValueType().isFloatingPoint())
9038 return Cmp;
9039
9040 // The instruction selector will select an FUCOM instruction instead of
9041 // FUCOMI, which writes the comparison result to FPSW instead of EFLAGS. Hence
9042 // build an SDNode sequence that transfers the result from FPSW into EFLAGS:
9043 // (X86sahf (trunc (srl (X86fp_stsw (trunc (X86cmp ...)), 8))))
9044 DebugLoc dl = Cmp.getDebugLoc();
9045 SDValue TruncFPSW = DAG.getNode(ISD::TRUNCATE, dl, MVT::i16, Cmp);
9046 SDValue FNStSW = DAG.getNode(X86ISD::FNSTSW16r, dl, MVT::i16, TruncFPSW);
9047 SDValue Srl = DAG.getNode(ISD::SRL, dl, MVT::i16, FNStSW,
9048 DAG.getConstant(8, MVT::i8));
9049 SDValue TruncSrl = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Srl);
9050 return DAG.getNode(X86ISD::SAHF, dl, MVT::i32, TruncSrl);
9051}
9052
Evan Cheng4e544802012-12-05 00:10:38 +00009053static bool isAllOnes(SDValue V) {
9054 ConstantSDNode *C = dyn_cast<ConstantSDNode>(V);
9055 return C && C->isAllOnesValue();
9056}
9057
Evan Chengd40d03e2010-01-06 19:38:29 +00009058/// LowerToBT - Result of 'and' is compared against zero. Turn it into a BT node
9059/// if it's possible.
Evan Cheng5528e7b2010-04-21 01:47:12 +00009060SDValue X86TargetLowering::LowerToBT(SDValue And, ISD::CondCode CC,
9061 DebugLoc dl, SelectionDAG &DAG) const {
Evan Cheng2c755ba2010-02-27 07:36:59 +00009062 SDValue Op0 = And.getOperand(0);
9063 SDValue Op1 = And.getOperand(1);
9064 if (Op0.getOpcode() == ISD::TRUNCATE)
9065 Op0 = Op0.getOperand(0);
9066 if (Op1.getOpcode() == ISD::TRUNCATE)
9067 Op1 = Op1.getOperand(0);
9068
Evan Chengd40d03e2010-01-06 19:38:29 +00009069 SDValue LHS, RHS;
Dan Gohman6b13cbc2010-06-24 02:07:59 +00009070 if (Op1.getOpcode() == ISD::SHL)
9071 std::swap(Op0, Op1);
9072 if (Op0.getOpcode() == ISD::SHL) {
Evan Cheng2c755ba2010-02-27 07:36:59 +00009073 if (ConstantSDNode *And00C = dyn_cast<ConstantSDNode>(Op0.getOperand(0)))
9074 if (And00C->getZExtValue() == 1) {
Dan Gohman6b13cbc2010-06-24 02:07:59 +00009075 // If we looked past a truncate, check that it's only truncating away
9076 // known zeros.
9077 unsigned BitWidth = Op0.getValueSizeInBits();
9078 unsigned AndBitWidth = And.getValueSizeInBits();
9079 if (BitWidth > AndBitWidth) {
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00009080 APInt Zeros, Ones;
9081 DAG.ComputeMaskedBits(Op0, Zeros, Ones);
Dan Gohman6b13cbc2010-06-24 02:07:59 +00009082 if (Zeros.countLeadingOnes() < BitWidth - AndBitWidth)
9083 return SDValue();
9084 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00009085 LHS = Op1;
9086 RHS = Op0.getOperand(1);
Evan Chengd40d03e2010-01-06 19:38:29 +00009087 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00009088 } else if (Op1.getOpcode() == ISD::Constant) {
9089 ConstantSDNode *AndRHS = cast<ConstantSDNode>(Op1);
Benjamin Kramerf238f502011-11-23 13:54:17 +00009090 uint64_t AndRHSVal = AndRHS->getZExtValue();
Evan Cheng2c755ba2010-02-27 07:36:59 +00009091 SDValue AndLHS = Op0;
Benjamin Kramerf238f502011-11-23 13:54:17 +00009092
9093 if (AndRHSVal == 1 && AndLHS.getOpcode() == ISD::SRL) {
Evan Chengd40d03e2010-01-06 19:38:29 +00009094 LHS = AndLHS.getOperand(0);
9095 RHS = AndLHS.getOperand(1);
Dan Gohmane5af2d32009-01-29 01:59:02 +00009096 }
Benjamin Kramerf238f502011-11-23 13:54:17 +00009097
9098 // Use BT if the immediate can't be encoded in a TEST instruction.
9099 if (!isUInt<32>(AndRHSVal) && isPowerOf2_64(AndRHSVal)) {
9100 LHS = AndLHS;
9101 RHS = DAG.getConstant(Log2_64_Ceil(AndRHSVal), LHS.getValueType());
9102 }
Evan Chengd40d03e2010-01-06 19:38:29 +00009103 }
Evan Cheng0488db92007-09-25 01:57:46 +00009104
Evan Chengd40d03e2010-01-06 19:38:29 +00009105 if (LHS.getNode()) {
Evan Cheng4e544802012-12-05 00:10:38 +00009106 // If the LHS is of the form (x ^ -1) then replace the LHS with x and flip
9107 // the condition code later.
9108 bool Invert = false;
9109 if (LHS.getOpcode() == ISD::XOR && isAllOnes(LHS.getOperand(1))) {
9110 Invert = true;
9111 LHS = LHS.getOperand(0);
9112 }
9113
Evan Chenge5b51ac2010-04-17 06:13:15 +00009114 // If LHS is i8, promote it to i32 with any_extend. There is no i8 BT
Evan Chengd40d03e2010-01-06 19:38:29 +00009115 // instruction. Since the shift amount is in-range-or-undefined, we know
Evan Chenge5b51ac2010-04-17 06:13:15 +00009116 // that doing a bittest on the i32 value is ok. We extend to i32 because
Evan Chengd40d03e2010-01-06 19:38:29 +00009117 // the encoding for the i16 version is larger than the i32 version.
Evan Chenge5b51ac2010-04-17 06:13:15 +00009118 // Also promote i16 to i32 for performance / code size reason.
9119 if (LHS.getValueType() == MVT::i8 ||
Evan Cheng2bce5f4b2010-04-28 08:30:49 +00009120 LHS.getValueType() == MVT::i16)
Evan Chengd40d03e2010-01-06 19:38:29 +00009121 LHS = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, LHS);
Chris Lattnere55484e2008-12-25 05:34:37 +00009122
Evan Chengd40d03e2010-01-06 19:38:29 +00009123 // If the operand types disagree, extend the shift amount to match. Since
9124 // BT ignores high bits (like shifts) we can use anyextend.
9125 if (LHS.getValueType() != RHS.getValueType())
9126 RHS = DAG.getNode(ISD::ANY_EXTEND, dl, LHS.getValueType(), RHS);
Dan Gohmane5af2d32009-01-29 01:59:02 +00009127
Evan Chengd40d03e2010-01-06 19:38:29 +00009128 SDValue BT = DAG.getNode(X86ISD::BT, dl, MVT::i32, LHS, RHS);
Evan Cheng4e544802012-12-05 00:10:38 +00009129 X86::CondCode Cond = CC == ISD::SETEQ ? X86::COND_AE : X86::COND_B;
9130 // Flip the condition if the LHS was a not instruction
9131 if (Invert)
9132 Cond = X86::GetOppositeBranchCondition(Cond);
Evan Chengd40d03e2010-01-06 19:38:29 +00009133 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
9134 DAG.getConstant(Cond, MVT::i8), BT);
Chris Lattnere55484e2008-12-25 05:34:37 +00009135 }
9136
Evan Cheng54de3ea2010-01-05 06:52:31 +00009137 return SDValue();
9138}
9139
Dan Gohmand858e902010-04-17 15:26:15 +00009140SDValue X86TargetLowering::LowerSETCC(SDValue Op, SelectionDAG &DAG) const {
Duncan Sands28b77e92011-09-06 19:07:46 +00009141
9142 if (Op.getValueType().isVector()) return LowerVSETCC(Op, DAG);
9143
Evan Cheng54de3ea2010-01-05 06:52:31 +00009144 assert(Op.getValueType() == MVT::i8 && "SetCC type must be 8-bit integer");
9145 SDValue Op0 = Op.getOperand(0);
9146 SDValue Op1 = Op.getOperand(1);
9147 DebugLoc dl = Op.getDebugLoc();
9148 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
9149
9150 // Optimize to BT if possible.
Evan Chengd40d03e2010-01-06 19:38:29 +00009151 // Lower (X & (1 << N)) == 0 to BT(X, N).
9152 // Lower ((X >>u N) & 1) != 0 to BT(X, N).
9153 // Lower ((X >>s N) & 1) != 0 to BT(X, N).
Andrew Trickf6c39412011-03-23 23:11:02 +00009154 if (Op0.getOpcode() == ISD::AND && Op0.hasOneUse() &&
Evan Chengd40d03e2010-01-06 19:38:29 +00009155 Op1.getOpcode() == ISD::Constant &&
Dan Gohmane368b462010-06-18 14:22:04 +00009156 cast<ConstantSDNode>(Op1)->isNullValue() &&
Evan Chengd40d03e2010-01-06 19:38:29 +00009157 (CC == ISD::SETEQ || CC == ISD::SETNE)) {
9158 SDValue NewSetCC = LowerToBT(Op0, CC, dl, DAG);
9159 if (NewSetCC.getNode())
9160 return NewSetCC;
9161 }
Evan Cheng54de3ea2010-01-05 06:52:31 +00009162
Chris Lattner481eebc2010-12-19 21:23:48 +00009163 // Look for X == 0, X == 1, X != 0, or X != 1. We can simplify some forms of
9164 // these.
9165 if (Op1.getOpcode() == ISD::Constant &&
Andrew Trickf6c39412011-03-23 23:11:02 +00009166 (cast<ConstantSDNode>(Op1)->getZExtValue() == 1 ||
Evan Cheng2c755ba2010-02-27 07:36:59 +00009167 cast<ConstantSDNode>(Op1)->isNullValue()) &&
9168 (CC == ISD::SETEQ || CC == ISD::SETNE)) {
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009169
Chris Lattner481eebc2010-12-19 21:23:48 +00009170 // If the input is a setcc, then reuse the input setcc or use a new one with
9171 // the inverted condition.
9172 if (Op0.getOpcode() == X86ISD::SETCC) {
9173 X86::CondCode CCode = (X86::CondCode)Op0.getConstantOperandVal(0);
9174 bool Invert = (CC == ISD::SETNE) ^
9175 cast<ConstantSDNode>(Op1)->isNullValue();
9176 if (!Invert) return Op0;
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009177
Evan Cheng2c755ba2010-02-27 07:36:59 +00009178 CCode = X86::GetOppositeBranchCondition(CCode);
Chris Lattner481eebc2010-12-19 21:23:48 +00009179 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
9180 DAG.getConstant(CCode, MVT::i8), Op0.getOperand(1));
9181 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00009182 }
9183
Evan Chenge5b51ac2010-04-17 06:13:15 +00009184 bool isFP = Op1.getValueType().isFloatingPoint();
Chris Lattnere55484e2008-12-25 05:34:37 +00009185 unsigned X86CC = TranslateX86CC(CC, isFP, Op0, Op1, DAG);
Dan Gohman1a492952009-10-20 16:22:37 +00009186 if (X86CC == X86::COND_INVALID)
9187 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00009188
Chris Lattnerc19d1c32010-12-19 22:08:31 +00009189 SDValue EFLAGS = EmitCmp(Op0, Op1, X86CC, DAG);
Benjamin Kramer17c836c2012-04-27 12:07:43 +00009190 EFLAGS = ConvertCmpIfNecessary(EFLAGS, DAG);
Owen Anderson825b72b2009-08-11 20:47:22 +00009191 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
Chris Lattnerc19d1c32010-12-19 22:08:31 +00009192 DAG.getConstant(X86CC, MVT::i8), EFLAGS);
Evan Cheng0488db92007-09-25 01:57:46 +00009193}
9194
Craig Topper89af15e2011-09-18 08:03:58 +00009195// Lower256IntVSETCC - Break a VSETCC 256-bit integer VSETCC into two new 128
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00009196// ones, and then concatenate the result back.
Craig Topper89af15e2011-09-18 08:03:58 +00009197static SDValue Lower256IntVSETCC(SDValue Op, SelectionDAG &DAG) {
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00009198 EVT VT = Op.getValueType();
9199
Craig Topper7a9a28b2012-08-12 02:23:29 +00009200 assert(VT.is256BitVector() && Op.getOpcode() == ISD::SETCC &&
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00009201 "Unsupported value type for operation");
9202
Craig Topper66ddd152012-04-27 22:54:43 +00009203 unsigned NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00009204 DebugLoc dl = Op.getDebugLoc();
9205 SDValue CC = Op.getOperand(2);
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00009206
9207 // Extract the LHS vectors
9208 SDValue LHS = Op.getOperand(0);
Craig Topperb14940a2012-04-22 20:55:18 +00009209 SDValue LHS1 = Extract128BitVector(LHS, 0, DAG, dl);
9210 SDValue LHS2 = Extract128BitVector(LHS, NumElems/2, DAG, dl);
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00009211
9212 // Extract the RHS vectors
9213 SDValue RHS = Op.getOperand(1);
Craig Topperb14940a2012-04-22 20:55:18 +00009214 SDValue RHS1 = Extract128BitVector(RHS, 0, DAG, dl);
9215 SDValue RHS2 = Extract128BitVector(RHS, NumElems/2, DAG, dl);
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00009216
9217 // Issue the operation on the smaller types and concatenate the result back
9218 MVT EltVT = VT.getVectorElementType().getSimpleVT();
9219 EVT NewVT = MVT::getVectorVT(EltVT, NumElems/2);
9220 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT,
9221 DAG.getNode(Op.getOpcode(), dl, NewVT, LHS1, RHS1, CC),
9222 DAG.getNode(Op.getOpcode(), dl, NewVT, LHS2, RHS2, CC));
9223}
9224
Dan Gohmand858e902010-04-17 15:26:15 +00009225SDValue X86TargetLowering::LowerVSETCC(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +00009226 SDValue Cond;
9227 SDValue Op0 = Op.getOperand(0);
9228 SDValue Op1 = Op.getOperand(1);
9229 SDValue CC = Op.getOperand(2);
Owen Andersone50ed302009-08-10 22:56:29 +00009230 EVT VT = Op.getValueType();
Nate Begeman30a0de92008-07-17 16:51:19 +00009231 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
9232 bool isFP = Op.getOperand(1).getValueType().isFloatingPoint();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009233 DebugLoc dl = Op.getDebugLoc();
Nate Begeman30a0de92008-07-17 16:51:19 +00009234
9235 if (isFP) {
Craig Topper523908d2012-08-13 02:34:03 +00009236#ifndef NDEBUG
Bruno Cardoso Lopes0f0e0a02011-08-09 00:46:57 +00009237 EVT EltVT = Op0.getValueType().getVectorElementType();
Craig Topper523908d2012-08-13 02:34:03 +00009238 assert(EltVT == MVT::f32 || EltVT == MVT::f64);
9239#endif
Bruno Cardoso Lopes0f0e0a02011-08-09 00:46:57 +00009240
Craig Topper523908d2012-08-13 02:34:03 +00009241 unsigned SSECC;
Nate Begeman30a0de92008-07-17 16:51:19 +00009242 bool Swap = false;
9243
Bruno Cardoso Lopes8e03a822011-09-12 19:30:40 +00009244 // SSE Condition code mapping:
9245 // 0 - EQ
9246 // 1 - LT
9247 // 2 - LE
9248 // 3 - UNORD
9249 // 4 - NEQ
9250 // 5 - NLT
9251 // 6 - NLE
9252 // 7 - ORD
Nate Begeman30a0de92008-07-17 16:51:19 +00009253 switch (SetCCOpcode) {
Craig Topper2f1b2ec2012-08-13 03:42:38 +00009254 default: llvm_unreachable("Unexpected SETCC condition");
Nate Begemanfb8ead02008-07-25 19:05:58 +00009255 case ISD::SETOEQ:
Nate Begeman30a0de92008-07-17 16:51:19 +00009256 case ISD::SETEQ: SSECC = 0; break;
Bruno Cardoso Lopes8e03a822011-09-12 19:30:40 +00009257 case ISD::SETOGT:
9258 case ISD::SETGT: Swap = true; // Fallthrough
Bruno Cardoso Lopes457d53d2011-09-12 21:24:07 +00009259 case ISD::SETLT:
9260 case ISD::SETOLT: SSECC = 1; break;
9261 case ISD::SETOGE:
9262 case ISD::SETGE: Swap = true; // Fallthrough
Nate Begeman30a0de92008-07-17 16:51:19 +00009263 case ISD::SETLE:
9264 case ISD::SETOLE: SSECC = 2; break;
9265 case ISD::SETUO: SSECC = 3; break;
Nate Begemanfb8ead02008-07-25 19:05:58 +00009266 case ISD::SETUNE:
Nate Begeman30a0de92008-07-17 16:51:19 +00009267 case ISD::SETNE: SSECC = 4; break;
Craig Topper523908d2012-08-13 02:34:03 +00009268 case ISD::SETULE: Swap = true; // Fallthrough
Nate Begeman30a0de92008-07-17 16:51:19 +00009269 case ISD::SETUGE: SSECC = 5; break;
Craig Topper523908d2012-08-13 02:34:03 +00009270 case ISD::SETULT: Swap = true; // Fallthrough
Nate Begeman30a0de92008-07-17 16:51:19 +00009271 case ISD::SETUGT: SSECC = 6; break;
9272 case ISD::SETO: SSECC = 7; break;
Craig Topper523908d2012-08-13 02:34:03 +00009273 case ISD::SETUEQ:
9274 case ISD::SETONE: SSECC = 8; break;
Nate Begeman30a0de92008-07-17 16:51:19 +00009275 }
9276 if (Swap)
9277 std::swap(Op0, Op1);
9278
Nate Begemanfb8ead02008-07-25 19:05:58 +00009279 // In the two special cases we can't handle, emit two comparisons.
Nate Begeman30a0de92008-07-17 16:51:19 +00009280 if (SSECC == 8) {
Craig Topper523908d2012-08-13 02:34:03 +00009281 unsigned CC0, CC1;
9282 unsigned CombineOpc;
Nate Begemanfb8ead02008-07-25 19:05:58 +00009283 if (SetCCOpcode == ISD::SETUEQ) {
Craig Topper523908d2012-08-13 02:34:03 +00009284 CC0 = 3; CC1 = 0; CombineOpc = ISD::OR;
9285 } else {
9286 assert(SetCCOpcode == ISD::SETONE);
9287 CC0 = 7; CC1 = 4; CombineOpc = ISD::AND;
Craig Topper69947b92012-04-23 06:57:04 +00009288 }
Craig Topper523908d2012-08-13 02:34:03 +00009289
9290 SDValue Cmp0 = DAG.getNode(X86ISD::CMPP, dl, VT, Op0, Op1,
9291 DAG.getConstant(CC0, MVT::i8));
9292 SDValue Cmp1 = DAG.getNode(X86ISD::CMPP, dl, VT, Op0, Op1,
9293 DAG.getConstant(CC1, MVT::i8));
9294 return DAG.getNode(CombineOpc, dl, VT, Cmp0, Cmp1);
Nate Begeman30a0de92008-07-17 16:51:19 +00009295 }
9296 // Handle all other FP comparisons here.
Craig Topper1906d322012-01-22 23:36:02 +00009297 return DAG.getNode(X86ISD::CMPP, dl, VT, Op0, Op1,
9298 DAG.getConstant(SSECC, MVT::i8));
Nate Begeman30a0de92008-07-17 16:51:19 +00009299 }
Scott Michelfdc40a02009-02-17 22:15:04 +00009300
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00009301 // Break 256-bit integer vector compare into smaller ones.
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00009302 if (VT.is256BitVector() && !Subtarget->hasInt256())
Craig Topper89af15e2011-09-18 08:03:58 +00009303 return Lower256IntVSETCC(Op, DAG);
Bruno Cardoso Lopes0f0e0a02011-08-09 00:46:57 +00009304
Nate Begeman30a0de92008-07-17 16:51:19 +00009305 // We are handling one of the integer comparisons here. Since SSE only has
9306 // GT and EQ comparisons for integer, swapping operands and multiple
9307 // operations may be required for some comparisons.
Craig Topper2f1b2ec2012-08-13 03:42:38 +00009308 unsigned Opc;
Nate Begeman30a0de92008-07-17 16:51:19 +00009309 bool Swap = false, Invert = false, FlipSigns = false;
Scott Michelfdc40a02009-02-17 22:15:04 +00009310
Nate Begeman30a0de92008-07-17 16:51:19 +00009311 switch (SetCCOpcode) {
Craig Topper2f1b2ec2012-08-13 03:42:38 +00009312 default: llvm_unreachable("Unexpected SETCC condition");
Nate Begeman30a0de92008-07-17 16:51:19 +00009313 case ISD::SETNE: Invert = true;
Craig Topper67609fd2012-01-22 22:42:16 +00009314 case ISD::SETEQ: Opc = X86ISD::PCMPEQ; break;
Nate Begeman30a0de92008-07-17 16:51:19 +00009315 case ISD::SETLT: Swap = true;
Craig Topper67609fd2012-01-22 22:42:16 +00009316 case ISD::SETGT: Opc = X86ISD::PCMPGT; break;
Nate Begeman30a0de92008-07-17 16:51:19 +00009317 case ISD::SETGE: Swap = true;
Craig Topper67609fd2012-01-22 22:42:16 +00009318 case ISD::SETLE: Opc = X86ISD::PCMPGT; Invert = true; break;
Nate Begeman30a0de92008-07-17 16:51:19 +00009319 case ISD::SETULT: Swap = true;
Craig Topper67609fd2012-01-22 22:42:16 +00009320 case ISD::SETUGT: Opc = X86ISD::PCMPGT; FlipSigns = true; break;
Nate Begeman30a0de92008-07-17 16:51:19 +00009321 case ISD::SETUGE: Swap = true;
Craig Topper67609fd2012-01-22 22:42:16 +00009322 case ISD::SETULE: Opc = X86ISD::PCMPGT; FlipSigns = true; Invert = true; break;
Nate Begeman30a0de92008-07-17 16:51:19 +00009323 }
9324 if (Swap)
9325 std::swap(Op0, Op1);
Scott Michelfdc40a02009-02-17 22:15:04 +00009326
Eli Friedman7d3e2b72011-09-28 21:00:25 +00009327 // Check that the operation in question is available (most are plain SSE2,
9328 // but PCMPGTQ and PCMPEQQ have different requirements).
Craig Topper2f1b2ec2012-08-13 03:42:38 +00009329 if (VT == MVT::v2i64) {
9330 if (Opc == X86ISD::PCMPGT && !Subtarget->hasSSE42())
9331 return SDValue();
Benjamin Kramer382ed782012-12-25 12:54:19 +00009332 if (Opc == X86ISD::PCMPEQ && !Subtarget->hasSSE41()) {
9333 // If pcmpeqq is missing but pcmpeqd is available synthesize pcmpeqq with
Benjamin Kramer99f78062012-12-25 13:09:08 +00009334 // pcmpeqd + pshufd + pand.
Benjamin Kramer382ed782012-12-25 12:54:19 +00009335 assert(Subtarget->hasSSE2() && !FlipSigns && "Don't know how to lower!");
9336
9337 // First cast everything to the right type,
9338 Op0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, Op0);
9339 Op1 = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, Op1);
9340
9341 // Do the compare.
9342 SDValue Result = DAG.getNode(Opc, dl, MVT::v4i32, Op0, Op1);
9343
9344 // Make sure the lower and upper halves are both all-ones.
Benjamin Kramer99f78062012-12-25 13:09:08 +00009345 const int Mask[] = { 1, 0, 3, 2 };
9346 SDValue Shuf = DAG.getVectorShuffle(MVT::v4i32, dl, Result, Result, Mask);
9347 Result = DAG.getNode(ISD::AND, dl, MVT::v4i32, Result, Shuf);
Benjamin Kramer382ed782012-12-25 12:54:19 +00009348
9349 if (Invert)
9350 Result = DAG.getNOT(dl, Result, MVT::v4i32);
9351
9352 return DAG.getNode(ISD::BITCAST, dl, VT, Result);
9353 }
Craig Topper2f1b2ec2012-08-13 03:42:38 +00009354 }
Eli Friedman7d3e2b72011-09-28 21:00:25 +00009355
Nate Begeman30a0de92008-07-17 16:51:19 +00009356 // Since SSE has no unsigned integer comparisons, we need to flip the sign
9357 // bits of the inputs before performing those operations.
9358 if (FlipSigns) {
Owen Andersone50ed302009-08-10 22:56:29 +00009359 EVT EltVT = VT.getVectorElementType();
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00009360 SDValue SignBit = DAG.getConstant(APInt::getSignBit(EltVT.getSizeInBits()),
9361 EltVT);
Dan Gohman475871a2008-07-27 21:46:04 +00009362 std::vector<SDValue> SignBits(VT.getVectorNumElements(), SignBit);
Evan Chenga87008d2009-02-25 22:49:59 +00009363 SDValue SignVec = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &SignBits[0],
9364 SignBits.size());
Dale Johannesenace16102009-02-03 19:33:06 +00009365 Op0 = DAG.getNode(ISD::XOR, dl, VT, Op0, SignVec);
9366 Op1 = DAG.getNode(ISD::XOR, dl, VT, Op1, SignVec);
Nate Begeman30a0de92008-07-17 16:51:19 +00009367 }
Scott Michelfdc40a02009-02-17 22:15:04 +00009368
Dale Johannesenace16102009-02-03 19:33:06 +00009369 SDValue Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
Nate Begeman30a0de92008-07-17 16:51:19 +00009370
9371 // If the logical-not of the result is required, perform that now.
Bob Wilson4c245462009-01-22 17:39:32 +00009372 if (Invert)
Dale Johannesenace16102009-02-03 19:33:06 +00009373 Result = DAG.getNOT(dl, Result, VT);
Bob Wilson4c245462009-01-22 17:39:32 +00009374
Nate Begeman30a0de92008-07-17 16:51:19 +00009375 return Result;
9376}
Evan Cheng0488db92007-09-25 01:57:46 +00009377
Evan Cheng370e5342008-12-03 08:38:43 +00009378// isX86LogicalCmp - Return true if opcode is a X86 logical comparison.
Dan Gohman076aee32009-03-04 19:44:21 +00009379static bool isX86LogicalCmp(SDValue Op) {
9380 unsigned Opc = Op.getNode()->getOpcode();
Benjamin Kramer17c836c2012-04-27 12:07:43 +00009381 if (Opc == X86ISD::CMP || Opc == X86ISD::COMI || Opc == X86ISD::UCOMI ||
9382 Opc == X86ISD::SAHF)
Dan Gohman076aee32009-03-04 19:44:21 +00009383 return true;
9384 if (Op.getResNo() == 1 &&
9385 (Opc == X86ISD::ADD ||
9386 Opc == X86ISD::SUB ||
Chris Lattner5b856542010-12-20 00:59:46 +00009387 Opc == X86ISD::ADC ||
9388 Opc == X86ISD::SBB ||
Dan Gohman076aee32009-03-04 19:44:21 +00009389 Opc == X86ISD::SMUL ||
9390 Opc == X86ISD::UMUL ||
9391 Opc == X86ISD::INC ||
Dan Gohmane220c4b2009-09-18 19:59:53 +00009392 Opc == X86ISD::DEC ||
9393 Opc == X86ISD::OR ||
9394 Opc == X86ISD::XOR ||
9395 Opc == X86ISD::AND))
Dan Gohman076aee32009-03-04 19:44:21 +00009396 return true;
9397
Chris Lattner9637d5b2010-12-05 07:49:54 +00009398 if (Op.getResNo() == 2 && Opc == X86ISD::UMUL)
9399 return true;
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009400
Dan Gohman076aee32009-03-04 19:44:21 +00009401 return false;
Evan Cheng370e5342008-12-03 08:38:43 +00009402}
9403
Chris Lattnera2b56002010-12-05 01:23:24 +00009404static bool isZero(SDValue V) {
9405 ConstantSDNode *C = dyn_cast<ConstantSDNode>(V);
9406 return C && C->isNullValue();
9407}
9408
Evan Chengb64dd5f2012-08-07 22:21:00 +00009409static bool isTruncWithZeroHighBitsInput(SDValue V, SelectionDAG &DAG) {
9410 if (V.getOpcode() != ISD::TRUNCATE)
9411 return false;
9412
9413 SDValue VOp0 = V.getOperand(0);
9414 unsigned InBits = VOp0.getValueSizeInBits();
9415 unsigned Bits = V.getValueSizeInBits();
9416 return DAG.MaskedValueIsZero(VOp0, APInt::getHighBitsSet(InBits,InBits-Bits));
9417}
9418
Dan Gohmand858e902010-04-17 15:26:15 +00009419SDValue X86TargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng734503b2006-09-11 02:19:56 +00009420 bool addTest = true;
Dan Gohman475871a2008-07-27 21:46:04 +00009421 SDValue Cond = Op.getOperand(0);
Chris Lattnera2b56002010-12-05 01:23:24 +00009422 SDValue Op1 = Op.getOperand(1);
9423 SDValue Op2 = Op.getOperand(2);
9424 DebugLoc DL = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00009425 SDValue CC;
Evan Cheng9bba8942006-01-26 02:13:10 +00009426
Dan Gohman1a492952009-10-20 16:22:37 +00009427 if (Cond.getOpcode() == ISD::SETCC) {
9428 SDValue NewCond = LowerSETCC(Cond, DAG);
9429 if (NewCond.getNode())
9430 Cond = NewCond;
9431 }
Evan Cheng734503b2006-09-11 02:19:56 +00009432
Chris Lattnera2b56002010-12-05 01:23:24 +00009433 // (select (x == 0), -1, y) -> (sign_bit (x - 1)) | y
Chris Lattner96908b12010-12-05 02:00:51 +00009434 // (select (x == 0), y, -1) -> ~(sign_bit (x - 1)) | y
Chris Lattnera2b56002010-12-05 01:23:24 +00009435 // (select (x != 0), y, -1) -> (sign_bit (x - 1)) | y
Chris Lattner96908b12010-12-05 02:00:51 +00009436 // (select (x != 0), -1, y) -> ~(sign_bit (x - 1)) | y
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00009437 if (Cond.getOpcode() == X86ISD::SETCC &&
Chris Lattner96908b12010-12-05 02:00:51 +00009438 Cond.getOperand(1).getOpcode() == X86ISD::CMP &&
9439 isZero(Cond.getOperand(1).getOperand(1))) {
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00009440 SDValue Cmp = Cond.getOperand(1);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009441
Chris Lattnera2b56002010-12-05 01:23:24 +00009442 unsigned CondCode =cast<ConstantSDNode>(Cond.getOperand(0))->getZExtValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009443
9444 if ((isAllOnes(Op1) || isAllOnes(Op2)) &&
Chris Lattner96908b12010-12-05 02:00:51 +00009445 (CondCode == X86::COND_E || CondCode == X86::COND_NE)) {
9446 SDValue Y = isAllOnes(Op2) ? Op1 : Op2;
Chris Lattnera2b56002010-12-05 01:23:24 +00009447
9448 SDValue CmpOp0 = Cmp.getOperand(0);
Manman Rened579842012-05-07 18:06:23 +00009449 // Apply further optimizations for special cases
9450 // (select (x != 0), -1, 0) -> neg & sbb
9451 // (select (x == 0), 0, -1) -> neg & sbb
9452 if (ConstantSDNode *YC = dyn_cast<ConstantSDNode>(Y))
Chad Rosiera20e1e72012-08-01 18:39:17 +00009453 if (YC->isNullValue() &&
Manman Rened579842012-05-07 18:06:23 +00009454 (isAllOnes(Op1) == (CondCode == X86::COND_NE))) {
9455 SDVTList VTs = DAG.getVTList(CmpOp0.getValueType(), MVT::i32);
Chad Rosiera20e1e72012-08-01 18:39:17 +00009456 SDValue Neg = DAG.getNode(X86ISD::SUB, DL, VTs,
9457 DAG.getConstant(0, CmpOp0.getValueType()),
Manman Rened579842012-05-07 18:06:23 +00009458 CmpOp0);
9459 SDValue Res = DAG.getNode(X86ISD::SETCC_CARRY, DL, Op.getValueType(),
9460 DAG.getConstant(X86::COND_B, MVT::i8),
9461 SDValue(Neg.getNode(), 1));
9462 return Res;
9463 }
9464
Chris Lattnera2b56002010-12-05 01:23:24 +00009465 Cmp = DAG.getNode(X86ISD::CMP, DL, MVT::i32,
9466 CmpOp0, DAG.getConstant(1, CmpOp0.getValueType()));
Benjamin Kramer17c836c2012-04-27 12:07:43 +00009467 Cmp = ConvertCmpIfNecessary(Cmp, DAG);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009468
Chris Lattner96908b12010-12-05 02:00:51 +00009469 SDValue Res = // Res = 0 or -1.
Chris Lattnera2b56002010-12-05 01:23:24 +00009470 DAG.getNode(X86ISD::SETCC_CARRY, DL, Op.getValueType(),
9471 DAG.getConstant(X86::COND_B, MVT::i8), Cmp);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009472
Chris Lattner96908b12010-12-05 02:00:51 +00009473 if (isAllOnes(Op1) != (CondCode == X86::COND_E))
9474 Res = DAG.getNOT(DL, Res, Res.getValueType());
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009475
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00009476 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(Op2);
Chris Lattnera2b56002010-12-05 01:23:24 +00009477 if (N2C == 0 || !N2C->isNullValue())
9478 Res = DAG.getNode(ISD::OR, DL, Res.getValueType(), Res, Y);
9479 return Res;
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00009480 }
9481 }
9482
Chris Lattnera2b56002010-12-05 01:23:24 +00009483 // Look past (and (setcc_carry (cmp ...)), 1).
Evan Chengad9c0a32009-12-15 00:53:42 +00009484 if (Cond.getOpcode() == ISD::AND &&
9485 Cond.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY) {
9486 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Cond.getOperand(1));
Michael J. Spencerec38de22010-10-10 22:04:20 +00009487 if (C && C->getAPIntValue() == 1)
Evan Chengad9c0a32009-12-15 00:53:42 +00009488 Cond = Cond.getOperand(0);
9489 }
9490
Evan Cheng3f41d662007-10-08 22:16:29 +00009491 // If condition flag is set by a X86ISD::CMP, then use it as the condition
9492 // setting operand in place of the X86ISD::SETCC.
Dan Gohman65fd6562011-11-03 21:49:52 +00009493 unsigned CondOpcode = Cond.getOpcode();
9494 if (CondOpcode == X86ISD::SETCC ||
9495 CondOpcode == X86ISD::SETCC_CARRY) {
Evan Cheng734503b2006-09-11 02:19:56 +00009496 CC = Cond.getOperand(0);
9497
Dan Gohman475871a2008-07-27 21:46:04 +00009498 SDValue Cmp = Cond.getOperand(1);
Evan Cheng734503b2006-09-11 02:19:56 +00009499 unsigned Opc = Cmp.getOpcode();
Owen Andersone50ed302009-08-10 22:56:29 +00009500 EVT VT = Op.getValueType();
Scott Michelfdc40a02009-02-17 22:15:04 +00009501
Evan Cheng3f41d662007-10-08 22:16:29 +00009502 bool IllegalFPCMov = false;
Duncan Sands83ec4b62008-06-06 12:08:01 +00009503 if (VT.isFloatingPoint() && !VT.isVector() &&
Chris Lattner78631162008-01-16 06:24:21 +00009504 !isScalarFPTypeInSSEReg(VT)) // FPStack?
Dan Gohman7810bfe2008-09-26 21:54:37 +00009505 IllegalFPCMov = !hasFPCMov(cast<ConstantSDNode>(CC)->getSExtValue());
Scott Michelfdc40a02009-02-17 22:15:04 +00009506
Chris Lattnerd1980a52009-03-12 06:52:53 +00009507 if ((isX86LogicalCmp(Cmp) && !IllegalFPCMov) ||
9508 Opc == X86ISD::BT) { // FIXME
Evan Cheng3f41d662007-10-08 22:16:29 +00009509 Cond = Cmp;
Evan Cheng0488db92007-09-25 01:57:46 +00009510 addTest = false;
9511 }
Dan Gohman65fd6562011-11-03 21:49:52 +00009512 } else if (CondOpcode == ISD::USUBO || CondOpcode == ISD::SSUBO ||
9513 CondOpcode == ISD::UADDO || CondOpcode == ISD::SADDO ||
9514 ((CondOpcode == ISD::UMULO || CondOpcode == ISD::SMULO) &&
9515 Cond.getOperand(0).getValueType() != MVT::i8)) {
9516 SDValue LHS = Cond.getOperand(0);
9517 SDValue RHS = Cond.getOperand(1);
9518 unsigned X86Opcode;
9519 unsigned X86Cond;
9520 SDVTList VTs;
9521 switch (CondOpcode) {
9522 case ISD::UADDO: X86Opcode = X86ISD::ADD; X86Cond = X86::COND_B; break;
9523 case ISD::SADDO: X86Opcode = X86ISD::ADD; X86Cond = X86::COND_O; break;
9524 case ISD::USUBO: X86Opcode = X86ISD::SUB; X86Cond = X86::COND_B; break;
9525 case ISD::SSUBO: X86Opcode = X86ISD::SUB; X86Cond = X86::COND_O; break;
9526 case ISD::UMULO: X86Opcode = X86ISD::UMUL; X86Cond = X86::COND_O; break;
9527 case ISD::SMULO: X86Opcode = X86ISD::SMUL; X86Cond = X86::COND_O; break;
9528 default: llvm_unreachable("unexpected overflowing operator");
9529 }
9530 if (CondOpcode == ISD::UMULO)
9531 VTs = DAG.getVTList(LHS.getValueType(), LHS.getValueType(),
9532 MVT::i32);
9533 else
9534 VTs = DAG.getVTList(LHS.getValueType(), MVT::i32);
9535
9536 SDValue X86Op = DAG.getNode(X86Opcode, DL, VTs, LHS, RHS);
9537
9538 if (CondOpcode == ISD::UMULO)
9539 Cond = X86Op.getValue(2);
9540 else
9541 Cond = X86Op.getValue(1);
9542
9543 CC = DAG.getConstant(X86Cond, MVT::i8);
9544 addTest = false;
Evan Cheng0488db92007-09-25 01:57:46 +00009545 }
9546
9547 if (addTest) {
Evan Chengb64dd5f2012-08-07 22:21:00 +00009548 // Look pass the truncate if the high bits are known zero.
9549 if (isTruncWithZeroHighBitsInput(Cond, DAG))
9550 Cond = Cond.getOperand(0);
Evan Chengd40d03e2010-01-06 19:38:29 +00009551
9552 // We know the result of AND is compared against zero. Try to match
9553 // it to BT.
Michael J. Spencerec38de22010-10-10 22:04:20 +00009554 if (Cond.getOpcode() == ISD::AND && Cond.hasOneUse()) {
Chris Lattnera2b56002010-12-05 01:23:24 +00009555 SDValue NewSetCC = LowerToBT(Cond, ISD::SETNE, DL, DAG);
Evan Chengd40d03e2010-01-06 19:38:29 +00009556 if (NewSetCC.getNode()) {
9557 CC = NewSetCC.getOperand(0);
9558 Cond = NewSetCC.getOperand(1);
9559 addTest = false;
9560 }
9561 }
9562 }
9563
9564 if (addTest) {
Owen Anderson825b72b2009-08-11 20:47:22 +00009565 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng552f09a2010-04-26 19:06:11 +00009566 Cond = EmitTest(Cond, X86::COND_NE, DAG);
Evan Cheng0488db92007-09-25 01:57:46 +00009567 }
9568
Benjamin Kramere915ff32010-12-22 23:09:28 +00009569 // a < b ? -1 : 0 -> RES = ~setcc_carry
9570 // a < b ? 0 : -1 -> RES = setcc_carry
9571 // a >= b ? -1 : 0 -> RES = setcc_carry
9572 // a >= b ? 0 : -1 -> RES = ~setcc_carry
Manman Ren39ad5682012-08-08 00:51:41 +00009573 if (Cond.getOpcode() == X86ISD::SUB) {
Benjamin Kramer17c836c2012-04-27 12:07:43 +00009574 Cond = ConvertCmpIfNecessary(Cond, DAG);
Benjamin Kramere915ff32010-12-22 23:09:28 +00009575 unsigned CondCode = cast<ConstantSDNode>(CC)->getZExtValue();
9576
9577 if ((CondCode == X86::COND_AE || CondCode == X86::COND_B) &&
9578 (isAllOnes(Op1) || isAllOnes(Op2)) && (isZero(Op1) || isZero(Op2))) {
9579 SDValue Res = DAG.getNode(X86ISD::SETCC_CARRY, DL, Op.getValueType(),
9580 DAG.getConstant(X86::COND_B, MVT::i8), Cond);
9581 if (isAllOnes(Op1) != (CondCode == X86::COND_B))
9582 return DAG.getNOT(DL, Res, Res.getValueType());
9583 return Res;
9584 }
9585 }
9586
Benjamin Kramer444dcce2012-10-13 10:39:49 +00009587 // X86 doesn't have an i8 cmov. If both operands are the result of a truncate
9588 // widen the cmov and push the truncate through. This avoids introducing a new
9589 // branch during isel and doesn't add any extensions.
9590 if (Op.getValueType() == MVT::i8 &&
9591 Op1.getOpcode() == ISD::TRUNCATE && Op2.getOpcode() == ISD::TRUNCATE) {
9592 SDValue T1 = Op1.getOperand(0), T2 = Op2.getOperand(0);
9593 if (T1.getValueType() == T2.getValueType() &&
9594 // Blacklist CopyFromReg to avoid partial register stalls.
9595 T1.getOpcode() != ISD::CopyFromReg && T2.getOpcode()!=ISD::CopyFromReg){
9596 SDVTList VTs = DAG.getVTList(T1.getValueType(), MVT::Glue);
Benjamin Kramerf8b65aa2012-10-13 12:50:19 +00009597 SDValue Cmov = DAG.getNode(X86ISD::CMOV, DL, VTs, T2, T1, CC, Cond);
Benjamin Kramer444dcce2012-10-13 10:39:49 +00009598 return DAG.getNode(ISD::TRUNCATE, DL, Op.getValueType(), Cmov);
9599 }
9600 }
9601
Evan Cheng0488db92007-09-25 01:57:46 +00009602 // X86ISD::CMOV means set the result (which is operand 1) to the RHS if
9603 // condition is true.
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00009604 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::Glue);
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00009605 SDValue Ops[] = { Op2, Op1, CC, Cond };
Chris Lattnera2b56002010-12-05 01:23:24 +00009606 return DAG.getNode(X86ISD::CMOV, DL, VTs, Ops, array_lengthof(Ops));
Evan Cheng0488db92007-09-25 01:57:46 +00009607}
9608
Nadav Rotem1a330af2012-12-27 22:47:16 +00009609SDValue X86TargetLowering::LowerSIGN_EXTEND(SDValue Op,
9610 SelectionDAG &DAG) const {
9611 EVT VT = Op->getValueType(0);
9612 SDValue In = Op->getOperand(0);
9613 EVT InVT = In.getValueType();
9614 DebugLoc dl = Op->getDebugLoc();
9615
Nadav Rotem587fb1d2012-12-27 23:08:05 +00009616 if ((VT != MVT::v4i64 || InVT != MVT::v4i32) &&
9617 (VT != MVT::v8i32 || InVT != MVT::v8i16))
9618 return SDValue();
Nadav Rotem1a330af2012-12-27 22:47:16 +00009619
Nadav Rotem587fb1d2012-12-27 23:08:05 +00009620 if (Subtarget->hasInt256())
9621 return DAG.getNode(X86ISD::VSEXT_MOVL, dl, VT, In);
Nadav Rotem1a330af2012-12-27 22:47:16 +00009622
Nadav Rotem587fb1d2012-12-27 23:08:05 +00009623 // Optimize vectors in AVX mode
9624 // Sign extend v8i16 to v8i32 and
9625 // v4i32 to v4i64
9626 //
9627 // Divide input vector into two parts
9628 // for v4i32 the shuffle mask will be { 0, 1, -1, -1} {2, 3, -1, -1}
9629 // use vpmovsx instruction to extend v4i32 -> v2i64; v8i16 -> v4i32
9630 // concat the vectors to original VT
Nadav Rotem1a330af2012-12-27 22:47:16 +00009631
Nadav Rotem587fb1d2012-12-27 23:08:05 +00009632 unsigned NumElems = InVT.getVectorNumElements();
9633 SDValue Undef = DAG.getUNDEF(InVT);
Nadav Rotem1a330af2012-12-27 22:47:16 +00009634
Nadav Rotem587fb1d2012-12-27 23:08:05 +00009635 SmallVector<int,8> ShufMask1(NumElems, -1);
9636 for (unsigned i = 0; i != NumElems/2; ++i)
9637 ShufMask1[i] = i;
Nadav Rotem1a330af2012-12-27 22:47:16 +00009638
Nadav Rotem587fb1d2012-12-27 23:08:05 +00009639 SDValue OpLo = DAG.getVectorShuffle(InVT, dl, In, Undef, &ShufMask1[0]);
Nadav Rotem1a330af2012-12-27 22:47:16 +00009640
Nadav Rotem587fb1d2012-12-27 23:08:05 +00009641 SmallVector<int,8> ShufMask2(NumElems, -1);
9642 for (unsigned i = 0; i != NumElems/2; ++i)
9643 ShufMask2[i] = i + NumElems/2;
Nadav Rotem1a330af2012-12-27 22:47:16 +00009644
Nadav Rotem587fb1d2012-12-27 23:08:05 +00009645 SDValue OpHi = DAG.getVectorShuffle(InVT, dl, In, Undef, &ShufMask2[0]);
Nadav Rotem1a330af2012-12-27 22:47:16 +00009646
Nadav Rotem587fb1d2012-12-27 23:08:05 +00009647 EVT HalfVT = EVT::getVectorVT(*DAG.getContext(), VT.getScalarType(),
9648 VT.getVectorNumElements()/2);
Nadav Rotem1a330af2012-12-27 22:47:16 +00009649
Nadav Rotem587fb1d2012-12-27 23:08:05 +00009650 OpLo = DAG.getNode(X86ISD::VSEXT_MOVL, dl, HalfVT, OpLo);
9651 OpHi = DAG.getNode(X86ISD::VSEXT_MOVL, dl, HalfVT, OpHi);
Nadav Rotem1a330af2012-12-27 22:47:16 +00009652
Nadav Rotem587fb1d2012-12-27 23:08:05 +00009653 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT, OpLo, OpHi);
Nadav Rotem1a330af2012-12-27 22:47:16 +00009654}
9655
Evan Cheng370e5342008-12-03 08:38:43 +00009656// isAndOrOfSingleUseSetCCs - Return true if node is an ISD::AND or
9657// ISD::OR of two X86ISD::SETCC nodes each of which has no other use apart
9658// from the AND / OR.
9659static bool isAndOrOfSetCCs(SDValue Op, unsigned &Opc) {
9660 Opc = Op.getOpcode();
9661 if (Opc != ISD::OR && Opc != ISD::AND)
9662 return false;
9663 return (Op.getOperand(0).getOpcode() == X86ISD::SETCC &&
9664 Op.getOperand(0).hasOneUse() &&
9665 Op.getOperand(1).getOpcode() == X86ISD::SETCC &&
9666 Op.getOperand(1).hasOneUse());
9667}
9668
Evan Cheng961d6d42009-02-02 08:19:07 +00009669// isXor1OfSetCC - Return true if node is an ISD::XOR of a X86ISD::SETCC and
9670// 1 and that the SETCC node has a single use.
Evan Cheng67ad9db2009-02-02 08:07:36 +00009671static bool isXor1OfSetCC(SDValue Op) {
9672 if (Op.getOpcode() != ISD::XOR)
9673 return false;
9674 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
9675 if (N1C && N1C->getAPIntValue() == 1) {
9676 return Op.getOperand(0).getOpcode() == X86ISD::SETCC &&
9677 Op.getOperand(0).hasOneUse();
9678 }
9679 return false;
9680}
9681
Dan Gohmand858e902010-04-17 15:26:15 +00009682SDValue X86TargetLowering::LowerBRCOND(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng734503b2006-09-11 02:19:56 +00009683 bool addTest = true;
Dan Gohman475871a2008-07-27 21:46:04 +00009684 SDValue Chain = Op.getOperand(0);
9685 SDValue Cond = Op.getOperand(1);
9686 SDValue Dest = Op.getOperand(2);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009687 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00009688 SDValue CC;
Dan Gohman65fd6562011-11-03 21:49:52 +00009689 bool Inverted = false;
Evan Cheng734503b2006-09-11 02:19:56 +00009690
Dan Gohman1a492952009-10-20 16:22:37 +00009691 if (Cond.getOpcode() == ISD::SETCC) {
Dan Gohman65fd6562011-11-03 21:49:52 +00009692 // Check for setcc([su]{add,sub,mul}o == 0).
9693 if (cast<CondCodeSDNode>(Cond.getOperand(2))->get() == ISD::SETEQ &&
9694 isa<ConstantSDNode>(Cond.getOperand(1)) &&
9695 cast<ConstantSDNode>(Cond.getOperand(1))->isNullValue() &&
9696 Cond.getOperand(0).getResNo() == 1 &&
9697 (Cond.getOperand(0).getOpcode() == ISD::SADDO ||
9698 Cond.getOperand(0).getOpcode() == ISD::UADDO ||
9699 Cond.getOperand(0).getOpcode() == ISD::SSUBO ||
9700 Cond.getOperand(0).getOpcode() == ISD::USUBO ||
9701 Cond.getOperand(0).getOpcode() == ISD::SMULO ||
9702 Cond.getOperand(0).getOpcode() == ISD::UMULO)) {
9703 Inverted = true;
9704 Cond = Cond.getOperand(0);
9705 } else {
9706 SDValue NewCond = LowerSETCC(Cond, DAG);
9707 if (NewCond.getNode())
9708 Cond = NewCond;
9709 }
Dan Gohman1a492952009-10-20 16:22:37 +00009710 }
Chris Lattnere55484e2008-12-25 05:34:37 +00009711#if 0
9712 // FIXME: LowerXALUO doesn't handle these!!
Bill Wendlingd350e022008-12-12 21:15:41 +00009713 else if (Cond.getOpcode() == X86ISD::ADD ||
9714 Cond.getOpcode() == X86ISD::SUB ||
9715 Cond.getOpcode() == X86ISD::SMUL ||
9716 Cond.getOpcode() == X86ISD::UMUL)
Bill Wendling74c37652008-12-09 22:08:41 +00009717 Cond = LowerXALUO(Cond, DAG);
Chris Lattnere55484e2008-12-25 05:34:37 +00009718#endif
Scott Michelfdc40a02009-02-17 22:15:04 +00009719
Evan Chengad9c0a32009-12-15 00:53:42 +00009720 // Look pass (and (setcc_carry (cmp ...)), 1).
9721 if (Cond.getOpcode() == ISD::AND &&
9722 Cond.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY) {
9723 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Cond.getOperand(1));
Michael J. Spencerec38de22010-10-10 22:04:20 +00009724 if (C && C->getAPIntValue() == 1)
Evan Chengad9c0a32009-12-15 00:53:42 +00009725 Cond = Cond.getOperand(0);
9726 }
9727
Evan Cheng3f41d662007-10-08 22:16:29 +00009728 // If condition flag is set by a X86ISD::CMP, then use it as the condition
9729 // setting operand in place of the X86ISD::SETCC.
Dan Gohman65fd6562011-11-03 21:49:52 +00009730 unsigned CondOpcode = Cond.getOpcode();
9731 if (CondOpcode == X86ISD::SETCC ||
9732 CondOpcode == X86ISD::SETCC_CARRY) {
Evan Cheng734503b2006-09-11 02:19:56 +00009733 CC = Cond.getOperand(0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00009734
Dan Gohman475871a2008-07-27 21:46:04 +00009735 SDValue Cmp = Cond.getOperand(1);
Evan Cheng734503b2006-09-11 02:19:56 +00009736 unsigned Opc = Cmp.getOpcode();
Chris Lattnere55484e2008-12-25 05:34:37 +00009737 // FIXME: WHY THE SPECIAL CASING OF LogicalCmp??
Dan Gohman076aee32009-03-04 19:44:21 +00009738 if (isX86LogicalCmp(Cmp) || Opc == X86ISD::BT) {
Evan Cheng3f41d662007-10-08 22:16:29 +00009739 Cond = Cmp;
Evan Cheng0488db92007-09-25 01:57:46 +00009740 addTest = false;
Bill Wendling61edeb52008-12-02 01:06:39 +00009741 } else {
Evan Cheng370e5342008-12-03 08:38:43 +00009742 switch (cast<ConstantSDNode>(CC)->getZExtValue()) {
Bill Wendling0ea25cb2008-12-03 08:32:02 +00009743 default: break;
9744 case X86::COND_O:
Dan Gohman653456c2009-01-07 00:15:08 +00009745 case X86::COND_B:
Chris Lattnere55484e2008-12-25 05:34:37 +00009746 // These can only come from an arithmetic instruction with overflow,
9747 // e.g. SADDO, UADDO.
Bill Wendling0ea25cb2008-12-03 08:32:02 +00009748 Cond = Cond.getNode()->getOperand(1);
9749 addTest = false;
9750 break;
Bill Wendling61edeb52008-12-02 01:06:39 +00009751 }
Evan Cheng0488db92007-09-25 01:57:46 +00009752 }
Dan Gohman65fd6562011-11-03 21:49:52 +00009753 }
9754 CondOpcode = Cond.getOpcode();
9755 if (CondOpcode == ISD::UADDO || CondOpcode == ISD::SADDO ||
9756 CondOpcode == ISD::USUBO || CondOpcode == ISD::SSUBO ||
9757 ((CondOpcode == ISD::UMULO || CondOpcode == ISD::SMULO) &&
9758 Cond.getOperand(0).getValueType() != MVT::i8)) {
9759 SDValue LHS = Cond.getOperand(0);
9760 SDValue RHS = Cond.getOperand(1);
9761 unsigned X86Opcode;
9762 unsigned X86Cond;
9763 SDVTList VTs;
9764 switch (CondOpcode) {
9765 case ISD::UADDO: X86Opcode = X86ISD::ADD; X86Cond = X86::COND_B; break;
9766 case ISD::SADDO: X86Opcode = X86ISD::ADD; X86Cond = X86::COND_O; break;
9767 case ISD::USUBO: X86Opcode = X86ISD::SUB; X86Cond = X86::COND_B; break;
9768 case ISD::SSUBO: X86Opcode = X86ISD::SUB; X86Cond = X86::COND_O; break;
9769 case ISD::UMULO: X86Opcode = X86ISD::UMUL; X86Cond = X86::COND_O; break;
9770 case ISD::SMULO: X86Opcode = X86ISD::SMUL; X86Cond = X86::COND_O; break;
9771 default: llvm_unreachable("unexpected overflowing operator");
9772 }
9773 if (Inverted)
9774 X86Cond = X86::GetOppositeBranchCondition((X86::CondCode)X86Cond);
9775 if (CondOpcode == ISD::UMULO)
9776 VTs = DAG.getVTList(LHS.getValueType(), LHS.getValueType(),
9777 MVT::i32);
9778 else
9779 VTs = DAG.getVTList(LHS.getValueType(), MVT::i32);
9780
9781 SDValue X86Op = DAG.getNode(X86Opcode, dl, VTs, LHS, RHS);
9782
9783 if (CondOpcode == ISD::UMULO)
9784 Cond = X86Op.getValue(2);
9785 else
9786 Cond = X86Op.getValue(1);
9787
9788 CC = DAG.getConstant(X86Cond, MVT::i8);
9789 addTest = false;
Evan Cheng370e5342008-12-03 08:38:43 +00009790 } else {
9791 unsigned CondOpc;
9792 if (Cond.hasOneUse() && isAndOrOfSetCCs(Cond, CondOpc)) {
9793 SDValue Cmp = Cond.getOperand(0).getOperand(1);
Evan Cheng370e5342008-12-03 08:38:43 +00009794 if (CondOpc == ISD::OR) {
9795 // Also, recognize the pattern generated by an FCMP_UNE. We can emit
9796 // two branches instead of an explicit OR instruction with a
9797 // separate test.
9798 if (Cmp == Cond.getOperand(1).getOperand(1) &&
Dan Gohman076aee32009-03-04 19:44:21 +00009799 isX86LogicalCmp(Cmp)) {
Evan Cheng370e5342008-12-03 08:38:43 +00009800 CC = Cond.getOperand(0).getOperand(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009801 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Evan Cheng370e5342008-12-03 08:38:43 +00009802 Chain, Dest, CC, Cmp);
9803 CC = Cond.getOperand(1).getOperand(0);
9804 Cond = Cmp;
9805 addTest = false;
9806 }
9807 } else { // ISD::AND
9808 // Also, recognize the pattern generated by an FCMP_OEQ. We can emit
9809 // two branches instead of an explicit AND instruction with a
9810 // separate test. However, we only do this if this block doesn't
9811 // have a fall-through edge, because this requires an explicit
9812 // jmp when the condition is false.
9813 if (Cmp == Cond.getOperand(1).getOperand(1) &&
Dan Gohman076aee32009-03-04 19:44:21 +00009814 isX86LogicalCmp(Cmp) &&
Evan Cheng370e5342008-12-03 08:38:43 +00009815 Op.getNode()->hasOneUse()) {
9816 X86::CondCode CCode =
9817 (X86::CondCode)Cond.getOperand(0).getConstantOperandVal(0);
9818 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00009819 CC = DAG.getConstant(CCode, MVT::i8);
Dan Gohman027657d2010-06-18 15:30:29 +00009820 SDNode *User = *Op.getNode()->use_begin();
Evan Cheng370e5342008-12-03 08:38:43 +00009821 // Look for an unconditional branch following this conditional branch.
9822 // We need this because we need to reverse the successors in order
9823 // to implement FCMP_OEQ.
Dan Gohman027657d2010-06-18 15:30:29 +00009824 if (User->getOpcode() == ISD::BR) {
9825 SDValue FalseBB = User->getOperand(1);
9826 SDNode *NewBR =
9827 DAG.UpdateNodeOperands(User, User->getOperand(0), Dest);
Evan Cheng370e5342008-12-03 08:38:43 +00009828 assert(NewBR == User);
Nick Lewycky2a3ee5e2010-06-20 20:27:42 +00009829 (void)NewBR;
Evan Cheng370e5342008-12-03 08:38:43 +00009830 Dest = FalseBB;
Dan Gohman279c22e2008-10-21 03:29:32 +00009831
Dale Johannesene4d209d2009-02-03 20:21:25 +00009832 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Evan Cheng370e5342008-12-03 08:38:43 +00009833 Chain, Dest, CC, Cmp);
9834 X86::CondCode CCode =
9835 (X86::CondCode)Cond.getOperand(1).getConstantOperandVal(0);
9836 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00009837 CC = DAG.getConstant(CCode, MVT::i8);
Evan Cheng370e5342008-12-03 08:38:43 +00009838 Cond = Cmp;
9839 addTest = false;
9840 }
9841 }
Dan Gohman279c22e2008-10-21 03:29:32 +00009842 }
Evan Cheng67ad9db2009-02-02 08:07:36 +00009843 } else if (Cond.hasOneUse() && isXor1OfSetCC(Cond)) {
9844 // Recognize for xorb (setcc), 1 patterns. The xor inverts the condition.
9845 // It should be transformed during dag combiner except when the condition
9846 // is set by a arithmetics with overflow node.
9847 X86::CondCode CCode =
9848 (X86::CondCode)Cond.getOperand(0).getConstantOperandVal(0);
9849 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00009850 CC = DAG.getConstant(CCode, MVT::i8);
Evan Cheng67ad9db2009-02-02 08:07:36 +00009851 Cond = Cond.getOperand(0).getOperand(1);
9852 addTest = false;
Dan Gohman65fd6562011-11-03 21:49:52 +00009853 } else if (Cond.getOpcode() == ISD::SETCC &&
9854 cast<CondCodeSDNode>(Cond.getOperand(2))->get() == ISD::SETOEQ) {
9855 // For FCMP_OEQ, we can emit
9856 // two branches instead of an explicit AND instruction with a
9857 // separate test. However, we only do this if this block doesn't
9858 // have a fall-through edge, because this requires an explicit
9859 // jmp when the condition is false.
9860 if (Op.getNode()->hasOneUse()) {
9861 SDNode *User = *Op.getNode()->use_begin();
9862 // Look for an unconditional branch following this conditional branch.
9863 // We need this because we need to reverse the successors in order
9864 // to implement FCMP_OEQ.
9865 if (User->getOpcode() == ISD::BR) {
9866 SDValue FalseBB = User->getOperand(1);
9867 SDNode *NewBR =
9868 DAG.UpdateNodeOperands(User, User->getOperand(0), Dest);
9869 assert(NewBR == User);
9870 (void)NewBR;
9871 Dest = FalseBB;
9872
9873 SDValue Cmp = DAG.getNode(X86ISD::CMP, dl, MVT::i32,
9874 Cond.getOperand(0), Cond.getOperand(1));
Benjamin Kramer17c836c2012-04-27 12:07:43 +00009875 Cmp = ConvertCmpIfNecessary(Cmp, DAG);
Dan Gohman65fd6562011-11-03 21:49:52 +00009876 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
9877 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
9878 Chain, Dest, CC, Cmp);
9879 CC = DAG.getConstant(X86::COND_P, MVT::i8);
9880 Cond = Cmp;
9881 addTest = false;
9882 }
9883 }
9884 } else if (Cond.getOpcode() == ISD::SETCC &&
9885 cast<CondCodeSDNode>(Cond.getOperand(2))->get() == ISD::SETUNE) {
9886 // For FCMP_UNE, we can emit
9887 // two branches instead of an explicit AND instruction with a
9888 // separate test. However, we only do this if this block doesn't
9889 // have a fall-through edge, because this requires an explicit
9890 // jmp when the condition is false.
9891 if (Op.getNode()->hasOneUse()) {
9892 SDNode *User = *Op.getNode()->use_begin();
9893 // Look for an unconditional branch following this conditional branch.
9894 // We need this because we need to reverse the successors in order
9895 // to implement FCMP_UNE.
9896 if (User->getOpcode() == ISD::BR) {
9897 SDValue FalseBB = User->getOperand(1);
9898 SDNode *NewBR =
9899 DAG.UpdateNodeOperands(User, User->getOperand(0), Dest);
9900 assert(NewBR == User);
9901 (void)NewBR;
9902
9903 SDValue Cmp = DAG.getNode(X86ISD::CMP, dl, MVT::i32,
9904 Cond.getOperand(0), Cond.getOperand(1));
Benjamin Kramer17c836c2012-04-27 12:07:43 +00009905 Cmp = ConvertCmpIfNecessary(Cmp, DAG);
Dan Gohman65fd6562011-11-03 21:49:52 +00009906 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
9907 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
9908 Chain, Dest, CC, Cmp);
9909 CC = DAG.getConstant(X86::COND_NP, MVT::i8);
9910 Cond = Cmp;
9911 addTest = false;
9912 Dest = FalseBB;
9913 }
9914 }
Dan Gohman279c22e2008-10-21 03:29:32 +00009915 }
Evan Cheng0488db92007-09-25 01:57:46 +00009916 }
9917
9918 if (addTest) {
Evan Chengb64dd5f2012-08-07 22:21:00 +00009919 // Look pass the truncate if the high bits are known zero.
9920 if (isTruncWithZeroHighBitsInput(Cond, DAG))
9921 Cond = Cond.getOperand(0);
Evan Chengd40d03e2010-01-06 19:38:29 +00009922
9923 // We know the result of AND is compared against zero. Try to match
9924 // it to BT.
Michael J. Spencerec38de22010-10-10 22:04:20 +00009925 if (Cond.getOpcode() == ISD::AND && Cond.hasOneUse()) {
Evan Chengd40d03e2010-01-06 19:38:29 +00009926 SDValue NewSetCC = LowerToBT(Cond, ISD::SETNE, dl, DAG);
9927 if (NewSetCC.getNode()) {
9928 CC = NewSetCC.getOperand(0);
9929 Cond = NewSetCC.getOperand(1);
9930 addTest = false;
9931 }
9932 }
9933 }
9934
9935 if (addTest) {
Owen Anderson825b72b2009-08-11 20:47:22 +00009936 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng552f09a2010-04-26 19:06:11 +00009937 Cond = EmitTest(Cond, X86::COND_NE, DAG);
Evan Cheng0488db92007-09-25 01:57:46 +00009938 }
Benjamin Kramer17c836c2012-04-27 12:07:43 +00009939 Cond = ConvertCmpIfNecessary(Cond, DAG);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009940 return DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Dan Gohman279c22e2008-10-21 03:29:32 +00009941 Chain, Dest, CC, Cond);
Evan Cheng0488db92007-09-25 01:57:46 +00009942}
9943
Anton Korobeynikove060b532007-04-17 19:34:00 +00009944// Lower dynamic stack allocation to _alloca call for Cygwin/Mingw targets.
9945// Calls to _alloca is needed to probe the stack when allocating more than 4k
9946// bytes in one go. Touching the stack at 4K increments is necessary to ensure
9947// that the guard pages used by the OS virtual memory manager are allocated in
9948// correct sequence.
Dan Gohman475871a2008-07-27 21:46:04 +00009949SDValue
9950X86TargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00009951 SelectionDAG &DAG) const {
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009952 assert((Subtarget->isTargetCygMing() || Subtarget->isTargetWindows() ||
Nick Lewycky8a8d4792011-12-02 22:16:29 +00009953 getTargetMachine().Options.EnableSegmentedStacks) &&
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009954 "This should be used only on Windows targets or when segmented stacks "
Rafael Espindola96428ce2011-09-06 18:43:08 +00009955 "are being used");
9956 assert(!Subtarget->isTargetEnvMacho() && "Not implemented");
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009957 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov096b4612008-06-11 20:16:42 +00009958
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00009959 // Get the inputs.
Dan Gohman475871a2008-07-27 21:46:04 +00009960 SDValue Chain = Op.getOperand(0);
9961 SDValue Size = Op.getOperand(1);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00009962 // FIXME: Ensure alignment here
9963
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009964 bool Is64Bit = Subtarget->is64Bit();
9965 EVT SPTy = Is64Bit ? MVT::i64 : MVT::i32;
Anton Korobeynikov096b4612008-06-11 20:16:42 +00009966
Nick Lewycky8a8d4792011-12-02 22:16:29 +00009967 if (getTargetMachine().Options.EnableSegmentedStacks) {
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009968 MachineFunction &MF = DAG.getMachineFunction();
9969 MachineRegisterInfo &MRI = MF.getRegInfo();
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00009970
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009971 if (Is64Bit) {
9972 // The 64 bit implementation of segmented stacks needs to clobber both r10
Rafael Espindola96428ce2011-09-06 18:43:08 +00009973 // r11. This makes it impossible to use it along with nested parameters.
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009974 const Function *F = MF.getFunction();
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00009975
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009976 for (Function::const_arg_iterator I = F->arg_begin(), E = F->arg_end();
Craig Topper31a207a2012-05-04 06:39:13 +00009977 I != E; ++I)
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009978 if (I->hasNestAttr())
9979 report_fatal_error("Cannot use segmented stacks with functions that "
9980 "have nested arguments.");
9981 }
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00009982
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009983 const TargetRegisterClass *AddrRegClass =
9984 getRegClassFor(Subtarget->is64Bit() ? MVT::i64:MVT::i32);
9985 unsigned Vreg = MRI.createVirtualRegister(AddrRegClass);
9986 Chain = DAG.getCopyToReg(Chain, dl, Vreg, Size);
9987 SDValue Value = DAG.getNode(X86ISD::SEG_ALLOCA, dl, SPTy, Chain,
9988 DAG.getRegister(Vreg, SPTy));
9989 SDValue Ops1[2] = { Value, Chain };
9990 return DAG.getMergeValues(Ops1, 2, dl);
9991 } else {
9992 SDValue Flag;
9993 unsigned Reg = (Subtarget->is64Bit() ? X86::RAX : X86::EAX);
Anton Korobeynikov096b4612008-06-11 20:16:42 +00009994
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009995 Chain = DAG.getCopyToReg(Chain, dl, Reg, Size, Flag);
9996 Flag = Chain.getValue(1);
9997 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Anton Korobeynikov096b4612008-06-11 20:16:42 +00009998
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009999 Chain = DAG.getNode(X86ISD::WIN_ALLOCA, dl, NodeTys, Chain, Flag);
10000 Flag = Chain.getValue(1);
10001
Michael Liaoc5c970e2012-10-31 04:14:09 +000010002 Chain = DAG.getCopyFromReg(Chain, dl, RegInfo->getStackRegister(),
10003 SPTy).getValue(1);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000010004
10005 SDValue Ops1[2] = { Chain.getValue(0), Chain };
10006 return DAG.getMergeValues(Ops1, 2, dl);
10007 }
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +000010008}
10009
Dan Gohmand858e902010-04-17 15:26:15 +000010010SDValue X86TargetLowering::LowerVASTART(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman1e93df62010-04-17 14:41:14 +000010011 MachineFunction &MF = DAG.getMachineFunction();
10012 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
10013
Dan Gohman69de1932008-02-06 22:27:42 +000010014 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Chris Lattner8026a9d2010-09-21 17:50:43 +000010015 DebugLoc DL = Op.getDebugLoc();
Evan Cheng8b2794a2006-10-13 21:14:26 +000010016
Anton Korobeynikove7beda12010-10-03 22:52:07 +000010017 if (!Subtarget->is64Bit() || Subtarget->isTargetWin64()) {
Evan Cheng25ab6902006-09-08 06:48:29 +000010018 // vastart just stores the address of the VarArgsFrameIndex slot into the
10019 // memory location argument.
Dan Gohman1e93df62010-04-17 14:41:14 +000010020 SDValue FR = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
10021 getPointerTy());
Chris Lattner8026a9d2010-09-21 17:50:43 +000010022 return DAG.getStore(Op.getOperand(0), DL, FR, Op.getOperand(1),
10023 MachinePointerInfo(SV), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +000010024 }
10025
10026 // __va_list_tag:
10027 // gp_offset (0 - 6 * 8)
10028 // fp_offset (48 - 48 + 8 * 16)
10029 // overflow_arg_area (point to parameters coming in memory).
10030 // reg_save_area
Dan Gohman475871a2008-07-27 21:46:04 +000010031 SmallVector<SDValue, 8> MemOps;
10032 SDValue FIN = Op.getOperand(1);
Evan Cheng25ab6902006-09-08 06:48:29 +000010033 // Store gp_offset
Chris Lattner8026a9d2010-09-21 17:50:43 +000010034 SDValue Store = DAG.getStore(Op.getOperand(0), DL,
Dan Gohman1e93df62010-04-17 14:41:14 +000010035 DAG.getConstant(FuncInfo->getVarArgsGPOffset(),
10036 MVT::i32),
Chris Lattner8026a9d2010-09-21 17:50:43 +000010037 FIN, MachinePointerInfo(SV), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +000010038 MemOps.push_back(Store);
10039
10040 // Store fp_offset
Chris Lattner8026a9d2010-09-21 17:50:43 +000010041 FIN = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +000010042 FIN, DAG.getIntPtrConstant(4));
Chris Lattner8026a9d2010-09-21 17:50:43 +000010043 Store = DAG.getStore(Op.getOperand(0), DL,
Dan Gohman1e93df62010-04-17 14:41:14 +000010044 DAG.getConstant(FuncInfo->getVarArgsFPOffset(),
10045 MVT::i32),
Chris Lattner8026a9d2010-09-21 17:50:43 +000010046 FIN, MachinePointerInfo(SV, 4), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +000010047 MemOps.push_back(Store);
10048
10049 // Store ptr to overflow_arg_area
Chris Lattner8026a9d2010-09-21 17:50:43 +000010050 FIN = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +000010051 FIN, DAG.getIntPtrConstant(4));
Dan Gohman1e93df62010-04-17 14:41:14 +000010052 SDValue OVFIN = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
10053 getPointerTy());
Chris Lattner8026a9d2010-09-21 17:50:43 +000010054 Store = DAG.getStore(Op.getOperand(0), DL, OVFIN, FIN,
10055 MachinePointerInfo(SV, 8),
David Greene67c9d422010-02-15 16:53:33 +000010056 false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +000010057 MemOps.push_back(Store);
10058
10059 // Store ptr to reg_save_area.
Chris Lattner8026a9d2010-09-21 17:50:43 +000010060 FIN = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +000010061 FIN, DAG.getIntPtrConstant(8));
Dan Gohman1e93df62010-04-17 14:41:14 +000010062 SDValue RSFIN = DAG.getFrameIndex(FuncInfo->getRegSaveFrameIndex(),
10063 getPointerTy());
Chris Lattner8026a9d2010-09-21 17:50:43 +000010064 Store = DAG.getStore(Op.getOperand(0), DL, RSFIN, FIN,
10065 MachinePointerInfo(SV, 16), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +000010066 MemOps.push_back(Store);
Chris Lattner8026a9d2010-09-21 17:50:43 +000010067 return DAG.getNode(ISD::TokenFactor, DL, MVT::Other,
Dale Johannesene4d209d2009-02-03 20:21:25 +000010068 &MemOps[0], MemOps.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +000010069}
10070
Dan Gohmand858e902010-04-17 15:26:15 +000010071SDValue X86TargetLowering::LowerVAARG(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman320afb82010-10-12 18:00:49 +000010072 assert(Subtarget->is64Bit() &&
10073 "LowerVAARG only handles 64-bit va_arg!");
10074 assert((Subtarget->isTargetLinux() ||
10075 Subtarget->isTargetDarwin()) &&
10076 "Unhandled target in LowerVAARG");
10077 assert(Op.getNode()->getNumOperands() == 4);
10078 SDValue Chain = Op.getOperand(0);
10079 SDValue SrcPtr = Op.getOperand(1);
10080 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
10081 unsigned Align = Op.getConstantOperandVal(3);
10082 DebugLoc dl = Op.getDebugLoc();
Dan Gohman9018e832008-05-10 01:26:14 +000010083
Dan Gohman320afb82010-10-12 18:00:49 +000010084 EVT ArgVT = Op.getNode()->getValueType(0);
Chris Lattnerdb125cf2011-07-18 04:54:35 +000010085 Type *ArgTy = ArgVT.getTypeForEVT(*DAG.getContext());
Micah Villmow3574eca2012-10-08 16:38:25 +000010086 uint32_t ArgSize = getDataLayout()->getTypeAllocSize(ArgTy);
Dan Gohman320afb82010-10-12 18:00:49 +000010087 uint8_t ArgMode;
10088
10089 // Decide which area this value should be read from.
10090 // TODO: Implement the AMD64 ABI in its entirety. This simple
10091 // selection mechanism works only for the basic types.
10092 if (ArgVT == MVT::f80) {
10093 llvm_unreachable("va_arg for f80 not yet implemented");
10094 } else if (ArgVT.isFloatingPoint() && ArgSize <= 16 /*bytes*/) {
10095 ArgMode = 2; // Argument passed in XMM register. Use fp_offset.
10096 } else if (ArgVT.isInteger() && ArgSize <= 32 /*bytes*/) {
10097 ArgMode = 1; // Argument passed in GPR64 register(s). Use gp_offset.
10098 } else {
10099 llvm_unreachable("Unhandled argument type in LowerVAARG");
10100 }
10101
10102 if (ArgMode == 2) {
10103 // Sanity Check: Make sure using fp_offset makes sense.
Nick Lewycky8a8d4792011-12-02 22:16:29 +000010104 assert(!getTargetMachine().Options.UseSoftFloat &&
Eric Christopher52b45052010-10-12 19:44:17 +000010105 !(DAG.getMachineFunction()
Bill Wendling67658342012-10-09 07:45:08 +000010106 .getFunction()->getFnAttributes()
Bill Wendling034b94b2012-12-19 07:18:57 +000010107 .hasAttribute(Attribute::NoImplicitFloat)) &&
Craig Topper1accb7e2012-01-10 06:54:16 +000010108 Subtarget->hasSSE1());
Dan Gohman320afb82010-10-12 18:00:49 +000010109 }
10110
10111 // Insert VAARG_64 node into the DAG
10112 // VAARG_64 returns two values: Variable Argument Address, Chain
10113 SmallVector<SDValue, 11> InstOps;
10114 InstOps.push_back(Chain);
10115 InstOps.push_back(SrcPtr);
10116 InstOps.push_back(DAG.getConstant(ArgSize, MVT::i32));
10117 InstOps.push_back(DAG.getConstant(ArgMode, MVT::i8));
10118 InstOps.push_back(DAG.getConstant(Align, MVT::i32));
10119 SDVTList VTs = DAG.getVTList(getPointerTy(), MVT::Other);
10120 SDValue VAARG = DAG.getMemIntrinsicNode(X86ISD::VAARG_64, dl,
10121 VTs, &InstOps[0], InstOps.size(),
10122 MVT::i64,
10123 MachinePointerInfo(SV),
10124 /*Align=*/0,
10125 /*Volatile=*/false,
10126 /*ReadMem=*/true,
10127 /*WriteMem=*/true);
10128 Chain = VAARG.getValue(1);
10129
10130 // Load the next argument and return it
10131 return DAG.getLoad(ArgVT, dl,
10132 Chain,
10133 VAARG,
10134 MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000010135 false, false, false, 0);
Dan Gohman9018e832008-05-10 01:26:14 +000010136}
10137
Craig Topper55b24052012-09-11 06:15:32 +000010138static SDValue LowerVACOPY(SDValue Op, const X86Subtarget *Subtarget,
10139 SelectionDAG &DAG) {
Evan Chengae642192007-03-02 23:16:35 +000010140 // X86-64 va_list is a struct { i32, i32, i8*, i8* }.
Dan Gohman28269132008-04-18 20:55:41 +000010141 assert(Subtarget->is64Bit() && "This code only handles 64-bit va_copy!");
Dan Gohman475871a2008-07-27 21:46:04 +000010142 SDValue Chain = Op.getOperand(0);
10143 SDValue DstPtr = Op.getOperand(1);
10144 SDValue SrcPtr = Op.getOperand(2);
Dan Gohman69de1932008-02-06 22:27:42 +000010145 const Value *DstSV = cast<SrcValueSDNode>(Op.getOperand(3))->getValue();
10146 const Value *SrcSV = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Chris Lattnere72f2022010-09-21 05:40:29 +000010147 DebugLoc DL = Op.getDebugLoc();
Evan Chengae642192007-03-02 23:16:35 +000010148
Chris Lattnere72f2022010-09-21 05:40:29 +000010149 return DAG.getMemcpy(Chain, DL, DstPtr, SrcPtr,
Mon P Wang20adc9d2010-04-04 03:10:48 +000010150 DAG.getIntPtrConstant(24), 8, /*isVolatile*/false,
Michael J. Spencerec38de22010-10-10 22:04:20 +000010151 false,
Chris Lattnere72f2022010-09-21 05:40:29 +000010152 MachinePointerInfo(DstSV), MachinePointerInfo(SrcSV));
Evan Chengae642192007-03-02 23:16:35 +000010153}
10154
Craig Topper80e46362012-01-23 06:16:53 +000010155// getTargetVShiftNOde - Handle vector element shifts where the shift amount
10156// may or may not be a constant. Takes immediate version of shift as input.
10157static SDValue getTargetVShiftNode(unsigned Opc, DebugLoc dl, EVT VT,
10158 SDValue SrcOp, SDValue ShAmt,
10159 SelectionDAG &DAG) {
10160 assert(ShAmt.getValueType() == MVT::i32 && "ShAmt is not i32");
10161
10162 if (isa<ConstantSDNode>(ShAmt)) {
Nadav Rotemd896e242012-07-15 20:27:43 +000010163 // Constant may be a TargetConstant. Use a regular constant.
10164 uint32_t ShiftAmt = cast<ConstantSDNode>(ShAmt)->getZExtValue();
Craig Topper80e46362012-01-23 06:16:53 +000010165 switch (Opc) {
10166 default: llvm_unreachable("Unknown target vector shift node");
10167 case X86ISD::VSHLI:
10168 case X86ISD::VSRLI:
10169 case X86ISD::VSRAI:
Nadav Rotemd896e242012-07-15 20:27:43 +000010170 return DAG.getNode(Opc, dl, VT, SrcOp,
10171 DAG.getConstant(ShiftAmt, MVT::i32));
Craig Topper80e46362012-01-23 06:16:53 +000010172 }
10173 }
10174
10175 // Change opcode to non-immediate version
10176 switch (Opc) {
10177 default: llvm_unreachable("Unknown target vector shift node");
10178 case X86ISD::VSHLI: Opc = X86ISD::VSHL; break;
10179 case X86ISD::VSRLI: Opc = X86ISD::VSRL; break;
10180 case X86ISD::VSRAI: Opc = X86ISD::VSRA; break;
10181 }
10182
10183 // Need to build a vector containing shift amount
10184 // Shift amount is 32-bits, but SSE instructions read 64-bit, so fill with 0
10185 SDValue ShOps[4];
10186 ShOps[0] = ShAmt;
10187 ShOps[1] = DAG.getConstant(0, MVT::i32);
Craig Topper6d688152012-08-14 07:43:25 +000010188 ShOps[2] = ShOps[3] = DAG.getUNDEF(MVT::i32);
Craig Topper80e46362012-01-23 06:16:53 +000010189 ShAmt = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, &ShOps[0], 4);
Nadav Rotem65f489f2012-07-14 22:26:05 +000010190
10191 // The return type has to be a 128-bit type with the same element
10192 // type as the input type.
10193 MVT EltVT = VT.getVectorElementType().getSimpleVT();
10194 EVT ShVT = MVT::getVectorVT(EltVT, 128/EltVT.getSizeInBits());
10195
10196 ShAmt = DAG.getNode(ISD::BITCAST, dl, ShVT, ShAmt);
Craig Topper80e46362012-01-23 06:16:53 +000010197 return DAG.getNode(Opc, dl, VT, SrcOp, ShAmt);
10198}
10199
Craig Topper55b24052012-09-11 06:15:32 +000010200static SDValue LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG) {
Dale Johannesen6f38cb62009-02-07 19:59:05 +000010201 DebugLoc dl = Op.getDebugLoc();
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +000010202 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +000010203 switch (IntNo) {
Dan Gohman475871a2008-07-27 21:46:04 +000010204 default: return SDValue(); // Don't custom lower most intrinsics.
Evan Cheng5759f972008-05-04 09:15:50 +000010205 // Comparison intrinsics.
Evan Cheng0db9fe62006-04-25 20:13:52 +000010206 case Intrinsic::x86_sse_comieq_ss:
10207 case Intrinsic::x86_sse_comilt_ss:
10208 case Intrinsic::x86_sse_comile_ss:
10209 case Intrinsic::x86_sse_comigt_ss:
10210 case Intrinsic::x86_sse_comige_ss:
10211 case Intrinsic::x86_sse_comineq_ss:
10212 case Intrinsic::x86_sse_ucomieq_ss:
10213 case Intrinsic::x86_sse_ucomilt_ss:
10214 case Intrinsic::x86_sse_ucomile_ss:
10215 case Intrinsic::x86_sse_ucomigt_ss:
10216 case Intrinsic::x86_sse_ucomige_ss:
10217 case Intrinsic::x86_sse_ucomineq_ss:
10218 case Intrinsic::x86_sse2_comieq_sd:
10219 case Intrinsic::x86_sse2_comilt_sd:
10220 case Intrinsic::x86_sse2_comile_sd:
10221 case Intrinsic::x86_sse2_comigt_sd:
10222 case Intrinsic::x86_sse2_comige_sd:
10223 case Intrinsic::x86_sse2_comineq_sd:
10224 case Intrinsic::x86_sse2_ucomieq_sd:
10225 case Intrinsic::x86_sse2_ucomilt_sd:
10226 case Intrinsic::x86_sse2_ucomile_sd:
10227 case Intrinsic::x86_sse2_ucomigt_sd:
10228 case Intrinsic::x86_sse2_ucomige_sd:
10229 case Intrinsic::x86_sse2_ucomineq_sd: {
Craig Topper6d688152012-08-14 07:43:25 +000010230 unsigned Opc;
10231 ISD::CondCode CC;
Evan Cheng0db9fe62006-04-25 20:13:52 +000010232 switch (IntNo) {
Craig Topper86c7c582012-01-30 01:10:15 +000010233 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000010234 case Intrinsic::x86_sse_comieq_ss:
10235 case Intrinsic::x86_sse2_comieq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +000010236 Opc = X86ISD::COMI;
10237 CC = ISD::SETEQ;
10238 break;
Evan Cheng6be2c582006-04-05 23:38:46 +000010239 case Intrinsic::x86_sse_comilt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +000010240 case Intrinsic::x86_sse2_comilt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +000010241 Opc = X86ISD::COMI;
10242 CC = ISD::SETLT;
10243 break;
10244 case Intrinsic::x86_sse_comile_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +000010245 case Intrinsic::x86_sse2_comile_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +000010246 Opc = X86ISD::COMI;
10247 CC = ISD::SETLE;
10248 break;
10249 case Intrinsic::x86_sse_comigt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +000010250 case Intrinsic::x86_sse2_comigt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +000010251 Opc = X86ISD::COMI;
10252 CC = ISD::SETGT;
10253 break;
10254 case Intrinsic::x86_sse_comige_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +000010255 case Intrinsic::x86_sse2_comige_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +000010256 Opc = X86ISD::COMI;
10257 CC = ISD::SETGE;
10258 break;
10259 case Intrinsic::x86_sse_comineq_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +000010260 case Intrinsic::x86_sse2_comineq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +000010261 Opc = X86ISD::COMI;
10262 CC = ISD::SETNE;
10263 break;
10264 case Intrinsic::x86_sse_ucomieq_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +000010265 case Intrinsic::x86_sse2_ucomieq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +000010266 Opc = X86ISD::UCOMI;
10267 CC = ISD::SETEQ;
10268 break;
10269 case Intrinsic::x86_sse_ucomilt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +000010270 case Intrinsic::x86_sse2_ucomilt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +000010271 Opc = X86ISD::UCOMI;
10272 CC = ISD::SETLT;
10273 break;
10274 case Intrinsic::x86_sse_ucomile_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +000010275 case Intrinsic::x86_sse2_ucomile_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +000010276 Opc = X86ISD::UCOMI;
10277 CC = ISD::SETLE;
10278 break;
10279 case Intrinsic::x86_sse_ucomigt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +000010280 case Intrinsic::x86_sse2_ucomigt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +000010281 Opc = X86ISD::UCOMI;
10282 CC = ISD::SETGT;
10283 break;
10284 case Intrinsic::x86_sse_ucomige_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +000010285 case Intrinsic::x86_sse2_ucomige_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +000010286 Opc = X86ISD::UCOMI;
10287 CC = ISD::SETGE;
10288 break;
10289 case Intrinsic::x86_sse_ucomineq_ss:
10290 case Intrinsic::x86_sse2_ucomineq_sd:
10291 Opc = X86ISD::UCOMI;
10292 CC = ISD::SETNE;
10293 break;
Evan Cheng6be2c582006-04-05 23:38:46 +000010294 }
Evan Cheng734503b2006-09-11 02:19:56 +000010295
Dan Gohman475871a2008-07-27 21:46:04 +000010296 SDValue LHS = Op.getOperand(1);
10297 SDValue RHS = Op.getOperand(2);
Chris Lattner1c39d4c2008-12-24 23:53:05 +000010298 unsigned X86CC = TranslateX86CC(CC, true, LHS, RHS, DAG);
Dan Gohman1a492952009-10-20 16:22:37 +000010299 assert(X86CC != X86::COND_INVALID && "Unexpected illegal condition!");
Owen Anderson825b72b2009-08-11 20:47:22 +000010300 SDValue Cond = DAG.getNode(Opc, dl, MVT::i32, LHS, RHS);
10301 SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
10302 DAG.getConstant(X86CC, MVT::i8), Cond);
10303 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC);
Evan Cheng6be2c582006-04-05 23:38:46 +000010304 }
Craig Topper6d688152012-08-14 07:43:25 +000010305
Duncan Sands04aa4ae2011-09-23 16:10:22 +000010306 // Arithmetic intrinsics.
Craig Topper5b209e82012-02-05 03:14:49 +000010307 case Intrinsic::x86_sse2_pmulu_dq:
10308 case Intrinsic::x86_avx2_pmulu_dq:
10309 return DAG.getNode(X86ISD::PMULUDQ, dl, Op.getValueType(),
10310 Op.getOperand(1), Op.getOperand(2));
Craig Topper6d688152012-08-14 07:43:25 +000010311
Benjamin Kramer388fc6a2012-12-15 16:47:44 +000010312 // SSE2/AVX2 sub with unsigned saturation intrinsics
10313 case Intrinsic::x86_sse2_psubus_b:
10314 case Intrinsic::x86_sse2_psubus_w:
10315 case Intrinsic::x86_avx2_psubus_b:
10316 case Intrinsic::x86_avx2_psubus_w:
10317 return DAG.getNode(X86ISD::SUBUS, dl, Op.getValueType(),
10318 Op.getOperand(1), Op.getOperand(2));
10319
Craig Topper6d688152012-08-14 07:43:25 +000010320 // SSE3/AVX horizontal add/sub intrinsics
Duncan Sands04aa4ae2011-09-23 16:10:22 +000010321 case Intrinsic::x86_sse3_hadd_ps:
10322 case Intrinsic::x86_sse3_hadd_pd:
10323 case Intrinsic::x86_avx_hadd_ps_256:
10324 case Intrinsic::x86_avx_hadd_pd_256:
Duncan Sands04aa4ae2011-09-23 16:10:22 +000010325 case Intrinsic::x86_sse3_hsub_ps:
10326 case Intrinsic::x86_sse3_hsub_pd:
10327 case Intrinsic::x86_avx_hsub_ps_256:
10328 case Intrinsic::x86_avx_hsub_pd_256:
Craig Topper4bb3f342012-01-25 05:37:32 +000010329 case Intrinsic::x86_ssse3_phadd_w_128:
10330 case Intrinsic::x86_ssse3_phadd_d_128:
10331 case Intrinsic::x86_avx2_phadd_w:
10332 case Intrinsic::x86_avx2_phadd_d:
Craig Topper4bb3f342012-01-25 05:37:32 +000010333 case Intrinsic::x86_ssse3_phsub_w_128:
10334 case Intrinsic::x86_ssse3_phsub_d_128:
10335 case Intrinsic::x86_avx2_phsub_w:
Craig Topper6d688152012-08-14 07:43:25 +000010336 case Intrinsic::x86_avx2_phsub_d: {
10337 unsigned Opcode;
10338 switch (IntNo) {
10339 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
10340 case Intrinsic::x86_sse3_hadd_ps:
10341 case Intrinsic::x86_sse3_hadd_pd:
10342 case Intrinsic::x86_avx_hadd_ps_256:
10343 case Intrinsic::x86_avx_hadd_pd_256:
10344 Opcode = X86ISD::FHADD;
10345 break;
10346 case Intrinsic::x86_sse3_hsub_ps:
10347 case Intrinsic::x86_sse3_hsub_pd:
10348 case Intrinsic::x86_avx_hsub_ps_256:
10349 case Intrinsic::x86_avx_hsub_pd_256:
10350 Opcode = X86ISD::FHSUB;
10351 break;
10352 case Intrinsic::x86_ssse3_phadd_w_128:
10353 case Intrinsic::x86_ssse3_phadd_d_128:
10354 case Intrinsic::x86_avx2_phadd_w:
10355 case Intrinsic::x86_avx2_phadd_d:
10356 Opcode = X86ISD::HADD;
10357 break;
10358 case Intrinsic::x86_ssse3_phsub_w_128:
10359 case Intrinsic::x86_ssse3_phsub_d_128:
10360 case Intrinsic::x86_avx2_phsub_w:
10361 case Intrinsic::x86_avx2_phsub_d:
10362 Opcode = X86ISD::HSUB;
10363 break;
10364 }
10365 return DAG.getNode(Opcode, dl, Op.getValueType(),
Craig Topper4bb3f342012-01-25 05:37:32 +000010366 Op.getOperand(1), Op.getOperand(2));
Craig Topper6d688152012-08-14 07:43:25 +000010367 }
10368
Benjamin Kramer739c7a82012-12-21 14:04:55 +000010369 // SSE2/SSE41/AVX2 integer max/min intrinsics.
10370 case Intrinsic::x86_sse2_pmaxu_b:
10371 case Intrinsic::x86_sse41_pmaxuw:
10372 case Intrinsic::x86_sse41_pmaxud:
10373 case Intrinsic::x86_avx2_pmaxu_b:
10374 case Intrinsic::x86_avx2_pmaxu_w:
10375 case Intrinsic::x86_avx2_pmaxu_d:
Benjamin Kramer739c7a82012-12-21 14:04:55 +000010376 case Intrinsic::x86_sse2_pminu_b:
10377 case Intrinsic::x86_sse41_pminuw:
10378 case Intrinsic::x86_sse41_pminud:
10379 case Intrinsic::x86_avx2_pminu_b:
10380 case Intrinsic::x86_avx2_pminu_w:
10381 case Intrinsic::x86_avx2_pminu_d:
Benjamin Kramer739c7a82012-12-21 14:04:55 +000010382 case Intrinsic::x86_sse41_pmaxsb:
10383 case Intrinsic::x86_sse2_pmaxs_w:
10384 case Intrinsic::x86_sse41_pmaxsd:
10385 case Intrinsic::x86_avx2_pmaxs_b:
10386 case Intrinsic::x86_avx2_pmaxs_w:
10387 case Intrinsic::x86_avx2_pmaxs_d:
Benjamin Kramer739c7a82012-12-21 14:04:55 +000010388 case Intrinsic::x86_sse41_pminsb:
10389 case Intrinsic::x86_sse2_pmins_w:
10390 case Intrinsic::x86_sse41_pminsd:
10391 case Intrinsic::x86_avx2_pmins_b:
10392 case Intrinsic::x86_avx2_pmins_w:
Craig Topper6f57f392012-12-29 17:19:06 +000010393 case Intrinsic::x86_avx2_pmins_d: {
10394 unsigned Opcode;
10395 switch (IntNo) {
10396 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
10397 case Intrinsic::x86_sse2_pmaxu_b:
10398 case Intrinsic::x86_sse41_pmaxuw:
10399 case Intrinsic::x86_sse41_pmaxud:
10400 case Intrinsic::x86_avx2_pmaxu_b:
10401 case Intrinsic::x86_avx2_pmaxu_w:
10402 case Intrinsic::x86_avx2_pmaxu_d:
10403 Opcode = X86ISD::UMAX;
10404 break;
10405 case Intrinsic::x86_sse2_pminu_b:
10406 case Intrinsic::x86_sse41_pminuw:
10407 case Intrinsic::x86_sse41_pminud:
10408 case Intrinsic::x86_avx2_pminu_b:
10409 case Intrinsic::x86_avx2_pminu_w:
10410 case Intrinsic::x86_avx2_pminu_d:
10411 Opcode = X86ISD::UMIN;
10412 break;
10413 case Intrinsic::x86_sse41_pmaxsb:
10414 case Intrinsic::x86_sse2_pmaxs_w:
10415 case Intrinsic::x86_sse41_pmaxsd:
10416 case Intrinsic::x86_avx2_pmaxs_b:
10417 case Intrinsic::x86_avx2_pmaxs_w:
10418 case Intrinsic::x86_avx2_pmaxs_d:
10419 Opcode = X86ISD::SMAX;
10420 break;
10421 case Intrinsic::x86_sse41_pminsb:
10422 case Intrinsic::x86_sse2_pmins_w:
10423 case Intrinsic::x86_sse41_pminsd:
10424 case Intrinsic::x86_avx2_pmins_b:
10425 case Intrinsic::x86_avx2_pmins_w:
10426 case Intrinsic::x86_avx2_pmins_d:
10427 Opcode = X86ISD::SMIN;
10428 break;
10429 }
10430 return DAG.getNode(Opcode, dl, Op.getValueType(),
Benjamin Kramer739c7a82012-12-21 14:04:55 +000010431 Op.getOperand(1), Op.getOperand(2));
Craig Topper6f57f392012-12-29 17:19:06 +000010432 }
Benjamin Kramer739c7a82012-12-21 14:04:55 +000010433
Craig Topper6d183e42012-12-29 16:44:25 +000010434 // SSE/SSE2/AVX floating point max/min intrinsics.
10435 case Intrinsic::x86_sse_max_ps:
10436 case Intrinsic::x86_sse2_max_pd:
10437 case Intrinsic::x86_avx_max_ps_256:
10438 case Intrinsic::x86_avx_max_pd_256:
10439 case Intrinsic::x86_sse_min_ps:
10440 case Intrinsic::x86_sse2_min_pd:
10441 case Intrinsic::x86_avx_min_ps_256:
10442 case Intrinsic::x86_avx_min_pd_256: {
10443 unsigned Opcode;
10444 switch (IntNo) {
10445 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
10446 case Intrinsic::x86_sse_max_ps:
10447 case Intrinsic::x86_sse2_max_pd:
10448 case Intrinsic::x86_avx_max_ps_256:
10449 case Intrinsic::x86_avx_max_pd_256:
10450 Opcode = X86ISD::FMAX;
10451 break;
10452 case Intrinsic::x86_sse_min_ps:
10453 case Intrinsic::x86_sse2_min_pd:
10454 case Intrinsic::x86_avx_min_ps_256:
10455 case Intrinsic::x86_avx_min_pd_256:
10456 Opcode = X86ISD::FMIN;
10457 break;
10458 }
10459 return DAG.getNode(Opcode, dl, Op.getValueType(),
10460 Op.getOperand(1), Op.getOperand(2));
10461 }
10462
Craig Topper6d688152012-08-14 07:43:25 +000010463 // AVX2 variable shift intrinsics
Craig Topper98fc7292011-11-19 17:46:46 +000010464 case Intrinsic::x86_avx2_psllv_d:
10465 case Intrinsic::x86_avx2_psllv_q:
10466 case Intrinsic::x86_avx2_psllv_d_256:
10467 case Intrinsic::x86_avx2_psllv_q_256:
Craig Topper98fc7292011-11-19 17:46:46 +000010468 case Intrinsic::x86_avx2_psrlv_d:
10469 case Intrinsic::x86_avx2_psrlv_q:
10470 case Intrinsic::x86_avx2_psrlv_d_256:
10471 case Intrinsic::x86_avx2_psrlv_q_256:
Craig Topper98fc7292011-11-19 17:46:46 +000010472 case Intrinsic::x86_avx2_psrav_d:
Craig Topper6d688152012-08-14 07:43:25 +000010473 case Intrinsic::x86_avx2_psrav_d_256: {
10474 unsigned Opcode;
10475 switch (IntNo) {
10476 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
10477 case Intrinsic::x86_avx2_psllv_d:
10478 case Intrinsic::x86_avx2_psllv_q:
10479 case Intrinsic::x86_avx2_psllv_d_256:
10480 case Intrinsic::x86_avx2_psllv_q_256:
10481 Opcode = ISD::SHL;
10482 break;
10483 case Intrinsic::x86_avx2_psrlv_d:
10484 case Intrinsic::x86_avx2_psrlv_q:
10485 case Intrinsic::x86_avx2_psrlv_d_256:
10486 case Intrinsic::x86_avx2_psrlv_q_256:
10487 Opcode = ISD::SRL;
10488 break;
10489 case Intrinsic::x86_avx2_psrav_d:
10490 case Intrinsic::x86_avx2_psrav_d_256:
10491 Opcode = ISD::SRA;
10492 break;
10493 }
10494 return DAG.getNode(Opcode, dl, Op.getValueType(),
10495 Op.getOperand(1), Op.getOperand(2));
10496 }
10497
Craig Topper969ba282012-01-25 06:43:11 +000010498 case Intrinsic::x86_ssse3_pshuf_b_128:
10499 case Intrinsic::x86_avx2_pshuf_b:
10500 return DAG.getNode(X86ISD::PSHUFB, dl, Op.getValueType(),
10501 Op.getOperand(1), Op.getOperand(2));
Craig Topper6d688152012-08-14 07:43:25 +000010502
Craig Topper969ba282012-01-25 06:43:11 +000010503 case Intrinsic::x86_ssse3_psign_b_128:
10504 case Intrinsic::x86_ssse3_psign_w_128:
10505 case Intrinsic::x86_ssse3_psign_d_128:
10506 case Intrinsic::x86_avx2_psign_b:
10507 case Intrinsic::x86_avx2_psign_w:
10508 case Intrinsic::x86_avx2_psign_d:
10509 return DAG.getNode(X86ISD::PSIGN, dl, Op.getValueType(),
10510 Op.getOperand(1), Op.getOperand(2));
Craig Topper6d688152012-08-14 07:43:25 +000010511
Craig Toppere566cd02012-01-26 07:18:03 +000010512 case Intrinsic::x86_sse41_insertps:
10513 return DAG.getNode(X86ISD::INSERTPS, dl, Op.getValueType(),
10514 Op.getOperand(1), Op.getOperand(2), Op.getOperand(3));
Craig Topper6d688152012-08-14 07:43:25 +000010515
Craig Toppere566cd02012-01-26 07:18:03 +000010516 case Intrinsic::x86_avx_vperm2f128_ps_256:
10517 case Intrinsic::x86_avx_vperm2f128_pd_256:
10518 case Intrinsic::x86_avx_vperm2f128_si_256:
10519 case Intrinsic::x86_avx2_vperm2i128:
10520 return DAG.getNode(X86ISD::VPERM2X128, dl, Op.getValueType(),
10521 Op.getOperand(1), Op.getOperand(2), Op.getOperand(3));
Craig Topper6d688152012-08-14 07:43:25 +000010522
Craig Topperffa6c402012-04-16 07:13:00 +000010523 case Intrinsic::x86_avx2_permd:
10524 case Intrinsic::x86_avx2_permps:
10525 // Operands intentionally swapped. Mask is last operand to intrinsic,
10526 // but second operand for node/intruction.
10527 return DAG.getNode(X86ISD::VPERMV, dl, Op.getValueType(),
10528 Op.getOperand(2), Op.getOperand(1));
Craig Topper98fc7292011-11-19 17:46:46 +000010529
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +000010530 // ptest and testp intrinsics. The intrinsic these come from are designed to
10531 // return an integer value, not just an instruction so lower it to the ptest
10532 // or testp pattern and a setcc for the result.
Eric Christopher71c67532009-07-29 00:28:05 +000010533 case Intrinsic::x86_sse41_ptestz:
10534 case Intrinsic::x86_sse41_ptestc:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +000010535 case Intrinsic::x86_sse41_ptestnzc:
10536 case Intrinsic::x86_avx_ptestz_256:
10537 case Intrinsic::x86_avx_ptestc_256:
10538 case Intrinsic::x86_avx_ptestnzc_256:
10539 case Intrinsic::x86_avx_vtestz_ps:
10540 case Intrinsic::x86_avx_vtestc_ps:
10541 case Intrinsic::x86_avx_vtestnzc_ps:
10542 case Intrinsic::x86_avx_vtestz_pd:
10543 case Intrinsic::x86_avx_vtestc_pd:
10544 case Intrinsic::x86_avx_vtestnzc_pd:
10545 case Intrinsic::x86_avx_vtestz_ps_256:
10546 case Intrinsic::x86_avx_vtestc_ps_256:
10547 case Intrinsic::x86_avx_vtestnzc_ps_256:
10548 case Intrinsic::x86_avx_vtestz_pd_256:
10549 case Intrinsic::x86_avx_vtestc_pd_256:
10550 case Intrinsic::x86_avx_vtestnzc_pd_256: {
10551 bool IsTestPacked = false;
Craig Topper6d688152012-08-14 07:43:25 +000010552 unsigned X86CC;
Eric Christopher71c67532009-07-29 00:28:05 +000010553 switch (IntNo) {
Eric Christopher978dae32009-07-29 18:14:04 +000010554 default: llvm_unreachable("Bad fallthrough in Intrinsic lowering.");
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +000010555 case Intrinsic::x86_avx_vtestz_ps:
10556 case Intrinsic::x86_avx_vtestz_pd:
10557 case Intrinsic::x86_avx_vtestz_ps_256:
10558 case Intrinsic::x86_avx_vtestz_pd_256:
10559 IsTestPacked = true; // Fallthrough
Eric Christopher71c67532009-07-29 00:28:05 +000010560 case Intrinsic::x86_sse41_ptestz:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +000010561 case Intrinsic::x86_avx_ptestz_256:
Eric Christopher71c67532009-07-29 00:28:05 +000010562 // ZF = 1
10563 X86CC = X86::COND_E;
10564 break;
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +000010565 case Intrinsic::x86_avx_vtestc_ps:
10566 case Intrinsic::x86_avx_vtestc_pd:
10567 case Intrinsic::x86_avx_vtestc_ps_256:
10568 case Intrinsic::x86_avx_vtestc_pd_256:
10569 IsTestPacked = true; // Fallthrough
Eric Christopher71c67532009-07-29 00:28:05 +000010570 case Intrinsic::x86_sse41_ptestc:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +000010571 case Intrinsic::x86_avx_ptestc_256:
Eric Christopher71c67532009-07-29 00:28:05 +000010572 // CF = 1
10573 X86CC = X86::COND_B;
10574 break;
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +000010575 case Intrinsic::x86_avx_vtestnzc_ps:
10576 case Intrinsic::x86_avx_vtestnzc_pd:
10577 case Intrinsic::x86_avx_vtestnzc_ps_256:
10578 case Intrinsic::x86_avx_vtestnzc_pd_256:
10579 IsTestPacked = true; // Fallthrough
Eric Christopherfd179292009-08-27 18:07:15 +000010580 case Intrinsic::x86_sse41_ptestnzc:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +000010581 case Intrinsic::x86_avx_ptestnzc_256:
Eric Christopher71c67532009-07-29 00:28:05 +000010582 // ZF and CF = 0
10583 X86CC = X86::COND_A;
10584 break;
10585 }
Eric Christopherfd179292009-08-27 18:07:15 +000010586
Eric Christopher71c67532009-07-29 00:28:05 +000010587 SDValue LHS = Op.getOperand(1);
10588 SDValue RHS = Op.getOperand(2);
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +000010589 unsigned TestOpc = IsTestPacked ? X86ISD::TESTP : X86ISD::PTEST;
10590 SDValue Test = DAG.getNode(TestOpc, dl, MVT::i32, LHS, RHS);
Owen Anderson825b72b2009-08-11 20:47:22 +000010591 SDValue CC = DAG.getConstant(X86CC, MVT::i8);
10592 SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8, CC, Test);
10593 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC);
Eric Christopher71c67532009-07-29 00:28:05 +000010594 }
Evan Cheng5759f972008-05-04 09:15:50 +000010595
Craig Topper80e46362012-01-23 06:16:53 +000010596 // SSE/AVX shift intrinsics
10597 case Intrinsic::x86_sse2_psll_w:
10598 case Intrinsic::x86_sse2_psll_d:
10599 case Intrinsic::x86_sse2_psll_q:
10600 case Intrinsic::x86_avx2_psll_w:
10601 case Intrinsic::x86_avx2_psll_d:
10602 case Intrinsic::x86_avx2_psll_q:
Craig Topper80e46362012-01-23 06:16:53 +000010603 case Intrinsic::x86_sse2_psrl_w:
10604 case Intrinsic::x86_sse2_psrl_d:
10605 case Intrinsic::x86_sse2_psrl_q:
10606 case Intrinsic::x86_avx2_psrl_w:
10607 case Intrinsic::x86_avx2_psrl_d:
10608 case Intrinsic::x86_avx2_psrl_q:
Craig Topper80e46362012-01-23 06:16:53 +000010609 case Intrinsic::x86_sse2_psra_w:
10610 case Intrinsic::x86_sse2_psra_d:
10611 case Intrinsic::x86_avx2_psra_w:
Craig Topper6d688152012-08-14 07:43:25 +000010612 case Intrinsic::x86_avx2_psra_d: {
10613 unsigned Opcode;
10614 switch (IntNo) {
10615 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
10616 case Intrinsic::x86_sse2_psll_w:
10617 case Intrinsic::x86_sse2_psll_d:
10618 case Intrinsic::x86_sse2_psll_q:
10619 case Intrinsic::x86_avx2_psll_w:
10620 case Intrinsic::x86_avx2_psll_d:
10621 case Intrinsic::x86_avx2_psll_q:
10622 Opcode = X86ISD::VSHL;
10623 break;
10624 case Intrinsic::x86_sse2_psrl_w:
10625 case Intrinsic::x86_sse2_psrl_d:
10626 case Intrinsic::x86_sse2_psrl_q:
10627 case Intrinsic::x86_avx2_psrl_w:
10628 case Intrinsic::x86_avx2_psrl_d:
10629 case Intrinsic::x86_avx2_psrl_q:
10630 Opcode = X86ISD::VSRL;
10631 break;
10632 case Intrinsic::x86_sse2_psra_w:
10633 case Intrinsic::x86_sse2_psra_d:
10634 case Intrinsic::x86_avx2_psra_w:
10635 case Intrinsic::x86_avx2_psra_d:
10636 Opcode = X86ISD::VSRA;
10637 break;
10638 }
10639 return DAG.getNode(Opcode, dl, Op.getValueType(),
Craig Topper80e46362012-01-23 06:16:53 +000010640 Op.getOperand(1), Op.getOperand(2));
Craig Topper6d688152012-08-14 07:43:25 +000010641 }
10642
10643 // SSE/AVX immediate shift intrinsics
Evan Cheng5759f972008-05-04 09:15:50 +000010644 case Intrinsic::x86_sse2_pslli_w:
10645 case Intrinsic::x86_sse2_pslli_d:
10646 case Intrinsic::x86_sse2_pslli_q:
Craig Topper80e46362012-01-23 06:16:53 +000010647 case Intrinsic::x86_avx2_pslli_w:
10648 case Intrinsic::x86_avx2_pslli_d:
10649 case Intrinsic::x86_avx2_pslli_q:
Evan Cheng5759f972008-05-04 09:15:50 +000010650 case Intrinsic::x86_sse2_psrli_w:
10651 case Intrinsic::x86_sse2_psrli_d:
10652 case Intrinsic::x86_sse2_psrli_q:
Craig Topper80e46362012-01-23 06:16:53 +000010653 case Intrinsic::x86_avx2_psrli_w:
10654 case Intrinsic::x86_avx2_psrli_d:
10655 case Intrinsic::x86_avx2_psrli_q:
Evan Cheng5759f972008-05-04 09:15:50 +000010656 case Intrinsic::x86_sse2_psrai_w:
10657 case Intrinsic::x86_sse2_psrai_d:
Craig Topper80e46362012-01-23 06:16:53 +000010658 case Intrinsic::x86_avx2_psrai_w:
Craig Topper6d688152012-08-14 07:43:25 +000010659 case Intrinsic::x86_avx2_psrai_d: {
10660 unsigned Opcode;
10661 switch (IntNo) {
10662 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
10663 case Intrinsic::x86_sse2_pslli_w:
10664 case Intrinsic::x86_sse2_pslli_d:
10665 case Intrinsic::x86_sse2_pslli_q:
10666 case Intrinsic::x86_avx2_pslli_w:
10667 case Intrinsic::x86_avx2_pslli_d:
10668 case Intrinsic::x86_avx2_pslli_q:
10669 Opcode = X86ISD::VSHLI;
10670 break;
10671 case Intrinsic::x86_sse2_psrli_w:
10672 case Intrinsic::x86_sse2_psrli_d:
10673 case Intrinsic::x86_sse2_psrli_q:
10674 case Intrinsic::x86_avx2_psrli_w:
10675 case Intrinsic::x86_avx2_psrli_d:
10676 case Intrinsic::x86_avx2_psrli_q:
10677 Opcode = X86ISD::VSRLI;
10678 break;
10679 case Intrinsic::x86_sse2_psrai_w:
10680 case Intrinsic::x86_sse2_psrai_d:
10681 case Intrinsic::x86_avx2_psrai_w:
10682 case Intrinsic::x86_avx2_psrai_d:
10683 Opcode = X86ISD::VSRAI;
10684 break;
10685 }
10686 return getTargetVShiftNode(Opcode, dl, Op.getValueType(),
Craig Topper80e46362012-01-23 06:16:53 +000010687 Op.getOperand(1), Op.getOperand(2), DAG);
Craig Topper6d688152012-08-14 07:43:25 +000010688 }
10689
Craig Topper4feb6472012-08-06 06:22:36 +000010690 case Intrinsic::x86_sse42_pcmpistria128:
10691 case Intrinsic::x86_sse42_pcmpestria128:
10692 case Intrinsic::x86_sse42_pcmpistric128:
10693 case Intrinsic::x86_sse42_pcmpestric128:
10694 case Intrinsic::x86_sse42_pcmpistrio128:
10695 case Intrinsic::x86_sse42_pcmpestrio128:
10696 case Intrinsic::x86_sse42_pcmpistris128:
10697 case Intrinsic::x86_sse42_pcmpestris128:
10698 case Intrinsic::x86_sse42_pcmpistriz128:
10699 case Intrinsic::x86_sse42_pcmpestriz128: {
10700 unsigned Opcode;
10701 unsigned X86CC;
10702 switch (IntNo) {
10703 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
10704 case Intrinsic::x86_sse42_pcmpistria128:
10705 Opcode = X86ISD::PCMPISTRI;
10706 X86CC = X86::COND_A;
10707 break;
10708 case Intrinsic::x86_sse42_pcmpestria128:
10709 Opcode = X86ISD::PCMPESTRI;
10710 X86CC = X86::COND_A;
10711 break;
10712 case Intrinsic::x86_sse42_pcmpistric128:
10713 Opcode = X86ISD::PCMPISTRI;
10714 X86CC = X86::COND_B;
10715 break;
10716 case Intrinsic::x86_sse42_pcmpestric128:
10717 Opcode = X86ISD::PCMPESTRI;
10718 X86CC = X86::COND_B;
10719 break;
10720 case Intrinsic::x86_sse42_pcmpistrio128:
10721 Opcode = X86ISD::PCMPISTRI;
10722 X86CC = X86::COND_O;
10723 break;
10724 case Intrinsic::x86_sse42_pcmpestrio128:
10725 Opcode = X86ISD::PCMPESTRI;
10726 X86CC = X86::COND_O;
10727 break;
10728 case Intrinsic::x86_sse42_pcmpistris128:
10729 Opcode = X86ISD::PCMPISTRI;
10730 X86CC = X86::COND_S;
10731 break;
10732 case Intrinsic::x86_sse42_pcmpestris128:
10733 Opcode = X86ISD::PCMPESTRI;
10734 X86CC = X86::COND_S;
10735 break;
10736 case Intrinsic::x86_sse42_pcmpistriz128:
10737 Opcode = X86ISD::PCMPISTRI;
10738 X86CC = X86::COND_E;
10739 break;
10740 case Intrinsic::x86_sse42_pcmpestriz128:
10741 Opcode = X86ISD::PCMPESTRI;
10742 X86CC = X86::COND_E;
10743 break;
10744 }
10745 SmallVector<SDValue, 5> NewOps;
10746 NewOps.append(Op->op_begin()+1, Op->op_end());
10747 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::i32);
10748 SDValue PCMP = DAG.getNode(Opcode, dl, VTs, NewOps.data(), NewOps.size());
10749 SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
10750 DAG.getConstant(X86CC, MVT::i8),
10751 SDValue(PCMP.getNode(), 1));
10752 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC);
10753 }
Craig Topper6d688152012-08-14 07:43:25 +000010754
Craig Topper4feb6472012-08-06 06:22:36 +000010755 case Intrinsic::x86_sse42_pcmpistri128:
10756 case Intrinsic::x86_sse42_pcmpestri128: {
10757 unsigned Opcode;
10758 if (IntNo == Intrinsic::x86_sse42_pcmpistri128)
10759 Opcode = X86ISD::PCMPISTRI;
10760 else
10761 Opcode = X86ISD::PCMPESTRI;
10762
10763 SmallVector<SDValue, 5> NewOps;
10764 NewOps.append(Op->op_begin()+1, Op->op_end());
10765 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::i32);
10766 return DAG.getNode(Opcode, dl, VTs, NewOps.data(), NewOps.size());
10767 }
Craig Topper0e292372012-08-24 04:03:22 +000010768 case Intrinsic::x86_fma_vfmadd_ps:
10769 case Intrinsic::x86_fma_vfmadd_pd:
10770 case Intrinsic::x86_fma_vfmsub_ps:
10771 case Intrinsic::x86_fma_vfmsub_pd:
10772 case Intrinsic::x86_fma_vfnmadd_ps:
10773 case Intrinsic::x86_fma_vfnmadd_pd:
10774 case Intrinsic::x86_fma_vfnmsub_ps:
10775 case Intrinsic::x86_fma_vfnmsub_pd:
10776 case Intrinsic::x86_fma_vfmaddsub_ps:
10777 case Intrinsic::x86_fma_vfmaddsub_pd:
10778 case Intrinsic::x86_fma_vfmsubadd_ps:
10779 case Intrinsic::x86_fma_vfmsubadd_pd:
10780 case Intrinsic::x86_fma_vfmadd_ps_256:
10781 case Intrinsic::x86_fma_vfmadd_pd_256:
10782 case Intrinsic::x86_fma_vfmsub_ps_256:
10783 case Intrinsic::x86_fma_vfmsub_pd_256:
10784 case Intrinsic::x86_fma_vfnmadd_ps_256:
10785 case Intrinsic::x86_fma_vfnmadd_pd_256:
10786 case Intrinsic::x86_fma_vfnmsub_ps_256:
10787 case Intrinsic::x86_fma_vfnmsub_pd_256:
10788 case Intrinsic::x86_fma_vfmaddsub_ps_256:
10789 case Intrinsic::x86_fma_vfmaddsub_pd_256:
10790 case Intrinsic::x86_fma_vfmsubadd_ps_256:
10791 case Intrinsic::x86_fma_vfmsubadd_pd_256: {
Craig Topper0e292372012-08-24 04:03:22 +000010792 unsigned Opc;
10793 switch (IntNo) {
10794 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
10795 case Intrinsic::x86_fma_vfmadd_ps:
10796 case Intrinsic::x86_fma_vfmadd_pd:
10797 case Intrinsic::x86_fma_vfmadd_ps_256:
10798 case Intrinsic::x86_fma_vfmadd_pd_256:
10799 Opc = X86ISD::FMADD;
10800 break;
10801 case Intrinsic::x86_fma_vfmsub_ps:
10802 case Intrinsic::x86_fma_vfmsub_pd:
10803 case Intrinsic::x86_fma_vfmsub_ps_256:
10804 case Intrinsic::x86_fma_vfmsub_pd_256:
10805 Opc = X86ISD::FMSUB;
10806 break;
10807 case Intrinsic::x86_fma_vfnmadd_ps:
10808 case Intrinsic::x86_fma_vfnmadd_pd:
10809 case Intrinsic::x86_fma_vfnmadd_ps_256:
10810 case Intrinsic::x86_fma_vfnmadd_pd_256:
10811 Opc = X86ISD::FNMADD;
10812 break;
10813 case Intrinsic::x86_fma_vfnmsub_ps:
10814 case Intrinsic::x86_fma_vfnmsub_pd:
10815 case Intrinsic::x86_fma_vfnmsub_ps_256:
10816 case Intrinsic::x86_fma_vfnmsub_pd_256:
10817 Opc = X86ISD::FNMSUB;
10818 break;
10819 case Intrinsic::x86_fma_vfmaddsub_ps:
10820 case Intrinsic::x86_fma_vfmaddsub_pd:
10821 case Intrinsic::x86_fma_vfmaddsub_ps_256:
10822 case Intrinsic::x86_fma_vfmaddsub_pd_256:
10823 Opc = X86ISD::FMADDSUB;
10824 break;
10825 case Intrinsic::x86_fma_vfmsubadd_ps:
10826 case Intrinsic::x86_fma_vfmsubadd_pd:
10827 case Intrinsic::x86_fma_vfmsubadd_ps_256:
10828 case Intrinsic::x86_fma_vfmsubadd_pd_256:
10829 Opc = X86ISD::FMSUBADD;
10830 break;
10831 }
10832
10833 return DAG.getNode(Opc, dl, Op.getValueType(), Op.getOperand(1),
10834 Op.getOperand(2), Op.getOperand(3));
10835 }
Evan Cheng38bcbaf2005-12-23 07:31:11 +000010836 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000010837}
Evan Cheng72261582005-12-20 06:22:03 +000010838
Craig Topper55b24052012-09-11 06:15:32 +000010839static SDValue LowerINTRINSIC_W_CHAIN(SDValue Op, SelectionDAG &DAG) {
Benjamin Kramerb9bee042012-07-12 09:31:43 +000010840 DebugLoc dl = Op.getDebugLoc();
10841 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
10842 switch (IntNo) {
10843 default: return SDValue(); // Don't custom lower most intrinsics.
10844
10845 // RDRAND intrinsics.
10846 case Intrinsic::x86_rdrand_16:
10847 case Intrinsic::x86_rdrand_32:
10848 case Intrinsic::x86_rdrand_64: {
10849 // Emit the node with the right value type.
Benjamin Kramerfeae00a2012-07-12 18:14:57 +000010850 SDVTList VTs = DAG.getVTList(Op->getValueType(0), MVT::Glue, MVT::Other);
10851 SDValue Result = DAG.getNode(X86ISD::RDRAND, dl, VTs, Op.getOperand(0));
Benjamin Kramerb9bee042012-07-12 09:31:43 +000010852
10853 // If the value returned by RDRAND was valid (CF=1), return 1. Otherwise
10854 // return the value from Rand, which is always 0, casted to i32.
10855 SDValue Ops[] = { DAG.getZExtOrTrunc(Result, dl, Op->getValueType(1)),
10856 DAG.getConstant(1, Op->getValueType(1)),
10857 DAG.getConstant(X86::COND_B, MVT::i32),
10858 SDValue(Result.getNode(), 1) };
10859 SDValue isValid = DAG.getNode(X86ISD::CMOV, dl,
10860 DAG.getVTList(Op->getValueType(1), MVT::Glue),
10861 Ops, 4);
10862
10863 // Return { result, isValid, chain }.
10864 return DAG.getNode(ISD::MERGE_VALUES, dl, Op->getVTList(), Result, isValid,
Benjamin Kramerfeae00a2012-07-12 18:14:57 +000010865 SDValue(Result.getNode(), 2));
Benjamin Kramerb9bee042012-07-12 09:31:43 +000010866 }
10867 }
10868}
10869
Dan Gohmand858e902010-04-17 15:26:15 +000010870SDValue X86TargetLowering::LowerRETURNADDR(SDValue Op,
10871 SelectionDAG &DAG) const {
Evan Cheng2457f2c2010-05-22 01:47:14 +000010872 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
10873 MFI->setReturnAddressIsTaken(true);
10874
Bill Wendling64e87322009-01-16 19:25:27 +000010875 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Dale Johannesen6f38cb62009-02-07 19:59:05 +000010876 DebugLoc dl = Op.getDebugLoc();
Michael Liaoaa3c2c02012-10-25 06:29:14 +000010877 EVT PtrVT = getPointerTy();
Bill Wendling64e87322009-01-16 19:25:27 +000010878
10879 if (Depth > 0) {
10880 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
10881 SDValue Offset =
Michael Liaoaa3c2c02012-10-25 06:29:14 +000010882 DAG.getConstant(RegInfo->getSlotSize(), PtrVT);
10883 return DAG.getLoad(PtrVT, dl, DAG.getEntryNode(),
10884 DAG.getNode(ISD::ADD, dl, PtrVT,
Dale Johannesene4d209d2009-02-03 20:21:25 +000010885 FrameAddr, Offset),
Pete Cooperd752e0f2011-11-08 18:42:53 +000010886 MachinePointerInfo(), false, false, false, 0);
Bill Wendling64e87322009-01-16 19:25:27 +000010887 }
10888
10889 // Just load the return address.
Dan Gohman475871a2008-07-27 21:46:04 +000010890 SDValue RetAddrFI = getReturnAddressFrameIndex(DAG);
Michael Liaoaa3c2c02012-10-25 06:29:14 +000010891 return DAG.getLoad(PtrVT, dl, DAG.getEntryNode(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000010892 RetAddrFI, MachinePointerInfo(), false, false, false, 0);
Nate Begemanbcc5f362007-01-29 22:58:52 +000010893}
10894
Dan Gohmand858e902010-04-17 15:26:15 +000010895SDValue X86TargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng184793f2008-09-27 01:56:22 +000010896 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
10897 MFI->setFrameAddressIsTaken(true);
Evan Cheng2457f2c2010-05-22 01:47:14 +000010898
Owen Andersone50ed302009-08-10 22:56:29 +000010899 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +000010900 DebugLoc dl = Op.getDebugLoc(); // FIXME probably not meaningful
Evan Cheng184793f2008-09-27 01:56:22 +000010901 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
10902 unsigned FrameReg = Subtarget->is64Bit() ? X86::RBP : X86::EBP;
Dale Johannesendd64c412009-02-04 00:33:20 +000010903 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, VT);
Evan Cheng184793f2008-09-27 01:56:22 +000010904 while (Depth--)
Chris Lattner51abfe42010-09-21 06:02:19 +000010905 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr,
10906 MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000010907 false, false, false, 0);
Evan Cheng184793f2008-09-27 01:56:22 +000010908 return FrameAddr;
Nate Begemanbcc5f362007-01-29 22:58:52 +000010909}
10910
Dan Gohman475871a2008-07-27 21:46:04 +000010911SDValue X86TargetLowering::LowerFRAME_TO_ARGS_OFFSET(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +000010912 SelectionDAG &DAG) const {
Michael Liaoaa3c2c02012-10-25 06:29:14 +000010913 return DAG.getIntPtrConstant(2 * RegInfo->getSlotSize());
Anton Korobeynikov2365f512007-07-14 14:06:15 +000010914}
10915
Dan Gohmand858e902010-04-17 15:26:15 +000010916SDValue X86TargetLowering::LowerEH_RETURN(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +000010917 SDValue Chain = Op.getOperand(0);
10918 SDValue Offset = Op.getOperand(1);
10919 SDValue Handler = Op.getOperand(2);
Dale Johannesen6f38cb62009-02-07 19:59:05 +000010920 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov2365f512007-07-14 14:06:15 +000010921
Dan Gohmand8816272010-08-11 18:14:00 +000010922 SDValue Frame = DAG.getCopyFromReg(DAG.getEntryNode(), dl,
10923 Subtarget->is64Bit() ? X86::RBP : X86::EBP,
10924 getPointerTy());
Anton Korobeynikovb84c1672008-09-08 21:12:47 +000010925 unsigned StoreAddrReg = (Subtarget->is64Bit() ? X86::RCX : X86::ECX);
Anton Korobeynikov2365f512007-07-14 14:06:15 +000010926
Dan Gohmand8816272010-08-11 18:14:00 +000010927 SDValue StoreAddr = DAG.getNode(ISD::ADD, dl, getPointerTy(), Frame,
Michael Liaoaa3c2c02012-10-25 06:29:14 +000010928 DAG.getIntPtrConstant(RegInfo->getSlotSize()));
Dale Johannesene4d209d2009-02-03 20:21:25 +000010929 StoreAddr = DAG.getNode(ISD::ADD, dl, getPointerTy(), StoreAddr, Offset);
Chris Lattner8026a9d2010-09-21 17:50:43 +000010930 Chain = DAG.getStore(Chain, dl, Handler, StoreAddr, MachinePointerInfo(),
10931 false, false, 0);
Dale Johannesendd64c412009-02-04 00:33:20 +000010932 Chain = DAG.getCopyToReg(Chain, dl, StoreAddrReg, StoreAddr);
Anton Korobeynikov2365f512007-07-14 14:06:15 +000010933
Dale Johannesene4d209d2009-02-03 20:21:25 +000010934 return DAG.getNode(X86ISD::EH_RETURN, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +000010935 MVT::Other,
Anton Korobeynikovb84c1672008-09-08 21:12:47 +000010936 Chain, DAG.getRegister(StoreAddrReg, getPointerTy()));
Anton Korobeynikov2365f512007-07-14 14:06:15 +000010937}
10938
Michael Liao6c0e04c2012-10-15 22:39:43 +000010939SDValue X86TargetLowering::lowerEH_SJLJ_SETJMP(SDValue Op,
10940 SelectionDAG &DAG) const {
10941 DebugLoc DL = Op.getDebugLoc();
10942 return DAG.getNode(X86ISD::EH_SJLJ_SETJMP, DL,
10943 DAG.getVTList(MVT::i32, MVT::Other),
10944 Op.getOperand(0), Op.getOperand(1));
10945}
10946
10947SDValue X86TargetLowering::lowerEH_SJLJ_LONGJMP(SDValue Op,
10948 SelectionDAG &DAG) const {
10949 DebugLoc DL = Op.getDebugLoc();
10950 return DAG.getNode(X86ISD::EH_SJLJ_LONGJMP, DL, MVT::Other,
10951 Op.getOperand(0), Op.getOperand(1));
10952}
10953
Craig Topper55b24052012-09-11 06:15:32 +000010954static SDValue LowerADJUST_TRAMPOLINE(SDValue Op, SelectionDAG &DAG) {
Duncan Sands4a544a72011-09-06 13:37:06 +000010955 return Op.getOperand(0);
10956}
10957
10958SDValue X86TargetLowering::LowerINIT_TRAMPOLINE(SDValue Op,
10959 SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +000010960 SDValue Root = Op.getOperand(0);
10961 SDValue Trmp = Op.getOperand(1); // trampoline
10962 SDValue FPtr = Op.getOperand(2); // nested function
10963 SDValue Nest = Op.getOperand(3); // 'nest' parameter value
Dale Johannesen6f38cb62009-02-07 19:59:05 +000010964 DebugLoc dl = Op.getDebugLoc();
Duncan Sandsb116fac2007-07-27 20:02:49 +000010965
Dan Gohman69de1932008-02-06 22:27:42 +000010966 const Value *TrmpAddr = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Michael Liao7abf67a2012-10-04 19:50:43 +000010967 const TargetRegisterInfo* TRI = getTargetMachine().getRegisterInfo();
Duncan Sandsb116fac2007-07-27 20:02:49 +000010968
10969 if (Subtarget->is64Bit()) {
Dan Gohman475871a2008-07-27 21:46:04 +000010970 SDValue OutChains[6];
Duncan Sands339e14f2008-01-16 22:55:25 +000010971
10972 // Large code-model.
Chris Lattnera62fe662010-02-05 19:20:30 +000010973 const unsigned char JMP64r = 0xFF; // 64-bit jmp through register opcode.
10974 const unsigned char MOV64ri = 0xB8; // X86::MOV64ri opcode.
Duncan Sands339e14f2008-01-16 22:55:25 +000010975
Michael Liao7abf67a2012-10-04 19:50:43 +000010976 const unsigned char N86R10 = TRI->getEncodingValue(X86::R10) & 0x7;
10977 const unsigned char N86R11 = TRI->getEncodingValue(X86::R11) & 0x7;
Duncan Sands339e14f2008-01-16 22:55:25 +000010978
10979 const unsigned char REX_WB = 0x40 | 0x08 | 0x01; // REX prefix
10980
10981 // Load the pointer to the nested function into R11.
10982 unsigned OpCode = ((MOV64ri | N86R11) << 8) | REX_WB; // movabsq r11
Dan Gohman475871a2008-07-27 21:46:04 +000010983 SDValue Addr = Trmp;
Owen Anderson825b72b2009-08-11 20:47:22 +000010984 OutChains[0] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Chris Lattner8026a9d2010-09-21 17:50:43 +000010985 Addr, MachinePointerInfo(TrmpAddr),
10986 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +000010987
Owen Anderson825b72b2009-08-11 20:47:22 +000010988 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
10989 DAG.getConstant(2, MVT::i64));
Chris Lattner8026a9d2010-09-21 17:50:43 +000010990 OutChains[1] = DAG.getStore(Root, dl, FPtr, Addr,
10991 MachinePointerInfo(TrmpAddr, 2),
David Greene67c9d422010-02-15 16:53:33 +000010992 false, false, 2);
Duncan Sands339e14f2008-01-16 22:55:25 +000010993
10994 // Load the 'nest' parameter value into R10.
10995 // R10 is specified in X86CallingConv.td
10996 OpCode = ((MOV64ri | N86R10) << 8) | REX_WB; // movabsq r10
Owen Anderson825b72b2009-08-11 20:47:22 +000010997 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
10998 DAG.getConstant(10, MVT::i64));
10999 OutChains[2] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Chris Lattner8026a9d2010-09-21 17:50:43 +000011000 Addr, MachinePointerInfo(TrmpAddr, 10),
11001 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +000011002
Owen Anderson825b72b2009-08-11 20:47:22 +000011003 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
11004 DAG.getConstant(12, MVT::i64));
Chris Lattner8026a9d2010-09-21 17:50:43 +000011005 OutChains[3] = DAG.getStore(Root, dl, Nest, Addr,
11006 MachinePointerInfo(TrmpAddr, 12),
David Greene67c9d422010-02-15 16:53:33 +000011007 false, false, 2);
Duncan Sands339e14f2008-01-16 22:55:25 +000011008
11009 // Jump to the nested function.
11010 OpCode = (JMP64r << 8) | REX_WB; // jmpq *...
Owen Anderson825b72b2009-08-11 20:47:22 +000011011 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
11012 DAG.getConstant(20, MVT::i64));
11013 OutChains[4] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Chris Lattner8026a9d2010-09-21 17:50:43 +000011014 Addr, MachinePointerInfo(TrmpAddr, 20),
11015 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +000011016
11017 unsigned char ModRM = N86R11 | (4 << 3) | (3 << 6); // ...r11
Owen Anderson825b72b2009-08-11 20:47:22 +000011018 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
11019 DAG.getConstant(22, MVT::i64));
11020 OutChains[5] = DAG.getStore(Root, dl, DAG.getConstant(ModRM, MVT::i8), Addr,
Chris Lattner8026a9d2010-09-21 17:50:43 +000011021 MachinePointerInfo(TrmpAddr, 22),
11022 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +000011023
Duncan Sands4a544a72011-09-06 13:37:06 +000011024 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains, 6);
Duncan Sandsb116fac2007-07-27 20:02:49 +000011025 } else {
Dan Gohmanbbfb9c52008-01-31 01:01:48 +000011026 const Function *Func =
Duncan Sandsb116fac2007-07-27 20:02:49 +000011027 cast<Function>(cast<SrcValueSDNode>(Op.getOperand(5))->getValue());
Sandeep Patel65c3c8f2009-09-02 08:44:58 +000011028 CallingConv::ID CC = Func->getCallingConv();
Duncan Sandsee465742007-08-29 19:01:20 +000011029 unsigned NestReg;
Duncan Sandsb116fac2007-07-27 20:02:49 +000011030
11031 switch (CC) {
11032 default:
Torok Edwinc23197a2009-07-14 16:55:14 +000011033 llvm_unreachable("Unsupported calling convention");
Duncan Sandsb116fac2007-07-27 20:02:49 +000011034 case CallingConv::C:
Duncan Sandsb116fac2007-07-27 20:02:49 +000011035 case CallingConv::X86_StdCall: {
11036 // Pass 'nest' parameter in ECX.
11037 // Must be kept in sync with X86CallingConv.td
Duncan Sandsee465742007-08-29 19:01:20 +000011038 NestReg = X86::ECX;
Duncan Sandsb116fac2007-07-27 20:02:49 +000011039
11040 // Check that ECX wasn't needed by an 'inreg' parameter.
Chris Lattnerdb125cf2011-07-18 04:54:35 +000011041 FunctionType *FTy = Func->getFunctionType();
Bill Wendling99faa3b2012-12-07 23:16:57 +000011042 const AttributeSet &Attrs = Func->getAttributes();
Duncan Sandsb116fac2007-07-27 20:02:49 +000011043
Chris Lattner58d74912008-03-12 17:45:29 +000011044 if (!Attrs.isEmpty() && !Func->isVarArg()) {
Duncan Sandsb116fac2007-07-27 20:02:49 +000011045 unsigned InRegCount = 0;
11046 unsigned Idx = 1;
11047
11048 for (FunctionType::param_iterator I = FTy->param_begin(),
11049 E = FTy->param_end(); I != E; ++I, ++Idx)
Bill Wendling034b94b2012-12-19 07:18:57 +000011050 if (Attrs.getParamAttributes(Idx).hasAttribute(Attribute::InReg))
Duncan Sandsb116fac2007-07-27 20:02:49 +000011051 // FIXME: should only count parameters that are lowered to integers.
Anton Korobeynikovbff66b02008-09-09 18:22:57 +000011052 InRegCount += (TD->getTypeSizeInBits(*I) + 31) / 32;
Duncan Sandsb116fac2007-07-27 20:02:49 +000011053
11054 if (InRegCount > 2) {
Eric Christopher90eb4022010-07-22 00:26:08 +000011055 report_fatal_error("Nest register in use - reduce number of inreg"
11056 " parameters!");
Duncan Sandsb116fac2007-07-27 20:02:49 +000011057 }
11058 }
11059 break;
11060 }
11061 case CallingConv::X86_FastCall:
Anton Korobeynikovded05e32010-05-16 09:08:45 +000011062 case CallingConv::X86_ThisCall:
Duncan Sandsbf53c292008-09-10 13:22:10 +000011063 case CallingConv::Fast:
Duncan Sandsb116fac2007-07-27 20:02:49 +000011064 // Pass 'nest' parameter in EAX.
11065 // Must be kept in sync with X86CallingConv.td
Duncan Sandsee465742007-08-29 19:01:20 +000011066 NestReg = X86::EAX;
Duncan Sandsb116fac2007-07-27 20:02:49 +000011067 break;
11068 }
11069
Dan Gohman475871a2008-07-27 21:46:04 +000011070 SDValue OutChains[4];
11071 SDValue Addr, Disp;
Duncan Sandsb116fac2007-07-27 20:02:49 +000011072
Owen Anderson825b72b2009-08-11 20:47:22 +000011073 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
11074 DAG.getConstant(10, MVT::i32));
11075 Disp = DAG.getNode(ISD::SUB, dl, MVT::i32, FPtr, Addr);
Duncan Sandsb116fac2007-07-27 20:02:49 +000011076
Chris Lattnera62fe662010-02-05 19:20:30 +000011077 // This is storing the opcode for MOV32ri.
11078 const unsigned char MOV32ri = 0xB8; // X86::MOV32ri's opcode byte.
Michael Liao7abf67a2012-10-04 19:50:43 +000011079 const unsigned char N86Reg = TRI->getEncodingValue(NestReg) & 0x7;
Scott Michelfdc40a02009-02-17 22:15:04 +000011080 OutChains[0] = DAG.getStore(Root, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +000011081 DAG.getConstant(MOV32ri|N86Reg, MVT::i8),
Chris Lattner8026a9d2010-09-21 17:50:43 +000011082 Trmp, MachinePointerInfo(TrmpAddr),
11083 false, false, 0);
Duncan Sandsb116fac2007-07-27 20:02:49 +000011084
Owen Anderson825b72b2009-08-11 20:47:22 +000011085 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
11086 DAG.getConstant(1, MVT::i32));
Chris Lattner8026a9d2010-09-21 17:50:43 +000011087 OutChains[1] = DAG.getStore(Root, dl, Nest, Addr,
11088 MachinePointerInfo(TrmpAddr, 1),
David Greene67c9d422010-02-15 16:53:33 +000011089 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +000011090
Chris Lattnera62fe662010-02-05 19:20:30 +000011091 const unsigned char JMP = 0xE9; // jmp <32bit dst> opcode.
Owen Anderson825b72b2009-08-11 20:47:22 +000011092 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
11093 DAG.getConstant(5, MVT::i32));
11094 OutChains[2] = DAG.getStore(Root, dl, DAG.getConstant(JMP, MVT::i8), Addr,
Chris Lattner8026a9d2010-09-21 17:50:43 +000011095 MachinePointerInfo(TrmpAddr, 5),
11096 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +000011097
Owen Anderson825b72b2009-08-11 20:47:22 +000011098 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
11099 DAG.getConstant(6, MVT::i32));
Chris Lattner8026a9d2010-09-21 17:50:43 +000011100 OutChains[3] = DAG.getStore(Root, dl, Disp, Addr,
11101 MachinePointerInfo(TrmpAddr, 6),
David Greene67c9d422010-02-15 16:53:33 +000011102 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +000011103
Duncan Sands4a544a72011-09-06 13:37:06 +000011104 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains, 4);
Duncan Sandsb116fac2007-07-27 20:02:49 +000011105 }
11106}
11107
Dan Gohmand858e902010-04-17 15:26:15 +000011108SDValue X86TargetLowering::LowerFLT_ROUNDS_(SDValue Op,
11109 SelectionDAG &DAG) const {
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000011110 /*
11111 The rounding mode is in bits 11:10 of FPSR, and has the following
11112 settings:
11113 00 Round to nearest
11114 01 Round to -inf
11115 10 Round to +inf
11116 11 Round to 0
11117
11118 FLT_ROUNDS, on the other hand, expects the following:
11119 -1 Undefined
11120 0 Round to 0
11121 1 Round to nearest
11122 2 Round to +inf
11123 3 Round to -inf
11124
11125 To perform the conversion, we do:
11126 (((((FPSR & 0x800) >> 11) | ((FPSR & 0x400) >> 9)) + 1) & 3)
11127 */
11128
11129 MachineFunction &MF = DAG.getMachineFunction();
11130 const TargetMachine &TM = MF.getTarget();
Anton Korobeynikov16c29b52011-01-10 12:39:04 +000011131 const TargetFrameLowering &TFI = *TM.getFrameLowering();
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000011132 unsigned StackAlignment = TFI.getStackAlignment();
Owen Andersone50ed302009-08-10 22:56:29 +000011133 EVT VT = Op.getValueType();
Chris Lattner2156b792010-09-22 01:11:26 +000011134 DebugLoc DL = Op.getDebugLoc();
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000011135
11136 // Save FP Control Word to stack slot
David Greene3f2bf852009-11-12 20:49:22 +000011137 int SSFI = MF.getFrameInfo()->CreateStackObject(2, StackAlignment, false);
Dan Gohman475871a2008-07-27 21:46:04 +000011138 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000011139
Chris Lattner2156b792010-09-22 01:11:26 +000011140 MachineMemOperand *MMO =
11141 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
11142 MachineMemOperand::MOStore, 2, 2);
Michael J. Spencerec38de22010-10-10 22:04:20 +000011143
Chris Lattner2156b792010-09-22 01:11:26 +000011144 SDValue Ops[] = { DAG.getEntryNode(), StackSlot };
11145 SDValue Chain = DAG.getMemIntrinsicNode(X86ISD::FNSTCW16m, DL,
11146 DAG.getVTList(MVT::Other),
11147 Ops, 2, MVT::i16, MMO);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000011148
11149 // Load FP Control Word from stack slot
Chris Lattner2156b792010-09-22 01:11:26 +000011150 SDValue CWD = DAG.getLoad(MVT::i16, DL, Chain, StackSlot,
Pete Cooperd752e0f2011-11-08 18:42:53 +000011151 MachinePointerInfo(), false, false, false, 0);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000011152
11153 // Transform as necessary
Dan Gohman475871a2008-07-27 21:46:04 +000011154 SDValue CWD1 =
Chris Lattner2156b792010-09-22 01:11:26 +000011155 DAG.getNode(ISD::SRL, DL, MVT::i16,
11156 DAG.getNode(ISD::AND, DL, MVT::i16,
Owen Anderson825b72b2009-08-11 20:47:22 +000011157 CWD, DAG.getConstant(0x800, MVT::i16)),
11158 DAG.getConstant(11, MVT::i8));
Dan Gohman475871a2008-07-27 21:46:04 +000011159 SDValue CWD2 =
Chris Lattner2156b792010-09-22 01:11:26 +000011160 DAG.getNode(ISD::SRL, DL, MVT::i16,
11161 DAG.getNode(ISD::AND, DL, MVT::i16,
Owen Anderson825b72b2009-08-11 20:47:22 +000011162 CWD, DAG.getConstant(0x400, MVT::i16)),
11163 DAG.getConstant(9, MVT::i8));
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000011164
Dan Gohman475871a2008-07-27 21:46:04 +000011165 SDValue RetVal =
Chris Lattner2156b792010-09-22 01:11:26 +000011166 DAG.getNode(ISD::AND, DL, MVT::i16,
11167 DAG.getNode(ISD::ADD, DL, MVT::i16,
11168 DAG.getNode(ISD::OR, DL, MVT::i16, CWD1, CWD2),
Owen Anderson825b72b2009-08-11 20:47:22 +000011169 DAG.getConstant(1, MVT::i16)),
11170 DAG.getConstant(3, MVT::i16));
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000011171
Duncan Sands83ec4b62008-06-06 12:08:01 +000011172 return DAG.getNode((VT.getSizeInBits() < 16 ?
Chris Lattner2156b792010-09-22 01:11:26 +000011173 ISD::TRUNCATE : ISD::ZERO_EXTEND), DL, VT, RetVal);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000011174}
11175
Craig Topper55b24052012-09-11 06:15:32 +000011176static SDValue LowerCTLZ(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +000011177 EVT VT = Op.getValueType();
11178 EVT OpVT = VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +000011179 unsigned NumBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +000011180 DebugLoc dl = Op.getDebugLoc();
Evan Cheng18efe262007-12-14 02:13:44 +000011181
11182 Op = Op.getOperand(0);
Owen Anderson825b72b2009-08-11 20:47:22 +000011183 if (VT == MVT::i8) {
Evan Cheng152804e2007-12-14 08:30:15 +000011184 // Zero extend to i32 since there is not an i8 bsr.
Owen Anderson825b72b2009-08-11 20:47:22 +000011185 OpVT = MVT::i32;
Dale Johannesene4d209d2009-02-03 20:21:25 +000011186 Op = DAG.getNode(ISD::ZERO_EXTEND, dl, OpVT, Op);
Evan Cheng18efe262007-12-14 02:13:44 +000011187 }
Evan Cheng18efe262007-12-14 02:13:44 +000011188
Evan Cheng152804e2007-12-14 08:30:15 +000011189 // Issue a bsr (scan bits in reverse) which also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +000011190 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +000011191 Op = DAG.getNode(X86ISD::BSR, dl, VTs, Op);
Evan Cheng152804e2007-12-14 08:30:15 +000011192
11193 // If src is zero (i.e. bsr sets ZF), returns NumBits.
Benjamin Kramer7f1a5602009-12-29 16:57:26 +000011194 SDValue Ops[] = {
11195 Op,
11196 DAG.getConstant(NumBits+NumBits-1, OpVT),
11197 DAG.getConstant(X86::COND_E, MVT::i8),
11198 Op.getValue(1)
11199 };
11200 Op = DAG.getNode(X86ISD::CMOV, dl, OpVT, Ops, array_lengthof(Ops));
Evan Cheng152804e2007-12-14 08:30:15 +000011201
11202 // Finally xor with NumBits-1.
Dale Johannesene4d209d2009-02-03 20:21:25 +000011203 Op = DAG.getNode(ISD::XOR, dl, OpVT, Op, DAG.getConstant(NumBits-1, OpVT));
Evan Cheng152804e2007-12-14 08:30:15 +000011204
Owen Anderson825b72b2009-08-11 20:47:22 +000011205 if (VT == MVT::i8)
11206 Op = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Op);
Evan Cheng18efe262007-12-14 02:13:44 +000011207 return Op;
11208}
11209
Craig Topper55b24052012-09-11 06:15:32 +000011210static SDValue LowerCTLZ_ZERO_UNDEF(SDValue Op, SelectionDAG &DAG) {
Chandler Carruthacc068e2011-12-24 10:55:54 +000011211 EVT VT = Op.getValueType();
11212 EVT OpVT = VT;
11213 unsigned NumBits = VT.getSizeInBits();
11214 DebugLoc dl = Op.getDebugLoc();
11215
11216 Op = Op.getOperand(0);
11217 if (VT == MVT::i8) {
11218 // Zero extend to i32 since there is not an i8 bsr.
11219 OpVT = MVT::i32;
11220 Op = DAG.getNode(ISD::ZERO_EXTEND, dl, OpVT, Op);
11221 }
11222
11223 // Issue a bsr (scan bits in reverse).
11224 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
11225 Op = DAG.getNode(X86ISD::BSR, dl, VTs, Op);
11226
11227 // And xor with NumBits-1.
11228 Op = DAG.getNode(ISD::XOR, dl, OpVT, Op, DAG.getConstant(NumBits-1, OpVT));
11229
11230 if (VT == MVT::i8)
11231 Op = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Op);
11232 return Op;
11233}
11234
Craig Topper55b24052012-09-11 06:15:32 +000011235static SDValue LowerCTTZ(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +000011236 EVT VT = Op.getValueType();
Duncan Sands83ec4b62008-06-06 12:08:01 +000011237 unsigned NumBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +000011238 DebugLoc dl = Op.getDebugLoc();
Evan Cheng18efe262007-12-14 02:13:44 +000011239 Op = Op.getOperand(0);
Evan Cheng152804e2007-12-14 08:30:15 +000011240
11241 // Issue a bsf (scan bits forward) which also sets EFLAGS.
Chandler Carruth77821022011-12-24 12:12:34 +000011242 SDVTList VTs = DAG.getVTList(VT, MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +000011243 Op = DAG.getNode(X86ISD::BSF, dl, VTs, Op);
Evan Cheng152804e2007-12-14 08:30:15 +000011244
11245 // If src is zero (i.e. bsf sets ZF), returns NumBits.
Benjamin Kramer7f1a5602009-12-29 16:57:26 +000011246 SDValue Ops[] = {
11247 Op,
Chandler Carruth77821022011-12-24 12:12:34 +000011248 DAG.getConstant(NumBits, VT),
Benjamin Kramer7f1a5602009-12-29 16:57:26 +000011249 DAG.getConstant(X86::COND_E, MVT::i8),
11250 Op.getValue(1)
11251 };
Chandler Carruth77821022011-12-24 12:12:34 +000011252 return DAG.getNode(X86ISD::CMOV, dl, VT, Ops, array_lengthof(Ops));
Evan Cheng18efe262007-12-14 02:13:44 +000011253}
11254
Craig Topper13894fa2011-08-24 06:14:18 +000011255// Lower256IntArith - Break a 256-bit integer operation into two new 128-bit
11256// ones, and then concatenate the result back.
11257static SDValue Lower256IntArith(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +000011258 EVT VT = Op.getValueType();
Craig Topper13894fa2011-08-24 06:14:18 +000011259
Craig Topper7a9a28b2012-08-12 02:23:29 +000011260 assert(VT.is256BitVector() && VT.isInteger() &&
Craig Topper13894fa2011-08-24 06:14:18 +000011261 "Unsupported value type for operation");
11262
Craig Topper66ddd152012-04-27 22:54:43 +000011263 unsigned NumElems = VT.getVectorNumElements();
Craig Topper13894fa2011-08-24 06:14:18 +000011264 DebugLoc dl = Op.getDebugLoc();
Craig Topper13894fa2011-08-24 06:14:18 +000011265
11266 // Extract the LHS vectors
11267 SDValue LHS = Op.getOperand(0);
Craig Topperb14940a2012-04-22 20:55:18 +000011268 SDValue LHS1 = Extract128BitVector(LHS, 0, DAG, dl);
11269 SDValue LHS2 = Extract128BitVector(LHS, NumElems/2, DAG, dl);
Craig Topper13894fa2011-08-24 06:14:18 +000011270
11271 // Extract the RHS vectors
11272 SDValue RHS = Op.getOperand(1);
Craig Topperb14940a2012-04-22 20:55:18 +000011273 SDValue RHS1 = Extract128BitVector(RHS, 0, DAG, dl);
11274 SDValue RHS2 = Extract128BitVector(RHS, NumElems/2, DAG, dl);
Craig Topper13894fa2011-08-24 06:14:18 +000011275
11276 MVT EltVT = VT.getVectorElementType().getSimpleVT();
11277 EVT NewVT = MVT::getVectorVT(EltVT, NumElems/2);
11278
11279 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT,
11280 DAG.getNode(Op.getOpcode(), dl, NewVT, LHS1, RHS1),
11281 DAG.getNode(Op.getOpcode(), dl, NewVT, LHS2, RHS2));
11282}
11283
Craig Topper55b24052012-09-11 06:15:32 +000011284static SDValue LowerADD(SDValue Op, SelectionDAG &DAG) {
Craig Topper7a9a28b2012-08-12 02:23:29 +000011285 assert(Op.getValueType().is256BitVector() &&
Craig Topper13894fa2011-08-24 06:14:18 +000011286 Op.getValueType().isInteger() &&
11287 "Only handle AVX 256-bit vector integer operation");
11288 return Lower256IntArith(Op, DAG);
11289}
11290
Craig Topper55b24052012-09-11 06:15:32 +000011291static SDValue LowerSUB(SDValue Op, SelectionDAG &DAG) {
Craig Topper7a9a28b2012-08-12 02:23:29 +000011292 assert(Op.getValueType().is256BitVector() &&
Craig Topper13894fa2011-08-24 06:14:18 +000011293 Op.getValueType().isInteger() &&
11294 "Only handle AVX 256-bit vector integer operation");
11295 return Lower256IntArith(Op, DAG);
11296}
11297
Craig Topper55b24052012-09-11 06:15:32 +000011298static SDValue LowerMUL(SDValue Op, const X86Subtarget *Subtarget,
11299 SelectionDAG &DAG) {
Benjamin Kramer2f8a6cd2012-12-22 16:07:56 +000011300 DebugLoc dl = Op.getDebugLoc();
Craig Topper13894fa2011-08-24 06:14:18 +000011301 EVT VT = Op.getValueType();
11302
11303 // Decompose 256-bit ops into smaller 128-bit ops.
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000011304 if (VT.is256BitVector() && !Subtarget->hasInt256())
Craig Topper13894fa2011-08-24 06:14:18 +000011305 return Lower256IntArith(Op, DAG);
11306
Benjamin Kramer2f8a6cd2012-12-22 16:07:56 +000011307 SDValue A = Op.getOperand(0);
11308 SDValue B = Op.getOperand(1);
11309
11310 // Lower v4i32 mul as 2x shuffle, 2x pmuludq, 2x shuffle.
11311 if (VT == MVT::v4i32) {
11312 assert(Subtarget->hasSSE2() && !Subtarget->hasSSE41() &&
11313 "Should not custom lower when pmuldq is available!");
11314
11315 // Extract the odd parts.
11316 const int UnpackMask[] = { 1, -1, 3, -1 };
11317 SDValue Aodds = DAG.getVectorShuffle(VT, dl, A, A, UnpackMask);
11318 SDValue Bodds = DAG.getVectorShuffle(VT, dl, B, B, UnpackMask);
11319
11320 // Multiply the even parts.
11321 SDValue Evens = DAG.getNode(X86ISD::PMULUDQ, dl, MVT::v2i64, A, B);
11322 // Now multiply odd parts.
11323 SDValue Odds = DAG.getNode(X86ISD::PMULUDQ, dl, MVT::v2i64, Aodds, Bodds);
11324
11325 Evens = DAG.getNode(ISD::BITCAST, dl, VT, Evens);
11326 Odds = DAG.getNode(ISD::BITCAST, dl, VT, Odds);
11327
11328 // Merge the two vectors back together with a shuffle. This expands into 2
11329 // shuffles.
11330 const int ShufMask[] = { 0, 4, 2, 6 };
11331 return DAG.getVectorShuffle(VT, dl, Evens, Odds, ShufMask);
11332 }
11333
Craig Topper5b209e82012-02-05 03:14:49 +000011334 assert((VT == MVT::v2i64 || VT == MVT::v4i64) &&
11335 "Only know how to lower V2I64/V4I64 multiply");
11336
Craig Topper5b209e82012-02-05 03:14:49 +000011337 // Ahi = psrlqi(a, 32);
11338 // Bhi = psrlqi(b, 32);
11339 //
11340 // AloBlo = pmuludq(a, b);
11341 // AloBhi = pmuludq(a, Bhi);
11342 // AhiBlo = pmuludq(Ahi, b);
11343
11344 // AloBhi = psllqi(AloBhi, 32);
11345 // AhiBlo = psllqi(AhiBlo, 32);
11346 // return AloBlo + AloBhi + AhiBlo;
11347
Craig Topper5b209e82012-02-05 03:14:49 +000011348 SDValue ShAmt = DAG.getConstant(32, MVT::i32);
Craig Topperaaa643c2011-11-09 07:28:55 +000011349
Craig Topper5b209e82012-02-05 03:14:49 +000011350 SDValue Ahi = DAG.getNode(X86ISD::VSRLI, dl, VT, A, ShAmt);
11351 SDValue Bhi = DAG.getNode(X86ISD::VSRLI, dl, VT, B, ShAmt);
Craig Topperaaa643c2011-11-09 07:28:55 +000011352
Craig Topper5b209e82012-02-05 03:14:49 +000011353 // Bit cast to 32-bit vectors for MULUDQ
11354 EVT MulVT = (VT == MVT::v2i64) ? MVT::v4i32 : MVT::v8i32;
11355 A = DAG.getNode(ISD::BITCAST, dl, MulVT, A);
11356 B = DAG.getNode(ISD::BITCAST, dl, MulVT, B);
11357 Ahi = DAG.getNode(ISD::BITCAST, dl, MulVT, Ahi);
11358 Bhi = DAG.getNode(ISD::BITCAST, dl, MulVT, Bhi);
Craig Topperaaa643c2011-11-09 07:28:55 +000011359
Craig Topper5b209e82012-02-05 03:14:49 +000011360 SDValue AloBlo = DAG.getNode(X86ISD::PMULUDQ, dl, VT, A, B);
11361 SDValue AloBhi = DAG.getNode(X86ISD::PMULUDQ, dl, VT, A, Bhi);
11362 SDValue AhiBlo = DAG.getNode(X86ISD::PMULUDQ, dl, VT, Ahi, B);
Craig Topperaaa643c2011-11-09 07:28:55 +000011363
Craig Topper5b209e82012-02-05 03:14:49 +000011364 AloBhi = DAG.getNode(X86ISD::VSHLI, dl, VT, AloBhi, ShAmt);
11365 AhiBlo = DAG.getNode(X86ISD::VSHLI, dl, VT, AhiBlo, ShAmt);
Mon P Wangaf9b9522008-12-18 21:42:19 +000011366
Dale Johannesene4d209d2009-02-03 20:21:25 +000011367 SDValue Res = DAG.getNode(ISD::ADD, dl, VT, AloBlo, AloBhi);
Craig Topper5b209e82012-02-05 03:14:49 +000011368 return DAG.getNode(ISD::ADD, dl, VT, Res, AhiBlo);
Mon P Wangaf9b9522008-12-18 21:42:19 +000011369}
11370
Nadav Rotem43012222011-05-11 08:12:09 +000011371SDValue X86TargetLowering::LowerShift(SDValue Op, SelectionDAG &DAG) const {
11372
Nate Begemanbdcb5af2010-07-27 22:37:06 +000011373 EVT VT = Op.getValueType();
11374 DebugLoc dl = Op.getDebugLoc();
11375 SDValue R = Op.getOperand(0);
Nadav Rotem43012222011-05-11 08:12:09 +000011376 SDValue Amt = Op.getOperand(1);
Nate Begemanbdcb5af2010-07-27 22:37:06 +000011377 LLVMContext *Context = DAG.getContext();
Nate Begemanbdcb5af2010-07-27 22:37:06 +000011378
Craig Topper1accb7e2012-01-10 06:54:16 +000011379 if (!Subtarget->hasSSE2())
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +000011380 return SDValue();
11381
Nadav Rotem43012222011-05-11 08:12:09 +000011382 // Optimize shl/srl/sra with constant shift amount.
11383 if (isSplatVector(Amt.getNode())) {
11384 SDValue SclrAmt = Amt->getOperand(0);
11385 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(SclrAmt)) {
11386 uint64_t ShiftAmt = C->getZExtValue();
11387
Craig Toppered2e13d2012-01-22 19:15:14 +000011388 if (VT == MVT::v2i64 || VT == MVT::v4i32 || VT == MVT::v8i16 ||
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000011389 (Subtarget->hasInt256() &&
Craig Toppered2e13d2012-01-22 19:15:14 +000011390 (VT == MVT::v4i64 || VT == MVT::v8i32 || VT == MVT::v16i16))) {
11391 if (Op.getOpcode() == ISD::SHL)
11392 return DAG.getNode(X86ISD::VSHLI, dl, VT, R,
11393 DAG.getConstant(ShiftAmt, MVT::i32));
11394 if (Op.getOpcode() == ISD::SRL)
11395 return DAG.getNode(X86ISD::VSRLI, dl, VT, R,
11396 DAG.getConstant(ShiftAmt, MVT::i32));
11397 if (Op.getOpcode() == ISD::SRA && VT != MVT::v2i64 && VT != MVT::v4i64)
11398 return DAG.getNode(X86ISD::VSRAI, dl, VT, R,
11399 DAG.getConstant(ShiftAmt, MVT::i32));
Benjamin Kramerdade3c12011-10-30 17:31:21 +000011400 }
11401
Craig Toppered2e13d2012-01-22 19:15:14 +000011402 if (VT == MVT::v16i8) {
11403 if (Op.getOpcode() == ISD::SHL) {
11404 // Make a large shift.
11405 SDValue SHL = DAG.getNode(X86ISD::VSHLI, dl, MVT::v8i16, R,
11406 DAG.getConstant(ShiftAmt, MVT::i32));
11407 SHL = DAG.getNode(ISD::BITCAST, dl, VT, SHL);
11408 // Zero out the rightmost bits.
11409 SmallVector<SDValue, 16> V(16,
11410 DAG.getConstant(uint8_t(-1U << ShiftAmt),
11411 MVT::i8));
11412 return DAG.getNode(ISD::AND, dl, VT, SHL,
11413 DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &V[0], 16));
Eli Friedmanf6aa6b12011-11-01 21:18:39 +000011414 }
Craig Toppered2e13d2012-01-22 19:15:14 +000011415 if (Op.getOpcode() == ISD::SRL) {
11416 // Make a large shift.
11417 SDValue SRL = DAG.getNode(X86ISD::VSRLI, dl, MVT::v8i16, R,
11418 DAG.getConstant(ShiftAmt, MVT::i32));
11419 SRL = DAG.getNode(ISD::BITCAST, dl, VT, SRL);
11420 // Zero out the leftmost bits.
11421 SmallVector<SDValue, 16> V(16,
11422 DAG.getConstant(uint8_t(-1U) >> ShiftAmt,
11423 MVT::i8));
11424 return DAG.getNode(ISD::AND, dl, VT, SRL,
11425 DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &V[0], 16));
11426 }
11427 if (Op.getOpcode() == ISD::SRA) {
11428 if (ShiftAmt == 7) {
11429 // R s>> 7 === R s< 0
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +000011430 SDValue Zeros = getZeroVector(VT, Subtarget, DAG, dl);
Craig Topper67609fd2012-01-22 22:42:16 +000011431 return DAG.getNode(X86ISD::PCMPGT, dl, VT, Zeros, R);
Craig Toppered2e13d2012-01-22 19:15:14 +000011432 }
Eli Friedmanf6aa6b12011-11-01 21:18:39 +000011433
Craig Toppered2e13d2012-01-22 19:15:14 +000011434 // R s>> a === ((R u>> a) ^ m) - m
11435 SDValue Res = DAG.getNode(ISD::SRL, dl, VT, R, Amt);
11436 SmallVector<SDValue, 16> V(16, DAG.getConstant(128 >> ShiftAmt,
11437 MVT::i8));
11438 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &V[0], 16);
11439 Res = DAG.getNode(ISD::XOR, dl, VT, Res, Mask);
11440 Res = DAG.getNode(ISD::SUB, dl, VT, Res, Mask);
11441 return Res;
11442 }
Craig Topper731dfd02012-04-23 03:42:40 +000011443 llvm_unreachable("Unknown shift opcode.");
Eli Friedmanf6aa6b12011-11-01 21:18:39 +000011444 }
Craig Topper46154eb2011-11-11 07:39:23 +000011445
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000011446 if (Subtarget->hasInt256() && VT == MVT::v32i8) {
Craig Topper0d86d462011-11-20 00:12:05 +000011447 if (Op.getOpcode() == ISD::SHL) {
11448 // Make a large shift.
Craig Toppered2e13d2012-01-22 19:15:14 +000011449 SDValue SHL = DAG.getNode(X86ISD::VSHLI, dl, MVT::v16i16, R,
11450 DAG.getConstant(ShiftAmt, MVT::i32));
11451 SHL = DAG.getNode(ISD::BITCAST, dl, VT, SHL);
Craig Topper0d86d462011-11-20 00:12:05 +000011452 // Zero out the rightmost bits.
Craig Toppered2e13d2012-01-22 19:15:14 +000011453 SmallVector<SDValue, 32> V(32,
11454 DAG.getConstant(uint8_t(-1U << ShiftAmt),
11455 MVT::i8));
Craig Topper0d86d462011-11-20 00:12:05 +000011456 return DAG.getNode(ISD::AND, dl, VT, SHL,
11457 DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &V[0], 32));
Craig Topper46154eb2011-11-11 07:39:23 +000011458 }
Craig Topper0d86d462011-11-20 00:12:05 +000011459 if (Op.getOpcode() == ISD::SRL) {
11460 // Make a large shift.
Craig Toppered2e13d2012-01-22 19:15:14 +000011461 SDValue SRL = DAG.getNode(X86ISD::VSRLI, dl, MVT::v16i16, R,
11462 DAG.getConstant(ShiftAmt, MVT::i32));
11463 SRL = DAG.getNode(ISD::BITCAST, dl, VT, SRL);
Craig Topper0d86d462011-11-20 00:12:05 +000011464 // Zero out the leftmost bits.
Craig Toppered2e13d2012-01-22 19:15:14 +000011465 SmallVector<SDValue, 32> V(32,
11466 DAG.getConstant(uint8_t(-1U) >> ShiftAmt,
11467 MVT::i8));
Craig Topper0d86d462011-11-20 00:12:05 +000011468 return DAG.getNode(ISD::AND, dl, VT, SRL,
11469 DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &V[0], 32));
11470 }
11471 if (Op.getOpcode() == ISD::SRA) {
11472 if (ShiftAmt == 7) {
11473 // R s>> 7 === R s< 0
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +000011474 SDValue Zeros = getZeroVector(VT, Subtarget, DAG, dl);
Craig Topper67609fd2012-01-22 22:42:16 +000011475 return DAG.getNode(X86ISD::PCMPGT, dl, VT, Zeros, R);
Craig Topper0d86d462011-11-20 00:12:05 +000011476 }
11477
11478 // R s>> a === ((R u>> a) ^ m) - m
11479 SDValue Res = DAG.getNode(ISD::SRL, dl, VT, R, Amt);
11480 SmallVector<SDValue, 32> V(32, DAG.getConstant(128 >> ShiftAmt,
11481 MVT::i8));
11482 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &V[0], 32);
11483 Res = DAG.getNode(ISD::XOR, dl, VT, Res, Mask);
11484 Res = DAG.getNode(ISD::SUB, dl, VT, Res, Mask);
11485 return Res;
11486 }
Craig Topper731dfd02012-04-23 03:42:40 +000011487 llvm_unreachable("Unknown shift opcode.");
Craig Topper0d86d462011-11-20 00:12:05 +000011488 }
Nadav Rotem43012222011-05-11 08:12:09 +000011489 }
11490 }
11491
11492 // Lower SHL with variable shift amount.
Nadav Rotem43012222011-05-11 08:12:09 +000011493 if (VT == MVT::v4i32 && Op->getOpcode() == ISD::SHL) {
Craig Toppered2e13d2012-01-22 19:15:14 +000011494 Op = DAG.getNode(X86ISD::VSHLI, dl, VT, Op.getOperand(1),
11495 DAG.getConstant(23, MVT::i32));
Nate Begeman51409212010-07-28 00:21:48 +000011496
Chris Lattner7302d802012-02-06 21:56:39 +000011497 const uint32_t CV[] = { 0x3f800000U, 0x3f800000U, 0x3f800000U, 0x3f800000U};
11498 Constant *C = ConstantDataVector::get(*Context, CV);
Nate Begeman51409212010-07-28 00:21:48 +000011499 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
11500 SDValue Addend = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +000011501 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000011502 false, false, false, 16);
Nate Begeman51409212010-07-28 00:21:48 +000011503
11504 Op = DAG.getNode(ISD::ADD, dl, VT, Op, Addend);
Wesley Peckbf17cfa2010-11-23 03:31:01 +000011505 Op = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, Op);
Nate Begeman51409212010-07-28 00:21:48 +000011506 Op = DAG.getNode(ISD::FP_TO_SINT, dl, VT, Op);
11507 return DAG.getNode(ISD::MUL, dl, VT, Op, R);
11508 }
Nadav Rotem43012222011-05-11 08:12:09 +000011509 if (VT == MVT::v16i8 && Op->getOpcode() == ISD::SHL) {
Craig Topper8b5a6b62012-01-17 08:23:44 +000011510 assert(Subtarget->hasSSE2() && "Need SSE2 for pslli/pcmpeq.");
Lang Hames8b99c1e2011-12-17 01:08:46 +000011511
Nate Begeman51409212010-07-28 00:21:48 +000011512 // a = a << 5;
Craig Toppered2e13d2012-01-22 19:15:14 +000011513 Op = DAG.getNode(X86ISD::VSHLI, dl, MVT::v8i16, Op.getOperand(1),
11514 DAG.getConstant(5, MVT::i32));
11515 Op = DAG.getNode(ISD::BITCAST, dl, VT, Op);
Nate Begeman51409212010-07-28 00:21:48 +000011516
Lang Hames8b99c1e2011-12-17 01:08:46 +000011517 // Turn 'a' into a mask suitable for VSELECT
11518 SDValue VSelM = DAG.getConstant(0x80, VT);
11519 SDValue OpVSel = DAG.getNode(ISD::AND, dl, VT, VSelM, Op);
Craig Topper67609fd2012-01-22 22:42:16 +000011520 OpVSel = DAG.getNode(X86ISD::PCMPEQ, dl, VT, OpVSel, VSelM);
Nate Begeman51409212010-07-28 00:21:48 +000011521
Lang Hames8b99c1e2011-12-17 01:08:46 +000011522 SDValue CM1 = DAG.getConstant(0x0f, VT);
11523 SDValue CM2 = DAG.getConstant(0x3f, VT);
Nate Begeman51409212010-07-28 00:21:48 +000011524
Lang Hames8b99c1e2011-12-17 01:08:46 +000011525 // r = VSELECT(r, psllw(r & (char16)15, 4), a);
11526 SDValue M = DAG.getNode(ISD::AND, dl, VT, R, CM1);
Craig Toppered2e13d2012-01-22 19:15:14 +000011527 M = getTargetVShiftNode(X86ISD::VSHLI, dl, MVT::v8i16, M,
11528 DAG.getConstant(4, MVT::i32), DAG);
11529 M = DAG.getNode(ISD::BITCAST, dl, VT, M);
Lang Hames8b99c1e2011-12-17 01:08:46 +000011530 R = DAG.getNode(ISD::VSELECT, dl, VT, OpVSel, M, R);
11531
Nate Begeman51409212010-07-28 00:21:48 +000011532 // a += a
11533 Op = DAG.getNode(ISD::ADD, dl, VT, Op, Op);
Lang Hames8b99c1e2011-12-17 01:08:46 +000011534 OpVSel = DAG.getNode(ISD::AND, dl, VT, VSelM, Op);
Craig Topper67609fd2012-01-22 22:42:16 +000011535 OpVSel = DAG.getNode(X86ISD::PCMPEQ, dl, VT, OpVSel, VSelM);
Michael J. Spencerec38de22010-10-10 22:04:20 +000011536
Lang Hames8b99c1e2011-12-17 01:08:46 +000011537 // r = VSELECT(r, psllw(r & (char16)63, 2), a);
11538 M = DAG.getNode(ISD::AND, dl, VT, R, CM2);
Craig Toppered2e13d2012-01-22 19:15:14 +000011539 M = getTargetVShiftNode(X86ISD::VSHLI, dl, MVT::v8i16, M,
11540 DAG.getConstant(2, MVT::i32), DAG);
11541 M = DAG.getNode(ISD::BITCAST, dl, VT, M);
Lang Hames8b99c1e2011-12-17 01:08:46 +000011542 R = DAG.getNode(ISD::VSELECT, dl, VT, OpVSel, M, R);
11543
Nate Begeman51409212010-07-28 00:21:48 +000011544 // a += a
11545 Op = DAG.getNode(ISD::ADD, dl, VT, Op, Op);
Lang Hames8b99c1e2011-12-17 01:08:46 +000011546 OpVSel = DAG.getNode(ISD::AND, dl, VT, VSelM, Op);
Craig Topper67609fd2012-01-22 22:42:16 +000011547 OpVSel = DAG.getNode(X86ISD::PCMPEQ, dl, VT, OpVSel, VSelM);
Michael J. Spencerec38de22010-10-10 22:04:20 +000011548
Lang Hames8b99c1e2011-12-17 01:08:46 +000011549 // return VSELECT(r, r+r, a);
11550 R = DAG.getNode(ISD::VSELECT, dl, VT, OpVSel,
Lang Hamesa0a25132011-12-15 18:57:27 +000011551 DAG.getNode(ISD::ADD, dl, VT, R, R), R);
Nate Begeman51409212010-07-28 00:21:48 +000011552 return R;
11553 }
Craig Topper46154eb2011-11-11 07:39:23 +000011554
11555 // Decompose 256-bit shifts into smaller 128-bit shifts.
Craig Topper7a9a28b2012-08-12 02:23:29 +000011556 if (VT.is256BitVector()) {
Craig Toppered2e13d2012-01-22 19:15:14 +000011557 unsigned NumElems = VT.getVectorNumElements();
Craig Topper46154eb2011-11-11 07:39:23 +000011558 MVT EltVT = VT.getVectorElementType().getSimpleVT();
11559 EVT NewVT = MVT::getVectorVT(EltVT, NumElems/2);
11560
11561 // Extract the two vectors
Craig Topperb14940a2012-04-22 20:55:18 +000011562 SDValue V1 = Extract128BitVector(R, 0, DAG, dl);
11563 SDValue V2 = Extract128BitVector(R, NumElems/2, DAG, dl);
Craig Topper46154eb2011-11-11 07:39:23 +000011564
11565 // Recreate the shift amount vectors
11566 SDValue Amt1, Amt2;
11567 if (Amt.getOpcode() == ISD::BUILD_VECTOR) {
11568 // Constant shift amount
11569 SmallVector<SDValue, 4> Amt1Csts;
11570 SmallVector<SDValue, 4> Amt2Csts;
Craig Toppered2e13d2012-01-22 19:15:14 +000011571 for (unsigned i = 0; i != NumElems/2; ++i)
Craig Topper46154eb2011-11-11 07:39:23 +000011572 Amt1Csts.push_back(Amt->getOperand(i));
Craig Toppered2e13d2012-01-22 19:15:14 +000011573 for (unsigned i = NumElems/2; i != NumElems; ++i)
Craig Topper46154eb2011-11-11 07:39:23 +000011574 Amt2Csts.push_back(Amt->getOperand(i));
11575
11576 Amt1 = DAG.getNode(ISD::BUILD_VECTOR, dl, NewVT,
11577 &Amt1Csts[0], NumElems/2);
11578 Amt2 = DAG.getNode(ISD::BUILD_VECTOR, dl, NewVT,
11579 &Amt2Csts[0], NumElems/2);
11580 } else {
11581 // Variable shift amount
Craig Topperb14940a2012-04-22 20:55:18 +000011582 Amt1 = Extract128BitVector(Amt, 0, DAG, dl);
11583 Amt2 = Extract128BitVector(Amt, NumElems/2, DAG, dl);
Craig Topper46154eb2011-11-11 07:39:23 +000011584 }
11585
11586 // Issue new vector shifts for the smaller types
11587 V1 = DAG.getNode(Op.getOpcode(), dl, NewVT, V1, Amt1);
11588 V2 = DAG.getNode(Op.getOpcode(), dl, NewVT, V2, Amt2);
11589
11590 // Concatenate the result back
11591 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT, V1, V2);
11592 }
11593
Nate Begeman51409212010-07-28 00:21:48 +000011594 return SDValue();
Nate Begemanbdcb5af2010-07-27 22:37:06 +000011595}
Mon P Wangaf9b9522008-12-18 21:42:19 +000011596
Craig Topper55b24052012-09-11 06:15:32 +000011597static SDValue LowerXALUO(SDValue Op, SelectionDAG &DAG) {
Bill Wendling74c37652008-12-09 22:08:41 +000011598 // Lower the "add/sub/mul with overflow" instruction into a regular ins plus
11599 // a "setcc" instruction that checks the overflow flag. The "brcond" lowering
Bill Wendling61edeb52008-12-02 01:06:39 +000011600 // looks for this combo and may remove the "setcc" instruction if the "setcc"
11601 // has only one use.
Bill Wendling3fafd932008-11-26 22:37:40 +000011602 SDNode *N = Op.getNode();
Bill Wendling61edeb52008-12-02 01:06:39 +000011603 SDValue LHS = N->getOperand(0);
11604 SDValue RHS = N->getOperand(1);
Bill Wendling74c37652008-12-09 22:08:41 +000011605 unsigned BaseOp = 0;
11606 unsigned Cond = 0;
Chris Lattnerb20e0b12010-12-05 07:30:36 +000011607 DebugLoc DL = Op.getDebugLoc();
Bill Wendling74c37652008-12-09 22:08:41 +000011608 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +000011609 default: llvm_unreachable("Unknown ovf instruction!");
Bill Wendling74c37652008-12-09 22:08:41 +000011610 case ISD::SADDO:
Dan Gohman076aee32009-03-04 19:44:21 +000011611 // A subtract of one will be selected as a INC. Note that INC doesn't
11612 // set CF, so we can't do this for UADDO.
Benjamin Kramerc175a4b2011-03-08 15:20:20 +000011613 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(RHS))
11614 if (C->isOne()) {
Dan Gohman076aee32009-03-04 19:44:21 +000011615 BaseOp = X86ISD::INC;
11616 Cond = X86::COND_O;
11617 break;
11618 }
Bill Wendlingab55ebd2008-12-12 00:56:36 +000011619 BaseOp = X86ISD::ADD;
Bill Wendling74c37652008-12-09 22:08:41 +000011620 Cond = X86::COND_O;
11621 break;
11622 case ISD::UADDO:
Bill Wendlingab55ebd2008-12-12 00:56:36 +000011623 BaseOp = X86ISD::ADD;
Dan Gohman653456c2009-01-07 00:15:08 +000011624 Cond = X86::COND_B;
Bill Wendling74c37652008-12-09 22:08:41 +000011625 break;
11626 case ISD::SSUBO:
Dan Gohman076aee32009-03-04 19:44:21 +000011627 // A subtract of one will be selected as a DEC. Note that DEC doesn't
11628 // set CF, so we can't do this for USUBO.
Benjamin Kramerc175a4b2011-03-08 15:20:20 +000011629 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(RHS))
11630 if (C->isOne()) {
Dan Gohman076aee32009-03-04 19:44:21 +000011631 BaseOp = X86ISD::DEC;
11632 Cond = X86::COND_O;
11633 break;
11634 }
Bill Wendlingab55ebd2008-12-12 00:56:36 +000011635 BaseOp = X86ISD::SUB;
Bill Wendling74c37652008-12-09 22:08:41 +000011636 Cond = X86::COND_O;
11637 break;
11638 case ISD::USUBO:
Bill Wendlingab55ebd2008-12-12 00:56:36 +000011639 BaseOp = X86ISD::SUB;
Dan Gohman653456c2009-01-07 00:15:08 +000011640 Cond = X86::COND_B;
Bill Wendling74c37652008-12-09 22:08:41 +000011641 break;
11642 case ISD::SMULO:
Bill Wendlingd350e022008-12-12 21:15:41 +000011643 BaseOp = X86ISD::SMUL;
Bill Wendling74c37652008-12-09 22:08:41 +000011644 Cond = X86::COND_O;
11645 break;
Chris Lattnerb20e0b12010-12-05 07:30:36 +000011646 case ISD::UMULO: { // i64, i8 = umulo lhs, rhs --> i64, i64, i32 umul lhs,rhs
11647 SDVTList VTs = DAG.getVTList(N->getValueType(0), N->getValueType(0),
11648 MVT::i32);
11649 SDValue Sum = DAG.getNode(X86ISD::UMUL, DL, VTs, LHS, RHS);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011650
Chris Lattnerb20e0b12010-12-05 07:30:36 +000011651 SDValue SetCC =
11652 DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
11653 DAG.getConstant(X86::COND_O, MVT::i32),
11654 SDValue(Sum.getNode(), 2));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011655
Dan Gohman6e5fda22011-07-22 18:45:15 +000011656 return DAG.getNode(ISD::MERGE_VALUES, DL, N->getVTList(), Sum, SetCC);
Chris Lattnerb20e0b12010-12-05 07:30:36 +000011657 }
Bill Wendling74c37652008-12-09 22:08:41 +000011658 }
Bill Wendling3fafd932008-11-26 22:37:40 +000011659
Bill Wendling61edeb52008-12-02 01:06:39 +000011660 // Also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +000011661 SDVTList VTs = DAG.getVTList(N->getValueType(0), MVT::i32);
Chris Lattnerb20e0b12010-12-05 07:30:36 +000011662 SDValue Sum = DAG.getNode(BaseOp, DL, VTs, LHS, RHS);
Bill Wendling3fafd932008-11-26 22:37:40 +000011663
Bill Wendling61edeb52008-12-02 01:06:39 +000011664 SDValue SetCC =
Chris Lattnerb20e0b12010-12-05 07:30:36 +000011665 DAG.getNode(X86ISD::SETCC, DL, N->getValueType(1),
11666 DAG.getConstant(Cond, MVT::i32),
11667 SDValue(Sum.getNode(), 1));
Bill Wendling3fafd932008-11-26 22:37:40 +000011668
Dan Gohman6e5fda22011-07-22 18:45:15 +000011669 return DAG.getNode(ISD::MERGE_VALUES, DL, N->getVTList(), Sum, SetCC);
Bill Wendling41ea7e72008-11-24 19:21:46 +000011670}
11671
Chad Rosier30450e82011-12-22 22:35:21 +000011672SDValue X86TargetLowering::LowerSIGN_EXTEND_INREG(SDValue Op,
11673 SelectionDAG &DAG) const {
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000011674 DebugLoc dl = Op.getDebugLoc();
Craig Toppera124f942011-11-21 01:12:36 +000011675 EVT ExtraVT = cast<VTSDNode>(Op.getOperand(1))->getVT();
11676 EVT VT = Op.getValueType();
11677
Craig Toppered2e13d2012-01-22 19:15:14 +000011678 if (!Subtarget->hasSSE2() || !VT.isVector())
11679 return SDValue();
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000011680
Craig Toppered2e13d2012-01-22 19:15:14 +000011681 unsigned BitsDiff = VT.getScalarType().getSizeInBits() -
11682 ExtraVT.getScalarType().getSizeInBits();
11683 SDValue ShAmt = DAG.getConstant(BitsDiff, MVT::i32);
11684
11685 switch (VT.getSimpleVT().SimpleTy) {
11686 default: return SDValue();
11687 case MVT::v8i32:
11688 case MVT::v16i16:
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000011689 if (!Subtarget->hasFp256())
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000011690 return SDValue();
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000011691 if (!Subtarget->hasInt256()) {
Craig Toppered2e13d2012-01-22 19:15:14 +000011692 // needs to be split
Craig Topper66ddd152012-04-27 22:54:43 +000011693 unsigned NumElems = VT.getVectorNumElements();
Craig Toppera124f942011-11-21 01:12:36 +000011694
Craig Toppered2e13d2012-01-22 19:15:14 +000011695 // Extract the LHS vectors
11696 SDValue LHS = Op.getOperand(0);
Craig Topperb14940a2012-04-22 20:55:18 +000011697 SDValue LHS1 = Extract128BitVector(LHS, 0, DAG, dl);
11698 SDValue LHS2 = Extract128BitVector(LHS, NumElems/2, DAG, dl);
Craig Toppera124f942011-11-21 01:12:36 +000011699
Craig Toppered2e13d2012-01-22 19:15:14 +000011700 MVT EltVT = VT.getVectorElementType().getSimpleVT();
11701 EVT NewVT = MVT::getVectorVT(EltVT, NumElems/2);
Craig Toppera124f942011-11-21 01:12:36 +000011702
Craig Toppered2e13d2012-01-22 19:15:14 +000011703 EVT ExtraEltVT = ExtraVT.getVectorElementType();
Craig Topperb6072642012-05-03 07:26:59 +000011704 unsigned ExtraNumElems = ExtraVT.getVectorNumElements();
Craig Toppered2e13d2012-01-22 19:15:14 +000011705 ExtraVT = EVT::getVectorVT(*DAG.getContext(), ExtraEltVT,
11706 ExtraNumElems/2);
11707 SDValue Extra = DAG.getValueType(ExtraVT);
Craig Toppera124f942011-11-21 01:12:36 +000011708
Craig Toppered2e13d2012-01-22 19:15:14 +000011709 LHS1 = DAG.getNode(Op.getOpcode(), dl, NewVT, LHS1, Extra);
11710 LHS2 = DAG.getNode(Op.getOpcode(), dl, NewVT, LHS2, Extra);
Craig Toppera124f942011-11-21 01:12:36 +000011711
Dmitri Gribenko2de05722012-09-10 21:26:47 +000011712 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT, LHS1, LHS2);
Craig Toppered2e13d2012-01-22 19:15:14 +000011713 }
11714 // fall through
11715 case MVT::v4i32:
11716 case MVT::v8i16: {
11717 SDValue Tmp1 = getTargetVShiftNode(X86ISD::VSHLI, dl, VT,
11718 Op.getOperand(0), ShAmt, DAG);
11719 return getTargetVShiftNode(X86ISD::VSRAI, dl, VT, Tmp1, ShAmt, DAG);
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000011720 }
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000011721 }
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000011722}
11723
Craig Topper55b24052012-09-11 06:15:32 +000011724static SDValue LowerMEMBARRIER(SDValue Op, const X86Subtarget *Subtarget,
11725 SelectionDAG &DAG) {
Eric Christopher9a9d2752010-07-22 02:48:34 +000011726 DebugLoc dl = Op.getDebugLoc();
Michael J. Spencerec38de22010-10-10 22:04:20 +000011727
Eric Christopher77ed1352011-07-08 00:04:56 +000011728 // Go ahead and emit the fence on x86-64 even if we asked for no-sse2.
11729 // There isn't any reason to disable it if the target processor supports it.
Craig Topper1accb7e2012-01-10 06:54:16 +000011730 if (!Subtarget->hasSSE2() && !Subtarget->is64Bit()) {
Eric Christopherc0b2a202010-08-14 21:51:50 +000011731 SDValue Chain = Op.getOperand(0);
Eric Christopher77ed1352011-07-08 00:04:56 +000011732 SDValue Zero = DAG.getConstant(0, MVT::i32);
Eric Christopherc0b2a202010-08-14 21:51:50 +000011733 SDValue Ops[] = {
11734 DAG.getRegister(X86::ESP, MVT::i32), // Base
11735 DAG.getTargetConstant(1, MVT::i8), // Scale
11736 DAG.getRegister(0, MVT::i32), // Index
11737 DAG.getTargetConstant(0, MVT::i32), // Disp
11738 DAG.getRegister(0, MVT::i32), // Segment.
11739 Zero,
11740 Chain
11741 };
Michael J. Spencerec38de22010-10-10 22:04:20 +000011742 SDNode *Res =
Eric Christopherc0b2a202010-08-14 21:51:50 +000011743 DAG.getMachineNode(X86::OR32mrLocked, dl, MVT::Other, Ops,
11744 array_lengthof(Ops));
11745 return SDValue(Res, 0);
Eric Christopherb6729dc2010-08-04 23:03:04 +000011746 }
Michael J. Spencerec38de22010-10-10 22:04:20 +000011747
Eric Christopher9a9d2752010-07-22 02:48:34 +000011748 unsigned isDev = cast<ConstantSDNode>(Op.getOperand(5))->getZExtValue();
Chris Lattner132929a2010-08-14 17:26:09 +000011749 if (!isDev)
Eric Christopher9a9d2752010-07-22 02:48:34 +000011750 return DAG.getNode(X86ISD::MEMBARRIER, dl, MVT::Other, Op.getOperand(0));
Michael J. Spencerec38de22010-10-10 22:04:20 +000011751
Chris Lattner132929a2010-08-14 17:26:09 +000011752 unsigned Op1 = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
11753 unsigned Op2 = cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue();
11754 unsigned Op3 = cast<ConstantSDNode>(Op.getOperand(3))->getZExtValue();
11755 unsigned Op4 = cast<ConstantSDNode>(Op.getOperand(4))->getZExtValue();
Michael J. Spencerec38de22010-10-10 22:04:20 +000011756
Chris Lattner132929a2010-08-14 17:26:09 +000011757 // def : Pat<(membarrier (i8 0), (i8 0), (i8 0), (i8 1), (i8 1)), (SFENCE)>;
11758 if (!Op1 && !Op2 && !Op3 && Op4)
11759 return DAG.getNode(X86ISD::SFENCE, dl, MVT::Other, Op.getOperand(0));
Michael J. Spencerec38de22010-10-10 22:04:20 +000011760
Chris Lattner132929a2010-08-14 17:26:09 +000011761 // def : Pat<(membarrier (i8 1), (i8 0), (i8 0), (i8 0), (i8 1)), (LFENCE)>;
11762 if (Op1 && !Op2 && !Op3 && !Op4)
11763 return DAG.getNode(X86ISD::LFENCE, dl, MVT::Other, Op.getOperand(0));
Michael J. Spencerec38de22010-10-10 22:04:20 +000011764
11765 // def : Pat<(membarrier (i8 imm), (i8 imm), (i8 imm), (i8 imm), (i8 1)),
Chris Lattner132929a2010-08-14 17:26:09 +000011766 // (MFENCE)>;
11767 return DAG.getNode(X86ISD::MFENCE, dl, MVT::Other, Op.getOperand(0));
Eric Christopher9a9d2752010-07-22 02:48:34 +000011768}
11769
Craig Topper55b24052012-09-11 06:15:32 +000011770static SDValue LowerATOMIC_FENCE(SDValue Op, const X86Subtarget *Subtarget,
11771 SelectionDAG &DAG) {
Eli Friedman14648462011-07-27 22:21:52 +000011772 DebugLoc dl = Op.getDebugLoc();
11773 AtomicOrdering FenceOrdering = static_cast<AtomicOrdering>(
11774 cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue());
11775 SynchronizationScope FenceScope = static_cast<SynchronizationScope>(
11776 cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue());
11777
11778 // The only fence that needs an instruction is a sequentially-consistent
11779 // cross-thread fence.
11780 if (FenceOrdering == SequentiallyConsistent && FenceScope == CrossThread) {
11781 // Use mfence if we have SSE2 or we're on x86-64 (even if we asked for
11782 // no-sse2). There isn't any reason to disable it if the target processor
11783 // supports it.
Craig Topper1accb7e2012-01-10 06:54:16 +000011784 if (Subtarget->hasSSE2() || Subtarget->is64Bit())
Eli Friedman14648462011-07-27 22:21:52 +000011785 return DAG.getNode(X86ISD::MFENCE, dl, MVT::Other, Op.getOperand(0));
11786
11787 SDValue Chain = Op.getOperand(0);
11788 SDValue Zero = DAG.getConstant(0, MVT::i32);
11789 SDValue Ops[] = {
11790 DAG.getRegister(X86::ESP, MVT::i32), // Base
11791 DAG.getTargetConstant(1, MVT::i8), // Scale
11792 DAG.getRegister(0, MVT::i32), // Index
11793 DAG.getTargetConstant(0, MVT::i32), // Disp
11794 DAG.getRegister(0, MVT::i32), // Segment.
11795 Zero,
11796 Chain
11797 };
11798 SDNode *Res =
11799 DAG.getMachineNode(X86::OR32mrLocked, dl, MVT::Other, Ops,
11800 array_lengthof(Ops));
11801 return SDValue(Res, 0);
11802 }
11803
11804 // MEMBARRIER is a compiler barrier; it codegens to a no-op.
11805 return DAG.getNode(X86ISD::MEMBARRIER, dl, MVT::Other, Op.getOperand(0));
11806}
11807
Craig Topper55b24052012-09-11 06:15:32 +000011808static SDValue LowerCMP_SWAP(SDValue Op, const X86Subtarget *Subtarget,
11809 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +000011810 EVT T = Op.getValueType();
Chris Lattner93c4a5b2010-09-21 23:59:42 +000011811 DebugLoc DL = Op.getDebugLoc();
Andrew Lenhartha76e2f02008-03-04 21:13:33 +000011812 unsigned Reg = 0;
11813 unsigned size = 0;
Owen Anderson825b72b2009-08-11 20:47:22 +000011814 switch(T.getSimpleVT().SimpleTy) {
Craig Topperabb94d02012-02-05 03:43:23 +000011815 default: llvm_unreachable("Invalid value type!");
Owen Anderson825b72b2009-08-11 20:47:22 +000011816 case MVT::i8: Reg = X86::AL; size = 1; break;
11817 case MVT::i16: Reg = X86::AX; size = 2; break;
11818 case MVT::i32: Reg = X86::EAX; size = 4; break;
11819 case MVT::i64:
Duncan Sands1607f052008-12-01 11:39:25 +000011820 assert(Subtarget->is64Bit() && "Node not type legal!");
11821 Reg = X86::RAX; size = 8;
Andrew Lenharthd19189e2008-03-05 01:15:49 +000011822 break;
Bill Wendling61edeb52008-12-02 01:06:39 +000011823 }
Chris Lattner93c4a5b2010-09-21 23:59:42 +000011824 SDValue cpIn = DAG.getCopyToReg(Op.getOperand(0), DL, Reg,
Dale Johannesend18a4622008-09-11 03:12:59 +000011825 Op.getOperand(2), SDValue());
Dan Gohman475871a2008-07-27 21:46:04 +000011826 SDValue Ops[] = { cpIn.getValue(0),
Evan Cheng8a186ae2008-09-24 23:26:36 +000011827 Op.getOperand(1),
11828 Op.getOperand(3),
Owen Anderson825b72b2009-08-11 20:47:22 +000011829 DAG.getTargetConstant(size, MVT::i8),
Evan Cheng8a186ae2008-09-24 23:26:36 +000011830 cpIn.getValue(1) };
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +000011831 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Chris Lattner93c4a5b2010-09-21 23:59:42 +000011832 MachineMemOperand *MMO = cast<AtomicSDNode>(Op)->getMemOperand();
11833 SDValue Result = DAG.getMemIntrinsicNode(X86ISD::LCMPXCHG_DAG, DL, Tys,
11834 Ops, 5, T, MMO);
Scott Michelfdc40a02009-02-17 22:15:04 +000011835 SDValue cpOut =
Chris Lattner93c4a5b2010-09-21 23:59:42 +000011836 DAG.getCopyFromReg(Result.getValue(0), DL, Reg, T, Result.getValue(1));
Andrew Lenharth26ed8692008-03-01 21:52:34 +000011837 return cpOut;
11838}
11839
Craig Topper55b24052012-09-11 06:15:32 +000011840static SDValue LowerREADCYCLECOUNTER(SDValue Op, const X86Subtarget *Subtarget,
11841 SelectionDAG &DAG) {
Duncan Sands1607f052008-12-01 11:39:25 +000011842 assert(Subtarget->is64Bit() && "Result not type legalized?");
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +000011843 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Duncan Sands1607f052008-12-01 11:39:25 +000011844 SDValue TheChain = Op.getOperand(0);
Dale Johannesen6f38cb62009-02-07 19:59:05 +000011845 DebugLoc dl = Op.getDebugLoc();
Dale Johannesene4d209d2009-02-03 20:21:25 +000011846 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, dl, Tys, &TheChain, 1);
Owen Anderson825b72b2009-08-11 20:47:22 +000011847 SDValue rax = DAG.getCopyFromReg(rd, dl, X86::RAX, MVT::i64, rd.getValue(1));
11848 SDValue rdx = DAG.getCopyFromReg(rax.getValue(1), dl, X86::RDX, MVT::i64,
Duncan Sands1607f052008-12-01 11:39:25 +000011849 rax.getValue(2));
Owen Anderson825b72b2009-08-11 20:47:22 +000011850 SDValue Tmp = DAG.getNode(ISD::SHL, dl, MVT::i64, rdx,
11851 DAG.getConstant(32, MVT::i8));
Duncan Sands1607f052008-12-01 11:39:25 +000011852 SDValue Ops[] = {
Owen Anderson825b72b2009-08-11 20:47:22 +000011853 DAG.getNode(ISD::OR, dl, MVT::i64, rax, Tmp),
Duncan Sands1607f052008-12-01 11:39:25 +000011854 rdx.getValue(1)
11855 };
Dale Johannesene4d209d2009-02-03 20:21:25 +000011856 return DAG.getMergeValues(Ops, 2, dl);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011857}
11858
Craig Topper55b24052012-09-11 06:15:32 +000011859SDValue X86TargetLowering::LowerBITCAST(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen7d07b482010-05-21 00:52:33 +000011860 EVT SrcVT = Op.getOperand(0).getValueType();
11861 EVT DstVT = Op.getValueType();
Craig Topper1accb7e2012-01-10 06:54:16 +000011862 assert(Subtarget->is64Bit() && !Subtarget->hasSSE2() &&
Chris Lattner2a786eb2010-12-19 20:19:20 +000011863 Subtarget->hasMMX() && "Unexpected custom BITCAST");
Michael J. Spencerec38de22010-10-10 22:04:20 +000011864 assert((DstVT == MVT::i64 ||
Dale Johannesen7d07b482010-05-21 00:52:33 +000011865 (DstVT.isVector() && DstVT.getSizeInBits()==64)) &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +000011866 "Unexpected custom BITCAST");
Dale Johannesen7d07b482010-05-21 00:52:33 +000011867 // i64 <=> MMX conversions are Legal.
11868 if (SrcVT==MVT::i64 && DstVT.isVector())
11869 return Op;
11870 if (DstVT==MVT::i64 && SrcVT.isVector())
11871 return Op;
Dale Johannesene39859a2010-05-21 18:40:15 +000011872 // MMX <=> MMX conversions are Legal.
11873 if (SrcVT.isVector() && DstVT.isVector())
11874 return Op;
Dale Johannesen7d07b482010-05-21 00:52:33 +000011875 // All other conversions need to be expanded.
11876 return SDValue();
11877}
Chris Lattner5b856542010-12-20 00:59:46 +000011878
Craig Topper55b24052012-09-11 06:15:32 +000011879static SDValue LowerLOAD_SUB(SDValue Op, SelectionDAG &DAG) {
Dale Johannesen71d1bf52008-09-29 22:25:26 +000011880 SDNode *Node = Op.getNode();
Dale Johannesene4d209d2009-02-03 20:21:25 +000011881 DebugLoc dl = Node->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +000011882 EVT T = Node->getValueType(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +000011883 SDValue negOp = DAG.getNode(ISD::SUB, dl, T,
Evan Cheng242b38b2009-02-23 09:03:22 +000011884 DAG.getConstant(0, T), Node->getOperand(2));
Dale Johannesene4d209d2009-02-03 20:21:25 +000011885 return DAG.getAtomic(ISD::ATOMIC_LOAD_ADD, dl,
Dan Gohman0b1d4a72008-12-23 21:37:04 +000011886 cast<AtomicSDNode>(Node)->getMemoryVT(),
Dale Johannesen71d1bf52008-09-29 22:25:26 +000011887 Node->getOperand(0),
11888 Node->getOperand(1), negOp,
11889 cast<AtomicSDNode>(Node)->getSrcValue(),
Eli Friedman55ba8162011-07-29 03:05:32 +000011890 cast<AtomicSDNode>(Node)->getAlignment(),
11891 cast<AtomicSDNode>(Node)->getOrdering(),
11892 cast<AtomicSDNode>(Node)->getSynchScope());
Mon P Wang63307c32008-05-05 19:05:59 +000011893}
11894
Eli Friedman327236c2011-08-24 20:50:09 +000011895static SDValue LowerATOMIC_STORE(SDValue Op, SelectionDAG &DAG) {
11896 SDNode *Node = Op.getNode();
11897 DebugLoc dl = Node->getDebugLoc();
Eli Friedmanf8f90f02011-08-24 22:33:28 +000011898 EVT VT = cast<AtomicSDNode>(Node)->getMemoryVT();
Eli Friedman327236c2011-08-24 20:50:09 +000011899
11900 // Convert seq_cst store -> xchg
Eli Friedmanf8f90f02011-08-24 22:33:28 +000011901 // Convert wide store -> swap (-> cmpxchg8b/cmpxchg16b)
11902 // FIXME: On 32-bit, store -> fist or movq would be more efficient
11903 // (The only way to get a 16-byte store is cmpxchg16b)
11904 // FIXME: 16-byte ATOMIC_SWAP isn't actually hooked up at the moment.
11905 if (cast<AtomicSDNode>(Node)->getOrdering() == SequentiallyConsistent ||
11906 !DAG.getTargetLoweringInfo().isTypeLegal(VT)) {
Eli Friedman4317fe12011-08-24 21:17:30 +000011907 SDValue Swap = DAG.getAtomic(ISD::ATOMIC_SWAP, dl,
11908 cast<AtomicSDNode>(Node)->getMemoryVT(),
11909 Node->getOperand(0),
11910 Node->getOperand(1), Node->getOperand(2),
Eli Friedmanf8f90f02011-08-24 22:33:28 +000011911 cast<AtomicSDNode>(Node)->getMemOperand(),
Eli Friedman4317fe12011-08-24 21:17:30 +000011912 cast<AtomicSDNode>(Node)->getOrdering(),
11913 cast<AtomicSDNode>(Node)->getSynchScope());
Eli Friedman327236c2011-08-24 20:50:09 +000011914 return Swap.getValue(1);
11915 }
11916 // Other atomic stores have a simple pattern.
11917 return Op;
11918}
11919
Chris Lattner5b856542010-12-20 00:59:46 +000011920static SDValue LowerADDC_ADDE_SUBC_SUBE(SDValue Op, SelectionDAG &DAG) {
11921 EVT VT = Op.getNode()->getValueType(0);
11922
11923 // Let legalize expand this if it isn't a legal type yet.
11924 if (!DAG.getTargetLoweringInfo().isTypeLegal(VT))
11925 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011926
Chris Lattner5b856542010-12-20 00:59:46 +000011927 SDVTList VTs = DAG.getVTList(VT, MVT::i32);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011928
Chris Lattner5b856542010-12-20 00:59:46 +000011929 unsigned Opc;
11930 bool ExtraOp = false;
11931 switch (Op.getOpcode()) {
Craig Topperabb94d02012-02-05 03:43:23 +000011932 default: llvm_unreachable("Invalid code");
Chris Lattner5b856542010-12-20 00:59:46 +000011933 case ISD::ADDC: Opc = X86ISD::ADD; break;
11934 case ISD::ADDE: Opc = X86ISD::ADC; ExtraOp = true; break;
11935 case ISD::SUBC: Opc = X86ISD::SUB; break;
11936 case ISD::SUBE: Opc = X86ISD::SBB; ExtraOp = true; break;
11937 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011938
Chris Lattner5b856542010-12-20 00:59:46 +000011939 if (!ExtraOp)
11940 return DAG.getNode(Opc, Op->getDebugLoc(), VTs, Op.getOperand(0),
11941 Op.getOperand(1));
11942 return DAG.getNode(Opc, Op->getDebugLoc(), VTs, Op.getOperand(0),
11943 Op.getOperand(1), Op.getOperand(2));
11944}
11945
Evan Cheng0db9fe62006-04-25 20:13:52 +000011946/// LowerOperation - Provide custom lowering hooks for some operations.
11947///
Dan Gohmand858e902010-04-17 15:26:15 +000011948SDValue X86TargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +000011949 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +000011950 default: llvm_unreachable("Should not custom lower this!");
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000011951 case ISD::SIGN_EXTEND_INREG: return LowerSIGN_EXTEND_INREG(Op,DAG);
Craig Topper55b24052012-09-11 06:15:32 +000011952 case ISD::MEMBARRIER: return LowerMEMBARRIER(Op, Subtarget, DAG);
11953 case ISD::ATOMIC_FENCE: return LowerATOMIC_FENCE(Op, Subtarget, DAG);
11954 case ISD::ATOMIC_CMP_SWAP: return LowerCMP_SWAP(Op, Subtarget, DAG);
Dan Gohman0b1d4a72008-12-23 21:37:04 +000011955 case ISD::ATOMIC_LOAD_SUB: return LowerLOAD_SUB(Op,DAG);
Eli Friedman327236c2011-08-24 20:50:09 +000011956 case ISD::ATOMIC_STORE: return LowerATOMIC_STORE(Op,DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000011957 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
Mon P Wangeb38ebf2010-01-24 00:05:03 +000011958 case ISD::CONCAT_VECTORS: return LowerCONCAT_VECTORS(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000011959 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
11960 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
11961 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
Craig Topper55b24052012-09-11 06:15:32 +000011962 case ISD::EXTRACT_SUBVECTOR: return LowerEXTRACT_SUBVECTOR(Op,Subtarget,DAG);
11963 case ISD::INSERT_SUBVECTOR: return LowerINSERT_SUBVECTOR(Op, Subtarget,DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000011964 case ISD::SCALAR_TO_VECTOR: return LowerSCALAR_TO_VECTOR(Op, DAG);
11965 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
11966 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +000011967 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Bill Wendling056292f2008-09-16 21:48:12 +000011968 case ISD::ExternalSymbol: return LowerExternalSymbol(Op, DAG);
Dan Gohmanf705adb2009-10-30 01:28:02 +000011969 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000011970 case ISD::SHL_PARTS:
11971 case ISD::SRA_PARTS:
Nadav Rotem43012222011-05-11 08:12:09 +000011972 case ISD::SRL_PARTS: return LowerShiftParts(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000011973 case ISD::SINT_TO_FP: return LowerSINT_TO_FP(Op, DAG);
Dale Johannesen1c15bf52008-10-21 20:50:01 +000011974 case ISD::UINT_TO_FP: return LowerUINT_TO_FP(Op, DAG);
Michael Liaobedcbd42012-10-16 18:14:11 +000011975 case ISD::TRUNCATE: return lowerTRUNCATE(Op, DAG);
Nadav Rotem0509db22012-12-28 05:45:24 +000011976 case ISD::ZERO_EXTEND: return LowerZERO_EXTEND(Op, DAG);
11977 case ISD::SIGN_EXTEND: return LowerSIGN_EXTEND(Op, DAG);
11978 case ISD::ANY_EXTEND: return LowerANY_EXTEND(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000011979 case ISD::FP_TO_SINT: return LowerFP_TO_SINT(Op, DAG);
Eli Friedman948e95a2009-05-23 09:59:16 +000011980 case ISD::FP_TO_UINT: return LowerFP_TO_UINT(Op, DAG);
Michael Liao9d796db2012-10-10 16:32:15 +000011981 case ISD::FP_EXTEND: return lowerFP_EXTEND(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000011982 case ISD::FABS: return LowerFABS(Op, DAG);
11983 case ISD::FNEG: return LowerFNEG(Op, DAG);
Evan Cheng68c47cb2007-01-05 07:55:56 +000011984 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Stuart Hastings4fd0dee2011-06-01 04:39:42 +000011985 case ISD::FGETSIGN: return LowerFGETSIGN(Op, DAG);
Evan Chenge5f62042007-09-29 00:00:36 +000011986 case ISD::SETCC: return LowerSETCC(Op, DAG);
11987 case ISD::SELECT: return LowerSELECT(Op, DAG);
11988 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000011989 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000011990 case ISD::VASTART: return LowerVASTART(Op, DAG);
Dan Gohman9018e832008-05-10 01:26:14 +000011991 case ISD::VAARG: return LowerVAARG(Op, DAG);
Craig Topper55b24052012-09-11 06:15:32 +000011992 case ISD::VACOPY: return LowerVACOPY(Op, Subtarget, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000011993 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
Benjamin Kramerb9bee042012-07-12 09:31:43 +000011994 case ISD::INTRINSIC_W_CHAIN: return LowerINTRINSIC_W_CHAIN(Op, DAG);
Nate Begemanbcc5f362007-01-29 22:58:52 +000011995 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
11996 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Anton Korobeynikov2365f512007-07-14 14:06:15 +000011997 case ISD::FRAME_TO_ARGS_OFFSET:
11998 return LowerFRAME_TO_ARGS_OFFSET(Op, DAG);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +000011999 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
Anton Korobeynikov2365f512007-07-14 14:06:15 +000012000 case ISD::EH_RETURN: return LowerEH_RETURN(Op, DAG);
Michael Liao6c0e04c2012-10-15 22:39:43 +000012001 case ISD::EH_SJLJ_SETJMP: return lowerEH_SJLJ_SETJMP(Op, DAG);
12002 case ISD::EH_SJLJ_LONGJMP: return lowerEH_SJLJ_LONGJMP(Op, DAG);
Duncan Sands4a544a72011-09-06 13:37:06 +000012003 case ISD::INIT_TRAMPOLINE: return LowerINIT_TRAMPOLINE(Op, DAG);
12004 case ISD::ADJUST_TRAMPOLINE: return LowerADJUST_TRAMPOLINE(Op, DAG);
Dan Gohman1a024862008-01-31 00:41:03 +000012005 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
Evan Cheng18efe262007-12-14 02:13:44 +000012006 case ISD::CTLZ: return LowerCTLZ(Op, DAG);
Chandler Carruthacc068e2011-12-24 10:55:54 +000012007 case ISD::CTLZ_ZERO_UNDEF: return LowerCTLZ_ZERO_UNDEF(Op, DAG);
Evan Cheng18efe262007-12-14 02:13:44 +000012008 case ISD::CTTZ: return LowerCTTZ(Op, DAG);
Craig Topper55b24052012-09-11 06:15:32 +000012009 case ISD::MUL: return LowerMUL(Op, Subtarget, DAG);
Nadav Rotem43012222011-05-11 08:12:09 +000012010 case ISD::SRA:
12011 case ISD::SRL:
12012 case ISD::SHL: return LowerShift(Op, DAG);
Bill Wendling74c37652008-12-09 22:08:41 +000012013 case ISD::SADDO:
12014 case ISD::UADDO:
12015 case ISD::SSUBO:
12016 case ISD::USUBO:
12017 case ISD::SMULO:
12018 case ISD::UMULO: return LowerXALUO(Op, DAG);
Craig Topper55b24052012-09-11 06:15:32 +000012019 case ISD::READCYCLECOUNTER: return LowerREADCYCLECOUNTER(Op, Subtarget,DAG);
Wesley Peckbf17cfa2010-11-23 03:31:01 +000012020 case ISD::BITCAST: return LowerBITCAST(Op, DAG);
Chris Lattner5b856542010-12-20 00:59:46 +000012021 case ISD::ADDC:
12022 case ISD::ADDE:
12023 case ISD::SUBC:
12024 case ISD::SUBE: return LowerADDC_ADDE_SUBC_SUBE(Op, DAG);
Craig Topper13894fa2011-08-24 06:14:18 +000012025 case ISD::ADD: return LowerADD(Op, DAG);
12026 case ISD::SUB: return LowerSUB(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000012027 }
Chris Lattner27a6c732007-11-24 07:07:01 +000012028}
12029
Eli Friedmanf8f90f02011-08-24 22:33:28 +000012030static void ReplaceATOMIC_LOAD(SDNode *Node,
12031 SmallVectorImpl<SDValue> &Results,
12032 SelectionDAG &DAG) {
12033 DebugLoc dl = Node->getDebugLoc();
12034 EVT VT = cast<AtomicSDNode>(Node)->getMemoryVT();
12035
12036 // Convert wide load -> cmpxchg8b/cmpxchg16b
12037 // FIXME: On 32-bit, load -> fild or movq would be more efficient
12038 // (The only way to get a 16-byte load is cmpxchg16b)
12039 // FIXME: 16-byte ATOMIC_CMP_SWAP isn't actually hooked up at the moment.
Benjamin Kramer2753ae32011-08-27 17:36:14 +000012040 SDValue Zero = DAG.getConstant(0, VT);
12041 SDValue Swap = DAG.getAtomic(ISD::ATOMIC_CMP_SWAP, dl, VT,
Eli Friedmanf8f90f02011-08-24 22:33:28 +000012042 Node->getOperand(0),
12043 Node->getOperand(1), Zero, Zero,
12044 cast<AtomicSDNode>(Node)->getMemOperand(),
12045 cast<AtomicSDNode>(Node)->getOrdering(),
12046 cast<AtomicSDNode>(Node)->getSynchScope());
12047 Results.push_back(Swap.getValue(0));
12048 Results.push_back(Swap.getValue(1));
12049}
12050
Craig Topperc0878702012-08-17 06:55:11 +000012051static void
Duncan Sands1607f052008-12-01 11:39:25 +000012052ReplaceATOMIC_BINARY_64(SDNode *Node, SmallVectorImpl<SDValue>&Results,
Craig Topperc0878702012-08-17 06:55:11 +000012053 SelectionDAG &DAG, unsigned NewOp) {
Dale Johannesene4d209d2009-02-03 20:21:25 +000012054 DebugLoc dl = Node->getDebugLoc();
Duncan Sands17001ce2011-10-18 12:44:00 +000012055 assert (Node->getValueType(0) == MVT::i64 &&
12056 "Only know how to expand i64 atomics");
Duncan Sands1607f052008-12-01 11:39:25 +000012057
12058 SDValue Chain = Node->getOperand(0);
12059 SDValue In1 = Node->getOperand(1);
Owen Anderson825b72b2009-08-11 20:47:22 +000012060 SDValue In2L = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +000012061 Node->getOperand(2), DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +000012062 SDValue In2H = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +000012063 Node->getOperand(2), DAG.getIntPtrConstant(1));
Dan Gohmanc76909a2009-09-25 20:36:54 +000012064 SDValue Ops[] = { Chain, In1, In2L, In2H };
Owen Anderson825b72b2009-08-11 20:47:22 +000012065 SDVTList Tys = DAG.getVTList(MVT::i32, MVT::i32, MVT::Other);
Dan Gohmanc76909a2009-09-25 20:36:54 +000012066 SDValue Result =
12067 DAG.getMemIntrinsicNode(NewOp, dl, Tys, Ops, 4, MVT::i64,
12068 cast<MemSDNode>(Node)->getMemOperand());
Duncan Sands1607f052008-12-01 11:39:25 +000012069 SDValue OpsF[] = { Result.getValue(0), Result.getValue(1)};
Owen Anderson825b72b2009-08-11 20:47:22 +000012070 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, OpsF, 2));
Duncan Sands1607f052008-12-01 11:39:25 +000012071 Results.push_back(Result.getValue(2));
12072}
12073
Duncan Sands126d9072008-07-04 11:47:58 +000012074/// ReplaceNodeResults - Replace a node with an illegal result type
12075/// with a new node built out of custom code.
Duncan Sands1607f052008-12-01 11:39:25 +000012076void X86TargetLowering::ReplaceNodeResults(SDNode *N,
12077 SmallVectorImpl<SDValue>&Results,
Dan Gohmand858e902010-04-17 15:26:15 +000012078 SelectionDAG &DAG) const {
Dale Johannesene4d209d2009-02-03 20:21:25 +000012079 DebugLoc dl = N->getDebugLoc();
Nadav Rotem0a1e9142012-12-14 21:20:37 +000012080 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Chris Lattner27a6c732007-11-24 07:07:01 +000012081 switch (N->getOpcode()) {
Duncan Sandsed294c42008-10-20 15:56:33 +000012082 default:
Craig Topperabb94d02012-02-05 03:43:23 +000012083 llvm_unreachable("Do not know how to custom type legalize this operation!");
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000012084 case ISD::SIGN_EXTEND_INREG:
Chris Lattner5b856542010-12-20 00:59:46 +000012085 case ISD::ADDC:
12086 case ISD::ADDE:
12087 case ISD::SUBC:
12088 case ISD::SUBE:
12089 // We don't want to expand or promote these.
12090 return;
Michael J. Spencer1a2d0612012-02-24 19:01:22 +000012091 case ISD::FP_TO_SINT:
12092 case ISD::FP_TO_UINT: {
12093 bool IsSigned = N->getOpcode() == ISD::FP_TO_SINT;
12094
12095 if (!IsSigned && !isIntegerTypeFTOL(SDValue(N, 0).getValueType()))
12096 return;
12097
Eli Friedman948e95a2009-05-23 09:59:16 +000012098 std::pair<SDValue,SDValue> Vals =
NAKAMURA Takumi9a68fdc2012-02-25 03:37:25 +000012099 FP_TO_INTHelper(SDValue(N, 0), DAG, IsSigned, /*IsReplace=*/ true);
Duncan Sands1607f052008-12-01 11:39:25 +000012100 SDValue FIST = Vals.first, StackSlot = Vals.second;
12101 if (FIST.getNode() != 0) {
Owen Andersone50ed302009-08-10 22:56:29 +000012102 EVT VT = N->getValueType(0);
Duncan Sands1607f052008-12-01 11:39:25 +000012103 // Return a load from the stack slot.
Michael J. Spencer1a2d0612012-02-24 19:01:22 +000012104 if (StackSlot.getNode() != 0)
12105 Results.push_back(DAG.getLoad(VT, dl, FIST, StackSlot,
12106 MachinePointerInfo(),
12107 false, false, false, 0));
12108 else
12109 Results.push_back(FIST);
Duncan Sands1607f052008-12-01 11:39:25 +000012110 }
12111 return;
12112 }
Michael Liao991b6a22012-10-24 04:09:32 +000012113 case ISD::UINT_TO_FP: {
12114 if (N->getOperand(0).getValueType() != MVT::v2i32 &&
12115 N->getValueType(0) != MVT::v2f32)
12116 return;
12117 SDValue ZExtIn = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v2i64,
12118 N->getOperand(0));
12119 SDValue Bias = DAG.getConstantFP(BitsToDouble(0x4330000000000000ULL),
12120 MVT::f64);
12121 SDValue VBias = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v2f64, Bias, Bias);
12122 SDValue Or = DAG.getNode(ISD::OR, dl, MVT::v2i64, ZExtIn,
12123 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64, VBias));
12124 Or = DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Or);
12125 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::v2f64, Or, VBias);
12126 Results.push_back(DAG.getNode(X86ISD::VFPROUND, dl, MVT::v4f32, Sub));
12127 return;
12128 }
Michael Liao44c2d612012-10-10 16:53:28 +000012129 case ISD::FP_ROUND: {
Nadav Rotem0a1e9142012-12-14 21:20:37 +000012130 if (!TLI.isTypeLegal(N->getOperand(0).getValueType()))
12131 return;
Michael Liao44c2d612012-10-10 16:53:28 +000012132 SDValue V = DAG.getNode(X86ISD::VFPROUND, dl, MVT::v4f32, N->getOperand(0));
12133 Results.push_back(V);
12134 return;
12135 }
Duncan Sands1607f052008-12-01 11:39:25 +000012136 case ISD::READCYCLECOUNTER: {
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +000012137 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Duncan Sands1607f052008-12-01 11:39:25 +000012138 SDValue TheChain = N->getOperand(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +000012139 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, dl, Tys, &TheChain, 1);
Owen Anderson825b72b2009-08-11 20:47:22 +000012140 SDValue eax = DAG.getCopyFromReg(rd, dl, X86::EAX, MVT::i32,
Dale Johannesendd64c412009-02-04 00:33:20 +000012141 rd.getValue(1));
Owen Anderson825b72b2009-08-11 20:47:22 +000012142 SDValue edx = DAG.getCopyFromReg(eax.getValue(1), dl, X86::EDX, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +000012143 eax.getValue(2));
12144 // Use a buildpair to merge the two 32-bit values into a 64-bit one.
12145 SDValue Ops[] = { eax, edx };
Owen Anderson825b72b2009-08-11 20:47:22 +000012146 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Ops, 2));
Duncan Sands1607f052008-12-01 11:39:25 +000012147 Results.push_back(edx.getValue(1));
12148 return;
12149 }
Dan Gohman0b1d4a72008-12-23 21:37:04 +000012150 case ISD::ATOMIC_CMP_SWAP: {
Owen Andersone50ed302009-08-10 22:56:29 +000012151 EVT T = N->getValueType(0);
Benjamin Kramer2753ae32011-08-27 17:36:14 +000012152 assert((T == MVT::i64 || T == MVT::i128) && "can only expand cmpxchg pair");
Eli Friedman43f51ae2011-08-26 21:21:21 +000012153 bool Regs64bit = T == MVT::i128;
12154 EVT HalfT = Regs64bit ? MVT::i64 : MVT::i32;
Duncan Sands1607f052008-12-01 11:39:25 +000012155 SDValue cpInL, cpInH;
Eli Friedman43f51ae2011-08-26 21:21:21 +000012156 cpInL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, HalfT, N->getOperand(2),
12157 DAG.getConstant(0, HalfT));
12158 cpInH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, HalfT, N->getOperand(2),
12159 DAG.getConstant(1, HalfT));
12160 cpInL = DAG.getCopyToReg(N->getOperand(0), dl,
12161 Regs64bit ? X86::RAX : X86::EAX,
12162 cpInL, SDValue());
12163 cpInH = DAG.getCopyToReg(cpInL.getValue(0), dl,
12164 Regs64bit ? X86::RDX : X86::EDX,
12165 cpInH, cpInL.getValue(1));
Duncan Sands1607f052008-12-01 11:39:25 +000012166 SDValue swapInL, swapInH;
Eli Friedman43f51ae2011-08-26 21:21:21 +000012167 swapInL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, HalfT, N->getOperand(3),
12168 DAG.getConstant(0, HalfT));
12169 swapInH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, HalfT, N->getOperand(3),
12170 DAG.getConstant(1, HalfT));
12171 swapInL = DAG.getCopyToReg(cpInH.getValue(0), dl,
12172 Regs64bit ? X86::RBX : X86::EBX,
12173 swapInL, cpInH.getValue(1));
12174 swapInH = DAG.getCopyToReg(swapInL.getValue(0), dl,
Chad Rosiera20e1e72012-08-01 18:39:17 +000012175 Regs64bit ? X86::RCX : X86::ECX,
Eli Friedman43f51ae2011-08-26 21:21:21 +000012176 swapInH, swapInL.getValue(1));
Duncan Sands1607f052008-12-01 11:39:25 +000012177 SDValue Ops[] = { swapInH.getValue(0),
12178 N->getOperand(1),
12179 swapInH.getValue(1) };
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +000012180 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Andrew Trick1a2cf3b2010-10-11 19:02:04 +000012181 MachineMemOperand *MMO = cast<AtomicSDNode>(N)->getMemOperand();
Eli Friedman43f51ae2011-08-26 21:21:21 +000012182 unsigned Opcode = Regs64bit ? X86ISD::LCMPXCHG16_DAG :
12183 X86ISD::LCMPXCHG8_DAG;
12184 SDValue Result = DAG.getMemIntrinsicNode(Opcode, dl, Tys,
Andrew Trick1a2cf3b2010-10-11 19:02:04 +000012185 Ops, 3, T, MMO);
Eli Friedman43f51ae2011-08-26 21:21:21 +000012186 SDValue cpOutL = DAG.getCopyFromReg(Result.getValue(0), dl,
12187 Regs64bit ? X86::RAX : X86::EAX,
12188 HalfT, Result.getValue(1));
12189 SDValue cpOutH = DAG.getCopyFromReg(cpOutL.getValue(1), dl,
12190 Regs64bit ? X86::RDX : X86::EDX,
12191 HalfT, cpOutL.getValue(2));
Duncan Sands1607f052008-12-01 11:39:25 +000012192 SDValue OpsF[] = { cpOutL.getValue(0), cpOutH.getValue(0)};
Eli Friedman43f51ae2011-08-26 21:21:21 +000012193 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, T, OpsF, 2));
Duncan Sands1607f052008-12-01 11:39:25 +000012194 Results.push_back(cpOutH.getValue(1));
12195 return;
12196 }
Dan Gohman0b1d4a72008-12-23 21:37:04 +000012197 case ISD::ATOMIC_LOAD_ADD:
Dan Gohman0b1d4a72008-12-23 21:37:04 +000012198 case ISD::ATOMIC_LOAD_AND:
Dan Gohman0b1d4a72008-12-23 21:37:04 +000012199 case ISD::ATOMIC_LOAD_NAND:
Dan Gohman0b1d4a72008-12-23 21:37:04 +000012200 case ISD::ATOMIC_LOAD_OR:
Dan Gohman0b1d4a72008-12-23 21:37:04 +000012201 case ISD::ATOMIC_LOAD_SUB:
Dan Gohman0b1d4a72008-12-23 21:37:04 +000012202 case ISD::ATOMIC_LOAD_XOR:
Michael Liaoe5e8f762012-09-25 18:08:13 +000012203 case ISD::ATOMIC_LOAD_MAX:
12204 case ISD::ATOMIC_LOAD_MIN:
12205 case ISD::ATOMIC_LOAD_UMAX:
12206 case ISD::ATOMIC_LOAD_UMIN:
Craig Topperc0878702012-08-17 06:55:11 +000012207 case ISD::ATOMIC_SWAP: {
12208 unsigned Opc;
12209 switch (N->getOpcode()) {
12210 default: llvm_unreachable("Unexpected opcode");
12211 case ISD::ATOMIC_LOAD_ADD:
12212 Opc = X86ISD::ATOMADD64_DAG;
12213 break;
12214 case ISD::ATOMIC_LOAD_AND:
12215 Opc = X86ISD::ATOMAND64_DAG;
12216 break;
12217 case ISD::ATOMIC_LOAD_NAND:
12218 Opc = X86ISD::ATOMNAND64_DAG;
12219 break;
12220 case ISD::ATOMIC_LOAD_OR:
12221 Opc = X86ISD::ATOMOR64_DAG;
12222 break;
12223 case ISD::ATOMIC_LOAD_SUB:
12224 Opc = X86ISD::ATOMSUB64_DAG;
12225 break;
12226 case ISD::ATOMIC_LOAD_XOR:
12227 Opc = X86ISD::ATOMXOR64_DAG;
12228 break;
Michael Liaoe5e8f762012-09-25 18:08:13 +000012229 case ISD::ATOMIC_LOAD_MAX:
12230 Opc = X86ISD::ATOMMAX64_DAG;
12231 break;
12232 case ISD::ATOMIC_LOAD_MIN:
12233 Opc = X86ISD::ATOMMIN64_DAG;
12234 break;
12235 case ISD::ATOMIC_LOAD_UMAX:
12236 Opc = X86ISD::ATOMUMAX64_DAG;
12237 break;
12238 case ISD::ATOMIC_LOAD_UMIN:
12239 Opc = X86ISD::ATOMUMIN64_DAG;
12240 break;
Craig Topperc0878702012-08-17 06:55:11 +000012241 case ISD::ATOMIC_SWAP:
12242 Opc = X86ISD::ATOMSWAP64_DAG;
12243 break;
12244 }
12245 ReplaceATOMIC_BINARY_64(N, Results, DAG, Opc);
Duncan Sands1607f052008-12-01 11:39:25 +000012246 return;
Craig Topperc0878702012-08-17 06:55:11 +000012247 }
Eli Friedmanf8f90f02011-08-24 22:33:28 +000012248 case ISD::ATOMIC_LOAD:
12249 ReplaceATOMIC_LOAD(N, Results, DAG);
Chris Lattner27a6c732007-11-24 07:07:01 +000012250 }
Evan Cheng0db9fe62006-04-25 20:13:52 +000012251}
12252
Evan Cheng72261582005-12-20 06:22:03 +000012253const char *X86TargetLowering::getTargetNodeName(unsigned Opcode) const {
12254 switch (Opcode) {
12255 default: return NULL;
Evan Cheng18efe262007-12-14 02:13:44 +000012256 case X86ISD::BSF: return "X86ISD::BSF";
12257 case X86ISD::BSR: return "X86ISD::BSR";
Evan Chenge3413162006-01-09 18:33:28 +000012258 case X86ISD::SHLD: return "X86ISD::SHLD";
12259 case X86ISD::SHRD: return "X86ISD::SHRD";
Evan Chengef6ffb12006-01-31 03:14:29 +000012260 case X86ISD::FAND: return "X86ISD::FAND";
Evan Cheng68c47cb2007-01-05 07:55:56 +000012261 case X86ISD::FOR: return "X86ISD::FOR";
Evan Cheng223547a2006-01-31 22:28:30 +000012262 case X86ISD::FXOR: return "X86ISD::FXOR";
Evan Cheng68c47cb2007-01-05 07:55:56 +000012263 case X86ISD::FSRL: return "X86ISD::FSRL";
Evan Chenga3195e82006-01-12 22:54:21 +000012264 case X86ISD::FILD: return "X86ISD::FILD";
Evan Chenge3de85b2006-02-04 02:20:30 +000012265 case X86ISD::FILD_FLAG: return "X86ISD::FILD_FLAG";
Evan Cheng72261582005-12-20 06:22:03 +000012266 case X86ISD::FP_TO_INT16_IN_MEM: return "X86ISD::FP_TO_INT16_IN_MEM";
12267 case X86ISD::FP_TO_INT32_IN_MEM: return "X86ISD::FP_TO_INT32_IN_MEM";
12268 case X86ISD::FP_TO_INT64_IN_MEM: return "X86ISD::FP_TO_INT64_IN_MEM";
Evan Chengb077b842005-12-21 02:39:21 +000012269 case X86ISD::FLD: return "X86ISD::FLD";
Evan Chengd90eb7f2006-01-05 00:27:02 +000012270 case X86ISD::FST: return "X86ISD::FST";
Evan Cheng72261582005-12-20 06:22:03 +000012271 case X86ISD::CALL: return "X86ISD::CALL";
Evan Cheng72261582005-12-20 06:22:03 +000012272 case X86ISD::RDTSC_DAG: return "X86ISD::RDTSC_DAG";
Dan Gohmanc7a37d42008-12-23 22:45:23 +000012273 case X86ISD::BT: return "X86ISD::BT";
Evan Cheng72261582005-12-20 06:22:03 +000012274 case X86ISD::CMP: return "X86ISD::CMP";
Evan Cheng6be2c582006-04-05 23:38:46 +000012275 case X86ISD::COMI: return "X86ISD::COMI";
12276 case X86ISD::UCOMI: return "X86ISD::UCOMI";
Evan Chengd5781fc2005-12-21 20:21:51 +000012277 case X86ISD::SETCC: return "X86ISD::SETCC";
Evan Chengad9c0a32009-12-15 00:53:42 +000012278 case X86ISD::SETCC_CARRY: return "X86ISD::SETCC_CARRY";
Stuart Hastings865f0932011-06-03 23:53:54 +000012279 case X86ISD::FSETCCsd: return "X86ISD::FSETCCsd";
12280 case X86ISD::FSETCCss: return "X86ISD::FSETCCss";
Evan Cheng72261582005-12-20 06:22:03 +000012281 case X86ISD::CMOV: return "X86ISD::CMOV";
12282 case X86ISD::BRCOND: return "X86ISD::BRCOND";
Evan Chengb077b842005-12-21 02:39:21 +000012283 case X86ISD::RET_FLAG: return "X86ISD::RET_FLAG";
Evan Cheng8df346b2006-03-04 01:12:00 +000012284 case X86ISD::REP_STOS: return "X86ISD::REP_STOS";
12285 case X86ISD::REP_MOVS: return "X86ISD::REP_MOVS";
Evan Cheng7ccced62006-02-18 00:15:05 +000012286 case X86ISD::GlobalBaseReg: return "X86ISD::GlobalBaseReg";
Evan Cheng020d2e82006-02-23 20:41:18 +000012287 case X86ISD::Wrapper: return "X86ISD::Wrapper";
Chris Lattner18c59872009-06-27 04:16:01 +000012288 case X86ISD::WrapperRIP: return "X86ISD::WrapperRIP";
Nate Begeman14d12ca2008-02-11 04:19:36 +000012289 case X86ISD::PEXTRB: return "X86ISD::PEXTRB";
Evan Chengb067a1e2006-03-31 19:22:53 +000012290 case X86ISD::PEXTRW: return "X86ISD::PEXTRW";
Nate Begeman14d12ca2008-02-11 04:19:36 +000012291 case X86ISD::INSERTPS: return "X86ISD::INSERTPS";
12292 case X86ISD::PINSRB: return "X86ISD::PINSRB";
Evan Cheng653159f2006-03-31 21:55:24 +000012293 case X86ISD::PINSRW: return "X86ISD::PINSRW";
Nate Begemanb9a47b82009-02-23 08:49:38 +000012294 case X86ISD::PSHUFB: return "X86ISD::PSHUFB";
Bruno Cardoso Lopesc1af4772011-07-13 21:36:47 +000012295 case X86ISD::ANDNP: return "X86ISD::ANDNP";
Craig Topper31133842011-11-19 07:33:10 +000012296 case X86ISD::PSIGN: return "X86ISD::PSIGN";
Craig Toppere6a62772011-11-13 17:31:07 +000012297 case X86ISD::BLENDV: return "X86ISD::BLENDV";
Elena Demikhovsky226e0e62012-12-05 09:24:57 +000012298 case X86ISD::BLENDI: return "X86ISD::BLENDI";
Benjamin Kramer388fc6a2012-12-15 16:47:44 +000012299 case X86ISD::SUBUS: return "X86ISD::SUBUS";
Craig Topperfe033152011-12-06 09:31:36 +000012300 case X86ISD::HADD: return "X86ISD::HADD";
12301 case X86ISD::HSUB: return "X86ISD::HSUB";
Craig Toppere6a62772011-11-13 17:31:07 +000012302 case X86ISD::FHADD: return "X86ISD::FHADD";
12303 case X86ISD::FHSUB: return "X86ISD::FHSUB";
Benjamin Kramer739c7a82012-12-21 14:04:55 +000012304 case X86ISD::UMAX: return "X86ISD::UMAX";
12305 case X86ISD::UMIN: return "X86ISD::UMIN";
12306 case X86ISD::SMAX: return "X86ISD::SMAX";
12307 case X86ISD::SMIN: return "X86ISD::SMIN";
Evan Cheng8ca29322006-11-10 21:43:37 +000012308 case X86ISD::FMAX: return "X86ISD::FMAX";
12309 case X86ISD::FMIN: return "X86ISD::FMIN";
Nadav Rotemd60cb112012-08-19 13:06:16 +000012310 case X86ISD::FMAXC: return "X86ISD::FMAXC";
12311 case X86ISD::FMINC: return "X86ISD::FMINC";
Dan Gohman20382522007-07-10 00:05:58 +000012312 case X86ISD::FRSQRT: return "X86ISD::FRSQRT";
12313 case X86ISD::FRCP: return "X86ISD::FRCP";
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +000012314 case X86ISD::TLSADDR: return "X86ISD::TLSADDR";
Hans Wennborgf0234fc2012-06-01 16:27:21 +000012315 case X86ISD::TLSBASEADDR: return "X86ISD::TLSBASEADDR";
Eric Christopher30ef0e52010-06-03 04:07:48 +000012316 case X86ISD::TLSCALL: return "X86ISD::TLSCALL";
Michael Liao6c0e04c2012-10-15 22:39:43 +000012317 case X86ISD::EH_SJLJ_SETJMP: return "X86ISD::EH_SJLJ_SETJMP";
12318 case X86ISD::EH_SJLJ_LONGJMP: return "X86ISD::EH_SJLJ_LONGJMP";
Anton Korobeynikov2365f512007-07-14 14:06:15 +000012319 case X86ISD::EH_RETURN: return "X86ISD::EH_RETURN";
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +000012320 case X86ISD::TC_RETURN: return "X86ISD::TC_RETURN";
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000012321 case X86ISD::FNSTCW16m: return "X86ISD::FNSTCW16m";
Benjamin Kramer17c836c2012-04-27 12:07:43 +000012322 case X86ISD::FNSTSW16r: return "X86ISD::FNSTSW16r";
Evan Cheng7e2ff772008-05-08 00:57:18 +000012323 case X86ISD::LCMPXCHG_DAG: return "X86ISD::LCMPXCHG_DAG";
12324 case X86ISD::LCMPXCHG8_DAG: return "X86ISD::LCMPXCHG8_DAG";
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012325 case X86ISD::ATOMADD64_DAG: return "X86ISD::ATOMADD64_DAG";
12326 case X86ISD::ATOMSUB64_DAG: return "X86ISD::ATOMSUB64_DAG";
12327 case X86ISD::ATOMOR64_DAG: return "X86ISD::ATOMOR64_DAG";
12328 case X86ISD::ATOMXOR64_DAG: return "X86ISD::ATOMXOR64_DAG";
12329 case X86ISD::ATOMAND64_DAG: return "X86ISD::ATOMAND64_DAG";
12330 case X86ISD::ATOMNAND64_DAG: return "X86ISD::ATOMNAND64_DAG";
Evan Chengd880b972008-05-09 21:53:03 +000012331 case X86ISD::VZEXT_MOVL: return "X86ISD::VZEXT_MOVL";
Michael Liaob7bf7262012-08-14 22:53:17 +000012332 case X86ISD::VSEXT_MOVL: return "X86ISD::VSEXT_MOVL";
Evan Chengd880b972008-05-09 21:53:03 +000012333 case X86ISD::VZEXT_LOAD: return "X86ISD::VZEXT_LOAD";
Michael Liaod9d09602012-10-23 17:34:00 +000012334 case X86ISD::VZEXT: return "X86ISD::VZEXT";
12335 case X86ISD::VSEXT: return "X86ISD::VSEXT";
Michael Liao7091b242012-08-14 21:24:47 +000012336 case X86ISD::VFPEXT: return "X86ISD::VFPEXT";
Michael Liao44c2d612012-10-10 16:53:28 +000012337 case X86ISD::VFPROUND: return "X86ISD::VFPROUND";
Craig Toppered2e13d2012-01-22 19:15:14 +000012338 case X86ISD::VSHLDQ: return "X86ISD::VSHLDQ";
12339 case X86ISD::VSRLDQ: return "X86ISD::VSRLDQ";
Evan Chengf26ffe92008-05-29 08:22:04 +000012340 case X86ISD::VSHL: return "X86ISD::VSHL";
12341 case X86ISD::VSRL: return "X86ISD::VSRL";
Craig Toppered2e13d2012-01-22 19:15:14 +000012342 case X86ISD::VSRA: return "X86ISD::VSRA";
12343 case X86ISD::VSHLI: return "X86ISD::VSHLI";
12344 case X86ISD::VSRLI: return "X86ISD::VSRLI";
12345 case X86ISD::VSRAI: return "X86ISD::VSRAI";
Craig Topper1906d322012-01-22 23:36:02 +000012346 case X86ISD::CMPP: return "X86ISD::CMPP";
Craig Topper67609fd2012-01-22 22:42:16 +000012347 case X86ISD::PCMPEQ: return "X86ISD::PCMPEQ";
12348 case X86ISD::PCMPGT: return "X86ISD::PCMPGT";
Bill Wendlingab55ebd2008-12-12 00:56:36 +000012349 case X86ISD::ADD: return "X86ISD::ADD";
12350 case X86ISD::SUB: return "X86ISD::SUB";
Chris Lattner5b856542010-12-20 00:59:46 +000012351 case X86ISD::ADC: return "X86ISD::ADC";
12352 case X86ISD::SBB: return "X86ISD::SBB";
Bill Wendlingd350e022008-12-12 21:15:41 +000012353 case X86ISD::SMUL: return "X86ISD::SMUL";
12354 case X86ISD::UMUL: return "X86ISD::UMUL";
Dan Gohman076aee32009-03-04 19:44:21 +000012355 case X86ISD::INC: return "X86ISD::INC";
12356 case X86ISD::DEC: return "X86ISD::DEC";
Dan Gohmane220c4b2009-09-18 19:59:53 +000012357 case X86ISD::OR: return "X86ISD::OR";
12358 case X86ISD::XOR: return "X86ISD::XOR";
12359 case X86ISD::AND: return "X86ISD::AND";
Craig Toppere6a62772011-11-13 17:31:07 +000012360 case X86ISD::BLSI: return "X86ISD::BLSI";
12361 case X86ISD::BLSMSK: return "X86ISD::BLSMSK";
12362 case X86ISD::BLSR: return "X86ISD::BLSR";
Evan Cheng73f24c92009-03-30 21:36:47 +000012363 case X86ISD::MUL_IMM: return "X86ISD::MUL_IMM";
Eric Christopher71c67532009-07-29 00:28:05 +000012364 case X86ISD::PTEST: return "X86ISD::PTEST";
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +000012365 case X86ISD::TESTP: return "X86ISD::TESTP";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000012366 case X86ISD::PALIGN: return "X86ISD::PALIGN";
12367 case X86ISD::PSHUFD: return "X86ISD::PSHUFD";
12368 case X86ISD::PSHUFHW: return "X86ISD::PSHUFHW";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000012369 case X86ISD::PSHUFLW: return "X86ISD::PSHUFLW";
Craig Topperb3982da2011-12-31 23:50:21 +000012370 case X86ISD::SHUFP: return "X86ISD::SHUFP";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000012371 case X86ISD::MOVLHPS: return "X86ISD::MOVLHPS";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000012372 case X86ISD::MOVLHPD: return "X86ISD::MOVLHPD";
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +000012373 case X86ISD::MOVHLPS: return "X86ISD::MOVHLPS";
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +000012374 case X86ISD::MOVLPS: return "X86ISD::MOVLPS";
12375 case X86ISD::MOVLPD: return "X86ISD::MOVLPD";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000012376 case X86ISD::MOVDDUP: return "X86ISD::MOVDDUP";
12377 case X86ISD::MOVSHDUP: return "X86ISD::MOVSHDUP";
12378 case X86ISD::MOVSLDUP: return "X86ISD::MOVSLDUP";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000012379 case X86ISD::MOVSD: return "X86ISD::MOVSD";
12380 case X86ISD::MOVSS: return "X86ISD::MOVSS";
Craig Topper34671b82011-12-06 08:21:25 +000012381 case X86ISD::UNPCKL: return "X86ISD::UNPCKL";
12382 case X86ISD::UNPCKH: return "X86ISD::UNPCKH";
Bruno Cardoso Lopes0e6d2302011-08-17 02:29:19 +000012383 case X86ISD::VBROADCAST: return "X86ISD::VBROADCAST";
Craig Topper316cd2a2011-11-30 06:25:25 +000012384 case X86ISD::VPERMILP: return "X86ISD::VPERMILP";
Craig Topperec24e612011-11-30 07:47:51 +000012385 case X86ISD::VPERM2X128: return "X86ISD::VPERM2X128";
Craig Topper8325c112012-04-16 00:41:45 +000012386 case X86ISD::VPERMV: return "X86ISD::VPERMV";
12387 case X86ISD::VPERMI: return "X86ISD::VPERMI";
Craig Topper5b209e82012-02-05 03:14:49 +000012388 case X86ISD::PMULUDQ: return "X86ISD::PMULUDQ";
Dan Gohmand6708ea2009-08-15 01:38:56 +000012389 case X86ISD::VASTART_SAVE_XMM_REGS: return "X86ISD::VASTART_SAVE_XMM_REGS";
Dan Gohman320afb82010-10-12 18:00:49 +000012390 case X86ISD::VAARG_64: return "X86ISD::VAARG_64";
Michael J. Spencere9c253e2010-10-21 01:41:01 +000012391 case X86ISD::WIN_ALLOCA: return "X86ISD::WIN_ALLOCA";
Eli Friedman14648462011-07-27 22:21:52 +000012392 case X86ISD::MEMBARRIER: return "X86ISD::MEMBARRIER";
Rafael Espindolad07b7ec2011-08-30 19:43:21 +000012393 case X86ISD::SEG_ALLOCA: return "X86ISD::SEG_ALLOCA";
Michael J. Spencer1a2d0612012-02-24 19:01:22 +000012394 case X86ISD::WIN_FTOL: return "X86ISD::WIN_FTOL";
Benjamin Kramer17c836c2012-04-27 12:07:43 +000012395 case X86ISD::SAHF: return "X86ISD::SAHF";
Benjamin Kramerb9bee042012-07-12 09:31:43 +000012396 case X86ISD::RDRAND: return "X86ISD::RDRAND";
Elena Demikhovsky1503aba2012-08-01 12:06:00 +000012397 case X86ISD::FMADD: return "X86ISD::FMADD";
12398 case X86ISD::FMSUB: return "X86ISD::FMSUB";
12399 case X86ISD::FNMADD: return "X86ISD::FNMADD";
12400 case X86ISD::FNMSUB: return "X86ISD::FNMSUB";
12401 case X86ISD::FMADDSUB: return "X86ISD::FMADDSUB";
12402 case X86ISD::FMSUBADD: return "X86ISD::FMSUBADD";
Craig Topper9c7ae012012-11-10 01:23:36 +000012403 case X86ISD::PCMPESTRI: return "X86ISD::PCMPESTRI";
12404 case X86ISD::PCMPISTRI: return "X86ISD::PCMPISTRI";
Evan Cheng72261582005-12-20 06:22:03 +000012405 }
12406}
Evan Cheng3a03ebb2005-12-21 23:05:39 +000012407
Chris Lattnerc9addb72007-03-30 23:15:24 +000012408// isLegalAddressingMode - Return true if the addressing mode represented
12409// by AM is legal for this target, for a load/store of the specified type.
Scott Michelfdc40a02009-02-17 22:15:04 +000012410bool X86TargetLowering::isLegalAddressingMode(const AddrMode &AM,
Chris Lattnerdb125cf2011-07-18 04:54:35 +000012411 Type *Ty) const {
Chris Lattnerc9addb72007-03-30 23:15:24 +000012412 // X86 supports extremely general addressing modes.
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000012413 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman92b651f2010-08-24 15:55:12 +000012414 Reloc::Model R = getTargetMachine().getRelocationModel();
Scott Michelfdc40a02009-02-17 22:15:04 +000012415
Chris Lattnerc9addb72007-03-30 23:15:24 +000012416 // X86 allows a sign-extended 32-bit immediate field as a displacement.
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000012417 if (!X86::isOffsetSuitableForCodeModel(AM.BaseOffs, M, AM.BaseGV != NULL))
Chris Lattnerc9addb72007-03-30 23:15:24 +000012418 return false;
Scott Michelfdc40a02009-02-17 22:15:04 +000012419
Chris Lattnerc9addb72007-03-30 23:15:24 +000012420 if (AM.BaseGV) {
Chris Lattnerdfed4132009-07-10 07:38:24 +000012421 unsigned GVFlags =
12422 Subtarget->ClassifyGlobalReference(AM.BaseGV, getTargetMachine());
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000012423
Chris Lattnerdfed4132009-07-10 07:38:24 +000012424 // If a reference to this global requires an extra load, we can't fold it.
12425 if (isGlobalStubReference(GVFlags))
Chris Lattnerc9addb72007-03-30 23:15:24 +000012426 return false;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000012427
Chris Lattnerdfed4132009-07-10 07:38:24 +000012428 // If BaseGV requires a register for the PIC base, we cannot also have a
12429 // BaseReg specified.
12430 if (AM.HasBaseReg && isGlobalRelativeToPICBase(GVFlags))
Dale Johannesen203af582008-12-05 21:47:27 +000012431 return false;
Evan Cheng52787842007-08-01 23:46:47 +000012432
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000012433 // If lower 4G is not available, then we must use rip-relative addressing.
Dan Gohman92b651f2010-08-24 15:55:12 +000012434 if ((M != CodeModel::Small || R != Reloc::Static) &&
12435 Subtarget->is64Bit() && (AM.BaseOffs || AM.Scale > 1))
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000012436 return false;
Chris Lattnerc9addb72007-03-30 23:15:24 +000012437 }
Scott Michelfdc40a02009-02-17 22:15:04 +000012438
Chris Lattnerc9addb72007-03-30 23:15:24 +000012439 switch (AM.Scale) {
12440 case 0:
12441 case 1:
12442 case 2:
12443 case 4:
12444 case 8:
12445 // These scales always work.
12446 break;
12447 case 3:
12448 case 5:
12449 case 9:
12450 // These scales are formed with basereg+scalereg. Only accept if there is
12451 // no basereg yet.
12452 if (AM.HasBaseReg)
12453 return false;
12454 break;
12455 default: // Other stuff never works.
12456 return false;
12457 }
Scott Michelfdc40a02009-02-17 22:15:04 +000012458
Chris Lattnerc9addb72007-03-30 23:15:24 +000012459 return true;
12460}
12461
Chris Lattnerdb125cf2011-07-18 04:54:35 +000012462bool X86TargetLowering::isTruncateFree(Type *Ty1, Type *Ty2) const {
Duncan Sandsb0bc6c32010-02-15 16:12:20 +000012463 if (!Ty1->isIntegerTy() || !Ty2->isIntegerTy())
Evan Cheng2bd122c2007-10-26 01:56:11 +000012464 return false;
Evan Chenge127a732007-10-29 07:57:50 +000012465 unsigned NumBits1 = Ty1->getPrimitiveSizeInBits();
12466 unsigned NumBits2 = Ty2->getPrimitiveSizeInBits();
Jakub Staszakc20323a2012-12-29 15:57:26 +000012467 return NumBits1 > NumBits2;
Evan Cheng2bd122c2007-10-26 01:56:11 +000012468}
12469
Evan Cheng70e10d32012-07-17 06:53:39 +000012470bool X86TargetLowering::isLegalICmpImmediate(int64_t Imm) const {
Jakub Staszakc20323a2012-12-29 15:57:26 +000012471 return isInt<32>(Imm);
Evan Cheng70e10d32012-07-17 06:53:39 +000012472}
12473
12474bool X86TargetLowering::isLegalAddImmediate(int64_t Imm) const {
Evan Chenga9e13ba2012-07-17 18:54:11 +000012475 // Can also use sub to handle negated immediates.
Jakub Staszakc20323a2012-12-29 15:57:26 +000012476 return isInt<32>(Imm);
Evan Cheng70e10d32012-07-17 06:53:39 +000012477}
12478
Owen Andersone50ed302009-08-10 22:56:29 +000012479bool X86TargetLowering::isTruncateFree(EVT VT1, EVT VT2) const {
Duncan Sands83ec4b62008-06-06 12:08:01 +000012480 if (!VT1.isInteger() || !VT2.isInteger())
Evan Cheng3c3ddb32007-10-29 19:58:20 +000012481 return false;
Duncan Sands83ec4b62008-06-06 12:08:01 +000012482 unsigned NumBits1 = VT1.getSizeInBits();
12483 unsigned NumBits2 = VT2.getSizeInBits();
Jakub Staszakc20323a2012-12-29 15:57:26 +000012484 return NumBits1 > NumBits2;
Evan Cheng3c3ddb32007-10-29 19:58:20 +000012485}
Evan Cheng2bd122c2007-10-26 01:56:11 +000012486
Chris Lattnerdb125cf2011-07-18 04:54:35 +000012487bool X86TargetLowering::isZExtFree(Type *Ty1, Type *Ty2) const {
Dan Gohman349ba492009-04-09 02:06:09 +000012488 // x86-64 implicitly zero-extends 32-bit results in 64-bit registers.
Duncan Sandsb0bc6c32010-02-15 16:12:20 +000012489 return Ty1->isIntegerTy(32) && Ty2->isIntegerTy(64) && Subtarget->is64Bit();
Dan Gohman97121ba2009-04-08 00:15:30 +000012490}
12491
Owen Andersone50ed302009-08-10 22:56:29 +000012492bool X86TargetLowering::isZExtFree(EVT VT1, EVT VT2) const {
Dan Gohman349ba492009-04-09 02:06:09 +000012493 // x86-64 implicitly zero-extends 32-bit results in 64-bit registers.
Owen Anderson825b72b2009-08-11 20:47:22 +000012494 return VT1 == MVT::i32 && VT2 == MVT::i64 && Subtarget->is64Bit();
Dan Gohman97121ba2009-04-08 00:15:30 +000012495}
12496
Evan Cheng2766a472012-12-06 19:13:27 +000012497bool X86TargetLowering::isZExtFree(SDValue Val, EVT VT2) const {
12498 EVT VT1 = Val.getValueType();
12499 if (isZExtFree(VT1, VT2))
12500 return true;
12501
12502 if (Val.getOpcode() != ISD::LOAD)
12503 return false;
12504
12505 if (!VT1.isSimple() || !VT1.isInteger() ||
12506 !VT2.isSimple() || !VT2.isInteger())
12507 return false;
12508
12509 switch (VT1.getSimpleVT().SimpleTy) {
12510 default: break;
12511 case MVT::i8:
12512 case MVT::i16:
12513 case MVT::i32:
12514 // X86 has 8, 16, and 32-bit zero-extending loads.
12515 return true;
12516 }
12517
12518 return false;
12519}
12520
Owen Andersone50ed302009-08-10 22:56:29 +000012521bool X86TargetLowering::isNarrowingProfitable(EVT VT1, EVT VT2) const {
Evan Cheng8b944d32009-05-28 00:35:15 +000012522 // i16 instructions are longer (0x66 prefix) and potentially slower.
Owen Anderson825b72b2009-08-11 20:47:22 +000012523 return !(VT1 == MVT::i32 && VT2 == MVT::i16);
Evan Cheng8b944d32009-05-28 00:35:15 +000012524}
12525
Evan Cheng60c07e12006-07-05 22:17:51 +000012526/// isShuffleMaskLegal - Targets can use this to indicate that they only
12527/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
12528/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
12529/// are assumed to be legal.
12530bool
Eric Christopherfd179292009-08-27 18:07:15 +000012531X86TargetLowering::isShuffleMaskLegal(const SmallVectorImpl<int> &M,
Owen Andersone50ed302009-08-10 22:56:29 +000012532 EVT VT) const {
Eric Christophercff6f852010-04-15 01:40:20 +000012533 // Very little shuffling can be done for 64-bit vectors right now.
Nate Begeman9008ca62009-04-27 18:41:29 +000012534 if (VT.getSizeInBits() == 64)
Craig Topper1dc0fbc2011-12-05 07:27:14 +000012535 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +000012536
Nate Begemana09008b2009-10-19 02:17:23 +000012537 // FIXME: pshufb, blends, shifts.
Nate Begeman9008ca62009-04-27 18:41:29 +000012538 return (VT.getVectorNumElements() == 2 ||
12539 ShuffleVectorSDNode::isSplatMask(&M[0], VT) ||
12540 isMOVLMask(M, VT) ||
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000012541 isSHUFPMask(M, VT, Subtarget->hasFp256()) ||
Nate Begeman9008ca62009-04-27 18:41:29 +000012542 isPSHUFDMask(M, VT) ||
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000012543 isPSHUFHWMask(M, VT, Subtarget->hasInt256()) ||
12544 isPSHUFLWMask(M, VT, Subtarget->hasInt256()) ||
Craig Topper0e2037b2012-01-20 05:53:00 +000012545 isPALIGNRMask(M, VT, Subtarget) ||
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000012546 isUNPCKLMask(M, VT, Subtarget->hasInt256()) ||
12547 isUNPCKHMask(M, VT, Subtarget->hasInt256()) ||
12548 isUNPCKL_v_undef_Mask(M, VT, Subtarget->hasInt256()) ||
12549 isUNPCKH_v_undef_Mask(M, VT, Subtarget->hasInt256()));
Evan Cheng60c07e12006-07-05 22:17:51 +000012550}
12551
Dan Gohman7d8143f2008-04-09 20:09:42 +000012552bool
Nate Begeman5a5ca152009-04-29 05:20:52 +000012553X86TargetLowering::isVectorClearMaskLegal(const SmallVectorImpl<int> &Mask,
Owen Andersone50ed302009-08-10 22:56:29 +000012554 EVT VT) const {
Nate Begeman9008ca62009-04-27 18:41:29 +000012555 unsigned NumElts = VT.getVectorNumElements();
12556 // FIXME: This collection of masks seems suspect.
12557 if (NumElts == 2)
12558 return true;
Craig Topper7a9a28b2012-08-12 02:23:29 +000012559 if (NumElts == 4 && VT.is128BitVector()) {
Nate Begeman9008ca62009-04-27 18:41:29 +000012560 return (isMOVLMask(Mask, VT) ||
12561 isCommutedMOVLMask(Mask, VT, true) ||
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000012562 isSHUFPMask(Mask, VT, Subtarget->hasFp256()) ||
12563 isSHUFPMask(Mask, VT, Subtarget->hasFp256(), /* Commuted */ true));
Evan Cheng60c07e12006-07-05 22:17:51 +000012564 }
12565 return false;
12566}
12567
12568//===----------------------------------------------------------------------===//
12569// X86 Scheduler Hooks
12570//===----------------------------------------------------------------------===//
12571
Michael Liaobe02a902012-11-08 07:28:54 +000012572/// Utility function to emit xbegin specifying the start of an RTM region.
Craig Topper2da36912012-11-11 22:45:02 +000012573static MachineBasicBlock *EmitXBegin(MachineInstr *MI, MachineBasicBlock *MBB,
12574 const TargetInstrInfo *TII) {
Michael Liaobe02a902012-11-08 07:28:54 +000012575 DebugLoc DL = MI->getDebugLoc();
Michael Liaobe02a902012-11-08 07:28:54 +000012576
12577 const BasicBlock *BB = MBB->getBasicBlock();
12578 MachineFunction::iterator I = MBB;
12579 ++I;
12580
12581 // For the v = xbegin(), we generate
12582 //
12583 // thisMBB:
12584 // xbegin sinkMBB
12585 //
12586 // mainMBB:
12587 // eax = -1
12588 //
12589 // sinkMBB:
12590 // v = eax
12591
12592 MachineBasicBlock *thisMBB = MBB;
12593 MachineFunction *MF = MBB->getParent();
12594 MachineBasicBlock *mainMBB = MF->CreateMachineBasicBlock(BB);
12595 MachineBasicBlock *sinkMBB = MF->CreateMachineBasicBlock(BB);
12596 MF->insert(I, mainMBB);
12597 MF->insert(I, sinkMBB);
12598
12599 // Transfer the remainder of BB and its successor edges to sinkMBB.
12600 sinkMBB->splice(sinkMBB->begin(), MBB,
12601 llvm::next(MachineBasicBlock::iterator(MI)), MBB->end());
12602 sinkMBB->transferSuccessorsAndUpdatePHIs(MBB);
12603
12604 // thisMBB:
12605 // xbegin sinkMBB
12606 // # fallthrough to mainMBB
12607 // # abortion to sinkMBB
12608 BuildMI(thisMBB, DL, TII->get(X86::XBEGIN_4)).addMBB(sinkMBB);
12609 thisMBB->addSuccessor(mainMBB);
12610 thisMBB->addSuccessor(sinkMBB);
12611
12612 // mainMBB:
12613 // EAX = -1
12614 BuildMI(mainMBB, DL, TII->get(X86::MOV32ri), X86::EAX).addImm(-1);
12615 mainMBB->addSuccessor(sinkMBB);
12616
12617 // sinkMBB:
12618 // EAX is live into the sinkMBB
12619 sinkMBB->addLiveIn(X86::EAX);
12620 BuildMI(*sinkMBB, sinkMBB->begin(), DL,
12621 TII->get(TargetOpcode::COPY), MI->getOperand(0).getReg())
12622 .addReg(X86::EAX);
12623
12624 MI->eraseFromParent();
12625 return sinkMBB;
12626}
12627
Michael Liaob118a072012-09-20 03:06:15 +000012628// Get CMPXCHG opcode for the specified data type.
12629static unsigned getCmpXChgOpcode(EVT VT) {
12630 switch (VT.getSimpleVT().SimpleTy) {
12631 case MVT::i8: return X86::LCMPXCHG8;
12632 case MVT::i16: return X86::LCMPXCHG16;
12633 case MVT::i32: return X86::LCMPXCHG32;
12634 case MVT::i64: return X86::LCMPXCHG64;
12635 default:
12636 break;
Richard Smith42fc29e2012-04-13 22:47:00 +000012637 }
Michael Liaob118a072012-09-20 03:06:15 +000012638 llvm_unreachable("Invalid operand size!");
Mon P Wang63307c32008-05-05 19:05:59 +000012639}
12640
Michael Liaob118a072012-09-20 03:06:15 +000012641// Get LOAD opcode for the specified data type.
12642static unsigned getLoadOpcode(EVT VT) {
12643 switch (VT.getSimpleVT().SimpleTy) {
12644 case MVT::i8: return X86::MOV8rm;
12645 case MVT::i16: return X86::MOV16rm;
12646 case MVT::i32: return X86::MOV32rm;
12647 case MVT::i64: return X86::MOV64rm;
12648 default:
12649 break;
12650 }
12651 llvm_unreachable("Invalid operand size!");
12652}
12653
12654// Get opcode of the non-atomic one from the specified atomic instruction.
12655static unsigned getNonAtomicOpcode(unsigned Opc) {
12656 switch (Opc) {
12657 case X86::ATOMAND8: return X86::AND8rr;
12658 case X86::ATOMAND16: return X86::AND16rr;
12659 case X86::ATOMAND32: return X86::AND32rr;
12660 case X86::ATOMAND64: return X86::AND64rr;
12661 case X86::ATOMOR8: return X86::OR8rr;
12662 case X86::ATOMOR16: return X86::OR16rr;
12663 case X86::ATOMOR32: return X86::OR32rr;
12664 case X86::ATOMOR64: return X86::OR64rr;
12665 case X86::ATOMXOR8: return X86::XOR8rr;
12666 case X86::ATOMXOR16: return X86::XOR16rr;
12667 case X86::ATOMXOR32: return X86::XOR32rr;
12668 case X86::ATOMXOR64: return X86::XOR64rr;
12669 }
12670 llvm_unreachable("Unhandled atomic-load-op opcode!");
12671}
12672
12673// Get opcode of the non-atomic one from the specified atomic instruction with
12674// extra opcode.
12675static unsigned getNonAtomicOpcodeWithExtraOpc(unsigned Opc,
12676 unsigned &ExtraOpc) {
12677 switch (Opc) {
12678 case X86::ATOMNAND8: ExtraOpc = X86::NOT8r; return X86::AND8rr;
12679 case X86::ATOMNAND16: ExtraOpc = X86::NOT16r; return X86::AND16rr;
12680 case X86::ATOMNAND32: ExtraOpc = X86::NOT32r; return X86::AND32rr;
12681 case X86::ATOMNAND64: ExtraOpc = X86::NOT64r; return X86::AND64rr;
Michael Liaofe87c302012-09-21 03:18:52 +000012682 case X86::ATOMMAX8: ExtraOpc = X86::CMP8rr; return X86::CMOVL32rr;
Michael Liaob118a072012-09-20 03:06:15 +000012683 case X86::ATOMMAX16: ExtraOpc = X86::CMP16rr; return X86::CMOVL16rr;
12684 case X86::ATOMMAX32: ExtraOpc = X86::CMP32rr; return X86::CMOVL32rr;
12685 case X86::ATOMMAX64: ExtraOpc = X86::CMP64rr; return X86::CMOVL64rr;
Michael Liaofe87c302012-09-21 03:18:52 +000012686 case X86::ATOMMIN8: ExtraOpc = X86::CMP8rr; return X86::CMOVG32rr;
Michael Liaob118a072012-09-20 03:06:15 +000012687 case X86::ATOMMIN16: ExtraOpc = X86::CMP16rr; return X86::CMOVG16rr;
12688 case X86::ATOMMIN32: ExtraOpc = X86::CMP32rr; return X86::CMOVG32rr;
12689 case X86::ATOMMIN64: ExtraOpc = X86::CMP64rr; return X86::CMOVG64rr;
Michael Liaofe87c302012-09-21 03:18:52 +000012690 case X86::ATOMUMAX8: ExtraOpc = X86::CMP8rr; return X86::CMOVB32rr;
Michael Liaob118a072012-09-20 03:06:15 +000012691 case X86::ATOMUMAX16: ExtraOpc = X86::CMP16rr; return X86::CMOVB16rr;
12692 case X86::ATOMUMAX32: ExtraOpc = X86::CMP32rr; return X86::CMOVB32rr;
12693 case X86::ATOMUMAX64: ExtraOpc = X86::CMP64rr; return X86::CMOVB64rr;
Michael Liaofe87c302012-09-21 03:18:52 +000012694 case X86::ATOMUMIN8: ExtraOpc = X86::CMP8rr; return X86::CMOVA32rr;
Michael Liaob118a072012-09-20 03:06:15 +000012695 case X86::ATOMUMIN16: ExtraOpc = X86::CMP16rr; return X86::CMOVA16rr;
12696 case X86::ATOMUMIN32: ExtraOpc = X86::CMP32rr; return X86::CMOVA32rr;
12697 case X86::ATOMUMIN64: ExtraOpc = X86::CMP64rr; return X86::CMOVA64rr;
12698 }
12699 llvm_unreachable("Unhandled atomic-load-op opcode!");
12700}
12701
12702// Get opcode of the non-atomic one from the specified atomic instruction for
12703// 64-bit data type on 32-bit target.
12704static unsigned getNonAtomic6432Opcode(unsigned Opc, unsigned &HiOpc) {
12705 switch (Opc) {
12706 case X86::ATOMAND6432: HiOpc = X86::AND32rr; return X86::AND32rr;
12707 case X86::ATOMOR6432: HiOpc = X86::OR32rr; return X86::OR32rr;
12708 case X86::ATOMXOR6432: HiOpc = X86::XOR32rr; return X86::XOR32rr;
12709 case X86::ATOMADD6432: HiOpc = X86::ADC32rr; return X86::ADD32rr;
12710 case X86::ATOMSUB6432: HiOpc = X86::SBB32rr; return X86::SUB32rr;
12711 case X86::ATOMSWAP6432: HiOpc = X86::MOV32rr; return X86::MOV32rr;
Michael Liaoe5e8f762012-09-25 18:08:13 +000012712 case X86::ATOMMAX6432: HiOpc = X86::SETLr; return X86::SETLr;
12713 case X86::ATOMMIN6432: HiOpc = X86::SETGr; return X86::SETGr;
12714 case X86::ATOMUMAX6432: HiOpc = X86::SETBr; return X86::SETBr;
12715 case X86::ATOMUMIN6432: HiOpc = X86::SETAr; return X86::SETAr;
Michael Liaob118a072012-09-20 03:06:15 +000012716 }
12717 llvm_unreachable("Unhandled atomic-load-op opcode!");
12718}
12719
12720// Get opcode of the non-atomic one from the specified atomic instruction for
12721// 64-bit data type on 32-bit target with extra opcode.
12722static unsigned getNonAtomic6432OpcodeWithExtraOpc(unsigned Opc,
12723 unsigned &HiOpc,
12724 unsigned &ExtraOpc) {
12725 switch (Opc) {
12726 case X86::ATOMNAND6432:
12727 ExtraOpc = X86::NOT32r;
12728 HiOpc = X86::AND32rr;
12729 return X86::AND32rr;
12730 }
12731 llvm_unreachable("Unhandled atomic-load-op opcode!");
12732}
12733
12734// Get pseudo CMOV opcode from the specified data type.
12735static unsigned getPseudoCMOVOpc(EVT VT) {
12736 switch (VT.getSimpleVT().SimpleTy) {
Michael Liaofe87c302012-09-21 03:18:52 +000012737 case MVT::i8: return X86::CMOV_GR8;
Michael Liaob118a072012-09-20 03:06:15 +000012738 case MVT::i16: return X86::CMOV_GR16;
12739 case MVT::i32: return X86::CMOV_GR32;
12740 default:
12741 break;
12742 }
12743 llvm_unreachable("Unknown CMOV opcode!");
12744}
12745
12746// EmitAtomicLoadArith - emit the code sequence for pseudo atomic instructions.
12747// They will be translated into a spin-loop or compare-exchange loop from
12748//
12749// ...
12750// dst = atomic-fetch-op MI.addr, MI.val
12751// ...
12752//
12753// to
12754//
12755// ...
12756// EAX = LOAD MI.addr
12757// loop:
12758// t1 = OP MI.val, EAX
12759// LCMPXCHG [MI.addr], t1, [EAX is implicitly used & defined]
12760// JNE loop
12761// sink:
12762// dst = EAX
12763// ...
Mon P Wang63307c32008-05-05 19:05:59 +000012764MachineBasicBlock *
Michael Liaob118a072012-09-20 03:06:15 +000012765X86TargetLowering::EmitAtomicLoadArith(MachineInstr *MI,
12766 MachineBasicBlock *MBB) const {
12767 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
12768 DebugLoc DL = MI->getDebugLoc();
12769
12770 MachineFunction *MF = MBB->getParent();
12771 MachineRegisterInfo &MRI = MF->getRegInfo();
12772
12773 const BasicBlock *BB = MBB->getBasicBlock();
12774 MachineFunction::iterator I = MBB;
12775 ++I;
12776
12777 assert(MI->getNumOperands() <= X86::AddrNumOperands + 2 &&
12778 "Unexpected number of operands");
12779
12780 assert(MI->hasOneMemOperand() &&
12781 "Expected atomic-load-op to have one memoperand");
12782
12783 // Memory Reference
12784 MachineInstr::mmo_iterator MMOBegin = MI->memoperands_begin();
12785 MachineInstr::mmo_iterator MMOEnd = MI->memoperands_end();
12786
12787 unsigned DstReg, SrcReg;
12788 unsigned MemOpndSlot;
12789
12790 unsigned CurOp = 0;
12791
12792 DstReg = MI->getOperand(CurOp++).getReg();
12793 MemOpndSlot = CurOp;
12794 CurOp += X86::AddrNumOperands;
12795 SrcReg = MI->getOperand(CurOp++).getReg();
12796
12797 const TargetRegisterClass *RC = MRI.getRegClass(DstReg);
Craig Topperf4d25a22012-09-30 19:49:56 +000012798 MVT::SimpleValueType VT = *RC->vt_begin();
Michael Liaob118a072012-09-20 03:06:15 +000012799 unsigned AccPhyReg = getX86SubSuperRegister(X86::EAX, VT);
12800
12801 unsigned LCMPXCHGOpc = getCmpXChgOpcode(VT);
12802 unsigned LOADOpc = getLoadOpcode(VT);
12803
12804 // For the atomic load-arith operator, we generate
12805 //
12806 // thisMBB:
12807 // EAX = LOAD [MI.addr]
12808 // mainMBB:
12809 // t1 = OP MI.val, EAX
12810 // LCMPXCHG [MI.addr], t1, [EAX is implicitly used & defined]
12811 // JNE mainMBB
12812 // sinkMBB:
12813
12814 MachineBasicBlock *thisMBB = MBB;
12815 MachineBasicBlock *mainMBB = MF->CreateMachineBasicBlock(BB);
12816 MachineBasicBlock *sinkMBB = MF->CreateMachineBasicBlock(BB);
12817 MF->insert(I, mainMBB);
12818 MF->insert(I, sinkMBB);
12819
12820 MachineInstrBuilder MIB;
12821
12822 // Transfer the remainder of BB and its successor edges to sinkMBB.
12823 sinkMBB->splice(sinkMBB->begin(), MBB,
12824 llvm::next(MachineBasicBlock::iterator(MI)), MBB->end());
12825 sinkMBB->transferSuccessorsAndUpdatePHIs(MBB);
12826
12827 // thisMBB:
12828 MIB = BuildMI(thisMBB, DL, TII->get(LOADOpc), AccPhyReg);
12829 for (unsigned i = 0; i < X86::AddrNumOperands; ++i)
12830 MIB.addOperand(MI->getOperand(MemOpndSlot + i));
12831 MIB.setMemRefs(MMOBegin, MMOEnd);
12832
12833 thisMBB->addSuccessor(mainMBB);
12834
12835 // mainMBB:
12836 MachineBasicBlock *origMainMBB = mainMBB;
12837 mainMBB->addLiveIn(AccPhyReg);
12838
12839 // Copy AccPhyReg as it is used more than once.
12840 unsigned AccReg = MRI.createVirtualRegister(RC);
12841 BuildMI(mainMBB, DL, TII->get(TargetOpcode::COPY), AccReg)
12842 .addReg(AccPhyReg);
12843
12844 unsigned t1 = MRI.createVirtualRegister(RC);
12845 unsigned Opc = MI->getOpcode();
12846 switch (Opc) {
12847 default:
12848 llvm_unreachable("Unhandled atomic-load-op opcode!");
12849 case X86::ATOMAND8:
12850 case X86::ATOMAND16:
12851 case X86::ATOMAND32:
12852 case X86::ATOMAND64:
12853 case X86::ATOMOR8:
12854 case X86::ATOMOR16:
12855 case X86::ATOMOR32:
12856 case X86::ATOMOR64:
12857 case X86::ATOMXOR8:
12858 case X86::ATOMXOR16:
12859 case X86::ATOMXOR32:
12860 case X86::ATOMXOR64: {
12861 unsigned ARITHOpc = getNonAtomicOpcode(Opc);
12862 BuildMI(mainMBB, DL, TII->get(ARITHOpc), t1).addReg(SrcReg)
12863 .addReg(AccReg);
12864 break;
12865 }
12866 case X86::ATOMNAND8:
12867 case X86::ATOMNAND16:
12868 case X86::ATOMNAND32:
12869 case X86::ATOMNAND64: {
12870 unsigned t2 = MRI.createVirtualRegister(RC);
12871 unsigned NOTOpc;
12872 unsigned ANDOpc = getNonAtomicOpcodeWithExtraOpc(Opc, NOTOpc);
12873 BuildMI(mainMBB, DL, TII->get(ANDOpc), t2).addReg(SrcReg)
12874 .addReg(AccReg);
12875 BuildMI(mainMBB, DL, TII->get(NOTOpc), t1).addReg(t2);
12876 break;
12877 }
Michael Liao08382492012-09-21 03:00:17 +000012878 case X86::ATOMMAX8:
Michael Liaob118a072012-09-20 03:06:15 +000012879 case X86::ATOMMAX16:
12880 case X86::ATOMMAX32:
12881 case X86::ATOMMAX64:
Michael Liaofe87c302012-09-21 03:18:52 +000012882 case X86::ATOMMIN8:
Michael Liaob118a072012-09-20 03:06:15 +000012883 case X86::ATOMMIN16:
12884 case X86::ATOMMIN32:
12885 case X86::ATOMMIN64:
Michael Liaofe87c302012-09-21 03:18:52 +000012886 case X86::ATOMUMAX8:
Michael Liaob118a072012-09-20 03:06:15 +000012887 case X86::ATOMUMAX16:
12888 case X86::ATOMUMAX32:
12889 case X86::ATOMUMAX64:
Michael Liaofe87c302012-09-21 03:18:52 +000012890 case X86::ATOMUMIN8:
Michael Liaob118a072012-09-20 03:06:15 +000012891 case X86::ATOMUMIN16:
12892 case X86::ATOMUMIN32:
12893 case X86::ATOMUMIN64: {
12894 unsigned CMPOpc;
12895 unsigned CMOVOpc = getNonAtomicOpcodeWithExtraOpc(Opc, CMPOpc);
12896
12897 BuildMI(mainMBB, DL, TII->get(CMPOpc))
12898 .addReg(SrcReg)
12899 .addReg(AccReg);
12900
12901 if (Subtarget->hasCMov()) {
Michael Liaofe87c302012-09-21 03:18:52 +000012902 if (VT != MVT::i8) {
12903 // Native support
12904 BuildMI(mainMBB, DL, TII->get(CMOVOpc), t1)
12905 .addReg(SrcReg)
12906 .addReg(AccReg);
12907 } else {
12908 // Promote i8 to i32 to use CMOV32
12909 const TargetRegisterClass *RC32 = getRegClassFor(MVT::i32);
12910 unsigned SrcReg32 = MRI.createVirtualRegister(RC32);
12911 unsigned AccReg32 = MRI.createVirtualRegister(RC32);
12912 unsigned t2 = MRI.createVirtualRegister(RC32);
12913
12914 unsigned Undef = MRI.createVirtualRegister(RC32);
12915 BuildMI(mainMBB, DL, TII->get(TargetOpcode::IMPLICIT_DEF), Undef);
12916
12917 BuildMI(mainMBB, DL, TII->get(TargetOpcode::INSERT_SUBREG), SrcReg32)
12918 .addReg(Undef)
12919 .addReg(SrcReg)
12920 .addImm(X86::sub_8bit);
12921 BuildMI(mainMBB, DL, TII->get(TargetOpcode::INSERT_SUBREG), AccReg32)
12922 .addReg(Undef)
12923 .addReg(AccReg)
12924 .addImm(X86::sub_8bit);
12925
12926 BuildMI(mainMBB, DL, TII->get(CMOVOpc), t2)
12927 .addReg(SrcReg32)
12928 .addReg(AccReg32);
12929
12930 BuildMI(mainMBB, DL, TII->get(TargetOpcode::COPY), t1)
12931 .addReg(t2, 0, X86::sub_8bit);
12932 }
Michael Liaob118a072012-09-20 03:06:15 +000012933 } else {
12934 // Use pseudo select and lower them.
Michael Liaofe87c302012-09-21 03:18:52 +000012935 assert((VT == MVT::i8 || VT == MVT::i16 || VT == MVT::i32) &&
Michael Liaob118a072012-09-20 03:06:15 +000012936 "Invalid atomic-load-op transformation!");
12937 unsigned SelOpc = getPseudoCMOVOpc(VT);
12938 X86::CondCode CC = X86::getCondFromCMovOpc(CMOVOpc);
12939 assert(CC != X86::COND_INVALID && "Invalid atomic-load-op transformation!");
12940 MIB = BuildMI(mainMBB, DL, TII->get(SelOpc), t1)
12941 .addReg(SrcReg).addReg(AccReg)
12942 .addImm(CC);
12943 mainMBB = EmitLoweredSelect(MIB, mainMBB);
12944 }
12945 break;
12946 }
12947 }
12948
12949 // Copy AccPhyReg back from virtual register.
12950 BuildMI(mainMBB, DL, TII->get(TargetOpcode::COPY), AccPhyReg)
12951 .addReg(AccReg);
12952
12953 MIB = BuildMI(mainMBB, DL, TII->get(LCMPXCHGOpc));
12954 for (unsigned i = 0; i < X86::AddrNumOperands; ++i)
12955 MIB.addOperand(MI->getOperand(MemOpndSlot + i));
12956 MIB.addReg(t1);
12957 MIB.setMemRefs(MMOBegin, MMOEnd);
12958
12959 BuildMI(mainMBB, DL, TII->get(X86::JNE_4)).addMBB(origMainMBB);
12960
12961 mainMBB->addSuccessor(origMainMBB);
12962 mainMBB->addSuccessor(sinkMBB);
12963
12964 // sinkMBB:
12965 sinkMBB->addLiveIn(AccPhyReg);
12966
12967 BuildMI(*sinkMBB, sinkMBB->begin(), DL,
12968 TII->get(TargetOpcode::COPY), DstReg)
12969 .addReg(AccPhyReg);
12970
12971 MI->eraseFromParent();
12972 return sinkMBB;
12973}
12974
12975// EmitAtomicLoadArith6432 - emit the code sequence for pseudo atomic
12976// instructions. They will be translated into a spin-loop or compare-exchange
12977// loop from
12978//
12979// ...
12980// dst = atomic-fetch-op MI.addr, MI.val
12981// ...
12982//
12983// to
12984//
12985// ...
12986// EAX = LOAD [MI.addr + 0]
12987// EDX = LOAD [MI.addr + 4]
12988// loop:
12989// EBX = OP MI.val.lo, EAX
12990// ECX = OP MI.val.hi, EDX
12991// LCMPXCHG8B [MI.addr], [ECX:EBX & EDX:EAX are implicitly used and EDX:EAX is implicitly defined]
12992// JNE loop
12993// sink:
12994// dst = EDX:EAX
12995// ...
12996MachineBasicBlock *
12997X86TargetLowering::EmitAtomicLoadArith6432(MachineInstr *MI,
12998 MachineBasicBlock *MBB) const {
12999 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
13000 DebugLoc DL = MI->getDebugLoc();
13001
13002 MachineFunction *MF = MBB->getParent();
13003 MachineRegisterInfo &MRI = MF->getRegInfo();
13004
13005 const BasicBlock *BB = MBB->getBasicBlock();
13006 MachineFunction::iterator I = MBB;
13007 ++I;
13008
13009 assert(MI->getNumOperands() <= X86::AddrNumOperands + 4 &&
13010 "Unexpected number of operands");
13011
13012 assert(MI->hasOneMemOperand() &&
13013 "Expected atomic-load-op32 to have one memoperand");
13014
13015 // Memory Reference
13016 MachineInstr::mmo_iterator MMOBegin = MI->memoperands_begin();
13017 MachineInstr::mmo_iterator MMOEnd = MI->memoperands_end();
13018
13019 unsigned DstLoReg, DstHiReg;
13020 unsigned SrcLoReg, SrcHiReg;
13021 unsigned MemOpndSlot;
13022
13023 unsigned CurOp = 0;
13024
13025 DstLoReg = MI->getOperand(CurOp++).getReg();
13026 DstHiReg = MI->getOperand(CurOp++).getReg();
13027 MemOpndSlot = CurOp;
13028 CurOp += X86::AddrNumOperands;
13029 SrcLoReg = MI->getOperand(CurOp++).getReg();
13030 SrcHiReg = MI->getOperand(CurOp++).getReg();
Dale Johannesen48c1bc22008-10-02 18:53:47 +000013031
Craig Topperc9099502012-04-20 06:31:50 +000013032 const TargetRegisterClass *RC = &X86::GR32RegClass;
Michael Liaoe5e8f762012-09-25 18:08:13 +000013033 const TargetRegisterClass *RC8 = &X86::GR8RegClass;
Scott Michelfdc40a02009-02-17 22:15:04 +000013034
Michael Liaob118a072012-09-20 03:06:15 +000013035 unsigned LCMPXCHGOpc = X86::LCMPXCHG8B;
13036 unsigned LOADOpc = X86::MOV32rm;
Scott Michelfdc40a02009-02-17 22:15:04 +000013037
Michael Liaob118a072012-09-20 03:06:15 +000013038 // For the atomic load-arith operator, we generate
Mon P Wang63307c32008-05-05 19:05:59 +000013039 //
Michael Liaob118a072012-09-20 03:06:15 +000013040 // thisMBB:
13041 // EAX = LOAD [MI.addr + 0]
13042 // EDX = LOAD [MI.addr + 4]
13043 // mainMBB:
13044 // EBX = OP MI.vallo, EAX
13045 // ECX = OP MI.valhi, EDX
13046 // LCMPXCHG8B [MI.addr], [ECX:EBX & EDX:EAX are implicitly used and EDX:EAX is implicitly defined]
13047 // JNE mainMBB
13048 // sinkMBB:
Scott Michelfdc40a02009-02-17 22:15:04 +000013049
Mon P Wang63307c32008-05-05 19:05:59 +000013050 MachineBasicBlock *thisMBB = MBB;
Michael Liaob118a072012-09-20 03:06:15 +000013051 MachineBasicBlock *mainMBB = MF->CreateMachineBasicBlock(BB);
13052 MachineBasicBlock *sinkMBB = MF->CreateMachineBasicBlock(BB);
13053 MF->insert(I, mainMBB);
13054 MF->insert(I, sinkMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000013055
Michael Liaob118a072012-09-20 03:06:15 +000013056 MachineInstrBuilder MIB;
Scott Michelfdc40a02009-02-17 22:15:04 +000013057
Michael Liaob118a072012-09-20 03:06:15 +000013058 // Transfer the remainder of BB and its successor edges to sinkMBB.
13059 sinkMBB->splice(sinkMBB->begin(), MBB,
13060 llvm::next(MachineBasicBlock::iterator(MI)), MBB->end());
13061 sinkMBB->transferSuccessorsAndUpdatePHIs(MBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000013062
Michael Liaob118a072012-09-20 03:06:15 +000013063 // thisMBB:
13064 // Lo
13065 MIB = BuildMI(thisMBB, DL, TII->get(LOADOpc), X86::EAX);
13066 for (unsigned i = 0; i < X86::AddrNumOperands; ++i)
13067 MIB.addOperand(MI->getOperand(MemOpndSlot + i));
13068 MIB.setMemRefs(MMOBegin, MMOEnd);
13069 // Hi
13070 MIB = BuildMI(thisMBB, DL, TII->get(LOADOpc), X86::EDX);
13071 for (unsigned i = 0; i < X86::AddrNumOperands; ++i) {
Evan Chenga395f4d2012-10-11 00:15:48 +000013072 if (i == X86::AddrDisp)
Michael Liaob118a072012-09-20 03:06:15 +000013073 MIB.addDisp(MI->getOperand(MemOpndSlot + i), 4); // 4 == sizeof(i32)
Evan Chenga395f4d2012-10-11 00:15:48 +000013074 else
Michael Liaob118a072012-09-20 03:06:15 +000013075 MIB.addOperand(MI->getOperand(MemOpndSlot + i));
13076 }
13077 MIB.setMemRefs(MMOBegin, MMOEnd);
Scott Michelfdc40a02009-02-17 22:15:04 +000013078
Michael Liaob118a072012-09-20 03:06:15 +000013079 thisMBB->addSuccessor(mainMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000013080
Michael Liaob118a072012-09-20 03:06:15 +000013081 // mainMBB:
13082 MachineBasicBlock *origMainMBB = mainMBB;
13083 mainMBB->addLiveIn(X86::EAX);
13084 mainMBB->addLiveIn(X86::EDX);
Scott Michelfdc40a02009-02-17 22:15:04 +000013085
Michael Liaob118a072012-09-20 03:06:15 +000013086 // Copy EDX:EAX as they are used more than once.
13087 unsigned LoReg = MRI.createVirtualRegister(RC);
13088 unsigned HiReg = MRI.createVirtualRegister(RC);
13089 BuildMI(mainMBB, DL, TII->get(TargetOpcode::COPY), LoReg).addReg(X86::EAX);
13090 BuildMI(mainMBB, DL, TII->get(TargetOpcode::COPY), HiReg).addReg(X86::EDX);
Mon P Wangab3e7472008-05-05 22:56:23 +000013091
Michael Liaob118a072012-09-20 03:06:15 +000013092 unsigned t1L = MRI.createVirtualRegister(RC);
13093 unsigned t1H = MRI.createVirtualRegister(RC);
Scott Michelfdc40a02009-02-17 22:15:04 +000013094
Michael Liaob118a072012-09-20 03:06:15 +000013095 unsigned Opc = MI->getOpcode();
13096 switch (Opc) {
13097 default:
13098 llvm_unreachable("Unhandled atomic-load-op6432 opcode!");
13099 case X86::ATOMAND6432:
13100 case X86::ATOMOR6432:
13101 case X86::ATOMXOR6432:
13102 case X86::ATOMADD6432:
13103 case X86::ATOMSUB6432: {
13104 unsigned HiOpc;
13105 unsigned LoOpc = getNonAtomic6432Opcode(Opc, HiOpc);
Michael Liaodd3383f2012-11-12 06:49:17 +000013106 BuildMI(mainMBB, DL, TII->get(LoOpc), t1L).addReg(LoReg).addReg(SrcLoReg);
13107 BuildMI(mainMBB, DL, TII->get(HiOpc), t1H).addReg(HiReg).addReg(SrcHiReg);
Michael Liaob118a072012-09-20 03:06:15 +000013108 break;
13109 }
13110 case X86::ATOMNAND6432: {
13111 unsigned HiOpc, NOTOpc;
13112 unsigned LoOpc = getNonAtomic6432OpcodeWithExtraOpc(Opc, HiOpc, NOTOpc);
13113 unsigned t2L = MRI.createVirtualRegister(RC);
13114 unsigned t2H = MRI.createVirtualRegister(RC);
13115 BuildMI(mainMBB, DL, TII->get(LoOpc), t2L).addReg(SrcLoReg).addReg(LoReg);
13116 BuildMI(mainMBB, DL, TII->get(HiOpc), t2H).addReg(SrcHiReg).addReg(HiReg);
13117 BuildMI(mainMBB, DL, TII->get(NOTOpc), t1L).addReg(t2L);
13118 BuildMI(mainMBB, DL, TII->get(NOTOpc), t1H).addReg(t2H);
13119 break;
13120 }
Michael Liaoe5e8f762012-09-25 18:08:13 +000013121 case X86::ATOMMAX6432:
13122 case X86::ATOMMIN6432:
13123 case X86::ATOMUMAX6432:
13124 case X86::ATOMUMIN6432: {
13125 unsigned HiOpc;
13126 unsigned LoOpc = getNonAtomic6432Opcode(Opc, HiOpc);
13127 unsigned cL = MRI.createVirtualRegister(RC8);
13128 unsigned cH = MRI.createVirtualRegister(RC8);
13129 unsigned cL32 = MRI.createVirtualRegister(RC);
13130 unsigned cH32 = MRI.createVirtualRegister(RC);
13131 unsigned cc = MRI.createVirtualRegister(RC);
13132 // cl := cmp src_lo, lo
13133 BuildMI(mainMBB, DL, TII->get(X86::CMP32rr))
13134 .addReg(SrcLoReg).addReg(LoReg);
13135 BuildMI(mainMBB, DL, TII->get(LoOpc), cL);
13136 BuildMI(mainMBB, DL, TII->get(X86::MOVZX32rr8), cL32).addReg(cL);
13137 // ch := cmp src_hi, hi
13138 BuildMI(mainMBB, DL, TII->get(X86::CMP32rr))
13139 .addReg(SrcHiReg).addReg(HiReg);
13140 BuildMI(mainMBB, DL, TII->get(HiOpc), cH);
13141 BuildMI(mainMBB, DL, TII->get(X86::MOVZX32rr8), cH32).addReg(cH);
13142 // cc := if (src_hi == hi) ? cl : ch;
13143 if (Subtarget->hasCMov()) {
13144 BuildMI(mainMBB, DL, TII->get(X86::CMOVE32rr), cc)
13145 .addReg(cH32).addReg(cL32);
13146 } else {
13147 MIB = BuildMI(mainMBB, DL, TII->get(X86::CMOV_GR32), cc)
13148 .addReg(cH32).addReg(cL32)
13149 .addImm(X86::COND_E);
13150 mainMBB = EmitLoweredSelect(MIB, mainMBB);
13151 }
13152 BuildMI(mainMBB, DL, TII->get(X86::TEST32rr)).addReg(cc).addReg(cc);
13153 if (Subtarget->hasCMov()) {
13154 BuildMI(mainMBB, DL, TII->get(X86::CMOVNE32rr), t1L)
13155 .addReg(SrcLoReg).addReg(LoReg);
13156 BuildMI(mainMBB, DL, TII->get(X86::CMOVNE32rr), t1H)
13157 .addReg(SrcHiReg).addReg(HiReg);
13158 } else {
13159 MIB = BuildMI(mainMBB, DL, TII->get(X86::CMOV_GR32), t1L)
13160 .addReg(SrcLoReg).addReg(LoReg)
13161 .addImm(X86::COND_NE);
13162 mainMBB = EmitLoweredSelect(MIB, mainMBB);
13163 MIB = BuildMI(mainMBB, DL, TII->get(X86::CMOV_GR32), t1H)
13164 .addReg(SrcHiReg).addReg(HiReg)
13165 .addImm(X86::COND_NE);
13166 mainMBB = EmitLoweredSelect(MIB, mainMBB);
13167 }
13168 break;
13169 }
Michael Liaob118a072012-09-20 03:06:15 +000013170 case X86::ATOMSWAP6432: {
13171 unsigned HiOpc;
13172 unsigned LoOpc = getNonAtomic6432Opcode(Opc, HiOpc);
13173 BuildMI(mainMBB, DL, TII->get(LoOpc), t1L).addReg(SrcLoReg);
13174 BuildMI(mainMBB, DL, TII->get(HiOpc), t1H).addReg(SrcHiReg);
13175 break;
13176 }
13177 }
Mon P Wang63307c32008-05-05 19:05:59 +000013178
Michael Liaob118a072012-09-20 03:06:15 +000013179 // Copy EDX:EAX back from HiReg:LoReg
13180 BuildMI(mainMBB, DL, TII->get(TargetOpcode::COPY), X86::EAX).addReg(LoReg);
13181 BuildMI(mainMBB, DL, TII->get(TargetOpcode::COPY), X86::EDX).addReg(HiReg);
13182 // Copy ECX:EBX from t1H:t1L
13183 BuildMI(mainMBB, DL, TII->get(TargetOpcode::COPY), X86::EBX).addReg(t1L);
13184 BuildMI(mainMBB, DL, TII->get(TargetOpcode::COPY), X86::ECX).addReg(t1H);
Mon P Wangab3e7472008-05-05 22:56:23 +000013185
Michael Liaob118a072012-09-20 03:06:15 +000013186 MIB = BuildMI(mainMBB, DL, TII->get(LCMPXCHGOpc));
13187 for (unsigned i = 0; i < X86::AddrNumOperands; ++i)
13188 MIB.addOperand(MI->getOperand(MemOpndSlot + i));
13189 MIB.setMemRefs(MMOBegin, MMOEnd);
Mon P Wang63307c32008-05-05 19:05:59 +000013190
Michael Liaob118a072012-09-20 03:06:15 +000013191 BuildMI(mainMBB, DL, TII->get(X86::JNE_4)).addMBB(origMainMBB);
Mon P Wang63307c32008-05-05 19:05:59 +000013192
Michael Liaob118a072012-09-20 03:06:15 +000013193 mainMBB->addSuccessor(origMainMBB);
13194 mainMBB->addSuccessor(sinkMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000013195
Michael Liaob118a072012-09-20 03:06:15 +000013196 // sinkMBB:
13197 sinkMBB->addLiveIn(X86::EAX);
13198 sinkMBB->addLiveIn(X86::EDX);
Scott Michelfdc40a02009-02-17 22:15:04 +000013199
Michael Liaob118a072012-09-20 03:06:15 +000013200 BuildMI(*sinkMBB, sinkMBB->begin(), DL,
13201 TII->get(TargetOpcode::COPY), DstLoReg)
13202 .addReg(X86::EAX);
13203 BuildMI(*sinkMBB, sinkMBB->begin(), DL,
13204 TII->get(TargetOpcode::COPY), DstHiReg)
13205 .addReg(X86::EDX);
Mon P Wang63307c32008-05-05 19:05:59 +000013206
Michael Liaob118a072012-09-20 03:06:15 +000013207 MI->eraseFromParent();
13208 return sinkMBB;
Mon P Wang63307c32008-05-05 19:05:59 +000013209}
13210
Eric Christopherf83a5de2009-08-27 18:08:16 +000013211// FIXME: When we get size specific XMM0 registers, i.e. XMM0_V16I8
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000013212// or XMM0_V32I8 in AVX all of this code can be replaced with that
13213// in the .td file.
Craig Topper8cb8c812012-11-10 09:02:47 +000013214static MachineBasicBlock *EmitPCMPSTRM(MachineInstr *MI, MachineBasicBlock *BB,
13215 const TargetInstrInfo *TII) {
Eric Christopherb120ab42009-08-18 22:50:32 +000013216 unsigned Opc;
Craig Topper8aae8dd2012-11-10 08:57:41 +000013217 switch (MI->getOpcode()) {
13218 default: llvm_unreachable("illegal opcode!");
13219 case X86::PCMPISTRM128REG: Opc = X86::PCMPISTRM128rr; break;
13220 case X86::VPCMPISTRM128REG: Opc = X86::VPCMPISTRM128rr; break;
13221 case X86::PCMPISTRM128MEM: Opc = X86::PCMPISTRM128rm; break;
13222 case X86::VPCMPISTRM128MEM: Opc = X86::VPCMPISTRM128rm; break;
13223 case X86::PCMPESTRM128REG: Opc = X86::PCMPESTRM128rr; break;
13224 case X86::VPCMPESTRM128REG: Opc = X86::VPCMPESTRM128rr; break;
13225 case X86::PCMPESTRM128MEM: Opc = X86::PCMPESTRM128rm; break;
13226 case X86::VPCMPESTRM128MEM: Opc = X86::VPCMPESTRM128rm; break;
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000013227 }
Eric Christopherb120ab42009-08-18 22:50:32 +000013228
Craig Topper8aae8dd2012-11-10 08:57:41 +000013229 DebugLoc dl = MI->getDebugLoc();
Eric Christopher41c902f2010-11-30 08:20:21 +000013230 MachineInstrBuilder MIB = BuildMI(*BB, MI, dl, TII->get(Opc));
Craig Topper8aae8dd2012-11-10 08:57:41 +000013231
Craig Topper52ea2452012-11-10 09:25:36 +000013232 unsigned NumArgs = MI->getNumOperands();
13233 for (unsigned i = 1; i < NumArgs; ++i) {
13234 MachineOperand &Op = MI->getOperand(i);
Eric Christopherb120ab42009-08-18 22:50:32 +000013235 if (!(Op.isReg() && Op.isImplicit()))
13236 MIB.addOperand(Op);
13237 }
Craig Topper8aae8dd2012-11-10 08:57:41 +000013238 if (MI->hasOneMemOperand())
Craig Topper9c7ae012012-11-10 01:23:36 +000013239 MIB->setMemRefs(MI->memoperands_begin(), MI->memoperands_end());
13240
Bruno Cardoso Lopes5affa512011-08-31 03:04:09 +000013241 BuildMI(*BB, MI, dl,
Craig Topper638aa682012-08-05 00:17:48 +000013242 TII->get(TargetOpcode::COPY), MI->getOperand(0).getReg())
Eric Christopherb120ab42009-08-18 22:50:32 +000013243 .addReg(X86::XMM0);
13244
Dan Gohman14152b42010-07-06 20:24:04 +000013245 MI->eraseFromParent();
Eric Christopherb120ab42009-08-18 22:50:32 +000013246 return BB;
13247}
13248
Craig Topper9c7ae012012-11-10 01:23:36 +000013249// FIXME: Custom handling because TableGen doesn't support multiple implicit
13250// defs in an instruction pattern
Craig Topper8cb8c812012-11-10 09:02:47 +000013251static MachineBasicBlock *EmitPCMPSTRI(MachineInstr *MI, MachineBasicBlock *BB,
13252 const TargetInstrInfo *TII) {
Craig Topper9c7ae012012-11-10 01:23:36 +000013253 unsigned Opc;
Craig Topper8aae8dd2012-11-10 08:57:41 +000013254 switch (MI->getOpcode()) {
13255 default: llvm_unreachable("illegal opcode!");
13256 case X86::PCMPISTRIREG: Opc = X86::PCMPISTRIrr; break;
13257 case X86::VPCMPISTRIREG: Opc = X86::VPCMPISTRIrr; break;
13258 case X86::PCMPISTRIMEM: Opc = X86::PCMPISTRIrm; break;
13259 case X86::VPCMPISTRIMEM: Opc = X86::VPCMPISTRIrm; break;
13260 case X86::PCMPESTRIREG: Opc = X86::PCMPESTRIrr; break;
13261 case X86::VPCMPESTRIREG: Opc = X86::VPCMPESTRIrr; break;
13262 case X86::PCMPESTRIMEM: Opc = X86::PCMPESTRIrm; break;
13263 case X86::VPCMPESTRIMEM: Opc = X86::VPCMPESTRIrm; break;
Craig Topper9c7ae012012-11-10 01:23:36 +000013264 }
13265
Craig Topper8aae8dd2012-11-10 08:57:41 +000013266 DebugLoc dl = MI->getDebugLoc();
Craig Topper9c7ae012012-11-10 01:23:36 +000013267 MachineInstrBuilder MIB = BuildMI(*BB, MI, dl, TII->get(Opc));
Craig Topper8aae8dd2012-11-10 08:57:41 +000013268
Craig Topper52ea2452012-11-10 09:25:36 +000013269 unsigned NumArgs = MI->getNumOperands(); // remove the results
13270 for (unsigned i = 1; i < NumArgs; ++i) {
13271 MachineOperand &Op = MI->getOperand(i);
Craig Topper9c7ae012012-11-10 01:23:36 +000013272 if (!(Op.isReg() && Op.isImplicit()))
13273 MIB.addOperand(Op);
13274 }
Craig Topper8aae8dd2012-11-10 08:57:41 +000013275 if (MI->hasOneMemOperand())
Craig Topper9c7ae012012-11-10 01:23:36 +000013276 MIB->setMemRefs(MI->memoperands_begin(), MI->memoperands_end());
13277
13278 BuildMI(*BB, MI, dl,
13279 TII->get(TargetOpcode::COPY), MI->getOperand(0).getReg())
13280 .addReg(X86::ECX);
13281
13282 MI->eraseFromParent();
13283 return BB;
13284}
13285
Craig Topper2da36912012-11-11 22:45:02 +000013286static MachineBasicBlock * EmitMonitor(MachineInstr *MI, MachineBasicBlock *BB,
13287 const TargetInstrInfo *TII,
13288 const X86Subtarget* Subtarget) {
Eric Christopher228232b2010-11-30 07:20:12 +000013289 DebugLoc dl = MI->getDebugLoc();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013290
Eric Christopher228232b2010-11-30 07:20:12 +000013291 // Address into RAX/EAX, other two args into ECX, EDX.
13292 unsigned MemOpc = Subtarget->is64Bit() ? X86::LEA64r : X86::LEA32r;
13293 unsigned MemReg = Subtarget->is64Bit() ? X86::RAX : X86::EAX;
13294 MachineInstrBuilder MIB = BuildMI(*BB, MI, dl, TII->get(MemOpc), MemReg);
13295 for (int i = 0; i < X86::AddrNumOperands; ++i)
Eric Christopher82be2202010-11-30 08:10:28 +000013296 MIB.addOperand(MI->getOperand(i));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013297
Eric Christopher228232b2010-11-30 07:20:12 +000013298 unsigned ValOps = X86::AddrNumOperands;
13299 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::ECX)
13300 .addReg(MI->getOperand(ValOps).getReg());
13301 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::EDX)
13302 .addReg(MI->getOperand(ValOps+1).getReg());
13303
13304 // The instruction doesn't actually take any operands though.
13305 BuildMI(*BB, MI, dl, TII->get(X86::MONITORrrr));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013306
Eric Christopher228232b2010-11-30 07:20:12 +000013307 MI->eraseFromParent(); // The pseudo is gone now.
13308 return BB;
13309}
13310
13311MachineBasicBlock *
Dan Gohman320afb82010-10-12 18:00:49 +000013312X86TargetLowering::EmitVAARG64WithCustomInserter(
13313 MachineInstr *MI,
13314 MachineBasicBlock *MBB) const {
13315 // Emit va_arg instruction on X86-64.
13316
13317 // Operands to this pseudo-instruction:
13318 // 0 ) Output : destination address (reg)
13319 // 1-5) Input : va_list address (addr, i64mem)
13320 // 6 ) ArgSize : Size (in bytes) of vararg type
13321 // 7 ) ArgMode : 0=overflow only, 1=use gp_offset, 2=use fp_offset
13322 // 8 ) Align : Alignment of type
13323 // 9 ) EFLAGS (implicit-def)
13324
13325 assert(MI->getNumOperands() == 10 && "VAARG_64 should have 10 operands!");
13326 assert(X86::AddrNumOperands == 5 && "VAARG_64 assumes 5 address operands");
13327
13328 unsigned DestReg = MI->getOperand(0).getReg();
13329 MachineOperand &Base = MI->getOperand(1);
13330 MachineOperand &Scale = MI->getOperand(2);
13331 MachineOperand &Index = MI->getOperand(3);
13332 MachineOperand &Disp = MI->getOperand(4);
13333 MachineOperand &Segment = MI->getOperand(5);
13334 unsigned ArgSize = MI->getOperand(6).getImm();
13335 unsigned ArgMode = MI->getOperand(7).getImm();
13336 unsigned Align = MI->getOperand(8).getImm();
13337
13338 // Memory Reference
13339 assert(MI->hasOneMemOperand() && "Expected VAARG_64 to have one memoperand");
13340 MachineInstr::mmo_iterator MMOBegin = MI->memoperands_begin();
13341 MachineInstr::mmo_iterator MMOEnd = MI->memoperands_end();
13342
13343 // Machine Information
13344 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
13345 MachineRegisterInfo &MRI = MBB->getParent()->getRegInfo();
13346 const TargetRegisterClass *AddrRegClass = getRegClassFor(MVT::i64);
13347 const TargetRegisterClass *OffsetRegClass = getRegClassFor(MVT::i32);
13348 DebugLoc DL = MI->getDebugLoc();
13349
13350 // struct va_list {
13351 // i32 gp_offset
13352 // i32 fp_offset
13353 // i64 overflow_area (address)
13354 // i64 reg_save_area (address)
13355 // }
13356 // sizeof(va_list) = 24
13357 // alignment(va_list) = 8
13358
13359 unsigned TotalNumIntRegs = 6;
13360 unsigned TotalNumXMMRegs = 8;
13361 bool UseGPOffset = (ArgMode == 1);
13362 bool UseFPOffset = (ArgMode == 2);
13363 unsigned MaxOffset = TotalNumIntRegs * 8 +
13364 (UseFPOffset ? TotalNumXMMRegs * 16 : 0);
13365
13366 /* Align ArgSize to a multiple of 8 */
13367 unsigned ArgSizeA8 = (ArgSize + 7) & ~7;
13368 bool NeedsAlign = (Align > 8);
13369
13370 MachineBasicBlock *thisMBB = MBB;
13371 MachineBasicBlock *overflowMBB;
13372 MachineBasicBlock *offsetMBB;
13373 MachineBasicBlock *endMBB;
13374
13375 unsigned OffsetDestReg = 0; // Argument address computed by offsetMBB
13376 unsigned OverflowDestReg = 0; // Argument address computed by overflowMBB
13377 unsigned OffsetReg = 0;
13378
13379 if (!UseGPOffset && !UseFPOffset) {
13380 // If we only pull from the overflow region, we don't create a branch.
13381 // We don't need to alter control flow.
13382 OffsetDestReg = 0; // unused
13383 OverflowDestReg = DestReg;
13384
13385 offsetMBB = NULL;
13386 overflowMBB = thisMBB;
13387 endMBB = thisMBB;
13388 } else {
13389 // First emit code to check if gp_offset (or fp_offset) is below the bound.
13390 // If so, pull the argument from reg_save_area. (branch to offsetMBB)
13391 // If not, pull from overflow_area. (branch to overflowMBB)
13392 //
13393 // thisMBB
13394 // | .
13395 // | .
13396 // offsetMBB overflowMBB
13397 // | .
13398 // | .
13399 // endMBB
13400
13401 // Registers for the PHI in endMBB
13402 OffsetDestReg = MRI.createVirtualRegister(AddrRegClass);
13403 OverflowDestReg = MRI.createVirtualRegister(AddrRegClass);
13404
13405 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
13406 MachineFunction *MF = MBB->getParent();
13407 overflowMBB = MF->CreateMachineBasicBlock(LLVM_BB);
13408 offsetMBB = MF->CreateMachineBasicBlock(LLVM_BB);
13409 endMBB = MF->CreateMachineBasicBlock(LLVM_BB);
13410
13411 MachineFunction::iterator MBBIter = MBB;
13412 ++MBBIter;
13413
13414 // Insert the new basic blocks
13415 MF->insert(MBBIter, offsetMBB);
13416 MF->insert(MBBIter, overflowMBB);
13417 MF->insert(MBBIter, endMBB);
13418
13419 // Transfer the remainder of MBB and its successor edges to endMBB.
13420 endMBB->splice(endMBB->begin(), thisMBB,
13421 llvm::next(MachineBasicBlock::iterator(MI)),
13422 thisMBB->end());
13423 endMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
13424
13425 // Make offsetMBB and overflowMBB successors of thisMBB
13426 thisMBB->addSuccessor(offsetMBB);
13427 thisMBB->addSuccessor(overflowMBB);
13428
13429 // endMBB is a successor of both offsetMBB and overflowMBB
13430 offsetMBB->addSuccessor(endMBB);
13431 overflowMBB->addSuccessor(endMBB);
13432
13433 // Load the offset value into a register
13434 OffsetReg = MRI.createVirtualRegister(OffsetRegClass);
13435 BuildMI(thisMBB, DL, TII->get(X86::MOV32rm), OffsetReg)
13436 .addOperand(Base)
13437 .addOperand(Scale)
13438 .addOperand(Index)
13439 .addDisp(Disp, UseFPOffset ? 4 : 0)
13440 .addOperand(Segment)
13441 .setMemRefs(MMOBegin, MMOEnd);
13442
13443 // Check if there is enough room left to pull this argument.
13444 BuildMI(thisMBB, DL, TII->get(X86::CMP32ri))
13445 .addReg(OffsetReg)
13446 .addImm(MaxOffset + 8 - ArgSizeA8);
13447
13448 // Branch to "overflowMBB" if offset >= max
13449 // Fall through to "offsetMBB" otherwise
13450 BuildMI(thisMBB, DL, TII->get(X86::GetCondBranchFromCond(X86::COND_AE)))
13451 .addMBB(overflowMBB);
13452 }
13453
13454 // In offsetMBB, emit code to use the reg_save_area.
13455 if (offsetMBB) {
13456 assert(OffsetReg != 0);
13457
13458 // Read the reg_save_area address.
13459 unsigned RegSaveReg = MRI.createVirtualRegister(AddrRegClass);
13460 BuildMI(offsetMBB, DL, TII->get(X86::MOV64rm), RegSaveReg)
13461 .addOperand(Base)
13462 .addOperand(Scale)
13463 .addOperand(Index)
13464 .addDisp(Disp, 16)
13465 .addOperand(Segment)
13466 .setMemRefs(MMOBegin, MMOEnd);
13467
13468 // Zero-extend the offset
13469 unsigned OffsetReg64 = MRI.createVirtualRegister(AddrRegClass);
13470 BuildMI(offsetMBB, DL, TII->get(X86::SUBREG_TO_REG), OffsetReg64)
13471 .addImm(0)
13472 .addReg(OffsetReg)
13473 .addImm(X86::sub_32bit);
13474
13475 // Add the offset to the reg_save_area to get the final address.
13476 BuildMI(offsetMBB, DL, TII->get(X86::ADD64rr), OffsetDestReg)
13477 .addReg(OffsetReg64)
13478 .addReg(RegSaveReg);
13479
13480 // Compute the offset for the next argument
13481 unsigned NextOffsetReg = MRI.createVirtualRegister(OffsetRegClass);
13482 BuildMI(offsetMBB, DL, TII->get(X86::ADD32ri), NextOffsetReg)
13483 .addReg(OffsetReg)
13484 .addImm(UseFPOffset ? 16 : 8);
13485
13486 // Store it back into the va_list.
13487 BuildMI(offsetMBB, DL, TII->get(X86::MOV32mr))
13488 .addOperand(Base)
13489 .addOperand(Scale)
13490 .addOperand(Index)
13491 .addDisp(Disp, UseFPOffset ? 4 : 0)
13492 .addOperand(Segment)
13493 .addReg(NextOffsetReg)
13494 .setMemRefs(MMOBegin, MMOEnd);
13495
13496 // Jump to endMBB
13497 BuildMI(offsetMBB, DL, TII->get(X86::JMP_4))
13498 .addMBB(endMBB);
13499 }
13500
13501 //
13502 // Emit code to use overflow area
13503 //
13504
13505 // Load the overflow_area address into a register.
13506 unsigned OverflowAddrReg = MRI.createVirtualRegister(AddrRegClass);
13507 BuildMI(overflowMBB, DL, TII->get(X86::MOV64rm), OverflowAddrReg)
13508 .addOperand(Base)
13509 .addOperand(Scale)
13510 .addOperand(Index)
13511 .addDisp(Disp, 8)
13512 .addOperand(Segment)
13513 .setMemRefs(MMOBegin, MMOEnd);
13514
13515 // If we need to align it, do so. Otherwise, just copy the address
13516 // to OverflowDestReg.
13517 if (NeedsAlign) {
13518 // Align the overflow address
13519 assert((Align & (Align-1)) == 0 && "Alignment must be a power of 2");
13520 unsigned TmpReg = MRI.createVirtualRegister(AddrRegClass);
13521
13522 // aligned_addr = (addr + (align-1)) & ~(align-1)
13523 BuildMI(overflowMBB, DL, TII->get(X86::ADD64ri32), TmpReg)
13524 .addReg(OverflowAddrReg)
13525 .addImm(Align-1);
13526
13527 BuildMI(overflowMBB, DL, TII->get(X86::AND64ri32), OverflowDestReg)
13528 .addReg(TmpReg)
13529 .addImm(~(uint64_t)(Align-1));
13530 } else {
13531 BuildMI(overflowMBB, DL, TII->get(TargetOpcode::COPY), OverflowDestReg)
13532 .addReg(OverflowAddrReg);
13533 }
13534
13535 // Compute the next overflow address after this argument.
13536 // (the overflow address should be kept 8-byte aligned)
13537 unsigned NextAddrReg = MRI.createVirtualRegister(AddrRegClass);
13538 BuildMI(overflowMBB, DL, TII->get(X86::ADD64ri32), NextAddrReg)
13539 .addReg(OverflowDestReg)
13540 .addImm(ArgSizeA8);
13541
13542 // Store the new overflow address.
13543 BuildMI(overflowMBB, DL, TII->get(X86::MOV64mr))
13544 .addOperand(Base)
13545 .addOperand(Scale)
13546 .addOperand(Index)
13547 .addDisp(Disp, 8)
13548 .addOperand(Segment)
13549 .addReg(NextAddrReg)
13550 .setMemRefs(MMOBegin, MMOEnd);
13551
13552 // If we branched, emit the PHI to the front of endMBB.
13553 if (offsetMBB) {
13554 BuildMI(*endMBB, endMBB->begin(), DL,
13555 TII->get(X86::PHI), DestReg)
13556 .addReg(OffsetDestReg).addMBB(offsetMBB)
13557 .addReg(OverflowDestReg).addMBB(overflowMBB);
13558 }
13559
13560 // Erase the pseudo instruction
13561 MI->eraseFromParent();
13562
13563 return endMBB;
13564}
13565
13566MachineBasicBlock *
Dan Gohmand6708ea2009-08-15 01:38:56 +000013567X86TargetLowering::EmitVAStartSaveXMMRegsWithCustomInserter(
13568 MachineInstr *MI,
13569 MachineBasicBlock *MBB) const {
13570 // Emit code to save XMM registers to the stack. The ABI says that the
13571 // number of registers to save is given in %al, so it's theoretically
13572 // possible to do an indirect jump trick to avoid saving all of them,
13573 // however this code takes a simpler approach and just executes all
13574 // of the stores if %al is non-zero. It's less code, and it's probably
13575 // easier on the hardware branch predictor, and stores aren't all that
13576 // expensive anyway.
13577
13578 // Create the new basic blocks. One block contains all the XMM stores,
13579 // and one block is the final destination regardless of whether any
13580 // stores were performed.
13581 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
13582 MachineFunction *F = MBB->getParent();
13583 MachineFunction::iterator MBBIter = MBB;
13584 ++MBBIter;
13585 MachineBasicBlock *XMMSaveMBB = F->CreateMachineBasicBlock(LLVM_BB);
13586 MachineBasicBlock *EndMBB = F->CreateMachineBasicBlock(LLVM_BB);
13587 F->insert(MBBIter, XMMSaveMBB);
13588 F->insert(MBBIter, EndMBB);
13589
Dan Gohman14152b42010-07-06 20:24:04 +000013590 // Transfer the remainder of MBB and its successor edges to EndMBB.
13591 EndMBB->splice(EndMBB->begin(), MBB,
13592 llvm::next(MachineBasicBlock::iterator(MI)),
13593 MBB->end());
13594 EndMBB->transferSuccessorsAndUpdatePHIs(MBB);
13595
Dan Gohmand6708ea2009-08-15 01:38:56 +000013596 // The original block will now fall through to the XMM save block.
13597 MBB->addSuccessor(XMMSaveMBB);
13598 // The XMMSaveMBB will fall through to the end block.
13599 XMMSaveMBB->addSuccessor(EndMBB);
13600
13601 // Now add the instructions.
13602 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
13603 DebugLoc DL = MI->getDebugLoc();
13604
13605 unsigned CountReg = MI->getOperand(0).getReg();
13606 int64_t RegSaveFrameIndex = MI->getOperand(1).getImm();
13607 int64_t VarArgsFPOffset = MI->getOperand(2).getImm();
13608
13609 if (!Subtarget->isTargetWin64()) {
13610 // If %al is 0, branch around the XMM save block.
13611 BuildMI(MBB, DL, TII->get(X86::TEST8rr)).addReg(CountReg).addReg(CountReg);
Chris Lattnerbd13fb62010-02-11 19:25:55 +000013612 BuildMI(MBB, DL, TII->get(X86::JE_4)).addMBB(EndMBB);
Dan Gohmand6708ea2009-08-15 01:38:56 +000013613 MBB->addSuccessor(EndMBB);
13614 }
13615
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000013616 unsigned MOVOpc = Subtarget->hasFp256() ? X86::VMOVAPSmr : X86::MOVAPSmr;
Dan Gohmand6708ea2009-08-15 01:38:56 +000013617 // In the XMM save block, save all the XMM argument registers.
13618 for (int i = 3, e = MI->getNumOperands(); i != e; ++i) {
13619 int64_t Offset = (i - 3) * 16 + VarArgsFPOffset;
Dan Gohmanc76909a2009-09-25 20:36:54 +000013620 MachineMemOperand *MMO =
Evan Chengff89dcb2009-10-18 18:16:27 +000013621 F->getMachineMemOperand(
Chris Lattnere8639032010-09-21 06:22:23 +000013622 MachinePointerInfo::getFixedStack(RegSaveFrameIndex, Offset),
Chris Lattner59db5492010-09-21 04:39:43 +000013623 MachineMemOperand::MOStore,
Evan Chengff89dcb2009-10-18 18:16:27 +000013624 /*Size=*/16, /*Align=*/16);
Bruno Cardoso Lopes5affa512011-08-31 03:04:09 +000013625 BuildMI(XMMSaveMBB, DL, TII->get(MOVOpc))
Dan Gohmand6708ea2009-08-15 01:38:56 +000013626 .addFrameIndex(RegSaveFrameIndex)
13627 .addImm(/*Scale=*/1)
13628 .addReg(/*IndexReg=*/0)
13629 .addImm(/*Disp=*/Offset)
13630 .addReg(/*Segment=*/0)
13631 .addReg(MI->getOperand(i).getReg())
Dan Gohmanc76909a2009-09-25 20:36:54 +000013632 .addMemOperand(MMO);
Dan Gohmand6708ea2009-08-15 01:38:56 +000013633 }
13634
Dan Gohman14152b42010-07-06 20:24:04 +000013635 MI->eraseFromParent(); // The pseudo instruction is gone now.
Dan Gohmand6708ea2009-08-15 01:38:56 +000013636
13637 return EndMBB;
13638}
Mon P Wang63307c32008-05-05 19:05:59 +000013639
Lang Hames6e3f7e42012-02-03 01:13:49 +000013640// The EFLAGS operand of SelectItr might be missing a kill marker
13641// because there were multiple uses of EFLAGS, and ISel didn't know
13642// which to mark. Figure out whether SelectItr should have had a
13643// kill marker, and set it if it should. Returns the correct kill
13644// marker value.
13645static bool checkAndUpdateEFLAGSKill(MachineBasicBlock::iterator SelectItr,
13646 MachineBasicBlock* BB,
13647 const TargetRegisterInfo* TRI) {
13648 // Scan forward through BB for a use/def of EFLAGS.
13649 MachineBasicBlock::iterator miI(llvm::next(SelectItr));
13650 for (MachineBasicBlock::iterator miE = BB->end(); miI != miE; ++miI) {
Lang Hames50a36f72012-02-02 07:48:37 +000013651 const MachineInstr& mi = *miI;
Lang Hames6e3f7e42012-02-03 01:13:49 +000013652 if (mi.readsRegister(X86::EFLAGS))
Lang Hames50a36f72012-02-02 07:48:37 +000013653 return false;
Lang Hames6e3f7e42012-02-03 01:13:49 +000013654 if (mi.definesRegister(X86::EFLAGS))
13655 break; // Should have kill-flag - update below.
13656 }
13657
13658 // If we hit the end of the block, check whether EFLAGS is live into a
13659 // successor.
13660 if (miI == BB->end()) {
13661 for (MachineBasicBlock::succ_iterator sItr = BB->succ_begin(),
13662 sEnd = BB->succ_end();
13663 sItr != sEnd; ++sItr) {
13664 MachineBasicBlock* succ = *sItr;
13665 if (succ->isLiveIn(X86::EFLAGS))
13666 return false;
Lang Hames50a36f72012-02-02 07:48:37 +000013667 }
13668 }
13669
Lang Hames6e3f7e42012-02-03 01:13:49 +000013670 // We found a def, or hit the end of the basic block and EFLAGS wasn't live
13671 // out. SelectMI should have a kill flag on EFLAGS.
13672 SelectItr->addRegisterKilled(X86::EFLAGS, TRI);
Lang Hames50a36f72012-02-02 07:48:37 +000013673 return true;
13674}
13675
Evan Cheng60c07e12006-07-05 22:17:51 +000013676MachineBasicBlock *
Chris Lattner52600972009-09-02 05:57:00 +000013677X86TargetLowering::EmitLoweredSelect(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000013678 MachineBasicBlock *BB) const {
Chris Lattner52600972009-09-02 05:57:00 +000013679 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
13680 DebugLoc DL = MI->getDebugLoc();
Daniel Dunbara279bc32009-09-20 02:20:51 +000013681
Chris Lattner52600972009-09-02 05:57:00 +000013682 // To "insert" a SELECT_CC instruction, we actually have to insert the
13683 // diamond control-flow pattern. The incoming instruction knows the
13684 // destination vreg to set, the condition code register to branch on, the
13685 // true/false values to select between, and a branch opcode to use.
13686 const BasicBlock *LLVM_BB = BB->getBasicBlock();
13687 MachineFunction::iterator It = BB;
13688 ++It;
Daniel Dunbara279bc32009-09-20 02:20:51 +000013689
Chris Lattner52600972009-09-02 05:57:00 +000013690 // thisMBB:
13691 // ...
13692 // TrueVal = ...
13693 // cmpTY ccX, r1, r2
13694 // bCC copy1MBB
13695 // fallthrough --> copy0MBB
13696 MachineBasicBlock *thisMBB = BB;
13697 MachineFunction *F = BB->getParent();
13698 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
13699 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Chris Lattner52600972009-09-02 05:57:00 +000013700 F->insert(It, copy0MBB);
13701 F->insert(It, sinkMBB);
Bill Wendling730c07e2010-06-25 20:48:10 +000013702
Bill Wendling730c07e2010-06-25 20:48:10 +000013703 // If the EFLAGS register isn't dead in the terminator, then claim that it's
13704 // live into the sink and copy blocks.
Lang Hames6e3f7e42012-02-03 01:13:49 +000013705 const TargetRegisterInfo* TRI = getTargetMachine().getRegisterInfo();
13706 if (!MI->killsRegister(X86::EFLAGS) &&
13707 !checkAndUpdateEFLAGSKill(MI, BB, TRI)) {
13708 copy0MBB->addLiveIn(X86::EFLAGS);
13709 sinkMBB->addLiveIn(X86::EFLAGS);
Bill Wendling730c07e2010-06-25 20:48:10 +000013710 }
13711
Dan Gohman14152b42010-07-06 20:24:04 +000013712 // Transfer the remainder of BB and its successor edges to sinkMBB.
13713 sinkMBB->splice(sinkMBB->begin(), BB,
13714 llvm::next(MachineBasicBlock::iterator(MI)),
13715 BB->end());
13716 sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
13717
13718 // Add the true and fallthrough blocks as its successors.
13719 BB->addSuccessor(copy0MBB);
13720 BB->addSuccessor(sinkMBB);
13721
13722 // Create the conditional branch instruction.
13723 unsigned Opc =
13724 X86::GetCondBranchFromCond((X86::CondCode)MI->getOperand(3).getImm());
13725 BuildMI(BB, DL, TII->get(Opc)).addMBB(sinkMBB);
13726
Chris Lattner52600972009-09-02 05:57:00 +000013727 // copy0MBB:
13728 // %FalseValue = ...
13729 // # fallthrough to sinkMBB
Dan Gohman3335a222010-04-30 20:14:26 +000013730 copy0MBB->addSuccessor(sinkMBB);
Daniel Dunbara279bc32009-09-20 02:20:51 +000013731
Chris Lattner52600972009-09-02 05:57:00 +000013732 // sinkMBB:
13733 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
13734 // ...
Dan Gohman14152b42010-07-06 20:24:04 +000013735 BuildMI(*sinkMBB, sinkMBB->begin(), DL,
13736 TII->get(X86::PHI), MI->getOperand(0).getReg())
Chris Lattner52600972009-09-02 05:57:00 +000013737 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
13738 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
13739
Dan Gohman14152b42010-07-06 20:24:04 +000013740 MI->eraseFromParent(); // The pseudo instruction is gone now.
Dan Gohman3335a222010-04-30 20:14:26 +000013741 return sinkMBB;
Chris Lattner52600972009-09-02 05:57:00 +000013742}
13743
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000013744MachineBasicBlock *
Rafael Espindola151ab3e2011-08-30 19:47:04 +000013745X86TargetLowering::EmitLoweredSegAlloca(MachineInstr *MI, MachineBasicBlock *BB,
13746 bool Is64Bit) const {
13747 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
13748 DebugLoc DL = MI->getDebugLoc();
13749 MachineFunction *MF = BB->getParent();
13750 const BasicBlock *LLVM_BB = BB->getBasicBlock();
13751
Nick Lewycky8a8d4792011-12-02 22:16:29 +000013752 assert(getTargetMachine().Options.EnableSegmentedStacks);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000013753
13754 unsigned TlsReg = Is64Bit ? X86::FS : X86::GS;
13755 unsigned TlsOffset = Is64Bit ? 0x70 : 0x30;
13756
13757 // BB:
13758 // ... [Till the alloca]
13759 // If stacklet is not large enough, jump to mallocMBB
13760 //
13761 // bumpMBB:
13762 // Allocate by subtracting from RSP
13763 // Jump to continueMBB
13764 //
13765 // mallocMBB:
13766 // Allocate by call to runtime
13767 //
13768 // continueMBB:
13769 // ...
13770 // [rest of original BB]
13771 //
13772
13773 MachineBasicBlock *mallocMBB = MF->CreateMachineBasicBlock(LLVM_BB);
13774 MachineBasicBlock *bumpMBB = MF->CreateMachineBasicBlock(LLVM_BB);
13775 MachineBasicBlock *continueMBB = MF->CreateMachineBasicBlock(LLVM_BB);
13776
13777 MachineRegisterInfo &MRI = MF->getRegInfo();
13778 const TargetRegisterClass *AddrRegClass =
13779 getRegClassFor(Is64Bit ? MVT::i64:MVT::i32);
13780
13781 unsigned mallocPtrVReg = MRI.createVirtualRegister(AddrRegClass),
13782 bumpSPPtrVReg = MRI.createVirtualRegister(AddrRegClass),
13783 tmpSPVReg = MRI.createVirtualRegister(AddrRegClass),
Rafael Espindola66bf7432011-10-26 21:16:41 +000013784 SPLimitVReg = MRI.createVirtualRegister(AddrRegClass),
Rafael Espindola151ab3e2011-08-30 19:47:04 +000013785 sizeVReg = MI->getOperand(1).getReg(),
13786 physSPReg = Is64Bit ? X86::RSP : X86::ESP;
13787
13788 MachineFunction::iterator MBBIter = BB;
13789 ++MBBIter;
13790
13791 MF->insert(MBBIter, bumpMBB);
13792 MF->insert(MBBIter, mallocMBB);
13793 MF->insert(MBBIter, continueMBB);
13794
13795 continueMBB->splice(continueMBB->begin(), BB, llvm::next
13796 (MachineBasicBlock::iterator(MI)), BB->end());
13797 continueMBB->transferSuccessorsAndUpdatePHIs(BB);
13798
13799 // Add code to the main basic block to check if the stack limit has been hit,
13800 // and if so, jump to mallocMBB otherwise to bumpMBB.
13801 BuildMI(BB, DL, TII->get(TargetOpcode::COPY), tmpSPVReg).addReg(physSPReg);
Rafael Espindola66bf7432011-10-26 21:16:41 +000013802 BuildMI(BB, DL, TII->get(Is64Bit ? X86::SUB64rr:X86::SUB32rr), SPLimitVReg)
Rafael Espindola151ab3e2011-08-30 19:47:04 +000013803 .addReg(tmpSPVReg).addReg(sizeVReg);
13804 BuildMI(BB, DL, TII->get(Is64Bit ? X86::CMP64mr:X86::CMP32mr))
Rafael Espindola014f7a32012-01-11 18:14:03 +000013805 .addReg(0).addImm(1).addReg(0).addImm(TlsOffset).addReg(TlsReg)
Rafael Espindola66bf7432011-10-26 21:16:41 +000013806 .addReg(SPLimitVReg);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000013807 BuildMI(BB, DL, TII->get(X86::JG_4)).addMBB(mallocMBB);
13808
13809 // bumpMBB simply decreases the stack pointer, since we know the current
13810 // stacklet has enough space.
13811 BuildMI(bumpMBB, DL, TII->get(TargetOpcode::COPY), physSPReg)
Rafael Espindola66bf7432011-10-26 21:16:41 +000013812 .addReg(SPLimitVReg);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000013813 BuildMI(bumpMBB, DL, TII->get(TargetOpcode::COPY), bumpSPPtrVReg)
Rafael Espindola66bf7432011-10-26 21:16:41 +000013814 .addReg(SPLimitVReg);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000013815 BuildMI(bumpMBB, DL, TII->get(X86::JMP_4)).addMBB(continueMBB);
13816
13817 // Calls into a routine in libgcc to allocate more space from the heap.
Jakob Stoklund Olesen8bcde2a2012-02-16 00:02:50 +000013818 const uint32_t *RegMask =
13819 getTargetMachine().getRegisterInfo()->getCallPreservedMask(CallingConv::C);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000013820 if (Is64Bit) {
13821 BuildMI(mallocMBB, DL, TII->get(X86::MOV64rr), X86::RDI)
13822 .addReg(sizeVReg);
13823 BuildMI(mallocMBB, DL, TII->get(X86::CALL64pcrel32))
Jakob Stoklund Olesen85dccf12012-07-04 23:53:27 +000013824 .addExternalSymbol("__morestack_allocate_stack_space")
Jakob Stoklund Olesen8bcde2a2012-02-16 00:02:50 +000013825 .addRegMask(RegMask)
Jakob Stoklund Olesen85dccf12012-07-04 23:53:27 +000013826 .addReg(X86::RDI, RegState::Implicit)
Jakob Stoklund Olesen8bcde2a2012-02-16 00:02:50 +000013827 .addReg(X86::RAX, RegState::ImplicitDefine);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000013828 } else {
13829 BuildMI(mallocMBB, DL, TII->get(X86::SUB32ri), physSPReg).addReg(physSPReg)
13830 .addImm(12);
13831 BuildMI(mallocMBB, DL, TII->get(X86::PUSH32r)).addReg(sizeVReg);
13832 BuildMI(mallocMBB, DL, TII->get(X86::CALLpcrel32))
Jakob Stoklund Olesen8bcde2a2012-02-16 00:02:50 +000013833 .addExternalSymbol("__morestack_allocate_stack_space")
13834 .addRegMask(RegMask)
13835 .addReg(X86::EAX, RegState::ImplicitDefine);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000013836 }
13837
13838 if (!Is64Bit)
13839 BuildMI(mallocMBB, DL, TII->get(X86::ADD32ri), physSPReg).addReg(physSPReg)
13840 .addImm(16);
13841
13842 BuildMI(mallocMBB, DL, TII->get(TargetOpcode::COPY), mallocPtrVReg)
13843 .addReg(Is64Bit ? X86::RAX : X86::EAX);
13844 BuildMI(mallocMBB, DL, TII->get(X86::JMP_4)).addMBB(continueMBB);
13845
13846 // Set up the CFG correctly.
13847 BB->addSuccessor(bumpMBB);
13848 BB->addSuccessor(mallocMBB);
13849 mallocMBB->addSuccessor(continueMBB);
13850 bumpMBB->addSuccessor(continueMBB);
13851
13852 // Take care of the PHI nodes.
13853 BuildMI(*continueMBB, continueMBB->begin(), DL, TII->get(X86::PHI),
13854 MI->getOperand(0).getReg())
13855 .addReg(mallocPtrVReg).addMBB(mallocMBB)
13856 .addReg(bumpSPPtrVReg).addMBB(bumpMBB);
13857
13858 // Delete the original pseudo instruction.
13859 MI->eraseFromParent();
13860
13861 // And we're done.
13862 return continueMBB;
13863}
13864
13865MachineBasicBlock *
Michael J. Spencere9c253e2010-10-21 01:41:01 +000013866X86TargetLowering::EmitLoweredWinAlloca(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000013867 MachineBasicBlock *BB) const {
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000013868 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
13869 DebugLoc DL = MI->getDebugLoc();
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000013870
NAKAMURA Takumia2e07622011-03-24 07:07:00 +000013871 assert(!Subtarget->isTargetEnvMacho());
13872
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000013873 // The lowering is pretty easy: we're just emitting the call to _alloca. The
13874 // non-trivial part is impdef of ESP.
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000013875
NAKAMURA Takumia2e07622011-03-24 07:07:00 +000013876 if (Subtarget->isTargetWin64()) {
13877 if (Subtarget->isTargetCygMing()) {
13878 // ___chkstk(Mingw64):
13879 // Clobbers R10, R11, RAX and EFLAGS.
13880 // Updates RSP.
13881 BuildMI(*BB, MI, DL, TII->get(X86::W64ALLOCA))
13882 .addExternalSymbol("___chkstk")
13883 .addReg(X86::RAX, RegState::Implicit)
13884 .addReg(X86::RSP, RegState::Implicit)
13885 .addReg(X86::RAX, RegState::Define | RegState::Implicit)
13886 .addReg(X86::RSP, RegState::Define | RegState::Implicit)
13887 .addReg(X86::EFLAGS, RegState::Define | RegState::Implicit);
13888 } else {
13889 // __chkstk(MSVCRT): does not update stack pointer.
13890 // Clobbers R10, R11 and EFLAGS.
13891 // FIXME: RAX(allocated size) might be reused and not killed.
13892 BuildMI(*BB, MI, DL, TII->get(X86::W64ALLOCA))
13893 .addExternalSymbol("__chkstk")
13894 .addReg(X86::RAX, RegState::Implicit)
13895 .addReg(X86::EFLAGS, RegState::Define | RegState::Implicit);
13896 // RAX has the offset to subtracted from RSP.
13897 BuildMI(*BB, MI, DL, TII->get(X86::SUB64rr), X86::RSP)
13898 .addReg(X86::RSP)
13899 .addReg(X86::RAX);
13900 }
13901 } else {
13902 const char *StackProbeSymbol =
Michael J. Spencere9c253e2010-10-21 01:41:01 +000013903 Subtarget->isTargetWindows() ? "_chkstk" : "_alloca";
13904
NAKAMURA Takumia2e07622011-03-24 07:07:00 +000013905 BuildMI(*BB, MI, DL, TII->get(X86::CALLpcrel32))
13906 .addExternalSymbol(StackProbeSymbol)
13907 .addReg(X86::EAX, RegState::Implicit)
13908 .addReg(X86::ESP, RegState::Implicit)
13909 .addReg(X86::EAX, RegState::Define | RegState::Implicit)
13910 .addReg(X86::ESP, RegState::Define | RegState::Implicit)
13911 .addReg(X86::EFLAGS, RegState::Define | RegState::Implicit);
13912 }
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000013913
Dan Gohman14152b42010-07-06 20:24:04 +000013914 MI->eraseFromParent(); // The pseudo instruction is gone now.
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000013915 return BB;
13916}
Chris Lattner52600972009-09-02 05:57:00 +000013917
13918MachineBasicBlock *
Eric Christopher30ef0e52010-06-03 04:07:48 +000013919X86TargetLowering::EmitLoweredTLSCall(MachineInstr *MI,
13920 MachineBasicBlock *BB) const {
13921 // This is pretty easy. We're taking the value that we received from
13922 // our load from the relocation, sticking it in either RDI (x86-64)
13923 // or EAX and doing an indirect call. The return value will then
13924 // be in the normal return register.
Michael J. Spencerec38de22010-10-10 22:04:20 +000013925 const X86InstrInfo *TII
Eric Christopher54415362010-06-08 22:04:25 +000013926 = static_cast<const X86InstrInfo*>(getTargetMachine().getInstrInfo());
Eric Christopher30ef0e52010-06-03 04:07:48 +000013927 DebugLoc DL = MI->getDebugLoc();
13928 MachineFunction *F = BB->getParent();
Eric Christopher722d3152010-09-27 06:01:51 +000013929
13930 assert(Subtarget->isTargetDarwin() && "Darwin only instr emitted?");
Eric Christopher54415362010-06-08 22:04:25 +000013931 assert(MI->getOperand(3).isGlobal() && "This should be a global");
Michael J. Spencerec38de22010-10-10 22:04:20 +000013932
Jakob Stoklund Olesen8bcde2a2012-02-16 00:02:50 +000013933 // Get a register mask for the lowered call.
13934 // FIXME: The 32-bit calls have non-standard calling conventions. Use a
13935 // proper register mask.
13936 const uint32_t *RegMask =
13937 getTargetMachine().getRegisterInfo()->getCallPreservedMask(CallingConv::C);
Eric Christopher30ef0e52010-06-03 04:07:48 +000013938 if (Subtarget->is64Bit()) {
Dan Gohman14152b42010-07-06 20:24:04 +000013939 MachineInstrBuilder MIB = BuildMI(*BB, MI, DL,
13940 TII->get(X86::MOV64rm), X86::RDI)
Eric Christopher54415362010-06-08 22:04:25 +000013941 .addReg(X86::RIP)
13942 .addImm(0).addReg(0)
Michael J. Spencerec38de22010-10-10 22:04:20 +000013943 .addGlobalAddress(MI->getOperand(3).getGlobal(), 0,
Eric Christopher54415362010-06-08 22:04:25 +000013944 MI->getOperand(3).getTargetFlags())
13945 .addReg(0);
Eric Christopher722d3152010-09-27 06:01:51 +000013946 MIB = BuildMI(*BB, MI, DL, TII->get(X86::CALL64m));
Chris Lattner599b5312010-07-08 23:46:44 +000013947 addDirectMem(MIB, X86::RDI);
Jakob Stoklund Olesen8bcde2a2012-02-16 00:02:50 +000013948 MIB.addReg(X86::RAX, RegState::ImplicitDefine).addRegMask(RegMask);
Eric Christopher61025492010-06-15 23:08:42 +000013949 } else if (getTargetMachine().getRelocationModel() != Reloc::PIC_) {
Dan Gohman14152b42010-07-06 20:24:04 +000013950 MachineInstrBuilder MIB = BuildMI(*BB, MI, DL,
13951 TII->get(X86::MOV32rm), X86::EAX)
Eric Christopher61025492010-06-15 23:08:42 +000013952 .addReg(0)
13953 .addImm(0).addReg(0)
Michael J. Spencerec38de22010-10-10 22:04:20 +000013954 .addGlobalAddress(MI->getOperand(3).getGlobal(), 0,
Eric Christopher61025492010-06-15 23:08:42 +000013955 MI->getOperand(3).getTargetFlags())
13956 .addReg(0);
Dan Gohman14152b42010-07-06 20:24:04 +000013957 MIB = BuildMI(*BB, MI, DL, TII->get(X86::CALL32m));
Chris Lattner599b5312010-07-08 23:46:44 +000013958 addDirectMem(MIB, X86::EAX);
Jakob Stoklund Olesen8bcde2a2012-02-16 00:02:50 +000013959 MIB.addReg(X86::EAX, RegState::ImplicitDefine).addRegMask(RegMask);
Eric Christopher30ef0e52010-06-03 04:07:48 +000013960 } else {
Dan Gohman14152b42010-07-06 20:24:04 +000013961 MachineInstrBuilder MIB = BuildMI(*BB, MI, DL,
13962 TII->get(X86::MOV32rm), X86::EAX)
Eric Christopher54415362010-06-08 22:04:25 +000013963 .addReg(TII->getGlobalBaseReg(F))
13964 .addImm(0).addReg(0)
Michael J. Spencerec38de22010-10-10 22:04:20 +000013965 .addGlobalAddress(MI->getOperand(3).getGlobal(), 0,
Eric Christopher54415362010-06-08 22:04:25 +000013966 MI->getOperand(3).getTargetFlags())
13967 .addReg(0);
Dan Gohman14152b42010-07-06 20:24:04 +000013968 MIB = BuildMI(*BB, MI, DL, TII->get(X86::CALL32m));
Chris Lattner599b5312010-07-08 23:46:44 +000013969 addDirectMem(MIB, X86::EAX);
Jakob Stoklund Olesen8bcde2a2012-02-16 00:02:50 +000013970 MIB.addReg(X86::EAX, RegState::ImplicitDefine).addRegMask(RegMask);
Eric Christopher30ef0e52010-06-03 04:07:48 +000013971 }
Michael J. Spencerec38de22010-10-10 22:04:20 +000013972
Dan Gohman14152b42010-07-06 20:24:04 +000013973 MI->eraseFromParent(); // The pseudo instruction is gone now.
Eric Christopher30ef0e52010-06-03 04:07:48 +000013974 return BB;
13975}
13976
13977MachineBasicBlock *
Michael Liao6c0e04c2012-10-15 22:39:43 +000013978X86TargetLowering::emitEHSjLjSetJmp(MachineInstr *MI,
13979 MachineBasicBlock *MBB) const {
13980 DebugLoc DL = MI->getDebugLoc();
13981 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
13982
13983 MachineFunction *MF = MBB->getParent();
13984 MachineRegisterInfo &MRI = MF->getRegInfo();
13985
13986 const BasicBlock *BB = MBB->getBasicBlock();
13987 MachineFunction::iterator I = MBB;
13988 ++I;
13989
13990 // Memory Reference
13991 MachineInstr::mmo_iterator MMOBegin = MI->memoperands_begin();
13992 MachineInstr::mmo_iterator MMOEnd = MI->memoperands_end();
13993
13994 unsigned DstReg;
13995 unsigned MemOpndSlot = 0;
13996
13997 unsigned CurOp = 0;
13998
13999 DstReg = MI->getOperand(CurOp++).getReg();
14000 const TargetRegisterClass *RC = MRI.getRegClass(DstReg);
14001 assert(RC->hasType(MVT::i32) && "Invalid destination!");
14002 unsigned mainDstReg = MRI.createVirtualRegister(RC);
14003 unsigned restoreDstReg = MRI.createVirtualRegister(RC);
14004
14005 MemOpndSlot = CurOp;
14006
14007 MVT PVT = getPointerTy();
14008 assert((PVT == MVT::i64 || PVT == MVT::i32) &&
14009 "Invalid Pointer Size!");
14010
14011 // For v = setjmp(buf), we generate
14012 //
14013 // thisMBB:
Michael Liao281ae5a2012-10-17 02:22:27 +000014014 // buf[LabelOffset] = restoreMBB
Michael Liao6c0e04c2012-10-15 22:39:43 +000014015 // SjLjSetup restoreMBB
14016 //
14017 // mainMBB:
14018 // v_main = 0
14019 //
14020 // sinkMBB:
14021 // v = phi(main, restore)
14022 //
14023 // restoreMBB:
14024 // v_restore = 1
14025
14026 MachineBasicBlock *thisMBB = MBB;
14027 MachineBasicBlock *mainMBB = MF->CreateMachineBasicBlock(BB);
14028 MachineBasicBlock *sinkMBB = MF->CreateMachineBasicBlock(BB);
14029 MachineBasicBlock *restoreMBB = MF->CreateMachineBasicBlock(BB);
14030 MF->insert(I, mainMBB);
14031 MF->insert(I, sinkMBB);
14032 MF->push_back(restoreMBB);
14033
14034 MachineInstrBuilder MIB;
14035
14036 // Transfer the remainder of BB and its successor edges to sinkMBB.
14037 sinkMBB->splice(sinkMBB->begin(), MBB,
14038 llvm::next(MachineBasicBlock::iterator(MI)), MBB->end());
14039 sinkMBB->transferSuccessorsAndUpdatePHIs(MBB);
14040
14041 // thisMBB:
Michael Liao281ae5a2012-10-17 02:22:27 +000014042 unsigned PtrStoreOpc = 0;
14043 unsigned LabelReg = 0;
14044 const int64_t LabelOffset = 1 * PVT.getStoreSize();
14045 Reloc::Model RM = getTargetMachine().getRelocationModel();
14046 bool UseImmLabel = (getTargetMachine().getCodeModel() == CodeModel::Small) &&
14047 (RM == Reloc::Static || RM == Reloc::DynamicNoPIC);
Michael Liao6c0e04c2012-10-15 22:39:43 +000014048
Michael Liao281ae5a2012-10-17 02:22:27 +000014049 // Prepare IP either in reg or imm.
14050 if (!UseImmLabel) {
14051 PtrStoreOpc = (PVT == MVT::i64) ? X86::MOV64mr : X86::MOV32mr;
14052 const TargetRegisterClass *PtrRC = getRegClassFor(PVT);
14053 LabelReg = MRI.createVirtualRegister(PtrRC);
14054 if (Subtarget->is64Bit()) {
14055 MIB = BuildMI(*thisMBB, MI, DL, TII->get(X86::LEA64r), LabelReg)
14056 .addReg(X86::RIP)
14057 .addImm(0)
14058 .addReg(0)
14059 .addMBB(restoreMBB)
14060 .addReg(0);
14061 } else {
14062 const X86InstrInfo *XII = static_cast<const X86InstrInfo*>(TII);
14063 MIB = BuildMI(*thisMBB, MI, DL, TII->get(X86::LEA32r), LabelReg)
14064 .addReg(XII->getGlobalBaseReg(MF))
14065 .addImm(0)
14066 .addReg(0)
14067 .addMBB(restoreMBB, Subtarget->ClassifyBlockAddressReference())
14068 .addReg(0);
14069 }
14070 } else
14071 PtrStoreOpc = (PVT == MVT::i64) ? X86::MOV64mi32 : X86::MOV32mi;
Michael Liao6c0e04c2012-10-15 22:39:43 +000014072 // Store IP
Michael Liao281ae5a2012-10-17 02:22:27 +000014073 MIB = BuildMI(*thisMBB, MI, DL, TII->get(PtrStoreOpc));
Michael Liao6c0e04c2012-10-15 22:39:43 +000014074 for (unsigned i = 0; i < X86::AddrNumOperands; ++i) {
14075 if (i == X86::AddrDisp)
Michael Liao281ae5a2012-10-17 02:22:27 +000014076 MIB.addDisp(MI->getOperand(MemOpndSlot + i), LabelOffset);
Michael Liao6c0e04c2012-10-15 22:39:43 +000014077 else
14078 MIB.addOperand(MI->getOperand(MemOpndSlot + i));
14079 }
Michael Liao281ae5a2012-10-17 02:22:27 +000014080 if (!UseImmLabel)
14081 MIB.addReg(LabelReg);
14082 else
14083 MIB.addMBB(restoreMBB);
Michael Liao6c0e04c2012-10-15 22:39:43 +000014084 MIB.setMemRefs(MMOBegin, MMOEnd);
14085 // Setup
14086 MIB = BuildMI(*thisMBB, MI, DL, TII->get(X86::EH_SjLj_Setup))
14087 .addMBB(restoreMBB);
14088 MIB.addRegMask(RegInfo->getNoPreservedMask());
14089 thisMBB->addSuccessor(mainMBB);
14090 thisMBB->addSuccessor(restoreMBB);
14091
14092 // mainMBB:
14093 // EAX = 0
14094 BuildMI(mainMBB, DL, TII->get(X86::MOV32r0), mainDstReg);
14095 mainMBB->addSuccessor(sinkMBB);
14096
14097 // sinkMBB:
14098 BuildMI(*sinkMBB, sinkMBB->begin(), DL,
14099 TII->get(X86::PHI), DstReg)
14100 .addReg(mainDstReg).addMBB(mainMBB)
14101 .addReg(restoreDstReg).addMBB(restoreMBB);
14102
14103 // restoreMBB:
14104 BuildMI(restoreMBB, DL, TII->get(X86::MOV32ri), restoreDstReg).addImm(1);
14105 BuildMI(restoreMBB, DL, TII->get(X86::JMP_4)).addMBB(sinkMBB);
14106 restoreMBB->addSuccessor(sinkMBB);
14107
14108 MI->eraseFromParent();
14109 return sinkMBB;
14110}
14111
14112MachineBasicBlock *
14113X86TargetLowering::emitEHSjLjLongJmp(MachineInstr *MI,
14114 MachineBasicBlock *MBB) const {
14115 DebugLoc DL = MI->getDebugLoc();
14116 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
14117
14118 MachineFunction *MF = MBB->getParent();
14119 MachineRegisterInfo &MRI = MF->getRegInfo();
14120
14121 // Memory Reference
14122 MachineInstr::mmo_iterator MMOBegin = MI->memoperands_begin();
14123 MachineInstr::mmo_iterator MMOEnd = MI->memoperands_end();
14124
14125 MVT PVT = getPointerTy();
14126 assert((PVT == MVT::i64 || PVT == MVT::i32) &&
14127 "Invalid Pointer Size!");
14128
14129 const TargetRegisterClass *RC =
14130 (PVT == MVT::i64) ? &X86::GR64RegClass : &X86::GR32RegClass;
14131 unsigned Tmp = MRI.createVirtualRegister(RC);
14132 // Since FP is only updated here but NOT referenced, it's treated as GPR.
14133 unsigned FP = (PVT == MVT::i64) ? X86::RBP : X86::EBP;
14134 unsigned SP = RegInfo->getStackRegister();
14135
14136 MachineInstrBuilder MIB;
14137
Michael Liao281ae5a2012-10-17 02:22:27 +000014138 const int64_t LabelOffset = 1 * PVT.getStoreSize();
14139 const int64_t SPOffset = 2 * PVT.getStoreSize();
Michael Liao6c0e04c2012-10-15 22:39:43 +000014140
14141 unsigned PtrLoadOpc = (PVT == MVT::i64) ? X86::MOV64rm : X86::MOV32rm;
14142 unsigned IJmpOpc = (PVT == MVT::i64) ? X86::JMP64r : X86::JMP32r;
14143
14144 // Reload FP
14145 MIB = BuildMI(*MBB, MI, DL, TII->get(PtrLoadOpc), FP);
14146 for (unsigned i = 0; i < X86::AddrNumOperands; ++i)
14147 MIB.addOperand(MI->getOperand(i));
14148 MIB.setMemRefs(MMOBegin, MMOEnd);
14149 // Reload IP
14150 MIB = BuildMI(*MBB, MI, DL, TII->get(PtrLoadOpc), Tmp);
14151 for (unsigned i = 0; i < X86::AddrNumOperands; ++i) {
14152 if (i == X86::AddrDisp)
Michael Liao281ae5a2012-10-17 02:22:27 +000014153 MIB.addDisp(MI->getOperand(i), LabelOffset);
Michael Liao6c0e04c2012-10-15 22:39:43 +000014154 else
14155 MIB.addOperand(MI->getOperand(i));
14156 }
14157 MIB.setMemRefs(MMOBegin, MMOEnd);
14158 // Reload SP
14159 MIB = BuildMI(*MBB, MI, DL, TII->get(PtrLoadOpc), SP);
14160 for (unsigned i = 0; i < X86::AddrNumOperands; ++i) {
14161 if (i == X86::AddrDisp)
Michael Liao281ae5a2012-10-17 02:22:27 +000014162 MIB.addDisp(MI->getOperand(i), SPOffset);
Michael Liao6c0e04c2012-10-15 22:39:43 +000014163 else
14164 MIB.addOperand(MI->getOperand(i));
14165 }
14166 MIB.setMemRefs(MMOBegin, MMOEnd);
14167 // Jump
14168 BuildMI(*MBB, MI, DL, TII->get(IJmpOpc)).addReg(Tmp);
14169
14170 MI->eraseFromParent();
14171 return MBB;
14172}
14173
14174MachineBasicBlock *
Evan Chengff9b3732008-01-30 18:18:23 +000014175X86TargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000014176 MachineBasicBlock *BB) const {
Evan Cheng60c07e12006-07-05 22:17:51 +000014177 switch (MI->getOpcode()) {
Craig Topperabb94d02012-02-05 03:43:23 +000014178 default: llvm_unreachable("Unexpected instr type to insert");
NAKAMURA Takumi7754f852011-01-26 02:04:09 +000014179 case X86::TAILJMPd64:
14180 case X86::TAILJMPr64:
14181 case X86::TAILJMPm64:
Craig Topper6d1263a2012-02-05 05:38:58 +000014182 llvm_unreachable("TAILJMP64 would not be touched here.");
NAKAMURA Takumi7754f852011-01-26 02:04:09 +000014183 case X86::TCRETURNdi64:
14184 case X86::TCRETURNri64:
14185 case X86::TCRETURNmi64:
NAKAMURA Takumi7754f852011-01-26 02:04:09 +000014186 return BB;
Michael J. Spencere9c253e2010-10-21 01:41:01 +000014187 case X86::WIN_ALLOCA:
14188 return EmitLoweredWinAlloca(MI, BB);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000014189 case X86::SEG_ALLOCA_32:
14190 return EmitLoweredSegAlloca(MI, BB, false);
14191 case X86::SEG_ALLOCA_64:
14192 return EmitLoweredSegAlloca(MI, BB, true);
Eric Christopher30ef0e52010-06-03 04:07:48 +000014193 case X86::TLSCall_32:
14194 case X86::TLSCall_64:
14195 return EmitLoweredTLSCall(MI, BB);
Dan Gohmancbbea0f2009-08-27 00:14:12 +000014196 case X86::CMOV_GR8:
Evan Cheng60c07e12006-07-05 22:17:51 +000014197 case X86::CMOV_FR32:
14198 case X86::CMOV_FR64:
14199 case X86::CMOV_V4F32:
14200 case X86::CMOV_V2F64:
Chris Lattner52600972009-09-02 05:57:00 +000014201 case X86::CMOV_V2I64:
Bruno Cardoso Lopesd40aa242011-08-09 23:27:13 +000014202 case X86::CMOV_V8F32:
14203 case X86::CMOV_V4F64:
14204 case X86::CMOV_V4I64:
Chris Lattner314a1132010-03-14 18:31:44 +000014205 case X86::CMOV_GR16:
14206 case X86::CMOV_GR32:
14207 case X86::CMOV_RFP32:
14208 case X86::CMOV_RFP64:
14209 case X86::CMOV_RFP80:
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000014210 return EmitLoweredSelect(MI, BB);
Evan Cheng60c07e12006-07-05 22:17:51 +000014211
Dale Johannesen849f2142007-07-03 00:53:03 +000014212 case X86::FP32_TO_INT16_IN_MEM:
14213 case X86::FP32_TO_INT32_IN_MEM:
14214 case X86::FP32_TO_INT64_IN_MEM:
14215 case X86::FP64_TO_INT16_IN_MEM:
14216 case X86::FP64_TO_INT32_IN_MEM:
Dale Johannesena996d522007-08-07 01:17:37 +000014217 case X86::FP64_TO_INT64_IN_MEM:
14218 case X86::FP80_TO_INT16_IN_MEM:
14219 case X86::FP80_TO_INT32_IN_MEM:
14220 case X86::FP80_TO_INT64_IN_MEM: {
Chris Lattner52600972009-09-02 05:57:00 +000014221 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
14222 DebugLoc DL = MI->getDebugLoc();
14223
Evan Cheng60c07e12006-07-05 22:17:51 +000014224 // Change the floating point control register to use "round towards zero"
14225 // mode when truncating to an integer value.
14226 MachineFunction *F = BB->getParent();
David Greene3f2bf852009-11-12 20:49:22 +000014227 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2, false);
Dan Gohman14152b42010-07-06 20:24:04 +000014228 addFrameReference(BuildMI(*BB, MI, DL,
14229 TII->get(X86::FNSTCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000014230
14231 // Load the old value of the high byte of the control word...
14232 unsigned OldCW =
Craig Topperc9099502012-04-20 06:31:50 +000014233 F->getRegInfo().createVirtualRegister(&X86::GR16RegClass);
Dan Gohman14152b42010-07-06 20:24:04 +000014234 addFrameReference(BuildMI(*BB, MI, DL, TII->get(X86::MOV16rm), OldCW),
Dale Johannesene4d209d2009-02-03 20:21:25 +000014235 CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000014236
14237 // Set the high part to be round to zero...
Dan Gohman14152b42010-07-06 20:24:04 +000014238 addFrameReference(BuildMI(*BB, MI, DL, TII->get(X86::MOV16mi)), CWFrameIdx)
Evan Chengc0f64ff2006-11-27 23:37:22 +000014239 .addImm(0xC7F);
Evan Cheng60c07e12006-07-05 22:17:51 +000014240
14241 // Reload the modified control word now...
Dan Gohman14152b42010-07-06 20:24:04 +000014242 addFrameReference(BuildMI(*BB, MI, DL,
14243 TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000014244
14245 // Restore the memory image of control word to original value
Dan Gohman14152b42010-07-06 20:24:04 +000014246 addFrameReference(BuildMI(*BB, MI, DL, TII->get(X86::MOV16mr)), CWFrameIdx)
Evan Chengc0f64ff2006-11-27 23:37:22 +000014247 .addReg(OldCW);
Evan Cheng60c07e12006-07-05 22:17:51 +000014248
14249 // Get the X86 opcode to use.
14250 unsigned Opc;
14251 switch (MI->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +000014252 default: llvm_unreachable("illegal opcode!");
Dale Johannesene377d4d2007-07-04 21:07:47 +000014253 case X86::FP32_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m32; break;
14254 case X86::FP32_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m32; break;
14255 case X86::FP32_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m32; break;
14256 case X86::FP64_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m64; break;
14257 case X86::FP64_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m64; break;
14258 case X86::FP64_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m64; break;
Dale Johannesena996d522007-08-07 01:17:37 +000014259 case X86::FP80_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m80; break;
14260 case X86::FP80_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m80; break;
14261 case X86::FP80_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m80; break;
Evan Cheng60c07e12006-07-05 22:17:51 +000014262 }
14263
14264 X86AddressMode AM;
14265 MachineOperand &Op = MI->getOperand(0);
Dan Gohmand735b802008-10-03 15:45:36 +000014266 if (Op.isReg()) {
Evan Cheng60c07e12006-07-05 22:17:51 +000014267 AM.BaseType = X86AddressMode::RegBase;
14268 AM.Base.Reg = Op.getReg();
14269 } else {
14270 AM.BaseType = X86AddressMode::FrameIndexBase;
Chris Lattner8aa797a2007-12-30 23:10:15 +000014271 AM.Base.FrameIndex = Op.getIndex();
Evan Cheng60c07e12006-07-05 22:17:51 +000014272 }
14273 Op = MI->getOperand(1);
Dan Gohmand735b802008-10-03 15:45:36 +000014274 if (Op.isImm())
Chris Lattner7fbe9722006-10-20 17:42:20 +000014275 AM.Scale = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +000014276 Op = MI->getOperand(2);
Dan Gohmand735b802008-10-03 15:45:36 +000014277 if (Op.isImm())
Chris Lattner7fbe9722006-10-20 17:42:20 +000014278 AM.IndexReg = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +000014279 Op = MI->getOperand(3);
Dan Gohmand735b802008-10-03 15:45:36 +000014280 if (Op.isGlobal()) {
Evan Cheng60c07e12006-07-05 22:17:51 +000014281 AM.GV = Op.getGlobal();
14282 } else {
Chris Lattner7fbe9722006-10-20 17:42:20 +000014283 AM.Disp = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +000014284 }
Dan Gohman14152b42010-07-06 20:24:04 +000014285 addFullAddress(BuildMI(*BB, MI, DL, TII->get(Opc)), AM)
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000014286 .addReg(MI->getOperand(X86::AddrNumOperands).getReg());
Evan Cheng60c07e12006-07-05 22:17:51 +000014287
14288 // Reload the original control word now.
Dan Gohman14152b42010-07-06 20:24:04 +000014289 addFrameReference(BuildMI(*BB, MI, DL,
14290 TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000014291
Dan Gohman14152b42010-07-06 20:24:04 +000014292 MI->eraseFromParent(); // The pseudo instruction is gone now.
Evan Cheng60c07e12006-07-05 22:17:51 +000014293 return BB;
14294 }
Eric Christopherb120ab42009-08-18 22:50:32 +000014295 // String/text processing lowering.
14296 case X86::PCMPISTRM128REG:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000014297 case X86::VPCMPISTRM128REG:
Eric Christopherb120ab42009-08-18 22:50:32 +000014298 case X86::PCMPISTRM128MEM:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000014299 case X86::VPCMPISTRM128MEM:
Eric Christopherb120ab42009-08-18 22:50:32 +000014300 case X86::PCMPESTRM128REG:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000014301 case X86::VPCMPESTRM128REG:
Eric Christopherb120ab42009-08-18 22:50:32 +000014302 case X86::PCMPESTRM128MEM:
Craig Topper8aae8dd2012-11-10 08:57:41 +000014303 case X86::VPCMPESTRM128MEM:
14304 assert(Subtarget->hasSSE42() &&
14305 "Target must have SSE4.2 or AVX features enabled");
14306 return EmitPCMPSTRM(MI, BB, getTargetMachine().getInstrInfo());
Craig Topper9c7ae012012-11-10 01:23:36 +000014307
14308 // String/text processing lowering.
14309 case X86::PCMPISTRIREG:
14310 case X86::VPCMPISTRIREG:
14311 case X86::PCMPISTRIMEM:
14312 case X86::VPCMPISTRIMEM:
14313 case X86::PCMPESTRIREG:
14314 case X86::VPCMPESTRIREG:
14315 case X86::PCMPESTRIMEM:
Craig Topper8aae8dd2012-11-10 08:57:41 +000014316 case X86::VPCMPESTRIMEM:
14317 assert(Subtarget->hasSSE42() &&
14318 "Target must have SSE4.2 or AVX features enabled");
14319 return EmitPCMPSTRI(MI, BB, getTargetMachine().getInstrInfo());
Eric Christopherb120ab42009-08-18 22:50:32 +000014320
Craig Topper8aae8dd2012-11-10 08:57:41 +000014321 // Thread synchronization.
Eric Christopher228232b2010-11-30 07:20:12 +000014322 case X86::MONITOR:
Craig Topper2da36912012-11-11 22:45:02 +000014323 return EmitMonitor(MI, BB, getTargetMachine().getInstrInfo(), Subtarget);
Eric Christopher228232b2010-11-30 07:20:12 +000014324
Michael Liaobe02a902012-11-08 07:28:54 +000014325 // xbegin
14326 case X86::XBEGIN:
Craig Topper2da36912012-11-11 22:45:02 +000014327 return EmitXBegin(MI, BB, getTargetMachine().getInstrInfo());
Michael Liaobe02a902012-11-08 07:28:54 +000014328
Craig Topper8aae8dd2012-11-10 08:57:41 +000014329 // Atomic Lowering.
Dale Johannesen140be2d2008-08-19 18:47:28 +000014330 case X86::ATOMAND8:
Michael Liaob118a072012-09-20 03:06:15 +000014331 case X86::ATOMAND16:
14332 case X86::ATOMAND32:
Dale Johannesena99e3842008-08-20 00:48:50 +000014333 case X86::ATOMAND64:
Michael Liaob118a072012-09-20 03:06:15 +000014334 // Fall through
14335 case X86::ATOMOR8:
14336 case X86::ATOMOR16:
14337 case X86::ATOMOR32:
Dale Johannesena99e3842008-08-20 00:48:50 +000014338 case X86::ATOMOR64:
Michael Liaob118a072012-09-20 03:06:15 +000014339 // Fall through
14340 case X86::ATOMXOR16:
14341 case X86::ATOMXOR8:
14342 case X86::ATOMXOR32:
Dale Johannesena99e3842008-08-20 00:48:50 +000014343 case X86::ATOMXOR64:
Michael Liaob118a072012-09-20 03:06:15 +000014344 // Fall through
14345 case X86::ATOMNAND8:
14346 case X86::ATOMNAND16:
14347 case X86::ATOMNAND32:
14348 case X86::ATOMNAND64:
14349 // Fall through
Michael Liaofe87c302012-09-21 03:18:52 +000014350 case X86::ATOMMAX8:
Michael Liaob118a072012-09-20 03:06:15 +000014351 case X86::ATOMMAX16:
14352 case X86::ATOMMAX32:
14353 case X86::ATOMMAX64:
14354 // Fall through
Michael Liaofe87c302012-09-21 03:18:52 +000014355 case X86::ATOMMIN8:
Michael Liaob118a072012-09-20 03:06:15 +000014356 case X86::ATOMMIN16:
14357 case X86::ATOMMIN32:
14358 case X86::ATOMMIN64:
14359 // Fall through
Michael Liaofe87c302012-09-21 03:18:52 +000014360 case X86::ATOMUMAX8:
Michael Liaob118a072012-09-20 03:06:15 +000014361 case X86::ATOMUMAX16:
14362 case X86::ATOMUMAX32:
14363 case X86::ATOMUMAX64:
14364 // Fall through
Michael Liaofe87c302012-09-21 03:18:52 +000014365 case X86::ATOMUMIN8:
Michael Liaob118a072012-09-20 03:06:15 +000014366 case X86::ATOMUMIN16:
14367 case X86::ATOMUMIN32:
14368 case X86::ATOMUMIN64:
14369 return EmitAtomicLoadArith(MI, BB);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000014370
14371 // This group does 64-bit operations on a 32-bit host.
14372 case X86::ATOMAND6432:
Dale Johannesen48c1bc22008-10-02 18:53:47 +000014373 case X86::ATOMOR6432:
Dale Johannesen48c1bc22008-10-02 18:53:47 +000014374 case X86::ATOMXOR6432:
Dale Johannesen48c1bc22008-10-02 18:53:47 +000014375 case X86::ATOMNAND6432:
Dale Johannesen48c1bc22008-10-02 18:53:47 +000014376 case X86::ATOMADD6432:
Dale Johannesen48c1bc22008-10-02 18:53:47 +000014377 case X86::ATOMSUB6432:
Michael Liaoe5e8f762012-09-25 18:08:13 +000014378 case X86::ATOMMAX6432:
14379 case X86::ATOMMIN6432:
14380 case X86::ATOMUMAX6432:
14381 case X86::ATOMUMIN6432:
Michael Liaob118a072012-09-20 03:06:15 +000014382 case X86::ATOMSWAP6432:
14383 return EmitAtomicLoadArith6432(MI, BB);
Craig Topperacaaa6f2012-08-18 06:39:34 +000014384
Dan Gohmand6708ea2009-08-15 01:38:56 +000014385 case X86::VASTART_SAVE_XMM_REGS:
14386 return EmitVAStartSaveXMMRegsWithCustomInserter(MI, BB);
Dan Gohman320afb82010-10-12 18:00:49 +000014387
14388 case X86::VAARG_64:
14389 return EmitVAARG64WithCustomInserter(MI, BB);
Michael Liao6c0e04c2012-10-15 22:39:43 +000014390
14391 case X86::EH_SjLj_SetJmp32:
14392 case X86::EH_SjLj_SetJmp64:
14393 return emitEHSjLjSetJmp(MI, BB);
14394
14395 case X86::EH_SjLj_LongJmp32:
14396 case X86::EH_SjLj_LongJmp64:
14397 return emitEHSjLjLongJmp(MI, BB);
Evan Cheng60c07e12006-07-05 22:17:51 +000014398 }
14399}
14400
14401//===----------------------------------------------------------------------===//
14402// X86 Optimization Hooks
14403//===----------------------------------------------------------------------===//
14404
Dan Gohman475871a2008-07-27 21:46:04 +000014405void X86TargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
Dan Gohmanfd29e0e2008-02-13 00:35:47 +000014406 APInt &KnownZero,
14407 APInt &KnownOne,
Dan Gohmanea859be2007-06-22 14:59:07 +000014408 const SelectionDAG &DAG,
Nate Begeman368e18d2006-02-16 21:11:51 +000014409 unsigned Depth) const {
Rafael Espindola26c8dcc2012-04-04 12:51:34 +000014410 unsigned BitWidth = KnownZero.getBitWidth();
Evan Cheng3a03ebb2005-12-21 23:05:39 +000014411 unsigned Opc = Op.getOpcode();
Evan Cheng865f0602006-04-05 06:11:20 +000014412 assert((Opc >= ISD::BUILTIN_OP_END ||
14413 Opc == ISD::INTRINSIC_WO_CHAIN ||
14414 Opc == ISD::INTRINSIC_W_CHAIN ||
14415 Opc == ISD::INTRINSIC_VOID) &&
14416 "Should use MaskedValueIsZero if you don't know whether Op"
14417 " is a target node!");
Evan Cheng3a03ebb2005-12-21 23:05:39 +000014418
Rafael Espindola26c8dcc2012-04-04 12:51:34 +000014419 KnownZero = KnownOne = APInt(BitWidth, 0); // Don't know anything.
Evan Cheng3a03ebb2005-12-21 23:05:39 +000014420 switch (Opc) {
Evan Cheng865f0602006-04-05 06:11:20 +000014421 default: break;
Evan Cheng97d0e0e2009-02-02 09:15:04 +000014422 case X86ISD::ADD:
14423 case X86ISD::SUB:
Chris Lattner5b856542010-12-20 00:59:46 +000014424 case X86ISD::ADC:
14425 case X86ISD::SBB:
Evan Cheng97d0e0e2009-02-02 09:15:04 +000014426 case X86ISD::SMUL:
14427 case X86ISD::UMUL:
Dan Gohman076aee32009-03-04 19:44:21 +000014428 case X86ISD::INC:
14429 case X86ISD::DEC:
Dan Gohmane220c4b2009-09-18 19:59:53 +000014430 case X86ISD::OR:
14431 case X86ISD::XOR:
14432 case X86ISD::AND:
Evan Cheng97d0e0e2009-02-02 09:15:04 +000014433 // These nodes' second result is a boolean.
14434 if (Op.getResNo() == 0)
14435 break;
14436 // Fallthrough
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000014437 case X86ISD::SETCC:
Rafael Espindola26c8dcc2012-04-04 12:51:34 +000014438 KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - 1);
Nate Begeman368e18d2006-02-16 21:11:51 +000014439 break;
Evan Cheng7c1780c2011-10-07 17:21:44 +000014440 case ISD::INTRINSIC_WO_CHAIN: {
14441 unsigned IntId = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
14442 unsigned NumLoBits = 0;
14443 switch (IntId) {
14444 default: break;
14445 case Intrinsic::x86_sse_movmsk_ps:
14446 case Intrinsic::x86_avx_movmsk_ps_256:
14447 case Intrinsic::x86_sse2_movmsk_pd:
14448 case Intrinsic::x86_avx_movmsk_pd_256:
14449 case Intrinsic::x86_mmx_pmovmskb:
Craig Topper3738ccd2011-12-27 06:27:23 +000014450 case Intrinsic::x86_sse2_pmovmskb_128:
14451 case Intrinsic::x86_avx2_pmovmskb: {
Evan Cheng7c1780c2011-10-07 17:21:44 +000014452 // High bits of movmskp{s|d}, pmovmskb are known zero.
14453 switch (IntId) {
Craig Topperabb94d02012-02-05 03:43:23 +000014454 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
Evan Cheng7c1780c2011-10-07 17:21:44 +000014455 case Intrinsic::x86_sse_movmsk_ps: NumLoBits = 4; break;
14456 case Intrinsic::x86_avx_movmsk_ps_256: NumLoBits = 8; break;
14457 case Intrinsic::x86_sse2_movmsk_pd: NumLoBits = 2; break;
14458 case Intrinsic::x86_avx_movmsk_pd_256: NumLoBits = 4; break;
14459 case Intrinsic::x86_mmx_pmovmskb: NumLoBits = 8; break;
14460 case Intrinsic::x86_sse2_pmovmskb_128: NumLoBits = 16; break;
Craig Topper3738ccd2011-12-27 06:27:23 +000014461 case Intrinsic::x86_avx2_pmovmskb: NumLoBits = 32; break;
Evan Cheng7c1780c2011-10-07 17:21:44 +000014462 }
Rafael Espindola26c8dcc2012-04-04 12:51:34 +000014463 KnownZero = APInt::getHighBitsSet(BitWidth, BitWidth - NumLoBits);
Evan Cheng7c1780c2011-10-07 17:21:44 +000014464 break;
14465 }
14466 }
14467 break;
14468 }
Evan Cheng3a03ebb2005-12-21 23:05:39 +000014469 }
Evan Cheng3a03ebb2005-12-21 23:05:39 +000014470}
Chris Lattner259e97c2006-01-31 19:43:35 +000014471
Owen Andersonbc146b02010-09-21 20:42:50 +000014472unsigned X86TargetLowering::ComputeNumSignBitsForTargetNode(SDValue Op,
14473 unsigned Depth) const {
14474 // SETCC_CARRY sets the dest to ~0 for true or 0 for false.
14475 if (Op.getOpcode() == X86ISD::SETCC_CARRY)
14476 return Op.getValueType().getScalarType().getSizeInBits();
Michael J. Spencerec38de22010-10-10 22:04:20 +000014477
Owen Andersonbc146b02010-09-21 20:42:50 +000014478 // Fallback case.
14479 return 1;
14480}
14481
Evan Cheng206ee9d2006-07-07 08:33:52 +000014482/// isGAPlusOffset - Returns true (and the GlobalValue and the offset) if the
Evan Chengad4196b2008-05-12 19:56:52 +000014483/// node is a GlobalAddress + offset.
14484bool X86TargetLowering::isGAPlusOffset(SDNode *N,
Dan Gohman46510a72010-04-15 01:51:59 +000014485 const GlobalValue* &GA,
14486 int64_t &Offset) const {
Evan Chengad4196b2008-05-12 19:56:52 +000014487 if (N->getOpcode() == X86ISD::Wrapper) {
14488 if (isa<GlobalAddressSDNode>(N->getOperand(0))) {
Evan Cheng206ee9d2006-07-07 08:33:52 +000014489 GA = cast<GlobalAddressSDNode>(N->getOperand(0))->getGlobal();
Dan Gohman6520e202008-10-18 02:06:02 +000014490 Offset = cast<GlobalAddressSDNode>(N->getOperand(0))->getOffset();
Evan Cheng206ee9d2006-07-07 08:33:52 +000014491 return true;
14492 }
Evan Cheng206ee9d2006-07-07 08:33:52 +000014493 }
Evan Chengad4196b2008-05-12 19:56:52 +000014494 return TargetLowering::isGAPlusOffset(N, GA, Offset);
Evan Cheng206ee9d2006-07-07 08:33:52 +000014495}
14496
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000014497/// isShuffleHigh128VectorInsertLow - Checks whether the shuffle node is the
14498/// same as extracting the high 128-bit part of 256-bit vector and then
14499/// inserting the result into the low part of a new 256-bit vector
14500static bool isShuffleHigh128VectorInsertLow(ShuffleVectorSDNode *SVOp) {
14501 EVT VT = SVOp->getValueType(0);
Craig Topper66ddd152012-04-27 22:54:43 +000014502 unsigned NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000014503
14504 // vector_shuffle <4, 5, 6, 7, u, u, u, u> or <2, 3, u, u>
Craig Topper66ddd152012-04-27 22:54:43 +000014505 for (unsigned i = 0, j = NumElems/2; i != NumElems/2; ++i, ++j)
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000014506 if (!isUndefOrEqual(SVOp->getMaskElt(i), j) ||
14507 SVOp->getMaskElt(j) >= 0)
14508 return false;
14509
14510 return true;
14511}
14512
14513/// isShuffleLow128VectorInsertHigh - Checks whether the shuffle node is the
14514/// same as extracting the low 128-bit part of 256-bit vector and then
14515/// inserting the result into the high part of a new 256-bit vector
14516static bool isShuffleLow128VectorInsertHigh(ShuffleVectorSDNode *SVOp) {
14517 EVT VT = SVOp->getValueType(0);
Craig Topper66ddd152012-04-27 22:54:43 +000014518 unsigned NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000014519
14520 // vector_shuffle <u, u, u, u, 0, 1, 2, 3> or <u, u, 0, 1>
Craig Topper66ddd152012-04-27 22:54:43 +000014521 for (unsigned i = NumElems/2, j = 0; i != NumElems; ++i, ++j)
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000014522 if (!isUndefOrEqual(SVOp->getMaskElt(i), j) ||
14523 SVOp->getMaskElt(j) >= 0)
14524 return false;
14525
14526 return true;
14527}
14528
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000014529/// PerformShuffleCombine256 - Performs shuffle combines for 256-bit vectors.
14530static SDValue PerformShuffleCombine256(SDNode *N, SelectionDAG &DAG,
Craig Topper12216172012-01-13 08:12:35 +000014531 TargetLowering::DAGCombinerInfo &DCI,
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +000014532 const X86Subtarget* Subtarget) {
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000014533 DebugLoc dl = N->getDebugLoc();
14534 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
14535 SDValue V1 = SVOp->getOperand(0);
14536 SDValue V2 = SVOp->getOperand(1);
14537 EVT VT = SVOp->getValueType(0);
Craig Topper66ddd152012-04-27 22:54:43 +000014538 unsigned NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000014539
14540 if (V1.getOpcode() == ISD::CONCAT_VECTORS &&
14541 V2.getOpcode() == ISD::CONCAT_VECTORS) {
14542 //
14543 // 0,0,0,...
Benjamin Kramer558cc5a2011-07-22 01:02:57 +000014544 // |
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000014545 // V UNDEF BUILD_VECTOR UNDEF
14546 // \ / \ /
14547 // CONCAT_VECTOR CONCAT_VECTOR
14548 // \ /
14549 // \ /
14550 // RESULT: V + zero extended
14551 //
14552 if (V2.getOperand(0).getOpcode() != ISD::BUILD_VECTOR ||
14553 V2.getOperand(1).getOpcode() != ISD::UNDEF ||
14554 V1.getOperand(1).getOpcode() != ISD::UNDEF)
14555 return SDValue();
14556
14557 if (!ISD::isBuildVectorAllZeros(V2.getOperand(0).getNode()))
14558 return SDValue();
14559
14560 // To match the shuffle mask, the first half of the mask should
14561 // be exactly the first vector, and all the rest a splat with the
14562 // first element of the second one.
Craig Topper66ddd152012-04-27 22:54:43 +000014563 for (unsigned i = 0; i != NumElems/2; ++i)
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000014564 if (!isUndefOrEqual(SVOp->getMaskElt(i), i) ||
14565 !isUndefOrEqual(SVOp->getMaskElt(i+NumElems/2), NumElems))
14566 return SDValue();
14567
Chad Rosier3d1161e2012-01-03 21:05:52 +000014568 // If V1 is coming from a vector load then just fold to a VZEXT_LOAD.
14569 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(V1.getOperand(0))) {
Chad Rosier42726832012-05-07 18:47:44 +000014570 if (Ld->hasNUsesOfValue(1, 0)) {
14571 SDVTList Tys = DAG.getVTList(MVT::v4i64, MVT::Other);
14572 SDValue Ops[] = { Ld->getChain(), Ld->getBasePtr() };
14573 SDValue ResNode =
14574 DAG.getMemIntrinsicNode(X86ISD::VZEXT_LOAD, dl, Tys, Ops, 2,
14575 Ld->getMemoryVT(),
14576 Ld->getPointerInfo(),
14577 Ld->getAlignment(),
14578 false/*isVolatile*/, true/*ReadMem*/,
14579 false/*WriteMem*/);
Manman Ren2adc5032012-11-13 19:13:05 +000014580
14581 // Make sure the newly-created LOAD is in the same position as Ld in
14582 // terms of dependency. We create a TokenFactor for Ld and ResNode,
14583 // and update uses of Ld's output chain to use the TokenFactor.
14584 if (Ld->hasAnyUseOfValue(1)) {
14585 SDValue NewChain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
14586 SDValue(Ld, 1), SDValue(ResNode.getNode(), 1));
14587 DAG.ReplaceAllUsesOfValueWith(SDValue(Ld, 1), NewChain);
14588 DAG.UpdateNodeOperands(NewChain.getNode(), SDValue(Ld, 1),
14589 SDValue(ResNode.getNode(), 1));
14590 }
14591
Chad Rosier42726832012-05-07 18:47:44 +000014592 return DAG.getNode(ISD::BITCAST, dl, VT, ResNode);
14593 }
Chad Rosiera20e1e72012-08-01 18:39:17 +000014594 }
Chad Rosier3d1161e2012-01-03 21:05:52 +000014595
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000014596 // Emit a zeroed vector and insert the desired subvector on its
14597 // first half.
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +000014598 SDValue Zeros = getZeroVector(VT, Subtarget, DAG, dl);
Craig Topperb14940a2012-04-22 20:55:18 +000014599 SDValue InsV = Insert128BitVector(Zeros, V1.getOperand(0), 0, DAG, dl);
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000014600 return DCI.CombineTo(N, InsV);
14601 }
14602
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000014603 //===--------------------------------------------------------------------===//
14604 // Combine some shuffles into subvector extracts and inserts:
14605 //
14606
14607 // vector_shuffle <4, 5, 6, 7, u, u, u, u> or <2, 3, u, u>
14608 if (isShuffleHigh128VectorInsertLow(SVOp)) {
Craig Topperb14940a2012-04-22 20:55:18 +000014609 SDValue V = Extract128BitVector(V1, NumElems/2, DAG, dl);
14610 SDValue InsV = Insert128BitVector(DAG.getUNDEF(VT), V, 0, DAG, dl);
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000014611 return DCI.CombineTo(N, InsV);
14612 }
14613
14614 // vector_shuffle <u, u, u, u, 0, 1, 2, 3> or <u, u, 0, 1>
14615 if (isShuffleLow128VectorInsertHigh(SVOp)) {
Craig Topperb14940a2012-04-22 20:55:18 +000014616 SDValue V = Extract128BitVector(V1, 0, DAG, dl);
14617 SDValue InsV = Insert128BitVector(DAG.getUNDEF(VT), V, NumElems/2, DAG, dl);
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000014618 return DCI.CombineTo(N, InsV);
14619 }
14620
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000014621 return SDValue();
14622}
14623
14624/// PerformShuffleCombine - Performs several different shuffle combines.
Dan Gohman475871a2008-07-27 21:46:04 +000014625static SDValue PerformShuffleCombine(SDNode *N, SelectionDAG &DAG,
Bruno Cardoso Lopes50b37c72011-08-15 21:45:54 +000014626 TargetLowering::DAGCombinerInfo &DCI,
14627 const X86Subtarget *Subtarget) {
Dale Johannesene4d209d2009-02-03 20:21:25 +000014628 DebugLoc dl = N->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +000014629 EVT VT = N->getValueType(0);
Mon P Wang1e955802009-04-03 02:43:30 +000014630
Mon P Wanga0fd0d52010-12-19 23:55:53 +000014631 // Don't create instructions with illegal types after legalize types has run.
14632 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
14633 if (!DCI.isBeforeLegalize() && !TLI.isTypeLegal(VT.getVectorElementType()))
14634 return SDValue();
14635
Bruno Cardoso Lopes50b37c72011-08-15 21:45:54 +000014636 // Combine 256-bit vector shuffles. This is only profitable when in AVX mode
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000014637 if (Subtarget->hasFp256() && VT.is256BitVector() &&
Bruno Cardoso Lopes50b37c72011-08-15 21:45:54 +000014638 N->getOpcode() == ISD::VECTOR_SHUFFLE)
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +000014639 return PerformShuffleCombine256(N, DAG, DCI, Subtarget);
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000014640
14641 // Only handle 128 wide vector from here on.
Craig Topper7a9a28b2012-08-12 02:23:29 +000014642 if (!VT.is128BitVector())
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000014643 return SDValue();
14644
14645 // Combine a vector_shuffle that is equal to build_vector load1, load2, load3,
14646 // load4, <0, 1, 2, 3> into a 128-bit load if the load addresses are
14647 // consecutive, non-overlapping, and in the right order.
Nate Begemanfdea31a2010-03-24 20:49:50 +000014648 SmallVector<SDValue, 16> Elts;
14649 for (unsigned i = 0, e = VT.getVectorNumElements(); i != e; ++i)
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000014650 Elts.push_back(getShuffleScalarElt(N, i, DAG, 0));
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +000014651
Nate Begemanfdea31a2010-03-24 20:49:50 +000014652 return EltsFromConsecutiveLoads(VT, Elts, dl, DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +000014653}
Evan Chengd880b972008-05-09 21:53:03 +000014654
Craig Topper55b24052012-09-11 06:15:32 +000014655/// PerformTruncateCombine - Converts truncate operation to
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000014656/// a sequence of vector shuffle operations.
14657/// It is possible when we truncate 256-bit vector to 128-bit vector
Craig Topper55b24052012-09-11 06:15:32 +000014658static SDValue PerformTruncateCombine(SDNode *N, SelectionDAG &DAG,
14659 TargetLowering::DAGCombinerInfo &DCI,
14660 const X86Subtarget *Subtarget) {
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000014661 return SDValue();
14662}
14663
Craig Topper89f4e662012-03-20 07:17:59 +000014664/// XFormVExtractWithShuffleIntoLoad - Check if a vector extract from a target
14665/// specific shuffle of a load can be folded into a single element load.
14666/// Similar handling for VECTOR_SHUFFLE is performed by DAGCombiner, but
14667/// shuffles have been customed lowered so we need to handle those here.
14668static SDValue XFormVExtractWithShuffleIntoLoad(SDNode *N, SelectionDAG &DAG,
14669 TargetLowering::DAGCombinerInfo &DCI) {
14670 if (DCI.isBeforeLegalizeOps())
14671 return SDValue();
14672
14673 SDValue InVec = N->getOperand(0);
14674 SDValue EltNo = N->getOperand(1);
14675
14676 if (!isa<ConstantSDNode>(EltNo))
14677 return SDValue();
14678
14679 EVT VT = InVec.getValueType();
14680
14681 bool HasShuffleIntoBitcast = false;
14682 if (InVec.getOpcode() == ISD::BITCAST) {
14683 // Don't duplicate a load with other uses.
14684 if (!InVec.hasOneUse())
14685 return SDValue();
14686 EVT BCVT = InVec.getOperand(0).getValueType();
14687 if (BCVT.getVectorNumElements() != VT.getVectorNumElements())
14688 return SDValue();
14689 InVec = InVec.getOperand(0);
14690 HasShuffleIntoBitcast = true;
14691 }
14692
14693 if (!isTargetShuffle(InVec.getOpcode()))
14694 return SDValue();
14695
14696 // Don't duplicate a load with other uses.
14697 if (!InVec.hasOneUse())
14698 return SDValue();
14699
14700 SmallVector<int, 16> ShuffleMask;
14701 bool UnaryShuffle;
Craig Topperd978c542012-05-06 19:46:21 +000014702 if (!getTargetShuffleMask(InVec.getNode(), VT.getSimpleVT(), ShuffleMask,
14703 UnaryShuffle))
Craig Topper89f4e662012-03-20 07:17:59 +000014704 return SDValue();
14705
14706 // Select the input vector, guarding against out of range extract vector.
14707 unsigned NumElems = VT.getVectorNumElements();
14708 int Elt = cast<ConstantSDNode>(EltNo)->getZExtValue();
14709 int Idx = (Elt > (int)NumElems) ? -1 : ShuffleMask[Elt];
14710 SDValue LdNode = (Idx < (int)NumElems) ? InVec.getOperand(0)
14711 : InVec.getOperand(1);
14712
14713 // If inputs to shuffle are the same for both ops, then allow 2 uses
14714 unsigned AllowedUses = InVec.getOperand(0) == InVec.getOperand(1) ? 2 : 1;
14715
14716 if (LdNode.getOpcode() == ISD::BITCAST) {
14717 // Don't duplicate a load with other uses.
14718 if (!LdNode.getNode()->hasNUsesOfValue(AllowedUses, 0))
14719 return SDValue();
14720
14721 AllowedUses = 1; // only allow 1 load use if we have a bitcast
14722 LdNode = LdNode.getOperand(0);
14723 }
14724
14725 if (!ISD::isNormalLoad(LdNode.getNode()))
14726 return SDValue();
14727
14728 LoadSDNode *LN0 = cast<LoadSDNode>(LdNode);
14729
14730 if (!LN0 ||!LN0->hasNUsesOfValue(AllowedUses, 0) || LN0->isVolatile())
14731 return SDValue();
14732
14733 if (HasShuffleIntoBitcast) {
14734 // If there's a bitcast before the shuffle, check if the load type and
14735 // alignment is valid.
14736 unsigned Align = LN0->getAlignment();
14737 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Micah Villmow3574eca2012-10-08 16:38:25 +000014738 unsigned NewAlign = TLI.getDataLayout()->
Craig Topper89f4e662012-03-20 07:17:59 +000014739 getABITypeAlignment(VT.getTypeForEVT(*DAG.getContext()));
14740
14741 if (NewAlign > Align || !TLI.isOperationLegalOrCustom(ISD::LOAD, VT))
14742 return SDValue();
14743 }
14744
14745 // All checks match so transform back to vector_shuffle so that DAG combiner
14746 // can finish the job
14747 DebugLoc dl = N->getDebugLoc();
14748
14749 // Create shuffle node taking into account the case that its a unary shuffle
14750 SDValue Shuffle = (UnaryShuffle) ? DAG.getUNDEF(VT) : InVec.getOperand(1);
14751 Shuffle = DAG.getVectorShuffle(InVec.getValueType(), dl,
14752 InVec.getOperand(0), Shuffle,
14753 &ShuffleMask[0]);
14754 Shuffle = DAG.getNode(ISD::BITCAST, dl, VT, Shuffle);
14755 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, N->getValueType(0), Shuffle,
14756 EltNo);
14757}
14758
Bruno Cardoso Lopesb3e06692010-09-03 19:55:05 +000014759/// PerformEXTRACT_VECTOR_ELTCombine - Detect vector gather/scatter index
14760/// generation and convert it from being a bunch of shuffles and extracts
14761/// to a simple store and scalar loads to extract the elements.
Dan Gohman1bbf72b2010-03-15 23:23:03 +000014762static SDValue PerformEXTRACT_VECTOR_ELTCombine(SDNode *N, SelectionDAG &DAG,
Craig Topper89f4e662012-03-20 07:17:59 +000014763 TargetLowering::DAGCombinerInfo &DCI) {
14764 SDValue NewOp = XFormVExtractWithShuffleIntoLoad(N, DAG, DCI);
14765 if (NewOp.getNode())
14766 return NewOp;
14767
Dan Gohman1bbf72b2010-03-15 23:23:03 +000014768 SDValue InputVector = N->getOperand(0);
Manman Ren4c74a952012-10-30 22:15:38 +000014769 // Detect whether we are trying to convert from mmx to i32 and the bitcast
14770 // from mmx to v2i32 has a single usage.
14771 if (InputVector.getNode()->getOpcode() == llvm::ISD::BITCAST &&
14772 InputVector.getNode()->getOperand(0).getValueType() == MVT::x86mmx &&
14773 InputVector.hasOneUse() && N->getValueType(0) == MVT::i32)
14774 return DAG.getNode(X86ISD::MMX_MOVD2W, InputVector.getDebugLoc(),
14775 N->getValueType(0),
14776 InputVector.getNode()->getOperand(0));
Dan Gohman1bbf72b2010-03-15 23:23:03 +000014777
14778 // Only operate on vectors of 4 elements, where the alternative shuffling
14779 // gets to be more expensive.
14780 if (InputVector.getValueType() != MVT::v4i32)
14781 return SDValue();
14782
14783 // Check whether every use of InputVector is an EXTRACT_VECTOR_ELT with a
14784 // single use which is a sign-extend or zero-extend, and all elements are
14785 // used.
14786 SmallVector<SDNode *, 4> Uses;
14787 unsigned ExtractedElements = 0;
14788 for (SDNode::use_iterator UI = InputVector.getNode()->use_begin(),
14789 UE = InputVector.getNode()->use_end(); UI != UE; ++UI) {
14790 if (UI.getUse().getResNo() != InputVector.getResNo())
14791 return SDValue();
14792
14793 SDNode *Extract = *UI;
14794 if (Extract->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
14795 return SDValue();
14796
14797 if (Extract->getValueType(0) != MVT::i32)
14798 return SDValue();
14799 if (!Extract->hasOneUse())
14800 return SDValue();
14801 if (Extract->use_begin()->getOpcode() != ISD::SIGN_EXTEND &&
14802 Extract->use_begin()->getOpcode() != ISD::ZERO_EXTEND)
14803 return SDValue();
14804 if (!isa<ConstantSDNode>(Extract->getOperand(1)))
14805 return SDValue();
14806
14807 // Record which element was extracted.
14808 ExtractedElements |=
14809 1 << cast<ConstantSDNode>(Extract->getOperand(1))->getZExtValue();
14810
14811 Uses.push_back(Extract);
14812 }
14813
14814 // If not all the elements were used, this may not be worthwhile.
14815 if (ExtractedElements != 15)
14816 return SDValue();
14817
14818 // Ok, we've now decided to do the transformation.
14819 DebugLoc dl = InputVector.getDebugLoc();
14820
14821 // Store the value to a temporary stack slot.
14822 SDValue StackPtr = DAG.CreateStackTemporary(InputVector.getValueType());
Chris Lattner8026a9d2010-09-21 17:50:43 +000014823 SDValue Ch = DAG.getStore(DAG.getEntryNode(), dl, InputVector, StackPtr,
14824 MachinePointerInfo(), false, false, 0);
Dan Gohman1bbf72b2010-03-15 23:23:03 +000014825
14826 // Replace each use (extract) with a load of the appropriate element.
14827 for (SmallVectorImpl<SDNode *>::iterator UI = Uses.begin(),
14828 UE = Uses.end(); UI != UE; ++UI) {
14829 SDNode *Extract = *UI;
14830
Nadav Rotem86694292011-05-17 08:31:57 +000014831 // cOMpute the element's address.
Dan Gohman1bbf72b2010-03-15 23:23:03 +000014832 SDValue Idx = Extract->getOperand(1);
14833 unsigned EltSize =
14834 InputVector.getValueType().getVectorElementType().getSizeInBits()/8;
14835 uint64_t Offset = EltSize * cast<ConstantSDNode>(Idx)->getZExtValue();
Craig Topper89f4e662012-03-20 07:17:59 +000014836 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Dan Gohman1bbf72b2010-03-15 23:23:03 +000014837 SDValue OffsetVal = DAG.getConstant(Offset, TLI.getPointerTy());
14838
Nadav Rotem86694292011-05-17 08:31:57 +000014839 SDValue ScalarAddr = DAG.getNode(ISD::ADD, dl, TLI.getPointerTy(),
Chris Lattner51abfe42010-09-21 06:02:19 +000014840 StackPtr, OffsetVal);
Dan Gohman1bbf72b2010-03-15 23:23:03 +000014841
14842 // Load the scalar.
Eric Christopher90eb4022010-07-22 00:26:08 +000014843 SDValue LoadScalar = DAG.getLoad(Extract->getValueType(0), dl, Ch,
Chris Lattner51abfe42010-09-21 06:02:19 +000014844 ScalarAddr, MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000014845 false, false, false, 0);
Dan Gohman1bbf72b2010-03-15 23:23:03 +000014846
14847 // Replace the exact with the load.
14848 DAG.ReplaceAllUsesOfValueWith(SDValue(Extract, 0), LoadScalar);
14849 }
14850
14851 // The replacement was made in place; don't return anything.
14852 return SDValue();
14853}
14854
Benjamin Kramer2556c6b2012-12-21 17:46:58 +000014855/// \brief Matches a VSELECT onto min/max or return 0 if the node doesn't match.
14856static unsigned matchIntegerMINMAX(SDValue Cond, EVT VT, SDValue LHS,
14857 SDValue RHS, SelectionDAG &DAG,
14858 const X86Subtarget *Subtarget) {
14859 if (!VT.isVector())
14860 return 0;
14861
14862 switch (VT.getSimpleVT().SimpleTy) {
14863 default: return 0;
14864 case MVT::v32i8:
14865 case MVT::v16i16:
14866 case MVT::v8i32:
14867 if (!Subtarget->hasAVX2())
14868 return 0;
14869 case MVT::v16i8:
14870 case MVT::v8i16:
14871 case MVT::v4i32:
14872 if (!Subtarget->hasSSE2())
14873 return 0;
14874 }
14875
14876 // SSE2 has only a small subset of the operations.
14877 bool hasUnsigned = Subtarget->hasSSE41() ||
14878 (Subtarget->hasSSE2() && VT == MVT::v16i8);
14879 bool hasSigned = Subtarget->hasSSE41() ||
14880 (Subtarget->hasSSE2() && VT == MVT::v8i16);
14881
14882 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
14883
14884 // Check for x CC y ? x : y.
14885 if (DAG.isEqualTo(LHS, Cond.getOperand(0)) &&
14886 DAG.isEqualTo(RHS, Cond.getOperand(1))) {
14887 switch (CC) {
14888 default: break;
14889 case ISD::SETULT:
14890 case ISD::SETULE:
14891 return hasUnsigned ? X86ISD::UMIN : 0;
14892 case ISD::SETUGT:
14893 case ISD::SETUGE:
14894 return hasUnsigned ? X86ISD::UMAX : 0;
14895 case ISD::SETLT:
14896 case ISD::SETLE:
14897 return hasSigned ? X86ISD::SMIN : 0;
14898 case ISD::SETGT:
14899 case ISD::SETGE:
14900 return hasSigned ? X86ISD::SMAX : 0;
14901 }
14902 // Check for x CC y ? y : x -- a min/max with reversed arms.
14903 } else if (DAG.isEqualTo(LHS, Cond.getOperand(1)) &&
14904 DAG.isEqualTo(RHS, Cond.getOperand(0))) {
14905 switch (CC) {
14906 default: break;
14907 case ISD::SETULT:
14908 case ISD::SETULE:
14909 return hasUnsigned ? X86ISD::UMAX : 0;
14910 case ISD::SETUGT:
14911 case ISD::SETUGE:
14912 return hasUnsigned ? X86ISD::UMIN : 0;
14913 case ISD::SETLT:
14914 case ISD::SETLE:
14915 return hasSigned ? X86ISD::SMAX : 0;
14916 case ISD::SETGT:
14917 case ISD::SETGE:
14918 return hasSigned ? X86ISD::SMIN : 0;
14919 }
14920 }
14921
14922 return 0;
14923}
14924
Duncan Sands6bcd2192011-09-17 16:49:39 +000014925/// PerformSELECTCombine - Do target-specific dag combines on SELECT and VSELECT
14926/// nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000014927static SDValue PerformSELECTCombine(SDNode *N, SelectionDAG &DAG,
Nadav Rotemcc616562012-01-15 19:27:55 +000014928 TargetLowering::DAGCombinerInfo &DCI,
Chris Lattner47b4ce82009-03-11 05:48:52 +000014929 const X86Subtarget *Subtarget) {
14930 DebugLoc DL = N->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +000014931 SDValue Cond = N->getOperand(0);
Chris Lattner47b4ce82009-03-11 05:48:52 +000014932 // Get the LHS/RHS of the select.
14933 SDValue LHS = N->getOperand(1);
14934 SDValue RHS = N->getOperand(2);
Bruno Cardoso Lopes149f29f2011-09-20 22:34:45 +000014935 EVT VT = LHS.getValueType();
Eric Christopherfd179292009-08-27 18:07:15 +000014936
Dan Gohman670e5392009-09-21 18:03:22 +000014937 // If we have SSE[12] support, try to form min/max nodes. SSE min/max
Dan Gohman8ce05da2010-02-22 04:03:39 +000014938 // instructions match the semantics of the common C idiom x<y?x:y but not
14939 // x<=y?x:y, because of how they handle negative zero (which can be
14940 // ignored in unsafe-math mode).
Benjamin Kramer2c2ccbf2011-09-22 03:27:22 +000014941 if (Cond.getOpcode() == ISD::SETCC && VT.isFloatingPoint() &&
14942 VT != MVT::f80 && DAG.getTargetLoweringInfo().isTypeLegal(VT) &&
Craig Topper1accb7e2012-01-10 06:54:16 +000014943 (Subtarget->hasSSE2() ||
14944 (Subtarget->hasSSE1() && VT.getScalarType() == MVT::f32))) {
Chris Lattner47b4ce82009-03-11 05:48:52 +000014945 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000014946
Chris Lattner47b4ce82009-03-11 05:48:52 +000014947 unsigned Opcode = 0;
Dan Gohman670e5392009-09-21 18:03:22 +000014948 // Check for x CC y ? x : y.
Dan Gohmane8326932010-02-24 06:52:40 +000014949 if (DAG.isEqualTo(LHS, Cond.getOperand(0)) &&
14950 DAG.isEqualTo(RHS, Cond.getOperand(1))) {
Chris Lattner47b4ce82009-03-11 05:48:52 +000014951 switch (CC) {
14952 default: break;
Dan Gohman670e5392009-09-21 18:03:22 +000014953 case ISD::SETULT:
Dan Gohmane8326932010-02-24 06:52:40 +000014954 // Converting this to a min would handle NaNs incorrectly, and swapping
14955 // the operands would cause it to handle comparisons between positive
14956 // and negative zero incorrectly.
Evan Cheng60108e92010-07-15 22:07:12 +000014957 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)) {
Nick Lewycky8a8d4792011-12-02 22:16:29 +000014958 if (!DAG.getTarget().Options.UnsafeFPMath &&
Dan Gohmane8326932010-02-24 06:52:40 +000014959 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
14960 break;
14961 std::swap(LHS, RHS);
14962 }
Dan Gohman670e5392009-09-21 18:03:22 +000014963 Opcode = X86ISD::FMIN;
14964 break;
14965 case ISD::SETOLE:
Dan Gohmane8326932010-02-24 06:52:40 +000014966 // Converting this to a min would handle comparisons between positive
14967 // and negative zero incorrectly.
Nick Lewycky8a8d4792011-12-02 22:16:29 +000014968 if (!DAG.getTarget().Options.UnsafeFPMath &&
Dan Gohmane8326932010-02-24 06:52:40 +000014969 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(RHS))
14970 break;
Dan Gohman670e5392009-09-21 18:03:22 +000014971 Opcode = X86ISD::FMIN;
14972 break;
Chris Lattner47b4ce82009-03-11 05:48:52 +000014973 case ISD::SETULE:
Dan Gohmane8326932010-02-24 06:52:40 +000014974 // Converting this to a min would handle both negative zeros and NaNs
14975 // incorrectly, but we can swap the operands to fix both.
14976 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000014977 case ISD::SETOLT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000014978 case ISD::SETLT:
Dan Gohman670e5392009-09-21 18:03:22 +000014979 case ISD::SETLE:
Chris Lattner47b4ce82009-03-11 05:48:52 +000014980 Opcode = X86ISD::FMIN;
14981 break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000014982
Dan Gohman670e5392009-09-21 18:03:22 +000014983 case ISD::SETOGE:
Dan Gohmane8326932010-02-24 06:52:40 +000014984 // Converting this to a max would handle comparisons between positive
14985 // and negative zero incorrectly.
Nick Lewycky8a8d4792011-12-02 22:16:29 +000014986 if (!DAG.getTarget().Options.UnsafeFPMath &&
Evan Chengdd5663c2011-08-04 18:38:15 +000014987 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000014988 break;
Dan Gohman670e5392009-09-21 18:03:22 +000014989 Opcode = X86ISD::FMAX;
14990 break;
Chris Lattner47b4ce82009-03-11 05:48:52 +000014991 case ISD::SETUGT:
Dan Gohmane8326932010-02-24 06:52:40 +000014992 // Converting this to a max would handle NaNs incorrectly, and swapping
14993 // the operands would cause it to handle comparisons between positive
14994 // and negative zero incorrectly.
Evan Cheng60108e92010-07-15 22:07:12 +000014995 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)) {
Nick Lewycky8a8d4792011-12-02 22:16:29 +000014996 if (!DAG.getTarget().Options.UnsafeFPMath &&
Dan Gohmane8326932010-02-24 06:52:40 +000014997 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
14998 break;
14999 std::swap(LHS, RHS);
15000 }
Dan Gohman670e5392009-09-21 18:03:22 +000015001 Opcode = X86ISD::FMAX;
15002 break;
15003 case ISD::SETUGE:
Dan Gohmane8326932010-02-24 06:52:40 +000015004 // Converting this to a max would handle both negative zeros and NaNs
15005 // incorrectly, but we can swap the operands to fix both.
15006 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000015007 case ISD::SETOGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000015008 case ISD::SETGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000015009 case ISD::SETGE:
15010 Opcode = X86ISD::FMAX;
15011 break;
Chris Lattner83e6c992006-10-04 06:57:07 +000015012 }
Dan Gohman670e5392009-09-21 18:03:22 +000015013 // Check for x CC y ? y : x -- a min/max with reversed arms.
Dan Gohmane8326932010-02-24 06:52:40 +000015014 } else if (DAG.isEqualTo(LHS, Cond.getOperand(1)) &&
15015 DAG.isEqualTo(RHS, Cond.getOperand(0))) {
Chris Lattner47b4ce82009-03-11 05:48:52 +000015016 switch (CC) {
15017 default: break;
Dan Gohman670e5392009-09-21 18:03:22 +000015018 case ISD::SETOGE:
Dan Gohmane8326932010-02-24 06:52:40 +000015019 // Converting this to a min would handle comparisons between positive
15020 // and negative zero incorrectly, and swapping the operands would
15021 // cause it to handle NaNs incorrectly.
Nick Lewycky8a8d4792011-12-02 22:16:29 +000015022 if (!DAG.getTarget().Options.UnsafeFPMath &&
Dan Gohmane8326932010-02-24 06:52:40 +000015023 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS))) {
Evan Cheng60108e92010-07-15 22:07:12 +000015024 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000015025 break;
15026 std::swap(LHS, RHS);
15027 }
Dan Gohman670e5392009-09-21 18:03:22 +000015028 Opcode = X86ISD::FMIN;
Dan Gohman8d44b282009-09-03 20:34:31 +000015029 break;
Dan Gohman670e5392009-09-21 18:03:22 +000015030 case ISD::SETUGT:
Dan Gohmane8326932010-02-24 06:52:40 +000015031 // Converting this to a min would handle NaNs incorrectly.
Nick Lewycky8a8d4792011-12-02 22:16:29 +000015032 if (!DAG.getTarget().Options.UnsafeFPMath &&
Dan Gohmane8326932010-02-24 06:52:40 +000015033 (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)))
15034 break;
Dan Gohman670e5392009-09-21 18:03:22 +000015035 Opcode = X86ISD::FMIN;
15036 break;
15037 case ISD::SETUGE:
Dan Gohmane8326932010-02-24 06:52:40 +000015038 // Converting this to a min would handle both negative zeros and NaNs
15039 // incorrectly, but we can swap the operands to fix both.
15040 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000015041 case ISD::SETOGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000015042 case ISD::SETGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000015043 case ISD::SETGE:
15044 Opcode = X86ISD::FMIN;
15045 break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000015046
Dan Gohman670e5392009-09-21 18:03:22 +000015047 case ISD::SETULT:
Dan Gohmane8326932010-02-24 06:52:40 +000015048 // Converting this to a max would handle NaNs incorrectly.
Evan Cheng60108e92010-07-15 22:07:12 +000015049 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000015050 break;
Dan Gohman670e5392009-09-21 18:03:22 +000015051 Opcode = X86ISD::FMAX;
Dan Gohman8d44b282009-09-03 20:34:31 +000015052 break;
Dan Gohman670e5392009-09-21 18:03:22 +000015053 case ISD::SETOLE:
Dan Gohmane8326932010-02-24 06:52:40 +000015054 // Converting this to a max would handle comparisons between positive
15055 // and negative zero incorrectly, and swapping the operands would
15056 // cause it to handle NaNs incorrectly.
Nick Lewycky8a8d4792011-12-02 22:16:29 +000015057 if (!DAG.getTarget().Options.UnsafeFPMath &&
Dan Gohmane8326932010-02-24 06:52:40 +000015058 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(RHS)) {
Evan Cheng60108e92010-07-15 22:07:12 +000015059 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000015060 break;
15061 std::swap(LHS, RHS);
15062 }
Dan Gohman670e5392009-09-21 18:03:22 +000015063 Opcode = X86ISD::FMAX;
15064 break;
15065 case ISD::SETULE:
Dan Gohmane8326932010-02-24 06:52:40 +000015066 // Converting this to a max would handle both negative zeros and NaNs
15067 // incorrectly, but we can swap the operands to fix both.
15068 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000015069 case ISD::SETOLT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000015070 case ISD::SETLT:
Dan Gohman670e5392009-09-21 18:03:22 +000015071 case ISD::SETLE:
Chris Lattner47b4ce82009-03-11 05:48:52 +000015072 Opcode = X86ISD::FMAX;
15073 break;
15074 }
Chris Lattner83e6c992006-10-04 06:57:07 +000015075 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000015076
Chris Lattner47b4ce82009-03-11 05:48:52 +000015077 if (Opcode)
15078 return DAG.getNode(Opcode, DL, N->getValueType(0), LHS, RHS);
Chris Lattner83e6c992006-10-04 06:57:07 +000015079 }
Eric Christopherfd179292009-08-27 18:07:15 +000015080
Chris Lattnerd1980a52009-03-12 06:52:53 +000015081 // If this is a select between two integer constants, try to do some
15082 // optimizations.
Chris Lattnercee56e72009-03-13 05:53:31 +000015083 if (ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(LHS)) {
15084 if (ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(RHS))
Chris Lattnerd1980a52009-03-12 06:52:53 +000015085 // Don't do this for crazy integer types.
15086 if (DAG.getTargetLoweringInfo().isTypeLegal(LHS.getValueType())) {
15087 // If this is efficiently invertible, canonicalize the LHSC/RHSC values
Chris Lattnercee56e72009-03-13 05:53:31 +000015088 // so that TrueC (the true value) is larger than FalseC.
Chris Lattnerd1980a52009-03-12 06:52:53 +000015089 bool NeedsCondInvert = false;
Eric Christopherfd179292009-08-27 18:07:15 +000015090
Chris Lattnercee56e72009-03-13 05:53:31 +000015091 if (TrueC->getAPIntValue().ult(FalseC->getAPIntValue()) &&
Chris Lattnerd1980a52009-03-12 06:52:53 +000015092 // Efficiently invertible.
15093 (Cond.getOpcode() == ISD::SETCC || // setcc -> invertible.
15094 (Cond.getOpcode() == ISD::XOR && // xor(X, C) -> invertible.
15095 isa<ConstantSDNode>(Cond.getOperand(1))))) {
15096 NeedsCondInvert = true;
Chris Lattnercee56e72009-03-13 05:53:31 +000015097 std::swap(TrueC, FalseC);
Chris Lattnerd1980a52009-03-12 06:52:53 +000015098 }
Eric Christopherfd179292009-08-27 18:07:15 +000015099
Chris Lattnerd1980a52009-03-12 06:52:53 +000015100 // Optimize C ? 8 : 0 -> zext(C) << 3. Likewise for any pow2/0.
Chris Lattnercee56e72009-03-13 05:53:31 +000015101 if (FalseC->getAPIntValue() == 0 &&
15102 TrueC->getAPIntValue().isPowerOf2()) {
Chris Lattnerd1980a52009-03-12 06:52:53 +000015103 if (NeedsCondInvert) // Invert the condition if needed.
15104 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
15105 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000015106
Chris Lattnerd1980a52009-03-12 06:52:53 +000015107 // Zero extend the condition if needed.
15108 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, LHS.getValueType(), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000015109
Chris Lattnercee56e72009-03-13 05:53:31 +000015110 unsigned ShAmt = TrueC->getAPIntValue().logBase2();
Chris Lattnerd1980a52009-03-12 06:52:53 +000015111 return DAG.getNode(ISD::SHL, DL, LHS.getValueType(), Cond,
Owen Anderson825b72b2009-08-11 20:47:22 +000015112 DAG.getConstant(ShAmt, MVT::i8));
Chris Lattnerd1980a52009-03-12 06:52:53 +000015113 }
Eric Christopherfd179292009-08-27 18:07:15 +000015114
Chris Lattner97a29a52009-03-13 05:22:11 +000015115 // Optimize Cond ? cst+1 : cst -> zext(setcc(C)+cst.
Chris Lattnercee56e72009-03-13 05:53:31 +000015116 if (FalseC->getAPIntValue()+1 == TrueC->getAPIntValue()) {
Chris Lattner97a29a52009-03-13 05:22:11 +000015117 if (NeedsCondInvert) // Invert the condition if needed.
15118 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
15119 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000015120
Chris Lattner97a29a52009-03-13 05:22:11 +000015121 // Zero extend the condition if needed.
Chris Lattnercee56e72009-03-13 05:53:31 +000015122 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL,
15123 FalseC->getValueType(0), Cond);
Chris Lattner97a29a52009-03-13 05:22:11 +000015124 return DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
Chris Lattnercee56e72009-03-13 05:53:31 +000015125 SDValue(FalseC, 0));
Chris Lattner97a29a52009-03-13 05:22:11 +000015126 }
Eric Christopherfd179292009-08-27 18:07:15 +000015127
Chris Lattnercee56e72009-03-13 05:53:31 +000015128 // Optimize cases that will turn into an LEA instruction. This requires
15129 // an i32 or i64 and an efficient multiplier (1, 2, 3, 4, 5, 8, 9).
Owen Anderson825b72b2009-08-11 20:47:22 +000015130 if (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i64) {
Chris Lattnercee56e72009-03-13 05:53:31 +000015131 uint64_t Diff = TrueC->getZExtValue()-FalseC->getZExtValue();
Owen Anderson825b72b2009-08-11 20:47:22 +000015132 if (N->getValueType(0) == MVT::i32) Diff = (unsigned)Diff;
Eric Christopherfd179292009-08-27 18:07:15 +000015133
Chris Lattnercee56e72009-03-13 05:53:31 +000015134 bool isFastMultiplier = false;
15135 if (Diff < 10) {
15136 switch ((unsigned char)Diff) {
15137 default: break;
15138 case 1: // result = add base, cond
15139 case 2: // result = lea base( , cond*2)
15140 case 3: // result = lea base(cond, cond*2)
15141 case 4: // result = lea base( , cond*4)
15142 case 5: // result = lea base(cond, cond*4)
15143 case 8: // result = lea base( , cond*8)
15144 case 9: // result = lea base(cond, cond*8)
15145 isFastMultiplier = true;
15146 break;
15147 }
15148 }
Eric Christopherfd179292009-08-27 18:07:15 +000015149
Chris Lattnercee56e72009-03-13 05:53:31 +000015150 if (isFastMultiplier) {
15151 APInt Diff = TrueC->getAPIntValue()-FalseC->getAPIntValue();
15152 if (NeedsCondInvert) // Invert the condition if needed.
15153 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
15154 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000015155
Chris Lattnercee56e72009-03-13 05:53:31 +000015156 // Zero extend the condition if needed.
15157 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, FalseC->getValueType(0),
15158 Cond);
15159 // Scale the condition by the difference.
15160 if (Diff != 1)
15161 Cond = DAG.getNode(ISD::MUL, DL, Cond.getValueType(), Cond,
15162 DAG.getConstant(Diff, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000015163
Chris Lattnercee56e72009-03-13 05:53:31 +000015164 // Add the base if non-zero.
15165 if (FalseC->getAPIntValue() != 0)
15166 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
15167 SDValue(FalseC, 0));
15168 return Cond;
15169 }
Eric Christopherfd179292009-08-27 18:07:15 +000015170 }
Chris Lattnerd1980a52009-03-12 06:52:53 +000015171 }
15172 }
Eric Christopherfd179292009-08-27 18:07:15 +000015173
Evan Cheng56f582d2012-01-04 01:41:39 +000015174 // Canonicalize max and min:
15175 // (x > y) ? x : y -> (x >= y) ? x : y
15176 // (x < y) ? x : y -> (x <= y) ? x : y
15177 // This allows use of COND_S / COND_NS (see TranslateX86CC) which eliminates
15178 // the need for an extra compare
15179 // against zero. e.g.
15180 // (x - y) > 0 : (x - y) ? 0 -> (x - y) >= 0 : (x - y) ? 0
15181 // subl %esi, %edi
15182 // testl %edi, %edi
15183 // movl $0, %eax
15184 // cmovgl %edi, %eax
15185 // =>
15186 // xorl %eax, %eax
15187 // subl %esi, $edi
15188 // cmovsl %eax, %edi
15189 if (N->getOpcode() == ISD::SELECT && Cond.getOpcode() == ISD::SETCC &&
15190 DAG.isEqualTo(LHS, Cond.getOperand(0)) &&
15191 DAG.isEqualTo(RHS, Cond.getOperand(1))) {
15192 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
15193 switch (CC) {
15194 default: break;
15195 case ISD::SETLT:
15196 case ISD::SETGT: {
15197 ISD::CondCode NewCC = (CC == ISD::SETLT) ? ISD::SETLE : ISD::SETGE;
15198 Cond = DAG.getSetCC(Cond.getDebugLoc(), Cond.getValueType(),
15199 Cond.getOperand(0), Cond.getOperand(1), NewCC);
15200 return DAG.getNode(ISD::SELECT, DL, VT, Cond, LHS, RHS);
15201 }
15202 }
15203 }
15204
Benjamin Kramer388fc6a2012-12-15 16:47:44 +000015205 // Match VSELECTs into subs with unsigned saturation.
15206 if (!DCI.isBeforeLegalize() &&
15207 N->getOpcode() == ISD::VSELECT && Cond.getOpcode() == ISD::SETCC &&
15208 // psubus is available in SSE2 and AVX2 for i8 and i16 vectors.
15209 ((Subtarget->hasSSE2() && (VT == MVT::v16i8 || VT == MVT::v8i16)) ||
15210 (Subtarget->hasAVX2() && (VT == MVT::v32i8 || VT == MVT::v16i16)))) {
15211 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
15212
15213 // Check if one of the arms of the VSELECT is a zero vector. If it's on the
15214 // left side invert the predicate to simplify logic below.
15215 SDValue Other;
15216 if (ISD::isBuildVectorAllZeros(LHS.getNode())) {
15217 Other = RHS;
15218 CC = ISD::getSetCCInverse(CC, true);
15219 } else if (ISD::isBuildVectorAllZeros(RHS.getNode())) {
15220 Other = LHS;
15221 }
15222
15223 if (Other.getNode() && Other->getNumOperands() == 2 &&
15224 DAG.isEqualTo(Other->getOperand(0), Cond.getOperand(0))) {
15225 SDValue OpLHS = Other->getOperand(0), OpRHS = Other->getOperand(1);
15226 SDValue CondRHS = Cond->getOperand(1);
15227
15228 // Look for a general sub with unsigned saturation first.
15229 // x >= y ? x-y : 0 --> subus x, y
15230 // x > y ? x-y : 0 --> subus x, y
15231 if ((CC == ISD::SETUGE || CC == ISD::SETUGT) &&
15232 Other->getOpcode() == ISD::SUB && DAG.isEqualTo(OpRHS, CondRHS))
15233 return DAG.getNode(X86ISD::SUBUS, DL, VT, OpLHS, OpRHS);
15234
15235 // If the RHS is a constant we have to reverse the const canonicalization.
15236 // x > C-1 ? x+-C : 0 --> subus x, C
15237 if (CC == ISD::SETUGT && Other->getOpcode() == ISD::ADD &&
15238 isSplatVector(CondRHS.getNode()) && isSplatVector(OpRHS.getNode())) {
15239 APInt A = cast<ConstantSDNode>(OpRHS.getOperand(0))->getAPIntValue();
15240 if (CondRHS.getConstantOperandVal(0) == -A-1) {
15241 SmallVector<SDValue, 32> V(VT.getVectorNumElements(),
15242 DAG.getConstant(-A, VT.getScalarType()));
15243 return DAG.getNode(X86ISD::SUBUS, DL, VT, OpLHS,
15244 DAG.getNode(ISD::BUILD_VECTOR, DL, VT,
15245 V.data(), V.size()));
15246 }
15247 }
15248
15249 // Another special case: If C was a sign bit, the sub has been
15250 // canonicalized into a xor.
15251 // FIXME: Would it be better to use ComputeMaskedBits to determine whether
15252 // it's safe to decanonicalize the xor?
15253 // x s< 0 ? x^C : 0 --> subus x, C
15254 if (CC == ISD::SETLT && Other->getOpcode() == ISD::XOR &&
15255 ISD::isBuildVectorAllZeros(CondRHS.getNode()) &&
15256 isSplatVector(OpRHS.getNode())) {
15257 APInt A = cast<ConstantSDNode>(OpRHS.getOperand(0))->getAPIntValue();
15258 if (A.isSignBit())
15259 return DAG.getNode(X86ISD::SUBUS, DL, VT, OpLHS, OpRHS);
15260 }
15261 }
15262 }
15263
Benjamin Kramer2556c6b2012-12-21 17:46:58 +000015264 // Try to match a min/max vector operation.
15265 if (!DCI.isBeforeLegalize() &&
15266 N->getOpcode() == ISD::VSELECT && Cond.getOpcode() == ISD::SETCC)
15267 if (unsigned Op = matchIntegerMINMAX(Cond, VT, LHS, RHS, DAG, Subtarget))
15268 return DAG.getNode(Op, DL, N->getValueType(0), LHS, RHS);
15269
Nadav Rotemcc616562012-01-15 19:27:55 +000015270 // If we know that this node is legal then we know that it is going to be
15271 // matched by one of the SSE/AVX BLEND instructions. These instructions only
15272 // depend on the highest bit in each word. Try to use SimplifyDemandedBits
15273 // to simplify previous instructions.
15274 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
15275 if (N->getOpcode() == ISD::VSELECT && DCI.isBeforeLegalizeOps() &&
Nadav Rotembdcae382012-06-07 20:53:48 +000015276 !DCI.isBeforeLegalize() && TLI.isOperationLegal(ISD::VSELECT, VT)) {
Nadav Rotemcc616562012-01-15 19:27:55 +000015277 unsigned BitWidth = Cond.getValueType().getScalarType().getSizeInBits();
Nadav Rotembdcae382012-06-07 20:53:48 +000015278
15279 // Don't optimize vector selects that map to mask-registers.
15280 if (BitWidth == 1)
15281 return SDValue();
15282
Nadav Rotemcc616562012-01-15 19:27:55 +000015283 assert(BitWidth >= 8 && BitWidth <= 64 && "Invalid mask size");
15284 APInt DemandedMask = APInt::getHighBitsSet(BitWidth, 1);
15285
15286 APInt KnownZero, KnownOne;
15287 TargetLowering::TargetLoweringOpt TLO(DAG, DCI.isBeforeLegalize(),
15288 DCI.isBeforeLegalizeOps());
15289 if (TLO.ShrinkDemandedConstant(Cond, DemandedMask) ||
15290 TLI.SimplifyDemandedBits(Cond, DemandedMask, KnownZero, KnownOne, TLO))
15291 DCI.CommitTargetLoweringOpt(TLO);
15292 }
15293
Dan Gohman475871a2008-07-27 21:46:04 +000015294 return SDValue();
Chris Lattner83e6c992006-10-04 06:57:07 +000015295}
15296
Michael Liao2a33cec2012-08-10 19:58:13 +000015297// Check whether a boolean test is testing a boolean value generated by
15298// X86ISD::SETCC. If so, return the operand of that SETCC and proper condition
15299// code.
15300//
15301// Simplify the following patterns:
15302// (Op (CMP (SETCC Cond EFLAGS) 1) EQ) or
15303// (Op (CMP (SETCC Cond EFLAGS) 0) NEQ)
15304// to (Op EFLAGS Cond)
15305//
15306// (Op (CMP (SETCC Cond EFLAGS) 0) EQ) or
15307// (Op (CMP (SETCC Cond EFLAGS) 1) NEQ)
15308// to (Op EFLAGS !Cond)
15309//
15310// where Op could be BRCOND or CMOV.
15311//
Michael Liaodbf8b5b2012-08-28 03:34:40 +000015312static SDValue checkBoolTestSetCCCombine(SDValue Cmp, X86::CondCode &CC) {
Michael Liao2a33cec2012-08-10 19:58:13 +000015313 // Quit if not CMP and SUB with its value result used.
15314 if (Cmp.getOpcode() != X86ISD::CMP &&
15315 (Cmp.getOpcode() != X86ISD::SUB || Cmp.getNode()->hasAnyUseOfValue(0)))
15316 return SDValue();
15317
15318 // Quit if not used as a boolean value.
15319 if (CC != X86::COND_E && CC != X86::COND_NE)
15320 return SDValue();
15321
15322 // Check CMP operands. One of them should be 0 or 1 and the other should be
15323 // an SetCC or extended from it.
15324 SDValue Op1 = Cmp.getOperand(0);
15325 SDValue Op2 = Cmp.getOperand(1);
15326
15327 SDValue SetCC;
15328 const ConstantSDNode* C = 0;
15329 bool needOppositeCond = (CC == X86::COND_E);
15330
15331 if ((C = dyn_cast<ConstantSDNode>(Op1)))
15332 SetCC = Op2;
15333 else if ((C = dyn_cast<ConstantSDNode>(Op2)))
15334 SetCC = Op1;
15335 else // Quit if all operands are not constants.
15336 return SDValue();
15337
15338 if (C->getZExtValue() == 1)
15339 needOppositeCond = !needOppositeCond;
15340 else if (C->getZExtValue() != 0)
15341 // Quit if the constant is neither 0 or 1.
15342 return SDValue();
15343
15344 // Skip 'zext' node.
15345 if (SetCC.getOpcode() == ISD::ZERO_EXTEND)
15346 SetCC = SetCC.getOperand(0);
15347
Michael Liao7fdc66b2012-09-10 16:36:16 +000015348 switch (SetCC.getOpcode()) {
15349 case X86ISD::SETCC:
15350 // Set the condition code or opposite one if necessary.
15351 CC = X86::CondCode(SetCC.getConstantOperandVal(0));
15352 if (needOppositeCond)
15353 CC = X86::GetOppositeBranchCondition(CC);
15354 return SetCC.getOperand(1);
15355 case X86ISD::CMOV: {
15356 // Check whether false/true value has canonical one, i.e. 0 or 1.
15357 ConstantSDNode *FVal = dyn_cast<ConstantSDNode>(SetCC.getOperand(0));
15358 ConstantSDNode *TVal = dyn_cast<ConstantSDNode>(SetCC.getOperand(1));
15359 // Quit if true value is not a constant.
15360 if (!TVal)
15361 return SDValue();
15362 // Quit if false value is not a constant.
15363 if (!FVal) {
15364 // A special case for rdrand, where 0 is set if false cond is found.
15365 SDValue Op = SetCC.getOperand(0);
15366 if (Op.getOpcode() != X86ISD::RDRAND)
15367 return SDValue();
15368 }
15369 // Quit if false value is not the constant 0 or 1.
15370 bool FValIsFalse = true;
15371 if (FVal && FVal->getZExtValue() != 0) {
15372 if (FVal->getZExtValue() != 1)
15373 return SDValue();
15374 // If FVal is 1, opposite cond is needed.
15375 needOppositeCond = !needOppositeCond;
15376 FValIsFalse = false;
15377 }
15378 // Quit if TVal is not the constant opposite of FVal.
15379 if (FValIsFalse && TVal->getZExtValue() != 1)
15380 return SDValue();
15381 if (!FValIsFalse && TVal->getZExtValue() != 0)
15382 return SDValue();
15383 CC = X86::CondCode(SetCC.getConstantOperandVal(2));
15384 if (needOppositeCond)
15385 CC = X86::GetOppositeBranchCondition(CC);
15386 return SetCC.getOperand(3);
15387 }
15388 }
Michael Liao2a33cec2012-08-10 19:58:13 +000015389
Michael Liao7fdc66b2012-09-10 16:36:16 +000015390 return SDValue();
Michael Liao2a33cec2012-08-10 19:58:13 +000015391}
15392
Chris Lattnerd1980a52009-03-12 06:52:53 +000015393/// Optimize X86ISD::CMOV [LHS, RHS, CONDCODE (e.g. X86::COND_NE), CONDVAL]
15394static SDValue PerformCMOVCombine(SDNode *N, SelectionDAG &DAG,
Michael Liaodbf8b5b2012-08-28 03:34:40 +000015395 TargetLowering::DAGCombinerInfo &DCI,
15396 const X86Subtarget *Subtarget) {
Chris Lattnerd1980a52009-03-12 06:52:53 +000015397 DebugLoc DL = N->getDebugLoc();
Eric Christopherfd179292009-08-27 18:07:15 +000015398
Chris Lattnerd1980a52009-03-12 06:52:53 +000015399 // If the flag operand isn't dead, don't touch this CMOV.
15400 if (N->getNumValues() == 2 && !SDValue(N, 1).use_empty())
15401 return SDValue();
Eric Christopherfd179292009-08-27 18:07:15 +000015402
Evan Chengb5a55d92011-05-24 01:48:22 +000015403 SDValue FalseOp = N->getOperand(0);
15404 SDValue TrueOp = N->getOperand(1);
15405 X86::CondCode CC = (X86::CondCode)N->getConstantOperandVal(2);
15406 SDValue Cond = N->getOperand(3);
Michael Liao2a33cec2012-08-10 19:58:13 +000015407
Evan Chengb5a55d92011-05-24 01:48:22 +000015408 if (CC == X86::COND_E || CC == X86::COND_NE) {
15409 switch (Cond.getOpcode()) {
15410 default: break;
15411 case X86ISD::BSR:
15412 case X86ISD::BSF:
15413 // If operand of BSR / BSF are proven never zero, then ZF cannot be set.
15414 if (DAG.isKnownNeverZero(Cond.getOperand(0)))
15415 return (CC == X86::COND_E) ? FalseOp : TrueOp;
15416 }
15417 }
15418
Michael Liao2a33cec2012-08-10 19:58:13 +000015419 SDValue Flags;
15420
Michael Liaodbf8b5b2012-08-28 03:34:40 +000015421 Flags = checkBoolTestSetCCCombine(Cond, CC);
Michael Liao9eac20a2012-08-11 23:47:06 +000015422 if (Flags.getNode() &&
15423 // Extra check as FCMOV only supports a subset of X86 cond.
Michael Liao7859f432012-09-06 07:11:22 +000015424 (FalseOp.getValueType() != MVT::f80 || hasFPCMov(CC))) {
Michael Liaodbf8b5b2012-08-28 03:34:40 +000015425 SDValue Ops[] = { FalseOp, TrueOp,
15426 DAG.getConstant(CC, MVT::i8), Flags };
15427 return DAG.getNode(X86ISD::CMOV, DL, N->getVTList(),
15428 Ops, array_lengthof(Ops));
15429 }
15430
Chris Lattnerd1980a52009-03-12 06:52:53 +000015431 // If this is a select between two integer constants, try to do some
15432 // optimizations. Note that the operands are ordered the opposite of SELECT
15433 // operands.
Evan Chengb5a55d92011-05-24 01:48:22 +000015434 if (ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(TrueOp)) {
15435 if (ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(FalseOp)) {
Chris Lattnerd1980a52009-03-12 06:52:53 +000015436 // Canonicalize the TrueC/FalseC values so that TrueC (the true value) is
15437 // larger than FalseC (the false value).
Chris Lattnerd1980a52009-03-12 06:52:53 +000015438 if (TrueC->getAPIntValue().ult(FalseC->getAPIntValue())) {
15439 CC = X86::GetOppositeBranchCondition(CC);
15440 std::swap(TrueC, FalseC);
NAKAMURA Takumie2687452012-10-16 06:28:34 +000015441 std::swap(TrueOp, FalseOp);
Chris Lattnerd1980a52009-03-12 06:52:53 +000015442 }
Eric Christopherfd179292009-08-27 18:07:15 +000015443
Chris Lattnerd1980a52009-03-12 06:52:53 +000015444 // Optimize C ? 8 : 0 -> zext(setcc(C)) << 3. Likewise for any pow2/0.
Chris Lattnercee56e72009-03-13 05:53:31 +000015445 // This is efficient for any integer data type (including i8/i16) and
15446 // shift amount.
Chris Lattnerd1980a52009-03-12 06:52:53 +000015447 if (FalseC->getAPIntValue() == 0 && TrueC->getAPIntValue().isPowerOf2()) {
Owen Anderson825b72b2009-08-11 20:47:22 +000015448 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
15449 DAG.getConstant(CC, MVT::i8), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000015450
Chris Lattnerd1980a52009-03-12 06:52:53 +000015451 // Zero extend the condition if needed.
15452 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, TrueC->getValueType(0), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000015453
Chris Lattnerd1980a52009-03-12 06:52:53 +000015454 unsigned ShAmt = TrueC->getAPIntValue().logBase2();
15455 Cond = DAG.getNode(ISD::SHL, DL, Cond.getValueType(), Cond,
Owen Anderson825b72b2009-08-11 20:47:22 +000015456 DAG.getConstant(ShAmt, MVT::i8));
Chris Lattnerd1980a52009-03-12 06:52:53 +000015457 if (N->getNumValues() == 2) // Dead flag value?
15458 return DCI.CombineTo(N, Cond, SDValue());
15459 return Cond;
15460 }
Eric Christopherfd179292009-08-27 18:07:15 +000015461
Chris Lattnercee56e72009-03-13 05:53:31 +000015462 // Optimize Cond ? cst+1 : cst -> zext(setcc(C)+cst. This is efficient
15463 // for any integer data type, including i8/i16.
Chris Lattner97a29a52009-03-13 05:22:11 +000015464 if (FalseC->getAPIntValue()+1 == TrueC->getAPIntValue()) {
Owen Anderson825b72b2009-08-11 20:47:22 +000015465 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
15466 DAG.getConstant(CC, MVT::i8), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000015467
Chris Lattner97a29a52009-03-13 05:22:11 +000015468 // Zero extend the condition if needed.
Chris Lattnercee56e72009-03-13 05:53:31 +000015469 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL,
15470 FalseC->getValueType(0), Cond);
Chris Lattner97a29a52009-03-13 05:22:11 +000015471 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
15472 SDValue(FalseC, 0));
Eric Christopherfd179292009-08-27 18:07:15 +000015473
Chris Lattner97a29a52009-03-13 05:22:11 +000015474 if (N->getNumValues() == 2) // Dead flag value?
15475 return DCI.CombineTo(N, Cond, SDValue());
15476 return Cond;
15477 }
Eric Christopherfd179292009-08-27 18:07:15 +000015478
Chris Lattnercee56e72009-03-13 05:53:31 +000015479 // Optimize cases that will turn into an LEA instruction. This requires
15480 // an i32 or i64 and an efficient multiplier (1, 2, 3, 4, 5, 8, 9).
Owen Anderson825b72b2009-08-11 20:47:22 +000015481 if (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i64) {
Chris Lattnercee56e72009-03-13 05:53:31 +000015482 uint64_t Diff = TrueC->getZExtValue()-FalseC->getZExtValue();
Owen Anderson825b72b2009-08-11 20:47:22 +000015483 if (N->getValueType(0) == MVT::i32) Diff = (unsigned)Diff;
Eric Christopherfd179292009-08-27 18:07:15 +000015484
Chris Lattnercee56e72009-03-13 05:53:31 +000015485 bool isFastMultiplier = false;
15486 if (Diff < 10) {
15487 switch ((unsigned char)Diff) {
15488 default: break;
15489 case 1: // result = add base, cond
15490 case 2: // result = lea base( , cond*2)
15491 case 3: // result = lea base(cond, cond*2)
15492 case 4: // result = lea base( , cond*4)
15493 case 5: // result = lea base(cond, cond*4)
15494 case 8: // result = lea base( , cond*8)
15495 case 9: // result = lea base(cond, cond*8)
15496 isFastMultiplier = true;
15497 break;
15498 }
15499 }
Eric Christopherfd179292009-08-27 18:07:15 +000015500
Chris Lattnercee56e72009-03-13 05:53:31 +000015501 if (isFastMultiplier) {
15502 APInt Diff = TrueC->getAPIntValue()-FalseC->getAPIntValue();
Owen Anderson825b72b2009-08-11 20:47:22 +000015503 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
15504 DAG.getConstant(CC, MVT::i8), Cond);
Chris Lattnercee56e72009-03-13 05:53:31 +000015505 // Zero extend the condition if needed.
15506 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, FalseC->getValueType(0),
15507 Cond);
15508 // Scale the condition by the difference.
15509 if (Diff != 1)
15510 Cond = DAG.getNode(ISD::MUL, DL, Cond.getValueType(), Cond,
15511 DAG.getConstant(Diff, Cond.getValueType()));
15512
15513 // Add the base if non-zero.
15514 if (FalseC->getAPIntValue() != 0)
15515 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
15516 SDValue(FalseC, 0));
15517 if (N->getNumValues() == 2) // Dead flag value?
15518 return DCI.CombineTo(N, Cond, SDValue());
15519 return Cond;
15520 }
Eric Christopherfd179292009-08-27 18:07:15 +000015521 }
Chris Lattnerd1980a52009-03-12 06:52:53 +000015522 }
15523 }
NAKAMURA Takumie2687452012-10-16 06:28:34 +000015524
15525 // Handle these cases:
15526 // (select (x != c), e, c) -> select (x != c), e, x),
15527 // (select (x == c), c, e) -> select (x == c), x, e)
15528 // where the c is an integer constant, and the "select" is the combination
15529 // of CMOV and CMP.
15530 //
15531 // The rationale for this change is that the conditional-move from a constant
15532 // needs two instructions, however, conditional-move from a register needs
15533 // only one instruction.
15534 //
15535 // CAVEAT: By replacing a constant with a symbolic value, it may obscure
15536 // some instruction-combining opportunities. This opt needs to be
15537 // postponed as late as possible.
15538 //
15539 if (!DCI.isBeforeLegalize() && !DCI.isBeforeLegalizeOps()) {
15540 // the DCI.xxxx conditions are provided to postpone the optimization as
15541 // late as possible.
15542
15543 ConstantSDNode *CmpAgainst = 0;
15544 if ((Cond.getOpcode() == X86ISD::CMP || Cond.getOpcode() == X86ISD::SUB) &&
15545 (CmpAgainst = dyn_cast<ConstantSDNode>(Cond.getOperand(1))) &&
15546 dyn_cast<ConstantSDNode>(Cond.getOperand(0)) == 0) {
15547
15548 if (CC == X86::COND_NE &&
15549 CmpAgainst == dyn_cast<ConstantSDNode>(FalseOp)) {
15550 CC = X86::GetOppositeBranchCondition(CC);
15551 std::swap(TrueOp, FalseOp);
15552 }
15553
15554 if (CC == X86::COND_E &&
15555 CmpAgainst == dyn_cast<ConstantSDNode>(TrueOp)) {
15556 SDValue Ops[] = { FalseOp, Cond.getOperand(0),
15557 DAG.getConstant(CC, MVT::i8), Cond };
15558 return DAG.getNode(X86ISD::CMOV, DL, N->getVTList (), Ops,
15559 array_lengthof(Ops));
15560 }
15561 }
15562 }
15563
Chris Lattnerd1980a52009-03-12 06:52:53 +000015564 return SDValue();
15565}
15566
Evan Cheng0b0cd912009-03-28 05:57:29 +000015567/// PerformMulCombine - Optimize a single multiply with constant into two
15568/// in order to implement it with two cheaper instructions, e.g.
15569/// LEA + SHL, LEA + LEA.
15570static SDValue PerformMulCombine(SDNode *N, SelectionDAG &DAG,
15571 TargetLowering::DAGCombinerInfo &DCI) {
Evan Cheng0b0cd912009-03-28 05:57:29 +000015572 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
15573 return SDValue();
15574
Owen Andersone50ed302009-08-10 22:56:29 +000015575 EVT VT = N->getValueType(0);
Owen Anderson825b72b2009-08-11 20:47:22 +000015576 if (VT != MVT::i64)
Evan Cheng0b0cd912009-03-28 05:57:29 +000015577 return SDValue();
15578
15579 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1));
15580 if (!C)
15581 return SDValue();
15582 uint64_t MulAmt = C->getZExtValue();
15583 if (isPowerOf2_64(MulAmt) || MulAmt == 3 || MulAmt == 5 || MulAmt == 9)
15584 return SDValue();
15585
15586 uint64_t MulAmt1 = 0;
15587 uint64_t MulAmt2 = 0;
15588 if ((MulAmt % 9) == 0) {
15589 MulAmt1 = 9;
15590 MulAmt2 = MulAmt / 9;
15591 } else if ((MulAmt % 5) == 0) {
15592 MulAmt1 = 5;
15593 MulAmt2 = MulAmt / 5;
15594 } else if ((MulAmt % 3) == 0) {
15595 MulAmt1 = 3;
15596 MulAmt2 = MulAmt / 3;
15597 }
15598 if (MulAmt2 &&
15599 (isPowerOf2_64(MulAmt2) || MulAmt2 == 3 || MulAmt2 == 5 || MulAmt2 == 9)){
15600 DebugLoc DL = N->getDebugLoc();
15601
15602 if (isPowerOf2_64(MulAmt2) &&
15603 !(N->hasOneUse() && N->use_begin()->getOpcode() == ISD::ADD))
15604 // If second multiplifer is pow2, issue it first. We want the multiply by
15605 // 3, 5, or 9 to be folded into the addressing mode unless the lone use
15606 // is an add.
15607 std::swap(MulAmt1, MulAmt2);
15608
15609 SDValue NewMul;
Eric Christopherfd179292009-08-27 18:07:15 +000015610 if (isPowerOf2_64(MulAmt1))
Evan Cheng0b0cd912009-03-28 05:57:29 +000015611 NewMul = DAG.getNode(ISD::SHL, DL, VT, N->getOperand(0),
Owen Anderson825b72b2009-08-11 20:47:22 +000015612 DAG.getConstant(Log2_64(MulAmt1), MVT::i8));
Evan Cheng0b0cd912009-03-28 05:57:29 +000015613 else
Evan Cheng73f24c92009-03-30 21:36:47 +000015614 NewMul = DAG.getNode(X86ISD::MUL_IMM, DL, VT, N->getOperand(0),
Evan Cheng0b0cd912009-03-28 05:57:29 +000015615 DAG.getConstant(MulAmt1, VT));
15616
Eric Christopherfd179292009-08-27 18:07:15 +000015617 if (isPowerOf2_64(MulAmt2))
Evan Cheng0b0cd912009-03-28 05:57:29 +000015618 NewMul = DAG.getNode(ISD::SHL, DL, VT, NewMul,
Owen Anderson825b72b2009-08-11 20:47:22 +000015619 DAG.getConstant(Log2_64(MulAmt2), MVT::i8));
Eric Christopherfd179292009-08-27 18:07:15 +000015620 else
Evan Cheng73f24c92009-03-30 21:36:47 +000015621 NewMul = DAG.getNode(X86ISD::MUL_IMM, DL, VT, NewMul,
Evan Cheng0b0cd912009-03-28 05:57:29 +000015622 DAG.getConstant(MulAmt2, VT));
15623
15624 // Do not add new nodes to DAG combiner worklist.
15625 DCI.CombineTo(N, NewMul, false);
15626 }
15627 return SDValue();
15628}
15629
Evan Chengad9c0a32009-12-15 00:53:42 +000015630static SDValue PerformSHLCombine(SDNode *N, SelectionDAG &DAG) {
15631 SDValue N0 = N->getOperand(0);
15632 SDValue N1 = N->getOperand(1);
15633 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
15634 EVT VT = N0.getValueType();
15635
15636 // fold (shl (and (setcc_c), c1), c2) -> (and setcc_c, (c1 << c2))
15637 // since the result of setcc_c is all zero's or all ones.
Nadav Rotemfb0dfbb2011-10-30 13:24:22 +000015638 if (VT.isInteger() && !VT.isVector() &&
15639 N1C && N0.getOpcode() == ISD::AND &&
Evan Chengad9c0a32009-12-15 00:53:42 +000015640 N0.getOperand(1).getOpcode() == ISD::Constant) {
15641 SDValue N00 = N0.getOperand(0);
15642 if (N00.getOpcode() == X86ISD::SETCC_CARRY ||
15643 ((N00.getOpcode() == ISD::ANY_EXTEND ||
15644 N00.getOpcode() == ISD::ZERO_EXTEND) &&
15645 N00.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY)) {
15646 APInt Mask = cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
15647 APInt ShAmt = N1C->getAPIntValue();
15648 Mask = Mask.shl(ShAmt);
15649 if (Mask != 0)
15650 return DAG.getNode(ISD::AND, N->getDebugLoc(), VT,
15651 N00, DAG.getConstant(Mask, VT));
15652 }
15653 }
15654
Nadav Rotemfb0dfbb2011-10-30 13:24:22 +000015655 // Hardware support for vector shifts is sparse which makes us scalarize the
15656 // vector operations in many cases. Also, on sandybridge ADD is faster than
15657 // shl.
15658 // (shl V, 1) -> add V,V
15659 if (isSplatVector(N1.getNode())) {
15660 assert(N0.getValueType().isVector() && "Invalid vector shift type");
15661 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1->getOperand(0));
15662 // We shift all of the values by one. In many cases we do not have
15663 // hardware support for this operation. This is better expressed as an ADD
15664 // of two values.
15665 if (N1C && (1 == N1C->getZExtValue())) {
15666 return DAG.getNode(ISD::ADD, N->getDebugLoc(), VT, N0, N0);
15667 }
15668 }
15669
Evan Chengad9c0a32009-12-15 00:53:42 +000015670 return SDValue();
15671}
Evan Cheng0b0cd912009-03-28 05:57:29 +000015672
Nate Begeman740ab032009-01-26 00:52:55 +000015673/// PerformShiftCombine - Transforms vector shift nodes to use vector shifts
15674/// when possible.
15675static SDValue PerformShiftCombine(SDNode* N, SelectionDAG &DAG,
Mon P Wang845b1892012-02-01 22:15:20 +000015676 TargetLowering::DAGCombinerInfo &DCI,
Nate Begeman740ab032009-01-26 00:52:55 +000015677 const X86Subtarget *Subtarget) {
Evan Chengad9c0a32009-12-15 00:53:42 +000015678 EVT VT = N->getValueType(0);
Nadav Rotemfb0dfbb2011-10-30 13:24:22 +000015679 if (N->getOpcode() == ISD::SHL) {
15680 SDValue V = PerformSHLCombine(N, DAG);
15681 if (V.getNode()) return V;
15682 }
Evan Chengad9c0a32009-12-15 00:53:42 +000015683
Nate Begeman740ab032009-01-26 00:52:55 +000015684 // On X86 with SSE2 support, we can transform this to a vector shift if
15685 // all elements are shifted by the same amount. We can't do this in legalize
15686 // because the a constant vector is typically transformed to a constant pool
15687 // so we have no knowledge of the shift amount.
Craig Topper1accb7e2012-01-10 06:54:16 +000015688 if (!Subtarget->hasSSE2())
Nate Begemanc2fd67f2009-01-26 03:15:31 +000015689 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +000015690
Craig Topper7be5dfd2011-11-12 09:58:49 +000015691 if (VT != MVT::v2i64 && VT != MVT::v4i32 && VT != MVT::v8i16 &&
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000015692 (!Subtarget->hasInt256() ||
Craig Topper7be5dfd2011-11-12 09:58:49 +000015693 (VT != MVT::v4i64 && VT != MVT::v8i32 && VT != MVT::v16i16)))
Nate Begemanc2fd67f2009-01-26 03:15:31 +000015694 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +000015695
Mon P Wang3becd092009-01-28 08:12:05 +000015696 SDValue ShAmtOp = N->getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +000015697 EVT EltVT = VT.getVectorElementType();
Chris Lattner47b4ce82009-03-11 05:48:52 +000015698 DebugLoc DL = N->getDebugLoc();
Mon P Wangefa42202009-09-03 19:56:25 +000015699 SDValue BaseShAmt = SDValue();
Mon P Wang3becd092009-01-28 08:12:05 +000015700 if (ShAmtOp.getOpcode() == ISD::BUILD_VECTOR) {
15701 unsigned NumElts = VT.getVectorNumElements();
15702 unsigned i = 0;
15703 for (; i != NumElts; ++i) {
15704 SDValue Arg = ShAmtOp.getOperand(i);
15705 if (Arg.getOpcode() == ISD::UNDEF) continue;
15706 BaseShAmt = Arg;
15707 break;
15708 }
Craig Topper37c26772012-01-17 04:44:50 +000015709 // Handle the case where the build_vector is all undef
15710 // FIXME: Should DAG allow this?
15711 if (i == NumElts)
15712 return SDValue();
15713
Mon P Wang3becd092009-01-28 08:12:05 +000015714 for (; i != NumElts; ++i) {
15715 SDValue Arg = ShAmtOp.getOperand(i);
15716 if (Arg.getOpcode() == ISD::UNDEF) continue;
15717 if (Arg != BaseShAmt) {
15718 return SDValue();
15719 }
15720 }
15721 } else if (ShAmtOp.getOpcode() == ISD::VECTOR_SHUFFLE &&
Nate Begeman9008ca62009-04-27 18:41:29 +000015722 cast<ShuffleVectorSDNode>(ShAmtOp)->isSplat()) {
Mon P Wangefa42202009-09-03 19:56:25 +000015723 SDValue InVec = ShAmtOp.getOperand(0);
15724 if (InVec.getOpcode() == ISD::BUILD_VECTOR) {
15725 unsigned NumElts = InVec.getValueType().getVectorNumElements();
15726 unsigned i = 0;
15727 for (; i != NumElts; ++i) {
15728 SDValue Arg = InVec.getOperand(i);
15729 if (Arg.getOpcode() == ISD::UNDEF) continue;
15730 BaseShAmt = Arg;
15731 break;
15732 }
15733 } else if (InVec.getOpcode() == ISD::INSERT_VECTOR_ELT) {
15734 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(InVec.getOperand(2))) {
Evan Chengae3ecf92010-02-16 21:09:44 +000015735 unsigned SplatIdx= cast<ShuffleVectorSDNode>(ShAmtOp)->getSplatIndex();
Mon P Wangefa42202009-09-03 19:56:25 +000015736 if (C->getZExtValue() == SplatIdx)
15737 BaseShAmt = InVec.getOperand(1);
15738 }
15739 }
Mon P Wang845b1892012-02-01 22:15:20 +000015740 if (BaseShAmt.getNode() == 0) {
15741 // Don't create instructions with illegal types after legalize
15742 // types has run.
15743 if (!DAG.getTargetLoweringInfo().isTypeLegal(EltVT) &&
15744 !DCI.isBeforeLegalize())
15745 return SDValue();
15746
Mon P Wangefa42202009-09-03 19:56:25 +000015747 BaseShAmt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, EltVT, ShAmtOp,
15748 DAG.getIntPtrConstant(0));
Mon P Wang845b1892012-02-01 22:15:20 +000015749 }
Mon P Wang3becd092009-01-28 08:12:05 +000015750 } else
Nate Begemanc2fd67f2009-01-26 03:15:31 +000015751 return SDValue();
Nate Begeman740ab032009-01-26 00:52:55 +000015752
Mon P Wangefa42202009-09-03 19:56:25 +000015753 // The shift amount is an i32.
Owen Anderson825b72b2009-08-11 20:47:22 +000015754 if (EltVT.bitsGT(MVT::i32))
15755 BaseShAmt = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, BaseShAmt);
15756 else if (EltVT.bitsLT(MVT::i32))
Mon P Wangefa42202009-09-03 19:56:25 +000015757 BaseShAmt = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::i32, BaseShAmt);
Nate Begeman740ab032009-01-26 00:52:55 +000015758
Nate Begemanc2fd67f2009-01-26 03:15:31 +000015759 // The shift amount is identical so we can do a vector shift.
15760 SDValue ValOp = N->getOperand(0);
15761 switch (N->getOpcode()) {
15762 default:
Torok Edwinc23197a2009-07-14 16:55:14 +000015763 llvm_unreachable("Unknown shift opcode!");
Nate Begemanc2fd67f2009-01-26 03:15:31 +000015764 case ISD::SHL:
Craig Toppered2e13d2012-01-22 19:15:14 +000015765 switch (VT.getSimpleVT().SimpleTy) {
15766 default: return SDValue();
15767 case MVT::v2i64:
15768 case MVT::v4i32:
15769 case MVT::v8i16:
15770 case MVT::v4i64:
15771 case MVT::v8i32:
15772 case MVT::v16i16:
15773 return getTargetVShiftNode(X86ISD::VSHLI, DL, VT, ValOp, BaseShAmt, DAG);
15774 }
Nate Begemanc2fd67f2009-01-26 03:15:31 +000015775 case ISD::SRA:
Craig Toppered2e13d2012-01-22 19:15:14 +000015776 switch (VT.getSimpleVT().SimpleTy) {
15777 default: return SDValue();
15778 case MVT::v4i32:
15779 case MVT::v8i16:
15780 case MVT::v8i32:
15781 case MVT::v16i16:
15782 return getTargetVShiftNode(X86ISD::VSRAI, DL, VT, ValOp, BaseShAmt, DAG);
15783 }
Nate Begemanc2fd67f2009-01-26 03:15:31 +000015784 case ISD::SRL:
Craig Toppered2e13d2012-01-22 19:15:14 +000015785 switch (VT.getSimpleVT().SimpleTy) {
15786 default: return SDValue();
15787 case MVT::v2i64:
15788 case MVT::v4i32:
15789 case MVT::v8i16:
15790 case MVT::v4i64:
15791 case MVT::v8i32:
15792 case MVT::v16i16:
15793 return getTargetVShiftNode(X86ISD::VSRLI, DL, VT, ValOp, BaseShAmt, DAG);
15794 }
Nate Begeman740ab032009-01-26 00:52:55 +000015795 }
Nate Begeman740ab032009-01-26 00:52:55 +000015796}
15797
Stuart Hastings865f0932011-06-03 23:53:54 +000015798// CMPEQCombine - Recognize the distinctive (AND (setcc ...) (setcc ..))
15799// where both setccs reference the same FP CMP, and rewrite for CMPEQSS
15800// and friends. Likewise for OR -> CMPNEQSS.
15801static SDValue CMPEQCombine(SDNode *N, SelectionDAG &DAG,
15802 TargetLowering::DAGCombinerInfo &DCI,
15803 const X86Subtarget *Subtarget) {
15804 unsigned opcode;
15805
15806 // SSE1 supports CMP{eq|ne}SS, and SSE2 added CMP{eq|ne}SD, but
15807 // we're requiring SSE2 for both.
Craig Topper1accb7e2012-01-10 06:54:16 +000015808 if (Subtarget->hasSSE2() && isAndOrOfSetCCs(SDValue(N, 0U), opcode)) {
Stuart Hastings865f0932011-06-03 23:53:54 +000015809 SDValue N0 = N->getOperand(0);
15810 SDValue N1 = N->getOperand(1);
15811 SDValue CMP0 = N0->getOperand(1);
15812 SDValue CMP1 = N1->getOperand(1);
15813 DebugLoc DL = N->getDebugLoc();
15814
15815 // The SETCCs should both refer to the same CMP.
15816 if (CMP0.getOpcode() != X86ISD::CMP || CMP0 != CMP1)
15817 return SDValue();
15818
15819 SDValue CMP00 = CMP0->getOperand(0);
15820 SDValue CMP01 = CMP0->getOperand(1);
15821 EVT VT = CMP00.getValueType();
15822
15823 if (VT == MVT::f32 || VT == MVT::f64) {
15824 bool ExpectingFlags = false;
15825 // Check for any users that want flags:
15826 for (SDNode::use_iterator UI = N->use_begin(),
15827 UE = N->use_end();
15828 !ExpectingFlags && UI != UE; ++UI)
15829 switch (UI->getOpcode()) {
15830 default:
15831 case ISD::BR_CC:
15832 case ISD::BRCOND:
15833 case ISD::SELECT:
15834 ExpectingFlags = true;
15835 break;
15836 case ISD::CopyToReg:
15837 case ISD::SIGN_EXTEND:
15838 case ISD::ZERO_EXTEND:
15839 case ISD::ANY_EXTEND:
15840 break;
15841 }
15842
15843 if (!ExpectingFlags) {
15844 enum X86::CondCode cc0 = (enum X86::CondCode)N0.getConstantOperandVal(0);
15845 enum X86::CondCode cc1 = (enum X86::CondCode)N1.getConstantOperandVal(0);
15846
15847 if (cc1 == X86::COND_E || cc1 == X86::COND_NE) {
15848 X86::CondCode tmp = cc0;
15849 cc0 = cc1;
15850 cc1 = tmp;
15851 }
15852
15853 if ((cc0 == X86::COND_E && cc1 == X86::COND_NP) ||
15854 (cc0 == X86::COND_NE && cc1 == X86::COND_P)) {
15855 bool is64BitFP = (CMP00.getValueType() == MVT::f64);
15856 X86ISD::NodeType NTOperator = is64BitFP ?
15857 X86ISD::FSETCCsd : X86ISD::FSETCCss;
15858 // FIXME: need symbolic constants for these magic numbers.
15859 // See X86ATTInstPrinter.cpp:printSSECC().
15860 unsigned x86cc = (cc0 == X86::COND_E) ? 0 : 4;
15861 SDValue OnesOrZeroesF = DAG.getNode(NTOperator, DL, MVT::f32, CMP00, CMP01,
15862 DAG.getConstant(x86cc, MVT::i8));
15863 SDValue OnesOrZeroesI = DAG.getNode(ISD::BITCAST, DL, MVT::i32,
15864 OnesOrZeroesF);
15865 SDValue ANDed = DAG.getNode(ISD::AND, DL, MVT::i32, OnesOrZeroesI,
15866 DAG.getConstant(1, MVT::i32));
15867 SDValue OneBitOfTruth = DAG.getNode(ISD::TRUNCATE, DL, MVT::i8, ANDed);
15868 return OneBitOfTruth;
15869 }
15870 }
15871 }
15872 }
15873 return SDValue();
15874}
15875
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +000015876/// CanFoldXORWithAllOnes - Test whether the XOR operand is a AllOnes vector
15877/// so it can be folded inside ANDNP.
15878static bool CanFoldXORWithAllOnes(const SDNode *N) {
15879 EVT VT = N->getValueType(0);
15880
15881 // Match direct AllOnes for 128 and 256-bit vectors
15882 if (ISD::isBuildVectorAllOnes(N))
15883 return true;
15884
15885 // Look through a bit convert.
15886 if (N->getOpcode() == ISD::BITCAST)
15887 N = N->getOperand(0).getNode();
15888
15889 // Sometimes the operand may come from a insert_subvector building a 256-bit
15890 // allones vector
Craig Topper7a9a28b2012-08-12 02:23:29 +000015891 if (VT.is256BitVector() &&
Bill Wendling456a9252011-08-04 00:32:58 +000015892 N->getOpcode() == ISD::INSERT_SUBVECTOR) {
15893 SDValue V1 = N->getOperand(0);
15894 SDValue V2 = N->getOperand(1);
15895
15896 if (V1.getOpcode() == ISD::INSERT_SUBVECTOR &&
15897 V1.getOperand(0).getOpcode() == ISD::UNDEF &&
15898 ISD::isBuildVectorAllOnes(V1.getOperand(1).getNode()) &&
15899 ISD::isBuildVectorAllOnes(V2.getNode()))
15900 return true;
15901 }
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +000015902
15903 return false;
15904}
15905
Nadav Rotemd6fb53a2012-12-27 08:15:45 +000015906// On AVX/AVX2 the type v8i1 is legalized to v8i16, which is an XMM sized
15907// register. In most cases we actually compare or select YMM-sized registers
15908// and mixing the two types creates horrible code. This method optimizes
15909// some of the transition sequences.
15910static SDValue WidenMaskArithmetic(SDNode *N, SelectionDAG &DAG,
15911 TargetLowering::DAGCombinerInfo &DCI,
15912 const X86Subtarget *Subtarget) {
15913 EVT VT = N->getValueType(0);
15914 if (VT.getSizeInBits() != 256)
15915 return SDValue();
15916
15917 assert((N->getOpcode() == ISD::ANY_EXTEND ||
15918 N->getOpcode() == ISD::ZERO_EXTEND ||
15919 N->getOpcode() == ISD::SIGN_EXTEND) && "Invalid Node");
15920
15921 SDValue Narrow = N->getOperand(0);
15922 EVT NarrowVT = Narrow->getValueType(0);
15923 if (NarrowVT.getSizeInBits() != 128)
15924 return SDValue();
15925
15926 if (Narrow->getOpcode() != ISD::XOR &&
15927 Narrow->getOpcode() != ISD::AND &&
15928 Narrow->getOpcode() != ISD::OR)
15929 return SDValue();
15930
15931 SDValue N0 = Narrow->getOperand(0);
15932 SDValue N1 = Narrow->getOperand(1);
15933 DebugLoc DL = Narrow->getDebugLoc();
15934
15935 // The Left side has to be a trunc.
15936 if (N0.getOpcode() != ISD::TRUNCATE)
15937 return SDValue();
15938
15939 // The type of the truncated inputs.
15940 EVT WideVT = N0->getOperand(0)->getValueType(0);
15941 if (WideVT != VT)
15942 return SDValue();
15943
15944 // The right side has to be a 'trunc' or a constant vector.
15945 bool RHSTrunc = N1.getOpcode() == ISD::TRUNCATE;
15946 bool RHSConst = (isSplatVector(N1.getNode()) &&
15947 isa<ConstantSDNode>(N1->getOperand(0)));
15948 if (!RHSTrunc && !RHSConst)
15949 return SDValue();
15950
15951 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
15952
15953 if (!TLI.isOperationLegalOrPromote(Narrow->getOpcode(), WideVT))
15954 return SDValue();
15955
15956 // Set N0 and N1 to hold the inputs to the new wide operation.
15957 N0 = N0->getOperand(0);
15958 if (RHSConst) {
15959 N1 = DAG.getNode(ISD::ZERO_EXTEND, DL, WideVT.getScalarType(),
15960 N1->getOperand(0));
15961 SmallVector<SDValue, 8> C(WideVT.getVectorNumElements(), N1);
15962 N1 = DAG.getNode(ISD::BUILD_VECTOR, DL, WideVT, &C[0], C.size());
15963 } else if (RHSTrunc) {
15964 N1 = N1->getOperand(0);
15965 }
15966
15967 // Generate the wide operation.
15968 SDValue Op = DAG.getNode(N->getOpcode(), DL, WideVT, N0, N1);
15969 unsigned Opcode = N->getOpcode();
15970 switch (Opcode) {
15971 case ISD::ANY_EXTEND:
15972 return Op;
15973 case ISD::ZERO_EXTEND: {
15974 unsigned InBits = NarrowVT.getScalarType().getSizeInBits();
15975 APInt Mask = APInt::getAllOnesValue(InBits);
15976 Mask = Mask.zext(VT.getScalarType().getSizeInBits());
15977 return DAG.getNode(ISD::AND, DL, VT,
15978 Op, DAG.getConstant(Mask, VT));
15979 }
15980 case ISD::SIGN_EXTEND:
15981 return DAG.getNode(ISD::SIGN_EXTEND_INREG, DL, VT,
15982 Op, DAG.getValueType(NarrowVT));
15983 default:
15984 llvm_unreachable("Unexpected opcode");
15985 }
15986}
15987
Nate Begemanb65c1752010-12-17 22:55:37 +000015988static SDValue PerformAndCombine(SDNode *N, SelectionDAG &DAG,
15989 TargetLowering::DAGCombinerInfo &DCI,
15990 const X86Subtarget *Subtarget) {
Nadav Rotemd6fb53a2012-12-27 08:15:45 +000015991 EVT VT = N->getValueType(0);
Nate Begemanb65c1752010-12-17 22:55:37 +000015992 if (DCI.isBeforeLegalizeOps())
15993 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000015994
Stuart Hastings865f0932011-06-03 23:53:54 +000015995 SDValue R = CMPEQCombine(N, DAG, DCI, Subtarget);
15996 if (R.getNode())
15997 return R;
15998
Craig Topperb926afc2012-12-17 05:12:30 +000015999 // Create BLSI, and BLSR instructions
Craig Topperb4c94572011-10-21 06:55:01 +000016000 // BLSI is X & (-X)
16001 // BLSR is X & (X-1)
Craig Topper54a11172011-10-14 07:06:56 +000016002 if (Subtarget->hasBMI() && (VT == MVT::i32 || VT == MVT::i64)) {
16003 SDValue N0 = N->getOperand(0);
16004 SDValue N1 = N->getOperand(1);
16005 DebugLoc DL = N->getDebugLoc();
16006
Craig Topperb4c94572011-10-21 06:55:01 +000016007 // Check LHS for neg
16008 if (N0.getOpcode() == ISD::SUB && N0.getOperand(1) == N1 &&
16009 isZero(N0.getOperand(0)))
16010 return DAG.getNode(X86ISD::BLSI, DL, VT, N1);
16011
16012 // Check RHS for neg
16013 if (N1.getOpcode() == ISD::SUB && N1.getOperand(1) == N0 &&
16014 isZero(N1.getOperand(0)))
16015 return DAG.getNode(X86ISD::BLSI, DL, VT, N0);
16016
16017 // Check LHS for X-1
16018 if (N0.getOpcode() == ISD::ADD && N0.getOperand(0) == N1 &&
16019 isAllOnes(N0.getOperand(1)))
16020 return DAG.getNode(X86ISD::BLSR, DL, VT, N1);
16021
16022 // Check RHS for X-1
16023 if (N1.getOpcode() == ISD::ADD && N1.getOperand(0) == N0 &&
16024 isAllOnes(N1.getOperand(1)))
16025 return DAG.getNode(X86ISD::BLSR, DL, VT, N0);
16026
Craig Topper54a11172011-10-14 07:06:56 +000016027 return SDValue();
16028 }
16029
Bruno Cardoso Lopes466b0222011-07-13 21:36:51 +000016030 // Want to form ANDNP nodes:
16031 // 1) In the hopes of then easily combining them with OR and AND nodes
16032 // to form PBLEND/PSIGN.
16033 // 2) To match ANDN packed intrinsics
Bruno Cardoso Lopes466b0222011-07-13 21:36:51 +000016034 if (VT != MVT::v2i64 && VT != MVT::v4i64)
Nate Begemanb65c1752010-12-17 22:55:37 +000016035 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000016036
Nate Begemanb65c1752010-12-17 22:55:37 +000016037 SDValue N0 = N->getOperand(0);
16038 SDValue N1 = N->getOperand(1);
16039 DebugLoc DL = N->getDebugLoc();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000016040
Nate Begemanb65c1752010-12-17 22:55:37 +000016041 // Check LHS for vnot
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000016042 if (N0.getOpcode() == ISD::XOR &&
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +000016043 //ISD::isBuildVectorAllOnes(N0.getOperand(1).getNode()))
16044 CanFoldXORWithAllOnes(N0.getOperand(1).getNode()))
Bruno Cardoso Lopesc1af4772011-07-13 21:36:47 +000016045 return DAG.getNode(X86ISD::ANDNP, DL, VT, N0.getOperand(0), N1);
Nate Begemanb65c1752010-12-17 22:55:37 +000016046
16047 // Check RHS for vnot
16048 if (N1.getOpcode() == ISD::XOR &&
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +000016049 //ISD::isBuildVectorAllOnes(N1.getOperand(1).getNode()))
16050 CanFoldXORWithAllOnes(N1.getOperand(1).getNode()))
Bruno Cardoso Lopesc1af4772011-07-13 21:36:47 +000016051 return DAG.getNode(X86ISD::ANDNP, DL, VT, N1.getOperand(0), N0);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000016052
Nate Begemanb65c1752010-12-17 22:55:37 +000016053 return SDValue();
16054}
16055
Evan Cheng760d1942010-01-04 21:22:48 +000016056static SDValue PerformOrCombine(SDNode *N, SelectionDAG &DAG,
Evan Cheng8b1190a2010-04-28 01:18:01 +000016057 TargetLowering::DAGCombinerInfo &DCI,
Evan Cheng760d1942010-01-04 21:22:48 +000016058 const X86Subtarget *Subtarget) {
Nadav Rotemd6fb53a2012-12-27 08:15:45 +000016059 EVT VT = N->getValueType(0);
Evan Cheng39cfeec2010-04-28 02:25:18 +000016060 if (DCI.isBeforeLegalizeOps())
Evan Cheng8b1190a2010-04-28 01:18:01 +000016061 return SDValue();
16062
Stuart Hastings865f0932011-06-03 23:53:54 +000016063 SDValue R = CMPEQCombine(N, DAG, DCI, Subtarget);
16064 if (R.getNode())
16065 return R;
16066
Evan Cheng760d1942010-01-04 21:22:48 +000016067 SDValue N0 = N->getOperand(0);
16068 SDValue N1 = N->getOperand(1);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000016069
Nate Begemanb65c1752010-12-17 22:55:37 +000016070 // look for psign/blend
Craig Topper1666cb62011-11-19 07:07:26 +000016071 if (VT == MVT::v2i64 || VT == MVT::v4i64) {
Craig Topperd0a31172012-01-10 06:37:29 +000016072 if (!Subtarget->hasSSSE3() ||
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000016073 (VT == MVT::v4i64 && !Subtarget->hasInt256()))
Craig Topper1666cb62011-11-19 07:07:26 +000016074 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000016075
Craig Topper1666cb62011-11-19 07:07:26 +000016076 // Canonicalize pandn to RHS
16077 if (N0.getOpcode() == X86ISD::ANDNP)
16078 std::swap(N0, N1);
Lang Hames9ffaa6a2012-01-10 22:53:20 +000016079 // or (and (m, y), (pandn m, x))
Craig Topper1666cb62011-11-19 07:07:26 +000016080 if (N0.getOpcode() == ISD::AND && N1.getOpcode() == X86ISD::ANDNP) {
16081 SDValue Mask = N1.getOperand(0);
16082 SDValue X = N1.getOperand(1);
16083 SDValue Y;
16084 if (N0.getOperand(0) == Mask)
16085 Y = N0.getOperand(1);
16086 if (N0.getOperand(1) == Mask)
16087 Y = N0.getOperand(0);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000016088
Craig Topper1666cb62011-11-19 07:07:26 +000016089 // Check to see if the mask appeared in both the AND and ANDNP and
16090 if (!Y.getNode())
16091 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000016092
Craig Topper1666cb62011-11-19 07:07:26 +000016093 // Validate that X, Y, and Mask are BIT_CONVERTS, and see through them.
Craig Topper1666cb62011-11-19 07:07:26 +000016094 // Look through mask bitcast.
Nadav Rotem4ac90812012-04-01 19:31:22 +000016095 if (Mask.getOpcode() == ISD::BITCAST)
16096 Mask = Mask.getOperand(0);
16097 if (X.getOpcode() == ISD::BITCAST)
16098 X = X.getOperand(0);
16099 if (Y.getOpcode() == ISD::BITCAST)
16100 Y = Y.getOperand(0);
16101
Craig Topper1666cb62011-11-19 07:07:26 +000016102 EVT MaskVT = Mask.getValueType();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000016103
Craig Toppered2e13d2012-01-22 19:15:14 +000016104 // Validate that the Mask operand is a vector sra node.
Craig Topper1666cb62011-11-19 07:07:26 +000016105 // FIXME: what to do for bytes, since there is a psignb/pblendvb, but
16106 // there is no psrai.b
Craig Topper7fb8b0c2012-01-23 06:46:22 +000016107 if (Mask.getOpcode() != X86ISD::VSRAI)
Craig Toppered2e13d2012-01-22 19:15:14 +000016108 return SDValue();
Craig Topper1666cb62011-11-19 07:07:26 +000016109
16110 // Check that the SRA is all signbits.
Craig Topper7fb8b0c2012-01-23 06:46:22 +000016111 SDValue SraC = Mask.getOperand(1);
Craig Topper1666cb62011-11-19 07:07:26 +000016112 unsigned SraAmt = cast<ConstantSDNode>(SraC)->getZExtValue();
16113 unsigned EltBits = MaskVT.getVectorElementType().getSizeInBits();
16114 if ((SraAmt + 1) != EltBits)
16115 return SDValue();
16116
16117 DebugLoc DL = N->getDebugLoc();
16118
Nadav Rotemaf59e9a2012-12-07 21:43:11 +000016119 // We are going to replace the AND, OR, NAND with either BLEND
16120 // or PSIGN, which only look at the MSB. The VSRAI instruction
16121 // does not affect the highest bit, so we can get rid of it.
16122 Mask = Mask.getOperand(0);
16123
Craig Topper1666cb62011-11-19 07:07:26 +000016124 // Now we know we at least have a plendvb with the mask val. See if
16125 // we can form a psignb/w/d.
16126 // psign = x.type == y.type == mask.type && y = sub(0, x);
Craig Topper1666cb62011-11-19 07:07:26 +000016127 if (Y.getOpcode() == ISD::SUB && Y.getOperand(1) == X &&
16128 ISD::isBuildVectorAllZeros(Y.getOperand(0).getNode()) &&
Craig Toppered2e13d2012-01-22 19:15:14 +000016129 X.getValueType() == MaskVT && Y.getValueType() == MaskVT) {
16130 assert((EltBits == 8 || EltBits == 16 || EltBits == 32) &&
16131 "Unsupported VT for PSIGN");
Nadav Rotemaf59e9a2012-12-07 21:43:11 +000016132 Mask = DAG.getNode(X86ISD::PSIGN, DL, MaskVT, X, Mask);
Craig Toppered2e13d2012-01-22 19:15:14 +000016133 return DAG.getNode(ISD::BITCAST, DL, VT, Mask);
Craig Topper1666cb62011-11-19 07:07:26 +000016134 }
16135 // PBLENDVB only available on SSE 4.1
Craig Topperd0a31172012-01-10 06:37:29 +000016136 if (!Subtarget->hasSSE41())
Craig Topper1666cb62011-11-19 07:07:26 +000016137 return SDValue();
16138
16139 EVT BlendVT = (VT == MVT::v4i64) ? MVT::v32i8 : MVT::v16i8;
16140
16141 X = DAG.getNode(ISD::BITCAST, DL, BlendVT, X);
16142 Y = DAG.getNode(ISD::BITCAST, DL, BlendVT, Y);
16143 Mask = DAG.getNode(ISD::BITCAST, DL, BlendVT, Mask);
Nadav Rotem18197d72011-11-30 10:13:37 +000016144 Mask = DAG.getNode(ISD::VSELECT, DL, BlendVT, Mask, Y, X);
Craig Topper1666cb62011-11-19 07:07:26 +000016145 return DAG.getNode(ISD::BITCAST, DL, VT, Mask);
Nate Begemanb65c1752010-12-17 22:55:37 +000016146 }
16147 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000016148
Craig Topper1666cb62011-11-19 07:07:26 +000016149 if (VT != MVT::i16 && VT != MVT::i32 && VT != MVT::i64)
16150 return SDValue();
16151
Nate Begemanb65c1752010-12-17 22:55:37 +000016152 // fold (or (x << c) | (y >> (64 - c))) ==> (shld64 x, y, c)
Evan Cheng760d1942010-01-04 21:22:48 +000016153 if (N0.getOpcode() == ISD::SRL && N1.getOpcode() == ISD::SHL)
16154 std::swap(N0, N1);
16155 if (N0.getOpcode() != ISD::SHL || N1.getOpcode() != ISD::SRL)
16156 return SDValue();
Evan Cheng8b1190a2010-04-28 01:18:01 +000016157 if (!N0.hasOneUse() || !N1.hasOneUse())
16158 return SDValue();
Evan Cheng760d1942010-01-04 21:22:48 +000016159
16160 SDValue ShAmt0 = N0.getOperand(1);
16161 if (ShAmt0.getValueType() != MVT::i8)
16162 return SDValue();
16163 SDValue ShAmt1 = N1.getOperand(1);
16164 if (ShAmt1.getValueType() != MVT::i8)
16165 return SDValue();
16166 if (ShAmt0.getOpcode() == ISD::TRUNCATE)
16167 ShAmt0 = ShAmt0.getOperand(0);
16168 if (ShAmt1.getOpcode() == ISD::TRUNCATE)
16169 ShAmt1 = ShAmt1.getOperand(0);
16170
16171 DebugLoc DL = N->getDebugLoc();
16172 unsigned Opc = X86ISD::SHLD;
16173 SDValue Op0 = N0.getOperand(0);
16174 SDValue Op1 = N1.getOperand(0);
16175 if (ShAmt0.getOpcode() == ISD::SUB) {
16176 Opc = X86ISD::SHRD;
16177 std::swap(Op0, Op1);
16178 std::swap(ShAmt0, ShAmt1);
16179 }
16180
Evan Cheng8b1190a2010-04-28 01:18:01 +000016181 unsigned Bits = VT.getSizeInBits();
Evan Cheng760d1942010-01-04 21:22:48 +000016182 if (ShAmt1.getOpcode() == ISD::SUB) {
16183 SDValue Sum = ShAmt1.getOperand(0);
16184 if (ConstantSDNode *SumC = dyn_cast<ConstantSDNode>(Sum)) {
Dan Gohman4e39e9d2010-06-24 14:30:44 +000016185 SDValue ShAmt1Op1 = ShAmt1.getOperand(1);
16186 if (ShAmt1Op1.getNode()->getOpcode() == ISD::TRUNCATE)
16187 ShAmt1Op1 = ShAmt1Op1.getOperand(0);
16188 if (SumC->getSExtValue() == Bits && ShAmt1Op1 == ShAmt0)
Evan Cheng760d1942010-01-04 21:22:48 +000016189 return DAG.getNode(Opc, DL, VT,
16190 Op0, Op1,
16191 DAG.getNode(ISD::TRUNCATE, DL,
16192 MVT::i8, ShAmt0));
16193 }
16194 } else if (ConstantSDNode *ShAmt1C = dyn_cast<ConstantSDNode>(ShAmt1)) {
16195 ConstantSDNode *ShAmt0C = dyn_cast<ConstantSDNode>(ShAmt0);
16196 if (ShAmt0C &&
Evan Cheng8b1190a2010-04-28 01:18:01 +000016197 ShAmt0C->getSExtValue() + ShAmt1C->getSExtValue() == Bits)
Evan Cheng760d1942010-01-04 21:22:48 +000016198 return DAG.getNode(Opc, DL, VT,
16199 N0.getOperand(0), N1.getOperand(0),
16200 DAG.getNode(ISD::TRUNCATE, DL,
16201 MVT::i8, ShAmt0));
16202 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000016203
Evan Cheng760d1942010-01-04 21:22:48 +000016204 return SDValue();
16205}
16206
Manman Ren92363622012-06-07 22:39:10 +000016207// Generate NEG and CMOV for integer abs.
16208static SDValue performIntegerAbsCombine(SDNode *N, SelectionDAG &DAG) {
16209 EVT VT = N->getValueType(0);
16210
16211 // Since X86 does not have CMOV for 8-bit integer, we don't convert
16212 // 8-bit integer abs to NEG and CMOV.
16213 if (VT.isInteger() && VT.getSizeInBits() == 8)
16214 return SDValue();
16215
16216 SDValue N0 = N->getOperand(0);
16217 SDValue N1 = N->getOperand(1);
16218 DebugLoc DL = N->getDebugLoc();
16219
16220 // Check pattern of XOR(ADD(X,Y), Y) where Y is SRA(X, size(X)-1)
16221 // and change it to SUB and CMOV.
16222 if (VT.isInteger() && N->getOpcode() == ISD::XOR &&
16223 N0.getOpcode() == ISD::ADD &&
16224 N0.getOperand(1) == N1 &&
16225 N1.getOpcode() == ISD::SRA &&
16226 N1.getOperand(0) == N0.getOperand(0))
16227 if (ConstantSDNode *Y1C = dyn_cast<ConstantSDNode>(N1.getOperand(1)))
16228 if (Y1C->getAPIntValue() == VT.getSizeInBits()-1) {
16229 // Generate SUB & CMOV.
16230 SDValue Neg = DAG.getNode(X86ISD::SUB, DL, DAG.getVTList(VT, MVT::i32),
16231 DAG.getConstant(0, VT), N0.getOperand(0));
16232
16233 SDValue Ops[] = { N0.getOperand(0), Neg,
16234 DAG.getConstant(X86::COND_GE, MVT::i8),
16235 SDValue(Neg.getNode(), 1) };
16236 return DAG.getNode(X86ISD::CMOV, DL, DAG.getVTList(VT, MVT::Glue),
16237 Ops, array_lengthof(Ops));
16238 }
16239 return SDValue();
16240}
16241
Craig Topper3738ccd2011-12-27 06:27:23 +000016242// PerformXorCombine - Attempts to turn XOR nodes into BLSMSK nodes
Craig Topperb4c94572011-10-21 06:55:01 +000016243static SDValue PerformXorCombine(SDNode *N, SelectionDAG &DAG,
16244 TargetLowering::DAGCombinerInfo &DCI,
16245 const X86Subtarget *Subtarget) {
Nadav Rotemd6fb53a2012-12-27 08:15:45 +000016246 EVT VT = N->getValueType(0);
Craig Topperb4c94572011-10-21 06:55:01 +000016247 if (DCI.isBeforeLegalizeOps())
16248 return SDValue();
16249
Manman Ren45d53b82012-06-08 18:58:26 +000016250 if (Subtarget->hasCMov()) {
16251 SDValue RV = performIntegerAbsCombine(N, DAG);
16252 if (RV.getNode())
16253 return RV;
16254 }
Manman Ren92363622012-06-07 22:39:10 +000016255
16256 // Try forming BMI if it is available.
16257 if (!Subtarget->hasBMI())
16258 return SDValue();
16259
Craig Topperb4c94572011-10-21 06:55:01 +000016260 if (VT != MVT::i32 && VT != MVT::i64)
16261 return SDValue();
16262
Craig Topper3738ccd2011-12-27 06:27:23 +000016263 assert(Subtarget->hasBMI() && "Creating BLSMSK requires BMI instructions");
16264
Craig Topperb4c94572011-10-21 06:55:01 +000016265 // Create BLSMSK instructions by finding X ^ (X-1)
16266 SDValue N0 = N->getOperand(0);
16267 SDValue N1 = N->getOperand(1);
16268 DebugLoc DL = N->getDebugLoc();
16269
16270 if (N0.getOpcode() == ISD::ADD && N0.getOperand(0) == N1 &&
16271 isAllOnes(N0.getOperand(1)))
16272 return DAG.getNode(X86ISD::BLSMSK, DL, VT, N1);
16273
16274 if (N1.getOpcode() == ISD::ADD && N1.getOperand(0) == N0 &&
16275 isAllOnes(N1.getOperand(1)))
16276 return DAG.getNode(X86ISD::BLSMSK, DL, VT, N0);
16277
16278 return SDValue();
16279}
16280
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016281/// PerformLOADCombine - Do target-specific dag combines on LOAD nodes.
16282static SDValue PerformLOADCombine(SDNode *N, SelectionDAG &DAG,
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000016283 TargetLowering::DAGCombinerInfo &DCI,
16284 const X86Subtarget *Subtarget) {
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016285 LoadSDNode *Ld = cast<LoadSDNode>(N);
16286 EVT RegVT = Ld->getValueType(0);
16287 EVT MemVT = Ld->getMemoryVT();
16288 DebugLoc dl = Ld->getDebugLoc();
16289 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
16290
16291 ISD::LoadExtType Ext = Ld->getExtensionType();
16292
Nadav Rotemca6f2962011-09-18 19:00:23 +000016293 // If this is a vector EXT Load then attempt to optimize it using a
Benjamin Kramer17347912012-12-22 11:34:28 +000016294 // shuffle. If SSSE3 is not available we may emit an illegal shuffle but the
16295 // expansion is still better than scalar code.
16296 // We generate X86ISD::VSEXT for SEXTLOADs if it's available, otherwise we'll
16297 // emit a shuffle and a arithmetic shift.
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016298 // TODO: It is possible to support ZExt by zeroing the undef values
16299 // during the shuffle phase or after the shuffle.
Benjamin Kramer17347912012-12-22 11:34:28 +000016300 if (RegVT.isVector() && RegVT.isInteger() && Subtarget->hasSSE2() &&
16301 (Ext == ISD::EXTLOAD || Ext == ISD::SEXTLOAD)) {
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016302 assert(MemVT != RegVT && "Cannot extend to the same type");
16303 assert(MemVT.isVector() && "Must load a vector from memory");
16304
16305 unsigned NumElems = RegVT.getVectorNumElements();
16306 unsigned RegSz = RegVT.getSizeInBits();
16307 unsigned MemSz = MemVT.getSizeInBits();
16308 assert(RegSz > MemSz && "Register size must be greater than the mem size");
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016309
Elena Demikhovsky4b977312012-12-19 07:50:20 +000016310 if (Ext == ISD::SEXTLOAD && RegSz == 256 && !Subtarget->hasInt256())
16311 return SDValue();
16312
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000016313 // All sizes must be a power of two.
16314 if (!isPowerOf2_32(RegSz * MemSz * NumElems))
16315 return SDValue();
16316
16317 // Attempt to load the original value using scalar loads.
16318 // Find the largest scalar type that divides the total loaded size.
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016319 MVT SclrLoadTy = MVT::i8;
16320 for (unsigned tp = MVT::FIRST_INTEGER_VALUETYPE;
16321 tp < MVT::LAST_INTEGER_VALUETYPE; ++tp) {
16322 MVT Tp = (MVT::SimpleValueType)tp;
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000016323 if (TLI.isTypeLegal(Tp) && ((MemSz % Tp.getSizeInBits()) == 0)) {
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016324 SclrLoadTy = Tp;
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016325 }
16326 }
16327
Nadav Rotem5cd95e12012-07-11 13:27:05 +000016328 // On 32bit systems, we can't save 64bit integers. Try bitcasting to F64.
16329 if (TLI.isTypeLegal(MVT::f64) && SclrLoadTy.getSizeInBits() < 64 &&
16330 (64 <= MemSz))
16331 SclrLoadTy = MVT::f64;
16332
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000016333 // Calculate the number of scalar loads that we need to perform
16334 // in order to load our vector from memory.
16335 unsigned NumLoads = MemSz / SclrLoadTy.getSizeInBits();
Elena Demikhovsky4b977312012-12-19 07:50:20 +000016336 if (Ext == ISD::SEXTLOAD && NumLoads > 1)
16337 return SDValue();
16338
16339 unsigned loadRegZize = RegSz;
16340 if (Ext == ISD::SEXTLOAD && RegSz == 256)
16341 loadRegZize /= 2;
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016342
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000016343 // Represent our vector as a sequence of elements which are the
16344 // largest scalar that we can load.
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016345 EVT LoadUnitVecVT = EVT::getVectorVT(*DAG.getContext(), SclrLoadTy,
Elena Demikhovsky4b977312012-12-19 07:50:20 +000016346 loadRegZize/SclrLoadTy.getSizeInBits());
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016347
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000016348 // Represent the data using the same element type that is stored in
16349 // memory. In practice, we ''widen'' MemVT.
Elena Demikhovsky4b977312012-12-19 07:50:20 +000016350 EVT WideVecVT =
16351 EVT::getVectorVT(*DAG.getContext(), MemVT.getScalarType(),
16352 loadRegZize/MemVT.getScalarType().getSizeInBits());
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016353
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000016354 assert(WideVecVT.getSizeInBits() == LoadUnitVecVT.getSizeInBits() &&
16355 "Invalid vector type");
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016356
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000016357 // We can't shuffle using an illegal type.
16358 if (!TLI.isTypeLegal(WideVecVT))
16359 return SDValue();
16360
16361 SmallVector<SDValue, 8> Chains;
16362 SDValue Ptr = Ld->getBasePtr();
16363 SDValue Increment = DAG.getConstant(SclrLoadTy.getSizeInBits()/8,
16364 TLI.getPointerTy());
16365 SDValue Res = DAG.getUNDEF(LoadUnitVecVT);
16366
16367 for (unsigned i = 0; i < NumLoads; ++i) {
16368 // Perform a single load.
16369 SDValue ScalarLoad = DAG.getLoad(SclrLoadTy, dl, Ld->getChain(),
16370 Ptr, Ld->getPointerInfo(),
16371 Ld->isVolatile(), Ld->isNonTemporal(),
16372 Ld->isInvariant(), Ld->getAlignment());
16373 Chains.push_back(ScalarLoad.getValue(1));
16374 // Create the first element type using SCALAR_TO_VECTOR in order to avoid
16375 // another round of DAGCombining.
16376 if (i == 0)
16377 Res = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, LoadUnitVecVT, ScalarLoad);
16378 else
16379 Res = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, LoadUnitVecVT, Res,
16380 ScalarLoad, DAG.getIntPtrConstant(i));
16381
16382 Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr, Increment);
16383 }
16384
16385 SDValue TF = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &Chains[0],
16386 Chains.size());
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016387
16388 // Bitcast the loaded value to a vector of the original element type, in
16389 // the size of the target vector type.
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000016390 SDValue SlicedVec = DAG.getNode(ISD::BITCAST, dl, WideVecVT, Res);
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016391 unsigned SizeRatio = RegSz/MemSz;
16392
Elena Demikhovsky4b977312012-12-19 07:50:20 +000016393 if (Ext == ISD::SEXTLOAD) {
Benjamin Kramer17347912012-12-22 11:34:28 +000016394 // If we have SSE4.1 we can directly emit a VSEXT node.
16395 if (Subtarget->hasSSE41()) {
16396 SDValue Sext = DAG.getNode(X86ISD::VSEXT, dl, RegVT, SlicedVec);
16397 return DCI.CombineTo(N, Sext, TF, true);
16398 }
16399
16400 // Otherwise we'll shuffle the small elements in the high bits of the
16401 // larger type and perform an arithmetic shift. If the shift is not legal
16402 // it's better to scalarize.
16403 if (!TLI.isOperationLegalOrCustom(ISD::SRA, RegVT))
16404 return SDValue();
16405
16406 // Redistribute the loaded elements into the different locations.
16407 SmallVector<int, 8> ShuffleVec(NumElems * SizeRatio, -1);
16408 for (unsigned i = 0; i != NumElems; ++i)
16409 ShuffleVec[i*SizeRatio + SizeRatio-1] = i;
16410
16411 SDValue Shuff = DAG.getVectorShuffle(WideVecVT, dl, SlicedVec,
16412 DAG.getUNDEF(WideVecVT),
16413 &ShuffleVec[0]);
16414
16415 Shuff = DAG.getNode(ISD::BITCAST, dl, RegVT, Shuff);
16416
16417 // Build the arithmetic shift.
16418 unsigned Amt = RegVT.getVectorElementType().getSizeInBits() -
16419 MemVT.getVectorElementType().getSizeInBits();
16420 SmallVector<SDValue, 8> C(NumElems,
16421 DAG.getConstant(Amt, RegVT.getScalarType()));
16422 SDValue BV = DAG.getNode(ISD::BUILD_VECTOR, dl, RegVT, &C[0], C.size());
16423 Shuff = DAG.getNode(ISD::SRA, dl, RegVT, Shuff, BV);
16424
16425 return DCI.CombineTo(N, Shuff, TF, true);
Elena Demikhovsky4b977312012-12-19 07:50:20 +000016426 }
Benjamin Kramer17347912012-12-22 11:34:28 +000016427
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016428 // Redistribute the loaded elements into the different locations.
16429 SmallVector<int, 8> ShuffleVec(NumElems * SizeRatio, -1);
Craig Topper31a207a2012-05-04 06:39:13 +000016430 for (unsigned i = 0; i != NumElems; ++i)
16431 ShuffleVec[i*SizeRatio] = i;
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016432
16433 SDValue Shuff = DAG.getVectorShuffle(WideVecVT, dl, SlicedVec,
Craig Topperdf966f62012-04-22 19:17:57 +000016434 DAG.getUNDEF(WideVecVT),
16435 &ShuffleVec[0]);
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016436
16437 // Bitcast to the requested type.
16438 Shuff = DAG.getNode(ISD::BITCAST, dl, RegVT, Shuff);
16439 // Replace the original load with the new sequence
16440 // and return the new chain.
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000016441 return DCI.CombineTo(N, Shuff, TF, true);
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016442 }
16443
16444 return SDValue();
16445}
16446
Chris Lattner149a4e52008-02-22 02:09:43 +000016447/// PerformSTORECombine - Do target-specific dag combines on STORE nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000016448static SDValue PerformSTORECombine(SDNode *N, SelectionDAG &DAG,
Evan Cheng536e6672009-03-12 05:59:15 +000016449 const X86Subtarget *Subtarget) {
Nadav Rotem614061b2011-08-10 19:30:14 +000016450 StoreSDNode *St = cast<StoreSDNode>(N);
16451 EVT VT = St->getValue().getValueType();
16452 EVT StVT = St->getMemoryVT();
16453 DebugLoc dl = St->getDebugLoc();
Nadav Rotem5e742a32011-08-11 16:41:21 +000016454 SDValue StoredVal = St->getOperand(1);
16455 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
16456
Nick Lewycky8a8d4792011-12-02 22:16:29 +000016457 // If we are saving a concatenation of two XMM registers, perform two stores.
Nadav Rotem87d35e82012-05-19 20:30:08 +000016458 // On Sandy Bridge, 256-bit memory operations are executed by two
16459 // 128-bit ports. However, on Haswell it is better to issue a single 256-bit
16460 // memory operation.
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000016461 if (VT.is256BitVector() && !Subtarget->hasInt256() &&
Craig Topperb4a8aef2012-04-27 21:05:09 +000016462 StoredVal.getNode()->getOpcode() == ISD::CONCAT_VECTORS &&
16463 StoredVal.getNumOperands() == 2) {
Nadav Rotem5e742a32011-08-11 16:41:21 +000016464 SDValue Value0 = StoredVal.getOperand(0);
16465 SDValue Value1 = StoredVal.getOperand(1);
16466
16467 SDValue Stride = DAG.getConstant(16, TLI.getPointerTy());
16468 SDValue Ptr0 = St->getBasePtr();
16469 SDValue Ptr1 = DAG.getNode(ISD::ADD, dl, Ptr0.getValueType(), Ptr0, Stride);
16470
16471 SDValue Ch0 = DAG.getStore(St->getChain(), dl, Value0, Ptr0,
16472 St->getPointerInfo(), St->isVolatile(),
16473 St->isNonTemporal(), St->getAlignment());
16474 SDValue Ch1 = DAG.getStore(St->getChain(), dl, Value1, Ptr1,
16475 St->getPointerInfo(), St->isVolatile(),
16476 St->isNonTemporal(), St->getAlignment());
16477 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Ch0, Ch1);
16478 }
Nadav Rotem614061b2011-08-10 19:30:14 +000016479
16480 // Optimize trunc store (of multiple scalars) to shuffle and store.
16481 // First, pack all of the elements in one place. Next, store to memory
16482 // in fewer chunks.
16483 if (St->isTruncatingStore() && VT.isVector()) {
16484 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
16485 unsigned NumElems = VT.getVectorNumElements();
16486 assert(StVT != VT && "Cannot truncate to the same type");
16487 unsigned FromSz = VT.getVectorElementType().getSizeInBits();
16488 unsigned ToSz = StVT.getVectorElementType().getSizeInBits();
16489
16490 // From, To sizes and ElemCount must be pow of two
16491 if (!isPowerOf2_32(NumElems * FromSz * ToSz)) return SDValue();
Nadav Rotem9c6cdf42011-09-21 08:45:10 +000016492 // We are going to use the original vector elt for storing.
Nadav Rotem64ac73b2011-09-21 17:14:40 +000016493 // Accumulated smaller vector elements must be a multiple of the store size.
Nadav Rotem9c6cdf42011-09-21 08:45:10 +000016494 if (0 != (NumElems * FromSz) % ToSz) return SDValue();
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016495
Nadav Rotem614061b2011-08-10 19:30:14 +000016496 unsigned SizeRatio = FromSz / ToSz;
16497
16498 assert(SizeRatio * NumElems * ToSz == VT.getSizeInBits());
16499
16500 // Create a type on which we perform the shuffle
16501 EVT WideVecVT = EVT::getVectorVT(*DAG.getContext(),
16502 StVT.getScalarType(), NumElems*SizeRatio);
16503
16504 assert(WideVecVT.getSizeInBits() == VT.getSizeInBits());
16505
16506 SDValue WideVec = DAG.getNode(ISD::BITCAST, dl, WideVecVT, St->getValue());
16507 SmallVector<int, 8> ShuffleVec(NumElems * SizeRatio, -1);
Craig Topper31a207a2012-05-04 06:39:13 +000016508 for (unsigned i = 0; i != NumElems; ++i)
16509 ShuffleVec[i] = i * SizeRatio;
Nadav Rotem614061b2011-08-10 19:30:14 +000016510
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000016511 // Can't shuffle using an illegal type.
16512 if (!TLI.isTypeLegal(WideVecVT))
16513 return SDValue();
Nadav Rotem614061b2011-08-10 19:30:14 +000016514
16515 SDValue Shuff = DAG.getVectorShuffle(WideVecVT, dl, WideVec,
Craig Topperdf966f62012-04-22 19:17:57 +000016516 DAG.getUNDEF(WideVecVT),
16517 &ShuffleVec[0]);
Nadav Rotem614061b2011-08-10 19:30:14 +000016518 // At this point all of the data is stored at the bottom of the
16519 // register. We now need to save it to mem.
16520
16521 // Find the largest store unit
16522 MVT StoreType = MVT::i8;
16523 for (unsigned tp = MVT::FIRST_INTEGER_VALUETYPE;
16524 tp < MVT::LAST_INTEGER_VALUETYPE; ++tp) {
16525 MVT Tp = (MVT::SimpleValueType)tp;
Nadav Rotem5cd95e12012-07-11 13:27:05 +000016526 if (TLI.isTypeLegal(Tp) && Tp.getSizeInBits() <= NumElems * ToSz)
Nadav Rotem614061b2011-08-10 19:30:14 +000016527 StoreType = Tp;
16528 }
16529
Nadav Rotem5cd95e12012-07-11 13:27:05 +000016530 // On 32bit systems, we can't save 64bit integers. Try bitcasting to F64.
16531 if (TLI.isTypeLegal(MVT::f64) && StoreType.getSizeInBits() < 64 &&
16532 (64 <= NumElems * ToSz))
16533 StoreType = MVT::f64;
16534
Nadav Rotem614061b2011-08-10 19:30:14 +000016535 // Bitcast the original vector into a vector of store-size units
16536 EVT StoreVecVT = EVT::getVectorVT(*DAG.getContext(),
Nadav Rotem5cd95e12012-07-11 13:27:05 +000016537 StoreType, VT.getSizeInBits()/StoreType.getSizeInBits());
Nadav Rotem614061b2011-08-10 19:30:14 +000016538 assert(StoreVecVT.getSizeInBits() == VT.getSizeInBits());
16539 SDValue ShuffWide = DAG.getNode(ISD::BITCAST, dl, StoreVecVT, Shuff);
16540 SmallVector<SDValue, 8> Chains;
16541 SDValue Increment = DAG.getConstant(StoreType.getSizeInBits()/8,
16542 TLI.getPointerTy());
16543 SDValue Ptr = St->getBasePtr();
16544
16545 // Perform one or more big stores into memory.
Craig Topper31a207a2012-05-04 06:39:13 +000016546 for (unsigned i=0, e=(ToSz*NumElems)/StoreType.getSizeInBits(); i!=e; ++i) {
Nadav Rotem614061b2011-08-10 19:30:14 +000016547 SDValue SubVec = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl,
16548 StoreType, ShuffWide,
16549 DAG.getIntPtrConstant(i));
16550 SDValue Ch = DAG.getStore(St->getChain(), dl, SubVec, Ptr,
16551 St->getPointerInfo(), St->isVolatile(),
16552 St->isNonTemporal(), St->getAlignment());
16553 Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr, Increment);
16554 Chains.push_back(Ch);
16555 }
16556
16557 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &Chains[0],
16558 Chains.size());
16559 }
16560
Chris Lattner149a4e52008-02-22 02:09:43 +000016561 // Turn load->store of MMX types into GPR load/stores. This avoids clobbering
16562 // the FP state in cases where an emms may be missing.
Dale Johannesen079f2a62008-02-25 19:20:14 +000016563 // A preferable solution to the general problem is to figure out the right
16564 // places to insert EMMS. This qualifies as a quick hack.
Evan Cheng536e6672009-03-12 05:59:15 +000016565
16566 // Similarly, turn load->store of i64 into double load/stores in 32-bit mode.
Evan Cheng536e6672009-03-12 05:59:15 +000016567 if (VT.getSizeInBits() != 64)
16568 return SDValue();
16569
Devang Patel578efa92009-06-05 21:57:13 +000016570 const Function *F = DAG.getMachineFunction().getFunction();
Bill Wendling67658342012-10-09 07:45:08 +000016571 bool NoImplicitFloatOps = F->getFnAttributes().
Bill Wendling034b94b2012-12-19 07:18:57 +000016572 hasAttribute(Attribute::NoImplicitFloat);
Nick Lewycky8a8d4792011-12-02 22:16:29 +000016573 bool F64IsLegal = !DAG.getTarget().Options.UseSoftFloat && !NoImplicitFloatOps
Craig Topper1accb7e2012-01-10 06:54:16 +000016574 && Subtarget->hasSSE2();
Evan Cheng536e6672009-03-12 05:59:15 +000016575 if ((VT.isVector() ||
Owen Anderson825b72b2009-08-11 20:47:22 +000016576 (VT == MVT::i64 && F64IsLegal && !Subtarget->is64Bit())) &&
Dale Johannesen079f2a62008-02-25 19:20:14 +000016577 isa<LoadSDNode>(St->getValue()) &&
16578 !cast<LoadSDNode>(St->getValue())->isVolatile() &&
16579 St->getChain().hasOneUse() && !St->isVolatile()) {
Gabor Greifba36cb52008-08-28 21:40:38 +000016580 SDNode* LdVal = St->getValue().getNode();
Dale Johannesen079f2a62008-02-25 19:20:14 +000016581 LoadSDNode *Ld = 0;
16582 int TokenFactorIndex = -1;
Dan Gohman475871a2008-07-27 21:46:04 +000016583 SmallVector<SDValue, 8> Ops;
Gabor Greifba36cb52008-08-28 21:40:38 +000016584 SDNode* ChainVal = St->getChain().getNode();
Dale Johannesen079f2a62008-02-25 19:20:14 +000016585 // Must be a store of a load. We currently handle two cases: the load
16586 // is a direct child, and it's under an intervening TokenFactor. It is
16587 // possible to dig deeper under nested TokenFactors.
Dale Johannesen14e2ea92008-02-25 22:29:22 +000016588 if (ChainVal == LdVal)
Dale Johannesen079f2a62008-02-25 19:20:14 +000016589 Ld = cast<LoadSDNode>(St->getChain());
16590 else if (St->getValue().hasOneUse() &&
16591 ChainVal->getOpcode() == ISD::TokenFactor) {
Chad Rosierc2348d52012-02-01 18:45:51 +000016592 for (unsigned i = 0, e = ChainVal->getNumOperands(); i != e; ++i) {
Gabor Greifba36cb52008-08-28 21:40:38 +000016593 if (ChainVal->getOperand(i).getNode() == LdVal) {
Dale Johannesen079f2a62008-02-25 19:20:14 +000016594 TokenFactorIndex = i;
16595 Ld = cast<LoadSDNode>(St->getValue());
16596 } else
16597 Ops.push_back(ChainVal->getOperand(i));
16598 }
16599 }
Dale Johannesen079f2a62008-02-25 19:20:14 +000016600
Evan Cheng536e6672009-03-12 05:59:15 +000016601 if (!Ld || !ISD::isNormalLoad(Ld))
16602 return SDValue();
Dale Johannesen079f2a62008-02-25 19:20:14 +000016603
Evan Cheng536e6672009-03-12 05:59:15 +000016604 // If this is not the MMX case, i.e. we are just turning i64 load/store
16605 // into f64 load/store, avoid the transformation if there are multiple
16606 // uses of the loaded value.
16607 if (!VT.isVector() && !Ld->hasNUsesOfValue(1, 0))
16608 return SDValue();
Dale Johannesen079f2a62008-02-25 19:20:14 +000016609
Evan Cheng536e6672009-03-12 05:59:15 +000016610 DebugLoc LdDL = Ld->getDebugLoc();
16611 DebugLoc StDL = N->getDebugLoc();
16612 // If we are a 64-bit capable x86, lower to a single movq load/store pair.
16613 // Otherwise, if it's legal to use f64 SSE instructions, use f64 load/store
16614 // pair instead.
16615 if (Subtarget->is64Bit() || F64IsLegal) {
Owen Anderson825b72b2009-08-11 20:47:22 +000016616 EVT LdVT = Subtarget->is64Bit() ? MVT::i64 : MVT::f64;
Chris Lattner51abfe42010-09-21 06:02:19 +000016617 SDValue NewLd = DAG.getLoad(LdVT, LdDL, Ld->getChain(), Ld->getBasePtr(),
16618 Ld->getPointerInfo(), Ld->isVolatile(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000016619 Ld->isNonTemporal(), Ld->isInvariant(),
16620 Ld->getAlignment());
Evan Cheng536e6672009-03-12 05:59:15 +000016621 SDValue NewChain = NewLd.getValue(1);
Dale Johannesen079f2a62008-02-25 19:20:14 +000016622 if (TokenFactorIndex != -1) {
Evan Cheng536e6672009-03-12 05:59:15 +000016623 Ops.push_back(NewChain);
Owen Anderson825b72b2009-08-11 20:47:22 +000016624 NewChain = DAG.getNode(ISD::TokenFactor, LdDL, MVT::Other, &Ops[0],
Dale Johannesen079f2a62008-02-25 19:20:14 +000016625 Ops.size());
16626 }
Evan Cheng536e6672009-03-12 05:59:15 +000016627 return DAG.getStore(NewChain, StDL, NewLd, St->getBasePtr(),
Chris Lattner51abfe42010-09-21 06:02:19 +000016628 St->getPointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +000016629 St->isVolatile(), St->isNonTemporal(),
16630 St->getAlignment());
Chris Lattner149a4e52008-02-22 02:09:43 +000016631 }
Evan Cheng536e6672009-03-12 05:59:15 +000016632
16633 // Otherwise, lower to two pairs of 32-bit loads / stores.
16634 SDValue LoAddr = Ld->getBasePtr();
Owen Anderson825b72b2009-08-11 20:47:22 +000016635 SDValue HiAddr = DAG.getNode(ISD::ADD, LdDL, MVT::i32, LoAddr,
16636 DAG.getConstant(4, MVT::i32));
Evan Cheng536e6672009-03-12 05:59:15 +000016637
Owen Anderson825b72b2009-08-11 20:47:22 +000016638 SDValue LoLd = DAG.getLoad(MVT::i32, LdDL, Ld->getChain(), LoAddr,
Chris Lattner51abfe42010-09-21 06:02:19 +000016639 Ld->getPointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +000016640 Ld->isVolatile(), Ld->isNonTemporal(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000016641 Ld->isInvariant(), Ld->getAlignment());
Owen Anderson825b72b2009-08-11 20:47:22 +000016642 SDValue HiLd = DAG.getLoad(MVT::i32, LdDL, Ld->getChain(), HiAddr,
Chris Lattner51abfe42010-09-21 06:02:19 +000016643 Ld->getPointerInfo().getWithOffset(4),
David Greene67c9d422010-02-15 16:53:33 +000016644 Ld->isVolatile(), Ld->isNonTemporal(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000016645 Ld->isInvariant(),
Evan Cheng536e6672009-03-12 05:59:15 +000016646 MinAlign(Ld->getAlignment(), 4));
16647
16648 SDValue NewChain = LoLd.getValue(1);
16649 if (TokenFactorIndex != -1) {
16650 Ops.push_back(LoLd);
16651 Ops.push_back(HiLd);
Owen Anderson825b72b2009-08-11 20:47:22 +000016652 NewChain = DAG.getNode(ISD::TokenFactor, LdDL, MVT::Other, &Ops[0],
Evan Cheng536e6672009-03-12 05:59:15 +000016653 Ops.size());
16654 }
16655
16656 LoAddr = St->getBasePtr();
Owen Anderson825b72b2009-08-11 20:47:22 +000016657 HiAddr = DAG.getNode(ISD::ADD, StDL, MVT::i32, LoAddr,
16658 DAG.getConstant(4, MVT::i32));
Evan Cheng536e6672009-03-12 05:59:15 +000016659
16660 SDValue LoSt = DAG.getStore(NewChain, StDL, LoLd, LoAddr,
Chris Lattner8026a9d2010-09-21 17:50:43 +000016661 St->getPointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +000016662 St->isVolatile(), St->isNonTemporal(),
16663 St->getAlignment());
Evan Cheng536e6672009-03-12 05:59:15 +000016664 SDValue HiSt = DAG.getStore(NewChain, StDL, HiLd, HiAddr,
Chris Lattner8026a9d2010-09-21 17:50:43 +000016665 St->getPointerInfo().getWithOffset(4),
Evan Cheng536e6672009-03-12 05:59:15 +000016666 St->isVolatile(),
David Greene67c9d422010-02-15 16:53:33 +000016667 St->isNonTemporal(),
Evan Cheng536e6672009-03-12 05:59:15 +000016668 MinAlign(St->getAlignment(), 4));
Owen Anderson825b72b2009-08-11 20:47:22 +000016669 return DAG.getNode(ISD::TokenFactor, StDL, MVT::Other, LoSt, HiSt);
Chris Lattner149a4e52008-02-22 02:09:43 +000016670 }
Dan Gohman475871a2008-07-27 21:46:04 +000016671 return SDValue();
Chris Lattner149a4e52008-02-22 02:09:43 +000016672}
16673
Duncan Sands17470be2011-09-22 20:15:48 +000016674/// isHorizontalBinOp - Return 'true' if this vector operation is "horizontal"
16675/// and return the operands for the horizontal operation in LHS and RHS. A
16676/// horizontal operation performs the binary operation on successive elements
16677/// of its first operand, then on successive elements of its second operand,
16678/// returning the resulting values in a vector. For example, if
16679/// A = < float a0, float a1, float a2, float a3 >
16680/// and
16681/// B = < float b0, float b1, float b2, float b3 >
16682/// then the result of doing a horizontal operation on A and B is
16683/// A horizontal-op B = < a0 op a1, a2 op a3, b0 op b1, b2 op b3 >.
16684/// In short, LHS and RHS are inspected to see if LHS op RHS is of the form
16685/// A horizontal-op B, for some already available A and B, and if so then LHS is
16686/// set to A, RHS to B, and the routine returns 'true'.
16687/// Note that the binary operation should have the property that if one of the
16688/// operands is UNDEF then the result is UNDEF.
Craig Topperbeabc6c2011-12-05 06:56:46 +000016689static bool isHorizontalBinOp(SDValue &LHS, SDValue &RHS, bool IsCommutative) {
Duncan Sands17470be2011-09-22 20:15:48 +000016690 // Look for the following pattern: if
16691 // A = < float a0, float a1, float a2, float a3 >
16692 // B = < float b0, float b1, float b2, float b3 >
16693 // and
16694 // LHS = VECTOR_SHUFFLE A, B, <0, 2, 4, 6>
16695 // RHS = VECTOR_SHUFFLE A, B, <1, 3, 5, 7>
16696 // then LHS op RHS = < a0 op a1, a2 op a3, b0 op b1, b2 op b3 >
16697 // which is A horizontal-op B.
16698
16699 // At least one of the operands should be a vector shuffle.
16700 if (LHS.getOpcode() != ISD::VECTOR_SHUFFLE &&
16701 RHS.getOpcode() != ISD::VECTOR_SHUFFLE)
16702 return false;
16703
16704 EVT VT = LHS.getValueType();
Craig Topperf8363302011-12-02 08:18:41 +000016705
16706 assert((VT.is128BitVector() || VT.is256BitVector()) &&
16707 "Unsupported vector type for horizontal add/sub");
16708
16709 // Handle 128 and 256-bit vector lengths. AVX defines horizontal add/sub to
16710 // operate independently on 128-bit lanes.
Craig Topperb72039c2011-11-30 09:10:50 +000016711 unsigned NumElts = VT.getVectorNumElements();
16712 unsigned NumLanes = VT.getSizeInBits()/128;
16713 unsigned NumLaneElts = NumElts / NumLanes;
Craig Topperf8363302011-12-02 08:18:41 +000016714 assert((NumLaneElts % 2 == 0) &&
16715 "Vector type should have an even number of elements in each lane");
16716 unsigned HalfLaneElts = NumLaneElts/2;
Duncan Sands17470be2011-09-22 20:15:48 +000016717
16718 // View LHS in the form
16719 // LHS = VECTOR_SHUFFLE A, B, LMask
16720 // If LHS is not a shuffle then pretend it is the shuffle
16721 // LHS = VECTOR_SHUFFLE LHS, undef, <0, 1, ..., N-1>
16722 // NOTE: in what follows a default initialized SDValue represents an UNDEF of
16723 // type VT.
16724 SDValue A, B;
Craig Topperb72039c2011-11-30 09:10:50 +000016725 SmallVector<int, 16> LMask(NumElts);
Duncan Sands17470be2011-09-22 20:15:48 +000016726 if (LHS.getOpcode() == ISD::VECTOR_SHUFFLE) {
16727 if (LHS.getOperand(0).getOpcode() != ISD::UNDEF)
16728 A = LHS.getOperand(0);
16729 if (LHS.getOperand(1).getOpcode() != ISD::UNDEF)
16730 B = LHS.getOperand(1);
Benjamin Kramered4c8c62012-01-15 13:16:05 +000016731 ArrayRef<int> Mask = cast<ShuffleVectorSDNode>(LHS.getNode())->getMask();
16732 std::copy(Mask.begin(), Mask.end(), LMask.begin());
Duncan Sands17470be2011-09-22 20:15:48 +000016733 } else {
16734 if (LHS.getOpcode() != ISD::UNDEF)
16735 A = LHS;
Craig Topperb72039c2011-11-30 09:10:50 +000016736 for (unsigned i = 0; i != NumElts; ++i)
Duncan Sands17470be2011-09-22 20:15:48 +000016737 LMask[i] = i;
16738 }
16739
16740 // Likewise, view RHS in the form
16741 // RHS = VECTOR_SHUFFLE C, D, RMask
16742 SDValue C, D;
Craig Topperb72039c2011-11-30 09:10:50 +000016743 SmallVector<int, 16> RMask(NumElts);
Duncan Sands17470be2011-09-22 20:15:48 +000016744 if (RHS.getOpcode() == ISD::VECTOR_SHUFFLE) {
16745 if (RHS.getOperand(0).getOpcode() != ISD::UNDEF)
16746 C = RHS.getOperand(0);
16747 if (RHS.getOperand(1).getOpcode() != ISD::UNDEF)
16748 D = RHS.getOperand(1);
Benjamin Kramered4c8c62012-01-15 13:16:05 +000016749 ArrayRef<int> Mask = cast<ShuffleVectorSDNode>(RHS.getNode())->getMask();
16750 std::copy(Mask.begin(), Mask.end(), RMask.begin());
Duncan Sands17470be2011-09-22 20:15:48 +000016751 } else {
16752 if (RHS.getOpcode() != ISD::UNDEF)
16753 C = RHS;
Craig Topperb72039c2011-11-30 09:10:50 +000016754 for (unsigned i = 0; i != NumElts; ++i)
Duncan Sands17470be2011-09-22 20:15:48 +000016755 RMask[i] = i;
16756 }
16757
16758 // Check that the shuffles are both shuffling the same vectors.
16759 if (!(A == C && B == D) && !(A == D && B == C))
16760 return false;
16761
16762 // If everything is UNDEF then bail out: it would be better to fold to UNDEF.
16763 if (!A.getNode() && !B.getNode())
16764 return false;
16765
16766 // If A and B occur in reverse order in RHS, then "swap" them (which means
16767 // rewriting the mask).
16768 if (A != C)
Craig Topperbeabc6c2011-12-05 06:56:46 +000016769 CommuteVectorShuffleMask(RMask, NumElts);
Duncan Sands17470be2011-09-22 20:15:48 +000016770
16771 // At this point LHS and RHS are equivalent to
16772 // LHS = VECTOR_SHUFFLE A, B, LMask
16773 // RHS = VECTOR_SHUFFLE A, B, RMask
16774 // Check that the masks correspond to performing a horizontal operation.
Craig Topperf8363302011-12-02 08:18:41 +000016775 for (unsigned i = 0; i != NumElts; ++i) {
Craig Topperbeabc6c2011-12-05 06:56:46 +000016776 int LIdx = LMask[i], RIdx = RMask[i];
Duncan Sands17470be2011-09-22 20:15:48 +000016777
Craig Topperf8363302011-12-02 08:18:41 +000016778 // Ignore any UNDEF components.
Craig Topperbeabc6c2011-12-05 06:56:46 +000016779 if (LIdx < 0 || RIdx < 0 ||
16780 (!A.getNode() && (LIdx < (int)NumElts || RIdx < (int)NumElts)) ||
16781 (!B.getNode() && (LIdx >= (int)NumElts || RIdx >= (int)NumElts)))
Craig Topperf8363302011-12-02 08:18:41 +000016782 continue;
Duncan Sands17470be2011-09-22 20:15:48 +000016783
Craig Topperf8363302011-12-02 08:18:41 +000016784 // Check that successive elements are being operated on. If not, this is
16785 // not a horizontal operation.
16786 unsigned Src = (i/HalfLaneElts) % 2; // each lane is split between srcs
16787 unsigned LaneStart = (i/NumLaneElts) * NumLaneElts;
Craig Topperbeabc6c2011-12-05 06:56:46 +000016788 int Index = 2*(i%HalfLaneElts) + NumElts*Src + LaneStart;
Craig Topperf8363302011-12-02 08:18:41 +000016789 if (!(LIdx == Index && RIdx == Index + 1) &&
Craig Topperbeabc6c2011-12-05 06:56:46 +000016790 !(IsCommutative && LIdx == Index + 1 && RIdx == Index))
Craig Topperf8363302011-12-02 08:18:41 +000016791 return false;
Duncan Sands17470be2011-09-22 20:15:48 +000016792 }
16793
16794 LHS = A.getNode() ? A : B; // If A is 'UNDEF', use B for it.
16795 RHS = B.getNode() ? B : A; // If B is 'UNDEF', use A for it.
16796 return true;
16797}
16798
16799/// PerformFADDCombine - Do target-specific dag combines on floating point adds.
16800static SDValue PerformFADDCombine(SDNode *N, SelectionDAG &DAG,
16801 const X86Subtarget *Subtarget) {
16802 EVT VT = N->getValueType(0);
16803 SDValue LHS = N->getOperand(0);
16804 SDValue RHS = N->getOperand(1);
16805
16806 // Try to synthesize horizontal adds from adds of shuffles.
Craig Topperd0a31172012-01-10 06:37:29 +000016807 if (((Subtarget->hasSSE3() && (VT == MVT::v4f32 || VT == MVT::v2f64)) ||
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000016808 (Subtarget->hasFp256() && (VT == MVT::v8f32 || VT == MVT::v4f64))) &&
Duncan Sands17470be2011-09-22 20:15:48 +000016809 isHorizontalBinOp(LHS, RHS, true))
16810 return DAG.getNode(X86ISD::FHADD, N->getDebugLoc(), VT, LHS, RHS);
16811 return SDValue();
16812}
16813
16814/// PerformFSUBCombine - Do target-specific dag combines on floating point subs.
16815static SDValue PerformFSUBCombine(SDNode *N, SelectionDAG &DAG,
16816 const X86Subtarget *Subtarget) {
16817 EVT VT = N->getValueType(0);
16818 SDValue LHS = N->getOperand(0);
16819 SDValue RHS = N->getOperand(1);
16820
16821 // Try to synthesize horizontal subs from subs of shuffles.
Craig Topperd0a31172012-01-10 06:37:29 +000016822 if (((Subtarget->hasSSE3() && (VT == MVT::v4f32 || VT == MVT::v2f64)) ||
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000016823 (Subtarget->hasFp256() && (VT == MVT::v8f32 || VT == MVT::v4f64))) &&
Duncan Sands17470be2011-09-22 20:15:48 +000016824 isHorizontalBinOp(LHS, RHS, false))
16825 return DAG.getNode(X86ISD::FHSUB, N->getDebugLoc(), VT, LHS, RHS);
16826 return SDValue();
16827}
16828
Chris Lattner6cf73262008-01-25 06:14:17 +000016829/// PerformFORCombine - Do target-specific dag combines on X86ISD::FOR and
16830/// X86ISD::FXOR nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000016831static SDValue PerformFORCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattner6cf73262008-01-25 06:14:17 +000016832 assert(N->getOpcode() == X86ISD::FOR || N->getOpcode() == X86ISD::FXOR);
16833 // F[X]OR(0.0, x) -> x
16834 // F[X]OR(x, 0.0) -> x
Chris Lattneraf723b92008-01-25 05:46:26 +000016835 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
16836 if (C->getValueAPF().isPosZero())
16837 return N->getOperand(1);
16838 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
16839 if (C->getValueAPF().isPosZero())
16840 return N->getOperand(0);
Dan Gohman475871a2008-07-27 21:46:04 +000016841 return SDValue();
Chris Lattneraf723b92008-01-25 05:46:26 +000016842}
16843
Nadav Rotemd60cb112012-08-19 13:06:16 +000016844/// PerformFMinFMaxCombine - Do target-specific dag combines on X86ISD::FMIN and
16845/// X86ISD::FMAX nodes.
16846static SDValue PerformFMinFMaxCombine(SDNode *N, SelectionDAG &DAG) {
16847 assert(N->getOpcode() == X86ISD::FMIN || N->getOpcode() == X86ISD::FMAX);
16848
16849 // Only perform optimizations if UnsafeMath is used.
16850 if (!DAG.getTarget().Options.UnsafeFPMath)
16851 return SDValue();
16852
16853 // If we run in unsafe-math mode, then convert the FMAX and FMIN nodes
Craig Topper8365e9b2012-09-01 06:33:50 +000016854 // into FMINC and FMAXC, which are Commutative operations.
Nadav Rotemd60cb112012-08-19 13:06:16 +000016855 unsigned NewOp = 0;
16856 switch (N->getOpcode()) {
16857 default: llvm_unreachable("unknown opcode");
16858 case X86ISD::FMIN: NewOp = X86ISD::FMINC; break;
16859 case X86ISD::FMAX: NewOp = X86ISD::FMAXC; break;
16860 }
16861
16862 return DAG.getNode(NewOp, N->getDebugLoc(), N->getValueType(0),
16863 N->getOperand(0), N->getOperand(1));
16864}
16865
Chris Lattneraf723b92008-01-25 05:46:26 +000016866/// PerformFANDCombine - Do target-specific dag combines on X86ISD::FAND nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000016867static SDValue PerformFANDCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattneraf723b92008-01-25 05:46:26 +000016868 // FAND(0.0, x) -> 0.0
16869 // FAND(x, 0.0) -> 0.0
16870 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
16871 if (C->getValueAPF().isPosZero())
16872 return N->getOperand(0);
16873 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
16874 if (C->getValueAPF().isPosZero())
16875 return N->getOperand(1);
Dan Gohman475871a2008-07-27 21:46:04 +000016876 return SDValue();
Chris Lattneraf723b92008-01-25 05:46:26 +000016877}
16878
Dan Gohmane5af2d32009-01-29 01:59:02 +000016879static SDValue PerformBTCombine(SDNode *N,
16880 SelectionDAG &DAG,
16881 TargetLowering::DAGCombinerInfo &DCI) {
16882 // BT ignores high bits in the bit index operand.
16883 SDValue Op1 = N->getOperand(1);
16884 if (Op1.hasOneUse()) {
16885 unsigned BitWidth = Op1.getValueSizeInBits();
16886 APInt DemandedMask = APInt::getLowBitsSet(BitWidth, Log2_32(BitWidth));
16887 APInt KnownZero, KnownOne;
Evan Chenge5b51ac2010-04-17 06:13:15 +000016888 TargetLowering::TargetLoweringOpt TLO(DAG, !DCI.isBeforeLegalize(),
16889 !DCI.isBeforeLegalizeOps());
Dan Gohmand858e902010-04-17 15:26:15 +000016890 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Dan Gohmane5af2d32009-01-29 01:59:02 +000016891 if (TLO.ShrinkDemandedConstant(Op1, DemandedMask) ||
16892 TLI.SimplifyDemandedBits(Op1, DemandedMask, KnownZero, KnownOne, TLO))
16893 DCI.CommitTargetLoweringOpt(TLO);
16894 }
16895 return SDValue();
16896}
Chris Lattner83e6c992006-10-04 06:57:07 +000016897
Eli Friedman7a5e5552009-06-07 06:52:44 +000016898static SDValue PerformVZEXT_MOVLCombine(SDNode *N, SelectionDAG &DAG) {
16899 SDValue Op = N->getOperand(0);
Wesley Peckbf17cfa2010-11-23 03:31:01 +000016900 if (Op.getOpcode() == ISD::BITCAST)
Eli Friedman7a5e5552009-06-07 06:52:44 +000016901 Op = Op.getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +000016902 EVT VT = N->getValueType(0), OpVT = Op.getValueType();
Eli Friedman7a5e5552009-06-07 06:52:44 +000016903 if (Op.getOpcode() == X86ISD::VZEXT_LOAD &&
Eric Christopherfd179292009-08-27 18:07:15 +000016904 VT.getVectorElementType().getSizeInBits() ==
Eli Friedman7a5e5552009-06-07 06:52:44 +000016905 OpVT.getVectorElementType().getSizeInBits()) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +000016906 return DAG.getNode(ISD::BITCAST, N->getDebugLoc(), VT, Op);
Eli Friedman7a5e5552009-06-07 06:52:44 +000016907 }
16908 return SDValue();
16909}
16910
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +000016911static SDValue PerformSExtCombine(SDNode *N, SelectionDAG &DAG,
16912 TargetLowering::DAGCombinerInfo &DCI,
16913 const X86Subtarget *Subtarget) {
16914 if (!DCI.isBeforeLegalizeOps())
16915 return SDValue();
16916
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000016917 if (!Subtarget->hasFp256())
Elena Demikhovskyf6020402012-02-08 08:37:26 +000016918 return SDValue();
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +000016919
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +000016920 EVT VT = N->getValueType(0);
Nadav Rotemd6fb53a2012-12-27 08:15:45 +000016921 if (VT.isVector() && VT.getSizeInBits() == 256) {
16922 SDValue R = WidenMaskArithmetic(N, DAG, DCI, Subtarget);
16923 if (R.getNode())
16924 return R;
16925 }
16926
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +000016927 return SDValue();
16928}
16929
Michael Liaof6c24ee2012-08-10 14:39:24 +000016930static SDValue PerformFMACombine(SDNode *N, SelectionDAG &DAG,
Elena Demikhovsky1503aba2012-08-01 12:06:00 +000016931 const X86Subtarget* Subtarget) {
16932 DebugLoc dl = N->getDebugLoc();
16933 EVT VT = N->getValueType(0);
16934
Craig Topperb1bdd7d2012-08-30 06:56:15 +000016935 // Let legalize expand this if it isn't a legal type yet.
16936 if (!DAG.getTargetLoweringInfo().isTypeLegal(VT))
16937 return SDValue();
16938
Elena Demikhovsky1503aba2012-08-01 12:06:00 +000016939 EVT ScalarVT = VT.getScalarType();
Craig Topperbf404372012-08-31 15:40:30 +000016940 if ((ScalarVT != MVT::f32 && ScalarVT != MVT::f64) ||
16941 (!Subtarget->hasFMA() && !Subtarget->hasFMA4()))
Elena Demikhovsky1503aba2012-08-01 12:06:00 +000016942 return SDValue();
16943
16944 SDValue A = N->getOperand(0);
16945 SDValue B = N->getOperand(1);
16946 SDValue C = N->getOperand(2);
16947
16948 bool NegA = (A.getOpcode() == ISD::FNEG);
16949 bool NegB = (B.getOpcode() == ISD::FNEG);
16950 bool NegC = (C.getOpcode() == ISD::FNEG);
16951
Michael Liaof6c24ee2012-08-10 14:39:24 +000016952 // Negative multiplication when NegA xor NegB
16953 bool NegMul = (NegA != NegB);
Elena Demikhovsky1503aba2012-08-01 12:06:00 +000016954 if (NegA)
16955 A = A.getOperand(0);
16956 if (NegB)
16957 B = B.getOperand(0);
16958 if (NegC)
16959 C = C.getOperand(0);
16960
16961 unsigned Opcode;
16962 if (!NegMul)
Craig Topperbf404372012-08-31 15:40:30 +000016963 Opcode = (!NegC) ? X86ISD::FMADD : X86ISD::FMSUB;
Elena Demikhovsky1503aba2012-08-01 12:06:00 +000016964 else
Craig Topperbf404372012-08-31 15:40:30 +000016965 Opcode = (!NegC) ? X86ISD::FNMADD : X86ISD::FNMSUB;
16966
Elena Demikhovsky1503aba2012-08-01 12:06:00 +000016967 return DAG.getNode(Opcode, dl, VT, A, B, C);
16968}
16969
Elena Demikhovsky28d7e712012-01-24 13:54:13 +000016970static SDValue PerformZExtCombine(SDNode *N, SelectionDAG &DAG,
Craig Topperc16f8512012-04-25 06:39:39 +000016971 TargetLowering::DAGCombinerInfo &DCI,
Elena Demikhovsky28d7e712012-01-24 13:54:13 +000016972 const X86Subtarget *Subtarget) {
Evan Cheng2e489c42009-12-16 00:53:11 +000016973 // (i32 zext (and (i8 x86isd::setcc_carry), 1)) ->
16974 // (and (i32 x86isd::setcc_carry), 1)
16975 // This eliminates the zext. This transformation is necessary because
16976 // ISD::SETCC is always legalized to i8.
16977 DebugLoc dl = N->getDebugLoc();
16978 SDValue N0 = N->getOperand(0);
16979 EVT VT = N->getValueType(0);
Elena Demikhovsky28d7e712012-01-24 13:54:13 +000016980
Evan Cheng2e489c42009-12-16 00:53:11 +000016981 if (N0.getOpcode() == ISD::AND &&
16982 N0.hasOneUse() &&
16983 N0.getOperand(0).hasOneUse()) {
16984 SDValue N00 = N0.getOperand(0);
Nadav Rotemd6fb53a2012-12-27 08:15:45 +000016985 if (N00.getOpcode() == X86ISD::SETCC_CARRY) {
16986 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
16987 if (!C || C->getZExtValue() != 1)
16988 return SDValue();
16989 return DAG.getNode(ISD::AND, dl, VT,
16990 DAG.getNode(X86ISD::SETCC_CARRY, dl, VT,
16991 N00.getOperand(0), N00.getOperand(1)),
16992 DAG.getConstant(1, VT));
16993 }
16994 }
16995
16996 if (VT.isVector() && VT.getSizeInBits() == 256) {
16997 SDValue R = WidenMaskArithmetic(N, DAG, DCI, Subtarget);
16998 if (R.getNode())
16999 return R;
Evan Cheng2e489c42009-12-16 00:53:11 +000017000 }
Craig Topperd0cf5652012-04-21 18:13:35 +000017001
Evan Cheng2e489c42009-12-16 00:53:11 +000017002 return SDValue();
17003}
17004
Chad Rosiera73b6fc2012-04-27 22:33:25 +000017005// Optimize x == -y --> x+y == 0
17006// x != -y --> x+y != 0
17007static SDValue PerformISDSETCCCombine(SDNode *N, SelectionDAG &DAG) {
17008 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(2))->get();
17009 SDValue LHS = N->getOperand(0);
Chad Rosiera20e1e72012-08-01 18:39:17 +000017010 SDValue RHS = N->getOperand(1);
Chad Rosiera73b6fc2012-04-27 22:33:25 +000017011
17012 if ((CC == ISD::SETNE || CC == ISD::SETEQ) && LHS.getOpcode() == ISD::SUB)
17013 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(LHS.getOperand(0)))
17014 if (C->getAPIntValue() == 0 && LHS.hasOneUse()) {
17015 SDValue addV = DAG.getNode(ISD::ADD, N->getDebugLoc(),
17016 LHS.getValueType(), RHS, LHS.getOperand(1));
17017 return DAG.getSetCC(N->getDebugLoc(), N->getValueType(0),
17018 addV, DAG.getConstant(0, addV.getValueType()), CC);
17019 }
17020 if ((CC == ISD::SETNE || CC == ISD::SETEQ) && RHS.getOpcode() == ISD::SUB)
17021 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(RHS.getOperand(0)))
17022 if (C->getAPIntValue() == 0 && RHS.hasOneUse()) {
17023 SDValue addV = DAG.getNode(ISD::ADD, N->getDebugLoc(),
17024 RHS.getValueType(), LHS, RHS.getOperand(1));
17025 return DAG.getSetCC(N->getDebugLoc(), N->getValueType(0),
17026 addV, DAG.getConstant(0, addV.getValueType()), CC);
17027 }
17028 return SDValue();
17029}
17030
Shuxin Yanga5526a92012-10-31 23:11:48 +000017031// Helper function of PerformSETCCCombine. It is to materialize "setb reg"
17032// as "sbb reg,reg", since it can be extended without zext and produces
17033// an all-ones bit which is more useful than 0/1 in some cases.
17034static SDValue MaterializeSETB(DebugLoc DL, SDValue EFLAGS, SelectionDAG &DAG) {
17035 return DAG.getNode(ISD::AND, DL, MVT::i8,
17036 DAG.getNode(X86ISD::SETCC_CARRY, DL, MVT::i8,
17037 DAG.getConstant(X86::COND_B, MVT::i8), EFLAGS),
17038 DAG.getConstant(1, MVT::i8));
17039}
17040
Chris Lattnerc19d1c32010-12-19 22:08:31 +000017041// Optimize RES = X86ISD::SETCC CONDCODE, EFLAG_INPUT
Michael Liaodbf8b5b2012-08-28 03:34:40 +000017042static SDValue PerformSETCCCombine(SDNode *N, SelectionDAG &DAG,
17043 TargetLowering::DAGCombinerInfo &DCI,
17044 const X86Subtarget *Subtarget) {
Chris Lattnerc19d1c32010-12-19 22:08:31 +000017045 DebugLoc DL = N->getDebugLoc();
Michael Liao2a33cec2012-08-10 19:58:13 +000017046 X86::CondCode CC = X86::CondCode(N->getConstantOperandVal(0));
17047 SDValue EFLAGS = N->getOperand(1);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000017048
Shuxin Yanga5526a92012-10-31 23:11:48 +000017049 if (CC == X86::COND_A) {
17050 // Try to convert COND_A into COND_B in an attempt to facilitate
17051 // materializing "setb reg".
17052 //
17053 // Do not flip "e > c", where "c" is a constant, because Cmp instruction
17054 // cannot take an immediate as its first operand.
17055 //
17056 if (EFLAGS.getOpcode() == X86ISD::SUB && EFLAGS.hasOneUse() &&
17057 EFLAGS.getValueType().isInteger() &&
17058 !isa<ConstantSDNode>(EFLAGS.getOperand(1))) {
17059 SDValue NewSub = DAG.getNode(X86ISD::SUB, EFLAGS.getDebugLoc(),
17060 EFLAGS.getNode()->getVTList(),
17061 EFLAGS.getOperand(1), EFLAGS.getOperand(0));
17062 SDValue NewEFLAGS = SDValue(NewSub.getNode(), EFLAGS.getResNo());
17063 return MaterializeSETB(DL, NewEFLAGS, DAG);
17064 }
17065 }
17066
Chris Lattnerc19d1c32010-12-19 22:08:31 +000017067 // Materialize "setb reg" as "sbb reg,reg", since it can be extended without
17068 // a zext and produces an all-ones bit which is more useful than 0/1 in some
17069 // cases.
Michael Liao2a33cec2012-08-10 19:58:13 +000017070 if (CC == X86::COND_B)
Shuxin Yanga5526a92012-10-31 23:11:48 +000017071 return MaterializeSETB(DL, EFLAGS, DAG);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000017072
Michael Liao2a33cec2012-08-10 19:58:13 +000017073 SDValue Flags;
17074
Michael Liaodbf8b5b2012-08-28 03:34:40 +000017075 Flags = checkBoolTestSetCCCombine(EFLAGS, CC);
17076 if (Flags.getNode()) {
17077 SDValue Cond = DAG.getConstant(CC, MVT::i8);
17078 return DAG.getNode(X86ISD::SETCC, DL, N->getVTList(), Cond, Flags);
17079 }
17080
Michael Liao2a33cec2012-08-10 19:58:13 +000017081 return SDValue();
17082}
17083
17084// Optimize branch condition evaluation.
17085//
17086static SDValue PerformBrCondCombine(SDNode *N, SelectionDAG &DAG,
17087 TargetLowering::DAGCombinerInfo &DCI,
17088 const X86Subtarget *Subtarget) {
17089 DebugLoc DL = N->getDebugLoc();
17090 SDValue Chain = N->getOperand(0);
17091 SDValue Dest = N->getOperand(1);
17092 SDValue EFLAGS = N->getOperand(3);
17093 X86::CondCode CC = X86::CondCode(N->getConstantOperandVal(2));
17094
17095 SDValue Flags;
17096
Michael Liaodbf8b5b2012-08-28 03:34:40 +000017097 Flags = checkBoolTestSetCCCombine(EFLAGS, CC);
17098 if (Flags.getNode()) {
17099 SDValue Cond = DAG.getConstant(CC, MVT::i8);
17100 return DAG.getNode(X86ISD::BRCOND, DL, N->getVTList(), Chain, Dest, Cond,
17101 Flags);
17102 }
17103
Chris Lattnerc19d1c32010-12-19 22:08:31 +000017104 return SDValue();
17105}
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000017106
Benjamin Kramer1396c402011-06-18 11:09:41 +000017107static SDValue PerformSINT_TO_FPCombine(SDNode *N, SelectionDAG &DAG,
17108 const X86TargetLowering *XTLI) {
Stuart Hastingsf99a4b82011-06-06 23:15:58 +000017109 SDValue Op0 = N->getOperand(0);
Nadav Rotema3540772012-04-23 21:53:37 +000017110 EVT InVT = Op0->getValueType(0);
Nadav Rotema3540772012-04-23 21:53:37 +000017111
17112 // SINT_TO_FP(v4i8) -> SINT_TO_FP(SEXT(v4i8 to v4i32))
Craig Topper7fd5e162012-04-24 06:02:29 +000017113 if (InVT == MVT::v8i8 || InVT == MVT::v4i8) {
Nadav Rotema3540772012-04-23 21:53:37 +000017114 DebugLoc dl = N->getDebugLoc();
Craig Topper7fd5e162012-04-24 06:02:29 +000017115 MVT DstVT = InVT == MVT::v4i8 ? MVT::v4i32 : MVT::v8i32;
Nadav Rotema3540772012-04-23 21:53:37 +000017116 SDValue P = DAG.getNode(ISD::SIGN_EXTEND, dl, DstVT, Op0);
17117 return DAG.getNode(ISD::SINT_TO_FP, dl, N->getValueType(0), P);
17118 }
17119
Stuart Hastingsf99a4b82011-06-06 23:15:58 +000017120 // Transform (SINT_TO_FP (i64 ...)) into an x87 operation if we have
17121 // a 32-bit target where SSE doesn't support i64->FP operations.
17122 if (Op0.getOpcode() == ISD::LOAD) {
17123 LoadSDNode *Ld = cast<LoadSDNode>(Op0.getNode());
17124 EVT VT = Ld->getValueType(0);
17125 if (!Ld->isVolatile() && !N->getValueType(0).isVector() &&
17126 ISD::isNON_EXTLoad(Op0.getNode()) && Op0.hasOneUse() &&
17127 !XTLI->getSubtarget()->is64Bit() &&
17128 !DAG.getTargetLoweringInfo().isTypeLegal(VT)) {
Benjamin Kramer1396c402011-06-18 11:09:41 +000017129 SDValue FILDChain = XTLI->BuildFILD(SDValue(N, 0), Ld->getValueType(0),
17130 Ld->getChain(), Op0, DAG);
Stuart Hastingsf99a4b82011-06-06 23:15:58 +000017131 DAG.ReplaceAllUsesOfValueWith(Op0.getValue(1), FILDChain.getValue(1));
17132 return FILDChain;
17133 }
17134 }
17135 return SDValue();
17136}
17137
Chris Lattner23a01992010-12-20 01:37:09 +000017138// Optimize RES, EFLAGS = X86ISD::ADC LHS, RHS, EFLAGS
17139static SDValue PerformADCCombine(SDNode *N, SelectionDAG &DAG,
17140 X86TargetLowering::DAGCombinerInfo &DCI) {
17141 // If the LHS and RHS of the ADC node are zero, then it can't overflow and
17142 // the result is either zero or one (depending on the input carry bit).
17143 // Strength reduce this down to a "set on carry" aka SETCC_CARRY&1.
17144 if (X86::isZeroNode(N->getOperand(0)) &&
17145 X86::isZeroNode(N->getOperand(1)) &&
17146 // We don't have a good way to replace an EFLAGS use, so only do this when
17147 // dead right now.
17148 SDValue(N, 1).use_empty()) {
17149 DebugLoc DL = N->getDebugLoc();
17150 EVT VT = N->getValueType(0);
17151 SDValue CarryOut = DAG.getConstant(0, N->getValueType(1));
17152 SDValue Res1 = DAG.getNode(ISD::AND, DL, VT,
17153 DAG.getNode(X86ISD::SETCC_CARRY, DL, VT,
17154 DAG.getConstant(X86::COND_B,MVT::i8),
17155 N->getOperand(2)),
17156 DAG.getConstant(1, VT));
17157 return DCI.CombineTo(N, Res1, CarryOut);
17158 }
17159
17160 return SDValue();
17161}
17162
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000017163// fold (add Y, (sete X, 0)) -> adc 0, Y
17164// (add Y, (setne X, 0)) -> sbb -1, Y
17165// (sub (sete X, 0), Y) -> sbb 0, Y
17166// (sub (setne X, 0), Y) -> adc -1, Y
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000017167static SDValue OptimizeConditionalInDecrement(SDNode *N, SelectionDAG &DAG) {
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000017168 DebugLoc DL = N->getDebugLoc();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000017169
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000017170 // Look through ZExts.
17171 SDValue Ext = N->getOperand(N->getOpcode() == ISD::SUB ? 1 : 0);
17172 if (Ext.getOpcode() != ISD::ZERO_EXTEND || !Ext.hasOneUse())
17173 return SDValue();
17174
17175 SDValue SetCC = Ext.getOperand(0);
17176 if (SetCC.getOpcode() != X86ISD::SETCC || !SetCC.hasOneUse())
17177 return SDValue();
17178
17179 X86::CondCode CC = (X86::CondCode)SetCC.getConstantOperandVal(0);
17180 if (CC != X86::COND_E && CC != X86::COND_NE)
17181 return SDValue();
17182
17183 SDValue Cmp = SetCC.getOperand(1);
17184 if (Cmp.getOpcode() != X86ISD::CMP || !Cmp.hasOneUse() ||
Chris Lattner9cd3da42011-01-16 02:56:53 +000017185 !X86::isZeroNode(Cmp.getOperand(1)) ||
17186 !Cmp.getOperand(0).getValueType().isInteger())
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000017187 return SDValue();
17188
17189 SDValue CmpOp0 = Cmp.getOperand(0);
17190 SDValue NewCmp = DAG.getNode(X86ISD::CMP, DL, MVT::i32, CmpOp0,
17191 DAG.getConstant(1, CmpOp0.getValueType()));
17192
17193 SDValue OtherVal = N->getOperand(N->getOpcode() == ISD::SUB ? 0 : 1);
17194 if (CC == X86::COND_NE)
17195 return DAG.getNode(N->getOpcode() == ISD::SUB ? X86ISD::ADC : X86ISD::SBB,
17196 DL, OtherVal.getValueType(), OtherVal,
17197 DAG.getConstant(-1ULL, OtherVal.getValueType()), NewCmp);
17198 return DAG.getNode(N->getOpcode() == ISD::SUB ? X86ISD::SBB : X86ISD::ADC,
17199 DL, OtherVal.getValueType(), OtherVal,
17200 DAG.getConstant(0, OtherVal.getValueType()), NewCmp);
17201}
Chris Lattnerc19d1c32010-12-19 22:08:31 +000017202
Craig Topper54f952a2011-11-19 09:02:40 +000017203/// PerformADDCombine - Do target-specific dag combines on integer adds.
17204static SDValue PerformAddCombine(SDNode *N, SelectionDAG &DAG,
17205 const X86Subtarget *Subtarget) {
17206 EVT VT = N->getValueType(0);
17207 SDValue Op0 = N->getOperand(0);
17208 SDValue Op1 = N->getOperand(1);
17209
17210 // Try to synthesize horizontal adds from adds of shuffles.
Craig Topperd0a31172012-01-10 06:37:29 +000017211 if (((Subtarget->hasSSSE3() && (VT == MVT::v8i16 || VT == MVT::v4i32)) ||
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000017212 (Subtarget->hasInt256() && (VT == MVT::v16i16 || VT == MVT::v8i32))) &&
Craig Topper54f952a2011-11-19 09:02:40 +000017213 isHorizontalBinOp(Op0, Op1, true))
17214 return DAG.getNode(X86ISD::HADD, N->getDebugLoc(), VT, Op0, Op1);
17215
17216 return OptimizeConditionalInDecrement(N, DAG);
17217}
17218
17219static SDValue PerformSubCombine(SDNode *N, SelectionDAG &DAG,
17220 const X86Subtarget *Subtarget) {
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000017221 SDValue Op0 = N->getOperand(0);
17222 SDValue Op1 = N->getOperand(1);
17223
17224 // X86 can't encode an immediate LHS of a sub. See if we can push the
17225 // negation into a preceding instruction.
17226 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op0)) {
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000017227 // If the RHS of the sub is a XOR with one use and a constant, invert the
17228 // immediate. Then add one to the LHS of the sub so we can turn
17229 // X-Y -> X+~Y+1, saving one register.
17230 if (Op1->hasOneUse() && Op1.getOpcode() == ISD::XOR &&
17231 isa<ConstantSDNode>(Op1.getOperand(1))) {
Nick Lewycky726ebd62011-08-23 19:01:24 +000017232 APInt XorC = cast<ConstantSDNode>(Op1.getOperand(1))->getAPIntValue();
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000017233 EVT VT = Op0.getValueType();
17234 SDValue NewXor = DAG.getNode(ISD::XOR, Op1.getDebugLoc(), VT,
17235 Op1.getOperand(0),
17236 DAG.getConstant(~XorC, VT));
17237 return DAG.getNode(ISD::ADD, N->getDebugLoc(), VT, NewXor,
Nick Lewycky726ebd62011-08-23 19:01:24 +000017238 DAG.getConstant(C->getAPIntValue()+1, VT));
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000017239 }
17240 }
17241
Craig Topper54f952a2011-11-19 09:02:40 +000017242 // Try to synthesize horizontal adds from adds of shuffles.
17243 EVT VT = N->getValueType(0);
Craig Topperd0a31172012-01-10 06:37:29 +000017244 if (((Subtarget->hasSSSE3() && (VT == MVT::v8i16 || VT == MVT::v4i32)) ||
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000017245 (Subtarget->hasInt256() && (VT == MVT::v16i16 || VT == MVT::v8i32))) &&
Craig Topperb72039c2011-11-30 09:10:50 +000017246 isHorizontalBinOp(Op0, Op1, true))
Craig Topper54f952a2011-11-19 09:02:40 +000017247 return DAG.getNode(X86ISD::HSUB, N->getDebugLoc(), VT, Op0, Op1);
17248
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000017249 return OptimizeConditionalInDecrement(N, DAG);
17250}
17251
Michael Liaod9d09602012-10-23 17:34:00 +000017252/// performVZEXTCombine - Performs build vector combines
17253static SDValue performVZEXTCombine(SDNode *N, SelectionDAG &DAG,
17254 TargetLowering::DAGCombinerInfo &DCI,
17255 const X86Subtarget *Subtarget) {
17256 // (vzext (bitcast (vzext (x)) -> (vzext x)
17257 SDValue In = N->getOperand(0);
17258 while (In.getOpcode() == ISD::BITCAST)
17259 In = In.getOperand(0);
17260
17261 if (In.getOpcode() != X86ISD::VZEXT)
17262 return SDValue();
17263
17264 return DAG.getNode(X86ISD::VZEXT, N->getDebugLoc(), N->getValueType(0), In.getOperand(0));
17265}
17266
Dan Gohman475871a2008-07-27 21:46:04 +000017267SDValue X86TargetLowering::PerformDAGCombine(SDNode *N,
Evan Cheng9dd93b32008-11-05 06:03:38 +000017268 DAGCombinerInfo &DCI) const {
Evan Cheng206ee9d2006-07-07 08:33:52 +000017269 SelectionDAG &DAG = DCI.DAG;
17270 switch (N->getOpcode()) {
17271 default: break;
Dan Gohman1bbf72b2010-03-15 23:23:03 +000017272 case ISD::EXTRACT_VECTOR_ELT:
Craig Topper89f4e662012-03-20 07:17:59 +000017273 return PerformEXTRACT_VECTOR_ELTCombine(N, DAG, DCI);
Duncan Sands6bcd2192011-09-17 16:49:39 +000017274 case ISD::VSELECT:
Nadav Rotemcc616562012-01-15 19:27:55 +000017275 case ISD::SELECT: return PerformSELECTCombine(N, DAG, DCI, Subtarget);
Michael Liaodbf8b5b2012-08-28 03:34:40 +000017276 case X86ISD::CMOV: return PerformCMOVCombine(N, DAG, DCI, Subtarget);
Craig Topper54f952a2011-11-19 09:02:40 +000017277 case ISD::ADD: return PerformAddCombine(N, DAG, Subtarget);
17278 case ISD::SUB: return PerformSubCombine(N, DAG, Subtarget);
Chris Lattner23a01992010-12-20 01:37:09 +000017279 case X86ISD::ADC: return PerformADCCombine(N, DAG, DCI);
Evan Cheng0b0cd912009-03-28 05:57:29 +000017280 case ISD::MUL: return PerformMulCombine(N, DAG, DCI);
Nate Begeman740ab032009-01-26 00:52:55 +000017281 case ISD::SHL:
17282 case ISD::SRA:
Mon P Wang845b1892012-02-01 22:15:20 +000017283 case ISD::SRL: return PerformShiftCombine(N, DAG, DCI, Subtarget);
Nate Begemanb65c1752010-12-17 22:55:37 +000017284 case ISD::AND: return PerformAndCombine(N, DAG, DCI, Subtarget);
Evan Cheng8b1190a2010-04-28 01:18:01 +000017285 case ISD::OR: return PerformOrCombine(N, DAG, DCI, Subtarget);
Craig Topperb4c94572011-10-21 06:55:01 +000017286 case ISD::XOR: return PerformXorCombine(N, DAG, DCI, Subtarget);
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000017287 case ISD::LOAD: return PerformLOADCombine(N, DAG, DCI, Subtarget);
Evan Cheng7e2ff772008-05-08 00:57:18 +000017288 case ISD::STORE: return PerformSTORECombine(N, DAG, Subtarget);
Stuart Hastingsf99a4b82011-06-06 23:15:58 +000017289 case ISD::SINT_TO_FP: return PerformSINT_TO_FPCombine(N, DAG, this);
Duncan Sands17470be2011-09-22 20:15:48 +000017290 case ISD::FADD: return PerformFADDCombine(N, DAG, Subtarget);
17291 case ISD::FSUB: return PerformFSUBCombine(N, DAG, Subtarget);
Chris Lattner6cf73262008-01-25 06:14:17 +000017292 case X86ISD::FXOR:
Chris Lattneraf723b92008-01-25 05:46:26 +000017293 case X86ISD::FOR: return PerformFORCombine(N, DAG);
Nadav Rotemd60cb112012-08-19 13:06:16 +000017294 case X86ISD::FMIN:
17295 case X86ISD::FMAX: return PerformFMinFMaxCombine(N, DAG);
Chris Lattneraf723b92008-01-25 05:46:26 +000017296 case X86ISD::FAND: return PerformFANDCombine(N, DAG);
Dan Gohmane5af2d32009-01-29 01:59:02 +000017297 case X86ISD::BT: return PerformBTCombine(N, DAG, DCI);
Eli Friedman7a5e5552009-06-07 06:52:44 +000017298 case X86ISD::VZEXT_MOVL: return PerformVZEXT_MOVLCombine(N, DAG);
Elena Demikhovsky1da58672012-04-22 09:39:03 +000017299 case ISD::ANY_EXTEND:
Craig Topperc16f8512012-04-25 06:39:39 +000017300 case ISD::ZERO_EXTEND: return PerformZExtCombine(N, DAG, DCI, Subtarget);
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +000017301 case ISD::SIGN_EXTEND: return PerformSExtCombine(N, DAG, DCI, Subtarget);
Craig Topper55b24052012-09-11 06:15:32 +000017302 case ISD::TRUNCATE: return PerformTruncateCombine(N, DAG,DCI,Subtarget);
Chad Rosiera73b6fc2012-04-27 22:33:25 +000017303 case ISD::SETCC: return PerformISDSETCCCombine(N, DAG);
Michael Liaodbf8b5b2012-08-28 03:34:40 +000017304 case X86ISD::SETCC: return PerformSETCCCombine(N, DAG, DCI, Subtarget);
Michael Liao2a33cec2012-08-10 19:58:13 +000017305 case X86ISD::BRCOND: return PerformBrCondCombine(N, DAG, DCI, Subtarget);
Michael Liaod9d09602012-10-23 17:34:00 +000017306 case X86ISD::VZEXT: return performVZEXTCombine(N, DAG, DCI, Subtarget);
Craig Topperb3982da2011-12-31 23:50:21 +000017307 case X86ISD::SHUFP: // Handle all target specific shuffles
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +000017308 case X86ISD::PALIGN:
Craig Topper34671b82011-12-06 08:21:25 +000017309 case X86ISD::UNPCKH:
17310 case X86ISD::UNPCKL:
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000017311 case X86ISD::MOVHLPS:
17312 case X86ISD::MOVLHPS:
17313 case X86ISD::PSHUFD:
17314 case X86ISD::PSHUFHW:
17315 case X86ISD::PSHUFLW:
17316 case X86ISD::MOVSS:
17317 case X86ISD::MOVSD:
Craig Topper316cd2a2011-11-30 06:25:25 +000017318 case X86ISD::VPERMILP:
Craig Topperec24e612011-11-30 07:47:51 +000017319 case X86ISD::VPERM2X128:
Bruno Cardoso Lopes50b37c72011-08-15 21:45:54 +000017320 case ISD::VECTOR_SHUFFLE: return PerformShuffleCombine(N, DAG, DCI,Subtarget);
Elena Demikhovsky1503aba2012-08-01 12:06:00 +000017321 case ISD::FMA: return PerformFMACombine(N, DAG, Subtarget);
Evan Cheng206ee9d2006-07-07 08:33:52 +000017322 }
17323
Dan Gohman475871a2008-07-27 21:46:04 +000017324 return SDValue();
Evan Cheng206ee9d2006-07-07 08:33:52 +000017325}
17326
Evan Chenge5b51ac2010-04-17 06:13:15 +000017327/// isTypeDesirableForOp - Return true if the target has native support for
17328/// the specified value type and it is 'desirable' to use the type for the
17329/// given node type. e.g. On x86 i16 is legal, but undesirable since i16
17330/// instruction encodings are longer and some i16 instructions are slow.
17331bool X86TargetLowering::isTypeDesirableForOp(unsigned Opc, EVT VT) const {
17332 if (!isTypeLegal(VT))
17333 return false;
Evan Cheng2bce5f4b2010-04-28 08:30:49 +000017334 if (VT != MVT::i16)
Evan Chenge5b51ac2010-04-17 06:13:15 +000017335 return true;
17336
17337 switch (Opc) {
17338 default:
17339 return true;
Evan Cheng4c26e932010-04-19 19:29:22 +000017340 case ISD::LOAD:
17341 case ISD::SIGN_EXTEND:
17342 case ISD::ZERO_EXTEND:
17343 case ISD::ANY_EXTEND:
Evan Chenge5b51ac2010-04-17 06:13:15 +000017344 case ISD::SHL:
Evan Chenge5b51ac2010-04-17 06:13:15 +000017345 case ISD::SRL:
17346 case ISD::SUB:
17347 case ISD::ADD:
17348 case ISD::MUL:
17349 case ISD::AND:
17350 case ISD::OR:
17351 case ISD::XOR:
17352 return false;
17353 }
17354}
17355
17356/// IsDesirableToPromoteOp - This method query the target whether it is
Evan Cheng64b7bf72010-04-16 06:14:10 +000017357/// beneficial for dag combiner to promote the specified node. If true, it
17358/// should return the desired promotion type by reference.
Evan Chenge5b51ac2010-04-17 06:13:15 +000017359bool X86TargetLowering::IsDesirableToPromoteOp(SDValue Op, EVT &PVT) const {
Evan Cheng64b7bf72010-04-16 06:14:10 +000017360 EVT VT = Op.getValueType();
17361 if (VT != MVT::i16)
17362 return false;
17363
Evan Cheng4c26e932010-04-19 19:29:22 +000017364 bool Promote = false;
17365 bool Commute = false;
Evan Cheng64b7bf72010-04-16 06:14:10 +000017366 switch (Op.getOpcode()) {
Evan Cheng4c26e932010-04-19 19:29:22 +000017367 default: break;
17368 case ISD::LOAD: {
17369 LoadSDNode *LD = cast<LoadSDNode>(Op);
17370 // If the non-extending load has a single use and it's not live out, then it
17371 // might be folded.
Evan Cheng2bce5f4b2010-04-28 08:30:49 +000017372 if (LD->getExtensionType() == ISD::NON_EXTLOAD /*&&
17373 Op.hasOneUse()*/) {
17374 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
17375 UE = Op.getNode()->use_end(); UI != UE; ++UI) {
17376 // The only case where we'd want to promote LOAD (rather then it being
17377 // promoted as an operand is when it's only use is liveout.
17378 if (UI->getOpcode() != ISD::CopyToReg)
17379 return false;
17380 }
17381 }
Evan Cheng4c26e932010-04-19 19:29:22 +000017382 Promote = true;
17383 break;
17384 }
17385 case ISD::SIGN_EXTEND:
17386 case ISD::ZERO_EXTEND:
17387 case ISD::ANY_EXTEND:
17388 Promote = true;
17389 break;
Evan Chenge5b51ac2010-04-17 06:13:15 +000017390 case ISD::SHL:
Evan Cheng2bce5f4b2010-04-28 08:30:49 +000017391 case ISD::SRL: {
Evan Chenge5b51ac2010-04-17 06:13:15 +000017392 SDValue N0 = Op.getOperand(0);
17393 // Look out for (store (shl (load), x)).
Evan Chengc82c20b2010-04-24 04:44:57 +000017394 if (MayFoldLoad(N0) && MayFoldIntoStore(Op))
Evan Chenge5b51ac2010-04-17 06:13:15 +000017395 return false;
Evan Cheng4c26e932010-04-19 19:29:22 +000017396 Promote = true;
Evan Chenge5b51ac2010-04-17 06:13:15 +000017397 break;
17398 }
Evan Cheng64b7bf72010-04-16 06:14:10 +000017399 case ISD::ADD:
17400 case ISD::MUL:
17401 case ISD::AND:
17402 case ISD::OR:
Evan Cheng4c26e932010-04-19 19:29:22 +000017403 case ISD::XOR:
17404 Commute = true;
17405 // fallthrough
17406 case ISD::SUB: {
Evan Cheng64b7bf72010-04-16 06:14:10 +000017407 SDValue N0 = Op.getOperand(0);
17408 SDValue N1 = Op.getOperand(1);
Evan Chengc82c20b2010-04-24 04:44:57 +000017409 if (!Commute && MayFoldLoad(N1))
Evan Cheng64b7bf72010-04-16 06:14:10 +000017410 return false;
17411 // Avoid disabling potential load folding opportunities.
Evan Chengc82c20b2010-04-24 04:44:57 +000017412 if (MayFoldLoad(N0) && (!isa<ConstantSDNode>(N1) || MayFoldIntoStore(Op)))
Evan Cheng64b7bf72010-04-16 06:14:10 +000017413 return false;
Evan Chengc82c20b2010-04-24 04:44:57 +000017414 if (MayFoldLoad(N1) && (!isa<ConstantSDNode>(N0) || MayFoldIntoStore(Op)))
Evan Cheng64b7bf72010-04-16 06:14:10 +000017415 return false;
Evan Cheng4c26e932010-04-19 19:29:22 +000017416 Promote = true;
Evan Cheng64b7bf72010-04-16 06:14:10 +000017417 }
17418 }
17419
17420 PVT = MVT::i32;
Evan Cheng4c26e932010-04-19 19:29:22 +000017421 return Promote;
Evan Cheng64b7bf72010-04-16 06:14:10 +000017422}
17423
Evan Cheng60c07e12006-07-05 22:17:51 +000017424//===----------------------------------------------------------------------===//
17425// X86 Inline Assembly Support
17426//===----------------------------------------------------------------------===//
17427
Benjamin Kramer2ea4cdb2011-12-18 19:59:20 +000017428namespace {
17429 // Helper to match a string separated by whitespace.
Benjamin Kramer0581ed72011-12-18 20:51:31 +000017430 bool matchAsmImpl(StringRef s, ArrayRef<const StringRef *> args) {
Benjamin Kramer2ea4cdb2011-12-18 19:59:20 +000017431 s = s.substr(s.find_first_not_of(" \t")); // Skip leading whitespace.
Benjamin Kramere6cddb72011-12-17 14:36:05 +000017432
Benjamin Kramer0581ed72011-12-18 20:51:31 +000017433 for (unsigned i = 0, e = args.size(); i != e; ++i) {
Benjamin Kramer2ea4cdb2011-12-18 19:59:20 +000017434 StringRef piece(*args[i]);
17435 if (!s.startswith(piece)) // Check if the piece matches.
17436 return false;
17437
17438 s = s.substr(piece.size());
17439 StringRef::size_type pos = s.find_first_not_of(" \t");
17440 if (pos == 0) // We matched a prefix.
17441 return false;
17442
17443 s = s.substr(pos);
Benjamin Kramere6cddb72011-12-17 14:36:05 +000017444 }
17445
Benjamin Kramer2ea4cdb2011-12-18 19:59:20 +000017446 return s.empty();
Benjamin Kramere6cddb72011-12-17 14:36:05 +000017447 }
Benjamin Kramer0581ed72011-12-18 20:51:31 +000017448 const VariadicFunction1<bool, StringRef, StringRef, matchAsmImpl> matchAsm={};
Benjamin Kramere6cddb72011-12-17 14:36:05 +000017449}
17450
Chris Lattnerb8105652009-07-20 17:51:36 +000017451bool X86TargetLowering::ExpandInlineAsm(CallInst *CI) const {
17452 InlineAsm *IA = cast<InlineAsm>(CI->getCalledValue());
Chris Lattnerb8105652009-07-20 17:51:36 +000017453
17454 std::string AsmStr = IA->getAsmString();
17455
Benjamin Kramere6cddb72011-12-17 14:36:05 +000017456 IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
17457 if (!Ty || Ty->getBitWidth() % 16 != 0)
17458 return false;
17459
Chris Lattnerb8105652009-07-20 17:51:36 +000017460 // TODO: should remove alternatives from the asmstring: "foo {a|b}" -> "foo a"
Benjamin Kramerd4f19592010-01-11 18:03:24 +000017461 SmallVector<StringRef, 4> AsmPieces;
Peter Collingbourne98361182010-11-13 19:54:23 +000017462 SplitString(AsmStr, AsmPieces, ";\n");
Chris Lattnerb8105652009-07-20 17:51:36 +000017463
17464 switch (AsmPieces.size()) {
17465 default: return false;
17466 case 1:
Chris Lattner7a2bdde2011-04-15 05:18:47 +000017467 // FIXME: this should verify that we are targeting a 486 or better. If not,
Benjamin Kramere6cddb72011-12-17 14:36:05 +000017468 // we will turn this bswap into something that will be lowered to logical
17469 // ops instead of emitting the bswap asm. For now, we don't support 486 or
17470 // lower so don't worry about this.
Chris Lattnerb8105652009-07-20 17:51:36 +000017471 // bswap $0
Benjamin Kramer2ea4cdb2011-12-18 19:59:20 +000017472 if (matchAsm(AsmPieces[0], "bswap", "$0") ||
17473 matchAsm(AsmPieces[0], "bswapl", "$0") ||
17474 matchAsm(AsmPieces[0], "bswapq", "$0") ||
17475 matchAsm(AsmPieces[0], "bswap", "${0:q}") ||
17476 matchAsm(AsmPieces[0], "bswapl", "${0:q}") ||
17477 matchAsm(AsmPieces[0], "bswapq", "${0:q}")) {
Chris Lattnerb8105652009-07-20 17:51:36 +000017478 // No need to check constraints, nothing other than the equivalent of
17479 // "=r,0" would be valid here.
Evan Cheng55d42002011-01-08 01:24:27 +000017480 return IntrinsicLowering::LowerToByteSwap(CI);
Chris Lattnerb8105652009-07-20 17:51:36 +000017481 }
Benjamin Kramere6cddb72011-12-17 14:36:05 +000017482
Chris Lattnerb8105652009-07-20 17:51:36 +000017483 // rorw $$8, ${0:w} --> llvm.bswap.i16
Duncan Sandsb0bc6c32010-02-15 16:12:20 +000017484 if (CI->getType()->isIntegerTy(16) &&
Benjamin Kramere6cddb72011-12-17 14:36:05 +000017485 IA->getConstraintString().compare(0, 5, "=r,0,") == 0 &&
Benjamin Kramer2ea4cdb2011-12-18 19:59:20 +000017486 (matchAsm(AsmPieces[0], "rorw", "$$8,", "${0:w}") ||
17487 matchAsm(AsmPieces[0], "rolw", "$$8,", "${0:w}"))) {
Dan Gohman0ef701e2010-03-04 19:58:08 +000017488 AsmPieces.clear();
Evan Cheng55d42002011-01-08 01:24:27 +000017489 const std::string &ConstraintsStr = IA->getConstraintString();
17490 SplitString(StringRef(ConstraintsStr).substr(5), AsmPieces, ",");
Dan Gohman0ef701e2010-03-04 19:58:08 +000017491 std::sort(AsmPieces.begin(), AsmPieces.end());
17492 if (AsmPieces.size() == 4 &&
17493 AsmPieces[0] == "~{cc}" &&
17494 AsmPieces[1] == "~{dirflag}" &&
17495 AsmPieces[2] == "~{flags}" &&
Benjamin Kramere6cddb72011-12-17 14:36:05 +000017496 AsmPieces[3] == "~{fpsr}")
17497 return IntrinsicLowering::LowerToByteSwap(CI);
Chris Lattnerb8105652009-07-20 17:51:36 +000017498 }
17499 break;
17500 case 3:
Peter Collingbourne948cf022010-11-13 19:54:30 +000017501 if (CI->getType()->isIntegerTy(32) &&
Benjamin Kramere6cddb72011-12-17 14:36:05 +000017502 IA->getConstraintString().compare(0, 5, "=r,0,") == 0 &&
Benjamin Kramer2ea4cdb2011-12-18 19:59:20 +000017503 matchAsm(AsmPieces[0], "rorw", "$$8,", "${0:w}") &&
17504 matchAsm(AsmPieces[1], "rorl", "$$16,", "$0") &&
17505 matchAsm(AsmPieces[2], "rorw", "$$8,", "${0:w}")) {
Benjamin Kramere6cddb72011-12-17 14:36:05 +000017506 AsmPieces.clear();
17507 const std::string &ConstraintsStr = IA->getConstraintString();
17508 SplitString(StringRef(ConstraintsStr).substr(5), AsmPieces, ",");
17509 std::sort(AsmPieces.begin(), AsmPieces.end());
17510 if (AsmPieces.size() == 4 &&
17511 AsmPieces[0] == "~{cc}" &&
17512 AsmPieces[1] == "~{dirflag}" &&
17513 AsmPieces[2] == "~{flags}" &&
17514 AsmPieces[3] == "~{fpsr}")
17515 return IntrinsicLowering::LowerToByteSwap(CI);
Peter Collingbourne948cf022010-11-13 19:54:30 +000017516 }
Evan Cheng55d42002011-01-08 01:24:27 +000017517
17518 if (CI->getType()->isIntegerTy(64)) {
17519 InlineAsm::ConstraintInfoVector Constraints = IA->ParseConstraints();
17520 if (Constraints.size() >= 2 &&
17521 Constraints[0].Codes.size() == 1 && Constraints[0].Codes[0] == "A" &&
17522 Constraints[1].Codes.size() == 1 && Constraints[1].Codes[0] == "0") {
17523 // bswap %eax / bswap %edx / xchgl %eax, %edx -> llvm.bswap.i64
Benjamin Kramer2ea4cdb2011-12-18 19:59:20 +000017524 if (matchAsm(AsmPieces[0], "bswap", "%eax") &&
17525 matchAsm(AsmPieces[1], "bswap", "%edx") &&
17526 matchAsm(AsmPieces[2], "xchgl", "%eax,", "%edx"))
Benjamin Kramere6cddb72011-12-17 14:36:05 +000017527 return IntrinsicLowering::LowerToByteSwap(CI);
Chris Lattnerb8105652009-07-20 17:51:36 +000017528 }
17529 }
17530 break;
17531 }
17532 return false;
17533}
17534
Chris Lattnerf4dff842006-07-11 02:54:03 +000017535/// getConstraintType - Given a constraint letter, return the type of
17536/// constraint it is for this target.
17537X86TargetLowering::ConstraintType
Chris Lattner4234f572007-03-25 02:14:49 +000017538X86TargetLowering::getConstraintType(const std::string &Constraint) const {
17539 if (Constraint.size() == 1) {
17540 switch (Constraint[0]) {
Chris Lattner4234f572007-03-25 02:14:49 +000017541 case 'R':
Chris Lattner4234f572007-03-25 02:14:49 +000017542 case 'q':
17543 case 'Q':
John Thompson44ab89e2010-10-29 17:29:13 +000017544 case 'f':
17545 case 't':
17546 case 'u':
Dale Johannesen2ffbcac2008-04-01 00:57:48 +000017547 case 'y':
John Thompson44ab89e2010-10-29 17:29:13 +000017548 case 'x':
Chris Lattner4234f572007-03-25 02:14:49 +000017549 case 'Y':
Eric Christopher31b5f002011-07-07 22:29:07 +000017550 case 'l':
Chris Lattner4234f572007-03-25 02:14:49 +000017551 return C_RegisterClass;
John Thompson44ab89e2010-10-29 17:29:13 +000017552 case 'a':
17553 case 'b':
17554 case 'c':
17555 case 'd':
17556 case 'S':
17557 case 'D':
17558 case 'A':
17559 return C_Register;
17560 case 'I':
17561 case 'J':
17562 case 'K':
17563 case 'L':
17564 case 'M':
17565 case 'N':
17566 case 'G':
17567 case 'C':
Dale Johannesen78e3e522009-02-12 20:58:09 +000017568 case 'e':
17569 case 'Z':
17570 return C_Other;
Chris Lattner4234f572007-03-25 02:14:49 +000017571 default:
17572 break;
17573 }
Chris Lattnerf4dff842006-07-11 02:54:03 +000017574 }
Chris Lattner4234f572007-03-25 02:14:49 +000017575 return TargetLowering::getConstraintType(Constraint);
Chris Lattnerf4dff842006-07-11 02:54:03 +000017576}
17577
John Thompson44ab89e2010-10-29 17:29:13 +000017578/// Examine constraint type and operand type and determine a weight value.
John Thompsoneac6e1d2010-09-13 18:15:37 +000017579/// This object must already have been set up with the operand type
17580/// and the current alternative constraint selected.
John Thompson44ab89e2010-10-29 17:29:13 +000017581TargetLowering::ConstraintWeight
17582 X86TargetLowering::getSingleConstraintMatchWeight(
John Thompsoneac6e1d2010-09-13 18:15:37 +000017583 AsmOperandInfo &info, const char *constraint) const {
John Thompson44ab89e2010-10-29 17:29:13 +000017584 ConstraintWeight weight = CW_Invalid;
John Thompsoneac6e1d2010-09-13 18:15:37 +000017585 Value *CallOperandVal = info.CallOperandVal;
17586 // If we don't have a value, we can't do a match,
17587 // but allow it at the lowest weight.
17588 if (CallOperandVal == NULL)
John Thompson44ab89e2010-10-29 17:29:13 +000017589 return CW_Default;
Chris Lattnerdb125cf2011-07-18 04:54:35 +000017590 Type *type = CallOperandVal->getType();
John Thompsoneac6e1d2010-09-13 18:15:37 +000017591 // Look at the constraint type.
17592 switch (*constraint) {
17593 default:
John Thompson44ab89e2010-10-29 17:29:13 +000017594 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
17595 case 'R':
17596 case 'q':
17597 case 'Q':
17598 case 'a':
17599 case 'b':
17600 case 'c':
17601 case 'd':
17602 case 'S':
17603 case 'D':
17604 case 'A':
17605 if (CallOperandVal->getType()->isIntegerTy())
17606 weight = CW_SpecificReg;
17607 break;
17608 case 'f':
17609 case 't':
17610 case 'u':
Jakub Staszakc20323a2012-12-29 15:57:26 +000017611 if (type->isFloatingPointTy())
17612 weight = CW_SpecificReg;
17613 break;
John Thompson44ab89e2010-10-29 17:29:13 +000017614 case 'y':
Jakub Staszakc20323a2012-12-29 15:57:26 +000017615 if (type->isX86_MMXTy() && Subtarget->hasMMX())
17616 weight = CW_SpecificReg;
17617 break;
John Thompson44ab89e2010-10-29 17:29:13 +000017618 case 'x':
17619 case 'Y':
Craig Topper1accb7e2012-01-10 06:54:16 +000017620 if (((type->getPrimitiveSizeInBits() == 128) && Subtarget->hasSSE1()) ||
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000017621 ((type->getPrimitiveSizeInBits() == 256) && Subtarget->hasFp256()))
John Thompson44ab89e2010-10-29 17:29:13 +000017622 weight = CW_Register;
John Thompsoneac6e1d2010-09-13 18:15:37 +000017623 break;
17624 case 'I':
17625 if (ConstantInt *C = dyn_cast<ConstantInt>(info.CallOperandVal)) {
17626 if (C->getZExtValue() <= 31)
John Thompson44ab89e2010-10-29 17:29:13 +000017627 weight = CW_Constant;
John Thompsoneac6e1d2010-09-13 18:15:37 +000017628 }
17629 break;
John Thompson44ab89e2010-10-29 17:29:13 +000017630 case 'J':
17631 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
17632 if (C->getZExtValue() <= 63)
17633 weight = CW_Constant;
17634 }
17635 break;
17636 case 'K':
17637 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
17638 if ((C->getSExtValue() >= -0x80) && (C->getSExtValue() <= 0x7f))
17639 weight = CW_Constant;
17640 }
17641 break;
17642 case 'L':
17643 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
17644 if ((C->getZExtValue() == 0xff) || (C->getZExtValue() == 0xffff))
17645 weight = CW_Constant;
17646 }
17647 break;
17648 case 'M':
17649 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
17650 if (C->getZExtValue() <= 3)
17651 weight = CW_Constant;
17652 }
17653 break;
17654 case 'N':
17655 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
17656 if (C->getZExtValue() <= 0xff)
17657 weight = CW_Constant;
17658 }
17659 break;
17660 case 'G':
17661 case 'C':
17662 if (dyn_cast<ConstantFP>(CallOperandVal)) {
17663 weight = CW_Constant;
17664 }
17665 break;
17666 case 'e':
17667 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
17668 if ((C->getSExtValue() >= -0x80000000LL) &&
17669 (C->getSExtValue() <= 0x7fffffffLL))
17670 weight = CW_Constant;
17671 }
17672 break;
17673 case 'Z':
17674 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
17675 if (C->getZExtValue() <= 0xffffffff)
17676 weight = CW_Constant;
17677 }
17678 break;
John Thompsoneac6e1d2010-09-13 18:15:37 +000017679 }
17680 return weight;
17681}
17682
Dale Johannesenba2a0b92008-01-29 02:21:21 +000017683/// LowerXConstraint - try to replace an X constraint, which matches anything,
17684/// with another that has more specific requirements based on the type of the
17685/// corresponding operand.
Chris Lattner5e764232008-04-26 23:02:14 +000017686const char *X86TargetLowering::
Owen Andersone50ed302009-08-10 22:56:29 +000017687LowerXConstraint(EVT ConstraintVT) const {
Chris Lattner5e764232008-04-26 23:02:14 +000017688 // FP X constraints get lowered to SSE1/2 registers if available, otherwise
17689 // 'f' like normal targets.
Duncan Sands83ec4b62008-06-06 12:08:01 +000017690 if (ConstraintVT.isFloatingPoint()) {
Craig Topper1accb7e2012-01-10 06:54:16 +000017691 if (Subtarget->hasSSE2())
Chris Lattner5e764232008-04-26 23:02:14 +000017692 return "Y";
Craig Topper1accb7e2012-01-10 06:54:16 +000017693 if (Subtarget->hasSSE1())
Chris Lattner5e764232008-04-26 23:02:14 +000017694 return "x";
17695 }
Scott Michelfdc40a02009-02-17 22:15:04 +000017696
Chris Lattner5e764232008-04-26 23:02:14 +000017697 return TargetLowering::LowerXConstraint(ConstraintVT);
Dale Johannesenba2a0b92008-01-29 02:21:21 +000017698}
17699
Chris Lattner48884cd2007-08-25 00:47:38 +000017700/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
17701/// vector. If it is invalid, don't add anything to Ops.
Dan Gohman475871a2008-07-27 21:46:04 +000017702void X86TargetLowering::LowerAsmOperandForConstraint(SDValue Op,
Eric Christopher100c8332011-06-02 23:16:42 +000017703 std::string &Constraint,
Dan Gohman475871a2008-07-27 21:46:04 +000017704 std::vector<SDValue>&Ops,
Chris Lattner5e764232008-04-26 23:02:14 +000017705 SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +000017706 SDValue Result(0, 0);
Scott Michelfdc40a02009-02-17 22:15:04 +000017707
Eric Christopher100c8332011-06-02 23:16:42 +000017708 // Only support length 1 constraints for now.
17709 if (Constraint.length() > 1) return;
Eric Christopher471e4222011-06-08 23:55:35 +000017710
Eric Christopher100c8332011-06-02 23:16:42 +000017711 char ConstraintLetter = Constraint[0];
17712 switch (ConstraintLetter) {
Chris Lattner22aaf1d2006-10-31 20:13:11 +000017713 default: break;
Devang Patel84f7fd22007-03-17 00:13:28 +000017714 case 'I':
Chris Lattner188b9fe2007-03-25 01:57:35 +000017715 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +000017716 if (C->getZExtValue() <= 31) {
17717 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattner48884cd2007-08-25 00:47:38 +000017718 break;
17719 }
Devang Patel84f7fd22007-03-17 00:13:28 +000017720 }
Chris Lattner48884cd2007-08-25 00:47:38 +000017721 return;
Evan Cheng364091e2008-09-22 23:57:37 +000017722 case 'J':
17723 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattner2e06dd22009-06-15 04:39:05 +000017724 if (C->getZExtValue() <= 63) {
Chris Lattnere4935152009-06-15 04:01:39 +000017725 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
17726 break;
17727 }
17728 }
17729 return;
17730 case 'K':
17731 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Jakub Staszakdccd7f92012-11-06 23:52:19 +000017732 if (isInt<8>(C->getSExtValue())) {
Evan Cheng364091e2008-09-22 23:57:37 +000017733 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
17734 break;
17735 }
17736 }
17737 return;
Chris Lattner188b9fe2007-03-25 01:57:35 +000017738 case 'N':
17739 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +000017740 if (C->getZExtValue() <= 255) {
17741 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattner48884cd2007-08-25 00:47:38 +000017742 break;
17743 }
Chris Lattner188b9fe2007-03-25 01:57:35 +000017744 }
Chris Lattner48884cd2007-08-25 00:47:38 +000017745 return;
Dale Johannesen78e3e522009-02-12 20:58:09 +000017746 case 'e': {
17747 // 32-bit signed value
17748 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohman7720cb32010-06-18 14:01:07 +000017749 if (ConstantInt::isValueValidForType(Type::getInt32Ty(*DAG.getContext()),
17750 C->getSExtValue())) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000017751 // Widen to 64 bits here to get it sign extended.
Owen Anderson825b72b2009-08-11 20:47:22 +000017752 Result = DAG.getTargetConstant(C->getSExtValue(), MVT::i64);
Dale Johannesen78e3e522009-02-12 20:58:09 +000017753 break;
17754 }
17755 // FIXME gcc accepts some relocatable values here too, but only in certain
17756 // memory models; it's complicated.
17757 }
17758 return;
17759 }
17760 case 'Z': {
17761 // 32-bit unsigned value
17762 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohman7720cb32010-06-18 14:01:07 +000017763 if (ConstantInt::isValueValidForType(Type::getInt32Ty(*DAG.getContext()),
17764 C->getZExtValue())) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000017765 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
17766 break;
17767 }
17768 }
17769 // FIXME gcc accepts some relocatable values here too, but only in certain
17770 // memory models; it's complicated.
17771 return;
17772 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000017773 case 'i': {
Chris Lattner22aaf1d2006-10-31 20:13:11 +000017774 // Literal immediates are always ok.
Chris Lattner48884cd2007-08-25 00:47:38 +000017775 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Op)) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000017776 // Widen to 64 bits here to get it sign extended.
Owen Anderson825b72b2009-08-11 20:47:22 +000017777 Result = DAG.getTargetConstant(CST->getSExtValue(), MVT::i64);
Chris Lattner48884cd2007-08-25 00:47:38 +000017778 break;
17779 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000017780
Dale Johannesene5ff9ef2010-06-24 20:14:51 +000017781 // In any sort of PIC mode addresses need to be computed at runtime by
17782 // adding in a register or some sort of table lookup. These can't
17783 // be used as immediates.
Dale Johannesene2b448c2010-07-06 23:27:00 +000017784 if (Subtarget->isPICStyleGOT() || Subtarget->isPICStyleStubPIC())
Dale Johannesene5ff9ef2010-06-24 20:14:51 +000017785 return;
17786
Chris Lattnerdc43a882007-05-03 16:52:29 +000017787 // If we are in non-pic codegen mode, we allow the address of a global (with
17788 // an optional displacement) to be used with 'i'.
Chris Lattner49921962009-05-08 18:23:14 +000017789 GlobalAddressSDNode *GA = 0;
Chris Lattnerdc43a882007-05-03 16:52:29 +000017790 int64_t Offset = 0;
Scott Michelfdc40a02009-02-17 22:15:04 +000017791
Chris Lattner49921962009-05-08 18:23:14 +000017792 // Match either (GA), (GA+C), (GA+C1+C2), etc.
17793 while (1) {
17794 if ((GA = dyn_cast<GlobalAddressSDNode>(Op))) {
17795 Offset += GA->getOffset();
17796 break;
17797 } else if (Op.getOpcode() == ISD::ADD) {
17798 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
17799 Offset += C->getZExtValue();
17800 Op = Op.getOperand(0);
17801 continue;
17802 }
17803 } else if (Op.getOpcode() == ISD::SUB) {
17804 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
17805 Offset += -C->getZExtValue();
17806 Op = Op.getOperand(0);
17807 continue;
17808 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000017809 }
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000017810
Chris Lattner49921962009-05-08 18:23:14 +000017811 // Otherwise, this isn't something we can handle, reject it.
17812 return;
Chris Lattnerdc43a882007-05-03 16:52:29 +000017813 }
Eric Christopherfd179292009-08-27 18:07:15 +000017814
Dan Gohman46510a72010-04-15 01:51:59 +000017815 const GlobalValue *GV = GA->getGlobal();
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000017816 // If we require an extra load to get this address, as in PIC mode, we
17817 // can't accept it.
Chris Lattner36c25012009-07-10 07:34:39 +000017818 if (isGlobalStubReference(Subtarget->ClassifyGlobalReference(GV,
17819 getTargetMachine())))
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000017820 return;
Scott Michelfdc40a02009-02-17 22:15:04 +000017821
Devang Patel0d881da2010-07-06 22:08:15 +000017822 Result = DAG.getTargetGlobalAddress(GV, Op.getDebugLoc(),
17823 GA->getValueType(0), Offset);
Chris Lattner49921962009-05-08 18:23:14 +000017824 break;
Chris Lattner22aaf1d2006-10-31 20:13:11 +000017825 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000017826 }
Scott Michelfdc40a02009-02-17 22:15:04 +000017827
Gabor Greifba36cb52008-08-28 21:40:38 +000017828 if (Result.getNode()) {
Chris Lattner48884cd2007-08-25 00:47:38 +000017829 Ops.push_back(Result);
17830 return;
17831 }
Dale Johannesen1784d162010-06-25 21:55:36 +000017832 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
Chris Lattner22aaf1d2006-10-31 20:13:11 +000017833}
17834
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000017835std::pair<unsigned, const TargetRegisterClass*>
Chris Lattnerf76d1802006-07-31 23:26:50 +000017836X86TargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +000017837 EVT VT) const {
Chris Lattnerad043e82007-04-09 05:11:28 +000017838 // First, see if this is a constraint that directly corresponds to an LLVM
17839 // register class.
17840 if (Constraint.size() == 1) {
17841 // GCC Constraint Letters
17842 switch (Constraint[0]) {
17843 default: break;
Eric Christopherd176af82011-06-29 17:23:50 +000017844 // TODO: Slight differences here in allocation order and leaving
17845 // RIP in the class. Do they matter any more here than they do
17846 // in the normal allocation?
17847 case 'q': // GENERAL_REGS in 64-bit mode, Q_REGS in 32-bit mode.
17848 if (Subtarget->is64Bit()) {
Craig Topperc9099502012-04-20 06:31:50 +000017849 if (VT == MVT::i32 || VT == MVT::f32)
17850 return std::make_pair(0U, &X86::GR32RegClass);
17851 if (VT == MVT::i16)
17852 return std::make_pair(0U, &X86::GR16RegClass);
17853 if (VT == MVT::i8 || VT == MVT::i1)
17854 return std::make_pair(0U, &X86::GR8RegClass);
17855 if (VT == MVT::i64 || VT == MVT::f64)
17856 return std::make_pair(0U, &X86::GR64RegClass);
17857 break;
Eric Christopherd176af82011-06-29 17:23:50 +000017858 }
17859 // 32-bit fallthrough
17860 case 'Q': // Q_REGS
Nick Lewycky9bf45d02011-07-08 00:19:27 +000017861 if (VT == MVT::i32 || VT == MVT::f32)
Craig Topperc9099502012-04-20 06:31:50 +000017862 return std::make_pair(0U, &X86::GR32_ABCDRegClass);
17863 if (VT == MVT::i16)
17864 return std::make_pair(0U, &X86::GR16_ABCDRegClass);
17865 if (VT == MVT::i8 || VT == MVT::i1)
17866 return std::make_pair(0U, &X86::GR8_ABCD_LRegClass);
17867 if (VT == MVT::i64)
17868 return std::make_pair(0U, &X86::GR64_ABCDRegClass);
Eric Christopherd176af82011-06-29 17:23:50 +000017869 break;
Chris Lattner0f65cad2007-04-09 05:49:22 +000017870 case 'r': // GENERAL_REGS
Chris Lattner0f65cad2007-04-09 05:49:22 +000017871 case 'l': // INDEX_REGS
Eric Christopher5427ede2011-07-14 20:13:52 +000017872 if (VT == MVT::i8 || VT == MVT::i1)
Craig Topperc9099502012-04-20 06:31:50 +000017873 return std::make_pair(0U, &X86::GR8RegClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000017874 if (VT == MVT::i16)
Craig Topperc9099502012-04-20 06:31:50 +000017875 return std::make_pair(0U, &X86::GR16RegClass);
Eric Christopher2bbecd82011-05-19 21:33:47 +000017876 if (VT == MVT::i32 || VT == MVT::f32 || !Subtarget->is64Bit())
Craig Topperc9099502012-04-20 06:31:50 +000017877 return std::make_pair(0U, &X86::GR32RegClass);
17878 return std::make_pair(0U, &X86::GR64RegClass);
Dale Johannesen5f3663e2009-10-07 22:47:20 +000017879 case 'R': // LEGACY_REGS
Eric Christopher5427ede2011-07-14 20:13:52 +000017880 if (VT == MVT::i8 || VT == MVT::i1)
Craig Topperc9099502012-04-20 06:31:50 +000017881 return std::make_pair(0U, &X86::GR8_NOREXRegClass);
Dale Johannesen5f3663e2009-10-07 22:47:20 +000017882 if (VT == MVT::i16)
Craig Topperc9099502012-04-20 06:31:50 +000017883 return std::make_pair(0U, &X86::GR16_NOREXRegClass);
Dale Johannesen5f3663e2009-10-07 22:47:20 +000017884 if (VT == MVT::i32 || !Subtarget->is64Bit())
Craig Topperc9099502012-04-20 06:31:50 +000017885 return std::make_pair(0U, &X86::GR32_NOREXRegClass);
17886 return std::make_pair(0U, &X86::GR64_NOREXRegClass);
Chris Lattnerfce84ac2008-03-11 19:06:29 +000017887 case 'f': // FP Stack registers.
17888 // If SSE is enabled for this VT, use f80 to ensure the isel moves the
17889 // value to the correct fpstack register class.
Owen Anderson825b72b2009-08-11 20:47:22 +000017890 if (VT == MVT::f32 && !isScalarFPTypeInSSEReg(VT))
Craig Topperc9099502012-04-20 06:31:50 +000017891 return std::make_pair(0U, &X86::RFP32RegClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000017892 if (VT == MVT::f64 && !isScalarFPTypeInSSEReg(VT))
Craig Topperc9099502012-04-20 06:31:50 +000017893 return std::make_pair(0U, &X86::RFP64RegClass);
17894 return std::make_pair(0U, &X86::RFP80RegClass);
Chris Lattner6c284d72007-04-12 04:14:49 +000017895 case 'y': // MMX_REGS if MMX allowed.
17896 if (!Subtarget->hasMMX()) break;
Craig Topperc9099502012-04-20 06:31:50 +000017897 return std::make_pair(0U, &X86::VR64RegClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +000017898 case 'Y': // SSE_REGS if SSE2 allowed
Craig Topper1accb7e2012-01-10 06:54:16 +000017899 if (!Subtarget->hasSSE2()) break;
Chris Lattner0f65cad2007-04-09 05:49:22 +000017900 // FALL THROUGH.
Eric Christopher55487552012-01-07 01:02:09 +000017901 case 'x': // SSE_REGS if SSE1 allowed or AVX_REGS if AVX allowed
Craig Topper1accb7e2012-01-10 06:54:16 +000017902 if (!Subtarget->hasSSE1()) break;
Duncan Sands83ec4b62008-06-06 12:08:01 +000017903
Owen Anderson825b72b2009-08-11 20:47:22 +000017904 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner0f65cad2007-04-09 05:49:22 +000017905 default: break;
17906 // Scalar SSE types.
Owen Anderson825b72b2009-08-11 20:47:22 +000017907 case MVT::f32:
17908 case MVT::i32:
Craig Topperc9099502012-04-20 06:31:50 +000017909 return std::make_pair(0U, &X86::FR32RegClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000017910 case MVT::f64:
17911 case MVT::i64:
Craig Topperc9099502012-04-20 06:31:50 +000017912 return std::make_pair(0U, &X86::FR64RegClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +000017913 // Vector types.
Owen Anderson825b72b2009-08-11 20:47:22 +000017914 case MVT::v16i8:
17915 case MVT::v8i16:
17916 case MVT::v4i32:
17917 case MVT::v2i64:
17918 case MVT::v4f32:
17919 case MVT::v2f64:
Craig Topperc9099502012-04-20 06:31:50 +000017920 return std::make_pair(0U, &X86::VR128RegClass);
Eric Christopher55487552012-01-07 01:02:09 +000017921 // AVX types.
17922 case MVT::v32i8:
17923 case MVT::v16i16:
17924 case MVT::v8i32:
17925 case MVT::v4i64:
17926 case MVT::v8f32:
17927 case MVT::v4f64:
Craig Topperc9099502012-04-20 06:31:50 +000017928 return std::make_pair(0U, &X86::VR256RegClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +000017929 }
Chris Lattnerad043e82007-04-09 05:11:28 +000017930 break;
17931 }
17932 }
Scott Michelfdc40a02009-02-17 22:15:04 +000017933
Chris Lattnerf76d1802006-07-31 23:26:50 +000017934 // Use the default implementation in TargetLowering to convert the register
17935 // constraint into a member of a register class.
17936 std::pair<unsigned, const TargetRegisterClass*> Res;
17937 Res = TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
Chris Lattner1a60aa72006-10-31 19:42:44 +000017938
17939 // Not found as a standard register?
17940 if (Res.second == 0) {
Chris Lattner56d77c72009-09-13 22:41:48 +000017941 // Map st(0) -> st(7) -> ST0
17942 if (Constraint.size() == 7 && Constraint[0] == '{' &&
17943 tolower(Constraint[1]) == 's' &&
17944 tolower(Constraint[2]) == 't' &&
17945 Constraint[3] == '(' &&
17946 (Constraint[4] >= '0' && Constraint[4] <= '7') &&
17947 Constraint[5] == ')' &&
17948 Constraint[6] == '}') {
Daniel Dunbara279bc32009-09-20 02:20:51 +000017949
Chris Lattner56d77c72009-09-13 22:41:48 +000017950 Res.first = X86::ST0+Constraint[4]-'0';
Craig Topperc9099502012-04-20 06:31:50 +000017951 Res.second = &X86::RFP80RegClass;
Chris Lattner56d77c72009-09-13 22:41:48 +000017952 return Res;
17953 }
Daniel Dunbara279bc32009-09-20 02:20:51 +000017954
Chris Lattner56d77c72009-09-13 22:41:48 +000017955 // GCC allows "st(0)" to be called just plain "st".
Benjamin Kramer05872ea2009-11-12 20:36:59 +000017956 if (StringRef("{st}").equals_lower(Constraint)) {
Chris Lattner1a60aa72006-10-31 19:42:44 +000017957 Res.first = X86::ST0;
Craig Topperc9099502012-04-20 06:31:50 +000017958 Res.second = &X86::RFP80RegClass;
Chris Lattner56d77c72009-09-13 22:41:48 +000017959 return Res;
Chris Lattner1a60aa72006-10-31 19:42:44 +000017960 }
Chris Lattner56d77c72009-09-13 22:41:48 +000017961
17962 // flags -> EFLAGS
Benjamin Kramer05872ea2009-11-12 20:36:59 +000017963 if (StringRef("{flags}").equals_lower(Constraint)) {
Chris Lattner56d77c72009-09-13 22:41:48 +000017964 Res.first = X86::EFLAGS;
Craig Topperc9099502012-04-20 06:31:50 +000017965 Res.second = &X86::CCRRegClass;
Chris Lattner56d77c72009-09-13 22:41:48 +000017966 return Res;
17967 }
Daniel Dunbara279bc32009-09-20 02:20:51 +000017968
Dale Johannesen330169f2008-11-13 21:52:36 +000017969 // 'A' means EAX + EDX.
17970 if (Constraint == "A") {
17971 Res.first = X86::EAX;
Craig Topperc9099502012-04-20 06:31:50 +000017972 Res.second = &X86::GR32_ADRegClass;
Chris Lattner56d77c72009-09-13 22:41:48 +000017973 return Res;
Dale Johannesen330169f2008-11-13 21:52:36 +000017974 }
Chris Lattner1a60aa72006-10-31 19:42:44 +000017975 return Res;
17976 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000017977
Chris Lattnerf76d1802006-07-31 23:26:50 +000017978 // Otherwise, check to see if this is a register class of the wrong value
17979 // type. For example, we want to map "{ax},i32" -> {eax}, we don't want it to
17980 // turn into {ax},{dx}.
17981 if (Res.second->hasType(VT))
17982 return Res; // Correct type already, nothing to do.
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000017983
Chris Lattnerf76d1802006-07-31 23:26:50 +000017984 // All of the single-register GCC register classes map their values onto
17985 // 16-bit register pieces "ax","dx","cx","bx","si","di","bp","sp". If we
17986 // really want an 8-bit or 32-bit register, map to the appropriate register
17987 // class and return the appropriate register.
Craig Topperc9099502012-04-20 06:31:50 +000017988 if (Res.second == &X86::GR16RegClass) {
Owen Anderson825b72b2009-08-11 20:47:22 +000017989 if (VT == MVT::i8) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000017990 unsigned DestReg = 0;
17991 switch (Res.first) {
17992 default: break;
17993 case X86::AX: DestReg = X86::AL; break;
17994 case X86::DX: DestReg = X86::DL; break;
17995 case X86::CX: DestReg = X86::CL; break;
17996 case X86::BX: DestReg = X86::BL; break;
17997 }
17998 if (DestReg) {
17999 Res.first = DestReg;
Craig Topperc9099502012-04-20 06:31:50 +000018000 Res.second = &X86::GR8RegClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000018001 }
Owen Anderson825b72b2009-08-11 20:47:22 +000018002 } else if (VT == MVT::i32) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000018003 unsigned DestReg = 0;
18004 switch (Res.first) {
18005 default: break;
18006 case X86::AX: DestReg = X86::EAX; break;
18007 case X86::DX: DestReg = X86::EDX; break;
18008 case X86::CX: DestReg = X86::ECX; break;
18009 case X86::BX: DestReg = X86::EBX; break;
18010 case X86::SI: DestReg = X86::ESI; break;
18011 case X86::DI: DestReg = X86::EDI; break;
18012 case X86::BP: DestReg = X86::EBP; break;
18013 case X86::SP: DestReg = X86::ESP; break;
18014 }
18015 if (DestReg) {
18016 Res.first = DestReg;
Craig Topperc9099502012-04-20 06:31:50 +000018017 Res.second = &X86::GR32RegClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000018018 }
Owen Anderson825b72b2009-08-11 20:47:22 +000018019 } else if (VT == MVT::i64) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000018020 unsigned DestReg = 0;
18021 switch (Res.first) {
18022 default: break;
18023 case X86::AX: DestReg = X86::RAX; break;
18024 case X86::DX: DestReg = X86::RDX; break;
18025 case X86::CX: DestReg = X86::RCX; break;
18026 case X86::BX: DestReg = X86::RBX; break;
18027 case X86::SI: DestReg = X86::RSI; break;
18028 case X86::DI: DestReg = X86::RDI; break;
18029 case X86::BP: DestReg = X86::RBP; break;
18030 case X86::SP: DestReg = X86::RSP; break;
18031 }
18032 if (DestReg) {
18033 Res.first = DestReg;
Craig Topperc9099502012-04-20 06:31:50 +000018034 Res.second = &X86::GR64RegClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000018035 }
Chris Lattnerf76d1802006-07-31 23:26:50 +000018036 }
Craig Topperc9099502012-04-20 06:31:50 +000018037 } else if (Res.second == &X86::FR32RegClass ||
18038 Res.second == &X86::FR64RegClass ||
18039 Res.second == &X86::VR128RegClass) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000018040 // Handle references to XMM physical registers that got mapped into the
18041 // wrong class. This can happen with constraints like {xmm0} where the
18042 // target independent register mapper will just pick the first match it can
18043 // find, ignoring the required type.
Eli Friedman52d418d2012-06-25 23:42:33 +000018044
18045 if (VT == MVT::f32 || VT == MVT::i32)
Craig Topperc9099502012-04-20 06:31:50 +000018046 Res.second = &X86::FR32RegClass;
Eli Friedman52d418d2012-06-25 23:42:33 +000018047 else if (VT == MVT::f64 || VT == MVT::i64)
Craig Topperc9099502012-04-20 06:31:50 +000018048 Res.second = &X86::FR64RegClass;
18049 else if (X86::VR128RegClass.hasType(VT))
18050 Res.second = &X86::VR128RegClass;
Eli Friedman52d418d2012-06-25 23:42:33 +000018051 else if (X86::VR256RegClass.hasType(VT))
18052 Res.second = &X86::VR256RegClass;
Chris Lattnerf76d1802006-07-31 23:26:50 +000018053 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000018054
Chris Lattnerf76d1802006-07-31 23:26:50 +000018055 return Res;
18056}
Nadav Rotemb4b04c32012-11-03 00:39:56 +000018057
Nadav Roteme6237022012-11-05 19:32:46 +000018058//===----------------------------------------------------------------------===//
18059//
18060// X86 cost model.
18061//
18062//===----------------------------------------------------------------------===//
18063
18064struct X86CostTblEntry {
18065 int ISD;
18066 MVT Type;
18067 unsigned Cost;
18068};
18069
Nadav Rotemd8eae8b2012-11-06 23:36:00 +000018070static int
18071FindInTable(const X86CostTblEntry *Tbl, unsigned len, int ISD, MVT Ty) {
Nadav Rotem7ae3bcc2012-11-05 23:48:20 +000018072 for (unsigned int i = 0; i < len; ++i)
18073 if (Tbl[i].ISD == ISD && Tbl[i].Type == Ty)
18074 return i;
18075
18076 // Could not find an entry.
18077 return -1;
18078}
18079
Nadav Rotemb0428682012-11-06 19:33:53 +000018080struct X86TypeConversionCostTblEntry {
18081 int ISD;
18082 MVT Dst;
18083 MVT Src;
18084 unsigned Cost;
18085};
18086
Nadav Rotemd8eae8b2012-11-06 23:36:00 +000018087static int
18088FindInConvertTable(const X86TypeConversionCostTblEntry *Tbl, unsigned len,
18089 int ISD, MVT Dst, MVT Src) {
Nadav Rotemb0428682012-11-06 19:33:53 +000018090 for (unsigned int i = 0; i < len; ++i)
18091 if (Tbl[i].ISD == ISD && Tbl[i].Src == Src && Tbl[i].Dst == Dst)
18092 return i;
18093
18094 // Could not find an entry.
18095 return -1;
18096}
18097
Shuxin Yang5518a132012-12-09 03:12:46 +000018098ScalarTargetTransformInfo::PopcntHwSupport
18099X86ScalarTargetTransformImpl::getPopcntHwSupport(unsigned TyWidth) const {
18100 assert(isPowerOf2_32(TyWidth) && "Ty width must be power of 2");
18101 const X86Subtarget &ST = TLI->getTargetMachine().getSubtarget<X86Subtarget>();
18102
18103 // TODO: Currently the __builtin_popcount() implementation using SSE3
18104 // instructions is inefficient. Once the problem is fixed, we should
18105 // call ST.hasSSE3() instead of ST.hasSSE4().
18106 return ST.hasSSE41() ? Fast : None;
18107}
18108
Nadav Rotemb4b04c32012-11-03 00:39:56 +000018109unsigned
18110X86VectorTargetTransformInfo::getArithmeticInstrCost(unsigned Opcode,
18111 Type *Ty) const {
Nadav Roteme6237022012-11-05 19:32:46 +000018112 // Legalize the type.
Nadav Rotem887c1fe2012-11-05 23:57:45 +000018113 std::pair<unsigned, MVT> LT = getTypeLegalizationCost(Ty);
Nadav Roteme6237022012-11-05 19:32:46 +000018114
18115 int ISD = InstructionOpcodeToISD(Opcode);
18116 assert(ISD && "Invalid opcode");
18117
Nadav Rotemb0428682012-11-06 19:33:53 +000018118 const X86Subtarget &ST = TLI->getTargetMachine().getSubtarget<X86Subtarget>();
Nadav Rotemb4b04c32012-11-03 00:39:56 +000018119
Nadav Roteme6237022012-11-05 19:32:46 +000018120 static const X86CostTblEntry AVX1CostTable[] = {
18121 // We don't have to scalarize unsupported ops. We can issue two half-sized
18122 // operations and we only need to extract the upper YMM half.
18123 // Two ops + 1 extract + 1 insert = 4.
18124 { ISD::MUL, MVT::v8i32, 4 },
18125 { ISD::SUB, MVT::v8i32, 4 },
18126 { ISD::ADD, MVT::v8i32, 4 },
18127 { ISD::MUL, MVT::v4i64, 4 },
18128 { ISD::SUB, MVT::v4i64, 4 },
18129 { ISD::ADD, MVT::v4i64, 4 },
18130 };
Nadav Rotemb4b04c32012-11-03 00:39:56 +000018131
Nadav Roteme6237022012-11-05 19:32:46 +000018132 // Look for AVX1 lowering tricks.
Nadav Rotem7ae3bcc2012-11-05 23:48:20 +000018133 if (ST.hasAVX()) {
18134 int Idx = FindInTable(AVX1CostTable, array_lengthof(AVX1CostTable), ISD,
18135 LT.second);
18136 if (Idx != -1)
18137 return LT.first * AVX1CostTable[Idx].Cost;
18138 }
Nadav Roteme6237022012-11-05 19:32:46 +000018139 // Fallback to the default implementation.
Nadav Rotemb4b04c32012-11-03 00:39:56 +000018140 return VectorTargetTransformImpl::getArithmeticInstrCost(Opcode, Ty);
18141}
18142
Nadav Rotemf5637c32012-12-21 01:33:59 +000018143unsigned
18144X86VectorTargetTransformInfo::getMemoryOpCost(unsigned Opcode, Type *Src,
18145 unsigned Alignment,
18146 unsigned AddressSpace) const {
18147 // Legalize the type.
18148 std::pair<unsigned, MVT> LT = getTypeLegalizationCost(Src);
Nick Lewycky71f30bf2012-12-24 19:58:45 +000018149 assert((Opcode == Instruction::Load || Opcode == Instruction::Store) &&
Nadav Rotemf5637c32012-12-21 01:33:59 +000018150 "Invalid Opcode");
18151
18152 const X86Subtarget &ST =
18153 TLI->getTargetMachine().getSubtarget<X86Subtarget>();
18154
18155 // Each load/store unit costs 1.
18156 unsigned Cost = LT.first * 1;
18157
18158 // On Sandybridge 256bit load/stores are double pumped
18159 // (but not on Haswell).
18160 if (LT.second.getSizeInBits() > 128 && !ST.hasAVX2())
18161 Cost*=2;
18162
18163 return Cost;
18164}
18165
Nadav Rotemb4b04c32012-11-03 00:39:56 +000018166unsigned
18167X86VectorTargetTransformInfo::getVectorInstrCost(unsigned Opcode, Type *Val,
Richard Smithe010eb32012-11-05 22:01:44 +000018168 unsigned Index) const {
Nadav Rotema4ab5292012-11-05 21:12:13 +000018169 assert(Val->isVectorTy() && "This must be a vector type");
18170
Nadav Rotem7ae3bcc2012-11-05 23:48:20 +000018171 if (Index != -1U) {
Nadav Rotema4ab5292012-11-05 21:12:13 +000018172 // Legalize the type.
Nadav Rotem887c1fe2012-11-05 23:57:45 +000018173 std::pair<unsigned, MVT> LT = getTypeLegalizationCost(Val);
Nadav Rotema4ab5292012-11-05 21:12:13 +000018174
18175 // This type is legalized to a scalar type.
18176 if (!LT.second.isVector())
18177 return 0;
18178
18179 // The type may be split. Normalize the index to the new type.
18180 unsigned Width = LT.second.getVectorNumElements();
18181 Index = Index % Width;
18182
18183 // Floating point scalars are already located in index #0.
18184 if (Val->getScalarType()->isFloatingPointTy() && Index == 0)
18185 return 0;
18186 }
18187
Nadav Rotemb4b04c32012-11-03 00:39:56 +000018188 return VectorTargetTransformImpl::getVectorInstrCost(Opcode, Val, Index);
18189}
18190
Nadav Rotem7ae3bcc2012-11-05 23:48:20 +000018191unsigned X86VectorTargetTransformInfo::getCmpSelInstrCost(unsigned Opcode,
18192 Type *ValTy,
18193 Type *CondTy) const {
18194 // Legalize the type.
Nadav Rotem887c1fe2012-11-05 23:57:45 +000018195 std::pair<unsigned, MVT> LT = getTypeLegalizationCost(ValTy);
Nadav Rotem7ae3bcc2012-11-05 23:48:20 +000018196
18197 MVT MTy = LT.second;
18198
18199 int ISD = InstructionOpcodeToISD(Opcode);
18200 assert(ISD && "Invalid opcode");
18201
18202 const X86Subtarget &ST =
18203 TLI->getTargetMachine().getSubtarget<X86Subtarget>();
18204
18205 static const X86CostTblEntry SSE42CostTbl[] = {
18206 { ISD::SETCC, MVT::v2f64, 1 },
18207 { ISD::SETCC, MVT::v4f32, 1 },
18208 { ISD::SETCC, MVT::v2i64, 1 },
18209 { ISD::SETCC, MVT::v4i32, 1 },
18210 { ISD::SETCC, MVT::v8i16, 1 },
18211 { ISD::SETCC, MVT::v16i8, 1 },
18212 };
18213
18214 static const X86CostTblEntry AVX1CostTbl[] = {
18215 { ISD::SETCC, MVT::v4f64, 1 },
18216 { ISD::SETCC, MVT::v8f32, 1 },
18217 // AVX1 does not support 8-wide integer compare.
18218 { ISD::SETCC, MVT::v4i64, 4 },
18219 { ISD::SETCC, MVT::v8i32, 4 },
18220 { ISD::SETCC, MVT::v16i16, 4 },
18221 { ISD::SETCC, MVT::v32i8, 4 },
18222 };
18223
18224 static const X86CostTblEntry AVX2CostTbl[] = {
18225 { ISD::SETCC, MVT::v4i64, 1 },
18226 { ISD::SETCC, MVT::v8i32, 1 },
18227 { ISD::SETCC, MVT::v16i16, 1 },
18228 { ISD::SETCC, MVT::v32i8, 1 },
18229 };
18230
Jakub Staszak270bfbd2012-12-18 22:57:56 +000018231 if (ST.hasAVX2()) {
18232 int Idx = FindInTable(AVX2CostTbl, array_lengthof(AVX2CostTbl), ISD, MTy);
Nadav Rotem7ae3bcc2012-11-05 23:48:20 +000018233 if (Idx != -1)
Jakub Staszak270bfbd2012-12-18 22:57:56 +000018234 return LT.first * AVX2CostTbl[Idx].Cost;
Nadav Rotem7ae3bcc2012-11-05 23:48:20 +000018235 }
18236
18237 if (ST.hasAVX()) {
18238 int Idx = FindInTable(AVX1CostTbl, array_lengthof(AVX1CostTbl), ISD, MTy);
18239 if (Idx != -1)
18240 return LT.first * AVX1CostTbl[Idx].Cost;
18241 }
18242
Jakub Staszak270bfbd2012-12-18 22:57:56 +000018243 if (ST.hasSSE42()) {
18244 int Idx = FindInTable(SSE42CostTbl, array_lengthof(SSE42CostTbl), ISD, MTy);
Nadav Rotem7ae3bcc2012-11-05 23:48:20 +000018245 if (Idx != -1)
Jakub Staszak270bfbd2012-12-18 22:57:56 +000018246 return LT.first * SSE42CostTbl[Idx].Cost;
Nadav Rotem7ae3bcc2012-11-05 23:48:20 +000018247 }
18248
18249 return VectorTargetTransformImpl::getCmpSelInstrCost(Opcode, ValTy, CondTy);
18250}
18251
Nadav Rotemb0428682012-11-06 19:33:53 +000018252unsigned X86VectorTargetTransformInfo::getCastInstrCost(unsigned Opcode,
18253 Type *Dst,
18254 Type *Src) const {
18255 int ISD = InstructionOpcodeToISD(Opcode);
18256 assert(ISD && "Invalid opcode");
Nadav Rotem7ae3bcc2012-11-05 23:48:20 +000018257
Nadav Rotemb0428682012-11-06 19:33:53 +000018258 EVT SrcTy = TLI->getValueType(Src);
18259 EVT DstTy = TLI->getValueType(Dst);
18260
18261 if (!SrcTy.isSimple() || !DstTy.isSimple())
18262 return VectorTargetTransformImpl::getCastInstrCost(Opcode, Dst, Src);
18263
18264 const X86Subtarget &ST = TLI->getTargetMachine().getSubtarget<X86Subtarget>();
18265
18266 static const X86TypeConversionCostTblEntry AVXConversionTbl[] = {
18267 { ISD::SIGN_EXTEND, MVT::v8i32, MVT::v8i16, 1 },
18268 { ISD::ZERO_EXTEND, MVT::v8i32, MVT::v8i16, 1 },
18269 { ISD::SIGN_EXTEND, MVT::v4i64, MVT::v4i32, 1 },
18270 { ISD::ZERO_EXTEND, MVT::v4i64, MVT::v4i32, 1 },
18271 { ISD::TRUNCATE, MVT::v4i32, MVT::v4i64, 1 },
18272 { ISD::TRUNCATE, MVT::v8i16, MVT::v8i32, 1 },
18273 { ISD::SINT_TO_FP, MVT::v8f32, MVT::v8i8, 1 },
18274 { ISD::SINT_TO_FP, MVT::v4f32, MVT::v4i8, 1 },
18275 { ISD::UINT_TO_FP, MVT::v8f32, MVT::v8i8, 1 },
18276 { ISD::UINT_TO_FP, MVT::v4f32, MVT::v4i8, 1 },
Nadav Rotemb14a5f52012-11-09 07:02:24 +000018277 { ISD::FP_TO_SINT, MVT::v8i8, MVT::v8f32, 1 },
18278 { ISD::FP_TO_SINT, MVT::v4i8, MVT::v4f32, 1 },
Nadav Rotemb0428682012-11-06 19:33:53 +000018279 { ISD::ZERO_EXTEND, MVT::v8i32, MVT::v8i1, 6 },
18280 { ISD::SIGN_EXTEND, MVT::v8i32, MVT::v8i1, 9 },
Nadav Rotema6fb97a2012-11-06 21:17:17 +000018281 { ISD::TRUNCATE, MVT::v8i32, MVT::v8i64, 3 },
Nadav Rotemb0428682012-11-06 19:33:53 +000018282 };
18283
18284 if (ST.hasAVX()) {
18285 int Idx = FindInConvertTable(AVXConversionTbl,
18286 array_lengthof(AVXConversionTbl),
18287 ISD, DstTy.getSimpleVT(), SrcTy.getSimpleVT());
18288 if (Idx != -1)
18289 return AVXConversionTbl[Idx].Cost;
18290 }
18291
18292 return VectorTargetTransformImpl::getCastInstrCost(Opcode, Dst, Src);
18293}
Nadav Rotem7ae3bcc2012-11-05 23:48:20 +000018294
Nadav Rotemae34b422012-12-28 08:19:03 +000018295
18296unsigned X86VectorTargetTransformInfo::getShuffleCost(ShuffleKind Kind, Type *Tp,
18297 int Index) const {
18298 // We only estimate the cost of reverse shuffles.
18299 if (Kind != Reverse)
18300 return VectorTargetTransformImpl::getShuffleCost(Kind, Tp, Index);
18301
18302 std::pair<unsigned, MVT> LT = getTypeLegalizationCost(Tp);
18303 unsigned Cost = 1;
18304 if (LT.second.getSizeInBits() > 128)
18305 Cost = 3; // Extract + insert + copy.
18306
18307 // Multiple by the number of parts.
18308 return Cost * LT.first;
18309}
18310