blob: ad86c99fd26f45b06edcd49a28e36af37b1397d7 [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);
1091
1092 setOperationAction(ISD::FP_TO_SINT, MVT::v8i16, Custom);
1093
Bruno Cardoso Lopes2e64ae42011-07-28 01:26:39 +00001094 setOperationAction(ISD::FP_TO_SINT, MVT::v8i32, Legal);
1095 setOperationAction(ISD::SINT_TO_FP, MVT::v8i32, Legal);
Bruno Cardoso Lopes55244ce2011-08-01 21:54:09 +00001096 setOperationAction(ISD::FP_ROUND, MVT::v4f32, Legal);
Bruno Cardoso Lopes2e64ae42011-07-28 01:26:39 +00001097
Michael Liaoa7554632012-10-23 17:36:08 +00001098 setOperationAction(ISD::ZERO_EXTEND, MVT::v8i32, Custom);
1099 setOperationAction(ISD::UINT_TO_FP, MVT::v8i8, Custom);
1100 setOperationAction(ISD::UINT_TO_FP, MVT::v8i16, Custom);
1101
Michael Liaob8150d82012-09-10 18:33:51 +00001102 setLoadExtAction(ISD::EXTLOAD, MVT::v4f32, Legal);
1103
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +00001104 setOperationAction(ISD::SRL, MVT::v16i16, Custom);
1105 setOperationAction(ISD::SRL, MVT::v32i8, Custom);
1106
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +00001107 setOperationAction(ISD::SHL, MVT::v16i16, Custom);
1108 setOperationAction(ISD::SHL, MVT::v32i8, Custom);
1109
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +00001110 setOperationAction(ISD::SRA, MVT::v16i16, Custom);
Craig Topper7be5dfd2011-11-12 09:58:49 +00001111 setOperationAction(ISD::SRA, MVT::v32i8, Custom);
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +00001112
Duncan Sands28b77e92011-09-06 19:07:46 +00001113 setOperationAction(ISD::SETCC, MVT::v32i8, Custom);
1114 setOperationAction(ISD::SETCC, MVT::v16i16, Custom);
1115 setOperationAction(ISD::SETCC, MVT::v8i32, Custom);
1116 setOperationAction(ISD::SETCC, MVT::v4i64, Custom);
Bruno Cardoso Lopes0f0e0a02011-08-09 00:46:57 +00001117
Bruno Cardoso Lopesd40aa242011-08-09 23:27:13 +00001118 setOperationAction(ISD::SELECT, MVT::v4f64, Custom);
1119 setOperationAction(ISD::SELECT, MVT::v4i64, Custom);
1120 setOperationAction(ISD::SELECT, MVT::v8f32, Custom);
1121
Craig Topperaaa643c2011-11-09 07:28:55 +00001122 setOperationAction(ISD::VSELECT, MVT::v4f64, Legal);
1123 setOperationAction(ISD::VSELECT, MVT::v4i64, Legal);
1124 setOperationAction(ISD::VSELECT, MVT::v8i32, Legal);
1125 setOperationAction(ISD::VSELECT, MVT::v8f32, Legal);
Nadav Rotem8ffad562011-09-09 20:29:17 +00001126
Craig Topperbf404372012-08-31 15:40:30 +00001127 if (Subtarget->hasFMA() || Subtarget->hasFMA4()) {
Craig Topper3dcefc82012-11-21 05:36:24 +00001128 setOperationAction(ISD::FMA, MVT::v8f32, Legal);
1129 setOperationAction(ISD::FMA, MVT::v4f64, Legal);
1130 setOperationAction(ISD::FMA, MVT::v4f32, Legal);
1131 setOperationAction(ISD::FMA, MVT::v2f64, Legal);
1132 setOperationAction(ISD::FMA, MVT::f32, Legal);
1133 setOperationAction(ISD::FMA, MVT::f64, Legal);
Elena Demikhovsky1503aba2012-08-01 12:06:00 +00001134 }
Craig Topper880ef452012-08-11 22:34:26 +00001135
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00001136 if (Subtarget->hasInt256()) {
Craig Topperaaa643c2011-11-09 07:28:55 +00001137 setOperationAction(ISD::ADD, MVT::v4i64, Legal);
1138 setOperationAction(ISD::ADD, MVT::v8i32, Legal);
1139 setOperationAction(ISD::ADD, MVT::v16i16, Legal);
1140 setOperationAction(ISD::ADD, MVT::v32i8, Legal);
Craig Topper13894fa2011-08-24 06:14:18 +00001141
Craig Topperaaa643c2011-11-09 07:28:55 +00001142 setOperationAction(ISD::SUB, MVT::v4i64, Legal);
1143 setOperationAction(ISD::SUB, MVT::v8i32, Legal);
1144 setOperationAction(ISD::SUB, MVT::v16i16, Legal);
1145 setOperationAction(ISD::SUB, MVT::v32i8, Legal);
Craig Topper13894fa2011-08-24 06:14:18 +00001146
Craig Topperaaa643c2011-11-09 07:28:55 +00001147 setOperationAction(ISD::MUL, MVT::v4i64, Custom);
1148 setOperationAction(ISD::MUL, MVT::v8i32, Legal);
1149 setOperationAction(ISD::MUL, MVT::v16i16, Legal);
Craig Topper46154eb2011-11-11 07:39:23 +00001150 // Don't lower v32i8 because there is no 128-bit byte mul
Nadav Rotembb539bf2011-11-09 13:21:28 +00001151
1152 setOperationAction(ISD::VSELECT, MVT::v32i8, Legal);
Craig Topper7be5dfd2011-11-12 09:58:49 +00001153
1154 setOperationAction(ISD::SRL, MVT::v4i64, Legal);
1155 setOperationAction(ISD::SRL, MVT::v8i32, Legal);
1156
1157 setOperationAction(ISD::SHL, MVT::v4i64, Legal);
1158 setOperationAction(ISD::SHL, MVT::v8i32, Legal);
1159
1160 setOperationAction(ISD::SRA, MVT::v8i32, Legal);
Craig Topperaaa643c2011-11-09 07:28:55 +00001161 } else {
1162 setOperationAction(ISD::ADD, MVT::v4i64, Custom);
1163 setOperationAction(ISD::ADD, MVT::v8i32, Custom);
1164 setOperationAction(ISD::ADD, MVT::v16i16, Custom);
1165 setOperationAction(ISD::ADD, MVT::v32i8, Custom);
1166
1167 setOperationAction(ISD::SUB, MVT::v4i64, Custom);
1168 setOperationAction(ISD::SUB, MVT::v8i32, Custom);
1169 setOperationAction(ISD::SUB, MVT::v16i16, Custom);
1170 setOperationAction(ISD::SUB, MVT::v32i8, Custom);
1171
1172 setOperationAction(ISD::MUL, MVT::v4i64, Custom);
1173 setOperationAction(ISD::MUL, MVT::v8i32, Custom);
1174 setOperationAction(ISD::MUL, MVT::v16i16, Custom);
1175 // Don't lower v32i8 because there is no 128-bit byte mul
Craig Topper7be5dfd2011-11-12 09:58:49 +00001176
1177 setOperationAction(ISD::SRL, MVT::v4i64, Custom);
1178 setOperationAction(ISD::SRL, MVT::v8i32, Custom);
1179
1180 setOperationAction(ISD::SHL, MVT::v4i64, Custom);
1181 setOperationAction(ISD::SHL, MVT::v8i32, Custom);
1182
1183 setOperationAction(ISD::SRA, MVT::v8i32, Custom);
Craig Topperaaa643c2011-11-09 07:28:55 +00001184 }
Craig Topper13894fa2011-08-24 06:14:18 +00001185
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001186 // Custom lower several nodes for 256-bit types.
Jakub Staszak6610b1d2012-04-29 20:52:53 +00001187 for (int i = MVT::FIRST_VECTOR_VALUETYPE;
1188 i <= MVT::LAST_VECTOR_VALUETYPE; ++i) {
Craig Topper0d1f1762012-08-12 00:34:56 +00001189 MVT VT = (MVT::SimpleValueType)i;
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001190
1191 // Extract subvector is special because the value type
1192 // (result) is 128-bit but the source is 256-bit wide.
1193 if (VT.is128BitVector())
Craig Topper0d1f1762012-08-12 00:34:56 +00001194 setOperationAction(ISD::EXTRACT_SUBVECTOR, VT, Custom);
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001195
1196 // Do not attempt to custom lower other non-256-bit vectors
1197 if (!VT.is256BitVector())
David Greene9b9838d2009-06-29 16:47:10 +00001198 continue;
David Greene54d8eba2011-01-27 22:38:56 +00001199
Craig Topper0d1f1762012-08-12 00:34:56 +00001200 setOperationAction(ISD::BUILD_VECTOR, VT, Custom);
1201 setOperationAction(ISD::VECTOR_SHUFFLE, VT, Custom);
1202 setOperationAction(ISD::INSERT_VECTOR_ELT, VT, Custom);
1203 setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT, Custom);
1204 setOperationAction(ISD::SCALAR_TO_VECTOR, VT, Custom);
1205 setOperationAction(ISD::INSERT_SUBVECTOR, VT, Custom);
1206 setOperationAction(ISD::CONCAT_VECTORS, VT, Custom);
David Greene9b9838d2009-06-29 16:47:10 +00001207 }
1208
David Greene54d8eba2011-01-27 22:38:56 +00001209 // Promote v32i8, v16i16, v8i32 select, and, or, xor to v4i64.
Jakub Staszak6610b1d2012-04-29 20:52:53 +00001210 for (int i = MVT::v32i8; i != MVT::v4i64; ++i) {
Craig Topper0d1f1762012-08-12 00:34:56 +00001211 MVT VT = (MVT::SimpleValueType)i;
David Greene54d8eba2011-01-27 22:38:56 +00001212
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001213 // Do not attempt to promote non-256-bit vectors
1214 if (!VT.is256BitVector())
David Greene54d8eba2011-01-27 22:38:56 +00001215 continue;
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001216
Craig Topper0d1f1762012-08-12 00:34:56 +00001217 setOperationAction(ISD::AND, VT, Promote);
1218 AddPromotedToType (ISD::AND, VT, MVT::v4i64);
1219 setOperationAction(ISD::OR, VT, Promote);
1220 AddPromotedToType (ISD::OR, VT, MVT::v4i64);
1221 setOperationAction(ISD::XOR, VT, Promote);
1222 AddPromotedToType (ISD::XOR, VT, MVT::v4i64);
1223 setOperationAction(ISD::LOAD, VT, Promote);
1224 AddPromotedToType (ISD::LOAD, VT, MVT::v4i64);
1225 setOperationAction(ISD::SELECT, VT, Promote);
1226 AddPromotedToType (ISD::SELECT, VT, MVT::v4i64);
David Greene54d8eba2011-01-27 22:38:56 +00001227 }
David Greene9b9838d2009-06-29 16:47:10 +00001228 }
1229
Nadav Rotemd0f3ef82011-07-14 11:11:14 +00001230 // SIGN_EXTEND_INREGs are evaluated by the extend type. Handle the expansion
1231 // of this type with custom code.
Jakub Staszak6610b1d2012-04-29 20:52:53 +00001232 for (int VT = MVT::FIRST_VECTOR_VALUETYPE;
1233 VT != MVT::LAST_VECTOR_VALUETYPE; VT++) {
Chad Rosier30450e82011-12-22 22:35:21 +00001234 setOperationAction(ISD::SIGN_EXTEND_INREG, (MVT::SimpleValueType)VT,
1235 Custom);
Nadav Rotemd0f3ef82011-07-14 11:11:14 +00001236 }
1237
Evan Cheng6be2c582006-04-05 23:38:46 +00001238 // We want to custom lower some of our intrinsics.
Owen Anderson825b72b2009-08-11 20:47:22 +00001239 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
Benjamin Kramerb9bee042012-07-12 09:31:43 +00001240 setOperationAction(ISD::INTRINSIC_W_CHAIN, MVT::Other, Custom);
Evan Cheng6be2c582006-04-05 23:38:46 +00001241
Eli Friedman962f5492010-06-02 19:35:46 +00001242 // Only custom-lower 64-bit SADDO and friends on 64-bit because we don't
1243 // handle type legalization for these operations here.
Dan Gohman71c62a22010-06-02 19:13:40 +00001244 //
Eli Friedman962f5492010-06-02 19:35:46 +00001245 // FIXME: We really should do custom legalization for addition and
1246 // subtraction on x86-32 once PR3203 is fixed. We really can't do much better
1247 // than generic legalization for 64-bit multiplication-with-overflow, though.
Chris Lattnera34b3cf2010-12-19 20:03:11 +00001248 for (unsigned i = 0, e = 3+Subtarget->is64Bit(); i != e; ++i) {
1249 // Add/Sub/Mul with overflow operations are custom lowered.
1250 MVT VT = IntVTs[i];
1251 setOperationAction(ISD::SADDO, VT, Custom);
1252 setOperationAction(ISD::UADDO, VT, Custom);
1253 setOperationAction(ISD::SSUBO, VT, Custom);
1254 setOperationAction(ISD::USUBO, VT, Custom);
1255 setOperationAction(ISD::SMULO, VT, Custom);
1256 setOperationAction(ISD::UMULO, VT, Custom);
Eli Friedmana993f0a2010-06-02 00:27:18 +00001257 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00001258
Chris Lattnera34b3cf2010-12-19 20:03:11 +00001259 // There are no 8-bit 3-address imul/mul instructions
1260 setOperationAction(ISD::SMULO, MVT::i8, Expand);
1261 setOperationAction(ISD::UMULO, MVT::i8, Expand);
Bill Wendling41ea7e72008-11-24 19:21:46 +00001262
Evan Chengd54f2d52009-03-31 19:38:51 +00001263 if (!Subtarget->is64Bit()) {
1264 // These libcalls are not available in 32-bit.
1265 setLibcallName(RTLIB::SHL_I128, 0);
1266 setLibcallName(RTLIB::SRL_I128, 0);
1267 setLibcallName(RTLIB::SRA_I128, 0);
1268 }
1269
Evan Cheng206ee9d2006-07-07 08:33:52 +00001270 // We have target-specific dag combine patterns for the following nodes:
1271 setTargetDAGCombine(ISD::VECTOR_SHUFFLE);
Dan Gohman1bbf72b2010-03-15 23:23:03 +00001272 setTargetDAGCombine(ISD::EXTRACT_VECTOR_ELT);
Duncan Sands6bcd2192011-09-17 16:49:39 +00001273 setTargetDAGCombine(ISD::VSELECT);
Chris Lattner83e6c992006-10-04 06:57:07 +00001274 setTargetDAGCombine(ISD::SELECT);
Nate Begeman740ab032009-01-26 00:52:55 +00001275 setTargetDAGCombine(ISD::SHL);
1276 setTargetDAGCombine(ISD::SRA);
1277 setTargetDAGCombine(ISD::SRL);
Evan Cheng760d1942010-01-04 21:22:48 +00001278 setTargetDAGCombine(ISD::OR);
Nate Begemanb65c1752010-12-17 22:55:37 +00001279 setTargetDAGCombine(ISD::AND);
Benjamin Kramer7d6fe132010-12-21 21:41:44 +00001280 setTargetDAGCombine(ISD::ADD);
Duncan Sands17470be2011-09-22 20:15:48 +00001281 setTargetDAGCombine(ISD::FADD);
1282 setTargetDAGCombine(ISD::FSUB);
Elena Demikhovsky1503aba2012-08-01 12:06:00 +00001283 setTargetDAGCombine(ISD::FMA);
Benjamin Kramer7d6fe132010-12-21 21:41:44 +00001284 setTargetDAGCombine(ISD::SUB);
Nadav Rotem91e43fd2011-09-18 10:39:32 +00001285 setTargetDAGCombine(ISD::LOAD);
Chris Lattner149a4e52008-02-22 02:09:43 +00001286 setTargetDAGCombine(ISD::STORE);
Evan Cheng2e489c42009-12-16 00:53:11 +00001287 setTargetDAGCombine(ISD::ZERO_EXTEND);
Elena Demikhovsky1da58672012-04-22 09:39:03 +00001288 setTargetDAGCombine(ISD::ANY_EXTEND);
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +00001289 setTargetDAGCombine(ISD::SIGN_EXTEND);
Elena Demikhovsky3ae98152012-02-01 07:56:44 +00001290 setTargetDAGCombine(ISD::TRUNCATE);
Stuart Hastingsf99a4b82011-06-06 23:15:58 +00001291 setTargetDAGCombine(ISD::SINT_TO_FP);
Chad Rosiera73b6fc2012-04-27 22:33:25 +00001292 setTargetDAGCombine(ISD::SETCC);
Evan Cheng0b0cd912009-03-28 05:57:29 +00001293 if (Subtarget->is64Bit())
1294 setTargetDAGCombine(ISD::MUL);
Manman Ren92363622012-06-07 22:39:10 +00001295 setTargetDAGCombine(ISD::XOR);
Evan Cheng206ee9d2006-07-07 08:33:52 +00001296
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001297 computeRegisterProperties();
1298
Evan Cheng05219282011-01-06 06:52:41 +00001299 // On Darwin, -Os means optimize for size without hurting performance,
1300 // do not reduce the limit.
Dan Gohman87060f52008-06-30 21:00:56 +00001301 maxStoresPerMemset = 16; // For @llvm.memset -> sequence of stores
Evan Cheng05219282011-01-06 06:52:41 +00001302 maxStoresPerMemsetOptSize = Subtarget->isTargetDarwin() ? 16 : 8;
Evan Cheng255f20f2010-04-01 06:04:33 +00001303 maxStoresPerMemcpy = 8; // For @llvm.memcpy -> sequence of stores
Evan Cheng05219282011-01-06 06:52:41 +00001304 maxStoresPerMemcpyOptSize = Subtarget->isTargetDarwin() ? 8 : 4;
1305 maxStoresPerMemmove = 8; // For @llvm.memmove -> sequence of stores
1306 maxStoresPerMemmoveOptSize = Subtarget->isTargetDarwin() ? 8 : 4;
Jakob Stoklund Olesen8c741b82011-12-06 01:26:19 +00001307 setPrefLoopAlignment(4); // 2^4 bytes.
Evan Cheng6ebf7bc2009-05-13 21:42:09 +00001308 benefitFromCodePlacementOpt = true;
Eli Friedmanfc5d3052011-05-06 20:34:06 +00001309
Benjamin Krameraaf723d2012-05-05 12:49:14 +00001310 // Predictable cmov don't hurt on atom because it's in-order.
1311 predictableSelectIsExpensive = !Subtarget->isAtom();
1312
Jakob Stoklund Olesen8c741b82011-12-06 01:26:19 +00001313 setPrefFunctionAlignment(4); // 2^4 bytes.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001314}
1315
Duncan Sands28b77e92011-09-06 19:07:46 +00001316EVT X86TargetLowering::getSetCCResultType(EVT VT) const {
1317 if (!VT.isVector()) return MVT::i8;
1318 return VT.changeVectorElementTypeToInteger();
Scott Michel5b8f82e2008-03-10 15:42:14 +00001319}
1320
Evan Cheng29286502008-01-23 23:17:41 +00001321/// getMaxByValAlign - Helper for getByValTypeAlignment to determine
1322/// the desired ByVal argument alignment.
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001323static void getMaxByValAlign(Type *Ty, unsigned &MaxAlign) {
Evan Cheng29286502008-01-23 23:17:41 +00001324 if (MaxAlign == 16)
1325 return;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001326 if (VectorType *VTy = dyn_cast<VectorType>(Ty)) {
Evan Cheng29286502008-01-23 23:17:41 +00001327 if (VTy->getBitWidth() == 128)
1328 MaxAlign = 16;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001329 } else if (ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
Evan Cheng29286502008-01-23 23:17:41 +00001330 unsigned EltAlign = 0;
1331 getMaxByValAlign(ATy->getElementType(), EltAlign);
1332 if (EltAlign > MaxAlign)
1333 MaxAlign = EltAlign;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001334 } else if (StructType *STy = dyn_cast<StructType>(Ty)) {
Evan Cheng29286502008-01-23 23:17:41 +00001335 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
1336 unsigned EltAlign = 0;
1337 getMaxByValAlign(STy->getElementType(i), EltAlign);
1338 if (EltAlign > MaxAlign)
1339 MaxAlign = EltAlign;
1340 if (MaxAlign == 16)
1341 break;
1342 }
1343 }
Evan Cheng29286502008-01-23 23:17:41 +00001344}
1345
1346/// getByValTypeAlignment - Return the desired alignment for ByVal aggregate
1347/// function arguments in the caller parameter area. For X86, aggregates
Dale Johannesen0c191872008-02-08 19:48:20 +00001348/// that contain SSE vectors are placed at 16-byte boundaries while the rest
1349/// are at 4-byte boundaries.
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001350unsigned X86TargetLowering::getByValTypeAlignment(Type *Ty) const {
Evan Cheng1887c1c2008-08-21 21:00:15 +00001351 if (Subtarget->is64Bit()) {
1352 // Max of 8 and alignment of type.
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00001353 unsigned TyAlign = TD->getABITypeAlignment(Ty);
Evan Cheng1887c1c2008-08-21 21:00:15 +00001354 if (TyAlign > 8)
1355 return TyAlign;
1356 return 8;
1357 }
1358
Evan Cheng29286502008-01-23 23:17:41 +00001359 unsigned Align = 4;
Craig Topper1accb7e2012-01-10 06:54:16 +00001360 if (Subtarget->hasSSE1())
Dale Johannesen0c191872008-02-08 19:48:20 +00001361 getMaxByValAlign(Ty, Align);
Evan Cheng29286502008-01-23 23:17:41 +00001362 return Align;
1363}
Chris Lattner2b02a442007-02-25 08:29:00 +00001364
Evan Chengf0df0312008-05-15 08:39:06 +00001365/// getOptimalMemOpType - Returns the target specific optimal type for load
Evan Chengc3b0c342010-04-08 07:37:57 +00001366/// and store operations as a result of memset, memcpy, and memmove
1367/// lowering. If DstAlign is zero that means it's safe to destination
1368/// alignment can satisfy any constraint. Similarly if SrcAlign is zero it
1369/// means there isn't a need to check it against alignment requirement,
Evan Cheng946a3a92012-12-12 02:34:41 +00001370/// probably because the source does not need to be loaded. If 'IsMemset' is
1371/// true, that means it's expanding a memset. If 'ZeroMemset' is true, that
1372/// means it's a memset of zero. 'MemcpyStrSrc' indicates whether the memcpy
1373/// source is constant so it does not need to be loaded.
Dan Gohman37f32ee2010-04-16 20:11:05 +00001374/// It returns EVT::Other if the type should be determined using generic
1375/// target-independent logic.
Owen Andersone50ed302009-08-10 22:56:29 +00001376EVT
Evan Cheng255f20f2010-04-01 06:04:33 +00001377X86TargetLowering::getOptimalMemOpType(uint64_t Size,
1378 unsigned DstAlign, unsigned SrcAlign,
Evan Cheng946a3a92012-12-12 02:34:41 +00001379 bool IsMemset, bool ZeroMemset,
Evan Chengc3b0c342010-04-08 07:37:57 +00001380 bool MemcpyStrSrc,
Dan Gohman37f32ee2010-04-16 20:11:05 +00001381 MachineFunction &MF) const {
Dan Gohman37f32ee2010-04-16 20:11:05 +00001382 const Function *F = MF.getFunction();
Evan Cheng946a3a92012-12-12 02:34:41 +00001383 if ((!IsMemset || ZeroMemset) &&
Bill Wendling034b94b2012-12-19 07:18:57 +00001384 !F->getFnAttributes().hasAttribute(Attribute::NoImplicitFloat)) {
Evan Cheng255f20f2010-04-01 06:04:33 +00001385 if (Size >= 16 &&
Evan Chenga5e13622011-01-07 19:35:30 +00001386 (Subtarget->isUnalignedMemAccessFast() ||
1387 ((DstAlign == 0 || DstAlign >= 16) &&
Benjamin Kramer2dbe9292012-11-14 20:08:40 +00001388 (SrcAlign == 0 || SrcAlign >= 16)))) {
1389 if (Size >= 32) {
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00001390 if (Subtarget->hasInt256())
Craig Topper562659f2012-01-13 08:32:21 +00001391 return MVT::v8i32;
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00001392 if (Subtarget->hasFp256())
Craig Topper562659f2012-01-13 08:32:21 +00001393 return MVT::v8f32;
1394 }
Craig Topper1accb7e2012-01-10 06:54:16 +00001395 if (Subtarget->hasSSE2())
Evan Cheng255f20f2010-04-01 06:04:33 +00001396 return MVT::v4i32;
Craig Topper1accb7e2012-01-10 06:54:16 +00001397 if (Subtarget->hasSSE1())
Evan Cheng255f20f2010-04-01 06:04:33 +00001398 return MVT::v4f32;
Evan Chengc3b0c342010-04-08 07:37:57 +00001399 } else if (!MemcpyStrSrc && Size >= 8 &&
Evan Cheng3ea97552010-04-01 20:27:45 +00001400 !Subtarget->is64Bit() &&
Craig Topper1accb7e2012-01-10 06:54:16 +00001401 Subtarget->hasSSE2()) {
Evan Chengc3b0c342010-04-08 07:37:57 +00001402 // Do not use f64 to lower memcpy if source is string constant. It's
1403 // better to use i32 to avoid the loads.
Evan Cheng255f20f2010-04-01 06:04:33 +00001404 return MVT::f64;
Evan Chengc3b0c342010-04-08 07:37:57 +00001405 }
Chris Lattner4002a1b2008-10-28 05:49:35 +00001406 }
Evan Chengf0df0312008-05-15 08:39:06 +00001407 if (Subtarget->is64Bit() && Size >= 8)
Owen Anderson825b72b2009-08-11 20:47:22 +00001408 return MVT::i64;
1409 return MVT::i32;
Evan Chengf0df0312008-05-15 08:39:06 +00001410}
1411
Evan Cheng7d342672012-12-12 01:32:07 +00001412bool X86TargetLowering::isSafeMemOpType(MVT VT) const {
Evan Cheng61f4dfe2012-12-12 00:42:09 +00001413 if (VT == MVT::f32)
1414 return X86ScalarSSEf32;
1415 else if (VT == MVT::f64)
1416 return X86ScalarSSEf64;
Evan Cheng7d342672012-12-12 01:32:07 +00001417 return true;
Evan Cheng61f4dfe2012-12-12 00:42:09 +00001418}
1419
Evan Cheng376642e2012-12-10 23:21:26 +00001420bool
1421X86TargetLowering::allowsUnalignedMemoryAccesses(EVT VT, bool *Fast) const {
1422 if (Fast)
1423 *Fast = Subtarget->isUnalignedMemAccessFast();
1424 return true;
1425}
1426
Chris Lattner5e1df8d2010-01-25 23:38:14 +00001427/// getJumpTableEncoding - Return the entry encoding for a jump table in the
1428/// current function. The returned value is a member of the
1429/// MachineJumpTableInfo::JTEntryKind enum.
1430unsigned X86TargetLowering::getJumpTableEncoding() const {
1431 // In GOT pic mode, each entry in the jump table is emitted as a @GOTOFF
1432 // symbol.
1433 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1434 Subtarget->isPICStyleGOT())
Chris Lattnerc64daab2010-01-26 05:02:42 +00001435 return MachineJumpTableInfo::EK_Custom32;
Michael J. Spencerec38de22010-10-10 22:04:20 +00001436
Chris Lattner5e1df8d2010-01-25 23:38:14 +00001437 // Otherwise, use the normal jump table encoding heuristics.
1438 return TargetLowering::getJumpTableEncoding();
1439}
1440
Chris Lattnerc64daab2010-01-26 05:02:42 +00001441const MCExpr *
1442X86TargetLowering::LowerCustomJumpTableEntry(const MachineJumpTableInfo *MJTI,
1443 const MachineBasicBlock *MBB,
1444 unsigned uid,MCContext &Ctx) const{
1445 assert(getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1446 Subtarget->isPICStyleGOT());
1447 // In 32-bit ELF systems, our jump table entries are formed with @GOTOFF
1448 // entries.
Daniel Dunbar4e815f82010-03-15 23:51:06 +00001449 return MCSymbolRefExpr::Create(MBB->getSymbol(),
1450 MCSymbolRefExpr::VK_GOTOFF, Ctx);
Chris Lattnerc64daab2010-01-26 05:02:42 +00001451}
1452
Evan Chengcc415862007-11-09 01:32:10 +00001453/// getPICJumpTableRelocaBase - Returns relocation base for the given PIC
1454/// jumptable.
Dan Gohman475871a2008-07-27 21:46:04 +00001455SDValue X86TargetLowering::getPICJumpTableRelocBase(SDValue Table,
Chris Lattner589c6f62010-01-26 06:28:43 +00001456 SelectionDAG &DAG) const {
Chris Lattnere4df7562009-07-09 03:15:51 +00001457 if (!Subtarget->is64Bit())
Dale Johannesenb300d2a2009-02-07 00:55:49 +00001458 // This doesn't have DebugLoc associated with it, but is not really the
1459 // same as a Register.
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00001460 return DAG.getNode(X86ISD::GlobalBaseReg, DebugLoc(), getPointerTy());
Evan Chengcc415862007-11-09 01:32:10 +00001461 return Table;
1462}
1463
Chris Lattner589c6f62010-01-26 06:28:43 +00001464/// getPICJumpTableRelocBaseExpr - This returns the relocation base for the
1465/// given PIC jumptable, the same as getPICJumpTableRelocBase, but as an
1466/// MCExpr.
1467const MCExpr *X86TargetLowering::
1468getPICJumpTableRelocBaseExpr(const MachineFunction *MF, unsigned JTI,
1469 MCContext &Ctx) const {
1470 // X86-64 uses RIP relative addressing based on the jump table label.
1471 if (Subtarget->isPICStyleRIPRel())
1472 return TargetLowering::getPICJumpTableRelocBaseExpr(MF, JTI, Ctx);
1473
1474 // Otherwise, the reference is relative to the PIC base.
Chris Lattner142b5312010-11-14 22:48:15 +00001475 return MCSymbolRefExpr::Create(MF->getPICBaseSymbol(), Ctx);
Chris Lattner589c6f62010-01-26 06:28:43 +00001476}
1477
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00001478// FIXME: Why this routine is here? Move to RegInfo!
Evan Chengdee81012010-07-26 21:50:05 +00001479std::pair<const TargetRegisterClass*, uint8_t>
Patrik Hagglund03405572012-12-19 11:30:36 +00001480X86TargetLowering::findRepresentativeClass(MVT VT) const{
Evan Chengdee81012010-07-26 21:50:05 +00001481 const TargetRegisterClass *RRC = 0;
1482 uint8_t Cost = 1;
Patrik Hagglund03405572012-12-19 11:30:36 +00001483 switch (VT.SimpleTy) {
Evan Chengdee81012010-07-26 21:50:05 +00001484 default:
1485 return TargetLowering::findRepresentativeClass(VT);
1486 case MVT::i8: case MVT::i16: case MVT::i32: case MVT::i64:
Craig Topperc9099502012-04-20 06:31:50 +00001487 RRC = Subtarget->is64Bit() ?
1488 (const TargetRegisterClass*)&X86::GR64RegClass :
1489 (const TargetRegisterClass*)&X86::GR32RegClass;
Evan Chengdee81012010-07-26 21:50:05 +00001490 break;
Dale Johannesen0488fb62010-09-30 23:57:10 +00001491 case MVT::x86mmx:
Craig Topperc9099502012-04-20 06:31:50 +00001492 RRC = &X86::VR64RegClass;
Evan Chengdee81012010-07-26 21:50:05 +00001493 break;
1494 case MVT::f32: case MVT::f64:
1495 case MVT::v16i8: case MVT::v8i16: case MVT::v4i32: case MVT::v2i64:
1496 case MVT::v4f32: case MVT::v2f64:
1497 case MVT::v32i8: case MVT::v8i32: case MVT::v4i64: case MVT::v8f32:
1498 case MVT::v4f64:
Craig Topperc9099502012-04-20 06:31:50 +00001499 RRC = &X86::VR128RegClass;
Evan Chengdee81012010-07-26 21:50:05 +00001500 break;
1501 }
1502 return std::make_pair(RRC, Cost);
1503}
1504
Eric Christopherf7a0c7b2010-07-06 05:18:56 +00001505bool X86TargetLowering::getStackCookieLocation(unsigned &AddressSpace,
1506 unsigned &Offset) const {
1507 if (!Subtarget->isTargetLinux())
1508 return false;
1509
1510 if (Subtarget->is64Bit()) {
1511 // %fs:0x28, unless we're using a Kernel code model, in which case it's %gs:
1512 Offset = 0x28;
1513 if (getTargetMachine().getCodeModel() == CodeModel::Kernel)
1514 AddressSpace = 256;
1515 else
1516 AddressSpace = 257;
1517 } else {
1518 // %gs:0x14 on i386
1519 Offset = 0x14;
1520 AddressSpace = 256;
1521 }
1522 return true;
1523}
1524
Chris Lattner2b02a442007-02-25 08:29:00 +00001525//===----------------------------------------------------------------------===//
1526// Return Value Calling Convention Implementation
1527//===----------------------------------------------------------------------===//
1528
Chris Lattner59ed56b2007-02-28 04:55:35 +00001529#include "X86GenCallingConv.inc"
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001530
Michael J. Spencerec38de22010-10-10 22:04:20 +00001531bool
Eric Christopher471e4222011-06-08 23:55:35 +00001532X86TargetLowering::CanLowerReturn(CallingConv::ID CallConv,
Craig Topper0fbf3642012-04-23 03:28:34 +00001533 MachineFunction &MF, bool isVarArg,
Dan Gohman84023e02010-07-10 09:00:22 +00001534 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9af33c2010-07-06 22:19:37 +00001535 LLVMContext &Context) const {
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +00001536 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00001537 CCState CCInfo(CallConv, isVarArg, MF, getTargetMachine(),
Dan Gohmanc9af33c2010-07-06 22:19:37 +00001538 RVLocs, Context);
Dan Gohman84023e02010-07-10 09:00:22 +00001539 return CCInfo.CheckReturn(Outs, RetCC_X86);
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +00001540}
1541
Dan Gohman98ca4f22009-08-05 01:29:28 +00001542SDValue
1543X86TargetLowering::LowerReturn(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001544 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001545 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001546 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohmand858e902010-04-17 15:26:15 +00001547 DebugLoc dl, SelectionDAG &DAG) const {
Dan Gohman1e93df62010-04-17 14:41:14 +00001548 MachineFunction &MF = DAG.getMachineFunction();
1549 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
Scott Michelfdc40a02009-02-17 22:15:04 +00001550
Chris Lattner9774c912007-02-27 05:28:59 +00001551 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00001552 CCState CCInfo(CallConv, isVarArg, MF, getTargetMachine(),
Dan Gohman98ca4f22009-08-05 01:29:28 +00001553 RVLocs, *DAG.getContext());
1554 CCInfo.AnalyzeReturn(Outs, RetCC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001555
Evan Chengdcea1632010-02-04 02:40:39 +00001556 // Add the regs to the liveout set for the function.
1557 MachineRegisterInfo &MRI = DAG.getMachineFunction().getRegInfo();
1558 for (unsigned i = 0; i != RVLocs.size(); ++i)
1559 if (RVLocs[i].isRegLoc() && !MRI.isLiveOut(RVLocs[i].getLocReg()))
1560 MRI.addLiveOut(RVLocs[i].getLocReg());
Scott Michelfdc40a02009-02-17 22:15:04 +00001561
Dan Gohman475871a2008-07-27 21:46:04 +00001562 SDValue Flag;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001563
Dan Gohman475871a2008-07-27 21:46:04 +00001564 SmallVector<SDValue, 6> RetOps;
Chris Lattner447ff682008-03-11 03:23:40 +00001565 RetOps.push_back(Chain); // Operand #0 = Chain (updated below)
1566 // Operand #1 = Bytes To Pop
Dan Gohman1e93df62010-04-17 14:41:14 +00001567 RetOps.push_back(DAG.getTargetConstant(FuncInfo->getBytesToPopOnReturn(),
1568 MVT::i16));
Scott Michelfdc40a02009-02-17 22:15:04 +00001569
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001570 // Copy the result values into the output registers.
Chris Lattner8e6da152008-03-10 21:08:41 +00001571 for (unsigned i = 0; i != RVLocs.size(); ++i) {
1572 CCValAssign &VA = RVLocs[i];
1573 assert(VA.isRegLoc() && "Can only return in registers!");
Dan Gohmanc9403652010-07-07 15:54:55 +00001574 SDValue ValToCopy = OutVals[i];
Dale Johannesenc76d23f2010-07-23 00:30:35 +00001575 EVT ValVT = ValToCopy.getValueType();
1576
Jakob Stoklund Olesenee66b412012-05-31 17:28:20 +00001577 // Promote values to the appropriate types
1578 if (VA.getLocInfo() == CCValAssign::SExt)
1579 ValToCopy = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), ValToCopy);
1580 else if (VA.getLocInfo() == CCValAssign::ZExt)
1581 ValToCopy = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), ValToCopy);
1582 else if (VA.getLocInfo() == CCValAssign::AExt)
1583 ValToCopy = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), ValToCopy);
1584 else if (VA.getLocInfo() == CCValAssign::BCvt)
1585 ValToCopy = DAG.getNode(ISD::BITCAST, dl, VA.getLocVT(), ValToCopy);
1586
Dale Johannesenc4510512010-09-24 19:05:48 +00001587 // If this is x86-64, and we disabled SSE, we can't return FP values,
1588 // or SSE or MMX vectors.
1589 if ((ValVT == MVT::f32 || ValVT == MVT::f64 ||
1590 VA.getLocReg() == X86::XMM0 || VA.getLocReg() == X86::XMM1) &&
Craig Topper1accb7e2012-01-10 06:54:16 +00001591 (Subtarget->is64Bit() && !Subtarget->hasSSE1())) {
Dale Johannesenc76d23f2010-07-23 00:30:35 +00001592 report_fatal_error("SSE register return with SSE disabled");
1593 }
1594 // Likewise we can't return F64 values with SSE1 only. gcc does so, but
1595 // llvm-gcc has never done it right and no one has noticed, so this
1596 // should be OK for now.
1597 if (ValVT == MVT::f64 &&
Craig Topper1accb7e2012-01-10 06:54:16 +00001598 (Subtarget->is64Bit() && !Subtarget->hasSSE2()))
Dale Johannesenc76d23f2010-07-23 00:30:35 +00001599 report_fatal_error("SSE2 register return with SSE2 disabled");
Scott Michelfdc40a02009-02-17 22:15:04 +00001600
Chris Lattner447ff682008-03-11 03:23:40 +00001601 // Returns in ST0/ST1 are handled specially: these are pushed as operands to
1602 // the RET instruction and handled by the FP Stackifier.
Dan Gohman37eed792009-02-04 17:28:58 +00001603 if (VA.getLocReg() == X86::ST0 ||
1604 VA.getLocReg() == X86::ST1) {
Chris Lattner447ff682008-03-11 03:23:40 +00001605 // If this is a copy from an xmm register to ST(0), use an FPExtend to
1606 // change the value to the FP stack register class.
Dan Gohman37eed792009-02-04 17:28:58 +00001607 if (isScalarFPTypeInSSEReg(VA.getValVT()))
Owen Anderson825b72b2009-08-11 20:47:22 +00001608 ValToCopy = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f80, ValToCopy);
Chris Lattner447ff682008-03-11 03:23:40 +00001609 RetOps.push_back(ValToCopy);
1610 // Don't emit a copytoreg.
1611 continue;
1612 }
Dale Johannesena68f9012008-06-24 22:01:44 +00001613
Evan Cheng242b38b2009-02-23 09:03:22 +00001614 // 64-bit vector (MMX) values are returned in XMM0 / XMM1 except for v1i64
1615 // which is returned in RAX / RDX.
Evan Cheng6140a8b2009-02-22 08:05:12 +00001616 if (Subtarget->is64Bit()) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00001617 if (ValVT == MVT::x86mmx) {
Chris Lattner97a2a562010-08-26 05:24:29 +00001618 if (VA.getLocReg() == X86::XMM0 || VA.getLocReg() == X86::XMM1) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001619 ValToCopy = DAG.getNode(ISD::BITCAST, dl, MVT::i64, ValToCopy);
Eric Christopher90eb4022010-07-22 00:26:08 +00001620 ValToCopy = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i64,
1621 ValToCopy);
Chris Lattner97a2a562010-08-26 05:24:29 +00001622 // If we don't have SSE2 available, convert to v4f32 so the generated
1623 // register is legal.
Craig Topper1accb7e2012-01-10 06:54:16 +00001624 if (!Subtarget->hasSSE2())
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001625 ValToCopy = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32,ValToCopy);
Chris Lattner97a2a562010-08-26 05:24:29 +00001626 }
Evan Cheng242b38b2009-02-23 09:03:22 +00001627 }
Evan Cheng6140a8b2009-02-22 08:05:12 +00001628 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00001629
Dale Johannesendd64c412009-02-04 00:33:20 +00001630 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), ValToCopy, Flag);
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001631 Flag = Chain.getValue(1);
1632 }
Dan Gohman61a92132008-04-21 23:59:07 +00001633
1634 // The x86-64 ABI for returning structs by value requires that we copy
1635 // the sret argument into %rax for the return. We saved the argument into
1636 // a virtual register in the entry block, so now we copy the value out
1637 // and into %rax.
1638 if (Subtarget->is64Bit() &&
1639 DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
1640 MachineFunction &MF = DAG.getMachineFunction();
1641 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1642 unsigned Reg = FuncInfo->getSRetReturnReg();
Michael J. Spencerec38de22010-10-10 22:04:20 +00001643 assert(Reg &&
Zhongxing Xuc2798a12010-05-26 08:10:02 +00001644 "SRetReturnReg should have been set in LowerFormalArguments().");
Dale Johannesendd64c412009-02-04 00:33:20 +00001645 SDValue Val = DAG.getCopyFromReg(Chain, dl, Reg, getPointerTy());
Dan Gohman61a92132008-04-21 23:59:07 +00001646
Dale Johannesendd64c412009-02-04 00:33:20 +00001647 Chain = DAG.getCopyToReg(Chain, dl, X86::RAX, Val, Flag);
Dan Gohman61a92132008-04-21 23:59:07 +00001648 Flag = Chain.getValue(1);
Dan Gohman00326812009-10-12 16:36:12 +00001649
1650 // RAX now acts like a return value.
Evan Chengdcea1632010-02-04 02:40:39 +00001651 MRI.addLiveOut(X86::RAX);
Dan Gohman61a92132008-04-21 23:59:07 +00001652 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001653
Chris Lattner447ff682008-03-11 03:23:40 +00001654 RetOps[0] = Chain; // Update chain.
1655
1656 // Add the flag if we have it.
Gabor Greifba36cb52008-08-28 21:40:38 +00001657 if (Flag.getNode())
Chris Lattner447ff682008-03-11 03:23:40 +00001658 RetOps.push_back(Flag);
Scott Michelfdc40a02009-02-17 22:15:04 +00001659
1660 return DAG.getNode(X86ISD::RET_FLAG, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001661 MVT::Other, &RetOps[0], RetOps.size());
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001662}
1663
Evan Chengbf010eb2012-04-10 01:51:00 +00001664bool X86TargetLowering::isUsedByReturnOnly(SDNode *N, SDValue &Chain) const {
Evan Cheng3d2125c2010-11-30 23:55:39 +00001665 if (N->getNumValues() != 1)
1666 return false;
1667 if (!N->hasNUsesOfValue(1, 0))
1668 return false;
1669
Evan Chengbf010eb2012-04-10 01:51:00 +00001670 SDValue TCChain = Chain;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001671 SDNode *Copy = *N->use_begin();
Chad Rosierc8d7eea2012-03-05 19:27:12 +00001672 if (Copy->getOpcode() == ISD::CopyToReg) {
1673 // If the copy has a glue operand, we conservatively assume it isn't safe to
1674 // perform a tail call.
1675 if (Copy->getOperand(Copy->getNumOperands()-1).getValueType() == MVT::Glue)
1676 return false;
Evan Chengbf010eb2012-04-10 01:51:00 +00001677 TCChain = Copy->getOperand(0);
Chad Rosierc8d7eea2012-03-05 19:27:12 +00001678 } else if (Copy->getOpcode() != ISD::FP_EXTEND)
Chad Rosier74bab7f2012-03-02 02:50:46 +00001679 return false;
1680
Evan Cheng1bf891a2010-12-01 22:59:46 +00001681 bool HasRet = false;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001682 for (SDNode::use_iterator UI = Copy->use_begin(), UE = Copy->use_end();
Evan Cheng1bf891a2010-12-01 22:59:46 +00001683 UI != UE; ++UI) {
Evan Cheng3d2125c2010-11-30 23:55:39 +00001684 if (UI->getOpcode() != X86ISD::RET_FLAG)
1685 return false;
Evan Cheng1bf891a2010-12-01 22:59:46 +00001686 HasRet = true;
1687 }
Evan Cheng3d2125c2010-11-30 23:55:39 +00001688
Evan Chengbf010eb2012-04-10 01:51:00 +00001689 if (!HasRet)
1690 return false;
1691
1692 Chain = TCChain;
1693 return true;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001694}
1695
Patrik Hagglunde5c65912012-12-19 12:02:25 +00001696MVT
1697X86TargetLowering::getTypeForExtArgOrReturn(MVT VT,
Cameron Zwarich44579682011-03-17 14:21:56 +00001698 ISD::NodeType ExtendKind) const {
Cameron Zwarich7bbf0ee2011-03-17 14:53:37 +00001699 MVT ReturnMVT;
Cameron Zwarichebe81732011-03-16 22:20:18 +00001700 // TODO: Is this also valid on 32-bit?
1701 if (Subtarget->is64Bit() && VT == MVT::i1 && ExtendKind == ISD::ZERO_EXTEND)
Cameron Zwarich7bbf0ee2011-03-17 14:53:37 +00001702 ReturnMVT = MVT::i8;
1703 else
1704 ReturnMVT = MVT::i32;
1705
Patrik Hagglunde5c65912012-12-19 12:02:25 +00001706 MVT MinVT = getRegisterType(ReturnMVT);
Cameron Zwarich7bbf0ee2011-03-17 14:53:37 +00001707 return VT.bitsLT(MinVT) ? MinVT : VT;
Cameron Zwarichebe81732011-03-16 22:20:18 +00001708}
1709
Dan Gohman98ca4f22009-08-05 01:29:28 +00001710/// LowerCallResult - Lower the result values of a call into the
1711/// appropriate copies out of appropriate physical registers.
1712///
1713SDValue
1714X86TargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001715 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001716 const SmallVectorImpl<ISD::InputArg> &Ins,
1717 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001718 SmallVectorImpl<SDValue> &InVals) const {
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001719
Chris Lattnere32bbf62007-02-28 07:09:55 +00001720 // Assign locations to each value returned by this call.
Chris Lattner9774c912007-02-27 05:28:59 +00001721 SmallVector<CCValAssign, 16> RVLocs;
Torok Edwin3f142c32009-02-01 18:15:56 +00001722 bool Is64Bit = Subtarget->is64Bit();
Eric Christopher471e4222011-06-08 23:55:35 +00001723 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
Craig Topper0fbf3642012-04-23 03:28:34 +00001724 getTargetMachine(), RVLocs, *DAG.getContext());
Dan Gohman98ca4f22009-08-05 01:29:28 +00001725 CCInfo.AnalyzeCallResult(Ins, RetCC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001726
Chris Lattner3085e152007-02-25 08:59:22 +00001727 // Copy all of the result registers out of their specified physreg.
Chris Lattner8e6da152008-03-10 21:08:41 +00001728 for (unsigned i = 0; i != RVLocs.size(); ++i) {
Dan Gohman37eed792009-02-04 17:28:58 +00001729 CCValAssign &VA = RVLocs[i];
Owen Andersone50ed302009-08-10 22:56:29 +00001730 EVT CopyVT = VA.getValVT();
Scott Michelfdc40a02009-02-17 22:15:04 +00001731
Torok Edwin3f142c32009-02-01 18:15:56 +00001732 // If this is x86-64, and we disabled SSE, we can't return FP values
Owen Anderson825b72b2009-08-11 20:47:22 +00001733 if ((CopyVT == MVT::f32 || CopyVT == MVT::f64) &&
Craig Topper1accb7e2012-01-10 06:54:16 +00001734 ((Is64Bit || Ins[i].Flags.isInReg()) && !Subtarget->hasSSE1())) {
Chris Lattner75361b62010-04-07 22:58:41 +00001735 report_fatal_error("SSE register return with SSE disabled");
Torok Edwin3f142c32009-02-01 18:15:56 +00001736 }
1737
Evan Cheng79fb3b42009-02-20 20:43:02 +00001738 SDValue Val;
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001739
1740 // If this is a call to a function that returns an fp value on the floating
Sylvestre Ledruc8e41c52012-07-23 08:51:15 +00001741 // point stack, we must guarantee the value is popped from the stack, so
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001742 // a CopyFromReg is not good enough - the copy instruction may be eliminated
Jakob Stoklund Olesen9bbe4d62011-06-28 18:32:28 +00001743 // if the return value is not used. We use the FpPOP_RETVAL instruction
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001744 // instead.
1745 if (VA.getLocReg() == X86::ST0 || VA.getLocReg() == X86::ST1) {
1746 // If we prefer to use the value in xmm registers, copy it out as f80 and
1747 // use a truncate to move it from fp stack reg to xmm reg.
1748 if (isScalarFPTypeInSSEReg(VA.getValVT())) CopyVT = MVT::f80;
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001749 SDValue Ops[] = { Chain, InFlag };
Jakob Stoklund Olesen9bbe4d62011-06-28 18:32:28 +00001750 Chain = SDValue(DAG.getMachineNode(X86::FpPOP_RETVAL, dl, CopyVT,
1751 MVT::Other, MVT::Glue, Ops, 2), 1);
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001752 Val = Chain.getValue(0);
1753
1754 // Round the f80 to the right size, which also moves it to the appropriate
1755 // xmm register.
1756 if (CopyVT != VA.getValVT())
1757 Val = DAG.getNode(ISD::FP_ROUND, dl, VA.getValVT(), Val,
1758 // This truncation won't change the value.
1759 DAG.getIntPtrConstant(1));
Evan Cheng79fb3b42009-02-20 20:43:02 +00001760 } else {
1761 Chain = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(),
1762 CopyVT, InFlag).getValue(1);
1763 Val = Chain.getValue(0);
1764 }
Chris Lattner8e6da152008-03-10 21:08:41 +00001765 InFlag = Chain.getValue(2);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001766 InVals.push_back(Val);
Chris Lattner3085e152007-02-25 08:59:22 +00001767 }
Duncan Sands4bdcb612008-07-02 17:40:58 +00001768
Dan Gohman98ca4f22009-08-05 01:29:28 +00001769 return Chain;
Chris Lattner2b02a442007-02-25 08:29:00 +00001770}
1771
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001772//===----------------------------------------------------------------------===//
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001773// C & StdCall & Fast Calling Convention implementation
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001774//===----------------------------------------------------------------------===//
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001775// StdCall calling convention seems to be standard for many Windows' API
1776// routines and around. It differs from C calling convention just a little:
1777// callee should clean up the stack, not caller. Symbols should be also
1778// decorated in some fancy way :) It doesn't support any vector arguments.
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001779// For info on fast calling convention see Fast Calling Convention (tail call)
1780// implementation LowerX86_32FastCCCallTo.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001781
Dan Gohman98ca4f22009-08-05 01:29:28 +00001782/// CallIsStructReturn - Determines whether a call uses struct return
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001783/// semantics.
Rafael Espindola1cee7102012-07-25 13:41:10 +00001784enum StructReturnType {
1785 NotStructReturn,
1786 RegStructReturn,
1787 StackStructReturn
1788};
1789static StructReturnType
1790callIsStructReturn(const SmallVectorImpl<ISD::OutputArg> &Outs) {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001791 if (Outs.empty())
Rafael Espindola1cee7102012-07-25 13:41:10 +00001792 return NotStructReturn;
Duncan Sands276dcbd2008-03-21 09:14:45 +00001793
Rafael Espindola1cee7102012-07-25 13:41:10 +00001794 const ISD::ArgFlagsTy &Flags = Outs[0].Flags;
1795 if (!Flags.isSRet())
1796 return NotStructReturn;
1797 if (Flags.isInReg())
1798 return RegStructReturn;
1799 return StackStructReturn;
Gordon Henriksen86737662008-01-05 16:56:59 +00001800}
1801
Dan Gohman7e77b0f2009-08-01 19:14:37 +00001802/// ArgsAreStructReturn - Determines whether a function uses struct
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001803/// return semantics.
Rafael Espindola1cee7102012-07-25 13:41:10 +00001804static StructReturnType
1805argsAreStructReturn(const SmallVectorImpl<ISD::InputArg> &Ins) {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001806 if (Ins.empty())
Rafael Espindola1cee7102012-07-25 13:41:10 +00001807 return NotStructReturn;
Duncan Sands276dcbd2008-03-21 09:14:45 +00001808
Rafael Espindola1cee7102012-07-25 13:41:10 +00001809 const ISD::ArgFlagsTy &Flags = Ins[0].Flags;
1810 if (!Flags.isSRet())
1811 return NotStructReturn;
1812 if (Flags.isInReg())
1813 return RegStructReturn;
1814 return StackStructReturn;
Gordon Henriksen86737662008-01-05 16:56:59 +00001815}
1816
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001817/// CreateCopyOfByValArgument - Make a copy of an aggregate at address specified
1818/// by "Src" to address "Dst" with size and alignment information specified by
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001819/// the specific parameter attribute. The copy will be passed as a byval
1820/// function parameter.
Scott Michelfdc40a02009-02-17 22:15:04 +00001821static SDValue
Dan Gohman475871a2008-07-27 21:46:04 +00001822CreateCopyOfByValArgument(SDValue Src, SDValue Dst, SDValue Chain,
Dale Johannesendd64c412009-02-04 00:33:20 +00001823 ISD::ArgFlagsTy Flags, SelectionDAG &DAG,
1824 DebugLoc dl) {
Chris Lattnere72f2022010-09-21 05:40:29 +00001825 SDValue SizeNode = DAG.getConstant(Flags.getByValSize(), MVT::i32);
Michael J. Spencerec38de22010-10-10 22:04:20 +00001826
Dale Johannesendd64c412009-02-04 00:33:20 +00001827 return DAG.getMemcpy(Chain, dl, Dst, Src, SizeNode, Flags.getByValAlign(),
Stuart Hastings03d58262011-03-10 00:25:53 +00001828 /*isVolatile*/false, /*AlwaysInline=*/true,
Chris Lattnerfc448ff2010-09-21 18:51:21 +00001829 MachinePointerInfo(), MachinePointerInfo());
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001830}
1831
Chris Lattner29689432010-03-11 00:22:57 +00001832/// IsTailCallConvention - Return true if the calling convention is one that
1833/// supports tail call optimization.
1834static bool IsTailCallConvention(CallingConv::ID CC) {
Duncan Sandsdc7f1742012-11-16 12:36:39 +00001835 return (CC == CallingConv::Fast || CC == CallingConv::GHC ||
1836 CC == CallingConv::HiPE);
Chris Lattner29689432010-03-11 00:22:57 +00001837}
1838
Evan Cheng485fafc2011-03-21 01:19:09 +00001839bool X86TargetLowering::mayBeEmittedAsTailCall(CallInst *CI) const {
Nick Lewycky22de16d2012-01-19 00:34:10 +00001840 if (!CI->isTailCall() || getTargetMachine().Options.DisableTailCalls)
Evan Cheng485fafc2011-03-21 01:19:09 +00001841 return false;
1842
1843 CallSite CS(CI);
1844 CallingConv::ID CalleeCC = CS.getCallingConv();
1845 if (!IsTailCallConvention(CalleeCC) && CalleeCC != CallingConv::C)
1846 return false;
1847
1848 return true;
1849}
1850
Evan Cheng0c439eb2010-01-27 00:07:07 +00001851/// FuncIsMadeTailCallSafe - Return true if the function is being made into
1852/// a tailcall target by changing its ABI.
Nick Lewycky8a8d4792011-12-02 22:16:29 +00001853static bool FuncIsMadeTailCallSafe(CallingConv::ID CC,
1854 bool GuaranteedTailCallOpt) {
Chris Lattner29689432010-03-11 00:22:57 +00001855 return GuaranteedTailCallOpt && IsTailCallConvention(CC);
Evan Cheng0c439eb2010-01-27 00:07:07 +00001856}
1857
Dan Gohman98ca4f22009-08-05 01:29:28 +00001858SDValue
1859X86TargetLowering::LowerMemArgument(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001860 CallingConv::ID CallConv,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001861 const SmallVectorImpl<ISD::InputArg> &Ins,
1862 DebugLoc dl, SelectionDAG &DAG,
1863 const CCValAssign &VA,
1864 MachineFrameInfo *MFI,
Dan Gohmand858e902010-04-17 15:26:15 +00001865 unsigned i) const {
Rafael Espindola7effac52007-09-14 15:48:13 +00001866 // Create the nodes corresponding to a load from this parameter slot.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001867 ISD::ArgFlagsTy Flags = Ins[i].Flags;
Nick Lewycky8a8d4792011-12-02 22:16:29 +00001868 bool AlwaysUseMutable = FuncIsMadeTailCallSafe(CallConv,
1869 getTargetMachine().Options.GuaranteedTailCallOpt);
Duncan Sands276dcbd2008-03-21 09:14:45 +00001870 bool isImmutable = !AlwaysUseMutable && !Flags.isByVal();
Anton Korobeynikov22472762009-08-14 18:19:10 +00001871 EVT ValVT;
1872
1873 // If value is passed by pointer we have address passed instead of the value
1874 // itself.
1875 if (VA.getLocInfo() == CCValAssign::Indirect)
1876 ValVT = VA.getLocVT();
1877 else
1878 ValVT = VA.getValVT();
Evan Chenge70bb592008-01-10 02:24:25 +00001879
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001880 // FIXME: For now, all byval parameter objects are marked mutable. This can be
Scott Michelfdc40a02009-02-17 22:15:04 +00001881 // changed with more analysis.
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001882 // In case of tail call optimization mark all arguments mutable. Since they
1883 // could be overwritten by lowering of arguments in case of a tail call.
Evan Cheng90567c32010-02-02 23:58:13 +00001884 if (Flags.isByVal()) {
Evan Chengee2e0e32011-03-30 23:44:13 +00001885 unsigned Bytes = Flags.getByValSize();
1886 if (Bytes == 0) Bytes = 1; // Don't create zero-sized stack objects.
1887 int FI = MFI->CreateFixedObject(Bytes, VA.getLocMemOffset(), isImmutable);
Evan Cheng90567c32010-02-02 23:58:13 +00001888 return DAG.getFrameIndex(FI, getPointerTy());
1889 } else {
1890 int FI = MFI->CreateFixedObject(ValVT.getSizeInBits()/8,
Evan Chenged2ae132010-07-03 00:40:23 +00001891 VA.getLocMemOffset(), isImmutable);
Evan Cheng90567c32010-02-02 23:58:13 +00001892 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
1893 return DAG.getLoad(ValVT, dl, Chain, FIN,
Chris Lattnere8639032010-09-21 06:22:23 +00001894 MachinePointerInfo::getFixedStack(FI),
Pete Cooperd752e0f2011-11-08 18:42:53 +00001895 false, false, false, 0);
Evan Cheng90567c32010-02-02 23:58:13 +00001896 }
Rafael Espindola7effac52007-09-14 15:48:13 +00001897}
1898
Dan Gohman475871a2008-07-27 21:46:04 +00001899SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00001900X86TargetLowering::LowerFormalArguments(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001901 CallingConv::ID CallConv,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001902 bool isVarArg,
1903 const SmallVectorImpl<ISD::InputArg> &Ins,
1904 DebugLoc dl,
1905 SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001906 SmallVectorImpl<SDValue> &InVals)
1907 const {
Evan Cheng1bc78042006-04-26 01:20:17 +00001908 MachineFunction &MF = DAG.getMachineFunction();
Gordon Henriksen86737662008-01-05 16:56:59 +00001909 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
Scott Michelfdc40a02009-02-17 22:15:04 +00001910
Gordon Henriksen86737662008-01-05 16:56:59 +00001911 const Function* Fn = MF.getFunction();
1912 if (Fn->hasExternalLinkage() &&
1913 Subtarget->isTargetCygMing() &&
1914 Fn->getName() == "main")
1915 FuncInfo->setForceFramePointer(true);
1916
Evan Cheng1bc78042006-04-26 01:20:17 +00001917 MachineFrameInfo *MFI = MF.getFrameInfo();
Gordon Henriksen86737662008-01-05 16:56:59 +00001918 bool Is64Bit = Subtarget->is64Bit();
Eli Friedman9a2478a2012-01-20 00:05:46 +00001919 bool IsWindows = Subtarget->isTargetWindows();
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001920 bool IsWin64 = Subtarget->isTargetWin64();
Gordon Henriksenae636f82008-01-03 16:47:34 +00001921
Chris Lattner29689432010-03-11 00:22:57 +00001922 assert(!(isVarArg && IsTailCallConvention(CallConv)) &&
Duncan Sandsdc7f1742012-11-16 12:36:39 +00001923 "Var args not supported with calling convention fastcc, ghc or hipe");
Gordon Henriksenae636f82008-01-03 16:47:34 +00001924
Chris Lattner638402b2007-02-28 07:00:42 +00001925 // Assign locations to all of the incoming arguments.
Chris Lattnerf39f7712007-02-28 05:46:49 +00001926 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00001927 CCState CCInfo(CallConv, isVarArg, MF, getTargetMachine(),
Dan Gohman98ca4f22009-08-05 01:29:28 +00001928 ArgLocs, *DAG.getContext());
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00001929
1930 // Allocate shadow area for Win64
1931 if (IsWin64) {
1932 CCInfo.AllocateStack(32, 8);
1933 }
1934
Duncan Sands45907662010-10-31 13:21:44 +00001935 CCInfo.AnalyzeFormalArguments(Ins, CC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001936
Chris Lattnerf39f7712007-02-28 05:46:49 +00001937 unsigned LastVal = ~0U;
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001938 SDValue ArgValue;
Chris Lattnerf39f7712007-02-28 05:46:49 +00001939 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1940 CCValAssign &VA = ArgLocs[i];
1941 // TODO: If an arg is passed in two places (e.g. reg and stack), skip later
1942 // places.
1943 assert(VA.getValNo() != LastVal &&
1944 "Don't support value assigned to multiple locs yet");
Duncan Sands17001ce2011-10-18 12:44:00 +00001945 (void)LastVal;
Chris Lattnerf39f7712007-02-28 05:46:49 +00001946 LastVal = VA.getValNo();
Scott Michelfdc40a02009-02-17 22:15:04 +00001947
Chris Lattnerf39f7712007-02-28 05:46:49 +00001948 if (VA.isRegLoc()) {
Owen Andersone50ed302009-08-10 22:56:29 +00001949 EVT RegVT = VA.getLocVT();
Craig Topper44d23822012-02-22 05:59:10 +00001950 const TargetRegisterClass *RC;
Owen Anderson825b72b2009-08-11 20:47:22 +00001951 if (RegVT == MVT::i32)
Craig Topperc9099502012-04-20 06:31:50 +00001952 RC = &X86::GR32RegClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001953 else if (Is64Bit && RegVT == MVT::i64)
Craig Topperc9099502012-04-20 06:31:50 +00001954 RC = &X86::GR64RegClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001955 else if (RegVT == MVT::f32)
Craig Topperc9099502012-04-20 06:31:50 +00001956 RC = &X86::FR32RegClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001957 else if (RegVT == MVT::f64)
Craig Topperc9099502012-04-20 06:31:50 +00001958 RC = &X86::FR64RegClass;
Craig Topper7a9a28b2012-08-12 02:23:29 +00001959 else if (RegVT.is256BitVector())
Craig Topperc9099502012-04-20 06:31:50 +00001960 RC = &X86::VR256RegClass;
Craig Topper7a9a28b2012-08-12 02:23:29 +00001961 else if (RegVT.is128BitVector())
Craig Topperc9099502012-04-20 06:31:50 +00001962 RC = &X86::VR128RegClass;
Dale Johannesen0488fb62010-09-30 23:57:10 +00001963 else if (RegVT == MVT::x86mmx)
Craig Topperc9099502012-04-20 06:31:50 +00001964 RC = &X86::VR64RegClass;
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001965 else
Torok Edwinc23197a2009-07-14 16:55:14 +00001966 llvm_unreachable("Unknown argument type!");
Gordon Henriksenae636f82008-01-03 16:47:34 +00001967
Devang Patel68e6bee2011-02-21 23:21:26 +00001968 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001969 ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT);
Scott Michelfdc40a02009-02-17 22:15:04 +00001970
Chris Lattnerf39f7712007-02-28 05:46:49 +00001971 // If this is an 8 or 16-bit value, it is really passed promoted to 32
1972 // bits. Insert an assert[sz]ext to capture this, then truncate to the
1973 // right size.
1974 if (VA.getLocInfo() == CCValAssign::SExt)
Dale Johannesenace16102009-02-03 19:33:06 +00001975 ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue,
Chris Lattnerf39f7712007-02-28 05:46:49 +00001976 DAG.getValueType(VA.getValVT()));
1977 else if (VA.getLocInfo() == CCValAssign::ZExt)
Dale Johannesenace16102009-02-03 19:33:06 +00001978 ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue,
Chris Lattnerf39f7712007-02-28 05:46:49 +00001979 DAG.getValueType(VA.getValVT()));
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001980 else if (VA.getLocInfo() == CCValAssign::BCvt)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001981 ArgValue = DAG.getNode(ISD::BITCAST, dl, VA.getValVT(), ArgValue);
Scott Michelfdc40a02009-02-17 22:15:04 +00001982
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001983 if (VA.isExtInLoc()) {
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001984 // Handle MMX values passed in XMM regs.
1985 if (RegVT.isVector()) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00001986 ArgValue = DAG.getNode(X86ISD::MOVDQ2Q, dl, VA.getValVT(),
1987 ArgValue);
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001988 } else
1989 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
Evan Cheng44c0fd12008-04-25 20:13:28 +00001990 }
Chris Lattnerf39f7712007-02-28 05:46:49 +00001991 } else {
1992 assert(VA.isMemLoc());
Dan Gohman98ca4f22009-08-05 01:29:28 +00001993 ArgValue = LowerMemArgument(Chain, CallConv, Ins, dl, DAG, VA, MFI, i);
Evan Cheng1bc78042006-04-26 01:20:17 +00001994 }
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001995
1996 // If value is passed via pointer - do a load.
1997 if (VA.getLocInfo() == CCValAssign::Indirect)
Chris Lattner51abfe42010-09-21 06:02:19 +00001998 ArgValue = DAG.getLoad(VA.getValVT(), dl, Chain, ArgValue,
Pete Cooperd752e0f2011-11-08 18:42:53 +00001999 MachinePointerInfo(), false, false, false, 0);
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00002000
Dan Gohman98ca4f22009-08-05 01:29:28 +00002001 InVals.push_back(ArgValue);
Evan Cheng1bc78042006-04-26 01:20:17 +00002002 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00002003
Dan Gohman61a92132008-04-21 23:59:07 +00002004 // The x86-64 ABI for returning structs by value requires that we copy
2005 // the sret argument into %rax for the return. Save the argument into
2006 // a virtual register so that we can access it from the return points.
Dan Gohman7e77b0f2009-08-01 19:14:37 +00002007 if (Is64Bit && MF.getFunction()->hasStructRetAttr()) {
Dan Gohman61a92132008-04-21 23:59:07 +00002008 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
2009 unsigned Reg = FuncInfo->getSRetReturnReg();
2010 if (!Reg) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002011 Reg = MF.getRegInfo().createVirtualRegister(getRegClassFor(MVT::i64));
Dan Gohman61a92132008-04-21 23:59:07 +00002012 FuncInfo->setSRetReturnReg(Reg);
2013 }
Dan Gohman98ca4f22009-08-05 01:29:28 +00002014 SDValue Copy = DAG.getCopyToReg(DAG.getEntryNode(), dl, Reg, InVals[0]);
Owen Anderson825b72b2009-08-11 20:47:22 +00002015 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Copy, Chain);
Dan Gohman61a92132008-04-21 23:59:07 +00002016 }
2017
Chris Lattnerf39f7712007-02-28 05:46:49 +00002018 unsigned StackSize = CCInfo.getNextStackOffset();
Evan Cheng0c439eb2010-01-27 00:07:07 +00002019 // Align stack specially for tail calls.
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002020 if (FuncIsMadeTailCallSafe(CallConv,
2021 MF.getTarget().Options.GuaranteedTailCallOpt))
Gordon Henriksenae636f82008-01-03 16:47:34 +00002022 StackSize = GetAlignedArgumentStackSize(StackSize, DAG);
Evan Cheng25caf632006-05-23 21:06:34 +00002023
Evan Cheng1bc78042006-04-26 01:20:17 +00002024 // If the function takes variable number of arguments, make a frame index for
2025 // the start of the first vararg value... for expansion of llvm.va_start.
Gordon Henriksenae636f82008-01-03 16:47:34 +00002026 if (isVarArg) {
NAKAMURA Takumi3ca99432011-03-09 11:33:15 +00002027 if (Is64Bit || (CallConv != CallingConv::X86_FastCall &&
2028 CallConv != CallingConv::X86_ThisCall)) {
Jakob Stoklund Olesenb2eeed72010-07-29 17:42:27 +00002029 FuncInfo->setVarArgsFrameIndex(MFI->CreateFixedObject(1, StackSize,true));
Gordon Henriksen86737662008-01-05 16:56:59 +00002030 }
2031 if (Is64Bit) {
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002032 unsigned TotalNumIntRegs = 0, TotalNumXMMRegs = 0;
2033
2034 // FIXME: We should really autogenerate these arrays
Craig Topperc5eaae42012-03-11 07:57:25 +00002035 static const uint16_t GPR64ArgRegsWin64[] = {
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002036 X86::RCX, X86::RDX, X86::R8, X86::R9
Gordon Henriksen86737662008-01-05 16:56:59 +00002037 };
Craig Topperc5eaae42012-03-11 07:57:25 +00002038 static const uint16_t GPR64ArgRegs64Bit[] = {
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002039 X86::RDI, X86::RSI, X86::RDX, X86::RCX, X86::R8, X86::R9
2040 };
Craig Topperc5eaae42012-03-11 07:57:25 +00002041 static const uint16_t XMMArgRegs64Bit[] = {
Gordon Henriksen86737662008-01-05 16:56:59 +00002042 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
2043 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
2044 };
Craig Topperc5eaae42012-03-11 07:57:25 +00002045 const uint16_t *GPR64ArgRegs;
Anton Korobeynikove7beda12010-10-03 22:52:07 +00002046 unsigned NumXMMRegs = 0;
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002047
2048 if (IsWin64) {
Anton Korobeynikove7beda12010-10-03 22:52:07 +00002049 // The XMM registers which might contain var arg parameters are shadowed
2050 // in their paired GPR. So we only need to save the GPR to their home
2051 // slots.
2052 TotalNumIntRegs = 4;
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002053 GPR64ArgRegs = GPR64ArgRegsWin64;
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002054 } else {
2055 TotalNumIntRegs = 6; TotalNumXMMRegs = 8;
2056 GPR64ArgRegs = GPR64ArgRegs64Bit;
Anton Korobeynikove7beda12010-10-03 22:52:07 +00002057
Chad Rosier30450e82011-12-22 22:35:21 +00002058 NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs64Bit,
2059 TotalNumXMMRegs);
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002060 }
2061 unsigned NumIntRegs = CCInfo.getFirstUnallocated(GPR64ArgRegs,
2062 TotalNumIntRegs);
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002063
Bill Wendling67658342012-10-09 07:45:08 +00002064 bool NoImplicitFloatOps = Fn->getFnAttributes().
Bill Wendling034b94b2012-12-19 07:18:57 +00002065 hasAttribute(Attribute::NoImplicitFloat);
Craig Topper1accb7e2012-01-10 06:54:16 +00002066 assert(!(NumXMMRegs && !Subtarget->hasSSE1()) &&
Torok Edwin3f142c32009-02-01 18:15:56 +00002067 "SSE register cannot be used when SSE is disabled!");
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002068 assert(!(NumXMMRegs && MF.getTarget().Options.UseSoftFloat &&
2069 NoImplicitFloatOps) &&
Evan Chengc7ce29b2009-02-13 22:36:38 +00002070 "SSE register cannot be used when SSE is disabled!");
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002071 if (MF.getTarget().Options.UseSoftFloat || NoImplicitFloatOps ||
Craig Topper1accb7e2012-01-10 06:54:16 +00002072 !Subtarget->hasSSE1())
Torok Edwin3f142c32009-02-01 18:15:56 +00002073 // Kernel mode asks for SSE to be disabled, so don't push them
2074 // on the stack.
2075 TotalNumXMMRegs = 0;
Bill Wendlingf9abd7e2009-03-11 22:30:01 +00002076
Anton Korobeynikove7beda12010-10-03 22:52:07 +00002077 if (IsWin64) {
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00002078 const TargetFrameLowering &TFI = *getTargetMachine().getFrameLowering();
Cameron Esfahaniec37b002010-10-08 19:24:18 +00002079 // Get to the caller-allocated home save location. Add 8 to account
2080 // for the return address.
2081 int HomeOffset = TFI.getOffsetOfLocalArea() + 8;
Anton Korobeynikove7beda12010-10-03 22:52:07 +00002082 FuncInfo->setRegSaveFrameIndex(
Cameron Esfahaniec37b002010-10-08 19:24:18 +00002083 MFI->CreateFixedObject(1, NumIntRegs * 8 + HomeOffset, false));
NAKAMURA Takumi3ca99432011-03-09 11:33:15 +00002084 // Fixup to set vararg frame on shadow area (4 x i64).
2085 if (NumIntRegs < 4)
2086 FuncInfo->setVarArgsFrameIndex(FuncInfo->getRegSaveFrameIndex());
Anton Korobeynikove7beda12010-10-03 22:52:07 +00002087 } else {
2088 // For X86-64, if there are vararg parameters that are passed via
Chad Rosier30450e82011-12-22 22:35:21 +00002089 // registers, then we must store them to their spots on the stack so
2090 // they may be loaded by deferencing the result of va_next.
Anton Korobeynikove7beda12010-10-03 22:52:07 +00002091 FuncInfo->setVarArgsGPOffset(NumIntRegs * 8);
2092 FuncInfo->setVarArgsFPOffset(TotalNumIntRegs * 8 + NumXMMRegs * 16);
2093 FuncInfo->setRegSaveFrameIndex(
2094 MFI->CreateStackObject(TotalNumIntRegs * 8 + TotalNumXMMRegs * 16, 16,
Dan Gohman1e93df62010-04-17 14:41:14 +00002095 false));
Anton Korobeynikove7beda12010-10-03 22:52:07 +00002096 }
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002097
Gordon Henriksen86737662008-01-05 16:56:59 +00002098 // Store the integer parameter registers.
Dan Gohman475871a2008-07-27 21:46:04 +00002099 SmallVector<SDValue, 8> MemOps;
Dan Gohman1e93df62010-04-17 14:41:14 +00002100 SDValue RSFIN = DAG.getFrameIndex(FuncInfo->getRegSaveFrameIndex(),
2101 getPointerTy());
2102 unsigned Offset = FuncInfo->getVarArgsGPOffset();
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002103 for (; NumIntRegs != TotalNumIntRegs; ++NumIntRegs) {
Dan Gohmand6708ea2009-08-15 01:38:56 +00002104 SDValue FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), RSFIN,
2105 DAG.getIntPtrConstant(Offset));
Bob Wilson998e1252009-04-20 18:36:57 +00002106 unsigned VReg = MF.addLiveIn(GPR64ArgRegs[NumIntRegs],
Craig Topperc9099502012-04-20 06:31:50 +00002107 &X86::GR64RegClass);
Owen Anderson825b72b2009-08-11 20:47:22 +00002108 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i64);
Dan Gohman475871a2008-07-27 21:46:04 +00002109 SDValue Store =
Dale Johannesenace16102009-02-03 19:33:06 +00002110 DAG.getStore(Val.getValue(1), dl, Val, FIN,
Chris Lattnere8639032010-09-21 06:22:23 +00002111 MachinePointerInfo::getFixedStack(
2112 FuncInfo->getRegSaveFrameIndex(), Offset),
2113 false, false, 0);
Gordon Henriksen86737662008-01-05 16:56:59 +00002114 MemOps.push_back(Store);
Dan Gohmand6708ea2009-08-15 01:38:56 +00002115 Offset += 8;
Gordon Henriksen86737662008-01-05 16:56:59 +00002116 }
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002117
Dan Gohmanface41a2009-08-16 21:24:25 +00002118 if (TotalNumXMMRegs != 0 && NumXMMRegs != TotalNumXMMRegs) {
2119 // Now store the XMM (fp + vector) parameter registers.
2120 SmallVector<SDValue, 11> SaveXMMOps;
2121 SaveXMMOps.push_back(Chain);
Dan Gohmand6708ea2009-08-15 01:38:56 +00002122
Craig Topperc9099502012-04-20 06:31:50 +00002123 unsigned AL = MF.addLiveIn(X86::AL, &X86::GR8RegClass);
Dan Gohmanface41a2009-08-16 21:24:25 +00002124 SDValue ALVal = DAG.getCopyFromReg(DAG.getEntryNode(), dl, AL, MVT::i8);
2125 SaveXMMOps.push_back(ALVal);
Dan Gohmand6708ea2009-08-15 01:38:56 +00002126
Dan Gohman1e93df62010-04-17 14:41:14 +00002127 SaveXMMOps.push_back(DAG.getIntPtrConstant(
2128 FuncInfo->getRegSaveFrameIndex()));
2129 SaveXMMOps.push_back(DAG.getIntPtrConstant(
2130 FuncInfo->getVarArgsFPOffset()));
Dan Gohmand6708ea2009-08-15 01:38:56 +00002131
Dan Gohmanface41a2009-08-16 21:24:25 +00002132 for (; NumXMMRegs != TotalNumXMMRegs; ++NumXMMRegs) {
Anton Korobeynikove7beda12010-10-03 22:52:07 +00002133 unsigned VReg = MF.addLiveIn(XMMArgRegs64Bit[NumXMMRegs],
Craig Topperc9099502012-04-20 06:31:50 +00002134 &X86::VR128RegClass);
Dan Gohmanface41a2009-08-16 21:24:25 +00002135 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::v4f32);
2136 SaveXMMOps.push_back(Val);
2137 }
2138 MemOps.push_back(DAG.getNode(X86ISD::VASTART_SAVE_XMM_REGS, dl,
2139 MVT::Other,
2140 &SaveXMMOps[0], SaveXMMOps.size()));
Gordon Henriksen86737662008-01-05 16:56:59 +00002141 }
Dan Gohmanface41a2009-08-16 21:24:25 +00002142
2143 if (!MemOps.empty())
2144 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
2145 &MemOps[0], MemOps.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00002146 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00002147 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002148
Gordon Henriksen86737662008-01-05 16:56:59 +00002149 // Some CCs need callee pop.
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002150 if (X86::isCalleePop(CallConv, Is64Bit, isVarArg,
2151 MF.getTarget().Options.GuaranteedTailCallOpt)) {
Dan Gohman1e93df62010-04-17 14:41:14 +00002152 FuncInfo->setBytesToPopOnReturn(StackSize); // Callee pops everything.
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00002153 } else {
Dan Gohman1e93df62010-04-17 14:41:14 +00002154 FuncInfo->setBytesToPopOnReturn(0); // Callee pops nothing.
Chris Lattnerf39f7712007-02-28 05:46:49 +00002155 // If this is an sret function, the return should pop the hidden pointer.
Eli Friedman9a2478a2012-01-20 00:05:46 +00002156 if (!Is64Bit && !IsTailCallConvention(CallConv) && !IsWindows &&
Rafael Espindola1cee7102012-07-25 13:41:10 +00002157 argsAreStructReturn(Ins) == StackStructReturn)
Dan Gohman1e93df62010-04-17 14:41:14 +00002158 FuncInfo->setBytesToPopOnReturn(4);
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00002159 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00002160
Gordon Henriksen86737662008-01-05 16:56:59 +00002161 if (!Is64Bit) {
Dan Gohman1e93df62010-04-17 14:41:14 +00002162 // RegSaveFrameIndex is X86-64 only.
2163 FuncInfo->setRegSaveFrameIndex(0xAAAAAAA);
Anton Korobeynikovded05e32010-05-16 09:08:45 +00002164 if (CallConv == CallingConv::X86_FastCall ||
2165 CallConv == CallingConv::X86_ThisCall)
Dan Gohman1e93df62010-04-17 14:41:14 +00002166 // fastcc functions can't have varargs.
2167 FuncInfo->setVarArgsFrameIndex(0xAAAAAAA);
Gordon Henriksen86737662008-01-05 16:56:59 +00002168 }
Evan Cheng25caf632006-05-23 21:06:34 +00002169
Rafael Espindola76927d752011-08-30 19:39:58 +00002170 FuncInfo->setArgumentStackSize(StackSize);
2171
Dan Gohman98ca4f22009-08-05 01:29:28 +00002172 return Chain;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002173}
2174
Dan Gohman475871a2008-07-27 21:46:04 +00002175SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00002176X86TargetLowering::LowerMemOpCallTo(SDValue Chain,
2177 SDValue StackPtr, SDValue Arg,
2178 DebugLoc dl, SelectionDAG &DAG,
Evan Chengdffbd832008-01-10 00:09:10 +00002179 const CCValAssign &VA,
Dan Gohmand858e902010-04-17 15:26:15 +00002180 ISD::ArgFlagsTy Flags) const {
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00002181 unsigned LocMemOffset = VA.getLocMemOffset();
Dan Gohman475871a2008-07-27 21:46:04 +00002182 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset);
Dale Johannesenace16102009-02-03 19:33:06 +00002183 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, PtrOff);
Chris Lattnerfc448ff2010-09-21 18:51:21 +00002184 if (Flags.isByVal())
Dale Johannesendd64c412009-02-04 00:33:20 +00002185 return CreateCopyOfByValArgument(Arg, PtrOff, Chain, Flags, DAG, dl);
Chris Lattnerfc448ff2010-09-21 18:51:21 +00002186
2187 return DAG.getStore(Chain, dl, Arg, PtrOff,
2188 MachinePointerInfo::getStack(LocMemOffset),
David Greene67c9d422010-02-15 16:53:33 +00002189 false, false, 0);
Evan Chengdffbd832008-01-10 00:09:10 +00002190}
2191
Bill Wendling64e87322009-01-16 19:25:27 +00002192/// EmitTailCallLoadRetAddr - Emit a load of return address if tail call
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002193/// optimization is performed and it is required.
Scott Michelfdc40a02009-02-17 22:15:04 +00002194SDValue
2195X86TargetLowering::EmitTailCallLoadRetAddr(SelectionDAG &DAG,
Evan Chengddc419c2010-01-26 19:04:47 +00002196 SDValue &OutRetAddr, SDValue Chain,
2197 bool IsTailCall, bool Is64Bit,
Dan Gohmand858e902010-04-17 15:26:15 +00002198 int FPDiff, DebugLoc dl) const {
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002199 // Adjust the Return address stack slot.
Owen Andersone50ed302009-08-10 22:56:29 +00002200 EVT VT = getPointerTy();
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002201 OutRetAddr = getReturnAddressFrameIndex(DAG);
Bill Wendling64e87322009-01-16 19:25:27 +00002202
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002203 // Load the "old" Return address.
Chris Lattner51abfe42010-09-21 06:02:19 +00002204 OutRetAddr = DAG.getLoad(VT, dl, Chain, OutRetAddr, MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002205 false, false, false, 0);
Gabor Greifba36cb52008-08-28 21:40:38 +00002206 return SDValue(OutRetAddr.getNode(), 1);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002207}
2208
Chris Lattner7a2bdde2011-04-15 05:18:47 +00002209/// EmitTailCallStoreRetAddr - Emit a store of the return address if tail call
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002210/// optimization is performed and it is required (FPDiff!=0).
Scott Michelfdc40a02009-02-17 22:15:04 +00002211static SDValue
2212EmitTailCallStoreRetAddr(SelectionDAG & DAG, MachineFunction &MF,
Michael Liaoaa3c2c02012-10-25 06:29:14 +00002213 SDValue Chain, SDValue RetAddrFrIdx, EVT PtrVT,
2214 unsigned SlotSize, int FPDiff, DebugLoc dl) {
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002215 // Store the return address to the appropriate stack slot.
2216 if (!FPDiff) return Chain;
2217 // Calculate the new stack slot for the return address.
Scott Michelfdc40a02009-02-17 22:15:04 +00002218 int NewReturnAddrFI =
Evan Chenged2ae132010-07-03 00:40:23 +00002219 MF.getFrameInfo()->CreateFixedObject(SlotSize, FPDiff-SlotSize, false);
Michael Liaoaa3c2c02012-10-25 06:29:14 +00002220 SDValue NewRetAddrFrIdx = DAG.getFrameIndex(NewReturnAddrFI, PtrVT);
Scott Michelfdc40a02009-02-17 22:15:04 +00002221 Chain = DAG.getStore(Chain, dl, RetAddrFrIdx, NewRetAddrFrIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00002222 MachinePointerInfo::getFixedStack(NewReturnAddrFI),
David Greene67c9d422010-02-15 16:53:33 +00002223 false, false, 0);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002224 return Chain;
2225}
2226
Dan Gohman98ca4f22009-08-05 01:29:28 +00002227SDValue
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00002228X86TargetLowering::LowerCall(TargetLowering::CallLoweringInfo &CLI,
Dan Gohmand858e902010-04-17 15:26:15 +00002229 SmallVectorImpl<SDValue> &InVals) const {
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00002230 SelectionDAG &DAG = CLI.DAG;
2231 DebugLoc &dl = CLI.DL;
2232 SmallVector<ISD::OutputArg, 32> &Outs = CLI.Outs;
2233 SmallVector<SDValue, 32> &OutVals = CLI.OutVals;
2234 SmallVector<ISD::InputArg, 32> &Ins = CLI.Ins;
2235 SDValue Chain = CLI.Chain;
2236 SDValue Callee = CLI.Callee;
2237 CallingConv::ID CallConv = CLI.CallConv;
2238 bool &isTailCall = CLI.IsTailCall;
2239 bool isVarArg = CLI.IsVarArg;
2240
Dan Gohman98ca4f22009-08-05 01:29:28 +00002241 MachineFunction &MF = DAG.getMachineFunction();
2242 bool Is64Bit = Subtarget->is64Bit();
NAKAMURA Takumifb840c92011-02-05 15:11:13 +00002243 bool IsWin64 = Subtarget->isTargetWin64();
Eli Friedman9a2478a2012-01-20 00:05:46 +00002244 bool IsWindows = Subtarget->isTargetWindows();
Rafael Espindola1cee7102012-07-25 13:41:10 +00002245 StructReturnType SR = callIsStructReturn(Outs);
Evan Cheng5f941932010-02-05 02:21:12 +00002246 bool IsSibcall = false;
Dan Gohman98ca4f22009-08-05 01:29:28 +00002247
Nick Lewycky22de16d2012-01-19 00:34:10 +00002248 if (MF.getTarget().Options.DisableTailCalls)
2249 isTailCall = false;
2250
Evan Cheng5f941932010-02-05 02:21:12 +00002251 if (isTailCall) {
Evan Cheng0c439eb2010-01-27 00:07:07 +00002252 // Check if it's really possible to do a tail call.
Evan Chenga375d472010-03-15 18:54:48 +00002253 isTailCall = IsEligibleForTailCallOptimization(Callee, CallConv,
Rafael Espindola1cee7102012-07-25 13:41:10 +00002254 isVarArg, SR != NotStructReturn,
Evan Chengb1cacc72012-09-25 05:32:34 +00002255 MF.getFunction()->hasStructRetAttr(), CLI.RetTy,
Rafael Espindola1cee7102012-07-25 13:41:10 +00002256 Outs, OutVals, Ins, DAG);
Evan Chengf22f9b32010-02-06 03:28:46 +00002257
2258 // Sibcalls are automatically detected tailcalls which do not require
2259 // ABI changes.
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002260 if (!MF.getTarget().Options.GuaranteedTailCallOpt && isTailCall)
Evan Cheng5f941932010-02-05 02:21:12 +00002261 IsSibcall = true;
Evan Chengf22f9b32010-02-06 03:28:46 +00002262
2263 if (isTailCall)
2264 ++NumTailCalls;
Evan Cheng5f941932010-02-05 02:21:12 +00002265 }
Evan Cheng0c439eb2010-01-27 00:07:07 +00002266
Chris Lattner29689432010-03-11 00:22:57 +00002267 assert(!(isVarArg && IsTailCallConvention(CallConv)) &&
Duncan Sandsdc7f1742012-11-16 12:36:39 +00002268 "Var args not supported with calling convention fastcc, ghc or hipe");
Gordon Henriksenae636f82008-01-03 16:47:34 +00002269
Chris Lattner638402b2007-02-28 07:00:42 +00002270 // Analyze operands of the call, assigning locations to each operand.
Chris Lattner423c5f42007-02-28 05:31:48 +00002271 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002272 CCState CCInfo(CallConv, isVarArg, MF, getTargetMachine(),
Dan Gohman98ca4f22009-08-05 01:29:28 +00002273 ArgLocs, *DAG.getContext());
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00002274
2275 // Allocate shadow area for Win64
2276 if (IsWin64) {
2277 CCInfo.AllocateStack(32, 8);
2278 }
2279
Duncan Sands45907662010-10-31 13:21:44 +00002280 CCInfo.AnalyzeCallOperands(Outs, CC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00002281
Chris Lattner423c5f42007-02-28 05:31:48 +00002282 // Get a count of how many bytes are to be pushed on the stack.
2283 unsigned NumBytes = CCInfo.getNextStackOffset();
Evan Chengf22f9b32010-02-06 03:28:46 +00002284 if (IsSibcall)
Evan Chengb2c92902010-02-02 02:22:50 +00002285 // This is a sibcall. The memory operands are available in caller's
2286 // own caller's stack.
2287 NumBytes = 0;
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002288 else if (getTargetMachine().Options.GuaranteedTailCallOpt &&
2289 IsTailCallConvention(CallConv))
Evan Chengf22f9b32010-02-06 03:28:46 +00002290 NumBytes = GetAlignedArgumentStackSize(NumBytes, DAG);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002291
Gordon Henriksen86737662008-01-05 16:56:59 +00002292 int FPDiff = 0;
Evan Chengf22f9b32010-02-06 03:28:46 +00002293 if (isTailCall && !IsSibcall) {
Gordon Henriksen86737662008-01-05 16:56:59 +00002294 // Lower arguments at fp - stackoffset + fpdiff.
Jakub Staszak96df4372012-10-29 22:02:26 +00002295 X86MachineFunctionInfo *X86Info = MF.getInfo<X86MachineFunctionInfo>();
2296 unsigned NumBytesCallerPushed = X86Info->getBytesToPopOnReturn();
2297
Gordon Henriksen86737662008-01-05 16:56:59 +00002298 FPDiff = NumBytesCallerPushed - NumBytes;
2299
2300 // Set the delta of movement of the returnaddr stackslot.
2301 // But only set if delta is greater than previous delta.
Jakub Staszak96df4372012-10-29 22:02:26 +00002302 if (FPDiff < X86Info->getTCReturnAddrDelta())
2303 X86Info->setTCReturnAddrDelta(FPDiff);
Gordon Henriksen86737662008-01-05 16:56:59 +00002304 }
2305
Evan Chengf22f9b32010-02-06 03:28:46 +00002306 if (!IsSibcall)
2307 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002308
Dan Gohman475871a2008-07-27 21:46:04 +00002309 SDValue RetAddrFrIdx;
Chris Lattner7a2bdde2011-04-15 05:18:47 +00002310 // Load return address for tail calls.
Evan Chengf22f9b32010-02-06 03:28:46 +00002311 if (isTailCall && FPDiff)
2312 Chain = EmitTailCallLoadRetAddr(DAG, RetAddrFrIdx, Chain, isTailCall,
2313 Is64Bit, FPDiff, dl);
Gordon Henriksen86737662008-01-05 16:56:59 +00002314
Dan Gohman475871a2008-07-27 21:46:04 +00002315 SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass;
2316 SmallVector<SDValue, 8> MemOpChains;
2317 SDValue StackPtr;
Chris Lattner423c5f42007-02-28 05:31:48 +00002318
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002319 // Walk the register/memloc assignments, inserting copies/loads. In the case
2320 // of tail call optimization arguments are handle later.
Chris Lattner423c5f42007-02-28 05:31:48 +00002321 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2322 CCValAssign &VA = ArgLocs[i];
Owen Andersone50ed302009-08-10 22:56:29 +00002323 EVT RegVT = VA.getLocVT();
Dan Gohmanc9403652010-07-07 15:54:55 +00002324 SDValue Arg = OutVals[i];
Dan Gohman98ca4f22009-08-05 01:29:28 +00002325 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Dan Gohman095cc292008-09-13 01:54:27 +00002326 bool isByVal = Flags.isByVal();
Scott Michelfdc40a02009-02-17 22:15:04 +00002327
Chris Lattner423c5f42007-02-28 05:31:48 +00002328 // Promote the value if needed.
2329 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002330 default: llvm_unreachable("Unknown loc info!");
Chris Lattner423c5f42007-02-28 05:31:48 +00002331 case CCValAssign::Full: break;
2332 case CCValAssign::SExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002333 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00002334 break;
2335 case CCValAssign::ZExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002336 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00002337 break;
2338 case CCValAssign::AExt:
Craig Topper7a9a28b2012-08-12 02:23:29 +00002339 if (RegVT.is128BitVector()) {
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002340 // Special case: passing MMX values in XMM registers.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002341 Arg = DAG.getNode(ISD::BITCAST, dl, MVT::i64, Arg);
Owen Anderson825b72b2009-08-11 20:47:22 +00002342 Arg = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i64, Arg);
2343 Arg = getMOVL(DAG, dl, MVT::v2i64, DAG.getUNDEF(MVT::v2i64), Arg);
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002344 } else
2345 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, RegVT, Arg);
2346 break;
2347 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002348 Arg = DAG.getNode(ISD::BITCAST, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00002349 break;
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00002350 case CCValAssign::Indirect: {
2351 // Store the argument.
2352 SDValue SpillSlot = DAG.CreateStackTemporary(VA.getValVT());
Evan Chengff89dcb2009-10-18 18:16:27 +00002353 int FI = cast<FrameIndexSDNode>(SpillSlot)->getIndex();
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00002354 Chain = DAG.getStore(Chain, dl, Arg, SpillSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00002355 MachinePointerInfo::getFixedStack(FI),
David Greene67c9d422010-02-15 16:53:33 +00002356 false, false, 0);
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00002357 Arg = SpillSlot;
2358 break;
2359 }
Evan Cheng6b5783d2006-05-25 18:56:34 +00002360 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002361
Chris Lattner423c5f42007-02-28 05:31:48 +00002362 if (VA.isRegLoc()) {
Stuart Hastings2aa0f232011-05-26 04:09:49 +00002363 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
2364 if (isVarArg && IsWin64) {
2365 // Win64 ABI requires argument XMM reg to be copied to the corresponding
2366 // shadow reg if callee is a varargs function.
2367 unsigned ShadowReg = 0;
2368 switch (VA.getLocReg()) {
2369 case X86::XMM0: ShadowReg = X86::RCX; break;
2370 case X86::XMM1: ShadowReg = X86::RDX; break;
2371 case X86::XMM2: ShadowReg = X86::R8; break;
2372 case X86::XMM3: ShadowReg = X86::R9; break;
Anton Korobeynikovc52bedb2010-08-27 14:43:06 +00002373 }
Stuart Hastings2aa0f232011-05-26 04:09:49 +00002374 if (ShadowReg)
2375 RegsToPass.push_back(std::make_pair(ShadowReg, Arg));
Anton Korobeynikovc52bedb2010-08-27 14:43:06 +00002376 }
Evan Chengf22f9b32010-02-06 03:28:46 +00002377 } else if (!IsSibcall && (!isTailCall || isByVal)) {
Evan Cheng5f941932010-02-05 02:21:12 +00002378 assert(VA.isMemLoc());
2379 if (StackPtr.getNode() == 0)
Michael Liaoc5c970e2012-10-31 04:14:09 +00002380 StackPtr = DAG.getCopyFromReg(Chain, dl, RegInfo->getStackRegister(),
2381 getPointerTy());
Evan Cheng5f941932010-02-05 02:21:12 +00002382 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Arg,
2383 dl, DAG, VA, Flags));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002384 }
Stuart Hastings2aa0f232011-05-26 04:09:49 +00002385 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002386
Evan Cheng32fe1032006-05-25 00:59:30 +00002387 if (!MemOpChains.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00002388 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002389 &MemOpChains[0], MemOpChains.size());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002390
Chris Lattner88e1fd52009-07-09 04:24:46 +00002391 if (Subtarget->isPICStyleGOT()) {
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002392 // ELF / PIC requires GOT in the EBX register before function calls via PLT
2393 // GOT pointer.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002394 if (!isTailCall) {
Jakob Stoklund Olesenb8720782012-07-04 19:28:31 +00002395 RegsToPass.push_back(std::make_pair(unsigned(X86::EBX),
2396 DAG.getNode(X86ISD::GlobalBaseReg, DebugLoc(), getPointerTy())));
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002397 } else {
2398 // If we are tail calling and generating PIC/GOT style code load the
2399 // address of the callee into ECX. The value in ecx is used as target of
2400 // the tail jump. This is done to circumvent the ebx/callee-saved problem
2401 // for tail calls on PIC/GOT architectures. Normally we would just put the
2402 // address of GOT into ebx and then call target@PLT. But for tail calls
2403 // ebx would be restored (since ebx is callee saved) before jumping to the
2404 // target@PLT.
2405
2406 // Note: The actual moving to ECX is done further down.
2407 GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee);
2408 if (G && !G->getGlobal()->hasHiddenVisibility() &&
2409 !G->getGlobal()->hasProtectedVisibility())
2410 Callee = LowerGlobalAddress(Callee, DAG);
2411 else if (isa<ExternalSymbolSDNode>(Callee))
Chris Lattner15a380a2009-07-09 04:39:06 +00002412 Callee = LowerExternalSymbol(Callee, DAG);
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002413 }
Anton Korobeynikov7f705592007-01-12 19:20:47 +00002414 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00002415
NAKAMURA Takumifb840c92011-02-05 15:11:13 +00002416 if (Is64Bit && isVarArg && !IsWin64) {
Gordon Henriksen86737662008-01-05 16:56:59 +00002417 // From AMD64 ABI document:
2418 // For calls that may call functions that use varargs or stdargs
2419 // (prototype-less calls or calls to functions containing ellipsis (...) in
2420 // the declaration) %al is used as hidden argument to specify the number
2421 // of SSE registers used. The contents of %al do not need to match exactly
2422 // the number of registers, but must be an ubound on the number of SSE
2423 // registers used and is in the range 0 - 8 inclusive.
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002424
Gordon Henriksen86737662008-01-05 16:56:59 +00002425 // Count the number of XMM registers allocated.
Craig Topperc5eaae42012-03-11 07:57:25 +00002426 static const uint16_t XMMArgRegs[] = {
Gordon Henriksen86737662008-01-05 16:56:59 +00002427 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
2428 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
2429 };
2430 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs, 8);
Craig Topper1accb7e2012-01-10 06:54:16 +00002431 assert((Subtarget->hasSSE1() || !NumXMMRegs)
Torok Edwin3f142c32009-02-01 18:15:56 +00002432 && "SSE registers cannot be used when SSE is disabled");
Scott Michelfdc40a02009-02-17 22:15:04 +00002433
Jakob Stoklund Olesenb8720782012-07-04 19:28:31 +00002434 RegsToPass.push_back(std::make_pair(unsigned(X86::AL),
2435 DAG.getConstant(NumXMMRegs, MVT::i8)));
Gordon Henriksen86737662008-01-05 16:56:59 +00002436 }
2437
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00002438 // For tail calls lower the arguments to the 'real' stack slot.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002439 if (isTailCall) {
2440 // Force all the incoming stack arguments to be loaded from the stack
2441 // before any new outgoing arguments are stored to the stack, because the
2442 // outgoing stack slots may alias the incoming argument stack slots, and
2443 // the alias isn't otherwise explicit. This is slightly more conservative
2444 // than necessary, because it means that each store effectively depends
2445 // on every argument instead of just those arguments it would clobber.
2446 SDValue ArgChain = DAG.getStackArgumentTokenFactor(Chain);
2447
Dan Gohman475871a2008-07-27 21:46:04 +00002448 SmallVector<SDValue, 8> MemOpChains2;
2449 SDValue FIN;
Gordon Henriksen86737662008-01-05 16:56:59 +00002450 int FI = 0;
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002451 if (getTargetMachine().Options.GuaranteedTailCallOpt) {
Evan Chengb2c92902010-02-02 02:22:50 +00002452 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2453 CCValAssign &VA = ArgLocs[i];
2454 if (VA.isRegLoc())
2455 continue;
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00002456 assert(VA.isMemLoc());
Dan Gohmanc9403652010-07-07 15:54:55 +00002457 SDValue Arg = OutVals[i];
Dan Gohman98ca4f22009-08-05 01:29:28 +00002458 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Gordon Henriksen86737662008-01-05 16:56:59 +00002459 // Create frame index.
2460 int32_t Offset = VA.getLocMemOffset()+FPDiff;
Duncan Sands83ec4b62008-06-06 12:08:01 +00002461 uint32_t OpSize = (VA.getLocVT().getSizeInBits()+7)/8;
Evan Chenged2ae132010-07-03 00:40:23 +00002462 FI = MF.getFrameInfo()->CreateFixedObject(OpSize, Offset, true);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002463 FIN = DAG.getFrameIndex(FI, getPointerTy());
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00002464
Duncan Sands276dcbd2008-03-21 09:14:45 +00002465 if (Flags.isByVal()) {
Evan Cheng8e5712b2008-01-12 01:08:07 +00002466 // Copy relative to framepointer.
Dan Gohman475871a2008-07-27 21:46:04 +00002467 SDValue Source = DAG.getIntPtrConstant(VA.getLocMemOffset());
Gabor Greifba36cb52008-08-28 21:40:38 +00002468 if (StackPtr.getNode() == 0)
Michael Liaoc5c970e2012-10-31 04:14:09 +00002469 StackPtr = DAG.getCopyFromReg(Chain, dl,
2470 RegInfo->getStackRegister(),
Dale Johannesendd64c412009-02-04 00:33:20 +00002471 getPointerTy());
Dale Johannesenace16102009-02-03 19:33:06 +00002472 Source = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, Source);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002473
Dan Gohman98ca4f22009-08-05 01:29:28 +00002474 MemOpChains2.push_back(CreateCopyOfByValArgument(Source, FIN,
2475 ArgChain,
Dale Johannesendd64c412009-02-04 00:33:20 +00002476 Flags, DAG, dl));
Gordon Henriksen86737662008-01-05 16:56:59 +00002477 } else {
Evan Cheng8e5712b2008-01-12 01:08:07 +00002478 // Store relative to framepointer.
Dan Gohman69de1932008-02-06 22:27:42 +00002479 MemOpChains2.push_back(
Dan Gohman98ca4f22009-08-05 01:29:28 +00002480 DAG.getStore(ArgChain, dl, Arg, FIN,
Chris Lattnere8639032010-09-21 06:22:23 +00002481 MachinePointerInfo::getFixedStack(FI),
David Greene67c9d422010-02-15 16:53:33 +00002482 false, false, 0));
Scott Michelfdc40a02009-02-17 22:15:04 +00002483 }
Gordon Henriksen86737662008-01-05 16:56:59 +00002484 }
2485 }
2486
2487 if (!MemOpChains2.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00002488 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Arnold Schwaighofer719eb022008-01-11 14:34:56 +00002489 &MemOpChains2[0], MemOpChains2.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00002490
2491 // Store the return address to the appropriate stack slot.
Michael Liaoaa3c2c02012-10-25 06:29:14 +00002492 Chain = EmitTailCallStoreRetAddr(DAG, MF, Chain, RetAddrFrIdx,
2493 getPointerTy(), RegInfo->getSlotSize(),
Dale Johannesenace16102009-02-03 19:33:06 +00002494 FPDiff, dl);
Gordon Henriksen86737662008-01-05 16:56:59 +00002495 }
2496
Jakob Stoklund Olesenb8720782012-07-04 19:28:31 +00002497 // Build a sequence of copy-to-reg nodes chained together with token chain
2498 // and flag operands which copy the outgoing args into registers.
2499 SDValue InFlag;
2500 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
2501 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
2502 RegsToPass[i].second, InFlag);
2503 InFlag = Chain.getValue(1);
2504 }
2505
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002506 if (getTargetMachine().getCodeModel() == CodeModel::Large) {
2507 assert(Is64Bit && "Large code model is only legal in 64-bit mode.");
2508 // In the 64-bit large code model, we have to make all calls
2509 // through a register, since the call instruction's 32-bit
2510 // pc-relative offset may not be large enough to hold the whole
2511 // address.
2512 } else if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002513 // If the callee is a GlobalAddress node (quite common, every direct call
2514 // is) turn it into a TargetGlobalAddress node so that legalize doesn't hack
2515 // it.
2516
Anton Korobeynikov2b2bc682006-12-22 22:29:05 +00002517 // We should use extra load for direct calls to dllimported functions in
2518 // non-JIT mode.
Dan Gohman46510a72010-04-15 01:51:59 +00002519 const GlobalValue *GV = G->getGlobal();
Chris Lattner754b7652009-07-10 05:48:03 +00002520 if (!GV->hasDLLImportLinkage()) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002521 unsigned char OpFlags = 0;
John McCall3a3465b2011-06-15 20:36:13 +00002522 bool ExtraLoad = false;
2523 unsigned WrapperKind = ISD::DELETED_NODE;
Eric Christopherfd179292009-08-27 18:07:15 +00002524
Chris Lattner48a7d022009-07-09 05:02:21 +00002525 // On ELF targets, in both X86-64 and X86-32 mode, direct calls to
2526 // external symbols most go through the PLT in PIC mode. If the symbol
2527 // has hidden or protected visibility, or if it is static or local, then
2528 // we don't need to use the PLT - we can directly call it.
2529 if (Subtarget->isTargetELF() &&
2530 getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
Chris Lattner74e726e2009-07-09 05:27:35 +00002531 GV->hasDefaultVisibility() && !GV->hasLocalLinkage()) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002532 OpFlags = X86II::MO_PLT;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00002533 } else if (Subtarget->isPICStyleStubAny() &&
Chris Lattner80945782010-09-27 06:34:01 +00002534 (GV->isDeclaration() || GV->isWeakForLinker()) &&
Daniel Dunbar558692f2011-04-20 00:14:25 +00002535 (!Subtarget->getTargetTriple().isMacOSX() ||
2536 Subtarget->getTargetTriple().isMacOSXVersionLT(10, 5))) {
Chris Lattner74e726e2009-07-09 05:27:35 +00002537 // PC-relative references to external symbols should go through $stub,
2538 // unless we're building with the leopard linker or later, which
2539 // automatically synthesizes these stubs.
2540 OpFlags = X86II::MO_DARWIN_STUB;
John McCall3a3465b2011-06-15 20:36:13 +00002541 } else if (Subtarget->isPICStyleRIPRel() &&
2542 isa<Function>(GV) &&
Bill Wendling67658342012-10-09 07:45:08 +00002543 cast<Function>(GV)->getFnAttributes().
Bill Wendling034b94b2012-12-19 07:18:57 +00002544 hasAttribute(Attribute::NonLazyBind)) {
John McCall3a3465b2011-06-15 20:36:13 +00002545 // If the function is marked as non-lazy, generate an indirect call
2546 // which loads from the GOT directly. This avoids runtime overhead
2547 // at the cost of eager binding (and one extra byte of encoding).
2548 OpFlags = X86II::MO_GOTPCREL;
2549 WrapperKind = X86ISD::WrapperRIP;
2550 ExtraLoad = true;
Chris Lattner74e726e2009-07-09 05:27:35 +00002551 }
Chris Lattner48a7d022009-07-09 05:02:21 +00002552
Devang Patel0d881da2010-07-06 22:08:15 +00002553 Callee = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(),
Chris Lattner48a7d022009-07-09 05:02:21 +00002554 G->getOffset(), OpFlags);
John McCall3a3465b2011-06-15 20:36:13 +00002555
2556 // Add a wrapper if needed.
2557 if (WrapperKind != ISD::DELETED_NODE)
2558 Callee = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Callee);
2559 // Add extra indirection if needed.
2560 if (ExtraLoad)
2561 Callee = DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(), Callee,
2562 MachinePointerInfo::getGOT(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002563 false, false, false, 0);
Chris Lattner48a7d022009-07-09 05:02:21 +00002564 }
Bill Wendling056292f2008-09-16 21:48:12 +00002565 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002566 unsigned char OpFlags = 0;
2567
Evan Cheng1bf891a2010-12-01 22:59:46 +00002568 // On ELF targets, in either X86-64 or X86-32 mode, direct calls to
2569 // external symbols should go through the PLT.
2570 if (Subtarget->isTargetELF() &&
2571 getTargetMachine().getRelocationModel() == Reloc::PIC_) {
2572 OpFlags = X86II::MO_PLT;
2573 } else if (Subtarget->isPICStyleStubAny() &&
Daniel Dunbar558692f2011-04-20 00:14:25 +00002574 (!Subtarget->getTargetTriple().isMacOSX() ||
2575 Subtarget->getTargetTriple().isMacOSXVersionLT(10, 5))) {
Evan Cheng1bf891a2010-12-01 22:59:46 +00002576 // PC-relative references to external symbols should go through $stub,
2577 // unless we're building with the leopard linker or later, which
2578 // automatically synthesizes these stubs.
2579 OpFlags = X86II::MO_DARWIN_STUB;
Chris Lattner74e726e2009-07-09 05:27:35 +00002580 }
Eric Christopherfd179292009-08-27 18:07:15 +00002581
Chris Lattner48a7d022009-07-09 05:02:21 +00002582 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy(),
2583 OpFlags);
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002584 }
2585
Chris Lattnerd96d0722007-02-25 06:40:16 +00002586 // Returns a chain & a flag for retval copy to use.
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002587 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Dan Gohman475871a2008-07-27 21:46:04 +00002588 SmallVector<SDValue, 8> Ops;
Gordon Henriksen86737662008-01-05 16:56:59 +00002589
Evan Chengf22f9b32010-02-06 03:28:46 +00002590 if (!IsSibcall && isTailCall) {
Dale Johannesene8d72302009-02-06 23:05:02 +00002591 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
2592 DAG.getIntPtrConstant(0, true), InFlag);
Gordon Henriksen86737662008-01-05 16:56:59 +00002593 InFlag = Chain.getValue(1);
Gordon Henriksen86737662008-01-05 16:56:59 +00002594 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002595
Nate Begeman4c5dcf52006-02-17 00:03:04 +00002596 Ops.push_back(Chain);
2597 Ops.push_back(Callee);
Evan Chengb69d1132006-06-14 18:17:40 +00002598
Dan Gohman98ca4f22009-08-05 01:29:28 +00002599 if (isTailCall)
Owen Anderson825b72b2009-08-11 20:47:22 +00002600 Ops.push_back(DAG.getConstant(FPDiff, MVT::i32));
Evan Chengf4684712007-02-21 21:18:14 +00002601
Gordon Henriksen86737662008-01-05 16:56:59 +00002602 // Add argument registers to the end of the list so that they are known live
2603 // into the call.
Evan Cheng9b449442008-01-07 23:08:23 +00002604 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
2605 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
2606 RegsToPass[i].second.getValueType()));
Scott Michelfdc40a02009-02-17 22:15:04 +00002607
Jakob Stoklund Olesen8bcde2a2012-02-16 00:02:50 +00002608 // Add a register mask operand representing the call-preserved registers.
2609 const TargetRegisterInfo *TRI = getTargetMachine().getRegisterInfo();
2610 const uint32_t *Mask = TRI->getCallPreservedMask(CallConv);
2611 assert(Mask && "Missing call preserved mask for calling convention");
2612 Ops.push_back(DAG.getRegisterMask(Mask));
Jakob Stoklund Olesenc38c4562012-01-18 23:52:22 +00002613
Gabor Greifba36cb52008-08-28 21:40:38 +00002614 if (InFlag.getNode())
Evan Cheng347d5f72006-04-28 21:29:37 +00002615 Ops.push_back(InFlag);
Gordon Henriksenae636f82008-01-03 16:47:34 +00002616
Dan Gohman98ca4f22009-08-05 01:29:28 +00002617 if (isTailCall) {
Dale Johannesen88004c22010-06-05 00:30:45 +00002618 // We used to do:
2619 //// If this is the first return lowered for this function, add the regs
2620 //// to the liveout set for the function.
2621 // This isn't right, although it's probably harmless on x86; liveouts
2622 // should be computed from returns not tail calls. Consider a void
2623 // function making a tail call to a function returning int.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002624 return DAG.getNode(X86ISD::TC_RETURN, dl,
2625 NodeTys, &Ops[0], Ops.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00002626 }
2627
Dale Johannesenace16102009-02-03 19:33:06 +00002628 Chain = DAG.getNode(X86ISD::CALL, dl, NodeTys, &Ops[0], Ops.size());
Evan Cheng347d5f72006-04-28 21:29:37 +00002629 InFlag = Chain.getValue(1);
Evan Chengd90eb7f2006-01-05 00:27:02 +00002630
Chris Lattner2d297092006-05-23 18:50:38 +00002631 // Create the CALLSEQ_END node.
Gordon Henriksen86737662008-01-05 16:56:59 +00002632 unsigned NumBytesForCalleeToPush;
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002633 if (X86::isCalleePop(CallConv, Is64Bit, isVarArg,
2634 getTargetMachine().Options.GuaranteedTailCallOpt))
Gordon Henriksen86737662008-01-05 16:56:59 +00002635 NumBytesForCalleeToPush = NumBytes; // Callee pops everything
Eli Friedman9a2478a2012-01-20 00:05:46 +00002636 else if (!Is64Bit && !IsTailCallConvention(CallConv) && !IsWindows &&
Rafael Espindola1cee7102012-07-25 13:41:10 +00002637 SR == StackStructReturn)
Dan Gohmanf451cb82010-02-10 16:03:48 +00002638 // If this is a call to a struct-return function, the callee
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00002639 // pops the hidden struct pointer, so we have to push it back.
2640 // This is common for Darwin/X86, Linux & Mingw32 targets.
Eli Friedman9a2478a2012-01-20 00:05:46 +00002641 // For MSVC Win32 targets, the caller pops the hidden struct pointer.
Gordon Henriksenae636f82008-01-03 16:47:34 +00002642 NumBytesForCalleeToPush = 4;
Gordon Henriksen86737662008-01-05 16:56:59 +00002643 else
Gordon Henriksenae636f82008-01-03 16:47:34 +00002644 NumBytesForCalleeToPush = 0; // Callee pops nothing.
Scott Michelfdc40a02009-02-17 22:15:04 +00002645
Gordon Henriksenae636f82008-01-03 16:47:34 +00002646 // Returns a flag for retval copy to use.
Evan Chengf22f9b32010-02-06 03:28:46 +00002647 if (!IsSibcall) {
2648 Chain = DAG.getCALLSEQ_END(Chain,
2649 DAG.getIntPtrConstant(NumBytes, true),
2650 DAG.getIntPtrConstant(NumBytesForCalleeToPush,
2651 true),
2652 InFlag);
2653 InFlag = Chain.getValue(1);
2654 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00002655
Chris Lattner3085e152007-02-25 08:59:22 +00002656 // Handle result values, copying them out of physregs into vregs that we
2657 // return.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002658 return LowerCallResult(Chain, InFlag, CallConv, isVarArg,
2659 Ins, dl, DAG, InVals);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002660}
2661
Evan Cheng25ab6902006-09-08 06:48:29 +00002662//===----------------------------------------------------------------------===//
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002663// Fast Calling Convention (tail call) implementation
2664//===----------------------------------------------------------------------===//
2665
2666// Like std call, callee cleans arguments, convention except that ECX is
2667// reserved for storing the tail called function address. Only 2 registers are
2668// free for argument passing (inreg). Tail call optimization is performed
2669// provided:
2670// * tailcallopt is enabled
2671// * caller/callee are fastcc
Arnold Schwaighofera2a4b472008-02-26 10:21:54 +00002672// On X86_64 architecture with GOT-style position independent code only local
2673// (within module) calls are supported at the moment.
Arnold Schwaighofer48abc5c2007-10-12 21:30:57 +00002674// To keep the stack aligned according to platform abi the function
2675// GetAlignedArgumentStackSize ensures that argument delta is always multiples
2676// of stack alignment. (Dynamic linkers need this - darwin's dyld for example)
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002677// If a tail called function callee has more arguments than the caller the
2678// caller needs to make sure that there is room to move the RETADDR to. This is
Arnold Schwaighofer48abc5c2007-10-12 21:30:57 +00002679// achieved by reserving an area the size of the argument delta right after the
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002680// original REtADDR, but before the saved framepointer or the spilled registers
2681// e.g. caller(arg1, arg2) calls callee(arg1, arg2,arg3,arg4)
2682// stack layout:
2683// arg1
2684// arg2
2685// RETADDR
Scott Michelfdc40a02009-02-17 22:15:04 +00002686// [ new RETADDR
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002687// move area ]
2688// (possible EBP)
2689// ESI
2690// EDI
2691// local1 ..
2692
2693/// GetAlignedArgumentStackSize - Make the stack size align e.g 16n + 12 aligned
2694/// for a 16 byte align requirement.
Dan Gohmand858e902010-04-17 15:26:15 +00002695unsigned
2696X86TargetLowering::GetAlignedArgumentStackSize(unsigned StackSize,
2697 SelectionDAG& DAG) const {
Evan Chenge9ac9e62008-09-07 09:07:23 +00002698 MachineFunction &MF = DAG.getMachineFunction();
2699 const TargetMachine &TM = MF.getTarget();
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00002700 const TargetFrameLowering &TFI = *TM.getFrameLowering();
Evan Chenge9ac9e62008-09-07 09:07:23 +00002701 unsigned StackAlignment = TFI.getStackAlignment();
Scott Michelfdc40a02009-02-17 22:15:04 +00002702 uint64_t AlignMask = StackAlignment - 1;
Evan Chenge9ac9e62008-09-07 09:07:23 +00002703 int64_t Offset = StackSize;
Michael Liaoaa3c2c02012-10-25 06:29:14 +00002704 unsigned SlotSize = RegInfo->getSlotSize();
Evan Chenge9ac9e62008-09-07 09:07:23 +00002705 if ( (Offset & AlignMask) <= (StackAlignment - SlotSize) ) {
2706 // Number smaller than 12 so just add the difference.
2707 Offset += ((StackAlignment - SlotSize) - (Offset & AlignMask));
2708 } else {
2709 // Mask out lower bits, add stackalignment once plus the 12 bytes.
Scott Michelfdc40a02009-02-17 22:15:04 +00002710 Offset = ((~AlignMask) & Offset) + StackAlignment +
Evan Chenge9ac9e62008-09-07 09:07:23 +00002711 (StackAlignment-SlotSize);
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002712 }
Evan Chenge9ac9e62008-09-07 09:07:23 +00002713 return Offset;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002714}
2715
Evan Cheng5f941932010-02-05 02:21:12 +00002716/// MatchingStackOffset - Return true if the given stack call argument is
2717/// already available in the same position (relatively) of the caller's
2718/// incoming argument stack.
2719static
2720bool MatchingStackOffset(SDValue Arg, unsigned Offset, ISD::ArgFlagsTy Flags,
2721 MachineFrameInfo *MFI, const MachineRegisterInfo *MRI,
2722 const X86InstrInfo *TII) {
Evan Cheng4cae1332010-03-05 08:38:04 +00002723 unsigned Bytes = Arg.getValueType().getSizeInBits() / 8;
2724 int FI = INT_MAX;
Evan Cheng5f941932010-02-05 02:21:12 +00002725 if (Arg.getOpcode() == ISD::CopyFromReg) {
2726 unsigned VR = cast<RegisterSDNode>(Arg.getOperand(1))->getReg();
Jakob Stoklund Olesenc9df0252011-01-10 02:58:51 +00002727 if (!TargetRegisterInfo::isVirtualRegister(VR))
Evan Cheng5f941932010-02-05 02:21:12 +00002728 return false;
2729 MachineInstr *Def = MRI->getVRegDef(VR);
2730 if (!Def)
2731 return false;
2732 if (!Flags.isByVal()) {
2733 if (!TII->isLoadFromStackSlot(Def, FI))
2734 return false;
2735 } else {
2736 unsigned Opcode = Def->getOpcode();
2737 if ((Opcode == X86::LEA32r || Opcode == X86::LEA64r) &&
2738 Def->getOperand(1).isFI()) {
2739 FI = Def->getOperand(1).getIndex();
Evan Cheng4cae1332010-03-05 08:38:04 +00002740 Bytes = Flags.getByValSize();
Evan Cheng5f941932010-02-05 02:21:12 +00002741 } else
2742 return false;
2743 }
Evan Cheng4cae1332010-03-05 08:38:04 +00002744 } else if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Arg)) {
2745 if (Flags.isByVal())
2746 // ByVal argument is passed in as a pointer but it's now being
Evan Cheng10718492010-03-05 19:55:55 +00002747 // dereferenced. e.g.
Evan Cheng4cae1332010-03-05 08:38:04 +00002748 // define @foo(%struct.X* %A) {
2749 // tail call @bar(%struct.X* byval %A)
2750 // }
Evan Cheng5f941932010-02-05 02:21:12 +00002751 return false;
2752 SDValue Ptr = Ld->getBasePtr();
2753 FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr);
2754 if (!FINode)
2755 return false;
2756 FI = FINode->getIndex();
Chad Rosierdf78fcd2011-06-25 02:04:56 +00002757 } else if (Arg.getOpcode() == ISD::FrameIndex && Flags.isByVal()) {
Chad Rosier14d71aa2011-06-25 18:51:28 +00002758 FrameIndexSDNode *FINode = cast<FrameIndexSDNode>(Arg);
Chad Rosierdf78fcd2011-06-25 02:04:56 +00002759 FI = FINode->getIndex();
2760 Bytes = Flags.getByValSize();
Evan Cheng4cae1332010-03-05 08:38:04 +00002761 } else
2762 return false;
Evan Cheng5f941932010-02-05 02:21:12 +00002763
Evan Cheng4cae1332010-03-05 08:38:04 +00002764 assert(FI != INT_MAX);
Evan Cheng5f941932010-02-05 02:21:12 +00002765 if (!MFI->isFixedObjectIndex(FI))
2766 return false;
Evan Cheng4cae1332010-03-05 08:38:04 +00002767 return Offset == MFI->getObjectOffset(FI) && Bytes == MFI->getObjectSize(FI);
Evan Cheng5f941932010-02-05 02:21:12 +00002768}
2769
Dan Gohman98ca4f22009-08-05 01:29:28 +00002770/// IsEligibleForTailCallOptimization - Check whether the call is eligible
2771/// for tail call optimization. Targets which want to do tail call
2772/// optimization should implement this function.
2773bool
2774X86TargetLowering::IsEligibleForTailCallOptimization(SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00002775 CallingConv::ID CalleeCC,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002776 bool isVarArg,
Evan Chenga375d472010-03-15 18:54:48 +00002777 bool isCalleeStructRet,
2778 bool isCallerStructRet,
Evan Chengb1cacc72012-09-25 05:32:34 +00002779 Type *RetTy,
Evan Chengb1712452010-01-27 06:25:16 +00002780 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00002781 const SmallVectorImpl<SDValue> &OutVals,
Evan Chengb1712452010-01-27 06:25:16 +00002782 const SmallVectorImpl<ISD::InputArg> &Ins,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002783 SelectionDAG& DAG) const {
Chris Lattner29689432010-03-11 00:22:57 +00002784 if (!IsTailCallConvention(CalleeCC) &&
Evan Chengb1712452010-01-27 06:25:16 +00002785 CalleeCC != CallingConv::C)
2786 return false;
2787
Evan Cheng7096ae42010-01-29 06:45:59 +00002788 // If -tailcallopt is specified, make fastcc functions tail-callable.
Evan Cheng2c12cb42010-03-26 16:26:03 +00002789 const MachineFunction &MF = DAG.getMachineFunction();
Evan Cheng7096ae42010-01-29 06:45:59 +00002790 const Function *CallerF = DAG.getMachineFunction().getFunction();
Evan Chengb1cacc72012-09-25 05:32:34 +00002791
2792 // If the function return type is x86_fp80 and the callee return type is not,
2793 // then the FP_EXTEND of the call result is not a nop. It's not safe to
2794 // perform a tailcall optimization here.
2795 if (CallerF->getReturnType()->isX86_FP80Ty() && !RetTy->isX86_FP80Ty())
2796 return false;
2797
Evan Cheng13617962010-04-30 01:12:32 +00002798 CallingConv::ID CallerCC = CallerF->getCallingConv();
2799 bool CCMatch = CallerCC == CalleeCC;
2800
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002801 if (getTargetMachine().Options.GuaranteedTailCallOpt) {
Evan Cheng13617962010-04-30 01:12:32 +00002802 if (IsTailCallConvention(CalleeCC) && CCMatch)
Evan Cheng843bd692010-01-31 06:44:49 +00002803 return true;
2804 return false;
2805 }
2806
Dale Johannesen2f05cc02010-05-28 23:24:28 +00002807 // Look for obvious safe cases to perform tail call optimization that do not
2808 // require ABI changes. This is what gcc calls sibcall.
Evan Chengb2c92902010-02-02 02:22:50 +00002809
Evan Cheng2c12cb42010-03-26 16:26:03 +00002810 // Can't do sibcall if stack needs to be dynamically re-aligned. PEI needs to
2811 // emit a special epilogue.
2812 if (RegInfo->needsStackRealignment(MF))
2813 return false;
2814
Evan Chenga375d472010-03-15 18:54:48 +00002815 // Also avoid sibcall optimization if either caller or callee uses struct
2816 // return semantics.
2817 if (isCalleeStructRet || isCallerStructRet)
2818 return false;
2819
Chad Rosier2416da32011-06-24 21:15:36 +00002820 // An stdcall caller is expected to clean up its arguments; the callee
2821 // isn't going to do that.
2822 if (!CCMatch && CallerCC==CallingConv::X86_StdCall)
2823 return false;
2824
Chad Rosier871f6642011-05-18 19:59:50 +00002825 // Do not sibcall optimize vararg calls unless all arguments are passed via
Chad Rosiera1660892011-05-20 00:59:28 +00002826 // registers.
Chad Rosier871f6642011-05-18 19:59:50 +00002827 if (isVarArg && !Outs.empty()) {
Chad Rosiera1660892011-05-20 00:59:28 +00002828
2829 // Optimizing for varargs on Win64 is unlikely to be safe without
2830 // additional testing.
2831 if (Subtarget->isTargetWin64())
2832 return false;
2833
Chad Rosier871f6642011-05-18 19:59:50 +00002834 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002835 CCState CCInfo(CalleeCC, isVarArg, DAG.getMachineFunction(),
Craig Topper0fbf3642012-04-23 03:28:34 +00002836 getTargetMachine(), ArgLocs, *DAG.getContext());
Chad Rosier871f6642011-05-18 19:59:50 +00002837
Chad Rosier871f6642011-05-18 19:59:50 +00002838 CCInfo.AnalyzeCallOperands(Outs, CC_X86);
2839 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i)
2840 if (!ArgLocs[i].isRegLoc())
2841 return false;
2842 }
2843
Chad Rosier30450e82011-12-22 22:35:21 +00002844 // If the call result is in ST0 / ST1, it needs to be popped off the x87
2845 // stack. Therefore, if it's not used by the call it is not safe to optimize
2846 // this into a sibcall.
Evan Chengf5b9d6c2010-03-20 02:58:15 +00002847 bool Unused = false;
2848 for (unsigned i = 0, e = Ins.size(); i != e; ++i) {
2849 if (!Ins[i].Used) {
2850 Unused = true;
2851 break;
2852 }
2853 }
2854 if (Unused) {
2855 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002856 CCState CCInfo(CalleeCC, false, DAG.getMachineFunction(),
Craig Topper0fbf3642012-04-23 03:28:34 +00002857 getTargetMachine(), RVLocs, *DAG.getContext());
Evan Chengf5b9d6c2010-03-20 02:58:15 +00002858 CCInfo.AnalyzeCallResult(Ins, RetCC_X86);
Evan Cheng13617962010-04-30 01:12:32 +00002859 for (unsigned i = 0, e = RVLocs.size(); i != e; ++i) {
Evan Chengf5b9d6c2010-03-20 02:58:15 +00002860 CCValAssign &VA = RVLocs[i];
2861 if (VA.getLocReg() == X86::ST0 || VA.getLocReg() == X86::ST1)
2862 return false;
2863 }
2864 }
2865
Evan Cheng13617962010-04-30 01:12:32 +00002866 // If the calling conventions do not match, then we'd better make sure the
2867 // results are returned in the same way as what the caller expects.
2868 if (!CCMatch) {
2869 SmallVector<CCValAssign, 16> RVLocs1;
Eric Christopher471e4222011-06-08 23:55:35 +00002870 CCState CCInfo1(CalleeCC, false, DAG.getMachineFunction(),
Craig Topper0fbf3642012-04-23 03:28:34 +00002871 getTargetMachine(), RVLocs1, *DAG.getContext());
Evan Cheng13617962010-04-30 01:12:32 +00002872 CCInfo1.AnalyzeCallResult(Ins, RetCC_X86);
2873
2874 SmallVector<CCValAssign, 16> RVLocs2;
Eric Christopher471e4222011-06-08 23:55:35 +00002875 CCState CCInfo2(CallerCC, false, DAG.getMachineFunction(),
Craig Topper0fbf3642012-04-23 03:28:34 +00002876 getTargetMachine(), RVLocs2, *DAG.getContext());
Evan Cheng13617962010-04-30 01:12:32 +00002877 CCInfo2.AnalyzeCallResult(Ins, RetCC_X86);
2878
2879 if (RVLocs1.size() != RVLocs2.size())
2880 return false;
2881 for (unsigned i = 0, e = RVLocs1.size(); i != e; ++i) {
2882 if (RVLocs1[i].isRegLoc() != RVLocs2[i].isRegLoc())
2883 return false;
2884 if (RVLocs1[i].getLocInfo() != RVLocs2[i].getLocInfo())
2885 return false;
2886 if (RVLocs1[i].isRegLoc()) {
2887 if (RVLocs1[i].getLocReg() != RVLocs2[i].getLocReg())
2888 return false;
2889 } else {
2890 if (RVLocs1[i].getLocMemOffset() != RVLocs2[i].getLocMemOffset())
2891 return false;
2892 }
2893 }
2894 }
2895
Evan Chenga6bff982010-01-30 01:22:00 +00002896 // If the callee takes no arguments then go on to check the results of the
2897 // call.
2898 if (!Outs.empty()) {
2899 // Check if stack adjustment is needed. For now, do not do this if any
2900 // argument is passed on the stack.
2901 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002902 CCState CCInfo(CalleeCC, isVarArg, DAG.getMachineFunction(),
Craig Topper0fbf3642012-04-23 03:28:34 +00002903 getTargetMachine(), ArgLocs, *DAG.getContext());
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00002904
2905 // Allocate shadow area for Win64
2906 if (Subtarget->isTargetWin64()) {
2907 CCInfo.AllocateStack(32, 8);
2908 }
2909
Duncan Sands45907662010-10-31 13:21:44 +00002910 CCInfo.AnalyzeCallOperands(Outs, CC_X86);
Stuart Hastings6db2c2f2011-05-17 16:59:46 +00002911 if (CCInfo.getNextStackOffset()) {
Evan Chengb2c92902010-02-02 02:22:50 +00002912 MachineFunction &MF = DAG.getMachineFunction();
2913 if (MF.getInfo<X86MachineFunctionInfo>()->getBytesToPopOnReturn())
2914 return false;
Evan Chengb2c92902010-02-02 02:22:50 +00002915
2916 // Check if the arguments are already laid out in the right way as
2917 // the caller's fixed stack objects.
2918 MachineFrameInfo *MFI = MF.getFrameInfo();
Evan Cheng5f941932010-02-05 02:21:12 +00002919 const MachineRegisterInfo *MRI = &MF.getRegInfo();
2920 const X86InstrInfo *TII =
Roman Divacky59324292012-09-05 22:26:57 +00002921 ((const X86TargetMachine&)getTargetMachine()).getInstrInfo();
Evan Chengb2c92902010-02-02 02:22:50 +00002922 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2923 CCValAssign &VA = ArgLocs[i];
Dan Gohmanc9403652010-07-07 15:54:55 +00002924 SDValue Arg = OutVals[i];
Evan Chengb2c92902010-02-02 02:22:50 +00002925 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Evan Chengb2c92902010-02-02 02:22:50 +00002926 if (VA.getLocInfo() == CCValAssign::Indirect)
2927 return false;
2928 if (!VA.isRegLoc()) {
Evan Cheng5f941932010-02-05 02:21:12 +00002929 if (!MatchingStackOffset(Arg, VA.getLocMemOffset(), Flags,
2930 MFI, MRI, TII))
Evan Chengb2c92902010-02-02 02:22:50 +00002931 return false;
2932 }
2933 }
2934 }
Evan Cheng9c044672010-05-29 01:35:22 +00002935
2936 // If the tailcall address may be in a register, then make sure it's
2937 // possible to register allocate for it. In 32-bit, the call address can
2938 // only target EAX, EDX, or ECX since the tail call must be scheduled after
Evan Chengdedd9742010-07-14 06:44:01 +00002939 // callee-saved registers are restored. These happen to be the same
2940 // registers used to pass 'inreg' arguments so watch out for those.
2941 if (!Subtarget->is64Bit() &&
2942 !isa<GlobalAddressSDNode>(Callee) &&
Evan Cheng9c044672010-05-29 01:35:22 +00002943 !isa<ExternalSymbolSDNode>(Callee)) {
Evan Cheng9c044672010-05-29 01:35:22 +00002944 unsigned NumInRegs = 0;
2945 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2946 CCValAssign &VA = ArgLocs[i];
Evan Chengdedd9742010-07-14 06:44:01 +00002947 if (!VA.isRegLoc())
2948 continue;
2949 unsigned Reg = VA.getLocReg();
2950 switch (Reg) {
2951 default: break;
2952 case X86::EAX: case X86::EDX: case X86::ECX:
2953 if (++NumInRegs == 3)
Evan Cheng9c044672010-05-29 01:35:22 +00002954 return false;
Evan Chengdedd9742010-07-14 06:44:01 +00002955 break;
Evan Cheng9c044672010-05-29 01:35:22 +00002956 }
2957 }
2958 }
Evan Chenga6bff982010-01-30 01:22:00 +00002959 }
Evan Chengb1712452010-01-27 06:25:16 +00002960
Evan Cheng86809cc2010-02-03 03:28:02 +00002961 return true;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002962}
2963
Dan Gohman3df24e62008-09-03 23:12:08 +00002964FastISel *
Bob Wilsond49edb72012-08-03 04:06:28 +00002965X86TargetLowering::createFastISel(FunctionLoweringInfo &funcInfo,
2966 const TargetLibraryInfo *libInfo) const {
2967 return X86::createFastISel(funcInfo, libInfo);
Dan Gohmand9f3c482008-08-19 21:32:53 +00002968}
2969
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00002970//===----------------------------------------------------------------------===//
2971// Other Lowering Hooks
2972//===----------------------------------------------------------------------===//
2973
Bruno Cardoso Lopese654b562010-09-01 00:51:36 +00002974static bool MayFoldLoad(SDValue Op) {
2975 return Op.hasOneUse() && ISD::isNormalLoad(Op.getNode());
2976}
2977
2978static bool MayFoldIntoStore(SDValue Op) {
2979 return Op.hasOneUse() && ISD::isNormalStore(*Op.getNode()->use_begin());
2980}
2981
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002982static bool isTargetShuffle(unsigned Opcode) {
2983 switch(Opcode) {
2984 default: return false;
2985 case X86ISD::PSHUFD:
2986 case X86ISD::PSHUFHW:
2987 case X86ISD::PSHUFLW:
Craig Topperb3982da2011-12-31 23:50:21 +00002988 case X86ISD::SHUFP:
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00002989 case X86ISD::PALIGN:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002990 case X86ISD::MOVLHPS:
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00002991 case X86ISD::MOVLHPD:
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00002992 case X86ISD::MOVHLPS:
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00002993 case X86ISD::MOVLPS:
2994 case X86ISD::MOVLPD:
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00002995 case X86ISD::MOVSHDUP:
Bruno Cardoso Lopes013bb3d2010-08-31 22:35:05 +00002996 case X86ISD::MOVSLDUP:
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00002997 case X86ISD::MOVDDUP:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002998 case X86ISD::MOVSS:
2999 case X86ISD::MOVSD:
Craig Topper34671b82011-12-06 08:21:25 +00003000 case X86ISD::UNPCKL:
3001 case X86ISD::UNPCKH:
Craig Topper316cd2a2011-11-30 06:25:25 +00003002 case X86ISD::VPERMILP:
Craig Topperec24e612011-11-30 07:47:51 +00003003 case X86ISD::VPERM2X128:
Craig Topperbdcbcb32012-05-06 18:54:26 +00003004 case X86ISD::VPERMI:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003005 return true;
3006 }
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003007}
3008
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00003009static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
Craig Topper3d092db2012-03-21 02:14:01 +00003010 SDValue V1, SelectionDAG &DAG) {
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00003011 switch(Opc) {
3012 default: llvm_unreachable("Unknown x86 shuffle node");
3013 case X86ISD::MOVSHDUP:
Bruno Cardoso Lopes013bb3d2010-08-31 22:35:05 +00003014 case X86ISD::MOVSLDUP:
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00003015 case X86ISD::MOVDDUP:
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00003016 return DAG.getNode(Opc, dl, VT, V1);
3017 }
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00003018}
3019
3020static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
Craig Topper3d092db2012-03-21 02:14:01 +00003021 SDValue V1, unsigned TargetMask,
3022 SelectionDAG &DAG) {
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00003023 switch(Opc) {
3024 default: llvm_unreachable("Unknown x86 shuffle node");
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00003025 case X86ISD::PSHUFD:
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00003026 case X86ISD::PSHUFHW:
3027 case X86ISD::PSHUFLW:
Craig Topper316cd2a2011-11-30 06:25:25 +00003028 case X86ISD::VPERMILP:
Craig Topper8325c112012-04-16 00:41:45 +00003029 case X86ISD::VPERMI:
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00003030 return DAG.getNode(Opc, dl, VT, V1, DAG.getConstant(TargetMask, MVT::i8));
3031 }
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00003032}
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00003033
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00003034static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
Craig Topper3d092db2012-03-21 02:14:01 +00003035 SDValue V1, SDValue V2, unsigned TargetMask,
3036 SelectionDAG &DAG) {
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00003037 switch(Opc) {
3038 default: llvm_unreachable("Unknown x86 shuffle node");
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00003039 case X86ISD::PALIGN:
Craig Topperb3982da2011-12-31 23:50:21 +00003040 case X86ISD::SHUFP:
Craig Topperec24e612011-11-30 07:47:51 +00003041 case X86ISD::VPERM2X128:
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00003042 return DAG.getNode(Opc, dl, VT, V1, V2,
3043 DAG.getConstant(TargetMask, MVT::i8));
3044 }
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00003045}
3046
3047static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
3048 SDValue V1, SDValue V2, SelectionDAG &DAG) {
3049 switch(Opc) {
3050 default: llvm_unreachable("Unknown x86 shuffle node");
3051 case X86ISD::MOVLHPS:
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00003052 case X86ISD::MOVLHPD:
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00003053 case X86ISD::MOVHLPS:
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00003054 case X86ISD::MOVLPS:
3055 case X86ISD::MOVLPD:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003056 case X86ISD::MOVSS:
3057 case X86ISD::MOVSD:
Craig Topper34671b82011-12-06 08:21:25 +00003058 case X86ISD::UNPCKL:
3059 case X86ISD::UNPCKH:
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00003060 return DAG.getNode(Opc, dl, VT, V1, V2);
3061 }
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00003062}
3063
Dan Gohmand858e902010-04-17 15:26:15 +00003064SDValue X86TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) const {
Anton Korobeynikova2780e12007-08-15 17:12:32 +00003065 MachineFunction &MF = DAG.getMachineFunction();
3066 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
3067 int ReturnAddrIndex = FuncInfo->getRAIndex();
3068
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00003069 if (ReturnAddrIndex == 0) {
3070 // Set up a frame object for the return address.
Michael Liaoaa3c2c02012-10-25 06:29:14 +00003071 unsigned SlotSize = RegInfo->getSlotSize();
David Greene3f2bf852009-11-12 20:49:22 +00003072 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(SlotSize, -SlotSize,
Evan Chenged2ae132010-07-03 00:40:23 +00003073 false);
Anton Korobeynikova2780e12007-08-15 17:12:32 +00003074 FuncInfo->setRAIndex(ReturnAddrIndex);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00003075 }
3076
Evan Cheng25ab6902006-09-08 06:48:29 +00003077 return DAG.getFrameIndex(ReturnAddrIndex, getPointerTy());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00003078}
3079
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00003080bool X86::isOffsetSuitableForCodeModel(int64_t Offset, CodeModel::Model M,
3081 bool hasSymbolicDisplacement) {
3082 // Offset should fit into 32 bit immediate field.
Benjamin Kramer34247a02010-03-29 21:13:41 +00003083 if (!isInt<32>(Offset))
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00003084 return false;
3085
3086 // If we don't have a symbolic displacement - we don't have any extra
3087 // restrictions.
3088 if (!hasSymbolicDisplacement)
3089 return true;
3090
3091 // FIXME: Some tweaks might be needed for medium code model.
3092 if (M != CodeModel::Small && M != CodeModel::Kernel)
3093 return false;
3094
3095 // For small code model we assume that latest object is 16MB before end of 31
3096 // bits boundary. We may also accept pretty large negative constants knowing
3097 // that all objects are in the positive half of address space.
3098 if (M == CodeModel::Small && Offset < 16*1024*1024)
3099 return true;
3100
3101 // For kernel code model we know that all object resist in the negative half
3102 // of 32bits address space. We may not accept negative offsets, since they may
3103 // be just off and we may accept pretty large positive ones.
3104 if (M == CodeModel::Kernel && Offset > 0)
3105 return true;
3106
3107 return false;
3108}
3109
Evan Chengef41ff62011-06-23 17:54:54 +00003110/// isCalleePop - Determines whether the callee is required to pop its
3111/// own arguments. Callee pop is necessary to support tail calls.
3112bool X86::isCalleePop(CallingConv::ID CallingConv,
3113 bool is64Bit, bool IsVarArg, bool TailCallOpt) {
3114 if (IsVarArg)
3115 return false;
3116
3117 switch (CallingConv) {
3118 default:
3119 return false;
3120 case CallingConv::X86_StdCall:
3121 return !is64Bit;
3122 case CallingConv::X86_FastCall:
3123 return !is64Bit;
3124 case CallingConv::X86_ThisCall:
3125 return !is64Bit;
3126 case CallingConv::Fast:
3127 return TailCallOpt;
3128 case CallingConv::GHC:
3129 return TailCallOpt;
Duncan Sandsdc7f1742012-11-16 12:36:39 +00003130 case CallingConv::HiPE:
3131 return TailCallOpt;
Evan Chengef41ff62011-06-23 17:54:54 +00003132 }
3133}
3134
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003135/// TranslateX86CC - do a one to one translation of a ISD::CondCode to the X86
3136/// specific condition code, returning the condition code and the LHS/RHS of the
3137/// comparison to make.
3138static unsigned TranslateX86CC(ISD::CondCode SetCCOpcode, bool isFP,
3139 SDValue &LHS, SDValue &RHS, SelectionDAG &DAG) {
Evan Chengd9558e02006-01-06 00:43:03 +00003140 if (!isFP) {
Chris Lattnerbfd68a72006-09-13 17:04:54 +00003141 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS)) {
3142 if (SetCCOpcode == ISD::SETGT && RHSC->isAllOnesValue()) {
3143 // X > -1 -> X == 0, jump !sign.
3144 RHS = DAG.getConstant(0, RHS.getValueType());
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003145 return X86::COND_NS;
Craig Topper69947b92012-04-23 06:57:04 +00003146 }
3147 if (SetCCOpcode == ISD::SETLT && RHSC->isNullValue()) {
Chris Lattnerbfd68a72006-09-13 17:04:54 +00003148 // X < 0 -> X == 0, jump on sign.
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003149 return X86::COND_S;
Craig Topper69947b92012-04-23 06:57:04 +00003150 }
3151 if (SetCCOpcode == ISD::SETLT && RHSC->getZExtValue() == 1) {
Dan Gohman5f6913c2007-09-17 14:49:27 +00003152 // X < 1 -> X <= 0
3153 RHS = DAG.getConstant(0, RHS.getValueType());
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003154 return X86::COND_LE;
Chris Lattnerbfd68a72006-09-13 17:04:54 +00003155 }
Chris Lattnerf9570512006-09-13 03:22:10 +00003156 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00003157
Evan Chengd9558e02006-01-06 00:43:03 +00003158 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00003159 default: llvm_unreachable("Invalid integer condition!");
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003160 case ISD::SETEQ: return X86::COND_E;
3161 case ISD::SETGT: return X86::COND_G;
3162 case ISD::SETGE: return X86::COND_GE;
3163 case ISD::SETLT: return X86::COND_L;
3164 case ISD::SETLE: return X86::COND_LE;
3165 case ISD::SETNE: return X86::COND_NE;
3166 case ISD::SETULT: return X86::COND_B;
3167 case ISD::SETUGT: return X86::COND_A;
3168 case ISD::SETULE: return X86::COND_BE;
3169 case ISD::SETUGE: return X86::COND_AE;
Evan Chengd9558e02006-01-06 00:43:03 +00003170 }
Chris Lattner4c78e022008-12-23 23:42:27 +00003171 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003172
Chris Lattner4c78e022008-12-23 23:42:27 +00003173 // First determine if it is required or is profitable to flip the operands.
Duncan Sands4047f4a2008-10-24 13:03:10 +00003174
Chris Lattner4c78e022008-12-23 23:42:27 +00003175 // If LHS is a foldable load, but RHS is not, flip the condition.
Rafael Espindolaf297c932011-02-03 03:58:05 +00003176 if (ISD::isNON_EXTLoad(LHS.getNode()) &&
3177 !ISD::isNON_EXTLoad(RHS.getNode())) {
Chris Lattner4c78e022008-12-23 23:42:27 +00003178 SetCCOpcode = getSetCCSwappedOperands(SetCCOpcode);
3179 std::swap(LHS, RHS);
Evan Cheng4d46d0a2008-08-28 23:48:31 +00003180 }
3181
Chris Lattner4c78e022008-12-23 23:42:27 +00003182 switch (SetCCOpcode) {
3183 default: break;
3184 case ISD::SETOLT:
3185 case ISD::SETOLE:
3186 case ISD::SETUGT:
3187 case ISD::SETUGE:
3188 std::swap(LHS, RHS);
3189 break;
3190 }
3191
3192 // On a floating point condition, the flags are set as follows:
3193 // ZF PF CF op
3194 // 0 | 0 | 0 | X > Y
3195 // 0 | 0 | 1 | X < Y
3196 // 1 | 0 | 0 | X == Y
3197 // 1 | 1 | 1 | unordered
3198 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00003199 default: llvm_unreachable("Condcode should be pre-legalized away");
Chris Lattner4c78e022008-12-23 23:42:27 +00003200 case ISD::SETUEQ:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003201 case ISD::SETEQ: return X86::COND_E;
Chris Lattner4c78e022008-12-23 23:42:27 +00003202 case ISD::SETOLT: // flipped
3203 case ISD::SETOGT:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003204 case ISD::SETGT: return X86::COND_A;
Chris Lattner4c78e022008-12-23 23:42:27 +00003205 case ISD::SETOLE: // flipped
3206 case ISD::SETOGE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003207 case ISD::SETGE: return X86::COND_AE;
Chris Lattner4c78e022008-12-23 23:42:27 +00003208 case ISD::SETUGT: // flipped
3209 case ISD::SETULT:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003210 case ISD::SETLT: return X86::COND_B;
Chris Lattner4c78e022008-12-23 23:42:27 +00003211 case ISD::SETUGE: // flipped
3212 case ISD::SETULE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003213 case ISD::SETLE: return X86::COND_BE;
Chris Lattner4c78e022008-12-23 23:42:27 +00003214 case ISD::SETONE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003215 case ISD::SETNE: return X86::COND_NE;
3216 case ISD::SETUO: return X86::COND_P;
3217 case ISD::SETO: return X86::COND_NP;
Dan Gohman1a492952009-10-20 16:22:37 +00003218 case ISD::SETOEQ:
3219 case ISD::SETUNE: return X86::COND_INVALID;
Chris Lattner4c78e022008-12-23 23:42:27 +00003220 }
Evan Chengd9558e02006-01-06 00:43:03 +00003221}
3222
Evan Cheng4a460802006-01-11 00:33:36 +00003223/// hasFPCMov - is there a floating point cmov for the specific X86 condition
3224/// code. Current x86 isa includes the following FP cmov instructions:
Evan Chengaaca22c2006-01-10 20:26:56 +00003225/// fcmovb, fcomvbe, fcomve, fcmovu, fcmovae, fcmova, fcmovne, fcmovnu.
Evan Cheng4a460802006-01-11 00:33:36 +00003226static bool hasFPCMov(unsigned X86CC) {
Evan Chengaaca22c2006-01-10 20:26:56 +00003227 switch (X86CC) {
3228 default:
3229 return false;
Chris Lattner7fbe9722006-10-20 17:42:20 +00003230 case X86::COND_B:
3231 case X86::COND_BE:
3232 case X86::COND_E:
3233 case X86::COND_P:
3234 case X86::COND_A:
3235 case X86::COND_AE:
3236 case X86::COND_NE:
3237 case X86::COND_NP:
Evan Chengaaca22c2006-01-10 20:26:56 +00003238 return true;
3239 }
3240}
3241
Evan Chengeb2f9692009-10-27 19:56:55 +00003242/// isFPImmLegal - Returns true if the target can instruction select the
3243/// specified FP immediate natively. If false, the legalizer will
3244/// materialize the FP immediate as a load from a constant pool.
Evan Chenga1eaa3c2009-10-28 01:43:28 +00003245bool X86TargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
Evan Chengeb2f9692009-10-27 19:56:55 +00003246 for (unsigned i = 0, e = LegalFPImmediates.size(); i != e; ++i) {
3247 if (Imm.bitwiseIsEqual(LegalFPImmediates[i]))
3248 return true;
3249 }
3250 return false;
3251}
3252
Nate Begeman9008ca62009-04-27 18:41:29 +00003253/// isUndefOrInRange - Return true if Val is undef or if its value falls within
3254/// the specified range (L, H].
3255static bool isUndefOrInRange(int Val, int Low, int Hi) {
3256 return (Val < 0) || (Val >= Low && Val < Hi);
3257}
3258
3259/// isUndefOrEqual - Val is either less than zero (undef) or equal to the
3260/// specified value.
3261static bool isUndefOrEqual(int Val, int CmpVal) {
Jakub Staszakb2af3a02012-12-06 18:22:59 +00003262 return (Val < 0 || Val == CmpVal);
Evan Chengc5cdff22006-04-07 21:53:05 +00003263}
3264
Benjamin Kramerd9b0b022012-06-02 10:20:22 +00003265/// isSequentialOrUndefInRange - Return true if every element in Mask, beginning
Bruno Cardoso Lopes4002d7e2011-08-12 21:54:42 +00003266/// from position Pos and ending in Pos+Size, falls within the specified
3267/// sequential range (L, L+Pos]. or is undef.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003268static bool isSequentialOrUndefInRange(ArrayRef<int> Mask,
Craig Topperb6072642012-05-03 07:26:59 +00003269 unsigned Pos, unsigned Size, int Low) {
3270 for (unsigned i = Pos, e = Pos+Size; i != e; ++i, ++Low)
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003271 if (!isUndefOrEqual(Mask[i], Low))
3272 return false;
3273 return true;
3274}
3275
Nate Begeman9008ca62009-04-27 18:41:29 +00003276/// isPSHUFDMask - Return true if the node specifies a shuffle of elements that
3277/// is suitable for input to PSHUFD or PSHUFW. That is, it doesn't reference
3278/// the second operand.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003279static bool isPSHUFDMask(ArrayRef<int> Mask, EVT VT) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00003280 if (VT == MVT::v4f32 || VT == MVT::v4i32 )
Nate Begeman9008ca62009-04-27 18:41:29 +00003281 return (Mask[0] < 4 && Mask[1] < 4 && Mask[2] < 4 && Mask[3] < 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00003282 if (VT == MVT::v2f64 || VT == MVT::v2i64)
Nate Begeman9008ca62009-04-27 18:41:29 +00003283 return (Mask[0] < 2 && Mask[1] < 2);
3284 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003285}
3286
Nate Begeman9008ca62009-04-27 18:41:29 +00003287/// isPSHUFHWMask - Return true if the node specifies a shuffle of elements that
3288/// is suitable for input to PSHUFHW.
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00003289static bool isPSHUFHWMask(ArrayRef<int> Mask, EVT VT, bool HasInt256) {
3290 if (VT != MVT::v8i16 && (!HasInt256 || VT != MVT::v16i16))
Evan Cheng0188ecb2006-03-22 18:59:22 +00003291 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003292
Nate Begeman9008ca62009-04-27 18:41:29 +00003293 // Lower quadword copied in order or undef.
Craig Topperc612d792012-01-02 09:17:37 +00003294 if (!isSequentialOrUndefInRange(Mask, 0, 4, 0))
3295 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003296
Evan Cheng506d3df2006-03-29 23:07:14 +00003297 // Upper quadword shuffled.
Craig Topperc612d792012-01-02 09:17:37 +00003298 for (unsigned i = 4; i != 8; ++i)
Craig Toppera9a568a2012-05-02 08:03:44 +00003299 if (!isUndefOrInRange(Mask[i], 4, 8))
Evan Cheng506d3df2006-03-29 23:07:14 +00003300 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003301
Craig Toppera9a568a2012-05-02 08:03:44 +00003302 if (VT == MVT::v16i16) {
3303 // Lower quadword copied in order or undef.
3304 if (!isSequentialOrUndefInRange(Mask, 8, 4, 8))
3305 return false;
3306
3307 // Upper quadword shuffled.
3308 for (unsigned i = 12; i != 16; ++i)
3309 if (!isUndefOrInRange(Mask[i], 12, 16))
3310 return false;
3311 }
3312
Evan Cheng506d3df2006-03-29 23:07:14 +00003313 return true;
3314}
3315
Nate Begeman9008ca62009-04-27 18:41:29 +00003316/// isPSHUFLWMask - Return true if the node specifies a shuffle of elements that
3317/// is suitable for input to PSHUFLW.
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00003318static bool isPSHUFLWMask(ArrayRef<int> Mask, EVT VT, bool HasInt256) {
3319 if (VT != MVT::v8i16 && (!HasInt256 || VT != MVT::v16i16))
Evan Cheng506d3df2006-03-29 23:07:14 +00003320 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003321
Rafael Espindola15684b22009-04-24 12:40:33 +00003322 // Upper quadword copied in order.
Craig Topperc612d792012-01-02 09:17:37 +00003323 if (!isSequentialOrUndefInRange(Mask, 4, 4, 4))
3324 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003325
Rafael Espindola15684b22009-04-24 12:40:33 +00003326 // Lower quadword shuffled.
Craig Topperc612d792012-01-02 09:17:37 +00003327 for (unsigned i = 0; i != 4; ++i)
Craig Toppera9a568a2012-05-02 08:03:44 +00003328 if (!isUndefOrInRange(Mask[i], 0, 4))
Rafael Espindola15684b22009-04-24 12:40:33 +00003329 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003330
Craig Toppera9a568a2012-05-02 08:03:44 +00003331 if (VT == MVT::v16i16) {
3332 // Upper quadword copied in order.
3333 if (!isSequentialOrUndefInRange(Mask, 12, 4, 12))
3334 return false;
3335
3336 // Lower quadword shuffled.
3337 for (unsigned i = 8; i != 12; ++i)
3338 if (!isUndefOrInRange(Mask[i], 8, 12))
3339 return false;
3340 }
3341
Rafael Espindola15684b22009-04-24 12:40:33 +00003342 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00003343}
3344
Nate Begemana09008b2009-10-19 02:17:23 +00003345/// isPALIGNRMask - Return true if the node specifies a shuffle of elements that
3346/// is suitable for input to PALIGNR.
Craig Topper0e2037b2012-01-20 05:53:00 +00003347static bool isPALIGNRMask(ArrayRef<int> Mask, EVT VT,
3348 const X86Subtarget *Subtarget) {
3349 if ((VT.getSizeInBits() == 128 && !Subtarget->hasSSSE3()) ||
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00003350 (VT.getSizeInBits() == 256 && !Subtarget->hasInt256()))
Bruno Cardoso Lopes9065d4b2011-07-29 01:30:59 +00003351 return false;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003352
Craig Topper0e2037b2012-01-20 05:53:00 +00003353 unsigned NumElts = VT.getVectorNumElements();
3354 unsigned NumLanes = VT.getSizeInBits()/128;
3355 unsigned NumLaneElts = NumElts/NumLanes;
3356
3357 // Do not handle 64-bit element shuffles with palignr.
3358 if (NumLaneElts == 2)
Nate Begemana09008b2009-10-19 02:17:23 +00003359 return false;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003360
Craig Topper0e2037b2012-01-20 05:53:00 +00003361 for (unsigned l = 0; l != NumElts; l+=NumLaneElts) {
3362 unsigned i;
3363 for (i = 0; i != NumLaneElts; ++i) {
3364 if (Mask[i+l] >= 0)
3365 break;
3366 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00003367
Craig Topper0e2037b2012-01-20 05:53:00 +00003368 // Lane is all undef, go to next lane
3369 if (i == NumLaneElts)
3370 continue;
Nate Begemana09008b2009-10-19 02:17:23 +00003371
Craig Topper0e2037b2012-01-20 05:53:00 +00003372 int Start = Mask[i+l];
Nate Begemana09008b2009-10-19 02:17:23 +00003373
Craig Topper0e2037b2012-01-20 05:53:00 +00003374 // Make sure its in this lane in one of the sources
3375 if (!isUndefOrInRange(Start, l, l+NumLaneElts) &&
3376 !isUndefOrInRange(Start, l+NumElts, l+NumElts+NumLaneElts))
Nate Begemana09008b2009-10-19 02:17:23 +00003377 return false;
Craig Topper0e2037b2012-01-20 05:53:00 +00003378
3379 // If not lane 0, then we must match lane 0
3380 if (l != 0 && Mask[i] >= 0 && !isUndefOrEqual(Start, Mask[i]+l))
3381 return false;
3382
3383 // Correct second source to be contiguous with first source
3384 if (Start >= (int)NumElts)
3385 Start -= NumElts - NumLaneElts;
3386
3387 // Make sure we're shifting in the right direction.
3388 if (Start <= (int)(i+l))
3389 return false;
3390
3391 Start -= i;
3392
3393 // Check the rest of the elements to see if they are consecutive.
3394 for (++i; i != NumLaneElts; ++i) {
3395 int Idx = Mask[i+l];
3396
3397 // Make sure its in this lane
3398 if (!isUndefOrInRange(Idx, l, l+NumLaneElts) &&
3399 !isUndefOrInRange(Idx, l+NumElts, l+NumElts+NumLaneElts))
3400 return false;
3401
3402 // If not lane 0, then we must match lane 0
3403 if (l != 0 && Mask[i] >= 0 && !isUndefOrEqual(Idx, Mask[i]+l))
3404 return false;
3405
3406 if (Idx >= (int)NumElts)
3407 Idx -= NumElts - NumLaneElts;
3408
3409 if (!isUndefOrEqual(Idx, Start+i))
3410 return false;
3411
3412 }
Nate Begemana09008b2009-10-19 02:17:23 +00003413 }
Craig Topper0e2037b2012-01-20 05:53:00 +00003414
Nate Begemana09008b2009-10-19 02:17:23 +00003415 return true;
3416}
3417
Craig Topper1a7700a2012-01-19 08:19:12 +00003418/// CommuteVectorShuffleMask - Change values in a shuffle permute mask assuming
3419/// the two vector operands have swapped position.
3420static void CommuteVectorShuffleMask(SmallVectorImpl<int> &Mask,
3421 unsigned NumElems) {
3422 for (unsigned i = 0; i != NumElems; ++i) {
3423 int idx = Mask[i];
3424 if (idx < 0)
3425 continue;
3426 else if (idx < (int)NumElems)
3427 Mask[i] = idx + NumElems;
3428 else
3429 Mask[i] = idx - NumElems;
3430 }
3431}
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00003432
Craig Topper1a7700a2012-01-19 08:19:12 +00003433/// isSHUFPMask - Return true if the specified VECTOR_SHUFFLE operand
3434/// specifies a shuffle of elements that is suitable for input to 128/256-bit
3435/// SHUFPS and SHUFPD. If Commuted is true, then it checks for sources to be
3436/// reverse of what x86 shuffles want.
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00003437static bool isSHUFPMask(ArrayRef<int> Mask, EVT VT, bool HasFp256,
Craig Topper1a7700a2012-01-19 08:19:12 +00003438 bool Commuted = false) {
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00003439 if (!HasFp256 && VT.getSizeInBits() == 256)
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00003440 return false;
3441
Craig Topper1a7700a2012-01-19 08:19:12 +00003442 unsigned NumElems = VT.getVectorNumElements();
3443 unsigned NumLanes = VT.getSizeInBits()/128;
3444 unsigned NumLaneElems = NumElems/NumLanes;
3445
3446 if (NumLaneElems != 2 && NumLaneElems != 4)
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00003447 return false;
3448
3449 // VSHUFPSY divides the resulting vector into 4 chunks.
3450 // The sources are also splitted into 4 chunks, and each destination
3451 // chunk must come from a different source chunk.
3452 //
3453 // SRC1 => X7 X6 X5 X4 X3 X2 X1 X0
3454 // SRC2 => Y7 Y6 Y5 Y4 Y3 Y2 Y1 Y9
3455 //
3456 // DST => Y7..Y4, Y7..Y4, X7..X4, X7..X4,
3457 // Y3..Y0, Y3..Y0, X3..X0, X3..X0
3458 //
Craig Topper9d7025b2011-11-27 21:41:12 +00003459 // VSHUFPDY divides the resulting vector into 4 chunks.
3460 // The sources are also splitted into 4 chunks, and each destination
3461 // chunk must come from a different source chunk.
3462 //
3463 // SRC1 => X3 X2 X1 X0
3464 // SRC2 => Y3 Y2 Y1 Y0
3465 //
3466 // DST => Y3..Y2, X3..X2, Y1..Y0, X1..X0
3467 //
Craig Topper1a7700a2012-01-19 08:19:12 +00003468 unsigned HalfLaneElems = NumLaneElems/2;
3469 for (unsigned l = 0; l != NumElems; l += NumLaneElems) {
3470 for (unsigned i = 0; i != NumLaneElems; ++i) {
3471 int Idx = Mask[i+l];
3472 unsigned RngStart = l + ((Commuted == (i<HalfLaneElems)) ? NumElems : 0);
3473 if (!isUndefOrInRange(Idx, RngStart, RngStart+NumLaneElems))
3474 return false;
3475 // For VSHUFPSY, the mask of the second half must be the same as the
3476 // first but with the appropriate offsets. This works in the same way as
3477 // VPERMILPS works with masks.
3478 if (NumElems != 8 || l == 0 || Mask[i] < 0)
3479 continue;
3480 if (!isUndefOrEqual(Idx, Mask[i]+l))
3481 return false;
Craig Topper1ff73d72011-12-06 04:59:07 +00003482 }
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00003483 }
3484
3485 return true;
3486}
3487
Evan Cheng2c0dbd02006-03-24 02:58:06 +00003488/// isMOVHLPSMask - Return true if the specified VECTOR_SHUFFLE operand
3489/// specifies a shuffle of elements that is suitable for input to MOVHLPS.
Craig Topperdd637ae2012-02-19 05:41:45 +00003490static bool isMOVHLPSMask(ArrayRef<int> Mask, EVT VT) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00003491 if (!VT.is128BitVector())
Bruno Cardoso Lopes61260052011-07-29 02:05:28 +00003492 return false;
3493
Craig Topper7a9a28b2012-08-12 02:23:29 +00003494 unsigned NumElems = VT.getVectorNumElements();
3495
Bruno Cardoso Lopes61260052011-07-29 02:05:28 +00003496 if (NumElems != 4)
Evan Cheng2c0dbd02006-03-24 02:58:06 +00003497 return false;
3498
Evan Cheng2064a2b2006-03-28 06:50:32 +00003499 // Expect bit0 == 6, bit1 == 7, bit2 == 2, bit3 == 3
Craig Topperdd637ae2012-02-19 05:41:45 +00003500 return isUndefOrEqual(Mask[0], 6) &&
3501 isUndefOrEqual(Mask[1], 7) &&
3502 isUndefOrEqual(Mask[2], 2) &&
3503 isUndefOrEqual(Mask[3], 3);
Evan Cheng6e56e2c2006-11-07 22:14:24 +00003504}
3505
Nate Begeman0b10b912009-11-07 23:17:15 +00003506/// isMOVHLPS_v_undef_Mask - Special case of isMOVHLPSMask for canonical form
3507/// of vector_shuffle v, v, <2, 3, 2, 3>, i.e. vector_shuffle v, undef,
3508/// <2, 3, 2, 3>
Craig Topperdd637ae2012-02-19 05:41:45 +00003509static bool isMOVHLPS_v_undef_Mask(ArrayRef<int> Mask, EVT VT) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00003510 if (!VT.is128BitVector())
Bruno Cardoso Lopes61260052011-07-29 02:05:28 +00003511 return false;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003512
Craig Topper7a9a28b2012-08-12 02:23:29 +00003513 unsigned NumElems = VT.getVectorNumElements();
3514
Nate Begeman0b10b912009-11-07 23:17:15 +00003515 if (NumElems != 4)
3516 return false;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003517
Craig Topperdd637ae2012-02-19 05:41:45 +00003518 return isUndefOrEqual(Mask[0], 2) &&
3519 isUndefOrEqual(Mask[1], 3) &&
3520 isUndefOrEqual(Mask[2], 2) &&
3521 isUndefOrEqual(Mask[3], 3);
Nate Begeman0b10b912009-11-07 23:17:15 +00003522}
3523
Evan Cheng5ced1d82006-04-06 23:23:56 +00003524/// isMOVLPMask - Return true if the specified VECTOR_SHUFFLE operand
3525/// specifies a shuffle of elements that is suitable for input to MOVLP{S|D}.
Craig Topperdd637ae2012-02-19 05:41:45 +00003526static bool isMOVLPMask(ArrayRef<int> Mask, EVT VT) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00003527 if (!VT.is128BitVector())
Elena Demikhovsky021c0a22011-12-28 08:14:01 +00003528 return false;
3529
Craig Topperdd637ae2012-02-19 05:41:45 +00003530 unsigned NumElems = VT.getVectorNumElements();
Evan Cheng5ced1d82006-04-06 23:23:56 +00003531
Evan Cheng5ced1d82006-04-06 23:23:56 +00003532 if (NumElems != 2 && NumElems != 4)
3533 return false;
3534
Chad Rosier238ae312012-04-30 17:47:15 +00003535 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
Craig Topperdd637ae2012-02-19 05:41:45 +00003536 if (!isUndefOrEqual(Mask[i], i + NumElems))
Evan Chengc5cdff22006-04-07 21:53:05 +00003537 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003538
Chad Rosier238ae312012-04-30 17:47:15 +00003539 for (unsigned i = NumElems/2, e = NumElems; i != e; ++i)
Craig Topperdd637ae2012-02-19 05:41:45 +00003540 if (!isUndefOrEqual(Mask[i], i))
Evan Chengc5cdff22006-04-07 21:53:05 +00003541 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003542
3543 return true;
3544}
3545
Nate Begeman0b10b912009-11-07 23:17:15 +00003546/// isMOVLHPSMask - Return true if the specified VECTOR_SHUFFLE operand
3547/// specifies a shuffle of elements that is suitable for input to MOVLHPS.
Craig Topperdd637ae2012-02-19 05:41:45 +00003548static bool isMOVLHPSMask(ArrayRef<int> Mask, EVT VT) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00003549 if (!VT.is128BitVector())
3550 return false;
3551
Craig Topperdd637ae2012-02-19 05:41:45 +00003552 unsigned NumElems = VT.getVectorNumElements();
Evan Cheng5ced1d82006-04-06 23:23:56 +00003553
Craig Topper7a9a28b2012-08-12 02:23:29 +00003554 if (NumElems != 2 && NumElems != 4)
Evan Cheng5ced1d82006-04-06 23:23:56 +00003555 return false;
3556
Chad Rosier238ae312012-04-30 17:47:15 +00003557 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
Craig Topperdd637ae2012-02-19 05:41:45 +00003558 if (!isUndefOrEqual(Mask[i], i))
Evan Chengc5cdff22006-04-07 21:53:05 +00003559 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003560
Chad Rosier238ae312012-04-30 17:47:15 +00003561 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
3562 if (!isUndefOrEqual(Mask[i + e], i + NumElems))
Evan Chengc5cdff22006-04-07 21:53:05 +00003563 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003564
3565 return true;
3566}
3567
Elena Demikhovsky15963732012-06-26 08:04:10 +00003568//
3569// Some special combinations that can be optimized.
3570//
3571static
3572SDValue Compact8x32ShuffleNode(ShuffleVectorSDNode *SVOp,
3573 SelectionDAG &DAG) {
3574 EVT VT = SVOp->getValueType(0);
Elena Demikhovsky15963732012-06-26 08:04:10 +00003575 DebugLoc dl = SVOp->getDebugLoc();
3576
3577 if (VT != MVT::v8i32 && VT != MVT::v8f32)
3578 return SDValue();
3579
3580 ArrayRef<int> Mask = SVOp->getMask();
3581
3582 // These are the special masks that may be optimized.
3583 static const int MaskToOptimizeEven[] = {0, 8, 2, 10, 4, 12, 6, 14};
3584 static const int MaskToOptimizeOdd[] = {1, 9, 3, 11, 5, 13, 7, 15};
3585 bool MatchEvenMask = true;
3586 bool MatchOddMask = true;
3587 for (int i=0; i<8; ++i) {
3588 if (!isUndefOrEqual(Mask[i], MaskToOptimizeEven[i]))
3589 MatchEvenMask = false;
3590 if (!isUndefOrEqual(Mask[i], MaskToOptimizeOdd[i]))
3591 MatchOddMask = false;
3592 }
Elena Demikhovsky15963732012-06-26 08:04:10 +00003593
Elena Demikhovsky32510202012-09-04 12:49:02 +00003594 if (!MatchEvenMask && !MatchOddMask)
Elena Demikhovsky15963732012-06-26 08:04:10 +00003595 return SDValue();
Michael Liao471b9172012-10-03 23:43:52 +00003596
Elena Demikhovsky15963732012-06-26 08:04:10 +00003597 SDValue UndefNode = DAG.getNode(ISD::UNDEF, dl, VT);
3598
Elena Demikhovsky32510202012-09-04 12:49:02 +00003599 SDValue Op0 = SVOp->getOperand(0);
3600 SDValue Op1 = SVOp->getOperand(1);
3601
3602 if (MatchEvenMask) {
3603 // Shift the second operand right to 32 bits.
3604 static const int ShiftRightMask[] = {-1, 0, -1, 2, -1, 4, -1, 6 };
3605 Op1 = DAG.getVectorShuffle(VT, dl, Op1, UndefNode, ShiftRightMask);
3606 } else {
3607 // Shift the first operand left to 32 bits.
3608 static const int ShiftLeftMask[] = {1, -1, 3, -1, 5, -1, 7, -1 };
3609 Op0 = DAG.getVectorShuffle(VT, dl, Op0, UndefNode, ShiftLeftMask);
3610 }
3611 static const int BlendMask[] = {0, 9, 2, 11, 4, 13, 6, 15};
3612 return DAG.getVectorShuffle(VT, dl, Op0, Op1, BlendMask);
Elena Demikhovsky15963732012-06-26 08:04:10 +00003613}
3614
Evan Cheng0038e592006-03-28 00:39:58 +00003615/// isUNPCKLMask - Return true if the specified VECTOR_SHUFFLE operand
3616/// specifies a shuffle of elements that is suitable for input to UNPCKL.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003617static bool isUNPCKLMask(ArrayRef<int> Mask, EVT VT,
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00003618 bool HasInt256, bool V2IsSplat = false) {
Craig Topper94438ba2011-12-16 08:06:31 +00003619 unsigned NumElts = VT.getVectorNumElements();
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003620
3621 assert((VT.is128BitVector() || VT.is256BitVector()) &&
3622 "Unsupported vector type for unpckh");
3623
Craig Topper6347e862011-11-21 06:57:39 +00003624 if (VT.getSizeInBits() == 256 && NumElts != 4 && NumElts != 8 &&
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00003625 (!HasInt256 || (NumElts != 16 && NumElts != 32)))
Evan Cheng0038e592006-03-28 00:39:58 +00003626 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003627
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003628 // Handle 128 and 256-bit vector lengths. AVX defines UNPCK* to operate
3629 // independently on 128-bit lanes.
3630 unsigned NumLanes = VT.getSizeInBits()/128;
3631 unsigned NumLaneElts = NumElts/NumLanes;
David Greenea20244d2011-03-02 17:23:43 +00003632
Craig Topper94438ba2011-12-16 08:06:31 +00003633 for (unsigned l = 0; l != NumLanes; ++l) {
3634 for (unsigned i = l*NumLaneElts, j = l*NumLaneElts;
3635 i != (l+1)*NumLaneElts;
David Greenea20244d2011-03-02 17:23:43 +00003636 i += 2, ++j) {
3637 int BitI = Mask[i];
3638 int BitI1 = Mask[i+1];
3639 if (!isUndefOrEqual(BitI, j))
Evan Cheng39623da2006-04-20 08:58:49 +00003640 return false;
David Greenea20244d2011-03-02 17:23:43 +00003641 if (V2IsSplat) {
3642 if (!isUndefOrEqual(BitI1, NumElts))
3643 return false;
3644 } else {
3645 if (!isUndefOrEqual(BitI1, j + NumElts))
3646 return false;
3647 }
Evan Cheng39623da2006-04-20 08:58:49 +00003648 }
Evan Cheng0038e592006-03-28 00:39:58 +00003649 }
David Greenea20244d2011-03-02 17:23:43 +00003650
Evan Cheng0038e592006-03-28 00:39:58 +00003651 return true;
3652}
3653
Evan Cheng4fcb9222006-03-28 02:43:26 +00003654/// isUNPCKHMask - Return true if the specified VECTOR_SHUFFLE operand
3655/// specifies a shuffle of elements that is suitable for input to UNPCKH.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003656static bool isUNPCKHMask(ArrayRef<int> Mask, EVT VT,
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00003657 bool HasInt256, bool V2IsSplat = false) {
Craig Topper94438ba2011-12-16 08:06:31 +00003658 unsigned NumElts = VT.getVectorNumElements();
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003659
3660 assert((VT.is128BitVector() || VT.is256BitVector()) &&
3661 "Unsupported vector type for unpckh");
3662
Craig Topper6347e862011-11-21 06:57:39 +00003663 if (VT.getSizeInBits() == 256 && NumElts != 4 && NumElts != 8 &&
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00003664 (!HasInt256 || (NumElts != 16 && NumElts != 32)))
Evan Cheng4fcb9222006-03-28 02:43:26 +00003665 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003666
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003667 // Handle 128 and 256-bit vector lengths. AVX defines UNPCK* to operate
3668 // independently on 128-bit lanes.
3669 unsigned NumLanes = VT.getSizeInBits()/128;
3670 unsigned NumLaneElts = NumElts/NumLanes;
3671
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003672 for (unsigned l = 0; l != NumLanes; ++l) {
Craig Topper94438ba2011-12-16 08:06:31 +00003673 for (unsigned i = l*NumLaneElts, j = (l*NumLaneElts)+NumLaneElts/2;
3674 i != (l+1)*NumLaneElts; i += 2, ++j) {
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003675 int BitI = Mask[i];
3676 int BitI1 = Mask[i+1];
3677 if (!isUndefOrEqual(BitI, j))
Evan Cheng39623da2006-04-20 08:58:49 +00003678 return false;
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003679 if (V2IsSplat) {
3680 if (isUndefOrEqual(BitI1, NumElts))
3681 return false;
3682 } else {
3683 if (!isUndefOrEqual(BitI1, j+NumElts))
3684 return false;
3685 }
Evan Cheng39623da2006-04-20 08:58:49 +00003686 }
Evan Cheng4fcb9222006-03-28 02:43:26 +00003687 }
Evan Cheng4fcb9222006-03-28 02:43:26 +00003688 return true;
3689}
3690
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003691/// isUNPCKL_v_undef_Mask - Special case of isUNPCKLMask for canonical form
3692/// of vector_shuffle v, v, <0, 4, 1, 5>, i.e. vector_shuffle v, undef,
3693/// <0, 0, 1, 1>
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003694static bool isUNPCKL_v_undef_Mask(ArrayRef<int> Mask, EVT VT,
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00003695 bool HasInt256) {
Craig Topper94438ba2011-12-16 08:06:31 +00003696 unsigned NumElts = VT.getVectorNumElements();
3697
3698 assert((VT.is128BitVector() || VT.is256BitVector()) &&
3699 "Unsupported vector type for unpckh");
3700
3701 if (VT.getSizeInBits() == 256 && NumElts != 4 && NumElts != 8 &&
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00003702 (!HasInt256 || (NumElts != 16 && NumElts != 32)))
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003703 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003704
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00003705 // For 256-bit i64/f64, use MOVDDUPY instead, so reject the matching pattern
3706 // FIXME: Need a better way to get rid of this, there's no latency difference
3707 // between UNPCKLPD and MOVDDUP, the later should always be checked first and
3708 // the former later. We should also remove the "_undef" special mask.
Craig Topper94438ba2011-12-16 08:06:31 +00003709 if (NumElts == 4 && VT.getSizeInBits() == 256)
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00003710 return false;
3711
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003712 // Handle 128 and 256-bit vector lengths. AVX defines UNPCK* to operate
3713 // independently on 128-bit lanes.
Craig Topper94438ba2011-12-16 08:06:31 +00003714 unsigned NumLanes = VT.getSizeInBits()/128;
3715 unsigned NumLaneElts = NumElts/NumLanes;
David Greenea20244d2011-03-02 17:23:43 +00003716
Craig Topper94438ba2011-12-16 08:06:31 +00003717 for (unsigned l = 0; l != NumLanes; ++l) {
3718 for (unsigned i = l*NumLaneElts, j = l*NumLaneElts;
3719 i != (l+1)*NumLaneElts;
David Greenea20244d2011-03-02 17:23:43 +00003720 i += 2, ++j) {
3721 int BitI = Mask[i];
3722 int BitI1 = Mask[i+1];
3723
3724 if (!isUndefOrEqual(BitI, j))
3725 return false;
3726 if (!isUndefOrEqual(BitI1, j))
3727 return false;
3728 }
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003729 }
David Greenea20244d2011-03-02 17:23:43 +00003730
Rafael Espindola15684b22009-04-24 12:40:33 +00003731 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00003732}
3733
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003734/// isUNPCKH_v_undef_Mask - Special case of isUNPCKHMask for canonical form
3735/// of vector_shuffle v, v, <2, 6, 3, 7>, i.e. vector_shuffle v, undef,
3736/// <2, 2, 3, 3>
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00003737static bool isUNPCKH_v_undef_Mask(ArrayRef<int> Mask, EVT VT, bool HasInt256) {
Craig Topper94438ba2011-12-16 08:06:31 +00003738 unsigned NumElts = VT.getVectorNumElements();
3739
3740 assert((VT.is128BitVector() || VT.is256BitVector()) &&
3741 "Unsupported vector type for unpckh");
3742
3743 if (VT.getSizeInBits() == 256 && NumElts != 4 && NumElts != 8 &&
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00003744 (!HasInt256 || (NumElts != 16 && NumElts != 32)))
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003745 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003746
Craig Topper94438ba2011-12-16 08:06:31 +00003747 // Handle 128 and 256-bit vector lengths. AVX defines UNPCK* to operate
3748 // independently on 128-bit lanes.
3749 unsigned NumLanes = VT.getSizeInBits()/128;
3750 unsigned NumLaneElts = NumElts/NumLanes;
3751
3752 for (unsigned l = 0; l != NumLanes; ++l) {
3753 for (unsigned i = l*NumLaneElts, j = (l*NumLaneElts)+NumLaneElts/2;
3754 i != (l+1)*NumLaneElts; i += 2, ++j) {
3755 int BitI = Mask[i];
3756 int BitI1 = Mask[i+1];
3757 if (!isUndefOrEqual(BitI, j))
3758 return false;
3759 if (!isUndefOrEqual(BitI1, j))
3760 return false;
3761 }
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003762 }
Rafael Espindola15684b22009-04-24 12:40:33 +00003763 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00003764}
3765
Evan Cheng017dcc62006-04-21 01:05:10 +00003766/// isMOVLMask - Return true if the specified VECTOR_SHUFFLE operand
3767/// specifies a shuffle of elements that is suitable for input to MOVSS,
3768/// MOVSD, and MOVD, i.e. setting the lowest element.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003769static bool isMOVLMask(ArrayRef<int> Mask, EVT VT) {
Eli Friedman10415532009-06-06 06:05:10 +00003770 if (VT.getVectorElementType().getSizeInBits() < 32)
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003771 return false;
Craig Topper7a9a28b2012-08-12 02:23:29 +00003772 if (!VT.is128BitVector())
Elena Demikhovsky021c0a22011-12-28 08:14:01 +00003773 return false;
Eli Friedman10415532009-06-06 06:05:10 +00003774
Craig Topperc612d792012-01-02 09:17:37 +00003775 unsigned NumElts = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00003776
Nate Begeman9008ca62009-04-27 18:41:29 +00003777 if (!isUndefOrEqual(Mask[0], NumElts))
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003778 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003779
Craig Topperc612d792012-01-02 09:17:37 +00003780 for (unsigned i = 1; i != NumElts; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003781 if (!isUndefOrEqual(Mask[i], i))
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003782 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003783
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003784 return true;
3785}
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003786
Craig Topper70b883b2011-11-28 10:14:51 +00003787/// isVPERM2X128Mask - Match 256-bit shuffles where the elements are considered
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003788/// as permutations between 128-bit chunks or halves. As an example: this
3789/// shuffle bellow:
3790/// vector_shuffle <4, 5, 6, 7, 12, 13, 14, 15>
3791/// The first half comes from the second half of V1 and the second half from the
3792/// the second half of V2.
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00003793static bool isVPERM2X128Mask(ArrayRef<int> Mask, EVT VT, bool HasFp256) {
3794 if (!HasFp256 || !VT.is256BitVector())
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003795 return false;
3796
3797 // The shuffle result is divided into half A and half B. In total the two
3798 // sources have 4 halves, namely: C, D, E, F. The final values of A and
3799 // B must come from C, D, E or F.
Craig Topperc612d792012-01-02 09:17:37 +00003800 unsigned HalfSize = VT.getVectorNumElements()/2;
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003801 bool MatchA = false, MatchB = false;
3802
3803 // Check if A comes from one of C, D, E, F.
Craig Topperc612d792012-01-02 09:17:37 +00003804 for (unsigned Half = 0; Half != 4; ++Half) {
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003805 if (isSequentialOrUndefInRange(Mask, 0, HalfSize, Half*HalfSize)) {
3806 MatchA = true;
3807 break;
3808 }
3809 }
3810
3811 // Check if B comes from one of C, D, E, F.
Craig Topperc612d792012-01-02 09:17:37 +00003812 for (unsigned Half = 0; Half != 4; ++Half) {
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003813 if (isSequentialOrUndefInRange(Mask, HalfSize, HalfSize, Half*HalfSize)) {
3814 MatchB = true;
3815 break;
3816 }
3817 }
3818
3819 return MatchA && MatchB;
3820}
3821
Craig Topper70b883b2011-11-28 10:14:51 +00003822/// getShuffleVPERM2X128Immediate - Return the appropriate immediate to shuffle
3823/// the specified VECTOR_MASK mask with VPERM2F128/VPERM2I128 instructions.
Craig Topperd93e4c32011-12-11 19:12:35 +00003824static unsigned getShuffleVPERM2X128Immediate(ShuffleVectorSDNode *SVOp) {
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003825 EVT VT = SVOp->getValueType(0);
3826
Craig Topperc612d792012-01-02 09:17:37 +00003827 unsigned HalfSize = VT.getVectorNumElements()/2;
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003828
Craig Topperc612d792012-01-02 09:17:37 +00003829 unsigned FstHalf = 0, SndHalf = 0;
3830 for (unsigned i = 0; i < HalfSize; ++i) {
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003831 if (SVOp->getMaskElt(i) > 0) {
3832 FstHalf = SVOp->getMaskElt(i)/HalfSize;
3833 break;
3834 }
3835 }
Craig Topperc612d792012-01-02 09:17:37 +00003836 for (unsigned i = HalfSize; i < HalfSize*2; ++i) {
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003837 if (SVOp->getMaskElt(i) > 0) {
3838 SndHalf = SVOp->getMaskElt(i)/HalfSize;
3839 break;
3840 }
3841 }
3842
3843 return (FstHalf | (SndHalf << 4));
3844}
3845
Craig Topper70b883b2011-11-28 10:14:51 +00003846/// isVPERMILPMask - Return true if the specified VECTOR_SHUFFLE operand
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00003847/// specifies a shuffle of elements that is suitable for input to VPERMILPD*.
3848/// Note that VPERMIL mask matching is different depending whether theunderlying
3849/// type is 32 or 64. In the VPERMILPS the high half of the mask should point
3850/// to the same elements of the low, but to the higher half of the source.
3851/// In VPERMILPD the two lanes could be shuffled independently of each other
Craig Topperdbd98a42012-02-07 06:28:42 +00003852/// with the same restriction that lanes can't be crossed. Also handles PSHUFDY.
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00003853static bool isVPERMILPMask(ArrayRef<int> Mask, EVT VT, bool HasFp256) {
3854 if (!HasFp256)
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00003855 return false;
3856
Craig Topperc612d792012-01-02 09:17:37 +00003857 unsigned NumElts = VT.getVectorNumElements();
Craig Topper70b883b2011-11-28 10:14:51 +00003858 // Only match 256-bit with 32/64-bit types
3859 if (VT.getSizeInBits() != 256 || (NumElts != 4 && NumElts != 8))
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00003860 return false;
3861
Craig Topperc612d792012-01-02 09:17:37 +00003862 unsigned NumLanes = VT.getSizeInBits()/128;
3863 unsigned LaneSize = NumElts/NumLanes;
Craig Topper1a7700a2012-01-19 08:19:12 +00003864 for (unsigned l = 0; l != NumElts; l += LaneSize) {
Craig Topperc612d792012-01-02 09:17:37 +00003865 for (unsigned i = 0; i != LaneSize; ++i) {
Craig Topper1a7700a2012-01-19 08:19:12 +00003866 if (!isUndefOrInRange(Mask[i+l], l, l+LaneSize))
Craig Topper70b883b2011-11-28 10:14:51 +00003867 return false;
Craig Topper1a7700a2012-01-19 08:19:12 +00003868 if (NumElts != 8 || l == 0)
Craig Topper70b883b2011-11-28 10:14:51 +00003869 continue;
3870 // VPERMILPS handling
3871 if (Mask[i] < 0)
3872 continue;
Craig Topper1a7700a2012-01-19 08:19:12 +00003873 if (!isUndefOrEqual(Mask[i+l], Mask[i]+l))
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00003874 return false;
3875 }
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00003876 }
3877
3878 return true;
3879}
3880
Craig Topper5aaffa82012-02-19 02:53:47 +00003881/// isCommutedMOVLMask - Returns true if the shuffle mask is except the reverse
Evan Cheng017dcc62006-04-21 01:05:10 +00003882/// of what x86 movss want. X86 movs requires the lowest element to be lowest
Evan Cheng39623da2006-04-20 08:58:49 +00003883/// element of vector 2 and the other elements to come from vector 1 in order.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003884static bool isCommutedMOVLMask(ArrayRef<int> Mask, EVT VT,
Nate Begeman9008ca62009-04-27 18:41:29 +00003885 bool V2IsSplat = false, bool V2IsUndef = false) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00003886 if (!VT.is128BitVector())
Craig Topper97327dc2012-03-18 22:50:10 +00003887 return false;
Craig Topper7a9a28b2012-08-12 02:23:29 +00003888
3889 unsigned NumOps = VT.getVectorNumElements();
Chris Lattner5a88b832007-02-25 07:10:00 +00003890 if (NumOps != 2 && NumOps != 4 && NumOps != 8 && NumOps != 16)
Evan Cheng39623da2006-04-20 08:58:49 +00003891 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003892
Nate Begeman9008ca62009-04-27 18:41:29 +00003893 if (!isUndefOrEqual(Mask[0], 0))
Evan Cheng39623da2006-04-20 08:58:49 +00003894 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003895
Craig Topperc612d792012-01-02 09:17:37 +00003896 for (unsigned i = 1; i != NumOps; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003897 if (!(isUndefOrEqual(Mask[i], i+NumOps) ||
3898 (V2IsUndef && isUndefOrInRange(Mask[i], NumOps, NumOps*2)) ||
3899 (V2IsSplat && isUndefOrEqual(Mask[i], NumOps))))
Evan Cheng8cf723d2006-09-08 01:50:06 +00003900 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003901
Evan Cheng39623da2006-04-20 08:58:49 +00003902 return true;
3903}
3904
Evan Chengd9539472006-04-14 21:59:03 +00003905/// isMOVSHDUPMask - Return true if the specified VECTOR_SHUFFLE operand
3906/// specifies a shuffle of elements that is suitable for input to MOVSHDUP.
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003907/// Masks to match: <1, 1, 3, 3> or <1, 1, 3, 3, 5, 5, 7, 7>
Craig Topperdd637ae2012-02-19 05:41:45 +00003908static bool isMOVSHDUPMask(ArrayRef<int> Mask, EVT VT,
Craig Topper5aaffa82012-02-19 02:53:47 +00003909 const X86Subtarget *Subtarget) {
Craig Topperd0a31172012-01-10 06:37:29 +00003910 if (!Subtarget->hasSSE3())
Evan Chengd9539472006-04-14 21:59:03 +00003911 return false;
3912
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003913 unsigned NumElems = VT.getVectorNumElements();
3914
3915 if ((VT.getSizeInBits() == 128 && NumElems != 4) ||
3916 (VT.getSizeInBits() == 256 && NumElems != 8))
3917 return false;
3918
3919 // "i+1" is the value the indexed mask element must have
Craig Topperdd637ae2012-02-19 05:41:45 +00003920 for (unsigned i = 0; i != NumElems; i += 2)
3921 if (!isUndefOrEqual(Mask[i], i+1) ||
3922 !isUndefOrEqual(Mask[i+1], i+1))
Nate Begeman9008ca62009-04-27 18:41:29 +00003923 return false;
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003924
3925 return true;
Evan Chengd9539472006-04-14 21:59:03 +00003926}
3927
3928/// isMOVSLDUPMask - Return true if the specified VECTOR_SHUFFLE operand
3929/// specifies a shuffle of elements that is suitable for input to MOVSLDUP.
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003930/// Masks to match: <0, 0, 2, 2> or <0, 0, 2, 2, 4, 4, 6, 6>
Craig Topperdd637ae2012-02-19 05:41:45 +00003931static bool isMOVSLDUPMask(ArrayRef<int> Mask, EVT VT,
Craig Topper5aaffa82012-02-19 02:53:47 +00003932 const X86Subtarget *Subtarget) {
Craig Topperd0a31172012-01-10 06:37:29 +00003933 if (!Subtarget->hasSSE3())
Evan Chengd9539472006-04-14 21:59:03 +00003934 return false;
3935
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003936 unsigned NumElems = VT.getVectorNumElements();
3937
3938 if ((VT.getSizeInBits() == 128 && NumElems != 4) ||
3939 (VT.getSizeInBits() == 256 && NumElems != 8))
3940 return false;
3941
3942 // "i" is the value the indexed mask element must have
Craig Topperc612d792012-01-02 09:17:37 +00003943 for (unsigned i = 0; i != NumElems; i += 2)
Craig Topperdd637ae2012-02-19 05:41:45 +00003944 if (!isUndefOrEqual(Mask[i], i) ||
3945 !isUndefOrEqual(Mask[i+1], i))
Nate Begeman9008ca62009-04-27 18:41:29 +00003946 return false;
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003947
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003948 return true;
Evan Chengd9539472006-04-14 21:59:03 +00003949}
3950
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00003951/// isMOVDDUPYMask - Return true if the specified VECTOR_SHUFFLE operand
3952/// specifies a shuffle of elements that is suitable for input to 256-bit
3953/// version of MOVDDUP.
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00003954static bool isMOVDDUPYMask(ArrayRef<int> Mask, EVT VT, bool HasFp256) {
3955 if (!HasFp256 || !VT.is256BitVector())
Craig Topper7a9a28b2012-08-12 02:23:29 +00003956 return false;
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00003957
Craig Topper7a9a28b2012-08-12 02:23:29 +00003958 unsigned NumElts = VT.getVectorNumElements();
3959 if (NumElts != 4)
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00003960 return false;
3961
Craig Topperc612d792012-01-02 09:17:37 +00003962 for (unsigned i = 0; i != NumElts/2; ++i)
Craig Topperbeabc6c2011-12-05 06:56:46 +00003963 if (!isUndefOrEqual(Mask[i], 0))
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00003964 return false;
Craig Topperc612d792012-01-02 09:17:37 +00003965 for (unsigned i = NumElts/2; i != NumElts; ++i)
Craig Topperbeabc6c2011-12-05 06:56:46 +00003966 if (!isUndefOrEqual(Mask[i], NumElts/2))
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00003967 return false;
3968 return true;
3969}
3970
Evan Cheng0b457f02008-09-25 20:50:48 +00003971/// isMOVDDUPMask - Return true if the specified VECTOR_SHUFFLE operand
Bruno Cardoso Lopes06ef9232011-08-25 21:40:34 +00003972/// specifies a shuffle of elements that is suitable for input to 128-bit
3973/// version of MOVDDUP.
Craig Topperdd637ae2012-02-19 05:41:45 +00003974static bool isMOVDDUPMask(ArrayRef<int> Mask, EVT VT) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00003975 if (!VT.is128BitVector())
Bruno Cardoso Lopes06ef9232011-08-25 21:40:34 +00003976 return false;
3977
Craig Topperc612d792012-01-02 09:17:37 +00003978 unsigned e = VT.getVectorNumElements() / 2;
3979 for (unsigned i = 0; i != e; ++i)
Craig Topperdd637ae2012-02-19 05:41:45 +00003980 if (!isUndefOrEqual(Mask[i], i))
Evan Cheng0b457f02008-09-25 20:50:48 +00003981 return false;
Craig Topperc612d792012-01-02 09:17:37 +00003982 for (unsigned i = 0; i != e; ++i)
Craig Topperdd637ae2012-02-19 05:41:45 +00003983 if (!isUndefOrEqual(Mask[e+i], i))
Evan Cheng0b457f02008-09-25 20:50:48 +00003984 return false;
3985 return true;
3986}
3987
David Greenec38a03e2011-02-03 15:50:00 +00003988/// isVEXTRACTF128Index - Return true if the specified
3989/// EXTRACT_SUBVECTOR operand specifies a vector extract that is
3990/// suitable for input to VEXTRACTF128.
3991bool X86::isVEXTRACTF128Index(SDNode *N) {
3992 if (!isa<ConstantSDNode>(N->getOperand(1).getNode()))
3993 return false;
3994
3995 // The index should be aligned on a 128-bit boundary.
3996 uint64_t Index =
3997 cast<ConstantSDNode>(N->getOperand(1).getNode())->getZExtValue();
3998
3999 unsigned VL = N->getValueType(0).getVectorNumElements();
4000 unsigned VBits = N->getValueType(0).getSizeInBits();
4001 unsigned ElSize = VBits / VL;
4002 bool Result = (Index * ElSize) % 128 == 0;
4003
4004 return Result;
4005}
4006
David Greeneccacdc12011-02-04 16:08:29 +00004007/// isVINSERTF128Index - Return true if the specified INSERT_SUBVECTOR
4008/// operand specifies a subvector insert that is suitable for input to
4009/// VINSERTF128.
4010bool X86::isVINSERTF128Index(SDNode *N) {
4011 if (!isa<ConstantSDNode>(N->getOperand(2).getNode()))
4012 return false;
4013
4014 // The index should be aligned on a 128-bit boundary.
4015 uint64_t Index =
4016 cast<ConstantSDNode>(N->getOperand(2).getNode())->getZExtValue();
4017
4018 unsigned VL = N->getValueType(0).getVectorNumElements();
4019 unsigned VBits = N->getValueType(0).getSizeInBits();
4020 unsigned ElSize = VBits / VL;
4021 bool Result = (Index * ElSize) % 128 == 0;
4022
4023 return Result;
4024}
4025
Evan Cheng63d33002006-03-22 08:01:21 +00004026/// getShuffleSHUFImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00004027/// the specified VECTOR_SHUFFLE mask with PSHUF* and SHUFP* instructions.
Craig Topper1a7700a2012-01-19 08:19:12 +00004028/// Handles 128-bit and 256-bit.
Craig Topper5aaffa82012-02-19 02:53:47 +00004029static unsigned getShuffleSHUFImmediate(ShuffleVectorSDNode *N) {
Craig Topper1a7700a2012-01-19 08:19:12 +00004030 EVT VT = N->getValueType(0);
Nate Begeman9008ca62009-04-27 18:41:29 +00004031
Craig Topper1a7700a2012-01-19 08:19:12 +00004032 assert((VT.is128BitVector() || VT.is256BitVector()) &&
4033 "Unsupported vector type for PSHUF/SHUFP");
4034
4035 // Handle 128 and 256-bit vector lengths. AVX defines PSHUF/SHUFP to operate
4036 // independently on 128-bit lanes.
4037 unsigned NumElts = VT.getVectorNumElements();
4038 unsigned NumLanes = VT.getSizeInBits()/128;
4039 unsigned NumLaneElts = NumElts/NumLanes;
4040
4041 assert((NumLaneElts == 2 || NumLaneElts == 4) &&
4042 "Only supports 2 or 4 elements per lane");
4043
4044 unsigned Shift = (NumLaneElts == 4) ? 1 : 0;
Evan Chengb9df0ca2006-03-22 02:53:00 +00004045 unsigned Mask = 0;
Craig Topper1a7700a2012-01-19 08:19:12 +00004046 for (unsigned i = 0; i != NumElts; ++i) {
4047 int Elt = N->getMaskElt(i);
4048 if (Elt < 0) continue;
Craig Topper6b28d352012-05-03 07:12:59 +00004049 Elt &= NumLaneElts - 1;
4050 unsigned ShAmt = (i << Shift) % 8;
Craig Topper1a7700a2012-01-19 08:19:12 +00004051 Mask |= Elt << ShAmt;
Evan Cheng36b27f32006-03-28 23:41:33 +00004052 }
Craig Topper1a7700a2012-01-19 08:19:12 +00004053
Evan Cheng63d33002006-03-22 08:01:21 +00004054 return Mask;
4055}
4056
Evan Cheng506d3df2006-03-29 23:07:14 +00004057/// getShufflePSHUFHWImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00004058/// the specified VECTOR_SHUFFLE mask with the PSHUFHW instruction.
Craig Topperdd637ae2012-02-19 05:41:45 +00004059static unsigned getShufflePSHUFHWImmediate(ShuffleVectorSDNode *N) {
Craig Topper6b28d352012-05-03 07:12:59 +00004060 EVT VT = N->getValueType(0);
4061
4062 assert((VT == MVT::v8i16 || VT == MVT::v16i16) &&
4063 "Unsupported vector type for PSHUFHW");
4064
4065 unsigned NumElts = VT.getVectorNumElements();
4066
Evan Cheng506d3df2006-03-29 23:07:14 +00004067 unsigned Mask = 0;
Craig Topper6b28d352012-05-03 07:12:59 +00004068 for (unsigned l = 0; l != NumElts; l += 8) {
4069 // 8 nodes per lane, but we only care about the last 4.
4070 for (unsigned i = 0; i < 4; ++i) {
4071 int Elt = N->getMaskElt(l+i+4);
4072 if (Elt < 0) continue;
4073 Elt &= 0x3; // only 2-bits.
4074 Mask |= Elt << (i * 2);
4075 }
Evan Cheng506d3df2006-03-29 23:07:14 +00004076 }
Craig Topper6b28d352012-05-03 07:12:59 +00004077
Evan Cheng506d3df2006-03-29 23:07:14 +00004078 return Mask;
4079}
4080
4081/// getShufflePSHUFLWImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00004082/// the specified VECTOR_SHUFFLE mask with the PSHUFLW instruction.
Craig Topperdd637ae2012-02-19 05:41:45 +00004083static unsigned getShufflePSHUFLWImmediate(ShuffleVectorSDNode *N) {
Craig Topper6b28d352012-05-03 07:12:59 +00004084 EVT VT = N->getValueType(0);
4085
4086 assert((VT == MVT::v8i16 || VT == MVT::v16i16) &&
4087 "Unsupported vector type for PSHUFHW");
4088
4089 unsigned NumElts = VT.getVectorNumElements();
4090
Evan Cheng506d3df2006-03-29 23:07:14 +00004091 unsigned Mask = 0;
Craig Topper6b28d352012-05-03 07:12:59 +00004092 for (unsigned l = 0; l != NumElts; l += 8) {
4093 // 8 nodes per lane, but we only care about the first 4.
4094 for (unsigned i = 0; i < 4; ++i) {
4095 int Elt = N->getMaskElt(l+i);
4096 if (Elt < 0) continue;
4097 Elt &= 0x3; // only 2-bits
4098 Mask |= Elt << (i * 2);
4099 }
Evan Cheng506d3df2006-03-29 23:07:14 +00004100 }
Craig Topper6b28d352012-05-03 07:12:59 +00004101
Evan Cheng506d3df2006-03-29 23:07:14 +00004102 return Mask;
4103}
4104
Nate Begemana09008b2009-10-19 02:17:23 +00004105/// getShufflePALIGNRImmediate - Return the appropriate immediate to shuffle
4106/// the specified VECTOR_SHUFFLE mask with the PALIGNR instruction.
Craig Topperd93e4c32011-12-11 19:12:35 +00004107static unsigned getShufflePALIGNRImmediate(ShuffleVectorSDNode *SVOp) {
4108 EVT VT = SVOp->getValueType(0);
4109 unsigned EltSize = VT.getVectorElementType().getSizeInBits() >> 3;
Nate Begemana09008b2009-10-19 02:17:23 +00004110
Craig Topper0e2037b2012-01-20 05:53:00 +00004111 unsigned NumElts = VT.getVectorNumElements();
4112 unsigned NumLanes = VT.getSizeInBits()/128;
4113 unsigned NumLaneElts = NumElts/NumLanes;
4114
4115 int Val = 0;
4116 unsigned i;
4117 for (i = 0; i != NumElts; ++i) {
Nate Begemana09008b2009-10-19 02:17:23 +00004118 Val = SVOp->getMaskElt(i);
4119 if (Val >= 0)
4120 break;
4121 }
Craig Topper0e2037b2012-01-20 05:53:00 +00004122 if (Val >= (int)NumElts)
4123 Val -= NumElts - NumLaneElts;
4124
Eli Friedman63f8dde2011-07-25 21:36:45 +00004125 assert(Val - i > 0 && "PALIGNR imm should be positive");
Nate Begemana09008b2009-10-19 02:17:23 +00004126 return (Val - i) * EltSize;
4127}
4128
David Greenec38a03e2011-02-03 15:50:00 +00004129/// getExtractVEXTRACTF128Immediate - Return the appropriate immediate
4130/// to extract the specified EXTRACT_SUBVECTOR index with VEXTRACTF128
4131/// instructions.
4132unsigned X86::getExtractVEXTRACTF128Immediate(SDNode *N) {
4133 if (!isa<ConstantSDNode>(N->getOperand(1).getNode()))
4134 llvm_unreachable("Illegal extract subvector for VEXTRACTF128");
4135
4136 uint64_t Index =
4137 cast<ConstantSDNode>(N->getOperand(1).getNode())->getZExtValue();
4138
4139 EVT VecVT = N->getOperand(0).getValueType();
4140 EVT ElVT = VecVT.getVectorElementType();
4141
4142 unsigned NumElemsPerChunk = 128 / ElVT.getSizeInBits();
David Greenec38a03e2011-02-03 15:50:00 +00004143 return Index / NumElemsPerChunk;
4144}
4145
David Greeneccacdc12011-02-04 16:08:29 +00004146/// getInsertVINSERTF128Immediate - Return the appropriate immediate
4147/// to insert at the specified INSERT_SUBVECTOR index with VINSERTF128
4148/// instructions.
4149unsigned X86::getInsertVINSERTF128Immediate(SDNode *N) {
4150 if (!isa<ConstantSDNode>(N->getOperand(2).getNode()))
4151 llvm_unreachable("Illegal insert subvector for VINSERTF128");
4152
4153 uint64_t Index =
NAKAMURA Takumi27635382011-02-05 15:10:54 +00004154 cast<ConstantSDNode>(N->getOperand(2).getNode())->getZExtValue();
David Greeneccacdc12011-02-04 16:08:29 +00004155
4156 EVT VecVT = N->getValueType(0);
4157 EVT ElVT = VecVT.getVectorElementType();
4158
4159 unsigned NumElemsPerChunk = 128 / ElVT.getSizeInBits();
David Greeneccacdc12011-02-04 16:08:29 +00004160 return Index / NumElemsPerChunk;
4161}
4162
Elena Demikhovsky73c504a2012-04-15 11:18:59 +00004163/// getShuffleCLImmediate - Return the appropriate immediate to shuffle
4164/// the specified VECTOR_SHUFFLE mask with VPERMQ and VPERMPD instructions.
4165/// Handles 256-bit.
4166static unsigned getShuffleCLImmediate(ShuffleVectorSDNode *N) {
4167 EVT VT = N->getValueType(0);
4168
Elena Demikhovsky73c504a2012-04-15 11:18:59 +00004169 unsigned NumElts = VT.getVectorNumElements();
4170
Craig Topper095c5282012-04-15 23:48:57 +00004171 assert((VT.is256BitVector() && NumElts == 4) &&
4172 "Unsupported vector type for VPERMQ/VPERMPD");
4173
Elena Demikhovsky73c504a2012-04-15 11:18:59 +00004174 unsigned Mask = 0;
4175 for (unsigned i = 0; i != NumElts; ++i) {
4176 int Elt = N->getMaskElt(i);
Craig Topper095c5282012-04-15 23:48:57 +00004177 if (Elt < 0)
Elena Demikhovsky73c504a2012-04-15 11:18:59 +00004178 continue;
4179 Mask |= Elt << (i*2);
4180 }
4181
4182 return Mask;
4183}
Evan Cheng37b73872009-07-30 08:33:02 +00004184/// isZeroNode - Returns true if Elt is a constant zero or a floating point
4185/// constant +0.0.
4186bool X86::isZeroNode(SDValue Elt) {
4187 return ((isa<ConstantSDNode>(Elt) &&
Dan Gohmane368b462010-06-18 14:22:04 +00004188 cast<ConstantSDNode>(Elt)->isNullValue()) ||
Evan Cheng37b73872009-07-30 08:33:02 +00004189 (isa<ConstantFPSDNode>(Elt) &&
4190 cast<ConstantFPSDNode>(Elt)->getValueAPF().isPosZero()));
4191}
4192
Nate Begeman9008ca62009-04-27 18:41:29 +00004193/// CommuteVectorShuffle - Swap vector_shuffle operands as well as values in
4194/// their permute mask.
4195static SDValue CommuteVectorShuffle(ShuffleVectorSDNode *SVOp,
4196 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00004197 EVT VT = SVOp->getValueType(0);
Nate Begeman5a5ca152009-04-29 05:20:52 +00004198 unsigned NumElems = VT.getVectorNumElements();
Nate Begeman9008ca62009-04-27 18:41:29 +00004199 SmallVector<int, 8> MaskVec;
Eric Christopherfd179292009-08-27 18:07:15 +00004200
Nate Begeman5a5ca152009-04-29 05:20:52 +00004201 for (unsigned i = 0; i != NumElems; ++i) {
Craig Topper8ae97ba2012-05-21 06:40:16 +00004202 int Idx = SVOp->getMaskElt(i);
4203 if (Idx >= 0) {
4204 if (Idx < (int)NumElems)
4205 Idx += NumElems;
4206 else
4207 Idx -= NumElems;
4208 }
4209 MaskVec.push_back(Idx);
Evan Cheng5ced1d82006-04-06 23:23:56 +00004210 }
Nate Begeman9008ca62009-04-27 18:41:29 +00004211 return DAG.getVectorShuffle(VT, SVOp->getDebugLoc(), SVOp->getOperand(1),
4212 SVOp->getOperand(0), &MaskVec[0]);
Evan Cheng5ced1d82006-04-06 23:23:56 +00004213}
4214
Evan Cheng533a0aa2006-04-19 20:35:22 +00004215/// ShouldXformToMOVHLPS - Return true if the node should be transformed to
4216/// match movhlps. The lower half elements should come from upper half of
4217/// V1 (and in order), and the upper half elements should come from the upper
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00004218/// half of V2 (and in order).
Craig Topperdd637ae2012-02-19 05:41:45 +00004219static bool ShouldXformToMOVHLPS(ArrayRef<int> Mask, EVT VT) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00004220 if (!VT.is128BitVector())
Bruno Cardoso Lopes59353b42011-08-11 18:59:13 +00004221 return false;
4222 if (VT.getVectorNumElements() != 4)
Evan Cheng533a0aa2006-04-19 20:35:22 +00004223 return false;
4224 for (unsigned i = 0, e = 2; i != e; ++i)
Craig Topperdd637ae2012-02-19 05:41:45 +00004225 if (!isUndefOrEqual(Mask[i], i+2))
Evan Cheng533a0aa2006-04-19 20:35:22 +00004226 return false;
4227 for (unsigned i = 2; i != 4; ++i)
Craig Topperdd637ae2012-02-19 05:41:45 +00004228 if (!isUndefOrEqual(Mask[i], i+4))
Evan Cheng533a0aa2006-04-19 20:35:22 +00004229 return false;
4230 return true;
4231}
4232
Evan Cheng5ced1d82006-04-06 23:23:56 +00004233/// isScalarLoadToVector - Returns true if the node is a scalar load that
Evan Cheng7e2ff772008-05-08 00:57:18 +00004234/// is promoted to a vector. It also returns the LoadSDNode by reference if
4235/// required.
4236static bool isScalarLoadToVector(SDNode *N, LoadSDNode **LD = NULL) {
Evan Cheng0b457f02008-09-25 20:50:48 +00004237 if (N->getOpcode() != ISD::SCALAR_TO_VECTOR)
4238 return false;
4239 N = N->getOperand(0).getNode();
4240 if (!ISD::isNON_EXTLoad(N))
4241 return false;
4242 if (LD)
4243 *LD = cast<LoadSDNode>(N);
4244 return true;
Evan Cheng5ced1d82006-04-06 23:23:56 +00004245}
4246
Dan Gohman65fd6562011-11-03 21:49:52 +00004247// Test whether the given value is a vector value which will be legalized
4248// into a load.
4249static bool WillBeConstantPoolLoad(SDNode *N) {
4250 if (N->getOpcode() != ISD::BUILD_VECTOR)
4251 return false;
4252
4253 // Check for any non-constant elements.
4254 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
4255 switch (N->getOperand(i).getNode()->getOpcode()) {
4256 case ISD::UNDEF:
4257 case ISD::ConstantFP:
4258 case ISD::Constant:
4259 break;
4260 default:
4261 return false;
4262 }
4263
4264 // Vectors of all-zeros and all-ones are materialized with special
4265 // instructions rather than being loaded.
4266 return !ISD::isBuildVectorAllZeros(N) &&
4267 !ISD::isBuildVectorAllOnes(N);
4268}
4269
Evan Cheng533a0aa2006-04-19 20:35:22 +00004270/// ShouldXformToMOVLP{S|D} - Return true if the node should be transformed to
4271/// match movlp{s|d}. The lower half elements should come from lower half of
4272/// V1 (and in order), and the upper half elements should come from the upper
4273/// half of V2 (and in order). And since V1 will become the source of the
4274/// MOVLP, it must be either a vector load or a scalar load to vector.
Nate Begeman9008ca62009-04-27 18:41:29 +00004275static bool ShouldXformToMOVLP(SDNode *V1, SDNode *V2,
Craig Topperdd637ae2012-02-19 05:41:45 +00004276 ArrayRef<int> Mask, EVT VT) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00004277 if (!VT.is128BitVector())
Bruno Cardoso Lopes59353b42011-08-11 18:59:13 +00004278 return false;
4279
Evan Cheng466685d2006-10-09 20:57:25 +00004280 if (!ISD::isNON_EXTLoad(V1) && !isScalarLoadToVector(V1))
Evan Cheng533a0aa2006-04-19 20:35:22 +00004281 return false;
Evan Cheng23425f52006-10-09 21:39:25 +00004282 // Is V2 is a vector load, don't do this transformation. We will try to use
4283 // load folding shufps op.
Dan Gohman65fd6562011-11-03 21:49:52 +00004284 if (ISD::isNON_EXTLoad(V2) || WillBeConstantPoolLoad(V2))
Evan Cheng23425f52006-10-09 21:39:25 +00004285 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00004286
Bruno Cardoso Lopes59353b42011-08-11 18:59:13 +00004287 unsigned NumElems = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00004288
Evan Cheng533a0aa2006-04-19 20:35:22 +00004289 if (NumElems != 2 && NumElems != 4)
4290 return false;
Nate Begeman5a5ca152009-04-29 05:20:52 +00004291 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
Craig Topperdd637ae2012-02-19 05:41:45 +00004292 if (!isUndefOrEqual(Mask[i], i))
Evan Cheng533a0aa2006-04-19 20:35:22 +00004293 return false;
Chad Rosier238ae312012-04-30 17:47:15 +00004294 for (unsigned i = NumElems/2, e = NumElems; i != e; ++i)
Craig Topperdd637ae2012-02-19 05:41:45 +00004295 if (!isUndefOrEqual(Mask[i], i+NumElems))
Evan Cheng533a0aa2006-04-19 20:35:22 +00004296 return false;
4297 return true;
Evan Cheng5ced1d82006-04-06 23:23:56 +00004298}
4299
Evan Cheng39623da2006-04-20 08:58:49 +00004300/// isSplatVector - Returns true if N is a BUILD_VECTOR node whose elements are
4301/// all the same.
4302static bool isSplatVector(SDNode *N) {
4303 if (N->getOpcode() != ISD::BUILD_VECTOR)
4304 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00004305
Dan Gohman475871a2008-07-27 21:46:04 +00004306 SDValue SplatValue = N->getOperand(0);
Evan Cheng39623da2006-04-20 08:58:49 +00004307 for (unsigned i = 1, e = N->getNumOperands(); i != e; ++i)
4308 if (N->getOperand(i) != SplatValue)
Evan Cheng5ced1d82006-04-06 23:23:56 +00004309 return false;
4310 return true;
4311}
4312
Evan Cheng213d2cf2007-05-17 18:45:50 +00004313/// isZeroShuffle - Returns true if N is a VECTOR_SHUFFLE that can be resolved
Eric Christopherfd179292009-08-27 18:07:15 +00004314/// to an zero vector.
Nate Begeman5a5ca152009-04-29 05:20:52 +00004315/// FIXME: move to dag combiner / method on ShuffleVectorSDNode
Nate Begeman9008ca62009-04-27 18:41:29 +00004316static bool isZeroShuffle(ShuffleVectorSDNode *N) {
Dan Gohman475871a2008-07-27 21:46:04 +00004317 SDValue V1 = N->getOperand(0);
4318 SDValue V2 = N->getOperand(1);
Nate Begeman5a5ca152009-04-29 05:20:52 +00004319 unsigned NumElems = N->getValueType(0).getVectorNumElements();
4320 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004321 int Idx = N->getMaskElt(i);
Nate Begeman5a5ca152009-04-29 05:20:52 +00004322 if (Idx >= (int)NumElems) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004323 unsigned Opc = V2.getOpcode();
Rafael Espindola15684b22009-04-24 12:40:33 +00004324 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V2.getNode()))
4325 continue;
Evan Cheng37b73872009-07-30 08:33:02 +00004326 if (Opc != ISD::BUILD_VECTOR ||
4327 !X86::isZeroNode(V2.getOperand(Idx-NumElems)))
Nate Begeman9008ca62009-04-27 18:41:29 +00004328 return false;
4329 } else if (Idx >= 0) {
4330 unsigned Opc = V1.getOpcode();
4331 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V1.getNode()))
4332 continue;
Evan Cheng37b73872009-07-30 08:33:02 +00004333 if (Opc != ISD::BUILD_VECTOR ||
4334 !X86::isZeroNode(V1.getOperand(Idx)))
Chris Lattner8a594482007-11-25 00:24:49 +00004335 return false;
Evan Cheng213d2cf2007-05-17 18:45:50 +00004336 }
4337 }
4338 return true;
4339}
4340
4341/// getZeroVector - Returns a vector of specified type with all zero elements.
4342///
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00004343static SDValue getZeroVector(EVT VT, const X86Subtarget *Subtarget,
Craig Topper12216172012-01-13 08:12:35 +00004344 SelectionDAG &DAG, DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004345 assert(VT.isVector() && "Expected a vector type");
Craig Topper9d352402012-04-23 07:24:41 +00004346 unsigned Size = VT.getSizeInBits();
Scott Michelfdc40a02009-02-17 22:15:04 +00004347
Dale Johannesen0488fb62010-09-30 23:57:10 +00004348 // Always build SSE zero vectors as <4 x i32> bitcasted
Bruno Cardoso Lopes8c05a852010-08-12 02:06:36 +00004349 // to their dest type. This ensures they get CSE'd.
Dan Gohman475871a2008-07-27 21:46:04 +00004350 SDValue Vec;
Craig Topper9d352402012-04-23 07:24:41 +00004351 if (Size == 128) { // SSE
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00004352 if (Subtarget->hasSSE2()) { // SSE2
Bruno Cardoso Lopes8c05a852010-08-12 02:06:36 +00004353 SDValue Cst = DAG.getTargetConstant(0, MVT::i32);
4354 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
4355 } else { // SSE1
4356 SDValue Cst = DAG.getTargetConstantFP(+0.0, MVT::f32);
4357 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4f32, Cst, Cst, Cst, Cst);
4358 }
Craig Topper9d352402012-04-23 07:24:41 +00004359 } else if (Size == 256) { // AVX
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00004360 if (Subtarget->hasInt256()) { // AVX2
Craig Topper12216172012-01-13 08:12:35 +00004361 SDValue Cst = DAG.getTargetConstant(0, MVT::i32);
4362 SDValue Ops[] = { Cst, Cst, Cst, Cst, Cst, Cst, Cst, Cst };
4363 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v8i32, Ops, 8);
4364 } else {
4365 // 256-bit logic and arithmetic instructions in AVX are all
4366 // floating-point, no support for integer ops. Emit fp zeroed vectors.
4367 SDValue Cst = DAG.getTargetConstantFP(+0.0, MVT::f32);
4368 SDValue Ops[] = { Cst, Cst, Cst, Cst, Cst, Cst, Cst, Cst };
4369 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v8f32, Ops, 8);
4370 }
Craig Topper9d352402012-04-23 07:24:41 +00004371 } else
4372 llvm_unreachable("Unexpected vector type");
4373
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004374 return DAG.getNode(ISD::BITCAST, dl, VT, Vec);
Evan Cheng213d2cf2007-05-17 18:45:50 +00004375}
4376
Chris Lattner8a594482007-11-25 00:24:49 +00004377/// getOnesVector - Returns a vector of specified type with all bits set.
Craig Topper745a86b2011-11-19 22:34:59 +00004378/// Always build ones vectors as <4 x i32> or <8 x i32>. For 256-bit types with
4379/// no AVX2 supprt, use two <4 x i32> inserted in a <8 x i32> appropriately.
4380/// Then bitcast to their original type, ensuring they get CSE'd.
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00004381static SDValue getOnesVector(EVT VT, bool HasInt256, SelectionDAG &DAG,
Craig Topper745a86b2011-11-19 22:34:59 +00004382 DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004383 assert(VT.isVector() && "Expected a vector type");
Craig Topper9d352402012-04-23 07:24:41 +00004384 unsigned Size = VT.getSizeInBits();
Scott Michelfdc40a02009-02-17 22:15:04 +00004385
Owen Anderson825b72b2009-08-11 20:47:22 +00004386 SDValue Cst = DAG.getTargetConstant(~0U, MVT::i32);
Craig Topper745a86b2011-11-19 22:34:59 +00004387 SDValue Vec;
Craig Topper9d352402012-04-23 07:24:41 +00004388 if (Size == 256) {
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00004389 if (HasInt256) { // AVX2
Craig Topper745a86b2011-11-19 22:34:59 +00004390 SDValue Ops[] = { Cst, Cst, Cst, Cst, Cst, Cst, Cst, Cst };
4391 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v8i32, Ops, 8);
4392 } else { // AVX
4393 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
Craig Topper4c7972d2012-04-22 18:15:59 +00004394 Vec = Concat128BitVectors(Vec, Vec, MVT::v8i32, 8, DAG, dl);
Craig Topper745a86b2011-11-19 22:34:59 +00004395 }
Craig Topper9d352402012-04-23 07:24:41 +00004396 } else if (Size == 128) {
Craig Topper745a86b2011-11-19 22:34:59 +00004397 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
Craig Topper9d352402012-04-23 07:24:41 +00004398 } else
4399 llvm_unreachable("Unexpected vector type");
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +00004400
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004401 return DAG.getNode(ISD::BITCAST, dl, VT, Vec);
Chris Lattner8a594482007-11-25 00:24:49 +00004402}
4403
Evan Cheng39623da2006-04-20 08:58:49 +00004404/// NormalizeMask - V2 is a splat, modify the mask (if needed) so all elements
4405/// that point to V2 points to its first element.
Craig Topper39a9e482012-02-11 06:24:48 +00004406static void NormalizeMask(SmallVectorImpl<int> &Mask, unsigned NumElems) {
Nate Begeman5a5ca152009-04-29 05:20:52 +00004407 for (unsigned i = 0; i != NumElems; ++i) {
Craig Topper39a9e482012-02-11 06:24:48 +00004408 if (Mask[i] > (int)NumElems) {
4409 Mask[i] = NumElems;
Evan Cheng39623da2006-04-20 08:58:49 +00004410 }
Evan Cheng39623da2006-04-20 08:58:49 +00004411 }
Evan Cheng39623da2006-04-20 08:58:49 +00004412}
4413
Evan Cheng017dcc62006-04-21 01:05:10 +00004414/// getMOVLMask - Returns a vector_shuffle mask for an movs{s|d}, movd
4415/// operation of specified width.
Owen Andersone50ed302009-08-10 22:56:29 +00004416static SDValue getMOVL(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00004417 SDValue V2) {
4418 unsigned NumElems = VT.getVectorNumElements();
4419 SmallVector<int, 8> Mask;
4420 Mask.push_back(NumElems);
Evan Cheng39623da2006-04-20 08:58:49 +00004421 for (unsigned i = 1; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004422 Mask.push_back(i);
4423 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Evan Cheng39623da2006-04-20 08:58:49 +00004424}
4425
Nate Begeman9008ca62009-04-27 18:41:29 +00004426/// getUnpackl - Returns a vector_shuffle node for an unpackl operation.
Owen Andersone50ed302009-08-10 22:56:29 +00004427static SDValue getUnpackl(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00004428 SDValue V2) {
4429 unsigned NumElems = VT.getVectorNumElements();
4430 SmallVector<int, 8> Mask;
Evan Chengc575ca22006-04-17 20:43:08 +00004431 for (unsigned i = 0, e = NumElems/2; i != e; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004432 Mask.push_back(i);
4433 Mask.push_back(i + NumElems);
Evan Chengc575ca22006-04-17 20:43:08 +00004434 }
Nate Begeman9008ca62009-04-27 18:41:29 +00004435 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Evan Chengc575ca22006-04-17 20:43:08 +00004436}
4437
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004438/// getUnpackh - Returns a vector_shuffle node for an unpackh operation.
Owen Andersone50ed302009-08-10 22:56:29 +00004439static SDValue getUnpackh(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00004440 SDValue V2) {
4441 unsigned NumElems = VT.getVectorNumElements();
Nate Begeman9008ca62009-04-27 18:41:29 +00004442 SmallVector<int, 8> Mask;
Chad Rosier238ae312012-04-30 17:47:15 +00004443 for (unsigned i = 0, Half = NumElems/2; i != Half; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004444 Mask.push_back(i + Half);
4445 Mask.push_back(i + NumElems + Half);
Evan Cheng39623da2006-04-20 08:58:49 +00004446 }
Nate Begeman9008ca62009-04-27 18:41:29 +00004447 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Chris Lattner62098042008-03-09 01:05:04 +00004448}
4449
Bruno Cardoso Lopes5f1d8ab2011-08-11 02:49:44 +00004450// PromoteSplati8i16 - All i16 and i8 vector types can't be used directly by
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004451// a generic shuffle instruction because the target has no such instructions.
4452// Generate shuffles which repeat i16 and i8 several times until they can be
4453// represented by v4f32 and then be manipulated by target suported shuffles.
Bruno Cardoso Lopes5f1d8ab2011-08-11 02:49:44 +00004454static SDValue PromoteSplati8i16(SDValue V, SelectionDAG &DAG, int &EltNo) {
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004455 EVT VT = V.getValueType();
Nate Begeman9008ca62009-04-27 18:41:29 +00004456 int NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004457 DebugLoc dl = V.getDebugLoc();
Rafael Espindola15684b22009-04-24 12:40:33 +00004458
Nate Begeman9008ca62009-04-27 18:41:29 +00004459 while (NumElems > 4) {
4460 if (EltNo < NumElems/2) {
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004461 V = getUnpackl(DAG, dl, VT, V, V);
Nate Begeman9008ca62009-04-27 18:41:29 +00004462 } else {
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004463 V = getUnpackh(DAG, dl, VT, V, V);
Nate Begeman9008ca62009-04-27 18:41:29 +00004464 EltNo -= NumElems/2;
4465 }
4466 NumElems >>= 1;
4467 }
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004468 return V;
4469}
Eric Christopherfd179292009-08-27 18:07:15 +00004470
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004471/// getLegalSplat - Generate a legal splat with supported x86 shuffles
4472static SDValue getLegalSplat(SelectionDAG &DAG, SDValue V, int EltNo) {
4473 EVT VT = V.getValueType();
4474 DebugLoc dl = V.getDebugLoc();
Craig Topper9d352402012-04-23 07:24:41 +00004475 unsigned Size = VT.getSizeInBits();
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004476
Craig Topper9d352402012-04-23 07:24:41 +00004477 if (Size == 128) {
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004478 V = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, V);
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004479 int SplatMask[4] = { EltNo, EltNo, EltNo, EltNo };
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004480 V = DAG.getVectorShuffle(MVT::v4f32, dl, V, DAG.getUNDEF(MVT::v4f32),
4481 &SplatMask[0]);
Craig Topper9d352402012-04-23 07:24:41 +00004482 } else if (Size == 256) {
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004483 // To use VPERMILPS to splat scalars, the second half of indicies must
4484 // refer to the higher part, which is a duplication of the lower one,
4485 // because VPERMILPS can only handle in-lane permutations.
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004486 int SplatMask[8] = { EltNo, EltNo, EltNo, EltNo,
4487 EltNo+4, EltNo+4, EltNo+4, EltNo+4 };
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004488
4489 V = DAG.getNode(ISD::BITCAST, dl, MVT::v8f32, V);
4490 V = DAG.getVectorShuffle(MVT::v8f32, dl, V, DAG.getUNDEF(MVT::v8f32),
4491 &SplatMask[0]);
Craig Topper9d352402012-04-23 07:24:41 +00004492 } else
4493 llvm_unreachable("Vector size not supported");
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004494
4495 return DAG.getNode(ISD::BITCAST, dl, VT, V);
4496}
4497
Bruno Cardoso Lopes8a5b2622011-08-17 02:29:13 +00004498/// PromoteSplat - Splat is promoted to target supported vector shuffles.
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004499static SDValue PromoteSplat(ShuffleVectorSDNode *SV, SelectionDAG &DAG) {
4500 EVT SrcVT = SV->getValueType(0);
4501 SDValue V1 = SV->getOperand(0);
4502 DebugLoc dl = SV->getDebugLoc();
4503
4504 int EltNo = SV->getSplatIndex();
4505 int NumElems = SrcVT.getVectorNumElements();
4506 unsigned Size = SrcVT.getSizeInBits();
4507
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004508 assert(((Size == 128 && NumElems > 4) || Size == 256) &&
4509 "Unknown how to promote splat for type");
4510
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004511 // Extract the 128-bit part containing the splat element and update
4512 // the splat element index when it refers to the higher register.
4513 if (Size == 256) {
Craig Topper7d1e3dc2012-04-30 05:17:10 +00004514 V1 = Extract128BitVector(V1, EltNo, DAG, dl);
4515 if (EltNo >= NumElems/2)
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004516 EltNo -= NumElems/2;
4517 }
4518
Bruno Cardoso Lopes8a5b2622011-08-17 02:29:13 +00004519 // All i16 and i8 vector types can't be used directly by a generic shuffle
4520 // instruction because the target has no such instruction. Generate shuffles
4521 // which repeat i16 and i8 several times until they fit in i32, and then can
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004522 // be manipulated by target suported shuffles.
Bruno Cardoso Lopes5f1d8ab2011-08-11 02:49:44 +00004523 EVT EltVT = SrcVT.getVectorElementType();
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004524 if (EltVT == MVT::i8 || EltVT == MVT::i16)
Bruno Cardoso Lopes5f1d8ab2011-08-11 02:49:44 +00004525 V1 = PromoteSplati8i16(V1, DAG, EltNo);
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004526
4527 // Recreate the 256-bit vector and place the same 128-bit vector
4528 // into the low and high part. This is necessary because we want
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004529 // to use VPERM* to shuffle the vectors
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004530 if (Size == 256) {
Craig Topper4c7972d2012-04-22 18:15:59 +00004531 V1 = DAG.getNode(ISD::CONCAT_VECTORS, dl, SrcVT, V1, V1);
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004532 }
4533
4534 return getLegalSplat(DAG, V1, EltNo);
Evan Chengc575ca22006-04-17 20:43:08 +00004535}
4536
Evan Chengba05f722006-04-21 23:03:30 +00004537/// getShuffleVectorZeroOrUndef - Return a vector_shuffle of the specified
Chris Lattner8a594482007-11-25 00:24:49 +00004538/// vector of zero or undef vector. This produces a shuffle where the low
4539/// element of V2 is swizzled into the zero/undef vector, landing at element
4540/// 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 +00004541static SDValue getShuffleVectorZeroOrUndef(SDValue V2, unsigned Idx,
Craig Topper12216172012-01-13 08:12:35 +00004542 bool IsZero,
4543 const X86Subtarget *Subtarget,
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00004544 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00004545 EVT VT = V2.getValueType();
Craig Topper12216172012-01-13 08:12:35 +00004546 SDValue V1 = IsZero
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00004547 ? getZeroVector(VT, Subtarget, DAG, V2.getDebugLoc()) : DAG.getUNDEF(VT);
Nate Begeman9008ca62009-04-27 18:41:29 +00004548 unsigned NumElems = VT.getVectorNumElements();
4549 SmallVector<int, 16> MaskVec;
Chris Lattner8a594482007-11-25 00:24:49 +00004550 for (unsigned i = 0; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004551 // If this is the insertion idx, put the low elt of V2 here.
4552 MaskVec.push_back(i == Idx ? NumElems : i);
4553 return DAG.getVectorShuffle(VT, V2.getDebugLoc(), V1, V2, &MaskVec[0]);
Evan Cheng017dcc62006-04-21 01:05:10 +00004554}
4555
Craig Toppera1ffc682012-03-20 06:42:26 +00004556/// getTargetShuffleMask - Calculates the shuffle mask corresponding to the
4557/// target specific opcode. Returns true if the Mask could be calculated.
Craig Topper89f4e662012-03-20 07:17:59 +00004558/// Sets IsUnary to true if only uses one source.
Craig Topperd978c542012-05-06 19:46:21 +00004559static bool getTargetShuffleMask(SDNode *N, MVT VT,
Craig Topper89f4e662012-03-20 07:17:59 +00004560 SmallVectorImpl<int> &Mask, bool &IsUnary) {
Craig Toppera1ffc682012-03-20 06:42:26 +00004561 unsigned NumElems = VT.getVectorNumElements();
4562 SDValue ImmN;
4563
Craig Topper89f4e662012-03-20 07:17:59 +00004564 IsUnary = false;
Craig Toppera1ffc682012-03-20 06:42:26 +00004565 switch(N->getOpcode()) {
4566 case X86ISD::SHUFP:
4567 ImmN = N->getOperand(N->getNumOperands()-1);
4568 DecodeSHUFPMask(VT, cast<ConstantSDNode>(ImmN)->getZExtValue(), Mask);
4569 break;
4570 case X86ISD::UNPCKH:
4571 DecodeUNPCKHMask(VT, Mask);
4572 break;
4573 case X86ISD::UNPCKL:
4574 DecodeUNPCKLMask(VT, Mask);
4575 break;
4576 case X86ISD::MOVHLPS:
4577 DecodeMOVHLPSMask(NumElems, Mask);
4578 break;
4579 case X86ISD::MOVLHPS:
4580 DecodeMOVLHPSMask(NumElems, Mask);
4581 break;
4582 case X86ISD::PSHUFD:
4583 case X86ISD::VPERMILP:
4584 ImmN = N->getOperand(N->getNumOperands()-1);
4585 DecodePSHUFMask(VT, cast<ConstantSDNode>(ImmN)->getZExtValue(), Mask);
Craig Topper89f4e662012-03-20 07:17:59 +00004586 IsUnary = true;
Craig Toppera1ffc682012-03-20 06:42:26 +00004587 break;
4588 case X86ISD::PSHUFHW:
4589 ImmN = N->getOperand(N->getNumOperands()-1);
Craig Toppera9a568a2012-05-02 08:03:44 +00004590 DecodePSHUFHWMask(VT, cast<ConstantSDNode>(ImmN)->getZExtValue(), Mask);
Craig Topper89f4e662012-03-20 07:17:59 +00004591 IsUnary = true;
Craig Toppera1ffc682012-03-20 06:42:26 +00004592 break;
4593 case X86ISD::PSHUFLW:
4594 ImmN = N->getOperand(N->getNumOperands()-1);
Craig Toppera9a568a2012-05-02 08:03:44 +00004595 DecodePSHUFLWMask(VT, cast<ConstantSDNode>(ImmN)->getZExtValue(), Mask);
Craig Topper89f4e662012-03-20 07:17:59 +00004596 IsUnary = true;
Craig Toppera1ffc682012-03-20 06:42:26 +00004597 break;
Craig Topperbdcbcb32012-05-06 18:54:26 +00004598 case X86ISD::VPERMI:
4599 ImmN = N->getOperand(N->getNumOperands()-1);
4600 DecodeVPERMMask(cast<ConstantSDNode>(ImmN)->getZExtValue(), Mask);
4601 IsUnary = true;
4602 break;
Craig Toppera1ffc682012-03-20 06:42:26 +00004603 case X86ISD::MOVSS:
4604 case X86ISD::MOVSD: {
4605 // The index 0 always comes from the first element of the second source,
4606 // this is why MOVSS and MOVSD are used in the first place. The other
4607 // elements come from the other positions of the first source vector
4608 Mask.push_back(NumElems);
4609 for (unsigned i = 1; i != NumElems; ++i) {
4610 Mask.push_back(i);
4611 }
4612 break;
4613 }
4614 case X86ISD::VPERM2X128:
4615 ImmN = N->getOperand(N->getNumOperands()-1);
4616 DecodeVPERM2X128Mask(VT, cast<ConstantSDNode>(ImmN)->getZExtValue(), Mask);
Craig Topper2091df32012-04-17 05:54:54 +00004617 if (Mask.empty()) return false;
Craig Toppera1ffc682012-03-20 06:42:26 +00004618 break;
4619 case X86ISD::MOVDDUP:
4620 case X86ISD::MOVLHPD:
4621 case X86ISD::MOVLPD:
4622 case X86ISD::MOVLPS:
4623 case X86ISD::MOVSHDUP:
4624 case X86ISD::MOVSLDUP:
4625 case X86ISD::PALIGN:
4626 // Not yet implemented
4627 return false;
4628 default: llvm_unreachable("unknown target shuffle node");
4629 }
4630
4631 return true;
4632}
4633
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004634/// getShuffleScalarElt - Returns the scalar element that will make up the ith
4635/// element of the result of the vector shuffle.
Craig Topper3d092db2012-03-21 02:14:01 +00004636static SDValue getShuffleScalarElt(SDNode *N, unsigned Index, SelectionDAG &DAG,
Benjamin Kramer050db522011-03-26 12:38:19 +00004637 unsigned Depth) {
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004638 if (Depth == 6)
4639 return SDValue(); // Limit search depth.
4640
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004641 SDValue V = SDValue(N, 0);
4642 EVT VT = V.getValueType();
4643 unsigned Opcode = V.getOpcode();
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004644
4645 // Recurse into ISD::VECTOR_SHUFFLE node to find scalars.
4646 if (const ShuffleVectorSDNode *SV = dyn_cast<ShuffleVectorSDNode>(N)) {
Craig Topper3d092db2012-03-21 02:14:01 +00004647 int Elt = SV->getMaskElt(Index);
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004648
Craig Topper3d092db2012-03-21 02:14:01 +00004649 if (Elt < 0)
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004650 return DAG.getUNDEF(VT.getVectorElementType());
4651
Craig Topperd156dc12012-02-06 07:17:51 +00004652 unsigned NumElems = VT.getVectorNumElements();
Craig Topper3d092db2012-03-21 02:14:01 +00004653 SDValue NewV = (Elt < (int)NumElems) ? SV->getOperand(0)
4654 : SV->getOperand(1);
4655 return getShuffleScalarElt(NewV.getNode(), Elt % NumElems, DAG, Depth+1);
Evan Chengf26ffe92008-05-29 08:22:04 +00004656 }
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004657
4658 // Recurse into target specific vector shuffles to find scalars.
4659 if (isTargetShuffle(Opcode)) {
Craig Topperd978c542012-05-06 19:46:21 +00004660 MVT ShufVT = V.getValueType().getSimpleVT();
4661 unsigned NumElems = ShufVT.getVectorNumElements();
Craig Toppera1ffc682012-03-20 06:42:26 +00004662 SmallVector<int, 16> ShuffleMask;
Craig Topper89f4e662012-03-20 07:17:59 +00004663 bool IsUnary;
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004664
Craig Topperd978c542012-05-06 19:46:21 +00004665 if (!getTargetShuffleMask(N, ShufVT, ShuffleMask, IsUnary))
Craig Toppera1ffc682012-03-20 06:42:26 +00004666 return SDValue();
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004667
Craig Topper3d092db2012-03-21 02:14:01 +00004668 int Elt = ShuffleMask[Index];
4669 if (Elt < 0)
Craig Topperd978c542012-05-06 19:46:21 +00004670 return DAG.getUNDEF(ShufVT.getVectorElementType());
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004671
Craig Topper3d092db2012-03-21 02:14:01 +00004672 SDValue NewV = (Elt < (int)NumElems) ? N->getOperand(0)
Craig Topperd978c542012-05-06 19:46:21 +00004673 : N->getOperand(1);
Craig Topper3d092db2012-03-21 02:14:01 +00004674 return getShuffleScalarElt(NewV.getNode(), Elt % NumElems, DAG,
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004675 Depth+1);
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004676 }
4677
4678 // Actual nodes that may contain scalar elements
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004679 if (Opcode == ISD::BITCAST) {
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004680 V = V.getOperand(0);
4681 EVT SrcVT = V.getValueType();
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00004682 unsigned NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004683
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00004684 if (!SrcVT.isVector() || SrcVT.getVectorNumElements() != NumElems)
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004685 return SDValue();
4686 }
4687
4688 if (V.getOpcode() == ISD::SCALAR_TO_VECTOR)
4689 return (Index == 0) ? V.getOperand(0)
Craig Topper3d092db2012-03-21 02:14:01 +00004690 : DAG.getUNDEF(VT.getVectorElementType());
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004691
4692 if (V.getOpcode() == ISD::BUILD_VECTOR)
4693 return V.getOperand(Index);
4694
4695 return SDValue();
4696}
4697
4698/// getNumOfConsecutiveZeros - Return the number of elements of a vector
4699/// shuffle operation which come from a consecutively from a zero. The
Chris Lattner7a2bdde2011-04-15 05:18:47 +00004700/// search can start in two different directions, from left or right.
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004701static
Craig Topper3d092db2012-03-21 02:14:01 +00004702unsigned getNumOfConsecutiveZeros(ShuffleVectorSDNode *SVOp, unsigned NumElems,
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004703 bool ZerosFromLeft, SelectionDAG &DAG) {
Craig Topper3d092db2012-03-21 02:14:01 +00004704 unsigned i;
4705 for (i = 0; i != NumElems; ++i) {
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004706 unsigned Index = ZerosFromLeft ? i : NumElems-i-1;
Craig Topper3d092db2012-03-21 02:14:01 +00004707 SDValue Elt = getShuffleScalarElt(SVOp, Index, DAG, 0);
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004708 if (!(Elt.getNode() &&
4709 (Elt.getOpcode() == ISD::UNDEF || X86::isZeroNode(Elt))))
4710 break;
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004711 }
4712
4713 return i;
4714}
4715
Craig Topper3d092db2012-03-21 02:14:01 +00004716/// isShuffleMaskConsecutive - Check if the shuffle mask indicies [MaskI, MaskE)
4717/// correspond consecutively to elements from one of the vector operands,
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004718/// starting from its index OpIdx. Also tell OpNum which source vector operand.
4719static
Craig Topper3d092db2012-03-21 02:14:01 +00004720bool isShuffleMaskConsecutive(ShuffleVectorSDNode *SVOp,
4721 unsigned MaskI, unsigned MaskE, unsigned OpIdx,
4722 unsigned NumElems, unsigned &OpNum) {
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004723 bool SeenV1 = false;
4724 bool SeenV2 = false;
4725
Craig Topper3d092db2012-03-21 02:14:01 +00004726 for (unsigned i = MaskI; i != MaskE; ++i, ++OpIdx) {
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004727 int Idx = SVOp->getMaskElt(i);
4728 // Ignore undef indicies
4729 if (Idx < 0)
4730 continue;
4731
Craig Topper3d092db2012-03-21 02:14:01 +00004732 if (Idx < (int)NumElems)
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004733 SeenV1 = true;
4734 else
4735 SeenV2 = true;
4736
4737 // Only accept consecutive elements from the same vector
4738 if ((Idx % NumElems != OpIdx) || (SeenV1 && SeenV2))
4739 return false;
4740 }
4741
4742 OpNum = SeenV1 ? 0 : 1;
4743 return true;
4744}
4745
4746/// isVectorShiftRight - Returns true if the shuffle can be implemented as a
4747/// logical left shift of a vector.
4748static bool isVectorShiftRight(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
4749 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
4750 unsigned NumElems = SVOp->getValueType(0).getVectorNumElements();
4751 unsigned NumZeros = getNumOfConsecutiveZeros(SVOp, NumElems,
4752 false /* check zeros from right */, DAG);
4753 unsigned OpSrc;
4754
4755 if (!NumZeros)
4756 return false;
4757
4758 // Considering the elements in the mask that are not consecutive zeros,
4759 // check if they consecutively come from only one of the source vectors.
4760 //
4761 // V1 = {X, A, B, C} 0
4762 // \ \ \ /
4763 // vector_shuffle V1, V2 <1, 2, 3, X>
4764 //
4765 if (!isShuffleMaskConsecutive(SVOp,
4766 0, // Mask Start Index
Craig Topper3d092db2012-03-21 02:14:01 +00004767 NumElems-NumZeros, // Mask End Index(exclusive)
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004768 NumZeros, // Where to start looking in the src vector
4769 NumElems, // Number of elements in vector
4770 OpSrc)) // Which source operand ?
4771 return false;
4772
4773 isLeft = false;
4774 ShAmt = NumZeros;
4775 ShVal = SVOp->getOperand(OpSrc);
4776 return true;
4777}
4778
4779/// isVectorShiftLeft - Returns true if the shuffle can be implemented as a
4780/// logical left shift of a vector.
4781static bool isVectorShiftLeft(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
4782 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
4783 unsigned NumElems = SVOp->getValueType(0).getVectorNumElements();
4784 unsigned NumZeros = getNumOfConsecutiveZeros(SVOp, NumElems,
4785 true /* check zeros from left */, DAG);
4786 unsigned OpSrc;
4787
4788 if (!NumZeros)
4789 return false;
4790
4791 // Considering the elements in the mask that are not consecutive zeros,
4792 // check if they consecutively come from only one of the source vectors.
4793 //
4794 // 0 { A, B, X, X } = V2
4795 // / \ / /
4796 // vector_shuffle V1, V2 <X, X, 4, 5>
4797 //
4798 if (!isShuffleMaskConsecutive(SVOp,
4799 NumZeros, // Mask Start Index
Craig Topper3d092db2012-03-21 02:14:01 +00004800 NumElems, // Mask End Index(exclusive)
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004801 0, // Where to start looking in the src vector
4802 NumElems, // Number of elements in vector
4803 OpSrc)) // Which source operand ?
4804 return false;
4805
4806 isLeft = true;
4807 ShAmt = NumZeros;
4808 ShVal = SVOp->getOperand(OpSrc);
4809 return true;
Evan Chengf26ffe92008-05-29 08:22:04 +00004810}
4811
4812/// isVectorShift - Returns true if the shuffle can be implemented as a
4813/// logical left or right shift of a vector.
Nate Begeman9008ca62009-04-27 18:41:29 +00004814static bool isVectorShift(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
Dan Gohman475871a2008-07-27 21:46:04 +00004815 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00004816 // Although the logic below support any bitwidth size, there are no
4817 // shift instructions which handle more than 128-bit vectors.
Craig Topper7a9a28b2012-08-12 02:23:29 +00004818 if (!SVOp->getValueType(0).is128BitVector())
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00004819 return false;
4820
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004821 if (isVectorShiftLeft(SVOp, DAG, isLeft, ShVal, ShAmt) ||
4822 isVectorShiftRight(SVOp, DAG, isLeft, ShVal, ShAmt))
4823 return true;
Evan Chengf26ffe92008-05-29 08:22:04 +00004824
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004825 return false;
Evan Chengf26ffe92008-05-29 08:22:04 +00004826}
4827
Evan Chengc78d3b42006-04-24 18:01:45 +00004828/// LowerBuildVectorv16i8 - Custom lower build_vector of v16i8.
4829///
Dan Gohman475871a2008-07-27 21:46:04 +00004830static SDValue LowerBuildVectorv16i8(SDValue Op, unsigned NonZeros,
Evan Chengc78d3b42006-04-24 18:01:45 +00004831 unsigned NumNonZero, unsigned NumZero,
Dan Gohmand858e902010-04-17 15:26:15 +00004832 SelectionDAG &DAG,
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00004833 const X86Subtarget* Subtarget,
Dan Gohmand858e902010-04-17 15:26:15 +00004834 const TargetLowering &TLI) {
Evan Chengc78d3b42006-04-24 18:01:45 +00004835 if (NumNonZero > 8)
Dan Gohman475871a2008-07-27 21:46:04 +00004836 return SDValue();
Evan Chengc78d3b42006-04-24 18:01:45 +00004837
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004838 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00004839 SDValue V(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00004840 bool First = true;
4841 for (unsigned i = 0; i < 16; ++i) {
4842 bool ThisIsNonZero = (NonZeros & (1 << i)) != 0;
4843 if (ThisIsNonZero && First) {
4844 if (NumZero)
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00004845 V = getZeroVector(MVT::v8i16, Subtarget, DAG, dl);
Evan Chengc78d3b42006-04-24 18:01:45 +00004846 else
Owen Anderson825b72b2009-08-11 20:47:22 +00004847 V = DAG.getUNDEF(MVT::v8i16);
Evan Chengc78d3b42006-04-24 18:01:45 +00004848 First = false;
4849 }
4850
4851 if ((i & 1) != 0) {
Dan Gohman475871a2008-07-27 21:46:04 +00004852 SDValue ThisElt(0, 0), LastElt(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00004853 bool LastIsNonZero = (NonZeros & (1 << (i-1))) != 0;
4854 if (LastIsNonZero) {
Scott Michelfdc40a02009-02-17 22:15:04 +00004855 LastElt = DAG.getNode(ISD::ZERO_EXTEND, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004856 MVT::i16, Op.getOperand(i-1));
Evan Chengc78d3b42006-04-24 18:01:45 +00004857 }
4858 if (ThisIsNonZero) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004859 ThisElt = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i16, Op.getOperand(i));
4860 ThisElt = DAG.getNode(ISD::SHL, dl, MVT::i16,
4861 ThisElt, DAG.getConstant(8, MVT::i8));
Evan Chengc78d3b42006-04-24 18:01:45 +00004862 if (LastIsNonZero)
Owen Anderson825b72b2009-08-11 20:47:22 +00004863 ThisElt = DAG.getNode(ISD::OR, dl, MVT::i16, ThisElt, LastElt);
Evan Chengc78d3b42006-04-24 18:01:45 +00004864 } else
4865 ThisElt = LastElt;
4866
Gabor Greifba36cb52008-08-28 21:40:38 +00004867 if (ThisElt.getNode())
Owen Anderson825b72b2009-08-11 20:47:22 +00004868 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, V, ThisElt,
Chris Lattner0bd48932008-01-17 07:00:52 +00004869 DAG.getIntPtrConstant(i/2));
Evan Chengc78d3b42006-04-24 18:01:45 +00004870 }
4871 }
4872
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004873 return DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, V);
Evan Chengc78d3b42006-04-24 18:01:45 +00004874}
4875
Bill Wendlinga348c562007-03-22 18:42:45 +00004876/// LowerBuildVectorv8i16 - Custom lower build_vector of v8i16.
Evan Chengc78d3b42006-04-24 18:01:45 +00004877///
Dan Gohman475871a2008-07-27 21:46:04 +00004878static SDValue LowerBuildVectorv8i16(SDValue Op, unsigned NonZeros,
Dan Gohmand858e902010-04-17 15:26:15 +00004879 unsigned NumNonZero, unsigned NumZero,
4880 SelectionDAG &DAG,
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00004881 const X86Subtarget* Subtarget,
Dan Gohmand858e902010-04-17 15:26:15 +00004882 const TargetLowering &TLI) {
Evan Chengc78d3b42006-04-24 18:01:45 +00004883 if (NumNonZero > 4)
Dan Gohman475871a2008-07-27 21:46:04 +00004884 return SDValue();
Evan Chengc78d3b42006-04-24 18:01:45 +00004885
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004886 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00004887 SDValue V(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00004888 bool First = true;
4889 for (unsigned i = 0; i < 8; ++i) {
4890 bool isNonZero = (NonZeros & (1 << i)) != 0;
4891 if (isNonZero) {
4892 if (First) {
4893 if (NumZero)
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00004894 V = getZeroVector(MVT::v8i16, Subtarget, DAG, dl);
Evan Chengc78d3b42006-04-24 18:01:45 +00004895 else
Owen Anderson825b72b2009-08-11 20:47:22 +00004896 V = DAG.getUNDEF(MVT::v8i16);
Evan Chengc78d3b42006-04-24 18:01:45 +00004897 First = false;
4898 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004899 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004900 MVT::v8i16, V, Op.getOperand(i),
Chris Lattner0bd48932008-01-17 07:00:52 +00004901 DAG.getIntPtrConstant(i));
Evan Chengc78d3b42006-04-24 18:01:45 +00004902 }
4903 }
4904
4905 return V;
4906}
4907
Evan Chengf26ffe92008-05-29 08:22:04 +00004908/// getVShift - Return a vector logical shift node.
4909///
Owen Andersone50ed302009-08-10 22:56:29 +00004910static SDValue getVShift(bool isLeft, EVT VT, SDValue SrcOp,
Nate Begeman9008ca62009-04-27 18:41:29 +00004911 unsigned NumBits, SelectionDAG &DAG,
4912 const TargetLowering &TLI, DebugLoc dl) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00004913 assert(VT.is128BitVector() && "Unknown type for VShift");
Dale Johannesen0488fb62010-09-30 23:57:10 +00004914 EVT ShVT = MVT::v2i64;
Craig Toppered2e13d2012-01-22 19:15:14 +00004915 unsigned Opc = isLeft ? X86ISD::VSHLDQ : X86ISD::VSRLDQ;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004916 SrcOp = DAG.getNode(ISD::BITCAST, dl, ShVT, SrcOp);
4917 return DAG.getNode(ISD::BITCAST, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00004918 DAG.getNode(Opc, dl, ShVT, SrcOp,
Owen Anderson95771af2011-02-25 21:41:48 +00004919 DAG.getConstant(NumBits,
4920 TLI.getShiftAmountTy(SrcOp.getValueType()))));
Evan Chengf26ffe92008-05-29 08:22:04 +00004921}
4922
Dan Gohman475871a2008-07-27 21:46:04 +00004923SDValue
Evan Chengc3630942009-12-09 21:00:30 +00004924X86TargetLowering::LowerAsSplatVectorLoad(SDValue SrcOp, EVT VT, DebugLoc dl,
Dan Gohmand858e902010-04-17 15:26:15 +00004925 SelectionDAG &DAG) const {
Michael J. Spencerec38de22010-10-10 22:04:20 +00004926
Evan Chengc3630942009-12-09 21:00:30 +00004927 // Check if the scalar load can be widened into a vector load. And if
4928 // the address is "base + cst" see if the cst can be "absorbed" into
4929 // the shuffle mask.
4930 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(SrcOp)) {
4931 SDValue Ptr = LD->getBasePtr();
4932 if (!ISD::isNormalLoad(LD) || LD->isVolatile())
4933 return SDValue();
4934 EVT PVT = LD->getValueType(0);
4935 if (PVT != MVT::i32 && PVT != MVT::f32)
4936 return SDValue();
4937
4938 int FI = -1;
4939 int64_t Offset = 0;
4940 if (FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr)) {
4941 FI = FINode->getIndex();
4942 Offset = 0;
Chris Lattner0a9481f2011-02-13 22:25:43 +00004943 } else if (DAG.isBaseWithConstantOffset(Ptr) &&
Evan Chengc3630942009-12-09 21:00:30 +00004944 isa<FrameIndexSDNode>(Ptr.getOperand(0))) {
4945 FI = cast<FrameIndexSDNode>(Ptr.getOperand(0))->getIndex();
4946 Offset = Ptr.getConstantOperandVal(1);
4947 Ptr = Ptr.getOperand(0);
4948 } else {
4949 return SDValue();
4950 }
4951
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004952 // FIXME: 256-bit vector instructions don't require a strict alignment,
4953 // improve this code to support it better.
4954 unsigned RequiredAlign = VT.getSizeInBits()/8;
Evan Chengc3630942009-12-09 21:00:30 +00004955 SDValue Chain = LD->getChain();
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004956 // Make sure the stack object alignment is at least 16 or 32.
Evan Chengc3630942009-12-09 21:00:30 +00004957 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004958 if (DAG.InferPtrAlignment(Ptr) < RequiredAlign) {
Evan Chengc3630942009-12-09 21:00:30 +00004959 if (MFI->isFixedObjectIndex(FI)) {
Eric Christophere9625cf2010-01-23 06:02:43 +00004960 // Can't change the alignment. FIXME: It's possible to compute
4961 // the exact stack offset and reference FI + adjust offset instead.
4962 // If someone *really* cares about this. That's the way to implement it.
4963 return SDValue();
Evan Chengc3630942009-12-09 21:00:30 +00004964 } else {
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004965 MFI->setObjectAlignment(FI, RequiredAlign);
Evan Chengc3630942009-12-09 21:00:30 +00004966 }
4967 }
4968
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004969 // (Offset % 16 or 32) must be multiple of 4. Then address is then
Evan Chengc3630942009-12-09 21:00:30 +00004970 // Ptr + (Offset & ~15).
4971 if (Offset < 0)
4972 return SDValue();
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004973 if ((Offset % RequiredAlign) & 3)
Evan Chengc3630942009-12-09 21:00:30 +00004974 return SDValue();
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004975 int64_t StartOffset = Offset & ~(RequiredAlign-1);
Evan Chengc3630942009-12-09 21:00:30 +00004976 if (StartOffset)
4977 Ptr = DAG.getNode(ISD::ADD, Ptr.getDebugLoc(), Ptr.getValueType(),
4978 Ptr,DAG.getConstant(StartOffset, Ptr.getValueType()));
4979
4980 int EltNo = (Offset - StartOffset) >> 2;
Craig Topper66ddd152012-04-27 22:54:43 +00004981 unsigned NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004982
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004983 EVT NVT = EVT::getVectorVT(*DAG.getContext(), PVT, NumElems);
4984 SDValue V1 = DAG.getLoad(NVT, dl, Chain, Ptr,
Chris Lattner51abfe42010-09-21 06:02:19 +00004985 LD->getPointerInfo().getWithOffset(StartOffset),
Pete Cooperd752e0f2011-11-08 18:42:53 +00004986 false, false, false, 0);
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004987
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004988 SmallVector<int, 8> Mask;
Craig Topper66ddd152012-04-27 22:54:43 +00004989 for (unsigned i = 0; i != NumElems; ++i)
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004990 Mask.push_back(EltNo);
4991
Craig Toppercc3000632012-01-30 07:50:31 +00004992 return DAG.getVectorShuffle(NVT, dl, V1, DAG.getUNDEF(NVT), &Mask[0]);
Evan Chengc3630942009-12-09 21:00:30 +00004993 }
4994
4995 return SDValue();
4996}
4997
Michael J. Spencerec38de22010-10-10 22:04:20 +00004998/// EltsFromConsecutiveLoads - Given the initializing elements 'Elts' of a
4999/// vector of type 'VT', see if the elements can be replaced by a single large
Nate Begeman1449f292010-03-24 22:19:06 +00005000/// load which has the same value as a build_vector whose operands are 'elts'.
5001///
5002/// Example: <load i32 *a, load i32 *a+4, undef, undef> -> zextload a
Michael J. Spencerec38de22010-10-10 22:04:20 +00005003///
Nate Begeman1449f292010-03-24 22:19:06 +00005004/// FIXME: we'd also like to handle the case where the last elements are zero
5005/// rather than undef via VZEXT_LOAD, but we do not detect that case today.
5006/// There's even a handy isZeroNode for that purpose.
Nate Begemanfdea31a2010-03-24 20:49:50 +00005007static SDValue EltsFromConsecutiveLoads(EVT VT, SmallVectorImpl<SDValue> &Elts,
Chris Lattner88641552010-09-22 00:34:38 +00005008 DebugLoc &DL, SelectionDAG &DAG) {
Nate Begemanfdea31a2010-03-24 20:49:50 +00005009 EVT EltVT = VT.getVectorElementType();
5010 unsigned NumElems = Elts.size();
Michael J. Spencerec38de22010-10-10 22:04:20 +00005011
Nate Begemanfdea31a2010-03-24 20:49:50 +00005012 LoadSDNode *LDBase = NULL;
5013 unsigned LastLoadedElt = -1U;
Michael J. Spencerec38de22010-10-10 22:04:20 +00005014
Nate Begeman1449f292010-03-24 22:19:06 +00005015 // For each element in the initializer, see if we've found a load or an undef.
Michael J. Spencerec38de22010-10-10 22:04:20 +00005016 // If we don't find an initial load element, or later load elements are
Nate Begeman1449f292010-03-24 22:19:06 +00005017 // non-consecutive, bail out.
Nate Begemanfdea31a2010-03-24 20:49:50 +00005018 for (unsigned i = 0; i < NumElems; ++i) {
5019 SDValue Elt = Elts[i];
Michael J. Spencerec38de22010-10-10 22:04:20 +00005020
Nate Begemanfdea31a2010-03-24 20:49:50 +00005021 if (!Elt.getNode() ||
5022 (Elt.getOpcode() != ISD::UNDEF && !ISD::isNON_EXTLoad(Elt.getNode())))
5023 return SDValue();
5024 if (!LDBase) {
5025 if (Elt.getNode()->getOpcode() == ISD::UNDEF)
5026 return SDValue();
5027 LDBase = cast<LoadSDNode>(Elt.getNode());
5028 LastLoadedElt = i;
5029 continue;
5030 }
5031 if (Elt.getOpcode() == ISD::UNDEF)
5032 continue;
5033
5034 LoadSDNode *LD = cast<LoadSDNode>(Elt);
5035 if (!DAG.isConsecutiveLoad(LD, LDBase, EltVT.getSizeInBits()/8, i))
5036 return SDValue();
5037 LastLoadedElt = i;
5038 }
Nate Begeman1449f292010-03-24 22:19:06 +00005039
5040 // If we have found an entire vector of loads and undefs, then return a large
5041 // load of the entire vector width starting at the base pointer. If we found
5042 // consecutive loads for the low half, generate a vzext_load node.
Nate Begemanfdea31a2010-03-24 20:49:50 +00005043 if (LastLoadedElt == NumElems - 1) {
5044 if (DAG.InferPtrAlignment(LDBase->getBasePtr()) >= 16)
Chris Lattner88641552010-09-22 00:34:38 +00005045 return DAG.getLoad(VT, DL, LDBase->getChain(), LDBase->getBasePtr(),
Chris Lattner51abfe42010-09-21 06:02:19 +00005046 LDBase->getPointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00005047 LDBase->isVolatile(), LDBase->isNonTemporal(),
5048 LDBase->isInvariant(), 0);
Chris Lattner88641552010-09-22 00:34:38 +00005049 return DAG.getLoad(VT, DL, LDBase->getChain(), LDBase->getBasePtr(),
Chris Lattner51abfe42010-09-21 06:02:19 +00005050 LDBase->getPointerInfo(),
Nate Begemanfdea31a2010-03-24 20:49:50 +00005051 LDBase->isVolatile(), LDBase->isNonTemporal(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00005052 LDBase->isInvariant(), LDBase->getAlignment());
Craig Topper69947b92012-04-23 06:57:04 +00005053 }
5054 if (NumElems == 4 && LastLoadedElt == 1 &&
5055 DAG.getTargetLoweringInfo().isTypeLegal(MVT::v2i64)) {
Nate Begemanfdea31a2010-03-24 20:49:50 +00005056 SDVTList Tys = DAG.getVTList(MVT::v2i64, MVT::Other);
5057 SDValue Ops[] = { LDBase->getChain(), LDBase->getBasePtr() };
Eli Friedman322ea082011-09-14 23:42:45 +00005058 SDValue ResNode =
5059 DAG.getMemIntrinsicNode(X86ISD::VZEXT_LOAD, DL, Tys, Ops, 2, MVT::i64,
5060 LDBase->getPointerInfo(),
5061 LDBase->getAlignment(),
5062 false/*isVolatile*/, true/*ReadMem*/,
5063 false/*WriteMem*/);
Manman Ren2b7a2e82012-08-31 23:16:57 +00005064
5065 // Make sure the newly-created LOAD is in the same position as LDBase in
5066 // terms of dependency. We create a TokenFactor for LDBase and ResNode, and
5067 // update uses of LDBase's output chain to use the TokenFactor.
5068 if (LDBase->hasAnyUseOfValue(1)) {
5069 SDValue NewChain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other,
5070 SDValue(LDBase, 1), SDValue(ResNode.getNode(), 1));
5071 DAG.ReplaceAllUsesOfValueWith(SDValue(LDBase, 1), NewChain);
5072 DAG.UpdateNodeOperands(NewChain.getNode(), SDValue(LDBase, 1),
5073 SDValue(ResNode.getNode(), 1));
5074 }
5075
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005076 return DAG.getNode(ISD::BITCAST, DL, VT, ResNode);
Nate Begemanfdea31a2010-03-24 20:49:50 +00005077 }
5078 return SDValue();
5079}
5080
Nadav Rotem9d68b062012-04-08 12:54:54 +00005081/// LowerVectorBroadcast - Attempt to use the vbroadcast instruction
5082/// to generate a splat value for the following cases:
5083/// 1. A splat BUILD_VECTOR which uses a single scalar load, or a constant.
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005084/// 2. A splat shuffle which uses a scalar_to_vector node which comes from
Nadav Rotem9d68b062012-04-08 12:54:54 +00005085/// a scalar load, or a constant.
5086/// The VBROADCAST node is returned when a pattern is found,
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00005087/// or SDValue() otherwise.
Nadav Rotem154819d2012-04-09 07:45:58 +00005088SDValue
Craig Topper55b24052012-09-11 06:15:32 +00005089X86TargetLowering::LowerVectorBroadcast(SDValue Op, SelectionDAG &DAG) const {
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00005090 if (!Subtarget->hasFp256())
Craig Toppera9376332012-01-10 08:23:59 +00005091 return SDValue();
5092
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005093 EVT VT = Op.getValueType();
Nadav Rotem154819d2012-04-09 07:45:58 +00005094 DebugLoc dl = Op.getDebugLoc();
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005095
Craig Topper5da8a802012-05-04 05:49:51 +00005096 assert((VT.is128BitVector() || VT.is256BitVector()) &&
5097 "Unsupported vector type for broadcast.");
5098
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005099 SDValue Ld;
Nadav Rotem9d68b062012-04-08 12:54:54 +00005100 bool ConstSplatVal;
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005101
Nadav Rotem9d68b062012-04-08 12:54:54 +00005102 switch (Op.getOpcode()) {
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005103 default:
5104 // Unknown pattern found.
5105 return SDValue();
5106
5107 case ISD::BUILD_VECTOR: {
5108 // The BUILD_VECTOR node must be a splat.
Nadav Rotem9d68b062012-04-08 12:54:54 +00005109 if (!isSplatVector(Op.getNode()))
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005110 return SDValue();
5111
Nadav Rotem9d68b062012-04-08 12:54:54 +00005112 Ld = Op.getOperand(0);
5113 ConstSplatVal = (Ld.getOpcode() == ISD::Constant ||
5114 Ld.getOpcode() == ISD::ConstantFP);
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00005115
5116 // The suspected load node has several users. Make sure that all
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005117 // of its users are from the BUILD_VECTOR node.
Nadav Rotem9d68b062012-04-08 12:54:54 +00005118 // Constants may have multiple users.
5119 if (!ConstSplatVal && !Ld->hasNUsesOfValue(VT.getVectorNumElements(), 0))
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005120 return SDValue();
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00005121 break;
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005122 }
5123
5124 case ISD::VECTOR_SHUFFLE: {
5125 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
5126
5127 // Shuffles must have a splat mask where the first element is
5128 // broadcasted.
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00005129 if ((!SVOp->isSplat()) || SVOp->getMaskElt(0) != 0)
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005130 return SDValue();
5131
5132 SDValue Sc = Op.getOperand(0);
Nadav Rotemb88e8dd2012-05-10 12:50:02 +00005133 if (Sc.getOpcode() != ISD::SCALAR_TO_VECTOR &&
Elena Demikhovsky8f40f7b2012-07-01 06:12:26 +00005134 Sc.getOpcode() != ISD::BUILD_VECTOR) {
5135
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00005136 if (!Subtarget->hasInt256())
Elena Demikhovsky8f40f7b2012-07-01 06:12:26 +00005137 return SDValue();
5138
5139 // Use the register form of the broadcast instruction available on AVX2.
5140 if (VT.is256BitVector())
5141 Sc = Extract128BitVector(Sc, 0, DAG, dl);
5142 return DAG.getNode(X86ISD::VBROADCAST, dl, VT, Sc);
5143 }
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005144
5145 Ld = Sc.getOperand(0);
Nadav Rotem9d68b062012-04-08 12:54:54 +00005146 ConstSplatVal = (Ld.getOpcode() == ISD::Constant ||
Nadav Rotem154819d2012-04-09 07:45:58 +00005147 Ld.getOpcode() == ISD::ConstantFP);
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005148
5149 // The scalar_to_vector node and the suspected
5150 // load node must have exactly one user.
Nadav Rotem9d68b062012-04-08 12:54:54 +00005151 // Constants may have multiple users.
5152 if (!ConstSplatVal && (!Sc.hasOneUse() || !Ld.hasOneUse()))
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005153 return SDValue();
5154 break;
5155 }
5156 }
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00005157
Craig Topper7a9a28b2012-08-12 02:23:29 +00005158 bool Is256 = VT.is256BitVector();
Nadav Rotem9d68b062012-04-08 12:54:54 +00005159
5160 // Handle the broadcasting a single constant scalar from the constant pool
5161 // into a vector. On Sandybridge it is still better to load a constant vector
5162 // from the constant pool and not to broadcast it from a scalar.
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00005163 if (ConstSplatVal && Subtarget->hasInt256()) {
Nadav Rotem9d68b062012-04-08 12:54:54 +00005164 EVT CVT = Ld.getValueType();
5165 assert(!CVT.isVector() && "Must not broadcast a vector type");
5166 unsigned ScalarSize = CVT.getSizeInBits();
5167
Craig Topper5da8a802012-05-04 05:49:51 +00005168 if (ScalarSize == 32 || (Is256 && ScalarSize == 64)) {
Nadav Rotem9d68b062012-04-08 12:54:54 +00005169 const Constant *C = 0;
5170 if (ConstantSDNode *CI = dyn_cast<ConstantSDNode>(Ld))
5171 C = CI->getConstantIntValue();
5172 else if (ConstantFPSDNode *CF = dyn_cast<ConstantFPSDNode>(Ld))
5173 C = CF->getConstantFPValue();
5174
5175 assert(C && "Invalid constant type");
5176
Nadav Rotem154819d2012-04-09 07:45:58 +00005177 SDValue CP = DAG.getConstantPool(C, getPointerTy());
Nadav Rotem9d68b062012-04-08 12:54:54 +00005178 unsigned Alignment = cast<ConstantPoolSDNode>(CP)->getAlignment();
Nadav Rotem154819d2012-04-09 07:45:58 +00005179 Ld = DAG.getLoad(CVT, dl, DAG.getEntryNode(), CP,
Craig Topper6643d9c2012-05-04 06:18:33 +00005180 MachinePointerInfo::getConstantPool(),
5181 false, false, false, Alignment);
Nadav Rotem9d68b062012-04-08 12:54:54 +00005182
Nadav Rotem9d68b062012-04-08 12:54:54 +00005183 return DAG.getNode(X86ISD::VBROADCAST, dl, VT, Ld);
5184 }
5185 }
5186
Nadav Rotem4fc8a5d2012-05-19 19:57:37 +00005187 bool IsLoad = ISD::isNormalLoad(Ld.getNode());
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005188 unsigned ScalarSize = Ld.getValueType().getSizeInBits();
5189
Nadav Rotem4fc8a5d2012-05-19 19:57:37 +00005190 // Handle AVX2 in-register broadcasts.
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00005191 if (!IsLoad && Subtarget->hasInt256() &&
Nadav Rotem4fc8a5d2012-05-19 19:57:37 +00005192 (ScalarSize == 32 || (Is256 && ScalarSize == 64)))
5193 return DAG.getNode(X86ISD::VBROADCAST, dl, VT, Ld);
5194
5195 // The scalar source must be a normal load.
5196 if (!IsLoad)
5197 return SDValue();
5198
Craig Topper5da8a802012-05-04 05:49:51 +00005199 if (ScalarSize == 32 || (Is256 && ScalarSize == 64))
Nadav Rotem9d68b062012-04-08 12:54:54 +00005200 return DAG.getNode(X86ISD::VBROADCAST, dl, VT, Ld);
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005201
Craig Toppera9376332012-01-10 08:23:59 +00005202 // The integer check is needed for the 64-bit into 128-bit so it doesn't match
Craig Topper5da8a802012-05-04 05:49:51 +00005203 // double since there is no vbroadcastsd xmm
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00005204 if (Subtarget->hasInt256() && Ld.getValueType().isInteger()) {
Craig Topper5da8a802012-05-04 05:49:51 +00005205 if (ScalarSize == 8 || ScalarSize == 16 || ScalarSize == 64)
Nadav Rotem9d68b062012-04-08 12:54:54 +00005206 return DAG.getNode(X86ISD::VBROADCAST, dl, VT, Ld);
Craig Toppera9376332012-01-10 08:23:59 +00005207 }
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00005208
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005209 // Unsupported broadcast.
5210 return SDValue();
5211}
5212
Evan Chengc3630942009-12-09 21:00:30 +00005213SDValue
Michael Liaofacace82012-10-19 17:15:18 +00005214X86TargetLowering::buildFromShuffleMostly(SDValue Op, SelectionDAG &DAG) const {
5215 EVT VT = Op.getValueType();
5216
5217 // Skip if insert_vec_elt is not supported.
5218 if (!isOperationLegalOrCustom(ISD::INSERT_VECTOR_ELT, VT))
5219 return SDValue();
5220
5221 DebugLoc DL = Op.getDebugLoc();
5222 unsigned NumElems = Op.getNumOperands();
5223
5224 SDValue VecIn1;
5225 SDValue VecIn2;
5226 SmallVector<unsigned, 4> InsertIndices;
5227 SmallVector<int, 8> Mask(NumElems, -1);
5228
5229 for (unsigned i = 0; i != NumElems; ++i) {
5230 unsigned Opc = Op.getOperand(i).getOpcode();
5231
5232 if (Opc == ISD::UNDEF)
5233 continue;
5234
5235 if (Opc != ISD::EXTRACT_VECTOR_ELT) {
5236 // Quit if more than 1 elements need inserting.
5237 if (InsertIndices.size() > 1)
5238 return SDValue();
5239
5240 InsertIndices.push_back(i);
5241 continue;
5242 }
5243
5244 SDValue ExtractedFromVec = Op.getOperand(i).getOperand(0);
5245 SDValue ExtIdx = Op.getOperand(i).getOperand(1);
5246
5247 // Quit if extracted from vector of different type.
5248 if (ExtractedFromVec.getValueType() != VT)
5249 return SDValue();
5250
5251 // Quit if non-constant index.
5252 if (!isa<ConstantSDNode>(ExtIdx))
5253 return SDValue();
5254
5255 if (VecIn1.getNode() == 0)
5256 VecIn1 = ExtractedFromVec;
5257 else if (VecIn1 != ExtractedFromVec) {
5258 if (VecIn2.getNode() == 0)
5259 VecIn2 = ExtractedFromVec;
5260 else if (VecIn2 != ExtractedFromVec)
5261 // Quit if more than 2 vectors to shuffle
5262 return SDValue();
5263 }
5264
5265 unsigned Idx = cast<ConstantSDNode>(ExtIdx)->getZExtValue();
5266
5267 if (ExtractedFromVec == VecIn1)
5268 Mask[i] = Idx;
5269 else if (ExtractedFromVec == VecIn2)
5270 Mask[i] = Idx + NumElems;
5271 }
5272
5273 if (VecIn1.getNode() == 0)
5274 return SDValue();
5275
5276 VecIn2 = VecIn2.getNode() ? VecIn2 : DAG.getUNDEF(VT);
5277 SDValue NV = DAG.getVectorShuffle(VT, DL, VecIn1, VecIn2, &Mask[0]);
5278 for (unsigned i = 0, e = InsertIndices.size(); i != e; ++i) {
5279 unsigned Idx = InsertIndices[i];
5280 NV = DAG.getNode(ISD::INSERT_VECTOR_ELT, DL, VT, NV, Op.getOperand(Idx),
5281 DAG.getIntPtrConstant(Idx));
5282 }
5283
5284 return NV;
5285}
5286
5287SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00005288X86TargetLowering::LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005289 DebugLoc dl = Op.getDebugLoc();
David Greenea5f26012011-02-07 19:36:54 +00005290
David Greenef125a292011-02-08 19:04:41 +00005291 EVT VT = Op.getValueType();
5292 EVT ExtVT = VT.getVectorElementType();
David Greenef125a292011-02-08 19:04:41 +00005293 unsigned NumElems = Op.getNumOperands();
5294
Bruno Cardoso Lopes531f19f2011-08-01 19:51:53 +00005295 // Vectors containing all zeros can be matched by pxor and xorps later
5296 if (ISD::isBuildVectorAllZeros(Op.getNode())) {
5297 // Canonicalize this to <4 x i32> to 1) ensure the zero vectors are CSE'd
5298 // and 2) ensure that i64 scalars are eliminated on x86-32 hosts.
Craig Topper07a27622012-01-22 03:07:48 +00005299 if (VT == MVT::v4i32 || VT == MVT::v8i32)
Chris Lattner8a594482007-11-25 00:24:49 +00005300 return Op;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005301
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00005302 return getZeroVector(VT, Subtarget, DAG, dl);
Chris Lattner8a594482007-11-25 00:24:49 +00005303 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00005304
Bruno Cardoso Lopes531f19f2011-08-01 19:51:53 +00005305 // Vectors containing all ones can be matched by pcmpeqd on 128-bit width
Craig Topper745a86b2011-11-19 22:34:59 +00005306 // vectors or broken into v4i32 operations on 256-bit vectors. AVX2 can use
5307 // vpcmpeqd on 256-bit vectors.
Bruno Cardoso Lopes531f19f2011-08-01 19:51:53 +00005308 if (ISD::isBuildVectorAllOnes(Op.getNode())) {
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00005309 if (VT == MVT::v4i32 || (VT == MVT::v8i32 && Subtarget->hasInt256()))
Bruno Cardoso Lopes531f19f2011-08-01 19:51:53 +00005310 return Op;
5311
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00005312 return getOnesVector(VT, Subtarget->hasInt256(), DAG, dl);
Bruno Cardoso Lopes531f19f2011-08-01 19:51:53 +00005313 }
5314
Nadav Rotem154819d2012-04-09 07:45:58 +00005315 SDValue Broadcast = LowerVectorBroadcast(Op, DAG);
Nadav Rotem9d68b062012-04-08 12:54:54 +00005316 if (Broadcast.getNode())
5317 return Broadcast;
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005318
Owen Andersone50ed302009-08-10 22:56:29 +00005319 unsigned EVTBits = ExtVT.getSizeInBits();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005320
Evan Cheng0db9fe62006-04-25 20:13:52 +00005321 unsigned NumZero = 0;
5322 unsigned NumNonZero = 0;
5323 unsigned NonZeros = 0;
Chris Lattnerc9517fb2008-03-08 22:48:29 +00005324 bool IsAllConstants = true;
Dan Gohman475871a2008-07-27 21:46:04 +00005325 SmallSet<SDValue, 8> Values;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005326 for (unsigned i = 0; i < NumElems; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00005327 SDValue Elt = Op.getOperand(i);
Evan Chengdb2d5242007-12-12 06:45:40 +00005328 if (Elt.getOpcode() == ISD::UNDEF)
5329 continue;
5330 Values.insert(Elt);
5331 if (Elt.getOpcode() != ISD::Constant &&
5332 Elt.getOpcode() != ISD::ConstantFP)
Chris Lattnerc9517fb2008-03-08 22:48:29 +00005333 IsAllConstants = false;
Evan Cheng37b73872009-07-30 08:33:02 +00005334 if (X86::isZeroNode(Elt))
Evan Chengdb2d5242007-12-12 06:45:40 +00005335 NumZero++;
5336 else {
5337 NonZeros |= (1 << i);
5338 NumNonZero++;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005339 }
5340 }
5341
Chris Lattner97a2a562010-08-26 05:24:29 +00005342 // All undef vector. Return an UNDEF. All zero vectors were handled above.
5343 if (NumNonZero == 0)
Dale Johannesene8d72302009-02-06 23:05:02 +00005344 return DAG.getUNDEF(VT);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005345
Chris Lattner67f453a2008-03-09 05:42:06 +00005346 // Special case for single non-zero, non-undef, element.
Eli Friedman10415532009-06-06 06:05:10 +00005347 if (NumNonZero == 1) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00005348 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dan Gohman475871a2008-07-27 21:46:04 +00005349 SDValue Item = Op.getOperand(Idx);
Scott Michelfdc40a02009-02-17 22:15:04 +00005350
Chris Lattner62098042008-03-09 01:05:04 +00005351 // If this is an insertion of an i64 value on x86-32, and if the top bits of
5352 // the value are obviously zero, truncate the value to i32 and do the
5353 // insertion that way. Only do this if the value is non-constant or if the
5354 // value is a constant being inserted into element 0. It is cheaper to do
5355 // a constant pool load than it is to do a movd + shuffle.
Owen Anderson825b72b2009-08-11 20:47:22 +00005356 if (ExtVT == MVT::i64 && !Subtarget->is64Bit() &&
Chris Lattner62098042008-03-09 01:05:04 +00005357 (!IsAllConstants || Idx == 0)) {
5358 if (DAG.MaskedValueIsZero(Item, APInt::getBitsSet(64, 32, 64))) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00005359 // Handle SSE only.
5360 assert(VT == MVT::v2i64 && "Expected an SSE value type!");
5361 EVT VecVT = MVT::v4i32;
5362 unsigned VecElts = 4;
Scott Michelfdc40a02009-02-17 22:15:04 +00005363
Chris Lattner62098042008-03-09 01:05:04 +00005364 // Truncate the value (which may itself be a constant) to i32, and
5365 // convert it to a vector with movd (S2V+shuffle to zero extend).
Owen Anderson825b72b2009-08-11 20:47:22 +00005366 Item = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Item);
Dale Johannesenace16102009-02-03 19:33:06 +00005367 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VecVT, Item);
Craig Topper12216172012-01-13 08:12:35 +00005368 Item = getShuffleVectorZeroOrUndef(Item, 0, true, Subtarget, DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +00005369
Chris Lattner62098042008-03-09 01:05:04 +00005370 // Now we have our 32-bit value zero extended in the low element of
5371 // a vector. If Idx != 0, swizzle it into place.
5372 if (Idx != 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005373 SmallVector<int, 4> Mask;
5374 Mask.push_back(Idx);
5375 for (unsigned i = 1; i != VecElts; ++i)
5376 Mask.push_back(i);
Craig Topperdf966f62012-04-22 19:17:57 +00005377 Item = DAG.getVectorShuffle(VecVT, dl, Item, DAG.getUNDEF(VecVT),
Nate Begeman9008ca62009-04-27 18:41:29 +00005378 &Mask[0]);
Chris Lattner62098042008-03-09 01:05:04 +00005379 }
Craig Topper07a27622012-01-22 03:07:48 +00005380 return DAG.getNode(ISD::BITCAST, dl, VT, Item);
Chris Lattner62098042008-03-09 01:05:04 +00005381 }
5382 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005383
Chris Lattner19f79692008-03-08 22:59:52 +00005384 // If we have a constant or non-constant insertion into the low element of
5385 // a vector, we can do this with SCALAR_TO_VECTOR + shuffle of zero into
5386 // the rest of the elements. This will be matched as movd/movq/movss/movsd
Eli Friedman10415532009-06-06 06:05:10 +00005387 // depending on what the source datatype is.
5388 if (Idx == 0) {
Craig Topperd62c16e2011-12-29 03:20:51 +00005389 if (NumZero == 0)
Eli Friedman10415532009-06-06 06:05:10 +00005390 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
Craig Topperd62c16e2011-12-29 03:20:51 +00005391
5392 if (ExtVT == MVT::i32 || ExtVT == MVT::f32 || ExtVT == MVT::f64 ||
Owen Anderson825b72b2009-08-11 20:47:22 +00005393 (ExtVT == MVT::i64 && Subtarget->is64Bit())) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00005394 if (VT.is256BitVector()) {
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00005395 SDValue ZeroVec = getZeroVector(VT, Subtarget, DAG, dl);
Nadav Rotem394a1f52012-01-11 14:07:51 +00005396 return DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, ZeroVec,
5397 Item, DAG.getIntPtrConstant(0));
Elena Demikhovsky021c0a22011-12-28 08:14:01 +00005398 }
Craig Topper7a9a28b2012-08-12 02:23:29 +00005399 assert(VT.is128BitVector() && "Expected an SSE value type!");
Eli Friedman10415532009-06-06 06:05:10 +00005400 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
5401 // Turn it into a MOVL (i.e. movss, movsd, or movd) to a zero vector.
Craig Topper12216172012-01-13 08:12:35 +00005402 return getShuffleVectorZeroOrUndef(Item, 0, true, Subtarget, DAG);
Craig Topperd62c16e2011-12-29 03:20:51 +00005403 }
5404
5405 if (ExtVT == MVT::i16 || ExtVT == MVT::i8) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005406 Item = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, Item);
Craig Topper3224e6b2011-12-29 03:09:33 +00005407 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32, Item);
Craig Topper7a9a28b2012-08-12 02:23:29 +00005408 if (VT.is256BitVector()) {
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00005409 SDValue ZeroVec = getZeroVector(MVT::v8i32, Subtarget, DAG, dl);
Craig Topperb14940a2012-04-22 20:55:18 +00005410 Item = Insert128BitVector(ZeroVec, Item, 0, DAG, dl);
Craig Topper19ec2a92011-12-29 03:34:54 +00005411 } else {
Craig Topper7a9a28b2012-08-12 02:23:29 +00005412 assert(VT.is128BitVector() && "Expected an SSE value type!");
Craig Topper12216172012-01-13 08:12:35 +00005413 Item = getShuffleVectorZeroOrUndef(Item, 0, true, Subtarget, DAG);
Craig Topper19ec2a92011-12-29 03:34:54 +00005414 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005415 return DAG.getNode(ISD::BITCAST, dl, VT, Item);
Eli Friedman10415532009-06-06 06:05:10 +00005416 }
Chris Lattnerc9517fb2008-03-08 22:48:29 +00005417 }
Evan Chengf26ffe92008-05-29 08:22:04 +00005418
5419 // Is it a vector logical left shift?
5420 if (NumElems == 2 && Idx == 1 &&
Evan Cheng37b73872009-07-30 08:33:02 +00005421 X86::isZeroNode(Op.getOperand(0)) &&
5422 !X86::isZeroNode(Op.getOperand(1))) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00005423 unsigned NumBits = VT.getSizeInBits();
Evan Chengf26ffe92008-05-29 08:22:04 +00005424 return getVShift(true, VT,
Scott Michelfdc40a02009-02-17 22:15:04 +00005425 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Dale Johannesenb300d2a2009-02-07 00:55:49 +00005426 VT, Op.getOperand(1)),
Dale Johannesenace16102009-02-03 19:33:06 +00005427 NumBits/2, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00005428 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005429
Chris Lattnerc9517fb2008-03-08 22:48:29 +00005430 if (IsAllConstants) // Otherwise, it's better to do a constpool load.
Dan Gohman475871a2008-07-27 21:46:04 +00005431 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005432
Chris Lattner19f79692008-03-08 22:59:52 +00005433 // Otherwise, if this is a vector with i32 or f32 elements, and the element
5434 // is a non-constant being inserted into an element other than the low one,
5435 // we can't use a constant pool load. Instead, use SCALAR_TO_VECTOR (aka
5436 // movd/movss) to move this into the low element, then shuffle it into
5437 // place.
Evan Cheng0db9fe62006-04-25 20:13:52 +00005438 if (EVTBits == 32) {
Dale Johannesenace16102009-02-03 19:33:06 +00005439 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
Scott Michelfdc40a02009-02-17 22:15:04 +00005440
Evan Cheng0db9fe62006-04-25 20:13:52 +00005441 // Turn it into a shuffle of zero and zero-extended scalar to vector.
Craig Topper12216172012-01-13 08:12:35 +00005442 Item = getShuffleVectorZeroOrUndef(Item, 0, NumZero > 0, Subtarget, DAG);
Nate Begeman9008ca62009-04-27 18:41:29 +00005443 SmallVector<int, 8> MaskVec;
Craig Topper31a207a2012-05-04 06:39:13 +00005444 for (unsigned i = 0; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00005445 MaskVec.push_back(i == Idx ? 0 : 1);
5446 return DAG.getVectorShuffle(VT, dl, Item, DAG.getUNDEF(VT), &MaskVec[0]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005447 }
5448 }
5449
Chris Lattner67f453a2008-03-09 05:42:06 +00005450 // Splat is obviously ok. Let legalizer expand it to a shuffle.
Evan Chengc3630942009-12-09 21:00:30 +00005451 if (Values.size() == 1) {
5452 if (EVTBits == 32) {
5453 // Instead of a shuffle like this:
5454 // shuffle (scalar_to_vector (load (ptr + 4))), undef, <0, 0, 0, 0>
5455 // Check if it's possible to issue this instead.
5456 // shuffle (vload ptr)), undef, <1, 1, 1, 1>
5457 unsigned Idx = CountTrailingZeros_32(NonZeros);
5458 SDValue Item = Op.getOperand(Idx);
5459 if (Op.getNode()->isOnlyUserOf(Item.getNode()))
5460 return LowerAsSplatVectorLoad(Item, VT, dl, DAG);
5461 }
Dan Gohman475871a2008-07-27 21:46:04 +00005462 return SDValue();
Evan Chengc3630942009-12-09 21:00:30 +00005463 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005464
Dan Gohmana3941172007-07-24 22:55:08 +00005465 // A vector full of immediates; various special cases are already
5466 // handled, so this is best done with a single constant-pool load.
Chris Lattnerc9517fb2008-03-08 22:48:29 +00005467 if (IsAllConstants)
Dan Gohman475871a2008-07-27 21:46:04 +00005468 return SDValue();
Dan Gohmana3941172007-07-24 22:55:08 +00005469
Bruno Cardoso Lopes6683efb2011-07-22 00:15:07 +00005470 // For AVX-length vectors, build the individual 128-bit pieces and use
5471 // shuffles to put them in place.
Craig Topper7a9a28b2012-08-12 02:23:29 +00005472 if (VT.is256BitVector()) {
Bruno Cardoso Lopes6683efb2011-07-22 00:15:07 +00005473 SmallVector<SDValue, 32> V;
Craig Topperfa5b70e2012-02-03 06:32:21 +00005474 for (unsigned i = 0; i != NumElems; ++i)
Bruno Cardoso Lopes6683efb2011-07-22 00:15:07 +00005475 V.push_back(Op.getOperand(i));
5476
5477 EVT HVT = EVT::getVectorVT(*DAG.getContext(), ExtVT, NumElems/2);
5478
5479 // Build both the lower and upper subvector.
5480 SDValue Lower = DAG.getNode(ISD::BUILD_VECTOR, dl, HVT, &V[0], NumElems/2);
5481 SDValue Upper = DAG.getNode(ISD::BUILD_VECTOR, dl, HVT, &V[NumElems / 2],
5482 NumElems/2);
5483
5484 // Recreate the wider vector with the lower and upper part.
Craig Topper4c7972d2012-04-22 18:15:59 +00005485 return Concat128BitVectors(Lower, Upper, VT, NumElems, DAG, dl);
Bruno Cardoso Lopes6683efb2011-07-22 00:15:07 +00005486 }
5487
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00005488 // Let legalizer expand 2-wide build_vectors.
Evan Cheng7e2ff772008-05-08 00:57:18 +00005489 if (EVTBits == 64) {
5490 if (NumNonZero == 1) {
5491 // One half is zero or undef.
5492 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dale Johannesenace16102009-02-03 19:33:06 +00005493 SDValue V2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT,
Evan Cheng7e2ff772008-05-08 00:57:18 +00005494 Op.getOperand(Idx));
Craig Topper12216172012-01-13 08:12:35 +00005495 return getShuffleVectorZeroOrUndef(V2, Idx, true, Subtarget, DAG);
Evan Cheng7e2ff772008-05-08 00:57:18 +00005496 }
Dan Gohman475871a2008-07-27 21:46:04 +00005497 return SDValue();
Evan Cheng7e2ff772008-05-08 00:57:18 +00005498 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00005499
5500 // If element VT is < 32 bits, convert it to inserts into a zero vector.
Bill Wendling826f36f2007-03-28 00:57:11 +00005501 if (EVTBits == 8 && NumElems == 16) {
Dan Gohman475871a2008-07-27 21:46:04 +00005502 SDValue V = LowerBuildVectorv16i8(Op, NonZeros,NumNonZero,NumZero, DAG,
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00005503 Subtarget, *this);
Gabor Greifba36cb52008-08-28 21:40:38 +00005504 if (V.getNode()) return V;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005505 }
5506
Bill Wendling826f36f2007-03-28 00:57:11 +00005507 if (EVTBits == 16 && NumElems == 8) {
Dan Gohman475871a2008-07-27 21:46:04 +00005508 SDValue V = LowerBuildVectorv8i16(Op, NonZeros,NumNonZero,NumZero, DAG,
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00005509 Subtarget, *this);
Gabor Greifba36cb52008-08-28 21:40:38 +00005510 if (V.getNode()) return V;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005511 }
5512
5513 // If element VT is == 32 bits, turn it into a number of shuffles.
Benjamin Kramer9c683542012-01-30 15:16:21 +00005514 SmallVector<SDValue, 8> V(NumElems);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005515 if (NumElems == 4 && NumZero > 0) {
5516 for (unsigned i = 0; i < 4; ++i) {
5517 bool isZero = !(NonZeros & (1 << i));
5518 if (isZero)
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00005519 V[i] = getZeroVector(VT, Subtarget, DAG, dl);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005520 else
Dale Johannesenace16102009-02-03 19:33:06 +00005521 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(i));
Evan Cheng0db9fe62006-04-25 20:13:52 +00005522 }
5523
5524 for (unsigned i = 0; i < 2; ++i) {
5525 switch ((NonZeros & (0x3 << i*2)) >> (i*2)) {
5526 default: break;
5527 case 0:
5528 V[i] = V[i*2]; // Must be a zero vector.
5529 break;
5530 case 1:
Nate Begeman9008ca62009-04-27 18:41:29 +00005531 V[i] = getMOVL(DAG, dl, VT, V[i*2+1], V[i*2]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005532 break;
5533 case 2:
Nate Begeman9008ca62009-04-27 18:41:29 +00005534 V[i] = getMOVL(DAG, dl, VT, V[i*2], V[i*2+1]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005535 break;
5536 case 3:
Nate Begeman9008ca62009-04-27 18:41:29 +00005537 V[i] = getUnpackl(DAG, dl, VT, V[i*2], V[i*2+1]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005538 break;
5539 }
5540 }
5541
Benjamin Kramer9c683542012-01-30 15:16:21 +00005542 bool Reverse1 = (NonZeros & 0x3) == 2;
5543 bool Reverse2 = ((NonZeros & (0x3 << 2)) >> 2) == 2;
5544 int MaskVec[] = {
5545 Reverse1 ? 1 : 0,
5546 Reverse1 ? 0 : 1,
Benjamin Kramer630ecf02012-01-30 20:01:35 +00005547 static_cast<int>(Reverse2 ? NumElems+1 : NumElems),
5548 static_cast<int>(Reverse2 ? NumElems : NumElems+1)
Benjamin Kramer9c683542012-01-30 15:16:21 +00005549 };
Nate Begeman9008ca62009-04-27 18:41:29 +00005550 return DAG.getVectorShuffle(VT, dl, V[0], V[1], &MaskVec[0]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005551 }
5552
Craig Topper7a9a28b2012-08-12 02:23:29 +00005553 if (Values.size() > 1 && VT.is128BitVector()) {
Nate Begemanfdea31a2010-03-24 20:49:50 +00005554 // Check for a build vector of consecutive loads.
5555 for (unsigned i = 0; i < NumElems; ++i)
5556 V[i] = Op.getOperand(i);
Michael J. Spencerec38de22010-10-10 22:04:20 +00005557
Nate Begemanfdea31a2010-03-24 20:49:50 +00005558 // Check for elements which are consecutive loads.
5559 SDValue LD = EltsFromConsecutiveLoads(VT, V, dl, DAG);
5560 if (LD.getNode())
5561 return LD;
Michael J. Spencerec38de22010-10-10 22:04:20 +00005562
Michael Liaofacace82012-10-19 17:15:18 +00005563 // Check for a build vector from mostly shuffle plus few inserting.
5564 SDValue Sh = buildFromShuffleMostly(Op, DAG);
5565 if (Sh.getNode())
5566 return Sh;
5567
Michael J. Spencerec38de22010-10-10 22:04:20 +00005568 // For SSE 4.1, use insertps to put the high elements into the low element.
Craig Topperd0a31172012-01-10 06:37:29 +00005569 if (getSubtarget()->hasSSE41()) {
Chris Lattner24faf612010-08-28 17:59:08 +00005570 SDValue Result;
5571 if (Op.getOperand(0).getOpcode() != ISD::UNDEF)
5572 Result = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(0));
5573 else
5574 Result = DAG.getUNDEF(VT);
Michael J. Spencerec38de22010-10-10 22:04:20 +00005575
Chris Lattner24faf612010-08-28 17:59:08 +00005576 for (unsigned i = 1; i < NumElems; ++i) {
5577 if (Op.getOperand(i).getOpcode() == ISD::UNDEF) continue;
5578 Result = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, Result,
Nate Begeman9008ca62009-04-27 18:41:29 +00005579 Op.getOperand(i), DAG.getIntPtrConstant(i));
Chris Lattner24faf612010-08-28 17:59:08 +00005580 }
5581 return Result;
Nate Begeman9008ca62009-04-27 18:41:29 +00005582 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00005583
Chris Lattner6e80e442010-08-28 17:15:43 +00005584 // Otherwise, expand into a number of unpckl*, start by extending each of
5585 // our (non-undef) elements to the full vector width with the element in the
5586 // bottom slot of the vector (which generates no code for SSE).
5587 for (unsigned i = 0; i < NumElems; ++i) {
5588 if (Op.getOperand(i).getOpcode() != ISD::UNDEF)
5589 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(i));
5590 else
5591 V[i] = DAG.getUNDEF(VT);
5592 }
5593
5594 // Next, we iteratively mix elements, e.g. for v4f32:
Evan Cheng0db9fe62006-04-25 20:13:52 +00005595 // Step 1: unpcklps 0, 2 ==> X: <?, ?, 2, 0>
5596 // : unpcklps 1, 3 ==> Y: <?, ?, 3, 1>
5597 // Step 2: unpcklps X, Y ==> <3, 2, 1, 0>
Chris Lattner6e80e442010-08-28 17:15:43 +00005598 unsigned EltStride = NumElems >> 1;
5599 while (EltStride != 0) {
Chris Lattner3ddcc432010-08-28 17:28:30 +00005600 for (unsigned i = 0; i < EltStride; ++i) {
5601 // If V[i+EltStride] is undef and this is the first round of mixing,
5602 // then it is safe to just drop this shuffle: V[i] is already in the
5603 // right place, the one element (since it's the first round) being
5604 // inserted as undef can be dropped. This isn't safe for successive
5605 // rounds because they will permute elements within both vectors.
5606 if (V[i+EltStride].getOpcode() == ISD::UNDEF &&
5607 EltStride == NumElems/2)
5608 continue;
Michael J. Spencerec38de22010-10-10 22:04:20 +00005609
Chris Lattner6e80e442010-08-28 17:15:43 +00005610 V[i] = getUnpackl(DAG, dl, VT, V[i], V[i + EltStride]);
Chris Lattner3ddcc432010-08-28 17:28:30 +00005611 }
Chris Lattner6e80e442010-08-28 17:15:43 +00005612 EltStride >>= 1;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005613 }
5614 return V[0];
5615 }
Dan Gohman475871a2008-07-27 21:46:04 +00005616 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005617}
5618
Bruno Cardoso Lopes8af24512011-08-01 21:54:02 +00005619// LowerAVXCONCAT_VECTORS - 256-bit AVX can use the vinsertf128 instruction
5620// to create 256-bit vectors from two other 128-bit ones.
5621static SDValue LowerAVXCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) {
5622 DebugLoc dl = Op.getDebugLoc();
5623 EVT ResVT = Op.getValueType();
5624
Craig Topper7a9a28b2012-08-12 02:23:29 +00005625 assert(ResVT.is256BitVector() && "Value type must be 256-bit wide");
Bruno Cardoso Lopes8af24512011-08-01 21:54:02 +00005626
5627 SDValue V1 = Op.getOperand(0);
5628 SDValue V2 = Op.getOperand(1);
5629 unsigned NumElems = ResVT.getVectorNumElements();
5630
Craig Topper4c7972d2012-04-22 18:15:59 +00005631 return Concat128BitVectors(V1, V2, ResVT, NumElems, DAG, dl);
Bruno Cardoso Lopes8af24512011-08-01 21:54:02 +00005632}
5633
Craig Topper55b24052012-09-11 06:15:32 +00005634static SDValue LowerCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) {
Bruno Cardoso Lopes8af24512011-08-01 21:54:02 +00005635 assert(Op.getNumOperands() == 2);
Bruno Cardoso Lopes8af24512011-08-01 21:54:02 +00005636
5637 // 256-bit AVX can use the vinsertf128 instruction to create 256-bit vectors
5638 // from two other 128-bit ones.
5639 return LowerAVXCONCAT_VECTORS(Op, DAG);
5640}
5641
Nadav Roteme80aa7c2012-04-09 08:33:21 +00005642// Try to lower a shuffle node into a simple blend instruction.
Craig Topper55b24052012-09-11 06:15:32 +00005643static SDValue
5644LowerVECTOR_SHUFFLEtoBlend(ShuffleVectorSDNode *SVOp,
5645 const X86Subtarget *Subtarget, SelectionDAG &DAG) {
Nadav Roteme80aa7c2012-04-09 08:33:21 +00005646 SDValue V1 = SVOp->getOperand(0);
5647 SDValue V2 = SVOp->getOperand(1);
5648 DebugLoc dl = SVOp->getDebugLoc();
Elena Demikhovsky226e0e62012-12-05 09:24:57 +00005649 EVT VT = SVOp->getValueType(0);
5650 EVT EltVT = VT.getVectorElementType();
Craig Topper1842ba02012-04-23 06:38:28 +00005651 unsigned NumElems = VT.getVectorNumElements();
Nadav Roteme80aa7c2012-04-09 08:33:21 +00005652
Elena Demikhovsky226e0e62012-12-05 09:24:57 +00005653 if (!Subtarget->hasSSE41() || EltVT == MVT::i8)
5654 return SDValue();
5655 if (!Subtarget->hasInt256() && VT == MVT::v16i16)
Nadav Roteme80aa7c2012-04-09 08:33:21 +00005656 return SDValue();
5657
Elena Demikhovsky226e0e62012-12-05 09:24:57 +00005658 // Check the mask for BLEND and build the value.
5659 unsigned MaskValue = 0;
5660 // There are 2 lanes if (NumElems > 8), and 1 lane otherwise.
5661 unsigned NumLanes = (NumElems-1)/8 + 1;
5662 unsigned NumElemsInLane = NumElems / NumLanes;
Nadav Roteme6113782012-04-11 06:40:27 +00005663
Elena Demikhovsky226e0e62012-12-05 09:24:57 +00005664 // Blend for v16i16 should be symetric for the both lanes.
5665 for (unsigned i = 0; i < NumElemsInLane; ++i) {
Nadav Roteme6113782012-04-11 06:40:27 +00005666
Elena Demikhovsky226e0e62012-12-05 09:24:57 +00005667 int SndLaneEltIdx = (NumLanes == 2) ?
5668 SVOp->getMaskElt(i + NumElemsInLane) : -1;
Nadav Roteme80aa7c2012-04-09 08:33:21 +00005669 int EltIdx = SVOp->getMaskElt(i);
Elena Demikhovsky226e0e62012-12-05 09:24:57 +00005670
5671 if ((EltIdx == -1 || EltIdx == (int)i) &&
5672 (SndLaneEltIdx == -1 || SndLaneEltIdx == (int)(i + NumElemsInLane)))
5673 continue;
5674
5675 if (((unsigned)EltIdx == (i + NumElems)) &&
5676 (SndLaneEltIdx == -1 ||
5677 (unsigned)SndLaneEltIdx == i + NumElems + NumElemsInLane))
5678 MaskValue |= (1<<i);
5679 else
Craig Topper1842ba02012-04-23 06:38:28 +00005680 return SDValue();
Nadav Roteme80aa7c2012-04-09 08:33:21 +00005681 }
5682
Elena Demikhovsky226e0e62012-12-05 09:24:57 +00005683 // Convert i32 vectors to floating point if it is not AVX2.
5684 // AVX2 introduced VPBLENDD instruction for 128 and 256-bit vectors.
5685 EVT BlendVT = VT;
5686 if (EltVT == MVT::i64 || (EltVT == MVT::i32 && !Subtarget->hasInt256())) {
5687 BlendVT = EVT::getVectorVT(*DAG.getContext(),
5688 EVT::getFloatingPointVT(EltVT.getSizeInBits()),
5689 NumElems);
5690 V1 = DAG.getNode(ISD::BITCAST, dl, VT, V1);
5691 V2 = DAG.getNode(ISD::BITCAST, dl, VT, V2);
5692 }
5693
5694 SDValue Ret = DAG.getNode(X86ISD::BLENDI, dl, BlendVT, V1, V2,
5695 DAG.getConstant(MaskValue, MVT::i32));
Nadav Roteme6113782012-04-11 06:40:27 +00005696 return DAG.getNode(ISD::BITCAST, dl, VT, Ret);
Nadav Roteme80aa7c2012-04-09 08:33:21 +00005697}
5698
Nate Begemanb9a47b82009-02-23 08:49:38 +00005699// v8i16 shuffles - Prefer shuffles in the following order:
5700// 1. [all] pshuflw, pshufhw, optional move
5701// 2. [ssse3] 1 x pshufb
5702// 3. [ssse3] 2 x pshufb + 1 x por
5703// 4. [all] mov + pshuflw + pshufhw + N x (pextrw + pinsrw)
Craig Topper55b24052012-09-11 06:15:32 +00005704static SDValue
5705LowerVECTOR_SHUFFLEv8i16(SDValue Op, const X86Subtarget *Subtarget,
5706 SelectionDAG &DAG) {
Bruno Cardoso Lopesbf8154a2010-08-21 01:32:18 +00005707 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Nate Begeman9008ca62009-04-27 18:41:29 +00005708 SDValue V1 = SVOp->getOperand(0);
5709 SDValue V2 = SVOp->getOperand(1);
5710 DebugLoc dl = SVOp->getDebugLoc();
Nate Begemanb9a47b82009-02-23 08:49:38 +00005711 SmallVector<int, 8> MaskVals;
Evan Cheng14b32e12007-12-11 01:46:18 +00005712
Nate Begemanb9a47b82009-02-23 08:49:38 +00005713 // Determine if more than 1 of the words in each of the low and high quadwords
5714 // of the result come from the same quadword of one of the two inputs. Undef
5715 // mask values count as coming from any quadword, for better codegen.
Benjamin Kramer003fad92011-10-15 13:28:31 +00005716 unsigned LoQuad[] = { 0, 0, 0, 0 };
5717 unsigned HiQuad[] = { 0, 0, 0, 0 };
Benjamin Kramer699ddcb2012-02-06 12:06:18 +00005718 std::bitset<4> InputQuads;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005719 for (unsigned i = 0; i < 8; ++i) {
Benjamin Kramer003fad92011-10-15 13:28:31 +00005720 unsigned *Quad = i < 4 ? LoQuad : HiQuad;
Nate Begeman9008ca62009-04-27 18:41:29 +00005721 int EltIdx = SVOp->getMaskElt(i);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005722 MaskVals.push_back(EltIdx);
5723 if (EltIdx < 0) {
5724 ++Quad[0];
5725 ++Quad[1];
5726 ++Quad[2];
5727 ++Quad[3];
Evan Cheng14b32e12007-12-11 01:46:18 +00005728 continue;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005729 }
5730 ++Quad[EltIdx / 4];
5731 InputQuads.set(EltIdx / 4);
Evan Cheng14b32e12007-12-11 01:46:18 +00005732 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00005733
Nate Begemanb9a47b82009-02-23 08:49:38 +00005734 int BestLoQuad = -1;
Evan Cheng14b32e12007-12-11 01:46:18 +00005735 unsigned MaxQuad = 1;
5736 for (unsigned i = 0; i < 4; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005737 if (LoQuad[i] > MaxQuad) {
5738 BestLoQuad = i;
5739 MaxQuad = LoQuad[i];
Evan Cheng14b32e12007-12-11 01:46:18 +00005740 }
Evan Cheng8a86c3f2007-12-07 08:07:39 +00005741 }
5742
Nate Begemanb9a47b82009-02-23 08:49:38 +00005743 int BestHiQuad = -1;
Evan Cheng14b32e12007-12-11 01:46:18 +00005744 MaxQuad = 1;
5745 for (unsigned i = 0; i < 4; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005746 if (HiQuad[i] > MaxQuad) {
5747 BestHiQuad = i;
5748 MaxQuad = HiQuad[i];
Evan Cheng14b32e12007-12-11 01:46:18 +00005749 }
5750 }
5751
Nate Begemanb9a47b82009-02-23 08:49:38 +00005752 // For SSSE3, If all 8 words of the result come from only 1 quadword of each
Eric Christopherfd179292009-08-27 18:07:15 +00005753 // of the two input vectors, shuffle them into one input vector so only a
Nate Begemanb9a47b82009-02-23 08:49:38 +00005754 // single pshufb instruction is necessary. If There are more than 2 input
5755 // quads, disable the next transformation since it does not help SSSE3.
5756 bool V1Used = InputQuads[0] || InputQuads[1];
5757 bool V2Used = InputQuads[2] || InputQuads[3];
Craig Topperd0a31172012-01-10 06:37:29 +00005758 if (Subtarget->hasSSSE3()) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005759 if (InputQuads.count() == 2 && V1Used && V2Used) {
Benjamin Kramer699ddcb2012-02-06 12:06:18 +00005760 BestLoQuad = InputQuads[0] ? 0 : 1;
5761 BestHiQuad = InputQuads[2] ? 2 : 3;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005762 }
5763 if (InputQuads.count() > 2) {
5764 BestLoQuad = -1;
5765 BestHiQuad = -1;
5766 }
5767 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00005768
Nate Begemanb9a47b82009-02-23 08:49:38 +00005769 // If BestLoQuad or BestHiQuad are set, shuffle the quads together and update
5770 // the shuffle mask. If a quad is scored as -1, that means that it contains
5771 // words from all 4 input quadwords.
5772 SDValue NewV;
5773 if (BestLoQuad >= 0 || BestHiQuad >= 0) {
Benjamin Kramer9c683542012-01-30 15:16:21 +00005774 int MaskV[] = {
5775 BestLoQuad < 0 ? 0 : BestLoQuad,
5776 BestHiQuad < 0 ? 1 : BestHiQuad
5777 };
Eric Christopherfd179292009-08-27 18:07:15 +00005778 NewV = DAG.getVectorShuffle(MVT::v2i64, dl,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005779 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64, V1),
5780 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64, V2), &MaskV[0]);
5781 NewV = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, NewV);
Evan Cheng14b32e12007-12-11 01:46:18 +00005782
Nate Begemanb9a47b82009-02-23 08:49:38 +00005783 // Rewrite the MaskVals and assign NewV to V1 if NewV now contains all the
5784 // source words for the shuffle, to aid later transformations.
5785 bool AllWordsInNewV = true;
Mon P Wang37b9a192009-03-11 06:35:11 +00005786 bool InOrder[2] = { true, true };
Evan Cheng14b32e12007-12-11 01:46:18 +00005787 for (unsigned i = 0; i != 8; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005788 int idx = MaskVals[i];
Mon P Wang37b9a192009-03-11 06:35:11 +00005789 if (idx != (int)i)
5790 InOrder[i/4] = false;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005791 if (idx < 0 || (idx/4) == BestLoQuad || (idx/4) == BestHiQuad)
Evan Cheng14b32e12007-12-11 01:46:18 +00005792 continue;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005793 AllWordsInNewV = false;
5794 break;
Evan Cheng14b32e12007-12-11 01:46:18 +00005795 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00005796
Nate Begemanb9a47b82009-02-23 08:49:38 +00005797 bool pshuflw = AllWordsInNewV, pshufhw = AllWordsInNewV;
5798 if (AllWordsInNewV) {
5799 for (int i = 0; i != 8; ++i) {
5800 int idx = MaskVals[i];
5801 if (idx < 0)
Evan Cheng14b32e12007-12-11 01:46:18 +00005802 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00005803 idx = MaskVals[i] = (idx / 4) == BestLoQuad ? (idx & 3) : (idx & 3) + 4;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005804 if ((idx != i) && idx < 4)
5805 pshufhw = false;
5806 if ((idx != i) && idx > 3)
5807 pshuflw = false;
Evan Cheng14b32e12007-12-11 01:46:18 +00005808 }
Nate Begemanb9a47b82009-02-23 08:49:38 +00005809 V1 = NewV;
5810 V2Used = false;
5811 BestLoQuad = 0;
5812 BestHiQuad = 1;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00005813 }
Evan Cheng14b32e12007-12-11 01:46:18 +00005814
Nate Begemanb9a47b82009-02-23 08:49:38 +00005815 // If we've eliminated the use of V2, and the new mask is a pshuflw or
5816 // pshufhw, that's as cheap as it gets. Return the new shuffle.
Mon P Wang37b9a192009-03-11 06:35:11 +00005817 if ((pshufhw && InOrder[0]) || (pshuflw && InOrder[1])) {
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00005818 unsigned Opc = pshufhw ? X86ISD::PSHUFHW : X86ISD::PSHUFLW;
5819 unsigned TargetMask = 0;
5820 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV,
Owen Anderson825b72b2009-08-11 20:47:22 +00005821 DAG.getUNDEF(MVT::v8i16), &MaskVals[0]);
Craig Topperdd637ae2012-02-19 05:41:45 +00005822 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(NewV.getNode());
5823 TargetMask = pshufhw ? getShufflePSHUFHWImmediate(SVOp):
5824 getShufflePSHUFLWImmediate(SVOp);
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00005825 V1 = NewV.getOperand(0);
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00005826 return getTargetShuffleNode(Opc, dl, MVT::v8i16, V1, TargetMask, DAG);
Evan Cheng14b32e12007-12-11 01:46:18 +00005827 }
Evan Cheng14b32e12007-12-11 01:46:18 +00005828 }
Eric Christopherfd179292009-08-27 18:07:15 +00005829
Nate Begemanb9a47b82009-02-23 08:49:38 +00005830 // If we have SSSE3, and all words of the result are from 1 input vector,
5831 // case 2 is generated, otherwise case 3 is generated. If no SSSE3
5832 // is present, fall back to case 4.
Craig Topperd0a31172012-01-10 06:37:29 +00005833 if (Subtarget->hasSSSE3()) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005834 SmallVector<SDValue,16> pshufbMask;
Eric Christopherfd179292009-08-27 18:07:15 +00005835
Nate Begemanb9a47b82009-02-23 08:49:38 +00005836 // If we have elements from both input vectors, set the high bit of the
Eric Christopherfd179292009-08-27 18:07:15 +00005837 // shuffle mask element to zero out elements that come from V2 in the V1
Nate Begemanb9a47b82009-02-23 08:49:38 +00005838 // mask, and elements that come from V1 in the V2 mask, so that the two
5839 // results can be OR'd together.
5840 bool TwoInputs = V1Used && V2Used;
5841 for (unsigned i = 0; i != 8; ++i) {
5842 int EltIdx = MaskVals[i] * 2;
Craig Topperbe97ae92012-05-18 07:07:36 +00005843 int Idx0 = (TwoInputs && (EltIdx >= 16)) ? 0x80 : EltIdx;
5844 int Idx1 = (TwoInputs && (EltIdx >= 16)) ? 0x80 : EltIdx+1;
5845 pshufbMask.push_back(DAG.getConstant(Idx0, MVT::i8));
5846 pshufbMask.push_back(DAG.getConstant(Idx1, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005847 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005848 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, V1);
Eric Christopherfd179292009-08-27 18:07:15 +00005849 V1 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V1,
Evan Chenga87008d2009-02-25 22:49:59 +00005850 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005851 MVT::v16i8, &pshufbMask[0], 16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005852 if (!TwoInputs)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005853 return DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V1);
Eric Christopherfd179292009-08-27 18:07:15 +00005854
Nate Begemanb9a47b82009-02-23 08:49:38 +00005855 // Calculate the shuffle mask for the second input, shuffle it, and
5856 // OR it with the first shuffled input.
5857 pshufbMask.clear();
5858 for (unsigned i = 0; i != 8; ++i) {
5859 int EltIdx = MaskVals[i] * 2;
Craig Topperbe97ae92012-05-18 07:07:36 +00005860 int Idx0 = (EltIdx < 16) ? 0x80 : EltIdx - 16;
5861 int Idx1 = (EltIdx < 16) ? 0x80 : EltIdx - 15;
5862 pshufbMask.push_back(DAG.getConstant(Idx0, MVT::i8));
5863 pshufbMask.push_back(DAG.getConstant(Idx1, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005864 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005865 V2 = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, V2);
Eric Christopherfd179292009-08-27 18:07:15 +00005866 V2 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V2,
Evan Chenga87008d2009-02-25 22:49:59 +00005867 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005868 MVT::v16i8, &pshufbMask[0], 16));
5869 V1 = DAG.getNode(ISD::OR, dl, MVT::v16i8, V1, V2);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005870 return DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005871 }
5872
5873 // If BestLoQuad >= 0, generate a pshuflw to put the low elements in order,
5874 // and update MaskVals with new element order.
Benjamin Kramer9c683542012-01-30 15:16:21 +00005875 std::bitset<8> InOrder;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005876 if (BestLoQuad >= 0) {
Benjamin Kramer9c683542012-01-30 15:16:21 +00005877 int MaskV[] = { -1, -1, -1, -1, 4, 5, 6, 7 };
Nate Begemanb9a47b82009-02-23 08:49:38 +00005878 for (int i = 0; i != 4; ++i) {
5879 int idx = MaskVals[i];
5880 if (idx < 0) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005881 InOrder.set(i);
5882 } else if ((idx / 4) == BestLoQuad) {
Benjamin Kramer9c683542012-01-30 15:16:21 +00005883 MaskV[i] = idx & 3;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005884 InOrder.set(i);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005885 }
5886 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005887 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV, DAG.getUNDEF(MVT::v8i16),
Nate Begeman9008ca62009-04-27 18:41:29 +00005888 &MaskV[0]);
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00005889
Craig Topperdd637ae2012-02-19 05:41:45 +00005890 if (NewV.getOpcode() == ISD::VECTOR_SHUFFLE && Subtarget->hasSSSE3()) {
5891 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(NewV.getNode());
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00005892 NewV = getTargetShuffleNode(X86ISD::PSHUFLW, dl, MVT::v8i16,
Craig Topperdd637ae2012-02-19 05:41:45 +00005893 NewV.getOperand(0),
5894 getShufflePSHUFLWImmediate(SVOp), DAG);
5895 }
Nate Begemanb9a47b82009-02-23 08:49:38 +00005896 }
Eric Christopherfd179292009-08-27 18:07:15 +00005897
Nate Begemanb9a47b82009-02-23 08:49:38 +00005898 // If BestHi >= 0, generate a pshufhw to put the high elements in order,
5899 // and update MaskVals with the new element order.
5900 if (BestHiQuad >= 0) {
Benjamin Kramer9c683542012-01-30 15:16:21 +00005901 int MaskV[] = { 0, 1, 2, 3, -1, -1, -1, -1 };
Nate Begemanb9a47b82009-02-23 08:49:38 +00005902 for (unsigned i = 4; i != 8; ++i) {
5903 int idx = MaskVals[i];
5904 if (idx < 0) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005905 InOrder.set(i);
5906 } else if ((idx / 4) == BestHiQuad) {
Benjamin Kramer9c683542012-01-30 15:16:21 +00005907 MaskV[i] = (idx & 3) + 4;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005908 InOrder.set(i);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005909 }
5910 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005911 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV, DAG.getUNDEF(MVT::v8i16),
Nate Begeman9008ca62009-04-27 18:41:29 +00005912 &MaskV[0]);
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00005913
Craig Topperdd637ae2012-02-19 05:41:45 +00005914 if (NewV.getOpcode() == ISD::VECTOR_SHUFFLE && Subtarget->hasSSSE3()) {
5915 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(NewV.getNode());
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00005916 NewV = getTargetShuffleNode(X86ISD::PSHUFHW, dl, MVT::v8i16,
Craig Topperdd637ae2012-02-19 05:41:45 +00005917 NewV.getOperand(0),
5918 getShufflePSHUFHWImmediate(SVOp), DAG);
5919 }
Nate Begemanb9a47b82009-02-23 08:49:38 +00005920 }
Eric Christopherfd179292009-08-27 18:07:15 +00005921
Nate Begemanb9a47b82009-02-23 08:49:38 +00005922 // In case BestHi & BestLo were both -1, which means each quadword has a word
5923 // from each of the four input quadwords, calculate the InOrder bitvector now
5924 // before falling through to the insert/extract cleanup.
5925 if (BestLoQuad == -1 && BestHiQuad == -1) {
5926 NewV = V1;
5927 for (int i = 0; i != 8; ++i)
5928 if (MaskVals[i] < 0 || MaskVals[i] == i)
5929 InOrder.set(i);
5930 }
Eric Christopherfd179292009-08-27 18:07:15 +00005931
Nate Begemanb9a47b82009-02-23 08:49:38 +00005932 // The other elements are put in the right place using pextrw and pinsrw.
5933 for (unsigned i = 0; i != 8; ++i) {
5934 if (InOrder[i])
5935 continue;
5936 int EltIdx = MaskVals[i];
5937 if (EltIdx < 0)
5938 continue;
Craig Topper6643d9c2012-05-04 06:18:33 +00005939 SDValue ExtOp = (EltIdx < 8) ?
5940 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, V1,
5941 DAG.getIntPtrConstant(EltIdx)) :
5942 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, V2,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005943 DAG.getIntPtrConstant(EltIdx - 8));
Owen Anderson825b72b2009-08-11 20:47:22 +00005944 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, ExtOp,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005945 DAG.getIntPtrConstant(i));
5946 }
5947 return NewV;
5948}
5949
5950// v16i8 shuffles - Prefer shuffles in the following order:
5951// 1. [ssse3] 1 x pshufb
5952// 2. [ssse3] 2 x pshufb + 1 x por
5953// 3. [all] v8i16 shuffle + N x pextrw + rotate + pinsrw
5954static
Nate Begeman9008ca62009-04-27 18:41:29 +00005955SDValue LowerVECTOR_SHUFFLEv16i8(ShuffleVectorSDNode *SVOp,
Dan Gohmand858e902010-04-17 15:26:15 +00005956 SelectionDAG &DAG,
5957 const X86TargetLowering &TLI) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005958 SDValue V1 = SVOp->getOperand(0);
5959 SDValue V2 = SVOp->getOperand(1);
5960 DebugLoc dl = SVOp->getDebugLoc();
Benjamin Kramered4c8c62012-01-15 13:16:05 +00005961 ArrayRef<int> MaskVals = SVOp->getMask();
Eric Christopherfd179292009-08-27 18:07:15 +00005962
Nate Begemanb9a47b82009-02-23 08:49:38 +00005963 // If we have SSSE3, case 1 is generated when all result bytes come from
Eric Christopherfd179292009-08-27 18:07:15 +00005964 // one of the inputs. Otherwise, case 2 is generated. If no SSSE3 is
Nate Begemanb9a47b82009-02-23 08:49:38 +00005965 // present, fall back to case 3.
Eric Christopherfd179292009-08-27 18:07:15 +00005966
Nate Begemanb9a47b82009-02-23 08:49:38 +00005967 // If SSSE3, use 1 pshufb instruction per vector with elements in the result.
Craig Topperd0a31172012-01-10 06:37:29 +00005968 if (TLI.getSubtarget()->hasSSSE3()) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005969 SmallVector<SDValue,16> pshufbMask;
Eric Christopherfd179292009-08-27 18:07:15 +00005970
Nate Begemanb9a47b82009-02-23 08:49:38 +00005971 // If all result elements are from one input vector, then only translate
Eric Christopherfd179292009-08-27 18:07:15 +00005972 // undef mask values to 0x80 (zero out result) in the pshufb mask.
Nate Begemanb9a47b82009-02-23 08:49:38 +00005973 //
5974 // Otherwise, we have elements from both input vectors, and must zero out
5975 // elements that come from V2 in the first mask, and V1 in the second mask
5976 // so that we can OR them together.
Nate Begemanb9a47b82009-02-23 08:49:38 +00005977 for (unsigned i = 0; i != 16; ++i) {
5978 int EltIdx = MaskVals[i];
Craig Topperb82b5ab2012-05-18 06:42:06 +00005979 if (EltIdx < 0 || EltIdx >= 16)
5980 EltIdx = 0x80;
Owen Anderson825b72b2009-08-11 20:47:22 +00005981 pshufbMask.push_back(DAG.getConstant(EltIdx, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005982 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005983 V1 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V1,
Evan Chenga87008d2009-02-25 22:49:59 +00005984 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005985 MVT::v16i8, &pshufbMask[0], 16));
Michael Liao265bcb12012-08-31 20:12:31 +00005986
5987 // As PSHUFB will zero elements with negative indices, it's safe to ignore
5988 // the 2nd operand if it's undefined or zero.
5989 if (V2.getOpcode() == ISD::UNDEF ||
5990 ISD::isBuildVectorAllZeros(V2.getNode()))
Nate Begemanb9a47b82009-02-23 08:49:38 +00005991 return V1;
Eric Christopherfd179292009-08-27 18:07:15 +00005992
Nate Begemanb9a47b82009-02-23 08:49:38 +00005993 // Calculate the shuffle mask for the second input, shuffle it, and
5994 // OR it with the first shuffled input.
5995 pshufbMask.clear();
5996 for (unsigned i = 0; i != 16; ++i) {
5997 int EltIdx = MaskVals[i];
Craig Topperb82b5ab2012-05-18 06:42:06 +00005998 EltIdx = (EltIdx < 16) ? 0x80 : EltIdx - 16;
Craig Topper85b9e562012-05-22 06:09:38 +00005999 pshufbMask.push_back(DAG.getConstant(EltIdx, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00006000 }
Owen Anderson825b72b2009-08-11 20:47:22 +00006001 V2 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V2,
Evan Chenga87008d2009-02-25 22:49:59 +00006002 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00006003 MVT::v16i8, &pshufbMask[0], 16));
6004 return DAG.getNode(ISD::OR, dl, MVT::v16i8, V1, V2);
Nate Begemanb9a47b82009-02-23 08:49:38 +00006005 }
Eric Christopherfd179292009-08-27 18:07:15 +00006006
Nate Begemanb9a47b82009-02-23 08:49:38 +00006007 // No SSSE3 - Calculate in place words and then fix all out of place words
6008 // With 0-16 extracts & inserts. Worst case is 16 bytes out of order from
6009 // the 16 different words that comprise the two doublequadword input vectors.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006010 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V1);
6011 V2 = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V2);
Craig Topperb82b5ab2012-05-18 06:42:06 +00006012 SDValue NewV = V1;
Nate Begemanb9a47b82009-02-23 08:49:38 +00006013 for (int i = 0; i != 8; ++i) {
6014 int Elt0 = MaskVals[i*2];
6015 int Elt1 = MaskVals[i*2+1];
Eric Christopherfd179292009-08-27 18:07:15 +00006016
Nate Begemanb9a47b82009-02-23 08:49:38 +00006017 // This word of the result is all undef, skip it.
6018 if (Elt0 < 0 && Elt1 < 0)
6019 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00006020
Nate Begemanb9a47b82009-02-23 08:49:38 +00006021 // This word of the result is already in the correct place, skip it.
Craig Topperb82b5ab2012-05-18 06:42:06 +00006022 if ((Elt0 == i*2) && (Elt1 == i*2+1))
Nate Begemanb9a47b82009-02-23 08:49:38 +00006023 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00006024
Nate Begemanb9a47b82009-02-23 08:49:38 +00006025 SDValue Elt0Src = Elt0 < 16 ? V1 : V2;
6026 SDValue Elt1Src = Elt1 < 16 ? V1 : V2;
6027 SDValue InsElt;
Mon P Wang6b3ef692009-03-11 18:47:57 +00006028
6029 // If Elt0 and Elt1 are defined, are consecutive, and can be load
6030 // using a single extract together, load it and store it.
6031 if ((Elt0 >= 0) && ((Elt0 + 1) == Elt1) && ((Elt0 & 1) == 0)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00006032 InsElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, Elt1Src,
Mon P Wang6b3ef692009-03-11 18:47:57 +00006033 DAG.getIntPtrConstant(Elt1 / 2));
Owen Anderson825b72b2009-08-11 20:47:22 +00006034 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, InsElt,
Mon P Wang6b3ef692009-03-11 18:47:57 +00006035 DAG.getIntPtrConstant(i));
6036 continue;
6037 }
6038
Nate Begemanb9a47b82009-02-23 08:49:38 +00006039 // If Elt1 is defined, extract it from the appropriate source. If the
Mon P Wang6b3ef692009-03-11 18:47:57 +00006040 // source byte is not also odd, shift the extracted word left 8 bits
6041 // otherwise clear the bottom 8 bits if we need to do an or.
Nate Begemanb9a47b82009-02-23 08:49:38 +00006042 if (Elt1 >= 0) {
Owen Anderson825b72b2009-08-11 20:47:22 +00006043 InsElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, Elt1Src,
Nate Begemanb9a47b82009-02-23 08:49:38 +00006044 DAG.getIntPtrConstant(Elt1 / 2));
6045 if ((Elt1 & 1) == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00006046 InsElt = DAG.getNode(ISD::SHL, dl, MVT::i16, InsElt,
Owen Anderson95771af2011-02-25 21:41:48 +00006047 DAG.getConstant(8,
6048 TLI.getShiftAmountTy(InsElt.getValueType())));
Mon P Wang6b3ef692009-03-11 18:47:57 +00006049 else if (Elt0 >= 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00006050 InsElt = DAG.getNode(ISD::AND, dl, MVT::i16, InsElt,
6051 DAG.getConstant(0xFF00, MVT::i16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00006052 }
6053 // If Elt0 is defined, extract it from the appropriate source. If the
6054 // source byte is not also even, shift the extracted word right 8 bits. If
6055 // Elt1 was also defined, OR the extracted values together before
6056 // inserting them in the result.
6057 if (Elt0 >= 0) {
Owen Anderson825b72b2009-08-11 20:47:22 +00006058 SDValue InsElt0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16,
Nate Begemanb9a47b82009-02-23 08:49:38 +00006059 Elt0Src, DAG.getIntPtrConstant(Elt0 / 2));
6060 if ((Elt0 & 1) != 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00006061 InsElt0 = DAG.getNode(ISD::SRL, dl, MVT::i16, InsElt0,
Owen Anderson95771af2011-02-25 21:41:48 +00006062 DAG.getConstant(8,
6063 TLI.getShiftAmountTy(InsElt0.getValueType())));
Mon P Wang6b3ef692009-03-11 18:47:57 +00006064 else if (Elt1 >= 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00006065 InsElt0 = DAG.getNode(ISD::AND, dl, MVT::i16, InsElt0,
6066 DAG.getConstant(0x00FF, MVT::i16));
6067 InsElt = Elt1 >= 0 ? DAG.getNode(ISD::OR, dl, MVT::i16, InsElt, InsElt0)
Nate Begemanb9a47b82009-02-23 08:49:38 +00006068 : InsElt0;
6069 }
Owen Anderson825b72b2009-08-11 20:47:22 +00006070 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, InsElt,
Nate Begemanb9a47b82009-02-23 08:49:38 +00006071 DAG.getIntPtrConstant(i));
6072 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006073 return DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, NewV);
Evan Cheng14b32e12007-12-11 01:46:18 +00006074}
6075
Elena Demikhovsky41789462012-09-06 12:42:01 +00006076// v32i8 shuffles - Translate to VPSHUFB if possible.
6077static
6078SDValue LowerVECTOR_SHUFFLEv32i8(ShuffleVectorSDNode *SVOp,
Craig Topper55b24052012-09-11 06:15:32 +00006079 const X86Subtarget *Subtarget,
6080 SelectionDAG &DAG) {
Elena Demikhovsky41789462012-09-06 12:42:01 +00006081 EVT VT = SVOp->getValueType(0);
6082 SDValue V1 = SVOp->getOperand(0);
6083 SDValue V2 = SVOp->getOperand(1);
6084 DebugLoc dl = SVOp->getDebugLoc();
Elena Demikhovsky8100d242012-09-10 12:13:11 +00006085 SmallVector<int, 32> MaskVals(SVOp->getMask().begin(), SVOp->getMask().end());
Elena Demikhovsky41789462012-09-06 12:42:01 +00006086
6087 bool V2IsUndef = V2.getOpcode() == ISD::UNDEF;
Elena Demikhovsky8100d242012-09-10 12:13:11 +00006088 bool V1IsAllZero = ISD::isBuildVectorAllZeros(V1.getNode());
6089 bool V2IsAllZero = ISD::isBuildVectorAllZeros(V2.getNode());
Elena Demikhovsky41789462012-09-06 12:42:01 +00006090
Michael Liao471b9172012-10-03 23:43:52 +00006091 // VPSHUFB may be generated if
Elena Demikhovsky8100d242012-09-10 12:13:11 +00006092 // (1) one of input vector is undefined or zeroinitializer.
6093 // The mask value 0x80 puts 0 in the corresponding slot of the vector.
6094 // And (2) the mask indexes don't cross the 128-bit lane.
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00006095 if (VT != MVT::v32i8 || !Subtarget->hasInt256() ||
Elena Demikhovsky8100d242012-09-10 12:13:11 +00006096 (!V2IsUndef && !V2IsAllZero && !V1IsAllZero))
Elena Demikhovsky41789462012-09-06 12:42:01 +00006097 return SDValue();
6098
Elena Demikhovsky8100d242012-09-10 12:13:11 +00006099 if (V1IsAllZero && !V2IsAllZero) {
6100 CommuteVectorShuffleMask(MaskVals, 32);
6101 V1 = V2;
6102 }
6103 SmallVector<SDValue, 32> pshufbMask;
Elena Demikhovsky41789462012-09-06 12:42:01 +00006104 for (unsigned i = 0; i != 32; i++) {
6105 int EltIdx = MaskVals[i];
6106 if (EltIdx < 0 || EltIdx >= 32)
6107 EltIdx = 0x80;
6108 else {
6109 if ((EltIdx >= 16 && i < 16) || (EltIdx < 16 && i >= 16))
6110 // Cross lane is not allowed.
6111 return SDValue();
6112 EltIdx &= 0xf;
6113 }
6114 pshufbMask.push_back(DAG.getConstant(EltIdx, MVT::i8));
6115 }
6116 return DAG.getNode(X86ISD::PSHUFB, dl, MVT::v32i8, V1,
6117 DAG.getNode(ISD::BUILD_VECTOR, dl,
6118 MVT::v32i8, &pshufbMask[0], 32));
6119}
6120
Evan Cheng7a831ce2007-12-15 03:00:47 +00006121/// RewriteAsNarrowerShuffle - Try rewriting v8i16 and v16i8 shuffles as 4 wide
Bruno Cardoso Lopes0a7dd4f2010-09-08 18:12:31 +00006122/// ones, or rewriting v4i32 / v4f32 as 2 wide ones if possible. This can be
Evan Cheng7a831ce2007-12-15 03:00:47 +00006123/// done when every pair / quad of shuffle mask elements point to elements in
6124/// the right sequence. e.g.
Bruno Cardoso Lopes0a7dd4f2010-09-08 18:12:31 +00006125/// vector_shuffle X, Y, <2, 3, | 10, 11, | 0, 1, | 14, 15>
Evan Cheng14b32e12007-12-11 01:46:18 +00006126static
Nate Begeman9008ca62009-04-27 18:41:29 +00006127SDValue RewriteAsNarrowerShuffle(ShuffleVectorSDNode *SVOp,
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006128 SelectionDAG &DAG, DebugLoc dl) {
Craig Topper11ac1f82012-05-04 04:08:44 +00006129 MVT VT = SVOp->getValueType(0).getSimpleVT();
Nate Begeman9008ca62009-04-27 18:41:29 +00006130 unsigned NumElems = VT.getVectorNumElements();
Craig Topper11ac1f82012-05-04 04:08:44 +00006131 MVT NewVT;
6132 unsigned Scale;
6133 switch (VT.SimpleTy) {
Craig Topperabb94d02012-02-05 03:43:23 +00006134 default: llvm_unreachable("Unexpected!");
Craig Topperf3640d72012-05-04 04:44:49 +00006135 case MVT::v4f32: NewVT = MVT::v2f64; Scale = 2; break;
6136 case MVT::v4i32: NewVT = MVT::v2i64; Scale = 2; break;
6137 case MVT::v8i16: NewVT = MVT::v4i32; Scale = 2; break;
6138 case MVT::v16i8: NewVT = MVT::v4i32; Scale = 4; break;
6139 case MVT::v16i16: NewVT = MVT::v8i32; Scale = 2; break;
6140 case MVT::v32i8: NewVT = MVT::v8i32; Scale = 4; break;
Evan Cheng7a831ce2007-12-15 03:00:47 +00006141 }
6142
Nate Begeman9008ca62009-04-27 18:41:29 +00006143 SmallVector<int, 8> MaskVec;
Craig Topper11ac1f82012-05-04 04:08:44 +00006144 for (unsigned i = 0; i != NumElems; i += Scale) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006145 int StartIdx = -1;
Craig Topper11ac1f82012-05-04 04:08:44 +00006146 for (unsigned j = 0; j != Scale; ++j) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006147 int EltIdx = SVOp->getMaskElt(i+j);
6148 if (EltIdx < 0)
Evan Cheng14b32e12007-12-11 01:46:18 +00006149 continue;
Craig Topper11ac1f82012-05-04 04:08:44 +00006150 if (StartIdx < 0)
6151 StartIdx = (EltIdx / Scale);
6152 if (EltIdx != (int)(StartIdx*Scale + j))
Dan Gohman475871a2008-07-27 21:46:04 +00006153 return SDValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00006154 }
Craig Topper11ac1f82012-05-04 04:08:44 +00006155 MaskVec.push_back(StartIdx);
Evan Cheng8a86c3f2007-12-07 08:07:39 +00006156 }
6157
Craig Topper11ac1f82012-05-04 04:08:44 +00006158 SDValue V1 = DAG.getNode(ISD::BITCAST, dl, NewVT, SVOp->getOperand(0));
6159 SDValue V2 = DAG.getNode(ISD::BITCAST, dl, NewVT, SVOp->getOperand(1));
Nate Begeman9008ca62009-04-27 18:41:29 +00006160 return DAG.getVectorShuffle(NewVT, dl, V1, V2, &MaskVec[0]);
Evan Cheng8a86c3f2007-12-07 08:07:39 +00006161}
6162
Evan Chengd880b972008-05-09 21:53:03 +00006163/// getVZextMovL - Return a zero-extending vector move low node.
Evan Cheng7e2ff772008-05-08 00:57:18 +00006164///
Owen Andersone50ed302009-08-10 22:56:29 +00006165static SDValue getVZextMovL(EVT VT, EVT OpVT,
Nate Begeman9008ca62009-04-27 18:41:29 +00006166 SDValue SrcOp, SelectionDAG &DAG,
6167 const X86Subtarget *Subtarget, DebugLoc dl) {
Owen Anderson825b72b2009-08-11 20:47:22 +00006168 if (VT == MVT::v2f64 || VT == MVT::v4f32) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00006169 LoadSDNode *LD = NULL;
Gabor Greifba36cb52008-08-28 21:40:38 +00006170 if (!isScalarLoadToVector(SrcOp.getNode(), &LD))
Evan Cheng7e2ff772008-05-08 00:57:18 +00006171 LD = dyn_cast<LoadSDNode>(SrcOp);
6172 if (!LD) {
6173 // movssrr and movsdrr do not clear top bits. Try to use movd, movq
6174 // instead.
Owen Anderson766b5ef2009-08-11 21:59:30 +00006175 MVT ExtVT = (OpVT == MVT::v2f64) ? MVT::i64 : MVT::i32;
Duncan Sandscdfad362010-11-03 12:17:33 +00006176 if ((ExtVT != MVT::i64 || Subtarget->is64Bit()) &&
Evan Cheng7e2ff772008-05-08 00:57:18 +00006177 SrcOp.getOpcode() == ISD::SCALAR_TO_VECTOR &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006178 SrcOp.getOperand(0).getOpcode() == ISD::BITCAST &&
Owen Anderson766b5ef2009-08-11 21:59:30 +00006179 SrcOp.getOperand(0).getOperand(0).getValueType() == ExtVT) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00006180 // PR2108
Owen Anderson825b72b2009-08-11 20:47:22 +00006181 OpVT = (OpVT == MVT::v2f64) ? MVT::v2i64 : MVT::v4i32;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006182 return DAG.getNode(ISD::BITCAST, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00006183 DAG.getNode(X86ISD::VZEXT_MOVL, dl, OpVT,
6184 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
6185 OpVT,
Gabor Greif327ef032008-08-28 23:19:51 +00006186 SrcOp.getOperand(0)
6187 .getOperand(0))));
Evan Cheng7e2ff772008-05-08 00:57:18 +00006188 }
6189 }
6190 }
6191
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006192 return DAG.getNode(ISD::BITCAST, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00006193 DAG.getNode(X86ISD::VZEXT_MOVL, dl, OpVT,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006194 DAG.getNode(ISD::BITCAST, dl,
Dale Johannesenace16102009-02-03 19:33:06 +00006195 OpVT, SrcOp)));
Evan Cheng7e2ff772008-05-08 00:57:18 +00006196}
6197
Bruno Cardoso Lopesd0888342011-07-22 00:14:56 +00006198/// LowerVECTOR_SHUFFLE_256 - Handle all 256-bit wide vectors shuffles
6199/// which could not be matched by any known target speficic shuffle
6200static SDValue
6201LowerVECTOR_SHUFFLE_256(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG) {
Elena Demikhovsky15963732012-06-26 08:04:10 +00006202
6203 SDValue NewOp = Compact8x32ShuffleNode(SVOp, DAG);
6204 if (NewOp.getNode())
6205 return NewOp;
6206
Craig Topper8f35c132012-01-20 09:29:03 +00006207 EVT VT = SVOp->getValueType(0);
Bruno Cardoso Lopes3b865982011-08-16 18:21:54 +00006208
Craig Topper8f35c132012-01-20 09:29:03 +00006209 unsigned NumElems = VT.getVectorNumElements();
6210 unsigned NumLaneElems = NumElems / 2;
6211
Craig Topper8f35c132012-01-20 09:29:03 +00006212 DebugLoc dl = SVOp->getDebugLoc();
6213 MVT EltVT = VT.getVectorElementType().getSimpleVT();
Craig Topper9a2b6e12012-04-06 07:45:23 +00006214 EVT NVT = MVT::getVectorVT(EltVT, NumLaneElems);
Craig Topper8ae97ba2012-05-21 06:40:16 +00006215 SDValue Output[2];
Craig Topper8f35c132012-01-20 09:29:03 +00006216
Craig Topper9a2b6e12012-04-06 07:45:23 +00006217 SmallVector<int, 16> Mask;
Craig Topper8f35c132012-01-20 09:29:03 +00006218 for (unsigned l = 0; l < 2; ++l) {
Craig Topper9a2b6e12012-04-06 07:45:23 +00006219 // Build a shuffle mask for the output, discovering on the fly which
6220 // input vectors to use as shuffle operands (recorded in InputUsed).
6221 // If building a suitable shuffle vector proves too hard, then bail
Craig Topper8ae97ba2012-05-21 06:40:16 +00006222 // out with UseBuildVector set.
6223 bool UseBuildVector = false;
Benjamin Kramer9e5512a2012-04-06 13:33:52 +00006224 int InputUsed[2] = { -1, -1 }; // Not yet discovered.
Craig Topper9a2b6e12012-04-06 07:45:23 +00006225 unsigned LaneStart = l * NumLaneElems;
6226 for (unsigned i = 0; i != NumLaneElems; ++i) {
6227 // The mask element. This indexes into the input.
6228 int Idx = SVOp->getMaskElt(i+LaneStart);
6229 if (Idx < 0) {
6230 // the mask element does not index into any input vector.
6231 Mask.push_back(-1);
6232 continue;
6233 }
Craig Topper8f35c132012-01-20 09:29:03 +00006234
Craig Topper9a2b6e12012-04-06 07:45:23 +00006235 // The input vector this mask element indexes into.
6236 int Input = Idx / NumLaneElems;
Craig Topper8f35c132012-01-20 09:29:03 +00006237
Craig Topper9a2b6e12012-04-06 07:45:23 +00006238 // Turn the index into an offset from the start of the input vector.
6239 Idx -= Input * NumLaneElems;
6240
6241 // Find or create a shuffle vector operand to hold this input.
6242 unsigned OpNo;
6243 for (OpNo = 0; OpNo < array_lengthof(InputUsed); ++OpNo) {
6244 if (InputUsed[OpNo] == Input)
6245 // This input vector is already an operand.
6246 break;
6247 if (InputUsed[OpNo] < 0) {
6248 // Create a new operand for this input vector.
6249 InputUsed[OpNo] = Input;
6250 break;
6251 }
6252 }
6253
6254 if (OpNo >= array_lengthof(InputUsed)) {
Craig Topper8ae97ba2012-05-21 06:40:16 +00006255 // More than two input vectors used! Give up on trying to create a
6256 // shuffle vector. Insert all elements into a BUILD_VECTOR instead.
6257 UseBuildVector = true;
6258 break;
Craig Topper9a2b6e12012-04-06 07:45:23 +00006259 }
6260
6261 // Add the mask index for the new shuffle vector.
6262 Mask.push_back(Idx + OpNo * NumLaneElems);
6263 }
6264
Craig Topper8ae97ba2012-05-21 06:40:16 +00006265 if (UseBuildVector) {
6266 SmallVector<SDValue, 16> SVOps;
6267 for (unsigned i = 0; i != NumLaneElems; ++i) {
6268 // The mask element. This indexes into the input.
6269 int Idx = SVOp->getMaskElt(i+LaneStart);
6270 if (Idx < 0) {
6271 SVOps.push_back(DAG.getUNDEF(EltVT));
6272 continue;
6273 }
6274
6275 // The input vector this mask element indexes into.
6276 int Input = Idx / NumElems;
6277
6278 // Turn the index into an offset from the start of the input vector.
6279 Idx -= Input * NumElems;
6280
6281 // Extract the vector element by hand.
6282 SVOps.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EltVT,
6283 SVOp->getOperand(Input),
6284 DAG.getIntPtrConstant(Idx)));
6285 }
6286
6287 // Construct the output using a BUILD_VECTOR.
6288 Output[l] = DAG.getNode(ISD::BUILD_VECTOR, dl, NVT, &SVOps[0],
6289 SVOps.size());
6290 } else if (InputUsed[0] < 0) {
Craig Topper9a2b6e12012-04-06 07:45:23 +00006291 // No input vectors were used! The result is undefined.
Craig Topper8ae97ba2012-05-21 06:40:16 +00006292 Output[l] = DAG.getUNDEF(NVT);
Craig Topper9a2b6e12012-04-06 07:45:23 +00006293 } else {
6294 SDValue Op0 = Extract128BitVector(SVOp->getOperand(InputUsed[0] / 2),
Craig Topperb14940a2012-04-22 20:55:18 +00006295 (InputUsed[0] % 2) * NumLaneElems,
6296 DAG, dl);
Craig Topper9a2b6e12012-04-06 07:45:23 +00006297 // If only one input was used, use an undefined vector for the other.
6298 SDValue Op1 = (InputUsed[1] < 0) ? DAG.getUNDEF(NVT) :
6299 Extract128BitVector(SVOp->getOperand(InputUsed[1] / 2),
Craig Topperb14940a2012-04-22 20:55:18 +00006300 (InputUsed[1] % 2) * NumLaneElems, DAG, dl);
Craig Topper9a2b6e12012-04-06 07:45:23 +00006301 // At least one input vector was used. Create a new shuffle vector.
Craig Topper8ae97ba2012-05-21 06:40:16 +00006302 Output[l] = DAG.getVectorShuffle(NVT, dl, Op0, Op1, &Mask[0]);
Craig Topper9a2b6e12012-04-06 07:45:23 +00006303 }
6304
6305 Mask.clear();
6306 }
Craig Topper8f35c132012-01-20 09:29:03 +00006307
6308 // Concatenate the result back
Craig Topper8ae97ba2012-05-21 06:40:16 +00006309 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT, Output[0], Output[1]);
Bruno Cardoso Lopesd0888342011-07-22 00:14:56 +00006310}
6311
Bruno Cardoso Lopes589b8972011-07-22 00:14:53 +00006312/// LowerVECTOR_SHUFFLE_128v4 - Handle all 128-bit wide vectors with
6313/// 4 elements, and match them with several different shuffle types.
Dan Gohman475871a2008-07-27 21:46:04 +00006314static SDValue
Bruno Cardoso Lopes589b8972011-07-22 00:14:53 +00006315LowerVECTOR_SHUFFLE_128v4(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006316 SDValue V1 = SVOp->getOperand(0);
6317 SDValue V2 = SVOp->getOperand(1);
6318 DebugLoc dl = SVOp->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00006319 EVT VT = SVOp->getValueType(0);
Eric Christopherfd179292009-08-27 18:07:15 +00006320
Craig Topper7a9a28b2012-08-12 02:23:29 +00006321 assert(VT.is128BitVector() && "Unsupported vector size");
Bruno Cardoso Lopes589b8972011-07-22 00:14:53 +00006322
Benjamin Kramer9c683542012-01-30 15:16:21 +00006323 std::pair<int, int> Locs[4];
6324 int Mask1[] = { -1, -1, -1, -1 };
Benjamin Kramered4c8c62012-01-15 13:16:05 +00006325 SmallVector<int, 8> PermMask(SVOp->getMask().begin(), SVOp->getMask().end());
Nate Begeman9008ca62009-04-27 18:41:29 +00006326
Evan Chengace3c172008-07-22 21:13:36 +00006327 unsigned NumHi = 0;
6328 unsigned NumLo = 0;
Evan Chengace3c172008-07-22 21:13:36 +00006329 for (unsigned i = 0; i != 4; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006330 int Idx = PermMask[i];
6331 if (Idx < 0) {
Evan Chengace3c172008-07-22 21:13:36 +00006332 Locs[i] = std::make_pair(-1, -1);
6333 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00006334 assert(Idx < 8 && "Invalid VECTOR_SHUFFLE index!");
6335 if (Idx < 4) {
Evan Chengace3c172008-07-22 21:13:36 +00006336 Locs[i] = std::make_pair(0, NumLo);
Nate Begeman9008ca62009-04-27 18:41:29 +00006337 Mask1[NumLo] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00006338 NumLo++;
6339 } else {
6340 Locs[i] = std::make_pair(1, NumHi);
6341 if (2+NumHi < 4)
Nate Begeman9008ca62009-04-27 18:41:29 +00006342 Mask1[2+NumHi] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00006343 NumHi++;
6344 }
6345 }
6346 }
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006347
Evan Chengace3c172008-07-22 21:13:36 +00006348 if (NumLo <= 2 && NumHi <= 2) {
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006349 // If no more than two elements come from either vector. This can be
6350 // implemented with two shuffles. First shuffle gather the elements.
6351 // The second shuffle, which takes the first shuffle as both of its
6352 // vector operands, put the elements into the right order.
Nate Begeman9008ca62009-04-27 18:41:29 +00006353 V1 = DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006354
Benjamin Kramer9c683542012-01-30 15:16:21 +00006355 int Mask2[] = { -1, -1, -1, -1 };
Eric Christopherfd179292009-08-27 18:07:15 +00006356
Benjamin Kramer9c683542012-01-30 15:16:21 +00006357 for (unsigned i = 0; i != 4; ++i)
6358 if (Locs[i].first != -1) {
Evan Chengace3c172008-07-22 21:13:36 +00006359 unsigned Idx = (i < 2) ? 0 : 4;
6360 Idx += Locs[i].first * 2 + Locs[i].second;
Nate Begeman9008ca62009-04-27 18:41:29 +00006361 Mask2[i] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00006362 }
Evan Chengace3c172008-07-22 21:13:36 +00006363
Nate Begeman9008ca62009-04-27 18:41:29 +00006364 return DAG.getVectorShuffle(VT, dl, V1, V1, &Mask2[0]);
Craig Topper69947b92012-04-23 06:57:04 +00006365 }
6366
6367 if (NumLo == 3 || NumHi == 3) {
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006368 // Otherwise, we must have three elements from one vector, call it X, and
6369 // one element from the other, call it Y. First, use a shufps to build an
6370 // intermediate vector with the one element from Y and the element from X
6371 // that will be in the same half in the final destination (the indexes don't
6372 // matter). Then, use a shufps to build the final vector, taking the half
6373 // containing the element from Y from the intermediate, and the other half
6374 // from X.
6375 if (NumHi == 3) {
6376 // Normalize it so the 3 elements come from V1.
Craig Topperbeabc6c2011-12-05 06:56:46 +00006377 CommuteVectorShuffleMask(PermMask, 4);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006378 std::swap(V1, V2);
6379 }
6380
6381 // Find the element from V2.
6382 unsigned HiIndex;
6383 for (HiIndex = 0; HiIndex < 3; ++HiIndex) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006384 int Val = PermMask[HiIndex];
6385 if (Val < 0)
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006386 continue;
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006387 if (Val >= 4)
6388 break;
6389 }
6390
Nate Begeman9008ca62009-04-27 18:41:29 +00006391 Mask1[0] = PermMask[HiIndex];
6392 Mask1[1] = -1;
6393 Mask1[2] = PermMask[HiIndex^1];
6394 Mask1[3] = -1;
6395 V2 = DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006396
6397 if (HiIndex >= 2) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006398 Mask1[0] = PermMask[0];
6399 Mask1[1] = PermMask[1];
6400 Mask1[2] = HiIndex & 1 ? 6 : 4;
6401 Mask1[3] = HiIndex & 1 ? 4 : 6;
6402 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006403 }
Craig Topper69947b92012-04-23 06:57:04 +00006404
6405 Mask1[0] = HiIndex & 1 ? 2 : 0;
6406 Mask1[1] = HiIndex & 1 ? 0 : 2;
6407 Mask1[2] = PermMask[2];
6408 Mask1[3] = PermMask[3];
6409 if (Mask1[2] >= 0)
6410 Mask1[2] += 4;
6411 if (Mask1[3] >= 0)
6412 Mask1[3] += 4;
6413 return DAG.getVectorShuffle(VT, dl, V2, V1, &Mask1[0]);
Evan Chengace3c172008-07-22 21:13:36 +00006414 }
6415
6416 // Break it into (shuffle shuffle_hi, shuffle_lo).
Benjamin Kramer9c683542012-01-30 15:16:21 +00006417 int LoMask[] = { -1, -1, -1, -1 };
6418 int HiMask[] = { -1, -1, -1, -1 };
Nate Begeman9008ca62009-04-27 18:41:29 +00006419
Benjamin Kramer9c683542012-01-30 15:16:21 +00006420 int *MaskPtr = LoMask;
Evan Chengace3c172008-07-22 21:13:36 +00006421 unsigned MaskIdx = 0;
6422 unsigned LoIdx = 0;
6423 unsigned HiIdx = 2;
6424 for (unsigned i = 0; i != 4; ++i) {
6425 if (i == 2) {
Benjamin Kramer9c683542012-01-30 15:16:21 +00006426 MaskPtr = HiMask;
Evan Chengace3c172008-07-22 21:13:36 +00006427 MaskIdx = 1;
6428 LoIdx = 0;
6429 HiIdx = 2;
6430 }
Nate Begeman9008ca62009-04-27 18:41:29 +00006431 int Idx = PermMask[i];
6432 if (Idx < 0) {
Evan Chengace3c172008-07-22 21:13:36 +00006433 Locs[i] = std::make_pair(-1, -1);
Nate Begeman9008ca62009-04-27 18:41:29 +00006434 } else if (Idx < 4) {
Evan Chengace3c172008-07-22 21:13:36 +00006435 Locs[i] = std::make_pair(MaskIdx, LoIdx);
Benjamin Kramer9c683542012-01-30 15:16:21 +00006436 MaskPtr[LoIdx] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00006437 LoIdx++;
6438 } else {
6439 Locs[i] = std::make_pair(MaskIdx, HiIdx);
Benjamin Kramer9c683542012-01-30 15:16:21 +00006440 MaskPtr[HiIdx] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00006441 HiIdx++;
6442 }
6443 }
6444
Nate Begeman9008ca62009-04-27 18:41:29 +00006445 SDValue LoShuffle = DAG.getVectorShuffle(VT, dl, V1, V2, &LoMask[0]);
6446 SDValue HiShuffle = DAG.getVectorShuffle(VT, dl, V1, V2, &HiMask[0]);
Benjamin Kramer9c683542012-01-30 15:16:21 +00006447 int MaskOps[] = { -1, -1, -1, -1 };
6448 for (unsigned i = 0; i != 4; ++i)
6449 if (Locs[i].first != -1)
6450 MaskOps[i] = Locs[i].first * 4 + Locs[i].second;
Nate Begeman9008ca62009-04-27 18:41:29 +00006451 return DAG.getVectorShuffle(VT, dl, LoShuffle, HiShuffle, &MaskOps[0]);
Evan Chengace3c172008-07-22 21:13:36 +00006452}
6453
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00006454static bool MayFoldVectorLoad(SDValue V) {
Jakub Staszaka24262a2012-10-30 00:01:57 +00006455 while (V.hasOneUse() && V.getOpcode() == ISD::BITCAST)
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00006456 V = V.getOperand(0);
Jakub Staszaka24262a2012-10-30 00:01:57 +00006457
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00006458 if (V.hasOneUse() && V.getOpcode() == ISD::SCALAR_TO_VECTOR)
6459 V = V.getOperand(0);
Evan Cheng7bc389b2011-11-08 00:31:58 +00006460 if (V.hasOneUse() && V.getOpcode() == ISD::BUILD_VECTOR &&
6461 V.getNumOperands() == 2 && V.getOperand(1).getOpcode() == ISD::UNDEF)
6462 // BUILD_VECTOR (load), undef
6463 V = V.getOperand(0);
Jakub Staszaka24262a2012-10-30 00:01:57 +00006464
6465 return MayFoldLoad(V);
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00006466}
6467
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006468static
Evan Cheng835580f2010-10-07 20:50:20 +00006469SDValue getMOVDDup(SDValue &Op, DebugLoc &dl, SDValue V1, SelectionDAG &DAG) {
6470 EVT VT = Op.getValueType();
6471
6472 // Canonizalize to v2f64.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006473 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, V1);
6474 return DAG.getNode(ISD::BITCAST, dl, VT,
Evan Cheng835580f2010-10-07 20:50:20 +00006475 getTargetShuffleNode(X86ISD::MOVDDUP, dl, MVT::v2f64,
6476 V1, DAG));
6477}
6478
6479static
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006480SDValue getMOVLowToHigh(SDValue &Op, DebugLoc &dl, SelectionDAG &DAG,
Craig Topper1accb7e2012-01-10 06:54:16 +00006481 bool HasSSE2) {
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006482 SDValue V1 = Op.getOperand(0);
6483 SDValue V2 = Op.getOperand(1);
6484 EVT VT = Op.getValueType();
6485
6486 assert(VT != MVT::v2i64 && "unsupported shuffle type");
6487
Craig Topper1accb7e2012-01-10 06:54:16 +00006488 if (HasSSE2 && VT == MVT::v2f64)
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006489 return getTargetShuffleNode(X86ISD::MOVLHPD, dl, VT, V1, V2, DAG);
6490
Evan Cheng0899f5c2011-08-31 02:05:24 +00006491 // v4f32 or v4i32: canonizalized to v4f32 (which is legal for SSE1)
6492 return DAG.getNode(ISD::BITCAST, dl, VT,
6493 getTargetShuffleNode(X86ISD::MOVLHPS, dl, MVT::v4f32,
6494 DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, V1),
6495 DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, V2), DAG));
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006496}
6497
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00006498static
6499SDValue getMOVHighToLow(SDValue &Op, DebugLoc &dl, SelectionDAG &DAG) {
6500 SDValue V1 = Op.getOperand(0);
6501 SDValue V2 = Op.getOperand(1);
6502 EVT VT = Op.getValueType();
6503
6504 assert((VT == MVT::v4i32 || VT == MVT::v4f32) &&
6505 "unsupported shuffle type");
6506
6507 if (V2.getOpcode() == ISD::UNDEF)
6508 V2 = V1;
6509
6510 // v4i32 or v4f32
6511 return getTargetShuffleNode(X86ISD::MOVHLPS, dl, VT, V1, V2, DAG);
6512}
6513
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006514static
Craig Topper1accb7e2012-01-10 06:54:16 +00006515SDValue getMOVLP(SDValue &Op, DebugLoc &dl, SelectionDAG &DAG, bool HasSSE2) {
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006516 SDValue V1 = Op.getOperand(0);
6517 SDValue V2 = Op.getOperand(1);
6518 EVT VT = Op.getValueType();
6519 unsigned NumElems = VT.getVectorNumElements();
6520
6521 // Use MOVLPS and MOVLPD in case V1 or V2 are loads. During isel, the second
6522 // operand of these instructions is only memory, so check if there's a
6523 // potencial load folding here, otherwise use SHUFPS or MOVSD to match the
6524 // same masks.
6525 bool CanFoldLoad = false;
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006526
Bruno Cardoso Lopesd00bfe12010-09-02 02:35:51 +00006527 // Trivial case, when V2 comes from a load.
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00006528 if (MayFoldVectorLoad(V2))
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006529 CanFoldLoad = true;
6530
6531 // When V1 is a load, it can be folded later into a store in isel, example:
6532 // (store (v4f32 (X86Movlps (load addr:$src1), VR128:$src2)), addr:$src1)
6533 // turns into:
6534 // (MOVLPSmr addr:$src1, VR128:$src2)
6535 // So, recognize this potential and also use MOVLPS or MOVLPD
Evan Cheng7bc389b2011-11-08 00:31:58 +00006536 else if (MayFoldVectorLoad(V1) && MayFoldIntoStore(Op))
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006537 CanFoldLoad = true;
6538
Dan Gohman65fd6562011-11-03 21:49:52 +00006539 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006540 if (CanFoldLoad) {
Craig Topper1accb7e2012-01-10 06:54:16 +00006541 if (HasSSE2 && NumElems == 2)
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006542 return getTargetShuffleNode(X86ISD::MOVLPD, dl, VT, V1, V2, DAG);
6543
6544 if (NumElems == 4)
Benjamin Kramerd9b0b022012-06-02 10:20:22 +00006545 // If we don't care about the second element, proceed to use movss.
Dan Gohman65fd6562011-11-03 21:49:52 +00006546 if (SVOp->getMaskElt(1) != -1)
6547 return getTargetShuffleNode(X86ISD::MOVLPS, dl, VT, V1, V2, DAG);
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006548 }
6549
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006550 // movl and movlp will both match v2i64, but v2i64 is never matched by
6551 // movl earlier because we make it strict to avoid messing with the movlp load
6552 // folding logic (see the code above getMOVLP call). Match it here then,
6553 // this is horrible, but will stay like this until we move all shuffle
6554 // matching to x86 specific nodes. Note that for the 1st condition all
6555 // types are matched with movsd.
Craig Topper1accb7e2012-01-10 06:54:16 +00006556 if (HasSSE2) {
Bruno Cardoso Lopes5ca0d142011-09-14 02:36:14 +00006557 // FIXME: isMOVLMask should be checked and matched before getMOVLP,
6558 // as to remove this logic from here, as much as possible
Craig Topper5aaffa82012-02-19 02:53:47 +00006559 if (NumElems == 2 || !isMOVLMask(SVOp->getMask(), VT))
Bruno Cardoso Lopes57d6a5e2011-08-31 03:04:20 +00006560 return getTargetShuffleNode(X86ISD::MOVSD, dl, VT, V1, V2, DAG);
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006561 return getTargetShuffleNode(X86ISD::MOVSS, dl, VT, V1, V2, DAG);
Bruno Cardoso Lopes57d6a5e2011-08-31 03:04:20 +00006562 }
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006563
6564 assert(VT != MVT::v4i32 && "unsupported shuffle type");
6565
6566 // Invert the operand order and use SHUFPS to match it.
Craig Topperb3982da2011-12-31 23:50:21 +00006567 return getTargetShuffleNode(X86ISD::SHUFP, dl, VT, V2, V1,
Craig Topper5aaffa82012-02-19 02:53:47 +00006568 getShuffleSHUFImmediate(SVOp), DAG);
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006569}
6570
Michael Liaod9d09602012-10-23 17:34:00 +00006571// Reduce a vector shuffle to zext.
6572SDValue
6573X86TargetLowering::lowerVectorIntExtend(SDValue Op, SelectionDAG &DAG) const {
6574 // PMOVZX is only available from SSE41.
6575 if (!Subtarget->hasSSE41())
6576 return SDValue();
6577
6578 EVT VT = Op.getValueType();
6579
6580 // Only AVX2 support 256-bit vector integer extending.
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00006581 if (!Subtarget->hasInt256() && VT.is256BitVector())
Michael Liaod9d09602012-10-23 17:34:00 +00006582 return SDValue();
6583
6584 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
6585 DebugLoc DL = Op.getDebugLoc();
6586 SDValue V1 = Op.getOperand(0);
6587 SDValue V2 = Op.getOperand(1);
6588 unsigned NumElems = VT.getVectorNumElements();
6589
6590 // Extending is an unary operation and the element type of the source vector
6591 // won't be equal to or larger than i64.
6592 if (V2.getOpcode() != ISD::UNDEF || !VT.isInteger() ||
6593 VT.getVectorElementType() == MVT::i64)
6594 return SDValue();
6595
6596 // Find the expansion ratio, e.g. expanding from i8 to i32 has a ratio of 4.
6597 unsigned Shift = 1; // Start from 2, i.e. 1 << 1.
Duncan Sands34739052012-10-29 11:29:53 +00006598 while ((1U << Shift) < NumElems) {
6599 if (SVOp->getMaskElt(1U << Shift) == 1)
Michael Liaod9d09602012-10-23 17:34:00 +00006600 break;
6601 Shift += 1;
6602 // The maximal ratio is 8, i.e. from i8 to i64.
6603 if (Shift > 3)
6604 return SDValue();
6605 }
6606
6607 // Check the shuffle mask.
6608 unsigned Mask = (1U << Shift) - 1;
6609 for (unsigned i = 0; i != NumElems; ++i) {
6610 int EltIdx = SVOp->getMaskElt(i);
6611 if ((i & Mask) != 0 && EltIdx != -1)
6612 return SDValue();
Matt Beaumont-Gaya999de02012-10-23 19:46:36 +00006613 if ((i & Mask) == 0 && (unsigned)EltIdx != (i >> Shift))
Michael Liaod9d09602012-10-23 17:34:00 +00006614 return SDValue();
6615 }
6616
6617 unsigned NBits = VT.getVectorElementType().getSizeInBits() << Shift;
6618 EVT NeVT = EVT::getIntegerVT(*DAG.getContext(), NBits);
6619 EVT NVT = EVT::getVectorVT(*DAG.getContext(), NeVT, NumElems >> Shift);
6620
6621 if (!isTypeLegal(NVT))
6622 return SDValue();
6623
6624 // Simplify the operand as it's prepared to be fed into shuffle.
6625 unsigned SignificantBits = NVT.getSizeInBits() >> Shift;
6626 if (V1.getOpcode() == ISD::BITCAST &&
6627 V1.getOperand(0).getOpcode() == ISD::SCALAR_TO_VECTOR &&
6628 V1.getOperand(0).getOperand(0).getOpcode() == ISD::EXTRACT_VECTOR_ELT &&
6629 V1.getOperand(0)
6630 .getOperand(0).getValueType().getSizeInBits() == SignificantBits) {
6631 // (bitcast (sclr2vec (ext_vec_elt x))) -> (bitcast x)
6632 SDValue V = V1.getOperand(0).getOperand(0).getOperand(0);
Michael Liao07872742012-10-23 21:40:15 +00006633 ConstantSDNode *CIdx =
6634 dyn_cast<ConstantSDNode>(V1.getOperand(0).getOperand(0).getOperand(1));
Michael Liaod9d09602012-10-23 17:34:00 +00006635 // If it's foldable, i.e. normal load with single use, we will let code
6636 // selection to fold it. Otherwise, we will short the conversion sequence.
Michael Liao07872742012-10-23 21:40:15 +00006637 if (CIdx && CIdx->getZExtValue() == 0 &&
6638 (!ISD::isNormalLoad(V.getNode()) || !V.hasOneUse()))
Michael Liaod9d09602012-10-23 17:34:00 +00006639 V1 = DAG.getNode(ISD::BITCAST, DL, V1.getValueType(), V);
6640 }
6641
6642 return DAG.getNode(ISD::BITCAST, DL, VT,
6643 DAG.getNode(X86ISD::VZEXT, DL, NVT, V1));
6644}
6645
Nadav Rotem154819d2012-04-09 07:45:58 +00006646SDValue
6647X86TargetLowering::NormalizeVectorShuffle(SDValue Op, SelectionDAG &DAG) const {
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006648 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
6649 EVT VT = Op.getValueType();
6650 DebugLoc dl = Op.getDebugLoc();
6651 SDValue V1 = Op.getOperand(0);
6652 SDValue V2 = Op.getOperand(1);
6653
6654 if (isZeroShuffle(SVOp))
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00006655 return getZeroVector(VT, Subtarget, DAG, dl);
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006656
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006657 // Handle splat operations
6658 if (SVOp->isSplat()) {
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00006659 unsigned NumElem = VT.getVectorNumElements();
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00006660 int Size = VT.getSizeInBits();
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006661
Bruno Cardoso Lopes0e6d2302011-08-17 02:29:19 +00006662 // Use vbroadcast whenever the splat comes from a foldable load
Nadav Rotem154819d2012-04-09 07:45:58 +00006663 SDValue Broadcast = LowerVectorBroadcast(Op, DAG);
Nadav Rotem9d68b062012-04-08 12:54:54 +00006664 if (Broadcast.getNode())
6665 return Broadcast;
Bruno Cardoso Lopes0e6d2302011-08-17 02:29:19 +00006666
Bruno Cardoso Lopes6a32adc2011-07-25 23:05:25 +00006667 // Handle splats by matching through known shuffle masks
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00006668 if ((Size == 128 && NumElem <= 4) ||
Jakub Staszakd642baf2012-11-26 19:24:31 +00006669 (Size == 256 && NumElem <= 8))
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006670 return SDValue();
6671
Bruno Cardoso Lopes8a5b2622011-08-17 02:29:13 +00006672 // All remaning splats are promoted to target supported vector shuffles.
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006673 return PromoteSplat(SVOp, DAG);
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006674 }
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006675
Michael Liaod9d09602012-10-23 17:34:00 +00006676 // Check integer expanding shuffles.
6677 SDValue NewOp = lowerVectorIntExtend(Op, DAG);
6678 if (NewOp.getNode())
6679 return NewOp;
6680
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006681 // If the shuffle can be profitably rewritten as a narrower shuffle, then
6682 // do it!
Craig Topperf3640d72012-05-04 04:44:49 +00006683 if (VT == MVT::v8i16 || VT == MVT::v16i8 ||
6684 VT == MVT::v16i16 || VT == MVT::v32i8) {
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006685 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, dl);
6686 if (NewOp.getNode())
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006687 return DAG.getNode(ISD::BITCAST, dl, VT, NewOp);
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006688 } else if ((VT == MVT::v4i32 ||
Craig Topper1accb7e2012-01-10 06:54:16 +00006689 (VT == MVT::v4f32 && Subtarget->hasSSE2()))) {
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006690 // FIXME: Figure out a cleaner way to do this.
6691 // Try to make use of movq to zero out the top part.
6692 if (ISD::isBuildVectorAllZeros(V2.getNode())) {
6693 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, dl);
6694 if (NewOp.getNode()) {
Craig Topper5aaffa82012-02-19 02:53:47 +00006695 EVT NewVT = NewOp.getValueType();
6696 if (isCommutedMOVLMask(cast<ShuffleVectorSDNode>(NewOp)->getMask(),
6697 NewVT, true, false))
6698 return getVZextMovL(VT, NewVT, NewOp.getOperand(0),
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006699 DAG, Subtarget, dl);
6700 }
6701 } else if (ISD::isBuildVectorAllZeros(V1.getNode())) {
6702 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, dl);
Craig Topper5aaffa82012-02-19 02:53:47 +00006703 if (NewOp.getNode()) {
6704 EVT NewVT = NewOp.getValueType();
6705 if (isMOVLMask(cast<ShuffleVectorSDNode>(NewOp)->getMask(), NewVT))
6706 return getVZextMovL(VT, NewVT, NewOp.getOperand(1),
6707 DAG, Subtarget, dl);
6708 }
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006709 }
6710 }
6711 return SDValue();
6712}
6713
Dan Gohman475871a2008-07-27 21:46:04 +00006714SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006715X86TargetLowering::LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) const {
Nate Begeman9008ca62009-04-27 18:41:29 +00006716 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Dan Gohman475871a2008-07-27 21:46:04 +00006717 SDValue V1 = Op.getOperand(0);
6718 SDValue V2 = Op.getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +00006719 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006720 DebugLoc dl = Op.getDebugLoc();
Nate Begeman9008ca62009-04-27 18:41:29 +00006721 unsigned NumElems = VT.getVectorNumElements();
Elena Demikhovsky16db7102012-01-12 20:33:10 +00006722 bool V1IsUndef = V1.getOpcode() == ISD::UNDEF;
Evan Cheng0db9fe62006-04-25 20:13:52 +00006723 bool V2IsUndef = V2.getOpcode() == ISD::UNDEF;
Evan Chengd9b8e402006-10-16 06:36:00 +00006724 bool V1IsSplat = false;
6725 bool V2IsSplat = false;
Craig Topper1accb7e2012-01-10 06:54:16 +00006726 bool HasSSE2 = Subtarget->hasSSE2();
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00006727 bool HasFp256 = Subtarget->hasFp256();
6728 bool HasInt256 = Subtarget->hasInt256();
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006729 MachineFunction &MF = DAG.getMachineFunction();
Bill Wendling67658342012-10-09 07:45:08 +00006730 bool OptForSize = MF.getFunction()->getFnAttributes().
Bill Wendling034b94b2012-12-19 07:18:57 +00006731 hasAttribute(Attribute::OptimizeForSize);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006732
Craig Topper3426a3e2011-11-14 06:46:21 +00006733 assert(VT.getSizeInBits() != 64 && "Can't lower MMX shuffles");
Bruno Cardoso Lopes58277b12010-09-07 18:41:45 +00006734
Elena Demikhovsky16db7102012-01-12 20:33:10 +00006735 if (V1IsUndef && V2IsUndef)
6736 return DAG.getUNDEF(VT);
6737
6738 assert(!V1IsUndef && "Op 1 of shuffle should not be undef");
Craig Topper38034c52011-11-26 22:55:48 +00006739
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006740 // Vector shuffle lowering takes 3 steps:
6741 //
6742 // 1) Normalize the input vectors. Here splats, zeroed vectors, profitable
6743 // narrowing and commutation of operands should be handled.
6744 // 2) Matching of shuffles with known shuffle masks to x86 target specific
6745 // shuffle nodes.
6746 // 3) Rewriting of unmatched masks into new generic shuffle operations,
6747 // so the shuffle can be broken into other shuffles and the legalizer can
6748 // try the lowering again.
6749 //
Craig Topper3426a3e2011-11-14 06:46:21 +00006750 // The general idea is that no vector_shuffle operation should be left to
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006751 // be matched during isel, all of them must be converted to a target specific
6752 // node here.
Bruno Cardoso Lopes0d1340b2010-09-07 20:20:27 +00006753
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006754 // Normalize the input vectors. Here splats, zeroed vectors, profitable
6755 // narrowing and commutation of operands should be handled. The actual code
6756 // doesn't include all of those, work in progress...
Nadav Rotem154819d2012-04-09 07:45:58 +00006757 SDValue NewOp = NormalizeVectorShuffle(Op, DAG);
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006758 if (NewOp.getNode())
6759 return NewOp;
Eric Christopherfd179292009-08-27 18:07:15 +00006760
Craig Topper5aaffa82012-02-19 02:53:47 +00006761 SmallVector<int, 8> M(SVOp->getMask().begin(), SVOp->getMask().end());
6762
Bruno Cardoso Lopesa22c8452010-09-04 00:39:43 +00006763 // NOTE: isPSHUFDMask can also match both masks below (unpckl_undef and
6764 // unpckh_undef). Only use pshufd if speed is more important than size.
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00006765 if (OptForSize && isUNPCKL_v_undef_Mask(M, VT, HasInt256))
Craig Topper34671b82011-12-06 08:21:25 +00006766 return getTargetShuffleNode(X86ISD::UNPCKL, dl, VT, V1, V1, DAG);
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00006767 if (OptForSize && isUNPCKH_v_undef_Mask(M, VT, HasInt256))
Craig Topper34671b82011-12-06 08:21:25 +00006768 return getTargetShuffleNode(X86ISD::UNPCKH, dl, VT, V1, V1, DAG);
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00006769
Craig Topperdd637ae2012-02-19 05:41:45 +00006770 if (isMOVDDUPMask(M, VT) && Subtarget->hasSSE3() &&
Jakub Staszakd3a05632012-12-06 19:05:46 +00006771 V2IsUndef && MayFoldVectorLoad(V1))
Evan Cheng835580f2010-10-07 20:50:20 +00006772 return getMOVDDup(Op, dl, V1, DAG);
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006773
Craig Topperdd637ae2012-02-19 05:41:45 +00006774 if (isMOVHLPS_v_undef_Mask(M, VT))
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006775 return getMOVHighToLow(Op, dl, DAG);
6776
6777 // Use to match splats
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00006778 if (HasSSE2 && isUNPCKHMask(M, VT, HasInt256) && V2IsUndef &&
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006779 (VT == MVT::v2f64 || VT == MVT::v2i64))
Craig Topper34671b82011-12-06 08:21:25 +00006780 return getTargetShuffleNode(X86ISD::UNPCKH, dl, VT, V1, V1, DAG);
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006781
Craig Topper5aaffa82012-02-19 02:53:47 +00006782 if (isPSHUFDMask(M, VT)) {
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006783 // The actual implementation will match the mask in the if above and then
6784 // during isel it can match several different instructions, not only pshufd
6785 // as its name says, sad but true, emulate the behavior for now...
Craig Topperdd637ae2012-02-19 05:41:45 +00006786 if (isMOVDDUPMask(M, VT) && ((VT == MVT::v4f32 || VT == MVT::v2i64)))
6787 return getTargetShuffleNode(X86ISD::MOVLHPS, dl, VT, V1, V1, DAG);
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006788
Craig Topper5aaffa82012-02-19 02:53:47 +00006789 unsigned TargetMask = getShuffleSHUFImmediate(SVOp);
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006790
Craig Topper1accb7e2012-01-10 06:54:16 +00006791 if (HasSSE2 && (VT == MVT::v4f32 || VT == MVT::v4i32))
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006792 return getTargetShuffleNode(X86ISD::PSHUFD, dl, VT, V1, TargetMask, DAG);
6793
Nadav Roteme4ccfef2012-12-07 19:01:13 +00006794 if (HasFp256 && (VT == MVT::v4f32 || VT == MVT::v2f64))
6795 return getTargetShuffleNode(X86ISD::VPERMILP, dl, VT, V1, TargetMask,
6796 DAG);
6797
Craig Topperb3982da2011-12-31 23:50:21 +00006798 return getTargetShuffleNode(X86ISD::SHUFP, dl, VT, V1, V1,
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00006799 TargetMask, DAG);
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006800 }
Eric Christopherfd179292009-08-27 18:07:15 +00006801
Evan Chengf26ffe92008-05-29 08:22:04 +00006802 // Check if this can be converted into a logical shift.
6803 bool isLeft = false;
6804 unsigned ShAmt = 0;
Dan Gohman475871a2008-07-27 21:46:04 +00006805 SDValue ShVal;
Craig Topper1accb7e2012-01-10 06:54:16 +00006806 bool isShift = HasSSE2 && isVectorShift(SVOp, DAG, isLeft, ShVal, ShAmt);
Evan Chengf26ffe92008-05-29 08:22:04 +00006807 if (isShift && ShVal.hasOneUse()) {
Scott Michelfdc40a02009-02-17 22:15:04 +00006808 // If the shifted value has multiple uses, it may be cheaper to use
Evan Chengf26ffe92008-05-29 08:22:04 +00006809 // v_set0 + movlhps or movhlps, etc.
Dan Gohman8a55ce42009-09-23 21:02:20 +00006810 EVT EltVT = VT.getVectorElementType();
6811 ShAmt *= EltVT.getSizeInBits();
Dale Johannesenace16102009-02-03 19:33:06 +00006812 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00006813 }
Eric Christopherfd179292009-08-27 18:07:15 +00006814
Craig Topper5aaffa82012-02-19 02:53:47 +00006815 if (isMOVLMask(M, VT)) {
Gabor Greifba36cb52008-08-28 21:40:38 +00006816 if (ISD::isBuildVectorAllZeros(V1.getNode()))
Dale Johannesenace16102009-02-03 19:33:06 +00006817 return getVZextMovL(VT, VT, V2, DAG, Subtarget, dl);
Craig Topperdd637ae2012-02-19 05:41:45 +00006818 if (!isMOVLPMask(M, VT)) {
Craig Topper1accb7e2012-01-10 06:54:16 +00006819 if (HasSSE2 && (VT == MVT::v2i64 || VT == MVT::v2f64))
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00006820 return getTargetShuffleNode(X86ISD::MOVSD, dl, VT, V1, V2, DAG);
6821
Bruno Cardoso Lopes4783a3e2010-09-01 22:59:03 +00006822 if (VT == MVT::v4i32 || VT == MVT::v4f32)
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00006823 return getTargetShuffleNode(X86ISD::MOVSS, dl, VT, V1, V2, DAG);
6824 }
Evan Cheng7e2ff772008-05-08 00:57:18 +00006825 }
Eric Christopherfd179292009-08-27 18:07:15 +00006826
Nate Begeman9008ca62009-04-27 18:41:29 +00006827 // FIXME: fold these into legal mask.
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00006828 if (isMOVLHPSMask(M, VT) && !isUNPCKLMask(M, VT, HasInt256))
Craig Topper1accb7e2012-01-10 06:54:16 +00006829 return getMOVLowToHigh(Op, dl, DAG, HasSSE2);
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006830
Craig Topperdd637ae2012-02-19 05:41:45 +00006831 if (isMOVHLPSMask(M, VT))
Dale Johannesen0488fb62010-09-30 23:57:10 +00006832 return getMOVHighToLow(Op, dl, DAG);
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00006833
Craig Topperdd637ae2012-02-19 05:41:45 +00006834 if (V2IsUndef && isMOVSHDUPMask(M, VT, Subtarget))
Dale Johannesen0488fb62010-09-30 23:57:10 +00006835 return getTargetShuffleNode(X86ISD::MOVSHDUP, dl, VT, V1, DAG);
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00006836
Craig Topperdd637ae2012-02-19 05:41:45 +00006837 if (V2IsUndef && isMOVSLDUPMask(M, VT, Subtarget))
Dale Johannesen0488fb62010-09-30 23:57:10 +00006838 return getTargetShuffleNode(X86ISD::MOVSLDUP, dl, VT, V1, DAG);
Bruno Cardoso Lopes013bb3d2010-08-31 22:35:05 +00006839
Craig Topperdd637ae2012-02-19 05:41:45 +00006840 if (isMOVLPMask(M, VT))
Craig Topper1accb7e2012-01-10 06:54:16 +00006841 return getMOVLP(Op, dl, DAG, HasSSE2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006842
Craig Topperdd637ae2012-02-19 05:41:45 +00006843 if (ShouldXformToMOVHLPS(M, VT) ||
6844 ShouldXformToMOVLP(V1.getNode(), V2.getNode(), M, VT))
Nate Begeman9008ca62009-04-27 18:41:29 +00006845 return CommuteVectorShuffle(SVOp, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006846
Evan Chengf26ffe92008-05-29 08:22:04 +00006847 if (isShift) {
Craig Toppered2e13d2012-01-22 19:15:14 +00006848 // No better options. Use a vshldq / vsrldq.
Dan Gohman8a55ce42009-09-23 21:02:20 +00006849 EVT EltVT = VT.getVectorElementType();
6850 ShAmt *= EltVT.getSizeInBits();
Dale Johannesenace16102009-02-03 19:33:06 +00006851 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00006852 }
Eric Christopherfd179292009-08-27 18:07:15 +00006853
Evan Cheng9eca5e82006-10-25 21:49:50 +00006854 bool Commuted = false;
Chris Lattner8a594482007-11-25 00:24:49 +00006855 // FIXME: This should also accept a bitcast of a splat? Be careful, not
6856 // 1,1,1,1 -> v8i16 though.
Gabor Greifba36cb52008-08-28 21:40:38 +00006857 V1IsSplat = isSplatVector(V1.getNode());
6858 V2IsSplat = isSplatVector(V2.getNode());
Scott Michelfdc40a02009-02-17 22:15:04 +00006859
Chris Lattner8a594482007-11-25 00:24:49 +00006860 // Canonicalize the splat or undef, if present, to be on the RHS.
Craig Topper39a9e482012-02-11 06:24:48 +00006861 if (!V2IsUndef && V1IsSplat && !V2IsSplat) {
6862 CommuteVectorShuffleMask(M, NumElems);
6863 std::swap(V1, V2);
Evan Cheng9bbbb982006-10-25 20:48:19 +00006864 std::swap(V1IsSplat, V2IsSplat);
Evan Cheng9eca5e82006-10-25 21:49:50 +00006865 Commuted = true;
Evan Cheng9bbbb982006-10-25 20:48:19 +00006866 }
6867
Craig Topperbeabc6c2011-12-05 06:56:46 +00006868 if (isCommutedMOVLMask(M, VT, V2IsSplat, V2IsUndef)) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006869 // Shuffling low element of v1 into undef, just return v1.
Eric Christopherfd179292009-08-27 18:07:15 +00006870 if (V2IsUndef)
Nate Begeman9008ca62009-04-27 18:41:29 +00006871 return V1;
6872 // If V2 is a splat, the mask may be malformed such as <4,3,3,3>, which
6873 // the instruction selector will not match, so get a canonical MOVL with
6874 // swapped operands to undo the commute.
6875 return getMOVL(DAG, dl, VT, V2, V1);
Evan Chengd9b8e402006-10-16 06:36:00 +00006876 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00006877
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00006878 if (isUNPCKLMask(M, VT, HasInt256))
Craig Topper34671b82011-12-06 08:21:25 +00006879 return getTargetShuffleNode(X86ISD::UNPCKL, dl, VT, V1, V2, DAG);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00006880
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00006881 if (isUNPCKHMask(M, VT, HasInt256))
Craig Topper34671b82011-12-06 08:21:25 +00006882 return getTargetShuffleNode(X86ISD::UNPCKH, dl, VT, V1, V2, DAG);
Evan Chenge1113032006-10-04 18:33:38 +00006883
Evan Cheng9bbbb982006-10-25 20:48:19 +00006884 if (V2IsSplat) {
6885 // Normalize mask so all entries that point to V2 points to its first
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00006886 // element then try to match unpck{h|l} again. If match, return a
Craig Topper39a9e482012-02-11 06:24:48 +00006887 // new vector_shuffle with the corrected mask.p
6888 SmallVector<int, 8> NewMask(M.begin(), M.end());
6889 NormalizeMask(NewMask, NumElems);
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00006890 if (isUNPCKLMask(NewMask, VT, HasInt256, true))
Craig Topper39a9e482012-02-11 06:24:48 +00006891 return getTargetShuffleNode(X86ISD::UNPCKL, dl, VT, V1, V2, DAG);
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00006892 if (isUNPCKHMask(NewMask, VT, HasInt256, true))
Craig Topper39a9e482012-02-11 06:24:48 +00006893 return getTargetShuffleNode(X86ISD::UNPCKH, dl, VT, V1, V2, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006894 }
6895
Evan Cheng9eca5e82006-10-25 21:49:50 +00006896 if (Commuted) {
6897 // Commute is back and try unpck* again.
Nate Begeman9008ca62009-04-27 18:41:29 +00006898 // FIXME: this seems wrong.
Craig Topper39a9e482012-02-11 06:24:48 +00006899 CommuteVectorShuffleMask(M, NumElems);
6900 std::swap(V1, V2);
6901 std::swap(V1IsSplat, V2IsSplat);
6902 Commuted = false;
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00006903
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00006904 if (isUNPCKLMask(M, VT, HasInt256))
Craig Topper39a9e482012-02-11 06:24:48 +00006905 return getTargetShuffleNode(X86ISD::UNPCKL, dl, VT, V1, V2, DAG);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00006906
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00006907 if (isUNPCKHMask(M, VT, HasInt256))
Craig Topper39a9e482012-02-11 06:24:48 +00006908 return getTargetShuffleNode(X86ISD::UNPCKH, dl, VT, V1, V2, DAG);
Evan Cheng9eca5e82006-10-25 21:49:50 +00006909 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00006910
Nate Begeman9008ca62009-04-27 18:41:29 +00006911 // Normalize the node to match x86 shuffle ops if needed
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00006912 if (!V2IsUndef && (isSHUFPMask(M, VT, HasFp256, /* Commuted */ true)))
Nate Begeman9008ca62009-04-27 18:41:29 +00006913 return CommuteVectorShuffle(SVOp, DAG);
6914
Bruno Cardoso Lopes7256e222010-09-03 23:24:06 +00006915 // The checks below are all present in isShuffleMaskLegal, but they are
6916 // inlined here right now to enable us to directly emit target specific
6917 // nodes, and remove one by one until they don't return Op anymore.
Bruno Cardoso Lopes7256e222010-09-03 23:24:06 +00006918
Craig Topper0e2037b2012-01-20 05:53:00 +00006919 if (isPALIGNRMask(M, VT, Subtarget))
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00006920 return getTargetShuffleNode(X86ISD::PALIGN, dl, VT, V1, V2,
Craig Topperd93e4c32011-12-11 19:12:35 +00006921 getShufflePALIGNRImmediate(SVOp),
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00006922 DAG);
6923
Bruno Cardoso Lopesc800c0d2010-09-04 02:02:14 +00006924 if (ShuffleVectorSDNode::isSplatMask(&M[0], VT) &&
6925 SVOp->getSplatIndex() == 0 && V2IsUndef) {
Craig Topperbeabc6c2011-12-05 06:56:46 +00006926 if (VT == MVT::v2f64 || VT == MVT::v2i64)
Craig Topper34671b82011-12-06 08:21:25 +00006927 return getTargetShuffleNode(X86ISD::UNPCKL, dl, VT, V1, V1, DAG);
Bruno Cardoso Lopesc800c0d2010-09-04 02:02:14 +00006928 }
6929
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00006930 if (isPSHUFHWMask(M, VT, HasInt256))
Bruno Cardoso Lopesbbfc3102010-09-04 01:36:45 +00006931 return getTargetShuffleNode(X86ISD::PSHUFHW, dl, VT, V1,
Craig Topper5aaffa82012-02-19 02:53:47 +00006932 getShufflePSHUFHWImmediate(SVOp),
Bruno Cardoso Lopesbbfc3102010-09-04 01:36:45 +00006933 DAG);
6934
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00006935 if (isPSHUFLWMask(M, VT, HasInt256))
Bruno Cardoso Lopesbbfc3102010-09-04 01:36:45 +00006936 return getTargetShuffleNode(X86ISD::PSHUFLW, dl, VT, V1,
Craig Topper5aaffa82012-02-19 02:53:47 +00006937 getShufflePSHUFLWImmediate(SVOp),
Bruno Cardoso Lopesbbfc3102010-09-04 01:36:45 +00006938 DAG);
6939
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00006940 if (isSHUFPMask(M, VT, HasFp256))
Craig Topperb3982da2011-12-31 23:50:21 +00006941 return getTargetShuffleNode(X86ISD::SHUFP, dl, VT, V1, V2,
Craig Topper5aaffa82012-02-19 02:53:47 +00006942 getShuffleSHUFImmediate(SVOp), DAG);
Bruno Cardoso Lopes4c827f52010-09-04 01:22:57 +00006943
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00006944 if (isUNPCKL_v_undef_Mask(M, VT, HasInt256))
Craig Topper34671b82011-12-06 08:21:25 +00006945 return getTargetShuffleNode(X86ISD::UNPCKL, dl, VT, V1, V1, DAG);
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00006946 if (isUNPCKH_v_undef_Mask(M, VT, HasInt256))
Craig Topper34671b82011-12-06 08:21:25 +00006947 return getTargetShuffleNode(X86ISD::UNPCKH, dl, VT, V1, V1, DAG);
Bruno Cardoso Lopesa22c8452010-09-04 00:39:43 +00006948
Bruno Cardoso Lopesd0888342011-07-22 00:14:56 +00006949 //===--------------------------------------------------------------------===//
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00006950 // Generate target specific nodes for 128 or 256-bit shuffles only
6951 // supported in the AVX instruction set.
6952 //
Bruno Cardoso Lopesd0888342011-07-22 00:14:56 +00006953
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00006954 // Handle VMOVDDUPY permutations
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00006955 if (V2IsUndef && isMOVDDUPYMask(M, VT, HasFp256))
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00006956 return getTargetShuffleNode(X86ISD::MOVDDUP, dl, VT, V1, DAG);
6957
Craig Topper70b883b2011-11-28 10:14:51 +00006958 // Handle VPERMILPS/D* permutations
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00006959 if (isVPERMILPMask(M, VT, HasFp256)) {
6960 if (HasInt256 && VT == MVT::v8i32)
Craig Topperdbd98a42012-02-07 06:28:42 +00006961 return getTargetShuffleNode(X86ISD::PSHUFD, dl, VT, V1,
Craig Topper5aaffa82012-02-19 02:53:47 +00006962 getShuffleSHUFImmediate(SVOp), DAG);
Craig Topper316cd2a2011-11-30 06:25:25 +00006963 return getTargetShuffleNode(X86ISD::VPERMILP, dl, VT, V1,
Craig Topper5aaffa82012-02-19 02:53:47 +00006964 getShuffleSHUFImmediate(SVOp), DAG);
Craig Topperdbd98a42012-02-07 06:28:42 +00006965 }
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00006966
Craig Topper70b883b2011-11-28 10:14:51 +00006967 // Handle VPERM2F128/VPERM2I128 permutations
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00006968 if (isVPERM2X128Mask(M, VT, HasFp256))
Craig Topperec24e612011-11-30 07:47:51 +00006969 return getTargetShuffleNode(X86ISD::VPERM2X128, dl, VT, V1,
Craig Topper70b883b2011-11-28 10:14:51 +00006970 V2, getShuffleVPERM2X128Immediate(SVOp), DAG);
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00006971
Craig Topper1842ba02012-04-23 06:38:28 +00006972 SDValue BlendOp = LowerVECTOR_SHUFFLEtoBlend(SVOp, Subtarget, DAG);
Nadav Roteme80aa7c2012-04-09 08:33:21 +00006973 if (BlendOp.getNode())
6974 return BlendOp;
Craig Topper095c5282012-04-15 23:48:57 +00006975
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00006976 if (V2IsUndef && HasInt256 && (VT == MVT::v8i32 || VT == MVT::v8f32)) {
Craig Topper095c5282012-04-15 23:48:57 +00006977 SmallVector<SDValue, 8> permclMask;
Elena Demikhovsky73c504a2012-04-15 11:18:59 +00006978 for (unsigned i = 0; i != 8; ++i) {
Craig Topper095c5282012-04-15 23:48:57 +00006979 permclMask.push_back(DAG.getConstant((M[i]>=0) ? M[i] : 0, MVT::i32));
Elena Demikhovsky73c504a2012-04-15 11:18:59 +00006980 }
Craig Topper92040742012-04-16 06:43:40 +00006981 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v8i32,
6982 &permclMask[0], 8);
6983 // Bitcast is for VPERMPS since mask is v8i32 but node takes v8f32
Craig Topper8325c112012-04-16 00:41:45 +00006984 return DAG.getNode(X86ISD::VPERMV, dl, VT,
Craig Topper92040742012-04-16 06:43:40 +00006985 DAG.getNode(ISD::BITCAST, dl, VT, Mask), V1);
Elena Demikhovsky73c504a2012-04-15 11:18:59 +00006986 }
Craig Topper095c5282012-04-15 23:48:57 +00006987
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00006988 if (V2IsUndef && HasInt256 && (VT == MVT::v4i64 || VT == MVT::v4f64))
Craig Topper8325c112012-04-16 00:41:45 +00006989 return getTargetShuffleNode(X86ISD::VPERMI, dl, VT, V1,
Elena Demikhovsky73c504a2012-04-15 11:18:59 +00006990 getShuffleCLImmediate(SVOp), DAG);
6991
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00006992 //===--------------------------------------------------------------------===//
6993 // Since no target specific shuffle was selected for this generic one,
6994 // lower it into other known shuffles. FIXME: this isn't true yet, but
6995 // this is the plan.
6996 //
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00006997
Bruno Cardoso Lopes9b4ad122011-07-27 00:56:37 +00006998 // Handle v8i16 specifically since SSE can do byte extraction and insertion.
6999 if (VT == MVT::v8i16) {
Craig Topper55b24052012-09-11 06:15:32 +00007000 SDValue NewOp = LowerVECTOR_SHUFFLEv8i16(Op, Subtarget, DAG);
Bruno Cardoso Lopes9b4ad122011-07-27 00:56:37 +00007001 if (NewOp.getNode())
7002 return NewOp;
7003 }
7004
7005 if (VT == MVT::v16i8) {
7006 SDValue NewOp = LowerVECTOR_SHUFFLEv16i8(SVOp, DAG, *this);
7007 if (NewOp.getNode())
7008 return NewOp;
7009 }
7010
Elena Demikhovsky41789462012-09-06 12:42:01 +00007011 if (VT == MVT::v32i8) {
Craig Topper55b24052012-09-11 06:15:32 +00007012 SDValue NewOp = LowerVECTOR_SHUFFLEv32i8(SVOp, Subtarget, DAG);
Elena Demikhovsky41789462012-09-06 12:42:01 +00007013 if (NewOp.getNode())
7014 return NewOp;
7015 }
7016
Bruno Cardoso Lopes9b4ad122011-07-27 00:56:37 +00007017 // Handle all 128-bit wide vectors with 4 elements, and match them with
7018 // several different shuffle types.
Craig Topper7a9a28b2012-08-12 02:23:29 +00007019 if (NumElems == 4 && VT.is128BitVector())
Bruno Cardoso Lopes9b4ad122011-07-27 00:56:37 +00007020 return LowerVECTOR_SHUFFLE_128v4(SVOp, DAG);
7021
Bruno Cardoso Lopesd0888342011-07-22 00:14:56 +00007022 // Handle general 256-bit shuffles
7023 if (VT.is256BitVector())
7024 return LowerVECTOR_SHUFFLE_256(SVOp, DAG);
7025
Dan Gohman475871a2008-07-27 21:46:04 +00007026 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007027}
7028
Dan Gohman475871a2008-07-27 21:46:04 +00007029SDValue
7030X86TargetLowering::LowerEXTRACT_VECTOR_ELT_SSE4(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00007031 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00007032 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007033 DebugLoc dl = Op.getDebugLoc();
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007034
Craig Topper7a9a28b2012-08-12 02:23:29 +00007035 if (!Op.getOperand(0).getValueType().is128BitVector())
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007036 return SDValue();
7037
Duncan Sands83ec4b62008-06-06 12:08:01 +00007038 if (VT.getSizeInBits() == 8) {
Owen Anderson825b72b2009-08-11 20:47:22 +00007039 SDValue Extract = DAG.getNode(X86ISD::PEXTRB, dl, MVT::i32,
Craig Topper7c022842012-09-12 06:20:41 +00007040 Op.getOperand(0), Op.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00007041 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Extract,
Craig Topper7c022842012-09-12 06:20:41 +00007042 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00007043 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Craig Topper69947b92012-04-23 06:57:04 +00007044 }
7045
7046 if (VT.getSizeInBits() == 16) {
Evan Cheng52ceafa2009-01-02 05:29:08 +00007047 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
7048 // If Idx is 0, it's cheaper to do a move instead of a pextrw.
7049 if (Idx == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00007050 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i16,
7051 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007052 DAG.getNode(ISD::BITCAST, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00007053 MVT::v4i32,
Evan Cheng52ceafa2009-01-02 05:29:08 +00007054 Op.getOperand(0)),
7055 Op.getOperand(1)));
Owen Anderson825b72b2009-08-11 20:47:22 +00007056 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, dl, MVT::i32,
Craig Topper7c022842012-09-12 06:20:41 +00007057 Op.getOperand(0), Op.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00007058 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Extract,
Craig Topper7c022842012-09-12 06:20:41 +00007059 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00007060 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Craig Topper69947b92012-04-23 06:57:04 +00007061 }
7062
7063 if (VT == MVT::f32) {
Evan Cheng62a3f152008-03-24 21:52:23 +00007064 // EXTRACTPS outputs to a GPR32 register which will require a movd to copy
7065 // the result back to FR32 register. It's only worth matching if the
Dan Gohmand17cfbe2008-10-31 00:57:24 +00007066 // result has a single use which is a store or a bitcast to i32. And in
7067 // the case of a store, it's not worth it if the index is a constant 0,
7068 // because a MOVSSmr can be used instead, which is smaller and faster.
Evan Cheng62a3f152008-03-24 21:52:23 +00007069 if (!Op.hasOneUse())
Dan Gohman475871a2008-07-27 21:46:04 +00007070 return SDValue();
Gabor Greifba36cb52008-08-28 21:40:38 +00007071 SDNode *User = *Op.getNode()->use_begin();
Dan Gohmand17cfbe2008-10-31 00:57:24 +00007072 if ((User->getOpcode() != ISD::STORE ||
7073 (isa<ConstantSDNode>(Op.getOperand(1)) &&
7074 cast<ConstantSDNode>(Op.getOperand(1))->isNullValue())) &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007075 (User->getOpcode() != ISD::BITCAST ||
Owen Anderson825b72b2009-08-11 20:47:22 +00007076 User->getValueType(0) != MVT::i32))
Dan Gohman475871a2008-07-27 21:46:04 +00007077 return SDValue();
Owen Anderson825b72b2009-08-11 20:47:22 +00007078 SDValue Extract = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007079 DAG.getNode(ISD::BITCAST, dl, MVT::v4i32,
Dale Johannesenace16102009-02-03 19:33:06 +00007080 Op.getOperand(0)),
7081 Op.getOperand(1));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007082 return DAG.getNode(ISD::BITCAST, dl, MVT::f32, Extract);
Craig Topper69947b92012-04-23 06:57:04 +00007083 }
7084
7085 if (VT == MVT::i32 || VT == MVT::i64) {
Pete Coopera77214a2011-11-14 19:38:42 +00007086 // ExtractPS/pextrq works with constant index.
Mon P Wangf0fcdd82009-01-15 21:10:20 +00007087 if (isa<ConstantSDNode>(Op.getOperand(1)))
7088 return Op;
Nate Begeman14d12ca2008-02-11 04:19:36 +00007089 }
Dan Gohman475871a2008-07-27 21:46:04 +00007090 return SDValue();
Nate Begeman14d12ca2008-02-11 04:19:36 +00007091}
7092
Dan Gohman475871a2008-07-27 21:46:04 +00007093SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007094X86TargetLowering::LowerEXTRACT_VECTOR_ELT(SDValue Op,
7095 SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00007096 if (!isa<ConstantSDNode>(Op.getOperand(1)))
Dan Gohman475871a2008-07-27 21:46:04 +00007097 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007098
David Greene74a579d2011-02-10 16:57:36 +00007099 SDValue Vec = Op.getOperand(0);
7100 EVT VecVT = Vec.getValueType();
7101
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007102 // If this is a 256-bit vector result, first extract the 128-bit vector and
7103 // then extract the element from the 128-bit vector.
Craig Topper7a9a28b2012-08-12 02:23:29 +00007104 if (VecVT.is256BitVector()) {
David Greene74a579d2011-02-10 16:57:36 +00007105 DebugLoc dl = Op.getNode()->getDebugLoc();
7106 unsigned NumElems = VecVT.getVectorNumElements();
7107 SDValue Idx = Op.getOperand(1);
David Greene74a579d2011-02-10 16:57:36 +00007108 unsigned IdxVal = cast<ConstantSDNode>(Idx)->getZExtValue();
7109
7110 // Get the 128-bit vector.
Craig Topper7d1e3dc2012-04-30 05:17:10 +00007111 Vec = Extract128BitVector(Vec, IdxVal, DAG, dl);
David Greene74a579d2011-02-10 16:57:36 +00007112
Craig Topper7d1e3dc2012-04-30 05:17:10 +00007113 if (IdxVal >= NumElems/2)
7114 IdxVal -= NumElems/2;
David Greene74a579d2011-02-10 16:57:36 +00007115 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, Op.getValueType(), Vec,
Craig Topper7d1e3dc2012-04-30 05:17:10 +00007116 DAG.getConstant(IdxVal, MVT::i32));
David Greene74a579d2011-02-10 16:57:36 +00007117 }
7118
Craig Topper7a9a28b2012-08-12 02:23:29 +00007119 assert(VecVT.is128BitVector() && "Unexpected vector length");
David Greene74a579d2011-02-10 16:57:36 +00007120
Craig Topperd0a31172012-01-10 06:37:29 +00007121 if (Subtarget->hasSSE41()) {
Dan Gohman475871a2008-07-27 21:46:04 +00007122 SDValue Res = LowerEXTRACT_VECTOR_ELT_SSE4(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00007123 if (Res.getNode())
Evan Cheng62a3f152008-03-24 21:52:23 +00007124 return Res;
7125 }
Nate Begeman14d12ca2008-02-11 04:19:36 +00007126
Owen Andersone50ed302009-08-10 22:56:29 +00007127 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007128 DebugLoc dl = Op.getDebugLoc();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007129 // TODO: handle v16i8.
Duncan Sands83ec4b62008-06-06 12:08:01 +00007130 if (VT.getSizeInBits() == 16) {
Dan Gohman475871a2008-07-27 21:46:04 +00007131 SDValue Vec = Op.getOperand(0);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007132 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00007133 if (Idx == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00007134 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i16,
7135 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007136 DAG.getNode(ISD::BITCAST, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00007137 MVT::v4i32, Vec),
Evan Cheng14b32e12007-12-11 01:46:18 +00007138 Op.getOperand(1)));
Evan Cheng0db9fe62006-04-25 20:13:52 +00007139 // Transform it so it match pextrw which produces a 32-bit result.
Ken Dyck70d0ef12009-12-17 15:31:52 +00007140 EVT EltVT = MVT::i32;
Dan Gohman8a55ce42009-09-23 21:02:20 +00007141 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, dl, EltVT,
Craig Topper7c022842012-09-12 06:20:41 +00007142 Op.getOperand(0), Op.getOperand(1));
Dan Gohman8a55ce42009-09-23 21:02:20 +00007143 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, EltVT, Extract,
Craig Topper7c022842012-09-12 06:20:41 +00007144 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00007145 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Craig Topper69947b92012-04-23 06:57:04 +00007146 }
7147
7148 if (VT.getSizeInBits() == 32) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007149 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007150 if (Idx == 0)
7151 return Op;
Eric Christopherfd179292009-08-27 18:07:15 +00007152
Evan Cheng0db9fe62006-04-25 20:13:52 +00007153 // SHUFPS the element to the lowest double word, then movss.
Jeffrey Yasskina44defe2011-07-27 06:22:51 +00007154 int Mask[4] = { static_cast<int>(Idx), -1, -1, -1 };
Owen Andersone50ed302009-08-10 22:56:29 +00007155 EVT VVT = Op.getOperand(0).getValueType();
Eric Christopherfd179292009-08-27 18:07:15 +00007156 SDValue Vec = DAG.getVectorShuffle(VVT, dl, Op.getOperand(0),
Nate Begeman9008ca62009-04-27 18:41:29 +00007157 DAG.getUNDEF(VVT), Mask);
Dale Johannesenace16102009-02-03 19:33:06 +00007158 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, Vec,
Chris Lattner0bd48932008-01-17 07:00:52 +00007159 DAG.getIntPtrConstant(0));
Craig Topper69947b92012-04-23 06:57:04 +00007160 }
7161
7162 if (VT.getSizeInBits() == 64) {
Nate Begeman14d12ca2008-02-11 04:19:36 +00007163 // FIXME: .td only matches this for <2 x f64>, not <2 x i64> on 32b
7164 // FIXME: seems like this should be unnecessary if mov{h,l}pd were taught
7165 // to match extract_elt for f64.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007166 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007167 if (Idx == 0)
7168 return Op;
7169
7170 // UNPCKHPD the element to the lowest double word, then movsd.
7171 // Note if the lower 64 bits of the result of the UNPCKHPD is then stored
7172 // to a f64mem, the whole operation is folded into a single MOVHPDmr.
Nate Begeman9008ca62009-04-27 18:41:29 +00007173 int Mask[2] = { 1, -1 };
Owen Andersone50ed302009-08-10 22:56:29 +00007174 EVT VVT = Op.getOperand(0).getValueType();
Eric Christopherfd179292009-08-27 18:07:15 +00007175 SDValue Vec = DAG.getVectorShuffle(VVT, dl, Op.getOperand(0),
Nate Begeman9008ca62009-04-27 18:41:29 +00007176 DAG.getUNDEF(VVT), Mask);
Dale Johannesenace16102009-02-03 19:33:06 +00007177 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, Vec,
Chris Lattner0bd48932008-01-17 07:00:52 +00007178 DAG.getIntPtrConstant(0));
Evan Cheng0db9fe62006-04-25 20:13:52 +00007179 }
7180
Dan Gohman475871a2008-07-27 21:46:04 +00007181 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007182}
7183
Dan Gohman475871a2008-07-27 21:46:04 +00007184SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007185X86TargetLowering::LowerINSERT_VECTOR_ELT_SSE4(SDValue Op,
7186 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00007187 EVT VT = Op.getValueType();
Dan Gohman8a55ce42009-09-23 21:02:20 +00007188 EVT EltVT = VT.getVectorElementType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007189 DebugLoc dl = Op.getDebugLoc();
Nate Begeman14d12ca2008-02-11 04:19:36 +00007190
Dan Gohman475871a2008-07-27 21:46:04 +00007191 SDValue N0 = Op.getOperand(0);
7192 SDValue N1 = Op.getOperand(1);
7193 SDValue N2 = Op.getOperand(2);
Nate Begeman14d12ca2008-02-11 04:19:36 +00007194
Craig Topper7a9a28b2012-08-12 02:23:29 +00007195 if (!VT.is128BitVector())
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007196 return SDValue();
7197
Dan Gohman8a55ce42009-09-23 21:02:20 +00007198 if ((EltVT.getSizeInBits() == 8 || EltVT.getSizeInBits() == 16) &&
Dan Gohmanef521f12008-08-14 22:53:18 +00007199 isa<ConstantSDNode>(N2)) {
Chris Lattner8f2b4cc2010-02-23 02:07:48 +00007200 unsigned Opc;
7201 if (VT == MVT::v8i16)
7202 Opc = X86ISD::PINSRW;
Chris Lattner8f2b4cc2010-02-23 02:07:48 +00007203 else if (VT == MVT::v16i8)
7204 Opc = X86ISD::PINSRB;
7205 else
7206 Opc = X86ISD::PINSRB;
7207
Nate Begeman14d12ca2008-02-11 04:19:36 +00007208 // Transform it so it match pinsr{b,w} which expects a GR32 as its second
7209 // argument.
Owen Anderson825b72b2009-08-11 20:47:22 +00007210 if (N1.getValueType() != MVT::i32)
7211 N1 = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, N1);
7212 if (N2.getValueType() != MVT::i32)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007213 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Dale Johannesenace16102009-02-03 19:33:06 +00007214 return DAG.getNode(Opc, dl, VT, N0, N1, N2);
Craig Topper69947b92012-04-23 06:57:04 +00007215 }
7216
7217 if (EltVT == MVT::f32 && isa<ConstantSDNode>(N2)) {
Nate Begeman14d12ca2008-02-11 04:19:36 +00007218 // Bits [7:6] of the constant are the source select. This will always be
7219 // zero here. The DAG Combiner may combine an extract_elt index into these
7220 // bits. For example (insert (extract, 3), 2) could be matched by putting
7221 // the '3' into bits [7:6] of X86ISD::INSERTPS.
Scott Michelfdc40a02009-02-17 22:15:04 +00007222 // Bits [5:4] of the constant are the destination select. This is the
Nate Begeman14d12ca2008-02-11 04:19:36 +00007223 // value of the incoming immediate.
Scott Michelfdc40a02009-02-17 22:15:04 +00007224 // Bits [3:0] of the constant are the zero mask. The DAG Combiner may
Nate Begeman14d12ca2008-02-11 04:19:36 +00007225 // combine either bitwise AND or insert of float 0.0 to set these bits.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007226 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue() << 4);
Eric Christopherfbd66872009-07-24 00:33:09 +00007227 // Create this as a scalar to vector..
Owen Anderson825b72b2009-08-11 20:47:22 +00007228 N1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4f32, N1);
Dale Johannesenace16102009-02-03 19:33:06 +00007229 return DAG.getNode(X86ISD::INSERTPS, dl, VT, N0, N1, N2);
Craig Topper69947b92012-04-23 06:57:04 +00007230 }
7231
7232 if ((EltVT == MVT::i32 || EltVT == MVT::i64) && isa<ConstantSDNode>(N2)) {
Eric Christopherfbd66872009-07-24 00:33:09 +00007233 // PINSR* works with constant index.
7234 return Op;
Nate Begeman14d12ca2008-02-11 04:19:36 +00007235 }
Dan Gohman475871a2008-07-27 21:46:04 +00007236 return SDValue();
Nate Begeman14d12ca2008-02-11 04:19:36 +00007237}
7238
Dan Gohman475871a2008-07-27 21:46:04 +00007239SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007240X86TargetLowering::LowerINSERT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00007241 EVT VT = Op.getValueType();
Dan Gohman8a55ce42009-09-23 21:02:20 +00007242 EVT EltVT = VT.getVectorElementType();
Nate Begeman14d12ca2008-02-11 04:19:36 +00007243
David Greene6b381262011-02-09 15:32:06 +00007244 DebugLoc dl = Op.getDebugLoc();
7245 SDValue N0 = Op.getOperand(0);
7246 SDValue N1 = Op.getOperand(1);
7247 SDValue N2 = Op.getOperand(2);
7248
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007249 // If this is a 256-bit vector result, first extract the 128-bit vector,
7250 // insert the element into the extracted half and then place it back.
Craig Topper7a9a28b2012-08-12 02:23:29 +00007251 if (VT.is256BitVector()) {
David Greene6b381262011-02-09 15:32:06 +00007252 if (!isa<ConstantSDNode>(N2))
7253 return SDValue();
7254
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007255 // Get the desired 128-bit vector half.
David Greene6b381262011-02-09 15:32:06 +00007256 unsigned NumElems = VT.getVectorNumElements();
7257 unsigned IdxVal = cast<ConstantSDNode>(N2)->getZExtValue();
Craig Topper7d1e3dc2012-04-30 05:17:10 +00007258 SDValue V = Extract128BitVector(N0, IdxVal, DAG, dl);
David Greene6b381262011-02-09 15:32:06 +00007259
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007260 // Insert the element into the desired half.
Craig Topper7d1e3dc2012-04-30 05:17:10 +00007261 bool Upper = IdxVal >= NumElems/2;
7262 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, V.getValueType(), V, N1,
7263 DAG.getConstant(Upper ? IdxVal-NumElems/2 : IdxVal, MVT::i32));
David Greene6b381262011-02-09 15:32:06 +00007264
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007265 // Insert the changed part back to the 256-bit vector
Craig Topper7d1e3dc2012-04-30 05:17:10 +00007266 return Insert128BitVector(N0, V, IdxVal, DAG, dl);
David Greene6b381262011-02-09 15:32:06 +00007267 }
7268
Craig Topperd0a31172012-01-10 06:37:29 +00007269 if (Subtarget->hasSSE41())
Nate Begeman14d12ca2008-02-11 04:19:36 +00007270 return LowerINSERT_VECTOR_ELT_SSE4(Op, DAG);
7271
Dan Gohman8a55ce42009-09-23 21:02:20 +00007272 if (EltVT == MVT::i8)
Dan Gohman475871a2008-07-27 21:46:04 +00007273 return SDValue();
Evan Cheng794405e2007-12-12 07:55:34 +00007274
Dan Gohman8a55ce42009-09-23 21:02:20 +00007275 if (EltVT.getSizeInBits() == 16 && isa<ConstantSDNode>(N2)) {
Evan Cheng794405e2007-12-12 07:55:34 +00007276 // Transform it so it match pinsrw which expects a 16-bit value in a GR32
7277 // as its second argument.
Owen Anderson825b72b2009-08-11 20:47:22 +00007278 if (N1.getValueType() != MVT::i32)
7279 N1 = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, N1);
7280 if (N2.getValueType() != MVT::i32)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007281 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Dale Johannesen0488fb62010-09-30 23:57:10 +00007282 return DAG.getNode(X86ISD::PINSRW, dl, VT, N0, N1, N2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007283 }
Dan Gohman475871a2008-07-27 21:46:04 +00007284 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007285}
7286
Craig Topper55b24052012-09-11 06:15:32 +00007287static SDValue LowerSCALAR_TO_VECTOR(SDValue Op, SelectionDAG &DAG) {
Bruno Cardoso Lopes233fa392011-07-25 23:05:16 +00007288 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007289 DebugLoc dl = Op.getDebugLoc();
David Greene2fcdfb42011-02-10 23:11:29 +00007290 EVT OpVT = Op.getValueType();
7291
Bruno Cardoso Lopes233fa392011-07-25 23:05:16 +00007292 // If this is a 256-bit vector result, first insert into a 128-bit
7293 // vector and then insert into the 256-bit vector.
Craig Topper7a9a28b2012-08-12 02:23:29 +00007294 if (!OpVT.is128BitVector()) {
Bruno Cardoso Lopes233fa392011-07-25 23:05:16 +00007295 // Insert into a 128-bit vector.
7296 EVT VT128 = EVT::getVectorVT(*Context,
7297 OpVT.getVectorElementType(),
7298 OpVT.getVectorNumElements() / 2);
7299
7300 Op = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT128, Op.getOperand(0));
7301
7302 // Insert the 128-bit vector.
Craig Topperb14940a2012-04-22 20:55:18 +00007303 return Insert128BitVector(DAG.getUNDEF(OpVT), Op, 0, DAG, dl);
Bruno Cardoso Lopes233fa392011-07-25 23:05:16 +00007304 }
7305
Craig Topperd77d2fe2012-04-29 20:22:05 +00007306 if (OpVT == MVT::v1i64 &&
Chris Lattnerf172ecd2010-07-04 23:07:25 +00007307 Op.getOperand(0).getValueType() == MVT::i64)
Owen Anderson825b72b2009-08-11 20:47:22 +00007308 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v1i64, Op.getOperand(0));
Rafael Espindoladef390a2009-08-03 02:45:34 +00007309
Owen Anderson825b72b2009-08-11 20:47:22 +00007310 SDValue AnyExt = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, Op.getOperand(0));
Craig Topper7a9a28b2012-08-12 02:23:29 +00007311 assert(OpVT.is128BitVector() && "Expected an SSE type!");
Craig Topperd77d2fe2012-04-29 20:22:05 +00007312 return DAG.getNode(ISD::BITCAST, dl, OpVT,
Dale Johannesen0488fb62010-09-30 23:57:10 +00007313 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,AnyExt));
Evan Cheng0db9fe62006-04-25 20:13:52 +00007314}
7315
David Greene91585092011-01-26 15:38:49 +00007316// Lower a node with an EXTRACT_SUBVECTOR opcode. This may result in
7317// a simple subregister reference or explicit instructions to grab
7318// upper bits of a vector.
Craig Topper55b24052012-09-11 06:15:32 +00007319static SDValue LowerEXTRACT_SUBVECTOR(SDValue Op, const X86Subtarget *Subtarget,
7320 SelectionDAG &DAG) {
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00007321 if (Subtarget->hasFp256()) {
David Greenea5f26012011-02-07 19:36:54 +00007322 DebugLoc dl = Op.getNode()->getDebugLoc();
7323 SDValue Vec = Op.getNode()->getOperand(0);
7324 SDValue Idx = Op.getNode()->getOperand(1);
7325
Craig Topper7a9a28b2012-08-12 02:23:29 +00007326 if (Op.getNode()->getValueType(0).is128BitVector() &&
7327 Vec.getNode()->getValueType(0).is256BitVector() &&
Craig Topperb14940a2012-04-22 20:55:18 +00007328 isa<ConstantSDNode>(Idx)) {
7329 unsigned IdxVal = cast<ConstantSDNode>(Idx)->getZExtValue();
7330 return Extract128BitVector(Vec, IdxVal, DAG, dl);
David Greenea5f26012011-02-07 19:36:54 +00007331 }
David Greene91585092011-01-26 15:38:49 +00007332 }
7333 return SDValue();
7334}
7335
David Greenecfe33c42011-01-26 19:13:22 +00007336// Lower a node with an INSERT_SUBVECTOR opcode. This may result in a
7337// simple superregister reference or explicit instructions to insert
7338// the upper bits of a vector.
Craig Topper55b24052012-09-11 06:15:32 +00007339static SDValue LowerINSERT_SUBVECTOR(SDValue Op, const X86Subtarget *Subtarget,
7340 SelectionDAG &DAG) {
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00007341 if (Subtarget->hasFp256()) {
David Greenecfe33c42011-01-26 19:13:22 +00007342 DebugLoc dl = Op.getNode()->getDebugLoc();
7343 SDValue Vec = Op.getNode()->getOperand(0);
7344 SDValue SubVec = Op.getNode()->getOperand(1);
7345 SDValue Idx = Op.getNode()->getOperand(2);
7346
Craig Topper7a9a28b2012-08-12 02:23:29 +00007347 if (Op.getNode()->getValueType(0).is256BitVector() &&
7348 SubVec.getNode()->getValueType(0).is128BitVector() &&
Craig Topperb14940a2012-04-22 20:55:18 +00007349 isa<ConstantSDNode>(Idx)) {
7350 unsigned IdxVal = cast<ConstantSDNode>(Idx)->getZExtValue();
7351 return Insert128BitVector(Vec, SubVec, IdxVal, DAG, dl);
David Greenecfe33c42011-01-26 19:13:22 +00007352 }
7353 }
7354 return SDValue();
7355}
7356
Bill Wendling056292f2008-09-16 21:48:12 +00007357// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
7358// their target countpart wrapped in the X86ISD::Wrapper node. Suppose N is
7359// one of the above mentioned nodes. It has to be wrapped because otherwise
7360// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
7361// be used to form addressing mode. These wrapped nodes will be selected
7362// into MOV32ri.
Dan Gohman475871a2008-07-27 21:46:04 +00007363SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007364X86TargetLowering::LowerConstantPool(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00007365 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Eric Christopherfd179292009-08-27 18:07:15 +00007366
Chris Lattner41621a22009-06-26 19:22:52 +00007367 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
7368 // global base reg.
7369 unsigned char OpFlag = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00007370 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007371 CodeModel::Model M = getTargetMachine().getCodeModel();
7372
Chris Lattner4f066492009-07-11 20:29:19 +00007373 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007374 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattnere4df7562009-07-09 03:15:51 +00007375 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00007376 else if (Subtarget->isPICStyleGOT())
Chris Lattner88e1fd52009-07-09 04:24:46 +00007377 OpFlag = X86II::MO_GOTOFF;
Chris Lattnere2c92082009-07-10 21:00:45 +00007378 else if (Subtarget->isPICStyleStubPIC())
Chris Lattner88e1fd52009-07-09 04:24:46 +00007379 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Eric Christopherfd179292009-08-27 18:07:15 +00007380
Evan Cheng1606e8e2009-03-13 07:51:59 +00007381 SDValue Result = DAG.getTargetConstantPool(CP->getConstVal(), getPointerTy(),
Chris Lattner41621a22009-06-26 19:22:52 +00007382 CP->getAlignment(),
7383 CP->getOffset(), OpFlag);
7384 DebugLoc DL = CP->getDebugLoc();
Chris Lattner18c59872009-06-27 04:16:01 +00007385 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00007386 // With PIC, the address is actually $g + Offset.
Chris Lattner41621a22009-06-26 19:22:52 +00007387 if (OpFlag) {
7388 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesenb300d2a2009-02-07 00:55:49 +00007389 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00007390 DebugLoc(), getPointerTy()),
Anton Korobeynikov7f705592007-01-12 19:20:47 +00007391 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007392 }
7393
7394 return Result;
7395}
7396
Dan Gohmand858e902010-04-17 15:26:15 +00007397SDValue X86TargetLowering::LowerJumpTable(SDValue Op, SelectionDAG &DAG) const {
Chris Lattner18c59872009-06-27 04:16:01 +00007398 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
Eric Christopherfd179292009-08-27 18:07:15 +00007399
Chris Lattner18c59872009-06-27 04:16:01 +00007400 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
7401 // global base reg.
7402 unsigned char OpFlag = 0;
7403 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007404 CodeModel::Model M = getTargetMachine().getCodeModel();
7405
Chris Lattner4f066492009-07-11 20:29:19 +00007406 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007407 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattnere4df7562009-07-09 03:15:51 +00007408 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00007409 else if (Subtarget->isPICStyleGOT())
Chris Lattner88e1fd52009-07-09 04:24:46 +00007410 OpFlag = X86II::MO_GOTOFF;
Chris Lattnere2c92082009-07-10 21:00:45 +00007411 else if (Subtarget->isPICStyleStubPIC())
Chris Lattner88e1fd52009-07-09 04:24:46 +00007412 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Eric Christopherfd179292009-08-27 18:07:15 +00007413
Chris Lattner18c59872009-06-27 04:16:01 +00007414 SDValue Result = DAG.getTargetJumpTable(JT->getIndex(), getPointerTy(),
7415 OpFlag);
7416 DebugLoc DL = JT->getDebugLoc();
7417 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Eric Christopherfd179292009-08-27 18:07:15 +00007418
Chris Lattner18c59872009-06-27 04:16:01 +00007419 // With PIC, the address is actually $g + Offset.
Chris Lattner1e61e692010-11-15 02:46:57 +00007420 if (OpFlag)
Chris Lattner18c59872009-06-27 04:16:01 +00007421 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
7422 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00007423 DebugLoc(), getPointerTy()),
Chris Lattner18c59872009-06-27 04:16:01 +00007424 Result);
Eric Christopherfd179292009-08-27 18:07:15 +00007425
Chris Lattner18c59872009-06-27 04:16:01 +00007426 return Result;
7427}
7428
7429SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007430X86TargetLowering::LowerExternalSymbol(SDValue Op, SelectionDAG &DAG) const {
Chris Lattner18c59872009-06-27 04:16:01 +00007431 const char *Sym = cast<ExternalSymbolSDNode>(Op)->getSymbol();
Eric Christopherfd179292009-08-27 18:07:15 +00007432
Chris Lattner18c59872009-06-27 04:16:01 +00007433 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
7434 // global base reg.
7435 unsigned char OpFlag = 0;
7436 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007437 CodeModel::Model M = getTargetMachine().getCodeModel();
7438
Chris Lattner4f066492009-07-11 20:29:19 +00007439 if (Subtarget->isPICStyleRIPRel() &&
Eli Friedman586272d2011-08-11 01:48:05 +00007440 (M == CodeModel::Small || M == CodeModel::Kernel)) {
7441 if (Subtarget->isTargetDarwin() || Subtarget->isTargetELF())
7442 OpFlag = X86II::MO_GOTPCREL;
Chris Lattnere4df7562009-07-09 03:15:51 +00007443 WrapperKind = X86ISD::WrapperRIP;
Eli Friedman586272d2011-08-11 01:48:05 +00007444 } else if (Subtarget->isPICStyleGOT()) {
7445 OpFlag = X86II::MO_GOT;
7446 } else if (Subtarget->isPICStyleStubPIC()) {
7447 OpFlag = X86II::MO_DARWIN_NONLAZY_PIC_BASE;
7448 } else if (Subtarget->isPICStyleStubNoDynamic()) {
7449 OpFlag = X86II::MO_DARWIN_NONLAZY;
7450 }
Eric Christopherfd179292009-08-27 18:07:15 +00007451
Chris Lattner18c59872009-06-27 04:16:01 +00007452 SDValue Result = DAG.getTargetExternalSymbol(Sym, getPointerTy(), OpFlag);
Eric Christopherfd179292009-08-27 18:07:15 +00007453
Chris Lattner18c59872009-06-27 04:16:01 +00007454 DebugLoc DL = Op.getDebugLoc();
7455 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Eric Christopherfd179292009-08-27 18:07:15 +00007456
Chris Lattner18c59872009-06-27 04:16:01 +00007457 // With PIC, the address is actually $g + Offset.
7458 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
Chris Lattnere4df7562009-07-09 03:15:51 +00007459 !Subtarget->is64Bit()) {
Chris Lattner18c59872009-06-27 04:16:01 +00007460 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
7461 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00007462 DebugLoc(), getPointerTy()),
Chris Lattner18c59872009-06-27 04:16:01 +00007463 Result);
7464 }
Eric Christopherfd179292009-08-27 18:07:15 +00007465
Eli Friedman586272d2011-08-11 01:48:05 +00007466 // For symbols that require a load from a stub to get the address, emit the
7467 // load.
7468 if (isGlobalStubReference(OpFlag))
7469 Result = DAG.getLoad(getPointerTy(), DL, DAG.getEntryNode(), Result,
Pete Cooperd752e0f2011-11-08 18:42:53 +00007470 MachinePointerInfo::getGOT(), false, false, false, 0);
Eli Friedman586272d2011-08-11 01:48:05 +00007471
Chris Lattner18c59872009-06-27 04:16:01 +00007472 return Result;
7473}
7474
Dan Gohman475871a2008-07-27 21:46:04 +00007475SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007476X86TargetLowering::LowerBlockAddress(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman29cbade2009-11-20 23:18:13 +00007477 // Create the TargetBlockAddressAddress node.
7478 unsigned char OpFlags =
7479 Subtarget->ClassifyBlockAddressReference();
Dan Gohmanf705adb2009-10-30 01:28:02 +00007480 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman46510a72010-04-15 01:51:59 +00007481 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
Michael Liao6c7ccaa2012-09-12 21:43:09 +00007482 int64_t Offset = cast<BlockAddressSDNode>(Op)->getOffset();
Dan Gohman29cbade2009-11-20 23:18:13 +00007483 DebugLoc dl = Op.getDebugLoc();
Michael Liao6c7ccaa2012-09-12 21:43:09 +00007484 SDValue Result = DAG.getTargetBlockAddress(BA, getPointerTy(), Offset,
7485 OpFlags);
Dan Gohman29cbade2009-11-20 23:18:13 +00007486
Dan Gohmanf705adb2009-10-30 01:28:02 +00007487 if (Subtarget->isPICStyleRIPRel() &&
7488 (M == CodeModel::Small || M == CodeModel::Kernel))
Dan Gohman29cbade2009-11-20 23:18:13 +00007489 Result = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Result);
7490 else
7491 Result = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), Result);
Dan Gohmanf705adb2009-10-30 01:28:02 +00007492
Dan Gohman29cbade2009-11-20 23:18:13 +00007493 // With PIC, the address is actually $g + Offset.
7494 if (isGlobalRelativeToPICBase(OpFlags)) {
7495 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(),
7496 DAG.getNode(X86ISD::GlobalBaseReg, dl, getPointerTy()),
7497 Result);
7498 }
Dan Gohmanf705adb2009-10-30 01:28:02 +00007499
7500 return Result;
7501}
7502
7503SDValue
Dale Johannesen33c960f2009-02-04 20:06:27 +00007504X86TargetLowering::LowerGlobalAddress(const GlobalValue *GV, DebugLoc dl,
Dan Gohman6520e202008-10-18 02:06:02 +00007505 int64_t Offset,
Evan Chengda43bcf2008-09-24 00:05:32 +00007506 SelectionDAG &DAG) const {
Dan Gohman6520e202008-10-18 02:06:02 +00007507 // Create the TargetGlobalAddress node, folding in the constant
7508 // offset if it is legal.
Chris Lattnerd392bd92009-07-10 07:20:05 +00007509 unsigned char OpFlags =
7510 Subtarget->ClassifyGlobalReference(GV, getTargetMachine());
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007511 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman6520e202008-10-18 02:06:02 +00007512 SDValue Result;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007513 if (OpFlags == X86II::MO_NO_FLAG &&
7514 X86::isOffsetSuitableForCodeModel(Offset, M)) {
Chris Lattner4aa21aa2009-07-09 00:58:53 +00007515 // A direct static reference to a global.
Devang Patel0d881da2010-07-06 22:08:15 +00007516 Result = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(), Offset);
Dan Gohman6520e202008-10-18 02:06:02 +00007517 Offset = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00007518 } else {
Devang Patel0d881da2010-07-06 22:08:15 +00007519 Result = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(), 0, OpFlags);
Chris Lattner18c59872009-06-27 04:16:01 +00007520 }
Eric Christopherfd179292009-08-27 18:07:15 +00007521
Chris Lattner4f066492009-07-11 20:29:19 +00007522 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007523 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattner18c59872009-06-27 04:16:01 +00007524 Result = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Result);
7525 else
7526 Result = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), Result);
Dan Gohman6520e202008-10-18 02:06:02 +00007527
Anton Korobeynikov7f705592007-01-12 19:20:47 +00007528 // With PIC, the address is actually $g + Offset.
Chris Lattner36c25012009-07-10 07:34:39 +00007529 if (isGlobalRelativeToPICBase(OpFlags)) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00007530 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(),
7531 DAG.getNode(X86ISD::GlobalBaseReg, dl, getPointerTy()),
Anton Korobeynikov7f705592007-01-12 19:20:47 +00007532 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007533 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007534
Chris Lattner36c25012009-07-10 07:34:39 +00007535 // For globals that require a load from a stub to get the address, emit the
7536 // load.
7537 if (isGlobalStubReference(OpFlags))
Dale Johannesen33c960f2009-02-04 20:06:27 +00007538 Result = DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(), Result,
Pete Cooperd752e0f2011-11-08 18:42:53 +00007539 MachinePointerInfo::getGOT(), false, false, false, 0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007540
Dan Gohman6520e202008-10-18 02:06:02 +00007541 // If there was a non-zero offset that we didn't fold, create an explicit
7542 // addition for it.
7543 if (Offset != 0)
Dale Johannesen33c960f2009-02-04 20:06:27 +00007544 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(), Result,
Dan Gohman6520e202008-10-18 02:06:02 +00007545 DAG.getConstant(Offset, getPointerTy()));
7546
Evan Cheng0db9fe62006-04-25 20:13:52 +00007547 return Result;
7548}
7549
Evan Chengda43bcf2008-09-24 00:05:32 +00007550SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007551X86TargetLowering::LowerGlobalAddress(SDValue Op, SelectionDAG &DAG) const {
Evan Chengda43bcf2008-09-24 00:05:32 +00007552 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Dan Gohman6520e202008-10-18 02:06:02 +00007553 int64_t Offset = cast<GlobalAddressSDNode>(Op)->getOffset();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007554 return LowerGlobalAddress(GV, Op.getDebugLoc(), Offset, DAG);
Evan Chengda43bcf2008-09-24 00:05:32 +00007555}
7556
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007557static SDValue
7558GetTLSADDR(SelectionDAG &DAG, SDValue Chain, GlobalAddressSDNode *GA,
Owen Andersone50ed302009-08-10 22:56:29 +00007559 SDValue *InFlag, const EVT PtrVT, unsigned ReturnReg,
Hans Wennborgf0234fc2012-06-01 16:27:21 +00007560 unsigned char OperandFlags, bool LocalDynamic = false) {
Anton Korobeynikov817a4642009-12-11 19:39:55 +00007561 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00007562 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007563 DebugLoc dl = GA->getDebugLoc();
Devang Patel0d881da2010-07-06 22:08:15 +00007564 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl,
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007565 GA->getValueType(0),
Chris Lattnerb903bed2009-06-26 21:20:29 +00007566 GA->getOffset(),
7567 OperandFlags);
Hans Wennborgf0234fc2012-06-01 16:27:21 +00007568
7569 X86ISD::NodeType CallType = LocalDynamic ? X86ISD::TLSBASEADDR
7570 : X86ISD::TLSADDR;
7571
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007572 if (InFlag) {
7573 SDValue Ops[] = { Chain, TGA, *InFlag };
Hans Wennborgf0234fc2012-06-01 16:27:21 +00007574 Chain = DAG.getNode(CallType, dl, NodeTys, Ops, 3);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007575 } else {
7576 SDValue Ops[] = { Chain, TGA };
Hans Wennborgf0234fc2012-06-01 16:27:21 +00007577 Chain = DAG.getNode(CallType, dl, NodeTys, Ops, 2);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007578 }
Anton Korobeynikov817a4642009-12-11 19:39:55 +00007579
7580 // TLSADDR will be codegen'ed as call. Inform MFI that function has calls.
Bill Wendlingb92187a2010-05-14 21:14:32 +00007581 MFI->setAdjustsStack(true);
Anton Korobeynikov817a4642009-12-11 19:39:55 +00007582
Rafael Espindola15f1b662009-04-24 12:59:40 +00007583 SDValue Flag = Chain.getValue(1);
7584 return DAG.getCopyFromReg(Chain, dl, ReturnReg, PtrVT, Flag);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007585}
7586
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007587// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 32 bit
Dan Gohman475871a2008-07-27 21:46:04 +00007588static SDValue
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007589LowerToTLSGeneralDynamicModel32(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00007590 const EVT PtrVT) {
Dan Gohman475871a2008-07-27 21:46:04 +00007591 SDValue InFlag;
Dale Johannesendd64c412009-02-04 00:33:20 +00007592 DebugLoc dl = GA->getDebugLoc(); // ? function entry point might be better
7593 SDValue Chain = DAG.getCopyToReg(DAG.getEntryNode(), dl, X86::EBX,
Craig Topper7c022842012-09-12 06:20:41 +00007594 DAG.getNode(X86ISD::GlobalBaseReg,
7595 DebugLoc(), PtrVT), InFlag);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007596 InFlag = Chain.getValue(1);
7597
Chris Lattnerb903bed2009-06-26 21:20:29 +00007598 return GetTLSADDR(DAG, Chain, GA, &InFlag, PtrVT, X86::EAX, X86II::MO_TLSGD);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007599}
7600
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007601// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 64 bit
Dan Gohman475871a2008-07-27 21:46:04 +00007602static SDValue
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007603LowerToTLSGeneralDynamicModel64(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00007604 const EVT PtrVT) {
Chris Lattnerb903bed2009-06-26 21:20:29 +00007605 return GetTLSADDR(DAG, DAG.getEntryNode(), GA, NULL, PtrVT,
7606 X86::RAX, X86II::MO_TLSGD);
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007607}
7608
Hans Wennborgf0234fc2012-06-01 16:27:21 +00007609static SDValue LowerToTLSLocalDynamicModel(GlobalAddressSDNode *GA,
7610 SelectionDAG &DAG,
7611 const EVT PtrVT,
7612 bool is64Bit) {
7613 DebugLoc dl = GA->getDebugLoc();
7614
7615 // Get the start address of the TLS block for this module.
7616 X86MachineFunctionInfo* MFI = DAG.getMachineFunction()
7617 .getInfo<X86MachineFunctionInfo>();
7618 MFI->incNumLocalDynamicTLSAccesses();
7619
7620 SDValue Base;
7621 if (is64Bit) {
7622 Base = GetTLSADDR(DAG, DAG.getEntryNode(), GA, NULL, PtrVT, X86::RAX,
7623 X86II::MO_TLSLD, /*LocalDynamic=*/true);
7624 } else {
7625 SDValue InFlag;
7626 SDValue Chain = DAG.getCopyToReg(DAG.getEntryNode(), dl, X86::EBX,
7627 DAG.getNode(X86ISD::GlobalBaseReg, DebugLoc(), PtrVT), InFlag);
7628 InFlag = Chain.getValue(1);
7629 Base = GetTLSADDR(DAG, Chain, GA, &InFlag, PtrVT, X86::EAX,
7630 X86II::MO_TLSLDM, /*LocalDynamic=*/true);
7631 }
7632
7633 // Note: the CleanupLocalDynamicTLSPass will remove redundant computations
7634 // of Base.
7635
7636 // Build x@dtpoff.
7637 unsigned char OperandFlags = X86II::MO_DTPOFF;
7638 unsigned WrapperKind = X86ISD::Wrapper;
7639 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl,
7640 GA->getValueType(0),
7641 GA->getOffset(), OperandFlags);
7642 SDValue Offset = DAG.getNode(WrapperKind, dl, PtrVT, TGA);
7643
7644 // Add x@dtpoff with the base.
7645 return DAG.getNode(ISD::ADD, dl, PtrVT, Offset, Base);
7646}
7647
Hans Wennborg228756c2012-05-11 10:11:01 +00007648// Lower ISD::GlobalTLSAddress using the "initial exec" or "local exec" model.
Dan Gohman475871a2008-07-27 21:46:04 +00007649static SDValue LowerToTLSExecModel(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00007650 const EVT PtrVT, TLSModel::Model model,
Hans Wennborg228756c2012-05-11 10:11:01 +00007651 bool is64Bit, bool isPIC) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00007652 DebugLoc dl = GA->getDebugLoc();
Michael J. Spencerec38de22010-10-10 22:04:20 +00007653
Chris Lattnerf93b90c2010-09-22 04:39:11 +00007654 // Get the Thread Pointer, which is %gs:0 (32-bit) or %fs:0 (64-bit).
7655 Value *Ptr = Constant::getNullValue(Type::getInt8PtrTy(*DAG.getContext(),
7656 is64Bit ? 257 : 256));
Rafael Espindola094fad32009-04-08 21:14:34 +00007657
Michael J. Spencerec38de22010-10-10 22:04:20 +00007658 SDValue ThreadPointer = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(),
Chris Lattnerf93b90c2010-09-22 04:39:11 +00007659 DAG.getIntPtrConstant(0),
Pete Cooperd752e0f2011-11-08 18:42:53 +00007660 MachinePointerInfo(Ptr),
7661 false, false, false, 0);
Rafael Espindola094fad32009-04-08 21:14:34 +00007662
Chris Lattnerb903bed2009-06-26 21:20:29 +00007663 unsigned char OperandFlags = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00007664 // Most TLS accesses are not RIP relative, even on x86-64. One exception is
7665 // initialexec.
7666 unsigned WrapperKind = X86ISD::Wrapper;
7667 if (model == TLSModel::LocalExec) {
Chris Lattnerb903bed2009-06-26 21:20:29 +00007668 OperandFlags = is64Bit ? X86II::MO_TPOFF : X86II::MO_NTPOFF;
Hans Wennborg228756c2012-05-11 10:11:01 +00007669 } else if (model == TLSModel::InitialExec) {
7670 if (is64Bit) {
7671 OperandFlags = X86II::MO_GOTTPOFF;
7672 WrapperKind = X86ISD::WrapperRIP;
7673 } else {
7674 OperandFlags = isPIC ? X86II::MO_GOTNTPOFF : X86II::MO_INDNTPOFF;
7675 }
Chris Lattner18c59872009-06-27 04:16:01 +00007676 } else {
Hans Wennborg228756c2012-05-11 10:11:01 +00007677 llvm_unreachable("Unexpected model");
Chris Lattnerb903bed2009-06-26 21:20:29 +00007678 }
Eric Christopherfd179292009-08-27 18:07:15 +00007679
Hans Wennborg228756c2012-05-11 10:11:01 +00007680 // emit "addl x@ntpoff,%eax" (local exec)
7681 // or "addl x@indntpoff,%eax" (initial exec)
7682 // or "addl x@gotntpoff(%ebx) ,%eax" (initial exec, 32-bit pic)
Michael J. Spencerec38de22010-10-10 22:04:20 +00007683 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl,
Devang Patel0d881da2010-07-06 22:08:15 +00007684 GA->getValueType(0),
Chris Lattnerb903bed2009-06-26 21:20:29 +00007685 GA->getOffset(), OperandFlags);
Chris Lattner18c59872009-06-27 04:16:01 +00007686 SDValue Offset = DAG.getNode(WrapperKind, dl, PtrVT, TGA);
Lauro Ramos Venancio7d2cc2b2007-04-22 22:50:52 +00007687
Hans Wennborg228756c2012-05-11 10:11:01 +00007688 if (model == TLSModel::InitialExec) {
7689 if (isPIC && !is64Bit) {
7690 Offset = DAG.getNode(ISD::ADD, dl, PtrVT,
7691 DAG.getNode(X86ISD::GlobalBaseReg, DebugLoc(), PtrVT),
7692 Offset);
Hans Wennborg228756c2012-05-11 10:11:01 +00007693 }
Rafael Espindola94e3b382012-06-29 04:22:35 +00007694
7695 Offset = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Offset,
7696 MachinePointerInfo::getGOT(), false, false, false,
7697 0);
Hans Wennborg228756c2012-05-11 10:11:01 +00007698 }
Lauro Ramos Venancio7d2cc2b2007-04-22 22:50:52 +00007699
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007700 // The address of the thread local variable is the add of the thread
7701 // pointer with the offset of the variable.
Dale Johannesen33c960f2009-02-04 20:06:27 +00007702 return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007703}
7704
Dan Gohman475871a2008-07-27 21:46:04 +00007705SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007706X86TargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const {
Michael J. Spencerec38de22010-10-10 22:04:20 +00007707
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007708 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
Chris Lattnerb903bed2009-06-26 21:20:29 +00007709 const GlobalValue *GV = GA->getGlobal();
Eric Christopherfd179292009-08-27 18:07:15 +00007710
Eric Christopher30ef0e52010-06-03 04:07:48 +00007711 if (Subtarget->isTargetELF()) {
Chandler Carruth34797132012-04-08 17:20:55 +00007712 TLSModel::Model model = getTargetMachine().getTLSModel(GV);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007713
Eric Christopher30ef0e52010-06-03 04:07:48 +00007714 switch (model) {
7715 case TLSModel::GeneralDynamic:
Eric Christopher30ef0e52010-06-03 04:07:48 +00007716 if (Subtarget->is64Bit())
7717 return LowerToTLSGeneralDynamicModel64(GA, DAG, getPointerTy());
7718 return LowerToTLSGeneralDynamicModel32(GA, DAG, getPointerTy());
Hans Wennborgf0234fc2012-06-01 16:27:21 +00007719 case TLSModel::LocalDynamic:
7720 return LowerToTLSLocalDynamicModel(GA, DAG, getPointerTy(),
7721 Subtarget->is64Bit());
Eric Christopher30ef0e52010-06-03 04:07:48 +00007722 case TLSModel::InitialExec:
7723 case TLSModel::LocalExec:
7724 return LowerToTLSExecModel(GA, DAG, getPointerTy(), model,
Hans Wennborg228756c2012-05-11 10:11:01 +00007725 Subtarget->is64Bit(),
7726 getTargetMachine().getRelocationModel() == Reloc::PIC_);
Eric Christopher30ef0e52010-06-03 04:07:48 +00007727 }
Craig Toppere8eb1162012-04-23 03:26:18 +00007728 llvm_unreachable("Unknown TLS model.");
7729 }
7730
7731 if (Subtarget->isTargetDarwin()) {
Eric Christopher30ef0e52010-06-03 04:07:48 +00007732 // Darwin only has one model of TLS. Lower to that.
7733 unsigned char OpFlag = 0;
7734 unsigned WrapperKind = Subtarget->isPICStyleRIPRel() ?
7735 X86ISD::WrapperRIP : X86ISD::Wrapper;
Michael J. Spencerec38de22010-10-10 22:04:20 +00007736
Eric Christopher30ef0e52010-06-03 04:07:48 +00007737 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
7738 // global base reg.
7739 bool PIC32 = (getTargetMachine().getRelocationModel() == Reloc::PIC_) &&
7740 !Subtarget->is64Bit();
7741 if (PIC32)
7742 OpFlag = X86II::MO_TLVP_PIC_BASE;
7743 else
7744 OpFlag = X86II::MO_TLVP;
Michael J. Spencerec38de22010-10-10 22:04:20 +00007745 DebugLoc DL = Op.getDebugLoc();
Devang Patel0d881da2010-07-06 22:08:15 +00007746 SDValue Result = DAG.getTargetGlobalAddress(GA->getGlobal(), DL,
Eric Christopherd8c05362010-12-09 06:25:53 +00007747 GA->getValueType(0),
Eric Christopher30ef0e52010-06-03 04:07:48 +00007748 GA->getOffset(), OpFlag);
Eric Christopher30ef0e52010-06-03 04:07:48 +00007749 SDValue Offset = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007750
Eric Christopher30ef0e52010-06-03 04:07:48 +00007751 // With PIC32, the address is actually $g + Offset.
7752 if (PIC32)
7753 Offset = DAG.getNode(ISD::ADD, DL, getPointerTy(),
7754 DAG.getNode(X86ISD::GlobalBaseReg,
7755 DebugLoc(), getPointerTy()),
7756 Offset);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007757
Eric Christopher30ef0e52010-06-03 04:07:48 +00007758 // Lowering the machine isd will make sure everything is in the right
7759 // location.
Eric Christopherd8c05362010-12-09 06:25:53 +00007760 SDValue Chain = DAG.getEntryNode();
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00007761 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Eric Christopherd8c05362010-12-09 06:25:53 +00007762 SDValue Args[] = { Chain, Offset };
7763 Chain = DAG.getNode(X86ISD::TLSCALL, DL, NodeTys, Args, 2);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007764
Eric Christopher30ef0e52010-06-03 04:07:48 +00007765 // TLSCALL will be codegen'ed as call. Inform MFI that function has calls.
7766 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
7767 MFI->setAdjustsStack(true);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00007768
Eric Christopher30ef0e52010-06-03 04:07:48 +00007769 // And our return value (tls address) is in the standard call return value
7770 // location.
Eric Christopherd8c05362010-12-09 06:25:53 +00007771 unsigned Reg = Subtarget->is64Bit() ? X86::RAX : X86::EAX;
Evan Chengfd230df2011-10-19 22:22:54 +00007772 return DAG.getCopyFromReg(Chain, DL, Reg, getPointerTy(),
7773 Chain.getValue(1));
Craig Toppere8eb1162012-04-23 03:26:18 +00007774 }
7775
7776 if (Subtarget->isTargetWindows()) {
Anton Korobeynikovd4a19b62012-02-11 17:26:53 +00007777 // Just use the implicit TLS architecture
7778 // Need to generate someting similar to:
7779 // mov rdx, qword [gs:abs 58H]; Load pointer to ThreadLocalStorage
7780 // ; from TEB
7781 // mov ecx, dword [rel _tls_index]: Load index (from C runtime)
7782 // mov rcx, qword [rdx+rcx*8]
7783 // mov eax, .tls$:tlsvar
7784 // [rax+rcx] contains the address
7785 // Windows 64bit: gs:0x58
7786 // Windows 32bit: fs:__tls_array
7787
7788 // If GV is an alias then use the aliasee for determining
7789 // thread-localness.
7790 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV))
7791 GV = GA->resolveAliasedGlobal(false);
7792 DebugLoc dl = GA->getDebugLoc();
7793 SDValue Chain = DAG.getEntryNode();
7794
7795 // Get the Thread Pointer, which is %fs:__tls_array (32-bit) or
7796 // %gs:0x58 (64-bit).
7797 Value *Ptr = Constant::getNullValue(Subtarget->is64Bit()
7798 ? Type::getInt8PtrTy(*DAG.getContext(),
7799 256)
7800 : Type::getInt32PtrTy(*DAG.getContext(),
7801 257));
7802
7803 SDValue ThreadPointer = DAG.getLoad(getPointerTy(), dl, Chain,
7804 Subtarget->is64Bit()
7805 ? DAG.getIntPtrConstant(0x58)
7806 : DAG.getExternalSymbol("_tls_array",
7807 getPointerTy()),
7808 MachinePointerInfo(Ptr),
7809 false, false, false, 0);
7810
7811 // Load the _tls_index variable
7812 SDValue IDX = DAG.getExternalSymbol("_tls_index", getPointerTy());
7813 if (Subtarget->is64Bit())
7814 IDX = DAG.getExtLoad(ISD::ZEXTLOAD, dl, getPointerTy(), Chain,
7815 IDX, MachinePointerInfo(), MVT::i32,
7816 false, false, 0);
7817 else
7818 IDX = DAG.getLoad(getPointerTy(), dl, Chain, IDX, MachinePointerInfo(),
7819 false, false, false, 0);
7820
Chandler Carruth426c2bf2012-11-01 09:14:31 +00007821 SDValue Scale = DAG.getConstant(Log2_64_Ceil(TD->getPointerSize()),
Craig Topper0fbf3642012-04-23 03:28:34 +00007822 getPointerTy());
Anton Korobeynikovd4a19b62012-02-11 17:26:53 +00007823 IDX = DAG.getNode(ISD::SHL, dl, getPointerTy(), IDX, Scale);
7824
7825 SDValue res = DAG.getNode(ISD::ADD, dl, getPointerTy(), ThreadPointer, IDX);
7826 res = DAG.getLoad(getPointerTy(), dl, Chain, res, MachinePointerInfo(),
7827 false, false, false, 0);
7828
7829 // Get the offset of start of .tls section
7830 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl,
7831 GA->getValueType(0),
7832 GA->getOffset(), X86II::MO_SECREL);
7833 SDValue Offset = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), TGA);
7834
7835 // The address of the thread local variable is the add of the thread
7836 // pointer with the offset of the variable.
7837 return DAG.getNode(ISD::ADD, dl, getPointerTy(), res, Offset);
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007838 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00007839
David Blaikie4d6ccb52012-01-20 21:51:11 +00007840 llvm_unreachable("TLS not implemented for this target.");
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007841}
7842
Chad Rosierb90d2a92012-01-03 23:19:12 +00007843/// LowerShiftParts - Lower SRA_PARTS and friends, which return two i32 values
7844/// and take a 2 x i32 value to shift plus a shift amount.
7845SDValue X86TargetLowering::LowerShiftParts(SDValue Op, SelectionDAG &DAG) const{
Dan Gohman4c1fa612008-03-03 22:22:09 +00007846 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
Owen Andersone50ed302009-08-10 22:56:29 +00007847 EVT VT = Op.getValueType();
Duncan Sands83ec4b62008-06-06 12:08:01 +00007848 unsigned VTBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007849 DebugLoc dl = Op.getDebugLoc();
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007850 bool isSRA = Op.getOpcode() == ISD::SRA_PARTS;
Dan Gohman475871a2008-07-27 21:46:04 +00007851 SDValue ShOpLo = Op.getOperand(0);
7852 SDValue ShOpHi = Op.getOperand(1);
7853 SDValue ShAmt = Op.getOperand(2);
Chris Lattner31dcfe62009-07-29 05:48:09 +00007854 SDValue Tmp1 = isSRA ? DAG.getNode(ISD::SRA, dl, VT, ShOpHi,
Owen Anderson825b72b2009-08-11 20:47:22 +00007855 DAG.getConstant(VTBits - 1, MVT::i8))
Chris Lattner31dcfe62009-07-29 05:48:09 +00007856 : DAG.getConstant(0, VT);
Evan Chenge3413162006-01-09 18:33:28 +00007857
Dan Gohman475871a2008-07-27 21:46:04 +00007858 SDValue Tmp2, Tmp3;
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007859 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dale Johannesenace16102009-02-03 19:33:06 +00007860 Tmp2 = DAG.getNode(X86ISD::SHLD, dl, VT, ShOpHi, ShOpLo, ShAmt);
7861 Tmp3 = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ShAmt);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007862 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00007863 Tmp2 = DAG.getNode(X86ISD::SHRD, dl, VT, ShOpLo, ShOpHi, ShAmt);
7864 Tmp3 = DAG.getNode(isSRA ? ISD::SRA : ISD::SRL, dl, VT, ShOpHi, ShAmt);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007865 }
Evan Chenge3413162006-01-09 18:33:28 +00007866
Owen Anderson825b72b2009-08-11 20:47:22 +00007867 SDValue AndNode = DAG.getNode(ISD::AND, dl, MVT::i8, ShAmt,
7868 DAG.getConstant(VTBits, MVT::i8));
Chris Lattnerccfea352010-02-22 00:28:59 +00007869 SDValue Cond = DAG.getNode(X86ISD::CMP, dl, MVT::i32,
Owen Anderson825b72b2009-08-11 20:47:22 +00007870 AndNode, DAG.getConstant(0, MVT::i8));
Evan Chenge3413162006-01-09 18:33:28 +00007871
Dan Gohman475871a2008-07-27 21:46:04 +00007872 SDValue Hi, Lo;
Owen Anderson825b72b2009-08-11 20:47:22 +00007873 SDValue CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Dan Gohman475871a2008-07-27 21:46:04 +00007874 SDValue Ops0[4] = { Tmp2, Tmp3, CC, Cond };
7875 SDValue Ops1[4] = { Tmp3, Tmp1, CC, Cond };
Duncan Sandsf9516202008-06-30 10:19:09 +00007876
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007877 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dale Johannesenace16102009-02-03 19:33:06 +00007878 Hi = DAG.getNode(X86ISD::CMOV, dl, VT, Ops0, 4);
7879 Lo = DAG.getNode(X86ISD::CMOV, dl, VT, Ops1, 4);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007880 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00007881 Lo = DAG.getNode(X86ISD::CMOV, dl, VT, Ops0, 4);
7882 Hi = DAG.getNode(X86ISD::CMOV, dl, VT, Ops1, 4);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007883 }
7884
Dan Gohman475871a2008-07-27 21:46:04 +00007885 SDValue Ops[2] = { Lo, Hi };
Dale Johannesenace16102009-02-03 19:33:06 +00007886 return DAG.getMergeValues(Ops, 2, dl);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007887}
Evan Chenga3195e82006-01-12 22:54:21 +00007888
Dan Gohmand858e902010-04-17 15:26:15 +00007889SDValue X86TargetLowering::LowerSINT_TO_FP(SDValue Op,
7890 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00007891 EVT SrcVT = Op.getOperand(0).getValueType();
Eli Friedman23ef1052009-06-06 03:57:58 +00007892
Dale Johannesen0488fb62010-09-30 23:57:10 +00007893 if (SrcVT.isVector())
Eli Friedman23ef1052009-06-06 03:57:58 +00007894 return SDValue();
Eli Friedman23ef1052009-06-06 03:57:58 +00007895
Owen Anderson825b72b2009-08-11 20:47:22 +00007896 assert(SrcVT.getSimpleVT() <= MVT::i64 && SrcVT.getSimpleVT() >= MVT::i16 &&
Chris Lattnerb09916b2008-02-27 05:57:41 +00007897 "Unknown SINT_TO_FP to lower!");
Scott Michelfdc40a02009-02-17 22:15:04 +00007898
Eli Friedman36df4992009-05-27 00:47:34 +00007899 // These are really Legal; return the operand so the caller accepts it as
7900 // Legal.
Owen Anderson825b72b2009-08-11 20:47:22 +00007901 if (SrcVT == MVT::i32 && isScalarFPTypeInSSEReg(Op.getValueType()))
Eli Friedman36df4992009-05-27 00:47:34 +00007902 return Op;
Owen Anderson825b72b2009-08-11 20:47:22 +00007903 if (SrcVT == MVT::i64 && isScalarFPTypeInSSEReg(Op.getValueType()) &&
Eli Friedman36df4992009-05-27 00:47:34 +00007904 Subtarget->is64Bit()) {
7905 return Op;
7906 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007907
Bruno Cardoso Lopesa511b8e2011-08-09 17:39:01 +00007908 DebugLoc dl = Op.getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +00007909 unsigned Size = SrcVT.getSizeInBits()/8;
Evan Cheng0db9fe62006-04-25 20:13:52 +00007910 MachineFunction &MF = DAG.getMachineFunction();
David Greene3f2bf852009-11-12 20:49:22 +00007911 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size, false);
Dan Gohman475871a2008-07-27 21:46:04 +00007912 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Dale Johannesenace16102009-02-03 19:33:06 +00007913 SDValue Chain = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Bill Wendling105be5a2009-03-13 08:41:47 +00007914 StackSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00007915 MachinePointerInfo::getFixedStack(SSFI),
David Greene67c9d422010-02-15 16:53:33 +00007916 false, false, 0);
Eli Friedman948e95a2009-05-23 09:59:16 +00007917 return BuildFILD(Op, SrcVT, Chain, StackSlot, DAG);
7918}
Evan Cheng0db9fe62006-04-25 20:13:52 +00007919
Owen Andersone50ed302009-08-10 22:56:29 +00007920SDValue X86TargetLowering::BuildFILD(SDValue Op, EVT SrcVT, SDValue Chain,
Michael J. Spencerec38de22010-10-10 22:04:20 +00007921 SDValue StackSlot,
Dan Gohmand858e902010-04-17 15:26:15 +00007922 SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00007923 // Build the FILD
Chris Lattner492a43e2010-09-22 01:28:21 +00007924 DebugLoc DL = Op.getDebugLoc();
Chris Lattner5a88b832007-02-25 07:10:00 +00007925 SDVTList Tys;
Chris Lattner78631162008-01-16 06:24:21 +00007926 bool useSSE = isScalarFPTypeInSSEReg(Op.getValueType());
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00007927 if (useSSE)
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00007928 Tys = DAG.getVTList(MVT::f64, MVT::Other, MVT::Glue);
Chris Lattner5a88b832007-02-25 07:10:00 +00007929 else
Owen Anderson825b72b2009-08-11 20:47:22 +00007930 Tys = DAG.getVTList(Op.getValueType(), MVT::Other);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007931
Chris Lattner492a43e2010-09-22 01:28:21 +00007932 unsigned ByteSize = SrcVT.getSizeInBits()/8;
Michael J. Spencerec38de22010-10-10 22:04:20 +00007933
Stuart Hastings84be9582011-06-02 15:57:11 +00007934 FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(StackSlot);
7935 MachineMemOperand *MMO;
7936 if (FI) {
7937 int SSFI = FI->getIndex();
7938 MMO =
7939 DAG.getMachineFunction()
7940 .getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
7941 MachineMemOperand::MOLoad, ByteSize, ByteSize);
7942 } else {
7943 MMO = cast<LoadSDNode>(StackSlot)->getMemOperand();
7944 StackSlot = StackSlot.getOperand(1);
7945 }
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00007946 SDValue Ops[] = { Chain, StackSlot, DAG.getValueType(SrcVT) };
Chris Lattner492a43e2010-09-22 01:28:21 +00007947 SDValue Result = DAG.getMemIntrinsicNode(useSSE ? X86ISD::FILD_FLAG :
7948 X86ISD::FILD, DL,
7949 Tys, Ops, array_lengthof(Ops),
7950 SrcVT, MMO);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007951
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00007952 if (useSSE) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00007953 Chain = Result.getValue(1);
Dan Gohman475871a2008-07-27 21:46:04 +00007954 SDValue InFlag = Result.getValue(2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007955
7956 // FIXME: Currently the FST is flagged to the FILD_FLAG. This
7957 // shouldn't be necessary except that RFP cannot be live across
7958 // multiple blocks. When stackifier is fixed, they can be uncoupled.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00007959 MachineFunction &MF = DAG.getMachineFunction();
Bob Wilsoneafca4e2010-09-22 17:35:14 +00007960 unsigned SSFISize = Op.getValueType().getSizeInBits()/8;
7961 int SSFI = MF.getFrameInfo()->CreateStackObject(SSFISize, SSFISize, false);
Dan Gohman475871a2008-07-27 21:46:04 +00007962 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Owen Anderson825b72b2009-08-11 20:47:22 +00007963 Tys = DAG.getVTList(MVT::Other);
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00007964 SDValue Ops[] = {
7965 Chain, Result, StackSlot, DAG.getValueType(Op.getValueType()), InFlag
7966 };
Chris Lattner492a43e2010-09-22 01:28:21 +00007967 MachineMemOperand *MMO =
7968 DAG.getMachineFunction()
7969 .getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
Bob Wilsoneafca4e2010-09-22 17:35:14 +00007970 MachineMemOperand::MOStore, SSFISize, SSFISize);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007971
Chris Lattner492a43e2010-09-22 01:28:21 +00007972 Chain = DAG.getMemIntrinsicNode(X86ISD::FST, DL, Tys,
7973 Ops, array_lengthof(Ops),
7974 Op.getValueType(), MMO);
7975 Result = DAG.getLoad(Op.getValueType(), DL, Chain, StackSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00007976 MachinePointerInfo::getFixedStack(SSFI),
Pete Cooperd752e0f2011-11-08 18:42:53 +00007977 false, false, false, 0);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00007978 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00007979
Evan Cheng0db9fe62006-04-25 20:13:52 +00007980 return Result;
7981}
7982
Bill Wendling8b8a6362009-01-17 03:56:04 +00007983// LowerUINT_TO_FP_i64 - 64-bit unsigned integer to double expansion.
Dan Gohmand858e902010-04-17 15:26:15 +00007984SDValue X86TargetLowering::LowerUINT_TO_FP_i64(SDValue Op,
7985 SelectionDAG &DAG) const {
Bill Wendling397ae212012-01-05 02:13:20 +00007986 // This algorithm is not obvious. Here it is what we're trying to output:
Bill Wendling8b8a6362009-01-17 03:56:04 +00007987 /*
Bill Wendling397ae212012-01-05 02:13:20 +00007988 movq %rax, %xmm0
7989 punpckldq (c0), %xmm0 // c0: (uint4){ 0x43300000U, 0x45300000U, 0U, 0U }
7990 subpd (c1), %xmm0 // c1: (double2){ 0x1.0p52, 0x1.0p52 * 0x1.0p32 }
7991 #ifdef __SSE3__
Chad Rosiera20e1e72012-08-01 18:39:17 +00007992 haddpd %xmm0, %xmm0
Bill Wendling397ae212012-01-05 02:13:20 +00007993 #else
Chad Rosiera20e1e72012-08-01 18:39:17 +00007994 pshufd $0x4e, %xmm0, %xmm1
Bill Wendling397ae212012-01-05 02:13:20 +00007995 addpd %xmm1, %xmm0
7996 #endif
Bill Wendling8b8a6362009-01-17 03:56:04 +00007997 */
Dale Johannesen040225f2008-10-21 23:07:49 +00007998
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007999 DebugLoc dl = Op.getDebugLoc();
Owen Andersona90b3dc2009-07-15 21:51:10 +00008000 LLVMContext *Context = DAG.getContext();
Dale Johannesenace16102009-02-03 19:33:06 +00008001
Dale Johannesen1c15bf52008-10-21 20:50:01 +00008002 // Build some magic constants.
Chris Lattner7302d802012-02-06 21:56:39 +00008003 const uint32_t CV0[] = { 0x43300000, 0x45300000, 0, 0 };
8004 Constant *C0 = ConstantDataVector::get(*Context, CV0);
Evan Cheng1606e8e2009-03-13 07:51:59 +00008005 SDValue CPIdx0 = DAG.getConstantPool(C0, getPointerTy(), 16);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00008006
Chris Lattner97484792012-01-25 09:56:22 +00008007 SmallVector<Constant*,2> CV1;
8008 CV1.push_back(
Chris Lattner4ca829e2012-01-25 06:02:56 +00008009 ConstantFP::get(*Context, APFloat(APInt(64, 0x4330000000000000ULL))));
Chris Lattner97484792012-01-25 09:56:22 +00008010 CV1.push_back(
8011 ConstantFP::get(*Context, APFloat(APInt(64, 0x4530000000000000ULL))));
8012 Constant *C1 = ConstantVector::get(CV1);
Evan Cheng1606e8e2009-03-13 07:51:59 +00008013 SDValue CPIdx1 = DAG.getConstantPool(C1, getPointerTy(), 16);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00008014
Bill Wendling397ae212012-01-05 02:13:20 +00008015 // Load the 64-bit value into an XMM register.
8016 SDValue XR1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i64,
8017 Op.getOperand(0));
Owen Anderson825b72b2009-08-11 20:47:22 +00008018 SDValue CLod0 = DAG.getLoad(MVT::v4i32, dl, DAG.getEntryNode(), CPIdx0,
Chris Lattnere8639032010-09-21 06:22:23 +00008019 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00008020 false, false, false, 16);
Bill Wendling397ae212012-01-05 02:13:20 +00008021 SDValue Unpck1 = getUnpackl(DAG, dl, MVT::v4i32,
8022 DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, XR1),
8023 CLod0);
8024
Owen Anderson825b72b2009-08-11 20:47:22 +00008025 SDValue CLod1 = DAG.getLoad(MVT::v2f64, dl, CLod0.getValue(1), CPIdx1,
Chris Lattnere8639032010-09-21 06:22:23 +00008026 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00008027 false, false, false, 16);
Bill Wendling397ae212012-01-05 02:13:20 +00008028 SDValue XR2F = DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Unpck1);
Owen Anderson825b72b2009-08-11 20:47:22 +00008029 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::v2f64, XR2F, CLod1);
Bill Wendling397ae212012-01-05 02:13:20 +00008030 SDValue Result;
Bill Wendling8b8a6362009-01-17 03:56:04 +00008031
Craig Topperd0a31172012-01-10 06:37:29 +00008032 if (Subtarget->hasSSE3()) {
Bill Wendling397ae212012-01-05 02:13:20 +00008033 // FIXME: The 'haddpd' instruction may be slower than 'movhlps + addsd'.
8034 Result = DAG.getNode(X86ISD::FHADD, dl, MVT::v2f64, Sub, Sub);
8035 } else {
8036 SDValue S2F = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, Sub);
8037 SDValue Shuffle = getTargetShuffleNode(X86ISD::PSHUFD, dl, MVT::v4i32,
8038 S2F, 0x4E, DAG);
8039 Result = DAG.getNode(ISD::FADD, dl, MVT::v2f64,
8040 DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Shuffle),
8041 Sub);
8042 }
8043
8044 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Result,
Dale Johannesen1c15bf52008-10-21 20:50:01 +00008045 DAG.getIntPtrConstant(0));
8046}
8047
Bill Wendling8b8a6362009-01-17 03:56:04 +00008048// LowerUINT_TO_FP_i32 - 32-bit unsigned integer to float expansion.
Dan Gohmand858e902010-04-17 15:26:15 +00008049SDValue X86TargetLowering::LowerUINT_TO_FP_i32(SDValue Op,
8050 SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008051 DebugLoc dl = Op.getDebugLoc();
Bill Wendling8b8a6362009-01-17 03:56:04 +00008052 // FP constant to bias correct the final result.
8053 SDValue Bias = DAG.getConstantFP(BitsToDouble(0x4330000000000000ULL),
Owen Anderson825b72b2009-08-11 20:47:22 +00008054 MVT::f64);
Bill Wendling8b8a6362009-01-17 03:56:04 +00008055
8056 // Load the 32-bit value into an XMM register.
Owen Anderson825b72b2009-08-11 20:47:22 +00008057 SDValue Load = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
Eli Friedman6cdc1f42011-08-02 18:38:35 +00008058 Op.getOperand(0));
Bill Wendling8b8a6362009-01-17 03:56:04 +00008059
Eli Friedmanf3704762011-08-29 21:15:46 +00008060 // Zero out the upper parts of the register.
Craig Topper12216172012-01-13 08:12:35 +00008061 Load = getShuffleVectorZeroOrUndef(Load, 0, true, Subtarget, DAG);
Eli Friedmanf3704762011-08-29 21:15:46 +00008062
Owen Anderson825b72b2009-08-11 20:47:22 +00008063 Load = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008064 DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Load),
Bill Wendling8b8a6362009-01-17 03:56:04 +00008065 DAG.getIntPtrConstant(0));
8066
8067 // Or the load with the bias.
Owen Anderson825b72b2009-08-11 20:47:22 +00008068 SDValue Or = DAG.getNode(ISD::OR, dl, MVT::v2i64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008069 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00008070 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00008071 MVT::v2f64, Load)),
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008072 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00008073 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00008074 MVT::v2f64, Bias)));
8075 Or = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008076 DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Or),
Bill Wendling8b8a6362009-01-17 03:56:04 +00008077 DAG.getIntPtrConstant(0));
8078
8079 // Subtract the bias.
Owen Anderson825b72b2009-08-11 20:47:22 +00008080 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::f64, Or, Bias);
Bill Wendling8b8a6362009-01-17 03:56:04 +00008081
8082 // Handle final rounding.
Owen Andersone50ed302009-08-10 22:56:29 +00008083 EVT DestVT = Op.getValueType();
Bill Wendling030939c2009-01-17 07:40:19 +00008084
Craig Topper69947b92012-04-23 06:57:04 +00008085 if (DestVT.bitsLT(MVT::f64))
Dale Johannesenace16102009-02-03 19:33:06 +00008086 return DAG.getNode(ISD::FP_ROUND, dl, DestVT, Sub,
Bill Wendling030939c2009-01-17 07:40:19 +00008087 DAG.getIntPtrConstant(0));
Craig Topper69947b92012-04-23 06:57:04 +00008088 if (DestVT.bitsGT(MVT::f64))
Dale Johannesenace16102009-02-03 19:33:06 +00008089 return DAG.getNode(ISD::FP_EXTEND, dl, DestVT, Sub);
Bill Wendling030939c2009-01-17 07:40:19 +00008090
8091 // Handle final rounding.
8092 return Sub;
Bill Wendling8b8a6362009-01-17 03:56:04 +00008093}
8094
Michael Liaoa7554632012-10-23 17:36:08 +00008095SDValue X86TargetLowering::lowerUINT_TO_FP_vec(SDValue Op,
8096 SelectionDAG &DAG) const {
8097 SDValue N0 = Op.getOperand(0);
8098 EVT SVT = N0.getValueType();
8099 DebugLoc dl = Op.getDebugLoc();
8100
8101 assert((SVT == MVT::v4i8 || SVT == MVT::v4i16 ||
8102 SVT == MVT::v8i8 || SVT == MVT::v8i16) &&
8103 "Custom UINT_TO_FP is not supported!");
8104
8105 EVT NVT = EVT::getVectorVT(*DAG.getContext(), MVT::i32, SVT.getVectorNumElements());
8106 return DAG.getNode(ISD::SINT_TO_FP, dl, Op.getValueType(),
8107 DAG.getNode(ISD::ZERO_EXTEND, dl, NVT, N0));
8108}
8109
Dan Gohmand858e902010-04-17 15:26:15 +00008110SDValue X86TargetLowering::LowerUINT_TO_FP(SDValue Op,
8111 SelectionDAG &DAG) const {
Evan Chenga06ec9e2009-01-19 08:08:22 +00008112 SDValue N0 = Op.getOperand(0);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008113 DebugLoc dl = Op.getDebugLoc();
Bill Wendling8b8a6362009-01-17 03:56:04 +00008114
Michael Liaoa7554632012-10-23 17:36:08 +00008115 if (Op.getValueType().isVector())
8116 return lowerUINT_TO_FP_vec(Op, DAG);
8117
Dale Johannesen8d908eb2010-05-15 18:51:12 +00008118 // Since UINT_TO_FP is legal (it's marked custom), dag combiner won't
Evan Chenga06ec9e2009-01-19 08:08:22 +00008119 // optimize it to a SINT_TO_FP when the sign bit is known zero. Perform
8120 // the optimization here.
8121 if (DAG.SignBitIsZero(N0))
Dale Johannesenace16102009-02-03 19:33:06 +00008122 return DAG.getNode(ISD::SINT_TO_FP, dl, Op.getValueType(), N0);
Evan Chenga06ec9e2009-01-19 08:08:22 +00008123
Owen Andersone50ed302009-08-10 22:56:29 +00008124 EVT SrcVT = N0.getValueType();
Dale Johannesen8d908eb2010-05-15 18:51:12 +00008125 EVT DstVT = Op.getValueType();
8126 if (SrcVT == MVT::i64 && DstVT == MVT::f64 && X86ScalarSSEf64)
Bill Wendling8b8a6362009-01-17 03:56:04 +00008127 return LowerUINT_TO_FP_i64(Op, DAG);
Craig Topper69947b92012-04-23 06:57:04 +00008128 if (SrcVT == MVT::i32 && X86ScalarSSEf64)
Bill Wendling8b8a6362009-01-17 03:56:04 +00008129 return LowerUINT_TO_FP_i32(Op, DAG);
Craig Topper69947b92012-04-23 06:57:04 +00008130 if (Subtarget->is64Bit() && SrcVT == MVT::i64 && DstVT == MVT::f32)
Bill Wendling397ae212012-01-05 02:13:20 +00008131 return SDValue();
Eli Friedman948e95a2009-05-23 09:59:16 +00008132
8133 // Make a 64-bit buffer, and use it to build an FILD.
Owen Anderson825b72b2009-08-11 20:47:22 +00008134 SDValue StackSlot = DAG.CreateStackTemporary(MVT::i64);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00008135 if (SrcVT == MVT::i32) {
8136 SDValue WordOff = DAG.getConstant(4, getPointerTy());
8137 SDValue OffsetSlot = DAG.getNode(ISD::ADD, dl,
8138 getPointerTy(), StackSlot, WordOff);
8139 SDValue Store1 = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Chris Lattner8026a9d2010-09-21 17:50:43 +00008140 StackSlot, MachinePointerInfo(),
8141 false, false, 0);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00008142 SDValue Store2 = DAG.getStore(Store1, dl, DAG.getConstant(0, MVT::i32),
Chris Lattner8026a9d2010-09-21 17:50:43 +00008143 OffsetSlot, MachinePointerInfo(),
8144 false, false, 0);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00008145 SDValue Fild = BuildFILD(Op, MVT::i64, Store2, StackSlot, DAG);
8146 return Fild;
8147 }
8148
8149 assert(SrcVT == MVT::i64 && "Unexpected type in UINT_TO_FP");
8150 SDValue Store = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Bill Wendlingf6c07472012-01-10 19:41:30 +00008151 StackSlot, MachinePointerInfo(),
Chris Lattner8026a9d2010-09-21 17:50:43 +00008152 false, false, 0);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00008153 // For i64 source, we need to add the appropriate power of 2 if the input
8154 // was negative. This is the same as the optimization in
8155 // DAGTypeLegalizer::ExpandIntOp_UNIT_TO_FP, and for it to be safe here,
8156 // we must be careful to do the computation in x87 extended precision, not
8157 // in SSE. (The generic code can't know it's OK to do this, or how to.)
Chris Lattner492a43e2010-09-22 01:28:21 +00008158 int SSFI = cast<FrameIndexSDNode>(StackSlot)->getIndex();
8159 MachineMemOperand *MMO =
8160 DAG.getMachineFunction()
8161 .getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
8162 MachineMemOperand::MOLoad, 8, 8);
Michael J. Spencerec38de22010-10-10 22:04:20 +00008163
Dale Johannesen8d908eb2010-05-15 18:51:12 +00008164 SDVTList Tys = DAG.getVTList(MVT::f80, MVT::Other);
8165 SDValue Ops[] = { Store, StackSlot, DAG.getValueType(MVT::i64) };
Chris Lattner492a43e2010-09-22 01:28:21 +00008166 SDValue Fild = DAG.getMemIntrinsicNode(X86ISD::FILD, dl, Tys, Ops, 3,
8167 MVT::i64, MMO);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00008168
8169 APInt FF(32, 0x5F800000ULL);
8170
8171 // Check whether the sign bit is set.
8172 SDValue SignSet = DAG.getSetCC(dl, getSetCCResultType(MVT::i64),
8173 Op.getOperand(0), DAG.getConstant(0, MVT::i64),
8174 ISD::SETLT);
8175
8176 // Build a 64 bit pair (0, FF) in the constant pool, with FF in the lo bits.
8177 SDValue FudgePtr = DAG.getConstantPool(
8178 ConstantInt::get(*DAG.getContext(), FF.zext(64)),
8179 getPointerTy());
8180
8181 // Get a pointer to FF if the sign bit was set, or to 0 otherwise.
8182 SDValue Zero = DAG.getIntPtrConstant(0);
8183 SDValue Four = DAG.getIntPtrConstant(4);
8184 SDValue Offset = DAG.getNode(ISD::SELECT, dl, Zero.getValueType(), SignSet,
8185 Zero, Four);
8186 FudgePtr = DAG.getNode(ISD::ADD, dl, getPointerTy(), FudgePtr, Offset);
8187
8188 // Load the value out, extending it from f32 to f80.
8189 // FIXME: Avoid the extend by constructing the right constant pool?
Stuart Hastingsa9011292011-02-16 16:23:55 +00008190 SDValue Fudge = DAG.getExtLoad(ISD::EXTLOAD, dl, MVT::f80, DAG.getEntryNode(),
Chris Lattnere8639032010-09-21 06:22:23 +00008191 FudgePtr, MachinePointerInfo::getConstantPool(),
8192 MVT::f32, false, false, 4);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00008193 // Extend everything to 80 bits to force it to be done on x87.
8194 SDValue Add = DAG.getNode(ISD::FADD, dl, MVT::f80, Fild, Fudge);
8195 return DAG.getNode(ISD::FP_ROUND, dl, DstVT, Add, DAG.getIntPtrConstant(0));
Bill Wendling8b8a6362009-01-17 03:56:04 +00008196}
8197
Dan Gohman475871a2008-07-27 21:46:04 +00008198std::pair<SDValue,SDValue> X86TargetLowering::
NAKAMURA Takumi9a68fdc2012-02-25 03:37:25 +00008199FP_TO_INTHelper(SDValue Op, SelectionDAG &DAG, bool IsSigned, bool IsReplace) const {
Chris Lattner07290932010-09-22 01:05:16 +00008200 DebugLoc DL = Op.getDebugLoc();
Eli Friedman948e95a2009-05-23 09:59:16 +00008201
Owen Andersone50ed302009-08-10 22:56:29 +00008202 EVT DstTy = Op.getValueType();
Eli Friedman948e95a2009-05-23 09:59:16 +00008203
Michael J. Spencer1a2d0612012-02-24 19:01:22 +00008204 if (!IsSigned && !isIntegerTypeFTOL(DstTy)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00008205 assert(DstTy == MVT::i32 && "Unexpected FP_TO_UINT");
8206 DstTy = MVT::i64;
Eli Friedman948e95a2009-05-23 09:59:16 +00008207 }
8208
Owen Anderson825b72b2009-08-11 20:47:22 +00008209 assert(DstTy.getSimpleVT() <= MVT::i64 &&
8210 DstTy.getSimpleVT() >= MVT::i16 &&
Michael J. Spencer1a2d0612012-02-24 19:01:22 +00008211 "Unknown FP_TO_INT to lower!");
Evan Cheng0db9fe62006-04-25 20:13:52 +00008212
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00008213 // These are really Legal.
Owen Anderson825b72b2009-08-11 20:47:22 +00008214 if (DstTy == MVT::i32 &&
Chris Lattner78631162008-01-16 06:24:21 +00008215 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Dan Gohman475871a2008-07-27 21:46:04 +00008216 return std::make_pair(SDValue(), SDValue());
Dale Johannesen73328d12007-09-19 23:55:34 +00008217 if (Subtarget->is64Bit() &&
Owen Anderson825b72b2009-08-11 20:47:22 +00008218 DstTy == MVT::i64 &&
Eli Friedman36df4992009-05-27 00:47:34 +00008219 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Dan Gohman475871a2008-07-27 21:46:04 +00008220 return std::make_pair(SDValue(), SDValue());
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00008221
Michael J. Spencer1a2d0612012-02-24 19:01:22 +00008222 // We lower FP->int64 either into FISTP64 followed by a load from a temporary
8223 // stack slot, or into the FTOL runtime function.
Evan Cheng87c89352007-10-15 20:11:21 +00008224 MachineFunction &MF = DAG.getMachineFunction();
Eli Friedman948e95a2009-05-23 09:59:16 +00008225 unsigned MemSize = DstTy.getSizeInBits()/8;
David Greene3f2bf852009-11-12 20:49:22 +00008226 int SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize, false);
Dan Gohman475871a2008-07-27 21:46:04 +00008227 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Eric Christopherfd179292009-08-27 18:07:15 +00008228
Evan Cheng0db9fe62006-04-25 20:13:52 +00008229 unsigned Opc;
Michael J. Spencer1a2d0612012-02-24 19:01:22 +00008230 if (!IsSigned && isIntegerTypeFTOL(DstTy))
8231 Opc = X86ISD::WIN_FTOL;
8232 else
8233 switch (DstTy.getSimpleVT().SimpleTy) {
8234 default: llvm_unreachable("Invalid FP_TO_SINT to lower!");
8235 case MVT::i16: Opc = X86ISD::FP_TO_INT16_IN_MEM; break;
8236 case MVT::i32: Opc = X86ISD::FP_TO_INT32_IN_MEM; break;
8237 case MVT::i64: Opc = X86ISD::FP_TO_INT64_IN_MEM; break;
8238 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00008239
Dan Gohman475871a2008-07-27 21:46:04 +00008240 SDValue Chain = DAG.getEntryNode();
8241 SDValue Value = Op.getOperand(0);
Chris Lattner492a43e2010-09-22 01:28:21 +00008242 EVT TheVT = Op.getOperand(0).getValueType();
Michael J. Spencer1a2d0612012-02-24 19:01:22 +00008243 // FIXME This causes a redundant load/store if the SSE-class value is already
8244 // in memory, such as if it is on the callstack.
Chris Lattner492a43e2010-09-22 01:28:21 +00008245 if (isScalarFPTypeInSSEReg(TheVT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00008246 assert(DstTy == MVT::i64 && "Invalid FP_TO_SINT to lower!");
Chris Lattner07290932010-09-22 01:05:16 +00008247 Chain = DAG.getStore(Chain, DL, Value, StackSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00008248 MachinePointerInfo::getFixedStack(SSFI),
David Greene67c9d422010-02-15 16:53:33 +00008249 false, false, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +00008250 SDVTList Tys = DAG.getVTList(Op.getOperand(0).getValueType(), MVT::Other);
Dan Gohman475871a2008-07-27 21:46:04 +00008251 SDValue Ops[] = {
Chris Lattner492a43e2010-09-22 01:28:21 +00008252 Chain, StackSlot, DAG.getValueType(TheVT)
Chris Lattner5a88b832007-02-25 07:10:00 +00008253 };
Michael J. Spencerec38de22010-10-10 22:04:20 +00008254
Chris Lattner492a43e2010-09-22 01:28:21 +00008255 MachineMemOperand *MMO =
8256 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
8257 MachineMemOperand::MOLoad, MemSize, MemSize);
8258 Value = DAG.getMemIntrinsicNode(X86ISD::FLD, DL, Tys, Ops, 3,
8259 DstTy, MMO);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008260 Chain = Value.getValue(1);
David Greene3f2bf852009-11-12 20:49:22 +00008261 SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize, false);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008262 StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
8263 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00008264
Chris Lattner07290932010-09-22 01:05:16 +00008265 MachineMemOperand *MMO =
8266 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
8267 MachineMemOperand::MOStore, MemSize, MemSize);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00008268
Michael J. Spencer1a2d0612012-02-24 19:01:22 +00008269 if (Opc != X86ISD::WIN_FTOL) {
8270 // Build the FP_TO_INT*_IN_MEM
8271 SDValue Ops[] = { Chain, Value, StackSlot };
8272 SDValue FIST = DAG.getMemIntrinsicNode(Opc, DL, DAG.getVTList(MVT::Other),
8273 Ops, 3, DstTy, MMO);
8274 return std::make_pair(FIST, StackSlot);
8275 } else {
8276 SDValue ftol = DAG.getNode(X86ISD::WIN_FTOL, DL,
8277 DAG.getVTList(MVT::Other, MVT::Glue),
8278 Chain, Value);
8279 SDValue eax = DAG.getCopyFromReg(ftol, DL, X86::EAX,
8280 MVT::i32, ftol.getValue(1));
8281 SDValue edx = DAG.getCopyFromReg(eax.getValue(1), DL, X86::EDX,
8282 MVT::i32, eax.getValue(2));
NAKAMURA Takumi9a68fdc2012-02-25 03:37:25 +00008283 SDValue Ops[] = { eax, edx };
8284 SDValue pair = IsReplace
8285 ? DAG.getNode(ISD::BUILD_PAIR, DL, MVT::i64, Ops, 2)
8286 : DAG.getMergeValues(Ops, 2, DL);
Michael J. Spencer1a2d0612012-02-24 19:01:22 +00008287 return std::make_pair(pair, SDValue());
8288 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00008289}
8290
Michael Liaoa7554632012-10-23 17:36:08 +00008291SDValue X86TargetLowering::lowerZERO_EXTEND(SDValue Op, SelectionDAG &DAG) const {
8292 DebugLoc DL = Op.getDebugLoc();
8293 EVT VT = Op.getValueType();
8294 SDValue In = Op.getOperand(0);
8295 EVT SVT = In.getValueType();
8296
8297 if (!VT.is256BitVector() || !SVT.is128BitVector() ||
8298 VT.getVectorNumElements() != SVT.getVectorNumElements())
8299 return SDValue();
8300
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00008301 assert(Subtarget->hasFp256() && "256-bit vector is observed without AVX!");
Michael Liaoa7554632012-10-23 17:36:08 +00008302
8303 // AVX2 has better support of integer extending.
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00008304 if (Subtarget->hasInt256())
Michael Liaoa7554632012-10-23 17:36:08 +00008305 return DAG.getNode(X86ISD::VZEXT, DL, VT, In);
8306
8307 SDValue Lo = DAG.getNode(X86ISD::VZEXT, DL, MVT::v4i32, In);
8308 static const int Mask[] = {4, 5, 6, 7, -1, -1, -1, -1};
8309 SDValue Hi = DAG.getNode(X86ISD::VZEXT, DL, MVT::v4i32,
8310 DAG.getVectorShuffle(MVT::v8i16, DL, In, DAG.getUNDEF(MVT::v8i16), &Mask[0]));
8311
8312 return DAG.getNode(ISD::CONCAT_VECTORS, DL, MVT::v8i32, Lo, Hi);
8313}
8314
Michael Liaobedcbd42012-10-16 18:14:11 +00008315SDValue X86TargetLowering::lowerTRUNCATE(SDValue Op, SelectionDAG &DAG) const {
8316 DebugLoc DL = Op.getDebugLoc();
8317 EVT VT = Op.getValueType();
8318 EVT SVT = Op.getOperand(0).getValueType();
8319
8320 if (!VT.is128BitVector() || !SVT.is256BitVector() ||
8321 VT.getVectorNumElements() != SVT.getVectorNumElements())
8322 return SDValue();
8323
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00008324 assert(Subtarget->hasFp256() && "256-bit vector is observed without AVX!");
Michael Liaobedcbd42012-10-16 18:14:11 +00008325
8326 unsigned NumElems = VT.getVectorNumElements();
8327 EVT NVT = EVT::getVectorVT(*DAG.getContext(), VT.getVectorElementType(),
8328 NumElems * 2);
8329
8330 SDValue In = Op.getOperand(0);
8331 SmallVector<int, 16> MaskVec(NumElems * 2, -1);
8332 // Prepare truncation shuffle mask
8333 for (unsigned i = 0; i != NumElems; ++i)
8334 MaskVec[i] = i * 2;
8335 SDValue V = DAG.getVectorShuffle(NVT, DL,
8336 DAG.getNode(ISD::BITCAST, DL, NVT, In),
8337 DAG.getUNDEF(NVT), &MaskVec[0]);
8338 return DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, VT, V,
8339 DAG.getIntPtrConstant(0));
8340}
8341
Dan Gohmand858e902010-04-17 15:26:15 +00008342SDValue X86TargetLowering::LowerFP_TO_SINT(SDValue Op,
8343 SelectionDAG &DAG) const {
Michael Liaobedcbd42012-10-16 18:14:11 +00008344 if (Op.getValueType().isVector()) {
8345 if (Op.getValueType() == MVT::v8i16)
8346 return DAG.getNode(ISD::TRUNCATE, Op.getDebugLoc(), Op.getValueType(),
8347 DAG.getNode(ISD::FP_TO_SINT, Op.getDebugLoc(),
8348 MVT::v8i32, Op.getOperand(0)));
Eli Friedman23ef1052009-06-06 03:57:58 +00008349 return SDValue();
Michael Liaobedcbd42012-10-16 18:14:11 +00008350 }
Eli Friedman23ef1052009-06-06 03:57:58 +00008351
NAKAMURA Takumi9a68fdc2012-02-25 03:37:25 +00008352 std::pair<SDValue,SDValue> Vals = FP_TO_INTHelper(Op, DAG,
8353 /*IsSigned=*/ true, /*IsReplace=*/ false);
Dan Gohman475871a2008-07-27 21:46:04 +00008354 SDValue FIST = Vals.first, StackSlot = Vals.second;
Eli Friedman36df4992009-05-27 00:47:34 +00008355 // If FP_TO_INTHelper failed, the node is actually supposed to be Legal.
8356 if (FIST.getNode() == 0) return Op;
Scott Michelfdc40a02009-02-17 22:15:04 +00008357
Michael J. Spencer1a2d0612012-02-24 19:01:22 +00008358 if (StackSlot.getNode())
8359 // Load the result.
8360 return DAG.getLoad(Op.getValueType(), Op.getDebugLoc(),
8361 FIST, StackSlot, MachinePointerInfo(),
8362 false, false, false, 0);
Craig Topper69947b92012-04-23 06:57:04 +00008363
8364 // The node is the result.
8365 return FIST;
Chris Lattner27a6c732007-11-24 07:07:01 +00008366}
8367
Dan Gohmand858e902010-04-17 15:26:15 +00008368SDValue X86TargetLowering::LowerFP_TO_UINT(SDValue Op,
8369 SelectionDAG &DAG) const {
NAKAMURA Takumi9a68fdc2012-02-25 03:37:25 +00008370 std::pair<SDValue,SDValue> Vals = FP_TO_INTHelper(Op, DAG,
8371 /*IsSigned=*/ false, /*IsReplace=*/ false);
Eli Friedman948e95a2009-05-23 09:59:16 +00008372 SDValue FIST = Vals.first, StackSlot = Vals.second;
8373 assert(FIST.getNode() && "Unexpected failure");
8374
NAKAMURA Takumi9a68fdc2012-02-25 03:37:25 +00008375 if (StackSlot.getNode())
8376 // Load the result.
8377 return DAG.getLoad(Op.getValueType(), Op.getDebugLoc(),
8378 FIST, StackSlot, MachinePointerInfo(),
8379 false, false, false, 0);
Craig Topper69947b92012-04-23 06:57:04 +00008380
8381 // The node is the result.
8382 return FIST;
Eli Friedman948e95a2009-05-23 09:59:16 +00008383}
8384
Michael Liao9d796db2012-10-10 16:32:15 +00008385SDValue X86TargetLowering::lowerFP_EXTEND(SDValue Op,
8386 SelectionDAG &DAG) const {
8387 DebugLoc DL = Op.getDebugLoc();
8388 EVT VT = Op.getValueType();
8389 SDValue In = Op.getOperand(0);
8390 EVT SVT = In.getValueType();
8391
8392 assert(SVT == MVT::v2f32 && "Only customize MVT::v2f32 type legalization!");
8393
8394 return DAG.getNode(X86ISD::VFPEXT, DL, VT,
8395 DAG.getNode(ISD::CONCAT_VECTORS, DL, MVT::v4f32,
8396 In, DAG.getUNDEF(SVT)));
8397}
8398
Craig Topper43620672012-09-08 07:31:51 +00008399SDValue X86TargetLowering::LowerFABS(SDValue Op, SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00008400 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008401 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00008402 EVT VT = Op.getValueType();
8403 EVT EltVT = VT;
Craig Topper43620672012-09-08 07:31:51 +00008404 unsigned NumElts = VT == MVT::f64 ? 2 : 4;
8405 if (VT.isVector()) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00008406 EltVT = VT.getVectorElementType();
Craig Topper43620672012-09-08 07:31:51 +00008407 NumElts = VT.getVectorNumElements();
Evan Cheng0db9fe62006-04-25 20:13:52 +00008408 }
Craig Topper43620672012-09-08 07:31:51 +00008409 Constant *C;
8410 if (EltVT == MVT::f64)
8411 C = ConstantFP::get(*Context, APFloat(APInt(64, ~(1ULL << 63))));
8412 else
8413 C = ConstantFP::get(*Context, APFloat(APInt(32, ~(1U << 31))));
8414 C = ConstantVector::getSplat(NumElts, C);
8415 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy());
8416 unsigned Alignment = cast<ConstantPoolSDNode>(CPIdx)->getAlignment();
Dale Johannesenace16102009-02-03 19:33:06 +00008417 SDValue Mask = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00008418 MachinePointerInfo::getConstantPool(),
Craig Topper43620672012-09-08 07:31:51 +00008419 false, false, false, Alignment);
8420 if (VT.isVector()) {
8421 MVT ANDVT = VT.is128BitVector() ? MVT::v2i64 : MVT::v4i64;
8422 return DAG.getNode(ISD::BITCAST, dl, VT,
8423 DAG.getNode(ISD::AND, dl, ANDVT,
8424 DAG.getNode(ISD::BITCAST, dl, ANDVT,
8425 Op.getOperand(0)),
8426 DAG.getNode(ISD::BITCAST, dl, ANDVT, Mask)));
8427 }
Dale Johannesenace16102009-02-03 19:33:06 +00008428 return DAG.getNode(X86ISD::FAND, dl, VT, Op.getOperand(0), Mask);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008429}
8430
Dan Gohmand858e902010-04-17 15:26:15 +00008431SDValue X86TargetLowering::LowerFNEG(SDValue Op, SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00008432 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008433 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00008434 EVT VT = Op.getValueType();
8435 EVT EltVT = VT;
Chad Rosiera860b182011-12-15 01:02:25 +00008436 unsigned NumElts = VT == MVT::f64 ? 2 : 4;
8437 if (VT.isVector()) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00008438 EltVT = VT.getVectorElementType();
Chad Rosiera860b182011-12-15 01:02:25 +00008439 NumElts = VT.getVectorNumElements();
8440 }
Chris Lattner4ca829e2012-01-25 06:02:56 +00008441 Constant *C;
8442 if (EltVT == MVT::f64)
8443 C = ConstantFP::get(*Context, APFloat(APInt(64, 1ULL << 63)));
8444 else
8445 C = ConstantFP::get(*Context, APFloat(APInt(32, 1U << 31)));
8446 C = ConstantVector::getSplat(NumElts, C);
Craig Toppercacd9d62012-09-08 07:46:05 +00008447 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy());
8448 unsigned Alignment = cast<ConstantPoolSDNode>(CPIdx)->getAlignment();
Dale Johannesenace16102009-02-03 19:33:06 +00008449 SDValue Mask = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00008450 MachinePointerInfo::getConstantPool(),
Craig Toppercacd9d62012-09-08 07:46:05 +00008451 false, false, false, Alignment);
Duncan Sands83ec4b62008-06-06 12:08:01 +00008452 if (VT.isVector()) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00008453 MVT XORVT = VT.is128BitVector() ? MVT::v2i64 : MVT::v4i64;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008454 return DAG.getNode(ISD::BITCAST, dl, VT,
Chad Rosiera860b182011-12-15 01:02:25 +00008455 DAG.getNode(ISD::XOR, dl, XORVT,
Craig Topper69947b92012-04-23 06:57:04 +00008456 DAG.getNode(ISD::BITCAST, dl, XORVT,
8457 Op.getOperand(0)),
8458 DAG.getNode(ISD::BITCAST, dl, XORVT, Mask)));
Evan Chengd4d01b72007-07-19 23:36:01 +00008459 }
Craig Topper69947b92012-04-23 06:57:04 +00008460
8461 return DAG.getNode(X86ISD::FXOR, dl, VT, Op.getOperand(0), Mask);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008462}
8463
Dan Gohmand858e902010-04-17 15:26:15 +00008464SDValue X86TargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00008465 LLVMContext *Context = DAG.getContext();
Dan Gohman475871a2008-07-27 21:46:04 +00008466 SDValue Op0 = Op.getOperand(0);
8467 SDValue Op1 = Op.getOperand(1);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008468 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00008469 EVT VT = Op.getValueType();
8470 EVT SrcVT = Op1.getValueType();
Evan Cheng73d6cf12007-01-05 21:37:56 +00008471
8472 // If second operand is smaller, extend it first.
Duncan Sands8e4eb092008-06-08 20:54:56 +00008473 if (SrcVT.bitsLT(VT)) {
Dale Johannesenace16102009-02-03 19:33:06 +00008474 Op1 = DAG.getNode(ISD::FP_EXTEND, dl, VT, Op1);
Evan Cheng73d6cf12007-01-05 21:37:56 +00008475 SrcVT = VT;
8476 }
Dale Johannesen61c7ef32007-10-21 01:07:44 +00008477 // And if it is bigger, shrink it first.
Duncan Sands8e4eb092008-06-08 20:54:56 +00008478 if (SrcVT.bitsGT(VT)) {
Dale Johannesenace16102009-02-03 19:33:06 +00008479 Op1 = DAG.getNode(ISD::FP_ROUND, dl, VT, Op1, DAG.getIntPtrConstant(1));
Dale Johannesen61c7ef32007-10-21 01:07:44 +00008480 SrcVT = VT;
Dale Johannesen61c7ef32007-10-21 01:07:44 +00008481 }
8482
8483 // At this point the operands and the result should have the same
8484 // type, and that won't be f80 since that is not custom lowered.
Evan Cheng73d6cf12007-01-05 21:37:56 +00008485
Evan Cheng68c47cb2007-01-05 07:55:56 +00008486 // First get the sign bit of second operand.
Chad Rosier01d426e2011-12-15 01:16:09 +00008487 SmallVector<Constant*,4> CV;
Owen Anderson825b72b2009-08-11 20:47:22 +00008488 if (SrcVT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00008489 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 1ULL << 63))));
8490 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 0))));
Evan Cheng68c47cb2007-01-05 07:55:56 +00008491 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00008492 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 1U << 31))));
8493 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
8494 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
8495 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
Evan Cheng68c47cb2007-01-05 07:55:56 +00008496 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00008497 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00008498 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00008499 SDValue Mask1 = DAG.getLoad(SrcVT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00008500 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00008501 false, false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00008502 SDValue SignBit = DAG.getNode(X86ISD::FAND, dl, SrcVT, Op1, Mask1);
Evan Cheng68c47cb2007-01-05 07:55:56 +00008503
8504 // Shift sign bit right or left if the two operands have different types.
Duncan Sands8e4eb092008-06-08 20:54:56 +00008505 if (SrcVT.bitsGT(VT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00008506 // Op0 is MVT::f32, Op1 is MVT::f64.
8507 SignBit = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f64, SignBit);
8508 SignBit = DAG.getNode(X86ISD::FSRL, dl, MVT::v2f64, SignBit,
8509 DAG.getConstant(32, MVT::i32));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008510 SignBit = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, SignBit);
Owen Anderson825b72b2009-08-11 20:47:22 +00008511 SignBit = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f32, SignBit,
Chris Lattner0bd48932008-01-17 07:00:52 +00008512 DAG.getIntPtrConstant(0));
Evan Cheng68c47cb2007-01-05 07:55:56 +00008513 }
8514
Evan Cheng73d6cf12007-01-05 21:37:56 +00008515 // Clear first operand sign bit.
8516 CV.clear();
Owen Anderson825b72b2009-08-11 20:47:22 +00008517 if (VT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00008518 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, ~(1ULL << 63)))));
8519 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 0))));
Evan Cheng73d6cf12007-01-05 21:37:56 +00008520 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00008521 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, ~(1U << 31)))));
8522 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
8523 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
8524 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
Evan Cheng73d6cf12007-01-05 21:37:56 +00008525 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00008526 C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00008527 CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00008528 SDValue Mask2 = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00008529 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00008530 false, false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00008531 SDValue Val = DAG.getNode(X86ISD::FAND, dl, VT, Op0, Mask2);
Evan Cheng73d6cf12007-01-05 21:37:56 +00008532
8533 // Or the value with the sign bit.
Dale Johannesenace16102009-02-03 19:33:06 +00008534 return DAG.getNode(X86ISD::FOR, dl, VT, Val, SignBit);
Evan Cheng68c47cb2007-01-05 07:55:56 +00008535}
8536
Craig Topper55b24052012-09-11 06:15:32 +00008537static SDValue LowerFGETSIGN(SDValue Op, SelectionDAG &DAG) {
Stuart Hastings4fd0dee2011-06-01 04:39:42 +00008538 SDValue N0 = Op.getOperand(0);
8539 DebugLoc dl = Op.getDebugLoc();
8540 EVT VT = Op.getValueType();
8541
8542 // Lower ISD::FGETSIGN to (AND (X86ISD::FGETSIGNx86 ...) 1).
8543 SDValue xFGETSIGN = DAG.getNode(X86ISD::FGETSIGNx86, dl, VT, N0,
8544 DAG.getConstant(1, VT));
8545 return DAG.getNode(ISD::AND, dl, VT, xFGETSIGN, DAG.getConstant(1, VT));
8546}
8547
Michael Liaof966e4e2012-09-13 20:24:54 +00008548// LowerVectorAllZeroTest - Check whether an OR'd tree is PTEST-able.
8549//
8550SDValue X86TargetLowering::LowerVectorAllZeroTest(SDValue Op, SelectionDAG &DAG) const {
8551 assert(Op.getOpcode() == ISD::OR && "Only check OR'd tree.");
8552
8553 if (!Subtarget->hasSSE41())
8554 return SDValue();
8555
8556 if (!Op->hasOneUse())
8557 return SDValue();
8558
8559 SDNode *N = Op.getNode();
8560 DebugLoc DL = N->getDebugLoc();
8561
8562 SmallVector<SDValue, 8> Opnds;
8563 DenseMap<SDValue, unsigned> VecInMap;
8564 EVT VT = MVT::Other;
8565
8566 // Recognize a special case where a vector is casted into wide integer to
8567 // test all 0s.
8568 Opnds.push_back(N->getOperand(0));
8569 Opnds.push_back(N->getOperand(1));
8570
8571 for (unsigned Slot = 0, e = Opnds.size(); Slot < e; ++Slot) {
8572 SmallVector<SDValue, 8>::const_iterator I = Opnds.begin() + Slot;
8573 // BFS traverse all OR'd operands.
8574 if (I->getOpcode() == ISD::OR) {
8575 Opnds.push_back(I->getOperand(0));
8576 Opnds.push_back(I->getOperand(1));
8577 // Re-evaluate the number of nodes to be traversed.
8578 e += 2; // 2 more nodes (LHS and RHS) are pushed.
8579 continue;
8580 }
8581
8582 // Quit if a non-EXTRACT_VECTOR_ELT
8583 if (I->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
8584 return SDValue();
8585
8586 // Quit if without a constant index.
8587 SDValue Idx = I->getOperand(1);
8588 if (!isa<ConstantSDNode>(Idx))
8589 return SDValue();
8590
8591 SDValue ExtractedFromVec = I->getOperand(0);
8592 DenseMap<SDValue, unsigned>::iterator M = VecInMap.find(ExtractedFromVec);
8593 if (M == VecInMap.end()) {
8594 VT = ExtractedFromVec.getValueType();
8595 // Quit if not 128/256-bit vector.
8596 if (!VT.is128BitVector() && !VT.is256BitVector())
8597 return SDValue();
8598 // Quit if not the same type.
8599 if (VecInMap.begin() != VecInMap.end() &&
8600 VT != VecInMap.begin()->first.getValueType())
8601 return SDValue();
8602 M = VecInMap.insert(std::make_pair(ExtractedFromVec, 0)).first;
8603 }
8604 M->second |= 1U << cast<ConstantSDNode>(Idx)->getZExtValue();
8605 }
8606
8607 assert((VT.is128BitVector() || VT.is256BitVector()) &&
Michael Liao9aba7ea2012-09-13 20:30:16 +00008608 "Not extracted from 128-/256-bit vector.");
Michael Liaof966e4e2012-09-13 20:24:54 +00008609
8610 unsigned FullMask = (1U << VT.getVectorNumElements()) - 1U;
8611 SmallVector<SDValue, 8> VecIns;
8612
8613 for (DenseMap<SDValue, unsigned>::const_iterator
8614 I = VecInMap.begin(), E = VecInMap.end(); I != E; ++I) {
8615 // Quit if not all elements are used.
8616 if (I->second != FullMask)
8617 return SDValue();
8618 VecIns.push_back(I->first);
8619 }
8620
8621 EVT TestVT = VT.is128BitVector() ? MVT::v2i64 : MVT::v4i64;
8622
8623 // Cast all vectors into TestVT for PTEST.
8624 for (unsigned i = 0, e = VecIns.size(); i < e; ++i)
8625 VecIns[i] = DAG.getNode(ISD::BITCAST, DL, TestVT, VecIns[i]);
8626
8627 // If more than one full vectors are evaluated, OR them first before PTEST.
8628 for (unsigned Slot = 0, e = VecIns.size(); e - Slot > 1; Slot += 2, e += 1) {
8629 // Each iteration will OR 2 nodes and append the result until there is only
8630 // 1 node left, i.e. the final OR'd value of all vectors.
8631 SDValue LHS = VecIns[Slot];
8632 SDValue RHS = VecIns[Slot + 1];
8633 VecIns.push_back(DAG.getNode(ISD::OR, DL, TestVT, LHS, RHS));
8634 }
8635
8636 return DAG.getNode(X86ISD::PTEST, DL, MVT::i32,
8637 VecIns.back(), VecIns.back());
8638}
8639
Dan Gohman076aee32009-03-04 19:44:21 +00008640/// Emit nodes that will be selected as "test Op0,Op0", or something
8641/// equivalent.
Dan Gohman31125812009-03-07 01:58:32 +00008642SDValue X86TargetLowering::EmitTest(SDValue Op, unsigned X86CC,
Evan Cheng552f09a2010-04-26 19:06:11 +00008643 SelectionDAG &DAG) const {
Dan Gohman076aee32009-03-04 19:44:21 +00008644 DebugLoc dl = Op.getDebugLoc();
8645
Dan Gohman31125812009-03-07 01:58:32 +00008646 // CF and OF aren't always set the way we want. Determine which
8647 // of these we need.
8648 bool NeedCF = false;
8649 bool NeedOF = false;
8650 switch (X86CC) {
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008651 default: break;
Dan Gohman31125812009-03-07 01:58:32 +00008652 case X86::COND_A: case X86::COND_AE:
8653 case X86::COND_B: case X86::COND_BE:
8654 NeedCF = true;
8655 break;
8656 case X86::COND_G: case X86::COND_GE:
8657 case X86::COND_L: case X86::COND_LE:
8658 case X86::COND_O: case X86::COND_NO:
8659 NeedOF = true;
8660 break;
Dan Gohman31125812009-03-07 01:58:32 +00008661 }
8662
Dan Gohman076aee32009-03-04 19:44:21 +00008663 // See if we can use the EFLAGS value from the operand instead of
Dan Gohman31125812009-03-07 01:58:32 +00008664 // doing a separate TEST. TEST always sets OF and CF to 0, so unless
8665 // we prove that the arithmetic won't overflow, we can't use OF or CF.
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008666 if (Op.getResNo() != 0 || NeedOF || NeedCF)
8667 // Emit a CMP with 0, which is the TEST pattern.
8668 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op,
8669 DAG.getConstant(0, Op.getValueType()));
8670
8671 unsigned Opcode = 0;
8672 unsigned NumOperands = 0;
Nadav Rotemb9d6b842012-08-18 17:53:03 +00008673
8674 // Truncate operations may prevent the merge of the SETCC instruction
8675 // and the arithmetic intruction before it. Attempt to truncate the operands
8676 // of the arithmetic instruction and use a reduced bit-width instruction.
8677 bool NeedTruncation = false;
8678 SDValue ArithOp = Op;
8679 if (Op->getOpcode() == ISD::TRUNCATE && Op->hasOneUse()) {
8680 SDValue Arith = Op->getOperand(0);
8681 // Both the trunc and the arithmetic op need to have one user each.
8682 if (Arith->hasOneUse())
8683 switch (Arith.getOpcode()) {
8684 default: break;
8685 case ISD::ADD:
8686 case ISD::SUB:
8687 case ISD::AND:
8688 case ISD::OR:
8689 case ISD::XOR: {
8690 NeedTruncation = true;
8691 ArithOp = Arith;
8692 }
8693 }
8694 }
8695
8696 // NOTICE: In the code below we use ArithOp to hold the arithmetic operation
8697 // which may be the result of a CAST. We use the variable 'Op', which is the
8698 // non-casted variable when we check for possible users.
8699 switch (ArithOp.getOpcode()) {
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008700 case ISD::ADD:
8701 // Due to an isel shortcoming, be conservative if this add is likely to be
8702 // selected as part of a load-modify-store instruction. When the root node
8703 // in a match is a store, isel doesn't know how to remap non-chain non-flag
8704 // uses of other nodes in the match, such as the ADD in this case. This
8705 // leads to the ADD being left around and reselected, with the result being
8706 // two adds in the output. Alas, even if none our users are stores, that
8707 // doesn't prove we're O.K. Ergo, if we have any parents that aren't
8708 // CopyToReg or SETCC, eschew INC/DEC. A better fix seems to require
8709 // climbing the DAG back to the root, and it doesn't seem to be worth the
8710 // effort.
8711 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
Pete Cooper2d496892011-11-15 21:57:53 +00008712 UE = Op.getNode()->use_end(); UI != UE; ++UI)
8713 if (UI->getOpcode() != ISD::CopyToReg &&
8714 UI->getOpcode() != ISD::SETCC &&
8715 UI->getOpcode() != ISD::STORE)
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008716 goto default_case;
8717
8718 if (ConstantSDNode *C =
Nadav Rotemb9d6b842012-08-18 17:53:03 +00008719 dyn_cast<ConstantSDNode>(ArithOp.getNode()->getOperand(1))) {
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008720 // An add of one will be selected as an INC.
8721 if (C->getAPIntValue() == 1) {
8722 Opcode = X86ISD::INC;
8723 NumOperands = 1;
8724 break;
Dan Gohmane220c4b2009-09-18 19:59:53 +00008725 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008726
8727 // An add of negative one (subtract of one) will be selected as a DEC.
8728 if (C->getAPIntValue().isAllOnesValue()) {
8729 Opcode = X86ISD::DEC;
8730 NumOperands = 1;
8731 break;
8732 }
Dan Gohman076aee32009-03-04 19:44:21 +00008733 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008734
8735 // Otherwise use a regular EFLAGS-setting add.
8736 Opcode = X86ISD::ADD;
8737 NumOperands = 2;
8738 break;
8739 case ISD::AND: {
8740 // If the primary and result isn't used, don't bother using X86ISD::AND,
8741 // because a TEST instruction will be better.
8742 bool NonFlagUse = false;
8743 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
8744 UE = Op.getNode()->use_end(); UI != UE; ++UI) {
8745 SDNode *User = *UI;
8746 unsigned UOpNo = UI.getOperandNo();
8747 if (User->getOpcode() == ISD::TRUNCATE && User->hasOneUse()) {
8748 // Look pass truncate.
8749 UOpNo = User->use_begin().getOperandNo();
8750 User = *User->use_begin();
8751 }
8752
8753 if (User->getOpcode() != ISD::BRCOND &&
8754 User->getOpcode() != ISD::SETCC &&
Nadav Rotemb9d6b842012-08-18 17:53:03 +00008755 !(User->getOpcode() == ISD::SELECT && UOpNo == 0)) {
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008756 NonFlagUse = true;
8757 break;
8758 }
Dan Gohman076aee32009-03-04 19:44:21 +00008759 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008760
8761 if (!NonFlagUse)
8762 break;
8763 }
8764 // FALL THROUGH
8765 case ISD::SUB:
8766 case ISD::OR:
8767 case ISD::XOR:
8768 // Due to the ISEL shortcoming noted above, be conservative if this op is
8769 // likely to be selected as part of a load-modify-store instruction.
8770 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
8771 UE = Op.getNode()->use_end(); UI != UE; ++UI)
8772 if (UI->getOpcode() == ISD::STORE)
8773 goto default_case;
8774
8775 // Otherwise use a regular EFLAGS-setting instruction.
Nadav Rotemb9d6b842012-08-18 17:53:03 +00008776 switch (ArithOp.getOpcode()) {
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008777 default: llvm_unreachable("unexpected operator!");
Nadav Rotemb9d6b842012-08-18 17:53:03 +00008778 case ISD::SUB: Opcode = X86ISD::SUB; break;
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008779 case ISD::XOR: Opcode = X86ISD::XOR; break;
8780 case ISD::AND: Opcode = X86ISD::AND; break;
Michael Liaof966e4e2012-09-13 20:24:54 +00008781 case ISD::OR: {
8782 if (!NeedTruncation && (X86CC == X86::COND_E || X86CC == X86::COND_NE)) {
8783 SDValue EFLAGS = LowerVectorAllZeroTest(Op, DAG);
8784 if (EFLAGS.getNode())
8785 return EFLAGS;
8786 }
8787 Opcode = X86ISD::OR;
8788 break;
8789 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008790 }
8791
8792 NumOperands = 2;
8793 break;
8794 case X86ISD::ADD:
8795 case X86ISD::SUB:
8796 case X86ISD::INC:
8797 case X86ISD::DEC:
8798 case X86ISD::OR:
8799 case X86ISD::XOR:
8800 case X86ISD::AND:
8801 return SDValue(Op.getNode(), 1);
8802 default:
8803 default_case:
8804 break;
Dan Gohman076aee32009-03-04 19:44:21 +00008805 }
8806
Nadav Rotemb9d6b842012-08-18 17:53:03 +00008807 // If we found that truncation is beneficial, perform the truncation and
8808 // update 'Op'.
8809 if (NeedTruncation) {
8810 EVT VT = Op.getValueType();
8811 SDValue WideVal = Op->getOperand(0);
8812 EVT WideVT = WideVal.getValueType();
8813 unsigned ConvertedOp = 0;
8814 // Use a target machine opcode to prevent further DAGCombine
8815 // optimizations that may separate the arithmetic operations
8816 // from the setcc node.
8817 switch (WideVal.getOpcode()) {
8818 default: break;
8819 case ISD::ADD: ConvertedOp = X86ISD::ADD; break;
8820 case ISD::SUB: ConvertedOp = X86ISD::SUB; break;
8821 case ISD::AND: ConvertedOp = X86ISD::AND; break;
8822 case ISD::OR: ConvertedOp = X86ISD::OR; break;
8823 case ISD::XOR: ConvertedOp = X86ISD::XOR; break;
8824 }
8825
8826 if (ConvertedOp) {
8827 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
8828 if (TLI.isOperationLegal(WideVal.getOpcode(), WideVT)) {
8829 SDValue V0 = DAG.getNode(ISD::TRUNCATE, dl, VT, WideVal.getOperand(0));
8830 SDValue V1 = DAG.getNode(ISD::TRUNCATE, dl, VT, WideVal.getOperand(1));
8831 Op = DAG.getNode(ConvertedOp, dl, VT, V0, V1);
8832 }
8833 }
8834 }
8835
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008836 if (Opcode == 0)
8837 // Emit a CMP with 0, which is the TEST pattern.
8838 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op,
8839 DAG.getConstant(0, Op.getValueType()));
8840
8841 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::i32);
8842 SmallVector<SDValue, 4> Ops;
8843 for (unsigned i = 0; i != NumOperands; ++i)
8844 Ops.push_back(Op.getOperand(i));
8845
8846 SDValue New = DAG.getNode(Opcode, dl, VTs, &Ops[0], NumOperands);
8847 DAG.ReplaceAllUsesWith(Op, New);
8848 return SDValue(New.getNode(), 1);
Dan Gohman076aee32009-03-04 19:44:21 +00008849}
8850
8851/// Emit nodes that will be selected as "cmp Op0,Op1", or something
8852/// equivalent.
Dan Gohman31125812009-03-07 01:58:32 +00008853SDValue X86TargetLowering::EmitCmp(SDValue Op0, SDValue Op1, unsigned X86CC,
Evan Cheng552f09a2010-04-26 19:06:11 +00008854 SelectionDAG &DAG) const {
Dan Gohman076aee32009-03-04 19:44:21 +00008855 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op1))
8856 if (C->getAPIntValue() == 0)
Evan Cheng552f09a2010-04-26 19:06:11 +00008857 return EmitTest(Op0, X86CC, DAG);
Dan Gohman076aee32009-03-04 19:44:21 +00008858
8859 DebugLoc dl = Op0.getDebugLoc();
Manman Ren39ad5682012-08-08 00:51:41 +00008860 if ((Op0.getValueType() == MVT::i8 || Op0.getValueType() == MVT::i16 ||
8861 Op0.getValueType() == MVT::i32 || Op0.getValueType() == MVT::i64)) {
8862 // Use SUB instead of CMP to enable CSE between SUB and CMP.
8863 SDVTList VTs = DAG.getVTList(Op0.getValueType(), MVT::i32);
8864 SDValue Sub = DAG.getNode(X86ISD::SUB, dl, VTs,
8865 Op0, Op1);
8866 return SDValue(Sub.getNode(), 1);
8867 }
Owen Anderson825b72b2009-08-11 20:47:22 +00008868 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op0, Op1);
Dan Gohman076aee32009-03-04 19:44:21 +00008869}
8870
Benjamin Kramer17c836c2012-04-27 12:07:43 +00008871/// Convert a comparison if required by the subtarget.
8872SDValue X86TargetLowering::ConvertCmpIfNecessary(SDValue Cmp,
8873 SelectionDAG &DAG) const {
8874 // If the subtarget does not support the FUCOMI instruction, floating-point
8875 // comparisons have to be converted.
8876 if (Subtarget->hasCMov() ||
8877 Cmp.getOpcode() != X86ISD::CMP ||
8878 !Cmp.getOperand(0).getValueType().isFloatingPoint() ||
8879 !Cmp.getOperand(1).getValueType().isFloatingPoint())
8880 return Cmp;
8881
8882 // The instruction selector will select an FUCOM instruction instead of
8883 // FUCOMI, which writes the comparison result to FPSW instead of EFLAGS. Hence
8884 // build an SDNode sequence that transfers the result from FPSW into EFLAGS:
8885 // (X86sahf (trunc (srl (X86fp_stsw (trunc (X86cmp ...)), 8))))
8886 DebugLoc dl = Cmp.getDebugLoc();
8887 SDValue TruncFPSW = DAG.getNode(ISD::TRUNCATE, dl, MVT::i16, Cmp);
8888 SDValue FNStSW = DAG.getNode(X86ISD::FNSTSW16r, dl, MVT::i16, TruncFPSW);
8889 SDValue Srl = DAG.getNode(ISD::SRL, dl, MVT::i16, FNStSW,
8890 DAG.getConstant(8, MVT::i8));
8891 SDValue TruncSrl = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Srl);
8892 return DAG.getNode(X86ISD::SAHF, dl, MVT::i32, TruncSrl);
8893}
8894
Evan Cheng4e544802012-12-05 00:10:38 +00008895static bool isAllOnes(SDValue V) {
8896 ConstantSDNode *C = dyn_cast<ConstantSDNode>(V);
8897 return C && C->isAllOnesValue();
8898}
8899
Evan Chengd40d03e2010-01-06 19:38:29 +00008900/// LowerToBT - Result of 'and' is compared against zero. Turn it into a BT node
8901/// if it's possible.
Evan Cheng5528e7b2010-04-21 01:47:12 +00008902SDValue X86TargetLowering::LowerToBT(SDValue And, ISD::CondCode CC,
8903 DebugLoc dl, SelectionDAG &DAG) const {
Evan Cheng2c755ba2010-02-27 07:36:59 +00008904 SDValue Op0 = And.getOperand(0);
8905 SDValue Op1 = And.getOperand(1);
8906 if (Op0.getOpcode() == ISD::TRUNCATE)
8907 Op0 = Op0.getOperand(0);
8908 if (Op1.getOpcode() == ISD::TRUNCATE)
8909 Op1 = Op1.getOperand(0);
8910
Evan Chengd40d03e2010-01-06 19:38:29 +00008911 SDValue LHS, RHS;
Dan Gohman6b13cbc2010-06-24 02:07:59 +00008912 if (Op1.getOpcode() == ISD::SHL)
8913 std::swap(Op0, Op1);
8914 if (Op0.getOpcode() == ISD::SHL) {
Evan Cheng2c755ba2010-02-27 07:36:59 +00008915 if (ConstantSDNode *And00C = dyn_cast<ConstantSDNode>(Op0.getOperand(0)))
8916 if (And00C->getZExtValue() == 1) {
Dan Gohman6b13cbc2010-06-24 02:07:59 +00008917 // If we looked past a truncate, check that it's only truncating away
8918 // known zeros.
8919 unsigned BitWidth = Op0.getValueSizeInBits();
8920 unsigned AndBitWidth = And.getValueSizeInBits();
8921 if (BitWidth > AndBitWidth) {
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00008922 APInt Zeros, Ones;
8923 DAG.ComputeMaskedBits(Op0, Zeros, Ones);
Dan Gohman6b13cbc2010-06-24 02:07:59 +00008924 if (Zeros.countLeadingOnes() < BitWidth - AndBitWidth)
8925 return SDValue();
8926 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00008927 LHS = Op1;
8928 RHS = Op0.getOperand(1);
Evan Chengd40d03e2010-01-06 19:38:29 +00008929 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00008930 } else if (Op1.getOpcode() == ISD::Constant) {
8931 ConstantSDNode *AndRHS = cast<ConstantSDNode>(Op1);
Benjamin Kramerf238f502011-11-23 13:54:17 +00008932 uint64_t AndRHSVal = AndRHS->getZExtValue();
Evan Cheng2c755ba2010-02-27 07:36:59 +00008933 SDValue AndLHS = Op0;
Benjamin Kramerf238f502011-11-23 13:54:17 +00008934
8935 if (AndRHSVal == 1 && AndLHS.getOpcode() == ISD::SRL) {
Evan Chengd40d03e2010-01-06 19:38:29 +00008936 LHS = AndLHS.getOperand(0);
8937 RHS = AndLHS.getOperand(1);
Dan Gohmane5af2d32009-01-29 01:59:02 +00008938 }
Benjamin Kramerf238f502011-11-23 13:54:17 +00008939
8940 // Use BT if the immediate can't be encoded in a TEST instruction.
8941 if (!isUInt<32>(AndRHSVal) && isPowerOf2_64(AndRHSVal)) {
8942 LHS = AndLHS;
8943 RHS = DAG.getConstant(Log2_64_Ceil(AndRHSVal), LHS.getValueType());
8944 }
Evan Chengd40d03e2010-01-06 19:38:29 +00008945 }
Evan Cheng0488db92007-09-25 01:57:46 +00008946
Evan Chengd40d03e2010-01-06 19:38:29 +00008947 if (LHS.getNode()) {
Evan Cheng4e544802012-12-05 00:10:38 +00008948 // If the LHS is of the form (x ^ -1) then replace the LHS with x and flip
8949 // the condition code later.
8950 bool Invert = false;
8951 if (LHS.getOpcode() == ISD::XOR && isAllOnes(LHS.getOperand(1))) {
8952 Invert = true;
8953 LHS = LHS.getOperand(0);
8954 }
8955
Evan Chenge5b51ac2010-04-17 06:13:15 +00008956 // If LHS is i8, promote it to i32 with any_extend. There is no i8 BT
Evan Chengd40d03e2010-01-06 19:38:29 +00008957 // instruction. Since the shift amount is in-range-or-undefined, we know
Evan Chenge5b51ac2010-04-17 06:13:15 +00008958 // that doing a bittest on the i32 value is ok. We extend to i32 because
Evan Chengd40d03e2010-01-06 19:38:29 +00008959 // the encoding for the i16 version is larger than the i32 version.
Evan Chenge5b51ac2010-04-17 06:13:15 +00008960 // Also promote i16 to i32 for performance / code size reason.
8961 if (LHS.getValueType() == MVT::i8 ||
Evan Cheng2bce5f4b2010-04-28 08:30:49 +00008962 LHS.getValueType() == MVT::i16)
Evan Chengd40d03e2010-01-06 19:38:29 +00008963 LHS = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, LHS);
Chris Lattnere55484e2008-12-25 05:34:37 +00008964
Evan Chengd40d03e2010-01-06 19:38:29 +00008965 // If the operand types disagree, extend the shift amount to match. Since
8966 // BT ignores high bits (like shifts) we can use anyextend.
8967 if (LHS.getValueType() != RHS.getValueType())
8968 RHS = DAG.getNode(ISD::ANY_EXTEND, dl, LHS.getValueType(), RHS);
Dan Gohmane5af2d32009-01-29 01:59:02 +00008969
Evan Chengd40d03e2010-01-06 19:38:29 +00008970 SDValue BT = DAG.getNode(X86ISD::BT, dl, MVT::i32, LHS, RHS);
Evan Cheng4e544802012-12-05 00:10:38 +00008971 X86::CondCode Cond = CC == ISD::SETEQ ? X86::COND_AE : X86::COND_B;
8972 // Flip the condition if the LHS was a not instruction
8973 if (Invert)
8974 Cond = X86::GetOppositeBranchCondition(Cond);
Evan Chengd40d03e2010-01-06 19:38:29 +00008975 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
8976 DAG.getConstant(Cond, MVT::i8), BT);
Chris Lattnere55484e2008-12-25 05:34:37 +00008977 }
8978
Evan Cheng54de3ea2010-01-05 06:52:31 +00008979 return SDValue();
8980}
8981
Dan Gohmand858e902010-04-17 15:26:15 +00008982SDValue X86TargetLowering::LowerSETCC(SDValue Op, SelectionDAG &DAG) const {
Duncan Sands28b77e92011-09-06 19:07:46 +00008983
8984 if (Op.getValueType().isVector()) return LowerVSETCC(Op, DAG);
8985
Evan Cheng54de3ea2010-01-05 06:52:31 +00008986 assert(Op.getValueType() == MVT::i8 && "SetCC type must be 8-bit integer");
8987 SDValue Op0 = Op.getOperand(0);
8988 SDValue Op1 = Op.getOperand(1);
8989 DebugLoc dl = Op.getDebugLoc();
8990 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
8991
8992 // Optimize to BT if possible.
Evan Chengd40d03e2010-01-06 19:38:29 +00008993 // Lower (X & (1 << N)) == 0 to BT(X, N).
8994 // Lower ((X >>u N) & 1) != 0 to BT(X, N).
8995 // Lower ((X >>s N) & 1) != 0 to BT(X, N).
Andrew Trickf6c39412011-03-23 23:11:02 +00008996 if (Op0.getOpcode() == ISD::AND && Op0.hasOneUse() &&
Evan Chengd40d03e2010-01-06 19:38:29 +00008997 Op1.getOpcode() == ISD::Constant &&
Dan Gohmane368b462010-06-18 14:22:04 +00008998 cast<ConstantSDNode>(Op1)->isNullValue() &&
Evan Chengd40d03e2010-01-06 19:38:29 +00008999 (CC == ISD::SETEQ || CC == ISD::SETNE)) {
9000 SDValue NewSetCC = LowerToBT(Op0, CC, dl, DAG);
9001 if (NewSetCC.getNode())
9002 return NewSetCC;
9003 }
Evan Cheng54de3ea2010-01-05 06:52:31 +00009004
Chris Lattner481eebc2010-12-19 21:23:48 +00009005 // Look for X == 0, X == 1, X != 0, or X != 1. We can simplify some forms of
9006 // these.
9007 if (Op1.getOpcode() == ISD::Constant &&
Andrew Trickf6c39412011-03-23 23:11:02 +00009008 (cast<ConstantSDNode>(Op1)->getZExtValue() == 1 ||
Evan Cheng2c755ba2010-02-27 07:36:59 +00009009 cast<ConstantSDNode>(Op1)->isNullValue()) &&
9010 (CC == ISD::SETEQ || CC == ISD::SETNE)) {
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009011
Chris Lattner481eebc2010-12-19 21:23:48 +00009012 // If the input is a setcc, then reuse the input setcc or use a new one with
9013 // the inverted condition.
9014 if (Op0.getOpcode() == X86ISD::SETCC) {
9015 X86::CondCode CCode = (X86::CondCode)Op0.getConstantOperandVal(0);
9016 bool Invert = (CC == ISD::SETNE) ^
9017 cast<ConstantSDNode>(Op1)->isNullValue();
9018 if (!Invert) return Op0;
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009019
Evan Cheng2c755ba2010-02-27 07:36:59 +00009020 CCode = X86::GetOppositeBranchCondition(CCode);
Chris Lattner481eebc2010-12-19 21:23:48 +00009021 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
9022 DAG.getConstant(CCode, MVT::i8), Op0.getOperand(1));
9023 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00009024 }
9025
Evan Chenge5b51ac2010-04-17 06:13:15 +00009026 bool isFP = Op1.getValueType().isFloatingPoint();
Chris Lattnere55484e2008-12-25 05:34:37 +00009027 unsigned X86CC = TranslateX86CC(CC, isFP, Op0, Op1, DAG);
Dan Gohman1a492952009-10-20 16:22:37 +00009028 if (X86CC == X86::COND_INVALID)
9029 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00009030
Chris Lattnerc19d1c32010-12-19 22:08:31 +00009031 SDValue EFLAGS = EmitCmp(Op0, Op1, X86CC, DAG);
Benjamin Kramer17c836c2012-04-27 12:07:43 +00009032 EFLAGS = ConvertCmpIfNecessary(EFLAGS, DAG);
Owen Anderson825b72b2009-08-11 20:47:22 +00009033 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
Chris Lattnerc19d1c32010-12-19 22:08:31 +00009034 DAG.getConstant(X86CC, MVT::i8), EFLAGS);
Evan Cheng0488db92007-09-25 01:57:46 +00009035}
9036
Craig Topper89af15e2011-09-18 08:03:58 +00009037// Lower256IntVSETCC - Break a VSETCC 256-bit integer VSETCC into two new 128
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00009038// ones, and then concatenate the result back.
Craig Topper89af15e2011-09-18 08:03:58 +00009039static SDValue Lower256IntVSETCC(SDValue Op, SelectionDAG &DAG) {
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00009040 EVT VT = Op.getValueType();
9041
Craig Topper7a9a28b2012-08-12 02:23:29 +00009042 assert(VT.is256BitVector() && Op.getOpcode() == ISD::SETCC &&
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00009043 "Unsupported value type for operation");
9044
Craig Topper66ddd152012-04-27 22:54:43 +00009045 unsigned NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00009046 DebugLoc dl = Op.getDebugLoc();
9047 SDValue CC = Op.getOperand(2);
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00009048
9049 // Extract the LHS vectors
9050 SDValue LHS = Op.getOperand(0);
Craig Topperb14940a2012-04-22 20:55:18 +00009051 SDValue LHS1 = Extract128BitVector(LHS, 0, DAG, dl);
9052 SDValue LHS2 = Extract128BitVector(LHS, NumElems/2, DAG, dl);
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00009053
9054 // Extract the RHS vectors
9055 SDValue RHS = Op.getOperand(1);
Craig Topperb14940a2012-04-22 20:55:18 +00009056 SDValue RHS1 = Extract128BitVector(RHS, 0, DAG, dl);
9057 SDValue RHS2 = Extract128BitVector(RHS, NumElems/2, DAG, dl);
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00009058
9059 // Issue the operation on the smaller types and concatenate the result back
9060 MVT EltVT = VT.getVectorElementType().getSimpleVT();
9061 EVT NewVT = MVT::getVectorVT(EltVT, NumElems/2);
9062 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT,
9063 DAG.getNode(Op.getOpcode(), dl, NewVT, LHS1, RHS1, CC),
9064 DAG.getNode(Op.getOpcode(), dl, NewVT, LHS2, RHS2, CC));
9065}
9066
Dan Gohmand858e902010-04-17 15:26:15 +00009067SDValue X86TargetLowering::LowerVSETCC(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +00009068 SDValue Cond;
9069 SDValue Op0 = Op.getOperand(0);
9070 SDValue Op1 = Op.getOperand(1);
9071 SDValue CC = Op.getOperand(2);
Owen Andersone50ed302009-08-10 22:56:29 +00009072 EVT VT = Op.getValueType();
Nate Begeman30a0de92008-07-17 16:51:19 +00009073 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
9074 bool isFP = Op.getOperand(1).getValueType().isFloatingPoint();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009075 DebugLoc dl = Op.getDebugLoc();
Nate Begeman30a0de92008-07-17 16:51:19 +00009076
9077 if (isFP) {
Craig Topper523908d2012-08-13 02:34:03 +00009078#ifndef NDEBUG
Bruno Cardoso Lopes0f0e0a02011-08-09 00:46:57 +00009079 EVT EltVT = Op0.getValueType().getVectorElementType();
Craig Topper523908d2012-08-13 02:34:03 +00009080 assert(EltVT == MVT::f32 || EltVT == MVT::f64);
9081#endif
Bruno Cardoso Lopes0f0e0a02011-08-09 00:46:57 +00009082
Craig Topper523908d2012-08-13 02:34:03 +00009083 unsigned SSECC;
Nate Begeman30a0de92008-07-17 16:51:19 +00009084 bool Swap = false;
9085
Bruno Cardoso Lopes8e03a822011-09-12 19:30:40 +00009086 // SSE Condition code mapping:
9087 // 0 - EQ
9088 // 1 - LT
9089 // 2 - LE
9090 // 3 - UNORD
9091 // 4 - NEQ
9092 // 5 - NLT
9093 // 6 - NLE
9094 // 7 - ORD
Nate Begeman30a0de92008-07-17 16:51:19 +00009095 switch (SetCCOpcode) {
Craig Topper2f1b2ec2012-08-13 03:42:38 +00009096 default: llvm_unreachable("Unexpected SETCC condition");
Nate Begemanfb8ead02008-07-25 19:05:58 +00009097 case ISD::SETOEQ:
Nate Begeman30a0de92008-07-17 16:51:19 +00009098 case ISD::SETEQ: SSECC = 0; break;
Bruno Cardoso Lopes8e03a822011-09-12 19:30:40 +00009099 case ISD::SETOGT:
9100 case ISD::SETGT: Swap = true; // Fallthrough
Bruno Cardoso Lopes457d53d2011-09-12 21:24:07 +00009101 case ISD::SETLT:
9102 case ISD::SETOLT: SSECC = 1; break;
9103 case ISD::SETOGE:
9104 case ISD::SETGE: Swap = true; // Fallthrough
Nate Begeman30a0de92008-07-17 16:51:19 +00009105 case ISD::SETLE:
9106 case ISD::SETOLE: SSECC = 2; break;
9107 case ISD::SETUO: SSECC = 3; break;
Nate Begemanfb8ead02008-07-25 19:05:58 +00009108 case ISD::SETUNE:
Nate Begeman30a0de92008-07-17 16:51:19 +00009109 case ISD::SETNE: SSECC = 4; break;
Craig Topper523908d2012-08-13 02:34:03 +00009110 case ISD::SETULE: Swap = true; // Fallthrough
Nate Begeman30a0de92008-07-17 16:51:19 +00009111 case ISD::SETUGE: SSECC = 5; break;
Craig Topper523908d2012-08-13 02:34:03 +00009112 case ISD::SETULT: Swap = true; // Fallthrough
Nate Begeman30a0de92008-07-17 16:51:19 +00009113 case ISD::SETUGT: SSECC = 6; break;
9114 case ISD::SETO: SSECC = 7; break;
Craig Topper523908d2012-08-13 02:34:03 +00009115 case ISD::SETUEQ:
9116 case ISD::SETONE: SSECC = 8; break;
Nate Begeman30a0de92008-07-17 16:51:19 +00009117 }
9118 if (Swap)
9119 std::swap(Op0, Op1);
9120
Nate Begemanfb8ead02008-07-25 19:05:58 +00009121 // In the two special cases we can't handle, emit two comparisons.
Nate Begeman30a0de92008-07-17 16:51:19 +00009122 if (SSECC == 8) {
Craig Topper523908d2012-08-13 02:34:03 +00009123 unsigned CC0, CC1;
9124 unsigned CombineOpc;
Nate Begemanfb8ead02008-07-25 19:05:58 +00009125 if (SetCCOpcode == ISD::SETUEQ) {
Craig Topper523908d2012-08-13 02:34:03 +00009126 CC0 = 3; CC1 = 0; CombineOpc = ISD::OR;
9127 } else {
9128 assert(SetCCOpcode == ISD::SETONE);
9129 CC0 = 7; CC1 = 4; CombineOpc = ISD::AND;
Craig Topper69947b92012-04-23 06:57:04 +00009130 }
Craig Topper523908d2012-08-13 02:34:03 +00009131
9132 SDValue Cmp0 = DAG.getNode(X86ISD::CMPP, dl, VT, Op0, Op1,
9133 DAG.getConstant(CC0, MVT::i8));
9134 SDValue Cmp1 = DAG.getNode(X86ISD::CMPP, dl, VT, Op0, Op1,
9135 DAG.getConstant(CC1, MVT::i8));
9136 return DAG.getNode(CombineOpc, dl, VT, Cmp0, Cmp1);
Nate Begeman30a0de92008-07-17 16:51:19 +00009137 }
9138 // Handle all other FP comparisons here.
Craig Topper1906d322012-01-22 23:36:02 +00009139 return DAG.getNode(X86ISD::CMPP, dl, VT, Op0, Op1,
9140 DAG.getConstant(SSECC, MVT::i8));
Nate Begeman30a0de92008-07-17 16:51:19 +00009141 }
Scott Michelfdc40a02009-02-17 22:15:04 +00009142
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00009143 // Break 256-bit integer vector compare into smaller ones.
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00009144 if (VT.is256BitVector() && !Subtarget->hasInt256())
Craig Topper89af15e2011-09-18 08:03:58 +00009145 return Lower256IntVSETCC(Op, DAG);
Bruno Cardoso Lopes0f0e0a02011-08-09 00:46:57 +00009146
Nate Begeman30a0de92008-07-17 16:51:19 +00009147 // We are handling one of the integer comparisons here. Since SSE only has
9148 // GT and EQ comparisons for integer, swapping operands and multiple
9149 // operations may be required for some comparisons.
Craig Topper2f1b2ec2012-08-13 03:42:38 +00009150 unsigned Opc;
Nate Begeman30a0de92008-07-17 16:51:19 +00009151 bool Swap = false, Invert = false, FlipSigns = false;
Scott Michelfdc40a02009-02-17 22:15:04 +00009152
Nate Begeman30a0de92008-07-17 16:51:19 +00009153 switch (SetCCOpcode) {
Craig Topper2f1b2ec2012-08-13 03:42:38 +00009154 default: llvm_unreachable("Unexpected SETCC condition");
Nate Begeman30a0de92008-07-17 16:51:19 +00009155 case ISD::SETNE: Invert = true;
Craig Topper67609fd2012-01-22 22:42:16 +00009156 case ISD::SETEQ: Opc = X86ISD::PCMPEQ; break;
Nate Begeman30a0de92008-07-17 16:51:19 +00009157 case ISD::SETLT: Swap = true;
Craig Topper67609fd2012-01-22 22:42:16 +00009158 case ISD::SETGT: Opc = X86ISD::PCMPGT; break;
Nate Begeman30a0de92008-07-17 16:51:19 +00009159 case ISD::SETGE: Swap = true;
Craig Topper67609fd2012-01-22 22:42:16 +00009160 case ISD::SETLE: Opc = X86ISD::PCMPGT; Invert = true; break;
Nate Begeman30a0de92008-07-17 16:51:19 +00009161 case ISD::SETULT: Swap = true;
Craig Topper67609fd2012-01-22 22:42:16 +00009162 case ISD::SETUGT: Opc = X86ISD::PCMPGT; FlipSigns = true; break;
Nate Begeman30a0de92008-07-17 16:51:19 +00009163 case ISD::SETUGE: Swap = true;
Craig Topper67609fd2012-01-22 22:42:16 +00009164 case ISD::SETULE: Opc = X86ISD::PCMPGT; FlipSigns = true; Invert = true; break;
Nate Begeman30a0de92008-07-17 16:51:19 +00009165 }
9166 if (Swap)
9167 std::swap(Op0, Op1);
Scott Michelfdc40a02009-02-17 22:15:04 +00009168
Eli Friedman7d3e2b72011-09-28 21:00:25 +00009169 // Check that the operation in question is available (most are plain SSE2,
9170 // but PCMPGTQ and PCMPEQQ have different requirements).
Craig Topper2f1b2ec2012-08-13 03:42:38 +00009171 if (VT == MVT::v2i64) {
9172 if (Opc == X86ISD::PCMPGT && !Subtarget->hasSSE42())
9173 return SDValue();
Benjamin Kramer382ed782012-12-25 12:54:19 +00009174 if (Opc == X86ISD::PCMPEQ && !Subtarget->hasSSE41()) {
9175 // If pcmpeqq is missing but pcmpeqd is available synthesize pcmpeqq with
Benjamin Kramer99f78062012-12-25 13:09:08 +00009176 // pcmpeqd + pshufd + pand.
Benjamin Kramer382ed782012-12-25 12:54:19 +00009177 assert(Subtarget->hasSSE2() && !FlipSigns && "Don't know how to lower!");
9178
9179 // First cast everything to the right type,
9180 Op0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, Op0);
9181 Op1 = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, Op1);
9182
9183 // Do the compare.
9184 SDValue Result = DAG.getNode(Opc, dl, MVT::v4i32, Op0, Op1);
9185
9186 // Make sure the lower and upper halves are both all-ones.
Benjamin Kramer99f78062012-12-25 13:09:08 +00009187 const int Mask[] = { 1, 0, 3, 2 };
9188 SDValue Shuf = DAG.getVectorShuffle(MVT::v4i32, dl, Result, Result, Mask);
9189 Result = DAG.getNode(ISD::AND, dl, MVT::v4i32, Result, Shuf);
Benjamin Kramer382ed782012-12-25 12:54:19 +00009190
9191 if (Invert)
9192 Result = DAG.getNOT(dl, Result, MVT::v4i32);
9193
9194 return DAG.getNode(ISD::BITCAST, dl, VT, Result);
9195 }
Craig Topper2f1b2ec2012-08-13 03:42:38 +00009196 }
Eli Friedman7d3e2b72011-09-28 21:00:25 +00009197
Nate Begeman30a0de92008-07-17 16:51:19 +00009198 // Since SSE has no unsigned integer comparisons, we need to flip the sign
9199 // bits of the inputs before performing those operations.
9200 if (FlipSigns) {
Owen Andersone50ed302009-08-10 22:56:29 +00009201 EVT EltVT = VT.getVectorElementType();
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00009202 SDValue SignBit = DAG.getConstant(APInt::getSignBit(EltVT.getSizeInBits()),
9203 EltVT);
Dan Gohman475871a2008-07-27 21:46:04 +00009204 std::vector<SDValue> SignBits(VT.getVectorNumElements(), SignBit);
Evan Chenga87008d2009-02-25 22:49:59 +00009205 SDValue SignVec = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &SignBits[0],
9206 SignBits.size());
Dale Johannesenace16102009-02-03 19:33:06 +00009207 Op0 = DAG.getNode(ISD::XOR, dl, VT, Op0, SignVec);
9208 Op1 = DAG.getNode(ISD::XOR, dl, VT, Op1, SignVec);
Nate Begeman30a0de92008-07-17 16:51:19 +00009209 }
Scott Michelfdc40a02009-02-17 22:15:04 +00009210
Dale Johannesenace16102009-02-03 19:33:06 +00009211 SDValue Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
Nate Begeman30a0de92008-07-17 16:51:19 +00009212
9213 // If the logical-not of the result is required, perform that now.
Bob Wilson4c245462009-01-22 17:39:32 +00009214 if (Invert)
Dale Johannesenace16102009-02-03 19:33:06 +00009215 Result = DAG.getNOT(dl, Result, VT);
Bob Wilson4c245462009-01-22 17:39:32 +00009216
Nate Begeman30a0de92008-07-17 16:51:19 +00009217 return Result;
9218}
Evan Cheng0488db92007-09-25 01:57:46 +00009219
Evan Cheng370e5342008-12-03 08:38:43 +00009220// isX86LogicalCmp - Return true if opcode is a X86 logical comparison.
Dan Gohman076aee32009-03-04 19:44:21 +00009221static bool isX86LogicalCmp(SDValue Op) {
9222 unsigned Opc = Op.getNode()->getOpcode();
Benjamin Kramer17c836c2012-04-27 12:07:43 +00009223 if (Opc == X86ISD::CMP || Opc == X86ISD::COMI || Opc == X86ISD::UCOMI ||
9224 Opc == X86ISD::SAHF)
Dan Gohman076aee32009-03-04 19:44:21 +00009225 return true;
9226 if (Op.getResNo() == 1 &&
9227 (Opc == X86ISD::ADD ||
9228 Opc == X86ISD::SUB ||
Chris Lattner5b856542010-12-20 00:59:46 +00009229 Opc == X86ISD::ADC ||
9230 Opc == X86ISD::SBB ||
Dan Gohman076aee32009-03-04 19:44:21 +00009231 Opc == X86ISD::SMUL ||
9232 Opc == X86ISD::UMUL ||
9233 Opc == X86ISD::INC ||
Dan Gohmane220c4b2009-09-18 19:59:53 +00009234 Opc == X86ISD::DEC ||
9235 Opc == X86ISD::OR ||
9236 Opc == X86ISD::XOR ||
9237 Opc == X86ISD::AND))
Dan Gohman076aee32009-03-04 19:44:21 +00009238 return true;
9239
Chris Lattner9637d5b2010-12-05 07:49:54 +00009240 if (Op.getResNo() == 2 && Opc == X86ISD::UMUL)
9241 return true;
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009242
Dan Gohman076aee32009-03-04 19:44:21 +00009243 return false;
Evan Cheng370e5342008-12-03 08:38:43 +00009244}
9245
Chris Lattnera2b56002010-12-05 01:23:24 +00009246static bool isZero(SDValue V) {
9247 ConstantSDNode *C = dyn_cast<ConstantSDNode>(V);
9248 return C && C->isNullValue();
9249}
9250
Evan Chengb64dd5f2012-08-07 22:21:00 +00009251static bool isTruncWithZeroHighBitsInput(SDValue V, SelectionDAG &DAG) {
9252 if (V.getOpcode() != ISD::TRUNCATE)
9253 return false;
9254
9255 SDValue VOp0 = V.getOperand(0);
9256 unsigned InBits = VOp0.getValueSizeInBits();
9257 unsigned Bits = V.getValueSizeInBits();
9258 return DAG.MaskedValueIsZero(VOp0, APInt::getHighBitsSet(InBits,InBits-Bits));
9259}
9260
Dan Gohmand858e902010-04-17 15:26:15 +00009261SDValue X86TargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng734503b2006-09-11 02:19:56 +00009262 bool addTest = true;
Dan Gohman475871a2008-07-27 21:46:04 +00009263 SDValue Cond = Op.getOperand(0);
Chris Lattnera2b56002010-12-05 01:23:24 +00009264 SDValue Op1 = Op.getOperand(1);
9265 SDValue Op2 = Op.getOperand(2);
9266 DebugLoc DL = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00009267 SDValue CC;
Evan Cheng9bba8942006-01-26 02:13:10 +00009268
Dan Gohman1a492952009-10-20 16:22:37 +00009269 if (Cond.getOpcode() == ISD::SETCC) {
9270 SDValue NewCond = LowerSETCC(Cond, DAG);
9271 if (NewCond.getNode())
9272 Cond = NewCond;
9273 }
Evan Cheng734503b2006-09-11 02:19:56 +00009274
Chris Lattnera2b56002010-12-05 01:23:24 +00009275 // (select (x == 0), -1, y) -> (sign_bit (x - 1)) | y
Chris Lattner96908b12010-12-05 02:00:51 +00009276 // (select (x == 0), y, -1) -> ~(sign_bit (x - 1)) | y
Chris Lattnera2b56002010-12-05 01:23:24 +00009277 // (select (x != 0), y, -1) -> (sign_bit (x - 1)) | y
Chris Lattner96908b12010-12-05 02:00:51 +00009278 // (select (x != 0), -1, y) -> ~(sign_bit (x - 1)) | y
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00009279 if (Cond.getOpcode() == X86ISD::SETCC &&
Chris Lattner96908b12010-12-05 02:00:51 +00009280 Cond.getOperand(1).getOpcode() == X86ISD::CMP &&
9281 isZero(Cond.getOperand(1).getOperand(1))) {
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00009282 SDValue Cmp = Cond.getOperand(1);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009283
Chris Lattnera2b56002010-12-05 01:23:24 +00009284 unsigned CondCode =cast<ConstantSDNode>(Cond.getOperand(0))->getZExtValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009285
9286 if ((isAllOnes(Op1) || isAllOnes(Op2)) &&
Chris Lattner96908b12010-12-05 02:00:51 +00009287 (CondCode == X86::COND_E || CondCode == X86::COND_NE)) {
9288 SDValue Y = isAllOnes(Op2) ? Op1 : Op2;
Chris Lattnera2b56002010-12-05 01:23:24 +00009289
9290 SDValue CmpOp0 = Cmp.getOperand(0);
Manman Rened579842012-05-07 18:06:23 +00009291 // Apply further optimizations for special cases
9292 // (select (x != 0), -1, 0) -> neg & sbb
9293 // (select (x == 0), 0, -1) -> neg & sbb
9294 if (ConstantSDNode *YC = dyn_cast<ConstantSDNode>(Y))
Chad Rosiera20e1e72012-08-01 18:39:17 +00009295 if (YC->isNullValue() &&
Manman Rened579842012-05-07 18:06:23 +00009296 (isAllOnes(Op1) == (CondCode == X86::COND_NE))) {
9297 SDVTList VTs = DAG.getVTList(CmpOp0.getValueType(), MVT::i32);
Chad Rosiera20e1e72012-08-01 18:39:17 +00009298 SDValue Neg = DAG.getNode(X86ISD::SUB, DL, VTs,
9299 DAG.getConstant(0, CmpOp0.getValueType()),
Manman Rened579842012-05-07 18:06:23 +00009300 CmpOp0);
9301 SDValue Res = DAG.getNode(X86ISD::SETCC_CARRY, DL, Op.getValueType(),
9302 DAG.getConstant(X86::COND_B, MVT::i8),
9303 SDValue(Neg.getNode(), 1));
9304 return Res;
9305 }
9306
Chris Lattnera2b56002010-12-05 01:23:24 +00009307 Cmp = DAG.getNode(X86ISD::CMP, DL, MVT::i32,
9308 CmpOp0, DAG.getConstant(1, CmpOp0.getValueType()));
Benjamin Kramer17c836c2012-04-27 12:07:43 +00009309 Cmp = ConvertCmpIfNecessary(Cmp, DAG);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009310
Chris Lattner96908b12010-12-05 02:00:51 +00009311 SDValue Res = // Res = 0 or -1.
Chris Lattnera2b56002010-12-05 01:23:24 +00009312 DAG.getNode(X86ISD::SETCC_CARRY, DL, Op.getValueType(),
9313 DAG.getConstant(X86::COND_B, MVT::i8), Cmp);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009314
Chris Lattner96908b12010-12-05 02:00:51 +00009315 if (isAllOnes(Op1) != (CondCode == X86::COND_E))
9316 Res = DAG.getNOT(DL, Res, Res.getValueType());
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009317
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00009318 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(Op2);
Chris Lattnera2b56002010-12-05 01:23:24 +00009319 if (N2C == 0 || !N2C->isNullValue())
9320 Res = DAG.getNode(ISD::OR, DL, Res.getValueType(), Res, Y);
9321 return Res;
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00009322 }
9323 }
9324
Chris Lattnera2b56002010-12-05 01:23:24 +00009325 // Look past (and (setcc_carry (cmp ...)), 1).
Evan Chengad9c0a32009-12-15 00:53:42 +00009326 if (Cond.getOpcode() == ISD::AND &&
9327 Cond.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY) {
9328 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Cond.getOperand(1));
Michael J. Spencerec38de22010-10-10 22:04:20 +00009329 if (C && C->getAPIntValue() == 1)
Evan Chengad9c0a32009-12-15 00:53:42 +00009330 Cond = Cond.getOperand(0);
9331 }
9332
Evan Cheng3f41d662007-10-08 22:16:29 +00009333 // If condition flag is set by a X86ISD::CMP, then use it as the condition
9334 // setting operand in place of the X86ISD::SETCC.
Dan Gohman65fd6562011-11-03 21:49:52 +00009335 unsigned CondOpcode = Cond.getOpcode();
9336 if (CondOpcode == X86ISD::SETCC ||
9337 CondOpcode == X86ISD::SETCC_CARRY) {
Evan Cheng734503b2006-09-11 02:19:56 +00009338 CC = Cond.getOperand(0);
9339
Dan Gohman475871a2008-07-27 21:46:04 +00009340 SDValue Cmp = Cond.getOperand(1);
Evan Cheng734503b2006-09-11 02:19:56 +00009341 unsigned Opc = Cmp.getOpcode();
Owen Andersone50ed302009-08-10 22:56:29 +00009342 EVT VT = Op.getValueType();
Scott Michelfdc40a02009-02-17 22:15:04 +00009343
Evan Cheng3f41d662007-10-08 22:16:29 +00009344 bool IllegalFPCMov = false;
Duncan Sands83ec4b62008-06-06 12:08:01 +00009345 if (VT.isFloatingPoint() && !VT.isVector() &&
Chris Lattner78631162008-01-16 06:24:21 +00009346 !isScalarFPTypeInSSEReg(VT)) // FPStack?
Dan Gohman7810bfe2008-09-26 21:54:37 +00009347 IllegalFPCMov = !hasFPCMov(cast<ConstantSDNode>(CC)->getSExtValue());
Scott Michelfdc40a02009-02-17 22:15:04 +00009348
Chris Lattnerd1980a52009-03-12 06:52:53 +00009349 if ((isX86LogicalCmp(Cmp) && !IllegalFPCMov) ||
9350 Opc == X86ISD::BT) { // FIXME
Evan Cheng3f41d662007-10-08 22:16:29 +00009351 Cond = Cmp;
Evan Cheng0488db92007-09-25 01:57:46 +00009352 addTest = false;
9353 }
Dan Gohman65fd6562011-11-03 21:49:52 +00009354 } else if (CondOpcode == ISD::USUBO || CondOpcode == ISD::SSUBO ||
9355 CondOpcode == ISD::UADDO || CondOpcode == ISD::SADDO ||
9356 ((CondOpcode == ISD::UMULO || CondOpcode == ISD::SMULO) &&
9357 Cond.getOperand(0).getValueType() != MVT::i8)) {
9358 SDValue LHS = Cond.getOperand(0);
9359 SDValue RHS = Cond.getOperand(1);
9360 unsigned X86Opcode;
9361 unsigned X86Cond;
9362 SDVTList VTs;
9363 switch (CondOpcode) {
9364 case ISD::UADDO: X86Opcode = X86ISD::ADD; X86Cond = X86::COND_B; break;
9365 case ISD::SADDO: X86Opcode = X86ISD::ADD; X86Cond = X86::COND_O; break;
9366 case ISD::USUBO: X86Opcode = X86ISD::SUB; X86Cond = X86::COND_B; break;
9367 case ISD::SSUBO: X86Opcode = X86ISD::SUB; X86Cond = X86::COND_O; break;
9368 case ISD::UMULO: X86Opcode = X86ISD::UMUL; X86Cond = X86::COND_O; break;
9369 case ISD::SMULO: X86Opcode = X86ISD::SMUL; X86Cond = X86::COND_O; break;
9370 default: llvm_unreachable("unexpected overflowing operator");
9371 }
9372 if (CondOpcode == ISD::UMULO)
9373 VTs = DAG.getVTList(LHS.getValueType(), LHS.getValueType(),
9374 MVT::i32);
9375 else
9376 VTs = DAG.getVTList(LHS.getValueType(), MVT::i32);
9377
9378 SDValue X86Op = DAG.getNode(X86Opcode, DL, VTs, LHS, RHS);
9379
9380 if (CondOpcode == ISD::UMULO)
9381 Cond = X86Op.getValue(2);
9382 else
9383 Cond = X86Op.getValue(1);
9384
9385 CC = DAG.getConstant(X86Cond, MVT::i8);
9386 addTest = false;
Evan Cheng0488db92007-09-25 01:57:46 +00009387 }
9388
9389 if (addTest) {
Evan Chengb64dd5f2012-08-07 22:21:00 +00009390 // Look pass the truncate if the high bits are known zero.
9391 if (isTruncWithZeroHighBitsInput(Cond, DAG))
9392 Cond = Cond.getOperand(0);
Evan Chengd40d03e2010-01-06 19:38:29 +00009393
9394 // We know the result of AND is compared against zero. Try to match
9395 // it to BT.
Michael J. Spencerec38de22010-10-10 22:04:20 +00009396 if (Cond.getOpcode() == ISD::AND && Cond.hasOneUse()) {
Chris Lattnera2b56002010-12-05 01:23:24 +00009397 SDValue NewSetCC = LowerToBT(Cond, ISD::SETNE, DL, DAG);
Evan Chengd40d03e2010-01-06 19:38:29 +00009398 if (NewSetCC.getNode()) {
9399 CC = NewSetCC.getOperand(0);
9400 Cond = NewSetCC.getOperand(1);
9401 addTest = false;
9402 }
9403 }
9404 }
9405
9406 if (addTest) {
Owen Anderson825b72b2009-08-11 20:47:22 +00009407 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng552f09a2010-04-26 19:06:11 +00009408 Cond = EmitTest(Cond, X86::COND_NE, DAG);
Evan Cheng0488db92007-09-25 01:57:46 +00009409 }
9410
Benjamin Kramere915ff32010-12-22 23:09:28 +00009411 // a < b ? -1 : 0 -> RES = ~setcc_carry
9412 // a < b ? 0 : -1 -> RES = setcc_carry
9413 // a >= b ? -1 : 0 -> RES = setcc_carry
9414 // a >= b ? 0 : -1 -> RES = ~setcc_carry
Manman Ren39ad5682012-08-08 00:51:41 +00009415 if (Cond.getOpcode() == X86ISD::SUB) {
Benjamin Kramer17c836c2012-04-27 12:07:43 +00009416 Cond = ConvertCmpIfNecessary(Cond, DAG);
Benjamin Kramere915ff32010-12-22 23:09:28 +00009417 unsigned CondCode = cast<ConstantSDNode>(CC)->getZExtValue();
9418
9419 if ((CondCode == X86::COND_AE || CondCode == X86::COND_B) &&
9420 (isAllOnes(Op1) || isAllOnes(Op2)) && (isZero(Op1) || isZero(Op2))) {
9421 SDValue Res = DAG.getNode(X86ISD::SETCC_CARRY, DL, Op.getValueType(),
9422 DAG.getConstant(X86::COND_B, MVT::i8), Cond);
9423 if (isAllOnes(Op1) != (CondCode == X86::COND_B))
9424 return DAG.getNOT(DL, Res, Res.getValueType());
9425 return Res;
9426 }
9427 }
9428
Benjamin Kramer444dcce2012-10-13 10:39:49 +00009429 // X86 doesn't have an i8 cmov. If both operands are the result of a truncate
9430 // widen the cmov and push the truncate through. This avoids introducing a new
9431 // branch during isel and doesn't add any extensions.
9432 if (Op.getValueType() == MVT::i8 &&
9433 Op1.getOpcode() == ISD::TRUNCATE && Op2.getOpcode() == ISD::TRUNCATE) {
9434 SDValue T1 = Op1.getOperand(0), T2 = Op2.getOperand(0);
9435 if (T1.getValueType() == T2.getValueType() &&
9436 // Blacklist CopyFromReg to avoid partial register stalls.
9437 T1.getOpcode() != ISD::CopyFromReg && T2.getOpcode()!=ISD::CopyFromReg){
9438 SDVTList VTs = DAG.getVTList(T1.getValueType(), MVT::Glue);
Benjamin Kramerf8b65aa2012-10-13 12:50:19 +00009439 SDValue Cmov = DAG.getNode(X86ISD::CMOV, DL, VTs, T2, T1, CC, Cond);
Benjamin Kramer444dcce2012-10-13 10:39:49 +00009440 return DAG.getNode(ISD::TRUNCATE, DL, Op.getValueType(), Cmov);
9441 }
9442 }
9443
Evan Cheng0488db92007-09-25 01:57:46 +00009444 // X86ISD::CMOV means set the result (which is operand 1) to the RHS if
9445 // condition is true.
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00009446 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::Glue);
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00009447 SDValue Ops[] = { Op2, Op1, CC, Cond };
Chris Lattnera2b56002010-12-05 01:23:24 +00009448 return DAG.getNode(X86ISD::CMOV, DL, VTs, Ops, array_lengthof(Ops));
Evan Cheng0488db92007-09-25 01:57:46 +00009449}
9450
Evan Cheng370e5342008-12-03 08:38:43 +00009451// isAndOrOfSingleUseSetCCs - Return true if node is an ISD::AND or
9452// ISD::OR of two X86ISD::SETCC nodes each of which has no other use apart
9453// from the AND / OR.
9454static bool isAndOrOfSetCCs(SDValue Op, unsigned &Opc) {
9455 Opc = Op.getOpcode();
9456 if (Opc != ISD::OR && Opc != ISD::AND)
9457 return false;
9458 return (Op.getOperand(0).getOpcode() == X86ISD::SETCC &&
9459 Op.getOperand(0).hasOneUse() &&
9460 Op.getOperand(1).getOpcode() == X86ISD::SETCC &&
9461 Op.getOperand(1).hasOneUse());
9462}
9463
Evan Cheng961d6d42009-02-02 08:19:07 +00009464// isXor1OfSetCC - Return true if node is an ISD::XOR of a X86ISD::SETCC and
9465// 1 and that the SETCC node has a single use.
Evan Cheng67ad9db2009-02-02 08:07:36 +00009466static bool isXor1OfSetCC(SDValue Op) {
9467 if (Op.getOpcode() != ISD::XOR)
9468 return false;
9469 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
9470 if (N1C && N1C->getAPIntValue() == 1) {
9471 return Op.getOperand(0).getOpcode() == X86ISD::SETCC &&
9472 Op.getOperand(0).hasOneUse();
9473 }
9474 return false;
9475}
9476
Dan Gohmand858e902010-04-17 15:26:15 +00009477SDValue X86TargetLowering::LowerBRCOND(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng734503b2006-09-11 02:19:56 +00009478 bool addTest = true;
Dan Gohman475871a2008-07-27 21:46:04 +00009479 SDValue Chain = Op.getOperand(0);
9480 SDValue Cond = Op.getOperand(1);
9481 SDValue Dest = Op.getOperand(2);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009482 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00009483 SDValue CC;
Dan Gohman65fd6562011-11-03 21:49:52 +00009484 bool Inverted = false;
Evan Cheng734503b2006-09-11 02:19:56 +00009485
Dan Gohman1a492952009-10-20 16:22:37 +00009486 if (Cond.getOpcode() == ISD::SETCC) {
Dan Gohman65fd6562011-11-03 21:49:52 +00009487 // Check for setcc([su]{add,sub,mul}o == 0).
9488 if (cast<CondCodeSDNode>(Cond.getOperand(2))->get() == ISD::SETEQ &&
9489 isa<ConstantSDNode>(Cond.getOperand(1)) &&
9490 cast<ConstantSDNode>(Cond.getOperand(1))->isNullValue() &&
9491 Cond.getOperand(0).getResNo() == 1 &&
9492 (Cond.getOperand(0).getOpcode() == ISD::SADDO ||
9493 Cond.getOperand(0).getOpcode() == ISD::UADDO ||
9494 Cond.getOperand(0).getOpcode() == ISD::SSUBO ||
9495 Cond.getOperand(0).getOpcode() == ISD::USUBO ||
9496 Cond.getOperand(0).getOpcode() == ISD::SMULO ||
9497 Cond.getOperand(0).getOpcode() == ISD::UMULO)) {
9498 Inverted = true;
9499 Cond = Cond.getOperand(0);
9500 } else {
9501 SDValue NewCond = LowerSETCC(Cond, DAG);
9502 if (NewCond.getNode())
9503 Cond = NewCond;
9504 }
Dan Gohman1a492952009-10-20 16:22:37 +00009505 }
Chris Lattnere55484e2008-12-25 05:34:37 +00009506#if 0
9507 // FIXME: LowerXALUO doesn't handle these!!
Bill Wendlingd350e022008-12-12 21:15:41 +00009508 else if (Cond.getOpcode() == X86ISD::ADD ||
9509 Cond.getOpcode() == X86ISD::SUB ||
9510 Cond.getOpcode() == X86ISD::SMUL ||
9511 Cond.getOpcode() == X86ISD::UMUL)
Bill Wendling74c37652008-12-09 22:08:41 +00009512 Cond = LowerXALUO(Cond, DAG);
Chris Lattnere55484e2008-12-25 05:34:37 +00009513#endif
Scott Michelfdc40a02009-02-17 22:15:04 +00009514
Evan Chengad9c0a32009-12-15 00:53:42 +00009515 // Look pass (and (setcc_carry (cmp ...)), 1).
9516 if (Cond.getOpcode() == ISD::AND &&
9517 Cond.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY) {
9518 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Cond.getOperand(1));
Michael J. Spencerec38de22010-10-10 22:04:20 +00009519 if (C && C->getAPIntValue() == 1)
Evan Chengad9c0a32009-12-15 00:53:42 +00009520 Cond = Cond.getOperand(0);
9521 }
9522
Evan Cheng3f41d662007-10-08 22:16:29 +00009523 // If condition flag is set by a X86ISD::CMP, then use it as the condition
9524 // setting operand in place of the X86ISD::SETCC.
Dan Gohman65fd6562011-11-03 21:49:52 +00009525 unsigned CondOpcode = Cond.getOpcode();
9526 if (CondOpcode == X86ISD::SETCC ||
9527 CondOpcode == X86ISD::SETCC_CARRY) {
Evan Cheng734503b2006-09-11 02:19:56 +00009528 CC = Cond.getOperand(0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00009529
Dan Gohman475871a2008-07-27 21:46:04 +00009530 SDValue Cmp = Cond.getOperand(1);
Evan Cheng734503b2006-09-11 02:19:56 +00009531 unsigned Opc = Cmp.getOpcode();
Chris Lattnere55484e2008-12-25 05:34:37 +00009532 // FIXME: WHY THE SPECIAL CASING OF LogicalCmp??
Dan Gohman076aee32009-03-04 19:44:21 +00009533 if (isX86LogicalCmp(Cmp) || Opc == X86ISD::BT) {
Evan Cheng3f41d662007-10-08 22:16:29 +00009534 Cond = Cmp;
Evan Cheng0488db92007-09-25 01:57:46 +00009535 addTest = false;
Bill Wendling61edeb52008-12-02 01:06:39 +00009536 } else {
Evan Cheng370e5342008-12-03 08:38:43 +00009537 switch (cast<ConstantSDNode>(CC)->getZExtValue()) {
Bill Wendling0ea25cb2008-12-03 08:32:02 +00009538 default: break;
9539 case X86::COND_O:
Dan Gohman653456c2009-01-07 00:15:08 +00009540 case X86::COND_B:
Chris Lattnere55484e2008-12-25 05:34:37 +00009541 // These can only come from an arithmetic instruction with overflow,
9542 // e.g. SADDO, UADDO.
Bill Wendling0ea25cb2008-12-03 08:32:02 +00009543 Cond = Cond.getNode()->getOperand(1);
9544 addTest = false;
9545 break;
Bill Wendling61edeb52008-12-02 01:06:39 +00009546 }
Evan Cheng0488db92007-09-25 01:57:46 +00009547 }
Dan Gohman65fd6562011-11-03 21:49:52 +00009548 }
9549 CondOpcode = Cond.getOpcode();
9550 if (CondOpcode == ISD::UADDO || CondOpcode == ISD::SADDO ||
9551 CondOpcode == ISD::USUBO || CondOpcode == ISD::SSUBO ||
9552 ((CondOpcode == ISD::UMULO || CondOpcode == ISD::SMULO) &&
9553 Cond.getOperand(0).getValueType() != MVT::i8)) {
9554 SDValue LHS = Cond.getOperand(0);
9555 SDValue RHS = Cond.getOperand(1);
9556 unsigned X86Opcode;
9557 unsigned X86Cond;
9558 SDVTList VTs;
9559 switch (CondOpcode) {
9560 case ISD::UADDO: X86Opcode = X86ISD::ADD; X86Cond = X86::COND_B; break;
9561 case ISD::SADDO: X86Opcode = X86ISD::ADD; X86Cond = X86::COND_O; break;
9562 case ISD::USUBO: X86Opcode = X86ISD::SUB; X86Cond = X86::COND_B; break;
9563 case ISD::SSUBO: X86Opcode = X86ISD::SUB; X86Cond = X86::COND_O; break;
9564 case ISD::UMULO: X86Opcode = X86ISD::UMUL; X86Cond = X86::COND_O; break;
9565 case ISD::SMULO: X86Opcode = X86ISD::SMUL; X86Cond = X86::COND_O; break;
9566 default: llvm_unreachable("unexpected overflowing operator");
9567 }
9568 if (Inverted)
9569 X86Cond = X86::GetOppositeBranchCondition((X86::CondCode)X86Cond);
9570 if (CondOpcode == ISD::UMULO)
9571 VTs = DAG.getVTList(LHS.getValueType(), LHS.getValueType(),
9572 MVT::i32);
9573 else
9574 VTs = DAG.getVTList(LHS.getValueType(), MVT::i32);
9575
9576 SDValue X86Op = DAG.getNode(X86Opcode, dl, VTs, LHS, RHS);
9577
9578 if (CondOpcode == ISD::UMULO)
9579 Cond = X86Op.getValue(2);
9580 else
9581 Cond = X86Op.getValue(1);
9582
9583 CC = DAG.getConstant(X86Cond, MVT::i8);
9584 addTest = false;
Evan Cheng370e5342008-12-03 08:38:43 +00009585 } else {
9586 unsigned CondOpc;
9587 if (Cond.hasOneUse() && isAndOrOfSetCCs(Cond, CondOpc)) {
9588 SDValue Cmp = Cond.getOperand(0).getOperand(1);
Evan Cheng370e5342008-12-03 08:38:43 +00009589 if (CondOpc == ISD::OR) {
9590 // Also, recognize the pattern generated by an FCMP_UNE. We can emit
9591 // two branches instead of an explicit OR instruction with a
9592 // separate test.
9593 if (Cmp == Cond.getOperand(1).getOperand(1) &&
Dan Gohman076aee32009-03-04 19:44:21 +00009594 isX86LogicalCmp(Cmp)) {
Evan Cheng370e5342008-12-03 08:38:43 +00009595 CC = Cond.getOperand(0).getOperand(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009596 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Evan Cheng370e5342008-12-03 08:38:43 +00009597 Chain, Dest, CC, Cmp);
9598 CC = Cond.getOperand(1).getOperand(0);
9599 Cond = Cmp;
9600 addTest = false;
9601 }
9602 } else { // ISD::AND
9603 // Also, recognize the pattern generated by an FCMP_OEQ. We can emit
9604 // two branches instead of an explicit AND instruction with a
9605 // separate test. However, we only do this if this block doesn't
9606 // have a fall-through edge, because this requires an explicit
9607 // jmp when the condition is false.
9608 if (Cmp == Cond.getOperand(1).getOperand(1) &&
Dan Gohman076aee32009-03-04 19:44:21 +00009609 isX86LogicalCmp(Cmp) &&
Evan Cheng370e5342008-12-03 08:38:43 +00009610 Op.getNode()->hasOneUse()) {
9611 X86::CondCode CCode =
9612 (X86::CondCode)Cond.getOperand(0).getConstantOperandVal(0);
9613 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00009614 CC = DAG.getConstant(CCode, MVT::i8);
Dan Gohman027657d2010-06-18 15:30:29 +00009615 SDNode *User = *Op.getNode()->use_begin();
Evan Cheng370e5342008-12-03 08:38:43 +00009616 // Look for an unconditional branch following this conditional branch.
9617 // We need this because we need to reverse the successors in order
9618 // to implement FCMP_OEQ.
Dan Gohman027657d2010-06-18 15:30:29 +00009619 if (User->getOpcode() == ISD::BR) {
9620 SDValue FalseBB = User->getOperand(1);
9621 SDNode *NewBR =
9622 DAG.UpdateNodeOperands(User, User->getOperand(0), Dest);
Evan Cheng370e5342008-12-03 08:38:43 +00009623 assert(NewBR == User);
Nick Lewycky2a3ee5e2010-06-20 20:27:42 +00009624 (void)NewBR;
Evan Cheng370e5342008-12-03 08:38:43 +00009625 Dest = FalseBB;
Dan Gohman279c22e2008-10-21 03:29:32 +00009626
Dale Johannesene4d209d2009-02-03 20:21:25 +00009627 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Evan Cheng370e5342008-12-03 08:38:43 +00009628 Chain, Dest, CC, Cmp);
9629 X86::CondCode CCode =
9630 (X86::CondCode)Cond.getOperand(1).getConstantOperandVal(0);
9631 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00009632 CC = DAG.getConstant(CCode, MVT::i8);
Evan Cheng370e5342008-12-03 08:38:43 +00009633 Cond = Cmp;
9634 addTest = false;
9635 }
9636 }
Dan Gohman279c22e2008-10-21 03:29:32 +00009637 }
Evan Cheng67ad9db2009-02-02 08:07:36 +00009638 } else if (Cond.hasOneUse() && isXor1OfSetCC(Cond)) {
9639 // Recognize for xorb (setcc), 1 patterns. The xor inverts the condition.
9640 // It should be transformed during dag combiner except when the condition
9641 // is set by a arithmetics with overflow node.
9642 X86::CondCode CCode =
9643 (X86::CondCode)Cond.getOperand(0).getConstantOperandVal(0);
9644 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00009645 CC = DAG.getConstant(CCode, MVT::i8);
Evan Cheng67ad9db2009-02-02 08:07:36 +00009646 Cond = Cond.getOperand(0).getOperand(1);
9647 addTest = false;
Dan Gohman65fd6562011-11-03 21:49:52 +00009648 } else if (Cond.getOpcode() == ISD::SETCC &&
9649 cast<CondCodeSDNode>(Cond.getOperand(2))->get() == ISD::SETOEQ) {
9650 // For FCMP_OEQ, we can emit
9651 // two branches instead of an explicit AND instruction with a
9652 // separate test. However, we only do this if this block doesn't
9653 // have a fall-through edge, because this requires an explicit
9654 // jmp when the condition is false.
9655 if (Op.getNode()->hasOneUse()) {
9656 SDNode *User = *Op.getNode()->use_begin();
9657 // Look for an unconditional branch following this conditional branch.
9658 // We need this because we need to reverse the successors in order
9659 // to implement FCMP_OEQ.
9660 if (User->getOpcode() == ISD::BR) {
9661 SDValue FalseBB = User->getOperand(1);
9662 SDNode *NewBR =
9663 DAG.UpdateNodeOperands(User, User->getOperand(0), Dest);
9664 assert(NewBR == User);
9665 (void)NewBR;
9666 Dest = FalseBB;
9667
9668 SDValue Cmp = DAG.getNode(X86ISD::CMP, dl, MVT::i32,
9669 Cond.getOperand(0), Cond.getOperand(1));
Benjamin Kramer17c836c2012-04-27 12:07:43 +00009670 Cmp = ConvertCmpIfNecessary(Cmp, DAG);
Dan Gohman65fd6562011-11-03 21:49:52 +00009671 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
9672 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
9673 Chain, Dest, CC, Cmp);
9674 CC = DAG.getConstant(X86::COND_P, MVT::i8);
9675 Cond = Cmp;
9676 addTest = false;
9677 }
9678 }
9679 } else if (Cond.getOpcode() == ISD::SETCC &&
9680 cast<CondCodeSDNode>(Cond.getOperand(2))->get() == ISD::SETUNE) {
9681 // For FCMP_UNE, we can emit
9682 // two branches instead of an explicit AND instruction with a
9683 // separate test. However, we only do this if this block doesn't
9684 // have a fall-through edge, because this requires an explicit
9685 // jmp when the condition is false.
9686 if (Op.getNode()->hasOneUse()) {
9687 SDNode *User = *Op.getNode()->use_begin();
9688 // Look for an unconditional branch following this conditional branch.
9689 // We need this because we need to reverse the successors in order
9690 // to implement FCMP_UNE.
9691 if (User->getOpcode() == ISD::BR) {
9692 SDValue FalseBB = User->getOperand(1);
9693 SDNode *NewBR =
9694 DAG.UpdateNodeOperands(User, User->getOperand(0), Dest);
9695 assert(NewBR == User);
9696 (void)NewBR;
9697
9698 SDValue Cmp = DAG.getNode(X86ISD::CMP, dl, MVT::i32,
9699 Cond.getOperand(0), Cond.getOperand(1));
Benjamin Kramer17c836c2012-04-27 12:07:43 +00009700 Cmp = ConvertCmpIfNecessary(Cmp, DAG);
Dan Gohman65fd6562011-11-03 21:49:52 +00009701 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
9702 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
9703 Chain, Dest, CC, Cmp);
9704 CC = DAG.getConstant(X86::COND_NP, MVT::i8);
9705 Cond = Cmp;
9706 addTest = false;
9707 Dest = FalseBB;
9708 }
9709 }
Dan Gohman279c22e2008-10-21 03:29:32 +00009710 }
Evan Cheng0488db92007-09-25 01:57:46 +00009711 }
9712
9713 if (addTest) {
Evan Chengb64dd5f2012-08-07 22:21:00 +00009714 // Look pass the truncate if the high bits are known zero.
9715 if (isTruncWithZeroHighBitsInput(Cond, DAG))
9716 Cond = Cond.getOperand(0);
Evan Chengd40d03e2010-01-06 19:38:29 +00009717
9718 // We know the result of AND is compared against zero. Try to match
9719 // it to BT.
Michael J. Spencerec38de22010-10-10 22:04:20 +00009720 if (Cond.getOpcode() == ISD::AND && Cond.hasOneUse()) {
Evan Chengd40d03e2010-01-06 19:38:29 +00009721 SDValue NewSetCC = LowerToBT(Cond, ISD::SETNE, dl, DAG);
9722 if (NewSetCC.getNode()) {
9723 CC = NewSetCC.getOperand(0);
9724 Cond = NewSetCC.getOperand(1);
9725 addTest = false;
9726 }
9727 }
9728 }
9729
9730 if (addTest) {
Owen Anderson825b72b2009-08-11 20:47:22 +00009731 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng552f09a2010-04-26 19:06:11 +00009732 Cond = EmitTest(Cond, X86::COND_NE, DAG);
Evan Cheng0488db92007-09-25 01:57:46 +00009733 }
Benjamin Kramer17c836c2012-04-27 12:07:43 +00009734 Cond = ConvertCmpIfNecessary(Cond, DAG);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009735 return DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Dan Gohman279c22e2008-10-21 03:29:32 +00009736 Chain, Dest, CC, Cond);
Evan Cheng0488db92007-09-25 01:57:46 +00009737}
9738
Anton Korobeynikove060b532007-04-17 19:34:00 +00009739// Lower dynamic stack allocation to _alloca call for Cygwin/Mingw targets.
9740// Calls to _alloca is needed to probe the stack when allocating more than 4k
9741// bytes in one go. Touching the stack at 4K increments is necessary to ensure
9742// that the guard pages used by the OS virtual memory manager are allocated in
9743// correct sequence.
Dan Gohman475871a2008-07-27 21:46:04 +00009744SDValue
9745X86TargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00009746 SelectionDAG &DAG) const {
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009747 assert((Subtarget->isTargetCygMing() || Subtarget->isTargetWindows() ||
Nick Lewycky8a8d4792011-12-02 22:16:29 +00009748 getTargetMachine().Options.EnableSegmentedStacks) &&
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009749 "This should be used only on Windows targets or when segmented stacks "
Rafael Espindola96428ce2011-09-06 18:43:08 +00009750 "are being used");
9751 assert(!Subtarget->isTargetEnvMacho() && "Not implemented");
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009752 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov096b4612008-06-11 20:16:42 +00009753
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00009754 // Get the inputs.
Dan Gohman475871a2008-07-27 21:46:04 +00009755 SDValue Chain = Op.getOperand(0);
9756 SDValue Size = Op.getOperand(1);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00009757 // FIXME: Ensure alignment here
9758
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009759 bool Is64Bit = Subtarget->is64Bit();
9760 EVT SPTy = Is64Bit ? MVT::i64 : MVT::i32;
Anton Korobeynikov096b4612008-06-11 20:16:42 +00009761
Nick Lewycky8a8d4792011-12-02 22:16:29 +00009762 if (getTargetMachine().Options.EnableSegmentedStacks) {
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009763 MachineFunction &MF = DAG.getMachineFunction();
9764 MachineRegisterInfo &MRI = MF.getRegInfo();
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00009765
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009766 if (Is64Bit) {
9767 // The 64 bit implementation of segmented stacks needs to clobber both r10
Rafael Espindola96428ce2011-09-06 18:43:08 +00009768 // r11. This makes it impossible to use it along with nested parameters.
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009769 const Function *F = MF.getFunction();
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00009770
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009771 for (Function::const_arg_iterator I = F->arg_begin(), E = F->arg_end();
Craig Topper31a207a2012-05-04 06:39:13 +00009772 I != E; ++I)
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009773 if (I->hasNestAttr())
9774 report_fatal_error("Cannot use segmented stacks with functions that "
9775 "have nested arguments.");
9776 }
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00009777
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009778 const TargetRegisterClass *AddrRegClass =
9779 getRegClassFor(Subtarget->is64Bit() ? MVT::i64:MVT::i32);
9780 unsigned Vreg = MRI.createVirtualRegister(AddrRegClass);
9781 Chain = DAG.getCopyToReg(Chain, dl, Vreg, Size);
9782 SDValue Value = DAG.getNode(X86ISD::SEG_ALLOCA, dl, SPTy, Chain,
9783 DAG.getRegister(Vreg, SPTy));
9784 SDValue Ops1[2] = { Value, Chain };
9785 return DAG.getMergeValues(Ops1, 2, dl);
9786 } else {
9787 SDValue Flag;
9788 unsigned Reg = (Subtarget->is64Bit() ? X86::RAX : X86::EAX);
Anton Korobeynikov096b4612008-06-11 20:16:42 +00009789
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009790 Chain = DAG.getCopyToReg(Chain, dl, Reg, Size, Flag);
9791 Flag = Chain.getValue(1);
9792 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Anton Korobeynikov096b4612008-06-11 20:16:42 +00009793
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009794 Chain = DAG.getNode(X86ISD::WIN_ALLOCA, dl, NodeTys, Chain, Flag);
9795 Flag = Chain.getValue(1);
9796
Michael Liaoc5c970e2012-10-31 04:14:09 +00009797 Chain = DAG.getCopyFromReg(Chain, dl, RegInfo->getStackRegister(),
9798 SPTy).getValue(1);
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009799
9800 SDValue Ops1[2] = { Chain.getValue(0), Chain };
9801 return DAG.getMergeValues(Ops1, 2, dl);
9802 }
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00009803}
9804
Dan Gohmand858e902010-04-17 15:26:15 +00009805SDValue X86TargetLowering::LowerVASTART(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman1e93df62010-04-17 14:41:14 +00009806 MachineFunction &MF = DAG.getMachineFunction();
9807 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
9808
Dan Gohman69de1932008-02-06 22:27:42 +00009809 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Chris Lattner8026a9d2010-09-21 17:50:43 +00009810 DebugLoc DL = Op.getDebugLoc();
Evan Cheng8b2794a2006-10-13 21:14:26 +00009811
Anton Korobeynikove7beda12010-10-03 22:52:07 +00009812 if (!Subtarget->is64Bit() || Subtarget->isTargetWin64()) {
Evan Cheng25ab6902006-09-08 06:48:29 +00009813 // vastart just stores the address of the VarArgsFrameIndex slot into the
9814 // memory location argument.
Dan Gohman1e93df62010-04-17 14:41:14 +00009815 SDValue FR = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
9816 getPointerTy());
Chris Lattner8026a9d2010-09-21 17:50:43 +00009817 return DAG.getStore(Op.getOperand(0), DL, FR, Op.getOperand(1),
9818 MachinePointerInfo(SV), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00009819 }
9820
9821 // __va_list_tag:
9822 // gp_offset (0 - 6 * 8)
9823 // fp_offset (48 - 48 + 8 * 16)
9824 // overflow_arg_area (point to parameters coming in memory).
9825 // reg_save_area
Dan Gohman475871a2008-07-27 21:46:04 +00009826 SmallVector<SDValue, 8> MemOps;
9827 SDValue FIN = Op.getOperand(1);
Evan Cheng25ab6902006-09-08 06:48:29 +00009828 // Store gp_offset
Chris Lattner8026a9d2010-09-21 17:50:43 +00009829 SDValue Store = DAG.getStore(Op.getOperand(0), DL,
Dan Gohman1e93df62010-04-17 14:41:14 +00009830 DAG.getConstant(FuncInfo->getVarArgsGPOffset(),
9831 MVT::i32),
Chris Lattner8026a9d2010-09-21 17:50:43 +00009832 FIN, MachinePointerInfo(SV), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00009833 MemOps.push_back(Store);
9834
9835 // Store fp_offset
Chris Lattner8026a9d2010-09-21 17:50:43 +00009836 FIN = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00009837 FIN, DAG.getIntPtrConstant(4));
Chris Lattner8026a9d2010-09-21 17:50:43 +00009838 Store = DAG.getStore(Op.getOperand(0), DL,
Dan Gohman1e93df62010-04-17 14:41:14 +00009839 DAG.getConstant(FuncInfo->getVarArgsFPOffset(),
9840 MVT::i32),
Chris Lattner8026a9d2010-09-21 17:50:43 +00009841 FIN, MachinePointerInfo(SV, 4), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00009842 MemOps.push_back(Store);
9843
9844 // Store ptr to overflow_arg_area
Chris Lattner8026a9d2010-09-21 17:50:43 +00009845 FIN = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00009846 FIN, DAG.getIntPtrConstant(4));
Dan Gohman1e93df62010-04-17 14:41:14 +00009847 SDValue OVFIN = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
9848 getPointerTy());
Chris Lattner8026a9d2010-09-21 17:50:43 +00009849 Store = DAG.getStore(Op.getOperand(0), DL, OVFIN, FIN,
9850 MachinePointerInfo(SV, 8),
David Greene67c9d422010-02-15 16:53:33 +00009851 false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00009852 MemOps.push_back(Store);
9853
9854 // Store ptr to reg_save_area.
Chris Lattner8026a9d2010-09-21 17:50:43 +00009855 FIN = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00009856 FIN, DAG.getIntPtrConstant(8));
Dan Gohman1e93df62010-04-17 14:41:14 +00009857 SDValue RSFIN = DAG.getFrameIndex(FuncInfo->getRegSaveFrameIndex(),
9858 getPointerTy());
Chris Lattner8026a9d2010-09-21 17:50:43 +00009859 Store = DAG.getStore(Op.getOperand(0), DL, RSFIN, FIN,
9860 MachinePointerInfo(SV, 16), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00009861 MemOps.push_back(Store);
Chris Lattner8026a9d2010-09-21 17:50:43 +00009862 return DAG.getNode(ISD::TokenFactor, DL, MVT::Other,
Dale Johannesene4d209d2009-02-03 20:21:25 +00009863 &MemOps[0], MemOps.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00009864}
9865
Dan Gohmand858e902010-04-17 15:26:15 +00009866SDValue X86TargetLowering::LowerVAARG(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman320afb82010-10-12 18:00:49 +00009867 assert(Subtarget->is64Bit() &&
9868 "LowerVAARG only handles 64-bit va_arg!");
9869 assert((Subtarget->isTargetLinux() ||
9870 Subtarget->isTargetDarwin()) &&
9871 "Unhandled target in LowerVAARG");
9872 assert(Op.getNode()->getNumOperands() == 4);
9873 SDValue Chain = Op.getOperand(0);
9874 SDValue SrcPtr = Op.getOperand(1);
9875 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
9876 unsigned Align = Op.getConstantOperandVal(3);
9877 DebugLoc dl = Op.getDebugLoc();
Dan Gohman9018e832008-05-10 01:26:14 +00009878
Dan Gohman320afb82010-10-12 18:00:49 +00009879 EVT ArgVT = Op.getNode()->getValueType(0);
Chris Lattnerdb125cf2011-07-18 04:54:35 +00009880 Type *ArgTy = ArgVT.getTypeForEVT(*DAG.getContext());
Micah Villmow3574eca2012-10-08 16:38:25 +00009881 uint32_t ArgSize = getDataLayout()->getTypeAllocSize(ArgTy);
Dan Gohman320afb82010-10-12 18:00:49 +00009882 uint8_t ArgMode;
9883
9884 // Decide which area this value should be read from.
9885 // TODO: Implement the AMD64 ABI in its entirety. This simple
9886 // selection mechanism works only for the basic types.
9887 if (ArgVT == MVT::f80) {
9888 llvm_unreachable("va_arg for f80 not yet implemented");
9889 } else if (ArgVT.isFloatingPoint() && ArgSize <= 16 /*bytes*/) {
9890 ArgMode = 2; // Argument passed in XMM register. Use fp_offset.
9891 } else if (ArgVT.isInteger() && ArgSize <= 32 /*bytes*/) {
9892 ArgMode = 1; // Argument passed in GPR64 register(s). Use gp_offset.
9893 } else {
9894 llvm_unreachable("Unhandled argument type in LowerVAARG");
9895 }
9896
9897 if (ArgMode == 2) {
9898 // Sanity Check: Make sure using fp_offset makes sense.
Nick Lewycky8a8d4792011-12-02 22:16:29 +00009899 assert(!getTargetMachine().Options.UseSoftFloat &&
Eric Christopher52b45052010-10-12 19:44:17 +00009900 !(DAG.getMachineFunction()
Bill Wendling67658342012-10-09 07:45:08 +00009901 .getFunction()->getFnAttributes()
Bill Wendling034b94b2012-12-19 07:18:57 +00009902 .hasAttribute(Attribute::NoImplicitFloat)) &&
Craig Topper1accb7e2012-01-10 06:54:16 +00009903 Subtarget->hasSSE1());
Dan Gohman320afb82010-10-12 18:00:49 +00009904 }
9905
9906 // Insert VAARG_64 node into the DAG
9907 // VAARG_64 returns two values: Variable Argument Address, Chain
9908 SmallVector<SDValue, 11> InstOps;
9909 InstOps.push_back(Chain);
9910 InstOps.push_back(SrcPtr);
9911 InstOps.push_back(DAG.getConstant(ArgSize, MVT::i32));
9912 InstOps.push_back(DAG.getConstant(ArgMode, MVT::i8));
9913 InstOps.push_back(DAG.getConstant(Align, MVT::i32));
9914 SDVTList VTs = DAG.getVTList(getPointerTy(), MVT::Other);
9915 SDValue VAARG = DAG.getMemIntrinsicNode(X86ISD::VAARG_64, dl,
9916 VTs, &InstOps[0], InstOps.size(),
9917 MVT::i64,
9918 MachinePointerInfo(SV),
9919 /*Align=*/0,
9920 /*Volatile=*/false,
9921 /*ReadMem=*/true,
9922 /*WriteMem=*/true);
9923 Chain = VAARG.getValue(1);
9924
9925 // Load the next argument and return it
9926 return DAG.getLoad(ArgVT, dl,
9927 Chain,
9928 VAARG,
9929 MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00009930 false, false, false, 0);
Dan Gohman9018e832008-05-10 01:26:14 +00009931}
9932
Craig Topper55b24052012-09-11 06:15:32 +00009933static SDValue LowerVACOPY(SDValue Op, const X86Subtarget *Subtarget,
9934 SelectionDAG &DAG) {
Evan Chengae642192007-03-02 23:16:35 +00009935 // X86-64 va_list is a struct { i32, i32, i8*, i8* }.
Dan Gohman28269132008-04-18 20:55:41 +00009936 assert(Subtarget->is64Bit() && "This code only handles 64-bit va_copy!");
Dan Gohman475871a2008-07-27 21:46:04 +00009937 SDValue Chain = Op.getOperand(0);
9938 SDValue DstPtr = Op.getOperand(1);
9939 SDValue SrcPtr = Op.getOperand(2);
Dan Gohman69de1932008-02-06 22:27:42 +00009940 const Value *DstSV = cast<SrcValueSDNode>(Op.getOperand(3))->getValue();
9941 const Value *SrcSV = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Chris Lattnere72f2022010-09-21 05:40:29 +00009942 DebugLoc DL = Op.getDebugLoc();
Evan Chengae642192007-03-02 23:16:35 +00009943
Chris Lattnere72f2022010-09-21 05:40:29 +00009944 return DAG.getMemcpy(Chain, DL, DstPtr, SrcPtr,
Mon P Wang20adc9d2010-04-04 03:10:48 +00009945 DAG.getIntPtrConstant(24), 8, /*isVolatile*/false,
Michael J. Spencerec38de22010-10-10 22:04:20 +00009946 false,
Chris Lattnere72f2022010-09-21 05:40:29 +00009947 MachinePointerInfo(DstSV), MachinePointerInfo(SrcSV));
Evan Chengae642192007-03-02 23:16:35 +00009948}
9949
Craig Topper80e46362012-01-23 06:16:53 +00009950// getTargetVShiftNOde - Handle vector element shifts where the shift amount
9951// may or may not be a constant. Takes immediate version of shift as input.
9952static SDValue getTargetVShiftNode(unsigned Opc, DebugLoc dl, EVT VT,
9953 SDValue SrcOp, SDValue ShAmt,
9954 SelectionDAG &DAG) {
9955 assert(ShAmt.getValueType() == MVT::i32 && "ShAmt is not i32");
9956
9957 if (isa<ConstantSDNode>(ShAmt)) {
Nadav Rotemd896e242012-07-15 20:27:43 +00009958 // Constant may be a TargetConstant. Use a regular constant.
9959 uint32_t ShiftAmt = cast<ConstantSDNode>(ShAmt)->getZExtValue();
Craig Topper80e46362012-01-23 06:16:53 +00009960 switch (Opc) {
9961 default: llvm_unreachable("Unknown target vector shift node");
9962 case X86ISD::VSHLI:
9963 case X86ISD::VSRLI:
9964 case X86ISD::VSRAI:
Nadav Rotemd896e242012-07-15 20:27:43 +00009965 return DAG.getNode(Opc, dl, VT, SrcOp,
9966 DAG.getConstant(ShiftAmt, MVT::i32));
Craig Topper80e46362012-01-23 06:16:53 +00009967 }
9968 }
9969
9970 // Change opcode to non-immediate version
9971 switch (Opc) {
9972 default: llvm_unreachable("Unknown target vector shift node");
9973 case X86ISD::VSHLI: Opc = X86ISD::VSHL; break;
9974 case X86ISD::VSRLI: Opc = X86ISD::VSRL; break;
9975 case X86ISD::VSRAI: Opc = X86ISD::VSRA; break;
9976 }
9977
9978 // Need to build a vector containing shift amount
9979 // Shift amount is 32-bits, but SSE instructions read 64-bit, so fill with 0
9980 SDValue ShOps[4];
9981 ShOps[0] = ShAmt;
9982 ShOps[1] = DAG.getConstant(0, MVT::i32);
Craig Topper6d688152012-08-14 07:43:25 +00009983 ShOps[2] = ShOps[3] = DAG.getUNDEF(MVT::i32);
Craig Topper80e46362012-01-23 06:16:53 +00009984 ShAmt = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, &ShOps[0], 4);
Nadav Rotem65f489f2012-07-14 22:26:05 +00009985
9986 // The return type has to be a 128-bit type with the same element
9987 // type as the input type.
9988 MVT EltVT = VT.getVectorElementType().getSimpleVT();
9989 EVT ShVT = MVT::getVectorVT(EltVT, 128/EltVT.getSizeInBits());
9990
9991 ShAmt = DAG.getNode(ISD::BITCAST, dl, ShVT, ShAmt);
Craig Topper80e46362012-01-23 06:16:53 +00009992 return DAG.getNode(Opc, dl, VT, SrcOp, ShAmt);
9993}
9994
Craig Topper55b24052012-09-11 06:15:32 +00009995static SDValue LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG) {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009996 DebugLoc dl = Op.getDebugLoc();
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00009997 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00009998 switch (IntNo) {
Dan Gohman475871a2008-07-27 21:46:04 +00009999 default: return SDValue(); // Don't custom lower most intrinsics.
Evan Cheng5759f972008-05-04 09:15:50 +000010000 // Comparison intrinsics.
Evan Cheng0db9fe62006-04-25 20:13:52 +000010001 case Intrinsic::x86_sse_comieq_ss:
10002 case Intrinsic::x86_sse_comilt_ss:
10003 case Intrinsic::x86_sse_comile_ss:
10004 case Intrinsic::x86_sse_comigt_ss:
10005 case Intrinsic::x86_sse_comige_ss:
10006 case Intrinsic::x86_sse_comineq_ss:
10007 case Intrinsic::x86_sse_ucomieq_ss:
10008 case Intrinsic::x86_sse_ucomilt_ss:
10009 case Intrinsic::x86_sse_ucomile_ss:
10010 case Intrinsic::x86_sse_ucomigt_ss:
10011 case Intrinsic::x86_sse_ucomige_ss:
10012 case Intrinsic::x86_sse_ucomineq_ss:
10013 case Intrinsic::x86_sse2_comieq_sd:
10014 case Intrinsic::x86_sse2_comilt_sd:
10015 case Intrinsic::x86_sse2_comile_sd:
10016 case Intrinsic::x86_sse2_comigt_sd:
10017 case Intrinsic::x86_sse2_comige_sd:
10018 case Intrinsic::x86_sse2_comineq_sd:
10019 case Intrinsic::x86_sse2_ucomieq_sd:
10020 case Intrinsic::x86_sse2_ucomilt_sd:
10021 case Intrinsic::x86_sse2_ucomile_sd:
10022 case Intrinsic::x86_sse2_ucomigt_sd:
10023 case Intrinsic::x86_sse2_ucomige_sd:
10024 case Intrinsic::x86_sse2_ucomineq_sd: {
Craig Topper6d688152012-08-14 07:43:25 +000010025 unsigned Opc;
10026 ISD::CondCode CC;
Evan Cheng0db9fe62006-04-25 20:13:52 +000010027 switch (IntNo) {
Craig Topper86c7c582012-01-30 01:10:15 +000010028 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000010029 case Intrinsic::x86_sse_comieq_ss:
10030 case Intrinsic::x86_sse2_comieq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +000010031 Opc = X86ISD::COMI;
10032 CC = ISD::SETEQ;
10033 break;
Evan Cheng6be2c582006-04-05 23:38:46 +000010034 case Intrinsic::x86_sse_comilt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +000010035 case Intrinsic::x86_sse2_comilt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +000010036 Opc = X86ISD::COMI;
10037 CC = ISD::SETLT;
10038 break;
10039 case Intrinsic::x86_sse_comile_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +000010040 case Intrinsic::x86_sse2_comile_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +000010041 Opc = X86ISD::COMI;
10042 CC = ISD::SETLE;
10043 break;
10044 case Intrinsic::x86_sse_comigt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +000010045 case Intrinsic::x86_sse2_comigt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +000010046 Opc = X86ISD::COMI;
10047 CC = ISD::SETGT;
10048 break;
10049 case Intrinsic::x86_sse_comige_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +000010050 case Intrinsic::x86_sse2_comige_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +000010051 Opc = X86ISD::COMI;
10052 CC = ISD::SETGE;
10053 break;
10054 case Intrinsic::x86_sse_comineq_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +000010055 case Intrinsic::x86_sse2_comineq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +000010056 Opc = X86ISD::COMI;
10057 CC = ISD::SETNE;
10058 break;
10059 case Intrinsic::x86_sse_ucomieq_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +000010060 case Intrinsic::x86_sse2_ucomieq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +000010061 Opc = X86ISD::UCOMI;
10062 CC = ISD::SETEQ;
10063 break;
10064 case Intrinsic::x86_sse_ucomilt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +000010065 case Intrinsic::x86_sse2_ucomilt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +000010066 Opc = X86ISD::UCOMI;
10067 CC = ISD::SETLT;
10068 break;
10069 case Intrinsic::x86_sse_ucomile_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +000010070 case Intrinsic::x86_sse2_ucomile_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +000010071 Opc = X86ISD::UCOMI;
10072 CC = ISD::SETLE;
10073 break;
10074 case Intrinsic::x86_sse_ucomigt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +000010075 case Intrinsic::x86_sse2_ucomigt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +000010076 Opc = X86ISD::UCOMI;
10077 CC = ISD::SETGT;
10078 break;
10079 case Intrinsic::x86_sse_ucomige_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +000010080 case Intrinsic::x86_sse2_ucomige_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +000010081 Opc = X86ISD::UCOMI;
10082 CC = ISD::SETGE;
10083 break;
10084 case Intrinsic::x86_sse_ucomineq_ss:
10085 case Intrinsic::x86_sse2_ucomineq_sd:
10086 Opc = X86ISD::UCOMI;
10087 CC = ISD::SETNE;
10088 break;
Evan Cheng6be2c582006-04-05 23:38:46 +000010089 }
Evan Cheng734503b2006-09-11 02:19:56 +000010090
Dan Gohman475871a2008-07-27 21:46:04 +000010091 SDValue LHS = Op.getOperand(1);
10092 SDValue RHS = Op.getOperand(2);
Chris Lattner1c39d4c2008-12-24 23:53:05 +000010093 unsigned X86CC = TranslateX86CC(CC, true, LHS, RHS, DAG);
Dan Gohman1a492952009-10-20 16:22:37 +000010094 assert(X86CC != X86::COND_INVALID && "Unexpected illegal condition!");
Owen Anderson825b72b2009-08-11 20:47:22 +000010095 SDValue Cond = DAG.getNode(Opc, dl, MVT::i32, LHS, RHS);
10096 SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
10097 DAG.getConstant(X86CC, MVT::i8), Cond);
10098 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC);
Evan Cheng6be2c582006-04-05 23:38:46 +000010099 }
Craig Topper6d688152012-08-14 07:43:25 +000010100
Duncan Sands04aa4ae2011-09-23 16:10:22 +000010101 // Arithmetic intrinsics.
Craig Topper5b209e82012-02-05 03:14:49 +000010102 case Intrinsic::x86_sse2_pmulu_dq:
10103 case Intrinsic::x86_avx2_pmulu_dq:
10104 return DAG.getNode(X86ISD::PMULUDQ, dl, Op.getValueType(),
10105 Op.getOperand(1), Op.getOperand(2));
Craig Topper6d688152012-08-14 07:43:25 +000010106
Benjamin Kramer388fc6a2012-12-15 16:47:44 +000010107 // SSE2/AVX2 sub with unsigned saturation intrinsics
10108 case Intrinsic::x86_sse2_psubus_b:
10109 case Intrinsic::x86_sse2_psubus_w:
10110 case Intrinsic::x86_avx2_psubus_b:
10111 case Intrinsic::x86_avx2_psubus_w:
10112 return DAG.getNode(X86ISD::SUBUS, dl, Op.getValueType(),
10113 Op.getOperand(1), Op.getOperand(2));
10114
Craig Topper6d688152012-08-14 07:43:25 +000010115 // SSE3/AVX horizontal add/sub intrinsics
Duncan Sands04aa4ae2011-09-23 16:10:22 +000010116 case Intrinsic::x86_sse3_hadd_ps:
10117 case Intrinsic::x86_sse3_hadd_pd:
10118 case Intrinsic::x86_avx_hadd_ps_256:
10119 case Intrinsic::x86_avx_hadd_pd_256:
Duncan Sands04aa4ae2011-09-23 16:10:22 +000010120 case Intrinsic::x86_sse3_hsub_ps:
10121 case Intrinsic::x86_sse3_hsub_pd:
10122 case Intrinsic::x86_avx_hsub_ps_256:
10123 case Intrinsic::x86_avx_hsub_pd_256:
Craig Topper4bb3f342012-01-25 05:37:32 +000010124 case Intrinsic::x86_ssse3_phadd_w_128:
10125 case Intrinsic::x86_ssse3_phadd_d_128:
10126 case Intrinsic::x86_avx2_phadd_w:
10127 case Intrinsic::x86_avx2_phadd_d:
Craig Topper4bb3f342012-01-25 05:37:32 +000010128 case Intrinsic::x86_ssse3_phsub_w_128:
10129 case Intrinsic::x86_ssse3_phsub_d_128:
10130 case Intrinsic::x86_avx2_phsub_w:
Craig Topper6d688152012-08-14 07:43:25 +000010131 case Intrinsic::x86_avx2_phsub_d: {
10132 unsigned Opcode;
10133 switch (IntNo) {
10134 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
10135 case Intrinsic::x86_sse3_hadd_ps:
10136 case Intrinsic::x86_sse3_hadd_pd:
10137 case Intrinsic::x86_avx_hadd_ps_256:
10138 case Intrinsic::x86_avx_hadd_pd_256:
10139 Opcode = X86ISD::FHADD;
10140 break;
10141 case Intrinsic::x86_sse3_hsub_ps:
10142 case Intrinsic::x86_sse3_hsub_pd:
10143 case Intrinsic::x86_avx_hsub_ps_256:
10144 case Intrinsic::x86_avx_hsub_pd_256:
10145 Opcode = X86ISD::FHSUB;
10146 break;
10147 case Intrinsic::x86_ssse3_phadd_w_128:
10148 case Intrinsic::x86_ssse3_phadd_d_128:
10149 case Intrinsic::x86_avx2_phadd_w:
10150 case Intrinsic::x86_avx2_phadd_d:
10151 Opcode = X86ISD::HADD;
10152 break;
10153 case Intrinsic::x86_ssse3_phsub_w_128:
10154 case Intrinsic::x86_ssse3_phsub_d_128:
10155 case Intrinsic::x86_avx2_phsub_w:
10156 case Intrinsic::x86_avx2_phsub_d:
10157 Opcode = X86ISD::HSUB;
10158 break;
10159 }
10160 return DAG.getNode(Opcode, dl, Op.getValueType(),
Craig Topper4bb3f342012-01-25 05:37:32 +000010161 Op.getOperand(1), Op.getOperand(2));
Craig Topper6d688152012-08-14 07:43:25 +000010162 }
10163
Benjamin Kramer739c7a82012-12-21 14:04:55 +000010164 // SSE2/SSE41/AVX2 integer max/min intrinsics.
10165 case Intrinsic::x86_sse2_pmaxu_b:
10166 case Intrinsic::x86_sse41_pmaxuw:
10167 case Intrinsic::x86_sse41_pmaxud:
10168 case Intrinsic::x86_avx2_pmaxu_b:
10169 case Intrinsic::x86_avx2_pmaxu_w:
10170 case Intrinsic::x86_avx2_pmaxu_d:
10171 return DAG.getNode(X86ISD::UMAX, dl, Op.getValueType(),
10172 Op.getOperand(1), Op.getOperand(2));
10173 case Intrinsic::x86_sse2_pminu_b:
10174 case Intrinsic::x86_sse41_pminuw:
10175 case Intrinsic::x86_sse41_pminud:
10176 case Intrinsic::x86_avx2_pminu_b:
10177 case Intrinsic::x86_avx2_pminu_w:
10178 case Intrinsic::x86_avx2_pminu_d:
10179 return DAG.getNode(X86ISD::UMIN, dl, Op.getValueType(),
10180 Op.getOperand(1), Op.getOperand(2));
10181 case Intrinsic::x86_sse41_pmaxsb:
10182 case Intrinsic::x86_sse2_pmaxs_w:
10183 case Intrinsic::x86_sse41_pmaxsd:
10184 case Intrinsic::x86_avx2_pmaxs_b:
10185 case Intrinsic::x86_avx2_pmaxs_w:
10186 case Intrinsic::x86_avx2_pmaxs_d:
10187 return DAG.getNode(X86ISD::SMAX, dl, Op.getValueType(),
10188 Op.getOperand(1), Op.getOperand(2));
10189 case Intrinsic::x86_sse41_pminsb:
10190 case Intrinsic::x86_sse2_pmins_w:
10191 case Intrinsic::x86_sse41_pminsd:
10192 case Intrinsic::x86_avx2_pmins_b:
10193 case Intrinsic::x86_avx2_pmins_w:
10194 case Intrinsic::x86_avx2_pmins_d:
10195 return DAG.getNode(X86ISD::SMIN, dl, Op.getValueType(),
10196 Op.getOperand(1), Op.getOperand(2));
10197
Craig Topper6d688152012-08-14 07:43:25 +000010198 // AVX2 variable shift intrinsics
Craig Topper98fc7292011-11-19 17:46:46 +000010199 case Intrinsic::x86_avx2_psllv_d:
10200 case Intrinsic::x86_avx2_psllv_q:
10201 case Intrinsic::x86_avx2_psllv_d_256:
10202 case Intrinsic::x86_avx2_psllv_q_256:
Craig Topper98fc7292011-11-19 17:46:46 +000010203 case Intrinsic::x86_avx2_psrlv_d:
10204 case Intrinsic::x86_avx2_psrlv_q:
10205 case Intrinsic::x86_avx2_psrlv_d_256:
10206 case Intrinsic::x86_avx2_psrlv_q_256:
Craig Topper98fc7292011-11-19 17:46:46 +000010207 case Intrinsic::x86_avx2_psrav_d:
Craig Topper6d688152012-08-14 07:43:25 +000010208 case Intrinsic::x86_avx2_psrav_d_256: {
10209 unsigned Opcode;
10210 switch (IntNo) {
10211 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
10212 case Intrinsic::x86_avx2_psllv_d:
10213 case Intrinsic::x86_avx2_psllv_q:
10214 case Intrinsic::x86_avx2_psllv_d_256:
10215 case Intrinsic::x86_avx2_psllv_q_256:
10216 Opcode = ISD::SHL;
10217 break;
10218 case Intrinsic::x86_avx2_psrlv_d:
10219 case Intrinsic::x86_avx2_psrlv_q:
10220 case Intrinsic::x86_avx2_psrlv_d_256:
10221 case Intrinsic::x86_avx2_psrlv_q_256:
10222 Opcode = ISD::SRL;
10223 break;
10224 case Intrinsic::x86_avx2_psrav_d:
10225 case Intrinsic::x86_avx2_psrav_d_256:
10226 Opcode = ISD::SRA;
10227 break;
10228 }
10229 return DAG.getNode(Opcode, dl, Op.getValueType(),
10230 Op.getOperand(1), Op.getOperand(2));
10231 }
10232
Craig Topper969ba282012-01-25 06:43:11 +000010233 case Intrinsic::x86_ssse3_pshuf_b_128:
10234 case Intrinsic::x86_avx2_pshuf_b:
10235 return DAG.getNode(X86ISD::PSHUFB, dl, Op.getValueType(),
10236 Op.getOperand(1), Op.getOperand(2));
Craig Topper6d688152012-08-14 07:43:25 +000010237
Craig Topper969ba282012-01-25 06:43:11 +000010238 case Intrinsic::x86_ssse3_psign_b_128:
10239 case Intrinsic::x86_ssse3_psign_w_128:
10240 case Intrinsic::x86_ssse3_psign_d_128:
10241 case Intrinsic::x86_avx2_psign_b:
10242 case Intrinsic::x86_avx2_psign_w:
10243 case Intrinsic::x86_avx2_psign_d:
10244 return DAG.getNode(X86ISD::PSIGN, dl, Op.getValueType(),
10245 Op.getOperand(1), Op.getOperand(2));
Craig Topper6d688152012-08-14 07:43:25 +000010246
Craig Toppere566cd02012-01-26 07:18:03 +000010247 case Intrinsic::x86_sse41_insertps:
10248 return DAG.getNode(X86ISD::INSERTPS, dl, Op.getValueType(),
10249 Op.getOperand(1), Op.getOperand(2), Op.getOperand(3));
Craig Topper6d688152012-08-14 07:43:25 +000010250
Craig Toppere566cd02012-01-26 07:18:03 +000010251 case Intrinsic::x86_avx_vperm2f128_ps_256:
10252 case Intrinsic::x86_avx_vperm2f128_pd_256:
10253 case Intrinsic::x86_avx_vperm2f128_si_256:
10254 case Intrinsic::x86_avx2_vperm2i128:
10255 return DAG.getNode(X86ISD::VPERM2X128, dl, Op.getValueType(),
10256 Op.getOperand(1), Op.getOperand(2), Op.getOperand(3));
Craig Topper6d688152012-08-14 07:43:25 +000010257
Craig Topperffa6c402012-04-16 07:13:00 +000010258 case Intrinsic::x86_avx2_permd:
10259 case Intrinsic::x86_avx2_permps:
10260 // Operands intentionally swapped. Mask is last operand to intrinsic,
10261 // but second operand for node/intruction.
10262 return DAG.getNode(X86ISD::VPERMV, dl, Op.getValueType(),
10263 Op.getOperand(2), Op.getOperand(1));
Craig Topper98fc7292011-11-19 17:46:46 +000010264
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +000010265 // ptest and testp intrinsics. The intrinsic these come from are designed to
10266 // return an integer value, not just an instruction so lower it to the ptest
10267 // or testp pattern and a setcc for the result.
Eric Christopher71c67532009-07-29 00:28:05 +000010268 case Intrinsic::x86_sse41_ptestz:
10269 case Intrinsic::x86_sse41_ptestc:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +000010270 case Intrinsic::x86_sse41_ptestnzc:
10271 case Intrinsic::x86_avx_ptestz_256:
10272 case Intrinsic::x86_avx_ptestc_256:
10273 case Intrinsic::x86_avx_ptestnzc_256:
10274 case Intrinsic::x86_avx_vtestz_ps:
10275 case Intrinsic::x86_avx_vtestc_ps:
10276 case Intrinsic::x86_avx_vtestnzc_ps:
10277 case Intrinsic::x86_avx_vtestz_pd:
10278 case Intrinsic::x86_avx_vtestc_pd:
10279 case Intrinsic::x86_avx_vtestnzc_pd:
10280 case Intrinsic::x86_avx_vtestz_ps_256:
10281 case Intrinsic::x86_avx_vtestc_ps_256:
10282 case Intrinsic::x86_avx_vtestnzc_ps_256:
10283 case Intrinsic::x86_avx_vtestz_pd_256:
10284 case Intrinsic::x86_avx_vtestc_pd_256:
10285 case Intrinsic::x86_avx_vtestnzc_pd_256: {
10286 bool IsTestPacked = false;
Craig Topper6d688152012-08-14 07:43:25 +000010287 unsigned X86CC;
Eric Christopher71c67532009-07-29 00:28:05 +000010288 switch (IntNo) {
Eric Christopher978dae32009-07-29 18:14:04 +000010289 default: llvm_unreachable("Bad fallthrough in Intrinsic lowering.");
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +000010290 case Intrinsic::x86_avx_vtestz_ps:
10291 case Intrinsic::x86_avx_vtestz_pd:
10292 case Intrinsic::x86_avx_vtestz_ps_256:
10293 case Intrinsic::x86_avx_vtestz_pd_256:
10294 IsTestPacked = true; // Fallthrough
Eric Christopher71c67532009-07-29 00:28:05 +000010295 case Intrinsic::x86_sse41_ptestz:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +000010296 case Intrinsic::x86_avx_ptestz_256:
Eric Christopher71c67532009-07-29 00:28:05 +000010297 // ZF = 1
10298 X86CC = X86::COND_E;
10299 break;
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +000010300 case Intrinsic::x86_avx_vtestc_ps:
10301 case Intrinsic::x86_avx_vtestc_pd:
10302 case Intrinsic::x86_avx_vtestc_ps_256:
10303 case Intrinsic::x86_avx_vtestc_pd_256:
10304 IsTestPacked = true; // Fallthrough
Eric Christopher71c67532009-07-29 00:28:05 +000010305 case Intrinsic::x86_sse41_ptestc:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +000010306 case Intrinsic::x86_avx_ptestc_256:
Eric Christopher71c67532009-07-29 00:28:05 +000010307 // CF = 1
10308 X86CC = X86::COND_B;
10309 break;
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +000010310 case Intrinsic::x86_avx_vtestnzc_ps:
10311 case Intrinsic::x86_avx_vtestnzc_pd:
10312 case Intrinsic::x86_avx_vtestnzc_ps_256:
10313 case Intrinsic::x86_avx_vtestnzc_pd_256:
10314 IsTestPacked = true; // Fallthrough
Eric Christopherfd179292009-08-27 18:07:15 +000010315 case Intrinsic::x86_sse41_ptestnzc:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +000010316 case Intrinsic::x86_avx_ptestnzc_256:
Eric Christopher71c67532009-07-29 00:28:05 +000010317 // ZF and CF = 0
10318 X86CC = X86::COND_A;
10319 break;
10320 }
Eric Christopherfd179292009-08-27 18:07:15 +000010321
Eric Christopher71c67532009-07-29 00:28:05 +000010322 SDValue LHS = Op.getOperand(1);
10323 SDValue RHS = Op.getOperand(2);
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +000010324 unsigned TestOpc = IsTestPacked ? X86ISD::TESTP : X86ISD::PTEST;
10325 SDValue Test = DAG.getNode(TestOpc, dl, MVT::i32, LHS, RHS);
Owen Anderson825b72b2009-08-11 20:47:22 +000010326 SDValue CC = DAG.getConstant(X86CC, MVT::i8);
10327 SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8, CC, Test);
10328 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC);
Eric Christopher71c67532009-07-29 00:28:05 +000010329 }
Evan Cheng5759f972008-05-04 09:15:50 +000010330
Craig Topper80e46362012-01-23 06:16:53 +000010331 // SSE/AVX shift intrinsics
10332 case Intrinsic::x86_sse2_psll_w:
10333 case Intrinsic::x86_sse2_psll_d:
10334 case Intrinsic::x86_sse2_psll_q:
10335 case Intrinsic::x86_avx2_psll_w:
10336 case Intrinsic::x86_avx2_psll_d:
10337 case Intrinsic::x86_avx2_psll_q:
Craig Topper80e46362012-01-23 06:16:53 +000010338 case Intrinsic::x86_sse2_psrl_w:
10339 case Intrinsic::x86_sse2_psrl_d:
10340 case Intrinsic::x86_sse2_psrl_q:
10341 case Intrinsic::x86_avx2_psrl_w:
10342 case Intrinsic::x86_avx2_psrl_d:
10343 case Intrinsic::x86_avx2_psrl_q:
Craig Topper80e46362012-01-23 06:16:53 +000010344 case Intrinsic::x86_sse2_psra_w:
10345 case Intrinsic::x86_sse2_psra_d:
10346 case Intrinsic::x86_avx2_psra_w:
Craig Topper6d688152012-08-14 07:43:25 +000010347 case Intrinsic::x86_avx2_psra_d: {
10348 unsigned Opcode;
10349 switch (IntNo) {
10350 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
10351 case Intrinsic::x86_sse2_psll_w:
10352 case Intrinsic::x86_sse2_psll_d:
10353 case Intrinsic::x86_sse2_psll_q:
10354 case Intrinsic::x86_avx2_psll_w:
10355 case Intrinsic::x86_avx2_psll_d:
10356 case Intrinsic::x86_avx2_psll_q:
10357 Opcode = X86ISD::VSHL;
10358 break;
10359 case Intrinsic::x86_sse2_psrl_w:
10360 case Intrinsic::x86_sse2_psrl_d:
10361 case Intrinsic::x86_sse2_psrl_q:
10362 case Intrinsic::x86_avx2_psrl_w:
10363 case Intrinsic::x86_avx2_psrl_d:
10364 case Intrinsic::x86_avx2_psrl_q:
10365 Opcode = X86ISD::VSRL;
10366 break;
10367 case Intrinsic::x86_sse2_psra_w:
10368 case Intrinsic::x86_sse2_psra_d:
10369 case Intrinsic::x86_avx2_psra_w:
10370 case Intrinsic::x86_avx2_psra_d:
10371 Opcode = X86ISD::VSRA;
10372 break;
10373 }
10374 return DAG.getNode(Opcode, dl, Op.getValueType(),
Craig Topper80e46362012-01-23 06:16:53 +000010375 Op.getOperand(1), Op.getOperand(2));
Craig Topper6d688152012-08-14 07:43:25 +000010376 }
10377
10378 // SSE/AVX immediate shift intrinsics
Evan Cheng5759f972008-05-04 09:15:50 +000010379 case Intrinsic::x86_sse2_pslli_w:
10380 case Intrinsic::x86_sse2_pslli_d:
10381 case Intrinsic::x86_sse2_pslli_q:
Craig Topper80e46362012-01-23 06:16:53 +000010382 case Intrinsic::x86_avx2_pslli_w:
10383 case Intrinsic::x86_avx2_pslli_d:
10384 case Intrinsic::x86_avx2_pslli_q:
Evan Cheng5759f972008-05-04 09:15:50 +000010385 case Intrinsic::x86_sse2_psrli_w:
10386 case Intrinsic::x86_sse2_psrli_d:
10387 case Intrinsic::x86_sse2_psrli_q:
Craig Topper80e46362012-01-23 06:16:53 +000010388 case Intrinsic::x86_avx2_psrli_w:
10389 case Intrinsic::x86_avx2_psrli_d:
10390 case Intrinsic::x86_avx2_psrli_q:
Evan Cheng5759f972008-05-04 09:15:50 +000010391 case Intrinsic::x86_sse2_psrai_w:
10392 case Intrinsic::x86_sse2_psrai_d:
Craig Topper80e46362012-01-23 06:16:53 +000010393 case Intrinsic::x86_avx2_psrai_w:
Craig Topper6d688152012-08-14 07:43:25 +000010394 case Intrinsic::x86_avx2_psrai_d: {
10395 unsigned Opcode;
10396 switch (IntNo) {
10397 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
10398 case Intrinsic::x86_sse2_pslli_w:
10399 case Intrinsic::x86_sse2_pslli_d:
10400 case Intrinsic::x86_sse2_pslli_q:
10401 case Intrinsic::x86_avx2_pslli_w:
10402 case Intrinsic::x86_avx2_pslli_d:
10403 case Intrinsic::x86_avx2_pslli_q:
10404 Opcode = X86ISD::VSHLI;
10405 break;
10406 case Intrinsic::x86_sse2_psrli_w:
10407 case Intrinsic::x86_sse2_psrli_d:
10408 case Intrinsic::x86_sse2_psrli_q:
10409 case Intrinsic::x86_avx2_psrli_w:
10410 case Intrinsic::x86_avx2_psrli_d:
10411 case Intrinsic::x86_avx2_psrli_q:
10412 Opcode = X86ISD::VSRLI;
10413 break;
10414 case Intrinsic::x86_sse2_psrai_w:
10415 case Intrinsic::x86_sse2_psrai_d:
10416 case Intrinsic::x86_avx2_psrai_w:
10417 case Intrinsic::x86_avx2_psrai_d:
10418 Opcode = X86ISD::VSRAI;
10419 break;
10420 }
10421 return getTargetVShiftNode(Opcode, dl, Op.getValueType(),
Craig Topper80e46362012-01-23 06:16:53 +000010422 Op.getOperand(1), Op.getOperand(2), DAG);
Craig Topper6d688152012-08-14 07:43:25 +000010423 }
10424
Craig Topper4feb6472012-08-06 06:22:36 +000010425 case Intrinsic::x86_sse42_pcmpistria128:
10426 case Intrinsic::x86_sse42_pcmpestria128:
10427 case Intrinsic::x86_sse42_pcmpistric128:
10428 case Intrinsic::x86_sse42_pcmpestric128:
10429 case Intrinsic::x86_sse42_pcmpistrio128:
10430 case Intrinsic::x86_sse42_pcmpestrio128:
10431 case Intrinsic::x86_sse42_pcmpistris128:
10432 case Intrinsic::x86_sse42_pcmpestris128:
10433 case Intrinsic::x86_sse42_pcmpistriz128:
10434 case Intrinsic::x86_sse42_pcmpestriz128: {
10435 unsigned Opcode;
10436 unsigned X86CC;
10437 switch (IntNo) {
10438 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
10439 case Intrinsic::x86_sse42_pcmpistria128:
10440 Opcode = X86ISD::PCMPISTRI;
10441 X86CC = X86::COND_A;
10442 break;
10443 case Intrinsic::x86_sse42_pcmpestria128:
10444 Opcode = X86ISD::PCMPESTRI;
10445 X86CC = X86::COND_A;
10446 break;
10447 case Intrinsic::x86_sse42_pcmpistric128:
10448 Opcode = X86ISD::PCMPISTRI;
10449 X86CC = X86::COND_B;
10450 break;
10451 case Intrinsic::x86_sse42_pcmpestric128:
10452 Opcode = X86ISD::PCMPESTRI;
10453 X86CC = X86::COND_B;
10454 break;
10455 case Intrinsic::x86_sse42_pcmpistrio128:
10456 Opcode = X86ISD::PCMPISTRI;
10457 X86CC = X86::COND_O;
10458 break;
10459 case Intrinsic::x86_sse42_pcmpestrio128:
10460 Opcode = X86ISD::PCMPESTRI;
10461 X86CC = X86::COND_O;
10462 break;
10463 case Intrinsic::x86_sse42_pcmpistris128:
10464 Opcode = X86ISD::PCMPISTRI;
10465 X86CC = X86::COND_S;
10466 break;
10467 case Intrinsic::x86_sse42_pcmpestris128:
10468 Opcode = X86ISD::PCMPESTRI;
10469 X86CC = X86::COND_S;
10470 break;
10471 case Intrinsic::x86_sse42_pcmpistriz128:
10472 Opcode = X86ISD::PCMPISTRI;
10473 X86CC = X86::COND_E;
10474 break;
10475 case Intrinsic::x86_sse42_pcmpestriz128:
10476 Opcode = X86ISD::PCMPESTRI;
10477 X86CC = X86::COND_E;
10478 break;
10479 }
10480 SmallVector<SDValue, 5> NewOps;
10481 NewOps.append(Op->op_begin()+1, Op->op_end());
10482 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::i32);
10483 SDValue PCMP = DAG.getNode(Opcode, dl, VTs, NewOps.data(), NewOps.size());
10484 SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
10485 DAG.getConstant(X86CC, MVT::i8),
10486 SDValue(PCMP.getNode(), 1));
10487 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC);
10488 }
Craig Topper6d688152012-08-14 07:43:25 +000010489
Craig Topper4feb6472012-08-06 06:22:36 +000010490 case Intrinsic::x86_sse42_pcmpistri128:
10491 case Intrinsic::x86_sse42_pcmpestri128: {
10492 unsigned Opcode;
10493 if (IntNo == Intrinsic::x86_sse42_pcmpistri128)
10494 Opcode = X86ISD::PCMPISTRI;
10495 else
10496 Opcode = X86ISD::PCMPESTRI;
10497
10498 SmallVector<SDValue, 5> NewOps;
10499 NewOps.append(Op->op_begin()+1, Op->op_end());
10500 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::i32);
10501 return DAG.getNode(Opcode, dl, VTs, NewOps.data(), NewOps.size());
10502 }
Craig Topper0e292372012-08-24 04:03:22 +000010503 case Intrinsic::x86_fma_vfmadd_ps:
10504 case Intrinsic::x86_fma_vfmadd_pd:
10505 case Intrinsic::x86_fma_vfmsub_ps:
10506 case Intrinsic::x86_fma_vfmsub_pd:
10507 case Intrinsic::x86_fma_vfnmadd_ps:
10508 case Intrinsic::x86_fma_vfnmadd_pd:
10509 case Intrinsic::x86_fma_vfnmsub_ps:
10510 case Intrinsic::x86_fma_vfnmsub_pd:
10511 case Intrinsic::x86_fma_vfmaddsub_ps:
10512 case Intrinsic::x86_fma_vfmaddsub_pd:
10513 case Intrinsic::x86_fma_vfmsubadd_ps:
10514 case Intrinsic::x86_fma_vfmsubadd_pd:
10515 case Intrinsic::x86_fma_vfmadd_ps_256:
10516 case Intrinsic::x86_fma_vfmadd_pd_256:
10517 case Intrinsic::x86_fma_vfmsub_ps_256:
10518 case Intrinsic::x86_fma_vfmsub_pd_256:
10519 case Intrinsic::x86_fma_vfnmadd_ps_256:
10520 case Intrinsic::x86_fma_vfnmadd_pd_256:
10521 case Intrinsic::x86_fma_vfnmsub_ps_256:
10522 case Intrinsic::x86_fma_vfnmsub_pd_256:
10523 case Intrinsic::x86_fma_vfmaddsub_ps_256:
10524 case Intrinsic::x86_fma_vfmaddsub_pd_256:
10525 case Intrinsic::x86_fma_vfmsubadd_ps_256:
10526 case Intrinsic::x86_fma_vfmsubadd_pd_256: {
Craig Topper0e292372012-08-24 04:03:22 +000010527 unsigned Opc;
10528 switch (IntNo) {
10529 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
10530 case Intrinsic::x86_fma_vfmadd_ps:
10531 case Intrinsic::x86_fma_vfmadd_pd:
10532 case Intrinsic::x86_fma_vfmadd_ps_256:
10533 case Intrinsic::x86_fma_vfmadd_pd_256:
10534 Opc = X86ISD::FMADD;
10535 break;
10536 case Intrinsic::x86_fma_vfmsub_ps:
10537 case Intrinsic::x86_fma_vfmsub_pd:
10538 case Intrinsic::x86_fma_vfmsub_ps_256:
10539 case Intrinsic::x86_fma_vfmsub_pd_256:
10540 Opc = X86ISD::FMSUB;
10541 break;
10542 case Intrinsic::x86_fma_vfnmadd_ps:
10543 case Intrinsic::x86_fma_vfnmadd_pd:
10544 case Intrinsic::x86_fma_vfnmadd_ps_256:
10545 case Intrinsic::x86_fma_vfnmadd_pd_256:
10546 Opc = X86ISD::FNMADD;
10547 break;
10548 case Intrinsic::x86_fma_vfnmsub_ps:
10549 case Intrinsic::x86_fma_vfnmsub_pd:
10550 case Intrinsic::x86_fma_vfnmsub_ps_256:
10551 case Intrinsic::x86_fma_vfnmsub_pd_256:
10552 Opc = X86ISD::FNMSUB;
10553 break;
10554 case Intrinsic::x86_fma_vfmaddsub_ps:
10555 case Intrinsic::x86_fma_vfmaddsub_pd:
10556 case Intrinsic::x86_fma_vfmaddsub_ps_256:
10557 case Intrinsic::x86_fma_vfmaddsub_pd_256:
10558 Opc = X86ISD::FMADDSUB;
10559 break;
10560 case Intrinsic::x86_fma_vfmsubadd_ps:
10561 case Intrinsic::x86_fma_vfmsubadd_pd:
10562 case Intrinsic::x86_fma_vfmsubadd_ps_256:
10563 case Intrinsic::x86_fma_vfmsubadd_pd_256:
10564 Opc = X86ISD::FMSUBADD;
10565 break;
10566 }
10567
10568 return DAG.getNode(Opc, dl, Op.getValueType(), Op.getOperand(1),
10569 Op.getOperand(2), Op.getOperand(3));
10570 }
Evan Cheng38bcbaf2005-12-23 07:31:11 +000010571 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000010572}
Evan Cheng72261582005-12-20 06:22:03 +000010573
Craig Topper55b24052012-09-11 06:15:32 +000010574static SDValue LowerINTRINSIC_W_CHAIN(SDValue Op, SelectionDAG &DAG) {
Benjamin Kramerb9bee042012-07-12 09:31:43 +000010575 DebugLoc dl = Op.getDebugLoc();
10576 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
10577 switch (IntNo) {
10578 default: return SDValue(); // Don't custom lower most intrinsics.
10579
10580 // RDRAND intrinsics.
10581 case Intrinsic::x86_rdrand_16:
10582 case Intrinsic::x86_rdrand_32:
10583 case Intrinsic::x86_rdrand_64: {
10584 // Emit the node with the right value type.
Benjamin Kramerfeae00a2012-07-12 18:14:57 +000010585 SDVTList VTs = DAG.getVTList(Op->getValueType(0), MVT::Glue, MVT::Other);
10586 SDValue Result = DAG.getNode(X86ISD::RDRAND, dl, VTs, Op.getOperand(0));
Benjamin Kramerb9bee042012-07-12 09:31:43 +000010587
10588 // If the value returned by RDRAND was valid (CF=1), return 1. Otherwise
10589 // return the value from Rand, which is always 0, casted to i32.
10590 SDValue Ops[] = { DAG.getZExtOrTrunc(Result, dl, Op->getValueType(1)),
10591 DAG.getConstant(1, Op->getValueType(1)),
10592 DAG.getConstant(X86::COND_B, MVT::i32),
10593 SDValue(Result.getNode(), 1) };
10594 SDValue isValid = DAG.getNode(X86ISD::CMOV, dl,
10595 DAG.getVTList(Op->getValueType(1), MVT::Glue),
10596 Ops, 4);
10597
10598 // Return { result, isValid, chain }.
10599 return DAG.getNode(ISD::MERGE_VALUES, dl, Op->getVTList(), Result, isValid,
Benjamin Kramerfeae00a2012-07-12 18:14:57 +000010600 SDValue(Result.getNode(), 2));
Benjamin Kramerb9bee042012-07-12 09:31:43 +000010601 }
10602 }
10603}
10604
Dan Gohmand858e902010-04-17 15:26:15 +000010605SDValue X86TargetLowering::LowerRETURNADDR(SDValue Op,
10606 SelectionDAG &DAG) const {
Evan Cheng2457f2c2010-05-22 01:47:14 +000010607 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
10608 MFI->setReturnAddressIsTaken(true);
10609
Bill Wendling64e87322009-01-16 19:25:27 +000010610 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Dale Johannesen6f38cb62009-02-07 19:59:05 +000010611 DebugLoc dl = Op.getDebugLoc();
Michael Liaoaa3c2c02012-10-25 06:29:14 +000010612 EVT PtrVT = getPointerTy();
Bill Wendling64e87322009-01-16 19:25:27 +000010613
10614 if (Depth > 0) {
10615 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
10616 SDValue Offset =
Michael Liaoaa3c2c02012-10-25 06:29:14 +000010617 DAG.getConstant(RegInfo->getSlotSize(), PtrVT);
10618 return DAG.getLoad(PtrVT, dl, DAG.getEntryNode(),
10619 DAG.getNode(ISD::ADD, dl, PtrVT,
Dale Johannesene4d209d2009-02-03 20:21:25 +000010620 FrameAddr, Offset),
Pete Cooperd752e0f2011-11-08 18:42:53 +000010621 MachinePointerInfo(), false, false, false, 0);
Bill Wendling64e87322009-01-16 19:25:27 +000010622 }
10623
10624 // Just load the return address.
Dan Gohman475871a2008-07-27 21:46:04 +000010625 SDValue RetAddrFI = getReturnAddressFrameIndex(DAG);
Michael Liaoaa3c2c02012-10-25 06:29:14 +000010626 return DAG.getLoad(PtrVT, dl, DAG.getEntryNode(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000010627 RetAddrFI, MachinePointerInfo(), false, false, false, 0);
Nate Begemanbcc5f362007-01-29 22:58:52 +000010628}
10629
Dan Gohmand858e902010-04-17 15:26:15 +000010630SDValue X86TargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng184793f2008-09-27 01:56:22 +000010631 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
10632 MFI->setFrameAddressIsTaken(true);
Evan Cheng2457f2c2010-05-22 01:47:14 +000010633
Owen Andersone50ed302009-08-10 22:56:29 +000010634 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +000010635 DebugLoc dl = Op.getDebugLoc(); // FIXME probably not meaningful
Evan Cheng184793f2008-09-27 01:56:22 +000010636 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
10637 unsigned FrameReg = Subtarget->is64Bit() ? X86::RBP : X86::EBP;
Dale Johannesendd64c412009-02-04 00:33:20 +000010638 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, VT);
Evan Cheng184793f2008-09-27 01:56:22 +000010639 while (Depth--)
Chris Lattner51abfe42010-09-21 06:02:19 +000010640 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr,
10641 MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000010642 false, false, false, 0);
Evan Cheng184793f2008-09-27 01:56:22 +000010643 return FrameAddr;
Nate Begemanbcc5f362007-01-29 22:58:52 +000010644}
10645
Dan Gohman475871a2008-07-27 21:46:04 +000010646SDValue X86TargetLowering::LowerFRAME_TO_ARGS_OFFSET(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +000010647 SelectionDAG &DAG) const {
Michael Liaoaa3c2c02012-10-25 06:29:14 +000010648 return DAG.getIntPtrConstant(2 * RegInfo->getSlotSize());
Anton Korobeynikov2365f512007-07-14 14:06:15 +000010649}
10650
Dan Gohmand858e902010-04-17 15:26:15 +000010651SDValue X86TargetLowering::LowerEH_RETURN(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +000010652 SDValue Chain = Op.getOperand(0);
10653 SDValue Offset = Op.getOperand(1);
10654 SDValue Handler = Op.getOperand(2);
Dale Johannesen6f38cb62009-02-07 19:59:05 +000010655 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov2365f512007-07-14 14:06:15 +000010656
Dan Gohmand8816272010-08-11 18:14:00 +000010657 SDValue Frame = DAG.getCopyFromReg(DAG.getEntryNode(), dl,
10658 Subtarget->is64Bit() ? X86::RBP : X86::EBP,
10659 getPointerTy());
Anton Korobeynikovb84c1672008-09-08 21:12:47 +000010660 unsigned StoreAddrReg = (Subtarget->is64Bit() ? X86::RCX : X86::ECX);
Anton Korobeynikov2365f512007-07-14 14:06:15 +000010661
Dan Gohmand8816272010-08-11 18:14:00 +000010662 SDValue StoreAddr = DAG.getNode(ISD::ADD, dl, getPointerTy(), Frame,
Michael Liaoaa3c2c02012-10-25 06:29:14 +000010663 DAG.getIntPtrConstant(RegInfo->getSlotSize()));
Dale Johannesene4d209d2009-02-03 20:21:25 +000010664 StoreAddr = DAG.getNode(ISD::ADD, dl, getPointerTy(), StoreAddr, Offset);
Chris Lattner8026a9d2010-09-21 17:50:43 +000010665 Chain = DAG.getStore(Chain, dl, Handler, StoreAddr, MachinePointerInfo(),
10666 false, false, 0);
Dale Johannesendd64c412009-02-04 00:33:20 +000010667 Chain = DAG.getCopyToReg(Chain, dl, StoreAddrReg, StoreAddr);
Anton Korobeynikov2365f512007-07-14 14:06:15 +000010668
Dale Johannesene4d209d2009-02-03 20:21:25 +000010669 return DAG.getNode(X86ISD::EH_RETURN, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +000010670 MVT::Other,
Anton Korobeynikovb84c1672008-09-08 21:12:47 +000010671 Chain, DAG.getRegister(StoreAddrReg, getPointerTy()));
Anton Korobeynikov2365f512007-07-14 14:06:15 +000010672}
10673
Michael Liao6c0e04c2012-10-15 22:39:43 +000010674SDValue X86TargetLowering::lowerEH_SJLJ_SETJMP(SDValue Op,
10675 SelectionDAG &DAG) const {
10676 DebugLoc DL = Op.getDebugLoc();
10677 return DAG.getNode(X86ISD::EH_SJLJ_SETJMP, DL,
10678 DAG.getVTList(MVT::i32, MVT::Other),
10679 Op.getOperand(0), Op.getOperand(1));
10680}
10681
10682SDValue X86TargetLowering::lowerEH_SJLJ_LONGJMP(SDValue Op,
10683 SelectionDAG &DAG) const {
10684 DebugLoc DL = Op.getDebugLoc();
10685 return DAG.getNode(X86ISD::EH_SJLJ_LONGJMP, DL, MVT::Other,
10686 Op.getOperand(0), Op.getOperand(1));
10687}
10688
Craig Topper55b24052012-09-11 06:15:32 +000010689static SDValue LowerADJUST_TRAMPOLINE(SDValue Op, SelectionDAG &DAG) {
Duncan Sands4a544a72011-09-06 13:37:06 +000010690 return Op.getOperand(0);
10691}
10692
10693SDValue X86TargetLowering::LowerINIT_TRAMPOLINE(SDValue Op,
10694 SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +000010695 SDValue Root = Op.getOperand(0);
10696 SDValue Trmp = Op.getOperand(1); // trampoline
10697 SDValue FPtr = Op.getOperand(2); // nested function
10698 SDValue Nest = Op.getOperand(3); // 'nest' parameter value
Dale Johannesen6f38cb62009-02-07 19:59:05 +000010699 DebugLoc dl = Op.getDebugLoc();
Duncan Sandsb116fac2007-07-27 20:02:49 +000010700
Dan Gohman69de1932008-02-06 22:27:42 +000010701 const Value *TrmpAddr = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Michael Liao7abf67a2012-10-04 19:50:43 +000010702 const TargetRegisterInfo* TRI = getTargetMachine().getRegisterInfo();
Duncan Sandsb116fac2007-07-27 20:02:49 +000010703
10704 if (Subtarget->is64Bit()) {
Dan Gohman475871a2008-07-27 21:46:04 +000010705 SDValue OutChains[6];
Duncan Sands339e14f2008-01-16 22:55:25 +000010706
10707 // Large code-model.
Chris Lattnera62fe662010-02-05 19:20:30 +000010708 const unsigned char JMP64r = 0xFF; // 64-bit jmp through register opcode.
10709 const unsigned char MOV64ri = 0xB8; // X86::MOV64ri opcode.
Duncan Sands339e14f2008-01-16 22:55:25 +000010710
Michael Liao7abf67a2012-10-04 19:50:43 +000010711 const unsigned char N86R10 = TRI->getEncodingValue(X86::R10) & 0x7;
10712 const unsigned char N86R11 = TRI->getEncodingValue(X86::R11) & 0x7;
Duncan Sands339e14f2008-01-16 22:55:25 +000010713
10714 const unsigned char REX_WB = 0x40 | 0x08 | 0x01; // REX prefix
10715
10716 // Load the pointer to the nested function into R11.
10717 unsigned OpCode = ((MOV64ri | N86R11) << 8) | REX_WB; // movabsq r11
Dan Gohman475871a2008-07-27 21:46:04 +000010718 SDValue Addr = Trmp;
Owen Anderson825b72b2009-08-11 20:47:22 +000010719 OutChains[0] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Chris Lattner8026a9d2010-09-21 17:50:43 +000010720 Addr, MachinePointerInfo(TrmpAddr),
10721 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +000010722
Owen Anderson825b72b2009-08-11 20:47:22 +000010723 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
10724 DAG.getConstant(2, MVT::i64));
Chris Lattner8026a9d2010-09-21 17:50:43 +000010725 OutChains[1] = DAG.getStore(Root, dl, FPtr, Addr,
10726 MachinePointerInfo(TrmpAddr, 2),
David Greene67c9d422010-02-15 16:53:33 +000010727 false, false, 2);
Duncan Sands339e14f2008-01-16 22:55:25 +000010728
10729 // Load the 'nest' parameter value into R10.
10730 // R10 is specified in X86CallingConv.td
10731 OpCode = ((MOV64ri | N86R10) << 8) | REX_WB; // movabsq r10
Owen Anderson825b72b2009-08-11 20:47:22 +000010732 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
10733 DAG.getConstant(10, MVT::i64));
10734 OutChains[2] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Chris Lattner8026a9d2010-09-21 17:50:43 +000010735 Addr, MachinePointerInfo(TrmpAddr, 10),
10736 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +000010737
Owen Anderson825b72b2009-08-11 20:47:22 +000010738 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
10739 DAG.getConstant(12, MVT::i64));
Chris Lattner8026a9d2010-09-21 17:50:43 +000010740 OutChains[3] = DAG.getStore(Root, dl, Nest, Addr,
10741 MachinePointerInfo(TrmpAddr, 12),
David Greene67c9d422010-02-15 16:53:33 +000010742 false, false, 2);
Duncan Sands339e14f2008-01-16 22:55:25 +000010743
10744 // Jump to the nested function.
10745 OpCode = (JMP64r << 8) | REX_WB; // jmpq *...
Owen Anderson825b72b2009-08-11 20:47:22 +000010746 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
10747 DAG.getConstant(20, MVT::i64));
10748 OutChains[4] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Chris Lattner8026a9d2010-09-21 17:50:43 +000010749 Addr, MachinePointerInfo(TrmpAddr, 20),
10750 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +000010751
10752 unsigned char ModRM = N86R11 | (4 << 3) | (3 << 6); // ...r11
Owen Anderson825b72b2009-08-11 20:47:22 +000010753 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
10754 DAG.getConstant(22, MVT::i64));
10755 OutChains[5] = DAG.getStore(Root, dl, DAG.getConstant(ModRM, MVT::i8), Addr,
Chris Lattner8026a9d2010-09-21 17:50:43 +000010756 MachinePointerInfo(TrmpAddr, 22),
10757 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +000010758
Duncan Sands4a544a72011-09-06 13:37:06 +000010759 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains, 6);
Duncan Sandsb116fac2007-07-27 20:02:49 +000010760 } else {
Dan Gohmanbbfb9c52008-01-31 01:01:48 +000010761 const Function *Func =
Duncan Sandsb116fac2007-07-27 20:02:49 +000010762 cast<Function>(cast<SrcValueSDNode>(Op.getOperand(5))->getValue());
Sandeep Patel65c3c8f2009-09-02 08:44:58 +000010763 CallingConv::ID CC = Func->getCallingConv();
Duncan Sandsee465742007-08-29 19:01:20 +000010764 unsigned NestReg;
Duncan Sandsb116fac2007-07-27 20:02:49 +000010765
10766 switch (CC) {
10767 default:
Torok Edwinc23197a2009-07-14 16:55:14 +000010768 llvm_unreachable("Unsupported calling convention");
Duncan Sandsb116fac2007-07-27 20:02:49 +000010769 case CallingConv::C:
Duncan Sandsb116fac2007-07-27 20:02:49 +000010770 case CallingConv::X86_StdCall: {
10771 // Pass 'nest' parameter in ECX.
10772 // Must be kept in sync with X86CallingConv.td
Duncan Sandsee465742007-08-29 19:01:20 +000010773 NestReg = X86::ECX;
Duncan Sandsb116fac2007-07-27 20:02:49 +000010774
10775 // Check that ECX wasn't needed by an 'inreg' parameter.
Chris Lattnerdb125cf2011-07-18 04:54:35 +000010776 FunctionType *FTy = Func->getFunctionType();
Bill Wendling99faa3b2012-12-07 23:16:57 +000010777 const AttributeSet &Attrs = Func->getAttributes();
Duncan Sandsb116fac2007-07-27 20:02:49 +000010778
Chris Lattner58d74912008-03-12 17:45:29 +000010779 if (!Attrs.isEmpty() && !Func->isVarArg()) {
Duncan Sandsb116fac2007-07-27 20:02:49 +000010780 unsigned InRegCount = 0;
10781 unsigned Idx = 1;
10782
10783 for (FunctionType::param_iterator I = FTy->param_begin(),
10784 E = FTy->param_end(); I != E; ++I, ++Idx)
Bill Wendling034b94b2012-12-19 07:18:57 +000010785 if (Attrs.getParamAttributes(Idx).hasAttribute(Attribute::InReg))
Duncan Sandsb116fac2007-07-27 20:02:49 +000010786 // FIXME: should only count parameters that are lowered to integers.
Anton Korobeynikovbff66b02008-09-09 18:22:57 +000010787 InRegCount += (TD->getTypeSizeInBits(*I) + 31) / 32;
Duncan Sandsb116fac2007-07-27 20:02:49 +000010788
10789 if (InRegCount > 2) {
Eric Christopher90eb4022010-07-22 00:26:08 +000010790 report_fatal_error("Nest register in use - reduce number of inreg"
10791 " parameters!");
Duncan Sandsb116fac2007-07-27 20:02:49 +000010792 }
10793 }
10794 break;
10795 }
10796 case CallingConv::X86_FastCall:
Anton Korobeynikovded05e32010-05-16 09:08:45 +000010797 case CallingConv::X86_ThisCall:
Duncan Sandsbf53c292008-09-10 13:22:10 +000010798 case CallingConv::Fast:
Duncan Sandsb116fac2007-07-27 20:02:49 +000010799 // Pass 'nest' parameter in EAX.
10800 // Must be kept in sync with X86CallingConv.td
Duncan Sandsee465742007-08-29 19:01:20 +000010801 NestReg = X86::EAX;
Duncan Sandsb116fac2007-07-27 20:02:49 +000010802 break;
10803 }
10804
Dan Gohman475871a2008-07-27 21:46:04 +000010805 SDValue OutChains[4];
10806 SDValue Addr, Disp;
Duncan Sandsb116fac2007-07-27 20:02:49 +000010807
Owen Anderson825b72b2009-08-11 20:47:22 +000010808 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
10809 DAG.getConstant(10, MVT::i32));
10810 Disp = DAG.getNode(ISD::SUB, dl, MVT::i32, FPtr, Addr);
Duncan Sandsb116fac2007-07-27 20:02:49 +000010811
Chris Lattnera62fe662010-02-05 19:20:30 +000010812 // This is storing the opcode for MOV32ri.
10813 const unsigned char MOV32ri = 0xB8; // X86::MOV32ri's opcode byte.
Michael Liao7abf67a2012-10-04 19:50:43 +000010814 const unsigned char N86Reg = TRI->getEncodingValue(NestReg) & 0x7;
Scott Michelfdc40a02009-02-17 22:15:04 +000010815 OutChains[0] = DAG.getStore(Root, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +000010816 DAG.getConstant(MOV32ri|N86Reg, MVT::i8),
Chris Lattner8026a9d2010-09-21 17:50:43 +000010817 Trmp, MachinePointerInfo(TrmpAddr),
10818 false, false, 0);
Duncan Sandsb116fac2007-07-27 20:02:49 +000010819
Owen Anderson825b72b2009-08-11 20:47:22 +000010820 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
10821 DAG.getConstant(1, MVT::i32));
Chris Lattner8026a9d2010-09-21 17:50:43 +000010822 OutChains[1] = DAG.getStore(Root, dl, Nest, Addr,
10823 MachinePointerInfo(TrmpAddr, 1),
David Greene67c9d422010-02-15 16:53:33 +000010824 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +000010825
Chris Lattnera62fe662010-02-05 19:20:30 +000010826 const unsigned char JMP = 0xE9; // jmp <32bit dst> opcode.
Owen Anderson825b72b2009-08-11 20:47:22 +000010827 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
10828 DAG.getConstant(5, MVT::i32));
10829 OutChains[2] = DAG.getStore(Root, dl, DAG.getConstant(JMP, MVT::i8), Addr,
Chris Lattner8026a9d2010-09-21 17:50:43 +000010830 MachinePointerInfo(TrmpAddr, 5),
10831 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +000010832
Owen Anderson825b72b2009-08-11 20:47:22 +000010833 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
10834 DAG.getConstant(6, MVT::i32));
Chris Lattner8026a9d2010-09-21 17:50:43 +000010835 OutChains[3] = DAG.getStore(Root, dl, Disp, Addr,
10836 MachinePointerInfo(TrmpAddr, 6),
David Greene67c9d422010-02-15 16:53:33 +000010837 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +000010838
Duncan Sands4a544a72011-09-06 13:37:06 +000010839 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains, 4);
Duncan Sandsb116fac2007-07-27 20:02:49 +000010840 }
10841}
10842
Dan Gohmand858e902010-04-17 15:26:15 +000010843SDValue X86TargetLowering::LowerFLT_ROUNDS_(SDValue Op,
10844 SelectionDAG &DAG) const {
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000010845 /*
10846 The rounding mode is in bits 11:10 of FPSR, and has the following
10847 settings:
10848 00 Round to nearest
10849 01 Round to -inf
10850 10 Round to +inf
10851 11 Round to 0
10852
10853 FLT_ROUNDS, on the other hand, expects the following:
10854 -1 Undefined
10855 0 Round to 0
10856 1 Round to nearest
10857 2 Round to +inf
10858 3 Round to -inf
10859
10860 To perform the conversion, we do:
10861 (((((FPSR & 0x800) >> 11) | ((FPSR & 0x400) >> 9)) + 1) & 3)
10862 */
10863
10864 MachineFunction &MF = DAG.getMachineFunction();
10865 const TargetMachine &TM = MF.getTarget();
Anton Korobeynikov16c29b52011-01-10 12:39:04 +000010866 const TargetFrameLowering &TFI = *TM.getFrameLowering();
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000010867 unsigned StackAlignment = TFI.getStackAlignment();
Owen Andersone50ed302009-08-10 22:56:29 +000010868 EVT VT = Op.getValueType();
Chris Lattner2156b792010-09-22 01:11:26 +000010869 DebugLoc DL = Op.getDebugLoc();
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000010870
10871 // Save FP Control Word to stack slot
David Greene3f2bf852009-11-12 20:49:22 +000010872 int SSFI = MF.getFrameInfo()->CreateStackObject(2, StackAlignment, false);
Dan Gohman475871a2008-07-27 21:46:04 +000010873 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000010874
Chris Lattner2156b792010-09-22 01:11:26 +000010875 MachineMemOperand *MMO =
10876 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
10877 MachineMemOperand::MOStore, 2, 2);
Michael J. Spencerec38de22010-10-10 22:04:20 +000010878
Chris Lattner2156b792010-09-22 01:11:26 +000010879 SDValue Ops[] = { DAG.getEntryNode(), StackSlot };
10880 SDValue Chain = DAG.getMemIntrinsicNode(X86ISD::FNSTCW16m, DL,
10881 DAG.getVTList(MVT::Other),
10882 Ops, 2, MVT::i16, MMO);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000010883
10884 // Load FP Control Word from stack slot
Chris Lattner2156b792010-09-22 01:11:26 +000010885 SDValue CWD = DAG.getLoad(MVT::i16, DL, Chain, StackSlot,
Pete Cooperd752e0f2011-11-08 18:42:53 +000010886 MachinePointerInfo(), false, false, false, 0);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000010887
10888 // Transform as necessary
Dan Gohman475871a2008-07-27 21:46:04 +000010889 SDValue CWD1 =
Chris Lattner2156b792010-09-22 01:11:26 +000010890 DAG.getNode(ISD::SRL, DL, MVT::i16,
10891 DAG.getNode(ISD::AND, DL, MVT::i16,
Owen Anderson825b72b2009-08-11 20:47:22 +000010892 CWD, DAG.getConstant(0x800, MVT::i16)),
10893 DAG.getConstant(11, MVT::i8));
Dan Gohman475871a2008-07-27 21:46:04 +000010894 SDValue CWD2 =
Chris Lattner2156b792010-09-22 01:11:26 +000010895 DAG.getNode(ISD::SRL, DL, MVT::i16,
10896 DAG.getNode(ISD::AND, DL, MVT::i16,
Owen Anderson825b72b2009-08-11 20:47:22 +000010897 CWD, DAG.getConstant(0x400, MVT::i16)),
10898 DAG.getConstant(9, MVT::i8));
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000010899
Dan Gohman475871a2008-07-27 21:46:04 +000010900 SDValue RetVal =
Chris Lattner2156b792010-09-22 01:11:26 +000010901 DAG.getNode(ISD::AND, DL, MVT::i16,
10902 DAG.getNode(ISD::ADD, DL, MVT::i16,
10903 DAG.getNode(ISD::OR, DL, MVT::i16, CWD1, CWD2),
Owen Anderson825b72b2009-08-11 20:47:22 +000010904 DAG.getConstant(1, MVT::i16)),
10905 DAG.getConstant(3, MVT::i16));
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000010906
Duncan Sands83ec4b62008-06-06 12:08:01 +000010907 return DAG.getNode((VT.getSizeInBits() < 16 ?
Chris Lattner2156b792010-09-22 01:11:26 +000010908 ISD::TRUNCATE : ISD::ZERO_EXTEND), DL, VT, RetVal);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000010909}
10910
Craig Topper55b24052012-09-11 06:15:32 +000010911static SDValue LowerCTLZ(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +000010912 EVT VT = Op.getValueType();
10913 EVT OpVT = VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +000010914 unsigned NumBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +000010915 DebugLoc dl = Op.getDebugLoc();
Evan Cheng18efe262007-12-14 02:13:44 +000010916
10917 Op = Op.getOperand(0);
Owen Anderson825b72b2009-08-11 20:47:22 +000010918 if (VT == MVT::i8) {
Evan Cheng152804e2007-12-14 08:30:15 +000010919 // Zero extend to i32 since there is not an i8 bsr.
Owen Anderson825b72b2009-08-11 20:47:22 +000010920 OpVT = MVT::i32;
Dale Johannesene4d209d2009-02-03 20:21:25 +000010921 Op = DAG.getNode(ISD::ZERO_EXTEND, dl, OpVT, Op);
Evan Cheng18efe262007-12-14 02:13:44 +000010922 }
Evan Cheng18efe262007-12-14 02:13:44 +000010923
Evan Cheng152804e2007-12-14 08:30:15 +000010924 // Issue a bsr (scan bits in reverse) which also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +000010925 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +000010926 Op = DAG.getNode(X86ISD::BSR, dl, VTs, Op);
Evan Cheng152804e2007-12-14 08:30:15 +000010927
10928 // If src is zero (i.e. bsr sets ZF), returns NumBits.
Benjamin Kramer7f1a5602009-12-29 16:57:26 +000010929 SDValue Ops[] = {
10930 Op,
10931 DAG.getConstant(NumBits+NumBits-1, OpVT),
10932 DAG.getConstant(X86::COND_E, MVT::i8),
10933 Op.getValue(1)
10934 };
10935 Op = DAG.getNode(X86ISD::CMOV, dl, OpVT, Ops, array_lengthof(Ops));
Evan Cheng152804e2007-12-14 08:30:15 +000010936
10937 // Finally xor with NumBits-1.
Dale Johannesene4d209d2009-02-03 20:21:25 +000010938 Op = DAG.getNode(ISD::XOR, dl, OpVT, Op, DAG.getConstant(NumBits-1, OpVT));
Evan Cheng152804e2007-12-14 08:30:15 +000010939
Owen Anderson825b72b2009-08-11 20:47:22 +000010940 if (VT == MVT::i8)
10941 Op = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Op);
Evan Cheng18efe262007-12-14 02:13:44 +000010942 return Op;
10943}
10944
Craig Topper55b24052012-09-11 06:15:32 +000010945static SDValue LowerCTLZ_ZERO_UNDEF(SDValue Op, SelectionDAG &DAG) {
Chandler Carruthacc068e2011-12-24 10:55:54 +000010946 EVT VT = Op.getValueType();
10947 EVT OpVT = VT;
10948 unsigned NumBits = VT.getSizeInBits();
10949 DebugLoc dl = Op.getDebugLoc();
10950
10951 Op = Op.getOperand(0);
10952 if (VT == MVT::i8) {
10953 // Zero extend to i32 since there is not an i8 bsr.
10954 OpVT = MVT::i32;
10955 Op = DAG.getNode(ISD::ZERO_EXTEND, dl, OpVT, Op);
10956 }
10957
10958 // Issue a bsr (scan bits in reverse).
10959 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
10960 Op = DAG.getNode(X86ISD::BSR, dl, VTs, Op);
10961
10962 // And xor with NumBits-1.
10963 Op = DAG.getNode(ISD::XOR, dl, OpVT, Op, DAG.getConstant(NumBits-1, OpVT));
10964
10965 if (VT == MVT::i8)
10966 Op = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Op);
10967 return Op;
10968}
10969
Craig Topper55b24052012-09-11 06:15:32 +000010970static SDValue LowerCTTZ(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +000010971 EVT VT = Op.getValueType();
Duncan Sands83ec4b62008-06-06 12:08:01 +000010972 unsigned NumBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +000010973 DebugLoc dl = Op.getDebugLoc();
Evan Cheng18efe262007-12-14 02:13:44 +000010974 Op = Op.getOperand(0);
Evan Cheng152804e2007-12-14 08:30:15 +000010975
10976 // Issue a bsf (scan bits forward) which also sets EFLAGS.
Chandler Carruth77821022011-12-24 12:12:34 +000010977 SDVTList VTs = DAG.getVTList(VT, MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +000010978 Op = DAG.getNode(X86ISD::BSF, dl, VTs, Op);
Evan Cheng152804e2007-12-14 08:30:15 +000010979
10980 // If src is zero (i.e. bsf sets ZF), returns NumBits.
Benjamin Kramer7f1a5602009-12-29 16:57:26 +000010981 SDValue Ops[] = {
10982 Op,
Chandler Carruth77821022011-12-24 12:12:34 +000010983 DAG.getConstant(NumBits, VT),
Benjamin Kramer7f1a5602009-12-29 16:57:26 +000010984 DAG.getConstant(X86::COND_E, MVT::i8),
10985 Op.getValue(1)
10986 };
Chandler Carruth77821022011-12-24 12:12:34 +000010987 return DAG.getNode(X86ISD::CMOV, dl, VT, Ops, array_lengthof(Ops));
Evan Cheng18efe262007-12-14 02:13:44 +000010988}
10989
Craig Topper13894fa2011-08-24 06:14:18 +000010990// Lower256IntArith - Break a 256-bit integer operation into two new 128-bit
10991// ones, and then concatenate the result back.
10992static SDValue Lower256IntArith(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +000010993 EVT VT = Op.getValueType();
Craig Topper13894fa2011-08-24 06:14:18 +000010994
Craig Topper7a9a28b2012-08-12 02:23:29 +000010995 assert(VT.is256BitVector() && VT.isInteger() &&
Craig Topper13894fa2011-08-24 06:14:18 +000010996 "Unsupported value type for operation");
10997
Craig Topper66ddd152012-04-27 22:54:43 +000010998 unsigned NumElems = VT.getVectorNumElements();
Craig Topper13894fa2011-08-24 06:14:18 +000010999 DebugLoc dl = Op.getDebugLoc();
Craig Topper13894fa2011-08-24 06:14:18 +000011000
11001 // Extract the LHS vectors
11002 SDValue LHS = Op.getOperand(0);
Craig Topperb14940a2012-04-22 20:55:18 +000011003 SDValue LHS1 = Extract128BitVector(LHS, 0, DAG, dl);
11004 SDValue LHS2 = Extract128BitVector(LHS, NumElems/2, DAG, dl);
Craig Topper13894fa2011-08-24 06:14:18 +000011005
11006 // Extract the RHS vectors
11007 SDValue RHS = Op.getOperand(1);
Craig Topperb14940a2012-04-22 20:55:18 +000011008 SDValue RHS1 = Extract128BitVector(RHS, 0, DAG, dl);
11009 SDValue RHS2 = Extract128BitVector(RHS, NumElems/2, DAG, dl);
Craig Topper13894fa2011-08-24 06:14:18 +000011010
11011 MVT EltVT = VT.getVectorElementType().getSimpleVT();
11012 EVT NewVT = MVT::getVectorVT(EltVT, NumElems/2);
11013
11014 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT,
11015 DAG.getNode(Op.getOpcode(), dl, NewVT, LHS1, RHS1),
11016 DAG.getNode(Op.getOpcode(), dl, NewVT, LHS2, RHS2));
11017}
11018
Craig Topper55b24052012-09-11 06:15:32 +000011019static SDValue LowerADD(SDValue Op, SelectionDAG &DAG) {
Craig Topper7a9a28b2012-08-12 02:23:29 +000011020 assert(Op.getValueType().is256BitVector() &&
Craig Topper13894fa2011-08-24 06:14:18 +000011021 Op.getValueType().isInteger() &&
11022 "Only handle AVX 256-bit vector integer operation");
11023 return Lower256IntArith(Op, DAG);
11024}
11025
Craig Topper55b24052012-09-11 06:15:32 +000011026static SDValue LowerSUB(SDValue Op, SelectionDAG &DAG) {
Craig Topper7a9a28b2012-08-12 02:23:29 +000011027 assert(Op.getValueType().is256BitVector() &&
Craig Topper13894fa2011-08-24 06:14:18 +000011028 Op.getValueType().isInteger() &&
11029 "Only handle AVX 256-bit vector integer operation");
11030 return Lower256IntArith(Op, DAG);
11031}
11032
Craig Topper55b24052012-09-11 06:15:32 +000011033static SDValue LowerMUL(SDValue Op, const X86Subtarget *Subtarget,
11034 SelectionDAG &DAG) {
Benjamin Kramer2f8a6cd2012-12-22 16:07:56 +000011035 DebugLoc dl = Op.getDebugLoc();
Craig Topper13894fa2011-08-24 06:14:18 +000011036 EVT VT = Op.getValueType();
11037
11038 // Decompose 256-bit ops into smaller 128-bit ops.
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000011039 if (VT.is256BitVector() && !Subtarget->hasInt256())
Craig Topper13894fa2011-08-24 06:14:18 +000011040 return Lower256IntArith(Op, DAG);
11041
Benjamin Kramer2f8a6cd2012-12-22 16:07:56 +000011042 SDValue A = Op.getOperand(0);
11043 SDValue B = Op.getOperand(1);
11044
11045 // Lower v4i32 mul as 2x shuffle, 2x pmuludq, 2x shuffle.
11046 if (VT == MVT::v4i32) {
11047 assert(Subtarget->hasSSE2() && !Subtarget->hasSSE41() &&
11048 "Should not custom lower when pmuldq is available!");
11049
11050 // Extract the odd parts.
11051 const int UnpackMask[] = { 1, -1, 3, -1 };
11052 SDValue Aodds = DAG.getVectorShuffle(VT, dl, A, A, UnpackMask);
11053 SDValue Bodds = DAG.getVectorShuffle(VT, dl, B, B, UnpackMask);
11054
11055 // Multiply the even parts.
11056 SDValue Evens = DAG.getNode(X86ISD::PMULUDQ, dl, MVT::v2i64, A, B);
11057 // Now multiply odd parts.
11058 SDValue Odds = DAG.getNode(X86ISD::PMULUDQ, dl, MVT::v2i64, Aodds, Bodds);
11059
11060 Evens = DAG.getNode(ISD::BITCAST, dl, VT, Evens);
11061 Odds = DAG.getNode(ISD::BITCAST, dl, VT, Odds);
11062
11063 // Merge the two vectors back together with a shuffle. This expands into 2
11064 // shuffles.
11065 const int ShufMask[] = { 0, 4, 2, 6 };
11066 return DAG.getVectorShuffle(VT, dl, Evens, Odds, ShufMask);
11067 }
11068
Craig Topper5b209e82012-02-05 03:14:49 +000011069 assert((VT == MVT::v2i64 || VT == MVT::v4i64) &&
11070 "Only know how to lower V2I64/V4I64 multiply");
11071
Craig Topper5b209e82012-02-05 03:14:49 +000011072 // Ahi = psrlqi(a, 32);
11073 // Bhi = psrlqi(b, 32);
11074 //
11075 // AloBlo = pmuludq(a, b);
11076 // AloBhi = pmuludq(a, Bhi);
11077 // AhiBlo = pmuludq(Ahi, b);
11078
11079 // AloBhi = psllqi(AloBhi, 32);
11080 // AhiBlo = psllqi(AhiBlo, 32);
11081 // return AloBlo + AloBhi + AhiBlo;
11082
Craig Topper5b209e82012-02-05 03:14:49 +000011083 SDValue ShAmt = DAG.getConstant(32, MVT::i32);
Craig Topperaaa643c2011-11-09 07:28:55 +000011084
Craig Topper5b209e82012-02-05 03:14:49 +000011085 SDValue Ahi = DAG.getNode(X86ISD::VSRLI, dl, VT, A, ShAmt);
11086 SDValue Bhi = DAG.getNode(X86ISD::VSRLI, dl, VT, B, ShAmt);
Craig Topperaaa643c2011-11-09 07:28:55 +000011087
Craig Topper5b209e82012-02-05 03:14:49 +000011088 // Bit cast to 32-bit vectors for MULUDQ
11089 EVT MulVT = (VT == MVT::v2i64) ? MVT::v4i32 : MVT::v8i32;
11090 A = DAG.getNode(ISD::BITCAST, dl, MulVT, A);
11091 B = DAG.getNode(ISD::BITCAST, dl, MulVT, B);
11092 Ahi = DAG.getNode(ISD::BITCAST, dl, MulVT, Ahi);
11093 Bhi = DAG.getNode(ISD::BITCAST, dl, MulVT, Bhi);
Craig Topperaaa643c2011-11-09 07:28:55 +000011094
Craig Topper5b209e82012-02-05 03:14:49 +000011095 SDValue AloBlo = DAG.getNode(X86ISD::PMULUDQ, dl, VT, A, B);
11096 SDValue AloBhi = DAG.getNode(X86ISD::PMULUDQ, dl, VT, A, Bhi);
11097 SDValue AhiBlo = DAG.getNode(X86ISD::PMULUDQ, dl, VT, Ahi, B);
Craig Topperaaa643c2011-11-09 07:28:55 +000011098
Craig Topper5b209e82012-02-05 03:14:49 +000011099 AloBhi = DAG.getNode(X86ISD::VSHLI, dl, VT, AloBhi, ShAmt);
11100 AhiBlo = DAG.getNode(X86ISD::VSHLI, dl, VT, AhiBlo, ShAmt);
Mon P Wangaf9b9522008-12-18 21:42:19 +000011101
Dale Johannesene4d209d2009-02-03 20:21:25 +000011102 SDValue Res = DAG.getNode(ISD::ADD, dl, VT, AloBlo, AloBhi);
Craig Topper5b209e82012-02-05 03:14:49 +000011103 return DAG.getNode(ISD::ADD, dl, VT, Res, AhiBlo);
Mon P Wangaf9b9522008-12-18 21:42:19 +000011104}
11105
Nadav Rotem43012222011-05-11 08:12:09 +000011106SDValue X86TargetLowering::LowerShift(SDValue Op, SelectionDAG &DAG) const {
11107
Nate Begemanbdcb5af2010-07-27 22:37:06 +000011108 EVT VT = Op.getValueType();
11109 DebugLoc dl = Op.getDebugLoc();
11110 SDValue R = Op.getOperand(0);
Nadav Rotem43012222011-05-11 08:12:09 +000011111 SDValue Amt = Op.getOperand(1);
Nate Begemanbdcb5af2010-07-27 22:37:06 +000011112 LLVMContext *Context = DAG.getContext();
Nate Begemanbdcb5af2010-07-27 22:37:06 +000011113
Craig Topper1accb7e2012-01-10 06:54:16 +000011114 if (!Subtarget->hasSSE2())
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +000011115 return SDValue();
11116
Nadav Rotem43012222011-05-11 08:12:09 +000011117 // Optimize shl/srl/sra with constant shift amount.
11118 if (isSplatVector(Amt.getNode())) {
11119 SDValue SclrAmt = Amt->getOperand(0);
11120 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(SclrAmt)) {
11121 uint64_t ShiftAmt = C->getZExtValue();
11122
Craig Toppered2e13d2012-01-22 19:15:14 +000011123 if (VT == MVT::v2i64 || VT == MVT::v4i32 || VT == MVT::v8i16 ||
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000011124 (Subtarget->hasInt256() &&
Craig Toppered2e13d2012-01-22 19:15:14 +000011125 (VT == MVT::v4i64 || VT == MVT::v8i32 || VT == MVT::v16i16))) {
11126 if (Op.getOpcode() == ISD::SHL)
11127 return DAG.getNode(X86ISD::VSHLI, dl, VT, R,
11128 DAG.getConstant(ShiftAmt, MVT::i32));
11129 if (Op.getOpcode() == ISD::SRL)
11130 return DAG.getNode(X86ISD::VSRLI, dl, VT, R,
11131 DAG.getConstant(ShiftAmt, MVT::i32));
11132 if (Op.getOpcode() == ISD::SRA && VT != MVT::v2i64 && VT != MVT::v4i64)
11133 return DAG.getNode(X86ISD::VSRAI, dl, VT, R,
11134 DAG.getConstant(ShiftAmt, MVT::i32));
Benjamin Kramerdade3c12011-10-30 17:31:21 +000011135 }
11136
Craig Toppered2e13d2012-01-22 19:15:14 +000011137 if (VT == MVT::v16i8) {
11138 if (Op.getOpcode() == ISD::SHL) {
11139 // Make a large shift.
11140 SDValue SHL = DAG.getNode(X86ISD::VSHLI, dl, MVT::v8i16, R,
11141 DAG.getConstant(ShiftAmt, MVT::i32));
11142 SHL = DAG.getNode(ISD::BITCAST, dl, VT, SHL);
11143 // Zero out the rightmost bits.
11144 SmallVector<SDValue, 16> V(16,
11145 DAG.getConstant(uint8_t(-1U << ShiftAmt),
11146 MVT::i8));
11147 return DAG.getNode(ISD::AND, dl, VT, SHL,
11148 DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &V[0], 16));
Eli Friedmanf6aa6b12011-11-01 21:18:39 +000011149 }
Craig Toppered2e13d2012-01-22 19:15:14 +000011150 if (Op.getOpcode() == ISD::SRL) {
11151 // Make a large shift.
11152 SDValue SRL = DAG.getNode(X86ISD::VSRLI, dl, MVT::v8i16, R,
11153 DAG.getConstant(ShiftAmt, MVT::i32));
11154 SRL = DAG.getNode(ISD::BITCAST, dl, VT, SRL);
11155 // Zero out the leftmost bits.
11156 SmallVector<SDValue, 16> V(16,
11157 DAG.getConstant(uint8_t(-1U) >> ShiftAmt,
11158 MVT::i8));
11159 return DAG.getNode(ISD::AND, dl, VT, SRL,
11160 DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &V[0], 16));
11161 }
11162 if (Op.getOpcode() == ISD::SRA) {
11163 if (ShiftAmt == 7) {
11164 // R s>> 7 === R s< 0
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +000011165 SDValue Zeros = getZeroVector(VT, Subtarget, DAG, dl);
Craig Topper67609fd2012-01-22 22:42:16 +000011166 return DAG.getNode(X86ISD::PCMPGT, dl, VT, Zeros, R);
Craig Toppered2e13d2012-01-22 19:15:14 +000011167 }
Eli Friedmanf6aa6b12011-11-01 21:18:39 +000011168
Craig Toppered2e13d2012-01-22 19:15:14 +000011169 // R s>> a === ((R u>> a) ^ m) - m
11170 SDValue Res = DAG.getNode(ISD::SRL, dl, VT, R, Amt);
11171 SmallVector<SDValue, 16> V(16, DAG.getConstant(128 >> ShiftAmt,
11172 MVT::i8));
11173 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &V[0], 16);
11174 Res = DAG.getNode(ISD::XOR, dl, VT, Res, Mask);
11175 Res = DAG.getNode(ISD::SUB, dl, VT, Res, Mask);
11176 return Res;
11177 }
Craig Topper731dfd02012-04-23 03:42:40 +000011178 llvm_unreachable("Unknown shift opcode.");
Eli Friedmanf6aa6b12011-11-01 21:18:39 +000011179 }
Craig Topper46154eb2011-11-11 07:39:23 +000011180
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000011181 if (Subtarget->hasInt256() && VT == MVT::v32i8) {
Craig Topper0d86d462011-11-20 00:12:05 +000011182 if (Op.getOpcode() == ISD::SHL) {
11183 // Make a large shift.
Craig Toppered2e13d2012-01-22 19:15:14 +000011184 SDValue SHL = DAG.getNode(X86ISD::VSHLI, dl, MVT::v16i16, R,
11185 DAG.getConstant(ShiftAmt, MVT::i32));
11186 SHL = DAG.getNode(ISD::BITCAST, dl, VT, SHL);
Craig Topper0d86d462011-11-20 00:12:05 +000011187 // Zero out the rightmost bits.
Craig Toppered2e13d2012-01-22 19:15:14 +000011188 SmallVector<SDValue, 32> V(32,
11189 DAG.getConstant(uint8_t(-1U << ShiftAmt),
11190 MVT::i8));
Craig Topper0d86d462011-11-20 00:12:05 +000011191 return DAG.getNode(ISD::AND, dl, VT, SHL,
11192 DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &V[0], 32));
Craig Topper46154eb2011-11-11 07:39:23 +000011193 }
Craig Topper0d86d462011-11-20 00:12:05 +000011194 if (Op.getOpcode() == ISD::SRL) {
11195 // Make a large shift.
Craig Toppered2e13d2012-01-22 19:15:14 +000011196 SDValue SRL = DAG.getNode(X86ISD::VSRLI, dl, MVT::v16i16, R,
11197 DAG.getConstant(ShiftAmt, MVT::i32));
11198 SRL = DAG.getNode(ISD::BITCAST, dl, VT, SRL);
Craig Topper0d86d462011-11-20 00:12:05 +000011199 // Zero out the leftmost bits.
Craig Toppered2e13d2012-01-22 19:15:14 +000011200 SmallVector<SDValue, 32> V(32,
11201 DAG.getConstant(uint8_t(-1U) >> ShiftAmt,
11202 MVT::i8));
Craig Topper0d86d462011-11-20 00:12:05 +000011203 return DAG.getNode(ISD::AND, dl, VT, SRL,
11204 DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &V[0], 32));
11205 }
11206 if (Op.getOpcode() == ISD::SRA) {
11207 if (ShiftAmt == 7) {
11208 // R s>> 7 === R s< 0
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +000011209 SDValue Zeros = getZeroVector(VT, Subtarget, DAG, dl);
Craig Topper67609fd2012-01-22 22:42:16 +000011210 return DAG.getNode(X86ISD::PCMPGT, dl, VT, Zeros, R);
Craig Topper0d86d462011-11-20 00:12:05 +000011211 }
11212
11213 // R s>> a === ((R u>> a) ^ m) - m
11214 SDValue Res = DAG.getNode(ISD::SRL, dl, VT, R, Amt);
11215 SmallVector<SDValue, 32> V(32, DAG.getConstant(128 >> ShiftAmt,
11216 MVT::i8));
11217 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &V[0], 32);
11218 Res = DAG.getNode(ISD::XOR, dl, VT, Res, Mask);
11219 Res = DAG.getNode(ISD::SUB, dl, VT, Res, Mask);
11220 return Res;
11221 }
Craig Topper731dfd02012-04-23 03:42:40 +000011222 llvm_unreachable("Unknown shift opcode.");
Craig Topper0d86d462011-11-20 00:12:05 +000011223 }
Nadav Rotem43012222011-05-11 08:12:09 +000011224 }
11225 }
11226
11227 // Lower SHL with variable shift amount.
Nadav Rotem43012222011-05-11 08:12:09 +000011228 if (VT == MVT::v4i32 && Op->getOpcode() == ISD::SHL) {
Craig Toppered2e13d2012-01-22 19:15:14 +000011229 Op = DAG.getNode(X86ISD::VSHLI, dl, VT, Op.getOperand(1),
11230 DAG.getConstant(23, MVT::i32));
Nate Begeman51409212010-07-28 00:21:48 +000011231
Chris Lattner7302d802012-02-06 21:56:39 +000011232 const uint32_t CV[] = { 0x3f800000U, 0x3f800000U, 0x3f800000U, 0x3f800000U};
11233 Constant *C = ConstantDataVector::get(*Context, CV);
Nate Begeman51409212010-07-28 00:21:48 +000011234 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
11235 SDValue Addend = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +000011236 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000011237 false, false, false, 16);
Nate Begeman51409212010-07-28 00:21:48 +000011238
11239 Op = DAG.getNode(ISD::ADD, dl, VT, Op, Addend);
Wesley Peckbf17cfa2010-11-23 03:31:01 +000011240 Op = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, Op);
Nate Begeman51409212010-07-28 00:21:48 +000011241 Op = DAG.getNode(ISD::FP_TO_SINT, dl, VT, Op);
11242 return DAG.getNode(ISD::MUL, dl, VT, Op, R);
11243 }
Nadav Rotem43012222011-05-11 08:12:09 +000011244 if (VT == MVT::v16i8 && Op->getOpcode() == ISD::SHL) {
Craig Topper8b5a6b62012-01-17 08:23:44 +000011245 assert(Subtarget->hasSSE2() && "Need SSE2 for pslli/pcmpeq.");
Lang Hames8b99c1e2011-12-17 01:08:46 +000011246
Nate Begeman51409212010-07-28 00:21:48 +000011247 // a = a << 5;
Craig Toppered2e13d2012-01-22 19:15:14 +000011248 Op = DAG.getNode(X86ISD::VSHLI, dl, MVT::v8i16, Op.getOperand(1),
11249 DAG.getConstant(5, MVT::i32));
11250 Op = DAG.getNode(ISD::BITCAST, dl, VT, Op);
Nate Begeman51409212010-07-28 00:21:48 +000011251
Lang Hames8b99c1e2011-12-17 01:08:46 +000011252 // Turn 'a' into a mask suitable for VSELECT
11253 SDValue VSelM = DAG.getConstant(0x80, VT);
11254 SDValue OpVSel = DAG.getNode(ISD::AND, dl, VT, VSelM, Op);
Craig Topper67609fd2012-01-22 22:42:16 +000011255 OpVSel = DAG.getNode(X86ISD::PCMPEQ, dl, VT, OpVSel, VSelM);
Nate Begeman51409212010-07-28 00:21:48 +000011256
Lang Hames8b99c1e2011-12-17 01:08:46 +000011257 SDValue CM1 = DAG.getConstant(0x0f, VT);
11258 SDValue CM2 = DAG.getConstant(0x3f, VT);
Nate Begeman51409212010-07-28 00:21:48 +000011259
Lang Hames8b99c1e2011-12-17 01:08:46 +000011260 // r = VSELECT(r, psllw(r & (char16)15, 4), a);
11261 SDValue M = DAG.getNode(ISD::AND, dl, VT, R, CM1);
Craig Toppered2e13d2012-01-22 19:15:14 +000011262 M = getTargetVShiftNode(X86ISD::VSHLI, dl, MVT::v8i16, M,
11263 DAG.getConstant(4, MVT::i32), DAG);
11264 M = DAG.getNode(ISD::BITCAST, dl, VT, M);
Lang Hames8b99c1e2011-12-17 01:08:46 +000011265 R = DAG.getNode(ISD::VSELECT, dl, VT, OpVSel, M, R);
11266
Nate Begeman51409212010-07-28 00:21:48 +000011267 // a += a
11268 Op = DAG.getNode(ISD::ADD, dl, VT, Op, Op);
Lang Hames8b99c1e2011-12-17 01:08:46 +000011269 OpVSel = DAG.getNode(ISD::AND, dl, VT, VSelM, Op);
Craig Topper67609fd2012-01-22 22:42:16 +000011270 OpVSel = DAG.getNode(X86ISD::PCMPEQ, dl, VT, OpVSel, VSelM);
Michael J. Spencerec38de22010-10-10 22:04:20 +000011271
Lang Hames8b99c1e2011-12-17 01:08:46 +000011272 // r = VSELECT(r, psllw(r & (char16)63, 2), a);
11273 M = DAG.getNode(ISD::AND, dl, VT, R, CM2);
Craig Toppered2e13d2012-01-22 19:15:14 +000011274 M = getTargetVShiftNode(X86ISD::VSHLI, dl, MVT::v8i16, M,
11275 DAG.getConstant(2, MVT::i32), DAG);
11276 M = DAG.getNode(ISD::BITCAST, dl, VT, M);
Lang Hames8b99c1e2011-12-17 01:08:46 +000011277 R = DAG.getNode(ISD::VSELECT, dl, VT, OpVSel, M, R);
11278
Nate Begeman51409212010-07-28 00:21:48 +000011279 // a += a
11280 Op = DAG.getNode(ISD::ADD, dl, VT, Op, Op);
Lang Hames8b99c1e2011-12-17 01:08:46 +000011281 OpVSel = DAG.getNode(ISD::AND, dl, VT, VSelM, Op);
Craig Topper67609fd2012-01-22 22:42:16 +000011282 OpVSel = DAG.getNode(X86ISD::PCMPEQ, dl, VT, OpVSel, VSelM);
Michael J. Spencerec38de22010-10-10 22:04:20 +000011283
Lang Hames8b99c1e2011-12-17 01:08:46 +000011284 // return VSELECT(r, r+r, a);
11285 R = DAG.getNode(ISD::VSELECT, dl, VT, OpVSel,
Lang Hamesa0a25132011-12-15 18:57:27 +000011286 DAG.getNode(ISD::ADD, dl, VT, R, R), R);
Nate Begeman51409212010-07-28 00:21:48 +000011287 return R;
11288 }
Craig Topper46154eb2011-11-11 07:39:23 +000011289
11290 // Decompose 256-bit shifts into smaller 128-bit shifts.
Craig Topper7a9a28b2012-08-12 02:23:29 +000011291 if (VT.is256BitVector()) {
Craig Toppered2e13d2012-01-22 19:15:14 +000011292 unsigned NumElems = VT.getVectorNumElements();
Craig Topper46154eb2011-11-11 07:39:23 +000011293 MVT EltVT = VT.getVectorElementType().getSimpleVT();
11294 EVT NewVT = MVT::getVectorVT(EltVT, NumElems/2);
11295
11296 // Extract the two vectors
Craig Topperb14940a2012-04-22 20:55:18 +000011297 SDValue V1 = Extract128BitVector(R, 0, DAG, dl);
11298 SDValue V2 = Extract128BitVector(R, NumElems/2, DAG, dl);
Craig Topper46154eb2011-11-11 07:39:23 +000011299
11300 // Recreate the shift amount vectors
11301 SDValue Amt1, Amt2;
11302 if (Amt.getOpcode() == ISD::BUILD_VECTOR) {
11303 // Constant shift amount
11304 SmallVector<SDValue, 4> Amt1Csts;
11305 SmallVector<SDValue, 4> Amt2Csts;
Craig Toppered2e13d2012-01-22 19:15:14 +000011306 for (unsigned i = 0; i != NumElems/2; ++i)
Craig Topper46154eb2011-11-11 07:39:23 +000011307 Amt1Csts.push_back(Amt->getOperand(i));
Craig Toppered2e13d2012-01-22 19:15:14 +000011308 for (unsigned i = NumElems/2; i != NumElems; ++i)
Craig Topper46154eb2011-11-11 07:39:23 +000011309 Amt2Csts.push_back(Amt->getOperand(i));
11310
11311 Amt1 = DAG.getNode(ISD::BUILD_VECTOR, dl, NewVT,
11312 &Amt1Csts[0], NumElems/2);
11313 Amt2 = DAG.getNode(ISD::BUILD_VECTOR, dl, NewVT,
11314 &Amt2Csts[0], NumElems/2);
11315 } else {
11316 // Variable shift amount
Craig Topperb14940a2012-04-22 20:55:18 +000011317 Amt1 = Extract128BitVector(Amt, 0, DAG, dl);
11318 Amt2 = Extract128BitVector(Amt, NumElems/2, DAG, dl);
Craig Topper46154eb2011-11-11 07:39:23 +000011319 }
11320
11321 // Issue new vector shifts for the smaller types
11322 V1 = DAG.getNode(Op.getOpcode(), dl, NewVT, V1, Amt1);
11323 V2 = DAG.getNode(Op.getOpcode(), dl, NewVT, V2, Amt2);
11324
11325 // Concatenate the result back
11326 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT, V1, V2);
11327 }
11328
Nate Begeman51409212010-07-28 00:21:48 +000011329 return SDValue();
Nate Begemanbdcb5af2010-07-27 22:37:06 +000011330}
Mon P Wangaf9b9522008-12-18 21:42:19 +000011331
Craig Topper55b24052012-09-11 06:15:32 +000011332static SDValue LowerXALUO(SDValue Op, SelectionDAG &DAG) {
Bill Wendling74c37652008-12-09 22:08:41 +000011333 // Lower the "add/sub/mul with overflow" instruction into a regular ins plus
11334 // a "setcc" instruction that checks the overflow flag. The "brcond" lowering
Bill Wendling61edeb52008-12-02 01:06:39 +000011335 // looks for this combo and may remove the "setcc" instruction if the "setcc"
11336 // has only one use.
Bill Wendling3fafd932008-11-26 22:37:40 +000011337 SDNode *N = Op.getNode();
Bill Wendling61edeb52008-12-02 01:06:39 +000011338 SDValue LHS = N->getOperand(0);
11339 SDValue RHS = N->getOperand(1);
Bill Wendling74c37652008-12-09 22:08:41 +000011340 unsigned BaseOp = 0;
11341 unsigned Cond = 0;
Chris Lattnerb20e0b12010-12-05 07:30:36 +000011342 DebugLoc DL = Op.getDebugLoc();
Bill Wendling74c37652008-12-09 22:08:41 +000011343 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +000011344 default: llvm_unreachable("Unknown ovf instruction!");
Bill Wendling74c37652008-12-09 22:08:41 +000011345 case ISD::SADDO:
Dan Gohman076aee32009-03-04 19:44:21 +000011346 // A subtract of one will be selected as a INC. Note that INC doesn't
11347 // set CF, so we can't do this for UADDO.
Benjamin Kramerc175a4b2011-03-08 15:20:20 +000011348 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(RHS))
11349 if (C->isOne()) {
Dan Gohman076aee32009-03-04 19:44:21 +000011350 BaseOp = X86ISD::INC;
11351 Cond = X86::COND_O;
11352 break;
11353 }
Bill Wendlingab55ebd2008-12-12 00:56:36 +000011354 BaseOp = X86ISD::ADD;
Bill Wendling74c37652008-12-09 22:08:41 +000011355 Cond = X86::COND_O;
11356 break;
11357 case ISD::UADDO:
Bill Wendlingab55ebd2008-12-12 00:56:36 +000011358 BaseOp = X86ISD::ADD;
Dan Gohman653456c2009-01-07 00:15:08 +000011359 Cond = X86::COND_B;
Bill Wendling74c37652008-12-09 22:08:41 +000011360 break;
11361 case ISD::SSUBO:
Dan Gohman076aee32009-03-04 19:44:21 +000011362 // A subtract of one will be selected as a DEC. Note that DEC doesn't
11363 // set CF, so we can't do this for USUBO.
Benjamin Kramerc175a4b2011-03-08 15:20:20 +000011364 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(RHS))
11365 if (C->isOne()) {
Dan Gohman076aee32009-03-04 19:44:21 +000011366 BaseOp = X86ISD::DEC;
11367 Cond = X86::COND_O;
11368 break;
11369 }
Bill Wendlingab55ebd2008-12-12 00:56:36 +000011370 BaseOp = X86ISD::SUB;
Bill Wendling74c37652008-12-09 22:08:41 +000011371 Cond = X86::COND_O;
11372 break;
11373 case ISD::USUBO:
Bill Wendlingab55ebd2008-12-12 00:56:36 +000011374 BaseOp = X86ISD::SUB;
Dan Gohman653456c2009-01-07 00:15:08 +000011375 Cond = X86::COND_B;
Bill Wendling74c37652008-12-09 22:08:41 +000011376 break;
11377 case ISD::SMULO:
Bill Wendlingd350e022008-12-12 21:15:41 +000011378 BaseOp = X86ISD::SMUL;
Bill Wendling74c37652008-12-09 22:08:41 +000011379 Cond = X86::COND_O;
11380 break;
Chris Lattnerb20e0b12010-12-05 07:30:36 +000011381 case ISD::UMULO: { // i64, i8 = umulo lhs, rhs --> i64, i64, i32 umul lhs,rhs
11382 SDVTList VTs = DAG.getVTList(N->getValueType(0), N->getValueType(0),
11383 MVT::i32);
11384 SDValue Sum = DAG.getNode(X86ISD::UMUL, DL, VTs, LHS, RHS);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011385
Chris Lattnerb20e0b12010-12-05 07:30:36 +000011386 SDValue SetCC =
11387 DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
11388 DAG.getConstant(X86::COND_O, MVT::i32),
11389 SDValue(Sum.getNode(), 2));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011390
Dan Gohman6e5fda22011-07-22 18:45:15 +000011391 return DAG.getNode(ISD::MERGE_VALUES, DL, N->getVTList(), Sum, SetCC);
Chris Lattnerb20e0b12010-12-05 07:30:36 +000011392 }
Bill Wendling74c37652008-12-09 22:08:41 +000011393 }
Bill Wendling3fafd932008-11-26 22:37:40 +000011394
Bill Wendling61edeb52008-12-02 01:06:39 +000011395 // Also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +000011396 SDVTList VTs = DAG.getVTList(N->getValueType(0), MVT::i32);
Chris Lattnerb20e0b12010-12-05 07:30:36 +000011397 SDValue Sum = DAG.getNode(BaseOp, DL, VTs, LHS, RHS);
Bill Wendling3fafd932008-11-26 22:37:40 +000011398
Bill Wendling61edeb52008-12-02 01:06:39 +000011399 SDValue SetCC =
Chris Lattnerb20e0b12010-12-05 07:30:36 +000011400 DAG.getNode(X86ISD::SETCC, DL, N->getValueType(1),
11401 DAG.getConstant(Cond, MVT::i32),
11402 SDValue(Sum.getNode(), 1));
Bill Wendling3fafd932008-11-26 22:37:40 +000011403
Dan Gohman6e5fda22011-07-22 18:45:15 +000011404 return DAG.getNode(ISD::MERGE_VALUES, DL, N->getVTList(), Sum, SetCC);
Bill Wendling41ea7e72008-11-24 19:21:46 +000011405}
11406
Chad Rosier30450e82011-12-22 22:35:21 +000011407SDValue X86TargetLowering::LowerSIGN_EXTEND_INREG(SDValue Op,
11408 SelectionDAG &DAG) const {
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000011409 DebugLoc dl = Op.getDebugLoc();
Craig Toppera124f942011-11-21 01:12:36 +000011410 EVT ExtraVT = cast<VTSDNode>(Op.getOperand(1))->getVT();
11411 EVT VT = Op.getValueType();
11412
Craig Toppered2e13d2012-01-22 19:15:14 +000011413 if (!Subtarget->hasSSE2() || !VT.isVector())
11414 return SDValue();
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000011415
Craig Toppered2e13d2012-01-22 19:15:14 +000011416 unsigned BitsDiff = VT.getScalarType().getSizeInBits() -
11417 ExtraVT.getScalarType().getSizeInBits();
11418 SDValue ShAmt = DAG.getConstant(BitsDiff, MVT::i32);
11419
11420 switch (VT.getSimpleVT().SimpleTy) {
11421 default: return SDValue();
11422 case MVT::v8i32:
11423 case MVT::v16i16:
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000011424 if (!Subtarget->hasFp256())
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000011425 return SDValue();
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000011426 if (!Subtarget->hasInt256()) {
Craig Toppered2e13d2012-01-22 19:15:14 +000011427 // needs to be split
Craig Topper66ddd152012-04-27 22:54:43 +000011428 unsigned NumElems = VT.getVectorNumElements();
Craig Toppera124f942011-11-21 01:12:36 +000011429
Craig Toppered2e13d2012-01-22 19:15:14 +000011430 // Extract the LHS vectors
11431 SDValue LHS = Op.getOperand(0);
Craig Topperb14940a2012-04-22 20:55:18 +000011432 SDValue LHS1 = Extract128BitVector(LHS, 0, DAG, dl);
11433 SDValue LHS2 = Extract128BitVector(LHS, NumElems/2, DAG, dl);
Craig Toppera124f942011-11-21 01:12:36 +000011434
Craig Toppered2e13d2012-01-22 19:15:14 +000011435 MVT EltVT = VT.getVectorElementType().getSimpleVT();
11436 EVT NewVT = MVT::getVectorVT(EltVT, NumElems/2);
Craig Toppera124f942011-11-21 01:12:36 +000011437
Craig Toppered2e13d2012-01-22 19:15:14 +000011438 EVT ExtraEltVT = ExtraVT.getVectorElementType();
Craig Topperb6072642012-05-03 07:26:59 +000011439 unsigned ExtraNumElems = ExtraVT.getVectorNumElements();
Craig Toppered2e13d2012-01-22 19:15:14 +000011440 ExtraVT = EVT::getVectorVT(*DAG.getContext(), ExtraEltVT,
11441 ExtraNumElems/2);
11442 SDValue Extra = DAG.getValueType(ExtraVT);
Craig Toppera124f942011-11-21 01:12:36 +000011443
Craig Toppered2e13d2012-01-22 19:15:14 +000011444 LHS1 = DAG.getNode(Op.getOpcode(), dl, NewVT, LHS1, Extra);
11445 LHS2 = DAG.getNode(Op.getOpcode(), dl, NewVT, LHS2, Extra);
Craig Toppera124f942011-11-21 01:12:36 +000011446
Dmitri Gribenko2de05722012-09-10 21:26:47 +000011447 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT, LHS1, LHS2);
Craig Toppered2e13d2012-01-22 19:15:14 +000011448 }
11449 // fall through
11450 case MVT::v4i32:
11451 case MVT::v8i16: {
11452 SDValue Tmp1 = getTargetVShiftNode(X86ISD::VSHLI, dl, VT,
11453 Op.getOperand(0), ShAmt, DAG);
11454 return getTargetVShiftNode(X86ISD::VSRAI, dl, VT, Tmp1, ShAmt, DAG);
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000011455 }
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000011456 }
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000011457}
11458
Craig Topper55b24052012-09-11 06:15:32 +000011459static SDValue LowerMEMBARRIER(SDValue Op, const X86Subtarget *Subtarget,
11460 SelectionDAG &DAG) {
Eric Christopher9a9d2752010-07-22 02:48:34 +000011461 DebugLoc dl = Op.getDebugLoc();
Michael J. Spencerec38de22010-10-10 22:04:20 +000011462
Eric Christopher77ed1352011-07-08 00:04:56 +000011463 // Go ahead and emit the fence on x86-64 even if we asked for no-sse2.
11464 // There isn't any reason to disable it if the target processor supports it.
Craig Topper1accb7e2012-01-10 06:54:16 +000011465 if (!Subtarget->hasSSE2() && !Subtarget->is64Bit()) {
Eric Christopherc0b2a202010-08-14 21:51:50 +000011466 SDValue Chain = Op.getOperand(0);
Eric Christopher77ed1352011-07-08 00:04:56 +000011467 SDValue Zero = DAG.getConstant(0, MVT::i32);
Eric Christopherc0b2a202010-08-14 21:51:50 +000011468 SDValue Ops[] = {
11469 DAG.getRegister(X86::ESP, MVT::i32), // Base
11470 DAG.getTargetConstant(1, MVT::i8), // Scale
11471 DAG.getRegister(0, MVT::i32), // Index
11472 DAG.getTargetConstant(0, MVT::i32), // Disp
11473 DAG.getRegister(0, MVT::i32), // Segment.
11474 Zero,
11475 Chain
11476 };
Michael J. Spencerec38de22010-10-10 22:04:20 +000011477 SDNode *Res =
Eric Christopherc0b2a202010-08-14 21:51:50 +000011478 DAG.getMachineNode(X86::OR32mrLocked, dl, MVT::Other, Ops,
11479 array_lengthof(Ops));
11480 return SDValue(Res, 0);
Eric Christopherb6729dc2010-08-04 23:03:04 +000011481 }
Michael J. Spencerec38de22010-10-10 22:04:20 +000011482
Eric Christopher9a9d2752010-07-22 02:48:34 +000011483 unsigned isDev = cast<ConstantSDNode>(Op.getOperand(5))->getZExtValue();
Chris Lattner132929a2010-08-14 17:26:09 +000011484 if (!isDev)
Eric Christopher9a9d2752010-07-22 02:48:34 +000011485 return DAG.getNode(X86ISD::MEMBARRIER, dl, MVT::Other, Op.getOperand(0));
Michael J. Spencerec38de22010-10-10 22:04:20 +000011486
Chris Lattner132929a2010-08-14 17:26:09 +000011487 unsigned Op1 = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
11488 unsigned Op2 = cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue();
11489 unsigned Op3 = cast<ConstantSDNode>(Op.getOperand(3))->getZExtValue();
11490 unsigned Op4 = cast<ConstantSDNode>(Op.getOperand(4))->getZExtValue();
Michael J. Spencerec38de22010-10-10 22:04:20 +000011491
Chris Lattner132929a2010-08-14 17:26:09 +000011492 // def : Pat<(membarrier (i8 0), (i8 0), (i8 0), (i8 1), (i8 1)), (SFENCE)>;
11493 if (!Op1 && !Op2 && !Op3 && Op4)
11494 return DAG.getNode(X86ISD::SFENCE, dl, MVT::Other, Op.getOperand(0));
Michael J. Spencerec38de22010-10-10 22:04:20 +000011495
Chris Lattner132929a2010-08-14 17:26:09 +000011496 // def : Pat<(membarrier (i8 1), (i8 0), (i8 0), (i8 0), (i8 1)), (LFENCE)>;
11497 if (Op1 && !Op2 && !Op3 && !Op4)
11498 return DAG.getNode(X86ISD::LFENCE, dl, MVT::Other, Op.getOperand(0));
Michael J. Spencerec38de22010-10-10 22:04:20 +000011499
11500 // def : Pat<(membarrier (i8 imm), (i8 imm), (i8 imm), (i8 imm), (i8 1)),
Chris Lattner132929a2010-08-14 17:26:09 +000011501 // (MFENCE)>;
11502 return DAG.getNode(X86ISD::MFENCE, dl, MVT::Other, Op.getOperand(0));
Eric Christopher9a9d2752010-07-22 02:48:34 +000011503}
11504
Craig Topper55b24052012-09-11 06:15:32 +000011505static SDValue LowerATOMIC_FENCE(SDValue Op, const X86Subtarget *Subtarget,
11506 SelectionDAG &DAG) {
Eli Friedman14648462011-07-27 22:21:52 +000011507 DebugLoc dl = Op.getDebugLoc();
11508 AtomicOrdering FenceOrdering = static_cast<AtomicOrdering>(
11509 cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue());
11510 SynchronizationScope FenceScope = static_cast<SynchronizationScope>(
11511 cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue());
11512
11513 // The only fence that needs an instruction is a sequentially-consistent
11514 // cross-thread fence.
11515 if (FenceOrdering == SequentiallyConsistent && FenceScope == CrossThread) {
11516 // Use mfence if we have SSE2 or we're on x86-64 (even if we asked for
11517 // no-sse2). There isn't any reason to disable it if the target processor
11518 // supports it.
Craig Topper1accb7e2012-01-10 06:54:16 +000011519 if (Subtarget->hasSSE2() || Subtarget->is64Bit())
Eli Friedman14648462011-07-27 22:21:52 +000011520 return DAG.getNode(X86ISD::MFENCE, dl, MVT::Other, Op.getOperand(0));
11521
11522 SDValue Chain = Op.getOperand(0);
11523 SDValue Zero = DAG.getConstant(0, MVT::i32);
11524 SDValue Ops[] = {
11525 DAG.getRegister(X86::ESP, MVT::i32), // Base
11526 DAG.getTargetConstant(1, MVT::i8), // Scale
11527 DAG.getRegister(0, MVT::i32), // Index
11528 DAG.getTargetConstant(0, MVT::i32), // Disp
11529 DAG.getRegister(0, MVT::i32), // Segment.
11530 Zero,
11531 Chain
11532 };
11533 SDNode *Res =
11534 DAG.getMachineNode(X86::OR32mrLocked, dl, MVT::Other, Ops,
11535 array_lengthof(Ops));
11536 return SDValue(Res, 0);
11537 }
11538
11539 // MEMBARRIER is a compiler barrier; it codegens to a no-op.
11540 return DAG.getNode(X86ISD::MEMBARRIER, dl, MVT::Other, Op.getOperand(0));
11541}
11542
Craig Topper55b24052012-09-11 06:15:32 +000011543static SDValue LowerCMP_SWAP(SDValue Op, const X86Subtarget *Subtarget,
11544 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +000011545 EVT T = Op.getValueType();
Chris Lattner93c4a5b2010-09-21 23:59:42 +000011546 DebugLoc DL = Op.getDebugLoc();
Andrew Lenhartha76e2f02008-03-04 21:13:33 +000011547 unsigned Reg = 0;
11548 unsigned size = 0;
Owen Anderson825b72b2009-08-11 20:47:22 +000011549 switch(T.getSimpleVT().SimpleTy) {
Craig Topperabb94d02012-02-05 03:43:23 +000011550 default: llvm_unreachable("Invalid value type!");
Owen Anderson825b72b2009-08-11 20:47:22 +000011551 case MVT::i8: Reg = X86::AL; size = 1; break;
11552 case MVT::i16: Reg = X86::AX; size = 2; break;
11553 case MVT::i32: Reg = X86::EAX; size = 4; break;
11554 case MVT::i64:
Duncan Sands1607f052008-12-01 11:39:25 +000011555 assert(Subtarget->is64Bit() && "Node not type legal!");
11556 Reg = X86::RAX; size = 8;
Andrew Lenharthd19189e2008-03-05 01:15:49 +000011557 break;
Bill Wendling61edeb52008-12-02 01:06:39 +000011558 }
Chris Lattner93c4a5b2010-09-21 23:59:42 +000011559 SDValue cpIn = DAG.getCopyToReg(Op.getOperand(0), DL, Reg,
Dale Johannesend18a4622008-09-11 03:12:59 +000011560 Op.getOperand(2), SDValue());
Dan Gohman475871a2008-07-27 21:46:04 +000011561 SDValue Ops[] = { cpIn.getValue(0),
Evan Cheng8a186ae2008-09-24 23:26:36 +000011562 Op.getOperand(1),
11563 Op.getOperand(3),
Owen Anderson825b72b2009-08-11 20:47:22 +000011564 DAG.getTargetConstant(size, MVT::i8),
Evan Cheng8a186ae2008-09-24 23:26:36 +000011565 cpIn.getValue(1) };
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +000011566 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Chris Lattner93c4a5b2010-09-21 23:59:42 +000011567 MachineMemOperand *MMO = cast<AtomicSDNode>(Op)->getMemOperand();
11568 SDValue Result = DAG.getMemIntrinsicNode(X86ISD::LCMPXCHG_DAG, DL, Tys,
11569 Ops, 5, T, MMO);
Scott Michelfdc40a02009-02-17 22:15:04 +000011570 SDValue cpOut =
Chris Lattner93c4a5b2010-09-21 23:59:42 +000011571 DAG.getCopyFromReg(Result.getValue(0), DL, Reg, T, Result.getValue(1));
Andrew Lenharth26ed8692008-03-01 21:52:34 +000011572 return cpOut;
11573}
11574
Craig Topper55b24052012-09-11 06:15:32 +000011575static SDValue LowerREADCYCLECOUNTER(SDValue Op, const X86Subtarget *Subtarget,
11576 SelectionDAG &DAG) {
Duncan Sands1607f052008-12-01 11:39:25 +000011577 assert(Subtarget->is64Bit() && "Result not type legalized?");
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +000011578 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Duncan Sands1607f052008-12-01 11:39:25 +000011579 SDValue TheChain = Op.getOperand(0);
Dale Johannesen6f38cb62009-02-07 19:59:05 +000011580 DebugLoc dl = Op.getDebugLoc();
Dale Johannesene4d209d2009-02-03 20:21:25 +000011581 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, dl, Tys, &TheChain, 1);
Owen Anderson825b72b2009-08-11 20:47:22 +000011582 SDValue rax = DAG.getCopyFromReg(rd, dl, X86::RAX, MVT::i64, rd.getValue(1));
11583 SDValue rdx = DAG.getCopyFromReg(rax.getValue(1), dl, X86::RDX, MVT::i64,
Duncan Sands1607f052008-12-01 11:39:25 +000011584 rax.getValue(2));
Owen Anderson825b72b2009-08-11 20:47:22 +000011585 SDValue Tmp = DAG.getNode(ISD::SHL, dl, MVT::i64, rdx,
11586 DAG.getConstant(32, MVT::i8));
Duncan Sands1607f052008-12-01 11:39:25 +000011587 SDValue Ops[] = {
Owen Anderson825b72b2009-08-11 20:47:22 +000011588 DAG.getNode(ISD::OR, dl, MVT::i64, rax, Tmp),
Duncan Sands1607f052008-12-01 11:39:25 +000011589 rdx.getValue(1)
11590 };
Dale Johannesene4d209d2009-02-03 20:21:25 +000011591 return DAG.getMergeValues(Ops, 2, dl);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011592}
11593
Craig Topper55b24052012-09-11 06:15:32 +000011594SDValue X86TargetLowering::LowerBITCAST(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen7d07b482010-05-21 00:52:33 +000011595 EVT SrcVT = Op.getOperand(0).getValueType();
11596 EVT DstVT = Op.getValueType();
Craig Topper1accb7e2012-01-10 06:54:16 +000011597 assert(Subtarget->is64Bit() && !Subtarget->hasSSE2() &&
Chris Lattner2a786eb2010-12-19 20:19:20 +000011598 Subtarget->hasMMX() && "Unexpected custom BITCAST");
Michael J. Spencerec38de22010-10-10 22:04:20 +000011599 assert((DstVT == MVT::i64 ||
Dale Johannesen7d07b482010-05-21 00:52:33 +000011600 (DstVT.isVector() && DstVT.getSizeInBits()==64)) &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +000011601 "Unexpected custom BITCAST");
Dale Johannesen7d07b482010-05-21 00:52:33 +000011602 // i64 <=> MMX conversions are Legal.
11603 if (SrcVT==MVT::i64 && DstVT.isVector())
11604 return Op;
11605 if (DstVT==MVT::i64 && SrcVT.isVector())
11606 return Op;
Dale Johannesene39859a2010-05-21 18:40:15 +000011607 // MMX <=> MMX conversions are Legal.
11608 if (SrcVT.isVector() && DstVT.isVector())
11609 return Op;
Dale Johannesen7d07b482010-05-21 00:52:33 +000011610 // All other conversions need to be expanded.
11611 return SDValue();
11612}
Chris Lattner5b856542010-12-20 00:59:46 +000011613
Craig Topper55b24052012-09-11 06:15:32 +000011614static SDValue LowerLOAD_SUB(SDValue Op, SelectionDAG &DAG) {
Dale Johannesen71d1bf52008-09-29 22:25:26 +000011615 SDNode *Node = Op.getNode();
Dale Johannesene4d209d2009-02-03 20:21:25 +000011616 DebugLoc dl = Node->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +000011617 EVT T = Node->getValueType(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +000011618 SDValue negOp = DAG.getNode(ISD::SUB, dl, T,
Evan Cheng242b38b2009-02-23 09:03:22 +000011619 DAG.getConstant(0, T), Node->getOperand(2));
Dale Johannesene4d209d2009-02-03 20:21:25 +000011620 return DAG.getAtomic(ISD::ATOMIC_LOAD_ADD, dl,
Dan Gohman0b1d4a72008-12-23 21:37:04 +000011621 cast<AtomicSDNode>(Node)->getMemoryVT(),
Dale Johannesen71d1bf52008-09-29 22:25:26 +000011622 Node->getOperand(0),
11623 Node->getOperand(1), negOp,
11624 cast<AtomicSDNode>(Node)->getSrcValue(),
Eli Friedman55ba8162011-07-29 03:05:32 +000011625 cast<AtomicSDNode>(Node)->getAlignment(),
11626 cast<AtomicSDNode>(Node)->getOrdering(),
11627 cast<AtomicSDNode>(Node)->getSynchScope());
Mon P Wang63307c32008-05-05 19:05:59 +000011628}
11629
Eli Friedman327236c2011-08-24 20:50:09 +000011630static SDValue LowerATOMIC_STORE(SDValue Op, SelectionDAG &DAG) {
11631 SDNode *Node = Op.getNode();
11632 DebugLoc dl = Node->getDebugLoc();
Eli Friedmanf8f90f02011-08-24 22:33:28 +000011633 EVT VT = cast<AtomicSDNode>(Node)->getMemoryVT();
Eli Friedman327236c2011-08-24 20:50:09 +000011634
11635 // Convert seq_cst store -> xchg
Eli Friedmanf8f90f02011-08-24 22:33:28 +000011636 // Convert wide store -> swap (-> cmpxchg8b/cmpxchg16b)
11637 // FIXME: On 32-bit, store -> fist or movq would be more efficient
11638 // (The only way to get a 16-byte store is cmpxchg16b)
11639 // FIXME: 16-byte ATOMIC_SWAP isn't actually hooked up at the moment.
11640 if (cast<AtomicSDNode>(Node)->getOrdering() == SequentiallyConsistent ||
11641 !DAG.getTargetLoweringInfo().isTypeLegal(VT)) {
Eli Friedman4317fe12011-08-24 21:17:30 +000011642 SDValue Swap = DAG.getAtomic(ISD::ATOMIC_SWAP, dl,
11643 cast<AtomicSDNode>(Node)->getMemoryVT(),
11644 Node->getOperand(0),
11645 Node->getOperand(1), Node->getOperand(2),
Eli Friedmanf8f90f02011-08-24 22:33:28 +000011646 cast<AtomicSDNode>(Node)->getMemOperand(),
Eli Friedman4317fe12011-08-24 21:17:30 +000011647 cast<AtomicSDNode>(Node)->getOrdering(),
11648 cast<AtomicSDNode>(Node)->getSynchScope());
Eli Friedman327236c2011-08-24 20:50:09 +000011649 return Swap.getValue(1);
11650 }
11651 // Other atomic stores have a simple pattern.
11652 return Op;
11653}
11654
Chris Lattner5b856542010-12-20 00:59:46 +000011655static SDValue LowerADDC_ADDE_SUBC_SUBE(SDValue Op, SelectionDAG &DAG) {
11656 EVT VT = Op.getNode()->getValueType(0);
11657
11658 // Let legalize expand this if it isn't a legal type yet.
11659 if (!DAG.getTargetLoweringInfo().isTypeLegal(VT))
11660 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011661
Chris Lattner5b856542010-12-20 00:59:46 +000011662 SDVTList VTs = DAG.getVTList(VT, MVT::i32);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011663
Chris Lattner5b856542010-12-20 00:59:46 +000011664 unsigned Opc;
11665 bool ExtraOp = false;
11666 switch (Op.getOpcode()) {
Craig Topperabb94d02012-02-05 03:43:23 +000011667 default: llvm_unreachable("Invalid code");
Chris Lattner5b856542010-12-20 00:59:46 +000011668 case ISD::ADDC: Opc = X86ISD::ADD; break;
11669 case ISD::ADDE: Opc = X86ISD::ADC; ExtraOp = true; break;
11670 case ISD::SUBC: Opc = X86ISD::SUB; break;
11671 case ISD::SUBE: Opc = X86ISD::SBB; ExtraOp = true; break;
11672 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011673
Chris Lattner5b856542010-12-20 00:59:46 +000011674 if (!ExtraOp)
11675 return DAG.getNode(Opc, Op->getDebugLoc(), VTs, Op.getOperand(0),
11676 Op.getOperand(1));
11677 return DAG.getNode(Opc, Op->getDebugLoc(), VTs, Op.getOperand(0),
11678 Op.getOperand(1), Op.getOperand(2));
11679}
11680
Evan Cheng0db9fe62006-04-25 20:13:52 +000011681/// LowerOperation - Provide custom lowering hooks for some operations.
11682///
Dan Gohmand858e902010-04-17 15:26:15 +000011683SDValue X86TargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +000011684 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +000011685 default: llvm_unreachable("Should not custom lower this!");
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000011686 case ISD::SIGN_EXTEND_INREG: return LowerSIGN_EXTEND_INREG(Op,DAG);
Craig Topper55b24052012-09-11 06:15:32 +000011687 case ISD::MEMBARRIER: return LowerMEMBARRIER(Op, Subtarget, DAG);
11688 case ISD::ATOMIC_FENCE: return LowerATOMIC_FENCE(Op, Subtarget, DAG);
11689 case ISD::ATOMIC_CMP_SWAP: return LowerCMP_SWAP(Op, Subtarget, DAG);
Dan Gohman0b1d4a72008-12-23 21:37:04 +000011690 case ISD::ATOMIC_LOAD_SUB: return LowerLOAD_SUB(Op,DAG);
Eli Friedman327236c2011-08-24 20:50:09 +000011691 case ISD::ATOMIC_STORE: return LowerATOMIC_STORE(Op,DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000011692 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
Mon P Wangeb38ebf2010-01-24 00:05:03 +000011693 case ISD::CONCAT_VECTORS: return LowerCONCAT_VECTORS(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000011694 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
11695 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
11696 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
Craig Topper55b24052012-09-11 06:15:32 +000011697 case ISD::EXTRACT_SUBVECTOR: return LowerEXTRACT_SUBVECTOR(Op,Subtarget,DAG);
11698 case ISD::INSERT_SUBVECTOR: return LowerINSERT_SUBVECTOR(Op, Subtarget,DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000011699 case ISD::SCALAR_TO_VECTOR: return LowerSCALAR_TO_VECTOR(Op, DAG);
11700 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
11701 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +000011702 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Bill Wendling056292f2008-09-16 21:48:12 +000011703 case ISD::ExternalSymbol: return LowerExternalSymbol(Op, DAG);
Dan Gohmanf705adb2009-10-30 01:28:02 +000011704 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000011705 case ISD::SHL_PARTS:
11706 case ISD::SRA_PARTS:
Nadav Rotem43012222011-05-11 08:12:09 +000011707 case ISD::SRL_PARTS: return LowerShiftParts(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000011708 case ISD::SINT_TO_FP: return LowerSINT_TO_FP(Op, DAG);
Dale Johannesen1c15bf52008-10-21 20:50:01 +000011709 case ISD::UINT_TO_FP: return LowerUINT_TO_FP(Op, DAG);
Michael Liaobedcbd42012-10-16 18:14:11 +000011710 case ISD::TRUNCATE: return lowerTRUNCATE(Op, DAG);
Michael Liaoa7554632012-10-23 17:36:08 +000011711 case ISD::ZERO_EXTEND: return lowerZERO_EXTEND(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000011712 case ISD::FP_TO_SINT: return LowerFP_TO_SINT(Op, DAG);
Eli Friedman948e95a2009-05-23 09:59:16 +000011713 case ISD::FP_TO_UINT: return LowerFP_TO_UINT(Op, DAG);
Michael Liao9d796db2012-10-10 16:32:15 +000011714 case ISD::FP_EXTEND: return lowerFP_EXTEND(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000011715 case ISD::FABS: return LowerFABS(Op, DAG);
11716 case ISD::FNEG: return LowerFNEG(Op, DAG);
Evan Cheng68c47cb2007-01-05 07:55:56 +000011717 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Stuart Hastings4fd0dee2011-06-01 04:39:42 +000011718 case ISD::FGETSIGN: return LowerFGETSIGN(Op, DAG);
Evan Chenge5f62042007-09-29 00:00:36 +000011719 case ISD::SETCC: return LowerSETCC(Op, DAG);
11720 case ISD::SELECT: return LowerSELECT(Op, DAG);
11721 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000011722 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000011723 case ISD::VASTART: return LowerVASTART(Op, DAG);
Dan Gohman9018e832008-05-10 01:26:14 +000011724 case ISD::VAARG: return LowerVAARG(Op, DAG);
Craig Topper55b24052012-09-11 06:15:32 +000011725 case ISD::VACOPY: return LowerVACOPY(Op, Subtarget, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000011726 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
Benjamin Kramerb9bee042012-07-12 09:31:43 +000011727 case ISD::INTRINSIC_W_CHAIN: return LowerINTRINSIC_W_CHAIN(Op, DAG);
Nate Begemanbcc5f362007-01-29 22:58:52 +000011728 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
11729 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Anton Korobeynikov2365f512007-07-14 14:06:15 +000011730 case ISD::FRAME_TO_ARGS_OFFSET:
11731 return LowerFRAME_TO_ARGS_OFFSET(Op, DAG);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +000011732 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
Anton Korobeynikov2365f512007-07-14 14:06:15 +000011733 case ISD::EH_RETURN: return LowerEH_RETURN(Op, DAG);
Michael Liao6c0e04c2012-10-15 22:39:43 +000011734 case ISD::EH_SJLJ_SETJMP: return lowerEH_SJLJ_SETJMP(Op, DAG);
11735 case ISD::EH_SJLJ_LONGJMP: return lowerEH_SJLJ_LONGJMP(Op, DAG);
Duncan Sands4a544a72011-09-06 13:37:06 +000011736 case ISD::INIT_TRAMPOLINE: return LowerINIT_TRAMPOLINE(Op, DAG);
11737 case ISD::ADJUST_TRAMPOLINE: return LowerADJUST_TRAMPOLINE(Op, DAG);
Dan Gohman1a024862008-01-31 00:41:03 +000011738 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
Evan Cheng18efe262007-12-14 02:13:44 +000011739 case ISD::CTLZ: return LowerCTLZ(Op, DAG);
Chandler Carruthacc068e2011-12-24 10:55:54 +000011740 case ISD::CTLZ_ZERO_UNDEF: return LowerCTLZ_ZERO_UNDEF(Op, DAG);
Evan Cheng18efe262007-12-14 02:13:44 +000011741 case ISD::CTTZ: return LowerCTTZ(Op, DAG);
Craig Topper55b24052012-09-11 06:15:32 +000011742 case ISD::MUL: return LowerMUL(Op, Subtarget, DAG);
Nadav Rotem43012222011-05-11 08:12:09 +000011743 case ISD::SRA:
11744 case ISD::SRL:
11745 case ISD::SHL: return LowerShift(Op, DAG);
Bill Wendling74c37652008-12-09 22:08:41 +000011746 case ISD::SADDO:
11747 case ISD::UADDO:
11748 case ISD::SSUBO:
11749 case ISD::USUBO:
11750 case ISD::SMULO:
11751 case ISD::UMULO: return LowerXALUO(Op, DAG);
Craig Topper55b24052012-09-11 06:15:32 +000011752 case ISD::READCYCLECOUNTER: return LowerREADCYCLECOUNTER(Op, Subtarget,DAG);
Wesley Peckbf17cfa2010-11-23 03:31:01 +000011753 case ISD::BITCAST: return LowerBITCAST(Op, DAG);
Chris Lattner5b856542010-12-20 00:59:46 +000011754 case ISD::ADDC:
11755 case ISD::ADDE:
11756 case ISD::SUBC:
11757 case ISD::SUBE: return LowerADDC_ADDE_SUBC_SUBE(Op, DAG);
Craig Topper13894fa2011-08-24 06:14:18 +000011758 case ISD::ADD: return LowerADD(Op, DAG);
11759 case ISD::SUB: return LowerSUB(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000011760 }
Chris Lattner27a6c732007-11-24 07:07:01 +000011761}
11762
Eli Friedmanf8f90f02011-08-24 22:33:28 +000011763static void ReplaceATOMIC_LOAD(SDNode *Node,
11764 SmallVectorImpl<SDValue> &Results,
11765 SelectionDAG &DAG) {
11766 DebugLoc dl = Node->getDebugLoc();
11767 EVT VT = cast<AtomicSDNode>(Node)->getMemoryVT();
11768
11769 // Convert wide load -> cmpxchg8b/cmpxchg16b
11770 // FIXME: On 32-bit, load -> fild or movq would be more efficient
11771 // (The only way to get a 16-byte load is cmpxchg16b)
11772 // FIXME: 16-byte ATOMIC_CMP_SWAP isn't actually hooked up at the moment.
Benjamin Kramer2753ae32011-08-27 17:36:14 +000011773 SDValue Zero = DAG.getConstant(0, VT);
11774 SDValue Swap = DAG.getAtomic(ISD::ATOMIC_CMP_SWAP, dl, VT,
Eli Friedmanf8f90f02011-08-24 22:33:28 +000011775 Node->getOperand(0),
11776 Node->getOperand(1), Zero, Zero,
11777 cast<AtomicSDNode>(Node)->getMemOperand(),
11778 cast<AtomicSDNode>(Node)->getOrdering(),
11779 cast<AtomicSDNode>(Node)->getSynchScope());
11780 Results.push_back(Swap.getValue(0));
11781 Results.push_back(Swap.getValue(1));
11782}
11783
Craig Topperc0878702012-08-17 06:55:11 +000011784static void
Duncan Sands1607f052008-12-01 11:39:25 +000011785ReplaceATOMIC_BINARY_64(SDNode *Node, SmallVectorImpl<SDValue>&Results,
Craig Topperc0878702012-08-17 06:55:11 +000011786 SelectionDAG &DAG, unsigned NewOp) {
Dale Johannesene4d209d2009-02-03 20:21:25 +000011787 DebugLoc dl = Node->getDebugLoc();
Duncan Sands17001ce2011-10-18 12:44:00 +000011788 assert (Node->getValueType(0) == MVT::i64 &&
11789 "Only know how to expand i64 atomics");
Duncan Sands1607f052008-12-01 11:39:25 +000011790
11791 SDValue Chain = Node->getOperand(0);
11792 SDValue In1 = Node->getOperand(1);
Owen Anderson825b72b2009-08-11 20:47:22 +000011793 SDValue In2L = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +000011794 Node->getOperand(2), DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +000011795 SDValue In2H = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +000011796 Node->getOperand(2), DAG.getIntPtrConstant(1));
Dan Gohmanc76909a2009-09-25 20:36:54 +000011797 SDValue Ops[] = { Chain, In1, In2L, In2H };
Owen Anderson825b72b2009-08-11 20:47:22 +000011798 SDVTList Tys = DAG.getVTList(MVT::i32, MVT::i32, MVT::Other);
Dan Gohmanc76909a2009-09-25 20:36:54 +000011799 SDValue Result =
11800 DAG.getMemIntrinsicNode(NewOp, dl, Tys, Ops, 4, MVT::i64,
11801 cast<MemSDNode>(Node)->getMemOperand());
Duncan Sands1607f052008-12-01 11:39:25 +000011802 SDValue OpsF[] = { Result.getValue(0), Result.getValue(1)};
Owen Anderson825b72b2009-08-11 20:47:22 +000011803 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, OpsF, 2));
Duncan Sands1607f052008-12-01 11:39:25 +000011804 Results.push_back(Result.getValue(2));
11805}
11806
Duncan Sands126d9072008-07-04 11:47:58 +000011807/// ReplaceNodeResults - Replace a node with an illegal result type
11808/// with a new node built out of custom code.
Duncan Sands1607f052008-12-01 11:39:25 +000011809void X86TargetLowering::ReplaceNodeResults(SDNode *N,
11810 SmallVectorImpl<SDValue>&Results,
Dan Gohmand858e902010-04-17 15:26:15 +000011811 SelectionDAG &DAG) const {
Dale Johannesene4d209d2009-02-03 20:21:25 +000011812 DebugLoc dl = N->getDebugLoc();
Nadav Rotem0a1e9142012-12-14 21:20:37 +000011813 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Chris Lattner27a6c732007-11-24 07:07:01 +000011814 switch (N->getOpcode()) {
Duncan Sandsed294c42008-10-20 15:56:33 +000011815 default:
Craig Topperabb94d02012-02-05 03:43:23 +000011816 llvm_unreachable("Do not know how to custom type legalize this operation!");
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000011817 case ISD::SIGN_EXTEND_INREG:
Chris Lattner5b856542010-12-20 00:59:46 +000011818 case ISD::ADDC:
11819 case ISD::ADDE:
11820 case ISD::SUBC:
11821 case ISD::SUBE:
11822 // We don't want to expand or promote these.
11823 return;
Michael J. Spencer1a2d0612012-02-24 19:01:22 +000011824 case ISD::FP_TO_SINT:
11825 case ISD::FP_TO_UINT: {
11826 bool IsSigned = N->getOpcode() == ISD::FP_TO_SINT;
11827
11828 if (!IsSigned && !isIntegerTypeFTOL(SDValue(N, 0).getValueType()))
11829 return;
11830
Eli Friedman948e95a2009-05-23 09:59:16 +000011831 std::pair<SDValue,SDValue> Vals =
NAKAMURA Takumi9a68fdc2012-02-25 03:37:25 +000011832 FP_TO_INTHelper(SDValue(N, 0), DAG, IsSigned, /*IsReplace=*/ true);
Duncan Sands1607f052008-12-01 11:39:25 +000011833 SDValue FIST = Vals.first, StackSlot = Vals.second;
11834 if (FIST.getNode() != 0) {
Owen Andersone50ed302009-08-10 22:56:29 +000011835 EVT VT = N->getValueType(0);
Duncan Sands1607f052008-12-01 11:39:25 +000011836 // Return a load from the stack slot.
Michael J. Spencer1a2d0612012-02-24 19:01:22 +000011837 if (StackSlot.getNode() != 0)
11838 Results.push_back(DAG.getLoad(VT, dl, FIST, StackSlot,
11839 MachinePointerInfo(),
11840 false, false, false, 0));
11841 else
11842 Results.push_back(FIST);
Duncan Sands1607f052008-12-01 11:39:25 +000011843 }
11844 return;
11845 }
Michael Liao991b6a22012-10-24 04:09:32 +000011846 case ISD::UINT_TO_FP: {
11847 if (N->getOperand(0).getValueType() != MVT::v2i32 &&
11848 N->getValueType(0) != MVT::v2f32)
11849 return;
11850 SDValue ZExtIn = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v2i64,
11851 N->getOperand(0));
11852 SDValue Bias = DAG.getConstantFP(BitsToDouble(0x4330000000000000ULL),
11853 MVT::f64);
11854 SDValue VBias = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v2f64, Bias, Bias);
11855 SDValue Or = DAG.getNode(ISD::OR, dl, MVT::v2i64, ZExtIn,
11856 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64, VBias));
11857 Or = DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Or);
11858 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::v2f64, Or, VBias);
11859 Results.push_back(DAG.getNode(X86ISD::VFPROUND, dl, MVT::v4f32, Sub));
11860 return;
11861 }
Michael Liao44c2d612012-10-10 16:53:28 +000011862 case ISD::FP_ROUND: {
Nadav Rotem0a1e9142012-12-14 21:20:37 +000011863 if (!TLI.isTypeLegal(N->getOperand(0).getValueType()))
11864 return;
Michael Liao44c2d612012-10-10 16:53:28 +000011865 SDValue V = DAG.getNode(X86ISD::VFPROUND, dl, MVT::v4f32, N->getOperand(0));
11866 Results.push_back(V);
11867 return;
11868 }
Duncan Sands1607f052008-12-01 11:39:25 +000011869 case ISD::READCYCLECOUNTER: {
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +000011870 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Duncan Sands1607f052008-12-01 11:39:25 +000011871 SDValue TheChain = N->getOperand(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +000011872 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, dl, Tys, &TheChain, 1);
Owen Anderson825b72b2009-08-11 20:47:22 +000011873 SDValue eax = DAG.getCopyFromReg(rd, dl, X86::EAX, MVT::i32,
Dale Johannesendd64c412009-02-04 00:33:20 +000011874 rd.getValue(1));
Owen Anderson825b72b2009-08-11 20:47:22 +000011875 SDValue edx = DAG.getCopyFromReg(eax.getValue(1), dl, X86::EDX, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +000011876 eax.getValue(2));
11877 // Use a buildpair to merge the two 32-bit values into a 64-bit one.
11878 SDValue Ops[] = { eax, edx };
Owen Anderson825b72b2009-08-11 20:47:22 +000011879 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Ops, 2));
Duncan Sands1607f052008-12-01 11:39:25 +000011880 Results.push_back(edx.getValue(1));
11881 return;
11882 }
Dan Gohman0b1d4a72008-12-23 21:37:04 +000011883 case ISD::ATOMIC_CMP_SWAP: {
Owen Andersone50ed302009-08-10 22:56:29 +000011884 EVT T = N->getValueType(0);
Benjamin Kramer2753ae32011-08-27 17:36:14 +000011885 assert((T == MVT::i64 || T == MVT::i128) && "can only expand cmpxchg pair");
Eli Friedman43f51ae2011-08-26 21:21:21 +000011886 bool Regs64bit = T == MVT::i128;
11887 EVT HalfT = Regs64bit ? MVT::i64 : MVT::i32;
Duncan Sands1607f052008-12-01 11:39:25 +000011888 SDValue cpInL, cpInH;
Eli Friedman43f51ae2011-08-26 21:21:21 +000011889 cpInL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, HalfT, N->getOperand(2),
11890 DAG.getConstant(0, HalfT));
11891 cpInH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, HalfT, N->getOperand(2),
11892 DAG.getConstant(1, HalfT));
11893 cpInL = DAG.getCopyToReg(N->getOperand(0), dl,
11894 Regs64bit ? X86::RAX : X86::EAX,
11895 cpInL, SDValue());
11896 cpInH = DAG.getCopyToReg(cpInL.getValue(0), dl,
11897 Regs64bit ? X86::RDX : X86::EDX,
11898 cpInH, cpInL.getValue(1));
Duncan Sands1607f052008-12-01 11:39:25 +000011899 SDValue swapInL, swapInH;
Eli Friedman43f51ae2011-08-26 21:21:21 +000011900 swapInL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, HalfT, N->getOperand(3),
11901 DAG.getConstant(0, HalfT));
11902 swapInH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, HalfT, N->getOperand(3),
11903 DAG.getConstant(1, HalfT));
11904 swapInL = DAG.getCopyToReg(cpInH.getValue(0), dl,
11905 Regs64bit ? X86::RBX : X86::EBX,
11906 swapInL, cpInH.getValue(1));
11907 swapInH = DAG.getCopyToReg(swapInL.getValue(0), dl,
Chad Rosiera20e1e72012-08-01 18:39:17 +000011908 Regs64bit ? X86::RCX : X86::ECX,
Eli Friedman43f51ae2011-08-26 21:21:21 +000011909 swapInH, swapInL.getValue(1));
Duncan Sands1607f052008-12-01 11:39:25 +000011910 SDValue Ops[] = { swapInH.getValue(0),
11911 N->getOperand(1),
11912 swapInH.getValue(1) };
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +000011913 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Andrew Trick1a2cf3b2010-10-11 19:02:04 +000011914 MachineMemOperand *MMO = cast<AtomicSDNode>(N)->getMemOperand();
Eli Friedman43f51ae2011-08-26 21:21:21 +000011915 unsigned Opcode = Regs64bit ? X86ISD::LCMPXCHG16_DAG :
11916 X86ISD::LCMPXCHG8_DAG;
11917 SDValue Result = DAG.getMemIntrinsicNode(Opcode, dl, Tys,
Andrew Trick1a2cf3b2010-10-11 19:02:04 +000011918 Ops, 3, T, MMO);
Eli Friedman43f51ae2011-08-26 21:21:21 +000011919 SDValue cpOutL = DAG.getCopyFromReg(Result.getValue(0), dl,
11920 Regs64bit ? X86::RAX : X86::EAX,
11921 HalfT, Result.getValue(1));
11922 SDValue cpOutH = DAG.getCopyFromReg(cpOutL.getValue(1), dl,
11923 Regs64bit ? X86::RDX : X86::EDX,
11924 HalfT, cpOutL.getValue(2));
Duncan Sands1607f052008-12-01 11:39:25 +000011925 SDValue OpsF[] = { cpOutL.getValue(0), cpOutH.getValue(0)};
Eli Friedman43f51ae2011-08-26 21:21:21 +000011926 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, T, OpsF, 2));
Duncan Sands1607f052008-12-01 11:39:25 +000011927 Results.push_back(cpOutH.getValue(1));
11928 return;
11929 }
Dan Gohman0b1d4a72008-12-23 21:37:04 +000011930 case ISD::ATOMIC_LOAD_ADD:
Dan Gohman0b1d4a72008-12-23 21:37:04 +000011931 case ISD::ATOMIC_LOAD_AND:
Dan Gohman0b1d4a72008-12-23 21:37:04 +000011932 case ISD::ATOMIC_LOAD_NAND:
Dan Gohman0b1d4a72008-12-23 21:37:04 +000011933 case ISD::ATOMIC_LOAD_OR:
Dan Gohman0b1d4a72008-12-23 21:37:04 +000011934 case ISD::ATOMIC_LOAD_SUB:
Dan Gohman0b1d4a72008-12-23 21:37:04 +000011935 case ISD::ATOMIC_LOAD_XOR:
Michael Liaoe5e8f762012-09-25 18:08:13 +000011936 case ISD::ATOMIC_LOAD_MAX:
11937 case ISD::ATOMIC_LOAD_MIN:
11938 case ISD::ATOMIC_LOAD_UMAX:
11939 case ISD::ATOMIC_LOAD_UMIN:
Craig Topperc0878702012-08-17 06:55:11 +000011940 case ISD::ATOMIC_SWAP: {
11941 unsigned Opc;
11942 switch (N->getOpcode()) {
11943 default: llvm_unreachable("Unexpected opcode");
11944 case ISD::ATOMIC_LOAD_ADD:
11945 Opc = X86ISD::ATOMADD64_DAG;
11946 break;
11947 case ISD::ATOMIC_LOAD_AND:
11948 Opc = X86ISD::ATOMAND64_DAG;
11949 break;
11950 case ISD::ATOMIC_LOAD_NAND:
11951 Opc = X86ISD::ATOMNAND64_DAG;
11952 break;
11953 case ISD::ATOMIC_LOAD_OR:
11954 Opc = X86ISD::ATOMOR64_DAG;
11955 break;
11956 case ISD::ATOMIC_LOAD_SUB:
11957 Opc = X86ISD::ATOMSUB64_DAG;
11958 break;
11959 case ISD::ATOMIC_LOAD_XOR:
11960 Opc = X86ISD::ATOMXOR64_DAG;
11961 break;
Michael Liaoe5e8f762012-09-25 18:08:13 +000011962 case ISD::ATOMIC_LOAD_MAX:
11963 Opc = X86ISD::ATOMMAX64_DAG;
11964 break;
11965 case ISD::ATOMIC_LOAD_MIN:
11966 Opc = X86ISD::ATOMMIN64_DAG;
11967 break;
11968 case ISD::ATOMIC_LOAD_UMAX:
11969 Opc = X86ISD::ATOMUMAX64_DAG;
11970 break;
11971 case ISD::ATOMIC_LOAD_UMIN:
11972 Opc = X86ISD::ATOMUMIN64_DAG;
11973 break;
Craig Topperc0878702012-08-17 06:55:11 +000011974 case ISD::ATOMIC_SWAP:
11975 Opc = X86ISD::ATOMSWAP64_DAG;
11976 break;
11977 }
11978 ReplaceATOMIC_BINARY_64(N, Results, DAG, Opc);
Duncan Sands1607f052008-12-01 11:39:25 +000011979 return;
Craig Topperc0878702012-08-17 06:55:11 +000011980 }
Eli Friedmanf8f90f02011-08-24 22:33:28 +000011981 case ISD::ATOMIC_LOAD:
11982 ReplaceATOMIC_LOAD(N, Results, DAG);
Chris Lattner27a6c732007-11-24 07:07:01 +000011983 }
Evan Cheng0db9fe62006-04-25 20:13:52 +000011984}
11985
Evan Cheng72261582005-12-20 06:22:03 +000011986const char *X86TargetLowering::getTargetNodeName(unsigned Opcode) const {
11987 switch (Opcode) {
11988 default: return NULL;
Evan Cheng18efe262007-12-14 02:13:44 +000011989 case X86ISD::BSF: return "X86ISD::BSF";
11990 case X86ISD::BSR: return "X86ISD::BSR";
Evan Chenge3413162006-01-09 18:33:28 +000011991 case X86ISD::SHLD: return "X86ISD::SHLD";
11992 case X86ISD::SHRD: return "X86ISD::SHRD";
Evan Chengef6ffb12006-01-31 03:14:29 +000011993 case X86ISD::FAND: return "X86ISD::FAND";
Evan Cheng68c47cb2007-01-05 07:55:56 +000011994 case X86ISD::FOR: return "X86ISD::FOR";
Evan Cheng223547a2006-01-31 22:28:30 +000011995 case X86ISD::FXOR: return "X86ISD::FXOR";
Evan Cheng68c47cb2007-01-05 07:55:56 +000011996 case X86ISD::FSRL: return "X86ISD::FSRL";
Evan Chenga3195e82006-01-12 22:54:21 +000011997 case X86ISD::FILD: return "X86ISD::FILD";
Evan Chenge3de85b2006-02-04 02:20:30 +000011998 case X86ISD::FILD_FLAG: return "X86ISD::FILD_FLAG";
Evan Cheng72261582005-12-20 06:22:03 +000011999 case X86ISD::FP_TO_INT16_IN_MEM: return "X86ISD::FP_TO_INT16_IN_MEM";
12000 case X86ISD::FP_TO_INT32_IN_MEM: return "X86ISD::FP_TO_INT32_IN_MEM";
12001 case X86ISD::FP_TO_INT64_IN_MEM: return "X86ISD::FP_TO_INT64_IN_MEM";
Evan Chengb077b842005-12-21 02:39:21 +000012002 case X86ISD::FLD: return "X86ISD::FLD";
Evan Chengd90eb7f2006-01-05 00:27:02 +000012003 case X86ISD::FST: return "X86ISD::FST";
Evan Cheng72261582005-12-20 06:22:03 +000012004 case X86ISD::CALL: return "X86ISD::CALL";
Evan Cheng72261582005-12-20 06:22:03 +000012005 case X86ISD::RDTSC_DAG: return "X86ISD::RDTSC_DAG";
Dan Gohmanc7a37d42008-12-23 22:45:23 +000012006 case X86ISD::BT: return "X86ISD::BT";
Evan Cheng72261582005-12-20 06:22:03 +000012007 case X86ISD::CMP: return "X86ISD::CMP";
Evan Cheng6be2c582006-04-05 23:38:46 +000012008 case X86ISD::COMI: return "X86ISD::COMI";
12009 case X86ISD::UCOMI: return "X86ISD::UCOMI";
Evan Chengd5781fc2005-12-21 20:21:51 +000012010 case X86ISD::SETCC: return "X86ISD::SETCC";
Evan Chengad9c0a32009-12-15 00:53:42 +000012011 case X86ISD::SETCC_CARRY: return "X86ISD::SETCC_CARRY";
Stuart Hastings865f0932011-06-03 23:53:54 +000012012 case X86ISD::FSETCCsd: return "X86ISD::FSETCCsd";
12013 case X86ISD::FSETCCss: return "X86ISD::FSETCCss";
Evan Cheng72261582005-12-20 06:22:03 +000012014 case X86ISD::CMOV: return "X86ISD::CMOV";
12015 case X86ISD::BRCOND: return "X86ISD::BRCOND";
Evan Chengb077b842005-12-21 02:39:21 +000012016 case X86ISD::RET_FLAG: return "X86ISD::RET_FLAG";
Evan Cheng8df346b2006-03-04 01:12:00 +000012017 case X86ISD::REP_STOS: return "X86ISD::REP_STOS";
12018 case X86ISD::REP_MOVS: return "X86ISD::REP_MOVS";
Evan Cheng7ccced62006-02-18 00:15:05 +000012019 case X86ISD::GlobalBaseReg: return "X86ISD::GlobalBaseReg";
Evan Cheng020d2e82006-02-23 20:41:18 +000012020 case X86ISD::Wrapper: return "X86ISD::Wrapper";
Chris Lattner18c59872009-06-27 04:16:01 +000012021 case X86ISD::WrapperRIP: return "X86ISD::WrapperRIP";
Nate Begeman14d12ca2008-02-11 04:19:36 +000012022 case X86ISD::PEXTRB: return "X86ISD::PEXTRB";
Evan Chengb067a1e2006-03-31 19:22:53 +000012023 case X86ISD::PEXTRW: return "X86ISD::PEXTRW";
Nate Begeman14d12ca2008-02-11 04:19:36 +000012024 case X86ISD::INSERTPS: return "X86ISD::INSERTPS";
12025 case X86ISD::PINSRB: return "X86ISD::PINSRB";
Evan Cheng653159f2006-03-31 21:55:24 +000012026 case X86ISD::PINSRW: return "X86ISD::PINSRW";
Nate Begemanb9a47b82009-02-23 08:49:38 +000012027 case X86ISD::PSHUFB: return "X86ISD::PSHUFB";
Bruno Cardoso Lopesc1af4772011-07-13 21:36:47 +000012028 case X86ISD::ANDNP: return "X86ISD::ANDNP";
Craig Topper31133842011-11-19 07:33:10 +000012029 case X86ISD::PSIGN: return "X86ISD::PSIGN";
Craig Toppere6a62772011-11-13 17:31:07 +000012030 case X86ISD::BLENDV: return "X86ISD::BLENDV";
Elena Demikhovsky226e0e62012-12-05 09:24:57 +000012031 case X86ISD::BLENDI: return "X86ISD::BLENDI";
Benjamin Kramer388fc6a2012-12-15 16:47:44 +000012032 case X86ISD::SUBUS: return "X86ISD::SUBUS";
Craig Topperfe033152011-12-06 09:31:36 +000012033 case X86ISD::HADD: return "X86ISD::HADD";
12034 case X86ISD::HSUB: return "X86ISD::HSUB";
Craig Toppere6a62772011-11-13 17:31:07 +000012035 case X86ISD::FHADD: return "X86ISD::FHADD";
12036 case X86ISD::FHSUB: return "X86ISD::FHSUB";
Benjamin Kramer739c7a82012-12-21 14:04:55 +000012037 case X86ISD::UMAX: return "X86ISD::UMAX";
12038 case X86ISD::UMIN: return "X86ISD::UMIN";
12039 case X86ISD::SMAX: return "X86ISD::SMAX";
12040 case X86ISD::SMIN: return "X86ISD::SMIN";
Evan Cheng8ca29322006-11-10 21:43:37 +000012041 case X86ISD::FMAX: return "X86ISD::FMAX";
12042 case X86ISD::FMIN: return "X86ISD::FMIN";
Nadav Rotemd60cb112012-08-19 13:06:16 +000012043 case X86ISD::FMAXC: return "X86ISD::FMAXC";
12044 case X86ISD::FMINC: return "X86ISD::FMINC";
Dan Gohman20382522007-07-10 00:05:58 +000012045 case X86ISD::FRSQRT: return "X86ISD::FRSQRT";
12046 case X86ISD::FRCP: return "X86ISD::FRCP";
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +000012047 case X86ISD::TLSADDR: return "X86ISD::TLSADDR";
Hans Wennborgf0234fc2012-06-01 16:27:21 +000012048 case X86ISD::TLSBASEADDR: return "X86ISD::TLSBASEADDR";
Eric Christopher30ef0e52010-06-03 04:07:48 +000012049 case X86ISD::TLSCALL: return "X86ISD::TLSCALL";
Michael Liao6c0e04c2012-10-15 22:39:43 +000012050 case X86ISD::EH_SJLJ_SETJMP: return "X86ISD::EH_SJLJ_SETJMP";
12051 case X86ISD::EH_SJLJ_LONGJMP: return "X86ISD::EH_SJLJ_LONGJMP";
Anton Korobeynikov2365f512007-07-14 14:06:15 +000012052 case X86ISD::EH_RETURN: return "X86ISD::EH_RETURN";
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +000012053 case X86ISD::TC_RETURN: return "X86ISD::TC_RETURN";
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000012054 case X86ISD::FNSTCW16m: return "X86ISD::FNSTCW16m";
Benjamin Kramer17c836c2012-04-27 12:07:43 +000012055 case X86ISD::FNSTSW16r: return "X86ISD::FNSTSW16r";
Evan Cheng7e2ff772008-05-08 00:57:18 +000012056 case X86ISD::LCMPXCHG_DAG: return "X86ISD::LCMPXCHG_DAG";
12057 case X86ISD::LCMPXCHG8_DAG: return "X86ISD::LCMPXCHG8_DAG";
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012058 case X86ISD::ATOMADD64_DAG: return "X86ISD::ATOMADD64_DAG";
12059 case X86ISD::ATOMSUB64_DAG: return "X86ISD::ATOMSUB64_DAG";
12060 case X86ISD::ATOMOR64_DAG: return "X86ISD::ATOMOR64_DAG";
12061 case X86ISD::ATOMXOR64_DAG: return "X86ISD::ATOMXOR64_DAG";
12062 case X86ISD::ATOMAND64_DAG: return "X86ISD::ATOMAND64_DAG";
12063 case X86ISD::ATOMNAND64_DAG: return "X86ISD::ATOMNAND64_DAG";
Evan Chengd880b972008-05-09 21:53:03 +000012064 case X86ISD::VZEXT_MOVL: return "X86ISD::VZEXT_MOVL";
Michael Liaob7bf7262012-08-14 22:53:17 +000012065 case X86ISD::VSEXT_MOVL: return "X86ISD::VSEXT_MOVL";
Evan Chengd880b972008-05-09 21:53:03 +000012066 case X86ISD::VZEXT_LOAD: return "X86ISD::VZEXT_LOAD";
Michael Liaod9d09602012-10-23 17:34:00 +000012067 case X86ISD::VZEXT: return "X86ISD::VZEXT";
12068 case X86ISD::VSEXT: return "X86ISD::VSEXT";
Michael Liao7091b242012-08-14 21:24:47 +000012069 case X86ISD::VFPEXT: return "X86ISD::VFPEXT";
Michael Liao44c2d612012-10-10 16:53:28 +000012070 case X86ISD::VFPROUND: return "X86ISD::VFPROUND";
Craig Toppered2e13d2012-01-22 19:15:14 +000012071 case X86ISD::VSHLDQ: return "X86ISD::VSHLDQ";
12072 case X86ISD::VSRLDQ: return "X86ISD::VSRLDQ";
Evan Chengf26ffe92008-05-29 08:22:04 +000012073 case X86ISD::VSHL: return "X86ISD::VSHL";
12074 case X86ISD::VSRL: return "X86ISD::VSRL";
Craig Toppered2e13d2012-01-22 19:15:14 +000012075 case X86ISD::VSRA: return "X86ISD::VSRA";
12076 case X86ISD::VSHLI: return "X86ISD::VSHLI";
12077 case X86ISD::VSRLI: return "X86ISD::VSRLI";
12078 case X86ISD::VSRAI: return "X86ISD::VSRAI";
Craig Topper1906d322012-01-22 23:36:02 +000012079 case X86ISD::CMPP: return "X86ISD::CMPP";
Craig Topper67609fd2012-01-22 22:42:16 +000012080 case X86ISD::PCMPEQ: return "X86ISD::PCMPEQ";
12081 case X86ISD::PCMPGT: return "X86ISD::PCMPGT";
Bill Wendlingab55ebd2008-12-12 00:56:36 +000012082 case X86ISD::ADD: return "X86ISD::ADD";
12083 case X86ISD::SUB: return "X86ISD::SUB";
Chris Lattner5b856542010-12-20 00:59:46 +000012084 case X86ISD::ADC: return "X86ISD::ADC";
12085 case X86ISD::SBB: return "X86ISD::SBB";
Bill Wendlingd350e022008-12-12 21:15:41 +000012086 case X86ISD::SMUL: return "X86ISD::SMUL";
12087 case X86ISD::UMUL: return "X86ISD::UMUL";
Dan Gohman076aee32009-03-04 19:44:21 +000012088 case X86ISD::INC: return "X86ISD::INC";
12089 case X86ISD::DEC: return "X86ISD::DEC";
Dan Gohmane220c4b2009-09-18 19:59:53 +000012090 case X86ISD::OR: return "X86ISD::OR";
12091 case X86ISD::XOR: return "X86ISD::XOR";
12092 case X86ISD::AND: return "X86ISD::AND";
Craig Toppere6a62772011-11-13 17:31:07 +000012093 case X86ISD::BLSI: return "X86ISD::BLSI";
12094 case X86ISD::BLSMSK: return "X86ISD::BLSMSK";
12095 case X86ISD::BLSR: return "X86ISD::BLSR";
Evan Cheng73f24c92009-03-30 21:36:47 +000012096 case X86ISD::MUL_IMM: return "X86ISD::MUL_IMM";
Eric Christopher71c67532009-07-29 00:28:05 +000012097 case X86ISD::PTEST: return "X86ISD::PTEST";
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +000012098 case X86ISD::TESTP: return "X86ISD::TESTP";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000012099 case X86ISD::PALIGN: return "X86ISD::PALIGN";
12100 case X86ISD::PSHUFD: return "X86ISD::PSHUFD";
12101 case X86ISD::PSHUFHW: return "X86ISD::PSHUFHW";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000012102 case X86ISD::PSHUFLW: return "X86ISD::PSHUFLW";
Craig Topperb3982da2011-12-31 23:50:21 +000012103 case X86ISD::SHUFP: return "X86ISD::SHUFP";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000012104 case X86ISD::MOVLHPS: return "X86ISD::MOVLHPS";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000012105 case X86ISD::MOVLHPD: return "X86ISD::MOVLHPD";
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +000012106 case X86ISD::MOVHLPS: return "X86ISD::MOVHLPS";
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +000012107 case X86ISD::MOVLPS: return "X86ISD::MOVLPS";
12108 case X86ISD::MOVLPD: return "X86ISD::MOVLPD";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000012109 case X86ISD::MOVDDUP: return "X86ISD::MOVDDUP";
12110 case X86ISD::MOVSHDUP: return "X86ISD::MOVSHDUP";
12111 case X86ISD::MOVSLDUP: return "X86ISD::MOVSLDUP";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000012112 case X86ISD::MOVSD: return "X86ISD::MOVSD";
12113 case X86ISD::MOVSS: return "X86ISD::MOVSS";
Craig Topper34671b82011-12-06 08:21:25 +000012114 case X86ISD::UNPCKL: return "X86ISD::UNPCKL";
12115 case X86ISD::UNPCKH: return "X86ISD::UNPCKH";
Bruno Cardoso Lopes0e6d2302011-08-17 02:29:19 +000012116 case X86ISD::VBROADCAST: return "X86ISD::VBROADCAST";
Craig Topper316cd2a2011-11-30 06:25:25 +000012117 case X86ISD::VPERMILP: return "X86ISD::VPERMILP";
Craig Topperec24e612011-11-30 07:47:51 +000012118 case X86ISD::VPERM2X128: return "X86ISD::VPERM2X128";
Craig Topper8325c112012-04-16 00:41:45 +000012119 case X86ISD::VPERMV: return "X86ISD::VPERMV";
12120 case X86ISD::VPERMI: return "X86ISD::VPERMI";
Craig Topper5b209e82012-02-05 03:14:49 +000012121 case X86ISD::PMULUDQ: return "X86ISD::PMULUDQ";
Dan Gohmand6708ea2009-08-15 01:38:56 +000012122 case X86ISD::VASTART_SAVE_XMM_REGS: return "X86ISD::VASTART_SAVE_XMM_REGS";
Dan Gohman320afb82010-10-12 18:00:49 +000012123 case X86ISD::VAARG_64: return "X86ISD::VAARG_64";
Michael J. Spencere9c253e2010-10-21 01:41:01 +000012124 case X86ISD::WIN_ALLOCA: return "X86ISD::WIN_ALLOCA";
Eli Friedman14648462011-07-27 22:21:52 +000012125 case X86ISD::MEMBARRIER: return "X86ISD::MEMBARRIER";
Rafael Espindolad07b7ec2011-08-30 19:43:21 +000012126 case X86ISD::SEG_ALLOCA: return "X86ISD::SEG_ALLOCA";
Michael J. Spencer1a2d0612012-02-24 19:01:22 +000012127 case X86ISD::WIN_FTOL: return "X86ISD::WIN_FTOL";
Benjamin Kramer17c836c2012-04-27 12:07:43 +000012128 case X86ISD::SAHF: return "X86ISD::SAHF";
Benjamin Kramerb9bee042012-07-12 09:31:43 +000012129 case X86ISD::RDRAND: return "X86ISD::RDRAND";
Elena Demikhovsky1503aba2012-08-01 12:06:00 +000012130 case X86ISD::FMADD: return "X86ISD::FMADD";
12131 case X86ISD::FMSUB: return "X86ISD::FMSUB";
12132 case X86ISD::FNMADD: return "X86ISD::FNMADD";
12133 case X86ISD::FNMSUB: return "X86ISD::FNMSUB";
12134 case X86ISD::FMADDSUB: return "X86ISD::FMADDSUB";
12135 case X86ISD::FMSUBADD: return "X86ISD::FMSUBADD";
Craig Topper9c7ae012012-11-10 01:23:36 +000012136 case X86ISD::PCMPESTRI: return "X86ISD::PCMPESTRI";
12137 case X86ISD::PCMPISTRI: return "X86ISD::PCMPISTRI";
Evan Cheng72261582005-12-20 06:22:03 +000012138 }
12139}
Evan Cheng3a03ebb2005-12-21 23:05:39 +000012140
Chris Lattnerc9addb72007-03-30 23:15:24 +000012141// isLegalAddressingMode - Return true if the addressing mode represented
12142// by AM is legal for this target, for a load/store of the specified type.
Scott Michelfdc40a02009-02-17 22:15:04 +000012143bool X86TargetLowering::isLegalAddressingMode(const AddrMode &AM,
Chris Lattnerdb125cf2011-07-18 04:54:35 +000012144 Type *Ty) const {
Chris Lattnerc9addb72007-03-30 23:15:24 +000012145 // X86 supports extremely general addressing modes.
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000012146 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman92b651f2010-08-24 15:55:12 +000012147 Reloc::Model R = getTargetMachine().getRelocationModel();
Scott Michelfdc40a02009-02-17 22:15:04 +000012148
Chris Lattnerc9addb72007-03-30 23:15:24 +000012149 // X86 allows a sign-extended 32-bit immediate field as a displacement.
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000012150 if (!X86::isOffsetSuitableForCodeModel(AM.BaseOffs, M, AM.BaseGV != NULL))
Chris Lattnerc9addb72007-03-30 23:15:24 +000012151 return false;
Scott Michelfdc40a02009-02-17 22:15:04 +000012152
Chris Lattnerc9addb72007-03-30 23:15:24 +000012153 if (AM.BaseGV) {
Chris Lattnerdfed4132009-07-10 07:38:24 +000012154 unsigned GVFlags =
12155 Subtarget->ClassifyGlobalReference(AM.BaseGV, getTargetMachine());
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000012156
Chris Lattnerdfed4132009-07-10 07:38:24 +000012157 // If a reference to this global requires an extra load, we can't fold it.
12158 if (isGlobalStubReference(GVFlags))
Chris Lattnerc9addb72007-03-30 23:15:24 +000012159 return false;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000012160
Chris Lattnerdfed4132009-07-10 07:38:24 +000012161 // If BaseGV requires a register for the PIC base, we cannot also have a
12162 // BaseReg specified.
12163 if (AM.HasBaseReg && isGlobalRelativeToPICBase(GVFlags))
Dale Johannesen203af582008-12-05 21:47:27 +000012164 return false;
Evan Cheng52787842007-08-01 23:46:47 +000012165
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000012166 // If lower 4G is not available, then we must use rip-relative addressing.
Dan Gohman92b651f2010-08-24 15:55:12 +000012167 if ((M != CodeModel::Small || R != Reloc::Static) &&
12168 Subtarget->is64Bit() && (AM.BaseOffs || AM.Scale > 1))
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000012169 return false;
Chris Lattnerc9addb72007-03-30 23:15:24 +000012170 }
Scott Michelfdc40a02009-02-17 22:15:04 +000012171
Chris Lattnerc9addb72007-03-30 23:15:24 +000012172 switch (AM.Scale) {
12173 case 0:
12174 case 1:
12175 case 2:
12176 case 4:
12177 case 8:
12178 // These scales always work.
12179 break;
12180 case 3:
12181 case 5:
12182 case 9:
12183 // These scales are formed with basereg+scalereg. Only accept if there is
12184 // no basereg yet.
12185 if (AM.HasBaseReg)
12186 return false;
12187 break;
12188 default: // Other stuff never works.
12189 return false;
12190 }
Scott Michelfdc40a02009-02-17 22:15:04 +000012191
Chris Lattnerc9addb72007-03-30 23:15:24 +000012192 return true;
12193}
12194
Chris Lattnerdb125cf2011-07-18 04:54:35 +000012195bool X86TargetLowering::isTruncateFree(Type *Ty1, Type *Ty2) const {
Duncan Sandsb0bc6c32010-02-15 16:12:20 +000012196 if (!Ty1->isIntegerTy() || !Ty2->isIntegerTy())
Evan Cheng2bd122c2007-10-26 01:56:11 +000012197 return false;
Evan Chenge127a732007-10-29 07:57:50 +000012198 unsigned NumBits1 = Ty1->getPrimitiveSizeInBits();
12199 unsigned NumBits2 = Ty2->getPrimitiveSizeInBits();
Evan Cheng260e07e2008-03-20 02:18:41 +000012200 if (NumBits1 <= NumBits2)
Evan Chenge127a732007-10-29 07:57:50 +000012201 return false;
Dan Gohman377fbc02010-02-25 03:04:36 +000012202 return true;
Evan Cheng2bd122c2007-10-26 01:56:11 +000012203}
12204
Evan Cheng70e10d32012-07-17 06:53:39 +000012205bool X86TargetLowering::isLegalICmpImmediate(int64_t Imm) const {
12206 return Imm == (int32_t)Imm;
12207}
12208
12209bool X86TargetLowering::isLegalAddImmediate(int64_t Imm) const {
Evan Chenga9e13ba2012-07-17 18:54:11 +000012210 // Can also use sub to handle negated immediates.
Evan Cheng70e10d32012-07-17 06:53:39 +000012211 return Imm == (int32_t)Imm;
12212}
12213
Owen Andersone50ed302009-08-10 22:56:29 +000012214bool X86TargetLowering::isTruncateFree(EVT VT1, EVT VT2) const {
Duncan Sands83ec4b62008-06-06 12:08:01 +000012215 if (!VT1.isInteger() || !VT2.isInteger())
Evan Cheng3c3ddb32007-10-29 19:58:20 +000012216 return false;
Duncan Sands83ec4b62008-06-06 12:08:01 +000012217 unsigned NumBits1 = VT1.getSizeInBits();
12218 unsigned NumBits2 = VT2.getSizeInBits();
Evan Cheng260e07e2008-03-20 02:18:41 +000012219 if (NumBits1 <= NumBits2)
Evan Cheng3c3ddb32007-10-29 19:58:20 +000012220 return false;
Dan Gohman377fbc02010-02-25 03:04:36 +000012221 return true;
Evan Cheng3c3ddb32007-10-29 19:58:20 +000012222}
Evan Cheng2bd122c2007-10-26 01:56:11 +000012223
Chris Lattnerdb125cf2011-07-18 04:54:35 +000012224bool X86TargetLowering::isZExtFree(Type *Ty1, Type *Ty2) const {
Dan Gohman349ba492009-04-09 02:06:09 +000012225 // x86-64 implicitly zero-extends 32-bit results in 64-bit registers.
Duncan Sandsb0bc6c32010-02-15 16:12:20 +000012226 return Ty1->isIntegerTy(32) && Ty2->isIntegerTy(64) && Subtarget->is64Bit();
Dan Gohman97121ba2009-04-08 00:15:30 +000012227}
12228
Owen Andersone50ed302009-08-10 22:56:29 +000012229bool X86TargetLowering::isZExtFree(EVT VT1, EVT VT2) const {
Dan Gohman349ba492009-04-09 02:06:09 +000012230 // x86-64 implicitly zero-extends 32-bit results in 64-bit registers.
Owen Anderson825b72b2009-08-11 20:47:22 +000012231 return VT1 == MVT::i32 && VT2 == MVT::i64 && Subtarget->is64Bit();
Dan Gohman97121ba2009-04-08 00:15:30 +000012232}
12233
Evan Cheng2766a472012-12-06 19:13:27 +000012234bool X86TargetLowering::isZExtFree(SDValue Val, EVT VT2) const {
12235 EVT VT1 = Val.getValueType();
12236 if (isZExtFree(VT1, VT2))
12237 return true;
12238
12239 if (Val.getOpcode() != ISD::LOAD)
12240 return false;
12241
12242 if (!VT1.isSimple() || !VT1.isInteger() ||
12243 !VT2.isSimple() || !VT2.isInteger())
12244 return false;
12245
12246 switch (VT1.getSimpleVT().SimpleTy) {
12247 default: break;
12248 case MVT::i8:
12249 case MVT::i16:
12250 case MVT::i32:
12251 // X86 has 8, 16, and 32-bit zero-extending loads.
12252 return true;
12253 }
12254
12255 return false;
12256}
12257
Owen Andersone50ed302009-08-10 22:56:29 +000012258bool X86TargetLowering::isNarrowingProfitable(EVT VT1, EVT VT2) const {
Evan Cheng8b944d32009-05-28 00:35:15 +000012259 // i16 instructions are longer (0x66 prefix) and potentially slower.
Owen Anderson825b72b2009-08-11 20:47:22 +000012260 return !(VT1 == MVT::i32 && VT2 == MVT::i16);
Evan Cheng8b944d32009-05-28 00:35:15 +000012261}
12262
Evan Cheng60c07e12006-07-05 22:17:51 +000012263/// isShuffleMaskLegal - Targets can use this to indicate that they only
12264/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
12265/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
12266/// are assumed to be legal.
12267bool
Eric Christopherfd179292009-08-27 18:07:15 +000012268X86TargetLowering::isShuffleMaskLegal(const SmallVectorImpl<int> &M,
Owen Andersone50ed302009-08-10 22:56:29 +000012269 EVT VT) const {
Eric Christophercff6f852010-04-15 01:40:20 +000012270 // Very little shuffling can be done for 64-bit vectors right now.
Nate Begeman9008ca62009-04-27 18:41:29 +000012271 if (VT.getSizeInBits() == 64)
Craig Topper1dc0fbc2011-12-05 07:27:14 +000012272 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +000012273
Nate Begemana09008b2009-10-19 02:17:23 +000012274 // FIXME: pshufb, blends, shifts.
Nate Begeman9008ca62009-04-27 18:41:29 +000012275 return (VT.getVectorNumElements() == 2 ||
12276 ShuffleVectorSDNode::isSplatMask(&M[0], VT) ||
12277 isMOVLMask(M, VT) ||
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000012278 isSHUFPMask(M, VT, Subtarget->hasFp256()) ||
Nate Begeman9008ca62009-04-27 18:41:29 +000012279 isPSHUFDMask(M, VT) ||
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000012280 isPSHUFHWMask(M, VT, Subtarget->hasInt256()) ||
12281 isPSHUFLWMask(M, VT, Subtarget->hasInt256()) ||
Craig Topper0e2037b2012-01-20 05:53:00 +000012282 isPALIGNRMask(M, VT, Subtarget) ||
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000012283 isUNPCKLMask(M, VT, Subtarget->hasInt256()) ||
12284 isUNPCKHMask(M, VT, Subtarget->hasInt256()) ||
12285 isUNPCKL_v_undef_Mask(M, VT, Subtarget->hasInt256()) ||
12286 isUNPCKH_v_undef_Mask(M, VT, Subtarget->hasInt256()));
Evan Cheng60c07e12006-07-05 22:17:51 +000012287}
12288
Dan Gohman7d8143f2008-04-09 20:09:42 +000012289bool
Nate Begeman5a5ca152009-04-29 05:20:52 +000012290X86TargetLowering::isVectorClearMaskLegal(const SmallVectorImpl<int> &Mask,
Owen Andersone50ed302009-08-10 22:56:29 +000012291 EVT VT) const {
Nate Begeman9008ca62009-04-27 18:41:29 +000012292 unsigned NumElts = VT.getVectorNumElements();
12293 // FIXME: This collection of masks seems suspect.
12294 if (NumElts == 2)
12295 return true;
Craig Topper7a9a28b2012-08-12 02:23:29 +000012296 if (NumElts == 4 && VT.is128BitVector()) {
Nate Begeman9008ca62009-04-27 18:41:29 +000012297 return (isMOVLMask(Mask, VT) ||
12298 isCommutedMOVLMask(Mask, VT, true) ||
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000012299 isSHUFPMask(Mask, VT, Subtarget->hasFp256()) ||
12300 isSHUFPMask(Mask, VT, Subtarget->hasFp256(), /* Commuted */ true));
Evan Cheng60c07e12006-07-05 22:17:51 +000012301 }
12302 return false;
12303}
12304
12305//===----------------------------------------------------------------------===//
12306// X86 Scheduler Hooks
12307//===----------------------------------------------------------------------===//
12308
Michael Liaobe02a902012-11-08 07:28:54 +000012309/// Utility function to emit xbegin specifying the start of an RTM region.
Craig Topper2da36912012-11-11 22:45:02 +000012310static MachineBasicBlock *EmitXBegin(MachineInstr *MI, MachineBasicBlock *MBB,
12311 const TargetInstrInfo *TII) {
Michael Liaobe02a902012-11-08 07:28:54 +000012312 DebugLoc DL = MI->getDebugLoc();
Michael Liaobe02a902012-11-08 07:28:54 +000012313
12314 const BasicBlock *BB = MBB->getBasicBlock();
12315 MachineFunction::iterator I = MBB;
12316 ++I;
12317
12318 // For the v = xbegin(), we generate
12319 //
12320 // thisMBB:
12321 // xbegin sinkMBB
12322 //
12323 // mainMBB:
12324 // eax = -1
12325 //
12326 // sinkMBB:
12327 // v = eax
12328
12329 MachineBasicBlock *thisMBB = MBB;
12330 MachineFunction *MF = MBB->getParent();
12331 MachineBasicBlock *mainMBB = MF->CreateMachineBasicBlock(BB);
12332 MachineBasicBlock *sinkMBB = MF->CreateMachineBasicBlock(BB);
12333 MF->insert(I, mainMBB);
12334 MF->insert(I, sinkMBB);
12335
12336 // Transfer the remainder of BB and its successor edges to sinkMBB.
12337 sinkMBB->splice(sinkMBB->begin(), MBB,
12338 llvm::next(MachineBasicBlock::iterator(MI)), MBB->end());
12339 sinkMBB->transferSuccessorsAndUpdatePHIs(MBB);
12340
12341 // thisMBB:
12342 // xbegin sinkMBB
12343 // # fallthrough to mainMBB
12344 // # abortion to sinkMBB
12345 BuildMI(thisMBB, DL, TII->get(X86::XBEGIN_4)).addMBB(sinkMBB);
12346 thisMBB->addSuccessor(mainMBB);
12347 thisMBB->addSuccessor(sinkMBB);
12348
12349 // mainMBB:
12350 // EAX = -1
12351 BuildMI(mainMBB, DL, TII->get(X86::MOV32ri), X86::EAX).addImm(-1);
12352 mainMBB->addSuccessor(sinkMBB);
12353
12354 // sinkMBB:
12355 // EAX is live into the sinkMBB
12356 sinkMBB->addLiveIn(X86::EAX);
12357 BuildMI(*sinkMBB, sinkMBB->begin(), DL,
12358 TII->get(TargetOpcode::COPY), MI->getOperand(0).getReg())
12359 .addReg(X86::EAX);
12360
12361 MI->eraseFromParent();
12362 return sinkMBB;
12363}
12364
Michael Liaob118a072012-09-20 03:06:15 +000012365// Get CMPXCHG opcode for the specified data type.
12366static unsigned getCmpXChgOpcode(EVT VT) {
12367 switch (VT.getSimpleVT().SimpleTy) {
12368 case MVT::i8: return X86::LCMPXCHG8;
12369 case MVT::i16: return X86::LCMPXCHG16;
12370 case MVT::i32: return X86::LCMPXCHG32;
12371 case MVT::i64: return X86::LCMPXCHG64;
12372 default:
12373 break;
Richard Smith42fc29e2012-04-13 22:47:00 +000012374 }
Michael Liaob118a072012-09-20 03:06:15 +000012375 llvm_unreachable("Invalid operand size!");
Mon P Wang63307c32008-05-05 19:05:59 +000012376}
12377
Michael Liaob118a072012-09-20 03:06:15 +000012378// Get LOAD opcode for the specified data type.
12379static unsigned getLoadOpcode(EVT VT) {
12380 switch (VT.getSimpleVT().SimpleTy) {
12381 case MVT::i8: return X86::MOV8rm;
12382 case MVT::i16: return X86::MOV16rm;
12383 case MVT::i32: return X86::MOV32rm;
12384 case MVT::i64: return X86::MOV64rm;
12385 default:
12386 break;
12387 }
12388 llvm_unreachable("Invalid operand size!");
12389}
12390
12391// Get opcode of the non-atomic one from the specified atomic instruction.
12392static unsigned getNonAtomicOpcode(unsigned Opc) {
12393 switch (Opc) {
12394 case X86::ATOMAND8: return X86::AND8rr;
12395 case X86::ATOMAND16: return X86::AND16rr;
12396 case X86::ATOMAND32: return X86::AND32rr;
12397 case X86::ATOMAND64: return X86::AND64rr;
12398 case X86::ATOMOR8: return X86::OR8rr;
12399 case X86::ATOMOR16: return X86::OR16rr;
12400 case X86::ATOMOR32: return X86::OR32rr;
12401 case X86::ATOMOR64: return X86::OR64rr;
12402 case X86::ATOMXOR8: return X86::XOR8rr;
12403 case X86::ATOMXOR16: return X86::XOR16rr;
12404 case X86::ATOMXOR32: return X86::XOR32rr;
12405 case X86::ATOMXOR64: return X86::XOR64rr;
12406 }
12407 llvm_unreachable("Unhandled atomic-load-op opcode!");
12408}
12409
12410// Get opcode of the non-atomic one from the specified atomic instruction with
12411// extra opcode.
12412static unsigned getNonAtomicOpcodeWithExtraOpc(unsigned Opc,
12413 unsigned &ExtraOpc) {
12414 switch (Opc) {
12415 case X86::ATOMNAND8: ExtraOpc = X86::NOT8r; return X86::AND8rr;
12416 case X86::ATOMNAND16: ExtraOpc = X86::NOT16r; return X86::AND16rr;
12417 case X86::ATOMNAND32: ExtraOpc = X86::NOT32r; return X86::AND32rr;
12418 case X86::ATOMNAND64: ExtraOpc = X86::NOT64r; return X86::AND64rr;
Michael Liaofe87c302012-09-21 03:18:52 +000012419 case X86::ATOMMAX8: ExtraOpc = X86::CMP8rr; return X86::CMOVL32rr;
Michael Liaob118a072012-09-20 03:06:15 +000012420 case X86::ATOMMAX16: ExtraOpc = X86::CMP16rr; return X86::CMOVL16rr;
12421 case X86::ATOMMAX32: ExtraOpc = X86::CMP32rr; return X86::CMOVL32rr;
12422 case X86::ATOMMAX64: ExtraOpc = X86::CMP64rr; return X86::CMOVL64rr;
Michael Liaofe87c302012-09-21 03:18:52 +000012423 case X86::ATOMMIN8: ExtraOpc = X86::CMP8rr; return X86::CMOVG32rr;
Michael Liaob118a072012-09-20 03:06:15 +000012424 case X86::ATOMMIN16: ExtraOpc = X86::CMP16rr; return X86::CMOVG16rr;
12425 case X86::ATOMMIN32: ExtraOpc = X86::CMP32rr; return X86::CMOVG32rr;
12426 case X86::ATOMMIN64: ExtraOpc = X86::CMP64rr; return X86::CMOVG64rr;
Michael Liaofe87c302012-09-21 03:18:52 +000012427 case X86::ATOMUMAX8: ExtraOpc = X86::CMP8rr; return X86::CMOVB32rr;
Michael Liaob118a072012-09-20 03:06:15 +000012428 case X86::ATOMUMAX16: ExtraOpc = X86::CMP16rr; return X86::CMOVB16rr;
12429 case X86::ATOMUMAX32: ExtraOpc = X86::CMP32rr; return X86::CMOVB32rr;
12430 case X86::ATOMUMAX64: ExtraOpc = X86::CMP64rr; return X86::CMOVB64rr;
Michael Liaofe87c302012-09-21 03:18:52 +000012431 case X86::ATOMUMIN8: ExtraOpc = X86::CMP8rr; return X86::CMOVA32rr;
Michael Liaob118a072012-09-20 03:06:15 +000012432 case X86::ATOMUMIN16: ExtraOpc = X86::CMP16rr; return X86::CMOVA16rr;
12433 case X86::ATOMUMIN32: ExtraOpc = X86::CMP32rr; return X86::CMOVA32rr;
12434 case X86::ATOMUMIN64: ExtraOpc = X86::CMP64rr; return X86::CMOVA64rr;
12435 }
12436 llvm_unreachable("Unhandled atomic-load-op opcode!");
12437}
12438
12439// Get opcode of the non-atomic one from the specified atomic instruction for
12440// 64-bit data type on 32-bit target.
12441static unsigned getNonAtomic6432Opcode(unsigned Opc, unsigned &HiOpc) {
12442 switch (Opc) {
12443 case X86::ATOMAND6432: HiOpc = X86::AND32rr; return X86::AND32rr;
12444 case X86::ATOMOR6432: HiOpc = X86::OR32rr; return X86::OR32rr;
12445 case X86::ATOMXOR6432: HiOpc = X86::XOR32rr; return X86::XOR32rr;
12446 case X86::ATOMADD6432: HiOpc = X86::ADC32rr; return X86::ADD32rr;
12447 case X86::ATOMSUB6432: HiOpc = X86::SBB32rr; return X86::SUB32rr;
12448 case X86::ATOMSWAP6432: HiOpc = X86::MOV32rr; return X86::MOV32rr;
Michael Liaoe5e8f762012-09-25 18:08:13 +000012449 case X86::ATOMMAX6432: HiOpc = X86::SETLr; return X86::SETLr;
12450 case X86::ATOMMIN6432: HiOpc = X86::SETGr; return X86::SETGr;
12451 case X86::ATOMUMAX6432: HiOpc = X86::SETBr; return X86::SETBr;
12452 case X86::ATOMUMIN6432: HiOpc = X86::SETAr; return X86::SETAr;
Michael Liaob118a072012-09-20 03:06:15 +000012453 }
12454 llvm_unreachable("Unhandled atomic-load-op opcode!");
12455}
12456
12457// Get opcode of the non-atomic one from the specified atomic instruction for
12458// 64-bit data type on 32-bit target with extra opcode.
12459static unsigned getNonAtomic6432OpcodeWithExtraOpc(unsigned Opc,
12460 unsigned &HiOpc,
12461 unsigned &ExtraOpc) {
12462 switch (Opc) {
12463 case X86::ATOMNAND6432:
12464 ExtraOpc = X86::NOT32r;
12465 HiOpc = X86::AND32rr;
12466 return X86::AND32rr;
12467 }
12468 llvm_unreachable("Unhandled atomic-load-op opcode!");
12469}
12470
12471// Get pseudo CMOV opcode from the specified data type.
12472static unsigned getPseudoCMOVOpc(EVT VT) {
12473 switch (VT.getSimpleVT().SimpleTy) {
Michael Liaofe87c302012-09-21 03:18:52 +000012474 case MVT::i8: return X86::CMOV_GR8;
Michael Liaob118a072012-09-20 03:06:15 +000012475 case MVT::i16: return X86::CMOV_GR16;
12476 case MVT::i32: return X86::CMOV_GR32;
12477 default:
12478 break;
12479 }
12480 llvm_unreachable("Unknown CMOV opcode!");
12481}
12482
12483// EmitAtomicLoadArith - emit the code sequence for pseudo atomic instructions.
12484// They will be translated into a spin-loop or compare-exchange loop from
12485//
12486// ...
12487// dst = atomic-fetch-op MI.addr, MI.val
12488// ...
12489//
12490// to
12491//
12492// ...
12493// EAX = LOAD MI.addr
12494// loop:
12495// t1 = OP MI.val, EAX
12496// LCMPXCHG [MI.addr], t1, [EAX is implicitly used & defined]
12497// JNE loop
12498// sink:
12499// dst = EAX
12500// ...
Mon P Wang63307c32008-05-05 19:05:59 +000012501MachineBasicBlock *
Michael Liaob118a072012-09-20 03:06:15 +000012502X86TargetLowering::EmitAtomicLoadArith(MachineInstr *MI,
12503 MachineBasicBlock *MBB) const {
12504 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
12505 DebugLoc DL = MI->getDebugLoc();
12506
12507 MachineFunction *MF = MBB->getParent();
12508 MachineRegisterInfo &MRI = MF->getRegInfo();
12509
12510 const BasicBlock *BB = MBB->getBasicBlock();
12511 MachineFunction::iterator I = MBB;
12512 ++I;
12513
12514 assert(MI->getNumOperands() <= X86::AddrNumOperands + 2 &&
12515 "Unexpected number of operands");
12516
12517 assert(MI->hasOneMemOperand() &&
12518 "Expected atomic-load-op to have one memoperand");
12519
12520 // Memory Reference
12521 MachineInstr::mmo_iterator MMOBegin = MI->memoperands_begin();
12522 MachineInstr::mmo_iterator MMOEnd = MI->memoperands_end();
12523
12524 unsigned DstReg, SrcReg;
12525 unsigned MemOpndSlot;
12526
12527 unsigned CurOp = 0;
12528
12529 DstReg = MI->getOperand(CurOp++).getReg();
12530 MemOpndSlot = CurOp;
12531 CurOp += X86::AddrNumOperands;
12532 SrcReg = MI->getOperand(CurOp++).getReg();
12533
12534 const TargetRegisterClass *RC = MRI.getRegClass(DstReg);
Craig Topperf4d25a22012-09-30 19:49:56 +000012535 MVT::SimpleValueType VT = *RC->vt_begin();
Michael Liaob118a072012-09-20 03:06:15 +000012536 unsigned AccPhyReg = getX86SubSuperRegister(X86::EAX, VT);
12537
12538 unsigned LCMPXCHGOpc = getCmpXChgOpcode(VT);
12539 unsigned LOADOpc = getLoadOpcode(VT);
12540
12541 // For the atomic load-arith operator, we generate
12542 //
12543 // thisMBB:
12544 // EAX = LOAD [MI.addr]
12545 // mainMBB:
12546 // t1 = OP MI.val, EAX
12547 // LCMPXCHG [MI.addr], t1, [EAX is implicitly used & defined]
12548 // JNE mainMBB
12549 // sinkMBB:
12550
12551 MachineBasicBlock *thisMBB = MBB;
12552 MachineBasicBlock *mainMBB = MF->CreateMachineBasicBlock(BB);
12553 MachineBasicBlock *sinkMBB = MF->CreateMachineBasicBlock(BB);
12554 MF->insert(I, mainMBB);
12555 MF->insert(I, sinkMBB);
12556
12557 MachineInstrBuilder MIB;
12558
12559 // Transfer the remainder of BB and its successor edges to sinkMBB.
12560 sinkMBB->splice(sinkMBB->begin(), MBB,
12561 llvm::next(MachineBasicBlock::iterator(MI)), MBB->end());
12562 sinkMBB->transferSuccessorsAndUpdatePHIs(MBB);
12563
12564 // thisMBB:
12565 MIB = BuildMI(thisMBB, DL, TII->get(LOADOpc), AccPhyReg);
12566 for (unsigned i = 0; i < X86::AddrNumOperands; ++i)
12567 MIB.addOperand(MI->getOperand(MemOpndSlot + i));
12568 MIB.setMemRefs(MMOBegin, MMOEnd);
12569
12570 thisMBB->addSuccessor(mainMBB);
12571
12572 // mainMBB:
12573 MachineBasicBlock *origMainMBB = mainMBB;
12574 mainMBB->addLiveIn(AccPhyReg);
12575
12576 // Copy AccPhyReg as it is used more than once.
12577 unsigned AccReg = MRI.createVirtualRegister(RC);
12578 BuildMI(mainMBB, DL, TII->get(TargetOpcode::COPY), AccReg)
12579 .addReg(AccPhyReg);
12580
12581 unsigned t1 = MRI.createVirtualRegister(RC);
12582 unsigned Opc = MI->getOpcode();
12583 switch (Opc) {
12584 default:
12585 llvm_unreachable("Unhandled atomic-load-op opcode!");
12586 case X86::ATOMAND8:
12587 case X86::ATOMAND16:
12588 case X86::ATOMAND32:
12589 case X86::ATOMAND64:
12590 case X86::ATOMOR8:
12591 case X86::ATOMOR16:
12592 case X86::ATOMOR32:
12593 case X86::ATOMOR64:
12594 case X86::ATOMXOR8:
12595 case X86::ATOMXOR16:
12596 case X86::ATOMXOR32:
12597 case X86::ATOMXOR64: {
12598 unsigned ARITHOpc = getNonAtomicOpcode(Opc);
12599 BuildMI(mainMBB, DL, TII->get(ARITHOpc), t1).addReg(SrcReg)
12600 .addReg(AccReg);
12601 break;
12602 }
12603 case X86::ATOMNAND8:
12604 case X86::ATOMNAND16:
12605 case X86::ATOMNAND32:
12606 case X86::ATOMNAND64: {
12607 unsigned t2 = MRI.createVirtualRegister(RC);
12608 unsigned NOTOpc;
12609 unsigned ANDOpc = getNonAtomicOpcodeWithExtraOpc(Opc, NOTOpc);
12610 BuildMI(mainMBB, DL, TII->get(ANDOpc), t2).addReg(SrcReg)
12611 .addReg(AccReg);
12612 BuildMI(mainMBB, DL, TII->get(NOTOpc), t1).addReg(t2);
12613 break;
12614 }
Michael Liao08382492012-09-21 03:00:17 +000012615 case X86::ATOMMAX8:
Michael Liaob118a072012-09-20 03:06:15 +000012616 case X86::ATOMMAX16:
12617 case X86::ATOMMAX32:
12618 case X86::ATOMMAX64:
Michael Liaofe87c302012-09-21 03:18:52 +000012619 case X86::ATOMMIN8:
Michael Liaob118a072012-09-20 03:06:15 +000012620 case X86::ATOMMIN16:
12621 case X86::ATOMMIN32:
12622 case X86::ATOMMIN64:
Michael Liaofe87c302012-09-21 03:18:52 +000012623 case X86::ATOMUMAX8:
Michael Liaob118a072012-09-20 03:06:15 +000012624 case X86::ATOMUMAX16:
12625 case X86::ATOMUMAX32:
12626 case X86::ATOMUMAX64:
Michael Liaofe87c302012-09-21 03:18:52 +000012627 case X86::ATOMUMIN8:
Michael Liaob118a072012-09-20 03:06:15 +000012628 case X86::ATOMUMIN16:
12629 case X86::ATOMUMIN32:
12630 case X86::ATOMUMIN64: {
12631 unsigned CMPOpc;
12632 unsigned CMOVOpc = getNonAtomicOpcodeWithExtraOpc(Opc, CMPOpc);
12633
12634 BuildMI(mainMBB, DL, TII->get(CMPOpc))
12635 .addReg(SrcReg)
12636 .addReg(AccReg);
12637
12638 if (Subtarget->hasCMov()) {
Michael Liaofe87c302012-09-21 03:18:52 +000012639 if (VT != MVT::i8) {
12640 // Native support
12641 BuildMI(mainMBB, DL, TII->get(CMOVOpc), t1)
12642 .addReg(SrcReg)
12643 .addReg(AccReg);
12644 } else {
12645 // Promote i8 to i32 to use CMOV32
12646 const TargetRegisterClass *RC32 = getRegClassFor(MVT::i32);
12647 unsigned SrcReg32 = MRI.createVirtualRegister(RC32);
12648 unsigned AccReg32 = MRI.createVirtualRegister(RC32);
12649 unsigned t2 = MRI.createVirtualRegister(RC32);
12650
12651 unsigned Undef = MRI.createVirtualRegister(RC32);
12652 BuildMI(mainMBB, DL, TII->get(TargetOpcode::IMPLICIT_DEF), Undef);
12653
12654 BuildMI(mainMBB, DL, TII->get(TargetOpcode::INSERT_SUBREG), SrcReg32)
12655 .addReg(Undef)
12656 .addReg(SrcReg)
12657 .addImm(X86::sub_8bit);
12658 BuildMI(mainMBB, DL, TII->get(TargetOpcode::INSERT_SUBREG), AccReg32)
12659 .addReg(Undef)
12660 .addReg(AccReg)
12661 .addImm(X86::sub_8bit);
12662
12663 BuildMI(mainMBB, DL, TII->get(CMOVOpc), t2)
12664 .addReg(SrcReg32)
12665 .addReg(AccReg32);
12666
12667 BuildMI(mainMBB, DL, TII->get(TargetOpcode::COPY), t1)
12668 .addReg(t2, 0, X86::sub_8bit);
12669 }
Michael Liaob118a072012-09-20 03:06:15 +000012670 } else {
12671 // Use pseudo select and lower them.
Michael Liaofe87c302012-09-21 03:18:52 +000012672 assert((VT == MVT::i8 || VT == MVT::i16 || VT == MVT::i32) &&
Michael Liaob118a072012-09-20 03:06:15 +000012673 "Invalid atomic-load-op transformation!");
12674 unsigned SelOpc = getPseudoCMOVOpc(VT);
12675 X86::CondCode CC = X86::getCondFromCMovOpc(CMOVOpc);
12676 assert(CC != X86::COND_INVALID && "Invalid atomic-load-op transformation!");
12677 MIB = BuildMI(mainMBB, DL, TII->get(SelOpc), t1)
12678 .addReg(SrcReg).addReg(AccReg)
12679 .addImm(CC);
12680 mainMBB = EmitLoweredSelect(MIB, mainMBB);
12681 }
12682 break;
12683 }
12684 }
12685
12686 // Copy AccPhyReg back from virtual register.
12687 BuildMI(mainMBB, DL, TII->get(TargetOpcode::COPY), AccPhyReg)
12688 .addReg(AccReg);
12689
12690 MIB = BuildMI(mainMBB, DL, TII->get(LCMPXCHGOpc));
12691 for (unsigned i = 0; i < X86::AddrNumOperands; ++i)
12692 MIB.addOperand(MI->getOperand(MemOpndSlot + i));
12693 MIB.addReg(t1);
12694 MIB.setMemRefs(MMOBegin, MMOEnd);
12695
12696 BuildMI(mainMBB, DL, TII->get(X86::JNE_4)).addMBB(origMainMBB);
12697
12698 mainMBB->addSuccessor(origMainMBB);
12699 mainMBB->addSuccessor(sinkMBB);
12700
12701 // sinkMBB:
12702 sinkMBB->addLiveIn(AccPhyReg);
12703
12704 BuildMI(*sinkMBB, sinkMBB->begin(), DL,
12705 TII->get(TargetOpcode::COPY), DstReg)
12706 .addReg(AccPhyReg);
12707
12708 MI->eraseFromParent();
12709 return sinkMBB;
12710}
12711
12712// EmitAtomicLoadArith6432 - emit the code sequence for pseudo atomic
12713// instructions. They will be translated into a spin-loop or compare-exchange
12714// loop from
12715//
12716// ...
12717// dst = atomic-fetch-op MI.addr, MI.val
12718// ...
12719//
12720// to
12721//
12722// ...
12723// EAX = LOAD [MI.addr + 0]
12724// EDX = LOAD [MI.addr + 4]
12725// loop:
12726// EBX = OP MI.val.lo, EAX
12727// ECX = OP MI.val.hi, EDX
12728// LCMPXCHG8B [MI.addr], [ECX:EBX & EDX:EAX are implicitly used and EDX:EAX is implicitly defined]
12729// JNE loop
12730// sink:
12731// dst = EDX:EAX
12732// ...
12733MachineBasicBlock *
12734X86TargetLowering::EmitAtomicLoadArith6432(MachineInstr *MI,
12735 MachineBasicBlock *MBB) const {
12736 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
12737 DebugLoc DL = MI->getDebugLoc();
12738
12739 MachineFunction *MF = MBB->getParent();
12740 MachineRegisterInfo &MRI = MF->getRegInfo();
12741
12742 const BasicBlock *BB = MBB->getBasicBlock();
12743 MachineFunction::iterator I = MBB;
12744 ++I;
12745
12746 assert(MI->getNumOperands() <= X86::AddrNumOperands + 4 &&
12747 "Unexpected number of operands");
12748
12749 assert(MI->hasOneMemOperand() &&
12750 "Expected atomic-load-op32 to have one memoperand");
12751
12752 // Memory Reference
12753 MachineInstr::mmo_iterator MMOBegin = MI->memoperands_begin();
12754 MachineInstr::mmo_iterator MMOEnd = MI->memoperands_end();
12755
12756 unsigned DstLoReg, DstHiReg;
12757 unsigned SrcLoReg, SrcHiReg;
12758 unsigned MemOpndSlot;
12759
12760 unsigned CurOp = 0;
12761
12762 DstLoReg = MI->getOperand(CurOp++).getReg();
12763 DstHiReg = MI->getOperand(CurOp++).getReg();
12764 MemOpndSlot = CurOp;
12765 CurOp += X86::AddrNumOperands;
12766 SrcLoReg = MI->getOperand(CurOp++).getReg();
12767 SrcHiReg = MI->getOperand(CurOp++).getReg();
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012768
Craig Topperc9099502012-04-20 06:31:50 +000012769 const TargetRegisterClass *RC = &X86::GR32RegClass;
Michael Liaoe5e8f762012-09-25 18:08:13 +000012770 const TargetRegisterClass *RC8 = &X86::GR8RegClass;
Scott Michelfdc40a02009-02-17 22:15:04 +000012771
Michael Liaob118a072012-09-20 03:06:15 +000012772 unsigned LCMPXCHGOpc = X86::LCMPXCHG8B;
12773 unsigned LOADOpc = X86::MOV32rm;
Scott Michelfdc40a02009-02-17 22:15:04 +000012774
Michael Liaob118a072012-09-20 03:06:15 +000012775 // For the atomic load-arith operator, we generate
Mon P Wang63307c32008-05-05 19:05:59 +000012776 //
Michael Liaob118a072012-09-20 03:06:15 +000012777 // thisMBB:
12778 // EAX = LOAD [MI.addr + 0]
12779 // EDX = LOAD [MI.addr + 4]
12780 // mainMBB:
12781 // EBX = OP MI.vallo, EAX
12782 // ECX = OP MI.valhi, EDX
12783 // LCMPXCHG8B [MI.addr], [ECX:EBX & EDX:EAX are implicitly used and EDX:EAX is implicitly defined]
12784 // JNE mainMBB
12785 // sinkMBB:
Scott Michelfdc40a02009-02-17 22:15:04 +000012786
Mon P Wang63307c32008-05-05 19:05:59 +000012787 MachineBasicBlock *thisMBB = MBB;
Michael Liaob118a072012-09-20 03:06:15 +000012788 MachineBasicBlock *mainMBB = MF->CreateMachineBasicBlock(BB);
12789 MachineBasicBlock *sinkMBB = MF->CreateMachineBasicBlock(BB);
12790 MF->insert(I, mainMBB);
12791 MF->insert(I, sinkMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000012792
Michael Liaob118a072012-09-20 03:06:15 +000012793 MachineInstrBuilder MIB;
Scott Michelfdc40a02009-02-17 22:15:04 +000012794
Michael Liaob118a072012-09-20 03:06:15 +000012795 // Transfer the remainder of BB and its successor edges to sinkMBB.
12796 sinkMBB->splice(sinkMBB->begin(), MBB,
12797 llvm::next(MachineBasicBlock::iterator(MI)), MBB->end());
12798 sinkMBB->transferSuccessorsAndUpdatePHIs(MBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000012799
Michael Liaob118a072012-09-20 03:06:15 +000012800 // thisMBB:
12801 // Lo
12802 MIB = BuildMI(thisMBB, DL, TII->get(LOADOpc), X86::EAX);
12803 for (unsigned i = 0; i < X86::AddrNumOperands; ++i)
12804 MIB.addOperand(MI->getOperand(MemOpndSlot + i));
12805 MIB.setMemRefs(MMOBegin, MMOEnd);
12806 // Hi
12807 MIB = BuildMI(thisMBB, DL, TII->get(LOADOpc), X86::EDX);
12808 for (unsigned i = 0; i < X86::AddrNumOperands; ++i) {
Evan Chenga395f4d2012-10-11 00:15:48 +000012809 if (i == X86::AddrDisp)
Michael Liaob118a072012-09-20 03:06:15 +000012810 MIB.addDisp(MI->getOperand(MemOpndSlot + i), 4); // 4 == sizeof(i32)
Evan Chenga395f4d2012-10-11 00:15:48 +000012811 else
Michael Liaob118a072012-09-20 03:06:15 +000012812 MIB.addOperand(MI->getOperand(MemOpndSlot + i));
12813 }
12814 MIB.setMemRefs(MMOBegin, MMOEnd);
Scott Michelfdc40a02009-02-17 22:15:04 +000012815
Michael Liaob118a072012-09-20 03:06:15 +000012816 thisMBB->addSuccessor(mainMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000012817
Michael Liaob118a072012-09-20 03:06:15 +000012818 // mainMBB:
12819 MachineBasicBlock *origMainMBB = mainMBB;
12820 mainMBB->addLiveIn(X86::EAX);
12821 mainMBB->addLiveIn(X86::EDX);
Scott Michelfdc40a02009-02-17 22:15:04 +000012822
Michael Liaob118a072012-09-20 03:06:15 +000012823 // Copy EDX:EAX as they are used more than once.
12824 unsigned LoReg = MRI.createVirtualRegister(RC);
12825 unsigned HiReg = MRI.createVirtualRegister(RC);
12826 BuildMI(mainMBB, DL, TII->get(TargetOpcode::COPY), LoReg).addReg(X86::EAX);
12827 BuildMI(mainMBB, DL, TII->get(TargetOpcode::COPY), HiReg).addReg(X86::EDX);
Mon P Wangab3e7472008-05-05 22:56:23 +000012828
Michael Liaob118a072012-09-20 03:06:15 +000012829 unsigned t1L = MRI.createVirtualRegister(RC);
12830 unsigned t1H = MRI.createVirtualRegister(RC);
Scott Michelfdc40a02009-02-17 22:15:04 +000012831
Michael Liaob118a072012-09-20 03:06:15 +000012832 unsigned Opc = MI->getOpcode();
12833 switch (Opc) {
12834 default:
12835 llvm_unreachable("Unhandled atomic-load-op6432 opcode!");
12836 case X86::ATOMAND6432:
12837 case X86::ATOMOR6432:
12838 case X86::ATOMXOR6432:
12839 case X86::ATOMADD6432:
12840 case X86::ATOMSUB6432: {
12841 unsigned HiOpc;
12842 unsigned LoOpc = getNonAtomic6432Opcode(Opc, HiOpc);
Michael Liaodd3383f2012-11-12 06:49:17 +000012843 BuildMI(mainMBB, DL, TII->get(LoOpc), t1L).addReg(LoReg).addReg(SrcLoReg);
12844 BuildMI(mainMBB, DL, TII->get(HiOpc), t1H).addReg(HiReg).addReg(SrcHiReg);
Michael Liaob118a072012-09-20 03:06:15 +000012845 break;
12846 }
12847 case X86::ATOMNAND6432: {
12848 unsigned HiOpc, NOTOpc;
12849 unsigned LoOpc = getNonAtomic6432OpcodeWithExtraOpc(Opc, HiOpc, NOTOpc);
12850 unsigned t2L = MRI.createVirtualRegister(RC);
12851 unsigned t2H = MRI.createVirtualRegister(RC);
12852 BuildMI(mainMBB, DL, TII->get(LoOpc), t2L).addReg(SrcLoReg).addReg(LoReg);
12853 BuildMI(mainMBB, DL, TII->get(HiOpc), t2H).addReg(SrcHiReg).addReg(HiReg);
12854 BuildMI(mainMBB, DL, TII->get(NOTOpc), t1L).addReg(t2L);
12855 BuildMI(mainMBB, DL, TII->get(NOTOpc), t1H).addReg(t2H);
12856 break;
12857 }
Michael Liaoe5e8f762012-09-25 18:08:13 +000012858 case X86::ATOMMAX6432:
12859 case X86::ATOMMIN6432:
12860 case X86::ATOMUMAX6432:
12861 case X86::ATOMUMIN6432: {
12862 unsigned HiOpc;
12863 unsigned LoOpc = getNonAtomic6432Opcode(Opc, HiOpc);
12864 unsigned cL = MRI.createVirtualRegister(RC8);
12865 unsigned cH = MRI.createVirtualRegister(RC8);
12866 unsigned cL32 = MRI.createVirtualRegister(RC);
12867 unsigned cH32 = MRI.createVirtualRegister(RC);
12868 unsigned cc = MRI.createVirtualRegister(RC);
12869 // cl := cmp src_lo, lo
12870 BuildMI(mainMBB, DL, TII->get(X86::CMP32rr))
12871 .addReg(SrcLoReg).addReg(LoReg);
12872 BuildMI(mainMBB, DL, TII->get(LoOpc), cL);
12873 BuildMI(mainMBB, DL, TII->get(X86::MOVZX32rr8), cL32).addReg(cL);
12874 // ch := cmp src_hi, hi
12875 BuildMI(mainMBB, DL, TII->get(X86::CMP32rr))
12876 .addReg(SrcHiReg).addReg(HiReg);
12877 BuildMI(mainMBB, DL, TII->get(HiOpc), cH);
12878 BuildMI(mainMBB, DL, TII->get(X86::MOVZX32rr8), cH32).addReg(cH);
12879 // cc := if (src_hi == hi) ? cl : ch;
12880 if (Subtarget->hasCMov()) {
12881 BuildMI(mainMBB, DL, TII->get(X86::CMOVE32rr), cc)
12882 .addReg(cH32).addReg(cL32);
12883 } else {
12884 MIB = BuildMI(mainMBB, DL, TII->get(X86::CMOV_GR32), cc)
12885 .addReg(cH32).addReg(cL32)
12886 .addImm(X86::COND_E);
12887 mainMBB = EmitLoweredSelect(MIB, mainMBB);
12888 }
12889 BuildMI(mainMBB, DL, TII->get(X86::TEST32rr)).addReg(cc).addReg(cc);
12890 if (Subtarget->hasCMov()) {
12891 BuildMI(mainMBB, DL, TII->get(X86::CMOVNE32rr), t1L)
12892 .addReg(SrcLoReg).addReg(LoReg);
12893 BuildMI(mainMBB, DL, TII->get(X86::CMOVNE32rr), t1H)
12894 .addReg(SrcHiReg).addReg(HiReg);
12895 } else {
12896 MIB = BuildMI(mainMBB, DL, TII->get(X86::CMOV_GR32), t1L)
12897 .addReg(SrcLoReg).addReg(LoReg)
12898 .addImm(X86::COND_NE);
12899 mainMBB = EmitLoweredSelect(MIB, mainMBB);
12900 MIB = BuildMI(mainMBB, DL, TII->get(X86::CMOV_GR32), t1H)
12901 .addReg(SrcHiReg).addReg(HiReg)
12902 .addImm(X86::COND_NE);
12903 mainMBB = EmitLoweredSelect(MIB, mainMBB);
12904 }
12905 break;
12906 }
Michael Liaob118a072012-09-20 03:06:15 +000012907 case X86::ATOMSWAP6432: {
12908 unsigned HiOpc;
12909 unsigned LoOpc = getNonAtomic6432Opcode(Opc, HiOpc);
12910 BuildMI(mainMBB, DL, TII->get(LoOpc), t1L).addReg(SrcLoReg);
12911 BuildMI(mainMBB, DL, TII->get(HiOpc), t1H).addReg(SrcHiReg);
12912 break;
12913 }
12914 }
Mon P Wang63307c32008-05-05 19:05:59 +000012915
Michael Liaob118a072012-09-20 03:06:15 +000012916 // Copy EDX:EAX back from HiReg:LoReg
12917 BuildMI(mainMBB, DL, TII->get(TargetOpcode::COPY), X86::EAX).addReg(LoReg);
12918 BuildMI(mainMBB, DL, TII->get(TargetOpcode::COPY), X86::EDX).addReg(HiReg);
12919 // Copy ECX:EBX from t1H:t1L
12920 BuildMI(mainMBB, DL, TII->get(TargetOpcode::COPY), X86::EBX).addReg(t1L);
12921 BuildMI(mainMBB, DL, TII->get(TargetOpcode::COPY), X86::ECX).addReg(t1H);
Mon P Wangab3e7472008-05-05 22:56:23 +000012922
Michael Liaob118a072012-09-20 03:06:15 +000012923 MIB = BuildMI(mainMBB, DL, TII->get(LCMPXCHGOpc));
12924 for (unsigned i = 0; i < X86::AddrNumOperands; ++i)
12925 MIB.addOperand(MI->getOperand(MemOpndSlot + i));
12926 MIB.setMemRefs(MMOBegin, MMOEnd);
Mon P Wang63307c32008-05-05 19:05:59 +000012927
Michael Liaob118a072012-09-20 03:06:15 +000012928 BuildMI(mainMBB, DL, TII->get(X86::JNE_4)).addMBB(origMainMBB);
Mon P Wang63307c32008-05-05 19:05:59 +000012929
Michael Liaob118a072012-09-20 03:06:15 +000012930 mainMBB->addSuccessor(origMainMBB);
12931 mainMBB->addSuccessor(sinkMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000012932
Michael Liaob118a072012-09-20 03:06:15 +000012933 // sinkMBB:
12934 sinkMBB->addLiveIn(X86::EAX);
12935 sinkMBB->addLiveIn(X86::EDX);
Scott Michelfdc40a02009-02-17 22:15:04 +000012936
Michael Liaob118a072012-09-20 03:06:15 +000012937 BuildMI(*sinkMBB, sinkMBB->begin(), DL,
12938 TII->get(TargetOpcode::COPY), DstLoReg)
12939 .addReg(X86::EAX);
12940 BuildMI(*sinkMBB, sinkMBB->begin(), DL,
12941 TII->get(TargetOpcode::COPY), DstHiReg)
12942 .addReg(X86::EDX);
Mon P Wang63307c32008-05-05 19:05:59 +000012943
Michael Liaob118a072012-09-20 03:06:15 +000012944 MI->eraseFromParent();
12945 return sinkMBB;
Mon P Wang63307c32008-05-05 19:05:59 +000012946}
12947
Eric Christopherf83a5de2009-08-27 18:08:16 +000012948// FIXME: When we get size specific XMM0 registers, i.e. XMM0_V16I8
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000012949// or XMM0_V32I8 in AVX all of this code can be replaced with that
12950// in the .td file.
Craig Topper8cb8c812012-11-10 09:02:47 +000012951static MachineBasicBlock *EmitPCMPSTRM(MachineInstr *MI, MachineBasicBlock *BB,
12952 const TargetInstrInfo *TII) {
Eric Christopherb120ab42009-08-18 22:50:32 +000012953 unsigned Opc;
Craig Topper8aae8dd2012-11-10 08:57:41 +000012954 switch (MI->getOpcode()) {
12955 default: llvm_unreachable("illegal opcode!");
12956 case X86::PCMPISTRM128REG: Opc = X86::PCMPISTRM128rr; break;
12957 case X86::VPCMPISTRM128REG: Opc = X86::VPCMPISTRM128rr; break;
12958 case X86::PCMPISTRM128MEM: Opc = X86::PCMPISTRM128rm; break;
12959 case X86::VPCMPISTRM128MEM: Opc = X86::VPCMPISTRM128rm; break;
12960 case X86::PCMPESTRM128REG: Opc = X86::PCMPESTRM128rr; break;
12961 case X86::VPCMPESTRM128REG: Opc = X86::VPCMPESTRM128rr; break;
12962 case X86::PCMPESTRM128MEM: Opc = X86::PCMPESTRM128rm; break;
12963 case X86::VPCMPESTRM128MEM: Opc = X86::VPCMPESTRM128rm; break;
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000012964 }
Eric Christopherb120ab42009-08-18 22:50:32 +000012965
Craig Topper8aae8dd2012-11-10 08:57:41 +000012966 DebugLoc dl = MI->getDebugLoc();
Eric Christopher41c902f2010-11-30 08:20:21 +000012967 MachineInstrBuilder MIB = BuildMI(*BB, MI, dl, TII->get(Opc));
Craig Topper8aae8dd2012-11-10 08:57:41 +000012968
Craig Topper52ea2452012-11-10 09:25:36 +000012969 unsigned NumArgs = MI->getNumOperands();
12970 for (unsigned i = 1; i < NumArgs; ++i) {
12971 MachineOperand &Op = MI->getOperand(i);
Eric Christopherb120ab42009-08-18 22:50:32 +000012972 if (!(Op.isReg() && Op.isImplicit()))
12973 MIB.addOperand(Op);
12974 }
Craig Topper8aae8dd2012-11-10 08:57:41 +000012975 if (MI->hasOneMemOperand())
Craig Topper9c7ae012012-11-10 01:23:36 +000012976 MIB->setMemRefs(MI->memoperands_begin(), MI->memoperands_end());
12977
Bruno Cardoso Lopes5affa512011-08-31 03:04:09 +000012978 BuildMI(*BB, MI, dl,
Craig Topper638aa682012-08-05 00:17:48 +000012979 TII->get(TargetOpcode::COPY), MI->getOperand(0).getReg())
Eric Christopherb120ab42009-08-18 22:50:32 +000012980 .addReg(X86::XMM0);
12981
Dan Gohman14152b42010-07-06 20:24:04 +000012982 MI->eraseFromParent();
Eric Christopherb120ab42009-08-18 22:50:32 +000012983 return BB;
12984}
12985
Craig Topper9c7ae012012-11-10 01:23:36 +000012986// FIXME: Custom handling because TableGen doesn't support multiple implicit
12987// defs in an instruction pattern
Craig Topper8cb8c812012-11-10 09:02:47 +000012988static MachineBasicBlock *EmitPCMPSTRI(MachineInstr *MI, MachineBasicBlock *BB,
12989 const TargetInstrInfo *TII) {
Craig Topper9c7ae012012-11-10 01:23:36 +000012990 unsigned Opc;
Craig Topper8aae8dd2012-11-10 08:57:41 +000012991 switch (MI->getOpcode()) {
12992 default: llvm_unreachable("illegal opcode!");
12993 case X86::PCMPISTRIREG: Opc = X86::PCMPISTRIrr; break;
12994 case X86::VPCMPISTRIREG: Opc = X86::VPCMPISTRIrr; break;
12995 case X86::PCMPISTRIMEM: Opc = X86::PCMPISTRIrm; break;
12996 case X86::VPCMPISTRIMEM: Opc = X86::VPCMPISTRIrm; break;
12997 case X86::PCMPESTRIREG: Opc = X86::PCMPESTRIrr; break;
12998 case X86::VPCMPESTRIREG: Opc = X86::VPCMPESTRIrr; break;
12999 case X86::PCMPESTRIMEM: Opc = X86::PCMPESTRIrm; break;
13000 case X86::VPCMPESTRIMEM: Opc = X86::VPCMPESTRIrm; break;
Craig Topper9c7ae012012-11-10 01:23:36 +000013001 }
13002
Craig Topper8aae8dd2012-11-10 08:57:41 +000013003 DebugLoc dl = MI->getDebugLoc();
Craig Topper9c7ae012012-11-10 01:23:36 +000013004 MachineInstrBuilder MIB = BuildMI(*BB, MI, dl, TII->get(Opc));
Craig Topper8aae8dd2012-11-10 08:57:41 +000013005
Craig Topper52ea2452012-11-10 09:25:36 +000013006 unsigned NumArgs = MI->getNumOperands(); // remove the results
13007 for (unsigned i = 1; i < NumArgs; ++i) {
13008 MachineOperand &Op = MI->getOperand(i);
Craig Topper9c7ae012012-11-10 01:23:36 +000013009 if (!(Op.isReg() && Op.isImplicit()))
13010 MIB.addOperand(Op);
13011 }
Craig Topper8aae8dd2012-11-10 08:57:41 +000013012 if (MI->hasOneMemOperand())
Craig Topper9c7ae012012-11-10 01:23:36 +000013013 MIB->setMemRefs(MI->memoperands_begin(), MI->memoperands_end());
13014
13015 BuildMI(*BB, MI, dl,
13016 TII->get(TargetOpcode::COPY), MI->getOperand(0).getReg())
13017 .addReg(X86::ECX);
13018
13019 MI->eraseFromParent();
13020 return BB;
13021}
13022
Craig Topper2da36912012-11-11 22:45:02 +000013023static MachineBasicBlock * EmitMonitor(MachineInstr *MI, MachineBasicBlock *BB,
13024 const TargetInstrInfo *TII,
13025 const X86Subtarget* Subtarget) {
Eric Christopher228232b2010-11-30 07:20:12 +000013026 DebugLoc dl = MI->getDebugLoc();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013027
Eric Christopher228232b2010-11-30 07:20:12 +000013028 // Address into RAX/EAX, other two args into ECX, EDX.
13029 unsigned MemOpc = Subtarget->is64Bit() ? X86::LEA64r : X86::LEA32r;
13030 unsigned MemReg = Subtarget->is64Bit() ? X86::RAX : X86::EAX;
13031 MachineInstrBuilder MIB = BuildMI(*BB, MI, dl, TII->get(MemOpc), MemReg);
13032 for (int i = 0; i < X86::AddrNumOperands; ++i)
Eric Christopher82be2202010-11-30 08:10:28 +000013033 MIB.addOperand(MI->getOperand(i));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013034
Eric Christopher228232b2010-11-30 07:20:12 +000013035 unsigned ValOps = X86::AddrNumOperands;
13036 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::ECX)
13037 .addReg(MI->getOperand(ValOps).getReg());
13038 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::EDX)
13039 .addReg(MI->getOperand(ValOps+1).getReg());
13040
13041 // The instruction doesn't actually take any operands though.
13042 BuildMI(*BB, MI, dl, TII->get(X86::MONITORrrr));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013043
Eric Christopher228232b2010-11-30 07:20:12 +000013044 MI->eraseFromParent(); // The pseudo is gone now.
13045 return BB;
13046}
13047
13048MachineBasicBlock *
Dan Gohman320afb82010-10-12 18:00:49 +000013049X86TargetLowering::EmitVAARG64WithCustomInserter(
13050 MachineInstr *MI,
13051 MachineBasicBlock *MBB) const {
13052 // Emit va_arg instruction on X86-64.
13053
13054 // Operands to this pseudo-instruction:
13055 // 0 ) Output : destination address (reg)
13056 // 1-5) Input : va_list address (addr, i64mem)
13057 // 6 ) ArgSize : Size (in bytes) of vararg type
13058 // 7 ) ArgMode : 0=overflow only, 1=use gp_offset, 2=use fp_offset
13059 // 8 ) Align : Alignment of type
13060 // 9 ) EFLAGS (implicit-def)
13061
13062 assert(MI->getNumOperands() == 10 && "VAARG_64 should have 10 operands!");
13063 assert(X86::AddrNumOperands == 5 && "VAARG_64 assumes 5 address operands");
13064
13065 unsigned DestReg = MI->getOperand(0).getReg();
13066 MachineOperand &Base = MI->getOperand(1);
13067 MachineOperand &Scale = MI->getOperand(2);
13068 MachineOperand &Index = MI->getOperand(3);
13069 MachineOperand &Disp = MI->getOperand(4);
13070 MachineOperand &Segment = MI->getOperand(5);
13071 unsigned ArgSize = MI->getOperand(6).getImm();
13072 unsigned ArgMode = MI->getOperand(7).getImm();
13073 unsigned Align = MI->getOperand(8).getImm();
13074
13075 // Memory Reference
13076 assert(MI->hasOneMemOperand() && "Expected VAARG_64 to have one memoperand");
13077 MachineInstr::mmo_iterator MMOBegin = MI->memoperands_begin();
13078 MachineInstr::mmo_iterator MMOEnd = MI->memoperands_end();
13079
13080 // Machine Information
13081 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
13082 MachineRegisterInfo &MRI = MBB->getParent()->getRegInfo();
13083 const TargetRegisterClass *AddrRegClass = getRegClassFor(MVT::i64);
13084 const TargetRegisterClass *OffsetRegClass = getRegClassFor(MVT::i32);
13085 DebugLoc DL = MI->getDebugLoc();
13086
13087 // struct va_list {
13088 // i32 gp_offset
13089 // i32 fp_offset
13090 // i64 overflow_area (address)
13091 // i64 reg_save_area (address)
13092 // }
13093 // sizeof(va_list) = 24
13094 // alignment(va_list) = 8
13095
13096 unsigned TotalNumIntRegs = 6;
13097 unsigned TotalNumXMMRegs = 8;
13098 bool UseGPOffset = (ArgMode == 1);
13099 bool UseFPOffset = (ArgMode == 2);
13100 unsigned MaxOffset = TotalNumIntRegs * 8 +
13101 (UseFPOffset ? TotalNumXMMRegs * 16 : 0);
13102
13103 /* Align ArgSize to a multiple of 8 */
13104 unsigned ArgSizeA8 = (ArgSize + 7) & ~7;
13105 bool NeedsAlign = (Align > 8);
13106
13107 MachineBasicBlock *thisMBB = MBB;
13108 MachineBasicBlock *overflowMBB;
13109 MachineBasicBlock *offsetMBB;
13110 MachineBasicBlock *endMBB;
13111
13112 unsigned OffsetDestReg = 0; // Argument address computed by offsetMBB
13113 unsigned OverflowDestReg = 0; // Argument address computed by overflowMBB
13114 unsigned OffsetReg = 0;
13115
13116 if (!UseGPOffset && !UseFPOffset) {
13117 // If we only pull from the overflow region, we don't create a branch.
13118 // We don't need to alter control flow.
13119 OffsetDestReg = 0; // unused
13120 OverflowDestReg = DestReg;
13121
13122 offsetMBB = NULL;
13123 overflowMBB = thisMBB;
13124 endMBB = thisMBB;
13125 } else {
13126 // First emit code to check if gp_offset (or fp_offset) is below the bound.
13127 // If so, pull the argument from reg_save_area. (branch to offsetMBB)
13128 // If not, pull from overflow_area. (branch to overflowMBB)
13129 //
13130 // thisMBB
13131 // | .
13132 // | .
13133 // offsetMBB overflowMBB
13134 // | .
13135 // | .
13136 // endMBB
13137
13138 // Registers for the PHI in endMBB
13139 OffsetDestReg = MRI.createVirtualRegister(AddrRegClass);
13140 OverflowDestReg = MRI.createVirtualRegister(AddrRegClass);
13141
13142 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
13143 MachineFunction *MF = MBB->getParent();
13144 overflowMBB = MF->CreateMachineBasicBlock(LLVM_BB);
13145 offsetMBB = MF->CreateMachineBasicBlock(LLVM_BB);
13146 endMBB = MF->CreateMachineBasicBlock(LLVM_BB);
13147
13148 MachineFunction::iterator MBBIter = MBB;
13149 ++MBBIter;
13150
13151 // Insert the new basic blocks
13152 MF->insert(MBBIter, offsetMBB);
13153 MF->insert(MBBIter, overflowMBB);
13154 MF->insert(MBBIter, endMBB);
13155
13156 // Transfer the remainder of MBB and its successor edges to endMBB.
13157 endMBB->splice(endMBB->begin(), thisMBB,
13158 llvm::next(MachineBasicBlock::iterator(MI)),
13159 thisMBB->end());
13160 endMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
13161
13162 // Make offsetMBB and overflowMBB successors of thisMBB
13163 thisMBB->addSuccessor(offsetMBB);
13164 thisMBB->addSuccessor(overflowMBB);
13165
13166 // endMBB is a successor of both offsetMBB and overflowMBB
13167 offsetMBB->addSuccessor(endMBB);
13168 overflowMBB->addSuccessor(endMBB);
13169
13170 // Load the offset value into a register
13171 OffsetReg = MRI.createVirtualRegister(OffsetRegClass);
13172 BuildMI(thisMBB, DL, TII->get(X86::MOV32rm), OffsetReg)
13173 .addOperand(Base)
13174 .addOperand(Scale)
13175 .addOperand(Index)
13176 .addDisp(Disp, UseFPOffset ? 4 : 0)
13177 .addOperand(Segment)
13178 .setMemRefs(MMOBegin, MMOEnd);
13179
13180 // Check if there is enough room left to pull this argument.
13181 BuildMI(thisMBB, DL, TII->get(X86::CMP32ri))
13182 .addReg(OffsetReg)
13183 .addImm(MaxOffset + 8 - ArgSizeA8);
13184
13185 // Branch to "overflowMBB" if offset >= max
13186 // Fall through to "offsetMBB" otherwise
13187 BuildMI(thisMBB, DL, TII->get(X86::GetCondBranchFromCond(X86::COND_AE)))
13188 .addMBB(overflowMBB);
13189 }
13190
13191 // In offsetMBB, emit code to use the reg_save_area.
13192 if (offsetMBB) {
13193 assert(OffsetReg != 0);
13194
13195 // Read the reg_save_area address.
13196 unsigned RegSaveReg = MRI.createVirtualRegister(AddrRegClass);
13197 BuildMI(offsetMBB, DL, TII->get(X86::MOV64rm), RegSaveReg)
13198 .addOperand(Base)
13199 .addOperand(Scale)
13200 .addOperand(Index)
13201 .addDisp(Disp, 16)
13202 .addOperand(Segment)
13203 .setMemRefs(MMOBegin, MMOEnd);
13204
13205 // Zero-extend the offset
13206 unsigned OffsetReg64 = MRI.createVirtualRegister(AddrRegClass);
13207 BuildMI(offsetMBB, DL, TII->get(X86::SUBREG_TO_REG), OffsetReg64)
13208 .addImm(0)
13209 .addReg(OffsetReg)
13210 .addImm(X86::sub_32bit);
13211
13212 // Add the offset to the reg_save_area to get the final address.
13213 BuildMI(offsetMBB, DL, TII->get(X86::ADD64rr), OffsetDestReg)
13214 .addReg(OffsetReg64)
13215 .addReg(RegSaveReg);
13216
13217 // Compute the offset for the next argument
13218 unsigned NextOffsetReg = MRI.createVirtualRegister(OffsetRegClass);
13219 BuildMI(offsetMBB, DL, TII->get(X86::ADD32ri), NextOffsetReg)
13220 .addReg(OffsetReg)
13221 .addImm(UseFPOffset ? 16 : 8);
13222
13223 // Store it back into the va_list.
13224 BuildMI(offsetMBB, DL, TII->get(X86::MOV32mr))
13225 .addOperand(Base)
13226 .addOperand(Scale)
13227 .addOperand(Index)
13228 .addDisp(Disp, UseFPOffset ? 4 : 0)
13229 .addOperand(Segment)
13230 .addReg(NextOffsetReg)
13231 .setMemRefs(MMOBegin, MMOEnd);
13232
13233 // Jump to endMBB
13234 BuildMI(offsetMBB, DL, TII->get(X86::JMP_4))
13235 .addMBB(endMBB);
13236 }
13237
13238 //
13239 // Emit code to use overflow area
13240 //
13241
13242 // Load the overflow_area address into a register.
13243 unsigned OverflowAddrReg = MRI.createVirtualRegister(AddrRegClass);
13244 BuildMI(overflowMBB, DL, TII->get(X86::MOV64rm), OverflowAddrReg)
13245 .addOperand(Base)
13246 .addOperand(Scale)
13247 .addOperand(Index)
13248 .addDisp(Disp, 8)
13249 .addOperand(Segment)
13250 .setMemRefs(MMOBegin, MMOEnd);
13251
13252 // If we need to align it, do so. Otherwise, just copy the address
13253 // to OverflowDestReg.
13254 if (NeedsAlign) {
13255 // Align the overflow address
13256 assert((Align & (Align-1)) == 0 && "Alignment must be a power of 2");
13257 unsigned TmpReg = MRI.createVirtualRegister(AddrRegClass);
13258
13259 // aligned_addr = (addr + (align-1)) & ~(align-1)
13260 BuildMI(overflowMBB, DL, TII->get(X86::ADD64ri32), TmpReg)
13261 .addReg(OverflowAddrReg)
13262 .addImm(Align-1);
13263
13264 BuildMI(overflowMBB, DL, TII->get(X86::AND64ri32), OverflowDestReg)
13265 .addReg(TmpReg)
13266 .addImm(~(uint64_t)(Align-1));
13267 } else {
13268 BuildMI(overflowMBB, DL, TII->get(TargetOpcode::COPY), OverflowDestReg)
13269 .addReg(OverflowAddrReg);
13270 }
13271
13272 // Compute the next overflow address after this argument.
13273 // (the overflow address should be kept 8-byte aligned)
13274 unsigned NextAddrReg = MRI.createVirtualRegister(AddrRegClass);
13275 BuildMI(overflowMBB, DL, TII->get(X86::ADD64ri32), NextAddrReg)
13276 .addReg(OverflowDestReg)
13277 .addImm(ArgSizeA8);
13278
13279 // Store the new overflow address.
13280 BuildMI(overflowMBB, DL, TII->get(X86::MOV64mr))
13281 .addOperand(Base)
13282 .addOperand(Scale)
13283 .addOperand(Index)
13284 .addDisp(Disp, 8)
13285 .addOperand(Segment)
13286 .addReg(NextAddrReg)
13287 .setMemRefs(MMOBegin, MMOEnd);
13288
13289 // If we branched, emit the PHI to the front of endMBB.
13290 if (offsetMBB) {
13291 BuildMI(*endMBB, endMBB->begin(), DL,
13292 TII->get(X86::PHI), DestReg)
13293 .addReg(OffsetDestReg).addMBB(offsetMBB)
13294 .addReg(OverflowDestReg).addMBB(overflowMBB);
13295 }
13296
13297 // Erase the pseudo instruction
13298 MI->eraseFromParent();
13299
13300 return endMBB;
13301}
13302
13303MachineBasicBlock *
Dan Gohmand6708ea2009-08-15 01:38:56 +000013304X86TargetLowering::EmitVAStartSaveXMMRegsWithCustomInserter(
13305 MachineInstr *MI,
13306 MachineBasicBlock *MBB) const {
13307 // Emit code to save XMM registers to the stack. The ABI says that the
13308 // number of registers to save is given in %al, so it's theoretically
13309 // possible to do an indirect jump trick to avoid saving all of them,
13310 // however this code takes a simpler approach and just executes all
13311 // of the stores if %al is non-zero. It's less code, and it's probably
13312 // easier on the hardware branch predictor, and stores aren't all that
13313 // expensive anyway.
13314
13315 // Create the new basic blocks. One block contains all the XMM stores,
13316 // and one block is the final destination regardless of whether any
13317 // stores were performed.
13318 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
13319 MachineFunction *F = MBB->getParent();
13320 MachineFunction::iterator MBBIter = MBB;
13321 ++MBBIter;
13322 MachineBasicBlock *XMMSaveMBB = F->CreateMachineBasicBlock(LLVM_BB);
13323 MachineBasicBlock *EndMBB = F->CreateMachineBasicBlock(LLVM_BB);
13324 F->insert(MBBIter, XMMSaveMBB);
13325 F->insert(MBBIter, EndMBB);
13326
Dan Gohman14152b42010-07-06 20:24:04 +000013327 // Transfer the remainder of MBB and its successor edges to EndMBB.
13328 EndMBB->splice(EndMBB->begin(), MBB,
13329 llvm::next(MachineBasicBlock::iterator(MI)),
13330 MBB->end());
13331 EndMBB->transferSuccessorsAndUpdatePHIs(MBB);
13332
Dan Gohmand6708ea2009-08-15 01:38:56 +000013333 // The original block will now fall through to the XMM save block.
13334 MBB->addSuccessor(XMMSaveMBB);
13335 // The XMMSaveMBB will fall through to the end block.
13336 XMMSaveMBB->addSuccessor(EndMBB);
13337
13338 // Now add the instructions.
13339 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
13340 DebugLoc DL = MI->getDebugLoc();
13341
13342 unsigned CountReg = MI->getOperand(0).getReg();
13343 int64_t RegSaveFrameIndex = MI->getOperand(1).getImm();
13344 int64_t VarArgsFPOffset = MI->getOperand(2).getImm();
13345
13346 if (!Subtarget->isTargetWin64()) {
13347 // If %al is 0, branch around the XMM save block.
13348 BuildMI(MBB, DL, TII->get(X86::TEST8rr)).addReg(CountReg).addReg(CountReg);
Chris Lattnerbd13fb62010-02-11 19:25:55 +000013349 BuildMI(MBB, DL, TII->get(X86::JE_4)).addMBB(EndMBB);
Dan Gohmand6708ea2009-08-15 01:38:56 +000013350 MBB->addSuccessor(EndMBB);
13351 }
13352
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000013353 unsigned MOVOpc = Subtarget->hasFp256() ? X86::VMOVAPSmr : X86::MOVAPSmr;
Dan Gohmand6708ea2009-08-15 01:38:56 +000013354 // In the XMM save block, save all the XMM argument registers.
13355 for (int i = 3, e = MI->getNumOperands(); i != e; ++i) {
13356 int64_t Offset = (i - 3) * 16 + VarArgsFPOffset;
Dan Gohmanc76909a2009-09-25 20:36:54 +000013357 MachineMemOperand *MMO =
Evan Chengff89dcb2009-10-18 18:16:27 +000013358 F->getMachineMemOperand(
Chris Lattnere8639032010-09-21 06:22:23 +000013359 MachinePointerInfo::getFixedStack(RegSaveFrameIndex, Offset),
Chris Lattner59db5492010-09-21 04:39:43 +000013360 MachineMemOperand::MOStore,
Evan Chengff89dcb2009-10-18 18:16:27 +000013361 /*Size=*/16, /*Align=*/16);
Bruno Cardoso Lopes5affa512011-08-31 03:04:09 +000013362 BuildMI(XMMSaveMBB, DL, TII->get(MOVOpc))
Dan Gohmand6708ea2009-08-15 01:38:56 +000013363 .addFrameIndex(RegSaveFrameIndex)
13364 .addImm(/*Scale=*/1)
13365 .addReg(/*IndexReg=*/0)
13366 .addImm(/*Disp=*/Offset)
13367 .addReg(/*Segment=*/0)
13368 .addReg(MI->getOperand(i).getReg())
Dan Gohmanc76909a2009-09-25 20:36:54 +000013369 .addMemOperand(MMO);
Dan Gohmand6708ea2009-08-15 01:38:56 +000013370 }
13371
Dan Gohman14152b42010-07-06 20:24:04 +000013372 MI->eraseFromParent(); // The pseudo instruction is gone now.
Dan Gohmand6708ea2009-08-15 01:38:56 +000013373
13374 return EndMBB;
13375}
Mon P Wang63307c32008-05-05 19:05:59 +000013376
Lang Hames6e3f7e42012-02-03 01:13:49 +000013377// The EFLAGS operand of SelectItr might be missing a kill marker
13378// because there were multiple uses of EFLAGS, and ISel didn't know
13379// which to mark. Figure out whether SelectItr should have had a
13380// kill marker, and set it if it should. Returns the correct kill
13381// marker value.
13382static bool checkAndUpdateEFLAGSKill(MachineBasicBlock::iterator SelectItr,
13383 MachineBasicBlock* BB,
13384 const TargetRegisterInfo* TRI) {
13385 // Scan forward through BB for a use/def of EFLAGS.
13386 MachineBasicBlock::iterator miI(llvm::next(SelectItr));
13387 for (MachineBasicBlock::iterator miE = BB->end(); miI != miE; ++miI) {
Lang Hames50a36f72012-02-02 07:48:37 +000013388 const MachineInstr& mi = *miI;
Lang Hames6e3f7e42012-02-03 01:13:49 +000013389 if (mi.readsRegister(X86::EFLAGS))
Lang Hames50a36f72012-02-02 07:48:37 +000013390 return false;
Lang Hames6e3f7e42012-02-03 01:13:49 +000013391 if (mi.definesRegister(X86::EFLAGS))
13392 break; // Should have kill-flag - update below.
13393 }
13394
13395 // If we hit the end of the block, check whether EFLAGS is live into a
13396 // successor.
13397 if (miI == BB->end()) {
13398 for (MachineBasicBlock::succ_iterator sItr = BB->succ_begin(),
13399 sEnd = BB->succ_end();
13400 sItr != sEnd; ++sItr) {
13401 MachineBasicBlock* succ = *sItr;
13402 if (succ->isLiveIn(X86::EFLAGS))
13403 return false;
Lang Hames50a36f72012-02-02 07:48:37 +000013404 }
13405 }
13406
Lang Hames6e3f7e42012-02-03 01:13:49 +000013407 // We found a def, or hit the end of the basic block and EFLAGS wasn't live
13408 // out. SelectMI should have a kill flag on EFLAGS.
13409 SelectItr->addRegisterKilled(X86::EFLAGS, TRI);
Lang Hames50a36f72012-02-02 07:48:37 +000013410 return true;
13411}
13412
Evan Cheng60c07e12006-07-05 22:17:51 +000013413MachineBasicBlock *
Chris Lattner52600972009-09-02 05:57:00 +000013414X86TargetLowering::EmitLoweredSelect(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000013415 MachineBasicBlock *BB) const {
Chris Lattner52600972009-09-02 05:57:00 +000013416 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
13417 DebugLoc DL = MI->getDebugLoc();
Daniel Dunbara279bc32009-09-20 02:20:51 +000013418
Chris Lattner52600972009-09-02 05:57:00 +000013419 // To "insert" a SELECT_CC instruction, we actually have to insert the
13420 // diamond control-flow pattern. The incoming instruction knows the
13421 // destination vreg to set, the condition code register to branch on, the
13422 // true/false values to select between, and a branch opcode to use.
13423 const BasicBlock *LLVM_BB = BB->getBasicBlock();
13424 MachineFunction::iterator It = BB;
13425 ++It;
Daniel Dunbara279bc32009-09-20 02:20:51 +000013426
Chris Lattner52600972009-09-02 05:57:00 +000013427 // thisMBB:
13428 // ...
13429 // TrueVal = ...
13430 // cmpTY ccX, r1, r2
13431 // bCC copy1MBB
13432 // fallthrough --> copy0MBB
13433 MachineBasicBlock *thisMBB = BB;
13434 MachineFunction *F = BB->getParent();
13435 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
13436 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Chris Lattner52600972009-09-02 05:57:00 +000013437 F->insert(It, copy0MBB);
13438 F->insert(It, sinkMBB);
Bill Wendling730c07e2010-06-25 20:48:10 +000013439
Bill Wendling730c07e2010-06-25 20:48:10 +000013440 // If the EFLAGS register isn't dead in the terminator, then claim that it's
13441 // live into the sink and copy blocks.
Lang Hames6e3f7e42012-02-03 01:13:49 +000013442 const TargetRegisterInfo* TRI = getTargetMachine().getRegisterInfo();
13443 if (!MI->killsRegister(X86::EFLAGS) &&
13444 !checkAndUpdateEFLAGSKill(MI, BB, TRI)) {
13445 copy0MBB->addLiveIn(X86::EFLAGS);
13446 sinkMBB->addLiveIn(X86::EFLAGS);
Bill Wendling730c07e2010-06-25 20:48:10 +000013447 }
13448
Dan Gohman14152b42010-07-06 20:24:04 +000013449 // Transfer the remainder of BB and its successor edges to sinkMBB.
13450 sinkMBB->splice(sinkMBB->begin(), BB,
13451 llvm::next(MachineBasicBlock::iterator(MI)),
13452 BB->end());
13453 sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
13454
13455 // Add the true and fallthrough blocks as its successors.
13456 BB->addSuccessor(copy0MBB);
13457 BB->addSuccessor(sinkMBB);
13458
13459 // Create the conditional branch instruction.
13460 unsigned Opc =
13461 X86::GetCondBranchFromCond((X86::CondCode)MI->getOperand(3).getImm());
13462 BuildMI(BB, DL, TII->get(Opc)).addMBB(sinkMBB);
13463
Chris Lattner52600972009-09-02 05:57:00 +000013464 // copy0MBB:
13465 // %FalseValue = ...
13466 // # fallthrough to sinkMBB
Dan Gohman3335a222010-04-30 20:14:26 +000013467 copy0MBB->addSuccessor(sinkMBB);
Daniel Dunbara279bc32009-09-20 02:20:51 +000013468
Chris Lattner52600972009-09-02 05:57:00 +000013469 // sinkMBB:
13470 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
13471 // ...
Dan Gohman14152b42010-07-06 20:24:04 +000013472 BuildMI(*sinkMBB, sinkMBB->begin(), DL,
13473 TII->get(X86::PHI), MI->getOperand(0).getReg())
Chris Lattner52600972009-09-02 05:57:00 +000013474 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
13475 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
13476
Dan Gohman14152b42010-07-06 20:24:04 +000013477 MI->eraseFromParent(); // The pseudo instruction is gone now.
Dan Gohman3335a222010-04-30 20:14:26 +000013478 return sinkMBB;
Chris Lattner52600972009-09-02 05:57:00 +000013479}
13480
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000013481MachineBasicBlock *
Rafael Espindola151ab3e2011-08-30 19:47:04 +000013482X86TargetLowering::EmitLoweredSegAlloca(MachineInstr *MI, MachineBasicBlock *BB,
13483 bool Is64Bit) const {
13484 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
13485 DebugLoc DL = MI->getDebugLoc();
13486 MachineFunction *MF = BB->getParent();
13487 const BasicBlock *LLVM_BB = BB->getBasicBlock();
13488
Nick Lewycky8a8d4792011-12-02 22:16:29 +000013489 assert(getTargetMachine().Options.EnableSegmentedStacks);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000013490
13491 unsigned TlsReg = Is64Bit ? X86::FS : X86::GS;
13492 unsigned TlsOffset = Is64Bit ? 0x70 : 0x30;
13493
13494 // BB:
13495 // ... [Till the alloca]
13496 // If stacklet is not large enough, jump to mallocMBB
13497 //
13498 // bumpMBB:
13499 // Allocate by subtracting from RSP
13500 // Jump to continueMBB
13501 //
13502 // mallocMBB:
13503 // Allocate by call to runtime
13504 //
13505 // continueMBB:
13506 // ...
13507 // [rest of original BB]
13508 //
13509
13510 MachineBasicBlock *mallocMBB = MF->CreateMachineBasicBlock(LLVM_BB);
13511 MachineBasicBlock *bumpMBB = MF->CreateMachineBasicBlock(LLVM_BB);
13512 MachineBasicBlock *continueMBB = MF->CreateMachineBasicBlock(LLVM_BB);
13513
13514 MachineRegisterInfo &MRI = MF->getRegInfo();
13515 const TargetRegisterClass *AddrRegClass =
13516 getRegClassFor(Is64Bit ? MVT::i64:MVT::i32);
13517
13518 unsigned mallocPtrVReg = MRI.createVirtualRegister(AddrRegClass),
13519 bumpSPPtrVReg = MRI.createVirtualRegister(AddrRegClass),
13520 tmpSPVReg = MRI.createVirtualRegister(AddrRegClass),
Rafael Espindola66bf7432011-10-26 21:16:41 +000013521 SPLimitVReg = MRI.createVirtualRegister(AddrRegClass),
Rafael Espindola151ab3e2011-08-30 19:47:04 +000013522 sizeVReg = MI->getOperand(1).getReg(),
13523 physSPReg = Is64Bit ? X86::RSP : X86::ESP;
13524
13525 MachineFunction::iterator MBBIter = BB;
13526 ++MBBIter;
13527
13528 MF->insert(MBBIter, bumpMBB);
13529 MF->insert(MBBIter, mallocMBB);
13530 MF->insert(MBBIter, continueMBB);
13531
13532 continueMBB->splice(continueMBB->begin(), BB, llvm::next
13533 (MachineBasicBlock::iterator(MI)), BB->end());
13534 continueMBB->transferSuccessorsAndUpdatePHIs(BB);
13535
13536 // Add code to the main basic block to check if the stack limit has been hit,
13537 // and if so, jump to mallocMBB otherwise to bumpMBB.
13538 BuildMI(BB, DL, TII->get(TargetOpcode::COPY), tmpSPVReg).addReg(physSPReg);
Rafael Espindola66bf7432011-10-26 21:16:41 +000013539 BuildMI(BB, DL, TII->get(Is64Bit ? X86::SUB64rr:X86::SUB32rr), SPLimitVReg)
Rafael Espindola151ab3e2011-08-30 19:47:04 +000013540 .addReg(tmpSPVReg).addReg(sizeVReg);
13541 BuildMI(BB, DL, TII->get(Is64Bit ? X86::CMP64mr:X86::CMP32mr))
Rafael Espindola014f7a32012-01-11 18:14:03 +000013542 .addReg(0).addImm(1).addReg(0).addImm(TlsOffset).addReg(TlsReg)
Rafael Espindola66bf7432011-10-26 21:16:41 +000013543 .addReg(SPLimitVReg);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000013544 BuildMI(BB, DL, TII->get(X86::JG_4)).addMBB(mallocMBB);
13545
13546 // bumpMBB simply decreases the stack pointer, since we know the current
13547 // stacklet has enough space.
13548 BuildMI(bumpMBB, DL, TII->get(TargetOpcode::COPY), physSPReg)
Rafael Espindola66bf7432011-10-26 21:16:41 +000013549 .addReg(SPLimitVReg);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000013550 BuildMI(bumpMBB, DL, TII->get(TargetOpcode::COPY), bumpSPPtrVReg)
Rafael Espindola66bf7432011-10-26 21:16:41 +000013551 .addReg(SPLimitVReg);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000013552 BuildMI(bumpMBB, DL, TII->get(X86::JMP_4)).addMBB(continueMBB);
13553
13554 // Calls into a routine in libgcc to allocate more space from the heap.
Jakob Stoklund Olesen8bcde2a2012-02-16 00:02:50 +000013555 const uint32_t *RegMask =
13556 getTargetMachine().getRegisterInfo()->getCallPreservedMask(CallingConv::C);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000013557 if (Is64Bit) {
13558 BuildMI(mallocMBB, DL, TII->get(X86::MOV64rr), X86::RDI)
13559 .addReg(sizeVReg);
13560 BuildMI(mallocMBB, DL, TII->get(X86::CALL64pcrel32))
Jakob Stoklund Olesen85dccf12012-07-04 23:53:27 +000013561 .addExternalSymbol("__morestack_allocate_stack_space")
Jakob Stoklund Olesen8bcde2a2012-02-16 00:02:50 +000013562 .addRegMask(RegMask)
Jakob Stoklund Olesen85dccf12012-07-04 23:53:27 +000013563 .addReg(X86::RDI, RegState::Implicit)
Jakob Stoklund Olesen8bcde2a2012-02-16 00:02:50 +000013564 .addReg(X86::RAX, RegState::ImplicitDefine);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000013565 } else {
13566 BuildMI(mallocMBB, DL, TII->get(X86::SUB32ri), physSPReg).addReg(physSPReg)
13567 .addImm(12);
13568 BuildMI(mallocMBB, DL, TII->get(X86::PUSH32r)).addReg(sizeVReg);
13569 BuildMI(mallocMBB, DL, TII->get(X86::CALLpcrel32))
Jakob Stoklund Olesen8bcde2a2012-02-16 00:02:50 +000013570 .addExternalSymbol("__morestack_allocate_stack_space")
13571 .addRegMask(RegMask)
13572 .addReg(X86::EAX, RegState::ImplicitDefine);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000013573 }
13574
13575 if (!Is64Bit)
13576 BuildMI(mallocMBB, DL, TII->get(X86::ADD32ri), physSPReg).addReg(physSPReg)
13577 .addImm(16);
13578
13579 BuildMI(mallocMBB, DL, TII->get(TargetOpcode::COPY), mallocPtrVReg)
13580 .addReg(Is64Bit ? X86::RAX : X86::EAX);
13581 BuildMI(mallocMBB, DL, TII->get(X86::JMP_4)).addMBB(continueMBB);
13582
13583 // Set up the CFG correctly.
13584 BB->addSuccessor(bumpMBB);
13585 BB->addSuccessor(mallocMBB);
13586 mallocMBB->addSuccessor(continueMBB);
13587 bumpMBB->addSuccessor(continueMBB);
13588
13589 // Take care of the PHI nodes.
13590 BuildMI(*continueMBB, continueMBB->begin(), DL, TII->get(X86::PHI),
13591 MI->getOperand(0).getReg())
13592 .addReg(mallocPtrVReg).addMBB(mallocMBB)
13593 .addReg(bumpSPPtrVReg).addMBB(bumpMBB);
13594
13595 // Delete the original pseudo instruction.
13596 MI->eraseFromParent();
13597
13598 // And we're done.
13599 return continueMBB;
13600}
13601
13602MachineBasicBlock *
Michael J. Spencere9c253e2010-10-21 01:41:01 +000013603X86TargetLowering::EmitLoweredWinAlloca(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000013604 MachineBasicBlock *BB) const {
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000013605 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
13606 DebugLoc DL = MI->getDebugLoc();
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000013607
NAKAMURA Takumia2e07622011-03-24 07:07:00 +000013608 assert(!Subtarget->isTargetEnvMacho());
13609
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000013610 // The lowering is pretty easy: we're just emitting the call to _alloca. The
13611 // non-trivial part is impdef of ESP.
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000013612
NAKAMURA Takumia2e07622011-03-24 07:07:00 +000013613 if (Subtarget->isTargetWin64()) {
13614 if (Subtarget->isTargetCygMing()) {
13615 // ___chkstk(Mingw64):
13616 // Clobbers R10, R11, RAX and EFLAGS.
13617 // Updates RSP.
13618 BuildMI(*BB, MI, DL, TII->get(X86::W64ALLOCA))
13619 .addExternalSymbol("___chkstk")
13620 .addReg(X86::RAX, RegState::Implicit)
13621 .addReg(X86::RSP, RegState::Implicit)
13622 .addReg(X86::RAX, RegState::Define | RegState::Implicit)
13623 .addReg(X86::RSP, RegState::Define | RegState::Implicit)
13624 .addReg(X86::EFLAGS, RegState::Define | RegState::Implicit);
13625 } else {
13626 // __chkstk(MSVCRT): does not update stack pointer.
13627 // Clobbers R10, R11 and EFLAGS.
13628 // FIXME: RAX(allocated size) might be reused and not killed.
13629 BuildMI(*BB, MI, DL, TII->get(X86::W64ALLOCA))
13630 .addExternalSymbol("__chkstk")
13631 .addReg(X86::RAX, RegState::Implicit)
13632 .addReg(X86::EFLAGS, RegState::Define | RegState::Implicit);
13633 // RAX has the offset to subtracted from RSP.
13634 BuildMI(*BB, MI, DL, TII->get(X86::SUB64rr), X86::RSP)
13635 .addReg(X86::RSP)
13636 .addReg(X86::RAX);
13637 }
13638 } else {
13639 const char *StackProbeSymbol =
Michael J. Spencere9c253e2010-10-21 01:41:01 +000013640 Subtarget->isTargetWindows() ? "_chkstk" : "_alloca";
13641
NAKAMURA Takumia2e07622011-03-24 07:07:00 +000013642 BuildMI(*BB, MI, DL, TII->get(X86::CALLpcrel32))
13643 .addExternalSymbol(StackProbeSymbol)
13644 .addReg(X86::EAX, RegState::Implicit)
13645 .addReg(X86::ESP, RegState::Implicit)
13646 .addReg(X86::EAX, RegState::Define | RegState::Implicit)
13647 .addReg(X86::ESP, RegState::Define | RegState::Implicit)
13648 .addReg(X86::EFLAGS, RegState::Define | RegState::Implicit);
13649 }
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000013650
Dan Gohman14152b42010-07-06 20:24:04 +000013651 MI->eraseFromParent(); // The pseudo instruction is gone now.
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000013652 return BB;
13653}
Chris Lattner52600972009-09-02 05:57:00 +000013654
13655MachineBasicBlock *
Eric Christopher30ef0e52010-06-03 04:07:48 +000013656X86TargetLowering::EmitLoweredTLSCall(MachineInstr *MI,
13657 MachineBasicBlock *BB) const {
13658 // This is pretty easy. We're taking the value that we received from
13659 // our load from the relocation, sticking it in either RDI (x86-64)
13660 // or EAX and doing an indirect call. The return value will then
13661 // be in the normal return register.
Michael J. Spencerec38de22010-10-10 22:04:20 +000013662 const X86InstrInfo *TII
Eric Christopher54415362010-06-08 22:04:25 +000013663 = static_cast<const X86InstrInfo*>(getTargetMachine().getInstrInfo());
Eric Christopher30ef0e52010-06-03 04:07:48 +000013664 DebugLoc DL = MI->getDebugLoc();
13665 MachineFunction *F = BB->getParent();
Eric Christopher722d3152010-09-27 06:01:51 +000013666
13667 assert(Subtarget->isTargetDarwin() && "Darwin only instr emitted?");
Eric Christopher54415362010-06-08 22:04:25 +000013668 assert(MI->getOperand(3).isGlobal() && "This should be a global");
Michael J. Spencerec38de22010-10-10 22:04:20 +000013669
Jakob Stoklund Olesen8bcde2a2012-02-16 00:02:50 +000013670 // Get a register mask for the lowered call.
13671 // FIXME: The 32-bit calls have non-standard calling conventions. Use a
13672 // proper register mask.
13673 const uint32_t *RegMask =
13674 getTargetMachine().getRegisterInfo()->getCallPreservedMask(CallingConv::C);
Eric Christopher30ef0e52010-06-03 04:07:48 +000013675 if (Subtarget->is64Bit()) {
Dan Gohman14152b42010-07-06 20:24:04 +000013676 MachineInstrBuilder MIB = BuildMI(*BB, MI, DL,
13677 TII->get(X86::MOV64rm), X86::RDI)
Eric Christopher54415362010-06-08 22:04:25 +000013678 .addReg(X86::RIP)
13679 .addImm(0).addReg(0)
Michael J. Spencerec38de22010-10-10 22:04:20 +000013680 .addGlobalAddress(MI->getOperand(3).getGlobal(), 0,
Eric Christopher54415362010-06-08 22:04:25 +000013681 MI->getOperand(3).getTargetFlags())
13682 .addReg(0);
Eric Christopher722d3152010-09-27 06:01:51 +000013683 MIB = BuildMI(*BB, MI, DL, TII->get(X86::CALL64m));
Chris Lattner599b5312010-07-08 23:46:44 +000013684 addDirectMem(MIB, X86::RDI);
Jakob Stoklund Olesen8bcde2a2012-02-16 00:02:50 +000013685 MIB.addReg(X86::RAX, RegState::ImplicitDefine).addRegMask(RegMask);
Eric Christopher61025492010-06-15 23:08:42 +000013686 } else if (getTargetMachine().getRelocationModel() != Reloc::PIC_) {
Dan Gohman14152b42010-07-06 20:24:04 +000013687 MachineInstrBuilder MIB = BuildMI(*BB, MI, DL,
13688 TII->get(X86::MOV32rm), X86::EAX)
Eric Christopher61025492010-06-15 23:08:42 +000013689 .addReg(0)
13690 .addImm(0).addReg(0)
Michael J. Spencerec38de22010-10-10 22:04:20 +000013691 .addGlobalAddress(MI->getOperand(3).getGlobal(), 0,
Eric Christopher61025492010-06-15 23:08:42 +000013692 MI->getOperand(3).getTargetFlags())
13693 .addReg(0);
Dan Gohman14152b42010-07-06 20:24:04 +000013694 MIB = BuildMI(*BB, MI, DL, TII->get(X86::CALL32m));
Chris Lattner599b5312010-07-08 23:46:44 +000013695 addDirectMem(MIB, X86::EAX);
Jakob Stoklund Olesen8bcde2a2012-02-16 00:02:50 +000013696 MIB.addReg(X86::EAX, RegState::ImplicitDefine).addRegMask(RegMask);
Eric Christopher30ef0e52010-06-03 04:07:48 +000013697 } else {
Dan Gohman14152b42010-07-06 20:24:04 +000013698 MachineInstrBuilder MIB = BuildMI(*BB, MI, DL,
13699 TII->get(X86::MOV32rm), X86::EAX)
Eric Christopher54415362010-06-08 22:04:25 +000013700 .addReg(TII->getGlobalBaseReg(F))
13701 .addImm(0).addReg(0)
Michael J. Spencerec38de22010-10-10 22:04:20 +000013702 .addGlobalAddress(MI->getOperand(3).getGlobal(), 0,
Eric Christopher54415362010-06-08 22:04:25 +000013703 MI->getOperand(3).getTargetFlags())
13704 .addReg(0);
Dan Gohman14152b42010-07-06 20:24:04 +000013705 MIB = BuildMI(*BB, MI, DL, TII->get(X86::CALL32m));
Chris Lattner599b5312010-07-08 23:46:44 +000013706 addDirectMem(MIB, X86::EAX);
Jakob Stoklund Olesen8bcde2a2012-02-16 00:02:50 +000013707 MIB.addReg(X86::EAX, RegState::ImplicitDefine).addRegMask(RegMask);
Eric Christopher30ef0e52010-06-03 04:07:48 +000013708 }
Michael J. Spencerec38de22010-10-10 22:04:20 +000013709
Dan Gohman14152b42010-07-06 20:24:04 +000013710 MI->eraseFromParent(); // The pseudo instruction is gone now.
Eric Christopher30ef0e52010-06-03 04:07:48 +000013711 return BB;
13712}
13713
13714MachineBasicBlock *
Michael Liao6c0e04c2012-10-15 22:39:43 +000013715X86TargetLowering::emitEHSjLjSetJmp(MachineInstr *MI,
13716 MachineBasicBlock *MBB) const {
13717 DebugLoc DL = MI->getDebugLoc();
13718 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
13719
13720 MachineFunction *MF = MBB->getParent();
13721 MachineRegisterInfo &MRI = MF->getRegInfo();
13722
13723 const BasicBlock *BB = MBB->getBasicBlock();
13724 MachineFunction::iterator I = MBB;
13725 ++I;
13726
13727 // Memory Reference
13728 MachineInstr::mmo_iterator MMOBegin = MI->memoperands_begin();
13729 MachineInstr::mmo_iterator MMOEnd = MI->memoperands_end();
13730
13731 unsigned DstReg;
13732 unsigned MemOpndSlot = 0;
13733
13734 unsigned CurOp = 0;
13735
13736 DstReg = MI->getOperand(CurOp++).getReg();
13737 const TargetRegisterClass *RC = MRI.getRegClass(DstReg);
13738 assert(RC->hasType(MVT::i32) && "Invalid destination!");
13739 unsigned mainDstReg = MRI.createVirtualRegister(RC);
13740 unsigned restoreDstReg = MRI.createVirtualRegister(RC);
13741
13742 MemOpndSlot = CurOp;
13743
13744 MVT PVT = getPointerTy();
13745 assert((PVT == MVT::i64 || PVT == MVT::i32) &&
13746 "Invalid Pointer Size!");
13747
13748 // For v = setjmp(buf), we generate
13749 //
13750 // thisMBB:
Michael Liao281ae5a2012-10-17 02:22:27 +000013751 // buf[LabelOffset] = restoreMBB
Michael Liao6c0e04c2012-10-15 22:39:43 +000013752 // SjLjSetup restoreMBB
13753 //
13754 // mainMBB:
13755 // v_main = 0
13756 //
13757 // sinkMBB:
13758 // v = phi(main, restore)
13759 //
13760 // restoreMBB:
13761 // v_restore = 1
13762
13763 MachineBasicBlock *thisMBB = MBB;
13764 MachineBasicBlock *mainMBB = MF->CreateMachineBasicBlock(BB);
13765 MachineBasicBlock *sinkMBB = MF->CreateMachineBasicBlock(BB);
13766 MachineBasicBlock *restoreMBB = MF->CreateMachineBasicBlock(BB);
13767 MF->insert(I, mainMBB);
13768 MF->insert(I, sinkMBB);
13769 MF->push_back(restoreMBB);
13770
13771 MachineInstrBuilder MIB;
13772
13773 // Transfer the remainder of BB and its successor edges to sinkMBB.
13774 sinkMBB->splice(sinkMBB->begin(), MBB,
13775 llvm::next(MachineBasicBlock::iterator(MI)), MBB->end());
13776 sinkMBB->transferSuccessorsAndUpdatePHIs(MBB);
13777
13778 // thisMBB:
Michael Liao281ae5a2012-10-17 02:22:27 +000013779 unsigned PtrStoreOpc = 0;
13780 unsigned LabelReg = 0;
13781 const int64_t LabelOffset = 1 * PVT.getStoreSize();
13782 Reloc::Model RM = getTargetMachine().getRelocationModel();
13783 bool UseImmLabel = (getTargetMachine().getCodeModel() == CodeModel::Small) &&
13784 (RM == Reloc::Static || RM == Reloc::DynamicNoPIC);
Michael Liao6c0e04c2012-10-15 22:39:43 +000013785
Michael Liao281ae5a2012-10-17 02:22:27 +000013786 // Prepare IP either in reg or imm.
13787 if (!UseImmLabel) {
13788 PtrStoreOpc = (PVT == MVT::i64) ? X86::MOV64mr : X86::MOV32mr;
13789 const TargetRegisterClass *PtrRC = getRegClassFor(PVT);
13790 LabelReg = MRI.createVirtualRegister(PtrRC);
13791 if (Subtarget->is64Bit()) {
13792 MIB = BuildMI(*thisMBB, MI, DL, TII->get(X86::LEA64r), LabelReg)
13793 .addReg(X86::RIP)
13794 .addImm(0)
13795 .addReg(0)
13796 .addMBB(restoreMBB)
13797 .addReg(0);
13798 } else {
13799 const X86InstrInfo *XII = static_cast<const X86InstrInfo*>(TII);
13800 MIB = BuildMI(*thisMBB, MI, DL, TII->get(X86::LEA32r), LabelReg)
13801 .addReg(XII->getGlobalBaseReg(MF))
13802 .addImm(0)
13803 .addReg(0)
13804 .addMBB(restoreMBB, Subtarget->ClassifyBlockAddressReference())
13805 .addReg(0);
13806 }
13807 } else
13808 PtrStoreOpc = (PVT == MVT::i64) ? X86::MOV64mi32 : X86::MOV32mi;
Michael Liao6c0e04c2012-10-15 22:39:43 +000013809 // Store IP
Michael Liao281ae5a2012-10-17 02:22:27 +000013810 MIB = BuildMI(*thisMBB, MI, DL, TII->get(PtrStoreOpc));
Michael Liao6c0e04c2012-10-15 22:39:43 +000013811 for (unsigned i = 0; i < X86::AddrNumOperands; ++i) {
13812 if (i == X86::AddrDisp)
Michael Liao281ae5a2012-10-17 02:22:27 +000013813 MIB.addDisp(MI->getOperand(MemOpndSlot + i), LabelOffset);
Michael Liao6c0e04c2012-10-15 22:39:43 +000013814 else
13815 MIB.addOperand(MI->getOperand(MemOpndSlot + i));
13816 }
Michael Liao281ae5a2012-10-17 02:22:27 +000013817 if (!UseImmLabel)
13818 MIB.addReg(LabelReg);
13819 else
13820 MIB.addMBB(restoreMBB);
Michael Liao6c0e04c2012-10-15 22:39:43 +000013821 MIB.setMemRefs(MMOBegin, MMOEnd);
13822 // Setup
13823 MIB = BuildMI(*thisMBB, MI, DL, TII->get(X86::EH_SjLj_Setup))
13824 .addMBB(restoreMBB);
13825 MIB.addRegMask(RegInfo->getNoPreservedMask());
13826 thisMBB->addSuccessor(mainMBB);
13827 thisMBB->addSuccessor(restoreMBB);
13828
13829 // mainMBB:
13830 // EAX = 0
13831 BuildMI(mainMBB, DL, TII->get(X86::MOV32r0), mainDstReg);
13832 mainMBB->addSuccessor(sinkMBB);
13833
13834 // sinkMBB:
13835 BuildMI(*sinkMBB, sinkMBB->begin(), DL,
13836 TII->get(X86::PHI), DstReg)
13837 .addReg(mainDstReg).addMBB(mainMBB)
13838 .addReg(restoreDstReg).addMBB(restoreMBB);
13839
13840 // restoreMBB:
13841 BuildMI(restoreMBB, DL, TII->get(X86::MOV32ri), restoreDstReg).addImm(1);
13842 BuildMI(restoreMBB, DL, TII->get(X86::JMP_4)).addMBB(sinkMBB);
13843 restoreMBB->addSuccessor(sinkMBB);
13844
13845 MI->eraseFromParent();
13846 return sinkMBB;
13847}
13848
13849MachineBasicBlock *
13850X86TargetLowering::emitEHSjLjLongJmp(MachineInstr *MI,
13851 MachineBasicBlock *MBB) const {
13852 DebugLoc DL = MI->getDebugLoc();
13853 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
13854
13855 MachineFunction *MF = MBB->getParent();
13856 MachineRegisterInfo &MRI = MF->getRegInfo();
13857
13858 // Memory Reference
13859 MachineInstr::mmo_iterator MMOBegin = MI->memoperands_begin();
13860 MachineInstr::mmo_iterator MMOEnd = MI->memoperands_end();
13861
13862 MVT PVT = getPointerTy();
13863 assert((PVT == MVT::i64 || PVT == MVT::i32) &&
13864 "Invalid Pointer Size!");
13865
13866 const TargetRegisterClass *RC =
13867 (PVT == MVT::i64) ? &X86::GR64RegClass : &X86::GR32RegClass;
13868 unsigned Tmp = MRI.createVirtualRegister(RC);
13869 // Since FP is only updated here but NOT referenced, it's treated as GPR.
13870 unsigned FP = (PVT == MVT::i64) ? X86::RBP : X86::EBP;
13871 unsigned SP = RegInfo->getStackRegister();
13872
13873 MachineInstrBuilder MIB;
13874
Michael Liao281ae5a2012-10-17 02:22:27 +000013875 const int64_t LabelOffset = 1 * PVT.getStoreSize();
13876 const int64_t SPOffset = 2 * PVT.getStoreSize();
Michael Liao6c0e04c2012-10-15 22:39:43 +000013877
13878 unsigned PtrLoadOpc = (PVT == MVT::i64) ? X86::MOV64rm : X86::MOV32rm;
13879 unsigned IJmpOpc = (PVT == MVT::i64) ? X86::JMP64r : X86::JMP32r;
13880
13881 // Reload FP
13882 MIB = BuildMI(*MBB, MI, DL, TII->get(PtrLoadOpc), FP);
13883 for (unsigned i = 0; i < X86::AddrNumOperands; ++i)
13884 MIB.addOperand(MI->getOperand(i));
13885 MIB.setMemRefs(MMOBegin, MMOEnd);
13886 // Reload IP
13887 MIB = BuildMI(*MBB, MI, DL, TII->get(PtrLoadOpc), Tmp);
13888 for (unsigned i = 0; i < X86::AddrNumOperands; ++i) {
13889 if (i == X86::AddrDisp)
Michael Liao281ae5a2012-10-17 02:22:27 +000013890 MIB.addDisp(MI->getOperand(i), LabelOffset);
Michael Liao6c0e04c2012-10-15 22:39:43 +000013891 else
13892 MIB.addOperand(MI->getOperand(i));
13893 }
13894 MIB.setMemRefs(MMOBegin, MMOEnd);
13895 // Reload SP
13896 MIB = BuildMI(*MBB, MI, DL, TII->get(PtrLoadOpc), SP);
13897 for (unsigned i = 0; i < X86::AddrNumOperands; ++i) {
13898 if (i == X86::AddrDisp)
Michael Liao281ae5a2012-10-17 02:22:27 +000013899 MIB.addDisp(MI->getOperand(i), SPOffset);
Michael Liao6c0e04c2012-10-15 22:39:43 +000013900 else
13901 MIB.addOperand(MI->getOperand(i));
13902 }
13903 MIB.setMemRefs(MMOBegin, MMOEnd);
13904 // Jump
13905 BuildMI(*MBB, MI, DL, TII->get(IJmpOpc)).addReg(Tmp);
13906
13907 MI->eraseFromParent();
13908 return MBB;
13909}
13910
13911MachineBasicBlock *
Evan Chengff9b3732008-01-30 18:18:23 +000013912X86TargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000013913 MachineBasicBlock *BB) const {
Evan Cheng60c07e12006-07-05 22:17:51 +000013914 switch (MI->getOpcode()) {
Craig Topperabb94d02012-02-05 03:43:23 +000013915 default: llvm_unreachable("Unexpected instr type to insert");
NAKAMURA Takumi7754f852011-01-26 02:04:09 +000013916 case X86::TAILJMPd64:
13917 case X86::TAILJMPr64:
13918 case X86::TAILJMPm64:
Craig Topper6d1263a2012-02-05 05:38:58 +000013919 llvm_unreachable("TAILJMP64 would not be touched here.");
NAKAMURA Takumi7754f852011-01-26 02:04:09 +000013920 case X86::TCRETURNdi64:
13921 case X86::TCRETURNri64:
13922 case X86::TCRETURNmi64:
NAKAMURA Takumi7754f852011-01-26 02:04:09 +000013923 return BB;
Michael J. Spencere9c253e2010-10-21 01:41:01 +000013924 case X86::WIN_ALLOCA:
13925 return EmitLoweredWinAlloca(MI, BB);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000013926 case X86::SEG_ALLOCA_32:
13927 return EmitLoweredSegAlloca(MI, BB, false);
13928 case X86::SEG_ALLOCA_64:
13929 return EmitLoweredSegAlloca(MI, BB, true);
Eric Christopher30ef0e52010-06-03 04:07:48 +000013930 case X86::TLSCall_32:
13931 case X86::TLSCall_64:
13932 return EmitLoweredTLSCall(MI, BB);
Dan Gohmancbbea0f2009-08-27 00:14:12 +000013933 case X86::CMOV_GR8:
Evan Cheng60c07e12006-07-05 22:17:51 +000013934 case X86::CMOV_FR32:
13935 case X86::CMOV_FR64:
13936 case X86::CMOV_V4F32:
13937 case X86::CMOV_V2F64:
Chris Lattner52600972009-09-02 05:57:00 +000013938 case X86::CMOV_V2I64:
Bruno Cardoso Lopesd40aa242011-08-09 23:27:13 +000013939 case X86::CMOV_V8F32:
13940 case X86::CMOV_V4F64:
13941 case X86::CMOV_V4I64:
Chris Lattner314a1132010-03-14 18:31:44 +000013942 case X86::CMOV_GR16:
13943 case X86::CMOV_GR32:
13944 case X86::CMOV_RFP32:
13945 case X86::CMOV_RFP64:
13946 case X86::CMOV_RFP80:
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000013947 return EmitLoweredSelect(MI, BB);
Evan Cheng60c07e12006-07-05 22:17:51 +000013948
Dale Johannesen849f2142007-07-03 00:53:03 +000013949 case X86::FP32_TO_INT16_IN_MEM:
13950 case X86::FP32_TO_INT32_IN_MEM:
13951 case X86::FP32_TO_INT64_IN_MEM:
13952 case X86::FP64_TO_INT16_IN_MEM:
13953 case X86::FP64_TO_INT32_IN_MEM:
Dale Johannesena996d522007-08-07 01:17:37 +000013954 case X86::FP64_TO_INT64_IN_MEM:
13955 case X86::FP80_TO_INT16_IN_MEM:
13956 case X86::FP80_TO_INT32_IN_MEM:
13957 case X86::FP80_TO_INT64_IN_MEM: {
Chris Lattner52600972009-09-02 05:57:00 +000013958 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
13959 DebugLoc DL = MI->getDebugLoc();
13960
Evan Cheng60c07e12006-07-05 22:17:51 +000013961 // Change the floating point control register to use "round towards zero"
13962 // mode when truncating to an integer value.
13963 MachineFunction *F = BB->getParent();
David Greene3f2bf852009-11-12 20:49:22 +000013964 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2, false);
Dan Gohman14152b42010-07-06 20:24:04 +000013965 addFrameReference(BuildMI(*BB, MI, DL,
13966 TII->get(X86::FNSTCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000013967
13968 // Load the old value of the high byte of the control word...
13969 unsigned OldCW =
Craig Topperc9099502012-04-20 06:31:50 +000013970 F->getRegInfo().createVirtualRegister(&X86::GR16RegClass);
Dan Gohman14152b42010-07-06 20:24:04 +000013971 addFrameReference(BuildMI(*BB, MI, DL, TII->get(X86::MOV16rm), OldCW),
Dale Johannesene4d209d2009-02-03 20:21:25 +000013972 CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000013973
13974 // Set the high part to be round to zero...
Dan Gohman14152b42010-07-06 20:24:04 +000013975 addFrameReference(BuildMI(*BB, MI, DL, TII->get(X86::MOV16mi)), CWFrameIdx)
Evan Chengc0f64ff2006-11-27 23:37:22 +000013976 .addImm(0xC7F);
Evan Cheng60c07e12006-07-05 22:17:51 +000013977
13978 // Reload the modified control word now...
Dan Gohman14152b42010-07-06 20:24:04 +000013979 addFrameReference(BuildMI(*BB, MI, DL,
13980 TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000013981
13982 // Restore the memory image of control word to original value
Dan Gohman14152b42010-07-06 20:24:04 +000013983 addFrameReference(BuildMI(*BB, MI, DL, TII->get(X86::MOV16mr)), CWFrameIdx)
Evan Chengc0f64ff2006-11-27 23:37:22 +000013984 .addReg(OldCW);
Evan Cheng60c07e12006-07-05 22:17:51 +000013985
13986 // Get the X86 opcode to use.
13987 unsigned Opc;
13988 switch (MI->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +000013989 default: llvm_unreachable("illegal opcode!");
Dale Johannesene377d4d2007-07-04 21:07:47 +000013990 case X86::FP32_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m32; break;
13991 case X86::FP32_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m32; break;
13992 case X86::FP32_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m32; break;
13993 case X86::FP64_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m64; break;
13994 case X86::FP64_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m64; break;
13995 case X86::FP64_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m64; break;
Dale Johannesena996d522007-08-07 01:17:37 +000013996 case X86::FP80_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m80; break;
13997 case X86::FP80_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m80; break;
13998 case X86::FP80_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m80; break;
Evan Cheng60c07e12006-07-05 22:17:51 +000013999 }
14000
14001 X86AddressMode AM;
14002 MachineOperand &Op = MI->getOperand(0);
Dan Gohmand735b802008-10-03 15:45:36 +000014003 if (Op.isReg()) {
Evan Cheng60c07e12006-07-05 22:17:51 +000014004 AM.BaseType = X86AddressMode::RegBase;
14005 AM.Base.Reg = Op.getReg();
14006 } else {
14007 AM.BaseType = X86AddressMode::FrameIndexBase;
Chris Lattner8aa797a2007-12-30 23:10:15 +000014008 AM.Base.FrameIndex = Op.getIndex();
Evan Cheng60c07e12006-07-05 22:17:51 +000014009 }
14010 Op = MI->getOperand(1);
Dan Gohmand735b802008-10-03 15:45:36 +000014011 if (Op.isImm())
Chris Lattner7fbe9722006-10-20 17:42:20 +000014012 AM.Scale = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +000014013 Op = MI->getOperand(2);
Dan Gohmand735b802008-10-03 15:45:36 +000014014 if (Op.isImm())
Chris Lattner7fbe9722006-10-20 17:42:20 +000014015 AM.IndexReg = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +000014016 Op = MI->getOperand(3);
Dan Gohmand735b802008-10-03 15:45:36 +000014017 if (Op.isGlobal()) {
Evan Cheng60c07e12006-07-05 22:17:51 +000014018 AM.GV = Op.getGlobal();
14019 } else {
Chris Lattner7fbe9722006-10-20 17:42:20 +000014020 AM.Disp = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +000014021 }
Dan Gohman14152b42010-07-06 20:24:04 +000014022 addFullAddress(BuildMI(*BB, MI, DL, TII->get(Opc)), AM)
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000014023 .addReg(MI->getOperand(X86::AddrNumOperands).getReg());
Evan Cheng60c07e12006-07-05 22:17:51 +000014024
14025 // Reload the original control word now.
Dan Gohman14152b42010-07-06 20:24:04 +000014026 addFrameReference(BuildMI(*BB, MI, DL,
14027 TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000014028
Dan Gohman14152b42010-07-06 20:24:04 +000014029 MI->eraseFromParent(); // The pseudo instruction is gone now.
Evan Cheng60c07e12006-07-05 22:17:51 +000014030 return BB;
14031 }
Eric Christopherb120ab42009-08-18 22:50:32 +000014032 // String/text processing lowering.
14033 case X86::PCMPISTRM128REG:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000014034 case X86::VPCMPISTRM128REG:
Eric Christopherb120ab42009-08-18 22:50:32 +000014035 case X86::PCMPISTRM128MEM:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000014036 case X86::VPCMPISTRM128MEM:
Eric Christopherb120ab42009-08-18 22:50:32 +000014037 case X86::PCMPESTRM128REG:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000014038 case X86::VPCMPESTRM128REG:
Eric Christopherb120ab42009-08-18 22:50:32 +000014039 case X86::PCMPESTRM128MEM:
Craig Topper8aae8dd2012-11-10 08:57:41 +000014040 case X86::VPCMPESTRM128MEM:
14041 assert(Subtarget->hasSSE42() &&
14042 "Target must have SSE4.2 or AVX features enabled");
14043 return EmitPCMPSTRM(MI, BB, getTargetMachine().getInstrInfo());
Craig Topper9c7ae012012-11-10 01:23:36 +000014044
14045 // String/text processing lowering.
14046 case X86::PCMPISTRIREG:
14047 case X86::VPCMPISTRIREG:
14048 case X86::PCMPISTRIMEM:
14049 case X86::VPCMPISTRIMEM:
14050 case X86::PCMPESTRIREG:
14051 case X86::VPCMPESTRIREG:
14052 case X86::PCMPESTRIMEM:
Craig Topper8aae8dd2012-11-10 08:57:41 +000014053 case X86::VPCMPESTRIMEM:
14054 assert(Subtarget->hasSSE42() &&
14055 "Target must have SSE4.2 or AVX features enabled");
14056 return EmitPCMPSTRI(MI, BB, getTargetMachine().getInstrInfo());
Eric Christopherb120ab42009-08-18 22:50:32 +000014057
Craig Topper8aae8dd2012-11-10 08:57:41 +000014058 // Thread synchronization.
Eric Christopher228232b2010-11-30 07:20:12 +000014059 case X86::MONITOR:
Craig Topper2da36912012-11-11 22:45:02 +000014060 return EmitMonitor(MI, BB, getTargetMachine().getInstrInfo(), Subtarget);
Eric Christopher228232b2010-11-30 07:20:12 +000014061
Michael Liaobe02a902012-11-08 07:28:54 +000014062 // xbegin
14063 case X86::XBEGIN:
Craig Topper2da36912012-11-11 22:45:02 +000014064 return EmitXBegin(MI, BB, getTargetMachine().getInstrInfo());
Michael Liaobe02a902012-11-08 07:28:54 +000014065
Craig Topper8aae8dd2012-11-10 08:57:41 +000014066 // Atomic Lowering.
Dale Johannesen140be2d2008-08-19 18:47:28 +000014067 case X86::ATOMAND8:
Michael Liaob118a072012-09-20 03:06:15 +000014068 case X86::ATOMAND16:
14069 case X86::ATOMAND32:
Dale Johannesena99e3842008-08-20 00:48:50 +000014070 case X86::ATOMAND64:
Michael Liaob118a072012-09-20 03:06:15 +000014071 // Fall through
14072 case X86::ATOMOR8:
14073 case X86::ATOMOR16:
14074 case X86::ATOMOR32:
Dale Johannesena99e3842008-08-20 00:48:50 +000014075 case X86::ATOMOR64:
Michael Liaob118a072012-09-20 03:06:15 +000014076 // Fall through
14077 case X86::ATOMXOR16:
14078 case X86::ATOMXOR8:
14079 case X86::ATOMXOR32:
Dale Johannesena99e3842008-08-20 00:48:50 +000014080 case X86::ATOMXOR64:
Michael Liaob118a072012-09-20 03:06:15 +000014081 // Fall through
14082 case X86::ATOMNAND8:
14083 case X86::ATOMNAND16:
14084 case X86::ATOMNAND32:
14085 case X86::ATOMNAND64:
14086 // Fall through
Michael Liaofe87c302012-09-21 03:18:52 +000014087 case X86::ATOMMAX8:
Michael Liaob118a072012-09-20 03:06:15 +000014088 case X86::ATOMMAX16:
14089 case X86::ATOMMAX32:
14090 case X86::ATOMMAX64:
14091 // Fall through
Michael Liaofe87c302012-09-21 03:18:52 +000014092 case X86::ATOMMIN8:
Michael Liaob118a072012-09-20 03:06:15 +000014093 case X86::ATOMMIN16:
14094 case X86::ATOMMIN32:
14095 case X86::ATOMMIN64:
14096 // Fall through
Michael Liaofe87c302012-09-21 03:18:52 +000014097 case X86::ATOMUMAX8:
Michael Liaob118a072012-09-20 03:06:15 +000014098 case X86::ATOMUMAX16:
14099 case X86::ATOMUMAX32:
14100 case X86::ATOMUMAX64:
14101 // Fall through
Michael Liaofe87c302012-09-21 03:18:52 +000014102 case X86::ATOMUMIN8:
Michael Liaob118a072012-09-20 03:06:15 +000014103 case X86::ATOMUMIN16:
14104 case X86::ATOMUMIN32:
14105 case X86::ATOMUMIN64:
14106 return EmitAtomicLoadArith(MI, BB);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000014107
14108 // This group does 64-bit operations on a 32-bit host.
14109 case X86::ATOMAND6432:
Dale Johannesen48c1bc22008-10-02 18:53:47 +000014110 case X86::ATOMOR6432:
Dale Johannesen48c1bc22008-10-02 18:53:47 +000014111 case X86::ATOMXOR6432:
Dale Johannesen48c1bc22008-10-02 18:53:47 +000014112 case X86::ATOMNAND6432:
Dale Johannesen48c1bc22008-10-02 18:53:47 +000014113 case X86::ATOMADD6432:
Dale Johannesen48c1bc22008-10-02 18:53:47 +000014114 case X86::ATOMSUB6432:
Michael Liaoe5e8f762012-09-25 18:08:13 +000014115 case X86::ATOMMAX6432:
14116 case X86::ATOMMIN6432:
14117 case X86::ATOMUMAX6432:
14118 case X86::ATOMUMIN6432:
Michael Liaob118a072012-09-20 03:06:15 +000014119 case X86::ATOMSWAP6432:
14120 return EmitAtomicLoadArith6432(MI, BB);
Craig Topperacaaa6f2012-08-18 06:39:34 +000014121
Dan Gohmand6708ea2009-08-15 01:38:56 +000014122 case X86::VASTART_SAVE_XMM_REGS:
14123 return EmitVAStartSaveXMMRegsWithCustomInserter(MI, BB);
Dan Gohman320afb82010-10-12 18:00:49 +000014124
14125 case X86::VAARG_64:
14126 return EmitVAARG64WithCustomInserter(MI, BB);
Michael Liao6c0e04c2012-10-15 22:39:43 +000014127
14128 case X86::EH_SjLj_SetJmp32:
14129 case X86::EH_SjLj_SetJmp64:
14130 return emitEHSjLjSetJmp(MI, BB);
14131
14132 case X86::EH_SjLj_LongJmp32:
14133 case X86::EH_SjLj_LongJmp64:
14134 return emitEHSjLjLongJmp(MI, BB);
Evan Cheng60c07e12006-07-05 22:17:51 +000014135 }
14136}
14137
14138//===----------------------------------------------------------------------===//
14139// X86 Optimization Hooks
14140//===----------------------------------------------------------------------===//
14141
Dan Gohman475871a2008-07-27 21:46:04 +000014142void X86TargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
Dan Gohmanfd29e0e2008-02-13 00:35:47 +000014143 APInt &KnownZero,
14144 APInt &KnownOne,
Dan Gohmanea859be2007-06-22 14:59:07 +000014145 const SelectionDAG &DAG,
Nate Begeman368e18d2006-02-16 21:11:51 +000014146 unsigned Depth) const {
Rafael Espindola26c8dcc2012-04-04 12:51:34 +000014147 unsigned BitWidth = KnownZero.getBitWidth();
Evan Cheng3a03ebb2005-12-21 23:05:39 +000014148 unsigned Opc = Op.getOpcode();
Evan Cheng865f0602006-04-05 06:11:20 +000014149 assert((Opc >= ISD::BUILTIN_OP_END ||
14150 Opc == ISD::INTRINSIC_WO_CHAIN ||
14151 Opc == ISD::INTRINSIC_W_CHAIN ||
14152 Opc == ISD::INTRINSIC_VOID) &&
14153 "Should use MaskedValueIsZero if you don't know whether Op"
14154 " is a target node!");
Evan Cheng3a03ebb2005-12-21 23:05:39 +000014155
Rafael Espindola26c8dcc2012-04-04 12:51:34 +000014156 KnownZero = KnownOne = APInt(BitWidth, 0); // Don't know anything.
Evan Cheng3a03ebb2005-12-21 23:05:39 +000014157 switch (Opc) {
Evan Cheng865f0602006-04-05 06:11:20 +000014158 default: break;
Evan Cheng97d0e0e2009-02-02 09:15:04 +000014159 case X86ISD::ADD:
14160 case X86ISD::SUB:
Chris Lattner5b856542010-12-20 00:59:46 +000014161 case X86ISD::ADC:
14162 case X86ISD::SBB:
Evan Cheng97d0e0e2009-02-02 09:15:04 +000014163 case X86ISD::SMUL:
14164 case X86ISD::UMUL:
Dan Gohman076aee32009-03-04 19:44:21 +000014165 case X86ISD::INC:
14166 case X86ISD::DEC:
Dan Gohmane220c4b2009-09-18 19:59:53 +000014167 case X86ISD::OR:
14168 case X86ISD::XOR:
14169 case X86ISD::AND:
Evan Cheng97d0e0e2009-02-02 09:15:04 +000014170 // These nodes' second result is a boolean.
14171 if (Op.getResNo() == 0)
14172 break;
14173 // Fallthrough
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000014174 case X86ISD::SETCC:
Rafael Espindola26c8dcc2012-04-04 12:51:34 +000014175 KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - 1);
Nate Begeman368e18d2006-02-16 21:11:51 +000014176 break;
Evan Cheng7c1780c2011-10-07 17:21:44 +000014177 case ISD::INTRINSIC_WO_CHAIN: {
14178 unsigned IntId = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
14179 unsigned NumLoBits = 0;
14180 switch (IntId) {
14181 default: break;
14182 case Intrinsic::x86_sse_movmsk_ps:
14183 case Intrinsic::x86_avx_movmsk_ps_256:
14184 case Intrinsic::x86_sse2_movmsk_pd:
14185 case Intrinsic::x86_avx_movmsk_pd_256:
14186 case Intrinsic::x86_mmx_pmovmskb:
Craig Topper3738ccd2011-12-27 06:27:23 +000014187 case Intrinsic::x86_sse2_pmovmskb_128:
14188 case Intrinsic::x86_avx2_pmovmskb: {
Evan Cheng7c1780c2011-10-07 17:21:44 +000014189 // High bits of movmskp{s|d}, pmovmskb are known zero.
14190 switch (IntId) {
Craig Topperabb94d02012-02-05 03:43:23 +000014191 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
Evan Cheng7c1780c2011-10-07 17:21:44 +000014192 case Intrinsic::x86_sse_movmsk_ps: NumLoBits = 4; break;
14193 case Intrinsic::x86_avx_movmsk_ps_256: NumLoBits = 8; break;
14194 case Intrinsic::x86_sse2_movmsk_pd: NumLoBits = 2; break;
14195 case Intrinsic::x86_avx_movmsk_pd_256: NumLoBits = 4; break;
14196 case Intrinsic::x86_mmx_pmovmskb: NumLoBits = 8; break;
14197 case Intrinsic::x86_sse2_pmovmskb_128: NumLoBits = 16; break;
Craig Topper3738ccd2011-12-27 06:27:23 +000014198 case Intrinsic::x86_avx2_pmovmskb: NumLoBits = 32; break;
Evan Cheng7c1780c2011-10-07 17:21:44 +000014199 }
Rafael Espindola26c8dcc2012-04-04 12:51:34 +000014200 KnownZero = APInt::getHighBitsSet(BitWidth, BitWidth - NumLoBits);
Evan Cheng7c1780c2011-10-07 17:21:44 +000014201 break;
14202 }
14203 }
14204 break;
14205 }
Evan Cheng3a03ebb2005-12-21 23:05:39 +000014206 }
Evan Cheng3a03ebb2005-12-21 23:05:39 +000014207}
Chris Lattner259e97c2006-01-31 19:43:35 +000014208
Owen Andersonbc146b02010-09-21 20:42:50 +000014209unsigned X86TargetLowering::ComputeNumSignBitsForTargetNode(SDValue Op,
14210 unsigned Depth) const {
14211 // SETCC_CARRY sets the dest to ~0 for true or 0 for false.
14212 if (Op.getOpcode() == X86ISD::SETCC_CARRY)
14213 return Op.getValueType().getScalarType().getSizeInBits();
Michael J. Spencerec38de22010-10-10 22:04:20 +000014214
Owen Andersonbc146b02010-09-21 20:42:50 +000014215 // Fallback case.
14216 return 1;
14217}
14218
Evan Cheng206ee9d2006-07-07 08:33:52 +000014219/// isGAPlusOffset - Returns true (and the GlobalValue and the offset) if the
Evan Chengad4196b2008-05-12 19:56:52 +000014220/// node is a GlobalAddress + offset.
14221bool X86TargetLowering::isGAPlusOffset(SDNode *N,
Dan Gohman46510a72010-04-15 01:51:59 +000014222 const GlobalValue* &GA,
14223 int64_t &Offset) const {
Evan Chengad4196b2008-05-12 19:56:52 +000014224 if (N->getOpcode() == X86ISD::Wrapper) {
14225 if (isa<GlobalAddressSDNode>(N->getOperand(0))) {
Evan Cheng206ee9d2006-07-07 08:33:52 +000014226 GA = cast<GlobalAddressSDNode>(N->getOperand(0))->getGlobal();
Dan Gohman6520e202008-10-18 02:06:02 +000014227 Offset = cast<GlobalAddressSDNode>(N->getOperand(0))->getOffset();
Evan Cheng206ee9d2006-07-07 08:33:52 +000014228 return true;
14229 }
Evan Cheng206ee9d2006-07-07 08:33:52 +000014230 }
Evan Chengad4196b2008-05-12 19:56:52 +000014231 return TargetLowering::isGAPlusOffset(N, GA, Offset);
Evan Cheng206ee9d2006-07-07 08:33:52 +000014232}
14233
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000014234/// isShuffleHigh128VectorInsertLow - Checks whether the shuffle node is the
14235/// same as extracting the high 128-bit part of 256-bit vector and then
14236/// inserting the result into the low part of a new 256-bit vector
14237static bool isShuffleHigh128VectorInsertLow(ShuffleVectorSDNode *SVOp) {
14238 EVT VT = SVOp->getValueType(0);
Craig Topper66ddd152012-04-27 22:54:43 +000014239 unsigned NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000014240
14241 // vector_shuffle <4, 5, 6, 7, u, u, u, u> or <2, 3, u, u>
Craig Topper66ddd152012-04-27 22:54:43 +000014242 for (unsigned i = 0, j = NumElems/2; i != NumElems/2; ++i, ++j)
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000014243 if (!isUndefOrEqual(SVOp->getMaskElt(i), j) ||
14244 SVOp->getMaskElt(j) >= 0)
14245 return false;
14246
14247 return true;
14248}
14249
14250/// isShuffleLow128VectorInsertHigh - Checks whether the shuffle node is the
14251/// same as extracting the low 128-bit part of 256-bit vector and then
14252/// inserting the result into the high part of a new 256-bit vector
14253static bool isShuffleLow128VectorInsertHigh(ShuffleVectorSDNode *SVOp) {
14254 EVT VT = SVOp->getValueType(0);
Craig Topper66ddd152012-04-27 22:54:43 +000014255 unsigned NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000014256
14257 // vector_shuffle <u, u, u, u, 0, 1, 2, 3> or <u, u, 0, 1>
Craig Topper66ddd152012-04-27 22:54:43 +000014258 for (unsigned i = NumElems/2, j = 0; i != NumElems; ++i, ++j)
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000014259 if (!isUndefOrEqual(SVOp->getMaskElt(i), j) ||
14260 SVOp->getMaskElt(j) >= 0)
14261 return false;
14262
14263 return true;
14264}
14265
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000014266/// PerformShuffleCombine256 - Performs shuffle combines for 256-bit vectors.
14267static SDValue PerformShuffleCombine256(SDNode *N, SelectionDAG &DAG,
Craig Topper12216172012-01-13 08:12:35 +000014268 TargetLowering::DAGCombinerInfo &DCI,
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +000014269 const X86Subtarget* Subtarget) {
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000014270 DebugLoc dl = N->getDebugLoc();
14271 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
14272 SDValue V1 = SVOp->getOperand(0);
14273 SDValue V2 = SVOp->getOperand(1);
14274 EVT VT = SVOp->getValueType(0);
Craig Topper66ddd152012-04-27 22:54:43 +000014275 unsigned NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000014276
14277 if (V1.getOpcode() == ISD::CONCAT_VECTORS &&
14278 V2.getOpcode() == ISD::CONCAT_VECTORS) {
14279 //
14280 // 0,0,0,...
Benjamin Kramer558cc5a2011-07-22 01:02:57 +000014281 // |
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000014282 // V UNDEF BUILD_VECTOR UNDEF
14283 // \ / \ /
14284 // CONCAT_VECTOR CONCAT_VECTOR
14285 // \ /
14286 // \ /
14287 // RESULT: V + zero extended
14288 //
14289 if (V2.getOperand(0).getOpcode() != ISD::BUILD_VECTOR ||
14290 V2.getOperand(1).getOpcode() != ISD::UNDEF ||
14291 V1.getOperand(1).getOpcode() != ISD::UNDEF)
14292 return SDValue();
14293
14294 if (!ISD::isBuildVectorAllZeros(V2.getOperand(0).getNode()))
14295 return SDValue();
14296
14297 // To match the shuffle mask, the first half of the mask should
14298 // be exactly the first vector, and all the rest a splat with the
14299 // first element of the second one.
Craig Topper66ddd152012-04-27 22:54:43 +000014300 for (unsigned i = 0; i != NumElems/2; ++i)
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000014301 if (!isUndefOrEqual(SVOp->getMaskElt(i), i) ||
14302 !isUndefOrEqual(SVOp->getMaskElt(i+NumElems/2), NumElems))
14303 return SDValue();
14304
Chad Rosier3d1161e2012-01-03 21:05:52 +000014305 // If V1 is coming from a vector load then just fold to a VZEXT_LOAD.
14306 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(V1.getOperand(0))) {
Chad Rosier42726832012-05-07 18:47:44 +000014307 if (Ld->hasNUsesOfValue(1, 0)) {
14308 SDVTList Tys = DAG.getVTList(MVT::v4i64, MVT::Other);
14309 SDValue Ops[] = { Ld->getChain(), Ld->getBasePtr() };
14310 SDValue ResNode =
14311 DAG.getMemIntrinsicNode(X86ISD::VZEXT_LOAD, dl, Tys, Ops, 2,
14312 Ld->getMemoryVT(),
14313 Ld->getPointerInfo(),
14314 Ld->getAlignment(),
14315 false/*isVolatile*/, true/*ReadMem*/,
14316 false/*WriteMem*/);
Manman Ren2adc5032012-11-13 19:13:05 +000014317
14318 // Make sure the newly-created LOAD is in the same position as Ld in
14319 // terms of dependency. We create a TokenFactor for Ld and ResNode,
14320 // and update uses of Ld's output chain to use the TokenFactor.
14321 if (Ld->hasAnyUseOfValue(1)) {
14322 SDValue NewChain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
14323 SDValue(Ld, 1), SDValue(ResNode.getNode(), 1));
14324 DAG.ReplaceAllUsesOfValueWith(SDValue(Ld, 1), NewChain);
14325 DAG.UpdateNodeOperands(NewChain.getNode(), SDValue(Ld, 1),
14326 SDValue(ResNode.getNode(), 1));
14327 }
14328
Chad Rosier42726832012-05-07 18:47:44 +000014329 return DAG.getNode(ISD::BITCAST, dl, VT, ResNode);
14330 }
Chad Rosiera20e1e72012-08-01 18:39:17 +000014331 }
Chad Rosier3d1161e2012-01-03 21:05:52 +000014332
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000014333 // Emit a zeroed vector and insert the desired subvector on its
14334 // first half.
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +000014335 SDValue Zeros = getZeroVector(VT, Subtarget, DAG, dl);
Craig Topperb14940a2012-04-22 20:55:18 +000014336 SDValue InsV = Insert128BitVector(Zeros, V1.getOperand(0), 0, DAG, dl);
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000014337 return DCI.CombineTo(N, InsV);
14338 }
14339
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000014340 //===--------------------------------------------------------------------===//
14341 // Combine some shuffles into subvector extracts and inserts:
14342 //
14343
14344 // vector_shuffle <4, 5, 6, 7, u, u, u, u> or <2, 3, u, u>
14345 if (isShuffleHigh128VectorInsertLow(SVOp)) {
Craig Topperb14940a2012-04-22 20:55:18 +000014346 SDValue V = Extract128BitVector(V1, NumElems/2, DAG, dl);
14347 SDValue InsV = Insert128BitVector(DAG.getUNDEF(VT), V, 0, DAG, dl);
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000014348 return DCI.CombineTo(N, InsV);
14349 }
14350
14351 // vector_shuffle <u, u, u, u, 0, 1, 2, 3> or <u, u, 0, 1>
14352 if (isShuffleLow128VectorInsertHigh(SVOp)) {
Craig Topperb14940a2012-04-22 20:55:18 +000014353 SDValue V = Extract128BitVector(V1, 0, DAG, dl);
14354 SDValue InsV = Insert128BitVector(DAG.getUNDEF(VT), V, NumElems/2, DAG, dl);
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000014355 return DCI.CombineTo(N, InsV);
14356 }
14357
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000014358 return SDValue();
14359}
14360
14361/// PerformShuffleCombine - Performs several different shuffle combines.
Dan Gohman475871a2008-07-27 21:46:04 +000014362static SDValue PerformShuffleCombine(SDNode *N, SelectionDAG &DAG,
Bruno Cardoso Lopes50b37c72011-08-15 21:45:54 +000014363 TargetLowering::DAGCombinerInfo &DCI,
14364 const X86Subtarget *Subtarget) {
Dale Johannesene4d209d2009-02-03 20:21:25 +000014365 DebugLoc dl = N->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +000014366 EVT VT = N->getValueType(0);
Mon P Wang1e955802009-04-03 02:43:30 +000014367
Mon P Wanga0fd0d52010-12-19 23:55:53 +000014368 // Don't create instructions with illegal types after legalize types has run.
14369 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
14370 if (!DCI.isBeforeLegalize() && !TLI.isTypeLegal(VT.getVectorElementType()))
14371 return SDValue();
14372
Bruno Cardoso Lopes50b37c72011-08-15 21:45:54 +000014373 // Combine 256-bit vector shuffles. This is only profitable when in AVX mode
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000014374 if (Subtarget->hasFp256() && VT.is256BitVector() &&
Bruno Cardoso Lopes50b37c72011-08-15 21:45:54 +000014375 N->getOpcode() == ISD::VECTOR_SHUFFLE)
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +000014376 return PerformShuffleCombine256(N, DAG, DCI, Subtarget);
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000014377
14378 // Only handle 128 wide vector from here on.
Craig Topper7a9a28b2012-08-12 02:23:29 +000014379 if (!VT.is128BitVector())
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000014380 return SDValue();
14381
14382 // Combine a vector_shuffle that is equal to build_vector load1, load2, load3,
14383 // load4, <0, 1, 2, 3> into a 128-bit load if the load addresses are
14384 // consecutive, non-overlapping, and in the right order.
Nate Begemanfdea31a2010-03-24 20:49:50 +000014385 SmallVector<SDValue, 16> Elts;
14386 for (unsigned i = 0, e = VT.getVectorNumElements(); i != e; ++i)
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000014387 Elts.push_back(getShuffleScalarElt(N, i, DAG, 0));
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +000014388
Nate Begemanfdea31a2010-03-24 20:49:50 +000014389 return EltsFromConsecutiveLoads(VT, Elts, dl, DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +000014390}
Evan Chengd880b972008-05-09 21:53:03 +000014391
Craig Topper55b24052012-09-11 06:15:32 +000014392/// PerformTruncateCombine - Converts truncate operation to
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000014393/// a sequence of vector shuffle operations.
14394/// It is possible when we truncate 256-bit vector to 128-bit vector
Craig Topper55b24052012-09-11 06:15:32 +000014395static SDValue PerformTruncateCombine(SDNode *N, SelectionDAG &DAG,
14396 TargetLowering::DAGCombinerInfo &DCI,
14397 const X86Subtarget *Subtarget) {
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000014398 if (!DCI.isBeforeLegalizeOps())
14399 return SDValue();
14400
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000014401 if (!Subtarget->hasFp256())
Craig Topper3ef43cf2012-04-24 06:36:35 +000014402 return SDValue();
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000014403
14404 EVT VT = N->getValueType(0);
14405 SDValue Op = N->getOperand(0);
14406 EVT OpVT = Op.getValueType();
14407 DebugLoc dl = N->getDebugLoc();
14408
14409 if ((VT == MVT::v4i32) && (OpVT == MVT::v4i64)) {
14410
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000014411 if (Subtarget->hasInt256()) {
Elena Demikhovsky1da58672012-04-22 09:39:03 +000014412 // AVX2: v4i64 -> v4i32
14413
14414 // VPERMD
14415 static const int ShufMask[] = {0, 2, 4, 6, -1, -1, -1, -1};
14416
14417 Op = DAG.getNode(ISD::BITCAST, dl, MVT::v8i32, Op);
14418 Op = DAG.getVectorShuffle(MVT::v8i32, dl, Op, DAG.getUNDEF(MVT::v8i32),
14419 ShufMask);
14420
Craig Topperd63fa652012-04-22 18:51:37 +000014421 return DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT, Op,
14422 DAG.getIntPtrConstant(0));
Elena Demikhovsky1da58672012-04-22 09:39:03 +000014423 }
14424
14425 // AVX: v4i64 -> v4i32
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000014426 SDValue OpLo = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v2i64, Op,
Craig Topperd63fa652012-04-22 18:51:37 +000014427 DAG.getIntPtrConstant(0));
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000014428
14429 SDValue OpHi = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v2i64, Op,
Craig Topperd63fa652012-04-22 18:51:37 +000014430 DAG.getIntPtrConstant(2));
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000014431
14432 OpLo = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, OpLo);
14433 OpHi = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, OpHi);
14434
14435 // PSHUFD
Craig Topper9e401f22012-04-21 18:58:38 +000014436 static const int ShufMask1[] = {0, 2, 0, 0};
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000014437
Craig Toppercacafd42012-08-14 08:18:43 +000014438 SDValue Undef = DAG.getUNDEF(VT);
14439 OpLo = DAG.getVectorShuffle(VT, dl, OpLo, Undef, ShufMask1);
14440 OpHi = DAG.getVectorShuffle(VT, dl, OpHi, Undef, ShufMask1);
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000014441
14442 // MOVLHPS
Craig Topper9e401f22012-04-21 18:58:38 +000014443 static const int ShufMask2[] = {0, 1, 4, 5};
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000014444
Elena Demikhovsky73252572012-02-01 10:33:05 +000014445 return DAG.getVectorShuffle(VT, dl, OpLo, OpHi, ShufMask2);
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000014446 }
Craig Topperd63fa652012-04-22 18:51:37 +000014447
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000014448 if ((VT == MVT::v8i16) && (OpVT == MVT::v8i32)) {
14449
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000014450 if (Subtarget->hasInt256()) {
Elena Demikhovsky1da58672012-04-22 09:39:03 +000014451 // AVX2: v8i32 -> v8i16
14452
14453 Op = DAG.getNode(ISD::BITCAST, dl, MVT::v32i8, Op);
Craig Topperd63fa652012-04-22 18:51:37 +000014454
Elena Demikhovsky1da58672012-04-22 09:39:03 +000014455 // PSHUFB
14456 SmallVector<SDValue,32> pshufbMask;
14457 for (unsigned i = 0; i < 2; ++i) {
14458 pshufbMask.push_back(DAG.getConstant(0x0, MVT::i8));
14459 pshufbMask.push_back(DAG.getConstant(0x1, MVT::i8));
14460 pshufbMask.push_back(DAG.getConstant(0x4, MVT::i8));
14461 pshufbMask.push_back(DAG.getConstant(0x5, MVT::i8));
14462 pshufbMask.push_back(DAG.getConstant(0x8, MVT::i8));
14463 pshufbMask.push_back(DAG.getConstant(0x9, MVT::i8));
14464 pshufbMask.push_back(DAG.getConstant(0xc, MVT::i8));
14465 pshufbMask.push_back(DAG.getConstant(0xd, MVT::i8));
14466 for (unsigned j = 0; j < 8; ++j)
14467 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
14468 }
Craig Topperd63fa652012-04-22 18:51:37 +000014469 SDValue BV = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v32i8,
14470 &pshufbMask[0], 32);
Elena Demikhovsky1da58672012-04-22 09:39:03 +000014471 Op = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v32i8, Op, BV);
14472
14473 Op = DAG.getNode(ISD::BITCAST, dl, MVT::v4i64, Op);
14474
14475 static const int ShufMask[] = {0, 2, -1, -1};
Craig Topperd63fa652012-04-22 18:51:37 +000014476 Op = DAG.getVectorShuffle(MVT::v4i64, dl, Op, DAG.getUNDEF(MVT::v4i64),
Elena Demikhovsky1da58672012-04-22 09:39:03 +000014477 &ShufMask[0]);
14478
Craig Topperd63fa652012-04-22 18:51:37 +000014479 Op = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v2i64, Op,
14480 DAG.getIntPtrConstant(0));
Elena Demikhovsky1da58672012-04-22 09:39:03 +000014481
14482 return DAG.getNode(ISD::BITCAST, dl, VT, Op);
14483 }
14484
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000014485 SDValue OpLo = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i32, Op,
Craig Topperd63fa652012-04-22 18:51:37 +000014486 DAG.getIntPtrConstant(0));
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000014487
14488 SDValue OpHi = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i32, Op,
Craig Topperd63fa652012-04-22 18:51:37 +000014489 DAG.getIntPtrConstant(4));
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000014490
14491 OpLo = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, OpLo);
14492 OpHi = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, OpHi);
14493
14494 // PSHUFB
Craig Topper9e401f22012-04-21 18:58:38 +000014495 static const int ShufMask1[] = {0, 1, 4, 5, 8, 9, 12, 13,
14496 -1, -1, -1, -1, -1, -1, -1, -1};
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000014497
Craig Toppercacafd42012-08-14 08:18:43 +000014498 SDValue Undef = DAG.getUNDEF(MVT::v16i8);
14499 OpLo = DAG.getVectorShuffle(MVT::v16i8, dl, OpLo, Undef, ShufMask1);
14500 OpHi = DAG.getVectorShuffle(MVT::v16i8, dl, OpHi, Undef, ShufMask1);
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000014501
14502 OpLo = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, OpLo);
14503 OpHi = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, OpHi);
14504
14505 // MOVLHPS
Craig Topper9e401f22012-04-21 18:58:38 +000014506 static const int ShufMask2[] = {0, 1, 4, 5};
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000014507
Elena Demikhovsky73252572012-02-01 10:33:05 +000014508 SDValue res = DAG.getVectorShuffle(MVT::v4i32, dl, OpLo, OpHi, ShufMask2);
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000014509 return DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, res);
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000014510 }
14511
14512 return SDValue();
14513}
14514
Craig Topper89f4e662012-03-20 07:17:59 +000014515/// XFormVExtractWithShuffleIntoLoad - Check if a vector extract from a target
14516/// specific shuffle of a load can be folded into a single element load.
14517/// Similar handling for VECTOR_SHUFFLE is performed by DAGCombiner, but
14518/// shuffles have been customed lowered so we need to handle those here.
14519static SDValue XFormVExtractWithShuffleIntoLoad(SDNode *N, SelectionDAG &DAG,
14520 TargetLowering::DAGCombinerInfo &DCI) {
14521 if (DCI.isBeforeLegalizeOps())
14522 return SDValue();
14523
14524 SDValue InVec = N->getOperand(0);
14525 SDValue EltNo = N->getOperand(1);
14526
14527 if (!isa<ConstantSDNode>(EltNo))
14528 return SDValue();
14529
14530 EVT VT = InVec.getValueType();
14531
14532 bool HasShuffleIntoBitcast = false;
14533 if (InVec.getOpcode() == ISD::BITCAST) {
14534 // Don't duplicate a load with other uses.
14535 if (!InVec.hasOneUse())
14536 return SDValue();
14537 EVT BCVT = InVec.getOperand(0).getValueType();
14538 if (BCVT.getVectorNumElements() != VT.getVectorNumElements())
14539 return SDValue();
14540 InVec = InVec.getOperand(0);
14541 HasShuffleIntoBitcast = true;
14542 }
14543
14544 if (!isTargetShuffle(InVec.getOpcode()))
14545 return SDValue();
14546
14547 // Don't duplicate a load with other uses.
14548 if (!InVec.hasOneUse())
14549 return SDValue();
14550
14551 SmallVector<int, 16> ShuffleMask;
14552 bool UnaryShuffle;
Craig Topperd978c542012-05-06 19:46:21 +000014553 if (!getTargetShuffleMask(InVec.getNode(), VT.getSimpleVT(), ShuffleMask,
14554 UnaryShuffle))
Craig Topper89f4e662012-03-20 07:17:59 +000014555 return SDValue();
14556
14557 // Select the input vector, guarding against out of range extract vector.
14558 unsigned NumElems = VT.getVectorNumElements();
14559 int Elt = cast<ConstantSDNode>(EltNo)->getZExtValue();
14560 int Idx = (Elt > (int)NumElems) ? -1 : ShuffleMask[Elt];
14561 SDValue LdNode = (Idx < (int)NumElems) ? InVec.getOperand(0)
14562 : InVec.getOperand(1);
14563
14564 // If inputs to shuffle are the same for both ops, then allow 2 uses
14565 unsigned AllowedUses = InVec.getOperand(0) == InVec.getOperand(1) ? 2 : 1;
14566
14567 if (LdNode.getOpcode() == ISD::BITCAST) {
14568 // Don't duplicate a load with other uses.
14569 if (!LdNode.getNode()->hasNUsesOfValue(AllowedUses, 0))
14570 return SDValue();
14571
14572 AllowedUses = 1; // only allow 1 load use if we have a bitcast
14573 LdNode = LdNode.getOperand(0);
14574 }
14575
14576 if (!ISD::isNormalLoad(LdNode.getNode()))
14577 return SDValue();
14578
14579 LoadSDNode *LN0 = cast<LoadSDNode>(LdNode);
14580
14581 if (!LN0 ||!LN0->hasNUsesOfValue(AllowedUses, 0) || LN0->isVolatile())
14582 return SDValue();
14583
14584 if (HasShuffleIntoBitcast) {
14585 // If there's a bitcast before the shuffle, check if the load type and
14586 // alignment is valid.
14587 unsigned Align = LN0->getAlignment();
14588 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Micah Villmow3574eca2012-10-08 16:38:25 +000014589 unsigned NewAlign = TLI.getDataLayout()->
Craig Topper89f4e662012-03-20 07:17:59 +000014590 getABITypeAlignment(VT.getTypeForEVT(*DAG.getContext()));
14591
14592 if (NewAlign > Align || !TLI.isOperationLegalOrCustom(ISD::LOAD, VT))
14593 return SDValue();
14594 }
14595
14596 // All checks match so transform back to vector_shuffle so that DAG combiner
14597 // can finish the job
14598 DebugLoc dl = N->getDebugLoc();
14599
14600 // Create shuffle node taking into account the case that its a unary shuffle
14601 SDValue Shuffle = (UnaryShuffle) ? DAG.getUNDEF(VT) : InVec.getOperand(1);
14602 Shuffle = DAG.getVectorShuffle(InVec.getValueType(), dl,
14603 InVec.getOperand(0), Shuffle,
14604 &ShuffleMask[0]);
14605 Shuffle = DAG.getNode(ISD::BITCAST, dl, VT, Shuffle);
14606 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, N->getValueType(0), Shuffle,
14607 EltNo);
14608}
14609
Bruno Cardoso Lopesb3e06692010-09-03 19:55:05 +000014610/// PerformEXTRACT_VECTOR_ELTCombine - Detect vector gather/scatter index
14611/// generation and convert it from being a bunch of shuffles and extracts
14612/// to a simple store and scalar loads to extract the elements.
Dan Gohman1bbf72b2010-03-15 23:23:03 +000014613static SDValue PerformEXTRACT_VECTOR_ELTCombine(SDNode *N, SelectionDAG &DAG,
Craig Topper89f4e662012-03-20 07:17:59 +000014614 TargetLowering::DAGCombinerInfo &DCI) {
14615 SDValue NewOp = XFormVExtractWithShuffleIntoLoad(N, DAG, DCI);
14616 if (NewOp.getNode())
14617 return NewOp;
14618
Dan Gohman1bbf72b2010-03-15 23:23:03 +000014619 SDValue InputVector = N->getOperand(0);
Manman Ren4c74a952012-10-30 22:15:38 +000014620 // Detect whether we are trying to convert from mmx to i32 and the bitcast
14621 // from mmx to v2i32 has a single usage.
14622 if (InputVector.getNode()->getOpcode() == llvm::ISD::BITCAST &&
14623 InputVector.getNode()->getOperand(0).getValueType() == MVT::x86mmx &&
14624 InputVector.hasOneUse() && N->getValueType(0) == MVT::i32)
14625 return DAG.getNode(X86ISD::MMX_MOVD2W, InputVector.getDebugLoc(),
14626 N->getValueType(0),
14627 InputVector.getNode()->getOperand(0));
Dan Gohman1bbf72b2010-03-15 23:23:03 +000014628
14629 // Only operate on vectors of 4 elements, where the alternative shuffling
14630 // gets to be more expensive.
14631 if (InputVector.getValueType() != MVT::v4i32)
14632 return SDValue();
14633
14634 // Check whether every use of InputVector is an EXTRACT_VECTOR_ELT with a
14635 // single use which is a sign-extend or zero-extend, and all elements are
14636 // used.
14637 SmallVector<SDNode *, 4> Uses;
14638 unsigned ExtractedElements = 0;
14639 for (SDNode::use_iterator UI = InputVector.getNode()->use_begin(),
14640 UE = InputVector.getNode()->use_end(); UI != UE; ++UI) {
14641 if (UI.getUse().getResNo() != InputVector.getResNo())
14642 return SDValue();
14643
14644 SDNode *Extract = *UI;
14645 if (Extract->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
14646 return SDValue();
14647
14648 if (Extract->getValueType(0) != MVT::i32)
14649 return SDValue();
14650 if (!Extract->hasOneUse())
14651 return SDValue();
14652 if (Extract->use_begin()->getOpcode() != ISD::SIGN_EXTEND &&
14653 Extract->use_begin()->getOpcode() != ISD::ZERO_EXTEND)
14654 return SDValue();
14655 if (!isa<ConstantSDNode>(Extract->getOperand(1)))
14656 return SDValue();
14657
14658 // Record which element was extracted.
14659 ExtractedElements |=
14660 1 << cast<ConstantSDNode>(Extract->getOperand(1))->getZExtValue();
14661
14662 Uses.push_back(Extract);
14663 }
14664
14665 // If not all the elements were used, this may not be worthwhile.
14666 if (ExtractedElements != 15)
14667 return SDValue();
14668
14669 // Ok, we've now decided to do the transformation.
14670 DebugLoc dl = InputVector.getDebugLoc();
14671
14672 // Store the value to a temporary stack slot.
14673 SDValue StackPtr = DAG.CreateStackTemporary(InputVector.getValueType());
Chris Lattner8026a9d2010-09-21 17:50:43 +000014674 SDValue Ch = DAG.getStore(DAG.getEntryNode(), dl, InputVector, StackPtr,
14675 MachinePointerInfo(), false, false, 0);
Dan Gohman1bbf72b2010-03-15 23:23:03 +000014676
14677 // Replace each use (extract) with a load of the appropriate element.
14678 for (SmallVectorImpl<SDNode *>::iterator UI = Uses.begin(),
14679 UE = Uses.end(); UI != UE; ++UI) {
14680 SDNode *Extract = *UI;
14681
Nadav Rotem86694292011-05-17 08:31:57 +000014682 // cOMpute the element's address.
Dan Gohman1bbf72b2010-03-15 23:23:03 +000014683 SDValue Idx = Extract->getOperand(1);
14684 unsigned EltSize =
14685 InputVector.getValueType().getVectorElementType().getSizeInBits()/8;
14686 uint64_t Offset = EltSize * cast<ConstantSDNode>(Idx)->getZExtValue();
Craig Topper89f4e662012-03-20 07:17:59 +000014687 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Dan Gohman1bbf72b2010-03-15 23:23:03 +000014688 SDValue OffsetVal = DAG.getConstant(Offset, TLI.getPointerTy());
14689
Nadav Rotem86694292011-05-17 08:31:57 +000014690 SDValue ScalarAddr = DAG.getNode(ISD::ADD, dl, TLI.getPointerTy(),
Chris Lattner51abfe42010-09-21 06:02:19 +000014691 StackPtr, OffsetVal);
Dan Gohman1bbf72b2010-03-15 23:23:03 +000014692
14693 // Load the scalar.
Eric Christopher90eb4022010-07-22 00:26:08 +000014694 SDValue LoadScalar = DAG.getLoad(Extract->getValueType(0), dl, Ch,
Chris Lattner51abfe42010-09-21 06:02:19 +000014695 ScalarAddr, MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000014696 false, false, false, 0);
Dan Gohman1bbf72b2010-03-15 23:23:03 +000014697
14698 // Replace the exact with the load.
14699 DAG.ReplaceAllUsesOfValueWith(SDValue(Extract, 0), LoadScalar);
14700 }
14701
14702 // The replacement was made in place; don't return anything.
14703 return SDValue();
14704}
14705
Benjamin Kramer2556c6b2012-12-21 17:46:58 +000014706/// \brief Matches a VSELECT onto min/max or return 0 if the node doesn't match.
14707static unsigned matchIntegerMINMAX(SDValue Cond, EVT VT, SDValue LHS,
14708 SDValue RHS, SelectionDAG &DAG,
14709 const X86Subtarget *Subtarget) {
14710 if (!VT.isVector())
14711 return 0;
14712
14713 switch (VT.getSimpleVT().SimpleTy) {
14714 default: return 0;
14715 case MVT::v32i8:
14716 case MVT::v16i16:
14717 case MVT::v8i32:
14718 if (!Subtarget->hasAVX2())
14719 return 0;
14720 case MVT::v16i8:
14721 case MVT::v8i16:
14722 case MVT::v4i32:
14723 if (!Subtarget->hasSSE2())
14724 return 0;
14725 }
14726
14727 // SSE2 has only a small subset of the operations.
14728 bool hasUnsigned = Subtarget->hasSSE41() ||
14729 (Subtarget->hasSSE2() && VT == MVT::v16i8);
14730 bool hasSigned = Subtarget->hasSSE41() ||
14731 (Subtarget->hasSSE2() && VT == MVT::v8i16);
14732
14733 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
14734
14735 // Check for x CC y ? x : y.
14736 if (DAG.isEqualTo(LHS, Cond.getOperand(0)) &&
14737 DAG.isEqualTo(RHS, Cond.getOperand(1))) {
14738 switch (CC) {
14739 default: break;
14740 case ISD::SETULT:
14741 case ISD::SETULE:
14742 return hasUnsigned ? X86ISD::UMIN : 0;
14743 case ISD::SETUGT:
14744 case ISD::SETUGE:
14745 return hasUnsigned ? X86ISD::UMAX : 0;
14746 case ISD::SETLT:
14747 case ISD::SETLE:
14748 return hasSigned ? X86ISD::SMIN : 0;
14749 case ISD::SETGT:
14750 case ISD::SETGE:
14751 return hasSigned ? X86ISD::SMAX : 0;
14752 }
14753 // Check for x CC y ? y : x -- a min/max with reversed arms.
14754 } else if (DAG.isEqualTo(LHS, Cond.getOperand(1)) &&
14755 DAG.isEqualTo(RHS, Cond.getOperand(0))) {
14756 switch (CC) {
14757 default: break;
14758 case ISD::SETULT:
14759 case ISD::SETULE:
14760 return hasUnsigned ? X86ISD::UMAX : 0;
14761 case ISD::SETUGT:
14762 case ISD::SETUGE:
14763 return hasUnsigned ? X86ISD::UMIN : 0;
14764 case ISD::SETLT:
14765 case ISD::SETLE:
14766 return hasSigned ? X86ISD::SMAX : 0;
14767 case ISD::SETGT:
14768 case ISD::SETGE:
14769 return hasSigned ? X86ISD::SMIN : 0;
14770 }
14771 }
14772
14773 return 0;
14774}
14775
Duncan Sands6bcd2192011-09-17 16:49:39 +000014776/// PerformSELECTCombine - Do target-specific dag combines on SELECT and VSELECT
14777/// nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000014778static SDValue PerformSELECTCombine(SDNode *N, SelectionDAG &DAG,
Nadav Rotemcc616562012-01-15 19:27:55 +000014779 TargetLowering::DAGCombinerInfo &DCI,
Chris Lattner47b4ce82009-03-11 05:48:52 +000014780 const X86Subtarget *Subtarget) {
14781 DebugLoc DL = N->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +000014782 SDValue Cond = N->getOperand(0);
Chris Lattner47b4ce82009-03-11 05:48:52 +000014783 // Get the LHS/RHS of the select.
14784 SDValue LHS = N->getOperand(1);
14785 SDValue RHS = N->getOperand(2);
Bruno Cardoso Lopes149f29f2011-09-20 22:34:45 +000014786 EVT VT = LHS.getValueType();
Eric Christopherfd179292009-08-27 18:07:15 +000014787
Dan Gohman670e5392009-09-21 18:03:22 +000014788 // If we have SSE[12] support, try to form min/max nodes. SSE min/max
Dan Gohman8ce05da2010-02-22 04:03:39 +000014789 // instructions match the semantics of the common C idiom x<y?x:y but not
14790 // x<=y?x:y, because of how they handle negative zero (which can be
14791 // ignored in unsafe-math mode).
Benjamin Kramer2c2ccbf2011-09-22 03:27:22 +000014792 if (Cond.getOpcode() == ISD::SETCC && VT.isFloatingPoint() &&
14793 VT != MVT::f80 && DAG.getTargetLoweringInfo().isTypeLegal(VT) &&
Craig Topper1accb7e2012-01-10 06:54:16 +000014794 (Subtarget->hasSSE2() ||
14795 (Subtarget->hasSSE1() && VT.getScalarType() == MVT::f32))) {
Chris Lattner47b4ce82009-03-11 05:48:52 +000014796 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000014797
Chris Lattner47b4ce82009-03-11 05:48:52 +000014798 unsigned Opcode = 0;
Dan Gohman670e5392009-09-21 18:03:22 +000014799 // Check for x CC y ? x : y.
Dan Gohmane8326932010-02-24 06:52:40 +000014800 if (DAG.isEqualTo(LHS, Cond.getOperand(0)) &&
14801 DAG.isEqualTo(RHS, Cond.getOperand(1))) {
Chris Lattner47b4ce82009-03-11 05:48:52 +000014802 switch (CC) {
14803 default: break;
Dan Gohman670e5392009-09-21 18:03:22 +000014804 case ISD::SETULT:
Dan Gohmane8326932010-02-24 06:52:40 +000014805 // Converting this to a min would handle NaNs incorrectly, and swapping
14806 // the operands would cause it to handle comparisons between positive
14807 // and negative zero incorrectly.
Evan Cheng60108e92010-07-15 22:07:12 +000014808 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)) {
Nick Lewycky8a8d4792011-12-02 22:16:29 +000014809 if (!DAG.getTarget().Options.UnsafeFPMath &&
Dan Gohmane8326932010-02-24 06:52:40 +000014810 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
14811 break;
14812 std::swap(LHS, RHS);
14813 }
Dan Gohman670e5392009-09-21 18:03:22 +000014814 Opcode = X86ISD::FMIN;
14815 break;
14816 case ISD::SETOLE:
Dan Gohmane8326932010-02-24 06:52:40 +000014817 // Converting this to a min would handle comparisons between positive
14818 // and negative zero incorrectly.
Nick Lewycky8a8d4792011-12-02 22:16:29 +000014819 if (!DAG.getTarget().Options.UnsafeFPMath &&
Dan Gohmane8326932010-02-24 06:52:40 +000014820 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(RHS))
14821 break;
Dan Gohman670e5392009-09-21 18:03:22 +000014822 Opcode = X86ISD::FMIN;
14823 break;
Chris Lattner47b4ce82009-03-11 05:48:52 +000014824 case ISD::SETULE:
Dan Gohmane8326932010-02-24 06:52:40 +000014825 // Converting this to a min would handle both negative zeros and NaNs
14826 // incorrectly, but we can swap the operands to fix both.
14827 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000014828 case ISD::SETOLT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000014829 case ISD::SETLT:
Dan Gohman670e5392009-09-21 18:03:22 +000014830 case ISD::SETLE:
Chris Lattner47b4ce82009-03-11 05:48:52 +000014831 Opcode = X86ISD::FMIN;
14832 break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000014833
Dan Gohman670e5392009-09-21 18:03:22 +000014834 case ISD::SETOGE:
Dan Gohmane8326932010-02-24 06:52:40 +000014835 // Converting this to a max would handle comparisons between positive
14836 // and negative zero incorrectly.
Nick Lewycky8a8d4792011-12-02 22:16:29 +000014837 if (!DAG.getTarget().Options.UnsafeFPMath &&
Evan Chengdd5663c2011-08-04 18:38:15 +000014838 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000014839 break;
Dan Gohman670e5392009-09-21 18:03:22 +000014840 Opcode = X86ISD::FMAX;
14841 break;
Chris Lattner47b4ce82009-03-11 05:48:52 +000014842 case ISD::SETUGT:
Dan Gohmane8326932010-02-24 06:52:40 +000014843 // Converting this to a max would handle NaNs incorrectly, and swapping
14844 // the operands would cause it to handle comparisons between positive
14845 // and negative zero incorrectly.
Evan Cheng60108e92010-07-15 22:07:12 +000014846 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)) {
Nick Lewycky8a8d4792011-12-02 22:16:29 +000014847 if (!DAG.getTarget().Options.UnsafeFPMath &&
Dan Gohmane8326932010-02-24 06:52:40 +000014848 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
14849 break;
14850 std::swap(LHS, RHS);
14851 }
Dan Gohman670e5392009-09-21 18:03:22 +000014852 Opcode = X86ISD::FMAX;
14853 break;
14854 case ISD::SETUGE:
Dan Gohmane8326932010-02-24 06:52:40 +000014855 // Converting this to a max would handle both negative zeros and NaNs
14856 // incorrectly, but we can swap the operands to fix both.
14857 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000014858 case ISD::SETOGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000014859 case ISD::SETGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000014860 case ISD::SETGE:
14861 Opcode = X86ISD::FMAX;
14862 break;
Chris Lattner83e6c992006-10-04 06:57:07 +000014863 }
Dan Gohman670e5392009-09-21 18:03:22 +000014864 // Check for x CC y ? y : x -- a min/max with reversed arms.
Dan Gohmane8326932010-02-24 06:52:40 +000014865 } else if (DAG.isEqualTo(LHS, Cond.getOperand(1)) &&
14866 DAG.isEqualTo(RHS, Cond.getOperand(0))) {
Chris Lattner47b4ce82009-03-11 05:48:52 +000014867 switch (CC) {
14868 default: break;
Dan Gohman670e5392009-09-21 18:03:22 +000014869 case ISD::SETOGE:
Dan Gohmane8326932010-02-24 06:52:40 +000014870 // Converting this to a min would handle comparisons between positive
14871 // and negative zero incorrectly, and swapping the operands would
14872 // cause it to handle NaNs incorrectly.
Nick Lewycky8a8d4792011-12-02 22:16:29 +000014873 if (!DAG.getTarget().Options.UnsafeFPMath &&
Dan Gohmane8326932010-02-24 06:52:40 +000014874 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS))) {
Evan Cheng60108e92010-07-15 22:07:12 +000014875 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000014876 break;
14877 std::swap(LHS, RHS);
14878 }
Dan Gohman670e5392009-09-21 18:03:22 +000014879 Opcode = X86ISD::FMIN;
Dan Gohman8d44b282009-09-03 20:34:31 +000014880 break;
Dan Gohman670e5392009-09-21 18:03:22 +000014881 case ISD::SETUGT:
Dan Gohmane8326932010-02-24 06:52:40 +000014882 // Converting this to a min would handle NaNs incorrectly.
Nick Lewycky8a8d4792011-12-02 22:16:29 +000014883 if (!DAG.getTarget().Options.UnsafeFPMath &&
Dan Gohmane8326932010-02-24 06:52:40 +000014884 (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)))
14885 break;
Dan Gohman670e5392009-09-21 18:03:22 +000014886 Opcode = X86ISD::FMIN;
14887 break;
14888 case ISD::SETUGE:
Dan Gohmane8326932010-02-24 06:52:40 +000014889 // Converting this to a min would handle both negative zeros and NaNs
14890 // incorrectly, but we can swap the operands to fix both.
14891 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000014892 case ISD::SETOGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000014893 case ISD::SETGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000014894 case ISD::SETGE:
14895 Opcode = X86ISD::FMIN;
14896 break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000014897
Dan Gohman670e5392009-09-21 18:03:22 +000014898 case ISD::SETULT:
Dan Gohmane8326932010-02-24 06:52:40 +000014899 // Converting this to a max would handle NaNs incorrectly.
Evan Cheng60108e92010-07-15 22:07:12 +000014900 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000014901 break;
Dan Gohman670e5392009-09-21 18:03:22 +000014902 Opcode = X86ISD::FMAX;
Dan Gohman8d44b282009-09-03 20:34:31 +000014903 break;
Dan Gohman670e5392009-09-21 18:03:22 +000014904 case ISD::SETOLE:
Dan Gohmane8326932010-02-24 06:52:40 +000014905 // Converting this to a max would handle comparisons between positive
14906 // and negative zero incorrectly, and swapping the operands would
14907 // cause it to handle NaNs incorrectly.
Nick Lewycky8a8d4792011-12-02 22:16:29 +000014908 if (!DAG.getTarget().Options.UnsafeFPMath &&
Dan Gohmane8326932010-02-24 06:52:40 +000014909 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(RHS)) {
Evan Cheng60108e92010-07-15 22:07:12 +000014910 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000014911 break;
14912 std::swap(LHS, RHS);
14913 }
Dan Gohman670e5392009-09-21 18:03:22 +000014914 Opcode = X86ISD::FMAX;
14915 break;
14916 case ISD::SETULE:
Dan Gohmane8326932010-02-24 06:52:40 +000014917 // Converting this to a max would handle both negative zeros and NaNs
14918 // incorrectly, but we can swap the operands to fix both.
14919 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000014920 case ISD::SETOLT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000014921 case ISD::SETLT:
Dan Gohman670e5392009-09-21 18:03:22 +000014922 case ISD::SETLE:
Chris Lattner47b4ce82009-03-11 05:48:52 +000014923 Opcode = X86ISD::FMAX;
14924 break;
14925 }
Chris Lattner83e6c992006-10-04 06:57:07 +000014926 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000014927
Chris Lattner47b4ce82009-03-11 05:48:52 +000014928 if (Opcode)
14929 return DAG.getNode(Opcode, DL, N->getValueType(0), LHS, RHS);
Chris Lattner83e6c992006-10-04 06:57:07 +000014930 }
Eric Christopherfd179292009-08-27 18:07:15 +000014931
Chris Lattnerd1980a52009-03-12 06:52:53 +000014932 // If this is a select between two integer constants, try to do some
14933 // optimizations.
Chris Lattnercee56e72009-03-13 05:53:31 +000014934 if (ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(LHS)) {
14935 if (ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(RHS))
Chris Lattnerd1980a52009-03-12 06:52:53 +000014936 // Don't do this for crazy integer types.
14937 if (DAG.getTargetLoweringInfo().isTypeLegal(LHS.getValueType())) {
14938 // If this is efficiently invertible, canonicalize the LHSC/RHSC values
Chris Lattnercee56e72009-03-13 05:53:31 +000014939 // so that TrueC (the true value) is larger than FalseC.
Chris Lattnerd1980a52009-03-12 06:52:53 +000014940 bool NeedsCondInvert = false;
Eric Christopherfd179292009-08-27 18:07:15 +000014941
Chris Lattnercee56e72009-03-13 05:53:31 +000014942 if (TrueC->getAPIntValue().ult(FalseC->getAPIntValue()) &&
Chris Lattnerd1980a52009-03-12 06:52:53 +000014943 // Efficiently invertible.
14944 (Cond.getOpcode() == ISD::SETCC || // setcc -> invertible.
14945 (Cond.getOpcode() == ISD::XOR && // xor(X, C) -> invertible.
14946 isa<ConstantSDNode>(Cond.getOperand(1))))) {
14947 NeedsCondInvert = true;
Chris Lattnercee56e72009-03-13 05:53:31 +000014948 std::swap(TrueC, FalseC);
Chris Lattnerd1980a52009-03-12 06:52:53 +000014949 }
Eric Christopherfd179292009-08-27 18:07:15 +000014950
Chris Lattnerd1980a52009-03-12 06:52:53 +000014951 // Optimize C ? 8 : 0 -> zext(C) << 3. Likewise for any pow2/0.
Chris Lattnercee56e72009-03-13 05:53:31 +000014952 if (FalseC->getAPIntValue() == 0 &&
14953 TrueC->getAPIntValue().isPowerOf2()) {
Chris Lattnerd1980a52009-03-12 06:52:53 +000014954 if (NeedsCondInvert) // Invert the condition if needed.
14955 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
14956 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000014957
Chris Lattnerd1980a52009-03-12 06:52:53 +000014958 // Zero extend the condition if needed.
14959 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, LHS.getValueType(), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000014960
Chris Lattnercee56e72009-03-13 05:53:31 +000014961 unsigned ShAmt = TrueC->getAPIntValue().logBase2();
Chris Lattnerd1980a52009-03-12 06:52:53 +000014962 return DAG.getNode(ISD::SHL, DL, LHS.getValueType(), Cond,
Owen Anderson825b72b2009-08-11 20:47:22 +000014963 DAG.getConstant(ShAmt, MVT::i8));
Chris Lattnerd1980a52009-03-12 06:52:53 +000014964 }
Eric Christopherfd179292009-08-27 18:07:15 +000014965
Chris Lattner97a29a52009-03-13 05:22:11 +000014966 // Optimize Cond ? cst+1 : cst -> zext(setcc(C)+cst.
Chris Lattnercee56e72009-03-13 05:53:31 +000014967 if (FalseC->getAPIntValue()+1 == TrueC->getAPIntValue()) {
Chris Lattner97a29a52009-03-13 05:22:11 +000014968 if (NeedsCondInvert) // Invert the condition if needed.
14969 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
14970 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000014971
Chris Lattner97a29a52009-03-13 05:22:11 +000014972 // Zero extend the condition if needed.
Chris Lattnercee56e72009-03-13 05:53:31 +000014973 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL,
14974 FalseC->getValueType(0), Cond);
Chris Lattner97a29a52009-03-13 05:22:11 +000014975 return DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
Chris Lattnercee56e72009-03-13 05:53:31 +000014976 SDValue(FalseC, 0));
Chris Lattner97a29a52009-03-13 05:22:11 +000014977 }
Eric Christopherfd179292009-08-27 18:07:15 +000014978
Chris Lattnercee56e72009-03-13 05:53:31 +000014979 // Optimize cases that will turn into an LEA instruction. This requires
14980 // an i32 or i64 and an efficient multiplier (1, 2, 3, 4, 5, 8, 9).
Owen Anderson825b72b2009-08-11 20:47:22 +000014981 if (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i64) {
Chris Lattnercee56e72009-03-13 05:53:31 +000014982 uint64_t Diff = TrueC->getZExtValue()-FalseC->getZExtValue();
Owen Anderson825b72b2009-08-11 20:47:22 +000014983 if (N->getValueType(0) == MVT::i32) Diff = (unsigned)Diff;
Eric Christopherfd179292009-08-27 18:07:15 +000014984
Chris Lattnercee56e72009-03-13 05:53:31 +000014985 bool isFastMultiplier = false;
14986 if (Diff < 10) {
14987 switch ((unsigned char)Diff) {
14988 default: break;
14989 case 1: // result = add base, cond
14990 case 2: // result = lea base( , cond*2)
14991 case 3: // result = lea base(cond, cond*2)
14992 case 4: // result = lea base( , cond*4)
14993 case 5: // result = lea base(cond, cond*4)
14994 case 8: // result = lea base( , cond*8)
14995 case 9: // result = lea base(cond, cond*8)
14996 isFastMultiplier = true;
14997 break;
14998 }
14999 }
Eric Christopherfd179292009-08-27 18:07:15 +000015000
Chris Lattnercee56e72009-03-13 05:53:31 +000015001 if (isFastMultiplier) {
15002 APInt Diff = TrueC->getAPIntValue()-FalseC->getAPIntValue();
15003 if (NeedsCondInvert) // Invert the condition if needed.
15004 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
15005 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000015006
Chris Lattnercee56e72009-03-13 05:53:31 +000015007 // Zero extend the condition if needed.
15008 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, FalseC->getValueType(0),
15009 Cond);
15010 // Scale the condition by the difference.
15011 if (Diff != 1)
15012 Cond = DAG.getNode(ISD::MUL, DL, Cond.getValueType(), Cond,
15013 DAG.getConstant(Diff, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000015014
Chris Lattnercee56e72009-03-13 05:53:31 +000015015 // Add the base if non-zero.
15016 if (FalseC->getAPIntValue() != 0)
15017 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
15018 SDValue(FalseC, 0));
15019 return Cond;
15020 }
Eric Christopherfd179292009-08-27 18:07:15 +000015021 }
Chris Lattnerd1980a52009-03-12 06:52:53 +000015022 }
15023 }
Eric Christopherfd179292009-08-27 18:07:15 +000015024
Evan Cheng56f582d2012-01-04 01:41:39 +000015025 // Canonicalize max and min:
15026 // (x > y) ? x : y -> (x >= y) ? x : y
15027 // (x < y) ? x : y -> (x <= y) ? x : y
15028 // This allows use of COND_S / COND_NS (see TranslateX86CC) which eliminates
15029 // the need for an extra compare
15030 // against zero. e.g.
15031 // (x - y) > 0 : (x - y) ? 0 -> (x - y) >= 0 : (x - y) ? 0
15032 // subl %esi, %edi
15033 // testl %edi, %edi
15034 // movl $0, %eax
15035 // cmovgl %edi, %eax
15036 // =>
15037 // xorl %eax, %eax
15038 // subl %esi, $edi
15039 // cmovsl %eax, %edi
15040 if (N->getOpcode() == ISD::SELECT && Cond.getOpcode() == ISD::SETCC &&
15041 DAG.isEqualTo(LHS, Cond.getOperand(0)) &&
15042 DAG.isEqualTo(RHS, Cond.getOperand(1))) {
15043 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
15044 switch (CC) {
15045 default: break;
15046 case ISD::SETLT:
15047 case ISD::SETGT: {
15048 ISD::CondCode NewCC = (CC == ISD::SETLT) ? ISD::SETLE : ISD::SETGE;
15049 Cond = DAG.getSetCC(Cond.getDebugLoc(), Cond.getValueType(),
15050 Cond.getOperand(0), Cond.getOperand(1), NewCC);
15051 return DAG.getNode(ISD::SELECT, DL, VT, Cond, LHS, RHS);
15052 }
15053 }
15054 }
15055
Benjamin Kramer388fc6a2012-12-15 16:47:44 +000015056 // Match VSELECTs into subs with unsigned saturation.
15057 if (!DCI.isBeforeLegalize() &&
15058 N->getOpcode() == ISD::VSELECT && Cond.getOpcode() == ISD::SETCC &&
15059 // psubus is available in SSE2 and AVX2 for i8 and i16 vectors.
15060 ((Subtarget->hasSSE2() && (VT == MVT::v16i8 || VT == MVT::v8i16)) ||
15061 (Subtarget->hasAVX2() && (VT == MVT::v32i8 || VT == MVT::v16i16)))) {
15062 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
15063
15064 // Check if one of the arms of the VSELECT is a zero vector. If it's on the
15065 // left side invert the predicate to simplify logic below.
15066 SDValue Other;
15067 if (ISD::isBuildVectorAllZeros(LHS.getNode())) {
15068 Other = RHS;
15069 CC = ISD::getSetCCInverse(CC, true);
15070 } else if (ISD::isBuildVectorAllZeros(RHS.getNode())) {
15071 Other = LHS;
15072 }
15073
15074 if (Other.getNode() && Other->getNumOperands() == 2 &&
15075 DAG.isEqualTo(Other->getOperand(0), Cond.getOperand(0))) {
15076 SDValue OpLHS = Other->getOperand(0), OpRHS = Other->getOperand(1);
15077 SDValue CondRHS = Cond->getOperand(1);
15078
15079 // Look for a general sub with unsigned saturation first.
15080 // x >= y ? x-y : 0 --> subus x, y
15081 // x > y ? x-y : 0 --> subus x, y
15082 if ((CC == ISD::SETUGE || CC == ISD::SETUGT) &&
15083 Other->getOpcode() == ISD::SUB && DAG.isEqualTo(OpRHS, CondRHS))
15084 return DAG.getNode(X86ISD::SUBUS, DL, VT, OpLHS, OpRHS);
15085
15086 // If the RHS is a constant we have to reverse the const canonicalization.
15087 // x > C-1 ? x+-C : 0 --> subus x, C
15088 if (CC == ISD::SETUGT && Other->getOpcode() == ISD::ADD &&
15089 isSplatVector(CondRHS.getNode()) && isSplatVector(OpRHS.getNode())) {
15090 APInt A = cast<ConstantSDNode>(OpRHS.getOperand(0))->getAPIntValue();
15091 if (CondRHS.getConstantOperandVal(0) == -A-1) {
15092 SmallVector<SDValue, 32> V(VT.getVectorNumElements(),
15093 DAG.getConstant(-A, VT.getScalarType()));
15094 return DAG.getNode(X86ISD::SUBUS, DL, VT, OpLHS,
15095 DAG.getNode(ISD::BUILD_VECTOR, DL, VT,
15096 V.data(), V.size()));
15097 }
15098 }
15099
15100 // Another special case: If C was a sign bit, the sub has been
15101 // canonicalized into a xor.
15102 // FIXME: Would it be better to use ComputeMaskedBits to determine whether
15103 // it's safe to decanonicalize the xor?
15104 // x s< 0 ? x^C : 0 --> subus x, C
15105 if (CC == ISD::SETLT && Other->getOpcode() == ISD::XOR &&
15106 ISD::isBuildVectorAllZeros(CondRHS.getNode()) &&
15107 isSplatVector(OpRHS.getNode())) {
15108 APInt A = cast<ConstantSDNode>(OpRHS.getOperand(0))->getAPIntValue();
15109 if (A.isSignBit())
15110 return DAG.getNode(X86ISD::SUBUS, DL, VT, OpLHS, OpRHS);
15111 }
15112 }
15113 }
15114
Benjamin Kramer2556c6b2012-12-21 17:46:58 +000015115 // Try to match a min/max vector operation.
15116 if (!DCI.isBeforeLegalize() &&
15117 N->getOpcode() == ISD::VSELECT && Cond.getOpcode() == ISD::SETCC)
15118 if (unsigned Op = matchIntegerMINMAX(Cond, VT, LHS, RHS, DAG, Subtarget))
15119 return DAG.getNode(Op, DL, N->getValueType(0), LHS, RHS);
15120
Nadav Rotemcc616562012-01-15 19:27:55 +000015121 // If we know that this node is legal then we know that it is going to be
15122 // matched by one of the SSE/AVX BLEND instructions. These instructions only
15123 // depend on the highest bit in each word. Try to use SimplifyDemandedBits
15124 // to simplify previous instructions.
15125 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
15126 if (N->getOpcode() == ISD::VSELECT && DCI.isBeforeLegalizeOps() &&
Nadav Rotembdcae382012-06-07 20:53:48 +000015127 !DCI.isBeforeLegalize() && TLI.isOperationLegal(ISD::VSELECT, VT)) {
Nadav Rotemcc616562012-01-15 19:27:55 +000015128 unsigned BitWidth = Cond.getValueType().getScalarType().getSizeInBits();
Nadav Rotembdcae382012-06-07 20:53:48 +000015129
15130 // Don't optimize vector selects that map to mask-registers.
15131 if (BitWidth == 1)
15132 return SDValue();
15133
Nadav Rotemcc616562012-01-15 19:27:55 +000015134 assert(BitWidth >= 8 && BitWidth <= 64 && "Invalid mask size");
15135 APInt DemandedMask = APInt::getHighBitsSet(BitWidth, 1);
15136
15137 APInt KnownZero, KnownOne;
15138 TargetLowering::TargetLoweringOpt TLO(DAG, DCI.isBeforeLegalize(),
15139 DCI.isBeforeLegalizeOps());
15140 if (TLO.ShrinkDemandedConstant(Cond, DemandedMask) ||
15141 TLI.SimplifyDemandedBits(Cond, DemandedMask, KnownZero, KnownOne, TLO))
15142 DCI.CommitTargetLoweringOpt(TLO);
15143 }
15144
Dan Gohman475871a2008-07-27 21:46:04 +000015145 return SDValue();
Chris Lattner83e6c992006-10-04 06:57:07 +000015146}
15147
Michael Liao2a33cec2012-08-10 19:58:13 +000015148// Check whether a boolean test is testing a boolean value generated by
15149// X86ISD::SETCC. If so, return the operand of that SETCC and proper condition
15150// code.
15151//
15152// Simplify the following patterns:
15153// (Op (CMP (SETCC Cond EFLAGS) 1) EQ) or
15154// (Op (CMP (SETCC Cond EFLAGS) 0) NEQ)
15155// to (Op EFLAGS Cond)
15156//
15157// (Op (CMP (SETCC Cond EFLAGS) 0) EQ) or
15158// (Op (CMP (SETCC Cond EFLAGS) 1) NEQ)
15159// to (Op EFLAGS !Cond)
15160//
15161// where Op could be BRCOND or CMOV.
15162//
Michael Liaodbf8b5b2012-08-28 03:34:40 +000015163static SDValue checkBoolTestSetCCCombine(SDValue Cmp, X86::CondCode &CC) {
Michael Liao2a33cec2012-08-10 19:58:13 +000015164 // Quit if not CMP and SUB with its value result used.
15165 if (Cmp.getOpcode() != X86ISD::CMP &&
15166 (Cmp.getOpcode() != X86ISD::SUB || Cmp.getNode()->hasAnyUseOfValue(0)))
15167 return SDValue();
15168
15169 // Quit if not used as a boolean value.
15170 if (CC != X86::COND_E && CC != X86::COND_NE)
15171 return SDValue();
15172
15173 // Check CMP operands. One of them should be 0 or 1 and the other should be
15174 // an SetCC or extended from it.
15175 SDValue Op1 = Cmp.getOperand(0);
15176 SDValue Op2 = Cmp.getOperand(1);
15177
15178 SDValue SetCC;
15179 const ConstantSDNode* C = 0;
15180 bool needOppositeCond = (CC == X86::COND_E);
15181
15182 if ((C = dyn_cast<ConstantSDNode>(Op1)))
15183 SetCC = Op2;
15184 else if ((C = dyn_cast<ConstantSDNode>(Op2)))
15185 SetCC = Op1;
15186 else // Quit if all operands are not constants.
15187 return SDValue();
15188
15189 if (C->getZExtValue() == 1)
15190 needOppositeCond = !needOppositeCond;
15191 else if (C->getZExtValue() != 0)
15192 // Quit if the constant is neither 0 or 1.
15193 return SDValue();
15194
15195 // Skip 'zext' node.
15196 if (SetCC.getOpcode() == ISD::ZERO_EXTEND)
15197 SetCC = SetCC.getOperand(0);
15198
Michael Liao7fdc66b2012-09-10 16:36:16 +000015199 switch (SetCC.getOpcode()) {
15200 case X86ISD::SETCC:
15201 // Set the condition code or opposite one if necessary.
15202 CC = X86::CondCode(SetCC.getConstantOperandVal(0));
15203 if (needOppositeCond)
15204 CC = X86::GetOppositeBranchCondition(CC);
15205 return SetCC.getOperand(1);
15206 case X86ISD::CMOV: {
15207 // Check whether false/true value has canonical one, i.e. 0 or 1.
15208 ConstantSDNode *FVal = dyn_cast<ConstantSDNode>(SetCC.getOperand(0));
15209 ConstantSDNode *TVal = dyn_cast<ConstantSDNode>(SetCC.getOperand(1));
15210 // Quit if true value is not a constant.
15211 if (!TVal)
15212 return SDValue();
15213 // Quit if false value is not a constant.
15214 if (!FVal) {
15215 // A special case for rdrand, where 0 is set if false cond is found.
15216 SDValue Op = SetCC.getOperand(0);
15217 if (Op.getOpcode() != X86ISD::RDRAND)
15218 return SDValue();
15219 }
15220 // Quit if false value is not the constant 0 or 1.
15221 bool FValIsFalse = true;
15222 if (FVal && FVal->getZExtValue() != 0) {
15223 if (FVal->getZExtValue() != 1)
15224 return SDValue();
15225 // If FVal is 1, opposite cond is needed.
15226 needOppositeCond = !needOppositeCond;
15227 FValIsFalse = false;
15228 }
15229 // Quit if TVal is not the constant opposite of FVal.
15230 if (FValIsFalse && TVal->getZExtValue() != 1)
15231 return SDValue();
15232 if (!FValIsFalse && TVal->getZExtValue() != 0)
15233 return SDValue();
15234 CC = X86::CondCode(SetCC.getConstantOperandVal(2));
15235 if (needOppositeCond)
15236 CC = X86::GetOppositeBranchCondition(CC);
15237 return SetCC.getOperand(3);
15238 }
15239 }
Michael Liao2a33cec2012-08-10 19:58:13 +000015240
Michael Liao7fdc66b2012-09-10 16:36:16 +000015241 return SDValue();
Michael Liao2a33cec2012-08-10 19:58:13 +000015242}
15243
Chris Lattnerd1980a52009-03-12 06:52:53 +000015244/// Optimize X86ISD::CMOV [LHS, RHS, CONDCODE (e.g. X86::COND_NE), CONDVAL]
15245static SDValue PerformCMOVCombine(SDNode *N, SelectionDAG &DAG,
Michael Liaodbf8b5b2012-08-28 03:34:40 +000015246 TargetLowering::DAGCombinerInfo &DCI,
15247 const X86Subtarget *Subtarget) {
Chris Lattnerd1980a52009-03-12 06:52:53 +000015248 DebugLoc DL = N->getDebugLoc();
Eric Christopherfd179292009-08-27 18:07:15 +000015249
Chris Lattnerd1980a52009-03-12 06:52:53 +000015250 // If the flag operand isn't dead, don't touch this CMOV.
15251 if (N->getNumValues() == 2 && !SDValue(N, 1).use_empty())
15252 return SDValue();
Eric Christopherfd179292009-08-27 18:07:15 +000015253
Evan Chengb5a55d92011-05-24 01:48:22 +000015254 SDValue FalseOp = N->getOperand(0);
15255 SDValue TrueOp = N->getOperand(1);
15256 X86::CondCode CC = (X86::CondCode)N->getConstantOperandVal(2);
15257 SDValue Cond = N->getOperand(3);
Michael Liao2a33cec2012-08-10 19:58:13 +000015258
Evan Chengb5a55d92011-05-24 01:48:22 +000015259 if (CC == X86::COND_E || CC == X86::COND_NE) {
15260 switch (Cond.getOpcode()) {
15261 default: break;
15262 case X86ISD::BSR:
15263 case X86ISD::BSF:
15264 // If operand of BSR / BSF are proven never zero, then ZF cannot be set.
15265 if (DAG.isKnownNeverZero(Cond.getOperand(0)))
15266 return (CC == X86::COND_E) ? FalseOp : TrueOp;
15267 }
15268 }
15269
Michael Liao2a33cec2012-08-10 19:58:13 +000015270 SDValue Flags;
15271
Michael Liaodbf8b5b2012-08-28 03:34:40 +000015272 Flags = checkBoolTestSetCCCombine(Cond, CC);
Michael Liao9eac20a2012-08-11 23:47:06 +000015273 if (Flags.getNode() &&
15274 // Extra check as FCMOV only supports a subset of X86 cond.
Michael Liao7859f432012-09-06 07:11:22 +000015275 (FalseOp.getValueType() != MVT::f80 || hasFPCMov(CC))) {
Michael Liaodbf8b5b2012-08-28 03:34:40 +000015276 SDValue Ops[] = { FalseOp, TrueOp,
15277 DAG.getConstant(CC, MVT::i8), Flags };
15278 return DAG.getNode(X86ISD::CMOV, DL, N->getVTList(),
15279 Ops, array_lengthof(Ops));
15280 }
15281
Chris Lattnerd1980a52009-03-12 06:52:53 +000015282 // If this is a select between two integer constants, try to do some
15283 // optimizations. Note that the operands are ordered the opposite of SELECT
15284 // operands.
Evan Chengb5a55d92011-05-24 01:48:22 +000015285 if (ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(TrueOp)) {
15286 if (ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(FalseOp)) {
Chris Lattnerd1980a52009-03-12 06:52:53 +000015287 // Canonicalize the TrueC/FalseC values so that TrueC (the true value) is
15288 // larger than FalseC (the false value).
Chris Lattnerd1980a52009-03-12 06:52:53 +000015289 if (TrueC->getAPIntValue().ult(FalseC->getAPIntValue())) {
15290 CC = X86::GetOppositeBranchCondition(CC);
15291 std::swap(TrueC, FalseC);
NAKAMURA Takumie2687452012-10-16 06:28:34 +000015292 std::swap(TrueOp, FalseOp);
Chris Lattnerd1980a52009-03-12 06:52:53 +000015293 }
Eric Christopherfd179292009-08-27 18:07:15 +000015294
Chris Lattnerd1980a52009-03-12 06:52:53 +000015295 // Optimize C ? 8 : 0 -> zext(setcc(C)) << 3. Likewise for any pow2/0.
Chris Lattnercee56e72009-03-13 05:53:31 +000015296 // This is efficient for any integer data type (including i8/i16) and
15297 // shift amount.
Chris Lattnerd1980a52009-03-12 06:52:53 +000015298 if (FalseC->getAPIntValue() == 0 && TrueC->getAPIntValue().isPowerOf2()) {
Owen Anderson825b72b2009-08-11 20:47:22 +000015299 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
15300 DAG.getConstant(CC, MVT::i8), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000015301
Chris Lattnerd1980a52009-03-12 06:52:53 +000015302 // Zero extend the condition if needed.
15303 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, TrueC->getValueType(0), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000015304
Chris Lattnerd1980a52009-03-12 06:52:53 +000015305 unsigned ShAmt = TrueC->getAPIntValue().logBase2();
15306 Cond = DAG.getNode(ISD::SHL, DL, Cond.getValueType(), Cond,
Owen Anderson825b72b2009-08-11 20:47:22 +000015307 DAG.getConstant(ShAmt, MVT::i8));
Chris Lattnerd1980a52009-03-12 06:52:53 +000015308 if (N->getNumValues() == 2) // Dead flag value?
15309 return DCI.CombineTo(N, Cond, SDValue());
15310 return Cond;
15311 }
Eric Christopherfd179292009-08-27 18:07:15 +000015312
Chris Lattnercee56e72009-03-13 05:53:31 +000015313 // Optimize Cond ? cst+1 : cst -> zext(setcc(C)+cst. This is efficient
15314 // for any integer data type, including i8/i16.
Chris Lattner97a29a52009-03-13 05:22:11 +000015315 if (FalseC->getAPIntValue()+1 == TrueC->getAPIntValue()) {
Owen Anderson825b72b2009-08-11 20:47:22 +000015316 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
15317 DAG.getConstant(CC, MVT::i8), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000015318
Chris Lattner97a29a52009-03-13 05:22:11 +000015319 // Zero extend the condition if needed.
Chris Lattnercee56e72009-03-13 05:53:31 +000015320 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL,
15321 FalseC->getValueType(0), Cond);
Chris Lattner97a29a52009-03-13 05:22:11 +000015322 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
15323 SDValue(FalseC, 0));
Eric Christopherfd179292009-08-27 18:07:15 +000015324
Chris Lattner97a29a52009-03-13 05:22:11 +000015325 if (N->getNumValues() == 2) // Dead flag value?
15326 return DCI.CombineTo(N, Cond, SDValue());
15327 return Cond;
15328 }
Eric Christopherfd179292009-08-27 18:07:15 +000015329
Chris Lattnercee56e72009-03-13 05:53:31 +000015330 // Optimize cases that will turn into an LEA instruction. This requires
15331 // an i32 or i64 and an efficient multiplier (1, 2, 3, 4, 5, 8, 9).
Owen Anderson825b72b2009-08-11 20:47:22 +000015332 if (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i64) {
Chris Lattnercee56e72009-03-13 05:53:31 +000015333 uint64_t Diff = TrueC->getZExtValue()-FalseC->getZExtValue();
Owen Anderson825b72b2009-08-11 20:47:22 +000015334 if (N->getValueType(0) == MVT::i32) Diff = (unsigned)Diff;
Eric Christopherfd179292009-08-27 18:07:15 +000015335
Chris Lattnercee56e72009-03-13 05:53:31 +000015336 bool isFastMultiplier = false;
15337 if (Diff < 10) {
15338 switch ((unsigned char)Diff) {
15339 default: break;
15340 case 1: // result = add base, cond
15341 case 2: // result = lea base( , cond*2)
15342 case 3: // result = lea base(cond, cond*2)
15343 case 4: // result = lea base( , cond*4)
15344 case 5: // result = lea base(cond, cond*4)
15345 case 8: // result = lea base( , cond*8)
15346 case 9: // result = lea base(cond, cond*8)
15347 isFastMultiplier = true;
15348 break;
15349 }
15350 }
Eric Christopherfd179292009-08-27 18:07:15 +000015351
Chris Lattnercee56e72009-03-13 05:53:31 +000015352 if (isFastMultiplier) {
15353 APInt Diff = TrueC->getAPIntValue()-FalseC->getAPIntValue();
Owen Anderson825b72b2009-08-11 20:47:22 +000015354 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
15355 DAG.getConstant(CC, MVT::i8), Cond);
Chris Lattnercee56e72009-03-13 05:53:31 +000015356 // Zero extend the condition if needed.
15357 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, FalseC->getValueType(0),
15358 Cond);
15359 // Scale the condition by the difference.
15360 if (Diff != 1)
15361 Cond = DAG.getNode(ISD::MUL, DL, Cond.getValueType(), Cond,
15362 DAG.getConstant(Diff, Cond.getValueType()));
15363
15364 // Add the base if non-zero.
15365 if (FalseC->getAPIntValue() != 0)
15366 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
15367 SDValue(FalseC, 0));
15368 if (N->getNumValues() == 2) // Dead flag value?
15369 return DCI.CombineTo(N, Cond, SDValue());
15370 return Cond;
15371 }
Eric Christopherfd179292009-08-27 18:07:15 +000015372 }
Chris Lattnerd1980a52009-03-12 06:52:53 +000015373 }
15374 }
NAKAMURA Takumie2687452012-10-16 06:28:34 +000015375
15376 // Handle these cases:
15377 // (select (x != c), e, c) -> select (x != c), e, x),
15378 // (select (x == c), c, e) -> select (x == c), x, e)
15379 // where the c is an integer constant, and the "select" is the combination
15380 // of CMOV and CMP.
15381 //
15382 // The rationale for this change is that the conditional-move from a constant
15383 // needs two instructions, however, conditional-move from a register needs
15384 // only one instruction.
15385 //
15386 // CAVEAT: By replacing a constant with a symbolic value, it may obscure
15387 // some instruction-combining opportunities. This opt needs to be
15388 // postponed as late as possible.
15389 //
15390 if (!DCI.isBeforeLegalize() && !DCI.isBeforeLegalizeOps()) {
15391 // the DCI.xxxx conditions are provided to postpone the optimization as
15392 // late as possible.
15393
15394 ConstantSDNode *CmpAgainst = 0;
15395 if ((Cond.getOpcode() == X86ISD::CMP || Cond.getOpcode() == X86ISD::SUB) &&
15396 (CmpAgainst = dyn_cast<ConstantSDNode>(Cond.getOperand(1))) &&
15397 dyn_cast<ConstantSDNode>(Cond.getOperand(0)) == 0) {
15398
15399 if (CC == X86::COND_NE &&
15400 CmpAgainst == dyn_cast<ConstantSDNode>(FalseOp)) {
15401 CC = X86::GetOppositeBranchCondition(CC);
15402 std::swap(TrueOp, FalseOp);
15403 }
15404
15405 if (CC == X86::COND_E &&
15406 CmpAgainst == dyn_cast<ConstantSDNode>(TrueOp)) {
15407 SDValue Ops[] = { FalseOp, Cond.getOperand(0),
15408 DAG.getConstant(CC, MVT::i8), Cond };
15409 return DAG.getNode(X86ISD::CMOV, DL, N->getVTList (), Ops,
15410 array_lengthof(Ops));
15411 }
15412 }
15413 }
15414
Chris Lattnerd1980a52009-03-12 06:52:53 +000015415 return SDValue();
15416}
15417
Evan Cheng0b0cd912009-03-28 05:57:29 +000015418/// PerformMulCombine - Optimize a single multiply with constant into two
15419/// in order to implement it with two cheaper instructions, e.g.
15420/// LEA + SHL, LEA + LEA.
15421static SDValue PerformMulCombine(SDNode *N, SelectionDAG &DAG,
15422 TargetLowering::DAGCombinerInfo &DCI) {
Evan Cheng0b0cd912009-03-28 05:57:29 +000015423 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
15424 return SDValue();
15425
Owen Andersone50ed302009-08-10 22:56:29 +000015426 EVT VT = N->getValueType(0);
Owen Anderson825b72b2009-08-11 20:47:22 +000015427 if (VT != MVT::i64)
Evan Cheng0b0cd912009-03-28 05:57:29 +000015428 return SDValue();
15429
15430 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1));
15431 if (!C)
15432 return SDValue();
15433 uint64_t MulAmt = C->getZExtValue();
15434 if (isPowerOf2_64(MulAmt) || MulAmt == 3 || MulAmt == 5 || MulAmt == 9)
15435 return SDValue();
15436
15437 uint64_t MulAmt1 = 0;
15438 uint64_t MulAmt2 = 0;
15439 if ((MulAmt % 9) == 0) {
15440 MulAmt1 = 9;
15441 MulAmt2 = MulAmt / 9;
15442 } else if ((MulAmt % 5) == 0) {
15443 MulAmt1 = 5;
15444 MulAmt2 = MulAmt / 5;
15445 } else if ((MulAmt % 3) == 0) {
15446 MulAmt1 = 3;
15447 MulAmt2 = MulAmt / 3;
15448 }
15449 if (MulAmt2 &&
15450 (isPowerOf2_64(MulAmt2) || MulAmt2 == 3 || MulAmt2 == 5 || MulAmt2 == 9)){
15451 DebugLoc DL = N->getDebugLoc();
15452
15453 if (isPowerOf2_64(MulAmt2) &&
15454 !(N->hasOneUse() && N->use_begin()->getOpcode() == ISD::ADD))
15455 // If second multiplifer is pow2, issue it first. We want the multiply by
15456 // 3, 5, or 9 to be folded into the addressing mode unless the lone use
15457 // is an add.
15458 std::swap(MulAmt1, MulAmt2);
15459
15460 SDValue NewMul;
Eric Christopherfd179292009-08-27 18:07:15 +000015461 if (isPowerOf2_64(MulAmt1))
Evan Cheng0b0cd912009-03-28 05:57:29 +000015462 NewMul = DAG.getNode(ISD::SHL, DL, VT, N->getOperand(0),
Owen Anderson825b72b2009-08-11 20:47:22 +000015463 DAG.getConstant(Log2_64(MulAmt1), MVT::i8));
Evan Cheng0b0cd912009-03-28 05:57:29 +000015464 else
Evan Cheng73f24c92009-03-30 21:36:47 +000015465 NewMul = DAG.getNode(X86ISD::MUL_IMM, DL, VT, N->getOperand(0),
Evan Cheng0b0cd912009-03-28 05:57:29 +000015466 DAG.getConstant(MulAmt1, VT));
15467
Eric Christopherfd179292009-08-27 18:07:15 +000015468 if (isPowerOf2_64(MulAmt2))
Evan Cheng0b0cd912009-03-28 05:57:29 +000015469 NewMul = DAG.getNode(ISD::SHL, DL, VT, NewMul,
Owen Anderson825b72b2009-08-11 20:47:22 +000015470 DAG.getConstant(Log2_64(MulAmt2), MVT::i8));
Eric Christopherfd179292009-08-27 18:07:15 +000015471 else
Evan Cheng73f24c92009-03-30 21:36:47 +000015472 NewMul = DAG.getNode(X86ISD::MUL_IMM, DL, VT, NewMul,
Evan Cheng0b0cd912009-03-28 05:57:29 +000015473 DAG.getConstant(MulAmt2, VT));
15474
15475 // Do not add new nodes to DAG combiner worklist.
15476 DCI.CombineTo(N, NewMul, false);
15477 }
15478 return SDValue();
15479}
15480
Evan Chengad9c0a32009-12-15 00:53:42 +000015481static SDValue PerformSHLCombine(SDNode *N, SelectionDAG &DAG) {
15482 SDValue N0 = N->getOperand(0);
15483 SDValue N1 = N->getOperand(1);
15484 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
15485 EVT VT = N0.getValueType();
15486
15487 // fold (shl (and (setcc_c), c1), c2) -> (and setcc_c, (c1 << c2))
15488 // since the result of setcc_c is all zero's or all ones.
Nadav Rotemfb0dfbb2011-10-30 13:24:22 +000015489 if (VT.isInteger() && !VT.isVector() &&
15490 N1C && N0.getOpcode() == ISD::AND &&
Evan Chengad9c0a32009-12-15 00:53:42 +000015491 N0.getOperand(1).getOpcode() == ISD::Constant) {
15492 SDValue N00 = N0.getOperand(0);
15493 if (N00.getOpcode() == X86ISD::SETCC_CARRY ||
15494 ((N00.getOpcode() == ISD::ANY_EXTEND ||
15495 N00.getOpcode() == ISD::ZERO_EXTEND) &&
15496 N00.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY)) {
15497 APInt Mask = cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
15498 APInt ShAmt = N1C->getAPIntValue();
15499 Mask = Mask.shl(ShAmt);
15500 if (Mask != 0)
15501 return DAG.getNode(ISD::AND, N->getDebugLoc(), VT,
15502 N00, DAG.getConstant(Mask, VT));
15503 }
15504 }
15505
Nadav Rotemfb0dfbb2011-10-30 13:24:22 +000015506 // Hardware support for vector shifts is sparse which makes us scalarize the
15507 // vector operations in many cases. Also, on sandybridge ADD is faster than
15508 // shl.
15509 // (shl V, 1) -> add V,V
15510 if (isSplatVector(N1.getNode())) {
15511 assert(N0.getValueType().isVector() && "Invalid vector shift type");
15512 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1->getOperand(0));
15513 // We shift all of the values by one. In many cases we do not have
15514 // hardware support for this operation. This is better expressed as an ADD
15515 // of two values.
15516 if (N1C && (1 == N1C->getZExtValue())) {
15517 return DAG.getNode(ISD::ADD, N->getDebugLoc(), VT, N0, N0);
15518 }
15519 }
15520
Evan Chengad9c0a32009-12-15 00:53:42 +000015521 return SDValue();
15522}
Evan Cheng0b0cd912009-03-28 05:57:29 +000015523
Nate Begeman740ab032009-01-26 00:52:55 +000015524/// PerformShiftCombine - Transforms vector shift nodes to use vector shifts
15525/// when possible.
15526static SDValue PerformShiftCombine(SDNode* N, SelectionDAG &DAG,
Mon P Wang845b1892012-02-01 22:15:20 +000015527 TargetLowering::DAGCombinerInfo &DCI,
Nate Begeman740ab032009-01-26 00:52:55 +000015528 const X86Subtarget *Subtarget) {
Evan Chengad9c0a32009-12-15 00:53:42 +000015529 EVT VT = N->getValueType(0);
Nadav Rotemfb0dfbb2011-10-30 13:24:22 +000015530 if (N->getOpcode() == ISD::SHL) {
15531 SDValue V = PerformSHLCombine(N, DAG);
15532 if (V.getNode()) return V;
15533 }
Evan Chengad9c0a32009-12-15 00:53:42 +000015534
Nate Begeman740ab032009-01-26 00:52:55 +000015535 // On X86 with SSE2 support, we can transform this to a vector shift if
15536 // all elements are shifted by the same amount. We can't do this in legalize
15537 // because the a constant vector is typically transformed to a constant pool
15538 // so we have no knowledge of the shift amount.
Craig Topper1accb7e2012-01-10 06:54:16 +000015539 if (!Subtarget->hasSSE2())
Nate Begemanc2fd67f2009-01-26 03:15:31 +000015540 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +000015541
Craig Topper7be5dfd2011-11-12 09:58:49 +000015542 if (VT != MVT::v2i64 && VT != MVT::v4i32 && VT != MVT::v8i16 &&
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000015543 (!Subtarget->hasInt256() ||
Craig Topper7be5dfd2011-11-12 09:58:49 +000015544 (VT != MVT::v4i64 && VT != MVT::v8i32 && VT != MVT::v16i16)))
Nate Begemanc2fd67f2009-01-26 03:15:31 +000015545 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +000015546
Mon P Wang3becd092009-01-28 08:12:05 +000015547 SDValue ShAmtOp = N->getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +000015548 EVT EltVT = VT.getVectorElementType();
Chris Lattner47b4ce82009-03-11 05:48:52 +000015549 DebugLoc DL = N->getDebugLoc();
Mon P Wangefa42202009-09-03 19:56:25 +000015550 SDValue BaseShAmt = SDValue();
Mon P Wang3becd092009-01-28 08:12:05 +000015551 if (ShAmtOp.getOpcode() == ISD::BUILD_VECTOR) {
15552 unsigned NumElts = VT.getVectorNumElements();
15553 unsigned i = 0;
15554 for (; i != NumElts; ++i) {
15555 SDValue Arg = ShAmtOp.getOperand(i);
15556 if (Arg.getOpcode() == ISD::UNDEF) continue;
15557 BaseShAmt = Arg;
15558 break;
15559 }
Craig Topper37c26772012-01-17 04:44:50 +000015560 // Handle the case where the build_vector is all undef
15561 // FIXME: Should DAG allow this?
15562 if (i == NumElts)
15563 return SDValue();
15564
Mon P Wang3becd092009-01-28 08:12:05 +000015565 for (; i != NumElts; ++i) {
15566 SDValue Arg = ShAmtOp.getOperand(i);
15567 if (Arg.getOpcode() == ISD::UNDEF) continue;
15568 if (Arg != BaseShAmt) {
15569 return SDValue();
15570 }
15571 }
15572 } else if (ShAmtOp.getOpcode() == ISD::VECTOR_SHUFFLE &&
Nate Begeman9008ca62009-04-27 18:41:29 +000015573 cast<ShuffleVectorSDNode>(ShAmtOp)->isSplat()) {
Mon P Wangefa42202009-09-03 19:56:25 +000015574 SDValue InVec = ShAmtOp.getOperand(0);
15575 if (InVec.getOpcode() == ISD::BUILD_VECTOR) {
15576 unsigned NumElts = InVec.getValueType().getVectorNumElements();
15577 unsigned i = 0;
15578 for (; i != NumElts; ++i) {
15579 SDValue Arg = InVec.getOperand(i);
15580 if (Arg.getOpcode() == ISD::UNDEF) continue;
15581 BaseShAmt = Arg;
15582 break;
15583 }
15584 } else if (InVec.getOpcode() == ISD::INSERT_VECTOR_ELT) {
15585 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(InVec.getOperand(2))) {
Evan Chengae3ecf92010-02-16 21:09:44 +000015586 unsigned SplatIdx= cast<ShuffleVectorSDNode>(ShAmtOp)->getSplatIndex();
Mon P Wangefa42202009-09-03 19:56:25 +000015587 if (C->getZExtValue() == SplatIdx)
15588 BaseShAmt = InVec.getOperand(1);
15589 }
15590 }
Mon P Wang845b1892012-02-01 22:15:20 +000015591 if (BaseShAmt.getNode() == 0) {
15592 // Don't create instructions with illegal types after legalize
15593 // types has run.
15594 if (!DAG.getTargetLoweringInfo().isTypeLegal(EltVT) &&
15595 !DCI.isBeforeLegalize())
15596 return SDValue();
15597
Mon P Wangefa42202009-09-03 19:56:25 +000015598 BaseShAmt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, EltVT, ShAmtOp,
15599 DAG.getIntPtrConstant(0));
Mon P Wang845b1892012-02-01 22:15:20 +000015600 }
Mon P Wang3becd092009-01-28 08:12:05 +000015601 } else
Nate Begemanc2fd67f2009-01-26 03:15:31 +000015602 return SDValue();
Nate Begeman740ab032009-01-26 00:52:55 +000015603
Mon P Wangefa42202009-09-03 19:56:25 +000015604 // The shift amount is an i32.
Owen Anderson825b72b2009-08-11 20:47:22 +000015605 if (EltVT.bitsGT(MVT::i32))
15606 BaseShAmt = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, BaseShAmt);
15607 else if (EltVT.bitsLT(MVT::i32))
Mon P Wangefa42202009-09-03 19:56:25 +000015608 BaseShAmt = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::i32, BaseShAmt);
Nate Begeman740ab032009-01-26 00:52:55 +000015609
Nate Begemanc2fd67f2009-01-26 03:15:31 +000015610 // The shift amount is identical so we can do a vector shift.
15611 SDValue ValOp = N->getOperand(0);
15612 switch (N->getOpcode()) {
15613 default:
Torok Edwinc23197a2009-07-14 16:55:14 +000015614 llvm_unreachable("Unknown shift opcode!");
Nate Begemanc2fd67f2009-01-26 03:15:31 +000015615 case ISD::SHL:
Craig Toppered2e13d2012-01-22 19:15:14 +000015616 switch (VT.getSimpleVT().SimpleTy) {
15617 default: return SDValue();
15618 case MVT::v2i64:
15619 case MVT::v4i32:
15620 case MVT::v8i16:
15621 case MVT::v4i64:
15622 case MVT::v8i32:
15623 case MVT::v16i16:
15624 return getTargetVShiftNode(X86ISD::VSHLI, DL, VT, ValOp, BaseShAmt, DAG);
15625 }
Nate Begemanc2fd67f2009-01-26 03:15:31 +000015626 case ISD::SRA:
Craig Toppered2e13d2012-01-22 19:15:14 +000015627 switch (VT.getSimpleVT().SimpleTy) {
15628 default: return SDValue();
15629 case MVT::v4i32:
15630 case MVT::v8i16:
15631 case MVT::v8i32:
15632 case MVT::v16i16:
15633 return getTargetVShiftNode(X86ISD::VSRAI, DL, VT, ValOp, BaseShAmt, DAG);
15634 }
Nate Begemanc2fd67f2009-01-26 03:15:31 +000015635 case ISD::SRL:
Craig Toppered2e13d2012-01-22 19:15:14 +000015636 switch (VT.getSimpleVT().SimpleTy) {
15637 default: return SDValue();
15638 case MVT::v2i64:
15639 case MVT::v4i32:
15640 case MVT::v8i16:
15641 case MVT::v4i64:
15642 case MVT::v8i32:
15643 case MVT::v16i16:
15644 return getTargetVShiftNode(X86ISD::VSRLI, DL, VT, ValOp, BaseShAmt, DAG);
15645 }
Nate Begeman740ab032009-01-26 00:52:55 +000015646 }
Nate Begeman740ab032009-01-26 00:52:55 +000015647}
15648
Stuart Hastings865f0932011-06-03 23:53:54 +000015649// CMPEQCombine - Recognize the distinctive (AND (setcc ...) (setcc ..))
15650// where both setccs reference the same FP CMP, and rewrite for CMPEQSS
15651// and friends. Likewise for OR -> CMPNEQSS.
15652static SDValue CMPEQCombine(SDNode *N, SelectionDAG &DAG,
15653 TargetLowering::DAGCombinerInfo &DCI,
15654 const X86Subtarget *Subtarget) {
15655 unsigned opcode;
15656
15657 // SSE1 supports CMP{eq|ne}SS, and SSE2 added CMP{eq|ne}SD, but
15658 // we're requiring SSE2 for both.
Craig Topper1accb7e2012-01-10 06:54:16 +000015659 if (Subtarget->hasSSE2() && isAndOrOfSetCCs(SDValue(N, 0U), opcode)) {
Stuart Hastings865f0932011-06-03 23:53:54 +000015660 SDValue N0 = N->getOperand(0);
15661 SDValue N1 = N->getOperand(1);
15662 SDValue CMP0 = N0->getOperand(1);
15663 SDValue CMP1 = N1->getOperand(1);
15664 DebugLoc DL = N->getDebugLoc();
15665
15666 // The SETCCs should both refer to the same CMP.
15667 if (CMP0.getOpcode() != X86ISD::CMP || CMP0 != CMP1)
15668 return SDValue();
15669
15670 SDValue CMP00 = CMP0->getOperand(0);
15671 SDValue CMP01 = CMP0->getOperand(1);
15672 EVT VT = CMP00.getValueType();
15673
15674 if (VT == MVT::f32 || VT == MVT::f64) {
15675 bool ExpectingFlags = false;
15676 // Check for any users that want flags:
15677 for (SDNode::use_iterator UI = N->use_begin(),
15678 UE = N->use_end();
15679 !ExpectingFlags && UI != UE; ++UI)
15680 switch (UI->getOpcode()) {
15681 default:
15682 case ISD::BR_CC:
15683 case ISD::BRCOND:
15684 case ISD::SELECT:
15685 ExpectingFlags = true;
15686 break;
15687 case ISD::CopyToReg:
15688 case ISD::SIGN_EXTEND:
15689 case ISD::ZERO_EXTEND:
15690 case ISD::ANY_EXTEND:
15691 break;
15692 }
15693
15694 if (!ExpectingFlags) {
15695 enum X86::CondCode cc0 = (enum X86::CondCode)N0.getConstantOperandVal(0);
15696 enum X86::CondCode cc1 = (enum X86::CondCode)N1.getConstantOperandVal(0);
15697
15698 if (cc1 == X86::COND_E || cc1 == X86::COND_NE) {
15699 X86::CondCode tmp = cc0;
15700 cc0 = cc1;
15701 cc1 = tmp;
15702 }
15703
15704 if ((cc0 == X86::COND_E && cc1 == X86::COND_NP) ||
15705 (cc0 == X86::COND_NE && cc1 == X86::COND_P)) {
15706 bool is64BitFP = (CMP00.getValueType() == MVT::f64);
15707 X86ISD::NodeType NTOperator = is64BitFP ?
15708 X86ISD::FSETCCsd : X86ISD::FSETCCss;
15709 // FIXME: need symbolic constants for these magic numbers.
15710 // See X86ATTInstPrinter.cpp:printSSECC().
15711 unsigned x86cc = (cc0 == X86::COND_E) ? 0 : 4;
15712 SDValue OnesOrZeroesF = DAG.getNode(NTOperator, DL, MVT::f32, CMP00, CMP01,
15713 DAG.getConstant(x86cc, MVT::i8));
15714 SDValue OnesOrZeroesI = DAG.getNode(ISD::BITCAST, DL, MVT::i32,
15715 OnesOrZeroesF);
15716 SDValue ANDed = DAG.getNode(ISD::AND, DL, MVT::i32, OnesOrZeroesI,
15717 DAG.getConstant(1, MVT::i32));
15718 SDValue OneBitOfTruth = DAG.getNode(ISD::TRUNCATE, DL, MVT::i8, ANDed);
15719 return OneBitOfTruth;
15720 }
15721 }
15722 }
15723 }
15724 return SDValue();
15725}
15726
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +000015727/// CanFoldXORWithAllOnes - Test whether the XOR operand is a AllOnes vector
15728/// so it can be folded inside ANDNP.
15729static bool CanFoldXORWithAllOnes(const SDNode *N) {
15730 EVT VT = N->getValueType(0);
15731
15732 // Match direct AllOnes for 128 and 256-bit vectors
15733 if (ISD::isBuildVectorAllOnes(N))
15734 return true;
15735
15736 // Look through a bit convert.
15737 if (N->getOpcode() == ISD::BITCAST)
15738 N = N->getOperand(0).getNode();
15739
15740 // Sometimes the operand may come from a insert_subvector building a 256-bit
15741 // allones vector
Craig Topper7a9a28b2012-08-12 02:23:29 +000015742 if (VT.is256BitVector() &&
Bill Wendling456a9252011-08-04 00:32:58 +000015743 N->getOpcode() == ISD::INSERT_SUBVECTOR) {
15744 SDValue V1 = N->getOperand(0);
15745 SDValue V2 = N->getOperand(1);
15746
15747 if (V1.getOpcode() == ISD::INSERT_SUBVECTOR &&
15748 V1.getOperand(0).getOpcode() == ISD::UNDEF &&
15749 ISD::isBuildVectorAllOnes(V1.getOperand(1).getNode()) &&
15750 ISD::isBuildVectorAllOnes(V2.getNode()))
15751 return true;
15752 }
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +000015753
15754 return false;
15755}
15756
Nate Begemanb65c1752010-12-17 22:55:37 +000015757static SDValue PerformAndCombine(SDNode *N, SelectionDAG &DAG,
15758 TargetLowering::DAGCombinerInfo &DCI,
15759 const X86Subtarget *Subtarget) {
15760 if (DCI.isBeforeLegalizeOps())
15761 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000015762
Stuart Hastings865f0932011-06-03 23:53:54 +000015763 SDValue R = CMPEQCombine(N, DAG, DCI, Subtarget);
15764 if (R.getNode())
15765 return R;
15766
Craig Topper54a11172011-10-14 07:06:56 +000015767 EVT VT = N->getValueType(0);
15768
Craig Topperb926afc2012-12-17 05:12:30 +000015769 // Create BLSI, and BLSR instructions
Craig Topperb4c94572011-10-21 06:55:01 +000015770 // BLSI is X & (-X)
15771 // BLSR is X & (X-1)
Craig Topper54a11172011-10-14 07:06:56 +000015772 if (Subtarget->hasBMI() && (VT == MVT::i32 || VT == MVT::i64)) {
15773 SDValue N0 = N->getOperand(0);
15774 SDValue N1 = N->getOperand(1);
15775 DebugLoc DL = N->getDebugLoc();
15776
Craig Topperb4c94572011-10-21 06:55:01 +000015777 // Check LHS for neg
15778 if (N0.getOpcode() == ISD::SUB && N0.getOperand(1) == N1 &&
15779 isZero(N0.getOperand(0)))
15780 return DAG.getNode(X86ISD::BLSI, DL, VT, N1);
15781
15782 // Check RHS for neg
15783 if (N1.getOpcode() == ISD::SUB && N1.getOperand(1) == N0 &&
15784 isZero(N1.getOperand(0)))
15785 return DAG.getNode(X86ISD::BLSI, DL, VT, N0);
15786
15787 // Check LHS for X-1
15788 if (N0.getOpcode() == ISD::ADD && N0.getOperand(0) == N1 &&
15789 isAllOnes(N0.getOperand(1)))
15790 return DAG.getNode(X86ISD::BLSR, DL, VT, N1);
15791
15792 // Check RHS for X-1
15793 if (N1.getOpcode() == ISD::ADD && N1.getOperand(0) == N0 &&
15794 isAllOnes(N1.getOperand(1)))
15795 return DAG.getNode(X86ISD::BLSR, DL, VT, N0);
15796
Craig Topper54a11172011-10-14 07:06:56 +000015797 return SDValue();
15798 }
15799
Bruno Cardoso Lopes466b0222011-07-13 21:36:51 +000015800 // Want to form ANDNP nodes:
15801 // 1) In the hopes of then easily combining them with OR and AND nodes
15802 // to form PBLEND/PSIGN.
15803 // 2) To match ANDN packed intrinsics
Bruno Cardoso Lopes466b0222011-07-13 21:36:51 +000015804 if (VT != MVT::v2i64 && VT != MVT::v4i64)
Nate Begemanb65c1752010-12-17 22:55:37 +000015805 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000015806
Nate Begemanb65c1752010-12-17 22:55:37 +000015807 SDValue N0 = N->getOperand(0);
15808 SDValue N1 = N->getOperand(1);
15809 DebugLoc DL = N->getDebugLoc();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000015810
Nate Begemanb65c1752010-12-17 22:55:37 +000015811 // Check LHS for vnot
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000015812 if (N0.getOpcode() == ISD::XOR &&
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +000015813 //ISD::isBuildVectorAllOnes(N0.getOperand(1).getNode()))
15814 CanFoldXORWithAllOnes(N0.getOperand(1).getNode()))
Bruno Cardoso Lopesc1af4772011-07-13 21:36:47 +000015815 return DAG.getNode(X86ISD::ANDNP, DL, VT, N0.getOperand(0), N1);
Nate Begemanb65c1752010-12-17 22:55:37 +000015816
15817 // Check RHS for vnot
15818 if (N1.getOpcode() == ISD::XOR &&
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +000015819 //ISD::isBuildVectorAllOnes(N1.getOperand(1).getNode()))
15820 CanFoldXORWithAllOnes(N1.getOperand(1).getNode()))
Bruno Cardoso Lopesc1af4772011-07-13 21:36:47 +000015821 return DAG.getNode(X86ISD::ANDNP, DL, VT, N1.getOperand(0), N0);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000015822
Nate Begemanb65c1752010-12-17 22:55:37 +000015823 return SDValue();
15824}
15825
Evan Cheng760d1942010-01-04 21:22:48 +000015826static SDValue PerformOrCombine(SDNode *N, SelectionDAG &DAG,
Evan Cheng8b1190a2010-04-28 01:18:01 +000015827 TargetLowering::DAGCombinerInfo &DCI,
Evan Cheng760d1942010-01-04 21:22:48 +000015828 const X86Subtarget *Subtarget) {
Evan Cheng39cfeec2010-04-28 02:25:18 +000015829 if (DCI.isBeforeLegalizeOps())
Evan Cheng8b1190a2010-04-28 01:18:01 +000015830 return SDValue();
15831
Stuart Hastings865f0932011-06-03 23:53:54 +000015832 SDValue R = CMPEQCombine(N, DAG, DCI, Subtarget);
15833 if (R.getNode())
15834 return R;
15835
Evan Cheng760d1942010-01-04 21:22:48 +000015836 EVT VT = N->getValueType(0);
Evan Cheng760d1942010-01-04 21:22:48 +000015837
Evan Cheng760d1942010-01-04 21:22:48 +000015838 SDValue N0 = N->getOperand(0);
15839 SDValue N1 = N->getOperand(1);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000015840
Nate Begemanb65c1752010-12-17 22:55:37 +000015841 // look for psign/blend
Craig Topper1666cb62011-11-19 07:07:26 +000015842 if (VT == MVT::v2i64 || VT == MVT::v4i64) {
Craig Topperd0a31172012-01-10 06:37:29 +000015843 if (!Subtarget->hasSSSE3() ||
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000015844 (VT == MVT::v4i64 && !Subtarget->hasInt256()))
Craig Topper1666cb62011-11-19 07:07:26 +000015845 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000015846
Craig Topper1666cb62011-11-19 07:07:26 +000015847 // Canonicalize pandn to RHS
15848 if (N0.getOpcode() == X86ISD::ANDNP)
15849 std::swap(N0, N1);
Lang Hames9ffaa6a2012-01-10 22:53:20 +000015850 // or (and (m, y), (pandn m, x))
Craig Topper1666cb62011-11-19 07:07:26 +000015851 if (N0.getOpcode() == ISD::AND && N1.getOpcode() == X86ISD::ANDNP) {
15852 SDValue Mask = N1.getOperand(0);
15853 SDValue X = N1.getOperand(1);
15854 SDValue Y;
15855 if (N0.getOperand(0) == Mask)
15856 Y = N0.getOperand(1);
15857 if (N0.getOperand(1) == Mask)
15858 Y = N0.getOperand(0);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000015859
Craig Topper1666cb62011-11-19 07:07:26 +000015860 // Check to see if the mask appeared in both the AND and ANDNP and
15861 if (!Y.getNode())
15862 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000015863
Craig Topper1666cb62011-11-19 07:07:26 +000015864 // Validate that X, Y, and Mask are BIT_CONVERTS, and see through them.
Craig Topper1666cb62011-11-19 07:07:26 +000015865 // Look through mask bitcast.
Nadav Rotem4ac90812012-04-01 19:31:22 +000015866 if (Mask.getOpcode() == ISD::BITCAST)
15867 Mask = Mask.getOperand(0);
15868 if (X.getOpcode() == ISD::BITCAST)
15869 X = X.getOperand(0);
15870 if (Y.getOpcode() == ISD::BITCAST)
15871 Y = Y.getOperand(0);
15872
Craig Topper1666cb62011-11-19 07:07:26 +000015873 EVT MaskVT = Mask.getValueType();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000015874
Craig Toppered2e13d2012-01-22 19:15:14 +000015875 // Validate that the Mask operand is a vector sra node.
Craig Topper1666cb62011-11-19 07:07:26 +000015876 // FIXME: what to do for bytes, since there is a psignb/pblendvb, but
15877 // there is no psrai.b
Craig Topper7fb8b0c2012-01-23 06:46:22 +000015878 if (Mask.getOpcode() != X86ISD::VSRAI)
Craig Toppered2e13d2012-01-22 19:15:14 +000015879 return SDValue();
Craig Topper1666cb62011-11-19 07:07:26 +000015880
15881 // Check that the SRA is all signbits.
Craig Topper7fb8b0c2012-01-23 06:46:22 +000015882 SDValue SraC = Mask.getOperand(1);
Craig Topper1666cb62011-11-19 07:07:26 +000015883 unsigned SraAmt = cast<ConstantSDNode>(SraC)->getZExtValue();
15884 unsigned EltBits = MaskVT.getVectorElementType().getSizeInBits();
15885 if ((SraAmt + 1) != EltBits)
15886 return SDValue();
15887
15888 DebugLoc DL = N->getDebugLoc();
15889
Nadav Rotemaf59e9a2012-12-07 21:43:11 +000015890 // We are going to replace the AND, OR, NAND with either BLEND
15891 // or PSIGN, which only look at the MSB. The VSRAI instruction
15892 // does not affect the highest bit, so we can get rid of it.
15893 Mask = Mask.getOperand(0);
15894
Craig Topper1666cb62011-11-19 07:07:26 +000015895 // Now we know we at least have a plendvb with the mask val. See if
15896 // we can form a psignb/w/d.
15897 // psign = x.type == y.type == mask.type && y = sub(0, x);
Craig Topper1666cb62011-11-19 07:07:26 +000015898 if (Y.getOpcode() == ISD::SUB && Y.getOperand(1) == X &&
15899 ISD::isBuildVectorAllZeros(Y.getOperand(0).getNode()) &&
Craig Toppered2e13d2012-01-22 19:15:14 +000015900 X.getValueType() == MaskVT && Y.getValueType() == MaskVT) {
15901 assert((EltBits == 8 || EltBits == 16 || EltBits == 32) &&
15902 "Unsupported VT for PSIGN");
Nadav Rotemaf59e9a2012-12-07 21:43:11 +000015903 Mask = DAG.getNode(X86ISD::PSIGN, DL, MaskVT, X, Mask);
Craig Toppered2e13d2012-01-22 19:15:14 +000015904 return DAG.getNode(ISD::BITCAST, DL, VT, Mask);
Craig Topper1666cb62011-11-19 07:07:26 +000015905 }
15906 // PBLENDVB only available on SSE 4.1
Craig Topperd0a31172012-01-10 06:37:29 +000015907 if (!Subtarget->hasSSE41())
Craig Topper1666cb62011-11-19 07:07:26 +000015908 return SDValue();
15909
15910 EVT BlendVT = (VT == MVT::v4i64) ? MVT::v32i8 : MVT::v16i8;
15911
15912 X = DAG.getNode(ISD::BITCAST, DL, BlendVT, X);
15913 Y = DAG.getNode(ISD::BITCAST, DL, BlendVT, Y);
15914 Mask = DAG.getNode(ISD::BITCAST, DL, BlendVT, Mask);
Nadav Rotem18197d72011-11-30 10:13:37 +000015915 Mask = DAG.getNode(ISD::VSELECT, DL, BlendVT, Mask, Y, X);
Craig Topper1666cb62011-11-19 07:07:26 +000015916 return DAG.getNode(ISD::BITCAST, DL, VT, Mask);
Nate Begemanb65c1752010-12-17 22:55:37 +000015917 }
15918 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000015919
Craig Topper1666cb62011-11-19 07:07:26 +000015920 if (VT != MVT::i16 && VT != MVT::i32 && VT != MVT::i64)
15921 return SDValue();
15922
Nate Begemanb65c1752010-12-17 22:55:37 +000015923 // fold (or (x << c) | (y >> (64 - c))) ==> (shld64 x, y, c)
Evan Cheng760d1942010-01-04 21:22:48 +000015924 if (N0.getOpcode() == ISD::SRL && N1.getOpcode() == ISD::SHL)
15925 std::swap(N0, N1);
15926 if (N0.getOpcode() != ISD::SHL || N1.getOpcode() != ISD::SRL)
15927 return SDValue();
Evan Cheng8b1190a2010-04-28 01:18:01 +000015928 if (!N0.hasOneUse() || !N1.hasOneUse())
15929 return SDValue();
Evan Cheng760d1942010-01-04 21:22:48 +000015930
15931 SDValue ShAmt0 = N0.getOperand(1);
15932 if (ShAmt0.getValueType() != MVT::i8)
15933 return SDValue();
15934 SDValue ShAmt1 = N1.getOperand(1);
15935 if (ShAmt1.getValueType() != MVT::i8)
15936 return SDValue();
15937 if (ShAmt0.getOpcode() == ISD::TRUNCATE)
15938 ShAmt0 = ShAmt0.getOperand(0);
15939 if (ShAmt1.getOpcode() == ISD::TRUNCATE)
15940 ShAmt1 = ShAmt1.getOperand(0);
15941
15942 DebugLoc DL = N->getDebugLoc();
15943 unsigned Opc = X86ISD::SHLD;
15944 SDValue Op0 = N0.getOperand(0);
15945 SDValue Op1 = N1.getOperand(0);
15946 if (ShAmt0.getOpcode() == ISD::SUB) {
15947 Opc = X86ISD::SHRD;
15948 std::swap(Op0, Op1);
15949 std::swap(ShAmt0, ShAmt1);
15950 }
15951
Evan Cheng8b1190a2010-04-28 01:18:01 +000015952 unsigned Bits = VT.getSizeInBits();
Evan Cheng760d1942010-01-04 21:22:48 +000015953 if (ShAmt1.getOpcode() == ISD::SUB) {
15954 SDValue Sum = ShAmt1.getOperand(0);
15955 if (ConstantSDNode *SumC = dyn_cast<ConstantSDNode>(Sum)) {
Dan Gohman4e39e9d2010-06-24 14:30:44 +000015956 SDValue ShAmt1Op1 = ShAmt1.getOperand(1);
15957 if (ShAmt1Op1.getNode()->getOpcode() == ISD::TRUNCATE)
15958 ShAmt1Op1 = ShAmt1Op1.getOperand(0);
15959 if (SumC->getSExtValue() == Bits && ShAmt1Op1 == ShAmt0)
Evan Cheng760d1942010-01-04 21:22:48 +000015960 return DAG.getNode(Opc, DL, VT,
15961 Op0, Op1,
15962 DAG.getNode(ISD::TRUNCATE, DL,
15963 MVT::i8, ShAmt0));
15964 }
15965 } else if (ConstantSDNode *ShAmt1C = dyn_cast<ConstantSDNode>(ShAmt1)) {
15966 ConstantSDNode *ShAmt0C = dyn_cast<ConstantSDNode>(ShAmt0);
15967 if (ShAmt0C &&
Evan Cheng8b1190a2010-04-28 01:18:01 +000015968 ShAmt0C->getSExtValue() + ShAmt1C->getSExtValue() == Bits)
Evan Cheng760d1942010-01-04 21:22:48 +000015969 return DAG.getNode(Opc, DL, VT,
15970 N0.getOperand(0), N1.getOperand(0),
15971 DAG.getNode(ISD::TRUNCATE, DL,
15972 MVT::i8, ShAmt0));
15973 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000015974
Evan Cheng760d1942010-01-04 21:22:48 +000015975 return SDValue();
15976}
15977
Manman Ren92363622012-06-07 22:39:10 +000015978// Generate NEG and CMOV for integer abs.
15979static SDValue performIntegerAbsCombine(SDNode *N, SelectionDAG &DAG) {
15980 EVT VT = N->getValueType(0);
15981
15982 // Since X86 does not have CMOV for 8-bit integer, we don't convert
15983 // 8-bit integer abs to NEG and CMOV.
15984 if (VT.isInteger() && VT.getSizeInBits() == 8)
15985 return SDValue();
15986
15987 SDValue N0 = N->getOperand(0);
15988 SDValue N1 = N->getOperand(1);
15989 DebugLoc DL = N->getDebugLoc();
15990
15991 // Check pattern of XOR(ADD(X,Y), Y) where Y is SRA(X, size(X)-1)
15992 // and change it to SUB and CMOV.
15993 if (VT.isInteger() && N->getOpcode() == ISD::XOR &&
15994 N0.getOpcode() == ISD::ADD &&
15995 N0.getOperand(1) == N1 &&
15996 N1.getOpcode() == ISD::SRA &&
15997 N1.getOperand(0) == N0.getOperand(0))
15998 if (ConstantSDNode *Y1C = dyn_cast<ConstantSDNode>(N1.getOperand(1)))
15999 if (Y1C->getAPIntValue() == VT.getSizeInBits()-1) {
16000 // Generate SUB & CMOV.
16001 SDValue Neg = DAG.getNode(X86ISD::SUB, DL, DAG.getVTList(VT, MVT::i32),
16002 DAG.getConstant(0, VT), N0.getOperand(0));
16003
16004 SDValue Ops[] = { N0.getOperand(0), Neg,
16005 DAG.getConstant(X86::COND_GE, MVT::i8),
16006 SDValue(Neg.getNode(), 1) };
16007 return DAG.getNode(X86ISD::CMOV, DL, DAG.getVTList(VT, MVT::Glue),
16008 Ops, array_lengthof(Ops));
16009 }
16010 return SDValue();
16011}
16012
Craig Topper3738ccd2011-12-27 06:27:23 +000016013// PerformXorCombine - Attempts to turn XOR nodes into BLSMSK nodes
Craig Topperb4c94572011-10-21 06:55:01 +000016014static SDValue PerformXorCombine(SDNode *N, SelectionDAG &DAG,
16015 TargetLowering::DAGCombinerInfo &DCI,
16016 const X86Subtarget *Subtarget) {
16017 if (DCI.isBeforeLegalizeOps())
16018 return SDValue();
16019
Manman Ren45d53b82012-06-08 18:58:26 +000016020 if (Subtarget->hasCMov()) {
16021 SDValue RV = performIntegerAbsCombine(N, DAG);
16022 if (RV.getNode())
16023 return RV;
16024 }
Manman Ren92363622012-06-07 22:39:10 +000016025
16026 // Try forming BMI if it is available.
16027 if (!Subtarget->hasBMI())
16028 return SDValue();
16029
Craig Topperb4c94572011-10-21 06:55:01 +000016030 EVT VT = N->getValueType(0);
16031
16032 if (VT != MVT::i32 && VT != MVT::i64)
16033 return SDValue();
16034
Craig Topper3738ccd2011-12-27 06:27:23 +000016035 assert(Subtarget->hasBMI() && "Creating BLSMSK requires BMI instructions");
16036
Craig Topperb4c94572011-10-21 06:55:01 +000016037 // Create BLSMSK instructions by finding X ^ (X-1)
16038 SDValue N0 = N->getOperand(0);
16039 SDValue N1 = N->getOperand(1);
16040 DebugLoc DL = N->getDebugLoc();
16041
16042 if (N0.getOpcode() == ISD::ADD && N0.getOperand(0) == N1 &&
16043 isAllOnes(N0.getOperand(1)))
16044 return DAG.getNode(X86ISD::BLSMSK, DL, VT, N1);
16045
16046 if (N1.getOpcode() == ISD::ADD && N1.getOperand(0) == N0 &&
16047 isAllOnes(N1.getOperand(1)))
16048 return DAG.getNode(X86ISD::BLSMSK, DL, VT, N0);
16049
16050 return SDValue();
16051}
16052
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016053/// PerformLOADCombine - Do target-specific dag combines on LOAD nodes.
16054static SDValue PerformLOADCombine(SDNode *N, SelectionDAG &DAG,
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000016055 TargetLowering::DAGCombinerInfo &DCI,
16056 const X86Subtarget *Subtarget) {
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016057 LoadSDNode *Ld = cast<LoadSDNode>(N);
16058 EVT RegVT = Ld->getValueType(0);
16059 EVT MemVT = Ld->getMemoryVT();
16060 DebugLoc dl = Ld->getDebugLoc();
16061 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
16062
16063 ISD::LoadExtType Ext = Ld->getExtensionType();
16064
Nadav Rotemca6f2962011-09-18 19:00:23 +000016065 // If this is a vector EXT Load then attempt to optimize it using a
Benjamin Kramer17347912012-12-22 11:34:28 +000016066 // shuffle. If SSSE3 is not available we may emit an illegal shuffle but the
16067 // expansion is still better than scalar code.
16068 // We generate X86ISD::VSEXT for SEXTLOADs if it's available, otherwise we'll
16069 // emit a shuffle and a arithmetic shift.
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016070 // TODO: It is possible to support ZExt by zeroing the undef values
16071 // during the shuffle phase or after the shuffle.
Benjamin Kramer17347912012-12-22 11:34:28 +000016072 if (RegVT.isVector() && RegVT.isInteger() && Subtarget->hasSSE2() &&
16073 (Ext == ISD::EXTLOAD || Ext == ISD::SEXTLOAD)) {
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016074 assert(MemVT != RegVT && "Cannot extend to the same type");
16075 assert(MemVT.isVector() && "Must load a vector from memory");
16076
16077 unsigned NumElems = RegVT.getVectorNumElements();
16078 unsigned RegSz = RegVT.getSizeInBits();
16079 unsigned MemSz = MemVT.getSizeInBits();
16080 assert(RegSz > MemSz && "Register size must be greater than the mem size");
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016081
Elena Demikhovsky4b977312012-12-19 07:50:20 +000016082 if (Ext == ISD::SEXTLOAD && RegSz == 256 && !Subtarget->hasInt256())
16083 return SDValue();
16084
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000016085 // All sizes must be a power of two.
16086 if (!isPowerOf2_32(RegSz * MemSz * NumElems))
16087 return SDValue();
16088
16089 // Attempt to load the original value using scalar loads.
16090 // Find the largest scalar type that divides the total loaded size.
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016091 MVT SclrLoadTy = MVT::i8;
16092 for (unsigned tp = MVT::FIRST_INTEGER_VALUETYPE;
16093 tp < MVT::LAST_INTEGER_VALUETYPE; ++tp) {
16094 MVT Tp = (MVT::SimpleValueType)tp;
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000016095 if (TLI.isTypeLegal(Tp) && ((MemSz % Tp.getSizeInBits()) == 0)) {
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016096 SclrLoadTy = Tp;
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016097 }
16098 }
16099
Nadav Rotem5cd95e12012-07-11 13:27:05 +000016100 // On 32bit systems, we can't save 64bit integers. Try bitcasting to F64.
16101 if (TLI.isTypeLegal(MVT::f64) && SclrLoadTy.getSizeInBits() < 64 &&
16102 (64 <= MemSz))
16103 SclrLoadTy = MVT::f64;
16104
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000016105 // Calculate the number of scalar loads that we need to perform
16106 // in order to load our vector from memory.
16107 unsigned NumLoads = MemSz / SclrLoadTy.getSizeInBits();
Elena Demikhovsky4b977312012-12-19 07:50:20 +000016108 if (Ext == ISD::SEXTLOAD && NumLoads > 1)
16109 return SDValue();
16110
16111 unsigned loadRegZize = RegSz;
16112 if (Ext == ISD::SEXTLOAD && RegSz == 256)
16113 loadRegZize /= 2;
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016114
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000016115 // Represent our vector as a sequence of elements which are the
16116 // largest scalar that we can load.
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016117 EVT LoadUnitVecVT = EVT::getVectorVT(*DAG.getContext(), SclrLoadTy,
Elena Demikhovsky4b977312012-12-19 07:50:20 +000016118 loadRegZize/SclrLoadTy.getSizeInBits());
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016119
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000016120 // Represent the data using the same element type that is stored in
16121 // memory. In practice, we ''widen'' MemVT.
Elena Demikhovsky4b977312012-12-19 07:50:20 +000016122 EVT WideVecVT =
16123 EVT::getVectorVT(*DAG.getContext(), MemVT.getScalarType(),
16124 loadRegZize/MemVT.getScalarType().getSizeInBits());
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016125
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000016126 assert(WideVecVT.getSizeInBits() == LoadUnitVecVT.getSizeInBits() &&
16127 "Invalid vector type");
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016128
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000016129 // We can't shuffle using an illegal type.
16130 if (!TLI.isTypeLegal(WideVecVT))
16131 return SDValue();
16132
16133 SmallVector<SDValue, 8> Chains;
16134 SDValue Ptr = Ld->getBasePtr();
16135 SDValue Increment = DAG.getConstant(SclrLoadTy.getSizeInBits()/8,
16136 TLI.getPointerTy());
16137 SDValue Res = DAG.getUNDEF(LoadUnitVecVT);
16138
16139 for (unsigned i = 0; i < NumLoads; ++i) {
16140 // Perform a single load.
16141 SDValue ScalarLoad = DAG.getLoad(SclrLoadTy, dl, Ld->getChain(),
16142 Ptr, Ld->getPointerInfo(),
16143 Ld->isVolatile(), Ld->isNonTemporal(),
16144 Ld->isInvariant(), Ld->getAlignment());
16145 Chains.push_back(ScalarLoad.getValue(1));
16146 // Create the first element type using SCALAR_TO_VECTOR in order to avoid
16147 // another round of DAGCombining.
16148 if (i == 0)
16149 Res = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, LoadUnitVecVT, ScalarLoad);
16150 else
16151 Res = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, LoadUnitVecVT, Res,
16152 ScalarLoad, DAG.getIntPtrConstant(i));
16153
16154 Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr, Increment);
16155 }
16156
16157 SDValue TF = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &Chains[0],
16158 Chains.size());
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016159
16160 // Bitcast the loaded value to a vector of the original element type, in
16161 // the size of the target vector type.
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000016162 SDValue SlicedVec = DAG.getNode(ISD::BITCAST, dl, WideVecVT, Res);
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016163 unsigned SizeRatio = RegSz/MemSz;
16164
Elena Demikhovsky4b977312012-12-19 07:50:20 +000016165 if (Ext == ISD::SEXTLOAD) {
Benjamin Kramer17347912012-12-22 11:34:28 +000016166 // If we have SSE4.1 we can directly emit a VSEXT node.
16167 if (Subtarget->hasSSE41()) {
16168 SDValue Sext = DAG.getNode(X86ISD::VSEXT, dl, RegVT, SlicedVec);
16169 return DCI.CombineTo(N, Sext, TF, true);
16170 }
16171
16172 // Otherwise we'll shuffle the small elements in the high bits of the
16173 // larger type and perform an arithmetic shift. If the shift is not legal
16174 // it's better to scalarize.
16175 if (!TLI.isOperationLegalOrCustom(ISD::SRA, RegVT))
16176 return SDValue();
16177
16178 // Redistribute the loaded elements into the different locations.
16179 SmallVector<int, 8> ShuffleVec(NumElems * SizeRatio, -1);
16180 for (unsigned i = 0; i != NumElems; ++i)
16181 ShuffleVec[i*SizeRatio + SizeRatio-1] = i;
16182
16183 SDValue Shuff = DAG.getVectorShuffle(WideVecVT, dl, SlicedVec,
16184 DAG.getUNDEF(WideVecVT),
16185 &ShuffleVec[0]);
16186
16187 Shuff = DAG.getNode(ISD::BITCAST, dl, RegVT, Shuff);
16188
16189 // Build the arithmetic shift.
16190 unsigned Amt = RegVT.getVectorElementType().getSizeInBits() -
16191 MemVT.getVectorElementType().getSizeInBits();
16192 SmallVector<SDValue, 8> C(NumElems,
16193 DAG.getConstant(Amt, RegVT.getScalarType()));
16194 SDValue BV = DAG.getNode(ISD::BUILD_VECTOR, dl, RegVT, &C[0], C.size());
16195 Shuff = DAG.getNode(ISD::SRA, dl, RegVT, Shuff, BV);
16196
16197 return DCI.CombineTo(N, Shuff, TF, true);
Elena Demikhovsky4b977312012-12-19 07:50:20 +000016198 }
Benjamin Kramer17347912012-12-22 11:34:28 +000016199
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016200 // Redistribute the loaded elements into the different locations.
16201 SmallVector<int, 8> ShuffleVec(NumElems * SizeRatio, -1);
Craig Topper31a207a2012-05-04 06:39:13 +000016202 for (unsigned i = 0; i != NumElems; ++i)
16203 ShuffleVec[i*SizeRatio] = i;
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016204
16205 SDValue Shuff = DAG.getVectorShuffle(WideVecVT, dl, SlicedVec,
Craig Topperdf966f62012-04-22 19:17:57 +000016206 DAG.getUNDEF(WideVecVT),
16207 &ShuffleVec[0]);
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016208
16209 // Bitcast to the requested type.
16210 Shuff = DAG.getNode(ISD::BITCAST, dl, RegVT, Shuff);
16211 // Replace the original load with the new sequence
16212 // and return the new chain.
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000016213 return DCI.CombineTo(N, Shuff, TF, true);
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016214 }
16215
16216 return SDValue();
16217}
16218
Chris Lattner149a4e52008-02-22 02:09:43 +000016219/// PerformSTORECombine - Do target-specific dag combines on STORE nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000016220static SDValue PerformSTORECombine(SDNode *N, SelectionDAG &DAG,
Evan Cheng536e6672009-03-12 05:59:15 +000016221 const X86Subtarget *Subtarget) {
Nadav Rotem614061b2011-08-10 19:30:14 +000016222 StoreSDNode *St = cast<StoreSDNode>(N);
16223 EVT VT = St->getValue().getValueType();
16224 EVT StVT = St->getMemoryVT();
16225 DebugLoc dl = St->getDebugLoc();
Nadav Rotem5e742a32011-08-11 16:41:21 +000016226 SDValue StoredVal = St->getOperand(1);
16227 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
16228
Nick Lewycky8a8d4792011-12-02 22:16:29 +000016229 // If we are saving a concatenation of two XMM registers, perform two stores.
Nadav Rotem87d35e82012-05-19 20:30:08 +000016230 // On Sandy Bridge, 256-bit memory operations are executed by two
16231 // 128-bit ports. However, on Haswell it is better to issue a single 256-bit
16232 // memory operation.
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000016233 if (VT.is256BitVector() && !Subtarget->hasInt256() &&
Craig Topperb4a8aef2012-04-27 21:05:09 +000016234 StoredVal.getNode()->getOpcode() == ISD::CONCAT_VECTORS &&
16235 StoredVal.getNumOperands() == 2) {
Nadav Rotem5e742a32011-08-11 16:41:21 +000016236 SDValue Value0 = StoredVal.getOperand(0);
16237 SDValue Value1 = StoredVal.getOperand(1);
16238
16239 SDValue Stride = DAG.getConstant(16, TLI.getPointerTy());
16240 SDValue Ptr0 = St->getBasePtr();
16241 SDValue Ptr1 = DAG.getNode(ISD::ADD, dl, Ptr0.getValueType(), Ptr0, Stride);
16242
16243 SDValue Ch0 = DAG.getStore(St->getChain(), dl, Value0, Ptr0,
16244 St->getPointerInfo(), St->isVolatile(),
16245 St->isNonTemporal(), St->getAlignment());
16246 SDValue Ch1 = DAG.getStore(St->getChain(), dl, Value1, Ptr1,
16247 St->getPointerInfo(), St->isVolatile(),
16248 St->isNonTemporal(), St->getAlignment());
16249 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Ch0, Ch1);
16250 }
Nadav Rotem614061b2011-08-10 19:30:14 +000016251
16252 // Optimize trunc store (of multiple scalars) to shuffle and store.
16253 // First, pack all of the elements in one place. Next, store to memory
16254 // in fewer chunks.
16255 if (St->isTruncatingStore() && VT.isVector()) {
16256 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
16257 unsigned NumElems = VT.getVectorNumElements();
16258 assert(StVT != VT && "Cannot truncate to the same type");
16259 unsigned FromSz = VT.getVectorElementType().getSizeInBits();
16260 unsigned ToSz = StVT.getVectorElementType().getSizeInBits();
16261
16262 // From, To sizes and ElemCount must be pow of two
16263 if (!isPowerOf2_32(NumElems * FromSz * ToSz)) return SDValue();
Nadav Rotem9c6cdf42011-09-21 08:45:10 +000016264 // We are going to use the original vector elt for storing.
Nadav Rotem64ac73b2011-09-21 17:14:40 +000016265 // Accumulated smaller vector elements must be a multiple of the store size.
Nadav Rotem9c6cdf42011-09-21 08:45:10 +000016266 if (0 != (NumElems * FromSz) % ToSz) return SDValue();
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016267
Nadav Rotem614061b2011-08-10 19:30:14 +000016268 unsigned SizeRatio = FromSz / ToSz;
16269
16270 assert(SizeRatio * NumElems * ToSz == VT.getSizeInBits());
16271
16272 // Create a type on which we perform the shuffle
16273 EVT WideVecVT = EVT::getVectorVT(*DAG.getContext(),
16274 StVT.getScalarType(), NumElems*SizeRatio);
16275
16276 assert(WideVecVT.getSizeInBits() == VT.getSizeInBits());
16277
16278 SDValue WideVec = DAG.getNode(ISD::BITCAST, dl, WideVecVT, St->getValue());
16279 SmallVector<int, 8> ShuffleVec(NumElems * SizeRatio, -1);
Craig Topper31a207a2012-05-04 06:39:13 +000016280 for (unsigned i = 0; i != NumElems; ++i)
16281 ShuffleVec[i] = i * SizeRatio;
Nadav Rotem614061b2011-08-10 19:30:14 +000016282
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000016283 // Can't shuffle using an illegal type.
16284 if (!TLI.isTypeLegal(WideVecVT))
16285 return SDValue();
Nadav Rotem614061b2011-08-10 19:30:14 +000016286
16287 SDValue Shuff = DAG.getVectorShuffle(WideVecVT, dl, WideVec,
Craig Topperdf966f62012-04-22 19:17:57 +000016288 DAG.getUNDEF(WideVecVT),
16289 &ShuffleVec[0]);
Nadav Rotem614061b2011-08-10 19:30:14 +000016290 // At this point all of the data is stored at the bottom of the
16291 // register. We now need to save it to mem.
16292
16293 // Find the largest store unit
16294 MVT StoreType = MVT::i8;
16295 for (unsigned tp = MVT::FIRST_INTEGER_VALUETYPE;
16296 tp < MVT::LAST_INTEGER_VALUETYPE; ++tp) {
16297 MVT Tp = (MVT::SimpleValueType)tp;
Nadav Rotem5cd95e12012-07-11 13:27:05 +000016298 if (TLI.isTypeLegal(Tp) && Tp.getSizeInBits() <= NumElems * ToSz)
Nadav Rotem614061b2011-08-10 19:30:14 +000016299 StoreType = Tp;
16300 }
16301
Nadav Rotem5cd95e12012-07-11 13:27:05 +000016302 // On 32bit systems, we can't save 64bit integers. Try bitcasting to F64.
16303 if (TLI.isTypeLegal(MVT::f64) && StoreType.getSizeInBits() < 64 &&
16304 (64 <= NumElems * ToSz))
16305 StoreType = MVT::f64;
16306
Nadav Rotem614061b2011-08-10 19:30:14 +000016307 // Bitcast the original vector into a vector of store-size units
16308 EVT StoreVecVT = EVT::getVectorVT(*DAG.getContext(),
Nadav Rotem5cd95e12012-07-11 13:27:05 +000016309 StoreType, VT.getSizeInBits()/StoreType.getSizeInBits());
Nadav Rotem614061b2011-08-10 19:30:14 +000016310 assert(StoreVecVT.getSizeInBits() == VT.getSizeInBits());
16311 SDValue ShuffWide = DAG.getNode(ISD::BITCAST, dl, StoreVecVT, Shuff);
16312 SmallVector<SDValue, 8> Chains;
16313 SDValue Increment = DAG.getConstant(StoreType.getSizeInBits()/8,
16314 TLI.getPointerTy());
16315 SDValue Ptr = St->getBasePtr();
16316
16317 // Perform one or more big stores into memory.
Craig Topper31a207a2012-05-04 06:39:13 +000016318 for (unsigned i=0, e=(ToSz*NumElems)/StoreType.getSizeInBits(); i!=e; ++i) {
Nadav Rotem614061b2011-08-10 19:30:14 +000016319 SDValue SubVec = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl,
16320 StoreType, ShuffWide,
16321 DAG.getIntPtrConstant(i));
16322 SDValue Ch = DAG.getStore(St->getChain(), dl, SubVec, Ptr,
16323 St->getPointerInfo(), St->isVolatile(),
16324 St->isNonTemporal(), St->getAlignment());
16325 Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr, Increment);
16326 Chains.push_back(Ch);
16327 }
16328
16329 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &Chains[0],
16330 Chains.size());
16331 }
16332
Chris Lattner149a4e52008-02-22 02:09:43 +000016333 // Turn load->store of MMX types into GPR load/stores. This avoids clobbering
16334 // the FP state in cases where an emms may be missing.
Dale Johannesen079f2a62008-02-25 19:20:14 +000016335 // A preferable solution to the general problem is to figure out the right
16336 // places to insert EMMS. This qualifies as a quick hack.
Evan Cheng536e6672009-03-12 05:59:15 +000016337
16338 // Similarly, turn load->store of i64 into double load/stores in 32-bit mode.
Evan Cheng536e6672009-03-12 05:59:15 +000016339 if (VT.getSizeInBits() != 64)
16340 return SDValue();
16341
Devang Patel578efa92009-06-05 21:57:13 +000016342 const Function *F = DAG.getMachineFunction().getFunction();
Bill Wendling67658342012-10-09 07:45:08 +000016343 bool NoImplicitFloatOps = F->getFnAttributes().
Bill Wendling034b94b2012-12-19 07:18:57 +000016344 hasAttribute(Attribute::NoImplicitFloat);
Nick Lewycky8a8d4792011-12-02 22:16:29 +000016345 bool F64IsLegal = !DAG.getTarget().Options.UseSoftFloat && !NoImplicitFloatOps
Craig Topper1accb7e2012-01-10 06:54:16 +000016346 && Subtarget->hasSSE2();
Evan Cheng536e6672009-03-12 05:59:15 +000016347 if ((VT.isVector() ||
Owen Anderson825b72b2009-08-11 20:47:22 +000016348 (VT == MVT::i64 && F64IsLegal && !Subtarget->is64Bit())) &&
Dale Johannesen079f2a62008-02-25 19:20:14 +000016349 isa<LoadSDNode>(St->getValue()) &&
16350 !cast<LoadSDNode>(St->getValue())->isVolatile() &&
16351 St->getChain().hasOneUse() && !St->isVolatile()) {
Gabor Greifba36cb52008-08-28 21:40:38 +000016352 SDNode* LdVal = St->getValue().getNode();
Dale Johannesen079f2a62008-02-25 19:20:14 +000016353 LoadSDNode *Ld = 0;
16354 int TokenFactorIndex = -1;
Dan Gohman475871a2008-07-27 21:46:04 +000016355 SmallVector<SDValue, 8> Ops;
Gabor Greifba36cb52008-08-28 21:40:38 +000016356 SDNode* ChainVal = St->getChain().getNode();
Dale Johannesen079f2a62008-02-25 19:20:14 +000016357 // Must be a store of a load. We currently handle two cases: the load
16358 // is a direct child, and it's under an intervening TokenFactor. It is
16359 // possible to dig deeper under nested TokenFactors.
Dale Johannesen14e2ea92008-02-25 22:29:22 +000016360 if (ChainVal == LdVal)
Dale Johannesen079f2a62008-02-25 19:20:14 +000016361 Ld = cast<LoadSDNode>(St->getChain());
16362 else if (St->getValue().hasOneUse() &&
16363 ChainVal->getOpcode() == ISD::TokenFactor) {
Chad Rosierc2348d52012-02-01 18:45:51 +000016364 for (unsigned i = 0, e = ChainVal->getNumOperands(); i != e; ++i) {
Gabor Greifba36cb52008-08-28 21:40:38 +000016365 if (ChainVal->getOperand(i).getNode() == LdVal) {
Dale Johannesen079f2a62008-02-25 19:20:14 +000016366 TokenFactorIndex = i;
16367 Ld = cast<LoadSDNode>(St->getValue());
16368 } else
16369 Ops.push_back(ChainVal->getOperand(i));
16370 }
16371 }
Dale Johannesen079f2a62008-02-25 19:20:14 +000016372
Evan Cheng536e6672009-03-12 05:59:15 +000016373 if (!Ld || !ISD::isNormalLoad(Ld))
16374 return SDValue();
Dale Johannesen079f2a62008-02-25 19:20:14 +000016375
Evan Cheng536e6672009-03-12 05:59:15 +000016376 // If this is not the MMX case, i.e. we are just turning i64 load/store
16377 // into f64 load/store, avoid the transformation if there are multiple
16378 // uses of the loaded value.
16379 if (!VT.isVector() && !Ld->hasNUsesOfValue(1, 0))
16380 return SDValue();
Dale Johannesen079f2a62008-02-25 19:20:14 +000016381
Evan Cheng536e6672009-03-12 05:59:15 +000016382 DebugLoc LdDL = Ld->getDebugLoc();
16383 DebugLoc StDL = N->getDebugLoc();
16384 // If we are a 64-bit capable x86, lower to a single movq load/store pair.
16385 // Otherwise, if it's legal to use f64 SSE instructions, use f64 load/store
16386 // pair instead.
16387 if (Subtarget->is64Bit() || F64IsLegal) {
Owen Anderson825b72b2009-08-11 20:47:22 +000016388 EVT LdVT = Subtarget->is64Bit() ? MVT::i64 : MVT::f64;
Chris Lattner51abfe42010-09-21 06:02:19 +000016389 SDValue NewLd = DAG.getLoad(LdVT, LdDL, Ld->getChain(), Ld->getBasePtr(),
16390 Ld->getPointerInfo(), Ld->isVolatile(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000016391 Ld->isNonTemporal(), Ld->isInvariant(),
16392 Ld->getAlignment());
Evan Cheng536e6672009-03-12 05:59:15 +000016393 SDValue NewChain = NewLd.getValue(1);
Dale Johannesen079f2a62008-02-25 19:20:14 +000016394 if (TokenFactorIndex != -1) {
Evan Cheng536e6672009-03-12 05:59:15 +000016395 Ops.push_back(NewChain);
Owen Anderson825b72b2009-08-11 20:47:22 +000016396 NewChain = DAG.getNode(ISD::TokenFactor, LdDL, MVT::Other, &Ops[0],
Dale Johannesen079f2a62008-02-25 19:20:14 +000016397 Ops.size());
16398 }
Evan Cheng536e6672009-03-12 05:59:15 +000016399 return DAG.getStore(NewChain, StDL, NewLd, St->getBasePtr(),
Chris Lattner51abfe42010-09-21 06:02:19 +000016400 St->getPointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +000016401 St->isVolatile(), St->isNonTemporal(),
16402 St->getAlignment());
Chris Lattner149a4e52008-02-22 02:09:43 +000016403 }
Evan Cheng536e6672009-03-12 05:59:15 +000016404
16405 // Otherwise, lower to two pairs of 32-bit loads / stores.
16406 SDValue LoAddr = Ld->getBasePtr();
Owen Anderson825b72b2009-08-11 20:47:22 +000016407 SDValue HiAddr = DAG.getNode(ISD::ADD, LdDL, MVT::i32, LoAddr,
16408 DAG.getConstant(4, MVT::i32));
Evan Cheng536e6672009-03-12 05:59:15 +000016409
Owen Anderson825b72b2009-08-11 20:47:22 +000016410 SDValue LoLd = DAG.getLoad(MVT::i32, LdDL, Ld->getChain(), LoAddr,
Chris Lattner51abfe42010-09-21 06:02:19 +000016411 Ld->getPointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +000016412 Ld->isVolatile(), Ld->isNonTemporal(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000016413 Ld->isInvariant(), Ld->getAlignment());
Owen Anderson825b72b2009-08-11 20:47:22 +000016414 SDValue HiLd = DAG.getLoad(MVT::i32, LdDL, Ld->getChain(), HiAddr,
Chris Lattner51abfe42010-09-21 06:02:19 +000016415 Ld->getPointerInfo().getWithOffset(4),
David Greene67c9d422010-02-15 16:53:33 +000016416 Ld->isVolatile(), Ld->isNonTemporal(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000016417 Ld->isInvariant(),
Evan Cheng536e6672009-03-12 05:59:15 +000016418 MinAlign(Ld->getAlignment(), 4));
16419
16420 SDValue NewChain = LoLd.getValue(1);
16421 if (TokenFactorIndex != -1) {
16422 Ops.push_back(LoLd);
16423 Ops.push_back(HiLd);
Owen Anderson825b72b2009-08-11 20:47:22 +000016424 NewChain = DAG.getNode(ISD::TokenFactor, LdDL, MVT::Other, &Ops[0],
Evan Cheng536e6672009-03-12 05:59:15 +000016425 Ops.size());
16426 }
16427
16428 LoAddr = St->getBasePtr();
Owen Anderson825b72b2009-08-11 20:47:22 +000016429 HiAddr = DAG.getNode(ISD::ADD, StDL, MVT::i32, LoAddr,
16430 DAG.getConstant(4, MVT::i32));
Evan Cheng536e6672009-03-12 05:59:15 +000016431
16432 SDValue LoSt = DAG.getStore(NewChain, StDL, LoLd, LoAddr,
Chris Lattner8026a9d2010-09-21 17:50:43 +000016433 St->getPointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +000016434 St->isVolatile(), St->isNonTemporal(),
16435 St->getAlignment());
Evan Cheng536e6672009-03-12 05:59:15 +000016436 SDValue HiSt = DAG.getStore(NewChain, StDL, HiLd, HiAddr,
Chris Lattner8026a9d2010-09-21 17:50:43 +000016437 St->getPointerInfo().getWithOffset(4),
Evan Cheng536e6672009-03-12 05:59:15 +000016438 St->isVolatile(),
David Greene67c9d422010-02-15 16:53:33 +000016439 St->isNonTemporal(),
Evan Cheng536e6672009-03-12 05:59:15 +000016440 MinAlign(St->getAlignment(), 4));
Owen Anderson825b72b2009-08-11 20:47:22 +000016441 return DAG.getNode(ISD::TokenFactor, StDL, MVT::Other, LoSt, HiSt);
Chris Lattner149a4e52008-02-22 02:09:43 +000016442 }
Dan Gohman475871a2008-07-27 21:46:04 +000016443 return SDValue();
Chris Lattner149a4e52008-02-22 02:09:43 +000016444}
16445
Duncan Sands17470be2011-09-22 20:15:48 +000016446/// isHorizontalBinOp - Return 'true' if this vector operation is "horizontal"
16447/// and return the operands for the horizontal operation in LHS and RHS. A
16448/// horizontal operation performs the binary operation on successive elements
16449/// of its first operand, then on successive elements of its second operand,
16450/// returning the resulting values in a vector. For example, if
16451/// A = < float a0, float a1, float a2, float a3 >
16452/// and
16453/// B = < float b0, float b1, float b2, float b3 >
16454/// then the result of doing a horizontal operation on A and B is
16455/// A horizontal-op B = < a0 op a1, a2 op a3, b0 op b1, b2 op b3 >.
16456/// In short, LHS and RHS are inspected to see if LHS op RHS is of the form
16457/// A horizontal-op B, for some already available A and B, and if so then LHS is
16458/// set to A, RHS to B, and the routine returns 'true'.
16459/// Note that the binary operation should have the property that if one of the
16460/// operands is UNDEF then the result is UNDEF.
Craig Topperbeabc6c2011-12-05 06:56:46 +000016461static bool isHorizontalBinOp(SDValue &LHS, SDValue &RHS, bool IsCommutative) {
Duncan Sands17470be2011-09-22 20:15:48 +000016462 // Look for the following pattern: if
16463 // A = < float a0, float a1, float a2, float a3 >
16464 // B = < float b0, float b1, float b2, float b3 >
16465 // and
16466 // LHS = VECTOR_SHUFFLE A, B, <0, 2, 4, 6>
16467 // RHS = VECTOR_SHUFFLE A, B, <1, 3, 5, 7>
16468 // then LHS op RHS = < a0 op a1, a2 op a3, b0 op b1, b2 op b3 >
16469 // which is A horizontal-op B.
16470
16471 // At least one of the operands should be a vector shuffle.
16472 if (LHS.getOpcode() != ISD::VECTOR_SHUFFLE &&
16473 RHS.getOpcode() != ISD::VECTOR_SHUFFLE)
16474 return false;
16475
16476 EVT VT = LHS.getValueType();
Craig Topperf8363302011-12-02 08:18:41 +000016477
16478 assert((VT.is128BitVector() || VT.is256BitVector()) &&
16479 "Unsupported vector type for horizontal add/sub");
16480
16481 // Handle 128 and 256-bit vector lengths. AVX defines horizontal add/sub to
16482 // operate independently on 128-bit lanes.
Craig Topperb72039c2011-11-30 09:10:50 +000016483 unsigned NumElts = VT.getVectorNumElements();
16484 unsigned NumLanes = VT.getSizeInBits()/128;
16485 unsigned NumLaneElts = NumElts / NumLanes;
Craig Topperf8363302011-12-02 08:18:41 +000016486 assert((NumLaneElts % 2 == 0) &&
16487 "Vector type should have an even number of elements in each lane");
16488 unsigned HalfLaneElts = NumLaneElts/2;
Duncan Sands17470be2011-09-22 20:15:48 +000016489
16490 // View LHS in the form
16491 // LHS = VECTOR_SHUFFLE A, B, LMask
16492 // If LHS is not a shuffle then pretend it is the shuffle
16493 // LHS = VECTOR_SHUFFLE LHS, undef, <0, 1, ..., N-1>
16494 // NOTE: in what follows a default initialized SDValue represents an UNDEF of
16495 // type VT.
16496 SDValue A, B;
Craig Topperb72039c2011-11-30 09:10:50 +000016497 SmallVector<int, 16> LMask(NumElts);
Duncan Sands17470be2011-09-22 20:15:48 +000016498 if (LHS.getOpcode() == ISD::VECTOR_SHUFFLE) {
16499 if (LHS.getOperand(0).getOpcode() != ISD::UNDEF)
16500 A = LHS.getOperand(0);
16501 if (LHS.getOperand(1).getOpcode() != ISD::UNDEF)
16502 B = LHS.getOperand(1);
Benjamin Kramered4c8c62012-01-15 13:16:05 +000016503 ArrayRef<int> Mask = cast<ShuffleVectorSDNode>(LHS.getNode())->getMask();
16504 std::copy(Mask.begin(), Mask.end(), LMask.begin());
Duncan Sands17470be2011-09-22 20:15:48 +000016505 } else {
16506 if (LHS.getOpcode() != ISD::UNDEF)
16507 A = LHS;
Craig Topperb72039c2011-11-30 09:10:50 +000016508 for (unsigned i = 0; i != NumElts; ++i)
Duncan Sands17470be2011-09-22 20:15:48 +000016509 LMask[i] = i;
16510 }
16511
16512 // Likewise, view RHS in the form
16513 // RHS = VECTOR_SHUFFLE C, D, RMask
16514 SDValue C, D;
Craig Topperb72039c2011-11-30 09:10:50 +000016515 SmallVector<int, 16> RMask(NumElts);
Duncan Sands17470be2011-09-22 20:15:48 +000016516 if (RHS.getOpcode() == ISD::VECTOR_SHUFFLE) {
16517 if (RHS.getOperand(0).getOpcode() != ISD::UNDEF)
16518 C = RHS.getOperand(0);
16519 if (RHS.getOperand(1).getOpcode() != ISD::UNDEF)
16520 D = RHS.getOperand(1);
Benjamin Kramered4c8c62012-01-15 13:16:05 +000016521 ArrayRef<int> Mask = cast<ShuffleVectorSDNode>(RHS.getNode())->getMask();
16522 std::copy(Mask.begin(), Mask.end(), RMask.begin());
Duncan Sands17470be2011-09-22 20:15:48 +000016523 } else {
16524 if (RHS.getOpcode() != ISD::UNDEF)
16525 C = RHS;
Craig Topperb72039c2011-11-30 09:10:50 +000016526 for (unsigned i = 0; i != NumElts; ++i)
Duncan Sands17470be2011-09-22 20:15:48 +000016527 RMask[i] = i;
16528 }
16529
16530 // Check that the shuffles are both shuffling the same vectors.
16531 if (!(A == C && B == D) && !(A == D && B == C))
16532 return false;
16533
16534 // If everything is UNDEF then bail out: it would be better to fold to UNDEF.
16535 if (!A.getNode() && !B.getNode())
16536 return false;
16537
16538 // If A and B occur in reverse order in RHS, then "swap" them (which means
16539 // rewriting the mask).
16540 if (A != C)
Craig Topperbeabc6c2011-12-05 06:56:46 +000016541 CommuteVectorShuffleMask(RMask, NumElts);
Duncan Sands17470be2011-09-22 20:15:48 +000016542
16543 // At this point LHS and RHS are equivalent to
16544 // LHS = VECTOR_SHUFFLE A, B, LMask
16545 // RHS = VECTOR_SHUFFLE A, B, RMask
16546 // Check that the masks correspond to performing a horizontal operation.
Craig Topperf8363302011-12-02 08:18:41 +000016547 for (unsigned i = 0; i != NumElts; ++i) {
Craig Topperbeabc6c2011-12-05 06:56:46 +000016548 int LIdx = LMask[i], RIdx = RMask[i];
Duncan Sands17470be2011-09-22 20:15:48 +000016549
Craig Topperf8363302011-12-02 08:18:41 +000016550 // Ignore any UNDEF components.
Craig Topperbeabc6c2011-12-05 06:56:46 +000016551 if (LIdx < 0 || RIdx < 0 ||
16552 (!A.getNode() && (LIdx < (int)NumElts || RIdx < (int)NumElts)) ||
16553 (!B.getNode() && (LIdx >= (int)NumElts || RIdx >= (int)NumElts)))
Craig Topperf8363302011-12-02 08:18:41 +000016554 continue;
Duncan Sands17470be2011-09-22 20:15:48 +000016555
Craig Topperf8363302011-12-02 08:18:41 +000016556 // Check that successive elements are being operated on. If not, this is
16557 // not a horizontal operation.
16558 unsigned Src = (i/HalfLaneElts) % 2; // each lane is split between srcs
16559 unsigned LaneStart = (i/NumLaneElts) * NumLaneElts;
Craig Topperbeabc6c2011-12-05 06:56:46 +000016560 int Index = 2*(i%HalfLaneElts) + NumElts*Src + LaneStart;
Craig Topperf8363302011-12-02 08:18:41 +000016561 if (!(LIdx == Index && RIdx == Index + 1) &&
Craig Topperbeabc6c2011-12-05 06:56:46 +000016562 !(IsCommutative && LIdx == Index + 1 && RIdx == Index))
Craig Topperf8363302011-12-02 08:18:41 +000016563 return false;
Duncan Sands17470be2011-09-22 20:15:48 +000016564 }
16565
16566 LHS = A.getNode() ? A : B; // If A is 'UNDEF', use B for it.
16567 RHS = B.getNode() ? B : A; // If B is 'UNDEF', use A for it.
16568 return true;
16569}
16570
16571/// PerformFADDCombine - Do target-specific dag combines on floating point adds.
16572static SDValue PerformFADDCombine(SDNode *N, SelectionDAG &DAG,
16573 const X86Subtarget *Subtarget) {
16574 EVT VT = N->getValueType(0);
16575 SDValue LHS = N->getOperand(0);
16576 SDValue RHS = N->getOperand(1);
16577
16578 // Try to synthesize horizontal adds from adds of shuffles.
Craig Topperd0a31172012-01-10 06:37:29 +000016579 if (((Subtarget->hasSSE3() && (VT == MVT::v4f32 || VT == MVT::v2f64)) ||
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000016580 (Subtarget->hasFp256() && (VT == MVT::v8f32 || VT == MVT::v4f64))) &&
Duncan Sands17470be2011-09-22 20:15:48 +000016581 isHorizontalBinOp(LHS, RHS, true))
16582 return DAG.getNode(X86ISD::FHADD, N->getDebugLoc(), VT, LHS, RHS);
16583 return SDValue();
16584}
16585
16586/// PerformFSUBCombine - Do target-specific dag combines on floating point subs.
16587static SDValue PerformFSUBCombine(SDNode *N, SelectionDAG &DAG,
16588 const X86Subtarget *Subtarget) {
16589 EVT VT = N->getValueType(0);
16590 SDValue LHS = N->getOperand(0);
16591 SDValue RHS = N->getOperand(1);
16592
16593 // Try to synthesize horizontal subs from subs of shuffles.
Craig Topperd0a31172012-01-10 06:37:29 +000016594 if (((Subtarget->hasSSE3() && (VT == MVT::v4f32 || VT == MVT::v2f64)) ||
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000016595 (Subtarget->hasFp256() && (VT == MVT::v8f32 || VT == MVT::v4f64))) &&
Duncan Sands17470be2011-09-22 20:15:48 +000016596 isHorizontalBinOp(LHS, RHS, false))
16597 return DAG.getNode(X86ISD::FHSUB, N->getDebugLoc(), VT, LHS, RHS);
16598 return SDValue();
16599}
16600
Chris Lattner6cf73262008-01-25 06:14:17 +000016601/// PerformFORCombine - Do target-specific dag combines on X86ISD::FOR and
16602/// X86ISD::FXOR nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000016603static SDValue PerformFORCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattner6cf73262008-01-25 06:14:17 +000016604 assert(N->getOpcode() == X86ISD::FOR || N->getOpcode() == X86ISD::FXOR);
16605 // F[X]OR(0.0, x) -> x
16606 // F[X]OR(x, 0.0) -> x
Chris Lattneraf723b92008-01-25 05:46:26 +000016607 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
16608 if (C->getValueAPF().isPosZero())
16609 return N->getOperand(1);
16610 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
16611 if (C->getValueAPF().isPosZero())
16612 return N->getOperand(0);
Dan Gohman475871a2008-07-27 21:46:04 +000016613 return SDValue();
Chris Lattneraf723b92008-01-25 05:46:26 +000016614}
16615
Nadav Rotemd60cb112012-08-19 13:06:16 +000016616/// PerformFMinFMaxCombine - Do target-specific dag combines on X86ISD::FMIN and
16617/// X86ISD::FMAX nodes.
16618static SDValue PerformFMinFMaxCombine(SDNode *N, SelectionDAG &DAG) {
16619 assert(N->getOpcode() == X86ISD::FMIN || N->getOpcode() == X86ISD::FMAX);
16620
16621 // Only perform optimizations if UnsafeMath is used.
16622 if (!DAG.getTarget().Options.UnsafeFPMath)
16623 return SDValue();
16624
16625 // If we run in unsafe-math mode, then convert the FMAX and FMIN nodes
Craig Topper8365e9b2012-09-01 06:33:50 +000016626 // into FMINC and FMAXC, which are Commutative operations.
Nadav Rotemd60cb112012-08-19 13:06:16 +000016627 unsigned NewOp = 0;
16628 switch (N->getOpcode()) {
16629 default: llvm_unreachable("unknown opcode");
16630 case X86ISD::FMIN: NewOp = X86ISD::FMINC; break;
16631 case X86ISD::FMAX: NewOp = X86ISD::FMAXC; break;
16632 }
16633
16634 return DAG.getNode(NewOp, N->getDebugLoc(), N->getValueType(0),
16635 N->getOperand(0), N->getOperand(1));
16636}
16637
Chris Lattneraf723b92008-01-25 05:46:26 +000016638/// PerformFANDCombine - Do target-specific dag combines on X86ISD::FAND nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000016639static SDValue PerformFANDCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattneraf723b92008-01-25 05:46:26 +000016640 // FAND(0.0, x) -> 0.0
16641 // FAND(x, 0.0) -> 0.0
16642 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
16643 if (C->getValueAPF().isPosZero())
16644 return N->getOperand(0);
16645 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
16646 if (C->getValueAPF().isPosZero())
16647 return N->getOperand(1);
Dan Gohman475871a2008-07-27 21:46:04 +000016648 return SDValue();
Chris Lattneraf723b92008-01-25 05:46:26 +000016649}
16650
Dan Gohmane5af2d32009-01-29 01:59:02 +000016651static SDValue PerformBTCombine(SDNode *N,
16652 SelectionDAG &DAG,
16653 TargetLowering::DAGCombinerInfo &DCI) {
16654 // BT ignores high bits in the bit index operand.
16655 SDValue Op1 = N->getOperand(1);
16656 if (Op1.hasOneUse()) {
16657 unsigned BitWidth = Op1.getValueSizeInBits();
16658 APInt DemandedMask = APInt::getLowBitsSet(BitWidth, Log2_32(BitWidth));
16659 APInt KnownZero, KnownOne;
Evan Chenge5b51ac2010-04-17 06:13:15 +000016660 TargetLowering::TargetLoweringOpt TLO(DAG, !DCI.isBeforeLegalize(),
16661 !DCI.isBeforeLegalizeOps());
Dan Gohmand858e902010-04-17 15:26:15 +000016662 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Dan Gohmane5af2d32009-01-29 01:59:02 +000016663 if (TLO.ShrinkDemandedConstant(Op1, DemandedMask) ||
16664 TLI.SimplifyDemandedBits(Op1, DemandedMask, KnownZero, KnownOne, TLO))
16665 DCI.CommitTargetLoweringOpt(TLO);
16666 }
16667 return SDValue();
16668}
Chris Lattner83e6c992006-10-04 06:57:07 +000016669
Eli Friedman7a5e5552009-06-07 06:52:44 +000016670static SDValue PerformVZEXT_MOVLCombine(SDNode *N, SelectionDAG &DAG) {
16671 SDValue Op = N->getOperand(0);
Wesley Peckbf17cfa2010-11-23 03:31:01 +000016672 if (Op.getOpcode() == ISD::BITCAST)
Eli Friedman7a5e5552009-06-07 06:52:44 +000016673 Op = Op.getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +000016674 EVT VT = N->getValueType(0), OpVT = Op.getValueType();
Eli Friedman7a5e5552009-06-07 06:52:44 +000016675 if (Op.getOpcode() == X86ISD::VZEXT_LOAD &&
Eric Christopherfd179292009-08-27 18:07:15 +000016676 VT.getVectorElementType().getSizeInBits() ==
Eli Friedman7a5e5552009-06-07 06:52:44 +000016677 OpVT.getVectorElementType().getSizeInBits()) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +000016678 return DAG.getNode(ISD::BITCAST, N->getDebugLoc(), VT, Op);
Eli Friedman7a5e5552009-06-07 06:52:44 +000016679 }
16680 return SDValue();
16681}
16682
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +000016683static SDValue PerformSExtCombine(SDNode *N, SelectionDAG &DAG,
16684 TargetLowering::DAGCombinerInfo &DCI,
16685 const X86Subtarget *Subtarget) {
16686 if (!DCI.isBeforeLegalizeOps())
16687 return SDValue();
16688
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000016689 if (!Subtarget->hasFp256())
Elena Demikhovskyf6020402012-02-08 08:37:26 +000016690 return SDValue();
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +000016691
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +000016692 EVT VT = N->getValueType(0);
16693 SDValue Op = N->getOperand(0);
16694 EVT OpVT = Op.getValueType();
16695 DebugLoc dl = N->getDebugLoc();
16696
Elena Demikhovskyf6020402012-02-08 08:37:26 +000016697 if ((VT == MVT::v4i64 && OpVT == MVT::v4i32) ||
16698 (VT == MVT::v8i32 && OpVT == MVT::v8i16)) {
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +000016699
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000016700 if (Subtarget->hasInt256())
Elena Demikhovsky1da58672012-04-22 09:39:03 +000016701 return DAG.getNode(X86ISD::VSEXT_MOVL, dl, VT, Op);
Elena Demikhovsky1da58672012-04-22 09:39:03 +000016702
16703 // Optimize vectors in AVX mode
16704 // Sign extend v8i16 to v8i32 and
16705 // v4i32 to v4i64
16706 //
16707 // Divide input vector into two parts
16708 // for v4i32 the shuffle mask will be { 0, 1, -1, -1} {2, 3, -1, -1}
16709 // use vpmovsx instruction to extend v4i32 -> v2i64; v8i16 -> v4i32
16710 // concat the vectors to original VT
16711
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +000016712 unsigned NumElems = OpVT.getVectorNumElements();
Craig Toppercacafd42012-08-14 08:18:43 +000016713 SDValue Undef = DAG.getUNDEF(OpVT);
16714
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +000016715 SmallVector<int,8> ShufMask1(NumElems, -1);
Craig Topper3ef43cf2012-04-24 06:36:35 +000016716 for (unsigned i = 0; i != NumElems/2; ++i)
16717 ShufMask1[i] = i;
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +000016718
Craig Toppercacafd42012-08-14 08:18:43 +000016719 SDValue OpLo = DAG.getVectorShuffle(OpVT, dl, Op, Undef, &ShufMask1[0]);
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +000016720
16721 SmallVector<int,8> ShufMask2(NumElems, -1);
Craig Topper3ef43cf2012-04-24 06:36:35 +000016722 for (unsigned i = 0; i != NumElems/2; ++i)
16723 ShufMask2[i] = i + NumElems/2;
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +000016724
Craig Toppercacafd42012-08-14 08:18:43 +000016725 SDValue OpHi = DAG.getVectorShuffle(OpVT, dl, Op, Undef, &ShufMask2[0]);
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +000016726
Craig Topper3ef43cf2012-04-24 06:36:35 +000016727 EVT HalfVT = EVT::getVectorVT(*DAG.getContext(), VT.getScalarType(),
Elena Demikhovskyf6020402012-02-08 08:37:26 +000016728 VT.getVectorNumElements()/2);
16729
Craig Topper3ef43cf2012-04-24 06:36:35 +000016730 OpLo = DAG.getNode(X86ISD::VSEXT_MOVL, dl, HalfVT, OpLo);
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +000016731 OpHi = DAG.getNode(X86ISD::VSEXT_MOVL, dl, HalfVT, OpHi);
16732
16733 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT, OpLo, OpHi);
16734 }
16735 return SDValue();
16736}
16737
Michael Liaof6c24ee2012-08-10 14:39:24 +000016738static SDValue PerformFMACombine(SDNode *N, SelectionDAG &DAG,
Elena Demikhovsky1503aba2012-08-01 12:06:00 +000016739 const X86Subtarget* Subtarget) {
16740 DebugLoc dl = N->getDebugLoc();
16741 EVT VT = N->getValueType(0);
16742
Craig Topperb1bdd7d2012-08-30 06:56:15 +000016743 // Let legalize expand this if it isn't a legal type yet.
16744 if (!DAG.getTargetLoweringInfo().isTypeLegal(VT))
16745 return SDValue();
16746
Elena Demikhovsky1503aba2012-08-01 12:06:00 +000016747 EVT ScalarVT = VT.getScalarType();
Craig Topperbf404372012-08-31 15:40:30 +000016748 if ((ScalarVT != MVT::f32 && ScalarVT != MVT::f64) ||
16749 (!Subtarget->hasFMA() && !Subtarget->hasFMA4()))
Elena Demikhovsky1503aba2012-08-01 12:06:00 +000016750 return SDValue();
16751
16752 SDValue A = N->getOperand(0);
16753 SDValue B = N->getOperand(1);
16754 SDValue C = N->getOperand(2);
16755
16756 bool NegA = (A.getOpcode() == ISD::FNEG);
16757 bool NegB = (B.getOpcode() == ISD::FNEG);
16758 bool NegC = (C.getOpcode() == ISD::FNEG);
16759
Michael Liaof6c24ee2012-08-10 14:39:24 +000016760 // Negative multiplication when NegA xor NegB
16761 bool NegMul = (NegA != NegB);
Elena Demikhovsky1503aba2012-08-01 12:06:00 +000016762 if (NegA)
16763 A = A.getOperand(0);
16764 if (NegB)
16765 B = B.getOperand(0);
16766 if (NegC)
16767 C = C.getOperand(0);
16768
16769 unsigned Opcode;
16770 if (!NegMul)
Craig Topperbf404372012-08-31 15:40:30 +000016771 Opcode = (!NegC) ? X86ISD::FMADD : X86ISD::FMSUB;
Elena Demikhovsky1503aba2012-08-01 12:06:00 +000016772 else
Craig Topperbf404372012-08-31 15:40:30 +000016773 Opcode = (!NegC) ? X86ISD::FNMADD : X86ISD::FNMSUB;
16774
Elena Demikhovsky1503aba2012-08-01 12:06:00 +000016775 return DAG.getNode(Opcode, dl, VT, A, B, C);
16776}
16777
Elena Demikhovsky28d7e712012-01-24 13:54:13 +000016778static SDValue PerformZExtCombine(SDNode *N, SelectionDAG &DAG,
Craig Topperc16f8512012-04-25 06:39:39 +000016779 TargetLowering::DAGCombinerInfo &DCI,
Elena Demikhovsky28d7e712012-01-24 13:54:13 +000016780 const X86Subtarget *Subtarget) {
Evan Cheng2e489c42009-12-16 00:53:11 +000016781 // (i32 zext (and (i8 x86isd::setcc_carry), 1)) ->
16782 // (and (i32 x86isd::setcc_carry), 1)
16783 // This eliminates the zext. This transformation is necessary because
16784 // ISD::SETCC is always legalized to i8.
16785 DebugLoc dl = N->getDebugLoc();
16786 SDValue N0 = N->getOperand(0);
16787 EVT VT = N->getValueType(0);
Elena Demikhovsky28d7e712012-01-24 13:54:13 +000016788 EVT OpVT = N0.getValueType();
16789
Evan Cheng2e489c42009-12-16 00:53:11 +000016790 if (N0.getOpcode() == ISD::AND &&
16791 N0.hasOneUse() &&
16792 N0.getOperand(0).hasOneUse()) {
16793 SDValue N00 = N0.getOperand(0);
16794 if (N00.getOpcode() != X86ISD::SETCC_CARRY)
16795 return SDValue();
16796 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
16797 if (!C || C->getZExtValue() != 1)
16798 return SDValue();
16799 return DAG.getNode(ISD::AND, dl, VT,
16800 DAG.getNode(X86ISD::SETCC_CARRY, dl, VT,
16801 N00.getOperand(0), N00.getOperand(1)),
16802 DAG.getConstant(1, VT));
16803 }
Craig Topperd0cf5652012-04-21 18:13:35 +000016804
Elena Demikhovsky28d7e712012-01-24 13:54:13 +000016805 // Optimize vectors in AVX mode:
16806 //
16807 // v8i16 -> v8i32
16808 // Use vpunpcklwd for 4 lower elements v8i16 -> v4i32.
16809 // Use vpunpckhwd for 4 upper elements v8i16 -> v4i32.
16810 // Concat upper and lower parts.
16811 //
16812 // v4i32 -> v4i64
16813 // Use vpunpckldq for 4 lower elements v4i32 -> v2i64.
16814 // Use vpunpckhdq for 4 upper elements v4i32 -> v2i64.
16815 // Concat upper and lower parts.
16816 //
Craig Topperc16f8512012-04-25 06:39:39 +000016817 if (!DCI.isBeforeLegalizeOps())
16818 return SDValue();
Elena Demikhovsky28d7e712012-01-24 13:54:13 +000016819
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000016820 if (!Subtarget->hasFp256())
Craig Topperc16f8512012-04-25 06:39:39 +000016821 return SDValue();
Elena Demikhovsky28d7e712012-01-24 13:54:13 +000016822
Craig Topperc16f8512012-04-25 06:39:39 +000016823 if (((VT == MVT::v8i32) && (OpVT == MVT::v8i16)) ||
16824 ((VT == MVT::v4i64) && (OpVT == MVT::v4i32))) {
Elena Demikhovsky1da58672012-04-22 09:39:03 +000016825
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000016826 if (Subtarget->hasInt256())
Craig Topperc16f8512012-04-25 06:39:39 +000016827 return DAG.getNode(X86ISD::VZEXT_MOVL, dl, VT, N0);
Elena Demikhovsky28d7e712012-01-24 13:54:13 +000016828
Craig Topperc16f8512012-04-25 06:39:39 +000016829 SDValue ZeroVec = getZeroVector(OpVT, Subtarget, DAG, dl);
16830 SDValue OpLo = getUnpackl(DAG, dl, OpVT, N0, ZeroVec);
16831 SDValue OpHi = getUnpackh(DAG, dl, OpVT, N0, ZeroVec);
Elena Demikhovsky28d7e712012-01-24 13:54:13 +000016832
Craig Topperc16f8512012-04-25 06:39:39 +000016833 EVT HVT = EVT::getVectorVT(*DAG.getContext(), VT.getVectorElementType(),
16834 VT.getVectorNumElements()/2);
Elena Demikhovsky28d7e712012-01-24 13:54:13 +000016835
Craig Topperc16f8512012-04-25 06:39:39 +000016836 OpLo = DAG.getNode(ISD::BITCAST, dl, HVT, OpLo);
16837 OpHi = DAG.getNode(ISD::BITCAST, dl, HVT, OpHi);
16838
16839 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT, OpLo, OpHi);
Elena Demikhovsky28d7e712012-01-24 13:54:13 +000016840 }
16841
Evan Cheng2e489c42009-12-16 00:53:11 +000016842 return SDValue();
16843}
16844
Chad Rosiera73b6fc2012-04-27 22:33:25 +000016845// Optimize x == -y --> x+y == 0
16846// x != -y --> x+y != 0
16847static SDValue PerformISDSETCCCombine(SDNode *N, SelectionDAG &DAG) {
16848 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(2))->get();
16849 SDValue LHS = N->getOperand(0);
Chad Rosiera20e1e72012-08-01 18:39:17 +000016850 SDValue RHS = N->getOperand(1);
Chad Rosiera73b6fc2012-04-27 22:33:25 +000016851
16852 if ((CC == ISD::SETNE || CC == ISD::SETEQ) && LHS.getOpcode() == ISD::SUB)
16853 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(LHS.getOperand(0)))
16854 if (C->getAPIntValue() == 0 && LHS.hasOneUse()) {
16855 SDValue addV = DAG.getNode(ISD::ADD, N->getDebugLoc(),
16856 LHS.getValueType(), RHS, LHS.getOperand(1));
16857 return DAG.getSetCC(N->getDebugLoc(), N->getValueType(0),
16858 addV, DAG.getConstant(0, addV.getValueType()), CC);
16859 }
16860 if ((CC == ISD::SETNE || CC == ISD::SETEQ) && RHS.getOpcode() == ISD::SUB)
16861 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(RHS.getOperand(0)))
16862 if (C->getAPIntValue() == 0 && RHS.hasOneUse()) {
16863 SDValue addV = DAG.getNode(ISD::ADD, N->getDebugLoc(),
16864 RHS.getValueType(), LHS, RHS.getOperand(1));
16865 return DAG.getSetCC(N->getDebugLoc(), N->getValueType(0),
16866 addV, DAG.getConstant(0, addV.getValueType()), CC);
16867 }
16868 return SDValue();
16869}
16870
Shuxin Yanga5526a92012-10-31 23:11:48 +000016871// Helper function of PerformSETCCCombine. It is to materialize "setb reg"
16872// as "sbb reg,reg", since it can be extended without zext and produces
16873// an all-ones bit which is more useful than 0/1 in some cases.
16874static SDValue MaterializeSETB(DebugLoc DL, SDValue EFLAGS, SelectionDAG &DAG) {
16875 return DAG.getNode(ISD::AND, DL, MVT::i8,
16876 DAG.getNode(X86ISD::SETCC_CARRY, DL, MVT::i8,
16877 DAG.getConstant(X86::COND_B, MVT::i8), EFLAGS),
16878 DAG.getConstant(1, MVT::i8));
16879}
16880
Chris Lattnerc19d1c32010-12-19 22:08:31 +000016881// Optimize RES = X86ISD::SETCC CONDCODE, EFLAG_INPUT
Michael Liaodbf8b5b2012-08-28 03:34:40 +000016882static SDValue PerformSETCCCombine(SDNode *N, SelectionDAG &DAG,
16883 TargetLowering::DAGCombinerInfo &DCI,
16884 const X86Subtarget *Subtarget) {
Chris Lattnerc19d1c32010-12-19 22:08:31 +000016885 DebugLoc DL = N->getDebugLoc();
Michael Liao2a33cec2012-08-10 19:58:13 +000016886 X86::CondCode CC = X86::CondCode(N->getConstantOperandVal(0));
16887 SDValue EFLAGS = N->getOperand(1);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000016888
Shuxin Yanga5526a92012-10-31 23:11:48 +000016889 if (CC == X86::COND_A) {
16890 // Try to convert COND_A into COND_B in an attempt to facilitate
16891 // materializing "setb reg".
16892 //
16893 // Do not flip "e > c", where "c" is a constant, because Cmp instruction
16894 // cannot take an immediate as its first operand.
16895 //
16896 if (EFLAGS.getOpcode() == X86ISD::SUB && EFLAGS.hasOneUse() &&
16897 EFLAGS.getValueType().isInteger() &&
16898 !isa<ConstantSDNode>(EFLAGS.getOperand(1))) {
16899 SDValue NewSub = DAG.getNode(X86ISD::SUB, EFLAGS.getDebugLoc(),
16900 EFLAGS.getNode()->getVTList(),
16901 EFLAGS.getOperand(1), EFLAGS.getOperand(0));
16902 SDValue NewEFLAGS = SDValue(NewSub.getNode(), EFLAGS.getResNo());
16903 return MaterializeSETB(DL, NewEFLAGS, DAG);
16904 }
16905 }
16906
Chris Lattnerc19d1c32010-12-19 22:08:31 +000016907 // Materialize "setb reg" as "sbb reg,reg", since it can be extended without
16908 // a zext and produces an all-ones bit which is more useful than 0/1 in some
16909 // cases.
Michael Liao2a33cec2012-08-10 19:58:13 +000016910 if (CC == X86::COND_B)
Shuxin Yanga5526a92012-10-31 23:11:48 +000016911 return MaterializeSETB(DL, EFLAGS, DAG);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000016912
Michael Liao2a33cec2012-08-10 19:58:13 +000016913 SDValue Flags;
16914
Michael Liaodbf8b5b2012-08-28 03:34:40 +000016915 Flags = checkBoolTestSetCCCombine(EFLAGS, CC);
16916 if (Flags.getNode()) {
16917 SDValue Cond = DAG.getConstant(CC, MVT::i8);
16918 return DAG.getNode(X86ISD::SETCC, DL, N->getVTList(), Cond, Flags);
16919 }
16920
Michael Liao2a33cec2012-08-10 19:58:13 +000016921 return SDValue();
16922}
16923
16924// Optimize branch condition evaluation.
16925//
16926static SDValue PerformBrCondCombine(SDNode *N, SelectionDAG &DAG,
16927 TargetLowering::DAGCombinerInfo &DCI,
16928 const X86Subtarget *Subtarget) {
16929 DebugLoc DL = N->getDebugLoc();
16930 SDValue Chain = N->getOperand(0);
16931 SDValue Dest = N->getOperand(1);
16932 SDValue EFLAGS = N->getOperand(3);
16933 X86::CondCode CC = X86::CondCode(N->getConstantOperandVal(2));
16934
16935 SDValue Flags;
16936
Michael Liaodbf8b5b2012-08-28 03:34:40 +000016937 Flags = checkBoolTestSetCCCombine(EFLAGS, CC);
16938 if (Flags.getNode()) {
16939 SDValue Cond = DAG.getConstant(CC, MVT::i8);
16940 return DAG.getNode(X86ISD::BRCOND, DL, N->getVTList(), Chain, Dest, Cond,
16941 Flags);
16942 }
16943
Chris Lattnerc19d1c32010-12-19 22:08:31 +000016944 return SDValue();
16945}
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000016946
Benjamin Kramer1396c402011-06-18 11:09:41 +000016947static SDValue PerformSINT_TO_FPCombine(SDNode *N, SelectionDAG &DAG,
16948 const X86TargetLowering *XTLI) {
Stuart Hastingsf99a4b82011-06-06 23:15:58 +000016949 SDValue Op0 = N->getOperand(0);
Nadav Rotema3540772012-04-23 21:53:37 +000016950 EVT InVT = Op0->getValueType(0);
Nadav Rotema3540772012-04-23 21:53:37 +000016951
16952 // SINT_TO_FP(v4i8) -> SINT_TO_FP(SEXT(v4i8 to v4i32))
Craig Topper7fd5e162012-04-24 06:02:29 +000016953 if (InVT == MVT::v8i8 || InVT == MVT::v4i8) {
Nadav Rotema3540772012-04-23 21:53:37 +000016954 DebugLoc dl = N->getDebugLoc();
Craig Topper7fd5e162012-04-24 06:02:29 +000016955 MVT DstVT = InVT == MVT::v4i8 ? MVT::v4i32 : MVT::v8i32;
Nadav Rotema3540772012-04-23 21:53:37 +000016956 SDValue P = DAG.getNode(ISD::SIGN_EXTEND, dl, DstVT, Op0);
16957 return DAG.getNode(ISD::SINT_TO_FP, dl, N->getValueType(0), P);
16958 }
16959
Stuart Hastingsf99a4b82011-06-06 23:15:58 +000016960 // Transform (SINT_TO_FP (i64 ...)) into an x87 operation if we have
16961 // a 32-bit target where SSE doesn't support i64->FP operations.
16962 if (Op0.getOpcode() == ISD::LOAD) {
16963 LoadSDNode *Ld = cast<LoadSDNode>(Op0.getNode());
16964 EVT VT = Ld->getValueType(0);
16965 if (!Ld->isVolatile() && !N->getValueType(0).isVector() &&
16966 ISD::isNON_EXTLoad(Op0.getNode()) && Op0.hasOneUse() &&
16967 !XTLI->getSubtarget()->is64Bit() &&
16968 !DAG.getTargetLoweringInfo().isTypeLegal(VT)) {
Benjamin Kramer1396c402011-06-18 11:09:41 +000016969 SDValue FILDChain = XTLI->BuildFILD(SDValue(N, 0), Ld->getValueType(0),
16970 Ld->getChain(), Op0, DAG);
Stuart Hastingsf99a4b82011-06-06 23:15:58 +000016971 DAG.ReplaceAllUsesOfValueWith(Op0.getValue(1), FILDChain.getValue(1));
16972 return FILDChain;
16973 }
16974 }
16975 return SDValue();
16976}
16977
Chris Lattner23a01992010-12-20 01:37:09 +000016978// Optimize RES, EFLAGS = X86ISD::ADC LHS, RHS, EFLAGS
16979static SDValue PerformADCCombine(SDNode *N, SelectionDAG &DAG,
16980 X86TargetLowering::DAGCombinerInfo &DCI) {
16981 // If the LHS and RHS of the ADC node are zero, then it can't overflow and
16982 // the result is either zero or one (depending on the input carry bit).
16983 // Strength reduce this down to a "set on carry" aka SETCC_CARRY&1.
16984 if (X86::isZeroNode(N->getOperand(0)) &&
16985 X86::isZeroNode(N->getOperand(1)) &&
16986 // We don't have a good way to replace an EFLAGS use, so only do this when
16987 // dead right now.
16988 SDValue(N, 1).use_empty()) {
16989 DebugLoc DL = N->getDebugLoc();
16990 EVT VT = N->getValueType(0);
16991 SDValue CarryOut = DAG.getConstant(0, N->getValueType(1));
16992 SDValue Res1 = DAG.getNode(ISD::AND, DL, VT,
16993 DAG.getNode(X86ISD::SETCC_CARRY, DL, VT,
16994 DAG.getConstant(X86::COND_B,MVT::i8),
16995 N->getOperand(2)),
16996 DAG.getConstant(1, VT));
16997 return DCI.CombineTo(N, Res1, CarryOut);
16998 }
16999
17000 return SDValue();
17001}
17002
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000017003// fold (add Y, (sete X, 0)) -> adc 0, Y
17004// (add Y, (setne X, 0)) -> sbb -1, Y
17005// (sub (sete X, 0), Y) -> sbb 0, Y
17006// (sub (setne X, 0), Y) -> adc -1, Y
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000017007static SDValue OptimizeConditionalInDecrement(SDNode *N, SelectionDAG &DAG) {
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000017008 DebugLoc DL = N->getDebugLoc();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000017009
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000017010 // Look through ZExts.
17011 SDValue Ext = N->getOperand(N->getOpcode() == ISD::SUB ? 1 : 0);
17012 if (Ext.getOpcode() != ISD::ZERO_EXTEND || !Ext.hasOneUse())
17013 return SDValue();
17014
17015 SDValue SetCC = Ext.getOperand(0);
17016 if (SetCC.getOpcode() != X86ISD::SETCC || !SetCC.hasOneUse())
17017 return SDValue();
17018
17019 X86::CondCode CC = (X86::CondCode)SetCC.getConstantOperandVal(0);
17020 if (CC != X86::COND_E && CC != X86::COND_NE)
17021 return SDValue();
17022
17023 SDValue Cmp = SetCC.getOperand(1);
17024 if (Cmp.getOpcode() != X86ISD::CMP || !Cmp.hasOneUse() ||
Chris Lattner9cd3da42011-01-16 02:56:53 +000017025 !X86::isZeroNode(Cmp.getOperand(1)) ||
17026 !Cmp.getOperand(0).getValueType().isInteger())
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000017027 return SDValue();
17028
17029 SDValue CmpOp0 = Cmp.getOperand(0);
17030 SDValue NewCmp = DAG.getNode(X86ISD::CMP, DL, MVT::i32, CmpOp0,
17031 DAG.getConstant(1, CmpOp0.getValueType()));
17032
17033 SDValue OtherVal = N->getOperand(N->getOpcode() == ISD::SUB ? 0 : 1);
17034 if (CC == X86::COND_NE)
17035 return DAG.getNode(N->getOpcode() == ISD::SUB ? X86ISD::ADC : X86ISD::SBB,
17036 DL, OtherVal.getValueType(), OtherVal,
17037 DAG.getConstant(-1ULL, OtherVal.getValueType()), NewCmp);
17038 return DAG.getNode(N->getOpcode() == ISD::SUB ? X86ISD::SBB : X86ISD::ADC,
17039 DL, OtherVal.getValueType(), OtherVal,
17040 DAG.getConstant(0, OtherVal.getValueType()), NewCmp);
17041}
Chris Lattnerc19d1c32010-12-19 22:08:31 +000017042
Craig Topper54f952a2011-11-19 09:02:40 +000017043/// PerformADDCombine - Do target-specific dag combines on integer adds.
17044static SDValue PerformAddCombine(SDNode *N, SelectionDAG &DAG,
17045 const X86Subtarget *Subtarget) {
17046 EVT VT = N->getValueType(0);
17047 SDValue Op0 = N->getOperand(0);
17048 SDValue Op1 = N->getOperand(1);
17049
17050 // Try to synthesize horizontal adds from adds of shuffles.
Craig Topperd0a31172012-01-10 06:37:29 +000017051 if (((Subtarget->hasSSSE3() && (VT == MVT::v8i16 || VT == MVT::v4i32)) ||
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000017052 (Subtarget->hasInt256() && (VT == MVT::v16i16 || VT == MVT::v8i32))) &&
Craig Topper54f952a2011-11-19 09:02:40 +000017053 isHorizontalBinOp(Op0, Op1, true))
17054 return DAG.getNode(X86ISD::HADD, N->getDebugLoc(), VT, Op0, Op1);
17055
17056 return OptimizeConditionalInDecrement(N, DAG);
17057}
17058
17059static SDValue PerformSubCombine(SDNode *N, SelectionDAG &DAG,
17060 const X86Subtarget *Subtarget) {
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000017061 SDValue Op0 = N->getOperand(0);
17062 SDValue Op1 = N->getOperand(1);
17063
17064 // X86 can't encode an immediate LHS of a sub. See if we can push the
17065 // negation into a preceding instruction.
17066 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op0)) {
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000017067 // If the RHS of the sub is a XOR with one use and a constant, invert the
17068 // immediate. Then add one to the LHS of the sub so we can turn
17069 // X-Y -> X+~Y+1, saving one register.
17070 if (Op1->hasOneUse() && Op1.getOpcode() == ISD::XOR &&
17071 isa<ConstantSDNode>(Op1.getOperand(1))) {
Nick Lewycky726ebd62011-08-23 19:01:24 +000017072 APInt XorC = cast<ConstantSDNode>(Op1.getOperand(1))->getAPIntValue();
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000017073 EVT VT = Op0.getValueType();
17074 SDValue NewXor = DAG.getNode(ISD::XOR, Op1.getDebugLoc(), VT,
17075 Op1.getOperand(0),
17076 DAG.getConstant(~XorC, VT));
17077 return DAG.getNode(ISD::ADD, N->getDebugLoc(), VT, NewXor,
Nick Lewycky726ebd62011-08-23 19:01:24 +000017078 DAG.getConstant(C->getAPIntValue()+1, VT));
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000017079 }
17080 }
17081
Craig Topper54f952a2011-11-19 09:02:40 +000017082 // Try to synthesize horizontal adds from adds of shuffles.
17083 EVT VT = N->getValueType(0);
Craig Topperd0a31172012-01-10 06:37:29 +000017084 if (((Subtarget->hasSSSE3() && (VT == MVT::v8i16 || VT == MVT::v4i32)) ||
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000017085 (Subtarget->hasInt256() && (VT == MVT::v16i16 || VT == MVT::v8i32))) &&
Craig Topperb72039c2011-11-30 09:10:50 +000017086 isHorizontalBinOp(Op0, Op1, true))
Craig Topper54f952a2011-11-19 09:02:40 +000017087 return DAG.getNode(X86ISD::HSUB, N->getDebugLoc(), VT, Op0, Op1);
17088
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000017089 return OptimizeConditionalInDecrement(N, DAG);
17090}
17091
Michael Liaod9d09602012-10-23 17:34:00 +000017092/// performVZEXTCombine - Performs build vector combines
17093static SDValue performVZEXTCombine(SDNode *N, SelectionDAG &DAG,
17094 TargetLowering::DAGCombinerInfo &DCI,
17095 const X86Subtarget *Subtarget) {
17096 // (vzext (bitcast (vzext (x)) -> (vzext x)
17097 SDValue In = N->getOperand(0);
17098 while (In.getOpcode() == ISD::BITCAST)
17099 In = In.getOperand(0);
17100
17101 if (In.getOpcode() != X86ISD::VZEXT)
17102 return SDValue();
17103
17104 return DAG.getNode(X86ISD::VZEXT, N->getDebugLoc(), N->getValueType(0), In.getOperand(0));
17105}
17106
Dan Gohman475871a2008-07-27 21:46:04 +000017107SDValue X86TargetLowering::PerformDAGCombine(SDNode *N,
Evan Cheng9dd93b32008-11-05 06:03:38 +000017108 DAGCombinerInfo &DCI) const {
Evan Cheng206ee9d2006-07-07 08:33:52 +000017109 SelectionDAG &DAG = DCI.DAG;
17110 switch (N->getOpcode()) {
17111 default: break;
Dan Gohman1bbf72b2010-03-15 23:23:03 +000017112 case ISD::EXTRACT_VECTOR_ELT:
Craig Topper89f4e662012-03-20 07:17:59 +000017113 return PerformEXTRACT_VECTOR_ELTCombine(N, DAG, DCI);
Duncan Sands6bcd2192011-09-17 16:49:39 +000017114 case ISD::VSELECT:
Nadav Rotemcc616562012-01-15 19:27:55 +000017115 case ISD::SELECT: return PerformSELECTCombine(N, DAG, DCI, Subtarget);
Michael Liaodbf8b5b2012-08-28 03:34:40 +000017116 case X86ISD::CMOV: return PerformCMOVCombine(N, DAG, DCI, Subtarget);
Craig Topper54f952a2011-11-19 09:02:40 +000017117 case ISD::ADD: return PerformAddCombine(N, DAG, Subtarget);
17118 case ISD::SUB: return PerformSubCombine(N, DAG, Subtarget);
Chris Lattner23a01992010-12-20 01:37:09 +000017119 case X86ISD::ADC: return PerformADCCombine(N, DAG, DCI);
Evan Cheng0b0cd912009-03-28 05:57:29 +000017120 case ISD::MUL: return PerformMulCombine(N, DAG, DCI);
Nate Begeman740ab032009-01-26 00:52:55 +000017121 case ISD::SHL:
17122 case ISD::SRA:
Mon P Wang845b1892012-02-01 22:15:20 +000017123 case ISD::SRL: return PerformShiftCombine(N, DAG, DCI, Subtarget);
Nate Begemanb65c1752010-12-17 22:55:37 +000017124 case ISD::AND: return PerformAndCombine(N, DAG, DCI, Subtarget);
Evan Cheng8b1190a2010-04-28 01:18:01 +000017125 case ISD::OR: return PerformOrCombine(N, DAG, DCI, Subtarget);
Craig Topperb4c94572011-10-21 06:55:01 +000017126 case ISD::XOR: return PerformXorCombine(N, DAG, DCI, Subtarget);
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000017127 case ISD::LOAD: return PerformLOADCombine(N, DAG, DCI, Subtarget);
Evan Cheng7e2ff772008-05-08 00:57:18 +000017128 case ISD::STORE: return PerformSTORECombine(N, DAG, Subtarget);
Stuart Hastingsf99a4b82011-06-06 23:15:58 +000017129 case ISD::SINT_TO_FP: return PerformSINT_TO_FPCombine(N, DAG, this);
Duncan Sands17470be2011-09-22 20:15:48 +000017130 case ISD::FADD: return PerformFADDCombine(N, DAG, Subtarget);
17131 case ISD::FSUB: return PerformFSUBCombine(N, DAG, Subtarget);
Chris Lattner6cf73262008-01-25 06:14:17 +000017132 case X86ISD::FXOR:
Chris Lattneraf723b92008-01-25 05:46:26 +000017133 case X86ISD::FOR: return PerformFORCombine(N, DAG);
Nadav Rotemd60cb112012-08-19 13:06:16 +000017134 case X86ISD::FMIN:
17135 case X86ISD::FMAX: return PerformFMinFMaxCombine(N, DAG);
Chris Lattneraf723b92008-01-25 05:46:26 +000017136 case X86ISD::FAND: return PerformFANDCombine(N, DAG);
Dan Gohmane5af2d32009-01-29 01:59:02 +000017137 case X86ISD::BT: return PerformBTCombine(N, DAG, DCI);
Eli Friedman7a5e5552009-06-07 06:52:44 +000017138 case X86ISD::VZEXT_MOVL: return PerformVZEXT_MOVLCombine(N, DAG);
Elena Demikhovsky1da58672012-04-22 09:39:03 +000017139 case ISD::ANY_EXTEND:
Craig Topperc16f8512012-04-25 06:39:39 +000017140 case ISD::ZERO_EXTEND: return PerformZExtCombine(N, DAG, DCI, Subtarget);
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +000017141 case ISD::SIGN_EXTEND: return PerformSExtCombine(N, DAG, DCI, Subtarget);
Craig Topper55b24052012-09-11 06:15:32 +000017142 case ISD::TRUNCATE: return PerformTruncateCombine(N, DAG,DCI,Subtarget);
Chad Rosiera73b6fc2012-04-27 22:33:25 +000017143 case ISD::SETCC: return PerformISDSETCCCombine(N, DAG);
Michael Liaodbf8b5b2012-08-28 03:34:40 +000017144 case X86ISD::SETCC: return PerformSETCCCombine(N, DAG, DCI, Subtarget);
Michael Liao2a33cec2012-08-10 19:58:13 +000017145 case X86ISD::BRCOND: return PerformBrCondCombine(N, DAG, DCI, Subtarget);
Michael Liaod9d09602012-10-23 17:34:00 +000017146 case X86ISD::VZEXT: return performVZEXTCombine(N, DAG, DCI, Subtarget);
Craig Topperb3982da2011-12-31 23:50:21 +000017147 case X86ISD::SHUFP: // Handle all target specific shuffles
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +000017148 case X86ISD::PALIGN:
Craig Topper34671b82011-12-06 08:21:25 +000017149 case X86ISD::UNPCKH:
17150 case X86ISD::UNPCKL:
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000017151 case X86ISD::MOVHLPS:
17152 case X86ISD::MOVLHPS:
17153 case X86ISD::PSHUFD:
17154 case X86ISD::PSHUFHW:
17155 case X86ISD::PSHUFLW:
17156 case X86ISD::MOVSS:
17157 case X86ISD::MOVSD:
Craig Topper316cd2a2011-11-30 06:25:25 +000017158 case X86ISD::VPERMILP:
Craig Topperec24e612011-11-30 07:47:51 +000017159 case X86ISD::VPERM2X128:
Bruno Cardoso Lopes50b37c72011-08-15 21:45:54 +000017160 case ISD::VECTOR_SHUFFLE: return PerformShuffleCombine(N, DAG, DCI,Subtarget);
Elena Demikhovsky1503aba2012-08-01 12:06:00 +000017161 case ISD::FMA: return PerformFMACombine(N, DAG, Subtarget);
Evan Cheng206ee9d2006-07-07 08:33:52 +000017162 }
17163
Dan Gohman475871a2008-07-27 21:46:04 +000017164 return SDValue();
Evan Cheng206ee9d2006-07-07 08:33:52 +000017165}
17166
Evan Chenge5b51ac2010-04-17 06:13:15 +000017167/// isTypeDesirableForOp - Return true if the target has native support for
17168/// the specified value type and it is 'desirable' to use the type for the
17169/// given node type. e.g. On x86 i16 is legal, but undesirable since i16
17170/// instruction encodings are longer and some i16 instructions are slow.
17171bool X86TargetLowering::isTypeDesirableForOp(unsigned Opc, EVT VT) const {
17172 if (!isTypeLegal(VT))
17173 return false;
Evan Cheng2bce5f4b2010-04-28 08:30:49 +000017174 if (VT != MVT::i16)
Evan Chenge5b51ac2010-04-17 06:13:15 +000017175 return true;
17176
17177 switch (Opc) {
17178 default:
17179 return true;
Evan Cheng4c26e932010-04-19 19:29:22 +000017180 case ISD::LOAD:
17181 case ISD::SIGN_EXTEND:
17182 case ISD::ZERO_EXTEND:
17183 case ISD::ANY_EXTEND:
Evan Chenge5b51ac2010-04-17 06:13:15 +000017184 case ISD::SHL:
Evan Chenge5b51ac2010-04-17 06:13:15 +000017185 case ISD::SRL:
17186 case ISD::SUB:
17187 case ISD::ADD:
17188 case ISD::MUL:
17189 case ISD::AND:
17190 case ISD::OR:
17191 case ISD::XOR:
17192 return false;
17193 }
17194}
17195
17196/// IsDesirableToPromoteOp - This method query the target whether it is
Evan Cheng64b7bf72010-04-16 06:14:10 +000017197/// beneficial for dag combiner to promote the specified node. If true, it
17198/// should return the desired promotion type by reference.
Evan Chenge5b51ac2010-04-17 06:13:15 +000017199bool X86TargetLowering::IsDesirableToPromoteOp(SDValue Op, EVT &PVT) const {
Evan Cheng64b7bf72010-04-16 06:14:10 +000017200 EVT VT = Op.getValueType();
17201 if (VT != MVT::i16)
17202 return false;
17203
Evan Cheng4c26e932010-04-19 19:29:22 +000017204 bool Promote = false;
17205 bool Commute = false;
Evan Cheng64b7bf72010-04-16 06:14:10 +000017206 switch (Op.getOpcode()) {
Evan Cheng4c26e932010-04-19 19:29:22 +000017207 default: break;
17208 case ISD::LOAD: {
17209 LoadSDNode *LD = cast<LoadSDNode>(Op);
17210 // If the non-extending load has a single use and it's not live out, then it
17211 // might be folded.
Evan Cheng2bce5f4b2010-04-28 08:30:49 +000017212 if (LD->getExtensionType() == ISD::NON_EXTLOAD /*&&
17213 Op.hasOneUse()*/) {
17214 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
17215 UE = Op.getNode()->use_end(); UI != UE; ++UI) {
17216 // The only case where we'd want to promote LOAD (rather then it being
17217 // promoted as an operand is when it's only use is liveout.
17218 if (UI->getOpcode() != ISD::CopyToReg)
17219 return false;
17220 }
17221 }
Evan Cheng4c26e932010-04-19 19:29:22 +000017222 Promote = true;
17223 break;
17224 }
17225 case ISD::SIGN_EXTEND:
17226 case ISD::ZERO_EXTEND:
17227 case ISD::ANY_EXTEND:
17228 Promote = true;
17229 break;
Evan Chenge5b51ac2010-04-17 06:13:15 +000017230 case ISD::SHL:
Evan Cheng2bce5f4b2010-04-28 08:30:49 +000017231 case ISD::SRL: {
Evan Chenge5b51ac2010-04-17 06:13:15 +000017232 SDValue N0 = Op.getOperand(0);
17233 // Look out for (store (shl (load), x)).
Evan Chengc82c20b2010-04-24 04:44:57 +000017234 if (MayFoldLoad(N0) && MayFoldIntoStore(Op))
Evan Chenge5b51ac2010-04-17 06:13:15 +000017235 return false;
Evan Cheng4c26e932010-04-19 19:29:22 +000017236 Promote = true;
Evan Chenge5b51ac2010-04-17 06:13:15 +000017237 break;
17238 }
Evan Cheng64b7bf72010-04-16 06:14:10 +000017239 case ISD::ADD:
17240 case ISD::MUL:
17241 case ISD::AND:
17242 case ISD::OR:
Evan Cheng4c26e932010-04-19 19:29:22 +000017243 case ISD::XOR:
17244 Commute = true;
17245 // fallthrough
17246 case ISD::SUB: {
Evan Cheng64b7bf72010-04-16 06:14:10 +000017247 SDValue N0 = Op.getOperand(0);
17248 SDValue N1 = Op.getOperand(1);
Evan Chengc82c20b2010-04-24 04:44:57 +000017249 if (!Commute && MayFoldLoad(N1))
Evan Cheng64b7bf72010-04-16 06:14:10 +000017250 return false;
17251 // Avoid disabling potential load folding opportunities.
Evan Chengc82c20b2010-04-24 04:44:57 +000017252 if (MayFoldLoad(N0) && (!isa<ConstantSDNode>(N1) || MayFoldIntoStore(Op)))
Evan Cheng64b7bf72010-04-16 06:14:10 +000017253 return false;
Evan Chengc82c20b2010-04-24 04:44:57 +000017254 if (MayFoldLoad(N1) && (!isa<ConstantSDNode>(N0) || MayFoldIntoStore(Op)))
Evan Cheng64b7bf72010-04-16 06:14:10 +000017255 return false;
Evan Cheng4c26e932010-04-19 19:29:22 +000017256 Promote = true;
Evan Cheng64b7bf72010-04-16 06:14:10 +000017257 }
17258 }
17259
17260 PVT = MVT::i32;
Evan Cheng4c26e932010-04-19 19:29:22 +000017261 return Promote;
Evan Cheng64b7bf72010-04-16 06:14:10 +000017262}
17263
Evan Cheng60c07e12006-07-05 22:17:51 +000017264//===----------------------------------------------------------------------===//
17265// X86 Inline Assembly Support
17266//===----------------------------------------------------------------------===//
17267
Benjamin Kramer2ea4cdb2011-12-18 19:59:20 +000017268namespace {
17269 // Helper to match a string separated by whitespace.
Benjamin Kramer0581ed72011-12-18 20:51:31 +000017270 bool matchAsmImpl(StringRef s, ArrayRef<const StringRef *> args) {
Benjamin Kramer2ea4cdb2011-12-18 19:59:20 +000017271 s = s.substr(s.find_first_not_of(" \t")); // Skip leading whitespace.
Benjamin Kramere6cddb72011-12-17 14:36:05 +000017272
Benjamin Kramer0581ed72011-12-18 20:51:31 +000017273 for (unsigned i = 0, e = args.size(); i != e; ++i) {
Benjamin Kramer2ea4cdb2011-12-18 19:59:20 +000017274 StringRef piece(*args[i]);
17275 if (!s.startswith(piece)) // Check if the piece matches.
17276 return false;
17277
17278 s = s.substr(piece.size());
17279 StringRef::size_type pos = s.find_first_not_of(" \t");
17280 if (pos == 0) // We matched a prefix.
17281 return false;
17282
17283 s = s.substr(pos);
Benjamin Kramere6cddb72011-12-17 14:36:05 +000017284 }
17285
Benjamin Kramer2ea4cdb2011-12-18 19:59:20 +000017286 return s.empty();
Benjamin Kramere6cddb72011-12-17 14:36:05 +000017287 }
Benjamin Kramer0581ed72011-12-18 20:51:31 +000017288 const VariadicFunction1<bool, StringRef, StringRef, matchAsmImpl> matchAsm={};
Benjamin Kramere6cddb72011-12-17 14:36:05 +000017289}
17290
Chris Lattnerb8105652009-07-20 17:51:36 +000017291bool X86TargetLowering::ExpandInlineAsm(CallInst *CI) const {
17292 InlineAsm *IA = cast<InlineAsm>(CI->getCalledValue());
Chris Lattnerb8105652009-07-20 17:51:36 +000017293
17294 std::string AsmStr = IA->getAsmString();
17295
Benjamin Kramere6cddb72011-12-17 14:36:05 +000017296 IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
17297 if (!Ty || Ty->getBitWidth() % 16 != 0)
17298 return false;
17299
Chris Lattnerb8105652009-07-20 17:51:36 +000017300 // TODO: should remove alternatives from the asmstring: "foo {a|b}" -> "foo a"
Benjamin Kramerd4f19592010-01-11 18:03:24 +000017301 SmallVector<StringRef, 4> AsmPieces;
Peter Collingbourne98361182010-11-13 19:54:23 +000017302 SplitString(AsmStr, AsmPieces, ";\n");
Chris Lattnerb8105652009-07-20 17:51:36 +000017303
17304 switch (AsmPieces.size()) {
17305 default: return false;
17306 case 1:
Chris Lattner7a2bdde2011-04-15 05:18:47 +000017307 // FIXME: this should verify that we are targeting a 486 or better. If not,
Benjamin Kramere6cddb72011-12-17 14:36:05 +000017308 // we will turn this bswap into something that will be lowered to logical
17309 // ops instead of emitting the bswap asm. For now, we don't support 486 or
17310 // lower so don't worry about this.
Chris Lattnerb8105652009-07-20 17:51:36 +000017311 // bswap $0
Benjamin Kramer2ea4cdb2011-12-18 19:59:20 +000017312 if (matchAsm(AsmPieces[0], "bswap", "$0") ||
17313 matchAsm(AsmPieces[0], "bswapl", "$0") ||
17314 matchAsm(AsmPieces[0], "bswapq", "$0") ||
17315 matchAsm(AsmPieces[0], "bswap", "${0:q}") ||
17316 matchAsm(AsmPieces[0], "bswapl", "${0:q}") ||
17317 matchAsm(AsmPieces[0], "bswapq", "${0:q}")) {
Chris Lattnerb8105652009-07-20 17:51:36 +000017318 // No need to check constraints, nothing other than the equivalent of
17319 // "=r,0" would be valid here.
Evan Cheng55d42002011-01-08 01:24:27 +000017320 return IntrinsicLowering::LowerToByteSwap(CI);
Chris Lattnerb8105652009-07-20 17:51:36 +000017321 }
Benjamin Kramere6cddb72011-12-17 14:36:05 +000017322
Chris Lattnerb8105652009-07-20 17:51:36 +000017323 // rorw $$8, ${0:w} --> llvm.bswap.i16
Duncan Sandsb0bc6c32010-02-15 16:12:20 +000017324 if (CI->getType()->isIntegerTy(16) &&
Benjamin Kramere6cddb72011-12-17 14:36:05 +000017325 IA->getConstraintString().compare(0, 5, "=r,0,") == 0 &&
Benjamin Kramer2ea4cdb2011-12-18 19:59:20 +000017326 (matchAsm(AsmPieces[0], "rorw", "$$8,", "${0:w}") ||
17327 matchAsm(AsmPieces[0], "rolw", "$$8,", "${0:w}"))) {
Dan Gohman0ef701e2010-03-04 19:58:08 +000017328 AsmPieces.clear();
Evan Cheng55d42002011-01-08 01:24:27 +000017329 const std::string &ConstraintsStr = IA->getConstraintString();
17330 SplitString(StringRef(ConstraintsStr).substr(5), AsmPieces, ",");
Dan Gohman0ef701e2010-03-04 19:58:08 +000017331 std::sort(AsmPieces.begin(), AsmPieces.end());
17332 if (AsmPieces.size() == 4 &&
17333 AsmPieces[0] == "~{cc}" &&
17334 AsmPieces[1] == "~{dirflag}" &&
17335 AsmPieces[2] == "~{flags}" &&
Benjamin Kramere6cddb72011-12-17 14:36:05 +000017336 AsmPieces[3] == "~{fpsr}")
17337 return IntrinsicLowering::LowerToByteSwap(CI);
Chris Lattnerb8105652009-07-20 17:51:36 +000017338 }
17339 break;
17340 case 3:
Peter Collingbourne948cf022010-11-13 19:54:30 +000017341 if (CI->getType()->isIntegerTy(32) &&
Benjamin Kramere6cddb72011-12-17 14:36:05 +000017342 IA->getConstraintString().compare(0, 5, "=r,0,") == 0 &&
Benjamin Kramer2ea4cdb2011-12-18 19:59:20 +000017343 matchAsm(AsmPieces[0], "rorw", "$$8,", "${0:w}") &&
17344 matchAsm(AsmPieces[1], "rorl", "$$16,", "$0") &&
17345 matchAsm(AsmPieces[2], "rorw", "$$8,", "${0:w}")) {
Benjamin Kramere6cddb72011-12-17 14:36:05 +000017346 AsmPieces.clear();
17347 const std::string &ConstraintsStr = IA->getConstraintString();
17348 SplitString(StringRef(ConstraintsStr).substr(5), AsmPieces, ",");
17349 std::sort(AsmPieces.begin(), AsmPieces.end());
17350 if (AsmPieces.size() == 4 &&
17351 AsmPieces[0] == "~{cc}" &&
17352 AsmPieces[1] == "~{dirflag}" &&
17353 AsmPieces[2] == "~{flags}" &&
17354 AsmPieces[3] == "~{fpsr}")
17355 return IntrinsicLowering::LowerToByteSwap(CI);
Peter Collingbourne948cf022010-11-13 19:54:30 +000017356 }
Evan Cheng55d42002011-01-08 01:24:27 +000017357
17358 if (CI->getType()->isIntegerTy(64)) {
17359 InlineAsm::ConstraintInfoVector Constraints = IA->ParseConstraints();
17360 if (Constraints.size() >= 2 &&
17361 Constraints[0].Codes.size() == 1 && Constraints[0].Codes[0] == "A" &&
17362 Constraints[1].Codes.size() == 1 && Constraints[1].Codes[0] == "0") {
17363 // bswap %eax / bswap %edx / xchgl %eax, %edx -> llvm.bswap.i64
Benjamin Kramer2ea4cdb2011-12-18 19:59:20 +000017364 if (matchAsm(AsmPieces[0], "bswap", "%eax") &&
17365 matchAsm(AsmPieces[1], "bswap", "%edx") &&
17366 matchAsm(AsmPieces[2], "xchgl", "%eax,", "%edx"))
Benjamin Kramere6cddb72011-12-17 14:36:05 +000017367 return IntrinsicLowering::LowerToByteSwap(CI);
Chris Lattnerb8105652009-07-20 17:51:36 +000017368 }
17369 }
17370 break;
17371 }
17372 return false;
17373}
17374
Chris Lattnerf4dff842006-07-11 02:54:03 +000017375/// getConstraintType - Given a constraint letter, return the type of
17376/// constraint it is for this target.
17377X86TargetLowering::ConstraintType
Chris Lattner4234f572007-03-25 02:14:49 +000017378X86TargetLowering::getConstraintType(const std::string &Constraint) const {
17379 if (Constraint.size() == 1) {
17380 switch (Constraint[0]) {
Chris Lattner4234f572007-03-25 02:14:49 +000017381 case 'R':
Chris Lattner4234f572007-03-25 02:14:49 +000017382 case 'q':
17383 case 'Q':
John Thompson44ab89e2010-10-29 17:29:13 +000017384 case 'f':
17385 case 't':
17386 case 'u':
Dale Johannesen2ffbcac2008-04-01 00:57:48 +000017387 case 'y':
John Thompson44ab89e2010-10-29 17:29:13 +000017388 case 'x':
Chris Lattner4234f572007-03-25 02:14:49 +000017389 case 'Y':
Eric Christopher31b5f002011-07-07 22:29:07 +000017390 case 'l':
Chris Lattner4234f572007-03-25 02:14:49 +000017391 return C_RegisterClass;
John Thompson44ab89e2010-10-29 17:29:13 +000017392 case 'a':
17393 case 'b':
17394 case 'c':
17395 case 'd':
17396 case 'S':
17397 case 'D':
17398 case 'A':
17399 return C_Register;
17400 case 'I':
17401 case 'J':
17402 case 'K':
17403 case 'L':
17404 case 'M':
17405 case 'N':
17406 case 'G':
17407 case 'C':
Dale Johannesen78e3e522009-02-12 20:58:09 +000017408 case 'e':
17409 case 'Z':
17410 return C_Other;
Chris Lattner4234f572007-03-25 02:14:49 +000017411 default:
17412 break;
17413 }
Chris Lattnerf4dff842006-07-11 02:54:03 +000017414 }
Chris Lattner4234f572007-03-25 02:14:49 +000017415 return TargetLowering::getConstraintType(Constraint);
Chris Lattnerf4dff842006-07-11 02:54:03 +000017416}
17417
John Thompson44ab89e2010-10-29 17:29:13 +000017418/// Examine constraint type and operand type and determine a weight value.
John Thompsoneac6e1d2010-09-13 18:15:37 +000017419/// This object must already have been set up with the operand type
17420/// and the current alternative constraint selected.
John Thompson44ab89e2010-10-29 17:29:13 +000017421TargetLowering::ConstraintWeight
17422 X86TargetLowering::getSingleConstraintMatchWeight(
John Thompsoneac6e1d2010-09-13 18:15:37 +000017423 AsmOperandInfo &info, const char *constraint) const {
John Thompson44ab89e2010-10-29 17:29:13 +000017424 ConstraintWeight weight = CW_Invalid;
John Thompsoneac6e1d2010-09-13 18:15:37 +000017425 Value *CallOperandVal = info.CallOperandVal;
17426 // If we don't have a value, we can't do a match,
17427 // but allow it at the lowest weight.
17428 if (CallOperandVal == NULL)
John Thompson44ab89e2010-10-29 17:29:13 +000017429 return CW_Default;
Chris Lattnerdb125cf2011-07-18 04:54:35 +000017430 Type *type = CallOperandVal->getType();
John Thompsoneac6e1d2010-09-13 18:15:37 +000017431 // Look at the constraint type.
17432 switch (*constraint) {
17433 default:
John Thompson44ab89e2010-10-29 17:29:13 +000017434 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
17435 case 'R':
17436 case 'q':
17437 case 'Q':
17438 case 'a':
17439 case 'b':
17440 case 'c':
17441 case 'd':
17442 case 'S':
17443 case 'D':
17444 case 'A':
17445 if (CallOperandVal->getType()->isIntegerTy())
17446 weight = CW_SpecificReg;
17447 break;
17448 case 'f':
17449 case 't':
17450 case 'u':
17451 if (type->isFloatingPointTy())
17452 weight = CW_SpecificReg;
17453 break;
17454 case 'y':
Chris Lattner2a786eb2010-12-19 20:19:20 +000017455 if (type->isX86_MMXTy() && Subtarget->hasMMX())
John Thompson44ab89e2010-10-29 17:29:13 +000017456 weight = CW_SpecificReg;
17457 break;
17458 case 'x':
17459 case 'Y':
Craig Topper1accb7e2012-01-10 06:54:16 +000017460 if (((type->getPrimitiveSizeInBits() == 128) && Subtarget->hasSSE1()) ||
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000017461 ((type->getPrimitiveSizeInBits() == 256) && Subtarget->hasFp256()))
John Thompson44ab89e2010-10-29 17:29:13 +000017462 weight = CW_Register;
John Thompsoneac6e1d2010-09-13 18:15:37 +000017463 break;
17464 case 'I':
17465 if (ConstantInt *C = dyn_cast<ConstantInt>(info.CallOperandVal)) {
17466 if (C->getZExtValue() <= 31)
John Thompson44ab89e2010-10-29 17:29:13 +000017467 weight = CW_Constant;
John Thompsoneac6e1d2010-09-13 18:15:37 +000017468 }
17469 break;
John Thompson44ab89e2010-10-29 17:29:13 +000017470 case 'J':
17471 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
17472 if (C->getZExtValue() <= 63)
17473 weight = CW_Constant;
17474 }
17475 break;
17476 case 'K':
17477 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
17478 if ((C->getSExtValue() >= -0x80) && (C->getSExtValue() <= 0x7f))
17479 weight = CW_Constant;
17480 }
17481 break;
17482 case 'L':
17483 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
17484 if ((C->getZExtValue() == 0xff) || (C->getZExtValue() == 0xffff))
17485 weight = CW_Constant;
17486 }
17487 break;
17488 case 'M':
17489 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
17490 if (C->getZExtValue() <= 3)
17491 weight = CW_Constant;
17492 }
17493 break;
17494 case 'N':
17495 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
17496 if (C->getZExtValue() <= 0xff)
17497 weight = CW_Constant;
17498 }
17499 break;
17500 case 'G':
17501 case 'C':
17502 if (dyn_cast<ConstantFP>(CallOperandVal)) {
17503 weight = CW_Constant;
17504 }
17505 break;
17506 case 'e':
17507 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
17508 if ((C->getSExtValue() >= -0x80000000LL) &&
17509 (C->getSExtValue() <= 0x7fffffffLL))
17510 weight = CW_Constant;
17511 }
17512 break;
17513 case 'Z':
17514 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
17515 if (C->getZExtValue() <= 0xffffffff)
17516 weight = CW_Constant;
17517 }
17518 break;
John Thompsoneac6e1d2010-09-13 18:15:37 +000017519 }
17520 return weight;
17521}
17522
Dale Johannesenba2a0b92008-01-29 02:21:21 +000017523/// LowerXConstraint - try to replace an X constraint, which matches anything,
17524/// with another that has more specific requirements based on the type of the
17525/// corresponding operand.
Chris Lattner5e764232008-04-26 23:02:14 +000017526const char *X86TargetLowering::
Owen Andersone50ed302009-08-10 22:56:29 +000017527LowerXConstraint(EVT ConstraintVT) const {
Chris Lattner5e764232008-04-26 23:02:14 +000017528 // FP X constraints get lowered to SSE1/2 registers if available, otherwise
17529 // 'f' like normal targets.
Duncan Sands83ec4b62008-06-06 12:08:01 +000017530 if (ConstraintVT.isFloatingPoint()) {
Craig Topper1accb7e2012-01-10 06:54:16 +000017531 if (Subtarget->hasSSE2())
Chris Lattner5e764232008-04-26 23:02:14 +000017532 return "Y";
Craig Topper1accb7e2012-01-10 06:54:16 +000017533 if (Subtarget->hasSSE1())
Chris Lattner5e764232008-04-26 23:02:14 +000017534 return "x";
17535 }
Scott Michelfdc40a02009-02-17 22:15:04 +000017536
Chris Lattner5e764232008-04-26 23:02:14 +000017537 return TargetLowering::LowerXConstraint(ConstraintVT);
Dale Johannesenba2a0b92008-01-29 02:21:21 +000017538}
17539
Chris Lattner48884cd2007-08-25 00:47:38 +000017540/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
17541/// vector. If it is invalid, don't add anything to Ops.
Dan Gohman475871a2008-07-27 21:46:04 +000017542void X86TargetLowering::LowerAsmOperandForConstraint(SDValue Op,
Eric Christopher100c8332011-06-02 23:16:42 +000017543 std::string &Constraint,
Dan Gohman475871a2008-07-27 21:46:04 +000017544 std::vector<SDValue>&Ops,
Chris Lattner5e764232008-04-26 23:02:14 +000017545 SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +000017546 SDValue Result(0, 0);
Scott Michelfdc40a02009-02-17 22:15:04 +000017547
Eric Christopher100c8332011-06-02 23:16:42 +000017548 // Only support length 1 constraints for now.
17549 if (Constraint.length() > 1) return;
Eric Christopher471e4222011-06-08 23:55:35 +000017550
Eric Christopher100c8332011-06-02 23:16:42 +000017551 char ConstraintLetter = Constraint[0];
17552 switch (ConstraintLetter) {
Chris Lattner22aaf1d2006-10-31 20:13:11 +000017553 default: break;
Devang Patel84f7fd22007-03-17 00:13:28 +000017554 case 'I':
Chris Lattner188b9fe2007-03-25 01:57:35 +000017555 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +000017556 if (C->getZExtValue() <= 31) {
17557 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattner48884cd2007-08-25 00:47:38 +000017558 break;
17559 }
Devang Patel84f7fd22007-03-17 00:13:28 +000017560 }
Chris Lattner48884cd2007-08-25 00:47:38 +000017561 return;
Evan Cheng364091e2008-09-22 23:57:37 +000017562 case 'J':
17563 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattner2e06dd22009-06-15 04:39:05 +000017564 if (C->getZExtValue() <= 63) {
Chris Lattnere4935152009-06-15 04:01:39 +000017565 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
17566 break;
17567 }
17568 }
17569 return;
17570 case 'K':
17571 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Jakub Staszakdccd7f92012-11-06 23:52:19 +000017572 if (isInt<8>(C->getSExtValue())) {
Evan Cheng364091e2008-09-22 23:57:37 +000017573 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
17574 break;
17575 }
17576 }
17577 return;
Chris Lattner188b9fe2007-03-25 01:57:35 +000017578 case 'N':
17579 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +000017580 if (C->getZExtValue() <= 255) {
17581 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattner48884cd2007-08-25 00:47:38 +000017582 break;
17583 }
Chris Lattner188b9fe2007-03-25 01:57:35 +000017584 }
Chris Lattner48884cd2007-08-25 00:47:38 +000017585 return;
Dale Johannesen78e3e522009-02-12 20:58:09 +000017586 case 'e': {
17587 // 32-bit signed value
17588 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohman7720cb32010-06-18 14:01:07 +000017589 if (ConstantInt::isValueValidForType(Type::getInt32Ty(*DAG.getContext()),
17590 C->getSExtValue())) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000017591 // Widen to 64 bits here to get it sign extended.
Owen Anderson825b72b2009-08-11 20:47:22 +000017592 Result = DAG.getTargetConstant(C->getSExtValue(), MVT::i64);
Dale Johannesen78e3e522009-02-12 20:58:09 +000017593 break;
17594 }
17595 // FIXME gcc accepts some relocatable values here too, but only in certain
17596 // memory models; it's complicated.
17597 }
17598 return;
17599 }
17600 case 'Z': {
17601 // 32-bit unsigned value
17602 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohman7720cb32010-06-18 14:01:07 +000017603 if (ConstantInt::isValueValidForType(Type::getInt32Ty(*DAG.getContext()),
17604 C->getZExtValue())) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000017605 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
17606 break;
17607 }
17608 }
17609 // FIXME gcc accepts some relocatable values here too, but only in certain
17610 // memory models; it's complicated.
17611 return;
17612 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000017613 case 'i': {
Chris Lattner22aaf1d2006-10-31 20:13:11 +000017614 // Literal immediates are always ok.
Chris Lattner48884cd2007-08-25 00:47:38 +000017615 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Op)) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000017616 // Widen to 64 bits here to get it sign extended.
Owen Anderson825b72b2009-08-11 20:47:22 +000017617 Result = DAG.getTargetConstant(CST->getSExtValue(), MVT::i64);
Chris Lattner48884cd2007-08-25 00:47:38 +000017618 break;
17619 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000017620
Dale Johannesene5ff9ef2010-06-24 20:14:51 +000017621 // In any sort of PIC mode addresses need to be computed at runtime by
17622 // adding in a register or some sort of table lookup. These can't
17623 // be used as immediates.
Dale Johannesene2b448c2010-07-06 23:27:00 +000017624 if (Subtarget->isPICStyleGOT() || Subtarget->isPICStyleStubPIC())
Dale Johannesene5ff9ef2010-06-24 20:14:51 +000017625 return;
17626
Chris Lattnerdc43a882007-05-03 16:52:29 +000017627 // If we are in non-pic codegen mode, we allow the address of a global (with
17628 // an optional displacement) to be used with 'i'.
Chris Lattner49921962009-05-08 18:23:14 +000017629 GlobalAddressSDNode *GA = 0;
Chris Lattnerdc43a882007-05-03 16:52:29 +000017630 int64_t Offset = 0;
Scott Michelfdc40a02009-02-17 22:15:04 +000017631
Chris Lattner49921962009-05-08 18:23:14 +000017632 // Match either (GA), (GA+C), (GA+C1+C2), etc.
17633 while (1) {
17634 if ((GA = dyn_cast<GlobalAddressSDNode>(Op))) {
17635 Offset += GA->getOffset();
17636 break;
17637 } else if (Op.getOpcode() == ISD::ADD) {
17638 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
17639 Offset += C->getZExtValue();
17640 Op = Op.getOperand(0);
17641 continue;
17642 }
17643 } else if (Op.getOpcode() == ISD::SUB) {
17644 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
17645 Offset += -C->getZExtValue();
17646 Op = Op.getOperand(0);
17647 continue;
17648 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000017649 }
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000017650
Chris Lattner49921962009-05-08 18:23:14 +000017651 // Otherwise, this isn't something we can handle, reject it.
17652 return;
Chris Lattnerdc43a882007-05-03 16:52:29 +000017653 }
Eric Christopherfd179292009-08-27 18:07:15 +000017654
Dan Gohman46510a72010-04-15 01:51:59 +000017655 const GlobalValue *GV = GA->getGlobal();
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000017656 // If we require an extra load to get this address, as in PIC mode, we
17657 // can't accept it.
Chris Lattner36c25012009-07-10 07:34:39 +000017658 if (isGlobalStubReference(Subtarget->ClassifyGlobalReference(GV,
17659 getTargetMachine())))
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000017660 return;
Scott Michelfdc40a02009-02-17 22:15:04 +000017661
Devang Patel0d881da2010-07-06 22:08:15 +000017662 Result = DAG.getTargetGlobalAddress(GV, Op.getDebugLoc(),
17663 GA->getValueType(0), Offset);
Chris Lattner49921962009-05-08 18:23:14 +000017664 break;
Chris Lattner22aaf1d2006-10-31 20:13:11 +000017665 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000017666 }
Scott Michelfdc40a02009-02-17 22:15:04 +000017667
Gabor Greifba36cb52008-08-28 21:40:38 +000017668 if (Result.getNode()) {
Chris Lattner48884cd2007-08-25 00:47:38 +000017669 Ops.push_back(Result);
17670 return;
17671 }
Dale Johannesen1784d162010-06-25 21:55:36 +000017672 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
Chris Lattner22aaf1d2006-10-31 20:13:11 +000017673}
17674
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000017675std::pair<unsigned, const TargetRegisterClass*>
Chris Lattnerf76d1802006-07-31 23:26:50 +000017676X86TargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +000017677 EVT VT) const {
Chris Lattnerad043e82007-04-09 05:11:28 +000017678 // First, see if this is a constraint that directly corresponds to an LLVM
17679 // register class.
17680 if (Constraint.size() == 1) {
17681 // GCC Constraint Letters
17682 switch (Constraint[0]) {
17683 default: break;
Eric Christopherd176af82011-06-29 17:23:50 +000017684 // TODO: Slight differences here in allocation order and leaving
17685 // RIP in the class. Do they matter any more here than they do
17686 // in the normal allocation?
17687 case 'q': // GENERAL_REGS in 64-bit mode, Q_REGS in 32-bit mode.
17688 if (Subtarget->is64Bit()) {
Craig Topperc9099502012-04-20 06:31:50 +000017689 if (VT == MVT::i32 || VT == MVT::f32)
17690 return std::make_pair(0U, &X86::GR32RegClass);
17691 if (VT == MVT::i16)
17692 return std::make_pair(0U, &X86::GR16RegClass);
17693 if (VT == MVT::i8 || VT == MVT::i1)
17694 return std::make_pair(0U, &X86::GR8RegClass);
17695 if (VT == MVT::i64 || VT == MVT::f64)
17696 return std::make_pair(0U, &X86::GR64RegClass);
17697 break;
Eric Christopherd176af82011-06-29 17:23:50 +000017698 }
17699 // 32-bit fallthrough
17700 case 'Q': // Q_REGS
Nick Lewycky9bf45d02011-07-08 00:19:27 +000017701 if (VT == MVT::i32 || VT == MVT::f32)
Craig Topperc9099502012-04-20 06:31:50 +000017702 return std::make_pair(0U, &X86::GR32_ABCDRegClass);
17703 if (VT == MVT::i16)
17704 return std::make_pair(0U, &X86::GR16_ABCDRegClass);
17705 if (VT == MVT::i8 || VT == MVT::i1)
17706 return std::make_pair(0U, &X86::GR8_ABCD_LRegClass);
17707 if (VT == MVT::i64)
17708 return std::make_pair(0U, &X86::GR64_ABCDRegClass);
Eric Christopherd176af82011-06-29 17:23:50 +000017709 break;
Chris Lattner0f65cad2007-04-09 05:49:22 +000017710 case 'r': // GENERAL_REGS
Chris Lattner0f65cad2007-04-09 05:49:22 +000017711 case 'l': // INDEX_REGS
Eric Christopher5427ede2011-07-14 20:13:52 +000017712 if (VT == MVT::i8 || VT == MVT::i1)
Craig Topperc9099502012-04-20 06:31:50 +000017713 return std::make_pair(0U, &X86::GR8RegClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000017714 if (VT == MVT::i16)
Craig Topperc9099502012-04-20 06:31:50 +000017715 return std::make_pair(0U, &X86::GR16RegClass);
Eric Christopher2bbecd82011-05-19 21:33:47 +000017716 if (VT == MVT::i32 || VT == MVT::f32 || !Subtarget->is64Bit())
Craig Topperc9099502012-04-20 06:31:50 +000017717 return std::make_pair(0U, &X86::GR32RegClass);
17718 return std::make_pair(0U, &X86::GR64RegClass);
Dale Johannesen5f3663e2009-10-07 22:47:20 +000017719 case 'R': // LEGACY_REGS
Eric Christopher5427ede2011-07-14 20:13:52 +000017720 if (VT == MVT::i8 || VT == MVT::i1)
Craig Topperc9099502012-04-20 06:31:50 +000017721 return std::make_pair(0U, &X86::GR8_NOREXRegClass);
Dale Johannesen5f3663e2009-10-07 22:47:20 +000017722 if (VT == MVT::i16)
Craig Topperc9099502012-04-20 06:31:50 +000017723 return std::make_pair(0U, &X86::GR16_NOREXRegClass);
Dale Johannesen5f3663e2009-10-07 22:47:20 +000017724 if (VT == MVT::i32 || !Subtarget->is64Bit())
Craig Topperc9099502012-04-20 06:31:50 +000017725 return std::make_pair(0U, &X86::GR32_NOREXRegClass);
17726 return std::make_pair(0U, &X86::GR64_NOREXRegClass);
Chris Lattnerfce84ac2008-03-11 19:06:29 +000017727 case 'f': // FP Stack registers.
17728 // If SSE is enabled for this VT, use f80 to ensure the isel moves the
17729 // value to the correct fpstack register class.
Owen Anderson825b72b2009-08-11 20:47:22 +000017730 if (VT == MVT::f32 && !isScalarFPTypeInSSEReg(VT))
Craig Topperc9099502012-04-20 06:31:50 +000017731 return std::make_pair(0U, &X86::RFP32RegClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000017732 if (VT == MVT::f64 && !isScalarFPTypeInSSEReg(VT))
Craig Topperc9099502012-04-20 06:31:50 +000017733 return std::make_pair(0U, &X86::RFP64RegClass);
17734 return std::make_pair(0U, &X86::RFP80RegClass);
Chris Lattner6c284d72007-04-12 04:14:49 +000017735 case 'y': // MMX_REGS if MMX allowed.
17736 if (!Subtarget->hasMMX()) break;
Craig Topperc9099502012-04-20 06:31:50 +000017737 return std::make_pair(0U, &X86::VR64RegClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +000017738 case 'Y': // SSE_REGS if SSE2 allowed
Craig Topper1accb7e2012-01-10 06:54:16 +000017739 if (!Subtarget->hasSSE2()) break;
Chris Lattner0f65cad2007-04-09 05:49:22 +000017740 // FALL THROUGH.
Eric Christopher55487552012-01-07 01:02:09 +000017741 case 'x': // SSE_REGS if SSE1 allowed or AVX_REGS if AVX allowed
Craig Topper1accb7e2012-01-10 06:54:16 +000017742 if (!Subtarget->hasSSE1()) break;
Duncan Sands83ec4b62008-06-06 12:08:01 +000017743
Owen Anderson825b72b2009-08-11 20:47:22 +000017744 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner0f65cad2007-04-09 05:49:22 +000017745 default: break;
17746 // Scalar SSE types.
Owen Anderson825b72b2009-08-11 20:47:22 +000017747 case MVT::f32:
17748 case MVT::i32:
Craig Topperc9099502012-04-20 06:31:50 +000017749 return std::make_pair(0U, &X86::FR32RegClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000017750 case MVT::f64:
17751 case MVT::i64:
Craig Topperc9099502012-04-20 06:31:50 +000017752 return std::make_pair(0U, &X86::FR64RegClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +000017753 // Vector types.
Owen Anderson825b72b2009-08-11 20:47:22 +000017754 case MVT::v16i8:
17755 case MVT::v8i16:
17756 case MVT::v4i32:
17757 case MVT::v2i64:
17758 case MVT::v4f32:
17759 case MVT::v2f64:
Craig Topperc9099502012-04-20 06:31:50 +000017760 return std::make_pair(0U, &X86::VR128RegClass);
Eric Christopher55487552012-01-07 01:02:09 +000017761 // AVX types.
17762 case MVT::v32i8:
17763 case MVT::v16i16:
17764 case MVT::v8i32:
17765 case MVT::v4i64:
17766 case MVT::v8f32:
17767 case MVT::v4f64:
Craig Topperc9099502012-04-20 06:31:50 +000017768 return std::make_pair(0U, &X86::VR256RegClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +000017769 }
Chris Lattnerad043e82007-04-09 05:11:28 +000017770 break;
17771 }
17772 }
Scott Michelfdc40a02009-02-17 22:15:04 +000017773
Chris Lattnerf76d1802006-07-31 23:26:50 +000017774 // Use the default implementation in TargetLowering to convert the register
17775 // constraint into a member of a register class.
17776 std::pair<unsigned, const TargetRegisterClass*> Res;
17777 Res = TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
Chris Lattner1a60aa72006-10-31 19:42:44 +000017778
17779 // Not found as a standard register?
17780 if (Res.second == 0) {
Chris Lattner56d77c72009-09-13 22:41:48 +000017781 // Map st(0) -> st(7) -> ST0
17782 if (Constraint.size() == 7 && Constraint[0] == '{' &&
17783 tolower(Constraint[1]) == 's' &&
17784 tolower(Constraint[2]) == 't' &&
17785 Constraint[3] == '(' &&
17786 (Constraint[4] >= '0' && Constraint[4] <= '7') &&
17787 Constraint[5] == ')' &&
17788 Constraint[6] == '}') {
Daniel Dunbara279bc32009-09-20 02:20:51 +000017789
Chris Lattner56d77c72009-09-13 22:41:48 +000017790 Res.first = X86::ST0+Constraint[4]-'0';
Craig Topperc9099502012-04-20 06:31:50 +000017791 Res.second = &X86::RFP80RegClass;
Chris Lattner56d77c72009-09-13 22:41:48 +000017792 return Res;
17793 }
Daniel Dunbara279bc32009-09-20 02:20:51 +000017794
Chris Lattner56d77c72009-09-13 22:41:48 +000017795 // GCC allows "st(0)" to be called just plain "st".
Benjamin Kramer05872ea2009-11-12 20:36:59 +000017796 if (StringRef("{st}").equals_lower(Constraint)) {
Chris Lattner1a60aa72006-10-31 19:42:44 +000017797 Res.first = X86::ST0;
Craig Topperc9099502012-04-20 06:31:50 +000017798 Res.second = &X86::RFP80RegClass;
Chris Lattner56d77c72009-09-13 22:41:48 +000017799 return Res;
Chris Lattner1a60aa72006-10-31 19:42:44 +000017800 }
Chris Lattner56d77c72009-09-13 22:41:48 +000017801
17802 // flags -> EFLAGS
Benjamin Kramer05872ea2009-11-12 20:36:59 +000017803 if (StringRef("{flags}").equals_lower(Constraint)) {
Chris Lattner56d77c72009-09-13 22:41:48 +000017804 Res.first = X86::EFLAGS;
Craig Topperc9099502012-04-20 06:31:50 +000017805 Res.second = &X86::CCRRegClass;
Chris Lattner56d77c72009-09-13 22:41:48 +000017806 return Res;
17807 }
Daniel Dunbara279bc32009-09-20 02:20:51 +000017808
Dale Johannesen330169f2008-11-13 21:52:36 +000017809 // 'A' means EAX + EDX.
17810 if (Constraint == "A") {
17811 Res.first = X86::EAX;
Craig Topperc9099502012-04-20 06:31:50 +000017812 Res.second = &X86::GR32_ADRegClass;
Chris Lattner56d77c72009-09-13 22:41:48 +000017813 return Res;
Dale Johannesen330169f2008-11-13 21:52:36 +000017814 }
Chris Lattner1a60aa72006-10-31 19:42:44 +000017815 return Res;
17816 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000017817
Chris Lattnerf76d1802006-07-31 23:26:50 +000017818 // Otherwise, check to see if this is a register class of the wrong value
17819 // type. For example, we want to map "{ax},i32" -> {eax}, we don't want it to
17820 // turn into {ax},{dx}.
17821 if (Res.second->hasType(VT))
17822 return Res; // Correct type already, nothing to do.
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000017823
Chris Lattnerf76d1802006-07-31 23:26:50 +000017824 // All of the single-register GCC register classes map their values onto
17825 // 16-bit register pieces "ax","dx","cx","bx","si","di","bp","sp". If we
17826 // really want an 8-bit or 32-bit register, map to the appropriate register
17827 // class and return the appropriate register.
Craig Topperc9099502012-04-20 06:31:50 +000017828 if (Res.second == &X86::GR16RegClass) {
Owen Anderson825b72b2009-08-11 20:47:22 +000017829 if (VT == MVT::i8) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000017830 unsigned DestReg = 0;
17831 switch (Res.first) {
17832 default: break;
17833 case X86::AX: DestReg = X86::AL; break;
17834 case X86::DX: DestReg = X86::DL; break;
17835 case X86::CX: DestReg = X86::CL; break;
17836 case X86::BX: DestReg = X86::BL; break;
17837 }
17838 if (DestReg) {
17839 Res.first = DestReg;
Craig Topperc9099502012-04-20 06:31:50 +000017840 Res.second = &X86::GR8RegClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000017841 }
Owen Anderson825b72b2009-08-11 20:47:22 +000017842 } else if (VT == MVT::i32) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000017843 unsigned DestReg = 0;
17844 switch (Res.first) {
17845 default: break;
17846 case X86::AX: DestReg = X86::EAX; break;
17847 case X86::DX: DestReg = X86::EDX; break;
17848 case X86::CX: DestReg = X86::ECX; break;
17849 case X86::BX: DestReg = X86::EBX; break;
17850 case X86::SI: DestReg = X86::ESI; break;
17851 case X86::DI: DestReg = X86::EDI; break;
17852 case X86::BP: DestReg = X86::EBP; break;
17853 case X86::SP: DestReg = X86::ESP; break;
17854 }
17855 if (DestReg) {
17856 Res.first = DestReg;
Craig Topperc9099502012-04-20 06:31:50 +000017857 Res.second = &X86::GR32RegClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000017858 }
Owen Anderson825b72b2009-08-11 20:47:22 +000017859 } else if (VT == MVT::i64) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000017860 unsigned DestReg = 0;
17861 switch (Res.first) {
17862 default: break;
17863 case X86::AX: DestReg = X86::RAX; break;
17864 case X86::DX: DestReg = X86::RDX; break;
17865 case X86::CX: DestReg = X86::RCX; break;
17866 case X86::BX: DestReg = X86::RBX; break;
17867 case X86::SI: DestReg = X86::RSI; break;
17868 case X86::DI: DestReg = X86::RDI; break;
17869 case X86::BP: DestReg = X86::RBP; break;
17870 case X86::SP: DestReg = X86::RSP; break;
17871 }
17872 if (DestReg) {
17873 Res.first = DestReg;
Craig Topperc9099502012-04-20 06:31:50 +000017874 Res.second = &X86::GR64RegClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000017875 }
Chris Lattnerf76d1802006-07-31 23:26:50 +000017876 }
Craig Topperc9099502012-04-20 06:31:50 +000017877 } else if (Res.second == &X86::FR32RegClass ||
17878 Res.second == &X86::FR64RegClass ||
17879 Res.second == &X86::VR128RegClass) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000017880 // Handle references to XMM physical registers that got mapped into the
17881 // wrong class. This can happen with constraints like {xmm0} where the
17882 // target independent register mapper will just pick the first match it can
17883 // find, ignoring the required type.
Eli Friedman52d418d2012-06-25 23:42:33 +000017884
17885 if (VT == MVT::f32 || VT == MVT::i32)
Craig Topperc9099502012-04-20 06:31:50 +000017886 Res.second = &X86::FR32RegClass;
Eli Friedman52d418d2012-06-25 23:42:33 +000017887 else if (VT == MVT::f64 || VT == MVT::i64)
Craig Topperc9099502012-04-20 06:31:50 +000017888 Res.second = &X86::FR64RegClass;
17889 else if (X86::VR128RegClass.hasType(VT))
17890 Res.second = &X86::VR128RegClass;
Eli Friedman52d418d2012-06-25 23:42:33 +000017891 else if (X86::VR256RegClass.hasType(VT))
17892 Res.second = &X86::VR256RegClass;
Chris Lattnerf76d1802006-07-31 23:26:50 +000017893 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000017894
Chris Lattnerf76d1802006-07-31 23:26:50 +000017895 return Res;
17896}
Nadav Rotemb4b04c32012-11-03 00:39:56 +000017897
Nadav Roteme6237022012-11-05 19:32:46 +000017898//===----------------------------------------------------------------------===//
17899//
17900// X86 cost model.
17901//
17902//===----------------------------------------------------------------------===//
17903
17904struct X86CostTblEntry {
17905 int ISD;
17906 MVT Type;
17907 unsigned Cost;
17908};
17909
Nadav Rotemd8eae8b2012-11-06 23:36:00 +000017910static int
17911FindInTable(const X86CostTblEntry *Tbl, unsigned len, int ISD, MVT Ty) {
Nadav Rotem7ae3bcc2012-11-05 23:48:20 +000017912 for (unsigned int i = 0; i < len; ++i)
17913 if (Tbl[i].ISD == ISD && Tbl[i].Type == Ty)
17914 return i;
17915
17916 // Could not find an entry.
17917 return -1;
17918}
17919
Nadav Rotemb0428682012-11-06 19:33:53 +000017920struct X86TypeConversionCostTblEntry {
17921 int ISD;
17922 MVT Dst;
17923 MVT Src;
17924 unsigned Cost;
17925};
17926
Nadav Rotemd8eae8b2012-11-06 23:36:00 +000017927static int
17928FindInConvertTable(const X86TypeConversionCostTblEntry *Tbl, unsigned len,
17929 int ISD, MVT Dst, MVT Src) {
Nadav Rotemb0428682012-11-06 19:33:53 +000017930 for (unsigned int i = 0; i < len; ++i)
17931 if (Tbl[i].ISD == ISD && Tbl[i].Src == Src && Tbl[i].Dst == Dst)
17932 return i;
17933
17934 // Could not find an entry.
17935 return -1;
17936}
17937
Shuxin Yang5518a132012-12-09 03:12:46 +000017938ScalarTargetTransformInfo::PopcntHwSupport
17939X86ScalarTargetTransformImpl::getPopcntHwSupport(unsigned TyWidth) const {
17940 assert(isPowerOf2_32(TyWidth) && "Ty width must be power of 2");
17941 const X86Subtarget &ST = TLI->getTargetMachine().getSubtarget<X86Subtarget>();
17942
17943 // TODO: Currently the __builtin_popcount() implementation using SSE3
17944 // instructions is inefficient. Once the problem is fixed, we should
17945 // call ST.hasSSE3() instead of ST.hasSSE4().
17946 return ST.hasSSE41() ? Fast : None;
17947}
17948
Nadav Rotemb4b04c32012-11-03 00:39:56 +000017949unsigned
17950X86VectorTargetTransformInfo::getArithmeticInstrCost(unsigned Opcode,
17951 Type *Ty) const {
Nadav Roteme6237022012-11-05 19:32:46 +000017952 // Legalize the type.
Nadav Rotem887c1fe2012-11-05 23:57:45 +000017953 std::pair<unsigned, MVT> LT = getTypeLegalizationCost(Ty);
Nadav Roteme6237022012-11-05 19:32:46 +000017954
17955 int ISD = InstructionOpcodeToISD(Opcode);
17956 assert(ISD && "Invalid opcode");
17957
Nadav Rotemb0428682012-11-06 19:33:53 +000017958 const X86Subtarget &ST = TLI->getTargetMachine().getSubtarget<X86Subtarget>();
Nadav Rotemb4b04c32012-11-03 00:39:56 +000017959
Nadav Roteme6237022012-11-05 19:32:46 +000017960 static const X86CostTblEntry AVX1CostTable[] = {
17961 // We don't have to scalarize unsupported ops. We can issue two half-sized
17962 // operations and we only need to extract the upper YMM half.
17963 // Two ops + 1 extract + 1 insert = 4.
17964 { ISD::MUL, MVT::v8i32, 4 },
17965 { ISD::SUB, MVT::v8i32, 4 },
17966 { ISD::ADD, MVT::v8i32, 4 },
17967 { ISD::MUL, MVT::v4i64, 4 },
17968 { ISD::SUB, MVT::v4i64, 4 },
17969 { ISD::ADD, MVT::v4i64, 4 },
17970 };
Nadav Rotemb4b04c32012-11-03 00:39:56 +000017971
Nadav Roteme6237022012-11-05 19:32:46 +000017972 // Look for AVX1 lowering tricks.
Nadav Rotem7ae3bcc2012-11-05 23:48:20 +000017973 if (ST.hasAVX()) {
17974 int Idx = FindInTable(AVX1CostTable, array_lengthof(AVX1CostTable), ISD,
17975 LT.second);
17976 if (Idx != -1)
17977 return LT.first * AVX1CostTable[Idx].Cost;
17978 }
Nadav Roteme6237022012-11-05 19:32:46 +000017979 // Fallback to the default implementation.
Nadav Rotemb4b04c32012-11-03 00:39:56 +000017980 return VectorTargetTransformImpl::getArithmeticInstrCost(Opcode, Ty);
17981}
17982
Nadav Rotemf5637c32012-12-21 01:33:59 +000017983unsigned
17984X86VectorTargetTransformInfo::getMemoryOpCost(unsigned Opcode, Type *Src,
17985 unsigned Alignment,
17986 unsigned AddressSpace) const {
17987 // Legalize the type.
17988 std::pair<unsigned, MVT> LT = getTypeLegalizationCost(Src);
Nick Lewycky71f30bf2012-12-24 19:58:45 +000017989 assert((Opcode == Instruction::Load || Opcode == Instruction::Store) &&
Nadav Rotemf5637c32012-12-21 01:33:59 +000017990 "Invalid Opcode");
17991
17992 const X86Subtarget &ST =
17993 TLI->getTargetMachine().getSubtarget<X86Subtarget>();
17994
17995 // Each load/store unit costs 1.
17996 unsigned Cost = LT.first * 1;
17997
17998 // On Sandybridge 256bit load/stores are double pumped
17999 // (but not on Haswell).
18000 if (LT.second.getSizeInBits() > 128 && !ST.hasAVX2())
18001 Cost*=2;
18002
18003 return Cost;
18004}
18005
Nadav Rotemb4b04c32012-11-03 00:39:56 +000018006unsigned
18007X86VectorTargetTransformInfo::getVectorInstrCost(unsigned Opcode, Type *Val,
Richard Smithe010eb32012-11-05 22:01:44 +000018008 unsigned Index) const {
Nadav Rotema4ab5292012-11-05 21:12:13 +000018009 assert(Val->isVectorTy() && "This must be a vector type");
18010
Nadav Rotem7ae3bcc2012-11-05 23:48:20 +000018011 if (Index != -1U) {
Nadav Rotema4ab5292012-11-05 21:12:13 +000018012 // Legalize the type.
Nadav Rotem887c1fe2012-11-05 23:57:45 +000018013 std::pair<unsigned, MVT> LT = getTypeLegalizationCost(Val);
Nadav Rotema4ab5292012-11-05 21:12:13 +000018014
18015 // This type is legalized to a scalar type.
18016 if (!LT.second.isVector())
18017 return 0;
18018
18019 // The type may be split. Normalize the index to the new type.
18020 unsigned Width = LT.second.getVectorNumElements();
18021 Index = Index % Width;
18022
18023 // Floating point scalars are already located in index #0.
18024 if (Val->getScalarType()->isFloatingPointTy() && Index == 0)
18025 return 0;
18026 }
18027
Nadav Rotemb4b04c32012-11-03 00:39:56 +000018028 return VectorTargetTransformImpl::getVectorInstrCost(Opcode, Val, Index);
18029}
18030
Nadav Rotem7ae3bcc2012-11-05 23:48:20 +000018031unsigned X86VectorTargetTransformInfo::getCmpSelInstrCost(unsigned Opcode,
18032 Type *ValTy,
18033 Type *CondTy) const {
18034 // Legalize the type.
Nadav Rotem887c1fe2012-11-05 23:57:45 +000018035 std::pair<unsigned, MVT> LT = getTypeLegalizationCost(ValTy);
Nadav Rotem7ae3bcc2012-11-05 23:48:20 +000018036
18037 MVT MTy = LT.second;
18038
18039 int ISD = InstructionOpcodeToISD(Opcode);
18040 assert(ISD && "Invalid opcode");
18041
18042 const X86Subtarget &ST =
18043 TLI->getTargetMachine().getSubtarget<X86Subtarget>();
18044
18045 static const X86CostTblEntry SSE42CostTbl[] = {
18046 { ISD::SETCC, MVT::v2f64, 1 },
18047 { ISD::SETCC, MVT::v4f32, 1 },
18048 { ISD::SETCC, MVT::v2i64, 1 },
18049 { ISD::SETCC, MVT::v4i32, 1 },
18050 { ISD::SETCC, MVT::v8i16, 1 },
18051 { ISD::SETCC, MVT::v16i8, 1 },
18052 };
18053
18054 static const X86CostTblEntry AVX1CostTbl[] = {
18055 { ISD::SETCC, MVT::v4f64, 1 },
18056 { ISD::SETCC, MVT::v8f32, 1 },
18057 // AVX1 does not support 8-wide integer compare.
18058 { ISD::SETCC, MVT::v4i64, 4 },
18059 { ISD::SETCC, MVT::v8i32, 4 },
18060 { ISD::SETCC, MVT::v16i16, 4 },
18061 { ISD::SETCC, MVT::v32i8, 4 },
18062 };
18063
18064 static const X86CostTblEntry AVX2CostTbl[] = {
18065 { ISD::SETCC, MVT::v4i64, 1 },
18066 { ISD::SETCC, MVT::v8i32, 1 },
18067 { ISD::SETCC, MVT::v16i16, 1 },
18068 { ISD::SETCC, MVT::v32i8, 1 },
18069 };
18070
Jakub Staszak270bfbd2012-12-18 22:57:56 +000018071 if (ST.hasAVX2()) {
18072 int Idx = FindInTable(AVX2CostTbl, array_lengthof(AVX2CostTbl), ISD, MTy);
Nadav Rotem7ae3bcc2012-11-05 23:48:20 +000018073 if (Idx != -1)
Jakub Staszak270bfbd2012-12-18 22:57:56 +000018074 return LT.first * AVX2CostTbl[Idx].Cost;
Nadav Rotem7ae3bcc2012-11-05 23:48:20 +000018075 }
18076
18077 if (ST.hasAVX()) {
18078 int Idx = FindInTable(AVX1CostTbl, array_lengthof(AVX1CostTbl), ISD, MTy);
18079 if (Idx != -1)
18080 return LT.first * AVX1CostTbl[Idx].Cost;
18081 }
18082
Jakub Staszak270bfbd2012-12-18 22:57:56 +000018083 if (ST.hasSSE42()) {
18084 int Idx = FindInTable(SSE42CostTbl, array_lengthof(SSE42CostTbl), ISD, MTy);
Nadav Rotem7ae3bcc2012-11-05 23:48:20 +000018085 if (Idx != -1)
Jakub Staszak270bfbd2012-12-18 22:57:56 +000018086 return LT.first * SSE42CostTbl[Idx].Cost;
Nadav Rotem7ae3bcc2012-11-05 23:48:20 +000018087 }
18088
18089 return VectorTargetTransformImpl::getCmpSelInstrCost(Opcode, ValTy, CondTy);
18090}
18091
Nadav Rotemb0428682012-11-06 19:33:53 +000018092unsigned X86VectorTargetTransformInfo::getCastInstrCost(unsigned Opcode,
18093 Type *Dst,
18094 Type *Src) const {
18095 int ISD = InstructionOpcodeToISD(Opcode);
18096 assert(ISD && "Invalid opcode");
Nadav Rotem7ae3bcc2012-11-05 23:48:20 +000018097
Nadav Rotemb0428682012-11-06 19:33:53 +000018098 EVT SrcTy = TLI->getValueType(Src);
18099 EVT DstTy = TLI->getValueType(Dst);
18100
18101 if (!SrcTy.isSimple() || !DstTy.isSimple())
18102 return VectorTargetTransformImpl::getCastInstrCost(Opcode, Dst, Src);
18103
18104 const X86Subtarget &ST = TLI->getTargetMachine().getSubtarget<X86Subtarget>();
18105
18106 static const X86TypeConversionCostTblEntry AVXConversionTbl[] = {
18107 { ISD::SIGN_EXTEND, MVT::v8i32, MVT::v8i16, 1 },
18108 { ISD::ZERO_EXTEND, MVT::v8i32, MVT::v8i16, 1 },
18109 { ISD::SIGN_EXTEND, MVT::v4i64, MVT::v4i32, 1 },
18110 { ISD::ZERO_EXTEND, MVT::v4i64, MVT::v4i32, 1 },
18111 { ISD::TRUNCATE, MVT::v4i32, MVT::v4i64, 1 },
18112 { ISD::TRUNCATE, MVT::v8i16, MVT::v8i32, 1 },
18113 { ISD::SINT_TO_FP, MVT::v8f32, MVT::v8i8, 1 },
18114 { ISD::SINT_TO_FP, MVT::v4f32, MVT::v4i8, 1 },
18115 { ISD::UINT_TO_FP, MVT::v8f32, MVT::v8i8, 1 },
18116 { ISD::UINT_TO_FP, MVT::v4f32, MVT::v4i8, 1 },
Nadav Rotemb14a5f52012-11-09 07:02:24 +000018117 { ISD::FP_TO_SINT, MVT::v8i8, MVT::v8f32, 1 },
18118 { ISD::FP_TO_SINT, MVT::v4i8, MVT::v4f32, 1 },
Nadav Rotemb0428682012-11-06 19:33:53 +000018119 { ISD::ZERO_EXTEND, MVT::v8i32, MVT::v8i1, 6 },
18120 { ISD::SIGN_EXTEND, MVT::v8i32, MVT::v8i1, 9 },
Nadav Rotema6fb97a2012-11-06 21:17:17 +000018121 { ISD::TRUNCATE, MVT::v8i32, MVT::v8i64, 3 },
Nadav Rotemb0428682012-11-06 19:33:53 +000018122 };
18123
18124 if (ST.hasAVX()) {
18125 int Idx = FindInConvertTable(AVXConversionTbl,
18126 array_lengthof(AVXConversionTbl),
18127 ISD, DstTy.getSimpleVT(), SrcTy.getSimpleVT());
18128 if (Idx != -1)
18129 return AVXConversionTbl[Idx].Cost;
18130 }
18131
18132 return VectorTargetTransformImpl::getCastInstrCost(Opcode, Dst, Src);
18133}
Nadav Rotem7ae3bcc2012-11-05 23:48:20 +000018134