blob: d3c21bd703f74fab742a2edfcb27394c6e67a997 [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
Craig Topperbf404372012-08-31 15:40:30 +00001128 if (Subtarget->hasFMA() || Subtarget->hasFMA4()) {
Craig Topper3dcefc82012-11-21 05:36:24 +00001129 setOperationAction(ISD::FMA, MVT::v8f32, Legal);
1130 setOperationAction(ISD::FMA, MVT::v4f64, Legal);
1131 setOperationAction(ISD::FMA, MVT::v4f32, Legal);
1132 setOperationAction(ISD::FMA, MVT::v2f64, Legal);
1133 setOperationAction(ISD::FMA, MVT::f32, Legal);
1134 setOperationAction(ISD::FMA, MVT::f64, Legal);
Elena Demikhovsky1503aba2012-08-01 12:06:00 +00001135 }
Craig Topper880ef452012-08-11 22:34:26 +00001136
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00001137 if (Subtarget->hasInt256()) {
Craig Topperaaa643c2011-11-09 07:28:55 +00001138 setOperationAction(ISD::ADD, MVT::v4i64, Legal);
1139 setOperationAction(ISD::ADD, MVT::v8i32, Legal);
1140 setOperationAction(ISD::ADD, MVT::v16i16, Legal);
1141 setOperationAction(ISD::ADD, MVT::v32i8, Legal);
Craig Topper13894fa2011-08-24 06:14:18 +00001142
Craig Topperaaa643c2011-11-09 07:28:55 +00001143 setOperationAction(ISD::SUB, MVT::v4i64, Legal);
1144 setOperationAction(ISD::SUB, MVT::v8i32, Legal);
1145 setOperationAction(ISD::SUB, MVT::v16i16, Legal);
1146 setOperationAction(ISD::SUB, MVT::v32i8, Legal);
Craig Topper13894fa2011-08-24 06:14:18 +00001147
Craig Topperaaa643c2011-11-09 07:28:55 +00001148 setOperationAction(ISD::MUL, MVT::v4i64, Custom);
1149 setOperationAction(ISD::MUL, MVT::v8i32, Legal);
1150 setOperationAction(ISD::MUL, MVT::v16i16, Legal);
Craig Topper46154eb2011-11-11 07:39:23 +00001151 // Don't lower v32i8 because there is no 128-bit byte mul
Nadav Rotembb539bf2011-11-09 13:21:28 +00001152
1153 setOperationAction(ISD::VSELECT, MVT::v32i8, Legal);
Craig Topper7be5dfd2011-11-12 09:58:49 +00001154
1155 setOperationAction(ISD::SRL, MVT::v4i64, Legal);
1156 setOperationAction(ISD::SRL, MVT::v8i32, Legal);
1157
1158 setOperationAction(ISD::SHL, MVT::v4i64, Legal);
1159 setOperationAction(ISD::SHL, MVT::v8i32, Legal);
1160
1161 setOperationAction(ISD::SRA, MVT::v8i32, Legal);
Craig Topperaaa643c2011-11-09 07:28:55 +00001162 } else {
1163 setOperationAction(ISD::ADD, MVT::v4i64, Custom);
1164 setOperationAction(ISD::ADD, MVT::v8i32, Custom);
1165 setOperationAction(ISD::ADD, MVT::v16i16, Custom);
1166 setOperationAction(ISD::ADD, MVT::v32i8, Custom);
1167
1168 setOperationAction(ISD::SUB, MVT::v4i64, Custom);
1169 setOperationAction(ISD::SUB, MVT::v8i32, Custom);
1170 setOperationAction(ISD::SUB, MVT::v16i16, Custom);
1171 setOperationAction(ISD::SUB, MVT::v32i8, Custom);
1172
1173 setOperationAction(ISD::MUL, MVT::v4i64, Custom);
1174 setOperationAction(ISD::MUL, MVT::v8i32, Custom);
1175 setOperationAction(ISD::MUL, MVT::v16i16, Custom);
1176 // Don't lower v32i8 because there is no 128-bit byte mul
Craig Topper7be5dfd2011-11-12 09:58:49 +00001177
1178 setOperationAction(ISD::SRL, MVT::v4i64, Custom);
1179 setOperationAction(ISD::SRL, MVT::v8i32, Custom);
1180
1181 setOperationAction(ISD::SHL, MVT::v4i64, Custom);
1182 setOperationAction(ISD::SHL, MVT::v8i32, Custom);
1183
1184 setOperationAction(ISD::SRA, MVT::v8i32, Custom);
Craig Topperaaa643c2011-11-09 07:28:55 +00001185 }
Craig Topper13894fa2011-08-24 06:14:18 +00001186
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001187 // Custom lower several nodes for 256-bit types.
Jakub Staszak6610b1d2012-04-29 20:52:53 +00001188 for (int i = MVT::FIRST_VECTOR_VALUETYPE;
1189 i <= MVT::LAST_VECTOR_VALUETYPE; ++i) {
Craig Topper0d1f1762012-08-12 00:34:56 +00001190 MVT VT = (MVT::SimpleValueType)i;
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001191
1192 // Extract subvector is special because the value type
1193 // (result) is 128-bit but the source is 256-bit wide.
1194 if (VT.is128BitVector())
Craig Topper0d1f1762012-08-12 00:34:56 +00001195 setOperationAction(ISD::EXTRACT_SUBVECTOR, VT, Custom);
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001196
1197 // Do not attempt to custom lower other non-256-bit vectors
1198 if (!VT.is256BitVector())
David Greene9b9838d2009-06-29 16:47:10 +00001199 continue;
David Greene54d8eba2011-01-27 22:38:56 +00001200
Craig Topper0d1f1762012-08-12 00:34:56 +00001201 setOperationAction(ISD::BUILD_VECTOR, VT, Custom);
1202 setOperationAction(ISD::VECTOR_SHUFFLE, VT, Custom);
1203 setOperationAction(ISD::INSERT_VECTOR_ELT, VT, Custom);
1204 setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT, Custom);
1205 setOperationAction(ISD::SCALAR_TO_VECTOR, VT, Custom);
1206 setOperationAction(ISD::INSERT_SUBVECTOR, VT, Custom);
1207 setOperationAction(ISD::CONCAT_VECTORS, VT, Custom);
David Greene9b9838d2009-06-29 16:47:10 +00001208 }
1209
David Greene54d8eba2011-01-27 22:38:56 +00001210 // Promote v32i8, v16i16, v8i32 select, and, or, xor to v4i64.
Jakub Staszak6610b1d2012-04-29 20:52:53 +00001211 for (int i = MVT::v32i8; i != MVT::v4i64; ++i) {
Craig Topper0d1f1762012-08-12 00:34:56 +00001212 MVT VT = (MVT::SimpleValueType)i;
David Greene54d8eba2011-01-27 22:38:56 +00001213
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001214 // Do not attempt to promote non-256-bit vectors
1215 if (!VT.is256BitVector())
David Greene54d8eba2011-01-27 22:38:56 +00001216 continue;
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001217
Craig Topper0d1f1762012-08-12 00:34:56 +00001218 setOperationAction(ISD::AND, VT, Promote);
1219 AddPromotedToType (ISD::AND, VT, MVT::v4i64);
1220 setOperationAction(ISD::OR, VT, Promote);
1221 AddPromotedToType (ISD::OR, VT, MVT::v4i64);
1222 setOperationAction(ISD::XOR, VT, Promote);
1223 AddPromotedToType (ISD::XOR, VT, MVT::v4i64);
1224 setOperationAction(ISD::LOAD, VT, Promote);
1225 AddPromotedToType (ISD::LOAD, VT, MVT::v4i64);
1226 setOperationAction(ISD::SELECT, VT, Promote);
1227 AddPromotedToType (ISD::SELECT, VT, MVT::v4i64);
David Greene54d8eba2011-01-27 22:38:56 +00001228 }
David Greene9b9838d2009-06-29 16:47:10 +00001229 }
1230
Nadav Rotemd0f3ef82011-07-14 11:11:14 +00001231 // SIGN_EXTEND_INREGs are evaluated by the extend type. Handle the expansion
1232 // of this type with custom code.
Jakub Staszak6610b1d2012-04-29 20:52:53 +00001233 for (int VT = MVT::FIRST_VECTOR_VALUETYPE;
1234 VT != MVT::LAST_VECTOR_VALUETYPE; VT++) {
Chad Rosier30450e82011-12-22 22:35:21 +00001235 setOperationAction(ISD::SIGN_EXTEND_INREG, (MVT::SimpleValueType)VT,
1236 Custom);
Nadav Rotemd0f3ef82011-07-14 11:11:14 +00001237 }
1238
Evan Cheng6be2c582006-04-05 23:38:46 +00001239 // We want to custom lower some of our intrinsics.
Owen Anderson825b72b2009-08-11 20:47:22 +00001240 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
Benjamin Kramerb9bee042012-07-12 09:31:43 +00001241 setOperationAction(ISD::INTRINSIC_W_CHAIN, MVT::Other, Custom);
Evan Cheng6be2c582006-04-05 23:38:46 +00001242
Eli Friedman962f5492010-06-02 19:35:46 +00001243 // Only custom-lower 64-bit SADDO and friends on 64-bit because we don't
1244 // handle type legalization for these operations here.
Dan Gohman71c62a22010-06-02 19:13:40 +00001245 //
Eli Friedman962f5492010-06-02 19:35:46 +00001246 // FIXME: We really should do custom legalization for addition and
1247 // subtraction on x86-32 once PR3203 is fixed. We really can't do much better
1248 // than generic legalization for 64-bit multiplication-with-overflow, though.
Chris Lattnera34b3cf2010-12-19 20:03:11 +00001249 for (unsigned i = 0, e = 3+Subtarget->is64Bit(); i != e; ++i) {
1250 // Add/Sub/Mul with overflow operations are custom lowered.
1251 MVT VT = IntVTs[i];
1252 setOperationAction(ISD::SADDO, VT, Custom);
1253 setOperationAction(ISD::UADDO, VT, Custom);
1254 setOperationAction(ISD::SSUBO, VT, Custom);
1255 setOperationAction(ISD::USUBO, VT, Custom);
1256 setOperationAction(ISD::SMULO, VT, Custom);
1257 setOperationAction(ISD::UMULO, VT, Custom);
Eli Friedmana993f0a2010-06-02 00:27:18 +00001258 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00001259
Chris Lattnera34b3cf2010-12-19 20:03:11 +00001260 // There are no 8-bit 3-address imul/mul instructions
1261 setOperationAction(ISD::SMULO, MVT::i8, Expand);
1262 setOperationAction(ISD::UMULO, MVT::i8, Expand);
Bill Wendling41ea7e72008-11-24 19:21:46 +00001263
Evan Chengd54f2d52009-03-31 19:38:51 +00001264 if (!Subtarget->is64Bit()) {
1265 // These libcalls are not available in 32-bit.
1266 setLibcallName(RTLIB::SHL_I128, 0);
1267 setLibcallName(RTLIB::SRL_I128, 0);
1268 setLibcallName(RTLIB::SRA_I128, 0);
1269 }
1270
Evan Cheng206ee9d2006-07-07 08:33:52 +00001271 // We have target-specific dag combine patterns for the following nodes:
1272 setTargetDAGCombine(ISD::VECTOR_SHUFFLE);
Dan Gohman1bbf72b2010-03-15 23:23:03 +00001273 setTargetDAGCombine(ISD::EXTRACT_VECTOR_ELT);
Duncan Sands6bcd2192011-09-17 16:49:39 +00001274 setTargetDAGCombine(ISD::VSELECT);
Chris Lattner83e6c992006-10-04 06:57:07 +00001275 setTargetDAGCombine(ISD::SELECT);
Nate Begeman740ab032009-01-26 00:52:55 +00001276 setTargetDAGCombine(ISD::SHL);
1277 setTargetDAGCombine(ISD::SRA);
1278 setTargetDAGCombine(ISD::SRL);
Evan Cheng760d1942010-01-04 21:22:48 +00001279 setTargetDAGCombine(ISD::OR);
Nate Begemanb65c1752010-12-17 22:55:37 +00001280 setTargetDAGCombine(ISD::AND);
Benjamin Kramer7d6fe132010-12-21 21:41:44 +00001281 setTargetDAGCombine(ISD::ADD);
Duncan Sands17470be2011-09-22 20:15:48 +00001282 setTargetDAGCombine(ISD::FADD);
1283 setTargetDAGCombine(ISD::FSUB);
Elena Demikhovsky1503aba2012-08-01 12:06:00 +00001284 setTargetDAGCombine(ISD::FMA);
Benjamin Kramer7d6fe132010-12-21 21:41:44 +00001285 setTargetDAGCombine(ISD::SUB);
Nadav Rotem91e43fd2011-09-18 10:39:32 +00001286 setTargetDAGCombine(ISD::LOAD);
Chris Lattner149a4e52008-02-22 02:09:43 +00001287 setTargetDAGCombine(ISD::STORE);
Evan Cheng2e489c42009-12-16 00:53:11 +00001288 setTargetDAGCombine(ISD::ZERO_EXTEND);
Elena Demikhovsky1da58672012-04-22 09:39:03 +00001289 setTargetDAGCombine(ISD::ANY_EXTEND);
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +00001290 setTargetDAGCombine(ISD::SIGN_EXTEND);
Elena Demikhovsky3ae98152012-02-01 07:56:44 +00001291 setTargetDAGCombine(ISD::TRUNCATE);
Stuart Hastingsf99a4b82011-06-06 23:15:58 +00001292 setTargetDAGCombine(ISD::SINT_TO_FP);
Chad Rosiera73b6fc2012-04-27 22:33:25 +00001293 setTargetDAGCombine(ISD::SETCC);
Evan Cheng0b0cd912009-03-28 05:57:29 +00001294 if (Subtarget->is64Bit())
1295 setTargetDAGCombine(ISD::MUL);
Manman Ren92363622012-06-07 22:39:10 +00001296 setTargetDAGCombine(ISD::XOR);
Evan Cheng206ee9d2006-07-07 08:33:52 +00001297
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001298 computeRegisterProperties();
1299
Evan Cheng05219282011-01-06 06:52:41 +00001300 // On Darwin, -Os means optimize for size without hurting performance,
1301 // do not reduce the limit.
Dan Gohman87060f52008-06-30 21:00:56 +00001302 maxStoresPerMemset = 16; // For @llvm.memset -> sequence of stores
Evan Cheng05219282011-01-06 06:52:41 +00001303 maxStoresPerMemsetOptSize = Subtarget->isTargetDarwin() ? 16 : 8;
Evan Cheng255f20f2010-04-01 06:04:33 +00001304 maxStoresPerMemcpy = 8; // For @llvm.memcpy -> sequence of stores
Evan Cheng05219282011-01-06 06:52:41 +00001305 maxStoresPerMemcpyOptSize = Subtarget->isTargetDarwin() ? 8 : 4;
1306 maxStoresPerMemmove = 8; // For @llvm.memmove -> sequence of stores
1307 maxStoresPerMemmoveOptSize = Subtarget->isTargetDarwin() ? 8 : 4;
Jakob Stoklund Olesen8c741b82011-12-06 01:26:19 +00001308 setPrefLoopAlignment(4); // 2^4 bytes.
Evan Cheng6ebf7bc2009-05-13 21:42:09 +00001309 benefitFromCodePlacementOpt = true;
Eli Friedmanfc5d3052011-05-06 20:34:06 +00001310
Benjamin Krameraaf723d2012-05-05 12:49:14 +00001311 // Predictable cmov don't hurt on atom because it's in-order.
1312 predictableSelectIsExpensive = !Subtarget->isAtom();
1313
Jakob Stoklund Olesen8c741b82011-12-06 01:26:19 +00001314 setPrefFunctionAlignment(4); // 2^4 bytes.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001315}
1316
Duncan Sands28b77e92011-09-06 19:07:46 +00001317EVT X86TargetLowering::getSetCCResultType(EVT VT) const {
1318 if (!VT.isVector()) return MVT::i8;
1319 return VT.changeVectorElementTypeToInteger();
Scott Michel5b8f82e2008-03-10 15:42:14 +00001320}
1321
Evan Cheng29286502008-01-23 23:17:41 +00001322/// getMaxByValAlign - Helper for getByValTypeAlignment to determine
1323/// the desired ByVal argument alignment.
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001324static void getMaxByValAlign(Type *Ty, unsigned &MaxAlign) {
Evan Cheng29286502008-01-23 23:17:41 +00001325 if (MaxAlign == 16)
1326 return;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001327 if (VectorType *VTy = dyn_cast<VectorType>(Ty)) {
Evan Cheng29286502008-01-23 23:17:41 +00001328 if (VTy->getBitWidth() == 128)
1329 MaxAlign = 16;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001330 } else if (ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
Evan Cheng29286502008-01-23 23:17:41 +00001331 unsigned EltAlign = 0;
1332 getMaxByValAlign(ATy->getElementType(), EltAlign);
1333 if (EltAlign > MaxAlign)
1334 MaxAlign = EltAlign;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001335 } else if (StructType *STy = dyn_cast<StructType>(Ty)) {
Evan Cheng29286502008-01-23 23:17:41 +00001336 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
1337 unsigned EltAlign = 0;
1338 getMaxByValAlign(STy->getElementType(i), EltAlign);
1339 if (EltAlign > MaxAlign)
1340 MaxAlign = EltAlign;
1341 if (MaxAlign == 16)
1342 break;
1343 }
1344 }
Evan Cheng29286502008-01-23 23:17:41 +00001345}
1346
1347/// getByValTypeAlignment - Return the desired alignment for ByVal aggregate
1348/// function arguments in the caller parameter area. For X86, aggregates
Dale Johannesen0c191872008-02-08 19:48:20 +00001349/// that contain SSE vectors are placed at 16-byte boundaries while the rest
1350/// are at 4-byte boundaries.
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001351unsigned X86TargetLowering::getByValTypeAlignment(Type *Ty) const {
Evan Cheng1887c1c2008-08-21 21:00:15 +00001352 if (Subtarget->is64Bit()) {
1353 // Max of 8 and alignment of type.
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00001354 unsigned TyAlign = TD->getABITypeAlignment(Ty);
Evan Cheng1887c1c2008-08-21 21:00:15 +00001355 if (TyAlign > 8)
1356 return TyAlign;
1357 return 8;
1358 }
1359
Evan Cheng29286502008-01-23 23:17:41 +00001360 unsigned Align = 4;
Craig Topper1accb7e2012-01-10 06:54:16 +00001361 if (Subtarget->hasSSE1())
Dale Johannesen0c191872008-02-08 19:48:20 +00001362 getMaxByValAlign(Ty, Align);
Evan Cheng29286502008-01-23 23:17:41 +00001363 return Align;
1364}
Chris Lattner2b02a442007-02-25 08:29:00 +00001365
Evan Chengf0df0312008-05-15 08:39:06 +00001366/// getOptimalMemOpType - Returns the target specific optimal type for load
Evan Chengc3b0c342010-04-08 07:37:57 +00001367/// and store operations as a result of memset, memcpy, and memmove
1368/// lowering. If DstAlign is zero that means it's safe to destination
1369/// alignment can satisfy any constraint. Similarly if SrcAlign is zero it
1370/// means there isn't a need to check it against alignment requirement,
Evan Cheng946a3a92012-12-12 02:34:41 +00001371/// probably because the source does not need to be loaded. If 'IsMemset' is
1372/// true, that means it's expanding a memset. If 'ZeroMemset' is true, that
1373/// means it's a memset of zero. 'MemcpyStrSrc' indicates whether the memcpy
1374/// source is constant so it does not need to be loaded.
Dan Gohman37f32ee2010-04-16 20:11:05 +00001375/// It returns EVT::Other if the type should be determined using generic
1376/// target-independent logic.
Owen Andersone50ed302009-08-10 22:56:29 +00001377EVT
Evan Cheng255f20f2010-04-01 06:04:33 +00001378X86TargetLowering::getOptimalMemOpType(uint64_t Size,
1379 unsigned DstAlign, unsigned SrcAlign,
Evan Cheng946a3a92012-12-12 02:34:41 +00001380 bool IsMemset, bool ZeroMemset,
Evan Chengc3b0c342010-04-08 07:37:57 +00001381 bool MemcpyStrSrc,
Dan Gohman37f32ee2010-04-16 20:11:05 +00001382 MachineFunction &MF) const {
Dan Gohman37f32ee2010-04-16 20:11:05 +00001383 const Function *F = MF.getFunction();
Evan Cheng946a3a92012-12-12 02:34:41 +00001384 if ((!IsMemset || ZeroMemset) &&
Bill Wendling034b94b2012-12-19 07:18:57 +00001385 !F->getFnAttributes().hasAttribute(Attribute::NoImplicitFloat)) {
Evan Cheng255f20f2010-04-01 06:04:33 +00001386 if (Size >= 16 &&
Evan Chenga5e13622011-01-07 19:35:30 +00001387 (Subtarget->isUnalignedMemAccessFast() ||
1388 ((DstAlign == 0 || DstAlign >= 16) &&
Benjamin Kramer2dbe9292012-11-14 20:08:40 +00001389 (SrcAlign == 0 || SrcAlign >= 16)))) {
1390 if (Size >= 32) {
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00001391 if (Subtarget->hasInt256())
Craig Topper562659f2012-01-13 08:32:21 +00001392 return MVT::v8i32;
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00001393 if (Subtarget->hasFp256())
Craig Topper562659f2012-01-13 08:32:21 +00001394 return MVT::v8f32;
1395 }
Craig Topper1accb7e2012-01-10 06:54:16 +00001396 if (Subtarget->hasSSE2())
Evan Cheng255f20f2010-04-01 06:04:33 +00001397 return MVT::v4i32;
Craig Topper1accb7e2012-01-10 06:54:16 +00001398 if (Subtarget->hasSSE1())
Evan Cheng255f20f2010-04-01 06:04:33 +00001399 return MVT::v4f32;
Evan Chengc3b0c342010-04-08 07:37:57 +00001400 } else if (!MemcpyStrSrc && Size >= 8 &&
Evan Cheng3ea97552010-04-01 20:27:45 +00001401 !Subtarget->is64Bit() &&
Craig Topper1accb7e2012-01-10 06:54:16 +00001402 Subtarget->hasSSE2()) {
Evan Chengc3b0c342010-04-08 07:37:57 +00001403 // Do not use f64 to lower memcpy if source is string constant. It's
1404 // better to use i32 to avoid the loads.
Evan Cheng255f20f2010-04-01 06:04:33 +00001405 return MVT::f64;
Evan Chengc3b0c342010-04-08 07:37:57 +00001406 }
Chris Lattner4002a1b2008-10-28 05:49:35 +00001407 }
Evan Chengf0df0312008-05-15 08:39:06 +00001408 if (Subtarget->is64Bit() && Size >= 8)
Owen Anderson825b72b2009-08-11 20:47:22 +00001409 return MVT::i64;
1410 return MVT::i32;
Evan Chengf0df0312008-05-15 08:39:06 +00001411}
1412
Evan Cheng7d342672012-12-12 01:32:07 +00001413bool X86TargetLowering::isSafeMemOpType(MVT VT) const {
Evan Cheng61f4dfe2012-12-12 00:42:09 +00001414 if (VT == MVT::f32)
1415 return X86ScalarSSEf32;
1416 else if (VT == MVT::f64)
1417 return X86ScalarSSEf64;
Evan Cheng7d342672012-12-12 01:32:07 +00001418 return true;
Evan Cheng61f4dfe2012-12-12 00:42:09 +00001419}
1420
Evan Cheng376642e2012-12-10 23:21:26 +00001421bool
1422X86TargetLowering::allowsUnalignedMemoryAccesses(EVT VT, bool *Fast) const {
1423 if (Fast)
1424 *Fast = Subtarget->isUnalignedMemAccessFast();
1425 return true;
1426}
1427
Chris Lattner5e1df8d2010-01-25 23:38:14 +00001428/// getJumpTableEncoding - Return the entry encoding for a jump table in the
1429/// current function. The returned value is a member of the
1430/// MachineJumpTableInfo::JTEntryKind enum.
1431unsigned X86TargetLowering::getJumpTableEncoding() const {
1432 // In GOT pic mode, each entry in the jump table is emitted as a @GOTOFF
1433 // symbol.
1434 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1435 Subtarget->isPICStyleGOT())
Chris Lattnerc64daab2010-01-26 05:02:42 +00001436 return MachineJumpTableInfo::EK_Custom32;
Michael J. Spencerec38de22010-10-10 22:04:20 +00001437
Chris Lattner5e1df8d2010-01-25 23:38:14 +00001438 // Otherwise, use the normal jump table encoding heuristics.
1439 return TargetLowering::getJumpTableEncoding();
1440}
1441
Chris Lattnerc64daab2010-01-26 05:02:42 +00001442const MCExpr *
1443X86TargetLowering::LowerCustomJumpTableEntry(const MachineJumpTableInfo *MJTI,
1444 const MachineBasicBlock *MBB,
1445 unsigned uid,MCContext &Ctx) const{
1446 assert(getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1447 Subtarget->isPICStyleGOT());
1448 // In 32-bit ELF systems, our jump table entries are formed with @GOTOFF
1449 // entries.
Daniel Dunbar4e815f82010-03-15 23:51:06 +00001450 return MCSymbolRefExpr::Create(MBB->getSymbol(),
1451 MCSymbolRefExpr::VK_GOTOFF, Ctx);
Chris Lattnerc64daab2010-01-26 05:02:42 +00001452}
1453
Evan Chengcc415862007-11-09 01:32:10 +00001454/// getPICJumpTableRelocaBase - Returns relocation base for the given PIC
1455/// jumptable.
Dan Gohman475871a2008-07-27 21:46:04 +00001456SDValue X86TargetLowering::getPICJumpTableRelocBase(SDValue Table,
Chris Lattner589c6f62010-01-26 06:28:43 +00001457 SelectionDAG &DAG) const {
Chris Lattnere4df7562009-07-09 03:15:51 +00001458 if (!Subtarget->is64Bit())
Dale Johannesenb300d2a2009-02-07 00:55:49 +00001459 // This doesn't have DebugLoc associated with it, but is not really the
1460 // same as a Register.
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00001461 return DAG.getNode(X86ISD::GlobalBaseReg, DebugLoc(), getPointerTy());
Evan Chengcc415862007-11-09 01:32:10 +00001462 return Table;
1463}
1464
Chris Lattner589c6f62010-01-26 06:28:43 +00001465/// getPICJumpTableRelocBaseExpr - This returns the relocation base for the
1466/// given PIC jumptable, the same as getPICJumpTableRelocBase, but as an
1467/// MCExpr.
1468const MCExpr *X86TargetLowering::
1469getPICJumpTableRelocBaseExpr(const MachineFunction *MF, unsigned JTI,
1470 MCContext &Ctx) const {
1471 // X86-64 uses RIP relative addressing based on the jump table label.
1472 if (Subtarget->isPICStyleRIPRel())
1473 return TargetLowering::getPICJumpTableRelocBaseExpr(MF, JTI, Ctx);
1474
1475 // Otherwise, the reference is relative to the PIC base.
Chris Lattner142b5312010-11-14 22:48:15 +00001476 return MCSymbolRefExpr::Create(MF->getPICBaseSymbol(), Ctx);
Chris Lattner589c6f62010-01-26 06:28:43 +00001477}
1478
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00001479// FIXME: Why this routine is here? Move to RegInfo!
Evan Chengdee81012010-07-26 21:50:05 +00001480std::pair<const TargetRegisterClass*, uint8_t>
Patrik Hagglund03405572012-12-19 11:30:36 +00001481X86TargetLowering::findRepresentativeClass(MVT VT) const{
Evan Chengdee81012010-07-26 21:50:05 +00001482 const TargetRegisterClass *RRC = 0;
1483 uint8_t Cost = 1;
Patrik Hagglund03405572012-12-19 11:30:36 +00001484 switch (VT.SimpleTy) {
Evan Chengdee81012010-07-26 21:50:05 +00001485 default:
1486 return TargetLowering::findRepresentativeClass(VT);
1487 case MVT::i8: case MVT::i16: case MVT::i32: case MVT::i64:
Craig Topperc9099502012-04-20 06:31:50 +00001488 RRC = Subtarget->is64Bit() ?
1489 (const TargetRegisterClass*)&X86::GR64RegClass :
1490 (const TargetRegisterClass*)&X86::GR32RegClass;
Evan Chengdee81012010-07-26 21:50:05 +00001491 break;
Dale Johannesen0488fb62010-09-30 23:57:10 +00001492 case MVT::x86mmx:
Craig Topperc9099502012-04-20 06:31:50 +00001493 RRC = &X86::VR64RegClass;
Evan Chengdee81012010-07-26 21:50:05 +00001494 break;
1495 case MVT::f32: case MVT::f64:
1496 case MVT::v16i8: case MVT::v8i16: case MVT::v4i32: case MVT::v2i64:
1497 case MVT::v4f32: case MVT::v2f64:
1498 case MVT::v32i8: case MVT::v8i32: case MVT::v4i64: case MVT::v8f32:
1499 case MVT::v4f64:
Craig Topperc9099502012-04-20 06:31:50 +00001500 RRC = &X86::VR128RegClass;
Evan Chengdee81012010-07-26 21:50:05 +00001501 break;
1502 }
1503 return std::make_pair(RRC, Cost);
1504}
1505
Eric Christopherf7a0c7b2010-07-06 05:18:56 +00001506bool X86TargetLowering::getStackCookieLocation(unsigned &AddressSpace,
1507 unsigned &Offset) const {
1508 if (!Subtarget->isTargetLinux())
1509 return false;
1510
1511 if (Subtarget->is64Bit()) {
1512 // %fs:0x28, unless we're using a Kernel code model, in which case it's %gs:
1513 Offset = 0x28;
1514 if (getTargetMachine().getCodeModel() == CodeModel::Kernel)
1515 AddressSpace = 256;
1516 else
1517 AddressSpace = 257;
1518 } else {
1519 // %gs:0x14 on i386
1520 Offset = 0x14;
1521 AddressSpace = 256;
1522 }
1523 return true;
1524}
1525
Chris Lattner2b02a442007-02-25 08:29:00 +00001526//===----------------------------------------------------------------------===//
1527// Return Value Calling Convention Implementation
1528//===----------------------------------------------------------------------===//
1529
Chris Lattner59ed56b2007-02-28 04:55:35 +00001530#include "X86GenCallingConv.inc"
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001531
Michael J. Spencerec38de22010-10-10 22:04:20 +00001532bool
Eric Christopher471e4222011-06-08 23:55:35 +00001533X86TargetLowering::CanLowerReturn(CallingConv::ID CallConv,
Craig Topper0fbf3642012-04-23 03:28:34 +00001534 MachineFunction &MF, bool isVarArg,
Dan Gohman84023e02010-07-10 09:00:22 +00001535 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9af33c2010-07-06 22:19:37 +00001536 LLVMContext &Context) const {
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +00001537 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00001538 CCState CCInfo(CallConv, isVarArg, MF, getTargetMachine(),
Dan Gohmanc9af33c2010-07-06 22:19:37 +00001539 RVLocs, Context);
Dan Gohman84023e02010-07-10 09:00:22 +00001540 return CCInfo.CheckReturn(Outs, RetCC_X86);
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +00001541}
1542
Dan Gohman98ca4f22009-08-05 01:29:28 +00001543SDValue
1544X86TargetLowering::LowerReturn(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001545 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001546 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001547 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohmand858e902010-04-17 15:26:15 +00001548 DebugLoc dl, SelectionDAG &DAG) const {
Dan Gohman1e93df62010-04-17 14:41:14 +00001549 MachineFunction &MF = DAG.getMachineFunction();
1550 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
Scott Michelfdc40a02009-02-17 22:15:04 +00001551
Chris Lattner9774c912007-02-27 05:28:59 +00001552 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00001553 CCState CCInfo(CallConv, isVarArg, MF, getTargetMachine(),
Dan Gohman98ca4f22009-08-05 01:29:28 +00001554 RVLocs, *DAG.getContext());
1555 CCInfo.AnalyzeReturn(Outs, RetCC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001556
Evan Chengdcea1632010-02-04 02:40:39 +00001557 // Add the regs to the liveout set for the function.
1558 MachineRegisterInfo &MRI = DAG.getMachineFunction().getRegInfo();
1559 for (unsigned i = 0; i != RVLocs.size(); ++i)
1560 if (RVLocs[i].isRegLoc() && !MRI.isLiveOut(RVLocs[i].getLocReg()))
1561 MRI.addLiveOut(RVLocs[i].getLocReg());
Scott Michelfdc40a02009-02-17 22:15:04 +00001562
Dan Gohman475871a2008-07-27 21:46:04 +00001563 SDValue Flag;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001564
Dan Gohman475871a2008-07-27 21:46:04 +00001565 SmallVector<SDValue, 6> RetOps;
Chris Lattner447ff682008-03-11 03:23:40 +00001566 RetOps.push_back(Chain); // Operand #0 = Chain (updated below)
1567 // Operand #1 = Bytes To Pop
Dan Gohman1e93df62010-04-17 14:41:14 +00001568 RetOps.push_back(DAG.getTargetConstant(FuncInfo->getBytesToPopOnReturn(),
1569 MVT::i16));
Scott Michelfdc40a02009-02-17 22:15:04 +00001570
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001571 // Copy the result values into the output registers.
Chris Lattner8e6da152008-03-10 21:08:41 +00001572 for (unsigned i = 0; i != RVLocs.size(); ++i) {
1573 CCValAssign &VA = RVLocs[i];
1574 assert(VA.isRegLoc() && "Can only return in registers!");
Dan Gohmanc9403652010-07-07 15:54:55 +00001575 SDValue ValToCopy = OutVals[i];
Dale Johannesenc76d23f2010-07-23 00:30:35 +00001576 EVT ValVT = ValToCopy.getValueType();
1577
Jakob Stoklund Olesenee66b412012-05-31 17:28:20 +00001578 // Promote values to the appropriate types
1579 if (VA.getLocInfo() == CCValAssign::SExt)
1580 ValToCopy = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), ValToCopy);
1581 else if (VA.getLocInfo() == CCValAssign::ZExt)
1582 ValToCopy = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), ValToCopy);
1583 else if (VA.getLocInfo() == CCValAssign::AExt)
1584 ValToCopy = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), ValToCopy);
1585 else if (VA.getLocInfo() == CCValAssign::BCvt)
1586 ValToCopy = DAG.getNode(ISD::BITCAST, dl, VA.getLocVT(), ValToCopy);
1587
Dale Johannesenc4510512010-09-24 19:05:48 +00001588 // If this is x86-64, and we disabled SSE, we can't return FP values,
1589 // or SSE or MMX vectors.
1590 if ((ValVT == MVT::f32 || ValVT == MVT::f64 ||
1591 VA.getLocReg() == X86::XMM0 || VA.getLocReg() == X86::XMM1) &&
Craig Topper1accb7e2012-01-10 06:54:16 +00001592 (Subtarget->is64Bit() && !Subtarget->hasSSE1())) {
Dale Johannesenc76d23f2010-07-23 00:30:35 +00001593 report_fatal_error("SSE register return with SSE disabled");
1594 }
1595 // Likewise we can't return F64 values with SSE1 only. gcc does so, but
1596 // llvm-gcc has never done it right and no one has noticed, so this
1597 // should be OK for now.
1598 if (ValVT == MVT::f64 &&
Craig Topper1accb7e2012-01-10 06:54:16 +00001599 (Subtarget->is64Bit() && !Subtarget->hasSSE2()))
Dale Johannesenc76d23f2010-07-23 00:30:35 +00001600 report_fatal_error("SSE2 register return with SSE2 disabled");
Scott Michelfdc40a02009-02-17 22:15:04 +00001601
Chris Lattner447ff682008-03-11 03:23:40 +00001602 // Returns in ST0/ST1 are handled specially: these are pushed as operands to
1603 // the RET instruction and handled by the FP Stackifier.
Dan Gohman37eed792009-02-04 17:28:58 +00001604 if (VA.getLocReg() == X86::ST0 ||
1605 VA.getLocReg() == X86::ST1) {
Chris Lattner447ff682008-03-11 03:23:40 +00001606 // If this is a copy from an xmm register to ST(0), use an FPExtend to
1607 // change the value to the FP stack register class.
Dan Gohman37eed792009-02-04 17:28:58 +00001608 if (isScalarFPTypeInSSEReg(VA.getValVT()))
Owen Anderson825b72b2009-08-11 20:47:22 +00001609 ValToCopy = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f80, ValToCopy);
Chris Lattner447ff682008-03-11 03:23:40 +00001610 RetOps.push_back(ValToCopy);
1611 // Don't emit a copytoreg.
1612 continue;
1613 }
Dale Johannesena68f9012008-06-24 22:01:44 +00001614
Evan Cheng242b38b2009-02-23 09:03:22 +00001615 // 64-bit vector (MMX) values are returned in XMM0 / XMM1 except for v1i64
1616 // which is returned in RAX / RDX.
Evan Cheng6140a8b2009-02-22 08:05:12 +00001617 if (Subtarget->is64Bit()) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00001618 if (ValVT == MVT::x86mmx) {
Chris Lattner97a2a562010-08-26 05:24:29 +00001619 if (VA.getLocReg() == X86::XMM0 || VA.getLocReg() == X86::XMM1) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001620 ValToCopy = DAG.getNode(ISD::BITCAST, dl, MVT::i64, ValToCopy);
Eric Christopher90eb4022010-07-22 00:26:08 +00001621 ValToCopy = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i64,
1622 ValToCopy);
Chris Lattner97a2a562010-08-26 05:24:29 +00001623 // If we don't have SSE2 available, convert to v4f32 so the generated
1624 // register is legal.
Craig Topper1accb7e2012-01-10 06:54:16 +00001625 if (!Subtarget->hasSSE2())
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001626 ValToCopy = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32,ValToCopy);
Chris Lattner97a2a562010-08-26 05:24:29 +00001627 }
Evan Cheng242b38b2009-02-23 09:03:22 +00001628 }
Evan Cheng6140a8b2009-02-22 08:05:12 +00001629 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00001630
Dale Johannesendd64c412009-02-04 00:33:20 +00001631 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), ValToCopy, Flag);
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001632 Flag = Chain.getValue(1);
1633 }
Dan Gohman61a92132008-04-21 23:59:07 +00001634
1635 // The x86-64 ABI for returning structs by value requires that we copy
1636 // the sret argument into %rax for the return. We saved the argument into
1637 // a virtual register in the entry block, so now we copy the value out
1638 // and into %rax.
1639 if (Subtarget->is64Bit() &&
1640 DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
1641 MachineFunction &MF = DAG.getMachineFunction();
1642 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1643 unsigned Reg = FuncInfo->getSRetReturnReg();
Michael J. Spencerec38de22010-10-10 22:04:20 +00001644 assert(Reg &&
Zhongxing Xuc2798a12010-05-26 08:10:02 +00001645 "SRetReturnReg should have been set in LowerFormalArguments().");
Dale Johannesendd64c412009-02-04 00:33:20 +00001646 SDValue Val = DAG.getCopyFromReg(Chain, dl, Reg, getPointerTy());
Dan Gohman61a92132008-04-21 23:59:07 +00001647
Dale Johannesendd64c412009-02-04 00:33:20 +00001648 Chain = DAG.getCopyToReg(Chain, dl, X86::RAX, Val, Flag);
Dan Gohman61a92132008-04-21 23:59:07 +00001649 Flag = Chain.getValue(1);
Dan Gohman00326812009-10-12 16:36:12 +00001650
1651 // RAX now acts like a return value.
Evan Chengdcea1632010-02-04 02:40:39 +00001652 MRI.addLiveOut(X86::RAX);
Dan Gohman61a92132008-04-21 23:59:07 +00001653 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001654
Chris Lattner447ff682008-03-11 03:23:40 +00001655 RetOps[0] = Chain; // Update chain.
1656
1657 // Add the flag if we have it.
Gabor Greifba36cb52008-08-28 21:40:38 +00001658 if (Flag.getNode())
Chris Lattner447ff682008-03-11 03:23:40 +00001659 RetOps.push_back(Flag);
Scott Michelfdc40a02009-02-17 22:15:04 +00001660
1661 return DAG.getNode(X86ISD::RET_FLAG, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001662 MVT::Other, &RetOps[0], RetOps.size());
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001663}
1664
Evan Chengbf010eb2012-04-10 01:51:00 +00001665bool X86TargetLowering::isUsedByReturnOnly(SDNode *N, SDValue &Chain) const {
Evan Cheng3d2125c2010-11-30 23:55:39 +00001666 if (N->getNumValues() != 1)
1667 return false;
1668 if (!N->hasNUsesOfValue(1, 0))
1669 return false;
1670
Evan Chengbf010eb2012-04-10 01:51:00 +00001671 SDValue TCChain = Chain;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001672 SDNode *Copy = *N->use_begin();
Chad Rosierc8d7eea2012-03-05 19:27:12 +00001673 if (Copy->getOpcode() == ISD::CopyToReg) {
1674 // If the copy has a glue operand, we conservatively assume it isn't safe to
1675 // perform a tail call.
1676 if (Copy->getOperand(Copy->getNumOperands()-1).getValueType() == MVT::Glue)
1677 return false;
Evan Chengbf010eb2012-04-10 01:51:00 +00001678 TCChain = Copy->getOperand(0);
Chad Rosierc8d7eea2012-03-05 19:27:12 +00001679 } else if (Copy->getOpcode() != ISD::FP_EXTEND)
Chad Rosier74bab7f2012-03-02 02:50:46 +00001680 return false;
1681
Evan Cheng1bf891a2010-12-01 22:59:46 +00001682 bool HasRet = false;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001683 for (SDNode::use_iterator UI = Copy->use_begin(), UE = Copy->use_end();
Evan Cheng1bf891a2010-12-01 22:59:46 +00001684 UI != UE; ++UI) {
Evan Cheng3d2125c2010-11-30 23:55:39 +00001685 if (UI->getOpcode() != X86ISD::RET_FLAG)
1686 return false;
Evan Cheng1bf891a2010-12-01 22:59:46 +00001687 HasRet = true;
1688 }
Evan Cheng3d2125c2010-11-30 23:55:39 +00001689
Evan Chengbf010eb2012-04-10 01:51:00 +00001690 if (!HasRet)
1691 return false;
1692
1693 Chain = TCChain;
1694 return true;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001695}
1696
Patrik Hagglunde5c65912012-12-19 12:02:25 +00001697MVT
1698X86TargetLowering::getTypeForExtArgOrReturn(MVT VT,
Cameron Zwarich44579682011-03-17 14:21:56 +00001699 ISD::NodeType ExtendKind) const {
Cameron Zwarich7bbf0ee2011-03-17 14:53:37 +00001700 MVT ReturnMVT;
Cameron Zwarichebe81732011-03-16 22:20:18 +00001701 // TODO: Is this also valid on 32-bit?
1702 if (Subtarget->is64Bit() && VT == MVT::i1 && ExtendKind == ISD::ZERO_EXTEND)
Cameron Zwarich7bbf0ee2011-03-17 14:53:37 +00001703 ReturnMVT = MVT::i8;
1704 else
1705 ReturnMVT = MVT::i32;
1706
Patrik Hagglunde5c65912012-12-19 12:02:25 +00001707 MVT MinVT = getRegisterType(ReturnMVT);
Cameron Zwarich7bbf0ee2011-03-17 14:53:37 +00001708 return VT.bitsLT(MinVT) ? MinVT : VT;
Cameron Zwarichebe81732011-03-16 22:20:18 +00001709}
1710
Dan Gohman98ca4f22009-08-05 01:29:28 +00001711/// LowerCallResult - Lower the result values of a call into the
1712/// appropriate copies out of appropriate physical registers.
1713///
1714SDValue
1715X86TargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001716 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001717 const SmallVectorImpl<ISD::InputArg> &Ins,
1718 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001719 SmallVectorImpl<SDValue> &InVals) const {
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001720
Chris Lattnere32bbf62007-02-28 07:09:55 +00001721 // Assign locations to each value returned by this call.
Chris Lattner9774c912007-02-27 05:28:59 +00001722 SmallVector<CCValAssign, 16> RVLocs;
Torok Edwin3f142c32009-02-01 18:15:56 +00001723 bool Is64Bit = Subtarget->is64Bit();
Eric Christopher471e4222011-06-08 23:55:35 +00001724 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
Craig Topper0fbf3642012-04-23 03:28:34 +00001725 getTargetMachine(), RVLocs, *DAG.getContext());
Dan Gohman98ca4f22009-08-05 01:29:28 +00001726 CCInfo.AnalyzeCallResult(Ins, RetCC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001727
Chris Lattner3085e152007-02-25 08:59:22 +00001728 // Copy all of the result registers out of their specified physreg.
Chris Lattner8e6da152008-03-10 21:08:41 +00001729 for (unsigned i = 0; i != RVLocs.size(); ++i) {
Dan Gohman37eed792009-02-04 17:28:58 +00001730 CCValAssign &VA = RVLocs[i];
Owen Andersone50ed302009-08-10 22:56:29 +00001731 EVT CopyVT = VA.getValVT();
Scott Michelfdc40a02009-02-17 22:15:04 +00001732
Torok Edwin3f142c32009-02-01 18:15:56 +00001733 // If this is x86-64, and we disabled SSE, we can't return FP values
Owen Anderson825b72b2009-08-11 20:47:22 +00001734 if ((CopyVT == MVT::f32 || CopyVT == MVT::f64) &&
Craig Topper1accb7e2012-01-10 06:54:16 +00001735 ((Is64Bit || Ins[i].Flags.isInReg()) && !Subtarget->hasSSE1())) {
Chris Lattner75361b62010-04-07 22:58:41 +00001736 report_fatal_error("SSE register return with SSE disabled");
Torok Edwin3f142c32009-02-01 18:15:56 +00001737 }
1738
Evan Cheng79fb3b42009-02-20 20:43:02 +00001739 SDValue Val;
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001740
1741 // If this is a call to a function that returns an fp value on the floating
Sylvestre Ledruc8e41c52012-07-23 08:51:15 +00001742 // point stack, we must guarantee the value is popped from the stack, so
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001743 // a CopyFromReg is not good enough - the copy instruction may be eliminated
Jakob Stoklund Olesen9bbe4d62011-06-28 18:32:28 +00001744 // if the return value is not used. We use the FpPOP_RETVAL instruction
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001745 // instead.
1746 if (VA.getLocReg() == X86::ST0 || VA.getLocReg() == X86::ST1) {
1747 // If we prefer to use the value in xmm registers, copy it out as f80 and
1748 // use a truncate to move it from fp stack reg to xmm reg.
1749 if (isScalarFPTypeInSSEReg(VA.getValVT())) CopyVT = MVT::f80;
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001750 SDValue Ops[] = { Chain, InFlag };
Jakob Stoklund Olesen9bbe4d62011-06-28 18:32:28 +00001751 Chain = SDValue(DAG.getMachineNode(X86::FpPOP_RETVAL, dl, CopyVT,
1752 MVT::Other, MVT::Glue, Ops, 2), 1);
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001753 Val = Chain.getValue(0);
1754
1755 // Round the f80 to the right size, which also moves it to the appropriate
1756 // xmm register.
1757 if (CopyVT != VA.getValVT())
1758 Val = DAG.getNode(ISD::FP_ROUND, dl, VA.getValVT(), Val,
1759 // This truncation won't change the value.
1760 DAG.getIntPtrConstant(1));
Evan Cheng79fb3b42009-02-20 20:43:02 +00001761 } else {
1762 Chain = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(),
1763 CopyVT, InFlag).getValue(1);
1764 Val = Chain.getValue(0);
1765 }
Chris Lattner8e6da152008-03-10 21:08:41 +00001766 InFlag = Chain.getValue(2);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001767 InVals.push_back(Val);
Chris Lattner3085e152007-02-25 08:59:22 +00001768 }
Duncan Sands4bdcb612008-07-02 17:40:58 +00001769
Dan Gohman98ca4f22009-08-05 01:29:28 +00001770 return Chain;
Chris Lattner2b02a442007-02-25 08:29:00 +00001771}
1772
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001773//===----------------------------------------------------------------------===//
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001774// C & StdCall & Fast Calling Convention implementation
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001775//===----------------------------------------------------------------------===//
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001776// StdCall calling convention seems to be standard for many Windows' API
1777// routines and around. It differs from C calling convention just a little:
1778// callee should clean up the stack, not caller. Symbols should be also
1779// decorated in some fancy way :) It doesn't support any vector arguments.
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001780// For info on fast calling convention see Fast Calling Convention (tail call)
1781// implementation LowerX86_32FastCCCallTo.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001782
Dan Gohman98ca4f22009-08-05 01:29:28 +00001783/// CallIsStructReturn - Determines whether a call uses struct return
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001784/// semantics.
Rafael Espindola1cee7102012-07-25 13:41:10 +00001785enum StructReturnType {
1786 NotStructReturn,
1787 RegStructReturn,
1788 StackStructReturn
1789};
1790static StructReturnType
1791callIsStructReturn(const SmallVectorImpl<ISD::OutputArg> &Outs) {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001792 if (Outs.empty())
Rafael Espindola1cee7102012-07-25 13:41:10 +00001793 return NotStructReturn;
Duncan Sands276dcbd2008-03-21 09:14:45 +00001794
Rafael Espindola1cee7102012-07-25 13:41:10 +00001795 const ISD::ArgFlagsTy &Flags = Outs[0].Flags;
1796 if (!Flags.isSRet())
1797 return NotStructReturn;
1798 if (Flags.isInReg())
1799 return RegStructReturn;
1800 return StackStructReturn;
Gordon Henriksen86737662008-01-05 16:56:59 +00001801}
1802
Dan Gohman7e77b0f2009-08-01 19:14:37 +00001803/// ArgsAreStructReturn - Determines whether a function uses struct
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001804/// return semantics.
Rafael Espindola1cee7102012-07-25 13:41:10 +00001805static StructReturnType
1806argsAreStructReturn(const SmallVectorImpl<ISD::InputArg> &Ins) {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001807 if (Ins.empty())
Rafael Espindola1cee7102012-07-25 13:41:10 +00001808 return NotStructReturn;
Duncan Sands276dcbd2008-03-21 09:14:45 +00001809
Rafael Espindola1cee7102012-07-25 13:41:10 +00001810 const ISD::ArgFlagsTy &Flags = Ins[0].Flags;
1811 if (!Flags.isSRet())
1812 return NotStructReturn;
1813 if (Flags.isInReg())
1814 return RegStructReturn;
1815 return StackStructReturn;
Gordon Henriksen86737662008-01-05 16:56:59 +00001816}
1817
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001818/// CreateCopyOfByValArgument - Make a copy of an aggregate at address specified
1819/// by "Src" to address "Dst" with size and alignment information specified by
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001820/// the specific parameter attribute. The copy will be passed as a byval
1821/// function parameter.
Scott Michelfdc40a02009-02-17 22:15:04 +00001822static SDValue
Dan Gohman475871a2008-07-27 21:46:04 +00001823CreateCopyOfByValArgument(SDValue Src, SDValue Dst, SDValue Chain,
Dale Johannesendd64c412009-02-04 00:33:20 +00001824 ISD::ArgFlagsTy Flags, SelectionDAG &DAG,
1825 DebugLoc dl) {
Chris Lattnere72f2022010-09-21 05:40:29 +00001826 SDValue SizeNode = DAG.getConstant(Flags.getByValSize(), MVT::i32);
Michael J. Spencerec38de22010-10-10 22:04:20 +00001827
Dale Johannesendd64c412009-02-04 00:33:20 +00001828 return DAG.getMemcpy(Chain, dl, Dst, Src, SizeNode, Flags.getByValAlign(),
Stuart Hastings03d58262011-03-10 00:25:53 +00001829 /*isVolatile*/false, /*AlwaysInline=*/true,
Chris Lattnerfc448ff2010-09-21 18:51:21 +00001830 MachinePointerInfo(), MachinePointerInfo());
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001831}
1832
Chris Lattner29689432010-03-11 00:22:57 +00001833/// IsTailCallConvention - Return true if the calling convention is one that
1834/// supports tail call optimization.
1835static bool IsTailCallConvention(CallingConv::ID CC) {
Duncan Sandsdc7f1742012-11-16 12:36:39 +00001836 return (CC == CallingConv::Fast || CC == CallingConv::GHC ||
1837 CC == CallingConv::HiPE);
Chris Lattner29689432010-03-11 00:22:57 +00001838}
1839
Evan Cheng485fafc2011-03-21 01:19:09 +00001840bool X86TargetLowering::mayBeEmittedAsTailCall(CallInst *CI) const {
Nick Lewycky22de16d2012-01-19 00:34:10 +00001841 if (!CI->isTailCall() || getTargetMachine().Options.DisableTailCalls)
Evan Cheng485fafc2011-03-21 01:19:09 +00001842 return false;
1843
1844 CallSite CS(CI);
1845 CallingConv::ID CalleeCC = CS.getCallingConv();
1846 if (!IsTailCallConvention(CalleeCC) && CalleeCC != CallingConv::C)
1847 return false;
1848
1849 return true;
1850}
1851
Evan Cheng0c439eb2010-01-27 00:07:07 +00001852/// FuncIsMadeTailCallSafe - Return true if the function is being made into
1853/// a tailcall target by changing its ABI.
Nick Lewycky8a8d4792011-12-02 22:16:29 +00001854static bool FuncIsMadeTailCallSafe(CallingConv::ID CC,
1855 bool GuaranteedTailCallOpt) {
Chris Lattner29689432010-03-11 00:22:57 +00001856 return GuaranteedTailCallOpt && IsTailCallConvention(CC);
Evan Cheng0c439eb2010-01-27 00:07:07 +00001857}
1858
Dan Gohman98ca4f22009-08-05 01:29:28 +00001859SDValue
1860X86TargetLowering::LowerMemArgument(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001861 CallingConv::ID CallConv,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001862 const SmallVectorImpl<ISD::InputArg> &Ins,
1863 DebugLoc dl, SelectionDAG &DAG,
1864 const CCValAssign &VA,
1865 MachineFrameInfo *MFI,
Dan Gohmand858e902010-04-17 15:26:15 +00001866 unsigned i) const {
Rafael Espindola7effac52007-09-14 15:48:13 +00001867 // Create the nodes corresponding to a load from this parameter slot.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001868 ISD::ArgFlagsTy Flags = Ins[i].Flags;
Nick Lewycky8a8d4792011-12-02 22:16:29 +00001869 bool AlwaysUseMutable = FuncIsMadeTailCallSafe(CallConv,
1870 getTargetMachine().Options.GuaranteedTailCallOpt);
Duncan Sands276dcbd2008-03-21 09:14:45 +00001871 bool isImmutable = !AlwaysUseMutable && !Flags.isByVal();
Anton Korobeynikov22472762009-08-14 18:19:10 +00001872 EVT ValVT;
1873
1874 // If value is passed by pointer we have address passed instead of the value
1875 // itself.
1876 if (VA.getLocInfo() == CCValAssign::Indirect)
1877 ValVT = VA.getLocVT();
1878 else
1879 ValVT = VA.getValVT();
Evan Chenge70bb592008-01-10 02:24:25 +00001880
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001881 // FIXME: For now, all byval parameter objects are marked mutable. This can be
Scott Michelfdc40a02009-02-17 22:15:04 +00001882 // changed with more analysis.
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001883 // In case of tail call optimization mark all arguments mutable. Since they
1884 // could be overwritten by lowering of arguments in case of a tail call.
Evan Cheng90567c32010-02-02 23:58:13 +00001885 if (Flags.isByVal()) {
Evan Chengee2e0e32011-03-30 23:44:13 +00001886 unsigned Bytes = Flags.getByValSize();
1887 if (Bytes == 0) Bytes = 1; // Don't create zero-sized stack objects.
1888 int FI = MFI->CreateFixedObject(Bytes, VA.getLocMemOffset(), isImmutable);
Evan Cheng90567c32010-02-02 23:58:13 +00001889 return DAG.getFrameIndex(FI, getPointerTy());
1890 } else {
1891 int FI = MFI->CreateFixedObject(ValVT.getSizeInBits()/8,
Evan Chenged2ae132010-07-03 00:40:23 +00001892 VA.getLocMemOffset(), isImmutable);
Evan Cheng90567c32010-02-02 23:58:13 +00001893 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
1894 return DAG.getLoad(ValVT, dl, Chain, FIN,
Chris Lattnere8639032010-09-21 06:22:23 +00001895 MachinePointerInfo::getFixedStack(FI),
Pete Cooperd752e0f2011-11-08 18:42:53 +00001896 false, false, false, 0);
Evan Cheng90567c32010-02-02 23:58:13 +00001897 }
Rafael Espindola7effac52007-09-14 15:48:13 +00001898}
1899
Dan Gohman475871a2008-07-27 21:46:04 +00001900SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00001901X86TargetLowering::LowerFormalArguments(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001902 CallingConv::ID CallConv,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001903 bool isVarArg,
1904 const SmallVectorImpl<ISD::InputArg> &Ins,
1905 DebugLoc dl,
1906 SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001907 SmallVectorImpl<SDValue> &InVals)
1908 const {
Evan Cheng1bc78042006-04-26 01:20:17 +00001909 MachineFunction &MF = DAG.getMachineFunction();
Gordon Henriksen86737662008-01-05 16:56:59 +00001910 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
Scott Michelfdc40a02009-02-17 22:15:04 +00001911
Gordon Henriksen86737662008-01-05 16:56:59 +00001912 const Function* Fn = MF.getFunction();
1913 if (Fn->hasExternalLinkage() &&
1914 Subtarget->isTargetCygMing() &&
1915 Fn->getName() == "main")
1916 FuncInfo->setForceFramePointer(true);
1917
Evan Cheng1bc78042006-04-26 01:20:17 +00001918 MachineFrameInfo *MFI = MF.getFrameInfo();
Gordon Henriksen86737662008-01-05 16:56:59 +00001919 bool Is64Bit = Subtarget->is64Bit();
Eli Friedman9a2478a2012-01-20 00:05:46 +00001920 bool IsWindows = Subtarget->isTargetWindows();
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001921 bool IsWin64 = Subtarget->isTargetWin64();
Gordon Henriksenae636f82008-01-03 16:47:34 +00001922
Chris Lattner29689432010-03-11 00:22:57 +00001923 assert(!(isVarArg && IsTailCallConvention(CallConv)) &&
Duncan Sandsdc7f1742012-11-16 12:36:39 +00001924 "Var args not supported with calling convention fastcc, ghc or hipe");
Gordon Henriksenae636f82008-01-03 16:47:34 +00001925
Chris Lattner638402b2007-02-28 07:00:42 +00001926 // Assign locations to all of the incoming arguments.
Chris Lattnerf39f7712007-02-28 05:46:49 +00001927 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00001928 CCState CCInfo(CallConv, isVarArg, MF, getTargetMachine(),
Dan Gohman98ca4f22009-08-05 01:29:28 +00001929 ArgLocs, *DAG.getContext());
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00001930
1931 // Allocate shadow area for Win64
1932 if (IsWin64) {
1933 CCInfo.AllocateStack(32, 8);
1934 }
1935
Duncan Sands45907662010-10-31 13:21:44 +00001936 CCInfo.AnalyzeFormalArguments(Ins, CC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001937
Chris Lattnerf39f7712007-02-28 05:46:49 +00001938 unsigned LastVal = ~0U;
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001939 SDValue ArgValue;
Chris Lattnerf39f7712007-02-28 05:46:49 +00001940 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1941 CCValAssign &VA = ArgLocs[i];
1942 // TODO: If an arg is passed in two places (e.g. reg and stack), skip later
1943 // places.
1944 assert(VA.getValNo() != LastVal &&
1945 "Don't support value assigned to multiple locs yet");
Duncan Sands17001ce2011-10-18 12:44:00 +00001946 (void)LastVal;
Chris Lattnerf39f7712007-02-28 05:46:49 +00001947 LastVal = VA.getValNo();
Scott Michelfdc40a02009-02-17 22:15:04 +00001948
Chris Lattnerf39f7712007-02-28 05:46:49 +00001949 if (VA.isRegLoc()) {
Owen Andersone50ed302009-08-10 22:56:29 +00001950 EVT RegVT = VA.getLocVT();
Craig Topper44d23822012-02-22 05:59:10 +00001951 const TargetRegisterClass *RC;
Owen Anderson825b72b2009-08-11 20:47:22 +00001952 if (RegVT == MVT::i32)
Craig Topperc9099502012-04-20 06:31:50 +00001953 RC = &X86::GR32RegClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001954 else if (Is64Bit && RegVT == MVT::i64)
Craig Topperc9099502012-04-20 06:31:50 +00001955 RC = &X86::GR64RegClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001956 else if (RegVT == MVT::f32)
Craig Topperc9099502012-04-20 06:31:50 +00001957 RC = &X86::FR32RegClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001958 else if (RegVT == MVT::f64)
Craig Topperc9099502012-04-20 06:31:50 +00001959 RC = &X86::FR64RegClass;
Craig Topper7a9a28b2012-08-12 02:23:29 +00001960 else if (RegVT.is256BitVector())
Craig Topperc9099502012-04-20 06:31:50 +00001961 RC = &X86::VR256RegClass;
Craig Topper7a9a28b2012-08-12 02:23:29 +00001962 else if (RegVT.is128BitVector())
Craig Topperc9099502012-04-20 06:31:50 +00001963 RC = &X86::VR128RegClass;
Dale Johannesen0488fb62010-09-30 23:57:10 +00001964 else if (RegVT == MVT::x86mmx)
Craig Topperc9099502012-04-20 06:31:50 +00001965 RC = &X86::VR64RegClass;
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001966 else
Torok Edwinc23197a2009-07-14 16:55:14 +00001967 llvm_unreachable("Unknown argument type!");
Gordon Henriksenae636f82008-01-03 16:47:34 +00001968
Devang Patel68e6bee2011-02-21 23:21:26 +00001969 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001970 ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT);
Scott Michelfdc40a02009-02-17 22:15:04 +00001971
Chris Lattnerf39f7712007-02-28 05:46:49 +00001972 // If this is an 8 or 16-bit value, it is really passed promoted to 32
1973 // bits. Insert an assert[sz]ext to capture this, then truncate to the
1974 // right size.
1975 if (VA.getLocInfo() == CCValAssign::SExt)
Dale Johannesenace16102009-02-03 19:33:06 +00001976 ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue,
Chris Lattnerf39f7712007-02-28 05:46:49 +00001977 DAG.getValueType(VA.getValVT()));
1978 else if (VA.getLocInfo() == CCValAssign::ZExt)
Dale Johannesenace16102009-02-03 19:33:06 +00001979 ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue,
Chris Lattnerf39f7712007-02-28 05:46:49 +00001980 DAG.getValueType(VA.getValVT()));
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001981 else if (VA.getLocInfo() == CCValAssign::BCvt)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001982 ArgValue = DAG.getNode(ISD::BITCAST, dl, VA.getValVT(), ArgValue);
Scott Michelfdc40a02009-02-17 22:15:04 +00001983
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001984 if (VA.isExtInLoc()) {
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001985 // Handle MMX values passed in XMM regs.
1986 if (RegVT.isVector()) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00001987 ArgValue = DAG.getNode(X86ISD::MOVDQ2Q, dl, VA.getValVT(),
1988 ArgValue);
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001989 } else
1990 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
Evan Cheng44c0fd12008-04-25 20:13:28 +00001991 }
Chris Lattnerf39f7712007-02-28 05:46:49 +00001992 } else {
1993 assert(VA.isMemLoc());
Dan Gohman98ca4f22009-08-05 01:29:28 +00001994 ArgValue = LowerMemArgument(Chain, CallConv, Ins, dl, DAG, VA, MFI, i);
Evan Cheng1bc78042006-04-26 01:20:17 +00001995 }
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001996
1997 // If value is passed via pointer - do a load.
1998 if (VA.getLocInfo() == CCValAssign::Indirect)
Chris Lattner51abfe42010-09-21 06:02:19 +00001999 ArgValue = DAG.getLoad(VA.getValVT(), dl, Chain, ArgValue,
Pete Cooperd752e0f2011-11-08 18:42:53 +00002000 MachinePointerInfo(), false, false, false, 0);
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00002001
Dan Gohman98ca4f22009-08-05 01:29:28 +00002002 InVals.push_back(ArgValue);
Evan Cheng1bc78042006-04-26 01:20:17 +00002003 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00002004
Dan Gohman61a92132008-04-21 23:59:07 +00002005 // The x86-64 ABI for returning structs by value requires that we copy
2006 // the sret argument into %rax for the return. Save the argument into
2007 // a virtual register so that we can access it from the return points.
Dan Gohman7e77b0f2009-08-01 19:14:37 +00002008 if (Is64Bit && MF.getFunction()->hasStructRetAttr()) {
Dan Gohman61a92132008-04-21 23:59:07 +00002009 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
2010 unsigned Reg = FuncInfo->getSRetReturnReg();
2011 if (!Reg) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002012 Reg = MF.getRegInfo().createVirtualRegister(getRegClassFor(MVT::i64));
Dan Gohman61a92132008-04-21 23:59:07 +00002013 FuncInfo->setSRetReturnReg(Reg);
2014 }
Dan Gohman98ca4f22009-08-05 01:29:28 +00002015 SDValue Copy = DAG.getCopyToReg(DAG.getEntryNode(), dl, Reg, InVals[0]);
Owen Anderson825b72b2009-08-11 20:47:22 +00002016 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Copy, Chain);
Dan Gohman61a92132008-04-21 23:59:07 +00002017 }
2018
Chris Lattnerf39f7712007-02-28 05:46:49 +00002019 unsigned StackSize = CCInfo.getNextStackOffset();
Evan Cheng0c439eb2010-01-27 00:07:07 +00002020 // Align stack specially for tail calls.
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002021 if (FuncIsMadeTailCallSafe(CallConv,
2022 MF.getTarget().Options.GuaranteedTailCallOpt))
Gordon Henriksenae636f82008-01-03 16:47:34 +00002023 StackSize = GetAlignedArgumentStackSize(StackSize, DAG);
Evan Cheng25caf632006-05-23 21:06:34 +00002024
Evan Cheng1bc78042006-04-26 01:20:17 +00002025 // If the function takes variable number of arguments, make a frame index for
2026 // the start of the first vararg value... for expansion of llvm.va_start.
Gordon Henriksenae636f82008-01-03 16:47:34 +00002027 if (isVarArg) {
NAKAMURA Takumi3ca99432011-03-09 11:33:15 +00002028 if (Is64Bit || (CallConv != CallingConv::X86_FastCall &&
2029 CallConv != CallingConv::X86_ThisCall)) {
Jakob Stoklund Olesenb2eeed72010-07-29 17:42:27 +00002030 FuncInfo->setVarArgsFrameIndex(MFI->CreateFixedObject(1, StackSize,true));
Gordon Henriksen86737662008-01-05 16:56:59 +00002031 }
2032 if (Is64Bit) {
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002033 unsigned TotalNumIntRegs = 0, TotalNumXMMRegs = 0;
2034
2035 // FIXME: We should really autogenerate these arrays
Craig Topperc5eaae42012-03-11 07:57:25 +00002036 static const uint16_t GPR64ArgRegsWin64[] = {
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002037 X86::RCX, X86::RDX, X86::R8, X86::R9
Gordon Henriksen86737662008-01-05 16:56:59 +00002038 };
Craig Topperc5eaae42012-03-11 07:57:25 +00002039 static const uint16_t GPR64ArgRegs64Bit[] = {
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002040 X86::RDI, X86::RSI, X86::RDX, X86::RCX, X86::R8, X86::R9
2041 };
Craig Topperc5eaae42012-03-11 07:57:25 +00002042 static const uint16_t XMMArgRegs64Bit[] = {
Gordon Henriksen86737662008-01-05 16:56:59 +00002043 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
2044 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
2045 };
Craig Topperc5eaae42012-03-11 07:57:25 +00002046 const uint16_t *GPR64ArgRegs;
Anton Korobeynikove7beda12010-10-03 22:52:07 +00002047 unsigned NumXMMRegs = 0;
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002048
2049 if (IsWin64) {
Anton Korobeynikove7beda12010-10-03 22:52:07 +00002050 // The XMM registers which might contain var arg parameters are shadowed
2051 // in their paired GPR. So we only need to save the GPR to their home
2052 // slots.
2053 TotalNumIntRegs = 4;
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002054 GPR64ArgRegs = GPR64ArgRegsWin64;
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002055 } else {
2056 TotalNumIntRegs = 6; TotalNumXMMRegs = 8;
2057 GPR64ArgRegs = GPR64ArgRegs64Bit;
Anton Korobeynikove7beda12010-10-03 22:52:07 +00002058
Chad Rosier30450e82011-12-22 22:35:21 +00002059 NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs64Bit,
2060 TotalNumXMMRegs);
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002061 }
2062 unsigned NumIntRegs = CCInfo.getFirstUnallocated(GPR64ArgRegs,
2063 TotalNumIntRegs);
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002064
Bill Wendling67658342012-10-09 07:45:08 +00002065 bool NoImplicitFloatOps = Fn->getFnAttributes().
Bill Wendling034b94b2012-12-19 07:18:57 +00002066 hasAttribute(Attribute::NoImplicitFloat);
Craig Topper1accb7e2012-01-10 06:54:16 +00002067 assert(!(NumXMMRegs && !Subtarget->hasSSE1()) &&
Torok Edwin3f142c32009-02-01 18:15:56 +00002068 "SSE register cannot be used when SSE is disabled!");
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002069 assert(!(NumXMMRegs && MF.getTarget().Options.UseSoftFloat &&
2070 NoImplicitFloatOps) &&
Evan Chengc7ce29b2009-02-13 22:36:38 +00002071 "SSE register cannot be used when SSE is disabled!");
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002072 if (MF.getTarget().Options.UseSoftFloat || NoImplicitFloatOps ||
Craig Topper1accb7e2012-01-10 06:54:16 +00002073 !Subtarget->hasSSE1())
Torok Edwin3f142c32009-02-01 18:15:56 +00002074 // Kernel mode asks for SSE to be disabled, so don't push them
2075 // on the stack.
2076 TotalNumXMMRegs = 0;
Bill Wendlingf9abd7e2009-03-11 22:30:01 +00002077
Anton Korobeynikove7beda12010-10-03 22:52:07 +00002078 if (IsWin64) {
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00002079 const TargetFrameLowering &TFI = *getTargetMachine().getFrameLowering();
Cameron Esfahaniec37b002010-10-08 19:24:18 +00002080 // Get to the caller-allocated home save location. Add 8 to account
2081 // for the return address.
2082 int HomeOffset = TFI.getOffsetOfLocalArea() + 8;
Anton Korobeynikove7beda12010-10-03 22:52:07 +00002083 FuncInfo->setRegSaveFrameIndex(
Cameron Esfahaniec37b002010-10-08 19:24:18 +00002084 MFI->CreateFixedObject(1, NumIntRegs * 8 + HomeOffset, false));
NAKAMURA Takumi3ca99432011-03-09 11:33:15 +00002085 // Fixup to set vararg frame on shadow area (4 x i64).
2086 if (NumIntRegs < 4)
2087 FuncInfo->setVarArgsFrameIndex(FuncInfo->getRegSaveFrameIndex());
Anton Korobeynikove7beda12010-10-03 22:52:07 +00002088 } else {
2089 // For X86-64, if there are vararg parameters that are passed via
Chad Rosier30450e82011-12-22 22:35:21 +00002090 // registers, then we must store them to their spots on the stack so
2091 // they may be loaded by deferencing the result of va_next.
Anton Korobeynikove7beda12010-10-03 22:52:07 +00002092 FuncInfo->setVarArgsGPOffset(NumIntRegs * 8);
2093 FuncInfo->setVarArgsFPOffset(TotalNumIntRegs * 8 + NumXMMRegs * 16);
2094 FuncInfo->setRegSaveFrameIndex(
2095 MFI->CreateStackObject(TotalNumIntRegs * 8 + TotalNumXMMRegs * 16, 16,
Dan Gohman1e93df62010-04-17 14:41:14 +00002096 false));
Anton Korobeynikove7beda12010-10-03 22:52:07 +00002097 }
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002098
Gordon Henriksen86737662008-01-05 16:56:59 +00002099 // Store the integer parameter registers.
Dan Gohman475871a2008-07-27 21:46:04 +00002100 SmallVector<SDValue, 8> MemOps;
Dan Gohman1e93df62010-04-17 14:41:14 +00002101 SDValue RSFIN = DAG.getFrameIndex(FuncInfo->getRegSaveFrameIndex(),
2102 getPointerTy());
2103 unsigned Offset = FuncInfo->getVarArgsGPOffset();
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002104 for (; NumIntRegs != TotalNumIntRegs; ++NumIntRegs) {
Dan Gohmand6708ea2009-08-15 01:38:56 +00002105 SDValue FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), RSFIN,
2106 DAG.getIntPtrConstant(Offset));
Bob Wilson998e1252009-04-20 18:36:57 +00002107 unsigned VReg = MF.addLiveIn(GPR64ArgRegs[NumIntRegs],
Craig Topperc9099502012-04-20 06:31:50 +00002108 &X86::GR64RegClass);
Owen Anderson825b72b2009-08-11 20:47:22 +00002109 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i64);
Dan Gohman475871a2008-07-27 21:46:04 +00002110 SDValue Store =
Dale Johannesenace16102009-02-03 19:33:06 +00002111 DAG.getStore(Val.getValue(1), dl, Val, FIN,
Chris Lattnere8639032010-09-21 06:22:23 +00002112 MachinePointerInfo::getFixedStack(
2113 FuncInfo->getRegSaveFrameIndex(), Offset),
2114 false, false, 0);
Gordon Henriksen86737662008-01-05 16:56:59 +00002115 MemOps.push_back(Store);
Dan Gohmand6708ea2009-08-15 01:38:56 +00002116 Offset += 8;
Gordon Henriksen86737662008-01-05 16:56:59 +00002117 }
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002118
Dan Gohmanface41a2009-08-16 21:24:25 +00002119 if (TotalNumXMMRegs != 0 && NumXMMRegs != TotalNumXMMRegs) {
2120 // Now store the XMM (fp + vector) parameter registers.
2121 SmallVector<SDValue, 11> SaveXMMOps;
2122 SaveXMMOps.push_back(Chain);
Dan Gohmand6708ea2009-08-15 01:38:56 +00002123
Craig Topperc9099502012-04-20 06:31:50 +00002124 unsigned AL = MF.addLiveIn(X86::AL, &X86::GR8RegClass);
Dan Gohmanface41a2009-08-16 21:24:25 +00002125 SDValue ALVal = DAG.getCopyFromReg(DAG.getEntryNode(), dl, AL, MVT::i8);
2126 SaveXMMOps.push_back(ALVal);
Dan Gohmand6708ea2009-08-15 01:38:56 +00002127
Dan Gohman1e93df62010-04-17 14:41:14 +00002128 SaveXMMOps.push_back(DAG.getIntPtrConstant(
2129 FuncInfo->getRegSaveFrameIndex()));
2130 SaveXMMOps.push_back(DAG.getIntPtrConstant(
2131 FuncInfo->getVarArgsFPOffset()));
Dan Gohmand6708ea2009-08-15 01:38:56 +00002132
Dan Gohmanface41a2009-08-16 21:24:25 +00002133 for (; NumXMMRegs != TotalNumXMMRegs; ++NumXMMRegs) {
Anton Korobeynikove7beda12010-10-03 22:52:07 +00002134 unsigned VReg = MF.addLiveIn(XMMArgRegs64Bit[NumXMMRegs],
Craig Topperc9099502012-04-20 06:31:50 +00002135 &X86::VR128RegClass);
Dan Gohmanface41a2009-08-16 21:24:25 +00002136 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::v4f32);
2137 SaveXMMOps.push_back(Val);
2138 }
2139 MemOps.push_back(DAG.getNode(X86ISD::VASTART_SAVE_XMM_REGS, dl,
2140 MVT::Other,
2141 &SaveXMMOps[0], SaveXMMOps.size()));
Gordon Henriksen86737662008-01-05 16:56:59 +00002142 }
Dan Gohmanface41a2009-08-16 21:24:25 +00002143
2144 if (!MemOps.empty())
2145 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
2146 &MemOps[0], MemOps.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00002147 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00002148 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002149
Gordon Henriksen86737662008-01-05 16:56:59 +00002150 // Some CCs need callee pop.
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002151 if (X86::isCalleePop(CallConv, Is64Bit, isVarArg,
2152 MF.getTarget().Options.GuaranteedTailCallOpt)) {
Dan Gohman1e93df62010-04-17 14:41:14 +00002153 FuncInfo->setBytesToPopOnReturn(StackSize); // Callee pops everything.
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00002154 } else {
Dan Gohman1e93df62010-04-17 14:41:14 +00002155 FuncInfo->setBytesToPopOnReturn(0); // Callee pops nothing.
Chris Lattnerf39f7712007-02-28 05:46:49 +00002156 // If this is an sret function, the return should pop the hidden pointer.
Eli Friedman9a2478a2012-01-20 00:05:46 +00002157 if (!Is64Bit && !IsTailCallConvention(CallConv) && !IsWindows &&
Rafael Espindola1cee7102012-07-25 13:41:10 +00002158 argsAreStructReturn(Ins) == StackStructReturn)
Dan Gohman1e93df62010-04-17 14:41:14 +00002159 FuncInfo->setBytesToPopOnReturn(4);
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00002160 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00002161
Gordon Henriksen86737662008-01-05 16:56:59 +00002162 if (!Is64Bit) {
Dan Gohman1e93df62010-04-17 14:41:14 +00002163 // RegSaveFrameIndex is X86-64 only.
2164 FuncInfo->setRegSaveFrameIndex(0xAAAAAAA);
Anton Korobeynikovded05e32010-05-16 09:08:45 +00002165 if (CallConv == CallingConv::X86_FastCall ||
2166 CallConv == CallingConv::X86_ThisCall)
Dan Gohman1e93df62010-04-17 14:41:14 +00002167 // fastcc functions can't have varargs.
2168 FuncInfo->setVarArgsFrameIndex(0xAAAAAAA);
Gordon Henriksen86737662008-01-05 16:56:59 +00002169 }
Evan Cheng25caf632006-05-23 21:06:34 +00002170
Rafael Espindola76927d752011-08-30 19:39:58 +00002171 FuncInfo->setArgumentStackSize(StackSize);
2172
Dan Gohman98ca4f22009-08-05 01:29:28 +00002173 return Chain;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002174}
2175
Dan Gohman475871a2008-07-27 21:46:04 +00002176SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00002177X86TargetLowering::LowerMemOpCallTo(SDValue Chain,
2178 SDValue StackPtr, SDValue Arg,
2179 DebugLoc dl, SelectionDAG &DAG,
Evan Chengdffbd832008-01-10 00:09:10 +00002180 const CCValAssign &VA,
Dan Gohmand858e902010-04-17 15:26:15 +00002181 ISD::ArgFlagsTy Flags) const {
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00002182 unsigned LocMemOffset = VA.getLocMemOffset();
Dan Gohman475871a2008-07-27 21:46:04 +00002183 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset);
Dale Johannesenace16102009-02-03 19:33:06 +00002184 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, PtrOff);
Chris Lattnerfc448ff2010-09-21 18:51:21 +00002185 if (Flags.isByVal())
Dale Johannesendd64c412009-02-04 00:33:20 +00002186 return CreateCopyOfByValArgument(Arg, PtrOff, Chain, Flags, DAG, dl);
Chris Lattnerfc448ff2010-09-21 18:51:21 +00002187
2188 return DAG.getStore(Chain, dl, Arg, PtrOff,
2189 MachinePointerInfo::getStack(LocMemOffset),
David Greene67c9d422010-02-15 16:53:33 +00002190 false, false, 0);
Evan Chengdffbd832008-01-10 00:09:10 +00002191}
2192
Bill Wendling64e87322009-01-16 19:25:27 +00002193/// EmitTailCallLoadRetAddr - Emit a load of return address if tail call
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002194/// optimization is performed and it is required.
Scott Michelfdc40a02009-02-17 22:15:04 +00002195SDValue
2196X86TargetLowering::EmitTailCallLoadRetAddr(SelectionDAG &DAG,
Evan Chengddc419c2010-01-26 19:04:47 +00002197 SDValue &OutRetAddr, SDValue Chain,
2198 bool IsTailCall, bool Is64Bit,
Dan Gohmand858e902010-04-17 15:26:15 +00002199 int FPDiff, DebugLoc dl) const {
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002200 // Adjust the Return address stack slot.
Owen Andersone50ed302009-08-10 22:56:29 +00002201 EVT VT = getPointerTy();
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002202 OutRetAddr = getReturnAddressFrameIndex(DAG);
Bill Wendling64e87322009-01-16 19:25:27 +00002203
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002204 // Load the "old" Return address.
Chris Lattner51abfe42010-09-21 06:02:19 +00002205 OutRetAddr = DAG.getLoad(VT, dl, Chain, OutRetAddr, MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002206 false, false, false, 0);
Gabor Greifba36cb52008-08-28 21:40:38 +00002207 return SDValue(OutRetAddr.getNode(), 1);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002208}
2209
Chris Lattner7a2bdde2011-04-15 05:18:47 +00002210/// EmitTailCallStoreRetAddr - Emit a store of the return address if tail call
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002211/// optimization is performed and it is required (FPDiff!=0).
Scott Michelfdc40a02009-02-17 22:15:04 +00002212static SDValue
2213EmitTailCallStoreRetAddr(SelectionDAG & DAG, MachineFunction &MF,
Michael Liaoaa3c2c02012-10-25 06:29:14 +00002214 SDValue Chain, SDValue RetAddrFrIdx, EVT PtrVT,
2215 unsigned SlotSize, int FPDiff, DebugLoc dl) {
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002216 // Store the return address to the appropriate stack slot.
2217 if (!FPDiff) return Chain;
2218 // Calculate the new stack slot for the return address.
Scott Michelfdc40a02009-02-17 22:15:04 +00002219 int NewReturnAddrFI =
Evan Chenged2ae132010-07-03 00:40:23 +00002220 MF.getFrameInfo()->CreateFixedObject(SlotSize, FPDiff-SlotSize, false);
Michael Liaoaa3c2c02012-10-25 06:29:14 +00002221 SDValue NewRetAddrFrIdx = DAG.getFrameIndex(NewReturnAddrFI, PtrVT);
Scott Michelfdc40a02009-02-17 22:15:04 +00002222 Chain = DAG.getStore(Chain, dl, RetAddrFrIdx, NewRetAddrFrIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00002223 MachinePointerInfo::getFixedStack(NewReturnAddrFI),
David Greene67c9d422010-02-15 16:53:33 +00002224 false, false, 0);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002225 return Chain;
2226}
2227
Dan Gohman98ca4f22009-08-05 01:29:28 +00002228SDValue
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00002229X86TargetLowering::LowerCall(TargetLowering::CallLoweringInfo &CLI,
Dan Gohmand858e902010-04-17 15:26:15 +00002230 SmallVectorImpl<SDValue> &InVals) const {
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00002231 SelectionDAG &DAG = CLI.DAG;
2232 DebugLoc &dl = CLI.DL;
2233 SmallVector<ISD::OutputArg, 32> &Outs = CLI.Outs;
2234 SmallVector<SDValue, 32> &OutVals = CLI.OutVals;
2235 SmallVector<ISD::InputArg, 32> &Ins = CLI.Ins;
2236 SDValue Chain = CLI.Chain;
2237 SDValue Callee = CLI.Callee;
2238 CallingConv::ID CallConv = CLI.CallConv;
2239 bool &isTailCall = CLI.IsTailCall;
2240 bool isVarArg = CLI.IsVarArg;
2241
Dan Gohman98ca4f22009-08-05 01:29:28 +00002242 MachineFunction &MF = DAG.getMachineFunction();
2243 bool Is64Bit = Subtarget->is64Bit();
NAKAMURA Takumifb840c92011-02-05 15:11:13 +00002244 bool IsWin64 = Subtarget->isTargetWin64();
Eli Friedman9a2478a2012-01-20 00:05:46 +00002245 bool IsWindows = Subtarget->isTargetWindows();
Rafael Espindola1cee7102012-07-25 13:41:10 +00002246 StructReturnType SR = callIsStructReturn(Outs);
Evan Cheng5f941932010-02-05 02:21:12 +00002247 bool IsSibcall = false;
Dan Gohman98ca4f22009-08-05 01:29:28 +00002248
Nick Lewycky22de16d2012-01-19 00:34:10 +00002249 if (MF.getTarget().Options.DisableTailCalls)
2250 isTailCall = false;
2251
Evan Cheng5f941932010-02-05 02:21:12 +00002252 if (isTailCall) {
Evan Cheng0c439eb2010-01-27 00:07:07 +00002253 // Check if it's really possible to do a tail call.
Evan Chenga375d472010-03-15 18:54:48 +00002254 isTailCall = IsEligibleForTailCallOptimization(Callee, CallConv,
Rafael Espindola1cee7102012-07-25 13:41:10 +00002255 isVarArg, SR != NotStructReturn,
Evan Chengb1cacc72012-09-25 05:32:34 +00002256 MF.getFunction()->hasStructRetAttr(), CLI.RetTy,
Rafael Espindola1cee7102012-07-25 13:41:10 +00002257 Outs, OutVals, Ins, DAG);
Evan Chengf22f9b32010-02-06 03:28:46 +00002258
2259 // Sibcalls are automatically detected tailcalls which do not require
2260 // ABI changes.
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002261 if (!MF.getTarget().Options.GuaranteedTailCallOpt && isTailCall)
Evan Cheng5f941932010-02-05 02:21:12 +00002262 IsSibcall = true;
Evan Chengf22f9b32010-02-06 03:28:46 +00002263
2264 if (isTailCall)
2265 ++NumTailCalls;
Evan Cheng5f941932010-02-05 02:21:12 +00002266 }
Evan Cheng0c439eb2010-01-27 00:07:07 +00002267
Chris Lattner29689432010-03-11 00:22:57 +00002268 assert(!(isVarArg && IsTailCallConvention(CallConv)) &&
Duncan Sandsdc7f1742012-11-16 12:36:39 +00002269 "Var args not supported with calling convention fastcc, ghc or hipe");
Gordon Henriksenae636f82008-01-03 16:47:34 +00002270
Chris Lattner638402b2007-02-28 07:00:42 +00002271 // Analyze operands of the call, assigning locations to each operand.
Chris Lattner423c5f42007-02-28 05:31:48 +00002272 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002273 CCState CCInfo(CallConv, isVarArg, MF, getTargetMachine(),
Dan Gohman98ca4f22009-08-05 01:29:28 +00002274 ArgLocs, *DAG.getContext());
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00002275
2276 // Allocate shadow area for Win64
2277 if (IsWin64) {
2278 CCInfo.AllocateStack(32, 8);
2279 }
2280
Duncan Sands45907662010-10-31 13:21:44 +00002281 CCInfo.AnalyzeCallOperands(Outs, CC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00002282
Chris Lattner423c5f42007-02-28 05:31:48 +00002283 // Get a count of how many bytes are to be pushed on the stack.
2284 unsigned NumBytes = CCInfo.getNextStackOffset();
Evan Chengf22f9b32010-02-06 03:28:46 +00002285 if (IsSibcall)
Evan Chengb2c92902010-02-02 02:22:50 +00002286 // This is a sibcall. The memory operands are available in caller's
2287 // own caller's stack.
2288 NumBytes = 0;
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002289 else if (getTargetMachine().Options.GuaranteedTailCallOpt &&
2290 IsTailCallConvention(CallConv))
Evan Chengf22f9b32010-02-06 03:28:46 +00002291 NumBytes = GetAlignedArgumentStackSize(NumBytes, DAG);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002292
Gordon Henriksen86737662008-01-05 16:56:59 +00002293 int FPDiff = 0;
Evan Chengf22f9b32010-02-06 03:28:46 +00002294 if (isTailCall && !IsSibcall) {
Gordon Henriksen86737662008-01-05 16:56:59 +00002295 // Lower arguments at fp - stackoffset + fpdiff.
Jakub Staszak96df4372012-10-29 22:02:26 +00002296 X86MachineFunctionInfo *X86Info = MF.getInfo<X86MachineFunctionInfo>();
2297 unsigned NumBytesCallerPushed = X86Info->getBytesToPopOnReturn();
2298
Gordon Henriksen86737662008-01-05 16:56:59 +00002299 FPDiff = NumBytesCallerPushed - NumBytes;
2300
2301 // Set the delta of movement of the returnaddr stackslot.
2302 // But only set if delta is greater than previous delta.
Jakub Staszak96df4372012-10-29 22:02:26 +00002303 if (FPDiff < X86Info->getTCReturnAddrDelta())
2304 X86Info->setTCReturnAddrDelta(FPDiff);
Gordon Henriksen86737662008-01-05 16:56:59 +00002305 }
2306
Evan Chengf22f9b32010-02-06 03:28:46 +00002307 if (!IsSibcall)
2308 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002309
Dan Gohman475871a2008-07-27 21:46:04 +00002310 SDValue RetAddrFrIdx;
Chris Lattner7a2bdde2011-04-15 05:18:47 +00002311 // Load return address for tail calls.
Evan Chengf22f9b32010-02-06 03:28:46 +00002312 if (isTailCall && FPDiff)
2313 Chain = EmitTailCallLoadRetAddr(DAG, RetAddrFrIdx, Chain, isTailCall,
2314 Is64Bit, FPDiff, dl);
Gordon Henriksen86737662008-01-05 16:56:59 +00002315
Dan Gohman475871a2008-07-27 21:46:04 +00002316 SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass;
2317 SmallVector<SDValue, 8> MemOpChains;
2318 SDValue StackPtr;
Chris Lattner423c5f42007-02-28 05:31:48 +00002319
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002320 // Walk the register/memloc assignments, inserting copies/loads. In the case
2321 // of tail call optimization arguments are handle later.
Chris Lattner423c5f42007-02-28 05:31:48 +00002322 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2323 CCValAssign &VA = ArgLocs[i];
Owen Andersone50ed302009-08-10 22:56:29 +00002324 EVT RegVT = VA.getLocVT();
Dan Gohmanc9403652010-07-07 15:54:55 +00002325 SDValue Arg = OutVals[i];
Dan Gohman98ca4f22009-08-05 01:29:28 +00002326 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Dan Gohman095cc292008-09-13 01:54:27 +00002327 bool isByVal = Flags.isByVal();
Scott Michelfdc40a02009-02-17 22:15:04 +00002328
Chris Lattner423c5f42007-02-28 05:31:48 +00002329 // Promote the value if needed.
2330 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002331 default: llvm_unreachable("Unknown loc info!");
Chris Lattner423c5f42007-02-28 05:31:48 +00002332 case CCValAssign::Full: break;
2333 case CCValAssign::SExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002334 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00002335 break;
2336 case CCValAssign::ZExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002337 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00002338 break;
2339 case CCValAssign::AExt:
Craig Topper7a9a28b2012-08-12 02:23:29 +00002340 if (RegVT.is128BitVector()) {
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002341 // Special case: passing MMX values in XMM registers.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002342 Arg = DAG.getNode(ISD::BITCAST, dl, MVT::i64, Arg);
Owen Anderson825b72b2009-08-11 20:47:22 +00002343 Arg = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i64, Arg);
2344 Arg = getMOVL(DAG, dl, MVT::v2i64, DAG.getUNDEF(MVT::v2i64), Arg);
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002345 } else
2346 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, RegVT, Arg);
2347 break;
2348 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002349 Arg = DAG.getNode(ISD::BITCAST, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00002350 break;
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00002351 case CCValAssign::Indirect: {
2352 // Store the argument.
2353 SDValue SpillSlot = DAG.CreateStackTemporary(VA.getValVT());
Evan Chengff89dcb2009-10-18 18:16:27 +00002354 int FI = cast<FrameIndexSDNode>(SpillSlot)->getIndex();
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00002355 Chain = DAG.getStore(Chain, dl, Arg, SpillSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00002356 MachinePointerInfo::getFixedStack(FI),
David Greene67c9d422010-02-15 16:53:33 +00002357 false, false, 0);
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00002358 Arg = SpillSlot;
2359 break;
2360 }
Evan Cheng6b5783d2006-05-25 18:56:34 +00002361 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002362
Chris Lattner423c5f42007-02-28 05:31:48 +00002363 if (VA.isRegLoc()) {
Stuart Hastings2aa0f232011-05-26 04:09:49 +00002364 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
2365 if (isVarArg && IsWin64) {
2366 // Win64 ABI requires argument XMM reg to be copied to the corresponding
2367 // shadow reg if callee is a varargs function.
2368 unsigned ShadowReg = 0;
2369 switch (VA.getLocReg()) {
2370 case X86::XMM0: ShadowReg = X86::RCX; break;
2371 case X86::XMM1: ShadowReg = X86::RDX; break;
2372 case X86::XMM2: ShadowReg = X86::R8; break;
2373 case X86::XMM3: ShadowReg = X86::R9; break;
Anton Korobeynikovc52bedb2010-08-27 14:43:06 +00002374 }
Stuart Hastings2aa0f232011-05-26 04:09:49 +00002375 if (ShadowReg)
2376 RegsToPass.push_back(std::make_pair(ShadowReg, Arg));
Anton Korobeynikovc52bedb2010-08-27 14:43:06 +00002377 }
Evan Chengf22f9b32010-02-06 03:28:46 +00002378 } else if (!IsSibcall && (!isTailCall || isByVal)) {
Evan Cheng5f941932010-02-05 02:21:12 +00002379 assert(VA.isMemLoc());
2380 if (StackPtr.getNode() == 0)
Michael Liaoc5c970e2012-10-31 04:14:09 +00002381 StackPtr = DAG.getCopyFromReg(Chain, dl, RegInfo->getStackRegister(),
2382 getPointerTy());
Evan Cheng5f941932010-02-05 02:21:12 +00002383 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Arg,
2384 dl, DAG, VA, Flags));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002385 }
Stuart Hastings2aa0f232011-05-26 04:09:49 +00002386 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002387
Evan Cheng32fe1032006-05-25 00:59:30 +00002388 if (!MemOpChains.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00002389 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002390 &MemOpChains[0], MemOpChains.size());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002391
Chris Lattner88e1fd52009-07-09 04:24:46 +00002392 if (Subtarget->isPICStyleGOT()) {
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002393 // ELF / PIC requires GOT in the EBX register before function calls via PLT
2394 // GOT pointer.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002395 if (!isTailCall) {
Jakob Stoklund Olesenb8720782012-07-04 19:28:31 +00002396 RegsToPass.push_back(std::make_pair(unsigned(X86::EBX),
2397 DAG.getNode(X86ISD::GlobalBaseReg, DebugLoc(), getPointerTy())));
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002398 } else {
2399 // If we are tail calling and generating PIC/GOT style code load the
2400 // address of the callee into ECX. The value in ecx is used as target of
2401 // the tail jump. This is done to circumvent the ebx/callee-saved problem
2402 // for tail calls on PIC/GOT architectures. Normally we would just put the
2403 // address of GOT into ebx and then call target@PLT. But for tail calls
2404 // ebx would be restored (since ebx is callee saved) before jumping to the
2405 // target@PLT.
2406
2407 // Note: The actual moving to ECX is done further down.
2408 GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee);
2409 if (G && !G->getGlobal()->hasHiddenVisibility() &&
2410 !G->getGlobal()->hasProtectedVisibility())
2411 Callee = LowerGlobalAddress(Callee, DAG);
2412 else if (isa<ExternalSymbolSDNode>(Callee))
Chris Lattner15a380a2009-07-09 04:39:06 +00002413 Callee = LowerExternalSymbol(Callee, DAG);
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002414 }
Anton Korobeynikov7f705592007-01-12 19:20:47 +00002415 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00002416
NAKAMURA Takumifb840c92011-02-05 15:11:13 +00002417 if (Is64Bit && isVarArg && !IsWin64) {
Gordon Henriksen86737662008-01-05 16:56:59 +00002418 // From AMD64 ABI document:
2419 // For calls that may call functions that use varargs or stdargs
2420 // (prototype-less calls or calls to functions containing ellipsis (...) in
2421 // the declaration) %al is used as hidden argument to specify the number
2422 // of SSE registers used. The contents of %al do not need to match exactly
2423 // the number of registers, but must be an ubound on the number of SSE
2424 // registers used and is in the range 0 - 8 inclusive.
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002425
Gordon Henriksen86737662008-01-05 16:56:59 +00002426 // Count the number of XMM registers allocated.
Craig Topperc5eaae42012-03-11 07:57:25 +00002427 static const uint16_t XMMArgRegs[] = {
Gordon Henriksen86737662008-01-05 16:56:59 +00002428 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
2429 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
2430 };
2431 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs, 8);
Craig Topper1accb7e2012-01-10 06:54:16 +00002432 assert((Subtarget->hasSSE1() || !NumXMMRegs)
Torok Edwin3f142c32009-02-01 18:15:56 +00002433 && "SSE registers cannot be used when SSE is disabled");
Scott Michelfdc40a02009-02-17 22:15:04 +00002434
Jakob Stoklund Olesenb8720782012-07-04 19:28:31 +00002435 RegsToPass.push_back(std::make_pair(unsigned(X86::AL),
2436 DAG.getConstant(NumXMMRegs, MVT::i8)));
Gordon Henriksen86737662008-01-05 16:56:59 +00002437 }
2438
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00002439 // For tail calls lower the arguments to the 'real' stack slot.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002440 if (isTailCall) {
2441 // Force all the incoming stack arguments to be loaded from the stack
2442 // before any new outgoing arguments are stored to the stack, because the
2443 // outgoing stack slots may alias the incoming argument stack slots, and
2444 // the alias isn't otherwise explicit. This is slightly more conservative
2445 // than necessary, because it means that each store effectively depends
2446 // on every argument instead of just those arguments it would clobber.
2447 SDValue ArgChain = DAG.getStackArgumentTokenFactor(Chain);
2448
Dan Gohman475871a2008-07-27 21:46:04 +00002449 SmallVector<SDValue, 8> MemOpChains2;
2450 SDValue FIN;
Gordon Henriksen86737662008-01-05 16:56:59 +00002451 int FI = 0;
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002452 if (getTargetMachine().Options.GuaranteedTailCallOpt) {
Evan Chengb2c92902010-02-02 02:22:50 +00002453 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2454 CCValAssign &VA = ArgLocs[i];
2455 if (VA.isRegLoc())
2456 continue;
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00002457 assert(VA.isMemLoc());
Dan Gohmanc9403652010-07-07 15:54:55 +00002458 SDValue Arg = OutVals[i];
Dan Gohman98ca4f22009-08-05 01:29:28 +00002459 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Gordon Henriksen86737662008-01-05 16:56:59 +00002460 // Create frame index.
2461 int32_t Offset = VA.getLocMemOffset()+FPDiff;
Duncan Sands83ec4b62008-06-06 12:08:01 +00002462 uint32_t OpSize = (VA.getLocVT().getSizeInBits()+7)/8;
Evan Chenged2ae132010-07-03 00:40:23 +00002463 FI = MF.getFrameInfo()->CreateFixedObject(OpSize, Offset, true);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002464 FIN = DAG.getFrameIndex(FI, getPointerTy());
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00002465
Duncan Sands276dcbd2008-03-21 09:14:45 +00002466 if (Flags.isByVal()) {
Evan Cheng8e5712b2008-01-12 01:08:07 +00002467 // Copy relative to framepointer.
Dan Gohman475871a2008-07-27 21:46:04 +00002468 SDValue Source = DAG.getIntPtrConstant(VA.getLocMemOffset());
Gabor Greifba36cb52008-08-28 21:40:38 +00002469 if (StackPtr.getNode() == 0)
Michael Liaoc5c970e2012-10-31 04:14:09 +00002470 StackPtr = DAG.getCopyFromReg(Chain, dl,
2471 RegInfo->getStackRegister(),
Dale Johannesendd64c412009-02-04 00:33:20 +00002472 getPointerTy());
Dale Johannesenace16102009-02-03 19:33:06 +00002473 Source = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, Source);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002474
Dan Gohman98ca4f22009-08-05 01:29:28 +00002475 MemOpChains2.push_back(CreateCopyOfByValArgument(Source, FIN,
2476 ArgChain,
Dale Johannesendd64c412009-02-04 00:33:20 +00002477 Flags, DAG, dl));
Gordon Henriksen86737662008-01-05 16:56:59 +00002478 } else {
Evan Cheng8e5712b2008-01-12 01:08:07 +00002479 // Store relative to framepointer.
Dan Gohman69de1932008-02-06 22:27:42 +00002480 MemOpChains2.push_back(
Dan Gohman98ca4f22009-08-05 01:29:28 +00002481 DAG.getStore(ArgChain, dl, Arg, FIN,
Chris Lattnere8639032010-09-21 06:22:23 +00002482 MachinePointerInfo::getFixedStack(FI),
David Greene67c9d422010-02-15 16:53:33 +00002483 false, false, 0));
Scott Michelfdc40a02009-02-17 22:15:04 +00002484 }
Gordon Henriksen86737662008-01-05 16:56:59 +00002485 }
2486 }
2487
2488 if (!MemOpChains2.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00002489 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Arnold Schwaighofer719eb022008-01-11 14:34:56 +00002490 &MemOpChains2[0], MemOpChains2.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00002491
2492 // Store the return address to the appropriate stack slot.
Michael Liaoaa3c2c02012-10-25 06:29:14 +00002493 Chain = EmitTailCallStoreRetAddr(DAG, MF, Chain, RetAddrFrIdx,
2494 getPointerTy(), RegInfo->getSlotSize(),
Dale Johannesenace16102009-02-03 19:33:06 +00002495 FPDiff, dl);
Gordon Henriksen86737662008-01-05 16:56:59 +00002496 }
2497
Jakob Stoklund Olesenb8720782012-07-04 19:28:31 +00002498 // Build a sequence of copy-to-reg nodes chained together with token chain
2499 // and flag operands which copy the outgoing args into registers.
2500 SDValue InFlag;
2501 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
2502 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
2503 RegsToPass[i].second, InFlag);
2504 InFlag = Chain.getValue(1);
2505 }
2506
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002507 if (getTargetMachine().getCodeModel() == CodeModel::Large) {
2508 assert(Is64Bit && "Large code model is only legal in 64-bit mode.");
2509 // In the 64-bit large code model, we have to make all calls
2510 // through a register, since the call instruction's 32-bit
2511 // pc-relative offset may not be large enough to hold the whole
2512 // address.
2513 } else if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002514 // If the callee is a GlobalAddress node (quite common, every direct call
2515 // is) turn it into a TargetGlobalAddress node so that legalize doesn't hack
2516 // it.
2517
Anton Korobeynikov2b2bc682006-12-22 22:29:05 +00002518 // We should use extra load for direct calls to dllimported functions in
2519 // non-JIT mode.
Dan Gohman46510a72010-04-15 01:51:59 +00002520 const GlobalValue *GV = G->getGlobal();
Chris Lattner754b7652009-07-10 05:48:03 +00002521 if (!GV->hasDLLImportLinkage()) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002522 unsigned char OpFlags = 0;
John McCall3a3465b2011-06-15 20:36:13 +00002523 bool ExtraLoad = false;
2524 unsigned WrapperKind = ISD::DELETED_NODE;
Eric Christopherfd179292009-08-27 18:07:15 +00002525
Chris Lattner48a7d022009-07-09 05:02:21 +00002526 // On ELF targets, in both X86-64 and X86-32 mode, direct calls to
2527 // external symbols most go through the PLT in PIC mode. If the symbol
2528 // has hidden or protected visibility, or if it is static or local, then
2529 // we don't need to use the PLT - we can directly call it.
2530 if (Subtarget->isTargetELF() &&
2531 getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
Chris Lattner74e726e2009-07-09 05:27:35 +00002532 GV->hasDefaultVisibility() && !GV->hasLocalLinkage()) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002533 OpFlags = X86II::MO_PLT;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00002534 } else if (Subtarget->isPICStyleStubAny() &&
Chris Lattner80945782010-09-27 06:34:01 +00002535 (GV->isDeclaration() || GV->isWeakForLinker()) &&
Daniel Dunbar558692f2011-04-20 00:14:25 +00002536 (!Subtarget->getTargetTriple().isMacOSX() ||
2537 Subtarget->getTargetTriple().isMacOSXVersionLT(10, 5))) {
Chris Lattner74e726e2009-07-09 05:27:35 +00002538 // PC-relative references to external symbols should go through $stub,
2539 // unless we're building with the leopard linker or later, which
2540 // automatically synthesizes these stubs.
2541 OpFlags = X86II::MO_DARWIN_STUB;
John McCall3a3465b2011-06-15 20:36:13 +00002542 } else if (Subtarget->isPICStyleRIPRel() &&
2543 isa<Function>(GV) &&
Bill Wendling67658342012-10-09 07:45:08 +00002544 cast<Function>(GV)->getFnAttributes().
Bill Wendling034b94b2012-12-19 07:18:57 +00002545 hasAttribute(Attribute::NonLazyBind)) {
John McCall3a3465b2011-06-15 20:36:13 +00002546 // If the function is marked as non-lazy, generate an indirect call
2547 // which loads from the GOT directly. This avoids runtime overhead
2548 // at the cost of eager binding (and one extra byte of encoding).
2549 OpFlags = X86II::MO_GOTPCREL;
2550 WrapperKind = X86ISD::WrapperRIP;
2551 ExtraLoad = true;
Chris Lattner74e726e2009-07-09 05:27:35 +00002552 }
Chris Lattner48a7d022009-07-09 05:02:21 +00002553
Devang Patel0d881da2010-07-06 22:08:15 +00002554 Callee = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(),
Chris Lattner48a7d022009-07-09 05:02:21 +00002555 G->getOffset(), OpFlags);
John McCall3a3465b2011-06-15 20:36:13 +00002556
2557 // Add a wrapper if needed.
2558 if (WrapperKind != ISD::DELETED_NODE)
2559 Callee = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Callee);
2560 // Add extra indirection if needed.
2561 if (ExtraLoad)
2562 Callee = DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(), Callee,
2563 MachinePointerInfo::getGOT(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002564 false, false, false, 0);
Chris Lattner48a7d022009-07-09 05:02:21 +00002565 }
Bill Wendling056292f2008-09-16 21:48:12 +00002566 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002567 unsigned char OpFlags = 0;
2568
Evan Cheng1bf891a2010-12-01 22:59:46 +00002569 // On ELF targets, in either X86-64 or X86-32 mode, direct calls to
2570 // external symbols should go through the PLT.
2571 if (Subtarget->isTargetELF() &&
2572 getTargetMachine().getRelocationModel() == Reloc::PIC_) {
2573 OpFlags = X86II::MO_PLT;
2574 } else if (Subtarget->isPICStyleStubAny() &&
Daniel Dunbar558692f2011-04-20 00:14:25 +00002575 (!Subtarget->getTargetTriple().isMacOSX() ||
2576 Subtarget->getTargetTriple().isMacOSXVersionLT(10, 5))) {
Evan Cheng1bf891a2010-12-01 22:59:46 +00002577 // PC-relative references to external symbols should go through $stub,
2578 // unless we're building with the leopard linker or later, which
2579 // automatically synthesizes these stubs.
2580 OpFlags = X86II::MO_DARWIN_STUB;
Chris Lattner74e726e2009-07-09 05:27:35 +00002581 }
Eric Christopherfd179292009-08-27 18:07:15 +00002582
Chris Lattner48a7d022009-07-09 05:02:21 +00002583 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy(),
2584 OpFlags);
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002585 }
2586
Chris Lattnerd96d0722007-02-25 06:40:16 +00002587 // Returns a chain & a flag for retval copy to use.
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002588 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Dan Gohman475871a2008-07-27 21:46:04 +00002589 SmallVector<SDValue, 8> Ops;
Gordon Henriksen86737662008-01-05 16:56:59 +00002590
Evan Chengf22f9b32010-02-06 03:28:46 +00002591 if (!IsSibcall && isTailCall) {
Dale Johannesene8d72302009-02-06 23:05:02 +00002592 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
2593 DAG.getIntPtrConstant(0, true), InFlag);
Gordon Henriksen86737662008-01-05 16:56:59 +00002594 InFlag = Chain.getValue(1);
Gordon Henriksen86737662008-01-05 16:56:59 +00002595 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002596
Nate Begeman4c5dcf52006-02-17 00:03:04 +00002597 Ops.push_back(Chain);
2598 Ops.push_back(Callee);
Evan Chengb69d1132006-06-14 18:17:40 +00002599
Dan Gohman98ca4f22009-08-05 01:29:28 +00002600 if (isTailCall)
Owen Anderson825b72b2009-08-11 20:47:22 +00002601 Ops.push_back(DAG.getConstant(FPDiff, MVT::i32));
Evan Chengf4684712007-02-21 21:18:14 +00002602
Gordon Henriksen86737662008-01-05 16:56:59 +00002603 // Add argument registers to the end of the list so that they are known live
2604 // into the call.
Evan Cheng9b449442008-01-07 23:08:23 +00002605 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
2606 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
2607 RegsToPass[i].second.getValueType()));
Scott Michelfdc40a02009-02-17 22:15:04 +00002608
Jakob Stoklund Olesen8bcde2a2012-02-16 00:02:50 +00002609 // Add a register mask operand representing the call-preserved registers.
2610 const TargetRegisterInfo *TRI = getTargetMachine().getRegisterInfo();
2611 const uint32_t *Mask = TRI->getCallPreservedMask(CallConv);
2612 assert(Mask && "Missing call preserved mask for calling convention");
2613 Ops.push_back(DAG.getRegisterMask(Mask));
Jakob Stoklund Olesenc38c4562012-01-18 23:52:22 +00002614
Gabor Greifba36cb52008-08-28 21:40:38 +00002615 if (InFlag.getNode())
Evan Cheng347d5f72006-04-28 21:29:37 +00002616 Ops.push_back(InFlag);
Gordon Henriksenae636f82008-01-03 16:47:34 +00002617
Dan Gohman98ca4f22009-08-05 01:29:28 +00002618 if (isTailCall) {
Dale Johannesen88004c22010-06-05 00:30:45 +00002619 // We used to do:
2620 //// If this is the first return lowered for this function, add the regs
2621 //// to the liveout set for the function.
2622 // This isn't right, although it's probably harmless on x86; liveouts
2623 // should be computed from returns not tail calls. Consider a void
2624 // function making a tail call to a function returning int.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002625 return DAG.getNode(X86ISD::TC_RETURN, dl,
2626 NodeTys, &Ops[0], Ops.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00002627 }
2628
Dale Johannesenace16102009-02-03 19:33:06 +00002629 Chain = DAG.getNode(X86ISD::CALL, dl, NodeTys, &Ops[0], Ops.size());
Evan Cheng347d5f72006-04-28 21:29:37 +00002630 InFlag = Chain.getValue(1);
Evan Chengd90eb7f2006-01-05 00:27:02 +00002631
Chris Lattner2d297092006-05-23 18:50:38 +00002632 // Create the CALLSEQ_END node.
Gordon Henriksen86737662008-01-05 16:56:59 +00002633 unsigned NumBytesForCalleeToPush;
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002634 if (X86::isCalleePop(CallConv, Is64Bit, isVarArg,
2635 getTargetMachine().Options.GuaranteedTailCallOpt))
Gordon Henriksen86737662008-01-05 16:56:59 +00002636 NumBytesForCalleeToPush = NumBytes; // Callee pops everything
Eli Friedman9a2478a2012-01-20 00:05:46 +00002637 else if (!Is64Bit && !IsTailCallConvention(CallConv) && !IsWindows &&
Rafael Espindola1cee7102012-07-25 13:41:10 +00002638 SR == StackStructReturn)
Dan Gohmanf451cb82010-02-10 16:03:48 +00002639 // If this is a call to a struct-return function, the callee
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00002640 // pops the hidden struct pointer, so we have to push it back.
2641 // This is common for Darwin/X86, Linux & Mingw32 targets.
Eli Friedman9a2478a2012-01-20 00:05:46 +00002642 // For MSVC Win32 targets, the caller pops the hidden struct pointer.
Gordon Henriksenae636f82008-01-03 16:47:34 +00002643 NumBytesForCalleeToPush = 4;
Gordon Henriksen86737662008-01-05 16:56:59 +00002644 else
Gordon Henriksenae636f82008-01-03 16:47:34 +00002645 NumBytesForCalleeToPush = 0; // Callee pops nothing.
Scott Michelfdc40a02009-02-17 22:15:04 +00002646
Gordon Henriksenae636f82008-01-03 16:47:34 +00002647 // Returns a flag for retval copy to use.
Evan Chengf22f9b32010-02-06 03:28:46 +00002648 if (!IsSibcall) {
2649 Chain = DAG.getCALLSEQ_END(Chain,
2650 DAG.getIntPtrConstant(NumBytes, true),
2651 DAG.getIntPtrConstant(NumBytesForCalleeToPush,
2652 true),
2653 InFlag);
2654 InFlag = Chain.getValue(1);
2655 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00002656
Chris Lattner3085e152007-02-25 08:59:22 +00002657 // Handle result values, copying them out of physregs into vregs that we
2658 // return.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002659 return LowerCallResult(Chain, InFlag, CallConv, isVarArg,
2660 Ins, dl, DAG, InVals);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002661}
2662
Evan Cheng25ab6902006-09-08 06:48:29 +00002663//===----------------------------------------------------------------------===//
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002664// Fast Calling Convention (tail call) implementation
2665//===----------------------------------------------------------------------===//
2666
2667// Like std call, callee cleans arguments, convention except that ECX is
2668// reserved for storing the tail called function address. Only 2 registers are
2669// free for argument passing (inreg). Tail call optimization is performed
2670// provided:
2671// * tailcallopt is enabled
2672// * caller/callee are fastcc
Arnold Schwaighofera2a4b472008-02-26 10:21:54 +00002673// On X86_64 architecture with GOT-style position independent code only local
2674// (within module) calls are supported at the moment.
Arnold Schwaighofer48abc5c2007-10-12 21:30:57 +00002675// To keep the stack aligned according to platform abi the function
2676// GetAlignedArgumentStackSize ensures that argument delta is always multiples
2677// of stack alignment. (Dynamic linkers need this - darwin's dyld for example)
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002678// If a tail called function callee has more arguments than the caller the
2679// caller needs to make sure that there is room to move the RETADDR to. This is
Arnold Schwaighofer48abc5c2007-10-12 21:30:57 +00002680// achieved by reserving an area the size of the argument delta right after the
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002681// original REtADDR, but before the saved framepointer or the spilled registers
2682// e.g. caller(arg1, arg2) calls callee(arg1, arg2,arg3,arg4)
2683// stack layout:
2684// arg1
2685// arg2
2686// RETADDR
Scott Michelfdc40a02009-02-17 22:15:04 +00002687// [ new RETADDR
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002688// move area ]
2689// (possible EBP)
2690// ESI
2691// EDI
2692// local1 ..
2693
2694/// GetAlignedArgumentStackSize - Make the stack size align e.g 16n + 12 aligned
2695/// for a 16 byte align requirement.
Dan Gohmand858e902010-04-17 15:26:15 +00002696unsigned
2697X86TargetLowering::GetAlignedArgumentStackSize(unsigned StackSize,
2698 SelectionDAG& DAG) const {
Evan Chenge9ac9e62008-09-07 09:07:23 +00002699 MachineFunction &MF = DAG.getMachineFunction();
2700 const TargetMachine &TM = MF.getTarget();
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00002701 const TargetFrameLowering &TFI = *TM.getFrameLowering();
Evan Chenge9ac9e62008-09-07 09:07:23 +00002702 unsigned StackAlignment = TFI.getStackAlignment();
Scott Michelfdc40a02009-02-17 22:15:04 +00002703 uint64_t AlignMask = StackAlignment - 1;
Evan Chenge9ac9e62008-09-07 09:07:23 +00002704 int64_t Offset = StackSize;
Michael Liaoaa3c2c02012-10-25 06:29:14 +00002705 unsigned SlotSize = RegInfo->getSlotSize();
Evan Chenge9ac9e62008-09-07 09:07:23 +00002706 if ( (Offset & AlignMask) <= (StackAlignment - SlotSize) ) {
2707 // Number smaller than 12 so just add the difference.
2708 Offset += ((StackAlignment - SlotSize) - (Offset & AlignMask));
2709 } else {
2710 // Mask out lower bits, add stackalignment once plus the 12 bytes.
Scott Michelfdc40a02009-02-17 22:15:04 +00002711 Offset = ((~AlignMask) & Offset) + StackAlignment +
Evan Chenge9ac9e62008-09-07 09:07:23 +00002712 (StackAlignment-SlotSize);
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002713 }
Evan Chenge9ac9e62008-09-07 09:07:23 +00002714 return Offset;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002715}
2716
Evan Cheng5f941932010-02-05 02:21:12 +00002717/// MatchingStackOffset - Return true if the given stack call argument is
2718/// already available in the same position (relatively) of the caller's
2719/// incoming argument stack.
2720static
2721bool MatchingStackOffset(SDValue Arg, unsigned Offset, ISD::ArgFlagsTy Flags,
2722 MachineFrameInfo *MFI, const MachineRegisterInfo *MRI,
2723 const X86InstrInfo *TII) {
Evan Cheng4cae1332010-03-05 08:38:04 +00002724 unsigned Bytes = Arg.getValueType().getSizeInBits() / 8;
2725 int FI = INT_MAX;
Evan Cheng5f941932010-02-05 02:21:12 +00002726 if (Arg.getOpcode() == ISD::CopyFromReg) {
2727 unsigned VR = cast<RegisterSDNode>(Arg.getOperand(1))->getReg();
Jakob Stoklund Olesenc9df0252011-01-10 02:58:51 +00002728 if (!TargetRegisterInfo::isVirtualRegister(VR))
Evan Cheng5f941932010-02-05 02:21:12 +00002729 return false;
2730 MachineInstr *Def = MRI->getVRegDef(VR);
2731 if (!Def)
2732 return false;
2733 if (!Flags.isByVal()) {
2734 if (!TII->isLoadFromStackSlot(Def, FI))
2735 return false;
2736 } else {
2737 unsigned Opcode = Def->getOpcode();
2738 if ((Opcode == X86::LEA32r || Opcode == X86::LEA64r) &&
2739 Def->getOperand(1).isFI()) {
2740 FI = Def->getOperand(1).getIndex();
Evan Cheng4cae1332010-03-05 08:38:04 +00002741 Bytes = Flags.getByValSize();
Evan Cheng5f941932010-02-05 02:21:12 +00002742 } else
2743 return false;
2744 }
Evan Cheng4cae1332010-03-05 08:38:04 +00002745 } else if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Arg)) {
2746 if (Flags.isByVal())
2747 // ByVal argument is passed in as a pointer but it's now being
Evan Cheng10718492010-03-05 19:55:55 +00002748 // dereferenced. e.g.
Evan Cheng4cae1332010-03-05 08:38:04 +00002749 // define @foo(%struct.X* %A) {
2750 // tail call @bar(%struct.X* byval %A)
2751 // }
Evan Cheng5f941932010-02-05 02:21:12 +00002752 return false;
2753 SDValue Ptr = Ld->getBasePtr();
2754 FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr);
2755 if (!FINode)
2756 return false;
2757 FI = FINode->getIndex();
Chad Rosierdf78fcd2011-06-25 02:04:56 +00002758 } else if (Arg.getOpcode() == ISD::FrameIndex && Flags.isByVal()) {
Chad Rosier14d71aa2011-06-25 18:51:28 +00002759 FrameIndexSDNode *FINode = cast<FrameIndexSDNode>(Arg);
Chad Rosierdf78fcd2011-06-25 02:04:56 +00002760 FI = FINode->getIndex();
2761 Bytes = Flags.getByValSize();
Evan Cheng4cae1332010-03-05 08:38:04 +00002762 } else
2763 return false;
Evan Cheng5f941932010-02-05 02:21:12 +00002764
Evan Cheng4cae1332010-03-05 08:38:04 +00002765 assert(FI != INT_MAX);
Evan Cheng5f941932010-02-05 02:21:12 +00002766 if (!MFI->isFixedObjectIndex(FI))
2767 return false;
Evan Cheng4cae1332010-03-05 08:38:04 +00002768 return Offset == MFI->getObjectOffset(FI) && Bytes == MFI->getObjectSize(FI);
Evan Cheng5f941932010-02-05 02:21:12 +00002769}
2770
Dan Gohman98ca4f22009-08-05 01:29:28 +00002771/// IsEligibleForTailCallOptimization - Check whether the call is eligible
2772/// for tail call optimization. Targets which want to do tail call
2773/// optimization should implement this function.
2774bool
2775X86TargetLowering::IsEligibleForTailCallOptimization(SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00002776 CallingConv::ID CalleeCC,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002777 bool isVarArg,
Evan Chenga375d472010-03-15 18:54:48 +00002778 bool isCalleeStructRet,
2779 bool isCallerStructRet,
Evan Chengb1cacc72012-09-25 05:32:34 +00002780 Type *RetTy,
Evan Chengb1712452010-01-27 06:25:16 +00002781 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00002782 const SmallVectorImpl<SDValue> &OutVals,
Evan Chengb1712452010-01-27 06:25:16 +00002783 const SmallVectorImpl<ISD::InputArg> &Ins,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002784 SelectionDAG& DAG) const {
Chris Lattner29689432010-03-11 00:22:57 +00002785 if (!IsTailCallConvention(CalleeCC) &&
Evan Chengb1712452010-01-27 06:25:16 +00002786 CalleeCC != CallingConv::C)
2787 return false;
2788
Evan Cheng7096ae42010-01-29 06:45:59 +00002789 // If -tailcallopt is specified, make fastcc functions tail-callable.
Evan Cheng2c12cb42010-03-26 16:26:03 +00002790 const MachineFunction &MF = DAG.getMachineFunction();
Evan Cheng7096ae42010-01-29 06:45:59 +00002791 const Function *CallerF = DAG.getMachineFunction().getFunction();
Evan Chengb1cacc72012-09-25 05:32:34 +00002792
2793 // If the function return type is x86_fp80 and the callee return type is not,
2794 // then the FP_EXTEND of the call result is not a nop. It's not safe to
2795 // perform a tailcall optimization here.
2796 if (CallerF->getReturnType()->isX86_FP80Ty() && !RetTy->isX86_FP80Ty())
2797 return false;
2798
Evan Cheng13617962010-04-30 01:12:32 +00002799 CallingConv::ID CallerCC = CallerF->getCallingConv();
2800 bool CCMatch = CallerCC == CalleeCC;
2801
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002802 if (getTargetMachine().Options.GuaranteedTailCallOpt) {
Evan Cheng13617962010-04-30 01:12:32 +00002803 if (IsTailCallConvention(CalleeCC) && CCMatch)
Evan Cheng843bd692010-01-31 06:44:49 +00002804 return true;
2805 return false;
2806 }
2807
Dale Johannesen2f05cc02010-05-28 23:24:28 +00002808 // Look for obvious safe cases to perform tail call optimization that do not
2809 // require ABI changes. This is what gcc calls sibcall.
Evan Chengb2c92902010-02-02 02:22:50 +00002810
Evan Cheng2c12cb42010-03-26 16:26:03 +00002811 // Can't do sibcall if stack needs to be dynamically re-aligned. PEI needs to
2812 // emit a special epilogue.
2813 if (RegInfo->needsStackRealignment(MF))
2814 return false;
2815
Evan Chenga375d472010-03-15 18:54:48 +00002816 // Also avoid sibcall optimization if either caller or callee uses struct
2817 // return semantics.
2818 if (isCalleeStructRet || isCallerStructRet)
2819 return false;
2820
Chad Rosier2416da32011-06-24 21:15:36 +00002821 // An stdcall caller is expected to clean up its arguments; the callee
2822 // isn't going to do that.
2823 if (!CCMatch && CallerCC==CallingConv::X86_StdCall)
2824 return false;
2825
Chad Rosier871f6642011-05-18 19:59:50 +00002826 // Do not sibcall optimize vararg calls unless all arguments are passed via
Chad Rosiera1660892011-05-20 00:59:28 +00002827 // registers.
Chad Rosier871f6642011-05-18 19:59:50 +00002828 if (isVarArg && !Outs.empty()) {
Chad Rosiera1660892011-05-20 00:59:28 +00002829
2830 // Optimizing for varargs on Win64 is unlikely to be safe without
2831 // additional testing.
2832 if (Subtarget->isTargetWin64())
2833 return false;
2834
Chad Rosier871f6642011-05-18 19:59:50 +00002835 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002836 CCState CCInfo(CalleeCC, isVarArg, DAG.getMachineFunction(),
Craig Topper0fbf3642012-04-23 03:28:34 +00002837 getTargetMachine(), ArgLocs, *DAG.getContext());
Chad Rosier871f6642011-05-18 19:59:50 +00002838
Chad Rosier871f6642011-05-18 19:59:50 +00002839 CCInfo.AnalyzeCallOperands(Outs, CC_X86);
2840 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i)
2841 if (!ArgLocs[i].isRegLoc())
2842 return false;
2843 }
2844
Chad Rosier30450e82011-12-22 22:35:21 +00002845 // If the call result is in ST0 / ST1, it needs to be popped off the x87
2846 // stack. Therefore, if it's not used by the call it is not safe to optimize
2847 // this into a sibcall.
Evan Chengf5b9d6c2010-03-20 02:58:15 +00002848 bool Unused = false;
2849 for (unsigned i = 0, e = Ins.size(); i != e; ++i) {
2850 if (!Ins[i].Used) {
2851 Unused = true;
2852 break;
2853 }
2854 }
2855 if (Unused) {
2856 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002857 CCState CCInfo(CalleeCC, false, DAG.getMachineFunction(),
Craig Topper0fbf3642012-04-23 03:28:34 +00002858 getTargetMachine(), RVLocs, *DAG.getContext());
Evan Chengf5b9d6c2010-03-20 02:58:15 +00002859 CCInfo.AnalyzeCallResult(Ins, RetCC_X86);
Evan Cheng13617962010-04-30 01:12:32 +00002860 for (unsigned i = 0, e = RVLocs.size(); i != e; ++i) {
Evan Chengf5b9d6c2010-03-20 02:58:15 +00002861 CCValAssign &VA = RVLocs[i];
2862 if (VA.getLocReg() == X86::ST0 || VA.getLocReg() == X86::ST1)
2863 return false;
2864 }
2865 }
2866
Evan Cheng13617962010-04-30 01:12:32 +00002867 // If the calling conventions do not match, then we'd better make sure the
2868 // results are returned in the same way as what the caller expects.
2869 if (!CCMatch) {
2870 SmallVector<CCValAssign, 16> RVLocs1;
Eric Christopher471e4222011-06-08 23:55:35 +00002871 CCState CCInfo1(CalleeCC, false, DAG.getMachineFunction(),
Craig Topper0fbf3642012-04-23 03:28:34 +00002872 getTargetMachine(), RVLocs1, *DAG.getContext());
Evan Cheng13617962010-04-30 01:12:32 +00002873 CCInfo1.AnalyzeCallResult(Ins, RetCC_X86);
2874
2875 SmallVector<CCValAssign, 16> RVLocs2;
Eric Christopher471e4222011-06-08 23:55:35 +00002876 CCState CCInfo2(CallerCC, false, DAG.getMachineFunction(),
Craig Topper0fbf3642012-04-23 03:28:34 +00002877 getTargetMachine(), RVLocs2, *DAG.getContext());
Evan Cheng13617962010-04-30 01:12:32 +00002878 CCInfo2.AnalyzeCallResult(Ins, RetCC_X86);
2879
2880 if (RVLocs1.size() != RVLocs2.size())
2881 return false;
2882 for (unsigned i = 0, e = RVLocs1.size(); i != e; ++i) {
2883 if (RVLocs1[i].isRegLoc() != RVLocs2[i].isRegLoc())
2884 return false;
2885 if (RVLocs1[i].getLocInfo() != RVLocs2[i].getLocInfo())
2886 return false;
2887 if (RVLocs1[i].isRegLoc()) {
2888 if (RVLocs1[i].getLocReg() != RVLocs2[i].getLocReg())
2889 return false;
2890 } else {
2891 if (RVLocs1[i].getLocMemOffset() != RVLocs2[i].getLocMemOffset())
2892 return false;
2893 }
2894 }
2895 }
2896
Evan Chenga6bff982010-01-30 01:22:00 +00002897 // If the callee takes no arguments then go on to check the results of the
2898 // call.
2899 if (!Outs.empty()) {
2900 // Check if stack adjustment is needed. For now, do not do this if any
2901 // argument is passed on the stack.
2902 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002903 CCState CCInfo(CalleeCC, isVarArg, DAG.getMachineFunction(),
Craig Topper0fbf3642012-04-23 03:28:34 +00002904 getTargetMachine(), ArgLocs, *DAG.getContext());
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00002905
2906 // Allocate shadow area for Win64
2907 if (Subtarget->isTargetWin64()) {
2908 CCInfo.AllocateStack(32, 8);
2909 }
2910
Duncan Sands45907662010-10-31 13:21:44 +00002911 CCInfo.AnalyzeCallOperands(Outs, CC_X86);
Stuart Hastings6db2c2f2011-05-17 16:59:46 +00002912 if (CCInfo.getNextStackOffset()) {
Evan Chengb2c92902010-02-02 02:22:50 +00002913 MachineFunction &MF = DAG.getMachineFunction();
2914 if (MF.getInfo<X86MachineFunctionInfo>()->getBytesToPopOnReturn())
2915 return false;
Evan Chengb2c92902010-02-02 02:22:50 +00002916
2917 // Check if the arguments are already laid out in the right way as
2918 // the caller's fixed stack objects.
2919 MachineFrameInfo *MFI = MF.getFrameInfo();
Evan Cheng5f941932010-02-05 02:21:12 +00002920 const MachineRegisterInfo *MRI = &MF.getRegInfo();
2921 const X86InstrInfo *TII =
Roman Divacky59324292012-09-05 22:26:57 +00002922 ((const X86TargetMachine&)getTargetMachine()).getInstrInfo();
Evan Chengb2c92902010-02-02 02:22:50 +00002923 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2924 CCValAssign &VA = ArgLocs[i];
Dan Gohmanc9403652010-07-07 15:54:55 +00002925 SDValue Arg = OutVals[i];
Evan Chengb2c92902010-02-02 02:22:50 +00002926 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Evan Chengb2c92902010-02-02 02:22:50 +00002927 if (VA.getLocInfo() == CCValAssign::Indirect)
2928 return false;
2929 if (!VA.isRegLoc()) {
Evan Cheng5f941932010-02-05 02:21:12 +00002930 if (!MatchingStackOffset(Arg, VA.getLocMemOffset(), Flags,
2931 MFI, MRI, TII))
Evan Chengb2c92902010-02-02 02:22:50 +00002932 return false;
2933 }
2934 }
2935 }
Evan Cheng9c044672010-05-29 01:35:22 +00002936
2937 // If the tailcall address may be in a register, then make sure it's
2938 // possible to register allocate for it. In 32-bit, the call address can
2939 // only target EAX, EDX, or ECX since the tail call must be scheduled after
Evan Chengdedd9742010-07-14 06:44:01 +00002940 // callee-saved registers are restored. These happen to be the same
2941 // registers used to pass 'inreg' arguments so watch out for those.
2942 if (!Subtarget->is64Bit() &&
2943 !isa<GlobalAddressSDNode>(Callee) &&
Evan Cheng9c044672010-05-29 01:35:22 +00002944 !isa<ExternalSymbolSDNode>(Callee)) {
Evan Cheng9c044672010-05-29 01:35:22 +00002945 unsigned NumInRegs = 0;
2946 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2947 CCValAssign &VA = ArgLocs[i];
Evan Chengdedd9742010-07-14 06:44:01 +00002948 if (!VA.isRegLoc())
2949 continue;
2950 unsigned Reg = VA.getLocReg();
2951 switch (Reg) {
2952 default: break;
2953 case X86::EAX: case X86::EDX: case X86::ECX:
2954 if (++NumInRegs == 3)
Evan Cheng9c044672010-05-29 01:35:22 +00002955 return false;
Evan Chengdedd9742010-07-14 06:44:01 +00002956 break;
Evan Cheng9c044672010-05-29 01:35:22 +00002957 }
2958 }
2959 }
Evan Chenga6bff982010-01-30 01:22:00 +00002960 }
Evan Chengb1712452010-01-27 06:25:16 +00002961
Evan Cheng86809cc2010-02-03 03:28:02 +00002962 return true;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002963}
2964
Dan Gohman3df24e62008-09-03 23:12:08 +00002965FastISel *
Bob Wilsond49edb72012-08-03 04:06:28 +00002966X86TargetLowering::createFastISel(FunctionLoweringInfo &funcInfo,
2967 const TargetLibraryInfo *libInfo) const {
2968 return X86::createFastISel(funcInfo, libInfo);
Dan Gohmand9f3c482008-08-19 21:32:53 +00002969}
2970
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00002971//===----------------------------------------------------------------------===//
2972// Other Lowering Hooks
2973//===----------------------------------------------------------------------===//
2974
Bruno Cardoso Lopese654b562010-09-01 00:51:36 +00002975static bool MayFoldLoad(SDValue Op) {
2976 return Op.hasOneUse() && ISD::isNormalLoad(Op.getNode());
2977}
2978
2979static bool MayFoldIntoStore(SDValue Op) {
2980 return Op.hasOneUse() && ISD::isNormalStore(*Op.getNode()->use_begin());
2981}
2982
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002983static bool isTargetShuffle(unsigned Opcode) {
2984 switch(Opcode) {
2985 default: return false;
2986 case X86ISD::PSHUFD:
2987 case X86ISD::PSHUFHW:
2988 case X86ISD::PSHUFLW:
Craig Topperb3982da2011-12-31 23:50:21 +00002989 case X86ISD::SHUFP:
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00002990 case X86ISD::PALIGN:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002991 case X86ISD::MOVLHPS:
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00002992 case X86ISD::MOVLHPD:
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00002993 case X86ISD::MOVHLPS:
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00002994 case X86ISD::MOVLPS:
2995 case X86ISD::MOVLPD:
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00002996 case X86ISD::MOVSHDUP:
Bruno Cardoso Lopes013bb3d2010-08-31 22:35:05 +00002997 case X86ISD::MOVSLDUP:
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00002998 case X86ISD::MOVDDUP:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002999 case X86ISD::MOVSS:
3000 case X86ISD::MOVSD:
Craig Topper34671b82011-12-06 08:21:25 +00003001 case X86ISD::UNPCKL:
3002 case X86ISD::UNPCKH:
Craig Topper316cd2a2011-11-30 06:25:25 +00003003 case X86ISD::VPERMILP:
Craig Topperec24e612011-11-30 07:47:51 +00003004 case X86ISD::VPERM2X128:
Craig Topperbdcbcb32012-05-06 18:54:26 +00003005 case X86ISD::VPERMI:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003006 return true;
3007 }
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003008}
3009
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00003010static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
Craig Topper3d092db2012-03-21 02:14:01 +00003011 SDValue V1, SelectionDAG &DAG) {
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00003012 switch(Opc) {
3013 default: llvm_unreachable("Unknown x86 shuffle node");
3014 case X86ISD::MOVSHDUP:
Bruno Cardoso Lopes013bb3d2010-08-31 22:35:05 +00003015 case X86ISD::MOVSLDUP:
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00003016 case X86ISD::MOVDDUP:
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00003017 return DAG.getNode(Opc, dl, VT, V1);
3018 }
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00003019}
3020
3021static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
Craig Topper3d092db2012-03-21 02:14:01 +00003022 SDValue V1, unsigned TargetMask,
3023 SelectionDAG &DAG) {
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00003024 switch(Opc) {
3025 default: llvm_unreachable("Unknown x86 shuffle node");
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00003026 case X86ISD::PSHUFD:
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00003027 case X86ISD::PSHUFHW:
3028 case X86ISD::PSHUFLW:
Craig Topper316cd2a2011-11-30 06:25:25 +00003029 case X86ISD::VPERMILP:
Craig Topper8325c112012-04-16 00:41:45 +00003030 case X86ISD::VPERMI:
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00003031 return DAG.getNode(Opc, dl, VT, V1, DAG.getConstant(TargetMask, MVT::i8));
3032 }
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00003033}
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00003034
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00003035static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
Craig Topper3d092db2012-03-21 02:14:01 +00003036 SDValue V1, SDValue V2, unsigned TargetMask,
3037 SelectionDAG &DAG) {
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00003038 switch(Opc) {
3039 default: llvm_unreachable("Unknown x86 shuffle node");
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00003040 case X86ISD::PALIGN:
Craig Topperb3982da2011-12-31 23:50:21 +00003041 case X86ISD::SHUFP:
Craig Topperec24e612011-11-30 07:47:51 +00003042 case X86ISD::VPERM2X128:
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00003043 return DAG.getNode(Opc, dl, VT, V1, V2,
3044 DAG.getConstant(TargetMask, MVT::i8));
3045 }
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00003046}
3047
3048static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
3049 SDValue V1, SDValue V2, SelectionDAG &DAG) {
3050 switch(Opc) {
3051 default: llvm_unreachable("Unknown x86 shuffle node");
3052 case X86ISD::MOVLHPS:
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00003053 case X86ISD::MOVLHPD:
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00003054 case X86ISD::MOVHLPS:
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00003055 case X86ISD::MOVLPS:
3056 case X86ISD::MOVLPD:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003057 case X86ISD::MOVSS:
3058 case X86ISD::MOVSD:
Craig Topper34671b82011-12-06 08:21:25 +00003059 case X86ISD::UNPCKL:
3060 case X86ISD::UNPCKH:
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00003061 return DAG.getNode(Opc, dl, VT, V1, V2);
3062 }
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00003063}
3064
Dan Gohmand858e902010-04-17 15:26:15 +00003065SDValue X86TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) const {
Anton Korobeynikova2780e12007-08-15 17:12:32 +00003066 MachineFunction &MF = DAG.getMachineFunction();
3067 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
3068 int ReturnAddrIndex = FuncInfo->getRAIndex();
3069
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00003070 if (ReturnAddrIndex == 0) {
3071 // Set up a frame object for the return address.
Michael Liaoaa3c2c02012-10-25 06:29:14 +00003072 unsigned SlotSize = RegInfo->getSlotSize();
David Greene3f2bf852009-11-12 20:49:22 +00003073 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(SlotSize, -SlotSize,
Evan Chenged2ae132010-07-03 00:40:23 +00003074 false);
Anton Korobeynikova2780e12007-08-15 17:12:32 +00003075 FuncInfo->setRAIndex(ReturnAddrIndex);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00003076 }
3077
Evan Cheng25ab6902006-09-08 06:48:29 +00003078 return DAG.getFrameIndex(ReturnAddrIndex, getPointerTy());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00003079}
3080
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00003081bool X86::isOffsetSuitableForCodeModel(int64_t Offset, CodeModel::Model M,
3082 bool hasSymbolicDisplacement) {
3083 // Offset should fit into 32 bit immediate field.
Benjamin Kramer34247a02010-03-29 21:13:41 +00003084 if (!isInt<32>(Offset))
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00003085 return false;
3086
3087 // If we don't have a symbolic displacement - we don't have any extra
3088 // restrictions.
3089 if (!hasSymbolicDisplacement)
3090 return true;
3091
3092 // FIXME: Some tweaks might be needed for medium code model.
3093 if (M != CodeModel::Small && M != CodeModel::Kernel)
3094 return false;
3095
3096 // For small code model we assume that latest object is 16MB before end of 31
3097 // bits boundary. We may also accept pretty large negative constants knowing
3098 // that all objects are in the positive half of address space.
3099 if (M == CodeModel::Small && Offset < 16*1024*1024)
3100 return true;
3101
3102 // For kernel code model we know that all object resist in the negative half
3103 // of 32bits address space. We may not accept negative offsets, since they may
3104 // be just off and we may accept pretty large positive ones.
3105 if (M == CodeModel::Kernel && Offset > 0)
3106 return true;
3107
3108 return false;
3109}
3110
Evan Chengef41ff62011-06-23 17:54:54 +00003111/// isCalleePop - Determines whether the callee is required to pop its
3112/// own arguments. Callee pop is necessary to support tail calls.
3113bool X86::isCalleePop(CallingConv::ID CallingConv,
3114 bool is64Bit, bool IsVarArg, bool TailCallOpt) {
3115 if (IsVarArg)
3116 return false;
3117
3118 switch (CallingConv) {
3119 default:
3120 return false;
3121 case CallingConv::X86_StdCall:
3122 return !is64Bit;
3123 case CallingConv::X86_FastCall:
3124 return !is64Bit;
3125 case CallingConv::X86_ThisCall:
3126 return !is64Bit;
3127 case CallingConv::Fast:
3128 return TailCallOpt;
3129 case CallingConv::GHC:
3130 return TailCallOpt;
Duncan Sandsdc7f1742012-11-16 12:36:39 +00003131 case CallingConv::HiPE:
3132 return TailCallOpt;
Evan Chengef41ff62011-06-23 17:54:54 +00003133 }
3134}
3135
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003136/// TranslateX86CC - do a one to one translation of a ISD::CondCode to the X86
3137/// specific condition code, returning the condition code and the LHS/RHS of the
3138/// comparison to make.
3139static unsigned TranslateX86CC(ISD::CondCode SetCCOpcode, bool isFP,
3140 SDValue &LHS, SDValue &RHS, SelectionDAG &DAG) {
Evan Chengd9558e02006-01-06 00:43:03 +00003141 if (!isFP) {
Chris Lattnerbfd68a72006-09-13 17:04:54 +00003142 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS)) {
3143 if (SetCCOpcode == ISD::SETGT && RHSC->isAllOnesValue()) {
3144 // X > -1 -> X == 0, jump !sign.
3145 RHS = DAG.getConstant(0, RHS.getValueType());
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003146 return X86::COND_NS;
Craig Topper69947b92012-04-23 06:57:04 +00003147 }
3148 if (SetCCOpcode == ISD::SETLT && RHSC->isNullValue()) {
Chris Lattnerbfd68a72006-09-13 17:04:54 +00003149 // X < 0 -> X == 0, jump on sign.
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003150 return X86::COND_S;
Craig Topper69947b92012-04-23 06:57:04 +00003151 }
3152 if (SetCCOpcode == ISD::SETLT && RHSC->getZExtValue() == 1) {
Dan Gohman5f6913c2007-09-17 14:49:27 +00003153 // X < 1 -> X <= 0
3154 RHS = DAG.getConstant(0, RHS.getValueType());
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003155 return X86::COND_LE;
Chris Lattnerbfd68a72006-09-13 17:04:54 +00003156 }
Chris Lattnerf9570512006-09-13 03:22:10 +00003157 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00003158
Evan Chengd9558e02006-01-06 00:43:03 +00003159 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00003160 default: llvm_unreachable("Invalid integer condition!");
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003161 case ISD::SETEQ: return X86::COND_E;
3162 case ISD::SETGT: return X86::COND_G;
3163 case ISD::SETGE: return X86::COND_GE;
3164 case ISD::SETLT: return X86::COND_L;
3165 case ISD::SETLE: return X86::COND_LE;
3166 case ISD::SETNE: return X86::COND_NE;
3167 case ISD::SETULT: return X86::COND_B;
3168 case ISD::SETUGT: return X86::COND_A;
3169 case ISD::SETULE: return X86::COND_BE;
3170 case ISD::SETUGE: return X86::COND_AE;
Evan Chengd9558e02006-01-06 00:43:03 +00003171 }
Chris Lattner4c78e022008-12-23 23:42:27 +00003172 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003173
Chris Lattner4c78e022008-12-23 23:42:27 +00003174 // First determine if it is required or is profitable to flip the operands.
Duncan Sands4047f4a2008-10-24 13:03:10 +00003175
Chris Lattner4c78e022008-12-23 23:42:27 +00003176 // If LHS is a foldable load, but RHS is not, flip the condition.
Rafael Espindolaf297c932011-02-03 03:58:05 +00003177 if (ISD::isNON_EXTLoad(LHS.getNode()) &&
3178 !ISD::isNON_EXTLoad(RHS.getNode())) {
Chris Lattner4c78e022008-12-23 23:42:27 +00003179 SetCCOpcode = getSetCCSwappedOperands(SetCCOpcode);
3180 std::swap(LHS, RHS);
Evan Cheng4d46d0a2008-08-28 23:48:31 +00003181 }
3182
Chris Lattner4c78e022008-12-23 23:42:27 +00003183 switch (SetCCOpcode) {
3184 default: break;
3185 case ISD::SETOLT:
3186 case ISD::SETOLE:
3187 case ISD::SETUGT:
3188 case ISD::SETUGE:
3189 std::swap(LHS, RHS);
3190 break;
3191 }
3192
3193 // On a floating point condition, the flags are set as follows:
3194 // ZF PF CF op
3195 // 0 | 0 | 0 | X > Y
3196 // 0 | 0 | 1 | X < Y
3197 // 1 | 0 | 0 | X == Y
3198 // 1 | 1 | 1 | unordered
3199 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00003200 default: llvm_unreachable("Condcode should be pre-legalized away");
Chris Lattner4c78e022008-12-23 23:42:27 +00003201 case ISD::SETUEQ:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003202 case ISD::SETEQ: return X86::COND_E;
Chris Lattner4c78e022008-12-23 23:42:27 +00003203 case ISD::SETOLT: // flipped
3204 case ISD::SETOGT:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003205 case ISD::SETGT: return X86::COND_A;
Chris Lattner4c78e022008-12-23 23:42:27 +00003206 case ISD::SETOLE: // flipped
3207 case ISD::SETOGE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003208 case ISD::SETGE: return X86::COND_AE;
Chris Lattner4c78e022008-12-23 23:42:27 +00003209 case ISD::SETUGT: // flipped
3210 case ISD::SETULT:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003211 case ISD::SETLT: return X86::COND_B;
Chris Lattner4c78e022008-12-23 23:42:27 +00003212 case ISD::SETUGE: // flipped
3213 case ISD::SETULE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003214 case ISD::SETLE: return X86::COND_BE;
Chris Lattner4c78e022008-12-23 23:42:27 +00003215 case ISD::SETONE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003216 case ISD::SETNE: return X86::COND_NE;
3217 case ISD::SETUO: return X86::COND_P;
3218 case ISD::SETO: return X86::COND_NP;
Dan Gohman1a492952009-10-20 16:22:37 +00003219 case ISD::SETOEQ:
3220 case ISD::SETUNE: return X86::COND_INVALID;
Chris Lattner4c78e022008-12-23 23:42:27 +00003221 }
Evan Chengd9558e02006-01-06 00:43:03 +00003222}
3223
Evan Cheng4a460802006-01-11 00:33:36 +00003224/// hasFPCMov - is there a floating point cmov for the specific X86 condition
3225/// code. Current x86 isa includes the following FP cmov instructions:
Evan Chengaaca22c2006-01-10 20:26:56 +00003226/// fcmovb, fcomvbe, fcomve, fcmovu, fcmovae, fcmova, fcmovne, fcmovnu.
Evan Cheng4a460802006-01-11 00:33:36 +00003227static bool hasFPCMov(unsigned X86CC) {
Evan Chengaaca22c2006-01-10 20:26:56 +00003228 switch (X86CC) {
3229 default:
3230 return false;
Chris Lattner7fbe9722006-10-20 17:42:20 +00003231 case X86::COND_B:
3232 case X86::COND_BE:
3233 case X86::COND_E:
3234 case X86::COND_P:
3235 case X86::COND_A:
3236 case X86::COND_AE:
3237 case X86::COND_NE:
3238 case X86::COND_NP:
Evan Chengaaca22c2006-01-10 20:26:56 +00003239 return true;
3240 }
3241}
3242
Evan Chengeb2f9692009-10-27 19:56:55 +00003243/// isFPImmLegal - Returns true if the target can instruction select the
3244/// specified FP immediate natively. If false, the legalizer will
3245/// materialize the FP immediate as a load from a constant pool.
Evan Chenga1eaa3c2009-10-28 01:43:28 +00003246bool X86TargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
Evan Chengeb2f9692009-10-27 19:56:55 +00003247 for (unsigned i = 0, e = LegalFPImmediates.size(); i != e; ++i) {
3248 if (Imm.bitwiseIsEqual(LegalFPImmediates[i]))
3249 return true;
3250 }
3251 return false;
3252}
3253
Nate Begeman9008ca62009-04-27 18:41:29 +00003254/// isUndefOrInRange - Return true if Val is undef or if its value falls within
3255/// the specified range (L, H].
3256static bool isUndefOrInRange(int Val, int Low, int Hi) {
3257 return (Val < 0) || (Val >= Low && Val < Hi);
3258}
3259
3260/// isUndefOrEqual - Val is either less than zero (undef) or equal to the
3261/// specified value.
3262static bool isUndefOrEqual(int Val, int CmpVal) {
Jakub Staszakb2af3a02012-12-06 18:22:59 +00003263 return (Val < 0 || Val == CmpVal);
Evan Chengc5cdff22006-04-07 21:53:05 +00003264}
3265
Benjamin Kramerd9b0b022012-06-02 10:20:22 +00003266/// isSequentialOrUndefInRange - Return true if every element in Mask, beginning
Bruno Cardoso Lopes4002d7e2011-08-12 21:54:42 +00003267/// from position Pos and ending in Pos+Size, falls within the specified
3268/// sequential range (L, L+Pos]. or is undef.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003269static bool isSequentialOrUndefInRange(ArrayRef<int> Mask,
Craig Topperb6072642012-05-03 07:26:59 +00003270 unsigned Pos, unsigned Size, int Low) {
3271 for (unsigned i = Pos, e = Pos+Size; i != e; ++i, ++Low)
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003272 if (!isUndefOrEqual(Mask[i], Low))
3273 return false;
3274 return true;
3275}
3276
Nate Begeman9008ca62009-04-27 18:41:29 +00003277/// isPSHUFDMask - Return true if the node specifies a shuffle of elements that
3278/// is suitable for input to PSHUFD or PSHUFW. That is, it doesn't reference
3279/// the second operand.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003280static bool isPSHUFDMask(ArrayRef<int> Mask, EVT VT) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00003281 if (VT == MVT::v4f32 || VT == MVT::v4i32 )
Nate Begeman9008ca62009-04-27 18:41:29 +00003282 return (Mask[0] < 4 && Mask[1] < 4 && Mask[2] < 4 && Mask[3] < 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00003283 if (VT == MVT::v2f64 || VT == MVT::v2i64)
Nate Begeman9008ca62009-04-27 18:41:29 +00003284 return (Mask[0] < 2 && Mask[1] < 2);
3285 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003286}
3287
Nate Begeman9008ca62009-04-27 18:41:29 +00003288/// isPSHUFHWMask - Return true if the node specifies a shuffle of elements that
3289/// is suitable for input to PSHUFHW.
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00003290static bool isPSHUFHWMask(ArrayRef<int> Mask, EVT VT, bool HasInt256) {
3291 if (VT != MVT::v8i16 && (!HasInt256 || VT != MVT::v16i16))
Evan Cheng0188ecb2006-03-22 18:59:22 +00003292 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003293
Nate Begeman9008ca62009-04-27 18:41:29 +00003294 // Lower quadword copied in order or undef.
Craig Topperc612d792012-01-02 09:17:37 +00003295 if (!isSequentialOrUndefInRange(Mask, 0, 4, 0))
3296 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003297
Evan Cheng506d3df2006-03-29 23:07:14 +00003298 // Upper quadword shuffled.
Craig Topperc612d792012-01-02 09:17:37 +00003299 for (unsigned i = 4; i != 8; ++i)
Craig Toppera9a568a2012-05-02 08:03:44 +00003300 if (!isUndefOrInRange(Mask[i], 4, 8))
Evan Cheng506d3df2006-03-29 23:07:14 +00003301 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003302
Craig Toppera9a568a2012-05-02 08:03:44 +00003303 if (VT == MVT::v16i16) {
3304 // Lower quadword copied in order or undef.
3305 if (!isSequentialOrUndefInRange(Mask, 8, 4, 8))
3306 return false;
3307
3308 // Upper quadword shuffled.
3309 for (unsigned i = 12; i != 16; ++i)
3310 if (!isUndefOrInRange(Mask[i], 12, 16))
3311 return false;
3312 }
3313
Evan Cheng506d3df2006-03-29 23:07:14 +00003314 return true;
3315}
3316
Nate Begeman9008ca62009-04-27 18:41:29 +00003317/// isPSHUFLWMask - Return true if the node specifies a shuffle of elements that
3318/// is suitable for input to PSHUFLW.
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00003319static bool isPSHUFLWMask(ArrayRef<int> Mask, EVT VT, bool HasInt256) {
3320 if (VT != MVT::v8i16 && (!HasInt256 || VT != MVT::v16i16))
Evan Cheng506d3df2006-03-29 23:07:14 +00003321 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003322
Rafael Espindola15684b22009-04-24 12:40:33 +00003323 // Upper quadword copied in order.
Craig Topperc612d792012-01-02 09:17:37 +00003324 if (!isSequentialOrUndefInRange(Mask, 4, 4, 4))
3325 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003326
Rafael Espindola15684b22009-04-24 12:40:33 +00003327 // Lower quadword shuffled.
Craig Topperc612d792012-01-02 09:17:37 +00003328 for (unsigned i = 0; i != 4; ++i)
Craig Toppera9a568a2012-05-02 08:03:44 +00003329 if (!isUndefOrInRange(Mask[i], 0, 4))
Rafael Espindola15684b22009-04-24 12:40:33 +00003330 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003331
Craig Toppera9a568a2012-05-02 08:03:44 +00003332 if (VT == MVT::v16i16) {
3333 // Upper quadword copied in order.
3334 if (!isSequentialOrUndefInRange(Mask, 12, 4, 12))
3335 return false;
3336
3337 // Lower quadword shuffled.
3338 for (unsigned i = 8; i != 12; ++i)
3339 if (!isUndefOrInRange(Mask[i], 8, 12))
3340 return false;
3341 }
3342
Rafael Espindola15684b22009-04-24 12:40:33 +00003343 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00003344}
3345
Nate Begemana09008b2009-10-19 02:17:23 +00003346/// isPALIGNRMask - Return true if the node specifies a shuffle of elements that
3347/// is suitable for input to PALIGNR.
Craig Topper0e2037b2012-01-20 05:53:00 +00003348static bool isPALIGNRMask(ArrayRef<int> Mask, EVT VT,
3349 const X86Subtarget *Subtarget) {
3350 if ((VT.getSizeInBits() == 128 && !Subtarget->hasSSSE3()) ||
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00003351 (VT.getSizeInBits() == 256 && !Subtarget->hasInt256()))
Bruno Cardoso Lopes9065d4b2011-07-29 01:30:59 +00003352 return false;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003353
Craig Topper0e2037b2012-01-20 05:53:00 +00003354 unsigned NumElts = VT.getVectorNumElements();
3355 unsigned NumLanes = VT.getSizeInBits()/128;
3356 unsigned NumLaneElts = NumElts/NumLanes;
3357
3358 // Do not handle 64-bit element shuffles with palignr.
3359 if (NumLaneElts == 2)
Nate Begemana09008b2009-10-19 02:17:23 +00003360 return false;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003361
Craig Topper0e2037b2012-01-20 05:53:00 +00003362 for (unsigned l = 0; l != NumElts; l+=NumLaneElts) {
3363 unsigned i;
3364 for (i = 0; i != NumLaneElts; ++i) {
3365 if (Mask[i+l] >= 0)
3366 break;
3367 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00003368
Craig Topper0e2037b2012-01-20 05:53:00 +00003369 // Lane is all undef, go to next lane
3370 if (i == NumLaneElts)
3371 continue;
Nate Begemana09008b2009-10-19 02:17:23 +00003372
Craig Topper0e2037b2012-01-20 05:53:00 +00003373 int Start = Mask[i+l];
Nate Begemana09008b2009-10-19 02:17:23 +00003374
Craig Topper0e2037b2012-01-20 05:53:00 +00003375 // Make sure its in this lane in one of the sources
3376 if (!isUndefOrInRange(Start, l, l+NumLaneElts) &&
3377 !isUndefOrInRange(Start, l+NumElts, l+NumElts+NumLaneElts))
Nate Begemana09008b2009-10-19 02:17:23 +00003378 return false;
Craig Topper0e2037b2012-01-20 05:53:00 +00003379
3380 // If not lane 0, then we must match lane 0
3381 if (l != 0 && Mask[i] >= 0 && !isUndefOrEqual(Start, Mask[i]+l))
3382 return false;
3383
3384 // Correct second source to be contiguous with first source
3385 if (Start >= (int)NumElts)
3386 Start -= NumElts - NumLaneElts;
3387
3388 // Make sure we're shifting in the right direction.
3389 if (Start <= (int)(i+l))
3390 return false;
3391
3392 Start -= i;
3393
3394 // Check the rest of the elements to see if they are consecutive.
3395 for (++i; i != NumLaneElts; ++i) {
3396 int Idx = Mask[i+l];
3397
3398 // Make sure its in this lane
3399 if (!isUndefOrInRange(Idx, l, l+NumLaneElts) &&
3400 !isUndefOrInRange(Idx, l+NumElts, l+NumElts+NumLaneElts))
3401 return false;
3402
3403 // If not lane 0, then we must match lane 0
3404 if (l != 0 && Mask[i] >= 0 && !isUndefOrEqual(Idx, Mask[i]+l))
3405 return false;
3406
3407 if (Idx >= (int)NumElts)
3408 Idx -= NumElts - NumLaneElts;
3409
3410 if (!isUndefOrEqual(Idx, Start+i))
3411 return false;
3412
3413 }
Nate Begemana09008b2009-10-19 02:17:23 +00003414 }
Craig Topper0e2037b2012-01-20 05:53:00 +00003415
Nate Begemana09008b2009-10-19 02:17:23 +00003416 return true;
3417}
3418
Craig Topper1a7700a2012-01-19 08:19:12 +00003419/// CommuteVectorShuffleMask - Change values in a shuffle permute mask assuming
3420/// the two vector operands have swapped position.
3421static void CommuteVectorShuffleMask(SmallVectorImpl<int> &Mask,
3422 unsigned NumElems) {
3423 for (unsigned i = 0; i != NumElems; ++i) {
3424 int idx = Mask[i];
3425 if (idx < 0)
3426 continue;
3427 else if (idx < (int)NumElems)
3428 Mask[i] = idx + NumElems;
3429 else
3430 Mask[i] = idx - NumElems;
3431 }
3432}
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00003433
Craig Topper1a7700a2012-01-19 08:19:12 +00003434/// isSHUFPMask - Return true if the specified VECTOR_SHUFFLE operand
3435/// specifies a shuffle of elements that is suitable for input to 128/256-bit
3436/// SHUFPS and SHUFPD. If Commuted is true, then it checks for sources to be
3437/// reverse of what x86 shuffles want.
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00003438static bool isSHUFPMask(ArrayRef<int> Mask, EVT VT, bool HasFp256,
Craig Topper1a7700a2012-01-19 08:19:12 +00003439 bool Commuted = false) {
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00003440 if (!HasFp256 && VT.getSizeInBits() == 256)
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00003441 return false;
3442
Craig Topper1a7700a2012-01-19 08:19:12 +00003443 unsigned NumElems = VT.getVectorNumElements();
3444 unsigned NumLanes = VT.getSizeInBits()/128;
3445 unsigned NumLaneElems = NumElems/NumLanes;
3446
3447 if (NumLaneElems != 2 && NumLaneElems != 4)
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00003448 return false;
3449
3450 // VSHUFPSY divides the resulting vector into 4 chunks.
3451 // The sources are also splitted into 4 chunks, and each destination
3452 // chunk must come from a different source chunk.
3453 //
3454 // SRC1 => X7 X6 X5 X4 X3 X2 X1 X0
3455 // SRC2 => Y7 Y6 Y5 Y4 Y3 Y2 Y1 Y9
3456 //
3457 // DST => Y7..Y4, Y7..Y4, X7..X4, X7..X4,
3458 // Y3..Y0, Y3..Y0, X3..X0, X3..X0
3459 //
Craig Topper9d7025b2011-11-27 21:41:12 +00003460 // VSHUFPDY divides the resulting vector into 4 chunks.
3461 // The sources are also splitted into 4 chunks, and each destination
3462 // chunk must come from a different source chunk.
3463 //
3464 // SRC1 => X3 X2 X1 X0
3465 // SRC2 => Y3 Y2 Y1 Y0
3466 //
3467 // DST => Y3..Y2, X3..X2, Y1..Y0, X1..X0
3468 //
Craig Topper1a7700a2012-01-19 08:19:12 +00003469 unsigned HalfLaneElems = NumLaneElems/2;
3470 for (unsigned l = 0; l != NumElems; l += NumLaneElems) {
3471 for (unsigned i = 0; i != NumLaneElems; ++i) {
3472 int Idx = Mask[i+l];
3473 unsigned RngStart = l + ((Commuted == (i<HalfLaneElems)) ? NumElems : 0);
3474 if (!isUndefOrInRange(Idx, RngStart, RngStart+NumLaneElems))
3475 return false;
3476 // For VSHUFPSY, the mask of the second half must be the same as the
3477 // first but with the appropriate offsets. This works in the same way as
3478 // VPERMILPS works with masks.
3479 if (NumElems != 8 || l == 0 || Mask[i] < 0)
3480 continue;
3481 if (!isUndefOrEqual(Idx, Mask[i]+l))
3482 return false;
Craig Topper1ff73d72011-12-06 04:59:07 +00003483 }
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00003484 }
3485
3486 return true;
3487}
3488
Evan Cheng2c0dbd02006-03-24 02:58:06 +00003489/// isMOVHLPSMask - Return true if the specified VECTOR_SHUFFLE operand
3490/// specifies a shuffle of elements that is suitable for input to MOVHLPS.
Craig Topperdd637ae2012-02-19 05:41:45 +00003491static bool isMOVHLPSMask(ArrayRef<int> Mask, EVT VT) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00003492 if (!VT.is128BitVector())
Bruno Cardoso Lopes61260052011-07-29 02:05:28 +00003493 return false;
3494
Craig Topper7a9a28b2012-08-12 02:23:29 +00003495 unsigned NumElems = VT.getVectorNumElements();
3496
Bruno Cardoso Lopes61260052011-07-29 02:05:28 +00003497 if (NumElems != 4)
Evan Cheng2c0dbd02006-03-24 02:58:06 +00003498 return false;
3499
Evan Cheng2064a2b2006-03-28 06:50:32 +00003500 // Expect bit0 == 6, bit1 == 7, bit2 == 2, bit3 == 3
Craig Topperdd637ae2012-02-19 05:41:45 +00003501 return isUndefOrEqual(Mask[0], 6) &&
3502 isUndefOrEqual(Mask[1], 7) &&
3503 isUndefOrEqual(Mask[2], 2) &&
3504 isUndefOrEqual(Mask[3], 3);
Evan Cheng6e56e2c2006-11-07 22:14:24 +00003505}
3506
Nate Begeman0b10b912009-11-07 23:17:15 +00003507/// isMOVHLPS_v_undef_Mask - Special case of isMOVHLPSMask for canonical form
3508/// of vector_shuffle v, v, <2, 3, 2, 3>, i.e. vector_shuffle v, undef,
3509/// <2, 3, 2, 3>
Craig Topperdd637ae2012-02-19 05:41:45 +00003510static bool isMOVHLPS_v_undef_Mask(ArrayRef<int> Mask, EVT VT) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00003511 if (!VT.is128BitVector())
Bruno Cardoso Lopes61260052011-07-29 02:05:28 +00003512 return false;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003513
Craig Topper7a9a28b2012-08-12 02:23:29 +00003514 unsigned NumElems = VT.getVectorNumElements();
3515
Nate Begeman0b10b912009-11-07 23:17:15 +00003516 if (NumElems != 4)
3517 return false;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003518
Craig Topperdd637ae2012-02-19 05:41:45 +00003519 return isUndefOrEqual(Mask[0], 2) &&
3520 isUndefOrEqual(Mask[1], 3) &&
3521 isUndefOrEqual(Mask[2], 2) &&
3522 isUndefOrEqual(Mask[3], 3);
Nate Begeman0b10b912009-11-07 23:17:15 +00003523}
3524
Evan Cheng5ced1d82006-04-06 23:23:56 +00003525/// isMOVLPMask - Return true if the specified VECTOR_SHUFFLE operand
3526/// specifies a shuffle of elements that is suitable for input to MOVLP{S|D}.
Craig Topperdd637ae2012-02-19 05:41:45 +00003527static bool isMOVLPMask(ArrayRef<int> Mask, EVT VT) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00003528 if (!VT.is128BitVector())
Elena Demikhovsky021c0a22011-12-28 08:14:01 +00003529 return false;
3530
Craig Topperdd637ae2012-02-19 05:41:45 +00003531 unsigned NumElems = VT.getVectorNumElements();
Evan Cheng5ced1d82006-04-06 23:23:56 +00003532
Evan Cheng5ced1d82006-04-06 23:23:56 +00003533 if (NumElems != 2 && NumElems != 4)
3534 return false;
3535
Chad Rosier238ae312012-04-30 17:47:15 +00003536 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
Craig Topperdd637ae2012-02-19 05:41:45 +00003537 if (!isUndefOrEqual(Mask[i], i + NumElems))
Evan Chengc5cdff22006-04-07 21:53:05 +00003538 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003539
Chad Rosier238ae312012-04-30 17:47:15 +00003540 for (unsigned i = NumElems/2, e = NumElems; i != e; ++i)
Craig Topperdd637ae2012-02-19 05:41:45 +00003541 if (!isUndefOrEqual(Mask[i], i))
Evan Chengc5cdff22006-04-07 21:53:05 +00003542 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003543
3544 return true;
3545}
3546
Nate Begeman0b10b912009-11-07 23:17:15 +00003547/// isMOVLHPSMask - Return true if the specified VECTOR_SHUFFLE operand
3548/// specifies a shuffle of elements that is suitable for input to MOVLHPS.
Craig Topperdd637ae2012-02-19 05:41:45 +00003549static bool isMOVLHPSMask(ArrayRef<int> Mask, EVT VT) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00003550 if (!VT.is128BitVector())
3551 return false;
3552
Craig Topperdd637ae2012-02-19 05:41:45 +00003553 unsigned NumElems = VT.getVectorNumElements();
Evan Cheng5ced1d82006-04-06 23:23:56 +00003554
Craig Topper7a9a28b2012-08-12 02:23:29 +00003555 if (NumElems != 2 && NumElems != 4)
Evan Cheng5ced1d82006-04-06 23:23:56 +00003556 return false;
3557
Chad Rosier238ae312012-04-30 17:47:15 +00003558 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
Craig Topperdd637ae2012-02-19 05:41:45 +00003559 if (!isUndefOrEqual(Mask[i], i))
Evan Chengc5cdff22006-04-07 21:53:05 +00003560 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003561
Chad Rosier238ae312012-04-30 17:47:15 +00003562 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
3563 if (!isUndefOrEqual(Mask[i + e], i + NumElems))
Evan Chengc5cdff22006-04-07 21:53:05 +00003564 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003565
3566 return true;
3567}
3568
Elena Demikhovsky15963732012-06-26 08:04:10 +00003569//
3570// Some special combinations that can be optimized.
3571//
3572static
3573SDValue Compact8x32ShuffleNode(ShuffleVectorSDNode *SVOp,
3574 SelectionDAG &DAG) {
3575 EVT VT = SVOp->getValueType(0);
Elena Demikhovsky15963732012-06-26 08:04:10 +00003576 DebugLoc dl = SVOp->getDebugLoc();
3577
3578 if (VT != MVT::v8i32 && VT != MVT::v8f32)
3579 return SDValue();
3580
3581 ArrayRef<int> Mask = SVOp->getMask();
3582
3583 // These are the special masks that may be optimized.
3584 static const int MaskToOptimizeEven[] = {0, 8, 2, 10, 4, 12, 6, 14};
3585 static const int MaskToOptimizeOdd[] = {1, 9, 3, 11, 5, 13, 7, 15};
3586 bool MatchEvenMask = true;
3587 bool MatchOddMask = true;
3588 for (int i=0; i<8; ++i) {
3589 if (!isUndefOrEqual(Mask[i], MaskToOptimizeEven[i]))
3590 MatchEvenMask = false;
3591 if (!isUndefOrEqual(Mask[i], MaskToOptimizeOdd[i]))
3592 MatchOddMask = false;
3593 }
Elena Demikhovsky15963732012-06-26 08:04:10 +00003594
Elena Demikhovsky32510202012-09-04 12:49:02 +00003595 if (!MatchEvenMask && !MatchOddMask)
Elena Demikhovsky15963732012-06-26 08:04:10 +00003596 return SDValue();
Michael Liao471b9172012-10-03 23:43:52 +00003597
Elena Demikhovsky15963732012-06-26 08:04:10 +00003598 SDValue UndefNode = DAG.getNode(ISD::UNDEF, dl, VT);
3599
Elena Demikhovsky32510202012-09-04 12:49:02 +00003600 SDValue Op0 = SVOp->getOperand(0);
3601 SDValue Op1 = SVOp->getOperand(1);
3602
3603 if (MatchEvenMask) {
3604 // Shift the second operand right to 32 bits.
3605 static const int ShiftRightMask[] = {-1, 0, -1, 2, -1, 4, -1, 6 };
3606 Op1 = DAG.getVectorShuffle(VT, dl, Op1, UndefNode, ShiftRightMask);
3607 } else {
3608 // Shift the first operand left to 32 bits.
3609 static const int ShiftLeftMask[] = {1, -1, 3, -1, 5, -1, 7, -1 };
3610 Op0 = DAG.getVectorShuffle(VT, dl, Op0, UndefNode, ShiftLeftMask);
3611 }
3612 static const int BlendMask[] = {0, 9, 2, 11, 4, 13, 6, 15};
3613 return DAG.getVectorShuffle(VT, dl, Op0, Op1, BlendMask);
Elena Demikhovsky15963732012-06-26 08:04:10 +00003614}
3615
Evan Cheng0038e592006-03-28 00:39:58 +00003616/// isUNPCKLMask - Return true if the specified VECTOR_SHUFFLE operand
3617/// specifies a shuffle of elements that is suitable for input to UNPCKL.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003618static bool isUNPCKLMask(ArrayRef<int> Mask, EVT VT,
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00003619 bool HasInt256, bool V2IsSplat = false) {
Craig Topper94438ba2011-12-16 08:06:31 +00003620 unsigned NumElts = VT.getVectorNumElements();
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003621
3622 assert((VT.is128BitVector() || VT.is256BitVector()) &&
3623 "Unsupported vector type for unpckh");
3624
Craig Topper6347e862011-11-21 06:57:39 +00003625 if (VT.getSizeInBits() == 256 && NumElts != 4 && NumElts != 8 &&
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00003626 (!HasInt256 || (NumElts != 16 && NumElts != 32)))
Evan Cheng0038e592006-03-28 00:39:58 +00003627 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003628
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003629 // Handle 128 and 256-bit vector lengths. AVX defines UNPCK* to operate
3630 // independently on 128-bit lanes.
3631 unsigned NumLanes = VT.getSizeInBits()/128;
3632 unsigned NumLaneElts = NumElts/NumLanes;
David Greenea20244d2011-03-02 17:23:43 +00003633
Craig Topper94438ba2011-12-16 08:06:31 +00003634 for (unsigned l = 0; l != NumLanes; ++l) {
3635 for (unsigned i = l*NumLaneElts, j = l*NumLaneElts;
3636 i != (l+1)*NumLaneElts;
David Greenea20244d2011-03-02 17:23:43 +00003637 i += 2, ++j) {
3638 int BitI = Mask[i];
3639 int BitI1 = Mask[i+1];
3640 if (!isUndefOrEqual(BitI, j))
Evan Cheng39623da2006-04-20 08:58:49 +00003641 return false;
David Greenea20244d2011-03-02 17:23:43 +00003642 if (V2IsSplat) {
3643 if (!isUndefOrEqual(BitI1, NumElts))
3644 return false;
3645 } else {
3646 if (!isUndefOrEqual(BitI1, j + NumElts))
3647 return false;
3648 }
Evan Cheng39623da2006-04-20 08:58:49 +00003649 }
Evan Cheng0038e592006-03-28 00:39:58 +00003650 }
David Greenea20244d2011-03-02 17:23:43 +00003651
Evan Cheng0038e592006-03-28 00:39:58 +00003652 return true;
3653}
3654
Evan Cheng4fcb9222006-03-28 02:43:26 +00003655/// isUNPCKHMask - Return true if the specified VECTOR_SHUFFLE operand
3656/// specifies a shuffle of elements that is suitable for input to UNPCKH.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003657static bool isUNPCKHMask(ArrayRef<int> Mask, EVT VT,
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00003658 bool HasInt256, bool V2IsSplat = false) {
Craig Topper94438ba2011-12-16 08:06:31 +00003659 unsigned NumElts = VT.getVectorNumElements();
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003660
3661 assert((VT.is128BitVector() || VT.is256BitVector()) &&
3662 "Unsupported vector type for unpckh");
3663
Craig Topper6347e862011-11-21 06:57:39 +00003664 if (VT.getSizeInBits() == 256 && NumElts != 4 && NumElts != 8 &&
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00003665 (!HasInt256 || (NumElts != 16 && NumElts != 32)))
Evan Cheng4fcb9222006-03-28 02:43:26 +00003666 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003667
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003668 // Handle 128 and 256-bit vector lengths. AVX defines UNPCK* to operate
3669 // independently on 128-bit lanes.
3670 unsigned NumLanes = VT.getSizeInBits()/128;
3671 unsigned NumLaneElts = NumElts/NumLanes;
3672
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003673 for (unsigned l = 0; l != NumLanes; ++l) {
Craig Topper94438ba2011-12-16 08:06:31 +00003674 for (unsigned i = l*NumLaneElts, j = (l*NumLaneElts)+NumLaneElts/2;
3675 i != (l+1)*NumLaneElts; i += 2, ++j) {
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003676 int BitI = Mask[i];
3677 int BitI1 = Mask[i+1];
3678 if (!isUndefOrEqual(BitI, j))
Evan Cheng39623da2006-04-20 08:58:49 +00003679 return false;
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003680 if (V2IsSplat) {
3681 if (isUndefOrEqual(BitI1, NumElts))
3682 return false;
3683 } else {
3684 if (!isUndefOrEqual(BitI1, j+NumElts))
3685 return false;
3686 }
Evan Cheng39623da2006-04-20 08:58:49 +00003687 }
Evan Cheng4fcb9222006-03-28 02:43:26 +00003688 }
Evan Cheng4fcb9222006-03-28 02:43:26 +00003689 return true;
3690}
3691
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003692/// isUNPCKL_v_undef_Mask - Special case of isUNPCKLMask for canonical form
3693/// of vector_shuffle v, v, <0, 4, 1, 5>, i.e. vector_shuffle v, undef,
3694/// <0, 0, 1, 1>
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003695static bool isUNPCKL_v_undef_Mask(ArrayRef<int> Mask, EVT VT,
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00003696 bool HasInt256) {
Craig Topper94438ba2011-12-16 08:06:31 +00003697 unsigned NumElts = VT.getVectorNumElements();
3698
3699 assert((VT.is128BitVector() || VT.is256BitVector()) &&
3700 "Unsupported vector type for unpckh");
3701
3702 if (VT.getSizeInBits() == 256 && NumElts != 4 && NumElts != 8 &&
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00003703 (!HasInt256 || (NumElts != 16 && NumElts != 32)))
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003704 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003705
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00003706 // For 256-bit i64/f64, use MOVDDUPY instead, so reject the matching pattern
3707 // FIXME: Need a better way to get rid of this, there's no latency difference
3708 // between UNPCKLPD and MOVDDUP, the later should always be checked first and
3709 // the former later. We should also remove the "_undef" special mask.
Craig Topper94438ba2011-12-16 08:06:31 +00003710 if (NumElts == 4 && VT.getSizeInBits() == 256)
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00003711 return false;
3712
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003713 // Handle 128 and 256-bit vector lengths. AVX defines UNPCK* to operate
3714 // independently on 128-bit lanes.
Craig Topper94438ba2011-12-16 08:06:31 +00003715 unsigned NumLanes = VT.getSizeInBits()/128;
3716 unsigned NumLaneElts = NumElts/NumLanes;
David Greenea20244d2011-03-02 17:23:43 +00003717
Craig Topper94438ba2011-12-16 08:06:31 +00003718 for (unsigned l = 0; l != NumLanes; ++l) {
3719 for (unsigned i = l*NumLaneElts, j = l*NumLaneElts;
3720 i != (l+1)*NumLaneElts;
David Greenea20244d2011-03-02 17:23:43 +00003721 i += 2, ++j) {
3722 int BitI = Mask[i];
3723 int BitI1 = Mask[i+1];
3724
3725 if (!isUndefOrEqual(BitI, j))
3726 return false;
3727 if (!isUndefOrEqual(BitI1, j))
3728 return false;
3729 }
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003730 }
David Greenea20244d2011-03-02 17:23:43 +00003731
Rafael Espindola15684b22009-04-24 12:40:33 +00003732 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00003733}
3734
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003735/// isUNPCKH_v_undef_Mask - Special case of isUNPCKHMask for canonical form
3736/// of vector_shuffle v, v, <2, 6, 3, 7>, i.e. vector_shuffle v, undef,
3737/// <2, 2, 3, 3>
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00003738static bool isUNPCKH_v_undef_Mask(ArrayRef<int> Mask, EVT VT, bool HasInt256) {
Craig Topper94438ba2011-12-16 08:06:31 +00003739 unsigned NumElts = VT.getVectorNumElements();
3740
3741 assert((VT.is128BitVector() || VT.is256BitVector()) &&
3742 "Unsupported vector type for unpckh");
3743
3744 if (VT.getSizeInBits() == 256 && NumElts != 4 && NumElts != 8 &&
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00003745 (!HasInt256 || (NumElts != 16 && NumElts != 32)))
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003746 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003747
Craig Topper94438ba2011-12-16 08:06:31 +00003748 // Handle 128 and 256-bit vector lengths. AVX defines UNPCK* to operate
3749 // independently on 128-bit lanes.
3750 unsigned NumLanes = VT.getSizeInBits()/128;
3751 unsigned NumLaneElts = NumElts/NumLanes;
3752
3753 for (unsigned l = 0; l != NumLanes; ++l) {
3754 for (unsigned i = l*NumLaneElts, j = (l*NumLaneElts)+NumLaneElts/2;
3755 i != (l+1)*NumLaneElts; i += 2, ++j) {
3756 int BitI = Mask[i];
3757 int BitI1 = Mask[i+1];
3758 if (!isUndefOrEqual(BitI, j))
3759 return false;
3760 if (!isUndefOrEqual(BitI1, j))
3761 return false;
3762 }
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003763 }
Rafael Espindola15684b22009-04-24 12:40:33 +00003764 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00003765}
3766
Evan Cheng017dcc62006-04-21 01:05:10 +00003767/// isMOVLMask - Return true if the specified VECTOR_SHUFFLE operand
3768/// specifies a shuffle of elements that is suitable for input to MOVSS,
3769/// MOVSD, and MOVD, i.e. setting the lowest element.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003770static bool isMOVLMask(ArrayRef<int> Mask, EVT VT) {
Eli Friedman10415532009-06-06 06:05:10 +00003771 if (VT.getVectorElementType().getSizeInBits() < 32)
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003772 return false;
Craig Topper7a9a28b2012-08-12 02:23:29 +00003773 if (!VT.is128BitVector())
Elena Demikhovsky021c0a22011-12-28 08:14:01 +00003774 return false;
Eli Friedman10415532009-06-06 06:05:10 +00003775
Craig Topperc612d792012-01-02 09:17:37 +00003776 unsigned NumElts = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00003777
Nate Begeman9008ca62009-04-27 18:41:29 +00003778 if (!isUndefOrEqual(Mask[0], NumElts))
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003779 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003780
Craig Topperc612d792012-01-02 09:17:37 +00003781 for (unsigned i = 1; i != NumElts; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003782 if (!isUndefOrEqual(Mask[i], i))
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003783 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003784
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003785 return true;
3786}
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003787
Craig Topper70b883b2011-11-28 10:14:51 +00003788/// isVPERM2X128Mask - Match 256-bit shuffles where the elements are considered
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003789/// as permutations between 128-bit chunks or halves. As an example: this
3790/// shuffle bellow:
3791/// vector_shuffle <4, 5, 6, 7, 12, 13, 14, 15>
3792/// The first half comes from the second half of V1 and the second half from the
3793/// the second half of V2.
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00003794static bool isVPERM2X128Mask(ArrayRef<int> Mask, EVT VT, bool HasFp256) {
3795 if (!HasFp256 || !VT.is256BitVector())
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003796 return false;
3797
3798 // The shuffle result is divided into half A and half B. In total the two
3799 // sources have 4 halves, namely: C, D, E, F. The final values of A and
3800 // B must come from C, D, E or F.
Craig Topperc612d792012-01-02 09:17:37 +00003801 unsigned HalfSize = VT.getVectorNumElements()/2;
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003802 bool MatchA = false, MatchB = false;
3803
3804 // Check if A comes from one of C, D, E, F.
Craig Topperc612d792012-01-02 09:17:37 +00003805 for (unsigned Half = 0; Half != 4; ++Half) {
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003806 if (isSequentialOrUndefInRange(Mask, 0, HalfSize, Half*HalfSize)) {
3807 MatchA = true;
3808 break;
3809 }
3810 }
3811
3812 // Check if B comes from one of C, D, E, F.
Craig Topperc612d792012-01-02 09:17:37 +00003813 for (unsigned Half = 0; Half != 4; ++Half) {
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003814 if (isSequentialOrUndefInRange(Mask, HalfSize, HalfSize, Half*HalfSize)) {
3815 MatchB = true;
3816 break;
3817 }
3818 }
3819
3820 return MatchA && MatchB;
3821}
3822
Craig Topper70b883b2011-11-28 10:14:51 +00003823/// getShuffleVPERM2X128Immediate - Return the appropriate immediate to shuffle
3824/// the specified VECTOR_MASK mask with VPERM2F128/VPERM2I128 instructions.
Craig Topperd93e4c32011-12-11 19:12:35 +00003825static unsigned getShuffleVPERM2X128Immediate(ShuffleVectorSDNode *SVOp) {
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003826 EVT VT = SVOp->getValueType(0);
3827
Craig Topperc612d792012-01-02 09:17:37 +00003828 unsigned HalfSize = VT.getVectorNumElements()/2;
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003829
Craig Topperc612d792012-01-02 09:17:37 +00003830 unsigned FstHalf = 0, SndHalf = 0;
3831 for (unsigned i = 0; i < HalfSize; ++i) {
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003832 if (SVOp->getMaskElt(i) > 0) {
3833 FstHalf = SVOp->getMaskElt(i)/HalfSize;
3834 break;
3835 }
3836 }
Craig Topperc612d792012-01-02 09:17:37 +00003837 for (unsigned i = HalfSize; i < HalfSize*2; ++i) {
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003838 if (SVOp->getMaskElt(i) > 0) {
3839 SndHalf = SVOp->getMaskElt(i)/HalfSize;
3840 break;
3841 }
3842 }
3843
3844 return (FstHalf | (SndHalf << 4));
3845}
3846
Craig Topper70b883b2011-11-28 10:14:51 +00003847/// isVPERMILPMask - Return true if the specified VECTOR_SHUFFLE operand
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00003848/// specifies a shuffle of elements that is suitable for input to VPERMILPD*.
3849/// Note that VPERMIL mask matching is different depending whether theunderlying
3850/// type is 32 or 64. In the VPERMILPS the high half of the mask should point
3851/// to the same elements of the low, but to the higher half of the source.
3852/// In VPERMILPD the two lanes could be shuffled independently of each other
Craig Topperdbd98a42012-02-07 06:28:42 +00003853/// with the same restriction that lanes can't be crossed. Also handles PSHUFDY.
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00003854static bool isVPERMILPMask(ArrayRef<int> Mask, EVT VT, bool HasFp256) {
3855 if (!HasFp256)
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00003856 return false;
3857
Craig Topperc612d792012-01-02 09:17:37 +00003858 unsigned NumElts = VT.getVectorNumElements();
Craig Topper70b883b2011-11-28 10:14:51 +00003859 // Only match 256-bit with 32/64-bit types
3860 if (VT.getSizeInBits() != 256 || (NumElts != 4 && NumElts != 8))
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00003861 return false;
3862
Craig Topperc612d792012-01-02 09:17:37 +00003863 unsigned NumLanes = VT.getSizeInBits()/128;
3864 unsigned LaneSize = NumElts/NumLanes;
Craig Topper1a7700a2012-01-19 08:19:12 +00003865 for (unsigned l = 0; l != NumElts; l += LaneSize) {
Craig Topperc612d792012-01-02 09:17:37 +00003866 for (unsigned i = 0; i != LaneSize; ++i) {
Craig Topper1a7700a2012-01-19 08:19:12 +00003867 if (!isUndefOrInRange(Mask[i+l], l, l+LaneSize))
Craig Topper70b883b2011-11-28 10:14:51 +00003868 return false;
Craig Topper1a7700a2012-01-19 08:19:12 +00003869 if (NumElts != 8 || l == 0)
Craig Topper70b883b2011-11-28 10:14:51 +00003870 continue;
3871 // VPERMILPS handling
3872 if (Mask[i] < 0)
3873 continue;
Craig Topper1a7700a2012-01-19 08:19:12 +00003874 if (!isUndefOrEqual(Mask[i+l], Mask[i]+l))
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00003875 return false;
3876 }
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00003877 }
3878
3879 return true;
3880}
3881
Craig Topper5aaffa82012-02-19 02:53:47 +00003882/// isCommutedMOVLMask - Returns true if the shuffle mask is except the reverse
Evan Cheng017dcc62006-04-21 01:05:10 +00003883/// of what x86 movss want. X86 movs requires the lowest element to be lowest
Evan Cheng39623da2006-04-20 08:58:49 +00003884/// element of vector 2 and the other elements to come from vector 1 in order.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003885static bool isCommutedMOVLMask(ArrayRef<int> Mask, EVT VT,
Nate Begeman9008ca62009-04-27 18:41:29 +00003886 bool V2IsSplat = false, bool V2IsUndef = false) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00003887 if (!VT.is128BitVector())
Craig Topper97327dc2012-03-18 22:50:10 +00003888 return false;
Craig Topper7a9a28b2012-08-12 02:23:29 +00003889
3890 unsigned NumOps = VT.getVectorNumElements();
Chris Lattner5a88b832007-02-25 07:10:00 +00003891 if (NumOps != 2 && NumOps != 4 && NumOps != 8 && NumOps != 16)
Evan Cheng39623da2006-04-20 08:58:49 +00003892 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003893
Nate Begeman9008ca62009-04-27 18:41:29 +00003894 if (!isUndefOrEqual(Mask[0], 0))
Evan Cheng39623da2006-04-20 08:58:49 +00003895 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003896
Craig Topperc612d792012-01-02 09:17:37 +00003897 for (unsigned i = 1; i != NumOps; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003898 if (!(isUndefOrEqual(Mask[i], i+NumOps) ||
3899 (V2IsUndef && isUndefOrInRange(Mask[i], NumOps, NumOps*2)) ||
3900 (V2IsSplat && isUndefOrEqual(Mask[i], NumOps))))
Evan Cheng8cf723d2006-09-08 01:50:06 +00003901 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003902
Evan Cheng39623da2006-04-20 08:58:49 +00003903 return true;
3904}
3905
Evan Chengd9539472006-04-14 21:59:03 +00003906/// isMOVSHDUPMask - Return true if the specified VECTOR_SHUFFLE operand
3907/// specifies a shuffle of elements that is suitable for input to MOVSHDUP.
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003908/// Masks to match: <1, 1, 3, 3> or <1, 1, 3, 3, 5, 5, 7, 7>
Craig Topperdd637ae2012-02-19 05:41:45 +00003909static bool isMOVSHDUPMask(ArrayRef<int> Mask, EVT VT,
Craig Topper5aaffa82012-02-19 02:53:47 +00003910 const X86Subtarget *Subtarget) {
Craig Topperd0a31172012-01-10 06:37:29 +00003911 if (!Subtarget->hasSSE3())
Evan Chengd9539472006-04-14 21:59:03 +00003912 return false;
3913
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003914 unsigned NumElems = VT.getVectorNumElements();
3915
3916 if ((VT.getSizeInBits() == 128 && NumElems != 4) ||
3917 (VT.getSizeInBits() == 256 && NumElems != 8))
3918 return false;
3919
3920 // "i+1" is the value the indexed mask element must have
Craig Topperdd637ae2012-02-19 05:41:45 +00003921 for (unsigned i = 0; i != NumElems; i += 2)
3922 if (!isUndefOrEqual(Mask[i], i+1) ||
3923 !isUndefOrEqual(Mask[i+1], i+1))
Nate Begeman9008ca62009-04-27 18:41:29 +00003924 return false;
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003925
3926 return true;
Evan Chengd9539472006-04-14 21:59:03 +00003927}
3928
3929/// isMOVSLDUPMask - Return true if the specified VECTOR_SHUFFLE operand
3930/// specifies a shuffle of elements that is suitable for input to MOVSLDUP.
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003931/// Masks to match: <0, 0, 2, 2> or <0, 0, 2, 2, 4, 4, 6, 6>
Craig Topperdd637ae2012-02-19 05:41:45 +00003932static bool isMOVSLDUPMask(ArrayRef<int> Mask, EVT VT,
Craig Topper5aaffa82012-02-19 02:53:47 +00003933 const X86Subtarget *Subtarget) {
Craig Topperd0a31172012-01-10 06:37:29 +00003934 if (!Subtarget->hasSSE3())
Evan Chengd9539472006-04-14 21:59:03 +00003935 return false;
3936
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003937 unsigned NumElems = VT.getVectorNumElements();
3938
3939 if ((VT.getSizeInBits() == 128 && NumElems != 4) ||
3940 (VT.getSizeInBits() == 256 && NumElems != 8))
3941 return false;
3942
3943 // "i" is the value the indexed mask element must have
Craig Topperc612d792012-01-02 09:17:37 +00003944 for (unsigned i = 0; i != NumElems; i += 2)
Craig Topperdd637ae2012-02-19 05:41:45 +00003945 if (!isUndefOrEqual(Mask[i], i) ||
3946 !isUndefOrEqual(Mask[i+1], i))
Nate Begeman9008ca62009-04-27 18:41:29 +00003947 return false;
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003948
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003949 return true;
Evan Chengd9539472006-04-14 21:59:03 +00003950}
3951
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00003952/// isMOVDDUPYMask - Return true if the specified VECTOR_SHUFFLE operand
3953/// specifies a shuffle of elements that is suitable for input to 256-bit
3954/// version of MOVDDUP.
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00003955static bool isMOVDDUPYMask(ArrayRef<int> Mask, EVT VT, bool HasFp256) {
3956 if (!HasFp256 || !VT.is256BitVector())
Craig Topper7a9a28b2012-08-12 02:23:29 +00003957 return false;
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00003958
Craig Topper7a9a28b2012-08-12 02:23:29 +00003959 unsigned NumElts = VT.getVectorNumElements();
3960 if (NumElts != 4)
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00003961 return false;
3962
Craig Topperc612d792012-01-02 09:17:37 +00003963 for (unsigned i = 0; i != NumElts/2; ++i)
Craig Topperbeabc6c2011-12-05 06:56:46 +00003964 if (!isUndefOrEqual(Mask[i], 0))
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00003965 return false;
Craig Topperc612d792012-01-02 09:17:37 +00003966 for (unsigned i = NumElts/2; i != NumElts; ++i)
Craig Topperbeabc6c2011-12-05 06:56:46 +00003967 if (!isUndefOrEqual(Mask[i], NumElts/2))
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00003968 return false;
3969 return true;
3970}
3971
Evan Cheng0b457f02008-09-25 20:50:48 +00003972/// isMOVDDUPMask - Return true if the specified VECTOR_SHUFFLE operand
Bruno Cardoso Lopes06ef9232011-08-25 21:40:34 +00003973/// specifies a shuffle of elements that is suitable for input to 128-bit
3974/// version of MOVDDUP.
Craig Topperdd637ae2012-02-19 05:41:45 +00003975static bool isMOVDDUPMask(ArrayRef<int> Mask, EVT VT) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00003976 if (!VT.is128BitVector())
Bruno Cardoso Lopes06ef9232011-08-25 21:40:34 +00003977 return false;
3978
Craig Topperc612d792012-01-02 09:17:37 +00003979 unsigned e = VT.getVectorNumElements() / 2;
3980 for (unsigned i = 0; i != e; ++i)
Craig Topperdd637ae2012-02-19 05:41:45 +00003981 if (!isUndefOrEqual(Mask[i], i))
Evan Cheng0b457f02008-09-25 20:50:48 +00003982 return false;
Craig Topperc612d792012-01-02 09:17:37 +00003983 for (unsigned i = 0; i != e; ++i)
Craig Topperdd637ae2012-02-19 05:41:45 +00003984 if (!isUndefOrEqual(Mask[e+i], i))
Evan Cheng0b457f02008-09-25 20:50:48 +00003985 return false;
3986 return true;
3987}
3988
David Greenec38a03e2011-02-03 15:50:00 +00003989/// isVEXTRACTF128Index - Return true if the specified
3990/// EXTRACT_SUBVECTOR operand specifies a vector extract that is
3991/// suitable for input to VEXTRACTF128.
3992bool X86::isVEXTRACTF128Index(SDNode *N) {
3993 if (!isa<ConstantSDNode>(N->getOperand(1).getNode()))
3994 return false;
3995
3996 // The index should be aligned on a 128-bit boundary.
3997 uint64_t Index =
3998 cast<ConstantSDNode>(N->getOperand(1).getNode())->getZExtValue();
3999
4000 unsigned VL = N->getValueType(0).getVectorNumElements();
4001 unsigned VBits = N->getValueType(0).getSizeInBits();
4002 unsigned ElSize = VBits / VL;
4003 bool Result = (Index * ElSize) % 128 == 0;
4004
4005 return Result;
4006}
4007
David Greeneccacdc12011-02-04 16:08:29 +00004008/// isVINSERTF128Index - Return true if the specified INSERT_SUBVECTOR
4009/// operand specifies a subvector insert that is suitable for input to
4010/// VINSERTF128.
4011bool X86::isVINSERTF128Index(SDNode *N) {
4012 if (!isa<ConstantSDNode>(N->getOperand(2).getNode()))
4013 return false;
4014
4015 // The index should be aligned on a 128-bit boundary.
4016 uint64_t Index =
4017 cast<ConstantSDNode>(N->getOperand(2).getNode())->getZExtValue();
4018
4019 unsigned VL = N->getValueType(0).getVectorNumElements();
4020 unsigned VBits = N->getValueType(0).getSizeInBits();
4021 unsigned ElSize = VBits / VL;
4022 bool Result = (Index * ElSize) % 128 == 0;
4023
4024 return Result;
4025}
4026
Evan Cheng63d33002006-03-22 08:01:21 +00004027/// getShuffleSHUFImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00004028/// the specified VECTOR_SHUFFLE mask with PSHUF* and SHUFP* instructions.
Craig Topper1a7700a2012-01-19 08:19:12 +00004029/// Handles 128-bit and 256-bit.
Craig Topper5aaffa82012-02-19 02:53:47 +00004030static unsigned getShuffleSHUFImmediate(ShuffleVectorSDNode *N) {
Craig Topper1a7700a2012-01-19 08:19:12 +00004031 EVT VT = N->getValueType(0);
Nate Begeman9008ca62009-04-27 18:41:29 +00004032
Craig Topper1a7700a2012-01-19 08:19:12 +00004033 assert((VT.is128BitVector() || VT.is256BitVector()) &&
4034 "Unsupported vector type for PSHUF/SHUFP");
4035
4036 // Handle 128 and 256-bit vector lengths. AVX defines PSHUF/SHUFP to operate
4037 // independently on 128-bit lanes.
4038 unsigned NumElts = VT.getVectorNumElements();
4039 unsigned NumLanes = VT.getSizeInBits()/128;
4040 unsigned NumLaneElts = NumElts/NumLanes;
4041
4042 assert((NumLaneElts == 2 || NumLaneElts == 4) &&
4043 "Only supports 2 or 4 elements per lane");
4044
4045 unsigned Shift = (NumLaneElts == 4) ? 1 : 0;
Evan Chengb9df0ca2006-03-22 02:53:00 +00004046 unsigned Mask = 0;
Craig Topper1a7700a2012-01-19 08:19:12 +00004047 for (unsigned i = 0; i != NumElts; ++i) {
4048 int Elt = N->getMaskElt(i);
4049 if (Elt < 0) continue;
Craig Topper6b28d352012-05-03 07:12:59 +00004050 Elt &= NumLaneElts - 1;
4051 unsigned ShAmt = (i << Shift) % 8;
Craig Topper1a7700a2012-01-19 08:19:12 +00004052 Mask |= Elt << ShAmt;
Evan Cheng36b27f32006-03-28 23:41:33 +00004053 }
Craig Topper1a7700a2012-01-19 08:19:12 +00004054
Evan Cheng63d33002006-03-22 08:01:21 +00004055 return Mask;
4056}
4057
Evan Cheng506d3df2006-03-29 23:07:14 +00004058/// getShufflePSHUFHWImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00004059/// the specified VECTOR_SHUFFLE mask with the PSHUFHW instruction.
Craig Topperdd637ae2012-02-19 05:41:45 +00004060static unsigned getShufflePSHUFHWImmediate(ShuffleVectorSDNode *N) {
Craig Topper6b28d352012-05-03 07:12:59 +00004061 EVT VT = N->getValueType(0);
4062
4063 assert((VT == MVT::v8i16 || VT == MVT::v16i16) &&
4064 "Unsupported vector type for PSHUFHW");
4065
4066 unsigned NumElts = VT.getVectorNumElements();
4067
Evan Cheng506d3df2006-03-29 23:07:14 +00004068 unsigned Mask = 0;
Craig Topper6b28d352012-05-03 07:12:59 +00004069 for (unsigned l = 0; l != NumElts; l += 8) {
4070 // 8 nodes per lane, but we only care about the last 4.
4071 for (unsigned i = 0; i < 4; ++i) {
4072 int Elt = N->getMaskElt(l+i+4);
4073 if (Elt < 0) continue;
4074 Elt &= 0x3; // only 2-bits.
4075 Mask |= Elt << (i * 2);
4076 }
Evan Cheng506d3df2006-03-29 23:07:14 +00004077 }
Craig Topper6b28d352012-05-03 07:12:59 +00004078
Evan Cheng506d3df2006-03-29 23:07:14 +00004079 return Mask;
4080}
4081
4082/// getShufflePSHUFLWImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00004083/// the specified VECTOR_SHUFFLE mask with the PSHUFLW instruction.
Craig Topperdd637ae2012-02-19 05:41:45 +00004084static unsigned getShufflePSHUFLWImmediate(ShuffleVectorSDNode *N) {
Craig Topper6b28d352012-05-03 07:12:59 +00004085 EVT VT = N->getValueType(0);
4086
4087 assert((VT == MVT::v8i16 || VT == MVT::v16i16) &&
4088 "Unsupported vector type for PSHUFHW");
4089
4090 unsigned NumElts = VT.getVectorNumElements();
4091
Evan Cheng506d3df2006-03-29 23:07:14 +00004092 unsigned Mask = 0;
Craig Topper6b28d352012-05-03 07:12:59 +00004093 for (unsigned l = 0; l != NumElts; l += 8) {
4094 // 8 nodes per lane, but we only care about the first 4.
4095 for (unsigned i = 0; i < 4; ++i) {
4096 int Elt = N->getMaskElt(l+i);
4097 if (Elt < 0) continue;
4098 Elt &= 0x3; // only 2-bits
4099 Mask |= Elt << (i * 2);
4100 }
Evan Cheng506d3df2006-03-29 23:07:14 +00004101 }
Craig Topper6b28d352012-05-03 07:12:59 +00004102
Evan Cheng506d3df2006-03-29 23:07:14 +00004103 return Mask;
4104}
4105
Nate Begemana09008b2009-10-19 02:17:23 +00004106/// getShufflePALIGNRImmediate - Return the appropriate immediate to shuffle
4107/// the specified VECTOR_SHUFFLE mask with the PALIGNR instruction.
Craig Topperd93e4c32011-12-11 19:12:35 +00004108static unsigned getShufflePALIGNRImmediate(ShuffleVectorSDNode *SVOp) {
4109 EVT VT = SVOp->getValueType(0);
4110 unsigned EltSize = VT.getVectorElementType().getSizeInBits() >> 3;
Nate Begemana09008b2009-10-19 02:17:23 +00004111
Craig Topper0e2037b2012-01-20 05:53:00 +00004112 unsigned NumElts = VT.getVectorNumElements();
4113 unsigned NumLanes = VT.getSizeInBits()/128;
4114 unsigned NumLaneElts = NumElts/NumLanes;
4115
4116 int Val = 0;
4117 unsigned i;
4118 for (i = 0; i != NumElts; ++i) {
Nate Begemana09008b2009-10-19 02:17:23 +00004119 Val = SVOp->getMaskElt(i);
4120 if (Val >= 0)
4121 break;
4122 }
Craig Topper0e2037b2012-01-20 05:53:00 +00004123 if (Val >= (int)NumElts)
4124 Val -= NumElts - NumLaneElts;
4125
Eli Friedman63f8dde2011-07-25 21:36:45 +00004126 assert(Val - i > 0 && "PALIGNR imm should be positive");
Nate Begemana09008b2009-10-19 02:17:23 +00004127 return (Val - i) * EltSize;
4128}
4129
David Greenec38a03e2011-02-03 15:50:00 +00004130/// getExtractVEXTRACTF128Immediate - Return the appropriate immediate
4131/// to extract the specified EXTRACT_SUBVECTOR index with VEXTRACTF128
4132/// instructions.
4133unsigned X86::getExtractVEXTRACTF128Immediate(SDNode *N) {
4134 if (!isa<ConstantSDNode>(N->getOperand(1).getNode()))
4135 llvm_unreachable("Illegal extract subvector for VEXTRACTF128");
4136
4137 uint64_t Index =
4138 cast<ConstantSDNode>(N->getOperand(1).getNode())->getZExtValue();
4139
4140 EVT VecVT = N->getOperand(0).getValueType();
4141 EVT ElVT = VecVT.getVectorElementType();
4142
4143 unsigned NumElemsPerChunk = 128 / ElVT.getSizeInBits();
David Greenec38a03e2011-02-03 15:50:00 +00004144 return Index / NumElemsPerChunk;
4145}
4146
David Greeneccacdc12011-02-04 16:08:29 +00004147/// getInsertVINSERTF128Immediate - Return the appropriate immediate
4148/// to insert at the specified INSERT_SUBVECTOR index with VINSERTF128
4149/// instructions.
4150unsigned X86::getInsertVINSERTF128Immediate(SDNode *N) {
4151 if (!isa<ConstantSDNode>(N->getOperand(2).getNode()))
4152 llvm_unreachable("Illegal insert subvector for VINSERTF128");
4153
4154 uint64_t Index =
NAKAMURA Takumi27635382011-02-05 15:10:54 +00004155 cast<ConstantSDNode>(N->getOperand(2).getNode())->getZExtValue();
David Greeneccacdc12011-02-04 16:08:29 +00004156
4157 EVT VecVT = N->getValueType(0);
4158 EVT ElVT = VecVT.getVectorElementType();
4159
4160 unsigned NumElemsPerChunk = 128 / ElVT.getSizeInBits();
David Greeneccacdc12011-02-04 16:08:29 +00004161 return Index / NumElemsPerChunk;
4162}
4163
Elena Demikhovsky73c504a2012-04-15 11:18:59 +00004164/// getShuffleCLImmediate - Return the appropriate immediate to shuffle
4165/// the specified VECTOR_SHUFFLE mask with VPERMQ and VPERMPD instructions.
4166/// Handles 256-bit.
4167static unsigned getShuffleCLImmediate(ShuffleVectorSDNode *N) {
4168 EVT VT = N->getValueType(0);
4169
Elena Demikhovsky73c504a2012-04-15 11:18:59 +00004170 unsigned NumElts = VT.getVectorNumElements();
4171
Craig Topper095c5282012-04-15 23:48:57 +00004172 assert((VT.is256BitVector() && NumElts == 4) &&
4173 "Unsupported vector type for VPERMQ/VPERMPD");
4174
Elena Demikhovsky73c504a2012-04-15 11:18:59 +00004175 unsigned Mask = 0;
4176 for (unsigned i = 0; i != NumElts; ++i) {
4177 int Elt = N->getMaskElt(i);
Craig Topper095c5282012-04-15 23:48:57 +00004178 if (Elt < 0)
Elena Demikhovsky73c504a2012-04-15 11:18:59 +00004179 continue;
4180 Mask |= Elt << (i*2);
4181 }
4182
4183 return Mask;
4184}
Evan Cheng37b73872009-07-30 08:33:02 +00004185/// isZeroNode - Returns true if Elt is a constant zero or a floating point
4186/// constant +0.0.
4187bool X86::isZeroNode(SDValue Elt) {
4188 return ((isa<ConstantSDNode>(Elt) &&
Dan Gohmane368b462010-06-18 14:22:04 +00004189 cast<ConstantSDNode>(Elt)->isNullValue()) ||
Evan Cheng37b73872009-07-30 08:33:02 +00004190 (isa<ConstantFPSDNode>(Elt) &&
4191 cast<ConstantFPSDNode>(Elt)->getValueAPF().isPosZero()));
4192}
4193
Nate Begeman9008ca62009-04-27 18:41:29 +00004194/// CommuteVectorShuffle - Swap vector_shuffle operands as well as values in
4195/// their permute mask.
4196static SDValue CommuteVectorShuffle(ShuffleVectorSDNode *SVOp,
4197 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00004198 EVT VT = SVOp->getValueType(0);
Nate Begeman5a5ca152009-04-29 05:20:52 +00004199 unsigned NumElems = VT.getVectorNumElements();
Nate Begeman9008ca62009-04-27 18:41:29 +00004200 SmallVector<int, 8> MaskVec;
Eric Christopherfd179292009-08-27 18:07:15 +00004201
Nate Begeman5a5ca152009-04-29 05:20:52 +00004202 for (unsigned i = 0; i != NumElems; ++i) {
Craig Topper8ae97ba2012-05-21 06:40:16 +00004203 int Idx = SVOp->getMaskElt(i);
4204 if (Idx >= 0) {
4205 if (Idx < (int)NumElems)
4206 Idx += NumElems;
4207 else
4208 Idx -= NumElems;
4209 }
4210 MaskVec.push_back(Idx);
Evan Cheng5ced1d82006-04-06 23:23:56 +00004211 }
Nate Begeman9008ca62009-04-27 18:41:29 +00004212 return DAG.getVectorShuffle(VT, SVOp->getDebugLoc(), SVOp->getOperand(1),
4213 SVOp->getOperand(0), &MaskVec[0]);
Evan Cheng5ced1d82006-04-06 23:23:56 +00004214}
4215
Evan Cheng533a0aa2006-04-19 20:35:22 +00004216/// ShouldXformToMOVHLPS - Return true if the node should be transformed to
4217/// match movhlps. The lower half elements should come from upper half of
4218/// V1 (and in order), and the upper half elements should come from the upper
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00004219/// half of V2 (and in order).
Craig Topperdd637ae2012-02-19 05:41:45 +00004220static bool ShouldXformToMOVHLPS(ArrayRef<int> Mask, EVT VT) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00004221 if (!VT.is128BitVector())
Bruno Cardoso Lopes59353b42011-08-11 18:59:13 +00004222 return false;
4223 if (VT.getVectorNumElements() != 4)
Evan Cheng533a0aa2006-04-19 20:35:22 +00004224 return false;
4225 for (unsigned i = 0, e = 2; i != e; ++i)
Craig Topperdd637ae2012-02-19 05:41:45 +00004226 if (!isUndefOrEqual(Mask[i], i+2))
Evan Cheng533a0aa2006-04-19 20:35:22 +00004227 return false;
4228 for (unsigned i = 2; i != 4; ++i)
Craig Topperdd637ae2012-02-19 05:41:45 +00004229 if (!isUndefOrEqual(Mask[i], i+4))
Evan Cheng533a0aa2006-04-19 20:35:22 +00004230 return false;
4231 return true;
4232}
4233
Evan Cheng5ced1d82006-04-06 23:23:56 +00004234/// isScalarLoadToVector - Returns true if the node is a scalar load that
Evan Cheng7e2ff772008-05-08 00:57:18 +00004235/// is promoted to a vector. It also returns the LoadSDNode by reference if
4236/// required.
4237static bool isScalarLoadToVector(SDNode *N, LoadSDNode **LD = NULL) {
Evan Cheng0b457f02008-09-25 20:50:48 +00004238 if (N->getOpcode() != ISD::SCALAR_TO_VECTOR)
4239 return false;
4240 N = N->getOperand(0).getNode();
4241 if (!ISD::isNON_EXTLoad(N))
4242 return false;
4243 if (LD)
4244 *LD = cast<LoadSDNode>(N);
4245 return true;
Evan Cheng5ced1d82006-04-06 23:23:56 +00004246}
4247
Dan Gohman65fd6562011-11-03 21:49:52 +00004248// Test whether the given value is a vector value which will be legalized
4249// into a load.
4250static bool WillBeConstantPoolLoad(SDNode *N) {
4251 if (N->getOpcode() != ISD::BUILD_VECTOR)
4252 return false;
4253
4254 // Check for any non-constant elements.
4255 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
4256 switch (N->getOperand(i).getNode()->getOpcode()) {
4257 case ISD::UNDEF:
4258 case ISD::ConstantFP:
4259 case ISD::Constant:
4260 break;
4261 default:
4262 return false;
4263 }
4264
4265 // Vectors of all-zeros and all-ones are materialized with special
4266 // instructions rather than being loaded.
4267 return !ISD::isBuildVectorAllZeros(N) &&
4268 !ISD::isBuildVectorAllOnes(N);
4269}
4270
Evan Cheng533a0aa2006-04-19 20:35:22 +00004271/// ShouldXformToMOVLP{S|D} - Return true if the node should be transformed to
4272/// match movlp{s|d}. The lower half elements should come from lower half of
4273/// V1 (and in order), and the upper half elements should come from the upper
4274/// half of V2 (and in order). And since V1 will become the source of the
4275/// MOVLP, it must be either a vector load or a scalar load to vector.
Nate Begeman9008ca62009-04-27 18:41:29 +00004276static bool ShouldXformToMOVLP(SDNode *V1, SDNode *V2,
Craig Topperdd637ae2012-02-19 05:41:45 +00004277 ArrayRef<int> Mask, EVT VT) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00004278 if (!VT.is128BitVector())
Bruno Cardoso Lopes59353b42011-08-11 18:59:13 +00004279 return false;
4280
Evan Cheng466685d2006-10-09 20:57:25 +00004281 if (!ISD::isNON_EXTLoad(V1) && !isScalarLoadToVector(V1))
Evan Cheng533a0aa2006-04-19 20:35:22 +00004282 return false;
Evan Cheng23425f52006-10-09 21:39:25 +00004283 // Is V2 is a vector load, don't do this transformation. We will try to use
4284 // load folding shufps op.
Dan Gohman65fd6562011-11-03 21:49:52 +00004285 if (ISD::isNON_EXTLoad(V2) || WillBeConstantPoolLoad(V2))
Evan Cheng23425f52006-10-09 21:39:25 +00004286 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00004287
Bruno Cardoso Lopes59353b42011-08-11 18:59:13 +00004288 unsigned NumElems = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00004289
Evan Cheng533a0aa2006-04-19 20:35:22 +00004290 if (NumElems != 2 && NumElems != 4)
4291 return false;
Nate Begeman5a5ca152009-04-29 05:20:52 +00004292 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
Craig Topperdd637ae2012-02-19 05:41:45 +00004293 if (!isUndefOrEqual(Mask[i], i))
Evan Cheng533a0aa2006-04-19 20:35:22 +00004294 return false;
Chad Rosier238ae312012-04-30 17:47:15 +00004295 for (unsigned i = NumElems/2, e = NumElems; i != e; ++i)
Craig Topperdd637ae2012-02-19 05:41:45 +00004296 if (!isUndefOrEqual(Mask[i], i+NumElems))
Evan Cheng533a0aa2006-04-19 20:35:22 +00004297 return false;
4298 return true;
Evan Cheng5ced1d82006-04-06 23:23:56 +00004299}
4300
Evan Cheng39623da2006-04-20 08:58:49 +00004301/// isSplatVector - Returns true if N is a BUILD_VECTOR node whose elements are
4302/// all the same.
4303static bool isSplatVector(SDNode *N) {
4304 if (N->getOpcode() != ISD::BUILD_VECTOR)
4305 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00004306
Dan Gohman475871a2008-07-27 21:46:04 +00004307 SDValue SplatValue = N->getOperand(0);
Evan Cheng39623da2006-04-20 08:58:49 +00004308 for (unsigned i = 1, e = N->getNumOperands(); i != e; ++i)
4309 if (N->getOperand(i) != SplatValue)
Evan Cheng5ced1d82006-04-06 23:23:56 +00004310 return false;
4311 return true;
4312}
4313
Evan Cheng213d2cf2007-05-17 18:45:50 +00004314/// isZeroShuffle - Returns true if N is a VECTOR_SHUFFLE that can be resolved
Eric Christopherfd179292009-08-27 18:07:15 +00004315/// to an zero vector.
Nate Begeman5a5ca152009-04-29 05:20:52 +00004316/// FIXME: move to dag combiner / method on ShuffleVectorSDNode
Nate Begeman9008ca62009-04-27 18:41:29 +00004317static bool isZeroShuffle(ShuffleVectorSDNode *N) {
Dan Gohman475871a2008-07-27 21:46:04 +00004318 SDValue V1 = N->getOperand(0);
4319 SDValue V2 = N->getOperand(1);
Nate Begeman5a5ca152009-04-29 05:20:52 +00004320 unsigned NumElems = N->getValueType(0).getVectorNumElements();
4321 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004322 int Idx = N->getMaskElt(i);
Nate Begeman5a5ca152009-04-29 05:20:52 +00004323 if (Idx >= (int)NumElems) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004324 unsigned Opc = V2.getOpcode();
Rafael Espindola15684b22009-04-24 12:40:33 +00004325 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V2.getNode()))
4326 continue;
Evan Cheng37b73872009-07-30 08:33:02 +00004327 if (Opc != ISD::BUILD_VECTOR ||
4328 !X86::isZeroNode(V2.getOperand(Idx-NumElems)))
Nate Begeman9008ca62009-04-27 18:41:29 +00004329 return false;
4330 } else if (Idx >= 0) {
4331 unsigned Opc = V1.getOpcode();
4332 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V1.getNode()))
4333 continue;
Evan Cheng37b73872009-07-30 08:33:02 +00004334 if (Opc != ISD::BUILD_VECTOR ||
4335 !X86::isZeroNode(V1.getOperand(Idx)))
Chris Lattner8a594482007-11-25 00:24:49 +00004336 return false;
Evan Cheng213d2cf2007-05-17 18:45:50 +00004337 }
4338 }
4339 return true;
4340}
4341
4342/// getZeroVector - Returns a vector of specified type with all zero elements.
4343///
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00004344static SDValue getZeroVector(EVT VT, const X86Subtarget *Subtarget,
Craig Topper12216172012-01-13 08:12:35 +00004345 SelectionDAG &DAG, DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004346 assert(VT.isVector() && "Expected a vector type");
Craig Topper9d352402012-04-23 07:24:41 +00004347 unsigned Size = VT.getSizeInBits();
Scott Michelfdc40a02009-02-17 22:15:04 +00004348
Dale Johannesen0488fb62010-09-30 23:57:10 +00004349 // Always build SSE zero vectors as <4 x i32> bitcasted
Bruno Cardoso Lopes8c05a852010-08-12 02:06:36 +00004350 // to their dest type. This ensures they get CSE'd.
Dan Gohman475871a2008-07-27 21:46:04 +00004351 SDValue Vec;
Craig Topper9d352402012-04-23 07:24:41 +00004352 if (Size == 128) { // SSE
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00004353 if (Subtarget->hasSSE2()) { // SSE2
Bruno Cardoso Lopes8c05a852010-08-12 02:06:36 +00004354 SDValue Cst = DAG.getTargetConstant(0, MVT::i32);
4355 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
4356 } else { // SSE1
4357 SDValue Cst = DAG.getTargetConstantFP(+0.0, MVT::f32);
4358 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4f32, Cst, Cst, Cst, Cst);
4359 }
Craig Topper9d352402012-04-23 07:24:41 +00004360 } else if (Size == 256) { // AVX
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00004361 if (Subtarget->hasInt256()) { // AVX2
Craig Topper12216172012-01-13 08:12:35 +00004362 SDValue Cst = DAG.getTargetConstant(0, MVT::i32);
4363 SDValue Ops[] = { Cst, Cst, Cst, Cst, Cst, Cst, Cst, Cst };
4364 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v8i32, Ops, 8);
4365 } else {
4366 // 256-bit logic and arithmetic instructions in AVX are all
4367 // floating-point, no support for integer ops. Emit fp zeroed vectors.
4368 SDValue Cst = DAG.getTargetConstantFP(+0.0, MVT::f32);
4369 SDValue Ops[] = { Cst, Cst, Cst, Cst, Cst, Cst, Cst, Cst };
4370 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v8f32, Ops, 8);
4371 }
Craig Topper9d352402012-04-23 07:24:41 +00004372 } else
4373 llvm_unreachable("Unexpected vector type");
4374
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004375 return DAG.getNode(ISD::BITCAST, dl, VT, Vec);
Evan Cheng213d2cf2007-05-17 18:45:50 +00004376}
4377
Chris Lattner8a594482007-11-25 00:24:49 +00004378/// getOnesVector - Returns a vector of specified type with all bits set.
Craig Topper745a86b2011-11-19 22:34:59 +00004379/// Always build ones vectors as <4 x i32> or <8 x i32>. For 256-bit types with
4380/// no AVX2 supprt, use two <4 x i32> inserted in a <8 x i32> appropriately.
4381/// Then bitcast to their original type, ensuring they get CSE'd.
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00004382static SDValue getOnesVector(EVT VT, bool HasInt256, SelectionDAG &DAG,
Craig Topper745a86b2011-11-19 22:34:59 +00004383 DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004384 assert(VT.isVector() && "Expected a vector type");
Craig Topper9d352402012-04-23 07:24:41 +00004385 unsigned Size = VT.getSizeInBits();
Scott Michelfdc40a02009-02-17 22:15:04 +00004386
Owen Anderson825b72b2009-08-11 20:47:22 +00004387 SDValue Cst = DAG.getTargetConstant(~0U, MVT::i32);
Craig Topper745a86b2011-11-19 22:34:59 +00004388 SDValue Vec;
Craig Topper9d352402012-04-23 07:24:41 +00004389 if (Size == 256) {
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00004390 if (HasInt256) { // AVX2
Craig Topper745a86b2011-11-19 22:34:59 +00004391 SDValue Ops[] = { Cst, Cst, Cst, Cst, Cst, Cst, Cst, Cst };
4392 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v8i32, Ops, 8);
4393 } else { // AVX
4394 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
Craig Topper4c7972d2012-04-22 18:15:59 +00004395 Vec = Concat128BitVectors(Vec, Vec, MVT::v8i32, 8, DAG, dl);
Craig Topper745a86b2011-11-19 22:34:59 +00004396 }
Craig Topper9d352402012-04-23 07:24:41 +00004397 } else if (Size == 128) {
Craig Topper745a86b2011-11-19 22:34:59 +00004398 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
Craig Topper9d352402012-04-23 07:24:41 +00004399 } else
4400 llvm_unreachable("Unexpected vector type");
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +00004401
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004402 return DAG.getNode(ISD::BITCAST, dl, VT, Vec);
Chris Lattner8a594482007-11-25 00:24:49 +00004403}
4404
Evan Cheng39623da2006-04-20 08:58:49 +00004405/// NormalizeMask - V2 is a splat, modify the mask (if needed) so all elements
4406/// that point to V2 points to its first element.
Craig Topper39a9e482012-02-11 06:24:48 +00004407static void NormalizeMask(SmallVectorImpl<int> &Mask, unsigned NumElems) {
Nate Begeman5a5ca152009-04-29 05:20:52 +00004408 for (unsigned i = 0; i != NumElems; ++i) {
Craig Topper39a9e482012-02-11 06:24:48 +00004409 if (Mask[i] > (int)NumElems) {
4410 Mask[i] = NumElems;
Evan Cheng39623da2006-04-20 08:58:49 +00004411 }
Evan Cheng39623da2006-04-20 08:58:49 +00004412 }
Evan Cheng39623da2006-04-20 08:58:49 +00004413}
4414
Evan Cheng017dcc62006-04-21 01:05:10 +00004415/// getMOVLMask - Returns a vector_shuffle mask for an movs{s|d}, movd
4416/// operation of specified width.
Owen Andersone50ed302009-08-10 22:56:29 +00004417static SDValue getMOVL(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00004418 SDValue V2) {
4419 unsigned NumElems = VT.getVectorNumElements();
4420 SmallVector<int, 8> Mask;
4421 Mask.push_back(NumElems);
Evan Cheng39623da2006-04-20 08:58:49 +00004422 for (unsigned i = 1; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004423 Mask.push_back(i);
4424 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Evan Cheng39623da2006-04-20 08:58:49 +00004425}
4426
Nate Begeman9008ca62009-04-27 18:41:29 +00004427/// getUnpackl - Returns a vector_shuffle node for an unpackl operation.
Owen Andersone50ed302009-08-10 22:56:29 +00004428static SDValue getUnpackl(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00004429 SDValue V2) {
4430 unsigned NumElems = VT.getVectorNumElements();
4431 SmallVector<int, 8> Mask;
Evan Chengc575ca22006-04-17 20:43:08 +00004432 for (unsigned i = 0, e = NumElems/2; i != e; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004433 Mask.push_back(i);
4434 Mask.push_back(i + NumElems);
Evan Chengc575ca22006-04-17 20:43:08 +00004435 }
Nate Begeman9008ca62009-04-27 18:41:29 +00004436 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Evan Chengc575ca22006-04-17 20:43:08 +00004437}
4438
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004439/// getUnpackh - Returns a vector_shuffle node for an unpackh operation.
Owen Andersone50ed302009-08-10 22:56:29 +00004440static SDValue getUnpackh(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00004441 SDValue V2) {
4442 unsigned NumElems = VT.getVectorNumElements();
Nate Begeman9008ca62009-04-27 18:41:29 +00004443 SmallVector<int, 8> Mask;
Chad Rosier238ae312012-04-30 17:47:15 +00004444 for (unsigned i = 0, Half = NumElems/2; i != Half; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004445 Mask.push_back(i + Half);
4446 Mask.push_back(i + NumElems + Half);
Evan Cheng39623da2006-04-20 08:58:49 +00004447 }
Nate Begeman9008ca62009-04-27 18:41:29 +00004448 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Chris Lattner62098042008-03-09 01:05:04 +00004449}
4450
Bruno Cardoso Lopes5f1d8ab2011-08-11 02:49:44 +00004451// PromoteSplati8i16 - All i16 and i8 vector types can't be used directly by
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004452// a generic shuffle instruction because the target has no such instructions.
4453// Generate shuffles which repeat i16 and i8 several times until they can be
4454// represented by v4f32 and then be manipulated by target suported shuffles.
Bruno Cardoso Lopes5f1d8ab2011-08-11 02:49:44 +00004455static SDValue PromoteSplati8i16(SDValue V, SelectionDAG &DAG, int &EltNo) {
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004456 EVT VT = V.getValueType();
Nate Begeman9008ca62009-04-27 18:41:29 +00004457 int NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004458 DebugLoc dl = V.getDebugLoc();
Rafael Espindola15684b22009-04-24 12:40:33 +00004459
Nate Begeman9008ca62009-04-27 18:41:29 +00004460 while (NumElems > 4) {
4461 if (EltNo < NumElems/2) {
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004462 V = getUnpackl(DAG, dl, VT, V, V);
Nate Begeman9008ca62009-04-27 18:41:29 +00004463 } else {
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004464 V = getUnpackh(DAG, dl, VT, V, V);
Nate Begeman9008ca62009-04-27 18:41:29 +00004465 EltNo -= NumElems/2;
4466 }
4467 NumElems >>= 1;
4468 }
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004469 return V;
4470}
Eric Christopherfd179292009-08-27 18:07:15 +00004471
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004472/// getLegalSplat - Generate a legal splat with supported x86 shuffles
4473static SDValue getLegalSplat(SelectionDAG &DAG, SDValue V, int EltNo) {
4474 EVT VT = V.getValueType();
4475 DebugLoc dl = V.getDebugLoc();
Craig Topper9d352402012-04-23 07:24:41 +00004476 unsigned Size = VT.getSizeInBits();
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004477
Craig Topper9d352402012-04-23 07:24:41 +00004478 if (Size == 128) {
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004479 V = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, V);
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004480 int SplatMask[4] = { EltNo, EltNo, EltNo, EltNo };
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004481 V = DAG.getVectorShuffle(MVT::v4f32, dl, V, DAG.getUNDEF(MVT::v4f32),
4482 &SplatMask[0]);
Craig Topper9d352402012-04-23 07:24:41 +00004483 } else if (Size == 256) {
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004484 // To use VPERMILPS to splat scalars, the second half of indicies must
4485 // refer to the higher part, which is a duplication of the lower one,
4486 // because VPERMILPS can only handle in-lane permutations.
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004487 int SplatMask[8] = { EltNo, EltNo, EltNo, EltNo,
4488 EltNo+4, EltNo+4, EltNo+4, EltNo+4 };
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004489
4490 V = DAG.getNode(ISD::BITCAST, dl, MVT::v8f32, V);
4491 V = DAG.getVectorShuffle(MVT::v8f32, dl, V, DAG.getUNDEF(MVT::v8f32),
4492 &SplatMask[0]);
Craig Topper9d352402012-04-23 07:24:41 +00004493 } else
4494 llvm_unreachable("Vector size not supported");
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004495
4496 return DAG.getNode(ISD::BITCAST, dl, VT, V);
4497}
4498
Bruno Cardoso Lopes8a5b2622011-08-17 02:29:13 +00004499/// PromoteSplat - Splat is promoted to target supported vector shuffles.
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004500static SDValue PromoteSplat(ShuffleVectorSDNode *SV, SelectionDAG &DAG) {
4501 EVT SrcVT = SV->getValueType(0);
4502 SDValue V1 = SV->getOperand(0);
4503 DebugLoc dl = SV->getDebugLoc();
4504
4505 int EltNo = SV->getSplatIndex();
4506 int NumElems = SrcVT.getVectorNumElements();
4507 unsigned Size = SrcVT.getSizeInBits();
4508
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004509 assert(((Size == 128 && NumElems > 4) || Size == 256) &&
4510 "Unknown how to promote splat for type");
4511
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004512 // Extract the 128-bit part containing the splat element and update
4513 // the splat element index when it refers to the higher register.
4514 if (Size == 256) {
Craig Topper7d1e3dc2012-04-30 05:17:10 +00004515 V1 = Extract128BitVector(V1, EltNo, DAG, dl);
4516 if (EltNo >= NumElems/2)
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004517 EltNo -= NumElems/2;
4518 }
4519
Bruno Cardoso Lopes8a5b2622011-08-17 02:29:13 +00004520 // All i16 and i8 vector types can't be used directly by a generic shuffle
4521 // instruction because the target has no such instruction. Generate shuffles
4522 // which repeat i16 and i8 several times until they fit in i32, and then can
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004523 // be manipulated by target suported shuffles.
Bruno Cardoso Lopes5f1d8ab2011-08-11 02:49:44 +00004524 EVT EltVT = SrcVT.getVectorElementType();
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004525 if (EltVT == MVT::i8 || EltVT == MVT::i16)
Bruno Cardoso Lopes5f1d8ab2011-08-11 02:49:44 +00004526 V1 = PromoteSplati8i16(V1, DAG, EltNo);
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004527
4528 // Recreate the 256-bit vector and place the same 128-bit vector
4529 // into the low and high part. This is necessary because we want
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004530 // to use VPERM* to shuffle the vectors
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004531 if (Size == 256) {
Craig Topper4c7972d2012-04-22 18:15:59 +00004532 V1 = DAG.getNode(ISD::CONCAT_VECTORS, dl, SrcVT, V1, V1);
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004533 }
4534
4535 return getLegalSplat(DAG, V1, EltNo);
Evan Chengc575ca22006-04-17 20:43:08 +00004536}
4537
Evan Chengba05f722006-04-21 23:03:30 +00004538/// getShuffleVectorZeroOrUndef - Return a vector_shuffle of the specified
Chris Lattner8a594482007-11-25 00:24:49 +00004539/// vector of zero or undef vector. This produces a shuffle where the low
4540/// element of V2 is swizzled into the zero/undef vector, landing at element
4541/// 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 +00004542static SDValue getShuffleVectorZeroOrUndef(SDValue V2, unsigned Idx,
Craig Topper12216172012-01-13 08:12:35 +00004543 bool IsZero,
4544 const X86Subtarget *Subtarget,
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00004545 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00004546 EVT VT = V2.getValueType();
Craig Topper12216172012-01-13 08:12:35 +00004547 SDValue V1 = IsZero
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00004548 ? getZeroVector(VT, Subtarget, DAG, V2.getDebugLoc()) : DAG.getUNDEF(VT);
Nate Begeman9008ca62009-04-27 18:41:29 +00004549 unsigned NumElems = VT.getVectorNumElements();
4550 SmallVector<int, 16> MaskVec;
Chris Lattner8a594482007-11-25 00:24:49 +00004551 for (unsigned i = 0; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004552 // If this is the insertion idx, put the low elt of V2 here.
4553 MaskVec.push_back(i == Idx ? NumElems : i);
4554 return DAG.getVectorShuffle(VT, V2.getDebugLoc(), V1, V2, &MaskVec[0]);
Evan Cheng017dcc62006-04-21 01:05:10 +00004555}
4556
Craig Toppera1ffc682012-03-20 06:42:26 +00004557/// getTargetShuffleMask - Calculates the shuffle mask corresponding to the
4558/// target specific opcode. Returns true if the Mask could be calculated.
Craig Topper89f4e662012-03-20 07:17:59 +00004559/// Sets IsUnary to true if only uses one source.
Craig Topperd978c542012-05-06 19:46:21 +00004560static bool getTargetShuffleMask(SDNode *N, MVT VT,
Craig Topper89f4e662012-03-20 07:17:59 +00004561 SmallVectorImpl<int> &Mask, bool &IsUnary) {
Craig Toppera1ffc682012-03-20 06:42:26 +00004562 unsigned NumElems = VT.getVectorNumElements();
4563 SDValue ImmN;
4564
Craig Topper89f4e662012-03-20 07:17:59 +00004565 IsUnary = false;
Craig Toppera1ffc682012-03-20 06:42:26 +00004566 switch(N->getOpcode()) {
4567 case X86ISD::SHUFP:
4568 ImmN = N->getOperand(N->getNumOperands()-1);
4569 DecodeSHUFPMask(VT, cast<ConstantSDNode>(ImmN)->getZExtValue(), Mask);
4570 break;
4571 case X86ISD::UNPCKH:
4572 DecodeUNPCKHMask(VT, Mask);
4573 break;
4574 case X86ISD::UNPCKL:
4575 DecodeUNPCKLMask(VT, Mask);
4576 break;
4577 case X86ISD::MOVHLPS:
4578 DecodeMOVHLPSMask(NumElems, Mask);
4579 break;
4580 case X86ISD::MOVLHPS:
4581 DecodeMOVLHPSMask(NumElems, Mask);
4582 break;
4583 case X86ISD::PSHUFD:
4584 case X86ISD::VPERMILP:
4585 ImmN = N->getOperand(N->getNumOperands()-1);
4586 DecodePSHUFMask(VT, cast<ConstantSDNode>(ImmN)->getZExtValue(), Mask);
Craig Topper89f4e662012-03-20 07:17:59 +00004587 IsUnary = true;
Craig Toppera1ffc682012-03-20 06:42:26 +00004588 break;
4589 case X86ISD::PSHUFHW:
4590 ImmN = N->getOperand(N->getNumOperands()-1);
Craig Toppera9a568a2012-05-02 08:03:44 +00004591 DecodePSHUFHWMask(VT, cast<ConstantSDNode>(ImmN)->getZExtValue(), Mask);
Craig Topper89f4e662012-03-20 07:17:59 +00004592 IsUnary = true;
Craig Toppera1ffc682012-03-20 06:42:26 +00004593 break;
4594 case X86ISD::PSHUFLW:
4595 ImmN = N->getOperand(N->getNumOperands()-1);
Craig Toppera9a568a2012-05-02 08:03:44 +00004596 DecodePSHUFLWMask(VT, cast<ConstantSDNode>(ImmN)->getZExtValue(), Mask);
Craig Topper89f4e662012-03-20 07:17:59 +00004597 IsUnary = true;
Craig Toppera1ffc682012-03-20 06:42:26 +00004598 break;
Craig Topperbdcbcb32012-05-06 18:54:26 +00004599 case X86ISD::VPERMI:
4600 ImmN = N->getOperand(N->getNumOperands()-1);
4601 DecodeVPERMMask(cast<ConstantSDNode>(ImmN)->getZExtValue(), Mask);
4602 IsUnary = true;
4603 break;
Craig Toppera1ffc682012-03-20 06:42:26 +00004604 case X86ISD::MOVSS:
4605 case X86ISD::MOVSD: {
4606 // The index 0 always comes from the first element of the second source,
4607 // this is why MOVSS and MOVSD are used in the first place. The other
4608 // elements come from the other positions of the first source vector
4609 Mask.push_back(NumElems);
4610 for (unsigned i = 1; i != NumElems; ++i) {
4611 Mask.push_back(i);
4612 }
4613 break;
4614 }
4615 case X86ISD::VPERM2X128:
4616 ImmN = N->getOperand(N->getNumOperands()-1);
4617 DecodeVPERM2X128Mask(VT, cast<ConstantSDNode>(ImmN)->getZExtValue(), Mask);
Craig Topper2091df32012-04-17 05:54:54 +00004618 if (Mask.empty()) return false;
Craig Toppera1ffc682012-03-20 06:42:26 +00004619 break;
4620 case X86ISD::MOVDDUP:
4621 case X86ISD::MOVLHPD:
4622 case X86ISD::MOVLPD:
4623 case X86ISD::MOVLPS:
4624 case X86ISD::MOVSHDUP:
4625 case X86ISD::MOVSLDUP:
4626 case X86ISD::PALIGN:
4627 // Not yet implemented
4628 return false;
4629 default: llvm_unreachable("unknown target shuffle node");
4630 }
4631
4632 return true;
4633}
4634
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004635/// getShuffleScalarElt - Returns the scalar element that will make up the ith
4636/// element of the result of the vector shuffle.
Craig Topper3d092db2012-03-21 02:14:01 +00004637static SDValue getShuffleScalarElt(SDNode *N, unsigned Index, SelectionDAG &DAG,
Benjamin Kramer050db522011-03-26 12:38:19 +00004638 unsigned Depth) {
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004639 if (Depth == 6)
4640 return SDValue(); // Limit search depth.
4641
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004642 SDValue V = SDValue(N, 0);
4643 EVT VT = V.getValueType();
4644 unsigned Opcode = V.getOpcode();
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004645
4646 // Recurse into ISD::VECTOR_SHUFFLE node to find scalars.
4647 if (const ShuffleVectorSDNode *SV = dyn_cast<ShuffleVectorSDNode>(N)) {
Craig Topper3d092db2012-03-21 02:14:01 +00004648 int Elt = SV->getMaskElt(Index);
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004649
Craig Topper3d092db2012-03-21 02:14:01 +00004650 if (Elt < 0)
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004651 return DAG.getUNDEF(VT.getVectorElementType());
4652
Craig Topperd156dc12012-02-06 07:17:51 +00004653 unsigned NumElems = VT.getVectorNumElements();
Craig Topper3d092db2012-03-21 02:14:01 +00004654 SDValue NewV = (Elt < (int)NumElems) ? SV->getOperand(0)
4655 : SV->getOperand(1);
4656 return getShuffleScalarElt(NewV.getNode(), Elt % NumElems, DAG, Depth+1);
Evan Chengf26ffe92008-05-29 08:22:04 +00004657 }
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004658
4659 // Recurse into target specific vector shuffles to find scalars.
4660 if (isTargetShuffle(Opcode)) {
Craig Topperd978c542012-05-06 19:46:21 +00004661 MVT ShufVT = V.getValueType().getSimpleVT();
4662 unsigned NumElems = ShufVT.getVectorNumElements();
Craig Toppera1ffc682012-03-20 06:42:26 +00004663 SmallVector<int, 16> ShuffleMask;
Craig Topper89f4e662012-03-20 07:17:59 +00004664 bool IsUnary;
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004665
Craig Topperd978c542012-05-06 19:46:21 +00004666 if (!getTargetShuffleMask(N, ShufVT, ShuffleMask, IsUnary))
Craig Toppera1ffc682012-03-20 06:42:26 +00004667 return SDValue();
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004668
Craig Topper3d092db2012-03-21 02:14:01 +00004669 int Elt = ShuffleMask[Index];
4670 if (Elt < 0)
Craig Topperd978c542012-05-06 19:46:21 +00004671 return DAG.getUNDEF(ShufVT.getVectorElementType());
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004672
Craig Topper3d092db2012-03-21 02:14:01 +00004673 SDValue NewV = (Elt < (int)NumElems) ? N->getOperand(0)
Craig Topperd978c542012-05-06 19:46:21 +00004674 : N->getOperand(1);
Craig Topper3d092db2012-03-21 02:14:01 +00004675 return getShuffleScalarElt(NewV.getNode(), Elt % NumElems, DAG,
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004676 Depth+1);
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004677 }
4678
4679 // Actual nodes that may contain scalar elements
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004680 if (Opcode == ISD::BITCAST) {
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004681 V = V.getOperand(0);
4682 EVT SrcVT = V.getValueType();
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00004683 unsigned NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004684
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00004685 if (!SrcVT.isVector() || SrcVT.getVectorNumElements() != NumElems)
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004686 return SDValue();
4687 }
4688
4689 if (V.getOpcode() == ISD::SCALAR_TO_VECTOR)
4690 return (Index == 0) ? V.getOperand(0)
Craig Topper3d092db2012-03-21 02:14:01 +00004691 : DAG.getUNDEF(VT.getVectorElementType());
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004692
4693 if (V.getOpcode() == ISD::BUILD_VECTOR)
4694 return V.getOperand(Index);
4695
4696 return SDValue();
4697}
4698
4699/// getNumOfConsecutiveZeros - Return the number of elements of a vector
4700/// shuffle operation which come from a consecutively from a zero. The
Chris Lattner7a2bdde2011-04-15 05:18:47 +00004701/// search can start in two different directions, from left or right.
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004702static
Craig Topper3d092db2012-03-21 02:14:01 +00004703unsigned getNumOfConsecutiveZeros(ShuffleVectorSDNode *SVOp, unsigned NumElems,
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004704 bool ZerosFromLeft, SelectionDAG &DAG) {
Craig Topper3d092db2012-03-21 02:14:01 +00004705 unsigned i;
4706 for (i = 0; i != NumElems; ++i) {
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004707 unsigned Index = ZerosFromLeft ? i : NumElems-i-1;
Craig Topper3d092db2012-03-21 02:14:01 +00004708 SDValue Elt = getShuffleScalarElt(SVOp, Index, DAG, 0);
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004709 if (!(Elt.getNode() &&
4710 (Elt.getOpcode() == ISD::UNDEF || X86::isZeroNode(Elt))))
4711 break;
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004712 }
4713
4714 return i;
4715}
4716
Craig Topper3d092db2012-03-21 02:14:01 +00004717/// isShuffleMaskConsecutive - Check if the shuffle mask indicies [MaskI, MaskE)
4718/// correspond consecutively to elements from one of the vector operands,
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004719/// starting from its index OpIdx. Also tell OpNum which source vector operand.
4720static
Craig Topper3d092db2012-03-21 02:14:01 +00004721bool isShuffleMaskConsecutive(ShuffleVectorSDNode *SVOp,
4722 unsigned MaskI, unsigned MaskE, unsigned OpIdx,
4723 unsigned NumElems, unsigned &OpNum) {
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004724 bool SeenV1 = false;
4725 bool SeenV2 = false;
4726
Craig Topper3d092db2012-03-21 02:14:01 +00004727 for (unsigned i = MaskI; i != MaskE; ++i, ++OpIdx) {
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004728 int Idx = SVOp->getMaskElt(i);
4729 // Ignore undef indicies
4730 if (Idx < 0)
4731 continue;
4732
Craig Topper3d092db2012-03-21 02:14:01 +00004733 if (Idx < (int)NumElems)
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004734 SeenV1 = true;
4735 else
4736 SeenV2 = true;
4737
4738 // Only accept consecutive elements from the same vector
4739 if ((Idx % NumElems != OpIdx) || (SeenV1 && SeenV2))
4740 return false;
4741 }
4742
4743 OpNum = SeenV1 ? 0 : 1;
4744 return true;
4745}
4746
4747/// isVectorShiftRight - Returns true if the shuffle can be implemented as a
4748/// logical left shift of a vector.
4749static bool isVectorShiftRight(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
4750 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
4751 unsigned NumElems = SVOp->getValueType(0).getVectorNumElements();
4752 unsigned NumZeros = getNumOfConsecutiveZeros(SVOp, NumElems,
4753 false /* check zeros from right */, DAG);
4754 unsigned OpSrc;
4755
4756 if (!NumZeros)
4757 return false;
4758
4759 // Considering the elements in the mask that are not consecutive zeros,
4760 // check if they consecutively come from only one of the source vectors.
4761 //
4762 // V1 = {X, A, B, C} 0
4763 // \ \ \ /
4764 // vector_shuffle V1, V2 <1, 2, 3, X>
4765 //
4766 if (!isShuffleMaskConsecutive(SVOp,
4767 0, // Mask Start Index
Craig Topper3d092db2012-03-21 02:14:01 +00004768 NumElems-NumZeros, // Mask End Index(exclusive)
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004769 NumZeros, // Where to start looking in the src vector
4770 NumElems, // Number of elements in vector
4771 OpSrc)) // Which source operand ?
4772 return false;
4773
4774 isLeft = false;
4775 ShAmt = NumZeros;
4776 ShVal = SVOp->getOperand(OpSrc);
4777 return true;
4778}
4779
4780/// isVectorShiftLeft - Returns true if the shuffle can be implemented as a
4781/// logical left shift of a vector.
4782static bool isVectorShiftLeft(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
4783 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
4784 unsigned NumElems = SVOp->getValueType(0).getVectorNumElements();
4785 unsigned NumZeros = getNumOfConsecutiveZeros(SVOp, NumElems,
4786 true /* check zeros from left */, DAG);
4787 unsigned OpSrc;
4788
4789 if (!NumZeros)
4790 return false;
4791
4792 // Considering the elements in the mask that are not consecutive zeros,
4793 // check if they consecutively come from only one of the source vectors.
4794 //
4795 // 0 { A, B, X, X } = V2
4796 // / \ / /
4797 // vector_shuffle V1, V2 <X, X, 4, 5>
4798 //
4799 if (!isShuffleMaskConsecutive(SVOp,
4800 NumZeros, // Mask Start Index
Craig Topper3d092db2012-03-21 02:14:01 +00004801 NumElems, // Mask End Index(exclusive)
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004802 0, // Where to start looking in the src vector
4803 NumElems, // Number of elements in vector
4804 OpSrc)) // Which source operand ?
4805 return false;
4806
4807 isLeft = true;
4808 ShAmt = NumZeros;
4809 ShVal = SVOp->getOperand(OpSrc);
4810 return true;
Evan Chengf26ffe92008-05-29 08:22:04 +00004811}
4812
4813/// isVectorShift - Returns true if the shuffle can be implemented as a
4814/// logical left or right shift of a vector.
Nate Begeman9008ca62009-04-27 18:41:29 +00004815static bool isVectorShift(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
Dan Gohman475871a2008-07-27 21:46:04 +00004816 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00004817 // Although the logic below support any bitwidth size, there are no
4818 // shift instructions which handle more than 128-bit vectors.
Craig Topper7a9a28b2012-08-12 02:23:29 +00004819 if (!SVOp->getValueType(0).is128BitVector())
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00004820 return false;
4821
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004822 if (isVectorShiftLeft(SVOp, DAG, isLeft, ShVal, ShAmt) ||
4823 isVectorShiftRight(SVOp, DAG, isLeft, ShVal, ShAmt))
4824 return true;
Evan Chengf26ffe92008-05-29 08:22:04 +00004825
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004826 return false;
Evan Chengf26ffe92008-05-29 08:22:04 +00004827}
4828
Evan Chengc78d3b42006-04-24 18:01:45 +00004829/// LowerBuildVectorv16i8 - Custom lower build_vector of v16i8.
4830///
Dan Gohman475871a2008-07-27 21:46:04 +00004831static SDValue LowerBuildVectorv16i8(SDValue Op, unsigned NonZeros,
Evan Chengc78d3b42006-04-24 18:01:45 +00004832 unsigned NumNonZero, unsigned NumZero,
Dan Gohmand858e902010-04-17 15:26:15 +00004833 SelectionDAG &DAG,
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00004834 const X86Subtarget* Subtarget,
Dan Gohmand858e902010-04-17 15:26:15 +00004835 const TargetLowering &TLI) {
Evan Chengc78d3b42006-04-24 18:01:45 +00004836 if (NumNonZero > 8)
Dan Gohman475871a2008-07-27 21:46:04 +00004837 return SDValue();
Evan Chengc78d3b42006-04-24 18:01:45 +00004838
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004839 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00004840 SDValue V(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00004841 bool First = true;
4842 for (unsigned i = 0; i < 16; ++i) {
4843 bool ThisIsNonZero = (NonZeros & (1 << i)) != 0;
4844 if (ThisIsNonZero && First) {
4845 if (NumZero)
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00004846 V = getZeroVector(MVT::v8i16, Subtarget, DAG, dl);
Evan Chengc78d3b42006-04-24 18:01:45 +00004847 else
Owen Anderson825b72b2009-08-11 20:47:22 +00004848 V = DAG.getUNDEF(MVT::v8i16);
Evan Chengc78d3b42006-04-24 18:01:45 +00004849 First = false;
4850 }
4851
4852 if ((i & 1) != 0) {
Dan Gohman475871a2008-07-27 21:46:04 +00004853 SDValue ThisElt(0, 0), LastElt(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00004854 bool LastIsNonZero = (NonZeros & (1 << (i-1))) != 0;
4855 if (LastIsNonZero) {
Scott Michelfdc40a02009-02-17 22:15:04 +00004856 LastElt = DAG.getNode(ISD::ZERO_EXTEND, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004857 MVT::i16, Op.getOperand(i-1));
Evan Chengc78d3b42006-04-24 18:01:45 +00004858 }
4859 if (ThisIsNonZero) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004860 ThisElt = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i16, Op.getOperand(i));
4861 ThisElt = DAG.getNode(ISD::SHL, dl, MVT::i16,
4862 ThisElt, DAG.getConstant(8, MVT::i8));
Evan Chengc78d3b42006-04-24 18:01:45 +00004863 if (LastIsNonZero)
Owen Anderson825b72b2009-08-11 20:47:22 +00004864 ThisElt = DAG.getNode(ISD::OR, dl, MVT::i16, ThisElt, LastElt);
Evan Chengc78d3b42006-04-24 18:01:45 +00004865 } else
4866 ThisElt = LastElt;
4867
Gabor Greifba36cb52008-08-28 21:40:38 +00004868 if (ThisElt.getNode())
Owen Anderson825b72b2009-08-11 20:47:22 +00004869 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, V, ThisElt,
Chris Lattner0bd48932008-01-17 07:00:52 +00004870 DAG.getIntPtrConstant(i/2));
Evan Chengc78d3b42006-04-24 18:01:45 +00004871 }
4872 }
4873
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004874 return DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, V);
Evan Chengc78d3b42006-04-24 18:01:45 +00004875}
4876
Bill Wendlinga348c562007-03-22 18:42:45 +00004877/// LowerBuildVectorv8i16 - Custom lower build_vector of v8i16.
Evan Chengc78d3b42006-04-24 18:01:45 +00004878///
Dan Gohman475871a2008-07-27 21:46:04 +00004879static SDValue LowerBuildVectorv8i16(SDValue Op, unsigned NonZeros,
Dan Gohmand858e902010-04-17 15:26:15 +00004880 unsigned NumNonZero, unsigned NumZero,
4881 SelectionDAG &DAG,
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00004882 const X86Subtarget* Subtarget,
Dan Gohmand858e902010-04-17 15:26:15 +00004883 const TargetLowering &TLI) {
Evan Chengc78d3b42006-04-24 18:01:45 +00004884 if (NumNonZero > 4)
Dan Gohman475871a2008-07-27 21:46:04 +00004885 return SDValue();
Evan Chengc78d3b42006-04-24 18:01:45 +00004886
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004887 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00004888 SDValue V(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00004889 bool First = true;
4890 for (unsigned i = 0; i < 8; ++i) {
4891 bool isNonZero = (NonZeros & (1 << i)) != 0;
4892 if (isNonZero) {
4893 if (First) {
4894 if (NumZero)
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00004895 V = getZeroVector(MVT::v8i16, Subtarget, DAG, dl);
Evan Chengc78d3b42006-04-24 18:01:45 +00004896 else
Owen Anderson825b72b2009-08-11 20:47:22 +00004897 V = DAG.getUNDEF(MVT::v8i16);
Evan Chengc78d3b42006-04-24 18:01:45 +00004898 First = false;
4899 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004900 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004901 MVT::v8i16, V, Op.getOperand(i),
Chris Lattner0bd48932008-01-17 07:00:52 +00004902 DAG.getIntPtrConstant(i));
Evan Chengc78d3b42006-04-24 18:01:45 +00004903 }
4904 }
4905
4906 return V;
4907}
4908
Evan Chengf26ffe92008-05-29 08:22:04 +00004909/// getVShift - Return a vector logical shift node.
4910///
Owen Andersone50ed302009-08-10 22:56:29 +00004911static SDValue getVShift(bool isLeft, EVT VT, SDValue SrcOp,
Nate Begeman9008ca62009-04-27 18:41:29 +00004912 unsigned NumBits, SelectionDAG &DAG,
4913 const TargetLowering &TLI, DebugLoc dl) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00004914 assert(VT.is128BitVector() && "Unknown type for VShift");
Dale Johannesen0488fb62010-09-30 23:57:10 +00004915 EVT ShVT = MVT::v2i64;
Craig Toppered2e13d2012-01-22 19:15:14 +00004916 unsigned Opc = isLeft ? X86ISD::VSHLDQ : X86ISD::VSRLDQ;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004917 SrcOp = DAG.getNode(ISD::BITCAST, dl, ShVT, SrcOp);
4918 return DAG.getNode(ISD::BITCAST, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00004919 DAG.getNode(Opc, dl, ShVT, SrcOp,
Owen Anderson95771af2011-02-25 21:41:48 +00004920 DAG.getConstant(NumBits,
4921 TLI.getShiftAmountTy(SrcOp.getValueType()))));
Evan Chengf26ffe92008-05-29 08:22:04 +00004922}
4923
Dan Gohman475871a2008-07-27 21:46:04 +00004924SDValue
Evan Chengc3630942009-12-09 21:00:30 +00004925X86TargetLowering::LowerAsSplatVectorLoad(SDValue SrcOp, EVT VT, DebugLoc dl,
Dan Gohmand858e902010-04-17 15:26:15 +00004926 SelectionDAG &DAG) const {
Michael J. Spencerec38de22010-10-10 22:04:20 +00004927
Evan Chengc3630942009-12-09 21:00:30 +00004928 // Check if the scalar load can be widened into a vector load. And if
4929 // the address is "base + cst" see if the cst can be "absorbed" into
4930 // the shuffle mask.
4931 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(SrcOp)) {
4932 SDValue Ptr = LD->getBasePtr();
4933 if (!ISD::isNormalLoad(LD) || LD->isVolatile())
4934 return SDValue();
4935 EVT PVT = LD->getValueType(0);
4936 if (PVT != MVT::i32 && PVT != MVT::f32)
4937 return SDValue();
4938
4939 int FI = -1;
4940 int64_t Offset = 0;
4941 if (FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr)) {
4942 FI = FINode->getIndex();
4943 Offset = 0;
Chris Lattner0a9481f2011-02-13 22:25:43 +00004944 } else if (DAG.isBaseWithConstantOffset(Ptr) &&
Evan Chengc3630942009-12-09 21:00:30 +00004945 isa<FrameIndexSDNode>(Ptr.getOperand(0))) {
4946 FI = cast<FrameIndexSDNode>(Ptr.getOperand(0))->getIndex();
4947 Offset = Ptr.getConstantOperandVal(1);
4948 Ptr = Ptr.getOperand(0);
4949 } else {
4950 return SDValue();
4951 }
4952
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004953 // FIXME: 256-bit vector instructions don't require a strict alignment,
4954 // improve this code to support it better.
4955 unsigned RequiredAlign = VT.getSizeInBits()/8;
Evan Chengc3630942009-12-09 21:00:30 +00004956 SDValue Chain = LD->getChain();
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004957 // Make sure the stack object alignment is at least 16 or 32.
Evan Chengc3630942009-12-09 21:00:30 +00004958 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004959 if (DAG.InferPtrAlignment(Ptr) < RequiredAlign) {
Evan Chengc3630942009-12-09 21:00:30 +00004960 if (MFI->isFixedObjectIndex(FI)) {
Eric Christophere9625cf2010-01-23 06:02:43 +00004961 // Can't change the alignment. FIXME: It's possible to compute
4962 // the exact stack offset and reference FI + adjust offset instead.
4963 // If someone *really* cares about this. That's the way to implement it.
4964 return SDValue();
Evan Chengc3630942009-12-09 21:00:30 +00004965 } else {
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004966 MFI->setObjectAlignment(FI, RequiredAlign);
Evan Chengc3630942009-12-09 21:00:30 +00004967 }
4968 }
4969
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004970 // (Offset % 16 or 32) must be multiple of 4. Then address is then
Evan Chengc3630942009-12-09 21:00:30 +00004971 // Ptr + (Offset & ~15).
4972 if (Offset < 0)
4973 return SDValue();
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004974 if ((Offset % RequiredAlign) & 3)
Evan Chengc3630942009-12-09 21:00:30 +00004975 return SDValue();
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004976 int64_t StartOffset = Offset & ~(RequiredAlign-1);
Evan Chengc3630942009-12-09 21:00:30 +00004977 if (StartOffset)
4978 Ptr = DAG.getNode(ISD::ADD, Ptr.getDebugLoc(), Ptr.getValueType(),
4979 Ptr,DAG.getConstant(StartOffset, Ptr.getValueType()));
4980
4981 int EltNo = (Offset - StartOffset) >> 2;
Craig Topper66ddd152012-04-27 22:54:43 +00004982 unsigned NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004983
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004984 EVT NVT = EVT::getVectorVT(*DAG.getContext(), PVT, NumElems);
4985 SDValue V1 = DAG.getLoad(NVT, dl, Chain, Ptr,
Chris Lattner51abfe42010-09-21 06:02:19 +00004986 LD->getPointerInfo().getWithOffset(StartOffset),
Pete Cooperd752e0f2011-11-08 18:42:53 +00004987 false, false, false, 0);
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004988
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004989 SmallVector<int, 8> Mask;
Craig Topper66ddd152012-04-27 22:54:43 +00004990 for (unsigned i = 0; i != NumElems; ++i)
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004991 Mask.push_back(EltNo);
4992
Craig Toppercc3000632012-01-30 07:50:31 +00004993 return DAG.getVectorShuffle(NVT, dl, V1, DAG.getUNDEF(NVT), &Mask[0]);
Evan Chengc3630942009-12-09 21:00:30 +00004994 }
4995
4996 return SDValue();
4997}
4998
Michael J. Spencerec38de22010-10-10 22:04:20 +00004999/// EltsFromConsecutiveLoads - Given the initializing elements 'Elts' of a
5000/// vector of type 'VT', see if the elements can be replaced by a single large
Nate Begeman1449f292010-03-24 22:19:06 +00005001/// load which has the same value as a build_vector whose operands are 'elts'.
5002///
5003/// Example: <load i32 *a, load i32 *a+4, undef, undef> -> zextload a
Michael J. Spencerec38de22010-10-10 22:04:20 +00005004///
Nate Begeman1449f292010-03-24 22:19:06 +00005005/// FIXME: we'd also like to handle the case where the last elements are zero
5006/// rather than undef via VZEXT_LOAD, but we do not detect that case today.
5007/// There's even a handy isZeroNode for that purpose.
Nate Begemanfdea31a2010-03-24 20:49:50 +00005008static SDValue EltsFromConsecutiveLoads(EVT VT, SmallVectorImpl<SDValue> &Elts,
Chris Lattner88641552010-09-22 00:34:38 +00005009 DebugLoc &DL, SelectionDAG &DAG) {
Nate Begemanfdea31a2010-03-24 20:49:50 +00005010 EVT EltVT = VT.getVectorElementType();
5011 unsigned NumElems = Elts.size();
Michael J. Spencerec38de22010-10-10 22:04:20 +00005012
Nate Begemanfdea31a2010-03-24 20:49:50 +00005013 LoadSDNode *LDBase = NULL;
5014 unsigned LastLoadedElt = -1U;
Michael J. Spencerec38de22010-10-10 22:04:20 +00005015
Nate Begeman1449f292010-03-24 22:19:06 +00005016 // For each element in the initializer, see if we've found a load or an undef.
Michael J. Spencerec38de22010-10-10 22:04:20 +00005017 // If we don't find an initial load element, or later load elements are
Nate Begeman1449f292010-03-24 22:19:06 +00005018 // non-consecutive, bail out.
Nate Begemanfdea31a2010-03-24 20:49:50 +00005019 for (unsigned i = 0; i < NumElems; ++i) {
5020 SDValue Elt = Elts[i];
Michael J. Spencerec38de22010-10-10 22:04:20 +00005021
Nate Begemanfdea31a2010-03-24 20:49:50 +00005022 if (!Elt.getNode() ||
5023 (Elt.getOpcode() != ISD::UNDEF && !ISD::isNON_EXTLoad(Elt.getNode())))
5024 return SDValue();
5025 if (!LDBase) {
5026 if (Elt.getNode()->getOpcode() == ISD::UNDEF)
5027 return SDValue();
5028 LDBase = cast<LoadSDNode>(Elt.getNode());
5029 LastLoadedElt = i;
5030 continue;
5031 }
5032 if (Elt.getOpcode() == ISD::UNDEF)
5033 continue;
5034
5035 LoadSDNode *LD = cast<LoadSDNode>(Elt);
5036 if (!DAG.isConsecutiveLoad(LD, LDBase, EltVT.getSizeInBits()/8, i))
5037 return SDValue();
5038 LastLoadedElt = i;
5039 }
Nate Begeman1449f292010-03-24 22:19:06 +00005040
5041 // If we have found an entire vector of loads and undefs, then return a large
5042 // load of the entire vector width starting at the base pointer. If we found
5043 // consecutive loads for the low half, generate a vzext_load node.
Nate Begemanfdea31a2010-03-24 20:49:50 +00005044 if (LastLoadedElt == NumElems - 1) {
5045 if (DAG.InferPtrAlignment(LDBase->getBasePtr()) >= 16)
Chris Lattner88641552010-09-22 00:34:38 +00005046 return DAG.getLoad(VT, DL, LDBase->getChain(), LDBase->getBasePtr(),
Chris Lattner51abfe42010-09-21 06:02:19 +00005047 LDBase->getPointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00005048 LDBase->isVolatile(), LDBase->isNonTemporal(),
5049 LDBase->isInvariant(), 0);
Chris Lattner88641552010-09-22 00:34:38 +00005050 return DAG.getLoad(VT, DL, LDBase->getChain(), LDBase->getBasePtr(),
Chris Lattner51abfe42010-09-21 06:02:19 +00005051 LDBase->getPointerInfo(),
Nate Begemanfdea31a2010-03-24 20:49:50 +00005052 LDBase->isVolatile(), LDBase->isNonTemporal(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00005053 LDBase->isInvariant(), LDBase->getAlignment());
Craig Topper69947b92012-04-23 06:57:04 +00005054 }
5055 if (NumElems == 4 && LastLoadedElt == 1 &&
5056 DAG.getTargetLoweringInfo().isTypeLegal(MVT::v2i64)) {
Nate Begemanfdea31a2010-03-24 20:49:50 +00005057 SDVTList Tys = DAG.getVTList(MVT::v2i64, MVT::Other);
5058 SDValue Ops[] = { LDBase->getChain(), LDBase->getBasePtr() };
Eli Friedman322ea082011-09-14 23:42:45 +00005059 SDValue ResNode =
5060 DAG.getMemIntrinsicNode(X86ISD::VZEXT_LOAD, DL, Tys, Ops, 2, MVT::i64,
5061 LDBase->getPointerInfo(),
5062 LDBase->getAlignment(),
5063 false/*isVolatile*/, true/*ReadMem*/,
5064 false/*WriteMem*/);
Manman Ren2b7a2e82012-08-31 23:16:57 +00005065
5066 // Make sure the newly-created LOAD is in the same position as LDBase in
5067 // terms of dependency. We create a TokenFactor for LDBase and ResNode, and
5068 // update uses of LDBase's output chain to use the TokenFactor.
5069 if (LDBase->hasAnyUseOfValue(1)) {
5070 SDValue NewChain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other,
5071 SDValue(LDBase, 1), SDValue(ResNode.getNode(), 1));
5072 DAG.ReplaceAllUsesOfValueWith(SDValue(LDBase, 1), NewChain);
5073 DAG.UpdateNodeOperands(NewChain.getNode(), SDValue(LDBase, 1),
5074 SDValue(ResNode.getNode(), 1));
5075 }
5076
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005077 return DAG.getNode(ISD::BITCAST, DL, VT, ResNode);
Nate Begemanfdea31a2010-03-24 20:49:50 +00005078 }
5079 return SDValue();
5080}
5081
Nadav Rotem9d68b062012-04-08 12:54:54 +00005082/// LowerVectorBroadcast - Attempt to use the vbroadcast instruction
5083/// to generate a splat value for the following cases:
5084/// 1. A splat BUILD_VECTOR which uses a single scalar load, or a constant.
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005085/// 2. A splat shuffle which uses a scalar_to_vector node which comes from
Nadav Rotem9d68b062012-04-08 12:54:54 +00005086/// a scalar load, or a constant.
5087/// The VBROADCAST node is returned when a pattern is found,
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00005088/// or SDValue() otherwise.
Nadav Rotem154819d2012-04-09 07:45:58 +00005089SDValue
Craig Topper55b24052012-09-11 06:15:32 +00005090X86TargetLowering::LowerVectorBroadcast(SDValue Op, SelectionDAG &DAG) const {
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00005091 if (!Subtarget->hasFp256())
Craig Toppera9376332012-01-10 08:23:59 +00005092 return SDValue();
5093
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005094 EVT VT = Op.getValueType();
Nadav Rotem154819d2012-04-09 07:45:58 +00005095 DebugLoc dl = Op.getDebugLoc();
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005096
Craig Topper5da8a802012-05-04 05:49:51 +00005097 assert((VT.is128BitVector() || VT.is256BitVector()) &&
5098 "Unsupported vector type for broadcast.");
5099
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005100 SDValue Ld;
Nadav Rotem9d68b062012-04-08 12:54:54 +00005101 bool ConstSplatVal;
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005102
Nadav Rotem9d68b062012-04-08 12:54:54 +00005103 switch (Op.getOpcode()) {
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005104 default:
5105 // Unknown pattern found.
5106 return SDValue();
5107
5108 case ISD::BUILD_VECTOR: {
5109 // The BUILD_VECTOR node must be a splat.
Nadav Rotem9d68b062012-04-08 12:54:54 +00005110 if (!isSplatVector(Op.getNode()))
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005111 return SDValue();
5112
Nadav Rotem9d68b062012-04-08 12:54:54 +00005113 Ld = Op.getOperand(0);
5114 ConstSplatVal = (Ld.getOpcode() == ISD::Constant ||
5115 Ld.getOpcode() == ISD::ConstantFP);
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00005116
5117 // The suspected load node has several users. Make sure that all
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005118 // of its users are from the BUILD_VECTOR node.
Nadav Rotem9d68b062012-04-08 12:54:54 +00005119 // Constants may have multiple users.
5120 if (!ConstSplatVal && !Ld->hasNUsesOfValue(VT.getVectorNumElements(), 0))
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005121 return SDValue();
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00005122 break;
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005123 }
5124
5125 case ISD::VECTOR_SHUFFLE: {
5126 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
5127
5128 // Shuffles must have a splat mask where the first element is
5129 // broadcasted.
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00005130 if ((!SVOp->isSplat()) || SVOp->getMaskElt(0) != 0)
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005131 return SDValue();
5132
5133 SDValue Sc = Op.getOperand(0);
Nadav Rotemb88e8dd2012-05-10 12:50:02 +00005134 if (Sc.getOpcode() != ISD::SCALAR_TO_VECTOR &&
Elena Demikhovsky8f40f7b2012-07-01 06:12:26 +00005135 Sc.getOpcode() != ISD::BUILD_VECTOR) {
5136
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00005137 if (!Subtarget->hasInt256())
Elena Demikhovsky8f40f7b2012-07-01 06:12:26 +00005138 return SDValue();
5139
5140 // Use the register form of the broadcast instruction available on AVX2.
5141 if (VT.is256BitVector())
5142 Sc = Extract128BitVector(Sc, 0, DAG, dl);
5143 return DAG.getNode(X86ISD::VBROADCAST, dl, VT, Sc);
5144 }
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005145
5146 Ld = Sc.getOperand(0);
Nadav Rotem9d68b062012-04-08 12:54:54 +00005147 ConstSplatVal = (Ld.getOpcode() == ISD::Constant ||
Nadav Rotem154819d2012-04-09 07:45:58 +00005148 Ld.getOpcode() == ISD::ConstantFP);
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005149
5150 // The scalar_to_vector node and the suspected
5151 // load node must have exactly one user.
Nadav Rotem9d68b062012-04-08 12:54:54 +00005152 // Constants may have multiple users.
5153 if (!ConstSplatVal && (!Sc.hasOneUse() || !Ld.hasOneUse()))
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005154 return SDValue();
5155 break;
5156 }
5157 }
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00005158
Craig Topper7a9a28b2012-08-12 02:23:29 +00005159 bool Is256 = VT.is256BitVector();
Nadav Rotem9d68b062012-04-08 12:54:54 +00005160
5161 // Handle the broadcasting a single constant scalar from the constant pool
5162 // into a vector. On Sandybridge it is still better to load a constant vector
5163 // from the constant pool and not to broadcast it from a scalar.
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00005164 if (ConstSplatVal && Subtarget->hasInt256()) {
Nadav Rotem9d68b062012-04-08 12:54:54 +00005165 EVT CVT = Ld.getValueType();
5166 assert(!CVT.isVector() && "Must not broadcast a vector type");
5167 unsigned ScalarSize = CVT.getSizeInBits();
5168
Craig Topper5da8a802012-05-04 05:49:51 +00005169 if (ScalarSize == 32 || (Is256 && ScalarSize == 64)) {
Nadav Rotem9d68b062012-04-08 12:54:54 +00005170 const Constant *C = 0;
5171 if (ConstantSDNode *CI = dyn_cast<ConstantSDNode>(Ld))
5172 C = CI->getConstantIntValue();
5173 else if (ConstantFPSDNode *CF = dyn_cast<ConstantFPSDNode>(Ld))
5174 C = CF->getConstantFPValue();
5175
5176 assert(C && "Invalid constant type");
5177
Nadav Rotem154819d2012-04-09 07:45:58 +00005178 SDValue CP = DAG.getConstantPool(C, getPointerTy());
Nadav Rotem9d68b062012-04-08 12:54:54 +00005179 unsigned Alignment = cast<ConstantPoolSDNode>(CP)->getAlignment();
Nadav Rotem154819d2012-04-09 07:45:58 +00005180 Ld = DAG.getLoad(CVT, dl, DAG.getEntryNode(), CP,
Craig Topper6643d9c2012-05-04 06:18:33 +00005181 MachinePointerInfo::getConstantPool(),
5182 false, false, false, Alignment);
Nadav Rotem9d68b062012-04-08 12:54:54 +00005183
Nadav Rotem9d68b062012-04-08 12:54:54 +00005184 return DAG.getNode(X86ISD::VBROADCAST, dl, VT, Ld);
5185 }
5186 }
5187
Nadav Rotem4fc8a5d2012-05-19 19:57:37 +00005188 bool IsLoad = ISD::isNormalLoad(Ld.getNode());
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005189 unsigned ScalarSize = Ld.getValueType().getSizeInBits();
5190
Nadav Rotem4fc8a5d2012-05-19 19:57:37 +00005191 // Handle AVX2 in-register broadcasts.
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00005192 if (!IsLoad && Subtarget->hasInt256() &&
Nadav Rotem4fc8a5d2012-05-19 19:57:37 +00005193 (ScalarSize == 32 || (Is256 && ScalarSize == 64)))
5194 return DAG.getNode(X86ISD::VBROADCAST, dl, VT, Ld);
5195
5196 // The scalar source must be a normal load.
5197 if (!IsLoad)
5198 return SDValue();
5199
Craig Topper5da8a802012-05-04 05:49:51 +00005200 if (ScalarSize == 32 || (Is256 && ScalarSize == 64))
Nadav Rotem9d68b062012-04-08 12:54:54 +00005201 return DAG.getNode(X86ISD::VBROADCAST, dl, VT, Ld);
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005202
Craig Toppera9376332012-01-10 08:23:59 +00005203 // The integer check is needed for the 64-bit into 128-bit so it doesn't match
Craig Topper5da8a802012-05-04 05:49:51 +00005204 // double since there is no vbroadcastsd xmm
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00005205 if (Subtarget->hasInt256() && Ld.getValueType().isInteger()) {
Craig Topper5da8a802012-05-04 05:49:51 +00005206 if (ScalarSize == 8 || ScalarSize == 16 || ScalarSize == 64)
Nadav Rotem9d68b062012-04-08 12:54:54 +00005207 return DAG.getNode(X86ISD::VBROADCAST, dl, VT, Ld);
Craig Toppera9376332012-01-10 08:23:59 +00005208 }
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00005209
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005210 // Unsupported broadcast.
5211 return SDValue();
5212}
5213
Evan Chengc3630942009-12-09 21:00:30 +00005214SDValue
Michael Liaofacace82012-10-19 17:15:18 +00005215X86TargetLowering::buildFromShuffleMostly(SDValue Op, SelectionDAG &DAG) const {
5216 EVT VT = Op.getValueType();
5217
5218 // Skip if insert_vec_elt is not supported.
5219 if (!isOperationLegalOrCustom(ISD::INSERT_VECTOR_ELT, VT))
5220 return SDValue();
5221
5222 DebugLoc DL = Op.getDebugLoc();
5223 unsigned NumElems = Op.getNumOperands();
5224
5225 SDValue VecIn1;
5226 SDValue VecIn2;
5227 SmallVector<unsigned, 4> InsertIndices;
5228 SmallVector<int, 8> Mask(NumElems, -1);
5229
5230 for (unsigned i = 0; i != NumElems; ++i) {
5231 unsigned Opc = Op.getOperand(i).getOpcode();
5232
5233 if (Opc == ISD::UNDEF)
5234 continue;
5235
5236 if (Opc != ISD::EXTRACT_VECTOR_ELT) {
5237 // Quit if more than 1 elements need inserting.
5238 if (InsertIndices.size() > 1)
5239 return SDValue();
5240
5241 InsertIndices.push_back(i);
5242 continue;
5243 }
5244
5245 SDValue ExtractedFromVec = Op.getOperand(i).getOperand(0);
5246 SDValue ExtIdx = Op.getOperand(i).getOperand(1);
5247
5248 // Quit if extracted from vector of different type.
5249 if (ExtractedFromVec.getValueType() != VT)
5250 return SDValue();
5251
5252 // Quit if non-constant index.
5253 if (!isa<ConstantSDNode>(ExtIdx))
5254 return SDValue();
5255
5256 if (VecIn1.getNode() == 0)
5257 VecIn1 = ExtractedFromVec;
5258 else if (VecIn1 != ExtractedFromVec) {
5259 if (VecIn2.getNode() == 0)
5260 VecIn2 = ExtractedFromVec;
5261 else if (VecIn2 != ExtractedFromVec)
5262 // Quit if more than 2 vectors to shuffle
5263 return SDValue();
5264 }
5265
5266 unsigned Idx = cast<ConstantSDNode>(ExtIdx)->getZExtValue();
5267
5268 if (ExtractedFromVec == VecIn1)
5269 Mask[i] = Idx;
5270 else if (ExtractedFromVec == VecIn2)
5271 Mask[i] = Idx + NumElems;
5272 }
5273
5274 if (VecIn1.getNode() == 0)
5275 return SDValue();
5276
5277 VecIn2 = VecIn2.getNode() ? VecIn2 : DAG.getUNDEF(VT);
5278 SDValue NV = DAG.getVectorShuffle(VT, DL, VecIn1, VecIn2, &Mask[0]);
5279 for (unsigned i = 0, e = InsertIndices.size(); i != e; ++i) {
5280 unsigned Idx = InsertIndices[i];
5281 NV = DAG.getNode(ISD::INSERT_VECTOR_ELT, DL, VT, NV, Op.getOperand(Idx),
5282 DAG.getIntPtrConstant(Idx));
5283 }
5284
5285 return NV;
5286}
5287
5288SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00005289X86TargetLowering::LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005290 DebugLoc dl = Op.getDebugLoc();
David Greenea5f26012011-02-07 19:36:54 +00005291
David Greenef125a292011-02-08 19:04:41 +00005292 EVT VT = Op.getValueType();
5293 EVT ExtVT = VT.getVectorElementType();
David Greenef125a292011-02-08 19:04:41 +00005294 unsigned NumElems = Op.getNumOperands();
5295
Bruno Cardoso Lopes531f19f2011-08-01 19:51:53 +00005296 // Vectors containing all zeros can be matched by pxor and xorps later
5297 if (ISD::isBuildVectorAllZeros(Op.getNode())) {
5298 // Canonicalize this to <4 x i32> to 1) ensure the zero vectors are CSE'd
5299 // and 2) ensure that i64 scalars are eliminated on x86-32 hosts.
Craig Topper07a27622012-01-22 03:07:48 +00005300 if (VT == MVT::v4i32 || VT == MVT::v8i32)
Chris Lattner8a594482007-11-25 00:24:49 +00005301 return Op;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005302
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00005303 return getZeroVector(VT, Subtarget, DAG, dl);
Chris Lattner8a594482007-11-25 00:24:49 +00005304 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00005305
Bruno Cardoso Lopes531f19f2011-08-01 19:51:53 +00005306 // Vectors containing all ones can be matched by pcmpeqd on 128-bit width
Craig Topper745a86b2011-11-19 22:34:59 +00005307 // vectors or broken into v4i32 operations on 256-bit vectors. AVX2 can use
5308 // vpcmpeqd on 256-bit vectors.
Bruno Cardoso Lopes531f19f2011-08-01 19:51:53 +00005309 if (ISD::isBuildVectorAllOnes(Op.getNode())) {
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00005310 if (VT == MVT::v4i32 || (VT == MVT::v8i32 && Subtarget->hasInt256()))
Bruno Cardoso Lopes531f19f2011-08-01 19:51:53 +00005311 return Op;
5312
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00005313 return getOnesVector(VT, Subtarget->hasInt256(), DAG, dl);
Bruno Cardoso Lopes531f19f2011-08-01 19:51:53 +00005314 }
5315
Nadav Rotem154819d2012-04-09 07:45:58 +00005316 SDValue Broadcast = LowerVectorBroadcast(Op, DAG);
Nadav Rotem9d68b062012-04-08 12:54:54 +00005317 if (Broadcast.getNode())
5318 return Broadcast;
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005319
Owen Andersone50ed302009-08-10 22:56:29 +00005320 unsigned EVTBits = ExtVT.getSizeInBits();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005321
Evan Cheng0db9fe62006-04-25 20:13:52 +00005322 unsigned NumZero = 0;
5323 unsigned NumNonZero = 0;
5324 unsigned NonZeros = 0;
Chris Lattnerc9517fb2008-03-08 22:48:29 +00005325 bool IsAllConstants = true;
Dan Gohman475871a2008-07-27 21:46:04 +00005326 SmallSet<SDValue, 8> Values;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005327 for (unsigned i = 0; i < NumElems; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00005328 SDValue Elt = Op.getOperand(i);
Evan Chengdb2d5242007-12-12 06:45:40 +00005329 if (Elt.getOpcode() == ISD::UNDEF)
5330 continue;
5331 Values.insert(Elt);
5332 if (Elt.getOpcode() != ISD::Constant &&
5333 Elt.getOpcode() != ISD::ConstantFP)
Chris Lattnerc9517fb2008-03-08 22:48:29 +00005334 IsAllConstants = false;
Evan Cheng37b73872009-07-30 08:33:02 +00005335 if (X86::isZeroNode(Elt))
Evan Chengdb2d5242007-12-12 06:45:40 +00005336 NumZero++;
5337 else {
5338 NonZeros |= (1 << i);
5339 NumNonZero++;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005340 }
5341 }
5342
Chris Lattner97a2a562010-08-26 05:24:29 +00005343 // All undef vector. Return an UNDEF. All zero vectors were handled above.
5344 if (NumNonZero == 0)
Dale Johannesene8d72302009-02-06 23:05:02 +00005345 return DAG.getUNDEF(VT);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005346
Chris Lattner67f453a2008-03-09 05:42:06 +00005347 // Special case for single non-zero, non-undef, element.
Eli Friedman10415532009-06-06 06:05:10 +00005348 if (NumNonZero == 1) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00005349 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dan Gohman475871a2008-07-27 21:46:04 +00005350 SDValue Item = Op.getOperand(Idx);
Scott Michelfdc40a02009-02-17 22:15:04 +00005351
Chris Lattner62098042008-03-09 01:05:04 +00005352 // If this is an insertion of an i64 value on x86-32, and if the top bits of
5353 // the value are obviously zero, truncate the value to i32 and do the
5354 // insertion that way. Only do this if the value is non-constant or if the
5355 // value is a constant being inserted into element 0. It is cheaper to do
5356 // a constant pool load than it is to do a movd + shuffle.
Owen Anderson825b72b2009-08-11 20:47:22 +00005357 if (ExtVT == MVT::i64 && !Subtarget->is64Bit() &&
Chris Lattner62098042008-03-09 01:05:04 +00005358 (!IsAllConstants || Idx == 0)) {
5359 if (DAG.MaskedValueIsZero(Item, APInt::getBitsSet(64, 32, 64))) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00005360 // Handle SSE only.
5361 assert(VT == MVT::v2i64 && "Expected an SSE value type!");
5362 EVT VecVT = MVT::v4i32;
5363 unsigned VecElts = 4;
Scott Michelfdc40a02009-02-17 22:15:04 +00005364
Chris Lattner62098042008-03-09 01:05:04 +00005365 // Truncate the value (which may itself be a constant) to i32, and
5366 // convert it to a vector with movd (S2V+shuffle to zero extend).
Owen Anderson825b72b2009-08-11 20:47:22 +00005367 Item = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Item);
Dale Johannesenace16102009-02-03 19:33:06 +00005368 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VecVT, Item);
Craig Topper12216172012-01-13 08:12:35 +00005369 Item = getShuffleVectorZeroOrUndef(Item, 0, true, Subtarget, DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +00005370
Chris Lattner62098042008-03-09 01:05:04 +00005371 // Now we have our 32-bit value zero extended in the low element of
5372 // a vector. If Idx != 0, swizzle it into place.
5373 if (Idx != 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005374 SmallVector<int, 4> Mask;
5375 Mask.push_back(Idx);
5376 for (unsigned i = 1; i != VecElts; ++i)
5377 Mask.push_back(i);
Craig Topperdf966f62012-04-22 19:17:57 +00005378 Item = DAG.getVectorShuffle(VecVT, dl, Item, DAG.getUNDEF(VecVT),
Nate Begeman9008ca62009-04-27 18:41:29 +00005379 &Mask[0]);
Chris Lattner62098042008-03-09 01:05:04 +00005380 }
Craig Topper07a27622012-01-22 03:07:48 +00005381 return DAG.getNode(ISD::BITCAST, dl, VT, Item);
Chris Lattner62098042008-03-09 01:05:04 +00005382 }
5383 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005384
Chris Lattner19f79692008-03-08 22:59:52 +00005385 // If we have a constant or non-constant insertion into the low element of
5386 // a vector, we can do this with SCALAR_TO_VECTOR + shuffle of zero into
5387 // the rest of the elements. This will be matched as movd/movq/movss/movsd
Eli Friedman10415532009-06-06 06:05:10 +00005388 // depending on what the source datatype is.
5389 if (Idx == 0) {
Craig Topperd62c16e2011-12-29 03:20:51 +00005390 if (NumZero == 0)
Eli Friedman10415532009-06-06 06:05:10 +00005391 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
Craig Topperd62c16e2011-12-29 03:20:51 +00005392
5393 if (ExtVT == MVT::i32 || ExtVT == MVT::f32 || ExtVT == MVT::f64 ||
Owen Anderson825b72b2009-08-11 20:47:22 +00005394 (ExtVT == MVT::i64 && Subtarget->is64Bit())) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00005395 if (VT.is256BitVector()) {
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00005396 SDValue ZeroVec = getZeroVector(VT, Subtarget, DAG, dl);
Nadav Rotem394a1f52012-01-11 14:07:51 +00005397 return DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, ZeroVec,
5398 Item, DAG.getIntPtrConstant(0));
Elena Demikhovsky021c0a22011-12-28 08:14:01 +00005399 }
Craig Topper7a9a28b2012-08-12 02:23:29 +00005400 assert(VT.is128BitVector() && "Expected an SSE value type!");
Eli Friedman10415532009-06-06 06:05:10 +00005401 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
5402 // Turn it into a MOVL (i.e. movss, movsd, or movd) to a zero vector.
Craig Topper12216172012-01-13 08:12:35 +00005403 return getShuffleVectorZeroOrUndef(Item, 0, true, Subtarget, DAG);
Craig Topperd62c16e2011-12-29 03:20:51 +00005404 }
5405
5406 if (ExtVT == MVT::i16 || ExtVT == MVT::i8) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005407 Item = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, Item);
Craig Topper3224e6b2011-12-29 03:09:33 +00005408 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32, Item);
Craig Topper7a9a28b2012-08-12 02:23:29 +00005409 if (VT.is256BitVector()) {
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00005410 SDValue ZeroVec = getZeroVector(MVT::v8i32, Subtarget, DAG, dl);
Craig Topperb14940a2012-04-22 20:55:18 +00005411 Item = Insert128BitVector(ZeroVec, Item, 0, DAG, dl);
Craig Topper19ec2a92011-12-29 03:34:54 +00005412 } else {
Craig Topper7a9a28b2012-08-12 02:23:29 +00005413 assert(VT.is128BitVector() && "Expected an SSE value type!");
Craig Topper12216172012-01-13 08:12:35 +00005414 Item = getShuffleVectorZeroOrUndef(Item, 0, true, Subtarget, DAG);
Craig Topper19ec2a92011-12-29 03:34:54 +00005415 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005416 return DAG.getNode(ISD::BITCAST, dl, VT, Item);
Eli Friedman10415532009-06-06 06:05:10 +00005417 }
Chris Lattnerc9517fb2008-03-08 22:48:29 +00005418 }
Evan Chengf26ffe92008-05-29 08:22:04 +00005419
5420 // Is it a vector logical left shift?
5421 if (NumElems == 2 && Idx == 1 &&
Evan Cheng37b73872009-07-30 08:33:02 +00005422 X86::isZeroNode(Op.getOperand(0)) &&
5423 !X86::isZeroNode(Op.getOperand(1))) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00005424 unsigned NumBits = VT.getSizeInBits();
Evan Chengf26ffe92008-05-29 08:22:04 +00005425 return getVShift(true, VT,
Scott Michelfdc40a02009-02-17 22:15:04 +00005426 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Dale Johannesenb300d2a2009-02-07 00:55:49 +00005427 VT, Op.getOperand(1)),
Dale Johannesenace16102009-02-03 19:33:06 +00005428 NumBits/2, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00005429 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005430
Chris Lattnerc9517fb2008-03-08 22:48:29 +00005431 if (IsAllConstants) // Otherwise, it's better to do a constpool load.
Dan Gohman475871a2008-07-27 21:46:04 +00005432 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005433
Chris Lattner19f79692008-03-08 22:59:52 +00005434 // Otherwise, if this is a vector with i32 or f32 elements, and the element
5435 // is a non-constant being inserted into an element other than the low one,
5436 // we can't use a constant pool load. Instead, use SCALAR_TO_VECTOR (aka
5437 // movd/movss) to move this into the low element, then shuffle it into
5438 // place.
Evan Cheng0db9fe62006-04-25 20:13:52 +00005439 if (EVTBits == 32) {
Dale Johannesenace16102009-02-03 19:33:06 +00005440 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
Scott Michelfdc40a02009-02-17 22:15:04 +00005441
Evan Cheng0db9fe62006-04-25 20:13:52 +00005442 // Turn it into a shuffle of zero and zero-extended scalar to vector.
Craig Topper12216172012-01-13 08:12:35 +00005443 Item = getShuffleVectorZeroOrUndef(Item, 0, NumZero > 0, Subtarget, DAG);
Nate Begeman9008ca62009-04-27 18:41:29 +00005444 SmallVector<int, 8> MaskVec;
Craig Topper31a207a2012-05-04 06:39:13 +00005445 for (unsigned i = 0; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00005446 MaskVec.push_back(i == Idx ? 0 : 1);
5447 return DAG.getVectorShuffle(VT, dl, Item, DAG.getUNDEF(VT), &MaskVec[0]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005448 }
5449 }
5450
Chris Lattner67f453a2008-03-09 05:42:06 +00005451 // Splat is obviously ok. Let legalizer expand it to a shuffle.
Evan Chengc3630942009-12-09 21:00:30 +00005452 if (Values.size() == 1) {
5453 if (EVTBits == 32) {
5454 // Instead of a shuffle like this:
5455 // shuffle (scalar_to_vector (load (ptr + 4))), undef, <0, 0, 0, 0>
5456 // Check if it's possible to issue this instead.
5457 // shuffle (vload ptr)), undef, <1, 1, 1, 1>
5458 unsigned Idx = CountTrailingZeros_32(NonZeros);
5459 SDValue Item = Op.getOperand(Idx);
5460 if (Op.getNode()->isOnlyUserOf(Item.getNode()))
5461 return LowerAsSplatVectorLoad(Item, VT, dl, DAG);
5462 }
Dan Gohman475871a2008-07-27 21:46:04 +00005463 return SDValue();
Evan Chengc3630942009-12-09 21:00:30 +00005464 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005465
Dan Gohmana3941172007-07-24 22:55:08 +00005466 // A vector full of immediates; various special cases are already
5467 // handled, so this is best done with a single constant-pool load.
Chris Lattnerc9517fb2008-03-08 22:48:29 +00005468 if (IsAllConstants)
Dan Gohman475871a2008-07-27 21:46:04 +00005469 return SDValue();
Dan Gohmana3941172007-07-24 22:55:08 +00005470
Bruno Cardoso Lopes6683efb2011-07-22 00:15:07 +00005471 // For AVX-length vectors, build the individual 128-bit pieces and use
5472 // shuffles to put them in place.
Craig Topper7a9a28b2012-08-12 02:23:29 +00005473 if (VT.is256BitVector()) {
Bruno Cardoso Lopes6683efb2011-07-22 00:15:07 +00005474 SmallVector<SDValue, 32> V;
Craig Topperfa5b70e2012-02-03 06:32:21 +00005475 for (unsigned i = 0; i != NumElems; ++i)
Bruno Cardoso Lopes6683efb2011-07-22 00:15:07 +00005476 V.push_back(Op.getOperand(i));
5477
5478 EVT HVT = EVT::getVectorVT(*DAG.getContext(), ExtVT, NumElems/2);
5479
5480 // Build both the lower and upper subvector.
5481 SDValue Lower = DAG.getNode(ISD::BUILD_VECTOR, dl, HVT, &V[0], NumElems/2);
5482 SDValue Upper = DAG.getNode(ISD::BUILD_VECTOR, dl, HVT, &V[NumElems / 2],
5483 NumElems/2);
5484
5485 // Recreate the wider vector with the lower and upper part.
Craig Topper4c7972d2012-04-22 18:15:59 +00005486 return Concat128BitVectors(Lower, Upper, VT, NumElems, DAG, dl);
Bruno Cardoso Lopes6683efb2011-07-22 00:15:07 +00005487 }
5488
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00005489 // Let legalizer expand 2-wide build_vectors.
Evan Cheng7e2ff772008-05-08 00:57:18 +00005490 if (EVTBits == 64) {
5491 if (NumNonZero == 1) {
5492 // One half is zero or undef.
5493 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dale Johannesenace16102009-02-03 19:33:06 +00005494 SDValue V2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT,
Evan Cheng7e2ff772008-05-08 00:57:18 +00005495 Op.getOperand(Idx));
Craig Topper12216172012-01-13 08:12:35 +00005496 return getShuffleVectorZeroOrUndef(V2, Idx, true, Subtarget, DAG);
Evan Cheng7e2ff772008-05-08 00:57:18 +00005497 }
Dan Gohman475871a2008-07-27 21:46:04 +00005498 return SDValue();
Evan Cheng7e2ff772008-05-08 00:57:18 +00005499 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00005500
5501 // If element VT is < 32 bits, convert it to inserts into a zero vector.
Bill Wendling826f36f2007-03-28 00:57:11 +00005502 if (EVTBits == 8 && NumElems == 16) {
Dan Gohman475871a2008-07-27 21:46:04 +00005503 SDValue V = LowerBuildVectorv16i8(Op, NonZeros,NumNonZero,NumZero, DAG,
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00005504 Subtarget, *this);
Gabor Greifba36cb52008-08-28 21:40:38 +00005505 if (V.getNode()) return V;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005506 }
5507
Bill Wendling826f36f2007-03-28 00:57:11 +00005508 if (EVTBits == 16 && NumElems == 8) {
Dan Gohman475871a2008-07-27 21:46:04 +00005509 SDValue V = LowerBuildVectorv8i16(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
5514 // If element VT is == 32 bits, turn it into a number of shuffles.
Benjamin Kramer9c683542012-01-30 15:16:21 +00005515 SmallVector<SDValue, 8> V(NumElems);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005516 if (NumElems == 4 && NumZero > 0) {
5517 for (unsigned i = 0; i < 4; ++i) {
5518 bool isZero = !(NonZeros & (1 << i));
5519 if (isZero)
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00005520 V[i] = getZeroVector(VT, Subtarget, DAG, dl);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005521 else
Dale Johannesenace16102009-02-03 19:33:06 +00005522 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(i));
Evan Cheng0db9fe62006-04-25 20:13:52 +00005523 }
5524
5525 for (unsigned i = 0; i < 2; ++i) {
5526 switch ((NonZeros & (0x3 << i*2)) >> (i*2)) {
5527 default: break;
5528 case 0:
5529 V[i] = V[i*2]; // Must be a zero vector.
5530 break;
5531 case 1:
Nate Begeman9008ca62009-04-27 18:41:29 +00005532 V[i] = getMOVL(DAG, dl, VT, V[i*2+1], V[i*2]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005533 break;
5534 case 2:
Nate Begeman9008ca62009-04-27 18:41:29 +00005535 V[i] = getMOVL(DAG, dl, VT, V[i*2], V[i*2+1]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005536 break;
5537 case 3:
Nate Begeman9008ca62009-04-27 18:41:29 +00005538 V[i] = getUnpackl(DAG, dl, VT, V[i*2], V[i*2+1]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005539 break;
5540 }
5541 }
5542
Benjamin Kramer9c683542012-01-30 15:16:21 +00005543 bool Reverse1 = (NonZeros & 0x3) == 2;
5544 bool Reverse2 = ((NonZeros & (0x3 << 2)) >> 2) == 2;
5545 int MaskVec[] = {
5546 Reverse1 ? 1 : 0,
5547 Reverse1 ? 0 : 1,
Benjamin Kramer630ecf02012-01-30 20:01:35 +00005548 static_cast<int>(Reverse2 ? NumElems+1 : NumElems),
5549 static_cast<int>(Reverse2 ? NumElems : NumElems+1)
Benjamin Kramer9c683542012-01-30 15:16:21 +00005550 };
Nate Begeman9008ca62009-04-27 18:41:29 +00005551 return DAG.getVectorShuffle(VT, dl, V[0], V[1], &MaskVec[0]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005552 }
5553
Craig Topper7a9a28b2012-08-12 02:23:29 +00005554 if (Values.size() > 1 && VT.is128BitVector()) {
Nate Begemanfdea31a2010-03-24 20:49:50 +00005555 // Check for a build vector of consecutive loads.
5556 for (unsigned i = 0; i < NumElems; ++i)
5557 V[i] = Op.getOperand(i);
Michael J. Spencerec38de22010-10-10 22:04:20 +00005558
Nate Begemanfdea31a2010-03-24 20:49:50 +00005559 // Check for elements which are consecutive loads.
5560 SDValue LD = EltsFromConsecutiveLoads(VT, V, dl, DAG);
5561 if (LD.getNode())
5562 return LD;
Michael J. Spencerec38de22010-10-10 22:04:20 +00005563
Michael Liaofacace82012-10-19 17:15:18 +00005564 // Check for a build vector from mostly shuffle plus few inserting.
5565 SDValue Sh = buildFromShuffleMostly(Op, DAG);
5566 if (Sh.getNode())
5567 return Sh;
5568
Michael J. Spencerec38de22010-10-10 22:04:20 +00005569 // For SSE 4.1, use insertps to put the high elements into the low element.
Craig Topperd0a31172012-01-10 06:37:29 +00005570 if (getSubtarget()->hasSSE41()) {
Chris Lattner24faf612010-08-28 17:59:08 +00005571 SDValue Result;
5572 if (Op.getOperand(0).getOpcode() != ISD::UNDEF)
5573 Result = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(0));
5574 else
5575 Result = DAG.getUNDEF(VT);
Michael J. Spencerec38de22010-10-10 22:04:20 +00005576
Chris Lattner24faf612010-08-28 17:59:08 +00005577 for (unsigned i = 1; i < NumElems; ++i) {
5578 if (Op.getOperand(i).getOpcode() == ISD::UNDEF) continue;
5579 Result = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, Result,
Nate Begeman9008ca62009-04-27 18:41:29 +00005580 Op.getOperand(i), DAG.getIntPtrConstant(i));
Chris Lattner24faf612010-08-28 17:59:08 +00005581 }
5582 return Result;
Nate Begeman9008ca62009-04-27 18:41:29 +00005583 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00005584
Chris Lattner6e80e442010-08-28 17:15:43 +00005585 // Otherwise, expand into a number of unpckl*, start by extending each of
5586 // our (non-undef) elements to the full vector width with the element in the
5587 // bottom slot of the vector (which generates no code for SSE).
5588 for (unsigned i = 0; i < NumElems; ++i) {
5589 if (Op.getOperand(i).getOpcode() != ISD::UNDEF)
5590 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(i));
5591 else
5592 V[i] = DAG.getUNDEF(VT);
5593 }
5594
5595 // Next, we iteratively mix elements, e.g. for v4f32:
Evan Cheng0db9fe62006-04-25 20:13:52 +00005596 // Step 1: unpcklps 0, 2 ==> X: <?, ?, 2, 0>
5597 // : unpcklps 1, 3 ==> Y: <?, ?, 3, 1>
5598 // Step 2: unpcklps X, Y ==> <3, 2, 1, 0>
Chris Lattner6e80e442010-08-28 17:15:43 +00005599 unsigned EltStride = NumElems >> 1;
5600 while (EltStride != 0) {
Chris Lattner3ddcc432010-08-28 17:28:30 +00005601 for (unsigned i = 0; i < EltStride; ++i) {
5602 // If V[i+EltStride] is undef and this is the first round of mixing,
5603 // then it is safe to just drop this shuffle: V[i] is already in the
5604 // right place, the one element (since it's the first round) being
5605 // inserted as undef can be dropped. This isn't safe for successive
5606 // rounds because they will permute elements within both vectors.
5607 if (V[i+EltStride].getOpcode() == ISD::UNDEF &&
5608 EltStride == NumElems/2)
5609 continue;
Michael J. Spencerec38de22010-10-10 22:04:20 +00005610
Chris Lattner6e80e442010-08-28 17:15:43 +00005611 V[i] = getUnpackl(DAG, dl, VT, V[i], V[i + EltStride]);
Chris Lattner3ddcc432010-08-28 17:28:30 +00005612 }
Chris Lattner6e80e442010-08-28 17:15:43 +00005613 EltStride >>= 1;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005614 }
5615 return V[0];
5616 }
Dan Gohman475871a2008-07-27 21:46:04 +00005617 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005618}
5619
Bruno Cardoso Lopes8af24512011-08-01 21:54:02 +00005620// LowerAVXCONCAT_VECTORS - 256-bit AVX can use the vinsertf128 instruction
5621// to create 256-bit vectors from two other 128-bit ones.
5622static SDValue LowerAVXCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) {
5623 DebugLoc dl = Op.getDebugLoc();
5624 EVT ResVT = Op.getValueType();
5625
Craig Topper7a9a28b2012-08-12 02:23:29 +00005626 assert(ResVT.is256BitVector() && "Value type must be 256-bit wide");
Bruno Cardoso Lopes8af24512011-08-01 21:54:02 +00005627
5628 SDValue V1 = Op.getOperand(0);
5629 SDValue V2 = Op.getOperand(1);
5630 unsigned NumElems = ResVT.getVectorNumElements();
5631
Craig Topper4c7972d2012-04-22 18:15:59 +00005632 return Concat128BitVectors(V1, V2, ResVT, NumElems, DAG, dl);
Bruno Cardoso Lopes8af24512011-08-01 21:54:02 +00005633}
5634
Craig Topper55b24052012-09-11 06:15:32 +00005635static SDValue LowerCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) {
Bruno Cardoso Lopes8af24512011-08-01 21:54:02 +00005636 assert(Op.getNumOperands() == 2);
Bruno Cardoso Lopes8af24512011-08-01 21:54:02 +00005637
5638 // 256-bit AVX can use the vinsertf128 instruction to create 256-bit vectors
5639 // from two other 128-bit ones.
5640 return LowerAVXCONCAT_VECTORS(Op, DAG);
5641}
5642
Nadav Roteme80aa7c2012-04-09 08:33:21 +00005643// Try to lower a shuffle node into a simple blend instruction.
Craig Topper55b24052012-09-11 06:15:32 +00005644static SDValue
5645LowerVECTOR_SHUFFLEtoBlend(ShuffleVectorSDNode *SVOp,
5646 const X86Subtarget *Subtarget, SelectionDAG &DAG) {
Nadav Roteme80aa7c2012-04-09 08:33:21 +00005647 SDValue V1 = SVOp->getOperand(0);
5648 SDValue V2 = SVOp->getOperand(1);
5649 DebugLoc dl = SVOp->getDebugLoc();
Elena Demikhovsky226e0e62012-12-05 09:24:57 +00005650 EVT VT = SVOp->getValueType(0);
5651 EVT EltVT = VT.getVectorElementType();
Craig Topper1842ba02012-04-23 06:38:28 +00005652 unsigned NumElems = VT.getVectorNumElements();
Nadav Roteme80aa7c2012-04-09 08:33:21 +00005653
Elena Demikhovsky226e0e62012-12-05 09:24:57 +00005654 if (!Subtarget->hasSSE41() || EltVT == MVT::i8)
5655 return SDValue();
5656 if (!Subtarget->hasInt256() && VT == MVT::v16i16)
Nadav Roteme80aa7c2012-04-09 08:33:21 +00005657 return SDValue();
5658
Elena Demikhovsky226e0e62012-12-05 09:24:57 +00005659 // Check the mask for BLEND and build the value.
5660 unsigned MaskValue = 0;
5661 // There are 2 lanes if (NumElems > 8), and 1 lane otherwise.
5662 unsigned NumLanes = (NumElems-1)/8 + 1;
5663 unsigned NumElemsInLane = NumElems / NumLanes;
Nadav Roteme6113782012-04-11 06:40:27 +00005664
Elena Demikhovsky226e0e62012-12-05 09:24:57 +00005665 // Blend for v16i16 should be symetric for the both lanes.
5666 for (unsigned i = 0; i < NumElemsInLane; ++i) {
Nadav Roteme6113782012-04-11 06:40:27 +00005667
Elena Demikhovsky226e0e62012-12-05 09:24:57 +00005668 int SndLaneEltIdx = (NumLanes == 2) ?
5669 SVOp->getMaskElt(i + NumElemsInLane) : -1;
Nadav Roteme80aa7c2012-04-09 08:33:21 +00005670 int EltIdx = SVOp->getMaskElt(i);
Elena Demikhovsky226e0e62012-12-05 09:24:57 +00005671
5672 if ((EltIdx == -1 || EltIdx == (int)i) &&
5673 (SndLaneEltIdx == -1 || SndLaneEltIdx == (int)(i + NumElemsInLane)))
5674 continue;
5675
5676 if (((unsigned)EltIdx == (i + NumElems)) &&
5677 (SndLaneEltIdx == -1 ||
5678 (unsigned)SndLaneEltIdx == i + NumElems + NumElemsInLane))
5679 MaskValue |= (1<<i);
5680 else
Craig Topper1842ba02012-04-23 06:38:28 +00005681 return SDValue();
Nadav Roteme80aa7c2012-04-09 08:33:21 +00005682 }
5683
Elena Demikhovsky226e0e62012-12-05 09:24:57 +00005684 // Convert i32 vectors to floating point if it is not AVX2.
5685 // AVX2 introduced VPBLENDD instruction for 128 and 256-bit vectors.
5686 EVT BlendVT = VT;
5687 if (EltVT == MVT::i64 || (EltVT == MVT::i32 && !Subtarget->hasInt256())) {
5688 BlendVT = EVT::getVectorVT(*DAG.getContext(),
5689 EVT::getFloatingPointVT(EltVT.getSizeInBits()),
5690 NumElems);
5691 V1 = DAG.getNode(ISD::BITCAST, dl, VT, V1);
5692 V2 = DAG.getNode(ISD::BITCAST, dl, VT, V2);
5693 }
5694
5695 SDValue Ret = DAG.getNode(X86ISD::BLENDI, dl, BlendVT, V1, V2,
5696 DAG.getConstant(MaskValue, MVT::i32));
Nadav Roteme6113782012-04-11 06:40:27 +00005697 return DAG.getNode(ISD::BITCAST, dl, VT, Ret);
Nadav Roteme80aa7c2012-04-09 08:33:21 +00005698}
5699
Nate Begemanb9a47b82009-02-23 08:49:38 +00005700// v8i16 shuffles - Prefer shuffles in the following order:
5701// 1. [all] pshuflw, pshufhw, optional move
5702// 2. [ssse3] 1 x pshufb
5703// 3. [ssse3] 2 x pshufb + 1 x por
5704// 4. [all] mov + pshuflw + pshufhw + N x (pextrw + pinsrw)
Craig Topper55b24052012-09-11 06:15:32 +00005705static SDValue
5706LowerVECTOR_SHUFFLEv8i16(SDValue Op, const X86Subtarget *Subtarget,
5707 SelectionDAG &DAG) {
Bruno Cardoso Lopesbf8154a2010-08-21 01:32:18 +00005708 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Nate Begeman9008ca62009-04-27 18:41:29 +00005709 SDValue V1 = SVOp->getOperand(0);
5710 SDValue V2 = SVOp->getOperand(1);
5711 DebugLoc dl = SVOp->getDebugLoc();
Nate Begemanb9a47b82009-02-23 08:49:38 +00005712 SmallVector<int, 8> MaskVals;
Evan Cheng14b32e12007-12-11 01:46:18 +00005713
Nate Begemanb9a47b82009-02-23 08:49:38 +00005714 // Determine if more than 1 of the words in each of the low and high quadwords
5715 // of the result come from the same quadword of one of the two inputs. Undef
5716 // mask values count as coming from any quadword, for better codegen.
Benjamin Kramer003fad92011-10-15 13:28:31 +00005717 unsigned LoQuad[] = { 0, 0, 0, 0 };
5718 unsigned HiQuad[] = { 0, 0, 0, 0 };
Benjamin Kramer699ddcb2012-02-06 12:06:18 +00005719 std::bitset<4> InputQuads;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005720 for (unsigned i = 0; i < 8; ++i) {
Benjamin Kramer003fad92011-10-15 13:28:31 +00005721 unsigned *Quad = i < 4 ? LoQuad : HiQuad;
Nate Begeman9008ca62009-04-27 18:41:29 +00005722 int EltIdx = SVOp->getMaskElt(i);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005723 MaskVals.push_back(EltIdx);
5724 if (EltIdx < 0) {
5725 ++Quad[0];
5726 ++Quad[1];
5727 ++Quad[2];
5728 ++Quad[3];
Evan Cheng14b32e12007-12-11 01:46:18 +00005729 continue;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005730 }
5731 ++Quad[EltIdx / 4];
5732 InputQuads.set(EltIdx / 4);
Evan Cheng14b32e12007-12-11 01:46:18 +00005733 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00005734
Nate Begemanb9a47b82009-02-23 08:49:38 +00005735 int BestLoQuad = -1;
Evan Cheng14b32e12007-12-11 01:46:18 +00005736 unsigned MaxQuad = 1;
5737 for (unsigned i = 0; i < 4; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005738 if (LoQuad[i] > MaxQuad) {
5739 BestLoQuad = i;
5740 MaxQuad = LoQuad[i];
Evan Cheng14b32e12007-12-11 01:46:18 +00005741 }
Evan Cheng8a86c3f2007-12-07 08:07:39 +00005742 }
5743
Nate Begemanb9a47b82009-02-23 08:49:38 +00005744 int BestHiQuad = -1;
Evan Cheng14b32e12007-12-11 01:46:18 +00005745 MaxQuad = 1;
5746 for (unsigned i = 0; i < 4; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005747 if (HiQuad[i] > MaxQuad) {
5748 BestHiQuad = i;
5749 MaxQuad = HiQuad[i];
Evan Cheng14b32e12007-12-11 01:46:18 +00005750 }
5751 }
5752
Nate Begemanb9a47b82009-02-23 08:49:38 +00005753 // For SSSE3, If all 8 words of the result come from only 1 quadword of each
Eric Christopherfd179292009-08-27 18:07:15 +00005754 // of the two input vectors, shuffle them into one input vector so only a
Nate Begemanb9a47b82009-02-23 08:49:38 +00005755 // single pshufb instruction is necessary. If There are more than 2 input
5756 // quads, disable the next transformation since it does not help SSSE3.
5757 bool V1Used = InputQuads[0] || InputQuads[1];
5758 bool V2Used = InputQuads[2] || InputQuads[3];
Craig Topperd0a31172012-01-10 06:37:29 +00005759 if (Subtarget->hasSSSE3()) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005760 if (InputQuads.count() == 2 && V1Used && V2Used) {
Benjamin Kramer699ddcb2012-02-06 12:06:18 +00005761 BestLoQuad = InputQuads[0] ? 0 : 1;
5762 BestHiQuad = InputQuads[2] ? 2 : 3;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005763 }
5764 if (InputQuads.count() > 2) {
5765 BestLoQuad = -1;
5766 BestHiQuad = -1;
5767 }
5768 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00005769
Nate Begemanb9a47b82009-02-23 08:49:38 +00005770 // If BestLoQuad or BestHiQuad are set, shuffle the quads together and update
5771 // the shuffle mask. If a quad is scored as -1, that means that it contains
5772 // words from all 4 input quadwords.
5773 SDValue NewV;
5774 if (BestLoQuad >= 0 || BestHiQuad >= 0) {
Benjamin Kramer9c683542012-01-30 15:16:21 +00005775 int MaskV[] = {
5776 BestLoQuad < 0 ? 0 : BestLoQuad,
5777 BestHiQuad < 0 ? 1 : BestHiQuad
5778 };
Eric Christopherfd179292009-08-27 18:07:15 +00005779 NewV = DAG.getVectorShuffle(MVT::v2i64, dl,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005780 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64, V1),
5781 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64, V2), &MaskV[0]);
5782 NewV = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, NewV);
Evan Cheng14b32e12007-12-11 01:46:18 +00005783
Nate Begemanb9a47b82009-02-23 08:49:38 +00005784 // Rewrite the MaskVals and assign NewV to V1 if NewV now contains all the
5785 // source words for the shuffle, to aid later transformations.
5786 bool AllWordsInNewV = true;
Mon P Wang37b9a192009-03-11 06:35:11 +00005787 bool InOrder[2] = { true, true };
Evan Cheng14b32e12007-12-11 01:46:18 +00005788 for (unsigned i = 0; i != 8; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005789 int idx = MaskVals[i];
Mon P Wang37b9a192009-03-11 06:35:11 +00005790 if (idx != (int)i)
5791 InOrder[i/4] = false;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005792 if (idx < 0 || (idx/4) == BestLoQuad || (idx/4) == BestHiQuad)
Evan Cheng14b32e12007-12-11 01:46:18 +00005793 continue;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005794 AllWordsInNewV = false;
5795 break;
Evan Cheng14b32e12007-12-11 01:46:18 +00005796 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00005797
Nate Begemanb9a47b82009-02-23 08:49:38 +00005798 bool pshuflw = AllWordsInNewV, pshufhw = AllWordsInNewV;
5799 if (AllWordsInNewV) {
5800 for (int i = 0; i != 8; ++i) {
5801 int idx = MaskVals[i];
5802 if (idx < 0)
Evan Cheng14b32e12007-12-11 01:46:18 +00005803 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00005804 idx = MaskVals[i] = (idx / 4) == BestLoQuad ? (idx & 3) : (idx & 3) + 4;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005805 if ((idx != i) && idx < 4)
5806 pshufhw = false;
5807 if ((idx != i) && idx > 3)
5808 pshuflw = false;
Evan Cheng14b32e12007-12-11 01:46:18 +00005809 }
Nate Begemanb9a47b82009-02-23 08:49:38 +00005810 V1 = NewV;
5811 V2Used = false;
5812 BestLoQuad = 0;
5813 BestHiQuad = 1;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00005814 }
Evan Cheng14b32e12007-12-11 01:46:18 +00005815
Nate Begemanb9a47b82009-02-23 08:49:38 +00005816 // If we've eliminated the use of V2, and the new mask is a pshuflw or
5817 // pshufhw, that's as cheap as it gets. Return the new shuffle.
Mon P Wang37b9a192009-03-11 06:35:11 +00005818 if ((pshufhw && InOrder[0]) || (pshuflw && InOrder[1])) {
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00005819 unsigned Opc = pshufhw ? X86ISD::PSHUFHW : X86ISD::PSHUFLW;
5820 unsigned TargetMask = 0;
5821 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV,
Owen Anderson825b72b2009-08-11 20:47:22 +00005822 DAG.getUNDEF(MVT::v8i16), &MaskVals[0]);
Craig Topperdd637ae2012-02-19 05:41:45 +00005823 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(NewV.getNode());
5824 TargetMask = pshufhw ? getShufflePSHUFHWImmediate(SVOp):
5825 getShufflePSHUFLWImmediate(SVOp);
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00005826 V1 = NewV.getOperand(0);
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00005827 return getTargetShuffleNode(Opc, dl, MVT::v8i16, V1, TargetMask, DAG);
Evan Cheng14b32e12007-12-11 01:46:18 +00005828 }
Evan Cheng14b32e12007-12-11 01:46:18 +00005829 }
Eric Christopherfd179292009-08-27 18:07:15 +00005830
Nate Begemanb9a47b82009-02-23 08:49:38 +00005831 // If we have SSSE3, and all words of the result are from 1 input vector,
5832 // case 2 is generated, otherwise case 3 is generated. If no SSSE3
5833 // is present, fall back to case 4.
Craig Topperd0a31172012-01-10 06:37:29 +00005834 if (Subtarget->hasSSSE3()) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005835 SmallVector<SDValue,16> pshufbMask;
Eric Christopherfd179292009-08-27 18:07:15 +00005836
Nate Begemanb9a47b82009-02-23 08:49:38 +00005837 // If we have elements from both input vectors, set the high bit of the
Eric Christopherfd179292009-08-27 18:07:15 +00005838 // shuffle mask element to zero out elements that come from V2 in the V1
Nate Begemanb9a47b82009-02-23 08:49:38 +00005839 // mask, and elements that come from V1 in the V2 mask, so that the two
5840 // results can be OR'd together.
5841 bool TwoInputs = V1Used && V2Used;
5842 for (unsigned i = 0; i != 8; ++i) {
5843 int EltIdx = MaskVals[i] * 2;
Craig Topperbe97ae92012-05-18 07:07:36 +00005844 int Idx0 = (TwoInputs && (EltIdx >= 16)) ? 0x80 : EltIdx;
5845 int Idx1 = (TwoInputs && (EltIdx >= 16)) ? 0x80 : EltIdx+1;
5846 pshufbMask.push_back(DAG.getConstant(Idx0, MVT::i8));
5847 pshufbMask.push_back(DAG.getConstant(Idx1, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005848 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005849 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, V1);
Eric Christopherfd179292009-08-27 18:07:15 +00005850 V1 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V1,
Evan Chenga87008d2009-02-25 22:49:59 +00005851 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005852 MVT::v16i8, &pshufbMask[0], 16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005853 if (!TwoInputs)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005854 return DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V1);
Eric Christopherfd179292009-08-27 18:07:15 +00005855
Nate Begemanb9a47b82009-02-23 08:49:38 +00005856 // Calculate the shuffle mask for the second input, shuffle it, and
5857 // OR it with the first shuffled input.
5858 pshufbMask.clear();
5859 for (unsigned i = 0; i != 8; ++i) {
5860 int EltIdx = MaskVals[i] * 2;
Craig Topperbe97ae92012-05-18 07:07:36 +00005861 int Idx0 = (EltIdx < 16) ? 0x80 : EltIdx - 16;
5862 int Idx1 = (EltIdx < 16) ? 0x80 : EltIdx - 15;
5863 pshufbMask.push_back(DAG.getConstant(Idx0, MVT::i8));
5864 pshufbMask.push_back(DAG.getConstant(Idx1, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005865 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005866 V2 = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, V2);
Eric Christopherfd179292009-08-27 18:07:15 +00005867 V2 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V2,
Evan Chenga87008d2009-02-25 22:49:59 +00005868 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005869 MVT::v16i8, &pshufbMask[0], 16));
5870 V1 = DAG.getNode(ISD::OR, dl, MVT::v16i8, V1, V2);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005871 return DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005872 }
5873
5874 // If BestLoQuad >= 0, generate a pshuflw to put the low elements in order,
5875 // and update MaskVals with new element order.
Benjamin Kramer9c683542012-01-30 15:16:21 +00005876 std::bitset<8> InOrder;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005877 if (BestLoQuad >= 0) {
Benjamin Kramer9c683542012-01-30 15:16:21 +00005878 int MaskV[] = { -1, -1, -1, -1, 4, 5, 6, 7 };
Nate Begemanb9a47b82009-02-23 08:49:38 +00005879 for (int i = 0; i != 4; ++i) {
5880 int idx = MaskVals[i];
5881 if (idx < 0) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005882 InOrder.set(i);
5883 } else if ((idx / 4) == BestLoQuad) {
Benjamin Kramer9c683542012-01-30 15:16:21 +00005884 MaskV[i] = idx & 3;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005885 InOrder.set(i);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005886 }
5887 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005888 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV, DAG.getUNDEF(MVT::v8i16),
Nate Begeman9008ca62009-04-27 18:41:29 +00005889 &MaskV[0]);
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00005890
Craig Topperdd637ae2012-02-19 05:41:45 +00005891 if (NewV.getOpcode() == ISD::VECTOR_SHUFFLE && Subtarget->hasSSSE3()) {
5892 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(NewV.getNode());
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00005893 NewV = getTargetShuffleNode(X86ISD::PSHUFLW, dl, MVT::v8i16,
Craig Topperdd637ae2012-02-19 05:41:45 +00005894 NewV.getOperand(0),
5895 getShufflePSHUFLWImmediate(SVOp), DAG);
5896 }
Nate Begemanb9a47b82009-02-23 08:49:38 +00005897 }
Eric Christopherfd179292009-08-27 18:07:15 +00005898
Nate Begemanb9a47b82009-02-23 08:49:38 +00005899 // If BestHi >= 0, generate a pshufhw to put the high elements in order,
5900 // and update MaskVals with the new element order.
5901 if (BestHiQuad >= 0) {
Benjamin Kramer9c683542012-01-30 15:16:21 +00005902 int MaskV[] = { 0, 1, 2, 3, -1, -1, -1, -1 };
Nate Begemanb9a47b82009-02-23 08:49:38 +00005903 for (unsigned i = 4; i != 8; ++i) {
5904 int idx = MaskVals[i];
5905 if (idx < 0) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005906 InOrder.set(i);
5907 } else if ((idx / 4) == BestHiQuad) {
Benjamin Kramer9c683542012-01-30 15:16:21 +00005908 MaskV[i] = (idx & 3) + 4;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005909 InOrder.set(i);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005910 }
5911 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005912 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV, DAG.getUNDEF(MVT::v8i16),
Nate Begeman9008ca62009-04-27 18:41:29 +00005913 &MaskV[0]);
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00005914
Craig Topperdd637ae2012-02-19 05:41:45 +00005915 if (NewV.getOpcode() == ISD::VECTOR_SHUFFLE && Subtarget->hasSSSE3()) {
5916 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(NewV.getNode());
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00005917 NewV = getTargetShuffleNode(X86ISD::PSHUFHW, dl, MVT::v8i16,
Craig Topperdd637ae2012-02-19 05:41:45 +00005918 NewV.getOperand(0),
5919 getShufflePSHUFHWImmediate(SVOp), DAG);
5920 }
Nate Begemanb9a47b82009-02-23 08:49:38 +00005921 }
Eric Christopherfd179292009-08-27 18:07:15 +00005922
Nate Begemanb9a47b82009-02-23 08:49:38 +00005923 // In case BestHi & BestLo were both -1, which means each quadword has a word
5924 // from each of the four input quadwords, calculate the InOrder bitvector now
5925 // before falling through to the insert/extract cleanup.
5926 if (BestLoQuad == -1 && BestHiQuad == -1) {
5927 NewV = V1;
5928 for (int i = 0; i != 8; ++i)
5929 if (MaskVals[i] < 0 || MaskVals[i] == i)
5930 InOrder.set(i);
5931 }
Eric Christopherfd179292009-08-27 18:07:15 +00005932
Nate Begemanb9a47b82009-02-23 08:49:38 +00005933 // The other elements are put in the right place using pextrw and pinsrw.
5934 for (unsigned i = 0; i != 8; ++i) {
5935 if (InOrder[i])
5936 continue;
5937 int EltIdx = MaskVals[i];
5938 if (EltIdx < 0)
5939 continue;
Craig Topper6643d9c2012-05-04 06:18:33 +00005940 SDValue ExtOp = (EltIdx < 8) ?
5941 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, V1,
5942 DAG.getIntPtrConstant(EltIdx)) :
5943 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, V2,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005944 DAG.getIntPtrConstant(EltIdx - 8));
Owen Anderson825b72b2009-08-11 20:47:22 +00005945 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, ExtOp,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005946 DAG.getIntPtrConstant(i));
5947 }
5948 return NewV;
5949}
5950
5951// v16i8 shuffles - Prefer shuffles in the following order:
5952// 1. [ssse3] 1 x pshufb
5953// 2. [ssse3] 2 x pshufb + 1 x por
5954// 3. [all] v8i16 shuffle + N x pextrw + rotate + pinsrw
5955static
Nate Begeman9008ca62009-04-27 18:41:29 +00005956SDValue LowerVECTOR_SHUFFLEv16i8(ShuffleVectorSDNode *SVOp,
Dan Gohmand858e902010-04-17 15:26:15 +00005957 SelectionDAG &DAG,
5958 const X86TargetLowering &TLI) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005959 SDValue V1 = SVOp->getOperand(0);
5960 SDValue V2 = SVOp->getOperand(1);
5961 DebugLoc dl = SVOp->getDebugLoc();
Benjamin Kramered4c8c62012-01-15 13:16:05 +00005962 ArrayRef<int> MaskVals = SVOp->getMask();
Eric Christopherfd179292009-08-27 18:07:15 +00005963
Nate Begemanb9a47b82009-02-23 08:49:38 +00005964 // If we have SSSE3, case 1 is generated when all result bytes come from
Eric Christopherfd179292009-08-27 18:07:15 +00005965 // one of the inputs. Otherwise, case 2 is generated. If no SSSE3 is
Nate Begemanb9a47b82009-02-23 08:49:38 +00005966 // present, fall back to case 3.
Eric Christopherfd179292009-08-27 18:07:15 +00005967
Nate Begemanb9a47b82009-02-23 08:49:38 +00005968 // If SSSE3, use 1 pshufb instruction per vector with elements in the result.
Craig Topperd0a31172012-01-10 06:37:29 +00005969 if (TLI.getSubtarget()->hasSSSE3()) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005970 SmallVector<SDValue,16> pshufbMask;
Eric Christopherfd179292009-08-27 18:07:15 +00005971
Nate Begemanb9a47b82009-02-23 08:49:38 +00005972 // If all result elements are from one input vector, then only translate
Eric Christopherfd179292009-08-27 18:07:15 +00005973 // undef mask values to 0x80 (zero out result) in the pshufb mask.
Nate Begemanb9a47b82009-02-23 08:49:38 +00005974 //
5975 // Otherwise, we have elements from both input vectors, and must zero out
5976 // elements that come from V2 in the first mask, and V1 in the second mask
5977 // so that we can OR them together.
Nate Begemanb9a47b82009-02-23 08:49:38 +00005978 for (unsigned i = 0; i != 16; ++i) {
5979 int EltIdx = MaskVals[i];
Craig Topperb82b5ab2012-05-18 06:42:06 +00005980 if (EltIdx < 0 || EltIdx >= 16)
5981 EltIdx = 0x80;
Owen Anderson825b72b2009-08-11 20:47:22 +00005982 pshufbMask.push_back(DAG.getConstant(EltIdx, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005983 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005984 V1 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V1,
Evan Chenga87008d2009-02-25 22:49:59 +00005985 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005986 MVT::v16i8, &pshufbMask[0], 16));
Michael Liao265bcb12012-08-31 20:12:31 +00005987
5988 // As PSHUFB will zero elements with negative indices, it's safe to ignore
5989 // the 2nd operand if it's undefined or zero.
5990 if (V2.getOpcode() == ISD::UNDEF ||
5991 ISD::isBuildVectorAllZeros(V2.getNode()))
Nate Begemanb9a47b82009-02-23 08:49:38 +00005992 return V1;
Eric Christopherfd179292009-08-27 18:07:15 +00005993
Nate Begemanb9a47b82009-02-23 08:49:38 +00005994 // Calculate the shuffle mask for the second input, shuffle it, and
5995 // OR it with the first shuffled input.
5996 pshufbMask.clear();
5997 for (unsigned i = 0; i != 16; ++i) {
5998 int EltIdx = MaskVals[i];
Craig Topperb82b5ab2012-05-18 06:42:06 +00005999 EltIdx = (EltIdx < 16) ? 0x80 : EltIdx - 16;
Craig Topper85b9e562012-05-22 06:09:38 +00006000 pshufbMask.push_back(DAG.getConstant(EltIdx, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00006001 }
Owen Anderson825b72b2009-08-11 20:47:22 +00006002 V2 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V2,
Evan Chenga87008d2009-02-25 22:49:59 +00006003 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00006004 MVT::v16i8, &pshufbMask[0], 16));
6005 return DAG.getNode(ISD::OR, dl, MVT::v16i8, V1, V2);
Nate Begemanb9a47b82009-02-23 08:49:38 +00006006 }
Eric Christopherfd179292009-08-27 18:07:15 +00006007
Nate Begemanb9a47b82009-02-23 08:49:38 +00006008 // No SSSE3 - Calculate in place words and then fix all out of place words
6009 // With 0-16 extracts & inserts. Worst case is 16 bytes out of order from
6010 // the 16 different words that comprise the two doublequadword input vectors.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006011 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V1);
6012 V2 = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V2);
Craig Topperb82b5ab2012-05-18 06:42:06 +00006013 SDValue NewV = V1;
Nate Begemanb9a47b82009-02-23 08:49:38 +00006014 for (int i = 0; i != 8; ++i) {
6015 int Elt0 = MaskVals[i*2];
6016 int Elt1 = MaskVals[i*2+1];
Eric Christopherfd179292009-08-27 18:07:15 +00006017
Nate Begemanb9a47b82009-02-23 08:49:38 +00006018 // This word of the result is all undef, skip it.
6019 if (Elt0 < 0 && Elt1 < 0)
6020 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00006021
Nate Begemanb9a47b82009-02-23 08:49:38 +00006022 // This word of the result is already in the correct place, skip it.
Craig Topperb82b5ab2012-05-18 06:42:06 +00006023 if ((Elt0 == i*2) && (Elt1 == i*2+1))
Nate Begemanb9a47b82009-02-23 08:49:38 +00006024 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00006025
Nate Begemanb9a47b82009-02-23 08:49:38 +00006026 SDValue Elt0Src = Elt0 < 16 ? V1 : V2;
6027 SDValue Elt1Src = Elt1 < 16 ? V1 : V2;
6028 SDValue InsElt;
Mon P Wang6b3ef692009-03-11 18:47:57 +00006029
6030 // If Elt0 and Elt1 are defined, are consecutive, and can be load
6031 // using a single extract together, load it and store it.
6032 if ((Elt0 >= 0) && ((Elt0 + 1) == Elt1) && ((Elt0 & 1) == 0)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00006033 InsElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, Elt1Src,
Mon P Wang6b3ef692009-03-11 18:47:57 +00006034 DAG.getIntPtrConstant(Elt1 / 2));
Owen Anderson825b72b2009-08-11 20:47:22 +00006035 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, InsElt,
Mon P Wang6b3ef692009-03-11 18:47:57 +00006036 DAG.getIntPtrConstant(i));
6037 continue;
6038 }
6039
Nate Begemanb9a47b82009-02-23 08:49:38 +00006040 // If Elt1 is defined, extract it from the appropriate source. If the
Mon P Wang6b3ef692009-03-11 18:47:57 +00006041 // source byte is not also odd, shift the extracted word left 8 bits
6042 // otherwise clear the bottom 8 bits if we need to do an or.
Nate Begemanb9a47b82009-02-23 08:49:38 +00006043 if (Elt1 >= 0) {
Owen Anderson825b72b2009-08-11 20:47:22 +00006044 InsElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, Elt1Src,
Nate Begemanb9a47b82009-02-23 08:49:38 +00006045 DAG.getIntPtrConstant(Elt1 / 2));
6046 if ((Elt1 & 1) == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00006047 InsElt = DAG.getNode(ISD::SHL, dl, MVT::i16, InsElt,
Owen Anderson95771af2011-02-25 21:41:48 +00006048 DAG.getConstant(8,
6049 TLI.getShiftAmountTy(InsElt.getValueType())));
Mon P Wang6b3ef692009-03-11 18:47:57 +00006050 else if (Elt0 >= 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00006051 InsElt = DAG.getNode(ISD::AND, dl, MVT::i16, InsElt,
6052 DAG.getConstant(0xFF00, MVT::i16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00006053 }
6054 // If Elt0 is defined, extract it from the appropriate source. If the
6055 // source byte is not also even, shift the extracted word right 8 bits. If
6056 // Elt1 was also defined, OR the extracted values together before
6057 // inserting them in the result.
6058 if (Elt0 >= 0) {
Owen Anderson825b72b2009-08-11 20:47:22 +00006059 SDValue InsElt0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16,
Nate Begemanb9a47b82009-02-23 08:49:38 +00006060 Elt0Src, DAG.getIntPtrConstant(Elt0 / 2));
6061 if ((Elt0 & 1) != 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00006062 InsElt0 = DAG.getNode(ISD::SRL, dl, MVT::i16, InsElt0,
Owen Anderson95771af2011-02-25 21:41:48 +00006063 DAG.getConstant(8,
6064 TLI.getShiftAmountTy(InsElt0.getValueType())));
Mon P Wang6b3ef692009-03-11 18:47:57 +00006065 else if (Elt1 >= 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00006066 InsElt0 = DAG.getNode(ISD::AND, dl, MVT::i16, InsElt0,
6067 DAG.getConstant(0x00FF, MVT::i16));
6068 InsElt = Elt1 >= 0 ? DAG.getNode(ISD::OR, dl, MVT::i16, InsElt, InsElt0)
Nate Begemanb9a47b82009-02-23 08:49:38 +00006069 : InsElt0;
6070 }
Owen Anderson825b72b2009-08-11 20:47:22 +00006071 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, InsElt,
Nate Begemanb9a47b82009-02-23 08:49:38 +00006072 DAG.getIntPtrConstant(i));
6073 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006074 return DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, NewV);
Evan Cheng14b32e12007-12-11 01:46:18 +00006075}
6076
Elena Demikhovsky41789462012-09-06 12:42:01 +00006077// v32i8 shuffles - Translate to VPSHUFB if possible.
6078static
6079SDValue LowerVECTOR_SHUFFLEv32i8(ShuffleVectorSDNode *SVOp,
Craig Topper55b24052012-09-11 06:15:32 +00006080 const X86Subtarget *Subtarget,
6081 SelectionDAG &DAG) {
Elena Demikhovsky41789462012-09-06 12:42:01 +00006082 EVT VT = SVOp->getValueType(0);
6083 SDValue V1 = SVOp->getOperand(0);
6084 SDValue V2 = SVOp->getOperand(1);
6085 DebugLoc dl = SVOp->getDebugLoc();
Elena Demikhovsky8100d242012-09-10 12:13:11 +00006086 SmallVector<int, 32> MaskVals(SVOp->getMask().begin(), SVOp->getMask().end());
Elena Demikhovsky41789462012-09-06 12:42:01 +00006087
6088 bool V2IsUndef = V2.getOpcode() == ISD::UNDEF;
Elena Demikhovsky8100d242012-09-10 12:13:11 +00006089 bool V1IsAllZero = ISD::isBuildVectorAllZeros(V1.getNode());
6090 bool V2IsAllZero = ISD::isBuildVectorAllZeros(V2.getNode());
Elena Demikhovsky41789462012-09-06 12:42:01 +00006091
Michael Liao471b9172012-10-03 23:43:52 +00006092 // VPSHUFB may be generated if
Elena Demikhovsky8100d242012-09-10 12:13:11 +00006093 // (1) one of input vector is undefined or zeroinitializer.
6094 // The mask value 0x80 puts 0 in the corresponding slot of the vector.
6095 // And (2) the mask indexes don't cross the 128-bit lane.
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00006096 if (VT != MVT::v32i8 || !Subtarget->hasInt256() ||
Elena Demikhovsky8100d242012-09-10 12:13:11 +00006097 (!V2IsUndef && !V2IsAllZero && !V1IsAllZero))
Elena Demikhovsky41789462012-09-06 12:42:01 +00006098 return SDValue();
6099
Elena Demikhovsky8100d242012-09-10 12:13:11 +00006100 if (V1IsAllZero && !V2IsAllZero) {
6101 CommuteVectorShuffleMask(MaskVals, 32);
6102 V1 = V2;
6103 }
6104 SmallVector<SDValue, 32> pshufbMask;
Elena Demikhovsky41789462012-09-06 12:42:01 +00006105 for (unsigned i = 0; i != 32; i++) {
6106 int EltIdx = MaskVals[i];
6107 if (EltIdx < 0 || EltIdx >= 32)
6108 EltIdx = 0x80;
6109 else {
6110 if ((EltIdx >= 16 && i < 16) || (EltIdx < 16 && i >= 16))
6111 // Cross lane is not allowed.
6112 return SDValue();
6113 EltIdx &= 0xf;
6114 }
6115 pshufbMask.push_back(DAG.getConstant(EltIdx, MVT::i8));
6116 }
6117 return DAG.getNode(X86ISD::PSHUFB, dl, MVT::v32i8, V1,
6118 DAG.getNode(ISD::BUILD_VECTOR, dl,
6119 MVT::v32i8, &pshufbMask[0], 32));
6120}
6121
Evan Cheng7a831ce2007-12-15 03:00:47 +00006122/// RewriteAsNarrowerShuffle - Try rewriting v8i16 and v16i8 shuffles as 4 wide
Bruno Cardoso Lopes0a7dd4f2010-09-08 18:12:31 +00006123/// ones, or rewriting v4i32 / v4f32 as 2 wide ones if possible. This can be
Evan Cheng7a831ce2007-12-15 03:00:47 +00006124/// done when every pair / quad of shuffle mask elements point to elements in
6125/// the right sequence. e.g.
Bruno Cardoso Lopes0a7dd4f2010-09-08 18:12:31 +00006126/// vector_shuffle X, Y, <2, 3, | 10, 11, | 0, 1, | 14, 15>
Evan Cheng14b32e12007-12-11 01:46:18 +00006127static
Nate Begeman9008ca62009-04-27 18:41:29 +00006128SDValue RewriteAsNarrowerShuffle(ShuffleVectorSDNode *SVOp,
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006129 SelectionDAG &DAG, DebugLoc dl) {
Craig Topper11ac1f82012-05-04 04:08:44 +00006130 MVT VT = SVOp->getValueType(0).getSimpleVT();
Nate Begeman9008ca62009-04-27 18:41:29 +00006131 unsigned NumElems = VT.getVectorNumElements();
Craig Topper11ac1f82012-05-04 04:08:44 +00006132 MVT NewVT;
6133 unsigned Scale;
6134 switch (VT.SimpleTy) {
Craig Topperabb94d02012-02-05 03:43:23 +00006135 default: llvm_unreachable("Unexpected!");
Craig Topperf3640d72012-05-04 04:44:49 +00006136 case MVT::v4f32: NewVT = MVT::v2f64; Scale = 2; break;
6137 case MVT::v4i32: NewVT = MVT::v2i64; Scale = 2; break;
6138 case MVT::v8i16: NewVT = MVT::v4i32; Scale = 2; break;
6139 case MVT::v16i8: NewVT = MVT::v4i32; Scale = 4; break;
6140 case MVT::v16i16: NewVT = MVT::v8i32; Scale = 2; break;
6141 case MVT::v32i8: NewVT = MVT::v8i32; Scale = 4; break;
Evan Cheng7a831ce2007-12-15 03:00:47 +00006142 }
6143
Nate Begeman9008ca62009-04-27 18:41:29 +00006144 SmallVector<int, 8> MaskVec;
Craig Topper11ac1f82012-05-04 04:08:44 +00006145 for (unsigned i = 0; i != NumElems; i += Scale) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006146 int StartIdx = -1;
Craig Topper11ac1f82012-05-04 04:08:44 +00006147 for (unsigned j = 0; j != Scale; ++j) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006148 int EltIdx = SVOp->getMaskElt(i+j);
6149 if (EltIdx < 0)
Evan Cheng14b32e12007-12-11 01:46:18 +00006150 continue;
Craig Topper11ac1f82012-05-04 04:08:44 +00006151 if (StartIdx < 0)
6152 StartIdx = (EltIdx / Scale);
6153 if (EltIdx != (int)(StartIdx*Scale + j))
Dan Gohman475871a2008-07-27 21:46:04 +00006154 return SDValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00006155 }
Craig Topper11ac1f82012-05-04 04:08:44 +00006156 MaskVec.push_back(StartIdx);
Evan Cheng8a86c3f2007-12-07 08:07:39 +00006157 }
6158
Craig Topper11ac1f82012-05-04 04:08:44 +00006159 SDValue V1 = DAG.getNode(ISD::BITCAST, dl, NewVT, SVOp->getOperand(0));
6160 SDValue V2 = DAG.getNode(ISD::BITCAST, dl, NewVT, SVOp->getOperand(1));
Nate Begeman9008ca62009-04-27 18:41:29 +00006161 return DAG.getVectorShuffle(NewVT, dl, V1, V2, &MaskVec[0]);
Evan Cheng8a86c3f2007-12-07 08:07:39 +00006162}
6163
Evan Chengd880b972008-05-09 21:53:03 +00006164/// getVZextMovL - Return a zero-extending vector move low node.
Evan Cheng7e2ff772008-05-08 00:57:18 +00006165///
Owen Andersone50ed302009-08-10 22:56:29 +00006166static SDValue getVZextMovL(EVT VT, EVT OpVT,
Nate Begeman9008ca62009-04-27 18:41:29 +00006167 SDValue SrcOp, SelectionDAG &DAG,
6168 const X86Subtarget *Subtarget, DebugLoc dl) {
Owen Anderson825b72b2009-08-11 20:47:22 +00006169 if (VT == MVT::v2f64 || VT == MVT::v4f32) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00006170 LoadSDNode *LD = NULL;
Gabor Greifba36cb52008-08-28 21:40:38 +00006171 if (!isScalarLoadToVector(SrcOp.getNode(), &LD))
Evan Cheng7e2ff772008-05-08 00:57:18 +00006172 LD = dyn_cast<LoadSDNode>(SrcOp);
6173 if (!LD) {
6174 // movssrr and movsdrr do not clear top bits. Try to use movd, movq
6175 // instead.
Owen Anderson766b5ef2009-08-11 21:59:30 +00006176 MVT ExtVT = (OpVT == MVT::v2f64) ? MVT::i64 : MVT::i32;
Duncan Sandscdfad362010-11-03 12:17:33 +00006177 if ((ExtVT != MVT::i64 || Subtarget->is64Bit()) &&
Evan Cheng7e2ff772008-05-08 00:57:18 +00006178 SrcOp.getOpcode() == ISD::SCALAR_TO_VECTOR &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006179 SrcOp.getOperand(0).getOpcode() == ISD::BITCAST &&
Owen Anderson766b5ef2009-08-11 21:59:30 +00006180 SrcOp.getOperand(0).getOperand(0).getValueType() == ExtVT) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00006181 // PR2108
Owen Anderson825b72b2009-08-11 20:47:22 +00006182 OpVT = (OpVT == MVT::v2f64) ? MVT::v2i64 : MVT::v4i32;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006183 return DAG.getNode(ISD::BITCAST, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00006184 DAG.getNode(X86ISD::VZEXT_MOVL, dl, OpVT,
6185 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
6186 OpVT,
Gabor Greif327ef032008-08-28 23:19:51 +00006187 SrcOp.getOperand(0)
6188 .getOperand(0))));
Evan Cheng7e2ff772008-05-08 00:57:18 +00006189 }
6190 }
6191 }
6192
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006193 return DAG.getNode(ISD::BITCAST, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00006194 DAG.getNode(X86ISD::VZEXT_MOVL, dl, OpVT,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006195 DAG.getNode(ISD::BITCAST, dl,
Dale Johannesenace16102009-02-03 19:33:06 +00006196 OpVT, SrcOp)));
Evan Cheng7e2ff772008-05-08 00:57:18 +00006197}
6198
Bruno Cardoso Lopesd0888342011-07-22 00:14:56 +00006199/// LowerVECTOR_SHUFFLE_256 - Handle all 256-bit wide vectors shuffles
6200/// which could not be matched by any known target speficic shuffle
6201static SDValue
6202LowerVECTOR_SHUFFLE_256(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG) {
Elena Demikhovsky15963732012-06-26 08:04:10 +00006203
6204 SDValue NewOp = Compact8x32ShuffleNode(SVOp, DAG);
6205 if (NewOp.getNode())
6206 return NewOp;
6207
Craig Topper8f35c132012-01-20 09:29:03 +00006208 EVT VT = SVOp->getValueType(0);
Bruno Cardoso Lopes3b865982011-08-16 18:21:54 +00006209
Craig Topper8f35c132012-01-20 09:29:03 +00006210 unsigned NumElems = VT.getVectorNumElements();
6211 unsigned NumLaneElems = NumElems / 2;
6212
Craig Topper8f35c132012-01-20 09:29:03 +00006213 DebugLoc dl = SVOp->getDebugLoc();
6214 MVT EltVT = VT.getVectorElementType().getSimpleVT();
Craig Topper9a2b6e12012-04-06 07:45:23 +00006215 EVT NVT = MVT::getVectorVT(EltVT, NumLaneElems);
Craig Topper8ae97ba2012-05-21 06:40:16 +00006216 SDValue Output[2];
Craig Topper8f35c132012-01-20 09:29:03 +00006217
Craig Topper9a2b6e12012-04-06 07:45:23 +00006218 SmallVector<int, 16> Mask;
Craig Topper8f35c132012-01-20 09:29:03 +00006219 for (unsigned l = 0; l < 2; ++l) {
Craig Topper9a2b6e12012-04-06 07:45:23 +00006220 // Build a shuffle mask for the output, discovering on the fly which
6221 // input vectors to use as shuffle operands (recorded in InputUsed).
6222 // If building a suitable shuffle vector proves too hard, then bail
Craig Topper8ae97ba2012-05-21 06:40:16 +00006223 // out with UseBuildVector set.
6224 bool UseBuildVector = false;
Benjamin Kramer9e5512a2012-04-06 13:33:52 +00006225 int InputUsed[2] = { -1, -1 }; // Not yet discovered.
Craig Topper9a2b6e12012-04-06 07:45:23 +00006226 unsigned LaneStart = l * NumLaneElems;
6227 for (unsigned i = 0; i != NumLaneElems; ++i) {
6228 // The mask element. This indexes into the input.
6229 int Idx = SVOp->getMaskElt(i+LaneStart);
6230 if (Idx < 0) {
6231 // the mask element does not index into any input vector.
6232 Mask.push_back(-1);
6233 continue;
6234 }
Craig Topper8f35c132012-01-20 09:29:03 +00006235
Craig Topper9a2b6e12012-04-06 07:45:23 +00006236 // The input vector this mask element indexes into.
6237 int Input = Idx / NumLaneElems;
Craig Topper8f35c132012-01-20 09:29:03 +00006238
Craig Topper9a2b6e12012-04-06 07:45:23 +00006239 // Turn the index into an offset from the start of the input vector.
6240 Idx -= Input * NumLaneElems;
6241
6242 // Find or create a shuffle vector operand to hold this input.
6243 unsigned OpNo;
6244 for (OpNo = 0; OpNo < array_lengthof(InputUsed); ++OpNo) {
6245 if (InputUsed[OpNo] == Input)
6246 // This input vector is already an operand.
6247 break;
6248 if (InputUsed[OpNo] < 0) {
6249 // Create a new operand for this input vector.
6250 InputUsed[OpNo] = Input;
6251 break;
6252 }
6253 }
6254
6255 if (OpNo >= array_lengthof(InputUsed)) {
Craig Topper8ae97ba2012-05-21 06:40:16 +00006256 // More than two input vectors used! Give up on trying to create a
6257 // shuffle vector. Insert all elements into a BUILD_VECTOR instead.
6258 UseBuildVector = true;
6259 break;
Craig Topper9a2b6e12012-04-06 07:45:23 +00006260 }
6261
6262 // Add the mask index for the new shuffle vector.
6263 Mask.push_back(Idx + OpNo * NumLaneElems);
6264 }
6265
Craig Topper8ae97ba2012-05-21 06:40:16 +00006266 if (UseBuildVector) {
6267 SmallVector<SDValue, 16> SVOps;
6268 for (unsigned i = 0; i != NumLaneElems; ++i) {
6269 // The mask element. This indexes into the input.
6270 int Idx = SVOp->getMaskElt(i+LaneStart);
6271 if (Idx < 0) {
6272 SVOps.push_back(DAG.getUNDEF(EltVT));
6273 continue;
6274 }
6275
6276 // The input vector this mask element indexes into.
6277 int Input = Idx / NumElems;
6278
6279 // Turn the index into an offset from the start of the input vector.
6280 Idx -= Input * NumElems;
6281
6282 // Extract the vector element by hand.
6283 SVOps.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EltVT,
6284 SVOp->getOperand(Input),
6285 DAG.getIntPtrConstant(Idx)));
6286 }
6287
6288 // Construct the output using a BUILD_VECTOR.
6289 Output[l] = DAG.getNode(ISD::BUILD_VECTOR, dl, NVT, &SVOps[0],
6290 SVOps.size());
6291 } else if (InputUsed[0] < 0) {
Craig Topper9a2b6e12012-04-06 07:45:23 +00006292 // No input vectors were used! The result is undefined.
Craig Topper8ae97ba2012-05-21 06:40:16 +00006293 Output[l] = DAG.getUNDEF(NVT);
Craig Topper9a2b6e12012-04-06 07:45:23 +00006294 } else {
6295 SDValue Op0 = Extract128BitVector(SVOp->getOperand(InputUsed[0] / 2),
Craig Topperb14940a2012-04-22 20:55:18 +00006296 (InputUsed[0] % 2) * NumLaneElems,
6297 DAG, dl);
Craig Topper9a2b6e12012-04-06 07:45:23 +00006298 // If only one input was used, use an undefined vector for the other.
6299 SDValue Op1 = (InputUsed[1] < 0) ? DAG.getUNDEF(NVT) :
6300 Extract128BitVector(SVOp->getOperand(InputUsed[1] / 2),
Craig Topperb14940a2012-04-22 20:55:18 +00006301 (InputUsed[1] % 2) * NumLaneElems, DAG, dl);
Craig Topper9a2b6e12012-04-06 07:45:23 +00006302 // At least one input vector was used. Create a new shuffle vector.
Craig Topper8ae97ba2012-05-21 06:40:16 +00006303 Output[l] = DAG.getVectorShuffle(NVT, dl, Op0, Op1, &Mask[0]);
Craig Topper9a2b6e12012-04-06 07:45:23 +00006304 }
6305
6306 Mask.clear();
6307 }
Craig Topper8f35c132012-01-20 09:29:03 +00006308
6309 // Concatenate the result back
Craig Topper8ae97ba2012-05-21 06:40:16 +00006310 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT, Output[0], Output[1]);
Bruno Cardoso Lopesd0888342011-07-22 00:14:56 +00006311}
6312
Bruno Cardoso Lopes589b8972011-07-22 00:14:53 +00006313/// LowerVECTOR_SHUFFLE_128v4 - Handle all 128-bit wide vectors with
6314/// 4 elements, and match them with several different shuffle types.
Dan Gohman475871a2008-07-27 21:46:04 +00006315static SDValue
Bruno Cardoso Lopes589b8972011-07-22 00:14:53 +00006316LowerVECTOR_SHUFFLE_128v4(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006317 SDValue V1 = SVOp->getOperand(0);
6318 SDValue V2 = SVOp->getOperand(1);
6319 DebugLoc dl = SVOp->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00006320 EVT VT = SVOp->getValueType(0);
Eric Christopherfd179292009-08-27 18:07:15 +00006321
Craig Topper7a9a28b2012-08-12 02:23:29 +00006322 assert(VT.is128BitVector() && "Unsupported vector size");
Bruno Cardoso Lopes589b8972011-07-22 00:14:53 +00006323
Benjamin Kramer9c683542012-01-30 15:16:21 +00006324 std::pair<int, int> Locs[4];
6325 int Mask1[] = { -1, -1, -1, -1 };
Benjamin Kramered4c8c62012-01-15 13:16:05 +00006326 SmallVector<int, 8> PermMask(SVOp->getMask().begin(), SVOp->getMask().end());
Nate Begeman9008ca62009-04-27 18:41:29 +00006327
Evan Chengace3c172008-07-22 21:13:36 +00006328 unsigned NumHi = 0;
6329 unsigned NumLo = 0;
Evan Chengace3c172008-07-22 21:13:36 +00006330 for (unsigned i = 0; i != 4; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006331 int Idx = PermMask[i];
6332 if (Idx < 0) {
Evan Chengace3c172008-07-22 21:13:36 +00006333 Locs[i] = std::make_pair(-1, -1);
6334 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00006335 assert(Idx < 8 && "Invalid VECTOR_SHUFFLE index!");
6336 if (Idx < 4) {
Evan Chengace3c172008-07-22 21:13:36 +00006337 Locs[i] = std::make_pair(0, NumLo);
Nate Begeman9008ca62009-04-27 18:41:29 +00006338 Mask1[NumLo] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00006339 NumLo++;
6340 } else {
6341 Locs[i] = std::make_pair(1, NumHi);
6342 if (2+NumHi < 4)
Nate Begeman9008ca62009-04-27 18:41:29 +00006343 Mask1[2+NumHi] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00006344 NumHi++;
6345 }
6346 }
6347 }
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006348
Evan Chengace3c172008-07-22 21:13:36 +00006349 if (NumLo <= 2 && NumHi <= 2) {
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006350 // If no more than two elements come from either vector. This can be
6351 // implemented with two shuffles. First shuffle gather the elements.
6352 // The second shuffle, which takes the first shuffle as both of its
6353 // vector operands, put the elements into the right order.
Nate Begeman9008ca62009-04-27 18:41:29 +00006354 V1 = DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006355
Benjamin Kramer9c683542012-01-30 15:16:21 +00006356 int Mask2[] = { -1, -1, -1, -1 };
Eric Christopherfd179292009-08-27 18:07:15 +00006357
Benjamin Kramer9c683542012-01-30 15:16:21 +00006358 for (unsigned i = 0; i != 4; ++i)
6359 if (Locs[i].first != -1) {
Evan Chengace3c172008-07-22 21:13:36 +00006360 unsigned Idx = (i < 2) ? 0 : 4;
6361 Idx += Locs[i].first * 2 + Locs[i].second;
Nate Begeman9008ca62009-04-27 18:41:29 +00006362 Mask2[i] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00006363 }
Evan Chengace3c172008-07-22 21:13:36 +00006364
Nate Begeman9008ca62009-04-27 18:41:29 +00006365 return DAG.getVectorShuffle(VT, dl, V1, V1, &Mask2[0]);
Craig Topper69947b92012-04-23 06:57:04 +00006366 }
6367
6368 if (NumLo == 3 || NumHi == 3) {
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006369 // Otherwise, we must have three elements from one vector, call it X, and
6370 // one element from the other, call it Y. First, use a shufps to build an
6371 // intermediate vector with the one element from Y and the element from X
6372 // that will be in the same half in the final destination (the indexes don't
6373 // matter). Then, use a shufps to build the final vector, taking the half
6374 // containing the element from Y from the intermediate, and the other half
6375 // from X.
6376 if (NumHi == 3) {
6377 // Normalize it so the 3 elements come from V1.
Craig Topperbeabc6c2011-12-05 06:56:46 +00006378 CommuteVectorShuffleMask(PermMask, 4);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006379 std::swap(V1, V2);
6380 }
6381
6382 // Find the element from V2.
6383 unsigned HiIndex;
6384 for (HiIndex = 0; HiIndex < 3; ++HiIndex) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006385 int Val = PermMask[HiIndex];
6386 if (Val < 0)
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006387 continue;
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006388 if (Val >= 4)
6389 break;
6390 }
6391
Nate Begeman9008ca62009-04-27 18:41:29 +00006392 Mask1[0] = PermMask[HiIndex];
6393 Mask1[1] = -1;
6394 Mask1[2] = PermMask[HiIndex^1];
6395 Mask1[3] = -1;
6396 V2 = DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006397
6398 if (HiIndex >= 2) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006399 Mask1[0] = PermMask[0];
6400 Mask1[1] = PermMask[1];
6401 Mask1[2] = HiIndex & 1 ? 6 : 4;
6402 Mask1[3] = HiIndex & 1 ? 4 : 6;
6403 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006404 }
Craig Topper69947b92012-04-23 06:57:04 +00006405
6406 Mask1[0] = HiIndex & 1 ? 2 : 0;
6407 Mask1[1] = HiIndex & 1 ? 0 : 2;
6408 Mask1[2] = PermMask[2];
6409 Mask1[3] = PermMask[3];
6410 if (Mask1[2] >= 0)
6411 Mask1[2] += 4;
6412 if (Mask1[3] >= 0)
6413 Mask1[3] += 4;
6414 return DAG.getVectorShuffle(VT, dl, V2, V1, &Mask1[0]);
Evan Chengace3c172008-07-22 21:13:36 +00006415 }
6416
6417 // Break it into (shuffle shuffle_hi, shuffle_lo).
Benjamin Kramer9c683542012-01-30 15:16:21 +00006418 int LoMask[] = { -1, -1, -1, -1 };
6419 int HiMask[] = { -1, -1, -1, -1 };
Nate Begeman9008ca62009-04-27 18:41:29 +00006420
Benjamin Kramer9c683542012-01-30 15:16:21 +00006421 int *MaskPtr = LoMask;
Evan Chengace3c172008-07-22 21:13:36 +00006422 unsigned MaskIdx = 0;
6423 unsigned LoIdx = 0;
6424 unsigned HiIdx = 2;
6425 for (unsigned i = 0; i != 4; ++i) {
6426 if (i == 2) {
Benjamin Kramer9c683542012-01-30 15:16:21 +00006427 MaskPtr = HiMask;
Evan Chengace3c172008-07-22 21:13:36 +00006428 MaskIdx = 1;
6429 LoIdx = 0;
6430 HiIdx = 2;
6431 }
Nate Begeman9008ca62009-04-27 18:41:29 +00006432 int Idx = PermMask[i];
6433 if (Idx < 0) {
Evan Chengace3c172008-07-22 21:13:36 +00006434 Locs[i] = std::make_pair(-1, -1);
Nate Begeman9008ca62009-04-27 18:41:29 +00006435 } else if (Idx < 4) {
Evan Chengace3c172008-07-22 21:13:36 +00006436 Locs[i] = std::make_pair(MaskIdx, LoIdx);
Benjamin Kramer9c683542012-01-30 15:16:21 +00006437 MaskPtr[LoIdx] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00006438 LoIdx++;
6439 } else {
6440 Locs[i] = std::make_pair(MaskIdx, HiIdx);
Benjamin Kramer9c683542012-01-30 15:16:21 +00006441 MaskPtr[HiIdx] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00006442 HiIdx++;
6443 }
6444 }
6445
Nate Begeman9008ca62009-04-27 18:41:29 +00006446 SDValue LoShuffle = DAG.getVectorShuffle(VT, dl, V1, V2, &LoMask[0]);
6447 SDValue HiShuffle = DAG.getVectorShuffle(VT, dl, V1, V2, &HiMask[0]);
Benjamin Kramer9c683542012-01-30 15:16:21 +00006448 int MaskOps[] = { -1, -1, -1, -1 };
6449 for (unsigned i = 0; i != 4; ++i)
6450 if (Locs[i].first != -1)
6451 MaskOps[i] = Locs[i].first * 4 + Locs[i].second;
Nate Begeman9008ca62009-04-27 18:41:29 +00006452 return DAG.getVectorShuffle(VT, dl, LoShuffle, HiShuffle, &MaskOps[0]);
Evan Chengace3c172008-07-22 21:13:36 +00006453}
6454
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00006455static bool MayFoldVectorLoad(SDValue V) {
Jakub Staszaka24262a2012-10-30 00:01:57 +00006456 while (V.hasOneUse() && V.getOpcode() == ISD::BITCAST)
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00006457 V = V.getOperand(0);
Jakub Staszaka24262a2012-10-30 00:01:57 +00006458
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00006459 if (V.hasOneUse() && V.getOpcode() == ISD::SCALAR_TO_VECTOR)
6460 V = V.getOperand(0);
Evan Cheng7bc389b2011-11-08 00:31:58 +00006461 if (V.hasOneUse() && V.getOpcode() == ISD::BUILD_VECTOR &&
6462 V.getNumOperands() == 2 && V.getOperand(1).getOpcode() == ISD::UNDEF)
6463 // BUILD_VECTOR (load), undef
6464 V = V.getOperand(0);
Jakub Staszaka24262a2012-10-30 00:01:57 +00006465
6466 return MayFoldLoad(V);
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00006467}
6468
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006469static
Evan Cheng835580f2010-10-07 20:50:20 +00006470SDValue getMOVDDup(SDValue &Op, DebugLoc &dl, SDValue V1, SelectionDAG &DAG) {
6471 EVT VT = Op.getValueType();
6472
6473 // Canonizalize to v2f64.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006474 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, V1);
6475 return DAG.getNode(ISD::BITCAST, dl, VT,
Evan Cheng835580f2010-10-07 20:50:20 +00006476 getTargetShuffleNode(X86ISD::MOVDDUP, dl, MVT::v2f64,
6477 V1, DAG));
6478}
6479
6480static
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006481SDValue getMOVLowToHigh(SDValue &Op, DebugLoc &dl, SelectionDAG &DAG,
Craig Topper1accb7e2012-01-10 06:54:16 +00006482 bool HasSSE2) {
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006483 SDValue V1 = Op.getOperand(0);
6484 SDValue V2 = Op.getOperand(1);
6485 EVT VT = Op.getValueType();
6486
6487 assert(VT != MVT::v2i64 && "unsupported shuffle type");
6488
Craig Topper1accb7e2012-01-10 06:54:16 +00006489 if (HasSSE2 && VT == MVT::v2f64)
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006490 return getTargetShuffleNode(X86ISD::MOVLHPD, dl, VT, V1, V2, DAG);
6491
Evan Cheng0899f5c2011-08-31 02:05:24 +00006492 // v4f32 or v4i32: canonizalized to v4f32 (which is legal for SSE1)
6493 return DAG.getNode(ISD::BITCAST, dl, VT,
6494 getTargetShuffleNode(X86ISD::MOVLHPS, dl, MVT::v4f32,
6495 DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, V1),
6496 DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, V2), DAG));
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006497}
6498
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00006499static
6500SDValue getMOVHighToLow(SDValue &Op, DebugLoc &dl, SelectionDAG &DAG) {
6501 SDValue V1 = Op.getOperand(0);
6502 SDValue V2 = Op.getOperand(1);
6503 EVT VT = Op.getValueType();
6504
6505 assert((VT == MVT::v4i32 || VT == MVT::v4f32) &&
6506 "unsupported shuffle type");
6507
6508 if (V2.getOpcode() == ISD::UNDEF)
6509 V2 = V1;
6510
6511 // v4i32 or v4f32
6512 return getTargetShuffleNode(X86ISD::MOVHLPS, dl, VT, V1, V2, DAG);
6513}
6514
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006515static
Craig Topper1accb7e2012-01-10 06:54:16 +00006516SDValue getMOVLP(SDValue &Op, DebugLoc &dl, SelectionDAG &DAG, bool HasSSE2) {
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006517 SDValue V1 = Op.getOperand(0);
6518 SDValue V2 = Op.getOperand(1);
6519 EVT VT = Op.getValueType();
6520 unsigned NumElems = VT.getVectorNumElements();
6521
6522 // Use MOVLPS and MOVLPD in case V1 or V2 are loads. During isel, the second
6523 // operand of these instructions is only memory, so check if there's a
6524 // potencial load folding here, otherwise use SHUFPS or MOVSD to match the
6525 // same masks.
6526 bool CanFoldLoad = false;
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006527
Bruno Cardoso Lopesd00bfe12010-09-02 02:35:51 +00006528 // Trivial case, when V2 comes from a load.
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00006529 if (MayFoldVectorLoad(V2))
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006530 CanFoldLoad = true;
6531
6532 // When V1 is a load, it can be folded later into a store in isel, example:
6533 // (store (v4f32 (X86Movlps (load addr:$src1), VR128:$src2)), addr:$src1)
6534 // turns into:
6535 // (MOVLPSmr addr:$src1, VR128:$src2)
6536 // So, recognize this potential and also use MOVLPS or MOVLPD
Evan Cheng7bc389b2011-11-08 00:31:58 +00006537 else if (MayFoldVectorLoad(V1) && MayFoldIntoStore(Op))
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006538 CanFoldLoad = true;
6539
Dan Gohman65fd6562011-11-03 21:49:52 +00006540 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006541 if (CanFoldLoad) {
Craig Topper1accb7e2012-01-10 06:54:16 +00006542 if (HasSSE2 && NumElems == 2)
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006543 return getTargetShuffleNode(X86ISD::MOVLPD, dl, VT, V1, V2, DAG);
6544
6545 if (NumElems == 4)
Benjamin Kramerd9b0b022012-06-02 10:20:22 +00006546 // If we don't care about the second element, proceed to use movss.
Dan Gohman65fd6562011-11-03 21:49:52 +00006547 if (SVOp->getMaskElt(1) != -1)
6548 return getTargetShuffleNode(X86ISD::MOVLPS, dl, VT, V1, V2, DAG);
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006549 }
6550
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006551 // movl and movlp will both match v2i64, but v2i64 is never matched by
6552 // movl earlier because we make it strict to avoid messing with the movlp load
6553 // folding logic (see the code above getMOVLP call). Match it here then,
6554 // this is horrible, but will stay like this until we move all shuffle
6555 // matching to x86 specific nodes. Note that for the 1st condition all
6556 // types are matched with movsd.
Craig Topper1accb7e2012-01-10 06:54:16 +00006557 if (HasSSE2) {
Bruno Cardoso Lopes5ca0d142011-09-14 02:36:14 +00006558 // FIXME: isMOVLMask should be checked and matched before getMOVLP,
6559 // as to remove this logic from here, as much as possible
Craig Topper5aaffa82012-02-19 02:53:47 +00006560 if (NumElems == 2 || !isMOVLMask(SVOp->getMask(), VT))
Bruno Cardoso Lopes57d6a5e2011-08-31 03:04:20 +00006561 return getTargetShuffleNode(X86ISD::MOVSD, dl, VT, V1, V2, DAG);
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006562 return getTargetShuffleNode(X86ISD::MOVSS, dl, VT, V1, V2, DAG);
Bruno Cardoso Lopes57d6a5e2011-08-31 03:04:20 +00006563 }
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006564
6565 assert(VT != MVT::v4i32 && "unsupported shuffle type");
6566
6567 // Invert the operand order and use SHUFPS to match it.
Craig Topperb3982da2011-12-31 23:50:21 +00006568 return getTargetShuffleNode(X86ISD::SHUFP, dl, VT, V2, V1,
Craig Topper5aaffa82012-02-19 02:53:47 +00006569 getShuffleSHUFImmediate(SVOp), DAG);
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006570}
6571
Michael Liaod9d09602012-10-23 17:34:00 +00006572// Reduce a vector shuffle to zext.
6573SDValue
6574X86TargetLowering::lowerVectorIntExtend(SDValue Op, SelectionDAG &DAG) const {
6575 // PMOVZX is only available from SSE41.
6576 if (!Subtarget->hasSSE41())
6577 return SDValue();
6578
6579 EVT VT = Op.getValueType();
6580
6581 // Only AVX2 support 256-bit vector integer extending.
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00006582 if (!Subtarget->hasInt256() && VT.is256BitVector())
Michael Liaod9d09602012-10-23 17:34:00 +00006583 return SDValue();
6584
6585 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
6586 DebugLoc DL = Op.getDebugLoc();
6587 SDValue V1 = Op.getOperand(0);
6588 SDValue V2 = Op.getOperand(1);
6589 unsigned NumElems = VT.getVectorNumElements();
6590
6591 // Extending is an unary operation and the element type of the source vector
6592 // won't be equal to or larger than i64.
6593 if (V2.getOpcode() != ISD::UNDEF || !VT.isInteger() ||
6594 VT.getVectorElementType() == MVT::i64)
6595 return SDValue();
6596
6597 // Find the expansion ratio, e.g. expanding from i8 to i32 has a ratio of 4.
6598 unsigned Shift = 1; // Start from 2, i.e. 1 << 1.
Duncan Sands34739052012-10-29 11:29:53 +00006599 while ((1U << Shift) < NumElems) {
6600 if (SVOp->getMaskElt(1U << Shift) == 1)
Michael Liaod9d09602012-10-23 17:34:00 +00006601 break;
6602 Shift += 1;
6603 // The maximal ratio is 8, i.e. from i8 to i64.
6604 if (Shift > 3)
6605 return SDValue();
6606 }
6607
6608 // Check the shuffle mask.
6609 unsigned Mask = (1U << Shift) - 1;
6610 for (unsigned i = 0; i != NumElems; ++i) {
6611 int EltIdx = SVOp->getMaskElt(i);
6612 if ((i & Mask) != 0 && EltIdx != -1)
6613 return SDValue();
Matt Beaumont-Gaya999de02012-10-23 19:46:36 +00006614 if ((i & Mask) == 0 && (unsigned)EltIdx != (i >> Shift))
Michael Liaod9d09602012-10-23 17:34:00 +00006615 return SDValue();
6616 }
6617
6618 unsigned NBits = VT.getVectorElementType().getSizeInBits() << Shift;
6619 EVT NeVT = EVT::getIntegerVT(*DAG.getContext(), NBits);
6620 EVT NVT = EVT::getVectorVT(*DAG.getContext(), NeVT, NumElems >> Shift);
6621
6622 if (!isTypeLegal(NVT))
6623 return SDValue();
6624
6625 // Simplify the operand as it's prepared to be fed into shuffle.
6626 unsigned SignificantBits = NVT.getSizeInBits() >> Shift;
6627 if (V1.getOpcode() == ISD::BITCAST &&
6628 V1.getOperand(0).getOpcode() == ISD::SCALAR_TO_VECTOR &&
6629 V1.getOperand(0).getOperand(0).getOpcode() == ISD::EXTRACT_VECTOR_ELT &&
6630 V1.getOperand(0)
6631 .getOperand(0).getValueType().getSizeInBits() == SignificantBits) {
6632 // (bitcast (sclr2vec (ext_vec_elt x))) -> (bitcast x)
6633 SDValue V = V1.getOperand(0).getOperand(0).getOperand(0);
Michael Liao07872742012-10-23 21:40:15 +00006634 ConstantSDNode *CIdx =
6635 dyn_cast<ConstantSDNode>(V1.getOperand(0).getOperand(0).getOperand(1));
Michael Liaod9d09602012-10-23 17:34:00 +00006636 // If it's foldable, i.e. normal load with single use, we will let code
6637 // selection to fold it. Otherwise, we will short the conversion sequence.
Michael Liao07872742012-10-23 21:40:15 +00006638 if (CIdx && CIdx->getZExtValue() == 0 &&
6639 (!ISD::isNormalLoad(V.getNode()) || !V.hasOneUse()))
Michael Liaod9d09602012-10-23 17:34:00 +00006640 V1 = DAG.getNode(ISD::BITCAST, DL, V1.getValueType(), V);
6641 }
6642
6643 return DAG.getNode(ISD::BITCAST, DL, VT,
6644 DAG.getNode(X86ISD::VZEXT, DL, NVT, V1));
6645}
6646
Nadav Rotem154819d2012-04-09 07:45:58 +00006647SDValue
6648X86TargetLowering::NormalizeVectorShuffle(SDValue Op, SelectionDAG &DAG) const {
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006649 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
6650 EVT VT = Op.getValueType();
6651 DebugLoc dl = Op.getDebugLoc();
6652 SDValue V1 = Op.getOperand(0);
6653 SDValue V2 = Op.getOperand(1);
6654
6655 if (isZeroShuffle(SVOp))
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00006656 return getZeroVector(VT, Subtarget, DAG, dl);
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006657
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006658 // Handle splat operations
6659 if (SVOp->isSplat()) {
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00006660 unsigned NumElem = VT.getVectorNumElements();
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00006661 int Size = VT.getSizeInBits();
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006662
Bruno Cardoso Lopes0e6d2302011-08-17 02:29:19 +00006663 // Use vbroadcast whenever the splat comes from a foldable load
Nadav Rotem154819d2012-04-09 07:45:58 +00006664 SDValue Broadcast = LowerVectorBroadcast(Op, DAG);
Nadav Rotem9d68b062012-04-08 12:54:54 +00006665 if (Broadcast.getNode())
6666 return Broadcast;
Bruno Cardoso Lopes0e6d2302011-08-17 02:29:19 +00006667
Bruno Cardoso Lopes6a32adc2011-07-25 23:05:25 +00006668 // Handle splats by matching through known shuffle masks
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00006669 if ((Size == 128 && NumElem <= 4) ||
Jakub Staszakd642baf2012-11-26 19:24:31 +00006670 (Size == 256 && NumElem <= 8))
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006671 return SDValue();
6672
Bruno Cardoso Lopes8a5b2622011-08-17 02:29:13 +00006673 // All remaning splats are promoted to target supported vector shuffles.
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006674 return PromoteSplat(SVOp, DAG);
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006675 }
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006676
Michael Liaod9d09602012-10-23 17:34:00 +00006677 // Check integer expanding shuffles.
6678 SDValue NewOp = lowerVectorIntExtend(Op, DAG);
6679 if (NewOp.getNode())
6680 return NewOp;
6681
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006682 // If the shuffle can be profitably rewritten as a narrower shuffle, then
6683 // do it!
Craig Topperf3640d72012-05-04 04:44:49 +00006684 if (VT == MVT::v8i16 || VT == MVT::v16i8 ||
6685 VT == MVT::v16i16 || VT == MVT::v32i8) {
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006686 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, dl);
6687 if (NewOp.getNode())
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006688 return DAG.getNode(ISD::BITCAST, dl, VT, NewOp);
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006689 } else if ((VT == MVT::v4i32 ||
Craig Topper1accb7e2012-01-10 06:54:16 +00006690 (VT == MVT::v4f32 && Subtarget->hasSSE2()))) {
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006691 // FIXME: Figure out a cleaner way to do this.
6692 // Try to make use of movq to zero out the top part.
6693 if (ISD::isBuildVectorAllZeros(V2.getNode())) {
6694 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, dl);
6695 if (NewOp.getNode()) {
Craig Topper5aaffa82012-02-19 02:53:47 +00006696 EVT NewVT = NewOp.getValueType();
6697 if (isCommutedMOVLMask(cast<ShuffleVectorSDNode>(NewOp)->getMask(),
6698 NewVT, true, false))
6699 return getVZextMovL(VT, NewVT, NewOp.getOperand(0),
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006700 DAG, Subtarget, dl);
6701 }
6702 } else if (ISD::isBuildVectorAllZeros(V1.getNode())) {
6703 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, dl);
Craig Topper5aaffa82012-02-19 02:53:47 +00006704 if (NewOp.getNode()) {
6705 EVT NewVT = NewOp.getValueType();
6706 if (isMOVLMask(cast<ShuffleVectorSDNode>(NewOp)->getMask(), NewVT))
6707 return getVZextMovL(VT, NewVT, NewOp.getOperand(1),
6708 DAG, Subtarget, dl);
6709 }
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006710 }
6711 }
6712 return SDValue();
6713}
6714
Dan Gohman475871a2008-07-27 21:46:04 +00006715SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006716X86TargetLowering::LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) const {
Nate Begeman9008ca62009-04-27 18:41:29 +00006717 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Dan Gohman475871a2008-07-27 21:46:04 +00006718 SDValue V1 = Op.getOperand(0);
6719 SDValue V2 = Op.getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +00006720 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006721 DebugLoc dl = Op.getDebugLoc();
Nate Begeman9008ca62009-04-27 18:41:29 +00006722 unsigned NumElems = VT.getVectorNumElements();
Elena Demikhovsky16db7102012-01-12 20:33:10 +00006723 bool V1IsUndef = V1.getOpcode() == ISD::UNDEF;
Evan Cheng0db9fe62006-04-25 20:13:52 +00006724 bool V2IsUndef = V2.getOpcode() == ISD::UNDEF;
Evan Chengd9b8e402006-10-16 06:36:00 +00006725 bool V1IsSplat = false;
6726 bool V2IsSplat = false;
Craig Topper1accb7e2012-01-10 06:54:16 +00006727 bool HasSSE2 = Subtarget->hasSSE2();
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00006728 bool HasFp256 = Subtarget->hasFp256();
6729 bool HasInt256 = Subtarget->hasInt256();
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006730 MachineFunction &MF = DAG.getMachineFunction();
Bill Wendling67658342012-10-09 07:45:08 +00006731 bool OptForSize = MF.getFunction()->getFnAttributes().
Bill Wendling034b94b2012-12-19 07:18:57 +00006732 hasAttribute(Attribute::OptimizeForSize);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006733
Craig Topper3426a3e2011-11-14 06:46:21 +00006734 assert(VT.getSizeInBits() != 64 && "Can't lower MMX shuffles");
Bruno Cardoso Lopes58277b12010-09-07 18:41:45 +00006735
Elena Demikhovsky16db7102012-01-12 20:33:10 +00006736 if (V1IsUndef && V2IsUndef)
6737 return DAG.getUNDEF(VT);
6738
6739 assert(!V1IsUndef && "Op 1 of shuffle should not be undef");
Craig Topper38034c52011-11-26 22:55:48 +00006740
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006741 // Vector shuffle lowering takes 3 steps:
6742 //
6743 // 1) Normalize the input vectors. Here splats, zeroed vectors, profitable
6744 // narrowing and commutation of operands should be handled.
6745 // 2) Matching of shuffles with known shuffle masks to x86 target specific
6746 // shuffle nodes.
6747 // 3) Rewriting of unmatched masks into new generic shuffle operations,
6748 // so the shuffle can be broken into other shuffles and the legalizer can
6749 // try the lowering again.
6750 //
Craig Topper3426a3e2011-11-14 06:46:21 +00006751 // The general idea is that no vector_shuffle operation should be left to
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006752 // be matched during isel, all of them must be converted to a target specific
6753 // node here.
Bruno Cardoso Lopes0d1340b2010-09-07 20:20:27 +00006754
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006755 // Normalize the input vectors. Here splats, zeroed vectors, profitable
6756 // narrowing and commutation of operands should be handled. The actual code
6757 // doesn't include all of those, work in progress...
Nadav Rotem154819d2012-04-09 07:45:58 +00006758 SDValue NewOp = NormalizeVectorShuffle(Op, DAG);
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006759 if (NewOp.getNode())
6760 return NewOp;
Eric Christopherfd179292009-08-27 18:07:15 +00006761
Craig Topper5aaffa82012-02-19 02:53:47 +00006762 SmallVector<int, 8> M(SVOp->getMask().begin(), SVOp->getMask().end());
6763
Bruno Cardoso Lopesa22c8452010-09-04 00:39:43 +00006764 // NOTE: isPSHUFDMask can also match both masks below (unpckl_undef and
6765 // unpckh_undef). Only use pshufd if speed is more important than size.
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00006766 if (OptForSize && isUNPCKL_v_undef_Mask(M, VT, HasInt256))
Craig Topper34671b82011-12-06 08:21:25 +00006767 return getTargetShuffleNode(X86ISD::UNPCKL, dl, VT, V1, V1, DAG);
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00006768 if (OptForSize && isUNPCKH_v_undef_Mask(M, VT, HasInt256))
Craig Topper34671b82011-12-06 08:21:25 +00006769 return getTargetShuffleNode(X86ISD::UNPCKH, dl, VT, V1, V1, DAG);
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00006770
Craig Topperdd637ae2012-02-19 05:41:45 +00006771 if (isMOVDDUPMask(M, VT) && Subtarget->hasSSE3() &&
Jakub Staszakd3a05632012-12-06 19:05:46 +00006772 V2IsUndef && MayFoldVectorLoad(V1))
Evan Cheng835580f2010-10-07 20:50:20 +00006773 return getMOVDDup(Op, dl, V1, DAG);
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006774
Craig Topperdd637ae2012-02-19 05:41:45 +00006775 if (isMOVHLPS_v_undef_Mask(M, VT))
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006776 return getMOVHighToLow(Op, dl, DAG);
6777
6778 // Use to match splats
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00006779 if (HasSSE2 && isUNPCKHMask(M, VT, HasInt256) && V2IsUndef &&
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006780 (VT == MVT::v2f64 || VT == MVT::v2i64))
Craig Topper34671b82011-12-06 08:21:25 +00006781 return getTargetShuffleNode(X86ISD::UNPCKH, dl, VT, V1, V1, DAG);
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006782
Craig Topper5aaffa82012-02-19 02:53:47 +00006783 if (isPSHUFDMask(M, VT)) {
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006784 // The actual implementation will match the mask in the if above and then
6785 // during isel it can match several different instructions, not only pshufd
6786 // as its name says, sad but true, emulate the behavior for now...
Craig Topperdd637ae2012-02-19 05:41:45 +00006787 if (isMOVDDUPMask(M, VT) && ((VT == MVT::v4f32 || VT == MVT::v2i64)))
6788 return getTargetShuffleNode(X86ISD::MOVLHPS, dl, VT, V1, V1, DAG);
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006789
Craig Topper5aaffa82012-02-19 02:53:47 +00006790 unsigned TargetMask = getShuffleSHUFImmediate(SVOp);
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006791
Craig Topper1accb7e2012-01-10 06:54:16 +00006792 if (HasSSE2 && (VT == MVT::v4f32 || VT == MVT::v4i32))
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006793 return getTargetShuffleNode(X86ISD::PSHUFD, dl, VT, V1, TargetMask, DAG);
6794
Nadav Roteme4ccfef2012-12-07 19:01:13 +00006795 if (HasFp256 && (VT == MVT::v4f32 || VT == MVT::v2f64))
6796 return getTargetShuffleNode(X86ISD::VPERMILP, dl, VT, V1, TargetMask,
6797 DAG);
6798
Craig Topperb3982da2011-12-31 23:50:21 +00006799 return getTargetShuffleNode(X86ISD::SHUFP, dl, VT, V1, V1,
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00006800 TargetMask, DAG);
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006801 }
Eric Christopherfd179292009-08-27 18:07:15 +00006802
Evan Chengf26ffe92008-05-29 08:22:04 +00006803 // Check if this can be converted into a logical shift.
6804 bool isLeft = false;
6805 unsigned ShAmt = 0;
Dan Gohman475871a2008-07-27 21:46:04 +00006806 SDValue ShVal;
Craig Topper1accb7e2012-01-10 06:54:16 +00006807 bool isShift = HasSSE2 && isVectorShift(SVOp, DAG, isLeft, ShVal, ShAmt);
Evan Chengf26ffe92008-05-29 08:22:04 +00006808 if (isShift && ShVal.hasOneUse()) {
Scott Michelfdc40a02009-02-17 22:15:04 +00006809 // If the shifted value has multiple uses, it may be cheaper to use
Evan Chengf26ffe92008-05-29 08:22:04 +00006810 // v_set0 + movlhps or movhlps, etc.
Dan Gohman8a55ce42009-09-23 21:02:20 +00006811 EVT EltVT = VT.getVectorElementType();
6812 ShAmt *= EltVT.getSizeInBits();
Dale Johannesenace16102009-02-03 19:33:06 +00006813 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00006814 }
Eric Christopherfd179292009-08-27 18:07:15 +00006815
Craig Topper5aaffa82012-02-19 02:53:47 +00006816 if (isMOVLMask(M, VT)) {
Gabor Greifba36cb52008-08-28 21:40:38 +00006817 if (ISD::isBuildVectorAllZeros(V1.getNode()))
Dale Johannesenace16102009-02-03 19:33:06 +00006818 return getVZextMovL(VT, VT, V2, DAG, Subtarget, dl);
Craig Topperdd637ae2012-02-19 05:41:45 +00006819 if (!isMOVLPMask(M, VT)) {
Craig Topper1accb7e2012-01-10 06:54:16 +00006820 if (HasSSE2 && (VT == MVT::v2i64 || VT == MVT::v2f64))
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00006821 return getTargetShuffleNode(X86ISD::MOVSD, dl, VT, V1, V2, DAG);
6822
Bruno Cardoso Lopes4783a3e2010-09-01 22:59:03 +00006823 if (VT == MVT::v4i32 || VT == MVT::v4f32)
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00006824 return getTargetShuffleNode(X86ISD::MOVSS, dl, VT, V1, V2, DAG);
6825 }
Evan Cheng7e2ff772008-05-08 00:57:18 +00006826 }
Eric Christopherfd179292009-08-27 18:07:15 +00006827
Nate Begeman9008ca62009-04-27 18:41:29 +00006828 // FIXME: fold these into legal mask.
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00006829 if (isMOVLHPSMask(M, VT) && !isUNPCKLMask(M, VT, HasInt256))
Craig Topper1accb7e2012-01-10 06:54:16 +00006830 return getMOVLowToHigh(Op, dl, DAG, HasSSE2);
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006831
Craig Topperdd637ae2012-02-19 05:41:45 +00006832 if (isMOVHLPSMask(M, VT))
Dale Johannesen0488fb62010-09-30 23:57:10 +00006833 return getMOVHighToLow(Op, dl, DAG);
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00006834
Craig Topperdd637ae2012-02-19 05:41:45 +00006835 if (V2IsUndef && isMOVSHDUPMask(M, VT, Subtarget))
Dale Johannesen0488fb62010-09-30 23:57:10 +00006836 return getTargetShuffleNode(X86ISD::MOVSHDUP, dl, VT, V1, DAG);
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00006837
Craig Topperdd637ae2012-02-19 05:41:45 +00006838 if (V2IsUndef && isMOVSLDUPMask(M, VT, Subtarget))
Dale Johannesen0488fb62010-09-30 23:57:10 +00006839 return getTargetShuffleNode(X86ISD::MOVSLDUP, dl, VT, V1, DAG);
Bruno Cardoso Lopes013bb3d2010-08-31 22:35:05 +00006840
Craig Topperdd637ae2012-02-19 05:41:45 +00006841 if (isMOVLPMask(M, VT))
Craig Topper1accb7e2012-01-10 06:54:16 +00006842 return getMOVLP(Op, dl, DAG, HasSSE2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006843
Craig Topperdd637ae2012-02-19 05:41:45 +00006844 if (ShouldXformToMOVHLPS(M, VT) ||
6845 ShouldXformToMOVLP(V1.getNode(), V2.getNode(), M, VT))
Nate Begeman9008ca62009-04-27 18:41:29 +00006846 return CommuteVectorShuffle(SVOp, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006847
Evan Chengf26ffe92008-05-29 08:22:04 +00006848 if (isShift) {
Craig Toppered2e13d2012-01-22 19:15:14 +00006849 // No better options. Use a vshldq / vsrldq.
Dan Gohman8a55ce42009-09-23 21:02:20 +00006850 EVT EltVT = VT.getVectorElementType();
6851 ShAmt *= EltVT.getSizeInBits();
Dale Johannesenace16102009-02-03 19:33:06 +00006852 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00006853 }
Eric Christopherfd179292009-08-27 18:07:15 +00006854
Evan Cheng9eca5e82006-10-25 21:49:50 +00006855 bool Commuted = false;
Chris Lattner8a594482007-11-25 00:24:49 +00006856 // FIXME: This should also accept a bitcast of a splat? Be careful, not
6857 // 1,1,1,1 -> v8i16 though.
Gabor Greifba36cb52008-08-28 21:40:38 +00006858 V1IsSplat = isSplatVector(V1.getNode());
6859 V2IsSplat = isSplatVector(V2.getNode());
Scott Michelfdc40a02009-02-17 22:15:04 +00006860
Chris Lattner8a594482007-11-25 00:24:49 +00006861 // Canonicalize the splat or undef, if present, to be on the RHS.
Craig Topper39a9e482012-02-11 06:24:48 +00006862 if (!V2IsUndef && V1IsSplat && !V2IsSplat) {
6863 CommuteVectorShuffleMask(M, NumElems);
6864 std::swap(V1, V2);
Evan Cheng9bbbb982006-10-25 20:48:19 +00006865 std::swap(V1IsSplat, V2IsSplat);
Evan Cheng9eca5e82006-10-25 21:49:50 +00006866 Commuted = true;
Evan Cheng9bbbb982006-10-25 20:48:19 +00006867 }
6868
Craig Topperbeabc6c2011-12-05 06:56:46 +00006869 if (isCommutedMOVLMask(M, VT, V2IsSplat, V2IsUndef)) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006870 // Shuffling low element of v1 into undef, just return v1.
Eric Christopherfd179292009-08-27 18:07:15 +00006871 if (V2IsUndef)
Nate Begeman9008ca62009-04-27 18:41:29 +00006872 return V1;
6873 // If V2 is a splat, the mask may be malformed such as <4,3,3,3>, which
6874 // the instruction selector will not match, so get a canonical MOVL with
6875 // swapped operands to undo the commute.
6876 return getMOVL(DAG, dl, VT, V2, V1);
Evan Chengd9b8e402006-10-16 06:36:00 +00006877 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00006878
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00006879 if (isUNPCKLMask(M, VT, HasInt256))
Craig Topper34671b82011-12-06 08:21:25 +00006880 return getTargetShuffleNode(X86ISD::UNPCKL, dl, VT, V1, V2, DAG);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00006881
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00006882 if (isUNPCKHMask(M, VT, HasInt256))
Craig Topper34671b82011-12-06 08:21:25 +00006883 return getTargetShuffleNode(X86ISD::UNPCKH, dl, VT, V1, V2, DAG);
Evan Chenge1113032006-10-04 18:33:38 +00006884
Evan Cheng9bbbb982006-10-25 20:48:19 +00006885 if (V2IsSplat) {
6886 // Normalize mask so all entries that point to V2 points to its first
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00006887 // element then try to match unpck{h|l} again. If match, return a
Craig Topper39a9e482012-02-11 06:24:48 +00006888 // new vector_shuffle with the corrected mask.p
6889 SmallVector<int, 8> NewMask(M.begin(), M.end());
6890 NormalizeMask(NewMask, NumElems);
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00006891 if (isUNPCKLMask(NewMask, VT, HasInt256, true))
Craig Topper39a9e482012-02-11 06:24:48 +00006892 return getTargetShuffleNode(X86ISD::UNPCKL, dl, VT, V1, V2, DAG);
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00006893 if (isUNPCKHMask(NewMask, VT, HasInt256, true))
Craig Topper39a9e482012-02-11 06:24:48 +00006894 return getTargetShuffleNode(X86ISD::UNPCKH, dl, VT, V1, V2, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006895 }
6896
Evan Cheng9eca5e82006-10-25 21:49:50 +00006897 if (Commuted) {
6898 // Commute is back and try unpck* again.
Nate Begeman9008ca62009-04-27 18:41:29 +00006899 // FIXME: this seems wrong.
Craig Topper39a9e482012-02-11 06:24:48 +00006900 CommuteVectorShuffleMask(M, NumElems);
6901 std::swap(V1, V2);
6902 std::swap(V1IsSplat, V2IsSplat);
6903 Commuted = false;
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00006904
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00006905 if (isUNPCKLMask(M, VT, HasInt256))
Craig Topper39a9e482012-02-11 06:24:48 +00006906 return getTargetShuffleNode(X86ISD::UNPCKL, dl, VT, V1, V2, DAG);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00006907
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00006908 if (isUNPCKHMask(M, VT, HasInt256))
Craig Topper39a9e482012-02-11 06:24:48 +00006909 return getTargetShuffleNode(X86ISD::UNPCKH, dl, VT, V1, V2, DAG);
Evan Cheng9eca5e82006-10-25 21:49:50 +00006910 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00006911
Nate Begeman9008ca62009-04-27 18:41:29 +00006912 // Normalize the node to match x86 shuffle ops if needed
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00006913 if (!V2IsUndef && (isSHUFPMask(M, VT, HasFp256, /* Commuted */ true)))
Nate Begeman9008ca62009-04-27 18:41:29 +00006914 return CommuteVectorShuffle(SVOp, DAG);
6915
Bruno Cardoso Lopes7256e222010-09-03 23:24:06 +00006916 // The checks below are all present in isShuffleMaskLegal, but they are
6917 // inlined here right now to enable us to directly emit target specific
6918 // nodes, and remove one by one until they don't return Op anymore.
Bruno Cardoso Lopes7256e222010-09-03 23:24:06 +00006919
Craig Topper0e2037b2012-01-20 05:53:00 +00006920 if (isPALIGNRMask(M, VT, Subtarget))
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00006921 return getTargetShuffleNode(X86ISD::PALIGN, dl, VT, V1, V2,
Craig Topperd93e4c32011-12-11 19:12:35 +00006922 getShufflePALIGNRImmediate(SVOp),
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00006923 DAG);
6924
Bruno Cardoso Lopesc800c0d2010-09-04 02:02:14 +00006925 if (ShuffleVectorSDNode::isSplatMask(&M[0], VT) &&
6926 SVOp->getSplatIndex() == 0 && V2IsUndef) {
Craig Topperbeabc6c2011-12-05 06:56:46 +00006927 if (VT == MVT::v2f64 || VT == MVT::v2i64)
Craig Topper34671b82011-12-06 08:21:25 +00006928 return getTargetShuffleNode(X86ISD::UNPCKL, dl, VT, V1, V1, DAG);
Bruno Cardoso Lopesc800c0d2010-09-04 02:02:14 +00006929 }
6930
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00006931 if (isPSHUFHWMask(M, VT, HasInt256))
Bruno Cardoso Lopesbbfc3102010-09-04 01:36:45 +00006932 return getTargetShuffleNode(X86ISD::PSHUFHW, dl, VT, V1,
Craig Topper5aaffa82012-02-19 02:53:47 +00006933 getShufflePSHUFHWImmediate(SVOp),
Bruno Cardoso Lopesbbfc3102010-09-04 01:36:45 +00006934 DAG);
6935
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00006936 if (isPSHUFLWMask(M, VT, HasInt256))
Bruno Cardoso Lopesbbfc3102010-09-04 01:36:45 +00006937 return getTargetShuffleNode(X86ISD::PSHUFLW, dl, VT, V1,
Craig Topper5aaffa82012-02-19 02:53:47 +00006938 getShufflePSHUFLWImmediate(SVOp),
Bruno Cardoso Lopesbbfc3102010-09-04 01:36:45 +00006939 DAG);
6940
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00006941 if (isSHUFPMask(M, VT, HasFp256))
Craig Topperb3982da2011-12-31 23:50:21 +00006942 return getTargetShuffleNode(X86ISD::SHUFP, dl, VT, V1, V2,
Craig Topper5aaffa82012-02-19 02:53:47 +00006943 getShuffleSHUFImmediate(SVOp), DAG);
Bruno Cardoso Lopes4c827f52010-09-04 01:22:57 +00006944
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00006945 if (isUNPCKL_v_undef_Mask(M, VT, HasInt256))
Craig Topper34671b82011-12-06 08:21:25 +00006946 return getTargetShuffleNode(X86ISD::UNPCKL, dl, VT, V1, V1, DAG);
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00006947 if (isUNPCKH_v_undef_Mask(M, VT, HasInt256))
Craig Topper34671b82011-12-06 08:21:25 +00006948 return getTargetShuffleNode(X86ISD::UNPCKH, dl, VT, V1, V1, DAG);
Bruno Cardoso Lopesa22c8452010-09-04 00:39:43 +00006949
Bruno Cardoso Lopesd0888342011-07-22 00:14:56 +00006950 //===--------------------------------------------------------------------===//
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00006951 // Generate target specific nodes for 128 or 256-bit shuffles only
6952 // supported in the AVX instruction set.
6953 //
Bruno Cardoso Lopesd0888342011-07-22 00:14:56 +00006954
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00006955 // Handle VMOVDDUPY permutations
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00006956 if (V2IsUndef && isMOVDDUPYMask(M, VT, HasFp256))
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00006957 return getTargetShuffleNode(X86ISD::MOVDDUP, dl, VT, V1, DAG);
6958
Craig Topper70b883b2011-11-28 10:14:51 +00006959 // Handle VPERMILPS/D* permutations
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00006960 if (isVPERMILPMask(M, VT, HasFp256)) {
6961 if (HasInt256 && VT == MVT::v8i32)
Craig Topperdbd98a42012-02-07 06:28:42 +00006962 return getTargetShuffleNode(X86ISD::PSHUFD, dl, VT, V1,
Craig Topper5aaffa82012-02-19 02:53:47 +00006963 getShuffleSHUFImmediate(SVOp), DAG);
Craig Topper316cd2a2011-11-30 06:25:25 +00006964 return getTargetShuffleNode(X86ISD::VPERMILP, dl, VT, V1,
Craig Topper5aaffa82012-02-19 02:53:47 +00006965 getShuffleSHUFImmediate(SVOp), DAG);
Craig Topperdbd98a42012-02-07 06:28:42 +00006966 }
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00006967
Craig Topper70b883b2011-11-28 10:14:51 +00006968 // Handle VPERM2F128/VPERM2I128 permutations
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00006969 if (isVPERM2X128Mask(M, VT, HasFp256))
Craig Topperec24e612011-11-30 07:47:51 +00006970 return getTargetShuffleNode(X86ISD::VPERM2X128, dl, VT, V1,
Craig Topper70b883b2011-11-28 10:14:51 +00006971 V2, getShuffleVPERM2X128Immediate(SVOp), DAG);
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00006972
Craig Topper1842ba02012-04-23 06:38:28 +00006973 SDValue BlendOp = LowerVECTOR_SHUFFLEtoBlend(SVOp, Subtarget, DAG);
Nadav Roteme80aa7c2012-04-09 08:33:21 +00006974 if (BlendOp.getNode())
6975 return BlendOp;
Craig Topper095c5282012-04-15 23:48:57 +00006976
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00006977 if (V2IsUndef && HasInt256 && (VT == MVT::v8i32 || VT == MVT::v8f32)) {
Craig Topper095c5282012-04-15 23:48:57 +00006978 SmallVector<SDValue, 8> permclMask;
Elena Demikhovsky73c504a2012-04-15 11:18:59 +00006979 for (unsigned i = 0; i != 8; ++i) {
Craig Topper095c5282012-04-15 23:48:57 +00006980 permclMask.push_back(DAG.getConstant((M[i]>=0) ? M[i] : 0, MVT::i32));
Elena Demikhovsky73c504a2012-04-15 11:18:59 +00006981 }
Craig Topper92040742012-04-16 06:43:40 +00006982 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v8i32,
6983 &permclMask[0], 8);
6984 // Bitcast is for VPERMPS since mask is v8i32 but node takes v8f32
Craig Topper8325c112012-04-16 00:41:45 +00006985 return DAG.getNode(X86ISD::VPERMV, dl, VT,
Craig Topper92040742012-04-16 06:43:40 +00006986 DAG.getNode(ISD::BITCAST, dl, VT, Mask), V1);
Elena Demikhovsky73c504a2012-04-15 11:18:59 +00006987 }
Craig Topper095c5282012-04-15 23:48:57 +00006988
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00006989 if (V2IsUndef && HasInt256 && (VT == MVT::v4i64 || VT == MVT::v4f64))
Craig Topper8325c112012-04-16 00:41:45 +00006990 return getTargetShuffleNode(X86ISD::VPERMI, dl, VT, V1,
Elena Demikhovsky73c504a2012-04-15 11:18:59 +00006991 getShuffleCLImmediate(SVOp), DAG);
6992
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00006993 //===--------------------------------------------------------------------===//
6994 // Since no target specific shuffle was selected for this generic one,
6995 // lower it into other known shuffles. FIXME: this isn't true yet, but
6996 // this is the plan.
6997 //
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00006998
Bruno Cardoso Lopes9b4ad122011-07-27 00:56:37 +00006999 // Handle v8i16 specifically since SSE can do byte extraction and insertion.
7000 if (VT == MVT::v8i16) {
Craig Topper55b24052012-09-11 06:15:32 +00007001 SDValue NewOp = LowerVECTOR_SHUFFLEv8i16(Op, Subtarget, DAG);
Bruno Cardoso Lopes9b4ad122011-07-27 00:56:37 +00007002 if (NewOp.getNode())
7003 return NewOp;
7004 }
7005
7006 if (VT == MVT::v16i8) {
7007 SDValue NewOp = LowerVECTOR_SHUFFLEv16i8(SVOp, DAG, *this);
7008 if (NewOp.getNode())
7009 return NewOp;
7010 }
7011
Elena Demikhovsky41789462012-09-06 12:42:01 +00007012 if (VT == MVT::v32i8) {
Craig Topper55b24052012-09-11 06:15:32 +00007013 SDValue NewOp = LowerVECTOR_SHUFFLEv32i8(SVOp, Subtarget, DAG);
Elena Demikhovsky41789462012-09-06 12:42:01 +00007014 if (NewOp.getNode())
7015 return NewOp;
7016 }
7017
Bruno Cardoso Lopes9b4ad122011-07-27 00:56:37 +00007018 // Handle all 128-bit wide vectors with 4 elements, and match them with
7019 // several different shuffle types.
Craig Topper7a9a28b2012-08-12 02:23:29 +00007020 if (NumElems == 4 && VT.is128BitVector())
Bruno Cardoso Lopes9b4ad122011-07-27 00:56:37 +00007021 return LowerVECTOR_SHUFFLE_128v4(SVOp, DAG);
7022
Bruno Cardoso Lopesd0888342011-07-22 00:14:56 +00007023 // Handle general 256-bit shuffles
7024 if (VT.is256BitVector())
7025 return LowerVECTOR_SHUFFLE_256(SVOp, DAG);
7026
Dan Gohman475871a2008-07-27 21:46:04 +00007027 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007028}
7029
Dan Gohman475871a2008-07-27 21:46:04 +00007030SDValue
7031X86TargetLowering::LowerEXTRACT_VECTOR_ELT_SSE4(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00007032 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00007033 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007034 DebugLoc dl = Op.getDebugLoc();
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007035
Craig Topper7a9a28b2012-08-12 02:23:29 +00007036 if (!Op.getOperand(0).getValueType().is128BitVector())
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007037 return SDValue();
7038
Duncan Sands83ec4b62008-06-06 12:08:01 +00007039 if (VT.getSizeInBits() == 8) {
Owen Anderson825b72b2009-08-11 20:47:22 +00007040 SDValue Extract = DAG.getNode(X86ISD::PEXTRB, dl, MVT::i32,
Craig Topper7c022842012-09-12 06:20:41 +00007041 Op.getOperand(0), Op.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00007042 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Extract,
Craig Topper7c022842012-09-12 06:20:41 +00007043 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00007044 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Craig Topper69947b92012-04-23 06:57:04 +00007045 }
7046
7047 if (VT.getSizeInBits() == 16) {
Evan Cheng52ceafa2009-01-02 05:29:08 +00007048 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
7049 // If Idx is 0, it's cheaper to do a move instead of a pextrw.
7050 if (Idx == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00007051 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i16,
7052 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007053 DAG.getNode(ISD::BITCAST, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00007054 MVT::v4i32,
Evan Cheng52ceafa2009-01-02 05:29:08 +00007055 Op.getOperand(0)),
7056 Op.getOperand(1)));
Owen Anderson825b72b2009-08-11 20:47:22 +00007057 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, dl, MVT::i32,
Craig Topper7c022842012-09-12 06:20:41 +00007058 Op.getOperand(0), Op.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00007059 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Extract,
Craig Topper7c022842012-09-12 06:20:41 +00007060 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00007061 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Craig Topper69947b92012-04-23 06:57:04 +00007062 }
7063
7064 if (VT == MVT::f32) {
Evan Cheng62a3f152008-03-24 21:52:23 +00007065 // EXTRACTPS outputs to a GPR32 register which will require a movd to copy
7066 // the result back to FR32 register. It's only worth matching if the
Dan Gohmand17cfbe2008-10-31 00:57:24 +00007067 // result has a single use which is a store or a bitcast to i32. And in
7068 // the case of a store, it's not worth it if the index is a constant 0,
7069 // because a MOVSSmr can be used instead, which is smaller and faster.
Evan Cheng62a3f152008-03-24 21:52:23 +00007070 if (!Op.hasOneUse())
Dan Gohman475871a2008-07-27 21:46:04 +00007071 return SDValue();
Gabor Greifba36cb52008-08-28 21:40:38 +00007072 SDNode *User = *Op.getNode()->use_begin();
Dan Gohmand17cfbe2008-10-31 00:57:24 +00007073 if ((User->getOpcode() != ISD::STORE ||
7074 (isa<ConstantSDNode>(Op.getOperand(1)) &&
7075 cast<ConstantSDNode>(Op.getOperand(1))->isNullValue())) &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007076 (User->getOpcode() != ISD::BITCAST ||
Owen Anderson825b72b2009-08-11 20:47:22 +00007077 User->getValueType(0) != MVT::i32))
Dan Gohman475871a2008-07-27 21:46:04 +00007078 return SDValue();
Owen Anderson825b72b2009-08-11 20:47:22 +00007079 SDValue Extract = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007080 DAG.getNode(ISD::BITCAST, dl, MVT::v4i32,
Dale Johannesenace16102009-02-03 19:33:06 +00007081 Op.getOperand(0)),
7082 Op.getOperand(1));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007083 return DAG.getNode(ISD::BITCAST, dl, MVT::f32, Extract);
Craig Topper69947b92012-04-23 06:57:04 +00007084 }
7085
7086 if (VT == MVT::i32 || VT == MVT::i64) {
Pete Coopera77214a2011-11-14 19:38:42 +00007087 // ExtractPS/pextrq works with constant index.
Mon P Wangf0fcdd82009-01-15 21:10:20 +00007088 if (isa<ConstantSDNode>(Op.getOperand(1)))
7089 return Op;
Nate Begeman14d12ca2008-02-11 04:19:36 +00007090 }
Dan Gohman475871a2008-07-27 21:46:04 +00007091 return SDValue();
Nate Begeman14d12ca2008-02-11 04:19:36 +00007092}
7093
Dan Gohman475871a2008-07-27 21:46:04 +00007094SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007095X86TargetLowering::LowerEXTRACT_VECTOR_ELT(SDValue Op,
7096 SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00007097 if (!isa<ConstantSDNode>(Op.getOperand(1)))
Dan Gohman475871a2008-07-27 21:46:04 +00007098 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007099
David Greene74a579d2011-02-10 16:57:36 +00007100 SDValue Vec = Op.getOperand(0);
7101 EVT VecVT = Vec.getValueType();
7102
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007103 // If this is a 256-bit vector result, first extract the 128-bit vector and
7104 // then extract the element from the 128-bit vector.
Craig Topper7a9a28b2012-08-12 02:23:29 +00007105 if (VecVT.is256BitVector()) {
David Greene74a579d2011-02-10 16:57:36 +00007106 DebugLoc dl = Op.getNode()->getDebugLoc();
7107 unsigned NumElems = VecVT.getVectorNumElements();
7108 SDValue Idx = Op.getOperand(1);
David Greene74a579d2011-02-10 16:57:36 +00007109 unsigned IdxVal = cast<ConstantSDNode>(Idx)->getZExtValue();
7110
7111 // Get the 128-bit vector.
Craig Topper7d1e3dc2012-04-30 05:17:10 +00007112 Vec = Extract128BitVector(Vec, IdxVal, DAG, dl);
David Greene74a579d2011-02-10 16:57:36 +00007113
Craig Topper7d1e3dc2012-04-30 05:17:10 +00007114 if (IdxVal >= NumElems/2)
7115 IdxVal -= NumElems/2;
David Greene74a579d2011-02-10 16:57:36 +00007116 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, Op.getValueType(), Vec,
Craig Topper7d1e3dc2012-04-30 05:17:10 +00007117 DAG.getConstant(IdxVal, MVT::i32));
David Greene74a579d2011-02-10 16:57:36 +00007118 }
7119
Craig Topper7a9a28b2012-08-12 02:23:29 +00007120 assert(VecVT.is128BitVector() && "Unexpected vector length");
David Greene74a579d2011-02-10 16:57:36 +00007121
Craig Topperd0a31172012-01-10 06:37:29 +00007122 if (Subtarget->hasSSE41()) {
Dan Gohman475871a2008-07-27 21:46:04 +00007123 SDValue Res = LowerEXTRACT_VECTOR_ELT_SSE4(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00007124 if (Res.getNode())
Evan Cheng62a3f152008-03-24 21:52:23 +00007125 return Res;
7126 }
Nate Begeman14d12ca2008-02-11 04:19:36 +00007127
Owen Andersone50ed302009-08-10 22:56:29 +00007128 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007129 DebugLoc dl = Op.getDebugLoc();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007130 // TODO: handle v16i8.
Duncan Sands83ec4b62008-06-06 12:08:01 +00007131 if (VT.getSizeInBits() == 16) {
Dan Gohman475871a2008-07-27 21:46:04 +00007132 SDValue Vec = Op.getOperand(0);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007133 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00007134 if (Idx == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00007135 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i16,
7136 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007137 DAG.getNode(ISD::BITCAST, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00007138 MVT::v4i32, Vec),
Evan Cheng14b32e12007-12-11 01:46:18 +00007139 Op.getOperand(1)));
Evan Cheng0db9fe62006-04-25 20:13:52 +00007140 // Transform it so it match pextrw which produces a 32-bit result.
Ken Dyck70d0ef12009-12-17 15:31:52 +00007141 EVT EltVT = MVT::i32;
Dan Gohman8a55ce42009-09-23 21:02:20 +00007142 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, dl, EltVT,
Craig Topper7c022842012-09-12 06:20:41 +00007143 Op.getOperand(0), Op.getOperand(1));
Dan Gohman8a55ce42009-09-23 21:02:20 +00007144 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, EltVT, Extract,
Craig Topper7c022842012-09-12 06:20:41 +00007145 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00007146 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Craig Topper69947b92012-04-23 06:57:04 +00007147 }
7148
7149 if (VT.getSizeInBits() == 32) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007150 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007151 if (Idx == 0)
7152 return Op;
Eric Christopherfd179292009-08-27 18:07:15 +00007153
Evan Cheng0db9fe62006-04-25 20:13:52 +00007154 // SHUFPS the element to the lowest double word, then movss.
Jeffrey Yasskina44defe2011-07-27 06:22:51 +00007155 int Mask[4] = { static_cast<int>(Idx), -1, -1, -1 };
Owen Andersone50ed302009-08-10 22:56:29 +00007156 EVT VVT = Op.getOperand(0).getValueType();
Eric Christopherfd179292009-08-27 18:07:15 +00007157 SDValue Vec = DAG.getVectorShuffle(VVT, dl, Op.getOperand(0),
Nate Begeman9008ca62009-04-27 18:41:29 +00007158 DAG.getUNDEF(VVT), Mask);
Dale Johannesenace16102009-02-03 19:33:06 +00007159 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, Vec,
Chris Lattner0bd48932008-01-17 07:00:52 +00007160 DAG.getIntPtrConstant(0));
Craig Topper69947b92012-04-23 06:57:04 +00007161 }
7162
7163 if (VT.getSizeInBits() == 64) {
Nate Begeman14d12ca2008-02-11 04:19:36 +00007164 // FIXME: .td only matches this for <2 x f64>, not <2 x i64> on 32b
7165 // FIXME: seems like this should be unnecessary if mov{h,l}pd were taught
7166 // to match extract_elt for f64.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007167 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007168 if (Idx == 0)
7169 return Op;
7170
7171 // UNPCKHPD the element to the lowest double word, then movsd.
7172 // Note if the lower 64 bits of the result of the UNPCKHPD is then stored
7173 // to a f64mem, the whole operation is folded into a single MOVHPDmr.
Nate Begeman9008ca62009-04-27 18:41:29 +00007174 int Mask[2] = { 1, -1 };
Owen Andersone50ed302009-08-10 22:56:29 +00007175 EVT VVT = Op.getOperand(0).getValueType();
Eric Christopherfd179292009-08-27 18:07:15 +00007176 SDValue Vec = DAG.getVectorShuffle(VVT, dl, Op.getOperand(0),
Nate Begeman9008ca62009-04-27 18:41:29 +00007177 DAG.getUNDEF(VVT), Mask);
Dale Johannesenace16102009-02-03 19:33:06 +00007178 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, Vec,
Chris Lattner0bd48932008-01-17 07:00:52 +00007179 DAG.getIntPtrConstant(0));
Evan Cheng0db9fe62006-04-25 20:13:52 +00007180 }
7181
Dan Gohman475871a2008-07-27 21:46:04 +00007182 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007183}
7184
Dan Gohman475871a2008-07-27 21:46:04 +00007185SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007186X86TargetLowering::LowerINSERT_VECTOR_ELT_SSE4(SDValue Op,
7187 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00007188 EVT VT = Op.getValueType();
Dan Gohman8a55ce42009-09-23 21:02:20 +00007189 EVT EltVT = VT.getVectorElementType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007190 DebugLoc dl = Op.getDebugLoc();
Nate Begeman14d12ca2008-02-11 04:19:36 +00007191
Dan Gohman475871a2008-07-27 21:46:04 +00007192 SDValue N0 = Op.getOperand(0);
7193 SDValue N1 = Op.getOperand(1);
7194 SDValue N2 = Op.getOperand(2);
Nate Begeman14d12ca2008-02-11 04:19:36 +00007195
Craig Topper7a9a28b2012-08-12 02:23:29 +00007196 if (!VT.is128BitVector())
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007197 return SDValue();
7198
Dan Gohman8a55ce42009-09-23 21:02:20 +00007199 if ((EltVT.getSizeInBits() == 8 || EltVT.getSizeInBits() == 16) &&
Dan Gohmanef521f12008-08-14 22:53:18 +00007200 isa<ConstantSDNode>(N2)) {
Chris Lattner8f2b4cc2010-02-23 02:07:48 +00007201 unsigned Opc;
7202 if (VT == MVT::v8i16)
7203 Opc = X86ISD::PINSRW;
Chris Lattner8f2b4cc2010-02-23 02:07:48 +00007204 else if (VT == MVT::v16i8)
7205 Opc = X86ISD::PINSRB;
7206 else
7207 Opc = X86ISD::PINSRB;
7208
Nate Begeman14d12ca2008-02-11 04:19:36 +00007209 // Transform it so it match pinsr{b,w} which expects a GR32 as its second
7210 // argument.
Owen Anderson825b72b2009-08-11 20:47:22 +00007211 if (N1.getValueType() != MVT::i32)
7212 N1 = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, N1);
7213 if (N2.getValueType() != MVT::i32)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007214 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Dale Johannesenace16102009-02-03 19:33:06 +00007215 return DAG.getNode(Opc, dl, VT, N0, N1, N2);
Craig Topper69947b92012-04-23 06:57:04 +00007216 }
7217
7218 if (EltVT == MVT::f32 && isa<ConstantSDNode>(N2)) {
Nate Begeman14d12ca2008-02-11 04:19:36 +00007219 // Bits [7:6] of the constant are the source select. This will always be
7220 // zero here. The DAG Combiner may combine an extract_elt index into these
7221 // bits. For example (insert (extract, 3), 2) could be matched by putting
7222 // the '3' into bits [7:6] of X86ISD::INSERTPS.
Scott Michelfdc40a02009-02-17 22:15:04 +00007223 // Bits [5:4] of the constant are the destination select. This is the
Nate Begeman14d12ca2008-02-11 04:19:36 +00007224 // value of the incoming immediate.
Scott Michelfdc40a02009-02-17 22:15:04 +00007225 // Bits [3:0] of the constant are the zero mask. The DAG Combiner may
Nate Begeman14d12ca2008-02-11 04:19:36 +00007226 // combine either bitwise AND or insert of float 0.0 to set these bits.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007227 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue() << 4);
Eric Christopherfbd66872009-07-24 00:33:09 +00007228 // Create this as a scalar to vector..
Owen Anderson825b72b2009-08-11 20:47:22 +00007229 N1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4f32, N1);
Dale Johannesenace16102009-02-03 19:33:06 +00007230 return DAG.getNode(X86ISD::INSERTPS, dl, VT, N0, N1, N2);
Craig Topper69947b92012-04-23 06:57:04 +00007231 }
7232
7233 if ((EltVT == MVT::i32 || EltVT == MVT::i64) && isa<ConstantSDNode>(N2)) {
Eric Christopherfbd66872009-07-24 00:33:09 +00007234 // PINSR* works with constant index.
7235 return Op;
Nate Begeman14d12ca2008-02-11 04:19:36 +00007236 }
Dan Gohman475871a2008-07-27 21:46:04 +00007237 return SDValue();
Nate Begeman14d12ca2008-02-11 04:19:36 +00007238}
7239
Dan Gohman475871a2008-07-27 21:46:04 +00007240SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007241X86TargetLowering::LowerINSERT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00007242 EVT VT = Op.getValueType();
Dan Gohman8a55ce42009-09-23 21:02:20 +00007243 EVT EltVT = VT.getVectorElementType();
Nate Begeman14d12ca2008-02-11 04:19:36 +00007244
David Greene6b381262011-02-09 15:32:06 +00007245 DebugLoc dl = Op.getDebugLoc();
7246 SDValue N0 = Op.getOperand(0);
7247 SDValue N1 = Op.getOperand(1);
7248 SDValue N2 = Op.getOperand(2);
7249
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007250 // If this is a 256-bit vector result, first extract the 128-bit vector,
7251 // insert the element into the extracted half and then place it back.
Craig Topper7a9a28b2012-08-12 02:23:29 +00007252 if (VT.is256BitVector()) {
David Greene6b381262011-02-09 15:32:06 +00007253 if (!isa<ConstantSDNode>(N2))
7254 return SDValue();
7255
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007256 // Get the desired 128-bit vector half.
David Greene6b381262011-02-09 15:32:06 +00007257 unsigned NumElems = VT.getVectorNumElements();
7258 unsigned IdxVal = cast<ConstantSDNode>(N2)->getZExtValue();
Craig Topper7d1e3dc2012-04-30 05:17:10 +00007259 SDValue V = Extract128BitVector(N0, IdxVal, DAG, dl);
David Greene6b381262011-02-09 15:32:06 +00007260
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007261 // Insert the element into the desired half.
Craig Topper7d1e3dc2012-04-30 05:17:10 +00007262 bool Upper = IdxVal >= NumElems/2;
7263 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, V.getValueType(), V, N1,
7264 DAG.getConstant(Upper ? IdxVal-NumElems/2 : IdxVal, MVT::i32));
David Greene6b381262011-02-09 15:32:06 +00007265
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007266 // Insert the changed part back to the 256-bit vector
Craig Topper7d1e3dc2012-04-30 05:17:10 +00007267 return Insert128BitVector(N0, V, IdxVal, DAG, dl);
David Greene6b381262011-02-09 15:32:06 +00007268 }
7269
Craig Topperd0a31172012-01-10 06:37:29 +00007270 if (Subtarget->hasSSE41())
Nate Begeman14d12ca2008-02-11 04:19:36 +00007271 return LowerINSERT_VECTOR_ELT_SSE4(Op, DAG);
7272
Dan Gohman8a55ce42009-09-23 21:02:20 +00007273 if (EltVT == MVT::i8)
Dan Gohman475871a2008-07-27 21:46:04 +00007274 return SDValue();
Evan Cheng794405e2007-12-12 07:55:34 +00007275
Dan Gohman8a55ce42009-09-23 21:02:20 +00007276 if (EltVT.getSizeInBits() == 16 && isa<ConstantSDNode>(N2)) {
Evan Cheng794405e2007-12-12 07:55:34 +00007277 // Transform it so it match pinsrw which expects a 16-bit value in a GR32
7278 // as its second argument.
Owen Anderson825b72b2009-08-11 20:47:22 +00007279 if (N1.getValueType() != MVT::i32)
7280 N1 = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, N1);
7281 if (N2.getValueType() != MVT::i32)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007282 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Dale Johannesen0488fb62010-09-30 23:57:10 +00007283 return DAG.getNode(X86ISD::PINSRW, dl, VT, N0, N1, N2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007284 }
Dan Gohman475871a2008-07-27 21:46:04 +00007285 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007286}
7287
Craig Topper55b24052012-09-11 06:15:32 +00007288static SDValue LowerSCALAR_TO_VECTOR(SDValue Op, SelectionDAG &DAG) {
Bruno Cardoso Lopes233fa392011-07-25 23:05:16 +00007289 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007290 DebugLoc dl = Op.getDebugLoc();
David Greene2fcdfb42011-02-10 23:11:29 +00007291 EVT OpVT = Op.getValueType();
7292
Bruno Cardoso Lopes233fa392011-07-25 23:05:16 +00007293 // If this is a 256-bit vector result, first insert into a 128-bit
7294 // vector and then insert into the 256-bit vector.
Craig Topper7a9a28b2012-08-12 02:23:29 +00007295 if (!OpVT.is128BitVector()) {
Bruno Cardoso Lopes233fa392011-07-25 23:05:16 +00007296 // Insert into a 128-bit vector.
7297 EVT VT128 = EVT::getVectorVT(*Context,
7298 OpVT.getVectorElementType(),
7299 OpVT.getVectorNumElements() / 2);
7300
7301 Op = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT128, Op.getOperand(0));
7302
7303 // Insert the 128-bit vector.
Craig Topperb14940a2012-04-22 20:55:18 +00007304 return Insert128BitVector(DAG.getUNDEF(OpVT), Op, 0, DAG, dl);
Bruno Cardoso Lopes233fa392011-07-25 23:05:16 +00007305 }
7306
Craig Topperd77d2fe2012-04-29 20:22:05 +00007307 if (OpVT == MVT::v1i64 &&
Chris Lattnerf172ecd2010-07-04 23:07:25 +00007308 Op.getOperand(0).getValueType() == MVT::i64)
Owen Anderson825b72b2009-08-11 20:47:22 +00007309 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v1i64, Op.getOperand(0));
Rafael Espindoladef390a2009-08-03 02:45:34 +00007310
Owen Anderson825b72b2009-08-11 20:47:22 +00007311 SDValue AnyExt = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, Op.getOperand(0));
Craig Topper7a9a28b2012-08-12 02:23:29 +00007312 assert(OpVT.is128BitVector() && "Expected an SSE type!");
Craig Topperd77d2fe2012-04-29 20:22:05 +00007313 return DAG.getNode(ISD::BITCAST, dl, OpVT,
Dale Johannesen0488fb62010-09-30 23:57:10 +00007314 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,AnyExt));
Evan Cheng0db9fe62006-04-25 20:13:52 +00007315}
7316
David Greene91585092011-01-26 15:38:49 +00007317// Lower a node with an EXTRACT_SUBVECTOR opcode. This may result in
7318// a simple subregister reference or explicit instructions to grab
7319// upper bits of a vector.
Craig Topper55b24052012-09-11 06:15:32 +00007320static SDValue LowerEXTRACT_SUBVECTOR(SDValue Op, const X86Subtarget *Subtarget,
7321 SelectionDAG &DAG) {
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00007322 if (Subtarget->hasFp256()) {
David Greenea5f26012011-02-07 19:36:54 +00007323 DebugLoc dl = Op.getNode()->getDebugLoc();
7324 SDValue Vec = Op.getNode()->getOperand(0);
7325 SDValue Idx = Op.getNode()->getOperand(1);
7326
Craig Topper7a9a28b2012-08-12 02:23:29 +00007327 if (Op.getNode()->getValueType(0).is128BitVector() &&
7328 Vec.getNode()->getValueType(0).is256BitVector() &&
Craig Topperb14940a2012-04-22 20:55:18 +00007329 isa<ConstantSDNode>(Idx)) {
7330 unsigned IdxVal = cast<ConstantSDNode>(Idx)->getZExtValue();
7331 return Extract128BitVector(Vec, IdxVal, DAG, dl);
David Greenea5f26012011-02-07 19:36:54 +00007332 }
David Greene91585092011-01-26 15:38:49 +00007333 }
7334 return SDValue();
7335}
7336
David Greenecfe33c42011-01-26 19:13:22 +00007337// Lower a node with an INSERT_SUBVECTOR opcode. This may result in a
7338// simple superregister reference or explicit instructions to insert
7339// the upper bits of a vector.
Craig Topper55b24052012-09-11 06:15:32 +00007340static SDValue LowerINSERT_SUBVECTOR(SDValue Op, const X86Subtarget *Subtarget,
7341 SelectionDAG &DAG) {
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00007342 if (Subtarget->hasFp256()) {
David Greenecfe33c42011-01-26 19:13:22 +00007343 DebugLoc dl = Op.getNode()->getDebugLoc();
7344 SDValue Vec = Op.getNode()->getOperand(0);
7345 SDValue SubVec = Op.getNode()->getOperand(1);
7346 SDValue Idx = Op.getNode()->getOperand(2);
7347
Craig Topper7a9a28b2012-08-12 02:23:29 +00007348 if (Op.getNode()->getValueType(0).is256BitVector() &&
7349 SubVec.getNode()->getValueType(0).is128BitVector() &&
Craig Topperb14940a2012-04-22 20:55:18 +00007350 isa<ConstantSDNode>(Idx)) {
7351 unsigned IdxVal = cast<ConstantSDNode>(Idx)->getZExtValue();
7352 return Insert128BitVector(Vec, SubVec, IdxVal, DAG, dl);
David Greenecfe33c42011-01-26 19:13:22 +00007353 }
7354 }
7355 return SDValue();
7356}
7357
Bill Wendling056292f2008-09-16 21:48:12 +00007358// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
7359// their target countpart wrapped in the X86ISD::Wrapper node. Suppose N is
7360// one of the above mentioned nodes. It has to be wrapped because otherwise
7361// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
7362// be used to form addressing mode. These wrapped nodes will be selected
7363// into MOV32ri.
Dan Gohman475871a2008-07-27 21:46:04 +00007364SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007365X86TargetLowering::LowerConstantPool(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00007366 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Eric Christopherfd179292009-08-27 18:07:15 +00007367
Chris Lattner41621a22009-06-26 19:22:52 +00007368 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
7369 // global base reg.
7370 unsigned char OpFlag = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00007371 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007372 CodeModel::Model M = getTargetMachine().getCodeModel();
7373
Chris Lattner4f066492009-07-11 20:29:19 +00007374 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007375 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattnere4df7562009-07-09 03:15:51 +00007376 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00007377 else if (Subtarget->isPICStyleGOT())
Chris Lattner88e1fd52009-07-09 04:24:46 +00007378 OpFlag = X86II::MO_GOTOFF;
Chris Lattnere2c92082009-07-10 21:00:45 +00007379 else if (Subtarget->isPICStyleStubPIC())
Chris Lattner88e1fd52009-07-09 04:24:46 +00007380 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Eric Christopherfd179292009-08-27 18:07:15 +00007381
Evan Cheng1606e8e2009-03-13 07:51:59 +00007382 SDValue Result = DAG.getTargetConstantPool(CP->getConstVal(), getPointerTy(),
Chris Lattner41621a22009-06-26 19:22:52 +00007383 CP->getAlignment(),
7384 CP->getOffset(), OpFlag);
7385 DebugLoc DL = CP->getDebugLoc();
Chris Lattner18c59872009-06-27 04:16:01 +00007386 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00007387 // With PIC, the address is actually $g + Offset.
Chris Lattner41621a22009-06-26 19:22:52 +00007388 if (OpFlag) {
7389 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesenb300d2a2009-02-07 00:55:49 +00007390 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00007391 DebugLoc(), getPointerTy()),
Anton Korobeynikov7f705592007-01-12 19:20:47 +00007392 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007393 }
7394
7395 return Result;
7396}
7397
Dan Gohmand858e902010-04-17 15:26:15 +00007398SDValue X86TargetLowering::LowerJumpTable(SDValue Op, SelectionDAG &DAG) const {
Chris Lattner18c59872009-06-27 04:16:01 +00007399 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
Eric Christopherfd179292009-08-27 18:07:15 +00007400
Chris Lattner18c59872009-06-27 04:16:01 +00007401 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
7402 // global base reg.
7403 unsigned char OpFlag = 0;
7404 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007405 CodeModel::Model M = getTargetMachine().getCodeModel();
7406
Chris Lattner4f066492009-07-11 20:29:19 +00007407 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007408 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattnere4df7562009-07-09 03:15:51 +00007409 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00007410 else if (Subtarget->isPICStyleGOT())
Chris Lattner88e1fd52009-07-09 04:24:46 +00007411 OpFlag = X86II::MO_GOTOFF;
Chris Lattnere2c92082009-07-10 21:00:45 +00007412 else if (Subtarget->isPICStyleStubPIC())
Chris Lattner88e1fd52009-07-09 04:24:46 +00007413 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Eric Christopherfd179292009-08-27 18:07:15 +00007414
Chris Lattner18c59872009-06-27 04:16:01 +00007415 SDValue Result = DAG.getTargetJumpTable(JT->getIndex(), getPointerTy(),
7416 OpFlag);
7417 DebugLoc DL = JT->getDebugLoc();
7418 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Eric Christopherfd179292009-08-27 18:07:15 +00007419
Chris Lattner18c59872009-06-27 04:16:01 +00007420 // With PIC, the address is actually $g + Offset.
Chris Lattner1e61e692010-11-15 02:46:57 +00007421 if (OpFlag)
Chris Lattner18c59872009-06-27 04:16:01 +00007422 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
7423 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00007424 DebugLoc(), getPointerTy()),
Chris Lattner18c59872009-06-27 04:16:01 +00007425 Result);
Eric Christopherfd179292009-08-27 18:07:15 +00007426
Chris Lattner18c59872009-06-27 04:16:01 +00007427 return Result;
7428}
7429
7430SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007431X86TargetLowering::LowerExternalSymbol(SDValue Op, SelectionDAG &DAG) const {
Chris Lattner18c59872009-06-27 04:16:01 +00007432 const char *Sym = cast<ExternalSymbolSDNode>(Op)->getSymbol();
Eric Christopherfd179292009-08-27 18:07:15 +00007433
Chris Lattner18c59872009-06-27 04:16:01 +00007434 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
7435 // global base reg.
7436 unsigned char OpFlag = 0;
7437 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007438 CodeModel::Model M = getTargetMachine().getCodeModel();
7439
Chris Lattner4f066492009-07-11 20:29:19 +00007440 if (Subtarget->isPICStyleRIPRel() &&
Eli Friedman586272d2011-08-11 01:48:05 +00007441 (M == CodeModel::Small || M == CodeModel::Kernel)) {
7442 if (Subtarget->isTargetDarwin() || Subtarget->isTargetELF())
7443 OpFlag = X86II::MO_GOTPCREL;
Chris Lattnere4df7562009-07-09 03:15:51 +00007444 WrapperKind = X86ISD::WrapperRIP;
Eli Friedman586272d2011-08-11 01:48:05 +00007445 } else if (Subtarget->isPICStyleGOT()) {
7446 OpFlag = X86II::MO_GOT;
7447 } else if (Subtarget->isPICStyleStubPIC()) {
7448 OpFlag = X86II::MO_DARWIN_NONLAZY_PIC_BASE;
7449 } else if (Subtarget->isPICStyleStubNoDynamic()) {
7450 OpFlag = X86II::MO_DARWIN_NONLAZY;
7451 }
Eric Christopherfd179292009-08-27 18:07:15 +00007452
Chris Lattner18c59872009-06-27 04:16:01 +00007453 SDValue Result = DAG.getTargetExternalSymbol(Sym, getPointerTy(), OpFlag);
Eric Christopherfd179292009-08-27 18:07:15 +00007454
Chris Lattner18c59872009-06-27 04:16:01 +00007455 DebugLoc DL = Op.getDebugLoc();
7456 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Eric Christopherfd179292009-08-27 18:07:15 +00007457
Chris Lattner18c59872009-06-27 04:16:01 +00007458 // With PIC, the address is actually $g + Offset.
7459 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
Chris Lattnere4df7562009-07-09 03:15:51 +00007460 !Subtarget->is64Bit()) {
Chris Lattner18c59872009-06-27 04:16:01 +00007461 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
7462 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00007463 DebugLoc(), getPointerTy()),
Chris Lattner18c59872009-06-27 04:16:01 +00007464 Result);
7465 }
Eric Christopherfd179292009-08-27 18:07:15 +00007466
Eli Friedman586272d2011-08-11 01:48:05 +00007467 // For symbols that require a load from a stub to get the address, emit the
7468 // load.
7469 if (isGlobalStubReference(OpFlag))
7470 Result = DAG.getLoad(getPointerTy(), DL, DAG.getEntryNode(), Result,
Pete Cooperd752e0f2011-11-08 18:42:53 +00007471 MachinePointerInfo::getGOT(), false, false, false, 0);
Eli Friedman586272d2011-08-11 01:48:05 +00007472
Chris Lattner18c59872009-06-27 04:16:01 +00007473 return Result;
7474}
7475
Dan Gohman475871a2008-07-27 21:46:04 +00007476SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007477X86TargetLowering::LowerBlockAddress(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman29cbade2009-11-20 23:18:13 +00007478 // Create the TargetBlockAddressAddress node.
7479 unsigned char OpFlags =
7480 Subtarget->ClassifyBlockAddressReference();
Dan Gohmanf705adb2009-10-30 01:28:02 +00007481 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman46510a72010-04-15 01:51:59 +00007482 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
Michael Liao6c7ccaa2012-09-12 21:43:09 +00007483 int64_t Offset = cast<BlockAddressSDNode>(Op)->getOffset();
Dan Gohman29cbade2009-11-20 23:18:13 +00007484 DebugLoc dl = Op.getDebugLoc();
Michael Liao6c7ccaa2012-09-12 21:43:09 +00007485 SDValue Result = DAG.getTargetBlockAddress(BA, getPointerTy(), Offset,
7486 OpFlags);
Dan Gohman29cbade2009-11-20 23:18:13 +00007487
Dan Gohmanf705adb2009-10-30 01:28:02 +00007488 if (Subtarget->isPICStyleRIPRel() &&
7489 (M == CodeModel::Small || M == CodeModel::Kernel))
Dan Gohman29cbade2009-11-20 23:18:13 +00007490 Result = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Result);
7491 else
7492 Result = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), Result);
Dan Gohmanf705adb2009-10-30 01:28:02 +00007493
Dan Gohman29cbade2009-11-20 23:18:13 +00007494 // With PIC, the address is actually $g + Offset.
7495 if (isGlobalRelativeToPICBase(OpFlags)) {
7496 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(),
7497 DAG.getNode(X86ISD::GlobalBaseReg, dl, getPointerTy()),
7498 Result);
7499 }
Dan Gohmanf705adb2009-10-30 01:28:02 +00007500
7501 return Result;
7502}
7503
7504SDValue
Dale Johannesen33c960f2009-02-04 20:06:27 +00007505X86TargetLowering::LowerGlobalAddress(const GlobalValue *GV, DebugLoc dl,
Dan Gohman6520e202008-10-18 02:06:02 +00007506 int64_t Offset,
Evan Chengda43bcf2008-09-24 00:05:32 +00007507 SelectionDAG &DAG) const {
Dan Gohman6520e202008-10-18 02:06:02 +00007508 // Create the TargetGlobalAddress node, folding in the constant
7509 // offset if it is legal.
Chris Lattnerd392bd92009-07-10 07:20:05 +00007510 unsigned char OpFlags =
7511 Subtarget->ClassifyGlobalReference(GV, getTargetMachine());
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007512 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman6520e202008-10-18 02:06:02 +00007513 SDValue Result;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007514 if (OpFlags == X86II::MO_NO_FLAG &&
7515 X86::isOffsetSuitableForCodeModel(Offset, M)) {
Chris Lattner4aa21aa2009-07-09 00:58:53 +00007516 // A direct static reference to a global.
Devang Patel0d881da2010-07-06 22:08:15 +00007517 Result = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(), Offset);
Dan Gohman6520e202008-10-18 02:06:02 +00007518 Offset = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00007519 } else {
Devang Patel0d881da2010-07-06 22:08:15 +00007520 Result = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(), 0, OpFlags);
Chris Lattner18c59872009-06-27 04:16:01 +00007521 }
Eric Christopherfd179292009-08-27 18:07:15 +00007522
Chris Lattner4f066492009-07-11 20:29:19 +00007523 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007524 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattner18c59872009-06-27 04:16:01 +00007525 Result = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Result);
7526 else
7527 Result = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), Result);
Dan Gohman6520e202008-10-18 02:06:02 +00007528
Anton Korobeynikov7f705592007-01-12 19:20:47 +00007529 // With PIC, the address is actually $g + Offset.
Chris Lattner36c25012009-07-10 07:34:39 +00007530 if (isGlobalRelativeToPICBase(OpFlags)) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00007531 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(),
7532 DAG.getNode(X86ISD::GlobalBaseReg, dl, getPointerTy()),
Anton Korobeynikov7f705592007-01-12 19:20:47 +00007533 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007534 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007535
Chris Lattner36c25012009-07-10 07:34:39 +00007536 // For globals that require a load from a stub to get the address, emit the
7537 // load.
7538 if (isGlobalStubReference(OpFlags))
Dale Johannesen33c960f2009-02-04 20:06:27 +00007539 Result = DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(), Result,
Pete Cooperd752e0f2011-11-08 18:42:53 +00007540 MachinePointerInfo::getGOT(), false, false, false, 0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007541
Dan Gohman6520e202008-10-18 02:06:02 +00007542 // If there was a non-zero offset that we didn't fold, create an explicit
7543 // addition for it.
7544 if (Offset != 0)
Dale Johannesen33c960f2009-02-04 20:06:27 +00007545 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(), Result,
Dan Gohman6520e202008-10-18 02:06:02 +00007546 DAG.getConstant(Offset, getPointerTy()));
7547
Evan Cheng0db9fe62006-04-25 20:13:52 +00007548 return Result;
7549}
7550
Evan Chengda43bcf2008-09-24 00:05:32 +00007551SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007552X86TargetLowering::LowerGlobalAddress(SDValue Op, SelectionDAG &DAG) const {
Evan Chengda43bcf2008-09-24 00:05:32 +00007553 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Dan Gohman6520e202008-10-18 02:06:02 +00007554 int64_t Offset = cast<GlobalAddressSDNode>(Op)->getOffset();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007555 return LowerGlobalAddress(GV, Op.getDebugLoc(), Offset, DAG);
Evan Chengda43bcf2008-09-24 00:05:32 +00007556}
7557
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007558static SDValue
7559GetTLSADDR(SelectionDAG &DAG, SDValue Chain, GlobalAddressSDNode *GA,
Owen Andersone50ed302009-08-10 22:56:29 +00007560 SDValue *InFlag, const EVT PtrVT, unsigned ReturnReg,
Hans Wennborgf0234fc2012-06-01 16:27:21 +00007561 unsigned char OperandFlags, bool LocalDynamic = false) {
Anton Korobeynikov817a4642009-12-11 19:39:55 +00007562 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00007563 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007564 DebugLoc dl = GA->getDebugLoc();
Devang Patel0d881da2010-07-06 22:08:15 +00007565 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl,
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007566 GA->getValueType(0),
Chris Lattnerb903bed2009-06-26 21:20:29 +00007567 GA->getOffset(),
7568 OperandFlags);
Hans Wennborgf0234fc2012-06-01 16:27:21 +00007569
7570 X86ISD::NodeType CallType = LocalDynamic ? X86ISD::TLSBASEADDR
7571 : X86ISD::TLSADDR;
7572
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007573 if (InFlag) {
7574 SDValue Ops[] = { Chain, TGA, *InFlag };
Hans Wennborgf0234fc2012-06-01 16:27:21 +00007575 Chain = DAG.getNode(CallType, dl, NodeTys, Ops, 3);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007576 } else {
7577 SDValue Ops[] = { Chain, TGA };
Hans Wennborgf0234fc2012-06-01 16:27:21 +00007578 Chain = DAG.getNode(CallType, dl, NodeTys, Ops, 2);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007579 }
Anton Korobeynikov817a4642009-12-11 19:39:55 +00007580
7581 // TLSADDR will be codegen'ed as call. Inform MFI that function has calls.
Bill Wendlingb92187a2010-05-14 21:14:32 +00007582 MFI->setAdjustsStack(true);
Anton Korobeynikov817a4642009-12-11 19:39:55 +00007583
Rafael Espindola15f1b662009-04-24 12:59:40 +00007584 SDValue Flag = Chain.getValue(1);
7585 return DAG.getCopyFromReg(Chain, dl, ReturnReg, PtrVT, Flag);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007586}
7587
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007588// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 32 bit
Dan Gohman475871a2008-07-27 21:46:04 +00007589static SDValue
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007590LowerToTLSGeneralDynamicModel32(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00007591 const EVT PtrVT) {
Dan Gohman475871a2008-07-27 21:46:04 +00007592 SDValue InFlag;
Dale Johannesendd64c412009-02-04 00:33:20 +00007593 DebugLoc dl = GA->getDebugLoc(); // ? function entry point might be better
7594 SDValue Chain = DAG.getCopyToReg(DAG.getEntryNode(), dl, X86::EBX,
Craig Topper7c022842012-09-12 06:20:41 +00007595 DAG.getNode(X86ISD::GlobalBaseReg,
7596 DebugLoc(), PtrVT), InFlag);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007597 InFlag = Chain.getValue(1);
7598
Chris Lattnerb903bed2009-06-26 21:20:29 +00007599 return GetTLSADDR(DAG, Chain, GA, &InFlag, PtrVT, X86::EAX, X86II::MO_TLSGD);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007600}
7601
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007602// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 64 bit
Dan Gohman475871a2008-07-27 21:46:04 +00007603static SDValue
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007604LowerToTLSGeneralDynamicModel64(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00007605 const EVT PtrVT) {
Chris Lattnerb903bed2009-06-26 21:20:29 +00007606 return GetTLSADDR(DAG, DAG.getEntryNode(), GA, NULL, PtrVT,
7607 X86::RAX, X86II::MO_TLSGD);
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007608}
7609
Hans Wennborgf0234fc2012-06-01 16:27:21 +00007610static SDValue LowerToTLSLocalDynamicModel(GlobalAddressSDNode *GA,
7611 SelectionDAG &DAG,
7612 const EVT PtrVT,
7613 bool is64Bit) {
7614 DebugLoc dl = GA->getDebugLoc();
7615
7616 // Get the start address of the TLS block for this module.
7617 X86MachineFunctionInfo* MFI = DAG.getMachineFunction()
7618 .getInfo<X86MachineFunctionInfo>();
7619 MFI->incNumLocalDynamicTLSAccesses();
7620
7621 SDValue Base;
7622 if (is64Bit) {
7623 Base = GetTLSADDR(DAG, DAG.getEntryNode(), GA, NULL, PtrVT, X86::RAX,
7624 X86II::MO_TLSLD, /*LocalDynamic=*/true);
7625 } else {
7626 SDValue InFlag;
7627 SDValue Chain = DAG.getCopyToReg(DAG.getEntryNode(), dl, X86::EBX,
7628 DAG.getNode(X86ISD::GlobalBaseReg, DebugLoc(), PtrVT), InFlag);
7629 InFlag = Chain.getValue(1);
7630 Base = GetTLSADDR(DAG, Chain, GA, &InFlag, PtrVT, X86::EAX,
7631 X86II::MO_TLSLDM, /*LocalDynamic=*/true);
7632 }
7633
7634 // Note: the CleanupLocalDynamicTLSPass will remove redundant computations
7635 // of Base.
7636
7637 // Build x@dtpoff.
7638 unsigned char OperandFlags = X86II::MO_DTPOFF;
7639 unsigned WrapperKind = X86ISD::Wrapper;
7640 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl,
7641 GA->getValueType(0),
7642 GA->getOffset(), OperandFlags);
7643 SDValue Offset = DAG.getNode(WrapperKind, dl, PtrVT, TGA);
7644
7645 // Add x@dtpoff with the base.
7646 return DAG.getNode(ISD::ADD, dl, PtrVT, Offset, Base);
7647}
7648
Hans Wennborg228756c2012-05-11 10:11:01 +00007649// Lower ISD::GlobalTLSAddress using the "initial exec" or "local exec" model.
Dan Gohman475871a2008-07-27 21:46:04 +00007650static SDValue LowerToTLSExecModel(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00007651 const EVT PtrVT, TLSModel::Model model,
Hans Wennborg228756c2012-05-11 10:11:01 +00007652 bool is64Bit, bool isPIC) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00007653 DebugLoc dl = GA->getDebugLoc();
Michael J. Spencerec38de22010-10-10 22:04:20 +00007654
Chris Lattnerf93b90c2010-09-22 04:39:11 +00007655 // Get the Thread Pointer, which is %gs:0 (32-bit) or %fs:0 (64-bit).
7656 Value *Ptr = Constant::getNullValue(Type::getInt8PtrTy(*DAG.getContext(),
7657 is64Bit ? 257 : 256));
Rafael Espindola094fad32009-04-08 21:14:34 +00007658
Michael J. Spencerec38de22010-10-10 22:04:20 +00007659 SDValue ThreadPointer = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(),
Chris Lattnerf93b90c2010-09-22 04:39:11 +00007660 DAG.getIntPtrConstant(0),
Pete Cooperd752e0f2011-11-08 18:42:53 +00007661 MachinePointerInfo(Ptr),
7662 false, false, false, 0);
Rafael Espindola094fad32009-04-08 21:14:34 +00007663
Chris Lattnerb903bed2009-06-26 21:20:29 +00007664 unsigned char OperandFlags = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00007665 // Most TLS accesses are not RIP relative, even on x86-64. One exception is
7666 // initialexec.
7667 unsigned WrapperKind = X86ISD::Wrapper;
7668 if (model == TLSModel::LocalExec) {
Chris Lattnerb903bed2009-06-26 21:20:29 +00007669 OperandFlags = is64Bit ? X86II::MO_TPOFF : X86II::MO_NTPOFF;
Hans Wennborg228756c2012-05-11 10:11:01 +00007670 } else if (model == TLSModel::InitialExec) {
7671 if (is64Bit) {
7672 OperandFlags = X86II::MO_GOTTPOFF;
7673 WrapperKind = X86ISD::WrapperRIP;
7674 } else {
7675 OperandFlags = isPIC ? X86II::MO_GOTNTPOFF : X86II::MO_INDNTPOFF;
7676 }
Chris Lattner18c59872009-06-27 04:16:01 +00007677 } else {
Hans Wennborg228756c2012-05-11 10:11:01 +00007678 llvm_unreachable("Unexpected model");
Chris Lattnerb903bed2009-06-26 21:20:29 +00007679 }
Eric Christopherfd179292009-08-27 18:07:15 +00007680
Hans Wennborg228756c2012-05-11 10:11:01 +00007681 // emit "addl x@ntpoff,%eax" (local exec)
7682 // or "addl x@indntpoff,%eax" (initial exec)
7683 // or "addl x@gotntpoff(%ebx) ,%eax" (initial exec, 32-bit pic)
Michael J. Spencerec38de22010-10-10 22:04:20 +00007684 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl,
Devang Patel0d881da2010-07-06 22:08:15 +00007685 GA->getValueType(0),
Chris Lattnerb903bed2009-06-26 21:20:29 +00007686 GA->getOffset(), OperandFlags);
Chris Lattner18c59872009-06-27 04:16:01 +00007687 SDValue Offset = DAG.getNode(WrapperKind, dl, PtrVT, TGA);
Lauro Ramos Venancio7d2cc2b2007-04-22 22:50:52 +00007688
Hans Wennborg228756c2012-05-11 10:11:01 +00007689 if (model == TLSModel::InitialExec) {
7690 if (isPIC && !is64Bit) {
7691 Offset = DAG.getNode(ISD::ADD, dl, PtrVT,
7692 DAG.getNode(X86ISD::GlobalBaseReg, DebugLoc(), PtrVT),
7693 Offset);
Hans Wennborg228756c2012-05-11 10:11:01 +00007694 }
Rafael Espindola94e3b382012-06-29 04:22:35 +00007695
7696 Offset = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Offset,
7697 MachinePointerInfo::getGOT(), false, false, false,
7698 0);
Hans Wennborg228756c2012-05-11 10:11:01 +00007699 }
Lauro Ramos Venancio7d2cc2b2007-04-22 22:50:52 +00007700
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007701 // The address of the thread local variable is the add of the thread
7702 // pointer with the offset of the variable.
Dale Johannesen33c960f2009-02-04 20:06:27 +00007703 return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007704}
7705
Dan Gohman475871a2008-07-27 21:46:04 +00007706SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007707X86TargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const {
Michael J. Spencerec38de22010-10-10 22:04:20 +00007708
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007709 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
Chris Lattnerb903bed2009-06-26 21:20:29 +00007710 const GlobalValue *GV = GA->getGlobal();
Eric Christopherfd179292009-08-27 18:07:15 +00007711
Eric Christopher30ef0e52010-06-03 04:07:48 +00007712 if (Subtarget->isTargetELF()) {
Chandler Carruth34797132012-04-08 17:20:55 +00007713 TLSModel::Model model = getTargetMachine().getTLSModel(GV);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007714
Eric Christopher30ef0e52010-06-03 04:07:48 +00007715 switch (model) {
7716 case TLSModel::GeneralDynamic:
Eric Christopher30ef0e52010-06-03 04:07:48 +00007717 if (Subtarget->is64Bit())
7718 return LowerToTLSGeneralDynamicModel64(GA, DAG, getPointerTy());
7719 return LowerToTLSGeneralDynamicModel32(GA, DAG, getPointerTy());
Hans Wennborgf0234fc2012-06-01 16:27:21 +00007720 case TLSModel::LocalDynamic:
7721 return LowerToTLSLocalDynamicModel(GA, DAG, getPointerTy(),
7722 Subtarget->is64Bit());
Eric Christopher30ef0e52010-06-03 04:07:48 +00007723 case TLSModel::InitialExec:
7724 case TLSModel::LocalExec:
7725 return LowerToTLSExecModel(GA, DAG, getPointerTy(), model,
Hans Wennborg228756c2012-05-11 10:11:01 +00007726 Subtarget->is64Bit(),
7727 getTargetMachine().getRelocationModel() == Reloc::PIC_);
Eric Christopher30ef0e52010-06-03 04:07:48 +00007728 }
Craig Toppere8eb1162012-04-23 03:26:18 +00007729 llvm_unreachable("Unknown TLS model.");
7730 }
7731
7732 if (Subtarget->isTargetDarwin()) {
Eric Christopher30ef0e52010-06-03 04:07:48 +00007733 // Darwin only has one model of TLS. Lower to that.
7734 unsigned char OpFlag = 0;
7735 unsigned WrapperKind = Subtarget->isPICStyleRIPRel() ?
7736 X86ISD::WrapperRIP : X86ISD::Wrapper;
Michael J. Spencerec38de22010-10-10 22:04:20 +00007737
Eric Christopher30ef0e52010-06-03 04:07:48 +00007738 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
7739 // global base reg.
7740 bool PIC32 = (getTargetMachine().getRelocationModel() == Reloc::PIC_) &&
7741 !Subtarget->is64Bit();
7742 if (PIC32)
7743 OpFlag = X86II::MO_TLVP_PIC_BASE;
7744 else
7745 OpFlag = X86II::MO_TLVP;
Michael J. Spencerec38de22010-10-10 22:04:20 +00007746 DebugLoc DL = Op.getDebugLoc();
Devang Patel0d881da2010-07-06 22:08:15 +00007747 SDValue Result = DAG.getTargetGlobalAddress(GA->getGlobal(), DL,
Eric Christopherd8c05362010-12-09 06:25:53 +00007748 GA->getValueType(0),
Eric Christopher30ef0e52010-06-03 04:07:48 +00007749 GA->getOffset(), OpFlag);
Eric Christopher30ef0e52010-06-03 04:07:48 +00007750 SDValue Offset = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007751
Eric Christopher30ef0e52010-06-03 04:07:48 +00007752 // With PIC32, the address is actually $g + Offset.
7753 if (PIC32)
7754 Offset = DAG.getNode(ISD::ADD, DL, getPointerTy(),
7755 DAG.getNode(X86ISD::GlobalBaseReg,
7756 DebugLoc(), getPointerTy()),
7757 Offset);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007758
Eric Christopher30ef0e52010-06-03 04:07:48 +00007759 // Lowering the machine isd will make sure everything is in the right
7760 // location.
Eric Christopherd8c05362010-12-09 06:25:53 +00007761 SDValue Chain = DAG.getEntryNode();
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00007762 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Eric Christopherd8c05362010-12-09 06:25:53 +00007763 SDValue Args[] = { Chain, Offset };
7764 Chain = DAG.getNode(X86ISD::TLSCALL, DL, NodeTys, Args, 2);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007765
Eric Christopher30ef0e52010-06-03 04:07:48 +00007766 // TLSCALL will be codegen'ed as call. Inform MFI that function has calls.
7767 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
7768 MFI->setAdjustsStack(true);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00007769
Eric Christopher30ef0e52010-06-03 04:07:48 +00007770 // And our return value (tls address) is in the standard call return value
7771 // location.
Eric Christopherd8c05362010-12-09 06:25:53 +00007772 unsigned Reg = Subtarget->is64Bit() ? X86::RAX : X86::EAX;
Evan Chengfd230df2011-10-19 22:22:54 +00007773 return DAG.getCopyFromReg(Chain, DL, Reg, getPointerTy(),
7774 Chain.getValue(1));
Craig Toppere8eb1162012-04-23 03:26:18 +00007775 }
7776
7777 if (Subtarget->isTargetWindows()) {
Anton Korobeynikovd4a19b62012-02-11 17:26:53 +00007778 // Just use the implicit TLS architecture
7779 // Need to generate someting similar to:
7780 // mov rdx, qword [gs:abs 58H]; Load pointer to ThreadLocalStorage
7781 // ; from TEB
7782 // mov ecx, dword [rel _tls_index]: Load index (from C runtime)
7783 // mov rcx, qword [rdx+rcx*8]
7784 // mov eax, .tls$:tlsvar
7785 // [rax+rcx] contains the address
7786 // Windows 64bit: gs:0x58
7787 // Windows 32bit: fs:__tls_array
7788
7789 // If GV is an alias then use the aliasee for determining
7790 // thread-localness.
7791 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV))
7792 GV = GA->resolveAliasedGlobal(false);
7793 DebugLoc dl = GA->getDebugLoc();
7794 SDValue Chain = DAG.getEntryNode();
7795
7796 // Get the Thread Pointer, which is %fs:__tls_array (32-bit) or
7797 // %gs:0x58 (64-bit).
7798 Value *Ptr = Constant::getNullValue(Subtarget->is64Bit()
7799 ? Type::getInt8PtrTy(*DAG.getContext(),
7800 256)
7801 : Type::getInt32PtrTy(*DAG.getContext(),
7802 257));
7803
7804 SDValue ThreadPointer = DAG.getLoad(getPointerTy(), dl, Chain,
7805 Subtarget->is64Bit()
7806 ? DAG.getIntPtrConstant(0x58)
7807 : DAG.getExternalSymbol("_tls_array",
7808 getPointerTy()),
7809 MachinePointerInfo(Ptr),
7810 false, false, false, 0);
7811
7812 // Load the _tls_index variable
7813 SDValue IDX = DAG.getExternalSymbol("_tls_index", getPointerTy());
7814 if (Subtarget->is64Bit())
7815 IDX = DAG.getExtLoad(ISD::ZEXTLOAD, dl, getPointerTy(), Chain,
7816 IDX, MachinePointerInfo(), MVT::i32,
7817 false, false, 0);
7818 else
7819 IDX = DAG.getLoad(getPointerTy(), dl, Chain, IDX, MachinePointerInfo(),
7820 false, false, false, 0);
7821
Chandler Carruth426c2bf2012-11-01 09:14:31 +00007822 SDValue Scale = DAG.getConstant(Log2_64_Ceil(TD->getPointerSize()),
Craig Topper0fbf3642012-04-23 03:28:34 +00007823 getPointerTy());
Anton Korobeynikovd4a19b62012-02-11 17:26:53 +00007824 IDX = DAG.getNode(ISD::SHL, dl, getPointerTy(), IDX, Scale);
7825
7826 SDValue res = DAG.getNode(ISD::ADD, dl, getPointerTy(), ThreadPointer, IDX);
7827 res = DAG.getLoad(getPointerTy(), dl, Chain, res, MachinePointerInfo(),
7828 false, false, false, 0);
7829
7830 // Get the offset of start of .tls section
7831 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl,
7832 GA->getValueType(0),
7833 GA->getOffset(), X86II::MO_SECREL);
7834 SDValue Offset = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), TGA);
7835
7836 // The address of the thread local variable is the add of the thread
7837 // pointer with the offset of the variable.
7838 return DAG.getNode(ISD::ADD, dl, getPointerTy(), res, Offset);
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007839 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00007840
David Blaikie4d6ccb52012-01-20 21:51:11 +00007841 llvm_unreachable("TLS not implemented for this target.");
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007842}
7843
Chad Rosierb90d2a92012-01-03 23:19:12 +00007844/// LowerShiftParts - Lower SRA_PARTS and friends, which return two i32 values
7845/// and take a 2 x i32 value to shift plus a shift amount.
7846SDValue X86TargetLowering::LowerShiftParts(SDValue Op, SelectionDAG &DAG) const{
Dan Gohman4c1fa612008-03-03 22:22:09 +00007847 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
Owen Andersone50ed302009-08-10 22:56:29 +00007848 EVT VT = Op.getValueType();
Duncan Sands83ec4b62008-06-06 12:08:01 +00007849 unsigned VTBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007850 DebugLoc dl = Op.getDebugLoc();
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007851 bool isSRA = Op.getOpcode() == ISD::SRA_PARTS;
Dan Gohman475871a2008-07-27 21:46:04 +00007852 SDValue ShOpLo = Op.getOperand(0);
7853 SDValue ShOpHi = Op.getOperand(1);
7854 SDValue ShAmt = Op.getOperand(2);
Chris Lattner31dcfe62009-07-29 05:48:09 +00007855 SDValue Tmp1 = isSRA ? DAG.getNode(ISD::SRA, dl, VT, ShOpHi,
Owen Anderson825b72b2009-08-11 20:47:22 +00007856 DAG.getConstant(VTBits - 1, MVT::i8))
Chris Lattner31dcfe62009-07-29 05:48:09 +00007857 : DAG.getConstant(0, VT);
Evan Chenge3413162006-01-09 18:33:28 +00007858
Dan Gohman475871a2008-07-27 21:46:04 +00007859 SDValue Tmp2, Tmp3;
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007860 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dale Johannesenace16102009-02-03 19:33:06 +00007861 Tmp2 = DAG.getNode(X86ISD::SHLD, dl, VT, ShOpHi, ShOpLo, ShAmt);
7862 Tmp3 = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ShAmt);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007863 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00007864 Tmp2 = DAG.getNode(X86ISD::SHRD, dl, VT, ShOpLo, ShOpHi, ShAmt);
7865 Tmp3 = DAG.getNode(isSRA ? ISD::SRA : ISD::SRL, dl, VT, ShOpHi, ShAmt);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007866 }
Evan Chenge3413162006-01-09 18:33:28 +00007867
Owen Anderson825b72b2009-08-11 20:47:22 +00007868 SDValue AndNode = DAG.getNode(ISD::AND, dl, MVT::i8, ShAmt,
7869 DAG.getConstant(VTBits, MVT::i8));
Chris Lattnerccfea352010-02-22 00:28:59 +00007870 SDValue Cond = DAG.getNode(X86ISD::CMP, dl, MVT::i32,
Owen Anderson825b72b2009-08-11 20:47:22 +00007871 AndNode, DAG.getConstant(0, MVT::i8));
Evan Chenge3413162006-01-09 18:33:28 +00007872
Dan Gohman475871a2008-07-27 21:46:04 +00007873 SDValue Hi, Lo;
Owen Anderson825b72b2009-08-11 20:47:22 +00007874 SDValue CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Dan Gohman475871a2008-07-27 21:46:04 +00007875 SDValue Ops0[4] = { Tmp2, Tmp3, CC, Cond };
7876 SDValue Ops1[4] = { Tmp3, Tmp1, CC, Cond };
Duncan Sandsf9516202008-06-30 10:19:09 +00007877
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007878 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dale Johannesenace16102009-02-03 19:33:06 +00007879 Hi = DAG.getNode(X86ISD::CMOV, dl, VT, Ops0, 4);
7880 Lo = DAG.getNode(X86ISD::CMOV, dl, VT, Ops1, 4);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007881 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00007882 Lo = DAG.getNode(X86ISD::CMOV, dl, VT, Ops0, 4);
7883 Hi = DAG.getNode(X86ISD::CMOV, dl, VT, Ops1, 4);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007884 }
7885
Dan Gohman475871a2008-07-27 21:46:04 +00007886 SDValue Ops[2] = { Lo, Hi };
Dale Johannesenace16102009-02-03 19:33:06 +00007887 return DAG.getMergeValues(Ops, 2, dl);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007888}
Evan Chenga3195e82006-01-12 22:54:21 +00007889
Dan Gohmand858e902010-04-17 15:26:15 +00007890SDValue X86TargetLowering::LowerSINT_TO_FP(SDValue Op,
7891 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00007892 EVT SrcVT = Op.getOperand(0).getValueType();
Eli Friedman23ef1052009-06-06 03:57:58 +00007893
Dale Johannesen0488fb62010-09-30 23:57:10 +00007894 if (SrcVT.isVector())
Eli Friedman23ef1052009-06-06 03:57:58 +00007895 return SDValue();
Eli Friedman23ef1052009-06-06 03:57:58 +00007896
Owen Anderson825b72b2009-08-11 20:47:22 +00007897 assert(SrcVT.getSimpleVT() <= MVT::i64 && SrcVT.getSimpleVT() >= MVT::i16 &&
Chris Lattnerb09916b2008-02-27 05:57:41 +00007898 "Unknown SINT_TO_FP to lower!");
Scott Michelfdc40a02009-02-17 22:15:04 +00007899
Eli Friedman36df4992009-05-27 00:47:34 +00007900 // These are really Legal; return the operand so the caller accepts it as
7901 // Legal.
Owen Anderson825b72b2009-08-11 20:47:22 +00007902 if (SrcVT == MVT::i32 && isScalarFPTypeInSSEReg(Op.getValueType()))
Eli Friedman36df4992009-05-27 00:47:34 +00007903 return Op;
Owen Anderson825b72b2009-08-11 20:47:22 +00007904 if (SrcVT == MVT::i64 && isScalarFPTypeInSSEReg(Op.getValueType()) &&
Eli Friedman36df4992009-05-27 00:47:34 +00007905 Subtarget->is64Bit()) {
7906 return Op;
7907 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007908
Bruno Cardoso Lopesa511b8e2011-08-09 17:39:01 +00007909 DebugLoc dl = Op.getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +00007910 unsigned Size = SrcVT.getSizeInBits()/8;
Evan Cheng0db9fe62006-04-25 20:13:52 +00007911 MachineFunction &MF = DAG.getMachineFunction();
David Greene3f2bf852009-11-12 20:49:22 +00007912 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size, false);
Dan Gohman475871a2008-07-27 21:46:04 +00007913 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Dale Johannesenace16102009-02-03 19:33:06 +00007914 SDValue Chain = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Bill Wendling105be5a2009-03-13 08:41:47 +00007915 StackSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00007916 MachinePointerInfo::getFixedStack(SSFI),
David Greene67c9d422010-02-15 16:53:33 +00007917 false, false, 0);
Eli Friedman948e95a2009-05-23 09:59:16 +00007918 return BuildFILD(Op, SrcVT, Chain, StackSlot, DAG);
7919}
Evan Cheng0db9fe62006-04-25 20:13:52 +00007920
Owen Andersone50ed302009-08-10 22:56:29 +00007921SDValue X86TargetLowering::BuildFILD(SDValue Op, EVT SrcVT, SDValue Chain,
Michael J. Spencerec38de22010-10-10 22:04:20 +00007922 SDValue StackSlot,
Dan Gohmand858e902010-04-17 15:26:15 +00007923 SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00007924 // Build the FILD
Chris Lattner492a43e2010-09-22 01:28:21 +00007925 DebugLoc DL = Op.getDebugLoc();
Chris Lattner5a88b832007-02-25 07:10:00 +00007926 SDVTList Tys;
Chris Lattner78631162008-01-16 06:24:21 +00007927 bool useSSE = isScalarFPTypeInSSEReg(Op.getValueType());
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00007928 if (useSSE)
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00007929 Tys = DAG.getVTList(MVT::f64, MVT::Other, MVT::Glue);
Chris Lattner5a88b832007-02-25 07:10:00 +00007930 else
Owen Anderson825b72b2009-08-11 20:47:22 +00007931 Tys = DAG.getVTList(Op.getValueType(), MVT::Other);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007932
Chris Lattner492a43e2010-09-22 01:28:21 +00007933 unsigned ByteSize = SrcVT.getSizeInBits()/8;
Michael J. Spencerec38de22010-10-10 22:04:20 +00007934
Stuart Hastings84be9582011-06-02 15:57:11 +00007935 FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(StackSlot);
7936 MachineMemOperand *MMO;
7937 if (FI) {
7938 int SSFI = FI->getIndex();
7939 MMO =
7940 DAG.getMachineFunction()
7941 .getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
7942 MachineMemOperand::MOLoad, ByteSize, ByteSize);
7943 } else {
7944 MMO = cast<LoadSDNode>(StackSlot)->getMemOperand();
7945 StackSlot = StackSlot.getOperand(1);
7946 }
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00007947 SDValue Ops[] = { Chain, StackSlot, DAG.getValueType(SrcVT) };
Chris Lattner492a43e2010-09-22 01:28:21 +00007948 SDValue Result = DAG.getMemIntrinsicNode(useSSE ? X86ISD::FILD_FLAG :
7949 X86ISD::FILD, DL,
7950 Tys, Ops, array_lengthof(Ops),
7951 SrcVT, MMO);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007952
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00007953 if (useSSE) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00007954 Chain = Result.getValue(1);
Dan Gohman475871a2008-07-27 21:46:04 +00007955 SDValue InFlag = Result.getValue(2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007956
7957 // FIXME: Currently the FST is flagged to the FILD_FLAG. This
7958 // shouldn't be necessary except that RFP cannot be live across
7959 // multiple blocks. When stackifier is fixed, they can be uncoupled.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00007960 MachineFunction &MF = DAG.getMachineFunction();
Bob Wilsoneafca4e2010-09-22 17:35:14 +00007961 unsigned SSFISize = Op.getValueType().getSizeInBits()/8;
7962 int SSFI = MF.getFrameInfo()->CreateStackObject(SSFISize, SSFISize, false);
Dan Gohman475871a2008-07-27 21:46:04 +00007963 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Owen Anderson825b72b2009-08-11 20:47:22 +00007964 Tys = DAG.getVTList(MVT::Other);
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00007965 SDValue Ops[] = {
7966 Chain, Result, StackSlot, DAG.getValueType(Op.getValueType()), InFlag
7967 };
Chris Lattner492a43e2010-09-22 01:28:21 +00007968 MachineMemOperand *MMO =
7969 DAG.getMachineFunction()
7970 .getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
Bob Wilsoneafca4e2010-09-22 17:35:14 +00007971 MachineMemOperand::MOStore, SSFISize, SSFISize);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007972
Chris Lattner492a43e2010-09-22 01:28:21 +00007973 Chain = DAG.getMemIntrinsicNode(X86ISD::FST, DL, Tys,
7974 Ops, array_lengthof(Ops),
7975 Op.getValueType(), MMO);
7976 Result = DAG.getLoad(Op.getValueType(), DL, Chain, StackSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00007977 MachinePointerInfo::getFixedStack(SSFI),
Pete Cooperd752e0f2011-11-08 18:42:53 +00007978 false, false, false, 0);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00007979 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00007980
Evan Cheng0db9fe62006-04-25 20:13:52 +00007981 return Result;
7982}
7983
Bill Wendling8b8a6362009-01-17 03:56:04 +00007984// LowerUINT_TO_FP_i64 - 64-bit unsigned integer to double expansion.
Dan Gohmand858e902010-04-17 15:26:15 +00007985SDValue X86TargetLowering::LowerUINT_TO_FP_i64(SDValue Op,
7986 SelectionDAG &DAG) const {
Bill Wendling397ae212012-01-05 02:13:20 +00007987 // This algorithm is not obvious. Here it is what we're trying to output:
Bill Wendling8b8a6362009-01-17 03:56:04 +00007988 /*
Bill Wendling397ae212012-01-05 02:13:20 +00007989 movq %rax, %xmm0
7990 punpckldq (c0), %xmm0 // c0: (uint4){ 0x43300000U, 0x45300000U, 0U, 0U }
7991 subpd (c1), %xmm0 // c1: (double2){ 0x1.0p52, 0x1.0p52 * 0x1.0p32 }
7992 #ifdef __SSE3__
Chad Rosiera20e1e72012-08-01 18:39:17 +00007993 haddpd %xmm0, %xmm0
Bill Wendling397ae212012-01-05 02:13:20 +00007994 #else
Chad Rosiera20e1e72012-08-01 18:39:17 +00007995 pshufd $0x4e, %xmm0, %xmm1
Bill Wendling397ae212012-01-05 02:13:20 +00007996 addpd %xmm1, %xmm0
7997 #endif
Bill Wendling8b8a6362009-01-17 03:56:04 +00007998 */
Dale Johannesen040225f2008-10-21 23:07:49 +00007999
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008000 DebugLoc dl = Op.getDebugLoc();
Owen Andersona90b3dc2009-07-15 21:51:10 +00008001 LLVMContext *Context = DAG.getContext();
Dale Johannesenace16102009-02-03 19:33:06 +00008002
Dale Johannesen1c15bf52008-10-21 20:50:01 +00008003 // Build some magic constants.
Chris Lattner7302d802012-02-06 21:56:39 +00008004 const uint32_t CV0[] = { 0x43300000, 0x45300000, 0, 0 };
8005 Constant *C0 = ConstantDataVector::get(*Context, CV0);
Evan Cheng1606e8e2009-03-13 07:51:59 +00008006 SDValue CPIdx0 = DAG.getConstantPool(C0, getPointerTy(), 16);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00008007
Chris Lattner97484792012-01-25 09:56:22 +00008008 SmallVector<Constant*,2> CV1;
8009 CV1.push_back(
Chris Lattner4ca829e2012-01-25 06:02:56 +00008010 ConstantFP::get(*Context, APFloat(APInt(64, 0x4330000000000000ULL))));
Chris Lattner97484792012-01-25 09:56:22 +00008011 CV1.push_back(
8012 ConstantFP::get(*Context, APFloat(APInt(64, 0x4530000000000000ULL))));
8013 Constant *C1 = ConstantVector::get(CV1);
Evan Cheng1606e8e2009-03-13 07:51:59 +00008014 SDValue CPIdx1 = DAG.getConstantPool(C1, getPointerTy(), 16);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00008015
Bill Wendling397ae212012-01-05 02:13:20 +00008016 // Load the 64-bit value into an XMM register.
8017 SDValue XR1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i64,
8018 Op.getOperand(0));
Owen Anderson825b72b2009-08-11 20:47:22 +00008019 SDValue CLod0 = DAG.getLoad(MVT::v4i32, dl, DAG.getEntryNode(), CPIdx0,
Chris Lattnere8639032010-09-21 06:22:23 +00008020 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00008021 false, false, false, 16);
Bill Wendling397ae212012-01-05 02:13:20 +00008022 SDValue Unpck1 = getUnpackl(DAG, dl, MVT::v4i32,
8023 DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, XR1),
8024 CLod0);
8025
Owen Anderson825b72b2009-08-11 20:47:22 +00008026 SDValue CLod1 = DAG.getLoad(MVT::v2f64, dl, CLod0.getValue(1), CPIdx1,
Chris Lattnere8639032010-09-21 06:22:23 +00008027 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00008028 false, false, false, 16);
Bill Wendling397ae212012-01-05 02:13:20 +00008029 SDValue XR2F = DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Unpck1);
Owen Anderson825b72b2009-08-11 20:47:22 +00008030 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::v2f64, XR2F, CLod1);
Bill Wendling397ae212012-01-05 02:13:20 +00008031 SDValue Result;
Bill Wendling8b8a6362009-01-17 03:56:04 +00008032
Craig Topperd0a31172012-01-10 06:37:29 +00008033 if (Subtarget->hasSSE3()) {
Bill Wendling397ae212012-01-05 02:13:20 +00008034 // FIXME: The 'haddpd' instruction may be slower than 'movhlps + addsd'.
8035 Result = DAG.getNode(X86ISD::FHADD, dl, MVT::v2f64, Sub, Sub);
8036 } else {
8037 SDValue S2F = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, Sub);
8038 SDValue Shuffle = getTargetShuffleNode(X86ISD::PSHUFD, dl, MVT::v4i32,
8039 S2F, 0x4E, DAG);
8040 Result = DAG.getNode(ISD::FADD, dl, MVT::v2f64,
8041 DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Shuffle),
8042 Sub);
8043 }
8044
8045 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Result,
Dale Johannesen1c15bf52008-10-21 20:50:01 +00008046 DAG.getIntPtrConstant(0));
8047}
8048
Bill Wendling8b8a6362009-01-17 03:56:04 +00008049// LowerUINT_TO_FP_i32 - 32-bit unsigned integer to float expansion.
Dan Gohmand858e902010-04-17 15:26:15 +00008050SDValue X86TargetLowering::LowerUINT_TO_FP_i32(SDValue Op,
8051 SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008052 DebugLoc dl = Op.getDebugLoc();
Bill Wendling8b8a6362009-01-17 03:56:04 +00008053 // FP constant to bias correct the final result.
8054 SDValue Bias = DAG.getConstantFP(BitsToDouble(0x4330000000000000ULL),
Owen Anderson825b72b2009-08-11 20:47:22 +00008055 MVT::f64);
Bill Wendling8b8a6362009-01-17 03:56:04 +00008056
8057 // Load the 32-bit value into an XMM register.
Owen Anderson825b72b2009-08-11 20:47:22 +00008058 SDValue Load = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
Eli Friedman6cdc1f42011-08-02 18:38:35 +00008059 Op.getOperand(0));
Bill Wendling8b8a6362009-01-17 03:56:04 +00008060
Eli Friedmanf3704762011-08-29 21:15:46 +00008061 // Zero out the upper parts of the register.
Craig Topper12216172012-01-13 08:12:35 +00008062 Load = getShuffleVectorZeroOrUndef(Load, 0, true, Subtarget, DAG);
Eli Friedmanf3704762011-08-29 21:15:46 +00008063
Owen Anderson825b72b2009-08-11 20:47:22 +00008064 Load = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008065 DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Load),
Bill Wendling8b8a6362009-01-17 03:56:04 +00008066 DAG.getIntPtrConstant(0));
8067
8068 // Or the load with the bias.
Owen Anderson825b72b2009-08-11 20:47:22 +00008069 SDValue Or = DAG.getNode(ISD::OR, dl, MVT::v2i64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008070 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00008071 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00008072 MVT::v2f64, Load)),
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008073 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00008074 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00008075 MVT::v2f64, Bias)));
8076 Or = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008077 DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Or),
Bill Wendling8b8a6362009-01-17 03:56:04 +00008078 DAG.getIntPtrConstant(0));
8079
8080 // Subtract the bias.
Owen Anderson825b72b2009-08-11 20:47:22 +00008081 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::f64, Or, Bias);
Bill Wendling8b8a6362009-01-17 03:56:04 +00008082
8083 // Handle final rounding.
Owen Andersone50ed302009-08-10 22:56:29 +00008084 EVT DestVT = Op.getValueType();
Bill Wendling030939c2009-01-17 07:40:19 +00008085
Craig Topper69947b92012-04-23 06:57:04 +00008086 if (DestVT.bitsLT(MVT::f64))
Dale Johannesenace16102009-02-03 19:33:06 +00008087 return DAG.getNode(ISD::FP_ROUND, dl, DestVT, Sub,
Bill Wendling030939c2009-01-17 07:40:19 +00008088 DAG.getIntPtrConstant(0));
Craig Topper69947b92012-04-23 06:57:04 +00008089 if (DestVT.bitsGT(MVT::f64))
Dale Johannesenace16102009-02-03 19:33:06 +00008090 return DAG.getNode(ISD::FP_EXTEND, dl, DestVT, Sub);
Bill Wendling030939c2009-01-17 07:40:19 +00008091
8092 // Handle final rounding.
8093 return Sub;
Bill Wendling8b8a6362009-01-17 03:56:04 +00008094}
8095
Michael Liaoa7554632012-10-23 17:36:08 +00008096SDValue X86TargetLowering::lowerUINT_TO_FP_vec(SDValue Op,
8097 SelectionDAG &DAG) const {
8098 SDValue N0 = Op.getOperand(0);
8099 EVT SVT = N0.getValueType();
8100 DebugLoc dl = Op.getDebugLoc();
8101
8102 assert((SVT == MVT::v4i8 || SVT == MVT::v4i16 ||
8103 SVT == MVT::v8i8 || SVT == MVT::v8i16) &&
8104 "Custom UINT_TO_FP is not supported!");
8105
8106 EVT NVT = EVT::getVectorVT(*DAG.getContext(), MVT::i32, SVT.getVectorNumElements());
8107 return DAG.getNode(ISD::SINT_TO_FP, dl, Op.getValueType(),
8108 DAG.getNode(ISD::ZERO_EXTEND, dl, NVT, N0));
8109}
8110
Dan Gohmand858e902010-04-17 15:26:15 +00008111SDValue X86TargetLowering::LowerUINT_TO_FP(SDValue Op,
8112 SelectionDAG &DAG) const {
Evan Chenga06ec9e2009-01-19 08:08:22 +00008113 SDValue N0 = Op.getOperand(0);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008114 DebugLoc dl = Op.getDebugLoc();
Bill Wendling8b8a6362009-01-17 03:56:04 +00008115
Michael Liaoa7554632012-10-23 17:36:08 +00008116 if (Op.getValueType().isVector())
8117 return lowerUINT_TO_FP_vec(Op, DAG);
8118
Dale Johannesen8d908eb2010-05-15 18:51:12 +00008119 // Since UINT_TO_FP is legal (it's marked custom), dag combiner won't
Evan Chenga06ec9e2009-01-19 08:08:22 +00008120 // optimize it to a SINT_TO_FP when the sign bit is known zero. Perform
8121 // the optimization here.
8122 if (DAG.SignBitIsZero(N0))
Dale Johannesenace16102009-02-03 19:33:06 +00008123 return DAG.getNode(ISD::SINT_TO_FP, dl, Op.getValueType(), N0);
Evan Chenga06ec9e2009-01-19 08:08:22 +00008124
Owen Andersone50ed302009-08-10 22:56:29 +00008125 EVT SrcVT = N0.getValueType();
Dale Johannesen8d908eb2010-05-15 18:51:12 +00008126 EVT DstVT = Op.getValueType();
8127 if (SrcVT == MVT::i64 && DstVT == MVT::f64 && X86ScalarSSEf64)
Bill Wendling8b8a6362009-01-17 03:56:04 +00008128 return LowerUINT_TO_FP_i64(Op, DAG);
Craig Topper69947b92012-04-23 06:57:04 +00008129 if (SrcVT == MVT::i32 && X86ScalarSSEf64)
Bill Wendling8b8a6362009-01-17 03:56:04 +00008130 return LowerUINT_TO_FP_i32(Op, DAG);
Craig Topper69947b92012-04-23 06:57:04 +00008131 if (Subtarget->is64Bit() && SrcVT == MVT::i64 && DstVT == MVT::f32)
Bill Wendling397ae212012-01-05 02:13:20 +00008132 return SDValue();
Eli Friedman948e95a2009-05-23 09:59:16 +00008133
8134 // Make a 64-bit buffer, and use it to build an FILD.
Owen Anderson825b72b2009-08-11 20:47:22 +00008135 SDValue StackSlot = DAG.CreateStackTemporary(MVT::i64);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00008136 if (SrcVT == MVT::i32) {
8137 SDValue WordOff = DAG.getConstant(4, getPointerTy());
8138 SDValue OffsetSlot = DAG.getNode(ISD::ADD, dl,
8139 getPointerTy(), StackSlot, WordOff);
8140 SDValue Store1 = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Chris Lattner8026a9d2010-09-21 17:50:43 +00008141 StackSlot, MachinePointerInfo(),
8142 false, false, 0);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00008143 SDValue Store2 = DAG.getStore(Store1, dl, DAG.getConstant(0, MVT::i32),
Chris Lattner8026a9d2010-09-21 17:50:43 +00008144 OffsetSlot, MachinePointerInfo(),
8145 false, false, 0);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00008146 SDValue Fild = BuildFILD(Op, MVT::i64, Store2, StackSlot, DAG);
8147 return Fild;
8148 }
8149
8150 assert(SrcVT == MVT::i64 && "Unexpected type in UINT_TO_FP");
8151 SDValue Store = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Bill Wendlingf6c07472012-01-10 19:41:30 +00008152 StackSlot, MachinePointerInfo(),
Chris Lattner8026a9d2010-09-21 17:50:43 +00008153 false, false, 0);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00008154 // For i64 source, we need to add the appropriate power of 2 if the input
8155 // was negative. This is the same as the optimization in
8156 // DAGTypeLegalizer::ExpandIntOp_UNIT_TO_FP, and for it to be safe here,
8157 // we must be careful to do the computation in x87 extended precision, not
8158 // in SSE. (The generic code can't know it's OK to do this, or how to.)
Chris Lattner492a43e2010-09-22 01:28:21 +00008159 int SSFI = cast<FrameIndexSDNode>(StackSlot)->getIndex();
8160 MachineMemOperand *MMO =
8161 DAG.getMachineFunction()
8162 .getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
8163 MachineMemOperand::MOLoad, 8, 8);
Michael J. Spencerec38de22010-10-10 22:04:20 +00008164
Dale Johannesen8d908eb2010-05-15 18:51:12 +00008165 SDVTList Tys = DAG.getVTList(MVT::f80, MVT::Other);
8166 SDValue Ops[] = { Store, StackSlot, DAG.getValueType(MVT::i64) };
Chris Lattner492a43e2010-09-22 01:28:21 +00008167 SDValue Fild = DAG.getMemIntrinsicNode(X86ISD::FILD, dl, Tys, Ops, 3,
8168 MVT::i64, MMO);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00008169
8170 APInt FF(32, 0x5F800000ULL);
8171
8172 // Check whether the sign bit is set.
8173 SDValue SignSet = DAG.getSetCC(dl, getSetCCResultType(MVT::i64),
8174 Op.getOperand(0), DAG.getConstant(0, MVT::i64),
8175 ISD::SETLT);
8176
8177 // Build a 64 bit pair (0, FF) in the constant pool, with FF in the lo bits.
8178 SDValue FudgePtr = DAG.getConstantPool(
8179 ConstantInt::get(*DAG.getContext(), FF.zext(64)),
8180 getPointerTy());
8181
8182 // Get a pointer to FF if the sign bit was set, or to 0 otherwise.
8183 SDValue Zero = DAG.getIntPtrConstant(0);
8184 SDValue Four = DAG.getIntPtrConstant(4);
8185 SDValue Offset = DAG.getNode(ISD::SELECT, dl, Zero.getValueType(), SignSet,
8186 Zero, Four);
8187 FudgePtr = DAG.getNode(ISD::ADD, dl, getPointerTy(), FudgePtr, Offset);
8188
8189 // Load the value out, extending it from f32 to f80.
8190 // FIXME: Avoid the extend by constructing the right constant pool?
Stuart Hastingsa9011292011-02-16 16:23:55 +00008191 SDValue Fudge = DAG.getExtLoad(ISD::EXTLOAD, dl, MVT::f80, DAG.getEntryNode(),
Chris Lattnere8639032010-09-21 06:22:23 +00008192 FudgePtr, MachinePointerInfo::getConstantPool(),
8193 MVT::f32, false, false, 4);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00008194 // Extend everything to 80 bits to force it to be done on x87.
8195 SDValue Add = DAG.getNode(ISD::FADD, dl, MVT::f80, Fild, Fudge);
8196 return DAG.getNode(ISD::FP_ROUND, dl, DstVT, Add, DAG.getIntPtrConstant(0));
Bill Wendling8b8a6362009-01-17 03:56:04 +00008197}
8198
Dan Gohman475871a2008-07-27 21:46:04 +00008199std::pair<SDValue,SDValue> X86TargetLowering::
NAKAMURA Takumi9a68fdc2012-02-25 03:37:25 +00008200FP_TO_INTHelper(SDValue Op, SelectionDAG &DAG, bool IsSigned, bool IsReplace) const {
Chris Lattner07290932010-09-22 01:05:16 +00008201 DebugLoc DL = Op.getDebugLoc();
Eli Friedman948e95a2009-05-23 09:59:16 +00008202
Owen Andersone50ed302009-08-10 22:56:29 +00008203 EVT DstTy = Op.getValueType();
Eli Friedman948e95a2009-05-23 09:59:16 +00008204
Michael J. Spencer1a2d0612012-02-24 19:01:22 +00008205 if (!IsSigned && !isIntegerTypeFTOL(DstTy)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00008206 assert(DstTy == MVT::i32 && "Unexpected FP_TO_UINT");
8207 DstTy = MVT::i64;
Eli Friedman948e95a2009-05-23 09:59:16 +00008208 }
8209
Owen Anderson825b72b2009-08-11 20:47:22 +00008210 assert(DstTy.getSimpleVT() <= MVT::i64 &&
8211 DstTy.getSimpleVT() >= MVT::i16 &&
Michael J. Spencer1a2d0612012-02-24 19:01:22 +00008212 "Unknown FP_TO_INT to lower!");
Evan Cheng0db9fe62006-04-25 20:13:52 +00008213
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00008214 // These are really Legal.
Owen Anderson825b72b2009-08-11 20:47:22 +00008215 if (DstTy == MVT::i32 &&
Chris Lattner78631162008-01-16 06:24:21 +00008216 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Dan Gohman475871a2008-07-27 21:46:04 +00008217 return std::make_pair(SDValue(), SDValue());
Dale Johannesen73328d12007-09-19 23:55:34 +00008218 if (Subtarget->is64Bit() &&
Owen Anderson825b72b2009-08-11 20:47:22 +00008219 DstTy == MVT::i64 &&
Eli Friedman36df4992009-05-27 00:47:34 +00008220 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Dan Gohman475871a2008-07-27 21:46:04 +00008221 return std::make_pair(SDValue(), SDValue());
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00008222
Michael J. Spencer1a2d0612012-02-24 19:01:22 +00008223 // We lower FP->int64 either into FISTP64 followed by a load from a temporary
8224 // stack slot, or into the FTOL runtime function.
Evan Cheng87c89352007-10-15 20:11:21 +00008225 MachineFunction &MF = DAG.getMachineFunction();
Eli Friedman948e95a2009-05-23 09:59:16 +00008226 unsigned MemSize = DstTy.getSizeInBits()/8;
David Greene3f2bf852009-11-12 20:49:22 +00008227 int SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize, false);
Dan Gohman475871a2008-07-27 21:46:04 +00008228 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Eric Christopherfd179292009-08-27 18:07:15 +00008229
Evan Cheng0db9fe62006-04-25 20:13:52 +00008230 unsigned Opc;
Michael J. Spencer1a2d0612012-02-24 19:01:22 +00008231 if (!IsSigned && isIntegerTypeFTOL(DstTy))
8232 Opc = X86ISD::WIN_FTOL;
8233 else
8234 switch (DstTy.getSimpleVT().SimpleTy) {
8235 default: llvm_unreachable("Invalid FP_TO_SINT to lower!");
8236 case MVT::i16: Opc = X86ISD::FP_TO_INT16_IN_MEM; break;
8237 case MVT::i32: Opc = X86ISD::FP_TO_INT32_IN_MEM; break;
8238 case MVT::i64: Opc = X86ISD::FP_TO_INT64_IN_MEM; break;
8239 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00008240
Dan Gohman475871a2008-07-27 21:46:04 +00008241 SDValue Chain = DAG.getEntryNode();
8242 SDValue Value = Op.getOperand(0);
Chris Lattner492a43e2010-09-22 01:28:21 +00008243 EVT TheVT = Op.getOperand(0).getValueType();
Michael J. Spencer1a2d0612012-02-24 19:01:22 +00008244 // FIXME This causes a redundant load/store if the SSE-class value is already
8245 // in memory, such as if it is on the callstack.
Chris Lattner492a43e2010-09-22 01:28:21 +00008246 if (isScalarFPTypeInSSEReg(TheVT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00008247 assert(DstTy == MVT::i64 && "Invalid FP_TO_SINT to lower!");
Chris Lattner07290932010-09-22 01:05:16 +00008248 Chain = DAG.getStore(Chain, DL, Value, StackSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00008249 MachinePointerInfo::getFixedStack(SSFI),
David Greene67c9d422010-02-15 16:53:33 +00008250 false, false, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +00008251 SDVTList Tys = DAG.getVTList(Op.getOperand(0).getValueType(), MVT::Other);
Dan Gohman475871a2008-07-27 21:46:04 +00008252 SDValue Ops[] = {
Chris Lattner492a43e2010-09-22 01:28:21 +00008253 Chain, StackSlot, DAG.getValueType(TheVT)
Chris Lattner5a88b832007-02-25 07:10:00 +00008254 };
Michael J. Spencerec38de22010-10-10 22:04:20 +00008255
Chris Lattner492a43e2010-09-22 01:28:21 +00008256 MachineMemOperand *MMO =
8257 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
8258 MachineMemOperand::MOLoad, MemSize, MemSize);
8259 Value = DAG.getMemIntrinsicNode(X86ISD::FLD, DL, Tys, Ops, 3,
8260 DstTy, MMO);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008261 Chain = Value.getValue(1);
David Greene3f2bf852009-11-12 20:49:22 +00008262 SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize, false);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008263 StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
8264 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00008265
Chris Lattner07290932010-09-22 01:05:16 +00008266 MachineMemOperand *MMO =
8267 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
8268 MachineMemOperand::MOStore, MemSize, MemSize);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00008269
Michael J. Spencer1a2d0612012-02-24 19:01:22 +00008270 if (Opc != X86ISD::WIN_FTOL) {
8271 // Build the FP_TO_INT*_IN_MEM
8272 SDValue Ops[] = { Chain, Value, StackSlot };
8273 SDValue FIST = DAG.getMemIntrinsicNode(Opc, DL, DAG.getVTList(MVT::Other),
8274 Ops, 3, DstTy, MMO);
8275 return std::make_pair(FIST, StackSlot);
8276 } else {
8277 SDValue ftol = DAG.getNode(X86ISD::WIN_FTOL, DL,
8278 DAG.getVTList(MVT::Other, MVT::Glue),
8279 Chain, Value);
8280 SDValue eax = DAG.getCopyFromReg(ftol, DL, X86::EAX,
8281 MVT::i32, ftol.getValue(1));
8282 SDValue edx = DAG.getCopyFromReg(eax.getValue(1), DL, X86::EDX,
8283 MVT::i32, eax.getValue(2));
NAKAMURA Takumi9a68fdc2012-02-25 03:37:25 +00008284 SDValue Ops[] = { eax, edx };
8285 SDValue pair = IsReplace
8286 ? DAG.getNode(ISD::BUILD_PAIR, DL, MVT::i64, Ops, 2)
8287 : DAG.getMergeValues(Ops, 2, DL);
Michael J. Spencer1a2d0612012-02-24 19:01:22 +00008288 return std::make_pair(pair, SDValue());
8289 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00008290}
8291
Michael Liaoa7554632012-10-23 17:36:08 +00008292SDValue X86TargetLowering::lowerZERO_EXTEND(SDValue Op, SelectionDAG &DAG) const {
8293 DebugLoc DL = Op.getDebugLoc();
8294 EVT VT = Op.getValueType();
8295 SDValue In = Op.getOperand(0);
8296 EVT SVT = In.getValueType();
8297
8298 if (!VT.is256BitVector() || !SVT.is128BitVector() ||
8299 VT.getVectorNumElements() != SVT.getVectorNumElements())
8300 return SDValue();
8301
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00008302 assert(Subtarget->hasFp256() && "256-bit vector is observed without AVX!");
Michael Liaoa7554632012-10-23 17:36:08 +00008303
8304 // AVX2 has better support of integer extending.
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00008305 if (Subtarget->hasInt256())
Michael Liaoa7554632012-10-23 17:36:08 +00008306 return DAG.getNode(X86ISD::VZEXT, DL, VT, In);
8307
8308 SDValue Lo = DAG.getNode(X86ISD::VZEXT, DL, MVT::v4i32, In);
8309 static const int Mask[] = {4, 5, 6, 7, -1, -1, -1, -1};
8310 SDValue Hi = DAG.getNode(X86ISD::VZEXT, DL, MVT::v4i32,
8311 DAG.getVectorShuffle(MVT::v8i16, DL, In, DAG.getUNDEF(MVT::v8i16), &Mask[0]));
8312
8313 return DAG.getNode(ISD::CONCAT_VECTORS, DL, MVT::v8i32, Lo, Hi);
8314}
8315
Michael Liaobedcbd42012-10-16 18:14:11 +00008316SDValue X86TargetLowering::lowerTRUNCATE(SDValue Op, SelectionDAG &DAG) const {
8317 DebugLoc DL = Op.getDebugLoc();
8318 EVT VT = Op.getValueType();
Nadav Rotem3c22a442012-12-27 07:45:10 +00008319 SDValue In = Op.getOperand(0);
8320 EVT SVT = In.getValueType();
Michael Liaobedcbd42012-10-16 18:14:11 +00008321
Nadav Rotem3c22a442012-12-27 07:45:10 +00008322 if ((VT == MVT::v4i32) && (SVT == MVT::v4i64)) {
8323 // On AVX2, v4i64 -> v4i32 becomes VPERMD.
8324 if (Subtarget->hasInt256()) {
8325 static const int ShufMask[] = {0, 2, 4, 6, -1, -1, -1, -1};
8326 In = DAG.getNode(ISD::BITCAST, DL, MVT::v8i32, In);
8327 In = DAG.getVectorShuffle(MVT::v8i32, DL, In, DAG.getUNDEF(MVT::v8i32),
8328 ShufMask);
8329 return DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, VT, In,
8330 DAG.getIntPtrConstant(0));
8331 }
8332
8333 // On AVX, v4i64 -> v4i32 becomes a sequence that uses PSHUFD and MOVLHPS.
8334 SDValue OpLo = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, MVT::v2i64, In,
8335 DAG.getIntPtrConstant(0));
8336 SDValue OpHi = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, MVT::v2i64, In,
8337 DAG.getIntPtrConstant(2));
8338
8339 OpLo = DAG.getNode(ISD::BITCAST, DL, MVT::v4i32, OpLo);
8340 OpHi = DAG.getNode(ISD::BITCAST, DL, MVT::v4i32, OpHi);
8341
8342 // The PSHUFD mask:
8343 static const int ShufMask1[] = {0, 2, 0, 0};
8344 SDValue Undef = DAG.getUNDEF(VT);
8345 OpLo = DAG.getVectorShuffle(VT, DL, OpLo, Undef, ShufMask1);
8346 OpHi = DAG.getVectorShuffle(VT, DL, OpHi, Undef, ShufMask1);
8347
8348 // The MOVLHPS mask:
8349 static const int ShufMask2[] = {0, 1, 4, 5};
8350 return DAG.getVectorShuffle(VT, DL, OpLo, OpHi, ShufMask2);
8351 }
8352
8353 if ((VT == MVT::v8i16) && (SVT == MVT::v8i32)) {
8354 // On AVX2, v8i32 -> v8i16 becomed PSHUFB.
8355 if (Subtarget->hasInt256()) {
8356 In = DAG.getNode(ISD::BITCAST, DL, MVT::v32i8, In);
8357
8358 SmallVector<SDValue,32> pshufbMask;
8359 for (unsigned i = 0; i < 2; ++i) {
8360 pshufbMask.push_back(DAG.getConstant(0x0, MVT::i8));
8361 pshufbMask.push_back(DAG.getConstant(0x1, MVT::i8));
8362 pshufbMask.push_back(DAG.getConstant(0x4, MVT::i8));
8363 pshufbMask.push_back(DAG.getConstant(0x5, MVT::i8));
8364 pshufbMask.push_back(DAG.getConstant(0x8, MVT::i8));
8365 pshufbMask.push_back(DAG.getConstant(0x9, MVT::i8));
8366 pshufbMask.push_back(DAG.getConstant(0xc, MVT::i8));
8367 pshufbMask.push_back(DAG.getConstant(0xd, MVT::i8));
8368 for (unsigned j = 0; j < 8; ++j)
8369 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
8370 }
8371 SDValue BV = DAG.getNode(ISD::BUILD_VECTOR, DL, MVT::v32i8,
8372 &pshufbMask[0], 32);
8373 In = DAG.getNode(X86ISD::PSHUFB, DL, MVT::v32i8, In, BV);
8374 In = DAG.getNode(ISD::BITCAST, DL, MVT::v4i64, In);
8375
8376 static const int ShufMask[] = {0, 2, -1, -1};
8377 In = DAG.getVectorShuffle(MVT::v4i64, DL, In, DAG.getUNDEF(MVT::v4i64),
8378 &ShufMask[0]);
8379 In = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, MVT::v2i64, In,
8380 DAG.getIntPtrConstant(0));
8381 return DAG.getNode(ISD::BITCAST, DL, VT, In);
8382 }
8383
8384 SDValue OpLo = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, MVT::v4i32, In,
8385 DAG.getIntPtrConstant(0));
8386
8387 SDValue OpHi = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, MVT::v4i32, In,
8388 DAG.getIntPtrConstant(4));
8389
8390 OpLo = DAG.getNode(ISD::BITCAST, DL, MVT::v16i8, OpLo);
8391 OpHi = DAG.getNode(ISD::BITCAST, DL, MVT::v16i8, OpHi);
8392
8393 // The PSHUFB mask:
8394 static const int ShufMask1[] = {0, 1, 4, 5, 8, 9, 12, 13,
8395 -1, -1, -1, -1, -1, -1, -1, -1};
8396
8397 SDValue Undef = DAG.getUNDEF(MVT::v16i8);
8398 OpLo = DAG.getVectorShuffle(MVT::v16i8, DL, OpLo, Undef, ShufMask1);
8399 OpHi = DAG.getVectorShuffle(MVT::v16i8, DL, OpHi, Undef, ShufMask1);
8400
8401 OpLo = DAG.getNode(ISD::BITCAST, DL, MVT::v4i32, OpLo);
8402 OpHi = DAG.getNode(ISD::BITCAST, DL, MVT::v4i32, OpHi);
8403
8404 // The MOVLHPS Mask:
8405 static const int ShufMask2[] = {0, 1, 4, 5};
8406 SDValue res = DAG.getVectorShuffle(MVT::v4i32, DL, OpLo, OpHi, ShufMask2);
8407 return DAG.getNode(ISD::BITCAST, DL, MVT::v8i16, res);
8408 }
8409
8410 // Handle truncation of V256 to V128 using shuffles.
8411 if (!VT.is128BitVector() || !SVT.is256BitVector())
Michael Liaobedcbd42012-10-16 18:14:11 +00008412 return SDValue();
8413
Nadav Rotem3c22a442012-12-27 07:45:10 +00008414 assert(VT.getVectorNumElements() != SVT.getVectorNumElements() &&
8415 "Invalid op");
8416 assert(Subtarget->hasFp256() && "256-bit vector without AVX!");
Michael Liaobedcbd42012-10-16 18:14:11 +00008417
8418 unsigned NumElems = VT.getVectorNumElements();
8419 EVT NVT = EVT::getVectorVT(*DAG.getContext(), VT.getVectorElementType(),
8420 NumElems * 2);
8421
Michael Liaobedcbd42012-10-16 18:14:11 +00008422 SmallVector<int, 16> MaskVec(NumElems * 2, -1);
8423 // Prepare truncation shuffle mask
8424 for (unsigned i = 0; i != NumElems; ++i)
8425 MaskVec[i] = i * 2;
8426 SDValue V = DAG.getVectorShuffle(NVT, DL,
8427 DAG.getNode(ISD::BITCAST, DL, NVT, In),
8428 DAG.getUNDEF(NVT), &MaskVec[0]);
8429 return DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, VT, V,
8430 DAG.getIntPtrConstant(0));
8431}
8432
Dan Gohmand858e902010-04-17 15:26:15 +00008433SDValue X86TargetLowering::LowerFP_TO_SINT(SDValue Op,
8434 SelectionDAG &DAG) const {
Michael Liaobedcbd42012-10-16 18:14:11 +00008435 if (Op.getValueType().isVector()) {
8436 if (Op.getValueType() == MVT::v8i16)
8437 return DAG.getNode(ISD::TRUNCATE, Op.getDebugLoc(), Op.getValueType(),
8438 DAG.getNode(ISD::FP_TO_SINT, Op.getDebugLoc(),
8439 MVT::v8i32, Op.getOperand(0)));
Eli Friedman23ef1052009-06-06 03:57:58 +00008440 return SDValue();
Michael Liaobedcbd42012-10-16 18:14:11 +00008441 }
Eli Friedman23ef1052009-06-06 03:57:58 +00008442
NAKAMURA Takumi9a68fdc2012-02-25 03:37:25 +00008443 std::pair<SDValue,SDValue> Vals = FP_TO_INTHelper(Op, DAG,
8444 /*IsSigned=*/ true, /*IsReplace=*/ false);
Dan Gohman475871a2008-07-27 21:46:04 +00008445 SDValue FIST = Vals.first, StackSlot = Vals.second;
Eli Friedman36df4992009-05-27 00:47:34 +00008446 // If FP_TO_INTHelper failed, the node is actually supposed to be Legal.
8447 if (FIST.getNode() == 0) return Op;
Scott Michelfdc40a02009-02-17 22:15:04 +00008448
Michael J. Spencer1a2d0612012-02-24 19:01:22 +00008449 if (StackSlot.getNode())
8450 // Load the result.
8451 return DAG.getLoad(Op.getValueType(), Op.getDebugLoc(),
8452 FIST, StackSlot, MachinePointerInfo(),
8453 false, false, false, 0);
Craig Topper69947b92012-04-23 06:57:04 +00008454
8455 // The node is the result.
8456 return FIST;
Chris Lattner27a6c732007-11-24 07:07:01 +00008457}
8458
Dan Gohmand858e902010-04-17 15:26:15 +00008459SDValue X86TargetLowering::LowerFP_TO_UINT(SDValue Op,
8460 SelectionDAG &DAG) const {
NAKAMURA Takumi9a68fdc2012-02-25 03:37:25 +00008461 std::pair<SDValue,SDValue> Vals = FP_TO_INTHelper(Op, DAG,
8462 /*IsSigned=*/ false, /*IsReplace=*/ false);
Eli Friedman948e95a2009-05-23 09:59:16 +00008463 SDValue FIST = Vals.first, StackSlot = Vals.second;
8464 assert(FIST.getNode() && "Unexpected failure");
8465
NAKAMURA Takumi9a68fdc2012-02-25 03:37:25 +00008466 if (StackSlot.getNode())
8467 // Load the result.
8468 return DAG.getLoad(Op.getValueType(), Op.getDebugLoc(),
8469 FIST, StackSlot, MachinePointerInfo(),
8470 false, false, false, 0);
Craig Topper69947b92012-04-23 06:57:04 +00008471
8472 // The node is the result.
8473 return FIST;
Eli Friedman948e95a2009-05-23 09:59:16 +00008474}
8475
Michael Liao9d796db2012-10-10 16:32:15 +00008476SDValue X86TargetLowering::lowerFP_EXTEND(SDValue Op,
8477 SelectionDAG &DAG) const {
8478 DebugLoc DL = Op.getDebugLoc();
8479 EVT VT = Op.getValueType();
8480 SDValue In = Op.getOperand(0);
8481 EVT SVT = In.getValueType();
8482
8483 assert(SVT == MVT::v2f32 && "Only customize MVT::v2f32 type legalization!");
8484
8485 return DAG.getNode(X86ISD::VFPEXT, DL, VT,
8486 DAG.getNode(ISD::CONCAT_VECTORS, DL, MVT::v4f32,
8487 In, DAG.getUNDEF(SVT)));
8488}
8489
Craig Topper43620672012-09-08 07:31:51 +00008490SDValue X86TargetLowering::LowerFABS(SDValue Op, SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00008491 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008492 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00008493 EVT VT = Op.getValueType();
8494 EVT EltVT = VT;
Craig Topper43620672012-09-08 07:31:51 +00008495 unsigned NumElts = VT == MVT::f64 ? 2 : 4;
8496 if (VT.isVector()) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00008497 EltVT = VT.getVectorElementType();
Craig Topper43620672012-09-08 07:31:51 +00008498 NumElts = VT.getVectorNumElements();
Evan Cheng0db9fe62006-04-25 20:13:52 +00008499 }
Craig Topper43620672012-09-08 07:31:51 +00008500 Constant *C;
8501 if (EltVT == MVT::f64)
8502 C = ConstantFP::get(*Context, APFloat(APInt(64, ~(1ULL << 63))));
8503 else
8504 C = ConstantFP::get(*Context, APFloat(APInt(32, ~(1U << 31))));
8505 C = ConstantVector::getSplat(NumElts, C);
8506 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy());
8507 unsigned Alignment = cast<ConstantPoolSDNode>(CPIdx)->getAlignment();
Dale Johannesenace16102009-02-03 19:33:06 +00008508 SDValue Mask = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00008509 MachinePointerInfo::getConstantPool(),
Craig Topper43620672012-09-08 07:31:51 +00008510 false, false, false, Alignment);
8511 if (VT.isVector()) {
8512 MVT ANDVT = VT.is128BitVector() ? MVT::v2i64 : MVT::v4i64;
8513 return DAG.getNode(ISD::BITCAST, dl, VT,
8514 DAG.getNode(ISD::AND, dl, ANDVT,
8515 DAG.getNode(ISD::BITCAST, dl, ANDVT,
8516 Op.getOperand(0)),
8517 DAG.getNode(ISD::BITCAST, dl, ANDVT, Mask)));
8518 }
Dale Johannesenace16102009-02-03 19:33:06 +00008519 return DAG.getNode(X86ISD::FAND, dl, VT, Op.getOperand(0), Mask);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008520}
8521
Dan Gohmand858e902010-04-17 15:26:15 +00008522SDValue X86TargetLowering::LowerFNEG(SDValue Op, SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00008523 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008524 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00008525 EVT VT = Op.getValueType();
8526 EVT EltVT = VT;
Chad Rosiera860b182011-12-15 01:02:25 +00008527 unsigned NumElts = VT == MVT::f64 ? 2 : 4;
8528 if (VT.isVector()) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00008529 EltVT = VT.getVectorElementType();
Chad Rosiera860b182011-12-15 01:02:25 +00008530 NumElts = VT.getVectorNumElements();
8531 }
Chris Lattner4ca829e2012-01-25 06:02:56 +00008532 Constant *C;
8533 if (EltVT == MVT::f64)
8534 C = ConstantFP::get(*Context, APFloat(APInt(64, 1ULL << 63)));
8535 else
8536 C = ConstantFP::get(*Context, APFloat(APInt(32, 1U << 31)));
8537 C = ConstantVector::getSplat(NumElts, C);
Craig Toppercacd9d62012-09-08 07:46:05 +00008538 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy());
8539 unsigned Alignment = cast<ConstantPoolSDNode>(CPIdx)->getAlignment();
Dale Johannesenace16102009-02-03 19:33:06 +00008540 SDValue Mask = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00008541 MachinePointerInfo::getConstantPool(),
Craig Toppercacd9d62012-09-08 07:46:05 +00008542 false, false, false, Alignment);
Duncan Sands83ec4b62008-06-06 12:08:01 +00008543 if (VT.isVector()) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00008544 MVT XORVT = VT.is128BitVector() ? MVT::v2i64 : MVT::v4i64;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008545 return DAG.getNode(ISD::BITCAST, dl, VT,
Chad Rosiera860b182011-12-15 01:02:25 +00008546 DAG.getNode(ISD::XOR, dl, XORVT,
Craig Topper69947b92012-04-23 06:57:04 +00008547 DAG.getNode(ISD::BITCAST, dl, XORVT,
8548 Op.getOperand(0)),
8549 DAG.getNode(ISD::BITCAST, dl, XORVT, Mask)));
Evan Chengd4d01b72007-07-19 23:36:01 +00008550 }
Craig Topper69947b92012-04-23 06:57:04 +00008551
8552 return DAG.getNode(X86ISD::FXOR, dl, VT, Op.getOperand(0), Mask);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008553}
8554
Dan Gohmand858e902010-04-17 15:26:15 +00008555SDValue X86TargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00008556 LLVMContext *Context = DAG.getContext();
Dan Gohman475871a2008-07-27 21:46:04 +00008557 SDValue Op0 = Op.getOperand(0);
8558 SDValue Op1 = Op.getOperand(1);
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 SrcVT = Op1.getValueType();
Evan Cheng73d6cf12007-01-05 21:37:56 +00008562
8563 // If second operand is smaller, extend it first.
Duncan Sands8e4eb092008-06-08 20:54:56 +00008564 if (SrcVT.bitsLT(VT)) {
Dale Johannesenace16102009-02-03 19:33:06 +00008565 Op1 = DAG.getNode(ISD::FP_EXTEND, dl, VT, Op1);
Evan Cheng73d6cf12007-01-05 21:37:56 +00008566 SrcVT = VT;
8567 }
Dale Johannesen61c7ef32007-10-21 01:07:44 +00008568 // And if it is bigger, shrink it first.
Duncan Sands8e4eb092008-06-08 20:54:56 +00008569 if (SrcVT.bitsGT(VT)) {
Dale Johannesenace16102009-02-03 19:33:06 +00008570 Op1 = DAG.getNode(ISD::FP_ROUND, dl, VT, Op1, DAG.getIntPtrConstant(1));
Dale Johannesen61c7ef32007-10-21 01:07:44 +00008571 SrcVT = VT;
Dale Johannesen61c7ef32007-10-21 01:07:44 +00008572 }
8573
8574 // At this point the operands and the result should have the same
8575 // type, and that won't be f80 since that is not custom lowered.
Evan Cheng73d6cf12007-01-05 21:37:56 +00008576
Evan Cheng68c47cb2007-01-05 07:55:56 +00008577 // First get the sign bit of second operand.
Chad Rosier01d426e2011-12-15 01:16:09 +00008578 SmallVector<Constant*,4> CV;
Owen Anderson825b72b2009-08-11 20:47:22 +00008579 if (SrcVT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00008580 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 1ULL << 63))));
8581 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 0))));
Evan Cheng68c47cb2007-01-05 07:55:56 +00008582 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00008583 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 1U << 31))));
8584 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
8585 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
8586 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
Evan Cheng68c47cb2007-01-05 07:55:56 +00008587 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00008588 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00008589 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00008590 SDValue Mask1 = DAG.getLoad(SrcVT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00008591 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00008592 false, false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00008593 SDValue SignBit = DAG.getNode(X86ISD::FAND, dl, SrcVT, Op1, Mask1);
Evan Cheng68c47cb2007-01-05 07:55:56 +00008594
8595 // Shift sign bit right or left if the two operands have different types.
Duncan Sands8e4eb092008-06-08 20:54:56 +00008596 if (SrcVT.bitsGT(VT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00008597 // Op0 is MVT::f32, Op1 is MVT::f64.
8598 SignBit = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f64, SignBit);
8599 SignBit = DAG.getNode(X86ISD::FSRL, dl, MVT::v2f64, SignBit,
8600 DAG.getConstant(32, MVT::i32));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008601 SignBit = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, SignBit);
Owen Anderson825b72b2009-08-11 20:47:22 +00008602 SignBit = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f32, SignBit,
Chris Lattner0bd48932008-01-17 07:00:52 +00008603 DAG.getIntPtrConstant(0));
Evan Cheng68c47cb2007-01-05 07:55:56 +00008604 }
8605
Evan Cheng73d6cf12007-01-05 21:37:56 +00008606 // Clear first operand sign bit.
8607 CV.clear();
Owen Anderson825b72b2009-08-11 20:47:22 +00008608 if (VT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00008609 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, ~(1ULL << 63)))));
8610 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 0))));
Evan Cheng73d6cf12007-01-05 21:37:56 +00008611 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00008612 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, ~(1U << 31)))));
8613 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
8614 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
8615 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
Evan Cheng73d6cf12007-01-05 21:37:56 +00008616 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00008617 C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00008618 CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00008619 SDValue Mask2 = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00008620 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00008621 false, false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00008622 SDValue Val = DAG.getNode(X86ISD::FAND, dl, VT, Op0, Mask2);
Evan Cheng73d6cf12007-01-05 21:37:56 +00008623
8624 // Or the value with the sign bit.
Dale Johannesenace16102009-02-03 19:33:06 +00008625 return DAG.getNode(X86ISD::FOR, dl, VT, Val, SignBit);
Evan Cheng68c47cb2007-01-05 07:55:56 +00008626}
8627
Craig Topper55b24052012-09-11 06:15:32 +00008628static SDValue LowerFGETSIGN(SDValue Op, SelectionDAG &DAG) {
Stuart Hastings4fd0dee2011-06-01 04:39:42 +00008629 SDValue N0 = Op.getOperand(0);
8630 DebugLoc dl = Op.getDebugLoc();
8631 EVT VT = Op.getValueType();
8632
8633 // Lower ISD::FGETSIGN to (AND (X86ISD::FGETSIGNx86 ...) 1).
8634 SDValue xFGETSIGN = DAG.getNode(X86ISD::FGETSIGNx86, dl, VT, N0,
8635 DAG.getConstant(1, VT));
8636 return DAG.getNode(ISD::AND, dl, VT, xFGETSIGN, DAG.getConstant(1, VT));
8637}
8638
Michael Liaof966e4e2012-09-13 20:24:54 +00008639// LowerVectorAllZeroTest - Check whether an OR'd tree is PTEST-able.
8640//
8641SDValue X86TargetLowering::LowerVectorAllZeroTest(SDValue Op, SelectionDAG &DAG) const {
8642 assert(Op.getOpcode() == ISD::OR && "Only check OR'd tree.");
8643
8644 if (!Subtarget->hasSSE41())
8645 return SDValue();
8646
8647 if (!Op->hasOneUse())
8648 return SDValue();
8649
8650 SDNode *N = Op.getNode();
8651 DebugLoc DL = N->getDebugLoc();
8652
8653 SmallVector<SDValue, 8> Opnds;
8654 DenseMap<SDValue, unsigned> VecInMap;
8655 EVT VT = MVT::Other;
8656
8657 // Recognize a special case where a vector is casted into wide integer to
8658 // test all 0s.
8659 Opnds.push_back(N->getOperand(0));
8660 Opnds.push_back(N->getOperand(1));
8661
8662 for (unsigned Slot = 0, e = Opnds.size(); Slot < e; ++Slot) {
8663 SmallVector<SDValue, 8>::const_iterator I = Opnds.begin() + Slot;
8664 // BFS traverse all OR'd operands.
8665 if (I->getOpcode() == ISD::OR) {
8666 Opnds.push_back(I->getOperand(0));
8667 Opnds.push_back(I->getOperand(1));
8668 // Re-evaluate the number of nodes to be traversed.
8669 e += 2; // 2 more nodes (LHS and RHS) are pushed.
8670 continue;
8671 }
8672
8673 // Quit if a non-EXTRACT_VECTOR_ELT
8674 if (I->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
8675 return SDValue();
8676
8677 // Quit if without a constant index.
8678 SDValue Idx = I->getOperand(1);
8679 if (!isa<ConstantSDNode>(Idx))
8680 return SDValue();
8681
8682 SDValue ExtractedFromVec = I->getOperand(0);
8683 DenseMap<SDValue, unsigned>::iterator M = VecInMap.find(ExtractedFromVec);
8684 if (M == VecInMap.end()) {
8685 VT = ExtractedFromVec.getValueType();
8686 // Quit if not 128/256-bit vector.
8687 if (!VT.is128BitVector() && !VT.is256BitVector())
8688 return SDValue();
8689 // Quit if not the same type.
8690 if (VecInMap.begin() != VecInMap.end() &&
8691 VT != VecInMap.begin()->first.getValueType())
8692 return SDValue();
8693 M = VecInMap.insert(std::make_pair(ExtractedFromVec, 0)).first;
8694 }
8695 M->second |= 1U << cast<ConstantSDNode>(Idx)->getZExtValue();
8696 }
8697
8698 assert((VT.is128BitVector() || VT.is256BitVector()) &&
Michael Liao9aba7ea2012-09-13 20:30:16 +00008699 "Not extracted from 128-/256-bit vector.");
Michael Liaof966e4e2012-09-13 20:24:54 +00008700
8701 unsigned FullMask = (1U << VT.getVectorNumElements()) - 1U;
8702 SmallVector<SDValue, 8> VecIns;
8703
8704 for (DenseMap<SDValue, unsigned>::const_iterator
8705 I = VecInMap.begin(), E = VecInMap.end(); I != E; ++I) {
8706 // Quit if not all elements are used.
8707 if (I->second != FullMask)
8708 return SDValue();
8709 VecIns.push_back(I->first);
8710 }
8711
8712 EVT TestVT = VT.is128BitVector() ? MVT::v2i64 : MVT::v4i64;
8713
8714 // Cast all vectors into TestVT for PTEST.
8715 for (unsigned i = 0, e = VecIns.size(); i < e; ++i)
8716 VecIns[i] = DAG.getNode(ISD::BITCAST, DL, TestVT, VecIns[i]);
8717
8718 // If more than one full vectors are evaluated, OR them first before PTEST.
8719 for (unsigned Slot = 0, e = VecIns.size(); e - Slot > 1; Slot += 2, e += 1) {
8720 // Each iteration will OR 2 nodes and append the result until there is only
8721 // 1 node left, i.e. the final OR'd value of all vectors.
8722 SDValue LHS = VecIns[Slot];
8723 SDValue RHS = VecIns[Slot + 1];
8724 VecIns.push_back(DAG.getNode(ISD::OR, DL, TestVT, LHS, RHS));
8725 }
8726
8727 return DAG.getNode(X86ISD::PTEST, DL, MVT::i32,
8728 VecIns.back(), VecIns.back());
8729}
8730
Dan Gohman076aee32009-03-04 19:44:21 +00008731/// Emit nodes that will be selected as "test Op0,Op0", or something
8732/// equivalent.
Dan Gohman31125812009-03-07 01:58:32 +00008733SDValue X86TargetLowering::EmitTest(SDValue Op, unsigned X86CC,
Evan Cheng552f09a2010-04-26 19:06:11 +00008734 SelectionDAG &DAG) const {
Dan Gohman076aee32009-03-04 19:44:21 +00008735 DebugLoc dl = Op.getDebugLoc();
8736
Dan Gohman31125812009-03-07 01:58:32 +00008737 // CF and OF aren't always set the way we want. Determine which
8738 // of these we need.
8739 bool NeedCF = false;
8740 bool NeedOF = false;
8741 switch (X86CC) {
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008742 default: break;
Dan Gohman31125812009-03-07 01:58:32 +00008743 case X86::COND_A: case X86::COND_AE:
8744 case X86::COND_B: case X86::COND_BE:
8745 NeedCF = true;
8746 break;
8747 case X86::COND_G: case X86::COND_GE:
8748 case X86::COND_L: case X86::COND_LE:
8749 case X86::COND_O: case X86::COND_NO:
8750 NeedOF = true;
8751 break;
Dan Gohman31125812009-03-07 01:58:32 +00008752 }
8753
Dan Gohman076aee32009-03-04 19:44:21 +00008754 // See if we can use the EFLAGS value from the operand instead of
Dan Gohman31125812009-03-07 01:58:32 +00008755 // doing a separate TEST. TEST always sets OF and CF to 0, so unless
8756 // we prove that the arithmetic won't overflow, we can't use OF or CF.
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008757 if (Op.getResNo() != 0 || NeedOF || NeedCF)
8758 // Emit a CMP with 0, which is the TEST pattern.
8759 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op,
8760 DAG.getConstant(0, Op.getValueType()));
8761
8762 unsigned Opcode = 0;
8763 unsigned NumOperands = 0;
Nadav Rotemb9d6b842012-08-18 17:53:03 +00008764
8765 // Truncate operations may prevent the merge of the SETCC instruction
8766 // and the arithmetic intruction before it. Attempt to truncate the operands
8767 // of the arithmetic instruction and use a reduced bit-width instruction.
8768 bool NeedTruncation = false;
8769 SDValue ArithOp = Op;
8770 if (Op->getOpcode() == ISD::TRUNCATE && Op->hasOneUse()) {
8771 SDValue Arith = Op->getOperand(0);
8772 // Both the trunc and the arithmetic op need to have one user each.
8773 if (Arith->hasOneUse())
8774 switch (Arith.getOpcode()) {
8775 default: break;
8776 case ISD::ADD:
8777 case ISD::SUB:
8778 case ISD::AND:
8779 case ISD::OR:
8780 case ISD::XOR: {
8781 NeedTruncation = true;
8782 ArithOp = Arith;
8783 }
8784 }
8785 }
8786
8787 // NOTICE: In the code below we use ArithOp to hold the arithmetic operation
8788 // which may be the result of a CAST. We use the variable 'Op', which is the
8789 // non-casted variable when we check for possible users.
8790 switch (ArithOp.getOpcode()) {
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008791 case ISD::ADD:
8792 // Due to an isel shortcoming, be conservative if this add is likely to be
8793 // selected as part of a load-modify-store instruction. When the root node
8794 // in a match is a store, isel doesn't know how to remap non-chain non-flag
8795 // uses of other nodes in the match, such as the ADD in this case. This
8796 // leads to the ADD being left around and reselected, with the result being
8797 // two adds in the output. Alas, even if none our users are stores, that
8798 // doesn't prove we're O.K. Ergo, if we have any parents that aren't
8799 // CopyToReg or SETCC, eschew INC/DEC. A better fix seems to require
8800 // climbing the DAG back to the root, and it doesn't seem to be worth the
8801 // effort.
8802 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
Pete Cooper2d496892011-11-15 21:57:53 +00008803 UE = Op.getNode()->use_end(); UI != UE; ++UI)
8804 if (UI->getOpcode() != ISD::CopyToReg &&
8805 UI->getOpcode() != ISD::SETCC &&
8806 UI->getOpcode() != ISD::STORE)
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008807 goto default_case;
8808
8809 if (ConstantSDNode *C =
Nadav Rotemb9d6b842012-08-18 17:53:03 +00008810 dyn_cast<ConstantSDNode>(ArithOp.getNode()->getOperand(1))) {
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008811 // An add of one will be selected as an INC.
8812 if (C->getAPIntValue() == 1) {
8813 Opcode = X86ISD::INC;
8814 NumOperands = 1;
8815 break;
Dan Gohmane220c4b2009-09-18 19:59:53 +00008816 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008817
8818 // An add of negative one (subtract of one) will be selected as a DEC.
8819 if (C->getAPIntValue().isAllOnesValue()) {
8820 Opcode = X86ISD::DEC;
8821 NumOperands = 1;
8822 break;
8823 }
Dan Gohman076aee32009-03-04 19:44:21 +00008824 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008825
8826 // Otherwise use a regular EFLAGS-setting add.
8827 Opcode = X86ISD::ADD;
8828 NumOperands = 2;
8829 break;
8830 case ISD::AND: {
8831 // If the primary and result isn't used, don't bother using X86ISD::AND,
8832 // because a TEST instruction will be better.
8833 bool NonFlagUse = false;
8834 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
8835 UE = Op.getNode()->use_end(); UI != UE; ++UI) {
8836 SDNode *User = *UI;
8837 unsigned UOpNo = UI.getOperandNo();
8838 if (User->getOpcode() == ISD::TRUNCATE && User->hasOneUse()) {
8839 // Look pass truncate.
8840 UOpNo = User->use_begin().getOperandNo();
8841 User = *User->use_begin();
8842 }
8843
8844 if (User->getOpcode() != ISD::BRCOND &&
8845 User->getOpcode() != ISD::SETCC &&
Nadav Rotemb9d6b842012-08-18 17:53:03 +00008846 !(User->getOpcode() == ISD::SELECT && UOpNo == 0)) {
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008847 NonFlagUse = true;
8848 break;
8849 }
Dan Gohman076aee32009-03-04 19:44:21 +00008850 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008851
8852 if (!NonFlagUse)
8853 break;
8854 }
8855 // FALL THROUGH
8856 case ISD::SUB:
8857 case ISD::OR:
8858 case ISD::XOR:
8859 // Due to the ISEL shortcoming noted above, be conservative if this op is
8860 // likely to be selected as part of a load-modify-store instruction.
8861 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
8862 UE = Op.getNode()->use_end(); UI != UE; ++UI)
8863 if (UI->getOpcode() == ISD::STORE)
8864 goto default_case;
8865
8866 // Otherwise use a regular EFLAGS-setting instruction.
Nadav Rotemb9d6b842012-08-18 17:53:03 +00008867 switch (ArithOp.getOpcode()) {
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008868 default: llvm_unreachable("unexpected operator!");
Nadav Rotemb9d6b842012-08-18 17:53:03 +00008869 case ISD::SUB: Opcode = X86ISD::SUB; break;
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008870 case ISD::XOR: Opcode = X86ISD::XOR; break;
8871 case ISD::AND: Opcode = X86ISD::AND; break;
Michael Liaof966e4e2012-09-13 20:24:54 +00008872 case ISD::OR: {
8873 if (!NeedTruncation && (X86CC == X86::COND_E || X86CC == X86::COND_NE)) {
8874 SDValue EFLAGS = LowerVectorAllZeroTest(Op, DAG);
8875 if (EFLAGS.getNode())
8876 return EFLAGS;
8877 }
8878 Opcode = X86ISD::OR;
8879 break;
8880 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008881 }
8882
8883 NumOperands = 2;
8884 break;
8885 case X86ISD::ADD:
8886 case X86ISD::SUB:
8887 case X86ISD::INC:
8888 case X86ISD::DEC:
8889 case X86ISD::OR:
8890 case X86ISD::XOR:
8891 case X86ISD::AND:
8892 return SDValue(Op.getNode(), 1);
8893 default:
8894 default_case:
8895 break;
Dan Gohman076aee32009-03-04 19:44:21 +00008896 }
8897
Nadav Rotemb9d6b842012-08-18 17:53:03 +00008898 // If we found that truncation is beneficial, perform the truncation and
8899 // update 'Op'.
8900 if (NeedTruncation) {
8901 EVT VT = Op.getValueType();
8902 SDValue WideVal = Op->getOperand(0);
8903 EVT WideVT = WideVal.getValueType();
8904 unsigned ConvertedOp = 0;
8905 // Use a target machine opcode to prevent further DAGCombine
8906 // optimizations that may separate the arithmetic operations
8907 // from the setcc node.
8908 switch (WideVal.getOpcode()) {
8909 default: break;
8910 case ISD::ADD: ConvertedOp = X86ISD::ADD; break;
8911 case ISD::SUB: ConvertedOp = X86ISD::SUB; break;
8912 case ISD::AND: ConvertedOp = X86ISD::AND; break;
8913 case ISD::OR: ConvertedOp = X86ISD::OR; break;
8914 case ISD::XOR: ConvertedOp = X86ISD::XOR; break;
8915 }
8916
8917 if (ConvertedOp) {
8918 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
8919 if (TLI.isOperationLegal(WideVal.getOpcode(), WideVT)) {
8920 SDValue V0 = DAG.getNode(ISD::TRUNCATE, dl, VT, WideVal.getOperand(0));
8921 SDValue V1 = DAG.getNode(ISD::TRUNCATE, dl, VT, WideVal.getOperand(1));
8922 Op = DAG.getNode(ConvertedOp, dl, VT, V0, V1);
8923 }
8924 }
8925 }
8926
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008927 if (Opcode == 0)
8928 // Emit a CMP with 0, which is the TEST pattern.
8929 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op,
8930 DAG.getConstant(0, Op.getValueType()));
8931
8932 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::i32);
8933 SmallVector<SDValue, 4> Ops;
8934 for (unsigned i = 0; i != NumOperands; ++i)
8935 Ops.push_back(Op.getOperand(i));
8936
8937 SDValue New = DAG.getNode(Opcode, dl, VTs, &Ops[0], NumOperands);
8938 DAG.ReplaceAllUsesWith(Op, New);
8939 return SDValue(New.getNode(), 1);
Dan Gohman076aee32009-03-04 19:44:21 +00008940}
8941
8942/// Emit nodes that will be selected as "cmp Op0,Op1", or something
8943/// equivalent.
Dan Gohman31125812009-03-07 01:58:32 +00008944SDValue X86TargetLowering::EmitCmp(SDValue Op0, SDValue Op1, unsigned X86CC,
Evan Cheng552f09a2010-04-26 19:06:11 +00008945 SelectionDAG &DAG) const {
Dan Gohman076aee32009-03-04 19:44:21 +00008946 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op1))
8947 if (C->getAPIntValue() == 0)
Evan Cheng552f09a2010-04-26 19:06:11 +00008948 return EmitTest(Op0, X86CC, DAG);
Dan Gohman076aee32009-03-04 19:44:21 +00008949
8950 DebugLoc dl = Op0.getDebugLoc();
Manman Ren39ad5682012-08-08 00:51:41 +00008951 if ((Op0.getValueType() == MVT::i8 || Op0.getValueType() == MVT::i16 ||
8952 Op0.getValueType() == MVT::i32 || Op0.getValueType() == MVT::i64)) {
8953 // Use SUB instead of CMP to enable CSE between SUB and CMP.
8954 SDVTList VTs = DAG.getVTList(Op0.getValueType(), MVT::i32);
8955 SDValue Sub = DAG.getNode(X86ISD::SUB, dl, VTs,
8956 Op0, Op1);
8957 return SDValue(Sub.getNode(), 1);
8958 }
Owen Anderson825b72b2009-08-11 20:47:22 +00008959 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op0, Op1);
Dan Gohman076aee32009-03-04 19:44:21 +00008960}
8961
Benjamin Kramer17c836c2012-04-27 12:07:43 +00008962/// Convert a comparison if required by the subtarget.
8963SDValue X86TargetLowering::ConvertCmpIfNecessary(SDValue Cmp,
8964 SelectionDAG &DAG) const {
8965 // If the subtarget does not support the FUCOMI instruction, floating-point
8966 // comparisons have to be converted.
8967 if (Subtarget->hasCMov() ||
8968 Cmp.getOpcode() != X86ISD::CMP ||
8969 !Cmp.getOperand(0).getValueType().isFloatingPoint() ||
8970 !Cmp.getOperand(1).getValueType().isFloatingPoint())
8971 return Cmp;
8972
8973 // The instruction selector will select an FUCOM instruction instead of
8974 // FUCOMI, which writes the comparison result to FPSW instead of EFLAGS. Hence
8975 // build an SDNode sequence that transfers the result from FPSW into EFLAGS:
8976 // (X86sahf (trunc (srl (X86fp_stsw (trunc (X86cmp ...)), 8))))
8977 DebugLoc dl = Cmp.getDebugLoc();
8978 SDValue TruncFPSW = DAG.getNode(ISD::TRUNCATE, dl, MVT::i16, Cmp);
8979 SDValue FNStSW = DAG.getNode(X86ISD::FNSTSW16r, dl, MVT::i16, TruncFPSW);
8980 SDValue Srl = DAG.getNode(ISD::SRL, dl, MVT::i16, FNStSW,
8981 DAG.getConstant(8, MVT::i8));
8982 SDValue TruncSrl = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Srl);
8983 return DAG.getNode(X86ISD::SAHF, dl, MVT::i32, TruncSrl);
8984}
8985
Evan Cheng4e544802012-12-05 00:10:38 +00008986static bool isAllOnes(SDValue V) {
8987 ConstantSDNode *C = dyn_cast<ConstantSDNode>(V);
8988 return C && C->isAllOnesValue();
8989}
8990
Evan Chengd40d03e2010-01-06 19:38:29 +00008991/// LowerToBT - Result of 'and' is compared against zero. Turn it into a BT node
8992/// if it's possible.
Evan Cheng5528e7b2010-04-21 01:47:12 +00008993SDValue X86TargetLowering::LowerToBT(SDValue And, ISD::CondCode CC,
8994 DebugLoc dl, SelectionDAG &DAG) const {
Evan Cheng2c755ba2010-02-27 07:36:59 +00008995 SDValue Op0 = And.getOperand(0);
8996 SDValue Op1 = And.getOperand(1);
8997 if (Op0.getOpcode() == ISD::TRUNCATE)
8998 Op0 = Op0.getOperand(0);
8999 if (Op1.getOpcode() == ISD::TRUNCATE)
9000 Op1 = Op1.getOperand(0);
9001
Evan Chengd40d03e2010-01-06 19:38:29 +00009002 SDValue LHS, RHS;
Dan Gohman6b13cbc2010-06-24 02:07:59 +00009003 if (Op1.getOpcode() == ISD::SHL)
9004 std::swap(Op0, Op1);
9005 if (Op0.getOpcode() == ISD::SHL) {
Evan Cheng2c755ba2010-02-27 07:36:59 +00009006 if (ConstantSDNode *And00C = dyn_cast<ConstantSDNode>(Op0.getOperand(0)))
9007 if (And00C->getZExtValue() == 1) {
Dan Gohman6b13cbc2010-06-24 02:07:59 +00009008 // If we looked past a truncate, check that it's only truncating away
9009 // known zeros.
9010 unsigned BitWidth = Op0.getValueSizeInBits();
9011 unsigned AndBitWidth = And.getValueSizeInBits();
9012 if (BitWidth > AndBitWidth) {
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00009013 APInt Zeros, Ones;
9014 DAG.ComputeMaskedBits(Op0, Zeros, Ones);
Dan Gohman6b13cbc2010-06-24 02:07:59 +00009015 if (Zeros.countLeadingOnes() < BitWidth - AndBitWidth)
9016 return SDValue();
9017 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00009018 LHS = Op1;
9019 RHS = Op0.getOperand(1);
Evan Chengd40d03e2010-01-06 19:38:29 +00009020 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00009021 } else if (Op1.getOpcode() == ISD::Constant) {
9022 ConstantSDNode *AndRHS = cast<ConstantSDNode>(Op1);
Benjamin Kramerf238f502011-11-23 13:54:17 +00009023 uint64_t AndRHSVal = AndRHS->getZExtValue();
Evan Cheng2c755ba2010-02-27 07:36:59 +00009024 SDValue AndLHS = Op0;
Benjamin Kramerf238f502011-11-23 13:54:17 +00009025
9026 if (AndRHSVal == 1 && AndLHS.getOpcode() == ISD::SRL) {
Evan Chengd40d03e2010-01-06 19:38:29 +00009027 LHS = AndLHS.getOperand(0);
9028 RHS = AndLHS.getOperand(1);
Dan Gohmane5af2d32009-01-29 01:59:02 +00009029 }
Benjamin Kramerf238f502011-11-23 13:54:17 +00009030
9031 // Use BT if the immediate can't be encoded in a TEST instruction.
9032 if (!isUInt<32>(AndRHSVal) && isPowerOf2_64(AndRHSVal)) {
9033 LHS = AndLHS;
9034 RHS = DAG.getConstant(Log2_64_Ceil(AndRHSVal), LHS.getValueType());
9035 }
Evan Chengd40d03e2010-01-06 19:38:29 +00009036 }
Evan Cheng0488db92007-09-25 01:57:46 +00009037
Evan Chengd40d03e2010-01-06 19:38:29 +00009038 if (LHS.getNode()) {
Evan Cheng4e544802012-12-05 00:10:38 +00009039 // If the LHS is of the form (x ^ -1) then replace the LHS with x and flip
9040 // the condition code later.
9041 bool Invert = false;
9042 if (LHS.getOpcode() == ISD::XOR && isAllOnes(LHS.getOperand(1))) {
9043 Invert = true;
9044 LHS = LHS.getOperand(0);
9045 }
9046
Evan Chenge5b51ac2010-04-17 06:13:15 +00009047 // If LHS is i8, promote it to i32 with any_extend. There is no i8 BT
Evan Chengd40d03e2010-01-06 19:38:29 +00009048 // instruction. Since the shift amount is in-range-or-undefined, we know
Evan Chenge5b51ac2010-04-17 06:13:15 +00009049 // that doing a bittest on the i32 value is ok. We extend to i32 because
Evan Chengd40d03e2010-01-06 19:38:29 +00009050 // the encoding for the i16 version is larger than the i32 version.
Evan Chenge5b51ac2010-04-17 06:13:15 +00009051 // Also promote i16 to i32 for performance / code size reason.
9052 if (LHS.getValueType() == MVT::i8 ||
Evan Cheng2bce5f4b2010-04-28 08:30:49 +00009053 LHS.getValueType() == MVT::i16)
Evan Chengd40d03e2010-01-06 19:38:29 +00009054 LHS = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, LHS);
Chris Lattnere55484e2008-12-25 05:34:37 +00009055
Evan Chengd40d03e2010-01-06 19:38:29 +00009056 // If the operand types disagree, extend the shift amount to match. Since
9057 // BT ignores high bits (like shifts) we can use anyextend.
9058 if (LHS.getValueType() != RHS.getValueType())
9059 RHS = DAG.getNode(ISD::ANY_EXTEND, dl, LHS.getValueType(), RHS);
Dan Gohmane5af2d32009-01-29 01:59:02 +00009060
Evan Chengd40d03e2010-01-06 19:38:29 +00009061 SDValue BT = DAG.getNode(X86ISD::BT, dl, MVT::i32, LHS, RHS);
Evan Cheng4e544802012-12-05 00:10:38 +00009062 X86::CondCode Cond = CC == ISD::SETEQ ? X86::COND_AE : X86::COND_B;
9063 // Flip the condition if the LHS was a not instruction
9064 if (Invert)
9065 Cond = X86::GetOppositeBranchCondition(Cond);
Evan Chengd40d03e2010-01-06 19:38:29 +00009066 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
9067 DAG.getConstant(Cond, MVT::i8), BT);
Chris Lattnere55484e2008-12-25 05:34:37 +00009068 }
9069
Evan Cheng54de3ea2010-01-05 06:52:31 +00009070 return SDValue();
9071}
9072
Dan Gohmand858e902010-04-17 15:26:15 +00009073SDValue X86TargetLowering::LowerSETCC(SDValue Op, SelectionDAG &DAG) const {
Duncan Sands28b77e92011-09-06 19:07:46 +00009074
9075 if (Op.getValueType().isVector()) return LowerVSETCC(Op, DAG);
9076
Evan Cheng54de3ea2010-01-05 06:52:31 +00009077 assert(Op.getValueType() == MVT::i8 && "SetCC type must be 8-bit integer");
9078 SDValue Op0 = Op.getOperand(0);
9079 SDValue Op1 = Op.getOperand(1);
9080 DebugLoc dl = Op.getDebugLoc();
9081 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
9082
9083 // Optimize to BT if possible.
Evan Chengd40d03e2010-01-06 19:38:29 +00009084 // Lower (X & (1 << N)) == 0 to BT(X, N).
9085 // Lower ((X >>u N) & 1) != 0 to BT(X, N).
9086 // Lower ((X >>s N) & 1) != 0 to BT(X, N).
Andrew Trickf6c39412011-03-23 23:11:02 +00009087 if (Op0.getOpcode() == ISD::AND && Op0.hasOneUse() &&
Evan Chengd40d03e2010-01-06 19:38:29 +00009088 Op1.getOpcode() == ISD::Constant &&
Dan Gohmane368b462010-06-18 14:22:04 +00009089 cast<ConstantSDNode>(Op1)->isNullValue() &&
Evan Chengd40d03e2010-01-06 19:38:29 +00009090 (CC == ISD::SETEQ || CC == ISD::SETNE)) {
9091 SDValue NewSetCC = LowerToBT(Op0, CC, dl, DAG);
9092 if (NewSetCC.getNode())
9093 return NewSetCC;
9094 }
Evan Cheng54de3ea2010-01-05 06:52:31 +00009095
Chris Lattner481eebc2010-12-19 21:23:48 +00009096 // Look for X == 0, X == 1, X != 0, or X != 1. We can simplify some forms of
9097 // these.
9098 if (Op1.getOpcode() == ISD::Constant &&
Andrew Trickf6c39412011-03-23 23:11:02 +00009099 (cast<ConstantSDNode>(Op1)->getZExtValue() == 1 ||
Evan Cheng2c755ba2010-02-27 07:36:59 +00009100 cast<ConstantSDNode>(Op1)->isNullValue()) &&
9101 (CC == ISD::SETEQ || CC == ISD::SETNE)) {
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009102
Chris Lattner481eebc2010-12-19 21:23:48 +00009103 // If the input is a setcc, then reuse the input setcc or use a new one with
9104 // the inverted condition.
9105 if (Op0.getOpcode() == X86ISD::SETCC) {
9106 X86::CondCode CCode = (X86::CondCode)Op0.getConstantOperandVal(0);
9107 bool Invert = (CC == ISD::SETNE) ^
9108 cast<ConstantSDNode>(Op1)->isNullValue();
9109 if (!Invert) return Op0;
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009110
Evan Cheng2c755ba2010-02-27 07:36:59 +00009111 CCode = X86::GetOppositeBranchCondition(CCode);
Chris Lattner481eebc2010-12-19 21:23:48 +00009112 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
9113 DAG.getConstant(CCode, MVT::i8), Op0.getOperand(1));
9114 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00009115 }
9116
Evan Chenge5b51ac2010-04-17 06:13:15 +00009117 bool isFP = Op1.getValueType().isFloatingPoint();
Chris Lattnere55484e2008-12-25 05:34:37 +00009118 unsigned X86CC = TranslateX86CC(CC, isFP, Op0, Op1, DAG);
Dan Gohman1a492952009-10-20 16:22:37 +00009119 if (X86CC == X86::COND_INVALID)
9120 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00009121
Chris Lattnerc19d1c32010-12-19 22:08:31 +00009122 SDValue EFLAGS = EmitCmp(Op0, Op1, X86CC, DAG);
Benjamin Kramer17c836c2012-04-27 12:07:43 +00009123 EFLAGS = ConvertCmpIfNecessary(EFLAGS, DAG);
Owen Anderson825b72b2009-08-11 20:47:22 +00009124 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
Chris Lattnerc19d1c32010-12-19 22:08:31 +00009125 DAG.getConstant(X86CC, MVT::i8), EFLAGS);
Evan Cheng0488db92007-09-25 01:57:46 +00009126}
9127
Craig Topper89af15e2011-09-18 08:03:58 +00009128// Lower256IntVSETCC - Break a VSETCC 256-bit integer VSETCC into two new 128
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00009129// ones, and then concatenate the result back.
Craig Topper89af15e2011-09-18 08:03:58 +00009130static SDValue Lower256IntVSETCC(SDValue Op, SelectionDAG &DAG) {
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00009131 EVT VT = Op.getValueType();
9132
Craig Topper7a9a28b2012-08-12 02:23:29 +00009133 assert(VT.is256BitVector() && Op.getOpcode() == ISD::SETCC &&
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00009134 "Unsupported value type for operation");
9135
Craig Topper66ddd152012-04-27 22:54:43 +00009136 unsigned NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00009137 DebugLoc dl = Op.getDebugLoc();
9138 SDValue CC = Op.getOperand(2);
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00009139
9140 // Extract the LHS vectors
9141 SDValue LHS = Op.getOperand(0);
Craig Topperb14940a2012-04-22 20:55:18 +00009142 SDValue LHS1 = Extract128BitVector(LHS, 0, DAG, dl);
9143 SDValue LHS2 = Extract128BitVector(LHS, NumElems/2, DAG, dl);
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00009144
9145 // Extract the RHS vectors
9146 SDValue RHS = Op.getOperand(1);
Craig Topperb14940a2012-04-22 20:55:18 +00009147 SDValue RHS1 = Extract128BitVector(RHS, 0, DAG, dl);
9148 SDValue RHS2 = Extract128BitVector(RHS, NumElems/2, DAG, dl);
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00009149
9150 // Issue the operation on the smaller types and concatenate the result back
9151 MVT EltVT = VT.getVectorElementType().getSimpleVT();
9152 EVT NewVT = MVT::getVectorVT(EltVT, NumElems/2);
9153 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT,
9154 DAG.getNode(Op.getOpcode(), dl, NewVT, LHS1, RHS1, CC),
9155 DAG.getNode(Op.getOpcode(), dl, NewVT, LHS2, RHS2, CC));
9156}
9157
Dan Gohmand858e902010-04-17 15:26:15 +00009158SDValue X86TargetLowering::LowerVSETCC(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +00009159 SDValue Cond;
9160 SDValue Op0 = Op.getOperand(0);
9161 SDValue Op1 = Op.getOperand(1);
9162 SDValue CC = Op.getOperand(2);
Owen Andersone50ed302009-08-10 22:56:29 +00009163 EVT VT = Op.getValueType();
Nate Begeman30a0de92008-07-17 16:51:19 +00009164 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
9165 bool isFP = Op.getOperand(1).getValueType().isFloatingPoint();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009166 DebugLoc dl = Op.getDebugLoc();
Nate Begeman30a0de92008-07-17 16:51:19 +00009167
9168 if (isFP) {
Craig Topper523908d2012-08-13 02:34:03 +00009169#ifndef NDEBUG
Bruno Cardoso Lopes0f0e0a02011-08-09 00:46:57 +00009170 EVT EltVT = Op0.getValueType().getVectorElementType();
Craig Topper523908d2012-08-13 02:34:03 +00009171 assert(EltVT == MVT::f32 || EltVT == MVT::f64);
9172#endif
Bruno Cardoso Lopes0f0e0a02011-08-09 00:46:57 +00009173
Craig Topper523908d2012-08-13 02:34:03 +00009174 unsigned SSECC;
Nate Begeman30a0de92008-07-17 16:51:19 +00009175 bool Swap = false;
9176
Bruno Cardoso Lopes8e03a822011-09-12 19:30:40 +00009177 // SSE Condition code mapping:
9178 // 0 - EQ
9179 // 1 - LT
9180 // 2 - LE
9181 // 3 - UNORD
9182 // 4 - NEQ
9183 // 5 - NLT
9184 // 6 - NLE
9185 // 7 - ORD
Nate Begeman30a0de92008-07-17 16:51:19 +00009186 switch (SetCCOpcode) {
Craig Topper2f1b2ec2012-08-13 03:42:38 +00009187 default: llvm_unreachable("Unexpected SETCC condition");
Nate Begemanfb8ead02008-07-25 19:05:58 +00009188 case ISD::SETOEQ:
Nate Begeman30a0de92008-07-17 16:51:19 +00009189 case ISD::SETEQ: SSECC = 0; break;
Bruno Cardoso Lopes8e03a822011-09-12 19:30:40 +00009190 case ISD::SETOGT:
9191 case ISD::SETGT: Swap = true; // Fallthrough
Bruno Cardoso Lopes457d53d2011-09-12 21:24:07 +00009192 case ISD::SETLT:
9193 case ISD::SETOLT: SSECC = 1; break;
9194 case ISD::SETOGE:
9195 case ISD::SETGE: Swap = true; // Fallthrough
Nate Begeman30a0de92008-07-17 16:51:19 +00009196 case ISD::SETLE:
9197 case ISD::SETOLE: SSECC = 2; break;
9198 case ISD::SETUO: SSECC = 3; break;
Nate Begemanfb8ead02008-07-25 19:05:58 +00009199 case ISD::SETUNE:
Nate Begeman30a0de92008-07-17 16:51:19 +00009200 case ISD::SETNE: SSECC = 4; break;
Craig Topper523908d2012-08-13 02:34:03 +00009201 case ISD::SETULE: Swap = true; // Fallthrough
Nate Begeman30a0de92008-07-17 16:51:19 +00009202 case ISD::SETUGE: SSECC = 5; break;
Craig Topper523908d2012-08-13 02:34:03 +00009203 case ISD::SETULT: Swap = true; // Fallthrough
Nate Begeman30a0de92008-07-17 16:51:19 +00009204 case ISD::SETUGT: SSECC = 6; break;
9205 case ISD::SETO: SSECC = 7; break;
Craig Topper523908d2012-08-13 02:34:03 +00009206 case ISD::SETUEQ:
9207 case ISD::SETONE: SSECC = 8; break;
Nate Begeman30a0de92008-07-17 16:51:19 +00009208 }
9209 if (Swap)
9210 std::swap(Op0, Op1);
9211
Nate Begemanfb8ead02008-07-25 19:05:58 +00009212 // In the two special cases we can't handle, emit two comparisons.
Nate Begeman30a0de92008-07-17 16:51:19 +00009213 if (SSECC == 8) {
Craig Topper523908d2012-08-13 02:34:03 +00009214 unsigned CC0, CC1;
9215 unsigned CombineOpc;
Nate Begemanfb8ead02008-07-25 19:05:58 +00009216 if (SetCCOpcode == ISD::SETUEQ) {
Craig Topper523908d2012-08-13 02:34:03 +00009217 CC0 = 3; CC1 = 0; CombineOpc = ISD::OR;
9218 } else {
9219 assert(SetCCOpcode == ISD::SETONE);
9220 CC0 = 7; CC1 = 4; CombineOpc = ISD::AND;
Craig Topper69947b92012-04-23 06:57:04 +00009221 }
Craig Topper523908d2012-08-13 02:34:03 +00009222
9223 SDValue Cmp0 = DAG.getNode(X86ISD::CMPP, dl, VT, Op0, Op1,
9224 DAG.getConstant(CC0, MVT::i8));
9225 SDValue Cmp1 = DAG.getNode(X86ISD::CMPP, dl, VT, Op0, Op1,
9226 DAG.getConstant(CC1, MVT::i8));
9227 return DAG.getNode(CombineOpc, dl, VT, Cmp0, Cmp1);
Nate Begeman30a0de92008-07-17 16:51:19 +00009228 }
9229 // Handle all other FP comparisons here.
Craig Topper1906d322012-01-22 23:36:02 +00009230 return DAG.getNode(X86ISD::CMPP, dl, VT, Op0, Op1,
9231 DAG.getConstant(SSECC, MVT::i8));
Nate Begeman30a0de92008-07-17 16:51:19 +00009232 }
Scott Michelfdc40a02009-02-17 22:15:04 +00009233
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00009234 // Break 256-bit integer vector compare into smaller ones.
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00009235 if (VT.is256BitVector() && !Subtarget->hasInt256())
Craig Topper89af15e2011-09-18 08:03:58 +00009236 return Lower256IntVSETCC(Op, DAG);
Bruno Cardoso Lopes0f0e0a02011-08-09 00:46:57 +00009237
Nate Begeman30a0de92008-07-17 16:51:19 +00009238 // We are handling one of the integer comparisons here. Since SSE only has
9239 // GT and EQ comparisons for integer, swapping operands and multiple
9240 // operations may be required for some comparisons.
Craig Topper2f1b2ec2012-08-13 03:42:38 +00009241 unsigned Opc;
Nate Begeman30a0de92008-07-17 16:51:19 +00009242 bool Swap = false, Invert = false, FlipSigns = false;
Scott Michelfdc40a02009-02-17 22:15:04 +00009243
Nate Begeman30a0de92008-07-17 16:51:19 +00009244 switch (SetCCOpcode) {
Craig Topper2f1b2ec2012-08-13 03:42:38 +00009245 default: llvm_unreachable("Unexpected SETCC condition");
Nate Begeman30a0de92008-07-17 16:51:19 +00009246 case ISD::SETNE: Invert = true;
Craig Topper67609fd2012-01-22 22:42:16 +00009247 case ISD::SETEQ: Opc = X86ISD::PCMPEQ; break;
Nate Begeman30a0de92008-07-17 16:51:19 +00009248 case ISD::SETLT: Swap = true;
Craig Topper67609fd2012-01-22 22:42:16 +00009249 case ISD::SETGT: Opc = X86ISD::PCMPGT; break;
Nate Begeman30a0de92008-07-17 16:51:19 +00009250 case ISD::SETGE: Swap = true;
Craig Topper67609fd2012-01-22 22:42:16 +00009251 case ISD::SETLE: Opc = X86ISD::PCMPGT; Invert = true; break;
Nate Begeman30a0de92008-07-17 16:51:19 +00009252 case ISD::SETULT: Swap = true;
Craig Topper67609fd2012-01-22 22:42:16 +00009253 case ISD::SETUGT: Opc = X86ISD::PCMPGT; FlipSigns = true; break;
Nate Begeman30a0de92008-07-17 16:51:19 +00009254 case ISD::SETUGE: Swap = true;
Craig Topper67609fd2012-01-22 22:42:16 +00009255 case ISD::SETULE: Opc = X86ISD::PCMPGT; FlipSigns = true; Invert = true; break;
Nate Begeman30a0de92008-07-17 16:51:19 +00009256 }
9257 if (Swap)
9258 std::swap(Op0, Op1);
Scott Michelfdc40a02009-02-17 22:15:04 +00009259
Eli Friedman7d3e2b72011-09-28 21:00:25 +00009260 // Check that the operation in question is available (most are plain SSE2,
9261 // but PCMPGTQ and PCMPEQQ have different requirements).
Craig Topper2f1b2ec2012-08-13 03:42:38 +00009262 if (VT == MVT::v2i64) {
9263 if (Opc == X86ISD::PCMPGT && !Subtarget->hasSSE42())
9264 return SDValue();
Benjamin Kramer382ed782012-12-25 12:54:19 +00009265 if (Opc == X86ISD::PCMPEQ && !Subtarget->hasSSE41()) {
9266 // If pcmpeqq is missing but pcmpeqd is available synthesize pcmpeqq with
Benjamin Kramer99f78062012-12-25 13:09:08 +00009267 // pcmpeqd + pshufd + pand.
Benjamin Kramer382ed782012-12-25 12:54:19 +00009268 assert(Subtarget->hasSSE2() && !FlipSigns && "Don't know how to lower!");
9269
9270 // First cast everything to the right type,
9271 Op0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, Op0);
9272 Op1 = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, Op1);
9273
9274 // Do the compare.
9275 SDValue Result = DAG.getNode(Opc, dl, MVT::v4i32, Op0, Op1);
9276
9277 // Make sure the lower and upper halves are both all-ones.
Benjamin Kramer99f78062012-12-25 13:09:08 +00009278 const int Mask[] = { 1, 0, 3, 2 };
9279 SDValue Shuf = DAG.getVectorShuffle(MVT::v4i32, dl, Result, Result, Mask);
9280 Result = DAG.getNode(ISD::AND, dl, MVT::v4i32, Result, Shuf);
Benjamin Kramer382ed782012-12-25 12:54:19 +00009281
9282 if (Invert)
9283 Result = DAG.getNOT(dl, Result, MVT::v4i32);
9284
9285 return DAG.getNode(ISD::BITCAST, dl, VT, Result);
9286 }
Craig Topper2f1b2ec2012-08-13 03:42:38 +00009287 }
Eli Friedman7d3e2b72011-09-28 21:00:25 +00009288
Nate Begeman30a0de92008-07-17 16:51:19 +00009289 // Since SSE has no unsigned integer comparisons, we need to flip the sign
9290 // bits of the inputs before performing those operations.
9291 if (FlipSigns) {
Owen Andersone50ed302009-08-10 22:56:29 +00009292 EVT EltVT = VT.getVectorElementType();
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00009293 SDValue SignBit = DAG.getConstant(APInt::getSignBit(EltVT.getSizeInBits()),
9294 EltVT);
Dan Gohman475871a2008-07-27 21:46:04 +00009295 std::vector<SDValue> SignBits(VT.getVectorNumElements(), SignBit);
Evan Chenga87008d2009-02-25 22:49:59 +00009296 SDValue SignVec = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &SignBits[0],
9297 SignBits.size());
Dale Johannesenace16102009-02-03 19:33:06 +00009298 Op0 = DAG.getNode(ISD::XOR, dl, VT, Op0, SignVec);
9299 Op1 = DAG.getNode(ISD::XOR, dl, VT, Op1, SignVec);
Nate Begeman30a0de92008-07-17 16:51:19 +00009300 }
Scott Michelfdc40a02009-02-17 22:15:04 +00009301
Dale Johannesenace16102009-02-03 19:33:06 +00009302 SDValue Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
Nate Begeman30a0de92008-07-17 16:51:19 +00009303
9304 // If the logical-not of the result is required, perform that now.
Bob Wilson4c245462009-01-22 17:39:32 +00009305 if (Invert)
Dale Johannesenace16102009-02-03 19:33:06 +00009306 Result = DAG.getNOT(dl, Result, VT);
Bob Wilson4c245462009-01-22 17:39:32 +00009307
Nate Begeman30a0de92008-07-17 16:51:19 +00009308 return Result;
9309}
Evan Cheng0488db92007-09-25 01:57:46 +00009310
Evan Cheng370e5342008-12-03 08:38:43 +00009311// isX86LogicalCmp - Return true if opcode is a X86 logical comparison.
Dan Gohman076aee32009-03-04 19:44:21 +00009312static bool isX86LogicalCmp(SDValue Op) {
9313 unsigned Opc = Op.getNode()->getOpcode();
Benjamin Kramer17c836c2012-04-27 12:07:43 +00009314 if (Opc == X86ISD::CMP || Opc == X86ISD::COMI || Opc == X86ISD::UCOMI ||
9315 Opc == X86ISD::SAHF)
Dan Gohman076aee32009-03-04 19:44:21 +00009316 return true;
9317 if (Op.getResNo() == 1 &&
9318 (Opc == X86ISD::ADD ||
9319 Opc == X86ISD::SUB ||
Chris Lattner5b856542010-12-20 00:59:46 +00009320 Opc == X86ISD::ADC ||
9321 Opc == X86ISD::SBB ||
Dan Gohman076aee32009-03-04 19:44:21 +00009322 Opc == X86ISD::SMUL ||
9323 Opc == X86ISD::UMUL ||
9324 Opc == X86ISD::INC ||
Dan Gohmane220c4b2009-09-18 19:59:53 +00009325 Opc == X86ISD::DEC ||
9326 Opc == X86ISD::OR ||
9327 Opc == X86ISD::XOR ||
9328 Opc == X86ISD::AND))
Dan Gohman076aee32009-03-04 19:44:21 +00009329 return true;
9330
Chris Lattner9637d5b2010-12-05 07:49:54 +00009331 if (Op.getResNo() == 2 && Opc == X86ISD::UMUL)
9332 return true;
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009333
Dan Gohman076aee32009-03-04 19:44:21 +00009334 return false;
Evan Cheng370e5342008-12-03 08:38:43 +00009335}
9336
Chris Lattnera2b56002010-12-05 01:23:24 +00009337static bool isZero(SDValue V) {
9338 ConstantSDNode *C = dyn_cast<ConstantSDNode>(V);
9339 return C && C->isNullValue();
9340}
9341
Evan Chengb64dd5f2012-08-07 22:21:00 +00009342static bool isTruncWithZeroHighBitsInput(SDValue V, SelectionDAG &DAG) {
9343 if (V.getOpcode() != ISD::TRUNCATE)
9344 return false;
9345
9346 SDValue VOp0 = V.getOperand(0);
9347 unsigned InBits = VOp0.getValueSizeInBits();
9348 unsigned Bits = V.getValueSizeInBits();
9349 return DAG.MaskedValueIsZero(VOp0, APInt::getHighBitsSet(InBits,InBits-Bits));
9350}
9351
Dan Gohmand858e902010-04-17 15:26:15 +00009352SDValue X86TargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng734503b2006-09-11 02:19:56 +00009353 bool addTest = true;
Dan Gohman475871a2008-07-27 21:46:04 +00009354 SDValue Cond = Op.getOperand(0);
Chris Lattnera2b56002010-12-05 01:23:24 +00009355 SDValue Op1 = Op.getOperand(1);
9356 SDValue Op2 = Op.getOperand(2);
9357 DebugLoc DL = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00009358 SDValue CC;
Evan Cheng9bba8942006-01-26 02:13:10 +00009359
Dan Gohman1a492952009-10-20 16:22:37 +00009360 if (Cond.getOpcode() == ISD::SETCC) {
9361 SDValue NewCond = LowerSETCC(Cond, DAG);
9362 if (NewCond.getNode())
9363 Cond = NewCond;
9364 }
Evan Cheng734503b2006-09-11 02:19:56 +00009365
Chris Lattnera2b56002010-12-05 01:23:24 +00009366 // (select (x == 0), -1, y) -> (sign_bit (x - 1)) | y
Chris Lattner96908b12010-12-05 02:00:51 +00009367 // (select (x == 0), y, -1) -> ~(sign_bit (x - 1)) | y
Chris Lattnera2b56002010-12-05 01:23:24 +00009368 // (select (x != 0), y, -1) -> (sign_bit (x - 1)) | y
Chris Lattner96908b12010-12-05 02:00:51 +00009369 // (select (x != 0), -1, y) -> ~(sign_bit (x - 1)) | y
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00009370 if (Cond.getOpcode() == X86ISD::SETCC &&
Chris Lattner96908b12010-12-05 02:00:51 +00009371 Cond.getOperand(1).getOpcode() == X86ISD::CMP &&
9372 isZero(Cond.getOperand(1).getOperand(1))) {
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00009373 SDValue Cmp = Cond.getOperand(1);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009374
Chris Lattnera2b56002010-12-05 01:23:24 +00009375 unsigned CondCode =cast<ConstantSDNode>(Cond.getOperand(0))->getZExtValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009376
9377 if ((isAllOnes(Op1) || isAllOnes(Op2)) &&
Chris Lattner96908b12010-12-05 02:00:51 +00009378 (CondCode == X86::COND_E || CondCode == X86::COND_NE)) {
9379 SDValue Y = isAllOnes(Op2) ? Op1 : Op2;
Chris Lattnera2b56002010-12-05 01:23:24 +00009380
9381 SDValue CmpOp0 = Cmp.getOperand(0);
Manman Rened579842012-05-07 18:06:23 +00009382 // Apply further optimizations for special cases
9383 // (select (x != 0), -1, 0) -> neg & sbb
9384 // (select (x == 0), 0, -1) -> neg & sbb
9385 if (ConstantSDNode *YC = dyn_cast<ConstantSDNode>(Y))
Chad Rosiera20e1e72012-08-01 18:39:17 +00009386 if (YC->isNullValue() &&
Manman Rened579842012-05-07 18:06:23 +00009387 (isAllOnes(Op1) == (CondCode == X86::COND_NE))) {
9388 SDVTList VTs = DAG.getVTList(CmpOp0.getValueType(), MVT::i32);
Chad Rosiera20e1e72012-08-01 18:39:17 +00009389 SDValue Neg = DAG.getNode(X86ISD::SUB, DL, VTs,
9390 DAG.getConstant(0, CmpOp0.getValueType()),
Manman Rened579842012-05-07 18:06:23 +00009391 CmpOp0);
9392 SDValue Res = DAG.getNode(X86ISD::SETCC_CARRY, DL, Op.getValueType(),
9393 DAG.getConstant(X86::COND_B, MVT::i8),
9394 SDValue(Neg.getNode(), 1));
9395 return Res;
9396 }
9397
Chris Lattnera2b56002010-12-05 01:23:24 +00009398 Cmp = DAG.getNode(X86ISD::CMP, DL, MVT::i32,
9399 CmpOp0, DAG.getConstant(1, CmpOp0.getValueType()));
Benjamin Kramer17c836c2012-04-27 12:07:43 +00009400 Cmp = ConvertCmpIfNecessary(Cmp, DAG);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009401
Chris Lattner96908b12010-12-05 02:00:51 +00009402 SDValue Res = // Res = 0 or -1.
Chris Lattnera2b56002010-12-05 01:23:24 +00009403 DAG.getNode(X86ISD::SETCC_CARRY, DL, Op.getValueType(),
9404 DAG.getConstant(X86::COND_B, MVT::i8), Cmp);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009405
Chris Lattner96908b12010-12-05 02:00:51 +00009406 if (isAllOnes(Op1) != (CondCode == X86::COND_E))
9407 Res = DAG.getNOT(DL, Res, Res.getValueType());
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009408
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00009409 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(Op2);
Chris Lattnera2b56002010-12-05 01:23:24 +00009410 if (N2C == 0 || !N2C->isNullValue())
9411 Res = DAG.getNode(ISD::OR, DL, Res.getValueType(), Res, Y);
9412 return Res;
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00009413 }
9414 }
9415
Chris Lattnera2b56002010-12-05 01:23:24 +00009416 // Look past (and (setcc_carry (cmp ...)), 1).
Evan Chengad9c0a32009-12-15 00:53:42 +00009417 if (Cond.getOpcode() == ISD::AND &&
9418 Cond.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY) {
9419 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Cond.getOperand(1));
Michael J. Spencerec38de22010-10-10 22:04:20 +00009420 if (C && C->getAPIntValue() == 1)
Evan Chengad9c0a32009-12-15 00:53:42 +00009421 Cond = Cond.getOperand(0);
9422 }
9423
Evan Cheng3f41d662007-10-08 22:16:29 +00009424 // If condition flag is set by a X86ISD::CMP, then use it as the condition
9425 // setting operand in place of the X86ISD::SETCC.
Dan Gohman65fd6562011-11-03 21:49:52 +00009426 unsigned CondOpcode = Cond.getOpcode();
9427 if (CondOpcode == X86ISD::SETCC ||
9428 CondOpcode == X86ISD::SETCC_CARRY) {
Evan Cheng734503b2006-09-11 02:19:56 +00009429 CC = Cond.getOperand(0);
9430
Dan Gohman475871a2008-07-27 21:46:04 +00009431 SDValue Cmp = Cond.getOperand(1);
Evan Cheng734503b2006-09-11 02:19:56 +00009432 unsigned Opc = Cmp.getOpcode();
Owen Andersone50ed302009-08-10 22:56:29 +00009433 EVT VT = Op.getValueType();
Scott Michelfdc40a02009-02-17 22:15:04 +00009434
Evan Cheng3f41d662007-10-08 22:16:29 +00009435 bool IllegalFPCMov = false;
Duncan Sands83ec4b62008-06-06 12:08:01 +00009436 if (VT.isFloatingPoint() && !VT.isVector() &&
Chris Lattner78631162008-01-16 06:24:21 +00009437 !isScalarFPTypeInSSEReg(VT)) // FPStack?
Dan Gohman7810bfe2008-09-26 21:54:37 +00009438 IllegalFPCMov = !hasFPCMov(cast<ConstantSDNode>(CC)->getSExtValue());
Scott Michelfdc40a02009-02-17 22:15:04 +00009439
Chris Lattnerd1980a52009-03-12 06:52:53 +00009440 if ((isX86LogicalCmp(Cmp) && !IllegalFPCMov) ||
9441 Opc == X86ISD::BT) { // FIXME
Evan Cheng3f41d662007-10-08 22:16:29 +00009442 Cond = Cmp;
Evan Cheng0488db92007-09-25 01:57:46 +00009443 addTest = false;
9444 }
Dan Gohman65fd6562011-11-03 21:49:52 +00009445 } else if (CondOpcode == ISD::USUBO || CondOpcode == ISD::SSUBO ||
9446 CondOpcode == ISD::UADDO || CondOpcode == ISD::SADDO ||
9447 ((CondOpcode == ISD::UMULO || CondOpcode == ISD::SMULO) &&
9448 Cond.getOperand(0).getValueType() != MVT::i8)) {
9449 SDValue LHS = Cond.getOperand(0);
9450 SDValue RHS = Cond.getOperand(1);
9451 unsigned X86Opcode;
9452 unsigned X86Cond;
9453 SDVTList VTs;
9454 switch (CondOpcode) {
9455 case ISD::UADDO: X86Opcode = X86ISD::ADD; X86Cond = X86::COND_B; break;
9456 case ISD::SADDO: X86Opcode = X86ISD::ADD; X86Cond = X86::COND_O; break;
9457 case ISD::USUBO: X86Opcode = X86ISD::SUB; X86Cond = X86::COND_B; break;
9458 case ISD::SSUBO: X86Opcode = X86ISD::SUB; X86Cond = X86::COND_O; break;
9459 case ISD::UMULO: X86Opcode = X86ISD::UMUL; X86Cond = X86::COND_O; break;
9460 case ISD::SMULO: X86Opcode = X86ISD::SMUL; X86Cond = X86::COND_O; break;
9461 default: llvm_unreachable("unexpected overflowing operator");
9462 }
9463 if (CondOpcode == ISD::UMULO)
9464 VTs = DAG.getVTList(LHS.getValueType(), LHS.getValueType(),
9465 MVT::i32);
9466 else
9467 VTs = DAG.getVTList(LHS.getValueType(), MVT::i32);
9468
9469 SDValue X86Op = DAG.getNode(X86Opcode, DL, VTs, LHS, RHS);
9470
9471 if (CondOpcode == ISD::UMULO)
9472 Cond = X86Op.getValue(2);
9473 else
9474 Cond = X86Op.getValue(1);
9475
9476 CC = DAG.getConstant(X86Cond, MVT::i8);
9477 addTest = false;
Evan Cheng0488db92007-09-25 01:57:46 +00009478 }
9479
9480 if (addTest) {
Evan Chengb64dd5f2012-08-07 22:21:00 +00009481 // Look pass the truncate if the high bits are known zero.
9482 if (isTruncWithZeroHighBitsInput(Cond, DAG))
9483 Cond = Cond.getOperand(0);
Evan Chengd40d03e2010-01-06 19:38:29 +00009484
9485 // We know the result of AND is compared against zero. Try to match
9486 // it to BT.
Michael J. Spencerec38de22010-10-10 22:04:20 +00009487 if (Cond.getOpcode() == ISD::AND && Cond.hasOneUse()) {
Chris Lattnera2b56002010-12-05 01:23:24 +00009488 SDValue NewSetCC = LowerToBT(Cond, ISD::SETNE, DL, DAG);
Evan Chengd40d03e2010-01-06 19:38:29 +00009489 if (NewSetCC.getNode()) {
9490 CC = NewSetCC.getOperand(0);
9491 Cond = NewSetCC.getOperand(1);
9492 addTest = false;
9493 }
9494 }
9495 }
9496
9497 if (addTest) {
Owen Anderson825b72b2009-08-11 20:47:22 +00009498 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng552f09a2010-04-26 19:06:11 +00009499 Cond = EmitTest(Cond, X86::COND_NE, DAG);
Evan Cheng0488db92007-09-25 01:57:46 +00009500 }
9501
Benjamin Kramere915ff32010-12-22 23:09:28 +00009502 // a < b ? -1 : 0 -> RES = ~setcc_carry
9503 // a < b ? 0 : -1 -> RES = setcc_carry
9504 // a >= b ? -1 : 0 -> RES = setcc_carry
9505 // a >= b ? 0 : -1 -> RES = ~setcc_carry
Manman Ren39ad5682012-08-08 00:51:41 +00009506 if (Cond.getOpcode() == X86ISD::SUB) {
Benjamin Kramer17c836c2012-04-27 12:07:43 +00009507 Cond = ConvertCmpIfNecessary(Cond, DAG);
Benjamin Kramere915ff32010-12-22 23:09:28 +00009508 unsigned CondCode = cast<ConstantSDNode>(CC)->getZExtValue();
9509
9510 if ((CondCode == X86::COND_AE || CondCode == X86::COND_B) &&
9511 (isAllOnes(Op1) || isAllOnes(Op2)) && (isZero(Op1) || isZero(Op2))) {
9512 SDValue Res = DAG.getNode(X86ISD::SETCC_CARRY, DL, Op.getValueType(),
9513 DAG.getConstant(X86::COND_B, MVT::i8), Cond);
9514 if (isAllOnes(Op1) != (CondCode == X86::COND_B))
9515 return DAG.getNOT(DL, Res, Res.getValueType());
9516 return Res;
9517 }
9518 }
9519
Benjamin Kramer444dcce2012-10-13 10:39:49 +00009520 // X86 doesn't have an i8 cmov. If both operands are the result of a truncate
9521 // widen the cmov and push the truncate through. This avoids introducing a new
9522 // branch during isel and doesn't add any extensions.
9523 if (Op.getValueType() == MVT::i8 &&
9524 Op1.getOpcode() == ISD::TRUNCATE && Op2.getOpcode() == ISD::TRUNCATE) {
9525 SDValue T1 = Op1.getOperand(0), T2 = Op2.getOperand(0);
9526 if (T1.getValueType() == T2.getValueType() &&
9527 // Blacklist CopyFromReg to avoid partial register stalls.
9528 T1.getOpcode() != ISD::CopyFromReg && T2.getOpcode()!=ISD::CopyFromReg){
9529 SDVTList VTs = DAG.getVTList(T1.getValueType(), MVT::Glue);
Benjamin Kramerf8b65aa2012-10-13 12:50:19 +00009530 SDValue Cmov = DAG.getNode(X86ISD::CMOV, DL, VTs, T2, T1, CC, Cond);
Benjamin Kramer444dcce2012-10-13 10:39:49 +00009531 return DAG.getNode(ISD::TRUNCATE, DL, Op.getValueType(), Cmov);
9532 }
9533 }
9534
Evan Cheng0488db92007-09-25 01:57:46 +00009535 // X86ISD::CMOV means set the result (which is operand 1) to the RHS if
9536 // condition is true.
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00009537 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::Glue);
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00009538 SDValue Ops[] = { Op2, Op1, CC, Cond };
Chris Lattnera2b56002010-12-05 01:23:24 +00009539 return DAG.getNode(X86ISD::CMOV, DL, VTs, Ops, array_lengthof(Ops));
Evan Cheng0488db92007-09-25 01:57:46 +00009540}
9541
Evan Cheng370e5342008-12-03 08:38:43 +00009542// isAndOrOfSingleUseSetCCs - Return true if node is an ISD::AND or
9543// ISD::OR of two X86ISD::SETCC nodes each of which has no other use apart
9544// from the AND / OR.
9545static bool isAndOrOfSetCCs(SDValue Op, unsigned &Opc) {
9546 Opc = Op.getOpcode();
9547 if (Opc != ISD::OR && Opc != ISD::AND)
9548 return false;
9549 return (Op.getOperand(0).getOpcode() == X86ISD::SETCC &&
9550 Op.getOperand(0).hasOneUse() &&
9551 Op.getOperand(1).getOpcode() == X86ISD::SETCC &&
9552 Op.getOperand(1).hasOneUse());
9553}
9554
Evan Cheng961d6d42009-02-02 08:19:07 +00009555// isXor1OfSetCC - Return true if node is an ISD::XOR of a X86ISD::SETCC and
9556// 1 and that the SETCC node has a single use.
Evan Cheng67ad9db2009-02-02 08:07:36 +00009557static bool isXor1OfSetCC(SDValue Op) {
9558 if (Op.getOpcode() != ISD::XOR)
9559 return false;
9560 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
9561 if (N1C && N1C->getAPIntValue() == 1) {
9562 return Op.getOperand(0).getOpcode() == X86ISD::SETCC &&
9563 Op.getOperand(0).hasOneUse();
9564 }
9565 return false;
9566}
9567
Dan Gohmand858e902010-04-17 15:26:15 +00009568SDValue X86TargetLowering::LowerBRCOND(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng734503b2006-09-11 02:19:56 +00009569 bool addTest = true;
Dan Gohman475871a2008-07-27 21:46:04 +00009570 SDValue Chain = Op.getOperand(0);
9571 SDValue Cond = Op.getOperand(1);
9572 SDValue Dest = Op.getOperand(2);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009573 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00009574 SDValue CC;
Dan Gohman65fd6562011-11-03 21:49:52 +00009575 bool Inverted = false;
Evan Cheng734503b2006-09-11 02:19:56 +00009576
Dan Gohman1a492952009-10-20 16:22:37 +00009577 if (Cond.getOpcode() == ISD::SETCC) {
Dan Gohman65fd6562011-11-03 21:49:52 +00009578 // Check for setcc([su]{add,sub,mul}o == 0).
9579 if (cast<CondCodeSDNode>(Cond.getOperand(2))->get() == ISD::SETEQ &&
9580 isa<ConstantSDNode>(Cond.getOperand(1)) &&
9581 cast<ConstantSDNode>(Cond.getOperand(1))->isNullValue() &&
9582 Cond.getOperand(0).getResNo() == 1 &&
9583 (Cond.getOperand(0).getOpcode() == ISD::SADDO ||
9584 Cond.getOperand(0).getOpcode() == ISD::UADDO ||
9585 Cond.getOperand(0).getOpcode() == ISD::SSUBO ||
9586 Cond.getOperand(0).getOpcode() == ISD::USUBO ||
9587 Cond.getOperand(0).getOpcode() == ISD::SMULO ||
9588 Cond.getOperand(0).getOpcode() == ISD::UMULO)) {
9589 Inverted = true;
9590 Cond = Cond.getOperand(0);
9591 } else {
9592 SDValue NewCond = LowerSETCC(Cond, DAG);
9593 if (NewCond.getNode())
9594 Cond = NewCond;
9595 }
Dan Gohman1a492952009-10-20 16:22:37 +00009596 }
Chris Lattnere55484e2008-12-25 05:34:37 +00009597#if 0
9598 // FIXME: LowerXALUO doesn't handle these!!
Bill Wendlingd350e022008-12-12 21:15:41 +00009599 else if (Cond.getOpcode() == X86ISD::ADD ||
9600 Cond.getOpcode() == X86ISD::SUB ||
9601 Cond.getOpcode() == X86ISD::SMUL ||
9602 Cond.getOpcode() == X86ISD::UMUL)
Bill Wendling74c37652008-12-09 22:08:41 +00009603 Cond = LowerXALUO(Cond, DAG);
Chris Lattnere55484e2008-12-25 05:34:37 +00009604#endif
Scott Michelfdc40a02009-02-17 22:15:04 +00009605
Evan Chengad9c0a32009-12-15 00:53:42 +00009606 // Look pass (and (setcc_carry (cmp ...)), 1).
9607 if (Cond.getOpcode() == ISD::AND &&
9608 Cond.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY) {
9609 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Cond.getOperand(1));
Michael J. Spencerec38de22010-10-10 22:04:20 +00009610 if (C && C->getAPIntValue() == 1)
Evan Chengad9c0a32009-12-15 00:53:42 +00009611 Cond = Cond.getOperand(0);
9612 }
9613
Evan Cheng3f41d662007-10-08 22:16:29 +00009614 // If condition flag is set by a X86ISD::CMP, then use it as the condition
9615 // setting operand in place of the X86ISD::SETCC.
Dan Gohman65fd6562011-11-03 21:49:52 +00009616 unsigned CondOpcode = Cond.getOpcode();
9617 if (CondOpcode == X86ISD::SETCC ||
9618 CondOpcode == X86ISD::SETCC_CARRY) {
Evan Cheng734503b2006-09-11 02:19:56 +00009619 CC = Cond.getOperand(0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00009620
Dan Gohman475871a2008-07-27 21:46:04 +00009621 SDValue Cmp = Cond.getOperand(1);
Evan Cheng734503b2006-09-11 02:19:56 +00009622 unsigned Opc = Cmp.getOpcode();
Chris Lattnere55484e2008-12-25 05:34:37 +00009623 // FIXME: WHY THE SPECIAL CASING OF LogicalCmp??
Dan Gohman076aee32009-03-04 19:44:21 +00009624 if (isX86LogicalCmp(Cmp) || Opc == X86ISD::BT) {
Evan Cheng3f41d662007-10-08 22:16:29 +00009625 Cond = Cmp;
Evan Cheng0488db92007-09-25 01:57:46 +00009626 addTest = false;
Bill Wendling61edeb52008-12-02 01:06:39 +00009627 } else {
Evan Cheng370e5342008-12-03 08:38:43 +00009628 switch (cast<ConstantSDNode>(CC)->getZExtValue()) {
Bill Wendling0ea25cb2008-12-03 08:32:02 +00009629 default: break;
9630 case X86::COND_O:
Dan Gohman653456c2009-01-07 00:15:08 +00009631 case X86::COND_B:
Chris Lattnere55484e2008-12-25 05:34:37 +00009632 // These can only come from an arithmetic instruction with overflow,
9633 // e.g. SADDO, UADDO.
Bill Wendling0ea25cb2008-12-03 08:32:02 +00009634 Cond = Cond.getNode()->getOperand(1);
9635 addTest = false;
9636 break;
Bill Wendling61edeb52008-12-02 01:06:39 +00009637 }
Evan Cheng0488db92007-09-25 01:57:46 +00009638 }
Dan Gohman65fd6562011-11-03 21:49:52 +00009639 }
9640 CondOpcode = Cond.getOpcode();
9641 if (CondOpcode == ISD::UADDO || CondOpcode == ISD::SADDO ||
9642 CondOpcode == ISD::USUBO || CondOpcode == ISD::SSUBO ||
9643 ((CondOpcode == ISD::UMULO || CondOpcode == ISD::SMULO) &&
9644 Cond.getOperand(0).getValueType() != MVT::i8)) {
9645 SDValue LHS = Cond.getOperand(0);
9646 SDValue RHS = Cond.getOperand(1);
9647 unsigned X86Opcode;
9648 unsigned X86Cond;
9649 SDVTList VTs;
9650 switch (CondOpcode) {
9651 case ISD::UADDO: X86Opcode = X86ISD::ADD; X86Cond = X86::COND_B; break;
9652 case ISD::SADDO: X86Opcode = X86ISD::ADD; X86Cond = X86::COND_O; break;
9653 case ISD::USUBO: X86Opcode = X86ISD::SUB; X86Cond = X86::COND_B; break;
9654 case ISD::SSUBO: X86Opcode = X86ISD::SUB; X86Cond = X86::COND_O; break;
9655 case ISD::UMULO: X86Opcode = X86ISD::UMUL; X86Cond = X86::COND_O; break;
9656 case ISD::SMULO: X86Opcode = X86ISD::SMUL; X86Cond = X86::COND_O; break;
9657 default: llvm_unreachable("unexpected overflowing operator");
9658 }
9659 if (Inverted)
9660 X86Cond = X86::GetOppositeBranchCondition((X86::CondCode)X86Cond);
9661 if (CondOpcode == ISD::UMULO)
9662 VTs = DAG.getVTList(LHS.getValueType(), LHS.getValueType(),
9663 MVT::i32);
9664 else
9665 VTs = DAG.getVTList(LHS.getValueType(), MVT::i32);
9666
9667 SDValue X86Op = DAG.getNode(X86Opcode, dl, VTs, LHS, RHS);
9668
9669 if (CondOpcode == ISD::UMULO)
9670 Cond = X86Op.getValue(2);
9671 else
9672 Cond = X86Op.getValue(1);
9673
9674 CC = DAG.getConstant(X86Cond, MVT::i8);
9675 addTest = false;
Evan Cheng370e5342008-12-03 08:38:43 +00009676 } else {
9677 unsigned CondOpc;
9678 if (Cond.hasOneUse() && isAndOrOfSetCCs(Cond, CondOpc)) {
9679 SDValue Cmp = Cond.getOperand(0).getOperand(1);
Evan Cheng370e5342008-12-03 08:38:43 +00009680 if (CondOpc == ISD::OR) {
9681 // Also, recognize the pattern generated by an FCMP_UNE. We can emit
9682 // two branches instead of an explicit OR instruction with a
9683 // separate test.
9684 if (Cmp == Cond.getOperand(1).getOperand(1) &&
Dan Gohman076aee32009-03-04 19:44:21 +00009685 isX86LogicalCmp(Cmp)) {
Evan Cheng370e5342008-12-03 08:38:43 +00009686 CC = Cond.getOperand(0).getOperand(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009687 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Evan Cheng370e5342008-12-03 08:38:43 +00009688 Chain, Dest, CC, Cmp);
9689 CC = Cond.getOperand(1).getOperand(0);
9690 Cond = Cmp;
9691 addTest = false;
9692 }
9693 } else { // ISD::AND
9694 // Also, recognize the pattern generated by an FCMP_OEQ. We can emit
9695 // two branches instead of an explicit AND instruction with a
9696 // separate test. However, we only do this if this block doesn't
9697 // have a fall-through edge, because this requires an explicit
9698 // jmp when the condition is false.
9699 if (Cmp == Cond.getOperand(1).getOperand(1) &&
Dan Gohman076aee32009-03-04 19:44:21 +00009700 isX86LogicalCmp(Cmp) &&
Evan Cheng370e5342008-12-03 08:38:43 +00009701 Op.getNode()->hasOneUse()) {
9702 X86::CondCode CCode =
9703 (X86::CondCode)Cond.getOperand(0).getConstantOperandVal(0);
9704 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00009705 CC = DAG.getConstant(CCode, MVT::i8);
Dan Gohman027657d2010-06-18 15:30:29 +00009706 SDNode *User = *Op.getNode()->use_begin();
Evan Cheng370e5342008-12-03 08:38:43 +00009707 // Look for an unconditional branch following this conditional branch.
9708 // We need this because we need to reverse the successors in order
9709 // to implement FCMP_OEQ.
Dan Gohman027657d2010-06-18 15:30:29 +00009710 if (User->getOpcode() == ISD::BR) {
9711 SDValue FalseBB = User->getOperand(1);
9712 SDNode *NewBR =
9713 DAG.UpdateNodeOperands(User, User->getOperand(0), Dest);
Evan Cheng370e5342008-12-03 08:38:43 +00009714 assert(NewBR == User);
Nick Lewycky2a3ee5e2010-06-20 20:27:42 +00009715 (void)NewBR;
Evan Cheng370e5342008-12-03 08:38:43 +00009716 Dest = FalseBB;
Dan Gohman279c22e2008-10-21 03:29:32 +00009717
Dale Johannesene4d209d2009-02-03 20:21:25 +00009718 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Evan Cheng370e5342008-12-03 08:38:43 +00009719 Chain, Dest, CC, Cmp);
9720 X86::CondCode CCode =
9721 (X86::CondCode)Cond.getOperand(1).getConstantOperandVal(0);
9722 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00009723 CC = DAG.getConstant(CCode, MVT::i8);
Evan Cheng370e5342008-12-03 08:38:43 +00009724 Cond = Cmp;
9725 addTest = false;
9726 }
9727 }
Dan Gohman279c22e2008-10-21 03:29:32 +00009728 }
Evan Cheng67ad9db2009-02-02 08:07:36 +00009729 } else if (Cond.hasOneUse() && isXor1OfSetCC(Cond)) {
9730 // Recognize for xorb (setcc), 1 patterns. The xor inverts the condition.
9731 // It should be transformed during dag combiner except when the condition
9732 // is set by a arithmetics with overflow node.
9733 X86::CondCode CCode =
9734 (X86::CondCode)Cond.getOperand(0).getConstantOperandVal(0);
9735 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00009736 CC = DAG.getConstant(CCode, MVT::i8);
Evan Cheng67ad9db2009-02-02 08:07:36 +00009737 Cond = Cond.getOperand(0).getOperand(1);
9738 addTest = false;
Dan Gohman65fd6562011-11-03 21:49:52 +00009739 } else if (Cond.getOpcode() == ISD::SETCC &&
9740 cast<CondCodeSDNode>(Cond.getOperand(2))->get() == ISD::SETOEQ) {
9741 // For FCMP_OEQ, we can emit
9742 // two branches instead of an explicit AND instruction with a
9743 // separate test. However, we only do this if this block doesn't
9744 // have a fall-through edge, because this requires an explicit
9745 // jmp when the condition is false.
9746 if (Op.getNode()->hasOneUse()) {
9747 SDNode *User = *Op.getNode()->use_begin();
9748 // Look for an unconditional branch following this conditional branch.
9749 // We need this because we need to reverse the successors in order
9750 // to implement FCMP_OEQ.
9751 if (User->getOpcode() == ISD::BR) {
9752 SDValue FalseBB = User->getOperand(1);
9753 SDNode *NewBR =
9754 DAG.UpdateNodeOperands(User, User->getOperand(0), Dest);
9755 assert(NewBR == User);
9756 (void)NewBR;
9757 Dest = FalseBB;
9758
9759 SDValue Cmp = DAG.getNode(X86ISD::CMP, dl, MVT::i32,
9760 Cond.getOperand(0), Cond.getOperand(1));
Benjamin Kramer17c836c2012-04-27 12:07:43 +00009761 Cmp = ConvertCmpIfNecessary(Cmp, DAG);
Dan Gohman65fd6562011-11-03 21:49:52 +00009762 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
9763 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
9764 Chain, Dest, CC, Cmp);
9765 CC = DAG.getConstant(X86::COND_P, MVT::i8);
9766 Cond = Cmp;
9767 addTest = false;
9768 }
9769 }
9770 } else if (Cond.getOpcode() == ISD::SETCC &&
9771 cast<CondCodeSDNode>(Cond.getOperand(2))->get() == ISD::SETUNE) {
9772 // For FCMP_UNE, we can emit
9773 // two branches instead of an explicit AND instruction with a
9774 // separate test. However, we only do this if this block doesn't
9775 // have a fall-through edge, because this requires an explicit
9776 // jmp when the condition is false.
9777 if (Op.getNode()->hasOneUse()) {
9778 SDNode *User = *Op.getNode()->use_begin();
9779 // Look for an unconditional branch following this conditional branch.
9780 // We need this because we need to reverse the successors in order
9781 // to implement FCMP_UNE.
9782 if (User->getOpcode() == ISD::BR) {
9783 SDValue FalseBB = User->getOperand(1);
9784 SDNode *NewBR =
9785 DAG.UpdateNodeOperands(User, User->getOperand(0), Dest);
9786 assert(NewBR == User);
9787 (void)NewBR;
9788
9789 SDValue Cmp = DAG.getNode(X86ISD::CMP, dl, MVT::i32,
9790 Cond.getOperand(0), Cond.getOperand(1));
Benjamin Kramer17c836c2012-04-27 12:07:43 +00009791 Cmp = ConvertCmpIfNecessary(Cmp, DAG);
Dan Gohman65fd6562011-11-03 21:49:52 +00009792 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
9793 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
9794 Chain, Dest, CC, Cmp);
9795 CC = DAG.getConstant(X86::COND_NP, MVT::i8);
9796 Cond = Cmp;
9797 addTest = false;
9798 Dest = FalseBB;
9799 }
9800 }
Dan Gohman279c22e2008-10-21 03:29:32 +00009801 }
Evan Cheng0488db92007-09-25 01:57:46 +00009802 }
9803
9804 if (addTest) {
Evan Chengb64dd5f2012-08-07 22:21:00 +00009805 // Look pass the truncate if the high bits are known zero.
9806 if (isTruncWithZeroHighBitsInput(Cond, DAG))
9807 Cond = Cond.getOperand(0);
Evan Chengd40d03e2010-01-06 19:38:29 +00009808
9809 // We know the result of AND is compared against zero. Try to match
9810 // it to BT.
Michael J. Spencerec38de22010-10-10 22:04:20 +00009811 if (Cond.getOpcode() == ISD::AND && Cond.hasOneUse()) {
Evan Chengd40d03e2010-01-06 19:38:29 +00009812 SDValue NewSetCC = LowerToBT(Cond, ISD::SETNE, dl, DAG);
9813 if (NewSetCC.getNode()) {
9814 CC = NewSetCC.getOperand(0);
9815 Cond = NewSetCC.getOperand(1);
9816 addTest = false;
9817 }
9818 }
9819 }
9820
9821 if (addTest) {
Owen Anderson825b72b2009-08-11 20:47:22 +00009822 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng552f09a2010-04-26 19:06:11 +00009823 Cond = EmitTest(Cond, X86::COND_NE, DAG);
Evan Cheng0488db92007-09-25 01:57:46 +00009824 }
Benjamin Kramer17c836c2012-04-27 12:07:43 +00009825 Cond = ConvertCmpIfNecessary(Cond, DAG);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009826 return DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Dan Gohman279c22e2008-10-21 03:29:32 +00009827 Chain, Dest, CC, Cond);
Evan Cheng0488db92007-09-25 01:57:46 +00009828}
9829
Anton Korobeynikove060b532007-04-17 19:34:00 +00009830// Lower dynamic stack allocation to _alloca call for Cygwin/Mingw targets.
9831// Calls to _alloca is needed to probe the stack when allocating more than 4k
9832// bytes in one go. Touching the stack at 4K increments is necessary to ensure
9833// that the guard pages used by the OS virtual memory manager are allocated in
9834// correct sequence.
Dan Gohman475871a2008-07-27 21:46:04 +00009835SDValue
9836X86TargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00009837 SelectionDAG &DAG) const {
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009838 assert((Subtarget->isTargetCygMing() || Subtarget->isTargetWindows() ||
Nick Lewycky8a8d4792011-12-02 22:16:29 +00009839 getTargetMachine().Options.EnableSegmentedStacks) &&
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009840 "This should be used only on Windows targets or when segmented stacks "
Rafael Espindola96428ce2011-09-06 18:43:08 +00009841 "are being used");
9842 assert(!Subtarget->isTargetEnvMacho() && "Not implemented");
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009843 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov096b4612008-06-11 20:16:42 +00009844
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00009845 // Get the inputs.
Dan Gohman475871a2008-07-27 21:46:04 +00009846 SDValue Chain = Op.getOperand(0);
9847 SDValue Size = Op.getOperand(1);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00009848 // FIXME: Ensure alignment here
9849
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009850 bool Is64Bit = Subtarget->is64Bit();
9851 EVT SPTy = Is64Bit ? MVT::i64 : MVT::i32;
Anton Korobeynikov096b4612008-06-11 20:16:42 +00009852
Nick Lewycky8a8d4792011-12-02 22:16:29 +00009853 if (getTargetMachine().Options.EnableSegmentedStacks) {
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009854 MachineFunction &MF = DAG.getMachineFunction();
9855 MachineRegisterInfo &MRI = MF.getRegInfo();
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00009856
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009857 if (Is64Bit) {
9858 // The 64 bit implementation of segmented stacks needs to clobber both r10
Rafael Espindola96428ce2011-09-06 18:43:08 +00009859 // r11. This makes it impossible to use it along with nested parameters.
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009860 const Function *F = MF.getFunction();
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00009861
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009862 for (Function::const_arg_iterator I = F->arg_begin(), E = F->arg_end();
Craig Topper31a207a2012-05-04 06:39:13 +00009863 I != E; ++I)
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009864 if (I->hasNestAttr())
9865 report_fatal_error("Cannot use segmented stacks with functions that "
9866 "have nested arguments.");
9867 }
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00009868
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009869 const TargetRegisterClass *AddrRegClass =
9870 getRegClassFor(Subtarget->is64Bit() ? MVT::i64:MVT::i32);
9871 unsigned Vreg = MRI.createVirtualRegister(AddrRegClass);
9872 Chain = DAG.getCopyToReg(Chain, dl, Vreg, Size);
9873 SDValue Value = DAG.getNode(X86ISD::SEG_ALLOCA, dl, SPTy, Chain,
9874 DAG.getRegister(Vreg, SPTy));
9875 SDValue Ops1[2] = { Value, Chain };
9876 return DAG.getMergeValues(Ops1, 2, dl);
9877 } else {
9878 SDValue Flag;
9879 unsigned Reg = (Subtarget->is64Bit() ? X86::RAX : X86::EAX);
Anton Korobeynikov096b4612008-06-11 20:16:42 +00009880
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009881 Chain = DAG.getCopyToReg(Chain, dl, Reg, Size, Flag);
9882 Flag = Chain.getValue(1);
9883 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Anton Korobeynikov096b4612008-06-11 20:16:42 +00009884
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009885 Chain = DAG.getNode(X86ISD::WIN_ALLOCA, dl, NodeTys, Chain, Flag);
9886 Flag = Chain.getValue(1);
9887
Michael Liaoc5c970e2012-10-31 04:14:09 +00009888 Chain = DAG.getCopyFromReg(Chain, dl, RegInfo->getStackRegister(),
9889 SPTy).getValue(1);
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009890
9891 SDValue Ops1[2] = { Chain.getValue(0), Chain };
9892 return DAG.getMergeValues(Ops1, 2, dl);
9893 }
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00009894}
9895
Dan Gohmand858e902010-04-17 15:26:15 +00009896SDValue X86TargetLowering::LowerVASTART(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman1e93df62010-04-17 14:41:14 +00009897 MachineFunction &MF = DAG.getMachineFunction();
9898 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
9899
Dan Gohman69de1932008-02-06 22:27:42 +00009900 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Chris Lattner8026a9d2010-09-21 17:50:43 +00009901 DebugLoc DL = Op.getDebugLoc();
Evan Cheng8b2794a2006-10-13 21:14:26 +00009902
Anton Korobeynikove7beda12010-10-03 22:52:07 +00009903 if (!Subtarget->is64Bit() || Subtarget->isTargetWin64()) {
Evan Cheng25ab6902006-09-08 06:48:29 +00009904 // vastart just stores the address of the VarArgsFrameIndex slot into the
9905 // memory location argument.
Dan Gohman1e93df62010-04-17 14:41:14 +00009906 SDValue FR = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
9907 getPointerTy());
Chris Lattner8026a9d2010-09-21 17:50:43 +00009908 return DAG.getStore(Op.getOperand(0), DL, FR, Op.getOperand(1),
9909 MachinePointerInfo(SV), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00009910 }
9911
9912 // __va_list_tag:
9913 // gp_offset (0 - 6 * 8)
9914 // fp_offset (48 - 48 + 8 * 16)
9915 // overflow_arg_area (point to parameters coming in memory).
9916 // reg_save_area
Dan Gohman475871a2008-07-27 21:46:04 +00009917 SmallVector<SDValue, 8> MemOps;
9918 SDValue FIN = Op.getOperand(1);
Evan Cheng25ab6902006-09-08 06:48:29 +00009919 // Store gp_offset
Chris Lattner8026a9d2010-09-21 17:50:43 +00009920 SDValue Store = DAG.getStore(Op.getOperand(0), DL,
Dan Gohman1e93df62010-04-17 14:41:14 +00009921 DAG.getConstant(FuncInfo->getVarArgsGPOffset(),
9922 MVT::i32),
Chris Lattner8026a9d2010-09-21 17:50:43 +00009923 FIN, MachinePointerInfo(SV), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00009924 MemOps.push_back(Store);
9925
9926 // Store fp_offset
Chris Lattner8026a9d2010-09-21 17:50:43 +00009927 FIN = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00009928 FIN, DAG.getIntPtrConstant(4));
Chris Lattner8026a9d2010-09-21 17:50:43 +00009929 Store = DAG.getStore(Op.getOperand(0), DL,
Dan Gohman1e93df62010-04-17 14:41:14 +00009930 DAG.getConstant(FuncInfo->getVarArgsFPOffset(),
9931 MVT::i32),
Chris Lattner8026a9d2010-09-21 17:50:43 +00009932 FIN, MachinePointerInfo(SV, 4), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00009933 MemOps.push_back(Store);
9934
9935 // Store ptr to overflow_arg_area
Chris Lattner8026a9d2010-09-21 17:50:43 +00009936 FIN = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00009937 FIN, DAG.getIntPtrConstant(4));
Dan Gohman1e93df62010-04-17 14:41:14 +00009938 SDValue OVFIN = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
9939 getPointerTy());
Chris Lattner8026a9d2010-09-21 17:50:43 +00009940 Store = DAG.getStore(Op.getOperand(0), DL, OVFIN, FIN,
9941 MachinePointerInfo(SV, 8),
David Greene67c9d422010-02-15 16:53:33 +00009942 false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00009943 MemOps.push_back(Store);
9944
9945 // Store ptr to reg_save_area.
Chris Lattner8026a9d2010-09-21 17:50:43 +00009946 FIN = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00009947 FIN, DAG.getIntPtrConstant(8));
Dan Gohman1e93df62010-04-17 14:41:14 +00009948 SDValue RSFIN = DAG.getFrameIndex(FuncInfo->getRegSaveFrameIndex(),
9949 getPointerTy());
Chris Lattner8026a9d2010-09-21 17:50:43 +00009950 Store = DAG.getStore(Op.getOperand(0), DL, RSFIN, FIN,
9951 MachinePointerInfo(SV, 16), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00009952 MemOps.push_back(Store);
Chris Lattner8026a9d2010-09-21 17:50:43 +00009953 return DAG.getNode(ISD::TokenFactor, DL, MVT::Other,
Dale Johannesene4d209d2009-02-03 20:21:25 +00009954 &MemOps[0], MemOps.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00009955}
9956
Dan Gohmand858e902010-04-17 15:26:15 +00009957SDValue X86TargetLowering::LowerVAARG(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman320afb82010-10-12 18:00:49 +00009958 assert(Subtarget->is64Bit() &&
9959 "LowerVAARG only handles 64-bit va_arg!");
9960 assert((Subtarget->isTargetLinux() ||
9961 Subtarget->isTargetDarwin()) &&
9962 "Unhandled target in LowerVAARG");
9963 assert(Op.getNode()->getNumOperands() == 4);
9964 SDValue Chain = Op.getOperand(0);
9965 SDValue SrcPtr = Op.getOperand(1);
9966 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
9967 unsigned Align = Op.getConstantOperandVal(3);
9968 DebugLoc dl = Op.getDebugLoc();
Dan Gohman9018e832008-05-10 01:26:14 +00009969
Dan Gohman320afb82010-10-12 18:00:49 +00009970 EVT ArgVT = Op.getNode()->getValueType(0);
Chris Lattnerdb125cf2011-07-18 04:54:35 +00009971 Type *ArgTy = ArgVT.getTypeForEVT(*DAG.getContext());
Micah Villmow3574eca2012-10-08 16:38:25 +00009972 uint32_t ArgSize = getDataLayout()->getTypeAllocSize(ArgTy);
Dan Gohman320afb82010-10-12 18:00:49 +00009973 uint8_t ArgMode;
9974
9975 // Decide which area this value should be read from.
9976 // TODO: Implement the AMD64 ABI in its entirety. This simple
9977 // selection mechanism works only for the basic types.
9978 if (ArgVT == MVT::f80) {
9979 llvm_unreachable("va_arg for f80 not yet implemented");
9980 } else if (ArgVT.isFloatingPoint() && ArgSize <= 16 /*bytes*/) {
9981 ArgMode = 2; // Argument passed in XMM register. Use fp_offset.
9982 } else if (ArgVT.isInteger() && ArgSize <= 32 /*bytes*/) {
9983 ArgMode = 1; // Argument passed in GPR64 register(s). Use gp_offset.
9984 } else {
9985 llvm_unreachable("Unhandled argument type in LowerVAARG");
9986 }
9987
9988 if (ArgMode == 2) {
9989 // Sanity Check: Make sure using fp_offset makes sense.
Nick Lewycky8a8d4792011-12-02 22:16:29 +00009990 assert(!getTargetMachine().Options.UseSoftFloat &&
Eric Christopher52b45052010-10-12 19:44:17 +00009991 !(DAG.getMachineFunction()
Bill Wendling67658342012-10-09 07:45:08 +00009992 .getFunction()->getFnAttributes()
Bill Wendling034b94b2012-12-19 07:18:57 +00009993 .hasAttribute(Attribute::NoImplicitFloat)) &&
Craig Topper1accb7e2012-01-10 06:54:16 +00009994 Subtarget->hasSSE1());
Dan Gohman320afb82010-10-12 18:00:49 +00009995 }
9996
9997 // Insert VAARG_64 node into the DAG
9998 // VAARG_64 returns two values: Variable Argument Address, Chain
9999 SmallVector<SDValue, 11> InstOps;
10000 InstOps.push_back(Chain);
10001 InstOps.push_back(SrcPtr);
10002 InstOps.push_back(DAG.getConstant(ArgSize, MVT::i32));
10003 InstOps.push_back(DAG.getConstant(ArgMode, MVT::i8));
10004 InstOps.push_back(DAG.getConstant(Align, MVT::i32));
10005 SDVTList VTs = DAG.getVTList(getPointerTy(), MVT::Other);
10006 SDValue VAARG = DAG.getMemIntrinsicNode(X86ISD::VAARG_64, dl,
10007 VTs, &InstOps[0], InstOps.size(),
10008 MVT::i64,
10009 MachinePointerInfo(SV),
10010 /*Align=*/0,
10011 /*Volatile=*/false,
10012 /*ReadMem=*/true,
10013 /*WriteMem=*/true);
10014 Chain = VAARG.getValue(1);
10015
10016 // Load the next argument and return it
10017 return DAG.getLoad(ArgVT, dl,
10018 Chain,
10019 VAARG,
10020 MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000010021 false, false, false, 0);
Dan Gohman9018e832008-05-10 01:26:14 +000010022}
10023
Craig Topper55b24052012-09-11 06:15:32 +000010024static SDValue LowerVACOPY(SDValue Op, const X86Subtarget *Subtarget,
10025 SelectionDAG &DAG) {
Evan Chengae642192007-03-02 23:16:35 +000010026 // X86-64 va_list is a struct { i32, i32, i8*, i8* }.
Dan Gohman28269132008-04-18 20:55:41 +000010027 assert(Subtarget->is64Bit() && "This code only handles 64-bit va_copy!");
Dan Gohman475871a2008-07-27 21:46:04 +000010028 SDValue Chain = Op.getOperand(0);
10029 SDValue DstPtr = Op.getOperand(1);
10030 SDValue SrcPtr = Op.getOperand(2);
Dan Gohman69de1932008-02-06 22:27:42 +000010031 const Value *DstSV = cast<SrcValueSDNode>(Op.getOperand(3))->getValue();
10032 const Value *SrcSV = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Chris Lattnere72f2022010-09-21 05:40:29 +000010033 DebugLoc DL = Op.getDebugLoc();
Evan Chengae642192007-03-02 23:16:35 +000010034
Chris Lattnere72f2022010-09-21 05:40:29 +000010035 return DAG.getMemcpy(Chain, DL, DstPtr, SrcPtr,
Mon P Wang20adc9d2010-04-04 03:10:48 +000010036 DAG.getIntPtrConstant(24), 8, /*isVolatile*/false,
Michael J. Spencerec38de22010-10-10 22:04:20 +000010037 false,
Chris Lattnere72f2022010-09-21 05:40:29 +000010038 MachinePointerInfo(DstSV), MachinePointerInfo(SrcSV));
Evan Chengae642192007-03-02 23:16:35 +000010039}
10040
Craig Topper80e46362012-01-23 06:16:53 +000010041// getTargetVShiftNOde - Handle vector element shifts where the shift amount
10042// may or may not be a constant. Takes immediate version of shift as input.
10043static SDValue getTargetVShiftNode(unsigned Opc, DebugLoc dl, EVT VT,
10044 SDValue SrcOp, SDValue ShAmt,
10045 SelectionDAG &DAG) {
10046 assert(ShAmt.getValueType() == MVT::i32 && "ShAmt is not i32");
10047
10048 if (isa<ConstantSDNode>(ShAmt)) {
Nadav Rotemd896e242012-07-15 20:27:43 +000010049 // Constant may be a TargetConstant. Use a regular constant.
10050 uint32_t ShiftAmt = cast<ConstantSDNode>(ShAmt)->getZExtValue();
Craig Topper80e46362012-01-23 06:16:53 +000010051 switch (Opc) {
10052 default: llvm_unreachable("Unknown target vector shift node");
10053 case X86ISD::VSHLI:
10054 case X86ISD::VSRLI:
10055 case X86ISD::VSRAI:
Nadav Rotemd896e242012-07-15 20:27:43 +000010056 return DAG.getNode(Opc, dl, VT, SrcOp,
10057 DAG.getConstant(ShiftAmt, MVT::i32));
Craig Topper80e46362012-01-23 06:16:53 +000010058 }
10059 }
10060
10061 // Change opcode to non-immediate version
10062 switch (Opc) {
10063 default: llvm_unreachable("Unknown target vector shift node");
10064 case X86ISD::VSHLI: Opc = X86ISD::VSHL; break;
10065 case X86ISD::VSRLI: Opc = X86ISD::VSRL; break;
10066 case X86ISD::VSRAI: Opc = X86ISD::VSRA; break;
10067 }
10068
10069 // Need to build a vector containing shift amount
10070 // Shift amount is 32-bits, but SSE instructions read 64-bit, so fill with 0
10071 SDValue ShOps[4];
10072 ShOps[0] = ShAmt;
10073 ShOps[1] = DAG.getConstant(0, MVT::i32);
Craig Topper6d688152012-08-14 07:43:25 +000010074 ShOps[2] = ShOps[3] = DAG.getUNDEF(MVT::i32);
Craig Topper80e46362012-01-23 06:16:53 +000010075 ShAmt = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, &ShOps[0], 4);
Nadav Rotem65f489f2012-07-14 22:26:05 +000010076
10077 // The return type has to be a 128-bit type with the same element
10078 // type as the input type.
10079 MVT EltVT = VT.getVectorElementType().getSimpleVT();
10080 EVT ShVT = MVT::getVectorVT(EltVT, 128/EltVT.getSizeInBits());
10081
10082 ShAmt = DAG.getNode(ISD::BITCAST, dl, ShVT, ShAmt);
Craig Topper80e46362012-01-23 06:16:53 +000010083 return DAG.getNode(Opc, dl, VT, SrcOp, ShAmt);
10084}
10085
Craig Topper55b24052012-09-11 06:15:32 +000010086static SDValue LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG) {
Dale Johannesen6f38cb62009-02-07 19:59:05 +000010087 DebugLoc dl = Op.getDebugLoc();
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +000010088 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +000010089 switch (IntNo) {
Dan Gohman475871a2008-07-27 21:46:04 +000010090 default: return SDValue(); // Don't custom lower most intrinsics.
Evan Cheng5759f972008-05-04 09:15:50 +000010091 // Comparison intrinsics.
Evan Cheng0db9fe62006-04-25 20:13:52 +000010092 case Intrinsic::x86_sse_comieq_ss:
10093 case Intrinsic::x86_sse_comilt_ss:
10094 case Intrinsic::x86_sse_comile_ss:
10095 case Intrinsic::x86_sse_comigt_ss:
10096 case Intrinsic::x86_sse_comige_ss:
10097 case Intrinsic::x86_sse_comineq_ss:
10098 case Intrinsic::x86_sse_ucomieq_ss:
10099 case Intrinsic::x86_sse_ucomilt_ss:
10100 case Intrinsic::x86_sse_ucomile_ss:
10101 case Intrinsic::x86_sse_ucomigt_ss:
10102 case Intrinsic::x86_sse_ucomige_ss:
10103 case Intrinsic::x86_sse_ucomineq_ss:
10104 case Intrinsic::x86_sse2_comieq_sd:
10105 case Intrinsic::x86_sse2_comilt_sd:
10106 case Intrinsic::x86_sse2_comile_sd:
10107 case Intrinsic::x86_sse2_comigt_sd:
10108 case Intrinsic::x86_sse2_comige_sd:
10109 case Intrinsic::x86_sse2_comineq_sd:
10110 case Intrinsic::x86_sse2_ucomieq_sd:
10111 case Intrinsic::x86_sse2_ucomilt_sd:
10112 case Intrinsic::x86_sse2_ucomile_sd:
10113 case Intrinsic::x86_sse2_ucomigt_sd:
10114 case Intrinsic::x86_sse2_ucomige_sd:
10115 case Intrinsic::x86_sse2_ucomineq_sd: {
Craig Topper6d688152012-08-14 07:43:25 +000010116 unsigned Opc;
10117 ISD::CondCode CC;
Evan Cheng0db9fe62006-04-25 20:13:52 +000010118 switch (IntNo) {
Craig Topper86c7c582012-01-30 01:10:15 +000010119 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000010120 case Intrinsic::x86_sse_comieq_ss:
10121 case Intrinsic::x86_sse2_comieq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +000010122 Opc = X86ISD::COMI;
10123 CC = ISD::SETEQ;
10124 break;
Evan Cheng6be2c582006-04-05 23:38:46 +000010125 case Intrinsic::x86_sse_comilt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +000010126 case Intrinsic::x86_sse2_comilt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +000010127 Opc = X86ISD::COMI;
10128 CC = ISD::SETLT;
10129 break;
10130 case Intrinsic::x86_sse_comile_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +000010131 case Intrinsic::x86_sse2_comile_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +000010132 Opc = X86ISD::COMI;
10133 CC = ISD::SETLE;
10134 break;
10135 case Intrinsic::x86_sse_comigt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +000010136 case Intrinsic::x86_sse2_comigt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +000010137 Opc = X86ISD::COMI;
10138 CC = ISD::SETGT;
10139 break;
10140 case Intrinsic::x86_sse_comige_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +000010141 case Intrinsic::x86_sse2_comige_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +000010142 Opc = X86ISD::COMI;
10143 CC = ISD::SETGE;
10144 break;
10145 case Intrinsic::x86_sse_comineq_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +000010146 case Intrinsic::x86_sse2_comineq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +000010147 Opc = X86ISD::COMI;
10148 CC = ISD::SETNE;
10149 break;
10150 case Intrinsic::x86_sse_ucomieq_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +000010151 case Intrinsic::x86_sse2_ucomieq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +000010152 Opc = X86ISD::UCOMI;
10153 CC = ISD::SETEQ;
10154 break;
10155 case Intrinsic::x86_sse_ucomilt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +000010156 case Intrinsic::x86_sse2_ucomilt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +000010157 Opc = X86ISD::UCOMI;
10158 CC = ISD::SETLT;
10159 break;
10160 case Intrinsic::x86_sse_ucomile_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +000010161 case Intrinsic::x86_sse2_ucomile_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +000010162 Opc = X86ISD::UCOMI;
10163 CC = ISD::SETLE;
10164 break;
10165 case Intrinsic::x86_sse_ucomigt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +000010166 case Intrinsic::x86_sse2_ucomigt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +000010167 Opc = X86ISD::UCOMI;
10168 CC = ISD::SETGT;
10169 break;
10170 case Intrinsic::x86_sse_ucomige_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +000010171 case Intrinsic::x86_sse2_ucomige_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +000010172 Opc = X86ISD::UCOMI;
10173 CC = ISD::SETGE;
10174 break;
10175 case Intrinsic::x86_sse_ucomineq_ss:
10176 case Intrinsic::x86_sse2_ucomineq_sd:
10177 Opc = X86ISD::UCOMI;
10178 CC = ISD::SETNE;
10179 break;
Evan Cheng6be2c582006-04-05 23:38:46 +000010180 }
Evan Cheng734503b2006-09-11 02:19:56 +000010181
Dan Gohman475871a2008-07-27 21:46:04 +000010182 SDValue LHS = Op.getOperand(1);
10183 SDValue RHS = Op.getOperand(2);
Chris Lattner1c39d4c2008-12-24 23:53:05 +000010184 unsigned X86CC = TranslateX86CC(CC, true, LHS, RHS, DAG);
Dan Gohman1a492952009-10-20 16:22:37 +000010185 assert(X86CC != X86::COND_INVALID && "Unexpected illegal condition!");
Owen Anderson825b72b2009-08-11 20:47:22 +000010186 SDValue Cond = DAG.getNode(Opc, dl, MVT::i32, LHS, RHS);
10187 SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
10188 DAG.getConstant(X86CC, MVT::i8), Cond);
10189 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC);
Evan Cheng6be2c582006-04-05 23:38:46 +000010190 }
Craig Topper6d688152012-08-14 07:43:25 +000010191
Duncan Sands04aa4ae2011-09-23 16:10:22 +000010192 // Arithmetic intrinsics.
Craig Topper5b209e82012-02-05 03:14:49 +000010193 case Intrinsic::x86_sse2_pmulu_dq:
10194 case Intrinsic::x86_avx2_pmulu_dq:
10195 return DAG.getNode(X86ISD::PMULUDQ, dl, Op.getValueType(),
10196 Op.getOperand(1), Op.getOperand(2));
Craig Topper6d688152012-08-14 07:43:25 +000010197
Benjamin Kramer388fc6a2012-12-15 16:47:44 +000010198 // SSE2/AVX2 sub with unsigned saturation intrinsics
10199 case Intrinsic::x86_sse2_psubus_b:
10200 case Intrinsic::x86_sse2_psubus_w:
10201 case Intrinsic::x86_avx2_psubus_b:
10202 case Intrinsic::x86_avx2_psubus_w:
10203 return DAG.getNode(X86ISD::SUBUS, dl, Op.getValueType(),
10204 Op.getOperand(1), Op.getOperand(2));
10205
Craig Topper6d688152012-08-14 07:43:25 +000010206 // SSE3/AVX horizontal add/sub intrinsics
Duncan Sands04aa4ae2011-09-23 16:10:22 +000010207 case Intrinsic::x86_sse3_hadd_ps:
10208 case Intrinsic::x86_sse3_hadd_pd:
10209 case Intrinsic::x86_avx_hadd_ps_256:
10210 case Intrinsic::x86_avx_hadd_pd_256:
Duncan Sands04aa4ae2011-09-23 16:10:22 +000010211 case Intrinsic::x86_sse3_hsub_ps:
10212 case Intrinsic::x86_sse3_hsub_pd:
10213 case Intrinsic::x86_avx_hsub_ps_256:
10214 case Intrinsic::x86_avx_hsub_pd_256:
Craig Topper4bb3f342012-01-25 05:37:32 +000010215 case Intrinsic::x86_ssse3_phadd_w_128:
10216 case Intrinsic::x86_ssse3_phadd_d_128:
10217 case Intrinsic::x86_avx2_phadd_w:
10218 case Intrinsic::x86_avx2_phadd_d:
Craig Topper4bb3f342012-01-25 05:37:32 +000010219 case Intrinsic::x86_ssse3_phsub_w_128:
10220 case Intrinsic::x86_ssse3_phsub_d_128:
10221 case Intrinsic::x86_avx2_phsub_w:
Craig Topper6d688152012-08-14 07:43:25 +000010222 case Intrinsic::x86_avx2_phsub_d: {
10223 unsigned Opcode;
10224 switch (IntNo) {
10225 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
10226 case Intrinsic::x86_sse3_hadd_ps:
10227 case Intrinsic::x86_sse3_hadd_pd:
10228 case Intrinsic::x86_avx_hadd_ps_256:
10229 case Intrinsic::x86_avx_hadd_pd_256:
10230 Opcode = X86ISD::FHADD;
10231 break;
10232 case Intrinsic::x86_sse3_hsub_ps:
10233 case Intrinsic::x86_sse3_hsub_pd:
10234 case Intrinsic::x86_avx_hsub_ps_256:
10235 case Intrinsic::x86_avx_hsub_pd_256:
10236 Opcode = X86ISD::FHSUB;
10237 break;
10238 case Intrinsic::x86_ssse3_phadd_w_128:
10239 case Intrinsic::x86_ssse3_phadd_d_128:
10240 case Intrinsic::x86_avx2_phadd_w:
10241 case Intrinsic::x86_avx2_phadd_d:
10242 Opcode = X86ISD::HADD;
10243 break;
10244 case Intrinsic::x86_ssse3_phsub_w_128:
10245 case Intrinsic::x86_ssse3_phsub_d_128:
10246 case Intrinsic::x86_avx2_phsub_w:
10247 case Intrinsic::x86_avx2_phsub_d:
10248 Opcode = X86ISD::HSUB;
10249 break;
10250 }
10251 return DAG.getNode(Opcode, dl, Op.getValueType(),
Craig Topper4bb3f342012-01-25 05:37:32 +000010252 Op.getOperand(1), Op.getOperand(2));
Craig Topper6d688152012-08-14 07:43:25 +000010253 }
10254
Benjamin Kramer739c7a82012-12-21 14:04:55 +000010255 // SSE2/SSE41/AVX2 integer max/min intrinsics.
10256 case Intrinsic::x86_sse2_pmaxu_b:
10257 case Intrinsic::x86_sse41_pmaxuw:
10258 case Intrinsic::x86_sse41_pmaxud:
10259 case Intrinsic::x86_avx2_pmaxu_b:
10260 case Intrinsic::x86_avx2_pmaxu_w:
10261 case Intrinsic::x86_avx2_pmaxu_d:
10262 return DAG.getNode(X86ISD::UMAX, dl, Op.getValueType(),
10263 Op.getOperand(1), Op.getOperand(2));
10264 case Intrinsic::x86_sse2_pminu_b:
10265 case Intrinsic::x86_sse41_pminuw:
10266 case Intrinsic::x86_sse41_pminud:
10267 case Intrinsic::x86_avx2_pminu_b:
10268 case Intrinsic::x86_avx2_pminu_w:
10269 case Intrinsic::x86_avx2_pminu_d:
10270 return DAG.getNode(X86ISD::UMIN, dl, Op.getValueType(),
10271 Op.getOperand(1), Op.getOperand(2));
10272 case Intrinsic::x86_sse41_pmaxsb:
10273 case Intrinsic::x86_sse2_pmaxs_w:
10274 case Intrinsic::x86_sse41_pmaxsd:
10275 case Intrinsic::x86_avx2_pmaxs_b:
10276 case Intrinsic::x86_avx2_pmaxs_w:
10277 case Intrinsic::x86_avx2_pmaxs_d:
10278 return DAG.getNode(X86ISD::SMAX, dl, Op.getValueType(),
10279 Op.getOperand(1), Op.getOperand(2));
10280 case Intrinsic::x86_sse41_pminsb:
10281 case Intrinsic::x86_sse2_pmins_w:
10282 case Intrinsic::x86_sse41_pminsd:
10283 case Intrinsic::x86_avx2_pmins_b:
10284 case Intrinsic::x86_avx2_pmins_w:
10285 case Intrinsic::x86_avx2_pmins_d:
10286 return DAG.getNode(X86ISD::SMIN, dl, Op.getValueType(),
10287 Op.getOperand(1), Op.getOperand(2));
10288
Craig Topper6d688152012-08-14 07:43:25 +000010289 // AVX2 variable shift intrinsics
Craig Topper98fc7292011-11-19 17:46:46 +000010290 case Intrinsic::x86_avx2_psllv_d:
10291 case Intrinsic::x86_avx2_psllv_q:
10292 case Intrinsic::x86_avx2_psllv_d_256:
10293 case Intrinsic::x86_avx2_psllv_q_256:
Craig Topper98fc7292011-11-19 17:46:46 +000010294 case Intrinsic::x86_avx2_psrlv_d:
10295 case Intrinsic::x86_avx2_psrlv_q:
10296 case Intrinsic::x86_avx2_psrlv_d_256:
10297 case Intrinsic::x86_avx2_psrlv_q_256:
Craig Topper98fc7292011-11-19 17:46:46 +000010298 case Intrinsic::x86_avx2_psrav_d:
Craig Topper6d688152012-08-14 07:43:25 +000010299 case Intrinsic::x86_avx2_psrav_d_256: {
10300 unsigned Opcode;
10301 switch (IntNo) {
10302 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
10303 case Intrinsic::x86_avx2_psllv_d:
10304 case Intrinsic::x86_avx2_psllv_q:
10305 case Intrinsic::x86_avx2_psllv_d_256:
10306 case Intrinsic::x86_avx2_psllv_q_256:
10307 Opcode = ISD::SHL;
10308 break;
10309 case Intrinsic::x86_avx2_psrlv_d:
10310 case Intrinsic::x86_avx2_psrlv_q:
10311 case Intrinsic::x86_avx2_psrlv_d_256:
10312 case Intrinsic::x86_avx2_psrlv_q_256:
10313 Opcode = ISD::SRL;
10314 break;
10315 case Intrinsic::x86_avx2_psrav_d:
10316 case Intrinsic::x86_avx2_psrav_d_256:
10317 Opcode = ISD::SRA;
10318 break;
10319 }
10320 return DAG.getNode(Opcode, dl, Op.getValueType(),
10321 Op.getOperand(1), Op.getOperand(2));
10322 }
10323
Craig Topper969ba282012-01-25 06:43:11 +000010324 case Intrinsic::x86_ssse3_pshuf_b_128:
10325 case Intrinsic::x86_avx2_pshuf_b:
10326 return DAG.getNode(X86ISD::PSHUFB, dl, Op.getValueType(),
10327 Op.getOperand(1), Op.getOperand(2));
Craig Topper6d688152012-08-14 07:43:25 +000010328
Craig Topper969ba282012-01-25 06:43:11 +000010329 case Intrinsic::x86_ssse3_psign_b_128:
10330 case Intrinsic::x86_ssse3_psign_w_128:
10331 case Intrinsic::x86_ssse3_psign_d_128:
10332 case Intrinsic::x86_avx2_psign_b:
10333 case Intrinsic::x86_avx2_psign_w:
10334 case Intrinsic::x86_avx2_psign_d:
10335 return DAG.getNode(X86ISD::PSIGN, dl, Op.getValueType(),
10336 Op.getOperand(1), Op.getOperand(2));
Craig Topper6d688152012-08-14 07:43:25 +000010337
Craig Toppere566cd02012-01-26 07:18:03 +000010338 case Intrinsic::x86_sse41_insertps:
10339 return DAG.getNode(X86ISD::INSERTPS, dl, Op.getValueType(),
10340 Op.getOperand(1), Op.getOperand(2), Op.getOperand(3));
Craig Topper6d688152012-08-14 07:43:25 +000010341
Craig Toppere566cd02012-01-26 07:18:03 +000010342 case Intrinsic::x86_avx_vperm2f128_ps_256:
10343 case Intrinsic::x86_avx_vperm2f128_pd_256:
10344 case Intrinsic::x86_avx_vperm2f128_si_256:
10345 case Intrinsic::x86_avx2_vperm2i128:
10346 return DAG.getNode(X86ISD::VPERM2X128, dl, Op.getValueType(),
10347 Op.getOperand(1), Op.getOperand(2), Op.getOperand(3));
Craig Topper6d688152012-08-14 07:43:25 +000010348
Craig Topperffa6c402012-04-16 07:13:00 +000010349 case Intrinsic::x86_avx2_permd:
10350 case Intrinsic::x86_avx2_permps:
10351 // Operands intentionally swapped. Mask is last operand to intrinsic,
10352 // but second operand for node/intruction.
10353 return DAG.getNode(X86ISD::VPERMV, dl, Op.getValueType(),
10354 Op.getOperand(2), Op.getOperand(1));
Craig Topper98fc7292011-11-19 17:46:46 +000010355
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +000010356 // ptest and testp intrinsics. The intrinsic these come from are designed to
10357 // return an integer value, not just an instruction so lower it to the ptest
10358 // or testp pattern and a setcc for the result.
Eric Christopher71c67532009-07-29 00:28:05 +000010359 case Intrinsic::x86_sse41_ptestz:
10360 case Intrinsic::x86_sse41_ptestc:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +000010361 case Intrinsic::x86_sse41_ptestnzc:
10362 case Intrinsic::x86_avx_ptestz_256:
10363 case Intrinsic::x86_avx_ptestc_256:
10364 case Intrinsic::x86_avx_ptestnzc_256:
10365 case Intrinsic::x86_avx_vtestz_ps:
10366 case Intrinsic::x86_avx_vtestc_ps:
10367 case Intrinsic::x86_avx_vtestnzc_ps:
10368 case Intrinsic::x86_avx_vtestz_pd:
10369 case Intrinsic::x86_avx_vtestc_pd:
10370 case Intrinsic::x86_avx_vtestnzc_pd:
10371 case Intrinsic::x86_avx_vtestz_ps_256:
10372 case Intrinsic::x86_avx_vtestc_ps_256:
10373 case Intrinsic::x86_avx_vtestnzc_ps_256:
10374 case Intrinsic::x86_avx_vtestz_pd_256:
10375 case Intrinsic::x86_avx_vtestc_pd_256:
10376 case Intrinsic::x86_avx_vtestnzc_pd_256: {
10377 bool IsTestPacked = false;
Craig Topper6d688152012-08-14 07:43:25 +000010378 unsigned X86CC;
Eric Christopher71c67532009-07-29 00:28:05 +000010379 switch (IntNo) {
Eric Christopher978dae32009-07-29 18:14:04 +000010380 default: llvm_unreachable("Bad fallthrough in Intrinsic lowering.");
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +000010381 case Intrinsic::x86_avx_vtestz_ps:
10382 case Intrinsic::x86_avx_vtestz_pd:
10383 case Intrinsic::x86_avx_vtestz_ps_256:
10384 case Intrinsic::x86_avx_vtestz_pd_256:
10385 IsTestPacked = true; // Fallthrough
Eric Christopher71c67532009-07-29 00:28:05 +000010386 case Intrinsic::x86_sse41_ptestz:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +000010387 case Intrinsic::x86_avx_ptestz_256:
Eric Christopher71c67532009-07-29 00:28:05 +000010388 // ZF = 1
10389 X86CC = X86::COND_E;
10390 break;
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +000010391 case Intrinsic::x86_avx_vtestc_ps:
10392 case Intrinsic::x86_avx_vtestc_pd:
10393 case Intrinsic::x86_avx_vtestc_ps_256:
10394 case Intrinsic::x86_avx_vtestc_pd_256:
10395 IsTestPacked = true; // Fallthrough
Eric Christopher71c67532009-07-29 00:28:05 +000010396 case Intrinsic::x86_sse41_ptestc:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +000010397 case Intrinsic::x86_avx_ptestc_256:
Eric Christopher71c67532009-07-29 00:28:05 +000010398 // CF = 1
10399 X86CC = X86::COND_B;
10400 break;
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +000010401 case Intrinsic::x86_avx_vtestnzc_ps:
10402 case Intrinsic::x86_avx_vtestnzc_pd:
10403 case Intrinsic::x86_avx_vtestnzc_ps_256:
10404 case Intrinsic::x86_avx_vtestnzc_pd_256:
10405 IsTestPacked = true; // Fallthrough
Eric Christopherfd179292009-08-27 18:07:15 +000010406 case Intrinsic::x86_sse41_ptestnzc:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +000010407 case Intrinsic::x86_avx_ptestnzc_256:
Eric Christopher71c67532009-07-29 00:28:05 +000010408 // ZF and CF = 0
10409 X86CC = X86::COND_A;
10410 break;
10411 }
Eric Christopherfd179292009-08-27 18:07:15 +000010412
Eric Christopher71c67532009-07-29 00:28:05 +000010413 SDValue LHS = Op.getOperand(1);
10414 SDValue RHS = Op.getOperand(2);
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +000010415 unsigned TestOpc = IsTestPacked ? X86ISD::TESTP : X86ISD::PTEST;
10416 SDValue Test = DAG.getNode(TestOpc, dl, MVT::i32, LHS, RHS);
Owen Anderson825b72b2009-08-11 20:47:22 +000010417 SDValue CC = DAG.getConstant(X86CC, MVT::i8);
10418 SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8, CC, Test);
10419 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC);
Eric Christopher71c67532009-07-29 00:28:05 +000010420 }
Evan Cheng5759f972008-05-04 09:15:50 +000010421
Craig Topper80e46362012-01-23 06:16:53 +000010422 // SSE/AVX shift intrinsics
10423 case Intrinsic::x86_sse2_psll_w:
10424 case Intrinsic::x86_sse2_psll_d:
10425 case Intrinsic::x86_sse2_psll_q:
10426 case Intrinsic::x86_avx2_psll_w:
10427 case Intrinsic::x86_avx2_psll_d:
10428 case Intrinsic::x86_avx2_psll_q:
Craig Topper80e46362012-01-23 06:16:53 +000010429 case Intrinsic::x86_sse2_psrl_w:
10430 case Intrinsic::x86_sse2_psrl_d:
10431 case Intrinsic::x86_sse2_psrl_q:
10432 case Intrinsic::x86_avx2_psrl_w:
10433 case Intrinsic::x86_avx2_psrl_d:
10434 case Intrinsic::x86_avx2_psrl_q:
Craig Topper80e46362012-01-23 06:16:53 +000010435 case Intrinsic::x86_sse2_psra_w:
10436 case Intrinsic::x86_sse2_psra_d:
10437 case Intrinsic::x86_avx2_psra_w:
Craig Topper6d688152012-08-14 07:43:25 +000010438 case Intrinsic::x86_avx2_psra_d: {
10439 unsigned Opcode;
10440 switch (IntNo) {
10441 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
10442 case Intrinsic::x86_sse2_psll_w:
10443 case Intrinsic::x86_sse2_psll_d:
10444 case Intrinsic::x86_sse2_psll_q:
10445 case Intrinsic::x86_avx2_psll_w:
10446 case Intrinsic::x86_avx2_psll_d:
10447 case Intrinsic::x86_avx2_psll_q:
10448 Opcode = X86ISD::VSHL;
10449 break;
10450 case Intrinsic::x86_sse2_psrl_w:
10451 case Intrinsic::x86_sse2_psrl_d:
10452 case Intrinsic::x86_sse2_psrl_q:
10453 case Intrinsic::x86_avx2_psrl_w:
10454 case Intrinsic::x86_avx2_psrl_d:
10455 case Intrinsic::x86_avx2_psrl_q:
10456 Opcode = X86ISD::VSRL;
10457 break;
10458 case Intrinsic::x86_sse2_psra_w:
10459 case Intrinsic::x86_sse2_psra_d:
10460 case Intrinsic::x86_avx2_psra_w:
10461 case Intrinsic::x86_avx2_psra_d:
10462 Opcode = X86ISD::VSRA;
10463 break;
10464 }
10465 return DAG.getNode(Opcode, dl, Op.getValueType(),
Craig Topper80e46362012-01-23 06:16:53 +000010466 Op.getOperand(1), Op.getOperand(2));
Craig Topper6d688152012-08-14 07:43:25 +000010467 }
10468
10469 // SSE/AVX immediate shift intrinsics
Evan Cheng5759f972008-05-04 09:15:50 +000010470 case Intrinsic::x86_sse2_pslli_w:
10471 case Intrinsic::x86_sse2_pslli_d:
10472 case Intrinsic::x86_sse2_pslli_q:
Craig Topper80e46362012-01-23 06:16:53 +000010473 case Intrinsic::x86_avx2_pslli_w:
10474 case Intrinsic::x86_avx2_pslli_d:
10475 case Intrinsic::x86_avx2_pslli_q:
Evan Cheng5759f972008-05-04 09:15:50 +000010476 case Intrinsic::x86_sse2_psrli_w:
10477 case Intrinsic::x86_sse2_psrli_d:
10478 case Intrinsic::x86_sse2_psrli_q:
Craig Topper80e46362012-01-23 06:16:53 +000010479 case Intrinsic::x86_avx2_psrli_w:
10480 case Intrinsic::x86_avx2_psrli_d:
10481 case Intrinsic::x86_avx2_psrli_q:
Evan Cheng5759f972008-05-04 09:15:50 +000010482 case Intrinsic::x86_sse2_psrai_w:
10483 case Intrinsic::x86_sse2_psrai_d:
Craig Topper80e46362012-01-23 06:16:53 +000010484 case Intrinsic::x86_avx2_psrai_w:
Craig Topper6d688152012-08-14 07:43:25 +000010485 case Intrinsic::x86_avx2_psrai_d: {
10486 unsigned Opcode;
10487 switch (IntNo) {
10488 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
10489 case Intrinsic::x86_sse2_pslli_w:
10490 case Intrinsic::x86_sse2_pslli_d:
10491 case Intrinsic::x86_sse2_pslli_q:
10492 case Intrinsic::x86_avx2_pslli_w:
10493 case Intrinsic::x86_avx2_pslli_d:
10494 case Intrinsic::x86_avx2_pslli_q:
10495 Opcode = X86ISD::VSHLI;
10496 break;
10497 case Intrinsic::x86_sse2_psrli_w:
10498 case Intrinsic::x86_sse2_psrli_d:
10499 case Intrinsic::x86_sse2_psrli_q:
10500 case Intrinsic::x86_avx2_psrli_w:
10501 case Intrinsic::x86_avx2_psrli_d:
10502 case Intrinsic::x86_avx2_psrli_q:
10503 Opcode = X86ISD::VSRLI;
10504 break;
10505 case Intrinsic::x86_sse2_psrai_w:
10506 case Intrinsic::x86_sse2_psrai_d:
10507 case Intrinsic::x86_avx2_psrai_w:
10508 case Intrinsic::x86_avx2_psrai_d:
10509 Opcode = X86ISD::VSRAI;
10510 break;
10511 }
10512 return getTargetVShiftNode(Opcode, dl, Op.getValueType(),
Craig Topper80e46362012-01-23 06:16:53 +000010513 Op.getOperand(1), Op.getOperand(2), DAG);
Craig Topper6d688152012-08-14 07:43:25 +000010514 }
10515
Craig Topper4feb6472012-08-06 06:22:36 +000010516 case Intrinsic::x86_sse42_pcmpistria128:
10517 case Intrinsic::x86_sse42_pcmpestria128:
10518 case Intrinsic::x86_sse42_pcmpistric128:
10519 case Intrinsic::x86_sse42_pcmpestric128:
10520 case Intrinsic::x86_sse42_pcmpistrio128:
10521 case Intrinsic::x86_sse42_pcmpestrio128:
10522 case Intrinsic::x86_sse42_pcmpistris128:
10523 case Intrinsic::x86_sse42_pcmpestris128:
10524 case Intrinsic::x86_sse42_pcmpistriz128:
10525 case Intrinsic::x86_sse42_pcmpestriz128: {
10526 unsigned Opcode;
10527 unsigned X86CC;
10528 switch (IntNo) {
10529 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
10530 case Intrinsic::x86_sse42_pcmpistria128:
10531 Opcode = X86ISD::PCMPISTRI;
10532 X86CC = X86::COND_A;
10533 break;
10534 case Intrinsic::x86_sse42_pcmpestria128:
10535 Opcode = X86ISD::PCMPESTRI;
10536 X86CC = X86::COND_A;
10537 break;
10538 case Intrinsic::x86_sse42_pcmpistric128:
10539 Opcode = X86ISD::PCMPISTRI;
10540 X86CC = X86::COND_B;
10541 break;
10542 case Intrinsic::x86_sse42_pcmpestric128:
10543 Opcode = X86ISD::PCMPESTRI;
10544 X86CC = X86::COND_B;
10545 break;
10546 case Intrinsic::x86_sse42_pcmpistrio128:
10547 Opcode = X86ISD::PCMPISTRI;
10548 X86CC = X86::COND_O;
10549 break;
10550 case Intrinsic::x86_sse42_pcmpestrio128:
10551 Opcode = X86ISD::PCMPESTRI;
10552 X86CC = X86::COND_O;
10553 break;
10554 case Intrinsic::x86_sse42_pcmpistris128:
10555 Opcode = X86ISD::PCMPISTRI;
10556 X86CC = X86::COND_S;
10557 break;
10558 case Intrinsic::x86_sse42_pcmpestris128:
10559 Opcode = X86ISD::PCMPESTRI;
10560 X86CC = X86::COND_S;
10561 break;
10562 case Intrinsic::x86_sse42_pcmpistriz128:
10563 Opcode = X86ISD::PCMPISTRI;
10564 X86CC = X86::COND_E;
10565 break;
10566 case Intrinsic::x86_sse42_pcmpestriz128:
10567 Opcode = X86ISD::PCMPESTRI;
10568 X86CC = X86::COND_E;
10569 break;
10570 }
10571 SmallVector<SDValue, 5> NewOps;
10572 NewOps.append(Op->op_begin()+1, Op->op_end());
10573 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::i32);
10574 SDValue PCMP = DAG.getNode(Opcode, dl, VTs, NewOps.data(), NewOps.size());
10575 SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
10576 DAG.getConstant(X86CC, MVT::i8),
10577 SDValue(PCMP.getNode(), 1));
10578 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC);
10579 }
Craig Topper6d688152012-08-14 07:43:25 +000010580
Craig Topper4feb6472012-08-06 06:22:36 +000010581 case Intrinsic::x86_sse42_pcmpistri128:
10582 case Intrinsic::x86_sse42_pcmpestri128: {
10583 unsigned Opcode;
10584 if (IntNo == Intrinsic::x86_sse42_pcmpistri128)
10585 Opcode = X86ISD::PCMPISTRI;
10586 else
10587 Opcode = X86ISD::PCMPESTRI;
10588
10589 SmallVector<SDValue, 5> NewOps;
10590 NewOps.append(Op->op_begin()+1, Op->op_end());
10591 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::i32);
10592 return DAG.getNode(Opcode, dl, VTs, NewOps.data(), NewOps.size());
10593 }
Craig Topper0e292372012-08-24 04:03:22 +000010594 case Intrinsic::x86_fma_vfmadd_ps:
10595 case Intrinsic::x86_fma_vfmadd_pd:
10596 case Intrinsic::x86_fma_vfmsub_ps:
10597 case Intrinsic::x86_fma_vfmsub_pd:
10598 case Intrinsic::x86_fma_vfnmadd_ps:
10599 case Intrinsic::x86_fma_vfnmadd_pd:
10600 case Intrinsic::x86_fma_vfnmsub_ps:
10601 case Intrinsic::x86_fma_vfnmsub_pd:
10602 case Intrinsic::x86_fma_vfmaddsub_ps:
10603 case Intrinsic::x86_fma_vfmaddsub_pd:
10604 case Intrinsic::x86_fma_vfmsubadd_ps:
10605 case Intrinsic::x86_fma_vfmsubadd_pd:
10606 case Intrinsic::x86_fma_vfmadd_ps_256:
10607 case Intrinsic::x86_fma_vfmadd_pd_256:
10608 case Intrinsic::x86_fma_vfmsub_ps_256:
10609 case Intrinsic::x86_fma_vfmsub_pd_256:
10610 case Intrinsic::x86_fma_vfnmadd_ps_256:
10611 case Intrinsic::x86_fma_vfnmadd_pd_256:
10612 case Intrinsic::x86_fma_vfnmsub_ps_256:
10613 case Intrinsic::x86_fma_vfnmsub_pd_256:
10614 case Intrinsic::x86_fma_vfmaddsub_ps_256:
10615 case Intrinsic::x86_fma_vfmaddsub_pd_256:
10616 case Intrinsic::x86_fma_vfmsubadd_ps_256:
10617 case Intrinsic::x86_fma_vfmsubadd_pd_256: {
Craig Topper0e292372012-08-24 04:03:22 +000010618 unsigned Opc;
10619 switch (IntNo) {
10620 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
10621 case Intrinsic::x86_fma_vfmadd_ps:
10622 case Intrinsic::x86_fma_vfmadd_pd:
10623 case Intrinsic::x86_fma_vfmadd_ps_256:
10624 case Intrinsic::x86_fma_vfmadd_pd_256:
10625 Opc = X86ISD::FMADD;
10626 break;
10627 case Intrinsic::x86_fma_vfmsub_ps:
10628 case Intrinsic::x86_fma_vfmsub_pd:
10629 case Intrinsic::x86_fma_vfmsub_ps_256:
10630 case Intrinsic::x86_fma_vfmsub_pd_256:
10631 Opc = X86ISD::FMSUB;
10632 break;
10633 case Intrinsic::x86_fma_vfnmadd_ps:
10634 case Intrinsic::x86_fma_vfnmadd_pd:
10635 case Intrinsic::x86_fma_vfnmadd_ps_256:
10636 case Intrinsic::x86_fma_vfnmadd_pd_256:
10637 Opc = X86ISD::FNMADD;
10638 break;
10639 case Intrinsic::x86_fma_vfnmsub_ps:
10640 case Intrinsic::x86_fma_vfnmsub_pd:
10641 case Intrinsic::x86_fma_vfnmsub_ps_256:
10642 case Intrinsic::x86_fma_vfnmsub_pd_256:
10643 Opc = X86ISD::FNMSUB;
10644 break;
10645 case Intrinsic::x86_fma_vfmaddsub_ps:
10646 case Intrinsic::x86_fma_vfmaddsub_pd:
10647 case Intrinsic::x86_fma_vfmaddsub_ps_256:
10648 case Intrinsic::x86_fma_vfmaddsub_pd_256:
10649 Opc = X86ISD::FMADDSUB;
10650 break;
10651 case Intrinsic::x86_fma_vfmsubadd_ps:
10652 case Intrinsic::x86_fma_vfmsubadd_pd:
10653 case Intrinsic::x86_fma_vfmsubadd_ps_256:
10654 case Intrinsic::x86_fma_vfmsubadd_pd_256:
10655 Opc = X86ISD::FMSUBADD;
10656 break;
10657 }
10658
10659 return DAG.getNode(Opc, dl, Op.getValueType(), Op.getOperand(1),
10660 Op.getOperand(2), Op.getOperand(3));
10661 }
Evan Cheng38bcbaf2005-12-23 07:31:11 +000010662 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000010663}
Evan Cheng72261582005-12-20 06:22:03 +000010664
Craig Topper55b24052012-09-11 06:15:32 +000010665static SDValue LowerINTRINSIC_W_CHAIN(SDValue Op, SelectionDAG &DAG) {
Benjamin Kramerb9bee042012-07-12 09:31:43 +000010666 DebugLoc dl = Op.getDebugLoc();
10667 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
10668 switch (IntNo) {
10669 default: return SDValue(); // Don't custom lower most intrinsics.
10670
10671 // RDRAND intrinsics.
10672 case Intrinsic::x86_rdrand_16:
10673 case Intrinsic::x86_rdrand_32:
10674 case Intrinsic::x86_rdrand_64: {
10675 // Emit the node with the right value type.
Benjamin Kramerfeae00a2012-07-12 18:14:57 +000010676 SDVTList VTs = DAG.getVTList(Op->getValueType(0), MVT::Glue, MVT::Other);
10677 SDValue Result = DAG.getNode(X86ISD::RDRAND, dl, VTs, Op.getOperand(0));
Benjamin Kramerb9bee042012-07-12 09:31:43 +000010678
10679 // If the value returned by RDRAND was valid (CF=1), return 1. Otherwise
10680 // return the value from Rand, which is always 0, casted to i32.
10681 SDValue Ops[] = { DAG.getZExtOrTrunc(Result, dl, Op->getValueType(1)),
10682 DAG.getConstant(1, Op->getValueType(1)),
10683 DAG.getConstant(X86::COND_B, MVT::i32),
10684 SDValue(Result.getNode(), 1) };
10685 SDValue isValid = DAG.getNode(X86ISD::CMOV, dl,
10686 DAG.getVTList(Op->getValueType(1), MVT::Glue),
10687 Ops, 4);
10688
10689 // Return { result, isValid, chain }.
10690 return DAG.getNode(ISD::MERGE_VALUES, dl, Op->getVTList(), Result, isValid,
Benjamin Kramerfeae00a2012-07-12 18:14:57 +000010691 SDValue(Result.getNode(), 2));
Benjamin Kramerb9bee042012-07-12 09:31:43 +000010692 }
10693 }
10694}
10695
Dan Gohmand858e902010-04-17 15:26:15 +000010696SDValue X86TargetLowering::LowerRETURNADDR(SDValue Op,
10697 SelectionDAG &DAG) const {
Evan Cheng2457f2c2010-05-22 01:47:14 +000010698 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
10699 MFI->setReturnAddressIsTaken(true);
10700
Bill Wendling64e87322009-01-16 19:25:27 +000010701 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Dale Johannesen6f38cb62009-02-07 19:59:05 +000010702 DebugLoc dl = Op.getDebugLoc();
Michael Liaoaa3c2c02012-10-25 06:29:14 +000010703 EVT PtrVT = getPointerTy();
Bill Wendling64e87322009-01-16 19:25:27 +000010704
10705 if (Depth > 0) {
10706 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
10707 SDValue Offset =
Michael Liaoaa3c2c02012-10-25 06:29:14 +000010708 DAG.getConstant(RegInfo->getSlotSize(), PtrVT);
10709 return DAG.getLoad(PtrVT, dl, DAG.getEntryNode(),
10710 DAG.getNode(ISD::ADD, dl, PtrVT,
Dale Johannesene4d209d2009-02-03 20:21:25 +000010711 FrameAddr, Offset),
Pete Cooperd752e0f2011-11-08 18:42:53 +000010712 MachinePointerInfo(), false, false, false, 0);
Bill Wendling64e87322009-01-16 19:25:27 +000010713 }
10714
10715 // Just load the return address.
Dan Gohman475871a2008-07-27 21:46:04 +000010716 SDValue RetAddrFI = getReturnAddressFrameIndex(DAG);
Michael Liaoaa3c2c02012-10-25 06:29:14 +000010717 return DAG.getLoad(PtrVT, dl, DAG.getEntryNode(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000010718 RetAddrFI, MachinePointerInfo(), false, false, false, 0);
Nate Begemanbcc5f362007-01-29 22:58:52 +000010719}
10720
Dan Gohmand858e902010-04-17 15:26:15 +000010721SDValue X86TargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng184793f2008-09-27 01:56:22 +000010722 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
10723 MFI->setFrameAddressIsTaken(true);
Evan Cheng2457f2c2010-05-22 01:47:14 +000010724
Owen Andersone50ed302009-08-10 22:56:29 +000010725 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +000010726 DebugLoc dl = Op.getDebugLoc(); // FIXME probably not meaningful
Evan Cheng184793f2008-09-27 01:56:22 +000010727 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
10728 unsigned FrameReg = Subtarget->is64Bit() ? X86::RBP : X86::EBP;
Dale Johannesendd64c412009-02-04 00:33:20 +000010729 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, VT);
Evan Cheng184793f2008-09-27 01:56:22 +000010730 while (Depth--)
Chris Lattner51abfe42010-09-21 06:02:19 +000010731 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr,
10732 MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000010733 false, false, false, 0);
Evan Cheng184793f2008-09-27 01:56:22 +000010734 return FrameAddr;
Nate Begemanbcc5f362007-01-29 22:58:52 +000010735}
10736
Dan Gohman475871a2008-07-27 21:46:04 +000010737SDValue X86TargetLowering::LowerFRAME_TO_ARGS_OFFSET(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +000010738 SelectionDAG &DAG) const {
Michael Liaoaa3c2c02012-10-25 06:29:14 +000010739 return DAG.getIntPtrConstant(2 * RegInfo->getSlotSize());
Anton Korobeynikov2365f512007-07-14 14:06:15 +000010740}
10741
Dan Gohmand858e902010-04-17 15:26:15 +000010742SDValue X86TargetLowering::LowerEH_RETURN(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +000010743 SDValue Chain = Op.getOperand(0);
10744 SDValue Offset = Op.getOperand(1);
10745 SDValue Handler = Op.getOperand(2);
Dale Johannesen6f38cb62009-02-07 19:59:05 +000010746 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov2365f512007-07-14 14:06:15 +000010747
Dan Gohmand8816272010-08-11 18:14:00 +000010748 SDValue Frame = DAG.getCopyFromReg(DAG.getEntryNode(), dl,
10749 Subtarget->is64Bit() ? X86::RBP : X86::EBP,
10750 getPointerTy());
Anton Korobeynikovb84c1672008-09-08 21:12:47 +000010751 unsigned StoreAddrReg = (Subtarget->is64Bit() ? X86::RCX : X86::ECX);
Anton Korobeynikov2365f512007-07-14 14:06:15 +000010752
Dan Gohmand8816272010-08-11 18:14:00 +000010753 SDValue StoreAddr = DAG.getNode(ISD::ADD, dl, getPointerTy(), Frame,
Michael Liaoaa3c2c02012-10-25 06:29:14 +000010754 DAG.getIntPtrConstant(RegInfo->getSlotSize()));
Dale Johannesene4d209d2009-02-03 20:21:25 +000010755 StoreAddr = DAG.getNode(ISD::ADD, dl, getPointerTy(), StoreAddr, Offset);
Chris Lattner8026a9d2010-09-21 17:50:43 +000010756 Chain = DAG.getStore(Chain, dl, Handler, StoreAddr, MachinePointerInfo(),
10757 false, false, 0);
Dale Johannesendd64c412009-02-04 00:33:20 +000010758 Chain = DAG.getCopyToReg(Chain, dl, StoreAddrReg, StoreAddr);
Anton Korobeynikov2365f512007-07-14 14:06:15 +000010759
Dale Johannesene4d209d2009-02-03 20:21:25 +000010760 return DAG.getNode(X86ISD::EH_RETURN, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +000010761 MVT::Other,
Anton Korobeynikovb84c1672008-09-08 21:12:47 +000010762 Chain, DAG.getRegister(StoreAddrReg, getPointerTy()));
Anton Korobeynikov2365f512007-07-14 14:06:15 +000010763}
10764
Michael Liao6c0e04c2012-10-15 22:39:43 +000010765SDValue X86TargetLowering::lowerEH_SJLJ_SETJMP(SDValue Op,
10766 SelectionDAG &DAG) const {
10767 DebugLoc DL = Op.getDebugLoc();
10768 return DAG.getNode(X86ISD::EH_SJLJ_SETJMP, DL,
10769 DAG.getVTList(MVT::i32, MVT::Other),
10770 Op.getOperand(0), Op.getOperand(1));
10771}
10772
10773SDValue X86TargetLowering::lowerEH_SJLJ_LONGJMP(SDValue Op,
10774 SelectionDAG &DAG) const {
10775 DebugLoc DL = Op.getDebugLoc();
10776 return DAG.getNode(X86ISD::EH_SJLJ_LONGJMP, DL, MVT::Other,
10777 Op.getOperand(0), Op.getOperand(1));
10778}
10779
Craig Topper55b24052012-09-11 06:15:32 +000010780static SDValue LowerADJUST_TRAMPOLINE(SDValue Op, SelectionDAG &DAG) {
Duncan Sands4a544a72011-09-06 13:37:06 +000010781 return Op.getOperand(0);
10782}
10783
10784SDValue X86TargetLowering::LowerINIT_TRAMPOLINE(SDValue Op,
10785 SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +000010786 SDValue Root = Op.getOperand(0);
10787 SDValue Trmp = Op.getOperand(1); // trampoline
10788 SDValue FPtr = Op.getOperand(2); // nested function
10789 SDValue Nest = Op.getOperand(3); // 'nest' parameter value
Dale Johannesen6f38cb62009-02-07 19:59:05 +000010790 DebugLoc dl = Op.getDebugLoc();
Duncan Sandsb116fac2007-07-27 20:02:49 +000010791
Dan Gohman69de1932008-02-06 22:27:42 +000010792 const Value *TrmpAddr = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Michael Liao7abf67a2012-10-04 19:50:43 +000010793 const TargetRegisterInfo* TRI = getTargetMachine().getRegisterInfo();
Duncan Sandsb116fac2007-07-27 20:02:49 +000010794
10795 if (Subtarget->is64Bit()) {
Dan Gohman475871a2008-07-27 21:46:04 +000010796 SDValue OutChains[6];
Duncan Sands339e14f2008-01-16 22:55:25 +000010797
10798 // Large code-model.
Chris Lattnera62fe662010-02-05 19:20:30 +000010799 const unsigned char JMP64r = 0xFF; // 64-bit jmp through register opcode.
10800 const unsigned char MOV64ri = 0xB8; // X86::MOV64ri opcode.
Duncan Sands339e14f2008-01-16 22:55:25 +000010801
Michael Liao7abf67a2012-10-04 19:50:43 +000010802 const unsigned char N86R10 = TRI->getEncodingValue(X86::R10) & 0x7;
10803 const unsigned char N86R11 = TRI->getEncodingValue(X86::R11) & 0x7;
Duncan Sands339e14f2008-01-16 22:55:25 +000010804
10805 const unsigned char REX_WB = 0x40 | 0x08 | 0x01; // REX prefix
10806
10807 // Load the pointer to the nested function into R11.
10808 unsigned OpCode = ((MOV64ri | N86R11) << 8) | REX_WB; // movabsq r11
Dan Gohman475871a2008-07-27 21:46:04 +000010809 SDValue Addr = Trmp;
Owen Anderson825b72b2009-08-11 20:47:22 +000010810 OutChains[0] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Chris Lattner8026a9d2010-09-21 17:50:43 +000010811 Addr, MachinePointerInfo(TrmpAddr),
10812 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +000010813
Owen Anderson825b72b2009-08-11 20:47:22 +000010814 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
10815 DAG.getConstant(2, MVT::i64));
Chris Lattner8026a9d2010-09-21 17:50:43 +000010816 OutChains[1] = DAG.getStore(Root, dl, FPtr, Addr,
10817 MachinePointerInfo(TrmpAddr, 2),
David Greene67c9d422010-02-15 16:53:33 +000010818 false, false, 2);
Duncan Sands339e14f2008-01-16 22:55:25 +000010819
10820 // Load the 'nest' parameter value into R10.
10821 // R10 is specified in X86CallingConv.td
10822 OpCode = ((MOV64ri | N86R10) << 8) | REX_WB; // movabsq r10
Owen Anderson825b72b2009-08-11 20:47:22 +000010823 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
10824 DAG.getConstant(10, MVT::i64));
10825 OutChains[2] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Chris Lattner8026a9d2010-09-21 17:50:43 +000010826 Addr, MachinePointerInfo(TrmpAddr, 10),
10827 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +000010828
Owen Anderson825b72b2009-08-11 20:47:22 +000010829 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
10830 DAG.getConstant(12, MVT::i64));
Chris Lattner8026a9d2010-09-21 17:50:43 +000010831 OutChains[3] = DAG.getStore(Root, dl, Nest, Addr,
10832 MachinePointerInfo(TrmpAddr, 12),
David Greene67c9d422010-02-15 16:53:33 +000010833 false, false, 2);
Duncan Sands339e14f2008-01-16 22:55:25 +000010834
10835 // Jump to the nested function.
10836 OpCode = (JMP64r << 8) | REX_WB; // jmpq *...
Owen Anderson825b72b2009-08-11 20:47:22 +000010837 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
10838 DAG.getConstant(20, MVT::i64));
10839 OutChains[4] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Chris Lattner8026a9d2010-09-21 17:50:43 +000010840 Addr, MachinePointerInfo(TrmpAddr, 20),
10841 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +000010842
10843 unsigned char ModRM = N86R11 | (4 << 3) | (3 << 6); // ...r11
Owen Anderson825b72b2009-08-11 20:47:22 +000010844 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
10845 DAG.getConstant(22, MVT::i64));
10846 OutChains[5] = DAG.getStore(Root, dl, DAG.getConstant(ModRM, MVT::i8), Addr,
Chris Lattner8026a9d2010-09-21 17:50:43 +000010847 MachinePointerInfo(TrmpAddr, 22),
10848 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +000010849
Duncan Sands4a544a72011-09-06 13:37:06 +000010850 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains, 6);
Duncan Sandsb116fac2007-07-27 20:02:49 +000010851 } else {
Dan Gohmanbbfb9c52008-01-31 01:01:48 +000010852 const Function *Func =
Duncan Sandsb116fac2007-07-27 20:02:49 +000010853 cast<Function>(cast<SrcValueSDNode>(Op.getOperand(5))->getValue());
Sandeep Patel65c3c8f2009-09-02 08:44:58 +000010854 CallingConv::ID CC = Func->getCallingConv();
Duncan Sandsee465742007-08-29 19:01:20 +000010855 unsigned NestReg;
Duncan Sandsb116fac2007-07-27 20:02:49 +000010856
10857 switch (CC) {
10858 default:
Torok Edwinc23197a2009-07-14 16:55:14 +000010859 llvm_unreachable("Unsupported calling convention");
Duncan Sandsb116fac2007-07-27 20:02:49 +000010860 case CallingConv::C:
Duncan Sandsb116fac2007-07-27 20:02:49 +000010861 case CallingConv::X86_StdCall: {
10862 // Pass 'nest' parameter in ECX.
10863 // Must be kept in sync with X86CallingConv.td
Duncan Sandsee465742007-08-29 19:01:20 +000010864 NestReg = X86::ECX;
Duncan Sandsb116fac2007-07-27 20:02:49 +000010865
10866 // Check that ECX wasn't needed by an 'inreg' parameter.
Chris Lattnerdb125cf2011-07-18 04:54:35 +000010867 FunctionType *FTy = Func->getFunctionType();
Bill Wendling99faa3b2012-12-07 23:16:57 +000010868 const AttributeSet &Attrs = Func->getAttributes();
Duncan Sandsb116fac2007-07-27 20:02:49 +000010869
Chris Lattner58d74912008-03-12 17:45:29 +000010870 if (!Attrs.isEmpty() && !Func->isVarArg()) {
Duncan Sandsb116fac2007-07-27 20:02:49 +000010871 unsigned InRegCount = 0;
10872 unsigned Idx = 1;
10873
10874 for (FunctionType::param_iterator I = FTy->param_begin(),
10875 E = FTy->param_end(); I != E; ++I, ++Idx)
Bill Wendling034b94b2012-12-19 07:18:57 +000010876 if (Attrs.getParamAttributes(Idx).hasAttribute(Attribute::InReg))
Duncan Sandsb116fac2007-07-27 20:02:49 +000010877 // FIXME: should only count parameters that are lowered to integers.
Anton Korobeynikovbff66b02008-09-09 18:22:57 +000010878 InRegCount += (TD->getTypeSizeInBits(*I) + 31) / 32;
Duncan Sandsb116fac2007-07-27 20:02:49 +000010879
10880 if (InRegCount > 2) {
Eric Christopher90eb4022010-07-22 00:26:08 +000010881 report_fatal_error("Nest register in use - reduce number of inreg"
10882 " parameters!");
Duncan Sandsb116fac2007-07-27 20:02:49 +000010883 }
10884 }
10885 break;
10886 }
10887 case CallingConv::X86_FastCall:
Anton Korobeynikovded05e32010-05-16 09:08:45 +000010888 case CallingConv::X86_ThisCall:
Duncan Sandsbf53c292008-09-10 13:22:10 +000010889 case CallingConv::Fast:
Duncan Sandsb116fac2007-07-27 20:02:49 +000010890 // Pass 'nest' parameter in EAX.
10891 // Must be kept in sync with X86CallingConv.td
Duncan Sandsee465742007-08-29 19:01:20 +000010892 NestReg = X86::EAX;
Duncan Sandsb116fac2007-07-27 20:02:49 +000010893 break;
10894 }
10895
Dan Gohman475871a2008-07-27 21:46:04 +000010896 SDValue OutChains[4];
10897 SDValue Addr, Disp;
Duncan Sandsb116fac2007-07-27 20:02:49 +000010898
Owen Anderson825b72b2009-08-11 20:47:22 +000010899 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
10900 DAG.getConstant(10, MVT::i32));
10901 Disp = DAG.getNode(ISD::SUB, dl, MVT::i32, FPtr, Addr);
Duncan Sandsb116fac2007-07-27 20:02:49 +000010902
Chris Lattnera62fe662010-02-05 19:20:30 +000010903 // This is storing the opcode for MOV32ri.
10904 const unsigned char MOV32ri = 0xB8; // X86::MOV32ri's opcode byte.
Michael Liao7abf67a2012-10-04 19:50:43 +000010905 const unsigned char N86Reg = TRI->getEncodingValue(NestReg) & 0x7;
Scott Michelfdc40a02009-02-17 22:15:04 +000010906 OutChains[0] = DAG.getStore(Root, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +000010907 DAG.getConstant(MOV32ri|N86Reg, MVT::i8),
Chris Lattner8026a9d2010-09-21 17:50:43 +000010908 Trmp, MachinePointerInfo(TrmpAddr),
10909 false, false, 0);
Duncan Sandsb116fac2007-07-27 20:02:49 +000010910
Owen Anderson825b72b2009-08-11 20:47:22 +000010911 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
10912 DAG.getConstant(1, MVT::i32));
Chris Lattner8026a9d2010-09-21 17:50:43 +000010913 OutChains[1] = DAG.getStore(Root, dl, Nest, Addr,
10914 MachinePointerInfo(TrmpAddr, 1),
David Greene67c9d422010-02-15 16:53:33 +000010915 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +000010916
Chris Lattnera62fe662010-02-05 19:20:30 +000010917 const unsigned char JMP = 0xE9; // jmp <32bit dst> opcode.
Owen Anderson825b72b2009-08-11 20:47:22 +000010918 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
10919 DAG.getConstant(5, MVT::i32));
10920 OutChains[2] = DAG.getStore(Root, dl, DAG.getConstant(JMP, MVT::i8), Addr,
Chris Lattner8026a9d2010-09-21 17:50:43 +000010921 MachinePointerInfo(TrmpAddr, 5),
10922 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +000010923
Owen Anderson825b72b2009-08-11 20:47:22 +000010924 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
10925 DAG.getConstant(6, MVT::i32));
Chris Lattner8026a9d2010-09-21 17:50:43 +000010926 OutChains[3] = DAG.getStore(Root, dl, Disp, Addr,
10927 MachinePointerInfo(TrmpAddr, 6),
David Greene67c9d422010-02-15 16:53:33 +000010928 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +000010929
Duncan Sands4a544a72011-09-06 13:37:06 +000010930 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains, 4);
Duncan Sandsb116fac2007-07-27 20:02:49 +000010931 }
10932}
10933
Dan Gohmand858e902010-04-17 15:26:15 +000010934SDValue X86TargetLowering::LowerFLT_ROUNDS_(SDValue Op,
10935 SelectionDAG &DAG) const {
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000010936 /*
10937 The rounding mode is in bits 11:10 of FPSR, and has the following
10938 settings:
10939 00 Round to nearest
10940 01 Round to -inf
10941 10 Round to +inf
10942 11 Round to 0
10943
10944 FLT_ROUNDS, on the other hand, expects the following:
10945 -1 Undefined
10946 0 Round to 0
10947 1 Round to nearest
10948 2 Round to +inf
10949 3 Round to -inf
10950
10951 To perform the conversion, we do:
10952 (((((FPSR & 0x800) >> 11) | ((FPSR & 0x400) >> 9)) + 1) & 3)
10953 */
10954
10955 MachineFunction &MF = DAG.getMachineFunction();
10956 const TargetMachine &TM = MF.getTarget();
Anton Korobeynikov16c29b52011-01-10 12:39:04 +000010957 const TargetFrameLowering &TFI = *TM.getFrameLowering();
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000010958 unsigned StackAlignment = TFI.getStackAlignment();
Owen Andersone50ed302009-08-10 22:56:29 +000010959 EVT VT = Op.getValueType();
Chris Lattner2156b792010-09-22 01:11:26 +000010960 DebugLoc DL = Op.getDebugLoc();
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000010961
10962 // Save FP Control Word to stack slot
David Greene3f2bf852009-11-12 20:49:22 +000010963 int SSFI = MF.getFrameInfo()->CreateStackObject(2, StackAlignment, false);
Dan Gohman475871a2008-07-27 21:46:04 +000010964 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000010965
Chris Lattner2156b792010-09-22 01:11:26 +000010966 MachineMemOperand *MMO =
10967 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
10968 MachineMemOperand::MOStore, 2, 2);
Michael J. Spencerec38de22010-10-10 22:04:20 +000010969
Chris Lattner2156b792010-09-22 01:11:26 +000010970 SDValue Ops[] = { DAG.getEntryNode(), StackSlot };
10971 SDValue Chain = DAG.getMemIntrinsicNode(X86ISD::FNSTCW16m, DL,
10972 DAG.getVTList(MVT::Other),
10973 Ops, 2, MVT::i16, MMO);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000010974
10975 // Load FP Control Word from stack slot
Chris Lattner2156b792010-09-22 01:11:26 +000010976 SDValue CWD = DAG.getLoad(MVT::i16, DL, Chain, StackSlot,
Pete Cooperd752e0f2011-11-08 18:42:53 +000010977 MachinePointerInfo(), false, false, false, 0);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000010978
10979 // Transform as necessary
Dan Gohman475871a2008-07-27 21:46:04 +000010980 SDValue CWD1 =
Chris Lattner2156b792010-09-22 01:11:26 +000010981 DAG.getNode(ISD::SRL, DL, MVT::i16,
10982 DAG.getNode(ISD::AND, DL, MVT::i16,
Owen Anderson825b72b2009-08-11 20:47:22 +000010983 CWD, DAG.getConstant(0x800, MVT::i16)),
10984 DAG.getConstant(11, MVT::i8));
Dan Gohman475871a2008-07-27 21:46:04 +000010985 SDValue CWD2 =
Chris Lattner2156b792010-09-22 01:11:26 +000010986 DAG.getNode(ISD::SRL, DL, MVT::i16,
10987 DAG.getNode(ISD::AND, DL, MVT::i16,
Owen Anderson825b72b2009-08-11 20:47:22 +000010988 CWD, DAG.getConstant(0x400, MVT::i16)),
10989 DAG.getConstant(9, MVT::i8));
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000010990
Dan Gohman475871a2008-07-27 21:46:04 +000010991 SDValue RetVal =
Chris Lattner2156b792010-09-22 01:11:26 +000010992 DAG.getNode(ISD::AND, DL, MVT::i16,
10993 DAG.getNode(ISD::ADD, DL, MVT::i16,
10994 DAG.getNode(ISD::OR, DL, MVT::i16, CWD1, CWD2),
Owen Anderson825b72b2009-08-11 20:47:22 +000010995 DAG.getConstant(1, MVT::i16)),
10996 DAG.getConstant(3, MVT::i16));
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000010997
Duncan Sands83ec4b62008-06-06 12:08:01 +000010998 return DAG.getNode((VT.getSizeInBits() < 16 ?
Chris Lattner2156b792010-09-22 01:11:26 +000010999 ISD::TRUNCATE : ISD::ZERO_EXTEND), DL, VT, RetVal);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000011000}
11001
Craig Topper55b24052012-09-11 06:15:32 +000011002static SDValue LowerCTLZ(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +000011003 EVT VT = Op.getValueType();
11004 EVT OpVT = VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +000011005 unsigned NumBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +000011006 DebugLoc dl = Op.getDebugLoc();
Evan Cheng18efe262007-12-14 02:13:44 +000011007
11008 Op = Op.getOperand(0);
Owen Anderson825b72b2009-08-11 20:47:22 +000011009 if (VT == MVT::i8) {
Evan Cheng152804e2007-12-14 08:30:15 +000011010 // Zero extend to i32 since there is not an i8 bsr.
Owen Anderson825b72b2009-08-11 20:47:22 +000011011 OpVT = MVT::i32;
Dale Johannesene4d209d2009-02-03 20:21:25 +000011012 Op = DAG.getNode(ISD::ZERO_EXTEND, dl, OpVT, Op);
Evan Cheng18efe262007-12-14 02:13:44 +000011013 }
Evan Cheng18efe262007-12-14 02:13:44 +000011014
Evan Cheng152804e2007-12-14 08:30:15 +000011015 // Issue a bsr (scan bits in reverse) which also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +000011016 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +000011017 Op = DAG.getNode(X86ISD::BSR, dl, VTs, Op);
Evan Cheng152804e2007-12-14 08:30:15 +000011018
11019 // If src is zero (i.e. bsr sets ZF), returns NumBits.
Benjamin Kramer7f1a5602009-12-29 16:57:26 +000011020 SDValue Ops[] = {
11021 Op,
11022 DAG.getConstant(NumBits+NumBits-1, OpVT),
11023 DAG.getConstant(X86::COND_E, MVT::i8),
11024 Op.getValue(1)
11025 };
11026 Op = DAG.getNode(X86ISD::CMOV, dl, OpVT, Ops, array_lengthof(Ops));
Evan Cheng152804e2007-12-14 08:30:15 +000011027
11028 // Finally xor with NumBits-1.
Dale Johannesene4d209d2009-02-03 20:21:25 +000011029 Op = DAG.getNode(ISD::XOR, dl, OpVT, Op, DAG.getConstant(NumBits-1, OpVT));
Evan Cheng152804e2007-12-14 08:30:15 +000011030
Owen Anderson825b72b2009-08-11 20:47:22 +000011031 if (VT == MVT::i8)
11032 Op = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Op);
Evan Cheng18efe262007-12-14 02:13:44 +000011033 return Op;
11034}
11035
Craig Topper55b24052012-09-11 06:15:32 +000011036static SDValue LowerCTLZ_ZERO_UNDEF(SDValue Op, SelectionDAG &DAG) {
Chandler Carruthacc068e2011-12-24 10:55:54 +000011037 EVT VT = Op.getValueType();
11038 EVT OpVT = VT;
11039 unsigned NumBits = VT.getSizeInBits();
11040 DebugLoc dl = Op.getDebugLoc();
11041
11042 Op = Op.getOperand(0);
11043 if (VT == MVT::i8) {
11044 // Zero extend to i32 since there is not an i8 bsr.
11045 OpVT = MVT::i32;
11046 Op = DAG.getNode(ISD::ZERO_EXTEND, dl, OpVT, Op);
11047 }
11048
11049 // Issue a bsr (scan bits in reverse).
11050 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
11051 Op = DAG.getNode(X86ISD::BSR, dl, VTs, Op);
11052
11053 // And xor with NumBits-1.
11054 Op = DAG.getNode(ISD::XOR, dl, OpVT, Op, DAG.getConstant(NumBits-1, OpVT));
11055
11056 if (VT == MVT::i8)
11057 Op = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Op);
11058 return Op;
11059}
11060
Craig Topper55b24052012-09-11 06:15:32 +000011061static SDValue LowerCTTZ(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +000011062 EVT VT = Op.getValueType();
Duncan Sands83ec4b62008-06-06 12:08:01 +000011063 unsigned NumBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +000011064 DebugLoc dl = Op.getDebugLoc();
Evan Cheng18efe262007-12-14 02:13:44 +000011065 Op = Op.getOperand(0);
Evan Cheng152804e2007-12-14 08:30:15 +000011066
11067 // Issue a bsf (scan bits forward) which also sets EFLAGS.
Chandler Carruth77821022011-12-24 12:12:34 +000011068 SDVTList VTs = DAG.getVTList(VT, MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +000011069 Op = DAG.getNode(X86ISD::BSF, dl, VTs, Op);
Evan Cheng152804e2007-12-14 08:30:15 +000011070
11071 // If src is zero (i.e. bsf sets ZF), returns NumBits.
Benjamin Kramer7f1a5602009-12-29 16:57:26 +000011072 SDValue Ops[] = {
11073 Op,
Chandler Carruth77821022011-12-24 12:12:34 +000011074 DAG.getConstant(NumBits, VT),
Benjamin Kramer7f1a5602009-12-29 16:57:26 +000011075 DAG.getConstant(X86::COND_E, MVT::i8),
11076 Op.getValue(1)
11077 };
Chandler Carruth77821022011-12-24 12:12:34 +000011078 return DAG.getNode(X86ISD::CMOV, dl, VT, Ops, array_lengthof(Ops));
Evan Cheng18efe262007-12-14 02:13:44 +000011079}
11080
Craig Topper13894fa2011-08-24 06:14:18 +000011081// Lower256IntArith - Break a 256-bit integer operation into two new 128-bit
11082// ones, and then concatenate the result back.
11083static SDValue Lower256IntArith(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +000011084 EVT VT = Op.getValueType();
Craig Topper13894fa2011-08-24 06:14:18 +000011085
Craig Topper7a9a28b2012-08-12 02:23:29 +000011086 assert(VT.is256BitVector() && VT.isInteger() &&
Craig Topper13894fa2011-08-24 06:14:18 +000011087 "Unsupported value type for operation");
11088
Craig Topper66ddd152012-04-27 22:54:43 +000011089 unsigned NumElems = VT.getVectorNumElements();
Craig Topper13894fa2011-08-24 06:14:18 +000011090 DebugLoc dl = Op.getDebugLoc();
Craig Topper13894fa2011-08-24 06:14:18 +000011091
11092 // Extract the LHS vectors
11093 SDValue LHS = Op.getOperand(0);
Craig Topperb14940a2012-04-22 20:55:18 +000011094 SDValue LHS1 = Extract128BitVector(LHS, 0, DAG, dl);
11095 SDValue LHS2 = Extract128BitVector(LHS, NumElems/2, DAG, dl);
Craig Topper13894fa2011-08-24 06:14:18 +000011096
11097 // Extract the RHS vectors
11098 SDValue RHS = Op.getOperand(1);
Craig Topperb14940a2012-04-22 20:55:18 +000011099 SDValue RHS1 = Extract128BitVector(RHS, 0, DAG, dl);
11100 SDValue RHS2 = Extract128BitVector(RHS, NumElems/2, DAG, dl);
Craig Topper13894fa2011-08-24 06:14:18 +000011101
11102 MVT EltVT = VT.getVectorElementType().getSimpleVT();
11103 EVT NewVT = MVT::getVectorVT(EltVT, NumElems/2);
11104
11105 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT,
11106 DAG.getNode(Op.getOpcode(), dl, NewVT, LHS1, RHS1),
11107 DAG.getNode(Op.getOpcode(), dl, NewVT, LHS2, RHS2));
11108}
11109
Craig Topper55b24052012-09-11 06:15:32 +000011110static SDValue LowerADD(SDValue Op, SelectionDAG &DAG) {
Craig Topper7a9a28b2012-08-12 02:23:29 +000011111 assert(Op.getValueType().is256BitVector() &&
Craig Topper13894fa2011-08-24 06:14:18 +000011112 Op.getValueType().isInteger() &&
11113 "Only handle AVX 256-bit vector integer operation");
11114 return Lower256IntArith(Op, DAG);
11115}
11116
Craig Topper55b24052012-09-11 06:15:32 +000011117static SDValue LowerSUB(SDValue Op, SelectionDAG &DAG) {
Craig Topper7a9a28b2012-08-12 02:23:29 +000011118 assert(Op.getValueType().is256BitVector() &&
Craig Topper13894fa2011-08-24 06:14:18 +000011119 Op.getValueType().isInteger() &&
11120 "Only handle AVX 256-bit vector integer operation");
11121 return Lower256IntArith(Op, DAG);
11122}
11123
Craig Topper55b24052012-09-11 06:15:32 +000011124static SDValue LowerMUL(SDValue Op, const X86Subtarget *Subtarget,
11125 SelectionDAG &DAG) {
Benjamin Kramer2f8a6cd2012-12-22 16:07:56 +000011126 DebugLoc dl = Op.getDebugLoc();
Craig Topper13894fa2011-08-24 06:14:18 +000011127 EVT VT = Op.getValueType();
11128
11129 // Decompose 256-bit ops into smaller 128-bit ops.
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000011130 if (VT.is256BitVector() && !Subtarget->hasInt256())
Craig Topper13894fa2011-08-24 06:14:18 +000011131 return Lower256IntArith(Op, DAG);
11132
Benjamin Kramer2f8a6cd2012-12-22 16:07:56 +000011133 SDValue A = Op.getOperand(0);
11134 SDValue B = Op.getOperand(1);
11135
11136 // Lower v4i32 mul as 2x shuffle, 2x pmuludq, 2x shuffle.
11137 if (VT == MVT::v4i32) {
11138 assert(Subtarget->hasSSE2() && !Subtarget->hasSSE41() &&
11139 "Should not custom lower when pmuldq is available!");
11140
11141 // Extract the odd parts.
11142 const int UnpackMask[] = { 1, -1, 3, -1 };
11143 SDValue Aodds = DAG.getVectorShuffle(VT, dl, A, A, UnpackMask);
11144 SDValue Bodds = DAG.getVectorShuffle(VT, dl, B, B, UnpackMask);
11145
11146 // Multiply the even parts.
11147 SDValue Evens = DAG.getNode(X86ISD::PMULUDQ, dl, MVT::v2i64, A, B);
11148 // Now multiply odd parts.
11149 SDValue Odds = DAG.getNode(X86ISD::PMULUDQ, dl, MVT::v2i64, Aodds, Bodds);
11150
11151 Evens = DAG.getNode(ISD::BITCAST, dl, VT, Evens);
11152 Odds = DAG.getNode(ISD::BITCAST, dl, VT, Odds);
11153
11154 // Merge the two vectors back together with a shuffle. This expands into 2
11155 // shuffles.
11156 const int ShufMask[] = { 0, 4, 2, 6 };
11157 return DAG.getVectorShuffle(VT, dl, Evens, Odds, ShufMask);
11158 }
11159
Craig Topper5b209e82012-02-05 03:14:49 +000011160 assert((VT == MVT::v2i64 || VT == MVT::v4i64) &&
11161 "Only know how to lower V2I64/V4I64 multiply");
11162
Craig Topper5b209e82012-02-05 03:14:49 +000011163 // Ahi = psrlqi(a, 32);
11164 // Bhi = psrlqi(b, 32);
11165 //
11166 // AloBlo = pmuludq(a, b);
11167 // AloBhi = pmuludq(a, Bhi);
11168 // AhiBlo = pmuludq(Ahi, b);
11169
11170 // AloBhi = psllqi(AloBhi, 32);
11171 // AhiBlo = psllqi(AhiBlo, 32);
11172 // return AloBlo + AloBhi + AhiBlo;
11173
Craig Topper5b209e82012-02-05 03:14:49 +000011174 SDValue ShAmt = DAG.getConstant(32, MVT::i32);
Craig Topperaaa643c2011-11-09 07:28:55 +000011175
Craig Topper5b209e82012-02-05 03:14:49 +000011176 SDValue Ahi = DAG.getNode(X86ISD::VSRLI, dl, VT, A, ShAmt);
11177 SDValue Bhi = DAG.getNode(X86ISD::VSRLI, dl, VT, B, ShAmt);
Craig Topperaaa643c2011-11-09 07:28:55 +000011178
Craig Topper5b209e82012-02-05 03:14:49 +000011179 // Bit cast to 32-bit vectors for MULUDQ
11180 EVT MulVT = (VT == MVT::v2i64) ? MVT::v4i32 : MVT::v8i32;
11181 A = DAG.getNode(ISD::BITCAST, dl, MulVT, A);
11182 B = DAG.getNode(ISD::BITCAST, dl, MulVT, B);
11183 Ahi = DAG.getNode(ISD::BITCAST, dl, MulVT, Ahi);
11184 Bhi = DAG.getNode(ISD::BITCAST, dl, MulVT, Bhi);
Craig Topperaaa643c2011-11-09 07:28:55 +000011185
Craig Topper5b209e82012-02-05 03:14:49 +000011186 SDValue AloBlo = DAG.getNode(X86ISD::PMULUDQ, dl, VT, A, B);
11187 SDValue AloBhi = DAG.getNode(X86ISD::PMULUDQ, dl, VT, A, Bhi);
11188 SDValue AhiBlo = DAG.getNode(X86ISD::PMULUDQ, dl, VT, Ahi, B);
Craig Topperaaa643c2011-11-09 07:28:55 +000011189
Craig Topper5b209e82012-02-05 03:14:49 +000011190 AloBhi = DAG.getNode(X86ISD::VSHLI, dl, VT, AloBhi, ShAmt);
11191 AhiBlo = DAG.getNode(X86ISD::VSHLI, dl, VT, AhiBlo, ShAmt);
Mon P Wangaf9b9522008-12-18 21:42:19 +000011192
Dale Johannesene4d209d2009-02-03 20:21:25 +000011193 SDValue Res = DAG.getNode(ISD::ADD, dl, VT, AloBlo, AloBhi);
Craig Topper5b209e82012-02-05 03:14:49 +000011194 return DAG.getNode(ISD::ADD, dl, VT, Res, AhiBlo);
Mon P Wangaf9b9522008-12-18 21:42:19 +000011195}
11196
Nadav Rotem43012222011-05-11 08:12:09 +000011197SDValue X86TargetLowering::LowerShift(SDValue Op, SelectionDAG &DAG) const {
11198
Nate Begemanbdcb5af2010-07-27 22:37:06 +000011199 EVT VT = Op.getValueType();
11200 DebugLoc dl = Op.getDebugLoc();
11201 SDValue R = Op.getOperand(0);
Nadav Rotem43012222011-05-11 08:12:09 +000011202 SDValue Amt = Op.getOperand(1);
Nate Begemanbdcb5af2010-07-27 22:37:06 +000011203 LLVMContext *Context = DAG.getContext();
Nate Begemanbdcb5af2010-07-27 22:37:06 +000011204
Craig Topper1accb7e2012-01-10 06:54:16 +000011205 if (!Subtarget->hasSSE2())
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +000011206 return SDValue();
11207
Nadav Rotem43012222011-05-11 08:12:09 +000011208 // Optimize shl/srl/sra with constant shift amount.
11209 if (isSplatVector(Amt.getNode())) {
11210 SDValue SclrAmt = Amt->getOperand(0);
11211 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(SclrAmt)) {
11212 uint64_t ShiftAmt = C->getZExtValue();
11213
Craig Toppered2e13d2012-01-22 19:15:14 +000011214 if (VT == MVT::v2i64 || VT == MVT::v4i32 || VT == MVT::v8i16 ||
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000011215 (Subtarget->hasInt256() &&
Craig Toppered2e13d2012-01-22 19:15:14 +000011216 (VT == MVT::v4i64 || VT == MVT::v8i32 || VT == MVT::v16i16))) {
11217 if (Op.getOpcode() == ISD::SHL)
11218 return DAG.getNode(X86ISD::VSHLI, dl, VT, R,
11219 DAG.getConstant(ShiftAmt, MVT::i32));
11220 if (Op.getOpcode() == ISD::SRL)
11221 return DAG.getNode(X86ISD::VSRLI, dl, VT, R,
11222 DAG.getConstant(ShiftAmt, MVT::i32));
11223 if (Op.getOpcode() == ISD::SRA && VT != MVT::v2i64 && VT != MVT::v4i64)
11224 return DAG.getNode(X86ISD::VSRAI, dl, VT, R,
11225 DAG.getConstant(ShiftAmt, MVT::i32));
Benjamin Kramerdade3c12011-10-30 17:31:21 +000011226 }
11227
Craig Toppered2e13d2012-01-22 19:15:14 +000011228 if (VT == MVT::v16i8) {
11229 if (Op.getOpcode() == ISD::SHL) {
11230 // Make a large shift.
11231 SDValue SHL = DAG.getNode(X86ISD::VSHLI, dl, MVT::v8i16, R,
11232 DAG.getConstant(ShiftAmt, MVT::i32));
11233 SHL = DAG.getNode(ISD::BITCAST, dl, VT, SHL);
11234 // Zero out the rightmost bits.
11235 SmallVector<SDValue, 16> V(16,
11236 DAG.getConstant(uint8_t(-1U << ShiftAmt),
11237 MVT::i8));
11238 return DAG.getNode(ISD::AND, dl, VT, SHL,
11239 DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &V[0], 16));
Eli Friedmanf6aa6b12011-11-01 21:18:39 +000011240 }
Craig Toppered2e13d2012-01-22 19:15:14 +000011241 if (Op.getOpcode() == ISD::SRL) {
11242 // Make a large shift.
11243 SDValue SRL = DAG.getNode(X86ISD::VSRLI, dl, MVT::v8i16, R,
11244 DAG.getConstant(ShiftAmt, MVT::i32));
11245 SRL = DAG.getNode(ISD::BITCAST, dl, VT, SRL);
11246 // Zero out the leftmost bits.
11247 SmallVector<SDValue, 16> V(16,
11248 DAG.getConstant(uint8_t(-1U) >> ShiftAmt,
11249 MVT::i8));
11250 return DAG.getNode(ISD::AND, dl, VT, SRL,
11251 DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &V[0], 16));
11252 }
11253 if (Op.getOpcode() == ISD::SRA) {
11254 if (ShiftAmt == 7) {
11255 // R s>> 7 === R s< 0
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +000011256 SDValue Zeros = getZeroVector(VT, Subtarget, DAG, dl);
Craig Topper67609fd2012-01-22 22:42:16 +000011257 return DAG.getNode(X86ISD::PCMPGT, dl, VT, Zeros, R);
Craig Toppered2e13d2012-01-22 19:15:14 +000011258 }
Eli Friedmanf6aa6b12011-11-01 21:18:39 +000011259
Craig Toppered2e13d2012-01-22 19:15:14 +000011260 // R s>> a === ((R u>> a) ^ m) - m
11261 SDValue Res = DAG.getNode(ISD::SRL, dl, VT, R, Amt);
11262 SmallVector<SDValue, 16> V(16, DAG.getConstant(128 >> ShiftAmt,
11263 MVT::i8));
11264 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &V[0], 16);
11265 Res = DAG.getNode(ISD::XOR, dl, VT, Res, Mask);
11266 Res = DAG.getNode(ISD::SUB, dl, VT, Res, Mask);
11267 return Res;
11268 }
Craig Topper731dfd02012-04-23 03:42:40 +000011269 llvm_unreachable("Unknown shift opcode.");
Eli Friedmanf6aa6b12011-11-01 21:18:39 +000011270 }
Craig Topper46154eb2011-11-11 07:39:23 +000011271
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000011272 if (Subtarget->hasInt256() && VT == MVT::v32i8) {
Craig Topper0d86d462011-11-20 00:12:05 +000011273 if (Op.getOpcode() == ISD::SHL) {
11274 // Make a large shift.
Craig Toppered2e13d2012-01-22 19:15:14 +000011275 SDValue SHL = DAG.getNode(X86ISD::VSHLI, dl, MVT::v16i16, R,
11276 DAG.getConstant(ShiftAmt, MVT::i32));
11277 SHL = DAG.getNode(ISD::BITCAST, dl, VT, SHL);
Craig Topper0d86d462011-11-20 00:12:05 +000011278 // Zero out the rightmost bits.
Craig Toppered2e13d2012-01-22 19:15:14 +000011279 SmallVector<SDValue, 32> V(32,
11280 DAG.getConstant(uint8_t(-1U << ShiftAmt),
11281 MVT::i8));
Craig Topper0d86d462011-11-20 00:12:05 +000011282 return DAG.getNode(ISD::AND, dl, VT, SHL,
11283 DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &V[0], 32));
Craig Topper46154eb2011-11-11 07:39:23 +000011284 }
Craig Topper0d86d462011-11-20 00:12:05 +000011285 if (Op.getOpcode() == ISD::SRL) {
11286 // Make a large shift.
Craig Toppered2e13d2012-01-22 19:15:14 +000011287 SDValue SRL = DAG.getNode(X86ISD::VSRLI, dl, MVT::v16i16, R,
11288 DAG.getConstant(ShiftAmt, MVT::i32));
11289 SRL = DAG.getNode(ISD::BITCAST, dl, VT, SRL);
Craig Topper0d86d462011-11-20 00:12:05 +000011290 // Zero out the leftmost bits.
Craig Toppered2e13d2012-01-22 19:15:14 +000011291 SmallVector<SDValue, 32> V(32,
11292 DAG.getConstant(uint8_t(-1U) >> ShiftAmt,
11293 MVT::i8));
Craig Topper0d86d462011-11-20 00:12:05 +000011294 return DAG.getNode(ISD::AND, dl, VT, SRL,
11295 DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &V[0], 32));
11296 }
11297 if (Op.getOpcode() == ISD::SRA) {
11298 if (ShiftAmt == 7) {
11299 // R s>> 7 === R s< 0
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +000011300 SDValue Zeros = getZeroVector(VT, Subtarget, DAG, dl);
Craig Topper67609fd2012-01-22 22:42:16 +000011301 return DAG.getNode(X86ISD::PCMPGT, dl, VT, Zeros, R);
Craig Topper0d86d462011-11-20 00:12:05 +000011302 }
11303
11304 // R s>> a === ((R u>> a) ^ m) - m
11305 SDValue Res = DAG.getNode(ISD::SRL, dl, VT, R, Amt);
11306 SmallVector<SDValue, 32> V(32, DAG.getConstant(128 >> ShiftAmt,
11307 MVT::i8));
11308 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &V[0], 32);
11309 Res = DAG.getNode(ISD::XOR, dl, VT, Res, Mask);
11310 Res = DAG.getNode(ISD::SUB, dl, VT, Res, Mask);
11311 return Res;
11312 }
Craig Topper731dfd02012-04-23 03:42:40 +000011313 llvm_unreachable("Unknown shift opcode.");
Craig Topper0d86d462011-11-20 00:12:05 +000011314 }
Nadav Rotem43012222011-05-11 08:12:09 +000011315 }
11316 }
11317
11318 // Lower SHL with variable shift amount.
Nadav Rotem43012222011-05-11 08:12:09 +000011319 if (VT == MVT::v4i32 && Op->getOpcode() == ISD::SHL) {
Craig Toppered2e13d2012-01-22 19:15:14 +000011320 Op = DAG.getNode(X86ISD::VSHLI, dl, VT, Op.getOperand(1),
11321 DAG.getConstant(23, MVT::i32));
Nate Begeman51409212010-07-28 00:21:48 +000011322
Chris Lattner7302d802012-02-06 21:56:39 +000011323 const uint32_t CV[] = { 0x3f800000U, 0x3f800000U, 0x3f800000U, 0x3f800000U};
11324 Constant *C = ConstantDataVector::get(*Context, CV);
Nate Begeman51409212010-07-28 00:21:48 +000011325 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
11326 SDValue Addend = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +000011327 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000011328 false, false, false, 16);
Nate Begeman51409212010-07-28 00:21:48 +000011329
11330 Op = DAG.getNode(ISD::ADD, dl, VT, Op, Addend);
Wesley Peckbf17cfa2010-11-23 03:31:01 +000011331 Op = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, Op);
Nate Begeman51409212010-07-28 00:21:48 +000011332 Op = DAG.getNode(ISD::FP_TO_SINT, dl, VT, Op);
11333 return DAG.getNode(ISD::MUL, dl, VT, Op, R);
11334 }
Nadav Rotem43012222011-05-11 08:12:09 +000011335 if (VT == MVT::v16i8 && Op->getOpcode() == ISD::SHL) {
Craig Topper8b5a6b62012-01-17 08:23:44 +000011336 assert(Subtarget->hasSSE2() && "Need SSE2 for pslli/pcmpeq.");
Lang Hames8b99c1e2011-12-17 01:08:46 +000011337
Nate Begeman51409212010-07-28 00:21:48 +000011338 // a = a << 5;
Craig Toppered2e13d2012-01-22 19:15:14 +000011339 Op = DAG.getNode(X86ISD::VSHLI, dl, MVT::v8i16, Op.getOperand(1),
11340 DAG.getConstant(5, MVT::i32));
11341 Op = DAG.getNode(ISD::BITCAST, dl, VT, Op);
Nate Begeman51409212010-07-28 00:21:48 +000011342
Lang Hames8b99c1e2011-12-17 01:08:46 +000011343 // Turn 'a' into a mask suitable for VSELECT
11344 SDValue VSelM = DAG.getConstant(0x80, VT);
11345 SDValue OpVSel = DAG.getNode(ISD::AND, dl, VT, VSelM, Op);
Craig Topper67609fd2012-01-22 22:42:16 +000011346 OpVSel = DAG.getNode(X86ISD::PCMPEQ, dl, VT, OpVSel, VSelM);
Nate Begeman51409212010-07-28 00:21:48 +000011347
Lang Hames8b99c1e2011-12-17 01:08:46 +000011348 SDValue CM1 = DAG.getConstant(0x0f, VT);
11349 SDValue CM2 = DAG.getConstant(0x3f, VT);
Nate Begeman51409212010-07-28 00:21:48 +000011350
Lang Hames8b99c1e2011-12-17 01:08:46 +000011351 // r = VSELECT(r, psllw(r & (char16)15, 4), a);
11352 SDValue M = DAG.getNode(ISD::AND, dl, VT, R, CM1);
Craig Toppered2e13d2012-01-22 19:15:14 +000011353 M = getTargetVShiftNode(X86ISD::VSHLI, dl, MVT::v8i16, M,
11354 DAG.getConstant(4, MVT::i32), DAG);
11355 M = DAG.getNode(ISD::BITCAST, dl, VT, M);
Lang Hames8b99c1e2011-12-17 01:08:46 +000011356 R = DAG.getNode(ISD::VSELECT, dl, VT, OpVSel, M, R);
11357
Nate Begeman51409212010-07-28 00:21:48 +000011358 // a += a
11359 Op = DAG.getNode(ISD::ADD, dl, VT, Op, Op);
Lang Hames8b99c1e2011-12-17 01:08:46 +000011360 OpVSel = DAG.getNode(ISD::AND, dl, VT, VSelM, Op);
Craig Topper67609fd2012-01-22 22:42:16 +000011361 OpVSel = DAG.getNode(X86ISD::PCMPEQ, dl, VT, OpVSel, VSelM);
Michael J. Spencerec38de22010-10-10 22:04:20 +000011362
Lang Hames8b99c1e2011-12-17 01:08:46 +000011363 // r = VSELECT(r, psllw(r & (char16)63, 2), a);
11364 M = DAG.getNode(ISD::AND, dl, VT, R, CM2);
Craig Toppered2e13d2012-01-22 19:15:14 +000011365 M = getTargetVShiftNode(X86ISD::VSHLI, dl, MVT::v8i16, M,
11366 DAG.getConstant(2, MVT::i32), DAG);
11367 M = DAG.getNode(ISD::BITCAST, dl, VT, M);
Lang Hames8b99c1e2011-12-17 01:08:46 +000011368 R = DAG.getNode(ISD::VSELECT, dl, VT, OpVSel, M, R);
11369
Nate Begeman51409212010-07-28 00:21:48 +000011370 // a += a
11371 Op = DAG.getNode(ISD::ADD, dl, VT, Op, Op);
Lang Hames8b99c1e2011-12-17 01:08:46 +000011372 OpVSel = DAG.getNode(ISD::AND, dl, VT, VSelM, Op);
Craig Topper67609fd2012-01-22 22:42:16 +000011373 OpVSel = DAG.getNode(X86ISD::PCMPEQ, dl, VT, OpVSel, VSelM);
Michael J. Spencerec38de22010-10-10 22:04:20 +000011374
Lang Hames8b99c1e2011-12-17 01:08:46 +000011375 // return VSELECT(r, r+r, a);
11376 R = DAG.getNode(ISD::VSELECT, dl, VT, OpVSel,
Lang Hamesa0a25132011-12-15 18:57:27 +000011377 DAG.getNode(ISD::ADD, dl, VT, R, R), R);
Nate Begeman51409212010-07-28 00:21:48 +000011378 return R;
11379 }
Craig Topper46154eb2011-11-11 07:39:23 +000011380
11381 // Decompose 256-bit shifts into smaller 128-bit shifts.
Craig Topper7a9a28b2012-08-12 02:23:29 +000011382 if (VT.is256BitVector()) {
Craig Toppered2e13d2012-01-22 19:15:14 +000011383 unsigned NumElems = VT.getVectorNumElements();
Craig Topper46154eb2011-11-11 07:39:23 +000011384 MVT EltVT = VT.getVectorElementType().getSimpleVT();
11385 EVT NewVT = MVT::getVectorVT(EltVT, NumElems/2);
11386
11387 // Extract the two vectors
Craig Topperb14940a2012-04-22 20:55:18 +000011388 SDValue V1 = Extract128BitVector(R, 0, DAG, dl);
11389 SDValue V2 = Extract128BitVector(R, NumElems/2, DAG, dl);
Craig Topper46154eb2011-11-11 07:39:23 +000011390
11391 // Recreate the shift amount vectors
11392 SDValue Amt1, Amt2;
11393 if (Amt.getOpcode() == ISD::BUILD_VECTOR) {
11394 // Constant shift amount
11395 SmallVector<SDValue, 4> Amt1Csts;
11396 SmallVector<SDValue, 4> Amt2Csts;
Craig Toppered2e13d2012-01-22 19:15:14 +000011397 for (unsigned i = 0; i != NumElems/2; ++i)
Craig Topper46154eb2011-11-11 07:39:23 +000011398 Amt1Csts.push_back(Amt->getOperand(i));
Craig Toppered2e13d2012-01-22 19:15:14 +000011399 for (unsigned i = NumElems/2; i != NumElems; ++i)
Craig Topper46154eb2011-11-11 07:39:23 +000011400 Amt2Csts.push_back(Amt->getOperand(i));
11401
11402 Amt1 = DAG.getNode(ISD::BUILD_VECTOR, dl, NewVT,
11403 &Amt1Csts[0], NumElems/2);
11404 Amt2 = DAG.getNode(ISD::BUILD_VECTOR, dl, NewVT,
11405 &Amt2Csts[0], NumElems/2);
11406 } else {
11407 // Variable shift amount
Craig Topperb14940a2012-04-22 20:55:18 +000011408 Amt1 = Extract128BitVector(Amt, 0, DAG, dl);
11409 Amt2 = Extract128BitVector(Amt, NumElems/2, DAG, dl);
Craig Topper46154eb2011-11-11 07:39:23 +000011410 }
11411
11412 // Issue new vector shifts for the smaller types
11413 V1 = DAG.getNode(Op.getOpcode(), dl, NewVT, V1, Amt1);
11414 V2 = DAG.getNode(Op.getOpcode(), dl, NewVT, V2, Amt2);
11415
11416 // Concatenate the result back
11417 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT, V1, V2);
11418 }
11419
Nate Begeman51409212010-07-28 00:21:48 +000011420 return SDValue();
Nate Begemanbdcb5af2010-07-27 22:37:06 +000011421}
Mon P Wangaf9b9522008-12-18 21:42:19 +000011422
Craig Topper55b24052012-09-11 06:15:32 +000011423static SDValue LowerXALUO(SDValue Op, SelectionDAG &DAG) {
Bill Wendling74c37652008-12-09 22:08:41 +000011424 // Lower the "add/sub/mul with overflow" instruction into a regular ins plus
11425 // a "setcc" instruction that checks the overflow flag. The "brcond" lowering
Bill Wendling61edeb52008-12-02 01:06:39 +000011426 // looks for this combo and may remove the "setcc" instruction if the "setcc"
11427 // has only one use.
Bill Wendling3fafd932008-11-26 22:37:40 +000011428 SDNode *N = Op.getNode();
Bill Wendling61edeb52008-12-02 01:06:39 +000011429 SDValue LHS = N->getOperand(0);
11430 SDValue RHS = N->getOperand(1);
Bill Wendling74c37652008-12-09 22:08:41 +000011431 unsigned BaseOp = 0;
11432 unsigned Cond = 0;
Chris Lattnerb20e0b12010-12-05 07:30:36 +000011433 DebugLoc DL = Op.getDebugLoc();
Bill Wendling74c37652008-12-09 22:08:41 +000011434 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +000011435 default: llvm_unreachable("Unknown ovf instruction!");
Bill Wendling74c37652008-12-09 22:08:41 +000011436 case ISD::SADDO:
Dan Gohman076aee32009-03-04 19:44:21 +000011437 // A subtract of one will be selected as a INC. Note that INC doesn't
11438 // set CF, so we can't do this for UADDO.
Benjamin Kramerc175a4b2011-03-08 15:20:20 +000011439 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(RHS))
11440 if (C->isOne()) {
Dan Gohman076aee32009-03-04 19:44:21 +000011441 BaseOp = X86ISD::INC;
11442 Cond = X86::COND_O;
11443 break;
11444 }
Bill Wendlingab55ebd2008-12-12 00:56:36 +000011445 BaseOp = X86ISD::ADD;
Bill Wendling74c37652008-12-09 22:08:41 +000011446 Cond = X86::COND_O;
11447 break;
11448 case ISD::UADDO:
Bill Wendlingab55ebd2008-12-12 00:56:36 +000011449 BaseOp = X86ISD::ADD;
Dan Gohman653456c2009-01-07 00:15:08 +000011450 Cond = X86::COND_B;
Bill Wendling74c37652008-12-09 22:08:41 +000011451 break;
11452 case ISD::SSUBO:
Dan Gohman076aee32009-03-04 19:44:21 +000011453 // A subtract of one will be selected as a DEC. Note that DEC doesn't
11454 // set CF, so we can't do this for USUBO.
Benjamin Kramerc175a4b2011-03-08 15:20:20 +000011455 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(RHS))
11456 if (C->isOne()) {
Dan Gohman076aee32009-03-04 19:44:21 +000011457 BaseOp = X86ISD::DEC;
11458 Cond = X86::COND_O;
11459 break;
11460 }
Bill Wendlingab55ebd2008-12-12 00:56:36 +000011461 BaseOp = X86ISD::SUB;
Bill Wendling74c37652008-12-09 22:08:41 +000011462 Cond = X86::COND_O;
11463 break;
11464 case ISD::USUBO:
Bill Wendlingab55ebd2008-12-12 00:56:36 +000011465 BaseOp = X86ISD::SUB;
Dan Gohman653456c2009-01-07 00:15:08 +000011466 Cond = X86::COND_B;
Bill Wendling74c37652008-12-09 22:08:41 +000011467 break;
11468 case ISD::SMULO:
Bill Wendlingd350e022008-12-12 21:15:41 +000011469 BaseOp = X86ISD::SMUL;
Bill Wendling74c37652008-12-09 22:08:41 +000011470 Cond = X86::COND_O;
11471 break;
Chris Lattnerb20e0b12010-12-05 07:30:36 +000011472 case ISD::UMULO: { // i64, i8 = umulo lhs, rhs --> i64, i64, i32 umul lhs,rhs
11473 SDVTList VTs = DAG.getVTList(N->getValueType(0), N->getValueType(0),
11474 MVT::i32);
11475 SDValue Sum = DAG.getNode(X86ISD::UMUL, DL, VTs, LHS, RHS);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011476
Chris Lattnerb20e0b12010-12-05 07:30:36 +000011477 SDValue SetCC =
11478 DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
11479 DAG.getConstant(X86::COND_O, MVT::i32),
11480 SDValue(Sum.getNode(), 2));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011481
Dan Gohman6e5fda22011-07-22 18:45:15 +000011482 return DAG.getNode(ISD::MERGE_VALUES, DL, N->getVTList(), Sum, SetCC);
Chris Lattnerb20e0b12010-12-05 07:30:36 +000011483 }
Bill Wendling74c37652008-12-09 22:08:41 +000011484 }
Bill Wendling3fafd932008-11-26 22:37:40 +000011485
Bill Wendling61edeb52008-12-02 01:06:39 +000011486 // Also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +000011487 SDVTList VTs = DAG.getVTList(N->getValueType(0), MVT::i32);
Chris Lattnerb20e0b12010-12-05 07:30:36 +000011488 SDValue Sum = DAG.getNode(BaseOp, DL, VTs, LHS, RHS);
Bill Wendling3fafd932008-11-26 22:37:40 +000011489
Bill Wendling61edeb52008-12-02 01:06:39 +000011490 SDValue SetCC =
Chris Lattnerb20e0b12010-12-05 07:30:36 +000011491 DAG.getNode(X86ISD::SETCC, DL, N->getValueType(1),
11492 DAG.getConstant(Cond, MVT::i32),
11493 SDValue(Sum.getNode(), 1));
Bill Wendling3fafd932008-11-26 22:37:40 +000011494
Dan Gohman6e5fda22011-07-22 18:45:15 +000011495 return DAG.getNode(ISD::MERGE_VALUES, DL, N->getVTList(), Sum, SetCC);
Bill Wendling41ea7e72008-11-24 19:21:46 +000011496}
11497
Chad Rosier30450e82011-12-22 22:35:21 +000011498SDValue X86TargetLowering::LowerSIGN_EXTEND_INREG(SDValue Op,
11499 SelectionDAG &DAG) const {
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000011500 DebugLoc dl = Op.getDebugLoc();
Craig Toppera124f942011-11-21 01:12:36 +000011501 EVT ExtraVT = cast<VTSDNode>(Op.getOperand(1))->getVT();
11502 EVT VT = Op.getValueType();
11503
Craig Toppered2e13d2012-01-22 19:15:14 +000011504 if (!Subtarget->hasSSE2() || !VT.isVector())
11505 return SDValue();
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000011506
Craig Toppered2e13d2012-01-22 19:15:14 +000011507 unsigned BitsDiff = VT.getScalarType().getSizeInBits() -
11508 ExtraVT.getScalarType().getSizeInBits();
11509 SDValue ShAmt = DAG.getConstant(BitsDiff, MVT::i32);
11510
11511 switch (VT.getSimpleVT().SimpleTy) {
11512 default: return SDValue();
11513 case MVT::v8i32:
11514 case MVT::v16i16:
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000011515 if (!Subtarget->hasFp256())
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000011516 return SDValue();
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000011517 if (!Subtarget->hasInt256()) {
Craig Toppered2e13d2012-01-22 19:15:14 +000011518 // needs to be split
Craig Topper66ddd152012-04-27 22:54:43 +000011519 unsigned NumElems = VT.getVectorNumElements();
Craig Toppera124f942011-11-21 01:12:36 +000011520
Craig Toppered2e13d2012-01-22 19:15:14 +000011521 // Extract the LHS vectors
11522 SDValue LHS = Op.getOperand(0);
Craig Topperb14940a2012-04-22 20:55:18 +000011523 SDValue LHS1 = Extract128BitVector(LHS, 0, DAG, dl);
11524 SDValue LHS2 = Extract128BitVector(LHS, NumElems/2, DAG, dl);
Craig Toppera124f942011-11-21 01:12:36 +000011525
Craig Toppered2e13d2012-01-22 19:15:14 +000011526 MVT EltVT = VT.getVectorElementType().getSimpleVT();
11527 EVT NewVT = MVT::getVectorVT(EltVT, NumElems/2);
Craig Toppera124f942011-11-21 01:12:36 +000011528
Craig Toppered2e13d2012-01-22 19:15:14 +000011529 EVT ExtraEltVT = ExtraVT.getVectorElementType();
Craig Topperb6072642012-05-03 07:26:59 +000011530 unsigned ExtraNumElems = ExtraVT.getVectorNumElements();
Craig Toppered2e13d2012-01-22 19:15:14 +000011531 ExtraVT = EVT::getVectorVT(*DAG.getContext(), ExtraEltVT,
11532 ExtraNumElems/2);
11533 SDValue Extra = DAG.getValueType(ExtraVT);
Craig Toppera124f942011-11-21 01:12:36 +000011534
Craig Toppered2e13d2012-01-22 19:15:14 +000011535 LHS1 = DAG.getNode(Op.getOpcode(), dl, NewVT, LHS1, Extra);
11536 LHS2 = DAG.getNode(Op.getOpcode(), dl, NewVT, LHS2, Extra);
Craig Toppera124f942011-11-21 01:12:36 +000011537
Dmitri Gribenko2de05722012-09-10 21:26:47 +000011538 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT, LHS1, LHS2);
Craig Toppered2e13d2012-01-22 19:15:14 +000011539 }
11540 // fall through
11541 case MVT::v4i32:
11542 case MVT::v8i16: {
11543 SDValue Tmp1 = getTargetVShiftNode(X86ISD::VSHLI, dl, VT,
11544 Op.getOperand(0), ShAmt, DAG);
11545 return getTargetVShiftNode(X86ISD::VSRAI, dl, VT, Tmp1, ShAmt, DAG);
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000011546 }
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000011547 }
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000011548}
11549
Craig Topper55b24052012-09-11 06:15:32 +000011550static SDValue LowerMEMBARRIER(SDValue Op, const X86Subtarget *Subtarget,
11551 SelectionDAG &DAG) {
Eric Christopher9a9d2752010-07-22 02:48:34 +000011552 DebugLoc dl = Op.getDebugLoc();
Michael J. Spencerec38de22010-10-10 22:04:20 +000011553
Eric Christopher77ed1352011-07-08 00:04:56 +000011554 // Go ahead and emit the fence on x86-64 even if we asked for no-sse2.
11555 // There isn't any reason to disable it if the target processor supports it.
Craig Topper1accb7e2012-01-10 06:54:16 +000011556 if (!Subtarget->hasSSE2() && !Subtarget->is64Bit()) {
Eric Christopherc0b2a202010-08-14 21:51:50 +000011557 SDValue Chain = Op.getOperand(0);
Eric Christopher77ed1352011-07-08 00:04:56 +000011558 SDValue Zero = DAG.getConstant(0, MVT::i32);
Eric Christopherc0b2a202010-08-14 21:51:50 +000011559 SDValue Ops[] = {
11560 DAG.getRegister(X86::ESP, MVT::i32), // Base
11561 DAG.getTargetConstant(1, MVT::i8), // Scale
11562 DAG.getRegister(0, MVT::i32), // Index
11563 DAG.getTargetConstant(0, MVT::i32), // Disp
11564 DAG.getRegister(0, MVT::i32), // Segment.
11565 Zero,
11566 Chain
11567 };
Michael J. Spencerec38de22010-10-10 22:04:20 +000011568 SDNode *Res =
Eric Christopherc0b2a202010-08-14 21:51:50 +000011569 DAG.getMachineNode(X86::OR32mrLocked, dl, MVT::Other, Ops,
11570 array_lengthof(Ops));
11571 return SDValue(Res, 0);
Eric Christopherb6729dc2010-08-04 23:03:04 +000011572 }
Michael J. Spencerec38de22010-10-10 22:04:20 +000011573
Eric Christopher9a9d2752010-07-22 02:48:34 +000011574 unsigned isDev = cast<ConstantSDNode>(Op.getOperand(5))->getZExtValue();
Chris Lattner132929a2010-08-14 17:26:09 +000011575 if (!isDev)
Eric Christopher9a9d2752010-07-22 02:48:34 +000011576 return DAG.getNode(X86ISD::MEMBARRIER, dl, MVT::Other, Op.getOperand(0));
Michael J. Spencerec38de22010-10-10 22:04:20 +000011577
Chris Lattner132929a2010-08-14 17:26:09 +000011578 unsigned Op1 = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
11579 unsigned Op2 = cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue();
11580 unsigned Op3 = cast<ConstantSDNode>(Op.getOperand(3))->getZExtValue();
11581 unsigned Op4 = cast<ConstantSDNode>(Op.getOperand(4))->getZExtValue();
Michael J. Spencerec38de22010-10-10 22:04:20 +000011582
Chris Lattner132929a2010-08-14 17:26:09 +000011583 // def : Pat<(membarrier (i8 0), (i8 0), (i8 0), (i8 1), (i8 1)), (SFENCE)>;
11584 if (!Op1 && !Op2 && !Op3 && Op4)
11585 return DAG.getNode(X86ISD::SFENCE, dl, MVT::Other, Op.getOperand(0));
Michael J. Spencerec38de22010-10-10 22:04:20 +000011586
Chris Lattner132929a2010-08-14 17:26:09 +000011587 // def : Pat<(membarrier (i8 1), (i8 0), (i8 0), (i8 0), (i8 1)), (LFENCE)>;
11588 if (Op1 && !Op2 && !Op3 && !Op4)
11589 return DAG.getNode(X86ISD::LFENCE, dl, MVT::Other, Op.getOperand(0));
Michael J. Spencerec38de22010-10-10 22:04:20 +000011590
11591 // def : Pat<(membarrier (i8 imm), (i8 imm), (i8 imm), (i8 imm), (i8 1)),
Chris Lattner132929a2010-08-14 17:26:09 +000011592 // (MFENCE)>;
11593 return DAG.getNode(X86ISD::MFENCE, dl, MVT::Other, Op.getOperand(0));
Eric Christopher9a9d2752010-07-22 02:48:34 +000011594}
11595
Craig Topper55b24052012-09-11 06:15:32 +000011596static SDValue LowerATOMIC_FENCE(SDValue Op, const X86Subtarget *Subtarget,
11597 SelectionDAG &DAG) {
Eli Friedman14648462011-07-27 22:21:52 +000011598 DebugLoc dl = Op.getDebugLoc();
11599 AtomicOrdering FenceOrdering = static_cast<AtomicOrdering>(
11600 cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue());
11601 SynchronizationScope FenceScope = static_cast<SynchronizationScope>(
11602 cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue());
11603
11604 // The only fence that needs an instruction is a sequentially-consistent
11605 // cross-thread fence.
11606 if (FenceOrdering == SequentiallyConsistent && FenceScope == CrossThread) {
11607 // Use mfence if we have SSE2 or we're on x86-64 (even if we asked for
11608 // no-sse2). There isn't any reason to disable it if the target processor
11609 // supports it.
Craig Topper1accb7e2012-01-10 06:54:16 +000011610 if (Subtarget->hasSSE2() || Subtarget->is64Bit())
Eli Friedman14648462011-07-27 22:21:52 +000011611 return DAG.getNode(X86ISD::MFENCE, dl, MVT::Other, Op.getOperand(0));
11612
11613 SDValue Chain = Op.getOperand(0);
11614 SDValue Zero = DAG.getConstant(0, MVT::i32);
11615 SDValue Ops[] = {
11616 DAG.getRegister(X86::ESP, MVT::i32), // Base
11617 DAG.getTargetConstant(1, MVT::i8), // Scale
11618 DAG.getRegister(0, MVT::i32), // Index
11619 DAG.getTargetConstant(0, MVT::i32), // Disp
11620 DAG.getRegister(0, MVT::i32), // Segment.
11621 Zero,
11622 Chain
11623 };
11624 SDNode *Res =
11625 DAG.getMachineNode(X86::OR32mrLocked, dl, MVT::Other, Ops,
11626 array_lengthof(Ops));
11627 return SDValue(Res, 0);
11628 }
11629
11630 // MEMBARRIER is a compiler barrier; it codegens to a no-op.
11631 return DAG.getNode(X86ISD::MEMBARRIER, dl, MVT::Other, Op.getOperand(0));
11632}
11633
Craig Topper55b24052012-09-11 06:15:32 +000011634static SDValue LowerCMP_SWAP(SDValue Op, const X86Subtarget *Subtarget,
11635 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +000011636 EVT T = Op.getValueType();
Chris Lattner93c4a5b2010-09-21 23:59:42 +000011637 DebugLoc DL = Op.getDebugLoc();
Andrew Lenhartha76e2f02008-03-04 21:13:33 +000011638 unsigned Reg = 0;
11639 unsigned size = 0;
Owen Anderson825b72b2009-08-11 20:47:22 +000011640 switch(T.getSimpleVT().SimpleTy) {
Craig Topperabb94d02012-02-05 03:43:23 +000011641 default: llvm_unreachable("Invalid value type!");
Owen Anderson825b72b2009-08-11 20:47:22 +000011642 case MVT::i8: Reg = X86::AL; size = 1; break;
11643 case MVT::i16: Reg = X86::AX; size = 2; break;
11644 case MVT::i32: Reg = X86::EAX; size = 4; break;
11645 case MVT::i64:
Duncan Sands1607f052008-12-01 11:39:25 +000011646 assert(Subtarget->is64Bit() && "Node not type legal!");
11647 Reg = X86::RAX; size = 8;
Andrew Lenharthd19189e2008-03-05 01:15:49 +000011648 break;
Bill Wendling61edeb52008-12-02 01:06:39 +000011649 }
Chris Lattner93c4a5b2010-09-21 23:59:42 +000011650 SDValue cpIn = DAG.getCopyToReg(Op.getOperand(0), DL, Reg,
Dale Johannesend18a4622008-09-11 03:12:59 +000011651 Op.getOperand(2), SDValue());
Dan Gohman475871a2008-07-27 21:46:04 +000011652 SDValue Ops[] = { cpIn.getValue(0),
Evan Cheng8a186ae2008-09-24 23:26:36 +000011653 Op.getOperand(1),
11654 Op.getOperand(3),
Owen Anderson825b72b2009-08-11 20:47:22 +000011655 DAG.getTargetConstant(size, MVT::i8),
Evan Cheng8a186ae2008-09-24 23:26:36 +000011656 cpIn.getValue(1) };
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +000011657 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Chris Lattner93c4a5b2010-09-21 23:59:42 +000011658 MachineMemOperand *MMO = cast<AtomicSDNode>(Op)->getMemOperand();
11659 SDValue Result = DAG.getMemIntrinsicNode(X86ISD::LCMPXCHG_DAG, DL, Tys,
11660 Ops, 5, T, MMO);
Scott Michelfdc40a02009-02-17 22:15:04 +000011661 SDValue cpOut =
Chris Lattner93c4a5b2010-09-21 23:59:42 +000011662 DAG.getCopyFromReg(Result.getValue(0), DL, Reg, T, Result.getValue(1));
Andrew Lenharth26ed8692008-03-01 21:52:34 +000011663 return cpOut;
11664}
11665
Craig Topper55b24052012-09-11 06:15:32 +000011666static SDValue LowerREADCYCLECOUNTER(SDValue Op, const X86Subtarget *Subtarget,
11667 SelectionDAG &DAG) {
Duncan Sands1607f052008-12-01 11:39:25 +000011668 assert(Subtarget->is64Bit() && "Result not type legalized?");
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +000011669 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Duncan Sands1607f052008-12-01 11:39:25 +000011670 SDValue TheChain = Op.getOperand(0);
Dale Johannesen6f38cb62009-02-07 19:59:05 +000011671 DebugLoc dl = Op.getDebugLoc();
Dale Johannesene4d209d2009-02-03 20:21:25 +000011672 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, dl, Tys, &TheChain, 1);
Owen Anderson825b72b2009-08-11 20:47:22 +000011673 SDValue rax = DAG.getCopyFromReg(rd, dl, X86::RAX, MVT::i64, rd.getValue(1));
11674 SDValue rdx = DAG.getCopyFromReg(rax.getValue(1), dl, X86::RDX, MVT::i64,
Duncan Sands1607f052008-12-01 11:39:25 +000011675 rax.getValue(2));
Owen Anderson825b72b2009-08-11 20:47:22 +000011676 SDValue Tmp = DAG.getNode(ISD::SHL, dl, MVT::i64, rdx,
11677 DAG.getConstant(32, MVT::i8));
Duncan Sands1607f052008-12-01 11:39:25 +000011678 SDValue Ops[] = {
Owen Anderson825b72b2009-08-11 20:47:22 +000011679 DAG.getNode(ISD::OR, dl, MVT::i64, rax, Tmp),
Duncan Sands1607f052008-12-01 11:39:25 +000011680 rdx.getValue(1)
11681 };
Dale Johannesene4d209d2009-02-03 20:21:25 +000011682 return DAG.getMergeValues(Ops, 2, dl);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011683}
11684
Craig Topper55b24052012-09-11 06:15:32 +000011685SDValue X86TargetLowering::LowerBITCAST(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen7d07b482010-05-21 00:52:33 +000011686 EVT SrcVT = Op.getOperand(0).getValueType();
11687 EVT DstVT = Op.getValueType();
Craig Topper1accb7e2012-01-10 06:54:16 +000011688 assert(Subtarget->is64Bit() && !Subtarget->hasSSE2() &&
Chris Lattner2a786eb2010-12-19 20:19:20 +000011689 Subtarget->hasMMX() && "Unexpected custom BITCAST");
Michael J. Spencerec38de22010-10-10 22:04:20 +000011690 assert((DstVT == MVT::i64 ||
Dale Johannesen7d07b482010-05-21 00:52:33 +000011691 (DstVT.isVector() && DstVT.getSizeInBits()==64)) &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +000011692 "Unexpected custom BITCAST");
Dale Johannesen7d07b482010-05-21 00:52:33 +000011693 // i64 <=> MMX conversions are Legal.
11694 if (SrcVT==MVT::i64 && DstVT.isVector())
11695 return Op;
11696 if (DstVT==MVT::i64 && SrcVT.isVector())
11697 return Op;
Dale Johannesene39859a2010-05-21 18:40:15 +000011698 // MMX <=> MMX conversions are Legal.
11699 if (SrcVT.isVector() && DstVT.isVector())
11700 return Op;
Dale Johannesen7d07b482010-05-21 00:52:33 +000011701 // All other conversions need to be expanded.
11702 return SDValue();
11703}
Chris Lattner5b856542010-12-20 00:59:46 +000011704
Craig Topper55b24052012-09-11 06:15:32 +000011705static SDValue LowerLOAD_SUB(SDValue Op, SelectionDAG &DAG) {
Dale Johannesen71d1bf52008-09-29 22:25:26 +000011706 SDNode *Node = Op.getNode();
Dale Johannesene4d209d2009-02-03 20:21:25 +000011707 DebugLoc dl = Node->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +000011708 EVT T = Node->getValueType(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +000011709 SDValue negOp = DAG.getNode(ISD::SUB, dl, T,
Evan Cheng242b38b2009-02-23 09:03:22 +000011710 DAG.getConstant(0, T), Node->getOperand(2));
Dale Johannesene4d209d2009-02-03 20:21:25 +000011711 return DAG.getAtomic(ISD::ATOMIC_LOAD_ADD, dl,
Dan Gohman0b1d4a72008-12-23 21:37:04 +000011712 cast<AtomicSDNode>(Node)->getMemoryVT(),
Dale Johannesen71d1bf52008-09-29 22:25:26 +000011713 Node->getOperand(0),
11714 Node->getOperand(1), negOp,
11715 cast<AtomicSDNode>(Node)->getSrcValue(),
Eli Friedman55ba8162011-07-29 03:05:32 +000011716 cast<AtomicSDNode>(Node)->getAlignment(),
11717 cast<AtomicSDNode>(Node)->getOrdering(),
11718 cast<AtomicSDNode>(Node)->getSynchScope());
Mon P Wang63307c32008-05-05 19:05:59 +000011719}
11720
Eli Friedman327236c2011-08-24 20:50:09 +000011721static SDValue LowerATOMIC_STORE(SDValue Op, SelectionDAG &DAG) {
11722 SDNode *Node = Op.getNode();
11723 DebugLoc dl = Node->getDebugLoc();
Eli Friedmanf8f90f02011-08-24 22:33:28 +000011724 EVT VT = cast<AtomicSDNode>(Node)->getMemoryVT();
Eli Friedman327236c2011-08-24 20:50:09 +000011725
11726 // Convert seq_cst store -> xchg
Eli Friedmanf8f90f02011-08-24 22:33:28 +000011727 // Convert wide store -> swap (-> cmpxchg8b/cmpxchg16b)
11728 // FIXME: On 32-bit, store -> fist or movq would be more efficient
11729 // (The only way to get a 16-byte store is cmpxchg16b)
11730 // FIXME: 16-byte ATOMIC_SWAP isn't actually hooked up at the moment.
11731 if (cast<AtomicSDNode>(Node)->getOrdering() == SequentiallyConsistent ||
11732 !DAG.getTargetLoweringInfo().isTypeLegal(VT)) {
Eli Friedman4317fe12011-08-24 21:17:30 +000011733 SDValue Swap = DAG.getAtomic(ISD::ATOMIC_SWAP, dl,
11734 cast<AtomicSDNode>(Node)->getMemoryVT(),
11735 Node->getOperand(0),
11736 Node->getOperand(1), Node->getOperand(2),
Eli Friedmanf8f90f02011-08-24 22:33:28 +000011737 cast<AtomicSDNode>(Node)->getMemOperand(),
Eli Friedman4317fe12011-08-24 21:17:30 +000011738 cast<AtomicSDNode>(Node)->getOrdering(),
11739 cast<AtomicSDNode>(Node)->getSynchScope());
Eli Friedman327236c2011-08-24 20:50:09 +000011740 return Swap.getValue(1);
11741 }
11742 // Other atomic stores have a simple pattern.
11743 return Op;
11744}
11745
Chris Lattner5b856542010-12-20 00:59:46 +000011746static SDValue LowerADDC_ADDE_SUBC_SUBE(SDValue Op, SelectionDAG &DAG) {
11747 EVT VT = Op.getNode()->getValueType(0);
11748
11749 // Let legalize expand this if it isn't a legal type yet.
11750 if (!DAG.getTargetLoweringInfo().isTypeLegal(VT))
11751 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011752
Chris Lattner5b856542010-12-20 00:59:46 +000011753 SDVTList VTs = DAG.getVTList(VT, MVT::i32);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011754
Chris Lattner5b856542010-12-20 00:59:46 +000011755 unsigned Opc;
11756 bool ExtraOp = false;
11757 switch (Op.getOpcode()) {
Craig Topperabb94d02012-02-05 03:43:23 +000011758 default: llvm_unreachable("Invalid code");
Chris Lattner5b856542010-12-20 00:59:46 +000011759 case ISD::ADDC: Opc = X86ISD::ADD; break;
11760 case ISD::ADDE: Opc = X86ISD::ADC; ExtraOp = true; break;
11761 case ISD::SUBC: Opc = X86ISD::SUB; break;
11762 case ISD::SUBE: Opc = X86ISD::SBB; ExtraOp = true; break;
11763 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011764
Chris Lattner5b856542010-12-20 00:59:46 +000011765 if (!ExtraOp)
11766 return DAG.getNode(Opc, Op->getDebugLoc(), VTs, Op.getOperand(0),
11767 Op.getOperand(1));
11768 return DAG.getNode(Opc, Op->getDebugLoc(), VTs, Op.getOperand(0),
11769 Op.getOperand(1), Op.getOperand(2));
11770}
11771
Evan Cheng0db9fe62006-04-25 20:13:52 +000011772/// LowerOperation - Provide custom lowering hooks for some operations.
11773///
Dan Gohmand858e902010-04-17 15:26:15 +000011774SDValue X86TargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +000011775 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +000011776 default: llvm_unreachable("Should not custom lower this!");
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000011777 case ISD::SIGN_EXTEND_INREG: return LowerSIGN_EXTEND_INREG(Op,DAG);
Craig Topper55b24052012-09-11 06:15:32 +000011778 case ISD::MEMBARRIER: return LowerMEMBARRIER(Op, Subtarget, DAG);
11779 case ISD::ATOMIC_FENCE: return LowerATOMIC_FENCE(Op, Subtarget, DAG);
11780 case ISD::ATOMIC_CMP_SWAP: return LowerCMP_SWAP(Op, Subtarget, DAG);
Dan Gohman0b1d4a72008-12-23 21:37:04 +000011781 case ISD::ATOMIC_LOAD_SUB: return LowerLOAD_SUB(Op,DAG);
Eli Friedman327236c2011-08-24 20:50:09 +000011782 case ISD::ATOMIC_STORE: return LowerATOMIC_STORE(Op,DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000011783 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
Mon P Wangeb38ebf2010-01-24 00:05:03 +000011784 case ISD::CONCAT_VECTORS: return LowerCONCAT_VECTORS(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000011785 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
11786 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
11787 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
Craig Topper55b24052012-09-11 06:15:32 +000011788 case ISD::EXTRACT_SUBVECTOR: return LowerEXTRACT_SUBVECTOR(Op,Subtarget,DAG);
11789 case ISD::INSERT_SUBVECTOR: return LowerINSERT_SUBVECTOR(Op, Subtarget,DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000011790 case ISD::SCALAR_TO_VECTOR: return LowerSCALAR_TO_VECTOR(Op, DAG);
11791 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
11792 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +000011793 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Bill Wendling056292f2008-09-16 21:48:12 +000011794 case ISD::ExternalSymbol: return LowerExternalSymbol(Op, DAG);
Dan Gohmanf705adb2009-10-30 01:28:02 +000011795 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000011796 case ISD::SHL_PARTS:
11797 case ISD::SRA_PARTS:
Nadav Rotem43012222011-05-11 08:12:09 +000011798 case ISD::SRL_PARTS: return LowerShiftParts(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000011799 case ISD::SINT_TO_FP: return LowerSINT_TO_FP(Op, DAG);
Dale Johannesen1c15bf52008-10-21 20:50:01 +000011800 case ISD::UINT_TO_FP: return LowerUINT_TO_FP(Op, DAG);
Michael Liaobedcbd42012-10-16 18:14:11 +000011801 case ISD::TRUNCATE: return lowerTRUNCATE(Op, DAG);
Michael Liaoa7554632012-10-23 17:36:08 +000011802 case ISD::ZERO_EXTEND: return lowerZERO_EXTEND(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000011803 case ISD::FP_TO_SINT: return LowerFP_TO_SINT(Op, DAG);
Eli Friedman948e95a2009-05-23 09:59:16 +000011804 case ISD::FP_TO_UINT: return LowerFP_TO_UINT(Op, DAG);
Michael Liao9d796db2012-10-10 16:32:15 +000011805 case ISD::FP_EXTEND: return lowerFP_EXTEND(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000011806 case ISD::FABS: return LowerFABS(Op, DAG);
11807 case ISD::FNEG: return LowerFNEG(Op, DAG);
Evan Cheng68c47cb2007-01-05 07:55:56 +000011808 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Stuart Hastings4fd0dee2011-06-01 04:39:42 +000011809 case ISD::FGETSIGN: return LowerFGETSIGN(Op, DAG);
Evan Chenge5f62042007-09-29 00:00:36 +000011810 case ISD::SETCC: return LowerSETCC(Op, DAG);
11811 case ISD::SELECT: return LowerSELECT(Op, DAG);
11812 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000011813 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000011814 case ISD::VASTART: return LowerVASTART(Op, DAG);
Dan Gohman9018e832008-05-10 01:26:14 +000011815 case ISD::VAARG: return LowerVAARG(Op, DAG);
Craig Topper55b24052012-09-11 06:15:32 +000011816 case ISD::VACOPY: return LowerVACOPY(Op, Subtarget, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000011817 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
Benjamin Kramerb9bee042012-07-12 09:31:43 +000011818 case ISD::INTRINSIC_W_CHAIN: return LowerINTRINSIC_W_CHAIN(Op, DAG);
Nate Begemanbcc5f362007-01-29 22:58:52 +000011819 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
11820 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Anton Korobeynikov2365f512007-07-14 14:06:15 +000011821 case ISD::FRAME_TO_ARGS_OFFSET:
11822 return LowerFRAME_TO_ARGS_OFFSET(Op, DAG);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +000011823 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
Anton Korobeynikov2365f512007-07-14 14:06:15 +000011824 case ISD::EH_RETURN: return LowerEH_RETURN(Op, DAG);
Michael Liao6c0e04c2012-10-15 22:39:43 +000011825 case ISD::EH_SJLJ_SETJMP: return lowerEH_SJLJ_SETJMP(Op, DAG);
11826 case ISD::EH_SJLJ_LONGJMP: return lowerEH_SJLJ_LONGJMP(Op, DAG);
Duncan Sands4a544a72011-09-06 13:37:06 +000011827 case ISD::INIT_TRAMPOLINE: return LowerINIT_TRAMPOLINE(Op, DAG);
11828 case ISD::ADJUST_TRAMPOLINE: return LowerADJUST_TRAMPOLINE(Op, DAG);
Dan Gohman1a024862008-01-31 00:41:03 +000011829 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
Evan Cheng18efe262007-12-14 02:13:44 +000011830 case ISD::CTLZ: return LowerCTLZ(Op, DAG);
Chandler Carruthacc068e2011-12-24 10:55:54 +000011831 case ISD::CTLZ_ZERO_UNDEF: return LowerCTLZ_ZERO_UNDEF(Op, DAG);
Evan Cheng18efe262007-12-14 02:13:44 +000011832 case ISD::CTTZ: return LowerCTTZ(Op, DAG);
Craig Topper55b24052012-09-11 06:15:32 +000011833 case ISD::MUL: return LowerMUL(Op, Subtarget, DAG);
Nadav Rotem43012222011-05-11 08:12:09 +000011834 case ISD::SRA:
11835 case ISD::SRL:
11836 case ISD::SHL: return LowerShift(Op, DAG);
Bill Wendling74c37652008-12-09 22:08:41 +000011837 case ISD::SADDO:
11838 case ISD::UADDO:
11839 case ISD::SSUBO:
11840 case ISD::USUBO:
11841 case ISD::SMULO:
11842 case ISD::UMULO: return LowerXALUO(Op, DAG);
Craig Topper55b24052012-09-11 06:15:32 +000011843 case ISD::READCYCLECOUNTER: return LowerREADCYCLECOUNTER(Op, Subtarget,DAG);
Wesley Peckbf17cfa2010-11-23 03:31:01 +000011844 case ISD::BITCAST: return LowerBITCAST(Op, DAG);
Chris Lattner5b856542010-12-20 00:59:46 +000011845 case ISD::ADDC:
11846 case ISD::ADDE:
11847 case ISD::SUBC:
11848 case ISD::SUBE: return LowerADDC_ADDE_SUBC_SUBE(Op, DAG);
Craig Topper13894fa2011-08-24 06:14:18 +000011849 case ISD::ADD: return LowerADD(Op, DAG);
11850 case ISD::SUB: return LowerSUB(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000011851 }
Chris Lattner27a6c732007-11-24 07:07:01 +000011852}
11853
Eli Friedmanf8f90f02011-08-24 22:33:28 +000011854static void ReplaceATOMIC_LOAD(SDNode *Node,
11855 SmallVectorImpl<SDValue> &Results,
11856 SelectionDAG &DAG) {
11857 DebugLoc dl = Node->getDebugLoc();
11858 EVT VT = cast<AtomicSDNode>(Node)->getMemoryVT();
11859
11860 // Convert wide load -> cmpxchg8b/cmpxchg16b
11861 // FIXME: On 32-bit, load -> fild or movq would be more efficient
11862 // (The only way to get a 16-byte load is cmpxchg16b)
11863 // FIXME: 16-byte ATOMIC_CMP_SWAP isn't actually hooked up at the moment.
Benjamin Kramer2753ae32011-08-27 17:36:14 +000011864 SDValue Zero = DAG.getConstant(0, VT);
11865 SDValue Swap = DAG.getAtomic(ISD::ATOMIC_CMP_SWAP, dl, VT,
Eli Friedmanf8f90f02011-08-24 22:33:28 +000011866 Node->getOperand(0),
11867 Node->getOperand(1), Zero, Zero,
11868 cast<AtomicSDNode>(Node)->getMemOperand(),
11869 cast<AtomicSDNode>(Node)->getOrdering(),
11870 cast<AtomicSDNode>(Node)->getSynchScope());
11871 Results.push_back(Swap.getValue(0));
11872 Results.push_back(Swap.getValue(1));
11873}
11874
Craig Topperc0878702012-08-17 06:55:11 +000011875static void
Duncan Sands1607f052008-12-01 11:39:25 +000011876ReplaceATOMIC_BINARY_64(SDNode *Node, SmallVectorImpl<SDValue>&Results,
Craig Topperc0878702012-08-17 06:55:11 +000011877 SelectionDAG &DAG, unsigned NewOp) {
Dale Johannesene4d209d2009-02-03 20:21:25 +000011878 DebugLoc dl = Node->getDebugLoc();
Duncan Sands17001ce2011-10-18 12:44:00 +000011879 assert (Node->getValueType(0) == MVT::i64 &&
11880 "Only know how to expand i64 atomics");
Duncan Sands1607f052008-12-01 11:39:25 +000011881
11882 SDValue Chain = Node->getOperand(0);
11883 SDValue In1 = Node->getOperand(1);
Owen Anderson825b72b2009-08-11 20:47:22 +000011884 SDValue In2L = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +000011885 Node->getOperand(2), DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +000011886 SDValue In2H = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +000011887 Node->getOperand(2), DAG.getIntPtrConstant(1));
Dan Gohmanc76909a2009-09-25 20:36:54 +000011888 SDValue Ops[] = { Chain, In1, In2L, In2H };
Owen Anderson825b72b2009-08-11 20:47:22 +000011889 SDVTList Tys = DAG.getVTList(MVT::i32, MVT::i32, MVT::Other);
Dan Gohmanc76909a2009-09-25 20:36:54 +000011890 SDValue Result =
11891 DAG.getMemIntrinsicNode(NewOp, dl, Tys, Ops, 4, MVT::i64,
11892 cast<MemSDNode>(Node)->getMemOperand());
Duncan Sands1607f052008-12-01 11:39:25 +000011893 SDValue OpsF[] = { Result.getValue(0), Result.getValue(1)};
Owen Anderson825b72b2009-08-11 20:47:22 +000011894 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, OpsF, 2));
Duncan Sands1607f052008-12-01 11:39:25 +000011895 Results.push_back(Result.getValue(2));
11896}
11897
Duncan Sands126d9072008-07-04 11:47:58 +000011898/// ReplaceNodeResults - Replace a node with an illegal result type
11899/// with a new node built out of custom code.
Duncan Sands1607f052008-12-01 11:39:25 +000011900void X86TargetLowering::ReplaceNodeResults(SDNode *N,
11901 SmallVectorImpl<SDValue>&Results,
Dan Gohmand858e902010-04-17 15:26:15 +000011902 SelectionDAG &DAG) const {
Dale Johannesene4d209d2009-02-03 20:21:25 +000011903 DebugLoc dl = N->getDebugLoc();
Nadav Rotem0a1e9142012-12-14 21:20:37 +000011904 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Chris Lattner27a6c732007-11-24 07:07:01 +000011905 switch (N->getOpcode()) {
Duncan Sandsed294c42008-10-20 15:56:33 +000011906 default:
Craig Topperabb94d02012-02-05 03:43:23 +000011907 llvm_unreachable("Do not know how to custom type legalize this operation!");
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000011908 case ISD::SIGN_EXTEND_INREG:
Chris Lattner5b856542010-12-20 00:59:46 +000011909 case ISD::ADDC:
11910 case ISD::ADDE:
11911 case ISD::SUBC:
11912 case ISD::SUBE:
11913 // We don't want to expand or promote these.
11914 return;
Michael J. Spencer1a2d0612012-02-24 19:01:22 +000011915 case ISD::FP_TO_SINT:
11916 case ISD::FP_TO_UINT: {
11917 bool IsSigned = N->getOpcode() == ISD::FP_TO_SINT;
11918
11919 if (!IsSigned && !isIntegerTypeFTOL(SDValue(N, 0).getValueType()))
11920 return;
11921
Eli Friedman948e95a2009-05-23 09:59:16 +000011922 std::pair<SDValue,SDValue> Vals =
NAKAMURA Takumi9a68fdc2012-02-25 03:37:25 +000011923 FP_TO_INTHelper(SDValue(N, 0), DAG, IsSigned, /*IsReplace=*/ true);
Duncan Sands1607f052008-12-01 11:39:25 +000011924 SDValue FIST = Vals.first, StackSlot = Vals.second;
11925 if (FIST.getNode() != 0) {
Owen Andersone50ed302009-08-10 22:56:29 +000011926 EVT VT = N->getValueType(0);
Duncan Sands1607f052008-12-01 11:39:25 +000011927 // Return a load from the stack slot.
Michael J. Spencer1a2d0612012-02-24 19:01:22 +000011928 if (StackSlot.getNode() != 0)
11929 Results.push_back(DAG.getLoad(VT, dl, FIST, StackSlot,
11930 MachinePointerInfo(),
11931 false, false, false, 0));
11932 else
11933 Results.push_back(FIST);
Duncan Sands1607f052008-12-01 11:39:25 +000011934 }
11935 return;
11936 }
Michael Liao991b6a22012-10-24 04:09:32 +000011937 case ISD::UINT_TO_FP: {
11938 if (N->getOperand(0).getValueType() != MVT::v2i32 &&
11939 N->getValueType(0) != MVT::v2f32)
11940 return;
11941 SDValue ZExtIn = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v2i64,
11942 N->getOperand(0));
11943 SDValue Bias = DAG.getConstantFP(BitsToDouble(0x4330000000000000ULL),
11944 MVT::f64);
11945 SDValue VBias = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v2f64, Bias, Bias);
11946 SDValue Or = DAG.getNode(ISD::OR, dl, MVT::v2i64, ZExtIn,
11947 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64, VBias));
11948 Or = DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Or);
11949 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::v2f64, Or, VBias);
11950 Results.push_back(DAG.getNode(X86ISD::VFPROUND, dl, MVT::v4f32, Sub));
11951 return;
11952 }
Michael Liao44c2d612012-10-10 16:53:28 +000011953 case ISD::FP_ROUND: {
Nadav Rotem0a1e9142012-12-14 21:20:37 +000011954 if (!TLI.isTypeLegal(N->getOperand(0).getValueType()))
11955 return;
Michael Liao44c2d612012-10-10 16:53:28 +000011956 SDValue V = DAG.getNode(X86ISD::VFPROUND, dl, MVT::v4f32, N->getOperand(0));
11957 Results.push_back(V);
11958 return;
11959 }
Duncan Sands1607f052008-12-01 11:39:25 +000011960 case ISD::READCYCLECOUNTER: {
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +000011961 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Duncan Sands1607f052008-12-01 11:39:25 +000011962 SDValue TheChain = N->getOperand(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +000011963 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, dl, Tys, &TheChain, 1);
Owen Anderson825b72b2009-08-11 20:47:22 +000011964 SDValue eax = DAG.getCopyFromReg(rd, dl, X86::EAX, MVT::i32,
Dale Johannesendd64c412009-02-04 00:33:20 +000011965 rd.getValue(1));
Owen Anderson825b72b2009-08-11 20:47:22 +000011966 SDValue edx = DAG.getCopyFromReg(eax.getValue(1), dl, X86::EDX, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +000011967 eax.getValue(2));
11968 // Use a buildpair to merge the two 32-bit values into a 64-bit one.
11969 SDValue Ops[] = { eax, edx };
Owen Anderson825b72b2009-08-11 20:47:22 +000011970 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Ops, 2));
Duncan Sands1607f052008-12-01 11:39:25 +000011971 Results.push_back(edx.getValue(1));
11972 return;
11973 }
Dan Gohman0b1d4a72008-12-23 21:37:04 +000011974 case ISD::ATOMIC_CMP_SWAP: {
Owen Andersone50ed302009-08-10 22:56:29 +000011975 EVT T = N->getValueType(0);
Benjamin Kramer2753ae32011-08-27 17:36:14 +000011976 assert((T == MVT::i64 || T == MVT::i128) && "can only expand cmpxchg pair");
Eli Friedman43f51ae2011-08-26 21:21:21 +000011977 bool Regs64bit = T == MVT::i128;
11978 EVT HalfT = Regs64bit ? MVT::i64 : MVT::i32;
Duncan Sands1607f052008-12-01 11:39:25 +000011979 SDValue cpInL, cpInH;
Eli Friedman43f51ae2011-08-26 21:21:21 +000011980 cpInL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, HalfT, N->getOperand(2),
11981 DAG.getConstant(0, HalfT));
11982 cpInH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, HalfT, N->getOperand(2),
11983 DAG.getConstant(1, HalfT));
11984 cpInL = DAG.getCopyToReg(N->getOperand(0), dl,
11985 Regs64bit ? X86::RAX : X86::EAX,
11986 cpInL, SDValue());
11987 cpInH = DAG.getCopyToReg(cpInL.getValue(0), dl,
11988 Regs64bit ? X86::RDX : X86::EDX,
11989 cpInH, cpInL.getValue(1));
Duncan Sands1607f052008-12-01 11:39:25 +000011990 SDValue swapInL, swapInH;
Eli Friedman43f51ae2011-08-26 21:21:21 +000011991 swapInL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, HalfT, N->getOperand(3),
11992 DAG.getConstant(0, HalfT));
11993 swapInH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, HalfT, N->getOperand(3),
11994 DAG.getConstant(1, HalfT));
11995 swapInL = DAG.getCopyToReg(cpInH.getValue(0), dl,
11996 Regs64bit ? X86::RBX : X86::EBX,
11997 swapInL, cpInH.getValue(1));
11998 swapInH = DAG.getCopyToReg(swapInL.getValue(0), dl,
Chad Rosiera20e1e72012-08-01 18:39:17 +000011999 Regs64bit ? X86::RCX : X86::ECX,
Eli Friedman43f51ae2011-08-26 21:21:21 +000012000 swapInH, swapInL.getValue(1));
Duncan Sands1607f052008-12-01 11:39:25 +000012001 SDValue Ops[] = { swapInH.getValue(0),
12002 N->getOperand(1),
12003 swapInH.getValue(1) };
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +000012004 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Andrew Trick1a2cf3b2010-10-11 19:02:04 +000012005 MachineMemOperand *MMO = cast<AtomicSDNode>(N)->getMemOperand();
Eli Friedman43f51ae2011-08-26 21:21:21 +000012006 unsigned Opcode = Regs64bit ? X86ISD::LCMPXCHG16_DAG :
12007 X86ISD::LCMPXCHG8_DAG;
12008 SDValue Result = DAG.getMemIntrinsicNode(Opcode, dl, Tys,
Andrew Trick1a2cf3b2010-10-11 19:02:04 +000012009 Ops, 3, T, MMO);
Eli Friedman43f51ae2011-08-26 21:21:21 +000012010 SDValue cpOutL = DAG.getCopyFromReg(Result.getValue(0), dl,
12011 Regs64bit ? X86::RAX : X86::EAX,
12012 HalfT, Result.getValue(1));
12013 SDValue cpOutH = DAG.getCopyFromReg(cpOutL.getValue(1), dl,
12014 Regs64bit ? X86::RDX : X86::EDX,
12015 HalfT, cpOutL.getValue(2));
Duncan Sands1607f052008-12-01 11:39:25 +000012016 SDValue OpsF[] = { cpOutL.getValue(0), cpOutH.getValue(0)};
Eli Friedman43f51ae2011-08-26 21:21:21 +000012017 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, T, OpsF, 2));
Duncan Sands1607f052008-12-01 11:39:25 +000012018 Results.push_back(cpOutH.getValue(1));
12019 return;
12020 }
Dan Gohman0b1d4a72008-12-23 21:37:04 +000012021 case ISD::ATOMIC_LOAD_ADD:
Dan Gohman0b1d4a72008-12-23 21:37:04 +000012022 case ISD::ATOMIC_LOAD_AND:
Dan Gohman0b1d4a72008-12-23 21:37:04 +000012023 case ISD::ATOMIC_LOAD_NAND:
Dan Gohman0b1d4a72008-12-23 21:37:04 +000012024 case ISD::ATOMIC_LOAD_OR:
Dan Gohman0b1d4a72008-12-23 21:37:04 +000012025 case ISD::ATOMIC_LOAD_SUB:
Dan Gohman0b1d4a72008-12-23 21:37:04 +000012026 case ISD::ATOMIC_LOAD_XOR:
Michael Liaoe5e8f762012-09-25 18:08:13 +000012027 case ISD::ATOMIC_LOAD_MAX:
12028 case ISD::ATOMIC_LOAD_MIN:
12029 case ISD::ATOMIC_LOAD_UMAX:
12030 case ISD::ATOMIC_LOAD_UMIN:
Craig Topperc0878702012-08-17 06:55:11 +000012031 case ISD::ATOMIC_SWAP: {
12032 unsigned Opc;
12033 switch (N->getOpcode()) {
12034 default: llvm_unreachable("Unexpected opcode");
12035 case ISD::ATOMIC_LOAD_ADD:
12036 Opc = X86ISD::ATOMADD64_DAG;
12037 break;
12038 case ISD::ATOMIC_LOAD_AND:
12039 Opc = X86ISD::ATOMAND64_DAG;
12040 break;
12041 case ISD::ATOMIC_LOAD_NAND:
12042 Opc = X86ISD::ATOMNAND64_DAG;
12043 break;
12044 case ISD::ATOMIC_LOAD_OR:
12045 Opc = X86ISD::ATOMOR64_DAG;
12046 break;
12047 case ISD::ATOMIC_LOAD_SUB:
12048 Opc = X86ISD::ATOMSUB64_DAG;
12049 break;
12050 case ISD::ATOMIC_LOAD_XOR:
12051 Opc = X86ISD::ATOMXOR64_DAG;
12052 break;
Michael Liaoe5e8f762012-09-25 18:08:13 +000012053 case ISD::ATOMIC_LOAD_MAX:
12054 Opc = X86ISD::ATOMMAX64_DAG;
12055 break;
12056 case ISD::ATOMIC_LOAD_MIN:
12057 Opc = X86ISD::ATOMMIN64_DAG;
12058 break;
12059 case ISD::ATOMIC_LOAD_UMAX:
12060 Opc = X86ISD::ATOMUMAX64_DAG;
12061 break;
12062 case ISD::ATOMIC_LOAD_UMIN:
12063 Opc = X86ISD::ATOMUMIN64_DAG;
12064 break;
Craig Topperc0878702012-08-17 06:55:11 +000012065 case ISD::ATOMIC_SWAP:
12066 Opc = X86ISD::ATOMSWAP64_DAG;
12067 break;
12068 }
12069 ReplaceATOMIC_BINARY_64(N, Results, DAG, Opc);
Duncan Sands1607f052008-12-01 11:39:25 +000012070 return;
Craig Topperc0878702012-08-17 06:55:11 +000012071 }
Eli Friedmanf8f90f02011-08-24 22:33:28 +000012072 case ISD::ATOMIC_LOAD:
12073 ReplaceATOMIC_LOAD(N, Results, DAG);
Chris Lattner27a6c732007-11-24 07:07:01 +000012074 }
Evan Cheng0db9fe62006-04-25 20:13:52 +000012075}
12076
Evan Cheng72261582005-12-20 06:22:03 +000012077const char *X86TargetLowering::getTargetNodeName(unsigned Opcode) const {
12078 switch (Opcode) {
12079 default: return NULL;
Evan Cheng18efe262007-12-14 02:13:44 +000012080 case X86ISD::BSF: return "X86ISD::BSF";
12081 case X86ISD::BSR: return "X86ISD::BSR";
Evan Chenge3413162006-01-09 18:33:28 +000012082 case X86ISD::SHLD: return "X86ISD::SHLD";
12083 case X86ISD::SHRD: return "X86ISD::SHRD";
Evan Chengef6ffb12006-01-31 03:14:29 +000012084 case X86ISD::FAND: return "X86ISD::FAND";
Evan Cheng68c47cb2007-01-05 07:55:56 +000012085 case X86ISD::FOR: return "X86ISD::FOR";
Evan Cheng223547a2006-01-31 22:28:30 +000012086 case X86ISD::FXOR: return "X86ISD::FXOR";
Evan Cheng68c47cb2007-01-05 07:55:56 +000012087 case X86ISD::FSRL: return "X86ISD::FSRL";
Evan Chenga3195e82006-01-12 22:54:21 +000012088 case X86ISD::FILD: return "X86ISD::FILD";
Evan Chenge3de85b2006-02-04 02:20:30 +000012089 case X86ISD::FILD_FLAG: return "X86ISD::FILD_FLAG";
Evan Cheng72261582005-12-20 06:22:03 +000012090 case X86ISD::FP_TO_INT16_IN_MEM: return "X86ISD::FP_TO_INT16_IN_MEM";
12091 case X86ISD::FP_TO_INT32_IN_MEM: return "X86ISD::FP_TO_INT32_IN_MEM";
12092 case X86ISD::FP_TO_INT64_IN_MEM: return "X86ISD::FP_TO_INT64_IN_MEM";
Evan Chengb077b842005-12-21 02:39:21 +000012093 case X86ISD::FLD: return "X86ISD::FLD";
Evan Chengd90eb7f2006-01-05 00:27:02 +000012094 case X86ISD::FST: return "X86ISD::FST";
Evan Cheng72261582005-12-20 06:22:03 +000012095 case X86ISD::CALL: return "X86ISD::CALL";
Evan Cheng72261582005-12-20 06:22:03 +000012096 case X86ISD::RDTSC_DAG: return "X86ISD::RDTSC_DAG";
Dan Gohmanc7a37d42008-12-23 22:45:23 +000012097 case X86ISD::BT: return "X86ISD::BT";
Evan Cheng72261582005-12-20 06:22:03 +000012098 case X86ISD::CMP: return "X86ISD::CMP";
Evan Cheng6be2c582006-04-05 23:38:46 +000012099 case X86ISD::COMI: return "X86ISD::COMI";
12100 case X86ISD::UCOMI: return "X86ISD::UCOMI";
Evan Chengd5781fc2005-12-21 20:21:51 +000012101 case X86ISD::SETCC: return "X86ISD::SETCC";
Evan Chengad9c0a32009-12-15 00:53:42 +000012102 case X86ISD::SETCC_CARRY: return "X86ISD::SETCC_CARRY";
Stuart Hastings865f0932011-06-03 23:53:54 +000012103 case X86ISD::FSETCCsd: return "X86ISD::FSETCCsd";
12104 case X86ISD::FSETCCss: return "X86ISD::FSETCCss";
Evan Cheng72261582005-12-20 06:22:03 +000012105 case X86ISD::CMOV: return "X86ISD::CMOV";
12106 case X86ISD::BRCOND: return "X86ISD::BRCOND";
Evan Chengb077b842005-12-21 02:39:21 +000012107 case X86ISD::RET_FLAG: return "X86ISD::RET_FLAG";
Evan Cheng8df346b2006-03-04 01:12:00 +000012108 case X86ISD::REP_STOS: return "X86ISD::REP_STOS";
12109 case X86ISD::REP_MOVS: return "X86ISD::REP_MOVS";
Evan Cheng7ccced62006-02-18 00:15:05 +000012110 case X86ISD::GlobalBaseReg: return "X86ISD::GlobalBaseReg";
Evan Cheng020d2e82006-02-23 20:41:18 +000012111 case X86ISD::Wrapper: return "X86ISD::Wrapper";
Chris Lattner18c59872009-06-27 04:16:01 +000012112 case X86ISD::WrapperRIP: return "X86ISD::WrapperRIP";
Nate Begeman14d12ca2008-02-11 04:19:36 +000012113 case X86ISD::PEXTRB: return "X86ISD::PEXTRB";
Evan Chengb067a1e2006-03-31 19:22:53 +000012114 case X86ISD::PEXTRW: return "X86ISD::PEXTRW";
Nate Begeman14d12ca2008-02-11 04:19:36 +000012115 case X86ISD::INSERTPS: return "X86ISD::INSERTPS";
12116 case X86ISD::PINSRB: return "X86ISD::PINSRB";
Evan Cheng653159f2006-03-31 21:55:24 +000012117 case X86ISD::PINSRW: return "X86ISD::PINSRW";
Nate Begemanb9a47b82009-02-23 08:49:38 +000012118 case X86ISD::PSHUFB: return "X86ISD::PSHUFB";
Bruno Cardoso Lopesc1af4772011-07-13 21:36:47 +000012119 case X86ISD::ANDNP: return "X86ISD::ANDNP";
Craig Topper31133842011-11-19 07:33:10 +000012120 case X86ISD::PSIGN: return "X86ISD::PSIGN";
Craig Toppere6a62772011-11-13 17:31:07 +000012121 case X86ISD::BLENDV: return "X86ISD::BLENDV";
Elena Demikhovsky226e0e62012-12-05 09:24:57 +000012122 case X86ISD::BLENDI: return "X86ISD::BLENDI";
Benjamin Kramer388fc6a2012-12-15 16:47:44 +000012123 case X86ISD::SUBUS: return "X86ISD::SUBUS";
Craig Topperfe033152011-12-06 09:31:36 +000012124 case X86ISD::HADD: return "X86ISD::HADD";
12125 case X86ISD::HSUB: return "X86ISD::HSUB";
Craig Toppere6a62772011-11-13 17:31:07 +000012126 case X86ISD::FHADD: return "X86ISD::FHADD";
12127 case X86ISD::FHSUB: return "X86ISD::FHSUB";
Benjamin Kramer739c7a82012-12-21 14:04:55 +000012128 case X86ISD::UMAX: return "X86ISD::UMAX";
12129 case X86ISD::UMIN: return "X86ISD::UMIN";
12130 case X86ISD::SMAX: return "X86ISD::SMAX";
12131 case X86ISD::SMIN: return "X86ISD::SMIN";
Evan Cheng8ca29322006-11-10 21:43:37 +000012132 case X86ISD::FMAX: return "X86ISD::FMAX";
12133 case X86ISD::FMIN: return "X86ISD::FMIN";
Nadav Rotemd60cb112012-08-19 13:06:16 +000012134 case X86ISD::FMAXC: return "X86ISD::FMAXC";
12135 case X86ISD::FMINC: return "X86ISD::FMINC";
Dan Gohman20382522007-07-10 00:05:58 +000012136 case X86ISD::FRSQRT: return "X86ISD::FRSQRT";
12137 case X86ISD::FRCP: return "X86ISD::FRCP";
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +000012138 case X86ISD::TLSADDR: return "X86ISD::TLSADDR";
Hans Wennborgf0234fc2012-06-01 16:27:21 +000012139 case X86ISD::TLSBASEADDR: return "X86ISD::TLSBASEADDR";
Eric Christopher30ef0e52010-06-03 04:07:48 +000012140 case X86ISD::TLSCALL: return "X86ISD::TLSCALL";
Michael Liao6c0e04c2012-10-15 22:39:43 +000012141 case X86ISD::EH_SJLJ_SETJMP: return "X86ISD::EH_SJLJ_SETJMP";
12142 case X86ISD::EH_SJLJ_LONGJMP: return "X86ISD::EH_SJLJ_LONGJMP";
Anton Korobeynikov2365f512007-07-14 14:06:15 +000012143 case X86ISD::EH_RETURN: return "X86ISD::EH_RETURN";
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +000012144 case X86ISD::TC_RETURN: return "X86ISD::TC_RETURN";
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000012145 case X86ISD::FNSTCW16m: return "X86ISD::FNSTCW16m";
Benjamin Kramer17c836c2012-04-27 12:07:43 +000012146 case X86ISD::FNSTSW16r: return "X86ISD::FNSTSW16r";
Evan Cheng7e2ff772008-05-08 00:57:18 +000012147 case X86ISD::LCMPXCHG_DAG: return "X86ISD::LCMPXCHG_DAG";
12148 case X86ISD::LCMPXCHG8_DAG: return "X86ISD::LCMPXCHG8_DAG";
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012149 case X86ISD::ATOMADD64_DAG: return "X86ISD::ATOMADD64_DAG";
12150 case X86ISD::ATOMSUB64_DAG: return "X86ISD::ATOMSUB64_DAG";
12151 case X86ISD::ATOMOR64_DAG: return "X86ISD::ATOMOR64_DAG";
12152 case X86ISD::ATOMXOR64_DAG: return "X86ISD::ATOMXOR64_DAG";
12153 case X86ISD::ATOMAND64_DAG: return "X86ISD::ATOMAND64_DAG";
12154 case X86ISD::ATOMNAND64_DAG: return "X86ISD::ATOMNAND64_DAG";
Evan Chengd880b972008-05-09 21:53:03 +000012155 case X86ISD::VZEXT_MOVL: return "X86ISD::VZEXT_MOVL";
Michael Liaob7bf7262012-08-14 22:53:17 +000012156 case X86ISD::VSEXT_MOVL: return "X86ISD::VSEXT_MOVL";
Evan Chengd880b972008-05-09 21:53:03 +000012157 case X86ISD::VZEXT_LOAD: return "X86ISD::VZEXT_LOAD";
Michael Liaod9d09602012-10-23 17:34:00 +000012158 case X86ISD::VZEXT: return "X86ISD::VZEXT";
12159 case X86ISD::VSEXT: return "X86ISD::VSEXT";
Michael Liao7091b242012-08-14 21:24:47 +000012160 case X86ISD::VFPEXT: return "X86ISD::VFPEXT";
Michael Liao44c2d612012-10-10 16:53:28 +000012161 case X86ISD::VFPROUND: return "X86ISD::VFPROUND";
Craig Toppered2e13d2012-01-22 19:15:14 +000012162 case X86ISD::VSHLDQ: return "X86ISD::VSHLDQ";
12163 case X86ISD::VSRLDQ: return "X86ISD::VSRLDQ";
Evan Chengf26ffe92008-05-29 08:22:04 +000012164 case X86ISD::VSHL: return "X86ISD::VSHL";
12165 case X86ISD::VSRL: return "X86ISD::VSRL";
Craig Toppered2e13d2012-01-22 19:15:14 +000012166 case X86ISD::VSRA: return "X86ISD::VSRA";
12167 case X86ISD::VSHLI: return "X86ISD::VSHLI";
12168 case X86ISD::VSRLI: return "X86ISD::VSRLI";
12169 case X86ISD::VSRAI: return "X86ISD::VSRAI";
Craig Topper1906d322012-01-22 23:36:02 +000012170 case X86ISD::CMPP: return "X86ISD::CMPP";
Craig Topper67609fd2012-01-22 22:42:16 +000012171 case X86ISD::PCMPEQ: return "X86ISD::PCMPEQ";
12172 case X86ISD::PCMPGT: return "X86ISD::PCMPGT";
Bill Wendlingab55ebd2008-12-12 00:56:36 +000012173 case X86ISD::ADD: return "X86ISD::ADD";
12174 case X86ISD::SUB: return "X86ISD::SUB";
Chris Lattner5b856542010-12-20 00:59:46 +000012175 case X86ISD::ADC: return "X86ISD::ADC";
12176 case X86ISD::SBB: return "X86ISD::SBB";
Bill Wendlingd350e022008-12-12 21:15:41 +000012177 case X86ISD::SMUL: return "X86ISD::SMUL";
12178 case X86ISD::UMUL: return "X86ISD::UMUL";
Dan Gohman076aee32009-03-04 19:44:21 +000012179 case X86ISD::INC: return "X86ISD::INC";
12180 case X86ISD::DEC: return "X86ISD::DEC";
Dan Gohmane220c4b2009-09-18 19:59:53 +000012181 case X86ISD::OR: return "X86ISD::OR";
12182 case X86ISD::XOR: return "X86ISD::XOR";
12183 case X86ISD::AND: return "X86ISD::AND";
Craig Toppere6a62772011-11-13 17:31:07 +000012184 case X86ISD::BLSI: return "X86ISD::BLSI";
12185 case X86ISD::BLSMSK: return "X86ISD::BLSMSK";
12186 case X86ISD::BLSR: return "X86ISD::BLSR";
Evan Cheng73f24c92009-03-30 21:36:47 +000012187 case X86ISD::MUL_IMM: return "X86ISD::MUL_IMM";
Eric Christopher71c67532009-07-29 00:28:05 +000012188 case X86ISD::PTEST: return "X86ISD::PTEST";
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +000012189 case X86ISD::TESTP: return "X86ISD::TESTP";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000012190 case X86ISD::PALIGN: return "X86ISD::PALIGN";
12191 case X86ISD::PSHUFD: return "X86ISD::PSHUFD";
12192 case X86ISD::PSHUFHW: return "X86ISD::PSHUFHW";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000012193 case X86ISD::PSHUFLW: return "X86ISD::PSHUFLW";
Craig Topperb3982da2011-12-31 23:50:21 +000012194 case X86ISD::SHUFP: return "X86ISD::SHUFP";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000012195 case X86ISD::MOVLHPS: return "X86ISD::MOVLHPS";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000012196 case X86ISD::MOVLHPD: return "X86ISD::MOVLHPD";
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +000012197 case X86ISD::MOVHLPS: return "X86ISD::MOVHLPS";
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +000012198 case X86ISD::MOVLPS: return "X86ISD::MOVLPS";
12199 case X86ISD::MOVLPD: return "X86ISD::MOVLPD";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000012200 case X86ISD::MOVDDUP: return "X86ISD::MOVDDUP";
12201 case X86ISD::MOVSHDUP: return "X86ISD::MOVSHDUP";
12202 case X86ISD::MOVSLDUP: return "X86ISD::MOVSLDUP";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000012203 case X86ISD::MOVSD: return "X86ISD::MOVSD";
12204 case X86ISD::MOVSS: return "X86ISD::MOVSS";
Craig Topper34671b82011-12-06 08:21:25 +000012205 case X86ISD::UNPCKL: return "X86ISD::UNPCKL";
12206 case X86ISD::UNPCKH: return "X86ISD::UNPCKH";
Bruno Cardoso Lopes0e6d2302011-08-17 02:29:19 +000012207 case X86ISD::VBROADCAST: return "X86ISD::VBROADCAST";
Craig Topper316cd2a2011-11-30 06:25:25 +000012208 case X86ISD::VPERMILP: return "X86ISD::VPERMILP";
Craig Topperec24e612011-11-30 07:47:51 +000012209 case X86ISD::VPERM2X128: return "X86ISD::VPERM2X128";
Craig Topper8325c112012-04-16 00:41:45 +000012210 case X86ISD::VPERMV: return "X86ISD::VPERMV";
12211 case X86ISD::VPERMI: return "X86ISD::VPERMI";
Craig Topper5b209e82012-02-05 03:14:49 +000012212 case X86ISD::PMULUDQ: return "X86ISD::PMULUDQ";
Dan Gohmand6708ea2009-08-15 01:38:56 +000012213 case X86ISD::VASTART_SAVE_XMM_REGS: return "X86ISD::VASTART_SAVE_XMM_REGS";
Dan Gohman320afb82010-10-12 18:00:49 +000012214 case X86ISD::VAARG_64: return "X86ISD::VAARG_64";
Michael J. Spencere9c253e2010-10-21 01:41:01 +000012215 case X86ISD::WIN_ALLOCA: return "X86ISD::WIN_ALLOCA";
Eli Friedman14648462011-07-27 22:21:52 +000012216 case X86ISD::MEMBARRIER: return "X86ISD::MEMBARRIER";
Rafael Espindolad07b7ec2011-08-30 19:43:21 +000012217 case X86ISD::SEG_ALLOCA: return "X86ISD::SEG_ALLOCA";
Michael J. Spencer1a2d0612012-02-24 19:01:22 +000012218 case X86ISD::WIN_FTOL: return "X86ISD::WIN_FTOL";
Benjamin Kramer17c836c2012-04-27 12:07:43 +000012219 case X86ISD::SAHF: return "X86ISD::SAHF";
Benjamin Kramerb9bee042012-07-12 09:31:43 +000012220 case X86ISD::RDRAND: return "X86ISD::RDRAND";
Elena Demikhovsky1503aba2012-08-01 12:06:00 +000012221 case X86ISD::FMADD: return "X86ISD::FMADD";
12222 case X86ISD::FMSUB: return "X86ISD::FMSUB";
12223 case X86ISD::FNMADD: return "X86ISD::FNMADD";
12224 case X86ISD::FNMSUB: return "X86ISD::FNMSUB";
12225 case X86ISD::FMADDSUB: return "X86ISD::FMADDSUB";
12226 case X86ISD::FMSUBADD: return "X86ISD::FMSUBADD";
Craig Topper9c7ae012012-11-10 01:23:36 +000012227 case X86ISD::PCMPESTRI: return "X86ISD::PCMPESTRI";
12228 case X86ISD::PCMPISTRI: return "X86ISD::PCMPISTRI";
Evan Cheng72261582005-12-20 06:22:03 +000012229 }
12230}
Evan Cheng3a03ebb2005-12-21 23:05:39 +000012231
Chris Lattnerc9addb72007-03-30 23:15:24 +000012232// isLegalAddressingMode - Return true if the addressing mode represented
12233// by AM is legal for this target, for a load/store of the specified type.
Scott Michelfdc40a02009-02-17 22:15:04 +000012234bool X86TargetLowering::isLegalAddressingMode(const AddrMode &AM,
Chris Lattnerdb125cf2011-07-18 04:54:35 +000012235 Type *Ty) const {
Chris Lattnerc9addb72007-03-30 23:15:24 +000012236 // X86 supports extremely general addressing modes.
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000012237 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman92b651f2010-08-24 15:55:12 +000012238 Reloc::Model R = getTargetMachine().getRelocationModel();
Scott Michelfdc40a02009-02-17 22:15:04 +000012239
Chris Lattnerc9addb72007-03-30 23:15:24 +000012240 // X86 allows a sign-extended 32-bit immediate field as a displacement.
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000012241 if (!X86::isOffsetSuitableForCodeModel(AM.BaseOffs, M, AM.BaseGV != NULL))
Chris Lattnerc9addb72007-03-30 23:15:24 +000012242 return false;
Scott Michelfdc40a02009-02-17 22:15:04 +000012243
Chris Lattnerc9addb72007-03-30 23:15:24 +000012244 if (AM.BaseGV) {
Chris Lattnerdfed4132009-07-10 07:38:24 +000012245 unsigned GVFlags =
12246 Subtarget->ClassifyGlobalReference(AM.BaseGV, getTargetMachine());
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000012247
Chris Lattnerdfed4132009-07-10 07:38:24 +000012248 // If a reference to this global requires an extra load, we can't fold it.
12249 if (isGlobalStubReference(GVFlags))
Chris Lattnerc9addb72007-03-30 23:15:24 +000012250 return false;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000012251
Chris Lattnerdfed4132009-07-10 07:38:24 +000012252 // If BaseGV requires a register for the PIC base, we cannot also have a
12253 // BaseReg specified.
12254 if (AM.HasBaseReg && isGlobalRelativeToPICBase(GVFlags))
Dale Johannesen203af582008-12-05 21:47:27 +000012255 return false;
Evan Cheng52787842007-08-01 23:46:47 +000012256
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000012257 // If lower 4G is not available, then we must use rip-relative addressing.
Dan Gohman92b651f2010-08-24 15:55:12 +000012258 if ((M != CodeModel::Small || R != Reloc::Static) &&
12259 Subtarget->is64Bit() && (AM.BaseOffs || AM.Scale > 1))
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000012260 return false;
Chris Lattnerc9addb72007-03-30 23:15:24 +000012261 }
Scott Michelfdc40a02009-02-17 22:15:04 +000012262
Chris Lattnerc9addb72007-03-30 23:15:24 +000012263 switch (AM.Scale) {
12264 case 0:
12265 case 1:
12266 case 2:
12267 case 4:
12268 case 8:
12269 // These scales always work.
12270 break;
12271 case 3:
12272 case 5:
12273 case 9:
12274 // These scales are formed with basereg+scalereg. Only accept if there is
12275 // no basereg yet.
12276 if (AM.HasBaseReg)
12277 return false;
12278 break;
12279 default: // Other stuff never works.
12280 return false;
12281 }
Scott Michelfdc40a02009-02-17 22:15:04 +000012282
Chris Lattnerc9addb72007-03-30 23:15:24 +000012283 return true;
12284}
12285
Chris Lattnerdb125cf2011-07-18 04:54:35 +000012286bool X86TargetLowering::isTruncateFree(Type *Ty1, Type *Ty2) const {
Duncan Sandsb0bc6c32010-02-15 16:12:20 +000012287 if (!Ty1->isIntegerTy() || !Ty2->isIntegerTy())
Evan Cheng2bd122c2007-10-26 01:56:11 +000012288 return false;
Evan Chenge127a732007-10-29 07:57:50 +000012289 unsigned NumBits1 = Ty1->getPrimitiveSizeInBits();
12290 unsigned NumBits2 = Ty2->getPrimitiveSizeInBits();
Evan Cheng260e07e2008-03-20 02:18:41 +000012291 if (NumBits1 <= NumBits2)
Evan Chenge127a732007-10-29 07:57:50 +000012292 return false;
Dan Gohman377fbc02010-02-25 03:04:36 +000012293 return true;
Evan Cheng2bd122c2007-10-26 01:56:11 +000012294}
12295
Evan Cheng70e10d32012-07-17 06:53:39 +000012296bool X86TargetLowering::isLegalICmpImmediate(int64_t Imm) const {
12297 return Imm == (int32_t)Imm;
12298}
12299
12300bool X86TargetLowering::isLegalAddImmediate(int64_t Imm) const {
Evan Chenga9e13ba2012-07-17 18:54:11 +000012301 // Can also use sub to handle negated immediates.
Evan Cheng70e10d32012-07-17 06:53:39 +000012302 return Imm == (int32_t)Imm;
12303}
12304
Owen Andersone50ed302009-08-10 22:56:29 +000012305bool X86TargetLowering::isTruncateFree(EVT VT1, EVT VT2) const {
Duncan Sands83ec4b62008-06-06 12:08:01 +000012306 if (!VT1.isInteger() || !VT2.isInteger())
Evan Cheng3c3ddb32007-10-29 19:58:20 +000012307 return false;
Duncan Sands83ec4b62008-06-06 12:08:01 +000012308 unsigned NumBits1 = VT1.getSizeInBits();
12309 unsigned NumBits2 = VT2.getSizeInBits();
Evan Cheng260e07e2008-03-20 02:18:41 +000012310 if (NumBits1 <= NumBits2)
Evan Cheng3c3ddb32007-10-29 19:58:20 +000012311 return false;
Dan Gohman377fbc02010-02-25 03:04:36 +000012312 return true;
Evan Cheng3c3ddb32007-10-29 19:58:20 +000012313}
Evan Cheng2bd122c2007-10-26 01:56:11 +000012314
Chris Lattnerdb125cf2011-07-18 04:54:35 +000012315bool X86TargetLowering::isZExtFree(Type *Ty1, Type *Ty2) const {
Dan Gohman349ba492009-04-09 02:06:09 +000012316 // x86-64 implicitly zero-extends 32-bit results in 64-bit registers.
Duncan Sandsb0bc6c32010-02-15 16:12:20 +000012317 return Ty1->isIntegerTy(32) && Ty2->isIntegerTy(64) && Subtarget->is64Bit();
Dan Gohman97121ba2009-04-08 00:15:30 +000012318}
12319
Owen Andersone50ed302009-08-10 22:56:29 +000012320bool X86TargetLowering::isZExtFree(EVT VT1, EVT VT2) const {
Dan Gohman349ba492009-04-09 02:06:09 +000012321 // x86-64 implicitly zero-extends 32-bit results in 64-bit registers.
Owen Anderson825b72b2009-08-11 20:47:22 +000012322 return VT1 == MVT::i32 && VT2 == MVT::i64 && Subtarget->is64Bit();
Dan Gohman97121ba2009-04-08 00:15:30 +000012323}
12324
Evan Cheng2766a472012-12-06 19:13:27 +000012325bool X86TargetLowering::isZExtFree(SDValue Val, EVT VT2) const {
12326 EVT VT1 = Val.getValueType();
12327 if (isZExtFree(VT1, VT2))
12328 return true;
12329
12330 if (Val.getOpcode() != ISD::LOAD)
12331 return false;
12332
12333 if (!VT1.isSimple() || !VT1.isInteger() ||
12334 !VT2.isSimple() || !VT2.isInteger())
12335 return false;
12336
12337 switch (VT1.getSimpleVT().SimpleTy) {
12338 default: break;
12339 case MVT::i8:
12340 case MVT::i16:
12341 case MVT::i32:
12342 // X86 has 8, 16, and 32-bit zero-extending loads.
12343 return true;
12344 }
12345
12346 return false;
12347}
12348
Owen Andersone50ed302009-08-10 22:56:29 +000012349bool X86TargetLowering::isNarrowingProfitable(EVT VT1, EVT VT2) const {
Evan Cheng8b944d32009-05-28 00:35:15 +000012350 // i16 instructions are longer (0x66 prefix) and potentially slower.
Owen Anderson825b72b2009-08-11 20:47:22 +000012351 return !(VT1 == MVT::i32 && VT2 == MVT::i16);
Evan Cheng8b944d32009-05-28 00:35:15 +000012352}
12353
Evan Cheng60c07e12006-07-05 22:17:51 +000012354/// isShuffleMaskLegal - Targets can use this to indicate that they only
12355/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
12356/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
12357/// are assumed to be legal.
12358bool
Eric Christopherfd179292009-08-27 18:07:15 +000012359X86TargetLowering::isShuffleMaskLegal(const SmallVectorImpl<int> &M,
Owen Andersone50ed302009-08-10 22:56:29 +000012360 EVT VT) const {
Eric Christophercff6f852010-04-15 01:40:20 +000012361 // Very little shuffling can be done for 64-bit vectors right now.
Nate Begeman9008ca62009-04-27 18:41:29 +000012362 if (VT.getSizeInBits() == 64)
Craig Topper1dc0fbc2011-12-05 07:27:14 +000012363 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +000012364
Nate Begemana09008b2009-10-19 02:17:23 +000012365 // FIXME: pshufb, blends, shifts.
Nate Begeman9008ca62009-04-27 18:41:29 +000012366 return (VT.getVectorNumElements() == 2 ||
12367 ShuffleVectorSDNode::isSplatMask(&M[0], VT) ||
12368 isMOVLMask(M, VT) ||
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000012369 isSHUFPMask(M, VT, Subtarget->hasFp256()) ||
Nate Begeman9008ca62009-04-27 18:41:29 +000012370 isPSHUFDMask(M, VT) ||
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000012371 isPSHUFHWMask(M, VT, Subtarget->hasInt256()) ||
12372 isPSHUFLWMask(M, VT, Subtarget->hasInt256()) ||
Craig Topper0e2037b2012-01-20 05:53:00 +000012373 isPALIGNRMask(M, VT, Subtarget) ||
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000012374 isUNPCKLMask(M, VT, Subtarget->hasInt256()) ||
12375 isUNPCKHMask(M, VT, Subtarget->hasInt256()) ||
12376 isUNPCKL_v_undef_Mask(M, VT, Subtarget->hasInt256()) ||
12377 isUNPCKH_v_undef_Mask(M, VT, Subtarget->hasInt256()));
Evan Cheng60c07e12006-07-05 22:17:51 +000012378}
12379
Dan Gohman7d8143f2008-04-09 20:09:42 +000012380bool
Nate Begeman5a5ca152009-04-29 05:20:52 +000012381X86TargetLowering::isVectorClearMaskLegal(const SmallVectorImpl<int> &Mask,
Owen Andersone50ed302009-08-10 22:56:29 +000012382 EVT VT) const {
Nate Begeman9008ca62009-04-27 18:41:29 +000012383 unsigned NumElts = VT.getVectorNumElements();
12384 // FIXME: This collection of masks seems suspect.
12385 if (NumElts == 2)
12386 return true;
Craig Topper7a9a28b2012-08-12 02:23:29 +000012387 if (NumElts == 4 && VT.is128BitVector()) {
Nate Begeman9008ca62009-04-27 18:41:29 +000012388 return (isMOVLMask(Mask, VT) ||
12389 isCommutedMOVLMask(Mask, VT, true) ||
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000012390 isSHUFPMask(Mask, VT, Subtarget->hasFp256()) ||
12391 isSHUFPMask(Mask, VT, Subtarget->hasFp256(), /* Commuted */ true));
Evan Cheng60c07e12006-07-05 22:17:51 +000012392 }
12393 return false;
12394}
12395
12396//===----------------------------------------------------------------------===//
12397// X86 Scheduler Hooks
12398//===----------------------------------------------------------------------===//
12399
Michael Liaobe02a902012-11-08 07:28:54 +000012400/// Utility function to emit xbegin specifying the start of an RTM region.
Craig Topper2da36912012-11-11 22:45:02 +000012401static MachineBasicBlock *EmitXBegin(MachineInstr *MI, MachineBasicBlock *MBB,
12402 const TargetInstrInfo *TII) {
Michael Liaobe02a902012-11-08 07:28:54 +000012403 DebugLoc DL = MI->getDebugLoc();
Michael Liaobe02a902012-11-08 07:28:54 +000012404
12405 const BasicBlock *BB = MBB->getBasicBlock();
12406 MachineFunction::iterator I = MBB;
12407 ++I;
12408
12409 // For the v = xbegin(), we generate
12410 //
12411 // thisMBB:
12412 // xbegin sinkMBB
12413 //
12414 // mainMBB:
12415 // eax = -1
12416 //
12417 // sinkMBB:
12418 // v = eax
12419
12420 MachineBasicBlock *thisMBB = MBB;
12421 MachineFunction *MF = MBB->getParent();
12422 MachineBasicBlock *mainMBB = MF->CreateMachineBasicBlock(BB);
12423 MachineBasicBlock *sinkMBB = MF->CreateMachineBasicBlock(BB);
12424 MF->insert(I, mainMBB);
12425 MF->insert(I, sinkMBB);
12426
12427 // Transfer the remainder of BB and its successor edges to sinkMBB.
12428 sinkMBB->splice(sinkMBB->begin(), MBB,
12429 llvm::next(MachineBasicBlock::iterator(MI)), MBB->end());
12430 sinkMBB->transferSuccessorsAndUpdatePHIs(MBB);
12431
12432 // thisMBB:
12433 // xbegin sinkMBB
12434 // # fallthrough to mainMBB
12435 // # abortion to sinkMBB
12436 BuildMI(thisMBB, DL, TII->get(X86::XBEGIN_4)).addMBB(sinkMBB);
12437 thisMBB->addSuccessor(mainMBB);
12438 thisMBB->addSuccessor(sinkMBB);
12439
12440 // mainMBB:
12441 // EAX = -1
12442 BuildMI(mainMBB, DL, TII->get(X86::MOV32ri), X86::EAX).addImm(-1);
12443 mainMBB->addSuccessor(sinkMBB);
12444
12445 // sinkMBB:
12446 // EAX is live into the sinkMBB
12447 sinkMBB->addLiveIn(X86::EAX);
12448 BuildMI(*sinkMBB, sinkMBB->begin(), DL,
12449 TII->get(TargetOpcode::COPY), MI->getOperand(0).getReg())
12450 .addReg(X86::EAX);
12451
12452 MI->eraseFromParent();
12453 return sinkMBB;
12454}
12455
Michael Liaob118a072012-09-20 03:06:15 +000012456// Get CMPXCHG opcode for the specified data type.
12457static unsigned getCmpXChgOpcode(EVT VT) {
12458 switch (VT.getSimpleVT().SimpleTy) {
12459 case MVT::i8: return X86::LCMPXCHG8;
12460 case MVT::i16: return X86::LCMPXCHG16;
12461 case MVT::i32: return X86::LCMPXCHG32;
12462 case MVT::i64: return X86::LCMPXCHG64;
12463 default:
12464 break;
Richard Smith42fc29e2012-04-13 22:47:00 +000012465 }
Michael Liaob118a072012-09-20 03:06:15 +000012466 llvm_unreachable("Invalid operand size!");
Mon P Wang63307c32008-05-05 19:05:59 +000012467}
12468
Michael Liaob118a072012-09-20 03:06:15 +000012469// Get LOAD opcode for the specified data type.
12470static unsigned getLoadOpcode(EVT VT) {
12471 switch (VT.getSimpleVT().SimpleTy) {
12472 case MVT::i8: return X86::MOV8rm;
12473 case MVT::i16: return X86::MOV16rm;
12474 case MVT::i32: return X86::MOV32rm;
12475 case MVT::i64: return X86::MOV64rm;
12476 default:
12477 break;
12478 }
12479 llvm_unreachable("Invalid operand size!");
12480}
12481
12482// Get opcode of the non-atomic one from the specified atomic instruction.
12483static unsigned getNonAtomicOpcode(unsigned Opc) {
12484 switch (Opc) {
12485 case X86::ATOMAND8: return X86::AND8rr;
12486 case X86::ATOMAND16: return X86::AND16rr;
12487 case X86::ATOMAND32: return X86::AND32rr;
12488 case X86::ATOMAND64: return X86::AND64rr;
12489 case X86::ATOMOR8: return X86::OR8rr;
12490 case X86::ATOMOR16: return X86::OR16rr;
12491 case X86::ATOMOR32: return X86::OR32rr;
12492 case X86::ATOMOR64: return X86::OR64rr;
12493 case X86::ATOMXOR8: return X86::XOR8rr;
12494 case X86::ATOMXOR16: return X86::XOR16rr;
12495 case X86::ATOMXOR32: return X86::XOR32rr;
12496 case X86::ATOMXOR64: return X86::XOR64rr;
12497 }
12498 llvm_unreachable("Unhandled atomic-load-op opcode!");
12499}
12500
12501// Get opcode of the non-atomic one from the specified atomic instruction with
12502// extra opcode.
12503static unsigned getNonAtomicOpcodeWithExtraOpc(unsigned Opc,
12504 unsigned &ExtraOpc) {
12505 switch (Opc) {
12506 case X86::ATOMNAND8: ExtraOpc = X86::NOT8r; return X86::AND8rr;
12507 case X86::ATOMNAND16: ExtraOpc = X86::NOT16r; return X86::AND16rr;
12508 case X86::ATOMNAND32: ExtraOpc = X86::NOT32r; return X86::AND32rr;
12509 case X86::ATOMNAND64: ExtraOpc = X86::NOT64r; return X86::AND64rr;
Michael Liaofe87c302012-09-21 03:18:52 +000012510 case X86::ATOMMAX8: ExtraOpc = X86::CMP8rr; return X86::CMOVL32rr;
Michael Liaob118a072012-09-20 03:06:15 +000012511 case X86::ATOMMAX16: ExtraOpc = X86::CMP16rr; return X86::CMOVL16rr;
12512 case X86::ATOMMAX32: ExtraOpc = X86::CMP32rr; return X86::CMOVL32rr;
12513 case X86::ATOMMAX64: ExtraOpc = X86::CMP64rr; return X86::CMOVL64rr;
Michael Liaofe87c302012-09-21 03:18:52 +000012514 case X86::ATOMMIN8: ExtraOpc = X86::CMP8rr; return X86::CMOVG32rr;
Michael Liaob118a072012-09-20 03:06:15 +000012515 case X86::ATOMMIN16: ExtraOpc = X86::CMP16rr; return X86::CMOVG16rr;
12516 case X86::ATOMMIN32: ExtraOpc = X86::CMP32rr; return X86::CMOVG32rr;
12517 case X86::ATOMMIN64: ExtraOpc = X86::CMP64rr; return X86::CMOVG64rr;
Michael Liaofe87c302012-09-21 03:18:52 +000012518 case X86::ATOMUMAX8: ExtraOpc = X86::CMP8rr; return X86::CMOVB32rr;
Michael Liaob118a072012-09-20 03:06:15 +000012519 case X86::ATOMUMAX16: ExtraOpc = X86::CMP16rr; return X86::CMOVB16rr;
12520 case X86::ATOMUMAX32: ExtraOpc = X86::CMP32rr; return X86::CMOVB32rr;
12521 case X86::ATOMUMAX64: ExtraOpc = X86::CMP64rr; return X86::CMOVB64rr;
Michael Liaofe87c302012-09-21 03:18:52 +000012522 case X86::ATOMUMIN8: ExtraOpc = X86::CMP8rr; return X86::CMOVA32rr;
Michael Liaob118a072012-09-20 03:06:15 +000012523 case X86::ATOMUMIN16: ExtraOpc = X86::CMP16rr; return X86::CMOVA16rr;
12524 case X86::ATOMUMIN32: ExtraOpc = X86::CMP32rr; return X86::CMOVA32rr;
12525 case X86::ATOMUMIN64: ExtraOpc = X86::CMP64rr; return X86::CMOVA64rr;
12526 }
12527 llvm_unreachable("Unhandled atomic-load-op opcode!");
12528}
12529
12530// Get opcode of the non-atomic one from the specified atomic instruction for
12531// 64-bit data type on 32-bit target.
12532static unsigned getNonAtomic6432Opcode(unsigned Opc, unsigned &HiOpc) {
12533 switch (Opc) {
12534 case X86::ATOMAND6432: HiOpc = X86::AND32rr; return X86::AND32rr;
12535 case X86::ATOMOR6432: HiOpc = X86::OR32rr; return X86::OR32rr;
12536 case X86::ATOMXOR6432: HiOpc = X86::XOR32rr; return X86::XOR32rr;
12537 case X86::ATOMADD6432: HiOpc = X86::ADC32rr; return X86::ADD32rr;
12538 case X86::ATOMSUB6432: HiOpc = X86::SBB32rr; return X86::SUB32rr;
12539 case X86::ATOMSWAP6432: HiOpc = X86::MOV32rr; return X86::MOV32rr;
Michael Liaoe5e8f762012-09-25 18:08:13 +000012540 case X86::ATOMMAX6432: HiOpc = X86::SETLr; return X86::SETLr;
12541 case X86::ATOMMIN6432: HiOpc = X86::SETGr; return X86::SETGr;
12542 case X86::ATOMUMAX6432: HiOpc = X86::SETBr; return X86::SETBr;
12543 case X86::ATOMUMIN6432: HiOpc = X86::SETAr; return X86::SETAr;
Michael Liaob118a072012-09-20 03:06:15 +000012544 }
12545 llvm_unreachable("Unhandled atomic-load-op opcode!");
12546}
12547
12548// Get opcode of the non-atomic one from the specified atomic instruction for
12549// 64-bit data type on 32-bit target with extra opcode.
12550static unsigned getNonAtomic6432OpcodeWithExtraOpc(unsigned Opc,
12551 unsigned &HiOpc,
12552 unsigned &ExtraOpc) {
12553 switch (Opc) {
12554 case X86::ATOMNAND6432:
12555 ExtraOpc = X86::NOT32r;
12556 HiOpc = X86::AND32rr;
12557 return X86::AND32rr;
12558 }
12559 llvm_unreachable("Unhandled atomic-load-op opcode!");
12560}
12561
12562// Get pseudo CMOV opcode from the specified data type.
12563static unsigned getPseudoCMOVOpc(EVT VT) {
12564 switch (VT.getSimpleVT().SimpleTy) {
Michael Liaofe87c302012-09-21 03:18:52 +000012565 case MVT::i8: return X86::CMOV_GR8;
Michael Liaob118a072012-09-20 03:06:15 +000012566 case MVT::i16: return X86::CMOV_GR16;
12567 case MVT::i32: return X86::CMOV_GR32;
12568 default:
12569 break;
12570 }
12571 llvm_unreachable("Unknown CMOV opcode!");
12572}
12573
12574// EmitAtomicLoadArith - emit the code sequence for pseudo atomic instructions.
12575// They will be translated into a spin-loop or compare-exchange loop from
12576//
12577// ...
12578// dst = atomic-fetch-op MI.addr, MI.val
12579// ...
12580//
12581// to
12582//
12583// ...
12584// EAX = LOAD MI.addr
12585// loop:
12586// t1 = OP MI.val, EAX
12587// LCMPXCHG [MI.addr], t1, [EAX is implicitly used & defined]
12588// JNE loop
12589// sink:
12590// dst = EAX
12591// ...
Mon P Wang63307c32008-05-05 19:05:59 +000012592MachineBasicBlock *
Michael Liaob118a072012-09-20 03:06:15 +000012593X86TargetLowering::EmitAtomicLoadArith(MachineInstr *MI,
12594 MachineBasicBlock *MBB) const {
12595 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
12596 DebugLoc DL = MI->getDebugLoc();
12597
12598 MachineFunction *MF = MBB->getParent();
12599 MachineRegisterInfo &MRI = MF->getRegInfo();
12600
12601 const BasicBlock *BB = MBB->getBasicBlock();
12602 MachineFunction::iterator I = MBB;
12603 ++I;
12604
12605 assert(MI->getNumOperands() <= X86::AddrNumOperands + 2 &&
12606 "Unexpected number of operands");
12607
12608 assert(MI->hasOneMemOperand() &&
12609 "Expected atomic-load-op to have one memoperand");
12610
12611 // Memory Reference
12612 MachineInstr::mmo_iterator MMOBegin = MI->memoperands_begin();
12613 MachineInstr::mmo_iterator MMOEnd = MI->memoperands_end();
12614
12615 unsigned DstReg, SrcReg;
12616 unsigned MemOpndSlot;
12617
12618 unsigned CurOp = 0;
12619
12620 DstReg = MI->getOperand(CurOp++).getReg();
12621 MemOpndSlot = CurOp;
12622 CurOp += X86::AddrNumOperands;
12623 SrcReg = MI->getOperand(CurOp++).getReg();
12624
12625 const TargetRegisterClass *RC = MRI.getRegClass(DstReg);
Craig Topperf4d25a22012-09-30 19:49:56 +000012626 MVT::SimpleValueType VT = *RC->vt_begin();
Michael Liaob118a072012-09-20 03:06:15 +000012627 unsigned AccPhyReg = getX86SubSuperRegister(X86::EAX, VT);
12628
12629 unsigned LCMPXCHGOpc = getCmpXChgOpcode(VT);
12630 unsigned LOADOpc = getLoadOpcode(VT);
12631
12632 // For the atomic load-arith operator, we generate
12633 //
12634 // thisMBB:
12635 // EAX = LOAD [MI.addr]
12636 // mainMBB:
12637 // t1 = OP MI.val, EAX
12638 // LCMPXCHG [MI.addr], t1, [EAX is implicitly used & defined]
12639 // JNE mainMBB
12640 // sinkMBB:
12641
12642 MachineBasicBlock *thisMBB = MBB;
12643 MachineBasicBlock *mainMBB = MF->CreateMachineBasicBlock(BB);
12644 MachineBasicBlock *sinkMBB = MF->CreateMachineBasicBlock(BB);
12645 MF->insert(I, mainMBB);
12646 MF->insert(I, sinkMBB);
12647
12648 MachineInstrBuilder MIB;
12649
12650 // Transfer the remainder of BB and its successor edges to sinkMBB.
12651 sinkMBB->splice(sinkMBB->begin(), MBB,
12652 llvm::next(MachineBasicBlock::iterator(MI)), MBB->end());
12653 sinkMBB->transferSuccessorsAndUpdatePHIs(MBB);
12654
12655 // thisMBB:
12656 MIB = BuildMI(thisMBB, DL, TII->get(LOADOpc), AccPhyReg);
12657 for (unsigned i = 0; i < X86::AddrNumOperands; ++i)
12658 MIB.addOperand(MI->getOperand(MemOpndSlot + i));
12659 MIB.setMemRefs(MMOBegin, MMOEnd);
12660
12661 thisMBB->addSuccessor(mainMBB);
12662
12663 // mainMBB:
12664 MachineBasicBlock *origMainMBB = mainMBB;
12665 mainMBB->addLiveIn(AccPhyReg);
12666
12667 // Copy AccPhyReg as it is used more than once.
12668 unsigned AccReg = MRI.createVirtualRegister(RC);
12669 BuildMI(mainMBB, DL, TII->get(TargetOpcode::COPY), AccReg)
12670 .addReg(AccPhyReg);
12671
12672 unsigned t1 = MRI.createVirtualRegister(RC);
12673 unsigned Opc = MI->getOpcode();
12674 switch (Opc) {
12675 default:
12676 llvm_unreachable("Unhandled atomic-load-op opcode!");
12677 case X86::ATOMAND8:
12678 case X86::ATOMAND16:
12679 case X86::ATOMAND32:
12680 case X86::ATOMAND64:
12681 case X86::ATOMOR8:
12682 case X86::ATOMOR16:
12683 case X86::ATOMOR32:
12684 case X86::ATOMOR64:
12685 case X86::ATOMXOR8:
12686 case X86::ATOMXOR16:
12687 case X86::ATOMXOR32:
12688 case X86::ATOMXOR64: {
12689 unsigned ARITHOpc = getNonAtomicOpcode(Opc);
12690 BuildMI(mainMBB, DL, TII->get(ARITHOpc), t1).addReg(SrcReg)
12691 .addReg(AccReg);
12692 break;
12693 }
12694 case X86::ATOMNAND8:
12695 case X86::ATOMNAND16:
12696 case X86::ATOMNAND32:
12697 case X86::ATOMNAND64: {
12698 unsigned t2 = MRI.createVirtualRegister(RC);
12699 unsigned NOTOpc;
12700 unsigned ANDOpc = getNonAtomicOpcodeWithExtraOpc(Opc, NOTOpc);
12701 BuildMI(mainMBB, DL, TII->get(ANDOpc), t2).addReg(SrcReg)
12702 .addReg(AccReg);
12703 BuildMI(mainMBB, DL, TII->get(NOTOpc), t1).addReg(t2);
12704 break;
12705 }
Michael Liao08382492012-09-21 03:00:17 +000012706 case X86::ATOMMAX8:
Michael Liaob118a072012-09-20 03:06:15 +000012707 case X86::ATOMMAX16:
12708 case X86::ATOMMAX32:
12709 case X86::ATOMMAX64:
Michael Liaofe87c302012-09-21 03:18:52 +000012710 case X86::ATOMMIN8:
Michael Liaob118a072012-09-20 03:06:15 +000012711 case X86::ATOMMIN16:
12712 case X86::ATOMMIN32:
12713 case X86::ATOMMIN64:
Michael Liaofe87c302012-09-21 03:18:52 +000012714 case X86::ATOMUMAX8:
Michael Liaob118a072012-09-20 03:06:15 +000012715 case X86::ATOMUMAX16:
12716 case X86::ATOMUMAX32:
12717 case X86::ATOMUMAX64:
Michael Liaofe87c302012-09-21 03:18:52 +000012718 case X86::ATOMUMIN8:
Michael Liaob118a072012-09-20 03:06:15 +000012719 case X86::ATOMUMIN16:
12720 case X86::ATOMUMIN32:
12721 case X86::ATOMUMIN64: {
12722 unsigned CMPOpc;
12723 unsigned CMOVOpc = getNonAtomicOpcodeWithExtraOpc(Opc, CMPOpc);
12724
12725 BuildMI(mainMBB, DL, TII->get(CMPOpc))
12726 .addReg(SrcReg)
12727 .addReg(AccReg);
12728
12729 if (Subtarget->hasCMov()) {
Michael Liaofe87c302012-09-21 03:18:52 +000012730 if (VT != MVT::i8) {
12731 // Native support
12732 BuildMI(mainMBB, DL, TII->get(CMOVOpc), t1)
12733 .addReg(SrcReg)
12734 .addReg(AccReg);
12735 } else {
12736 // Promote i8 to i32 to use CMOV32
12737 const TargetRegisterClass *RC32 = getRegClassFor(MVT::i32);
12738 unsigned SrcReg32 = MRI.createVirtualRegister(RC32);
12739 unsigned AccReg32 = MRI.createVirtualRegister(RC32);
12740 unsigned t2 = MRI.createVirtualRegister(RC32);
12741
12742 unsigned Undef = MRI.createVirtualRegister(RC32);
12743 BuildMI(mainMBB, DL, TII->get(TargetOpcode::IMPLICIT_DEF), Undef);
12744
12745 BuildMI(mainMBB, DL, TII->get(TargetOpcode::INSERT_SUBREG), SrcReg32)
12746 .addReg(Undef)
12747 .addReg(SrcReg)
12748 .addImm(X86::sub_8bit);
12749 BuildMI(mainMBB, DL, TII->get(TargetOpcode::INSERT_SUBREG), AccReg32)
12750 .addReg(Undef)
12751 .addReg(AccReg)
12752 .addImm(X86::sub_8bit);
12753
12754 BuildMI(mainMBB, DL, TII->get(CMOVOpc), t2)
12755 .addReg(SrcReg32)
12756 .addReg(AccReg32);
12757
12758 BuildMI(mainMBB, DL, TII->get(TargetOpcode::COPY), t1)
12759 .addReg(t2, 0, X86::sub_8bit);
12760 }
Michael Liaob118a072012-09-20 03:06:15 +000012761 } else {
12762 // Use pseudo select and lower them.
Michael Liaofe87c302012-09-21 03:18:52 +000012763 assert((VT == MVT::i8 || VT == MVT::i16 || VT == MVT::i32) &&
Michael Liaob118a072012-09-20 03:06:15 +000012764 "Invalid atomic-load-op transformation!");
12765 unsigned SelOpc = getPseudoCMOVOpc(VT);
12766 X86::CondCode CC = X86::getCondFromCMovOpc(CMOVOpc);
12767 assert(CC != X86::COND_INVALID && "Invalid atomic-load-op transformation!");
12768 MIB = BuildMI(mainMBB, DL, TII->get(SelOpc), t1)
12769 .addReg(SrcReg).addReg(AccReg)
12770 .addImm(CC);
12771 mainMBB = EmitLoweredSelect(MIB, mainMBB);
12772 }
12773 break;
12774 }
12775 }
12776
12777 // Copy AccPhyReg back from virtual register.
12778 BuildMI(mainMBB, DL, TII->get(TargetOpcode::COPY), AccPhyReg)
12779 .addReg(AccReg);
12780
12781 MIB = BuildMI(mainMBB, DL, TII->get(LCMPXCHGOpc));
12782 for (unsigned i = 0; i < X86::AddrNumOperands; ++i)
12783 MIB.addOperand(MI->getOperand(MemOpndSlot + i));
12784 MIB.addReg(t1);
12785 MIB.setMemRefs(MMOBegin, MMOEnd);
12786
12787 BuildMI(mainMBB, DL, TII->get(X86::JNE_4)).addMBB(origMainMBB);
12788
12789 mainMBB->addSuccessor(origMainMBB);
12790 mainMBB->addSuccessor(sinkMBB);
12791
12792 // sinkMBB:
12793 sinkMBB->addLiveIn(AccPhyReg);
12794
12795 BuildMI(*sinkMBB, sinkMBB->begin(), DL,
12796 TII->get(TargetOpcode::COPY), DstReg)
12797 .addReg(AccPhyReg);
12798
12799 MI->eraseFromParent();
12800 return sinkMBB;
12801}
12802
12803// EmitAtomicLoadArith6432 - emit the code sequence for pseudo atomic
12804// instructions. They will be translated into a spin-loop or compare-exchange
12805// loop from
12806//
12807// ...
12808// dst = atomic-fetch-op MI.addr, MI.val
12809// ...
12810//
12811// to
12812//
12813// ...
12814// EAX = LOAD [MI.addr + 0]
12815// EDX = LOAD [MI.addr + 4]
12816// loop:
12817// EBX = OP MI.val.lo, EAX
12818// ECX = OP MI.val.hi, EDX
12819// LCMPXCHG8B [MI.addr], [ECX:EBX & EDX:EAX are implicitly used and EDX:EAX is implicitly defined]
12820// JNE loop
12821// sink:
12822// dst = EDX:EAX
12823// ...
12824MachineBasicBlock *
12825X86TargetLowering::EmitAtomicLoadArith6432(MachineInstr *MI,
12826 MachineBasicBlock *MBB) const {
12827 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
12828 DebugLoc DL = MI->getDebugLoc();
12829
12830 MachineFunction *MF = MBB->getParent();
12831 MachineRegisterInfo &MRI = MF->getRegInfo();
12832
12833 const BasicBlock *BB = MBB->getBasicBlock();
12834 MachineFunction::iterator I = MBB;
12835 ++I;
12836
12837 assert(MI->getNumOperands() <= X86::AddrNumOperands + 4 &&
12838 "Unexpected number of operands");
12839
12840 assert(MI->hasOneMemOperand() &&
12841 "Expected atomic-load-op32 to have one memoperand");
12842
12843 // Memory Reference
12844 MachineInstr::mmo_iterator MMOBegin = MI->memoperands_begin();
12845 MachineInstr::mmo_iterator MMOEnd = MI->memoperands_end();
12846
12847 unsigned DstLoReg, DstHiReg;
12848 unsigned SrcLoReg, SrcHiReg;
12849 unsigned MemOpndSlot;
12850
12851 unsigned CurOp = 0;
12852
12853 DstLoReg = MI->getOperand(CurOp++).getReg();
12854 DstHiReg = MI->getOperand(CurOp++).getReg();
12855 MemOpndSlot = CurOp;
12856 CurOp += X86::AddrNumOperands;
12857 SrcLoReg = MI->getOperand(CurOp++).getReg();
12858 SrcHiReg = MI->getOperand(CurOp++).getReg();
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012859
Craig Topperc9099502012-04-20 06:31:50 +000012860 const TargetRegisterClass *RC = &X86::GR32RegClass;
Michael Liaoe5e8f762012-09-25 18:08:13 +000012861 const TargetRegisterClass *RC8 = &X86::GR8RegClass;
Scott Michelfdc40a02009-02-17 22:15:04 +000012862
Michael Liaob118a072012-09-20 03:06:15 +000012863 unsigned LCMPXCHGOpc = X86::LCMPXCHG8B;
12864 unsigned LOADOpc = X86::MOV32rm;
Scott Michelfdc40a02009-02-17 22:15:04 +000012865
Michael Liaob118a072012-09-20 03:06:15 +000012866 // For the atomic load-arith operator, we generate
Mon P Wang63307c32008-05-05 19:05:59 +000012867 //
Michael Liaob118a072012-09-20 03:06:15 +000012868 // thisMBB:
12869 // EAX = LOAD [MI.addr + 0]
12870 // EDX = LOAD [MI.addr + 4]
12871 // mainMBB:
12872 // EBX = OP MI.vallo, EAX
12873 // ECX = OP MI.valhi, EDX
12874 // LCMPXCHG8B [MI.addr], [ECX:EBX & EDX:EAX are implicitly used and EDX:EAX is implicitly defined]
12875 // JNE mainMBB
12876 // sinkMBB:
Scott Michelfdc40a02009-02-17 22:15:04 +000012877
Mon P Wang63307c32008-05-05 19:05:59 +000012878 MachineBasicBlock *thisMBB = MBB;
Michael Liaob118a072012-09-20 03:06:15 +000012879 MachineBasicBlock *mainMBB = MF->CreateMachineBasicBlock(BB);
12880 MachineBasicBlock *sinkMBB = MF->CreateMachineBasicBlock(BB);
12881 MF->insert(I, mainMBB);
12882 MF->insert(I, sinkMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000012883
Michael Liaob118a072012-09-20 03:06:15 +000012884 MachineInstrBuilder MIB;
Scott Michelfdc40a02009-02-17 22:15:04 +000012885
Michael Liaob118a072012-09-20 03:06:15 +000012886 // Transfer the remainder of BB and its successor edges to sinkMBB.
12887 sinkMBB->splice(sinkMBB->begin(), MBB,
12888 llvm::next(MachineBasicBlock::iterator(MI)), MBB->end());
12889 sinkMBB->transferSuccessorsAndUpdatePHIs(MBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000012890
Michael Liaob118a072012-09-20 03:06:15 +000012891 // thisMBB:
12892 // Lo
12893 MIB = BuildMI(thisMBB, DL, TII->get(LOADOpc), X86::EAX);
12894 for (unsigned i = 0; i < X86::AddrNumOperands; ++i)
12895 MIB.addOperand(MI->getOperand(MemOpndSlot + i));
12896 MIB.setMemRefs(MMOBegin, MMOEnd);
12897 // Hi
12898 MIB = BuildMI(thisMBB, DL, TII->get(LOADOpc), X86::EDX);
12899 for (unsigned i = 0; i < X86::AddrNumOperands; ++i) {
Evan Chenga395f4d2012-10-11 00:15:48 +000012900 if (i == X86::AddrDisp)
Michael Liaob118a072012-09-20 03:06:15 +000012901 MIB.addDisp(MI->getOperand(MemOpndSlot + i), 4); // 4 == sizeof(i32)
Evan Chenga395f4d2012-10-11 00:15:48 +000012902 else
Michael Liaob118a072012-09-20 03:06:15 +000012903 MIB.addOperand(MI->getOperand(MemOpndSlot + i));
12904 }
12905 MIB.setMemRefs(MMOBegin, MMOEnd);
Scott Michelfdc40a02009-02-17 22:15:04 +000012906
Michael Liaob118a072012-09-20 03:06:15 +000012907 thisMBB->addSuccessor(mainMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000012908
Michael Liaob118a072012-09-20 03:06:15 +000012909 // mainMBB:
12910 MachineBasicBlock *origMainMBB = mainMBB;
12911 mainMBB->addLiveIn(X86::EAX);
12912 mainMBB->addLiveIn(X86::EDX);
Scott Michelfdc40a02009-02-17 22:15:04 +000012913
Michael Liaob118a072012-09-20 03:06:15 +000012914 // Copy EDX:EAX as they are used more than once.
12915 unsigned LoReg = MRI.createVirtualRegister(RC);
12916 unsigned HiReg = MRI.createVirtualRegister(RC);
12917 BuildMI(mainMBB, DL, TII->get(TargetOpcode::COPY), LoReg).addReg(X86::EAX);
12918 BuildMI(mainMBB, DL, TII->get(TargetOpcode::COPY), HiReg).addReg(X86::EDX);
Mon P Wangab3e7472008-05-05 22:56:23 +000012919
Michael Liaob118a072012-09-20 03:06:15 +000012920 unsigned t1L = MRI.createVirtualRegister(RC);
12921 unsigned t1H = MRI.createVirtualRegister(RC);
Scott Michelfdc40a02009-02-17 22:15:04 +000012922
Michael Liaob118a072012-09-20 03:06:15 +000012923 unsigned Opc = MI->getOpcode();
12924 switch (Opc) {
12925 default:
12926 llvm_unreachable("Unhandled atomic-load-op6432 opcode!");
12927 case X86::ATOMAND6432:
12928 case X86::ATOMOR6432:
12929 case X86::ATOMXOR6432:
12930 case X86::ATOMADD6432:
12931 case X86::ATOMSUB6432: {
12932 unsigned HiOpc;
12933 unsigned LoOpc = getNonAtomic6432Opcode(Opc, HiOpc);
Michael Liaodd3383f2012-11-12 06:49:17 +000012934 BuildMI(mainMBB, DL, TII->get(LoOpc), t1L).addReg(LoReg).addReg(SrcLoReg);
12935 BuildMI(mainMBB, DL, TII->get(HiOpc), t1H).addReg(HiReg).addReg(SrcHiReg);
Michael Liaob118a072012-09-20 03:06:15 +000012936 break;
12937 }
12938 case X86::ATOMNAND6432: {
12939 unsigned HiOpc, NOTOpc;
12940 unsigned LoOpc = getNonAtomic6432OpcodeWithExtraOpc(Opc, HiOpc, NOTOpc);
12941 unsigned t2L = MRI.createVirtualRegister(RC);
12942 unsigned t2H = MRI.createVirtualRegister(RC);
12943 BuildMI(mainMBB, DL, TII->get(LoOpc), t2L).addReg(SrcLoReg).addReg(LoReg);
12944 BuildMI(mainMBB, DL, TII->get(HiOpc), t2H).addReg(SrcHiReg).addReg(HiReg);
12945 BuildMI(mainMBB, DL, TII->get(NOTOpc), t1L).addReg(t2L);
12946 BuildMI(mainMBB, DL, TII->get(NOTOpc), t1H).addReg(t2H);
12947 break;
12948 }
Michael Liaoe5e8f762012-09-25 18:08:13 +000012949 case X86::ATOMMAX6432:
12950 case X86::ATOMMIN6432:
12951 case X86::ATOMUMAX6432:
12952 case X86::ATOMUMIN6432: {
12953 unsigned HiOpc;
12954 unsigned LoOpc = getNonAtomic6432Opcode(Opc, HiOpc);
12955 unsigned cL = MRI.createVirtualRegister(RC8);
12956 unsigned cH = MRI.createVirtualRegister(RC8);
12957 unsigned cL32 = MRI.createVirtualRegister(RC);
12958 unsigned cH32 = MRI.createVirtualRegister(RC);
12959 unsigned cc = MRI.createVirtualRegister(RC);
12960 // cl := cmp src_lo, lo
12961 BuildMI(mainMBB, DL, TII->get(X86::CMP32rr))
12962 .addReg(SrcLoReg).addReg(LoReg);
12963 BuildMI(mainMBB, DL, TII->get(LoOpc), cL);
12964 BuildMI(mainMBB, DL, TII->get(X86::MOVZX32rr8), cL32).addReg(cL);
12965 // ch := cmp src_hi, hi
12966 BuildMI(mainMBB, DL, TII->get(X86::CMP32rr))
12967 .addReg(SrcHiReg).addReg(HiReg);
12968 BuildMI(mainMBB, DL, TII->get(HiOpc), cH);
12969 BuildMI(mainMBB, DL, TII->get(X86::MOVZX32rr8), cH32).addReg(cH);
12970 // cc := if (src_hi == hi) ? cl : ch;
12971 if (Subtarget->hasCMov()) {
12972 BuildMI(mainMBB, DL, TII->get(X86::CMOVE32rr), cc)
12973 .addReg(cH32).addReg(cL32);
12974 } else {
12975 MIB = BuildMI(mainMBB, DL, TII->get(X86::CMOV_GR32), cc)
12976 .addReg(cH32).addReg(cL32)
12977 .addImm(X86::COND_E);
12978 mainMBB = EmitLoweredSelect(MIB, mainMBB);
12979 }
12980 BuildMI(mainMBB, DL, TII->get(X86::TEST32rr)).addReg(cc).addReg(cc);
12981 if (Subtarget->hasCMov()) {
12982 BuildMI(mainMBB, DL, TII->get(X86::CMOVNE32rr), t1L)
12983 .addReg(SrcLoReg).addReg(LoReg);
12984 BuildMI(mainMBB, DL, TII->get(X86::CMOVNE32rr), t1H)
12985 .addReg(SrcHiReg).addReg(HiReg);
12986 } else {
12987 MIB = BuildMI(mainMBB, DL, TII->get(X86::CMOV_GR32), t1L)
12988 .addReg(SrcLoReg).addReg(LoReg)
12989 .addImm(X86::COND_NE);
12990 mainMBB = EmitLoweredSelect(MIB, mainMBB);
12991 MIB = BuildMI(mainMBB, DL, TII->get(X86::CMOV_GR32), t1H)
12992 .addReg(SrcHiReg).addReg(HiReg)
12993 .addImm(X86::COND_NE);
12994 mainMBB = EmitLoweredSelect(MIB, mainMBB);
12995 }
12996 break;
12997 }
Michael Liaob118a072012-09-20 03:06:15 +000012998 case X86::ATOMSWAP6432: {
12999 unsigned HiOpc;
13000 unsigned LoOpc = getNonAtomic6432Opcode(Opc, HiOpc);
13001 BuildMI(mainMBB, DL, TII->get(LoOpc), t1L).addReg(SrcLoReg);
13002 BuildMI(mainMBB, DL, TII->get(HiOpc), t1H).addReg(SrcHiReg);
13003 break;
13004 }
13005 }
Mon P Wang63307c32008-05-05 19:05:59 +000013006
Michael Liaob118a072012-09-20 03:06:15 +000013007 // Copy EDX:EAX back from HiReg:LoReg
13008 BuildMI(mainMBB, DL, TII->get(TargetOpcode::COPY), X86::EAX).addReg(LoReg);
13009 BuildMI(mainMBB, DL, TII->get(TargetOpcode::COPY), X86::EDX).addReg(HiReg);
13010 // Copy ECX:EBX from t1H:t1L
13011 BuildMI(mainMBB, DL, TII->get(TargetOpcode::COPY), X86::EBX).addReg(t1L);
13012 BuildMI(mainMBB, DL, TII->get(TargetOpcode::COPY), X86::ECX).addReg(t1H);
Mon P Wangab3e7472008-05-05 22:56:23 +000013013
Michael Liaob118a072012-09-20 03:06:15 +000013014 MIB = BuildMI(mainMBB, DL, TII->get(LCMPXCHGOpc));
13015 for (unsigned i = 0; i < X86::AddrNumOperands; ++i)
13016 MIB.addOperand(MI->getOperand(MemOpndSlot + i));
13017 MIB.setMemRefs(MMOBegin, MMOEnd);
Mon P Wang63307c32008-05-05 19:05:59 +000013018
Michael Liaob118a072012-09-20 03:06:15 +000013019 BuildMI(mainMBB, DL, TII->get(X86::JNE_4)).addMBB(origMainMBB);
Mon P Wang63307c32008-05-05 19:05:59 +000013020
Michael Liaob118a072012-09-20 03:06:15 +000013021 mainMBB->addSuccessor(origMainMBB);
13022 mainMBB->addSuccessor(sinkMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000013023
Michael Liaob118a072012-09-20 03:06:15 +000013024 // sinkMBB:
13025 sinkMBB->addLiveIn(X86::EAX);
13026 sinkMBB->addLiveIn(X86::EDX);
Scott Michelfdc40a02009-02-17 22:15:04 +000013027
Michael Liaob118a072012-09-20 03:06:15 +000013028 BuildMI(*sinkMBB, sinkMBB->begin(), DL,
13029 TII->get(TargetOpcode::COPY), DstLoReg)
13030 .addReg(X86::EAX);
13031 BuildMI(*sinkMBB, sinkMBB->begin(), DL,
13032 TII->get(TargetOpcode::COPY), DstHiReg)
13033 .addReg(X86::EDX);
Mon P Wang63307c32008-05-05 19:05:59 +000013034
Michael Liaob118a072012-09-20 03:06:15 +000013035 MI->eraseFromParent();
13036 return sinkMBB;
Mon P Wang63307c32008-05-05 19:05:59 +000013037}
13038
Eric Christopherf83a5de2009-08-27 18:08:16 +000013039// FIXME: When we get size specific XMM0 registers, i.e. XMM0_V16I8
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000013040// or XMM0_V32I8 in AVX all of this code can be replaced with that
13041// in the .td file.
Craig Topper8cb8c812012-11-10 09:02:47 +000013042static MachineBasicBlock *EmitPCMPSTRM(MachineInstr *MI, MachineBasicBlock *BB,
13043 const TargetInstrInfo *TII) {
Eric Christopherb120ab42009-08-18 22:50:32 +000013044 unsigned Opc;
Craig Topper8aae8dd2012-11-10 08:57:41 +000013045 switch (MI->getOpcode()) {
13046 default: llvm_unreachable("illegal opcode!");
13047 case X86::PCMPISTRM128REG: Opc = X86::PCMPISTRM128rr; break;
13048 case X86::VPCMPISTRM128REG: Opc = X86::VPCMPISTRM128rr; break;
13049 case X86::PCMPISTRM128MEM: Opc = X86::PCMPISTRM128rm; break;
13050 case X86::VPCMPISTRM128MEM: Opc = X86::VPCMPISTRM128rm; break;
13051 case X86::PCMPESTRM128REG: Opc = X86::PCMPESTRM128rr; break;
13052 case X86::VPCMPESTRM128REG: Opc = X86::VPCMPESTRM128rr; break;
13053 case X86::PCMPESTRM128MEM: Opc = X86::PCMPESTRM128rm; break;
13054 case X86::VPCMPESTRM128MEM: Opc = X86::VPCMPESTRM128rm; break;
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000013055 }
Eric Christopherb120ab42009-08-18 22:50:32 +000013056
Craig Topper8aae8dd2012-11-10 08:57:41 +000013057 DebugLoc dl = MI->getDebugLoc();
Eric Christopher41c902f2010-11-30 08:20:21 +000013058 MachineInstrBuilder MIB = BuildMI(*BB, MI, dl, TII->get(Opc));
Craig Topper8aae8dd2012-11-10 08:57:41 +000013059
Craig Topper52ea2452012-11-10 09:25:36 +000013060 unsigned NumArgs = MI->getNumOperands();
13061 for (unsigned i = 1; i < NumArgs; ++i) {
13062 MachineOperand &Op = MI->getOperand(i);
Eric Christopherb120ab42009-08-18 22:50:32 +000013063 if (!(Op.isReg() && Op.isImplicit()))
13064 MIB.addOperand(Op);
13065 }
Craig Topper8aae8dd2012-11-10 08:57:41 +000013066 if (MI->hasOneMemOperand())
Craig Topper9c7ae012012-11-10 01:23:36 +000013067 MIB->setMemRefs(MI->memoperands_begin(), MI->memoperands_end());
13068
Bruno Cardoso Lopes5affa512011-08-31 03:04:09 +000013069 BuildMI(*BB, MI, dl,
Craig Topper638aa682012-08-05 00:17:48 +000013070 TII->get(TargetOpcode::COPY), MI->getOperand(0).getReg())
Eric Christopherb120ab42009-08-18 22:50:32 +000013071 .addReg(X86::XMM0);
13072
Dan Gohman14152b42010-07-06 20:24:04 +000013073 MI->eraseFromParent();
Eric Christopherb120ab42009-08-18 22:50:32 +000013074 return BB;
13075}
13076
Craig Topper9c7ae012012-11-10 01:23:36 +000013077// FIXME: Custom handling because TableGen doesn't support multiple implicit
13078// defs in an instruction pattern
Craig Topper8cb8c812012-11-10 09:02:47 +000013079static MachineBasicBlock *EmitPCMPSTRI(MachineInstr *MI, MachineBasicBlock *BB,
13080 const TargetInstrInfo *TII) {
Craig Topper9c7ae012012-11-10 01:23:36 +000013081 unsigned Opc;
Craig Topper8aae8dd2012-11-10 08:57:41 +000013082 switch (MI->getOpcode()) {
13083 default: llvm_unreachable("illegal opcode!");
13084 case X86::PCMPISTRIREG: Opc = X86::PCMPISTRIrr; break;
13085 case X86::VPCMPISTRIREG: Opc = X86::VPCMPISTRIrr; break;
13086 case X86::PCMPISTRIMEM: Opc = X86::PCMPISTRIrm; break;
13087 case X86::VPCMPISTRIMEM: Opc = X86::VPCMPISTRIrm; break;
13088 case X86::PCMPESTRIREG: Opc = X86::PCMPESTRIrr; break;
13089 case X86::VPCMPESTRIREG: Opc = X86::VPCMPESTRIrr; break;
13090 case X86::PCMPESTRIMEM: Opc = X86::PCMPESTRIrm; break;
13091 case X86::VPCMPESTRIMEM: Opc = X86::VPCMPESTRIrm; break;
Craig Topper9c7ae012012-11-10 01:23:36 +000013092 }
13093
Craig Topper8aae8dd2012-11-10 08:57:41 +000013094 DebugLoc dl = MI->getDebugLoc();
Craig Topper9c7ae012012-11-10 01:23:36 +000013095 MachineInstrBuilder MIB = BuildMI(*BB, MI, dl, TII->get(Opc));
Craig Topper8aae8dd2012-11-10 08:57:41 +000013096
Craig Topper52ea2452012-11-10 09:25:36 +000013097 unsigned NumArgs = MI->getNumOperands(); // remove the results
13098 for (unsigned i = 1; i < NumArgs; ++i) {
13099 MachineOperand &Op = MI->getOperand(i);
Craig Topper9c7ae012012-11-10 01:23:36 +000013100 if (!(Op.isReg() && Op.isImplicit()))
13101 MIB.addOperand(Op);
13102 }
Craig Topper8aae8dd2012-11-10 08:57:41 +000013103 if (MI->hasOneMemOperand())
Craig Topper9c7ae012012-11-10 01:23:36 +000013104 MIB->setMemRefs(MI->memoperands_begin(), MI->memoperands_end());
13105
13106 BuildMI(*BB, MI, dl,
13107 TII->get(TargetOpcode::COPY), MI->getOperand(0).getReg())
13108 .addReg(X86::ECX);
13109
13110 MI->eraseFromParent();
13111 return BB;
13112}
13113
Craig Topper2da36912012-11-11 22:45:02 +000013114static MachineBasicBlock * EmitMonitor(MachineInstr *MI, MachineBasicBlock *BB,
13115 const TargetInstrInfo *TII,
13116 const X86Subtarget* Subtarget) {
Eric Christopher228232b2010-11-30 07:20:12 +000013117 DebugLoc dl = MI->getDebugLoc();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013118
Eric Christopher228232b2010-11-30 07:20:12 +000013119 // Address into RAX/EAX, other two args into ECX, EDX.
13120 unsigned MemOpc = Subtarget->is64Bit() ? X86::LEA64r : X86::LEA32r;
13121 unsigned MemReg = Subtarget->is64Bit() ? X86::RAX : X86::EAX;
13122 MachineInstrBuilder MIB = BuildMI(*BB, MI, dl, TII->get(MemOpc), MemReg);
13123 for (int i = 0; i < X86::AddrNumOperands; ++i)
Eric Christopher82be2202010-11-30 08:10:28 +000013124 MIB.addOperand(MI->getOperand(i));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013125
Eric Christopher228232b2010-11-30 07:20:12 +000013126 unsigned ValOps = X86::AddrNumOperands;
13127 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::ECX)
13128 .addReg(MI->getOperand(ValOps).getReg());
13129 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::EDX)
13130 .addReg(MI->getOperand(ValOps+1).getReg());
13131
13132 // The instruction doesn't actually take any operands though.
13133 BuildMI(*BB, MI, dl, TII->get(X86::MONITORrrr));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013134
Eric Christopher228232b2010-11-30 07:20:12 +000013135 MI->eraseFromParent(); // The pseudo is gone now.
13136 return BB;
13137}
13138
13139MachineBasicBlock *
Dan Gohman320afb82010-10-12 18:00:49 +000013140X86TargetLowering::EmitVAARG64WithCustomInserter(
13141 MachineInstr *MI,
13142 MachineBasicBlock *MBB) const {
13143 // Emit va_arg instruction on X86-64.
13144
13145 // Operands to this pseudo-instruction:
13146 // 0 ) Output : destination address (reg)
13147 // 1-5) Input : va_list address (addr, i64mem)
13148 // 6 ) ArgSize : Size (in bytes) of vararg type
13149 // 7 ) ArgMode : 0=overflow only, 1=use gp_offset, 2=use fp_offset
13150 // 8 ) Align : Alignment of type
13151 // 9 ) EFLAGS (implicit-def)
13152
13153 assert(MI->getNumOperands() == 10 && "VAARG_64 should have 10 operands!");
13154 assert(X86::AddrNumOperands == 5 && "VAARG_64 assumes 5 address operands");
13155
13156 unsigned DestReg = MI->getOperand(0).getReg();
13157 MachineOperand &Base = MI->getOperand(1);
13158 MachineOperand &Scale = MI->getOperand(2);
13159 MachineOperand &Index = MI->getOperand(3);
13160 MachineOperand &Disp = MI->getOperand(4);
13161 MachineOperand &Segment = MI->getOperand(5);
13162 unsigned ArgSize = MI->getOperand(6).getImm();
13163 unsigned ArgMode = MI->getOperand(7).getImm();
13164 unsigned Align = MI->getOperand(8).getImm();
13165
13166 // Memory Reference
13167 assert(MI->hasOneMemOperand() && "Expected VAARG_64 to have one memoperand");
13168 MachineInstr::mmo_iterator MMOBegin = MI->memoperands_begin();
13169 MachineInstr::mmo_iterator MMOEnd = MI->memoperands_end();
13170
13171 // Machine Information
13172 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
13173 MachineRegisterInfo &MRI = MBB->getParent()->getRegInfo();
13174 const TargetRegisterClass *AddrRegClass = getRegClassFor(MVT::i64);
13175 const TargetRegisterClass *OffsetRegClass = getRegClassFor(MVT::i32);
13176 DebugLoc DL = MI->getDebugLoc();
13177
13178 // struct va_list {
13179 // i32 gp_offset
13180 // i32 fp_offset
13181 // i64 overflow_area (address)
13182 // i64 reg_save_area (address)
13183 // }
13184 // sizeof(va_list) = 24
13185 // alignment(va_list) = 8
13186
13187 unsigned TotalNumIntRegs = 6;
13188 unsigned TotalNumXMMRegs = 8;
13189 bool UseGPOffset = (ArgMode == 1);
13190 bool UseFPOffset = (ArgMode == 2);
13191 unsigned MaxOffset = TotalNumIntRegs * 8 +
13192 (UseFPOffset ? TotalNumXMMRegs * 16 : 0);
13193
13194 /* Align ArgSize to a multiple of 8 */
13195 unsigned ArgSizeA8 = (ArgSize + 7) & ~7;
13196 bool NeedsAlign = (Align > 8);
13197
13198 MachineBasicBlock *thisMBB = MBB;
13199 MachineBasicBlock *overflowMBB;
13200 MachineBasicBlock *offsetMBB;
13201 MachineBasicBlock *endMBB;
13202
13203 unsigned OffsetDestReg = 0; // Argument address computed by offsetMBB
13204 unsigned OverflowDestReg = 0; // Argument address computed by overflowMBB
13205 unsigned OffsetReg = 0;
13206
13207 if (!UseGPOffset && !UseFPOffset) {
13208 // If we only pull from the overflow region, we don't create a branch.
13209 // We don't need to alter control flow.
13210 OffsetDestReg = 0; // unused
13211 OverflowDestReg = DestReg;
13212
13213 offsetMBB = NULL;
13214 overflowMBB = thisMBB;
13215 endMBB = thisMBB;
13216 } else {
13217 // First emit code to check if gp_offset (or fp_offset) is below the bound.
13218 // If so, pull the argument from reg_save_area. (branch to offsetMBB)
13219 // If not, pull from overflow_area. (branch to overflowMBB)
13220 //
13221 // thisMBB
13222 // | .
13223 // | .
13224 // offsetMBB overflowMBB
13225 // | .
13226 // | .
13227 // endMBB
13228
13229 // Registers for the PHI in endMBB
13230 OffsetDestReg = MRI.createVirtualRegister(AddrRegClass);
13231 OverflowDestReg = MRI.createVirtualRegister(AddrRegClass);
13232
13233 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
13234 MachineFunction *MF = MBB->getParent();
13235 overflowMBB = MF->CreateMachineBasicBlock(LLVM_BB);
13236 offsetMBB = MF->CreateMachineBasicBlock(LLVM_BB);
13237 endMBB = MF->CreateMachineBasicBlock(LLVM_BB);
13238
13239 MachineFunction::iterator MBBIter = MBB;
13240 ++MBBIter;
13241
13242 // Insert the new basic blocks
13243 MF->insert(MBBIter, offsetMBB);
13244 MF->insert(MBBIter, overflowMBB);
13245 MF->insert(MBBIter, endMBB);
13246
13247 // Transfer the remainder of MBB and its successor edges to endMBB.
13248 endMBB->splice(endMBB->begin(), thisMBB,
13249 llvm::next(MachineBasicBlock::iterator(MI)),
13250 thisMBB->end());
13251 endMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
13252
13253 // Make offsetMBB and overflowMBB successors of thisMBB
13254 thisMBB->addSuccessor(offsetMBB);
13255 thisMBB->addSuccessor(overflowMBB);
13256
13257 // endMBB is a successor of both offsetMBB and overflowMBB
13258 offsetMBB->addSuccessor(endMBB);
13259 overflowMBB->addSuccessor(endMBB);
13260
13261 // Load the offset value into a register
13262 OffsetReg = MRI.createVirtualRegister(OffsetRegClass);
13263 BuildMI(thisMBB, DL, TII->get(X86::MOV32rm), OffsetReg)
13264 .addOperand(Base)
13265 .addOperand(Scale)
13266 .addOperand(Index)
13267 .addDisp(Disp, UseFPOffset ? 4 : 0)
13268 .addOperand(Segment)
13269 .setMemRefs(MMOBegin, MMOEnd);
13270
13271 // Check if there is enough room left to pull this argument.
13272 BuildMI(thisMBB, DL, TII->get(X86::CMP32ri))
13273 .addReg(OffsetReg)
13274 .addImm(MaxOffset + 8 - ArgSizeA8);
13275
13276 // Branch to "overflowMBB" if offset >= max
13277 // Fall through to "offsetMBB" otherwise
13278 BuildMI(thisMBB, DL, TII->get(X86::GetCondBranchFromCond(X86::COND_AE)))
13279 .addMBB(overflowMBB);
13280 }
13281
13282 // In offsetMBB, emit code to use the reg_save_area.
13283 if (offsetMBB) {
13284 assert(OffsetReg != 0);
13285
13286 // Read the reg_save_area address.
13287 unsigned RegSaveReg = MRI.createVirtualRegister(AddrRegClass);
13288 BuildMI(offsetMBB, DL, TII->get(X86::MOV64rm), RegSaveReg)
13289 .addOperand(Base)
13290 .addOperand(Scale)
13291 .addOperand(Index)
13292 .addDisp(Disp, 16)
13293 .addOperand(Segment)
13294 .setMemRefs(MMOBegin, MMOEnd);
13295
13296 // Zero-extend the offset
13297 unsigned OffsetReg64 = MRI.createVirtualRegister(AddrRegClass);
13298 BuildMI(offsetMBB, DL, TII->get(X86::SUBREG_TO_REG), OffsetReg64)
13299 .addImm(0)
13300 .addReg(OffsetReg)
13301 .addImm(X86::sub_32bit);
13302
13303 // Add the offset to the reg_save_area to get the final address.
13304 BuildMI(offsetMBB, DL, TII->get(X86::ADD64rr), OffsetDestReg)
13305 .addReg(OffsetReg64)
13306 .addReg(RegSaveReg);
13307
13308 // Compute the offset for the next argument
13309 unsigned NextOffsetReg = MRI.createVirtualRegister(OffsetRegClass);
13310 BuildMI(offsetMBB, DL, TII->get(X86::ADD32ri), NextOffsetReg)
13311 .addReg(OffsetReg)
13312 .addImm(UseFPOffset ? 16 : 8);
13313
13314 // Store it back into the va_list.
13315 BuildMI(offsetMBB, DL, TII->get(X86::MOV32mr))
13316 .addOperand(Base)
13317 .addOperand(Scale)
13318 .addOperand(Index)
13319 .addDisp(Disp, UseFPOffset ? 4 : 0)
13320 .addOperand(Segment)
13321 .addReg(NextOffsetReg)
13322 .setMemRefs(MMOBegin, MMOEnd);
13323
13324 // Jump to endMBB
13325 BuildMI(offsetMBB, DL, TII->get(X86::JMP_4))
13326 .addMBB(endMBB);
13327 }
13328
13329 //
13330 // Emit code to use overflow area
13331 //
13332
13333 // Load the overflow_area address into a register.
13334 unsigned OverflowAddrReg = MRI.createVirtualRegister(AddrRegClass);
13335 BuildMI(overflowMBB, DL, TII->get(X86::MOV64rm), OverflowAddrReg)
13336 .addOperand(Base)
13337 .addOperand(Scale)
13338 .addOperand(Index)
13339 .addDisp(Disp, 8)
13340 .addOperand(Segment)
13341 .setMemRefs(MMOBegin, MMOEnd);
13342
13343 // If we need to align it, do so. Otherwise, just copy the address
13344 // to OverflowDestReg.
13345 if (NeedsAlign) {
13346 // Align the overflow address
13347 assert((Align & (Align-1)) == 0 && "Alignment must be a power of 2");
13348 unsigned TmpReg = MRI.createVirtualRegister(AddrRegClass);
13349
13350 // aligned_addr = (addr + (align-1)) & ~(align-1)
13351 BuildMI(overflowMBB, DL, TII->get(X86::ADD64ri32), TmpReg)
13352 .addReg(OverflowAddrReg)
13353 .addImm(Align-1);
13354
13355 BuildMI(overflowMBB, DL, TII->get(X86::AND64ri32), OverflowDestReg)
13356 .addReg(TmpReg)
13357 .addImm(~(uint64_t)(Align-1));
13358 } else {
13359 BuildMI(overflowMBB, DL, TII->get(TargetOpcode::COPY), OverflowDestReg)
13360 .addReg(OverflowAddrReg);
13361 }
13362
13363 // Compute the next overflow address after this argument.
13364 // (the overflow address should be kept 8-byte aligned)
13365 unsigned NextAddrReg = MRI.createVirtualRegister(AddrRegClass);
13366 BuildMI(overflowMBB, DL, TII->get(X86::ADD64ri32), NextAddrReg)
13367 .addReg(OverflowDestReg)
13368 .addImm(ArgSizeA8);
13369
13370 // Store the new overflow address.
13371 BuildMI(overflowMBB, DL, TII->get(X86::MOV64mr))
13372 .addOperand(Base)
13373 .addOperand(Scale)
13374 .addOperand(Index)
13375 .addDisp(Disp, 8)
13376 .addOperand(Segment)
13377 .addReg(NextAddrReg)
13378 .setMemRefs(MMOBegin, MMOEnd);
13379
13380 // If we branched, emit the PHI to the front of endMBB.
13381 if (offsetMBB) {
13382 BuildMI(*endMBB, endMBB->begin(), DL,
13383 TII->get(X86::PHI), DestReg)
13384 .addReg(OffsetDestReg).addMBB(offsetMBB)
13385 .addReg(OverflowDestReg).addMBB(overflowMBB);
13386 }
13387
13388 // Erase the pseudo instruction
13389 MI->eraseFromParent();
13390
13391 return endMBB;
13392}
13393
13394MachineBasicBlock *
Dan Gohmand6708ea2009-08-15 01:38:56 +000013395X86TargetLowering::EmitVAStartSaveXMMRegsWithCustomInserter(
13396 MachineInstr *MI,
13397 MachineBasicBlock *MBB) const {
13398 // Emit code to save XMM registers to the stack. The ABI says that the
13399 // number of registers to save is given in %al, so it's theoretically
13400 // possible to do an indirect jump trick to avoid saving all of them,
13401 // however this code takes a simpler approach and just executes all
13402 // of the stores if %al is non-zero. It's less code, and it's probably
13403 // easier on the hardware branch predictor, and stores aren't all that
13404 // expensive anyway.
13405
13406 // Create the new basic blocks. One block contains all the XMM stores,
13407 // and one block is the final destination regardless of whether any
13408 // stores were performed.
13409 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
13410 MachineFunction *F = MBB->getParent();
13411 MachineFunction::iterator MBBIter = MBB;
13412 ++MBBIter;
13413 MachineBasicBlock *XMMSaveMBB = F->CreateMachineBasicBlock(LLVM_BB);
13414 MachineBasicBlock *EndMBB = F->CreateMachineBasicBlock(LLVM_BB);
13415 F->insert(MBBIter, XMMSaveMBB);
13416 F->insert(MBBIter, EndMBB);
13417
Dan Gohman14152b42010-07-06 20:24:04 +000013418 // Transfer the remainder of MBB and its successor edges to EndMBB.
13419 EndMBB->splice(EndMBB->begin(), MBB,
13420 llvm::next(MachineBasicBlock::iterator(MI)),
13421 MBB->end());
13422 EndMBB->transferSuccessorsAndUpdatePHIs(MBB);
13423
Dan Gohmand6708ea2009-08-15 01:38:56 +000013424 // The original block will now fall through to the XMM save block.
13425 MBB->addSuccessor(XMMSaveMBB);
13426 // The XMMSaveMBB will fall through to the end block.
13427 XMMSaveMBB->addSuccessor(EndMBB);
13428
13429 // Now add the instructions.
13430 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
13431 DebugLoc DL = MI->getDebugLoc();
13432
13433 unsigned CountReg = MI->getOperand(0).getReg();
13434 int64_t RegSaveFrameIndex = MI->getOperand(1).getImm();
13435 int64_t VarArgsFPOffset = MI->getOperand(2).getImm();
13436
13437 if (!Subtarget->isTargetWin64()) {
13438 // If %al is 0, branch around the XMM save block.
13439 BuildMI(MBB, DL, TII->get(X86::TEST8rr)).addReg(CountReg).addReg(CountReg);
Chris Lattnerbd13fb62010-02-11 19:25:55 +000013440 BuildMI(MBB, DL, TII->get(X86::JE_4)).addMBB(EndMBB);
Dan Gohmand6708ea2009-08-15 01:38:56 +000013441 MBB->addSuccessor(EndMBB);
13442 }
13443
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000013444 unsigned MOVOpc = Subtarget->hasFp256() ? X86::VMOVAPSmr : X86::MOVAPSmr;
Dan Gohmand6708ea2009-08-15 01:38:56 +000013445 // In the XMM save block, save all the XMM argument registers.
13446 for (int i = 3, e = MI->getNumOperands(); i != e; ++i) {
13447 int64_t Offset = (i - 3) * 16 + VarArgsFPOffset;
Dan Gohmanc76909a2009-09-25 20:36:54 +000013448 MachineMemOperand *MMO =
Evan Chengff89dcb2009-10-18 18:16:27 +000013449 F->getMachineMemOperand(
Chris Lattnere8639032010-09-21 06:22:23 +000013450 MachinePointerInfo::getFixedStack(RegSaveFrameIndex, Offset),
Chris Lattner59db5492010-09-21 04:39:43 +000013451 MachineMemOperand::MOStore,
Evan Chengff89dcb2009-10-18 18:16:27 +000013452 /*Size=*/16, /*Align=*/16);
Bruno Cardoso Lopes5affa512011-08-31 03:04:09 +000013453 BuildMI(XMMSaveMBB, DL, TII->get(MOVOpc))
Dan Gohmand6708ea2009-08-15 01:38:56 +000013454 .addFrameIndex(RegSaveFrameIndex)
13455 .addImm(/*Scale=*/1)
13456 .addReg(/*IndexReg=*/0)
13457 .addImm(/*Disp=*/Offset)
13458 .addReg(/*Segment=*/0)
13459 .addReg(MI->getOperand(i).getReg())
Dan Gohmanc76909a2009-09-25 20:36:54 +000013460 .addMemOperand(MMO);
Dan Gohmand6708ea2009-08-15 01:38:56 +000013461 }
13462
Dan Gohman14152b42010-07-06 20:24:04 +000013463 MI->eraseFromParent(); // The pseudo instruction is gone now.
Dan Gohmand6708ea2009-08-15 01:38:56 +000013464
13465 return EndMBB;
13466}
Mon P Wang63307c32008-05-05 19:05:59 +000013467
Lang Hames6e3f7e42012-02-03 01:13:49 +000013468// The EFLAGS operand of SelectItr might be missing a kill marker
13469// because there were multiple uses of EFLAGS, and ISel didn't know
13470// which to mark. Figure out whether SelectItr should have had a
13471// kill marker, and set it if it should. Returns the correct kill
13472// marker value.
13473static bool checkAndUpdateEFLAGSKill(MachineBasicBlock::iterator SelectItr,
13474 MachineBasicBlock* BB,
13475 const TargetRegisterInfo* TRI) {
13476 // Scan forward through BB for a use/def of EFLAGS.
13477 MachineBasicBlock::iterator miI(llvm::next(SelectItr));
13478 for (MachineBasicBlock::iterator miE = BB->end(); miI != miE; ++miI) {
Lang Hames50a36f72012-02-02 07:48:37 +000013479 const MachineInstr& mi = *miI;
Lang Hames6e3f7e42012-02-03 01:13:49 +000013480 if (mi.readsRegister(X86::EFLAGS))
Lang Hames50a36f72012-02-02 07:48:37 +000013481 return false;
Lang Hames6e3f7e42012-02-03 01:13:49 +000013482 if (mi.definesRegister(X86::EFLAGS))
13483 break; // Should have kill-flag - update below.
13484 }
13485
13486 // If we hit the end of the block, check whether EFLAGS is live into a
13487 // successor.
13488 if (miI == BB->end()) {
13489 for (MachineBasicBlock::succ_iterator sItr = BB->succ_begin(),
13490 sEnd = BB->succ_end();
13491 sItr != sEnd; ++sItr) {
13492 MachineBasicBlock* succ = *sItr;
13493 if (succ->isLiveIn(X86::EFLAGS))
13494 return false;
Lang Hames50a36f72012-02-02 07:48:37 +000013495 }
13496 }
13497
Lang Hames6e3f7e42012-02-03 01:13:49 +000013498 // We found a def, or hit the end of the basic block and EFLAGS wasn't live
13499 // out. SelectMI should have a kill flag on EFLAGS.
13500 SelectItr->addRegisterKilled(X86::EFLAGS, TRI);
Lang Hames50a36f72012-02-02 07:48:37 +000013501 return true;
13502}
13503
Evan Cheng60c07e12006-07-05 22:17:51 +000013504MachineBasicBlock *
Chris Lattner52600972009-09-02 05:57:00 +000013505X86TargetLowering::EmitLoweredSelect(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000013506 MachineBasicBlock *BB) const {
Chris Lattner52600972009-09-02 05:57:00 +000013507 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
13508 DebugLoc DL = MI->getDebugLoc();
Daniel Dunbara279bc32009-09-20 02:20:51 +000013509
Chris Lattner52600972009-09-02 05:57:00 +000013510 // To "insert" a SELECT_CC instruction, we actually have to insert the
13511 // diamond control-flow pattern. The incoming instruction knows the
13512 // destination vreg to set, the condition code register to branch on, the
13513 // true/false values to select between, and a branch opcode to use.
13514 const BasicBlock *LLVM_BB = BB->getBasicBlock();
13515 MachineFunction::iterator It = BB;
13516 ++It;
Daniel Dunbara279bc32009-09-20 02:20:51 +000013517
Chris Lattner52600972009-09-02 05:57:00 +000013518 // thisMBB:
13519 // ...
13520 // TrueVal = ...
13521 // cmpTY ccX, r1, r2
13522 // bCC copy1MBB
13523 // fallthrough --> copy0MBB
13524 MachineBasicBlock *thisMBB = BB;
13525 MachineFunction *F = BB->getParent();
13526 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
13527 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Chris Lattner52600972009-09-02 05:57:00 +000013528 F->insert(It, copy0MBB);
13529 F->insert(It, sinkMBB);
Bill Wendling730c07e2010-06-25 20:48:10 +000013530
Bill Wendling730c07e2010-06-25 20:48:10 +000013531 // If the EFLAGS register isn't dead in the terminator, then claim that it's
13532 // live into the sink and copy blocks.
Lang Hames6e3f7e42012-02-03 01:13:49 +000013533 const TargetRegisterInfo* TRI = getTargetMachine().getRegisterInfo();
13534 if (!MI->killsRegister(X86::EFLAGS) &&
13535 !checkAndUpdateEFLAGSKill(MI, BB, TRI)) {
13536 copy0MBB->addLiveIn(X86::EFLAGS);
13537 sinkMBB->addLiveIn(X86::EFLAGS);
Bill Wendling730c07e2010-06-25 20:48:10 +000013538 }
13539
Dan Gohman14152b42010-07-06 20:24:04 +000013540 // Transfer the remainder of BB and its successor edges to sinkMBB.
13541 sinkMBB->splice(sinkMBB->begin(), BB,
13542 llvm::next(MachineBasicBlock::iterator(MI)),
13543 BB->end());
13544 sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
13545
13546 // Add the true and fallthrough blocks as its successors.
13547 BB->addSuccessor(copy0MBB);
13548 BB->addSuccessor(sinkMBB);
13549
13550 // Create the conditional branch instruction.
13551 unsigned Opc =
13552 X86::GetCondBranchFromCond((X86::CondCode)MI->getOperand(3).getImm());
13553 BuildMI(BB, DL, TII->get(Opc)).addMBB(sinkMBB);
13554
Chris Lattner52600972009-09-02 05:57:00 +000013555 // copy0MBB:
13556 // %FalseValue = ...
13557 // # fallthrough to sinkMBB
Dan Gohman3335a222010-04-30 20:14:26 +000013558 copy0MBB->addSuccessor(sinkMBB);
Daniel Dunbara279bc32009-09-20 02:20:51 +000013559
Chris Lattner52600972009-09-02 05:57:00 +000013560 // sinkMBB:
13561 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
13562 // ...
Dan Gohman14152b42010-07-06 20:24:04 +000013563 BuildMI(*sinkMBB, sinkMBB->begin(), DL,
13564 TII->get(X86::PHI), MI->getOperand(0).getReg())
Chris Lattner52600972009-09-02 05:57:00 +000013565 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
13566 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
13567
Dan Gohman14152b42010-07-06 20:24:04 +000013568 MI->eraseFromParent(); // The pseudo instruction is gone now.
Dan Gohman3335a222010-04-30 20:14:26 +000013569 return sinkMBB;
Chris Lattner52600972009-09-02 05:57:00 +000013570}
13571
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000013572MachineBasicBlock *
Rafael Espindola151ab3e2011-08-30 19:47:04 +000013573X86TargetLowering::EmitLoweredSegAlloca(MachineInstr *MI, MachineBasicBlock *BB,
13574 bool Is64Bit) const {
13575 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
13576 DebugLoc DL = MI->getDebugLoc();
13577 MachineFunction *MF = BB->getParent();
13578 const BasicBlock *LLVM_BB = BB->getBasicBlock();
13579
Nick Lewycky8a8d4792011-12-02 22:16:29 +000013580 assert(getTargetMachine().Options.EnableSegmentedStacks);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000013581
13582 unsigned TlsReg = Is64Bit ? X86::FS : X86::GS;
13583 unsigned TlsOffset = Is64Bit ? 0x70 : 0x30;
13584
13585 // BB:
13586 // ... [Till the alloca]
13587 // If stacklet is not large enough, jump to mallocMBB
13588 //
13589 // bumpMBB:
13590 // Allocate by subtracting from RSP
13591 // Jump to continueMBB
13592 //
13593 // mallocMBB:
13594 // Allocate by call to runtime
13595 //
13596 // continueMBB:
13597 // ...
13598 // [rest of original BB]
13599 //
13600
13601 MachineBasicBlock *mallocMBB = MF->CreateMachineBasicBlock(LLVM_BB);
13602 MachineBasicBlock *bumpMBB = MF->CreateMachineBasicBlock(LLVM_BB);
13603 MachineBasicBlock *continueMBB = MF->CreateMachineBasicBlock(LLVM_BB);
13604
13605 MachineRegisterInfo &MRI = MF->getRegInfo();
13606 const TargetRegisterClass *AddrRegClass =
13607 getRegClassFor(Is64Bit ? MVT::i64:MVT::i32);
13608
13609 unsigned mallocPtrVReg = MRI.createVirtualRegister(AddrRegClass),
13610 bumpSPPtrVReg = MRI.createVirtualRegister(AddrRegClass),
13611 tmpSPVReg = MRI.createVirtualRegister(AddrRegClass),
Rafael Espindola66bf7432011-10-26 21:16:41 +000013612 SPLimitVReg = MRI.createVirtualRegister(AddrRegClass),
Rafael Espindola151ab3e2011-08-30 19:47:04 +000013613 sizeVReg = MI->getOperand(1).getReg(),
13614 physSPReg = Is64Bit ? X86::RSP : X86::ESP;
13615
13616 MachineFunction::iterator MBBIter = BB;
13617 ++MBBIter;
13618
13619 MF->insert(MBBIter, bumpMBB);
13620 MF->insert(MBBIter, mallocMBB);
13621 MF->insert(MBBIter, continueMBB);
13622
13623 continueMBB->splice(continueMBB->begin(), BB, llvm::next
13624 (MachineBasicBlock::iterator(MI)), BB->end());
13625 continueMBB->transferSuccessorsAndUpdatePHIs(BB);
13626
13627 // Add code to the main basic block to check if the stack limit has been hit,
13628 // and if so, jump to mallocMBB otherwise to bumpMBB.
13629 BuildMI(BB, DL, TII->get(TargetOpcode::COPY), tmpSPVReg).addReg(physSPReg);
Rafael Espindola66bf7432011-10-26 21:16:41 +000013630 BuildMI(BB, DL, TII->get(Is64Bit ? X86::SUB64rr:X86::SUB32rr), SPLimitVReg)
Rafael Espindola151ab3e2011-08-30 19:47:04 +000013631 .addReg(tmpSPVReg).addReg(sizeVReg);
13632 BuildMI(BB, DL, TII->get(Is64Bit ? X86::CMP64mr:X86::CMP32mr))
Rafael Espindola014f7a32012-01-11 18:14:03 +000013633 .addReg(0).addImm(1).addReg(0).addImm(TlsOffset).addReg(TlsReg)
Rafael Espindola66bf7432011-10-26 21:16:41 +000013634 .addReg(SPLimitVReg);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000013635 BuildMI(BB, DL, TII->get(X86::JG_4)).addMBB(mallocMBB);
13636
13637 // bumpMBB simply decreases the stack pointer, since we know the current
13638 // stacklet has enough space.
13639 BuildMI(bumpMBB, DL, TII->get(TargetOpcode::COPY), physSPReg)
Rafael Espindola66bf7432011-10-26 21:16:41 +000013640 .addReg(SPLimitVReg);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000013641 BuildMI(bumpMBB, DL, TII->get(TargetOpcode::COPY), bumpSPPtrVReg)
Rafael Espindola66bf7432011-10-26 21:16:41 +000013642 .addReg(SPLimitVReg);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000013643 BuildMI(bumpMBB, DL, TII->get(X86::JMP_4)).addMBB(continueMBB);
13644
13645 // Calls into a routine in libgcc to allocate more space from the heap.
Jakob Stoklund Olesen8bcde2a2012-02-16 00:02:50 +000013646 const uint32_t *RegMask =
13647 getTargetMachine().getRegisterInfo()->getCallPreservedMask(CallingConv::C);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000013648 if (Is64Bit) {
13649 BuildMI(mallocMBB, DL, TII->get(X86::MOV64rr), X86::RDI)
13650 .addReg(sizeVReg);
13651 BuildMI(mallocMBB, DL, TII->get(X86::CALL64pcrel32))
Jakob Stoklund Olesen85dccf12012-07-04 23:53:27 +000013652 .addExternalSymbol("__morestack_allocate_stack_space")
Jakob Stoklund Olesen8bcde2a2012-02-16 00:02:50 +000013653 .addRegMask(RegMask)
Jakob Stoklund Olesen85dccf12012-07-04 23:53:27 +000013654 .addReg(X86::RDI, RegState::Implicit)
Jakob Stoklund Olesen8bcde2a2012-02-16 00:02:50 +000013655 .addReg(X86::RAX, RegState::ImplicitDefine);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000013656 } else {
13657 BuildMI(mallocMBB, DL, TII->get(X86::SUB32ri), physSPReg).addReg(physSPReg)
13658 .addImm(12);
13659 BuildMI(mallocMBB, DL, TII->get(X86::PUSH32r)).addReg(sizeVReg);
13660 BuildMI(mallocMBB, DL, TII->get(X86::CALLpcrel32))
Jakob Stoklund Olesen8bcde2a2012-02-16 00:02:50 +000013661 .addExternalSymbol("__morestack_allocate_stack_space")
13662 .addRegMask(RegMask)
13663 .addReg(X86::EAX, RegState::ImplicitDefine);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000013664 }
13665
13666 if (!Is64Bit)
13667 BuildMI(mallocMBB, DL, TII->get(X86::ADD32ri), physSPReg).addReg(physSPReg)
13668 .addImm(16);
13669
13670 BuildMI(mallocMBB, DL, TII->get(TargetOpcode::COPY), mallocPtrVReg)
13671 .addReg(Is64Bit ? X86::RAX : X86::EAX);
13672 BuildMI(mallocMBB, DL, TII->get(X86::JMP_4)).addMBB(continueMBB);
13673
13674 // Set up the CFG correctly.
13675 BB->addSuccessor(bumpMBB);
13676 BB->addSuccessor(mallocMBB);
13677 mallocMBB->addSuccessor(continueMBB);
13678 bumpMBB->addSuccessor(continueMBB);
13679
13680 // Take care of the PHI nodes.
13681 BuildMI(*continueMBB, continueMBB->begin(), DL, TII->get(X86::PHI),
13682 MI->getOperand(0).getReg())
13683 .addReg(mallocPtrVReg).addMBB(mallocMBB)
13684 .addReg(bumpSPPtrVReg).addMBB(bumpMBB);
13685
13686 // Delete the original pseudo instruction.
13687 MI->eraseFromParent();
13688
13689 // And we're done.
13690 return continueMBB;
13691}
13692
13693MachineBasicBlock *
Michael J. Spencere9c253e2010-10-21 01:41:01 +000013694X86TargetLowering::EmitLoweredWinAlloca(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000013695 MachineBasicBlock *BB) const {
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000013696 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
13697 DebugLoc DL = MI->getDebugLoc();
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000013698
NAKAMURA Takumia2e07622011-03-24 07:07:00 +000013699 assert(!Subtarget->isTargetEnvMacho());
13700
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000013701 // The lowering is pretty easy: we're just emitting the call to _alloca. The
13702 // non-trivial part is impdef of ESP.
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000013703
NAKAMURA Takumia2e07622011-03-24 07:07:00 +000013704 if (Subtarget->isTargetWin64()) {
13705 if (Subtarget->isTargetCygMing()) {
13706 // ___chkstk(Mingw64):
13707 // Clobbers R10, R11, RAX and EFLAGS.
13708 // Updates RSP.
13709 BuildMI(*BB, MI, DL, TII->get(X86::W64ALLOCA))
13710 .addExternalSymbol("___chkstk")
13711 .addReg(X86::RAX, RegState::Implicit)
13712 .addReg(X86::RSP, RegState::Implicit)
13713 .addReg(X86::RAX, RegState::Define | RegState::Implicit)
13714 .addReg(X86::RSP, RegState::Define | RegState::Implicit)
13715 .addReg(X86::EFLAGS, RegState::Define | RegState::Implicit);
13716 } else {
13717 // __chkstk(MSVCRT): does not update stack pointer.
13718 // Clobbers R10, R11 and EFLAGS.
13719 // FIXME: RAX(allocated size) might be reused and not killed.
13720 BuildMI(*BB, MI, DL, TII->get(X86::W64ALLOCA))
13721 .addExternalSymbol("__chkstk")
13722 .addReg(X86::RAX, RegState::Implicit)
13723 .addReg(X86::EFLAGS, RegState::Define | RegState::Implicit);
13724 // RAX has the offset to subtracted from RSP.
13725 BuildMI(*BB, MI, DL, TII->get(X86::SUB64rr), X86::RSP)
13726 .addReg(X86::RSP)
13727 .addReg(X86::RAX);
13728 }
13729 } else {
13730 const char *StackProbeSymbol =
Michael J. Spencere9c253e2010-10-21 01:41:01 +000013731 Subtarget->isTargetWindows() ? "_chkstk" : "_alloca";
13732
NAKAMURA Takumia2e07622011-03-24 07:07:00 +000013733 BuildMI(*BB, MI, DL, TII->get(X86::CALLpcrel32))
13734 .addExternalSymbol(StackProbeSymbol)
13735 .addReg(X86::EAX, RegState::Implicit)
13736 .addReg(X86::ESP, RegState::Implicit)
13737 .addReg(X86::EAX, RegState::Define | RegState::Implicit)
13738 .addReg(X86::ESP, RegState::Define | RegState::Implicit)
13739 .addReg(X86::EFLAGS, RegState::Define | RegState::Implicit);
13740 }
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000013741
Dan Gohman14152b42010-07-06 20:24:04 +000013742 MI->eraseFromParent(); // The pseudo instruction is gone now.
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000013743 return BB;
13744}
Chris Lattner52600972009-09-02 05:57:00 +000013745
13746MachineBasicBlock *
Eric Christopher30ef0e52010-06-03 04:07:48 +000013747X86TargetLowering::EmitLoweredTLSCall(MachineInstr *MI,
13748 MachineBasicBlock *BB) const {
13749 // This is pretty easy. We're taking the value that we received from
13750 // our load from the relocation, sticking it in either RDI (x86-64)
13751 // or EAX and doing an indirect call. The return value will then
13752 // be in the normal return register.
Michael J. Spencerec38de22010-10-10 22:04:20 +000013753 const X86InstrInfo *TII
Eric Christopher54415362010-06-08 22:04:25 +000013754 = static_cast<const X86InstrInfo*>(getTargetMachine().getInstrInfo());
Eric Christopher30ef0e52010-06-03 04:07:48 +000013755 DebugLoc DL = MI->getDebugLoc();
13756 MachineFunction *F = BB->getParent();
Eric Christopher722d3152010-09-27 06:01:51 +000013757
13758 assert(Subtarget->isTargetDarwin() && "Darwin only instr emitted?");
Eric Christopher54415362010-06-08 22:04:25 +000013759 assert(MI->getOperand(3).isGlobal() && "This should be a global");
Michael J. Spencerec38de22010-10-10 22:04:20 +000013760
Jakob Stoklund Olesen8bcde2a2012-02-16 00:02:50 +000013761 // Get a register mask for the lowered call.
13762 // FIXME: The 32-bit calls have non-standard calling conventions. Use a
13763 // proper register mask.
13764 const uint32_t *RegMask =
13765 getTargetMachine().getRegisterInfo()->getCallPreservedMask(CallingConv::C);
Eric Christopher30ef0e52010-06-03 04:07:48 +000013766 if (Subtarget->is64Bit()) {
Dan Gohman14152b42010-07-06 20:24:04 +000013767 MachineInstrBuilder MIB = BuildMI(*BB, MI, DL,
13768 TII->get(X86::MOV64rm), X86::RDI)
Eric Christopher54415362010-06-08 22:04:25 +000013769 .addReg(X86::RIP)
13770 .addImm(0).addReg(0)
Michael J. Spencerec38de22010-10-10 22:04:20 +000013771 .addGlobalAddress(MI->getOperand(3).getGlobal(), 0,
Eric Christopher54415362010-06-08 22:04:25 +000013772 MI->getOperand(3).getTargetFlags())
13773 .addReg(0);
Eric Christopher722d3152010-09-27 06:01:51 +000013774 MIB = BuildMI(*BB, MI, DL, TII->get(X86::CALL64m));
Chris Lattner599b5312010-07-08 23:46:44 +000013775 addDirectMem(MIB, X86::RDI);
Jakob Stoklund Olesen8bcde2a2012-02-16 00:02:50 +000013776 MIB.addReg(X86::RAX, RegState::ImplicitDefine).addRegMask(RegMask);
Eric Christopher61025492010-06-15 23:08:42 +000013777 } else if (getTargetMachine().getRelocationModel() != Reloc::PIC_) {
Dan Gohman14152b42010-07-06 20:24:04 +000013778 MachineInstrBuilder MIB = BuildMI(*BB, MI, DL,
13779 TII->get(X86::MOV32rm), X86::EAX)
Eric Christopher61025492010-06-15 23:08:42 +000013780 .addReg(0)
13781 .addImm(0).addReg(0)
Michael J. Spencerec38de22010-10-10 22:04:20 +000013782 .addGlobalAddress(MI->getOperand(3).getGlobal(), 0,
Eric Christopher61025492010-06-15 23:08:42 +000013783 MI->getOperand(3).getTargetFlags())
13784 .addReg(0);
Dan Gohman14152b42010-07-06 20:24:04 +000013785 MIB = BuildMI(*BB, MI, DL, TII->get(X86::CALL32m));
Chris Lattner599b5312010-07-08 23:46:44 +000013786 addDirectMem(MIB, X86::EAX);
Jakob Stoklund Olesen8bcde2a2012-02-16 00:02:50 +000013787 MIB.addReg(X86::EAX, RegState::ImplicitDefine).addRegMask(RegMask);
Eric Christopher30ef0e52010-06-03 04:07:48 +000013788 } else {
Dan Gohman14152b42010-07-06 20:24:04 +000013789 MachineInstrBuilder MIB = BuildMI(*BB, MI, DL,
13790 TII->get(X86::MOV32rm), X86::EAX)
Eric Christopher54415362010-06-08 22:04:25 +000013791 .addReg(TII->getGlobalBaseReg(F))
13792 .addImm(0).addReg(0)
Michael J. Spencerec38de22010-10-10 22:04:20 +000013793 .addGlobalAddress(MI->getOperand(3).getGlobal(), 0,
Eric Christopher54415362010-06-08 22:04:25 +000013794 MI->getOperand(3).getTargetFlags())
13795 .addReg(0);
Dan Gohman14152b42010-07-06 20:24:04 +000013796 MIB = BuildMI(*BB, MI, DL, TII->get(X86::CALL32m));
Chris Lattner599b5312010-07-08 23:46:44 +000013797 addDirectMem(MIB, X86::EAX);
Jakob Stoklund Olesen8bcde2a2012-02-16 00:02:50 +000013798 MIB.addReg(X86::EAX, RegState::ImplicitDefine).addRegMask(RegMask);
Eric Christopher30ef0e52010-06-03 04:07:48 +000013799 }
Michael J. Spencerec38de22010-10-10 22:04:20 +000013800
Dan Gohman14152b42010-07-06 20:24:04 +000013801 MI->eraseFromParent(); // The pseudo instruction is gone now.
Eric Christopher30ef0e52010-06-03 04:07:48 +000013802 return BB;
13803}
13804
13805MachineBasicBlock *
Michael Liao6c0e04c2012-10-15 22:39:43 +000013806X86TargetLowering::emitEHSjLjSetJmp(MachineInstr *MI,
13807 MachineBasicBlock *MBB) const {
13808 DebugLoc DL = MI->getDebugLoc();
13809 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
13810
13811 MachineFunction *MF = MBB->getParent();
13812 MachineRegisterInfo &MRI = MF->getRegInfo();
13813
13814 const BasicBlock *BB = MBB->getBasicBlock();
13815 MachineFunction::iterator I = MBB;
13816 ++I;
13817
13818 // Memory Reference
13819 MachineInstr::mmo_iterator MMOBegin = MI->memoperands_begin();
13820 MachineInstr::mmo_iterator MMOEnd = MI->memoperands_end();
13821
13822 unsigned DstReg;
13823 unsigned MemOpndSlot = 0;
13824
13825 unsigned CurOp = 0;
13826
13827 DstReg = MI->getOperand(CurOp++).getReg();
13828 const TargetRegisterClass *RC = MRI.getRegClass(DstReg);
13829 assert(RC->hasType(MVT::i32) && "Invalid destination!");
13830 unsigned mainDstReg = MRI.createVirtualRegister(RC);
13831 unsigned restoreDstReg = MRI.createVirtualRegister(RC);
13832
13833 MemOpndSlot = CurOp;
13834
13835 MVT PVT = getPointerTy();
13836 assert((PVT == MVT::i64 || PVT == MVT::i32) &&
13837 "Invalid Pointer Size!");
13838
13839 // For v = setjmp(buf), we generate
13840 //
13841 // thisMBB:
Michael Liao281ae5a2012-10-17 02:22:27 +000013842 // buf[LabelOffset] = restoreMBB
Michael Liao6c0e04c2012-10-15 22:39:43 +000013843 // SjLjSetup restoreMBB
13844 //
13845 // mainMBB:
13846 // v_main = 0
13847 //
13848 // sinkMBB:
13849 // v = phi(main, restore)
13850 //
13851 // restoreMBB:
13852 // v_restore = 1
13853
13854 MachineBasicBlock *thisMBB = MBB;
13855 MachineBasicBlock *mainMBB = MF->CreateMachineBasicBlock(BB);
13856 MachineBasicBlock *sinkMBB = MF->CreateMachineBasicBlock(BB);
13857 MachineBasicBlock *restoreMBB = MF->CreateMachineBasicBlock(BB);
13858 MF->insert(I, mainMBB);
13859 MF->insert(I, sinkMBB);
13860 MF->push_back(restoreMBB);
13861
13862 MachineInstrBuilder MIB;
13863
13864 // Transfer the remainder of BB and its successor edges to sinkMBB.
13865 sinkMBB->splice(sinkMBB->begin(), MBB,
13866 llvm::next(MachineBasicBlock::iterator(MI)), MBB->end());
13867 sinkMBB->transferSuccessorsAndUpdatePHIs(MBB);
13868
13869 // thisMBB:
Michael Liao281ae5a2012-10-17 02:22:27 +000013870 unsigned PtrStoreOpc = 0;
13871 unsigned LabelReg = 0;
13872 const int64_t LabelOffset = 1 * PVT.getStoreSize();
13873 Reloc::Model RM = getTargetMachine().getRelocationModel();
13874 bool UseImmLabel = (getTargetMachine().getCodeModel() == CodeModel::Small) &&
13875 (RM == Reloc::Static || RM == Reloc::DynamicNoPIC);
Michael Liao6c0e04c2012-10-15 22:39:43 +000013876
Michael Liao281ae5a2012-10-17 02:22:27 +000013877 // Prepare IP either in reg or imm.
13878 if (!UseImmLabel) {
13879 PtrStoreOpc = (PVT == MVT::i64) ? X86::MOV64mr : X86::MOV32mr;
13880 const TargetRegisterClass *PtrRC = getRegClassFor(PVT);
13881 LabelReg = MRI.createVirtualRegister(PtrRC);
13882 if (Subtarget->is64Bit()) {
13883 MIB = BuildMI(*thisMBB, MI, DL, TII->get(X86::LEA64r), LabelReg)
13884 .addReg(X86::RIP)
13885 .addImm(0)
13886 .addReg(0)
13887 .addMBB(restoreMBB)
13888 .addReg(0);
13889 } else {
13890 const X86InstrInfo *XII = static_cast<const X86InstrInfo*>(TII);
13891 MIB = BuildMI(*thisMBB, MI, DL, TII->get(X86::LEA32r), LabelReg)
13892 .addReg(XII->getGlobalBaseReg(MF))
13893 .addImm(0)
13894 .addReg(0)
13895 .addMBB(restoreMBB, Subtarget->ClassifyBlockAddressReference())
13896 .addReg(0);
13897 }
13898 } else
13899 PtrStoreOpc = (PVT == MVT::i64) ? X86::MOV64mi32 : X86::MOV32mi;
Michael Liao6c0e04c2012-10-15 22:39:43 +000013900 // Store IP
Michael Liao281ae5a2012-10-17 02:22:27 +000013901 MIB = BuildMI(*thisMBB, MI, DL, TII->get(PtrStoreOpc));
Michael Liao6c0e04c2012-10-15 22:39:43 +000013902 for (unsigned i = 0; i < X86::AddrNumOperands; ++i) {
13903 if (i == X86::AddrDisp)
Michael Liao281ae5a2012-10-17 02:22:27 +000013904 MIB.addDisp(MI->getOperand(MemOpndSlot + i), LabelOffset);
Michael Liao6c0e04c2012-10-15 22:39:43 +000013905 else
13906 MIB.addOperand(MI->getOperand(MemOpndSlot + i));
13907 }
Michael Liao281ae5a2012-10-17 02:22:27 +000013908 if (!UseImmLabel)
13909 MIB.addReg(LabelReg);
13910 else
13911 MIB.addMBB(restoreMBB);
Michael Liao6c0e04c2012-10-15 22:39:43 +000013912 MIB.setMemRefs(MMOBegin, MMOEnd);
13913 // Setup
13914 MIB = BuildMI(*thisMBB, MI, DL, TII->get(X86::EH_SjLj_Setup))
13915 .addMBB(restoreMBB);
13916 MIB.addRegMask(RegInfo->getNoPreservedMask());
13917 thisMBB->addSuccessor(mainMBB);
13918 thisMBB->addSuccessor(restoreMBB);
13919
13920 // mainMBB:
13921 // EAX = 0
13922 BuildMI(mainMBB, DL, TII->get(X86::MOV32r0), mainDstReg);
13923 mainMBB->addSuccessor(sinkMBB);
13924
13925 // sinkMBB:
13926 BuildMI(*sinkMBB, sinkMBB->begin(), DL,
13927 TII->get(X86::PHI), DstReg)
13928 .addReg(mainDstReg).addMBB(mainMBB)
13929 .addReg(restoreDstReg).addMBB(restoreMBB);
13930
13931 // restoreMBB:
13932 BuildMI(restoreMBB, DL, TII->get(X86::MOV32ri), restoreDstReg).addImm(1);
13933 BuildMI(restoreMBB, DL, TII->get(X86::JMP_4)).addMBB(sinkMBB);
13934 restoreMBB->addSuccessor(sinkMBB);
13935
13936 MI->eraseFromParent();
13937 return sinkMBB;
13938}
13939
13940MachineBasicBlock *
13941X86TargetLowering::emitEHSjLjLongJmp(MachineInstr *MI,
13942 MachineBasicBlock *MBB) const {
13943 DebugLoc DL = MI->getDebugLoc();
13944 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
13945
13946 MachineFunction *MF = MBB->getParent();
13947 MachineRegisterInfo &MRI = MF->getRegInfo();
13948
13949 // Memory Reference
13950 MachineInstr::mmo_iterator MMOBegin = MI->memoperands_begin();
13951 MachineInstr::mmo_iterator MMOEnd = MI->memoperands_end();
13952
13953 MVT PVT = getPointerTy();
13954 assert((PVT == MVT::i64 || PVT == MVT::i32) &&
13955 "Invalid Pointer Size!");
13956
13957 const TargetRegisterClass *RC =
13958 (PVT == MVT::i64) ? &X86::GR64RegClass : &X86::GR32RegClass;
13959 unsigned Tmp = MRI.createVirtualRegister(RC);
13960 // Since FP is only updated here but NOT referenced, it's treated as GPR.
13961 unsigned FP = (PVT == MVT::i64) ? X86::RBP : X86::EBP;
13962 unsigned SP = RegInfo->getStackRegister();
13963
13964 MachineInstrBuilder MIB;
13965
Michael Liao281ae5a2012-10-17 02:22:27 +000013966 const int64_t LabelOffset = 1 * PVT.getStoreSize();
13967 const int64_t SPOffset = 2 * PVT.getStoreSize();
Michael Liao6c0e04c2012-10-15 22:39:43 +000013968
13969 unsigned PtrLoadOpc = (PVT == MVT::i64) ? X86::MOV64rm : X86::MOV32rm;
13970 unsigned IJmpOpc = (PVT == MVT::i64) ? X86::JMP64r : X86::JMP32r;
13971
13972 // Reload FP
13973 MIB = BuildMI(*MBB, MI, DL, TII->get(PtrLoadOpc), FP);
13974 for (unsigned i = 0; i < X86::AddrNumOperands; ++i)
13975 MIB.addOperand(MI->getOperand(i));
13976 MIB.setMemRefs(MMOBegin, MMOEnd);
13977 // Reload IP
13978 MIB = BuildMI(*MBB, MI, DL, TII->get(PtrLoadOpc), Tmp);
13979 for (unsigned i = 0; i < X86::AddrNumOperands; ++i) {
13980 if (i == X86::AddrDisp)
Michael Liao281ae5a2012-10-17 02:22:27 +000013981 MIB.addDisp(MI->getOperand(i), LabelOffset);
Michael Liao6c0e04c2012-10-15 22:39:43 +000013982 else
13983 MIB.addOperand(MI->getOperand(i));
13984 }
13985 MIB.setMemRefs(MMOBegin, MMOEnd);
13986 // Reload SP
13987 MIB = BuildMI(*MBB, MI, DL, TII->get(PtrLoadOpc), SP);
13988 for (unsigned i = 0; i < X86::AddrNumOperands; ++i) {
13989 if (i == X86::AddrDisp)
Michael Liao281ae5a2012-10-17 02:22:27 +000013990 MIB.addDisp(MI->getOperand(i), SPOffset);
Michael Liao6c0e04c2012-10-15 22:39:43 +000013991 else
13992 MIB.addOperand(MI->getOperand(i));
13993 }
13994 MIB.setMemRefs(MMOBegin, MMOEnd);
13995 // Jump
13996 BuildMI(*MBB, MI, DL, TII->get(IJmpOpc)).addReg(Tmp);
13997
13998 MI->eraseFromParent();
13999 return MBB;
14000}
14001
14002MachineBasicBlock *
Evan Chengff9b3732008-01-30 18:18:23 +000014003X86TargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000014004 MachineBasicBlock *BB) const {
Evan Cheng60c07e12006-07-05 22:17:51 +000014005 switch (MI->getOpcode()) {
Craig Topperabb94d02012-02-05 03:43:23 +000014006 default: llvm_unreachable("Unexpected instr type to insert");
NAKAMURA Takumi7754f852011-01-26 02:04:09 +000014007 case X86::TAILJMPd64:
14008 case X86::TAILJMPr64:
14009 case X86::TAILJMPm64:
Craig Topper6d1263a2012-02-05 05:38:58 +000014010 llvm_unreachable("TAILJMP64 would not be touched here.");
NAKAMURA Takumi7754f852011-01-26 02:04:09 +000014011 case X86::TCRETURNdi64:
14012 case X86::TCRETURNri64:
14013 case X86::TCRETURNmi64:
NAKAMURA Takumi7754f852011-01-26 02:04:09 +000014014 return BB;
Michael J. Spencere9c253e2010-10-21 01:41:01 +000014015 case X86::WIN_ALLOCA:
14016 return EmitLoweredWinAlloca(MI, BB);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000014017 case X86::SEG_ALLOCA_32:
14018 return EmitLoweredSegAlloca(MI, BB, false);
14019 case X86::SEG_ALLOCA_64:
14020 return EmitLoweredSegAlloca(MI, BB, true);
Eric Christopher30ef0e52010-06-03 04:07:48 +000014021 case X86::TLSCall_32:
14022 case X86::TLSCall_64:
14023 return EmitLoweredTLSCall(MI, BB);
Dan Gohmancbbea0f2009-08-27 00:14:12 +000014024 case X86::CMOV_GR8:
Evan Cheng60c07e12006-07-05 22:17:51 +000014025 case X86::CMOV_FR32:
14026 case X86::CMOV_FR64:
14027 case X86::CMOV_V4F32:
14028 case X86::CMOV_V2F64:
Chris Lattner52600972009-09-02 05:57:00 +000014029 case X86::CMOV_V2I64:
Bruno Cardoso Lopesd40aa242011-08-09 23:27:13 +000014030 case X86::CMOV_V8F32:
14031 case X86::CMOV_V4F64:
14032 case X86::CMOV_V4I64:
Chris Lattner314a1132010-03-14 18:31:44 +000014033 case X86::CMOV_GR16:
14034 case X86::CMOV_GR32:
14035 case X86::CMOV_RFP32:
14036 case X86::CMOV_RFP64:
14037 case X86::CMOV_RFP80:
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000014038 return EmitLoweredSelect(MI, BB);
Evan Cheng60c07e12006-07-05 22:17:51 +000014039
Dale Johannesen849f2142007-07-03 00:53:03 +000014040 case X86::FP32_TO_INT16_IN_MEM:
14041 case X86::FP32_TO_INT32_IN_MEM:
14042 case X86::FP32_TO_INT64_IN_MEM:
14043 case X86::FP64_TO_INT16_IN_MEM:
14044 case X86::FP64_TO_INT32_IN_MEM:
Dale Johannesena996d522007-08-07 01:17:37 +000014045 case X86::FP64_TO_INT64_IN_MEM:
14046 case X86::FP80_TO_INT16_IN_MEM:
14047 case X86::FP80_TO_INT32_IN_MEM:
14048 case X86::FP80_TO_INT64_IN_MEM: {
Chris Lattner52600972009-09-02 05:57:00 +000014049 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
14050 DebugLoc DL = MI->getDebugLoc();
14051
Evan Cheng60c07e12006-07-05 22:17:51 +000014052 // Change the floating point control register to use "round towards zero"
14053 // mode when truncating to an integer value.
14054 MachineFunction *F = BB->getParent();
David Greene3f2bf852009-11-12 20:49:22 +000014055 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2, false);
Dan Gohman14152b42010-07-06 20:24:04 +000014056 addFrameReference(BuildMI(*BB, MI, DL,
14057 TII->get(X86::FNSTCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000014058
14059 // Load the old value of the high byte of the control word...
14060 unsigned OldCW =
Craig Topperc9099502012-04-20 06:31:50 +000014061 F->getRegInfo().createVirtualRegister(&X86::GR16RegClass);
Dan Gohman14152b42010-07-06 20:24:04 +000014062 addFrameReference(BuildMI(*BB, MI, DL, TII->get(X86::MOV16rm), OldCW),
Dale Johannesene4d209d2009-02-03 20:21:25 +000014063 CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000014064
14065 // Set the high part to be round to zero...
Dan Gohman14152b42010-07-06 20:24:04 +000014066 addFrameReference(BuildMI(*BB, MI, DL, TII->get(X86::MOV16mi)), CWFrameIdx)
Evan Chengc0f64ff2006-11-27 23:37:22 +000014067 .addImm(0xC7F);
Evan Cheng60c07e12006-07-05 22:17:51 +000014068
14069 // Reload the modified control word now...
Dan Gohman14152b42010-07-06 20:24:04 +000014070 addFrameReference(BuildMI(*BB, MI, DL,
14071 TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000014072
14073 // Restore the memory image of control word to original value
Dan Gohman14152b42010-07-06 20:24:04 +000014074 addFrameReference(BuildMI(*BB, MI, DL, TII->get(X86::MOV16mr)), CWFrameIdx)
Evan Chengc0f64ff2006-11-27 23:37:22 +000014075 .addReg(OldCW);
Evan Cheng60c07e12006-07-05 22:17:51 +000014076
14077 // Get the X86 opcode to use.
14078 unsigned Opc;
14079 switch (MI->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +000014080 default: llvm_unreachable("illegal opcode!");
Dale Johannesene377d4d2007-07-04 21:07:47 +000014081 case X86::FP32_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m32; break;
14082 case X86::FP32_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m32; break;
14083 case X86::FP32_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m32; break;
14084 case X86::FP64_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m64; break;
14085 case X86::FP64_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m64; break;
14086 case X86::FP64_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m64; break;
Dale Johannesena996d522007-08-07 01:17:37 +000014087 case X86::FP80_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m80; break;
14088 case X86::FP80_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m80; break;
14089 case X86::FP80_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m80; break;
Evan Cheng60c07e12006-07-05 22:17:51 +000014090 }
14091
14092 X86AddressMode AM;
14093 MachineOperand &Op = MI->getOperand(0);
Dan Gohmand735b802008-10-03 15:45:36 +000014094 if (Op.isReg()) {
Evan Cheng60c07e12006-07-05 22:17:51 +000014095 AM.BaseType = X86AddressMode::RegBase;
14096 AM.Base.Reg = Op.getReg();
14097 } else {
14098 AM.BaseType = X86AddressMode::FrameIndexBase;
Chris Lattner8aa797a2007-12-30 23:10:15 +000014099 AM.Base.FrameIndex = Op.getIndex();
Evan Cheng60c07e12006-07-05 22:17:51 +000014100 }
14101 Op = MI->getOperand(1);
Dan Gohmand735b802008-10-03 15:45:36 +000014102 if (Op.isImm())
Chris Lattner7fbe9722006-10-20 17:42:20 +000014103 AM.Scale = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +000014104 Op = MI->getOperand(2);
Dan Gohmand735b802008-10-03 15:45:36 +000014105 if (Op.isImm())
Chris Lattner7fbe9722006-10-20 17:42:20 +000014106 AM.IndexReg = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +000014107 Op = MI->getOperand(3);
Dan Gohmand735b802008-10-03 15:45:36 +000014108 if (Op.isGlobal()) {
Evan Cheng60c07e12006-07-05 22:17:51 +000014109 AM.GV = Op.getGlobal();
14110 } else {
Chris Lattner7fbe9722006-10-20 17:42:20 +000014111 AM.Disp = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +000014112 }
Dan Gohman14152b42010-07-06 20:24:04 +000014113 addFullAddress(BuildMI(*BB, MI, DL, TII->get(Opc)), AM)
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000014114 .addReg(MI->getOperand(X86::AddrNumOperands).getReg());
Evan Cheng60c07e12006-07-05 22:17:51 +000014115
14116 // Reload the original control word now.
Dan Gohman14152b42010-07-06 20:24:04 +000014117 addFrameReference(BuildMI(*BB, MI, DL,
14118 TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000014119
Dan Gohman14152b42010-07-06 20:24:04 +000014120 MI->eraseFromParent(); // The pseudo instruction is gone now.
Evan Cheng60c07e12006-07-05 22:17:51 +000014121 return BB;
14122 }
Eric Christopherb120ab42009-08-18 22:50:32 +000014123 // String/text processing lowering.
14124 case X86::PCMPISTRM128REG:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000014125 case X86::VPCMPISTRM128REG:
Eric Christopherb120ab42009-08-18 22:50:32 +000014126 case X86::PCMPISTRM128MEM:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000014127 case X86::VPCMPISTRM128MEM:
Eric Christopherb120ab42009-08-18 22:50:32 +000014128 case X86::PCMPESTRM128REG:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000014129 case X86::VPCMPESTRM128REG:
Eric Christopherb120ab42009-08-18 22:50:32 +000014130 case X86::PCMPESTRM128MEM:
Craig Topper8aae8dd2012-11-10 08:57:41 +000014131 case X86::VPCMPESTRM128MEM:
14132 assert(Subtarget->hasSSE42() &&
14133 "Target must have SSE4.2 or AVX features enabled");
14134 return EmitPCMPSTRM(MI, BB, getTargetMachine().getInstrInfo());
Craig Topper9c7ae012012-11-10 01:23:36 +000014135
14136 // String/text processing lowering.
14137 case X86::PCMPISTRIREG:
14138 case X86::VPCMPISTRIREG:
14139 case X86::PCMPISTRIMEM:
14140 case X86::VPCMPISTRIMEM:
14141 case X86::PCMPESTRIREG:
14142 case X86::VPCMPESTRIREG:
14143 case X86::PCMPESTRIMEM:
Craig Topper8aae8dd2012-11-10 08:57:41 +000014144 case X86::VPCMPESTRIMEM:
14145 assert(Subtarget->hasSSE42() &&
14146 "Target must have SSE4.2 or AVX features enabled");
14147 return EmitPCMPSTRI(MI, BB, getTargetMachine().getInstrInfo());
Eric Christopherb120ab42009-08-18 22:50:32 +000014148
Craig Topper8aae8dd2012-11-10 08:57:41 +000014149 // Thread synchronization.
Eric Christopher228232b2010-11-30 07:20:12 +000014150 case X86::MONITOR:
Craig Topper2da36912012-11-11 22:45:02 +000014151 return EmitMonitor(MI, BB, getTargetMachine().getInstrInfo(), Subtarget);
Eric Christopher228232b2010-11-30 07:20:12 +000014152
Michael Liaobe02a902012-11-08 07:28:54 +000014153 // xbegin
14154 case X86::XBEGIN:
Craig Topper2da36912012-11-11 22:45:02 +000014155 return EmitXBegin(MI, BB, getTargetMachine().getInstrInfo());
Michael Liaobe02a902012-11-08 07:28:54 +000014156
Craig Topper8aae8dd2012-11-10 08:57:41 +000014157 // Atomic Lowering.
Dale Johannesen140be2d2008-08-19 18:47:28 +000014158 case X86::ATOMAND8:
Michael Liaob118a072012-09-20 03:06:15 +000014159 case X86::ATOMAND16:
14160 case X86::ATOMAND32:
Dale Johannesena99e3842008-08-20 00:48:50 +000014161 case X86::ATOMAND64:
Michael Liaob118a072012-09-20 03:06:15 +000014162 // Fall through
14163 case X86::ATOMOR8:
14164 case X86::ATOMOR16:
14165 case X86::ATOMOR32:
Dale Johannesena99e3842008-08-20 00:48:50 +000014166 case X86::ATOMOR64:
Michael Liaob118a072012-09-20 03:06:15 +000014167 // Fall through
14168 case X86::ATOMXOR16:
14169 case X86::ATOMXOR8:
14170 case X86::ATOMXOR32:
Dale Johannesena99e3842008-08-20 00:48:50 +000014171 case X86::ATOMXOR64:
Michael Liaob118a072012-09-20 03:06:15 +000014172 // Fall through
14173 case X86::ATOMNAND8:
14174 case X86::ATOMNAND16:
14175 case X86::ATOMNAND32:
14176 case X86::ATOMNAND64:
14177 // Fall through
Michael Liaofe87c302012-09-21 03:18:52 +000014178 case X86::ATOMMAX8:
Michael Liaob118a072012-09-20 03:06:15 +000014179 case X86::ATOMMAX16:
14180 case X86::ATOMMAX32:
14181 case X86::ATOMMAX64:
14182 // Fall through
Michael Liaofe87c302012-09-21 03:18:52 +000014183 case X86::ATOMMIN8:
Michael Liaob118a072012-09-20 03:06:15 +000014184 case X86::ATOMMIN16:
14185 case X86::ATOMMIN32:
14186 case X86::ATOMMIN64:
14187 // Fall through
Michael Liaofe87c302012-09-21 03:18:52 +000014188 case X86::ATOMUMAX8:
Michael Liaob118a072012-09-20 03:06:15 +000014189 case X86::ATOMUMAX16:
14190 case X86::ATOMUMAX32:
14191 case X86::ATOMUMAX64:
14192 // Fall through
Michael Liaofe87c302012-09-21 03:18:52 +000014193 case X86::ATOMUMIN8:
Michael Liaob118a072012-09-20 03:06:15 +000014194 case X86::ATOMUMIN16:
14195 case X86::ATOMUMIN32:
14196 case X86::ATOMUMIN64:
14197 return EmitAtomicLoadArith(MI, BB);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000014198
14199 // This group does 64-bit operations on a 32-bit host.
14200 case X86::ATOMAND6432:
Dale Johannesen48c1bc22008-10-02 18:53:47 +000014201 case X86::ATOMOR6432:
Dale Johannesen48c1bc22008-10-02 18:53:47 +000014202 case X86::ATOMXOR6432:
Dale Johannesen48c1bc22008-10-02 18:53:47 +000014203 case X86::ATOMNAND6432:
Dale Johannesen48c1bc22008-10-02 18:53:47 +000014204 case X86::ATOMADD6432:
Dale Johannesen48c1bc22008-10-02 18:53:47 +000014205 case X86::ATOMSUB6432:
Michael Liaoe5e8f762012-09-25 18:08:13 +000014206 case X86::ATOMMAX6432:
14207 case X86::ATOMMIN6432:
14208 case X86::ATOMUMAX6432:
14209 case X86::ATOMUMIN6432:
Michael Liaob118a072012-09-20 03:06:15 +000014210 case X86::ATOMSWAP6432:
14211 return EmitAtomicLoadArith6432(MI, BB);
Craig Topperacaaa6f2012-08-18 06:39:34 +000014212
Dan Gohmand6708ea2009-08-15 01:38:56 +000014213 case X86::VASTART_SAVE_XMM_REGS:
14214 return EmitVAStartSaveXMMRegsWithCustomInserter(MI, BB);
Dan Gohman320afb82010-10-12 18:00:49 +000014215
14216 case X86::VAARG_64:
14217 return EmitVAARG64WithCustomInserter(MI, BB);
Michael Liao6c0e04c2012-10-15 22:39:43 +000014218
14219 case X86::EH_SjLj_SetJmp32:
14220 case X86::EH_SjLj_SetJmp64:
14221 return emitEHSjLjSetJmp(MI, BB);
14222
14223 case X86::EH_SjLj_LongJmp32:
14224 case X86::EH_SjLj_LongJmp64:
14225 return emitEHSjLjLongJmp(MI, BB);
Evan Cheng60c07e12006-07-05 22:17:51 +000014226 }
14227}
14228
14229//===----------------------------------------------------------------------===//
14230// X86 Optimization Hooks
14231//===----------------------------------------------------------------------===//
14232
Dan Gohman475871a2008-07-27 21:46:04 +000014233void X86TargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
Dan Gohmanfd29e0e2008-02-13 00:35:47 +000014234 APInt &KnownZero,
14235 APInt &KnownOne,
Dan Gohmanea859be2007-06-22 14:59:07 +000014236 const SelectionDAG &DAG,
Nate Begeman368e18d2006-02-16 21:11:51 +000014237 unsigned Depth) const {
Rafael Espindola26c8dcc2012-04-04 12:51:34 +000014238 unsigned BitWidth = KnownZero.getBitWidth();
Evan Cheng3a03ebb2005-12-21 23:05:39 +000014239 unsigned Opc = Op.getOpcode();
Evan Cheng865f0602006-04-05 06:11:20 +000014240 assert((Opc >= ISD::BUILTIN_OP_END ||
14241 Opc == ISD::INTRINSIC_WO_CHAIN ||
14242 Opc == ISD::INTRINSIC_W_CHAIN ||
14243 Opc == ISD::INTRINSIC_VOID) &&
14244 "Should use MaskedValueIsZero if you don't know whether Op"
14245 " is a target node!");
Evan Cheng3a03ebb2005-12-21 23:05:39 +000014246
Rafael Espindola26c8dcc2012-04-04 12:51:34 +000014247 KnownZero = KnownOne = APInt(BitWidth, 0); // Don't know anything.
Evan Cheng3a03ebb2005-12-21 23:05:39 +000014248 switch (Opc) {
Evan Cheng865f0602006-04-05 06:11:20 +000014249 default: break;
Evan Cheng97d0e0e2009-02-02 09:15:04 +000014250 case X86ISD::ADD:
14251 case X86ISD::SUB:
Chris Lattner5b856542010-12-20 00:59:46 +000014252 case X86ISD::ADC:
14253 case X86ISD::SBB:
Evan Cheng97d0e0e2009-02-02 09:15:04 +000014254 case X86ISD::SMUL:
14255 case X86ISD::UMUL:
Dan Gohman076aee32009-03-04 19:44:21 +000014256 case X86ISD::INC:
14257 case X86ISD::DEC:
Dan Gohmane220c4b2009-09-18 19:59:53 +000014258 case X86ISD::OR:
14259 case X86ISD::XOR:
14260 case X86ISD::AND:
Evan Cheng97d0e0e2009-02-02 09:15:04 +000014261 // These nodes' second result is a boolean.
14262 if (Op.getResNo() == 0)
14263 break;
14264 // Fallthrough
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000014265 case X86ISD::SETCC:
Rafael Espindola26c8dcc2012-04-04 12:51:34 +000014266 KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - 1);
Nate Begeman368e18d2006-02-16 21:11:51 +000014267 break;
Evan Cheng7c1780c2011-10-07 17:21:44 +000014268 case ISD::INTRINSIC_WO_CHAIN: {
14269 unsigned IntId = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
14270 unsigned NumLoBits = 0;
14271 switch (IntId) {
14272 default: break;
14273 case Intrinsic::x86_sse_movmsk_ps:
14274 case Intrinsic::x86_avx_movmsk_ps_256:
14275 case Intrinsic::x86_sse2_movmsk_pd:
14276 case Intrinsic::x86_avx_movmsk_pd_256:
14277 case Intrinsic::x86_mmx_pmovmskb:
Craig Topper3738ccd2011-12-27 06:27:23 +000014278 case Intrinsic::x86_sse2_pmovmskb_128:
14279 case Intrinsic::x86_avx2_pmovmskb: {
Evan Cheng7c1780c2011-10-07 17:21:44 +000014280 // High bits of movmskp{s|d}, pmovmskb are known zero.
14281 switch (IntId) {
Craig Topperabb94d02012-02-05 03:43:23 +000014282 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
Evan Cheng7c1780c2011-10-07 17:21:44 +000014283 case Intrinsic::x86_sse_movmsk_ps: NumLoBits = 4; break;
14284 case Intrinsic::x86_avx_movmsk_ps_256: NumLoBits = 8; break;
14285 case Intrinsic::x86_sse2_movmsk_pd: NumLoBits = 2; break;
14286 case Intrinsic::x86_avx_movmsk_pd_256: NumLoBits = 4; break;
14287 case Intrinsic::x86_mmx_pmovmskb: NumLoBits = 8; break;
14288 case Intrinsic::x86_sse2_pmovmskb_128: NumLoBits = 16; break;
Craig Topper3738ccd2011-12-27 06:27:23 +000014289 case Intrinsic::x86_avx2_pmovmskb: NumLoBits = 32; break;
Evan Cheng7c1780c2011-10-07 17:21:44 +000014290 }
Rafael Espindola26c8dcc2012-04-04 12:51:34 +000014291 KnownZero = APInt::getHighBitsSet(BitWidth, BitWidth - NumLoBits);
Evan Cheng7c1780c2011-10-07 17:21:44 +000014292 break;
14293 }
14294 }
14295 break;
14296 }
Evan Cheng3a03ebb2005-12-21 23:05:39 +000014297 }
Evan Cheng3a03ebb2005-12-21 23:05:39 +000014298}
Chris Lattner259e97c2006-01-31 19:43:35 +000014299
Owen Andersonbc146b02010-09-21 20:42:50 +000014300unsigned X86TargetLowering::ComputeNumSignBitsForTargetNode(SDValue Op,
14301 unsigned Depth) const {
14302 // SETCC_CARRY sets the dest to ~0 for true or 0 for false.
14303 if (Op.getOpcode() == X86ISD::SETCC_CARRY)
14304 return Op.getValueType().getScalarType().getSizeInBits();
Michael J. Spencerec38de22010-10-10 22:04:20 +000014305
Owen Andersonbc146b02010-09-21 20:42:50 +000014306 // Fallback case.
14307 return 1;
14308}
14309
Evan Cheng206ee9d2006-07-07 08:33:52 +000014310/// isGAPlusOffset - Returns true (and the GlobalValue and the offset) if the
Evan Chengad4196b2008-05-12 19:56:52 +000014311/// node is a GlobalAddress + offset.
14312bool X86TargetLowering::isGAPlusOffset(SDNode *N,
Dan Gohman46510a72010-04-15 01:51:59 +000014313 const GlobalValue* &GA,
14314 int64_t &Offset) const {
Evan Chengad4196b2008-05-12 19:56:52 +000014315 if (N->getOpcode() == X86ISD::Wrapper) {
14316 if (isa<GlobalAddressSDNode>(N->getOperand(0))) {
Evan Cheng206ee9d2006-07-07 08:33:52 +000014317 GA = cast<GlobalAddressSDNode>(N->getOperand(0))->getGlobal();
Dan Gohman6520e202008-10-18 02:06:02 +000014318 Offset = cast<GlobalAddressSDNode>(N->getOperand(0))->getOffset();
Evan Cheng206ee9d2006-07-07 08:33:52 +000014319 return true;
14320 }
Evan Cheng206ee9d2006-07-07 08:33:52 +000014321 }
Evan Chengad4196b2008-05-12 19:56:52 +000014322 return TargetLowering::isGAPlusOffset(N, GA, Offset);
Evan Cheng206ee9d2006-07-07 08:33:52 +000014323}
14324
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000014325/// isShuffleHigh128VectorInsertLow - Checks whether the shuffle node is the
14326/// same as extracting the high 128-bit part of 256-bit vector and then
14327/// inserting the result into the low part of a new 256-bit vector
14328static bool isShuffleHigh128VectorInsertLow(ShuffleVectorSDNode *SVOp) {
14329 EVT VT = SVOp->getValueType(0);
Craig Topper66ddd152012-04-27 22:54:43 +000014330 unsigned NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000014331
14332 // vector_shuffle <4, 5, 6, 7, u, u, u, u> or <2, 3, u, u>
Craig Topper66ddd152012-04-27 22:54:43 +000014333 for (unsigned i = 0, j = NumElems/2; i != NumElems/2; ++i, ++j)
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000014334 if (!isUndefOrEqual(SVOp->getMaskElt(i), j) ||
14335 SVOp->getMaskElt(j) >= 0)
14336 return false;
14337
14338 return true;
14339}
14340
14341/// isShuffleLow128VectorInsertHigh - Checks whether the shuffle node is the
14342/// same as extracting the low 128-bit part of 256-bit vector and then
14343/// inserting the result into the high part of a new 256-bit vector
14344static bool isShuffleLow128VectorInsertHigh(ShuffleVectorSDNode *SVOp) {
14345 EVT VT = SVOp->getValueType(0);
Craig Topper66ddd152012-04-27 22:54:43 +000014346 unsigned NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000014347
14348 // vector_shuffle <u, u, u, u, 0, 1, 2, 3> or <u, u, 0, 1>
Craig Topper66ddd152012-04-27 22:54:43 +000014349 for (unsigned i = NumElems/2, j = 0; i != NumElems; ++i, ++j)
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000014350 if (!isUndefOrEqual(SVOp->getMaskElt(i), j) ||
14351 SVOp->getMaskElt(j) >= 0)
14352 return false;
14353
14354 return true;
14355}
14356
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000014357/// PerformShuffleCombine256 - Performs shuffle combines for 256-bit vectors.
14358static SDValue PerformShuffleCombine256(SDNode *N, SelectionDAG &DAG,
Craig Topper12216172012-01-13 08:12:35 +000014359 TargetLowering::DAGCombinerInfo &DCI,
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +000014360 const X86Subtarget* Subtarget) {
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000014361 DebugLoc dl = N->getDebugLoc();
14362 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
14363 SDValue V1 = SVOp->getOperand(0);
14364 SDValue V2 = SVOp->getOperand(1);
14365 EVT VT = SVOp->getValueType(0);
Craig Topper66ddd152012-04-27 22:54:43 +000014366 unsigned NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000014367
14368 if (V1.getOpcode() == ISD::CONCAT_VECTORS &&
14369 V2.getOpcode() == ISD::CONCAT_VECTORS) {
14370 //
14371 // 0,0,0,...
Benjamin Kramer558cc5a2011-07-22 01:02:57 +000014372 // |
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000014373 // V UNDEF BUILD_VECTOR UNDEF
14374 // \ / \ /
14375 // CONCAT_VECTOR CONCAT_VECTOR
14376 // \ /
14377 // \ /
14378 // RESULT: V + zero extended
14379 //
14380 if (V2.getOperand(0).getOpcode() != ISD::BUILD_VECTOR ||
14381 V2.getOperand(1).getOpcode() != ISD::UNDEF ||
14382 V1.getOperand(1).getOpcode() != ISD::UNDEF)
14383 return SDValue();
14384
14385 if (!ISD::isBuildVectorAllZeros(V2.getOperand(0).getNode()))
14386 return SDValue();
14387
14388 // To match the shuffle mask, the first half of the mask should
14389 // be exactly the first vector, and all the rest a splat with the
14390 // first element of the second one.
Craig Topper66ddd152012-04-27 22:54:43 +000014391 for (unsigned i = 0; i != NumElems/2; ++i)
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000014392 if (!isUndefOrEqual(SVOp->getMaskElt(i), i) ||
14393 !isUndefOrEqual(SVOp->getMaskElt(i+NumElems/2), NumElems))
14394 return SDValue();
14395
Chad Rosier3d1161e2012-01-03 21:05:52 +000014396 // If V1 is coming from a vector load then just fold to a VZEXT_LOAD.
14397 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(V1.getOperand(0))) {
Chad Rosier42726832012-05-07 18:47:44 +000014398 if (Ld->hasNUsesOfValue(1, 0)) {
14399 SDVTList Tys = DAG.getVTList(MVT::v4i64, MVT::Other);
14400 SDValue Ops[] = { Ld->getChain(), Ld->getBasePtr() };
14401 SDValue ResNode =
14402 DAG.getMemIntrinsicNode(X86ISD::VZEXT_LOAD, dl, Tys, Ops, 2,
14403 Ld->getMemoryVT(),
14404 Ld->getPointerInfo(),
14405 Ld->getAlignment(),
14406 false/*isVolatile*/, true/*ReadMem*/,
14407 false/*WriteMem*/);
Manman Ren2adc5032012-11-13 19:13:05 +000014408
14409 // Make sure the newly-created LOAD is in the same position as Ld in
14410 // terms of dependency. We create a TokenFactor for Ld and ResNode,
14411 // and update uses of Ld's output chain to use the TokenFactor.
14412 if (Ld->hasAnyUseOfValue(1)) {
14413 SDValue NewChain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
14414 SDValue(Ld, 1), SDValue(ResNode.getNode(), 1));
14415 DAG.ReplaceAllUsesOfValueWith(SDValue(Ld, 1), NewChain);
14416 DAG.UpdateNodeOperands(NewChain.getNode(), SDValue(Ld, 1),
14417 SDValue(ResNode.getNode(), 1));
14418 }
14419
Chad Rosier42726832012-05-07 18:47:44 +000014420 return DAG.getNode(ISD::BITCAST, dl, VT, ResNode);
14421 }
Chad Rosiera20e1e72012-08-01 18:39:17 +000014422 }
Chad Rosier3d1161e2012-01-03 21:05:52 +000014423
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000014424 // Emit a zeroed vector and insert the desired subvector on its
14425 // first half.
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +000014426 SDValue Zeros = getZeroVector(VT, Subtarget, DAG, dl);
Craig Topperb14940a2012-04-22 20:55:18 +000014427 SDValue InsV = Insert128BitVector(Zeros, V1.getOperand(0), 0, DAG, dl);
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000014428 return DCI.CombineTo(N, InsV);
14429 }
14430
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000014431 //===--------------------------------------------------------------------===//
14432 // Combine some shuffles into subvector extracts and inserts:
14433 //
14434
14435 // vector_shuffle <4, 5, 6, 7, u, u, u, u> or <2, 3, u, u>
14436 if (isShuffleHigh128VectorInsertLow(SVOp)) {
Craig Topperb14940a2012-04-22 20:55:18 +000014437 SDValue V = Extract128BitVector(V1, NumElems/2, DAG, dl);
14438 SDValue InsV = Insert128BitVector(DAG.getUNDEF(VT), V, 0, DAG, dl);
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000014439 return DCI.CombineTo(N, InsV);
14440 }
14441
14442 // vector_shuffle <u, u, u, u, 0, 1, 2, 3> or <u, u, 0, 1>
14443 if (isShuffleLow128VectorInsertHigh(SVOp)) {
Craig Topperb14940a2012-04-22 20:55:18 +000014444 SDValue V = Extract128BitVector(V1, 0, DAG, dl);
14445 SDValue InsV = Insert128BitVector(DAG.getUNDEF(VT), V, NumElems/2, DAG, dl);
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000014446 return DCI.CombineTo(N, InsV);
14447 }
14448
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000014449 return SDValue();
14450}
14451
14452/// PerformShuffleCombine - Performs several different shuffle combines.
Dan Gohman475871a2008-07-27 21:46:04 +000014453static SDValue PerformShuffleCombine(SDNode *N, SelectionDAG &DAG,
Bruno Cardoso Lopes50b37c72011-08-15 21:45:54 +000014454 TargetLowering::DAGCombinerInfo &DCI,
14455 const X86Subtarget *Subtarget) {
Dale Johannesene4d209d2009-02-03 20:21:25 +000014456 DebugLoc dl = N->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +000014457 EVT VT = N->getValueType(0);
Mon P Wang1e955802009-04-03 02:43:30 +000014458
Mon P Wanga0fd0d52010-12-19 23:55:53 +000014459 // Don't create instructions with illegal types after legalize types has run.
14460 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
14461 if (!DCI.isBeforeLegalize() && !TLI.isTypeLegal(VT.getVectorElementType()))
14462 return SDValue();
14463
Bruno Cardoso Lopes50b37c72011-08-15 21:45:54 +000014464 // Combine 256-bit vector shuffles. This is only profitable when in AVX mode
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000014465 if (Subtarget->hasFp256() && VT.is256BitVector() &&
Bruno Cardoso Lopes50b37c72011-08-15 21:45:54 +000014466 N->getOpcode() == ISD::VECTOR_SHUFFLE)
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +000014467 return PerformShuffleCombine256(N, DAG, DCI, Subtarget);
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000014468
14469 // Only handle 128 wide vector from here on.
Craig Topper7a9a28b2012-08-12 02:23:29 +000014470 if (!VT.is128BitVector())
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000014471 return SDValue();
14472
14473 // Combine a vector_shuffle that is equal to build_vector load1, load2, load3,
14474 // load4, <0, 1, 2, 3> into a 128-bit load if the load addresses are
14475 // consecutive, non-overlapping, and in the right order.
Nate Begemanfdea31a2010-03-24 20:49:50 +000014476 SmallVector<SDValue, 16> Elts;
14477 for (unsigned i = 0, e = VT.getVectorNumElements(); i != e; ++i)
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000014478 Elts.push_back(getShuffleScalarElt(N, i, DAG, 0));
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +000014479
Nate Begemanfdea31a2010-03-24 20:49:50 +000014480 return EltsFromConsecutiveLoads(VT, Elts, dl, DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +000014481}
Evan Chengd880b972008-05-09 21:53:03 +000014482
Craig Topper55b24052012-09-11 06:15:32 +000014483/// PerformTruncateCombine - Converts truncate operation to
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000014484/// a sequence of vector shuffle operations.
14485/// It is possible when we truncate 256-bit vector to 128-bit vector
Craig Topper55b24052012-09-11 06:15:32 +000014486static SDValue PerformTruncateCombine(SDNode *N, SelectionDAG &DAG,
14487 TargetLowering::DAGCombinerInfo &DCI,
14488 const X86Subtarget *Subtarget) {
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000014489 return SDValue();
14490}
14491
Craig Topper89f4e662012-03-20 07:17:59 +000014492/// XFormVExtractWithShuffleIntoLoad - Check if a vector extract from a target
14493/// specific shuffle of a load can be folded into a single element load.
14494/// Similar handling for VECTOR_SHUFFLE is performed by DAGCombiner, but
14495/// shuffles have been customed lowered so we need to handle those here.
14496static SDValue XFormVExtractWithShuffleIntoLoad(SDNode *N, SelectionDAG &DAG,
14497 TargetLowering::DAGCombinerInfo &DCI) {
14498 if (DCI.isBeforeLegalizeOps())
14499 return SDValue();
14500
14501 SDValue InVec = N->getOperand(0);
14502 SDValue EltNo = N->getOperand(1);
14503
14504 if (!isa<ConstantSDNode>(EltNo))
14505 return SDValue();
14506
14507 EVT VT = InVec.getValueType();
14508
14509 bool HasShuffleIntoBitcast = false;
14510 if (InVec.getOpcode() == ISD::BITCAST) {
14511 // Don't duplicate a load with other uses.
14512 if (!InVec.hasOneUse())
14513 return SDValue();
14514 EVT BCVT = InVec.getOperand(0).getValueType();
14515 if (BCVT.getVectorNumElements() != VT.getVectorNumElements())
14516 return SDValue();
14517 InVec = InVec.getOperand(0);
14518 HasShuffleIntoBitcast = true;
14519 }
14520
14521 if (!isTargetShuffle(InVec.getOpcode()))
14522 return SDValue();
14523
14524 // Don't duplicate a load with other uses.
14525 if (!InVec.hasOneUse())
14526 return SDValue();
14527
14528 SmallVector<int, 16> ShuffleMask;
14529 bool UnaryShuffle;
Craig Topperd978c542012-05-06 19:46:21 +000014530 if (!getTargetShuffleMask(InVec.getNode(), VT.getSimpleVT(), ShuffleMask,
14531 UnaryShuffle))
Craig Topper89f4e662012-03-20 07:17:59 +000014532 return SDValue();
14533
14534 // Select the input vector, guarding against out of range extract vector.
14535 unsigned NumElems = VT.getVectorNumElements();
14536 int Elt = cast<ConstantSDNode>(EltNo)->getZExtValue();
14537 int Idx = (Elt > (int)NumElems) ? -1 : ShuffleMask[Elt];
14538 SDValue LdNode = (Idx < (int)NumElems) ? InVec.getOperand(0)
14539 : InVec.getOperand(1);
14540
14541 // If inputs to shuffle are the same for both ops, then allow 2 uses
14542 unsigned AllowedUses = InVec.getOperand(0) == InVec.getOperand(1) ? 2 : 1;
14543
14544 if (LdNode.getOpcode() == ISD::BITCAST) {
14545 // Don't duplicate a load with other uses.
14546 if (!LdNode.getNode()->hasNUsesOfValue(AllowedUses, 0))
14547 return SDValue();
14548
14549 AllowedUses = 1; // only allow 1 load use if we have a bitcast
14550 LdNode = LdNode.getOperand(0);
14551 }
14552
14553 if (!ISD::isNormalLoad(LdNode.getNode()))
14554 return SDValue();
14555
14556 LoadSDNode *LN0 = cast<LoadSDNode>(LdNode);
14557
14558 if (!LN0 ||!LN0->hasNUsesOfValue(AllowedUses, 0) || LN0->isVolatile())
14559 return SDValue();
14560
14561 if (HasShuffleIntoBitcast) {
14562 // If there's a bitcast before the shuffle, check if the load type and
14563 // alignment is valid.
14564 unsigned Align = LN0->getAlignment();
14565 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Micah Villmow3574eca2012-10-08 16:38:25 +000014566 unsigned NewAlign = TLI.getDataLayout()->
Craig Topper89f4e662012-03-20 07:17:59 +000014567 getABITypeAlignment(VT.getTypeForEVT(*DAG.getContext()));
14568
14569 if (NewAlign > Align || !TLI.isOperationLegalOrCustom(ISD::LOAD, VT))
14570 return SDValue();
14571 }
14572
14573 // All checks match so transform back to vector_shuffle so that DAG combiner
14574 // can finish the job
14575 DebugLoc dl = N->getDebugLoc();
14576
14577 // Create shuffle node taking into account the case that its a unary shuffle
14578 SDValue Shuffle = (UnaryShuffle) ? DAG.getUNDEF(VT) : InVec.getOperand(1);
14579 Shuffle = DAG.getVectorShuffle(InVec.getValueType(), dl,
14580 InVec.getOperand(0), Shuffle,
14581 &ShuffleMask[0]);
14582 Shuffle = DAG.getNode(ISD::BITCAST, dl, VT, Shuffle);
14583 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, N->getValueType(0), Shuffle,
14584 EltNo);
14585}
14586
Bruno Cardoso Lopesb3e06692010-09-03 19:55:05 +000014587/// PerformEXTRACT_VECTOR_ELTCombine - Detect vector gather/scatter index
14588/// generation and convert it from being a bunch of shuffles and extracts
14589/// to a simple store and scalar loads to extract the elements.
Dan Gohman1bbf72b2010-03-15 23:23:03 +000014590static SDValue PerformEXTRACT_VECTOR_ELTCombine(SDNode *N, SelectionDAG &DAG,
Craig Topper89f4e662012-03-20 07:17:59 +000014591 TargetLowering::DAGCombinerInfo &DCI) {
14592 SDValue NewOp = XFormVExtractWithShuffleIntoLoad(N, DAG, DCI);
14593 if (NewOp.getNode())
14594 return NewOp;
14595
Dan Gohman1bbf72b2010-03-15 23:23:03 +000014596 SDValue InputVector = N->getOperand(0);
Manman Ren4c74a952012-10-30 22:15:38 +000014597 // Detect whether we are trying to convert from mmx to i32 and the bitcast
14598 // from mmx to v2i32 has a single usage.
14599 if (InputVector.getNode()->getOpcode() == llvm::ISD::BITCAST &&
14600 InputVector.getNode()->getOperand(0).getValueType() == MVT::x86mmx &&
14601 InputVector.hasOneUse() && N->getValueType(0) == MVT::i32)
14602 return DAG.getNode(X86ISD::MMX_MOVD2W, InputVector.getDebugLoc(),
14603 N->getValueType(0),
14604 InputVector.getNode()->getOperand(0));
Dan Gohman1bbf72b2010-03-15 23:23:03 +000014605
14606 // Only operate on vectors of 4 elements, where the alternative shuffling
14607 // gets to be more expensive.
14608 if (InputVector.getValueType() != MVT::v4i32)
14609 return SDValue();
14610
14611 // Check whether every use of InputVector is an EXTRACT_VECTOR_ELT with a
14612 // single use which is a sign-extend or zero-extend, and all elements are
14613 // used.
14614 SmallVector<SDNode *, 4> Uses;
14615 unsigned ExtractedElements = 0;
14616 for (SDNode::use_iterator UI = InputVector.getNode()->use_begin(),
14617 UE = InputVector.getNode()->use_end(); UI != UE; ++UI) {
14618 if (UI.getUse().getResNo() != InputVector.getResNo())
14619 return SDValue();
14620
14621 SDNode *Extract = *UI;
14622 if (Extract->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
14623 return SDValue();
14624
14625 if (Extract->getValueType(0) != MVT::i32)
14626 return SDValue();
14627 if (!Extract->hasOneUse())
14628 return SDValue();
14629 if (Extract->use_begin()->getOpcode() != ISD::SIGN_EXTEND &&
14630 Extract->use_begin()->getOpcode() != ISD::ZERO_EXTEND)
14631 return SDValue();
14632 if (!isa<ConstantSDNode>(Extract->getOperand(1)))
14633 return SDValue();
14634
14635 // Record which element was extracted.
14636 ExtractedElements |=
14637 1 << cast<ConstantSDNode>(Extract->getOperand(1))->getZExtValue();
14638
14639 Uses.push_back(Extract);
14640 }
14641
14642 // If not all the elements were used, this may not be worthwhile.
14643 if (ExtractedElements != 15)
14644 return SDValue();
14645
14646 // Ok, we've now decided to do the transformation.
14647 DebugLoc dl = InputVector.getDebugLoc();
14648
14649 // Store the value to a temporary stack slot.
14650 SDValue StackPtr = DAG.CreateStackTemporary(InputVector.getValueType());
Chris Lattner8026a9d2010-09-21 17:50:43 +000014651 SDValue Ch = DAG.getStore(DAG.getEntryNode(), dl, InputVector, StackPtr,
14652 MachinePointerInfo(), false, false, 0);
Dan Gohman1bbf72b2010-03-15 23:23:03 +000014653
14654 // Replace each use (extract) with a load of the appropriate element.
14655 for (SmallVectorImpl<SDNode *>::iterator UI = Uses.begin(),
14656 UE = Uses.end(); UI != UE; ++UI) {
14657 SDNode *Extract = *UI;
14658
Nadav Rotem86694292011-05-17 08:31:57 +000014659 // cOMpute the element's address.
Dan Gohman1bbf72b2010-03-15 23:23:03 +000014660 SDValue Idx = Extract->getOperand(1);
14661 unsigned EltSize =
14662 InputVector.getValueType().getVectorElementType().getSizeInBits()/8;
14663 uint64_t Offset = EltSize * cast<ConstantSDNode>(Idx)->getZExtValue();
Craig Topper89f4e662012-03-20 07:17:59 +000014664 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Dan Gohman1bbf72b2010-03-15 23:23:03 +000014665 SDValue OffsetVal = DAG.getConstant(Offset, TLI.getPointerTy());
14666
Nadav Rotem86694292011-05-17 08:31:57 +000014667 SDValue ScalarAddr = DAG.getNode(ISD::ADD, dl, TLI.getPointerTy(),
Chris Lattner51abfe42010-09-21 06:02:19 +000014668 StackPtr, OffsetVal);
Dan Gohman1bbf72b2010-03-15 23:23:03 +000014669
14670 // Load the scalar.
Eric Christopher90eb4022010-07-22 00:26:08 +000014671 SDValue LoadScalar = DAG.getLoad(Extract->getValueType(0), dl, Ch,
Chris Lattner51abfe42010-09-21 06:02:19 +000014672 ScalarAddr, MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000014673 false, false, false, 0);
Dan Gohman1bbf72b2010-03-15 23:23:03 +000014674
14675 // Replace the exact with the load.
14676 DAG.ReplaceAllUsesOfValueWith(SDValue(Extract, 0), LoadScalar);
14677 }
14678
14679 // The replacement was made in place; don't return anything.
14680 return SDValue();
14681}
14682
Benjamin Kramer2556c6b2012-12-21 17:46:58 +000014683/// \brief Matches a VSELECT onto min/max or return 0 if the node doesn't match.
14684static unsigned matchIntegerMINMAX(SDValue Cond, EVT VT, SDValue LHS,
14685 SDValue RHS, SelectionDAG &DAG,
14686 const X86Subtarget *Subtarget) {
14687 if (!VT.isVector())
14688 return 0;
14689
14690 switch (VT.getSimpleVT().SimpleTy) {
14691 default: return 0;
14692 case MVT::v32i8:
14693 case MVT::v16i16:
14694 case MVT::v8i32:
14695 if (!Subtarget->hasAVX2())
14696 return 0;
14697 case MVT::v16i8:
14698 case MVT::v8i16:
14699 case MVT::v4i32:
14700 if (!Subtarget->hasSSE2())
14701 return 0;
14702 }
14703
14704 // SSE2 has only a small subset of the operations.
14705 bool hasUnsigned = Subtarget->hasSSE41() ||
14706 (Subtarget->hasSSE2() && VT == MVT::v16i8);
14707 bool hasSigned = Subtarget->hasSSE41() ||
14708 (Subtarget->hasSSE2() && VT == MVT::v8i16);
14709
14710 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
14711
14712 // Check for x CC y ? x : y.
14713 if (DAG.isEqualTo(LHS, Cond.getOperand(0)) &&
14714 DAG.isEqualTo(RHS, Cond.getOperand(1))) {
14715 switch (CC) {
14716 default: break;
14717 case ISD::SETULT:
14718 case ISD::SETULE:
14719 return hasUnsigned ? X86ISD::UMIN : 0;
14720 case ISD::SETUGT:
14721 case ISD::SETUGE:
14722 return hasUnsigned ? X86ISD::UMAX : 0;
14723 case ISD::SETLT:
14724 case ISD::SETLE:
14725 return hasSigned ? X86ISD::SMIN : 0;
14726 case ISD::SETGT:
14727 case ISD::SETGE:
14728 return hasSigned ? X86ISD::SMAX : 0;
14729 }
14730 // Check for x CC y ? y : x -- a min/max with reversed arms.
14731 } else if (DAG.isEqualTo(LHS, Cond.getOperand(1)) &&
14732 DAG.isEqualTo(RHS, Cond.getOperand(0))) {
14733 switch (CC) {
14734 default: break;
14735 case ISD::SETULT:
14736 case ISD::SETULE:
14737 return hasUnsigned ? X86ISD::UMAX : 0;
14738 case ISD::SETUGT:
14739 case ISD::SETUGE:
14740 return hasUnsigned ? X86ISD::UMIN : 0;
14741 case ISD::SETLT:
14742 case ISD::SETLE:
14743 return hasSigned ? X86ISD::SMAX : 0;
14744 case ISD::SETGT:
14745 case ISD::SETGE:
14746 return hasSigned ? X86ISD::SMIN : 0;
14747 }
14748 }
14749
14750 return 0;
14751}
14752
Duncan Sands6bcd2192011-09-17 16:49:39 +000014753/// PerformSELECTCombine - Do target-specific dag combines on SELECT and VSELECT
14754/// nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000014755static SDValue PerformSELECTCombine(SDNode *N, SelectionDAG &DAG,
Nadav Rotemcc616562012-01-15 19:27:55 +000014756 TargetLowering::DAGCombinerInfo &DCI,
Chris Lattner47b4ce82009-03-11 05:48:52 +000014757 const X86Subtarget *Subtarget) {
14758 DebugLoc DL = N->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +000014759 SDValue Cond = N->getOperand(0);
Chris Lattner47b4ce82009-03-11 05:48:52 +000014760 // Get the LHS/RHS of the select.
14761 SDValue LHS = N->getOperand(1);
14762 SDValue RHS = N->getOperand(2);
Bruno Cardoso Lopes149f29f2011-09-20 22:34:45 +000014763 EVT VT = LHS.getValueType();
Eric Christopherfd179292009-08-27 18:07:15 +000014764
Dan Gohman670e5392009-09-21 18:03:22 +000014765 // If we have SSE[12] support, try to form min/max nodes. SSE min/max
Dan Gohman8ce05da2010-02-22 04:03:39 +000014766 // instructions match the semantics of the common C idiom x<y?x:y but not
14767 // x<=y?x:y, because of how they handle negative zero (which can be
14768 // ignored in unsafe-math mode).
Benjamin Kramer2c2ccbf2011-09-22 03:27:22 +000014769 if (Cond.getOpcode() == ISD::SETCC && VT.isFloatingPoint() &&
14770 VT != MVT::f80 && DAG.getTargetLoweringInfo().isTypeLegal(VT) &&
Craig Topper1accb7e2012-01-10 06:54:16 +000014771 (Subtarget->hasSSE2() ||
14772 (Subtarget->hasSSE1() && VT.getScalarType() == MVT::f32))) {
Chris Lattner47b4ce82009-03-11 05:48:52 +000014773 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000014774
Chris Lattner47b4ce82009-03-11 05:48:52 +000014775 unsigned Opcode = 0;
Dan Gohman670e5392009-09-21 18:03:22 +000014776 // Check for x CC y ? x : y.
Dan Gohmane8326932010-02-24 06:52:40 +000014777 if (DAG.isEqualTo(LHS, Cond.getOperand(0)) &&
14778 DAG.isEqualTo(RHS, Cond.getOperand(1))) {
Chris Lattner47b4ce82009-03-11 05:48:52 +000014779 switch (CC) {
14780 default: break;
Dan Gohman670e5392009-09-21 18:03:22 +000014781 case ISD::SETULT:
Dan Gohmane8326932010-02-24 06:52:40 +000014782 // Converting this to a min would handle NaNs incorrectly, and swapping
14783 // the operands would cause it to handle comparisons between positive
14784 // and negative zero incorrectly.
Evan Cheng60108e92010-07-15 22:07:12 +000014785 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)) {
Nick Lewycky8a8d4792011-12-02 22:16:29 +000014786 if (!DAG.getTarget().Options.UnsafeFPMath &&
Dan Gohmane8326932010-02-24 06:52:40 +000014787 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
14788 break;
14789 std::swap(LHS, RHS);
14790 }
Dan Gohman670e5392009-09-21 18:03:22 +000014791 Opcode = X86ISD::FMIN;
14792 break;
14793 case ISD::SETOLE:
Dan Gohmane8326932010-02-24 06:52:40 +000014794 // Converting this to a min would handle comparisons between positive
14795 // and negative zero incorrectly.
Nick Lewycky8a8d4792011-12-02 22:16:29 +000014796 if (!DAG.getTarget().Options.UnsafeFPMath &&
Dan Gohmane8326932010-02-24 06:52:40 +000014797 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(RHS))
14798 break;
Dan Gohman670e5392009-09-21 18:03:22 +000014799 Opcode = X86ISD::FMIN;
14800 break;
Chris Lattner47b4ce82009-03-11 05:48:52 +000014801 case ISD::SETULE:
Dan Gohmane8326932010-02-24 06:52:40 +000014802 // Converting this to a min would handle both negative zeros and NaNs
14803 // incorrectly, but we can swap the operands to fix both.
14804 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000014805 case ISD::SETOLT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000014806 case ISD::SETLT:
Dan Gohman670e5392009-09-21 18:03:22 +000014807 case ISD::SETLE:
Chris Lattner47b4ce82009-03-11 05:48:52 +000014808 Opcode = X86ISD::FMIN;
14809 break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000014810
Dan Gohman670e5392009-09-21 18:03:22 +000014811 case ISD::SETOGE:
Dan Gohmane8326932010-02-24 06:52:40 +000014812 // Converting this to a max would handle comparisons between positive
14813 // and negative zero incorrectly.
Nick Lewycky8a8d4792011-12-02 22:16:29 +000014814 if (!DAG.getTarget().Options.UnsafeFPMath &&
Evan Chengdd5663c2011-08-04 18:38:15 +000014815 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000014816 break;
Dan Gohman670e5392009-09-21 18:03:22 +000014817 Opcode = X86ISD::FMAX;
14818 break;
Chris Lattner47b4ce82009-03-11 05:48:52 +000014819 case ISD::SETUGT:
Dan Gohmane8326932010-02-24 06:52:40 +000014820 // Converting this to a max would handle NaNs incorrectly, and swapping
14821 // the operands would cause it to handle comparisons between positive
14822 // and negative zero incorrectly.
Evan Cheng60108e92010-07-15 22:07:12 +000014823 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)) {
Nick Lewycky8a8d4792011-12-02 22:16:29 +000014824 if (!DAG.getTarget().Options.UnsafeFPMath &&
Dan Gohmane8326932010-02-24 06:52:40 +000014825 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
14826 break;
14827 std::swap(LHS, RHS);
14828 }
Dan Gohman670e5392009-09-21 18:03:22 +000014829 Opcode = X86ISD::FMAX;
14830 break;
14831 case ISD::SETUGE:
Dan Gohmane8326932010-02-24 06:52:40 +000014832 // Converting this to a max would handle both negative zeros and NaNs
14833 // incorrectly, but we can swap the operands to fix both.
14834 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000014835 case ISD::SETOGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000014836 case ISD::SETGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000014837 case ISD::SETGE:
14838 Opcode = X86ISD::FMAX;
14839 break;
Chris Lattner83e6c992006-10-04 06:57:07 +000014840 }
Dan Gohman670e5392009-09-21 18:03:22 +000014841 // Check for x CC y ? y : x -- a min/max with reversed arms.
Dan Gohmane8326932010-02-24 06:52:40 +000014842 } else if (DAG.isEqualTo(LHS, Cond.getOperand(1)) &&
14843 DAG.isEqualTo(RHS, Cond.getOperand(0))) {
Chris Lattner47b4ce82009-03-11 05:48:52 +000014844 switch (CC) {
14845 default: break;
Dan Gohman670e5392009-09-21 18:03:22 +000014846 case ISD::SETOGE:
Dan Gohmane8326932010-02-24 06:52:40 +000014847 // Converting this to a min would handle comparisons between positive
14848 // and negative zero incorrectly, and swapping the operands would
14849 // cause it to handle NaNs incorrectly.
Nick Lewycky8a8d4792011-12-02 22:16:29 +000014850 if (!DAG.getTarget().Options.UnsafeFPMath &&
Dan Gohmane8326932010-02-24 06:52:40 +000014851 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS))) {
Evan Cheng60108e92010-07-15 22:07:12 +000014852 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000014853 break;
14854 std::swap(LHS, RHS);
14855 }
Dan Gohman670e5392009-09-21 18:03:22 +000014856 Opcode = X86ISD::FMIN;
Dan Gohman8d44b282009-09-03 20:34:31 +000014857 break;
Dan Gohman670e5392009-09-21 18:03:22 +000014858 case ISD::SETUGT:
Dan Gohmane8326932010-02-24 06:52:40 +000014859 // Converting this to a min would handle NaNs incorrectly.
Nick Lewycky8a8d4792011-12-02 22:16:29 +000014860 if (!DAG.getTarget().Options.UnsafeFPMath &&
Dan Gohmane8326932010-02-24 06:52:40 +000014861 (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)))
14862 break;
Dan Gohman670e5392009-09-21 18:03:22 +000014863 Opcode = X86ISD::FMIN;
14864 break;
14865 case ISD::SETUGE:
Dan Gohmane8326932010-02-24 06:52:40 +000014866 // Converting this to a min would handle both negative zeros and NaNs
14867 // incorrectly, but we can swap the operands to fix both.
14868 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000014869 case ISD::SETOGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000014870 case ISD::SETGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000014871 case ISD::SETGE:
14872 Opcode = X86ISD::FMIN;
14873 break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000014874
Dan Gohman670e5392009-09-21 18:03:22 +000014875 case ISD::SETULT:
Dan Gohmane8326932010-02-24 06:52:40 +000014876 // Converting this to a max would handle NaNs incorrectly.
Evan Cheng60108e92010-07-15 22:07:12 +000014877 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000014878 break;
Dan Gohman670e5392009-09-21 18:03:22 +000014879 Opcode = X86ISD::FMAX;
Dan Gohman8d44b282009-09-03 20:34:31 +000014880 break;
Dan Gohman670e5392009-09-21 18:03:22 +000014881 case ISD::SETOLE:
Dan Gohmane8326932010-02-24 06:52:40 +000014882 // Converting this to a max would handle comparisons between positive
14883 // and negative zero incorrectly, and swapping the operands would
14884 // cause it to handle NaNs incorrectly.
Nick Lewycky8a8d4792011-12-02 22:16:29 +000014885 if (!DAG.getTarget().Options.UnsafeFPMath &&
Dan Gohmane8326932010-02-24 06:52:40 +000014886 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(RHS)) {
Evan Cheng60108e92010-07-15 22:07:12 +000014887 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000014888 break;
14889 std::swap(LHS, RHS);
14890 }
Dan Gohman670e5392009-09-21 18:03:22 +000014891 Opcode = X86ISD::FMAX;
14892 break;
14893 case ISD::SETULE:
Dan Gohmane8326932010-02-24 06:52:40 +000014894 // Converting this to a max would handle both negative zeros and NaNs
14895 // incorrectly, but we can swap the operands to fix both.
14896 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000014897 case ISD::SETOLT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000014898 case ISD::SETLT:
Dan Gohman670e5392009-09-21 18:03:22 +000014899 case ISD::SETLE:
Chris Lattner47b4ce82009-03-11 05:48:52 +000014900 Opcode = X86ISD::FMAX;
14901 break;
14902 }
Chris Lattner83e6c992006-10-04 06:57:07 +000014903 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000014904
Chris Lattner47b4ce82009-03-11 05:48:52 +000014905 if (Opcode)
14906 return DAG.getNode(Opcode, DL, N->getValueType(0), LHS, RHS);
Chris Lattner83e6c992006-10-04 06:57:07 +000014907 }
Eric Christopherfd179292009-08-27 18:07:15 +000014908
Chris Lattnerd1980a52009-03-12 06:52:53 +000014909 // If this is a select between two integer constants, try to do some
14910 // optimizations.
Chris Lattnercee56e72009-03-13 05:53:31 +000014911 if (ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(LHS)) {
14912 if (ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(RHS))
Chris Lattnerd1980a52009-03-12 06:52:53 +000014913 // Don't do this for crazy integer types.
14914 if (DAG.getTargetLoweringInfo().isTypeLegal(LHS.getValueType())) {
14915 // If this is efficiently invertible, canonicalize the LHSC/RHSC values
Chris Lattnercee56e72009-03-13 05:53:31 +000014916 // so that TrueC (the true value) is larger than FalseC.
Chris Lattnerd1980a52009-03-12 06:52:53 +000014917 bool NeedsCondInvert = false;
Eric Christopherfd179292009-08-27 18:07:15 +000014918
Chris Lattnercee56e72009-03-13 05:53:31 +000014919 if (TrueC->getAPIntValue().ult(FalseC->getAPIntValue()) &&
Chris Lattnerd1980a52009-03-12 06:52:53 +000014920 // Efficiently invertible.
14921 (Cond.getOpcode() == ISD::SETCC || // setcc -> invertible.
14922 (Cond.getOpcode() == ISD::XOR && // xor(X, C) -> invertible.
14923 isa<ConstantSDNode>(Cond.getOperand(1))))) {
14924 NeedsCondInvert = true;
Chris Lattnercee56e72009-03-13 05:53:31 +000014925 std::swap(TrueC, FalseC);
Chris Lattnerd1980a52009-03-12 06:52:53 +000014926 }
Eric Christopherfd179292009-08-27 18:07:15 +000014927
Chris Lattnerd1980a52009-03-12 06:52:53 +000014928 // Optimize C ? 8 : 0 -> zext(C) << 3. Likewise for any pow2/0.
Chris Lattnercee56e72009-03-13 05:53:31 +000014929 if (FalseC->getAPIntValue() == 0 &&
14930 TrueC->getAPIntValue().isPowerOf2()) {
Chris Lattnerd1980a52009-03-12 06:52:53 +000014931 if (NeedsCondInvert) // Invert the condition if needed.
14932 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
14933 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000014934
Chris Lattnerd1980a52009-03-12 06:52:53 +000014935 // Zero extend the condition if needed.
14936 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, LHS.getValueType(), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000014937
Chris Lattnercee56e72009-03-13 05:53:31 +000014938 unsigned ShAmt = TrueC->getAPIntValue().logBase2();
Chris Lattnerd1980a52009-03-12 06:52:53 +000014939 return DAG.getNode(ISD::SHL, DL, LHS.getValueType(), Cond,
Owen Anderson825b72b2009-08-11 20:47:22 +000014940 DAG.getConstant(ShAmt, MVT::i8));
Chris Lattnerd1980a52009-03-12 06:52:53 +000014941 }
Eric Christopherfd179292009-08-27 18:07:15 +000014942
Chris Lattner97a29a52009-03-13 05:22:11 +000014943 // Optimize Cond ? cst+1 : cst -> zext(setcc(C)+cst.
Chris Lattnercee56e72009-03-13 05:53:31 +000014944 if (FalseC->getAPIntValue()+1 == TrueC->getAPIntValue()) {
Chris Lattner97a29a52009-03-13 05:22:11 +000014945 if (NeedsCondInvert) // Invert the condition if needed.
14946 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
14947 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000014948
Chris Lattner97a29a52009-03-13 05:22:11 +000014949 // Zero extend the condition if needed.
Chris Lattnercee56e72009-03-13 05:53:31 +000014950 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL,
14951 FalseC->getValueType(0), Cond);
Chris Lattner97a29a52009-03-13 05:22:11 +000014952 return DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
Chris Lattnercee56e72009-03-13 05:53:31 +000014953 SDValue(FalseC, 0));
Chris Lattner97a29a52009-03-13 05:22:11 +000014954 }
Eric Christopherfd179292009-08-27 18:07:15 +000014955
Chris Lattnercee56e72009-03-13 05:53:31 +000014956 // Optimize cases that will turn into an LEA instruction. This requires
14957 // an i32 or i64 and an efficient multiplier (1, 2, 3, 4, 5, 8, 9).
Owen Anderson825b72b2009-08-11 20:47:22 +000014958 if (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i64) {
Chris Lattnercee56e72009-03-13 05:53:31 +000014959 uint64_t Diff = TrueC->getZExtValue()-FalseC->getZExtValue();
Owen Anderson825b72b2009-08-11 20:47:22 +000014960 if (N->getValueType(0) == MVT::i32) Diff = (unsigned)Diff;
Eric Christopherfd179292009-08-27 18:07:15 +000014961
Chris Lattnercee56e72009-03-13 05:53:31 +000014962 bool isFastMultiplier = false;
14963 if (Diff < 10) {
14964 switch ((unsigned char)Diff) {
14965 default: break;
14966 case 1: // result = add base, cond
14967 case 2: // result = lea base( , cond*2)
14968 case 3: // result = lea base(cond, cond*2)
14969 case 4: // result = lea base( , cond*4)
14970 case 5: // result = lea base(cond, cond*4)
14971 case 8: // result = lea base( , cond*8)
14972 case 9: // result = lea base(cond, cond*8)
14973 isFastMultiplier = true;
14974 break;
14975 }
14976 }
Eric Christopherfd179292009-08-27 18:07:15 +000014977
Chris Lattnercee56e72009-03-13 05:53:31 +000014978 if (isFastMultiplier) {
14979 APInt Diff = TrueC->getAPIntValue()-FalseC->getAPIntValue();
14980 if (NeedsCondInvert) // Invert the condition if needed.
14981 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
14982 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000014983
Chris Lattnercee56e72009-03-13 05:53:31 +000014984 // Zero extend the condition if needed.
14985 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, FalseC->getValueType(0),
14986 Cond);
14987 // Scale the condition by the difference.
14988 if (Diff != 1)
14989 Cond = DAG.getNode(ISD::MUL, DL, Cond.getValueType(), Cond,
14990 DAG.getConstant(Diff, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000014991
Chris Lattnercee56e72009-03-13 05:53:31 +000014992 // Add the base if non-zero.
14993 if (FalseC->getAPIntValue() != 0)
14994 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
14995 SDValue(FalseC, 0));
14996 return Cond;
14997 }
Eric Christopherfd179292009-08-27 18:07:15 +000014998 }
Chris Lattnerd1980a52009-03-12 06:52:53 +000014999 }
15000 }
Eric Christopherfd179292009-08-27 18:07:15 +000015001
Evan Cheng56f582d2012-01-04 01:41:39 +000015002 // Canonicalize max and min:
15003 // (x > y) ? x : y -> (x >= y) ? x : y
15004 // (x < y) ? x : y -> (x <= y) ? x : y
15005 // This allows use of COND_S / COND_NS (see TranslateX86CC) which eliminates
15006 // the need for an extra compare
15007 // against zero. e.g.
15008 // (x - y) > 0 : (x - y) ? 0 -> (x - y) >= 0 : (x - y) ? 0
15009 // subl %esi, %edi
15010 // testl %edi, %edi
15011 // movl $0, %eax
15012 // cmovgl %edi, %eax
15013 // =>
15014 // xorl %eax, %eax
15015 // subl %esi, $edi
15016 // cmovsl %eax, %edi
15017 if (N->getOpcode() == ISD::SELECT && Cond.getOpcode() == ISD::SETCC &&
15018 DAG.isEqualTo(LHS, Cond.getOperand(0)) &&
15019 DAG.isEqualTo(RHS, Cond.getOperand(1))) {
15020 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
15021 switch (CC) {
15022 default: break;
15023 case ISD::SETLT:
15024 case ISD::SETGT: {
15025 ISD::CondCode NewCC = (CC == ISD::SETLT) ? ISD::SETLE : ISD::SETGE;
15026 Cond = DAG.getSetCC(Cond.getDebugLoc(), Cond.getValueType(),
15027 Cond.getOperand(0), Cond.getOperand(1), NewCC);
15028 return DAG.getNode(ISD::SELECT, DL, VT, Cond, LHS, RHS);
15029 }
15030 }
15031 }
15032
Benjamin Kramer388fc6a2012-12-15 16:47:44 +000015033 // Match VSELECTs into subs with unsigned saturation.
15034 if (!DCI.isBeforeLegalize() &&
15035 N->getOpcode() == ISD::VSELECT && Cond.getOpcode() == ISD::SETCC &&
15036 // psubus is available in SSE2 and AVX2 for i8 and i16 vectors.
15037 ((Subtarget->hasSSE2() && (VT == MVT::v16i8 || VT == MVT::v8i16)) ||
15038 (Subtarget->hasAVX2() && (VT == MVT::v32i8 || VT == MVT::v16i16)))) {
15039 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
15040
15041 // Check if one of the arms of the VSELECT is a zero vector. If it's on the
15042 // left side invert the predicate to simplify logic below.
15043 SDValue Other;
15044 if (ISD::isBuildVectorAllZeros(LHS.getNode())) {
15045 Other = RHS;
15046 CC = ISD::getSetCCInverse(CC, true);
15047 } else if (ISD::isBuildVectorAllZeros(RHS.getNode())) {
15048 Other = LHS;
15049 }
15050
15051 if (Other.getNode() && Other->getNumOperands() == 2 &&
15052 DAG.isEqualTo(Other->getOperand(0), Cond.getOperand(0))) {
15053 SDValue OpLHS = Other->getOperand(0), OpRHS = Other->getOperand(1);
15054 SDValue CondRHS = Cond->getOperand(1);
15055
15056 // Look for a general sub with unsigned saturation first.
15057 // x >= y ? x-y : 0 --> subus x, y
15058 // x > y ? x-y : 0 --> subus x, y
15059 if ((CC == ISD::SETUGE || CC == ISD::SETUGT) &&
15060 Other->getOpcode() == ISD::SUB && DAG.isEqualTo(OpRHS, CondRHS))
15061 return DAG.getNode(X86ISD::SUBUS, DL, VT, OpLHS, OpRHS);
15062
15063 // If the RHS is a constant we have to reverse the const canonicalization.
15064 // x > C-1 ? x+-C : 0 --> subus x, C
15065 if (CC == ISD::SETUGT && Other->getOpcode() == ISD::ADD &&
15066 isSplatVector(CondRHS.getNode()) && isSplatVector(OpRHS.getNode())) {
15067 APInt A = cast<ConstantSDNode>(OpRHS.getOperand(0))->getAPIntValue();
15068 if (CondRHS.getConstantOperandVal(0) == -A-1) {
15069 SmallVector<SDValue, 32> V(VT.getVectorNumElements(),
15070 DAG.getConstant(-A, VT.getScalarType()));
15071 return DAG.getNode(X86ISD::SUBUS, DL, VT, OpLHS,
15072 DAG.getNode(ISD::BUILD_VECTOR, DL, VT,
15073 V.data(), V.size()));
15074 }
15075 }
15076
15077 // Another special case: If C was a sign bit, the sub has been
15078 // canonicalized into a xor.
15079 // FIXME: Would it be better to use ComputeMaskedBits to determine whether
15080 // it's safe to decanonicalize the xor?
15081 // x s< 0 ? x^C : 0 --> subus x, C
15082 if (CC == ISD::SETLT && Other->getOpcode() == ISD::XOR &&
15083 ISD::isBuildVectorAllZeros(CondRHS.getNode()) &&
15084 isSplatVector(OpRHS.getNode())) {
15085 APInt A = cast<ConstantSDNode>(OpRHS.getOperand(0))->getAPIntValue();
15086 if (A.isSignBit())
15087 return DAG.getNode(X86ISD::SUBUS, DL, VT, OpLHS, OpRHS);
15088 }
15089 }
15090 }
15091
Benjamin Kramer2556c6b2012-12-21 17:46:58 +000015092 // Try to match a min/max vector operation.
15093 if (!DCI.isBeforeLegalize() &&
15094 N->getOpcode() == ISD::VSELECT && Cond.getOpcode() == ISD::SETCC)
15095 if (unsigned Op = matchIntegerMINMAX(Cond, VT, LHS, RHS, DAG, Subtarget))
15096 return DAG.getNode(Op, DL, N->getValueType(0), LHS, RHS);
15097
Nadav Rotemcc616562012-01-15 19:27:55 +000015098 // If we know that this node is legal then we know that it is going to be
15099 // matched by one of the SSE/AVX BLEND instructions. These instructions only
15100 // depend on the highest bit in each word. Try to use SimplifyDemandedBits
15101 // to simplify previous instructions.
15102 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
15103 if (N->getOpcode() == ISD::VSELECT && DCI.isBeforeLegalizeOps() &&
Nadav Rotembdcae382012-06-07 20:53:48 +000015104 !DCI.isBeforeLegalize() && TLI.isOperationLegal(ISD::VSELECT, VT)) {
Nadav Rotemcc616562012-01-15 19:27:55 +000015105 unsigned BitWidth = Cond.getValueType().getScalarType().getSizeInBits();
Nadav Rotembdcae382012-06-07 20:53:48 +000015106
15107 // Don't optimize vector selects that map to mask-registers.
15108 if (BitWidth == 1)
15109 return SDValue();
15110
Nadav Rotemcc616562012-01-15 19:27:55 +000015111 assert(BitWidth >= 8 && BitWidth <= 64 && "Invalid mask size");
15112 APInt DemandedMask = APInt::getHighBitsSet(BitWidth, 1);
15113
15114 APInt KnownZero, KnownOne;
15115 TargetLowering::TargetLoweringOpt TLO(DAG, DCI.isBeforeLegalize(),
15116 DCI.isBeforeLegalizeOps());
15117 if (TLO.ShrinkDemandedConstant(Cond, DemandedMask) ||
15118 TLI.SimplifyDemandedBits(Cond, DemandedMask, KnownZero, KnownOne, TLO))
15119 DCI.CommitTargetLoweringOpt(TLO);
15120 }
15121
Dan Gohman475871a2008-07-27 21:46:04 +000015122 return SDValue();
Chris Lattner83e6c992006-10-04 06:57:07 +000015123}
15124
Michael Liao2a33cec2012-08-10 19:58:13 +000015125// Check whether a boolean test is testing a boolean value generated by
15126// X86ISD::SETCC. If so, return the operand of that SETCC and proper condition
15127// code.
15128//
15129// Simplify the following patterns:
15130// (Op (CMP (SETCC Cond EFLAGS) 1) EQ) or
15131// (Op (CMP (SETCC Cond EFLAGS) 0) NEQ)
15132// to (Op EFLAGS Cond)
15133//
15134// (Op (CMP (SETCC Cond EFLAGS) 0) EQ) or
15135// (Op (CMP (SETCC Cond EFLAGS) 1) NEQ)
15136// to (Op EFLAGS !Cond)
15137//
15138// where Op could be BRCOND or CMOV.
15139//
Michael Liaodbf8b5b2012-08-28 03:34:40 +000015140static SDValue checkBoolTestSetCCCombine(SDValue Cmp, X86::CondCode &CC) {
Michael Liao2a33cec2012-08-10 19:58:13 +000015141 // Quit if not CMP and SUB with its value result used.
15142 if (Cmp.getOpcode() != X86ISD::CMP &&
15143 (Cmp.getOpcode() != X86ISD::SUB || Cmp.getNode()->hasAnyUseOfValue(0)))
15144 return SDValue();
15145
15146 // Quit if not used as a boolean value.
15147 if (CC != X86::COND_E && CC != X86::COND_NE)
15148 return SDValue();
15149
15150 // Check CMP operands. One of them should be 0 or 1 and the other should be
15151 // an SetCC or extended from it.
15152 SDValue Op1 = Cmp.getOperand(0);
15153 SDValue Op2 = Cmp.getOperand(1);
15154
15155 SDValue SetCC;
15156 const ConstantSDNode* C = 0;
15157 bool needOppositeCond = (CC == X86::COND_E);
15158
15159 if ((C = dyn_cast<ConstantSDNode>(Op1)))
15160 SetCC = Op2;
15161 else if ((C = dyn_cast<ConstantSDNode>(Op2)))
15162 SetCC = Op1;
15163 else // Quit if all operands are not constants.
15164 return SDValue();
15165
15166 if (C->getZExtValue() == 1)
15167 needOppositeCond = !needOppositeCond;
15168 else if (C->getZExtValue() != 0)
15169 // Quit if the constant is neither 0 or 1.
15170 return SDValue();
15171
15172 // Skip 'zext' node.
15173 if (SetCC.getOpcode() == ISD::ZERO_EXTEND)
15174 SetCC = SetCC.getOperand(0);
15175
Michael Liao7fdc66b2012-09-10 16:36:16 +000015176 switch (SetCC.getOpcode()) {
15177 case X86ISD::SETCC:
15178 // Set the condition code or opposite one if necessary.
15179 CC = X86::CondCode(SetCC.getConstantOperandVal(0));
15180 if (needOppositeCond)
15181 CC = X86::GetOppositeBranchCondition(CC);
15182 return SetCC.getOperand(1);
15183 case X86ISD::CMOV: {
15184 // Check whether false/true value has canonical one, i.e. 0 or 1.
15185 ConstantSDNode *FVal = dyn_cast<ConstantSDNode>(SetCC.getOperand(0));
15186 ConstantSDNode *TVal = dyn_cast<ConstantSDNode>(SetCC.getOperand(1));
15187 // Quit if true value is not a constant.
15188 if (!TVal)
15189 return SDValue();
15190 // Quit if false value is not a constant.
15191 if (!FVal) {
15192 // A special case for rdrand, where 0 is set if false cond is found.
15193 SDValue Op = SetCC.getOperand(0);
15194 if (Op.getOpcode() != X86ISD::RDRAND)
15195 return SDValue();
15196 }
15197 // Quit if false value is not the constant 0 or 1.
15198 bool FValIsFalse = true;
15199 if (FVal && FVal->getZExtValue() != 0) {
15200 if (FVal->getZExtValue() != 1)
15201 return SDValue();
15202 // If FVal is 1, opposite cond is needed.
15203 needOppositeCond = !needOppositeCond;
15204 FValIsFalse = false;
15205 }
15206 // Quit if TVal is not the constant opposite of FVal.
15207 if (FValIsFalse && TVal->getZExtValue() != 1)
15208 return SDValue();
15209 if (!FValIsFalse && TVal->getZExtValue() != 0)
15210 return SDValue();
15211 CC = X86::CondCode(SetCC.getConstantOperandVal(2));
15212 if (needOppositeCond)
15213 CC = X86::GetOppositeBranchCondition(CC);
15214 return SetCC.getOperand(3);
15215 }
15216 }
Michael Liao2a33cec2012-08-10 19:58:13 +000015217
Michael Liao7fdc66b2012-09-10 16:36:16 +000015218 return SDValue();
Michael Liao2a33cec2012-08-10 19:58:13 +000015219}
15220
Chris Lattnerd1980a52009-03-12 06:52:53 +000015221/// Optimize X86ISD::CMOV [LHS, RHS, CONDCODE (e.g. X86::COND_NE), CONDVAL]
15222static SDValue PerformCMOVCombine(SDNode *N, SelectionDAG &DAG,
Michael Liaodbf8b5b2012-08-28 03:34:40 +000015223 TargetLowering::DAGCombinerInfo &DCI,
15224 const X86Subtarget *Subtarget) {
Chris Lattnerd1980a52009-03-12 06:52:53 +000015225 DebugLoc DL = N->getDebugLoc();
Eric Christopherfd179292009-08-27 18:07:15 +000015226
Chris Lattnerd1980a52009-03-12 06:52:53 +000015227 // If the flag operand isn't dead, don't touch this CMOV.
15228 if (N->getNumValues() == 2 && !SDValue(N, 1).use_empty())
15229 return SDValue();
Eric Christopherfd179292009-08-27 18:07:15 +000015230
Evan Chengb5a55d92011-05-24 01:48:22 +000015231 SDValue FalseOp = N->getOperand(0);
15232 SDValue TrueOp = N->getOperand(1);
15233 X86::CondCode CC = (X86::CondCode)N->getConstantOperandVal(2);
15234 SDValue Cond = N->getOperand(3);
Michael Liao2a33cec2012-08-10 19:58:13 +000015235
Evan Chengb5a55d92011-05-24 01:48:22 +000015236 if (CC == X86::COND_E || CC == X86::COND_NE) {
15237 switch (Cond.getOpcode()) {
15238 default: break;
15239 case X86ISD::BSR:
15240 case X86ISD::BSF:
15241 // If operand of BSR / BSF are proven never zero, then ZF cannot be set.
15242 if (DAG.isKnownNeverZero(Cond.getOperand(0)))
15243 return (CC == X86::COND_E) ? FalseOp : TrueOp;
15244 }
15245 }
15246
Michael Liao2a33cec2012-08-10 19:58:13 +000015247 SDValue Flags;
15248
Michael Liaodbf8b5b2012-08-28 03:34:40 +000015249 Flags = checkBoolTestSetCCCombine(Cond, CC);
Michael Liao9eac20a2012-08-11 23:47:06 +000015250 if (Flags.getNode() &&
15251 // Extra check as FCMOV only supports a subset of X86 cond.
Michael Liao7859f432012-09-06 07:11:22 +000015252 (FalseOp.getValueType() != MVT::f80 || hasFPCMov(CC))) {
Michael Liaodbf8b5b2012-08-28 03:34:40 +000015253 SDValue Ops[] = { FalseOp, TrueOp,
15254 DAG.getConstant(CC, MVT::i8), Flags };
15255 return DAG.getNode(X86ISD::CMOV, DL, N->getVTList(),
15256 Ops, array_lengthof(Ops));
15257 }
15258
Chris Lattnerd1980a52009-03-12 06:52:53 +000015259 // If this is a select between two integer constants, try to do some
15260 // optimizations. Note that the operands are ordered the opposite of SELECT
15261 // operands.
Evan Chengb5a55d92011-05-24 01:48:22 +000015262 if (ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(TrueOp)) {
15263 if (ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(FalseOp)) {
Chris Lattnerd1980a52009-03-12 06:52:53 +000015264 // Canonicalize the TrueC/FalseC values so that TrueC (the true value) is
15265 // larger than FalseC (the false value).
Chris Lattnerd1980a52009-03-12 06:52:53 +000015266 if (TrueC->getAPIntValue().ult(FalseC->getAPIntValue())) {
15267 CC = X86::GetOppositeBranchCondition(CC);
15268 std::swap(TrueC, FalseC);
NAKAMURA Takumie2687452012-10-16 06:28:34 +000015269 std::swap(TrueOp, FalseOp);
Chris Lattnerd1980a52009-03-12 06:52:53 +000015270 }
Eric Christopherfd179292009-08-27 18:07:15 +000015271
Chris Lattnerd1980a52009-03-12 06:52:53 +000015272 // Optimize C ? 8 : 0 -> zext(setcc(C)) << 3. Likewise for any pow2/0.
Chris Lattnercee56e72009-03-13 05:53:31 +000015273 // This is efficient for any integer data type (including i8/i16) and
15274 // shift amount.
Chris Lattnerd1980a52009-03-12 06:52:53 +000015275 if (FalseC->getAPIntValue() == 0 && TrueC->getAPIntValue().isPowerOf2()) {
Owen Anderson825b72b2009-08-11 20:47:22 +000015276 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
15277 DAG.getConstant(CC, MVT::i8), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000015278
Chris Lattnerd1980a52009-03-12 06:52:53 +000015279 // Zero extend the condition if needed.
15280 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, TrueC->getValueType(0), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000015281
Chris Lattnerd1980a52009-03-12 06:52:53 +000015282 unsigned ShAmt = TrueC->getAPIntValue().logBase2();
15283 Cond = DAG.getNode(ISD::SHL, DL, Cond.getValueType(), Cond,
Owen Anderson825b72b2009-08-11 20:47:22 +000015284 DAG.getConstant(ShAmt, MVT::i8));
Chris Lattnerd1980a52009-03-12 06:52:53 +000015285 if (N->getNumValues() == 2) // Dead flag value?
15286 return DCI.CombineTo(N, Cond, SDValue());
15287 return Cond;
15288 }
Eric Christopherfd179292009-08-27 18:07:15 +000015289
Chris Lattnercee56e72009-03-13 05:53:31 +000015290 // Optimize Cond ? cst+1 : cst -> zext(setcc(C)+cst. This is efficient
15291 // for any integer data type, including i8/i16.
Chris Lattner97a29a52009-03-13 05:22:11 +000015292 if (FalseC->getAPIntValue()+1 == TrueC->getAPIntValue()) {
Owen Anderson825b72b2009-08-11 20:47:22 +000015293 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
15294 DAG.getConstant(CC, MVT::i8), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000015295
Chris Lattner97a29a52009-03-13 05:22:11 +000015296 // Zero extend the condition if needed.
Chris Lattnercee56e72009-03-13 05:53:31 +000015297 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL,
15298 FalseC->getValueType(0), Cond);
Chris Lattner97a29a52009-03-13 05:22:11 +000015299 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
15300 SDValue(FalseC, 0));
Eric Christopherfd179292009-08-27 18:07:15 +000015301
Chris Lattner97a29a52009-03-13 05:22:11 +000015302 if (N->getNumValues() == 2) // Dead flag value?
15303 return DCI.CombineTo(N, Cond, SDValue());
15304 return Cond;
15305 }
Eric Christopherfd179292009-08-27 18:07:15 +000015306
Chris Lattnercee56e72009-03-13 05:53:31 +000015307 // Optimize cases that will turn into an LEA instruction. This requires
15308 // an i32 or i64 and an efficient multiplier (1, 2, 3, 4, 5, 8, 9).
Owen Anderson825b72b2009-08-11 20:47:22 +000015309 if (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i64) {
Chris Lattnercee56e72009-03-13 05:53:31 +000015310 uint64_t Diff = TrueC->getZExtValue()-FalseC->getZExtValue();
Owen Anderson825b72b2009-08-11 20:47:22 +000015311 if (N->getValueType(0) == MVT::i32) Diff = (unsigned)Diff;
Eric Christopherfd179292009-08-27 18:07:15 +000015312
Chris Lattnercee56e72009-03-13 05:53:31 +000015313 bool isFastMultiplier = false;
15314 if (Diff < 10) {
15315 switch ((unsigned char)Diff) {
15316 default: break;
15317 case 1: // result = add base, cond
15318 case 2: // result = lea base( , cond*2)
15319 case 3: // result = lea base(cond, cond*2)
15320 case 4: // result = lea base( , cond*4)
15321 case 5: // result = lea base(cond, cond*4)
15322 case 8: // result = lea base( , cond*8)
15323 case 9: // result = lea base(cond, cond*8)
15324 isFastMultiplier = true;
15325 break;
15326 }
15327 }
Eric Christopherfd179292009-08-27 18:07:15 +000015328
Chris Lattnercee56e72009-03-13 05:53:31 +000015329 if (isFastMultiplier) {
15330 APInt Diff = TrueC->getAPIntValue()-FalseC->getAPIntValue();
Owen Anderson825b72b2009-08-11 20:47:22 +000015331 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
15332 DAG.getConstant(CC, MVT::i8), Cond);
Chris Lattnercee56e72009-03-13 05:53:31 +000015333 // Zero extend the condition if needed.
15334 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, FalseC->getValueType(0),
15335 Cond);
15336 // Scale the condition by the difference.
15337 if (Diff != 1)
15338 Cond = DAG.getNode(ISD::MUL, DL, Cond.getValueType(), Cond,
15339 DAG.getConstant(Diff, Cond.getValueType()));
15340
15341 // Add the base if non-zero.
15342 if (FalseC->getAPIntValue() != 0)
15343 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
15344 SDValue(FalseC, 0));
15345 if (N->getNumValues() == 2) // Dead flag value?
15346 return DCI.CombineTo(N, Cond, SDValue());
15347 return Cond;
15348 }
Eric Christopherfd179292009-08-27 18:07:15 +000015349 }
Chris Lattnerd1980a52009-03-12 06:52:53 +000015350 }
15351 }
NAKAMURA Takumie2687452012-10-16 06:28:34 +000015352
15353 // Handle these cases:
15354 // (select (x != c), e, c) -> select (x != c), e, x),
15355 // (select (x == c), c, e) -> select (x == c), x, e)
15356 // where the c is an integer constant, and the "select" is the combination
15357 // of CMOV and CMP.
15358 //
15359 // The rationale for this change is that the conditional-move from a constant
15360 // needs two instructions, however, conditional-move from a register needs
15361 // only one instruction.
15362 //
15363 // CAVEAT: By replacing a constant with a symbolic value, it may obscure
15364 // some instruction-combining opportunities. This opt needs to be
15365 // postponed as late as possible.
15366 //
15367 if (!DCI.isBeforeLegalize() && !DCI.isBeforeLegalizeOps()) {
15368 // the DCI.xxxx conditions are provided to postpone the optimization as
15369 // late as possible.
15370
15371 ConstantSDNode *CmpAgainst = 0;
15372 if ((Cond.getOpcode() == X86ISD::CMP || Cond.getOpcode() == X86ISD::SUB) &&
15373 (CmpAgainst = dyn_cast<ConstantSDNode>(Cond.getOperand(1))) &&
15374 dyn_cast<ConstantSDNode>(Cond.getOperand(0)) == 0) {
15375
15376 if (CC == X86::COND_NE &&
15377 CmpAgainst == dyn_cast<ConstantSDNode>(FalseOp)) {
15378 CC = X86::GetOppositeBranchCondition(CC);
15379 std::swap(TrueOp, FalseOp);
15380 }
15381
15382 if (CC == X86::COND_E &&
15383 CmpAgainst == dyn_cast<ConstantSDNode>(TrueOp)) {
15384 SDValue Ops[] = { FalseOp, Cond.getOperand(0),
15385 DAG.getConstant(CC, MVT::i8), Cond };
15386 return DAG.getNode(X86ISD::CMOV, DL, N->getVTList (), Ops,
15387 array_lengthof(Ops));
15388 }
15389 }
15390 }
15391
Chris Lattnerd1980a52009-03-12 06:52:53 +000015392 return SDValue();
15393}
15394
Evan Cheng0b0cd912009-03-28 05:57:29 +000015395/// PerformMulCombine - Optimize a single multiply with constant into two
15396/// in order to implement it with two cheaper instructions, e.g.
15397/// LEA + SHL, LEA + LEA.
15398static SDValue PerformMulCombine(SDNode *N, SelectionDAG &DAG,
15399 TargetLowering::DAGCombinerInfo &DCI) {
Evan Cheng0b0cd912009-03-28 05:57:29 +000015400 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
15401 return SDValue();
15402
Owen Andersone50ed302009-08-10 22:56:29 +000015403 EVT VT = N->getValueType(0);
Owen Anderson825b72b2009-08-11 20:47:22 +000015404 if (VT != MVT::i64)
Evan Cheng0b0cd912009-03-28 05:57:29 +000015405 return SDValue();
15406
15407 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1));
15408 if (!C)
15409 return SDValue();
15410 uint64_t MulAmt = C->getZExtValue();
15411 if (isPowerOf2_64(MulAmt) || MulAmt == 3 || MulAmt == 5 || MulAmt == 9)
15412 return SDValue();
15413
15414 uint64_t MulAmt1 = 0;
15415 uint64_t MulAmt2 = 0;
15416 if ((MulAmt % 9) == 0) {
15417 MulAmt1 = 9;
15418 MulAmt2 = MulAmt / 9;
15419 } else if ((MulAmt % 5) == 0) {
15420 MulAmt1 = 5;
15421 MulAmt2 = MulAmt / 5;
15422 } else if ((MulAmt % 3) == 0) {
15423 MulAmt1 = 3;
15424 MulAmt2 = MulAmt / 3;
15425 }
15426 if (MulAmt2 &&
15427 (isPowerOf2_64(MulAmt2) || MulAmt2 == 3 || MulAmt2 == 5 || MulAmt2 == 9)){
15428 DebugLoc DL = N->getDebugLoc();
15429
15430 if (isPowerOf2_64(MulAmt2) &&
15431 !(N->hasOneUse() && N->use_begin()->getOpcode() == ISD::ADD))
15432 // If second multiplifer is pow2, issue it first. We want the multiply by
15433 // 3, 5, or 9 to be folded into the addressing mode unless the lone use
15434 // is an add.
15435 std::swap(MulAmt1, MulAmt2);
15436
15437 SDValue NewMul;
Eric Christopherfd179292009-08-27 18:07:15 +000015438 if (isPowerOf2_64(MulAmt1))
Evan Cheng0b0cd912009-03-28 05:57:29 +000015439 NewMul = DAG.getNode(ISD::SHL, DL, VT, N->getOperand(0),
Owen Anderson825b72b2009-08-11 20:47:22 +000015440 DAG.getConstant(Log2_64(MulAmt1), MVT::i8));
Evan Cheng0b0cd912009-03-28 05:57:29 +000015441 else
Evan Cheng73f24c92009-03-30 21:36:47 +000015442 NewMul = DAG.getNode(X86ISD::MUL_IMM, DL, VT, N->getOperand(0),
Evan Cheng0b0cd912009-03-28 05:57:29 +000015443 DAG.getConstant(MulAmt1, VT));
15444
Eric Christopherfd179292009-08-27 18:07:15 +000015445 if (isPowerOf2_64(MulAmt2))
Evan Cheng0b0cd912009-03-28 05:57:29 +000015446 NewMul = DAG.getNode(ISD::SHL, DL, VT, NewMul,
Owen Anderson825b72b2009-08-11 20:47:22 +000015447 DAG.getConstant(Log2_64(MulAmt2), MVT::i8));
Eric Christopherfd179292009-08-27 18:07:15 +000015448 else
Evan Cheng73f24c92009-03-30 21:36:47 +000015449 NewMul = DAG.getNode(X86ISD::MUL_IMM, DL, VT, NewMul,
Evan Cheng0b0cd912009-03-28 05:57:29 +000015450 DAG.getConstant(MulAmt2, VT));
15451
15452 // Do not add new nodes to DAG combiner worklist.
15453 DCI.CombineTo(N, NewMul, false);
15454 }
15455 return SDValue();
15456}
15457
Evan Chengad9c0a32009-12-15 00:53:42 +000015458static SDValue PerformSHLCombine(SDNode *N, SelectionDAG &DAG) {
15459 SDValue N0 = N->getOperand(0);
15460 SDValue N1 = N->getOperand(1);
15461 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
15462 EVT VT = N0.getValueType();
15463
15464 // fold (shl (and (setcc_c), c1), c2) -> (and setcc_c, (c1 << c2))
15465 // since the result of setcc_c is all zero's or all ones.
Nadav Rotemfb0dfbb2011-10-30 13:24:22 +000015466 if (VT.isInteger() && !VT.isVector() &&
15467 N1C && N0.getOpcode() == ISD::AND &&
Evan Chengad9c0a32009-12-15 00:53:42 +000015468 N0.getOperand(1).getOpcode() == ISD::Constant) {
15469 SDValue N00 = N0.getOperand(0);
15470 if (N00.getOpcode() == X86ISD::SETCC_CARRY ||
15471 ((N00.getOpcode() == ISD::ANY_EXTEND ||
15472 N00.getOpcode() == ISD::ZERO_EXTEND) &&
15473 N00.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY)) {
15474 APInt Mask = cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
15475 APInt ShAmt = N1C->getAPIntValue();
15476 Mask = Mask.shl(ShAmt);
15477 if (Mask != 0)
15478 return DAG.getNode(ISD::AND, N->getDebugLoc(), VT,
15479 N00, DAG.getConstant(Mask, VT));
15480 }
15481 }
15482
Nadav Rotemfb0dfbb2011-10-30 13:24:22 +000015483 // Hardware support for vector shifts is sparse which makes us scalarize the
15484 // vector operations in many cases. Also, on sandybridge ADD is faster than
15485 // shl.
15486 // (shl V, 1) -> add V,V
15487 if (isSplatVector(N1.getNode())) {
15488 assert(N0.getValueType().isVector() && "Invalid vector shift type");
15489 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1->getOperand(0));
15490 // We shift all of the values by one. In many cases we do not have
15491 // hardware support for this operation. This is better expressed as an ADD
15492 // of two values.
15493 if (N1C && (1 == N1C->getZExtValue())) {
15494 return DAG.getNode(ISD::ADD, N->getDebugLoc(), VT, N0, N0);
15495 }
15496 }
15497
Evan Chengad9c0a32009-12-15 00:53:42 +000015498 return SDValue();
15499}
Evan Cheng0b0cd912009-03-28 05:57:29 +000015500
Nate Begeman740ab032009-01-26 00:52:55 +000015501/// PerformShiftCombine - Transforms vector shift nodes to use vector shifts
15502/// when possible.
15503static SDValue PerformShiftCombine(SDNode* N, SelectionDAG &DAG,
Mon P Wang845b1892012-02-01 22:15:20 +000015504 TargetLowering::DAGCombinerInfo &DCI,
Nate Begeman740ab032009-01-26 00:52:55 +000015505 const X86Subtarget *Subtarget) {
Evan Chengad9c0a32009-12-15 00:53:42 +000015506 EVT VT = N->getValueType(0);
Nadav Rotemfb0dfbb2011-10-30 13:24:22 +000015507 if (N->getOpcode() == ISD::SHL) {
15508 SDValue V = PerformSHLCombine(N, DAG);
15509 if (V.getNode()) return V;
15510 }
Evan Chengad9c0a32009-12-15 00:53:42 +000015511
Nate Begeman740ab032009-01-26 00:52:55 +000015512 // On X86 with SSE2 support, we can transform this to a vector shift if
15513 // all elements are shifted by the same amount. We can't do this in legalize
15514 // because the a constant vector is typically transformed to a constant pool
15515 // so we have no knowledge of the shift amount.
Craig Topper1accb7e2012-01-10 06:54:16 +000015516 if (!Subtarget->hasSSE2())
Nate Begemanc2fd67f2009-01-26 03:15:31 +000015517 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +000015518
Craig Topper7be5dfd2011-11-12 09:58:49 +000015519 if (VT != MVT::v2i64 && VT != MVT::v4i32 && VT != MVT::v8i16 &&
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000015520 (!Subtarget->hasInt256() ||
Craig Topper7be5dfd2011-11-12 09:58:49 +000015521 (VT != MVT::v4i64 && VT != MVT::v8i32 && VT != MVT::v16i16)))
Nate Begemanc2fd67f2009-01-26 03:15:31 +000015522 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +000015523
Mon P Wang3becd092009-01-28 08:12:05 +000015524 SDValue ShAmtOp = N->getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +000015525 EVT EltVT = VT.getVectorElementType();
Chris Lattner47b4ce82009-03-11 05:48:52 +000015526 DebugLoc DL = N->getDebugLoc();
Mon P Wangefa42202009-09-03 19:56:25 +000015527 SDValue BaseShAmt = SDValue();
Mon P Wang3becd092009-01-28 08:12:05 +000015528 if (ShAmtOp.getOpcode() == ISD::BUILD_VECTOR) {
15529 unsigned NumElts = VT.getVectorNumElements();
15530 unsigned i = 0;
15531 for (; i != NumElts; ++i) {
15532 SDValue Arg = ShAmtOp.getOperand(i);
15533 if (Arg.getOpcode() == ISD::UNDEF) continue;
15534 BaseShAmt = Arg;
15535 break;
15536 }
Craig Topper37c26772012-01-17 04:44:50 +000015537 // Handle the case where the build_vector is all undef
15538 // FIXME: Should DAG allow this?
15539 if (i == NumElts)
15540 return SDValue();
15541
Mon P Wang3becd092009-01-28 08:12:05 +000015542 for (; i != NumElts; ++i) {
15543 SDValue Arg = ShAmtOp.getOperand(i);
15544 if (Arg.getOpcode() == ISD::UNDEF) continue;
15545 if (Arg != BaseShAmt) {
15546 return SDValue();
15547 }
15548 }
15549 } else if (ShAmtOp.getOpcode() == ISD::VECTOR_SHUFFLE &&
Nate Begeman9008ca62009-04-27 18:41:29 +000015550 cast<ShuffleVectorSDNode>(ShAmtOp)->isSplat()) {
Mon P Wangefa42202009-09-03 19:56:25 +000015551 SDValue InVec = ShAmtOp.getOperand(0);
15552 if (InVec.getOpcode() == ISD::BUILD_VECTOR) {
15553 unsigned NumElts = InVec.getValueType().getVectorNumElements();
15554 unsigned i = 0;
15555 for (; i != NumElts; ++i) {
15556 SDValue Arg = InVec.getOperand(i);
15557 if (Arg.getOpcode() == ISD::UNDEF) continue;
15558 BaseShAmt = Arg;
15559 break;
15560 }
15561 } else if (InVec.getOpcode() == ISD::INSERT_VECTOR_ELT) {
15562 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(InVec.getOperand(2))) {
Evan Chengae3ecf92010-02-16 21:09:44 +000015563 unsigned SplatIdx= cast<ShuffleVectorSDNode>(ShAmtOp)->getSplatIndex();
Mon P Wangefa42202009-09-03 19:56:25 +000015564 if (C->getZExtValue() == SplatIdx)
15565 BaseShAmt = InVec.getOperand(1);
15566 }
15567 }
Mon P Wang845b1892012-02-01 22:15:20 +000015568 if (BaseShAmt.getNode() == 0) {
15569 // Don't create instructions with illegal types after legalize
15570 // types has run.
15571 if (!DAG.getTargetLoweringInfo().isTypeLegal(EltVT) &&
15572 !DCI.isBeforeLegalize())
15573 return SDValue();
15574
Mon P Wangefa42202009-09-03 19:56:25 +000015575 BaseShAmt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, EltVT, ShAmtOp,
15576 DAG.getIntPtrConstant(0));
Mon P Wang845b1892012-02-01 22:15:20 +000015577 }
Mon P Wang3becd092009-01-28 08:12:05 +000015578 } else
Nate Begemanc2fd67f2009-01-26 03:15:31 +000015579 return SDValue();
Nate Begeman740ab032009-01-26 00:52:55 +000015580
Mon P Wangefa42202009-09-03 19:56:25 +000015581 // The shift amount is an i32.
Owen Anderson825b72b2009-08-11 20:47:22 +000015582 if (EltVT.bitsGT(MVT::i32))
15583 BaseShAmt = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, BaseShAmt);
15584 else if (EltVT.bitsLT(MVT::i32))
Mon P Wangefa42202009-09-03 19:56:25 +000015585 BaseShAmt = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::i32, BaseShAmt);
Nate Begeman740ab032009-01-26 00:52:55 +000015586
Nate Begemanc2fd67f2009-01-26 03:15:31 +000015587 // The shift amount is identical so we can do a vector shift.
15588 SDValue ValOp = N->getOperand(0);
15589 switch (N->getOpcode()) {
15590 default:
Torok Edwinc23197a2009-07-14 16:55:14 +000015591 llvm_unreachable("Unknown shift opcode!");
Nate Begemanc2fd67f2009-01-26 03:15:31 +000015592 case ISD::SHL:
Craig Toppered2e13d2012-01-22 19:15:14 +000015593 switch (VT.getSimpleVT().SimpleTy) {
15594 default: return SDValue();
15595 case MVT::v2i64:
15596 case MVT::v4i32:
15597 case MVT::v8i16:
15598 case MVT::v4i64:
15599 case MVT::v8i32:
15600 case MVT::v16i16:
15601 return getTargetVShiftNode(X86ISD::VSHLI, DL, VT, ValOp, BaseShAmt, DAG);
15602 }
Nate Begemanc2fd67f2009-01-26 03:15:31 +000015603 case ISD::SRA:
Craig Toppered2e13d2012-01-22 19:15:14 +000015604 switch (VT.getSimpleVT().SimpleTy) {
15605 default: return SDValue();
15606 case MVT::v4i32:
15607 case MVT::v8i16:
15608 case MVT::v8i32:
15609 case MVT::v16i16:
15610 return getTargetVShiftNode(X86ISD::VSRAI, DL, VT, ValOp, BaseShAmt, DAG);
15611 }
Nate Begemanc2fd67f2009-01-26 03:15:31 +000015612 case ISD::SRL:
Craig Toppered2e13d2012-01-22 19:15:14 +000015613 switch (VT.getSimpleVT().SimpleTy) {
15614 default: return SDValue();
15615 case MVT::v2i64:
15616 case MVT::v4i32:
15617 case MVT::v8i16:
15618 case MVT::v4i64:
15619 case MVT::v8i32:
15620 case MVT::v16i16:
15621 return getTargetVShiftNode(X86ISD::VSRLI, DL, VT, ValOp, BaseShAmt, DAG);
15622 }
Nate Begeman740ab032009-01-26 00:52:55 +000015623 }
Nate Begeman740ab032009-01-26 00:52:55 +000015624}
15625
Stuart Hastings865f0932011-06-03 23:53:54 +000015626// CMPEQCombine - Recognize the distinctive (AND (setcc ...) (setcc ..))
15627// where both setccs reference the same FP CMP, and rewrite for CMPEQSS
15628// and friends. Likewise for OR -> CMPNEQSS.
15629static SDValue CMPEQCombine(SDNode *N, SelectionDAG &DAG,
15630 TargetLowering::DAGCombinerInfo &DCI,
15631 const X86Subtarget *Subtarget) {
15632 unsigned opcode;
15633
15634 // SSE1 supports CMP{eq|ne}SS, and SSE2 added CMP{eq|ne}SD, but
15635 // we're requiring SSE2 for both.
Craig Topper1accb7e2012-01-10 06:54:16 +000015636 if (Subtarget->hasSSE2() && isAndOrOfSetCCs(SDValue(N, 0U), opcode)) {
Stuart Hastings865f0932011-06-03 23:53:54 +000015637 SDValue N0 = N->getOperand(0);
15638 SDValue N1 = N->getOperand(1);
15639 SDValue CMP0 = N0->getOperand(1);
15640 SDValue CMP1 = N1->getOperand(1);
15641 DebugLoc DL = N->getDebugLoc();
15642
15643 // The SETCCs should both refer to the same CMP.
15644 if (CMP0.getOpcode() != X86ISD::CMP || CMP0 != CMP1)
15645 return SDValue();
15646
15647 SDValue CMP00 = CMP0->getOperand(0);
15648 SDValue CMP01 = CMP0->getOperand(1);
15649 EVT VT = CMP00.getValueType();
15650
15651 if (VT == MVT::f32 || VT == MVT::f64) {
15652 bool ExpectingFlags = false;
15653 // Check for any users that want flags:
15654 for (SDNode::use_iterator UI = N->use_begin(),
15655 UE = N->use_end();
15656 !ExpectingFlags && UI != UE; ++UI)
15657 switch (UI->getOpcode()) {
15658 default:
15659 case ISD::BR_CC:
15660 case ISD::BRCOND:
15661 case ISD::SELECT:
15662 ExpectingFlags = true;
15663 break;
15664 case ISD::CopyToReg:
15665 case ISD::SIGN_EXTEND:
15666 case ISD::ZERO_EXTEND:
15667 case ISD::ANY_EXTEND:
15668 break;
15669 }
15670
15671 if (!ExpectingFlags) {
15672 enum X86::CondCode cc0 = (enum X86::CondCode)N0.getConstantOperandVal(0);
15673 enum X86::CondCode cc1 = (enum X86::CondCode)N1.getConstantOperandVal(0);
15674
15675 if (cc1 == X86::COND_E || cc1 == X86::COND_NE) {
15676 X86::CondCode tmp = cc0;
15677 cc0 = cc1;
15678 cc1 = tmp;
15679 }
15680
15681 if ((cc0 == X86::COND_E && cc1 == X86::COND_NP) ||
15682 (cc0 == X86::COND_NE && cc1 == X86::COND_P)) {
15683 bool is64BitFP = (CMP00.getValueType() == MVT::f64);
15684 X86ISD::NodeType NTOperator = is64BitFP ?
15685 X86ISD::FSETCCsd : X86ISD::FSETCCss;
15686 // FIXME: need symbolic constants for these magic numbers.
15687 // See X86ATTInstPrinter.cpp:printSSECC().
15688 unsigned x86cc = (cc0 == X86::COND_E) ? 0 : 4;
15689 SDValue OnesOrZeroesF = DAG.getNode(NTOperator, DL, MVT::f32, CMP00, CMP01,
15690 DAG.getConstant(x86cc, MVT::i8));
15691 SDValue OnesOrZeroesI = DAG.getNode(ISD::BITCAST, DL, MVT::i32,
15692 OnesOrZeroesF);
15693 SDValue ANDed = DAG.getNode(ISD::AND, DL, MVT::i32, OnesOrZeroesI,
15694 DAG.getConstant(1, MVT::i32));
15695 SDValue OneBitOfTruth = DAG.getNode(ISD::TRUNCATE, DL, MVT::i8, ANDed);
15696 return OneBitOfTruth;
15697 }
15698 }
15699 }
15700 }
15701 return SDValue();
15702}
15703
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +000015704/// CanFoldXORWithAllOnes - Test whether the XOR operand is a AllOnes vector
15705/// so it can be folded inside ANDNP.
15706static bool CanFoldXORWithAllOnes(const SDNode *N) {
15707 EVT VT = N->getValueType(0);
15708
15709 // Match direct AllOnes for 128 and 256-bit vectors
15710 if (ISD::isBuildVectorAllOnes(N))
15711 return true;
15712
15713 // Look through a bit convert.
15714 if (N->getOpcode() == ISD::BITCAST)
15715 N = N->getOperand(0).getNode();
15716
15717 // Sometimes the operand may come from a insert_subvector building a 256-bit
15718 // allones vector
Craig Topper7a9a28b2012-08-12 02:23:29 +000015719 if (VT.is256BitVector() &&
Bill Wendling456a9252011-08-04 00:32:58 +000015720 N->getOpcode() == ISD::INSERT_SUBVECTOR) {
15721 SDValue V1 = N->getOperand(0);
15722 SDValue V2 = N->getOperand(1);
15723
15724 if (V1.getOpcode() == ISD::INSERT_SUBVECTOR &&
15725 V1.getOperand(0).getOpcode() == ISD::UNDEF &&
15726 ISD::isBuildVectorAllOnes(V1.getOperand(1).getNode()) &&
15727 ISD::isBuildVectorAllOnes(V2.getNode()))
15728 return true;
15729 }
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +000015730
15731 return false;
15732}
15733
Nadav Rotemd6fb53a2012-12-27 08:15:45 +000015734// On AVX/AVX2 the type v8i1 is legalized to v8i16, which is an XMM sized
15735// register. In most cases we actually compare or select YMM-sized registers
15736// and mixing the two types creates horrible code. This method optimizes
15737// some of the transition sequences.
15738static SDValue WidenMaskArithmetic(SDNode *N, SelectionDAG &DAG,
15739 TargetLowering::DAGCombinerInfo &DCI,
15740 const X86Subtarget *Subtarget) {
15741 EVT VT = N->getValueType(0);
15742 if (VT.getSizeInBits() != 256)
15743 return SDValue();
15744
15745 assert((N->getOpcode() == ISD::ANY_EXTEND ||
15746 N->getOpcode() == ISD::ZERO_EXTEND ||
15747 N->getOpcode() == ISD::SIGN_EXTEND) && "Invalid Node");
15748
15749 SDValue Narrow = N->getOperand(0);
15750 EVT NarrowVT = Narrow->getValueType(0);
15751 if (NarrowVT.getSizeInBits() != 128)
15752 return SDValue();
15753
15754 if (Narrow->getOpcode() != ISD::XOR &&
15755 Narrow->getOpcode() != ISD::AND &&
15756 Narrow->getOpcode() != ISD::OR)
15757 return SDValue();
15758
15759 SDValue N0 = Narrow->getOperand(0);
15760 SDValue N1 = Narrow->getOperand(1);
15761 DebugLoc DL = Narrow->getDebugLoc();
15762
15763 // The Left side has to be a trunc.
15764 if (N0.getOpcode() != ISD::TRUNCATE)
15765 return SDValue();
15766
15767 // The type of the truncated inputs.
15768 EVT WideVT = N0->getOperand(0)->getValueType(0);
15769 if (WideVT != VT)
15770 return SDValue();
15771
15772 // The right side has to be a 'trunc' or a constant vector.
15773 bool RHSTrunc = N1.getOpcode() == ISD::TRUNCATE;
15774 bool RHSConst = (isSplatVector(N1.getNode()) &&
15775 isa<ConstantSDNode>(N1->getOperand(0)));
15776 if (!RHSTrunc && !RHSConst)
15777 return SDValue();
15778
15779 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
15780
15781 if (!TLI.isOperationLegalOrPromote(Narrow->getOpcode(), WideVT))
15782 return SDValue();
15783
15784 // Set N0 and N1 to hold the inputs to the new wide operation.
15785 N0 = N0->getOperand(0);
15786 if (RHSConst) {
15787 N1 = DAG.getNode(ISD::ZERO_EXTEND, DL, WideVT.getScalarType(),
15788 N1->getOperand(0));
15789 SmallVector<SDValue, 8> C(WideVT.getVectorNumElements(), N1);
15790 N1 = DAG.getNode(ISD::BUILD_VECTOR, DL, WideVT, &C[0], C.size());
15791 } else if (RHSTrunc) {
15792 N1 = N1->getOperand(0);
15793 }
15794
15795 // Generate the wide operation.
15796 SDValue Op = DAG.getNode(N->getOpcode(), DL, WideVT, N0, N1);
15797 unsigned Opcode = N->getOpcode();
15798 switch (Opcode) {
15799 case ISD::ANY_EXTEND:
15800 return Op;
15801 case ISD::ZERO_EXTEND: {
15802 unsigned InBits = NarrowVT.getScalarType().getSizeInBits();
15803 APInt Mask = APInt::getAllOnesValue(InBits);
15804 Mask = Mask.zext(VT.getScalarType().getSizeInBits());
15805 return DAG.getNode(ISD::AND, DL, VT,
15806 Op, DAG.getConstant(Mask, VT));
15807 }
15808 case ISD::SIGN_EXTEND:
15809 return DAG.getNode(ISD::SIGN_EXTEND_INREG, DL, VT,
15810 Op, DAG.getValueType(NarrowVT));
15811 default:
15812 llvm_unreachable("Unexpected opcode");
15813 }
15814}
15815
Nate Begemanb65c1752010-12-17 22:55:37 +000015816static SDValue PerformAndCombine(SDNode *N, SelectionDAG &DAG,
15817 TargetLowering::DAGCombinerInfo &DCI,
15818 const X86Subtarget *Subtarget) {
Nadav Rotemd6fb53a2012-12-27 08:15:45 +000015819 EVT VT = N->getValueType(0);
Nate Begemanb65c1752010-12-17 22:55:37 +000015820 if (DCI.isBeforeLegalizeOps())
15821 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000015822
Stuart Hastings865f0932011-06-03 23:53:54 +000015823 SDValue R = CMPEQCombine(N, DAG, DCI, Subtarget);
15824 if (R.getNode())
15825 return R;
15826
Craig Topperb926afc2012-12-17 05:12:30 +000015827 // Create BLSI, and BLSR instructions
Craig Topperb4c94572011-10-21 06:55:01 +000015828 // BLSI is X & (-X)
15829 // BLSR is X & (X-1)
Craig Topper54a11172011-10-14 07:06:56 +000015830 if (Subtarget->hasBMI() && (VT == MVT::i32 || VT == MVT::i64)) {
15831 SDValue N0 = N->getOperand(0);
15832 SDValue N1 = N->getOperand(1);
15833 DebugLoc DL = N->getDebugLoc();
15834
Craig Topperb4c94572011-10-21 06:55:01 +000015835 // Check LHS for neg
15836 if (N0.getOpcode() == ISD::SUB && N0.getOperand(1) == N1 &&
15837 isZero(N0.getOperand(0)))
15838 return DAG.getNode(X86ISD::BLSI, DL, VT, N1);
15839
15840 // Check RHS for neg
15841 if (N1.getOpcode() == ISD::SUB && N1.getOperand(1) == N0 &&
15842 isZero(N1.getOperand(0)))
15843 return DAG.getNode(X86ISD::BLSI, DL, VT, N0);
15844
15845 // Check LHS for X-1
15846 if (N0.getOpcode() == ISD::ADD && N0.getOperand(0) == N1 &&
15847 isAllOnes(N0.getOperand(1)))
15848 return DAG.getNode(X86ISD::BLSR, DL, VT, N1);
15849
15850 // Check RHS for X-1
15851 if (N1.getOpcode() == ISD::ADD && N1.getOperand(0) == N0 &&
15852 isAllOnes(N1.getOperand(1)))
15853 return DAG.getNode(X86ISD::BLSR, DL, VT, N0);
15854
Craig Topper54a11172011-10-14 07:06:56 +000015855 return SDValue();
15856 }
15857
Bruno Cardoso Lopes466b0222011-07-13 21:36:51 +000015858 // Want to form ANDNP nodes:
15859 // 1) In the hopes of then easily combining them with OR and AND nodes
15860 // to form PBLEND/PSIGN.
15861 // 2) To match ANDN packed intrinsics
Bruno Cardoso Lopes466b0222011-07-13 21:36:51 +000015862 if (VT != MVT::v2i64 && VT != MVT::v4i64)
Nate Begemanb65c1752010-12-17 22:55:37 +000015863 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000015864
Nate Begemanb65c1752010-12-17 22:55:37 +000015865 SDValue N0 = N->getOperand(0);
15866 SDValue N1 = N->getOperand(1);
15867 DebugLoc DL = N->getDebugLoc();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000015868
Nate Begemanb65c1752010-12-17 22:55:37 +000015869 // Check LHS for vnot
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000015870 if (N0.getOpcode() == ISD::XOR &&
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +000015871 //ISD::isBuildVectorAllOnes(N0.getOperand(1).getNode()))
15872 CanFoldXORWithAllOnes(N0.getOperand(1).getNode()))
Bruno Cardoso Lopesc1af4772011-07-13 21:36:47 +000015873 return DAG.getNode(X86ISD::ANDNP, DL, VT, N0.getOperand(0), N1);
Nate Begemanb65c1752010-12-17 22:55:37 +000015874
15875 // Check RHS for vnot
15876 if (N1.getOpcode() == ISD::XOR &&
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +000015877 //ISD::isBuildVectorAllOnes(N1.getOperand(1).getNode()))
15878 CanFoldXORWithAllOnes(N1.getOperand(1).getNode()))
Bruno Cardoso Lopesc1af4772011-07-13 21:36:47 +000015879 return DAG.getNode(X86ISD::ANDNP, DL, VT, N1.getOperand(0), N0);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000015880
Nate Begemanb65c1752010-12-17 22:55:37 +000015881 return SDValue();
15882}
15883
Evan Cheng760d1942010-01-04 21:22:48 +000015884static SDValue PerformOrCombine(SDNode *N, SelectionDAG &DAG,
Evan Cheng8b1190a2010-04-28 01:18:01 +000015885 TargetLowering::DAGCombinerInfo &DCI,
Evan Cheng760d1942010-01-04 21:22:48 +000015886 const X86Subtarget *Subtarget) {
Nadav Rotemd6fb53a2012-12-27 08:15:45 +000015887 EVT VT = N->getValueType(0);
Evan Cheng39cfeec2010-04-28 02:25:18 +000015888 if (DCI.isBeforeLegalizeOps())
Evan Cheng8b1190a2010-04-28 01:18:01 +000015889 return SDValue();
15890
Stuart Hastings865f0932011-06-03 23:53:54 +000015891 SDValue R = CMPEQCombine(N, DAG, DCI, Subtarget);
15892 if (R.getNode())
15893 return R;
15894
Evan Cheng760d1942010-01-04 21:22:48 +000015895 SDValue N0 = N->getOperand(0);
15896 SDValue N1 = N->getOperand(1);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000015897
Nate Begemanb65c1752010-12-17 22:55:37 +000015898 // look for psign/blend
Craig Topper1666cb62011-11-19 07:07:26 +000015899 if (VT == MVT::v2i64 || VT == MVT::v4i64) {
Craig Topperd0a31172012-01-10 06:37:29 +000015900 if (!Subtarget->hasSSSE3() ||
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000015901 (VT == MVT::v4i64 && !Subtarget->hasInt256()))
Craig Topper1666cb62011-11-19 07:07:26 +000015902 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000015903
Craig Topper1666cb62011-11-19 07:07:26 +000015904 // Canonicalize pandn to RHS
15905 if (N0.getOpcode() == X86ISD::ANDNP)
15906 std::swap(N0, N1);
Lang Hames9ffaa6a2012-01-10 22:53:20 +000015907 // or (and (m, y), (pandn m, x))
Craig Topper1666cb62011-11-19 07:07:26 +000015908 if (N0.getOpcode() == ISD::AND && N1.getOpcode() == X86ISD::ANDNP) {
15909 SDValue Mask = N1.getOperand(0);
15910 SDValue X = N1.getOperand(1);
15911 SDValue Y;
15912 if (N0.getOperand(0) == Mask)
15913 Y = N0.getOperand(1);
15914 if (N0.getOperand(1) == Mask)
15915 Y = N0.getOperand(0);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000015916
Craig Topper1666cb62011-11-19 07:07:26 +000015917 // Check to see if the mask appeared in both the AND and ANDNP and
15918 if (!Y.getNode())
15919 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000015920
Craig Topper1666cb62011-11-19 07:07:26 +000015921 // Validate that X, Y, and Mask are BIT_CONVERTS, and see through them.
Craig Topper1666cb62011-11-19 07:07:26 +000015922 // Look through mask bitcast.
Nadav Rotem4ac90812012-04-01 19:31:22 +000015923 if (Mask.getOpcode() == ISD::BITCAST)
15924 Mask = Mask.getOperand(0);
15925 if (X.getOpcode() == ISD::BITCAST)
15926 X = X.getOperand(0);
15927 if (Y.getOpcode() == ISD::BITCAST)
15928 Y = Y.getOperand(0);
15929
Craig Topper1666cb62011-11-19 07:07:26 +000015930 EVT MaskVT = Mask.getValueType();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000015931
Craig Toppered2e13d2012-01-22 19:15:14 +000015932 // Validate that the Mask operand is a vector sra node.
Craig Topper1666cb62011-11-19 07:07:26 +000015933 // FIXME: what to do for bytes, since there is a psignb/pblendvb, but
15934 // there is no psrai.b
Craig Topper7fb8b0c2012-01-23 06:46:22 +000015935 if (Mask.getOpcode() != X86ISD::VSRAI)
Craig Toppered2e13d2012-01-22 19:15:14 +000015936 return SDValue();
Craig Topper1666cb62011-11-19 07:07:26 +000015937
15938 // Check that the SRA is all signbits.
Craig Topper7fb8b0c2012-01-23 06:46:22 +000015939 SDValue SraC = Mask.getOperand(1);
Craig Topper1666cb62011-11-19 07:07:26 +000015940 unsigned SraAmt = cast<ConstantSDNode>(SraC)->getZExtValue();
15941 unsigned EltBits = MaskVT.getVectorElementType().getSizeInBits();
15942 if ((SraAmt + 1) != EltBits)
15943 return SDValue();
15944
15945 DebugLoc DL = N->getDebugLoc();
15946
Nadav Rotemaf59e9a2012-12-07 21:43:11 +000015947 // We are going to replace the AND, OR, NAND with either BLEND
15948 // or PSIGN, which only look at the MSB. The VSRAI instruction
15949 // does not affect the highest bit, so we can get rid of it.
15950 Mask = Mask.getOperand(0);
15951
Craig Topper1666cb62011-11-19 07:07:26 +000015952 // Now we know we at least have a plendvb with the mask val. See if
15953 // we can form a psignb/w/d.
15954 // psign = x.type == y.type == mask.type && y = sub(0, x);
Craig Topper1666cb62011-11-19 07:07:26 +000015955 if (Y.getOpcode() == ISD::SUB && Y.getOperand(1) == X &&
15956 ISD::isBuildVectorAllZeros(Y.getOperand(0).getNode()) &&
Craig Toppered2e13d2012-01-22 19:15:14 +000015957 X.getValueType() == MaskVT && Y.getValueType() == MaskVT) {
15958 assert((EltBits == 8 || EltBits == 16 || EltBits == 32) &&
15959 "Unsupported VT for PSIGN");
Nadav Rotemaf59e9a2012-12-07 21:43:11 +000015960 Mask = DAG.getNode(X86ISD::PSIGN, DL, MaskVT, X, Mask);
Craig Toppered2e13d2012-01-22 19:15:14 +000015961 return DAG.getNode(ISD::BITCAST, DL, VT, Mask);
Craig Topper1666cb62011-11-19 07:07:26 +000015962 }
15963 // PBLENDVB only available on SSE 4.1
Craig Topperd0a31172012-01-10 06:37:29 +000015964 if (!Subtarget->hasSSE41())
Craig Topper1666cb62011-11-19 07:07:26 +000015965 return SDValue();
15966
15967 EVT BlendVT = (VT == MVT::v4i64) ? MVT::v32i8 : MVT::v16i8;
15968
15969 X = DAG.getNode(ISD::BITCAST, DL, BlendVT, X);
15970 Y = DAG.getNode(ISD::BITCAST, DL, BlendVT, Y);
15971 Mask = DAG.getNode(ISD::BITCAST, DL, BlendVT, Mask);
Nadav Rotem18197d72011-11-30 10:13:37 +000015972 Mask = DAG.getNode(ISD::VSELECT, DL, BlendVT, Mask, Y, X);
Craig Topper1666cb62011-11-19 07:07:26 +000015973 return DAG.getNode(ISD::BITCAST, DL, VT, Mask);
Nate Begemanb65c1752010-12-17 22:55:37 +000015974 }
15975 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000015976
Craig Topper1666cb62011-11-19 07:07:26 +000015977 if (VT != MVT::i16 && VT != MVT::i32 && VT != MVT::i64)
15978 return SDValue();
15979
Nate Begemanb65c1752010-12-17 22:55:37 +000015980 // fold (or (x << c) | (y >> (64 - c))) ==> (shld64 x, y, c)
Evan Cheng760d1942010-01-04 21:22:48 +000015981 if (N0.getOpcode() == ISD::SRL && N1.getOpcode() == ISD::SHL)
15982 std::swap(N0, N1);
15983 if (N0.getOpcode() != ISD::SHL || N1.getOpcode() != ISD::SRL)
15984 return SDValue();
Evan Cheng8b1190a2010-04-28 01:18:01 +000015985 if (!N0.hasOneUse() || !N1.hasOneUse())
15986 return SDValue();
Evan Cheng760d1942010-01-04 21:22:48 +000015987
15988 SDValue ShAmt0 = N0.getOperand(1);
15989 if (ShAmt0.getValueType() != MVT::i8)
15990 return SDValue();
15991 SDValue ShAmt1 = N1.getOperand(1);
15992 if (ShAmt1.getValueType() != MVT::i8)
15993 return SDValue();
15994 if (ShAmt0.getOpcode() == ISD::TRUNCATE)
15995 ShAmt0 = ShAmt0.getOperand(0);
15996 if (ShAmt1.getOpcode() == ISD::TRUNCATE)
15997 ShAmt1 = ShAmt1.getOperand(0);
15998
15999 DebugLoc DL = N->getDebugLoc();
16000 unsigned Opc = X86ISD::SHLD;
16001 SDValue Op0 = N0.getOperand(0);
16002 SDValue Op1 = N1.getOperand(0);
16003 if (ShAmt0.getOpcode() == ISD::SUB) {
16004 Opc = X86ISD::SHRD;
16005 std::swap(Op0, Op1);
16006 std::swap(ShAmt0, ShAmt1);
16007 }
16008
Evan Cheng8b1190a2010-04-28 01:18:01 +000016009 unsigned Bits = VT.getSizeInBits();
Evan Cheng760d1942010-01-04 21:22:48 +000016010 if (ShAmt1.getOpcode() == ISD::SUB) {
16011 SDValue Sum = ShAmt1.getOperand(0);
16012 if (ConstantSDNode *SumC = dyn_cast<ConstantSDNode>(Sum)) {
Dan Gohman4e39e9d2010-06-24 14:30:44 +000016013 SDValue ShAmt1Op1 = ShAmt1.getOperand(1);
16014 if (ShAmt1Op1.getNode()->getOpcode() == ISD::TRUNCATE)
16015 ShAmt1Op1 = ShAmt1Op1.getOperand(0);
16016 if (SumC->getSExtValue() == Bits && ShAmt1Op1 == ShAmt0)
Evan Cheng760d1942010-01-04 21:22:48 +000016017 return DAG.getNode(Opc, DL, VT,
16018 Op0, Op1,
16019 DAG.getNode(ISD::TRUNCATE, DL,
16020 MVT::i8, ShAmt0));
16021 }
16022 } else if (ConstantSDNode *ShAmt1C = dyn_cast<ConstantSDNode>(ShAmt1)) {
16023 ConstantSDNode *ShAmt0C = dyn_cast<ConstantSDNode>(ShAmt0);
16024 if (ShAmt0C &&
Evan Cheng8b1190a2010-04-28 01:18:01 +000016025 ShAmt0C->getSExtValue() + ShAmt1C->getSExtValue() == Bits)
Evan Cheng760d1942010-01-04 21:22:48 +000016026 return DAG.getNode(Opc, DL, VT,
16027 N0.getOperand(0), N1.getOperand(0),
16028 DAG.getNode(ISD::TRUNCATE, DL,
16029 MVT::i8, ShAmt0));
16030 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000016031
Evan Cheng760d1942010-01-04 21:22:48 +000016032 return SDValue();
16033}
16034
Manman Ren92363622012-06-07 22:39:10 +000016035// Generate NEG and CMOV for integer abs.
16036static SDValue performIntegerAbsCombine(SDNode *N, SelectionDAG &DAG) {
16037 EVT VT = N->getValueType(0);
16038
16039 // Since X86 does not have CMOV for 8-bit integer, we don't convert
16040 // 8-bit integer abs to NEG and CMOV.
16041 if (VT.isInteger() && VT.getSizeInBits() == 8)
16042 return SDValue();
16043
16044 SDValue N0 = N->getOperand(0);
16045 SDValue N1 = N->getOperand(1);
16046 DebugLoc DL = N->getDebugLoc();
16047
16048 // Check pattern of XOR(ADD(X,Y), Y) where Y is SRA(X, size(X)-1)
16049 // and change it to SUB and CMOV.
16050 if (VT.isInteger() && N->getOpcode() == ISD::XOR &&
16051 N0.getOpcode() == ISD::ADD &&
16052 N0.getOperand(1) == N1 &&
16053 N1.getOpcode() == ISD::SRA &&
16054 N1.getOperand(0) == N0.getOperand(0))
16055 if (ConstantSDNode *Y1C = dyn_cast<ConstantSDNode>(N1.getOperand(1)))
16056 if (Y1C->getAPIntValue() == VT.getSizeInBits()-1) {
16057 // Generate SUB & CMOV.
16058 SDValue Neg = DAG.getNode(X86ISD::SUB, DL, DAG.getVTList(VT, MVT::i32),
16059 DAG.getConstant(0, VT), N0.getOperand(0));
16060
16061 SDValue Ops[] = { N0.getOperand(0), Neg,
16062 DAG.getConstant(X86::COND_GE, MVT::i8),
16063 SDValue(Neg.getNode(), 1) };
16064 return DAG.getNode(X86ISD::CMOV, DL, DAG.getVTList(VT, MVT::Glue),
16065 Ops, array_lengthof(Ops));
16066 }
16067 return SDValue();
16068}
16069
Craig Topper3738ccd2011-12-27 06:27:23 +000016070// PerformXorCombine - Attempts to turn XOR nodes into BLSMSK nodes
Craig Topperb4c94572011-10-21 06:55:01 +000016071static SDValue PerformXorCombine(SDNode *N, SelectionDAG &DAG,
16072 TargetLowering::DAGCombinerInfo &DCI,
16073 const X86Subtarget *Subtarget) {
Nadav Rotemd6fb53a2012-12-27 08:15:45 +000016074 EVT VT = N->getValueType(0);
Craig Topperb4c94572011-10-21 06:55:01 +000016075 if (DCI.isBeforeLegalizeOps())
16076 return SDValue();
16077
Manman Ren45d53b82012-06-08 18:58:26 +000016078 if (Subtarget->hasCMov()) {
16079 SDValue RV = performIntegerAbsCombine(N, DAG);
16080 if (RV.getNode())
16081 return RV;
16082 }
Manman Ren92363622012-06-07 22:39:10 +000016083
16084 // Try forming BMI if it is available.
16085 if (!Subtarget->hasBMI())
16086 return SDValue();
16087
Craig Topperb4c94572011-10-21 06:55:01 +000016088 if (VT != MVT::i32 && VT != MVT::i64)
16089 return SDValue();
16090
Craig Topper3738ccd2011-12-27 06:27:23 +000016091 assert(Subtarget->hasBMI() && "Creating BLSMSK requires BMI instructions");
16092
Craig Topperb4c94572011-10-21 06:55:01 +000016093 // Create BLSMSK instructions by finding X ^ (X-1)
16094 SDValue N0 = N->getOperand(0);
16095 SDValue N1 = N->getOperand(1);
16096 DebugLoc DL = N->getDebugLoc();
16097
16098 if (N0.getOpcode() == ISD::ADD && N0.getOperand(0) == N1 &&
16099 isAllOnes(N0.getOperand(1)))
16100 return DAG.getNode(X86ISD::BLSMSK, DL, VT, N1);
16101
16102 if (N1.getOpcode() == ISD::ADD && N1.getOperand(0) == N0 &&
16103 isAllOnes(N1.getOperand(1)))
16104 return DAG.getNode(X86ISD::BLSMSK, DL, VT, N0);
16105
16106 return SDValue();
16107}
16108
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016109/// PerformLOADCombine - Do target-specific dag combines on LOAD nodes.
16110static SDValue PerformLOADCombine(SDNode *N, SelectionDAG &DAG,
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000016111 TargetLowering::DAGCombinerInfo &DCI,
16112 const X86Subtarget *Subtarget) {
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016113 LoadSDNode *Ld = cast<LoadSDNode>(N);
16114 EVT RegVT = Ld->getValueType(0);
16115 EVT MemVT = Ld->getMemoryVT();
16116 DebugLoc dl = Ld->getDebugLoc();
16117 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
16118
16119 ISD::LoadExtType Ext = Ld->getExtensionType();
16120
Nadav Rotemca6f2962011-09-18 19:00:23 +000016121 // If this is a vector EXT Load then attempt to optimize it using a
Benjamin Kramer17347912012-12-22 11:34:28 +000016122 // shuffle. If SSSE3 is not available we may emit an illegal shuffle but the
16123 // expansion is still better than scalar code.
16124 // We generate X86ISD::VSEXT for SEXTLOADs if it's available, otherwise we'll
16125 // emit a shuffle and a arithmetic shift.
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016126 // TODO: It is possible to support ZExt by zeroing the undef values
16127 // during the shuffle phase or after the shuffle.
Benjamin Kramer17347912012-12-22 11:34:28 +000016128 if (RegVT.isVector() && RegVT.isInteger() && Subtarget->hasSSE2() &&
16129 (Ext == ISD::EXTLOAD || Ext == ISD::SEXTLOAD)) {
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016130 assert(MemVT != RegVT && "Cannot extend to the same type");
16131 assert(MemVT.isVector() && "Must load a vector from memory");
16132
16133 unsigned NumElems = RegVT.getVectorNumElements();
16134 unsigned RegSz = RegVT.getSizeInBits();
16135 unsigned MemSz = MemVT.getSizeInBits();
16136 assert(RegSz > MemSz && "Register size must be greater than the mem size");
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016137
Elena Demikhovsky4b977312012-12-19 07:50:20 +000016138 if (Ext == ISD::SEXTLOAD && RegSz == 256 && !Subtarget->hasInt256())
16139 return SDValue();
16140
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000016141 // All sizes must be a power of two.
16142 if (!isPowerOf2_32(RegSz * MemSz * NumElems))
16143 return SDValue();
16144
16145 // Attempt to load the original value using scalar loads.
16146 // Find the largest scalar type that divides the total loaded size.
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016147 MVT SclrLoadTy = MVT::i8;
16148 for (unsigned tp = MVT::FIRST_INTEGER_VALUETYPE;
16149 tp < MVT::LAST_INTEGER_VALUETYPE; ++tp) {
16150 MVT Tp = (MVT::SimpleValueType)tp;
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000016151 if (TLI.isTypeLegal(Tp) && ((MemSz % Tp.getSizeInBits()) == 0)) {
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016152 SclrLoadTy = Tp;
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016153 }
16154 }
16155
Nadav Rotem5cd95e12012-07-11 13:27:05 +000016156 // On 32bit systems, we can't save 64bit integers. Try bitcasting to F64.
16157 if (TLI.isTypeLegal(MVT::f64) && SclrLoadTy.getSizeInBits() < 64 &&
16158 (64 <= MemSz))
16159 SclrLoadTy = MVT::f64;
16160
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000016161 // Calculate the number of scalar loads that we need to perform
16162 // in order to load our vector from memory.
16163 unsigned NumLoads = MemSz / SclrLoadTy.getSizeInBits();
Elena Demikhovsky4b977312012-12-19 07:50:20 +000016164 if (Ext == ISD::SEXTLOAD && NumLoads > 1)
16165 return SDValue();
16166
16167 unsigned loadRegZize = RegSz;
16168 if (Ext == ISD::SEXTLOAD && RegSz == 256)
16169 loadRegZize /= 2;
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016170
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000016171 // Represent our vector as a sequence of elements which are the
16172 // largest scalar that we can load.
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016173 EVT LoadUnitVecVT = EVT::getVectorVT(*DAG.getContext(), SclrLoadTy,
Elena Demikhovsky4b977312012-12-19 07:50:20 +000016174 loadRegZize/SclrLoadTy.getSizeInBits());
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016175
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000016176 // Represent the data using the same element type that is stored in
16177 // memory. In practice, we ''widen'' MemVT.
Elena Demikhovsky4b977312012-12-19 07:50:20 +000016178 EVT WideVecVT =
16179 EVT::getVectorVT(*DAG.getContext(), MemVT.getScalarType(),
16180 loadRegZize/MemVT.getScalarType().getSizeInBits());
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016181
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000016182 assert(WideVecVT.getSizeInBits() == LoadUnitVecVT.getSizeInBits() &&
16183 "Invalid vector type");
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016184
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000016185 // We can't shuffle using an illegal type.
16186 if (!TLI.isTypeLegal(WideVecVT))
16187 return SDValue();
16188
16189 SmallVector<SDValue, 8> Chains;
16190 SDValue Ptr = Ld->getBasePtr();
16191 SDValue Increment = DAG.getConstant(SclrLoadTy.getSizeInBits()/8,
16192 TLI.getPointerTy());
16193 SDValue Res = DAG.getUNDEF(LoadUnitVecVT);
16194
16195 for (unsigned i = 0; i < NumLoads; ++i) {
16196 // Perform a single load.
16197 SDValue ScalarLoad = DAG.getLoad(SclrLoadTy, dl, Ld->getChain(),
16198 Ptr, Ld->getPointerInfo(),
16199 Ld->isVolatile(), Ld->isNonTemporal(),
16200 Ld->isInvariant(), Ld->getAlignment());
16201 Chains.push_back(ScalarLoad.getValue(1));
16202 // Create the first element type using SCALAR_TO_VECTOR in order to avoid
16203 // another round of DAGCombining.
16204 if (i == 0)
16205 Res = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, LoadUnitVecVT, ScalarLoad);
16206 else
16207 Res = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, LoadUnitVecVT, Res,
16208 ScalarLoad, DAG.getIntPtrConstant(i));
16209
16210 Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr, Increment);
16211 }
16212
16213 SDValue TF = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &Chains[0],
16214 Chains.size());
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016215
16216 // Bitcast the loaded value to a vector of the original element type, in
16217 // the size of the target vector type.
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000016218 SDValue SlicedVec = DAG.getNode(ISD::BITCAST, dl, WideVecVT, Res);
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016219 unsigned SizeRatio = RegSz/MemSz;
16220
Elena Demikhovsky4b977312012-12-19 07:50:20 +000016221 if (Ext == ISD::SEXTLOAD) {
Benjamin Kramer17347912012-12-22 11:34:28 +000016222 // If we have SSE4.1 we can directly emit a VSEXT node.
16223 if (Subtarget->hasSSE41()) {
16224 SDValue Sext = DAG.getNode(X86ISD::VSEXT, dl, RegVT, SlicedVec);
16225 return DCI.CombineTo(N, Sext, TF, true);
16226 }
16227
16228 // Otherwise we'll shuffle the small elements in the high bits of the
16229 // larger type and perform an arithmetic shift. If the shift is not legal
16230 // it's better to scalarize.
16231 if (!TLI.isOperationLegalOrCustom(ISD::SRA, RegVT))
16232 return SDValue();
16233
16234 // Redistribute the loaded elements into the different locations.
16235 SmallVector<int, 8> ShuffleVec(NumElems * SizeRatio, -1);
16236 for (unsigned i = 0; i != NumElems; ++i)
16237 ShuffleVec[i*SizeRatio + SizeRatio-1] = i;
16238
16239 SDValue Shuff = DAG.getVectorShuffle(WideVecVT, dl, SlicedVec,
16240 DAG.getUNDEF(WideVecVT),
16241 &ShuffleVec[0]);
16242
16243 Shuff = DAG.getNode(ISD::BITCAST, dl, RegVT, Shuff);
16244
16245 // Build the arithmetic shift.
16246 unsigned Amt = RegVT.getVectorElementType().getSizeInBits() -
16247 MemVT.getVectorElementType().getSizeInBits();
16248 SmallVector<SDValue, 8> C(NumElems,
16249 DAG.getConstant(Amt, RegVT.getScalarType()));
16250 SDValue BV = DAG.getNode(ISD::BUILD_VECTOR, dl, RegVT, &C[0], C.size());
16251 Shuff = DAG.getNode(ISD::SRA, dl, RegVT, Shuff, BV);
16252
16253 return DCI.CombineTo(N, Shuff, TF, true);
Elena Demikhovsky4b977312012-12-19 07:50:20 +000016254 }
Benjamin Kramer17347912012-12-22 11:34:28 +000016255
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016256 // Redistribute the loaded elements into the different locations.
16257 SmallVector<int, 8> ShuffleVec(NumElems * SizeRatio, -1);
Craig Topper31a207a2012-05-04 06:39:13 +000016258 for (unsigned i = 0; i != NumElems; ++i)
16259 ShuffleVec[i*SizeRatio] = i;
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016260
16261 SDValue Shuff = DAG.getVectorShuffle(WideVecVT, dl, SlicedVec,
Craig Topperdf966f62012-04-22 19:17:57 +000016262 DAG.getUNDEF(WideVecVT),
16263 &ShuffleVec[0]);
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016264
16265 // Bitcast to the requested type.
16266 Shuff = DAG.getNode(ISD::BITCAST, dl, RegVT, Shuff);
16267 // Replace the original load with the new sequence
16268 // and return the new chain.
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000016269 return DCI.CombineTo(N, Shuff, TF, true);
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016270 }
16271
16272 return SDValue();
16273}
16274
Chris Lattner149a4e52008-02-22 02:09:43 +000016275/// PerformSTORECombine - Do target-specific dag combines on STORE nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000016276static SDValue PerformSTORECombine(SDNode *N, SelectionDAG &DAG,
Evan Cheng536e6672009-03-12 05:59:15 +000016277 const X86Subtarget *Subtarget) {
Nadav Rotem614061b2011-08-10 19:30:14 +000016278 StoreSDNode *St = cast<StoreSDNode>(N);
16279 EVT VT = St->getValue().getValueType();
16280 EVT StVT = St->getMemoryVT();
16281 DebugLoc dl = St->getDebugLoc();
Nadav Rotem5e742a32011-08-11 16:41:21 +000016282 SDValue StoredVal = St->getOperand(1);
16283 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
16284
Nick Lewycky8a8d4792011-12-02 22:16:29 +000016285 // If we are saving a concatenation of two XMM registers, perform two stores.
Nadav Rotem87d35e82012-05-19 20:30:08 +000016286 // On Sandy Bridge, 256-bit memory operations are executed by two
16287 // 128-bit ports. However, on Haswell it is better to issue a single 256-bit
16288 // memory operation.
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000016289 if (VT.is256BitVector() && !Subtarget->hasInt256() &&
Craig Topperb4a8aef2012-04-27 21:05:09 +000016290 StoredVal.getNode()->getOpcode() == ISD::CONCAT_VECTORS &&
16291 StoredVal.getNumOperands() == 2) {
Nadav Rotem5e742a32011-08-11 16:41:21 +000016292 SDValue Value0 = StoredVal.getOperand(0);
16293 SDValue Value1 = StoredVal.getOperand(1);
16294
16295 SDValue Stride = DAG.getConstant(16, TLI.getPointerTy());
16296 SDValue Ptr0 = St->getBasePtr();
16297 SDValue Ptr1 = DAG.getNode(ISD::ADD, dl, Ptr0.getValueType(), Ptr0, Stride);
16298
16299 SDValue Ch0 = DAG.getStore(St->getChain(), dl, Value0, Ptr0,
16300 St->getPointerInfo(), St->isVolatile(),
16301 St->isNonTemporal(), St->getAlignment());
16302 SDValue Ch1 = DAG.getStore(St->getChain(), dl, Value1, Ptr1,
16303 St->getPointerInfo(), St->isVolatile(),
16304 St->isNonTemporal(), St->getAlignment());
16305 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Ch0, Ch1);
16306 }
Nadav Rotem614061b2011-08-10 19:30:14 +000016307
16308 // Optimize trunc store (of multiple scalars) to shuffle and store.
16309 // First, pack all of the elements in one place. Next, store to memory
16310 // in fewer chunks.
16311 if (St->isTruncatingStore() && VT.isVector()) {
16312 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
16313 unsigned NumElems = VT.getVectorNumElements();
16314 assert(StVT != VT && "Cannot truncate to the same type");
16315 unsigned FromSz = VT.getVectorElementType().getSizeInBits();
16316 unsigned ToSz = StVT.getVectorElementType().getSizeInBits();
16317
16318 // From, To sizes and ElemCount must be pow of two
16319 if (!isPowerOf2_32(NumElems * FromSz * ToSz)) return SDValue();
Nadav Rotem9c6cdf42011-09-21 08:45:10 +000016320 // We are going to use the original vector elt for storing.
Nadav Rotem64ac73b2011-09-21 17:14:40 +000016321 // Accumulated smaller vector elements must be a multiple of the store size.
Nadav Rotem9c6cdf42011-09-21 08:45:10 +000016322 if (0 != (NumElems * FromSz) % ToSz) return SDValue();
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016323
Nadav Rotem614061b2011-08-10 19:30:14 +000016324 unsigned SizeRatio = FromSz / ToSz;
16325
16326 assert(SizeRatio * NumElems * ToSz == VT.getSizeInBits());
16327
16328 // Create a type on which we perform the shuffle
16329 EVT WideVecVT = EVT::getVectorVT(*DAG.getContext(),
16330 StVT.getScalarType(), NumElems*SizeRatio);
16331
16332 assert(WideVecVT.getSizeInBits() == VT.getSizeInBits());
16333
16334 SDValue WideVec = DAG.getNode(ISD::BITCAST, dl, WideVecVT, St->getValue());
16335 SmallVector<int, 8> ShuffleVec(NumElems * SizeRatio, -1);
Craig Topper31a207a2012-05-04 06:39:13 +000016336 for (unsigned i = 0; i != NumElems; ++i)
16337 ShuffleVec[i] = i * SizeRatio;
Nadav Rotem614061b2011-08-10 19:30:14 +000016338
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000016339 // Can't shuffle using an illegal type.
16340 if (!TLI.isTypeLegal(WideVecVT))
16341 return SDValue();
Nadav Rotem614061b2011-08-10 19:30:14 +000016342
16343 SDValue Shuff = DAG.getVectorShuffle(WideVecVT, dl, WideVec,
Craig Topperdf966f62012-04-22 19:17:57 +000016344 DAG.getUNDEF(WideVecVT),
16345 &ShuffleVec[0]);
Nadav Rotem614061b2011-08-10 19:30:14 +000016346 // At this point all of the data is stored at the bottom of the
16347 // register. We now need to save it to mem.
16348
16349 // Find the largest store unit
16350 MVT StoreType = MVT::i8;
16351 for (unsigned tp = MVT::FIRST_INTEGER_VALUETYPE;
16352 tp < MVT::LAST_INTEGER_VALUETYPE; ++tp) {
16353 MVT Tp = (MVT::SimpleValueType)tp;
Nadav Rotem5cd95e12012-07-11 13:27:05 +000016354 if (TLI.isTypeLegal(Tp) && Tp.getSizeInBits() <= NumElems * ToSz)
Nadav Rotem614061b2011-08-10 19:30:14 +000016355 StoreType = Tp;
16356 }
16357
Nadav Rotem5cd95e12012-07-11 13:27:05 +000016358 // On 32bit systems, we can't save 64bit integers. Try bitcasting to F64.
16359 if (TLI.isTypeLegal(MVT::f64) && StoreType.getSizeInBits() < 64 &&
16360 (64 <= NumElems * ToSz))
16361 StoreType = MVT::f64;
16362
Nadav Rotem614061b2011-08-10 19:30:14 +000016363 // Bitcast the original vector into a vector of store-size units
16364 EVT StoreVecVT = EVT::getVectorVT(*DAG.getContext(),
Nadav Rotem5cd95e12012-07-11 13:27:05 +000016365 StoreType, VT.getSizeInBits()/StoreType.getSizeInBits());
Nadav Rotem614061b2011-08-10 19:30:14 +000016366 assert(StoreVecVT.getSizeInBits() == VT.getSizeInBits());
16367 SDValue ShuffWide = DAG.getNode(ISD::BITCAST, dl, StoreVecVT, Shuff);
16368 SmallVector<SDValue, 8> Chains;
16369 SDValue Increment = DAG.getConstant(StoreType.getSizeInBits()/8,
16370 TLI.getPointerTy());
16371 SDValue Ptr = St->getBasePtr();
16372
16373 // Perform one or more big stores into memory.
Craig Topper31a207a2012-05-04 06:39:13 +000016374 for (unsigned i=0, e=(ToSz*NumElems)/StoreType.getSizeInBits(); i!=e; ++i) {
Nadav Rotem614061b2011-08-10 19:30:14 +000016375 SDValue SubVec = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl,
16376 StoreType, ShuffWide,
16377 DAG.getIntPtrConstant(i));
16378 SDValue Ch = DAG.getStore(St->getChain(), dl, SubVec, Ptr,
16379 St->getPointerInfo(), St->isVolatile(),
16380 St->isNonTemporal(), St->getAlignment());
16381 Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr, Increment);
16382 Chains.push_back(Ch);
16383 }
16384
16385 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &Chains[0],
16386 Chains.size());
16387 }
16388
Chris Lattner149a4e52008-02-22 02:09:43 +000016389 // Turn load->store of MMX types into GPR load/stores. This avoids clobbering
16390 // the FP state in cases where an emms may be missing.
Dale Johannesen079f2a62008-02-25 19:20:14 +000016391 // A preferable solution to the general problem is to figure out the right
16392 // places to insert EMMS. This qualifies as a quick hack.
Evan Cheng536e6672009-03-12 05:59:15 +000016393
16394 // Similarly, turn load->store of i64 into double load/stores in 32-bit mode.
Evan Cheng536e6672009-03-12 05:59:15 +000016395 if (VT.getSizeInBits() != 64)
16396 return SDValue();
16397
Devang Patel578efa92009-06-05 21:57:13 +000016398 const Function *F = DAG.getMachineFunction().getFunction();
Bill Wendling67658342012-10-09 07:45:08 +000016399 bool NoImplicitFloatOps = F->getFnAttributes().
Bill Wendling034b94b2012-12-19 07:18:57 +000016400 hasAttribute(Attribute::NoImplicitFloat);
Nick Lewycky8a8d4792011-12-02 22:16:29 +000016401 bool F64IsLegal = !DAG.getTarget().Options.UseSoftFloat && !NoImplicitFloatOps
Craig Topper1accb7e2012-01-10 06:54:16 +000016402 && Subtarget->hasSSE2();
Evan Cheng536e6672009-03-12 05:59:15 +000016403 if ((VT.isVector() ||
Owen Anderson825b72b2009-08-11 20:47:22 +000016404 (VT == MVT::i64 && F64IsLegal && !Subtarget->is64Bit())) &&
Dale Johannesen079f2a62008-02-25 19:20:14 +000016405 isa<LoadSDNode>(St->getValue()) &&
16406 !cast<LoadSDNode>(St->getValue())->isVolatile() &&
16407 St->getChain().hasOneUse() && !St->isVolatile()) {
Gabor Greifba36cb52008-08-28 21:40:38 +000016408 SDNode* LdVal = St->getValue().getNode();
Dale Johannesen079f2a62008-02-25 19:20:14 +000016409 LoadSDNode *Ld = 0;
16410 int TokenFactorIndex = -1;
Dan Gohman475871a2008-07-27 21:46:04 +000016411 SmallVector<SDValue, 8> Ops;
Gabor Greifba36cb52008-08-28 21:40:38 +000016412 SDNode* ChainVal = St->getChain().getNode();
Dale Johannesen079f2a62008-02-25 19:20:14 +000016413 // Must be a store of a load. We currently handle two cases: the load
16414 // is a direct child, and it's under an intervening TokenFactor. It is
16415 // possible to dig deeper under nested TokenFactors.
Dale Johannesen14e2ea92008-02-25 22:29:22 +000016416 if (ChainVal == LdVal)
Dale Johannesen079f2a62008-02-25 19:20:14 +000016417 Ld = cast<LoadSDNode>(St->getChain());
16418 else if (St->getValue().hasOneUse() &&
16419 ChainVal->getOpcode() == ISD::TokenFactor) {
Chad Rosierc2348d52012-02-01 18:45:51 +000016420 for (unsigned i = 0, e = ChainVal->getNumOperands(); i != e; ++i) {
Gabor Greifba36cb52008-08-28 21:40:38 +000016421 if (ChainVal->getOperand(i).getNode() == LdVal) {
Dale Johannesen079f2a62008-02-25 19:20:14 +000016422 TokenFactorIndex = i;
16423 Ld = cast<LoadSDNode>(St->getValue());
16424 } else
16425 Ops.push_back(ChainVal->getOperand(i));
16426 }
16427 }
Dale Johannesen079f2a62008-02-25 19:20:14 +000016428
Evan Cheng536e6672009-03-12 05:59:15 +000016429 if (!Ld || !ISD::isNormalLoad(Ld))
16430 return SDValue();
Dale Johannesen079f2a62008-02-25 19:20:14 +000016431
Evan Cheng536e6672009-03-12 05:59:15 +000016432 // If this is not the MMX case, i.e. we are just turning i64 load/store
16433 // into f64 load/store, avoid the transformation if there are multiple
16434 // uses of the loaded value.
16435 if (!VT.isVector() && !Ld->hasNUsesOfValue(1, 0))
16436 return SDValue();
Dale Johannesen079f2a62008-02-25 19:20:14 +000016437
Evan Cheng536e6672009-03-12 05:59:15 +000016438 DebugLoc LdDL = Ld->getDebugLoc();
16439 DebugLoc StDL = N->getDebugLoc();
16440 // If we are a 64-bit capable x86, lower to a single movq load/store pair.
16441 // Otherwise, if it's legal to use f64 SSE instructions, use f64 load/store
16442 // pair instead.
16443 if (Subtarget->is64Bit() || F64IsLegal) {
Owen Anderson825b72b2009-08-11 20:47:22 +000016444 EVT LdVT = Subtarget->is64Bit() ? MVT::i64 : MVT::f64;
Chris Lattner51abfe42010-09-21 06:02:19 +000016445 SDValue NewLd = DAG.getLoad(LdVT, LdDL, Ld->getChain(), Ld->getBasePtr(),
16446 Ld->getPointerInfo(), Ld->isVolatile(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000016447 Ld->isNonTemporal(), Ld->isInvariant(),
16448 Ld->getAlignment());
Evan Cheng536e6672009-03-12 05:59:15 +000016449 SDValue NewChain = NewLd.getValue(1);
Dale Johannesen079f2a62008-02-25 19:20:14 +000016450 if (TokenFactorIndex != -1) {
Evan Cheng536e6672009-03-12 05:59:15 +000016451 Ops.push_back(NewChain);
Owen Anderson825b72b2009-08-11 20:47:22 +000016452 NewChain = DAG.getNode(ISD::TokenFactor, LdDL, MVT::Other, &Ops[0],
Dale Johannesen079f2a62008-02-25 19:20:14 +000016453 Ops.size());
16454 }
Evan Cheng536e6672009-03-12 05:59:15 +000016455 return DAG.getStore(NewChain, StDL, NewLd, St->getBasePtr(),
Chris Lattner51abfe42010-09-21 06:02:19 +000016456 St->getPointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +000016457 St->isVolatile(), St->isNonTemporal(),
16458 St->getAlignment());
Chris Lattner149a4e52008-02-22 02:09:43 +000016459 }
Evan Cheng536e6672009-03-12 05:59:15 +000016460
16461 // Otherwise, lower to two pairs of 32-bit loads / stores.
16462 SDValue LoAddr = Ld->getBasePtr();
Owen Anderson825b72b2009-08-11 20:47:22 +000016463 SDValue HiAddr = DAG.getNode(ISD::ADD, LdDL, MVT::i32, LoAddr,
16464 DAG.getConstant(4, MVT::i32));
Evan Cheng536e6672009-03-12 05:59:15 +000016465
Owen Anderson825b72b2009-08-11 20:47:22 +000016466 SDValue LoLd = DAG.getLoad(MVT::i32, LdDL, Ld->getChain(), LoAddr,
Chris Lattner51abfe42010-09-21 06:02:19 +000016467 Ld->getPointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +000016468 Ld->isVolatile(), Ld->isNonTemporal(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000016469 Ld->isInvariant(), Ld->getAlignment());
Owen Anderson825b72b2009-08-11 20:47:22 +000016470 SDValue HiLd = DAG.getLoad(MVT::i32, LdDL, Ld->getChain(), HiAddr,
Chris Lattner51abfe42010-09-21 06:02:19 +000016471 Ld->getPointerInfo().getWithOffset(4),
David Greene67c9d422010-02-15 16:53:33 +000016472 Ld->isVolatile(), Ld->isNonTemporal(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000016473 Ld->isInvariant(),
Evan Cheng536e6672009-03-12 05:59:15 +000016474 MinAlign(Ld->getAlignment(), 4));
16475
16476 SDValue NewChain = LoLd.getValue(1);
16477 if (TokenFactorIndex != -1) {
16478 Ops.push_back(LoLd);
16479 Ops.push_back(HiLd);
Owen Anderson825b72b2009-08-11 20:47:22 +000016480 NewChain = DAG.getNode(ISD::TokenFactor, LdDL, MVT::Other, &Ops[0],
Evan Cheng536e6672009-03-12 05:59:15 +000016481 Ops.size());
16482 }
16483
16484 LoAddr = St->getBasePtr();
Owen Anderson825b72b2009-08-11 20:47:22 +000016485 HiAddr = DAG.getNode(ISD::ADD, StDL, MVT::i32, LoAddr,
16486 DAG.getConstant(4, MVT::i32));
Evan Cheng536e6672009-03-12 05:59:15 +000016487
16488 SDValue LoSt = DAG.getStore(NewChain, StDL, LoLd, LoAddr,
Chris Lattner8026a9d2010-09-21 17:50:43 +000016489 St->getPointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +000016490 St->isVolatile(), St->isNonTemporal(),
16491 St->getAlignment());
Evan Cheng536e6672009-03-12 05:59:15 +000016492 SDValue HiSt = DAG.getStore(NewChain, StDL, HiLd, HiAddr,
Chris Lattner8026a9d2010-09-21 17:50:43 +000016493 St->getPointerInfo().getWithOffset(4),
Evan Cheng536e6672009-03-12 05:59:15 +000016494 St->isVolatile(),
David Greene67c9d422010-02-15 16:53:33 +000016495 St->isNonTemporal(),
Evan Cheng536e6672009-03-12 05:59:15 +000016496 MinAlign(St->getAlignment(), 4));
Owen Anderson825b72b2009-08-11 20:47:22 +000016497 return DAG.getNode(ISD::TokenFactor, StDL, MVT::Other, LoSt, HiSt);
Chris Lattner149a4e52008-02-22 02:09:43 +000016498 }
Dan Gohman475871a2008-07-27 21:46:04 +000016499 return SDValue();
Chris Lattner149a4e52008-02-22 02:09:43 +000016500}
16501
Duncan Sands17470be2011-09-22 20:15:48 +000016502/// isHorizontalBinOp - Return 'true' if this vector operation is "horizontal"
16503/// and return the operands for the horizontal operation in LHS and RHS. A
16504/// horizontal operation performs the binary operation on successive elements
16505/// of its first operand, then on successive elements of its second operand,
16506/// returning the resulting values in a vector. For example, if
16507/// A = < float a0, float a1, float a2, float a3 >
16508/// and
16509/// B = < float b0, float b1, float b2, float b3 >
16510/// then the result of doing a horizontal operation on A and B is
16511/// A horizontal-op B = < a0 op a1, a2 op a3, b0 op b1, b2 op b3 >.
16512/// In short, LHS and RHS are inspected to see if LHS op RHS is of the form
16513/// A horizontal-op B, for some already available A and B, and if so then LHS is
16514/// set to A, RHS to B, and the routine returns 'true'.
16515/// Note that the binary operation should have the property that if one of the
16516/// operands is UNDEF then the result is UNDEF.
Craig Topperbeabc6c2011-12-05 06:56:46 +000016517static bool isHorizontalBinOp(SDValue &LHS, SDValue &RHS, bool IsCommutative) {
Duncan Sands17470be2011-09-22 20:15:48 +000016518 // Look for the following pattern: if
16519 // A = < float a0, float a1, float a2, float a3 >
16520 // B = < float b0, float b1, float b2, float b3 >
16521 // and
16522 // LHS = VECTOR_SHUFFLE A, B, <0, 2, 4, 6>
16523 // RHS = VECTOR_SHUFFLE A, B, <1, 3, 5, 7>
16524 // then LHS op RHS = < a0 op a1, a2 op a3, b0 op b1, b2 op b3 >
16525 // which is A horizontal-op B.
16526
16527 // At least one of the operands should be a vector shuffle.
16528 if (LHS.getOpcode() != ISD::VECTOR_SHUFFLE &&
16529 RHS.getOpcode() != ISD::VECTOR_SHUFFLE)
16530 return false;
16531
16532 EVT VT = LHS.getValueType();
Craig Topperf8363302011-12-02 08:18:41 +000016533
16534 assert((VT.is128BitVector() || VT.is256BitVector()) &&
16535 "Unsupported vector type for horizontal add/sub");
16536
16537 // Handle 128 and 256-bit vector lengths. AVX defines horizontal add/sub to
16538 // operate independently on 128-bit lanes.
Craig Topperb72039c2011-11-30 09:10:50 +000016539 unsigned NumElts = VT.getVectorNumElements();
16540 unsigned NumLanes = VT.getSizeInBits()/128;
16541 unsigned NumLaneElts = NumElts / NumLanes;
Craig Topperf8363302011-12-02 08:18:41 +000016542 assert((NumLaneElts % 2 == 0) &&
16543 "Vector type should have an even number of elements in each lane");
16544 unsigned HalfLaneElts = NumLaneElts/2;
Duncan Sands17470be2011-09-22 20:15:48 +000016545
16546 // View LHS in the form
16547 // LHS = VECTOR_SHUFFLE A, B, LMask
16548 // If LHS is not a shuffle then pretend it is the shuffle
16549 // LHS = VECTOR_SHUFFLE LHS, undef, <0, 1, ..., N-1>
16550 // NOTE: in what follows a default initialized SDValue represents an UNDEF of
16551 // type VT.
16552 SDValue A, B;
Craig Topperb72039c2011-11-30 09:10:50 +000016553 SmallVector<int, 16> LMask(NumElts);
Duncan Sands17470be2011-09-22 20:15:48 +000016554 if (LHS.getOpcode() == ISD::VECTOR_SHUFFLE) {
16555 if (LHS.getOperand(0).getOpcode() != ISD::UNDEF)
16556 A = LHS.getOperand(0);
16557 if (LHS.getOperand(1).getOpcode() != ISD::UNDEF)
16558 B = LHS.getOperand(1);
Benjamin Kramered4c8c62012-01-15 13:16:05 +000016559 ArrayRef<int> Mask = cast<ShuffleVectorSDNode>(LHS.getNode())->getMask();
16560 std::copy(Mask.begin(), Mask.end(), LMask.begin());
Duncan Sands17470be2011-09-22 20:15:48 +000016561 } else {
16562 if (LHS.getOpcode() != ISD::UNDEF)
16563 A = LHS;
Craig Topperb72039c2011-11-30 09:10:50 +000016564 for (unsigned i = 0; i != NumElts; ++i)
Duncan Sands17470be2011-09-22 20:15:48 +000016565 LMask[i] = i;
16566 }
16567
16568 // Likewise, view RHS in the form
16569 // RHS = VECTOR_SHUFFLE C, D, RMask
16570 SDValue C, D;
Craig Topperb72039c2011-11-30 09:10:50 +000016571 SmallVector<int, 16> RMask(NumElts);
Duncan Sands17470be2011-09-22 20:15:48 +000016572 if (RHS.getOpcode() == ISD::VECTOR_SHUFFLE) {
16573 if (RHS.getOperand(0).getOpcode() != ISD::UNDEF)
16574 C = RHS.getOperand(0);
16575 if (RHS.getOperand(1).getOpcode() != ISD::UNDEF)
16576 D = RHS.getOperand(1);
Benjamin Kramered4c8c62012-01-15 13:16:05 +000016577 ArrayRef<int> Mask = cast<ShuffleVectorSDNode>(RHS.getNode())->getMask();
16578 std::copy(Mask.begin(), Mask.end(), RMask.begin());
Duncan Sands17470be2011-09-22 20:15:48 +000016579 } else {
16580 if (RHS.getOpcode() != ISD::UNDEF)
16581 C = RHS;
Craig Topperb72039c2011-11-30 09:10:50 +000016582 for (unsigned i = 0; i != NumElts; ++i)
Duncan Sands17470be2011-09-22 20:15:48 +000016583 RMask[i] = i;
16584 }
16585
16586 // Check that the shuffles are both shuffling the same vectors.
16587 if (!(A == C && B == D) && !(A == D && B == C))
16588 return false;
16589
16590 // If everything is UNDEF then bail out: it would be better to fold to UNDEF.
16591 if (!A.getNode() && !B.getNode())
16592 return false;
16593
16594 // If A and B occur in reverse order in RHS, then "swap" them (which means
16595 // rewriting the mask).
16596 if (A != C)
Craig Topperbeabc6c2011-12-05 06:56:46 +000016597 CommuteVectorShuffleMask(RMask, NumElts);
Duncan Sands17470be2011-09-22 20:15:48 +000016598
16599 // At this point LHS and RHS are equivalent to
16600 // LHS = VECTOR_SHUFFLE A, B, LMask
16601 // RHS = VECTOR_SHUFFLE A, B, RMask
16602 // Check that the masks correspond to performing a horizontal operation.
Craig Topperf8363302011-12-02 08:18:41 +000016603 for (unsigned i = 0; i != NumElts; ++i) {
Craig Topperbeabc6c2011-12-05 06:56:46 +000016604 int LIdx = LMask[i], RIdx = RMask[i];
Duncan Sands17470be2011-09-22 20:15:48 +000016605
Craig Topperf8363302011-12-02 08:18:41 +000016606 // Ignore any UNDEF components.
Craig Topperbeabc6c2011-12-05 06:56:46 +000016607 if (LIdx < 0 || RIdx < 0 ||
16608 (!A.getNode() && (LIdx < (int)NumElts || RIdx < (int)NumElts)) ||
16609 (!B.getNode() && (LIdx >= (int)NumElts || RIdx >= (int)NumElts)))
Craig Topperf8363302011-12-02 08:18:41 +000016610 continue;
Duncan Sands17470be2011-09-22 20:15:48 +000016611
Craig Topperf8363302011-12-02 08:18:41 +000016612 // Check that successive elements are being operated on. If not, this is
16613 // not a horizontal operation.
16614 unsigned Src = (i/HalfLaneElts) % 2; // each lane is split between srcs
16615 unsigned LaneStart = (i/NumLaneElts) * NumLaneElts;
Craig Topperbeabc6c2011-12-05 06:56:46 +000016616 int Index = 2*(i%HalfLaneElts) + NumElts*Src + LaneStart;
Craig Topperf8363302011-12-02 08:18:41 +000016617 if (!(LIdx == Index && RIdx == Index + 1) &&
Craig Topperbeabc6c2011-12-05 06:56:46 +000016618 !(IsCommutative && LIdx == Index + 1 && RIdx == Index))
Craig Topperf8363302011-12-02 08:18:41 +000016619 return false;
Duncan Sands17470be2011-09-22 20:15:48 +000016620 }
16621
16622 LHS = A.getNode() ? A : B; // If A is 'UNDEF', use B for it.
16623 RHS = B.getNode() ? B : A; // If B is 'UNDEF', use A for it.
16624 return true;
16625}
16626
16627/// PerformFADDCombine - Do target-specific dag combines on floating point adds.
16628static SDValue PerformFADDCombine(SDNode *N, SelectionDAG &DAG,
16629 const X86Subtarget *Subtarget) {
16630 EVT VT = N->getValueType(0);
16631 SDValue LHS = N->getOperand(0);
16632 SDValue RHS = N->getOperand(1);
16633
16634 // Try to synthesize horizontal adds from adds of shuffles.
Craig Topperd0a31172012-01-10 06:37:29 +000016635 if (((Subtarget->hasSSE3() && (VT == MVT::v4f32 || VT == MVT::v2f64)) ||
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000016636 (Subtarget->hasFp256() && (VT == MVT::v8f32 || VT == MVT::v4f64))) &&
Duncan Sands17470be2011-09-22 20:15:48 +000016637 isHorizontalBinOp(LHS, RHS, true))
16638 return DAG.getNode(X86ISD::FHADD, N->getDebugLoc(), VT, LHS, RHS);
16639 return SDValue();
16640}
16641
16642/// PerformFSUBCombine - Do target-specific dag combines on floating point subs.
16643static SDValue PerformFSUBCombine(SDNode *N, SelectionDAG &DAG,
16644 const X86Subtarget *Subtarget) {
16645 EVT VT = N->getValueType(0);
16646 SDValue LHS = N->getOperand(0);
16647 SDValue RHS = N->getOperand(1);
16648
16649 // Try to synthesize horizontal subs from subs of shuffles.
Craig Topperd0a31172012-01-10 06:37:29 +000016650 if (((Subtarget->hasSSE3() && (VT == MVT::v4f32 || VT == MVT::v2f64)) ||
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000016651 (Subtarget->hasFp256() && (VT == MVT::v8f32 || VT == MVT::v4f64))) &&
Duncan Sands17470be2011-09-22 20:15:48 +000016652 isHorizontalBinOp(LHS, RHS, false))
16653 return DAG.getNode(X86ISD::FHSUB, N->getDebugLoc(), VT, LHS, RHS);
16654 return SDValue();
16655}
16656
Chris Lattner6cf73262008-01-25 06:14:17 +000016657/// PerformFORCombine - Do target-specific dag combines on X86ISD::FOR and
16658/// X86ISD::FXOR nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000016659static SDValue PerformFORCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattner6cf73262008-01-25 06:14:17 +000016660 assert(N->getOpcode() == X86ISD::FOR || N->getOpcode() == X86ISD::FXOR);
16661 // F[X]OR(0.0, x) -> x
16662 // F[X]OR(x, 0.0) -> x
Chris Lattneraf723b92008-01-25 05:46:26 +000016663 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
16664 if (C->getValueAPF().isPosZero())
16665 return N->getOperand(1);
16666 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
16667 if (C->getValueAPF().isPosZero())
16668 return N->getOperand(0);
Dan Gohman475871a2008-07-27 21:46:04 +000016669 return SDValue();
Chris Lattneraf723b92008-01-25 05:46:26 +000016670}
16671
Nadav Rotemd60cb112012-08-19 13:06:16 +000016672/// PerformFMinFMaxCombine - Do target-specific dag combines on X86ISD::FMIN and
16673/// X86ISD::FMAX nodes.
16674static SDValue PerformFMinFMaxCombine(SDNode *N, SelectionDAG &DAG) {
16675 assert(N->getOpcode() == X86ISD::FMIN || N->getOpcode() == X86ISD::FMAX);
16676
16677 // Only perform optimizations if UnsafeMath is used.
16678 if (!DAG.getTarget().Options.UnsafeFPMath)
16679 return SDValue();
16680
16681 // If we run in unsafe-math mode, then convert the FMAX and FMIN nodes
Craig Topper8365e9b2012-09-01 06:33:50 +000016682 // into FMINC and FMAXC, which are Commutative operations.
Nadav Rotemd60cb112012-08-19 13:06:16 +000016683 unsigned NewOp = 0;
16684 switch (N->getOpcode()) {
16685 default: llvm_unreachable("unknown opcode");
16686 case X86ISD::FMIN: NewOp = X86ISD::FMINC; break;
16687 case X86ISD::FMAX: NewOp = X86ISD::FMAXC; break;
16688 }
16689
16690 return DAG.getNode(NewOp, N->getDebugLoc(), N->getValueType(0),
16691 N->getOperand(0), N->getOperand(1));
16692}
16693
Chris Lattneraf723b92008-01-25 05:46:26 +000016694/// PerformFANDCombine - Do target-specific dag combines on X86ISD::FAND nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000016695static SDValue PerformFANDCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattneraf723b92008-01-25 05:46:26 +000016696 // FAND(0.0, x) -> 0.0
16697 // FAND(x, 0.0) -> 0.0
16698 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
16699 if (C->getValueAPF().isPosZero())
16700 return N->getOperand(0);
16701 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
16702 if (C->getValueAPF().isPosZero())
16703 return N->getOperand(1);
Dan Gohman475871a2008-07-27 21:46:04 +000016704 return SDValue();
Chris Lattneraf723b92008-01-25 05:46:26 +000016705}
16706
Dan Gohmane5af2d32009-01-29 01:59:02 +000016707static SDValue PerformBTCombine(SDNode *N,
16708 SelectionDAG &DAG,
16709 TargetLowering::DAGCombinerInfo &DCI) {
16710 // BT ignores high bits in the bit index operand.
16711 SDValue Op1 = N->getOperand(1);
16712 if (Op1.hasOneUse()) {
16713 unsigned BitWidth = Op1.getValueSizeInBits();
16714 APInt DemandedMask = APInt::getLowBitsSet(BitWidth, Log2_32(BitWidth));
16715 APInt KnownZero, KnownOne;
Evan Chenge5b51ac2010-04-17 06:13:15 +000016716 TargetLowering::TargetLoweringOpt TLO(DAG, !DCI.isBeforeLegalize(),
16717 !DCI.isBeforeLegalizeOps());
Dan Gohmand858e902010-04-17 15:26:15 +000016718 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Dan Gohmane5af2d32009-01-29 01:59:02 +000016719 if (TLO.ShrinkDemandedConstant(Op1, DemandedMask) ||
16720 TLI.SimplifyDemandedBits(Op1, DemandedMask, KnownZero, KnownOne, TLO))
16721 DCI.CommitTargetLoweringOpt(TLO);
16722 }
16723 return SDValue();
16724}
Chris Lattner83e6c992006-10-04 06:57:07 +000016725
Eli Friedman7a5e5552009-06-07 06:52:44 +000016726static SDValue PerformVZEXT_MOVLCombine(SDNode *N, SelectionDAG &DAG) {
16727 SDValue Op = N->getOperand(0);
Wesley Peckbf17cfa2010-11-23 03:31:01 +000016728 if (Op.getOpcode() == ISD::BITCAST)
Eli Friedman7a5e5552009-06-07 06:52:44 +000016729 Op = Op.getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +000016730 EVT VT = N->getValueType(0), OpVT = Op.getValueType();
Eli Friedman7a5e5552009-06-07 06:52:44 +000016731 if (Op.getOpcode() == X86ISD::VZEXT_LOAD &&
Eric Christopherfd179292009-08-27 18:07:15 +000016732 VT.getVectorElementType().getSizeInBits() ==
Eli Friedman7a5e5552009-06-07 06:52:44 +000016733 OpVT.getVectorElementType().getSizeInBits()) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +000016734 return DAG.getNode(ISD::BITCAST, N->getDebugLoc(), VT, Op);
Eli Friedman7a5e5552009-06-07 06:52:44 +000016735 }
16736 return SDValue();
16737}
16738
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +000016739static SDValue PerformSExtCombine(SDNode *N, SelectionDAG &DAG,
16740 TargetLowering::DAGCombinerInfo &DCI,
16741 const X86Subtarget *Subtarget) {
16742 if (!DCI.isBeforeLegalizeOps())
16743 return SDValue();
16744
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000016745 if (!Subtarget->hasFp256())
Elena Demikhovskyf6020402012-02-08 08:37:26 +000016746 return SDValue();
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +000016747
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +000016748 EVT VT = N->getValueType(0);
16749 SDValue Op = N->getOperand(0);
16750 EVT OpVT = Op.getValueType();
16751 DebugLoc dl = N->getDebugLoc();
16752
Nadav Rotemd6fb53a2012-12-27 08:15:45 +000016753 if (VT.isVector() && VT.getSizeInBits() == 256) {
16754 SDValue R = WidenMaskArithmetic(N, DAG, DCI, Subtarget);
16755 if (R.getNode())
16756 return R;
16757 }
16758
Elena Demikhovskyf6020402012-02-08 08:37:26 +000016759 if ((VT == MVT::v4i64 && OpVT == MVT::v4i32) ||
16760 (VT == MVT::v8i32 && OpVT == MVT::v8i16)) {
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +000016761
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000016762 if (Subtarget->hasInt256())
Elena Demikhovsky1da58672012-04-22 09:39:03 +000016763 return DAG.getNode(X86ISD::VSEXT_MOVL, dl, VT, Op);
Elena Demikhovsky1da58672012-04-22 09:39:03 +000016764
16765 // Optimize vectors in AVX mode
16766 // Sign extend v8i16 to v8i32 and
16767 // v4i32 to v4i64
16768 //
16769 // Divide input vector into two parts
16770 // for v4i32 the shuffle mask will be { 0, 1, -1, -1} {2, 3, -1, -1}
16771 // use vpmovsx instruction to extend v4i32 -> v2i64; v8i16 -> v4i32
16772 // concat the vectors to original VT
16773
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +000016774 unsigned NumElems = OpVT.getVectorNumElements();
Craig Toppercacafd42012-08-14 08:18:43 +000016775 SDValue Undef = DAG.getUNDEF(OpVT);
16776
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +000016777 SmallVector<int,8> ShufMask1(NumElems, -1);
Craig Topper3ef43cf2012-04-24 06:36:35 +000016778 for (unsigned i = 0; i != NumElems/2; ++i)
16779 ShufMask1[i] = i;
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +000016780
Craig Toppercacafd42012-08-14 08:18:43 +000016781 SDValue OpLo = DAG.getVectorShuffle(OpVT, dl, Op, Undef, &ShufMask1[0]);
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +000016782
16783 SmallVector<int,8> ShufMask2(NumElems, -1);
Craig Topper3ef43cf2012-04-24 06:36:35 +000016784 for (unsigned i = 0; i != NumElems/2; ++i)
16785 ShufMask2[i] = i + NumElems/2;
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +000016786
Craig Toppercacafd42012-08-14 08:18:43 +000016787 SDValue OpHi = DAG.getVectorShuffle(OpVT, dl, Op, Undef, &ShufMask2[0]);
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +000016788
Craig Topper3ef43cf2012-04-24 06:36:35 +000016789 EVT HalfVT = EVT::getVectorVT(*DAG.getContext(), VT.getScalarType(),
Elena Demikhovskyf6020402012-02-08 08:37:26 +000016790 VT.getVectorNumElements()/2);
16791
Craig Topper3ef43cf2012-04-24 06:36:35 +000016792 OpLo = DAG.getNode(X86ISD::VSEXT_MOVL, dl, HalfVT, OpLo);
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +000016793 OpHi = DAG.getNode(X86ISD::VSEXT_MOVL, dl, HalfVT, OpHi);
16794
16795 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT, OpLo, OpHi);
16796 }
16797 return SDValue();
16798}
16799
Michael Liaof6c24ee2012-08-10 14:39:24 +000016800static SDValue PerformFMACombine(SDNode *N, SelectionDAG &DAG,
Elena Demikhovsky1503aba2012-08-01 12:06:00 +000016801 const X86Subtarget* Subtarget) {
16802 DebugLoc dl = N->getDebugLoc();
16803 EVT VT = N->getValueType(0);
16804
Craig Topperb1bdd7d2012-08-30 06:56:15 +000016805 // Let legalize expand this if it isn't a legal type yet.
16806 if (!DAG.getTargetLoweringInfo().isTypeLegal(VT))
16807 return SDValue();
16808
Elena Demikhovsky1503aba2012-08-01 12:06:00 +000016809 EVT ScalarVT = VT.getScalarType();
Craig Topperbf404372012-08-31 15:40:30 +000016810 if ((ScalarVT != MVT::f32 && ScalarVT != MVT::f64) ||
16811 (!Subtarget->hasFMA() && !Subtarget->hasFMA4()))
Elena Demikhovsky1503aba2012-08-01 12:06:00 +000016812 return SDValue();
16813
16814 SDValue A = N->getOperand(0);
16815 SDValue B = N->getOperand(1);
16816 SDValue C = N->getOperand(2);
16817
16818 bool NegA = (A.getOpcode() == ISD::FNEG);
16819 bool NegB = (B.getOpcode() == ISD::FNEG);
16820 bool NegC = (C.getOpcode() == ISD::FNEG);
16821
Michael Liaof6c24ee2012-08-10 14:39:24 +000016822 // Negative multiplication when NegA xor NegB
16823 bool NegMul = (NegA != NegB);
Elena Demikhovsky1503aba2012-08-01 12:06:00 +000016824 if (NegA)
16825 A = A.getOperand(0);
16826 if (NegB)
16827 B = B.getOperand(0);
16828 if (NegC)
16829 C = C.getOperand(0);
16830
16831 unsigned Opcode;
16832 if (!NegMul)
Craig Topperbf404372012-08-31 15:40:30 +000016833 Opcode = (!NegC) ? X86ISD::FMADD : X86ISD::FMSUB;
Elena Demikhovsky1503aba2012-08-01 12:06:00 +000016834 else
Craig Topperbf404372012-08-31 15:40:30 +000016835 Opcode = (!NegC) ? X86ISD::FNMADD : X86ISD::FNMSUB;
16836
Elena Demikhovsky1503aba2012-08-01 12:06:00 +000016837 return DAG.getNode(Opcode, dl, VT, A, B, C);
16838}
16839
Elena Demikhovsky28d7e712012-01-24 13:54:13 +000016840static SDValue PerformZExtCombine(SDNode *N, SelectionDAG &DAG,
Craig Topperc16f8512012-04-25 06:39:39 +000016841 TargetLowering::DAGCombinerInfo &DCI,
Elena Demikhovsky28d7e712012-01-24 13:54:13 +000016842 const X86Subtarget *Subtarget) {
Evan Cheng2e489c42009-12-16 00:53:11 +000016843 // (i32 zext (and (i8 x86isd::setcc_carry), 1)) ->
16844 // (and (i32 x86isd::setcc_carry), 1)
16845 // This eliminates the zext. This transformation is necessary because
16846 // ISD::SETCC is always legalized to i8.
16847 DebugLoc dl = N->getDebugLoc();
16848 SDValue N0 = N->getOperand(0);
16849 EVT VT = N->getValueType(0);
Elena Demikhovsky28d7e712012-01-24 13:54:13 +000016850 EVT OpVT = N0.getValueType();
16851
Evan Cheng2e489c42009-12-16 00:53:11 +000016852 if (N0.getOpcode() == ISD::AND &&
16853 N0.hasOneUse() &&
16854 N0.getOperand(0).hasOneUse()) {
16855 SDValue N00 = N0.getOperand(0);
Nadav Rotemd6fb53a2012-12-27 08:15:45 +000016856 if (N00.getOpcode() == X86ISD::SETCC_CARRY) {
16857 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
16858 if (!C || C->getZExtValue() != 1)
16859 return SDValue();
16860 return DAG.getNode(ISD::AND, dl, VT,
16861 DAG.getNode(X86ISD::SETCC_CARRY, dl, VT,
16862 N00.getOperand(0), N00.getOperand(1)),
16863 DAG.getConstant(1, VT));
16864 }
16865 }
16866
16867 if (VT.isVector() && VT.getSizeInBits() == 256) {
16868 SDValue R = WidenMaskArithmetic(N, DAG, DCI, Subtarget);
16869 if (R.getNode())
16870 return R;
Evan Cheng2e489c42009-12-16 00:53:11 +000016871 }
Craig Topperd0cf5652012-04-21 18:13:35 +000016872
Elena Demikhovsky28d7e712012-01-24 13:54:13 +000016873 // Optimize vectors in AVX mode:
16874 //
16875 // v8i16 -> v8i32
16876 // Use vpunpcklwd for 4 lower elements v8i16 -> v4i32.
16877 // Use vpunpckhwd for 4 upper elements v8i16 -> v4i32.
16878 // Concat upper and lower parts.
16879 //
16880 // v4i32 -> v4i64
16881 // Use vpunpckldq for 4 lower elements v4i32 -> v2i64.
16882 // Use vpunpckhdq for 4 upper elements v4i32 -> v2i64.
16883 // Concat upper and lower parts.
16884 //
Craig Topperc16f8512012-04-25 06:39:39 +000016885 if (!DCI.isBeforeLegalizeOps())
16886 return SDValue();
Elena Demikhovsky28d7e712012-01-24 13:54:13 +000016887
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000016888 if (!Subtarget->hasFp256())
Craig Topperc16f8512012-04-25 06:39:39 +000016889 return SDValue();
Elena Demikhovsky28d7e712012-01-24 13:54:13 +000016890
Craig Topperc16f8512012-04-25 06:39:39 +000016891 if (((VT == MVT::v8i32) && (OpVT == MVT::v8i16)) ||
16892 ((VT == MVT::v4i64) && (OpVT == MVT::v4i32))) {
Elena Demikhovsky1da58672012-04-22 09:39:03 +000016893
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000016894 if (Subtarget->hasInt256())
Craig Topperc16f8512012-04-25 06:39:39 +000016895 return DAG.getNode(X86ISD::VZEXT_MOVL, dl, VT, N0);
Elena Demikhovsky28d7e712012-01-24 13:54:13 +000016896
Craig Topperc16f8512012-04-25 06:39:39 +000016897 SDValue ZeroVec = getZeroVector(OpVT, Subtarget, DAG, dl);
16898 SDValue OpLo = getUnpackl(DAG, dl, OpVT, N0, ZeroVec);
16899 SDValue OpHi = getUnpackh(DAG, dl, OpVT, N0, ZeroVec);
Elena Demikhovsky28d7e712012-01-24 13:54:13 +000016900
Craig Topperc16f8512012-04-25 06:39:39 +000016901 EVT HVT = EVT::getVectorVT(*DAG.getContext(), VT.getVectorElementType(),
16902 VT.getVectorNumElements()/2);
Elena Demikhovsky28d7e712012-01-24 13:54:13 +000016903
Craig Topperc16f8512012-04-25 06:39:39 +000016904 OpLo = DAG.getNode(ISD::BITCAST, dl, HVT, OpLo);
16905 OpHi = DAG.getNode(ISD::BITCAST, dl, HVT, OpHi);
16906
16907 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT, OpLo, OpHi);
Elena Demikhovsky28d7e712012-01-24 13:54:13 +000016908 }
16909
Evan Cheng2e489c42009-12-16 00:53:11 +000016910 return SDValue();
16911}
16912
Chad Rosiera73b6fc2012-04-27 22:33:25 +000016913// Optimize x == -y --> x+y == 0
16914// x != -y --> x+y != 0
16915static SDValue PerformISDSETCCCombine(SDNode *N, SelectionDAG &DAG) {
16916 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(2))->get();
16917 SDValue LHS = N->getOperand(0);
Chad Rosiera20e1e72012-08-01 18:39:17 +000016918 SDValue RHS = N->getOperand(1);
Chad Rosiera73b6fc2012-04-27 22:33:25 +000016919
16920 if ((CC == ISD::SETNE || CC == ISD::SETEQ) && LHS.getOpcode() == ISD::SUB)
16921 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(LHS.getOperand(0)))
16922 if (C->getAPIntValue() == 0 && LHS.hasOneUse()) {
16923 SDValue addV = DAG.getNode(ISD::ADD, N->getDebugLoc(),
16924 LHS.getValueType(), RHS, LHS.getOperand(1));
16925 return DAG.getSetCC(N->getDebugLoc(), N->getValueType(0),
16926 addV, DAG.getConstant(0, addV.getValueType()), CC);
16927 }
16928 if ((CC == ISD::SETNE || CC == ISD::SETEQ) && RHS.getOpcode() == ISD::SUB)
16929 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(RHS.getOperand(0)))
16930 if (C->getAPIntValue() == 0 && RHS.hasOneUse()) {
16931 SDValue addV = DAG.getNode(ISD::ADD, N->getDebugLoc(),
16932 RHS.getValueType(), LHS, RHS.getOperand(1));
16933 return DAG.getSetCC(N->getDebugLoc(), N->getValueType(0),
16934 addV, DAG.getConstant(0, addV.getValueType()), CC);
16935 }
16936 return SDValue();
16937}
16938
Shuxin Yanga5526a92012-10-31 23:11:48 +000016939// Helper function of PerformSETCCCombine. It is to materialize "setb reg"
16940// as "sbb reg,reg", since it can be extended without zext and produces
16941// an all-ones bit which is more useful than 0/1 in some cases.
16942static SDValue MaterializeSETB(DebugLoc DL, SDValue EFLAGS, SelectionDAG &DAG) {
16943 return DAG.getNode(ISD::AND, DL, MVT::i8,
16944 DAG.getNode(X86ISD::SETCC_CARRY, DL, MVT::i8,
16945 DAG.getConstant(X86::COND_B, MVT::i8), EFLAGS),
16946 DAG.getConstant(1, MVT::i8));
16947}
16948
Chris Lattnerc19d1c32010-12-19 22:08:31 +000016949// Optimize RES = X86ISD::SETCC CONDCODE, EFLAG_INPUT
Michael Liaodbf8b5b2012-08-28 03:34:40 +000016950static SDValue PerformSETCCCombine(SDNode *N, SelectionDAG &DAG,
16951 TargetLowering::DAGCombinerInfo &DCI,
16952 const X86Subtarget *Subtarget) {
Chris Lattnerc19d1c32010-12-19 22:08:31 +000016953 DebugLoc DL = N->getDebugLoc();
Michael Liao2a33cec2012-08-10 19:58:13 +000016954 X86::CondCode CC = X86::CondCode(N->getConstantOperandVal(0));
16955 SDValue EFLAGS = N->getOperand(1);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000016956
Shuxin Yanga5526a92012-10-31 23:11:48 +000016957 if (CC == X86::COND_A) {
16958 // Try to convert COND_A into COND_B in an attempt to facilitate
16959 // materializing "setb reg".
16960 //
16961 // Do not flip "e > c", where "c" is a constant, because Cmp instruction
16962 // cannot take an immediate as its first operand.
16963 //
16964 if (EFLAGS.getOpcode() == X86ISD::SUB && EFLAGS.hasOneUse() &&
16965 EFLAGS.getValueType().isInteger() &&
16966 !isa<ConstantSDNode>(EFLAGS.getOperand(1))) {
16967 SDValue NewSub = DAG.getNode(X86ISD::SUB, EFLAGS.getDebugLoc(),
16968 EFLAGS.getNode()->getVTList(),
16969 EFLAGS.getOperand(1), EFLAGS.getOperand(0));
16970 SDValue NewEFLAGS = SDValue(NewSub.getNode(), EFLAGS.getResNo());
16971 return MaterializeSETB(DL, NewEFLAGS, DAG);
16972 }
16973 }
16974
Chris Lattnerc19d1c32010-12-19 22:08:31 +000016975 // Materialize "setb reg" as "sbb reg,reg", since it can be extended without
16976 // a zext and produces an all-ones bit which is more useful than 0/1 in some
16977 // cases.
Michael Liao2a33cec2012-08-10 19:58:13 +000016978 if (CC == X86::COND_B)
Shuxin Yanga5526a92012-10-31 23:11:48 +000016979 return MaterializeSETB(DL, EFLAGS, DAG);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000016980
Michael Liao2a33cec2012-08-10 19:58:13 +000016981 SDValue Flags;
16982
Michael Liaodbf8b5b2012-08-28 03:34:40 +000016983 Flags = checkBoolTestSetCCCombine(EFLAGS, CC);
16984 if (Flags.getNode()) {
16985 SDValue Cond = DAG.getConstant(CC, MVT::i8);
16986 return DAG.getNode(X86ISD::SETCC, DL, N->getVTList(), Cond, Flags);
16987 }
16988
Michael Liao2a33cec2012-08-10 19:58:13 +000016989 return SDValue();
16990}
16991
16992// Optimize branch condition evaluation.
16993//
16994static SDValue PerformBrCondCombine(SDNode *N, SelectionDAG &DAG,
16995 TargetLowering::DAGCombinerInfo &DCI,
16996 const X86Subtarget *Subtarget) {
16997 DebugLoc DL = N->getDebugLoc();
16998 SDValue Chain = N->getOperand(0);
16999 SDValue Dest = N->getOperand(1);
17000 SDValue EFLAGS = N->getOperand(3);
17001 X86::CondCode CC = X86::CondCode(N->getConstantOperandVal(2));
17002
17003 SDValue Flags;
17004
Michael Liaodbf8b5b2012-08-28 03:34:40 +000017005 Flags = checkBoolTestSetCCCombine(EFLAGS, CC);
17006 if (Flags.getNode()) {
17007 SDValue Cond = DAG.getConstant(CC, MVT::i8);
17008 return DAG.getNode(X86ISD::BRCOND, DL, N->getVTList(), Chain, Dest, Cond,
17009 Flags);
17010 }
17011
Chris Lattnerc19d1c32010-12-19 22:08:31 +000017012 return SDValue();
17013}
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000017014
Benjamin Kramer1396c402011-06-18 11:09:41 +000017015static SDValue PerformSINT_TO_FPCombine(SDNode *N, SelectionDAG &DAG,
17016 const X86TargetLowering *XTLI) {
Stuart Hastingsf99a4b82011-06-06 23:15:58 +000017017 SDValue Op0 = N->getOperand(0);
Nadav Rotema3540772012-04-23 21:53:37 +000017018 EVT InVT = Op0->getValueType(0);
Nadav Rotema3540772012-04-23 21:53:37 +000017019
17020 // SINT_TO_FP(v4i8) -> SINT_TO_FP(SEXT(v4i8 to v4i32))
Craig Topper7fd5e162012-04-24 06:02:29 +000017021 if (InVT == MVT::v8i8 || InVT == MVT::v4i8) {
Nadav Rotema3540772012-04-23 21:53:37 +000017022 DebugLoc dl = N->getDebugLoc();
Craig Topper7fd5e162012-04-24 06:02:29 +000017023 MVT DstVT = InVT == MVT::v4i8 ? MVT::v4i32 : MVT::v8i32;
Nadav Rotema3540772012-04-23 21:53:37 +000017024 SDValue P = DAG.getNode(ISD::SIGN_EXTEND, dl, DstVT, Op0);
17025 return DAG.getNode(ISD::SINT_TO_FP, dl, N->getValueType(0), P);
17026 }
17027
Stuart Hastingsf99a4b82011-06-06 23:15:58 +000017028 // Transform (SINT_TO_FP (i64 ...)) into an x87 operation if we have
17029 // a 32-bit target where SSE doesn't support i64->FP operations.
17030 if (Op0.getOpcode() == ISD::LOAD) {
17031 LoadSDNode *Ld = cast<LoadSDNode>(Op0.getNode());
17032 EVT VT = Ld->getValueType(0);
17033 if (!Ld->isVolatile() && !N->getValueType(0).isVector() &&
17034 ISD::isNON_EXTLoad(Op0.getNode()) && Op0.hasOneUse() &&
17035 !XTLI->getSubtarget()->is64Bit() &&
17036 !DAG.getTargetLoweringInfo().isTypeLegal(VT)) {
Benjamin Kramer1396c402011-06-18 11:09:41 +000017037 SDValue FILDChain = XTLI->BuildFILD(SDValue(N, 0), Ld->getValueType(0),
17038 Ld->getChain(), Op0, DAG);
Stuart Hastingsf99a4b82011-06-06 23:15:58 +000017039 DAG.ReplaceAllUsesOfValueWith(Op0.getValue(1), FILDChain.getValue(1));
17040 return FILDChain;
17041 }
17042 }
17043 return SDValue();
17044}
17045
Chris Lattner23a01992010-12-20 01:37:09 +000017046// Optimize RES, EFLAGS = X86ISD::ADC LHS, RHS, EFLAGS
17047static SDValue PerformADCCombine(SDNode *N, SelectionDAG &DAG,
17048 X86TargetLowering::DAGCombinerInfo &DCI) {
17049 // If the LHS and RHS of the ADC node are zero, then it can't overflow and
17050 // the result is either zero or one (depending on the input carry bit).
17051 // Strength reduce this down to a "set on carry" aka SETCC_CARRY&1.
17052 if (X86::isZeroNode(N->getOperand(0)) &&
17053 X86::isZeroNode(N->getOperand(1)) &&
17054 // We don't have a good way to replace an EFLAGS use, so only do this when
17055 // dead right now.
17056 SDValue(N, 1).use_empty()) {
17057 DebugLoc DL = N->getDebugLoc();
17058 EVT VT = N->getValueType(0);
17059 SDValue CarryOut = DAG.getConstant(0, N->getValueType(1));
17060 SDValue Res1 = DAG.getNode(ISD::AND, DL, VT,
17061 DAG.getNode(X86ISD::SETCC_CARRY, DL, VT,
17062 DAG.getConstant(X86::COND_B,MVT::i8),
17063 N->getOperand(2)),
17064 DAG.getConstant(1, VT));
17065 return DCI.CombineTo(N, Res1, CarryOut);
17066 }
17067
17068 return SDValue();
17069}
17070
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000017071// fold (add Y, (sete X, 0)) -> adc 0, Y
17072// (add Y, (setne X, 0)) -> sbb -1, Y
17073// (sub (sete X, 0), Y) -> sbb 0, Y
17074// (sub (setne X, 0), Y) -> adc -1, Y
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000017075static SDValue OptimizeConditionalInDecrement(SDNode *N, SelectionDAG &DAG) {
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000017076 DebugLoc DL = N->getDebugLoc();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000017077
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000017078 // Look through ZExts.
17079 SDValue Ext = N->getOperand(N->getOpcode() == ISD::SUB ? 1 : 0);
17080 if (Ext.getOpcode() != ISD::ZERO_EXTEND || !Ext.hasOneUse())
17081 return SDValue();
17082
17083 SDValue SetCC = Ext.getOperand(0);
17084 if (SetCC.getOpcode() != X86ISD::SETCC || !SetCC.hasOneUse())
17085 return SDValue();
17086
17087 X86::CondCode CC = (X86::CondCode)SetCC.getConstantOperandVal(0);
17088 if (CC != X86::COND_E && CC != X86::COND_NE)
17089 return SDValue();
17090
17091 SDValue Cmp = SetCC.getOperand(1);
17092 if (Cmp.getOpcode() != X86ISD::CMP || !Cmp.hasOneUse() ||
Chris Lattner9cd3da42011-01-16 02:56:53 +000017093 !X86::isZeroNode(Cmp.getOperand(1)) ||
17094 !Cmp.getOperand(0).getValueType().isInteger())
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000017095 return SDValue();
17096
17097 SDValue CmpOp0 = Cmp.getOperand(0);
17098 SDValue NewCmp = DAG.getNode(X86ISD::CMP, DL, MVT::i32, CmpOp0,
17099 DAG.getConstant(1, CmpOp0.getValueType()));
17100
17101 SDValue OtherVal = N->getOperand(N->getOpcode() == ISD::SUB ? 0 : 1);
17102 if (CC == X86::COND_NE)
17103 return DAG.getNode(N->getOpcode() == ISD::SUB ? X86ISD::ADC : X86ISD::SBB,
17104 DL, OtherVal.getValueType(), OtherVal,
17105 DAG.getConstant(-1ULL, OtherVal.getValueType()), NewCmp);
17106 return DAG.getNode(N->getOpcode() == ISD::SUB ? X86ISD::SBB : X86ISD::ADC,
17107 DL, OtherVal.getValueType(), OtherVal,
17108 DAG.getConstant(0, OtherVal.getValueType()), NewCmp);
17109}
Chris Lattnerc19d1c32010-12-19 22:08:31 +000017110
Craig Topper54f952a2011-11-19 09:02:40 +000017111/// PerformADDCombine - Do target-specific dag combines on integer adds.
17112static SDValue PerformAddCombine(SDNode *N, SelectionDAG &DAG,
17113 const X86Subtarget *Subtarget) {
17114 EVT VT = N->getValueType(0);
17115 SDValue Op0 = N->getOperand(0);
17116 SDValue Op1 = N->getOperand(1);
17117
17118 // Try to synthesize horizontal adds from adds of shuffles.
Craig Topperd0a31172012-01-10 06:37:29 +000017119 if (((Subtarget->hasSSSE3() && (VT == MVT::v8i16 || VT == MVT::v4i32)) ||
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000017120 (Subtarget->hasInt256() && (VT == MVT::v16i16 || VT == MVT::v8i32))) &&
Craig Topper54f952a2011-11-19 09:02:40 +000017121 isHorizontalBinOp(Op0, Op1, true))
17122 return DAG.getNode(X86ISD::HADD, N->getDebugLoc(), VT, Op0, Op1);
17123
17124 return OptimizeConditionalInDecrement(N, DAG);
17125}
17126
17127static SDValue PerformSubCombine(SDNode *N, SelectionDAG &DAG,
17128 const X86Subtarget *Subtarget) {
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000017129 SDValue Op0 = N->getOperand(0);
17130 SDValue Op1 = N->getOperand(1);
17131
17132 // X86 can't encode an immediate LHS of a sub. See if we can push the
17133 // negation into a preceding instruction.
17134 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op0)) {
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000017135 // If the RHS of the sub is a XOR with one use and a constant, invert the
17136 // immediate. Then add one to the LHS of the sub so we can turn
17137 // X-Y -> X+~Y+1, saving one register.
17138 if (Op1->hasOneUse() && Op1.getOpcode() == ISD::XOR &&
17139 isa<ConstantSDNode>(Op1.getOperand(1))) {
Nick Lewycky726ebd62011-08-23 19:01:24 +000017140 APInt XorC = cast<ConstantSDNode>(Op1.getOperand(1))->getAPIntValue();
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000017141 EVT VT = Op0.getValueType();
17142 SDValue NewXor = DAG.getNode(ISD::XOR, Op1.getDebugLoc(), VT,
17143 Op1.getOperand(0),
17144 DAG.getConstant(~XorC, VT));
17145 return DAG.getNode(ISD::ADD, N->getDebugLoc(), VT, NewXor,
Nick Lewycky726ebd62011-08-23 19:01:24 +000017146 DAG.getConstant(C->getAPIntValue()+1, VT));
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000017147 }
17148 }
17149
Craig Topper54f952a2011-11-19 09:02:40 +000017150 // Try to synthesize horizontal adds from adds of shuffles.
17151 EVT VT = N->getValueType(0);
Craig Topperd0a31172012-01-10 06:37:29 +000017152 if (((Subtarget->hasSSSE3() && (VT == MVT::v8i16 || VT == MVT::v4i32)) ||
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000017153 (Subtarget->hasInt256() && (VT == MVT::v16i16 || VT == MVT::v8i32))) &&
Craig Topperb72039c2011-11-30 09:10:50 +000017154 isHorizontalBinOp(Op0, Op1, true))
Craig Topper54f952a2011-11-19 09:02:40 +000017155 return DAG.getNode(X86ISD::HSUB, N->getDebugLoc(), VT, Op0, Op1);
17156
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000017157 return OptimizeConditionalInDecrement(N, DAG);
17158}
17159
Michael Liaod9d09602012-10-23 17:34:00 +000017160/// performVZEXTCombine - Performs build vector combines
17161static SDValue performVZEXTCombine(SDNode *N, SelectionDAG &DAG,
17162 TargetLowering::DAGCombinerInfo &DCI,
17163 const X86Subtarget *Subtarget) {
17164 // (vzext (bitcast (vzext (x)) -> (vzext x)
17165 SDValue In = N->getOperand(0);
17166 while (In.getOpcode() == ISD::BITCAST)
17167 In = In.getOperand(0);
17168
17169 if (In.getOpcode() != X86ISD::VZEXT)
17170 return SDValue();
17171
17172 return DAG.getNode(X86ISD::VZEXT, N->getDebugLoc(), N->getValueType(0), In.getOperand(0));
17173}
17174
Dan Gohman475871a2008-07-27 21:46:04 +000017175SDValue X86TargetLowering::PerformDAGCombine(SDNode *N,
Evan Cheng9dd93b32008-11-05 06:03:38 +000017176 DAGCombinerInfo &DCI) const {
Evan Cheng206ee9d2006-07-07 08:33:52 +000017177 SelectionDAG &DAG = DCI.DAG;
17178 switch (N->getOpcode()) {
17179 default: break;
Dan Gohman1bbf72b2010-03-15 23:23:03 +000017180 case ISD::EXTRACT_VECTOR_ELT:
Craig Topper89f4e662012-03-20 07:17:59 +000017181 return PerformEXTRACT_VECTOR_ELTCombine(N, DAG, DCI);
Duncan Sands6bcd2192011-09-17 16:49:39 +000017182 case ISD::VSELECT:
Nadav Rotemcc616562012-01-15 19:27:55 +000017183 case ISD::SELECT: return PerformSELECTCombine(N, DAG, DCI, Subtarget);
Michael Liaodbf8b5b2012-08-28 03:34:40 +000017184 case X86ISD::CMOV: return PerformCMOVCombine(N, DAG, DCI, Subtarget);
Craig Topper54f952a2011-11-19 09:02:40 +000017185 case ISD::ADD: return PerformAddCombine(N, DAG, Subtarget);
17186 case ISD::SUB: return PerformSubCombine(N, DAG, Subtarget);
Chris Lattner23a01992010-12-20 01:37:09 +000017187 case X86ISD::ADC: return PerformADCCombine(N, DAG, DCI);
Evan Cheng0b0cd912009-03-28 05:57:29 +000017188 case ISD::MUL: return PerformMulCombine(N, DAG, DCI);
Nate Begeman740ab032009-01-26 00:52:55 +000017189 case ISD::SHL:
17190 case ISD::SRA:
Mon P Wang845b1892012-02-01 22:15:20 +000017191 case ISD::SRL: return PerformShiftCombine(N, DAG, DCI, Subtarget);
Nate Begemanb65c1752010-12-17 22:55:37 +000017192 case ISD::AND: return PerformAndCombine(N, DAG, DCI, Subtarget);
Evan Cheng8b1190a2010-04-28 01:18:01 +000017193 case ISD::OR: return PerformOrCombine(N, DAG, DCI, Subtarget);
Craig Topperb4c94572011-10-21 06:55:01 +000017194 case ISD::XOR: return PerformXorCombine(N, DAG, DCI, Subtarget);
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000017195 case ISD::LOAD: return PerformLOADCombine(N, DAG, DCI, Subtarget);
Evan Cheng7e2ff772008-05-08 00:57:18 +000017196 case ISD::STORE: return PerformSTORECombine(N, DAG, Subtarget);
Stuart Hastingsf99a4b82011-06-06 23:15:58 +000017197 case ISD::SINT_TO_FP: return PerformSINT_TO_FPCombine(N, DAG, this);
Duncan Sands17470be2011-09-22 20:15:48 +000017198 case ISD::FADD: return PerformFADDCombine(N, DAG, Subtarget);
17199 case ISD::FSUB: return PerformFSUBCombine(N, DAG, Subtarget);
Chris Lattner6cf73262008-01-25 06:14:17 +000017200 case X86ISD::FXOR:
Chris Lattneraf723b92008-01-25 05:46:26 +000017201 case X86ISD::FOR: return PerformFORCombine(N, DAG);
Nadav Rotemd60cb112012-08-19 13:06:16 +000017202 case X86ISD::FMIN:
17203 case X86ISD::FMAX: return PerformFMinFMaxCombine(N, DAG);
Chris Lattneraf723b92008-01-25 05:46:26 +000017204 case X86ISD::FAND: return PerformFANDCombine(N, DAG);
Dan Gohmane5af2d32009-01-29 01:59:02 +000017205 case X86ISD::BT: return PerformBTCombine(N, DAG, DCI);
Eli Friedman7a5e5552009-06-07 06:52:44 +000017206 case X86ISD::VZEXT_MOVL: return PerformVZEXT_MOVLCombine(N, DAG);
Elena Demikhovsky1da58672012-04-22 09:39:03 +000017207 case ISD::ANY_EXTEND:
Craig Topperc16f8512012-04-25 06:39:39 +000017208 case ISD::ZERO_EXTEND: return PerformZExtCombine(N, DAG, DCI, Subtarget);
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +000017209 case ISD::SIGN_EXTEND: return PerformSExtCombine(N, DAG, DCI, Subtarget);
Craig Topper55b24052012-09-11 06:15:32 +000017210 case ISD::TRUNCATE: return PerformTruncateCombine(N, DAG,DCI,Subtarget);
Chad Rosiera73b6fc2012-04-27 22:33:25 +000017211 case ISD::SETCC: return PerformISDSETCCCombine(N, DAG);
Michael Liaodbf8b5b2012-08-28 03:34:40 +000017212 case X86ISD::SETCC: return PerformSETCCCombine(N, DAG, DCI, Subtarget);
Michael Liao2a33cec2012-08-10 19:58:13 +000017213 case X86ISD::BRCOND: return PerformBrCondCombine(N, DAG, DCI, Subtarget);
Michael Liaod9d09602012-10-23 17:34:00 +000017214 case X86ISD::VZEXT: return performVZEXTCombine(N, DAG, DCI, Subtarget);
Craig Topperb3982da2011-12-31 23:50:21 +000017215 case X86ISD::SHUFP: // Handle all target specific shuffles
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +000017216 case X86ISD::PALIGN:
Craig Topper34671b82011-12-06 08:21:25 +000017217 case X86ISD::UNPCKH:
17218 case X86ISD::UNPCKL:
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000017219 case X86ISD::MOVHLPS:
17220 case X86ISD::MOVLHPS:
17221 case X86ISD::PSHUFD:
17222 case X86ISD::PSHUFHW:
17223 case X86ISD::PSHUFLW:
17224 case X86ISD::MOVSS:
17225 case X86ISD::MOVSD:
Craig Topper316cd2a2011-11-30 06:25:25 +000017226 case X86ISD::VPERMILP:
Craig Topperec24e612011-11-30 07:47:51 +000017227 case X86ISD::VPERM2X128:
Bruno Cardoso Lopes50b37c72011-08-15 21:45:54 +000017228 case ISD::VECTOR_SHUFFLE: return PerformShuffleCombine(N, DAG, DCI,Subtarget);
Elena Demikhovsky1503aba2012-08-01 12:06:00 +000017229 case ISD::FMA: return PerformFMACombine(N, DAG, Subtarget);
Evan Cheng206ee9d2006-07-07 08:33:52 +000017230 }
17231
Dan Gohman475871a2008-07-27 21:46:04 +000017232 return SDValue();
Evan Cheng206ee9d2006-07-07 08:33:52 +000017233}
17234
Evan Chenge5b51ac2010-04-17 06:13:15 +000017235/// isTypeDesirableForOp - Return true if the target has native support for
17236/// the specified value type and it is 'desirable' to use the type for the
17237/// given node type. e.g. On x86 i16 is legal, but undesirable since i16
17238/// instruction encodings are longer and some i16 instructions are slow.
17239bool X86TargetLowering::isTypeDesirableForOp(unsigned Opc, EVT VT) const {
17240 if (!isTypeLegal(VT))
17241 return false;
Evan Cheng2bce5f4b2010-04-28 08:30:49 +000017242 if (VT != MVT::i16)
Evan Chenge5b51ac2010-04-17 06:13:15 +000017243 return true;
17244
17245 switch (Opc) {
17246 default:
17247 return true;
Evan Cheng4c26e932010-04-19 19:29:22 +000017248 case ISD::LOAD:
17249 case ISD::SIGN_EXTEND:
17250 case ISD::ZERO_EXTEND:
17251 case ISD::ANY_EXTEND:
Evan Chenge5b51ac2010-04-17 06:13:15 +000017252 case ISD::SHL:
Evan Chenge5b51ac2010-04-17 06:13:15 +000017253 case ISD::SRL:
17254 case ISD::SUB:
17255 case ISD::ADD:
17256 case ISD::MUL:
17257 case ISD::AND:
17258 case ISD::OR:
17259 case ISD::XOR:
17260 return false;
17261 }
17262}
17263
17264/// IsDesirableToPromoteOp - This method query the target whether it is
Evan Cheng64b7bf72010-04-16 06:14:10 +000017265/// beneficial for dag combiner to promote the specified node. If true, it
17266/// should return the desired promotion type by reference.
Evan Chenge5b51ac2010-04-17 06:13:15 +000017267bool X86TargetLowering::IsDesirableToPromoteOp(SDValue Op, EVT &PVT) const {
Evan Cheng64b7bf72010-04-16 06:14:10 +000017268 EVT VT = Op.getValueType();
17269 if (VT != MVT::i16)
17270 return false;
17271
Evan Cheng4c26e932010-04-19 19:29:22 +000017272 bool Promote = false;
17273 bool Commute = false;
Evan Cheng64b7bf72010-04-16 06:14:10 +000017274 switch (Op.getOpcode()) {
Evan Cheng4c26e932010-04-19 19:29:22 +000017275 default: break;
17276 case ISD::LOAD: {
17277 LoadSDNode *LD = cast<LoadSDNode>(Op);
17278 // If the non-extending load has a single use and it's not live out, then it
17279 // might be folded.
Evan Cheng2bce5f4b2010-04-28 08:30:49 +000017280 if (LD->getExtensionType() == ISD::NON_EXTLOAD /*&&
17281 Op.hasOneUse()*/) {
17282 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
17283 UE = Op.getNode()->use_end(); UI != UE; ++UI) {
17284 // The only case where we'd want to promote LOAD (rather then it being
17285 // promoted as an operand is when it's only use is liveout.
17286 if (UI->getOpcode() != ISD::CopyToReg)
17287 return false;
17288 }
17289 }
Evan Cheng4c26e932010-04-19 19:29:22 +000017290 Promote = true;
17291 break;
17292 }
17293 case ISD::SIGN_EXTEND:
17294 case ISD::ZERO_EXTEND:
17295 case ISD::ANY_EXTEND:
17296 Promote = true;
17297 break;
Evan Chenge5b51ac2010-04-17 06:13:15 +000017298 case ISD::SHL:
Evan Cheng2bce5f4b2010-04-28 08:30:49 +000017299 case ISD::SRL: {
Evan Chenge5b51ac2010-04-17 06:13:15 +000017300 SDValue N0 = Op.getOperand(0);
17301 // Look out for (store (shl (load), x)).
Evan Chengc82c20b2010-04-24 04:44:57 +000017302 if (MayFoldLoad(N0) && MayFoldIntoStore(Op))
Evan Chenge5b51ac2010-04-17 06:13:15 +000017303 return false;
Evan Cheng4c26e932010-04-19 19:29:22 +000017304 Promote = true;
Evan Chenge5b51ac2010-04-17 06:13:15 +000017305 break;
17306 }
Evan Cheng64b7bf72010-04-16 06:14:10 +000017307 case ISD::ADD:
17308 case ISD::MUL:
17309 case ISD::AND:
17310 case ISD::OR:
Evan Cheng4c26e932010-04-19 19:29:22 +000017311 case ISD::XOR:
17312 Commute = true;
17313 // fallthrough
17314 case ISD::SUB: {
Evan Cheng64b7bf72010-04-16 06:14:10 +000017315 SDValue N0 = Op.getOperand(0);
17316 SDValue N1 = Op.getOperand(1);
Evan Chengc82c20b2010-04-24 04:44:57 +000017317 if (!Commute && MayFoldLoad(N1))
Evan Cheng64b7bf72010-04-16 06:14:10 +000017318 return false;
17319 // Avoid disabling potential load folding opportunities.
Evan Chengc82c20b2010-04-24 04:44:57 +000017320 if (MayFoldLoad(N0) && (!isa<ConstantSDNode>(N1) || MayFoldIntoStore(Op)))
Evan Cheng64b7bf72010-04-16 06:14:10 +000017321 return false;
Evan Chengc82c20b2010-04-24 04:44:57 +000017322 if (MayFoldLoad(N1) && (!isa<ConstantSDNode>(N0) || MayFoldIntoStore(Op)))
Evan Cheng64b7bf72010-04-16 06:14:10 +000017323 return false;
Evan Cheng4c26e932010-04-19 19:29:22 +000017324 Promote = true;
Evan Cheng64b7bf72010-04-16 06:14:10 +000017325 }
17326 }
17327
17328 PVT = MVT::i32;
Evan Cheng4c26e932010-04-19 19:29:22 +000017329 return Promote;
Evan Cheng64b7bf72010-04-16 06:14:10 +000017330}
17331
Evan Cheng60c07e12006-07-05 22:17:51 +000017332//===----------------------------------------------------------------------===//
17333// X86 Inline Assembly Support
17334//===----------------------------------------------------------------------===//
17335
Benjamin Kramer2ea4cdb2011-12-18 19:59:20 +000017336namespace {
17337 // Helper to match a string separated by whitespace.
Benjamin Kramer0581ed72011-12-18 20:51:31 +000017338 bool matchAsmImpl(StringRef s, ArrayRef<const StringRef *> args) {
Benjamin Kramer2ea4cdb2011-12-18 19:59:20 +000017339 s = s.substr(s.find_first_not_of(" \t")); // Skip leading whitespace.
Benjamin Kramere6cddb72011-12-17 14:36:05 +000017340
Benjamin Kramer0581ed72011-12-18 20:51:31 +000017341 for (unsigned i = 0, e = args.size(); i != e; ++i) {
Benjamin Kramer2ea4cdb2011-12-18 19:59:20 +000017342 StringRef piece(*args[i]);
17343 if (!s.startswith(piece)) // Check if the piece matches.
17344 return false;
17345
17346 s = s.substr(piece.size());
17347 StringRef::size_type pos = s.find_first_not_of(" \t");
17348 if (pos == 0) // We matched a prefix.
17349 return false;
17350
17351 s = s.substr(pos);
Benjamin Kramere6cddb72011-12-17 14:36:05 +000017352 }
17353
Benjamin Kramer2ea4cdb2011-12-18 19:59:20 +000017354 return s.empty();
Benjamin Kramere6cddb72011-12-17 14:36:05 +000017355 }
Benjamin Kramer0581ed72011-12-18 20:51:31 +000017356 const VariadicFunction1<bool, StringRef, StringRef, matchAsmImpl> matchAsm={};
Benjamin Kramere6cddb72011-12-17 14:36:05 +000017357}
17358
Chris Lattnerb8105652009-07-20 17:51:36 +000017359bool X86TargetLowering::ExpandInlineAsm(CallInst *CI) const {
17360 InlineAsm *IA = cast<InlineAsm>(CI->getCalledValue());
Chris Lattnerb8105652009-07-20 17:51:36 +000017361
17362 std::string AsmStr = IA->getAsmString();
17363
Benjamin Kramere6cddb72011-12-17 14:36:05 +000017364 IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
17365 if (!Ty || Ty->getBitWidth() % 16 != 0)
17366 return false;
17367
Chris Lattnerb8105652009-07-20 17:51:36 +000017368 // TODO: should remove alternatives from the asmstring: "foo {a|b}" -> "foo a"
Benjamin Kramerd4f19592010-01-11 18:03:24 +000017369 SmallVector<StringRef, 4> AsmPieces;
Peter Collingbourne98361182010-11-13 19:54:23 +000017370 SplitString(AsmStr, AsmPieces, ";\n");
Chris Lattnerb8105652009-07-20 17:51:36 +000017371
17372 switch (AsmPieces.size()) {
17373 default: return false;
17374 case 1:
Chris Lattner7a2bdde2011-04-15 05:18:47 +000017375 // FIXME: this should verify that we are targeting a 486 or better. If not,
Benjamin Kramere6cddb72011-12-17 14:36:05 +000017376 // we will turn this bswap into something that will be lowered to logical
17377 // ops instead of emitting the bswap asm. For now, we don't support 486 or
17378 // lower so don't worry about this.
Chris Lattnerb8105652009-07-20 17:51:36 +000017379 // bswap $0
Benjamin Kramer2ea4cdb2011-12-18 19:59:20 +000017380 if (matchAsm(AsmPieces[0], "bswap", "$0") ||
17381 matchAsm(AsmPieces[0], "bswapl", "$0") ||
17382 matchAsm(AsmPieces[0], "bswapq", "$0") ||
17383 matchAsm(AsmPieces[0], "bswap", "${0:q}") ||
17384 matchAsm(AsmPieces[0], "bswapl", "${0:q}") ||
17385 matchAsm(AsmPieces[0], "bswapq", "${0:q}")) {
Chris Lattnerb8105652009-07-20 17:51:36 +000017386 // No need to check constraints, nothing other than the equivalent of
17387 // "=r,0" would be valid here.
Evan Cheng55d42002011-01-08 01:24:27 +000017388 return IntrinsicLowering::LowerToByteSwap(CI);
Chris Lattnerb8105652009-07-20 17:51:36 +000017389 }
Benjamin Kramere6cddb72011-12-17 14:36:05 +000017390
Chris Lattnerb8105652009-07-20 17:51:36 +000017391 // rorw $$8, ${0:w} --> llvm.bswap.i16
Duncan Sandsb0bc6c32010-02-15 16:12:20 +000017392 if (CI->getType()->isIntegerTy(16) &&
Benjamin Kramere6cddb72011-12-17 14:36:05 +000017393 IA->getConstraintString().compare(0, 5, "=r,0,") == 0 &&
Benjamin Kramer2ea4cdb2011-12-18 19:59:20 +000017394 (matchAsm(AsmPieces[0], "rorw", "$$8,", "${0:w}") ||
17395 matchAsm(AsmPieces[0], "rolw", "$$8,", "${0:w}"))) {
Dan Gohman0ef701e2010-03-04 19:58:08 +000017396 AsmPieces.clear();
Evan Cheng55d42002011-01-08 01:24:27 +000017397 const std::string &ConstraintsStr = IA->getConstraintString();
17398 SplitString(StringRef(ConstraintsStr).substr(5), AsmPieces, ",");
Dan Gohman0ef701e2010-03-04 19:58:08 +000017399 std::sort(AsmPieces.begin(), AsmPieces.end());
17400 if (AsmPieces.size() == 4 &&
17401 AsmPieces[0] == "~{cc}" &&
17402 AsmPieces[1] == "~{dirflag}" &&
17403 AsmPieces[2] == "~{flags}" &&
Benjamin Kramere6cddb72011-12-17 14:36:05 +000017404 AsmPieces[3] == "~{fpsr}")
17405 return IntrinsicLowering::LowerToByteSwap(CI);
Chris Lattnerb8105652009-07-20 17:51:36 +000017406 }
17407 break;
17408 case 3:
Peter Collingbourne948cf022010-11-13 19:54:30 +000017409 if (CI->getType()->isIntegerTy(32) &&
Benjamin Kramere6cddb72011-12-17 14:36:05 +000017410 IA->getConstraintString().compare(0, 5, "=r,0,") == 0 &&
Benjamin Kramer2ea4cdb2011-12-18 19:59:20 +000017411 matchAsm(AsmPieces[0], "rorw", "$$8,", "${0:w}") &&
17412 matchAsm(AsmPieces[1], "rorl", "$$16,", "$0") &&
17413 matchAsm(AsmPieces[2], "rorw", "$$8,", "${0:w}")) {
Benjamin Kramere6cddb72011-12-17 14:36:05 +000017414 AsmPieces.clear();
17415 const std::string &ConstraintsStr = IA->getConstraintString();
17416 SplitString(StringRef(ConstraintsStr).substr(5), AsmPieces, ",");
17417 std::sort(AsmPieces.begin(), AsmPieces.end());
17418 if (AsmPieces.size() == 4 &&
17419 AsmPieces[0] == "~{cc}" &&
17420 AsmPieces[1] == "~{dirflag}" &&
17421 AsmPieces[2] == "~{flags}" &&
17422 AsmPieces[3] == "~{fpsr}")
17423 return IntrinsicLowering::LowerToByteSwap(CI);
Peter Collingbourne948cf022010-11-13 19:54:30 +000017424 }
Evan Cheng55d42002011-01-08 01:24:27 +000017425
17426 if (CI->getType()->isIntegerTy(64)) {
17427 InlineAsm::ConstraintInfoVector Constraints = IA->ParseConstraints();
17428 if (Constraints.size() >= 2 &&
17429 Constraints[0].Codes.size() == 1 && Constraints[0].Codes[0] == "A" &&
17430 Constraints[1].Codes.size() == 1 && Constraints[1].Codes[0] == "0") {
17431 // bswap %eax / bswap %edx / xchgl %eax, %edx -> llvm.bswap.i64
Benjamin Kramer2ea4cdb2011-12-18 19:59:20 +000017432 if (matchAsm(AsmPieces[0], "bswap", "%eax") &&
17433 matchAsm(AsmPieces[1], "bswap", "%edx") &&
17434 matchAsm(AsmPieces[2], "xchgl", "%eax,", "%edx"))
Benjamin Kramere6cddb72011-12-17 14:36:05 +000017435 return IntrinsicLowering::LowerToByteSwap(CI);
Chris Lattnerb8105652009-07-20 17:51:36 +000017436 }
17437 }
17438 break;
17439 }
17440 return false;
17441}
17442
Chris Lattnerf4dff842006-07-11 02:54:03 +000017443/// getConstraintType - Given a constraint letter, return the type of
17444/// constraint it is for this target.
17445X86TargetLowering::ConstraintType
Chris Lattner4234f572007-03-25 02:14:49 +000017446X86TargetLowering::getConstraintType(const std::string &Constraint) const {
17447 if (Constraint.size() == 1) {
17448 switch (Constraint[0]) {
Chris Lattner4234f572007-03-25 02:14:49 +000017449 case 'R':
Chris Lattner4234f572007-03-25 02:14:49 +000017450 case 'q':
17451 case 'Q':
John Thompson44ab89e2010-10-29 17:29:13 +000017452 case 'f':
17453 case 't':
17454 case 'u':
Dale Johannesen2ffbcac2008-04-01 00:57:48 +000017455 case 'y':
John Thompson44ab89e2010-10-29 17:29:13 +000017456 case 'x':
Chris Lattner4234f572007-03-25 02:14:49 +000017457 case 'Y':
Eric Christopher31b5f002011-07-07 22:29:07 +000017458 case 'l':
Chris Lattner4234f572007-03-25 02:14:49 +000017459 return C_RegisterClass;
John Thompson44ab89e2010-10-29 17:29:13 +000017460 case 'a':
17461 case 'b':
17462 case 'c':
17463 case 'd':
17464 case 'S':
17465 case 'D':
17466 case 'A':
17467 return C_Register;
17468 case 'I':
17469 case 'J':
17470 case 'K':
17471 case 'L':
17472 case 'M':
17473 case 'N':
17474 case 'G':
17475 case 'C':
Dale Johannesen78e3e522009-02-12 20:58:09 +000017476 case 'e':
17477 case 'Z':
17478 return C_Other;
Chris Lattner4234f572007-03-25 02:14:49 +000017479 default:
17480 break;
17481 }
Chris Lattnerf4dff842006-07-11 02:54:03 +000017482 }
Chris Lattner4234f572007-03-25 02:14:49 +000017483 return TargetLowering::getConstraintType(Constraint);
Chris Lattnerf4dff842006-07-11 02:54:03 +000017484}
17485
John Thompson44ab89e2010-10-29 17:29:13 +000017486/// Examine constraint type and operand type and determine a weight value.
John Thompsoneac6e1d2010-09-13 18:15:37 +000017487/// This object must already have been set up with the operand type
17488/// and the current alternative constraint selected.
John Thompson44ab89e2010-10-29 17:29:13 +000017489TargetLowering::ConstraintWeight
17490 X86TargetLowering::getSingleConstraintMatchWeight(
John Thompsoneac6e1d2010-09-13 18:15:37 +000017491 AsmOperandInfo &info, const char *constraint) const {
John Thompson44ab89e2010-10-29 17:29:13 +000017492 ConstraintWeight weight = CW_Invalid;
John Thompsoneac6e1d2010-09-13 18:15:37 +000017493 Value *CallOperandVal = info.CallOperandVal;
17494 // If we don't have a value, we can't do a match,
17495 // but allow it at the lowest weight.
17496 if (CallOperandVal == NULL)
John Thompson44ab89e2010-10-29 17:29:13 +000017497 return CW_Default;
Chris Lattnerdb125cf2011-07-18 04:54:35 +000017498 Type *type = CallOperandVal->getType();
John Thompsoneac6e1d2010-09-13 18:15:37 +000017499 // Look at the constraint type.
17500 switch (*constraint) {
17501 default:
John Thompson44ab89e2010-10-29 17:29:13 +000017502 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
17503 case 'R':
17504 case 'q':
17505 case 'Q':
17506 case 'a':
17507 case 'b':
17508 case 'c':
17509 case 'd':
17510 case 'S':
17511 case 'D':
17512 case 'A':
17513 if (CallOperandVal->getType()->isIntegerTy())
17514 weight = CW_SpecificReg;
17515 break;
17516 case 'f':
17517 case 't':
17518 case 'u':
17519 if (type->isFloatingPointTy())
17520 weight = CW_SpecificReg;
17521 break;
17522 case 'y':
Chris Lattner2a786eb2010-12-19 20:19:20 +000017523 if (type->isX86_MMXTy() && Subtarget->hasMMX())
John Thompson44ab89e2010-10-29 17:29:13 +000017524 weight = CW_SpecificReg;
17525 break;
17526 case 'x':
17527 case 'Y':
Craig Topper1accb7e2012-01-10 06:54:16 +000017528 if (((type->getPrimitiveSizeInBits() == 128) && Subtarget->hasSSE1()) ||
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000017529 ((type->getPrimitiveSizeInBits() == 256) && Subtarget->hasFp256()))
John Thompson44ab89e2010-10-29 17:29:13 +000017530 weight = CW_Register;
John Thompsoneac6e1d2010-09-13 18:15:37 +000017531 break;
17532 case 'I':
17533 if (ConstantInt *C = dyn_cast<ConstantInt>(info.CallOperandVal)) {
17534 if (C->getZExtValue() <= 31)
John Thompson44ab89e2010-10-29 17:29:13 +000017535 weight = CW_Constant;
John Thompsoneac6e1d2010-09-13 18:15:37 +000017536 }
17537 break;
John Thompson44ab89e2010-10-29 17:29:13 +000017538 case 'J':
17539 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
17540 if (C->getZExtValue() <= 63)
17541 weight = CW_Constant;
17542 }
17543 break;
17544 case 'K':
17545 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
17546 if ((C->getSExtValue() >= -0x80) && (C->getSExtValue() <= 0x7f))
17547 weight = CW_Constant;
17548 }
17549 break;
17550 case 'L':
17551 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
17552 if ((C->getZExtValue() == 0xff) || (C->getZExtValue() == 0xffff))
17553 weight = CW_Constant;
17554 }
17555 break;
17556 case 'M':
17557 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
17558 if (C->getZExtValue() <= 3)
17559 weight = CW_Constant;
17560 }
17561 break;
17562 case 'N':
17563 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
17564 if (C->getZExtValue() <= 0xff)
17565 weight = CW_Constant;
17566 }
17567 break;
17568 case 'G':
17569 case 'C':
17570 if (dyn_cast<ConstantFP>(CallOperandVal)) {
17571 weight = CW_Constant;
17572 }
17573 break;
17574 case 'e':
17575 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
17576 if ((C->getSExtValue() >= -0x80000000LL) &&
17577 (C->getSExtValue() <= 0x7fffffffLL))
17578 weight = CW_Constant;
17579 }
17580 break;
17581 case 'Z':
17582 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
17583 if (C->getZExtValue() <= 0xffffffff)
17584 weight = CW_Constant;
17585 }
17586 break;
John Thompsoneac6e1d2010-09-13 18:15:37 +000017587 }
17588 return weight;
17589}
17590
Dale Johannesenba2a0b92008-01-29 02:21:21 +000017591/// LowerXConstraint - try to replace an X constraint, which matches anything,
17592/// with another that has more specific requirements based on the type of the
17593/// corresponding operand.
Chris Lattner5e764232008-04-26 23:02:14 +000017594const char *X86TargetLowering::
Owen Andersone50ed302009-08-10 22:56:29 +000017595LowerXConstraint(EVT ConstraintVT) const {
Chris Lattner5e764232008-04-26 23:02:14 +000017596 // FP X constraints get lowered to SSE1/2 registers if available, otherwise
17597 // 'f' like normal targets.
Duncan Sands83ec4b62008-06-06 12:08:01 +000017598 if (ConstraintVT.isFloatingPoint()) {
Craig Topper1accb7e2012-01-10 06:54:16 +000017599 if (Subtarget->hasSSE2())
Chris Lattner5e764232008-04-26 23:02:14 +000017600 return "Y";
Craig Topper1accb7e2012-01-10 06:54:16 +000017601 if (Subtarget->hasSSE1())
Chris Lattner5e764232008-04-26 23:02:14 +000017602 return "x";
17603 }
Scott Michelfdc40a02009-02-17 22:15:04 +000017604
Chris Lattner5e764232008-04-26 23:02:14 +000017605 return TargetLowering::LowerXConstraint(ConstraintVT);
Dale Johannesenba2a0b92008-01-29 02:21:21 +000017606}
17607
Chris Lattner48884cd2007-08-25 00:47:38 +000017608/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
17609/// vector. If it is invalid, don't add anything to Ops.
Dan Gohman475871a2008-07-27 21:46:04 +000017610void X86TargetLowering::LowerAsmOperandForConstraint(SDValue Op,
Eric Christopher100c8332011-06-02 23:16:42 +000017611 std::string &Constraint,
Dan Gohman475871a2008-07-27 21:46:04 +000017612 std::vector<SDValue>&Ops,
Chris Lattner5e764232008-04-26 23:02:14 +000017613 SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +000017614 SDValue Result(0, 0);
Scott Michelfdc40a02009-02-17 22:15:04 +000017615
Eric Christopher100c8332011-06-02 23:16:42 +000017616 // Only support length 1 constraints for now.
17617 if (Constraint.length() > 1) return;
Eric Christopher471e4222011-06-08 23:55:35 +000017618
Eric Christopher100c8332011-06-02 23:16:42 +000017619 char ConstraintLetter = Constraint[0];
17620 switch (ConstraintLetter) {
Chris Lattner22aaf1d2006-10-31 20:13:11 +000017621 default: break;
Devang Patel84f7fd22007-03-17 00:13:28 +000017622 case 'I':
Chris Lattner188b9fe2007-03-25 01:57:35 +000017623 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +000017624 if (C->getZExtValue() <= 31) {
17625 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattner48884cd2007-08-25 00:47:38 +000017626 break;
17627 }
Devang Patel84f7fd22007-03-17 00:13:28 +000017628 }
Chris Lattner48884cd2007-08-25 00:47:38 +000017629 return;
Evan Cheng364091e2008-09-22 23:57:37 +000017630 case 'J':
17631 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattner2e06dd22009-06-15 04:39:05 +000017632 if (C->getZExtValue() <= 63) {
Chris Lattnere4935152009-06-15 04:01:39 +000017633 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
17634 break;
17635 }
17636 }
17637 return;
17638 case 'K':
17639 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Jakub Staszakdccd7f92012-11-06 23:52:19 +000017640 if (isInt<8>(C->getSExtValue())) {
Evan Cheng364091e2008-09-22 23:57:37 +000017641 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
17642 break;
17643 }
17644 }
17645 return;
Chris Lattner188b9fe2007-03-25 01:57:35 +000017646 case 'N':
17647 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +000017648 if (C->getZExtValue() <= 255) {
17649 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattner48884cd2007-08-25 00:47:38 +000017650 break;
17651 }
Chris Lattner188b9fe2007-03-25 01:57:35 +000017652 }
Chris Lattner48884cd2007-08-25 00:47:38 +000017653 return;
Dale Johannesen78e3e522009-02-12 20:58:09 +000017654 case 'e': {
17655 // 32-bit signed value
17656 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohman7720cb32010-06-18 14:01:07 +000017657 if (ConstantInt::isValueValidForType(Type::getInt32Ty(*DAG.getContext()),
17658 C->getSExtValue())) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000017659 // Widen to 64 bits here to get it sign extended.
Owen Anderson825b72b2009-08-11 20:47:22 +000017660 Result = DAG.getTargetConstant(C->getSExtValue(), MVT::i64);
Dale Johannesen78e3e522009-02-12 20:58:09 +000017661 break;
17662 }
17663 // FIXME gcc accepts some relocatable values here too, but only in certain
17664 // memory models; it's complicated.
17665 }
17666 return;
17667 }
17668 case 'Z': {
17669 // 32-bit unsigned value
17670 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohman7720cb32010-06-18 14:01:07 +000017671 if (ConstantInt::isValueValidForType(Type::getInt32Ty(*DAG.getContext()),
17672 C->getZExtValue())) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000017673 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
17674 break;
17675 }
17676 }
17677 // FIXME gcc accepts some relocatable values here too, but only in certain
17678 // memory models; it's complicated.
17679 return;
17680 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000017681 case 'i': {
Chris Lattner22aaf1d2006-10-31 20:13:11 +000017682 // Literal immediates are always ok.
Chris Lattner48884cd2007-08-25 00:47:38 +000017683 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Op)) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000017684 // Widen to 64 bits here to get it sign extended.
Owen Anderson825b72b2009-08-11 20:47:22 +000017685 Result = DAG.getTargetConstant(CST->getSExtValue(), MVT::i64);
Chris Lattner48884cd2007-08-25 00:47:38 +000017686 break;
17687 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000017688
Dale Johannesene5ff9ef2010-06-24 20:14:51 +000017689 // In any sort of PIC mode addresses need to be computed at runtime by
17690 // adding in a register or some sort of table lookup. These can't
17691 // be used as immediates.
Dale Johannesene2b448c2010-07-06 23:27:00 +000017692 if (Subtarget->isPICStyleGOT() || Subtarget->isPICStyleStubPIC())
Dale Johannesene5ff9ef2010-06-24 20:14:51 +000017693 return;
17694
Chris Lattnerdc43a882007-05-03 16:52:29 +000017695 // If we are in non-pic codegen mode, we allow the address of a global (with
17696 // an optional displacement) to be used with 'i'.
Chris Lattner49921962009-05-08 18:23:14 +000017697 GlobalAddressSDNode *GA = 0;
Chris Lattnerdc43a882007-05-03 16:52:29 +000017698 int64_t Offset = 0;
Scott Michelfdc40a02009-02-17 22:15:04 +000017699
Chris Lattner49921962009-05-08 18:23:14 +000017700 // Match either (GA), (GA+C), (GA+C1+C2), etc.
17701 while (1) {
17702 if ((GA = dyn_cast<GlobalAddressSDNode>(Op))) {
17703 Offset += GA->getOffset();
17704 break;
17705 } else if (Op.getOpcode() == ISD::ADD) {
17706 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
17707 Offset += C->getZExtValue();
17708 Op = Op.getOperand(0);
17709 continue;
17710 }
17711 } else if (Op.getOpcode() == ISD::SUB) {
17712 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
17713 Offset += -C->getZExtValue();
17714 Op = Op.getOperand(0);
17715 continue;
17716 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000017717 }
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000017718
Chris Lattner49921962009-05-08 18:23:14 +000017719 // Otherwise, this isn't something we can handle, reject it.
17720 return;
Chris Lattnerdc43a882007-05-03 16:52:29 +000017721 }
Eric Christopherfd179292009-08-27 18:07:15 +000017722
Dan Gohman46510a72010-04-15 01:51:59 +000017723 const GlobalValue *GV = GA->getGlobal();
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000017724 // If we require an extra load to get this address, as in PIC mode, we
17725 // can't accept it.
Chris Lattner36c25012009-07-10 07:34:39 +000017726 if (isGlobalStubReference(Subtarget->ClassifyGlobalReference(GV,
17727 getTargetMachine())))
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000017728 return;
Scott Michelfdc40a02009-02-17 22:15:04 +000017729
Devang Patel0d881da2010-07-06 22:08:15 +000017730 Result = DAG.getTargetGlobalAddress(GV, Op.getDebugLoc(),
17731 GA->getValueType(0), Offset);
Chris Lattner49921962009-05-08 18:23:14 +000017732 break;
Chris Lattner22aaf1d2006-10-31 20:13:11 +000017733 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000017734 }
Scott Michelfdc40a02009-02-17 22:15:04 +000017735
Gabor Greifba36cb52008-08-28 21:40:38 +000017736 if (Result.getNode()) {
Chris Lattner48884cd2007-08-25 00:47:38 +000017737 Ops.push_back(Result);
17738 return;
17739 }
Dale Johannesen1784d162010-06-25 21:55:36 +000017740 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
Chris Lattner22aaf1d2006-10-31 20:13:11 +000017741}
17742
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000017743std::pair<unsigned, const TargetRegisterClass*>
Chris Lattnerf76d1802006-07-31 23:26:50 +000017744X86TargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +000017745 EVT VT) const {
Chris Lattnerad043e82007-04-09 05:11:28 +000017746 // First, see if this is a constraint that directly corresponds to an LLVM
17747 // register class.
17748 if (Constraint.size() == 1) {
17749 // GCC Constraint Letters
17750 switch (Constraint[0]) {
17751 default: break;
Eric Christopherd176af82011-06-29 17:23:50 +000017752 // TODO: Slight differences here in allocation order and leaving
17753 // RIP in the class. Do they matter any more here than they do
17754 // in the normal allocation?
17755 case 'q': // GENERAL_REGS in 64-bit mode, Q_REGS in 32-bit mode.
17756 if (Subtarget->is64Bit()) {
Craig Topperc9099502012-04-20 06:31:50 +000017757 if (VT == MVT::i32 || VT == MVT::f32)
17758 return std::make_pair(0U, &X86::GR32RegClass);
17759 if (VT == MVT::i16)
17760 return std::make_pair(0U, &X86::GR16RegClass);
17761 if (VT == MVT::i8 || VT == MVT::i1)
17762 return std::make_pair(0U, &X86::GR8RegClass);
17763 if (VT == MVT::i64 || VT == MVT::f64)
17764 return std::make_pair(0U, &X86::GR64RegClass);
17765 break;
Eric Christopherd176af82011-06-29 17:23:50 +000017766 }
17767 // 32-bit fallthrough
17768 case 'Q': // Q_REGS
Nick Lewycky9bf45d02011-07-08 00:19:27 +000017769 if (VT == MVT::i32 || VT == MVT::f32)
Craig Topperc9099502012-04-20 06:31:50 +000017770 return std::make_pair(0U, &X86::GR32_ABCDRegClass);
17771 if (VT == MVT::i16)
17772 return std::make_pair(0U, &X86::GR16_ABCDRegClass);
17773 if (VT == MVT::i8 || VT == MVT::i1)
17774 return std::make_pair(0U, &X86::GR8_ABCD_LRegClass);
17775 if (VT == MVT::i64)
17776 return std::make_pair(0U, &X86::GR64_ABCDRegClass);
Eric Christopherd176af82011-06-29 17:23:50 +000017777 break;
Chris Lattner0f65cad2007-04-09 05:49:22 +000017778 case 'r': // GENERAL_REGS
Chris Lattner0f65cad2007-04-09 05:49:22 +000017779 case 'l': // INDEX_REGS
Eric Christopher5427ede2011-07-14 20:13:52 +000017780 if (VT == MVT::i8 || VT == MVT::i1)
Craig Topperc9099502012-04-20 06:31:50 +000017781 return std::make_pair(0U, &X86::GR8RegClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000017782 if (VT == MVT::i16)
Craig Topperc9099502012-04-20 06:31:50 +000017783 return std::make_pair(0U, &X86::GR16RegClass);
Eric Christopher2bbecd82011-05-19 21:33:47 +000017784 if (VT == MVT::i32 || VT == MVT::f32 || !Subtarget->is64Bit())
Craig Topperc9099502012-04-20 06:31:50 +000017785 return std::make_pair(0U, &X86::GR32RegClass);
17786 return std::make_pair(0U, &X86::GR64RegClass);
Dale Johannesen5f3663e2009-10-07 22:47:20 +000017787 case 'R': // LEGACY_REGS
Eric Christopher5427ede2011-07-14 20:13:52 +000017788 if (VT == MVT::i8 || VT == MVT::i1)
Craig Topperc9099502012-04-20 06:31:50 +000017789 return std::make_pair(0U, &X86::GR8_NOREXRegClass);
Dale Johannesen5f3663e2009-10-07 22:47:20 +000017790 if (VT == MVT::i16)
Craig Topperc9099502012-04-20 06:31:50 +000017791 return std::make_pair(0U, &X86::GR16_NOREXRegClass);
Dale Johannesen5f3663e2009-10-07 22:47:20 +000017792 if (VT == MVT::i32 || !Subtarget->is64Bit())
Craig Topperc9099502012-04-20 06:31:50 +000017793 return std::make_pair(0U, &X86::GR32_NOREXRegClass);
17794 return std::make_pair(0U, &X86::GR64_NOREXRegClass);
Chris Lattnerfce84ac2008-03-11 19:06:29 +000017795 case 'f': // FP Stack registers.
17796 // If SSE is enabled for this VT, use f80 to ensure the isel moves the
17797 // value to the correct fpstack register class.
Owen Anderson825b72b2009-08-11 20:47:22 +000017798 if (VT == MVT::f32 && !isScalarFPTypeInSSEReg(VT))
Craig Topperc9099502012-04-20 06:31:50 +000017799 return std::make_pair(0U, &X86::RFP32RegClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000017800 if (VT == MVT::f64 && !isScalarFPTypeInSSEReg(VT))
Craig Topperc9099502012-04-20 06:31:50 +000017801 return std::make_pair(0U, &X86::RFP64RegClass);
17802 return std::make_pair(0U, &X86::RFP80RegClass);
Chris Lattner6c284d72007-04-12 04:14:49 +000017803 case 'y': // MMX_REGS if MMX allowed.
17804 if (!Subtarget->hasMMX()) break;
Craig Topperc9099502012-04-20 06:31:50 +000017805 return std::make_pair(0U, &X86::VR64RegClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +000017806 case 'Y': // SSE_REGS if SSE2 allowed
Craig Topper1accb7e2012-01-10 06:54:16 +000017807 if (!Subtarget->hasSSE2()) break;
Chris Lattner0f65cad2007-04-09 05:49:22 +000017808 // FALL THROUGH.
Eric Christopher55487552012-01-07 01:02:09 +000017809 case 'x': // SSE_REGS if SSE1 allowed or AVX_REGS if AVX allowed
Craig Topper1accb7e2012-01-10 06:54:16 +000017810 if (!Subtarget->hasSSE1()) break;
Duncan Sands83ec4b62008-06-06 12:08:01 +000017811
Owen Anderson825b72b2009-08-11 20:47:22 +000017812 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner0f65cad2007-04-09 05:49:22 +000017813 default: break;
17814 // Scalar SSE types.
Owen Anderson825b72b2009-08-11 20:47:22 +000017815 case MVT::f32:
17816 case MVT::i32:
Craig Topperc9099502012-04-20 06:31:50 +000017817 return std::make_pair(0U, &X86::FR32RegClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000017818 case MVT::f64:
17819 case MVT::i64:
Craig Topperc9099502012-04-20 06:31:50 +000017820 return std::make_pair(0U, &X86::FR64RegClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +000017821 // Vector types.
Owen Anderson825b72b2009-08-11 20:47:22 +000017822 case MVT::v16i8:
17823 case MVT::v8i16:
17824 case MVT::v4i32:
17825 case MVT::v2i64:
17826 case MVT::v4f32:
17827 case MVT::v2f64:
Craig Topperc9099502012-04-20 06:31:50 +000017828 return std::make_pair(0U, &X86::VR128RegClass);
Eric Christopher55487552012-01-07 01:02:09 +000017829 // AVX types.
17830 case MVT::v32i8:
17831 case MVT::v16i16:
17832 case MVT::v8i32:
17833 case MVT::v4i64:
17834 case MVT::v8f32:
17835 case MVT::v4f64:
Craig Topperc9099502012-04-20 06:31:50 +000017836 return std::make_pair(0U, &X86::VR256RegClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +000017837 }
Chris Lattnerad043e82007-04-09 05:11:28 +000017838 break;
17839 }
17840 }
Scott Michelfdc40a02009-02-17 22:15:04 +000017841
Chris Lattnerf76d1802006-07-31 23:26:50 +000017842 // Use the default implementation in TargetLowering to convert the register
17843 // constraint into a member of a register class.
17844 std::pair<unsigned, const TargetRegisterClass*> Res;
17845 Res = TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
Chris Lattner1a60aa72006-10-31 19:42:44 +000017846
17847 // Not found as a standard register?
17848 if (Res.second == 0) {
Chris Lattner56d77c72009-09-13 22:41:48 +000017849 // Map st(0) -> st(7) -> ST0
17850 if (Constraint.size() == 7 && Constraint[0] == '{' &&
17851 tolower(Constraint[1]) == 's' &&
17852 tolower(Constraint[2]) == 't' &&
17853 Constraint[3] == '(' &&
17854 (Constraint[4] >= '0' && Constraint[4] <= '7') &&
17855 Constraint[5] == ')' &&
17856 Constraint[6] == '}') {
Daniel Dunbara279bc32009-09-20 02:20:51 +000017857
Chris Lattner56d77c72009-09-13 22:41:48 +000017858 Res.first = X86::ST0+Constraint[4]-'0';
Craig Topperc9099502012-04-20 06:31:50 +000017859 Res.second = &X86::RFP80RegClass;
Chris Lattner56d77c72009-09-13 22:41:48 +000017860 return Res;
17861 }
Daniel Dunbara279bc32009-09-20 02:20:51 +000017862
Chris Lattner56d77c72009-09-13 22:41:48 +000017863 // GCC allows "st(0)" to be called just plain "st".
Benjamin Kramer05872ea2009-11-12 20:36:59 +000017864 if (StringRef("{st}").equals_lower(Constraint)) {
Chris Lattner1a60aa72006-10-31 19:42:44 +000017865 Res.first = X86::ST0;
Craig Topperc9099502012-04-20 06:31:50 +000017866 Res.second = &X86::RFP80RegClass;
Chris Lattner56d77c72009-09-13 22:41:48 +000017867 return Res;
Chris Lattner1a60aa72006-10-31 19:42:44 +000017868 }
Chris Lattner56d77c72009-09-13 22:41:48 +000017869
17870 // flags -> EFLAGS
Benjamin Kramer05872ea2009-11-12 20:36:59 +000017871 if (StringRef("{flags}").equals_lower(Constraint)) {
Chris Lattner56d77c72009-09-13 22:41:48 +000017872 Res.first = X86::EFLAGS;
Craig Topperc9099502012-04-20 06:31:50 +000017873 Res.second = &X86::CCRRegClass;
Chris Lattner56d77c72009-09-13 22:41:48 +000017874 return Res;
17875 }
Daniel Dunbara279bc32009-09-20 02:20:51 +000017876
Dale Johannesen330169f2008-11-13 21:52:36 +000017877 // 'A' means EAX + EDX.
17878 if (Constraint == "A") {
17879 Res.first = X86::EAX;
Craig Topperc9099502012-04-20 06:31:50 +000017880 Res.second = &X86::GR32_ADRegClass;
Chris Lattner56d77c72009-09-13 22:41:48 +000017881 return Res;
Dale Johannesen330169f2008-11-13 21:52:36 +000017882 }
Chris Lattner1a60aa72006-10-31 19:42:44 +000017883 return Res;
17884 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000017885
Chris Lattnerf76d1802006-07-31 23:26:50 +000017886 // Otherwise, check to see if this is a register class of the wrong value
17887 // type. For example, we want to map "{ax},i32" -> {eax}, we don't want it to
17888 // turn into {ax},{dx}.
17889 if (Res.second->hasType(VT))
17890 return Res; // Correct type already, nothing to do.
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000017891
Chris Lattnerf76d1802006-07-31 23:26:50 +000017892 // All of the single-register GCC register classes map their values onto
17893 // 16-bit register pieces "ax","dx","cx","bx","si","di","bp","sp". If we
17894 // really want an 8-bit or 32-bit register, map to the appropriate register
17895 // class and return the appropriate register.
Craig Topperc9099502012-04-20 06:31:50 +000017896 if (Res.second == &X86::GR16RegClass) {
Owen Anderson825b72b2009-08-11 20:47:22 +000017897 if (VT == MVT::i8) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000017898 unsigned DestReg = 0;
17899 switch (Res.first) {
17900 default: break;
17901 case X86::AX: DestReg = X86::AL; break;
17902 case X86::DX: DestReg = X86::DL; break;
17903 case X86::CX: DestReg = X86::CL; break;
17904 case X86::BX: DestReg = X86::BL; break;
17905 }
17906 if (DestReg) {
17907 Res.first = DestReg;
Craig Topperc9099502012-04-20 06:31:50 +000017908 Res.second = &X86::GR8RegClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000017909 }
Owen Anderson825b72b2009-08-11 20:47:22 +000017910 } else if (VT == MVT::i32) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000017911 unsigned DestReg = 0;
17912 switch (Res.first) {
17913 default: break;
17914 case X86::AX: DestReg = X86::EAX; break;
17915 case X86::DX: DestReg = X86::EDX; break;
17916 case X86::CX: DestReg = X86::ECX; break;
17917 case X86::BX: DestReg = X86::EBX; break;
17918 case X86::SI: DestReg = X86::ESI; break;
17919 case X86::DI: DestReg = X86::EDI; break;
17920 case X86::BP: DestReg = X86::EBP; break;
17921 case X86::SP: DestReg = X86::ESP; break;
17922 }
17923 if (DestReg) {
17924 Res.first = DestReg;
Craig Topperc9099502012-04-20 06:31:50 +000017925 Res.second = &X86::GR32RegClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000017926 }
Owen Anderson825b72b2009-08-11 20:47:22 +000017927 } else if (VT == MVT::i64) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000017928 unsigned DestReg = 0;
17929 switch (Res.first) {
17930 default: break;
17931 case X86::AX: DestReg = X86::RAX; break;
17932 case X86::DX: DestReg = X86::RDX; break;
17933 case X86::CX: DestReg = X86::RCX; break;
17934 case X86::BX: DestReg = X86::RBX; break;
17935 case X86::SI: DestReg = X86::RSI; break;
17936 case X86::DI: DestReg = X86::RDI; break;
17937 case X86::BP: DestReg = X86::RBP; break;
17938 case X86::SP: DestReg = X86::RSP; break;
17939 }
17940 if (DestReg) {
17941 Res.first = DestReg;
Craig Topperc9099502012-04-20 06:31:50 +000017942 Res.second = &X86::GR64RegClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000017943 }
Chris Lattnerf76d1802006-07-31 23:26:50 +000017944 }
Craig Topperc9099502012-04-20 06:31:50 +000017945 } else if (Res.second == &X86::FR32RegClass ||
17946 Res.second == &X86::FR64RegClass ||
17947 Res.second == &X86::VR128RegClass) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000017948 // Handle references to XMM physical registers that got mapped into the
17949 // wrong class. This can happen with constraints like {xmm0} where the
17950 // target independent register mapper will just pick the first match it can
17951 // find, ignoring the required type.
Eli Friedman52d418d2012-06-25 23:42:33 +000017952
17953 if (VT == MVT::f32 || VT == MVT::i32)
Craig Topperc9099502012-04-20 06:31:50 +000017954 Res.second = &X86::FR32RegClass;
Eli Friedman52d418d2012-06-25 23:42:33 +000017955 else if (VT == MVT::f64 || VT == MVT::i64)
Craig Topperc9099502012-04-20 06:31:50 +000017956 Res.second = &X86::FR64RegClass;
17957 else if (X86::VR128RegClass.hasType(VT))
17958 Res.second = &X86::VR128RegClass;
Eli Friedman52d418d2012-06-25 23:42:33 +000017959 else if (X86::VR256RegClass.hasType(VT))
17960 Res.second = &X86::VR256RegClass;
Chris Lattnerf76d1802006-07-31 23:26:50 +000017961 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000017962
Chris Lattnerf76d1802006-07-31 23:26:50 +000017963 return Res;
17964}
Nadav Rotemb4b04c32012-11-03 00:39:56 +000017965
Nadav Roteme6237022012-11-05 19:32:46 +000017966//===----------------------------------------------------------------------===//
17967//
17968// X86 cost model.
17969//
17970//===----------------------------------------------------------------------===//
17971
17972struct X86CostTblEntry {
17973 int ISD;
17974 MVT Type;
17975 unsigned Cost;
17976};
17977
Nadav Rotemd8eae8b2012-11-06 23:36:00 +000017978static int
17979FindInTable(const X86CostTblEntry *Tbl, unsigned len, int ISD, MVT Ty) {
Nadav Rotem7ae3bcc2012-11-05 23:48:20 +000017980 for (unsigned int i = 0; i < len; ++i)
17981 if (Tbl[i].ISD == ISD && Tbl[i].Type == Ty)
17982 return i;
17983
17984 // Could not find an entry.
17985 return -1;
17986}
17987
Nadav Rotemb0428682012-11-06 19:33:53 +000017988struct X86TypeConversionCostTblEntry {
17989 int ISD;
17990 MVT Dst;
17991 MVT Src;
17992 unsigned Cost;
17993};
17994
Nadav Rotemd8eae8b2012-11-06 23:36:00 +000017995static int
17996FindInConvertTable(const X86TypeConversionCostTblEntry *Tbl, unsigned len,
17997 int ISD, MVT Dst, MVT Src) {
Nadav Rotemb0428682012-11-06 19:33:53 +000017998 for (unsigned int i = 0; i < len; ++i)
17999 if (Tbl[i].ISD == ISD && Tbl[i].Src == Src && Tbl[i].Dst == Dst)
18000 return i;
18001
18002 // Could not find an entry.
18003 return -1;
18004}
18005
Shuxin Yang5518a132012-12-09 03:12:46 +000018006ScalarTargetTransformInfo::PopcntHwSupport
18007X86ScalarTargetTransformImpl::getPopcntHwSupport(unsigned TyWidth) const {
18008 assert(isPowerOf2_32(TyWidth) && "Ty width must be power of 2");
18009 const X86Subtarget &ST = TLI->getTargetMachine().getSubtarget<X86Subtarget>();
18010
18011 // TODO: Currently the __builtin_popcount() implementation using SSE3
18012 // instructions is inefficient. Once the problem is fixed, we should
18013 // call ST.hasSSE3() instead of ST.hasSSE4().
18014 return ST.hasSSE41() ? Fast : None;
18015}
18016
Nadav Rotemb4b04c32012-11-03 00:39:56 +000018017unsigned
18018X86VectorTargetTransformInfo::getArithmeticInstrCost(unsigned Opcode,
18019 Type *Ty) const {
Nadav Roteme6237022012-11-05 19:32:46 +000018020 // Legalize the type.
Nadav Rotem887c1fe2012-11-05 23:57:45 +000018021 std::pair<unsigned, MVT> LT = getTypeLegalizationCost(Ty);
Nadav Roteme6237022012-11-05 19:32:46 +000018022
18023 int ISD = InstructionOpcodeToISD(Opcode);
18024 assert(ISD && "Invalid opcode");
18025
Nadav Rotemb0428682012-11-06 19:33:53 +000018026 const X86Subtarget &ST = TLI->getTargetMachine().getSubtarget<X86Subtarget>();
Nadav Rotemb4b04c32012-11-03 00:39:56 +000018027
Nadav Roteme6237022012-11-05 19:32:46 +000018028 static const X86CostTblEntry AVX1CostTable[] = {
18029 // We don't have to scalarize unsupported ops. We can issue two half-sized
18030 // operations and we only need to extract the upper YMM half.
18031 // Two ops + 1 extract + 1 insert = 4.
18032 { ISD::MUL, MVT::v8i32, 4 },
18033 { ISD::SUB, MVT::v8i32, 4 },
18034 { ISD::ADD, MVT::v8i32, 4 },
18035 { ISD::MUL, MVT::v4i64, 4 },
18036 { ISD::SUB, MVT::v4i64, 4 },
18037 { ISD::ADD, MVT::v4i64, 4 },
18038 };
Nadav Rotemb4b04c32012-11-03 00:39:56 +000018039
Nadav Roteme6237022012-11-05 19:32:46 +000018040 // Look for AVX1 lowering tricks.
Nadav Rotem7ae3bcc2012-11-05 23:48:20 +000018041 if (ST.hasAVX()) {
18042 int Idx = FindInTable(AVX1CostTable, array_lengthof(AVX1CostTable), ISD,
18043 LT.second);
18044 if (Idx != -1)
18045 return LT.first * AVX1CostTable[Idx].Cost;
18046 }
Nadav Roteme6237022012-11-05 19:32:46 +000018047 // Fallback to the default implementation.
Nadav Rotemb4b04c32012-11-03 00:39:56 +000018048 return VectorTargetTransformImpl::getArithmeticInstrCost(Opcode, Ty);
18049}
18050
Nadav Rotemf5637c32012-12-21 01:33:59 +000018051unsigned
18052X86VectorTargetTransformInfo::getMemoryOpCost(unsigned Opcode, Type *Src,
18053 unsigned Alignment,
18054 unsigned AddressSpace) const {
18055 // Legalize the type.
18056 std::pair<unsigned, MVT> LT = getTypeLegalizationCost(Src);
Nick Lewycky71f30bf2012-12-24 19:58:45 +000018057 assert((Opcode == Instruction::Load || Opcode == Instruction::Store) &&
Nadav Rotemf5637c32012-12-21 01:33:59 +000018058 "Invalid Opcode");
18059
18060 const X86Subtarget &ST =
18061 TLI->getTargetMachine().getSubtarget<X86Subtarget>();
18062
18063 // Each load/store unit costs 1.
18064 unsigned Cost = LT.first * 1;
18065
18066 // On Sandybridge 256bit load/stores are double pumped
18067 // (but not on Haswell).
18068 if (LT.second.getSizeInBits() > 128 && !ST.hasAVX2())
18069 Cost*=2;
18070
18071 return Cost;
18072}
18073
Nadav Rotemb4b04c32012-11-03 00:39:56 +000018074unsigned
18075X86VectorTargetTransformInfo::getVectorInstrCost(unsigned Opcode, Type *Val,
Richard Smithe010eb32012-11-05 22:01:44 +000018076 unsigned Index) const {
Nadav Rotema4ab5292012-11-05 21:12:13 +000018077 assert(Val->isVectorTy() && "This must be a vector type");
18078
Nadav Rotem7ae3bcc2012-11-05 23:48:20 +000018079 if (Index != -1U) {
Nadav Rotema4ab5292012-11-05 21:12:13 +000018080 // Legalize the type.
Nadav Rotem887c1fe2012-11-05 23:57:45 +000018081 std::pair<unsigned, MVT> LT = getTypeLegalizationCost(Val);
Nadav Rotema4ab5292012-11-05 21:12:13 +000018082
18083 // This type is legalized to a scalar type.
18084 if (!LT.second.isVector())
18085 return 0;
18086
18087 // The type may be split. Normalize the index to the new type.
18088 unsigned Width = LT.second.getVectorNumElements();
18089 Index = Index % Width;
18090
18091 // Floating point scalars are already located in index #0.
18092 if (Val->getScalarType()->isFloatingPointTy() && Index == 0)
18093 return 0;
18094 }
18095
Nadav Rotemb4b04c32012-11-03 00:39:56 +000018096 return VectorTargetTransformImpl::getVectorInstrCost(Opcode, Val, Index);
18097}
18098
Nadav Rotem7ae3bcc2012-11-05 23:48:20 +000018099unsigned X86VectorTargetTransformInfo::getCmpSelInstrCost(unsigned Opcode,
18100 Type *ValTy,
18101 Type *CondTy) const {
18102 // Legalize the type.
Nadav Rotem887c1fe2012-11-05 23:57:45 +000018103 std::pair<unsigned, MVT> LT = getTypeLegalizationCost(ValTy);
Nadav Rotem7ae3bcc2012-11-05 23:48:20 +000018104
18105 MVT MTy = LT.second;
18106
18107 int ISD = InstructionOpcodeToISD(Opcode);
18108 assert(ISD && "Invalid opcode");
18109
18110 const X86Subtarget &ST =
18111 TLI->getTargetMachine().getSubtarget<X86Subtarget>();
18112
18113 static const X86CostTblEntry SSE42CostTbl[] = {
18114 { ISD::SETCC, MVT::v2f64, 1 },
18115 { ISD::SETCC, MVT::v4f32, 1 },
18116 { ISD::SETCC, MVT::v2i64, 1 },
18117 { ISD::SETCC, MVT::v4i32, 1 },
18118 { ISD::SETCC, MVT::v8i16, 1 },
18119 { ISD::SETCC, MVT::v16i8, 1 },
18120 };
18121
18122 static const X86CostTblEntry AVX1CostTbl[] = {
18123 { ISD::SETCC, MVT::v4f64, 1 },
18124 { ISD::SETCC, MVT::v8f32, 1 },
18125 // AVX1 does not support 8-wide integer compare.
18126 { ISD::SETCC, MVT::v4i64, 4 },
18127 { ISD::SETCC, MVT::v8i32, 4 },
18128 { ISD::SETCC, MVT::v16i16, 4 },
18129 { ISD::SETCC, MVT::v32i8, 4 },
18130 };
18131
18132 static const X86CostTblEntry AVX2CostTbl[] = {
18133 { ISD::SETCC, MVT::v4i64, 1 },
18134 { ISD::SETCC, MVT::v8i32, 1 },
18135 { ISD::SETCC, MVT::v16i16, 1 },
18136 { ISD::SETCC, MVT::v32i8, 1 },
18137 };
18138
Jakub Staszak270bfbd2012-12-18 22:57:56 +000018139 if (ST.hasAVX2()) {
18140 int Idx = FindInTable(AVX2CostTbl, array_lengthof(AVX2CostTbl), ISD, MTy);
Nadav Rotem7ae3bcc2012-11-05 23:48:20 +000018141 if (Idx != -1)
Jakub Staszak270bfbd2012-12-18 22:57:56 +000018142 return LT.first * AVX2CostTbl[Idx].Cost;
Nadav Rotem7ae3bcc2012-11-05 23:48:20 +000018143 }
18144
18145 if (ST.hasAVX()) {
18146 int Idx = FindInTable(AVX1CostTbl, array_lengthof(AVX1CostTbl), ISD, MTy);
18147 if (Idx != -1)
18148 return LT.first * AVX1CostTbl[Idx].Cost;
18149 }
18150
Jakub Staszak270bfbd2012-12-18 22:57:56 +000018151 if (ST.hasSSE42()) {
18152 int Idx = FindInTable(SSE42CostTbl, array_lengthof(SSE42CostTbl), ISD, MTy);
Nadav Rotem7ae3bcc2012-11-05 23:48:20 +000018153 if (Idx != -1)
Jakub Staszak270bfbd2012-12-18 22:57:56 +000018154 return LT.first * SSE42CostTbl[Idx].Cost;
Nadav Rotem7ae3bcc2012-11-05 23:48:20 +000018155 }
18156
18157 return VectorTargetTransformImpl::getCmpSelInstrCost(Opcode, ValTy, CondTy);
18158}
18159
Nadav Rotemb0428682012-11-06 19:33:53 +000018160unsigned X86VectorTargetTransformInfo::getCastInstrCost(unsigned Opcode,
18161 Type *Dst,
18162 Type *Src) const {
18163 int ISD = InstructionOpcodeToISD(Opcode);
18164 assert(ISD && "Invalid opcode");
Nadav Rotem7ae3bcc2012-11-05 23:48:20 +000018165
Nadav Rotemb0428682012-11-06 19:33:53 +000018166 EVT SrcTy = TLI->getValueType(Src);
18167 EVT DstTy = TLI->getValueType(Dst);
18168
18169 if (!SrcTy.isSimple() || !DstTy.isSimple())
18170 return VectorTargetTransformImpl::getCastInstrCost(Opcode, Dst, Src);
18171
18172 const X86Subtarget &ST = TLI->getTargetMachine().getSubtarget<X86Subtarget>();
18173
18174 static const X86TypeConversionCostTblEntry AVXConversionTbl[] = {
18175 { ISD::SIGN_EXTEND, MVT::v8i32, MVT::v8i16, 1 },
18176 { ISD::ZERO_EXTEND, MVT::v8i32, MVT::v8i16, 1 },
18177 { ISD::SIGN_EXTEND, MVT::v4i64, MVT::v4i32, 1 },
18178 { ISD::ZERO_EXTEND, MVT::v4i64, MVT::v4i32, 1 },
18179 { ISD::TRUNCATE, MVT::v4i32, MVT::v4i64, 1 },
18180 { ISD::TRUNCATE, MVT::v8i16, MVT::v8i32, 1 },
18181 { ISD::SINT_TO_FP, MVT::v8f32, MVT::v8i8, 1 },
18182 { ISD::SINT_TO_FP, MVT::v4f32, MVT::v4i8, 1 },
18183 { ISD::UINT_TO_FP, MVT::v8f32, MVT::v8i8, 1 },
18184 { ISD::UINT_TO_FP, MVT::v4f32, MVT::v4i8, 1 },
Nadav Rotemb14a5f52012-11-09 07:02:24 +000018185 { ISD::FP_TO_SINT, MVT::v8i8, MVT::v8f32, 1 },
18186 { ISD::FP_TO_SINT, MVT::v4i8, MVT::v4f32, 1 },
Nadav Rotemb0428682012-11-06 19:33:53 +000018187 { ISD::ZERO_EXTEND, MVT::v8i32, MVT::v8i1, 6 },
18188 { ISD::SIGN_EXTEND, MVT::v8i32, MVT::v8i1, 9 },
Nadav Rotema6fb97a2012-11-06 21:17:17 +000018189 { ISD::TRUNCATE, MVT::v8i32, MVT::v8i64, 3 },
Nadav Rotemb0428682012-11-06 19:33:53 +000018190 };
18191
18192 if (ST.hasAVX()) {
18193 int Idx = FindInConvertTable(AVXConversionTbl,
18194 array_lengthof(AVXConversionTbl),
18195 ISD, DstTy.getSimpleVT(), SrcTy.getSimpleVT());
18196 if (Idx != -1)
18197 return AVXConversionTbl[Idx].Cost;
18198 }
18199
18200 return VectorTargetTransformImpl::getCastInstrCost(Opcode, Dst, Src);
18201}
Nadav Rotem7ae3bcc2012-11-05 23:48:20 +000018202