blob: 81e8a7bd88d1004c74d88fe7dfeae9f57e2ef8d6 [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);
873 setOperationAction(ISD::MUL, MVT::v2i64, Custom);
874 setOperationAction(ISD::SUB, MVT::v16i8, Legal);
875 setOperationAction(ISD::SUB, MVT::v8i16, Legal);
876 setOperationAction(ISD::SUB, MVT::v4i32, Legal);
877 setOperationAction(ISD::SUB, MVT::v2i64, Legal);
878 setOperationAction(ISD::MUL, MVT::v8i16, Legal);
879 setOperationAction(ISD::FADD, MVT::v2f64, Legal);
880 setOperationAction(ISD::FSUB, MVT::v2f64, Legal);
881 setOperationAction(ISD::FMUL, MVT::v2f64, Legal);
882 setOperationAction(ISD::FDIV, MVT::v2f64, Legal);
883 setOperationAction(ISD::FSQRT, MVT::v2f64, Legal);
884 setOperationAction(ISD::FNEG, MVT::v2f64, Custom);
Craig Topper43620672012-09-08 07:31:51 +0000885 setOperationAction(ISD::FABS, MVT::v2f64, Custom);
Evan Cheng2c3ae372006-04-12 21:21:57 +0000886
Nadav Rotem354efd82011-09-18 14:57:03 +0000887 setOperationAction(ISD::SETCC, MVT::v2i64, Custom);
Duncan Sands28b77e92011-09-06 19:07:46 +0000888 setOperationAction(ISD::SETCC, MVT::v16i8, Custom);
889 setOperationAction(ISD::SETCC, MVT::v8i16, Custom);
890 setOperationAction(ISD::SETCC, MVT::v4i32, Custom);
Nate Begemanc2616e42008-05-12 20:34:32 +0000891
Owen Anderson825b72b2009-08-11 20:47:22 +0000892 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v16i8, Custom);
893 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i16, Custom);
894 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
895 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom);
896 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
Evan Chengf7c378e2006-04-10 07:23:14 +0000897
Evan Cheng2c3ae372006-04-12 21:21:57 +0000898 // Custom lower build_vector, vector_shuffle, and extract_vector_elt.
Jakub Staszak6610b1d2012-04-29 20:52:53 +0000899 for (int i = MVT::v16i8; i != MVT::v2i64; ++i) {
Craig Topper0d1f1762012-08-12 00:34:56 +0000900 MVT VT = (MVT::SimpleValueType)i;
Nate Begeman844e0f92007-12-11 01:41:33 +0000901 // Do not attempt to custom lower non-power-of-2 vectors
Duncan Sands83ec4b62008-06-06 12:08:01 +0000902 if (!isPowerOf2_32(VT.getVectorNumElements()))
Nate Begeman844e0f92007-12-11 01:41:33 +0000903 continue;
David Greene9b9838d2009-06-29 16:47:10 +0000904 // Do not attempt to custom lower non-128-bit vectors
905 if (!VT.is128BitVector())
906 continue;
Craig Topper0d1f1762012-08-12 00:34:56 +0000907 setOperationAction(ISD::BUILD_VECTOR, VT, Custom);
908 setOperationAction(ISD::VECTOR_SHUFFLE, VT, Custom);
909 setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT, Custom);
Evan Cheng2c3ae372006-04-12 21:21:57 +0000910 }
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000911
Owen Anderson825b72b2009-08-11 20:47:22 +0000912 setOperationAction(ISD::BUILD_VECTOR, MVT::v2f64, Custom);
913 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i64, Custom);
914 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2f64, Custom);
915 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i64, Custom);
916 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2f64, Custom);
917 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2f64, Custom);
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000918
Nate Begemancdd1eec2008-02-12 22:51:28 +0000919 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000920 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i64, Custom);
921 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Custom);
Nate Begemancdd1eec2008-02-12 22:51:28 +0000922 }
Evan Cheng2c3ae372006-04-12 21:21:57 +0000923
Anton Korobeynikov12c49af2006-11-21 00:01:06 +0000924 // Promote v16i8, v8i16, v4i32 load, select, and, or, xor to v2i64.
Craig Topper31a207a2012-05-04 06:39:13 +0000925 for (int i = MVT::v16i8; i != MVT::v2i64; ++i) {
Craig Topper0d1f1762012-08-12 00:34:56 +0000926 MVT VT = (MVT::SimpleValueType)i;
David Greene9b9838d2009-06-29 16:47:10 +0000927
928 // Do not attempt to promote non-128-bit vectors
Chris Lattner32b4b5a2010-07-05 05:53:14 +0000929 if (!VT.is128BitVector())
David Greene9b9838d2009-06-29 16:47:10 +0000930 continue;
Michael J. Spencerec38de22010-10-10 22:04:20 +0000931
Craig Topper0d1f1762012-08-12 00:34:56 +0000932 setOperationAction(ISD::AND, VT, Promote);
933 AddPromotedToType (ISD::AND, VT, MVT::v2i64);
934 setOperationAction(ISD::OR, VT, Promote);
935 AddPromotedToType (ISD::OR, VT, MVT::v2i64);
936 setOperationAction(ISD::XOR, VT, Promote);
937 AddPromotedToType (ISD::XOR, VT, MVT::v2i64);
938 setOperationAction(ISD::LOAD, VT, Promote);
939 AddPromotedToType (ISD::LOAD, VT, MVT::v2i64);
940 setOperationAction(ISD::SELECT, VT, Promote);
941 AddPromotedToType (ISD::SELECT, VT, MVT::v2i64);
Evan Chengf7c378e2006-04-10 07:23:14 +0000942 }
Evan Cheng2c3ae372006-04-12 21:21:57 +0000943
Owen Anderson825b72b2009-08-11 20:47:22 +0000944 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000945
Evan Cheng2c3ae372006-04-12 21:21:57 +0000946 // Custom lower v2i64 and v2f64 selects.
Owen Anderson825b72b2009-08-11 20:47:22 +0000947 setOperationAction(ISD::LOAD, MVT::v2f64, Legal);
948 setOperationAction(ISD::LOAD, MVT::v2i64, Legal);
949 setOperationAction(ISD::SELECT, MVT::v2f64, Custom);
950 setOperationAction(ISD::SELECT, MVT::v2i64, Custom);
Scott Michelfdc40a02009-02-17 22:15:04 +0000951
Owen Anderson825b72b2009-08-11 20:47:22 +0000952 setOperationAction(ISD::FP_TO_SINT, MVT::v4i32, Legal);
953 setOperationAction(ISD::SINT_TO_FP, MVT::v4i32, Legal);
Michael Liaob8150d82012-09-10 18:33:51 +0000954
Michael Liaoa7554632012-10-23 17:36:08 +0000955 setOperationAction(ISD::UINT_TO_FP, MVT::v4i8, Custom);
956 setOperationAction(ISD::UINT_TO_FP, MVT::v4i16, Custom);
Michael Liao991b6a22012-10-24 04:09:32 +0000957 // As there is no 64-bit GPR available, we need build a special custom
958 // sequence to convert from v2i32 to v2f32.
959 if (!Subtarget->is64Bit())
960 setOperationAction(ISD::UINT_TO_FP, MVT::v2f32, Custom);
Michael Liaoa7554632012-10-23 17:36:08 +0000961
Michael Liao9d796db2012-10-10 16:32:15 +0000962 setOperationAction(ISD::FP_EXTEND, MVT::v2f32, Custom);
Michael Liao44c2d612012-10-10 16:53:28 +0000963 setOperationAction(ISD::FP_ROUND, MVT::v2f32, Custom);
Michael Liao9d796db2012-10-10 16:32:15 +0000964
Michael Liaob8150d82012-09-10 18:33:51 +0000965 setLoadExtAction(ISD::EXTLOAD, MVT::v2f32, Legal);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000966 }
Evan Chengc7ce29b2009-02-13 22:36:38 +0000967
Craig Topperd0a31172012-01-10 06:37:29 +0000968 if (Subtarget->hasSSE41()) {
Benjamin Kramerb6533972011-12-09 15:44:03 +0000969 setOperationAction(ISD::FFLOOR, MVT::f32, Legal);
970 setOperationAction(ISD::FCEIL, MVT::f32, Legal);
971 setOperationAction(ISD::FTRUNC, MVT::f32, Legal);
972 setOperationAction(ISD::FRINT, MVT::f32, Legal);
973 setOperationAction(ISD::FNEARBYINT, MVT::f32, Legal);
974 setOperationAction(ISD::FFLOOR, MVT::f64, Legal);
975 setOperationAction(ISD::FCEIL, MVT::f64, Legal);
976 setOperationAction(ISD::FTRUNC, MVT::f64, Legal);
977 setOperationAction(ISD::FRINT, MVT::f64, Legal);
978 setOperationAction(ISD::FNEARBYINT, MVT::f64, Legal);
979
Craig Topper12fb5c62012-09-08 17:42:27 +0000980 setOperationAction(ISD::FFLOOR, MVT::v4f32, Legal);
Craig Topperd5775522012-11-16 06:37:56 +0000981 setOperationAction(ISD::FCEIL, MVT::v4f32, Legal);
982 setOperationAction(ISD::FTRUNC, MVT::v4f32, Legal);
983 setOperationAction(ISD::FRINT, MVT::v4f32, Legal);
984 setOperationAction(ISD::FNEARBYINT, MVT::v4f32, Legal);
Craig Topper12fb5c62012-09-08 17:42:27 +0000985 setOperationAction(ISD::FFLOOR, MVT::v2f64, Legal);
Craig Topperd5775522012-11-16 06:37:56 +0000986 setOperationAction(ISD::FCEIL, MVT::v2f64, Legal);
987 setOperationAction(ISD::FTRUNC, MVT::v2f64, Legal);
988 setOperationAction(ISD::FRINT, MVT::v2f64, Legal);
989 setOperationAction(ISD::FNEARBYINT, MVT::v2f64, Legal);
Craig Topper12fb5c62012-09-08 17:42:27 +0000990
Nate Begeman14d12ca2008-02-11 04:19:36 +0000991 // FIXME: Do we need to handle scalar-to-vector here?
Owen Anderson825b72b2009-08-11 20:47:22 +0000992 setOperationAction(ISD::MUL, MVT::v4i32, Legal);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000993
Nadav Rotemfbad25e2011-09-11 15:02:23 +0000994 setOperationAction(ISD::VSELECT, MVT::v2f64, Legal);
995 setOperationAction(ISD::VSELECT, MVT::v2i64, Legal);
996 setOperationAction(ISD::VSELECT, MVT::v16i8, Legal);
997 setOperationAction(ISD::VSELECT, MVT::v4i32, Legal);
998 setOperationAction(ISD::VSELECT, MVT::v4f32, Legal);
Nadav Rotemffe3e7d2011-09-08 08:11:19 +0000999
Nate Begeman14d12ca2008-02-11 04:19:36 +00001000 // i8 and i16 vectors are custom , because the source register and source
1001 // source memory operand types are not the same width. f32 vectors are
1002 // custom since the immediate controlling the insert encodes additional
1003 // information.
Owen Anderson825b72b2009-08-11 20:47:22 +00001004 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v16i8, Custom);
1005 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
1006 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom);
1007 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
Nate Begeman14d12ca2008-02-11 04:19:36 +00001008
Owen Anderson825b72b2009-08-11 20:47:22 +00001009 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v16i8, Custom);
1010 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v8i16, Custom);
1011 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4i32, Custom);
1012 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
Nate Begeman14d12ca2008-02-11 04:19:36 +00001013
Pete Coopera77214a2011-11-14 19:38:42 +00001014 // FIXME: these should be Legal but thats only for the case where
Chad Rosier30450e82011-12-22 22:35:21 +00001015 // the index is constant. For now custom expand to deal with that.
Nate Begeman14d12ca2008-02-11 04:19:36 +00001016 if (Subtarget->is64Bit()) {
Pete Coopera77214a2011-11-14 19:38:42 +00001017 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i64, Custom);
1018 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Custom);
Nate Begeman14d12ca2008-02-11 04:19:36 +00001019 }
1020 }
Evan Cheng470a6ad2006-02-22 02:26:30 +00001021
Craig Topper1accb7e2012-01-10 06:54:16 +00001022 if (Subtarget->hasSSE2()) {
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +00001023 setOperationAction(ISD::SRL, MVT::v8i16, Custom);
Craig Topper7be5dfd2011-11-12 09:58:49 +00001024 setOperationAction(ISD::SRL, MVT::v16i8, Custom);
Nadav Rotem43012222011-05-11 08:12:09 +00001025
Nadav Rotem43012222011-05-11 08:12:09 +00001026 setOperationAction(ISD::SHL, MVT::v8i16, Custom);
Craig Topper7be5dfd2011-11-12 09:58:49 +00001027 setOperationAction(ISD::SHL, MVT::v16i8, Custom);
Nadav Rotem43012222011-05-11 08:12:09 +00001028
Nadav Rotem43012222011-05-11 08:12:09 +00001029 setOperationAction(ISD::SRA, MVT::v8i16, Custom);
Eli Friedmanf6aa6b12011-11-01 21:18:39 +00001030 setOperationAction(ISD::SRA, MVT::v16i8, Custom);
Craig Topper7be5dfd2011-11-12 09:58:49 +00001031
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00001032 if (Subtarget->hasInt256()) {
Craig Topper7be5dfd2011-11-12 09:58:49 +00001033 setOperationAction(ISD::SRL, MVT::v2i64, Legal);
1034 setOperationAction(ISD::SRL, MVT::v4i32, Legal);
1035
1036 setOperationAction(ISD::SHL, MVT::v2i64, Legal);
1037 setOperationAction(ISD::SHL, MVT::v4i32, Legal);
1038
1039 setOperationAction(ISD::SRA, MVT::v4i32, Legal);
1040 } else {
1041 setOperationAction(ISD::SRL, MVT::v2i64, Custom);
1042 setOperationAction(ISD::SRL, MVT::v4i32, Custom);
1043
1044 setOperationAction(ISD::SHL, MVT::v2i64, Custom);
1045 setOperationAction(ISD::SHL, MVT::v4i32, Custom);
1046
1047 setOperationAction(ISD::SRA, MVT::v4i32, Custom);
1048 }
Nadav Rotem43012222011-05-11 08:12:09 +00001049 }
1050
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00001051 if (!TM.Options.UseSoftFloat && Subtarget->hasFp256()) {
Craig Topperc9099502012-04-20 06:31:50 +00001052 addRegisterClass(MVT::v32i8, &X86::VR256RegClass);
1053 addRegisterClass(MVT::v16i16, &X86::VR256RegClass);
1054 addRegisterClass(MVT::v8i32, &X86::VR256RegClass);
1055 addRegisterClass(MVT::v8f32, &X86::VR256RegClass);
1056 addRegisterClass(MVT::v4i64, &X86::VR256RegClass);
1057 addRegisterClass(MVT::v4f64, &X86::VR256RegClass);
David Greened94c1012009-06-29 22:50:51 +00001058
Owen Anderson825b72b2009-08-11 20:47:22 +00001059 setOperationAction(ISD::LOAD, MVT::v8f32, Legal);
Owen Anderson825b72b2009-08-11 20:47:22 +00001060 setOperationAction(ISD::LOAD, MVT::v4f64, Legal);
1061 setOperationAction(ISD::LOAD, MVT::v4i64, Legal);
David Greene54d8eba2011-01-27 22:38:56 +00001062
Owen Anderson825b72b2009-08-11 20:47:22 +00001063 setOperationAction(ISD::FADD, MVT::v8f32, Legal);
1064 setOperationAction(ISD::FSUB, MVT::v8f32, Legal);
1065 setOperationAction(ISD::FMUL, MVT::v8f32, Legal);
1066 setOperationAction(ISD::FDIV, MVT::v8f32, Legal);
1067 setOperationAction(ISD::FSQRT, MVT::v8f32, Legal);
Craig Topper12fb5c62012-09-08 17:42:27 +00001068 setOperationAction(ISD::FFLOOR, MVT::v8f32, Legal);
Craig Topperd5775522012-11-16 06:37:56 +00001069 setOperationAction(ISD::FCEIL, MVT::v8f32, Legal);
1070 setOperationAction(ISD::FTRUNC, MVT::v8f32, Legal);
1071 setOperationAction(ISD::FRINT, MVT::v8f32, Legal);
1072 setOperationAction(ISD::FNEARBYINT, MVT::v8f32, Legal);
Owen Anderson825b72b2009-08-11 20:47:22 +00001073 setOperationAction(ISD::FNEG, MVT::v8f32, Custom);
Craig Topper43620672012-09-08 07:31:51 +00001074 setOperationAction(ISD::FABS, MVT::v8f32, Custom);
David Greene9b9838d2009-06-29 16:47:10 +00001075
Owen Anderson825b72b2009-08-11 20:47:22 +00001076 setOperationAction(ISD::FADD, MVT::v4f64, Legal);
1077 setOperationAction(ISD::FSUB, MVT::v4f64, Legal);
1078 setOperationAction(ISD::FMUL, MVT::v4f64, Legal);
1079 setOperationAction(ISD::FDIV, MVT::v4f64, Legal);
1080 setOperationAction(ISD::FSQRT, MVT::v4f64, Legal);
Craig Topper12fb5c62012-09-08 17:42:27 +00001081 setOperationAction(ISD::FFLOOR, MVT::v4f64, Legal);
Craig Topperd5775522012-11-16 06:37:56 +00001082 setOperationAction(ISD::FCEIL, MVT::v4f64, Legal);
1083 setOperationAction(ISD::FTRUNC, MVT::v4f64, Legal);
1084 setOperationAction(ISD::FRINT, MVT::v4f64, Legal);
1085 setOperationAction(ISD::FNEARBYINT, MVT::v4f64, Legal);
Owen Anderson825b72b2009-08-11 20:47:22 +00001086 setOperationAction(ISD::FNEG, MVT::v4f64, Custom);
Craig Topper43620672012-09-08 07:31:51 +00001087 setOperationAction(ISD::FABS, MVT::v4f64, Custom);
David Greene9b9838d2009-06-29 16:47:10 +00001088
Michael Liaobedcbd42012-10-16 18:14:11 +00001089 setOperationAction(ISD::TRUNCATE, MVT::v8i16, Custom);
1090
1091 setOperationAction(ISD::FP_TO_SINT, MVT::v8i16, Custom);
1092
Bruno Cardoso Lopes2e64ae42011-07-28 01:26:39 +00001093 setOperationAction(ISD::FP_TO_SINT, MVT::v8i32, Legal);
1094 setOperationAction(ISD::SINT_TO_FP, MVT::v8i32, Legal);
Bruno Cardoso Lopes55244ce2011-08-01 21:54:09 +00001095 setOperationAction(ISD::FP_ROUND, MVT::v4f32, Legal);
Bruno Cardoso Lopes2e64ae42011-07-28 01:26:39 +00001096
Michael Liaoa7554632012-10-23 17:36:08 +00001097 setOperationAction(ISD::ZERO_EXTEND, MVT::v8i32, Custom);
1098 setOperationAction(ISD::UINT_TO_FP, MVT::v8i8, Custom);
1099 setOperationAction(ISD::UINT_TO_FP, MVT::v8i16, Custom);
1100
Michael Liaob8150d82012-09-10 18:33:51 +00001101 setLoadExtAction(ISD::EXTLOAD, MVT::v4f32, Legal);
1102
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +00001103 setOperationAction(ISD::SRL, MVT::v16i16, Custom);
1104 setOperationAction(ISD::SRL, MVT::v32i8, Custom);
1105
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +00001106 setOperationAction(ISD::SHL, MVT::v16i16, Custom);
1107 setOperationAction(ISD::SHL, MVT::v32i8, Custom);
1108
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +00001109 setOperationAction(ISD::SRA, MVT::v16i16, Custom);
Craig Topper7be5dfd2011-11-12 09:58:49 +00001110 setOperationAction(ISD::SRA, MVT::v32i8, Custom);
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +00001111
Duncan Sands28b77e92011-09-06 19:07:46 +00001112 setOperationAction(ISD::SETCC, MVT::v32i8, Custom);
1113 setOperationAction(ISD::SETCC, MVT::v16i16, Custom);
1114 setOperationAction(ISD::SETCC, MVT::v8i32, Custom);
1115 setOperationAction(ISD::SETCC, MVT::v4i64, Custom);
Bruno Cardoso Lopes0f0e0a02011-08-09 00:46:57 +00001116
Bruno Cardoso Lopesd40aa242011-08-09 23:27:13 +00001117 setOperationAction(ISD::SELECT, MVT::v4f64, Custom);
1118 setOperationAction(ISD::SELECT, MVT::v4i64, Custom);
1119 setOperationAction(ISD::SELECT, MVT::v8f32, Custom);
1120
Craig Topperaaa643c2011-11-09 07:28:55 +00001121 setOperationAction(ISD::VSELECT, MVT::v4f64, Legal);
1122 setOperationAction(ISD::VSELECT, MVT::v4i64, Legal);
1123 setOperationAction(ISD::VSELECT, MVT::v8i32, Legal);
1124 setOperationAction(ISD::VSELECT, MVT::v8f32, Legal);
Nadav Rotem8ffad562011-09-09 20:29:17 +00001125
Craig Topperbf404372012-08-31 15:40:30 +00001126 if (Subtarget->hasFMA() || Subtarget->hasFMA4()) {
Craig Topper3dcefc82012-11-21 05:36:24 +00001127 setOperationAction(ISD::FMA, MVT::v8f32, Legal);
1128 setOperationAction(ISD::FMA, MVT::v4f64, Legal);
1129 setOperationAction(ISD::FMA, MVT::v4f32, Legal);
1130 setOperationAction(ISD::FMA, MVT::v2f64, Legal);
1131 setOperationAction(ISD::FMA, MVT::f32, Legal);
1132 setOperationAction(ISD::FMA, MVT::f64, Legal);
Elena Demikhovsky1503aba2012-08-01 12:06:00 +00001133 }
Craig Topper880ef452012-08-11 22:34:26 +00001134
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00001135 if (Subtarget->hasInt256()) {
Craig Topperaaa643c2011-11-09 07:28:55 +00001136 setOperationAction(ISD::ADD, MVT::v4i64, Legal);
1137 setOperationAction(ISD::ADD, MVT::v8i32, Legal);
1138 setOperationAction(ISD::ADD, MVT::v16i16, Legal);
1139 setOperationAction(ISD::ADD, MVT::v32i8, Legal);
Craig Topper13894fa2011-08-24 06:14:18 +00001140
Craig Topperaaa643c2011-11-09 07:28:55 +00001141 setOperationAction(ISD::SUB, MVT::v4i64, Legal);
1142 setOperationAction(ISD::SUB, MVT::v8i32, Legal);
1143 setOperationAction(ISD::SUB, MVT::v16i16, Legal);
1144 setOperationAction(ISD::SUB, MVT::v32i8, Legal);
Craig Topper13894fa2011-08-24 06:14:18 +00001145
Craig Topperaaa643c2011-11-09 07:28:55 +00001146 setOperationAction(ISD::MUL, MVT::v4i64, Custom);
1147 setOperationAction(ISD::MUL, MVT::v8i32, Legal);
1148 setOperationAction(ISD::MUL, MVT::v16i16, Legal);
Craig Topper46154eb2011-11-11 07:39:23 +00001149 // Don't lower v32i8 because there is no 128-bit byte mul
Nadav Rotembb539bf2011-11-09 13:21:28 +00001150
1151 setOperationAction(ISD::VSELECT, MVT::v32i8, Legal);
Craig Topper7be5dfd2011-11-12 09:58:49 +00001152
1153 setOperationAction(ISD::SRL, MVT::v4i64, Legal);
1154 setOperationAction(ISD::SRL, MVT::v8i32, Legal);
1155
1156 setOperationAction(ISD::SHL, MVT::v4i64, Legal);
1157 setOperationAction(ISD::SHL, MVT::v8i32, Legal);
1158
1159 setOperationAction(ISD::SRA, MVT::v8i32, Legal);
Craig Topperaaa643c2011-11-09 07:28:55 +00001160 } else {
1161 setOperationAction(ISD::ADD, MVT::v4i64, Custom);
1162 setOperationAction(ISD::ADD, MVT::v8i32, Custom);
1163 setOperationAction(ISD::ADD, MVT::v16i16, Custom);
1164 setOperationAction(ISD::ADD, MVT::v32i8, Custom);
1165
1166 setOperationAction(ISD::SUB, MVT::v4i64, Custom);
1167 setOperationAction(ISD::SUB, MVT::v8i32, Custom);
1168 setOperationAction(ISD::SUB, MVT::v16i16, Custom);
1169 setOperationAction(ISD::SUB, MVT::v32i8, Custom);
1170
1171 setOperationAction(ISD::MUL, MVT::v4i64, Custom);
1172 setOperationAction(ISD::MUL, MVT::v8i32, Custom);
1173 setOperationAction(ISD::MUL, MVT::v16i16, Custom);
1174 // Don't lower v32i8 because there is no 128-bit byte mul
Craig Topper7be5dfd2011-11-12 09:58:49 +00001175
1176 setOperationAction(ISD::SRL, MVT::v4i64, Custom);
1177 setOperationAction(ISD::SRL, MVT::v8i32, Custom);
1178
1179 setOperationAction(ISD::SHL, MVT::v4i64, Custom);
1180 setOperationAction(ISD::SHL, MVT::v8i32, Custom);
1181
1182 setOperationAction(ISD::SRA, MVT::v8i32, Custom);
Craig Topperaaa643c2011-11-09 07:28:55 +00001183 }
Craig Topper13894fa2011-08-24 06:14:18 +00001184
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001185 // Custom lower several nodes for 256-bit types.
Jakub Staszak6610b1d2012-04-29 20:52:53 +00001186 for (int i = MVT::FIRST_VECTOR_VALUETYPE;
1187 i <= MVT::LAST_VECTOR_VALUETYPE; ++i) {
Craig Topper0d1f1762012-08-12 00:34:56 +00001188 MVT VT = (MVT::SimpleValueType)i;
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001189
1190 // Extract subvector is special because the value type
1191 // (result) is 128-bit but the source is 256-bit wide.
1192 if (VT.is128BitVector())
Craig Topper0d1f1762012-08-12 00:34:56 +00001193 setOperationAction(ISD::EXTRACT_SUBVECTOR, VT, Custom);
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001194
1195 // Do not attempt to custom lower other non-256-bit vectors
1196 if (!VT.is256BitVector())
David Greene9b9838d2009-06-29 16:47:10 +00001197 continue;
David Greene54d8eba2011-01-27 22:38:56 +00001198
Craig Topper0d1f1762012-08-12 00:34:56 +00001199 setOperationAction(ISD::BUILD_VECTOR, VT, Custom);
1200 setOperationAction(ISD::VECTOR_SHUFFLE, VT, Custom);
1201 setOperationAction(ISD::INSERT_VECTOR_ELT, VT, Custom);
1202 setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT, Custom);
1203 setOperationAction(ISD::SCALAR_TO_VECTOR, VT, Custom);
1204 setOperationAction(ISD::INSERT_SUBVECTOR, VT, Custom);
1205 setOperationAction(ISD::CONCAT_VECTORS, VT, Custom);
David Greene9b9838d2009-06-29 16:47:10 +00001206 }
1207
David Greene54d8eba2011-01-27 22:38:56 +00001208 // Promote v32i8, v16i16, v8i32 select, and, or, xor to v4i64.
Jakub Staszak6610b1d2012-04-29 20:52:53 +00001209 for (int i = MVT::v32i8; i != MVT::v4i64; ++i) {
Craig Topper0d1f1762012-08-12 00:34:56 +00001210 MVT VT = (MVT::SimpleValueType)i;
David Greene54d8eba2011-01-27 22:38:56 +00001211
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001212 // Do not attempt to promote non-256-bit vectors
1213 if (!VT.is256BitVector())
David Greene54d8eba2011-01-27 22:38:56 +00001214 continue;
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001215
Craig Topper0d1f1762012-08-12 00:34:56 +00001216 setOperationAction(ISD::AND, VT, Promote);
1217 AddPromotedToType (ISD::AND, VT, MVT::v4i64);
1218 setOperationAction(ISD::OR, VT, Promote);
1219 AddPromotedToType (ISD::OR, VT, MVT::v4i64);
1220 setOperationAction(ISD::XOR, VT, Promote);
1221 AddPromotedToType (ISD::XOR, VT, MVT::v4i64);
1222 setOperationAction(ISD::LOAD, VT, Promote);
1223 AddPromotedToType (ISD::LOAD, VT, MVT::v4i64);
1224 setOperationAction(ISD::SELECT, VT, Promote);
1225 AddPromotedToType (ISD::SELECT, VT, MVT::v4i64);
David Greene54d8eba2011-01-27 22:38:56 +00001226 }
David Greene9b9838d2009-06-29 16:47:10 +00001227 }
1228
Nadav Rotemd0f3ef82011-07-14 11:11:14 +00001229 // SIGN_EXTEND_INREGs are evaluated by the extend type. Handle the expansion
1230 // of this type with custom code.
Jakub Staszak6610b1d2012-04-29 20:52:53 +00001231 for (int VT = MVT::FIRST_VECTOR_VALUETYPE;
1232 VT != MVT::LAST_VECTOR_VALUETYPE; VT++) {
Chad Rosier30450e82011-12-22 22:35:21 +00001233 setOperationAction(ISD::SIGN_EXTEND_INREG, (MVT::SimpleValueType)VT,
1234 Custom);
Nadav Rotemd0f3ef82011-07-14 11:11:14 +00001235 }
1236
Evan Cheng6be2c582006-04-05 23:38:46 +00001237 // We want to custom lower some of our intrinsics.
Owen Anderson825b72b2009-08-11 20:47:22 +00001238 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
Benjamin Kramerb9bee042012-07-12 09:31:43 +00001239 setOperationAction(ISD::INTRINSIC_W_CHAIN, MVT::Other, Custom);
Evan Cheng6be2c582006-04-05 23:38:46 +00001240
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00001241
Eli Friedman962f5492010-06-02 19:35:46 +00001242 // Only custom-lower 64-bit SADDO and friends on 64-bit because we don't
1243 // handle type legalization for these operations here.
Dan Gohman71c62a22010-06-02 19:13:40 +00001244 //
Eli Friedman962f5492010-06-02 19:35:46 +00001245 // FIXME: We really should do custom legalization for addition and
1246 // subtraction on x86-32 once PR3203 is fixed. We really can't do much better
1247 // than generic legalization for 64-bit multiplication-with-overflow, though.
Chris Lattnera34b3cf2010-12-19 20:03:11 +00001248 for (unsigned i = 0, e = 3+Subtarget->is64Bit(); i != e; ++i) {
1249 // Add/Sub/Mul with overflow operations are custom lowered.
1250 MVT VT = IntVTs[i];
1251 setOperationAction(ISD::SADDO, VT, Custom);
1252 setOperationAction(ISD::UADDO, VT, Custom);
1253 setOperationAction(ISD::SSUBO, VT, Custom);
1254 setOperationAction(ISD::USUBO, VT, Custom);
1255 setOperationAction(ISD::SMULO, VT, Custom);
1256 setOperationAction(ISD::UMULO, VT, Custom);
Eli Friedmana993f0a2010-06-02 00:27:18 +00001257 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00001258
Chris Lattnera34b3cf2010-12-19 20:03:11 +00001259 // There are no 8-bit 3-address imul/mul instructions
1260 setOperationAction(ISD::SMULO, MVT::i8, Expand);
1261 setOperationAction(ISD::UMULO, MVT::i8, Expand);
Bill Wendling41ea7e72008-11-24 19:21:46 +00001262
Evan Chengd54f2d52009-03-31 19:38:51 +00001263 if (!Subtarget->is64Bit()) {
1264 // These libcalls are not available in 32-bit.
1265 setLibcallName(RTLIB::SHL_I128, 0);
1266 setLibcallName(RTLIB::SRL_I128, 0);
1267 setLibcallName(RTLIB::SRA_I128, 0);
1268 }
1269
Evan Cheng206ee9d2006-07-07 08:33:52 +00001270 // We have target-specific dag combine patterns for the following nodes:
1271 setTargetDAGCombine(ISD::VECTOR_SHUFFLE);
Dan Gohman1bbf72b2010-03-15 23:23:03 +00001272 setTargetDAGCombine(ISD::EXTRACT_VECTOR_ELT);
Duncan Sands6bcd2192011-09-17 16:49:39 +00001273 setTargetDAGCombine(ISD::VSELECT);
Chris Lattner83e6c992006-10-04 06:57:07 +00001274 setTargetDAGCombine(ISD::SELECT);
Nate Begeman740ab032009-01-26 00:52:55 +00001275 setTargetDAGCombine(ISD::SHL);
1276 setTargetDAGCombine(ISD::SRA);
1277 setTargetDAGCombine(ISD::SRL);
Evan Cheng760d1942010-01-04 21:22:48 +00001278 setTargetDAGCombine(ISD::OR);
Nate Begemanb65c1752010-12-17 22:55:37 +00001279 setTargetDAGCombine(ISD::AND);
Benjamin Kramer7d6fe132010-12-21 21:41:44 +00001280 setTargetDAGCombine(ISD::ADD);
Duncan Sands17470be2011-09-22 20:15:48 +00001281 setTargetDAGCombine(ISD::FADD);
1282 setTargetDAGCombine(ISD::FSUB);
Elena Demikhovsky1503aba2012-08-01 12:06:00 +00001283 setTargetDAGCombine(ISD::FMA);
Benjamin Kramer7d6fe132010-12-21 21:41:44 +00001284 setTargetDAGCombine(ISD::SUB);
Nadav Rotem91e43fd2011-09-18 10:39:32 +00001285 setTargetDAGCombine(ISD::LOAD);
Chris Lattner149a4e52008-02-22 02:09:43 +00001286 setTargetDAGCombine(ISD::STORE);
Evan Cheng2e489c42009-12-16 00:53:11 +00001287 setTargetDAGCombine(ISD::ZERO_EXTEND);
Elena Demikhovsky1da58672012-04-22 09:39:03 +00001288 setTargetDAGCombine(ISD::ANY_EXTEND);
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +00001289 setTargetDAGCombine(ISD::SIGN_EXTEND);
Elena Demikhovsky3ae98152012-02-01 07:56:44 +00001290 setTargetDAGCombine(ISD::TRUNCATE);
Stuart Hastingsf99a4b82011-06-06 23:15:58 +00001291 setTargetDAGCombine(ISD::SINT_TO_FP);
Chad Rosiera73b6fc2012-04-27 22:33:25 +00001292 setTargetDAGCombine(ISD::SETCC);
Evan Cheng0b0cd912009-03-28 05:57:29 +00001293 if (Subtarget->is64Bit())
1294 setTargetDAGCombine(ISD::MUL);
Manman Ren92363622012-06-07 22:39:10 +00001295 setTargetDAGCombine(ISD::XOR);
Evan Cheng206ee9d2006-07-07 08:33:52 +00001296
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001297 computeRegisterProperties();
1298
Evan Cheng05219282011-01-06 06:52:41 +00001299 // On Darwin, -Os means optimize for size without hurting performance,
1300 // do not reduce the limit.
Dan Gohman87060f52008-06-30 21:00:56 +00001301 maxStoresPerMemset = 16; // For @llvm.memset -> sequence of stores
Evan Cheng05219282011-01-06 06:52:41 +00001302 maxStoresPerMemsetOptSize = Subtarget->isTargetDarwin() ? 16 : 8;
Evan Cheng255f20f2010-04-01 06:04:33 +00001303 maxStoresPerMemcpy = 8; // For @llvm.memcpy -> sequence of stores
Evan Cheng05219282011-01-06 06:52:41 +00001304 maxStoresPerMemcpyOptSize = Subtarget->isTargetDarwin() ? 8 : 4;
1305 maxStoresPerMemmove = 8; // For @llvm.memmove -> sequence of stores
1306 maxStoresPerMemmoveOptSize = Subtarget->isTargetDarwin() ? 8 : 4;
Jakob Stoklund Olesen8c741b82011-12-06 01:26:19 +00001307 setPrefLoopAlignment(4); // 2^4 bytes.
Evan Cheng6ebf7bc2009-05-13 21:42:09 +00001308 benefitFromCodePlacementOpt = true;
Eli Friedmanfc5d3052011-05-06 20:34:06 +00001309
Benjamin Krameraaf723d2012-05-05 12:49:14 +00001310 // Predictable cmov don't hurt on atom because it's in-order.
1311 predictableSelectIsExpensive = !Subtarget->isAtom();
1312
Jakob Stoklund Olesen8c741b82011-12-06 01:26:19 +00001313 setPrefFunctionAlignment(4); // 2^4 bytes.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001314}
1315
Scott Michel5b8f82e2008-03-10 15:42:14 +00001316
Duncan Sands28b77e92011-09-06 19:07:46 +00001317EVT X86TargetLowering::getSetCCResultType(EVT VT) const {
1318 if (!VT.isVector()) return MVT::i8;
1319 return VT.changeVectorElementTypeToInteger();
Scott Michel5b8f82e2008-03-10 15:42:14 +00001320}
1321
1322
Evan Cheng29286502008-01-23 23:17:41 +00001323/// getMaxByValAlign - Helper for getByValTypeAlignment to determine
1324/// the desired ByVal argument alignment.
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001325static void getMaxByValAlign(Type *Ty, unsigned &MaxAlign) {
Evan Cheng29286502008-01-23 23:17:41 +00001326 if (MaxAlign == 16)
1327 return;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001328 if (VectorType *VTy = dyn_cast<VectorType>(Ty)) {
Evan Cheng29286502008-01-23 23:17:41 +00001329 if (VTy->getBitWidth() == 128)
1330 MaxAlign = 16;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001331 } else if (ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
Evan Cheng29286502008-01-23 23:17:41 +00001332 unsigned EltAlign = 0;
1333 getMaxByValAlign(ATy->getElementType(), EltAlign);
1334 if (EltAlign > MaxAlign)
1335 MaxAlign = EltAlign;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001336 } else if (StructType *STy = dyn_cast<StructType>(Ty)) {
Evan Cheng29286502008-01-23 23:17:41 +00001337 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
1338 unsigned EltAlign = 0;
1339 getMaxByValAlign(STy->getElementType(i), EltAlign);
1340 if (EltAlign > MaxAlign)
1341 MaxAlign = EltAlign;
1342 if (MaxAlign == 16)
1343 break;
1344 }
1345 }
Evan Cheng29286502008-01-23 23:17:41 +00001346}
1347
1348/// getByValTypeAlignment - Return the desired alignment for ByVal aggregate
1349/// function arguments in the caller parameter area. For X86, aggregates
Dale Johannesen0c191872008-02-08 19:48:20 +00001350/// that contain SSE vectors are placed at 16-byte boundaries while the rest
1351/// are at 4-byte boundaries.
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001352unsigned X86TargetLowering::getByValTypeAlignment(Type *Ty) const {
Evan Cheng1887c1c2008-08-21 21:00:15 +00001353 if (Subtarget->is64Bit()) {
1354 // Max of 8 and alignment of type.
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00001355 unsigned TyAlign = TD->getABITypeAlignment(Ty);
Evan Cheng1887c1c2008-08-21 21:00:15 +00001356 if (TyAlign > 8)
1357 return TyAlign;
1358 return 8;
1359 }
1360
Evan Cheng29286502008-01-23 23:17:41 +00001361 unsigned Align = 4;
Craig Topper1accb7e2012-01-10 06:54:16 +00001362 if (Subtarget->hasSSE1())
Dale Johannesen0c191872008-02-08 19:48:20 +00001363 getMaxByValAlign(Ty, Align);
Evan Cheng29286502008-01-23 23:17:41 +00001364 return Align;
1365}
Chris Lattner2b02a442007-02-25 08:29:00 +00001366
Evan Chengf0df0312008-05-15 08:39:06 +00001367/// getOptimalMemOpType - Returns the target specific optimal type for load
Evan Chengc3b0c342010-04-08 07:37:57 +00001368/// and store operations as a result of memset, memcpy, and memmove
1369/// lowering. If DstAlign is zero that means it's safe to destination
1370/// alignment can satisfy any constraint. Similarly if SrcAlign is zero it
1371/// means there isn't a need to check it against alignment requirement,
1372/// probably because the source does not need to be loaded. If
Lang Hames15701f82011-10-26 23:50:43 +00001373/// 'IsZeroVal' is true, that means it's safe to return a
Evan Chengc3b0c342010-04-08 07:37:57 +00001374/// non-scalar-integer type, e.g. empty string source, constant, or loaded
1375/// from memory. 'MemcpyStrSrc' indicates whether the memcpy source is
1376/// constant so it does not need to be loaded.
Dan Gohman37f32ee2010-04-16 20:11:05 +00001377/// It returns EVT::Other if the type should be determined using generic
1378/// target-independent logic.
Owen Andersone50ed302009-08-10 22:56:29 +00001379EVT
Evan Cheng255f20f2010-04-01 06:04:33 +00001380X86TargetLowering::getOptimalMemOpType(uint64_t Size,
1381 unsigned DstAlign, unsigned SrcAlign,
Lang Hames15701f82011-10-26 23:50:43 +00001382 bool IsZeroVal,
Evan Chengc3b0c342010-04-08 07:37:57 +00001383 bool MemcpyStrSrc,
Dan Gohman37f32ee2010-04-16 20:11:05 +00001384 MachineFunction &MF) const {
Dan Gohman37f32ee2010-04-16 20:11:05 +00001385 const Function *F = MF.getFunction();
Lang Hames15701f82011-10-26 23:50:43 +00001386 if (IsZeroVal &&
Bill Wendling67658342012-10-09 07:45:08 +00001387 !F->getFnAttributes().hasAttribute(Attributes::NoImplicitFloat)) {
Evan Cheng255f20f2010-04-01 06:04:33 +00001388 if (Size >= 16 &&
Evan Chenga5e13622011-01-07 19:35:30 +00001389 (Subtarget->isUnalignedMemAccessFast() ||
1390 ((DstAlign == 0 || DstAlign >= 16) &&
Benjamin Kramer2dbe9292012-11-14 20:08:40 +00001391 (SrcAlign == 0 || SrcAlign >= 16)))) {
1392 if (Size >= 32) {
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00001393 if (Subtarget->hasInt256())
Craig Topper562659f2012-01-13 08:32:21 +00001394 return MVT::v8i32;
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00001395 if (Subtarget->hasFp256())
Craig Topper562659f2012-01-13 08:32:21 +00001396 return MVT::v8f32;
1397 }
Craig Topper1accb7e2012-01-10 06:54:16 +00001398 if (Subtarget->hasSSE2())
Evan Cheng255f20f2010-04-01 06:04:33 +00001399 return MVT::v4i32;
Craig Topper1accb7e2012-01-10 06:54:16 +00001400 if (Subtarget->hasSSE1())
Evan Cheng255f20f2010-04-01 06:04:33 +00001401 return MVT::v4f32;
Evan Chengc3b0c342010-04-08 07:37:57 +00001402 } else if (!MemcpyStrSrc && Size >= 8 &&
Evan Cheng3ea97552010-04-01 20:27:45 +00001403 !Subtarget->is64Bit() &&
Craig Topper1accb7e2012-01-10 06:54:16 +00001404 Subtarget->hasSSE2()) {
Evan Chengc3b0c342010-04-08 07:37:57 +00001405 // Do not use f64 to lower memcpy if source is string constant. It's
1406 // better to use i32 to avoid the loads.
Evan Cheng255f20f2010-04-01 06:04:33 +00001407 return MVT::f64;
Evan Chengc3b0c342010-04-08 07:37:57 +00001408 }
Chris Lattner4002a1b2008-10-28 05:49:35 +00001409 }
Evan Chengf0df0312008-05-15 08:39:06 +00001410 if (Subtarget->is64Bit() && Size >= 8)
Owen Anderson825b72b2009-08-11 20:47:22 +00001411 return MVT::i64;
1412 return MVT::i32;
Evan Chengf0df0312008-05-15 08:39:06 +00001413}
1414
Chris Lattner5e1df8d2010-01-25 23:38:14 +00001415/// getJumpTableEncoding - Return the entry encoding for a jump table in the
1416/// current function. The returned value is a member of the
1417/// MachineJumpTableInfo::JTEntryKind enum.
1418unsigned X86TargetLowering::getJumpTableEncoding() const {
1419 // In GOT pic mode, each entry in the jump table is emitted as a @GOTOFF
1420 // symbol.
1421 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1422 Subtarget->isPICStyleGOT())
Chris Lattnerc64daab2010-01-26 05:02:42 +00001423 return MachineJumpTableInfo::EK_Custom32;
Michael J. Spencerec38de22010-10-10 22:04:20 +00001424
Chris Lattner5e1df8d2010-01-25 23:38:14 +00001425 // Otherwise, use the normal jump table encoding heuristics.
1426 return TargetLowering::getJumpTableEncoding();
1427}
1428
Chris Lattnerc64daab2010-01-26 05:02:42 +00001429const MCExpr *
1430X86TargetLowering::LowerCustomJumpTableEntry(const MachineJumpTableInfo *MJTI,
1431 const MachineBasicBlock *MBB,
1432 unsigned uid,MCContext &Ctx) const{
1433 assert(getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1434 Subtarget->isPICStyleGOT());
1435 // In 32-bit ELF systems, our jump table entries are formed with @GOTOFF
1436 // entries.
Daniel Dunbar4e815f82010-03-15 23:51:06 +00001437 return MCSymbolRefExpr::Create(MBB->getSymbol(),
1438 MCSymbolRefExpr::VK_GOTOFF, Ctx);
Chris Lattnerc64daab2010-01-26 05:02:42 +00001439}
1440
Evan Chengcc415862007-11-09 01:32:10 +00001441/// getPICJumpTableRelocaBase - Returns relocation base for the given PIC
1442/// jumptable.
Dan Gohman475871a2008-07-27 21:46:04 +00001443SDValue X86TargetLowering::getPICJumpTableRelocBase(SDValue Table,
Chris Lattner589c6f62010-01-26 06:28:43 +00001444 SelectionDAG &DAG) const {
Chris Lattnere4df7562009-07-09 03:15:51 +00001445 if (!Subtarget->is64Bit())
Dale Johannesenb300d2a2009-02-07 00:55:49 +00001446 // This doesn't have DebugLoc associated with it, but is not really the
1447 // same as a Register.
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00001448 return DAG.getNode(X86ISD::GlobalBaseReg, DebugLoc(), getPointerTy());
Evan Chengcc415862007-11-09 01:32:10 +00001449 return Table;
1450}
1451
Chris Lattner589c6f62010-01-26 06:28:43 +00001452/// getPICJumpTableRelocBaseExpr - This returns the relocation base for the
1453/// given PIC jumptable, the same as getPICJumpTableRelocBase, but as an
1454/// MCExpr.
1455const MCExpr *X86TargetLowering::
1456getPICJumpTableRelocBaseExpr(const MachineFunction *MF, unsigned JTI,
1457 MCContext &Ctx) const {
1458 // X86-64 uses RIP relative addressing based on the jump table label.
1459 if (Subtarget->isPICStyleRIPRel())
1460 return TargetLowering::getPICJumpTableRelocBaseExpr(MF, JTI, Ctx);
1461
1462 // Otherwise, the reference is relative to the PIC base.
Chris Lattner142b5312010-11-14 22:48:15 +00001463 return MCSymbolRefExpr::Create(MF->getPICBaseSymbol(), Ctx);
Chris Lattner589c6f62010-01-26 06:28:43 +00001464}
1465
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00001466// FIXME: Why this routine is here? Move to RegInfo!
Evan Chengdee81012010-07-26 21:50:05 +00001467std::pair<const TargetRegisterClass*, uint8_t>
1468X86TargetLowering::findRepresentativeClass(EVT VT) const{
1469 const TargetRegisterClass *RRC = 0;
1470 uint8_t Cost = 1;
1471 switch (VT.getSimpleVT().SimpleTy) {
1472 default:
1473 return TargetLowering::findRepresentativeClass(VT);
1474 case MVT::i8: case MVT::i16: case MVT::i32: case MVT::i64:
Craig Topperc9099502012-04-20 06:31:50 +00001475 RRC = Subtarget->is64Bit() ?
1476 (const TargetRegisterClass*)&X86::GR64RegClass :
1477 (const TargetRegisterClass*)&X86::GR32RegClass;
Evan Chengdee81012010-07-26 21:50:05 +00001478 break;
Dale Johannesen0488fb62010-09-30 23:57:10 +00001479 case MVT::x86mmx:
Craig Topperc9099502012-04-20 06:31:50 +00001480 RRC = &X86::VR64RegClass;
Evan Chengdee81012010-07-26 21:50:05 +00001481 break;
1482 case MVT::f32: case MVT::f64:
1483 case MVT::v16i8: case MVT::v8i16: case MVT::v4i32: case MVT::v2i64:
1484 case MVT::v4f32: case MVT::v2f64:
1485 case MVT::v32i8: case MVT::v8i32: case MVT::v4i64: case MVT::v8f32:
1486 case MVT::v4f64:
Craig Topperc9099502012-04-20 06:31:50 +00001487 RRC = &X86::VR128RegClass;
Evan Chengdee81012010-07-26 21:50:05 +00001488 break;
1489 }
1490 return std::make_pair(RRC, Cost);
1491}
1492
Eric Christopherf7a0c7b2010-07-06 05:18:56 +00001493bool X86TargetLowering::getStackCookieLocation(unsigned &AddressSpace,
1494 unsigned &Offset) const {
1495 if (!Subtarget->isTargetLinux())
1496 return false;
1497
1498 if (Subtarget->is64Bit()) {
1499 // %fs:0x28, unless we're using a Kernel code model, in which case it's %gs:
1500 Offset = 0x28;
1501 if (getTargetMachine().getCodeModel() == CodeModel::Kernel)
1502 AddressSpace = 256;
1503 else
1504 AddressSpace = 257;
1505 } else {
1506 // %gs:0x14 on i386
1507 Offset = 0x14;
1508 AddressSpace = 256;
1509 }
1510 return true;
1511}
1512
1513
Chris Lattner2b02a442007-02-25 08:29:00 +00001514//===----------------------------------------------------------------------===//
1515// Return Value Calling Convention Implementation
1516//===----------------------------------------------------------------------===//
1517
Chris Lattner59ed56b2007-02-28 04:55:35 +00001518#include "X86GenCallingConv.inc"
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001519
Michael J. Spencerec38de22010-10-10 22:04:20 +00001520bool
Eric Christopher471e4222011-06-08 23:55:35 +00001521X86TargetLowering::CanLowerReturn(CallingConv::ID CallConv,
Craig Topper0fbf3642012-04-23 03:28:34 +00001522 MachineFunction &MF, bool isVarArg,
Dan Gohman84023e02010-07-10 09:00:22 +00001523 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9af33c2010-07-06 22:19:37 +00001524 LLVMContext &Context) const {
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +00001525 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00001526 CCState CCInfo(CallConv, isVarArg, MF, getTargetMachine(),
Dan Gohmanc9af33c2010-07-06 22:19:37 +00001527 RVLocs, Context);
Dan Gohman84023e02010-07-10 09:00:22 +00001528 return CCInfo.CheckReturn(Outs, RetCC_X86);
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +00001529}
1530
Dan Gohman98ca4f22009-08-05 01:29:28 +00001531SDValue
1532X86TargetLowering::LowerReturn(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001533 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001534 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001535 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohmand858e902010-04-17 15:26:15 +00001536 DebugLoc dl, SelectionDAG &DAG) const {
Dan Gohman1e93df62010-04-17 14:41:14 +00001537 MachineFunction &MF = DAG.getMachineFunction();
1538 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
Scott Michelfdc40a02009-02-17 22:15:04 +00001539
Chris Lattner9774c912007-02-27 05:28:59 +00001540 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00001541 CCState CCInfo(CallConv, isVarArg, MF, getTargetMachine(),
Dan Gohman98ca4f22009-08-05 01:29:28 +00001542 RVLocs, *DAG.getContext());
1543 CCInfo.AnalyzeReturn(Outs, RetCC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001544
Evan Chengdcea1632010-02-04 02:40:39 +00001545 // Add the regs to the liveout set for the function.
1546 MachineRegisterInfo &MRI = DAG.getMachineFunction().getRegInfo();
1547 for (unsigned i = 0; i != RVLocs.size(); ++i)
1548 if (RVLocs[i].isRegLoc() && !MRI.isLiveOut(RVLocs[i].getLocReg()))
1549 MRI.addLiveOut(RVLocs[i].getLocReg());
Scott Michelfdc40a02009-02-17 22:15:04 +00001550
Dan Gohman475871a2008-07-27 21:46:04 +00001551 SDValue Flag;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001552
Dan Gohman475871a2008-07-27 21:46:04 +00001553 SmallVector<SDValue, 6> RetOps;
Chris Lattner447ff682008-03-11 03:23:40 +00001554 RetOps.push_back(Chain); // Operand #0 = Chain (updated below)
1555 // Operand #1 = Bytes To Pop
Dan Gohman1e93df62010-04-17 14:41:14 +00001556 RetOps.push_back(DAG.getTargetConstant(FuncInfo->getBytesToPopOnReturn(),
1557 MVT::i16));
Scott Michelfdc40a02009-02-17 22:15:04 +00001558
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001559 // Copy the result values into the output registers.
Chris Lattner8e6da152008-03-10 21:08:41 +00001560 for (unsigned i = 0; i != RVLocs.size(); ++i) {
1561 CCValAssign &VA = RVLocs[i];
1562 assert(VA.isRegLoc() && "Can only return in registers!");
Dan Gohmanc9403652010-07-07 15:54:55 +00001563 SDValue ValToCopy = OutVals[i];
Dale Johannesenc76d23f2010-07-23 00:30:35 +00001564 EVT ValVT = ValToCopy.getValueType();
1565
Jakob Stoklund Olesenee66b412012-05-31 17:28:20 +00001566 // Promote values to the appropriate types
1567 if (VA.getLocInfo() == CCValAssign::SExt)
1568 ValToCopy = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), ValToCopy);
1569 else if (VA.getLocInfo() == CCValAssign::ZExt)
1570 ValToCopy = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), ValToCopy);
1571 else if (VA.getLocInfo() == CCValAssign::AExt)
1572 ValToCopy = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), ValToCopy);
1573 else if (VA.getLocInfo() == CCValAssign::BCvt)
1574 ValToCopy = DAG.getNode(ISD::BITCAST, dl, VA.getLocVT(), ValToCopy);
1575
Dale Johannesenc4510512010-09-24 19:05:48 +00001576 // If this is x86-64, and we disabled SSE, we can't return FP values,
1577 // or SSE or MMX vectors.
1578 if ((ValVT == MVT::f32 || ValVT == MVT::f64 ||
1579 VA.getLocReg() == X86::XMM0 || VA.getLocReg() == X86::XMM1) &&
Craig Topper1accb7e2012-01-10 06:54:16 +00001580 (Subtarget->is64Bit() && !Subtarget->hasSSE1())) {
Dale Johannesenc76d23f2010-07-23 00:30:35 +00001581 report_fatal_error("SSE register return with SSE disabled");
1582 }
1583 // Likewise we can't return F64 values with SSE1 only. gcc does so, but
1584 // llvm-gcc has never done it right and no one has noticed, so this
1585 // should be OK for now.
1586 if (ValVT == MVT::f64 &&
Craig Topper1accb7e2012-01-10 06:54:16 +00001587 (Subtarget->is64Bit() && !Subtarget->hasSSE2()))
Dale Johannesenc76d23f2010-07-23 00:30:35 +00001588 report_fatal_error("SSE2 register return with SSE2 disabled");
Scott Michelfdc40a02009-02-17 22:15:04 +00001589
Chris Lattner447ff682008-03-11 03:23:40 +00001590 // Returns in ST0/ST1 are handled specially: these are pushed as operands to
1591 // the RET instruction and handled by the FP Stackifier.
Dan Gohman37eed792009-02-04 17:28:58 +00001592 if (VA.getLocReg() == X86::ST0 ||
1593 VA.getLocReg() == X86::ST1) {
Chris Lattner447ff682008-03-11 03:23:40 +00001594 // If this is a copy from an xmm register to ST(0), use an FPExtend to
1595 // change the value to the FP stack register class.
Dan Gohman37eed792009-02-04 17:28:58 +00001596 if (isScalarFPTypeInSSEReg(VA.getValVT()))
Owen Anderson825b72b2009-08-11 20:47:22 +00001597 ValToCopy = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f80, ValToCopy);
Chris Lattner447ff682008-03-11 03:23:40 +00001598 RetOps.push_back(ValToCopy);
1599 // Don't emit a copytoreg.
1600 continue;
1601 }
Dale Johannesena68f9012008-06-24 22:01:44 +00001602
Evan Cheng242b38b2009-02-23 09:03:22 +00001603 // 64-bit vector (MMX) values are returned in XMM0 / XMM1 except for v1i64
1604 // which is returned in RAX / RDX.
Evan Cheng6140a8b2009-02-22 08:05:12 +00001605 if (Subtarget->is64Bit()) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00001606 if (ValVT == MVT::x86mmx) {
Chris Lattner97a2a562010-08-26 05:24:29 +00001607 if (VA.getLocReg() == X86::XMM0 || VA.getLocReg() == X86::XMM1) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001608 ValToCopy = DAG.getNode(ISD::BITCAST, dl, MVT::i64, ValToCopy);
Eric Christopher90eb4022010-07-22 00:26:08 +00001609 ValToCopy = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i64,
1610 ValToCopy);
Chris Lattner97a2a562010-08-26 05:24:29 +00001611 // If we don't have SSE2 available, convert to v4f32 so the generated
1612 // register is legal.
Craig Topper1accb7e2012-01-10 06:54:16 +00001613 if (!Subtarget->hasSSE2())
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001614 ValToCopy = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32,ValToCopy);
Chris Lattner97a2a562010-08-26 05:24:29 +00001615 }
Evan Cheng242b38b2009-02-23 09:03:22 +00001616 }
Evan Cheng6140a8b2009-02-22 08:05:12 +00001617 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00001618
Dale Johannesendd64c412009-02-04 00:33:20 +00001619 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), ValToCopy, Flag);
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001620 Flag = Chain.getValue(1);
1621 }
Dan Gohman61a92132008-04-21 23:59:07 +00001622
1623 // The x86-64 ABI for returning structs by value requires that we copy
1624 // the sret argument into %rax for the return. We saved the argument into
1625 // a virtual register in the entry block, so now we copy the value out
1626 // and into %rax.
1627 if (Subtarget->is64Bit() &&
1628 DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
1629 MachineFunction &MF = DAG.getMachineFunction();
1630 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1631 unsigned Reg = FuncInfo->getSRetReturnReg();
Michael J. Spencerec38de22010-10-10 22:04:20 +00001632 assert(Reg &&
Zhongxing Xuc2798a12010-05-26 08:10:02 +00001633 "SRetReturnReg should have been set in LowerFormalArguments().");
Dale Johannesendd64c412009-02-04 00:33:20 +00001634 SDValue Val = DAG.getCopyFromReg(Chain, dl, Reg, getPointerTy());
Dan Gohman61a92132008-04-21 23:59:07 +00001635
Dale Johannesendd64c412009-02-04 00:33:20 +00001636 Chain = DAG.getCopyToReg(Chain, dl, X86::RAX, Val, Flag);
Dan Gohman61a92132008-04-21 23:59:07 +00001637 Flag = Chain.getValue(1);
Dan Gohman00326812009-10-12 16:36:12 +00001638
1639 // RAX now acts like a return value.
Evan Chengdcea1632010-02-04 02:40:39 +00001640 MRI.addLiveOut(X86::RAX);
Dan Gohman61a92132008-04-21 23:59:07 +00001641 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001642
Chris Lattner447ff682008-03-11 03:23:40 +00001643 RetOps[0] = Chain; // Update chain.
1644
1645 // Add the flag if we have it.
Gabor Greifba36cb52008-08-28 21:40:38 +00001646 if (Flag.getNode())
Chris Lattner447ff682008-03-11 03:23:40 +00001647 RetOps.push_back(Flag);
Scott Michelfdc40a02009-02-17 22:15:04 +00001648
1649 return DAG.getNode(X86ISD::RET_FLAG, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001650 MVT::Other, &RetOps[0], RetOps.size());
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001651}
1652
Evan Chengbf010eb2012-04-10 01:51:00 +00001653bool X86TargetLowering::isUsedByReturnOnly(SDNode *N, SDValue &Chain) const {
Evan Cheng3d2125c2010-11-30 23:55:39 +00001654 if (N->getNumValues() != 1)
1655 return false;
1656 if (!N->hasNUsesOfValue(1, 0))
1657 return false;
1658
Evan Chengbf010eb2012-04-10 01:51:00 +00001659 SDValue TCChain = Chain;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001660 SDNode *Copy = *N->use_begin();
Chad Rosierc8d7eea2012-03-05 19:27:12 +00001661 if (Copy->getOpcode() == ISD::CopyToReg) {
1662 // If the copy has a glue operand, we conservatively assume it isn't safe to
1663 // perform a tail call.
1664 if (Copy->getOperand(Copy->getNumOperands()-1).getValueType() == MVT::Glue)
1665 return false;
Evan Chengbf010eb2012-04-10 01:51:00 +00001666 TCChain = Copy->getOperand(0);
Chad Rosierc8d7eea2012-03-05 19:27:12 +00001667 } else if (Copy->getOpcode() != ISD::FP_EXTEND)
Chad Rosier74bab7f2012-03-02 02:50:46 +00001668 return false;
1669
Evan Cheng1bf891a2010-12-01 22:59:46 +00001670 bool HasRet = false;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001671 for (SDNode::use_iterator UI = Copy->use_begin(), UE = Copy->use_end();
Evan Cheng1bf891a2010-12-01 22:59:46 +00001672 UI != UE; ++UI) {
Evan Cheng3d2125c2010-11-30 23:55:39 +00001673 if (UI->getOpcode() != X86ISD::RET_FLAG)
1674 return false;
Evan Cheng1bf891a2010-12-01 22:59:46 +00001675 HasRet = true;
1676 }
Evan Cheng3d2125c2010-11-30 23:55:39 +00001677
Evan Chengbf010eb2012-04-10 01:51:00 +00001678 if (!HasRet)
1679 return false;
1680
1681 Chain = TCChain;
1682 return true;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001683}
1684
Cameron Zwarich7bbf0ee2011-03-17 14:53:37 +00001685EVT
1686X86TargetLowering::getTypeForExtArgOrReturn(LLVMContext &Context, EVT VT,
Cameron Zwarich44579682011-03-17 14:21:56 +00001687 ISD::NodeType ExtendKind) const {
Cameron Zwarich7bbf0ee2011-03-17 14:53:37 +00001688 MVT ReturnMVT;
Cameron Zwarichebe81732011-03-16 22:20:18 +00001689 // TODO: Is this also valid on 32-bit?
1690 if (Subtarget->is64Bit() && VT == MVT::i1 && ExtendKind == ISD::ZERO_EXTEND)
Cameron Zwarich7bbf0ee2011-03-17 14:53:37 +00001691 ReturnMVT = MVT::i8;
1692 else
1693 ReturnMVT = MVT::i32;
1694
1695 EVT MinVT = getRegisterType(Context, ReturnMVT);
1696 return VT.bitsLT(MinVT) ? MinVT : VT;
Cameron Zwarichebe81732011-03-16 22:20:18 +00001697}
1698
Dan Gohman98ca4f22009-08-05 01:29:28 +00001699/// LowerCallResult - Lower the result values of a call into the
1700/// appropriate copies out of appropriate physical registers.
1701///
1702SDValue
1703X86TargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001704 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001705 const SmallVectorImpl<ISD::InputArg> &Ins,
1706 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001707 SmallVectorImpl<SDValue> &InVals) const {
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001708
Chris Lattnere32bbf62007-02-28 07:09:55 +00001709 // Assign locations to each value returned by this call.
Chris Lattner9774c912007-02-27 05:28:59 +00001710 SmallVector<CCValAssign, 16> RVLocs;
Torok Edwin3f142c32009-02-01 18:15:56 +00001711 bool Is64Bit = Subtarget->is64Bit();
Eric Christopher471e4222011-06-08 23:55:35 +00001712 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
Craig Topper0fbf3642012-04-23 03:28:34 +00001713 getTargetMachine(), RVLocs, *DAG.getContext());
Dan Gohman98ca4f22009-08-05 01:29:28 +00001714 CCInfo.AnalyzeCallResult(Ins, RetCC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001715
Chris Lattner3085e152007-02-25 08:59:22 +00001716 // Copy all of the result registers out of their specified physreg.
Chris Lattner8e6da152008-03-10 21:08:41 +00001717 for (unsigned i = 0; i != RVLocs.size(); ++i) {
Dan Gohman37eed792009-02-04 17:28:58 +00001718 CCValAssign &VA = RVLocs[i];
Owen Andersone50ed302009-08-10 22:56:29 +00001719 EVT CopyVT = VA.getValVT();
Scott Michelfdc40a02009-02-17 22:15:04 +00001720
Torok Edwin3f142c32009-02-01 18:15:56 +00001721 // If this is x86-64, and we disabled SSE, we can't return FP values
Owen Anderson825b72b2009-08-11 20:47:22 +00001722 if ((CopyVT == MVT::f32 || CopyVT == MVT::f64) &&
Craig Topper1accb7e2012-01-10 06:54:16 +00001723 ((Is64Bit || Ins[i].Flags.isInReg()) && !Subtarget->hasSSE1())) {
Chris Lattner75361b62010-04-07 22:58:41 +00001724 report_fatal_error("SSE register return with SSE disabled");
Torok Edwin3f142c32009-02-01 18:15:56 +00001725 }
1726
Evan Cheng79fb3b42009-02-20 20:43:02 +00001727 SDValue Val;
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001728
1729 // If this is a call to a function that returns an fp value on the floating
Sylvestre Ledruc8e41c52012-07-23 08:51:15 +00001730 // point stack, we must guarantee the value is popped from the stack, so
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001731 // a CopyFromReg is not good enough - the copy instruction may be eliminated
Jakob Stoklund Olesen9bbe4d62011-06-28 18:32:28 +00001732 // if the return value is not used. We use the FpPOP_RETVAL instruction
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001733 // instead.
1734 if (VA.getLocReg() == X86::ST0 || VA.getLocReg() == X86::ST1) {
1735 // If we prefer to use the value in xmm registers, copy it out as f80 and
1736 // use a truncate to move it from fp stack reg to xmm reg.
1737 if (isScalarFPTypeInSSEReg(VA.getValVT())) CopyVT = MVT::f80;
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001738 SDValue Ops[] = { Chain, InFlag };
Jakob Stoklund Olesen9bbe4d62011-06-28 18:32:28 +00001739 Chain = SDValue(DAG.getMachineNode(X86::FpPOP_RETVAL, dl, CopyVT,
1740 MVT::Other, MVT::Glue, Ops, 2), 1);
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001741 Val = Chain.getValue(0);
1742
1743 // Round the f80 to the right size, which also moves it to the appropriate
1744 // xmm register.
1745 if (CopyVT != VA.getValVT())
1746 Val = DAG.getNode(ISD::FP_ROUND, dl, VA.getValVT(), Val,
1747 // This truncation won't change the value.
1748 DAG.getIntPtrConstant(1));
Evan Cheng79fb3b42009-02-20 20:43:02 +00001749 } else {
1750 Chain = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(),
1751 CopyVT, InFlag).getValue(1);
1752 Val = Chain.getValue(0);
1753 }
Chris Lattner8e6da152008-03-10 21:08:41 +00001754 InFlag = Chain.getValue(2);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001755 InVals.push_back(Val);
Chris Lattner3085e152007-02-25 08:59:22 +00001756 }
Duncan Sands4bdcb612008-07-02 17:40:58 +00001757
Dan Gohman98ca4f22009-08-05 01:29:28 +00001758 return Chain;
Chris Lattner2b02a442007-02-25 08:29:00 +00001759}
1760
1761
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001762//===----------------------------------------------------------------------===//
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001763// C & StdCall & Fast Calling Convention implementation
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001764//===----------------------------------------------------------------------===//
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001765// StdCall calling convention seems to be standard for many Windows' API
1766// routines and around. It differs from C calling convention just a little:
1767// callee should clean up the stack, not caller. Symbols should be also
1768// decorated in some fancy way :) It doesn't support any vector arguments.
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001769// For info on fast calling convention see Fast Calling Convention (tail call)
1770// implementation LowerX86_32FastCCCallTo.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001771
Dan Gohman98ca4f22009-08-05 01:29:28 +00001772/// CallIsStructReturn - Determines whether a call uses struct return
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001773/// semantics.
Rafael Espindola1cee7102012-07-25 13:41:10 +00001774enum StructReturnType {
1775 NotStructReturn,
1776 RegStructReturn,
1777 StackStructReturn
1778};
1779static StructReturnType
1780callIsStructReturn(const SmallVectorImpl<ISD::OutputArg> &Outs) {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001781 if (Outs.empty())
Rafael Espindola1cee7102012-07-25 13:41:10 +00001782 return NotStructReturn;
Duncan Sands276dcbd2008-03-21 09:14:45 +00001783
Rafael Espindola1cee7102012-07-25 13:41:10 +00001784 const ISD::ArgFlagsTy &Flags = Outs[0].Flags;
1785 if (!Flags.isSRet())
1786 return NotStructReturn;
1787 if (Flags.isInReg())
1788 return RegStructReturn;
1789 return StackStructReturn;
Gordon Henriksen86737662008-01-05 16:56:59 +00001790}
1791
Dan Gohman7e77b0f2009-08-01 19:14:37 +00001792/// ArgsAreStructReturn - Determines whether a function uses struct
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001793/// return semantics.
Rafael Espindola1cee7102012-07-25 13:41:10 +00001794static StructReturnType
1795argsAreStructReturn(const SmallVectorImpl<ISD::InputArg> &Ins) {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001796 if (Ins.empty())
Rafael Espindola1cee7102012-07-25 13:41:10 +00001797 return NotStructReturn;
Duncan Sands276dcbd2008-03-21 09:14:45 +00001798
Rafael Espindola1cee7102012-07-25 13:41:10 +00001799 const ISD::ArgFlagsTy &Flags = Ins[0].Flags;
1800 if (!Flags.isSRet())
1801 return NotStructReturn;
1802 if (Flags.isInReg())
1803 return RegStructReturn;
1804 return StackStructReturn;
Gordon Henriksen86737662008-01-05 16:56:59 +00001805}
1806
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001807/// CreateCopyOfByValArgument - Make a copy of an aggregate at address specified
1808/// by "Src" to address "Dst" with size and alignment information specified by
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001809/// the specific parameter attribute. The copy will be passed as a byval
1810/// function parameter.
Scott Michelfdc40a02009-02-17 22:15:04 +00001811static SDValue
Dan Gohman475871a2008-07-27 21:46:04 +00001812CreateCopyOfByValArgument(SDValue Src, SDValue Dst, SDValue Chain,
Dale Johannesendd64c412009-02-04 00:33:20 +00001813 ISD::ArgFlagsTy Flags, SelectionDAG &DAG,
1814 DebugLoc dl) {
Chris Lattnere72f2022010-09-21 05:40:29 +00001815 SDValue SizeNode = DAG.getConstant(Flags.getByValSize(), MVT::i32);
Michael J. Spencerec38de22010-10-10 22:04:20 +00001816
Dale Johannesendd64c412009-02-04 00:33:20 +00001817 return DAG.getMemcpy(Chain, dl, Dst, Src, SizeNode, Flags.getByValAlign(),
Stuart Hastings03d58262011-03-10 00:25:53 +00001818 /*isVolatile*/false, /*AlwaysInline=*/true,
Chris Lattnerfc448ff2010-09-21 18:51:21 +00001819 MachinePointerInfo(), MachinePointerInfo());
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001820}
1821
Chris Lattner29689432010-03-11 00:22:57 +00001822/// IsTailCallConvention - Return true if the calling convention is one that
1823/// supports tail call optimization.
1824static bool IsTailCallConvention(CallingConv::ID CC) {
Duncan Sandsdc7f1742012-11-16 12:36:39 +00001825 return (CC == CallingConv::Fast || CC == CallingConv::GHC ||
1826 CC == CallingConv::HiPE);
Chris Lattner29689432010-03-11 00:22:57 +00001827}
1828
Evan Cheng485fafc2011-03-21 01:19:09 +00001829bool X86TargetLowering::mayBeEmittedAsTailCall(CallInst *CI) const {
Nick Lewycky22de16d2012-01-19 00:34:10 +00001830 if (!CI->isTailCall() || getTargetMachine().Options.DisableTailCalls)
Evan Cheng485fafc2011-03-21 01:19:09 +00001831 return false;
1832
1833 CallSite CS(CI);
1834 CallingConv::ID CalleeCC = CS.getCallingConv();
1835 if (!IsTailCallConvention(CalleeCC) && CalleeCC != CallingConv::C)
1836 return false;
1837
1838 return true;
1839}
1840
Evan Cheng0c439eb2010-01-27 00:07:07 +00001841/// FuncIsMadeTailCallSafe - Return true if the function is being made into
1842/// a tailcall target by changing its ABI.
Nick Lewycky8a8d4792011-12-02 22:16:29 +00001843static bool FuncIsMadeTailCallSafe(CallingConv::ID CC,
1844 bool GuaranteedTailCallOpt) {
Chris Lattner29689432010-03-11 00:22:57 +00001845 return GuaranteedTailCallOpt && IsTailCallConvention(CC);
Evan Cheng0c439eb2010-01-27 00:07:07 +00001846}
1847
Dan Gohman98ca4f22009-08-05 01:29:28 +00001848SDValue
1849X86TargetLowering::LowerMemArgument(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001850 CallingConv::ID CallConv,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001851 const SmallVectorImpl<ISD::InputArg> &Ins,
1852 DebugLoc dl, SelectionDAG &DAG,
1853 const CCValAssign &VA,
1854 MachineFrameInfo *MFI,
Dan Gohmand858e902010-04-17 15:26:15 +00001855 unsigned i) const {
Rafael Espindola7effac52007-09-14 15:48:13 +00001856 // Create the nodes corresponding to a load from this parameter slot.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001857 ISD::ArgFlagsTy Flags = Ins[i].Flags;
Nick Lewycky8a8d4792011-12-02 22:16:29 +00001858 bool AlwaysUseMutable = FuncIsMadeTailCallSafe(CallConv,
1859 getTargetMachine().Options.GuaranteedTailCallOpt);
Duncan Sands276dcbd2008-03-21 09:14:45 +00001860 bool isImmutable = !AlwaysUseMutable && !Flags.isByVal();
Anton Korobeynikov22472762009-08-14 18:19:10 +00001861 EVT ValVT;
1862
1863 // If value is passed by pointer we have address passed instead of the value
1864 // itself.
1865 if (VA.getLocInfo() == CCValAssign::Indirect)
1866 ValVT = VA.getLocVT();
1867 else
1868 ValVT = VA.getValVT();
Evan Chenge70bb592008-01-10 02:24:25 +00001869
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001870 // FIXME: For now, all byval parameter objects are marked mutable. This can be
Scott Michelfdc40a02009-02-17 22:15:04 +00001871 // changed with more analysis.
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001872 // In case of tail call optimization mark all arguments mutable. Since they
1873 // could be overwritten by lowering of arguments in case of a tail call.
Evan Cheng90567c32010-02-02 23:58:13 +00001874 if (Flags.isByVal()) {
Evan Chengee2e0e32011-03-30 23:44:13 +00001875 unsigned Bytes = Flags.getByValSize();
1876 if (Bytes == 0) Bytes = 1; // Don't create zero-sized stack objects.
1877 int FI = MFI->CreateFixedObject(Bytes, VA.getLocMemOffset(), isImmutable);
Evan Cheng90567c32010-02-02 23:58:13 +00001878 return DAG.getFrameIndex(FI, getPointerTy());
1879 } else {
1880 int FI = MFI->CreateFixedObject(ValVT.getSizeInBits()/8,
Evan Chenged2ae132010-07-03 00:40:23 +00001881 VA.getLocMemOffset(), isImmutable);
Evan Cheng90567c32010-02-02 23:58:13 +00001882 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
1883 return DAG.getLoad(ValVT, dl, Chain, FIN,
Chris Lattnere8639032010-09-21 06:22:23 +00001884 MachinePointerInfo::getFixedStack(FI),
Pete Cooperd752e0f2011-11-08 18:42:53 +00001885 false, false, false, 0);
Evan Cheng90567c32010-02-02 23:58:13 +00001886 }
Rafael Espindola7effac52007-09-14 15:48:13 +00001887}
1888
Dan Gohman475871a2008-07-27 21:46:04 +00001889SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00001890X86TargetLowering::LowerFormalArguments(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001891 CallingConv::ID CallConv,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001892 bool isVarArg,
1893 const SmallVectorImpl<ISD::InputArg> &Ins,
1894 DebugLoc dl,
1895 SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001896 SmallVectorImpl<SDValue> &InVals)
1897 const {
Evan Cheng1bc78042006-04-26 01:20:17 +00001898 MachineFunction &MF = DAG.getMachineFunction();
Gordon Henriksen86737662008-01-05 16:56:59 +00001899 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
Scott Michelfdc40a02009-02-17 22:15:04 +00001900
Gordon Henriksen86737662008-01-05 16:56:59 +00001901 const Function* Fn = MF.getFunction();
1902 if (Fn->hasExternalLinkage() &&
1903 Subtarget->isTargetCygMing() &&
1904 Fn->getName() == "main")
1905 FuncInfo->setForceFramePointer(true);
1906
Evan Cheng1bc78042006-04-26 01:20:17 +00001907 MachineFrameInfo *MFI = MF.getFrameInfo();
Gordon Henriksen86737662008-01-05 16:56:59 +00001908 bool Is64Bit = Subtarget->is64Bit();
Eli Friedman9a2478a2012-01-20 00:05:46 +00001909 bool IsWindows = Subtarget->isTargetWindows();
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001910 bool IsWin64 = Subtarget->isTargetWin64();
Gordon Henriksenae636f82008-01-03 16:47:34 +00001911
Chris Lattner29689432010-03-11 00:22:57 +00001912 assert(!(isVarArg && IsTailCallConvention(CallConv)) &&
Duncan Sandsdc7f1742012-11-16 12:36:39 +00001913 "Var args not supported with calling convention fastcc, ghc or hipe");
Gordon Henriksenae636f82008-01-03 16:47:34 +00001914
Chris Lattner638402b2007-02-28 07:00:42 +00001915 // Assign locations to all of the incoming arguments.
Chris Lattnerf39f7712007-02-28 05:46:49 +00001916 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00001917 CCState CCInfo(CallConv, isVarArg, MF, getTargetMachine(),
Dan Gohman98ca4f22009-08-05 01:29:28 +00001918 ArgLocs, *DAG.getContext());
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00001919
1920 // Allocate shadow area for Win64
1921 if (IsWin64) {
1922 CCInfo.AllocateStack(32, 8);
1923 }
1924
Duncan Sands45907662010-10-31 13:21:44 +00001925 CCInfo.AnalyzeFormalArguments(Ins, CC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001926
Chris Lattnerf39f7712007-02-28 05:46:49 +00001927 unsigned LastVal = ~0U;
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001928 SDValue ArgValue;
Chris Lattnerf39f7712007-02-28 05:46:49 +00001929 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1930 CCValAssign &VA = ArgLocs[i];
1931 // TODO: If an arg is passed in two places (e.g. reg and stack), skip later
1932 // places.
1933 assert(VA.getValNo() != LastVal &&
1934 "Don't support value assigned to multiple locs yet");
Duncan Sands17001ce2011-10-18 12:44:00 +00001935 (void)LastVal;
Chris Lattnerf39f7712007-02-28 05:46:49 +00001936 LastVal = VA.getValNo();
Scott Michelfdc40a02009-02-17 22:15:04 +00001937
Chris Lattnerf39f7712007-02-28 05:46:49 +00001938 if (VA.isRegLoc()) {
Owen Andersone50ed302009-08-10 22:56:29 +00001939 EVT RegVT = VA.getLocVT();
Craig Topper44d23822012-02-22 05:59:10 +00001940 const TargetRegisterClass *RC;
Owen Anderson825b72b2009-08-11 20:47:22 +00001941 if (RegVT == MVT::i32)
Craig Topperc9099502012-04-20 06:31:50 +00001942 RC = &X86::GR32RegClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001943 else if (Is64Bit && RegVT == MVT::i64)
Craig Topperc9099502012-04-20 06:31:50 +00001944 RC = &X86::GR64RegClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001945 else if (RegVT == MVT::f32)
Craig Topperc9099502012-04-20 06:31:50 +00001946 RC = &X86::FR32RegClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001947 else if (RegVT == MVT::f64)
Craig Topperc9099502012-04-20 06:31:50 +00001948 RC = &X86::FR64RegClass;
Craig Topper7a9a28b2012-08-12 02:23:29 +00001949 else if (RegVT.is256BitVector())
Craig Topperc9099502012-04-20 06:31:50 +00001950 RC = &X86::VR256RegClass;
Craig Topper7a9a28b2012-08-12 02:23:29 +00001951 else if (RegVT.is128BitVector())
Craig Topperc9099502012-04-20 06:31:50 +00001952 RC = &X86::VR128RegClass;
Dale Johannesen0488fb62010-09-30 23:57:10 +00001953 else if (RegVT == MVT::x86mmx)
Craig Topperc9099502012-04-20 06:31:50 +00001954 RC = &X86::VR64RegClass;
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001955 else
Torok Edwinc23197a2009-07-14 16:55:14 +00001956 llvm_unreachable("Unknown argument type!");
Gordon Henriksenae636f82008-01-03 16:47:34 +00001957
Devang Patel68e6bee2011-02-21 23:21:26 +00001958 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001959 ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT);
Scott Michelfdc40a02009-02-17 22:15:04 +00001960
Chris Lattnerf39f7712007-02-28 05:46:49 +00001961 // If this is an 8 or 16-bit value, it is really passed promoted to 32
1962 // bits. Insert an assert[sz]ext to capture this, then truncate to the
1963 // right size.
1964 if (VA.getLocInfo() == CCValAssign::SExt)
Dale Johannesenace16102009-02-03 19:33:06 +00001965 ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue,
Chris Lattnerf39f7712007-02-28 05:46:49 +00001966 DAG.getValueType(VA.getValVT()));
1967 else if (VA.getLocInfo() == CCValAssign::ZExt)
Dale Johannesenace16102009-02-03 19:33:06 +00001968 ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue,
Chris Lattnerf39f7712007-02-28 05:46:49 +00001969 DAG.getValueType(VA.getValVT()));
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001970 else if (VA.getLocInfo() == CCValAssign::BCvt)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001971 ArgValue = DAG.getNode(ISD::BITCAST, dl, VA.getValVT(), ArgValue);
Scott Michelfdc40a02009-02-17 22:15:04 +00001972
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001973 if (VA.isExtInLoc()) {
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001974 // Handle MMX values passed in XMM regs.
1975 if (RegVT.isVector()) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00001976 ArgValue = DAG.getNode(X86ISD::MOVDQ2Q, dl, VA.getValVT(),
1977 ArgValue);
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001978 } else
1979 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
Evan Cheng44c0fd12008-04-25 20:13:28 +00001980 }
Chris Lattnerf39f7712007-02-28 05:46:49 +00001981 } else {
1982 assert(VA.isMemLoc());
Dan Gohman98ca4f22009-08-05 01:29:28 +00001983 ArgValue = LowerMemArgument(Chain, CallConv, Ins, dl, DAG, VA, MFI, i);
Evan Cheng1bc78042006-04-26 01:20:17 +00001984 }
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001985
1986 // If value is passed via pointer - do a load.
1987 if (VA.getLocInfo() == CCValAssign::Indirect)
Chris Lattner51abfe42010-09-21 06:02:19 +00001988 ArgValue = DAG.getLoad(VA.getValVT(), dl, Chain, ArgValue,
Pete Cooperd752e0f2011-11-08 18:42:53 +00001989 MachinePointerInfo(), false, false, false, 0);
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001990
Dan Gohman98ca4f22009-08-05 01:29:28 +00001991 InVals.push_back(ArgValue);
Evan Cheng1bc78042006-04-26 01:20:17 +00001992 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001993
Dan Gohman61a92132008-04-21 23:59:07 +00001994 // The x86-64 ABI for returning structs by value requires that we copy
1995 // the sret argument into %rax for the return. Save the argument into
1996 // a virtual register so that we can access it from the return points.
Dan Gohman7e77b0f2009-08-01 19:14:37 +00001997 if (Is64Bit && MF.getFunction()->hasStructRetAttr()) {
Dan Gohman61a92132008-04-21 23:59:07 +00001998 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1999 unsigned Reg = FuncInfo->getSRetReturnReg();
2000 if (!Reg) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002001 Reg = MF.getRegInfo().createVirtualRegister(getRegClassFor(MVT::i64));
Dan Gohman61a92132008-04-21 23:59:07 +00002002 FuncInfo->setSRetReturnReg(Reg);
2003 }
Dan Gohman98ca4f22009-08-05 01:29:28 +00002004 SDValue Copy = DAG.getCopyToReg(DAG.getEntryNode(), dl, Reg, InVals[0]);
Owen Anderson825b72b2009-08-11 20:47:22 +00002005 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Copy, Chain);
Dan Gohman61a92132008-04-21 23:59:07 +00002006 }
2007
Chris Lattnerf39f7712007-02-28 05:46:49 +00002008 unsigned StackSize = CCInfo.getNextStackOffset();
Evan Cheng0c439eb2010-01-27 00:07:07 +00002009 // Align stack specially for tail calls.
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002010 if (FuncIsMadeTailCallSafe(CallConv,
2011 MF.getTarget().Options.GuaranteedTailCallOpt))
Gordon Henriksenae636f82008-01-03 16:47:34 +00002012 StackSize = GetAlignedArgumentStackSize(StackSize, DAG);
Evan Cheng25caf632006-05-23 21:06:34 +00002013
Evan Cheng1bc78042006-04-26 01:20:17 +00002014 // If the function takes variable number of arguments, make a frame index for
2015 // the start of the first vararg value... for expansion of llvm.va_start.
Gordon Henriksenae636f82008-01-03 16:47:34 +00002016 if (isVarArg) {
NAKAMURA Takumi3ca99432011-03-09 11:33:15 +00002017 if (Is64Bit || (CallConv != CallingConv::X86_FastCall &&
2018 CallConv != CallingConv::X86_ThisCall)) {
Jakob Stoklund Olesenb2eeed72010-07-29 17:42:27 +00002019 FuncInfo->setVarArgsFrameIndex(MFI->CreateFixedObject(1, StackSize,true));
Gordon Henriksen86737662008-01-05 16:56:59 +00002020 }
2021 if (Is64Bit) {
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002022 unsigned TotalNumIntRegs = 0, TotalNumXMMRegs = 0;
2023
2024 // FIXME: We should really autogenerate these arrays
Craig Topperc5eaae42012-03-11 07:57:25 +00002025 static const uint16_t GPR64ArgRegsWin64[] = {
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002026 X86::RCX, X86::RDX, X86::R8, X86::R9
Gordon Henriksen86737662008-01-05 16:56:59 +00002027 };
Craig Topperc5eaae42012-03-11 07:57:25 +00002028 static const uint16_t GPR64ArgRegs64Bit[] = {
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002029 X86::RDI, X86::RSI, X86::RDX, X86::RCX, X86::R8, X86::R9
2030 };
Craig Topperc5eaae42012-03-11 07:57:25 +00002031 static const uint16_t XMMArgRegs64Bit[] = {
Gordon Henriksen86737662008-01-05 16:56:59 +00002032 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
2033 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
2034 };
Craig Topperc5eaae42012-03-11 07:57:25 +00002035 const uint16_t *GPR64ArgRegs;
Anton Korobeynikove7beda12010-10-03 22:52:07 +00002036 unsigned NumXMMRegs = 0;
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002037
2038 if (IsWin64) {
Anton Korobeynikove7beda12010-10-03 22:52:07 +00002039 // The XMM registers which might contain var arg parameters are shadowed
2040 // in their paired GPR. So we only need to save the GPR to their home
2041 // slots.
2042 TotalNumIntRegs = 4;
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002043 GPR64ArgRegs = GPR64ArgRegsWin64;
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002044 } else {
2045 TotalNumIntRegs = 6; TotalNumXMMRegs = 8;
2046 GPR64ArgRegs = GPR64ArgRegs64Bit;
Anton Korobeynikove7beda12010-10-03 22:52:07 +00002047
Chad Rosier30450e82011-12-22 22:35:21 +00002048 NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs64Bit,
2049 TotalNumXMMRegs);
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002050 }
2051 unsigned NumIntRegs = CCInfo.getFirstUnallocated(GPR64ArgRegs,
2052 TotalNumIntRegs);
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002053
Bill Wendling67658342012-10-09 07:45:08 +00002054 bool NoImplicitFloatOps = Fn->getFnAttributes().
2055 hasAttribute(Attributes::NoImplicitFloat);
Craig Topper1accb7e2012-01-10 06:54:16 +00002056 assert(!(NumXMMRegs && !Subtarget->hasSSE1()) &&
Torok Edwin3f142c32009-02-01 18:15:56 +00002057 "SSE register cannot be used when SSE is disabled!");
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002058 assert(!(NumXMMRegs && MF.getTarget().Options.UseSoftFloat &&
2059 NoImplicitFloatOps) &&
Evan Chengc7ce29b2009-02-13 22:36:38 +00002060 "SSE register cannot be used when SSE is disabled!");
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002061 if (MF.getTarget().Options.UseSoftFloat || NoImplicitFloatOps ||
Craig Topper1accb7e2012-01-10 06:54:16 +00002062 !Subtarget->hasSSE1())
Torok Edwin3f142c32009-02-01 18:15:56 +00002063 // Kernel mode asks for SSE to be disabled, so don't push them
2064 // on the stack.
2065 TotalNumXMMRegs = 0;
Bill Wendlingf9abd7e2009-03-11 22:30:01 +00002066
Anton Korobeynikove7beda12010-10-03 22:52:07 +00002067 if (IsWin64) {
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00002068 const TargetFrameLowering &TFI = *getTargetMachine().getFrameLowering();
Cameron Esfahaniec37b002010-10-08 19:24:18 +00002069 // Get to the caller-allocated home save location. Add 8 to account
2070 // for the return address.
2071 int HomeOffset = TFI.getOffsetOfLocalArea() + 8;
Anton Korobeynikove7beda12010-10-03 22:52:07 +00002072 FuncInfo->setRegSaveFrameIndex(
Cameron Esfahaniec37b002010-10-08 19:24:18 +00002073 MFI->CreateFixedObject(1, NumIntRegs * 8 + HomeOffset, false));
NAKAMURA Takumi3ca99432011-03-09 11:33:15 +00002074 // Fixup to set vararg frame on shadow area (4 x i64).
2075 if (NumIntRegs < 4)
2076 FuncInfo->setVarArgsFrameIndex(FuncInfo->getRegSaveFrameIndex());
Anton Korobeynikove7beda12010-10-03 22:52:07 +00002077 } else {
2078 // For X86-64, if there are vararg parameters that are passed via
Chad Rosier30450e82011-12-22 22:35:21 +00002079 // registers, then we must store them to their spots on the stack so
2080 // they may be loaded by deferencing the result of va_next.
Anton Korobeynikove7beda12010-10-03 22:52:07 +00002081 FuncInfo->setVarArgsGPOffset(NumIntRegs * 8);
2082 FuncInfo->setVarArgsFPOffset(TotalNumIntRegs * 8 + NumXMMRegs * 16);
2083 FuncInfo->setRegSaveFrameIndex(
2084 MFI->CreateStackObject(TotalNumIntRegs * 8 + TotalNumXMMRegs * 16, 16,
Dan Gohman1e93df62010-04-17 14:41:14 +00002085 false));
Anton Korobeynikove7beda12010-10-03 22:52:07 +00002086 }
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002087
Gordon Henriksen86737662008-01-05 16:56:59 +00002088 // Store the integer parameter registers.
Dan Gohman475871a2008-07-27 21:46:04 +00002089 SmallVector<SDValue, 8> MemOps;
Dan Gohman1e93df62010-04-17 14:41:14 +00002090 SDValue RSFIN = DAG.getFrameIndex(FuncInfo->getRegSaveFrameIndex(),
2091 getPointerTy());
2092 unsigned Offset = FuncInfo->getVarArgsGPOffset();
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002093 for (; NumIntRegs != TotalNumIntRegs; ++NumIntRegs) {
Dan Gohmand6708ea2009-08-15 01:38:56 +00002094 SDValue FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), RSFIN,
2095 DAG.getIntPtrConstant(Offset));
Bob Wilson998e1252009-04-20 18:36:57 +00002096 unsigned VReg = MF.addLiveIn(GPR64ArgRegs[NumIntRegs],
Craig Topperc9099502012-04-20 06:31:50 +00002097 &X86::GR64RegClass);
Owen Anderson825b72b2009-08-11 20:47:22 +00002098 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i64);
Dan Gohman475871a2008-07-27 21:46:04 +00002099 SDValue Store =
Dale Johannesenace16102009-02-03 19:33:06 +00002100 DAG.getStore(Val.getValue(1), dl, Val, FIN,
Chris Lattnere8639032010-09-21 06:22:23 +00002101 MachinePointerInfo::getFixedStack(
2102 FuncInfo->getRegSaveFrameIndex(), Offset),
2103 false, false, 0);
Gordon Henriksen86737662008-01-05 16:56:59 +00002104 MemOps.push_back(Store);
Dan Gohmand6708ea2009-08-15 01:38:56 +00002105 Offset += 8;
Gordon Henriksen86737662008-01-05 16:56:59 +00002106 }
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002107
Dan Gohmanface41a2009-08-16 21:24:25 +00002108 if (TotalNumXMMRegs != 0 && NumXMMRegs != TotalNumXMMRegs) {
2109 // Now store the XMM (fp + vector) parameter registers.
2110 SmallVector<SDValue, 11> SaveXMMOps;
2111 SaveXMMOps.push_back(Chain);
Dan Gohmand6708ea2009-08-15 01:38:56 +00002112
Craig Topperc9099502012-04-20 06:31:50 +00002113 unsigned AL = MF.addLiveIn(X86::AL, &X86::GR8RegClass);
Dan Gohmanface41a2009-08-16 21:24:25 +00002114 SDValue ALVal = DAG.getCopyFromReg(DAG.getEntryNode(), dl, AL, MVT::i8);
2115 SaveXMMOps.push_back(ALVal);
Dan Gohmand6708ea2009-08-15 01:38:56 +00002116
Dan Gohman1e93df62010-04-17 14:41:14 +00002117 SaveXMMOps.push_back(DAG.getIntPtrConstant(
2118 FuncInfo->getRegSaveFrameIndex()));
2119 SaveXMMOps.push_back(DAG.getIntPtrConstant(
2120 FuncInfo->getVarArgsFPOffset()));
Dan Gohmand6708ea2009-08-15 01:38:56 +00002121
Dan Gohmanface41a2009-08-16 21:24:25 +00002122 for (; NumXMMRegs != TotalNumXMMRegs; ++NumXMMRegs) {
Anton Korobeynikove7beda12010-10-03 22:52:07 +00002123 unsigned VReg = MF.addLiveIn(XMMArgRegs64Bit[NumXMMRegs],
Craig Topperc9099502012-04-20 06:31:50 +00002124 &X86::VR128RegClass);
Dan Gohmanface41a2009-08-16 21:24:25 +00002125 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::v4f32);
2126 SaveXMMOps.push_back(Val);
2127 }
2128 MemOps.push_back(DAG.getNode(X86ISD::VASTART_SAVE_XMM_REGS, dl,
2129 MVT::Other,
2130 &SaveXMMOps[0], SaveXMMOps.size()));
Gordon Henriksen86737662008-01-05 16:56:59 +00002131 }
Dan Gohmanface41a2009-08-16 21:24:25 +00002132
2133 if (!MemOps.empty())
2134 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
2135 &MemOps[0], MemOps.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00002136 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00002137 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002138
Gordon Henriksen86737662008-01-05 16:56:59 +00002139 // Some CCs need callee pop.
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002140 if (X86::isCalleePop(CallConv, Is64Bit, isVarArg,
2141 MF.getTarget().Options.GuaranteedTailCallOpt)) {
Dan Gohman1e93df62010-04-17 14:41:14 +00002142 FuncInfo->setBytesToPopOnReturn(StackSize); // Callee pops everything.
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00002143 } else {
Dan Gohman1e93df62010-04-17 14:41:14 +00002144 FuncInfo->setBytesToPopOnReturn(0); // Callee pops nothing.
Chris Lattnerf39f7712007-02-28 05:46:49 +00002145 // If this is an sret function, the return should pop the hidden pointer.
Eli Friedman9a2478a2012-01-20 00:05:46 +00002146 if (!Is64Bit && !IsTailCallConvention(CallConv) && !IsWindows &&
Rafael Espindola1cee7102012-07-25 13:41:10 +00002147 argsAreStructReturn(Ins) == StackStructReturn)
Dan Gohman1e93df62010-04-17 14:41:14 +00002148 FuncInfo->setBytesToPopOnReturn(4);
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00002149 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00002150
Gordon Henriksen86737662008-01-05 16:56:59 +00002151 if (!Is64Bit) {
Dan Gohman1e93df62010-04-17 14:41:14 +00002152 // RegSaveFrameIndex is X86-64 only.
2153 FuncInfo->setRegSaveFrameIndex(0xAAAAAAA);
Anton Korobeynikovded05e32010-05-16 09:08:45 +00002154 if (CallConv == CallingConv::X86_FastCall ||
2155 CallConv == CallingConv::X86_ThisCall)
Dan Gohman1e93df62010-04-17 14:41:14 +00002156 // fastcc functions can't have varargs.
2157 FuncInfo->setVarArgsFrameIndex(0xAAAAAAA);
Gordon Henriksen86737662008-01-05 16:56:59 +00002158 }
Evan Cheng25caf632006-05-23 21:06:34 +00002159
Rafael Espindola76927d752011-08-30 19:39:58 +00002160 FuncInfo->setArgumentStackSize(StackSize);
2161
Dan Gohman98ca4f22009-08-05 01:29:28 +00002162 return Chain;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002163}
2164
Dan Gohman475871a2008-07-27 21:46:04 +00002165SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00002166X86TargetLowering::LowerMemOpCallTo(SDValue Chain,
2167 SDValue StackPtr, SDValue Arg,
2168 DebugLoc dl, SelectionDAG &DAG,
Evan Chengdffbd832008-01-10 00:09:10 +00002169 const CCValAssign &VA,
Dan Gohmand858e902010-04-17 15:26:15 +00002170 ISD::ArgFlagsTy Flags) const {
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00002171 unsigned LocMemOffset = VA.getLocMemOffset();
Dan Gohman475871a2008-07-27 21:46:04 +00002172 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset);
Dale Johannesenace16102009-02-03 19:33:06 +00002173 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, PtrOff);
Chris Lattnerfc448ff2010-09-21 18:51:21 +00002174 if (Flags.isByVal())
Dale Johannesendd64c412009-02-04 00:33:20 +00002175 return CreateCopyOfByValArgument(Arg, PtrOff, Chain, Flags, DAG, dl);
Chris Lattnerfc448ff2010-09-21 18:51:21 +00002176
2177 return DAG.getStore(Chain, dl, Arg, PtrOff,
2178 MachinePointerInfo::getStack(LocMemOffset),
David Greene67c9d422010-02-15 16:53:33 +00002179 false, false, 0);
Evan Chengdffbd832008-01-10 00:09:10 +00002180}
2181
Bill Wendling64e87322009-01-16 19:25:27 +00002182/// EmitTailCallLoadRetAddr - Emit a load of return address if tail call
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002183/// optimization is performed and it is required.
Scott Michelfdc40a02009-02-17 22:15:04 +00002184SDValue
2185X86TargetLowering::EmitTailCallLoadRetAddr(SelectionDAG &DAG,
Evan Chengddc419c2010-01-26 19:04:47 +00002186 SDValue &OutRetAddr, SDValue Chain,
2187 bool IsTailCall, bool Is64Bit,
Dan Gohmand858e902010-04-17 15:26:15 +00002188 int FPDiff, DebugLoc dl) const {
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002189 // Adjust the Return address stack slot.
Owen Andersone50ed302009-08-10 22:56:29 +00002190 EVT VT = getPointerTy();
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002191 OutRetAddr = getReturnAddressFrameIndex(DAG);
Bill Wendling64e87322009-01-16 19:25:27 +00002192
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002193 // Load the "old" Return address.
Chris Lattner51abfe42010-09-21 06:02:19 +00002194 OutRetAddr = DAG.getLoad(VT, dl, Chain, OutRetAddr, MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002195 false, false, false, 0);
Gabor Greifba36cb52008-08-28 21:40:38 +00002196 return SDValue(OutRetAddr.getNode(), 1);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002197}
2198
Chris Lattner7a2bdde2011-04-15 05:18:47 +00002199/// EmitTailCallStoreRetAddr - Emit a store of the return address if tail call
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002200/// optimization is performed and it is required (FPDiff!=0).
Scott Michelfdc40a02009-02-17 22:15:04 +00002201static SDValue
2202EmitTailCallStoreRetAddr(SelectionDAG & DAG, MachineFunction &MF,
Michael Liaoaa3c2c02012-10-25 06:29:14 +00002203 SDValue Chain, SDValue RetAddrFrIdx, EVT PtrVT,
2204 unsigned SlotSize, int FPDiff, DebugLoc dl) {
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002205 // Store the return address to the appropriate stack slot.
2206 if (!FPDiff) return Chain;
2207 // Calculate the new stack slot for the return address.
Scott Michelfdc40a02009-02-17 22:15:04 +00002208 int NewReturnAddrFI =
Evan Chenged2ae132010-07-03 00:40:23 +00002209 MF.getFrameInfo()->CreateFixedObject(SlotSize, FPDiff-SlotSize, false);
Michael Liaoaa3c2c02012-10-25 06:29:14 +00002210 SDValue NewRetAddrFrIdx = DAG.getFrameIndex(NewReturnAddrFI, PtrVT);
Scott Michelfdc40a02009-02-17 22:15:04 +00002211 Chain = DAG.getStore(Chain, dl, RetAddrFrIdx, NewRetAddrFrIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00002212 MachinePointerInfo::getFixedStack(NewReturnAddrFI),
David Greene67c9d422010-02-15 16:53:33 +00002213 false, false, 0);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002214 return Chain;
2215}
2216
Dan Gohman98ca4f22009-08-05 01:29:28 +00002217SDValue
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00002218X86TargetLowering::LowerCall(TargetLowering::CallLoweringInfo &CLI,
Dan Gohmand858e902010-04-17 15:26:15 +00002219 SmallVectorImpl<SDValue> &InVals) const {
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00002220 SelectionDAG &DAG = CLI.DAG;
2221 DebugLoc &dl = CLI.DL;
2222 SmallVector<ISD::OutputArg, 32> &Outs = CLI.Outs;
2223 SmallVector<SDValue, 32> &OutVals = CLI.OutVals;
2224 SmallVector<ISD::InputArg, 32> &Ins = CLI.Ins;
2225 SDValue Chain = CLI.Chain;
2226 SDValue Callee = CLI.Callee;
2227 CallingConv::ID CallConv = CLI.CallConv;
2228 bool &isTailCall = CLI.IsTailCall;
2229 bool isVarArg = CLI.IsVarArg;
2230
Dan Gohman98ca4f22009-08-05 01:29:28 +00002231 MachineFunction &MF = DAG.getMachineFunction();
2232 bool Is64Bit = Subtarget->is64Bit();
NAKAMURA Takumifb840c92011-02-05 15:11:13 +00002233 bool IsWin64 = Subtarget->isTargetWin64();
Eli Friedman9a2478a2012-01-20 00:05:46 +00002234 bool IsWindows = Subtarget->isTargetWindows();
Rafael Espindola1cee7102012-07-25 13:41:10 +00002235 StructReturnType SR = callIsStructReturn(Outs);
Evan Cheng5f941932010-02-05 02:21:12 +00002236 bool IsSibcall = false;
Dan Gohman98ca4f22009-08-05 01:29:28 +00002237
Nick Lewycky22de16d2012-01-19 00:34:10 +00002238 if (MF.getTarget().Options.DisableTailCalls)
2239 isTailCall = false;
2240
Evan Cheng5f941932010-02-05 02:21:12 +00002241 if (isTailCall) {
Evan Cheng0c439eb2010-01-27 00:07:07 +00002242 // Check if it's really possible to do a tail call.
Evan Chenga375d472010-03-15 18:54:48 +00002243 isTailCall = IsEligibleForTailCallOptimization(Callee, CallConv,
Rafael Espindola1cee7102012-07-25 13:41:10 +00002244 isVarArg, SR != NotStructReturn,
Evan Chengb1cacc72012-09-25 05:32:34 +00002245 MF.getFunction()->hasStructRetAttr(), CLI.RetTy,
Rafael Espindola1cee7102012-07-25 13:41:10 +00002246 Outs, OutVals, Ins, DAG);
Evan Chengf22f9b32010-02-06 03:28:46 +00002247
2248 // Sibcalls are automatically detected tailcalls which do not require
2249 // ABI changes.
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002250 if (!MF.getTarget().Options.GuaranteedTailCallOpt && isTailCall)
Evan Cheng5f941932010-02-05 02:21:12 +00002251 IsSibcall = true;
Evan Chengf22f9b32010-02-06 03:28:46 +00002252
2253 if (isTailCall)
2254 ++NumTailCalls;
Evan Cheng5f941932010-02-05 02:21:12 +00002255 }
Evan Cheng0c439eb2010-01-27 00:07:07 +00002256
Chris Lattner29689432010-03-11 00:22:57 +00002257 assert(!(isVarArg && IsTailCallConvention(CallConv)) &&
Duncan Sandsdc7f1742012-11-16 12:36:39 +00002258 "Var args not supported with calling convention fastcc, ghc or hipe");
Gordon Henriksenae636f82008-01-03 16:47:34 +00002259
Chris Lattner638402b2007-02-28 07:00:42 +00002260 // Analyze operands of the call, assigning locations to each operand.
Chris Lattner423c5f42007-02-28 05:31:48 +00002261 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002262 CCState CCInfo(CallConv, isVarArg, MF, getTargetMachine(),
Dan Gohman98ca4f22009-08-05 01:29:28 +00002263 ArgLocs, *DAG.getContext());
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00002264
2265 // Allocate shadow area for Win64
2266 if (IsWin64) {
2267 CCInfo.AllocateStack(32, 8);
2268 }
2269
Duncan Sands45907662010-10-31 13:21:44 +00002270 CCInfo.AnalyzeCallOperands(Outs, CC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00002271
Chris Lattner423c5f42007-02-28 05:31:48 +00002272 // Get a count of how many bytes are to be pushed on the stack.
2273 unsigned NumBytes = CCInfo.getNextStackOffset();
Evan Chengf22f9b32010-02-06 03:28:46 +00002274 if (IsSibcall)
Evan Chengb2c92902010-02-02 02:22:50 +00002275 // This is a sibcall. The memory operands are available in caller's
2276 // own caller's stack.
2277 NumBytes = 0;
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002278 else if (getTargetMachine().Options.GuaranteedTailCallOpt &&
2279 IsTailCallConvention(CallConv))
Evan Chengf22f9b32010-02-06 03:28:46 +00002280 NumBytes = GetAlignedArgumentStackSize(NumBytes, DAG);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002281
Gordon Henriksen86737662008-01-05 16:56:59 +00002282 int FPDiff = 0;
Evan Chengf22f9b32010-02-06 03:28:46 +00002283 if (isTailCall && !IsSibcall) {
Gordon Henriksen86737662008-01-05 16:56:59 +00002284 // Lower arguments at fp - stackoffset + fpdiff.
Jakub Staszak96df4372012-10-29 22:02:26 +00002285 X86MachineFunctionInfo *X86Info = MF.getInfo<X86MachineFunctionInfo>();
2286 unsigned NumBytesCallerPushed = X86Info->getBytesToPopOnReturn();
2287
Gordon Henriksen86737662008-01-05 16:56:59 +00002288 FPDiff = NumBytesCallerPushed - NumBytes;
2289
2290 // Set the delta of movement of the returnaddr stackslot.
2291 // But only set if delta is greater than previous delta.
Jakub Staszak96df4372012-10-29 22:02:26 +00002292 if (FPDiff < X86Info->getTCReturnAddrDelta())
2293 X86Info->setTCReturnAddrDelta(FPDiff);
Gordon Henriksen86737662008-01-05 16:56:59 +00002294 }
2295
Evan Chengf22f9b32010-02-06 03:28:46 +00002296 if (!IsSibcall)
2297 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002298
Dan Gohman475871a2008-07-27 21:46:04 +00002299 SDValue RetAddrFrIdx;
Chris Lattner7a2bdde2011-04-15 05:18:47 +00002300 // Load return address for tail calls.
Evan Chengf22f9b32010-02-06 03:28:46 +00002301 if (isTailCall && FPDiff)
2302 Chain = EmitTailCallLoadRetAddr(DAG, RetAddrFrIdx, Chain, isTailCall,
2303 Is64Bit, FPDiff, dl);
Gordon Henriksen86737662008-01-05 16:56:59 +00002304
Dan Gohman475871a2008-07-27 21:46:04 +00002305 SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass;
2306 SmallVector<SDValue, 8> MemOpChains;
2307 SDValue StackPtr;
Chris Lattner423c5f42007-02-28 05:31:48 +00002308
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002309 // Walk the register/memloc assignments, inserting copies/loads. In the case
2310 // of tail call optimization arguments are handle later.
Chris Lattner423c5f42007-02-28 05:31:48 +00002311 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2312 CCValAssign &VA = ArgLocs[i];
Owen Andersone50ed302009-08-10 22:56:29 +00002313 EVT RegVT = VA.getLocVT();
Dan Gohmanc9403652010-07-07 15:54:55 +00002314 SDValue Arg = OutVals[i];
Dan Gohman98ca4f22009-08-05 01:29:28 +00002315 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Dan Gohman095cc292008-09-13 01:54:27 +00002316 bool isByVal = Flags.isByVal();
Scott Michelfdc40a02009-02-17 22:15:04 +00002317
Chris Lattner423c5f42007-02-28 05:31:48 +00002318 // Promote the value if needed.
2319 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002320 default: llvm_unreachable("Unknown loc info!");
Chris Lattner423c5f42007-02-28 05:31:48 +00002321 case CCValAssign::Full: break;
2322 case CCValAssign::SExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002323 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00002324 break;
2325 case CCValAssign::ZExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002326 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00002327 break;
2328 case CCValAssign::AExt:
Craig Topper7a9a28b2012-08-12 02:23:29 +00002329 if (RegVT.is128BitVector()) {
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002330 // Special case: passing MMX values in XMM registers.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002331 Arg = DAG.getNode(ISD::BITCAST, dl, MVT::i64, Arg);
Owen Anderson825b72b2009-08-11 20:47:22 +00002332 Arg = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i64, Arg);
2333 Arg = getMOVL(DAG, dl, MVT::v2i64, DAG.getUNDEF(MVT::v2i64), Arg);
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002334 } else
2335 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, RegVT, Arg);
2336 break;
2337 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002338 Arg = DAG.getNode(ISD::BITCAST, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00002339 break;
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00002340 case CCValAssign::Indirect: {
2341 // Store the argument.
2342 SDValue SpillSlot = DAG.CreateStackTemporary(VA.getValVT());
Evan Chengff89dcb2009-10-18 18:16:27 +00002343 int FI = cast<FrameIndexSDNode>(SpillSlot)->getIndex();
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00002344 Chain = DAG.getStore(Chain, dl, Arg, SpillSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00002345 MachinePointerInfo::getFixedStack(FI),
David Greene67c9d422010-02-15 16:53:33 +00002346 false, false, 0);
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00002347 Arg = SpillSlot;
2348 break;
2349 }
Evan Cheng6b5783d2006-05-25 18:56:34 +00002350 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002351
Chris Lattner423c5f42007-02-28 05:31:48 +00002352 if (VA.isRegLoc()) {
Stuart Hastings2aa0f232011-05-26 04:09:49 +00002353 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
2354 if (isVarArg && IsWin64) {
2355 // Win64 ABI requires argument XMM reg to be copied to the corresponding
2356 // shadow reg if callee is a varargs function.
2357 unsigned ShadowReg = 0;
2358 switch (VA.getLocReg()) {
2359 case X86::XMM0: ShadowReg = X86::RCX; break;
2360 case X86::XMM1: ShadowReg = X86::RDX; break;
2361 case X86::XMM2: ShadowReg = X86::R8; break;
2362 case X86::XMM3: ShadowReg = X86::R9; break;
Anton Korobeynikovc52bedb2010-08-27 14:43:06 +00002363 }
Stuart Hastings2aa0f232011-05-26 04:09:49 +00002364 if (ShadowReg)
2365 RegsToPass.push_back(std::make_pair(ShadowReg, Arg));
Anton Korobeynikovc52bedb2010-08-27 14:43:06 +00002366 }
Evan Chengf22f9b32010-02-06 03:28:46 +00002367 } else if (!IsSibcall && (!isTailCall || isByVal)) {
Evan Cheng5f941932010-02-05 02:21:12 +00002368 assert(VA.isMemLoc());
2369 if (StackPtr.getNode() == 0)
Michael Liaoc5c970e2012-10-31 04:14:09 +00002370 StackPtr = DAG.getCopyFromReg(Chain, dl, RegInfo->getStackRegister(),
2371 getPointerTy());
Evan Cheng5f941932010-02-05 02:21:12 +00002372 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Arg,
2373 dl, DAG, VA, Flags));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002374 }
Stuart Hastings2aa0f232011-05-26 04:09:49 +00002375 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002376
Evan Cheng32fe1032006-05-25 00:59:30 +00002377 if (!MemOpChains.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00002378 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002379 &MemOpChains[0], MemOpChains.size());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002380
Chris Lattner88e1fd52009-07-09 04:24:46 +00002381 if (Subtarget->isPICStyleGOT()) {
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002382 // ELF / PIC requires GOT in the EBX register before function calls via PLT
2383 // GOT pointer.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002384 if (!isTailCall) {
Jakob Stoklund Olesenb8720782012-07-04 19:28:31 +00002385 RegsToPass.push_back(std::make_pair(unsigned(X86::EBX),
2386 DAG.getNode(X86ISD::GlobalBaseReg, DebugLoc(), getPointerTy())));
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002387 } else {
2388 // If we are tail calling and generating PIC/GOT style code load the
2389 // address of the callee into ECX. The value in ecx is used as target of
2390 // the tail jump. This is done to circumvent the ebx/callee-saved problem
2391 // for tail calls on PIC/GOT architectures. Normally we would just put the
2392 // address of GOT into ebx and then call target@PLT. But for tail calls
2393 // ebx would be restored (since ebx is callee saved) before jumping to the
2394 // target@PLT.
2395
2396 // Note: The actual moving to ECX is done further down.
2397 GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee);
2398 if (G && !G->getGlobal()->hasHiddenVisibility() &&
2399 !G->getGlobal()->hasProtectedVisibility())
2400 Callee = LowerGlobalAddress(Callee, DAG);
2401 else if (isa<ExternalSymbolSDNode>(Callee))
Chris Lattner15a380a2009-07-09 04:39:06 +00002402 Callee = LowerExternalSymbol(Callee, DAG);
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002403 }
Anton Korobeynikov7f705592007-01-12 19:20:47 +00002404 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00002405
NAKAMURA Takumifb840c92011-02-05 15:11:13 +00002406 if (Is64Bit && isVarArg && !IsWin64) {
Gordon Henriksen86737662008-01-05 16:56:59 +00002407 // From AMD64 ABI document:
2408 // For calls that may call functions that use varargs or stdargs
2409 // (prototype-less calls or calls to functions containing ellipsis (...) in
2410 // the declaration) %al is used as hidden argument to specify the number
2411 // of SSE registers used. The contents of %al do not need to match exactly
2412 // the number of registers, but must be an ubound on the number of SSE
2413 // registers used and is in the range 0 - 8 inclusive.
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002414
Gordon Henriksen86737662008-01-05 16:56:59 +00002415 // Count the number of XMM registers allocated.
Craig Topperc5eaae42012-03-11 07:57:25 +00002416 static const uint16_t XMMArgRegs[] = {
Gordon Henriksen86737662008-01-05 16:56:59 +00002417 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
2418 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
2419 };
2420 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs, 8);
Craig Topper1accb7e2012-01-10 06:54:16 +00002421 assert((Subtarget->hasSSE1() || !NumXMMRegs)
Torok Edwin3f142c32009-02-01 18:15:56 +00002422 && "SSE registers cannot be used when SSE is disabled");
Scott Michelfdc40a02009-02-17 22:15:04 +00002423
Jakob Stoklund Olesenb8720782012-07-04 19:28:31 +00002424 RegsToPass.push_back(std::make_pair(unsigned(X86::AL),
2425 DAG.getConstant(NumXMMRegs, MVT::i8)));
Gordon Henriksen86737662008-01-05 16:56:59 +00002426 }
2427
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00002428 // For tail calls lower the arguments to the 'real' stack slot.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002429 if (isTailCall) {
2430 // Force all the incoming stack arguments to be loaded from the stack
2431 // before any new outgoing arguments are stored to the stack, because the
2432 // outgoing stack slots may alias the incoming argument stack slots, and
2433 // the alias isn't otherwise explicit. This is slightly more conservative
2434 // than necessary, because it means that each store effectively depends
2435 // on every argument instead of just those arguments it would clobber.
2436 SDValue ArgChain = DAG.getStackArgumentTokenFactor(Chain);
2437
Dan Gohman475871a2008-07-27 21:46:04 +00002438 SmallVector<SDValue, 8> MemOpChains2;
2439 SDValue FIN;
Gordon Henriksen86737662008-01-05 16:56:59 +00002440 int FI = 0;
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002441 if (getTargetMachine().Options.GuaranteedTailCallOpt) {
Evan Chengb2c92902010-02-02 02:22:50 +00002442 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2443 CCValAssign &VA = ArgLocs[i];
2444 if (VA.isRegLoc())
2445 continue;
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00002446 assert(VA.isMemLoc());
Dan Gohmanc9403652010-07-07 15:54:55 +00002447 SDValue Arg = OutVals[i];
Dan Gohman98ca4f22009-08-05 01:29:28 +00002448 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Gordon Henriksen86737662008-01-05 16:56:59 +00002449 // Create frame index.
2450 int32_t Offset = VA.getLocMemOffset()+FPDiff;
Duncan Sands83ec4b62008-06-06 12:08:01 +00002451 uint32_t OpSize = (VA.getLocVT().getSizeInBits()+7)/8;
Evan Chenged2ae132010-07-03 00:40:23 +00002452 FI = MF.getFrameInfo()->CreateFixedObject(OpSize, Offset, true);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002453 FIN = DAG.getFrameIndex(FI, getPointerTy());
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00002454
Duncan Sands276dcbd2008-03-21 09:14:45 +00002455 if (Flags.isByVal()) {
Evan Cheng8e5712b2008-01-12 01:08:07 +00002456 // Copy relative to framepointer.
Dan Gohman475871a2008-07-27 21:46:04 +00002457 SDValue Source = DAG.getIntPtrConstant(VA.getLocMemOffset());
Gabor Greifba36cb52008-08-28 21:40:38 +00002458 if (StackPtr.getNode() == 0)
Michael Liaoc5c970e2012-10-31 04:14:09 +00002459 StackPtr = DAG.getCopyFromReg(Chain, dl,
2460 RegInfo->getStackRegister(),
Dale Johannesendd64c412009-02-04 00:33:20 +00002461 getPointerTy());
Dale Johannesenace16102009-02-03 19:33:06 +00002462 Source = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, Source);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002463
Dan Gohman98ca4f22009-08-05 01:29:28 +00002464 MemOpChains2.push_back(CreateCopyOfByValArgument(Source, FIN,
2465 ArgChain,
Dale Johannesendd64c412009-02-04 00:33:20 +00002466 Flags, DAG, dl));
Gordon Henriksen86737662008-01-05 16:56:59 +00002467 } else {
Evan Cheng8e5712b2008-01-12 01:08:07 +00002468 // Store relative to framepointer.
Dan Gohman69de1932008-02-06 22:27:42 +00002469 MemOpChains2.push_back(
Dan Gohman98ca4f22009-08-05 01:29:28 +00002470 DAG.getStore(ArgChain, dl, Arg, FIN,
Chris Lattnere8639032010-09-21 06:22:23 +00002471 MachinePointerInfo::getFixedStack(FI),
David Greene67c9d422010-02-15 16:53:33 +00002472 false, false, 0));
Scott Michelfdc40a02009-02-17 22:15:04 +00002473 }
Gordon Henriksen86737662008-01-05 16:56:59 +00002474 }
2475 }
2476
2477 if (!MemOpChains2.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00002478 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Arnold Schwaighofer719eb022008-01-11 14:34:56 +00002479 &MemOpChains2[0], MemOpChains2.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00002480
2481 // Store the return address to the appropriate stack slot.
Michael Liaoaa3c2c02012-10-25 06:29:14 +00002482 Chain = EmitTailCallStoreRetAddr(DAG, MF, Chain, RetAddrFrIdx,
2483 getPointerTy(), RegInfo->getSlotSize(),
Dale Johannesenace16102009-02-03 19:33:06 +00002484 FPDiff, dl);
Gordon Henriksen86737662008-01-05 16:56:59 +00002485 }
2486
Jakob Stoklund Olesenb8720782012-07-04 19:28:31 +00002487 // Build a sequence of copy-to-reg nodes chained together with token chain
2488 // and flag operands which copy the outgoing args into registers.
2489 SDValue InFlag;
2490 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
2491 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
2492 RegsToPass[i].second, InFlag);
2493 InFlag = Chain.getValue(1);
2494 }
2495
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002496 if (getTargetMachine().getCodeModel() == CodeModel::Large) {
2497 assert(Is64Bit && "Large code model is only legal in 64-bit mode.");
2498 // In the 64-bit large code model, we have to make all calls
2499 // through a register, since the call instruction's 32-bit
2500 // pc-relative offset may not be large enough to hold the whole
2501 // address.
2502 } else if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002503 // If the callee is a GlobalAddress node (quite common, every direct call
2504 // is) turn it into a TargetGlobalAddress node so that legalize doesn't hack
2505 // it.
2506
Anton Korobeynikov2b2bc682006-12-22 22:29:05 +00002507 // We should use extra load for direct calls to dllimported functions in
2508 // non-JIT mode.
Dan Gohman46510a72010-04-15 01:51:59 +00002509 const GlobalValue *GV = G->getGlobal();
Chris Lattner754b7652009-07-10 05:48:03 +00002510 if (!GV->hasDLLImportLinkage()) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002511 unsigned char OpFlags = 0;
John McCall3a3465b2011-06-15 20:36:13 +00002512 bool ExtraLoad = false;
2513 unsigned WrapperKind = ISD::DELETED_NODE;
Eric Christopherfd179292009-08-27 18:07:15 +00002514
Chris Lattner48a7d022009-07-09 05:02:21 +00002515 // On ELF targets, in both X86-64 and X86-32 mode, direct calls to
2516 // external symbols most go through the PLT in PIC mode. If the symbol
2517 // has hidden or protected visibility, or if it is static or local, then
2518 // we don't need to use the PLT - we can directly call it.
2519 if (Subtarget->isTargetELF() &&
2520 getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
Chris Lattner74e726e2009-07-09 05:27:35 +00002521 GV->hasDefaultVisibility() && !GV->hasLocalLinkage()) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002522 OpFlags = X86II::MO_PLT;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00002523 } else if (Subtarget->isPICStyleStubAny() &&
Chris Lattner80945782010-09-27 06:34:01 +00002524 (GV->isDeclaration() || GV->isWeakForLinker()) &&
Daniel Dunbar558692f2011-04-20 00:14:25 +00002525 (!Subtarget->getTargetTriple().isMacOSX() ||
2526 Subtarget->getTargetTriple().isMacOSXVersionLT(10, 5))) {
Chris Lattner74e726e2009-07-09 05:27:35 +00002527 // PC-relative references to external symbols should go through $stub,
2528 // unless we're building with the leopard linker or later, which
2529 // automatically synthesizes these stubs.
2530 OpFlags = X86II::MO_DARWIN_STUB;
John McCall3a3465b2011-06-15 20:36:13 +00002531 } else if (Subtarget->isPICStyleRIPRel() &&
2532 isa<Function>(GV) &&
Bill Wendling67658342012-10-09 07:45:08 +00002533 cast<Function>(GV)->getFnAttributes().
2534 hasAttribute(Attributes::NonLazyBind)) {
John McCall3a3465b2011-06-15 20:36:13 +00002535 // If the function is marked as non-lazy, generate an indirect call
2536 // which loads from the GOT directly. This avoids runtime overhead
2537 // at the cost of eager binding (and one extra byte of encoding).
2538 OpFlags = X86II::MO_GOTPCREL;
2539 WrapperKind = X86ISD::WrapperRIP;
2540 ExtraLoad = true;
Chris Lattner74e726e2009-07-09 05:27:35 +00002541 }
Chris Lattner48a7d022009-07-09 05:02:21 +00002542
Devang Patel0d881da2010-07-06 22:08:15 +00002543 Callee = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(),
Chris Lattner48a7d022009-07-09 05:02:21 +00002544 G->getOffset(), OpFlags);
John McCall3a3465b2011-06-15 20:36:13 +00002545
2546 // Add a wrapper if needed.
2547 if (WrapperKind != ISD::DELETED_NODE)
2548 Callee = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Callee);
2549 // Add extra indirection if needed.
2550 if (ExtraLoad)
2551 Callee = DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(), Callee,
2552 MachinePointerInfo::getGOT(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002553 false, false, false, 0);
Chris Lattner48a7d022009-07-09 05:02:21 +00002554 }
Bill Wendling056292f2008-09-16 21:48:12 +00002555 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002556 unsigned char OpFlags = 0;
2557
Evan Cheng1bf891a2010-12-01 22:59:46 +00002558 // On ELF targets, in either X86-64 or X86-32 mode, direct calls to
2559 // external symbols should go through the PLT.
2560 if (Subtarget->isTargetELF() &&
2561 getTargetMachine().getRelocationModel() == Reloc::PIC_) {
2562 OpFlags = X86II::MO_PLT;
2563 } else if (Subtarget->isPICStyleStubAny() &&
Daniel Dunbar558692f2011-04-20 00:14:25 +00002564 (!Subtarget->getTargetTriple().isMacOSX() ||
2565 Subtarget->getTargetTriple().isMacOSXVersionLT(10, 5))) {
Evan Cheng1bf891a2010-12-01 22:59:46 +00002566 // PC-relative references to external symbols should go through $stub,
2567 // unless we're building with the leopard linker or later, which
2568 // automatically synthesizes these stubs.
2569 OpFlags = X86II::MO_DARWIN_STUB;
Chris Lattner74e726e2009-07-09 05:27:35 +00002570 }
Eric Christopherfd179292009-08-27 18:07:15 +00002571
Chris Lattner48a7d022009-07-09 05:02:21 +00002572 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy(),
2573 OpFlags);
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002574 }
2575
Chris Lattnerd96d0722007-02-25 06:40:16 +00002576 // Returns a chain & a flag for retval copy to use.
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002577 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Dan Gohman475871a2008-07-27 21:46:04 +00002578 SmallVector<SDValue, 8> Ops;
Gordon Henriksen86737662008-01-05 16:56:59 +00002579
Evan Chengf22f9b32010-02-06 03:28:46 +00002580 if (!IsSibcall && isTailCall) {
Dale Johannesene8d72302009-02-06 23:05:02 +00002581 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
2582 DAG.getIntPtrConstant(0, true), InFlag);
Gordon Henriksen86737662008-01-05 16:56:59 +00002583 InFlag = Chain.getValue(1);
Gordon Henriksen86737662008-01-05 16:56:59 +00002584 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002585
Nate Begeman4c5dcf52006-02-17 00:03:04 +00002586 Ops.push_back(Chain);
2587 Ops.push_back(Callee);
Evan Chengb69d1132006-06-14 18:17:40 +00002588
Dan Gohman98ca4f22009-08-05 01:29:28 +00002589 if (isTailCall)
Owen Anderson825b72b2009-08-11 20:47:22 +00002590 Ops.push_back(DAG.getConstant(FPDiff, MVT::i32));
Evan Chengf4684712007-02-21 21:18:14 +00002591
Gordon Henriksen86737662008-01-05 16:56:59 +00002592 // Add argument registers to the end of the list so that they are known live
2593 // into the call.
Evan Cheng9b449442008-01-07 23:08:23 +00002594 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
2595 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
2596 RegsToPass[i].second.getValueType()));
Scott Michelfdc40a02009-02-17 22:15:04 +00002597
Jakob Stoklund Olesen8bcde2a2012-02-16 00:02:50 +00002598 // Add a register mask operand representing the call-preserved registers.
2599 const TargetRegisterInfo *TRI = getTargetMachine().getRegisterInfo();
2600 const uint32_t *Mask = TRI->getCallPreservedMask(CallConv);
2601 assert(Mask && "Missing call preserved mask for calling convention");
2602 Ops.push_back(DAG.getRegisterMask(Mask));
Jakob Stoklund Olesenc38c4562012-01-18 23:52:22 +00002603
Gabor Greifba36cb52008-08-28 21:40:38 +00002604 if (InFlag.getNode())
Evan Cheng347d5f72006-04-28 21:29:37 +00002605 Ops.push_back(InFlag);
Gordon Henriksenae636f82008-01-03 16:47:34 +00002606
Dan Gohman98ca4f22009-08-05 01:29:28 +00002607 if (isTailCall) {
Dale Johannesen88004c22010-06-05 00:30:45 +00002608 // We used to do:
2609 //// If this is the first return lowered for this function, add the regs
2610 //// to the liveout set for the function.
2611 // This isn't right, although it's probably harmless on x86; liveouts
2612 // should be computed from returns not tail calls. Consider a void
2613 // function making a tail call to a function returning int.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002614 return DAG.getNode(X86ISD::TC_RETURN, dl,
2615 NodeTys, &Ops[0], Ops.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00002616 }
2617
Dale Johannesenace16102009-02-03 19:33:06 +00002618 Chain = DAG.getNode(X86ISD::CALL, dl, NodeTys, &Ops[0], Ops.size());
Evan Cheng347d5f72006-04-28 21:29:37 +00002619 InFlag = Chain.getValue(1);
Evan Chengd90eb7f2006-01-05 00:27:02 +00002620
Chris Lattner2d297092006-05-23 18:50:38 +00002621 // Create the CALLSEQ_END node.
Gordon Henriksen86737662008-01-05 16:56:59 +00002622 unsigned NumBytesForCalleeToPush;
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002623 if (X86::isCalleePop(CallConv, Is64Bit, isVarArg,
2624 getTargetMachine().Options.GuaranteedTailCallOpt))
Gordon Henriksen86737662008-01-05 16:56:59 +00002625 NumBytesForCalleeToPush = NumBytes; // Callee pops everything
Eli Friedman9a2478a2012-01-20 00:05:46 +00002626 else if (!Is64Bit && !IsTailCallConvention(CallConv) && !IsWindows &&
Rafael Espindola1cee7102012-07-25 13:41:10 +00002627 SR == StackStructReturn)
Dan Gohmanf451cb82010-02-10 16:03:48 +00002628 // If this is a call to a struct-return function, the callee
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00002629 // pops the hidden struct pointer, so we have to push it back.
2630 // This is common for Darwin/X86, Linux & Mingw32 targets.
Eli Friedman9a2478a2012-01-20 00:05:46 +00002631 // For MSVC Win32 targets, the caller pops the hidden struct pointer.
Gordon Henriksenae636f82008-01-03 16:47:34 +00002632 NumBytesForCalleeToPush = 4;
Gordon Henriksen86737662008-01-05 16:56:59 +00002633 else
Gordon Henriksenae636f82008-01-03 16:47:34 +00002634 NumBytesForCalleeToPush = 0; // Callee pops nothing.
Scott Michelfdc40a02009-02-17 22:15:04 +00002635
Gordon Henriksenae636f82008-01-03 16:47:34 +00002636 // Returns a flag for retval copy to use.
Evan Chengf22f9b32010-02-06 03:28:46 +00002637 if (!IsSibcall) {
2638 Chain = DAG.getCALLSEQ_END(Chain,
2639 DAG.getIntPtrConstant(NumBytes, true),
2640 DAG.getIntPtrConstant(NumBytesForCalleeToPush,
2641 true),
2642 InFlag);
2643 InFlag = Chain.getValue(1);
2644 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00002645
Chris Lattner3085e152007-02-25 08:59:22 +00002646 // Handle result values, copying them out of physregs into vregs that we
2647 // return.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002648 return LowerCallResult(Chain, InFlag, CallConv, isVarArg,
2649 Ins, dl, DAG, InVals);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002650}
2651
Evan Cheng25ab6902006-09-08 06:48:29 +00002652
2653//===----------------------------------------------------------------------===//
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002654// Fast Calling Convention (tail call) implementation
2655//===----------------------------------------------------------------------===//
2656
2657// Like std call, callee cleans arguments, convention except that ECX is
2658// reserved for storing the tail called function address. Only 2 registers are
2659// free for argument passing (inreg). Tail call optimization is performed
2660// provided:
2661// * tailcallopt is enabled
2662// * caller/callee are fastcc
Arnold Schwaighofera2a4b472008-02-26 10:21:54 +00002663// On X86_64 architecture with GOT-style position independent code only local
2664// (within module) calls are supported at the moment.
Arnold Schwaighofer48abc5c2007-10-12 21:30:57 +00002665// To keep the stack aligned according to platform abi the function
2666// GetAlignedArgumentStackSize ensures that argument delta is always multiples
2667// of stack alignment. (Dynamic linkers need this - darwin's dyld for example)
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002668// If a tail called function callee has more arguments than the caller the
2669// caller needs to make sure that there is room to move the RETADDR to. This is
Arnold Schwaighofer48abc5c2007-10-12 21:30:57 +00002670// achieved by reserving an area the size of the argument delta right after the
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002671// original REtADDR, but before the saved framepointer or the spilled registers
2672// e.g. caller(arg1, arg2) calls callee(arg1, arg2,arg3,arg4)
2673// stack layout:
2674// arg1
2675// arg2
2676// RETADDR
Scott Michelfdc40a02009-02-17 22:15:04 +00002677// [ new RETADDR
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002678// move area ]
2679// (possible EBP)
2680// ESI
2681// EDI
2682// local1 ..
2683
2684/// GetAlignedArgumentStackSize - Make the stack size align e.g 16n + 12 aligned
2685/// for a 16 byte align requirement.
Dan Gohmand858e902010-04-17 15:26:15 +00002686unsigned
2687X86TargetLowering::GetAlignedArgumentStackSize(unsigned StackSize,
2688 SelectionDAG& DAG) const {
Evan Chenge9ac9e62008-09-07 09:07:23 +00002689 MachineFunction &MF = DAG.getMachineFunction();
2690 const TargetMachine &TM = MF.getTarget();
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00002691 const TargetFrameLowering &TFI = *TM.getFrameLowering();
Evan Chenge9ac9e62008-09-07 09:07:23 +00002692 unsigned StackAlignment = TFI.getStackAlignment();
Scott Michelfdc40a02009-02-17 22:15:04 +00002693 uint64_t AlignMask = StackAlignment - 1;
Evan Chenge9ac9e62008-09-07 09:07:23 +00002694 int64_t Offset = StackSize;
Michael Liaoaa3c2c02012-10-25 06:29:14 +00002695 unsigned SlotSize = RegInfo->getSlotSize();
Evan Chenge9ac9e62008-09-07 09:07:23 +00002696 if ( (Offset & AlignMask) <= (StackAlignment - SlotSize) ) {
2697 // Number smaller than 12 so just add the difference.
2698 Offset += ((StackAlignment - SlotSize) - (Offset & AlignMask));
2699 } else {
2700 // Mask out lower bits, add stackalignment once plus the 12 bytes.
Scott Michelfdc40a02009-02-17 22:15:04 +00002701 Offset = ((~AlignMask) & Offset) + StackAlignment +
Evan Chenge9ac9e62008-09-07 09:07:23 +00002702 (StackAlignment-SlotSize);
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002703 }
Evan Chenge9ac9e62008-09-07 09:07:23 +00002704 return Offset;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002705}
2706
Evan Cheng5f941932010-02-05 02:21:12 +00002707/// MatchingStackOffset - Return true if the given stack call argument is
2708/// already available in the same position (relatively) of the caller's
2709/// incoming argument stack.
2710static
2711bool MatchingStackOffset(SDValue Arg, unsigned Offset, ISD::ArgFlagsTy Flags,
2712 MachineFrameInfo *MFI, const MachineRegisterInfo *MRI,
2713 const X86InstrInfo *TII) {
Evan Cheng4cae1332010-03-05 08:38:04 +00002714 unsigned Bytes = Arg.getValueType().getSizeInBits() / 8;
2715 int FI = INT_MAX;
Evan Cheng5f941932010-02-05 02:21:12 +00002716 if (Arg.getOpcode() == ISD::CopyFromReg) {
2717 unsigned VR = cast<RegisterSDNode>(Arg.getOperand(1))->getReg();
Jakob Stoklund Olesenc9df0252011-01-10 02:58:51 +00002718 if (!TargetRegisterInfo::isVirtualRegister(VR))
Evan Cheng5f941932010-02-05 02:21:12 +00002719 return false;
2720 MachineInstr *Def = MRI->getVRegDef(VR);
2721 if (!Def)
2722 return false;
2723 if (!Flags.isByVal()) {
2724 if (!TII->isLoadFromStackSlot(Def, FI))
2725 return false;
2726 } else {
2727 unsigned Opcode = Def->getOpcode();
2728 if ((Opcode == X86::LEA32r || Opcode == X86::LEA64r) &&
2729 Def->getOperand(1).isFI()) {
2730 FI = Def->getOperand(1).getIndex();
Evan Cheng4cae1332010-03-05 08:38:04 +00002731 Bytes = Flags.getByValSize();
Evan Cheng5f941932010-02-05 02:21:12 +00002732 } else
2733 return false;
2734 }
Evan Cheng4cae1332010-03-05 08:38:04 +00002735 } else if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Arg)) {
2736 if (Flags.isByVal())
2737 // ByVal argument is passed in as a pointer but it's now being
Evan Cheng10718492010-03-05 19:55:55 +00002738 // dereferenced. e.g.
Evan Cheng4cae1332010-03-05 08:38:04 +00002739 // define @foo(%struct.X* %A) {
2740 // tail call @bar(%struct.X* byval %A)
2741 // }
Evan Cheng5f941932010-02-05 02:21:12 +00002742 return false;
2743 SDValue Ptr = Ld->getBasePtr();
2744 FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr);
2745 if (!FINode)
2746 return false;
2747 FI = FINode->getIndex();
Chad Rosierdf78fcd2011-06-25 02:04:56 +00002748 } else if (Arg.getOpcode() == ISD::FrameIndex && Flags.isByVal()) {
Chad Rosier14d71aa2011-06-25 18:51:28 +00002749 FrameIndexSDNode *FINode = cast<FrameIndexSDNode>(Arg);
Chad Rosierdf78fcd2011-06-25 02:04:56 +00002750 FI = FINode->getIndex();
2751 Bytes = Flags.getByValSize();
Evan Cheng4cae1332010-03-05 08:38:04 +00002752 } else
2753 return false;
Evan Cheng5f941932010-02-05 02:21:12 +00002754
Evan Cheng4cae1332010-03-05 08:38:04 +00002755 assert(FI != INT_MAX);
Evan Cheng5f941932010-02-05 02:21:12 +00002756 if (!MFI->isFixedObjectIndex(FI))
2757 return false;
Evan Cheng4cae1332010-03-05 08:38:04 +00002758 return Offset == MFI->getObjectOffset(FI) && Bytes == MFI->getObjectSize(FI);
Evan Cheng5f941932010-02-05 02:21:12 +00002759}
2760
Dan Gohman98ca4f22009-08-05 01:29:28 +00002761/// IsEligibleForTailCallOptimization - Check whether the call is eligible
2762/// for tail call optimization. Targets which want to do tail call
2763/// optimization should implement this function.
2764bool
2765X86TargetLowering::IsEligibleForTailCallOptimization(SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00002766 CallingConv::ID CalleeCC,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002767 bool isVarArg,
Evan Chenga375d472010-03-15 18:54:48 +00002768 bool isCalleeStructRet,
2769 bool isCallerStructRet,
Evan Chengb1cacc72012-09-25 05:32:34 +00002770 Type *RetTy,
Evan Chengb1712452010-01-27 06:25:16 +00002771 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00002772 const SmallVectorImpl<SDValue> &OutVals,
Evan Chengb1712452010-01-27 06:25:16 +00002773 const SmallVectorImpl<ISD::InputArg> &Ins,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002774 SelectionDAG& DAG) const {
Chris Lattner29689432010-03-11 00:22:57 +00002775 if (!IsTailCallConvention(CalleeCC) &&
Evan Chengb1712452010-01-27 06:25:16 +00002776 CalleeCC != CallingConv::C)
2777 return false;
2778
Evan Cheng7096ae42010-01-29 06:45:59 +00002779 // If -tailcallopt is specified, make fastcc functions tail-callable.
Evan Cheng2c12cb42010-03-26 16:26:03 +00002780 const MachineFunction &MF = DAG.getMachineFunction();
Evan Cheng7096ae42010-01-29 06:45:59 +00002781 const Function *CallerF = DAG.getMachineFunction().getFunction();
Evan Chengb1cacc72012-09-25 05:32:34 +00002782
2783 // If the function return type is x86_fp80 and the callee return type is not,
2784 // then the FP_EXTEND of the call result is not a nop. It's not safe to
2785 // perform a tailcall optimization here.
2786 if (CallerF->getReturnType()->isX86_FP80Ty() && !RetTy->isX86_FP80Ty())
2787 return false;
2788
Evan Cheng13617962010-04-30 01:12:32 +00002789 CallingConv::ID CallerCC = CallerF->getCallingConv();
2790 bool CCMatch = CallerCC == CalleeCC;
2791
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002792 if (getTargetMachine().Options.GuaranteedTailCallOpt) {
Evan Cheng13617962010-04-30 01:12:32 +00002793 if (IsTailCallConvention(CalleeCC) && CCMatch)
Evan Cheng843bd692010-01-31 06:44:49 +00002794 return true;
2795 return false;
2796 }
2797
Dale Johannesen2f05cc02010-05-28 23:24:28 +00002798 // Look for obvious safe cases to perform tail call optimization that do not
2799 // require ABI changes. This is what gcc calls sibcall.
Evan Chengb2c92902010-02-02 02:22:50 +00002800
Evan Cheng2c12cb42010-03-26 16:26:03 +00002801 // Can't do sibcall if stack needs to be dynamically re-aligned. PEI needs to
2802 // emit a special epilogue.
2803 if (RegInfo->needsStackRealignment(MF))
2804 return false;
2805
Evan Chenga375d472010-03-15 18:54:48 +00002806 // Also avoid sibcall optimization if either caller or callee uses struct
2807 // return semantics.
2808 if (isCalleeStructRet || isCallerStructRet)
2809 return false;
2810
Chad Rosier2416da32011-06-24 21:15:36 +00002811 // An stdcall caller is expected to clean up its arguments; the callee
2812 // isn't going to do that.
2813 if (!CCMatch && CallerCC==CallingConv::X86_StdCall)
2814 return false;
2815
Chad Rosier871f6642011-05-18 19:59:50 +00002816 // Do not sibcall optimize vararg calls unless all arguments are passed via
Chad Rosiera1660892011-05-20 00:59:28 +00002817 // registers.
Chad Rosier871f6642011-05-18 19:59:50 +00002818 if (isVarArg && !Outs.empty()) {
Chad Rosiera1660892011-05-20 00:59:28 +00002819
2820 // Optimizing for varargs on Win64 is unlikely to be safe without
2821 // additional testing.
2822 if (Subtarget->isTargetWin64())
2823 return false;
2824
Chad Rosier871f6642011-05-18 19:59:50 +00002825 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002826 CCState CCInfo(CalleeCC, isVarArg, DAG.getMachineFunction(),
Craig Topper0fbf3642012-04-23 03:28:34 +00002827 getTargetMachine(), ArgLocs, *DAG.getContext());
Chad Rosier871f6642011-05-18 19:59:50 +00002828
Chad Rosier871f6642011-05-18 19:59:50 +00002829 CCInfo.AnalyzeCallOperands(Outs, CC_X86);
2830 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i)
2831 if (!ArgLocs[i].isRegLoc())
2832 return false;
2833 }
2834
Chad Rosier30450e82011-12-22 22:35:21 +00002835 // If the call result is in ST0 / ST1, it needs to be popped off the x87
2836 // stack. Therefore, if it's not used by the call it is not safe to optimize
2837 // this into a sibcall.
Evan Chengf5b9d6c2010-03-20 02:58:15 +00002838 bool Unused = false;
2839 for (unsigned i = 0, e = Ins.size(); i != e; ++i) {
2840 if (!Ins[i].Used) {
2841 Unused = true;
2842 break;
2843 }
2844 }
2845 if (Unused) {
2846 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002847 CCState CCInfo(CalleeCC, false, DAG.getMachineFunction(),
Craig Topper0fbf3642012-04-23 03:28:34 +00002848 getTargetMachine(), RVLocs, *DAG.getContext());
Evan Chengf5b9d6c2010-03-20 02:58:15 +00002849 CCInfo.AnalyzeCallResult(Ins, RetCC_X86);
Evan Cheng13617962010-04-30 01:12:32 +00002850 for (unsigned i = 0, e = RVLocs.size(); i != e; ++i) {
Evan Chengf5b9d6c2010-03-20 02:58:15 +00002851 CCValAssign &VA = RVLocs[i];
2852 if (VA.getLocReg() == X86::ST0 || VA.getLocReg() == X86::ST1)
2853 return false;
2854 }
2855 }
2856
Evan Cheng13617962010-04-30 01:12:32 +00002857 // If the calling conventions do not match, then we'd better make sure the
2858 // results are returned in the same way as what the caller expects.
2859 if (!CCMatch) {
2860 SmallVector<CCValAssign, 16> RVLocs1;
Eric Christopher471e4222011-06-08 23:55:35 +00002861 CCState CCInfo1(CalleeCC, false, DAG.getMachineFunction(),
Craig Topper0fbf3642012-04-23 03:28:34 +00002862 getTargetMachine(), RVLocs1, *DAG.getContext());
Evan Cheng13617962010-04-30 01:12:32 +00002863 CCInfo1.AnalyzeCallResult(Ins, RetCC_X86);
2864
2865 SmallVector<CCValAssign, 16> RVLocs2;
Eric Christopher471e4222011-06-08 23:55:35 +00002866 CCState CCInfo2(CallerCC, false, DAG.getMachineFunction(),
Craig Topper0fbf3642012-04-23 03:28:34 +00002867 getTargetMachine(), RVLocs2, *DAG.getContext());
Evan Cheng13617962010-04-30 01:12:32 +00002868 CCInfo2.AnalyzeCallResult(Ins, RetCC_X86);
2869
2870 if (RVLocs1.size() != RVLocs2.size())
2871 return false;
2872 for (unsigned i = 0, e = RVLocs1.size(); i != e; ++i) {
2873 if (RVLocs1[i].isRegLoc() != RVLocs2[i].isRegLoc())
2874 return false;
2875 if (RVLocs1[i].getLocInfo() != RVLocs2[i].getLocInfo())
2876 return false;
2877 if (RVLocs1[i].isRegLoc()) {
2878 if (RVLocs1[i].getLocReg() != RVLocs2[i].getLocReg())
2879 return false;
2880 } else {
2881 if (RVLocs1[i].getLocMemOffset() != RVLocs2[i].getLocMemOffset())
2882 return false;
2883 }
2884 }
2885 }
2886
Evan Chenga6bff982010-01-30 01:22:00 +00002887 // If the callee takes no arguments then go on to check the results of the
2888 // call.
2889 if (!Outs.empty()) {
2890 // Check if stack adjustment is needed. For now, do not do this if any
2891 // argument is passed on the stack.
2892 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002893 CCState CCInfo(CalleeCC, isVarArg, DAG.getMachineFunction(),
Craig Topper0fbf3642012-04-23 03:28:34 +00002894 getTargetMachine(), ArgLocs, *DAG.getContext());
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00002895
2896 // Allocate shadow area for Win64
2897 if (Subtarget->isTargetWin64()) {
2898 CCInfo.AllocateStack(32, 8);
2899 }
2900
Duncan Sands45907662010-10-31 13:21:44 +00002901 CCInfo.AnalyzeCallOperands(Outs, CC_X86);
Stuart Hastings6db2c2f2011-05-17 16:59:46 +00002902 if (CCInfo.getNextStackOffset()) {
Evan Chengb2c92902010-02-02 02:22:50 +00002903 MachineFunction &MF = DAG.getMachineFunction();
2904 if (MF.getInfo<X86MachineFunctionInfo>()->getBytesToPopOnReturn())
2905 return false;
Evan Chengb2c92902010-02-02 02:22:50 +00002906
2907 // Check if the arguments are already laid out in the right way as
2908 // the caller's fixed stack objects.
2909 MachineFrameInfo *MFI = MF.getFrameInfo();
Evan Cheng5f941932010-02-05 02:21:12 +00002910 const MachineRegisterInfo *MRI = &MF.getRegInfo();
2911 const X86InstrInfo *TII =
Roman Divacky59324292012-09-05 22:26:57 +00002912 ((const X86TargetMachine&)getTargetMachine()).getInstrInfo();
Evan Chengb2c92902010-02-02 02:22:50 +00002913 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2914 CCValAssign &VA = ArgLocs[i];
Dan Gohmanc9403652010-07-07 15:54:55 +00002915 SDValue Arg = OutVals[i];
Evan Chengb2c92902010-02-02 02:22:50 +00002916 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Evan Chengb2c92902010-02-02 02:22:50 +00002917 if (VA.getLocInfo() == CCValAssign::Indirect)
2918 return false;
2919 if (!VA.isRegLoc()) {
Evan Cheng5f941932010-02-05 02:21:12 +00002920 if (!MatchingStackOffset(Arg, VA.getLocMemOffset(), Flags,
2921 MFI, MRI, TII))
Evan Chengb2c92902010-02-02 02:22:50 +00002922 return false;
2923 }
2924 }
2925 }
Evan Cheng9c044672010-05-29 01:35:22 +00002926
2927 // If the tailcall address may be in a register, then make sure it's
2928 // possible to register allocate for it. In 32-bit, the call address can
2929 // only target EAX, EDX, or ECX since the tail call must be scheduled after
Evan Chengdedd9742010-07-14 06:44:01 +00002930 // callee-saved registers are restored. These happen to be the same
2931 // registers used to pass 'inreg' arguments so watch out for those.
2932 if (!Subtarget->is64Bit() &&
2933 !isa<GlobalAddressSDNode>(Callee) &&
Evan Cheng9c044672010-05-29 01:35:22 +00002934 !isa<ExternalSymbolSDNode>(Callee)) {
Evan Cheng9c044672010-05-29 01:35:22 +00002935 unsigned NumInRegs = 0;
2936 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2937 CCValAssign &VA = ArgLocs[i];
Evan Chengdedd9742010-07-14 06:44:01 +00002938 if (!VA.isRegLoc())
2939 continue;
2940 unsigned Reg = VA.getLocReg();
2941 switch (Reg) {
2942 default: break;
2943 case X86::EAX: case X86::EDX: case X86::ECX:
2944 if (++NumInRegs == 3)
Evan Cheng9c044672010-05-29 01:35:22 +00002945 return false;
Evan Chengdedd9742010-07-14 06:44:01 +00002946 break;
Evan Cheng9c044672010-05-29 01:35:22 +00002947 }
2948 }
2949 }
Evan Chenga6bff982010-01-30 01:22:00 +00002950 }
Evan Chengb1712452010-01-27 06:25:16 +00002951
Evan Cheng86809cc2010-02-03 03:28:02 +00002952 return true;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002953}
2954
Dan Gohman3df24e62008-09-03 23:12:08 +00002955FastISel *
Bob Wilsond49edb72012-08-03 04:06:28 +00002956X86TargetLowering::createFastISel(FunctionLoweringInfo &funcInfo,
2957 const TargetLibraryInfo *libInfo) const {
2958 return X86::createFastISel(funcInfo, libInfo);
Dan Gohmand9f3c482008-08-19 21:32:53 +00002959}
2960
2961
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00002962//===----------------------------------------------------------------------===//
2963// Other Lowering Hooks
2964//===----------------------------------------------------------------------===//
2965
Bruno Cardoso Lopese654b562010-09-01 00:51:36 +00002966static bool MayFoldLoad(SDValue Op) {
2967 return Op.hasOneUse() && ISD::isNormalLoad(Op.getNode());
2968}
2969
2970static bool MayFoldIntoStore(SDValue Op) {
2971 return Op.hasOneUse() && ISD::isNormalStore(*Op.getNode()->use_begin());
2972}
2973
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002974static bool isTargetShuffle(unsigned Opcode) {
2975 switch(Opcode) {
2976 default: return false;
2977 case X86ISD::PSHUFD:
2978 case X86ISD::PSHUFHW:
2979 case X86ISD::PSHUFLW:
Craig Topperb3982da2011-12-31 23:50:21 +00002980 case X86ISD::SHUFP:
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00002981 case X86ISD::PALIGN:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002982 case X86ISD::MOVLHPS:
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00002983 case X86ISD::MOVLHPD:
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00002984 case X86ISD::MOVHLPS:
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00002985 case X86ISD::MOVLPS:
2986 case X86ISD::MOVLPD:
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00002987 case X86ISD::MOVSHDUP:
Bruno Cardoso Lopes013bb3d2010-08-31 22:35:05 +00002988 case X86ISD::MOVSLDUP:
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00002989 case X86ISD::MOVDDUP:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002990 case X86ISD::MOVSS:
2991 case X86ISD::MOVSD:
Craig Topper34671b82011-12-06 08:21:25 +00002992 case X86ISD::UNPCKL:
2993 case X86ISD::UNPCKH:
Craig Topper316cd2a2011-11-30 06:25:25 +00002994 case X86ISD::VPERMILP:
Craig Topperec24e612011-11-30 07:47:51 +00002995 case X86ISD::VPERM2X128:
Craig Topperbdcbcb32012-05-06 18:54:26 +00002996 case X86ISD::VPERMI:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002997 return true;
2998 }
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002999}
3000
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00003001static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
Craig Topper3d092db2012-03-21 02:14:01 +00003002 SDValue V1, SelectionDAG &DAG) {
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00003003 switch(Opc) {
3004 default: llvm_unreachable("Unknown x86 shuffle node");
3005 case X86ISD::MOVSHDUP:
Bruno Cardoso Lopes013bb3d2010-08-31 22:35:05 +00003006 case X86ISD::MOVSLDUP:
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00003007 case X86ISD::MOVDDUP:
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00003008 return DAG.getNode(Opc, dl, VT, V1);
3009 }
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00003010}
3011
3012static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
Craig Topper3d092db2012-03-21 02:14:01 +00003013 SDValue V1, unsigned TargetMask,
3014 SelectionDAG &DAG) {
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00003015 switch(Opc) {
3016 default: llvm_unreachable("Unknown x86 shuffle node");
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00003017 case X86ISD::PSHUFD:
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00003018 case X86ISD::PSHUFHW:
3019 case X86ISD::PSHUFLW:
Craig Topper316cd2a2011-11-30 06:25:25 +00003020 case X86ISD::VPERMILP:
Craig Topper8325c112012-04-16 00:41:45 +00003021 case X86ISD::VPERMI:
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00003022 return DAG.getNode(Opc, dl, VT, V1, DAG.getConstant(TargetMask, MVT::i8));
3023 }
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00003024}
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00003025
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00003026static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
Craig Topper3d092db2012-03-21 02:14:01 +00003027 SDValue V1, SDValue V2, unsigned TargetMask,
3028 SelectionDAG &DAG) {
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00003029 switch(Opc) {
3030 default: llvm_unreachable("Unknown x86 shuffle node");
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00003031 case X86ISD::PALIGN:
Craig Topperb3982da2011-12-31 23:50:21 +00003032 case X86ISD::SHUFP:
Craig Topperec24e612011-11-30 07:47:51 +00003033 case X86ISD::VPERM2X128:
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00003034 return DAG.getNode(Opc, dl, VT, V1, V2,
3035 DAG.getConstant(TargetMask, MVT::i8));
3036 }
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00003037}
3038
3039static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
3040 SDValue V1, SDValue V2, SelectionDAG &DAG) {
3041 switch(Opc) {
3042 default: llvm_unreachable("Unknown x86 shuffle node");
3043 case X86ISD::MOVLHPS:
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00003044 case X86ISD::MOVLHPD:
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00003045 case X86ISD::MOVHLPS:
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00003046 case X86ISD::MOVLPS:
3047 case X86ISD::MOVLPD:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003048 case X86ISD::MOVSS:
3049 case X86ISD::MOVSD:
Craig Topper34671b82011-12-06 08:21:25 +00003050 case X86ISD::UNPCKL:
3051 case X86ISD::UNPCKH:
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00003052 return DAG.getNode(Opc, dl, VT, V1, V2);
3053 }
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00003054}
3055
Dan Gohmand858e902010-04-17 15:26:15 +00003056SDValue X86TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) const {
Anton Korobeynikova2780e12007-08-15 17:12:32 +00003057 MachineFunction &MF = DAG.getMachineFunction();
3058 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
3059 int ReturnAddrIndex = FuncInfo->getRAIndex();
3060
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00003061 if (ReturnAddrIndex == 0) {
3062 // Set up a frame object for the return address.
Michael Liaoaa3c2c02012-10-25 06:29:14 +00003063 unsigned SlotSize = RegInfo->getSlotSize();
David Greene3f2bf852009-11-12 20:49:22 +00003064 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(SlotSize, -SlotSize,
Evan Chenged2ae132010-07-03 00:40:23 +00003065 false);
Anton Korobeynikova2780e12007-08-15 17:12:32 +00003066 FuncInfo->setRAIndex(ReturnAddrIndex);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00003067 }
3068
Evan Cheng25ab6902006-09-08 06:48:29 +00003069 return DAG.getFrameIndex(ReturnAddrIndex, getPointerTy());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00003070}
3071
3072
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00003073bool X86::isOffsetSuitableForCodeModel(int64_t Offset, CodeModel::Model M,
3074 bool hasSymbolicDisplacement) {
3075 // Offset should fit into 32 bit immediate field.
Benjamin Kramer34247a02010-03-29 21:13:41 +00003076 if (!isInt<32>(Offset))
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00003077 return false;
3078
3079 // If we don't have a symbolic displacement - we don't have any extra
3080 // restrictions.
3081 if (!hasSymbolicDisplacement)
3082 return true;
3083
3084 // FIXME: Some tweaks might be needed for medium code model.
3085 if (M != CodeModel::Small && M != CodeModel::Kernel)
3086 return false;
3087
3088 // For small code model we assume that latest object is 16MB before end of 31
3089 // bits boundary. We may also accept pretty large negative constants knowing
3090 // that all objects are in the positive half of address space.
3091 if (M == CodeModel::Small && Offset < 16*1024*1024)
3092 return true;
3093
3094 // For kernel code model we know that all object resist in the negative half
3095 // of 32bits address space. We may not accept negative offsets, since they may
3096 // be just off and we may accept pretty large positive ones.
3097 if (M == CodeModel::Kernel && Offset > 0)
3098 return true;
3099
3100 return false;
3101}
3102
Evan Chengef41ff62011-06-23 17:54:54 +00003103/// isCalleePop - Determines whether the callee is required to pop its
3104/// own arguments. Callee pop is necessary to support tail calls.
3105bool X86::isCalleePop(CallingConv::ID CallingConv,
3106 bool is64Bit, bool IsVarArg, bool TailCallOpt) {
3107 if (IsVarArg)
3108 return false;
3109
3110 switch (CallingConv) {
3111 default:
3112 return false;
3113 case CallingConv::X86_StdCall:
3114 return !is64Bit;
3115 case CallingConv::X86_FastCall:
3116 return !is64Bit;
3117 case CallingConv::X86_ThisCall:
3118 return !is64Bit;
3119 case CallingConv::Fast:
3120 return TailCallOpt;
3121 case CallingConv::GHC:
3122 return TailCallOpt;
Duncan Sandsdc7f1742012-11-16 12:36:39 +00003123 case CallingConv::HiPE:
3124 return TailCallOpt;
Evan Chengef41ff62011-06-23 17:54:54 +00003125 }
3126}
3127
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003128/// TranslateX86CC - do a one to one translation of a ISD::CondCode to the X86
3129/// specific condition code, returning the condition code and the LHS/RHS of the
3130/// comparison to make.
3131static unsigned TranslateX86CC(ISD::CondCode SetCCOpcode, bool isFP,
3132 SDValue &LHS, SDValue &RHS, SelectionDAG &DAG) {
Evan Chengd9558e02006-01-06 00:43:03 +00003133 if (!isFP) {
Chris Lattnerbfd68a72006-09-13 17:04:54 +00003134 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS)) {
3135 if (SetCCOpcode == ISD::SETGT && RHSC->isAllOnesValue()) {
3136 // X > -1 -> X == 0, jump !sign.
3137 RHS = DAG.getConstant(0, RHS.getValueType());
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003138 return X86::COND_NS;
Craig Topper69947b92012-04-23 06:57:04 +00003139 }
3140 if (SetCCOpcode == ISD::SETLT && RHSC->isNullValue()) {
Chris Lattnerbfd68a72006-09-13 17:04:54 +00003141 // X < 0 -> X == 0, jump on sign.
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003142 return X86::COND_S;
Craig Topper69947b92012-04-23 06:57:04 +00003143 }
3144 if (SetCCOpcode == ISD::SETLT && RHSC->getZExtValue() == 1) {
Dan Gohman5f6913c2007-09-17 14:49:27 +00003145 // X < 1 -> X <= 0
3146 RHS = DAG.getConstant(0, RHS.getValueType());
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003147 return X86::COND_LE;
Chris Lattnerbfd68a72006-09-13 17:04:54 +00003148 }
Chris Lattnerf9570512006-09-13 03:22:10 +00003149 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00003150
Evan Chengd9558e02006-01-06 00:43:03 +00003151 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00003152 default: llvm_unreachable("Invalid integer condition!");
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003153 case ISD::SETEQ: return X86::COND_E;
3154 case ISD::SETGT: return X86::COND_G;
3155 case ISD::SETGE: return X86::COND_GE;
3156 case ISD::SETLT: return X86::COND_L;
3157 case ISD::SETLE: return X86::COND_LE;
3158 case ISD::SETNE: return X86::COND_NE;
3159 case ISD::SETULT: return X86::COND_B;
3160 case ISD::SETUGT: return X86::COND_A;
3161 case ISD::SETULE: return X86::COND_BE;
3162 case ISD::SETUGE: return X86::COND_AE;
Evan Chengd9558e02006-01-06 00:43:03 +00003163 }
Chris Lattner4c78e022008-12-23 23:42:27 +00003164 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003165
Chris Lattner4c78e022008-12-23 23:42:27 +00003166 // First determine if it is required or is profitable to flip the operands.
Duncan Sands4047f4a2008-10-24 13:03:10 +00003167
Chris Lattner4c78e022008-12-23 23:42:27 +00003168 // If LHS is a foldable load, but RHS is not, flip the condition.
Rafael Espindolaf297c932011-02-03 03:58:05 +00003169 if (ISD::isNON_EXTLoad(LHS.getNode()) &&
3170 !ISD::isNON_EXTLoad(RHS.getNode())) {
Chris Lattner4c78e022008-12-23 23:42:27 +00003171 SetCCOpcode = getSetCCSwappedOperands(SetCCOpcode);
3172 std::swap(LHS, RHS);
Evan Cheng4d46d0a2008-08-28 23:48:31 +00003173 }
3174
Chris Lattner4c78e022008-12-23 23:42:27 +00003175 switch (SetCCOpcode) {
3176 default: break;
3177 case ISD::SETOLT:
3178 case ISD::SETOLE:
3179 case ISD::SETUGT:
3180 case ISD::SETUGE:
3181 std::swap(LHS, RHS);
3182 break;
3183 }
3184
3185 // On a floating point condition, the flags are set as follows:
3186 // ZF PF CF op
3187 // 0 | 0 | 0 | X > Y
3188 // 0 | 0 | 1 | X < Y
3189 // 1 | 0 | 0 | X == Y
3190 // 1 | 1 | 1 | unordered
3191 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00003192 default: llvm_unreachable("Condcode should be pre-legalized away");
Chris Lattner4c78e022008-12-23 23:42:27 +00003193 case ISD::SETUEQ:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003194 case ISD::SETEQ: return X86::COND_E;
Chris Lattner4c78e022008-12-23 23:42:27 +00003195 case ISD::SETOLT: // flipped
3196 case ISD::SETOGT:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003197 case ISD::SETGT: return X86::COND_A;
Chris Lattner4c78e022008-12-23 23:42:27 +00003198 case ISD::SETOLE: // flipped
3199 case ISD::SETOGE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003200 case ISD::SETGE: return X86::COND_AE;
Chris Lattner4c78e022008-12-23 23:42:27 +00003201 case ISD::SETUGT: // flipped
3202 case ISD::SETULT:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003203 case ISD::SETLT: return X86::COND_B;
Chris Lattner4c78e022008-12-23 23:42:27 +00003204 case ISD::SETUGE: // flipped
3205 case ISD::SETULE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003206 case ISD::SETLE: return X86::COND_BE;
Chris Lattner4c78e022008-12-23 23:42:27 +00003207 case ISD::SETONE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003208 case ISD::SETNE: return X86::COND_NE;
3209 case ISD::SETUO: return X86::COND_P;
3210 case ISD::SETO: return X86::COND_NP;
Dan Gohman1a492952009-10-20 16:22:37 +00003211 case ISD::SETOEQ:
3212 case ISD::SETUNE: return X86::COND_INVALID;
Chris Lattner4c78e022008-12-23 23:42:27 +00003213 }
Evan Chengd9558e02006-01-06 00:43:03 +00003214}
3215
Evan Cheng4a460802006-01-11 00:33:36 +00003216/// hasFPCMov - is there a floating point cmov for the specific X86 condition
3217/// code. Current x86 isa includes the following FP cmov instructions:
Evan Chengaaca22c2006-01-10 20:26:56 +00003218/// fcmovb, fcomvbe, fcomve, fcmovu, fcmovae, fcmova, fcmovne, fcmovnu.
Evan Cheng4a460802006-01-11 00:33:36 +00003219static bool hasFPCMov(unsigned X86CC) {
Evan Chengaaca22c2006-01-10 20:26:56 +00003220 switch (X86CC) {
3221 default:
3222 return false;
Chris Lattner7fbe9722006-10-20 17:42:20 +00003223 case X86::COND_B:
3224 case X86::COND_BE:
3225 case X86::COND_E:
3226 case X86::COND_P:
3227 case X86::COND_A:
3228 case X86::COND_AE:
3229 case X86::COND_NE:
3230 case X86::COND_NP:
Evan Chengaaca22c2006-01-10 20:26:56 +00003231 return true;
3232 }
3233}
3234
Evan Chengeb2f9692009-10-27 19:56:55 +00003235/// isFPImmLegal - Returns true if the target can instruction select the
3236/// specified FP immediate natively. If false, the legalizer will
3237/// materialize the FP immediate as a load from a constant pool.
Evan Chenga1eaa3c2009-10-28 01:43:28 +00003238bool X86TargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
Evan Chengeb2f9692009-10-27 19:56:55 +00003239 for (unsigned i = 0, e = LegalFPImmediates.size(); i != e; ++i) {
3240 if (Imm.bitwiseIsEqual(LegalFPImmediates[i]))
3241 return true;
3242 }
3243 return false;
3244}
3245
Nate Begeman9008ca62009-04-27 18:41:29 +00003246/// isUndefOrInRange - Return true if Val is undef or if its value falls within
3247/// the specified range (L, H].
3248static bool isUndefOrInRange(int Val, int Low, int Hi) {
3249 return (Val < 0) || (Val >= Low && Val < Hi);
3250}
3251
3252/// isUndefOrEqual - Val is either less than zero (undef) or equal to the
3253/// specified value.
3254static bool isUndefOrEqual(int Val, int CmpVal) {
3255 if (Val < 0 || Val == CmpVal)
Evan Cheng5ced1d82006-04-06 23:23:56 +00003256 return true;
Nate Begeman9008ca62009-04-27 18:41:29 +00003257 return false;
Evan Chengc5cdff22006-04-07 21:53:05 +00003258}
3259
Benjamin Kramerd9b0b022012-06-02 10:20:22 +00003260/// isSequentialOrUndefInRange - Return true if every element in Mask, beginning
Bruno Cardoso Lopes4002d7e2011-08-12 21:54:42 +00003261/// from position Pos and ending in Pos+Size, falls within the specified
3262/// sequential range (L, L+Pos]. or is undef.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003263static bool isSequentialOrUndefInRange(ArrayRef<int> Mask,
Craig Topperb6072642012-05-03 07:26:59 +00003264 unsigned Pos, unsigned Size, int Low) {
3265 for (unsigned i = Pos, e = Pos+Size; i != e; ++i, ++Low)
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003266 if (!isUndefOrEqual(Mask[i], Low))
3267 return false;
3268 return true;
3269}
3270
Nate Begeman9008ca62009-04-27 18:41:29 +00003271/// isPSHUFDMask - Return true if the node specifies a shuffle of elements that
3272/// is suitable for input to PSHUFD or PSHUFW. That is, it doesn't reference
3273/// the second operand.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003274static bool isPSHUFDMask(ArrayRef<int> Mask, EVT VT) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00003275 if (VT == MVT::v4f32 || VT == MVT::v4i32 )
Nate Begeman9008ca62009-04-27 18:41:29 +00003276 return (Mask[0] < 4 && Mask[1] < 4 && Mask[2] < 4 && Mask[3] < 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00003277 if (VT == MVT::v2f64 || VT == MVT::v2i64)
Nate Begeman9008ca62009-04-27 18:41:29 +00003278 return (Mask[0] < 2 && Mask[1] < 2);
3279 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003280}
3281
Nate Begeman9008ca62009-04-27 18:41:29 +00003282/// isPSHUFHWMask - Return true if the node specifies a shuffle of elements that
3283/// is suitable for input to PSHUFHW.
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00003284static bool isPSHUFHWMask(ArrayRef<int> Mask, EVT VT, bool HasInt256) {
3285 if (VT != MVT::v8i16 && (!HasInt256 || VT != MVT::v16i16))
Evan Cheng0188ecb2006-03-22 18:59:22 +00003286 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003287
Nate Begeman9008ca62009-04-27 18:41:29 +00003288 // Lower quadword copied in order or undef.
Craig Topperc612d792012-01-02 09:17:37 +00003289 if (!isSequentialOrUndefInRange(Mask, 0, 4, 0))
3290 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003291
Evan Cheng506d3df2006-03-29 23:07:14 +00003292 // Upper quadword shuffled.
Craig Topperc612d792012-01-02 09:17:37 +00003293 for (unsigned i = 4; i != 8; ++i)
Craig Toppera9a568a2012-05-02 08:03:44 +00003294 if (!isUndefOrInRange(Mask[i], 4, 8))
Evan Cheng506d3df2006-03-29 23:07:14 +00003295 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003296
Craig Toppera9a568a2012-05-02 08:03:44 +00003297 if (VT == MVT::v16i16) {
3298 // Lower quadword copied in order or undef.
3299 if (!isSequentialOrUndefInRange(Mask, 8, 4, 8))
3300 return false;
3301
3302 // Upper quadword shuffled.
3303 for (unsigned i = 12; i != 16; ++i)
3304 if (!isUndefOrInRange(Mask[i], 12, 16))
3305 return false;
3306 }
3307
Evan Cheng506d3df2006-03-29 23:07:14 +00003308 return true;
3309}
3310
Nate Begeman9008ca62009-04-27 18:41:29 +00003311/// isPSHUFLWMask - Return true if the node specifies a shuffle of elements that
3312/// is suitable for input to PSHUFLW.
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00003313static bool isPSHUFLWMask(ArrayRef<int> Mask, EVT VT, bool HasInt256) {
3314 if (VT != MVT::v8i16 && (!HasInt256 || VT != MVT::v16i16))
Evan Cheng506d3df2006-03-29 23:07:14 +00003315 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003316
Rafael Espindola15684b22009-04-24 12:40:33 +00003317 // Upper quadword copied in order.
Craig Topperc612d792012-01-02 09:17:37 +00003318 if (!isSequentialOrUndefInRange(Mask, 4, 4, 4))
3319 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003320
Rafael Espindola15684b22009-04-24 12:40:33 +00003321 // Lower quadword shuffled.
Craig Topperc612d792012-01-02 09:17:37 +00003322 for (unsigned i = 0; i != 4; ++i)
Craig Toppera9a568a2012-05-02 08:03:44 +00003323 if (!isUndefOrInRange(Mask[i], 0, 4))
Rafael Espindola15684b22009-04-24 12:40:33 +00003324 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003325
Craig Toppera9a568a2012-05-02 08:03:44 +00003326 if (VT == MVT::v16i16) {
3327 // Upper quadword copied in order.
3328 if (!isSequentialOrUndefInRange(Mask, 12, 4, 12))
3329 return false;
3330
3331 // Lower quadword shuffled.
3332 for (unsigned i = 8; i != 12; ++i)
3333 if (!isUndefOrInRange(Mask[i], 8, 12))
3334 return false;
3335 }
3336
Rafael Espindola15684b22009-04-24 12:40:33 +00003337 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00003338}
3339
Nate Begemana09008b2009-10-19 02:17:23 +00003340/// isPALIGNRMask - Return true if the node specifies a shuffle of elements that
3341/// is suitable for input to PALIGNR.
Craig Topper0e2037b2012-01-20 05:53:00 +00003342static bool isPALIGNRMask(ArrayRef<int> Mask, EVT VT,
3343 const X86Subtarget *Subtarget) {
3344 if ((VT.getSizeInBits() == 128 && !Subtarget->hasSSSE3()) ||
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00003345 (VT.getSizeInBits() == 256 && !Subtarget->hasInt256()))
Bruno Cardoso Lopes9065d4b2011-07-29 01:30:59 +00003346 return false;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003347
Craig Topper0e2037b2012-01-20 05:53:00 +00003348 unsigned NumElts = VT.getVectorNumElements();
3349 unsigned NumLanes = VT.getSizeInBits()/128;
3350 unsigned NumLaneElts = NumElts/NumLanes;
3351
3352 // Do not handle 64-bit element shuffles with palignr.
3353 if (NumLaneElts == 2)
Nate Begemana09008b2009-10-19 02:17:23 +00003354 return false;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003355
Craig Topper0e2037b2012-01-20 05:53:00 +00003356 for (unsigned l = 0; l != NumElts; l+=NumLaneElts) {
3357 unsigned i;
3358 for (i = 0; i != NumLaneElts; ++i) {
3359 if (Mask[i+l] >= 0)
3360 break;
3361 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00003362
Craig Topper0e2037b2012-01-20 05:53:00 +00003363 // Lane is all undef, go to next lane
3364 if (i == NumLaneElts)
3365 continue;
Nate Begemana09008b2009-10-19 02:17:23 +00003366
Craig Topper0e2037b2012-01-20 05:53:00 +00003367 int Start = Mask[i+l];
Nate Begemana09008b2009-10-19 02:17:23 +00003368
Craig Topper0e2037b2012-01-20 05:53:00 +00003369 // Make sure its in this lane in one of the sources
3370 if (!isUndefOrInRange(Start, l, l+NumLaneElts) &&
3371 !isUndefOrInRange(Start, l+NumElts, l+NumElts+NumLaneElts))
Nate Begemana09008b2009-10-19 02:17:23 +00003372 return false;
Craig Topper0e2037b2012-01-20 05:53:00 +00003373
3374 // If not lane 0, then we must match lane 0
3375 if (l != 0 && Mask[i] >= 0 && !isUndefOrEqual(Start, Mask[i]+l))
3376 return false;
3377
3378 // Correct second source to be contiguous with first source
3379 if (Start >= (int)NumElts)
3380 Start -= NumElts - NumLaneElts;
3381
3382 // Make sure we're shifting in the right direction.
3383 if (Start <= (int)(i+l))
3384 return false;
3385
3386 Start -= i;
3387
3388 // Check the rest of the elements to see if they are consecutive.
3389 for (++i; i != NumLaneElts; ++i) {
3390 int Idx = Mask[i+l];
3391
3392 // Make sure its in this lane
3393 if (!isUndefOrInRange(Idx, l, l+NumLaneElts) &&
3394 !isUndefOrInRange(Idx, l+NumElts, l+NumElts+NumLaneElts))
3395 return false;
3396
3397 // If not lane 0, then we must match lane 0
3398 if (l != 0 && Mask[i] >= 0 && !isUndefOrEqual(Idx, Mask[i]+l))
3399 return false;
3400
3401 if (Idx >= (int)NumElts)
3402 Idx -= NumElts - NumLaneElts;
3403
3404 if (!isUndefOrEqual(Idx, Start+i))
3405 return false;
3406
3407 }
Nate Begemana09008b2009-10-19 02:17:23 +00003408 }
Craig Topper0e2037b2012-01-20 05:53:00 +00003409
Nate Begemana09008b2009-10-19 02:17:23 +00003410 return true;
3411}
3412
Craig Topper1a7700a2012-01-19 08:19:12 +00003413/// CommuteVectorShuffleMask - Change values in a shuffle permute mask assuming
3414/// the two vector operands have swapped position.
3415static void CommuteVectorShuffleMask(SmallVectorImpl<int> &Mask,
3416 unsigned NumElems) {
3417 for (unsigned i = 0; i != NumElems; ++i) {
3418 int idx = Mask[i];
3419 if (idx < 0)
3420 continue;
3421 else if (idx < (int)NumElems)
3422 Mask[i] = idx + NumElems;
3423 else
3424 Mask[i] = idx - NumElems;
3425 }
3426}
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00003427
Craig Topper1a7700a2012-01-19 08:19:12 +00003428/// isSHUFPMask - Return true if the specified VECTOR_SHUFFLE operand
3429/// specifies a shuffle of elements that is suitable for input to 128/256-bit
3430/// SHUFPS and SHUFPD. If Commuted is true, then it checks for sources to be
3431/// reverse of what x86 shuffles want.
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00003432static bool isSHUFPMask(ArrayRef<int> Mask, EVT VT, bool HasFp256,
Craig Topper1a7700a2012-01-19 08:19:12 +00003433 bool Commuted = false) {
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00003434 if (!HasFp256 && VT.getSizeInBits() == 256)
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00003435 return false;
3436
Craig Topper1a7700a2012-01-19 08:19:12 +00003437 unsigned NumElems = VT.getVectorNumElements();
3438 unsigned NumLanes = VT.getSizeInBits()/128;
3439 unsigned NumLaneElems = NumElems/NumLanes;
3440
3441 if (NumLaneElems != 2 && NumLaneElems != 4)
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00003442 return false;
3443
3444 // VSHUFPSY divides the resulting vector into 4 chunks.
3445 // The sources are also splitted into 4 chunks, and each destination
3446 // chunk must come from a different source chunk.
3447 //
3448 // SRC1 => X7 X6 X5 X4 X3 X2 X1 X0
3449 // SRC2 => Y7 Y6 Y5 Y4 Y3 Y2 Y1 Y9
3450 //
3451 // DST => Y7..Y4, Y7..Y4, X7..X4, X7..X4,
3452 // Y3..Y0, Y3..Y0, X3..X0, X3..X0
3453 //
Craig Topper9d7025b2011-11-27 21:41:12 +00003454 // VSHUFPDY divides the resulting vector into 4 chunks.
3455 // The sources are also splitted into 4 chunks, and each destination
3456 // chunk must come from a different source chunk.
3457 //
3458 // SRC1 => X3 X2 X1 X0
3459 // SRC2 => Y3 Y2 Y1 Y0
3460 //
3461 // DST => Y3..Y2, X3..X2, Y1..Y0, X1..X0
3462 //
Craig Topper1a7700a2012-01-19 08:19:12 +00003463 unsigned HalfLaneElems = NumLaneElems/2;
3464 for (unsigned l = 0; l != NumElems; l += NumLaneElems) {
3465 for (unsigned i = 0; i != NumLaneElems; ++i) {
3466 int Idx = Mask[i+l];
3467 unsigned RngStart = l + ((Commuted == (i<HalfLaneElems)) ? NumElems : 0);
3468 if (!isUndefOrInRange(Idx, RngStart, RngStart+NumLaneElems))
3469 return false;
3470 // For VSHUFPSY, the mask of the second half must be the same as the
3471 // first but with the appropriate offsets. This works in the same way as
3472 // VPERMILPS works with masks.
3473 if (NumElems != 8 || l == 0 || Mask[i] < 0)
3474 continue;
3475 if (!isUndefOrEqual(Idx, Mask[i]+l))
3476 return false;
Craig Topper1ff73d72011-12-06 04:59:07 +00003477 }
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00003478 }
3479
3480 return true;
3481}
3482
Evan Cheng2c0dbd02006-03-24 02:58:06 +00003483/// isMOVHLPSMask - Return true if the specified VECTOR_SHUFFLE operand
3484/// specifies a shuffle of elements that is suitable for input to MOVHLPS.
Craig Topperdd637ae2012-02-19 05:41:45 +00003485static bool isMOVHLPSMask(ArrayRef<int> Mask, EVT VT) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00003486 if (!VT.is128BitVector())
Bruno Cardoso Lopes61260052011-07-29 02:05:28 +00003487 return false;
3488
Craig Topper7a9a28b2012-08-12 02:23:29 +00003489 unsigned NumElems = VT.getVectorNumElements();
3490
Bruno Cardoso Lopes61260052011-07-29 02:05:28 +00003491 if (NumElems != 4)
Evan Cheng2c0dbd02006-03-24 02:58:06 +00003492 return false;
3493
Evan Cheng2064a2b2006-03-28 06:50:32 +00003494 // Expect bit0 == 6, bit1 == 7, bit2 == 2, bit3 == 3
Craig Topperdd637ae2012-02-19 05:41:45 +00003495 return isUndefOrEqual(Mask[0], 6) &&
3496 isUndefOrEqual(Mask[1], 7) &&
3497 isUndefOrEqual(Mask[2], 2) &&
3498 isUndefOrEqual(Mask[3], 3);
Evan Cheng6e56e2c2006-11-07 22:14:24 +00003499}
3500
Nate Begeman0b10b912009-11-07 23:17:15 +00003501/// isMOVHLPS_v_undef_Mask - Special case of isMOVHLPSMask for canonical form
3502/// of vector_shuffle v, v, <2, 3, 2, 3>, i.e. vector_shuffle v, undef,
3503/// <2, 3, 2, 3>
Craig Topperdd637ae2012-02-19 05:41:45 +00003504static bool isMOVHLPS_v_undef_Mask(ArrayRef<int> Mask, EVT VT) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00003505 if (!VT.is128BitVector())
Bruno Cardoso Lopes61260052011-07-29 02:05:28 +00003506 return false;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003507
Craig Topper7a9a28b2012-08-12 02:23:29 +00003508 unsigned NumElems = VT.getVectorNumElements();
3509
Nate Begeman0b10b912009-11-07 23:17:15 +00003510 if (NumElems != 4)
3511 return false;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003512
Craig Topperdd637ae2012-02-19 05:41:45 +00003513 return isUndefOrEqual(Mask[0], 2) &&
3514 isUndefOrEqual(Mask[1], 3) &&
3515 isUndefOrEqual(Mask[2], 2) &&
3516 isUndefOrEqual(Mask[3], 3);
Nate Begeman0b10b912009-11-07 23:17:15 +00003517}
3518
Evan Cheng5ced1d82006-04-06 23:23:56 +00003519/// isMOVLPMask - Return true if the specified VECTOR_SHUFFLE operand
3520/// specifies a shuffle of elements that is suitable for input to MOVLP{S|D}.
Craig Topperdd637ae2012-02-19 05:41:45 +00003521static bool isMOVLPMask(ArrayRef<int> Mask, EVT VT) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00003522 if (!VT.is128BitVector())
Elena Demikhovsky021c0a22011-12-28 08:14:01 +00003523 return false;
3524
Craig Topperdd637ae2012-02-19 05:41:45 +00003525 unsigned NumElems = VT.getVectorNumElements();
Evan Cheng5ced1d82006-04-06 23:23:56 +00003526
Evan Cheng5ced1d82006-04-06 23:23:56 +00003527 if (NumElems != 2 && NumElems != 4)
3528 return false;
3529
Chad Rosier238ae312012-04-30 17:47:15 +00003530 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
Craig Topperdd637ae2012-02-19 05:41:45 +00003531 if (!isUndefOrEqual(Mask[i], i + NumElems))
Evan Chengc5cdff22006-04-07 21:53:05 +00003532 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003533
Chad Rosier238ae312012-04-30 17:47:15 +00003534 for (unsigned i = NumElems/2, e = NumElems; i != e; ++i)
Craig Topperdd637ae2012-02-19 05:41:45 +00003535 if (!isUndefOrEqual(Mask[i], i))
Evan Chengc5cdff22006-04-07 21:53:05 +00003536 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003537
3538 return true;
3539}
3540
Nate Begeman0b10b912009-11-07 23:17:15 +00003541/// isMOVLHPSMask - Return true if the specified VECTOR_SHUFFLE operand
3542/// specifies a shuffle of elements that is suitable for input to MOVLHPS.
Craig Topperdd637ae2012-02-19 05:41:45 +00003543static bool isMOVLHPSMask(ArrayRef<int> Mask, EVT VT) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00003544 if (!VT.is128BitVector())
3545 return false;
3546
Craig Topperdd637ae2012-02-19 05:41:45 +00003547 unsigned NumElems = VT.getVectorNumElements();
Evan Cheng5ced1d82006-04-06 23:23:56 +00003548
Craig Topper7a9a28b2012-08-12 02:23:29 +00003549 if (NumElems != 2 && NumElems != 4)
Evan Cheng5ced1d82006-04-06 23:23:56 +00003550 return false;
3551
Chad Rosier238ae312012-04-30 17:47:15 +00003552 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
Craig Topperdd637ae2012-02-19 05:41:45 +00003553 if (!isUndefOrEqual(Mask[i], i))
Evan Chengc5cdff22006-04-07 21:53:05 +00003554 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003555
Chad Rosier238ae312012-04-30 17:47:15 +00003556 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
3557 if (!isUndefOrEqual(Mask[i + e], i + NumElems))
Evan Chengc5cdff22006-04-07 21:53:05 +00003558 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003559
3560 return true;
3561}
3562
Elena Demikhovsky15963732012-06-26 08:04:10 +00003563//
3564// Some special combinations that can be optimized.
3565//
3566static
3567SDValue Compact8x32ShuffleNode(ShuffleVectorSDNode *SVOp,
3568 SelectionDAG &DAG) {
3569 EVT VT = SVOp->getValueType(0);
Elena Demikhovsky15963732012-06-26 08:04:10 +00003570 DebugLoc dl = SVOp->getDebugLoc();
3571
3572 if (VT != MVT::v8i32 && VT != MVT::v8f32)
3573 return SDValue();
3574
3575 ArrayRef<int> Mask = SVOp->getMask();
3576
3577 // These are the special masks that may be optimized.
3578 static const int MaskToOptimizeEven[] = {0, 8, 2, 10, 4, 12, 6, 14};
3579 static const int MaskToOptimizeOdd[] = {1, 9, 3, 11, 5, 13, 7, 15};
3580 bool MatchEvenMask = true;
3581 bool MatchOddMask = true;
3582 for (int i=0; i<8; ++i) {
3583 if (!isUndefOrEqual(Mask[i], MaskToOptimizeEven[i]))
3584 MatchEvenMask = false;
3585 if (!isUndefOrEqual(Mask[i], MaskToOptimizeOdd[i]))
3586 MatchOddMask = false;
3587 }
Elena Demikhovsky15963732012-06-26 08:04:10 +00003588
Elena Demikhovsky32510202012-09-04 12:49:02 +00003589 if (!MatchEvenMask && !MatchOddMask)
Elena Demikhovsky15963732012-06-26 08:04:10 +00003590 return SDValue();
Michael Liao471b9172012-10-03 23:43:52 +00003591
Elena Demikhovsky15963732012-06-26 08:04:10 +00003592 SDValue UndefNode = DAG.getNode(ISD::UNDEF, dl, VT);
3593
Elena Demikhovsky32510202012-09-04 12:49:02 +00003594 SDValue Op0 = SVOp->getOperand(0);
3595 SDValue Op1 = SVOp->getOperand(1);
3596
3597 if (MatchEvenMask) {
3598 // Shift the second operand right to 32 bits.
3599 static const int ShiftRightMask[] = {-1, 0, -1, 2, -1, 4, -1, 6 };
3600 Op1 = DAG.getVectorShuffle(VT, dl, Op1, UndefNode, ShiftRightMask);
3601 } else {
3602 // Shift the first operand left to 32 bits.
3603 static const int ShiftLeftMask[] = {1, -1, 3, -1, 5, -1, 7, -1 };
3604 Op0 = DAG.getVectorShuffle(VT, dl, Op0, UndefNode, ShiftLeftMask);
3605 }
3606 static const int BlendMask[] = {0, 9, 2, 11, 4, 13, 6, 15};
3607 return DAG.getVectorShuffle(VT, dl, Op0, Op1, BlendMask);
Elena Demikhovsky15963732012-06-26 08:04:10 +00003608}
3609
Evan Cheng0038e592006-03-28 00:39:58 +00003610/// isUNPCKLMask - Return true if the specified VECTOR_SHUFFLE operand
3611/// specifies a shuffle of elements that is suitable for input to UNPCKL.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003612static bool isUNPCKLMask(ArrayRef<int> Mask, EVT VT,
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00003613 bool HasInt256, bool V2IsSplat = false) {
Craig Topper94438ba2011-12-16 08:06:31 +00003614 unsigned NumElts = VT.getVectorNumElements();
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003615
3616 assert((VT.is128BitVector() || VT.is256BitVector()) &&
3617 "Unsupported vector type for unpckh");
3618
Craig Topper6347e862011-11-21 06:57:39 +00003619 if (VT.getSizeInBits() == 256 && NumElts != 4 && NumElts != 8 &&
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00003620 (!HasInt256 || (NumElts != 16 && NumElts != 32)))
Evan Cheng0038e592006-03-28 00:39:58 +00003621 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003622
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003623 // Handle 128 and 256-bit vector lengths. AVX defines UNPCK* to operate
3624 // independently on 128-bit lanes.
3625 unsigned NumLanes = VT.getSizeInBits()/128;
3626 unsigned NumLaneElts = NumElts/NumLanes;
David Greenea20244d2011-03-02 17:23:43 +00003627
Craig Topper94438ba2011-12-16 08:06:31 +00003628 for (unsigned l = 0; l != NumLanes; ++l) {
3629 for (unsigned i = l*NumLaneElts, j = l*NumLaneElts;
3630 i != (l+1)*NumLaneElts;
David Greenea20244d2011-03-02 17:23:43 +00003631 i += 2, ++j) {
3632 int BitI = Mask[i];
3633 int BitI1 = Mask[i+1];
3634 if (!isUndefOrEqual(BitI, j))
Evan Cheng39623da2006-04-20 08:58:49 +00003635 return false;
David Greenea20244d2011-03-02 17:23:43 +00003636 if (V2IsSplat) {
3637 if (!isUndefOrEqual(BitI1, NumElts))
3638 return false;
3639 } else {
3640 if (!isUndefOrEqual(BitI1, j + NumElts))
3641 return false;
3642 }
Evan Cheng39623da2006-04-20 08:58:49 +00003643 }
Evan Cheng0038e592006-03-28 00:39:58 +00003644 }
David Greenea20244d2011-03-02 17:23:43 +00003645
Evan Cheng0038e592006-03-28 00:39:58 +00003646 return true;
3647}
3648
Evan Cheng4fcb9222006-03-28 02:43:26 +00003649/// isUNPCKHMask - Return true if the specified VECTOR_SHUFFLE operand
3650/// specifies a shuffle of elements that is suitable for input to UNPCKH.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003651static bool isUNPCKHMask(ArrayRef<int> Mask, EVT VT,
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00003652 bool HasInt256, bool V2IsSplat = false) {
Craig Topper94438ba2011-12-16 08:06:31 +00003653 unsigned NumElts = VT.getVectorNumElements();
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003654
3655 assert((VT.is128BitVector() || VT.is256BitVector()) &&
3656 "Unsupported vector type for unpckh");
3657
Craig Topper6347e862011-11-21 06:57:39 +00003658 if (VT.getSizeInBits() == 256 && NumElts != 4 && NumElts != 8 &&
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00003659 (!HasInt256 || (NumElts != 16 && NumElts != 32)))
Evan Cheng4fcb9222006-03-28 02:43:26 +00003660 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003661
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003662 // Handle 128 and 256-bit vector lengths. AVX defines UNPCK* to operate
3663 // independently on 128-bit lanes.
3664 unsigned NumLanes = VT.getSizeInBits()/128;
3665 unsigned NumLaneElts = NumElts/NumLanes;
3666
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003667 for (unsigned l = 0; l != NumLanes; ++l) {
Craig Topper94438ba2011-12-16 08:06:31 +00003668 for (unsigned i = l*NumLaneElts, j = (l*NumLaneElts)+NumLaneElts/2;
3669 i != (l+1)*NumLaneElts; i += 2, ++j) {
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003670 int BitI = Mask[i];
3671 int BitI1 = Mask[i+1];
3672 if (!isUndefOrEqual(BitI, j))
Evan Cheng39623da2006-04-20 08:58:49 +00003673 return false;
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003674 if (V2IsSplat) {
3675 if (isUndefOrEqual(BitI1, NumElts))
3676 return false;
3677 } else {
3678 if (!isUndefOrEqual(BitI1, j+NumElts))
3679 return false;
3680 }
Evan Cheng39623da2006-04-20 08:58:49 +00003681 }
Evan Cheng4fcb9222006-03-28 02:43:26 +00003682 }
Evan Cheng4fcb9222006-03-28 02:43:26 +00003683 return true;
3684}
3685
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003686/// isUNPCKL_v_undef_Mask - Special case of isUNPCKLMask for canonical form
3687/// of vector_shuffle v, v, <0, 4, 1, 5>, i.e. vector_shuffle v, undef,
3688/// <0, 0, 1, 1>
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003689static bool isUNPCKL_v_undef_Mask(ArrayRef<int> Mask, EVT VT,
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00003690 bool HasInt256) {
Craig Topper94438ba2011-12-16 08:06:31 +00003691 unsigned NumElts = VT.getVectorNumElements();
3692
3693 assert((VT.is128BitVector() || VT.is256BitVector()) &&
3694 "Unsupported vector type for unpckh");
3695
3696 if (VT.getSizeInBits() == 256 && NumElts != 4 && NumElts != 8 &&
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00003697 (!HasInt256 || (NumElts != 16 && NumElts != 32)))
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003698 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003699
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00003700 // For 256-bit i64/f64, use MOVDDUPY instead, so reject the matching pattern
3701 // FIXME: Need a better way to get rid of this, there's no latency difference
3702 // between UNPCKLPD and MOVDDUP, the later should always be checked first and
3703 // the former later. We should also remove the "_undef" special mask.
Craig Topper94438ba2011-12-16 08:06:31 +00003704 if (NumElts == 4 && VT.getSizeInBits() == 256)
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00003705 return false;
3706
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003707 // Handle 128 and 256-bit vector lengths. AVX defines UNPCK* to operate
3708 // independently on 128-bit lanes.
Craig Topper94438ba2011-12-16 08:06:31 +00003709 unsigned NumLanes = VT.getSizeInBits()/128;
3710 unsigned NumLaneElts = NumElts/NumLanes;
David Greenea20244d2011-03-02 17:23:43 +00003711
Craig Topper94438ba2011-12-16 08:06:31 +00003712 for (unsigned l = 0; l != NumLanes; ++l) {
3713 for (unsigned i = l*NumLaneElts, j = l*NumLaneElts;
3714 i != (l+1)*NumLaneElts;
David Greenea20244d2011-03-02 17:23:43 +00003715 i += 2, ++j) {
3716 int BitI = Mask[i];
3717 int BitI1 = Mask[i+1];
3718
3719 if (!isUndefOrEqual(BitI, j))
3720 return false;
3721 if (!isUndefOrEqual(BitI1, j))
3722 return false;
3723 }
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003724 }
David Greenea20244d2011-03-02 17:23:43 +00003725
Rafael Espindola15684b22009-04-24 12:40:33 +00003726 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00003727}
3728
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003729/// isUNPCKH_v_undef_Mask - Special case of isUNPCKHMask for canonical form
3730/// of vector_shuffle v, v, <2, 6, 3, 7>, i.e. vector_shuffle v, undef,
3731/// <2, 2, 3, 3>
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00003732static bool isUNPCKH_v_undef_Mask(ArrayRef<int> Mask, EVT VT, bool HasInt256) {
Craig Topper94438ba2011-12-16 08:06:31 +00003733 unsigned NumElts = VT.getVectorNumElements();
3734
3735 assert((VT.is128BitVector() || VT.is256BitVector()) &&
3736 "Unsupported vector type for unpckh");
3737
3738 if (VT.getSizeInBits() == 256 && NumElts != 4 && NumElts != 8 &&
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00003739 (!HasInt256 || (NumElts != 16 && NumElts != 32)))
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003740 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003741
Craig Topper94438ba2011-12-16 08:06:31 +00003742 // Handle 128 and 256-bit vector lengths. AVX defines UNPCK* to operate
3743 // independently on 128-bit lanes.
3744 unsigned NumLanes = VT.getSizeInBits()/128;
3745 unsigned NumLaneElts = NumElts/NumLanes;
3746
3747 for (unsigned l = 0; l != NumLanes; ++l) {
3748 for (unsigned i = l*NumLaneElts, j = (l*NumLaneElts)+NumLaneElts/2;
3749 i != (l+1)*NumLaneElts; i += 2, ++j) {
3750 int BitI = Mask[i];
3751 int BitI1 = Mask[i+1];
3752 if (!isUndefOrEqual(BitI, j))
3753 return false;
3754 if (!isUndefOrEqual(BitI1, j))
3755 return false;
3756 }
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003757 }
Rafael Espindola15684b22009-04-24 12:40:33 +00003758 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00003759}
3760
Evan Cheng017dcc62006-04-21 01:05:10 +00003761/// isMOVLMask - Return true if the specified VECTOR_SHUFFLE operand
3762/// specifies a shuffle of elements that is suitable for input to MOVSS,
3763/// MOVSD, and MOVD, i.e. setting the lowest element.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003764static bool isMOVLMask(ArrayRef<int> Mask, EVT VT) {
Eli Friedman10415532009-06-06 06:05:10 +00003765 if (VT.getVectorElementType().getSizeInBits() < 32)
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003766 return false;
Craig Topper7a9a28b2012-08-12 02:23:29 +00003767 if (!VT.is128BitVector())
Elena Demikhovsky021c0a22011-12-28 08:14:01 +00003768 return false;
Eli Friedman10415532009-06-06 06:05:10 +00003769
Craig Topperc612d792012-01-02 09:17:37 +00003770 unsigned NumElts = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00003771
Nate Begeman9008ca62009-04-27 18:41:29 +00003772 if (!isUndefOrEqual(Mask[0], NumElts))
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003773 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003774
Craig Topperc612d792012-01-02 09:17:37 +00003775 for (unsigned i = 1; i != NumElts; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003776 if (!isUndefOrEqual(Mask[i], i))
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003777 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003778
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003779 return true;
3780}
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003781
Craig Topper70b883b2011-11-28 10:14:51 +00003782/// isVPERM2X128Mask - Match 256-bit shuffles where the elements are considered
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003783/// as permutations between 128-bit chunks or halves. As an example: this
3784/// shuffle bellow:
3785/// vector_shuffle <4, 5, 6, 7, 12, 13, 14, 15>
3786/// The first half comes from the second half of V1 and the second half from the
3787/// the second half of V2.
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00003788static bool isVPERM2X128Mask(ArrayRef<int> Mask, EVT VT, bool HasFp256) {
3789 if (!HasFp256 || !VT.is256BitVector())
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003790 return false;
3791
3792 // The shuffle result is divided into half A and half B. In total the two
3793 // sources have 4 halves, namely: C, D, E, F. The final values of A and
3794 // B must come from C, D, E or F.
Craig Topperc612d792012-01-02 09:17:37 +00003795 unsigned HalfSize = VT.getVectorNumElements()/2;
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003796 bool MatchA = false, MatchB = false;
3797
3798 // Check if A comes from one of C, D, E, F.
Craig Topperc612d792012-01-02 09:17:37 +00003799 for (unsigned Half = 0; Half != 4; ++Half) {
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003800 if (isSequentialOrUndefInRange(Mask, 0, HalfSize, Half*HalfSize)) {
3801 MatchA = true;
3802 break;
3803 }
3804 }
3805
3806 // Check if B comes from one of C, D, E, F.
Craig Topperc612d792012-01-02 09:17:37 +00003807 for (unsigned Half = 0; Half != 4; ++Half) {
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003808 if (isSequentialOrUndefInRange(Mask, HalfSize, HalfSize, Half*HalfSize)) {
3809 MatchB = true;
3810 break;
3811 }
3812 }
3813
3814 return MatchA && MatchB;
3815}
3816
Craig Topper70b883b2011-11-28 10:14:51 +00003817/// getShuffleVPERM2X128Immediate - Return the appropriate immediate to shuffle
3818/// the specified VECTOR_MASK mask with VPERM2F128/VPERM2I128 instructions.
Craig Topperd93e4c32011-12-11 19:12:35 +00003819static unsigned getShuffleVPERM2X128Immediate(ShuffleVectorSDNode *SVOp) {
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003820 EVT VT = SVOp->getValueType(0);
3821
Craig Topperc612d792012-01-02 09:17:37 +00003822 unsigned HalfSize = VT.getVectorNumElements()/2;
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003823
Craig Topperc612d792012-01-02 09:17:37 +00003824 unsigned FstHalf = 0, SndHalf = 0;
3825 for (unsigned i = 0; i < HalfSize; ++i) {
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003826 if (SVOp->getMaskElt(i) > 0) {
3827 FstHalf = SVOp->getMaskElt(i)/HalfSize;
3828 break;
3829 }
3830 }
Craig Topperc612d792012-01-02 09:17:37 +00003831 for (unsigned i = HalfSize; i < HalfSize*2; ++i) {
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003832 if (SVOp->getMaskElt(i) > 0) {
3833 SndHalf = SVOp->getMaskElt(i)/HalfSize;
3834 break;
3835 }
3836 }
3837
3838 return (FstHalf | (SndHalf << 4));
3839}
3840
Craig Topper70b883b2011-11-28 10:14:51 +00003841/// isVPERMILPMask - Return true if the specified VECTOR_SHUFFLE operand
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00003842/// specifies a shuffle of elements that is suitable for input to VPERMILPD*.
3843/// Note that VPERMIL mask matching is different depending whether theunderlying
3844/// type is 32 or 64. In the VPERMILPS the high half of the mask should point
3845/// to the same elements of the low, but to the higher half of the source.
3846/// In VPERMILPD the two lanes could be shuffled independently of each other
Craig Topperdbd98a42012-02-07 06:28:42 +00003847/// with the same restriction that lanes can't be crossed. Also handles PSHUFDY.
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00003848static bool isVPERMILPMask(ArrayRef<int> Mask, EVT VT, bool HasFp256) {
3849 if (!HasFp256)
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00003850 return false;
3851
Craig Topperc612d792012-01-02 09:17:37 +00003852 unsigned NumElts = VT.getVectorNumElements();
Craig Topper70b883b2011-11-28 10:14:51 +00003853 // Only match 256-bit with 32/64-bit types
3854 if (VT.getSizeInBits() != 256 || (NumElts != 4 && NumElts != 8))
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00003855 return false;
3856
Craig Topperc612d792012-01-02 09:17:37 +00003857 unsigned NumLanes = VT.getSizeInBits()/128;
3858 unsigned LaneSize = NumElts/NumLanes;
Craig Topper1a7700a2012-01-19 08:19:12 +00003859 for (unsigned l = 0; l != NumElts; l += LaneSize) {
Craig Topperc612d792012-01-02 09:17:37 +00003860 for (unsigned i = 0; i != LaneSize; ++i) {
Craig Topper1a7700a2012-01-19 08:19:12 +00003861 if (!isUndefOrInRange(Mask[i+l], l, l+LaneSize))
Craig Topper70b883b2011-11-28 10:14:51 +00003862 return false;
Craig Topper1a7700a2012-01-19 08:19:12 +00003863 if (NumElts != 8 || l == 0)
Craig Topper70b883b2011-11-28 10:14:51 +00003864 continue;
3865 // VPERMILPS handling
3866 if (Mask[i] < 0)
3867 continue;
Craig Topper1a7700a2012-01-19 08:19:12 +00003868 if (!isUndefOrEqual(Mask[i+l], Mask[i]+l))
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00003869 return false;
3870 }
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00003871 }
3872
3873 return true;
3874}
3875
Craig Topper5aaffa82012-02-19 02:53:47 +00003876/// isCommutedMOVLMask - Returns true if the shuffle mask is except the reverse
Evan Cheng017dcc62006-04-21 01:05:10 +00003877/// of what x86 movss want. X86 movs requires the lowest element to be lowest
Evan Cheng39623da2006-04-20 08:58:49 +00003878/// element of vector 2 and the other elements to come from vector 1 in order.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003879static bool isCommutedMOVLMask(ArrayRef<int> Mask, EVT VT,
Nate Begeman9008ca62009-04-27 18:41:29 +00003880 bool V2IsSplat = false, bool V2IsUndef = false) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00003881 if (!VT.is128BitVector())
Craig Topper97327dc2012-03-18 22:50:10 +00003882 return false;
Craig Topper7a9a28b2012-08-12 02:23:29 +00003883
3884 unsigned NumOps = VT.getVectorNumElements();
Chris Lattner5a88b832007-02-25 07:10:00 +00003885 if (NumOps != 2 && NumOps != 4 && NumOps != 8 && NumOps != 16)
Evan Cheng39623da2006-04-20 08:58:49 +00003886 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003887
Nate Begeman9008ca62009-04-27 18:41:29 +00003888 if (!isUndefOrEqual(Mask[0], 0))
Evan Cheng39623da2006-04-20 08:58:49 +00003889 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003890
Craig Topperc612d792012-01-02 09:17:37 +00003891 for (unsigned i = 1; i != NumOps; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003892 if (!(isUndefOrEqual(Mask[i], i+NumOps) ||
3893 (V2IsUndef && isUndefOrInRange(Mask[i], NumOps, NumOps*2)) ||
3894 (V2IsSplat && isUndefOrEqual(Mask[i], NumOps))))
Evan Cheng8cf723d2006-09-08 01:50:06 +00003895 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003896
Evan Cheng39623da2006-04-20 08:58:49 +00003897 return true;
3898}
3899
Evan Chengd9539472006-04-14 21:59:03 +00003900/// isMOVSHDUPMask - Return true if the specified VECTOR_SHUFFLE operand
3901/// specifies a shuffle of elements that is suitable for input to MOVSHDUP.
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003902/// Masks to match: <1, 1, 3, 3> or <1, 1, 3, 3, 5, 5, 7, 7>
Craig Topperdd637ae2012-02-19 05:41:45 +00003903static bool isMOVSHDUPMask(ArrayRef<int> Mask, EVT VT,
Craig Topper5aaffa82012-02-19 02:53:47 +00003904 const X86Subtarget *Subtarget) {
Craig Topperd0a31172012-01-10 06:37:29 +00003905 if (!Subtarget->hasSSE3())
Evan Chengd9539472006-04-14 21:59:03 +00003906 return false;
3907
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003908 unsigned NumElems = VT.getVectorNumElements();
3909
3910 if ((VT.getSizeInBits() == 128 && NumElems != 4) ||
3911 (VT.getSizeInBits() == 256 && NumElems != 8))
3912 return false;
3913
3914 // "i+1" is the value the indexed mask element must have
Craig Topperdd637ae2012-02-19 05:41:45 +00003915 for (unsigned i = 0; i != NumElems; i += 2)
3916 if (!isUndefOrEqual(Mask[i], i+1) ||
3917 !isUndefOrEqual(Mask[i+1], i+1))
Nate Begeman9008ca62009-04-27 18:41:29 +00003918 return false;
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003919
3920 return true;
Evan Chengd9539472006-04-14 21:59:03 +00003921}
3922
3923/// isMOVSLDUPMask - Return true if the specified VECTOR_SHUFFLE operand
3924/// specifies a shuffle of elements that is suitable for input to MOVSLDUP.
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003925/// Masks to match: <0, 0, 2, 2> or <0, 0, 2, 2, 4, 4, 6, 6>
Craig Topperdd637ae2012-02-19 05:41:45 +00003926static bool isMOVSLDUPMask(ArrayRef<int> Mask, EVT VT,
Craig Topper5aaffa82012-02-19 02:53:47 +00003927 const X86Subtarget *Subtarget) {
Craig Topperd0a31172012-01-10 06:37:29 +00003928 if (!Subtarget->hasSSE3())
Evan Chengd9539472006-04-14 21:59:03 +00003929 return false;
3930
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003931 unsigned NumElems = VT.getVectorNumElements();
3932
3933 if ((VT.getSizeInBits() == 128 && NumElems != 4) ||
3934 (VT.getSizeInBits() == 256 && NumElems != 8))
3935 return false;
3936
3937 // "i" is the value the indexed mask element must have
Craig Topperc612d792012-01-02 09:17:37 +00003938 for (unsigned i = 0; i != NumElems; i += 2)
Craig Topperdd637ae2012-02-19 05:41:45 +00003939 if (!isUndefOrEqual(Mask[i], i) ||
3940 !isUndefOrEqual(Mask[i+1], i))
Nate Begeman9008ca62009-04-27 18:41:29 +00003941 return false;
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003942
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003943 return true;
Evan Chengd9539472006-04-14 21:59:03 +00003944}
3945
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00003946/// isMOVDDUPYMask - Return true if the specified VECTOR_SHUFFLE operand
3947/// specifies a shuffle of elements that is suitable for input to 256-bit
3948/// version of MOVDDUP.
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00003949static bool isMOVDDUPYMask(ArrayRef<int> Mask, EVT VT, bool HasFp256) {
3950 if (!HasFp256 || !VT.is256BitVector())
Craig Topper7a9a28b2012-08-12 02:23:29 +00003951 return false;
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00003952
Craig Topper7a9a28b2012-08-12 02:23:29 +00003953 unsigned NumElts = VT.getVectorNumElements();
3954 if (NumElts != 4)
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00003955 return false;
3956
Craig Topperc612d792012-01-02 09:17:37 +00003957 for (unsigned i = 0; i != NumElts/2; ++i)
Craig Topperbeabc6c2011-12-05 06:56:46 +00003958 if (!isUndefOrEqual(Mask[i], 0))
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00003959 return false;
Craig Topperc612d792012-01-02 09:17:37 +00003960 for (unsigned i = NumElts/2; i != NumElts; ++i)
Craig Topperbeabc6c2011-12-05 06:56:46 +00003961 if (!isUndefOrEqual(Mask[i], NumElts/2))
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00003962 return false;
3963 return true;
3964}
3965
Evan Cheng0b457f02008-09-25 20:50:48 +00003966/// isMOVDDUPMask - Return true if the specified VECTOR_SHUFFLE operand
Bruno Cardoso Lopes06ef9232011-08-25 21:40:34 +00003967/// specifies a shuffle of elements that is suitable for input to 128-bit
3968/// version of MOVDDUP.
Craig Topperdd637ae2012-02-19 05:41:45 +00003969static bool isMOVDDUPMask(ArrayRef<int> Mask, EVT VT) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00003970 if (!VT.is128BitVector())
Bruno Cardoso Lopes06ef9232011-08-25 21:40:34 +00003971 return false;
3972
Craig Topperc612d792012-01-02 09:17:37 +00003973 unsigned e = VT.getVectorNumElements() / 2;
3974 for (unsigned i = 0; i != e; ++i)
Craig Topperdd637ae2012-02-19 05:41:45 +00003975 if (!isUndefOrEqual(Mask[i], i))
Evan Cheng0b457f02008-09-25 20:50:48 +00003976 return false;
Craig Topperc612d792012-01-02 09:17:37 +00003977 for (unsigned i = 0; i != e; ++i)
Craig Topperdd637ae2012-02-19 05:41:45 +00003978 if (!isUndefOrEqual(Mask[e+i], i))
Evan Cheng0b457f02008-09-25 20:50:48 +00003979 return false;
3980 return true;
3981}
3982
David Greenec38a03e2011-02-03 15:50:00 +00003983/// isVEXTRACTF128Index - Return true if the specified
3984/// EXTRACT_SUBVECTOR operand specifies a vector extract that is
3985/// suitable for input to VEXTRACTF128.
3986bool X86::isVEXTRACTF128Index(SDNode *N) {
3987 if (!isa<ConstantSDNode>(N->getOperand(1).getNode()))
3988 return false;
3989
3990 // The index should be aligned on a 128-bit boundary.
3991 uint64_t Index =
3992 cast<ConstantSDNode>(N->getOperand(1).getNode())->getZExtValue();
3993
3994 unsigned VL = N->getValueType(0).getVectorNumElements();
3995 unsigned VBits = N->getValueType(0).getSizeInBits();
3996 unsigned ElSize = VBits / VL;
3997 bool Result = (Index * ElSize) % 128 == 0;
3998
3999 return Result;
4000}
4001
David Greeneccacdc12011-02-04 16:08:29 +00004002/// isVINSERTF128Index - Return true if the specified INSERT_SUBVECTOR
4003/// operand specifies a subvector insert that is suitable for input to
4004/// VINSERTF128.
4005bool X86::isVINSERTF128Index(SDNode *N) {
4006 if (!isa<ConstantSDNode>(N->getOperand(2).getNode()))
4007 return false;
4008
4009 // The index should be aligned on a 128-bit boundary.
4010 uint64_t Index =
4011 cast<ConstantSDNode>(N->getOperand(2).getNode())->getZExtValue();
4012
4013 unsigned VL = N->getValueType(0).getVectorNumElements();
4014 unsigned VBits = N->getValueType(0).getSizeInBits();
4015 unsigned ElSize = VBits / VL;
4016 bool Result = (Index * ElSize) % 128 == 0;
4017
4018 return Result;
4019}
4020
Evan Cheng63d33002006-03-22 08:01:21 +00004021/// getShuffleSHUFImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00004022/// the specified VECTOR_SHUFFLE mask with PSHUF* and SHUFP* instructions.
Craig Topper1a7700a2012-01-19 08:19:12 +00004023/// Handles 128-bit and 256-bit.
Craig Topper5aaffa82012-02-19 02:53:47 +00004024static unsigned getShuffleSHUFImmediate(ShuffleVectorSDNode *N) {
Craig Topper1a7700a2012-01-19 08:19:12 +00004025 EVT VT = N->getValueType(0);
Nate Begeman9008ca62009-04-27 18:41:29 +00004026
Craig Topper1a7700a2012-01-19 08:19:12 +00004027 assert((VT.is128BitVector() || VT.is256BitVector()) &&
4028 "Unsupported vector type for PSHUF/SHUFP");
4029
4030 // Handle 128 and 256-bit vector lengths. AVX defines PSHUF/SHUFP to operate
4031 // independently on 128-bit lanes.
4032 unsigned NumElts = VT.getVectorNumElements();
4033 unsigned NumLanes = VT.getSizeInBits()/128;
4034 unsigned NumLaneElts = NumElts/NumLanes;
4035
4036 assert((NumLaneElts == 2 || NumLaneElts == 4) &&
4037 "Only supports 2 or 4 elements per lane");
4038
4039 unsigned Shift = (NumLaneElts == 4) ? 1 : 0;
Evan Chengb9df0ca2006-03-22 02:53:00 +00004040 unsigned Mask = 0;
Craig Topper1a7700a2012-01-19 08:19:12 +00004041 for (unsigned i = 0; i != NumElts; ++i) {
4042 int Elt = N->getMaskElt(i);
4043 if (Elt < 0) continue;
Craig Topper6b28d352012-05-03 07:12:59 +00004044 Elt &= NumLaneElts - 1;
4045 unsigned ShAmt = (i << Shift) % 8;
Craig Topper1a7700a2012-01-19 08:19:12 +00004046 Mask |= Elt << ShAmt;
Evan Cheng36b27f32006-03-28 23:41:33 +00004047 }
Craig Topper1a7700a2012-01-19 08:19:12 +00004048
Evan Cheng63d33002006-03-22 08:01:21 +00004049 return Mask;
4050}
4051
Evan Cheng506d3df2006-03-29 23:07:14 +00004052/// getShufflePSHUFHWImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00004053/// the specified VECTOR_SHUFFLE mask with the PSHUFHW instruction.
Craig Topperdd637ae2012-02-19 05:41:45 +00004054static unsigned getShufflePSHUFHWImmediate(ShuffleVectorSDNode *N) {
Craig Topper6b28d352012-05-03 07:12:59 +00004055 EVT VT = N->getValueType(0);
4056
4057 assert((VT == MVT::v8i16 || VT == MVT::v16i16) &&
4058 "Unsupported vector type for PSHUFHW");
4059
4060 unsigned NumElts = VT.getVectorNumElements();
4061
Evan Cheng506d3df2006-03-29 23:07:14 +00004062 unsigned Mask = 0;
Craig Topper6b28d352012-05-03 07:12:59 +00004063 for (unsigned l = 0; l != NumElts; l += 8) {
4064 // 8 nodes per lane, but we only care about the last 4.
4065 for (unsigned i = 0; i < 4; ++i) {
4066 int Elt = N->getMaskElt(l+i+4);
4067 if (Elt < 0) continue;
4068 Elt &= 0x3; // only 2-bits.
4069 Mask |= Elt << (i * 2);
4070 }
Evan Cheng506d3df2006-03-29 23:07:14 +00004071 }
Craig Topper6b28d352012-05-03 07:12:59 +00004072
Evan Cheng506d3df2006-03-29 23:07:14 +00004073 return Mask;
4074}
4075
4076/// getShufflePSHUFLWImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00004077/// the specified VECTOR_SHUFFLE mask with the PSHUFLW instruction.
Craig Topperdd637ae2012-02-19 05:41:45 +00004078static unsigned getShufflePSHUFLWImmediate(ShuffleVectorSDNode *N) {
Craig Topper6b28d352012-05-03 07:12:59 +00004079 EVT VT = N->getValueType(0);
4080
4081 assert((VT == MVT::v8i16 || VT == MVT::v16i16) &&
4082 "Unsupported vector type for PSHUFHW");
4083
4084 unsigned NumElts = VT.getVectorNumElements();
4085
Evan Cheng506d3df2006-03-29 23:07:14 +00004086 unsigned Mask = 0;
Craig Topper6b28d352012-05-03 07:12:59 +00004087 for (unsigned l = 0; l != NumElts; l += 8) {
4088 // 8 nodes per lane, but we only care about the first 4.
4089 for (unsigned i = 0; i < 4; ++i) {
4090 int Elt = N->getMaskElt(l+i);
4091 if (Elt < 0) continue;
4092 Elt &= 0x3; // only 2-bits
4093 Mask |= Elt << (i * 2);
4094 }
Evan Cheng506d3df2006-03-29 23:07:14 +00004095 }
Craig Topper6b28d352012-05-03 07:12:59 +00004096
Evan Cheng506d3df2006-03-29 23:07:14 +00004097 return Mask;
4098}
4099
Nate Begemana09008b2009-10-19 02:17:23 +00004100/// getShufflePALIGNRImmediate - Return the appropriate immediate to shuffle
4101/// the specified VECTOR_SHUFFLE mask with the PALIGNR instruction.
Craig Topperd93e4c32011-12-11 19:12:35 +00004102static unsigned getShufflePALIGNRImmediate(ShuffleVectorSDNode *SVOp) {
4103 EVT VT = SVOp->getValueType(0);
4104 unsigned EltSize = VT.getVectorElementType().getSizeInBits() >> 3;
Nate Begemana09008b2009-10-19 02:17:23 +00004105
Craig Topper0e2037b2012-01-20 05:53:00 +00004106 unsigned NumElts = VT.getVectorNumElements();
4107 unsigned NumLanes = VT.getSizeInBits()/128;
4108 unsigned NumLaneElts = NumElts/NumLanes;
4109
4110 int Val = 0;
4111 unsigned i;
4112 for (i = 0; i != NumElts; ++i) {
Nate Begemana09008b2009-10-19 02:17:23 +00004113 Val = SVOp->getMaskElt(i);
4114 if (Val >= 0)
4115 break;
4116 }
Craig Topper0e2037b2012-01-20 05:53:00 +00004117 if (Val >= (int)NumElts)
4118 Val -= NumElts - NumLaneElts;
4119
Eli Friedman63f8dde2011-07-25 21:36:45 +00004120 assert(Val - i > 0 && "PALIGNR imm should be positive");
Nate Begemana09008b2009-10-19 02:17:23 +00004121 return (Val - i) * EltSize;
4122}
4123
David Greenec38a03e2011-02-03 15:50:00 +00004124/// getExtractVEXTRACTF128Immediate - Return the appropriate immediate
4125/// to extract the specified EXTRACT_SUBVECTOR index with VEXTRACTF128
4126/// instructions.
4127unsigned X86::getExtractVEXTRACTF128Immediate(SDNode *N) {
4128 if (!isa<ConstantSDNode>(N->getOperand(1).getNode()))
4129 llvm_unreachable("Illegal extract subvector for VEXTRACTF128");
4130
4131 uint64_t Index =
4132 cast<ConstantSDNode>(N->getOperand(1).getNode())->getZExtValue();
4133
4134 EVT VecVT = N->getOperand(0).getValueType();
4135 EVT ElVT = VecVT.getVectorElementType();
4136
4137 unsigned NumElemsPerChunk = 128 / ElVT.getSizeInBits();
David Greenec38a03e2011-02-03 15:50:00 +00004138 return Index / NumElemsPerChunk;
4139}
4140
David Greeneccacdc12011-02-04 16:08:29 +00004141/// getInsertVINSERTF128Immediate - Return the appropriate immediate
4142/// to insert at the specified INSERT_SUBVECTOR index with VINSERTF128
4143/// instructions.
4144unsigned X86::getInsertVINSERTF128Immediate(SDNode *N) {
4145 if (!isa<ConstantSDNode>(N->getOperand(2).getNode()))
4146 llvm_unreachable("Illegal insert subvector for VINSERTF128");
4147
4148 uint64_t Index =
NAKAMURA Takumi27635382011-02-05 15:10:54 +00004149 cast<ConstantSDNode>(N->getOperand(2).getNode())->getZExtValue();
David Greeneccacdc12011-02-04 16:08:29 +00004150
4151 EVT VecVT = N->getValueType(0);
4152 EVT ElVT = VecVT.getVectorElementType();
4153
4154 unsigned NumElemsPerChunk = 128 / ElVT.getSizeInBits();
David Greeneccacdc12011-02-04 16:08:29 +00004155 return Index / NumElemsPerChunk;
4156}
4157
Elena Demikhovsky73c504a2012-04-15 11:18:59 +00004158/// getShuffleCLImmediate - Return the appropriate immediate to shuffle
4159/// the specified VECTOR_SHUFFLE mask with VPERMQ and VPERMPD instructions.
4160/// Handles 256-bit.
4161static unsigned getShuffleCLImmediate(ShuffleVectorSDNode *N) {
4162 EVT VT = N->getValueType(0);
4163
Elena Demikhovsky73c504a2012-04-15 11:18:59 +00004164 unsigned NumElts = VT.getVectorNumElements();
4165
Craig Topper095c5282012-04-15 23:48:57 +00004166 assert((VT.is256BitVector() && NumElts == 4) &&
4167 "Unsupported vector type for VPERMQ/VPERMPD");
4168
Elena Demikhovsky73c504a2012-04-15 11:18:59 +00004169 unsigned Mask = 0;
4170 for (unsigned i = 0; i != NumElts; ++i) {
4171 int Elt = N->getMaskElt(i);
Craig Topper095c5282012-04-15 23:48:57 +00004172 if (Elt < 0)
Elena Demikhovsky73c504a2012-04-15 11:18:59 +00004173 continue;
4174 Mask |= Elt << (i*2);
4175 }
4176
4177 return Mask;
4178}
Evan Cheng37b73872009-07-30 08:33:02 +00004179/// isZeroNode - Returns true if Elt is a constant zero or a floating point
4180/// constant +0.0.
4181bool X86::isZeroNode(SDValue Elt) {
4182 return ((isa<ConstantSDNode>(Elt) &&
Dan Gohmane368b462010-06-18 14:22:04 +00004183 cast<ConstantSDNode>(Elt)->isNullValue()) ||
Evan Cheng37b73872009-07-30 08:33:02 +00004184 (isa<ConstantFPSDNode>(Elt) &&
4185 cast<ConstantFPSDNode>(Elt)->getValueAPF().isPosZero()));
4186}
4187
Nate Begeman9008ca62009-04-27 18:41:29 +00004188/// CommuteVectorShuffle - Swap vector_shuffle operands as well as values in
4189/// their permute mask.
4190static SDValue CommuteVectorShuffle(ShuffleVectorSDNode *SVOp,
4191 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00004192 EVT VT = SVOp->getValueType(0);
Nate Begeman5a5ca152009-04-29 05:20:52 +00004193 unsigned NumElems = VT.getVectorNumElements();
Nate Begeman9008ca62009-04-27 18:41:29 +00004194 SmallVector<int, 8> MaskVec;
Eric Christopherfd179292009-08-27 18:07:15 +00004195
Nate Begeman5a5ca152009-04-29 05:20:52 +00004196 for (unsigned i = 0; i != NumElems; ++i) {
Craig Topper8ae97ba2012-05-21 06:40:16 +00004197 int Idx = SVOp->getMaskElt(i);
4198 if (Idx >= 0) {
4199 if (Idx < (int)NumElems)
4200 Idx += NumElems;
4201 else
4202 Idx -= NumElems;
4203 }
4204 MaskVec.push_back(Idx);
Evan Cheng5ced1d82006-04-06 23:23:56 +00004205 }
Nate Begeman9008ca62009-04-27 18:41:29 +00004206 return DAG.getVectorShuffle(VT, SVOp->getDebugLoc(), SVOp->getOperand(1),
4207 SVOp->getOperand(0), &MaskVec[0]);
Evan Cheng5ced1d82006-04-06 23:23:56 +00004208}
4209
Evan Cheng533a0aa2006-04-19 20:35:22 +00004210/// ShouldXformToMOVHLPS - Return true if the node should be transformed to
4211/// match movhlps. The lower half elements should come from upper half of
4212/// V1 (and in order), and the upper half elements should come from the upper
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00004213/// half of V2 (and in order).
Craig Topperdd637ae2012-02-19 05:41:45 +00004214static bool ShouldXformToMOVHLPS(ArrayRef<int> Mask, EVT VT) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00004215 if (!VT.is128BitVector())
Bruno Cardoso Lopes59353b42011-08-11 18:59:13 +00004216 return false;
4217 if (VT.getVectorNumElements() != 4)
Evan Cheng533a0aa2006-04-19 20:35:22 +00004218 return false;
4219 for (unsigned i = 0, e = 2; i != e; ++i)
Craig Topperdd637ae2012-02-19 05:41:45 +00004220 if (!isUndefOrEqual(Mask[i], i+2))
Evan Cheng533a0aa2006-04-19 20:35:22 +00004221 return false;
4222 for (unsigned i = 2; i != 4; ++i)
Craig Topperdd637ae2012-02-19 05:41:45 +00004223 if (!isUndefOrEqual(Mask[i], i+4))
Evan Cheng533a0aa2006-04-19 20:35:22 +00004224 return false;
4225 return true;
4226}
4227
Evan Cheng5ced1d82006-04-06 23:23:56 +00004228/// isScalarLoadToVector - Returns true if the node is a scalar load that
Evan Cheng7e2ff772008-05-08 00:57:18 +00004229/// is promoted to a vector. It also returns the LoadSDNode by reference if
4230/// required.
4231static bool isScalarLoadToVector(SDNode *N, LoadSDNode **LD = NULL) {
Evan Cheng0b457f02008-09-25 20:50:48 +00004232 if (N->getOpcode() != ISD::SCALAR_TO_VECTOR)
4233 return false;
4234 N = N->getOperand(0).getNode();
4235 if (!ISD::isNON_EXTLoad(N))
4236 return false;
4237 if (LD)
4238 *LD = cast<LoadSDNode>(N);
4239 return true;
Evan Cheng5ced1d82006-04-06 23:23:56 +00004240}
4241
Dan Gohman65fd6562011-11-03 21:49:52 +00004242// Test whether the given value is a vector value which will be legalized
4243// into a load.
4244static bool WillBeConstantPoolLoad(SDNode *N) {
4245 if (N->getOpcode() != ISD::BUILD_VECTOR)
4246 return false;
4247
4248 // Check for any non-constant elements.
4249 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
4250 switch (N->getOperand(i).getNode()->getOpcode()) {
4251 case ISD::UNDEF:
4252 case ISD::ConstantFP:
4253 case ISD::Constant:
4254 break;
4255 default:
4256 return false;
4257 }
4258
4259 // Vectors of all-zeros and all-ones are materialized with special
4260 // instructions rather than being loaded.
4261 return !ISD::isBuildVectorAllZeros(N) &&
4262 !ISD::isBuildVectorAllOnes(N);
4263}
4264
Evan Cheng533a0aa2006-04-19 20:35:22 +00004265/// ShouldXformToMOVLP{S|D} - Return true if the node should be transformed to
4266/// match movlp{s|d}. The lower half elements should come from lower half of
4267/// V1 (and in order), and the upper half elements should come from the upper
4268/// half of V2 (and in order). And since V1 will become the source of the
4269/// MOVLP, it must be either a vector load or a scalar load to vector.
Nate Begeman9008ca62009-04-27 18:41:29 +00004270static bool ShouldXformToMOVLP(SDNode *V1, SDNode *V2,
Craig Topperdd637ae2012-02-19 05:41:45 +00004271 ArrayRef<int> Mask, EVT VT) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00004272 if (!VT.is128BitVector())
Bruno Cardoso Lopes59353b42011-08-11 18:59:13 +00004273 return false;
4274
Evan Cheng466685d2006-10-09 20:57:25 +00004275 if (!ISD::isNON_EXTLoad(V1) && !isScalarLoadToVector(V1))
Evan Cheng533a0aa2006-04-19 20:35:22 +00004276 return false;
Evan Cheng23425f52006-10-09 21:39:25 +00004277 // Is V2 is a vector load, don't do this transformation. We will try to use
4278 // load folding shufps op.
Dan Gohman65fd6562011-11-03 21:49:52 +00004279 if (ISD::isNON_EXTLoad(V2) || WillBeConstantPoolLoad(V2))
Evan Cheng23425f52006-10-09 21:39:25 +00004280 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00004281
Bruno Cardoso Lopes59353b42011-08-11 18:59:13 +00004282 unsigned NumElems = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00004283
Evan Cheng533a0aa2006-04-19 20:35:22 +00004284 if (NumElems != 2 && NumElems != 4)
4285 return false;
Nate Begeman5a5ca152009-04-29 05:20:52 +00004286 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
Craig Topperdd637ae2012-02-19 05:41:45 +00004287 if (!isUndefOrEqual(Mask[i], i))
Evan Cheng533a0aa2006-04-19 20:35:22 +00004288 return false;
Chad Rosier238ae312012-04-30 17:47:15 +00004289 for (unsigned i = NumElems/2, e = NumElems; i != e; ++i)
Craig Topperdd637ae2012-02-19 05:41:45 +00004290 if (!isUndefOrEqual(Mask[i], i+NumElems))
Evan Cheng533a0aa2006-04-19 20:35:22 +00004291 return false;
4292 return true;
Evan Cheng5ced1d82006-04-06 23:23:56 +00004293}
4294
Evan Cheng39623da2006-04-20 08:58:49 +00004295/// isSplatVector - Returns true if N is a BUILD_VECTOR node whose elements are
4296/// all the same.
4297static bool isSplatVector(SDNode *N) {
4298 if (N->getOpcode() != ISD::BUILD_VECTOR)
4299 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00004300
Dan Gohman475871a2008-07-27 21:46:04 +00004301 SDValue SplatValue = N->getOperand(0);
Evan Cheng39623da2006-04-20 08:58:49 +00004302 for (unsigned i = 1, e = N->getNumOperands(); i != e; ++i)
4303 if (N->getOperand(i) != SplatValue)
Evan Cheng5ced1d82006-04-06 23:23:56 +00004304 return false;
4305 return true;
4306}
4307
Evan Cheng213d2cf2007-05-17 18:45:50 +00004308/// isZeroShuffle - Returns true if N is a VECTOR_SHUFFLE that can be resolved
Eric Christopherfd179292009-08-27 18:07:15 +00004309/// to an zero vector.
Nate Begeman5a5ca152009-04-29 05:20:52 +00004310/// FIXME: move to dag combiner / method on ShuffleVectorSDNode
Nate Begeman9008ca62009-04-27 18:41:29 +00004311static bool isZeroShuffle(ShuffleVectorSDNode *N) {
Dan Gohman475871a2008-07-27 21:46:04 +00004312 SDValue V1 = N->getOperand(0);
4313 SDValue V2 = N->getOperand(1);
Nate Begeman5a5ca152009-04-29 05:20:52 +00004314 unsigned NumElems = N->getValueType(0).getVectorNumElements();
4315 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004316 int Idx = N->getMaskElt(i);
Nate Begeman5a5ca152009-04-29 05:20:52 +00004317 if (Idx >= (int)NumElems) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004318 unsigned Opc = V2.getOpcode();
Rafael Espindola15684b22009-04-24 12:40:33 +00004319 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V2.getNode()))
4320 continue;
Evan Cheng37b73872009-07-30 08:33:02 +00004321 if (Opc != ISD::BUILD_VECTOR ||
4322 !X86::isZeroNode(V2.getOperand(Idx-NumElems)))
Nate Begeman9008ca62009-04-27 18:41:29 +00004323 return false;
4324 } else if (Idx >= 0) {
4325 unsigned Opc = V1.getOpcode();
4326 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V1.getNode()))
4327 continue;
Evan Cheng37b73872009-07-30 08:33:02 +00004328 if (Opc != ISD::BUILD_VECTOR ||
4329 !X86::isZeroNode(V1.getOperand(Idx)))
Chris Lattner8a594482007-11-25 00:24:49 +00004330 return false;
Evan Cheng213d2cf2007-05-17 18:45:50 +00004331 }
4332 }
4333 return true;
4334}
4335
4336/// getZeroVector - Returns a vector of specified type with all zero elements.
4337///
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00004338static SDValue getZeroVector(EVT VT, const X86Subtarget *Subtarget,
Craig Topper12216172012-01-13 08:12:35 +00004339 SelectionDAG &DAG, DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004340 assert(VT.isVector() && "Expected a vector type");
Craig Topper9d352402012-04-23 07:24:41 +00004341 unsigned Size = VT.getSizeInBits();
Scott Michelfdc40a02009-02-17 22:15:04 +00004342
Dale Johannesen0488fb62010-09-30 23:57:10 +00004343 // Always build SSE zero vectors as <4 x i32> bitcasted
Bruno Cardoso Lopes8c05a852010-08-12 02:06:36 +00004344 // to their dest type. This ensures they get CSE'd.
Dan Gohman475871a2008-07-27 21:46:04 +00004345 SDValue Vec;
Craig Topper9d352402012-04-23 07:24:41 +00004346 if (Size == 128) { // SSE
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00004347 if (Subtarget->hasSSE2()) { // SSE2
Bruno Cardoso Lopes8c05a852010-08-12 02:06:36 +00004348 SDValue Cst = DAG.getTargetConstant(0, MVT::i32);
4349 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
4350 } else { // SSE1
4351 SDValue Cst = DAG.getTargetConstantFP(+0.0, MVT::f32);
4352 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4f32, Cst, Cst, Cst, Cst);
4353 }
Craig Topper9d352402012-04-23 07:24:41 +00004354 } else if (Size == 256) { // AVX
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00004355 if (Subtarget->hasInt256()) { // AVX2
Craig Topper12216172012-01-13 08:12:35 +00004356 SDValue Cst = DAG.getTargetConstant(0, MVT::i32);
4357 SDValue Ops[] = { Cst, Cst, Cst, Cst, Cst, Cst, Cst, Cst };
4358 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v8i32, Ops, 8);
4359 } else {
4360 // 256-bit logic and arithmetic instructions in AVX are all
4361 // floating-point, no support for integer ops. Emit fp zeroed vectors.
4362 SDValue Cst = DAG.getTargetConstantFP(+0.0, MVT::f32);
4363 SDValue Ops[] = { Cst, Cst, Cst, Cst, Cst, Cst, Cst, Cst };
4364 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v8f32, Ops, 8);
4365 }
Craig Topper9d352402012-04-23 07:24:41 +00004366 } else
4367 llvm_unreachable("Unexpected vector type");
4368
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004369 return DAG.getNode(ISD::BITCAST, dl, VT, Vec);
Evan Cheng213d2cf2007-05-17 18:45:50 +00004370}
4371
Chris Lattner8a594482007-11-25 00:24:49 +00004372/// getOnesVector - Returns a vector of specified type with all bits set.
Craig Topper745a86b2011-11-19 22:34:59 +00004373/// Always build ones vectors as <4 x i32> or <8 x i32>. For 256-bit types with
4374/// no AVX2 supprt, use two <4 x i32> inserted in a <8 x i32> appropriately.
4375/// Then bitcast to their original type, ensuring they get CSE'd.
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00004376static SDValue getOnesVector(EVT VT, bool HasInt256, SelectionDAG &DAG,
Craig Topper745a86b2011-11-19 22:34:59 +00004377 DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004378 assert(VT.isVector() && "Expected a vector type");
Craig Topper9d352402012-04-23 07:24:41 +00004379 unsigned Size = VT.getSizeInBits();
Scott Michelfdc40a02009-02-17 22:15:04 +00004380
Owen Anderson825b72b2009-08-11 20:47:22 +00004381 SDValue Cst = DAG.getTargetConstant(~0U, MVT::i32);
Craig Topper745a86b2011-11-19 22:34:59 +00004382 SDValue Vec;
Craig Topper9d352402012-04-23 07:24:41 +00004383 if (Size == 256) {
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00004384 if (HasInt256) { // AVX2
Craig Topper745a86b2011-11-19 22:34:59 +00004385 SDValue Ops[] = { Cst, Cst, Cst, Cst, Cst, Cst, Cst, Cst };
4386 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v8i32, Ops, 8);
4387 } else { // AVX
4388 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
Craig Topper4c7972d2012-04-22 18:15:59 +00004389 Vec = Concat128BitVectors(Vec, Vec, MVT::v8i32, 8, DAG, dl);
Craig Topper745a86b2011-11-19 22:34:59 +00004390 }
Craig Topper9d352402012-04-23 07:24:41 +00004391 } else if (Size == 128) {
Craig Topper745a86b2011-11-19 22:34:59 +00004392 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
Craig Topper9d352402012-04-23 07:24:41 +00004393 } else
4394 llvm_unreachable("Unexpected vector type");
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +00004395
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004396 return DAG.getNode(ISD::BITCAST, dl, VT, Vec);
Chris Lattner8a594482007-11-25 00:24:49 +00004397}
4398
Evan Cheng39623da2006-04-20 08:58:49 +00004399/// NormalizeMask - V2 is a splat, modify the mask (if needed) so all elements
4400/// that point to V2 points to its first element.
Craig Topper39a9e482012-02-11 06:24:48 +00004401static void NormalizeMask(SmallVectorImpl<int> &Mask, unsigned NumElems) {
Nate Begeman5a5ca152009-04-29 05:20:52 +00004402 for (unsigned i = 0; i != NumElems; ++i) {
Craig Topper39a9e482012-02-11 06:24:48 +00004403 if (Mask[i] > (int)NumElems) {
4404 Mask[i] = NumElems;
Evan Cheng39623da2006-04-20 08:58:49 +00004405 }
Evan Cheng39623da2006-04-20 08:58:49 +00004406 }
Evan Cheng39623da2006-04-20 08:58:49 +00004407}
4408
Evan Cheng017dcc62006-04-21 01:05:10 +00004409/// getMOVLMask - Returns a vector_shuffle mask for an movs{s|d}, movd
4410/// operation of specified width.
Owen Andersone50ed302009-08-10 22:56:29 +00004411static SDValue getMOVL(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00004412 SDValue V2) {
4413 unsigned NumElems = VT.getVectorNumElements();
4414 SmallVector<int, 8> Mask;
4415 Mask.push_back(NumElems);
Evan Cheng39623da2006-04-20 08:58:49 +00004416 for (unsigned i = 1; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004417 Mask.push_back(i);
4418 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Evan Cheng39623da2006-04-20 08:58:49 +00004419}
4420
Nate Begeman9008ca62009-04-27 18:41:29 +00004421/// getUnpackl - Returns a vector_shuffle node for an unpackl operation.
Owen Andersone50ed302009-08-10 22:56:29 +00004422static SDValue getUnpackl(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00004423 SDValue V2) {
4424 unsigned NumElems = VT.getVectorNumElements();
4425 SmallVector<int, 8> Mask;
Evan Chengc575ca22006-04-17 20:43:08 +00004426 for (unsigned i = 0, e = NumElems/2; i != e; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004427 Mask.push_back(i);
4428 Mask.push_back(i + NumElems);
Evan Chengc575ca22006-04-17 20:43:08 +00004429 }
Nate Begeman9008ca62009-04-27 18:41:29 +00004430 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Evan Chengc575ca22006-04-17 20:43:08 +00004431}
4432
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004433/// getUnpackh - Returns a vector_shuffle node for an unpackh operation.
Owen Andersone50ed302009-08-10 22:56:29 +00004434static SDValue getUnpackh(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00004435 SDValue V2) {
4436 unsigned NumElems = VT.getVectorNumElements();
Nate Begeman9008ca62009-04-27 18:41:29 +00004437 SmallVector<int, 8> Mask;
Chad Rosier238ae312012-04-30 17:47:15 +00004438 for (unsigned i = 0, Half = NumElems/2; i != Half; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004439 Mask.push_back(i + Half);
4440 Mask.push_back(i + NumElems + Half);
Evan Cheng39623da2006-04-20 08:58:49 +00004441 }
Nate Begeman9008ca62009-04-27 18:41:29 +00004442 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Chris Lattner62098042008-03-09 01:05:04 +00004443}
4444
Bruno Cardoso Lopes5f1d8ab2011-08-11 02:49:44 +00004445// PromoteSplati8i16 - All i16 and i8 vector types can't be used directly by
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004446// a generic shuffle instruction because the target has no such instructions.
4447// Generate shuffles which repeat i16 and i8 several times until they can be
4448// represented by v4f32 and then be manipulated by target suported shuffles.
Bruno Cardoso Lopes5f1d8ab2011-08-11 02:49:44 +00004449static SDValue PromoteSplati8i16(SDValue V, SelectionDAG &DAG, int &EltNo) {
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004450 EVT VT = V.getValueType();
Nate Begeman9008ca62009-04-27 18:41:29 +00004451 int NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004452 DebugLoc dl = V.getDebugLoc();
Rafael Espindola15684b22009-04-24 12:40:33 +00004453
Nate Begeman9008ca62009-04-27 18:41:29 +00004454 while (NumElems > 4) {
4455 if (EltNo < NumElems/2) {
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004456 V = getUnpackl(DAG, dl, VT, V, V);
Nate Begeman9008ca62009-04-27 18:41:29 +00004457 } else {
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004458 V = getUnpackh(DAG, dl, VT, V, V);
Nate Begeman9008ca62009-04-27 18:41:29 +00004459 EltNo -= NumElems/2;
4460 }
4461 NumElems >>= 1;
4462 }
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004463 return V;
4464}
Eric Christopherfd179292009-08-27 18:07:15 +00004465
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004466/// getLegalSplat - Generate a legal splat with supported x86 shuffles
4467static SDValue getLegalSplat(SelectionDAG &DAG, SDValue V, int EltNo) {
4468 EVT VT = V.getValueType();
4469 DebugLoc dl = V.getDebugLoc();
Craig Topper9d352402012-04-23 07:24:41 +00004470 unsigned Size = VT.getSizeInBits();
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004471
Craig Topper9d352402012-04-23 07:24:41 +00004472 if (Size == 128) {
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004473 V = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, V);
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004474 int SplatMask[4] = { EltNo, EltNo, EltNo, EltNo };
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004475 V = DAG.getVectorShuffle(MVT::v4f32, dl, V, DAG.getUNDEF(MVT::v4f32),
4476 &SplatMask[0]);
Craig Topper9d352402012-04-23 07:24:41 +00004477 } else if (Size == 256) {
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004478 // To use VPERMILPS to splat scalars, the second half of indicies must
4479 // refer to the higher part, which is a duplication of the lower one,
4480 // because VPERMILPS can only handle in-lane permutations.
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004481 int SplatMask[8] = { EltNo, EltNo, EltNo, EltNo,
4482 EltNo+4, EltNo+4, EltNo+4, EltNo+4 };
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004483
4484 V = DAG.getNode(ISD::BITCAST, dl, MVT::v8f32, V);
4485 V = DAG.getVectorShuffle(MVT::v8f32, dl, V, DAG.getUNDEF(MVT::v8f32),
4486 &SplatMask[0]);
Craig Topper9d352402012-04-23 07:24:41 +00004487 } else
4488 llvm_unreachable("Vector size not supported");
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004489
4490 return DAG.getNode(ISD::BITCAST, dl, VT, V);
4491}
4492
Bruno Cardoso Lopes8a5b2622011-08-17 02:29:13 +00004493/// PromoteSplat - Splat is promoted to target supported vector shuffles.
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004494static SDValue PromoteSplat(ShuffleVectorSDNode *SV, SelectionDAG &DAG) {
4495 EVT SrcVT = SV->getValueType(0);
4496 SDValue V1 = SV->getOperand(0);
4497 DebugLoc dl = SV->getDebugLoc();
4498
4499 int EltNo = SV->getSplatIndex();
4500 int NumElems = SrcVT.getVectorNumElements();
4501 unsigned Size = SrcVT.getSizeInBits();
4502
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004503 assert(((Size == 128 && NumElems > 4) || Size == 256) &&
4504 "Unknown how to promote splat for type");
4505
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004506 // Extract the 128-bit part containing the splat element and update
4507 // the splat element index when it refers to the higher register.
4508 if (Size == 256) {
Craig Topper7d1e3dc2012-04-30 05:17:10 +00004509 V1 = Extract128BitVector(V1, EltNo, DAG, dl);
4510 if (EltNo >= NumElems/2)
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004511 EltNo -= NumElems/2;
4512 }
4513
Bruno Cardoso Lopes8a5b2622011-08-17 02:29:13 +00004514 // All i16 and i8 vector types can't be used directly by a generic shuffle
4515 // instruction because the target has no such instruction. Generate shuffles
4516 // which repeat i16 and i8 several times until they fit in i32, and then can
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004517 // be manipulated by target suported shuffles.
Bruno Cardoso Lopes5f1d8ab2011-08-11 02:49:44 +00004518 EVT EltVT = SrcVT.getVectorElementType();
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004519 if (EltVT == MVT::i8 || EltVT == MVT::i16)
Bruno Cardoso Lopes5f1d8ab2011-08-11 02:49:44 +00004520 V1 = PromoteSplati8i16(V1, DAG, EltNo);
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004521
4522 // Recreate the 256-bit vector and place the same 128-bit vector
4523 // into the low and high part. This is necessary because we want
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004524 // to use VPERM* to shuffle the vectors
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004525 if (Size == 256) {
Craig Topper4c7972d2012-04-22 18:15:59 +00004526 V1 = DAG.getNode(ISD::CONCAT_VECTORS, dl, SrcVT, V1, V1);
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004527 }
4528
4529 return getLegalSplat(DAG, V1, EltNo);
Evan Chengc575ca22006-04-17 20:43:08 +00004530}
4531
Evan Chengba05f722006-04-21 23:03:30 +00004532/// getShuffleVectorZeroOrUndef - Return a vector_shuffle of the specified
Chris Lattner8a594482007-11-25 00:24:49 +00004533/// vector of zero or undef vector. This produces a shuffle where the low
4534/// element of V2 is swizzled into the zero/undef vector, landing at element
4535/// 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 +00004536static SDValue getShuffleVectorZeroOrUndef(SDValue V2, unsigned Idx,
Craig Topper12216172012-01-13 08:12:35 +00004537 bool IsZero,
4538 const X86Subtarget *Subtarget,
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00004539 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00004540 EVT VT = V2.getValueType();
Craig Topper12216172012-01-13 08:12:35 +00004541 SDValue V1 = IsZero
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00004542 ? getZeroVector(VT, Subtarget, DAG, V2.getDebugLoc()) : DAG.getUNDEF(VT);
Nate Begeman9008ca62009-04-27 18:41:29 +00004543 unsigned NumElems = VT.getVectorNumElements();
4544 SmallVector<int, 16> MaskVec;
Chris Lattner8a594482007-11-25 00:24:49 +00004545 for (unsigned i = 0; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004546 // If this is the insertion idx, put the low elt of V2 here.
4547 MaskVec.push_back(i == Idx ? NumElems : i);
4548 return DAG.getVectorShuffle(VT, V2.getDebugLoc(), V1, V2, &MaskVec[0]);
Evan Cheng017dcc62006-04-21 01:05:10 +00004549}
4550
Craig Toppera1ffc682012-03-20 06:42:26 +00004551/// getTargetShuffleMask - Calculates the shuffle mask corresponding to the
4552/// target specific opcode. Returns true if the Mask could be calculated.
Craig Topper89f4e662012-03-20 07:17:59 +00004553/// Sets IsUnary to true if only uses one source.
Craig Topperd978c542012-05-06 19:46:21 +00004554static bool getTargetShuffleMask(SDNode *N, MVT VT,
Craig Topper89f4e662012-03-20 07:17:59 +00004555 SmallVectorImpl<int> &Mask, bool &IsUnary) {
Craig Toppera1ffc682012-03-20 06:42:26 +00004556 unsigned NumElems = VT.getVectorNumElements();
4557 SDValue ImmN;
4558
Craig Topper89f4e662012-03-20 07:17:59 +00004559 IsUnary = false;
Craig Toppera1ffc682012-03-20 06:42:26 +00004560 switch(N->getOpcode()) {
4561 case X86ISD::SHUFP:
4562 ImmN = N->getOperand(N->getNumOperands()-1);
4563 DecodeSHUFPMask(VT, cast<ConstantSDNode>(ImmN)->getZExtValue(), Mask);
4564 break;
4565 case X86ISD::UNPCKH:
4566 DecodeUNPCKHMask(VT, Mask);
4567 break;
4568 case X86ISD::UNPCKL:
4569 DecodeUNPCKLMask(VT, Mask);
4570 break;
4571 case X86ISD::MOVHLPS:
4572 DecodeMOVHLPSMask(NumElems, Mask);
4573 break;
4574 case X86ISD::MOVLHPS:
4575 DecodeMOVLHPSMask(NumElems, Mask);
4576 break;
4577 case X86ISD::PSHUFD:
4578 case X86ISD::VPERMILP:
4579 ImmN = N->getOperand(N->getNumOperands()-1);
4580 DecodePSHUFMask(VT, cast<ConstantSDNode>(ImmN)->getZExtValue(), Mask);
Craig Topper89f4e662012-03-20 07:17:59 +00004581 IsUnary = true;
Craig Toppera1ffc682012-03-20 06:42:26 +00004582 break;
4583 case X86ISD::PSHUFHW:
4584 ImmN = N->getOperand(N->getNumOperands()-1);
Craig Toppera9a568a2012-05-02 08:03:44 +00004585 DecodePSHUFHWMask(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::PSHUFLW:
4589 ImmN = N->getOperand(N->getNumOperands()-1);
Craig Toppera9a568a2012-05-02 08:03:44 +00004590 DecodePSHUFLWMask(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;
Craig Topperbdcbcb32012-05-06 18:54:26 +00004593 case X86ISD::VPERMI:
4594 ImmN = N->getOperand(N->getNumOperands()-1);
4595 DecodeVPERMMask(cast<ConstantSDNode>(ImmN)->getZExtValue(), Mask);
4596 IsUnary = true;
4597 break;
Craig Toppera1ffc682012-03-20 06:42:26 +00004598 case X86ISD::MOVSS:
4599 case X86ISD::MOVSD: {
4600 // The index 0 always comes from the first element of the second source,
4601 // this is why MOVSS and MOVSD are used in the first place. The other
4602 // elements come from the other positions of the first source vector
4603 Mask.push_back(NumElems);
4604 for (unsigned i = 1; i != NumElems; ++i) {
4605 Mask.push_back(i);
4606 }
4607 break;
4608 }
4609 case X86ISD::VPERM2X128:
4610 ImmN = N->getOperand(N->getNumOperands()-1);
4611 DecodeVPERM2X128Mask(VT, cast<ConstantSDNode>(ImmN)->getZExtValue(), Mask);
Craig Topper2091df32012-04-17 05:54:54 +00004612 if (Mask.empty()) return false;
Craig Toppera1ffc682012-03-20 06:42:26 +00004613 break;
4614 case X86ISD::MOVDDUP:
4615 case X86ISD::MOVLHPD:
4616 case X86ISD::MOVLPD:
4617 case X86ISD::MOVLPS:
4618 case X86ISD::MOVSHDUP:
4619 case X86ISD::MOVSLDUP:
4620 case X86ISD::PALIGN:
4621 // Not yet implemented
4622 return false;
4623 default: llvm_unreachable("unknown target shuffle node");
4624 }
4625
4626 return true;
4627}
4628
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004629/// getShuffleScalarElt - Returns the scalar element that will make up the ith
4630/// element of the result of the vector shuffle.
Craig Topper3d092db2012-03-21 02:14:01 +00004631static SDValue getShuffleScalarElt(SDNode *N, unsigned Index, SelectionDAG &DAG,
Benjamin Kramer050db522011-03-26 12:38:19 +00004632 unsigned Depth) {
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004633 if (Depth == 6)
4634 return SDValue(); // Limit search depth.
4635
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004636 SDValue V = SDValue(N, 0);
4637 EVT VT = V.getValueType();
4638 unsigned Opcode = V.getOpcode();
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004639
4640 // Recurse into ISD::VECTOR_SHUFFLE node to find scalars.
4641 if (const ShuffleVectorSDNode *SV = dyn_cast<ShuffleVectorSDNode>(N)) {
Craig Topper3d092db2012-03-21 02:14:01 +00004642 int Elt = SV->getMaskElt(Index);
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004643
Craig Topper3d092db2012-03-21 02:14:01 +00004644 if (Elt < 0)
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004645 return DAG.getUNDEF(VT.getVectorElementType());
4646
Craig Topperd156dc12012-02-06 07:17:51 +00004647 unsigned NumElems = VT.getVectorNumElements();
Craig Topper3d092db2012-03-21 02:14:01 +00004648 SDValue NewV = (Elt < (int)NumElems) ? SV->getOperand(0)
4649 : SV->getOperand(1);
4650 return getShuffleScalarElt(NewV.getNode(), Elt % NumElems, DAG, Depth+1);
Evan Chengf26ffe92008-05-29 08:22:04 +00004651 }
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004652
4653 // Recurse into target specific vector shuffles to find scalars.
4654 if (isTargetShuffle(Opcode)) {
Craig Topperd978c542012-05-06 19:46:21 +00004655 MVT ShufVT = V.getValueType().getSimpleVT();
4656 unsigned NumElems = ShufVT.getVectorNumElements();
Craig Toppera1ffc682012-03-20 06:42:26 +00004657 SmallVector<int, 16> ShuffleMask;
Craig Topper89f4e662012-03-20 07:17:59 +00004658 bool IsUnary;
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004659
Craig Topperd978c542012-05-06 19:46:21 +00004660 if (!getTargetShuffleMask(N, ShufVT, ShuffleMask, IsUnary))
Craig Toppera1ffc682012-03-20 06:42:26 +00004661 return SDValue();
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004662
Craig Topper3d092db2012-03-21 02:14:01 +00004663 int Elt = ShuffleMask[Index];
4664 if (Elt < 0)
Craig Topperd978c542012-05-06 19:46:21 +00004665 return DAG.getUNDEF(ShufVT.getVectorElementType());
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004666
Craig Topper3d092db2012-03-21 02:14:01 +00004667 SDValue NewV = (Elt < (int)NumElems) ? N->getOperand(0)
Craig Topperd978c542012-05-06 19:46:21 +00004668 : N->getOperand(1);
Craig Topper3d092db2012-03-21 02:14:01 +00004669 return getShuffleScalarElt(NewV.getNode(), Elt % NumElems, DAG,
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004670 Depth+1);
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004671 }
4672
4673 // Actual nodes that may contain scalar elements
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004674 if (Opcode == ISD::BITCAST) {
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004675 V = V.getOperand(0);
4676 EVT SrcVT = V.getValueType();
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00004677 unsigned NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004678
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00004679 if (!SrcVT.isVector() || SrcVT.getVectorNumElements() != NumElems)
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004680 return SDValue();
4681 }
4682
4683 if (V.getOpcode() == ISD::SCALAR_TO_VECTOR)
4684 return (Index == 0) ? V.getOperand(0)
Craig Topper3d092db2012-03-21 02:14:01 +00004685 : DAG.getUNDEF(VT.getVectorElementType());
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004686
4687 if (V.getOpcode() == ISD::BUILD_VECTOR)
4688 return V.getOperand(Index);
4689
4690 return SDValue();
4691}
4692
4693/// getNumOfConsecutiveZeros - Return the number of elements of a vector
4694/// shuffle operation which come from a consecutively from a zero. The
Chris Lattner7a2bdde2011-04-15 05:18:47 +00004695/// search can start in two different directions, from left or right.
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004696static
Craig Topper3d092db2012-03-21 02:14:01 +00004697unsigned getNumOfConsecutiveZeros(ShuffleVectorSDNode *SVOp, unsigned NumElems,
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004698 bool ZerosFromLeft, SelectionDAG &DAG) {
Craig Topper3d092db2012-03-21 02:14:01 +00004699 unsigned i;
4700 for (i = 0; i != NumElems; ++i) {
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004701 unsigned Index = ZerosFromLeft ? i : NumElems-i-1;
Craig Topper3d092db2012-03-21 02:14:01 +00004702 SDValue Elt = getShuffleScalarElt(SVOp, Index, DAG, 0);
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004703 if (!(Elt.getNode() &&
4704 (Elt.getOpcode() == ISD::UNDEF || X86::isZeroNode(Elt))))
4705 break;
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004706 }
4707
4708 return i;
4709}
4710
Craig Topper3d092db2012-03-21 02:14:01 +00004711/// isShuffleMaskConsecutive - Check if the shuffle mask indicies [MaskI, MaskE)
4712/// correspond consecutively to elements from one of the vector operands,
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004713/// starting from its index OpIdx. Also tell OpNum which source vector operand.
4714static
Craig Topper3d092db2012-03-21 02:14:01 +00004715bool isShuffleMaskConsecutive(ShuffleVectorSDNode *SVOp,
4716 unsigned MaskI, unsigned MaskE, unsigned OpIdx,
4717 unsigned NumElems, unsigned &OpNum) {
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004718 bool SeenV1 = false;
4719 bool SeenV2 = false;
4720
Craig Topper3d092db2012-03-21 02:14:01 +00004721 for (unsigned i = MaskI; i != MaskE; ++i, ++OpIdx) {
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004722 int Idx = SVOp->getMaskElt(i);
4723 // Ignore undef indicies
4724 if (Idx < 0)
4725 continue;
4726
Craig Topper3d092db2012-03-21 02:14:01 +00004727 if (Idx < (int)NumElems)
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004728 SeenV1 = true;
4729 else
4730 SeenV2 = true;
4731
4732 // Only accept consecutive elements from the same vector
4733 if ((Idx % NumElems != OpIdx) || (SeenV1 && SeenV2))
4734 return false;
4735 }
4736
4737 OpNum = SeenV1 ? 0 : 1;
4738 return true;
4739}
4740
4741/// isVectorShiftRight - Returns true if the shuffle can be implemented as a
4742/// logical left shift of a vector.
4743static bool isVectorShiftRight(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
4744 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
4745 unsigned NumElems = SVOp->getValueType(0).getVectorNumElements();
4746 unsigned NumZeros = getNumOfConsecutiveZeros(SVOp, NumElems,
4747 false /* check zeros from right */, DAG);
4748 unsigned OpSrc;
4749
4750 if (!NumZeros)
4751 return false;
4752
4753 // Considering the elements in the mask that are not consecutive zeros,
4754 // check if they consecutively come from only one of the source vectors.
4755 //
4756 // V1 = {X, A, B, C} 0
4757 // \ \ \ /
4758 // vector_shuffle V1, V2 <1, 2, 3, X>
4759 //
4760 if (!isShuffleMaskConsecutive(SVOp,
4761 0, // Mask Start Index
Craig Topper3d092db2012-03-21 02:14:01 +00004762 NumElems-NumZeros, // Mask End Index(exclusive)
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004763 NumZeros, // Where to start looking in the src vector
4764 NumElems, // Number of elements in vector
4765 OpSrc)) // Which source operand ?
4766 return false;
4767
4768 isLeft = false;
4769 ShAmt = NumZeros;
4770 ShVal = SVOp->getOperand(OpSrc);
4771 return true;
4772}
4773
4774/// isVectorShiftLeft - Returns true if the shuffle can be implemented as a
4775/// logical left shift of a vector.
4776static bool isVectorShiftLeft(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
4777 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
4778 unsigned NumElems = SVOp->getValueType(0).getVectorNumElements();
4779 unsigned NumZeros = getNumOfConsecutiveZeros(SVOp, NumElems,
4780 true /* check zeros from left */, DAG);
4781 unsigned OpSrc;
4782
4783 if (!NumZeros)
4784 return false;
4785
4786 // Considering the elements in the mask that are not consecutive zeros,
4787 // check if they consecutively come from only one of the source vectors.
4788 //
4789 // 0 { A, B, X, X } = V2
4790 // / \ / /
4791 // vector_shuffle V1, V2 <X, X, 4, 5>
4792 //
4793 if (!isShuffleMaskConsecutive(SVOp,
4794 NumZeros, // Mask Start Index
Craig Topper3d092db2012-03-21 02:14:01 +00004795 NumElems, // Mask End Index(exclusive)
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004796 0, // Where to start looking in the src vector
4797 NumElems, // Number of elements in vector
4798 OpSrc)) // Which source operand ?
4799 return false;
4800
4801 isLeft = true;
4802 ShAmt = NumZeros;
4803 ShVal = SVOp->getOperand(OpSrc);
4804 return true;
Evan Chengf26ffe92008-05-29 08:22:04 +00004805}
4806
4807/// isVectorShift - Returns true if the shuffle can be implemented as a
4808/// logical left or right shift of a vector.
Nate Begeman9008ca62009-04-27 18:41:29 +00004809static bool isVectorShift(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
Dan Gohman475871a2008-07-27 21:46:04 +00004810 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00004811 // Although the logic below support any bitwidth size, there are no
4812 // shift instructions which handle more than 128-bit vectors.
Craig Topper7a9a28b2012-08-12 02:23:29 +00004813 if (!SVOp->getValueType(0).is128BitVector())
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00004814 return false;
4815
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004816 if (isVectorShiftLeft(SVOp, DAG, isLeft, ShVal, ShAmt) ||
4817 isVectorShiftRight(SVOp, DAG, isLeft, ShVal, ShAmt))
4818 return true;
Evan Chengf26ffe92008-05-29 08:22:04 +00004819
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004820 return false;
Evan Chengf26ffe92008-05-29 08:22:04 +00004821}
4822
Evan Chengc78d3b42006-04-24 18:01:45 +00004823/// LowerBuildVectorv16i8 - Custom lower build_vector of v16i8.
4824///
Dan Gohman475871a2008-07-27 21:46:04 +00004825static SDValue LowerBuildVectorv16i8(SDValue Op, unsigned NonZeros,
Evan Chengc78d3b42006-04-24 18:01:45 +00004826 unsigned NumNonZero, unsigned NumZero,
Dan Gohmand858e902010-04-17 15:26:15 +00004827 SelectionDAG &DAG,
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00004828 const X86Subtarget* Subtarget,
Dan Gohmand858e902010-04-17 15:26:15 +00004829 const TargetLowering &TLI) {
Evan Chengc78d3b42006-04-24 18:01:45 +00004830 if (NumNonZero > 8)
Dan Gohman475871a2008-07-27 21:46:04 +00004831 return SDValue();
Evan Chengc78d3b42006-04-24 18:01:45 +00004832
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004833 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00004834 SDValue V(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00004835 bool First = true;
4836 for (unsigned i = 0; i < 16; ++i) {
4837 bool ThisIsNonZero = (NonZeros & (1 << i)) != 0;
4838 if (ThisIsNonZero && First) {
4839 if (NumZero)
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00004840 V = getZeroVector(MVT::v8i16, Subtarget, DAG, dl);
Evan Chengc78d3b42006-04-24 18:01:45 +00004841 else
Owen Anderson825b72b2009-08-11 20:47:22 +00004842 V = DAG.getUNDEF(MVT::v8i16);
Evan Chengc78d3b42006-04-24 18:01:45 +00004843 First = false;
4844 }
4845
4846 if ((i & 1) != 0) {
Dan Gohman475871a2008-07-27 21:46:04 +00004847 SDValue ThisElt(0, 0), LastElt(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00004848 bool LastIsNonZero = (NonZeros & (1 << (i-1))) != 0;
4849 if (LastIsNonZero) {
Scott Michelfdc40a02009-02-17 22:15:04 +00004850 LastElt = DAG.getNode(ISD::ZERO_EXTEND, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004851 MVT::i16, Op.getOperand(i-1));
Evan Chengc78d3b42006-04-24 18:01:45 +00004852 }
4853 if (ThisIsNonZero) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004854 ThisElt = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i16, Op.getOperand(i));
4855 ThisElt = DAG.getNode(ISD::SHL, dl, MVT::i16,
4856 ThisElt, DAG.getConstant(8, MVT::i8));
Evan Chengc78d3b42006-04-24 18:01:45 +00004857 if (LastIsNonZero)
Owen Anderson825b72b2009-08-11 20:47:22 +00004858 ThisElt = DAG.getNode(ISD::OR, dl, MVT::i16, ThisElt, LastElt);
Evan Chengc78d3b42006-04-24 18:01:45 +00004859 } else
4860 ThisElt = LastElt;
4861
Gabor Greifba36cb52008-08-28 21:40:38 +00004862 if (ThisElt.getNode())
Owen Anderson825b72b2009-08-11 20:47:22 +00004863 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, V, ThisElt,
Chris Lattner0bd48932008-01-17 07:00:52 +00004864 DAG.getIntPtrConstant(i/2));
Evan Chengc78d3b42006-04-24 18:01:45 +00004865 }
4866 }
4867
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004868 return DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, V);
Evan Chengc78d3b42006-04-24 18:01:45 +00004869}
4870
Bill Wendlinga348c562007-03-22 18:42:45 +00004871/// LowerBuildVectorv8i16 - Custom lower build_vector of v8i16.
Evan Chengc78d3b42006-04-24 18:01:45 +00004872///
Dan Gohman475871a2008-07-27 21:46:04 +00004873static SDValue LowerBuildVectorv8i16(SDValue Op, unsigned NonZeros,
Dan Gohmand858e902010-04-17 15:26:15 +00004874 unsigned NumNonZero, unsigned NumZero,
4875 SelectionDAG &DAG,
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00004876 const X86Subtarget* Subtarget,
Dan Gohmand858e902010-04-17 15:26:15 +00004877 const TargetLowering &TLI) {
Evan Chengc78d3b42006-04-24 18:01:45 +00004878 if (NumNonZero > 4)
Dan Gohman475871a2008-07-27 21:46:04 +00004879 return SDValue();
Evan Chengc78d3b42006-04-24 18:01:45 +00004880
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004881 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00004882 SDValue V(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00004883 bool First = true;
4884 for (unsigned i = 0; i < 8; ++i) {
4885 bool isNonZero = (NonZeros & (1 << i)) != 0;
4886 if (isNonZero) {
4887 if (First) {
4888 if (NumZero)
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00004889 V = getZeroVector(MVT::v8i16, Subtarget, DAG, dl);
Evan Chengc78d3b42006-04-24 18:01:45 +00004890 else
Owen Anderson825b72b2009-08-11 20:47:22 +00004891 V = DAG.getUNDEF(MVT::v8i16);
Evan Chengc78d3b42006-04-24 18:01:45 +00004892 First = false;
4893 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004894 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004895 MVT::v8i16, V, Op.getOperand(i),
Chris Lattner0bd48932008-01-17 07:00:52 +00004896 DAG.getIntPtrConstant(i));
Evan Chengc78d3b42006-04-24 18:01:45 +00004897 }
4898 }
4899
4900 return V;
4901}
4902
Evan Chengf26ffe92008-05-29 08:22:04 +00004903/// getVShift - Return a vector logical shift node.
4904///
Owen Andersone50ed302009-08-10 22:56:29 +00004905static SDValue getVShift(bool isLeft, EVT VT, SDValue SrcOp,
Nate Begeman9008ca62009-04-27 18:41:29 +00004906 unsigned NumBits, SelectionDAG &DAG,
4907 const TargetLowering &TLI, DebugLoc dl) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00004908 assert(VT.is128BitVector() && "Unknown type for VShift");
Dale Johannesen0488fb62010-09-30 23:57:10 +00004909 EVT ShVT = MVT::v2i64;
Craig Toppered2e13d2012-01-22 19:15:14 +00004910 unsigned Opc = isLeft ? X86ISD::VSHLDQ : X86ISD::VSRLDQ;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004911 SrcOp = DAG.getNode(ISD::BITCAST, dl, ShVT, SrcOp);
4912 return DAG.getNode(ISD::BITCAST, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00004913 DAG.getNode(Opc, dl, ShVT, SrcOp,
Owen Anderson95771af2011-02-25 21:41:48 +00004914 DAG.getConstant(NumBits,
4915 TLI.getShiftAmountTy(SrcOp.getValueType()))));
Evan Chengf26ffe92008-05-29 08:22:04 +00004916}
4917
Dan Gohman475871a2008-07-27 21:46:04 +00004918SDValue
Evan Chengc3630942009-12-09 21:00:30 +00004919X86TargetLowering::LowerAsSplatVectorLoad(SDValue SrcOp, EVT VT, DebugLoc dl,
Dan Gohmand858e902010-04-17 15:26:15 +00004920 SelectionDAG &DAG) const {
Michael J. Spencerec38de22010-10-10 22:04:20 +00004921
Evan Chengc3630942009-12-09 21:00:30 +00004922 // Check if the scalar load can be widened into a vector load. And if
4923 // the address is "base + cst" see if the cst can be "absorbed" into
4924 // the shuffle mask.
4925 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(SrcOp)) {
4926 SDValue Ptr = LD->getBasePtr();
4927 if (!ISD::isNormalLoad(LD) || LD->isVolatile())
4928 return SDValue();
4929 EVT PVT = LD->getValueType(0);
4930 if (PVT != MVT::i32 && PVT != MVT::f32)
4931 return SDValue();
4932
4933 int FI = -1;
4934 int64_t Offset = 0;
4935 if (FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr)) {
4936 FI = FINode->getIndex();
4937 Offset = 0;
Chris Lattner0a9481f2011-02-13 22:25:43 +00004938 } else if (DAG.isBaseWithConstantOffset(Ptr) &&
Evan Chengc3630942009-12-09 21:00:30 +00004939 isa<FrameIndexSDNode>(Ptr.getOperand(0))) {
4940 FI = cast<FrameIndexSDNode>(Ptr.getOperand(0))->getIndex();
4941 Offset = Ptr.getConstantOperandVal(1);
4942 Ptr = Ptr.getOperand(0);
4943 } else {
4944 return SDValue();
4945 }
4946
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004947 // FIXME: 256-bit vector instructions don't require a strict alignment,
4948 // improve this code to support it better.
4949 unsigned RequiredAlign = VT.getSizeInBits()/8;
Evan Chengc3630942009-12-09 21:00:30 +00004950 SDValue Chain = LD->getChain();
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004951 // Make sure the stack object alignment is at least 16 or 32.
Evan Chengc3630942009-12-09 21:00:30 +00004952 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004953 if (DAG.InferPtrAlignment(Ptr) < RequiredAlign) {
Evan Chengc3630942009-12-09 21:00:30 +00004954 if (MFI->isFixedObjectIndex(FI)) {
Eric Christophere9625cf2010-01-23 06:02:43 +00004955 // Can't change the alignment. FIXME: It's possible to compute
4956 // the exact stack offset and reference FI + adjust offset instead.
4957 // If someone *really* cares about this. That's the way to implement it.
4958 return SDValue();
Evan Chengc3630942009-12-09 21:00:30 +00004959 } else {
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004960 MFI->setObjectAlignment(FI, RequiredAlign);
Evan Chengc3630942009-12-09 21:00:30 +00004961 }
4962 }
4963
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004964 // (Offset % 16 or 32) must be multiple of 4. Then address is then
Evan Chengc3630942009-12-09 21:00:30 +00004965 // Ptr + (Offset & ~15).
4966 if (Offset < 0)
4967 return SDValue();
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004968 if ((Offset % RequiredAlign) & 3)
Evan Chengc3630942009-12-09 21:00:30 +00004969 return SDValue();
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004970 int64_t StartOffset = Offset & ~(RequiredAlign-1);
Evan Chengc3630942009-12-09 21:00:30 +00004971 if (StartOffset)
4972 Ptr = DAG.getNode(ISD::ADD, Ptr.getDebugLoc(), Ptr.getValueType(),
4973 Ptr,DAG.getConstant(StartOffset, Ptr.getValueType()));
4974
4975 int EltNo = (Offset - StartOffset) >> 2;
Craig Topper66ddd152012-04-27 22:54:43 +00004976 unsigned NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004977
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004978 EVT NVT = EVT::getVectorVT(*DAG.getContext(), PVT, NumElems);
4979 SDValue V1 = DAG.getLoad(NVT, dl, Chain, Ptr,
Chris Lattner51abfe42010-09-21 06:02:19 +00004980 LD->getPointerInfo().getWithOffset(StartOffset),
Pete Cooperd752e0f2011-11-08 18:42:53 +00004981 false, false, false, 0);
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004982
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004983 SmallVector<int, 8> Mask;
Craig Topper66ddd152012-04-27 22:54:43 +00004984 for (unsigned i = 0; i != NumElems; ++i)
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004985 Mask.push_back(EltNo);
4986
Craig Toppercc3000632012-01-30 07:50:31 +00004987 return DAG.getVectorShuffle(NVT, dl, V1, DAG.getUNDEF(NVT), &Mask[0]);
Evan Chengc3630942009-12-09 21:00:30 +00004988 }
4989
4990 return SDValue();
4991}
4992
Michael J. Spencerec38de22010-10-10 22:04:20 +00004993/// EltsFromConsecutiveLoads - Given the initializing elements 'Elts' of a
4994/// vector of type 'VT', see if the elements can be replaced by a single large
Nate Begeman1449f292010-03-24 22:19:06 +00004995/// load which has the same value as a build_vector whose operands are 'elts'.
4996///
4997/// Example: <load i32 *a, load i32 *a+4, undef, undef> -> zextload a
Michael J. Spencerec38de22010-10-10 22:04:20 +00004998///
Nate Begeman1449f292010-03-24 22:19:06 +00004999/// FIXME: we'd also like to handle the case where the last elements are zero
5000/// rather than undef via VZEXT_LOAD, but we do not detect that case today.
5001/// There's even a handy isZeroNode for that purpose.
Nate Begemanfdea31a2010-03-24 20:49:50 +00005002static SDValue EltsFromConsecutiveLoads(EVT VT, SmallVectorImpl<SDValue> &Elts,
Chris Lattner88641552010-09-22 00:34:38 +00005003 DebugLoc &DL, SelectionDAG &DAG) {
Nate Begemanfdea31a2010-03-24 20:49:50 +00005004 EVT EltVT = VT.getVectorElementType();
5005 unsigned NumElems = Elts.size();
Michael J. Spencerec38de22010-10-10 22:04:20 +00005006
Nate Begemanfdea31a2010-03-24 20:49:50 +00005007 LoadSDNode *LDBase = NULL;
5008 unsigned LastLoadedElt = -1U;
Michael J. Spencerec38de22010-10-10 22:04:20 +00005009
Nate Begeman1449f292010-03-24 22:19:06 +00005010 // For each element in the initializer, see if we've found a load or an undef.
Michael J. Spencerec38de22010-10-10 22:04:20 +00005011 // If we don't find an initial load element, or later load elements are
Nate Begeman1449f292010-03-24 22:19:06 +00005012 // non-consecutive, bail out.
Nate Begemanfdea31a2010-03-24 20:49:50 +00005013 for (unsigned i = 0; i < NumElems; ++i) {
5014 SDValue Elt = Elts[i];
Michael J. Spencerec38de22010-10-10 22:04:20 +00005015
Nate Begemanfdea31a2010-03-24 20:49:50 +00005016 if (!Elt.getNode() ||
5017 (Elt.getOpcode() != ISD::UNDEF && !ISD::isNON_EXTLoad(Elt.getNode())))
5018 return SDValue();
5019 if (!LDBase) {
5020 if (Elt.getNode()->getOpcode() == ISD::UNDEF)
5021 return SDValue();
5022 LDBase = cast<LoadSDNode>(Elt.getNode());
5023 LastLoadedElt = i;
5024 continue;
5025 }
5026 if (Elt.getOpcode() == ISD::UNDEF)
5027 continue;
5028
5029 LoadSDNode *LD = cast<LoadSDNode>(Elt);
5030 if (!DAG.isConsecutiveLoad(LD, LDBase, EltVT.getSizeInBits()/8, i))
5031 return SDValue();
5032 LastLoadedElt = i;
5033 }
Nate Begeman1449f292010-03-24 22:19:06 +00005034
5035 // If we have found an entire vector of loads and undefs, then return a large
5036 // load of the entire vector width starting at the base pointer. If we found
5037 // consecutive loads for the low half, generate a vzext_load node.
Nate Begemanfdea31a2010-03-24 20:49:50 +00005038 if (LastLoadedElt == NumElems - 1) {
5039 if (DAG.InferPtrAlignment(LDBase->getBasePtr()) >= 16)
Chris Lattner88641552010-09-22 00:34:38 +00005040 return DAG.getLoad(VT, DL, LDBase->getChain(), LDBase->getBasePtr(),
Chris Lattner51abfe42010-09-21 06:02:19 +00005041 LDBase->getPointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00005042 LDBase->isVolatile(), LDBase->isNonTemporal(),
5043 LDBase->isInvariant(), 0);
Chris Lattner88641552010-09-22 00:34:38 +00005044 return DAG.getLoad(VT, DL, LDBase->getChain(), LDBase->getBasePtr(),
Chris Lattner51abfe42010-09-21 06:02:19 +00005045 LDBase->getPointerInfo(),
Nate Begemanfdea31a2010-03-24 20:49:50 +00005046 LDBase->isVolatile(), LDBase->isNonTemporal(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00005047 LDBase->isInvariant(), LDBase->getAlignment());
Craig Topper69947b92012-04-23 06:57:04 +00005048 }
5049 if (NumElems == 4 && LastLoadedElt == 1 &&
5050 DAG.getTargetLoweringInfo().isTypeLegal(MVT::v2i64)) {
Nate Begemanfdea31a2010-03-24 20:49:50 +00005051 SDVTList Tys = DAG.getVTList(MVT::v2i64, MVT::Other);
5052 SDValue Ops[] = { LDBase->getChain(), LDBase->getBasePtr() };
Eli Friedman322ea082011-09-14 23:42:45 +00005053 SDValue ResNode =
5054 DAG.getMemIntrinsicNode(X86ISD::VZEXT_LOAD, DL, Tys, Ops, 2, MVT::i64,
5055 LDBase->getPointerInfo(),
5056 LDBase->getAlignment(),
5057 false/*isVolatile*/, true/*ReadMem*/,
5058 false/*WriteMem*/);
Manman Ren2b7a2e82012-08-31 23:16:57 +00005059
5060 // Make sure the newly-created LOAD is in the same position as LDBase in
5061 // terms of dependency. We create a TokenFactor for LDBase and ResNode, and
5062 // update uses of LDBase's output chain to use the TokenFactor.
5063 if (LDBase->hasAnyUseOfValue(1)) {
5064 SDValue NewChain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other,
5065 SDValue(LDBase, 1), SDValue(ResNode.getNode(), 1));
5066 DAG.ReplaceAllUsesOfValueWith(SDValue(LDBase, 1), NewChain);
5067 DAG.UpdateNodeOperands(NewChain.getNode(), SDValue(LDBase, 1),
5068 SDValue(ResNode.getNode(), 1));
5069 }
5070
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005071 return DAG.getNode(ISD::BITCAST, DL, VT, ResNode);
Nate Begemanfdea31a2010-03-24 20:49:50 +00005072 }
5073 return SDValue();
5074}
5075
Nadav Rotem9d68b062012-04-08 12:54:54 +00005076/// LowerVectorBroadcast - Attempt to use the vbroadcast instruction
5077/// to generate a splat value for the following cases:
5078/// 1. A splat BUILD_VECTOR which uses a single scalar load, or a constant.
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005079/// 2. A splat shuffle which uses a scalar_to_vector node which comes from
Nadav Rotem9d68b062012-04-08 12:54:54 +00005080/// a scalar load, or a constant.
5081/// The VBROADCAST node is returned when a pattern is found,
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00005082/// or SDValue() otherwise.
Nadav Rotem154819d2012-04-09 07:45:58 +00005083SDValue
Craig Topper55b24052012-09-11 06:15:32 +00005084X86TargetLowering::LowerVectorBroadcast(SDValue Op, SelectionDAG &DAG) const {
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00005085 if (!Subtarget->hasFp256())
Craig Toppera9376332012-01-10 08:23:59 +00005086 return SDValue();
5087
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005088 EVT VT = Op.getValueType();
Nadav Rotem154819d2012-04-09 07:45:58 +00005089 DebugLoc dl = Op.getDebugLoc();
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005090
Craig Topper5da8a802012-05-04 05:49:51 +00005091 assert((VT.is128BitVector() || VT.is256BitVector()) &&
5092 "Unsupported vector type for broadcast.");
5093
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005094 SDValue Ld;
Nadav Rotem9d68b062012-04-08 12:54:54 +00005095 bool ConstSplatVal;
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005096
Nadav Rotem9d68b062012-04-08 12:54:54 +00005097 switch (Op.getOpcode()) {
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005098 default:
5099 // Unknown pattern found.
5100 return SDValue();
5101
5102 case ISD::BUILD_VECTOR: {
5103 // The BUILD_VECTOR node must be a splat.
Nadav Rotem9d68b062012-04-08 12:54:54 +00005104 if (!isSplatVector(Op.getNode()))
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005105 return SDValue();
5106
Nadav Rotem9d68b062012-04-08 12:54:54 +00005107 Ld = Op.getOperand(0);
5108 ConstSplatVal = (Ld.getOpcode() == ISD::Constant ||
5109 Ld.getOpcode() == ISD::ConstantFP);
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00005110
5111 // The suspected load node has several users. Make sure that all
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005112 // of its users are from the BUILD_VECTOR node.
Nadav Rotem9d68b062012-04-08 12:54:54 +00005113 // Constants may have multiple users.
5114 if (!ConstSplatVal && !Ld->hasNUsesOfValue(VT.getVectorNumElements(), 0))
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005115 return SDValue();
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00005116 break;
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005117 }
5118
5119 case ISD::VECTOR_SHUFFLE: {
5120 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
5121
5122 // Shuffles must have a splat mask where the first element is
5123 // broadcasted.
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00005124 if ((!SVOp->isSplat()) || SVOp->getMaskElt(0) != 0)
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005125 return SDValue();
5126
5127 SDValue Sc = Op.getOperand(0);
Nadav Rotemb88e8dd2012-05-10 12:50:02 +00005128 if (Sc.getOpcode() != ISD::SCALAR_TO_VECTOR &&
Elena Demikhovsky8f40f7b2012-07-01 06:12:26 +00005129 Sc.getOpcode() != ISD::BUILD_VECTOR) {
5130
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00005131 if (!Subtarget->hasInt256())
Elena Demikhovsky8f40f7b2012-07-01 06:12:26 +00005132 return SDValue();
5133
5134 // Use the register form of the broadcast instruction available on AVX2.
5135 if (VT.is256BitVector())
5136 Sc = Extract128BitVector(Sc, 0, DAG, dl);
5137 return DAG.getNode(X86ISD::VBROADCAST, dl, VT, Sc);
5138 }
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005139
5140 Ld = Sc.getOperand(0);
Nadav Rotem9d68b062012-04-08 12:54:54 +00005141 ConstSplatVal = (Ld.getOpcode() == ISD::Constant ||
Nadav Rotem154819d2012-04-09 07:45:58 +00005142 Ld.getOpcode() == ISD::ConstantFP);
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005143
5144 // The scalar_to_vector node and the suspected
5145 // load node must have exactly one user.
Nadav Rotem9d68b062012-04-08 12:54:54 +00005146 // Constants may have multiple users.
5147 if (!ConstSplatVal && (!Sc.hasOneUse() || !Ld.hasOneUse()))
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005148 return SDValue();
5149 break;
5150 }
5151 }
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00005152
Craig Topper7a9a28b2012-08-12 02:23:29 +00005153 bool Is256 = VT.is256BitVector();
Nadav Rotem9d68b062012-04-08 12:54:54 +00005154
5155 // Handle the broadcasting a single constant scalar from the constant pool
5156 // into a vector. On Sandybridge it is still better to load a constant vector
5157 // from the constant pool and not to broadcast it from a scalar.
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00005158 if (ConstSplatVal && Subtarget->hasInt256()) {
Nadav Rotem9d68b062012-04-08 12:54:54 +00005159 EVT CVT = Ld.getValueType();
5160 assert(!CVT.isVector() && "Must not broadcast a vector type");
5161 unsigned ScalarSize = CVT.getSizeInBits();
5162
Craig Topper5da8a802012-05-04 05:49:51 +00005163 if (ScalarSize == 32 || (Is256 && ScalarSize == 64)) {
Nadav Rotem9d68b062012-04-08 12:54:54 +00005164 const Constant *C = 0;
5165 if (ConstantSDNode *CI = dyn_cast<ConstantSDNode>(Ld))
5166 C = CI->getConstantIntValue();
5167 else if (ConstantFPSDNode *CF = dyn_cast<ConstantFPSDNode>(Ld))
5168 C = CF->getConstantFPValue();
5169
5170 assert(C && "Invalid constant type");
5171
Nadav Rotem154819d2012-04-09 07:45:58 +00005172 SDValue CP = DAG.getConstantPool(C, getPointerTy());
Nadav Rotem9d68b062012-04-08 12:54:54 +00005173 unsigned Alignment = cast<ConstantPoolSDNode>(CP)->getAlignment();
Nadav Rotem154819d2012-04-09 07:45:58 +00005174 Ld = DAG.getLoad(CVT, dl, DAG.getEntryNode(), CP,
Craig Topper6643d9c2012-05-04 06:18:33 +00005175 MachinePointerInfo::getConstantPool(),
5176 false, false, false, Alignment);
Nadav Rotem9d68b062012-04-08 12:54:54 +00005177
Nadav Rotem9d68b062012-04-08 12:54:54 +00005178 return DAG.getNode(X86ISD::VBROADCAST, dl, VT, Ld);
5179 }
5180 }
5181
Nadav Rotem4fc8a5d2012-05-19 19:57:37 +00005182 bool IsLoad = ISD::isNormalLoad(Ld.getNode());
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005183 unsigned ScalarSize = Ld.getValueType().getSizeInBits();
5184
Nadav Rotem4fc8a5d2012-05-19 19:57:37 +00005185 // Handle AVX2 in-register broadcasts.
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00005186 if (!IsLoad && Subtarget->hasInt256() &&
Nadav Rotem4fc8a5d2012-05-19 19:57:37 +00005187 (ScalarSize == 32 || (Is256 && ScalarSize == 64)))
5188 return DAG.getNode(X86ISD::VBROADCAST, dl, VT, Ld);
5189
5190 // The scalar source must be a normal load.
5191 if (!IsLoad)
5192 return SDValue();
5193
Craig Topper5da8a802012-05-04 05:49:51 +00005194 if (ScalarSize == 32 || (Is256 && ScalarSize == 64))
Nadav Rotem9d68b062012-04-08 12:54:54 +00005195 return DAG.getNode(X86ISD::VBROADCAST, dl, VT, Ld);
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005196
Craig Toppera9376332012-01-10 08:23:59 +00005197 // The integer check is needed for the 64-bit into 128-bit so it doesn't match
Craig Topper5da8a802012-05-04 05:49:51 +00005198 // double since there is no vbroadcastsd xmm
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00005199 if (Subtarget->hasInt256() && Ld.getValueType().isInteger()) {
Craig Topper5da8a802012-05-04 05:49:51 +00005200 if (ScalarSize == 8 || ScalarSize == 16 || ScalarSize == 64)
Nadav Rotem9d68b062012-04-08 12:54:54 +00005201 return DAG.getNode(X86ISD::VBROADCAST, dl, VT, Ld);
Craig Toppera9376332012-01-10 08:23:59 +00005202 }
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00005203
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005204 // Unsupported broadcast.
5205 return SDValue();
5206}
5207
Evan Chengc3630942009-12-09 21:00:30 +00005208SDValue
Michael Liaofacace82012-10-19 17:15:18 +00005209X86TargetLowering::buildFromShuffleMostly(SDValue Op, SelectionDAG &DAG) const {
5210 EVT VT = Op.getValueType();
5211
5212 // Skip if insert_vec_elt is not supported.
5213 if (!isOperationLegalOrCustom(ISD::INSERT_VECTOR_ELT, VT))
5214 return SDValue();
5215
5216 DebugLoc DL = Op.getDebugLoc();
5217 unsigned NumElems = Op.getNumOperands();
5218
5219 SDValue VecIn1;
5220 SDValue VecIn2;
5221 SmallVector<unsigned, 4> InsertIndices;
5222 SmallVector<int, 8> Mask(NumElems, -1);
5223
5224 for (unsigned i = 0; i != NumElems; ++i) {
5225 unsigned Opc = Op.getOperand(i).getOpcode();
5226
5227 if (Opc == ISD::UNDEF)
5228 continue;
5229
5230 if (Opc != ISD::EXTRACT_VECTOR_ELT) {
5231 // Quit if more than 1 elements need inserting.
5232 if (InsertIndices.size() > 1)
5233 return SDValue();
5234
5235 InsertIndices.push_back(i);
5236 continue;
5237 }
5238
5239 SDValue ExtractedFromVec = Op.getOperand(i).getOperand(0);
5240 SDValue ExtIdx = Op.getOperand(i).getOperand(1);
5241
5242 // Quit if extracted from vector of different type.
5243 if (ExtractedFromVec.getValueType() != VT)
5244 return SDValue();
5245
5246 // Quit if non-constant index.
5247 if (!isa<ConstantSDNode>(ExtIdx))
5248 return SDValue();
5249
5250 if (VecIn1.getNode() == 0)
5251 VecIn1 = ExtractedFromVec;
5252 else if (VecIn1 != ExtractedFromVec) {
5253 if (VecIn2.getNode() == 0)
5254 VecIn2 = ExtractedFromVec;
5255 else if (VecIn2 != ExtractedFromVec)
5256 // Quit if more than 2 vectors to shuffle
5257 return SDValue();
5258 }
5259
5260 unsigned Idx = cast<ConstantSDNode>(ExtIdx)->getZExtValue();
5261
5262 if (ExtractedFromVec == VecIn1)
5263 Mask[i] = Idx;
5264 else if (ExtractedFromVec == VecIn2)
5265 Mask[i] = Idx + NumElems;
5266 }
5267
5268 if (VecIn1.getNode() == 0)
5269 return SDValue();
5270
5271 VecIn2 = VecIn2.getNode() ? VecIn2 : DAG.getUNDEF(VT);
5272 SDValue NV = DAG.getVectorShuffle(VT, DL, VecIn1, VecIn2, &Mask[0]);
5273 for (unsigned i = 0, e = InsertIndices.size(); i != e; ++i) {
5274 unsigned Idx = InsertIndices[i];
5275 NV = DAG.getNode(ISD::INSERT_VECTOR_ELT, DL, VT, NV, Op.getOperand(Idx),
5276 DAG.getIntPtrConstant(Idx));
5277 }
5278
5279 return NV;
5280}
5281
5282SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00005283X86TargetLowering::LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005284 DebugLoc dl = Op.getDebugLoc();
David Greenea5f26012011-02-07 19:36:54 +00005285
David Greenef125a292011-02-08 19:04:41 +00005286 EVT VT = Op.getValueType();
5287 EVT ExtVT = VT.getVectorElementType();
David Greenef125a292011-02-08 19:04:41 +00005288 unsigned NumElems = Op.getNumOperands();
5289
Bruno Cardoso Lopes531f19f2011-08-01 19:51:53 +00005290 // Vectors containing all zeros can be matched by pxor and xorps later
5291 if (ISD::isBuildVectorAllZeros(Op.getNode())) {
5292 // Canonicalize this to <4 x i32> to 1) ensure the zero vectors are CSE'd
5293 // and 2) ensure that i64 scalars are eliminated on x86-32 hosts.
Craig Topper07a27622012-01-22 03:07:48 +00005294 if (VT == MVT::v4i32 || VT == MVT::v8i32)
Chris Lattner8a594482007-11-25 00:24:49 +00005295 return Op;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005296
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00005297 return getZeroVector(VT, Subtarget, DAG, dl);
Chris Lattner8a594482007-11-25 00:24:49 +00005298 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00005299
Bruno Cardoso Lopes531f19f2011-08-01 19:51:53 +00005300 // Vectors containing all ones can be matched by pcmpeqd on 128-bit width
Craig Topper745a86b2011-11-19 22:34:59 +00005301 // vectors or broken into v4i32 operations on 256-bit vectors. AVX2 can use
5302 // vpcmpeqd on 256-bit vectors.
Bruno Cardoso Lopes531f19f2011-08-01 19:51:53 +00005303 if (ISD::isBuildVectorAllOnes(Op.getNode())) {
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00005304 if (VT == MVT::v4i32 || (VT == MVT::v8i32 && Subtarget->hasInt256()))
Bruno Cardoso Lopes531f19f2011-08-01 19:51:53 +00005305 return Op;
5306
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00005307 return getOnesVector(VT, Subtarget->hasInt256(), DAG, dl);
Bruno Cardoso Lopes531f19f2011-08-01 19:51:53 +00005308 }
5309
Nadav Rotem154819d2012-04-09 07:45:58 +00005310 SDValue Broadcast = LowerVectorBroadcast(Op, DAG);
Nadav Rotem9d68b062012-04-08 12:54:54 +00005311 if (Broadcast.getNode())
5312 return Broadcast;
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005313
Owen Andersone50ed302009-08-10 22:56:29 +00005314 unsigned EVTBits = ExtVT.getSizeInBits();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005315
Evan Cheng0db9fe62006-04-25 20:13:52 +00005316 unsigned NumZero = 0;
5317 unsigned NumNonZero = 0;
5318 unsigned NonZeros = 0;
Chris Lattnerc9517fb2008-03-08 22:48:29 +00005319 bool IsAllConstants = true;
Dan Gohman475871a2008-07-27 21:46:04 +00005320 SmallSet<SDValue, 8> Values;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005321 for (unsigned i = 0; i < NumElems; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00005322 SDValue Elt = Op.getOperand(i);
Evan Chengdb2d5242007-12-12 06:45:40 +00005323 if (Elt.getOpcode() == ISD::UNDEF)
5324 continue;
5325 Values.insert(Elt);
5326 if (Elt.getOpcode() != ISD::Constant &&
5327 Elt.getOpcode() != ISD::ConstantFP)
Chris Lattnerc9517fb2008-03-08 22:48:29 +00005328 IsAllConstants = false;
Evan Cheng37b73872009-07-30 08:33:02 +00005329 if (X86::isZeroNode(Elt))
Evan Chengdb2d5242007-12-12 06:45:40 +00005330 NumZero++;
5331 else {
5332 NonZeros |= (1 << i);
5333 NumNonZero++;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005334 }
5335 }
5336
Chris Lattner97a2a562010-08-26 05:24:29 +00005337 // All undef vector. Return an UNDEF. All zero vectors were handled above.
5338 if (NumNonZero == 0)
Dale Johannesene8d72302009-02-06 23:05:02 +00005339 return DAG.getUNDEF(VT);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005340
Chris Lattner67f453a2008-03-09 05:42:06 +00005341 // Special case for single non-zero, non-undef, element.
Eli Friedman10415532009-06-06 06:05:10 +00005342 if (NumNonZero == 1) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00005343 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dan Gohman475871a2008-07-27 21:46:04 +00005344 SDValue Item = Op.getOperand(Idx);
Scott Michelfdc40a02009-02-17 22:15:04 +00005345
Chris Lattner62098042008-03-09 01:05:04 +00005346 // If this is an insertion of an i64 value on x86-32, and if the top bits of
5347 // the value are obviously zero, truncate the value to i32 and do the
5348 // insertion that way. Only do this if the value is non-constant or if the
5349 // value is a constant being inserted into element 0. It is cheaper to do
5350 // a constant pool load than it is to do a movd + shuffle.
Owen Anderson825b72b2009-08-11 20:47:22 +00005351 if (ExtVT == MVT::i64 && !Subtarget->is64Bit() &&
Chris Lattner62098042008-03-09 01:05:04 +00005352 (!IsAllConstants || Idx == 0)) {
5353 if (DAG.MaskedValueIsZero(Item, APInt::getBitsSet(64, 32, 64))) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00005354 // Handle SSE only.
5355 assert(VT == MVT::v2i64 && "Expected an SSE value type!");
5356 EVT VecVT = MVT::v4i32;
5357 unsigned VecElts = 4;
Scott Michelfdc40a02009-02-17 22:15:04 +00005358
Chris Lattner62098042008-03-09 01:05:04 +00005359 // Truncate the value (which may itself be a constant) to i32, and
5360 // convert it to a vector with movd (S2V+shuffle to zero extend).
Owen Anderson825b72b2009-08-11 20:47:22 +00005361 Item = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Item);
Dale Johannesenace16102009-02-03 19:33:06 +00005362 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VecVT, Item);
Craig Topper12216172012-01-13 08:12:35 +00005363 Item = getShuffleVectorZeroOrUndef(Item, 0, true, Subtarget, DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +00005364
Chris Lattner62098042008-03-09 01:05:04 +00005365 // Now we have our 32-bit value zero extended in the low element of
5366 // a vector. If Idx != 0, swizzle it into place.
5367 if (Idx != 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005368 SmallVector<int, 4> Mask;
5369 Mask.push_back(Idx);
5370 for (unsigned i = 1; i != VecElts; ++i)
5371 Mask.push_back(i);
Craig Topperdf966f62012-04-22 19:17:57 +00005372 Item = DAG.getVectorShuffle(VecVT, dl, Item, DAG.getUNDEF(VecVT),
Nate Begeman9008ca62009-04-27 18:41:29 +00005373 &Mask[0]);
Chris Lattner62098042008-03-09 01:05:04 +00005374 }
Craig Topper07a27622012-01-22 03:07:48 +00005375 return DAG.getNode(ISD::BITCAST, dl, VT, Item);
Chris Lattner62098042008-03-09 01:05:04 +00005376 }
5377 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005378
Chris Lattner19f79692008-03-08 22:59:52 +00005379 // If we have a constant or non-constant insertion into the low element of
5380 // a vector, we can do this with SCALAR_TO_VECTOR + shuffle of zero into
5381 // the rest of the elements. This will be matched as movd/movq/movss/movsd
Eli Friedman10415532009-06-06 06:05:10 +00005382 // depending on what the source datatype is.
5383 if (Idx == 0) {
Craig Topperd62c16e2011-12-29 03:20:51 +00005384 if (NumZero == 0)
Eli Friedman10415532009-06-06 06:05:10 +00005385 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
Craig Topperd62c16e2011-12-29 03:20:51 +00005386
5387 if (ExtVT == MVT::i32 || ExtVT == MVT::f32 || ExtVT == MVT::f64 ||
Owen Anderson825b72b2009-08-11 20:47:22 +00005388 (ExtVT == MVT::i64 && Subtarget->is64Bit())) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00005389 if (VT.is256BitVector()) {
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00005390 SDValue ZeroVec = getZeroVector(VT, Subtarget, DAG, dl);
Nadav Rotem394a1f52012-01-11 14:07:51 +00005391 return DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, ZeroVec,
5392 Item, DAG.getIntPtrConstant(0));
Elena Demikhovsky021c0a22011-12-28 08:14:01 +00005393 }
Craig Topper7a9a28b2012-08-12 02:23:29 +00005394 assert(VT.is128BitVector() && "Expected an SSE value type!");
Eli Friedman10415532009-06-06 06:05:10 +00005395 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
5396 // Turn it into a MOVL (i.e. movss, movsd, or movd) to a zero vector.
Craig Topper12216172012-01-13 08:12:35 +00005397 return getShuffleVectorZeroOrUndef(Item, 0, true, Subtarget, DAG);
Craig Topperd62c16e2011-12-29 03:20:51 +00005398 }
5399
5400 if (ExtVT == MVT::i16 || ExtVT == MVT::i8) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005401 Item = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, Item);
Craig Topper3224e6b2011-12-29 03:09:33 +00005402 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32, Item);
Craig Topper7a9a28b2012-08-12 02:23:29 +00005403 if (VT.is256BitVector()) {
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00005404 SDValue ZeroVec = getZeroVector(MVT::v8i32, Subtarget, DAG, dl);
Craig Topperb14940a2012-04-22 20:55:18 +00005405 Item = Insert128BitVector(ZeroVec, Item, 0, DAG, dl);
Craig Topper19ec2a92011-12-29 03:34:54 +00005406 } else {
Craig Topper7a9a28b2012-08-12 02:23:29 +00005407 assert(VT.is128BitVector() && "Expected an SSE value type!");
Craig Topper12216172012-01-13 08:12:35 +00005408 Item = getShuffleVectorZeroOrUndef(Item, 0, true, Subtarget, DAG);
Craig Topper19ec2a92011-12-29 03:34:54 +00005409 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005410 return DAG.getNode(ISD::BITCAST, dl, VT, Item);
Eli Friedman10415532009-06-06 06:05:10 +00005411 }
Chris Lattnerc9517fb2008-03-08 22:48:29 +00005412 }
Evan Chengf26ffe92008-05-29 08:22:04 +00005413
5414 // Is it a vector logical left shift?
5415 if (NumElems == 2 && Idx == 1 &&
Evan Cheng37b73872009-07-30 08:33:02 +00005416 X86::isZeroNode(Op.getOperand(0)) &&
5417 !X86::isZeroNode(Op.getOperand(1))) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00005418 unsigned NumBits = VT.getSizeInBits();
Evan Chengf26ffe92008-05-29 08:22:04 +00005419 return getVShift(true, VT,
Scott Michelfdc40a02009-02-17 22:15:04 +00005420 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Dale Johannesenb300d2a2009-02-07 00:55:49 +00005421 VT, Op.getOperand(1)),
Dale Johannesenace16102009-02-03 19:33:06 +00005422 NumBits/2, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00005423 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005424
Chris Lattnerc9517fb2008-03-08 22:48:29 +00005425 if (IsAllConstants) // Otherwise, it's better to do a constpool load.
Dan Gohman475871a2008-07-27 21:46:04 +00005426 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005427
Chris Lattner19f79692008-03-08 22:59:52 +00005428 // Otherwise, if this is a vector with i32 or f32 elements, and the element
5429 // is a non-constant being inserted into an element other than the low one,
5430 // we can't use a constant pool load. Instead, use SCALAR_TO_VECTOR (aka
5431 // movd/movss) to move this into the low element, then shuffle it into
5432 // place.
Evan Cheng0db9fe62006-04-25 20:13:52 +00005433 if (EVTBits == 32) {
Dale Johannesenace16102009-02-03 19:33:06 +00005434 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
Scott Michelfdc40a02009-02-17 22:15:04 +00005435
Evan Cheng0db9fe62006-04-25 20:13:52 +00005436 // Turn it into a shuffle of zero and zero-extended scalar to vector.
Craig Topper12216172012-01-13 08:12:35 +00005437 Item = getShuffleVectorZeroOrUndef(Item, 0, NumZero > 0, Subtarget, DAG);
Nate Begeman9008ca62009-04-27 18:41:29 +00005438 SmallVector<int, 8> MaskVec;
Craig Topper31a207a2012-05-04 06:39:13 +00005439 for (unsigned i = 0; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00005440 MaskVec.push_back(i == Idx ? 0 : 1);
5441 return DAG.getVectorShuffle(VT, dl, Item, DAG.getUNDEF(VT), &MaskVec[0]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005442 }
5443 }
5444
Chris Lattner67f453a2008-03-09 05:42:06 +00005445 // Splat is obviously ok. Let legalizer expand it to a shuffle.
Evan Chengc3630942009-12-09 21:00:30 +00005446 if (Values.size() == 1) {
5447 if (EVTBits == 32) {
5448 // Instead of a shuffle like this:
5449 // shuffle (scalar_to_vector (load (ptr + 4))), undef, <0, 0, 0, 0>
5450 // Check if it's possible to issue this instead.
5451 // shuffle (vload ptr)), undef, <1, 1, 1, 1>
5452 unsigned Idx = CountTrailingZeros_32(NonZeros);
5453 SDValue Item = Op.getOperand(Idx);
5454 if (Op.getNode()->isOnlyUserOf(Item.getNode()))
5455 return LowerAsSplatVectorLoad(Item, VT, dl, DAG);
5456 }
Dan Gohman475871a2008-07-27 21:46:04 +00005457 return SDValue();
Evan Chengc3630942009-12-09 21:00:30 +00005458 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005459
Dan Gohmana3941172007-07-24 22:55:08 +00005460 // A vector full of immediates; various special cases are already
5461 // handled, so this is best done with a single constant-pool load.
Chris Lattnerc9517fb2008-03-08 22:48:29 +00005462 if (IsAllConstants)
Dan Gohman475871a2008-07-27 21:46:04 +00005463 return SDValue();
Dan Gohmana3941172007-07-24 22:55:08 +00005464
Bruno Cardoso Lopes6683efb2011-07-22 00:15:07 +00005465 // For AVX-length vectors, build the individual 128-bit pieces and use
5466 // shuffles to put them in place.
Craig Topper7a9a28b2012-08-12 02:23:29 +00005467 if (VT.is256BitVector()) {
Bruno Cardoso Lopes6683efb2011-07-22 00:15:07 +00005468 SmallVector<SDValue, 32> V;
Craig Topperfa5b70e2012-02-03 06:32:21 +00005469 for (unsigned i = 0; i != NumElems; ++i)
Bruno Cardoso Lopes6683efb2011-07-22 00:15:07 +00005470 V.push_back(Op.getOperand(i));
5471
5472 EVT HVT = EVT::getVectorVT(*DAG.getContext(), ExtVT, NumElems/2);
5473
5474 // Build both the lower and upper subvector.
5475 SDValue Lower = DAG.getNode(ISD::BUILD_VECTOR, dl, HVT, &V[0], NumElems/2);
5476 SDValue Upper = DAG.getNode(ISD::BUILD_VECTOR, dl, HVT, &V[NumElems / 2],
5477 NumElems/2);
5478
5479 // Recreate the wider vector with the lower and upper part.
Craig Topper4c7972d2012-04-22 18:15:59 +00005480 return Concat128BitVectors(Lower, Upper, VT, NumElems, DAG, dl);
Bruno Cardoso Lopes6683efb2011-07-22 00:15:07 +00005481 }
5482
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00005483 // Let legalizer expand 2-wide build_vectors.
Evan Cheng7e2ff772008-05-08 00:57:18 +00005484 if (EVTBits == 64) {
5485 if (NumNonZero == 1) {
5486 // One half is zero or undef.
5487 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dale Johannesenace16102009-02-03 19:33:06 +00005488 SDValue V2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT,
Evan Cheng7e2ff772008-05-08 00:57:18 +00005489 Op.getOperand(Idx));
Craig Topper12216172012-01-13 08:12:35 +00005490 return getShuffleVectorZeroOrUndef(V2, Idx, true, Subtarget, DAG);
Evan Cheng7e2ff772008-05-08 00:57:18 +00005491 }
Dan Gohman475871a2008-07-27 21:46:04 +00005492 return SDValue();
Evan Cheng7e2ff772008-05-08 00:57:18 +00005493 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00005494
5495 // If element VT is < 32 bits, convert it to inserts into a zero vector.
Bill Wendling826f36f2007-03-28 00:57:11 +00005496 if (EVTBits == 8 && NumElems == 16) {
Dan Gohman475871a2008-07-27 21:46:04 +00005497 SDValue V = LowerBuildVectorv16i8(Op, NonZeros,NumNonZero,NumZero, DAG,
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00005498 Subtarget, *this);
Gabor Greifba36cb52008-08-28 21:40:38 +00005499 if (V.getNode()) return V;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005500 }
5501
Bill Wendling826f36f2007-03-28 00:57:11 +00005502 if (EVTBits == 16 && NumElems == 8) {
Dan Gohman475871a2008-07-27 21:46:04 +00005503 SDValue V = LowerBuildVectorv8i16(Op, NonZeros,NumNonZero,NumZero, DAG,
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00005504 Subtarget, *this);
Gabor Greifba36cb52008-08-28 21:40:38 +00005505 if (V.getNode()) return V;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005506 }
5507
5508 // If element VT is == 32 bits, turn it into a number of shuffles.
Benjamin Kramer9c683542012-01-30 15:16:21 +00005509 SmallVector<SDValue, 8> V(NumElems);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005510 if (NumElems == 4 && NumZero > 0) {
5511 for (unsigned i = 0; i < 4; ++i) {
5512 bool isZero = !(NonZeros & (1 << i));
5513 if (isZero)
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00005514 V[i] = getZeroVector(VT, Subtarget, DAG, dl);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005515 else
Dale Johannesenace16102009-02-03 19:33:06 +00005516 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(i));
Evan Cheng0db9fe62006-04-25 20:13:52 +00005517 }
5518
5519 for (unsigned i = 0; i < 2; ++i) {
5520 switch ((NonZeros & (0x3 << i*2)) >> (i*2)) {
5521 default: break;
5522 case 0:
5523 V[i] = V[i*2]; // Must be a zero vector.
5524 break;
5525 case 1:
Nate Begeman9008ca62009-04-27 18:41:29 +00005526 V[i] = getMOVL(DAG, dl, VT, V[i*2+1], V[i*2]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005527 break;
5528 case 2:
Nate Begeman9008ca62009-04-27 18:41:29 +00005529 V[i] = getMOVL(DAG, dl, VT, V[i*2], V[i*2+1]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005530 break;
5531 case 3:
Nate Begeman9008ca62009-04-27 18:41:29 +00005532 V[i] = getUnpackl(DAG, dl, VT, V[i*2], V[i*2+1]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005533 break;
5534 }
5535 }
5536
Benjamin Kramer9c683542012-01-30 15:16:21 +00005537 bool Reverse1 = (NonZeros & 0x3) == 2;
5538 bool Reverse2 = ((NonZeros & (0x3 << 2)) >> 2) == 2;
5539 int MaskVec[] = {
5540 Reverse1 ? 1 : 0,
5541 Reverse1 ? 0 : 1,
Benjamin Kramer630ecf02012-01-30 20:01:35 +00005542 static_cast<int>(Reverse2 ? NumElems+1 : NumElems),
5543 static_cast<int>(Reverse2 ? NumElems : NumElems+1)
Benjamin Kramer9c683542012-01-30 15:16:21 +00005544 };
Nate Begeman9008ca62009-04-27 18:41:29 +00005545 return DAG.getVectorShuffle(VT, dl, V[0], V[1], &MaskVec[0]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005546 }
5547
Craig Topper7a9a28b2012-08-12 02:23:29 +00005548 if (Values.size() > 1 && VT.is128BitVector()) {
Nate Begemanfdea31a2010-03-24 20:49:50 +00005549 // Check for a build vector of consecutive loads.
5550 for (unsigned i = 0; i < NumElems; ++i)
5551 V[i] = Op.getOperand(i);
Michael J. Spencerec38de22010-10-10 22:04:20 +00005552
Nate Begemanfdea31a2010-03-24 20:49:50 +00005553 // Check for elements which are consecutive loads.
5554 SDValue LD = EltsFromConsecutiveLoads(VT, V, dl, DAG);
5555 if (LD.getNode())
5556 return LD;
Michael J. Spencerec38de22010-10-10 22:04:20 +00005557
Michael Liaofacace82012-10-19 17:15:18 +00005558 // Check for a build vector from mostly shuffle plus few inserting.
5559 SDValue Sh = buildFromShuffleMostly(Op, DAG);
5560 if (Sh.getNode())
5561 return Sh;
5562
Michael J. Spencerec38de22010-10-10 22:04:20 +00005563 // For SSE 4.1, use insertps to put the high elements into the low element.
Craig Topperd0a31172012-01-10 06:37:29 +00005564 if (getSubtarget()->hasSSE41()) {
Chris Lattner24faf612010-08-28 17:59:08 +00005565 SDValue Result;
5566 if (Op.getOperand(0).getOpcode() != ISD::UNDEF)
5567 Result = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(0));
5568 else
5569 Result = DAG.getUNDEF(VT);
Michael J. Spencerec38de22010-10-10 22:04:20 +00005570
Chris Lattner24faf612010-08-28 17:59:08 +00005571 for (unsigned i = 1; i < NumElems; ++i) {
5572 if (Op.getOperand(i).getOpcode() == ISD::UNDEF) continue;
5573 Result = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, Result,
Nate Begeman9008ca62009-04-27 18:41:29 +00005574 Op.getOperand(i), DAG.getIntPtrConstant(i));
Chris Lattner24faf612010-08-28 17:59:08 +00005575 }
5576 return Result;
Nate Begeman9008ca62009-04-27 18:41:29 +00005577 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00005578
Chris Lattner6e80e442010-08-28 17:15:43 +00005579 // Otherwise, expand into a number of unpckl*, start by extending each of
5580 // our (non-undef) elements to the full vector width with the element in the
5581 // bottom slot of the vector (which generates no code for SSE).
5582 for (unsigned i = 0; i < NumElems; ++i) {
5583 if (Op.getOperand(i).getOpcode() != ISD::UNDEF)
5584 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(i));
5585 else
5586 V[i] = DAG.getUNDEF(VT);
5587 }
5588
5589 // Next, we iteratively mix elements, e.g. for v4f32:
Evan Cheng0db9fe62006-04-25 20:13:52 +00005590 // Step 1: unpcklps 0, 2 ==> X: <?, ?, 2, 0>
5591 // : unpcklps 1, 3 ==> Y: <?, ?, 3, 1>
5592 // Step 2: unpcklps X, Y ==> <3, 2, 1, 0>
Chris Lattner6e80e442010-08-28 17:15:43 +00005593 unsigned EltStride = NumElems >> 1;
5594 while (EltStride != 0) {
Chris Lattner3ddcc432010-08-28 17:28:30 +00005595 for (unsigned i = 0; i < EltStride; ++i) {
5596 // If V[i+EltStride] is undef and this is the first round of mixing,
5597 // then it is safe to just drop this shuffle: V[i] is already in the
5598 // right place, the one element (since it's the first round) being
5599 // inserted as undef can be dropped. This isn't safe for successive
5600 // rounds because they will permute elements within both vectors.
5601 if (V[i+EltStride].getOpcode() == ISD::UNDEF &&
5602 EltStride == NumElems/2)
5603 continue;
Michael J. Spencerec38de22010-10-10 22:04:20 +00005604
Chris Lattner6e80e442010-08-28 17:15:43 +00005605 V[i] = getUnpackl(DAG, dl, VT, V[i], V[i + EltStride]);
Chris Lattner3ddcc432010-08-28 17:28:30 +00005606 }
Chris Lattner6e80e442010-08-28 17:15:43 +00005607 EltStride >>= 1;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005608 }
5609 return V[0];
5610 }
Dan Gohman475871a2008-07-27 21:46:04 +00005611 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005612}
5613
Bruno Cardoso Lopes8af24512011-08-01 21:54:02 +00005614// LowerAVXCONCAT_VECTORS - 256-bit AVX can use the vinsertf128 instruction
5615// to create 256-bit vectors from two other 128-bit ones.
5616static SDValue LowerAVXCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) {
5617 DebugLoc dl = Op.getDebugLoc();
5618 EVT ResVT = Op.getValueType();
5619
Craig Topper7a9a28b2012-08-12 02:23:29 +00005620 assert(ResVT.is256BitVector() && "Value type must be 256-bit wide");
Bruno Cardoso Lopes8af24512011-08-01 21:54:02 +00005621
5622 SDValue V1 = Op.getOperand(0);
5623 SDValue V2 = Op.getOperand(1);
5624 unsigned NumElems = ResVT.getVectorNumElements();
5625
Craig Topper4c7972d2012-04-22 18:15:59 +00005626 return Concat128BitVectors(V1, V2, ResVT, NumElems, DAG, dl);
Bruno Cardoso Lopes8af24512011-08-01 21:54:02 +00005627}
5628
Craig Topper55b24052012-09-11 06:15:32 +00005629static SDValue LowerCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) {
Bruno Cardoso Lopes8af24512011-08-01 21:54:02 +00005630 assert(Op.getNumOperands() == 2);
Bruno Cardoso Lopes8af24512011-08-01 21:54:02 +00005631
5632 // 256-bit AVX can use the vinsertf128 instruction to create 256-bit vectors
5633 // from two other 128-bit ones.
5634 return LowerAVXCONCAT_VECTORS(Op, DAG);
5635}
5636
Nadav Roteme80aa7c2012-04-09 08:33:21 +00005637// Try to lower a shuffle node into a simple blend instruction.
Craig Topper55b24052012-09-11 06:15:32 +00005638static SDValue
5639LowerVECTOR_SHUFFLEtoBlend(ShuffleVectorSDNode *SVOp,
5640 const X86Subtarget *Subtarget, SelectionDAG &DAG) {
Nadav Roteme80aa7c2012-04-09 08:33:21 +00005641 SDValue V1 = SVOp->getOperand(0);
5642 SDValue V2 = SVOp->getOperand(1);
5643 DebugLoc dl = SVOp->getDebugLoc();
Craig Topper708e44f2012-04-23 07:36:33 +00005644 MVT VT = SVOp->getValueType(0).getSimpleVT();
Craig Topper1842ba02012-04-23 06:38:28 +00005645 unsigned NumElems = VT.getVectorNumElements();
Nadav Roteme80aa7c2012-04-09 08:33:21 +00005646
Nadav Roteme6113782012-04-11 06:40:27 +00005647 if (!Subtarget->hasSSE41())
Nadav Roteme80aa7c2012-04-09 08:33:21 +00005648 return SDValue();
5649
Craig Topper1842ba02012-04-23 06:38:28 +00005650 unsigned ISDNo = 0;
Nadav Roteme6113782012-04-11 06:40:27 +00005651 MVT OpTy;
5652
Craig Topper708e44f2012-04-23 07:36:33 +00005653 switch (VT.SimpleTy) {
Nadav Roteme6113782012-04-11 06:40:27 +00005654 default: return SDValue();
5655 case MVT::v8i16:
Craig Topper1842ba02012-04-23 06:38:28 +00005656 ISDNo = X86ISD::BLENDPW;
5657 OpTy = MVT::v8i16;
5658 break;
Nadav Roteme6113782012-04-11 06:40:27 +00005659 case MVT::v4i32:
5660 case MVT::v4f32:
Craig Topper1842ba02012-04-23 06:38:28 +00005661 ISDNo = X86ISD::BLENDPS;
5662 OpTy = MVT::v4f32;
5663 break;
Nadav Roteme6113782012-04-11 06:40:27 +00005664 case MVT::v2i64:
5665 case MVT::v2f64:
Craig Topper1842ba02012-04-23 06:38:28 +00005666 ISDNo = X86ISD::BLENDPD;
5667 OpTy = MVT::v2f64;
5668 break;
Nadav Roteme6113782012-04-11 06:40:27 +00005669 case MVT::v8i32:
5670 case MVT::v8f32:
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00005671 if (!Subtarget->hasFp256())
Craig Topper1842ba02012-04-23 06:38:28 +00005672 return SDValue();
5673 ISDNo = X86ISD::BLENDPS;
5674 OpTy = MVT::v8f32;
5675 break;
Nadav Roteme6113782012-04-11 06:40:27 +00005676 case MVT::v4i64:
5677 case MVT::v4f64:
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00005678 if (!Subtarget->hasFp256())
Craig Topper1842ba02012-04-23 06:38:28 +00005679 return SDValue();
5680 ISDNo = X86ISD::BLENDPD;
5681 OpTy = MVT::v4f64;
5682 break;
Nadav Roteme6113782012-04-11 06:40:27 +00005683 }
5684 assert(ISDNo && "Invalid Op Number");
5685
5686 unsigned MaskVals = 0;
Nadav Roteme80aa7c2012-04-09 08:33:21 +00005687
Craig Topper1842ba02012-04-23 06:38:28 +00005688 for (unsigned i = 0; i != NumElems; ++i) {
Nadav Roteme80aa7c2012-04-09 08:33:21 +00005689 int EltIdx = SVOp->getMaskElt(i);
Craig Topper1842ba02012-04-23 06:38:28 +00005690 if (EltIdx == (int)i || EltIdx < 0)
Nadav Roteme6113782012-04-11 06:40:27 +00005691 MaskVals |= (1<<i);
Craig Topper1842ba02012-04-23 06:38:28 +00005692 else if (EltIdx == (int)(i + NumElems))
Nadav Roteme6113782012-04-11 06:40:27 +00005693 continue; // Bit is set to zero;
Craig Topper1842ba02012-04-23 06:38:28 +00005694 else
5695 return SDValue();
Nadav Roteme80aa7c2012-04-09 08:33:21 +00005696 }
5697
Nadav Roteme6113782012-04-11 06:40:27 +00005698 V1 = DAG.getNode(ISD::BITCAST, dl, OpTy, V1);
5699 V2 = DAG.getNode(ISD::BITCAST, dl, OpTy, V2);
5700 SDValue Ret = DAG.getNode(ISDNo, dl, OpTy, V1, V2,
5701 DAG.getConstant(MaskVals, MVT::i32));
5702 return DAG.getNode(ISD::BITCAST, dl, VT, Ret);
Nadav Roteme80aa7c2012-04-09 08:33:21 +00005703}
5704
Nate Begemanb9a47b82009-02-23 08:49:38 +00005705// v8i16 shuffles - Prefer shuffles in the following order:
5706// 1. [all] pshuflw, pshufhw, optional move
5707// 2. [ssse3] 1 x pshufb
5708// 3. [ssse3] 2 x pshufb + 1 x por
5709// 4. [all] mov + pshuflw + pshufhw + N x (pextrw + pinsrw)
Craig Topper55b24052012-09-11 06:15:32 +00005710static SDValue
5711LowerVECTOR_SHUFFLEv8i16(SDValue Op, const X86Subtarget *Subtarget,
5712 SelectionDAG &DAG) {
Bruno Cardoso Lopesbf8154a2010-08-21 01:32:18 +00005713 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Nate Begeman9008ca62009-04-27 18:41:29 +00005714 SDValue V1 = SVOp->getOperand(0);
5715 SDValue V2 = SVOp->getOperand(1);
5716 DebugLoc dl = SVOp->getDebugLoc();
Nate Begemanb9a47b82009-02-23 08:49:38 +00005717 SmallVector<int, 8> MaskVals;
Evan Cheng14b32e12007-12-11 01:46:18 +00005718
Nate Begemanb9a47b82009-02-23 08:49:38 +00005719 // Determine if more than 1 of the words in each of the low and high quadwords
5720 // of the result come from the same quadword of one of the two inputs. Undef
5721 // mask values count as coming from any quadword, for better codegen.
Benjamin Kramer003fad92011-10-15 13:28:31 +00005722 unsigned LoQuad[] = { 0, 0, 0, 0 };
5723 unsigned HiQuad[] = { 0, 0, 0, 0 };
Benjamin Kramer699ddcb2012-02-06 12:06:18 +00005724 std::bitset<4> InputQuads;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005725 for (unsigned i = 0; i < 8; ++i) {
Benjamin Kramer003fad92011-10-15 13:28:31 +00005726 unsigned *Quad = i < 4 ? LoQuad : HiQuad;
Nate Begeman9008ca62009-04-27 18:41:29 +00005727 int EltIdx = SVOp->getMaskElt(i);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005728 MaskVals.push_back(EltIdx);
5729 if (EltIdx < 0) {
5730 ++Quad[0];
5731 ++Quad[1];
5732 ++Quad[2];
5733 ++Quad[3];
Evan Cheng14b32e12007-12-11 01:46:18 +00005734 continue;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005735 }
5736 ++Quad[EltIdx / 4];
5737 InputQuads.set(EltIdx / 4);
Evan Cheng14b32e12007-12-11 01:46:18 +00005738 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00005739
Nate Begemanb9a47b82009-02-23 08:49:38 +00005740 int BestLoQuad = -1;
Evan Cheng14b32e12007-12-11 01:46:18 +00005741 unsigned MaxQuad = 1;
5742 for (unsigned i = 0; i < 4; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005743 if (LoQuad[i] > MaxQuad) {
5744 BestLoQuad = i;
5745 MaxQuad = LoQuad[i];
Evan Cheng14b32e12007-12-11 01:46:18 +00005746 }
Evan Cheng8a86c3f2007-12-07 08:07:39 +00005747 }
5748
Nate Begemanb9a47b82009-02-23 08:49:38 +00005749 int BestHiQuad = -1;
Evan Cheng14b32e12007-12-11 01:46:18 +00005750 MaxQuad = 1;
5751 for (unsigned i = 0; i < 4; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005752 if (HiQuad[i] > MaxQuad) {
5753 BestHiQuad = i;
5754 MaxQuad = HiQuad[i];
Evan Cheng14b32e12007-12-11 01:46:18 +00005755 }
5756 }
5757
Nate Begemanb9a47b82009-02-23 08:49:38 +00005758 // For SSSE3, If all 8 words of the result come from only 1 quadword of each
Eric Christopherfd179292009-08-27 18:07:15 +00005759 // of the two input vectors, shuffle them into one input vector so only a
Nate Begemanb9a47b82009-02-23 08:49:38 +00005760 // single pshufb instruction is necessary. If There are more than 2 input
5761 // quads, disable the next transformation since it does not help SSSE3.
5762 bool V1Used = InputQuads[0] || InputQuads[1];
5763 bool V2Used = InputQuads[2] || InputQuads[3];
Craig Topperd0a31172012-01-10 06:37:29 +00005764 if (Subtarget->hasSSSE3()) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005765 if (InputQuads.count() == 2 && V1Used && V2Used) {
Benjamin Kramer699ddcb2012-02-06 12:06:18 +00005766 BestLoQuad = InputQuads[0] ? 0 : 1;
5767 BestHiQuad = InputQuads[2] ? 2 : 3;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005768 }
5769 if (InputQuads.count() > 2) {
5770 BestLoQuad = -1;
5771 BestHiQuad = -1;
5772 }
5773 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00005774
Nate Begemanb9a47b82009-02-23 08:49:38 +00005775 // If BestLoQuad or BestHiQuad are set, shuffle the quads together and update
5776 // the shuffle mask. If a quad is scored as -1, that means that it contains
5777 // words from all 4 input quadwords.
5778 SDValue NewV;
5779 if (BestLoQuad >= 0 || BestHiQuad >= 0) {
Benjamin Kramer9c683542012-01-30 15:16:21 +00005780 int MaskV[] = {
5781 BestLoQuad < 0 ? 0 : BestLoQuad,
5782 BestHiQuad < 0 ? 1 : BestHiQuad
5783 };
Eric Christopherfd179292009-08-27 18:07:15 +00005784 NewV = DAG.getVectorShuffle(MVT::v2i64, dl,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005785 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64, V1),
5786 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64, V2), &MaskV[0]);
5787 NewV = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, NewV);
Evan Cheng14b32e12007-12-11 01:46:18 +00005788
Nate Begemanb9a47b82009-02-23 08:49:38 +00005789 // Rewrite the MaskVals and assign NewV to V1 if NewV now contains all the
5790 // source words for the shuffle, to aid later transformations.
5791 bool AllWordsInNewV = true;
Mon P Wang37b9a192009-03-11 06:35:11 +00005792 bool InOrder[2] = { true, true };
Evan Cheng14b32e12007-12-11 01:46:18 +00005793 for (unsigned i = 0; i != 8; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005794 int idx = MaskVals[i];
Mon P Wang37b9a192009-03-11 06:35:11 +00005795 if (idx != (int)i)
5796 InOrder[i/4] = false;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005797 if (idx < 0 || (idx/4) == BestLoQuad || (idx/4) == BestHiQuad)
Evan Cheng14b32e12007-12-11 01:46:18 +00005798 continue;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005799 AllWordsInNewV = false;
5800 break;
Evan Cheng14b32e12007-12-11 01:46:18 +00005801 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00005802
Nate Begemanb9a47b82009-02-23 08:49:38 +00005803 bool pshuflw = AllWordsInNewV, pshufhw = AllWordsInNewV;
5804 if (AllWordsInNewV) {
5805 for (int i = 0; i != 8; ++i) {
5806 int idx = MaskVals[i];
5807 if (idx < 0)
Evan Cheng14b32e12007-12-11 01:46:18 +00005808 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00005809 idx = MaskVals[i] = (idx / 4) == BestLoQuad ? (idx & 3) : (idx & 3) + 4;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005810 if ((idx != i) && idx < 4)
5811 pshufhw = false;
5812 if ((idx != i) && idx > 3)
5813 pshuflw = false;
Evan Cheng14b32e12007-12-11 01:46:18 +00005814 }
Nate Begemanb9a47b82009-02-23 08:49:38 +00005815 V1 = NewV;
5816 V2Used = false;
5817 BestLoQuad = 0;
5818 BestHiQuad = 1;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00005819 }
Evan Cheng14b32e12007-12-11 01:46:18 +00005820
Nate Begemanb9a47b82009-02-23 08:49:38 +00005821 // If we've eliminated the use of V2, and the new mask is a pshuflw or
5822 // pshufhw, that's as cheap as it gets. Return the new shuffle.
Mon P Wang37b9a192009-03-11 06:35:11 +00005823 if ((pshufhw && InOrder[0]) || (pshuflw && InOrder[1])) {
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00005824 unsigned Opc = pshufhw ? X86ISD::PSHUFHW : X86ISD::PSHUFLW;
5825 unsigned TargetMask = 0;
5826 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV,
Owen Anderson825b72b2009-08-11 20:47:22 +00005827 DAG.getUNDEF(MVT::v8i16), &MaskVals[0]);
Craig Topperdd637ae2012-02-19 05:41:45 +00005828 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(NewV.getNode());
5829 TargetMask = pshufhw ? getShufflePSHUFHWImmediate(SVOp):
5830 getShufflePSHUFLWImmediate(SVOp);
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00005831 V1 = NewV.getOperand(0);
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00005832 return getTargetShuffleNode(Opc, dl, MVT::v8i16, V1, TargetMask, DAG);
Evan Cheng14b32e12007-12-11 01:46:18 +00005833 }
Evan Cheng14b32e12007-12-11 01:46:18 +00005834 }
Eric Christopherfd179292009-08-27 18:07:15 +00005835
Nate Begemanb9a47b82009-02-23 08:49:38 +00005836 // If we have SSSE3, and all words of the result are from 1 input vector,
5837 // case 2 is generated, otherwise case 3 is generated. If no SSSE3
5838 // is present, fall back to case 4.
Craig Topperd0a31172012-01-10 06:37:29 +00005839 if (Subtarget->hasSSSE3()) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005840 SmallVector<SDValue,16> pshufbMask;
Eric Christopherfd179292009-08-27 18:07:15 +00005841
Nate Begemanb9a47b82009-02-23 08:49:38 +00005842 // If we have elements from both input vectors, set the high bit of the
Eric Christopherfd179292009-08-27 18:07:15 +00005843 // shuffle mask element to zero out elements that come from V2 in the V1
Nate Begemanb9a47b82009-02-23 08:49:38 +00005844 // mask, and elements that come from V1 in the V2 mask, so that the two
5845 // results can be OR'd together.
5846 bool TwoInputs = V1Used && V2Used;
5847 for (unsigned i = 0; i != 8; ++i) {
5848 int EltIdx = MaskVals[i] * 2;
Craig Topperbe97ae92012-05-18 07:07:36 +00005849 int Idx0 = (TwoInputs && (EltIdx >= 16)) ? 0x80 : EltIdx;
5850 int Idx1 = (TwoInputs && (EltIdx >= 16)) ? 0x80 : EltIdx+1;
5851 pshufbMask.push_back(DAG.getConstant(Idx0, MVT::i8));
5852 pshufbMask.push_back(DAG.getConstant(Idx1, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005853 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005854 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, V1);
Eric Christopherfd179292009-08-27 18:07:15 +00005855 V1 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V1,
Evan Chenga87008d2009-02-25 22:49:59 +00005856 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005857 MVT::v16i8, &pshufbMask[0], 16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005858 if (!TwoInputs)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005859 return DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V1);
Eric Christopherfd179292009-08-27 18:07:15 +00005860
Nate Begemanb9a47b82009-02-23 08:49:38 +00005861 // Calculate the shuffle mask for the second input, shuffle it, and
5862 // OR it with the first shuffled input.
5863 pshufbMask.clear();
5864 for (unsigned i = 0; i != 8; ++i) {
5865 int EltIdx = MaskVals[i] * 2;
Craig Topperbe97ae92012-05-18 07:07:36 +00005866 int Idx0 = (EltIdx < 16) ? 0x80 : EltIdx - 16;
5867 int Idx1 = (EltIdx < 16) ? 0x80 : EltIdx - 15;
5868 pshufbMask.push_back(DAG.getConstant(Idx0, MVT::i8));
5869 pshufbMask.push_back(DAG.getConstant(Idx1, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005870 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005871 V2 = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, V2);
Eric Christopherfd179292009-08-27 18:07:15 +00005872 V2 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V2,
Evan Chenga87008d2009-02-25 22:49:59 +00005873 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005874 MVT::v16i8, &pshufbMask[0], 16));
5875 V1 = DAG.getNode(ISD::OR, dl, MVT::v16i8, V1, V2);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005876 return DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005877 }
5878
5879 // If BestLoQuad >= 0, generate a pshuflw to put the low elements in order,
5880 // and update MaskVals with new element order.
Benjamin Kramer9c683542012-01-30 15:16:21 +00005881 std::bitset<8> InOrder;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005882 if (BestLoQuad >= 0) {
Benjamin Kramer9c683542012-01-30 15:16:21 +00005883 int MaskV[] = { -1, -1, -1, -1, 4, 5, 6, 7 };
Nate Begemanb9a47b82009-02-23 08:49:38 +00005884 for (int i = 0; i != 4; ++i) {
5885 int idx = MaskVals[i];
5886 if (idx < 0) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005887 InOrder.set(i);
5888 } else if ((idx / 4) == BestLoQuad) {
Benjamin Kramer9c683542012-01-30 15:16:21 +00005889 MaskV[i] = idx & 3;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005890 InOrder.set(i);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005891 }
5892 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005893 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV, DAG.getUNDEF(MVT::v8i16),
Nate Begeman9008ca62009-04-27 18:41:29 +00005894 &MaskV[0]);
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00005895
Craig Topperdd637ae2012-02-19 05:41:45 +00005896 if (NewV.getOpcode() == ISD::VECTOR_SHUFFLE && Subtarget->hasSSSE3()) {
5897 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(NewV.getNode());
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00005898 NewV = getTargetShuffleNode(X86ISD::PSHUFLW, dl, MVT::v8i16,
Craig Topperdd637ae2012-02-19 05:41:45 +00005899 NewV.getOperand(0),
5900 getShufflePSHUFLWImmediate(SVOp), DAG);
5901 }
Nate Begemanb9a47b82009-02-23 08:49:38 +00005902 }
Eric Christopherfd179292009-08-27 18:07:15 +00005903
Nate Begemanb9a47b82009-02-23 08:49:38 +00005904 // If BestHi >= 0, generate a pshufhw to put the high elements in order,
5905 // and update MaskVals with the new element order.
5906 if (BestHiQuad >= 0) {
Benjamin Kramer9c683542012-01-30 15:16:21 +00005907 int MaskV[] = { 0, 1, 2, 3, -1, -1, -1, -1 };
Nate Begemanb9a47b82009-02-23 08:49:38 +00005908 for (unsigned i = 4; i != 8; ++i) {
5909 int idx = MaskVals[i];
5910 if (idx < 0) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005911 InOrder.set(i);
5912 } else if ((idx / 4) == BestHiQuad) {
Benjamin Kramer9c683542012-01-30 15:16:21 +00005913 MaskV[i] = (idx & 3) + 4;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005914 InOrder.set(i);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005915 }
5916 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005917 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV, DAG.getUNDEF(MVT::v8i16),
Nate Begeman9008ca62009-04-27 18:41:29 +00005918 &MaskV[0]);
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00005919
Craig Topperdd637ae2012-02-19 05:41:45 +00005920 if (NewV.getOpcode() == ISD::VECTOR_SHUFFLE && Subtarget->hasSSSE3()) {
5921 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(NewV.getNode());
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00005922 NewV = getTargetShuffleNode(X86ISD::PSHUFHW, dl, MVT::v8i16,
Craig Topperdd637ae2012-02-19 05:41:45 +00005923 NewV.getOperand(0),
5924 getShufflePSHUFHWImmediate(SVOp), DAG);
5925 }
Nate Begemanb9a47b82009-02-23 08:49:38 +00005926 }
Eric Christopherfd179292009-08-27 18:07:15 +00005927
Nate Begemanb9a47b82009-02-23 08:49:38 +00005928 // In case BestHi & BestLo were both -1, which means each quadword has a word
5929 // from each of the four input quadwords, calculate the InOrder bitvector now
5930 // before falling through to the insert/extract cleanup.
5931 if (BestLoQuad == -1 && BestHiQuad == -1) {
5932 NewV = V1;
5933 for (int i = 0; i != 8; ++i)
5934 if (MaskVals[i] < 0 || MaskVals[i] == i)
5935 InOrder.set(i);
5936 }
Eric Christopherfd179292009-08-27 18:07:15 +00005937
Nate Begemanb9a47b82009-02-23 08:49:38 +00005938 // The other elements are put in the right place using pextrw and pinsrw.
5939 for (unsigned i = 0; i != 8; ++i) {
5940 if (InOrder[i])
5941 continue;
5942 int EltIdx = MaskVals[i];
5943 if (EltIdx < 0)
5944 continue;
Craig Topper6643d9c2012-05-04 06:18:33 +00005945 SDValue ExtOp = (EltIdx < 8) ?
5946 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, V1,
5947 DAG.getIntPtrConstant(EltIdx)) :
5948 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, V2,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005949 DAG.getIntPtrConstant(EltIdx - 8));
Owen Anderson825b72b2009-08-11 20:47:22 +00005950 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, ExtOp,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005951 DAG.getIntPtrConstant(i));
5952 }
5953 return NewV;
5954}
5955
5956// v16i8 shuffles - Prefer shuffles in the following order:
5957// 1. [ssse3] 1 x pshufb
5958// 2. [ssse3] 2 x pshufb + 1 x por
5959// 3. [all] v8i16 shuffle + N x pextrw + rotate + pinsrw
5960static
Nate Begeman9008ca62009-04-27 18:41:29 +00005961SDValue LowerVECTOR_SHUFFLEv16i8(ShuffleVectorSDNode *SVOp,
Dan Gohmand858e902010-04-17 15:26:15 +00005962 SelectionDAG &DAG,
5963 const X86TargetLowering &TLI) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005964 SDValue V1 = SVOp->getOperand(0);
5965 SDValue V2 = SVOp->getOperand(1);
5966 DebugLoc dl = SVOp->getDebugLoc();
Benjamin Kramered4c8c62012-01-15 13:16:05 +00005967 ArrayRef<int> MaskVals = SVOp->getMask();
Eric Christopherfd179292009-08-27 18:07:15 +00005968
Nate Begemanb9a47b82009-02-23 08:49:38 +00005969 // If we have SSSE3, case 1 is generated when all result bytes come from
Eric Christopherfd179292009-08-27 18:07:15 +00005970 // one of the inputs. Otherwise, case 2 is generated. If no SSSE3 is
Nate Begemanb9a47b82009-02-23 08:49:38 +00005971 // present, fall back to case 3.
Eric Christopherfd179292009-08-27 18:07:15 +00005972
Nate Begemanb9a47b82009-02-23 08:49:38 +00005973 // If SSSE3, use 1 pshufb instruction per vector with elements in the result.
Craig Topperd0a31172012-01-10 06:37:29 +00005974 if (TLI.getSubtarget()->hasSSSE3()) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005975 SmallVector<SDValue,16> pshufbMask;
Eric Christopherfd179292009-08-27 18:07:15 +00005976
Nate Begemanb9a47b82009-02-23 08:49:38 +00005977 // If all result elements are from one input vector, then only translate
Eric Christopherfd179292009-08-27 18:07:15 +00005978 // undef mask values to 0x80 (zero out result) in the pshufb mask.
Nate Begemanb9a47b82009-02-23 08:49:38 +00005979 //
5980 // Otherwise, we have elements from both input vectors, and must zero out
5981 // elements that come from V2 in the first mask, and V1 in the second mask
5982 // so that we can OR them together.
Nate Begemanb9a47b82009-02-23 08:49:38 +00005983 for (unsigned i = 0; i != 16; ++i) {
5984 int EltIdx = MaskVals[i];
Craig Topperb82b5ab2012-05-18 06:42:06 +00005985 if (EltIdx < 0 || EltIdx >= 16)
5986 EltIdx = 0x80;
Owen Anderson825b72b2009-08-11 20:47:22 +00005987 pshufbMask.push_back(DAG.getConstant(EltIdx, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005988 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005989 V1 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V1,
Evan Chenga87008d2009-02-25 22:49:59 +00005990 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005991 MVT::v16i8, &pshufbMask[0], 16));
Michael Liao265bcb12012-08-31 20:12:31 +00005992
5993 // As PSHUFB will zero elements with negative indices, it's safe to ignore
5994 // the 2nd operand if it's undefined or zero.
5995 if (V2.getOpcode() == ISD::UNDEF ||
5996 ISD::isBuildVectorAllZeros(V2.getNode()))
Nate Begemanb9a47b82009-02-23 08:49:38 +00005997 return V1;
Eric Christopherfd179292009-08-27 18:07:15 +00005998
Nate Begemanb9a47b82009-02-23 08:49:38 +00005999 // Calculate the shuffle mask for the second input, shuffle it, and
6000 // OR it with the first shuffled input.
6001 pshufbMask.clear();
6002 for (unsigned i = 0; i != 16; ++i) {
6003 int EltIdx = MaskVals[i];
Craig Topperb82b5ab2012-05-18 06:42:06 +00006004 EltIdx = (EltIdx < 16) ? 0x80 : EltIdx - 16;
Craig Topper85b9e562012-05-22 06:09:38 +00006005 pshufbMask.push_back(DAG.getConstant(EltIdx, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00006006 }
Owen Anderson825b72b2009-08-11 20:47:22 +00006007 V2 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V2,
Evan Chenga87008d2009-02-25 22:49:59 +00006008 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00006009 MVT::v16i8, &pshufbMask[0], 16));
6010 return DAG.getNode(ISD::OR, dl, MVT::v16i8, V1, V2);
Nate Begemanb9a47b82009-02-23 08:49:38 +00006011 }
Eric Christopherfd179292009-08-27 18:07:15 +00006012
Nate Begemanb9a47b82009-02-23 08:49:38 +00006013 // No SSSE3 - Calculate in place words and then fix all out of place words
6014 // With 0-16 extracts & inserts. Worst case is 16 bytes out of order from
6015 // the 16 different words that comprise the two doublequadword input vectors.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006016 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V1);
6017 V2 = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V2);
Craig Topperb82b5ab2012-05-18 06:42:06 +00006018 SDValue NewV = V1;
Nate Begemanb9a47b82009-02-23 08:49:38 +00006019 for (int i = 0; i != 8; ++i) {
6020 int Elt0 = MaskVals[i*2];
6021 int Elt1 = MaskVals[i*2+1];
Eric Christopherfd179292009-08-27 18:07:15 +00006022
Nate Begemanb9a47b82009-02-23 08:49:38 +00006023 // This word of the result is all undef, skip it.
6024 if (Elt0 < 0 && Elt1 < 0)
6025 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00006026
Nate Begemanb9a47b82009-02-23 08:49:38 +00006027 // This word of the result is already in the correct place, skip it.
Craig Topperb82b5ab2012-05-18 06:42:06 +00006028 if ((Elt0 == i*2) && (Elt1 == i*2+1))
Nate Begemanb9a47b82009-02-23 08:49:38 +00006029 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00006030
Nate Begemanb9a47b82009-02-23 08:49:38 +00006031 SDValue Elt0Src = Elt0 < 16 ? V1 : V2;
6032 SDValue Elt1Src = Elt1 < 16 ? V1 : V2;
6033 SDValue InsElt;
Mon P Wang6b3ef692009-03-11 18:47:57 +00006034
6035 // If Elt0 and Elt1 are defined, are consecutive, and can be load
6036 // using a single extract together, load it and store it.
6037 if ((Elt0 >= 0) && ((Elt0 + 1) == Elt1) && ((Elt0 & 1) == 0)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00006038 InsElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, Elt1Src,
Mon P Wang6b3ef692009-03-11 18:47:57 +00006039 DAG.getIntPtrConstant(Elt1 / 2));
Owen Anderson825b72b2009-08-11 20:47:22 +00006040 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, InsElt,
Mon P Wang6b3ef692009-03-11 18:47:57 +00006041 DAG.getIntPtrConstant(i));
6042 continue;
6043 }
6044
Nate Begemanb9a47b82009-02-23 08:49:38 +00006045 // If Elt1 is defined, extract it from the appropriate source. If the
Mon P Wang6b3ef692009-03-11 18:47:57 +00006046 // source byte is not also odd, shift the extracted word left 8 bits
6047 // otherwise clear the bottom 8 bits if we need to do an or.
Nate Begemanb9a47b82009-02-23 08:49:38 +00006048 if (Elt1 >= 0) {
Owen Anderson825b72b2009-08-11 20:47:22 +00006049 InsElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, Elt1Src,
Nate Begemanb9a47b82009-02-23 08:49:38 +00006050 DAG.getIntPtrConstant(Elt1 / 2));
6051 if ((Elt1 & 1) == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00006052 InsElt = DAG.getNode(ISD::SHL, dl, MVT::i16, InsElt,
Owen Anderson95771af2011-02-25 21:41:48 +00006053 DAG.getConstant(8,
6054 TLI.getShiftAmountTy(InsElt.getValueType())));
Mon P Wang6b3ef692009-03-11 18:47:57 +00006055 else if (Elt0 >= 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00006056 InsElt = DAG.getNode(ISD::AND, dl, MVT::i16, InsElt,
6057 DAG.getConstant(0xFF00, MVT::i16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00006058 }
6059 // If Elt0 is defined, extract it from the appropriate source. If the
6060 // source byte is not also even, shift the extracted word right 8 bits. If
6061 // Elt1 was also defined, OR the extracted values together before
6062 // inserting them in the result.
6063 if (Elt0 >= 0) {
Owen Anderson825b72b2009-08-11 20:47:22 +00006064 SDValue InsElt0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16,
Nate Begemanb9a47b82009-02-23 08:49:38 +00006065 Elt0Src, DAG.getIntPtrConstant(Elt0 / 2));
6066 if ((Elt0 & 1) != 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00006067 InsElt0 = DAG.getNode(ISD::SRL, dl, MVT::i16, InsElt0,
Owen Anderson95771af2011-02-25 21:41:48 +00006068 DAG.getConstant(8,
6069 TLI.getShiftAmountTy(InsElt0.getValueType())));
Mon P Wang6b3ef692009-03-11 18:47:57 +00006070 else if (Elt1 >= 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00006071 InsElt0 = DAG.getNode(ISD::AND, dl, MVT::i16, InsElt0,
6072 DAG.getConstant(0x00FF, MVT::i16));
6073 InsElt = Elt1 >= 0 ? DAG.getNode(ISD::OR, dl, MVT::i16, InsElt, InsElt0)
Nate Begemanb9a47b82009-02-23 08:49:38 +00006074 : InsElt0;
6075 }
Owen Anderson825b72b2009-08-11 20:47:22 +00006076 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, InsElt,
Nate Begemanb9a47b82009-02-23 08:49:38 +00006077 DAG.getIntPtrConstant(i));
6078 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006079 return DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, NewV);
Evan Cheng14b32e12007-12-11 01:46:18 +00006080}
6081
Elena Demikhovsky41789462012-09-06 12:42:01 +00006082// v32i8 shuffles - Translate to VPSHUFB if possible.
6083static
6084SDValue LowerVECTOR_SHUFFLEv32i8(ShuffleVectorSDNode *SVOp,
Craig Topper55b24052012-09-11 06:15:32 +00006085 const X86Subtarget *Subtarget,
6086 SelectionDAG &DAG) {
Elena Demikhovsky41789462012-09-06 12:42:01 +00006087 EVT VT = SVOp->getValueType(0);
6088 SDValue V1 = SVOp->getOperand(0);
6089 SDValue V2 = SVOp->getOperand(1);
6090 DebugLoc dl = SVOp->getDebugLoc();
Elena Demikhovsky8100d242012-09-10 12:13:11 +00006091 SmallVector<int, 32> MaskVals(SVOp->getMask().begin(), SVOp->getMask().end());
Elena Demikhovsky41789462012-09-06 12:42:01 +00006092
6093 bool V2IsUndef = V2.getOpcode() == ISD::UNDEF;
Elena Demikhovsky8100d242012-09-10 12:13:11 +00006094 bool V1IsAllZero = ISD::isBuildVectorAllZeros(V1.getNode());
6095 bool V2IsAllZero = ISD::isBuildVectorAllZeros(V2.getNode());
Elena Demikhovsky41789462012-09-06 12:42:01 +00006096
Michael Liao471b9172012-10-03 23:43:52 +00006097 // VPSHUFB may be generated if
Elena Demikhovsky8100d242012-09-10 12:13:11 +00006098 // (1) one of input vector is undefined or zeroinitializer.
6099 // The mask value 0x80 puts 0 in the corresponding slot of the vector.
6100 // And (2) the mask indexes don't cross the 128-bit lane.
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00006101 if (VT != MVT::v32i8 || !Subtarget->hasInt256() ||
Elena Demikhovsky8100d242012-09-10 12:13:11 +00006102 (!V2IsUndef && !V2IsAllZero && !V1IsAllZero))
Elena Demikhovsky41789462012-09-06 12:42:01 +00006103 return SDValue();
6104
Elena Demikhovsky8100d242012-09-10 12:13:11 +00006105 if (V1IsAllZero && !V2IsAllZero) {
6106 CommuteVectorShuffleMask(MaskVals, 32);
6107 V1 = V2;
6108 }
6109 SmallVector<SDValue, 32> pshufbMask;
Elena Demikhovsky41789462012-09-06 12:42:01 +00006110 for (unsigned i = 0; i != 32; i++) {
6111 int EltIdx = MaskVals[i];
6112 if (EltIdx < 0 || EltIdx >= 32)
6113 EltIdx = 0x80;
6114 else {
6115 if ((EltIdx >= 16 && i < 16) || (EltIdx < 16 && i >= 16))
6116 // Cross lane is not allowed.
6117 return SDValue();
6118 EltIdx &= 0xf;
6119 }
6120 pshufbMask.push_back(DAG.getConstant(EltIdx, MVT::i8));
6121 }
6122 return DAG.getNode(X86ISD::PSHUFB, dl, MVT::v32i8, V1,
6123 DAG.getNode(ISD::BUILD_VECTOR, dl,
6124 MVT::v32i8, &pshufbMask[0], 32));
6125}
6126
Evan Cheng7a831ce2007-12-15 03:00:47 +00006127/// RewriteAsNarrowerShuffle - Try rewriting v8i16 and v16i8 shuffles as 4 wide
Bruno Cardoso Lopes0a7dd4f2010-09-08 18:12:31 +00006128/// ones, or rewriting v4i32 / v4f32 as 2 wide ones if possible. This can be
Evan Cheng7a831ce2007-12-15 03:00:47 +00006129/// done when every pair / quad of shuffle mask elements point to elements in
6130/// the right sequence. e.g.
Bruno Cardoso Lopes0a7dd4f2010-09-08 18:12:31 +00006131/// vector_shuffle X, Y, <2, 3, | 10, 11, | 0, 1, | 14, 15>
Evan Cheng14b32e12007-12-11 01:46:18 +00006132static
Nate Begeman9008ca62009-04-27 18:41:29 +00006133SDValue RewriteAsNarrowerShuffle(ShuffleVectorSDNode *SVOp,
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006134 SelectionDAG &DAG, DebugLoc dl) {
Craig Topper11ac1f82012-05-04 04:08:44 +00006135 MVT VT = SVOp->getValueType(0).getSimpleVT();
Nate Begeman9008ca62009-04-27 18:41:29 +00006136 unsigned NumElems = VT.getVectorNumElements();
Craig Topper11ac1f82012-05-04 04:08:44 +00006137 MVT NewVT;
6138 unsigned Scale;
6139 switch (VT.SimpleTy) {
Craig Topperabb94d02012-02-05 03:43:23 +00006140 default: llvm_unreachable("Unexpected!");
Craig Topperf3640d72012-05-04 04:44:49 +00006141 case MVT::v4f32: NewVT = MVT::v2f64; Scale = 2; break;
6142 case MVT::v4i32: NewVT = MVT::v2i64; Scale = 2; break;
6143 case MVT::v8i16: NewVT = MVT::v4i32; Scale = 2; break;
6144 case MVT::v16i8: NewVT = MVT::v4i32; Scale = 4; break;
6145 case MVT::v16i16: NewVT = MVT::v8i32; Scale = 2; break;
6146 case MVT::v32i8: NewVT = MVT::v8i32; Scale = 4; break;
Evan Cheng7a831ce2007-12-15 03:00:47 +00006147 }
6148
Nate Begeman9008ca62009-04-27 18:41:29 +00006149 SmallVector<int, 8> MaskVec;
Craig Topper11ac1f82012-05-04 04:08:44 +00006150 for (unsigned i = 0; i != NumElems; i += Scale) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006151 int StartIdx = -1;
Craig Topper11ac1f82012-05-04 04:08:44 +00006152 for (unsigned j = 0; j != Scale; ++j) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006153 int EltIdx = SVOp->getMaskElt(i+j);
6154 if (EltIdx < 0)
Evan Cheng14b32e12007-12-11 01:46:18 +00006155 continue;
Craig Topper11ac1f82012-05-04 04:08:44 +00006156 if (StartIdx < 0)
6157 StartIdx = (EltIdx / Scale);
6158 if (EltIdx != (int)(StartIdx*Scale + j))
Dan Gohman475871a2008-07-27 21:46:04 +00006159 return SDValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00006160 }
Craig Topper11ac1f82012-05-04 04:08:44 +00006161 MaskVec.push_back(StartIdx);
Evan Cheng8a86c3f2007-12-07 08:07:39 +00006162 }
6163
Craig Topper11ac1f82012-05-04 04:08:44 +00006164 SDValue V1 = DAG.getNode(ISD::BITCAST, dl, NewVT, SVOp->getOperand(0));
6165 SDValue V2 = DAG.getNode(ISD::BITCAST, dl, NewVT, SVOp->getOperand(1));
Nate Begeman9008ca62009-04-27 18:41:29 +00006166 return DAG.getVectorShuffle(NewVT, dl, V1, V2, &MaskVec[0]);
Evan Cheng8a86c3f2007-12-07 08:07:39 +00006167}
6168
Evan Chengd880b972008-05-09 21:53:03 +00006169/// getVZextMovL - Return a zero-extending vector move low node.
Evan Cheng7e2ff772008-05-08 00:57:18 +00006170///
Owen Andersone50ed302009-08-10 22:56:29 +00006171static SDValue getVZextMovL(EVT VT, EVT OpVT,
Nate Begeman9008ca62009-04-27 18:41:29 +00006172 SDValue SrcOp, SelectionDAG &DAG,
6173 const X86Subtarget *Subtarget, DebugLoc dl) {
Owen Anderson825b72b2009-08-11 20:47:22 +00006174 if (VT == MVT::v2f64 || VT == MVT::v4f32) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00006175 LoadSDNode *LD = NULL;
Gabor Greifba36cb52008-08-28 21:40:38 +00006176 if (!isScalarLoadToVector(SrcOp.getNode(), &LD))
Evan Cheng7e2ff772008-05-08 00:57:18 +00006177 LD = dyn_cast<LoadSDNode>(SrcOp);
6178 if (!LD) {
6179 // movssrr and movsdrr do not clear top bits. Try to use movd, movq
6180 // instead.
Owen Anderson766b5ef2009-08-11 21:59:30 +00006181 MVT ExtVT = (OpVT == MVT::v2f64) ? MVT::i64 : MVT::i32;
Duncan Sandscdfad362010-11-03 12:17:33 +00006182 if ((ExtVT != MVT::i64 || Subtarget->is64Bit()) &&
Evan Cheng7e2ff772008-05-08 00:57:18 +00006183 SrcOp.getOpcode() == ISD::SCALAR_TO_VECTOR &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006184 SrcOp.getOperand(0).getOpcode() == ISD::BITCAST &&
Owen Anderson766b5ef2009-08-11 21:59:30 +00006185 SrcOp.getOperand(0).getOperand(0).getValueType() == ExtVT) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00006186 // PR2108
Owen Anderson825b72b2009-08-11 20:47:22 +00006187 OpVT = (OpVT == MVT::v2f64) ? MVT::v2i64 : MVT::v4i32;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006188 return DAG.getNode(ISD::BITCAST, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00006189 DAG.getNode(X86ISD::VZEXT_MOVL, dl, OpVT,
6190 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
6191 OpVT,
Gabor Greif327ef032008-08-28 23:19:51 +00006192 SrcOp.getOperand(0)
6193 .getOperand(0))));
Evan Cheng7e2ff772008-05-08 00:57:18 +00006194 }
6195 }
6196 }
6197
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006198 return DAG.getNode(ISD::BITCAST, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00006199 DAG.getNode(X86ISD::VZEXT_MOVL, dl, OpVT,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006200 DAG.getNode(ISD::BITCAST, dl,
Dale Johannesenace16102009-02-03 19:33:06 +00006201 OpVT, SrcOp)));
Evan Cheng7e2ff772008-05-08 00:57:18 +00006202}
6203
Bruno Cardoso Lopesd0888342011-07-22 00:14:56 +00006204/// LowerVECTOR_SHUFFLE_256 - Handle all 256-bit wide vectors shuffles
6205/// which could not be matched by any known target speficic shuffle
6206static SDValue
6207LowerVECTOR_SHUFFLE_256(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG) {
Elena Demikhovsky15963732012-06-26 08:04:10 +00006208
6209 SDValue NewOp = Compact8x32ShuffleNode(SVOp, DAG);
6210 if (NewOp.getNode())
6211 return NewOp;
6212
Craig Topper8f35c132012-01-20 09:29:03 +00006213 EVT VT = SVOp->getValueType(0);
Bruno Cardoso Lopes3b865982011-08-16 18:21:54 +00006214
Craig Topper8f35c132012-01-20 09:29:03 +00006215 unsigned NumElems = VT.getVectorNumElements();
6216 unsigned NumLaneElems = NumElems / 2;
6217
Craig Topper8f35c132012-01-20 09:29:03 +00006218 DebugLoc dl = SVOp->getDebugLoc();
6219 MVT EltVT = VT.getVectorElementType().getSimpleVT();
Craig Topper9a2b6e12012-04-06 07:45:23 +00006220 EVT NVT = MVT::getVectorVT(EltVT, NumLaneElems);
Craig Topper8ae97ba2012-05-21 06:40:16 +00006221 SDValue Output[2];
Craig Topper8f35c132012-01-20 09:29:03 +00006222
Craig Topper9a2b6e12012-04-06 07:45:23 +00006223 SmallVector<int, 16> Mask;
Craig Topper8f35c132012-01-20 09:29:03 +00006224 for (unsigned l = 0; l < 2; ++l) {
Craig Topper9a2b6e12012-04-06 07:45:23 +00006225 // Build a shuffle mask for the output, discovering on the fly which
6226 // input vectors to use as shuffle operands (recorded in InputUsed).
6227 // If building a suitable shuffle vector proves too hard, then bail
Craig Topper8ae97ba2012-05-21 06:40:16 +00006228 // out with UseBuildVector set.
6229 bool UseBuildVector = false;
Benjamin Kramer9e5512a2012-04-06 13:33:52 +00006230 int InputUsed[2] = { -1, -1 }; // Not yet discovered.
Craig Topper9a2b6e12012-04-06 07:45:23 +00006231 unsigned LaneStart = l * NumLaneElems;
6232 for (unsigned i = 0; i != NumLaneElems; ++i) {
6233 // The mask element. This indexes into the input.
6234 int Idx = SVOp->getMaskElt(i+LaneStart);
6235 if (Idx < 0) {
6236 // the mask element does not index into any input vector.
6237 Mask.push_back(-1);
6238 continue;
6239 }
Craig Topper8f35c132012-01-20 09:29:03 +00006240
Craig Topper9a2b6e12012-04-06 07:45:23 +00006241 // The input vector this mask element indexes into.
6242 int Input = Idx / NumLaneElems;
Craig Topper8f35c132012-01-20 09:29:03 +00006243
Craig Topper9a2b6e12012-04-06 07:45:23 +00006244 // Turn the index into an offset from the start of the input vector.
6245 Idx -= Input * NumLaneElems;
6246
6247 // Find or create a shuffle vector operand to hold this input.
6248 unsigned OpNo;
6249 for (OpNo = 0; OpNo < array_lengthof(InputUsed); ++OpNo) {
6250 if (InputUsed[OpNo] == Input)
6251 // This input vector is already an operand.
6252 break;
6253 if (InputUsed[OpNo] < 0) {
6254 // Create a new operand for this input vector.
6255 InputUsed[OpNo] = Input;
6256 break;
6257 }
6258 }
6259
6260 if (OpNo >= array_lengthof(InputUsed)) {
Craig Topper8ae97ba2012-05-21 06:40:16 +00006261 // More than two input vectors used! Give up on trying to create a
6262 // shuffle vector. Insert all elements into a BUILD_VECTOR instead.
6263 UseBuildVector = true;
6264 break;
Craig Topper9a2b6e12012-04-06 07:45:23 +00006265 }
6266
6267 // Add the mask index for the new shuffle vector.
6268 Mask.push_back(Idx + OpNo * NumLaneElems);
6269 }
6270
Craig Topper8ae97ba2012-05-21 06:40:16 +00006271 if (UseBuildVector) {
6272 SmallVector<SDValue, 16> SVOps;
6273 for (unsigned i = 0; i != NumLaneElems; ++i) {
6274 // The mask element. This indexes into the input.
6275 int Idx = SVOp->getMaskElt(i+LaneStart);
6276 if (Idx < 0) {
6277 SVOps.push_back(DAG.getUNDEF(EltVT));
6278 continue;
6279 }
6280
6281 // The input vector this mask element indexes into.
6282 int Input = Idx / NumElems;
6283
6284 // Turn the index into an offset from the start of the input vector.
6285 Idx -= Input * NumElems;
6286
6287 // Extract the vector element by hand.
6288 SVOps.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EltVT,
6289 SVOp->getOperand(Input),
6290 DAG.getIntPtrConstant(Idx)));
6291 }
6292
6293 // Construct the output using a BUILD_VECTOR.
6294 Output[l] = DAG.getNode(ISD::BUILD_VECTOR, dl, NVT, &SVOps[0],
6295 SVOps.size());
6296 } else if (InputUsed[0] < 0) {
Craig Topper9a2b6e12012-04-06 07:45:23 +00006297 // No input vectors were used! The result is undefined.
Craig Topper8ae97ba2012-05-21 06:40:16 +00006298 Output[l] = DAG.getUNDEF(NVT);
Craig Topper9a2b6e12012-04-06 07:45:23 +00006299 } else {
6300 SDValue Op0 = Extract128BitVector(SVOp->getOperand(InputUsed[0] / 2),
Craig Topperb14940a2012-04-22 20:55:18 +00006301 (InputUsed[0] % 2) * NumLaneElems,
6302 DAG, dl);
Craig Topper9a2b6e12012-04-06 07:45:23 +00006303 // If only one input was used, use an undefined vector for the other.
6304 SDValue Op1 = (InputUsed[1] < 0) ? DAG.getUNDEF(NVT) :
6305 Extract128BitVector(SVOp->getOperand(InputUsed[1] / 2),
Craig Topperb14940a2012-04-22 20:55:18 +00006306 (InputUsed[1] % 2) * NumLaneElems, DAG, dl);
Craig Topper9a2b6e12012-04-06 07:45:23 +00006307 // At least one input vector was used. Create a new shuffle vector.
Craig Topper8ae97ba2012-05-21 06:40:16 +00006308 Output[l] = DAG.getVectorShuffle(NVT, dl, Op0, Op1, &Mask[0]);
Craig Topper9a2b6e12012-04-06 07:45:23 +00006309 }
6310
6311 Mask.clear();
6312 }
Craig Topper8f35c132012-01-20 09:29:03 +00006313
6314 // Concatenate the result back
Craig Topper8ae97ba2012-05-21 06:40:16 +00006315 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT, Output[0], Output[1]);
Bruno Cardoso Lopesd0888342011-07-22 00:14:56 +00006316}
6317
Bruno Cardoso Lopes589b8972011-07-22 00:14:53 +00006318/// LowerVECTOR_SHUFFLE_128v4 - Handle all 128-bit wide vectors with
6319/// 4 elements, and match them with several different shuffle types.
Dan Gohman475871a2008-07-27 21:46:04 +00006320static SDValue
Bruno Cardoso Lopes589b8972011-07-22 00:14:53 +00006321LowerVECTOR_SHUFFLE_128v4(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006322 SDValue V1 = SVOp->getOperand(0);
6323 SDValue V2 = SVOp->getOperand(1);
6324 DebugLoc dl = SVOp->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00006325 EVT VT = SVOp->getValueType(0);
Eric Christopherfd179292009-08-27 18:07:15 +00006326
Craig Topper7a9a28b2012-08-12 02:23:29 +00006327 assert(VT.is128BitVector() && "Unsupported vector size");
Bruno Cardoso Lopes589b8972011-07-22 00:14:53 +00006328
Benjamin Kramer9c683542012-01-30 15:16:21 +00006329 std::pair<int, int> Locs[4];
6330 int Mask1[] = { -1, -1, -1, -1 };
Benjamin Kramered4c8c62012-01-15 13:16:05 +00006331 SmallVector<int, 8> PermMask(SVOp->getMask().begin(), SVOp->getMask().end());
Nate Begeman9008ca62009-04-27 18:41:29 +00006332
Evan Chengace3c172008-07-22 21:13:36 +00006333 unsigned NumHi = 0;
6334 unsigned NumLo = 0;
Evan Chengace3c172008-07-22 21:13:36 +00006335 for (unsigned i = 0; i != 4; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006336 int Idx = PermMask[i];
6337 if (Idx < 0) {
Evan Chengace3c172008-07-22 21:13:36 +00006338 Locs[i] = std::make_pair(-1, -1);
6339 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00006340 assert(Idx < 8 && "Invalid VECTOR_SHUFFLE index!");
6341 if (Idx < 4) {
Evan Chengace3c172008-07-22 21:13:36 +00006342 Locs[i] = std::make_pair(0, NumLo);
Nate Begeman9008ca62009-04-27 18:41:29 +00006343 Mask1[NumLo] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00006344 NumLo++;
6345 } else {
6346 Locs[i] = std::make_pair(1, NumHi);
6347 if (2+NumHi < 4)
Nate Begeman9008ca62009-04-27 18:41:29 +00006348 Mask1[2+NumHi] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00006349 NumHi++;
6350 }
6351 }
6352 }
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006353
Evan Chengace3c172008-07-22 21:13:36 +00006354 if (NumLo <= 2 && NumHi <= 2) {
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006355 // If no more than two elements come from either vector. This can be
6356 // implemented with two shuffles. First shuffle gather the elements.
6357 // The second shuffle, which takes the first shuffle as both of its
6358 // vector operands, put the elements into the right order.
Nate Begeman9008ca62009-04-27 18:41:29 +00006359 V1 = DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006360
Benjamin Kramer9c683542012-01-30 15:16:21 +00006361 int Mask2[] = { -1, -1, -1, -1 };
Eric Christopherfd179292009-08-27 18:07:15 +00006362
Benjamin Kramer9c683542012-01-30 15:16:21 +00006363 for (unsigned i = 0; i != 4; ++i)
6364 if (Locs[i].first != -1) {
Evan Chengace3c172008-07-22 21:13:36 +00006365 unsigned Idx = (i < 2) ? 0 : 4;
6366 Idx += Locs[i].first * 2 + Locs[i].second;
Nate Begeman9008ca62009-04-27 18:41:29 +00006367 Mask2[i] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00006368 }
Evan Chengace3c172008-07-22 21:13:36 +00006369
Nate Begeman9008ca62009-04-27 18:41:29 +00006370 return DAG.getVectorShuffle(VT, dl, V1, V1, &Mask2[0]);
Craig Topper69947b92012-04-23 06:57:04 +00006371 }
6372
6373 if (NumLo == 3 || NumHi == 3) {
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006374 // Otherwise, we must have three elements from one vector, call it X, and
6375 // one element from the other, call it Y. First, use a shufps to build an
6376 // intermediate vector with the one element from Y and the element from X
6377 // that will be in the same half in the final destination (the indexes don't
6378 // matter). Then, use a shufps to build the final vector, taking the half
6379 // containing the element from Y from the intermediate, and the other half
6380 // from X.
6381 if (NumHi == 3) {
6382 // Normalize it so the 3 elements come from V1.
Craig Topperbeabc6c2011-12-05 06:56:46 +00006383 CommuteVectorShuffleMask(PermMask, 4);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006384 std::swap(V1, V2);
6385 }
6386
6387 // Find the element from V2.
6388 unsigned HiIndex;
6389 for (HiIndex = 0; HiIndex < 3; ++HiIndex) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006390 int Val = PermMask[HiIndex];
6391 if (Val < 0)
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006392 continue;
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006393 if (Val >= 4)
6394 break;
6395 }
6396
Nate Begeman9008ca62009-04-27 18:41:29 +00006397 Mask1[0] = PermMask[HiIndex];
6398 Mask1[1] = -1;
6399 Mask1[2] = PermMask[HiIndex^1];
6400 Mask1[3] = -1;
6401 V2 = DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006402
6403 if (HiIndex >= 2) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006404 Mask1[0] = PermMask[0];
6405 Mask1[1] = PermMask[1];
6406 Mask1[2] = HiIndex & 1 ? 6 : 4;
6407 Mask1[3] = HiIndex & 1 ? 4 : 6;
6408 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006409 }
Craig Topper69947b92012-04-23 06:57:04 +00006410
6411 Mask1[0] = HiIndex & 1 ? 2 : 0;
6412 Mask1[1] = HiIndex & 1 ? 0 : 2;
6413 Mask1[2] = PermMask[2];
6414 Mask1[3] = PermMask[3];
6415 if (Mask1[2] >= 0)
6416 Mask1[2] += 4;
6417 if (Mask1[3] >= 0)
6418 Mask1[3] += 4;
6419 return DAG.getVectorShuffle(VT, dl, V2, V1, &Mask1[0]);
Evan Chengace3c172008-07-22 21:13:36 +00006420 }
6421
6422 // Break it into (shuffle shuffle_hi, shuffle_lo).
Benjamin Kramer9c683542012-01-30 15:16:21 +00006423 int LoMask[] = { -1, -1, -1, -1 };
6424 int HiMask[] = { -1, -1, -1, -1 };
Nate Begeman9008ca62009-04-27 18:41:29 +00006425
Benjamin Kramer9c683542012-01-30 15:16:21 +00006426 int *MaskPtr = LoMask;
Evan Chengace3c172008-07-22 21:13:36 +00006427 unsigned MaskIdx = 0;
6428 unsigned LoIdx = 0;
6429 unsigned HiIdx = 2;
6430 for (unsigned i = 0; i != 4; ++i) {
6431 if (i == 2) {
Benjamin Kramer9c683542012-01-30 15:16:21 +00006432 MaskPtr = HiMask;
Evan Chengace3c172008-07-22 21:13:36 +00006433 MaskIdx = 1;
6434 LoIdx = 0;
6435 HiIdx = 2;
6436 }
Nate Begeman9008ca62009-04-27 18:41:29 +00006437 int Idx = PermMask[i];
6438 if (Idx < 0) {
Evan Chengace3c172008-07-22 21:13:36 +00006439 Locs[i] = std::make_pair(-1, -1);
Nate Begeman9008ca62009-04-27 18:41:29 +00006440 } else if (Idx < 4) {
Evan Chengace3c172008-07-22 21:13:36 +00006441 Locs[i] = std::make_pair(MaskIdx, LoIdx);
Benjamin Kramer9c683542012-01-30 15:16:21 +00006442 MaskPtr[LoIdx] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00006443 LoIdx++;
6444 } else {
6445 Locs[i] = std::make_pair(MaskIdx, HiIdx);
Benjamin Kramer9c683542012-01-30 15:16:21 +00006446 MaskPtr[HiIdx] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00006447 HiIdx++;
6448 }
6449 }
6450
Nate Begeman9008ca62009-04-27 18:41:29 +00006451 SDValue LoShuffle = DAG.getVectorShuffle(VT, dl, V1, V2, &LoMask[0]);
6452 SDValue HiShuffle = DAG.getVectorShuffle(VT, dl, V1, V2, &HiMask[0]);
Benjamin Kramer9c683542012-01-30 15:16:21 +00006453 int MaskOps[] = { -1, -1, -1, -1 };
6454 for (unsigned i = 0; i != 4; ++i)
6455 if (Locs[i].first != -1)
6456 MaskOps[i] = Locs[i].first * 4 + Locs[i].second;
Nate Begeman9008ca62009-04-27 18:41:29 +00006457 return DAG.getVectorShuffle(VT, dl, LoShuffle, HiShuffle, &MaskOps[0]);
Evan Chengace3c172008-07-22 21:13:36 +00006458}
6459
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00006460static bool MayFoldVectorLoad(SDValue V) {
Jakub Staszaka24262a2012-10-30 00:01:57 +00006461 while (V.hasOneUse() && V.getOpcode() == ISD::BITCAST)
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00006462 V = V.getOperand(0);
Jakub Staszaka24262a2012-10-30 00:01:57 +00006463
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00006464 if (V.hasOneUse() && V.getOpcode() == ISD::SCALAR_TO_VECTOR)
6465 V = V.getOperand(0);
Evan Cheng7bc389b2011-11-08 00:31:58 +00006466 if (V.hasOneUse() && V.getOpcode() == ISD::BUILD_VECTOR &&
6467 V.getNumOperands() == 2 && V.getOperand(1).getOpcode() == ISD::UNDEF)
6468 // BUILD_VECTOR (load), undef
6469 V = V.getOperand(0);
Jakub Staszaka24262a2012-10-30 00:01:57 +00006470
6471 return MayFoldLoad(V);
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00006472}
6473
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006474// FIXME: the version above should always be used. Since there's
6475// a bug where several vector shuffles can't be folded because the
6476// DAG is not updated during lowering and a node claims to have two
6477// uses while it only has one, use this version, and let isel match
6478// another instruction if the load really happens to have more than
6479// one use. Remove this version after this bug get fixed.
Evan Cheng835580f2010-10-07 20:50:20 +00006480// rdar://8434668, PR8156
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006481static bool RelaxedMayFoldVectorLoad(SDValue V) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006482 if (V.hasOneUse() && V.getOpcode() == ISD::BITCAST)
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006483 V = V.getOperand(0);
6484 if (V.hasOneUse() && V.getOpcode() == ISD::SCALAR_TO_VECTOR)
6485 V = V.getOperand(0);
6486 if (ISD::isNormalLoad(V.getNode()))
6487 return true;
6488 return false;
6489}
6490
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006491static
Evan Cheng835580f2010-10-07 20:50:20 +00006492SDValue getMOVDDup(SDValue &Op, DebugLoc &dl, SDValue V1, SelectionDAG &DAG) {
6493 EVT VT = Op.getValueType();
6494
6495 // Canonizalize to v2f64.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006496 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, V1);
6497 return DAG.getNode(ISD::BITCAST, dl, VT,
Evan Cheng835580f2010-10-07 20:50:20 +00006498 getTargetShuffleNode(X86ISD::MOVDDUP, dl, MVT::v2f64,
6499 V1, DAG));
6500}
6501
6502static
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006503SDValue getMOVLowToHigh(SDValue &Op, DebugLoc &dl, SelectionDAG &DAG,
Craig Topper1accb7e2012-01-10 06:54:16 +00006504 bool HasSSE2) {
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006505 SDValue V1 = Op.getOperand(0);
6506 SDValue V2 = Op.getOperand(1);
6507 EVT VT = Op.getValueType();
6508
6509 assert(VT != MVT::v2i64 && "unsupported shuffle type");
6510
Craig Topper1accb7e2012-01-10 06:54:16 +00006511 if (HasSSE2 && VT == MVT::v2f64)
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006512 return getTargetShuffleNode(X86ISD::MOVLHPD, dl, VT, V1, V2, DAG);
6513
Evan Cheng0899f5c2011-08-31 02:05:24 +00006514 // v4f32 or v4i32: canonizalized to v4f32 (which is legal for SSE1)
6515 return DAG.getNode(ISD::BITCAST, dl, VT,
6516 getTargetShuffleNode(X86ISD::MOVLHPS, dl, MVT::v4f32,
6517 DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, V1),
6518 DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, V2), DAG));
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006519}
6520
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00006521static
6522SDValue getMOVHighToLow(SDValue &Op, DebugLoc &dl, SelectionDAG &DAG) {
6523 SDValue V1 = Op.getOperand(0);
6524 SDValue V2 = Op.getOperand(1);
6525 EVT VT = Op.getValueType();
6526
6527 assert((VT == MVT::v4i32 || VT == MVT::v4f32) &&
6528 "unsupported shuffle type");
6529
6530 if (V2.getOpcode() == ISD::UNDEF)
6531 V2 = V1;
6532
6533 // v4i32 or v4f32
6534 return getTargetShuffleNode(X86ISD::MOVHLPS, dl, VT, V1, V2, DAG);
6535}
6536
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006537static
Craig Topper1accb7e2012-01-10 06:54:16 +00006538SDValue getMOVLP(SDValue &Op, DebugLoc &dl, SelectionDAG &DAG, bool HasSSE2) {
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006539 SDValue V1 = Op.getOperand(0);
6540 SDValue V2 = Op.getOperand(1);
6541 EVT VT = Op.getValueType();
6542 unsigned NumElems = VT.getVectorNumElements();
6543
6544 // Use MOVLPS and MOVLPD in case V1 or V2 are loads. During isel, the second
6545 // operand of these instructions is only memory, so check if there's a
6546 // potencial load folding here, otherwise use SHUFPS or MOVSD to match the
6547 // same masks.
6548 bool CanFoldLoad = false;
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006549
Bruno Cardoso Lopesd00bfe12010-09-02 02:35:51 +00006550 // Trivial case, when V2 comes from a load.
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00006551 if (MayFoldVectorLoad(V2))
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006552 CanFoldLoad = true;
6553
6554 // When V1 is a load, it can be folded later into a store in isel, example:
6555 // (store (v4f32 (X86Movlps (load addr:$src1), VR128:$src2)), addr:$src1)
6556 // turns into:
6557 // (MOVLPSmr addr:$src1, VR128:$src2)
6558 // So, recognize this potential and also use MOVLPS or MOVLPD
Evan Cheng7bc389b2011-11-08 00:31:58 +00006559 else if (MayFoldVectorLoad(V1) && MayFoldIntoStore(Op))
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006560 CanFoldLoad = true;
6561
Dan Gohman65fd6562011-11-03 21:49:52 +00006562 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006563 if (CanFoldLoad) {
Craig Topper1accb7e2012-01-10 06:54:16 +00006564 if (HasSSE2 && NumElems == 2)
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006565 return getTargetShuffleNode(X86ISD::MOVLPD, dl, VT, V1, V2, DAG);
6566
6567 if (NumElems == 4)
Benjamin Kramerd9b0b022012-06-02 10:20:22 +00006568 // If we don't care about the second element, proceed to use movss.
Dan Gohman65fd6562011-11-03 21:49:52 +00006569 if (SVOp->getMaskElt(1) != -1)
6570 return getTargetShuffleNode(X86ISD::MOVLPS, dl, VT, V1, V2, DAG);
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006571 }
6572
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006573 // movl and movlp will both match v2i64, but v2i64 is never matched by
6574 // movl earlier because we make it strict to avoid messing with the movlp load
6575 // folding logic (see the code above getMOVLP call). Match it here then,
6576 // this is horrible, but will stay like this until we move all shuffle
6577 // matching to x86 specific nodes. Note that for the 1st condition all
6578 // types are matched with movsd.
Craig Topper1accb7e2012-01-10 06:54:16 +00006579 if (HasSSE2) {
Bruno Cardoso Lopes5ca0d142011-09-14 02:36:14 +00006580 // FIXME: isMOVLMask should be checked and matched before getMOVLP,
6581 // as to remove this logic from here, as much as possible
Craig Topper5aaffa82012-02-19 02:53:47 +00006582 if (NumElems == 2 || !isMOVLMask(SVOp->getMask(), VT))
Bruno Cardoso Lopes57d6a5e2011-08-31 03:04:20 +00006583 return getTargetShuffleNode(X86ISD::MOVSD, dl, VT, V1, V2, DAG);
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006584 return getTargetShuffleNode(X86ISD::MOVSS, dl, VT, V1, V2, DAG);
Bruno Cardoso Lopes57d6a5e2011-08-31 03:04:20 +00006585 }
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006586
6587 assert(VT != MVT::v4i32 && "unsupported shuffle type");
6588
6589 // Invert the operand order and use SHUFPS to match it.
Craig Topperb3982da2011-12-31 23:50:21 +00006590 return getTargetShuffleNode(X86ISD::SHUFP, dl, VT, V2, V1,
Craig Topper5aaffa82012-02-19 02:53:47 +00006591 getShuffleSHUFImmediate(SVOp), DAG);
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006592}
6593
Michael Liaod9d09602012-10-23 17:34:00 +00006594// Reduce a vector shuffle to zext.
6595SDValue
6596X86TargetLowering::lowerVectorIntExtend(SDValue Op, SelectionDAG &DAG) const {
6597 // PMOVZX is only available from SSE41.
6598 if (!Subtarget->hasSSE41())
6599 return SDValue();
6600
6601 EVT VT = Op.getValueType();
6602
6603 // Only AVX2 support 256-bit vector integer extending.
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00006604 if (!Subtarget->hasInt256() && VT.is256BitVector())
Michael Liaod9d09602012-10-23 17:34:00 +00006605 return SDValue();
6606
6607 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
6608 DebugLoc DL = Op.getDebugLoc();
6609 SDValue V1 = Op.getOperand(0);
6610 SDValue V2 = Op.getOperand(1);
6611 unsigned NumElems = VT.getVectorNumElements();
6612
6613 // Extending is an unary operation and the element type of the source vector
6614 // won't be equal to or larger than i64.
6615 if (V2.getOpcode() != ISD::UNDEF || !VT.isInteger() ||
6616 VT.getVectorElementType() == MVT::i64)
6617 return SDValue();
6618
6619 // Find the expansion ratio, e.g. expanding from i8 to i32 has a ratio of 4.
6620 unsigned Shift = 1; // Start from 2, i.e. 1 << 1.
Duncan Sands34739052012-10-29 11:29:53 +00006621 while ((1U << Shift) < NumElems) {
6622 if (SVOp->getMaskElt(1U << Shift) == 1)
Michael Liaod9d09602012-10-23 17:34:00 +00006623 break;
6624 Shift += 1;
6625 // The maximal ratio is 8, i.e. from i8 to i64.
6626 if (Shift > 3)
6627 return SDValue();
6628 }
6629
6630 // Check the shuffle mask.
6631 unsigned Mask = (1U << Shift) - 1;
6632 for (unsigned i = 0; i != NumElems; ++i) {
6633 int EltIdx = SVOp->getMaskElt(i);
6634 if ((i & Mask) != 0 && EltIdx != -1)
6635 return SDValue();
Matt Beaumont-Gaya999de02012-10-23 19:46:36 +00006636 if ((i & Mask) == 0 && (unsigned)EltIdx != (i >> Shift))
Michael Liaod9d09602012-10-23 17:34:00 +00006637 return SDValue();
6638 }
6639
6640 unsigned NBits = VT.getVectorElementType().getSizeInBits() << Shift;
6641 EVT NeVT = EVT::getIntegerVT(*DAG.getContext(), NBits);
6642 EVT NVT = EVT::getVectorVT(*DAG.getContext(), NeVT, NumElems >> Shift);
6643
6644 if (!isTypeLegal(NVT))
6645 return SDValue();
6646
6647 // Simplify the operand as it's prepared to be fed into shuffle.
6648 unsigned SignificantBits = NVT.getSizeInBits() >> Shift;
6649 if (V1.getOpcode() == ISD::BITCAST &&
6650 V1.getOperand(0).getOpcode() == ISD::SCALAR_TO_VECTOR &&
6651 V1.getOperand(0).getOperand(0).getOpcode() == ISD::EXTRACT_VECTOR_ELT &&
6652 V1.getOperand(0)
6653 .getOperand(0).getValueType().getSizeInBits() == SignificantBits) {
6654 // (bitcast (sclr2vec (ext_vec_elt x))) -> (bitcast x)
6655 SDValue V = V1.getOperand(0).getOperand(0).getOperand(0);
Michael Liao07872742012-10-23 21:40:15 +00006656 ConstantSDNode *CIdx =
6657 dyn_cast<ConstantSDNode>(V1.getOperand(0).getOperand(0).getOperand(1));
Michael Liaod9d09602012-10-23 17:34:00 +00006658 // If it's foldable, i.e. normal load with single use, we will let code
6659 // selection to fold it. Otherwise, we will short the conversion sequence.
Michael Liao07872742012-10-23 21:40:15 +00006660 if (CIdx && CIdx->getZExtValue() == 0 &&
6661 (!ISD::isNormalLoad(V.getNode()) || !V.hasOneUse()))
Michael Liaod9d09602012-10-23 17:34:00 +00006662 V1 = DAG.getNode(ISD::BITCAST, DL, V1.getValueType(), V);
6663 }
6664
6665 return DAG.getNode(ISD::BITCAST, DL, VT,
6666 DAG.getNode(X86ISD::VZEXT, DL, NVT, V1));
6667}
6668
Nadav Rotem154819d2012-04-09 07:45:58 +00006669SDValue
6670X86TargetLowering::NormalizeVectorShuffle(SDValue Op, SelectionDAG &DAG) const {
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006671 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
6672 EVT VT = Op.getValueType();
6673 DebugLoc dl = Op.getDebugLoc();
6674 SDValue V1 = Op.getOperand(0);
6675 SDValue V2 = Op.getOperand(1);
6676
6677 if (isZeroShuffle(SVOp))
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00006678 return getZeroVector(VT, Subtarget, DAG, dl);
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006679
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006680 // Handle splat operations
6681 if (SVOp->isSplat()) {
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00006682 unsigned NumElem = VT.getVectorNumElements();
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00006683 int Size = VT.getSizeInBits();
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006684
Bruno Cardoso Lopes0e6d2302011-08-17 02:29:19 +00006685 // Use vbroadcast whenever the splat comes from a foldable load
Nadav Rotem154819d2012-04-09 07:45:58 +00006686 SDValue Broadcast = LowerVectorBroadcast(Op, DAG);
Nadav Rotem9d68b062012-04-08 12:54:54 +00006687 if (Broadcast.getNode())
6688 return Broadcast;
Bruno Cardoso Lopes0e6d2302011-08-17 02:29:19 +00006689
Bruno Cardoso Lopes6a32adc2011-07-25 23:05:25 +00006690 // Handle splats by matching through known shuffle masks
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00006691 if ((Size == 128 && NumElem <= 4) ||
Jakub Staszakd642baf2012-11-26 19:24:31 +00006692 (Size == 256 && NumElem <= 8))
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006693 return SDValue();
6694
Bruno Cardoso Lopes8a5b2622011-08-17 02:29:13 +00006695 // All remaning splats are promoted to target supported vector shuffles.
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006696 return PromoteSplat(SVOp, DAG);
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006697 }
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006698
Michael Liaod9d09602012-10-23 17:34:00 +00006699 // Check integer expanding shuffles.
6700 SDValue NewOp = lowerVectorIntExtend(Op, DAG);
6701 if (NewOp.getNode())
6702 return NewOp;
6703
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006704 // If the shuffle can be profitably rewritten as a narrower shuffle, then
6705 // do it!
Craig Topperf3640d72012-05-04 04:44:49 +00006706 if (VT == MVT::v8i16 || VT == MVT::v16i8 ||
6707 VT == MVT::v16i16 || VT == MVT::v32i8) {
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006708 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, dl);
6709 if (NewOp.getNode())
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006710 return DAG.getNode(ISD::BITCAST, dl, VT, NewOp);
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006711 } else if ((VT == MVT::v4i32 ||
Craig Topper1accb7e2012-01-10 06:54:16 +00006712 (VT == MVT::v4f32 && Subtarget->hasSSE2()))) {
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006713 // FIXME: Figure out a cleaner way to do this.
6714 // Try to make use of movq to zero out the top part.
6715 if (ISD::isBuildVectorAllZeros(V2.getNode())) {
6716 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, dl);
6717 if (NewOp.getNode()) {
Craig Topper5aaffa82012-02-19 02:53:47 +00006718 EVT NewVT = NewOp.getValueType();
6719 if (isCommutedMOVLMask(cast<ShuffleVectorSDNode>(NewOp)->getMask(),
6720 NewVT, true, false))
6721 return getVZextMovL(VT, NewVT, NewOp.getOperand(0),
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006722 DAG, Subtarget, dl);
6723 }
6724 } else if (ISD::isBuildVectorAllZeros(V1.getNode())) {
6725 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, dl);
Craig Topper5aaffa82012-02-19 02:53:47 +00006726 if (NewOp.getNode()) {
6727 EVT NewVT = NewOp.getValueType();
6728 if (isMOVLMask(cast<ShuffleVectorSDNode>(NewOp)->getMask(), NewVT))
6729 return getVZextMovL(VT, NewVT, NewOp.getOperand(1),
6730 DAG, Subtarget, dl);
6731 }
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006732 }
6733 }
6734 return SDValue();
6735}
6736
Dan Gohman475871a2008-07-27 21:46:04 +00006737SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006738X86TargetLowering::LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) const {
Nate Begeman9008ca62009-04-27 18:41:29 +00006739 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Dan Gohman475871a2008-07-27 21:46:04 +00006740 SDValue V1 = Op.getOperand(0);
6741 SDValue V2 = Op.getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +00006742 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006743 DebugLoc dl = Op.getDebugLoc();
Nate Begeman9008ca62009-04-27 18:41:29 +00006744 unsigned NumElems = VT.getVectorNumElements();
Elena Demikhovsky16db7102012-01-12 20:33:10 +00006745 bool V1IsUndef = V1.getOpcode() == ISD::UNDEF;
Evan Cheng0db9fe62006-04-25 20:13:52 +00006746 bool V2IsUndef = V2.getOpcode() == ISD::UNDEF;
Evan Chengd9b8e402006-10-16 06:36:00 +00006747 bool V1IsSplat = false;
6748 bool V2IsSplat = false;
Craig Topper1accb7e2012-01-10 06:54:16 +00006749 bool HasSSE2 = Subtarget->hasSSE2();
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00006750 bool HasFp256 = Subtarget->hasFp256();
6751 bool HasInt256 = Subtarget->hasInt256();
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006752 MachineFunction &MF = DAG.getMachineFunction();
Bill Wendling67658342012-10-09 07:45:08 +00006753 bool OptForSize = MF.getFunction()->getFnAttributes().
6754 hasAttribute(Attributes::OptimizeForSize);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006755
Craig Topper3426a3e2011-11-14 06:46:21 +00006756 assert(VT.getSizeInBits() != 64 && "Can't lower MMX shuffles");
Bruno Cardoso Lopes58277b12010-09-07 18:41:45 +00006757
Elena Demikhovsky16db7102012-01-12 20:33:10 +00006758 if (V1IsUndef && V2IsUndef)
6759 return DAG.getUNDEF(VT);
6760
6761 assert(!V1IsUndef && "Op 1 of shuffle should not be undef");
Craig Topper38034c52011-11-26 22:55:48 +00006762
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006763 // Vector shuffle lowering takes 3 steps:
6764 //
6765 // 1) Normalize the input vectors. Here splats, zeroed vectors, profitable
6766 // narrowing and commutation of operands should be handled.
6767 // 2) Matching of shuffles with known shuffle masks to x86 target specific
6768 // shuffle nodes.
6769 // 3) Rewriting of unmatched masks into new generic shuffle operations,
6770 // so the shuffle can be broken into other shuffles and the legalizer can
6771 // try the lowering again.
6772 //
Craig Topper3426a3e2011-11-14 06:46:21 +00006773 // The general idea is that no vector_shuffle operation should be left to
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006774 // be matched during isel, all of them must be converted to a target specific
6775 // node here.
Bruno Cardoso Lopes0d1340b2010-09-07 20:20:27 +00006776
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006777 // Normalize the input vectors. Here splats, zeroed vectors, profitable
6778 // narrowing and commutation of operands should be handled. The actual code
6779 // doesn't include all of those, work in progress...
Nadav Rotem154819d2012-04-09 07:45:58 +00006780 SDValue NewOp = NormalizeVectorShuffle(Op, DAG);
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006781 if (NewOp.getNode())
6782 return NewOp;
Eric Christopherfd179292009-08-27 18:07:15 +00006783
Craig Topper5aaffa82012-02-19 02:53:47 +00006784 SmallVector<int, 8> M(SVOp->getMask().begin(), SVOp->getMask().end());
6785
Bruno Cardoso Lopesa22c8452010-09-04 00:39:43 +00006786 // NOTE: isPSHUFDMask can also match both masks below (unpckl_undef and
6787 // unpckh_undef). Only use pshufd if speed is more important than size.
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00006788 if (OptForSize && isUNPCKL_v_undef_Mask(M, VT, HasInt256))
Craig Topper34671b82011-12-06 08:21:25 +00006789 return getTargetShuffleNode(X86ISD::UNPCKL, dl, VT, V1, V1, DAG);
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00006790 if (OptForSize && isUNPCKH_v_undef_Mask(M, VT, HasInt256))
Craig Topper34671b82011-12-06 08:21:25 +00006791 return getTargetShuffleNode(X86ISD::UNPCKH, dl, VT, V1, V1, DAG);
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00006792
Craig Topperdd637ae2012-02-19 05:41:45 +00006793 if (isMOVDDUPMask(M, VT) && Subtarget->hasSSE3() &&
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006794 V2IsUndef && RelaxedMayFoldVectorLoad(V1))
Evan Cheng835580f2010-10-07 20:50:20 +00006795 return getMOVDDup(Op, dl, V1, DAG);
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006796
Craig Topperdd637ae2012-02-19 05:41:45 +00006797 if (isMOVHLPS_v_undef_Mask(M, VT))
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006798 return getMOVHighToLow(Op, dl, DAG);
6799
6800 // Use to match splats
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00006801 if (HasSSE2 && isUNPCKHMask(M, VT, HasInt256) && V2IsUndef &&
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006802 (VT == MVT::v2f64 || VT == MVT::v2i64))
Craig Topper34671b82011-12-06 08:21:25 +00006803 return getTargetShuffleNode(X86ISD::UNPCKH, dl, VT, V1, V1, DAG);
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006804
Craig Topper5aaffa82012-02-19 02:53:47 +00006805 if (isPSHUFDMask(M, VT)) {
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006806 // The actual implementation will match the mask in the if above and then
6807 // during isel it can match several different instructions, not only pshufd
6808 // as its name says, sad but true, emulate the behavior for now...
Craig Topperdd637ae2012-02-19 05:41:45 +00006809 if (isMOVDDUPMask(M, VT) && ((VT == MVT::v4f32 || VT == MVT::v2i64)))
6810 return getTargetShuffleNode(X86ISD::MOVLHPS, dl, VT, V1, V1, DAG);
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006811
Craig Topper5aaffa82012-02-19 02:53:47 +00006812 unsigned TargetMask = getShuffleSHUFImmediate(SVOp);
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006813
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00006814 if (HasFp256 && (VT == MVT::v4f32 || VT == MVT::v2f64))
Craig Topperdbd98a42012-02-07 06:28:42 +00006815 return getTargetShuffleNode(X86ISD::VPERMILP, dl, VT, V1, TargetMask, DAG);
6816
Craig Topper1accb7e2012-01-10 06:54:16 +00006817 if (HasSSE2 && (VT == MVT::v4f32 || VT == MVT::v4i32))
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006818 return getTargetShuffleNode(X86ISD::PSHUFD, dl, VT, V1, TargetMask, DAG);
6819
Craig Topperb3982da2011-12-31 23:50:21 +00006820 return getTargetShuffleNode(X86ISD::SHUFP, dl, VT, V1, V1,
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00006821 TargetMask, DAG);
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006822 }
Eric Christopherfd179292009-08-27 18:07:15 +00006823
Evan Chengf26ffe92008-05-29 08:22:04 +00006824 // Check if this can be converted into a logical shift.
6825 bool isLeft = false;
6826 unsigned ShAmt = 0;
Dan Gohman475871a2008-07-27 21:46:04 +00006827 SDValue ShVal;
Craig Topper1accb7e2012-01-10 06:54:16 +00006828 bool isShift = HasSSE2 && isVectorShift(SVOp, DAG, isLeft, ShVal, ShAmt);
Evan Chengf26ffe92008-05-29 08:22:04 +00006829 if (isShift && ShVal.hasOneUse()) {
Scott Michelfdc40a02009-02-17 22:15:04 +00006830 // If the shifted value has multiple uses, it may be cheaper to use
Evan Chengf26ffe92008-05-29 08:22:04 +00006831 // v_set0 + movlhps or movhlps, etc.
Dan Gohman8a55ce42009-09-23 21:02:20 +00006832 EVT EltVT = VT.getVectorElementType();
6833 ShAmt *= EltVT.getSizeInBits();
Dale Johannesenace16102009-02-03 19:33:06 +00006834 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00006835 }
Eric Christopherfd179292009-08-27 18:07:15 +00006836
Craig Topper5aaffa82012-02-19 02:53:47 +00006837 if (isMOVLMask(M, VT)) {
Gabor Greifba36cb52008-08-28 21:40:38 +00006838 if (ISD::isBuildVectorAllZeros(V1.getNode()))
Dale Johannesenace16102009-02-03 19:33:06 +00006839 return getVZextMovL(VT, VT, V2, DAG, Subtarget, dl);
Craig Topperdd637ae2012-02-19 05:41:45 +00006840 if (!isMOVLPMask(M, VT)) {
Craig Topper1accb7e2012-01-10 06:54:16 +00006841 if (HasSSE2 && (VT == MVT::v2i64 || VT == MVT::v2f64))
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00006842 return getTargetShuffleNode(X86ISD::MOVSD, dl, VT, V1, V2, DAG);
6843
Bruno Cardoso Lopes4783a3e2010-09-01 22:59:03 +00006844 if (VT == MVT::v4i32 || VT == MVT::v4f32)
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00006845 return getTargetShuffleNode(X86ISD::MOVSS, dl, VT, V1, V2, DAG);
6846 }
Evan Cheng7e2ff772008-05-08 00:57:18 +00006847 }
Eric Christopherfd179292009-08-27 18:07:15 +00006848
Nate Begeman9008ca62009-04-27 18:41:29 +00006849 // FIXME: fold these into legal mask.
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00006850 if (isMOVLHPSMask(M, VT) && !isUNPCKLMask(M, VT, HasInt256))
Craig Topper1accb7e2012-01-10 06:54:16 +00006851 return getMOVLowToHigh(Op, dl, DAG, HasSSE2);
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006852
Craig Topperdd637ae2012-02-19 05:41:45 +00006853 if (isMOVHLPSMask(M, VT))
Dale Johannesen0488fb62010-09-30 23:57:10 +00006854 return getMOVHighToLow(Op, dl, DAG);
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00006855
Craig Topperdd637ae2012-02-19 05:41:45 +00006856 if (V2IsUndef && isMOVSHDUPMask(M, VT, Subtarget))
Dale Johannesen0488fb62010-09-30 23:57:10 +00006857 return getTargetShuffleNode(X86ISD::MOVSHDUP, dl, VT, V1, DAG);
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00006858
Craig Topperdd637ae2012-02-19 05:41:45 +00006859 if (V2IsUndef && isMOVSLDUPMask(M, VT, Subtarget))
Dale Johannesen0488fb62010-09-30 23:57:10 +00006860 return getTargetShuffleNode(X86ISD::MOVSLDUP, dl, VT, V1, DAG);
Bruno Cardoso Lopes013bb3d2010-08-31 22:35:05 +00006861
Craig Topperdd637ae2012-02-19 05:41:45 +00006862 if (isMOVLPMask(M, VT))
Craig Topper1accb7e2012-01-10 06:54:16 +00006863 return getMOVLP(Op, dl, DAG, HasSSE2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006864
Craig Topperdd637ae2012-02-19 05:41:45 +00006865 if (ShouldXformToMOVHLPS(M, VT) ||
6866 ShouldXformToMOVLP(V1.getNode(), V2.getNode(), M, VT))
Nate Begeman9008ca62009-04-27 18:41:29 +00006867 return CommuteVectorShuffle(SVOp, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006868
Evan Chengf26ffe92008-05-29 08:22:04 +00006869 if (isShift) {
Craig Toppered2e13d2012-01-22 19:15:14 +00006870 // No better options. Use a vshldq / vsrldq.
Dan Gohman8a55ce42009-09-23 21:02:20 +00006871 EVT EltVT = VT.getVectorElementType();
6872 ShAmt *= EltVT.getSizeInBits();
Dale Johannesenace16102009-02-03 19:33:06 +00006873 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00006874 }
Eric Christopherfd179292009-08-27 18:07:15 +00006875
Evan Cheng9eca5e82006-10-25 21:49:50 +00006876 bool Commuted = false;
Chris Lattner8a594482007-11-25 00:24:49 +00006877 // FIXME: This should also accept a bitcast of a splat? Be careful, not
6878 // 1,1,1,1 -> v8i16 though.
Gabor Greifba36cb52008-08-28 21:40:38 +00006879 V1IsSplat = isSplatVector(V1.getNode());
6880 V2IsSplat = isSplatVector(V2.getNode());
Scott Michelfdc40a02009-02-17 22:15:04 +00006881
Chris Lattner8a594482007-11-25 00:24:49 +00006882 // Canonicalize the splat or undef, if present, to be on the RHS.
Craig Topper39a9e482012-02-11 06:24:48 +00006883 if (!V2IsUndef && V1IsSplat && !V2IsSplat) {
6884 CommuteVectorShuffleMask(M, NumElems);
6885 std::swap(V1, V2);
Evan Cheng9bbbb982006-10-25 20:48:19 +00006886 std::swap(V1IsSplat, V2IsSplat);
Evan Cheng9eca5e82006-10-25 21:49:50 +00006887 Commuted = true;
Evan Cheng9bbbb982006-10-25 20:48:19 +00006888 }
6889
Craig Topperbeabc6c2011-12-05 06:56:46 +00006890 if (isCommutedMOVLMask(M, VT, V2IsSplat, V2IsUndef)) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006891 // Shuffling low element of v1 into undef, just return v1.
Eric Christopherfd179292009-08-27 18:07:15 +00006892 if (V2IsUndef)
Nate Begeman9008ca62009-04-27 18:41:29 +00006893 return V1;
6894 // If V2 is a splat, the mask may be malformed such as <4,3,3,3>, which
6895 // the instruction selector will not match, so get a canonical MOVL with
6896 // swapped operands to undo the commute.
6897 return getMOVL(DAG, dl, VT, V2, V1);
Evan Chengd9b8e402006-10-16 06:36:00 +00006898 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00006899
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00006900 if (isUNPCKLMask(M, VT, HasInt256))
Craig Topper34671b82011-12-06 08:21:25 +00006901 return getTargetShuffleNode(X86ISD::UNPCKL, dl, VT, V1, V2, DAG);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00006902
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00006903 if (isUNPCKHMask(M, VT, HasInt256))
Craig Topper34671b82011-12-06 08:21:25 +00006904 return getTargetShuffleNode(X86ISD::UNPCKH, dl, VT, V1, V2, DAG);
Evan Chenge1113032006-10-04 18:33:38 +00006905
Evan Cheng9bbbb982006-10-25 20:48:19 +00006906 if (V2IsSplat) {
6907 // Normalize mask so all entries that point to V2 points to its first
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00006908 // element then try to match unpck{h|l} again. If match, return a
Craig Topper39a9e482012-02-11 06:24:48 +00006909 // new vector_shuffle with the corrected mask.p
6910 SmallVector<int, 8> NewMask(M.begin(), M.end());
6911 NormalizeMask(NewMask, NumElems);
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00006912 if (isUNPCKLMask(NewMask, VT, HasInt256, true))
Craig Topper39a9e482012-02-11 06:24:48 +00006913 return getTargetShuffleNode(X86ISD::UNPCKL, dl, VT, V1, V2, DAG);
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00006914 if (isUNPCKHMask(NewMask, VT, HasInt256, true))
Craig Topper39a9e482012-02-11 06:24:48 +00006915 return getTargetShuffleNode(X86ISD::UNPCKH, dl, VT, V1, V2, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006916 }
6917
Evan Cheng9eca5e82006-10-25 21:49:50 +00006918 if (Commuted) {
6919 // Commute is back and try unpck* again.
Nate Begeman9008ca62009-04-27 18:41:29 +00006920 // FIXME: this seems wrong.
Craig Topper39a9e482012-02-11 06:24:48 +00006921 CommuteVectorShuffleMask(M, NumElems);
6922 std::swap(V1, V2);
6923 std::swap(V1IsSplat, V2IsSplat);
6924 Commuted = false;
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00006925
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00006926 if (isUNPCKLMask(M, VT, HasInt256))
Craig Topper39a9e482012-02-11 06:24:48 +00006927 return getTargetShuffleNode(X86ISD::UNPCKL, dl, VT, V1, V2, DAG);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00006928
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00006929 if (isUNPCKHMask(M, VT, HasInt256))
Craig Topper39a9e482012-02-11 06:24:48 +00006930 return getTargetShuffleNode(X86ISD::UNPCKH, dl, VT, V1, V2, DAG);
Evan Cheng9eca5e82006-10-25 21:49:50 +00006931 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00006932
Nate Begeman9008ca62009-04-27 18:41:29 +00006933 // Normalize the node to match x86 shuffle ops if needed
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00006934 if (!V2IsUndef && (isSHUFPMask(M, VT, HasFp256, /* Commuted */ true)))
Nate Begeman9008ca62009-04-27 18:41:29 +00006935 return CommuteVectorShuffle(SVOp, DAG);
6936
Bruno Cardoso Lopes7256e222010-09-03 23:24:06 +00006937 // The checks below are all present in isShuffleMaskLegal, but they are
6938 // inlined here right now to enable us to directly emit target specific
6939 // nodes, and remove one by one until they don't return Op anymore.
Bruno Cardoso Lopes7256e222010-09-03 23:24:06 +00006940
Craig Topper0e2037b2012-01-20 05:53:00 +00006941 if (isPALIGNRMask(M, VT, Subtarget))
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00006942 return getTargetShuffleNode(X86ISD::PALIGN, dl, VT, V1, V2,
Craig Topperd93e4c32011-12-11 19:12:35 +00006943 getShufflePALIGNRImmediate(SVOp),
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00006944 DAG);
6945
Bruno Cardoso Lopesc800c0d2010-09-04 02:02:14 +00006946 if (ShuffleVectorSDNode::isSplatMask(&M[0], VT) &&
6947 SVOp->getSplatIndex() == 0 && V2IsUndef) {
Craig Topperbeabc6c2011-12-05 06:56:46 +00006948 if (VT == MVT::v2f64 || VT == MVT::v2i64)
Craig Topper34671b82011-12-06 08:21:25 +00006949 return getTargetShuffleNode(X86ISD::UNPCKL, dl, VT, V1, V1, DAG);
Bruno Cardoso Lopesc800c0d2010-09-04 02:02:14 +00006950 }
6951
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00006952 if (isPSHUFHWMask(M, VT, HasInt256))
Bruno Cardoso Lopesbbfc3102010-09-04 01:36:45 +00006953 return getTargetShuffleNode(X86ISD::PSHUFHW, dl, VT, V1,
Craig Topper5aaffa82012-02-19 02:53:47 +00006954 getShufflePSHUFHWImmediate(SVOp),
Bruno Cardoso Lopesbbfc3102010-09-04 01:36:45 +00006955 DAG);
6956
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00006957 if (isPSHUFLWMask(M, VT, HasInt256))
Bruno Cardoso Lopesbbfc3102010-09-04 01:36:45 +00006958 return getTargetShuffleNode(X86ISD::PSHUFLW, dl, VT, V1,
Craig Topper5aaffa82012-02-19 02:53:47 +00006959 getShufflePSHUFLWImmediate(SVOp),
Bruno Cardoso Lopesbbfc3102010-09-04 01:36:45 +00006960 DAG);
6961
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00006962 if (isSHUFPMask(M, VT, HasFp256))
Craig Topperb3982da2011-12-31 23:50:21 +00006963 return getTargetShuffleNode(X86ISD::SHUFP, dl, VT, V1, V2,
Craig Topper5aaffa82012-02-19 02:53:47 +00006964 getShuffleSHUFImmediate(SVOp), DAG);
Bruno Cardoso Lopes4c827f52010-09-04 01:22:57 +00006965
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00006966 if (isUNPCKL_v_undef_Mask(M, VT, HasInt256))
Craig Topper34671b82011-12-06 08:21:25 +00006967 return getTargetShuffleNode(X86ISD::UNPCKL, dl, VT, V1, V1, DAG);
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00006968 if (isUNPCKH_v_undef_Mask(M, VT, HasInt256))
Craig Topper34671b82011-12-06 08:21:25 +00006969 return getTargetShuffleNode(X86ISD::UNPCKH, dl, VT, V1, V1, DAG);
Bruno Cardoso Lopesa22c8452010-09-04 00:39:43 +00006970
Bruno Cardoso Lopesd0888342011-07-22 00:14:56 +00006971 //===--------------------------------------------------------------------===//
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00006972 // Generate target specific nodes for 128 or 256-bit shuffles only
6973 // supported in the AVX instruction set.
6974 //
Bruno Cardoso Lopesd0888342011-07-22 00:14:56 +00006975
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00006976 // Handle VMOVDDUPY permutations
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00006977 if (V2IsUndef && isMOVDDUPYMask(M, VT, HasFp256))
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00006978 return getTargetShuffleNode(X86ISD::MOVDDUP, dl, VT, V1, DAG);
6979
Craig Topper70b883b2011-11-28 10:14:51 +00006980 // Handle VPERMILPS/D* permutations
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00006981 if (isVPERMILPMask(M, VT, HasFp256)) {
6982 if (HasInt256 && VT == MVT::v8i32)
Craig Topperdbd98a42012-02-07 06:28:42 +00006983 return getTargetShuffleNode(X86ISD::PSHUFD, dl, VT, V1,
Craig Topper5aaffa82012-02-19 02:53:47 +00006984 getShuffleSHUFImmediate(SVOp), DAG);
Craig Topper316cd2a2011-11-30 06:25:25 +00006985 return getTargetShuffleNode(X86ISD::VPERMILP, dl, VT, V1,
Craig Topper5aaffa82012-02-19 02:53:47 +00006986 getShuffleSHUFImmediate(SVOp), DAG);
Craig Topperdbd98a42012-02-07 06:28:42 +00006987 }
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00006988
Craig Topper70b883b2011-11-28 10:14:51 +00006989 // Handle VPERM2F128/VPERM2I128 permutations
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00006990 if (isVPERM2X128Mask(M, VT, HasFp256))
Craig Topperec24e612011-11-30 07:47:51 +00006991 return getTargetShuffleNode(X86ISD::VPERM2X128, dl, VT, V1,
Craig Topper70b883b2011-11-28 10:14:51 +00006992 V2, getShuffleVPERM2X128Immediate(SVOp), DAG);
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00006993
Craig Topper1842ba02012-04-23 06:38:28 +00006994 SDValue BlendOp = LowerVECTOR_SHUFFLEtoBlend(SVOp, Subtarget, DAG);
Nadav Roteme80aa7c2012-04-09 08:33:21 +00006995 if (BlendOp.getNode())
6996 return BlendOp;
Craig Topper095c5282012-04-15 23:48:57 +00006997
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00006998 if (V2IsUndef && HasInt256 && (VT == MVT::v8i32 || VT == MVT::v8f32)) {
Craig Topper095c5282012-04-15 23:48:57 +00006999 SmallVector<SDValue, 8> permclMask;
Elena Demikhovsky73c504a2012-04-15 11:18:59 +00007000 for (unsigned i = 0; i != 8; ++i) {
Craig Topper095c5282012-04-15 23:48:57 +00007001 permclMask.push_back(DAG.getConstant((M[i]>=0) ? M[i] : 0, MVT::i32));
Elena Demikhovsky73c504a2012-04-15 11:18:59 +00007002 }
Craig Topper92040742012-04-16 06:43:40 +00007003 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v8i32,
7004 &permclMask[0], 8);
7005 // Bitcast is for VPERMPS since mask is v8i32 but node takes v8f32
Craig Topper8325c112012-04-16 00:41:45 +00007006 return DAG.getNode(X86ISD::VPERMV, dl, VT,
Craig Topper92040742012-04-16 06:43:40 +00007007 DAG.getNode(ISD::BITCAST, dl, VT, Mask), V1);
Elena Demikhovsky73c504a2012-04-15 11:18:59 +00007008 }
Craig Topper095c5282012-04-15 23:48:57 +00007009
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00007010 if (V2IsUndef && HasInt256 && (VT == MVT::v4i64 || VT == MVT::v4f64))
Craig Topper8325c112012-04-16 00:41:45 +00007011 return getTargetShuffleNode(X86ISD::VPERMI, dl, VT, V1,
Elena Demikhovsky73c504a2012-04-15 11:18:59 +00007012 getShuffleCLImmediate(SVOp), DAG);
7013
Nadav Roteme80aa7c2012-04-09 08:33:21 +00007014
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00007015 //===--------------------------------------------------------------------===//
7016 // Since no target specific shuffle was selected for this generic one,
7017 // lower it into other known shuffles. FIXME: this isn't true yet, but
7018 // this is the plan.
7019 //
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00007020
Bruno Cardoso Lopes9b4ad122011-07-27 00:56:37 +00007021 // Handle v8i16 specifically since SSE can do byte extraction and insertion.
7022 if (VT == MVT::v8i16) {
Craig Topper55b24052012-09-11 06:15:32 +00007023 SDValue NewOp = LowerVECTOR_SHUFFLEv8i16(Op, Subtarget, DAG);
Bruno Cardoso Lopes9b4ad122011-07-27 00:56:37 +00007024 if (NewOp.getNode())
7025 return NewOp;
7026 }
7027
7028 if (VT == MVT::v16i8) {
7029 SDValue NewOp = LowerVECTOR_SHUFFLEv16i8(SVOp, DAG, *this);
7030 if (NewOp.getNode())
7031 return NewOp;
7032 }
7033
Elena Demikhovsky41789462012-09-06 12:42:01 +00007034 if (VT == MVT::v32i8) {
Craig Topper55b24052012-09-11 06:15:32 +00007035 SDValue NewOp = LowerVECTOR_SHUFFLEv32i8(SVOp, Subtarget, DAG);
Elena Demikhovsky41789462012-09-06 12:42:01 +00007036 if (NewOp.getNode())
7037 return NewOp;
7038 }
7039
Bruno Cardoso Lopes9b4ad122011-07-27 00:56:37 +00007040 // Handle all 128-bit wide vectors with 4 elements, and match them with
7041 // several different shuffle types.
Craig Topper7a9a28b2012-08-12 02:23:29 +00007042 if (NumElems == 4 && VT.is128BitVector())
Bruno Cardoso Lopes9b4ad122011-07-27 00:56:37 +00007043 return LowerVECTOR_SHUFFLE_128v4(SVOp, DAG);
7044
Bruno Cardoso Lopesd0888342011-07-22 00:14:56 +00007045 // Handle general 256-bit shuffles
7046 if (VT.is256BitVector())
7047 return LowerVECTOR_SHUFFLE_256(SVOp, DAG);
7048
Dan Gohman475871a2008-07-27 21:46:04 +00007049 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007050}
7051
Dan Gohman475871a2008-07-27 21:46:04 +00007052SDValue
7053X86TargetLowering::LowerEXTRACT_VECTOR_ELT_SSE4(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00007054 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00007055 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007056 DebugLoc dl = Op.getDebugLoc();
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007057
Craig Topper7a9a28b2012-08-12 02:23:29 +00007058 if (!Op.getOperand(0).getValueType().is128BitVector())
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007059 return SDValue();
7060
Duncan Sands83ec4b62008-06-06 12:08:01 +00007061 if (VT.getSizeInBits() == 8) {
Owen Anderson825b72b2009-08-11 20:47:22 +00007062 SDValue Extract = DAG.getNode(X86ISD::PEXTRB, dl, MVT::i32,
Craig Topper7c022842012-09-12 06:20:41 +00007063 Op.getOperand(0), Op.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00007064 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Extract,
Craig Topper7c022842012-09-12 06:20:41 +00007065 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00007066 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Craig Topper69947b92012-04-23 06:57:04 +00007067 }
7068
7069 if (VT.getSizeInBits() == 16) {
Evan Cheng52ceafa2009-01-02 05:29:08 +00007070 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
7071 // If Idx is 0, it's cheaper to do a move instead of a pextrw.
7072 if (Idx == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00007073 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i16,
7074 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007075 DAG.getNode(ISD::BITCAST, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00007076 MVT::v4i32,
Evan Cheng52ceafa2009-01-02 05:29:08 +00007077 Op.getOperand(0)),
7078 Op.getOperand(1)));
Owen Anderson825b72b2009-08-11 20:47:22 +00007079 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, dl, MVT::i32,
Craig Topper7c022842012-09-12 06:20:41 +00007080 Op.getOperand(0), Op.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00007081 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Extract,
Craig Topper7c022842012-09-12 06:20:41 +00007082 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00007083 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Craig Topper69947b92012-04-23 06:57:04 +00007084 }
7085
7086 if (VT == MVT::f32) {
Evan Cheng62a3f152008-03-24 21:52:23 +00007087 // EXTRACTPS outputs to a GPR32 register which will require a movd to copy
7088 // the result back to FR32 register. It's only worth matching if the
Dan Gohmand17cfbe2008-10-31 00:57:24 +00007089 // result has a single use which is a store or a bitcast to i32. And in
7090 // the case of a store, it's not worth it if the index is a constant 0,
7091 // because a MOVSSmr can be used instead, which is smaller and faster.
Evan Cheng62a3f152008-03-24 21:52:23 +00007092 if (!Op.hasOneUse())
Dan Gohman475871a2008-07-27 21:46:04 +00007093 return SDValue();
Gabor Greifba36cb52008-08-28 21:40:38 +00007094 SDNode *User = *Op.getNode()->use_begin();
Dan Gohmand17cfbe2008-10-31 00:57:24 +00007095 if ((User->getOpcode() != ISD::STORE ||
7096 (isa<ConstantSDNode>(Op.getOperand(1)) &&
7097 cast<ConstantSDNode>(Op.getOperand(1))->isNullValue())) &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007098 (User->getOpcode() != ISD::BITCAST ||
Owen Anderson825b72b2009-08-11 20:47:22 +00007099 User->getValueType(0) != MVT::i32))
Dan Gohman475871a2008-07-27 21:46:04 +00007100 return SDValue();
Owen Anderson825b72b2009-08-11 20:47:22 +00007101 SDValue Extract = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007102 DAG.getNode(ISD::BITCAST, dl, MVT::v4i32,
Dale Johannesenace16102009-02-03 19:33:06 +00007103 Op.getOperand(0)),
7104 Op.getOperand(1));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007105 return DAG.getNode(ISD::BITCAST, dl, MVT::f32, Extract);
Craig Topper69947b92012-04-23 06:57:04 +00007106 }
7107
7108 if (VT == MVT::i32 || VT == MVT::i64) {
Pete Coopera77214a2011-11-14 19:38:42 +00007109 // ExtractPS/pextrq works with constant index.
Mon P Wangf0fcdd82009-01-15 21:10:20 +00007110 if (isa<ConstantSDNode>(Op.getOperand(1)))
7111 return Op;
Nate Begeman14d12ca2008-02-11 04:19:36 +00007112 }
Dan Gohman475871a2008-07-27 21:46:04 +00007113 return SDValue();
Nate Begeman14d12ca2008-02-11 04:19:36 +00007114}
7115
7116
Dan Gohman475871a2008-07-27 21:46:04 +00007117SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007118X86TargetLowering::LowerEXTRACT_VECTOR_ELT(SDValue Op,
7119 SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00007120 if (!isa<ConstantSDNode>(Op.getOperand(1)))
Dan Gohman475871a2008-07-27 21:46:04 +00007121 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007122
David Greene74a579d2011-02-10 16:57:36 +00007123 SDValue Vec = Op.getOperand(0);
7124 EVT VecVT = Vec.getValueType();
7125
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007126 // If this is a 256-bit vector result, first extract the 128-bit vector and
7127 // then extract the element from the 128-bit vector.
Craig Topper7a9a28b2012-08-12 02:23:29 +00007128 if (VecVT.is256BitVector()) {
David Greene74a579d2011-02-10 16:57:36 +00007129 DebugLoc dl = Op.getNode()->getDebugLoc();
7130 unsigned NumElems = VecVT.getVectorNumElements();
7131 SDValue Idx = Op.getOperand(1);
David Greene74a579d2011-02-10 16:57:36 +00007132 unsigned IdxVal = cast<ConstantSDNode>(Idx)->getZExtValue();
7133
7134 // Get the 128-bit vector.
Craig Topper7d1e3dc2012-04-30 05:17:10 +00007135 Vec = Extract128BitVector(Vec, IdxVal, DAG, dl);
David Greene74a579d2011-02-10 16:57:36 +00007136
Craig Topper7d1e3dc2012-04-30 05:17:10 +00007137 if (IdxVal >= NumElems/2)
7138 IdxVal -= NumElems/2;
David Greene74a579d2011-02-10 16:57:36 +00007139 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, Op.getValueType(), Vec,
Craig Topper7d1e3dc2012-04-30 05:17:10 +00007140 DAG.getConstant(IdxVal, MVT::i32));
David Greene74a579d2011-02-10 16:57:36 +00007141 }
7142
Craig Topper7a9a28b2012-08-12 02:23:29 +00007143 assert(VecVT.is128BitVector() && "Unexpected vector length");
David Greene74a579d2011-02-10 16:57:36 +00007144
Craig Topperd0a31172012-01-10 06:37:29 +00007145 if (Subtarget->hasSSE41()) {
Dan Gohman475871a2008-07-27 21:46:04 +00007146 SDValue Res = LowerEXTRACT_VECTOR_ELT_SSE4(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00007147 if (Res.getNode())
Evan Cheng62a3f152008-03-24 21:52:23 +00007148 return Res;
7149 }
Nate Begeman14d12ca2008-02-11 04:19:36 +00007150
Owen Andersone50ed302009-08-10 22:56:29 +00007151 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007152 DebugLoc dl = Op.getDebugLoc();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007153 // TODO: handle v16i8.
Duncan Sands83ec4b62008-06-06 12:08:01 +00007154 if (VT.getSizeInBits() == 16) {
Dan Gohman475871a2008-07-27 21:46:04 +00007155 SDValue Vec = Op.getOperand(0);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007156 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00007157 if (Idx == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00007158 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i16,
7159 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007160 DAG.getNode(ISD::BITCAST, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00007161 MVT::v4i32, Vec),
Evan Cheng14b32e12007-12-11 01:46:18 +00007162 Op.getOperand(1)));
Evan Cheng0db9fe62006-04-25 20:13:52 +00007163 // Transform it so it match pextrw which produces a 32-bit result.
Ken Dyck70d0ef12009-12-17 15:31:52 +00007164 EVT EltVT = MVT::i32;
Dan Gohman8a55ce42009-09-23 21:02:20 +00007165 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, dl, EltVT,
Craig Topper7c022842012-09-12 06:20:41 +00007166 Op.getOperand(0), Op.getOperand(1));
Dan Gohman8a55ce42009-09-23 21:02:20 +00007167 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, EltVT, Extract,
Craig Topper7c022842012-09-12 06:20:41 +00007168 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00007169 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Craig Topper69947b92012-04-23 06:57:04 +00007170 }
7171
7172 if (VT.getSizeInBits() == 32) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007173 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007174 if (Idx == 0)
7175 return Op;
Eric Christopherfd179292009-08-27 18:07:15 +00007176
Evan Cheng0db9fe62006-04-25 20:13:52 +00007177 // SHUFPS the element to the lowest double word, then movss.
Jeffrey Yasskina44defe2011-07-27 06:22:51 +00007178 int Mask[4] = { static_cast<int>(Idx), -1, -1, -1 };
Owen Andersone50ed302009-08-10 22:56:29 +00007179 EVT VVT = Op.getOperand(0).getValueType();
Eric Christopherfd179292009-08-27 18:07:15 +00007180 SDValue Vec = DAG.getVectorShuffle(VVT, dl, Op.getOperand(0),
Nate Begeman9008ca62009-04-27 18:41:29 +00007181 DAG.getUNDEF(VVT), Mask);
Dale Johannesenace16102009-02-03 19:33:06 +00007182 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, Vec,
Chris Lattner0bd48932008-01-17 07:00:52 +00007183 DAG.getIntPtrConstant(0));
Craig Topper69947b92012-04-23 06:57:04 +00007184 }
7185
7186 if (VT.getSizeInBits() == 64) {
Nate Begeman14d12ca2008-02-11 04:19:36 +00007187 // FIXME: .td only matches this for <2 x f64>, not <2 x i64> on 32b
7188 // FIXME: seems like this should be unnecessary if mov{h,l}pd were taught
7189 // to match extract_elt for f64.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007190 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007191 if (Idx == 0)
7192 return Op;
7193
7194 // UNPCKHPD the element to the lowest double word, then movsd.
7195 // Note if the lower 64 bits of the result of the UNPCKHPD is then stored
7196 // to a f64mem, the whole operation is folded into a single MOVHPDmr.
Nate Begeman9008ca62009-04-27 18:41:29 +00007197 int Mask[2] = { 1, -1 };
Owen Andersone50ed302009-08-10 22:56:29 +00007198 EVT VVT = Op.getOperand(0).getValueType();
Eric Christopherfd179292009-08-27 18:07:15 +00007199 SDValue Vec = DAG.getVectorShuffle(VVT, dl, Op.getOperand(0),
Nate Begeman9008ca62009-04-27 18:41:29 +00007200 DAG.getUNDEF(VVT), Mask);
Dale Johannesenace16102009-02-03 19:33:06 +00007201 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, Vec,
Chris Lattner0bd48932008-01-17 07:00:52 +00007202 DAG.getIntPtrConstant(0));
Evan Cheng0db9fe62006-04-25 20:13:52 +00007203 }
7204
Dan Gohman475871a2008-07-27 21:46:04 +00007205 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007206}
7207
Dan Gohman475871a2008-07-27 21:46:04 +00007208SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007209X86TargetLowering::LowerINSERT_VECTOR_ELT_SSE4(SDValue Op,
7210 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00007211 EVT VT = Op.getValueType();
Dan Gohman8a55ce42009-09-23 21:02:20 +00007212 EVT EltVT = VT.getVectorElementType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007213 DebugLoc dl = Op.getDebugLoc();
Nate Begeman14d12ca2008-02-11 04:19:36 +00007214
Dan Gohman475871a2008-07-27 21:46:04 +00007215 SDValue N0 = Op.getOperand(0);
7216 SDValue N1 = Op.getOperand(1);
7217 SDValue N2 = Op.getOperand(2);
Nate Begeman14d12ca2008-02-11 04:19:36 +00007218
Craig Topper7a9a28b2012-08-12 02:23:29 +00007219 if (!VT.is128BitVector())
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007220 return SDValue();
7221
Dan Gohman8a55ce42009-09-23 21:02:20 +00007222 if ((EltVT.getSizeInBits() == 8 || EltVT.getSizeInBits() == 16) &&
Dan Gohmanef521f12008-08-14 22:53:18 +00007223 isa<ConstantSDNode>(N2)) {
Chris Lattner8f2b4cc2010-02-23 02:07:48 +00007224 unsigned Opc;
7225 if (VT == MVT::v8i16)
7226 Opc = X86ISD::PINSRW;
Chris Lattner8f2b4cc2010-02-23 02:07:48 +00007227 else if (VT == MVT::v16i8)
7228 Opc = X86ISD::PINSRB;
7229 else
7230 Opc = X86ISD::PINSRB;
7231
Nate Begeman14d12ca2008-02-11 04:19:36 +00007232 // Transform it so it match pinsr{b,w} which expects a GR32 as its second
7233 // argument.
Owen Anderson825b72b2009-08-11 20:47:22 +00007234 if (N1.getValueType() != MVT::i32)
7235 N1 = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, N1);
7236 if (N2.getValueType() != MVT::i32)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007237 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Dale Johannesenace16102009-02-03 19:33:06 +00007238 return DAG.getNode(Opc, dl, VT, N0, N1, N2);
Craig Topper69947b92012-04-23 06:57:04 +00007239 }
7240
7241 if (EltVT == MVT::f32 && isa<ConstantSDNode>(N2)) {
Nate Begeman14d12ca2008-02-11 04:19:36 +00007242 // Bits [7:6] of the constant are the source select. This will always be
7243 // zero here. The DAG Combiner may combine an extract_elt index into these
7244 // bits. For example (insert (extract, 3), 2) could be matched by putting
7245 // the '3' into bits [7:6] of X86ISD::INSERTPS.
Scott Michelfdc40a02009-02-17 22:15:04 +00007246 // Bits [5:4] of the constant are the destination select. This is the
Nate Begeman14d12ca2008-02-11 04:19:36 +00007247 // value of the incoming immediate.
Scott Michelfdc40a02009-02-17 22:15:04 +00007248 // Bits [3:0] of the constant are the zero mask. The DAG Combiner may
Nate Begeman14d12ca2008-02-11 04:19:36 +00007249 // combine either bitwise AND or insert of float 0.0 to set these bits.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007250 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue() << 4);
Eric Christopherfbd66872009-07-24 00:33:09 +00007251 // Create this as a scalar to vector..
Owen Anderson825b72b2009-08-11 20:47:22 +00007252 N1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4f32, N1);
Dale Johannesenace16102009-02-03 19:33:06 +00007253 return DAG.getNode(X86ISD::INSERTPS, dl, VT, N0, N1, N2);
Craig Topper69947b92012-04-23 06:57:04 +00007254 }
7255
7256 if ((EltVT == MVT::i32 || EltVT == MVT::i64) && isa<ConstantSDNode>(N2)) {
Eric Christopherfbd66872009-07-24 00:33:09 +00007257 // PINSR* works with constant index.
7258 return Op;
Nate Begeman14d12ca2008-02-11 04:19:36 +00007259 }
Dan Gohman475871a2008-07-27 21:46:04 +00007260 return SDValue();
Nate Begeman14d12ca2008-02-11 04:19:36 +00007261}
7262
Dan Gohman475871a2008-07-27 21:46:04 +00007263SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007264X86TargetLowering::LowerINSERT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00007265 EVT VT = Op.getValueType();
Dan Gohman8a55ce42009-09-23 21:02:20 +00007266 EVT EltVT = VT.getVectorElementType();
Nate Begeman14d12ca2008-02-11 04:19:36 +00007267
David Greene6b381262011-02-09 15:32:06 +00007268 DebugLoc dl = Op.getDebugLoc();
7269 SDValue N0 = Op.getOperand(0);
7270 SDValue N1 = Op.getOperand(1);
7271 SDValue N2 = Op.getOperand(2);
7272
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007273 // If this is a 256-bit vector result, first extract the 128-bit vector,
7274 // insert the element into the extracted half and then place it back.
Craig Topper7a9a28b2012-08-12 02:23:29 +00007275 if (VT.is256BitVector()) {
David Greene6b381262011-02-09 15:32:06 +00007276 if (!isa<ConstantSDNode>(N2))
7277 return SDValue();
7278
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007279 // Get the desired 128-bit vector half.
David Greene6b381262011-02-09 15:32:06 +00007280 unsigned NumElems = VT.getVectorNumElements();
7281 unsigned IdxVal = cast<ConstantSDNode>(N2)->getZExtValue();
Craig Topper7d1e3dc2012-04-30 05:17:10 +00007282 SDValue V = Extract128BitVector(N0, IdxVal, DAG, dl);
David Greene6b381262011-02-09 15:32:06 +00007283
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007284 // Insert the element into the desired half.
Craig Topper7d1e3dc2012-04-30 05:17:10 +00007285 bool Upper = IdxVal >= NumElems/2;
7286 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, V.getValueType(), V, N1,
7287 DAG.getConstant(Upper ? IdxVal-NumElems/2 : IdxVal, MVT::i32));
David Greene6b381262011-02-09 15:32:06 +00007288
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007289 // Insert the changed part back to the 256-bit vector
Craig Topper7d1e3dc2012-04-30 05:17:10 +00007290 return Insert128BitVector(N0, V, IdxVal, DAG, dl);
David Greene6b381262011-02-09 15:32:06 +00007291 }
7292
Craig Topperd0a31172012-01-10 06:37:29 +00007293 if (Subtarget->hasSSE41())
Nate Begeman14d12ca2008-02-11 04:19:36 +00007294 return LowerINSERT_VECTOR_ELT_SSE4(Op, DAG);
7295
Dan Gohman8a55ce42009-09-23 21:02:20 +00007296 if (EltVT == MVT::i8)
Dan Gohman475871a2008-07-27 21:46:04 +00007297 return SDValue();
Evan Cheng794405e2007-12-12 07:55:34 +00007298
Dan Gohman8a55ce42009-09-23 21:02:20 +00007299 if (EltVT.getSizeInBits() == 16 && isa<ConstantSDNode>(N2)) {
Evan Cheng794405e2007-12-12 07:55:34 +00007300 // Transform it so it match pinsrw which expects a 16-bit value in a GR32
7301 // as its second argument.
Owen Anderson825b72b2009-08-11 20:47:22 +00007302 if (N1.getValueType() != MVT::i32)
7303 N1 = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, N1);
7304 if (N2.getValueType() != MVT::i32)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007305 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Dale Johannesen0488fb62010-09-30 23:57:10 +00007306 return DAG.getNode(X86ISD::PINSRW, dl, VT, N0, N1, N2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007307 }
Dan Gohman475871a2008-07-27 21:46:04 +00007308 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007309}
7310
Craig Topper55b24052012-09-11 06:15:32 +00007311static SDValue LowerSCALAR_TO_VECTOR(SDValue Op, SelectionDAG &DAG) {
Bruno Cardoso Lopes233fa392011-07-25 23:05:16 +00007312 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007313 DebugLoc dl = Op.getDebugLoc();
David Greene2fcdfb42011-02-10 23:11:29 +00007314 EVT OpVT = Op.getValueType();
7315
Bruno Cardoso Lopes233fa392011-07-25 23:05:16 +00007316 // If this is a 256-bit vector result, first insert into a 128-bit
7317 // vector and then insert into the 256-bit vector.
Craig Topper7a9a28b2012-08-12 02:23:29 +00007318 if (!OpVT.is128BitVector()) {
Bruno Cardoso Lopes233fa392011-07-25 23:05:16 +00007319 // Insert into a 128-bit vector.
7320 EVT VT128 = EVT::getVectorVT(*Context,
7321 OpVT.getVectorElementType(),
7322 OpVT.getVectorNumElements() / 2);
7323
7324 Op = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT128, Op.getOperand(0));
7325
7326 // Insert the 128-bit vector.
Craig Topperb14940a2012-04-22 20:55:18 +00007327 return Insert128BitVector(DAG.getUNDEF(OpVT), Op, 0, DAG, dl);
Bruno Cardoso Lopes233fa392011-07-25 23:05:16 +00007328 }
7329
Craig Topperd77d2fe2012-04-29 20:22:05 +00007330 if (OpVT == MVT::v1i64 &&
Chris Lattnerf172ecd2010-07-04 23:07:25 +00007331 Op.getOperand(0).getValueType() == MVT::i64)
Owen Anderson825b72b2009-08-11 20:47:22 +00007332 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v1i64, Op.getOperand(0));
Rafael Espindoladef390a2009-08-03 02:45:34 +00007333
Owen Anderson825b72b2009-08-11 20:47:22 +00007334 SDValue AnyExt = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, Op.getOperand(0));
Craig Topper7a9a28b2012-08-12 02:23:29 +00007335 assert(OpVT.is128BitVector() && "Expected an SSE type!");
Craig Topperd77d2fe2012-04-29 20:22:05 +00007336 return DAG.getNode(ISD::BITCAST, dl, OpVT,
Dale Johannesen0488fb62010-09-30 23:57:10 +00007337 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,AnyExt));
Evan Cheng0db9fe62006-04-25 20:13:52 +00007338}
7339
David Greene91585092011-01-26 15:38:49 +00007340// Lower a node with an EXTRACT_SUBVECTOR opcode. This may result in
7341// a simple subregister reference or explicit instructions to grab
7342// upper bits of a vector.
Craig Topper55b24052012-09-11 06:15:32 +00007343static SDValue LowerEXTRACT_SUBVECTOR(SDValue Op, const X86Subtarget *Subtarget,
7344 SelectionDAG &DAG) {
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00007345 if (Subtarget->hasFp256()) {
David Greenea5f26012011-02-07 19:36:54 +00007346 DebugLoc dl = Op.getNode()->getDebugLoc();
7347 SDValue Vec = Op.getNode()->getOperand(0);
7348 SDValue Idx = Op.getNode()->getOperand(1);
7349
Craig Topper7a9a28b2012-08-12 02:23:29 +00007350 if (Op.getNode()->getValueType(0).is128BitVector() &&
7351 Vec.getNode()->getValueType(0).is256BitVector() &&
Craig Topperb14940a2012-04-22 20:55:18 +00007352 isa<ConstantSDNode>(Idx)) {
7353 unsigned IdxVal = cast<ConstantSDNode>(Idx)->getZExtValue();
7354 return Extract128BitVector(Vec, IdxVal, DAG, dl);
David Greenea5f26012011-02-07 19:36:54 +00007355 }
David Greene91585092011-01-26 15:38:49 +00007356 }
7357 return SDValue();
7358}
7359
David Greenecfe33c42011-01-26 19:13:22 +00007360// Lower a node with an INSERT_SUBVECTOR opcode. This may result in a
7361// simple superregister reference or explicit instructions to insert
7362// the upper bits of a vector.
Craig Topper55b24052012-09-11 06:15:32 +00007363static SDValue LowerINSERT_SUBVECTOR(SDValue Op, const X86Subtarget *Subtarget,
7364 SelectionDAG &DAG) {
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00007365 if (Subtarget->hasFp256()) {
David Greenecfe33c42011-01-26 19:13:22 +00007366 DebugLoc dl = Op.getNode()->getDebugLoc();
7367 SDValue Vec = Op.getNode()->getOperand(0);
7368 SDValue SubVec = Op.getNode()->getOperand(1);
7369 SDValue Idx = Op.getNode()->getOperand(2);
7370
Craig Topper7a9a28b2012-08-12 02:23:29 +00007371 if (Op.getNode()->getValueType(0).is256BitVector() &&
7372 SubVec.getNode()->getValueType(0).is128BitVector() &&
Craig Topperb14940a2012-04-22 20:55:18 +00007373 isa<ConstantSDNode>(Idx)) {
7374 unsigned IdxVal = cast<ConstantSDNode>(Idx)->getZExtValue();
7375 return Insert128BitVector(Vec, SubVec, IdxVal, DAG, dl);
David Greenecfe33c42011-01-26 19:13:22 +00007376 }
7377 }
7378 return SDValue();
7379}
7380
Bill Wendling056292f2008-09-16 21:48:12 +00007381// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
7382// their target countpart wrapped in the X86ISD::Wrapper node. Suppose N is
7383// one of the above mentioned nodes. It has to be wrapped because otherwise
7384// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
7385// be used to form addressing mode. These wrapped nodes will be selected
7386// into MOV32ri.
Dan Gohman475871a2008-07-27 21:46:04 +00007387SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007388X86TargetLowering::LowerConstantPool(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00007389 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Eric Christopherfd179292009-08-27 18:07:15 +00007390
Chris Lattner41621a22009-06-26 19:22:52 +00007391 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
7392 // global base reg.
7393 unsigned char OpFlag = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00007394 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007395 CodeModel::Model M = getTargetMachine().getCodeModel();
7396
Chris Lattner4f066492009-07-11 20:29:19 +00007397 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007398 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattnere4df7562009-07-09 03:15:51 +00007399 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00007400 else if (Subtarget->isPICStyleGOT())
Chris Lattner88e1fd52009-07-09 04:24:46 +00007401 OpFlag = X86II::MO_GOTOFF;
Chris Lattnere2c92082009-07-10 21:00:45 +00007402 else if (Subtarget->isPICStyleStubPIC())
Chris Lattner88e1fd52009-07-09 04:24:46 +00007403 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Eric Christopherfd179292009-08-27 18:07:15 +00007404
Evan Cheng1606e8e2009-03-13 07:51:59 +00007405 SDValue Result = DAG.getTargetConstantPool(CP->getConstVal(), getPointerTy(),
Chris Lattner41621a22009-06-26 19:22:52 +00007406 CP->getAlignment(),
7407 CP->getOffset(), OpFlag);
7408 DebugLoc DL = CP->getDebugLoc();
Chris Lattner18c59872009-06-27 04:16:01 +00007409 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00007410 // With PIC, the address is actually $g + Offset.
Chris Lattner41621a22009-06-26 19:22:52 +00007411 if (OpFlag) {
7412 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesenb300d2a2009-02-07 00:55:49 +00007413 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00007414 DebugLoc(), getPointerTy()),
Anton Korobeynikov7f705592007-01-12 19:20:47 +00007415 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007416 }
7417
7418 return Result;
7419}
7420
Dan Gohmand858e902010-04-17 15:26:15 +00007421SDValue X86TargetLowering::LowerJumpTable(SDValue Op, SelectionDAG &DAG) const {
Chris Lattner18c59872009-06-27 04:16:01 +00007422 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
Eric Christopherfd179292009-08-27 18:07:15 +00007423
Chris Lattner18c59872009-06-27 04:16:01 +00007424 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
7425 // global base reg.
7426 unsigned char OpFlag = 0;
7427 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007428 CodeModel::Model M = getTargetMachine().getCodeModel();
7429
Chris Lattner4f066492009-07-11 20:29:19 +00007430 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007431 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattnere4df7562009-07-09 03:15:51 +00007432 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00007433 else if (Subtarget->isPICStyleGOT())
Chris Lattner88e1fd52009-07-09 04:24:46 +00007434 OpFlag = X86II::MO_GOTOFF;
Chris Lattnere2c92082009-07-10 21:00:45 +00007435 else if (Subtarget->isPICStyleStubPIC())
Chris Lattner88e1fd52009-07-09 04:24:46 +00007436 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Eric Christopherfd179292009-08-27 18:07:15 +00007437
Chris Lattner18c59872009-06-27 04:16:01 +00007438 SDValue Result = DAG.getTargetJumpTable(JT->getIndex(), getPointerTy(),
7439 OpFlag);
7440 DebugLoc DL = JT->getDebugLoc();
7441 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Eric Christopherfd179292009-08-27 18:07:15 +00007442
Chris Lattner18c59872009-06-27 04:16:01 +00007443 // With PIC, the address is actually $g + Offset.
Chris Lattner1e61e692010-11-15 02:46:57 +00007444 if (OpFlag)
Chris Lattner18c59872009-06-27 04:16:01 +00007445 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
7446 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00007447 DebugLoc(), getPointerTy()),
Chris Lattner18c59872009-06-27 04:16:01 +00007448 Result);
Eric Christopherfd179292009-08-27 18:07:15 +00007449
Chris Lattner18c59872009-06-27 04:16:01 +00007450 return Result;
7451}
7452
7453SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007454X86TargetLowering::LowerExternalSymbol(SDValue Op, SelectionDAG &DAG) const {
Chris Lattner18c59872009-06-27 04:16:01 +00007455 const char *Sym = cast<ExternalSymbolSDNode>(Op)->getSymbol();
Eric Christopherfd179292009-08-27 18:07:15 +00007456
Chris Lattner18c59872009-06-27 04:16:01 +00007457 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
7458 // global base reg.
7459 unsigned char OpFlag = 0;
7460 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007461 CodeModel::Model M = getTargetMachine().getCodeModel();
7462
Chris Lattner4f066492009-07-11 20:29:19 +00007463 if (Subtarget->isPICStyleRIPRel() &&
Eli Friedman586272d2011-08-11 01:48:05 +00007464 (M == CodeModel::Small || M == CodeModel::Kernel)) {
7465 if (Subtarget->isTargetDarwin() || Subtarget->isTargetELF())
7466 OpFlag = X86II::MO_GOTPCREL;
Chris Lattnere4df7562009-07-09 03:15:51 +00007467 WrapperKind = X86ISD::WrapperRIP;
Eli Friedman586272d2011-08-11 01:48:05 +00007468 } else if (Subtarget->isPICStyleGOT()) {
7469 OpFlag = X86II::MO_GOT;
7470 } else if (Subtarget->isPICStyleStubPIC()) {
7471 OpFlag = X86II::MO_DARWIN_NONLAZY_PIC_BASE;
7472 } else if (Subtarget->isPICStyleStubNoDynamic()) {
7473 OpFlag = X86II::MO_DARWIN_NONLAZY;
7474 }
Eric Christopherfd179292009-08-27 18:07:15 +00007475
Chris Lattner18c59872009-06-27 04:16:01 +00007476 SDValue Result = DAG.getTargetExternalSymbol(Sym, getPointerTy(), OpFlag);
Eric Christopherfd179292009-08-27 18:07:15 +00007477
Chris Lattner18c59872009-06-27 04:16:01 +00007478 DebugLoc DL = Op.getDebugLoc();
7479 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Eric Christopherfd179292009-08-27 18:07:15 +00007480
7481
Chris Lattner18c59872009-06-27 04:16:01 +00007482 // With PIC, the address is actually $g + Offset.
7483 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
Chris Lattnere4df7562009-07-09 03:15:51 +00007484 !Subtarget->is64Bit()) {
Chris Lattner18c59872009-06-27 04:16:01 +00007485 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
7486 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00007487 DebugLoc(), getPointerTy()),
Chris Lattner18c59872009-06-27 04:16:01 +00007488 Result);
7489 }
Eric Christopherfd179292009-08-27 18:07:15 +00007490
Eli Friedman586272d2011-08-11 01:48:05 +00007491 // For symbols that require a load from a stub to get the address, emit the
7492 // load.
7493 if (isGlobalStubReference(OpFlag))
7494 Result = DAG.getLoad(getPointerTy(), DL, DAG.getEntryNode(), Result,
Pete Cooperd752e0f2011-11-08 18:42:53 +00007495 MachinePointerInfo::getGOT(), false, false, false, 0);
Eli Friedman586272d2011-08-11 01:48:05 +00007496
Chris Lattner18c59872009-06-27 04:16:01 +00007497 return Result;
7498}
7499
Dan Gohman475871a2008-07-27 21:46:04 +00007500SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007501X86TargetLowering::LowerBlockAddress(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman29cbade2009-11-20 23:18:13 +00007502 // Create the TargetBlockAddressAddress node.
7503 unsigned char OpFlags =
7504 Subtarget->ClassifyBlockAddressReference();
Dan Gohmanf705adb2009-10-30 01:28:02 +00007505 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman46510a72010-04-15 01:51:59 +00007506 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
Michael Liao6c7ccaa2012-09-12 21:43:09 +00007507 int64_t Offset = cast<BlockAddressSDNode>(Op)->getOffset();
Dan Gohman29cbade2009-11-20 23:18:13 +00007508 DebugLoc dl = Op.getDebugLoc();
Michael Liao6c7ccaa2012-09-12 21:43:09 +00007509 SDValue Result = DAG.getTargetBlockAddress(BA, getPointerTy(), Offset,
7510 OpFlags);
Dan Gohman29cbade2009-11-20 23:18:13 +00007511
Dan Gohmanf705adb2009-10-30 01:28:02 +00007512 if (Subtarget->isPICStyleRIPRel() &&
7513 (M == CodeModel::Small || M == CodeModel::Kernel))
Dan Gohman29cbade2009-11-20 23:18:13 +00007514 Result = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Result);
7515 else
7516 Result = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), Result);
Dan Gohmanf705adb2009-10-30 01:28:02 +00007517
Dan Gohman29cbade2009-11-20 23:18:13 +00007518 // With PIC, the address is actually $g + Offset.
7519 if (isGlobalRelativeToPICBase(OpFlags)) {
7520 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(),
7521 DAG.getNode(X86ISD::GlobalBaseReg, dl, getPointerTy()),
7522 Result);
7523 }
Dan Gohmanf705adb2009-10-30 01:28:02 +00007524
7525 return Result;
7526}
7527
7528SDValue
Dale Johannesen33c960f2009-02-04 20:06:27 +00007529X86TargetLowering::LowerGlobalAddress(const GlobalValue *GV, DebugLoc dl,
Dan Gohman6520e202008-10-18 02:06:02 +00007530 int64_t Offset,
Evan Chengda43bcf2008-09-24 00:05:32 +00007531 SelectionDAG &DAG) const {
Dan Gohman6520e202008-10-18 02:06:02 +00007532 // Create the TargetGlobalAddress node, folding in the constant
7533 // offset if it is legal.
Chris Lattnerd392bd92009-07-10 07:20:05 +00007534 unsigned char OpFlags =
7535 Subtarget->ClassifyGlobalReference(GV, getTargetMachine());
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007536 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman6520e202008-10-18 02:06:02 +00007537 SDValue Result;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007538 if (OpFlags == X86II::MO_NO_FLAG &&
7539 X86::isOffsetSuitableForCodeModel(Offset, M)) {
Chris Lattner4aa21aa2009-07-09 00:58:53 +00007540 // A direct static reference to a global.
Devang Patel0d881da2010-07-06 22:08:15 +00007541 Result = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(), Offset);
Dan Gohman6520e202008-10-18 02:06:02 +00007542 Offset = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00007543 } else {
Devang Patel0d881da2010-07-06 22:08:15 +00007544 Result = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(), 0, OpFlags);
Chris Lattner18c59872009-06-27 04:16:01 +00007545 }
Eric Christopherfd179292009-08-27 18:07:15 +00007546
Chris Lattner4f066492009-07-11 20:29:19 +00007547 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007548 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattner18c59872009-06-27 04:16:01 +00007549 Result = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Result);
7550 else
7551 Result = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), Result);
Dan Gohman6520e202008-10-18 02:06:02 +00007552
Anton Korobeynikov7f705592007-01-12 19:20:47 +00007553 // With PIC, the address is actually $g + Offset.
Chris Lattner36c25012009-07-10 07:34:39 +00007554 if (isGlobalRelativeToPICBase(OpFlags)) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00007555 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(),
7556 DAG.getNode(X86ISD::GlobalBaseReg, dl, getPointerTy()),
Anton Korobeynikov7f705592007-01-12 19:20:47 +00007557 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007558 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007559
Chris Lattner36c25012009-07-10 07:34:39 +00007560 // For globals that require a load from a stub to get the address, emit the
7561 // load.
7562 if (isGlobalStubReference(OpFlags))
Dale Johannesen33c960f2009-02-04 20:06:27 +00007563 Result = DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(), Result,
Pete Cooperd752e0f2011-11-08 18:42:53 +00007564 MachinePointerInfo::getGOT(), false, false, false, 0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007565
Dan Gohman6520e202008-10-18 02:06:02 +00007566 // If there was a non-zero offset that we didn't fold, create an explicit
7567 // addition for it.
7568 if (Offset != 0)
Dale Johannesen33c960f2009-02-04 20:06:27 +00007569 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(), Result,
Dan Gohman6520e202008-10-18 02:06:02 +00007570 DAG.getConstant(Offset, getPointerTy()));
7571
Evan Cheng0db9fe62006-04-25 20:13:52 +00007572 return Result;
7573}
7574
Evan Chengda43bcf2008-09-24 00:05:32 +00007575SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007576X86TargetLowering::LowerGlobalAddress(SDValue Op, SelectionDAG &DAG) const {
Evan Chengda43bcf2008-09-24 00:05:32 +00007577 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Dan Gohman6520e202008-10-18 02:06:02 +00007578 int64_t Offset = cast<GlobalAddressSDNode>(Op)->getOffset();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007579 return LowerGlobalAddress(GV, Op.getDebugLoc(), Offset, DAG);
Evan Chengda43bcf2008-09-24 00:05:32 +00007580}
7581
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007582static SDValue
7583GetTLSADDR(SelectionDAG &DAG, SDValue Chain, GlobalAddressSDNode *GA,
Owen Andersone50ed302009-08-10 22:56:29 +00007584 SDValue *InFlag, const EVT PtrVT, unsigned ReturnReg,
Hans Wennborgf0234fc2012-06-01 16:27:21 +00007585 unsigned char OperandFlags, bool LocalDynamic = false) {
Anton Korobeynikov817a4642009-12-11 19:39:55 +00007586 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00007587 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007588 DebugLoc dl = GA->getDebugLoc();
Devang Patel0d881da2010-07-06 22:08:15 +00007589 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl,
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007590 GA->getValueType(0),
Chris Lattnerb903bed2009-06-26 21:20:29 +00007591 GA->getOffset(),
7592 OperandFlags);
Hans Wennborgf0234fc2012-06-01 16:27:21 +00007593
7594 X86ISD::NodeType CallType = LocalDynamic ? X86ISD::TLSBASEADDR
7595 : X86ISD::TLSADDR;
7596
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007597 if (InFlag) {
7598 SDValue Ops[] = { Chain, TGA, *InFlag };
Hans Wennborgf0234fc2012-06-01 16:27:21 +00007599 Chain = DAG.getNode(CallType, dl, NodeTys, Ops, 3);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007600 } else {
7601 SDValue Ops[] = { Chain, TGA };
Hans Wennborgf0234fc2012-06-01 16:27:21 +00007602 Chain = DAG.getNode(CallType, dl, NodeTys, Ops, 2);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007603 }
Anton Korobeynikov817a4642009-12-11 19:39:55 +00007604
7605 // TLSADDR will be codegen'ed as call. Inform MFI that function has calls.
Bill Wendlingb92187a2010-05-14 21:14:32 +00007606 MFI->setAdjustsStack(true);
Anton Korobeynikov817a4642009-12-11 19:39:55 +00007607
Rafael Espindola15f1b662009-04-24 12:59:40 +00007608 SDValue Flag = Chain.getValue(1);
7609 return DAG.getCopyFromReg(Chain, dl, ReturnReg, PtrVT, Flag);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007610}
7611
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007612// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 32 bit
Dan Gohman475871a2008-07-27 21:46:04 +00007613static SDValue
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007614LowerToTLSGeneralDynamicModel32(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00007615 const EVT PtrVT) {
Dan Gohman475871a2008-07-27 21:46:04 +00007616 SDValue InFlag;
Dale Johannesendd64c412009-02-04 00:33:20 +00007617 DebugLoc dl = GA->getDebugLoc(); // ? function entry point might be better
7618 SDValue Chain = DAG.getCopyToReg(DAG.getEntryNode(), dl, X86::EBX,
Craig Topper7c022842012-09-12 06:20:41 +00007619 DAG.getNode(X86ISD::GlobalBaseReg,
7620 DebugLoc(), PtrVT), InFlag);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007621 InFlag = Chain.getValue(1);
7622
Chris Lattnerb903bed2009-06-26 21:20:29 +00007623 return GetTLSADDR(DAG, Chain, GA, &InFlag, PtrVT, X86::EAX, X86II::MO_TLSGD);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007624}
7625
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007626// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 64 bit
Dan Gohman475871a2008-07-27 21:46:04 +00007627static SDValue
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007628LowerToTLSGeneralDynamicModel64(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00007629 const EVT PtrVT) {
Chris Lattnerb903bed2009-06-26 21:20:29 +00007630 return GetTLSADDR(DAG, DAG.getEntryNode(), GA, NULL, PtrVT,
7631 X86::RAX, X86II::MO_TLSGD);
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007632}
7633
Hans Wennborgf0234fc2012-06-01 16:27:21 +00007634static SDValue LowerToTLSLocalDynamicModel(GlobalAddressSDNode *GA,
7635 SelectionDAG &DAG,
7636 const EVT PtrVT,
7637 bool is64Bit) {
7638 DebugLoc dl = GA->getDebugLoc();
7639
7640 // Get the start address of the TLS block for this module.
7641 X86MachineFunctionInfo* MFI = DAG.getMachineFunction()
7642 .getInfo<X86MachineFunctionInfo>();
7643 MFI->incNumLocalDynamicTLSAccesses();
7644
7645 SDValue Base;
7646 if (is64Bit) {
7647 Base = GetTLSADDR(DAG, DAG.getEntryNode(), GA, NULL, PtrVT, X86::RAX,
7648 X86II::MO_TLSLD, /*LocalDynamic=*/true);
7649 } else {
7650 SDValue InFlag;
7651 SDValue Chain = DAG.getCopyToReg(DAG.getEntryNode(), dl, X86::EBX,
7652 DAG.getNode(X86ISD::GlobalBaseReg, DebugLoc(), PtrVT), InFlag);
7653 InFlag = Chain.getValue(1);
7654 Base = GetTLSADDR(DAG, Chain, GA, &InFlag, PtrVT, X86::EAX,
7655 X86II::MO_TLSLDM, /*LocalDynamic=*/true);
7656 }
7657
7658 // Note: the CleanupLocalDynamicTLSPass will remove redundant computations
7659 // of Base.
7660
7661 // Build x@dtpoff.
7662 unsigned char OperandFlags = X86II::MO_DTPOFF;
7663 unsigned WrapperKind = X86ISD::Wrapper;
7664 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl,
7665 GA->getValueType(0),
7666 GA->getOffset(), OperandFlags);
7667 SDValue Offset = DAG.getNode(WrapperKind, dl, PtrVT, TGA);
7668
7669 // Add x@dtpoff with the base.
7670 return DAG.getNode(ISD::ADD, dl, PtrVT, Offset, Base);
7671}
7672
Hans Wennborg228756c2012-05-11 10:11:01 +00007673// Lower ISD::GlobalTLSAddress using the "initial exec" or "local exec" model.
Dan Gohman475871a2008-07-27 21:46:04 +00007674static SDValue LowerToTLSExecModel(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00007675 const EVT PtrVT, TLSModel::Model model,
Hans Wennborg228756c2012-05-11 10:11:01 +00007676 bool is64Bit, bool isPIC) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00007677 DebugLoc dl = GA->getDebugLoc();
Michael J. Spencerec38de22010-10-10 22:04:20 +00007678
Chris Lattnerf93b90c2010-09-22 04:39:11 +00007679 // Get the Thread Pointer, which is %gs:0 (32-bit) or %fs:0 (64-bit).
7680 Value *Ptr = Constant::getNullValue(Type::getInt8PtrTy(*DAG.getContext(),
7681 is64Bit ? 257 : 256));
Rafael Espindola094fad32009-04-08 21:14:34 +00007682
Michael J. Spencerec38de22010-10-10 22:04:20 +00007683 SDValue ThreadPointer = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(),
Chris Lattnerf93b90c2010-09-22 04:39:11 +00007684 DAG.getIntPtrConstant(0),
Pete Cooperd752e0f2011-11-08 18:42:53 +00007685 MachinePointerInfo(Ptr),
7686 false, false, false, 0);
Rafael Espindola094fad32009-04-08 21:14:34 +00007687
Chris Lattnerb903bed2009-06-26 21:20:29 +00007688 unsigned char OperandFlags = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00007689 // Most TLS accesses are not RIP relative, even on x86-64. One exception is
7690 // initialexec.
7691 unsigned WrapperKind = X86ISD::Wrapper;
7692 if (model == TLSModel::LocalExec) {
Chris Lattnerb903bed2009-06-26 21:20:29 +00007693 OperandFlags = is64Bit ? X86II::MO_TPOFF : X86II::MO_NTPOFF;
Hans Wennborg228756c2012-05-11 10:11:01 +00007694 } else if (model == TLSModel::InitialExec) {
7695 if (is64Bit) {
7696 OperandFlags = X86II::MO_GOTTPOFF;
7697 WrapperKind = X86ISD::WrapperRIP;
7698 } else {
7699 OperandFlags = isPIC ? X86II::MO_GOTNTPOFF : X86II::MO_INDNTPOFF;
7700 }
Chris Lattner18c59872009-06-27 04:16:01 +00007701 } else {
Hans Wennborg228756c2012-05-11 10:11:01 +00007702 llvm_unreachable("Unexpected model");
Chris Lattnerb903bed2009-06-26 21:20:29 +00007703 }
Eric Christopherfd179292009-08-27 18:07:15 +00007704
Hans Wennborg228756c2012-05-11 10:11:01 +00007705 // emit "addl x@ntpoff,%eax" (local exec)
7706 // or "addl x@indntpoff,%eax" (initial exec)
7707 // or "addl x@gotntpoff(%ebx) ,%eax" (initial exec, 32-bit pic)
Michael J. Spencerec38de22010-10-10 22:04:20 +00007708 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl,
Devang Patel0d881da2010-07-06 22:08:15 +00007709 GA->getValueType(0),
Chris Lattnerb903bed2009-06-26 21:20:29 +00007710 GA->getOffset(), OperandFlags);
Chris Lattner18c59872009-06-27 04:16:01 +00007711 SDValue Offset = DAG.getNode(WrapperKind, dl, PtrVT, TGA);
Lauro Ramos Venancio7d2cc2b2007-04-22 22:50:52 +00007712
Hans Wennborg228756c2012-05-11 10:11:01 +00007713 if (model == TLSModel::InitialExec) {
7714 if (isPIC && !is64Bit) {
7715 Offset = DAG.getNode(ISD::ADD, dl, PtrVT,
7716 DAG.getNode(X86ISD::GlobalBaseReg, DebugLoc(), PtrVT),
7717 Offset);
Hans Wennborg228756c2012-05-11 10:11:01 +00007718 }
Rafael Espindola94e3b382012-06-29 04:22:35 +00007719
7720 Offset = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Offset,
7721 MachinePointerInfo::getGOT(), false, false, false,
7722 0);
Hans Wennborg228756c2012-05-11 10:11:01 +00007723 }
Lauro Ramos Venancio7d2cc2b2007-04-22 22:50:52 +00007724
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007725 // The address of the thread local variable is the add of the thread
7726 // pointer with the offset of the variable.
Dale Johannesen33c960f2009-02-04 20:06:27 +00007727 return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007728}
7729
Dan Gohman475871a2008-07-27 21:46:04 +00007730SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007731X86TargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const {
Michael J. Spencerec38de22010-10-10 22:04:20 +00007732
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007733 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
Chris Lattnerb903bed2009-06-26 21:20:29 +00007734 const GlobalValue *GV = GA->getGlobal();
Eric Christopherfd179292009-08-27 18:07:15 +00007735
Eric Christopher30ef0e52010-06-03 04:07:48 +00007736 if (Subtarget->isTargetELF()) {
Chandler Carruth34797132012-04-08 17:20:55 +00007737 TLSModel::Model model = getTargetMachine().getTLSModel(GV);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007738
Eric Christopher30ef0e52010-06-03 04:07:48 +00007739 switch (model) {
7740 case TLSModel::GeneralDynamic:
Eric Christopher30ef0e52010-06-03 04:07:48 +00007741 if (Subtarget->is64Bit())
7742 return LowerToTLSGeneralDynamicModel64(GA, DAG, getPointerTy());
7743 return LowerToTLSGeneralDynamicModel32(GA, DAG, getPointerTy());
Hans Wennborgf0234fc2012-06-01 16:27:21 +00007744 case TLSModel::LocalDynamic:
7745 return LowerToTLSLocalDynamicModel(GA, DAG, getPointerTy(),
7746 Subtarget->is64Bit());
Eric Christopher30ef0e52010-06-03 04:07:48 +00007747 case TLSModel::InitialExec:
7748 case TLSModel::LocalExec:
7749 return LowerToTLSExecModel(GA, DAG, getPointerTy(), model,
Hans Wennborg228756c2012-05-11 10:11:01 +00007750 Subtarget->is64Bit(),
7751 getTargetMachine().getRelocationModel() == Reloc::PIC_);
Eric Christopher30ef0e52010-06-03 04:07:48 +00007752 }
Craig Toppere8eb1162012-04-23 03:26:18 +00007753 llvm_unreachable("Unknown TLS model.");
7754 }
7755
7756 if (Subtarget->isTargetDarwin()) {
Eric Christopher30ef0e52010-06-03 04:07:48 +00007757 // Darwin only has one model of TLS. Lower to that.
7758 unsigned char OpFlag = 0;
7759 unsigned WrapperKind = Subtarget->isPICStyleRIPRel() ?
7760 X86ISD::WrapperRIP : X86ISD::Wrapper;
Michael J. Spencerec38de22010-10-10 22:04:20 +00007761
Eric Christopher30ef0e52010-06-03 04:07:48 +00007762 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
7763 // global base reg.
7764 bool PIC32 = (getTargetMachine().getRelocationModel() == Reloc::PIC_) &&
7765 !Subtarget->is64Bit();
7766 if (PIC32)
7767 OpFlag = X86II::MO_TLVP_PIC_BASE;
7768 else
7769 OpFlag = X86II::MO_TLVP;
Michael J. Spencerec38de22010-10-10 22:04:20 +00007770 DebugLoc DL = Op.getDebugLoc();
Devang Patel0d881da2010-07-06 22:08:15 +00007771 SDValue Result = DAG.getTargetGlobalAddress(GA->getGlobal(), DL,
Eric Christopherd8c05362010-12-09 06:25:53 +00007772 GA->getValueType(0),
Eric Christopher30ef0e52010-06-03 04:07:48 +00007773 GA->getOffset(), OpFlag);
Eric Christopher30ef0e52010-06-03 04:07:48 +00007774 SDValue Offset = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007775
Eric Christopher30ef0e52010-06-03 04:07:48 +00007776 // With PIC32, the address is actually $g + Offset.
7777 if (PIC32)
7778 Offset = DAG.getNode(ISD::ADD, DL, getPointerTy(),
7779 DAG.getNode(X86ISD::GlobalBaseReg,
7780 DebugLoc(), getPointerTy()),
7781 Offset);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007782
Eric Christopher30ef0e52010-06-03 04:07:48 +00007783 // Lowering the machine isd will make sure everything is in the right
7784 // location.
Eric Christopherd8c05362010-12-09 06:25:53 +00007785 SDValue Chain = DAG.getEntryNode();
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00007786 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Eric Christopherd8c05362010-12-09 06:25:53 +00007787 SDValue Args[] = { Chain, Offset };
7788 Chain = DAG.getNode(X86ISD::TLSCALL, DL, NodeTys, Args, 2);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007789
Eric Christopher30ef0e52010-06-03 04:07:48 +00007790 // TLSCALL will be codegen'ed as call. Inform MFI that function has calls.
7791 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
7792 MFI->setAdjustsStack(true);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00007793
Eric Christopher30ef0e52010-06-03 04:07:48 +00007794 // And our return value (tls address) is in the standard call return value
7795 // location.
Eric Christopherd8c05362010-12-09 06:25:53 +00007796 unsigned Reg = Subtarget->is64Bit() ? X86::RAX : X86::EAX;
Evan Chengfd230df2011-10-19 22:22:54 +00007797 return DAG.getCopyFromReg(Chain, DL, Reg, getPointerTy(),
7798 Chain.getValue(1));
Craig Toppere8eb1162012-04-23 03:26:18 +00007799 }
7800
7801 if (Subtarget->isTargetWindows()) {
Anton Korobeynikovd4a19b62012-02-11 17:26:53 +00007802 // Just use the implicit TLS architecture
7803 // Need to generate someting similar to:
7804 // mov rdx, qword [gs:abs 58H]; Load pointer to ThreadLocalStorage
7805 // ; from TEB
7806 // mov ecx, dword [rel _tls_index]: Load index (from C runtime)
7807 // mov rcx, qword [rdx+rcx*8]
7808 // mov eax, .tls$:tlsvar
7809 // [rax+rcx] contains the address
7810 // Windows 64bit: gs:0x58
7811 // Windows 32bit: fs:__tls_array
7812
7813 // If GV is an alias then use the aliasee for determining
7814 // thread-localness.
7815 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV))
7816 GV = GA->resolveAliasedGlobal(false);
7817 DebugLoc dl = GA->getDebugLoc();
7818 SDValue Chain = DAG.getEntryNode();
7819
7820 // Get the Thread Pointer, which is %fs:__tls_array (32-bit) or
7821 // %gs:0x58 (64-bit).
7822 Value *Ptr = Constant::getNullValue(Subtarget->is64Bit()
7823 ? Type::getInt8PtrTy(*DAG.getContext(),
7824 256)
7825 : Type::getInt32PtrTy(*DAG.getContext(),
7826 257));
7827
7828 SDValue ThreadPointer = DAG.getLoad(getPointerTy(), dl, Chain,
7829 Subtarget->is64Bit()
7830 ? DAG.getIntPtrConstant(0x58)
7831 : DAG.getExternalSymbol("_tls_array",
7832 getPointerTy()),
7833 MachinePointerInfo(Ptr),
7834 false, false, false, 0);
7835
7836 // Load the _tls_index variable
7837 SDValue IDX = DAG.getExternalSymbol("_tls_index", getPointerTy());
7838 if (Subtarget->is64Bit())
7839 IDX = DAG.getExtLoad(ISD::ZEXTLOAD, dl, getPointerTy(), Chain,
7840 IDX, MachinePointerInfo(), MVT::i32,
7841 false, false, 0);
7842 else
7843 IDX = DAG.getLoad(getPointerTy(), dl, Chain, IDX, MachinePointerInfo(),
7844 false, false, false, 0);
7845
Chandler Carruth426c2bf2012-11-01 09:14:31 +00007846 SDValue Scale = DAG.getConstant(Log2_64_Ceil(TD->getPointerSize()),
Craig Topper0fbf3642012-04-23 03:28:34 +00007847 getPointerTy());
Anton Korobeynikovd4a19b62012-02-11 17:26:53 +00007848 IDX = DAG.getNode(ISD::SHL, dl, getPointerTy(), IDX, Scale);
7849
7850 SDValue res = DAG.getNode(ISD::ADD, dl, getPointerTy(), ThreadPointer, IDX);
7851 res = DAG.getLoad(getPointerTy(), dl, Chain, res, MachinePointerInfo(),
7852 false, false, false, 0);
7853
7854 // Get the offset of start of .tls section
7855 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl,
7856 GA->getValueType(0),
7857 GA->getOffset(), X86II::MO_SECREL);
7858 SDValue Offset = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), TGA);
7859
7860 // The address of the thread local variable is the add of the thread
7861 // pointer with the offset of the variable.
7862 return DAG.getNode(ISD::ADD, dl, getPointerTy(), res, Offset);
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007863 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00007864
David Blaikie4d6ccb52012-01-20 21:51:11 +00007865 llvm_unreachable("TLS not implemented for this target.");
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007866}
7867
Evan Cheng0db9fe62006-04-25 20:13:52 +00007868
Chad Rosierb90d2a92012-01-03 23:19:12 +00007869/// LowerShiftParts - Lower SRA_PARTS and friends, which return two i32 values
7870/// and take a 2 x i32 value to shift plus a shift amount.
7871SDValue X86TargetLowering::LowerShiftParts(SDValue Op, SelectionDAG &DAG) const{
Dan Gohman4c1fa612008-03-03 22:22:09 +00007872 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
Owen Andersone50ed302009-08-10 22:56:29 +00007873 EVT VT = Op.getValueType();
Duncan Sands83ec4b62008-06-06 12:08:01 +00007874 unsigned VTBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007875 DebugLoc dl = Op.getDebugLoc();
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007876 bool isSRA = Op.getOpcode() == ISD::SRA_PARTS;
Dan Gohman475871a2008-07-27 21:46:04 +00007877 SDValue ShOpLo = Op.getOperand(0);
7878 SDValue ShOpHi = Op.getOperand(1);
7879 SDValue ShAmt = Op.getOperand(2);
Chris Lattner31dcfe62009-07-29 05:48:09 +00007880 SDValue Tmp1 = isSRA ? DAG.getNode(ISD::SRA, dl, VT, ShOpHi,
Owen Anderson825b72b2009-08-11 20:47:22 +00007881 DAG.getConstant(VTBits - 1, MVT::i8))
Chris Lattner31dcfe62009-07-29 05:48:09 +00007882 : DAG.getConstant(0, VT);
Evan Chenge3413162006-01-09 18:33:28 +00007883
Dan Gohman475871a2008-07-27 21:46:04 +00007884 SDValue Tmp2, Tmp3;
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007885 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dale Johannesenace16102009-02-03 19:33:06 +00007886 Tmp2 = DAG.getNode(X86ISD::SHLD, dl, VT, ShOpHi, ShOpLo, ShAmt);
7887 Tmp3 = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ShAmt);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007888 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00007889 Tmp2 = DAG.getNode(X86ISD::SHRD, dl, VT, ShOpLo, ShOpHi, ShAmt);
7890 Tmp3 = DAG.getNode(isSRA ? ISD::SRA : ISD::SRL, dl, VT, ShOpHi, ShAmt);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007891 }
Evan Chenge3413162006-01-09 18:33:28 +00007892
Owen Anderson825b72b2009-08-11 20:47:22 +00007893 SDValue AndNode = DAG.getNode(ISD::AND, dl, MVT::i8, ShAmt,
7894 DAG.getConstant(VTBits, MVT::i8));
Chris Lattnerccfea352010-02-22 00:28:59 +00007895 SDValue Cond = DAG.getNode(X86ISD::CMP, dl, MVT::i32,
Owen Anderson825b72b2009-08-11 20:47:22 +00007896 AndNode, DAG.getConstant(0, MVT::i8));
Evan Chenge3413162006-01-09 18:33:28 +00007897
Dan Gohman475871a2008-07-27 21:46:04 +00007898 SDValue Hi, Lo;
Owen Anderson825b72b2009-08-11 20:47:22 +00007899 SDValue CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Dan Gohman475871a2008-07-27 21:46:04 +00007900 SDValue Ops0[4] = { Tmp2, Tmp3, CC, Cond };
7901 SDValue Ops1[4] = { Tmp3, Tmp1, CC, Cond };
Duncan Sandsf9516202008-06-30 10:19:09 +00007902
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007903 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dale Johannesenace16102009-02-03 19:33:06 +00007904 Hi = DAG.getNode(X86ISD::CMOV, dl, VT, Ops0, 4);
7905 Lo = DAG.getNode(X86ISD::CMOV, dl, VT, Ops1, 4);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007906 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00007907 Lo = DAG.getNode(X86ISD::CMOV, dl, VT, Ops0, 4);
7908 Hi = DAG.getNode(X86ISD::CMOV, dl, VT, Ops1, 4);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007909 }
7910
Dan Gohman475871a2008-07-27 21:46:04 +00007911 SDValue Ops[2] = { Lo, Hi };
Dale Johannesenace16102009-02-03 19:33:06 +00007912 return DAG.getMergeValues(Ops, 2, dl);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007913}
Evan Chenga3195e82006-01-12 22:54:21 +00007914
Dan Gohmand858e902010-04-17 15:26:15 +00007915SDValue X86TargetLowering::LowerSINT_TO_FP(SDValue Op,
7916 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00007917 EVT SrcVT = Op.getOperand(0).getValueType();
Eli Friedman23ef1052009-06-06 03:57:58 +00007918
Dale Johannesen0488fb62010-09-30 23:57:10 +00007919 if (SrcVT.isVector())
Eli Friedman23ef1052009-06-06 03:57:58 +00007920 return SDValue();
Eli Friedman23ef1052009-06-06 03:57:58 +00007921
Owen Anderson825b72b2009-08-11 20:47:22 +00007922 assert(SrcVT.getSimpleVT() <= MVT::i64 && SrcVT.getSimpleVT() >= MVT::i16 &&
Chris Lattnerb09916b2008-02-27 05:57:41 +00007923 "Unknown SINT_TO_FP to lower!");
Scott Michelfdc40a02009-02-17 22:15:04 +00007924
Eli Friedman36df4992009-05-27 00:47:34 +00007925 // These are really Legal; return the operand so the caller accepts it as
7926 // Legal.
Owen Anderson825b72b2009-08-11 20:47:22 +00007927 if (SrcVT == MVT::i32 && isScalarFPTypeInSSEReg(Op.getValueType()))
Eli Friedman36df4992009-05-27 00:47:34 +00007928 return Op;
Owen Anderson825b72b2009-08-11 20:47:22 +00007929 if (SrcVT == MVT::i64 && isScalarFPTypeInSSEReg(Op.getValueType()) &&
Eli Friedman36df4992009-05-27 00:47:34 +00007930 Subtarget->is64Bit()) {
7931 return Op;
7932 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007933
Bruno Cardoso Lopesa511b8e2011-08-09 17:39:01 +00007934 DebugLoc dl = Op.getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +00007935 unsigned Size = SrcVT.getSizeInBits()/8;
Evan Cheng0db9fe62006-04-25 20:13:52 +00007936 MachineFunction &MF = DAG.getMachineFunction();
David Greene3f2bf852009-11-12 20:49:22 +00007937 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size, false);
Dan Gohman475871a2008-07-27 21:46:04 +00007938 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Dale Johannesenace16102009-02-03 19:33:06 +00007939 SDValue Chain = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Bill Wendling105be5a2009-03-13 08:41:47 +00007940 StackSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00007941 MachinePointerInfo::getFixedStack(SSFI),
David Greene67c9d422010-02-15 16:53:33 +00007942 false, false, 0);
Eli Friedman948e95a2009-05-23 09:59:16 +00007943 return BuildFILD(Op, SrcVT, Chain, StackSlot, DAG);
7944}
Evan Cheng0db9fe62006-04-25 20:13:52 +00007945
Owen Andersone50ed302009-08-10 22:56:29 +00007946SDValue X86TargetLowering::BuildFILD(SDValue Op, EVT SrcVT, SDValue Chain,
Michael J. Spencerec38de22010-10-10 22:04:20 +00007947 SDValue StackSlot,
Dan Gohmand858e902010-04-17 15:26:15 +00007948 SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00007949 // Build the FILD
Chris Lattner492a43e2010-09-22 01:28:21 +00007950 DebugLoc DL = Op.getDebugLoc();
Chris Lattner5a88b832007-02-25 07:10:00 +00007951 SDVTList Tys;
Chris Lattner78631162008-01-16 06:24:21 +00007952 bool useSSE = isScalarFPTypeInSSEReg(Op.getValueType());
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00007953 if (useSSE)
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00007954 Tys = DAG.getVTList(MVT::f64, MVT::Other, MVT::Glue);
Chris Lattner5a88b832007-02-25 07:10:00 +00007955 else
Owen Anderson825b72b2009-08-11 20:47:22 +00007956 Tys = DAG.getVTList(Op.getValueType(), MVT::Other);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007957
Chris Lattner492a43e2010-09-22 01:28:21 +00007958 unsigned ByteSize = SrcVT.getSizeInBits()/8;
Michael J. Spencerec38de22010-10-10 22:04:20 +00007959
Stuart Hastings84be9582011-06-02 15:57:11 +00007960 FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(StackSlot);
7961 MachineMemOperand *MMO;
7962 if (FI) {
7963 int SSFI = FI->getIndex();
7964 MMO =
7965 DAG.getMachineFunction()
7966 .getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
7967 MachineMemOperand::MOLoad, ByteSize, ByteSize);
7968 } else {
7969 MMO = cast<LoadSDNode>(StackSlot)->getMemOperand();
7970 StackSlot = StackSlot.getOperand(1);
7971 }
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00007972 SDValue Ops[] = { Chain, StackSlot, DAG.getValueType(SrcVT) };
Chris Lattner492a43e2010-09-22 01:28:21 +00007973 SDValue Result = DAG.getMemIntrinsicNode(useSSE ? X86ISD::FILD_FLAG :
7974 X86ISD::FILD, DL,
7975 Tys, Ops, array_lengthof(Ops),
7976 SrcVT, MMO);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007977
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00007978 if (useSSE) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00007979 Chain = Result.getValue(1);
Dan Gohman475871a2008-07-27 21:46:04 +00007980 SDValue InFlag = Result.getValue(2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007981
7982 // FIXME: Currently the FST is flagged to the FILD_FLAG. This
7983 // shouldn't be necessary except that RFP cannot be live across
7984 // multiple blocks. When stackifier is fixed, they can be uncoupled.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00007985 MachineFunction &MF = DAG.getMachineFunction();
Bob Wilsoneafca4e2010-09-22 17:35:14 +00007986 unsigned SSFISize = Op.getValueType().getSizeInBits()/8;
7987 int SSFI = MF.getFrameInfo()->CreateStackObject(SSFISize, SSFISize, false);
Dan Gohman475871a2008-07-27 21:46:04 +00007988 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Owen Anderson825b72b2009-08-11 20:47:22 +00007989 Tys = DAG.getVTList(MVT::Other);
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00007990 SDValue Ops[] = {
7991 Chain, Result, StackSlot, DAG.getValueType(Op.getValueType()), InFlag
7992 };
Chris Lattner492a43e2010-09-22 01:28:21 +00007993 MachineMemOperand *MMO =
7994 DAG.getMachineFunction()
7995 .getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
Bob Wilsoneafca4e2010-09-22 17:35:14 +00007996 MachineMemOperand::MOStore, SSFISize, SSFISize);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007997
Chris Lattner492a43e2010-09-22 01:28:21 +00007998 Chain = DAG.getMemIntrinsicNode(X86ISD::FST, DL, Tys,
7999 Ops, array_lengthof(Ops),
8000 Op.getValueType(), MMO);
8001 Result = DAG.getLoad(Op.getValueType(), DL, Chain, StackSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00008002 MachinePointerInfo::getFixedStack(SSFI),
Pete Cooperd752e0f2011-11-08 18:42:53 +00008003 false, false, false, 0);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00008004 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00008005
Evan Cheng0db9fe62006-04-25 20:13:52 +00008006 return Result;
8007}
8008
Bill Wendling8b8a6362009-01-17 03:56:04 +00008009// LowerUINT_TO_FP_i64 - 64-bit unsigned integer to double expansion.
Dan Gohmand858e902010-04-17 15:26:15 +00008010SDValue X86TargetLowering::LowerUINT_TO_FP_i64(SDValue Op,
8011 SelectionDAG &DAG) const {
Bill Wendling397ae212012-01-05 02:13:20 +00008012 // This algorithm is not obvious. Here it is what we're trying to output:
Bill Wendling8b8a6362009-01-17 03:56:04 +00008013 /*
Bill Wendling397ae212012-01-05 02:13:20 +00008014 movq %rax, %xmm0
8015 punpckldq (c0), %xmm0 // c0: (uint4){ 0x43300000U, 0x45300000U, 0U, 0U }
8016 subpd (c1), %xmm0 // c1: (double2){ 0x1.0p52, 0x1.0p52 * 0x1.0p32 }
8017 #ifdef __SSE3__
Chad Rosiera20e1e72012-08-01 18:39:17 +00008018 haddpd %xmm0, %xmm0
Bill Wendling397ae212012-01-05 02:13:20 +00008019 #else
Chad Rosiera20e1e72012-08-01 18:39:17 +00008020 pshufd $0x4e, %xmm0, %xmm1
Bill Wendling397ae212012-01-05 02:13:20 +00008021 addpd %xmm1, %xmm0
8022 #endif
Bill Wendling8b8a6362009-01-17 03:56:04 +00008023 */
Dale Johannesen040225f2008-10-21 23:07:49 +00008024
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008025 DebugLoc dl = Op.getDebugLoc();
Owen Andersona90b3dc2009-07-15 21:51:10 +00008026 LLVMContext *Context = DAG.getContext();
Dale Johannesenace16102009-02-03 19:33:06 +00008027
Dale Johannesen1c15bf52008-10-21 20:50:01 +00008028 // Build some magic constants.
Chris Lattner7302d802012-02-06 21:56:39 +00008029 const uint32_t CV0[] = { 0x43300000, 0x45300000, 0, 0 };
8030 Constant *C0 = ConstantDataVector::get(*Context, CV0);
Evan Cheng1606e8e2009-03-13 07:51:59 +00008031 SDValue CPIdx0 = DAG.getConstantPool(C0, getPointerTy(), 16);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00008032
Chris Lattner97484792012-01-25 09:56:22 +00008033 SmallVector<Constant*,2> CV1;
8034 CV1.push_back(
Chris Lattner4ca829e2012-01-25 06:02:56 +00008035 ConstantFP::get(*Context, APFloat(APInt(64, 0x4330000000000000ULL))));
Chris Lattner97484792012-01-25 09:56:22 +00008036 CV1.push_back(
8037 ConstantFP::get(*Context, APFloat(APInt(64, 0x4530000000000000ULL))));
8038 Constant *C1 = ConstantVector::get(CV1);
Evan Cheng1606e8e2009-03-13 07:51:59 +00008039 SDValue CPIdx1 = DAG.getConstantPool(C1, getPointerTy(), 16);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00008040
Bill Wendling397ae212012-01-05 02:13:20 +00008041 // Load the 64-bit value into an XMM register.
8042 SDValue XR1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i64,
8043 Op.getOperand(0));
Owen Anderson825b72b2009-08-11 20:47:22 +00008044 SDValue CLod0 = DAG.getLoad(MVT::v4i32, dl, DAG.getEntryNode(), CPIdx0,
Chris Lattnere8639032010-09-21 06:22:23 +00008045 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00008046 false, false, false, 16);
Bill Wendling397ae212012-01-05 02:13:20 +00008047 SDValue Unpck1 = getUnpackl(DAG, dl, MVT::v4i32,
8048 DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, XR1),
8049 CLod0);
8050
Owen Anderson825b72b2009-08-11 20:47:22 +00008051 SDValue CLod1 = DAG.getLoad(MVT::v2f64, dl, CLod0.getValue(1), CPIdx1,
Chris Lattnere8639032010-09-21 06:22:23 +00008052 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00008053 false, false, false, 16);
Bill Wendling397ae212012-01-05 02:13:20 +00008054 SDValue XR2F = DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Unpck1);
Owen Anderson825b72b2009-08-11 20:47:22 +00008055 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::v2f64, XR2F, CLod1);
Bill Wendling397ae212012-01-05 02:13:20 +00008056 SDValue Result;
Bill Wendling8b8a6362009-01-17 03:56:04 +00008057
Craig Topperd0a31172012-01-10 06:37:29 +00008058 if (Subtarget->hasSSE3()) {
Bill Wendling397ae212012-01-05 02:13:20 +00008059 // FIXME: The 'haddpd' instruction may be slower than 'movhlps + addsd'.
8060 Result = DAG.getNode(X86ISD::FHADD, dl, MVT::v2f64, Sub, Sub);
8061 } else {
8062 SDValue S2F = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, Sub);
8063 SDValue Shuffle = getTargetShuffleNode(X86ISD::PSHUFD, dl, MVT::v4i32,
8064 S2F, 0x4E, DAG);
8065 Result = DAG.getNode(ISD::FADD, dl, MVT::v2f64,
8066 DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Shuffle),
8067 Sub);
8068 }
8069
8070 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Result,
Dale Johannesen1c15bf52008-10-21 20:50:01 +00008071 DAG.getIntPtrConstant(0));
8072}
8073
Bill Wendling8b8a6362009-01-17 03:56:04 +00008074// LowerUINT_TO_FP_i32 - 32-bit unsigned integer to float expansion.
Dan Gohmand858e902010-04-17 15:26:15 +00008075SDValue X86TargetLowering::LowerUINT_TO_FP_i32(SDValue Op,
8076 SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008077 DebugLoc dl = Op.getDebugLoc();
Bill Wendling8b8a6362009-01-17 03:56:04 +00008078 // FP constant to bias correct the final result.
8079 SDValue Bias = DAG.getConstantFP(BitsToDouble(0x4330000000000000ULL),
Owen Anderson825b72b2009-08-11 20:47:22 +00008080 MVT::f64);
Bill Wendling8b8a6362009-01-17 03:56:04 +00008081
8082 // Load the 32-bit value into an XMM register.
Owen Anderson825b72b2009-08-11 20:47:22 +00008083 SDValue Load = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
Eli Friedman6cdc1f42011-08-02 18:38:35 +00008084 Op.getOperand(0));
Bill Wendling8b8a6362009-01-17 03:56:04 +00008085
Eli Friedmanf3704762011-08-29 21:15:46 +00008086 // Zero out the upper parts of the register.
Craig Topper12216172012-01-13 08:12:35 +00008087 Load = getShuffleVectorZeroOrUndef(Load, 0, true, Subtarget, DAG);
Eli Friedmanf3704762011-08-29 21:15:46 +00008088
Owen Anderson825b72b2009-08-11 20:47:22 +00008089 Load = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008090 DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Load),
Bill Wendling8b8a6362009-01-17 03:56:04 +00008091 DAG.getIntPtrConstant(0));
8092
8093 // Or the load with the bias.
Owen Anderson825b72b2009-08-11 20:47:22 +00008094 SDValue Or = DAG.getNode(ISD::OR, dl, MVT::v2i64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008095 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00008096 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00008097 MVT::v2f64, Load)),
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008098 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00008099 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00008100 MVT::v2f64, Bias)));
8101 Or = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008102 DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Or),
Bill Wendling8b8a6362009-01-17 03:56:04 +00008103 DAG.getIntPtrConstant(0));
8104
8105 // Subtract the bias.
Owen Anderson825b72b2009-08-11 20:47:22 +00008106 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::f64, Or, Bias);
Bill Wendling8b8a6362009-01-17 03:56:04 +00008107
8108 // Handle final rounding.
Owen Andersone50ed302009-08-10 22:56:29 +00008109 EVT DestVT = Op.getValueType();
Bill Wendling030939c2009-01-17 07:40:19 +00008110
Craig Topper69947b92012-04-23 06:57:04 +00008111 if (DestVT.bitsLT(MVT::f64))
Dale Johannesenace16102009-02-03 19:33:06 +00008112 return DAG.getNode(ISD::FP_ROUND, dl, DestVT, Sub,
Bill Wendling030939c2009-01-17 07:40:19 +00008113 DAG.getIntPtrConstant(0));
Craig Topper69947b92012-04-23 06:57:04 +00008114 if (DestVT.bitsGT(MVT::f64))
Dale Johannesenace16102009-02-03 19:33:06 +00008115 return DAG.getNode(ISD::FP_EXTEND, dl, DestVT, Sub);
Bill Wendling030939c2009-01-17 07:40:19 +00008116
8117 // Handle final rounding.
8118 return Sub;
Bill Wendling8b8a6362009-01-17 03:56:04 +00008119}
8120
Michael Liaoa7554632012-10-23 17:36:08 +00008121SDValue X86TargetLowering::lowerUINT_TO_FP_vec(SDValue Op,
8122 SelectionDAG &DAG) const {
8123 SDValue N0 = Op.getOperand(0);
8124 EVT SVT = N0.getValueType();
8125 DebugLoc dl = Op.getDebugLoc();
8126
8127 assert((SVT == MVT::v4i8 || SVT == MVT::v4i16 ||
8128 SVT == MVT::v8i8 || SVT == MVT::v8i16) &&
8129 "Custom UINT_TO_FP is not supported!");
8130
8131 EVT NVT = EVT::getVectorVT(*DAG.getContext(), MVT::i32, SVT.getVectorNumElements());
8132 return DAG.getNode(ISD::SINT_TO_FP, dl, Op.getValueType(),
8133 DAG.getNode(ISD::ZERO_EXTEND, dl, NVT, N0));
8134}
8135
Dan Gohmand858e902010-04-17 15:26:15 +00008136SDValue X86TargetLowering::LowerUINT_TO_FP(SDValue Op,
8137 SelectionDAG &DAG) const {
Evan Chenga06ec9e2009-01-19 08:08:22 +00008138 SDValue N0 = Op.getOperand(0);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008139 DebugLoc dl = Op.getDebugLoc();
Bill Wendling8b8a6362009-01-17 03:56:04 +00008140
Michael Liaoa7554632012-10-23 17:36:08 +00008141 if (Op.getValueType().isVector())
8142 return lowerUINT_TO_FP_vec(Op, DAG);
8143
Dale Johannesen8d908eb2010-05-15 18:51:12 +00008144 // Since UINT_TO_FP is legal (it's marked custom), dag combiner won't
Evan Chenga06ec9e2009-01-19 08:08:22 +00008145 // optimize it to a SINT_TO_FP when the sign bit is known zero. Perform
8146 // the optimization here.
8147 if (DAG.SignBitIsZero(N0))
Dale Johannesenace16102009-02-03 19:33:06 +00008148 return DAG.getNode(ISD::SINT_TO_FP, dl, Op.getValueType(), N0);
Evan Chenga06ec9e2009-01-19 08:08:22 +00008149
Owen Andersone50ed302009-08-10 22:56:29 +00008150 EVT SrcVT = N0.getValueType();
Dale Johannesen8d908eb2010-05-15 18:51:12 +00008151 EVT DstVT = Op.getValueType();
8152 if (SrcVT == MVT::i64 && DstVT == MVT::f64 && X86ScalarSSEf64)
Bill Wendling8b8a6362009-01-17 03:56:04 +00008153 return LowerUINT_TO_FP_i64(Op, DAG);
Craig Topper69947b92012-04-23 06:57:04 +00008154 if (SrcVT == MVT::i32 && X86ScalarSSEf64)
Bill Wendling8b8a6362009-01-17 03:56:04 +00008155 return LowerUINT_TO_FP_i32(Op, DAG);
Craig Topper69947b92012-04-23 06:57:04 +00008156 if (Subtarget->is64Bit() && SrcVT == MVT::i64 && DstVT == MVT::f32)
Bill Wendling397ae212012-01-05 02:13:20 +00008157 return SDValue();
Eli Friedman948e95a2009-05-23 09:59:16 +00008158
8159 // Make a 64-bit buffer, and use it to build an FILD.
Owen Anderson825b72b2009-08-11 20:47:22 +00008160 SDValue StackSlot = DAG.CreateStackTemporary(MVT::i64);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00008161 if (SrcVT == MVT::i32) {
8162 SDValue WordOff = DAG.getConstant(4, getPointerTy());
8163 SDValue OffsetSlot = DAG.getNode(ISD::ADD, dl,
8164 getPointerTy(), StackSlot, WordOff);
8165 SDValue Store1 = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Chris Lattner8026a9d2010-09-21 17:50:43 +00008166 StackSlot, MachinePointerInfo(),
8167 false, false, 0);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00008168 SDValue Store2 = DAG.getStore(Store1, dl, DAG.getConstant(0, MVT::i32),
Chris Lattner8026a9d2010-09-21 17:50:43 +00008169 OffsetSlot, MachinePointerInfo(),
8170 false, false, 0);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00008171 SDValue Fild = BuildFILD(Op, MVT::i64, Store2, StackSlot, DAG);
8172 return Fild;
8173 }
8174
8175 assert(SrcVT == MVT::i64 && "Unexpected type in UINT_TO_FP");
8176 SDValue Store = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Bill Wendlingf6c07472012-01-10 19:41:30 +00008177 StackSlot, MachinePointerInfo(),
Chris Lattner8026a9d2010-09-21 17:50:43 +00008178 false, false, 0);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00008179 // For i64 source, we need to add the appropriate power of 2 if the input
8180 // was negative. This is the same as the optimization in
8181 // DAGTypeLegalizer::ExpandIntOp_UNIT_TO_FP, and for it to be safe here,
8182 // we must be careful to do the computation in x87 extended precision, not
8183 // in SSE. (The generic code can't know it's OK to do this, or how to.)
Chris Lattner492a43e2010-09-22 01:28:21 +00008184 int SSFI = cast<FrameIndexSDNode>(StackSlot)->getIndex();
8185 MachineMemOperand *MMO =
8186 DAG.getMachineFunction()
8187 .getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
8188 MachineMemOperand::MOLoad, 8, 8);
Michael J. Spencerec38de22010-10-10 22:04:20 +00008189
Dale Johannesen8d908eb2010-05-15 18:51:12 +00008190 SDVTList Tys = DAG.getVTList(MVT::f80, MVT::Other);
8191 SDValue Ops[] = { Store, StackSlot, DAG.getValueType(MVT::i64) };
Chris Lattner492a43e2010-09-22 01:28:21 +00008192 SDValue Fild = DAG.getMemIntrinsicNode(X86ISD::FILD, dl, Tys, Ops, 3,
8193 MVT::i64, MMO);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00008194
8195 APInt FF(32, 0x5F800000ULL);
8196
8197 // Check whether the sign bit is set.
8198 SDValue SignSet = DAG.getSetCC(dl, getSetCCResultType(MVT::i64),
8199 Op.getOperand(0), DAG.getConstant(0, MVT::i64),
8200 ISD::SETLT);
8201
8202 // Build a 64 bit pair (0, FF) in the constant pool, with FF in the lo bits.
8203 SDValue FudgePtr = DAG.getConstantPool(
8204 ConstantInt::get(*DAG.getContext(), FF.zext(64)),
8205 getPointerTy());
8206
8207 // Get a pointer to FF if the sign bit was set, or to 0 otherwise.
8208 SDValue Zero = DAG.getIntPtrConstant(0);
8209 SDValue Four = DAG.getIntPtrConstant(4);
8210 SDValue Offset = DAG.getNode(ISD::SELECT, dl, Zero.getValueType(), SignSet,
8211 Zero, Four);
8212 FudgePtr = DAG.getNode(ISD::ADD, dl, getPointerTy(), FudgePtr, Offset);
8213
8214 // Load the value out, extending it from f32 to f80.
8215 // FIXME: Avoid the extend by constructing the right constant pool?
Stuart Hastingsa9011292011-02-16 16:23:55 +00008216 SDValue Fudge = DAG.getExtLoad(ISD::EXTLOAD, dl, MVT::f80, DAG.getEntryNode(),
Chris Lattnere8639032010-09-21 06:22:23 +00008217 FudgePtr, MachinePointerInfo::getConstantPool(),
8218 MVT::f32, false, false, 4);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00008219 // Extend everything to 80 bits to force it to be done on x87.
8220 SDValue Add = DAG.getNode(ISD::FADD, dl, MVT::f80, Fild, Fudge);
8221 return DAG.getNode(ISD::FP_ROUND, dl, DstVT, Add, DAG.getIntPtrConstant(0));
Bill Wendling8b8a6362009-01-17 03:56:04 +00008222}
8223
Dan Gohman475871a2008-07-27 21:46:04 +00008224std::pair<SDValue,SDValue> X86TargetLowering::
NAKAMURA Takumi9a68fdc2012-02-25 03:37:25 +00008225FP_TO_INTHelper(SDValue Op, SelectionDAG &DAG, bool IsSigned, bool IsReplace) const {
Chris Lattner07290932010-09-22 01:05:16 +00008226 DebugLoc DL = Op.getDebugLoc();
Eli Friedman948e95a2009-05-23 09:59:16 +00008227
Owen Andersone50ed302009-08-10 22:56:29 +00008228 EVT DstTy = Op.getValueType();
Eli Friedman948e95a2009-05-23 09:59:16 +00008229
Michael J. Spencer1a2d0612012-02-24 19:01:22 +00008230 if (!IsSigned && !isIntegerTypeFTOL(DstTy)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00008231 assert(DstTy == MVT::i32 && "Unexpected FP_TO_UINT");
8232 DstTy = MVT::i64;
Eli Friedman948e95a2009-05-23 09:59:16 +00008233 }
8234
Owen Anderson825b72b2009-08-11 20:47:22 +00008235 assert(DstTy.getSimpleVT() <= MVT::i64 &&
8236 DstTy.getSimpleVT() >= MVT::i16 &&
Michael J. Spencer1a2d0612012-02-24 19:01:22 +00008237 "Unknown FP_TO_INT to lower!");
Evan Cheng0db9fe62006-04-25 20:13:52 +00008238
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00008239 // These are really Legal.
Owen Anderson825b72b2009-08-11 20:47:22 +00008240 if (DstTy == MVT::i32 &&
Chris Lattner78631162008-01-16 06:24:21 +00008241 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Dan Gohman475871a2008-07-27 21:46:04 +00008242 return std::make_pair(SDValue(), SDValue());
Dale Johannesen73328d12007-09-19 23:55:34 +00008243 if (Subtarget->is64Bit() &&
Owen Anderson825b72b2009-08-11 20:47:22 +00008244 DstTy == MVT::i64 &&
Eli Friedman36df4992009-05-27 00:47:34 +00008245 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Dan Gohman475871a2008-07-27 21:46:04 +00008246 return std::make_pair(SDValue(), SDValue());
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00008247
Michael J. Spencer1a2d0612012-02-24 19:01:22 +00008248 // We lower FP->int64 either into FISTP64 followed by a load from a temporary
8249 // stack slot, or into the FTOL runtime function.
Evan Cheng87c89352007-10-15 20:11:21 +00008250 MachineFunction &MF = DAG.getMachineFunction();
Eli Friedman948e95a2009-05-23 09:59:16 +00008251 unsigned MemSize = DstTy.getSizeInBits()/8;
David Greene3f2bf852009-11-12 20:49:22 +00008252 int SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize, false);
Dan Gohman475871a2008-07-27 21:46:04 +00008253 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Eric Christopherfd179292009-08-27 18:07:15 +00008254
Evan Cheng0db9fe62006-04-25 20:13:52 +00008255 unsigned Opc;
Michael J. Spencer1a2d0612012-02-24 19:01:22 +00008256 if (!IsSigned && isIntegerTypeFTOL(DstTy))
8257 Opc = X86ISD::WIN_FTOL;
8258 else
8259 switch (DstTy.getSimpleVT().SimpleTy) {
8260 default: llvm_unreachable("Invalid FP_TO_SINT to lower!");
8261 case MVT::i16: Opc = X86ISD::FP_TO_INT16_IN_MEM; break;
8262 case MVT::i32: Opc = X86ISD::FP_TO_INT32_IN_MEM; break;
8263 case MVT::i64: Opc = X86ISD::FP_TO_INT64_IN_MEM; break;
8264 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00008265
Dan Gohman475871a2008-07-27 21:46:04 +00008266 SDValue Chain = DAG.getEntryNode();
8267 SDValue Value = Op.getOperand(0);
Chris Lattner492a43e2010-09-22 01:28:21 +00008268 EVT TheVT = Op.getOperand(0).getValueType();
Michael J. Spencer1a2d0612012-02-24 19:01:22 +00008269 // FIXME This causes a redundant load/store if the SSE-class value is already
8270 // in memory, such as if it is on the callstack.
Chris Lattner492a43e2010-09-22 01:28:21 +00008271 if (isScalarFPTypeInSSEReg(TheVT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00008272 assert(DstTy == MVT::i64 && "Invalid FP_TO_SINT to lower!");
Chris Lattner07290932010-09-22 01:05:16 +00008273 Chain = DAG.getStore(Chain, DL, Value, StackSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00008274 MachinePointerInfo::getFixedStack(SSFI),
David Greene67c9d422010-02-15 16:53:33 +00008275 false, false, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +00008276 SDVTList Tys = DAG.getVTList(Op.getOperand(0).getValueType(), MVT::Other);
Dan Gohman475871a2008-07-27 21:46:04 +00008277 SDValue Ops[] = {
Chris Lattner492a43e2010-09-22 01:28:21 +00008278 Chain, StackSlot, DAG.getValueType(TheVT)
Chris Lattner5a88b832007-02-25 07:10:00 +00008279 };
Michael J. Spencerec38de22010-10-10 22:04:20 +00008280
Chris Lattner492a43e2010-09-22 01:28:21 +00008281 MachineMemOperand *MMO =
8282 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
8283 MachineMemOperand::MOLoad, MemSize, MemSize);
8284 Value = DAG.getMemIntrinsicNode(X86ISD::FLD, DL, Tys, Ops, 3,
8285 DstTy, MMO);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008286 Chain = Value.getValue(1);
David Greene3f2bf852009-11-12 20:49:22 +00008287 SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize, false);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008288 StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
8289 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00008290
Chris Lattner07290932010-09-22 01:05:16 +00008291 MachineMemOperand *MMO =
8292 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
8293 MachineMemOperand::MOStore, MemSize, MemSize);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00008294
Michael J. Spencer1a2d0612012-02-24 19:01:22 +00008295 if (Opc != X86ISD::WIN_FTOL) {
8296 // Build the FP_TO_INT*_IN_MEM
8297 SDValue Ops[] = { Chain, Value, StackSlot };
8298 SDValue FIST = DAG.getMemIntrinsicNode(Opc, DL, DAG.getVTList(MVT::Other),
8299 Ops, 3, DstTy, MMO);
8300 return std::make_pair(FIST, StackSlot);
8301 } else {
8302 SDValue ftol = DAG.getNode(X86ISD::WIN_FTOL, DL,
8303 DAG.getVTList(MVT::Other, MVT::Glue),
8304 Chain, Value);
8305 SDValue eax = DAG.getCopyFromReg(ftol, DL, X86::EAX,
8306 MVT::i32, ftol.getValue(1));
8307 SDValue edx = DAG.getCopyFromReg(eax.getValue(1), DL, X86::EDX,
8308 MVT::i32, eax.getValue(2));
NAKAMURA Takumi9a68fdc2012-02-25 03:37:25 +00008309 SDValue Ops[] = { eax, edx };
8310 SDValue pair = IsReplace
8311 ? DAG.getNode(ISD::BUILD_PAIR, DL, MVT::i64, Ops, 2)
8312 : DAG.getMergeValues(Ops, 2, DL);
Michael J. Spencer1a2d0612012-02-24 19:01:22 +00008313 return std::make_pair(pair, SDValue());
8314 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00008315}
8316
Michael Liaoa7554632012-10-23 17:36:08 +00008317SDValue X86TargetLowering::lowerZERO_EXTEND(SDValue Op, SelectionDAG &DAG) const {
8318 DebugLoc DL = Op.getDebugLoc();
8319 EVT VT = Op.getValueType();
8320 SDValue In = Op.getOperand(0);
8321 EVT SVT = In.getValueType();
8322
8323 if (!VT.is256BitVector() || !SVT.is128BitVector() ||
8324 VT.getVectorNumElements() != SVT.getVectorNumElements())
8325 return SDValue();
8326
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00008327 assert(Subtarget->hasFp256() && "256-bit vector is observed without AVX!");
Michael Liaoa7554632012-10-23 17:36:08 +00008328
8329 // AVX2 has better support of integer extending.
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00008330 if (Subtarget->hasInt256())
Michael Liaoa7554632012-10-23 17:36:08 +00008331 return DAG.getNode(X86ISD::VZEXT, DL, VT, In);
8332
8333 SDValue Lo = DAG.getNode(X86ISD::VZEXT, DL, MVT::v4i32, In);
8334 static const int Mask[] = {4, 5, 6, 7, -1, -1, -1, -1};
8335 SDValue Hi = DAG.getNode(X86ISD::VZEXT, DL, MVT::v4i32,
8336 DAG.getVectorShuffle(MVT::v8i16, DL, In, DAG.getUNDEF(MVT::v8i16), &Mask[0]));
8337
8338 return DAG.getNode(ISD::CONCAT_VECTORS, DL, MVT::v8i32, Lo, Hi);
8339}
8340
Michael Liaobedcbd42012-10-16 18:14:11 +00008341SDValue X86TargetLowering::lowerTRUNCATE(SDValue Op, SelectionDAG &DAG) const {
8342 DebugLoc DL = Op.getDebugLoc();
8343 EVT VT = Op.getValueType();
8344 EVT SVT = Op.getOperand(0).getValueType();
8345
8346 if (!VT.is128BitVector() || !SVT.is256BitVector() ||
8347 VT.getVectorNumElements() != SVT.getVectorNumElements())
8348 return SDValue();
8349
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00008350 assert(Subtarget->hasFp256() && "256-bit vector is observed without AVX!");
Michael Liaobedcbd42012-10-16 18:14:11 +00008351
8352 unsigned NumElems = VT.getVectorNumElements();
8353 EVT NVT = EVT::getVectorVT(*DAG.getContext(), VT.getVectorElementType(),
8354 NumElems * 2);
8355
8356 SDValue In = Op.getOperand(0);
8357 SmallVector<int, 16> MaskVec(NumElems * 2, -1);
8358 // Prepare truncation shuffle mask
8359 for (unsigned i = 0; i != NumElems; ++i)
8360 MaskVec[i] = i * 2;
8361 SDValue V = DAG.getVectorShuffle(NVT, DL,
8362 DAG.getNode(ISD::BITCAST, DL, NVT, In),
8363 DAG.getUNDEF(NVT), &MaskVec[0]);
8364 return DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, VT, V,
8365 DAG.getIntPtrConstant(0));
8366}
8367
Dan Gohmand858e902010-04-17 15:26:15 +00008368SDValue X86TargetLowering::LowerFP_TO_SINT(SDValue Op,
8369 SelectionDAG &DAG) const {
Michael Liaobedcbd42012-10-16 18:14:11 +00008370 if (Op.getValueType().isVector()) {
8371 if (Op.getValueType() == MVT::v8i16)
8372 return DAG.getNode(ISD::TRUNCATE, Op.getDebugLoc(), Op.getValueType(),
8373 DAG.getNode(ISD::FP_TO_SINT, Op.getDebugLoc(),
8374 MVT::v8i32, Op.getOperand(0)));
Eli Friedman23ef1052009-06-06 03:57:58 +00008375 return SDValue();
Michael Liaobedcbd42012-10-16 18:14:11 +00008376 }
Eli Friedman23ef1052009-06-06 03:57:58 +00008377
NAKAMURA Takumi9a68fdc2012-02-25 03:37:25 +00008378 std::pair<SDValue,SDValue> Vals = FP_TO_INTHelper(Op, DAG,
8379 /*IsSigned=*/ true, /*IsReplace=*/ false);
Dan Gohman475871a2008-07-27 21:46:04 +00008380 SDValue FIST = Vals.first, StackSlot = Vals.second;
Eli Friedman36df4992009-05-27 00:47:34 +00008381 // If FP_TO_INTHelper failed, the node is actually supposed to be Legal.
8382 if (FIST.getNode() == 0) return Op;
Scott Michelfdc40a02009-02-17 22:15:04 +00008383
Michael J. Spencer1a2d0612012-02-24 19:01:22 +00008384 if (StackSlot.getNode())
8385 // Load the result.
8386 return DAG.getLoad(Op.getValueType(), Op.getDebugLoc(),
8387 FIST, StackSlot, MachinePointerInfo(),
8388 false, false, false, 0);
Craig Topper69947b92012-04-23 06:57:04 +00008389
8390 // The node is the result.
8391 return FIST;
Chris Lattner27a6c732007-11-24 07:07:01 +00008392}
8393
Dan Gohmand858e902010-04-17 15:26:15 +00008394SDValue X86TargetLowering::LowerFP_TO_UINT(SDValue Op,
8395 SelectionDAG &DAG) const {
NAKAMURA Takumi9a68fdc2012-02-25 03:37:25 +00008396 std::pair<SDValue,SDValue> Vals = FP_TO_INTHelper(Op, DAG,
8397 /*IsSigned=*/ false, /*IsReplace=*/ false);
Eli Friedman948e95a2009-05-23 09:59:16 +00008398 SDValue FIST = Vals.first, StackSlot = Vals.second;
8399 assert(FIST.getNode() && "Unexpected failure");
8400
NAKAMURA Takumi9a68fdc2012-02-25 03:37:25 +00008401 if (StackSlot.getNode())
8402 // Load the result.
8403 return DAG.getLoad(Op.getValueType(), Op.getDebugLoc(),
8404 FIST, StackSlot, MachinePointerInfo(),
8405 false, false, false, 0);
Craig Topper69947b92012-04-23 06:57:04 +00008406
8407 // The node is the result.
8408 return FIST;
Eli Friedman948e95a2009-05-23 09:59:16 +00008409}
8410
Michael Liao9d796db2012-10-10 16:32:15 +00008411SDValue X86TargetLowering::lowerFP_EXTEND(SDValue Op,
8412 SelectionDAG &DAG) const {
8413 DebugLoc DL = Op.getDebugLoc();
8414 EVT VT = Op.getValueType();
8415 SDValue In = Op.getOperand(0);
8416 EVT SVT = In.getValueType();
8417
8418 assert(SVT == MVT::v2f32 && "Only customize MVT::v2f32 type legalization!");
8419
8420 return DAG.getNode(X86ISD::VFPEXT, DL, VT,
8421 DAG.getNode(ISD::CONCAT_VECTORS, DL, MVT::v4f32,
8422 In, DAG.getUNDEF(SVT)));
8423}
8424
Craig Topper43620672012-09-08 07:31:51 +00008425SDValue X86TargetLowering::LowerFABS(SDValue Op, SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00008426 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008427 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00008428 EVT VT = Op.getValueType();
8429 EVT EltVT = VT;
Craig Topper43620672012-09-08 07:31:51 +00008430 unsigned NumElts = VT == MVT::f64 ? 2 : 4;
8431 if (VT.isVector()) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00008432 EltVT = VT.getVectorElementType();
Craig Topper43620672012-09-08 07:31:51 +00008433 NumElts = VT.getVectorNumElements();
Evan Cheng0db9fe62006-04-25 20:13:52 +00008434 }
Craig Topper43620672012-09-08 07:31:51 +00008435 Constant *C;
8436 if (EltVT == MVT::f64)
8437 C = ConstantFP::get(*Context, APFloat(APInt(64, ~(1ULL << 63))));
8438 else
8439 C = ConstantFP::get(*Context, APFloat(APInt(32, ~(1U << 31))));
8440 C = ConstantVector::getSplat(NumElts, C);
8441 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy());
8442 unsigned Alignment = cast<ConstantPoolSDNode>(CPIdx)->getAlignment();
Dale Johannesenace16102009-02-03 19:33:06 +00008443 SDValue Mask = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00008444 MachinePointerInfo::getConstantPool(),
Craig Topper43620672012-09-08 07:31:51 +00008445 false, false, false, Alignment);
8446 if (VT.isVector()) {
8447 MVT ANDVT = VT.is128BitVector() ? MVT::v2i64 : MVT::v4i64;
8448 return DAG.getNode(ISD::BITCAST, dl, VT,
8449 DAG.getNode(ISD::AND, dl, ANDVT,
8450 DAG.getNode(ISD::BITCAST, dl, ANDVT,
8451 Op.getOperand(0)),
8452 DAG.getNode(ISD::BITCAST, dl, ANDVT, Mask)));
8453 }
Dale Johannesenace16102009-02-03 19:33:06 +00008454 return DAG.getNode(X86ISD::FAND, dl, VT, Op.getOperand(0), Mask);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008455}
8456
Dan Gohmand858e902010-04-17 15:26:15 +00008457SDValue X86TargetLowering::LowerFNEG(SDValue Op, SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00008458 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008459 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00008460 EVT VT = Op.getValueType();
8461 EVT EltVT = VT;
Chad Rosiera860b182011-12-15 01:02:25 +00008462 unsigned NumElts = VT == MVT::f64 ? 2 : 4;
8463 if (VT.isVector()) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00008464 EltVT = VT.getVectorElementType();
Chad Rosiera860b182011-12-15 01:02:25 +00008465 NumElts = VT.getVectorNumElements();
8466 }
Chris Lattner4ca829e2012-01-25 06:02:56 +00008467 Constant *C;
8468 if (EltVT == MVT::f64)
8469 C = ConstantFP::get(*Context, APFloat(APInt(64, 1ULL << 63)));
8470 else
8471 C = ConstantFP::get(*Context, APFloat(APInt(32, 1U << 31)));
8472 C = ConstantVector::getSplat(NumElts, C);
Craig Toppercacd9d62012-09-08 07:46:05 +00008473 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy());
8474 unsigned Alignment = cast<ConstantPoolSDNode>(CPIdx)->getAlignment();
Dale Johannesenace16102009-02-03 19:33:06 +00008475 SDValue Mask = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00008476 MachinePointerInfo::getConstantPool(),
Craig Toppercacd9d62012-09-08 07:46:05 +00008477 false, false, false, Alignment);
Duncan Sands83ec4b62008-06-06 12:08:01 +00008478 if (VT.isVector()) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00008479 MVT XORVT = VT.is128BitVector() ? MVT::v2i64 : MVT::v4i64;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008480 return DAG.getNode(ISD::BITCAST, dl, VT,
Chad Rosiera860b182011-12-15 01:02:25 +00008481 DAG.getNode(ISD::XOR, dl, XORVT,
Craig Topper69947b92012-04-23 06:57:04 +00008482 DAG.getNode(ISD::BITCAST, dl, XORVT,
8483 Op.getOperand(0)),
8484 DAG.getNode(ISD::BITCAST, dl, XORVT, Mask)));
Evan Chengd4d01b72007-07-19 23:36:01 +00008485 }
Craig Topper69947b92012-04-23 06:57:04 +00008486
8487 return DAG.getNode(X86ISD::FXOR, dl, VT, Op.getOperand(0), Mask);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008488}
8489
Dan Gohmand858e902010-04-17 15:26:15 +00008490SDValue X86TargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00008491 LLVMContext *Context = DAG.getContext();
Dan Gohman475871a2008-07-27 21:46:04 +00008492 SDValue Op0 = Op.getOperand(0);
8493 SDValue Op1 = Op.getOperand(1);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008494 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00008495 EVT VT = Op.getValueType();
8496 EVT SrcVT = Op1.getValueType();
Evan Cheng73d6cf12007-01-05 21:37:56 +00008497
8498 // If second operand is smaller, extend it first.
Duncan Sands8e4eb092008-06-08 20:54:56 +00008499 if (SrcVT.bitsLT(VT)) {
Dale Johannesenace16102009-02-03 19:33:06 +00008500 Op1 = DAG.getNode(ISD::FP_EXTEND, dl, VT, Op1);
Evan Cheng73d6cf12007-01-05 21:37:56 +00008501 SrcVT = VT;
8502 }
Dale Johannesen61c7ef32007-10-21 01:07:44 +00008503 // And if it is bigger, shrink it first.
Duncan Sands8e4eb092008-06-08 20:54:56 +00008504 if (SrcVT.bitsGT(VT)) {
Dale Johannesenace16102009-02-03 19:33:06 +00008505 Op1 = DAG.getNode(ISD::FP_ROUND, dl, VT, Op1, DAG.getIntPtrConstant(1));
Dale Johannesen61c7ef32007-10-21 01:07:44 +00008506 SrcVT = VT;
Dale Johannesen61c7ef32007-10-21 01:07:44 +00008507 }
8508
8509 // At this point the operands and the result should have the same
8510 // type, and that won't be f80 since that is not custom lowered.
Evan Cheng73d6cf12007-01-05 21:37:56 +00008511
Evan Cheng68c47cb2007-01-05 07:55:56 +00008512 // First get the sign bit of second operand.
Chad Rosier01d426e2011-12-15 01:16:09 +00008513 SmallVector<Constant*,4> CV;
Owen Anderson825b72b2009-08-11 20:47:22 +00008514 if (SrcVT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00008515 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 1ULL << 63))));
8516 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 0))));
Evan Cheng68c47cb2007-01-05 07:55:56 +00008517 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00008518 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 1U << 31))));
8519 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
8520 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
8521 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
Evan Cheng68c47cb2007-01-05 07:55:56 +00008522 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00008523 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00008524 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00008525 SDValue Mask1 = DAG.getLoad(SrcVT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00008526 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00008527 false, false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00008528 SDValue SignBit = DAG.getNode(X86ISD::FAND, dl, SrcVT, Op1, Mask1);
Evan Cheng68c47cb2007-01-05 07:55:56 +00008529
8530 // Shift sign bit right or left if the two operands have different types.
Duncan Sands8e4eb092008-06-08 20:54:56 +00008531 if (SrcVT.bitsGT(VT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00008532 // Op0 is MVT::f32, Op1 is MVT::f64.
8533 SignBit = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f64, SignBit);
8534 SignBit = DAG.getNode(X86ISD::FSRL, dl, MVT::v2f64, SignBit,
8535 DAG.getConstant(32, MVT::i32));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008536 SignBit = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, SignBit);
Owen Anderson825b72b2009-08-11 20:47:22 +00008537 SignBit = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f32, SignBit,
Chris Lattner0bd48932008-01-17 07:00:52 +00008538 DAG.getIntPtrConstant(0));
Evan Cheng68c47cb2007-01-05 07:55:56 +00008539 }
8540
Evan Cheng73d6cf12007-01-05 21:37:56 +00008541 // Clear first operand sign bit.
8542 CV.clear();
Owen Anderson825b72b2009-08-11 20:47:22 +00008543 if (VT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00008544 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, ~(1ULL << 63)))));
8545 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 0))));
Evan Cheng73d6cf12007-01-05 21:37:56 +00008546 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00008547 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, ~(1U << 31)))));
8548 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
8549 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
8550 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
Evan Cheng73d6cf12007-01-05 21:37:56 +00008551 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00008552 C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00008553 CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00008554 SDValue Mask2 = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00008555 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00008556 false, false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00008557 SDValue Val = DAG.getNode(X86ISD::FAND, dl, VT, Op0, Mask2);
Evan Cheng73d6cf12007-01-05 21:37:56 +00008558
8559 // Or the value with the sign bit.
Dale Johannesenace16102009-02-03 19:33:06 +00008560 return DAG.getNode(X86ISD::FOR, dl, VT, Val, SignBit);
Evan Cheng68c47cb2007-01-05 07:55:56 +00008561}
8562
Craig Topper55b24052012-09-11 06:15:32 +00008563static SDValue LowerFGETSIGN(SDValue Op, SelectionDAG &DAG) {
Stuart Hastings4fd0dee2011-06-01 04:39:42 +00008564 SDValue N0 = Op.getOperand(0);
8565 DebugLoc dl = Op.getDebugLoc();
8566 EVT VT = Op.getValueType();
8567
8568 // Lower ISD::FGETSIGN to (AND (X86ISD::FGETSIGNx86 ...) 1).
8569 SDValue xFGETSIGN = DAG.getNode(X86ISD::FGETSIGNx86, dl, VT, N0,
8570 DAG.getConstant(1, VT));
8571 return DAG.getNode(ISD::AND, dl, VT, xFGETSIGN, DAG.getConstant(1, VT));
8572}
8573
Michael Liaof966e4e2012-09-13 20:24:54 +00008574// LowerVectorAllZeroTest - Check whether an OR'd tree is PTEST-able.
8575//
8576SDValue X86TargetLowering::LowerVectorAllZeroTest(SDValue Op, SelectionDAG &DAG) const {
8577 assert(Op.getOpcode() == ISD::OR && "Only check OR'd tree.");
8578
8579 if (!Subtarget->hasSSE41())
8580 return SDValue();
8581
8582 if (!Op->hasOneUse())
8583 return SDValue();
8584
8585 SDNode *N = Op.getNode();
8586 DebugLoc DL = N->getDebugLoc();
8587
8588 SmallVector<SDValue, 8> Opnds;
8589 DenseMap<SDValue, unsigned> VecInMap;
8590 EVT VT = MVT::Other;
8591
8592 // Recognize a special case where a vector is casted into wide integer to
8593 // test all 0s.
8594 Opnds.push_back(N->getOperand(0));
8595 Opnds.push_back(N->getOperand(1));
8596
8597 for (unsigned Slot = 0, e = Opnds.size(); Slot < e; ++Slot) {
8598 SmallVector<SDValue, 8>::const_iterator I = Opnds.begin() + Slot;
8599 // BFS traverse all OR'd operands.
8600 if (I->getOpcode() == ISD::OR) {
8601 Opnds.push_back(I->getOperand(0));
8602 Opnds.push_back(I->getOperand(1));
8603 // Re-evaluate the number of nodes to be traversed.
8604 e += 2; // 2 more nodes (LHS and RHS) are pushed.
8605 continue;
8606 }
8607
8608 // Quit if a non-EXTRACT_VECTOR_ELT
8609 if (I->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
8610 return SDValue();
8611
8612 // Quit if without a constant index.
8613 SDValue Idx = I->getOperand(1);
8614 if (!isa<ConstantSDNode>(Idx))
8615 return SDValue();
8616
8617 SDValue ExtractedFromVec = I->getOperand(0);
8618 DenseMap<SDValue, unsigned>::iterator M = VecInMap.find(ExtractedFromVec);
8619 if (M == VecInMap.end()) {
8620 VT = ExtractedFromVec.getValueType();
8621 // Quit if not 128/256-bit vector.
8622 if (!VT.is128BitVector() && !VT.is256BitVector())
8623 return SDValue();
8624 // Quit if not the same type.
8625 if (VecInMap.begin() != VecInMap.end() &&
8626 VT != VecInMap.begin()->first.getValueType())
8627 return SDValue();
8628 M = VecInMap.insert(std::make_pair(ExtractedFromVec, 0)).first;
8629 }
8630 M->second |= 1U << cast<ConstantSDNode>(Idx)->getZExtValue();
8631 }
8632
8633 assert((VT.is128BitVector() || VT.is256BitVector()) &&
Michael Liao9aba7ea2012-09-13 20:30:16 +00008634 "Not extracted from 128-/256-bit vector.");
Michael Liaof966e4e2012-09-13 20:24:54 +00008635
8636 unsigned FullMask = (1U << VT.getVectorNumElements()) - 1U;
8637 SmallVector<SDValue, 8> VecIns;
8638
8639 for (DenseMap<SDValue, unsigned>::const_iterator
8640 I = VecInMap.begin(), E = VecInMap.end(); I != E; ++I) {
8641 // Quit if not all elements are used.
8642 if (I->second != FullMask)
8643 return SDValue();
8644 VecIns.push_back(I->first);
8645 }
8646
8647 EVT TestVT = VT.is128BitVector() ? MVT::v2i64 : MVT::v4i64;
8648
8649 // Cast all vectors into TestVT for PTEST.
8650 for (unsigned i = 0, e = VecIns.size(); i < e; ++i)
8651 VecIns[i] = DAG.getNode(ISD::BITCAST, DL, TestVT, VecIns[i]);
8652
8653 // If more than one full vectors are evaluated, OR them first before PTEST.
8654 for (unsigned Slot = 0, e = VecIns.size(); e - Slot > 1; Slot += 2, e += 1) {
8655 // Each iteration will OR 2 nodes and append the result until there is only
8656 // 1 node left, i.e. the final OR'd value of all vectors.
8657 SDValue LHS = VecIns[Slot];
8658 SDValue RHS = VecIns[Slot + 1];
8659 VecIns.push_back(DAG.getNode(ISD::OR, DL, TestVT, LHS, RHS));
8660 }
8661
8662 return DAG.getNode(X86ISD::PTEST, DL, MVT::i32,
8663 VecIns.back(), VecIns.back());
8664}
8665
Dan Gohman076aee32009-03-04 19:44:21 +00008666/// Emit nodes that will be selected as "test Op0,Op0", or something
8667/// equivalent.
Dan Gohman31125812009-03-07 01:58:32 +00008668SDValue X86TargetLowering::EmitTest(SDValue Op, unsigned X86CC,
Evan Cheng552f09a2010-04-26 19:06:11 +00008669 SelectionDAG &DAG) const {
Dan Gohman076aee32009-03-04 19:44:21 +00008670 DebugLoc dl = Op.getDebugLoc();
8671
Dan Gohman31125812009-03-07 01:58:32 +00008672 // CF and OF aren't always set the way we want. Determine which
8673 // of these we need.
8674 bool NeedCF = false;
8675 bool NeedOF = false;
8676 switch (X86CC) {
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008677 default: break;
Dan Gohman31125812009-03-07 01:58:32 +00008678 case X86::COND_A: case X86::COND_AE:
8679 case X86::COND_B: case X86::COND_BE:
8680 NeedCF = true;
8681 break;
8682 case X86::COND_G: case X86::COND_GE:
8683 case X86::COND_L: case X86::COND_LE:
8684 case X86::COND_O: case X86::COND_NO:
8685 NeedOF = true;
8686 break;
Dan Gohman31125812009-03-07 01:58:32 +00008687 }
8688
Dan Gohman076aee32009-03-04 19:44:21 +00008689 // See if we can use the EFLAGS value from the operand instead of
Dan Gohman31125812009-03-07 01:58:32 +00008690 // doing a separate TEST. TEST always sets OF and CF to 0, so unless
8691 // we prove that the arithmetic won't overflow, we can't use OF or CF.
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008692 if (Op.getResNo() != 0 || NeedOF || NeedCF)
8693 // Emit a CMP with 0, which is the TEST pattern.
8694 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op,
8695 DAG.getConstant(0, Op.getValueType()));
8696
8697 unsigned Opcode = 0;
8698 unsigned NumOperands = 0;
Nadav Rotemb9d6b842012-08-18 17:53:03 +00008699
8700 // Truncate operations may prevent the merge of the SETCC instruction
8701 // and the arithmetic intruction before it. Attempt to truncate the operands
8702 // of the arithmetic instruction and use a reduced bit-width instruction.
8703 bool NeedTruncation = false;
8704 SDValue ArithOp = Op;
8705 if (Op->getOpcode() == ISD::TRUNCATE && Op->hasOneUse()) {
8706 SDValue Arith = Op->getOperand(0);
8707 // Both the trunc and the arithmetic op need to have one user each.
8708 if (Arith->hasOneUse())
8709 switch (Arith.getOpcode()) {
8710 default: break;
8711 case ISD::ADD:
8712 case ISD::SUB:
8713 case ISD::AND:
8714 case ISD::OR:
8715 case ISD::XOR: {
8716 NeedTruncation = true;
8717 ArithOp = Arith;
8718 }
8719 }
8720 }
8721
8722 // NOTICE: In the code below we use ArithOp to hold the arithmetic operation
8723 // which may be the result of a CAST. We use the variable 'Op', which is the
8724 // non-casted variable when we check for possible users.
8725 switch (ArithOp.getOpcode()) {
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008726 case ISD::ADD:
8727 // Due to an isel shortcoming, be conservative if this add is likely to be
8728 // selected as part of a load-modify-store instruction. When the root node
8729 // in a match is a store, isel doesn't know how to remap non-chain non-flag
8730 // uses of other nodes in the match, such as the ADD in this case. This
8731 // leads to the ADD being left around and reselected, with the result being
8732 // two adds in the output. Alas, even if none our users are stores, that
8733 // doesn't prove we're O.K. Ergo, if we have any parents that aren't
8734 // CopyToReg or SETCC, eschew INC/DEC. A better fix seems to require
8735 // climbing the DAG back to the root, and it doesn't seem to be worth the
8736 // effort.
8737 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
Pete Cooper2d496892011-11-15 21:57:53 +00008738 UE = Op.getNode()->use_end(); UI != UE; ++UI)
8739 if (UI->getOpcode() != ISD::CopyToReg &&
8740 UI->getOpcode() != ISD::SETCC &&
8741 UI->getOpcode() != ISD::STORE)
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008742 goto default_case;
8743
8744 if (ConstantSDNode *C =
Nadav Rotemb9d6b842012-08-18 17:53:03 +00008745 dyn_cast<ConstantSDNode>(ArithOp.getNode()->getOperand(1))) {
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008746 // An add of one will be selected as an INC.
8747 if (C->getAPIntValue() == 1) {
8748 Opcode = X86ISD::INC;
8749 NumOperands = 1;
8750 break;
Dan Gohmane220c4b2009-09-18 19:59:53 +00008751 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008752
8753 // An add of negative one (subtract of one) will be selected as a DEC.
8754 if (C->getAPIntValue().isAllOnesValue()) {
8755 Opcode = X86ISD::DEC;
8756 NumOperands = 1;
8757 break;
8758 }
Dan Gohman076aee32009-03-04 19:44:21 +00008759 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008760
8761 // Otherwise use a regular EFLAGS-setting add.
8762 Opcode = X86ISD::ADD;
8763 NumOperands = 2;
8764 break;
8765 case ISD::AND: {
8766 // If the primary and result isn't used, don't bother using X86ISD::AND,
8767 // because a TEST instruction will be better.
8768 bool NonFlagUse = false;
8769 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
8770 UE = Op.getNode()->use_end(); UI != UE; ++UI) {
8771 SDNode *User = *UI;
8772 unsigned UOpNo = UI.getOperandNo();
8773 if (User->getOpcode() == ISD::TRUNCATE && User->hasOneUse()) {
8774 // Look pass truncate.
8775 UOpNo = User->use_begin().getOperandNo();
8776 User = *User->use_begin();
8777 }
8778
8779 if (User->getOpcode() != ISD::BRCOND &&
8780 User->getOpcode() != ISD::SETCC &&
Nadav Rotemb9d6b842012-08-18 17:53:03 +00008781 !(User->getOpcode() == ISD::SELECT && UOpNo == 0)) {
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008782 NonFlagUse = true;
8783 break;
8784 }
Dan Gohman076aee32009-03-04 19:44:21 +00008785 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008786
8787 if (!NonFlagUse)
8788 break;
8789 }
8790 // FALL THROUGH
8791 case ISD::SUB:
8792 case ISD::OR:
8793 case ISD::XOR:
8794 // Due to the ISEL shortcoming noted above, be conservative if this op is
8795 // likely to be selected as part of a load-modify-store instruction.
8796 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
8797 UE = Op.getNode()->use_end(); UI != UE; ++UI)
8798 if (UI->getOpcode() == ISD::STORE)
8799 goto default_case;
8800
8801 // Otherwise use a regular EFLAGS-setting instruction.
Nadav Rotemb9d6b842012-08-18 17:53:03 +00008802 switch (ArithOp.getOpcode()) {
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008803 default: llvm_unreachable("unexpected operator!");
Nadav Rotemb9d6b842012-08-18 17:53:03 +00008804 case ISD::SUB: Opcode = X86ISD::SUB; break;
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008805 case ISD::XOR: Opcode = X86ISD::XOR; break;
8806 case ISD::AND: Opcode = X86ISD::AND; break;
Michael Liaof966e4e2012-09-13 20:24:54 +00008807 case ISD::OR: {
8808 if (!NeedTruncation && (X86CC == X86::COND_E || X86CC == X86::COND_NE)) {
8809 SDValue EFLAGS = LowerVectorAllZeroTest(Op, DAG);
8810 if (EFLAGS.getNode())
8811 return EFLAGS;
8812 }
8813 Opcode = X86ISD::OR;
8814 break;
8815 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008816 }
8817
8818 NumOperands = 2;
8819 break;
8820 case X86ISD::ADD:
8821 case X86ISD::SUB:
8822 case X86ISD::INC:
8823 case X86ISD::DEC:
8824 case X86ISD::OR:
8825 case X86ISD::XOR:
8826 case X86ISD::AND:
8827 return SDValue(Op.getNode(), 1);
8828 default:
8829 default_case:
8830 break;
Dan Gohman076aee32009-03-04 19:44:21 +00008831 }
8832
Nadav Rotemb9d6b842012-08-18 17:53:03 +00008833 // If we found that truncation is beneficial, perform the truncation and
8834 // update 'Op'.
8835 if (NeedTruncation) {
8836 EVT VT = Op.getValueType();
8837 SDValue WideVal = Op->getOperand(0);
8838 EVT WideVT = WideVal.getValueType();
8839 unsigned ConvertedOp = 0;
8840 // Use a target machine opcode to prevent further DAGCombine
8841 // optimizations that may separate the arithmetic operations
8842 // from the setcc node.
8843 switch (WideVal.getOpcode()) {
8844 default: break;
8845 case ISD::ADD: ConvertedOp = X86ISD::ADD; break;
8846 case ISD::SUB: ConvertedOp = X86ISD::SUB; break;
8847 case ISD::AND: ConvertedOp = X86ISD::AND; break;
8848 case ISD::OR: ConvertedOp = X86ISD::OR; break;
8849 case ISD::XOR: ConvertedOp = X86ISD::XOR; break;
8850 }
8851
8852 if (ConvertedOp) {
8853 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
8854 if (TLI.isOperationLegal(WideVal.getOpcode(), WideVT)) {
8855 SDValue V0 = DAG.getNode(ISD::TRUNCATE, dl, VT, WideVal.getOperand(0));
8856 SDValue V1 = DAG.getNode(ISD::TRUNCATE, dl, VT, WideVal.getOperand(1));
8857 Op = DAG.getNode(ConvertedOp, dl, VT, V0, V1);
8858 }
8859 }
8860 }
8861
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008862 if (Opcode == 0)
8863 // Emit a CMP with 0, which is the TEST pattern.
8864 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op,
8865 DAG.getConstant(0, Op.getValueType()));
8866
8867 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::i32);
8868 SmallVector<SDValue, 4> Ops;
8869 for (unsigned i = 0; i != NumOperands; ++i)
8870 Ops.push_back(Op.getOperand(i));
8871
8872 SDValue New = DAG.getNode(Opcode, dl, VTs, &Ops[0], NumOperands);
8873 DAG.ReplaceAllUsesWith(Op, New);
8874 return SDValue(New.getNode(), 1);
Dan Gohman076aee32009-03-04 19:44:21 +00008875}
8876
8877/// Emit nodes that will be selected as "cmp Op0,Op1", or something
8878/// equivalent.
Dan Gohman31125812009-03-07 01:58:32 +00008879SDValue X86TargetLowering::EmitCmp(SDValue Op0, SDValue Op1, unsigned X86CC,
Evan Cheng552f09a2010-04-26 19:06:11 +00008880 SelectionDAG &DAG) const {
Dan Gohman076aee32009-03-04 19:44:21 +00008881 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op1))
8882 if (C->getAPIntValue() == 0)
Evan Cheng552f09a2010-04-26 19:06:11 +00008883 return EmitTest(Op0, X86CC, DAG);
Dan Gohman076aee32009-03-04 19:44:21 +00008884
8885 DebugLoc dl = Op0.getDebugLoc();
Manman Ren39ad5682012-08-08 00:51:41 +00008886 if ((Op0.getValueType() == MVT::i8 || Op0.getValueType() == MVT::i16 ||
8887 Op0.getValueType() == MVT::i32 || Op0.getValueType() == MVT::i64)) {
8888 // Use SUB instead of CMP to enable CSE between SUB and CMP.
8889 SDVTList VTs = DAG.getVTList(Op0.getValueType(), MVT::i32);
8890 SDValue Sub = DAG.getNode(X86ISD::SUB, dl, VTs,
8891 Op0, Op1);
8892 return SDValue(Sub.getNode(), 1);
8893 }
Owen Anderson825b72b2009-08-11 20:47:22 +00008894 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op0, Op1);
Dan Gohman076aee32009-03-04 19:44:21 +00008895}
8896
Benjamin Kramer17c836c2012-04-27 12:07:43 +00008897/// Convert a comparison if required by the subtarget.
8898SDValue X86TargetLowering::ConvertCmpIfNecessary(SDValue Cmp,
8899 SelectionDAG &DAG) const {
8900 // If the subtarget does not support the FUCOMI instruction, floating-point
8901 // comparisons have to be converted.
8902 if (Subtarget->hasCMov() ||
8903 Cmp.getOpcode() != X86ISD::CMP ||
8904 !Cmp.getOperand(0).getValueType().isFloatingPoint() ||
8905 !Cmp.getOperand(1).getValueType().isFloatingPoint())
8906 return Cmp;
8907
8908 // The instruction selector will select an FUCOM instruction instead of
8909 // FUCOMI, which writes the comparison result to FPSW instead of EFLAGS. Hence
8910 // build an SDNode sequence that transfers the result from FPSW into EFLAGS:
8911 // (X86sahf (trunc (srl (X86fp_stsw (trunc (X86cmp ...)), 8))))
8912 DebugLoc dl = Cmp.getDebugLoc();
8913 SDValue TruncFPSW = DAG.getNode(ISD::TRUNCATE, dl, MVT::i16, Cmp);
8914 SDValue FNStSW = DAG.getNode(X86ISD::FNSTSW16r, dl, MVT::i16, TruncFPSW);
8915 SDValue Srl = DAG.getNode(ISD::SRL, dl, MVT::i16, FNStSW,
8916 DAG.getConstant(8, MVT::i8));
8917 SDValue TruncSrl = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Srl);
8918 return DAG.getNode(X86ISD::SAHF, dl, MVT::i32, TruncSrl);
8919}
8920
Evan Cheng4e544802012-12-05 00:10:38 +00008921static bool isAllOnes(SDValue V) {
8922 ConstantSDNode *C = dyn_cast<ConstantSDNode>(V);
8923 return C && C->isAllOnesValue();
8924}
8925
Evan Chengd40d03e2010-01-06 19:38:29 +00008926/// LowerToBT - Result of 'and' is compared against zero. Turn it into a BT node
8927/// if it's possible.
Evan Cheng5528e7b2010-04-21 01:47:12 +00008928SDValue X86TargetLowering::LowerToBT(SDValue And, ISD::CondCode CC,
8929 DebugLoc dl, SelectionDAG &DAG) const {
Evan Cheng2c755ba2010-02-27 07:36:59 +00008930 SDValue Op0 = And.getOperand(0);
8931 SDValue Op1 = And.getOperand(1);
8932 if (Op0.getOpcode() == ISD::TRUNCATE)
8933 Op0 = Op0.getOperand(0);
8934 if (Op1.getOpcode() == ISD::TRUNCATE)
8935 Op1 = Op1.getOperand(0);
8936
Evan Chengd40d03e2010-01-06 19:38:29 +00008937 SDValue LHS, RHS;
Dan Gohman6b13cbc2010-06-24 02:07:59 +00008938 if (Op1.getOpcode() == ISD::SHL)
8939 std::swap(Op0, Op1);
8940 if (Op0.getOpcode() == ISD::SHL) {
Evan Cheng2c755ba2010-02-27 07:36:59 +00008941 if (ConstantSDNode *And00C = dyn_cast<ConstantSDNode>(Op0.getOperand(0)))
8942 if (And00C->getZExtValue() == 1) {
Dan Gohman6b13cbc2010-06-24 02:07:59 +00008943 // If we looked past a truncate, check that it's only truncating away
8944 // known zeros.
8945 unsigned BitWidth = Op0.getValueSizeInBits();
8946 unsigned AndBitWidth = And.getValueSizeInBits();
8947 if (BitWidth > AndBitWidth) {
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00008948 APInt Zeros, Ones;
8949 DAG.ComputeMaskedBits(Op0, Zeros, Ones);
Dan Gohman6b13cbc2010-06-24 02:07:59 +00008950 if (Zeros.countLeadingOnes() < BitWidth - AndBitWidth)
8951 return SDValue();
8952 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00008953 LHS = Op1;
8954 RHS = Op0.getOperand(1);
Evan Chengd40d03e2010-01-06 19:38:29 +00008955 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00008956 } else if (Op1.getOpcode() == ISD::Constant) {
8957 ConstantSDNode *AndRHS = cast<ConstantSDNode>(Op1);
Benjamin Kramerf238f502011-11-23 13:54:17 +00008958 uint64_t AndRHSVal = AndRHS->getZExtValue();
Evan Cheng2c755ba2010-02-27 07:36:59 +00008959 SDValue AndLHS = Op0;
Benjamin Kramerf238f502011-11-23 13:54:17 +00008960
8961 if (AndRHSVal == 1 && AndLHS.getOpcode() == ISD::SRL) {
Evan Chengd40d03e2010-01-06 19:38:29 +00008962 LHS = AndLHS.getOperand(0);
8963 RHS = AndLHS.getOperand(1);
Dan Gohmane5af2d32009-01-29 01:59:02 +00008964 }
Benjamin Kramerf238f502011-11-23 13:54:17 +00008965
8966 // Use BT if the immediate can't be encoded in a TEST instruction.
8967 if (!isUInt<32>(AndRHSVal) && isPowerOf2_64(AndRHSVal)) {
8968 LHS = AndLHS;
8969 RHS = DAG.getConstant(Log2_64_Ceil(AndRHSVal), LHS.getValueType());
8970 }
Evan Chengd40d03e2010-01-06 19:38:29 +00008971 }
Evan Cheng0488db92007-09-25 01:57:46 +00008972
Evan Chengd40d03e2010-01-06 19:38:29 +00008973 if (LHS.getNode()) {
Evan Cheng4e544802012-12-05 00:10:38 +00008974 // If the LHS is of the form (x ^ -1) then replace the LHS with x and flip
8975 // the condition code later.
8976 bool Invert = false;
8977 if (LHS.getOpcode() == ISD::XOR && isAllOnes(LHS.getOperand(1))) {
8978 Invert = true;
8979 LHS = LHS.getOperand(0);
8980 }
8981
Evan Chenge5b51ac2010-04-17 06:13:15 +00008982 // If LHS is i8, promote it to i32 with any_extend. There is no i8 BT
Evan Chengd40d03e2010-01-06 19:38:29 +00008983 // instruction. Since the shift amount is in-range-or-undefined, we know
Evan Chenge5b51ac2010-04-17 06:13:15 +00008984 // that doing a bittest on the i32 value is ok. We extend to i32 because
Evan Chengd40d03e2010-01-06 19:38:29 +00008985 // the encoding for the i16 version is larger than the i32 version.
Evan Chenge5b51ac2010-04-17 06:13:15 +00008986 // Also promote i16 to i32 for performance / code size reason.
8987 if (LHS.getValueType() == MVT::i8 ||
Evan Cheng2bce5f4b2010-04-28 08:30:49 +00008988 LHS.getValueType() == MVT::i16)
Evan Chengd40d03e2010-01-06 19:38:29 +00008989 LHS = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, LHS);
Chris Lattnere55484e2008-12-25 05:34:37 +00008990
Evan Chengd40d03e2010-01-06 19:38:29 +00008991 // If the operand types disagree, extend the shift amount to match. Since
8992 // BT ignores high bits (like shifts) we can use anyextend.
8993 if (LHS.getValueType() != RHS.getValueType())
8994 RHS = DAG.getNode(ISD::ANY_EXTEND, dl, LHS.getValueType(), RHS);
Dan Gohmane5af2d32009-01-29 01:59:02 +00008995
Evan Chengd40d03e2010-01-06 19:38:29 +00008996 SDValue BT = DAG.getNode(X86ISD::BT, dl, MVT::i32, LHS, RHS);
Evan Cheng4e544802012-12-05 00:10:38 +00008997 X86::CondCode Cond = CC == ISD::SETEQ ? X86::COND_AE : X86::COND_B;
8998 // Flip the condition if the LHS was a not instruction
8999 if (Invert)
9000 Cond = X86::GetOppositeBranchCondition(Cond);
Evan Chengd40d03e2010-01-06 19:38:29 +00009001 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
9002 DAG.getConstant(Cond, MVT::i8), BT);
Chris Lattnere55484e2008-12-25 05:34:37 +00009003 }
9004
Evan Cheng54de3ea2010-01-05 06:52:31 +00009005 return SDValue();
9006}
9007
Dan Gohmand858e902010-04-17 15:26:15 +00009008SDValue X86TargetLowering::LowerSETCC(SDValue Op, SelectionDAG &DAG) const {
Duncan Sands28b77e92011-09-06 19:07:46 +00009009
9010 if (Op.getValueType().isVector()) return LowerVSETCC(Op, DAG);
9011
Evan Cheng54de3ea2010-01-05 06:52:31 +00009012 assert(Op.getValueType() == MVT::i8 && "SetCC type must be 8-bit integer");
9013 SDValue Op0 = Op.getOperand(0);
9014 SDValue Op1 = Op.getOperand(1);
9015 DebugLoc dl = Op.getDebugLoc();
9016 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
9017
9018 // Optimize to BT if possible.
Evan Chengd40d03e2010-01-06 19:38:29 +00009019 // Lower (X & (1 << N)) == 0 to BT(X, N).
9020 // Lower ((X >>u N) & 1) != 0 to BT(X, N).
9021 // Lower ((X >>s N) & 1) != 0 to BT(X, N).
Andrew Trickf6c39412011-03-23 23:11:02 +00009022 if (Op0.getOpcode() == ISD::AND && Op0.hasOneUse() &&
Evan Chengd40d03e2010-01-06 19:38:29 +00009023 Op1.getOpcode() == ISD::Constant &&
Dan Gohmane368b462010-06-18 14:22:04 +00009024 cast<ConstantSDNode>(Op1)->isNullValue() &&
Evan Chengd40d03e2010-01-06 19:38:29 +00009025 (CC == ISD::SETEQ || CC == ISD::SETNE)) {
9026 SDValue NewSetCC = LowerToBT(Op0, CC, dl, DAG);
9027 if (NewSetCC.getNode())
9028 return NewSetCC;
9029 }
Evan Cheng54de3ea2010-01-05 06:52:31 +00009030
Chris Lattner481eebc2010-12-19 21:23:48 +00009031 // Look for X == 0, X == 1, X != 0, or X != 1. We can simplify some forms of
9032 // these.
9033 if (Op1.getOpcode() == ISD::Constant &&
Andrew Trickf6c39412011-03-23 23:11:02 +00009034 (cast<ConstantSDNode>(Op1)->getZExtValue() == 1 ||
Evan Cheng2c755ba2010-02-27 07:36:59 +00009035 cast<ConstantSDNode>(Op1)->isNullValue()) &&
9036 (CC == ISD::SETEQ || CC == ISD::SETNE)) {
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009037
Chris Lattner481eebc2010-12-19 21:23:48 +00009038 // If the input is a setcc, then reuse the input setcc or use a new one with
9039 // the inverted condition.
9040 if (Op0.getOpcode() == X86ISD::SETCC) {
9041 X86::CondCode CCode = (X86::CondCode)Op0.getConstantOperandVal(0);
9042 bool Invert = (CC == ISD::SETNE) ^
9043 cast<ConstantSDNode>(Op1)->isNullValue();
9044 if (!Invert) return Op0;
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009045
Evan Cheng2c755ba2010-02-27 07:36:59 +00009046 CCode = X86::GetOppositeBranchCondition(CCode);
Chris Lattner481eebc2010-12-19 21:23:48 +00009047 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
9048 DAG.getConstant(CCode, MVT::i8), Op0.getOperand(1));
9049 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00009050 }
9051
Evan Chenge5b51ac2010-04-17 06:13:15 +00009052 bool isFP = Op1.getValueType().isFloatingPoint();
Chris Lattnere55484e2008-12-25 05:34:37 +00009053 unsigned X86CC = TranslateX86CC(CC, isFP, Op0, Op1, DAG);
Dan Gohman1a492952009-10-20 16:22:37 +00009054 if (X86CC == X86::COND_INVALID)
9055 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00009056
Chris Lattnerc19d1c32010-12-19 22:08:31 +00009057 SDValue EFLAGS = EmitCmp(Op0, Op1, X86CC, DAG);
Benjamin Kramer17c836c2012-04-27 12:07:43 +00009058 EFLAGS = ConvertCmpIfNecessary(EFLAGS, DAG);
Owen Anderson825b72b2009-08-11 20:47:22 +00009059 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
Chris Lattnerc19d1c32010-12-19 22:08:31 +00009060 DAG.getConstant(X86CC, MVT::i8), EFLAGS);
Evan Cheng0488db92007-09-25 01:57:46 +00009061}
9062
Craig Topper89af15e2011-09-18 08:03:58 +00009063// Lower256IntVSETCC - Break a VSETCC 256-bit integer VSETCC into two new 128
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00009064// ones, and then concatenate the result back.
Craig Topper89af15e2011-09-18 08:03:58 +00009065static SDValue Lower256IntVSETCC(SDValue Op, SelectionDAG &DAG) {
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00009066 EVT VT = Op.getValueType();
9067
Craig Topper7a9a28b2012-08-12 02:23:29 +00009068 assert(VT.is256BitVector() && Op.getOpcode() == ISD::SETCC &&
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00009069 "Unsupported value type for operation");
9070
Craig Topper66ddd152012-04-27 22:54:43 +00009071 unsigned NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00009072 DebugLoc dl = Op.getDebugLoc();
9073 SDValue CC = Op.getOperand(2);
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00009074
9075 // Extract the LHS vectors
9076 SDValue LHS = Op.getOperand(0);
Craig Topperb14940a2012-04-22 20:55:18 +00009077 SDValue LHS1 = Extract128BitVector(LHS, 0, DAG, dl);
9078 SDValue LHS2 = Extract128BitVector(LHS, NumElems/2, DAG, dl);
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00009079
9080 // Extract the RHS vectors
9081 SDValue RHS = Op.getOperand(1);
Craig Topperb14940a2012-04-22 20:55:18 +00009082 SDValue RHS1 = Extract128BitVector(RHS, 0, DAG, dl);
9083 SDValue RHS2 = Extract128BitVector(RHS, NumElems/2, DAG, dl);
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00009084
9085 // Issue the operation on the smaller types and concatenate the result back
9086 MVT EltVT = VT.getVectorElementType().getSimpleVT();
9087 EVT NewVT = MVT::getVectorVT(EltVT, NumElems/2);
9088 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT,
9089 DAG.getNode(Op.getOpcode(), dl, NewVT, LHS1, RHS1, CC),
9090 DAG.getNode(Op.getOpcode(), dl, NewVT, LHS2, RHS2, CC));
9091}
9092
9093
Dan Gohmand858e902010-04-17 15:26:15 +00009094SDValue X86TargetLowering::LowerVSETCC(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +00009095 SDValue Cond;
9096 SDValue Op0 = Op.getOperand(0);
9097 SDValue Op1 = Op.getOperand(1);
9098 SDValue CC = Op.getOperand(2);
Owen Andersone50ed302009-08-10 22:56:29 +00009099 EVT VT = Op.getValueType();
Nate Begeman30a0de92008-07-17 16:51:19 +00009100 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
9101 bool isFP = Op.getOperand(1).getValueType().isFloatingPoint();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009102 DebugLoc dl = Op.getDebugLoc();
Nate Begeman30a0de92008-07-17 16:51:19 +00009103
9104 if (isFP) {
Craig Topper523908d2012-08-13 02:34:03 +00009105#ifndef NDEBUG
Bruno Cardoso Lopes0f0e0a02011-08-09 00:46:57 +00009106 EVT EltVT = Op0.getValueType().getVectorElementType();
Craig Topper523908d2012-08-13 02:34:03 +00009107 assert(EltVT == MVT::f32 || EltVT == MVT::f64);
9108#endif
Bruno Cardoso Lopes0f0e0a02011-08-09 00:46:57 +00009109
Craig Topper523908d2012-08-13 02:34:03 +00009110 unsigned SSECC;
Nate Begeman30a0de92008-07-17 16:51:19 +00009111 bool Swap = false;
9112
Bruno Cardoso Lopes8e03a822011-09-12 19:30:40 +00009113 // SSE Condition code mapping:
9114 // 0 - EQ
9115 // 1 - LT
9116 // 2 - LE
9117 // 3 - UNORD
9118 // 4 - NEQ
9119 // 5 - NLT
9120 // 6 - NLE
9121 // 7 - ORD
Nate Begeman30a0de92008-07-17 16:51:19 +00009122 switch (SetCCOpcode) {
Craig Topper2f1b2ec2012-08-13 03:42:38 +00009123 default: llvm_unreachable("Unexpected SETCC condition");
Nate Begemanfb8ead02008-07-25 19:05:58 +00009124 case ISD::SETOEQ:
Nate Begeman30a0de92008-07-17 16:51:19 +00009125 case ISD::SETEQ: SSECC = 0; break;
Bruno Cardoso Lopes8e03a822011-09-12 19:30:40 +00009126 case ISD::SETOGT:
9127 case ISD::SETGT: Swap = true; // Fallthrough
Bruno Cardoso Lopes457d53d2011-09-12 21:24:07 +00009128 case ISD::SETLT:
9129 case ISD::SETOLT: SSECC = 1; break;
9130 case ISD::SETOGE:
9131 case ISD::SETGE: Swap = true; // Fallthrough
Nate Begeman30a0de92008-07-17 16:51:19 +00009132 case ISD::SETLE:
9133 case ISD::SETOLE: SSECC = 2; break;
9134 case ISD::SETUO: SSECC = 3; break;
Nate Begemanfb8ead02008-07-25 19:05:58 +00009135 case ISD::SETUNE:
Nate Begeman30a0de92008-07-17 16:51:19 +00009136 case ISD::SETNE: SSECC = 4; break;
Craig Topper523908d2012-08-13 02:34:03 +00009137 case ISD::SETULE: Swap = true; // Fallthrough
Nate Begeman30a0de92008-07-17 16:51:19 +00009138 case ISD::SETUGE: SSECC = 5; break;
Craig Topper523908d2012-08-13 02:34:03 +00009139 case ISD::SETULT: Swap = true; // Fallthrough
Nate Begeman30a0de92008-07-17 16:51:19 +00009140 case ISD::SETUGT: SSECC = 6; break;
9141 case ISD::SETO: SSECC = 7; break;
Craig Topper523908d2012-08-13 02:34:03 +00009142 case ISD::SETUEQ:
9143 case ISD::SETONE: SSECC = 8; break;
Nate Begeman30a0de92008-07-17 16:51:19 +00009144 }
9145 if (Swap)
9146 std::swap(Op0, Op1);
9147
Nate Begemanfb8ead02008-07-25 19:05:58 +00009148 // In the two special cases we can't handle, emit two comparisons.
Nate Begeman30a0de92008-07-17 16:51:19 +00009149 if (SSECC == 8) {
Craig Topper523908d2012-08-13 02:34:03 +00009150 unsigned CC0, CC1;
9151 unsigned CombineOpc;
Nate Begemanfb8ead02008-07-25 19:05:58 +00009152 if (SetCCOpcode == ISD::SETUEQ) {
Craig Topper523908d2012-08-13 02:34:03 +00009153 CC0 = 3; CC1 = 0; CombineOpc = ISD::OR;
9154 } else {
9155 assert(SetCCOpcode == ISD::SETONE);
9156 CC0 = 7; CC1 = 4; CombineOpc = ISD::AND;
Craig Topper69947b92012-04-23 06:57:04 +00009157 }
Craig Topper523908d2012-08-13 02:34:03 +00009158
9159 SDValue Cmp0 = DAG.getNode(X86ISD::CMPP, dl, VT, Op0, Op1,
9160 DAG.getConstant(CC0, MVT::i8));
9161 SDValue Cmp1 = DAG.getNode(X86ISD::CMPP, dl, VT, Op0, Op1,
9162 DAG.getConstant(CC1, MVT::i8));
9163 return DAG.getNode(CombineOpc, dl, VT, Cmp0, Cmp1);
Nate Begeman30a0de92008-07-17 16:51:19 +00009164 }
9165 // Handle all other FP comparisons here.
Craig Topper1906d322012-01-22 23:36:02 +00009166 return DAG.getNode(X86ISD::CMPP, dl, VT, Op0, Op1,
9167 DAG.getConstant(SSECC, MVT::i8));
Nate Begeman30a0de92008-07-17 16:51:19 +00009168 }
Scott Michelfdc40a02009-02-17 22:15:04 +00009169
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00009170 // Break 256-bit integer vector compare into smaller ones.
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00009171 if (VT.is256BitVector() && !Subtarget->hasInt256())
Craig Topper89af15e2011-09-18 08:03:58 +00009172 return Lower256IntVSETCC(Op, DAG);
Bruno Cardoso Lopes0f0e0a02011-08-09 00:46:57 +00009173
Nate Begeman30a0de92008-07-17 16:51:19 +00009174 // We are handling one of the integer comparisons here. Since SSE only has
9175 // GT and EQ comparisons for integer, swapping operands and multiple
9176 // operations may be required for some comparisons.
Craig Topper2f1b2ec2012-08-13 03:42:38 +00009177 unsigned Opc;
Nate Begeman30a0de92008-07-17 16:51:19 +00009178 bool Swap = false, Invert = false, FlipSigns = false;
Scott Michelfdc40a02009-02-17 22:15:04 +00009179
Nate Begeman30a0de92008-07-17 16:51:19 +00009180 switch (SetCCOpcode) {
Craig Topper2f1b2ec2012-08-13 03:42:38 +00009181 default: llvm_unreachable("Unexpected SETCC condition");
Nate Begeman30a0de92008-07-17 16:51:19 +00009182 case ISD::SETNE: Invert = true;
Craig Topper67609fd2012-01-22 22:42:16 +00009183 case ISD::SETEQ: Opc = X86ISD::PCMPEQ; break;
Nate Begeman30a0de92008-07-17 16:51:19 +00009184 case ISD::SETLT: Swap = true;
Craig Topper67609fd2012-01-22 22:42:16 +00009185 case ISD::SETGT: Opc = X86ISD::PCMPGT; break;
Nate Begeman30a0de92008-07-17 16:51:19 +00009186 case ISD::SETGE: Swap = true;
Craig Topper67609fd2012-01-22 22:42:16 +00009187 case ISD::SETLE: Opc = X86ISD::PCMPGT; Invert = true; break;
Nate Begeman30a0de92008-07-17 16:51:19 +00009188 case ISD::SETULT: Swap = true;
Craig Topper67609fd2012-01-22 22:42:16 +00009189 case ISD::SETUGT: Opc = X86ISD::PCMPGT; FlipSigns = true; break;
Nate Begeman30a0de92008-07-17 16:51:19 +00009190 case ISD::SETUGE: Swap = true;
Craig Topper67609fd2012-01-22 22:42:16 +00009191 case ISD::SETULE: Opc = X86ISD::PCMPGT; FlipSigns = true; Invert = true; break;
Nate Begeman30a0de92008-07-17 16:51:19 +00009192 }
9193 if (Swap)
9194 std::swap(Op0, Op1);
Scott Michelfdc40a02009-02-17 22:15:04 +00009195
Eli Friedman7d3e2b72011-09-28 21:00:25 +00009196 // Check that the operation in question is available (most are plain SSE2,
9197 // but PCMPGTQ and PCMPEQQ have different requirements).
Craig Topper2f1b2ec2012-08-13 03:42:38 +00009198 if (VT == MVT::v2i64) {
9199 if (Opc == X86ISD::PCMPGT && !Subtarget->hasSSE42())
9200 return SDValue();
9201 if (Opc == X86ISD::PCMPEQ && !Subtarget->hasSSE41())
9202 return SDValue();
9203 }
Eli Friedman7d3e2b72011-09-28 21:00:25 +00009204
Nate Begeman30a0de92008-07-17 16:51:19 +00009205 // Since SSE has no unsigned integer comparisons, we need to flip the sign
9206 // bits of the inputs before performing those operations.
9207 if (FlipSigns) {
Owen Andersone50ed302009-08-10 22:56:29 +00009208 EVT EltVT = VT.getVectorElementType();
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00009209 SDValue SignBit = DAG.getConstant(APInt::getSignBit(EltVT.getSizeInBits()),
9210 EltVT);
Dan Gohman475871a2008-07-27 21:46:04 +00009211 std::vector<SDValue> SignBits(VT.getVectorNumElements(), SignBit);
Evan Chenga87008d2009-02-25 22:49:59 +00009212 SDValue SignVec = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &SignBits[0],
9213 SignBits.size());
Dale Johannesenace16102009-02-03 19:33:06 +00009214 Op0 = DAG.getNode(ISD::XOR, dl, VT, Op0, SignVec);
9215 Op1 = DAG.getNode(ISD::XOR, dl, VT, Op1, SignVec);
Nate Begeman30a0de92008-07-17 16:51:19 +00009216 }
Scott Michelfdc40a02009-02-17 22:15:04 +00009217
Dale Johannesenace16102009-02-03 19:33:06 +00009218 SDValue Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
Nate Begeman30a0de92008-07-17 16:51:19 +00009219
9220 // If the logical-not of the result is required, perform that now.
Bob Wilson4c245462009-01-22 17:39:32 +00009221 if (Invert)
Dale Johannesenace16102009-02-03 19:33:06 +00009222 Result = DAG.getNOT(dl, Result, VT);
Bob Wilson4c245462009-01-22 17:39:32 +00009223
Nate Begeman30a0de92008-07-17 16:51:19 +00009224 return Result;
9225}
Evan Cheng0488db92007-09-25 01:57:46 +00009226
Evan Cheng370e5342008-12-03 08:38:43 +00009227// isX86LogicalCmp - Return true if opcode is a X86 logical comparison.
Dan Gohman076aee32009-03-04 19:44:21 +00009228static bool isX86LogicalCmp(SDValue Op) {
9229 unsigned Opc = Op.getNode()->getOpcode();
Benjamin Kramer17c836c2012-04-27 12:07:43 +00009230 if (Opc == X86ISD::CMP || Opc == X86ISD::COMI || Opc == X86ISD::UCOMI ||
9231 Opc == X86ISD::SAHF)
Dan Gohman076aee32009-03-04 19:44:21 +00009232 return true;
9233 if (Op.getResNo() == 1 &&
9234 (Opc == X86ISD::ADD ||
9235 Opc == X86ISD::SUB ||
Chris Lattner5b856542010-12-20 00:59:46 +00009236 Opc == X86ISD::ADC ||
9237 Opc == X86ISD::SBB ||
Dan Gohman076aee32009-03-04 19:44:21 +00009238 Opc == X86ISD::SMUL ||
9239 Opc == X86ISD::UMUL ||
9240 Opc == X86ISD::INC ||
Dan Gohmane220c4b2009-09-18 19:59:53 +00009241 Opc == X86ISD::DEC ||
9242 Opc == X86ISD::OR ||
9243 Opc == X86ISD::XOR ||
9244 Opc == X86ISD::AND))
Dan Gohman076aee32009-03-04 19:44:21 +00009245 return true;
9246
Chris Lattner9637d5b2010-12-05 07:49:54 +00009247 if (Op.getResNo() == 2 && Opc == X86ISD::UMUL)
9248 return true;
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009249
Dan Gohman076aee32009-03-04 19:44:21 +00009250 return false;
Evan Cheng370e5342008-12-03 08:38:43 +00009251}
9252
Chris Lattnera2b56002010-12-05 01:23:24 +00009253static bool isZero(SDValue V) {
9254 ConstantSDNode *C = dyn_cast<ConstantSDNode>(V);
9255 return C && C->isNullValue();
9256}
9257
Evan Chengb64dd5f2012-08-07 22:21:00 +00009258static bool isTruncWithZeroHighBitsInput(SDValue V, SelectionDAG &DAG) {
9259 if (V.getOpcode() != ISD::TRUNCATE)
9260 return false;
9261
9262 SDValue VOp0 = V.getOperand(0);
9263 unsigned InBits = VOp0.getValueSizeInBits();
9264 unsigned Bits = V.getValueSizeInBits();
9265 return DAG.MaskedValueIsZero(VOp0, APInt::getHighBitsSet(InBits,InBits-Bits));
9266}
9267
Dan Gohmand858e902010-04-17 15:26:15 +00009268SDValue X86TargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng734503b2006-09-11 02:19:56 +00009269 bool addTest = true;
Dan Gohman475871a2008-07-27 21:46:04 +00009270 SDValue Cond = Op.getOperand(0);
Chris Lattnera2b56002010-12-05 01:23:24 +00009271 SDValue Op1 = Op.getOperand(1);
9272 SDValue Op2 = Op.getOperand(2);
9273 DebugLoc DL = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00009274 SDValue CC;
Evan Cheng9bba8942006-01-26 02:13:10 +00009275
Dan Gohman1a492952009-10-20 16:22:37 +00009276 if (Cond.getOpcode() == ISD::SETCC) {
9277 SDValue NewCond = LowerSETCC(Cond, DAG);
9278 if (NewCond.getNode())
9279 Cond = NewCond;
9280 }
Evan Cheng734503b2006-09-11 02:19:56 +00009281
Chris Lattnera2b56002010-12-05 01:23:24 +00009282 // (select (x == 0), -1, y) -> (sign_bit (x - 1)) | y
Chris Lattner96908b12010-12-05 02:00:51 +00009283 // (select (x == 0), y, -1) -> ~(sign_bit (x - 1)) | y
Chris Lattnera2b56002010-12-05 01:23:24 +00009284 // (select (x != 0), y, -1) -> (sign_bit (x - 1)) | y
Chris Lattner96908b12010-12-05 02:00:51 +00009285 // (select (x != 0), -1, y) -> ~(sign_bit (x - 1)) | y
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00009286 if (Cond.getOpcode() == X86ISD::SETCC &&
Chris Lattner96908b12010-12-05 02:00:51 +00009287 Cond.getOperand(1).getOpcode() == X86ISD::CMP &&
9288 isZero(Cond.getOperand(1).getOperand(1))) {
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00009289 SDValue Cmp = Cond.getOperand(1);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009290
Chris Lattnera2b56002010-12-05 01:23:24 +00009291 unsigned CondCode =cast<ConstantSDNode>(Cond.getOperand(0))->getZExtValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009292
9293 if ((isAllOnes(Op1) || isAllOnes(Op2)) &&
Chris Lattner96908b12010-12-05 02:00:51 +00009294 (CondCode == X86::COND_E || CondCode == X86::COND_NE)) {
9295 SDValue Y = isAllOnes(Op2) ? Op1 : Op2;
Chris Lattnera2b56002010-12-05 01:23:24 +00009296
9297 SDValue CmpOp0 = Cmp.getOperand(0);
Manman Rened579842012-05-07 18:06:23 +00009298 // Apply further optimizations for special cases
9299 // (select (x != 0), -1, 0) -> neg & sbb
9300 // (select (x == 0), 0, -1) -> neg & sbb
9301 if (ConstantSDNode *YC = dyn_cast<ConstantSDNode>(Y))
Chad Rosiera20e1e72012-08-01 18:39:17 +00009302 if (YC->isNullValue() &&
Manman Rened579842012-05-07 18:06:23 +00009303 (isAllOnes(Op1) == (CondCode == X86::COND_NE))) {
9304 SDVTList VTs = DAG.getVTList(CmpOp0.getValueType(), MVT::i32);
Chad Rosiera20e1e72012-08-01 18:39:17 +00009305 SDValue Neg = DAG.getNode(X86ISD::SUB, DL, VTs,
9306 DAG.getConstant(0, CmpOp0.getValueType()),
Manman Rened579842012-05-07 18:06:23 +00009307 CmpOp0);
9308 SDValue Res = DAG.getNode(X86ISD::SETCC_CARRY, DL, Op.getValueType(),
9309 DAG.getConstant(X86::COND_B, MVT::i8),
9310 SDValue(Neg.getNode(), 1));
9311 return Res;
9312 }
9313
Chris Lattnera2b56002010-12-05 01:23:24 +00009314 Cmp = DAG.getNode(X86ISD::CMP, DL, MVT::i32,
9315 CmpOp0, DAG.getConstant(1, CmpOp0.getValueType()));
Benjamin Kramer17c836c2012-04-27 12:07:43 +00009316 Cmp = ConvertCmpIfNecessary(Cmp, DAG);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009317
Chris Lattner96908b12010-12-05 02:00:51 +00009318 SDValue Res = // Res = 0 or -1.
Chris Lattnera2b56002010-12-05 01:23:24 +00009319 DAG.getNode(X86ISD::SETCC_CARRY, DL, Op.getValueType(),
9320 DAG.getConstant(X86::COND_B, MVT::i8), Cmp);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009321
Chris Lattner96908b12010-12-05 02:00:51 +00009322 if (isAllOnes(Op1) != (CondCode == X86::COND_E))
9323 Res = DAG.getNOT(DL, Res, Res.getValueType());
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009324
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00009325 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(Op2);
Chris Lattnera2b56002010-12-05 01:23:24 +00009326 if (N2C == 0 || !N2C->isNullValue())
9327 Res = DAG.getNode(ISD::OR, DL, Res.getValueType(), Res, Y);
9328 return Res;
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00009329 }
9330 }
9331
Chris Lattnera2b56002010-12-05 01:23:24 +00009332 // Look past (and (setcc_carry (cmp ...)), 1).
Evan Chengad9c0a32009-12-15 00:53:42 +00009333 if (Cond.getOpcode() == ISD::AND &&
9334 Cond.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY) {
9335 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Cond.getOperand(1));
Michael J. Spencerec38de22010-10-10 22:04:20 +00009336 if (C && C->getAPIntValue() == 1)
Evan Chengad9c0a32009-12-15 00:53:42 +00009337 Cond = Cond.getOperand(0);
9338 }
9339
Evan Cheng3f41d662007-10-08 22:16:29 +00009340 // If condition flag is set by a X86ISD::CMP, then use it as the condition
9341 // setting operand in place of the X86ISD::SETCC.
Dan Gohman65fd6562011-11-03 21:49:52 +00009342 unsigned CondOpcode = Cond.getOpcode();
9343 if (CondOpcode == X86ISD::SETCC ||
9344 CondOpcode == X86ISD::SETCC_CARRY) {
Evan Cheng734503b2006-09-11 02:19:56 +00009345 CC = Cond.getOperand(0);
9346
Dan Gohman475871a2008-07-27 21:46:04 +00009347 SDValue Cmp = Cond.getOperand(1);
Evan Cheng734503b2006-09-11 02:19:56 +00009348 unsigned Opc = Cmp.getOpcode();
Owen Andersone50ed302009-08-10 22:56:29 +00009349 EVT VT = Op.getValueType();
Scott Michelfdc40a02009-02-17 22:15:04 +00009350
Evan Cheng3f41d662007-10-08 22:16:29 +00009351 bool IllegalFPCMov = false;
Duncan Sands83ec4b62008-06-06 12:08:01 +00009352 if (VT.isFloatingPoint() && !VT.isVector() &&
Chris Lattner78631162008-01-16 06:24:21 +00009353 !isScalarFPTypeInSSEReg(VT)) // FPStack?
Dan Gohman7810bfe2008-09-26 21:54:37 +00009354 IllegalFPCMov = !hasFPCMov(cast<ConstantSDNode>(CC)->getSExtValue());
Scott Michelfdc40a02009-02-17 22:15:04 +00009355
Chris Lattnerd1980a52009-03-12 06:52:53 +00009356 if ((isX86LogicalCmp(Cmp) && !IllegalFPCMov) ||
9357 Opc == X86ISD::BT) { // FIXME
Evan Cheng3f41d662007-10-08 22:16:29 +00009358 Cond = Cmp;
Evan Cheng0488db92007-09-25 01:57:46 +00009359 addTest = false;
9360 }
Dan Gohman65fd6562011-11-03 21:49:52 +00009361 } else if (CondOpcode == ISD::USUBO || CondOpcode == ISD::SSUBO ||
9362 CondOpcode == ISD::UADDO || CondOpcode == ISD::SADDO ||
9363 ((CondOpcode == ISD::UMULO || CondOpcode == ISD::SMULO) &&
9364 Cond.getOperand(0).getValueType() != MVT::i8)) {
9365 SDValue LHS = Cond.getOperand(0);
9366 SDValue RHS = Cond.getOperand(1);
9367 unsigned X86Opcode;
9368 unsigned X86Cond;
9369 SDVTList VTs;
9370 switch (CondOpcode) {
9371 case ISD::UADDO: X86Opcode = X86ISD::ADD; X86Cond = X86::COND_B; break;
9372 case ISD::SADDO: X86Opcode = X86ISD::ADD; X86Cond = X86::COND_O; break;
9373 case ISD::USUBO: X86Opcode = X86ISD::SUB; X86Cond = X86::COND_B; break;
9374 case ISD::SSUBO: X86Opcode = X86ISD::SUB; X86Cond = X86::COND_O; break;
9375 case ISD::UMULO: X86Opcode = X86ISD::UMUL; X86Cond = X86::COND_O; break;
9376 case ISD::SMULO: X86Opcode = X86ISD::SMUL; X86Cond = X86::COND_O; break;
9377 default: llvm_unreachable("unexpected overflowing operator");
9378 }
9379 if (CondOpcode == ISD::UMULO)
9380 VTs = DAG.getVTList(LHS.getValueType(), LHS.getValueType(),
9381 MVT::i32);
9382 else
9383 VTs = DAG.getVTList(LHS.getValueType(), MVT::i32);
9384
9385 SDValue X86Op = DAG.getNode(X86Opcode, DL, VTs, LHS, RHS);
9386
9387 if (CondOpcode == ISD::UMULO)
9388 Cond = X86Op.getValue(2);
9389 else
9390 Cond = X86Op.getValue(1);
9391
9392 CC = DAG.getConstant(X86Cond, MVT::i8);
9393 addTest = false;
Evan Cheng0488db92007-09-25 01:57:46 +00009394 }
9395
9396 if (addTest) {
Evan Chengb64dd5f2012-08-07 22:21:00 +00009397 // Look pass the truncate if the high bits are known zero.
9398 if (isTruncWithZeroHighBitsInput(Cond, DAG))
9399 Cond = Cond.getOperand(0);
Evan Chengd40d03e2010-01-06 19:38:29 +00009400
9401 // We know the result of AND is compared against zero. Try to match
9402 // it to BT.
Michael J. Spencerec38de22010-10-10 22:04:20 +00009403 if (Cond.getOpcode() == ISD::AND && Cond.hasOneUse()) {
Chris Lattnera2b56002010-12-05 01:23:24 +00009404 SDValue NewSetCC = LowerToBT(Cond, ISD::SETNE, DL, DAG);
Evan Chengd40d03e2010-01-06 19:38:29 +00009405 if (NewSetCC.getNode()) {
9406 CC = NewSetCC.getOperand(0);
9407 Cond = NewSetCC.getOperand(1);
9408 addTest = false;
9409 }
9410 }
9411 }
9412
9413 if (addTest) {
Owen Anderson825b72b2009-08-11 20:47:22 +00009414 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng552f09a2010-04-26 19:06:11 +00009415 Cond = EmitTest(Cond, X86::COND_NE, DAG);
Evan Cheng0488db92007-09-25 01:57:46 +00009416 }
9417
Benjamin Kramere915ff32010-12-22 23:09:28 +00009418 // a < b ? -1 : 0 -> RES = ~setcc_carry
9419 // a < b ? 0 : -1 -> RES = setcc_carry
9420 // a >= b ? -1 : 0 -> RES = setcc_carry
9421 // a >= b ? 0 : -1 -> RES = ~setcc_carry
Manman Ren39ad5682012-08-08 00:51:41 +00009422 if (Cond.getOpcode() == X86ISD::SUB) {
Benjamin Kramer17c836c2012-04-27 12:07:43 +00009423 Cond = ConvertCmpIfNecessary(Cond, DAG);
Benjamin Kramere915ff32010-12-22 23:09:28 +00009424 unsigned CondCode = cast<ConstantSDNode>(CC)->getZExtValue();
9425
9426 if ((CondCode == X86::COND_AE || CondCode == X86::COND_B) &&
9427 (isAllOnes(Op1) || isAllOnes(Op2)) && (isZero(Op1) || isZero(Op2))) {
9428 SDValue Res = DAG.getNode(X86ISD::SETCC_CARRY, DL, Op.getValueType(),
9429 DAG.getConstant(X86::COND_B, MVT::i8), Cond);
9430 if (isAllOnes(Op1) != (CondCode == X86::COND_B))
9431 return DAG.getNOT(DL, Res, Res.getValueType());
9432 return Res;
9433 }
9434 }
9435
Benjamin Kramer444dcce2012-10-13 10:39:49 +00009436 // X86 doesn't have an i8 cmov. If both operands are the result of a truncate
9437 // widen the cmov and push the truncate through. This avoids introducing a new
9438 // branch during isel and doesn't add any extensions.
9439 if (Op.getValueType() == MVT::i8 &&
9440 Op1.getOpcode() == ISD::TRUNCATE && Op2.getOpcode() == ISD::TRUNCATE) {
9441 SDValue T1 = Op1.getOperand(0), T2 = Op2.getOperand(0);
9442 if (T1.getValueType() == T2.getValueType() &&
9443 // Blacklist CopyFromReg to avoid partial register stalls.
9444 T1.getOpcode() != ISD::CopyFromReg && T2.getOpcode()!=ISD::CopyFromReg){
9445 SDVTList VTs = DAG.getVTList(T1.getValueType(), MVT::Glue);
Benjamin Kramerf8b65aa2012-10-13 12:50:19 +00009446 SDValue Cmov = DAG.getNode(X86ISD::CMOV, DL, VTs, T2, T1, CC, Cond);
Benjamin Kramer444dcce2012-10-13 10:39:49 +00009447 return DAG.getNode(ISD::TRUNCATE, DL, Op.getValueType(), Cmov);
9448 }
9449 }
9450
Evan Cheng0488db92007-09-25 01:57:46 +00009451 // X86ISD::CMOV means set the result (which is operand 1) to the RHS if
9452 // condition is true.
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00009453 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::Glue);
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00009454 SDValue Ops[] = { Op2, Op1, CC, Cond };
Chris Lattnera2b56002010-12-05 01:23:24 +00009455 return DAG.getNode(X86ISD::CMOV, DL, VTs, Ops, array_lengthof(Ops));
Evan Cheng0488db92007-09-25 01:57:46 +00009456}
9457
Evan Cheng370e5342008-12-03 08:38:43 +00009458// isAndOrOfSingleUseSetCCs - Return true if node is an ISD::AND or
9459// ISD::OR of two X86ISD::SETCC nodes each of which has no other use apart
9460// from the AND / OR.
9461static bool isAndOrOfSetCCs(SDValue Op, unsigned &Opc) {
9462 Opc = Op.getOpcode();
9463 if (Opc != ISD::OR && Opc != ISD::AND)
9464 return false;
9465 return (Op.getOperand(0).getOpcode() == X86ISD::SETCC &&
9466 Op.getOperand(0).hasOneUse() &&
9467 Op.getOperand(1).getOpcode() == X86ISD::SETCC &&
9468 Op.getOperand(1).hasOneUse());
9469}
9470
Evan Cheng961d6d42009-02-02 08:19:07 +00009471// isXor1OfSetCC - Return true if node is an ISD::XOR of a X86ISD::SETCC and
9472// 1 and that the SETCC node has a single use.
Evan Cheng67ad9db2009-02-02 08:07:36 +00009473static bool isXor1OfSetCC(SDValue Op) {
9474 if (Op.getOpcode() != ISD::XOR)
9475 return false;
9476 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
9477 if (N1C && N1C->getAPIntValue() == 1) {
9478 return Op.getOperand(0).getOpcode() == X86ISD::SETCC &&
9479 Op.getOperand(0).hasOneUse();
9480 }
9481 return false;
9482}
9483
Dan Gohmand858e902010-04-17 15:26:15 +00009484SDValue X86TargetLowering::LowerBRCOND(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng734503b2006-09-11 02:19:56 +00009485 bool addTest = true;
Dan Gohman475871a2008-07-27 21:46:04 +00009486 SDValue Chain = Op.getOperand(0);
9487 SDValue Cond = Op.getOperand(1);
9488 SDValue Dest = Op.getOperand(2);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009489 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00009490 SDValue CC;
Dan Gohman65fd6562011-11-03 21:49:52 +00009491 bool Inverted = false;
Evan Cheng734503b2006-09-11 02:19:56 +00009492
Dan Gohman1a492952009-10-20 16:22:37 +00009493 if (Cond.getOpcode() == ISD::SETCC) {
Dan Gohman65fd6562011-11-03 21:49:52 +00009494 // Check for setcc([su]{add,sub,mul}o == 0).
9495 if (cast<CondCodeSDNode>(Cond.getOperand(2))->get() == ISD::SETEQ &&
9496 isa<ConstantSDNode>(Cond.getOperand(1)) &&
9497 cast<ConstantSDNode>(Cond.getOperand(1))->isNullValue() &&
9498 Cond.getOperand(0).getResNo() == 1 &&
9499 (Cond.getOperand(0).getOpcode() == ISD::SADDO ||
9500 Cond.getOperand(0).getOpcode() == ISD::UADDO ||
9501 Cond.getOperand(0).getOpcode() == ISD::SSUBO ||
9502 Cond.getOperand(0).getOpcode() == ISD::USUBO ||
9503 Cond.getOperand(0).getOpcode() == ISD::SMULO ||
9504 Cond.getOperand(0).getOpcode() == ISD::UMULO)) {
9505 Inverted = true;
9506 Cond = Cond.getOperand(0);
9507 } else {
9508 SDValue NewCond = LowerSETCC(Cond, DAG);
9509 if (NewCond.getNode())
9510 Cond = NewCond;
9511 }
Dan Gohman1a492952009-10-20 16:22:37 +00009512 }
Chris Lattnere55484e2008-12-25 05:34:37 +00009513#if 0
9514 // FIXME: LowerXALUO doesn't handle these!!
Bill Wendlingd350e022008-12-12 21:15:41 +00009515 else if (Cond.getOpcode() == X86ISD::ADD ||
9516 Cond.getOpcode() == X86ISD::SUB ||
9517 Cond.getOpcode() == X86ISD::SMUL ||
9518 Cond.getOpcode() == X86ISD::UMUL)
Bill Wendling74c37652008-12-09 22:08:41 +00009519 Cond = LowerXALUO(Cond, DAG);
Chris Lattnere55484e2008-12-25 05:34:37 +00009520#endif
Scott Michelfdc40a02009-02-17 22:15:04 +00009521
Evan Chengad9c0a32009-12-15 00:53:42 +00009522 // Look pass (and (setcc_carry (cmp ...)), 1).
9523 if (Cond.getOpcode() == ISD::AND &&
9524 Cond.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY) {
9525 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Cond.getOperand(1));
Michael J. Spencerec38de22010-10-10 22:04:20 +00009526 if (C && C->getAPIntValue() == 1)
Evan Chengad9c0a32009-12-15 00:53:42 +00009527 Cond = Cond.getOperand(0);
9528 }
9529
Evan Cheng3f41d662007-10-08 22:16:29 +00009530 // If condition flag is set by a X86ISD::CMP, then use it as the condition
9531 // setting operand in place of the X86ISD::SETCC.
Dan Gohman65fd6562011-11-03 21:49:52 +00009532 unsigned CondOpcode = Cond.getOpcode();
9533 if (CondOpcode == X86ISD::SETCC ||
9534 CondOpcode == X86ISD::SETCC_CARRY) {
Evan Cheng734503b2006-09-11 02:19:56 +00009535 CC = Cond.getOperand(0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00009536
Dan Gohman475871a2008-07-27 21:46:04 +00009537 SDValue Cmp = Cond.getOperand(1);
Evan Cheng734503b2006-09-11 02:19:56 +00009538 unsigned Opc = Cmp.getOpcode();
Chris Lattnere55484e2008-12-25 05:34:37 +00009539 // FIXME: WHY THE SPECIAL CASING OF LogicalCmp??
Dan Gohman076aee32009-03-04 19:44:21 +00009540 if (isX86LogicalCmp(Cmp) || Opc == X86ISD::BT) {
Evan Cheng3f41d662007-10-08 22:16:29 +00009541 Cond = Cmp;
Evan Cheng0488db92007-09-25 01:57:46 +00009542 addTest = false;
Bill Wendling61edeb52008-12-02 01:06:39 +00009543 } else {
Evan Cheng370e5342008-12-03 08:38:43 +00009544 switch (cast<ConstantSDNode>(CC)->getZExtValue()) {
Bill Wendling0ea25cb2008-12-03 08:32:02 +00009545 default: break;
9546 case X86::COND_O:
Dan Gohman653456c2009-01-07 00:15:08 +00009547 case X86::COND_B:
Chris Lattnere55484e2008-12-25 05:34:37 +00009548 // These can only come from an arithmetic instruction with overflow,
9549 // e.g. SADDO, UADDO.
Bill Wendling0ea25cb2008-12-03 08:32:02 +00009550 Cond = Cond.getNode()->getOperand(1);
9551 addTest = false;
9552 break;
Bill Wendling61edeb52008-12-02 01:06:39 +00009553 }
Evan Cheng0488db92007-09-25 01:57:46 +00009554 }
Dan Gohman65fd6562011-11-03 21:49:52 +00009555 }
9556 CondOpcode = Cond.getOpcode();
9557 if (CondOpcode == ISD::UADDO || CondOpcode == ISD::SADDO ||
9558 CondOpcode == ISD::USUBO || CondOpcode == ISD::SSUBO ||
9559 ((CondOpcode == ISD::UMULO || CondOpcode == ISD::SMULO) &&
9560 Cond.getOperand(0).getValueType() != MVT::i8)) {
9561 SDValue LHS = Cond.getOperand(0);
9562 SDValue RHS = Cond.getOperand(1);
9563 unsigned X86Opcode;
9564 unsigned X86Cond;
9565 SDVTList VTs;
9566 switch (CondOpcode) {
9567 case ISD::UADDO: X86Opcode = X86ISD::ADD; X86Cond = X86::COND_B; break;
9568 case ISD::SADDO: X86Opcode = X86ISD::ADD; X86Cond = X86::COND_O; break;
9569 case ISD::USUBO: X86Opcode = X86ISD::SUB; X86Cond = X86::COND_B; break;
9570 case ISD::SSUBO: X86Opcode = X86ISD::SUB; X86Cond = X86::COND_O; break;
9571 case ISD::UMULO: X86Opcode = X86ISD::UMUL; X86Cond = X86::COND_O; break;
9572 case ISD::SMULO: X86Opcode = X86ISD::SMUL; X86Cond = X86::COND_O; break;
9573 default: llvm_unreachable("unexpected overflowing operator");
9574 }
9575 if (Inverted)
9576 X86Cond = X86::GetOppositeBranchCondition((X86::CondCode)X86Cond);
9577 if (CondOpcode == ISD::UMULO)
9578 VTs = DAG.getVTList(LHS.getValueType(), LHS.getValueType(),
9579 MVT::i32);
9580 else
9581 VTs = DAG.getVTList(LHS.getValueType(), MVT::i32);
9582
9583 SDValue X86Op = DAG.getNode(X86Opcode, dl, VTs, LHS, RHS);
9584
9585 if (CondOpcode == ISD::UMULO)
9586 Cond = X86Op.getValue(2);
9587 else
9588 Cond = X86Op.getValue(1);
9589
9590 CC = DAG.getConstant(X86Cond, MVT::i8);
9591 addTest = false;
Evan Cheng370e5342008-12-03 08:38:43 +00009592 } else {
9593 unsigned CondOpc;
9594 if (Cond.hasOneUse() && isAndOrOfSetCCs(Cond, CondOpc)) {
9595 SDValue Cmp = Cond.getOperand(0).getOperand(1);
Evan Cheng370e5342008-12-03 08:38:43 +00009596 if (CondOpc == ISD::OR) {
9597 // Also, recognize the pattern generated by an FCMP_UNE. We can emit
9598 // two branches instead of an explicit OR instruction with a
9599 // separate test.
9600 if (Cmp == Cond.getOperand(1).getOperand(1) &&
Dan Gohman076aee32009-03-04 19:44:21 +00009601 isX86LogicalCmp(Cmp)) {
Evan Cheng370e5342008-12-03 08:38:43 +00009602 CC = Cond.getOperand(0).getOperand(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009603 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Evan Cheng370e5342008-12-03 08:38:43 +00009604 Chain, Dest, CC, Cmp);
9605 CC = Cond.getOperand(1).getOperand(0);
9606 Cond = Cmp;
9607 addTest = false;
9608 }
9609 } else { // ISD::AND
9610 // Also, recognize the pattern generated by an FCMP_OEQ. We can emit
9611 // two branches instead of an explicit AND instruction with a
9612 // separate test. However, we only do this if this block doesn't
9613 // have a fall-through edge, because this requires an explicit
9614 // jmp when the condition is false.
9615 if (Cmp == Cond.getOperand(1).getOperand(1) &&
Dan Gohman076aee32009-03-04 19:44:21 +00009616 isX86LogicalCmp(Cmp) &&
Evan Cheng370e5342008-12-03 08:38:43 +00009617 Op.getNode()->hasOneUse()) {
9618 X86::CondCode CCode =
9619 (X86::CondCode)Cond.getOperand(0).getConstantOperandVal(0);
9620 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00009621 CC = DAG.getConstant(CCode, MVT::i8);
Dan Gohman027657d2010-06-18 15:30:29 +00009622 SDNode *User = *Op.getNode()->use_begin();
Evan Cheng370e5342008-12-03 08:38:43 +00009623 // Look for an unconditional branch following this conditional branch.
9624 // We need this because we need to reverse the successors in order
9625 // to implement FCMP_OEQ.
Dan Gohman027657d2010-06-18 15:30:29 +00009626 if (User->getOpcode() == ISD::BR) {
9627 SDValue FalseBB = User->getOperand(1);
9628 SDNode *NewBR =
9629 DAG.UpdateNodeOperands(User, User->getOperand(0), Dest);
Evan Cheng370e5342008-12-03 08:38:43 +00009630 assert(NewBR == User);
Nick Lewycky2a3ee5e2010-06-20 20:27:42 +00009631 (void)NewBR;
Evan Cheng370e5342008-12-03 08:38:43 +00009632 Dest = FalseBB;
Dan Gohman279c22e2008-10-21 03:29:32 +00009633
Dale Johannesene4d209d2009-02-03 20:21:25 +00009634 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Evan Cheng370e5342008-12-03 08:38:43 +00009635 Chain, Dest, CC, Cmp);
9636 X86::CondCode CCode =
9637 (X86::CondCode)Cond.getOperand(1).getConstantOperandVal(0);
9638 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00009639 CC = DAG.getConstant(CCode, MVT::i8);
Evan Cheng370e5342008-12-03 08:38:43 +00009640 Cond = Cmp;
9641 addTest = false;
9642 }
9643 }
Dan Gohman279c22e2008-10-21 03:29:32 +00009644 }
Evan Cheng67ad9db2009-02-02 08:07:36 +00009645 } else if (Cond.hasOneUse() && isXor1OfSetCC(Cond)) {
9646 // Recognize for xorb (setcc), 1 patterns. The xor inverts the condition.
9647 // It should be transformed during dag combiner except when the condition
9648 // is set by a arithmetics with overflow node.
9649 X86::CondCode CCode =
9650 (X86::CondCode)Cond.getOperand(0).getConstantOperandVal(0);
9651 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00009652 CC = DAG.getConstant(CCode, MVT::i8);
Evan Cheng67ad9db2009-02-02 08:07:36 +00009653 Cond = Cond.getOperand(0).getOperand(1);
9654 addTest = false;
Dan Gohman65fd6562011-11-03 21:49:52 +00009655 } else if (Cond.getOpcode() == ISD::SETCC &&
9656 cast<CondCodeSDNode>(Cond.getOperand(2))->get() == ISD::SETOEQ) {
9657 // For FCMP_OEQ, we can emit
9658 // two branches instead of an explicit AND instruction with a
9659 // separate test. However, we only do this if this block doesn't
9660 // have a fall-through edge, because this requires an explicit
9661 // jmp when the condition is false.
9662 if (Op.getNode()->hasOneUse()) {
9663 SDNode *User = *Op.getNode()->use_begin();
9664 // Look for an unconditional branch following this conditional branch.
9665 // We need this because we need to reverse the successors in order
9666 // to implement FCMP_OEQ.
9667 if (User->getOpcode() == ISD::BR) {
9668 SDValue FalseBB = User->getOperand(1);
9669 SDNode *NewBR =
9670 DAG.UpdateNodeOperands(User, User->getOperand(0), Dest);
9671 assert(NewBR == User);
9672 (void)NewBR;
9673 Dest = FalseBB;
9674
9675 SDValue Cmp = DAG.getNode(X86ISD::CMP, dl, MVT::i32,
9676 Cond.getOperand(0), Cond.getOperand(1));
Benjamin Kramer17c836c2012-04-27 12:07:43 +00009677 Cmp = ConvertCmpIfNecessary(Cmp, DAG);
Dan Gohman65fd6562011-11-03 21:49:52 +00009678 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
9679 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
9680 Chain, Dest, CC, Cmp);
9681 CC = DAG.getConstant(X86::COND_P, MVT::i8);
9682 Cond = Cmp;
9683 addTest = false;
9684 }
9685 }
9686 } else if (Cond.getOpcode() == ISD::SETCC &&
9687 cast<CondCodeSDNode>(Cond.getOperand(2))->get() == ISD::SETUNE) {
9688 // For FCMP_UNE, we can emit
9689 // two branches instead of an explicit AND instruction with a
9690 // separate test. However, we only do this if this block doesn't
9691 // have a fall-through edge, because this requires an explicit
9692 // jmp when the condition is false.
9693 if (Op.getNode()->hasOneUse()) {
9694 SDNode *User = *Op.getNode()->use_begin();
9695 // Look for an unconditional branch following this conditional branch.
9696 // We need this because we need to reverse the successors in order
9697 // to implement FCMP_UNE.
9698 if (User->getOpcode() == ISD::BR) {
9699 SDValue FalseBB = User->getOperand(1);
9700 SDNode *NewBR =
9701 DAG.UpdateNodeOperands(User, User->getOperand(0), Dest);
9702 assert(NewBR == User);
9703 (void)NewBR;
9704
9705 SDValue Cmp = DAG.getNode(X86ISD::CMP, dl, MVT::i32,
9706 Cond.getOperand(0), Cond.getOperand(1));
Benjamin Kramer17c836c2012-04-27 12:07:43 +00009707 Cmp = ConvertCmpIfNecessary(Cmp, DAG);
Dan Gohman65fd6562011-11-03 21:49:52 +00009708 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
9709 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
9710 Chain, Dest, CC, Cmp);
9711 CC = DAG.getConstant(X86::COND_NP, MVT::i8);
9712 Cond = Cmp;
9713 addTest = false;
9714 Dest = FalseBB;
9715 }
9716 }
Dan Gohman279c22e2008-10-21 03:29:32 +00009717 }
Evan Cheng0488db92007-09-25 01:57:46 +00009718 }
9719
9720 if (addTest) {
Evan Chengb64dd5f2012-08-07 22:21:00 +00009721 // Look pass the truncate if the high bits are known zero.
9722 if (isTruncWithZeroHighBitsInput(Cond, DAG))
9723 Cond = Cond.getOperand(0);
Evan Chengd40d03e2010-01-06 19:38:29 +00009724
9725 // We know the result of AND is compared against zero. Try to match
9726 // it to BT.
Michael J. Spencerec38de22010-10-10 22:04:20 +00009727 if (Cond.getOpcode() == ISD::AND && Cond.hasOneUse()) {
Evan Chengd40d03e2010-01-06 19:38:29 +00009728 SDValue NewSetCC = LowerToBT(Cond, ISD::SETNE, dl, DAG);
9729 if (NewSetCC.getNode()) {
9730 CC = NewSetCC.getOperand(0);
9731 Cond = NewSetCC.getOperand(1);
9732 addTest = false;
9733 }
9734 }
9735 }
9736
9737 if (addTest) {
Owen Anderson825b72b2009-08-11 20:47:22 +00009738 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng552f09a2010-04-26 19:06:11 +00009739 Cond = EmitTest(Cond, X86::COND_NE, DAG);
Evan Cheng0488db92007-09-25 01:57:46 +00009740 }
Benjamin Kramer17c836c2012-04-27 12:07:43 +00009741 Cond = ConvertCmpIfNecessary(Cond, DAG);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009742 return DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Dan Gohman279c22e2008-10-21 03:29:32 +00009743 Chain, Dest, CC, Cond);
Evan Cheng0488db92007-09-25 01:57:46 +00009744}
9745
Anton Korobeynikove060b532007-04-17 19:34:00 +00009746
9747// Lower dynamic stack allocation to _alloca call for Cygwin/Mingw targets.
9748// Calls to _alloca is needed to probe the stack when allocating more than 4k
9749// bytes in one go. Touching the stack at 4K increments is necessary to ensure
9750// that the guard pages used by the OS virtual memory manager are allocated in
9751// correct sequence.
Dan Gohman475871a2008-07-27 21:46:04 +00009752SDValue
9753X86TargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00009754 SelectionDAG &DAG) const {
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009755 assert((Subtarget->isTargetCygMing() || Subtarget->isTargetWindows() ||
Nick Lewycky8a8d4792011-12-02 22:16:29 +00009756 getTargetMachine().Options.EnableSegmentedStacks) &&
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009757 "This should be used only on Windows targets or when segmented stacks "
Rafael Espindola96428ce2011-09-06 18:43:08 +00009758 "are being used");
9759 assert(!Subtarget->isTargetEnvMacho() && "Not implemented");
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009760 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov096b4612008-06-11 20:16:42 +00009761
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00009762 // Get the inputs.
Dan Gohman475871a2008-07-27 21:46:04 +00009763 SDValue Chain = Op.getOperand(0);
9764 SDValue Size = Op.getOperand(1);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00009765 // FIXME: Ensure alignment here
9766
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009767 bool Is64Bit = Subtarget->is64Bit();
9768 EVT SPTy = Is64Bit ? MVT::i64 : MVT::i32;
Anton Korobeynikov096b4612008-06-11 20:16:42 +00009769
Nick Lewycky8a8d4792011-12-02 22:16:29 +00009770 if (getTargetMachine().Options.EnableSegmentedStacks) {
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009771 MachineFunction &MF = DAG.getMachineFunction();
9772 MachineRegisterInfo &MRI = MF.getRegInfo();
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00009773
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009774 if (Is64Bit) {
9775 // The 64 bit implementation of segmented stacks needs to clobber both r10
Rafael Espindola96428ce2011-09-06 18:43:08 +00009776 // r11. This makes it impossible to use it along with nested parameters.
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009777 const Function *F = MF.getFunction();
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00009778
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009779 for (Function::const_arg_iterator I = F->arg_begin(), E = F->arg_end();
Craig Topper31a207a2012-05-04 06:39:13 +00009780 I != E; ++I)
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009781 if (I->hasNestAttr())
9782 report_fatal_error("Cannot use segmented stacks with functions that "
9783 "have nested arguments.");
9784 }
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00009785
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009786 const TargetRegisterClass *AddrRegClass =
9787 getRegClassFor(Subtarget->is64Bit() ? MVT::i64:MVT::i32);
9788 unsigned Vreg = MRI.createVirtualRegister(AddrRegClass);
9789 Chain = DAG.getCopyToReg(Chain, dl, Vreg, Size);
9790 SDValue Value = DAG.getNode(X86ISD::SEG_ALLOCA, dl, SPTy, Chain,
9791 DAG.getRegister(Vreg, SPTy));
9792 SDValue Ops1[2] = { Value, Chain };
9793 return DAG.getMergeValues(Ops1, 2, dl);
9794 } else {
9795 SDValue Flag;
9796 unsigned Reg = (Subtarget->is64Bit() ? X86::RAX : X86::EAX);
Anton Korobeynikov096b4612008-06-11 20:16:42 +00009797
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009798 Chain = DAG.getCopyToReg(Chain, dl, Reg, Size, Flag);
9799 Flag = Chain.getValue(1);
9800 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Anton Korobeynikov096b4612008-06-11 20:16:42 +00009801
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009802 Chain = DAG.getNode(X86ISD::WIN_ALLOCA, dl, NodeTys, Chain, Flag);
9803 Flag = Chain.getValue(1);
9804
Michael Liaoc5c970e2012-10-31 04:14:09 +00009805 Chain = DAG.getCopyFromReg(Chain, dl, RegInfo->getStackRegister(),
9806 SPTy).getValue(1);
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009807
9808 SDValue Ops1[2] = { Chain.getValue(0), Chain };
9809 return DAG.getMergeValues(Ops1, 2, dl);
9810 }
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00009811}
9812
Dan Gohmand858e902010-04-17 15:26:15 +00009813SDValue X86TargetLowering::LowerVASTART(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman1e93df62010-04-17 14:41:14 +00009814 MachineFunction &MF = DAG.getMachineFunction();
9815 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
9816
Dan Gohman69de1932008-02-06 22:27:42 +00009817 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Chris Lattner8026a9d2010-09-21 17:50:43 +00009818 DebugLoc DL = Op.getDebugLoc();
Evan Cheng8b2794a2006-10-13 21:14:26 +00009819
Anton Korobeynikove7beda12010-10-03 22:52:07 +00009820 if (!Subtarget->is64Bit() || Subtarget->isTargetWin64()) {
Evan Cheng25ab6902006-09-08 06:48:29 +00009821 // vastart just stores the address of the VarArgsFrameIndex slot into the
9822 // memory location argument.
Dan Gohman1e93df62010-04-17 14:41:14 +00009823 SDValue FR = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
9824 getPointerTy());
Chris Lattner8026a9d2010-09-21 17:50:43 +00009825 return DAG.getStore(Op.getOperand(0), DL, FR, Op.getOperand(1),
9826 MachinePointerInfo(SV), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00009827 }
9828
9829 // __va_list_tag:
9830 // gp_offset (0 - 6 * 8)
9831 // fp_offset (48 - 48 + 8 * 16)
9832 // overflow_arg_area (point to parameters coming in memory).
9833 // reg_save_area
Dan Gohman475871a2008-07-27 21:46:04 +00009834 SmallVector<SDValue, 8> MemOps;
9835 SDValue FIN = Op.getOperand(1);
Evan Cheng25ab6902006-09-08 06:48:29 +00009836 // Store gp_offset
Chris Lattner8026a9d2010-09-21 17:50:43 +00009837 SDValue Store = DAG.getStore(Op.getOperand(0), DL,
Dan Gohman1e93df62010-04-17 14:41:14 +00009838 DAG.getConstant(FuncInfo->getVarArgsGPOffset(),
9839 MVT::i32),
Chris Lattner8026a9d2010-09-21 17:50:43 +00009840 FIN, MachinePointerInfo(SV), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00009841 MemOps.push_back(Store);
9842
9843 // Store fp_offset
Chris Lattner8026a9d2010-09-21 17:50:43 +00009844 FIN = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00009845 FIN, DAG.getIntPtrConstant(4));
Chris Lattner8026a9d2010-09-21 17:50:43 +00009846 Store = DAG.getStore(Op.getOperand(0), DL,
Dan Gohman1e93df62010-04-17 14:41:14 +00009847 DAG.getConstant(FuncInfo->getVarArgsFPOffset(),
9848 MVT::i32),
Chris Lattner8026a9d2010-09-21 17:50:43 +00009849 FIN, MachinePointerInfo(SV, 4), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00009850 MemOps.push_back(Store);
9851
9852 // Store ptr to overflow_arg_area
Chris Lattner8026a9d2010-09-21 17:50:43 +00009853 FIN = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00009854 FIN, DAG.getIntPtrConstant(4));
Dan Gohman1e93df62010-04-17 14:41:14 +00009855 SDValue OVFIN = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
9856 getPointerTy());
Chris Lattner8026a9d2010-09-21 17:50:43 +00009857 Store = DAG.getStore(Op.getOperand(0), DL, OVFIN, FIN,
9858 MachinePointerInfo(SV, 8),
David Greene67c9d422010-02-15 16:53:33 +00009859 false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00009860 MemOps.push_back(Store);
9861
9862 // Store ptr to reg_save_area.
Chris Lattner8026a9d2010-09-21 17:50:43 +00009863 FIN = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00009864 FIN, DAG.getIntPtrConstant(8));
Dan Gohman1e93df62010-04-17 14:41:14 +00009865 SDValue RSFIN = DAG.getFrameIndex(FuncInfo->getRegSaveFrameIndex(),
9866 getPointerTy());
Chris Lattner8026a9d2010-09-21 17:50:43 +00009867 Store = DAG.getStore(Op.getOperand(0), DL, RSFIN, FIN,
9868 MachinePointerInfo(SV, 16), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00009869 MemOps.push_back(Store);
Chris Lattner8026a9d2010-09-21 17:50:43 +00009870 return DAG.getNode(ISD::TokenFactor, DL, MVT::Other,
Dale Johannesene4d209d2009-02-03 20:21:25 +00009871 &MemOps[0], MemOps.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00009872}
9873
Dan Gohmand858e902010-04-17 15:26:15 +00009874SDValue X86TargetLowering::LowerVAARG(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman320afb82010-10-12 18:00:49 +00009875 assert(Subtarget->is64Bit() &&
9876 "LowerVAARG only handles 64-bit va_arg!");
9877 assert((Subtarget->isTargetLinux() ||
9878 Subtarget->isTargetDarwin()) &&
9879 "Unhandled target in LowerVAARG");
9880 assert(Op.getNode()->getNumOperands() == 4);
9881 SDValue Chain = Op.getOperand(0);
9882 SDValue SrcPtr = Op.getOperand(1);
9883 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
9884 unsigned Align = Op.getConstantOperandVal(3);
9885 DebugLoc dl = Op.getDebugLoc();
Dan Gohman9018e832008-05-10 01:26:14 +00009886
Dan Gohman320afb82010-10-12 18:00:49 +00009887 EVT ArgVT = Op.getNode()->getValueType(0);
Chris Lattnerdb125cf2011-07-18 04:54:35 +00009888 Type *ArgTy = ArgVT.getTypeForEVT(*DAG.getContext());
Micah Villmow3574eca2012-10-08 16:38:25 +00009889 uint32_t ArgSize = getDataLayout()->getTypeAllocSize(ArgTy);
Dan Gohman320afb82010-10-12 18:00:49 +00009890 uint8_t ArgMode;
9891
9892 // Decide which area this value should be read from.
9893 // TODO: Implement the AMD64 ABI in its entirety. This simple
9894 // selection mechanism works only for the basic types.
9895 if (ArgVT == MVT::f80) {
9896 llvm_unreachable("va_arg for f80 not yet implemented");
9897 } else if (ArgVT.isFloatingPoint() && ArgSize <= 16 /*bytes*/) {
9898 ArgMode = 2; // Argument passed in XMM register. Use fp_offset.
9899 } else if (ArgVT.isInteger() && ArgSize <= 32 /*bytes*/) {
9900 ArgMode = 1; // Argument passed in GPR64 register(s). Use gp_offset.
9901 } else {
9902 llvm_unreachable("Unhandled argument type in LowerVAARG");
9903 }
9904
9905 if (ArgMode == 2) {
9906 // Sanity Check: Make sure using fp_offset makes sense.
Nick Lewycky8a8d4792011-12-02 22:16:29 +00009907 assert(!getTargetMachine().Options.UseSoftFloat &&
Eric Christopher52b45052010-10-12 19:44:17 +00009908 !(DAG.getMachineFunction()
Bill Wendling67658342012-10-09 07:45:08 +00009909 .getFunction()->getFnAttributes()
9910 .hasAttribute(Attributes::NoImplicitFloat)) &&
Craig Topper1accb7e2012-01-10 06:54:16 +00009911 Subtarget->hasSSE1());
Dan Gohman320afb82010-10-12 18:00:49 +00009912 }
9913
9914 // Insert VAARG_64 node into the DAG
9915 // VAARG_64 returns two values: Variable Argument Address, Chain
9916 SmallVector<SDValue, 11> InstOps;
9917 InstOps.push_back(Chain);
9918 InstOps.push_back(SrcPtr);
9919 InstOps.push_back(DAG.getConstant(ArgSize, MVT::i32));
9920 InstOps.push_back(DAG.getConstant(ArgMode, MVT::i8));
9921 InstOps.push_back(DAG.getConstant(Align, MVT::i32));
9922 SDVTList VTs = DAG.getVTList(getPointerTy(), MVT::Other);
9923 SDValue VAARG = DAG.getMemIntrinsicNode(X86ISD::VAARG_64, dl,
9924 VTs, &InstOps[0], InstOps.size(),
9925 MVT::i64,
9926 MachinePointerInfo(SV),
9927 /*Align=*/0,
9928 /*Volatile=*/false,
9929 /*ReadMem=*/true,
9930 /*WriteMem=*/true);
9931 Chain = VAARG.getValue(1);
9932
9933 // Load the next argument and return it
9934 return DAG.getLoad(ArgVT, dl,
9935 Chain,
9936 VAARG,
9937 MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00009938 false, false, false, 0);
Dan Gohman9018e832008-05-10 01:26:14 +00009939}
9940
Craig Topper55b24052012-09-11 06:15:32 +00009941static SDValue LowerVACOPY(SDValue Op, const X86Subtarget *Subtarget,
9942 SelectionDAG &DAG) {
Evan Chengae642192007-03-02 23:16:35 +00009943 // X86-64 va_list is a struct { i32, i32, i8*, i8* }.
Dan Gohman28269132008-04-18 20:55:41 +00009944 assert(Subtarget->is64Bit() && "This code only handles 64-bit va_copy!");
Dan Gohman475871a2008-07-27 21:46:04 +00009945 SDValue Chain = Op.getOperand(0);
9946 SDValue DstPtr = Op.getOperand(1);
9947 SDValue SrcPtr = Op.getOperand(2);
Dan Gohman69de1932008-02-06 22:27:42 +00009948 const Value *DstSV = cast<SrcValueSDNode>(Op.getOperand(3))->getValue();
9949 const Value *SrcSV = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Chris Lattnere72f2022010-09-21 05:40:29 +00009950 DebugLoc DL = Op.getDebugLoc();
Evan Chengae642192007-03-02 23:16:35 +00009951
Chris Lattnere72f2022010-09-21 05:40:29 +00009952 return DAG.getMemcpy(Chain, DL, DstPtr, SrcPtr,
Mon P Wang20adc9d2010-04-04 03:10:48 +00009953 DAG.getIntPtrConstant(24), 8, /*isVolatile*/false,
Michael J. Spencerec38de22010-10-10 22:04:20 +00009954 false,
Chris Lattnere72f2022010-09-21 05:40:29 +00009955 MachinePointerInfo(DstSV), MachinePointerInfo(SrcSV));
Evan Chengae642192007-03-02 23:16:35 +00009956}
9957
Craig Topper80e46362012-01-23 06:16:53 +00009958// getTargetVShiftNOde - Handle vector element shifts where the shift amount
9959// may or may not be a constant. Takes immediate version of shift as input.
9960static SDValue getTargetVShiftNode(unsigned Opc, DebugLoc dl, EVT VT,
9961 SDValue SrcOp, SDValue ShAmt,
9962 SelectionDAG &DAG) {
9963 assert(ShAmt.getValueType() == MVT::i32 && "ShAmt is not i32");
9964
9965 if (isa<ConstantSDNode>(ShAmt)) {
Nadav Rotemd896e242012-07-15 20:27:43 +00009966 // Constant may be a TargetConstant. Use a regular constant.
9967 uint32_t ShiftAmt = cast<ConstantSDNode>(ShAmt)->getZExtValue();
Craig Topper80e46362012-01-23 06:16:53 +00009968 switch (Opc) {
9969 default: llvm_unreachable("Unknown target vector shift node");
9970 case X86ISD::VSHLI:
9971 case X86ISD::VSRLI:
9972 case X86ISD::VSRAI:
Nadav Rotemd896e242012-07-15 20:27:43 +00009973 return DAG.getNode(Opc, dl, VT, SrcOp,
9974 DAG.getConstant(ShiftAmt, MVT::i32));
Craig Topper80e46362012-01-23 06:16:53 +00009975 }
9976 }
9977
9978 // Change opcode to non-immediate version
9979 switch (Opc) {
9980 default: llvm_unreachable("Unknown target vector shift node");
9981 case X86ISD::VSHLI: Opc = X86ISD::VSHL; break;
9982 case X86ISD::VSRLI: Opc = X86ISD::VSRL; break;
9983 case X86ISD::VSRAI: Opc = X86ISD::VSRA; break;
9984 }
9985
9986 // Need to build a vector containing shift amount
9987 // Shift amount is 32-bits, but SSE instructions read 64-bit, so fill with 0
9988 SDValue ShOps[4];
9989 ShOps[0] = ShAmt;
9990 ShOps[1] = DAG.getConstant(0, MVT::i32);
Craig Topper6d688152012-08-14 07:43:25 +00009991 ShOps[2] = ShOps[3] = DAG.getUNDEF(MVT::i32);
Craig Topper80e46362012-01-23 06:16:53 +00009992 ShAmt = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, &ShOps[0], 4);
Nadav Rotem65f489f2012-07-14 22:26:05 +00009993
9994 // The return type has to be a 128-bit type with the same element
9995 // type as the input type.
9996 MVT EltVT = VT.getVectorElementType().getSimpleVT();
9997 EVT ShVT = MVT::getVectorVT(EltVT, 128/EltVT.getSizeInBits());
9998
9999 ShAmt = DAG.getNode(ISD::BITCAST, dl, ShVT, ShAmt);
Craig Topper80e46362012-01-23 06:16:53 +000010000 return DAG.getNode(Opc, dl, VT, SrcOp, ShAmt);
10001}
10002
Craig Topper55b24052012-09-11 06:15:32 +000010003static SDValue LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG) {
Dale Johannesen6f38cb62009-02-07 19:59:05 +000010004 DebugLoc dl = Op.getDebugLoc();
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +000010005 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +000010006 switch (IntNo) {
Dan Gohman475871a2008-07-27 21:46:04 +000010007 default: return SDValue(); // Don't custom lower most intrinsics.
Evan Cheng5759f972008-05-04 09:15:50 +000010008 // Comparison intrinsics.
Evan Cheng0db9fe62006-04-25 20:13:52 +000010009 case Intrinsic::x86_sse_comieq_ss:
10010 case Intrinsic::x86_sse_comilt_ss:
10011 case Intrinsic::x86_sse_comile_ss:
10012 case Intrinsic::x86_sse_comigt_ss:
10013 case Intrinsic::x86_sse_comige_ss:
10014 case Intrinsic::x86_sse_comineq_ss:
10015 case Intrinsic::x86_sse_ucomieq_ss:
10016 case Intrinsic::x86_sse_ucomilt_ss:
10017 case Intrinsic::x86_sse_ucomile_ss:
10018 case Intrinsic::x86_sse_ucomigt_ss:
10019 case Intrinsic::x86_sse_ucomige_ss:
10020 case Intrinsic::x86_sse_ucomineq_ss:
10021 case Intrinsic::x86_sse2_comieq_sd:
10022 case Intrinsic::x86_sse2_comilt_sd:
10023 case Intrinsic::x86_sse2_comile_sd:
10024 case Intrinsic::x86_sse2_comigt_sd:
10025 case Intrinsic::x86_sse2_comige_sd:
10026 case Intrinsic::x86_sse2_comineq_sd:
10027 case Intrinsic::x86_sse2_ucomieq_sd:
10028 case Intrinsic::x86_sse2_ucomilt_sd:
10029 case Intrinsic::x86_sse2_ucomile_sd:
10030 case Intrinsic::x86_sse2_ucomigt_sd:
10031 case Intrinsic::x86_sse2_ucomige_sd:
10032 case Intrinsic::x86_sse2_ucomineq_sd: {
Craig Topper6d688152012-08-14 07:43:25 +000010033 unsigned Opc;
10034 ISD::CondCode CC;
Evan Cheng0db9fe62006-04-25 20:13:52 +000010035 switch (IntNo) {
Craig Topper86c7c582012-01-30 01:10:15 +000010036 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000010037 case Intrinsic::x86_sse_comieq_ss:
10038 case Intrinsic::x86_sse2_comieq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +000010039 Opc = X86ISD::COMI;
10040 CC = ISD::SETEQ;
10041 break;
Evan Cheng6be2c582006-04-05 23:38:46 +000010042 case Intrinsic::x86_sse_comilt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +000010043 case Intrinsic::x86_sse2_comilt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +000010044 Opc = X86ISD::COMI;
10045 CC = ISD::SETLT;
10046 break;
10047 case Intrinsic::x86_sse_comile_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +000010048 case Intrinsic::x86_sse2_comile_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +000010049 Opc = X86ISD::COMI;
10050 CC = ISD::SETLE;
10051 break;
10052 case Intrinsic::x86_sse_comigt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +000010053 case Intrinsic::x86_sse2_comigt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +000010054 Opc = X86ISD::COMI;
10055 CC = ISD::SETGT;
10056 break;
10057 case Intrinsic::x86_sse_comige_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +000010058 case Intrinsic::x86_sse2_comige_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +000010059 Opc = X86ISD::COMI;
10060 CC = ISD::SETGE;
10061 break;
10062 case Intrinsic::x86_sse_comineq_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +000010063 case Intrinsic::x86_sse2_comineq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +000010064 Opc = X86ISD::COMI;
10065 CC = ISD::SETNE;
10066 break;
10067 case Intrinsic::x86_sse_ucomieq_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +000010068 case Intrinsic::x86_sse2_ucomieq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +000010069 Opc = X86ISD::UCOMI;
10070 CC = ISD::SETEQ;
10071 break;
10072 case Intrinsic::x86_sse_ucomilt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +000010073 case Intrinsic::x86_sse2_ucomilt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +000010074 Opc = X86ISD::UCOMI;
10075 CC = ISD::SETLT;
10076 break;
10077 case Intrinsic::x86_sse_ucomile_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +000010078 case Intrinsic::x86_sse2_ucomile_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +000010079 Opc = X86ISD::UCOMI;
10080 CC = ISD::SETLE;
10081 break;
10082 case Intrinsic::x86_sse_ucomigt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +000010083 case Intrinsic::x86_sse2_ucomigt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +000010084 Opc = X86ISD::UCOMI;
10085 CC = ISD::SETGT;
10086 break;
10087 case Intrinsic::x86_sse_ucomige_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +000010088 case Intrinsic::x86_sse2_ucomige_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +000010089 Opc = X86ISD::UCOMI;
10090 CC = ISD::SETGE;
10091 break;
10092 case Intrinsic::x86_sse_ucomineq_ss:
10093 case Intrinsic::x86_sse2_ucomineq_sd:
10094 Opc = X86ISD::UCOMI;
10095 CC = ISD::SETNE;
10096 break;
Evan Cheng6be2c582006-04-05 23:38:46 +000010097 }
Evan Cheng734503b2006-09-11 02:19:56 +000010098
Dan Gohman475871a2008-07-27 21:46:04 +000010099 SDValue LHS = Op.getOperand(1);
10100 SDValue RHS = Op.getOperand(2);
Chris Lattner1c39d4c2008-12-24 23:53:05 +000010101 unsigned X86CC = TranslateX86CC(CC, true, LHS, RHS, DAG);
Dan Gohman1a492952009-10-20 16:22:37 +000010102 assert(X86CC != X86::COND_INVALID && "Unexpected illegal condition!");
Owen Anderson825b72b2009-08-11 20:47:22 +000010103 SDValue Cond = DAG.getNode(Opc, dl, MVT::i32, LHS, RHS);
10104 SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
10105 DAG.getConstant(X86CC, MVT::i8), Cond);
10106 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC);
Evan Cheng6be2c582006-04-05 23:38:46 +000010107 }
Craig Topper6d688152012-08-14 07:43:25 +000010108
Duncan Sands04aa4ae2011-09-23 16:10:22 +000010109 // Arithmetic intrinsics.
Craig Topper5b209e82012-02-05 03:14:49 +000010110 case Intrinsic::x86_sse2_pmulu_dq:
10111 case Intrinsic::x86_avx2_pmulu_dq:
10112 return DAG.getNode(X86ISD::PMULUDQ, dl, Op.getValueType(),
10113 Op.getOperand(1), Op.getOperand(2));
Craig Topper6d688152012-08-14 07:43:25 +000010114
10115 // 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
10164 // AVX2 variable shift intrinsics
Craig Topper98fc7292011-11-19 17:46:46 +000010165 case Intrinsic::x86_avx2_psllv_d:
10166 case Intrinsic::x86_avx2_psllv_q:
10167 case Intrinsic::x86_avx2_psllv_d_256:
10168 case Intrinsic::x86_avx2_psllv_q_256:
Craig Topper98fc7292011-11-19 17:46:46 +000010169 case Intrinsic::x86_avx2_psrlv_d:
10170 case Intrinsic::x86_avx2_psrlv_q:
10171 case Intrinsic::x86_avx2_psrlv_d_256:
10172 case Intrinsic::x86_avx2_psrlv_q_256:
Craig Topper98fc7292011-11-19 17:46:46 +000010173 case Intrinsic::x86_avx2_psrav_d:
Craig Topper6d688152012-08-14 07:43:25 +000010174 case Intrinsic::x86_avx2_psrav_d_256: {
10175 unsigned Opcode;
10176 switch (IntNo) {
10177 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
10178 case Intrinsic::x86_avx2_psllv_d:
10179 case Intrinsic::x86_avx2_psllv_q:
10180 case Intrinsic::x86_avx2_psllv_d_256:
10181 case Intrinsic::x86_avx2_psllv_q_256:
10182 Opcode = ISD::SHL;
10183 break;
10184 case Intrinsic::x86_avx2_psrlv_d:
10185 case Intrinsic::x86_avx2_psrlv_q:
10186 case Intrinsic::x86_avx2_psrlv_d_256:
10187 case Intrinsic::x86_avx2_psrlv_q_256:
10188 Opcode = ISD::SRL;
10189 break;
10190 case Intrinsic::x86_avx2_psrav_d:
10191 case Intrinsic::x86_avx2_psrav_d_256:
10192 Opcode = ISD::SRA;
10193 break;
10194 }
10195 return DAG.getNode(Opcode, dl, Op.getValueType(),
10196 Op.getOperand(1), Op.getOperand(2));
10197 }
10198
Craig Topper969ba282012-01-25 06:43:11 +000010199 case Intrinsic::x86_ssse3_pshuf_b_128:
10200 case Intrinsic::x86_avx2_pshuf_b:
10201 return DAG.getNode(X86ISD::PSHUFB, dl, Op.getValueType(),
10202 Op.getOperand(1), Op.getOperand(2));
Craig Topper6d688152012-08-14 07:43:25 +000010203
Craig Topper969ba282012-01-25 06:43:11 +000010204 case Intrinsic::x86_ssse3_psign_b_128:
10205 case Intrinsic::x86_ssse3_psign_w_128:
10206 case Intrinsic::x86_ssse3_psign_d_128:
10207 case Intrinsic::x86_avx2_psign_b:
10208 case Intrinsic::x86_avx2_psign_w:
10209 case Intrinsic::x86_avx2_psign_d:
10210 return DAG.getNode(X86ISD::PSIGN, dl, Op.getValueType(),
10211 Op.getOperand(1), Op.getOperand(2));
Craig Topper6d688152012-08-14 07:43:25 +000010212
Craig Toppere566cd02012-01-26 07:18:03 +000010213 case Intrinsic::x86_sse41_insertps:
10214 return DAG.getNode(X86ISD::INSERTPS, dl, Op.getValueType(),
10215 Op.getOperand(1), Op.getOperand(2), Op.getOperand(3));
Craig Topper6d688152012-08-14 07:43:25 +000010216
Craig Toppere566cd02012-01-26 07:18:03 +000010217 case Intrinsic::x86_avx_vperm2f128_ps_256:
10218 case Intrinsic::x86_avx_vperm2f128_pd_256:
10219 case Intrinsic::x86_avx_vperm2f128_si_256:
10220 case Intrinsic::x86_avx2_vperm2i128:
10221 return DAG.getNode(X86ISD::VPERM2X128, dl, Op.getValueType(),
10222 Op.getOperand(1), Op.getOperand(2), Op.getOperand(3));
Craig Topper6d688152012-08-14 07:43:25 +000010223
Craig Topperffa6c402012-04-16 07:13:00 +000010224 case Intrinsic::x86_avx2_permd:
10225 case Intrinsic::x86_avx2_permps:
10226 // Operands intentionally swapped. Mask is last operand to intrinsic,
10227 // but second operand for node/intruction.
10228 return DAG.getNode(X86ISD::VPERMV, dl, Op.getValueType(),
10229 Op.getOperand(2), Op.getOperand(1));
Craig Topper98fc7292011-11-19 17:46:46 +000010230
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +000010231 // ptest and testp intrinsics. The intrinsic these come from are designed to
10232 // return an integer value, not just an instruction so lower it to the ptest
10233 // or testp pattern and a setcc for the result.
Eric Christopher71c67532009-07-29 00:28:05 +000010234 case Intrinsic::x86_sse41_ptestz:
10235 case Intrinsic::x86_sse41_ptestc:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +000010236 case Intrinsic::x86_sse41_ptestnzc:
10237 case Intrinsic::x86_avx_ptestz_256:
10238 case Intrinsic::x86_avx_ptestc_256:
10239 case Intrinsic::x86_avx_ptestnzc_256:
10240 case Intrinsic::x86_avx_vtestz_ps:
10241 case Intrinsic::x86_avx_vtestc_ps:
10242 case Intrinsic::x86_avx_vtestnzc_ps:
10243 case Intrinsic::x86_avx_vtestz_pd:
10244 case Intrinsic::x86_avx_vtestc_pd:
10245 case Intrinsic::x86_avx_vtestnzc_pd:
10246 case Intrinsic::x86_avx_vtestz_ps_256:
10247 case Intrinsic::x86_avx_vtestc_ps_256:
10248 case Intrinsic::x86_avx_vtestnzc_ps_256:
10249 case Intrinsic::x86_avx_vtestz_pd_256:
10250 case Intrinsic::x86_avx_vtestc_pd_256:
10251 case Intrinsic::x86_avx_vtestnzc_pd_256: {
10252 bool IsTestPacked = false;
Craig Topper6d688152012-08-14 07:43:25 +000010253 unsigned X86CC;
Eric Christopher71c67532009-07-29 00:28:05 +000010254 switch (IntNo) {
Eric Christopher978dae32009-07-29 18:14:04 +000010255 default: llvm_unreachable("Bad fallthrough in Intrinsic lowering.");
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +000010256 case Intrinsic::x86_avx_vtestz_ps:
10257 case Intrinsic::x86_avx_vtestz_pd:
10258 case Intrinsic::x86_avx_vtestz_ps_256:
10259 case Intrinsic::x86_avx_vtestz_pd_256:
10260 IsTestPacked = true; // Fallthrough
Eric Christopher71c67532009-07-29 00:28:05 +000010261 case Intrinsic::x86_sse41_ptestz:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +000010262 case Intrinsic::x86_avx_ptestz_256:
Eric Christopher71c67532009-07-29 00:28:05 +000010263 // ZF = 1
10264 X86CC = X86::COND_E;
10265 break;
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +000010266 case Intrinsic::x86_avx_vtestc_ps:
10267 case Intrinsic::x86_avx_vtestc_pd:
10268 case Intrinsic::x86_avx_vtestc_ps_256:
10269 case Intrinsic::x86_avx_vtestc_pd_256:
10270 IsTestPacked = true; // Fallthrough
Eric Christopher71c67532009-07-29 00:28:05 +000010271 case Intrinsic::x86_sse41_ptestc:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +000010272 case Intrinsic::x86_avx_ptestc_256:
Eric Christopher71c67532009-07-29 00:28:05 +000010273 // CF = 1
10274 X86CC = X86::COND_B;
10275 break;
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +000010276 case Intrinsic::x86_avx_vtestnzc_ps:
10277 case Intrinsic::x86_avx_vtestnzc_pd:
10278 case Intrinsic::x86_avx_vtestnzc_ps_256:
10279 case Intrinsic::x86_avx_vtestnzc_pd_256:
10280 IsTestPacked = true; // Fallthrough
Eric Christopherfd179292009-08-27 18:07:15 +000010281 case Intrinsic::x86_sse41_ptestnzc:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +000010282 case Intrinsic::x86_avx_ptestnzc_256:
Eric Christopher71c67532009-07-29 00:28:05 +000010283 // ZF and CF = 0
10284 X86CC = X86::COND_A;
10285 break;
10286 }
Eric Christopherfd179292009-08-27 18:07:15 +000010287
Eric Christopher71c67532009-07-29 00:28:05 +000010288 SDValue LHS = Op.getOperand(1);
10289 SDValue RHS = Op.getOperand(2);
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +000010290 unsigned TestOpc = IsTestPacked ? X86ISD::TESTP : X86ISD::PTEST;
10291 SDValue Test = DAG.getNode(TestOpc, dl, MVT::i32, LHS, RHS);
Owen Anderson825b72b2009-08-11 20:47:22 +000010292 SDValue CC = DAG.getConstant(X86CC, MVT::i8);
10293 SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8, CC, Test);
10294 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC);
Eric Christopher71c67532009-07-29 00:28:05 +000010295 }
Evan Cheng5759f972008-05-04 09:15:50 +000010296
Craig Topper80e46362012-01-23 06:16:53 +000010297 // SSE/AVX shift intrinsics
10298 case Intrinsic::x86_sse2_psll_w:
10299 case Intrinsic::x86_sse2_psll_d:
10300 case Intrinsic::x86_sse2_psll_q:
10301 case Intrinsic::x86_avx2_psll_w:
10302 case Intrinsic::x86_avx2_psll_d:
10303 case Intrinsic::x86_avx2_psll_q:
Craig Topper80e46362012-01-23 06:16:53 +000010304 case Intrinsic::x86_sse2_psrl_w:
10305 case Intrinsic::x86_sse2_psrl_d:
10306 case Intrinsic::x86_sse2_psrl_q:
10307 case Intrinsic::x86_avx2_psrl_w:
10308 case Intrinsic::x86_avx2_psrl_d:
10309 case Intrinsic::x86_avx2_psrl_q:
Craig Topper80e46362012-01-23 06:16:53 +000010310 case Intrinsic::x86_sse2_psra_w:
10311 case Intrinsic::x86_sse2_psra_d:
10312 case Intrinsic::x86_avx2_psra_w:
Craig Topper6d688152012-08-14 07:43:25 +000010313 case Intrinsic::x86_avx2_psra_d: {
10314 unsigned Opcode;
10315 switch (IntNo) {
10316 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
10317 case Intrinsic::x86_sse2_psll_w:
10318 case Intrinsic::x86_sse2_psll_d:
10319 case Intrinsic::x86_sse2_psll_q:
10320 case Intrinsic::x86_avx2_psll_w:
10321 case Intrinsic::x86_avx2_psll_d:
10322 case Intrinsic::x86_avx2_psll_q:
10323 Opcode = X86ISD::VSHL;
10324 break;
10325 case Intrinsic::x86_sse2_psrl_w:
10326 case Intrinsic::x86_sse2_psrl_d:
10327 case Intrinsic::x86_sse2_psrl_q:
10328 case Intrinsic::x86_avx2_psrl_w:
10329 case Intrinsic::x86_avx2_psrl_d:
10330 case Intrinsic::x86_avx2_psrl_q:
10331 Opcode = X86ISD::VSRL;
10332 break;
10333 case Intrinsic::x86_sse2_psra_w:
10334 case Intrinsic::x86_sse2_psra_d:
10335 case Intrinsic::x86_avx2_psra_w:
10336 case Intrinsic::x86_avx2_psra_d:
10337 Opcode = X86ISD::VSRA;
10338 break;
10339 }
10340 return DAG.getNode(Opcode, dl, Op.getValueType(),
Craig Topper80e46362012-01-23 06:16:53 +000010341 Op.getOperand(1), Op.getOperand(2));
Craig Topper6d688152012-08-14 07:43:25 +000010342 }
10343
10344 // SSE/AVX immediate shift intrinsics
Evan Cheng5759f972008-05-04 09:15:50 +000010345 case Intrinsic::x86_sse2_pslli_w:
10346 case Intrinsic::x86_sse2_pslli_d:
10347 case Intrinsic::x86_sse2_pslli_q:
Craig Topper80e46362012-01-23 06:16:53 +000010348 case Intrinsic::x86_avx2_pslli_w:
10349 case Intrinsic::x86_avx2_pslli_d:
10350 case Intrinsic::x86_avx2_pslli_q:
Evan Cheng5759f972008-05-04 09:15:50 +000010351 case Intrinsic::x86_sse2_psrli_w:
10352 case Intrinsic::x86_sse2_psrli_d:
10353 case Intrinsic::x86_sse2_psrli_q:
Craig Topper80e46362012-01-23 06:16:53 +000010354 case Intrinsic::x86_avx2_psrli_w:
10355 case Intrinsic::x86_avx2_psrli_d:
10356 case Intrinsic::x86_avx2_psrli_q:
Evan Cheng5759f972008-05-04 09:15:50 +000010357 case Intrinsic::x86_sse2_psrai_w:
10358 case Intrinsic::x86_sse2_psrai_d:
Craig Topper80e46362012-01-23 06:16:53 +000010359 case Intrinsic::x86_avx2_psrai_w:
Craig Topper6d688152012-08-14 07:43:25 +000010360 case Intrinsic::x86_avx2_psrai_d: {
10361 unsigned Opcode;
10362 switch (IntNo) {
10363 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
10364 case Intrinsic::x86_sse2_pslli_w:
10365 case Intrinsic::x86_sse2_pslli_d:
10366 case Intrinsic::x86_sse2_pslli_q:
10367 case Intrinsic::x86_avx2_pslli_w:
10368 case Intrinsic::x86_avx2_pslli_d:
10369 case Intrinsic::x86_avx2_pslli_q:
10370 Opcode = X86ISD::VSHLI;
10371 break;
10372 case Intrinsic::x86_sse2_psrli_w:
10373 case Intrinsic::x86_sse2_psrli_d:
10374 case Intrinsic::x86_sse2_psrli_q:
10375 case Intrinsic::x86_avx2_psrli_w:
10376 case Intrinsic::x86_avx2_psrli_d:
10377 case Intrinsic::x86_avx2_psrli_q:
10378 Opcode = X86ISD::VSRLI;
10379 break;
10380 case Intrinsic::x86_sse2_psrai_w:
10381 case Intrinsic::x86_sse2_psrai_d:
10382 case Intrinsic::x86_avx2_psrai_w:
10383 case Intrinsic::x86_avx2_psrai_d:
10384 Opcode = X86ISD::VSRAI;
10385 break;
10386 }
10387 return getTargetVShiftNode(Opcode, dl, Op.getValueType(),
Craig Topper80e46362012-01-23 06:16:53 +000010388 Op.getOperand(1), Op.getOperand(2), DAG);
Craig Topper6d688152012-08-14 07:43:25 +000010389 }
10390
Craig Topper4feb6472012-08-06 06:22:36 +000010391 case Intrinsic::x86_sse42_pcmpistria128:
10392 case Intrinsic::x86_sse42_pcmpestria128:
10393 case Intrinsic::x86_sse42_pcmpistric128:
10394 case Intrinsic::x86_sse42_pcmpestric128:
10395 case Intrinsic::x86_sse42_pcmpistrio128:
10396 case Intrinsic::x86_sse42_pcmpestrio128:
10397 case Intrinsic::x86_sse42_pcmpistris128:
10398 case Intrinsic::x86_sse42_pcmpestris128:
10399 case Intrinsic::x86_sse42_pcmpistriz128:
10400 case Intrinsic::x86_sse42_pcmpestriz128: {
10401 unsigned Opcode;
10402 unsigned X86CC;
10403 switch (IntNo) {
10404 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
10405 case Intrinsic::x86_sse42_pcmpistria128:
10406 Opcode = X86ISD::PCMPISTRI;
10407 X86CC = X86::COND_A;
10408 break;
10409 case Intrinsic::x86_sse42_pcmpestria128:
10410 Opcode = X86ISD::PCMPESTRI;
10411 X86CC = X86::COND_A;
10412 break;
10413 case Intrinsic::x86_sse42_pcmpistric128:
10414 Opcode = X86ISD::PCMPISTRI;
10415 X86CC = X86::COND_B;
10416 break;
10417 case Intrinsic::x86_sse42_pcmpestric128:
10418 Opcode = X86ISD::PCMPESTRI;
10419 X86CC = X86::COND_B;
10420 break;
10421 case Intrinsic::x86_sse42_pcmpistrio128:
10422 Opcode = X86ISD::PCMPISTRI;
10423 X86CC = X86::COND_O;
10424 break;
10425 case Intrinsic::x86_sse42_pcmpestrio128:
10426 Opcode = X86ISD::PCMPESTRI;
10427 X86CC = X86::COND_O;
10428 break;
10429 case Intrinsic::x86_sse42_pcmpistris128:
10430 Opcode = X86ISD::PCMPISTRI;
10431 X86CC = X86::COND_S;
10432 break;
10433 case Intrinsic::x86_sse42_pcmpestris128:
10434 Opcode = X86ISD::PCMPESTRI;
10435 X86CC = X86::COND_S;
10436 break;
10437 case Intrinsic::x86_sse42_pcmpistriz128:
10438 Opcode = X86ISD::PCMPISTRI;
10439 X86CC = X86::COND_E;
10440 break;
10441 case Intrinsic::x86_sse42_pcmpestriz128:
10442 Opcode = X86ISD::PCMPESTRI;
10443 X86CC = X86::COND_E;
10444 break;
10445 }
10446 SmallVector<SDValue, 5> NewOps;
10447 NewOps.append(Op->op_begin()+1, Op->op_end());
10448 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::i32);
10449 SDValue PCMP = DAG.getNode(Opcode, dl, VTs, NewOps.data(), NewOps.size());
10450 SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
10451 DAG.getConstant(X86CC, MVT::i8),
10452 SDValue(PCMP.getNode(), 1));
10453 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC);
10454 }
Craig Topper6d688152012-08-14 07:43:25 +000010455
Craig Topper4feb6472012-08-06 06:22:36 +000010456 case Intrinsic::x86_sse42_pcmpistri128:
10457 case Intrinsic::x86_sse42_pcmpestri128: {
10458 unsigned Opcode;
10459 if (IntNo == Intrinsic::x86_sse42_pcmpistri128)
10460 Opcode = X86ISD::PCMPISTRI;
10461 else
10462 Opcode = X86ISD::PCMPESTRI;
10463
10464 SmallVector<SDValue, 5> NewOps;
10465 NewOps.append(Op->op_begin()+1, Op->op_end());
10466 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::i32);
10467 return DAG.getNode(Opcode, dl, VTs, NewOps.data(), NewOps.size());
10468 }
Craig Topper0e292372012-08-24 04:03:22 +000010469 case Intrinsic::x86_fma_vfmadd_ps:
10470 case Intrinsic::x86_fma_vfmadd_pd:
10471 case Intrinsic::x86_fma_vfmsub_ps:
10472 case Intrinsic::x86_fma_vfmsub_pd:
10473 case Intrinsic::x86_fma_vfnmadd_ps:
10474 case Intrinsic::x86_fma_vfnmadd_pd:
10475 case Intrinsic::x86_fma_vfnmsub_ps:
10476 case Intrinsic::x86_fma_vfnmsub_pd:
10477 case Intrinsic::x86_fma_vfmaddsub_ps:
10478 case Intrinsic::x86_fma_vfmaddsub_pd:
10479 case Intrinsic::x86_fma_vfmsubadd_ps:
10480 case Intrinsic::x86_fma_vfmsubadd_pd:
10481 case Intrinsic::x86_fma_vfmadd_ps_256:
10482 case Intrinsic::x86_fma_vfmadd_pd_256:
10483 case Intrinsic::x86_fma_vfmsub_ps_256:
10484 case Intrinsic::x86_fma_vfmsub_pd_256:
10485 case Intrinsic::x86_fma_vfnmadd_ps_256:
10486 case Intrinsic::x86_fma_vfnmadd_pd_256:
10487 case Intrinsic::x86_fma_vfnmsub_ps_256:
10488 case Intrinsic::x86_fma_vfnmsub_pd_256:
10489 case Intrinsic::x86_fma_vfmaddsub_ps_256:
10490 case Intrinsic::x86_fma_vfmaddsub_pd_256:
10491 case Intrinsic::x86_fma_vfmsubadd_ps_256:
10492 case Intrinsic::x86_fma_vfmsubadd_pd_256: {
Craig Topper0e292372012-08-24 04:03:22 +000010493 unsigned Opc;
10494 switch (IntNo) {
10495 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
10496 case Intrinsic::x86_fma_vfmadd_ps:
10497 case Intrinsic::x86_fma_vfmadd_pd:
10498 case Intrinsic::x86_fma_vfmadd_ps_256:
10499 case Intrinsic::x86_fma_vfmadd_pd_256:
10500 Opc = X86ISD::FMADD;
10501 break;
10502 case Intrinsic::x86_fma_vfmsub_ps:
10503 case Intrinsic::x86_fma_vfmsub_pd:
10504 case Intrinsic::x86_fma_vfmsub_ps_256:
10505 case Intrinsic::x86_fma_vfmsub_pd_256:
10506 Opc = X86ISD::FMSUB;
10507 break;
10508 case Intrinsic::x86_fma_vfnmadd_ps:
10509 case Intrinsic::x86_fma_vfnmadd_pd:
10510 case Intrinsic::x86_fma_vfnmadd_ps_256:
10511 case Intrinsic::x86_fma_vfnmadd_pd_256:
10512 Opc = X86ISD::FNMADD;
10513 break;
10514 case Intrinsic::x86_fma_vfnmsub_ps:
10515 case Intrinsic::x86_fma_vfnmsub_pd:
10516 case Intrinsic::x86_fma_vfnmsub_ps_256:
10517 case Intrinsic::x86_fma_vfnmsub_pd_256:
10518 Opc = X86ISD::FNMSUB;
10519 break;
10520 case Intrinsic::x86_fma_vfmaddsub_ps:
10521 case Intrinsic::x86_fma_vfmaddsub_pd:
10522 case Intrinsic::x86_fma_vfmaddsub_ps_256:
10523 case Intrinsic::x86_fma_vfmaddsub_pd_256:
10524 Opc = X86ISD::FMADDSUB;
10525 break;
10526 case Intrinsic::x86_fma_vfmsubadd_ps:
10527 case Intrinsic::x86_fma_vfmsubadd_pd:
10528 case Intrinsic::x86_fma_vfmsubadd_ps_256:
10529 case Intrinsic::x86_fma_vfmsubadd_pd_256:
10530 Opc = X86ISD::FMSUBADD;
10531 break;
10532 }
10533
10534 return DAG.getNode(Opc, dl, Op.getValueType(), Op.getOperand(1),
10535 Op.getOperand(2), Op.getOperand(3));
10536 }
Evan Cheng38bcbaf2005-12-23 07:31:11 +000010537 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000010538}
Evan Cheng72261582005-12-20 06:22:03 +000010539
Craig Topper55b24052012-09-11 06:15:32 +000010540static SDValue LowerINTRINSIC_W_CHAIN(SDValue Op, SelectionDAG &DAG) {
Benjamin Kramerb9bee042012-07-12 09:31:43 +000010541 DebugLoc dl = Op.getDebugLoc();
10542 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
10543 switch (IntNo) {
10544 default: return SDValue(); // Don't custom lower most intrinsics.
10545
10546 // RDRAND intrinsics.
10547 case Intrinsic::x86_rdrand_16:
10548 case Intrinsic::x86_rdrand_32:
10549 case Intrinsic::x86_rdrand_64: {
10550 // Emit the node with the right value type.
Benjamin Kramerfeae00a2012-07-12 18:14:57 +000010551 SDVTList VTs = DAG.getVTList(Op->getValueType(0), MVT::Glue, MVT::Other);
10552 SDValue Result = DAG.getNode(X86ISD::RDRAND, dl, VTs, Op.getOperand(0));
Benjamin Kramerb9bee042012-07-12 09:31:43 +000010553
10554 // If the value returned by RDRAND was valid (CF=1), return 1. Otherwise
10555 // return the value from Rand, which is always 0, casted to i32.
10556 SDValue Ops[] = { DAG.getZExtOrTrunc(Result, dl, Op->getValueType(1)),
10557 DAG.getConstant(1, Op->getValueType(1)),
10558 DAG.getConstant(X86::COND_B, MVT::i32),
10559 SDValue(Result.getNode(), 1) };
10560 SDValue isValid = DAG.getNode(X86ISD::CMOV, dl,
10561 DAG.getVTList(Op->getValueType(1), MVT::Glue),
10562 Ops, 4);
10563
10564 // Return { result, isValid, chain }.
10565 return DAG.getNode(ISD::MERGE_VALUES, dl, Op->getVTList(), Result, isValid,
Benjamin Kramerfeae00a2012-07-12 18:14:57 +000010566 SDValue(Result.getNode(), 2));
Benjamin Kramerb9bee042012-07-12 09:31:43 +000010567 }
10568 }
10569}
10570
Dan Gohmand858e902010-04-17 15:26:15 +000010571SDValue X86TargetLowering::LowerRETURNADDR(SDValue Op,
10572 SelectionDAG &DAG) const {
Evan Cheng2457f2c2010-05-22 01:47:14 +000010573 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
10574 MFI->setReturnAddressIsTaken(true);
10575
Bill Wendling64e87322009-01-16 19:25:27 +000010576 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Dale Johannesen6f38cb62009-02-07 19:59:05 +000010577 DebugLoc dl = Op.getDebugLoc();
Michael Liaoaa3c2c02012-10-25 06:29:14 +000010578 EVT PtrVT = getPointerTy();
Bill Wendling64e87322009-01-16 19:25:27 +000010579
10580 if (Depth > 0) {
10581 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
10582 SDValue Offset =
Michael Liaoaa3c2c02012-10-25 06:29:14 +000010583 DAG.getConstant(RegInfo->getSlotSize(), PtrVT);
10584 return DAG.getLoad(PtrVT, dl, DAG.getEntryNode(),
10585 DAG.getNode(ISD::ADD, dl, PtrVT,
Dale Johannesene4d209d2009-02-03 20:21:25 +000010586 FrameAddr, Offset),
Pete Cooperd752e0f2011-11-08 18:42:53 +000010587 MachinePointerInfo(), false, false, false, 0);
Bill Wendling64e87322009-01-16 19:25:27 +000010588 }
10589
10590 // Just load the return address.
Dan Gohman475871a2008-07-27 21:46:04 +000010591 SDValue RetAddrFI = getReturnAddressFrameIndex(DAG);
Michael Liaoaa3c2c02012-10-25 06:29:14 +000010592 return DAG.getLoad(PtrVT, dl, DAG.getEntryNode(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000010593 RetAddrFI, MachinePointerInfo(), false, false, false, 0);
Nate Begemanbcc5f362007-01-29 22:58:52 +000010594}
10595
Dan Gohmand858e902010-04-17 15:26:15 +000010596SDValue X86TargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng184793f2008-09-27 01:56:22 +000010597 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
10598 MFI->setFrameAddressIsTaken(true);
Evan Cheng2457f2c2010-05-22 01:47:14 +000010599
Owen Andersone50ed302009-08-10 22:56:29 +000010600 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +000010601 DebugLoc dl = Op.getDebugLoc(); // FIXME probably not meaningful
Evan Cheng184793f2008-09-27 01:56:22 +000010602 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
10603 unsigned FrameReg = Subtarget->is64Bit() ? X86::RBP : X86::EBP;
Dale Johannesendd64c412009-02-04 00:33:20 +000010604 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, VT);
Evan Cheng184793f2008-09-27 01:56:22 +000010605 while (Depth--)
Chris Lattner51abfe42010-09-21 06:02:19 +000010606 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr,
10607 MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000010608 false, false, false, 0);
Evan Cheng184793f2008-09-27 01:56:22 +000010609 return FrameAddr;
Nate Begemanbcc5f362007-01-29 22:58:52 +000010610}
10611
Dan Gohman475871a2008-07-27 21:46:04 +000010612SDValue X86TargetLowering::LowerFRAME_TO_ARGS_OFFSET(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +000010613 SelectionDAG &DAG) const {
Michael Liaoaa3c2c02012-10-25 06:29:14 +000010614 return DAG.getIntPtrConstant(2 * RegInfo->getSlotSize());
Anton Korobeynikov2365f512007-07-14 14:06:15 +000010615}
10616
Dan Gohmand858e902010-04-17 15:26:15 +000010617SDValue X86TargetLowering::LowerEH_RETURN(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +000010618 SDValue Chain = Op.getOperand(0);
10619 SDValue Offset = Op.getOperand(1);
10620 SDValue Handler = Op.getOperand(2);
Dale Johannesen6f38cb62009-02-07 19:59:05 +000010621 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov2365f512007-07-14 14:06:15 +000010622
Dan Gohmand8816272010-08-11 18:14:00 +000010623 SDValue Frame = DAG.getCopyFromReg(DAG.getEntryNode(), dl,
10624 Subtarget->is64Bit() ? X86::RBP : X86::EBP,
10625 getPointerTy());
Anton Korobeynikovb84c1672008-09-08 21:12:47 +000010626 unsigned StoreAddrReg = (Subtarget->is64Bit() ? X86::RCX : X86::ECX);
Anton Korobeynikov2365f512007-07-14 14:06:15 +000010627
Dan Gohmand8816272010-08-11 18:14:00 +000010628 SDValue StoreAddr = DAG.getNode(ISD::ADD, dl, getPointerTy(), Frame,
Michael Liaoaa3c2c02012-10-25 06:29:14 +000010629 DAG.getIntPtrConstant(RegInfo->getSlotSize()));
Dale Johannesene4d209d2009-02-03 20:21:25 +000010630 StoreAddr = DAG.getNode(ISD::ADD, dl, getPointerTy(), StoreAddr, Offset);
Chris Lattner8026a9d2010-09-21 17:50:43 +000010631 Chain = DAG.getStore(Chain, dl, Handler, StoreAddr, MachinePointerInfo(),
10632 false, false, 0);
Dale Johannesendd64c412009-02-04 00:33:20 +000010633 Chain = DAG.getCopyToReg(Chain, dl, StoreAddrReg, StoreAddr);
Anton Korobeynikov2365f512007-07-14 14:06:15 +000010634
Dale Johannesene4d209d2009-02-03 20:21:25 +000010635 return DAG.getNode(X86ISD::EH_RETURN, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +000010636 MVT::Other,
Anton Korobeynikovb84c1672008-09-08 21:12:47 +000010637 Chain, DAG.getRegister(StoreAddrReg, getPointerTy()));
Anton Korobeynikov2365f512007-07-14 14:06:15 +000010638}
10639
Michael Liao6c0e04c2012-10-15 22:39:43 +000010640SDValue X86TargetLowering::lowerEH_SJLJ_SETJMP(SDValue Op,
10641 SelectionDAG &DAG) const {
10642 DebugLoc DL = Op.getDebugLoc();
10643 return DAG.getNode(X86ISD::EH_SJLJ_SETJMP, DL,
10644 DAG.getVTList(MVT::i32, MVT::Other),
10645 Op.getOperand(0), Op.getOperand(1));
10646}
10647
10648SDValue X86TargetLowering::lowerEH_SJLJ_LONGJMP(SDValue Op,
10649 SelectionDAG &DAG) const {
10650 DebugLoc DL = Op.getDebugLoc();
10651 return DAG.getNode(X86ISD::EH_SJLJ_LONGJMP, DL, MVT::Other,
10652 Op.getOperand(0), Op.getOperand(1));
10653}
10654
Craig Topper55b24052012-09-11 06:15:32 +000010655static SDValue LowerADJUST_TRAMPOLINE(SDValue Op, SelectionDAG &DAG) {
Duncan Sands4a544a72011-09-06 13:37:06 +000010656 return Op.getOperand(0);
10657}
10658
10659SDValue X86TargetLowering::LowerINIT_TRAMPOLINE(SDValue Op,
10660 SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +000010661 SDValue Root = Op.getOperand(0);
10662 SDValue Trmp = Op.getOperand(1); // trampoline
10663 SDValue FPtr = Op.getOperand(2); // nested function
10664 SDValue Nest = Op.getOperand(3); // 'nest' parameter value
Dale Johannesen6f38cb62009-02-07 19:59:05 +000010665 DebugLoc dl = Op.getDebugLoc();
Duncan Sandsb116fac2007-07-27 20:02:49 +000010666
Dan Gohman69de1932008-02-06 22:27:42 +000010667 const Value *TrmpAddr = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Michael Liao7abf67a2012-10-04 19:50:43 +000010668 const TargetRegisterInfo* TRI = getTargetMachine().getRegisterInfo();
Duncan Sandsb116fac2007-07-27 20:02:49 +000010669
10670 if (Subtarget->is64Bit()) {
Dan Gohman475871a2008-07-27 21:46:04 +000010671 SDValue OutChains[6];
Duncan Sands339e14f2008-01-16 22:55:25 +000010672
10673 // Large code-model.
Chris Lattnera62fe662010-02-05 19:20:30 +000010674 const unsigned char JMP64r = 0xFF; // 64-bit jmp through register opcode.
10675 const unsigned char MOV64ri = 0xB8; // X86::MOV64ri opcode.
Duncan Sands339e14f2008-01-16 22:55:25 +000010676
Michael Liao7abf67a2012-10-04 19:50:43 +000010677 const unsigned char N86R10 = TRI->getEncodingValue(X86::R10) & 0x7;
10678 const unsigned char N86R11 = TRI->getEncodingValue(X86::R11) & 0x7;
Duncan Sands339e14f2008-01-16 22:55:25 +000010679
10680 const unsigned char REX_WB = 0x40 | 0x08 | 0x01; // REX prefix
10681
10682 // Load the pointer to the nested function into R11.
10683 unsigned OpCode = ((MOV64ri | N86R11) << 8) | REX_WB; // movabsq r11
Dan Gohman475871a2008-07-27 21:46:04 +000010684 SDValue Addr = Trmp;
Owen Anderson825b72b2009-08-11 20:47:22 +000010685 OutChains[0] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Chris Lattner8026a9d2010-09-21 17:50:43 +000010686 Addr, MachinePointerInfo(TrmpAddr),
10687 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +000010688
Owen Anderson825b72b2009-08-11 20:47:22 +000010689 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
10690 DAG.getConstant(2, MVT::i64));
Chris Lattner8026a9d2010-09-21 17:50:43 +000010691 OutChains[1] = DAG.getStore(Root, dl, FPtr, Addr,
10692 MachinePointerInfo(TrmpAddr, 2),
David Greene67c9d422010-02-15 16:53:33 +000010693 false, false, 2);
Duncan Sands339e14f2008-01-16 22:55:25 +000010694
10695 // Load the 'nest' parameter value into R10.
10696 // R10 is specified in X86CallingConv.td
10697 OpCode = ((MOV64ri | N86R10) << 8) | REX_WB; // movabsq r10
Owen Anderson825b72b2009-08-11 20:47:22 +000010698 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
10699 DAG.getConstant(10, MVT::i64));
10700 OutChains[2] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Chris Lattner8026a9d2010-09-21 17:50:43 +000010701 Addr, MachinePointerInfo(TrmpAddr, 10),
10702 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +000010703
Owen Anderson825b72b2009-08-11 20:47:22 +000010704 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
10705 DAG.getConstant(12, MVT::i64));
Chris Lattner8026a9d2010-09-21 17:50:43 +000010706 OutChains[3] = DAG.getStore(Root, dl, Nest, Addr,
10707 MachinePointerInfo(TrmpAddr, 12),
David Greene67c9d422010-02-15 16:53:33 +000010708 false, false, 2);
Duncan Sands339e14f2008-01-16 22:55:25 +000010709
10710 // Jump to the nested function.
10711 OpCode = (JMP64r << 8) | REX_WB; // jmpq *...
Owen Anderson825b72b2009-08-11 20:47:22 +000010712 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
10713 DAG.getConstant(20, MVT::i64));
10714 OutChains[4] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Chris Lattner8026a9d2010-09-21 17:50:43 +000010715 Addr, MachinePointerInfo(TrmpAddr, 20),
10716 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +000010717
10718 unsigned char ModRM = N86R11 | (4 << 3) | (3 << 6); // ...r11
Owen Anderson825b72b2009-08-11 20:47:22 +000010719 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
10720 DAG.getConstant(22, MVT::i64));
10721 OutChains[5] = DAG.getStore(Root, dl, DAG.getConstant(ModRM, MVT::i8), Addr,
Chris Lattner8026a9d2010-09-21 17:50:43 +000010722 MachinePointerInfo(TrmpAddr, 22),
10723 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +000010724
Duncan Sands4a544a72011-09-06 13:37:06 +000010725 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains, 6);
Duncan Sandsb116fac2007-07-27 20:02:49 +000010726 } else {
Dan Gohmanbbfb9c52008-01-31 01:01:48 +000010727 const Function *Func =
Duncan Sandsb116fac2007-07-27 20:02:49 +000010728 cast<Function>(cast<SrcValueSDNode>(Op.getOperand(5))->getValue());
Sandeep Patel65c3c8f2009-09-02 08:44:58 +000010729 CallingConv::ID CC = Func->getCallingConv();
Duncan Sandsee465742007-08-29 19:01:20 +000010730 unsigned NestReg;
Duncan Sandsb116fac2007-07-27 20:02:49 +000010731
10732 switch (CC) {
10733 default:
Torok Edwinc23197a2009-07-14 16:55:14 +000010734 llvm_unreachable("Unsupported calling convention");
Duncan Sandsb116fac2007-07-27 20:02:49 +000010735 case CallingConv::C:
Duncan Sandsb116fac2007-07-27 20:02:49 +000010736 case CallingConv::X86_StdCall: {
10737 // Pass 'nest' parameter in ECX.
10738 // Must be kept in sync with X86CallingConv.td
Duncan Sandsee465742007-08-29 19:01:20 +000010739 NestReg = X86::ECX;
Duncan Sandsb116fac2007-07-27 20:02:49 +000010740
10741 // Check that ECX wasn't needed by an 'inreg' parameter.
Chris Lattnerdb125cf2011-07-18 04:54:35 +000010742 FunctionType *FTy = Func->getFunctionType();
Devang Patel05988662008-09-25 21:00:45 +000010743 const AttrListPtr &Attrs = Func->getAttributes();
Duncan Sandsb116fac2007-07-27 20:02:49 +000010744
Chris Lattner58d74912008-03-12 17:45:29 +000010745 if (!Attrs.isEmpty() && !Func->isVarArg()) {
Duncan Sandsb116fac2007-07-27 20:02:49 +000010746 unsigned InRegCount = 0;
10747 unsigned Idx = 1;
10748
10749 for (FunctionType::param_iterator I = FTy->param_begin(),
10750 E = FTy->param_end(); I != E; ++I, ++Idx)
Bill Wendling67658342012-10-09 07:45:08 +000010751 if (Attrs.getParamAttributes(Idx).hasAttribute(Attributes::InReg))
Duncan Sandsb116fac2007-07-27 20:02:49 +000010752 // FIXME: should only count parameters that are lowered to integers.
Anton Korobeynikovbff66b02008-09-09 18:22:57 +000010753 InRegCount += (TD->getTypeSizeInBits(*I) + 31) / 32;
Duncan Sandsb116fac2007-07-27 20:02:49 +000010754
10755 if (InRegCount > 2) {
Eric Christopher90eb4022010-07-22 00:26:08 +000010756 report_fatal_error("Nest register in use - reduce number of inreg"
10757 " parameters!");
Duncan Sandsb116fac2007-07-27 20:02:49 +000010758 }
10759 }
10760 break;
10761 }
10762 case CallingConv::X86_FastCall:
Anton Korobeynikovded05e32010-05-16 09:08:45 +000010763 case CallingConv::X86_ThisCall:
Duncan Sandsbf53c292008-09-10 13:22:10 +000010764 case CallingConv::Fast:
Duncan Sandsb116fac2007-07-27 20:02:49 +000010765 // Pass 'nest' parameter in EAX.
10766 // Must be kept in sync with X86CallingConv.td
Duncan Sandsee465742007-08-29 19:01:20 +000010767 NestReg = X86::EAX;
Duncan Sandsb116fac2007-07-27 20:02:49 +000010768 break;
10769 }
10770
Dan Gohman475871a2008-07-27 21:46:04 +000010771 SDValue OutChains[4];
10772 SDValue Addr, Disp;
Duncan Sandsb116fac2007-07-27 20:02:49 +000010773
Owen Anderson825b72b2009-08-11 20:47:22 +000010774 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
10775 DAG.getConstant(10, MVT::i32));
10776 Disp = DAG.getNode(ISD::SUB, dl, MVT::i32, FPtr, Addr);
Duncan Sandsb116fac2007-07-27 20:02:49 +000010777
Chris Lattnera62fe662010-02-05 19:20:30 +000010778 // This is storing the opcode for MOV32ri.
10779 const unsigned char MOV32ri = 0xB8; // X86::MOV32ri's opcode byte.
Michael Liao7abf67a2012-10-04 19:50:43 +000010780 const unsigned char N86Reg = TRI->getEncodingValue(NestReg) & 0x7;
Scott Michelfdc40a02009-02-17 22:15:04 +000010781 OutChains[0] = DAG.getStore(Root, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +000010782 DAG.getConstant(MOV32ri|N86Reg, MVT::i8),
Chris Lattner8026a9d2010-09-21 17:50:43 +000010783 Trmp, MachinePointerInfo(TrmpAddr),
10784 false, false, 0);
Duncan Sandsb116fac2007-07-27 20:02:49 +000010785
Owen Anderson825b72b2009-08-11 20:47:22 +000010786 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
10787 DAG.getConstant(1, MVT::i32));
Chris Lattner8026a9d2010-09-21 17:50:43 +000010788 OutChains[1] = DAG.getStore(Root, dl, Nest, Addr,
10789 MachinePointerInfo(TrmpAddr, 1),
David Greene67c9d422010-02-15 16:53:33 +000010790 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +000010791
Chris Lattnera62fe662010-02-05 19:20:30 +000010792 const unsigned char JMP = 0xE9; // jmp <32bit dst> opcode.
Owen Anderson825b72b2009-08-11 20:47:22 +000010793 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
10794 DAG.getConstant(5, MVT::i32));
10795 OutChains[2] = DAG.getStore(Root, dl, DAG.getConstant(JMP, MVT::i8), Addr,
Chris Lattner8026a9d2010-09-21 17:50:43 +000010796 MachinePointerInfo(TrmpAddr, 5),
10797 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +000010798
Owen Anderson825b72b2009-08-11 20:47:22 +000010799 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
10800 DAG.getConstant(6, MVT::i32));
Chris Lattner8026a9d2010-09-21 17:50:43 +000010801 OutChains[3] = DAG.getStore(Root, dl, Disp, Addr,
10802 MachinePointerInfo(TrmpAddr, 6),
David Greene67c9d422010-02-15 16:53:33 +000010803 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +000010804
Duncan Sands4a544a72011-09-06 13:37:06 +000010805 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains, 4);
Duncan Sandsb116fac2007-07-27 20:02:49 +000010806 }
10807}
10808
Dan Gohmand858e902010-04-17 15:26:15 +000010809SDValue X86TargetLowering::LowerFLT_ROUNDS_(SDValue Op,
10810 SelectionDAG &DAG) const {
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000010811 /*
10812 The rounding mode is in bits 11:10 of FPSR, and has the following
10813 settings:
10814 00 Round to nearest
10815 01 Round to -inf
10816 10 Round to +inf
10817 11 Round to 0
10818
10819 FLT_ROUNDS, on the other hand, expects the following:
10820 -1 Undefined
10821 0 Round to 0
10822 1 Round to nearest
10823 2 Round to +inf
10824 3 Round to -inf
10825
10826 To perform the conversion, we do:
10827 (((((FPSR & 0x800) >> 11) | ((FPSR & 0x400) >> 9)) + 1) & 3)
10828 */
10829
10830 MachineFunction &MF = DAG.getMachineFunction();
10831 const TargetMachine &TM = MF.getTarget();
Anton Korobeynikov16c29b52011-01-10 12:39:04 +000010832 const TargetFrameLowering &TFI = *TM.getFrameLowering();
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000010833 unsigned StackAlignment = TFI.getStackAlignment();
Owen Andersone50ed302009-08-10 22:56:29 +000010834 EVT VT = Op.getValueType();
Chris Lattner2156b792010-09-22 01:11:26 +000010835 DebugLoc DL = Op.getDebugLoc();
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000010836
10837 // Save FP Control Word to stack slot
David Greene3f2bf852009-11-12 20:49:22 +000010838 int SSFI = MF.getFrameInfo()->CreateStackObject(2, StackAlignment, false);
Dan Gohman475871a2008-07-27 21:46:04 +000010839 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000010840
Michael J. Spencerec38de22010-10-10 22:04:20 +000010841
Chris Lattner2156b792010-09-22 01:11:26 +000010842 MachineMemOperand *MMO =
10843 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
10844 MachineMemOperand::MOStore, 2, 2);
Michael J. Spencerec38de22010-10-10 22:04:20 +000010845
Chris Lattner2156b792010-09-22 01:11:26 +000010846 SDValue Ops[] = { DAG.getEntryNode(), StackSlot };
10847 SDValue Chain = DAG.getMemIntrinsicNode(X86ISD::FNSTCW16m, DL,
10848 DAG.getVTList(MVT::Other),
10849 Ops, 2, MVT::i16, MMO);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000010850
10851 // Load FP Control Word from stack slot
Chris Lattner2156b792010-09-22 01:11:26 +000010852 SDValue CWD = DAG.getLoad(MVT::i16, DL, Chain, StackSlot,
Pete Cooperd752e0f2011-11-08 18:42:53 +000010853 MachinePointerInfo(), false, false, false, 0);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000010854
10855 // Transform as necessary
Dan Gohman475871a2008-07-27 21:46:04 +000010856 SDValue CWD1 =
Chris Lattner2156b792010-09-22 01:11:26 +000010857 DAG.getNode(ISD::SRL, DL, MVT::i16,
10858 DAG.getNode(ISD::AND, DL, MVT::i16,
Owen Anderson825b72b2009-08-11 20:47:22 +000010859 CWD, DAG.getConstant(0x800, MVT::i16)),
10860 DAG.getConstant(11, MVT::i8));
Dan Gohman475871a2008-07-27 21:46:04 +000010861 SDValue CWD2 =
Chris Lattner2156b792010-09-22 01:11:26 +000010862 DAG.getNode(ISD::SRL, DL, MVT::i16,
10863 DAG.getNode(ISD::AND, DL, MVT::i16,
Owen Anderson825b72b2009-08-11 20:47:22 +000010864 CWD, DAG.getConstant(0x400, MVT::i16)),
10865 DAG.getConstant(9, MVT::i8));
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000010866
Dan Gohman475871a2008-07-27 21:46:04 +000010867 SDValue RetVal =
Chris Lattner2156b792010-09-22 01:11:26 +000010868 DAG.getNode(ISD::AND, DL, MVT::i16,
10869 DAG.getNode(ISD::ADD, DL, MVT::i16,
10870 DAG.getNode(ISD::OR, DL, MVT::i16, CWD1, CWD2),
Owen Anderson825b72b2009-08-11 20:47:22 +000010871 DAG.getConstant(1, MVT::i16)),
10872 DAG.getConstant(3, MVT::i16));
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000010873
10874
Duncan Sands83ec4b62008-06-06 12:08:01 +000010875 return DAG.getNode((VT.getSizeInBits() < 16 ?
Chris Lattner2156b792010-09-22 01:11:26 +000010876 ISD::TRUNCATE : ISD::ZERO_EXTEND), DL, VT, RetVal);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000010877}
10878
Craig Topper55b24052012-09-11 06:15:32 +000010879static SDValue LowerCTLZ(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +000010880 EVT VT = Op.getValueType();
10881 EVT OpVT = VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +000010882 unsigned NumBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +000010883 DebugLoc dl = Op.getDebugLoc();
Evan Cheng18efe262007-12-14 02:13:44 +000010884
10885 Op = Op.getOperand(0);
Owen Anderson825b72b2009-08-11 20:47:22 +000010886 if (VT == MVT::i8) {
Evan Cheng152804e2007-12-14 08:30:15 +000010887 // Zero extend to i32 since there is not an i8 bsr.
Owen Anderson825b72b2009-08-11 20:47:22 +000010888 OpVT = MVT::i32;
Dale Johannesene4d209d2009-02-03 20:21:25 +000010889 Op = DAG.getNode(ISD::ZERO_EXTEND, dl, OpVT, Op);
Evan Cheng18efe262007-12-14 02:13:44 +000010890 }
Evan Cheng18efe262007-12-14 02:13:44 +000010891
Evan Cheng152804e2007-12-14 08:30:15 +000010892 // Issue a bsr (scan bits in reverse) which also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +000010893 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +000010894 Op = DAG.getNode(X86ISD::BSR, dl, VTs, Op);
Evan Cheng152804e2007-12-14 08:30:15 +000010895
10896 // If src is zero (i.e. bsr sets ZF), returns NumBits.
Benjamin Kramer7f1a5602009-12-29 16:57:26 +000010897 SDValue Ops[] = {
10898 Op,
10899 DAG.getConstant(NumBits+NumBits-1, OpVT),
10900 DAG.getConstant(X86::COND_E, MVT::i8),
10901 Op.getValue(1)
10902 };
10903 Op = DAG.getNode(X86ISD::CMOV, dl, OpVT, Ops, array_lengthof(Ops));
Evan Cheng152804e2007-12-14 08:30:15 +000010904
10905 // Finally xor with NumBits-1.
Dale Johannesene4d209d2009-02-03 20:21:25 +000010906 Op = DAG.getNode(ISD::XOR, dl, OpVT, Op, DAG.getConstant(NumBits-1, OpVT));
Evan Cheng152804e2007-12-14 08:30:15 +000010907
Owen Anderson825b72b2009-08-11 20:47:22 +000010908 if (VT == MVT::i8)
10909 Op = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Op);
Evan Cheng18efe262007-12-14 02:13:44 +000010910 return Op;
10911}
10912
Craig Topper55b24052012-09-11 06:15:32 +000010913static SDValue LowerCTLZ_ZERO_UNDEF(SDValue Op, SelectionDAG &DAG) {
Chandler Carruthacc068e2011-12-24 10:55:54 +000010914 EVT VT = Op.getValueType();
10915 EVT OpVT = VT;
10916 unsigned NumBits = VT.getSizeInBits();
10917 DebugLoc dl = Op.getDebugLoc();
10918
10919 Op = Op.getOperand(0);
10920 if (VT == MVT::i8) {
10921 // Zero extend to i32 since there is not an i8 bsr.
10922 OpVT = MVT::i32;
10923 Op = DAG.getNode(ISD::ZERO_EXTEND, dl, OpVT, Op);
10924 }
10925
10926 // Issue a bsr (scan bits in reverse).
10927 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
10928 Op = DAG.getNode(X86ISD::BSR, dl, VTs, Op);
10929
10930 // And xor with NumBits-1.
10931 Op = DAG.getNode(ISD::XOR, dl, OpVT, Op, DAG.getConstant(NumBits-1, OpVT));
10932
10933 if (VT == MVT::i8)
10934 Op = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Op);
10935 return Op;
10936}
10937
Craig Topper55b24052012-09-11 06:15:32 +000010938static SDValue LowerCTTZ(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +000010939 EVT VT = Op.getValueType();
Duncan Sands83ec4b62008-06-06 12:08:01 +000010940 unsigned NumBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +000010941 DebugLoc dl = Op.getDebugLoc();
Evan Cheng18efe262007-12-14 02:13:44 +000010942 Op = Op.getOperand(0);
Evan Cheng152804e2007-12-14 08:30:15 +000010943
10944 // Issue a bsf (scan bits forward) which also sets EFLAGS.
Chandler Carruth77821022011-12-24 12:12:34 +000010945 SDVTList VTs = DAG.getVTList(VT, MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +000010946 Op = DAG.getNode(X86ISD::BSF, dl, VTs, Op);
Evan Cheng152804e2007-12-14 08:30:15 +000010947
10948 // If src is zero (i.e. bsf sets ZF), returns NumBits.
Benjamin Kramer7f1a5602009-12-29 16:57:26 +000010949 SDValue Ops[] = {
10950 Op,
Chandler Carruth77821022011-12-24 12:12:34 +000010951 DAG.getConstant(NumBits, VT),
Benjamin Kramer7f1a5602009-12-29 16:57:26 +000010952 DAG.getConstant(X86::COND_E, MVT::i8),
10953 Op.getValue(1)
10954 };
Chandler Carruth77821022011-12-24 12:12:34 +000010955 return DAG.getNode(X86ISD::CMOV, dl, VT, Ops, array_lengthof(Ops));
Evan Cheng18efe262007-12-14 02:13:44 +000010956}
10957
Craig Topper13894fa2011-08-24 06:14:18 +000010958// Lower256IntArith - Break a 256-bit integer operation into two new 128-bit
10959// ones, and then concatenate the result back.
10960static SDValue Lower256IntArith(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +000010961 EVT VT = Op.getValueType();
Craig Topper13894fa2011-08-24 06:14:18 +000010962
Craig Topper7a9a28b2012-08-12 02:23:29 +000010963 assert(VT.is256BitVector() && VT.isInteger() &&
Craig Topper13894fa2011-08-24 06:14:18 +000010964 "Unsupported value type for operation");
10965
Craig Topper66ddd152012-04-27 22:54:43 +000010966 unsigned NumElems = VT.getVectorNumElements();
Craig Topper13894fa2011-08-24 06:14:18 +000010967 DebugLoc dl = Op.getDebugLoc();
Craig Topper13894fa2011-08-24 06:14:18 +000010968
10969 // Extract the LHS vectors
10970 SDValue LHS = Op.getOperand(0);
Craig Topperb14940a2012-04-22 20:55:18 +000010971 SDValue LHS1 = Extract128BitVector(LHS, 0, DAG, dl);
10972 SDValue LHS2 = Extract128BitVector(LHS, NumElems/2, DAG, dl);
Craig Topper13894fa2011-08-24 06:14:18 +000010973
10974 // Extract the RHS vectors
10975 SDValue RHS = Op.getOperand(1);
Craig Topperb14940a2012-04-22 20:55:18 +000010976 SDValue RHS1 = Extract128BitVector(RHS, 0, DAG, dl);
10977 SDValue RHS2 = Extract128BitVector(RHS, NumElems/2, DAG, dl);
Craig Topper13894fa2011-08-24 06:14:18 +000010978
10979 MVT EltVT = VT.getVectorElementType().getSimpleVT();
10980 EVT NewVT = MVT::getVectorVT(EltVT, NumElems/2);
10981
10982 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT,
10983 DAG.getNode(Op.getOpcode(), dl, NewVT, LHS1, RHS1),
10984 DAG.getNode(Op.getOpcode(), dl, NewVT, LHS2, RHS2));
10985}
10986
Craig Topper55b24052012-09-11 06:15:32 +000010987static SDValue LowerADD(SDValue Op, SelectionDAG &DAG) {
Craig Topper7a9a28b2012-08-12 02:23:29 +000010988 assert(Op.getValueType().is256BitVector() &&
Craig Topper13894fa2011-08-24 06:14:18 +000010989 Op.getValueType().isInteger() &&
10990 "Only handle AVX 256-bit vector integer operation");
10991 return Lower256IntArith(Op, DAG);
10992}
10993
Craig Topper55b24052012-09-11 06:15:32 +000010994static SDValue LowerSUB(SDValue Op, SelectionDAG &DAG) {
Craig Topper7a9a28b2012-08-12 02:23:29 +000010995 assert(Op.getValueType().is256BitVector() &&
Craig Topper13894fa2011-08-24 06:14:18 +000010996 Op.getValueType().isInteger() &&
10997 "Only handle AVX 256-bit vector integer operation");
10998 return Lower256IntArith(Op, DAG);
10999}
11000
Craig Topper55b24052012-09-11 06:15:32 +000011001static SDValue LowerMUL(SDValue Op, const X86Subtarget *Subtarget,
11002 SelectionDAG &DAG) {
Craig Topper13894fa2011-08-24 06:14:18 +000011003 EVT VT = Op.getValueType();
11004
11005 // Decompose 256-bit ops into smaller 128-bit ops.
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000011006 if (VT.is256BitVector() && !Subtarget->hasInt256())
Craig Topper13894fa2011-08-24 06:14:18 +000011007 return Lower256IntArith(Op, DAG);
11008
Craig Topper5b209e82012-02-05 03:14:49 +000011009 assert((VT == MVT::v2i64 || VT == MVT::v4i64) &&
11010 "Only know how to lower V2I64/V4I64 multiply");
11011
Dale Johannesen6f38cb62009-02-07 19:59:05 +000011012 DebugLoc dl = Op.getDebugLoc();
Scott Michelfdc40a02009-02-17 22:15:04 +000011013
Craig Topper5b209e82012-02-05 03:14:49 +000011014 // Ahi = psrlqi(a, 32);
11015 // Bhi = psrlqi(b, 32);
11016 //
11017 // AloBlo = pmuludq(a, b);
11018 // AloBhi = pmuludq(a, Bhi);
11019 // AhiBlo = pmuludq(Ahi, b);
11020
11021 // AloBhi = psllqi(AloBhi, 32);
11022 // AhiBlo = psllqi(AhiBlo, 32);
11023 // return AloBlo + AloBhi + AhiBlo;
11024
Craig Topperaaa643c2011-11-09 07:28:55 +000011025 SDValue A = Op.getOperand(0);
11026 SDValue B = Op.getOperand(1);
11027
Craig Topper5b209e82012-02-05 03:14:49 +000011028 SDValue ShAmt = DAG.getConstant(32, MVT::i32);
Craig Topperaaa643c2011-11-09 07:28:55 +000011029
Craig Topper5b209e82012-02-05 03:14:49 +000011030 SDValue Ahi = DAG.getNode(X86ISD::VSRLI, dl, VT, A, ShAmt);
11031 SDValue Bhi = DAG.getNode(X86ISD::VSRLI, dl, VT, B, ShAmt);
Craig Topperaaa643c2011-11-09 07:28:55 +000011032
Craig Topper5b209e82012-02-05 03:14:49 +000011033 // Bit cast to 32-bit vectors for MULUDQ
11034 EVT MulVT = (VT == MVT::v2i64) ? MVT::v4i32 : MVT::v8i32;
11035 A = DAG.getNode(ISD::BITCAST, dl, MulVT, A);
11036 B = DAG.getNode(ISD::BITCAST, dl, MulVT, B);
11037 Ahi = DAG.getNode(ISD::BITCAST, dl, MulVT, Ahi);
11038 Bhi = DAG.getNode(ISD::BITCAST, dl, MulVT, Bhi);
Craig Topperaaa643c2011-11-09 07:28:55 +000011039
Craig Topper5b209e82012-02-05 03:14:49 +000011040 SDValue AloBlo = DAG.getNode(X86ISD::PMULUDQ, dl, VT, A, B);
11041 SDValue AloBhi = DAG.getNode(X86ISD::PMULUDQ, dl, VT, A, Bhi);
11042 SDValue AhiBlo = DAG.getNode(X86ISD::PMULUDQ, dl, VT, Ahi, B);
Craig Topperaaa643c2011-11-09 07:28:55 +000011043
Craig Topper5b209e82012-02-05 03:14:49 +000011044 AloBhi = DAG.getNode(X86ISD::VSHLI, dl, VT, AloBhi, ShAmt);
11045 AhiBlo = DAG.getNode(X86ISD::VSHLI, dl, VT, AhiBlo, ShAmt);
Mon P Wangaf9b9522008-12-18 21:42:19 +000011046
Dale Johannesene4d209d2009-02-03 20:21:25 +000011047 SDValue Res = DAG.getNode(ISD::ADD, dl, VT, AloBlo, AloBhi);
Craig Topper5b209e82012-02-05 03:14:49 +000011048 return DAG.getNode(ISD::ADD, dl, VT, Res, AhiBlo);
Mon P Wangaf9b9522008-12-18 21:42:19 +000011049}
11050
Nadav Rotem43012222011-05-11 08:12:09 +000011051SDValue X86TargetLowering::LowerShift(SDValue Op, SelectionDAG &DAG) const {
11052
Nate Begemanbdcb5af2010-07-27 22:37:06 +000011053 EVT VT = Op.getValueType();
11054 DebugLoc dl = Op.getDebugLoc();
11055 SDValue R = Op.getOperand(0);
Nadav Rotem43012222011-05-11 08:12:09 +000011056 SDValue Amt = Op.getOperand(1);
Nate Begemanbdcb5af2010-07-27 22:37:06 +000011057 LLVMContext *Context = DAG.getContext();
Nate Begemanbdcb5af2010-07-27 22:37:06 +000011058
Craig Topper1accb7e2012-01-10 06:54:16 +000011059 if (!Subtarget->hasSSE2())
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +000011060 return SDValue();
11061
Nadav Rotem43012222011-05-11 08:12:09 +000011062 // Optimize shl/srl/sra with constant shift amount.
11063 if (isSplatVector(Amt.getNode())) {
11064 SDValue SclrAmt = Amt->getOperand(0);
11065 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(SclrAmt)) {
11066 uint64_t ShiftAmt = C->getZExtValue();
11067
Craig Toppered2e13d2012-01-22 19:15:14 +000011068 if (VT == MVT::v2i64 || VT == MVT::v4i32 || VT == MVT::v8i16 ||
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000011069 (Subtarget->hasInt256() &&
Craig Toppered2e13d2012-01-22 19:15:14 +000011070 (VT == MVT::v4i64 || VT == MVT::v8i32 || VT == MVT::v16i16))) {
11071 if (Op.getOpcode() == ISD::SHL)
11072 return DAG.getNode(X86ISD::VSHLI, dl, VT, R,
11073 DAG.getConstant(ShiftAmt, MVT::i32));
11074 if (Op.getOpcode() == ISD::SRL)
11075 return DAG.getNode(X86ISD::VSRLI, dl, VT, R,
11076 DAG.getConstant(ShiftAmt, MVT::i32));
11077 if (Op.getOpcode() == ISD::SRA && VT != MVT::v2i64 && VT != MVT::v4i64)
11078 return DAG.getNode(X86ISD::VSRAI, dl, VT, R,
11079 DAG.getConstant(ShiftAmt, MVT::i32));
Benjamin Kramerdade3c12011-10-30 17:31:21 +000011080 }
11081
Craig Toppered2e13d2012-01-22 19:15:14 +000011082 if (VT == MVT::v16i8) {
11083 if (Op.getOpcode() == ISD::SHL) {
11084 // Make a large shift.
11085 SDValue SHL = DAG.getNode(X86ISD::VSHLI, dl, MVT::v8i16, R,
11086 DAG.getConstant(ShiftAmt, MVT::i32));
11087 SHL = DAG.getNode(ISD::BITCAST, dl, VT, SHL);
11088 // Zero out the rightmost bits.
11089 SmallVector<SDValue, 16> V(16,
11090 DAG.getConstant(uint8_t(-1U << ShiftAmt),
11091 MVT::i8));
11092 return DAG.getNode(ISD::AND, dl, VT, SHL,
11093 DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &V[0], 16));
Eli Friedmanf6aa6b12011-11-01 21:18:39 +000011094 }
Craig Toppered2e13d2012-01-22 19:15:14 +000011095 if (Op.getOpcode() == ISD::SRL) {
11096 // Make a large shift.
11097 SDValue SRL = DAG.getNode(X86ISD::VSRLI, dl, MVT::v8i16, R,
11098 DAG.getConstant(ShiftAmt, MVT::i32));
11099 SRL = DAG.getNode(ISD::BITCAST, dl, VT, SRL);
11100 // Zero out the leftmost bits.
11101 SmallVector<SDValue, 16> V(16,
11102 DAG.getConstant(uint8_t(-1U) >> ShiftAmt,
11103 MVT::i8));
11104 return DAG.getNode(ISD::AND, dl, VT, SRL,
11105 DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &V[0], 16));
11106 }
11107 if (Op.getOpcode() == ISD::SRA) {
11108 if (ShiftAmt == 7) {
11109 // R s>> 7 === R s< 0
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +000011110 SDValue Zeros = getZeroVector(VT, Subtarget, DAG, dl);
Craig Topper67609fd2012-01-22 22:42:16 +000011111 return DAG.getNode(X86ISD::PCMPGT, dl, VT, Zeros, R);
Craig Toppered2e13d2012-01-22 19:15:14 +000011112 }
Eli Friedmanf6aa6b12011-11-01 21:18:39 +000011113
Craig Toppered2e13d2012-01-22 19:15:14 +000011114 // R s>> a === ((R u>> a) ^ m) - m
11115 SDValue Res = DAG.getNode(ISD::SRL, dl, VT, R, Amt);
11116 SmallVector<SDValue, 16> V(16, DAG.getConstant(128 >> ShiftAmt,
11117 MVT::i8));
11118 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &V[0], 16);
11119 Res = DAG.getNode(ISD::XOR, dl, VT, Res, Mask);
11120 Res = DAG.getNode(ISD::SUB, dl, VT, Res, Mask);
11121 return Res;
11122 }
Craig Topper731dfd02012-04-23 03:42:40 +000011123 llvm_unreachable("Unknown shift opcode.");
Eli Friedmanf6aa6b12011-11-01 21:18:39 +000011124 }
Craig Topper46154eb2011-11-11 07:39:23 +000011125
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000011126 if (Subtarget->hasInt256() && VT == MVT::v32i8) {
Craig Topper0d86d462011-11-20 00:12:05 +000011127 if (Op.getOpcode() == ISD::SHL) {
11128 // Make a large shift.
Craig Toppered2e13d2012-01-22 19:15:14 +000011129 SDValue SHL = DAG.getNode(X86ISD::VSHLI, dl, MVT::v16i16, R,
11130 DAG.getConstant(ShiftAmt, MVT::i32));
11131 SHL = DAG.getNode(ISD::BITCAST, dl, VT, SHL);
Craig Topper0d86d462011-11-20 00:12:05 +000011132 // Zero out the rightmost bits.
Craig Toppered2e13d2012-01-22 19:15:14 +000011133 SmallVector<SDValue, 32> V(32,
11134 DAG.getConstant(uint8_t(-1U << ShiftAmt),
11135 MVT::i8));
Craig Topper0d86d462011-11-20 00:12:05 +000011136 return DAG.getNode(ISD::AND, dl, VT, SHL,
11137 DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &V[0], 32));
Craig Topper46154eb2011-11-11 07:39:23 +000011138 }
Craig Topper0d86d462011-11-20 00:12:05 +000011139 if (Op.getOpcode() == ISD::SRL) {
11140 // Make a large shift.
Craig Toppered2e13d2012-01-22 19:15:14 +000011141 SDValue SRL = DAG.getNode(X86ISD::VSRLI, dl, MVT::v16i16, R,
11142 DAG.getConstant(ShiftAmt, MVT::i32));
11143 SRL = DAG.getNode(ISD::BITCAST, dl, VT, SRL);
Craig Topper0d86d462011-11-20 00:12:05 +000011144 // Zero out the leftmost bits.
Craig Toppered2e13d2012-01-22 19:15:14 +000011145 SmallVector<SDValue, 32> V(32,
11146 DAG.getConstant(uint8_t(-1U) >> ShiftAmt,
11147 MVT::i8));
Craig Topper0d86d462011-11-20 00:12:05 +000011148 return DAG.getNode(ISD::AND, dl, VT, SRL,
11149 DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &V[0], 32));
11150 }
11151 if (Op.getOpcode() == ISD::SRA) {
11152 if (ShiftAmt == 7) {
11153 // R s>> 7 === R s< 0
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +000011154 SDValue Zeros = getZeroVector(VT, Subtarget, DAG, dl);
Craig Topper67609fd2012-01-22 22:42:16 +000011155 return DAG.getNode(X86ISD::PCMPGT, dl, VT, Zeros, R);
Craig Topper0d86d462011-11-20 00:12:05 +000011156 }
11157
11158 // R s>> a === ((R u>> a) ^ m) - m
11159 SDValue Res = DAG.getNode(ISD::SRL, dl, VT, R, Amt);
11160 SmallVector<SDValue, 32> V(32, DAG.getConstant(128 >> ShiftAmt,
11161 MVT::i8));
11162 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &V[0], 32);
11163 Res = DAG.getNode(ISD::XOR, dl, VT, Res, Mask);
11164 Res = DAG.getNode(ISD::SUB, dl, VT, Res, Mask);
11165 return Res;
11166 }
Craig Topper731dfd02012-04-23 03:42:40 +000011167 llvm_unreachable("Unknown shift opcode.");
Craig Topper0d86d462011-11-20 00:12:05 +000011168 }
Nadav Rotem43012222011-05-11 08:12:09 +000011169 }
11170 }
11171
11172 // Lower SHL with variable shift amount.
Nadav Rotem43012222011-05-11 08:12:09 +000011173 if (VT == MVT::v4i32 && Op->getOpcode() == ISD::SHL) {
Craig Toppered2e13d2012-01-22 19:15:14 +000011174 Op = DAG.getNode(X86ISD::VSHLI, dl, VT, Op.getOperand(1),
11175 DAG.getConstant(23, MVT::i32));
Nate Begeman51409212010-07-28 00:21:48 +000011176
Chris Lattner7302d802012-02-06 21:56:39 +000011177 const uint32_t CV[] = { 0x3f800000U, 0x3f800000U, 0x3f800000U, 0x3f800000U};
11178 Constant *C = ConstantDataVector::get(*Context, CV);
Nate Begeman51409212010-07-28 00:21:48 +000011179 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
11180 SDValue Addend = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +000011181 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000011182 false, false, false, 16);
Nate Begeman51409212010-07-28 00:21:48 +000011183
11184 Op = DAG.getNode(ISD::ADD, dl, VT, Op, Addend);
Wesley Peckbf17cfa2010-11-23 03:31:01 +000011185 Op = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, Op);
Nate Begeman51409212010-07-28 00:21:48 +000011186 Op = DAG.getNode(ISD::FP_TO_SINT, dl, VT, Op);
11187 return DAG.getNode(ISD::MUL, dl, VT, Op, R);
11188 }
Nadav Rotem43012222011-05-11 08:12:09 +000011189 if (VT == MVT::v16i8 && Op->getOpcode() == ISD::SHL) {
Craig Topper8b5a6b62012-01-17 08:23:44 +000011190 assert(Subtarget->hasSSE2() && "Need SSE2 for pslli/pcmpeq.");
Lang Hames8b99c1e2011-12-17 01:08:46 +000011191
Nate Begeman51409212010-07-28 00:21:48 +000011192 // a = a << 5;
Craig Toppered2e13d2012-01-22 19:15:14 +000011193 Op = DAG.getNode(X86ISD::VSHLI, dl, MVT::v8i16, Op.getOperand(1),
11194 DAG.getConstant(5, MVT::i32));
11195 Op = DAG.getNode(ISD::BITCAST, dl, VT, Op);
Nate Begeman51409212010-07-28 00:21:48 +000011196
Lang Hames8b99c1e2011-12-17 01:08:46 +000011197 // Turn 'a' into a mask suitable for VSELECT
11198 SDValue VSelM = DAG.getConstant(0x80, VT);
11199 SDValue OpVSel = DAG.getNode(ISD::AND, dl, VT, VSelM, Op);
Craig Topper67609fd2012-01-22 22:42:16 +000011200 OpVSel = DAG.getNode(X86ISD::PCMPEQ, dl, VT, OpVSel, VSelM);
Nate Begeman51409212010-07-28 00:21:48 +000011201
Lang Hames8b99c1e2011-12-17 01:08:46 +000011202 SDValue CM1 = DAG.getConstant(0x0f, VT);
11203 SDValue CM2 = DAG.getConstant(0x3f, VT);
Nate Begeman51409212010-07-28 00:21:48 +000011204
Lang Hames8b99c1e2011-12-17 01:08:46 +000011205 // r = VSELECT(r, psllw(r & (char16)15, 4), a);
11206 SDValue M = DAG.getNode(ISD::AND, dl, VT, R, CM1);
Craig Toppered2e13d2012-01-22 19:15:14 +000011207 M = getTargetVShiftNode(X86ISD::VSHLI, dl, MVT::v8i16, M,
11208 DAG.getConstant(4, MVT::i32), DAG);
11209 M = DAG.getNode(ISD::BITCAST, dl, VT, M);
Lang Hames8b99c1e2011-12-17 01:08:46 +000011210 R = DAG.getNode(ISD::VSELECT, dl, VT, OpVSel, M, R);
11211
Nate Begeman51409212010-07-28 00:21:48 +000011212 // a += a
11213 Op = DAG.getNode(ISD::ADD, dl, VT, Op, Op);
Lang Hames8b99c1e2011-12-17 01:08:46 +000011214 OpVSel = DAG.getNode(ISD::AND, dl, VT, VSelM, Op);
Craig Topper67609fd2012-01-22 22:42:16 +000011215 OpVSel = DAG.getNode(X86ISD::PCMPEQ, dl, VT, OpVSel, VSelM);
Michael J. Spencerec38de22010-10-10 22:04:20 +000011216
Lang Hames8b99c1e2011-12-17 01:08:46 +000011217 // r = VSELECT(r, psllw(r & (char16)63, 2), a);
11218 M = DAG.getNode(ISD::AND, dl, VT, R, CM2);
Craig Toppered2e13d2012-01-22 19:15:14 +000011219 M = getTargetVShiftNode(X86ISD::VSHLI, dl, MVT::v8i16, M,
11220 DAG.getConstant(2, MVT::i32), DAG);
11221 M = DAG.getNode(ISD::BITCAST, dl, VT, M);
Lang Hames8b99c1e2011-12-17 01:08:46 +000011222 R = DAG.getNode(ISD::VSELECT, dl, VT, OpVSel, M, R);
11223
Nate Begeman51409212010-07-28 00:21:48 +000011224 // a += a
11225 Op = DAG.getNode(ISD::ADD, dl, VT, Op, Op);
Lang Hames8b99c1e2011-12-17 01:08:46 +000011226 OpVSel = DAG.getNode(ISD::AND, dl, VT, VSelM, Op);
Craig Topper67609fd2012-01-22 22:42:16 +000011227 OpVSel = DAG.getNode(X86ISD::PCMPEQ, dl, VT, OpVSel, VSelM);
Michael J. Spencerec38de22010-10-10 22:04:20 +000011228
Lang Hames8b99c1e2011-12-17 01:08:46 +000011229 // return VSELECT(r, r+r, a);
11230 R = DAG.getNode(ISD::VSELECT, dl, VT, OpVSel,
Lang Hamesa0a25132011-12-15 18:57:27 +000011231 DAG.getNode(ISD::ADD, dl, VT, R, R), R);
Nate Begeman51409212010-07-28 00:21:48 +000011232 return R;
11233 }
Craig Topper46154eb2011-11-11 07:39:23 +000011234
11235 // Decompose 256-bit shifts into smaller 128-bit shifts.
Craig Topper7a9a28b2012-08-12 02:23:29 +000011236 if (VT.is256BitVector()) {
Craig Toppered2e13d2012-01-22 19:15:14 +000011237 unsigned NumElems = VT.getVectorNumElements();
Craig Topper46154eb2011-11-11 07:39:23 +000011238 MVT EltVT = VT.getVectorElementType().getSimpleVT();
11239 EVT NewVT = MVT::getVectorVT(EltVT, NumElems/2);
11240
11241 // Extract the two vectors
Craig Topperb14940a2012-04-22 20:55:18 +000011242 SDValue V1 = Extract128BitVector(R, 0, DAG, dl);
11243 SDValue V2 = Extract128BitVector(R, NumElems/2, DAG, dl);
Craig Topper46154eb2011-11-11 07:39:23 +000011244
11245 // Recreate the shift amount vectors
11246 SDValue Amt1, Amt2;
11247 if (Amt.getOpcode() == ISD::BUILD_VECTOR) {
11248 // Constant shift amount
11249 SmallVector<SDValue, 4> Amt1Csts;
11250 SmallVector<SDValue, 4> Amt2Csts;
Craig Toppered2e13d2012-01-22 19:15:14 +000011251 for (unsigned i = 0; i != NumElems/2; ++i)
Craig Topper46154eb2011-11-11 07:39:23 +000011252 Amt1Csts.push_back(Amt->getOperand(i));
Craig Toppered2e13d2012-01-22 19:15:14 +000011253 for (unsigned i = NumElems/2; i != NumElems; ++i)
Craig Topper46154eb2011-11-11 07:39:23 +000011254 Amt2Csts.push_back(Amt->getOperand(i));
11255
11256 Amt1 = DAG.getNode(ISD::BUILD_VECTOR, dl, NewVT,
11257 &Amt1Csts[0], NumElems/2);
11258 Amt2 = DAG.getNode(ISD::BUILD_VECTOR, dl, NewVT,
11259 &Amt2Csts[0], NumElems/2);
11260 } else {
11261 // Variable shift amount
Craig Topperb14940a2012-04-22 20:55:18 +000011262 Amt1 = Extract128BitVector(Amt, 0, DAG, dl);
11263 Amt2 = Extract128BitVector(Amt, NumElems/2, DAG, dl);
Craig Topper46154eb2011-11-11 07:39:23 +000011264 }
11265
11266 // Issue new vector shifts for the smaller types
11267 V1 = DAG.getNode(Op.getOpcode(), dl, NewVT, V1, Amt1);
11268 V2 = DAG.getNode(Op.getOpcode(), dl, NewVT, V2, Amt2);
11269
11270 // Concatenate the result back
11271 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT, V1, V2);
11272 }
11273
Nate Begeman51409212010-07-28 00:21:48 +000011274 return SDValue();
Nate Begemanbdcb5af2010-07-27 22:37:06 +000011275}
Mon P Wangaf9b9522008-12-18 21:42:19 +000011276
Craig Topper55b24052012-09-11 06:15:32 +000011277static SDValue LowerXALUO(SDValue Op, SelectionDAG &DAG) {
Bill Wendling74c37652008-12-09 22:08:41 +000011278 // Lower the "add/sub/mul with overflow" instruction into a regular ins plus
11279 // a "setcc" instruction that checks the overflow flag. The "brcond" lowering
Bill Wendling61edeb52008-12-02 01:06:39 +000011280 // looks for this combo and may remove the "setcc" instruction if the "setcc"
11281 // has only one use.
Bill Wendling3fafd932008-11-26 22:37:40 +000011282 SDNode *N = Op.getNode();
Bill Wendling61edeb52008-12-02 01:06:39 +000011283 SDValue LHS = N->getOperand(0);
11284 SDValue RHS = N->getOperand(1);
Bill Wendling74c37652008-12-09 22:08:41 +000011285 unsigned BaseOp = 0;
11286 unsigned Cond = 0;
Chris Lattnerb20e0b12010-12-05 07:30:36 +000011287 DebugLoc DL = Op.getDebugLoc();
Bill Wendling74c37652008-12-09 22:08:41 +000011288 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +000011289 default: llvm_unreachable("Unknown ovf instruction!");
Bill Wendling74c37652008-12-09 22:08:41 +000011290 case ISD::SADDO:
Dan Gohman076aee32009-03-04 19:44:21 +000011291 // A subtract of one will be selected as a INC. Note that INC doesn't
11292 // set CF, so we can't do this for UADDO.
Benjamin Kramerc175a4b2011-03-08 15:20:20 +000011293 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(RHS))
11294 if (C->isOne()) {
Dan Gohman076aee32009-03-04 19:44:21 +000011295 BaseOp = X86ISD::INC;
11296 Cond = X86::COND_O;
11297 break;
11298 }
Bill Wendlingab55ebd2008-12-12 00:56:36 +000011299 BaseOp = X86ISD::ADD;
Bill Wendling74c37652008-12-09 22:08:41 +000011300 Cond = X86::COND_O;
11301 break;
11302 case ISD::UADDO:
Bill Wendlingab55ebd2008-12-12 00:56:36 +000011303 BaseOp = X86ISD::ADD;
Dan Gohman653456c2009-01-07 00:15:08 +000011304 Cond = X86::COND_B;
Bill Wendling74c37652008-12-09 22:08:41 +000011305 break;
11306 case ISD::SSUBO:
Dan Gohman076aee32009-03-04 19:44:21 +000011307 // A subtract of one will be selected as a DEC. Note that DEC doesn't
11308 // set CF, so we can't do this for USUBO.
Benjamin Kramerc175a4b2011-03-08 15:20:20 +000011309 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(RHS))
11310 if (C->isOne()) {
Dan Gohman076aee32009-03-04 19:44:21 +000011311 BaseOp = X86ISD::DEC;
11312 Cond = X86::COND_O;
11313 break;
11314 }
Bill Wendlingab55ebd2008-12-12 00:56:36 +000011315 BaseOp = X86ISD::SUB;
Bill Wendling74c37652008-12-09 22:08:41 +000011316 Cond = X86::COND_O;
11317 break;
11318 case ISD::USUBO:
Bill Wendlingab55ebd2008-12-12 00:56:36 +000011319 BaseOp = X86ISD::SUB;
Dan Gohman653456c2009-01-07 00:15:08 +000011320 Cond = X86::COND_B;
Bill Wendling74c37652008-12-09 22:08:41 +000011321 break;
11322 case ISD::SMULO:
Bill Wendlingd350e022008-12-12 21:15:41 +000011323 BaseOp = X86ISD::SMUL;
Bill Wendling74c37652008-12-09 22:08:41 +000011324 Cond = X86::COND_O;
11325 break;
Chris Lattnerb20e0b12010-12-05 07:30:36 +000011326 case ISD::UMULO: { // i64, i8 = umulo lhs, rhs --> i64, i64, i32 umul lhs,rhs
11327 SDVTList VTs = DAG.getVTList(N->getValueType(0), N->getValueType(0),
11328 MVT::i32);
11329 SDValue Sum = DAG.getNode(X86ISD::UMUL, DL, VTs, LHS, RHS);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011330
Chris Lattnerb20e0b12010-12-05 07:30:36 +000011331 SDValue SetCC =
11332 DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
11333 DAG.getConstant(X86::COND_O, MVT::i32),
11334 SDValue(Sum.getNode(), 2));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011335
Dan Gohman6e5fda22011-07-22 18:45:15 +000011336 return DAG.getNode(ISD::MERGE_VALUES, DL, N->getVTList(), Sum, SetCC);
Chris Lattnerb20e0b12010-12-05 07:30:36 +000011337 }
Bill Wendling74c37652008-12-09 22:08:41 +000011338 }
Bill Wendling3fafd932008-11-26 22:37:40 +000011339
Bill Wendling61edeb52008-12-02 01:06:39 +000011340 // Also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +000011341 SDVTList VTs = DAG.getVTList(N->getValueType(0), MVT::i32);
Chris Lattnerb20e0b12010-12-05 07:30:36 +000011342 SDValue Sum = DAG.getNode(BaseOp, DL, VTs, LHS, RHS);
Bill Wendling3fafd932008-11-26 22:37:40 +000011343
Bill Wendling61edeb52008-12-02 01:06:39 +000011344 SDValue SetCC =
Chris Lattnerb20e0b12010-12-05 07:30:36 +000011345 DAG.getNode(X86ISD::SETCC, DL, N->getValueType(1),
11346 DAG.getConstant(Cond, MVT::i32),
11347 SDValue(Sum.getNode(), 1));
Bill Wendling3fafd932008-11-26 22:37:40 +000011348
Dan Gohman6e5fda22011-07-22 18:45:15 +000011349 return DAG.getNode(ISD::MERGE_VALUES, DL, N->getVTList(), Sum, SetCC);
Bill Wendling41ea7e72008-11-24 19:21:46 +000011350}
11351
Chad Rosier30450e82011-12-22 22:35:21 +000011352SDValue X86TargetLowering::LowerSIGN_EXTEND_INREG(SDValue Op,
11353 SelectionDAG &DAG) const {
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000011354 DebugLoc dl = Op.getDebugLoc();
Craig Toppera124f942011-11-21 01:12:36 +000011355 EVT ExtraVT = cast<VTSDNode>(Op.getOperand(1))->getVT();
11356 EVT VT = Op.getValueType();
11357
Craig Toppered2e13d2012-01-22 19:15:14 +000011358 if (!Subtarget->hasSSE2() || !VT.isVector())
11359 return SDValue();
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000011360
Craig Toppered2e13d2012-01-22 19:15:14 +000011361 unsigned BitsDiff = VT.getScalarType().getSizeInBits() -
11362 ExtraVT.getScalarType().getSizeInBits();
11363 SDValue ShAmt = DAG.getConstant(BitsDiff, MVT::i32);
11364
11365 switch (VT.getSimpleVT().SimpleTy) {
11366 default: return SDValue();
11367 case MVT::v8i32:
11368 case MVT::v16i16:
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000011369 if (!Subtarget->hasFp256())
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000011370 return SDValue();
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000011371 if (!Subtarget->hasInt256()) {
Craig Toppered2e13d2012-01-22 19:15:14 +000011372 // needs to be split
Craig Topper66ddd152012-04-27 22:54:43 +000011373 unsigned NumElems = VT.getVectorNumElements();
Craig Toppera124f942011-11-21 01:12:36 +000011374
Craig Toppered2e13d2012-01-22 19:15:14 +000011375 // Extract the LHS vectors
11376 SDValue LHS = Op.getOperand(0);
Craig Topperb14940a2012-04-22 20:55:18 +000011377 SDValue LHS1 = Extract128BitVector(LHS, 0, DAG, dl);
11378 SDValue LHS2 = Extract128BitVector(LHS, NumElems/2, DAG, dl);
Craig Toppera124f942011-11-21 01:12:36 +000011379
Craig Toppered2e13d2012-01-22 19:15:14 +000011380 MVT EltVT = VT.getVectorElementType().getSimpleVT();
11381 EVT NewVT = MVT::getVectorVT(EltVT, NumElems/2);
Craig Toppera124f942011-11-21 01:12:36 +000011382
Craig Toppered2e13d2012-01-22 19:15:14 +000011383 EVT ExtraEltVT = ExtraVT.getVectorElementType();
Craig Topperb6072642012-05-03 07:26:59 +000011384 unsigned ExtraNumElems = ExtraVT.getVectorNumElements();
Craig Toppered2e13d2012-01-22 19:15:14 +000011385 ExtraVT = EVT::getVectorVT(*DAG.getContext(), ExtraEltVT,
11386 ExtraNumElems/2);
11387 SDValue Extra = DAG.getValueType(ExtraVT);
Craig Toppera124f942011-11-21 01:12:36 +000011388
Craig Toppered2e13d2012-01-22 19:15:14 +000011389 LHS1 = DAG.getNode(Op.getOpcode(), dl, NewVT, LHS1, Extra);
11390 LHS2 = DAG.getNode(Op.getOpcode(), dl, NewVT, LHS2, Extra);
Craig Toppera124f942011-11-21 01:12:36 +000011391
Dmitri Gribenko2de05722012-09-10 21:26:47 +000011392 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT, LHS1, LHS2);
Craig Toppered2e13d2012-01-22 19:15:14 +000011393 }
11394 // fall through
11395 case MVT::v4i32:
11396 case MVT::v8i16: {
11397 SDValue Tmp1 = getTargetVShiftNode(X86ISD::VSHLI, dl, VT,
11398 Op.getOperand(0), ShAmt, DAG);
11399 return getTargetVShiftNode(X86ISD::VSRAI, dl, VT, Tmp1, ShAmt, DAG);
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000011400 }
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000011401 }
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000011402}
11403
11404
Craig Topper55b24052012-09-11 06:15:32 +000011405static SDValue LowerMEMBARRIER(SDValue Op, const X86Subtarget *Subtarget,
11406 SelectionDAG &DAG) {
Eric Christopher9a9d2752010-07-22 02:48:34 +000011407 DebugLoc dl = Op.getDebugLoc();
Michael J. Spencerec38de22010-10-10 22:04:20 +000011408
Eric Christopher77ed1352011-07-08 00:04:56 +000011409 // Go ahead and emit the fence on x86-64 even if we asked for no-sse2.
11410 // There isn't any reason to disable it if the target processor supports it.
Craig Topper1accb7e2012-01-10 06:54:16 +000011411 if (!Subtarget->hasSSE2() && !Subtarget->is64Bit()) {
Eric Christopherc0b2a202010-08-14 21:51:50 +000011412 SDValue Chain = Op.getOperand(0);
Eric Christopher77ed1352011-07-08 00:04:56 +000011413 SDValue Zero = DAG.getConstant(0, MVT::i32);
Eric Christopherc0b2a202010-08-14 21:51:50 +000011414 SDValue Ops[] = {
11415 DAG.getRegister(X86::ESP, MVT::i32), // Base
11416 DAG.getTargetConstant(1, MVT::i8), // Scale
11417 DAG.getRegister(0, MVT::i32), // Index
11418 DAG.getTargetConstant(0, MVT::i32), // Disp
11419 DAG.getRegister(0, MVT::i32), // Segment.
11420 Zero,
11421 Chain
11422 };
Michael J. Spencerec38de22010-10-10 22:04:20 +000011423 SDNode *Res =
Eric Christopherc0b2a202010-08-14 21:51:50 +000011424 DAG.getMachineNode(X86::OR32mrLocked, dl, MVT::Other, Ops,
11425 array_lengthof(Ops));
11426 return SDValue(Res, 0);
Eric Christopherb6729dc2010-08-04 23:03:04 +000011427 }
Michael J. Spencerec38de22010-10-10 22:04:20 +000011428
Eric Christopher9a9d2752010-07-22 02:48:34 +000011429 unsigned isDev = cast<ConstantSDNode>(Op.getOperand(5))->getZExtValue();
Chris Lattner132929a2010-08-14 17:26:09 +000011430 if (!isDev)
Eric Christopher9a9d2752010-07-22 02:48:34 +000011431 return DAG.getNode(X86ISD::MEMBARRIER, dl, MVT::Other, Op.getOperand(0));
Michael J. Spencerec38de22010-10-10 22:04:20 +000011432
Chris Lattner132929a2010-08-14 17:26:09 +000011433 unsigned Op1 = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
11434 unsigned Op2 = cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue();
11435 unsigned Op3 = cast<ConstantSDNode>(Op.getOperand(3))->getZExtValue();
11436 unsigned Op4 = cast<ConstantSDNode>(Op.getOperand(4))->getZExtValue();
Michael J. Spencerec38de22010-10-10 22:04:20 +000011437
Chris Lattner132929a2010-08-14 17:26:09 +000011438 // def : Pat<(membarrier (i8 0), (i8 0), (i8 0), (i8 1), (i8 1)), (SFENCE)>;
11439 if (!Op1 && !Op2 && !Op3 && Op4)
11440 return DAG.getNode(X86ISD::SFENCE, dl, MVT::Other, Op.getOperand(0));
Michael J. Spencerec38de22010-10-10 22:04:20 +000011441
Chris Lattner132929a2010-08-14 17:26:09 +000011442 // def : Pat<(membarrier (i8 1), (i8 0), (i8 0), (i8 0), (i8 1)), (LFENCE)>;
11443 if (Op1 && !Op2 && !Op3 && !Op4)
11444 return DAG.getNode(X86ISD::LFENCE, dl, MVT::Other, Op.getOperand(0));
Michael J. Spencerec38de22010-10-10 22:04:20 +000011445
11446 // def : Pat<(membarrier (i8 imm), (i8 imm), (i8 imm), (i8 imm), (i8 1)),
Chris Lattner132929a2010-08-14 17:26:09 +000011447 // (MFENCE)>;
11448 return DAG.getNode(X86ISD::MFENCE, dl, MVT::Other, Op.getOperand(0));
Eric Christopher9a9d2752010-07-22 02:48:34 +000011449}
11450
Craig Topper55b24052012-09-11 06:15:32 +000011451static SDValue LowerATOMIC_FENCE(SDValue Op, const X86Subtarget *Subtarget,
11452 SelectionDAG &DAG) {
Eli Friedman14648462011-07-27 22:21:52 +000011453 DebugLoc dl = Op.getDebugLoc();
11454 AtomicOrdering FenceOrdering = static_cast<AtomicOrdering>(
11455 cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue());
11456 SynchronizationScope FenceScope = static_cast<SynchronizationScope>(
11457 cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue());
11458
11459 // The only fence that needs an instruction is a sequentially-consistent
11460 // cross-thread fence.
11461 if (FenceOrdering == SequentiallyConsistent && FenceScope == CrossThread) {
11462 // Use mfence if we have SSE2 or we're on x86-64 (even if we asked for
11463 // no-sse2). There isn't any reason to disable it if the target processor
11464 // supports it.
Craig Topper1accb7e2012-01-10 06:54:16 +000011465 if (Subtarget->hasSSE2() || Subtarget->is64Bit())
Eli Friedman14648462011-07-27 22:21:52 +000011466 return DAG.getNode(X86ISD::MFENCE, dl, MVT::Other, Op.getOperand(0));
11467
11468 SDValue Chain = Op.getOperand(0);
11469 SDValue Zero = DAG.getConstant(0, MVT::i32);
11470 SDValue Ops[] = {
11471 DAG.getRegister(X86::ESP, MVT::i32), // Base
11472 DAG.getTargetConstant(1, MVT::i8), // Scale
11473 DAG.getRegister(0, MVT::i32), // Index
11474 DAG.getTargetConstant(0, MVT::i32), // Disp
11475 DAG.getRegister(0, MVT::i32), // Segment.
11476 Zero,
11477 Chain
11478 };
11479 SDNode *Res =
11480 DAG.getMachineNode(X86::OR32mrLocked, dl, MVT::Other, Ops,
11481 array_lengthof(Ops));
11482 return SDValue(Res, 0);
11483 }
11484
11485 // MEMBARRIER is a compiler barrier; it codegens to a no-op.
11486 return DAG.getNode(X86ISD::MEMBARRIER, dl, MVT::Other, Op.getOperand(0));
11487}
11488
11489
Craig Topper55b24052012-09-11 06:15:32 +000011490static SDValue LowerCMP_SWAP(SDValue Op, const X86Subtarget *Subtarget,
11491 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +000011492 EVT T = Op.getValueType();
Chris Lattner93c4a5b2010-09-21 23:59:42 +000011493 DebugLoc DL = Op.getDebugLoc();
Andrew Lenhartha76e2f02008-03-04 21:13:33 +000011494 unsigned Reg = 0;
11495 unsigned size = 0;
Owen Anderson825b72b2009-08-11 20:47:22 +000011496 switch(T.getSimpleVT().SimpleTy) {
Craig Topperabb94d02012-02-05 03:43:23 +000011497 default: llvm_unreachable("Invalid value type!");
Owen Anderson825b72b2009-08-11 20:47:22 +000011498 case MVT::i8: Reg = X86::AL; size = 1; break;
11499 case MVT::i16: Reg = X86::AX; size = 2; break;
11500 case MVT::i32: Reg = X86::EAX; size = 4; break;
11501 case MVT::i64:
Duncan Sands1607f052008-12-01 11:39:25 +000011502 assert(Subtarget->is64Bit() && "Node not type legal!");
11503 Reg = X86::RAX; size = 8;
Andrew Lenharthd19189e2008-03-05 01:15:49 +000011504 break;
Bill Wendling61edeb52008-12-02 01:06:39 +000011505 }
Chris Lattner93c4a5b2010-09-21 23:59:42 +000011506 SDValue cpIn = DAG.getCopyToReg(Op.getOperand(0), DL, Reg,
Dale Johannesend18a4622008-09-11 03:12:59 +000011507 Op.getOperand(2), SDValue());
Dan Gohman475871a2008-07-27 21:46:04 +000011508 SDValue Ops[] = { cpIn.getValue(0),
Evan Cheng8a186ae2008-09-24 23:26:36 +000011509 Op.getOperand(1),
11510 Op.getOperand(3),
Owen Anderson825b72b2009-08-11 20:47:22 +000011511 DAG.getTargetConstant(size, MVT::i8),
Evan Cheng8a186ae2008-09-24 23:26:36 +000011512 cpIn.getValue(1) };
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +000011513 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Chris Lattner93c4a5b2010-09-21 23:59:42 +000011514 MachineMemOperand *MMO = cast<AtomicSDNode>(Op)->getMemOperand();
11515 SDValue Result = DAG.getMemIntrinsicNode(X86ISD::LCMPXCHG_DAG, DL, Tys,
11516 Ops, 5, T, MMO);
Scott Michelfdc40a02009-02-17 22:15:04 +000011517 SDValue cpOut =
Chris Lattner93c4a5b2010-09-21 23:59:42 +000011518 DAG.getCopyFromReg(Result.getValue(0), DL, Reg, T, Result.getValue(1));
Andrew Lenharth26ed8692008-03-01 21:52:34 +000011519 return cpOut;
11520}
11521
Craig Topper55b24052012-09-11 06:15:32 +000011522static SDValue LowerREADCYCLECOUNTER(SDValue Op, const X86Subtarget *Subtarget,
11523 SelectionDAG &DAG) {
Duncan Sands1607f052008-12-01 11:39:25 +000011524 assert(Subtarget->is64Bit() && "Result not type legalized?");
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +000011525 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Duncan Sands1607f052008-12-01 11:39:25 +000011526 SDValue TheChain = Op.getOperand(0);
Dale Johannesen6f38cb62009-02-07 19:59:05 +000011527 DebugLoc dl = Op.getDebugLoc();
Dale Johannesene4d209d2009-02-03 20:21:25 +000011528 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, dl, Tys, &TheChain, 1);
Owen Anderson825b72b2009-08-11 20:47:22 +000011529 SDValue rax = DAG.getCopyFromReg(rd, dl, X86::RAX, MVT::i64, rd.getValue(1));
11530 SDValue rdx = DAG.getCopyFromReg(rax.getValue(1), dl, X86::RDX, MVT::i64,
Duncan Sands1607f052008-12-01 11:39:25 +000011531 rax.getValue(2));
Owen Anderson825b72b2009-08-11 20:47:22 +000011532 SDValue Tmp = DAG.getNode(ISD::SHL, dl, MVT::i64, rdx,
11533 DAG.getConstant(32, MVT::i8));
Duncan Sands1607f052008-12-01 11:39:25 +000011534 SDValue Ops[] = {
Owen Anderson825b72b2009-08-11 20:47:22 +000011535 DAG.getNode(ISD::OR, dl, MVT::i64, rax, Tmp),
Duncan Sands1607f052008-12-01 11:39:25 +000011536 rdx.getValue(1)
11537 };
Dale Johannesene4d209d2009-02-03 20:21:25 +000011538 return DAG.getMergeValues(Ops, 2, dl);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011539}
11540
Craig Topper55b24052012-09-11 06:15:32 +000011541SDValue X86TargetLowering::LowerBITCAST(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen7d07b482010-05-21 00:52:33 +000011542 EVT SrcVT = Op.getOperand(0).getValueType();
11543 EVT DstVT = Op.getValueType();
Craig Topper1accb7e2012-01-10 06:54:16 +000011544 assert(Subtarget->is64Bit() && !Subtarget->hasSSE2() &&
Chris Lattner2a786eb2010-12-19 20:19:20 +000011545 Subtarget->hasMMX() && "Unexpected custom BITCAST");
Michael J. Spencerec38de22010-10-10 22:04:20 +000011546 assert((DstVT == MVT::i64 ||
Dale Johannesen7d07b482010-05-21 00:52:33 +000011547 (DstVT.isVector() && DstVT.getSizeInBits()==64)) &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +000011548 "Unexpected custom BITCAST");
Dale Johannesen7d07b482010-05-21 00:52:33 +000011549 // i64 <=> MMX conversions are Legal.
11550 if (SrcVT==MVT::i64 && DstVT.isVector())
11551 return Op;
11552 if (DstVT==MVT::i64 && SrcVT.isVector())
11553 return Op;
Dale Johannesene39859a2010-05-21 18:40:15 +000011554 // MMX <=> MMX conversions are Legal.
11555 if (SrcVT.isVector() && DstVT.isVector())
11556 return Op;
Dale Johannesen7d07b482010-05-21 00:52:33 +000011557 // All other conversions need to be expanded.
11558 return SDValue();
11559}
Chris Lattner5b856542010-12-20 00:59:46 +000011560
Craig Topper55b24052012-09-11 06:15:32 +000011561static SDValue LowerLOAD_SUB(SDValue Op, SelectionDAG &DAG) {
Dale Johannesen71d1bf52008-09-29 22:25:26 +000011562 SDNode *Node = Op.getNode();
Dale Johannesene4d209d2009-02-03 20:21:25 +000011563 DebugLoc dl = Node->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +000011564 EVT T = Node->getValueType(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +000011565 SDValue negOp = DAG.getNode(ISD::SUB, dl, T,
Evan Cheng242b38b2009-02-23 09:03:22 +000011566 DAG.getConstant(0, T), Node->getOperand(2));
Dale Johannesene4d209d2009-02-03 20:21:25 +000011567 return DAG.getAtomic(ISD::ATOMIC_LOAD_ADD, dl,
Dan Gohman0b1d4a72008-12-23 21:37:04 +000011568 cast<AtomicSDNode>(Node)->getMemoryVT(),
Dale Johannesen71d1bf52008-09-29 22:25:26 +000011569 Node->getOperand(0),
11570 Node->getOperand(1), negOp,
11571 cast<AtomicSDNode>(Node)->getSrcValue(),
Eli Friedman55ba8162011-07-29 03:05:32 +000011572 cast<AtomicSDNode>(Node)->getAlignment(),
11573 cast<AtomicSDNode>(Node)->getOrdering(),
11574 cast<AtomicSDNode>(Node)->getSynchScope());
Mon P Wang63307c32008-05-05 19:05:59 +000011575}
11576
Eli Friedman327236c2011-08-24 20:50:09 +000011577static SDValue LowerATOMIC_STORE(SDValue Op, SelectionDAG &DAG) {
11578 SDNode *Node = Op.getNode();
11579 DebugLoc dl = Node->getDebugLoc();
Eli Friedmanf8f90f02011-08-24 22:33:28 +000011580 EVT VT = cast<AtomicSDNode>(Node)->getMemoryVT();
Eli Friedman327236c2011-08-24 20:50:09 +000011581
11582 // Convert seq_cst store -> xchg
Eli Friedmanf8f90f02011-08-24 22:33:28 +000011583 // Convert wide store -> swap (-> cmpxchg8b/cmpxchg16b)
11584 // FIXME: On 32-bit, store -> fist or movq would be more efficient
11585 // (The only way to get a 16-byte store is cmpxchg16b)
11586 // FIXME: 16-byte ATOMIC_SWAP isn't actually hooked up at the moment.
11587 if (cast<AtomicSDNode>(Node)->getOrdering() == SequentiallyConsistent ||
11588 !DAG.getTargetLoweringInfo().isTypeLegal(VT)) {
Eli Friedman4317fe12011-08-24 21:17:30 +000011589 SDValue Swap = DAG.getAtomic(ISD::ATOMIC_SWAP, dl,
11590 cast<AtomicSDNode>(Node)->getMemoryVT(),
11591 Node->getOperand(0),
11592 Node->getOperand(1), Node->getOperand(2),
Eli Friedmanf8f90f02011-08-24 22:33:28 +000011593 cast<AtomicSDNode>(Node)->getMemOperand(),
Eli Friedman4317fe12011-08-24 21:17:30 +000011594 cast<AtomicSDNode>(Node)->getOrdering(),
11595 cast<AtomicSDNode>(Node)->getSynchScope());
Eli Friedman327236c2011-08-24 20:50:09 +000011596 return Swap.getValue(1);
11597 }
11598 // Other atomic stores have a simple pattern.
11599 return Op;
11600}
11601
Chris Lattner5b856542010-12-20 00:59:46 +000011602static SDValue LowerADDC_ADDE_SUBC_SUBE(SDValue Op, SelectionDAG &DAG) {
11603 EVT VT = Op.getNode()->getValueType(0);
11604
11605 // Let legalize expand this if it isn't a legal type yet.
11606 if (!DAG.getTargetLoweringInfo().isTypeLegal(VT))
11607 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011608
Chris Lattner5b856542010-12-20 00:59:46 +000011609 SDVTList VTs = DAG.getVTList(VT, MVT::i32);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011610
Chris Lattner5b856542010-12-20 00:59:46 +000011611 unsigned Opc;
11612 bool ExtraOp = false;
11613 switch (Op.getOpcode()) {
Craig Topperabb94d02012-02-05 03:43:23 +000011614 default: llvm_unreachable("Invalid code");
Chris Lattner5b856542010-12-20 00:59:46 +000011615 case ISD::ADDC: Opc = X86ISD::ADD; break;
11616 case ISD::ADDE: Opc = X86ISD::ADC; ExtraOp = true; break;
11617 case ISD::SUBC: Opc = X86ISD::SUB; break;
11618 case ISD::SUBE: Opc = X86ISD::SBB; ExtraOp = true; break;
11619 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011620
Chris Lattner5b856542010-12-20 00:59:46 +000011621 if (!ExtraOp)
11622 return DAG.getNode(Opc, Op->getDebugLoc(), VTs, Op.getOperand(0),
11623 Op.getOperand(1));
11624 return DAG.getNode(Opc, Op->getDebugLoc(), VTs, Op.getOperand(0),
11625 Op.getOperand(1), Op.getOperand(2));
11626}
11627
Evan Cheng0db9fe62006-04-25 20:13:52 +000011628/// LowerOperation - Provide custom lowering hooks for some operations.
11629///
Dan Gohmand858e902010-04-17 15:26:15 +000011630SDValue X86TargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +000011631 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +000011632 default: llvm_unreachable("Should not custom lower this!");
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000011633 case ISD::SIGN_EXTEND_INREG: return LowerSIGN_EXTEND_INREG(Op,DAG);
Craig Topper55b24052012-09-11 06:15:32 +000011634 case ISD::MEMBARRIER: return LowerMEMBARRIER(Op, Subtarget, DAG);
11635 case ISD::ATOMIC_FENCE: return LowerATOMIC_FENCE(Op, Subtarget, DAG);
11636 case ISD::ATOMIC_CMP_SWAP: return LowerCMP_SWAP(Op, Subtarget, DAG);
Dan Gohman0b1d4a72008-12-23 21:37:04 +000011637 case ISD::ATOMIC_LOAD_SUB: return LowerLOAD_SUB(Op,DAG);
Eli Friedman327236c2011-08-24 20:50:09 +000011638 case ISD::ATOMIC_STORE: return LowerATOMIC_STORE(Op,DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000011639 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
Mon P Wangeb38ebf2010-01-24 00:05:03 +000011640 case ISD::CONCAT_VECTORS: return LowerCONCAT_VECTORS(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000011641 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
11642 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
11643 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
Craig Topper55b24052012-09-11 06:15:32 +000011644 case ISD::EXTRACT_SUBVECTOR: return LowerEXTRACT_SUBVECTOR(Op,Subtarget,DAG);
11645 case ISD::INSERT_SUBVECTOR: return LowerINSERT_SUBVECTOR(Op, Subtarget,DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000011646 case ISD::SCALAR_TO_VECTOR: return LowerSCALAR_TO_VECTOR(Op, DAG);
11647 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
11648 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +000011649 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Bill Wendling056292f2008-09-16 21:48:12 +000011650 case ISD::ExternalSymbol: return LowerExternalSymbol(Op, DAG);
Dan Gohmanf705adb2009-10-30 01:28:02 +000011651 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000011652 case ISD::SHL_PARTS:
11653 case ISD::SRA_PARTS:
Nadav Rotem43012222011-05-11 08:12:09 +000011654 case ISD::SRL_PARTS: return LowerShiftParts(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000011655 case ISD::SINT_TO_FP: return LowerSINT_TO_FP(Op, DAG);
Dale Johannesen1c15bf52008-10-21 20:50:01 +000011656 case ISD::UINT_TO_FP: return LowerUINT_TO_FP(Op, DAG);
Michael Liaobedcbd42012-10-16 18:14:11 +000011657 case ISD::TRUNCATE: return lowerTRUNCATE(Op, DAG);
Michael Liaoa7554632012-10-23 17:36:08 +000011658 case ISD::ZERO_EXTEND: return lowerZERO_EXTEND(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000011659 case ISD::FP_TO_SINT: return LowerFP_TO_SINT(Op, DAG);
Eli Friedman948e95a2009-05-23 09:59:16 +000011660 case ISD::FP_TO_UINT: return LowerFP_TO_UINT(Op, DAG);
Michael Liao9d796db2012-10-10 16:32:15 +000011661 case ISD::FP_EXTEND: return lowerFP_EXTEND(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000011662 case ISD::FABS: return LowerFABS(Op, DAG);
11663 case ISD::FNEG: return LowerFNEG(Op, DAG);
Evan Cheng68c47cb2007-01-05 07:55:56 +000011664 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Stuart Hastings4fd0dee2011-06-01 04:39:42 +000011665 case ISD::FGETSIGN: return LowerFGETSIGN(Op, DAG);
Evan Chenge5f62042007-09-29 00:00:36 +000011666 case ISD::SETCC: return LowerSETCC(Op, DAG);
11667 case ISD::SELECT: return LowerSELECT(Op, DAG);
11668 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000011669 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000011670 case ISD::VASTART: return LowerVASTART(Op, DAG);
Dan Gohman9018e832008-05-10 01:26:14 +000011671 case ISD::VAARG: return LowerVAARG(Op, DAG);
Craig Topper55b24052012-09-11 06:15:32 +000011672 case ISD::VACOPY: return LowerVACOPY(Op, Subtarget, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000011673 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
Benjamin Kramerb9bee042012-07-12 09:31:43 +000011674 case ISD::INTRINSIC_W_CHAIN: return LowerINTRINSIC_W_CHAIN(Op, DAG);
Nate Begemanbcc5f362007-01-29 22:58:52 +000011675 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
11676 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Anton Korobeynikov2365f512007-07-14 14:06:15 +000011677 case ISD::FRAME_TO_ARGS_OFFSET:
11678 return LowerFRAME_TO_ARGS_OFFSET(Op, DAG);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +000011679 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
Anton Korobeynikov2365f512007-07-14 14:06:15 +000011680 case ISD::EH_RETURN: return LowerEH_RETURN(Op, DAG);
Michael Liao6c0e04c2012-10-15 22:39:43 +000011681 case ISD::EH_SJLJ_SETJMP: return lowerEH_SJLJ_SETJMP(Op, DAG);
11682 case ISD::EH_SJLJ_LONGJMP: return lowerEH_SJLJ_LONGJMP(Op, DAG);
Duncan Sands4a544a72011-09-06 13:37:06 +000011683 case ISD::INIT_TRAMPOLINE: return LowerINIT_TRAMPOLINE(Op, DAG);
11684 case ISD::ADJUST_TRAMPOLINE: return LowerADJUST_TRAMPOLINE(Op, DAG);
Dan Gohman1a024862008-01-31 00:41:03 +000011685 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
Evan Cheng18efe262007-12-14 02:13:44 +000011686 case ISD::CTLZ: return LowerCTLZ(Op, DAG);
Chandler Carruthacc068e2011-12-24 10:55:54 +000011687 case ISD::CTLZ_ZERO_UNDEF: return LowerCTLZ_ZERO_UNDEF(Op, DAG);
Evan Cheng18efe262007-12-14 02:13:44 +000011688 case ISD::CTTZ: return LowerCTTZ(Op, DAG);
Craig Topper55b24052012-09-11 06:15:32 +000011689 case ISD::MUL: return LowerMUL(Op, Subtarget, DAG);
Nadav Rotem43012222011-05-11 08:12:09 +000011690 case ISD::SRA:
11691 case ISD::SRL:
11692 case ISD::SHL: return LowerShift(Op, DAG);
Bill Wendling74c37652008-12-09 22:08:41 +000011693 case ISD::SADDO:
11694 case ISD::UADDO:
11695 case ISD::SSUBO:
11696 case ISD::USUBO:
11697 case ISD::SMULO:
11698 case ISD::UMULO: return LowerXALUO(Op, DAG);
Craig Topper55b24052012-09-11 06:15:32 +000011699 case ISD::READCYCLECOUNTER: return LowerREADCYCLECOUNTER(Op, Subtarget,DAG);
Wesley Peckbf17cfa2010-11-23 03:31:01 +000011700 case ISD::BITCAST: return LowerBITCAST(Op, DAG);
Chris Lattner5b856542010-12-20 00:59:46 +000011701 case ISD::ADDC:
11702 case ISD::ADDE:
11703 case ISD::SUBC:
11704 case ISD::SUBE: return LowerADDC_ADDE_SUBC_SUBE(Op, DAG);
Craig Topper13894fa2011-08-24 06:14:18 +000011705 case ISD::ADD: return LowerADD(Op, DAG);
11706 case ISD::SUB: return LowerSUB(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000011707 }
Chris Lattner27a6c732007-11-24 07:07:01 +000011708}
11709
Eli Friedmanf8f90f02011-08-24 22:33:28 +000011710static void ReplaceATOMIC_LOAD(SDNode *Node,
11711 SmallVectorImpl<SDValue> &Results,
11712 SelectionDAG &DAG) {
11713 DebugLoc dl = Node->getDebugLoc();
11714 EVT VT = cast<AtomicSDNode>(Node)->getMemoryVT();
11715
11716 // Convert wide load -> cmpxchg8b/cmpxchg16b
11717 // FIXME: On 32-bit, load -> fild or movq would be more efficient
11718 // (The only way to get a 16-byte load is cmpxchg16b)
11719 // FIXME: 16-byte ATOMIC_CMP_SWAP isn't actually hooked up at the moment.
Benjamin Kramer2753ae32011-08-27 17:36:14 +000011720 SDValue Zero = DAG.getConstant(0, VT);
11721 SDValue Swap = DAG.getAtomic(ISD::ATOMIC_CMP_SWAP, dl, VT,
Eli Friedmanf8f90f02011-08-24 22:33:28 +000011722 Node->getOperand(0),
11723 Node->getOperand(1), Zero, Zero,
11724 cast<AtomicSDNode>(Node)->getMemOperand(),
11725 cast<AtomicSDNode>(Node)->getOrdering(),
11726 cast<AtomicSDNode>(Node)->getSynchScope());
11727 Results.push_back(Swap.getValue(0));
11728 Results.push_back(Swap.getValue(1));
11729}
11730
Craig Topperc0878702012-08-17 06:55:11 +000011731static void
Duncan Sands1607f052008-12-01 11:39:25 +000011732ReplaceATOMIC_BINARY_64(SDNode *Node, SmallVectorImpl<SDValue>&Results,
Craig Topperc0878702012-08-17 06:55:11 +000011733 SelectionDAG &DAG, unsigned NewOp) {
Dale Johannesene4d209d2009-02-03 20:21:25 +000011734 DebugLoc dl = Node->getDebugLoc();
Duncan Sands17001ce2011-10-18 12:44:00 +000011735 assert (Node->getValueType(0) == MVT::i64 &&
11736 "Only know how to expand i64 atomics");
Duncan Sands1607f052008-12-01 11:39:25 +000011737
11738 SDValue Chain = Node->getOperand(0);
11739 SDValue In1 = Node->getOperand(1);
Owen Anderson825b72b2009-08-11 20:47:22 +000011740 SDValue In2L = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +000011741 Node->getOperand(2), DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +000011742 SDValue In2H = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +000011743 Node->getOperand(2), DAG.getIntPtrConstant(1));
Dan Gohmanc76909a2009-09-25 20:36:54 +000011744 SDValue Ops[] = { Chain, In1, In2L, In2H };
Owen Anderson825b72b2009-08-11 20:47:22 +000011745 SDVTList Tys = DAG.getVTList(MVT::i32, MVT::i32, MVT::Other);
Dan Gohmanc76909a2009-09-25 20:36:54 +000011746 SDValue Result =
11747 DAG.getMemIntrinsicNode(NewOp, dl, Tys, Ops, 4, MVT::i64,
11748 cast<MemSDNode>(Node)->getMemOperand());
Duncan Sands1607f052008-12-01 11:39:25 +000011749 SDValue OpsF[] = { Result.getValue(0), Result.getValue(1)};
Owen Anderson825b72b2009-08-11 20:47:22 +000011750 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, OpsF, 2));
Duncan Sands1607f052008-12-01 11:39:25 +000011751 Results.push_back(Result.getValue(2));
11752}
11753
Duncan Sands126d9072008-07-04 11:47:58 +000011754/// ReplaceNodeResults - Replace a node with an illegal result type
11755/// with a new node built out of custom code.
Duncan Sands1607f052008-12-01 11:39:25 +000011756void X86TargetLowering::ReplaceNodeResults(SDNode *N,
11757 SmallVectorImpl<SDValue>&Results,
Dan Gohmand858e902010-04-17 15:26:15 +000011758 SelectionDAG &DAG) const {
Dale Johannesene4d209d2009-02-03 20:21:25 +000011759 DebugLoc dl = N->getDebugLoc();
Chris Lattner27a6c732007-11-24 07:07:01 +000011760 switch (N->getOpcode()) {
Duncan Sandsed294c42008-10-20 15:56:33 +000011761 default:
Craig Topperabb94d02012-02-05 03:43:23 +000011762 llvm_unreachable("Do not know how to custom type legalize this operation!");
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000011763 case ISD::SIGN_EXTEND_INREG:
Chris Lattner5b856542010-12-20 00:59:46 +000011764 case ISD::ADDC:
11765 case ISD::ADDE:
11766 case ISD::SUBC:
11767 case ISD::SUBE:
11768 // We don't want to expand or promote these.
11769 return;
Michael J. Spencer1a2d0612012-02-24 19:01:22 +000011770 case ISD::FP_TO_SINT:
11771 case ISD::FP_TO_UINT: {
11772 bool IsSigned = N->getOpcode() == ISD::FP_TO_SINT;
11773
11774 if (!IsSigned && !isIntegerTypeFTOL(SDValue(N, 0).getValueType()))
11775 return;
11776
Eli Friedman948e95a2009-05-23 09:59:16 +000011777 std::pair<SDValue,SDValue> Vals =
NAKAMURA Takumi9a68fdc2012-02-25 03:37:25 +000011778 FP_TO_INTHelper(SDValue(N, 0), DAG, IsSigned, /*IsReplace=*/ true);
Duncan Sands1607f052008-12-01 11:39:25 +000011779 SDValue FIST = Vals.first, StackSlot = Vals.second;
11780 if (FIST.getNode() != 0) {
Owen Andersone50ed302009-08-10 22:56:29 +000011781 EVT VT = N->getValueType(0);
Duncan Sands1607f052008-12-01 11:39:25 +000011782 // Return a load from the stack slot.
Michael J. Spencer1a2d0612012-02-24 19:01:22 +000011783 if (StackSlot.getNode() != 0)
11784 Results.push_back(DAG.getLoad(VT, dl, FIST, StackSlot,
11785 MachinePointerInfo(),
11786 false, false, false, 0));
11787 else
11788 Results.push_back(FIST);
Duncan Sands1607f052008-12-01 11:39:25 +000011789 }
11790 return;
11791 }
Michael Liao991b6a22012-10-24 04:09:32 +000011792 case ISD::UINT_TO_FP: {
11793 if (N->getOperand(0).getValueType() != MVT::v2i32 &&
11794 N->getValueType(0) != MVT::v2f32)
11795 return;
11796 SDValue ZExtIn = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v2i64,
11797 N->getOperand(0));
11798 SDValue Bias = DAG.getConstantFP(BitsToDouble(0x4330000000000000ULL),
11799 MVT::f64);
11800 SDValue VBias = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v2f64, Bias, Bias);
11801 SDValue Or = DAG.getNode(ISD::OR, dl, MVT::v2i64, ZExtIn,
11802 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64, VBias));
11803 Or = DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Or);
11804 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::v2f64, Or, VBias);
11805 Results.push_back(DAG.getNode(X86ISD::VFPROUND, dl, MVT::v4f32, Sub));
11806 return;
11807 }
Michael Liao44c2d612012-10-10 16:53:28 +000011808 case ISD::FP_ROUND: {
11809 SDValue V = DAG.getNode(X86ISD::VFPROUND, dl, MVT::v4f32, N->getOperand(0));
11810 Results.push_back(V);
11811 return;
11812 }
Duncan Sands1607f052008-12-01 11:39:25 +000011813 case ISD::READCYCLECOUNTER: {
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +000011814 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Duncan Sands1607f052008-12-01 11:39:25 +000011815 SDValue TheChain = N->getOperand(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +000011816 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, dl, Tys, &TheChain, 1);
Owen Anderson825b72b2009-08-11 20:47:22 +000011817 SDValue eax = DAG.getCopyFromReg(rd, dl, X86::EAX, MVT::i32,
Dale Johannesendd64c412009-02-04 00:33:20 +000011818 rd.getValue(1));
Owen Anderson825b72b2009-08-11 20:47:22 +000011819 SDValue edx = DAG.getCopyFromReg(eax.getValue(1), dl, X86::EDX, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +000011820 eax.getValue(2));
11821 // Use a buildpair to merge the two 32-bit values into a 64-bit one.
11822 SDValue Ops[] = { eax, edx };
Owen Anderson825b72b2009-08-11 20:47:22 +000011823 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Ops, 2));
Duncan Sands1607f052008-12-01 11:39:25 +000011824 Results.push_back(edx.getValue(1));
11825 return;
11826 }
Dan Gohman0b1d4a72008-12-23 21:37:04 +000011827 case ISD::ATOMIC_CMP_SWAP: {
Owen Andersone50ed302009-08-10 22:56:29 +000011828 EVT T = N->getValueType(0);
Benjamin Kramer2753ae32011-08-27 17:36:14 +000011829 assert((T == MVT::i64 || T == MVT::i128) && "can only expand cmpxchg pair");
Eli Friedman43f51ae2011-08-26 21:21:21 +000011830 bool Regs64bit = T == MVT::i128;
11831 EVT HalfT = Regs64bit ? MVT::i64 : MVT::i32;
Duncan Sands1607f052008-12-01 11:39:25 +000011832 SDValue cpInL, cpInH;
Eli Friedman43f51ae2011-08-26 21:21:21 +000011833 cpInL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, HalfT, N->getOperand(2),
11834 DAG.getConstant(0, HalfT));
11835 cpInH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, HalfT, N->getOperand(2),
11836 DAG.getConstant(1, HalfT));
11837 cpInL = DAG.getCopyToReg(N->getOperand(0), dl,
11838 Regs64bit ? X86::RAX : X86::EAX,
11839 cpInL, SDValue());
11840 cpInH = DAG.getCopyToReg(cpInL.getValue(0), dl,
11841 Regs64bit ? X86::RDX : X86::EDX,
11842 cpInH, cpInL.getValue(1));
Duncan Sands1607f052008-12-01 11:39:25 +000011843 SDValue swapInL, swapInH;
Eli Friedman43f51ae2011-08-26 21:21:21 +000011844 swapInL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, HalfT, N->getOperand(3),
11845 DAG.getConstant(0, HalfT));
11846 swapInH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, HalfT, N->getOperand(3),
11847 DAG.getConstant(1, HalfT));
11848 swapInL = DAG.getCopyToReg(cpInH.getValue(0), dl,
11849 Regs64bit ? X86::RBX : X86::EBX,
11850 swapInL, cpInH.getValue(1));
11851 swapInH = DAG.getCopyToReg(swapInL.getValue(0), dl,
Chad Rosiera20e1e72012-08-01 18:39:17 +000011852 Regs64bit ? X86::RCX : X86::ECX,
Eli Friedman43f51ae2011-08-26 21:21:21 +000011853 swapInH, swapInL.getValue(1));
Duncan Sands1607f052008-12-01 11:39:25 +000011854 SDValue Ops[] = { swapInH.getValue(0),
11855 N->getOperand(1),
11856 swapInH.getValue(1) };
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +000011857 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Andrew Trick1a2cf3b2010-10-11 19:02:04 +000011858 MachineMemOperand *MMO = cast<AtomicSDNode>(N)->getMemOperand();
Eli Friedman43f51ae2011-08-26 21:21:21 +000011859 unsigned Opcode = Regs64bit ? X86ISD::LCMPXCHG16_DAG :
11860 X86ISD::LCMPXCHG8_DAG;
11861 SDValue Result = DAG.getMemIntrinsicNode(Opcode, dl, Tys,
Andrew Trick1a2cf3b2010-10-11 19:02:04 +000011862 Ops, 3, T, MMO);
Eli Friedman43f51ae2011-08-26 21:21:21 +000011863 SDValue cpOutL = DAG.getCopyFromReg(Result.getValue(0), dl,
11864 Regs64bit ? X86::RAX : X86::EAX,
11865 HalfT, Result.getValue(1));
11866 SDValue cpOutH = DAG.getCopyFromReg(cpOutL.getValue(1), dl,
11867 Regs64bit ? X86::RDX : X86::EDX,
11868 HalfT, cpOutL.getValue(2));
Duncan Sands1607f052008-12-01 11:39:25 +000011869 SDValue OpsF[] = { cpOutL.getValue(0), cpOutH.getValue(0)};
Eli Friedman43f51ae2011-08-26 21:21:21 +000011870 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, T, OpsF, 2));
Duncan Sands1607f052008-12-01 11:39:25 +000011871 Results.push_back(cpOutH.getValue(1));
11872 return;
11873 }
Dan Gohman0b1d4a72008-12-23 21:37:04 +000011874 case ISD::ATOMIC_LOAD_ADD:
Dan Gohman0b1d4a72008-12-23 21:37:04 +000011875 case ISD::ATOMIC_LOAD_AND:
Dan Gohman0b1d4a72008-12-23 21:37:04 +000011876 case ISD::ATOMIC_LOAD_NAND:
Dan Gohman0b1d4a72008-12-23 21:37:04 +000011877 case ISD::ATOMIC_LOAD_OR:
Dan Gohman0b1d4a72008-12-23 21:37:04 +000011878 case ISD::ATOMIC_LOAD_SUB:
Dan Gohman0b1d4a72008-12-23 21:37:04 +000011879 case ISD::ATOMIC_LOAD_XOR:
Michael Liaoe5e8f762012-09-25 18:08:13 +000011880 case ISD::ATOMIC_LOAD_MAX:
11881 case ISD::ATOMIC_LOAD_MIN:
11882 case ISD::ATOMIC_LOAD_UMAX:
11883 case ISD::ATOMIC_LOAD_UMIN:
Craig Topperc0878702012-08-17 06:55:11 +000011884 case ISD::ATOMIC_SWAP: {
11885 unsigned Opc;
11886 switch (N->getOpcode()) {
11887 default: llvm_unreachable("Unexpected opcode");
11888 case ISD::ATOMIC_LOAD_ADD:
11889 Opc = X86ISD::ATOMADD64_DAG;
11890 break;
11891 case ISD::ATOMIC_LOAD_AND:
11892 Opc = X86ISD::ATOMAND64_DAG;
11893 break;
11894 case ISD::ATOMIC_LOAD_NAND:
11895 Opc = X86ISD::ATOMNAND64_DAG;
11896 break;
11897 case ISD::ATOMIC_LOAD_OR:
11898 Opc = X86ISD::ATOMOR64_DAG;
11899 break;
11900 case ISD::ATOMIC_LOAD_SUB:
11901 Opc = X86ISD::ATOMSUB64_DAG;
11902 break;
11903 case ISD::ATOMIC_LOAD_XOR:
11904 Opc = X86ISD::ATOMXOR64_DAG;
11905 break;
Michael Liaoe5e8f762012-09-25 18:08:13 +000011906 case ISD::ATOMIC_LOAD_MAX:
11907 Opc = X86ISD::ATOMMAX64_DAG;
11908 break;
11909 case ISD::ATOMIC_LOAD_MIN:
11910 Opc = X86ISD::ATOMMIN64_DAG;
11911 break;
11912 case ISD::ATOMIC_LOAD_UMAX:
11913 Opc = X86ISD::ATOMUMAX64_DAG;
11914 break;
11915 case ISD::ATOMIC_LOAD_UMIN:
11916 Opc = X86ISD::ATOMUMIN64_DAG;
11917 break;
Craig Topperc0878702012-08-17 06:55:11 +000011918 case ISD::ATOMIC_SWAP:
11919 Opc = X86ISD::ATOMSWAP64_DAG;
11920 break;
11921 }
11922 ReplaceATOMIC_BINARY_64(N, Results, DAG, Opc);
Duncan Sands1607f052008-12-01 11:39:25 +000011923 return;
Craig Topperc0878702012-08-17 06:55:11 +000011924 }
Eli Friedmanf8f90f02011-08-24 22:33:28 +000011925 case ISD::ATOMIC_LOAD:
11926 ReplaceATOMIC_LOAD(N, Results, DAG);
Chris Lattner27a6c732007-11-24 07:07:01 +000011927 }
Evan Cheng0db9fe62006-04-25 20:13:52 +000011928}
11929
Evan Cheng72261582005-12-20 06:22:03 +000011930const char *X86TargetLowering::getTargetNodeName(unsigned Opcode) const {
11931 switch (Opcode) {
11932 default: return NULL;
Evan Cheng18efe262007-12-14 02:13:44 +000011933 case X86ISD::BSF: return "X86ISD::BSF";
11934 case X86ISD::BSR: return "X86ISD::BSR";
Evan Chenge3413162006-01-09 18:33:28 +000011935 case X86ISD::SHLD: return "X86ISD::SHLD";
11936 case X86ISD::SHRD: return "X86ISD::SHRD";
Evan Chengef6ffb12006-01-31 03:14:29 +000011937 case X86ISD::FAND: return "X86ISD::FAND";
Evan Cheng68c47cb2007-01-05 07:55:56 +000011938 case X86ISD::FOR: return "X86ISD::FOR";
Evan Cheng223547a2006-01-31 22:28:30 +000011939 case X86ISD::FXOR: return "X86ISD::FXOR";
Evan Cheng68c47cb2007-01-05 07:55:56 +000011940 case X86ISD::FSRL: return "X86ISD::FSRL";
Evan Chenga3195e82006-01-12 22:54:21 +000011941 case X86ISD::FILD: return "X86ISD::FILD";
Evan Chenge3de85b2006-02-04 02:20:30 +000011942 case X86ISD::FILD_FLAG: return "X86ISD::FILD_FLAG";
Evan Cheng72261582005-12-20 06:22:03 +000011943 case X86ISD::FP_TO_INT16_IN_MEM: return "X86ISD::FP_TO_INT16_IN_MEM";
11944 case X86ISD::FP_TO_INT32_IN_MEM: return "X86ISD::FP_TO_INT32_IN_MEM";
11945 case X86ISD::FP_TO_INT64_IN_MEM: return "X86ISD::FP_TO_INT64_IN_MEM";
Evan Chengb077b842005-12-21 02:39:21 +000011946 case X86ISD::FLD: return "X86ISD::FLD";
Evan Chengd90eb7f2006-01-05 00:27:02 +000011947 case X86ISD::FST: return "X86ISD::FST";
Evan Cheng72261582005-12-20 06:22:03 +000011948 case X86ISD::CALL: return "X86ISD::CALL";
Evan Cheng72261582005-12-20 06:22:03 +000011949 case X86ISD::RDTSC_DAG: return "X86ISD::RDTSC_DAG";
Dan Gohmanc7a37d42008-12-23 22:45:23 +000011950 case X86ISD::BT: return "X86ISD::BT";
Evan Cheng72261582005-12-20 06:22:03 +000011951 case X86ISD::CMP: return "X86ISD::CMP";
Evan Cheng6be2c582006-04-05 23:38:46 +000011952 case X86ISD::COMI: return "X86ISD::COMI";
11953 case X86ISD::UCOMI: return "X86ISD::UCOMI";
Evan Chengd5781fc2005-12-21 20:21:51 +000011954 case X86ISD::SETCC: return "X86ISD::SETCC";
Evan Chengad9c0a32009-12-15 00:53:42 +000011955 case X86ISD::SETCC_CARRY: return "X86ISD::SETCC_CARRY";
Stuart Hastings865f0932011-06-03 23:53:54 +000011956 case X86ISD::FSETCCsd: return "X86ISD::FSETCCsd";
11957 case X86ISD::FSETCCss: return "X86ISD::FSETCCss";
Evan Cheng72261582005-12-20 06:22:03 +000011958 case X86ISD::CMOV: return "X86ISD::CMOV";
11959 case X86ISD::BRCOND: return "X86ISD::BRCOND";
Evan Chengb077b842005-12-21 02:39:21 +000011960 case X86ISD::RET_FLAG: return "X86ISD::RET_FLAG";
Evan Cheng8df346b2006-03-04 01:12:00 +000011961 case X86ISD::REP_STOS: return "X86ISD::REP_STOS";
11962 case X86ISD::REP_MOVS: return "X86ISD::REP_MOVS";
Evan Cheng7ccced62006-02-18 00:15:05 +000011963 case X86ISD::GlobalBaseReg: return "X86ISD::GlobalBaseReg";
Evan Cheng020d2e82006-02-23 20:41:18 +000011964 case X86ISD::Wrapper: return "X86ISD::Wrapper";
Chris Lattner18c59872009-06-27 04:16:01 +000011965 case X86ISD::WrapperRIP: return "X86ISD::WrapperRIP";
Nate Begeman14d12ca2008-02-11 04:19:36 +000011966 case X86ISD::PEXTRB: return "X86ISD::PEXTRB";
Evan Chengb067a1e2006-03-31 19:22:53 +000011967 case X86ISD::PEXTRW: return "X86ISD::PEXTRW";
Nate Begeman14d12ca2008-02-11 04:19:36 +000011968 case X86ISD::INSERTPS: return "X86ISD::INSERTPS";
11969 case X86ISD::PINSRB: return "X86ISD::PINSRB";
Evan Cheng653159f2006-03-31 21:55:24 +000011970 case X86ISD::PINSRW: return "X86ISD::PINSRW";
Nate Begemanb9a47b82009-02-23 08:49:38 +000011971 case X86ISD::PSHUFB: return "X86ISD::PSHUFB";
Bruno Cardoso Lopesc1af4772011-07-13 21:36:47 +000011972 case X86ISD::ANDNP: return "X86ISD::ANDNP";
Craig Topper31133842011-11-19 07:33:10 +000011973 case X86ISD::PSIGN: return "X86ISD::PSIGN";
Craig Toppere6a62772011-11-13 17:31:07 +000011974 case X86ISD::BLENDV: return "X86ISD::BLENDV";
Nadav Roteme6113782012-04-11 06:40:27 +000011975 case X86ISD::BLENDPW: return "X86ISD::BLENDPW";
11976 case X86ISD::BLENDPS: return "X86ISD::BLENDPS";
11977 case X86ISD::BLENDPD: return "X86ISD::BLENDPD";
Craig Topperfe033152011-12-06 09:31:36 +000011978 case X86ISD::HADD: return "X86ISD::HADD";
11979 case X86ISD::HSUB: return "X86ISD::HSUB";
Craig Toppere6a62772011-11-13 17:31:07 +000011980 case X86ISD::FHADD: return "X86ISD::FHADD";
11981 case X86ISD::FHSUB: return "X86ISD::FHSUB";
Evan Cheng8ca29322006-11-10 21:43:37 +000011982 case X86ISD::FMAX: return "X86ISD::FMAX";
11983 case X86ISD::FMIN: return "X86ISD::FMIN";
Nadav Rotemd60cb112012-08-19 13:06:16 +000011984 case X86ISD::FMAXC: return "X86ISD::FMAXC";
11985 case X86ISD::FMINC: return "X86ISD::FMINC";
Dan Gohman20382522007-07-10 00:05:58 +000011986 case X86ISD::FRSQRT: return "X86ISD::FRSQRT";
11987 case X86ISD::FRCP: return "X86ISD::FRCP";
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +000011988 case X86ISD::TLSADDR: return "X86ISD::TLSADDR";
Hans Wennborgf0234fc2012-06-01 16:27:21 +000011989 case X86ISD::TLSBASEADDR: return "X86ISD::TLSBASEADDR";
Eric Christopher30ef0e52010-06-03 04:07:48 +000011990 case X86ISD::TLSCALL: return "X86ISD::TLSCALL";
Michael Liao6c0e04c2012-10-15 22:39:43 +000011991 case X86ISD::EH_SJLJ_SETJMP: return "X86ISD::EH_SJLJ_SETJMP";
11992 case X86ISD::EH_SJLJ_LONGJMP: return "X86ISD::EH_SJLJ_LONGJMP";
Anton Korobeynikov2365f512007-07-14 14:06:15 +000011993 case X86ISD::EH_RETURN: return "X86ISD::EH_RETURN";
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +000011994 case X86ISD::TC_RETURN: return "X86ISD::TC_RETURN";
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000011995 case X86ISD::FNSTCW16m: return "X86ISD::FNSTCW16m";
Benjamin Kramer17c836c2012-04-27 12:07:43 +000011996 case X86ISD::FNSTSW16r: return "X86ISD::FNSTSW16r";
Evan Cheng7e2ff772008-05-08 00:57:18 +000011997 case X86ISD::LCMPXCHG_DAG: return "X86ISD::LCMPXCHG_DAG";
11998 case X86ISD::LCMPXCHG8_DAG: return "X86ISD::LCMPXCHG8_DAG";
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011999 case X86ISD::ATOMADD64_DAG: return "X86ISD::ATOMADD64_DAG";
12000 case X86ISD::ATOMSUB64_DAG: return "X86ISD::ATOMSUB64_DAG";
12001 case X86ISD::ATOMOR64_DAG: return "X86ISD::ATOMOR64_DAG";
12002 case X86ISD::ATOMXOR64_DAG: return "X86ISD::ATOMXOR64_DAG";
12003 case X86ISD::ATOMAND64_DAG: return "X86ISD::ATOMAND64_DAG";
12004 case X86ISD::ATOMNAND64_DAG: return "X86ISD::ATOMNAND64_DAG";
Evan Chengd880b972008-05-09 21:53:03 +000012005 case X86ISD::VZEXT_MOVL: return "X86ISD::VZEXT_MOVL";
Michael Liaob7bf7262012-08-14 22:53:17 +000012006 case X86ISD::VSEXT_MOVL: return "X86ISD::VSEXT_MOVL";
Evan Chengd880b972008-05-09 21:53:03 +000012007 case X86ISD::VZEXT_LOAD: return "X86ISD::VZEXT_LOAD";
Michael Liaod9d09602012-10-23 17:34:00 +000012008 case X86ISD::VZEXT: return "X86ISD::VZEXT";
12009 case X86ISD::VSEXT: return "X86ISD::VSEXT";
Michael Liao7091b242012-08-14 21:24:47 +000012010 case X86ISD::VFPEXT: return "X86ISD::VFPEXT";
Michael Liao44c2d612012-10-10 16:53:28 +000012011 case X86ISD::VFPROUND: return "X86ISD::VFPROUND";
Craig Toppered2e13d2012-01-22 19:15:14 +000012012 case X86ISD::VSHLDQ: return "X86ISD::VSHLDQ";
12013 case X86ISD::VSRLDQ: return "X86ISD::VSRLDQ";
Evan Chengf26ffe92008-05-29 08:22:04 +000012014 case X86ISD::VSHL: return "X86ISD::VSHL";
12015 case X86ISD::VSRL: return "X86ISD::VSRL";
Craig Toppered2e13d2012-01-22 19:15:14 +000012016 case X86ISD::VSRA: return "X86ISD::VSRA";
12017 case X86ISD::VSHLI: return "X86ISD::VSHLI";
12018 case X86ISD::VSRLI: return "X86ISD::VSRLI";
12019 case X86ISD::VSRAI: return "X86ISD::VSRAI";
Craig Topper1906d322012-01-22 23:36:02 +000012020 case X86ISD::CMPP: return "X86ISD::CMPP";
Craig Topper67609fd2012-01-22 22:42:16 +000012021 case X86ISD::PCMPEQ: return "X86ISD::PCMPEQ";
12022 case X86ISD::PCMPGT: return "X86ISD::PCMPGT";
Bill Wendlingab55ebd2008-12-12 00:56:36 +000012023 case X86ISD::ADD: return "X86ISD::ADD";
12024 case X86ISD::SUB: return "X86ISD::SUB";
Chris Lattner5b856542010-12-20 00:59:46 +000012025 case X86ISD::ADC: return "X86ISD::ADC";
12026 case X86ISD::SBB: return "X86ISD::SBB";
Bill Wendlingd350e022008-12-12 21:15:41 +000012027 case X86ISD::SMUL: return "X86ISD::SMUL";
12028 case X86ISD::UMUL: return "X86ISD::UMUL";
Dan Gohman076aee32009-03-04 19:44:21 +000012029 case X86ISD::INC: return "X86ISD::INC";
12030 case X86ISD::DEC: return "X86ISD::DEC";
Dan Gohmane220c4b2009-09-18 19:59:53 +000012031 case X86ISD::OR: return "X86ISD::OR";
12032 case X86ISD::XOR: return "X86ISD::XOR";
12033 case X86ISD::AND: return "X86ISD::AND";
Craig Topper54a11172011-10-14 07:06:56 +000012034 case X86ISD::ANDN: return "X86ISD::ANDN";
Craig Toppere6a62772011-11-13 17:31:07 +000012035 case X86ISD::BLSI: return "X86ISD::BLSI";
12036 case X86ISD::BLSMSK: return "X86ISD::BLSMSK";
12037 case X86ISD::BLSR: return "X86ISD::BLSR";
Evan Cheng73f24c92009-03-30 21:36:47 +000012038 case X86ISD::MUL_IMM: return "X86ISD::MUL_IMM";
Eric Christopher71c67532009-07-29 00:28:05 +000012039 case X86ISD::PTEST: return "X86ISD::PTEST";
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +000012040 case X86ISD::TESTP: return "X86ISD::TESTP";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000012041 case X86ISD::PALIGN: return "X86ISD::PALIGN";
12042 case X86ISD::PSHUFD: return "X86ISD::PSHUFD";
12043 case X86ISD::PSHUFHW: return "X86ISD::PSHUFHW";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000012044 case X86ISD::PSHUFLW: return "X86ISD::PSHUFLW";
Craig Topperb3982da2011-12-31 23:50:21 +000012045 case X86ISD::SHUFP: return "X86ISD::SHUFP";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000012046 case X86ISD::MOVLHPS: return "X86ISD::MOVLHPS";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000012047 case X86ISD::MOVLHPD: return "X86ISD::MOVLHPD";
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +000012048 case X86ISD::MOVHLPS: return "X86ISD::MOVHLPS";
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +000012049 case X86ISD::MOVLPS: return "X86ISD::MOVLPS";
12050 case X86ISD::MOVLPD: return "X86ISD::MOVLPD";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000012051 case X86ISD::MOVDDUP: return "X86ISD::MOVDDUP";
12052 case X86ISD::MOVSHDUP: return "X86ISD::MOVSHDUP";
12053 case X86ISD::MOVSLDUP: return "X86ISD::MOVSLDUP";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000012054 case X86ISD::MOVSD: return "X86ISD::MOVSD";
12055 case X86ISD::MOVSS: return "X86ISD::MOVSS";
Craig Topper34671b82011-12-06 08:21:25 +000012056 case X86ISD::UNPCKL: return "X86ISD::UNPCKL";
12057 case X86ISD::UNPCKH: return "X86ISD::UNPCKH";
Bruno Cardoso Lopes0e6d2302011-08-17 02:29:19 +000012058 case X86ISD::VBROADCAST: return "X86ISD::VBROADCAST";
Craig Topper316cd2a2011-11-30 06:25:25 +000012059 case X86ISD::VPERMILP: return "X86ISD::VPERMILP";
Craig Topperec24e612011-11-30 07:47:51 +000012060 case X86ISD::VPERM2X128: return "X86ISD::VPERM2X128";
Craig Topper8325c112012-04-16 00:41:45 +000012061 case X86ISD::VPERMV: return "X86ISD::VPERMV";
12062 case X86ISD::VPERMI: return "X86ISD::VPERMI";
Craig Topper5b209e82012-02-05 03:14:49 +000012063 case X86ISD::PMULUDQ: return "X86ISD::PMULUDQ";
Dan Gohmand6708ea2009-08-15 01:38:56 +000012064 case X86ISD::VASTART_SAVE_XMM_REGS: return "X86ISD::VASTART_SAVE_XMM_REGS";
Dan Gohman320afb82010-10-12 18:00:49 +000012065 case X86ISD::VAARG_64: return "X86ISD::VAARG_64";
Michael J. Spencere9c253e2010-10-21 01:41:01 +000012066 case X86ISD::WIN_ALLOCA: return "X86ISD::WIN_ALLOCA";
Eli Friedman14648462011-07-27 22:21:52 +000012067 case X86ISD::MEMBARRIER: return "X86ISD::MEMBARRIER";
Rafael Espindolad07b7ec2011-08-30 19:43:21 +000012068 case X86ISD::SEG_ALLOCA: return "X86ISD::SEG_ALLOCA";
Michael J. Spencer1a2d0612012-02-24 19:01:22 +000012069 case X86ISD::WIN_FTOL: return "X86ISD::WIN_FTOL";
Benjamin Kramer17c836c2012-04-27 12:07:43 +000012070 case X86ISD::SAHF: return "X86ISD::SAHF";
Benjamin Kramerb9bee042012-07-12 09:31:43 +000012071 case X86ISD::RDRAND: return "X86ISD::RDRAND";
Elena Demikhovsky1503aba2012-08-01 12:06:00 +000012072 case X86ISD::FMADD: return "X86ISD::FMADD";
12073 case X86ISD::FMSUB: return "X86ISD::FMSUB";
12074 case X86ISD::FNMADD: return "X86ISD::FNMADD";
12075 case X86ISD::FNMSUB: return "X86ISD::FNMSUB";
12076 case X86ISD::FMADDSUB: return "X86ISD::FMADDSUB";
12077 case X86ISD::FMSUBADD: return "X86ISD::FMSUBADD";
Craig Topper9c7ae012012-11-10 01:23:36 +000012078 case X86ISD::PCMPESTRI: return "X86ISD::PCMPESTRI";
12079 case X86ISD::PCMPISTRI: return "X86ISD::PCMPISTRI";
Evan Cheng72261582005-12-20 06:22:03 +000012080 }
12081}
Evan Cheng3a03ebb2005-12-21 23:05:39 +000012082
Chris Lattnerc9addb72007-03-30 23:15:24 +000012083// isLegalAddressingMode - Return true if the addressing mode represented
12084// by AM is legal for this target, for a load/store of the specified type.
Scott Michelfdc40a02009-02-17 22:15:04 +000012085bool X86TargetLowering::isLegalAddressingMode(const AddrMode &AM,
Chris Lattnerdb125cf2011-07-18 04:54:35 +000012086 Type *Ty) const {
Chris Lattnerc9addb72007-03-30 23:15:24 +000012087 // X86 supports extremely general addressing modes.
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000012088 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman92b651f2010-08-24 15:55:12 +000012089 Reloc::Model R = getTargetMachine().getRelocationModel();
Scott Michelfdc40a02009-02-17 22:15:04 +000012090
Chris Lattnerc9addb72007-03-30 23:15:24 +000012091 // X86 allows a sign-extended 32-bit immediate field as a displacement.
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000012092 if (!X86::isOffsetSuitableForCodeModel(AM.BaseOffs, M, AM.BaseGV != NULL))
Chris Lattnerc9addb72007-03-30 23:15:24 +000012093 return false;
Scott Michelfdc40a02009-02-17 22:15:04 +000012094
Chris Lattnerc9addb72007-03-30 23:15:24 +000012095 if (AM.BaseGV) {
Chris Lattnerdfed4132009-07-10 07:38:24 +000012096 unsigned GVFlags =
12097 Subtarget->ClassifyGlobalReference(AM.BaseGV, getTargetMachine());
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000012098
Chris Lattnerdfed4132009-07-10 07:38:24 +000012099 // If a reference to this global requires an extra load, we can't fold it.
12100 if (isGlobalStubReference(GVFlags))
Chris Lattnerc9addb72007-03-30 23:15:24 +000012101 return false;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000012102
Chris Lattnerdfed4132009-07-10 07:38:24 +000012103 // If BaseGV requires a register for the PIC base, we cannot also have a
12104 // BaseReg specified.
12105 if (AM.HasBaseReg && isGlobalRelativeToPICBase(GVFlags))
Dale Johannesen203af582008-12-05 21:47:27 +000012106 return false;
Evan Cheng52787842007-08-01 23:46:47 +000012107
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000012108 // If lower 4G is not available, then we must use rip-relative addressing.
Dan Gohman92b651f2010-08-24 15:55:12 +000012109 if ((M != CodeModel::Small || R != Reloc::Static) &&
12110 Subtarget->is64Bit() && (AM.BaseOffs || AM.Scale > 1))
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000012111 return false;
Chris Lattnerc9addb72007-03-30 23:15:24 +000012112 }
Scott Michelfdc40a02009-02-17 22:15:04 +000012113
Chris Lattnerc9addb72007-03-30 23:15:24 +000012114 switch (AM.Scale) {
12115 case 0:
12116 case 1:
12117 case 2:
12118 case 4:
12119 case 8:
12120 // These scales always work.
12121 break;
12122 case 3:
12123 case 5:
12124 case 9:
12125 // These scales are formed with basereg+scalereg. Only accept if there is
12126 // no basereg yet.
12127 if (AM.HasBaseReg)
12128 return false;
12129 break;
12130 default: // Other stuff never works.
12131 return false;
12132 }
Scott Michelfdc40a02009-02-17 22:15:04 +000012133
Chris Lattnerc9addb72007-03-30 23:15:24 +000012134 return true;
12135}
12136
12137
Chris Lattnerdb125cf2011-07-18 04:54:35 +000012138bool X86TargetLowering::isTruncateFree(Type *Ty1, Type *Ty2) const {
Duncan Sandsb0bc6c32010-02-15 16:12:20 +000012139 if (!Ty1->isIntegerTy() || !Ty2->isIntegerTy())
Evan Cheng2bd122c2007-10-26 01:56:11 +000012140 return false;
Evan Chenge127a732007-10-29 07:57:50 +000012141 unsigned NumBits1 = Ty1->getPrimitiveSizeInBits();
12142 unsigned NumBits2 = Ty2->getPrimitiveSizeInBits();
Evan Cheng260e07e2008-03-20 02:18:41 +000012143 if (NumBits1 <= NumBits2)
Evan Chenge127a732007-10-29 07:57:50 +000012144 return false;
Dan Gohman377fbc02010-02-25 03:04:36 +000012145 return true;
Evan Cheng2bd122c2007-10-26 01:56:11 +000012146}
12147
Evan Cheng70e10d32012-07-17 06:53:39 +000012148bool X86TargetLowering::isLegalICmpImmediate(int64_t Imm) const {
12149 return Imm == (int32_t)Imm;
12150}
12151
12152bool X86TargetLowering::isLegalAddImmediate(int64_t Imm) const {
Evan Chenga9e13ba2012-07-17 18:54:11 +000012153 // Can also use sub to handle negated immediates.
Evan Cheng70e10d32012-07-17 06:53:39 +000012154 return Imm == (int32_t)Imm;
12155}
12156
Owen Andersone50ed302009-08-10 22:56:29 +000012157bool X86TargetLowering::isTruncateFree(EVT VT1, EVT VT2) const {
Duncan Sands83ec4b62008-06-06 12:08:01 +000012158 if (!VT1.isInteger() || !VT2.isInteger())
Evan Cheng3c3ddb32007-10-29 19:58:20 +000012159 return false;
Duncan Sands83ec4b62008-06-06 12:08:01 +000012160 unsigned NumBits1 = VT1.getSizeInBits();
12161 unsigned NumBits2 = VT2.getSizeInBits();
Evan Cheng260e07e2008-03-20 02:18:41 +000012162 if (NumBits1 <= NumBits2)
Evan Cheng3c3ddb32007-10-29 19:58:20 +000012163 return false;
Dan Gohman377fbc02010-02-25 03:04:36 +000012164 return true;
Evan Cheng3c3ddb32007-10-29 19:58:20 +000012165}
Evan Cheng2bd122c2007-10-26 01:56:11 +000012166
Chris Lattnerdb125cf2011-07-18 04:54:35 +000012167bool X86TargetLowering::isZExtFree(Type *Ty1, Type *Ty2) const {
Dan Gohman349ba492009-04-09 02:06:09 +000012168 // x86-64 implicitly zero-extends 32-bit results in 64-bit registers.
Duncan Sandsb0bc6c32010-02-15 16:12:20 +000012169 return Ty1->isIntegerTy(32) && Ty2->isIntegerTy(64) && Subtarget->is64Bit();
Dan Gohman97121ba2009-04-08 00:15:30 +000012170}
12171
Owen Andersone50ed302009-08-10 22:56:29 +000012172bool X86TargetLowering::isZExtFree(EVT VT1, EVT VT2) const {
Dan Gohman349ba492009-04-09 02:06:09 +000012173 // x86-64 implicitly zero-extends 32-bit results in 64-bit registers.
Owen Anderson825b72b2009-08-11 20:47:22 +000012174 return VT1 == MVT::i32 && VT2 == MVT::i64 && Subtarget->is64Bit();
Dan Gohman97121ba2009-04-08 00:15:30 +000012175}
12176
Owen Andersone50ed302009-08-10 22:56:29 +000012177bool X86TargetLowering::isNarrowingProfitable(EVT VT1, EVT VT2) const {
Evan Cheng8b944d32009-05-28 00:35:15 +000012178 // i16 instructions are longer (0x66 prefix) and potentially slower.
Owen Anderson825b72b2009-08-11 20:47:22 +000012179 return !(VT1 == MVT::i32 && VT2 == MVT::i16);
Evan Cheng8b944d32009-05-28 00:35:15 +000012180}
12181
Evan Cheng60c07e12006-07-05 22:17:51 +000012182/// isShuffleMaskLegal - Targets can use this to indicate that they only
12183/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
12184/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
12185/// are assumed to be legal.
12186bool
Eric Christopherfd179292009-08-27 18:07:15 +000012187X86TargetLowering::isShuffleMaskLegal(const SmallVectorImpl<int> &M,
Owen Andersone50ed302009-08-10 22:56:29 +000012188 EVT VT) const {
Eric Christophercff6f852010-04-15 01:40:20 +000012189 // Very little shuffling can be done for 64-bit vectors right now.
Nate Begeman9008ca62009-04-27 18:41:29 +000012190 if (VT.getSizeInBits() == 64)
Craig Topper1dc0fbc2011-12-05 07:27:14 +000012191 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +000012192
Nate Begemana09008b2009-10-19 02:17:23 +000012193 // FIXME: pshufb, blends, shifts.
Nate Begeman9008ca62009-04-27 18:41:29 +000012194 return (VT.getVectorNumElements() == 2 ||
12195 ShuffleVectorSDNode::isSplatMask(&M[0], VT) ||
12196 isMOVLMask(M, VT) ||
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000012197 isSHUFPMask(M, VT, Subtarget->hasFp256()) ||
Nate Begeman9008ca62009-04-27 18:41:29 +000012198 isPSHUFDMask(M, VT) ||
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000012199 isPSHUFHWMask(M, VT, Subtarget->hasInt256()) ||
12200 isPSHUFLWMask(M, VT, Subtarget->hasInt256()) ||
Craig Topper0e2037b2012-01-20 05:53:00 +000012201 isPALIGNRMask(M, VT, Subtarget) ||
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000012202 isUNPCKLMask(M, VT, Subtarget->hasInt256()) ||
12203 isUNPCKHMask(M, VT, Subtarget->hasInt256()) ||
12204 isUNPCKL_v_undef_Mask(M, VT, Subtarget->hasInt256()) ||
12205 isUNPCKH_v_undef_Mask(M, VT, Subtarget->hasInt256()));
Evan Cheng60c07e12006-07-05 22:17:51 +000012206}
12207
Dan Gohman7d8143f2008-04-09 20:09:42 +000012208bool
Nate Begeman5a5ca152009-04-29 05:20:52 +000012209X86TargetLowering::isVectorClearMaskLegal(const SmallVectorImpl<int> &Mask,
Owen Andersone50ed302009-08-10 22:56:29 +000012210 EVT VT) const {
Nate Begeman9008ca62009-04-27 18:41:29 +000012211 unsigned NumElts = VT.getVectorNumElements();
12212 // FIXME: This collection of masks seems suspect.
12213 if (NumElts == 2)
12214 return true;
Craig Topper7a9a28b2012-08-12 02:23:29 +000012215 if (NumElts == 4 && VT.is128BitVector()) {
Nate Begeman9008ca62009-04-27 18:41:29 +000012216 return (isMOVLMask(Mask, VT) ||
12217 isCommutedMOVLMask(Mask, VT, true) ||
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000012218 isSHUFPMask(Mask, VT, Subtarget->hasFp256()) ||
12219 isSHUFPMask(Mask, VT, Subtarget->hasFp256(), /* Commuted */ true));
Evan Cheng60c07e12006-07-05 22:17:51 +000012220 }
12221 return false;
12222}
12223
12224//===----------------------------------------------------------------------===//
12225// X86 Scheduler Hooks
12226//===----------------------------------------------------------------------===//
12227
Michael Liaobe02a902012-11-08 07:28:54 +000012228/// Utility function to emit xbegin specifying the start of an RTM region.
Craig Topper2da36912012-11-11 22:45:02 +000012229static MachineBasicBlock *EmitXBegin(MachineInstr *MI, MachineBasicBlock *MBB,
12230 const TargetInstrInfo *TII) {
Michael Liaobe02a902012-11-08 07:28:54 +000012231 DebugLoc DL = MI->getDebugLoc();
Michael Liaobe02a902012-11-08 07:28:54 +000012232
12233 const BasicBlock *BB = MBB->getBasicBlock();
12234 MachineFunction::iterator I = MBB;
12235 ++I;
12236
12237 // For the v = xbegin(), we generate
12238 //
12239 // thisMBB:
12240 // xbegin sinkMBB
12241 //
12242 // mainMBB:
12243 // eax = -1
12244 //
12245 // sinkMBB:
12246 // v = eax
12247
12248 MachineBasicBlock *thisMBB = MBB;
12249 MachineFunction *MF = MBB->getParent();
12250 MachineBasicBlock *mainMBB = MF->CreateMachineBasicBlock(BB);
12251 MachineBasicBlock *sinkMBB = MF->CreateMachineBasicBlock(BB);
12252 MF->insert(I, mainMBB);
12253 MF->insert(I, sinkMBB);
12254
12255 // Transfer the remainder of BB and its successor edges to sinkMBB.
12256 sinkMBB->splice(sinkMBB->begin(), MBB,
12257 llvm::next(MachineBasicBlock::iterator(MI)), MBB->end());
12258 sinkMBB->transferSuccessorsAndUpdatePHIs(MBB);
12259
12260 // thisMBB:
12261 // xbegin sinkMBB
12262 // # fallthrough to mainMBB
12263 // # abortion to sinkMBB
12264 BuildMI(thisMBB, DL, TII->get(X86::XBEGIN_4)).addMBB(sinkMBB);
12265 thisMBB->addSuccessor(mainMBB);
12266 thisMBB->addSuccessor(sinkMBB);
12267
12268 // mainMBB:
12269 // EAX = -1
12270 BuildMI(mainMBB, DL, TII->get(X86::MOV32ri), X86::EAX).addImm(-1);
12271 mainMBB->addSuccessor(sinkMBB);
12272
12273 // sinkMBB:
12274 // EAX is live into the sinkMBB
12275 sinkMBB->addLiveIn(X86::EAX);
12276 BuildMI(*sinkMBB, sinkMBB->begin(), DL,
12277 TII->get(TargetOpcode::COPY), MI->getOperand(0).getReg())
12278 .addReg(X86::EAX);
12279
12280 MI->eraseFromParent();
12281 return sinkMBB;
12282}
12283
Michael Liaob118a072012-09-20 03:06:15 +000012284// Get CMPXCHG opcode for the specified data type.
12285static unsigned getCmpXChgOpcode(EVT VT) {
12286 switch (VT.getSimpleVT().SimpleTy) {
12287 case MVT::i8: return X86::LCMPXCHG8;
12288 case MVT::i16: return X86::LCMPXCHG16;
12289 case MVT::i32: return X86::LCMPXCHG32;
12290 case MVT::i64: return X86::LCMPXCHG64;
12291 default:
12292 break;
Richard Smith42fc29e2012-04-13 22:47:00 +000012293 }
Michael Liaob118a072012-09-20 03:06:15 +000012294 llvm_unreachable("Invalid operand size!");
Mon P Wang63307c32008-05-05 19:05:59 +000012295}
12296
Michael Liaob118a072012-09-20 03:06:15 +000012297// Get LOAD opcode for the specified data type.
12298static unsigned getLoadOpcode(EVT VT) {
12299 switch (VT.getSimpleVT().SimpleTy) {
12300 case MVT::i8: return X86::MOV8rm;
12301 case MVT::i16: return X86::MOV16rm;
12302 case MVT::i32: return X86::MOV32rm;
12303 case MVT::i64: return X86::MOV64rm;
12304 default:
12305 break;
12306 }
12307 llvm_unreachable("Invalid operand size!");
12308}
12309
12310// Get opcode of the non-atomic one from the specified atomic instruction.
12311static unsigned getNonAtomicOpcode(unsigned Opc) {
12312 switch (Opc) {
12313 case X86::ATOMAND8: return X86::AND8rr;
12314 case X86::ATOMAND16: return X86::AND16rr;
12315 case X86::ATOMAND32: return X86::AND32rr;
12316 case X86::ATOMAND64: return X86::AND64rr;
12317 case X86::ATOMOR8: return X86::OR8rr;
12318 case X86::ATOMOR16: return X86::OR16rr;
12319 case X86::ATOMOR32: return X86::OR32rr;
12320 case X86::ATOMOR64: return X86::OR64rr;
12321 case X86::ATOMXOR8: return X86::XOR8rr;
12322 case X86::ATOMXOR16: return X86::XOR16rr;
12323 case X86::ATOMXOR32: return X86::XOR32rr;
12324 case X86::ATOMXOR64: return X86::XOR64rr;
12325 }
12326 llvm_unreachable("Unhandled atomic-load-op opcode!");
12327}
12328
12329// Get opcode of the non-atomic one from the specified atomic instruction with
12330// extra opcode.
12331static unsigned getNonAtomicOpcodeWithExtraOpc(unsigned Opc,
12332 unsigned &ExtraOpc) {
12333 switch (Opc) {
12334 case X86::ATOMNAND8: ExtraOpc = X86::NOT8r; return X86::AND8rr;
12335 case X86::ATOMNAND16: ExtraOpc = X86::NOT16r; return X86::AND16rr;
12336 case X86::ATOMNAND32: ExtraOpc = X86::NOT32r; return X86::AND32rr;
12337 case X86::ATOMNAND64: ExtraOpc = X86::NOT64r; return X86::AND64rr;
Michael Liaofe87c302012-09-21 03:18:52 +000012338 case X86::ATOMMAX8: ExtraOpc = X86::CMP8rr; return X86::CMOVL32rr;
Michael Liaob118a072012-09-20 03:06:15 +000012339 case X86::ATOMMAX16: ExtraOpc = X86::CMP16rr; return X86::CMOVL16rr;
12340 case X86::ATOMMAX32: ExtraOpc = X86::CMP32rr; return X86::CMOVL32rr;
12341 case X86::ATOMMAX64: ExtraOpc = X86::CMP64rr; return X86::CMOVL64rr;
Michael Liaofe87c302012-09-21 03:18:52 +000012342 case X86::ATOMMIN8: ExtraOpc = X86::CMP8rr; return X86::CMOVG32rr;
Michael Liaob118a072012-09-20 03:06:15 +000012343 case X86::ATOMMIN16: ExtraOpc = X86::CMP16rr; return X86::CMOVG16rr;
12344 case X86::ATOMMIN32: ExtraOpc = X86::CMP32rr; return X86::CMOVG32rr;
12345 case X86::ATOMMIN64: ExtraOpc = X86::CMP64rr; return X86::CMOVG64rr;
Michael Liaofe87c302012-09-21 03:18:52 +000012346 case X86::ATOMUMAX8: ExtraOpc = X86::CMP8rr; return X86::CMOVB32rr;
Michael Liaob118a072012-09-20 03:06:15 +000012347 case X86::ATOMUMAX16: ExtraOpc = X86::CMP16rr; return X86::CMOVB16rr;
12348 case X86::ATOMUMAX32: ExtraOpc = X86::CMP32rr; return X86::CMOVB32rr;
12349 case X86::ATOMUMAX64: ExtraOpc = X86::CMP64rr; return X86::CMOVB64rr;
Michael Liaofe87c302012-09-21 03:18:52 +000012350 case X86::ATOMUMIN8: ExtraOpc = X86::CMP8rr; return X86::CMOVA32rr;
Michael Liaob118a072012-09-20 03:06:15 +000012351 case X86::ATOMUMIN16: ExtraOpc = X86::CMP16rr; return X86::CMOVA16rr;
12352 case X86::ATOMUMIN32: ExtraOpc = X86::CMP32rr; return X86::CMOVA32rr;
12353 case X86::ATOMUMIN64: ExtraOpc = X86::CMP64rr; return X86::CMOVA64rr;
12354 }
12355 llvm_unreachable("Unhandled atomic-load-op opcode!");
12356}
12357
12358// Get opcode of the non-atomic one from the specified atomic instruction for
12359// 64-bit data type on 32-bit target.
12360static unsigned getNonAtomic6432Opcode(unsigned Opc, unsigned &HiOpc) {
12361 switch (Opc) {
12362 case X86::ATOMAND6432: HiOpc = X86::AND32rr; return X86::AND32rr;
12363 case X86::ATOMOR6432: HiOpc = X86::OR32rr; return X86::OR32rr;
12364 case X86::ATOMXOR6432: HiOpc = X86::XOR32rr; return X86::XOR32rr;
12365 case X86::ATOMADD6432: HiOpc = X86::ADC32rr; return X86::ADD32rr;
12366 case X86::ATOMSUB6432: HiOpc = X86::SBB32rr; return X86::SUB32rr;
12367 case X86::ATOMSWAP6432: HiOpc = X86::MOV32rr; return X86::MOV32rr;
Michael Liaoe5e8f762012-09-25 18:08:13 +000012368 case X86::ATOMMAX6432: HiOpc = X86::SETLr; return X86::SETLr;
12369 case X86::ATOMMIN6432: HiOpc = X86::SETGr; return X86::SETGr;
12370 case X86::ATOMUMAX6432: HiOpc = X86::SETBr; return X86::SETBr;
12371 case X86::ATOMUMIN6432: HiOpc = X86::SETAr; return X86::SETAr;
Michael Liaob118a072012-09-20 03:06:15 +000012372 }
12373 llvm_unreachable("Unhandled atomic-load-op opcode!");
12374}
12375
12376// Get opcode of the non-atomic one from the specified atomic instruction for
12377// 64-bit data type on 32-bit target with extra opcode.
12378static unsigned getNonAtomic6432OpcodeWithExtraOpc(unsigned Opc,
12379 unsigned &HiOpc,
12380 unsigned &ExtraOpc) {
12381 switch (Opc) {
12382 case X86::ATOMNAND6432:
12383 ExtraOpc = X86::NOT32r;
12384 HiOpc = X86::AND32rr;
12385 return X86::AND32rr;
12386 }
12387 llvm_unreachable("Unhandled atomic-load-op opcode!");
12388}
12389
12390// Get pseudo CMOV opcode from the specified data type.
12391static unsigned getPseudoCMOVOpc(EVT VT) {
12392 switch (VT.getSimpleVT().SimpleTy) {
Michael Liaofe87c302012-09-21 03:18:52 +000012393 case MVT::i8: return X86::CMOV_GR8;
Michael Liaob118a072012-09-20 03:06:15 +000012394 case MVT::i16: return X86::CMOV_GR16;
12395 case MVT::i32: return X86::CMOV_GR32;
12396 default:
12397 break;
12398 }
12399 llvm_unreachable("Unknown CMOV opcode!");
12400}
12401
12402// EmitAtomicLoadArith - emit the code sequence for pseudo atomic instructions.
12403// They will be translated into a spin-loop or compare-exchange loop from
12404//
12405// ...
12406// dst = atomic-fetch-op MI.addr, MI.val
12407// ...
12408//
12409// to
12410//
12411// ...
12412// EAX = LOAD MI.addr
12413// loop:
12414// t1 = OP MI.val, EAX
12415// LCMPXCHG [MI.addr], t1, [EAX is implicitly used & defined]
12416// JNE loop
12417// sink:
12418// dst = EAX
12419// ...
Mon P Wang63307c32008-05-05 19:05:59 +000012420MachineBasicBlock *
Michael Liaob118a072012-09-20 03:06:15 +000012421X86TargetLowering::EmitAtomicLoadArith(MachineInstr *MI,
12422 MachineBasicBlock *MBB) const {
12423 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
12424 DebugLoc DL = MI->getDebugLoc();
12425
12426 MachineFunction *MF = MBB->getParent();
12427 MachineRegisterInfo &MRI = MF->getRegInfo();
12428
12429 const BasicBlock *BB = MBB->getBasicBlock();
12430 MachineFunction::iterator I = MBB;
12431 ++I;
12432
12433 assert(MI->getNumOperands() <= X86::AddrNumOperands + 2 &&
12434 "Unexpected number of operands");
12435
12436 assert(MI->hasOneMemOperand() &&
12437 "Expected atomic-load-op to have one memoperand");
12438
12439 // Memory Reference
12440 MachineInstr::mmo_iterator MMOBegin = MI->memoperands_begin();
12441 MachineInstr::mmo_iterator MMOEnd = MI->memoperands_end();
12442
12443 unsigned DstReg, SrcReg;
12444 unsigned MemOpndSlot;
12445
12446 unsigned CurOp = 0;
12447
12448 DstReg = MI->getOperand(CurOp++).getReg();
12449 MemOpndSlot = CurOp;
12450 CurOp += X86::AddrNumOperands;
12451 SrcReg = MI->getOperand(CurOp++).getReg();
12452
12453 const TargetRegisterClass *RC = MRI.getRegClass(DstReg);
Craig Topperf4d25a22012-09-30 19:49:56 +000012454 MVT::SimpleValueType VT = *RC->vt_begin();
Michael Liaob118a072012-09-20 03:06:15 +000012455 unsigned AccPhyReg = getX86SubSuperRegister(X86::EAX, VT);
12456
12457 unsigned LCMPXCHGOpc = getCmpXChgOpcode(VT);
12458 unsigned LOADOpc = getLoadOpcode(VT);
12459
12460 // For the atomic load-arith operator, we generate
12461 //
12462 // thisMBB:
12463 // EAX = LOAD [MI.addr]
12464 // mainMBB:
12465 // t1 = OP MI.val, EAX
12466 // LCMPXCHG [MI.addr], t1, [EAX is implicitly used & defined]
12467 // JNE mainMBB
12468 // sinkMBB:
12469
12470 MachineBasicBlock *thisMBB = MBB;
12471 MachineBasicBlock *mainMBB = MF->CreateMachineBasicBlock(BB);
12472 MachineBasicBlock *sinkMBB = MF->CreateMachineBasicBlock(BB);
12473 MF->insert(I, mainMBB);
12474 MF->insert(I, sinkMBB);
12475
12476 MachineInstrBuilder MIB;
12477
12478 // Transfer the remainder of BB and its successor edges to sinkMBB.
12479 sinkMBB->splice(sinkMBB->begin(), MBB,
12480 llvm::next(MachineBasicBlock::iterator(MI)), MBB->end());
12481 sinkMBB->transferSuccessorsAndUpdatePHIs(MBB);
12482
12483 // thisMBB:
12484 MIB = BuildMI(thisMBB, DL, TII->get(LOADOpc), AccPhyReg);
12485 for (unsigned i = 0; i < X86::AddrNumOperands; ++i)
12486 MIB.addOperand(MI->getOperand(MemOpndSlot + i));
12487 MIB.setMemRefs(MMOBegin, MMOEnd);
12488
12489 thisMBB->addSuccessor(mainMBB);
12490
12491 // mainMBB:
12492 MachineBasicBlock *origMainMBB = mainMBB;
12493 mainMBB->addLiveIn(AccPhyReg);
12494
12495 // Copy AccPhyReg as it is used more than once.
12496 unsigned AccReg = MRI.createVirtualRegister(RC);
12497 BuildMI(mainMBB, DL, TII->get(TargetOpcode::COPY), AccReg)
12498 .addReg(AccPhyReg);
12499
12500 unsigned t1 = MRI.createVirtualRegister(RC);
12501 unsigned Opc = MI->getOpcode();
12502 switch (Opc) {
12503 default:
12504 llvm_unreachable("Unhandled atomic-load-op opcode!");
12505 case X86::ATOMAND8:
12506 case X86::ATOMAND16:
12507 case X86::ATOMAND32:
12508 case X86::ATOMAND64:
12509 case X86::ATOMOR8:
12510 case X86::ATOMOR16:
12511 case X86::ATOMOR32:
12512 case X86::ATOMOR64:
12513 case X86::ATOMXOR8:
12514 case X86::ATOMXOR16:
12515 case X86::ATOMXOR32:
12516 case X86::ATOMXOR64: {
12517 unsigned ARITHOpc = getNonAtomicOpcode(Opc);
12518 BuildMI(mainMBB, DL, TII->get(ARITHOpc), t1).addReg(SrcReg)
12519 .addReg(AccReg);
12520 break;
12521 }
12522 case X86::ATOMNAND8:
12523 case X86::ATOMNAND16:
12524 case X86::ATOMNAND32:
12525 case X86::ATOMNAND64: {
12526 unsigned t2 = MRI.createVirtualRegister(RC);
12527 unsigned NOTOpc;
12528 unsigned ANDOpc = getNonAtomicOpcodeWithExtraOpc(Opc, NOTOpc);
12529 BuildMI(mainMBB, DL, TII->get(ANDOpc), t2).addReg(SrcReg)
12530 .addReg(AccReg);
12531 BuildMI(mainMBB, DL, TII->get(NOTOpc), t1).addReg(t2);
12532 break;
12533 }
Michael Liao08382492012-09-21 03:00:17 +000012534 case X86::ATOMMAX8:
Michael Liaob118a072012-09-20 03:06:15 +000012535 case X86::ATOMMAX16:
12536 case X86::ATOMMAX32:
12537 case X86::ATOMMAX64:
Michael Liaofe87c302012-09-21 03:18:52 +000012538 case X86::ATOMMIN8:
Michael Liaob118a072012-09-20 03:06:15 +000012539 case X86::ATOMMIN16:
12540 case X86::ATOMMIN32:
12541 case X86::ATOMMIN64:
Michael Liaofe87c302012-09-21 03:18:52 +000012542 case X86::ATOMUMAX8:
Michael Liaob118a072012-09-20 03:06:15 +000012543 case X86::ATOMUMAX16:
12544 case X86::ATOMUMAX32:
12545 case X86::ATOMUMAX64:
Michael Liaofe87c302012-09-21 03:18:52 +000012546 case X86::ATOMUMIN8:
Michael Liaob118a072012-09-20 03:06:15 +000012547 case X86::ATOMUMIN16:
12548 case X86::ATOMUMIN32:
12549 case X86::ATOMUMIN64: {
12550 unsigned CMPOpc;
12551 unsigned CMOVOpc = getNonAtomicOpcodeWithExtraOpc(Opc, CMPOpc);
12552
12553 BuildMI(mainMBB, DL, TII->get(CMPOpc))
12554 .addReg(SrcReg)
12555 .addReg(AccReg);
12556
12557 if (Subtarget->hasCMov()) {
Michael Liaofe87c302012-09-21 03:18:52 +000012558 if (VT != MVT::i8) {
12559 // Native support
12560 BuildMI(mainMBB, DL, TII->get(CMOVOpc), t1)
12561 .addReg(SrcReg)
12562 .addReg(AccReg);
12563 } else {
12564 // Promote i8 to i32 to use CMOV32
12565 const TargetRegisterClass *RC32 = getRegClassFor(MVT::i32);
12566 unsigned SrcReg32 = MRI.createVirtualRegister(RC32);
12567 unsigned AccReg32 = MRI.createVirtualRegister(RC32);
12568 unsigned t2 = MRI.createVirtualRegister(RC32);
12569
12570 unsigned Undef = MRI.createVirtualRegister(RC32);
12571 BuildMI(mainMBB, DL, TII->get(TargetOpcode::IMPLICIT_DEF), Undef);
12572
12573 BuildMI(mainMBB, DL, TII->get(TargetOpcode::INSERT_SUBREG), SrcReg32)
12574 .addReg(Undef)
12575 .addReg(SrcReg)
12576 .addImm(X86::sub_8bit);
12577 BuildMI(mainMBB, DL, TII->get(TargetOpcode::INSERT_SUBREG), AccReg32)
12578 .addReg(Undef)
12579 .addReg(AccReg)
12580 .addImm(X86::sub_8bit);
12581
12582 BuildMI(mainMBB, DL, TII->get(CMOVOpc), t2)
12583 .addReg(SrcReg32)
12584 .addReg(AccReg32);
12585
12586 BuildMI(mainMBB, DL, TII->get(TargetOpcode::COPY), t1)
12587 .addReg(t2, 0, X86::sub_8bit);
12588 }
Michael Liaob118a072012-09-20 03:06:15 +000012589 } else {
12590 // Use pseudo select and lower them.
Michael Liaofe87c302012-09-21 03:18:52 +000012591 assert((VT == MVT::i8 || VT == MVT::i16 || VT == MVT::i32) &&
Michael Liaob118a072012-09-20 03:06:15 +000012592 "Invalid atomic-load-op transformation!");
12593 unsigned SelOpc = getPseudoCMOVOpc(VT);
12594 X86::CondCode CC = X86::getCondFromCMovOpc(CMOVOpc);
12595 assert(CC != X86::COND_INVALID && "Invalid atomic-load-op transformation!");
12596 MIB = BuildMI(mainMBB, DL, TII->get(SelOpc), t1)
12597 .addReg(SrcReg).addReg(AccReg)
12598 .addImm(CC);
12599 mainMBB = EmitLoweredSelect(MIB, mainMBB);
12600 }
12601 break;
12602 }
12603 }
12604
12605 // Copy AccPhyReg back from virtual register.
12606 BuildMI(mainMBB, DL, TII->get(TargetOpcode::COPY), AccPhyReg)
12607 .addReg(AccReg);
12608
12609 MIB = BuildMI(mainMBB, DL, TII->get(LCMPXCHGOpc));
12610 for (unsigned i = 0; i < X86::AddrNumOperands; ++i)
12611 MIB.addOperand(MI->getOperand(MemOpndSlot + i));
12612 MIB.addReg(t1);
12613 MIB.setMemRefs(MMOBegin, MMOEnd);
12614
12615 BuildMI(mainMBB, DL, TII->get(X86::JNE_4)).addMBB(origMainMBB);
12616
12617 mainMBB->addSuccessor(origMainMBB);
12618 mainMBB->addSuccessor(sinkMBB);
12619
12620 // sinkMBB:
12621 sinkMBB->addLiveIn(AccPhyReg);
12622
12623 BuildMI(*sinkMBB, sinkMBB->begin(), DL,
12624 TII->get(TargetOpcode::COPY), DstReg)
12625 .addReg(AccPhyReg);
12626
12627 MI->eraseFromParent();
12628 return sinkMBB;
12629}
12630
12631// EmitAtomicLoadArith6432 - emit the code sequence for pseudo atomic
12632// instructions. They will be translated into a spin-loop or compare-exchange
12633// loop from
12634//
12635// ...
12636// dst = atomic-fetch-op MI.addr, MI.val
12637// ...
12638//
12639// to
12640//
12641// ...
12642// EAX = LOAD [MI.addr + 0]
12643// EDX = LOAD [MI.addr + 4]
12644// loop:
12645// EBX = OP MI.val.lo, EAX
12646// ECX = OP MI.val.hi, EDX
12647// LCMPXCHG8B [MI.addr], [ECX:EBX & EDX:EAX are implicitly used and EDX:EAX is implicitly defined]
12648// JNE loop
12649// sink:
12650// dst = EDX:EAX
12651// ...
12652MachineBasicBlock *
12653X86TargetLowering::EmitAtomicLoadArith6432(MachineInstr *MI,
12654 MachineBasicBlock *MBB) const {
12655 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
12656 DebugLoc DL = MI->getDebugLoc();
12657
12658 MachineFunction *MF = MBB->getParent();
12659 MachineRegisterInfo &MRI = MF->getRegInfo();
12660
12661 const BasicBlock *BB = MBB->getBasicBlock();
12662 MachineFunction::iterator I = MBB;
12663 ++I;
12664
12665 assert(MI->getNumOperands() <= X86::AddrNumOperands + 4 &&
12666 "Unexpected number of operands");
12667
12668 assert(MI->hasOneMemOperand() &&
12669 "Expected atomic-load-op32 to have one memoperand");
12670
12671 // Memory Reference
12672 MachineInstr::mmo_iterator MMOBegin = MI->memoperands_begin();
12673 MachineInstr::mmo_iterator MMOEnd = MI->memoperands_end();
12674
12675 unsigned DstLoReg, DstHiReg;
12676 unsigned SrcLoReg, SrcHiReg;
12677 unsigned MemOpndSlot;
12678
12679 unsigned CurOp = 0;
12680
12681 DstLoReg = MI->getOperand(CurOp++).getReg();
12682 DstHiReg = MI->getOperand(CurOp++).getReg();
12683 MemOpndSlot = CurOp;
12684 CurOp += X86::AddrNumOperands;
12685 SrcLoReg = MI->getOperand(CurOp++).getReg();
12686 SrcHiReg = MI->getOperand(CurOp++).getReg();
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012687
Craig Topperc9099502012-04-20 06:31:50 +000012688 const TargetRegisterClass *RC = &X86::GR32RegClass;
Michael Liaoe5e8f762012-09-25 18:08:13 +000012689 const TargetRegisterClass *RC8 = &X86::GR8RegClass;
Scott Michelfdc40a02009-02-17 22:15:04 +000012690
Michael Liaob118a072012-09-20 03:06:15 +000012691 unsigned LCMPXCHGOpc = X86::LCMPXCHG8B;
12692 unsigned LOADOpc = X86::MOV32rm;
Scott Michelfdc40a02009-02-17 22:15:04 +000012693
Michael Liaob118a072012-09-20 03:06:15 +000012694 // For the atomic load-arith operator, we generate
Mon P Wang63307c32008-05-05 19:05:59 +000012695 //
Michael Liaob118a072012-09-20 03:06:15 +000012696 // thisMBB:
12697 // EAX = LOAD [MI.addr + 0]
12698 // EDX = LOAD [MI.addr + 4]
12699 // mainMBB:
12700 // EBX = OP MI.vallo, EAX
12701 // ECX = OP MI.valhi, EDX
12702 // LCMPXCHG8B [MI.addr], [ECX:EBX & EDX:EAX are implicitly used and EDX:EAX is implicitly defined]
12703 // JNE mainMBB
12704 // sinkMBB:
Scott Michelfdc40a02009-02-17 22:15:04 +000012705
Mon P Wang63307c32008-05-05 19:05:59 +000012706 MachineBasicBlock *thisMBB = MBB;
Michael Liaob118a072012-09-20 03:06:15 +000012707 MachineBasicBlock *mainMBB = MF->CreateMachineBasicBlock(BB);
12708 MachineBasicBlock *sinkMBB = MF->CreateMachineBasicBlock(BB);
12709 MF->insert(I, mainMBB);
12710 MF->insert(I, sinkMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000012711
Michael Liaob118a072012-09-20 03:06:15 +000012712 MachineInstrBuilder MIB;
Scott Michelfdc40a02009-02-17 22:15:04 +000012713
Michael Liaob118a072012-09-20 03:06:15 +000012714 // Transfer the remainder of BB and its successor edges to sinkMBB.
12715 sinkMBB->splice(sinkMBB->begin(), MBB,
12716 llvm::next(MachineBasicBlock::iterator(MI)), MBB->end());
12717 sinkMBB->transferSuccessorsAndUpdatePHIs(MBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000012718
Michael Liaob118a072012-09-20 03:06:15 +000012719 // thisMBB:
12720 // Lo
12721 MIB = BuildMI(thisMBB, DL, TII->get(LOADOpc), X86::EAX);
12722 for (unsigned i = 0; i < X86::AddrNumOperands; ++i)
12723 MIB.addOperand(MI->getOperand(MemOpndSlot + i));
12724 MIB.setMemRefs(MMOBegin, MMOEnd);
12725 // Hi
12726 MIB = BuildMI(thisMBB, DL, TII->get(LOADOpc), X86::EDX);
12727 for (unsigned i = 0; i < X86::AddrNumOperands; ++i) {
Evan Chenga395f4d2012-10-11 00:15:48 +000012728 if (i == X86::AddrDisp)
Michael Liaob118a072012-09-20 03:06:15 +000012729 MIB.addDisp(MI->getOperand(MemOpndSlot + i), 4); // 4 == sizeof(i32)
Evan Chenga395f4d2012-10-11 00:15:48 +000012730 else
Michael Liaob118a072012-09-20 03:06:15 +000012731 MIB.addOperand(MI->getOperand(MemOpndSlot + i));
12732 }
12733 MIB.setMemRefs(MMOBegin, MMOEnd);
Scott Michelfdc40a02009-02-17 22:15:04 +000012734
Michael Liaob118a072012-09-20 03:06:15 +000012735 thisMBB->addSuccessor(mainMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000012736
Michael Liaob118a072012-09-20 03:06:15 +000012737 // mainMBB:
12738 MachineBasicBlock *origMainMBB = mainMBB;
12739 mainMBB->addLiveIn(X86::EAX);
12740 mainMBB->addLiveIn(X86::EDX);
Scott Michelfdc40a02009-02-17 22:15:04 +000012741
Michael Liaob118a072012-09-20 03:06:15 +000012742 // Copy EDX:EAX as they are used more than once.
12743 unsigned LoReg = MRI.createVirtualRegister(RC);
12744 unsigned HiReg = MRI.createVirtualRegister(RC);
12745 BuildMI(mainMBB, DL, TII->get(TargetOpcode::COPY), LoReg).addReg(X86::EAX);
12746 BuildMI(mainMBB, DL, TII->get(TargetOpcode::COPY), HiReg).addReg(X86::EDX);
Mon P Wangab3e7472008-05-05 22:56:23 +000012747
Michael Liaob118a072012-09-20 03:06:15 +000012748 unsigned t1L = MRI.createVirtualRegister(RC);
12749 unsigned t1H = MRI.createVirtualRegister(RC);
Scott Michelfdc40a02009-02-17 22:15:04 +000012750
Michael Liaob118a072012-09-20 03:06:15 +000012751 unsigned Opc = MI->getOpcode();
12752 switch (Opc) {
12753 default:
12754 llvm_unreachable("Unhandled atomic-load-op6432 opcode!");
12755 case X86::ATOMAND6432:
12756 case X86::ATOMOR6432:
12757 case X86::ATOMXOR6432:
12758 case X86::ATOMADD6432:
12759 case X86::ATOMSUB6432: {
12760 unsigned HiOpc;
12761 unsigned LoOpc = getNonAtomic6432Opcode(Opc, HiOpc);
Michael Liaodd3383f2012-11-12 06:49:17 +000012762 BuildMI(mainMBB, DL, TII->get(LoOpc), t1L).addReg(LoReg).addReg(SrcLoReg);
12763 BuildMI(mainMBB, DL, TII->get(HiOpc), t1H).addReg(HiReg).addReg(SrcHiReg);
Michael Liaob118a072012-09-20 03:06:15 +000012764 break;
12765 }
12766 case X86::ATOMNAND6432: {
12767 unsigned HiOpc, NOTOpc;
12768 unsigned LoOpc = getNonAtomic6432OpcodeWithExtraOpc(Opc, HiOpc, NOTOpc);
12769 unsigned t2L = MRI.createVirtualRegister(RC);
12770 unsigned t2H = MRI.createVirtualRegister(RC);
12771 BuildMI(mainMBB, DL, TII->get(LoOpc), t2L).addReg(SrcLoReg).addReg(LoReg);
12772 BuildMI(mainMBB, DL, TII->get(HiOpc), t2H).addReg(SrcHiReg).addReg(HiReg);
12773 BuildMI(mainMBB, DL, TII->get(NOTOpc), t1L).addReg(t2L);
12774 BuildMI(mainMBB, DL, TII->get(NOTOpc), t1H).addReg(t2H);
12775 break;
12776 }
Michael Liaoe5e8f762012-09-25 18:08:13 +000012777 case X86::ATOMMAX6432:
12778 case X86::ATOMMIN6432:
12779 case X86::ATOMUMAX6432:
12780 case X86::ATOMUMIN6432: {
12781 unsigned HiOpc;
12782 unsigned LoOpc = getNonAtomic6432Opcode(Opc, HiOpc);
12783 unsigned cL = MRI.createVirtualRegister(RC8);
12784 unsigned cH = MRI.createVirtualRegister(RC8);
12785 unsigned cL32 = MRI.createVirtualRegister(RC);
12786 unsigned cH32 = MRI.createVirtualRegister(RC);
12787 unsigned cc = MRI.createVirtualRegister(RC);
12788 // cl := cmp src_lo, lo
12789 BuildMI(mainMBB, DL, TII->get(X86::CMP32rr))
12790 .addReg(SrcLoReg).addReg(LoReg);
12791 BuildMI(mainMBB, DL, TII->get(LoOpc), cL);
12792 BuildMI(mainMBB, DL, TII->get(X86::MOVZX32rr8), cL32).addReg(cL);
12793 // ch := cmp src_hi, hi
12794 BuildMI(mainMBB, DL, TII->get(X86::CMP32rr))
12795 .addReg(SrcHiReg).addReg(HiReg);
12796 BuildMI(mainMBB, DL, TII->get(HiOpc), cH);
12797 BuildMI(mainMBB, DL, TII->get(X86::MOVZX32rr8), cH32).addReg(cH);
12798 // cc := if (src_hi == hi) ? cl : ch;
12799 if (Subtarget->hasCMov()) {
12800 BuildMI(mainMBB, DL, TII->get(X86::CMOVE32rr), cc)
12801 .addReg(cH32).addReg(cL32);
12802 } else {
12803 MIB = BuildMI(mainMBB, DL, TII->get(X86::CMOV_GR32), cc)
12804 .addReg(cH32).addReg(cL32)
12805 .addImm(X86::COND_E);
12806 mainMBB = EmitLoweredSelect(MIB, mainMBB);
12807 }
12808 BuildMI(mainMBB, DL, TII->get(X86::TEST32rr)).addReg(cc).addReg(cc);
12809 if (Subtarget->hasCMov()) {
12810 BuildMI(mainMBB, DL, TII->get(X86::CMOVNE32rr), t1L)
12811 .addReg(SrcLoReg).addReg(LoReg);
12812 BuildMI(mainMBB, DL, TII->get(X86::CMOVNE32rr), t1H)
12813 .addReg(SrcHiReg).addReg(HiReg);
12814 } else {
12815 MIB = BuildMI(mainMBB, DL, TII->get(X86::CMOV_GR32), t1L)
12816 .addReg(SrcLoReg).addReg(LoReg)
12817 .addImm(X86::COND_NE);
12818 mainMBB = EmitLoweredSelect(MIB, mainMBB);
12819 MIB = BuildMI(mainMBB, DL, TII->get(X86::CMOV_GR32), t1H)
12820 .addReg(SrcHiReg).addReg(HiReg)
12821 .addImm(X86::COND_NE);
12822 mainMBB = EmitLoweredSelect(MIB, mainMBB);
12823 }
12824 break;
12825 }
Michael Liaob118a072012-09-20 03:06:15 +000012826 case X86::ATOMSWAP6432: {
12827 unsigned HiOpc;
12828 unsigned LoOpc = getNonAtomic6432Opcode(Opc, HiOpc);
12829 BuildMI(mainMBB, DL, TII->get(LoOpc), t1L).addReg(SrcLoReg);
12830 BuildMI(mainMBB, DL, TII->get(HiOpc), t1H).addReg(SrcHiReg);
12831 break;
12832 }
12833 }
Mon P Wang63307c32008-05-05 19:05:59 +000012834
Michael Liaob118a072012-09-20 03:06:15 +000012835 // Copy EDX:EAX back from HiReg:LoReg
12836 BuildMI(mainMBB, DL, TII->get(TargetOpcode::COPY), X86::EAX).addReg(LoReg);
12837 BuildMI(mainMBB, DL, TII->get(TargetOpcode::COPY), X86::EDX).addReg(HiReg);
12838 // Copy ECX:EBX from t1H:t1L
12839 BuildMI(mainMBB, DL, TII->get(TargetOpcode::COPY), X86::EBX).addReg(t1L);
12840 BuildMI(mainMBB, DL, TII->get(TargetOpcode::COPY), X86::ECX).addReg(t1H);
Mon P Wangab3e7472008-05-05 22:56:23 +000012841
Michael Liaob118a072012-09-20 03:06:15 +000012842 MIB = BuildMI(mainMBB, DL, TII->get(LCMPXCHGOpc));
12843 for (unsigned i = 0; i < X86::AddrNumOperands; ++i)
12844 MIB.addOperand(MI->getOperand(MemOpndSlot + i));
12845 MIB.setMemRefs(MMOBegin, MMOEnd);
Mon P Wang63307c32008-05-05 19:05:59 +000012846
Michael Liaob118a072012-09-20 03:06:15 +000012847 BuildMI(mainMBB, DL, TII->get(X86::JNE_4)).addMBB(origMainMBB);
Mon P Wang63307c32008-05-05 19:05:59 +000012848
Michael Liaob118a072012-09-20 03:06:15 +000012849 mainMBB->addSuccessor(origMainMBB);
12850 mainMBB->addSuccessor(sinkMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000012851
Michael Liaob118a072012-09-20 03:06:15 +000012852 // sinkMBB:
12853 sinkMBB->addLiveIn(X86::EAX);
12854 sinkMBB->addLiveIn(X86::EDX);
Scott Michelfdc40a02009-02-17 22:15:04 +000012855
Michael Liaob118a072012-09-20 03:06:15 +000012856 BuildMI(*sinkMBB, sinkMBB->begin(), DL,
12857 TII->get(TargetOpcode::COPY), DstLoReg)
12858 .addReg(X86::EAX);
12859 BuildMI(*sinkMBB, sinkMBB->begin(), DL,
12860 TII->get(TargetOpcode::COPY), DstHiReg)
12861 .addReg(X86::EDX);
Mon P Wang63307c32008-05-05 19:05:59 +000012862
Michael Liaob118a072012-09-20 03:06:15 +000012863 MI->eraseFromParent();
12864 return sinkMBB;
Mon P Wang63307c32008-05-05 19:05:59 +000012865}
12866
Eric Christopherf83a5de2009-08-27 18:08:16 +000012867// FIXME: When we get size specific XMM0 registers, i.e. XMM0_V16I8
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000012868// or XMM0_V32I8 in AVX all of this code can be replaced with that
12869// in the .td file.
Craig Topper8cb8c812012-11-10 09:02:47 +000012870static MachineBasicBlock *EmitPCMPSTRM(MachineInstr *MI, MachineBasicBlock *BB,
12871 const TargetInstrInfo *TII) {
Eric Christopherb120ab42009-08-18 22:50:32 +000012872 unsigned Opc;
Craig Topper8aae8dd2012-11-10 08:57:41 +000012873 switch (MI->getOpcode()) {
12874 default: llvm_unreachable("illegal opcode!");
12875 case X86::PCMPISTRM128REG: Opc = X86::PCMPISTRM128rr; break;
12876 case X86::VPCMPISTRM128REG: Opc = X86::VPCMPISTRM128rr; break;
12877 case X86::PCMPISTRM128MEM: Opc = X86::PCMPISTRM128rm; break;
12878 case X86::VPCMPISTRM128MEM: Opc = X86::VPCMPISTRM128rm; break;
12879 case X86::PCMPESTRM128REG: Opc = X86::PCMPESTRM128rr; break;
12880 case X86::VPCMPESTRM128REG: Opc = X86::VPCMPESTRM128rr; break;
12881 case X86::PCMPESTRM128MEM: Opc = X86::PCMPESTRM128rm; break;
12882 case X86::VPCMPESTRM128MEM: Opc = X86::VPCMPESTRM128rm; break;
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000012883 }
Eric Christopherb120ab42009-08-18 22:50:32 +000012884
Craig Topper8aae8dd2012-11-10 08:57:41 +000012885 DebugLoc dl = MI->getDebugLoc();
Eric Christopher41c902f2010-11-30 08:20:21 +000012886 MachineInstrBuilder MIB = BuildMI(*BB, MI, dl, TII->get(Opc));
Craig Topper8aae8dd2012-11-10 08:57:41 +000012887
Craig Topper52ea2452012-11-10 09:25:36 +000012888 unsigned NumArgs = MI->getNumOperands();
12889 for (unsigned i = 1; i < NumArgs; ++i) {
12890 MachineOperand &Op = MI->getOperand(i);
Eric Christopherb120ab42009-08-18 22:50:32 +000012891 if (!(Op.isReg() && Op.isImplicit()))
12892 MIB.addOperand(Op);
12893 }
Craig Topper8aae8dd2012-11-10 08:57:41 +000012894 if (MI->hasOneMemOperand())
Craig Topper9c7ae012012-11-10 01:23:36 +000012895 MIB->setMemRefs(MI->memoperands_begin(), MI->memoperands_end());
12896
Bruno Cardoso Lopes5affa512011-08-31 03:04:09 +000012897 BuildMI(*BB, MI, dl,
Craig Topper638aa682012-08-05 00:17:48 +000012898 TII->get(TargetOpcode::COPY), MI->getOperand(0).getReg())
Eric Christopherb120ab42009-08-18 22:50:32 +000012899 .addReg(X86::XMM0);
12900
Dan Gohman14152b42010-07-06 20:24:04 +000012901 MI->eraseFromParent();
Eric Christopherb120ab42009-08-18 22:50:32 +000012902 return BB;
12903}
12904
Craig Topper9c7ae012012-11-10 01:23:36 +000012905// FIXME: Custom handling because TableGen doesn't support multiple implicit
12906// defs in an instruction pattern
Craig Topper8cb8c812012-11-10 09:02:47 +000012907static MachineBasicBlock *EmitPCMPSTRI(MachineInstr *MI, MachineBasicBlock *BB,
12908 const TargetInstrInfo *TII) {
Craig Topper9c7ae012012-11-10 01:23:36 +000012909 unsigned Opc;
Craig Topper8aae8dd2012-11-10 08:57:41 +000012910 switch (MI->getOpcode()) {
12911 default: llvm_unreachable("illegal opcode!");
12912 case X86::PCMPISTRIREG: Opc = X86::PCMPISTRIrr; break;
12913 case X86::VPCMPISTRIREG: Opc = X86::VPCMPISTRIrr; break;
12914 case X86::PCMPISTRIMEM: Opc = X86::PCMPISTRIrm; break;
12915 case X86::VPCMPISTRIMEM: Opc = X86::VPCMPISTRIrm; break;
12916 case X86::PCMPESTRIREG: Opc = X86::PCMPESTRIrr; break;
12917 case X86::VPCMPESTRIREG: Opc = X86::VPCMPESTRIrr; break;
12918 case X86::PCMPESTRIMEM: Opc = X86::PCMPESTRIrm; break;
12919 case X86::VPCMPESTRIMEM: Opc = X86::VPCMPESTRIrm; break;
Craig Topper9c7ae012012-11-10 01:23:36 +000012920 }
12921
Craig Topper8aae8dd2012-11-10 08:57:41 +000012922 DebugLoc dl = MI->getDebugLoc();
Craig Topper9c7ae012012-11-10 01:23:36 +000012923 MachineInstrBuilder MIB = BuildMI(*BB, MI, dl, TII->get(Opc));
Craig Topper8aae8dd2012-11-10 08:57:41 +000012924
Craig Topper52ea2452012-11-10 09:25:36 +000012925 unsigned NumArgs = MI->getNumOperands(); // remove the results
12926 for (unsigned i = 1; i < NumArgs; ++i) {
12927 MachineOperand &Op = MI->getOperand(i);
Craig Topper9c7ae012012-11-10 01:23:36 +000012928 if (!(Op.isReg() && Op.isImplicit()))
12929 MIB.addOperand(Op);
12930 }
Craig Topper8aae8dd2012-11-10 08:57:41 +000012931 if (MI->hasOneMemOperand())
Craig Topper9c7ae012012-11-10 01:23:36 +000012932 MIB->setMemRefs(MI->memoperands_begin(), MI->memoperands_end());
12933
12934 BuildMI(*BB, MI, dl,
12935 TII->get(TargetOpcode::COPY), MI->getOperand(0).getReg())
12936 .addReg(X86::ECX);
12937
12938 MI->eraseFromParent();
12939 return BB;
12940}
12941
Craig Topper2da36912012-11-11 22:45:02 +000012942static MachineBasicBlock * EmitMonitor(MachineInstr *MI, MachineBasicBlock *BB,
12943 const TargetInstrInfo *TII,
12944 const X86Subtarget* Subtarget) {
Eric Christopher228232b2010-11-30 07:20:12 +000012945 DebugLoc dl = MI->getDebugLoc();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000012946
Eric Christopher228232b2010-11-30 07:20:12 +000012947 // Address into RAX/EAX, other two args into ECX, EDX.
12948 unsigned MemOpc = Subtarget->is64Bit() ? X86::LEA64r : X86::LEA32r;
12949 unsigned MemReg = Subtarget->is64Bit() ? X86::RAX : X86::EAX;
12950 MachineInstrBuilder MIB = BuildMI(*BB, MI, dl, TII->get(MemOpc), MemReg);
12951 for (int i = 0; i < X86::AddrNumOperands; ++i)
Eric Christopher82be2202010-11-30 08:10:28 +000012952 MIB.addOperand(MI->getOperand(i));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000012953
Eric Christopher228232b2010-11-30 07:20:12 +000012954 unsigned ValOps = X86::AddrNumOperands;
12955 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::ECX)
12956 .addReg(MI->getOperand(ValOps).getReg());
12957 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::EDX)
12958 .addReg(MI->getOperand(ValOps+1).getReg());
12959
12960 // The instruction doesn't actually take any operands though.
12961 BuildMI(*BB, MI, dl, TII->get(X86::MONITORrrr));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000012962
Eric Christopher228232b2010-11-30 07:20:12 +000012963 MI->eraseFromParent(); // The pseudo is gone now.
12964 return BB;
12965}
12966
12967MachineBasicBlock *
Dan Gohman320afb82010-10-12 18:00:49 +000012968X86TargetLowering::EmitVAARG64WithCustomInserter(
12969 MachineInstr *MI,
12970 MachineBasicBlock *MBB) const {
12971 // Emit va_arg instruction on X86-64.
12972
12973 // Operands to this pseudo-instruction:
12974 // 0 ) Output : destination address (reg)
12975 // 1-5) Input : va_list address (addr, i64mem)
12976 // 6 ) ArgSize : Size (in bytes) of vararg type
12977 // 7 ) ArgMode : 0=overflow only, 1=use gp_offset, 2=use fp_offset
12978 // 8 ) Align : Alignment of type
12979 // 9 ) EFLAGS (implicit-def)
12980
12981 assert(MI->getNumOperands() == 10 && "VAARG_64 should have 10 operands!");
12982 assert(X86::AddrNumOperands == 5 && "VAARG_64 assumes 5 address operands");
12983
12984 unsigned DestReg = MI->getOperand(0).getReg();
12985 MachineOperand &Base = MI->getOperand(1);
12986 MachineOperand &Scale = MI->getOperand(2);
12987 MachineOperand &Index = MI->getOperand(3);
12988 MachineOperand &Disp = MI->getOperand(4);
12989 MachineOperand &Segment = MI->getOperand(5);
12990 unsigned ArgSize = MI->getOperand(6).getImm();
12991 unsigned ArgMode = MI->getOperand(7).getImm();
12992 unsigned Align = MI->getOperand(8).getImm();
12993
12994 // Memory Reference
12995 assert(MI->hasOneMemOperand() && "Expected VAARG_64 to have one memoperand");
12996 MachineInstr::mmo_iterator MMOBegin = MI->memoperands_begin();
12997 MachineInstr::mmo_iterator MMOEnd = MI->memoperands_end();
12998
12999 // Machine Information
13000 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
13001 MachineRegisterInfo &MRI = MBB->getParent()->getRegInfo();
13002 const TargetRegisterClass *AddrRegClass = getRegClassFor(MVT::i64);
13003 const TargetRegisterClass *OffsetRegClass = getRegClassFor(MVT::i32);
13004 DebugLoc DL = MI->getDebugLoc();
13005
13006 // struct va_list {
13007 // i32 gp_offset
13008 // i32 fp_offset
13009 // i64 overflow_area (address)
13010 // i64 reg_save_area (address)
13011 // }
13012 // sizeof(va_list) = 24
13013 // alignment(va_list) = 8
13014
13015 unsigned TotalNumIntRegs = 6;
13016 unsigned TotalNumXMMRegs = 8;
13017 bool UseGPOffset = (ArgMode == 1);
13018 bool UseFPOffset = (ArgMode == 2);
13019 unsigned MaxOffset = TotalNumIntRegs * 8 +
13020 (UseFPOffset ? TotalNumXMMRegs * 16 : 0);
13021
13022 /* Align ArgSize to a multiple of 8 */
13023 unsigned ArgSizeA8 = (ArgSize + 7) & ~7;
13024 bool NeedsAlign = (Align > 8);
13025
13026 MachineBasicBlock *thisMBB = MBB;
13027 MachineBasicBlock *overflowMBB;
13028 MachineBasicBlock *offsetMBB;
13029 MachineBasicBlock *endMBB;
13030
13031 unsigned OffsetDestReg = 0; // Argument address computed by offsetMBB
13032 unsigned OverflowDestReg = 0; // Argument address computed by overflowMBB
13033 unsigned OffsetReg = 0;
13034
13035 if (!UseGPOffset && !UseFPOffset) {
13036 // If we only pull from the overflow region, we don't create a branch.
13037 // We don't need to alter control flow.
13038 OffsetDestReg = 0; // unused
13039 OverflowDestReg = DestReg;
13040
13041 offsetMBB = NULL;
13042 overflowMBB = thisMBB;
13043 endMBB = thisMBB;
13044 } else {
13045 // First emit code to check if gp_offset (or fp_offset) is below the bound.
13046 // If so, pull the argument from reg_save_area. (branch to offsetMBB)
13047 // If not, pull from overflow_area. (branch to overflowMBB)
13048 //
13049 // thisMBB
13050 // | .
13051 // | .
13052 // offsetMBB overflowMBB
13053 // | .
13054 // | .
13055 // endMBB
13056
13057 // Registers for the PHI in endMBB
13058 OffsetDestReg = MRI.createVirtualRegister(AddrRegClass);
13059 OverflowDestReg = MRI.createVirtualRegister(AddrRegClass);
13060
13061 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
13062 MachineFunction *MF = MBB->getParent();
13063 overflowMBB = MF->CreateMachineBasicBlock(LLVM_BB);
13064 offsetMBB = MF->CreateMachineBasicBlock(LLVM_BB);
13065 endMBB = MF->CreateMachineBasicBlock(LLVM_BB);
13066
13067 MachineFunction::iterator MBBIter = MBB;
13068 ++MBBIter;
13069
13070 // Insert the new basic blocks
13071 MF->insert(MBBIter, offsetMBB);
13072 MF->insert(MBBIter, overflowMBB);
13073 MF->insert(MBBIter, endMBB);
13074
13075 // Transfer the remainder of MBB and its successor edges to endMBB.
13076 endMBB->splice(endMBB->begin(), thisMBB,
13077 llvm::next(MachineBasicBlock::iterator(MI)),
13078 thisMBB->end());
13079 endMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
13080
13081 // Make offsetMBB and overflowMBB successors of thisMBB
13082 thisMBB->addSuccessor(offsetMBB);
13083 thisMBB->addSuccessor(overflowMBB);
13084
13085 // endMBB is a successor of both offsetMBB and overflowMBB
13086 offsetMBB->addSuccessor(endMBB);
13087 overflowMBB->addSuccessor(endMBB);
13088
13089 // Load the offset value into a register
13090 OffsetReg = MRI.createVirtualRegister(OffsetRegClass);
13091 BuildMI(thisMBB, DL, TII->get(X86::MOV32rm), OffsetReg)
13092 .addOperand(Base)
13093 .addOperand(Scale)
13094 .addOperand(Index)
13095 .addDisp(Disp, UseFPOffset ? 4 : 0)
13096 .addOperand(Segment)
13097 .setMemRefs(MMOBegin, MMOEnd);
13098
13099 // Check if there is enough room left to pull this argument.
13100 BuildMI(thisMBB, DL, TII->get(X86::CMP32ri))
13101 .addReg(OffsetReg)
13102 .addImm(MaxOffset + 8 - ArgSizeA8);
13103
13104 // Branch to "overflowMBB" if offset >= max
13105 // Fall through to "offsetMBB" otherwise
13106 BuildMI(thisMBB, DL, TII->get(X86::GetCondBranchFromCond(X86::COND_AE)))
13107 .addMBB(overflowMBB);
13108 }
13109
13110 // In offsetMBB, emit code to use the reg_save_area.
13111 if (offsetMBB) {
13112 assert(OffsetReg != 0);
13113
13114 // Read the reg_save_area address.
13115 unsigned RegSaveReg = MRI.createVirtualRegister(AddrRegClass);
13116 BuildMI(offsetMBB, DL, TII->get(X86::MOV64rm), RegSaveReg)
13117 .addOperand(Base)
13118 .addOperand(Scale)
13119 .addOperand(Index)
13120 .addDisp(Disp, 16)
13121 .addOperand(Segment)
13122 .setMemRefs(MMOBegin, MMOEnd);
13123
13124 // Zero-extend the offset
13125 unsigned OffsetReg64 = MRI.createVirtualRegister(AddrRegClass);
13126 BuildMI(offsetMBB, DL, TII->get(X86::SUBREG_TO_REG), OffsetReg64)
13127 .addImm(0)
13128 .addReg(OffsetReg)
13129 .addImm(X86::sub_32bit);
13130
13131 // Add the offset to the reg_save_area to get the final address.
13132 BuildMI(offsetMBB, DL, TII->get(X86::ADD64rr), OffsetDestReg)
13133 .addReg(OffsetReg64)
13134 .addReg(RegSaveReg);
13135
13136 // Compute the offset for the next argument
13137 unsigned NextOffsetReg = MRI.createVirtualRegister(OffsetRegClass);
13138 BuildMI(offsetMBB, DL, TII->get(X86::ADD32ri), NextOffsetReg)
13139 .addReg(OffsetReg)
13140 .addImm(UseFPOffset ? 16 : 8);
13141
13142 // Store it back into the va_list.
13143 BuildMI(offsetMBB, DL, TII->get(X86::MOV32mr))
13144 .addOperand(Base)
13145 .addOperand(Scale)
13146 .addOperand(Index)
13147 .addDisp(Disp, UseFPOffset ? 4 : 0)
13148 .addOperand(Segment)
13149 .addReg(NextOffsetReg)
13150 .setMemRefs(MMOBegin, MMOEnd);
13151
13152 // Jump to endMBB
13153 BuildMI(offsetMBB, DL, TII->get(X86::JMP_4))
13154 .addMBB(endMBB);
13155 }
13156
13157 //
13158 // Emit code to use overflow area
13159 //
13160
13161 // Load the overflow_area address into a register.
13162 unsigned OverflowAddrReg = MRI.createVirtualRegister(AddrRegClass);
13163 BuildMI(overflowMBB, DL, TII->get(X86::MOV64rm), OverflowAddrReg)
13164 .addOperand(Base)
13165 .addOperand(Scale)
13166 .addOperand(Index)
13167 .addDisp(Disp, 8)
13168 .addOperand(Segment)
13169 .setMemRefs(MMOBegin, MMOEnd);
13170
13171 // If we need to align it, do so. Otherwise, just copy the address
13172 // to OverflowDestReg.
13173 if (NeedsAlign) {
13174 // Align the overflow address
13175 assert((Align & (Align-1)) == 0 && "Alignment must be a power of 2");
13176 unsigned TmpReg = MRI.createVirtualRegister(AddrRegClass);
13177
13178 // aligned_addr = (addr + (align-1)) & ~(align-1)
13179 BuildMI(overflowMBB, DL, TII->get(X86::ADD64ri32), TmpReg)
13180 .addReg(OverflowAddrReg)
13181 .addImm(Align-1);
13182
13183 BuildMI(overflowMBB, DL, TII->get(X86::AND64ri32), OverflowDestReg)
13184 .addReg(TmpReg)
13185 .addImm(~(uint64_t)(Align-1));
13186 } else {
13187 BuildMI(overflowMBB, DL, TII->get(TargetOpcode::COPY), OverflowDestReg)
13188 .addReg(OverflowAddrReg);
13189 }
13190
13191 // Compute the next overflow address after this argument.
13192 // (the overflow address should be kept 8-byte aligned)
13193 unsigned NextAddrReg = MRI.createVirtualRegister(AddrRegClass);
13194 BuildMI(overflowMBB, DL, TII->get(X86::ADD64ri32), NextAddrReg)
13195 .addReg(OverflowDestReg)
13196 .addImm(ArgSizeA8);
13197
13198 // Store the new overflow address.
13199 BuildMI(overflowMBB, DL, TII->get(X86::MOV64mr))
13200 .addOperand(Base)
13201 .addOperand(Scale)
13202 .addOperand(Index)
13203 .addDisp(Disp, 8)
13204 .addOperand(Segment)
13205 .addReg(NextAddrReg)
13206 .setMemRefs(MMOBegin, MMOEnd);
13207
13208 // If we branched, emit the PHI to the front of endMBB.
13209 if (offsetMBB) {
13210 BuildMI(*endMBB, endMBB->begin(), DL,
13211 TII->get(X86::PHI), DestReg)
13212 .addReg(OffsetDestReg).addMBB(offsetMBB)
13213 .addReg(OverflowDestReg).addMBB(overflowMBB);
13214 }
13215
13216 // Erase the pseudo instruction
13217 MI->eraseFromParent();
13218
13219 return endMBB;
13220}
13221
13222MachineBasicBlock *
Dan Gohmand6708ea2009-08-15 01:38:56 +000013223X86TargetLowering::EmitVAStartSaveXMMRegsWithCustomInserter(
13224 MachineInstr *MI,
13225 MachineBasicBlock *MBB) const {
13226 // Emit code to save XMM registers to the stack. The ABI says that the
13227 // number of registers to save is given in %al, so it's theoretically
13228 // possible to do an indirect jump trick to avoid saving all of them,
13229 // however this code takes a simpler approach and just executes all
13230 // of the stores if %al is non-zero. It's less code, and it's probably
13231 // easier on the hardware branch predictor, and stores aren't all that
13232 // expensive anyway.
13233
13234 // Create the new basic blocks. One block contains all the XMM stores,
13235 // and one block is the final destination regardless of whether any
13236 // stores were performed.
13237 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
13238 MachineFunction *F = MBB->getParent();
13239 MachineFunction::iterator MBBIter = MBB;
13240 ++MBBIter;
13241 MachineBasicBlock *XMMSaveMBB = F->CreateMachineBasicBlock(LLVM_BB);
13242 MachineBasicBlock *EndMBB = F->CreateMachineBasicBlock(LLVM_BB);
13243 F->insert(MBBIter, XMMSaveMBB);
13244 F->insert(MBBIter, EndMBB);
13245
Dan Gohman14152b42010-07-06 20:24:04 +000013246 // Transfer the remainder of MBB and its successor edges to EndMBB.
13247 EndMBB->splice(EndMBB->begin(), MBB,
13248 llvm::next(MachineBasicBlock::iterator(MI)),
13249 MBB->end());
13250 EndMBB->transferSuccessorsAndUpdatePHIs(MBB);
13251
Dan Gohmand6708ea2009-08-15 01:38:56 +000013252 // The original block will now fall through to the XMM save block.
13253 MBB->addSuccessor(XMMSaveMBB);
13254 // The XMMSaveMBB will fall through to the end block.
13255 XMMSaveMBB->addSuccessor(EndMBB);
13256
13257 // Now add the instructions.
13258 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
13259 DebugLoc DL = MI->getDebugLoc();
13260
13261 unsigned CountReg = MI->getOperand(0).getReg();
13262 int64_t RegSaveFrameIndex = MI->getOperand(1).getImm();
13263 int64_t VarArgsFPOffset = MI->getOperand(2).getImm();
13264
13265 if (!Subtarget->isTargetWin64()) {
13266 // If %al is 0, branch around the XMM save block.
13267 BuildMI(MBB, DL, TII->get(X86::TEST8rr)).addReg(CountReg).addReg(CountReg);
Chris Lattnerbd13fb62010-02-11 19:25:55 +000013268 BuildMI(MBB, DL, TII->get(X86::JE_4)).addMBB(EndMBB);
Dan Gohmand6708ea2009-08-15 01:38:56 +000013269 MBB->addSuccessor(EndMBB);
13270 }
13271
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000013272 unsigned MOVOpc = Subtarget->hasFp256() ? X86::VMOVAPSmr : X86::MOVAPSmr;
Dan Gohmand6708ea2009-08-15 01:38:56 +000013273 // In the XMM save block, save all the XMM argument registers.
13274 for (int i = 3, e = MI->getNumOperands(); i != e; ++i) {
13275 int64_t Offset = (i - 3) * 16 + VarArgsFPOffset;
Dan Gohmanc76909a2009-09-25 20:36:54 +000013276 MachineMemOperand *MMO =
Evan Chengff89dcb2009-10-18 18:16:27 +000013277 F->getMachineMemOperand(
Chris Lattnere8639032010-09-21 06:22:23 +000013278 MachinePointerInfo::getFixedStack(RegSaveFrameIndex, Offset),
Chris Lattner59db5492010-09-21 04:39:43 +000013279 MachineMemOperand::MOStore,
Evan Chengff89dcb2009-10-18 18:16:27 +000013280 /*Size=*/16, /*Align=*/16);
Bruno Cardoso Lopes5affa512011-08-31 03:04:09 +000013281 BuildMI(XMMSaveMBB, DL, TII->get(MOVOpc))
Dan Gohmand6708ea2009-08-15 01:38:56 +000013282 .addFrameIndex(RegSaveFrameIndex)
13283 .addImm(/*Scale=*/1)
13284 .addReg(/*IndexReg=*/0)
13285 .addImm(/*Disp=*/Offset)
13286 .addReg(/*Segment=*/0)
13287 .addReg(MI->getOperand(i).getReg())
Dan Gohmanc76909a2009-09-25 20:36:54 +000013288 .addMemOperand(MMO);
Dan Gohmand6708ea2009-08-15 01:38:56 +000013289 }
13290
Dan Gohman14152b42010-07-06 20:24:04 +000013291 MI->eraseFromParent(); // The pseudo instruction is gone now.
Dan Gohmand6708ea2009-08-15 01:38:56 +000013292
13293 return EndMBB;
13294}
Mon P Wang63307c32008-05-05 19:05:59 +000013295
Lang Hames6e3f7e42012-02-03 01:13:49 +000013296// The EFLAGS operand of SelectItr might be missing a kill marker
13297// because there were multiple uses of EFLAGS, and ISel didn't know
13298// which to mark. Figure out whether SelectItr should have had a
13299// kill marker, and set it if it should. Returns the correct kill
13300// marker value.
13301static bool checkAndUpdateEFLAGSKill(MachineBasicBlock::iterator SelectItr,
13302 MachineBasicBlock* BB,
13303 const TargetRegisterInfo* TRI) {
13304 // Scan forward through BB for a use/def of EFLAGS.
13305 MachineBasicBlock::iterator miI(llvm::next(SelectItr));
13306 for (MachineBasicBlock::iterator miE = BB->end(); miI != miE; ++miI) {
Lang Hames50a36f72012-02-02 07:48:37 +000013307 const MachineInstr& mi = *miI;
Lang Hames6e3f7e42012-02-03 01:13:49 +000013308 if (mi.readsRegister(X86::EFLAGS))
Lang Hames50a36f72012-02-02 07:48:37 +000013309 return false;
Lang Hames6e3f7e42012-02-03 01:13:49 +000013310 if (mi.definesRegister(X86::EFLAGS))
13311 break; // Should have kill-flag - update below.
13312 }
13313
13314 // If we hit the end of the block, check whether EFLAGS is live into a
13315 // successor.
13316 if (miI == BB->end()) {
13317 for (MachineBasicBlock::succ_iterator sItr = BB->succ_begin(),
13318 sEnd = BB->succ_end();
13319 sItr != sEnd; ++sItr) {
13320 MachineBasicBlock* succ = *sItr;
13321 if (succ->isLiveIn(X86::EFLAGS))
13322 return false;
Lang Hames50a36f72012-02-02 07:48:37 +000013323 }
13324 }
13325
Lang Hames6e3f7e42012-02-03 01:13:49 +000013326 // We found a def, or hit the end of the basic block and EFLAGS wasn't live
13327 // out. SelectMI should have a kill flag on EFLAGS.
13328 SelectItr->addRegisterKilled(X86::EFLAGS, TRI);
Lang Hames50a36f72012-02-02 07:48:37 +000013329 return true;
13330}
13331
Evan Cheng60c07e12006-07-05 22:17:51 +000013332MachineBasicBlock *
Chris Lattner52600972009-09-02 05:57:00 +000013333X86TargetLowering::EmitLoweredSelect(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000013334 MachineBasicBlock *BB) const {
Chris Lattner52600972009-09-02 05:57:00 +000013335 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
13336 DebugLoc DL = MI->getDebugLoc();
Daniel Dunbara279bc32009-09-20 02:20:51 +000013337
Chris Lattner52600972009-09-02 05:57:00 +000013338 // To "insert" a SELECT_CC instruction, we actually have to insert the
13339 // diamond control-flow pattern. The incoming instruction knows the
13340 // destination vreg to set, the condition code register to branch on, the
13341 // true/false values to select between, and a branch opcode to use.
13342 const BasicBlock *LLVM_BB = BB->getBasicBlock();
13343 MachineFunction::iterator It = BB;
13344 ++It;
Daniel Dunbara279bc32009-09-20 02:20:51 +000013345
Chris Lattner52600972009-09-02 05:57:00 +000013346 // thisMBB:
13347 // ...
13348 // TrueVal = ...
13349 // cmpTY ccX, r1, r2
13350 // bCC copy1MBB
13351 // fallthrough --> copy0MBB
13352 MachineBasicBlock *thisMBB = BB;
13353 MachineFunction *F = BB->getParent();
13354 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
13355 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Chris Lattner52600972009-09-02 05:57:00 +000013356 F->insert(It, copy0MBB);
13357 F->insert(It, sinkMBB);
Bill Wendling730c07e2010-06-25 20:48:10 +000013358
Bill Wendling730c07e2010-06-25 20:48:10 +000013359 // If the EFLAGS register isn't dead in the terminator, then claim that it's
13360 // live into the sink and copy blocks.
Lang Hames6e3f7e42012-02-03 01:13:49 +000013361 const TargetRegisterInfo* TRI = getTargetMachine().getRegisterInfo();
13362 if (!MI->killsRegister(X86::EFLAGS) &&
13363 !checkAndUpdateEFLAGSKill(MI, BB, TRI)) {
13364 copy0MBB->addLiveIn(X86::EFLAGS);
13365 sinkMBB->addLiveIn(X86::EFLAGS);
Bill Wendling730c07e2010-06-25 20:48:10 +000013366 }
13367
Dan Gohman14152b42010-07-06 20:24:04 +000013368 // Transfer the remainder of BB and its successor edges to sinkMBB.
13369 sinkMBB->splice(sinkMBB->begin(), BB,
13370 llvm::next(MachineBasicBlock::iterator(MI)),
13371 BB->end());
13372 sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
13373
13374 // Add the true and fallthrough blocks as its successors.
13375 BB->addSuccessor(copy0MBB);
13376 BB->addSuccessor(sinkMBB);
13377
13378 // Create the conditional branch instruction.
13379 unsigned Opc =
13380 X86::GetCondBranchFromCond((X86::CondCode)MI->getOperand(3).getImm());
13381 BuildMI(BB, DL, TII->get(Opc)).addMBB(sinkMBB);
13382
Chris Lattner52600972009-09-02 05:57:00 +000013383 // copy0MBB:
13384 // %FalseValue = ...
13385 // # fallthrough to sinkMBB
Dan Gohman3335a222010-04-30 20:14:26 +000013386 copy0MBB->addSuccessor(sinkMBB);
Daniel Dunbara279bc32009-09-20 02:20:51 +000013387
Chris Lattner52600972009-09-02 05:57:00 +000013388 // sinkMBB:
13389 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
13390 // ...
Dan Gohman14152b42010-07-06 20:24:04 +000013391 BuildMI(*sinkMBB, sinkMBB->begin(), DL,
13392 TII->get(X86::PHI), MI->getOperand(0).getReg())
Chris Lattner52600972009-09-02 05:57:00 +000013393 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
13394 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
13395
Dan Gohman14152b42010-07-06 20:24:04 +000013396 MI->eraseFromParent(); // The pseudo instruction is gone now.
Dan Gohman3335a222010-04-30 20:14:26 +000013397 return sinkMBB;
Chris Lattner52600972009-09-02 05:57:00 +000013398}
13399
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000013400MachineBasicBlock *
Rafael Espindola151ab3e2011-08-30 19:47:04 +000013401X86TargetLowering::EmitLoweredSegAlloca(MachineInstr *MI, MachineBasicBlock *BB,
13402 bool Is64Bit) const {
13403 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
13404 DebugLoc DL = MI->getDebugLoc();
13405 MachineFunction *MF = BB->getParent();
13406 const BasicBlock *LLVM_BB = BB->getBasicBlock();
13407
Nick Lewycky8a8d4792011-12-02 22:16:29 +000013408 assert(getTargetMachine().Options.EnableSegmentedStacks);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000013409
13410 unsigned TlsReg = Is64Bit ? X86::FS : X86::GS;
13411 unsigned TlsOffset = Is64Bit ? 0x70 : 0x30;
13412
13413 // BB:
13414 // ... [Till the alloca]
13415 // If stacklet is not large enough, jump to mallocMBB
13416 //
13417 // bumpMBB:
13418 // Allocate by subtracting from RSP
13419 // Jump to continueMBB
13420 //
13421 // mallocMBB:
13422 // Allocate by call to runtime
13423 //
13424 // continueMBB:
13425 // ...
13426 // [rest of original BB]
13427 //
13428
13429 MachineBasicBlock *mallocMBB = MF->CreateMachineBasicBlock(LLVM_BB);
13430 MachineBasicBlock *bumpMBB = MF->CreateMachineBasicBlock(LLVM_BB);
13431 MachineBasicBlock *continueMBB = MF->CreateMachineBasicBlock(LLVM_BB);
13432
13433 MachineRegisterInfo &MRI = MF->getRegInfo();
13434 const TargetRegisterClass *AddrRegClass =
13435 getRegClassFor(Is64Bit ? MVT::i64:MVT::i32);
13436
13437 unsigned mallocPtrVReg = MRI.createVirtualRegister(AddrRegClass),
13438 bumpSPPtrVReg = MRI.createVirtualRegister(AddrRegClass),
13439 tmpSPVReg = MRI.createVirtualRegister(AddrRegClass),
Rafael Espindola66bf7432011-10-26 21:16:41 +000013440 SPLimitVReg = MRI.createVirtualRegister(AddrRegClass),
Rafael Espindola151ab3e2011-08-30 19:47:04 +000013441 sizeVReg = MI->getOperand(1).getReg(),
13442 physSPReg = Is64Bit ? X86::RSP : X86::ESP;
13443
13444 MachineFunction::iterator MBBIter = BB;
13445 ++MBBIter;
13446
13447 MF->insert(MBBIter, bumpMBB);
13448 MF->insert(MBBIter, mallocMBB);
13449 MF->insert(MBBIter, continueMBB);
13450
13451 continueMBB->splice(continueMBB->begin(), BB, llvm::next
13452 (MachineBasicBlock::iterator(MI)), BB->end());
13453 continueMBB->transferSuccessorsAndUpdatePHIs(BB);
13454
13455 // Add code to the main basic block to check if the stack limit has been hit,
13456 // and if so, jump to mallocMBB otherwise to bumpMBB.
13457 BuildMI(BB, DL, TII->get(TargetOpcode::COPY), tmpSPVReg).addReg(physSPReg);
Rafael Espindola66bf7432011-10-26 21:16:41 +000013458 BuildMI(BB, DL, TII->get(Is64Bit ? X86::SUB64rr:X86::SUB32rr), SPLimitVReg)
Rafael Espindola151ab3e2011-08-30 19:47:04 +000013459 .addReg(tmpSPVReg).addReg(sizeVReg);
13460 BuildMI(BB, DL, TII->get(Is64Bit ? X86::CMP64mr:X86::CMP32mr))
Rafael Espindola014f7a32012-01-11 18:14:03 +000013461 .addReg(0).addImm(1).addReg(0).addImm(TlsOffset).addReg(TlsReg)
Rafael Espindola66bf7432011-10-26 21:16:41 +000013462 .addReg(SPLimitVReg);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000013463 BuildMI(BB, DL, TII->get(X86::JG_4)).addMBB(mallocMBB);
13464
13465 // bumpMBB simply decreases the stack pointer, since we know the current
13466 // stacklet has enough space.
13467 BuildMI(bumpMBB, DL, TII->get(TargetOpcode::COPY), physSPReg)
Rafael Espindola66bf7432011-10-26 21:16:41 +000013468 .addReg(SPLimitVReg);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000013469 BuildMI(bumpMBB, DL, TII->get(TargetOpcode::COPY), bumpSPPtrVReg)
Rafael Espindola66bf7432011-10-26 21:16:41 +000013470 .addReg(SPLimitVReg);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000013471 BuildMI(bumpMBB, DL, TII->get(X86::JMP_4)).addMBB(continueMBB);
13472
13473 // Calls into a routine in libgcc to allocate more space from the heap.
Jakob Stoklund Olesen8bcde2a2012-02-16 00:02:50 +000013474 const uint32_t *RegMask =
13475 getTargetMachine().getRegisterInfo()->getCallPreservedMask(CallingConv::C);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000013476 if (Is64Bit) {
13477 BuildMI(mallocMBB, DL, TII->get(X86::MOV64rr), X86::RDI)
13478 .addReg(sizeVReg);
13479 BuildMI(mallocMBB, DL, TII->get(X86::CALL64pcrel32))
Jakob Stoklund Olesen85dccf12012-07-04 23:53:27 +000013480 .addExternalSymbol("__morestack_allocate_stack_space")
Jakob Stoklund Olesen8bcde2a2012-02-16 00:02:50 +000013481 .addRegMask(RegMask)
Jakob Stoklund Olesen85dccf12012-07-04 23:53:27 +000013482 .addReg(X86::RDI, RegState::Implicit)
Jakob Stoklund Olesen8bcde2a2012-02-16 00:02:50 +000013483 .addReg(X86::RAX, RegState::ImplicitDefine);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000013484 } else {
13485 BuildMI(mallocMBB, DL, TII->get(X86::SUB32ri), physSPReg).addReg(physSPReg)
13486 .addImm(12);
13487 BuildMI(mallocMBB, DL, TII->get(X86::PUSH32r)).addReg(sizeVReg);
13488 BuildMI(mallocMBB, DL, TII->get(X86::CALLpcrel32))
Jakob Stoklund Olesen8bcde2a2012-02-16 00:02:50 +000013489 .addExternalSymbol("__morestack_allocate_stack_space")
13490 .addRegMask(RegMask)
13491 .addReg(X86::EAX, RegState::ImplicitDefine);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000013492 }
13493
13494 if (!Is64Bit)
13495 BuildMI(mallocMBB, DL, TII->get(X86::ADD32ri), physSPReg).addReg(physSPReg)
13496 .addImm(16);
13497
13498 BuildMI(mallocMBB, DL, TII->get(TargetOpcode::COPY), mallocPtrVReg)
13499 .addReg(Is64Bit ? X86::RAX : X86::EAX);
13500 BuildMI(mallocMBB, DL, TII->get(X86::JMP_4)).addMBB(continueMBB);
13501
13502 // Set up the CFG correctly.
13503 BB->addSuccessor(bumpMBB);
13504 BB->addSuccessor(mallocMBB);
13505 mallocMBB->addSuccessor(continueMBB);
13506 bumpMBB->addSuccessor(continueMBB);
13507
13508 // Take care of the PHI nodes.
13509 BuildMI(*continueMBB, continueMBB->begin(), DL, TII->get(X86::PHI),
13510 MI->getOperand(0).getReg())
13511 .addReg(mallocPtrVReg).addMBB(mallocMBB)
13512 .addReg(bumpSPPtrVReg).addMBB(bumpMBB);
13513
13514 // Delete the original pseudo instruction.
13515 MI->eraseFromParent();
13516
13517 // And we're done.
13518 return continueMBB;
13519}
13520
13521MachineBasicBlock *
Michael J. Spencere9c253e2010-10-21 01:41:01 +000013522X86TargetLowering::EmitLoweredWinAlloca(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000013523 MachineBasicBlock *BB) const {
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000013524 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
13525 DebugLoc DL = MI->getDebugLoc();
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000013526
NAKAMURA Takumia2e07622011-03-24 07:07:00 +000013527 assert(!Subtarget->isTargetEnvMacho());
13528
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000013529 // The lowering is pretty easy: we're just emitting the call to _alloca. The
13530 // non-trivial part is impdef of ESP.
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000013531
NAKAMURA Takumia2e07622011-03-24 07:07:00 +000013532 if (Subtarget->isTargetWin64()) {
13533 if (Subtarget->isTargetCygMing()) {
13534 // ___chkstk(Mingw64):
13535 // Clobbers R10, R11, RAX and EFLAGS.
13536 // Updates RSP.
13537 BuildMI(*BB, MI, DL, TII->get(X86::W64ALLOCA))
13538 .addExternalSymbol("___chkstk")
13539 .addReg(X86::RAX, RegState::Implicit)
13540 .addReg(X86::RSP, RegState::Implicit)
13541 .addReg(X86::RAX, RegState::Define | RegState::Implicit)
13542 .addReg(X86::RSP, RegState::Define | RegState::Implicit)
13543 .addReg(X86::EFLAGS, RegState::Define | RegState::Implicit);
13544 } else {
13545 // __chkstk(MSVCRT): does not update stack pointer.
13546 // Clobbers R10, R11 and EFLAGS.
13547 // FIXME: RAX(allocated size) might be reused and not killed.
13548 BuildMI(*BB, MI, DL, TII->get(X86::W64ALLOCA))
13549 .addExternalSymbol("__chkstk")
13550 .addReg(X86::RAX, RegState::Implicit)
13551 .addReg(X86::EFLAGS, RegState::Define | RegState::Implicit);
13552 // RAX has the offset to subtracted from RSP.
13553 BuildMI(*BB, MI, DL, TII->get(X86::SUB64rr), X86::RSP)
13554 .addReg(X86::RSP)
13555 .addReg(X86::RAX);
13556 }
13557 } else {
13558 const char *StackProbeSymbol =
Michael J. Spencere9c253e2010-10-21 01:41:01 +000013559 Subtarget->isTargetWindows() ? "_chkstk" : "_alloca";
13560
NAKAMURA Takumia2e07622011-03-24 07:07:00 +000013561 BuildMI(*BB, MI, DL, TII->get(X86::CALLpcrel32))
13562 .addExternalSymbol(StackProbeSymbol)
13563 .addReg(X86::EAX, RegState::Implicit)
13564 .addReg(X86::ESP, RegState::Implicit)
13565 .addReg(X86::EAX, RegState::Define | RegState::Implicit)
13566 .addReg(X86::ESP, RegState::Define | RegState::Implicit)
13567 .addReg(X86::EFLAGS, RegState::Define | RegState::Implicit);
13568 }
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000013569
Dan Gohman14152b42010-07-06 20:24:04 +000013570 MI->eraseFromParent(); // The pseudo instruction is gone now.
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000013571 return BB;
13572}
Chris Lattner52600972009-09-02 05:57:00 +000013573
13574MachineBasicBlock *
Eric Christopher30ef0e52010-06-03 04:07:48 +000013575X86TargetLowering::EmitLoweredTLSCall(MachineInstr *MI,
13576 MachineBasicBlock *BB) const {
13577 // This is pretty easy. We're taking the value that we received from
13578 // our load from the relocation, sticking it in either RDI (x86-64)
13579 // or EAX and doing an indirect call. The return value will then
13580 // be in the normal return register.
Michael J. Spencerec38de22010-10-10 22:04:20 +000013581 const X86InstrInfo *TII
Eric Christopher54415362010-06-08 22:04:25 +000013582 = static_cast<const X86InstrInfo*>(getTargetMachine().getInstrInfo());
Eric Christopher30ef0e52010-06-03 04:07:48 +000013583 DebugLoc DL = MI->getDebugLoc();
13584 MachineFunction *F = BB->getParent();
Eric Christopher722d3152010-09-27 06:01:51 +000013585
13586 assert(Subtarget->isTargetDarwin() && "Darwin only instr emitted?");
Eric Christopher54415362010-06-08 22:04:25 +000013587 assert(MI->getOperand(3).isGlobal() && "This should be a global");
Michael J. Spencerec38de22010-10-10 22:04:20 +000013588
Jakob Stoklund Olesen8bcde2a2012-02-16 00:02:50 +000013589 // Get a register mask for the lowered call.
13590 // FIXME: The 32-bit calls have non-standard calling conventions. Use a
13591 // proper register mask.
13592 const uint32_t *RegMask =
13593 getTargetMachine().getRegisterInfo()->getCallPreservedMask(CallingConv::C);
Eric Christopher30ef0e52010-06-03 04:07:48 +000013594 if (Subtarget->is64Bit()) {
Dan Gohman14152b42010-07-06 20:24:04 +000013595 MachineInstrBuilder MIB = BuildMI(*BB, MI, DL,
13596 TII->get(X86::MOV64rm), X86::RDI)
Eric Christopher54415362010-06-08 22:04:25 +000013597 .addReg(X86::RIP)
13598 .addImm(0).addReg(0)
Michael J. Spencerec38de22010-10-10 22:04:20 +000013599 .addGlobalAddress(MI->getOperand(3).getGlobal(), 0,
Eric Christopher54415362010-06-08 22:04:25 +000013600 MI->getOperand(3).getTargetFlags())
13601 .addReg(0);
Eric Christopher722d3152010-09-27 06:01:51 +000013602 MIB = BuildMI(*BB, MI, DL, TII->get(X86::CALL64m));
Chris Lattner599b5312010-07-08 23:46:44 +000013603 addDirectMem(MIB, X86::RDI);
Jakob Stoklund Olesen8bcde2a2012-02-16 00:02:50 +000013604 MIB.addReg(X86::RAX, RegState::ImplicitDefine).addRegMask(RegMask);
Eric Christopher61025492010-06-15 23:08:42 +000013605 } else if (getTargetMachine().getRelocationModel() != Reloc::PIC_) {
Dan Gohman14152b42010-07-06 20:24:04 +000013606 MachineInstrBuilder MIB = BuildMI(*BB, MI, DL,
13607 TII->get(X86::MOV32rm), X86::EAX)
Eric Christopher61025492010-06-15 23:08:42 +000013608 .addReg(0)
13609 .addImm(0).addReg(0)
Michael J. Spencerec38de22010-10-10 22:04:20 +000013610 .addGlobalAddress(MI->getOperand(3).getGlobal(), 0,
Eric Christopher61025492010-06-15 23:08:42 +000013611 MI->getOperand(3).getTargetFlags())
13612 .addReg(0);
Dan Gohman14152b42010-07-06 20:24:04 +000013613 MIB = BuildMI(*BB, MI, DL, TII->get(X86::CALL32m));
Chris Lattner599b5312010-07-08 23:46:44 +000013614 addDirectMem(MIB, X86::EAX);
Jakob Stoklund Olesen8bcde2a2012-02-16 00:02:50 +000013615 MIB.addReg(X86::EAX, RegState::ImplicitDefine).addRegMask(RegMask);
Eric Christopher30ef0e52010-06-03 04:07:48 +000013616 } else {
Dan Gohman14152b42010-07-06 20:24:04 +000013617 MachineInstrBuilder MIB = BuildMI(*BB, MI, DL,
13618 TII->get(X86::MOV32rm), X86::EAX)
Eric Christopher54415362010-06-08 22:04:25 +000013619 .addReg(TII->getGlobalBaseReg(F))
13620 .addImm(0).addReg(0)
Michael J. Spencerec38de22010-10-10 22:04:20 +000013621 .addGlobalAddress(MI->getOperand(3).getGlobal(), 0,
Eric Christopher54415362010-06-08 22:04:25 +000013622 MI->getOperand(3).getTargetFlags())
13623 .addReg(0);
Dan Gohman14152b42010-07-06 20:24:04 +000013624 MIB = BuildMI(*BB, MI, DL, TII->get(X86::CALL32m));
Chris Lattner599b5312010-07-08 23:46:44 +000013625 addDirectMem(MIB, X86::EAX);
Jakob Stoklund Olesen8bcde2a2012-02-16 00:02:50 +000013626 MIB.addReg(X86::EAX, RegState::ImplicitDefine).addRegMask(RegMask);
Eric Christopher30ef0e52010-06-03 04:07:48 +000013627 }
Michael J. Spencerec38de22010-10-10 22:04:20 +000013628
Dan Gohman14152b42010-07-06 20:24:04 +000013629 MI->eraseFromParent(); // The pseudo instruction is gone now.
Eric Christopher30ef0e52010-06-03 04:07:48 +000013630 return BB;
13631}
13632
13633MachineBasicBlock *
Michael Liao6c0e04c2012-10-15 22:39:43 +000013634X86TargetLowering::emitEHSjLjSetJmp(MachineInstr *MI,
13635 MachineBasicBlock *MBB) const {
13636 DebugLoc DL = MI->getDebugLoc();
13637 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
13638
13639 MachineFunction *MF = MBB->getParent();
13640 MachineRegisterInfo &MRI = MF->getRegInfo();
13641
13642 const BasicBlock *BB = MBB->getBasicBlock();
13643 MachineFunction::iterator I = MBB;
13644 ++I;
13645
13646 // Memory Reference
13647 MachineInstr::mmo_iterator MMOBegin = MI->memoperands_begin();
13648 MachineInstr::mmo_iterator MMOEnd = MI->memoperands_end();
13649
13650 unsigned DstReg;
13651 unsigned MemOpndSlot = 0;
13652
13653 unsigned CurOp = 0;
13654
13655 DstReg = MI->getOperand(CurOp++).getReg();
13656 const TargetRegisterClass *RC = MRI.getRegClass(DstReg);
13657 assert(RC->hasType(MVT::i32) && "Invalid destination!");
13658 unsigned mainDstReg = MRI.createVirtualRegister(RC);
13659 unsigned restoreDstReg = MRI.createVirtualRegister(RC);
13660
13661 MemOpndSlot = CurOp;
13662
13663 MVT PVT = getPointerTy();
13664 assert((PVT == MVT::i64 || PVT == MVT::i32) &&
13665 "Invalid Pointer Size!");
13666
13667 // For v = setjmp(buf), we generate
13668 //
13669 // thisMBB:
Michael Liao281ae5a2012-10-17 02:22:27 +000013670 // buf[LabelOffset] = restoreMBB
Michael Liao6c0e04c2012-10-15 22:39:43 +000013671 // SjLjSetup restoreMBB
13672 //
13673 // mainMBB:
13674 // v_main = 0
13675 //
13676 // sinkMBB:
13677 // v = phi(main, restore)
13678 //
13679 // restoreMBB:
13680 // v_restore = 1
13681
13682 MachineBasicBlock *thisMBB = MBB;
13683 MachineBasicBlock *mainMBB = MF->CreateMachineBasicBlock(BB);
13684 MachineBasicBlock *sinkMBB = MF->CreateMachineBasicBlock(BB);
13685 MachineBasicBlock *restoreMBB = MF->CreateMachineBasicBlock(BB);
13686 MF->insert(I, mainMBB);
13687 MF->insert(I, sinkMBB);
13688 MF->push_back(restoreMBB);
13689
13690 MachineInstrBuilder MIB;
13691
13692 // Transfer the remainder of BB and its successor edges to sinkMBB.
13693 sinkMBB->splice(sinkMBB->begin(), MBB,
13694 llvm::next(MachineBasicBlock::iterator(MI)), MBB->end());
13695 sinkMBB->transferSuccessorsAndUpdatePHIs(MBB);
13696
13697 // thisMBB:
Michael Liao281ae5a2012-10-17 02:22:27 +000013698 unsigned PtrStoreOpc = 0;
13699 unsigned LabelReg = 0;
13700 const int64_t LabelOffset = 1 * PVT.getStoreSize();
13701 Reloc::Model RM = getTargetMachine().getRelocationModel();
13702 bool UseImmLabel = (getTargetMachine().getCodeModel() == CodeModel::Small) &&
13703 (RM == Reloc::Static || RM == Reloc::DynamicNoPIC);
Michael Liao6c0e04c2012-10-15 22:39:43 +000013704
Michael Liao281ae5a2012-10-17 02:22:27 +000013705 // Prepare IP either in reg or imm.
13706 if (!UseImmLabel) {
13707 PtrStoreOpc = (PVT == MVT::i64) ? X86::MOV64mr : X86::MOV32mr;
13708 const TargetRegisterClass *PtrRC = getRegClassFor(PVT);
13709 LabelReg = MRI.createVirtualRegister(PtrRC);
13710 if (Subtarget->is64Bit()) {
13711 MIB = BuildMI(*thisMBB, MI, DL, TII->get(X86::LEA64r), LabelReg)
13712 .addReg(X86::RIP)
13713 .addImm(0)
13714 .addReg(0)
13715 .addMBB(restoreMBB)
13716 .addReg(0);
13717 } else {
13718 const X86InstrInfo *XII = static_cast<const X86InstrInfo*>(TII);
13719 MIB = BuildMI(*thisMBB, MI, DL, TII->get(X86::LEA32r), LabelReg)
13720 .addReg(XII->getGlobalBaseReg(MF))
13721 .addImm(0)
13722 .addReg(0)
13723 .addMBB(restoreMBB, Subtarget->ClassifyBlockAddressReference())
13724 .addReg(0);
13725 }
13726 } else
13727 PtrStoreOpc = (PVT == MVT::i64) ? X86::MOV64mi32 : X86::MOV32mi;
Michael Liao6c0e04c2012-10-15 22:39:43 +000013728 // Store IP
Michael Liao281ae5a2012-10-17 02:22:27 +000013729 MIB = BuildMI(*thisMBB, MI, DL, TII->get(PtrStoreOpc));
Michael Liao6c0e04c2012-10-15 22:39:43 +000013730 for (unsigned i = 0; i < X86::AddrNumOperands; ++i) {
13731 if (i == X86::AddrDisp)
Michael Liao281ae5a2012-10-17 02:22:27 +000013732 MIB.addDisp(MI->getOperand(MemOpndSlot + i), LabelOffset);
Michael Liao6c0e04c2012-10-15 22:39:43 +000013733 else
13734 MIB.addOperand(MI->getOperand(MemOpndSlot + i));
13735 }
Michael Liao281ae5a2012-10-17 02:22:27 +000013736 if (!UseImmLabel)
13737 MIB.addReg(LabelReg);
13738 else
13739 MIB.addMBB(restoreMBB);
Michael Liao6c0e04c2012-10-15 22:39:43 +000013740 MIB.setMemRefs(MMOBegin, MMOEnd);
13741 // Setup
13742 MIB = BuildMI(*thisMBB, MI, DL, TII->get(X86::EH_SjLj_Setup))
13743 .addMBB(restoreMBB);
13744 MIB.addRegMask(RegInfo->getNoPreservedMask());
13745 thisMBB->addSuccessor(mainMBB);
13746 thisMBB->addSuccessor(restoreMBB);
13747
13748 // mainMBB:
13749 // EAX = 0
13750 BuildMI(mainMBB, DL, TII->get(X86::MOV32r0), mainDstReg);
13751 mainMBB->addSuccessor(sinkMBB);
13752
13753 // sinkMBB:
13754 BuildMI(*sinkMBB, sinkMBB->begin(), DL,
13755 TII->get(X86::PHI), DstReg)
13756 .addReg(mainDstReg).addMBB(mainMBB)
13757 .addReg(restoreDstReg).addMBB(restoreMBB);
13758
13759 // restoreMBB:
13760 BuildMI(restoreMBB, DL, TII->get(X86::MOV32ri), restoreDstReg).addImm(1);
13761 BuildMI(restoreMBB, DL, TII->get(X86::JMP_4)).addMBB(sinkMBB);
13762 restoreMBB->addSuccessor(sinkMBB);
13763
13764 MI->eraseFromParent();
13765 return sinkMBB;
13766}
13767
13768MachineBasicBlock *
13769X86TargetLowering::emitEHSjLjLongJmp(MachineInstr *MI,
13770 MachineBasicBlock *MBB) const {
13771 DebugLoc DL = MI->getDebugLoc();
13772 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
13773
13774 MachineFunction *MF = MBB->getParent();
13775 MachineRegisterInfo &MRI = MF->getRegInfo();
13776
13777 // Memory Reference
13778 MachineInstr::mmo_iterator MMOBegin = MI->memoperands_begin();
13779 MachineInstr::mmo_iterator MMOEnd = MI->memoperands_end();
13780
13781 MVT PVT = getPointerTy();
13782 assert((PVT == MVT::i64 || PVT == MVT::i32) &&
13783 "Invalid Pointer Size!");
13784
13785 const TargetRegisterClass *RC =
13786 (PVT == MVT::i64) ? &X86::GR64RegClass : &X86::GR32RegClass;
13787 unsigned Tmp = MRI.createVirtualRegister(RC);
13788 // Since FP is only updated here but NOT referenced, it's treated as GPR.
13789 unsigned FP = (PVT == MVT::i64) ? X86::RBP : X86::EBP;
13790 unsigned SP = RegInfo->getStackRegister();
13791
13792 MachineInstrBuilder MIB;
13793
Michael Liao281ae5a2012-10-17 02:22:27 +000013794 const int64_t LabelOffset = 1 * PVT.getStoreSize();
13795 const int64_t SPOffset = 2 * PVT.getStoreSize();
Michael Liao6c0e04c2012-10-15 22:39:43 +000013796
13797 unsigned PtrLoadOpc = (PVT == MVT::i64) ? X86::MOV64rm : X86::MOV32rm;
13798 unsigned IJmpOpc = (PVT == MVT::i64) ? X86::JMP64r : X86::JMP32r;
13799
13800 // Reload FP
13801 MIB = BuildMI(*MBB, MI, DL, TII->get(PtrLoadOpc), FP);
13802 for (unsigned i = 0; i < X86::AddrNumOperands; ++i)
13803 MIB.addOperand(MI->getOperand(i));
13804 MIB.setMemRefs(MMOBegin, MMOEnd);
13805 // Reload IP
13806 MIB = BuildMI(*MBB, MI, DL, TII->get(PtrLoadOpc), Tmp);
13807 for (unsigned i = 0; i < X86::AddrNumOperands; ++i) {
13808 if (i == X86::AddrDisp)
Michael Liao281ae5a2012-10-17 02:22:27 +000013809 MIB.addDisp(MI->getOperand(i), LabelOffset);
Michael Liao6c0e04c2012-10-15 22:39:43 +000013810 else
13811 MIB.addOperand(MI->getOperand(i));
13812 }
13813 MIB.setMemRefs(MMOBegin, MMOEnd);
13814 // Reload SP
13815 MIB = BuildMI(*MBB, MI, DL, TII->get(PtrLoadOpc), SP);
13816 for (unsigned i = 0; i < X86::AddrNumOperands; ++i) {
13817 if (i == X86::AddrDisp)
Michael Liao281ae5a2012-10-17 02:22:27 +000013818 MIB.addDisp(MI->getOperand(i), SPOffset);
Michael Liao6c0e04c2012-10-15 22:39:43 +000013819 else
13820 MIB.addOperand(MI->getOperand(i));
13821 }
13822 MIB.setMemRefs(MMOBegin, MMOEnd);
13823 // Jump
13824 BuildMI(*MBB, MI, DL, TII->get(IJmpOpc)).addReg(Tmp);
13825
13826 MI->eraseFromParent();
13827 return MBB;
13828}
13829
13830MachineBasicBlock *
Evan Chengff9b3732008-01-30 18:18:23 +000013831X86TargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000013832 MachineBasicBlock *BB) const {
Evan Cheng60c07e12006-07-05 22:17:51 +000013833 switch (MI->getOpcode()) {
Craig Topperabb94d02012-02-05 03:43:23 +000013834 default: llvm_unreachable("Unexpected instr type to insert");
NAKAMURA Takumi7754f852011-01-26 02:04:09 +000013835 case X86::TAILJMPd64:
13836 case X86::TAILJMPr64:
13837 case X86::TAILJMPm64:
Craig Topper6d1263a2012-02-05 05:38:58 +000013838 llvm_unreachable("TAILJMP64 would not be touched here.");
NAKAMURA Takumi7754f852011-01-26 02:04:09 +000013839 case X86::TCRETURNdi64:
13840 case X86::TCRETURNri64:
13841 case X86::TCRETURNmi64:
NAKAMURA Takumi7754f852011-01-26 02:04:09 +000013842 return BB;
Michael J. Spencere9c253e2010-10-21 01:41:01 +000013843 case X86::WIN_ALLOCA:
13844 return EmitLoweredWinAlloca(MI, BB);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000013845 case X86::SEG_ALLOCA_32:
13846 return EmitLoweredSegAlloca(MI, BB, false);
13847 case X86::SEG_ALLOCA_64:
13848 return EmitLoweredSegAlloca(MI, BB, true);
Eric Christopher30ef0e52010-06-03 04:07:48 +000013849 case X86::TLSCall_32:
13850 case X86::TLSCall_64:
13851 return EmitLoweredTLSCall(MI, BB);
Dan Gohmancbbea0f2009-08-27 00:14:12 +000013852 case X86::CMOV_GR8:
Evan Cheng60c07e12006-07-05 22:17:51 +000013853 case X86::CMOV_FR32:
13854 case X86::CMOV_FR64:
13855 case X86::CMOV_V4F32:
13856 case X86::CMOV_V2F64:
Chris Lattner52600972009-09-02 05:57:00 +000013857 case X86::CMOV_V2I64:
Bruno Cardoso Lopesd40aa242011-08-09 23:27:13 +000013858 case X86::CMOV_V8F32:
13859 case X86::CMOV_V4F64:
13860 case X86::CMOV_V4I64:
Chris Lattner314a1132010-03-14 18:31:44 +000013861 case X86::CMOV_GR16:
13862 case X86::CMOV_GR32:
13863 case X86::CMOV_RFP32:
13864 case X86::CMOV_RFP64:
13865 case X86::CMOV_RFP80:
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000013866 return EmitLoweredSelect(MI, BB);
Evan Cheng60c07e12006-07-05 22:17:51 +000013867
Dale Johannesen849f2142007-07-03 00:53:03 +000013868 case X86::FP32_TO_INT16_IN_MEM:
13869 case X86::FP32_TO_INT32_IN_MEM:
13870 case X86::FP32_TO_INT64_IN_MEM:
13871 case X86::FP64_TO_INT16_IN_MEM:
13872 case X86::FP64_TO_INT32_IN_MEM:
Dale Johannesena996d522007-08-07 01:17:37 +000013873 case X86::FP64_TO_INT64_IN_MEM:
13874 case X86::FP80_TO_INT16_IN_MEM:
13875 case X86::FP80_TO_INT32_IN_MEM:
13876 case X86::FP80_TO_INT64_IN_MEM: {
Chris Lattner52600972009-09-02 05:57:00 +000013877 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
13878 DebugLoc DL = MI->getDebugLoc();
13879
Evan Cheng60c07e12006-07-05 22:17:51 +000013880 // Change the floating point control register to use "round towards zero"
13881 // mode when truncating to an integer value.
13882 MachineFunction *F = BB->getParent();
David Greene3f2bf852009-11-12 20:49:22 +000013883 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2, false);
Dan Gohman14152b42010-07-06 20:24:04 +000013884 addFrameReference(BuildMI(*BB, MI, DL,
13885 TII->get(X86::FNSTCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000013886
13887 // Load the old value of the high byte of the control word...
13888 unsigned OldCW =
Craig Topperc9099502012-04-20 06:31:50 +000013889 F->getRegInfo().createVirtualRegister(&X86::GR16RegClass);
Dan Gohman14152b42010-07-06 20:24:04 +000013890 addFrameReference(BuildMI(*BB, MI, DL, TII->get(X86::MOV16rm), OldCW),
Dale Johannesene4d209d2009-02-03 20:21:25 +000013891 CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000013892
13893 // Set the high part to be round to zero...
Dan Gohman14152b42010-07-06 20:24:04 +000013894 addFrameReference(BuildMI(*BB, MI, DL, TII->get(X86::MOV16mi)), CWFrameIdx)
Evan Chengc0f64ff2006-11-27 23:37:22 +000013895 .addImm(0xC7F);
Evan Cheng60c07e12006-07-05 22:17:51 +000013896
13897 // Reload the modified control word now...
Dan Gohman14152b42010-07-06 20:24:04 +000013898 addFrameReference(BuildMI(*BB, MI, DL,
13899 TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000013900
13901 // Restore the memory image of control word to original value
Dan Gohman14152b42010-07-06 20:24:04 +000013902 addFrameReference(BuildMI(*BB, MI, DL, TII->get(X86::MOV16mr)), CWFrameIdx)
Evan Chengc0f64ff2006-11-27 23:37:22 +000013903 .addReg(OldCW);
Evan Cheng60c07e12006-07-05 22:17:51 +000013904
13905 // Get the X86 opcode to use.
13906 unsigned Opc;
13907 switch (MI->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +000013908 default: llvm_unreachable("illegal opcode!");
Dale Johannesene377d4d2007-07-04 21:07:47 +000013909 case X86::FP32_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m32; break;
13910 case X86::FP32_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m32; break;
13911 case X86::FP32_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m32; break;
13912 case X86::FP64_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m64; break;
13913 case X86::FP64_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m64; break;
13914 case X86::FP64_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m64; break;
Dale Johannesena996d522007-08-07 01:17:37 +000013915 case X86::FP80_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m80; break;
13916 case X86::FP80_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m80; break;
13917 case X86::FP80_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m80; break;
Evan Cheng60c07e12006-07-05 22:17:51 +000013918 }
13919
13920 X86AddressMode AM;
13921 MachineOperand &Op = MI->getOperand(0);
Dan Gohmand735b802008-10-03 15:45:36 +000013922 if (Op.isReg()) {
Evan Cheng60c07e12006-07-05 22:17:51 +000013923 AM.BaseType = X86AddressMode::RegBase;
13924 AM.Base.Reg = Op.getReg();
13925 } else {
13926 AM.BaseType = X86AddressMode::FrameIndexBase;
Chris Lattner8aa797a2007-12-30 23:10:15 +000013927 AM.Base.FrameIndex = Op.getIndex();
Evan Cheng60c07e12006-07-05 22:17:51 +000013928 }
13929 Op = MI->getOperand(1);
Dan Gohmand735b802008-10-03 15:45:36 +000013930 if (Op.isImm())
Chris Lattner7fbe9722006-10-20 17:42:20 +000013931 AM.Scale = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +000013932 Op = MI->getOperand(2);
Dan Gohmand735b802008-10-03 15:45:36 +000013933 if (Op.isImm())
Chris Lattner7fbe9722006-10-20 17:42:20 +000013934 AM.IndexReg = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +000013935 Op = MI->getOperand(3);
Dan Gohmand735b802008-10-03 15:45:36 +000013936 if (Op.isGlobal()) {
Evan Cheng60c07e12006-07-05 22:17:51 +000013937 AM.GV = Op.getGlobal();
13938 } else {
Chris Lattner7fbe9722006-10-20 17:42:20 +000013939 AM.Disp = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +000013940 }
Dan Gohman14152b42010-07-06 20:24:04 +000013941 addFullAddress(BuildMI(*BB, MI, DL, TII->get(Opc)), AM)
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000013942 .addReg(MI->getOperand(X86::AddrNumOperands).getReg());
Evan Cheng60c07e12006-07-05 22:17:51 +000013943
13944 // Reload the original control word now.
Dan Gohman14152b42010-07-06 20:24:04 +000013945 addFrameReference(BuildMI(*BB, MI, DL,
13946 TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000013947
Dan Gohman14152b42010-07-06 20:24:04 +000013948 MI->eraseFromParent(); // The pseudo instruction is gone now.
Evan Cheng60c07e12006-07-05 22:17:51 +000013949 return BB;
13950 }
Eric Christopherb120ab42009-08-18 22:50:32 +000013951 // String/text processing lowering.
13952 case X86::PCMPISTRM128REG:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000013953 case X86::VPCMPISTRM128REG:
Eric Christopherb120ab42009-08-18 22:50:32 +000013954 case X86::PCMPISTRM128MEM:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000013955 case X86::VPCMPISTRM128MEM:
Eric Christopherb120ab42009-08-18 22:50:32 +000013956 case X86::PCMPESTRM128REG:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000013957 case X86::VPCMPESTRM128REG:
Eric Christopherb120ab42009-08-18 22:50:32 +000013958 case X86::PCMPESTRM128MEM:
Craig Topper8aae8dd2012-11-10 08:57:41 +000013959 case X86::VPCMPESTRM128MEM:
13960 assert(Subtarget->hasSSE42() &&
13961 "Target must have SSE4.2 or AVX features enabled");
13962 return EmitPCMPSTRM(MI, BB, getTargetMachine().getInstrInfo());
Craig Topper9c7ae012012-11-10 01:23:36 +000013963
13964 // String/text processing lowering.
13965 case X86::PCMPISTRIREG:
13966 case X86::VPCMPISTRIREG:
13967 case X86::PCMPISTRIMEM:
13968 case X86::VPCMPISTRIMEM:
13969 case X86::PCMPESTRIREG:
13970 case X86::VPCMPESTRIREG:
13971 case X86::PCMPESTRIMEM:
Craig Topper8aae8dd2012-11-10 08:57:41 +000013972 case X86::VPCMPESTRIMEM:
13973 assert(Subtarget->hasSSE42() &&
13974 "Target must have SSE4.2 or AVX features enabled");
13975 return EmitPCMPSTRI(MI, BB, getTargetMachine().getInstrInfo());
Eric Christopherb120ab42009-08-18 22:50:32 +000013976
Craig Topper8aae8dd2012-11-10 08:57:41 +000013977 // Thread synchronization.
Eric Christopher228232b2010-11-30 07:20:12 +000013978 case X86::MONITOR:
Craig Topper2da36912012-11-11 22:45:02 +000013979 return EmitMonitor(MI, BB, getTargetMachine().getInstrInfo(), Subtarget);
Eric Christopher228232b2010-11-30 07:20:12 +000013980
Michael Liaobe02a902012-11-08 07:28:54 +000013981 // xbegin
13982 case X86::XBEGIN:
Craig Topper2da36912012-11-11 22:45:02 +000013983 return EmitXBegin(MI, BB, getTargetMachine().getInstrInfo());
Michael Liaobe02a902012-11-08 07:28:54 +000013984
Craig Topper8aae8dd2012-11-10 08:57:41 +000013985 // Atomic Lowering.
Dale Johannesen140be2d2008-08-19 18:47:28 +000013986 case X86::ATOMAND8:
Michael Liaob118a072012-09-20 03:06:15 +000013987 case X86::ATOMAND16:
13988 case X86::ATOMAND32:
Dale Johannesena99e3842008-08-20 00:48:50 +000013989 case X86::ATOMAND64:
Michael Liaob118a072012-09-20 03:06:15 +000013990 // Fall through
13991 case X86::ATOMOR8:
13992 case X86::ATOMOR16:
13993 case X86::ATOMOR32:
Dale Johannesena99e3842008-08-20 00:48:50 +000013994 case X86::ATOMOR64:
Michael Liaob118a072012-09-20 03:06:15 +000013995 // Fall through
13996 case X86::ATOMXOR16:
13997 case X86::ATOMXOR8:
13998 case X86::ATOMXOR32:
Dale Johannesena99e3842008-08-20 00:48:50 +000013999 case X86::ATOMXOR64:
Michael Liaob118a072012-09-20 03:06:15 +000014000 // Fall through
14001 case X86::ATOMNAND8:
14002 case X86::ATOMNAND16:
14003 case X86::ATOMNAND32:
14004 case X86::ATOMNAND64:
14005 // Fall through
Michael Liaofe87c302012-09-21 03:18:52 +000014006 case X86::ATOMMAX8:
Michael Liaob118a072012-09-20 03:06:15 +000014007 case X86::ATOMMAX16:
14008 case X86::ATOMMAX32:
14009 case X86::ATOMMAX64:
14010 // Fall through
Michael Liaofe87c302012-09-21 03:18:52 +000014011 case X86::ATOMMIN8:
Michael Liaob118a072012-09-20 03:06:15 +000014012 case X86::ATOMMIN16:
14013 case X86::ATOMMIN32:
14014 case X86::ATOMMIN64:
14015 // Fall through
Michael Liaofe87c302012-09-21 03:18:52 +000014016 case X86::ATOMUMAX8:
Michael Liaob118a072012-09-20 03:06:15 +000014017 case X86::ATOMUMAX16:
14018 case X86::ATOMUMAX32:
14019 case X86::ATOMUMAX64:
14020 // Fall through
Michael Liaofe87c302012-09-21 03:18:52 +000014021 case X86::ATOMUMIN8:
Michael Liaob118a072012-09-20 03:06:15 +000014022 case X86::ATOMUMIN16:
14023 case X86::ATOMUMIN32:
14024 case X86::ATOMUMIN64:
14025 return EmitAtomicLoadArith(MI, BB);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000014026
14027 // This group does 64-bit operations on a 32-bit host.
14028 case X86::ATOMAND6432:
Dale Johannesen48c1bc22008-10-02 18:53:47 +000014029 case X86::ATOMOR6432:
Dale Johannesen48c1bc22008-10-02 18:53:47 +000014030 case X86::ATOMXOR6432:
Dale Johannesen48c1bc22008-10-02 18:53:47 +000014031 case X86::ATOMNAND6432:
Dale Johannesen48c1bc22008-10-02 18:53:47 +000014032 case X86::ATOMADD6432:
Dale Johannesen48c1bc22008-10-02 18:53:47 +000014033 case X86::ATOMSUB6432:
Michael Liaoe5e8f762012-09-25 18:08:13 +000014034 case X86::ATOMMAX6432:
14035 case X86::ATOMMIN6432:
14036 case X86::ATOMUMAX6432:
14037 case X86::ATOMUMIN6432:
Michael Liaob118a072012-09-20 03:06:15 +000014038 case X86::ATOMSWAP6432:
14039 return EmitAtomicLoadArith6432(MI, BB);
Craig Topperacaaa6f2012-08-18 06:39:34 +000014040
Dan Gohmand6708ea2009-08-15 01:38:56 +000014041 case X86::VASTART_SAVE_XMM_REGS:
14042 return EmitVAStartSaveXMMRegsWithCustomInserter(MI, BB);
Dan Gohman320afb82010-10-12 18:00:49 +000014043
14044 case X86::VAARG_64:
14045 return EmitVAARG64WithCustomInserter(MI, BB);
Michael Liao6c0e04c2012-10-15 22:39:43 +000014046
14047 case X86::EH_SjLj_SetJmp32:
14048 case X86::EH_SjLj_SetJmp64:
14049 return emitEHSjLjSetJmp(MI, BB);
14050
14051 case X86::EH_SjLj_LongJmp32:
14052 case X86::EH_SjLj_LongJmp64:
14053 return emitEHSjLjLongJmp(MI, BB);
Evan Cheng60c07e12006-07-05 22:17:51 +000014054 }
14055}
14056
14057//===----------------------------------------------------------------------===//
14058// X86 Optimization Hooks
14059//===----------------------------------------------------------------------===//
14060
Dan Gohman475871a2008-07-27 21:46:04 +000014061void X86TargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
Dan Gohmanfd29e0e2008-02-13 00:35:47 +000014062 APInt &KnownZero,
14063 APInt &KnownOne,
Dan Gohmanea859be2007-06-22 14:59:07 +000014064 const SelectionDAG &DAG,
Nate Begeman368e18d2006-02-16 21:11:51 +000014065 unsigned Depth) const {
Rafael Espindola26c8dcc2012-04-04 12:51:34 +000014066 unsigned BitWidth = KnownZero.getBitWidth();
Evan Cheng3a03ebb2005-12-21 23:05:39 +000014067 unsigned Opc = Op.getOpcode();
Evan Cheng865f0602006-04-05 06:11:20 +000014068 assert((Opc >= ISD::BUILTIN_OP_END ||
14069 Opc == ISD::INTRINSIC_WO_CHAIN ||
14070 Opc == ISD::INTRINSIC_W_CHAIN ||
14071 Opc == ISD::INTRINSIC_VOID) &&
14072 "Should use MaskedValueIsZero if you don't know whether Op"
14073 " is a target node!");
Evan Cheng3a03ebb2005-12-21 23:05:39 +000014074
Rafael Espindola26c8dcc2012-04-04 12:51:34 +000014075 KnownZero = KnownOne = APInt(BitWidth, 0); // Don't know anything.
Evan Cheng3a03ebb2005-12-21 23:05:39 +000014076 switch (Opc) {
Evan Cheng865f0602006-04-05 06:11:20 +000014077 default: break;
Evan Cheng97d0e0e2009-02-02 09:15:04 +000014078 case X86ISD::ADD:
14079 case X86ISD::SUB:
Chris Lattner5b856542010-12-20 00:59:46 +000014080 case X86ISD::ADC:
14081 case X86ISD::SBB:
Evan Cheng97d0e0e2009-02-02 09:15:04 +000014082 case X86ISD::SMUL:
14083 case X86ISD::UMUL:
Dan Gohman076aee32009-03-04 19:44:21 +000014084 case X86ISD::INC:
14085 case X86ISD::DEC:
Dan Gohmane220c4b2009-09-18 19:59:53 +000014086 case X86ISD::OR:
14087 case X86ISD::XOR:
14088 case X86ISD::AND:
Evan Cheng97d0e0e2009-02-02 09:15:04 +000014089 // These nodes' second result is a boolean.
14090 if (Op.getResNo() == 0)
14091 break;
14092 // Fallthrough
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000014093 case X86ISD::SETCC:
Rafael Espindola26c8dcc2012-04-04 12:51:34 +000014094 KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - 1);
Nate Begeman368e18d2006-02-16 21:11:51 +000014095 break;
Evan Cheng7c1780c2011-10-07 17:21:44 +000014096 case ISD::INTRINSIC_WO_CHAIN: {
14097 unsigned IntId = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
14098 unsigned NumLoBits = 0;
14099 switch (IntId) {
14100 default: break;
14101 case Intrinsic::x86_sse_movmsk_ps:
14102 case Intrinsic::x86_avx_movmsk_ps_256:
14103 case Intrinsic::x86_sse2_movmsk_pd:
14104 case Intrinsic::x86_avx_movmsk_pd_256:
14105 case Intrinsic::x86_mmx_pmovmskb:
Craig Topper3738ccd2011-12-27 06:27:23 +000014106 case Intrinsic::x86_sse2_pmovmskb_128:
14107 case Intrinsic::x86_avx2_pmovmskb: {
Evan Cheng7c1780c2011-10-07 17:21:44 +000014108 // High bits of movmskp{s|d}, pmovmskb are known zero.
14109 switch (IntId) {
Craig Topperabb94d02012-02-05 03:43:23 +000014110 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
Evan Cheng7c1780c2011-10-07 17:21:44 +000014111 case Intrinsic::x86_sse_movmsk_ps: NumLoBits = 4; break;
14112 case Intrinsic::x86_avx_movmsk_ps_256: NumLoBits = 8; break;
14113 case Intrinsic::x86_sse2_movmsk_pd: NumLoBits = 2; break;
14114 case Intrinsic::x86_avx_movmsk_pd_256: NumLoBits = 4; break;
14115 case Intrinsic::x86_mmx_pmovmskb: NumLoBits = 8; break;
14116 case Intrinsic::x86_sse2_pmovmskb_128: NumLoBits = 16; break;
Craig Topper3738ccd2011-12-27 06:27:23 +000014117 case Intrinsic::x86_avx2_pmovmskb: NumLoBits = 32; break;
Evan Cheng7c1780c2011-10-07 17:21:44 +000014118 }
Rafael Espindola26c8dcc2012-04-04 12:51:34 +000014119 KnownZero = APInt::getHighBitsSet(BitWidth, BitWidth - NumLoBits);
Evan Cheng7c1780c2011-10-07 17:21:44 +000014120 break;
14121 }
14122 }
14123 break;
14124 }
Evan Cheng3a03ebb2005-12-21 23:05:39 +000014125 }
Evan Cheng3a03ebb2005-12-21 23:05:39 +000014126}
Chris Lattner259e97c2006-01-31 19:43:35 +000014127
Owen Andersonbc146b02010-09-21 20:42:50 +000014128unsigned X86TargetLowering::ComputeNumSignBitsForTargetNode(SDValue Op,
14129 unsigned Depth) const {
14130 // SETCC_CARRY sets the dest to ~0 for true or 0 for false.
14131 if (Op.getOpcode() == X86ISD::SETCC_CARRY)
14132 return Op.getValueType().getScalarType().getSizeInBits();
Michael J. Spencerec38de22010-10-10 22:04:20 +000014133
Owen Andersonbc146b02010-09-21 20:42:50 +000014134 // Fallback case.
14135 return 1;
14136}
14137
Evan Cheng206ee9d2006-07-07 08:33:52 +000014138/// isGAPlusOffset - Returns true (and the GlobalValue and the offset) if the
Evan Chengad4196b2008-05-12 19:56:52 +000014139/// node is a GlobalAddress + offset.
14140bool X86TargetLowering::isGAPlusOffset(SDNode *N,
Dan Gohman46510a72010-04-15 01:51:59 +000014141 const GlobalValue* &GA,
14142 int64_t &Offset) const {
Evan Chengad4196b2008-05-12 19:56:52 +000014143 if (N->getOpcode() == X86ISD::Wrapper) {
14144 if (isa<GlobalAddressSDNode>(N->getOperand(0))) {
Evan Cheng206ee9d2006-07-07 08:33:52 +000014145 GA = cast<GlobalAddressSDNode>(N->getOperand(0))->getGlobal();
Dan Gohman6520e202008-10-18 02:06:02 +000014146 Offset = cast<GlobalAddressSDNode>(N->getOperand(0))->getOffset();
Evan Cheng206ee9d2006-07-07 08:33:52 +000014147 return true;
14148 }
Evan Cheng206ee9d2006-07-07 08:33:52 +000014149 }
Evan Chengad4196b2008-05-12 19:56:52 +000014150 return TargetLowering::isGAPlusOffset(N, GA, Offset);
Evan Cheng206ee9d2006-07-07 08:33:52 +000014151}
14152
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000014153/// isShuffleHigh128VectorInsertLow - Checks whether the shuffle node is the
14154/// same as extracting the high 128-bit part of 256-bit vector and then
14155/// inserting the result into the low part of a new 256-bit vector
14156static bool isShuffleHigh128VectorInsertLow(ShuffleVectorSDNode *SVOp) {
14157 EVT VT = SVOp->getValueType(0);
Craig Topper66ddd152012-04-27 22:54:43 +000014158 unsigned NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000014159
14160 // vector_shuffle <4, 5, 6, 7, u, u, u, u> or <2, 3, u, u>
Craig Topper66ddd152012-04-27 22:54:43 +000014161 for (unsigned i = 0, j = NumElems/2; i != NumElems/2; ++i, ++j)
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000014162 if (!isUndefOrEqual(SVOp->getMaskElt(i), j) ||
14163 SVOp->getMaskElt(j) >= 0)
14164 return false;
14165
14166 return true;
14167}
14168
14169/// isShuffleLow128VectorInsertHigh - Checks whether the shuffle node is the
14170/// same as extracting the low 128-bit part of 256-bit vector and then
14171/// inserting the result into the high part of a new 256-bit vector
14172static bool isShuffleLow128VectorInsertHigh(ShuffleVectorSDNode *SVOp) {
14173 EVT VT = SVOp->getValueType(0);
Craig Topper66ddd152012-04-27 22:54:43 +000014174 unsigned NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000014175
14176 // vector_shuffle <u, u, u, u, 0, 1, 2, 3> or <u, u, 0, 1>
Craig Topper66ddd152012-04-27 22:54:43 +000014177 for (unsigned i = NumElems/2, j = 0; i != NumElems; ++i, ++j)
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000014178 if (!isUndefOrEqual(SVOp->getMaskElt(i), j) ||
14179 SVOp->getMaskElt(j) >= 0)
14180 return false;
14181
14182 return true;
14183}
14184
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000014185/// PerformShuffleCombine256 - Performs shuffle combines for 256-bit vectors.
14186static SDValue PerformShuffleCombine256(SDNode *N, SelectionDAG &DAG,
Craig Topper12216172012-01-13 08:12:35 +000014187 TargetLowering::DAGCombinerInfo &DCI,
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +000014188 const X86Subtarget* Subtarget) {
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000014189 DebugLoc dl = N->getDebugLoc();
14190 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
14191 SDValue V1 = SVOp->getOperand(0);
14192 SDValue V2 = SVOp->getOperand(1);
14193 EVT VT = SVOp->getValueType(0);
Craig Topper66ddd152012-04-27 22:54:43 +000014194 unsigned NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000014195
14196 if (V1.getOpcode() == ISD::CONCAT_VECTORS &&
14197 V2.getOpcode() == ISD::CONCAT_VECTORS) {
14198 //
14199 // 0,0,0,...
Benjamin Kramer558cc5a2011-07-22 01:02:57 +000014200 // |
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000014201 // V UNDEF BUILD_VECTOR UNDEF
14202 // \ / \ /
14203 // CONCAT_VECTOR CONCAT_VECTOR
14204 // \ /
14205 // \ /
14206 // RESULT: V + zero extended
14207 //
14208 if (V2.getOperand(0).getOpcode() != ISD::BUILD_VECTOR ||
14209 V2.getOperand(1).getOpcode() != ISD::UNDEF ||
14210 V1.getOperand(1).getOpcode() != ISD::UNDEF)
14211 return SDValue();
14212
14213 if (!ISD::isBuildVectorAllZeros(V2.getOperand(0).getNode()))
14214 return SDValue();
14215
14216 // To match the shuffle mask, the first half of the mask should
14217 // be exactly the first vector, and all the rest a splat with the
14218 // first element of the second one.
Craig Topper66ddd152012-04-27 22:54:43 +000014219 for (unsigned i = 0; i != NumElems/2; ++i)
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000014220 if (!isUndefOrEqual(SVOp->getMaskElt(i), i) ||
14221 !isUndefOrEqual(SVOp->getMaskElt(i+NumElems/2), NumElems))
14222 return SDValue();
14223
Chad Rosier3d1161e2012-01-03 21:05:52 +000014224 // If V1 is coming from a vector load then just fold to a VZEXT_LOAD.
14225 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(V1.getOperand(0))) {
Chad Rosier42726832012-05-07 18:47:44 +000014226 if (Ld->hasNUsesOfValue(1, 0)) {
14227 SDVTList Tys = DAG.getVTList(MVT::v4i64, MVT::Other);
14228 SDValue Ops[] = { Ld->getChain(), Ld->getBasePtr() };
14229 SDValue ResNode =
14230 DAG.getMemIntrinsicNode(X86ISD::VZEXT_LOAD, dl, Tys, Ops, 2,
14231 Ld->getMemoryVT(),
14232 Ld->getPointerInfo(),
14233 Ld->getAlignment(),
14234 false/*isVolatile*/, true/*ReadMem*/,
14235 false/*WriteMem*/);
Manman Ren2adc5032012-11-13 19:13:05 +000014236
14237 // Make sure the newly-created LOAD is in the same position as Ld in
14238 // terms of dependency. We create a TokenFactor for Ld and ResNode,
14239 // and update uses of Ld's output chain to use the TokenFactor.
14240 if (Ld->hasAnyUseOfValue(1)) {
14241 SDValue NewChain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
14242 SDValue(Ld, 1), SDValue(ResNode.getNode(), 1));
14243 DAG.ReplaceAllUsesOfValueWith(SDValue(Ld, 1), NewChain);
14244 DAG.UpdateNodeOperands(NewChain.getNode(), SDValue(Ld, 1),
14245 SDValue(ResNode.getNode(), 1));
14246 }
14247
Chad Rosier42726832012-05-07 18:47:44 +000014248 return DAG.getNode(ISD::BITCAST, dl, VT, ResNode);
14249 }
Chad Rosiera20e1e72012-08-01 18:39:17 +000014250 }
Chad Rosier3d1161e2012-01-03 21:05:52 +000014251
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000014252 // Emit a zeroed vector and insert the desired subvector on its
14253 // first half.
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +000014254 SDValue Zeros = getZeroVector(VT, Subtarget, DAG, dl);
Craig Topperb14940a2012-04-22 20:55:18 +000014255 SDValue InsV = Insert128BitVector(Zeros, V1.getOperand(0), 0, DAG, dl);
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000014256 return DCI.CombineTo(N, InsV);
14257 }
14258
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000014259 //===--------------------------------------------------------------------===//
14260 // Combine some shuffles into subvector extracts and inserts:
14261 //
14262
14263 // vector_shuffle <4, 5, 6, 7, u, u, u, u> or <2, 3, u, u>
14264 if (isShuffleHigh128VectorInsertLow(SVOp)) {
Craig Topperb14940a2012-04-22 20:55:18 +000014265 SDValue V = Extract128BitVector(V1, NumElems/2, DAG, dl);
14266 SDValue InsV = Insert128BitVector(DAG.getUNDEF(VT), V, 0, DAG, dl);
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000014267 return DCI.CombineTo(N, InsV);
14268 }
14269
14270 // vector_shuffle <u, u, u, u, 0, 1, 2, 3> or <u, u, 0, 1>
14271 if (isShuffleLow128VectorInsertHigh(SVOp)) {
Craig Topperb14940a2012-04-22 20:55:18 +000014272 SDValue V = Extract128BitVector(V1, 0, DAG, dl);
14273 SDValue InsV = Insert128BitVector(DAG.getUNDEF(VT), V, NumElems/2, DAG, dl);
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000014274 return DCI.CombineTo(N, InsV);
14275 }
14276
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000014277 return SDValue();
14278}
14279
14280/// PerformShuffleCombine - Performs several different shuffle combines.
Dan Gohman475871a2008-07-27 21:46:04 +000014281static SDValue PerformShuffleCombine(SDNode *N, SelectionDAG &DAG,
Bruno Cardoso Lopes50b37c72011-08-15 21:45:54 +000014282 TargetLowering::DAGCombinerInfo &DCI,
14283 const X86Subtarget *Subtarget) {
Dale Johannesene4d209d2009-02-03 20:21:25 +000014284 DebugLoc dl = N->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +000014285 EVT VT = N->getValueType(0);
Mon P Wang1e955802009-04-03 02:43:30 +000014286
Mon P Wanga0fd0d52010-12-19 23:55:53 +000014287 // Don't create instructions with illegal types after legalize types has run.
14288 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
14289 if (!DCI.isBeforeLegalize() && !TLI.isTypeLegal(VT.getVectorElementType()))
14290 return SDValue();
14291
Bruno Cardoso Lopes50b37c72011-08-15 21:45:54 +000014292 // Combine 256-bit vector shuffles. This is only profitable when in AVX mode
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000014293 if (Subtarget->hasFp256() && VT.is256BitVector() &&
Bruno Cardoso Lopes50b37c72011-08-15 21:45:54 +000014294 N->getOpcode() == ISD::VECTOR_SHUFFLE)
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +000014295 return PerformShuffleCombine256(N, DAG, DCI, Subtarget);
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000014296
14297 // Only handle 128 wide vector from here on.
Craig Topper7a9a28b2012-08-12 02:23:29 +000014298 if (!VT.is128BitVector())
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000014299 return SDValue();
14300
14301 // Combine a vector_shuffle that is equal to build_vector load1, load2, load3,
14302 // load4, <0, 1, 2, 3> into a 128-bit load if the load addresses are
14303 // consecutive, non-overlapping, and in the right order.
Nate Begemanfdea31a2010-03-24 20:49:50 +000014304 SmallVector<SDValue, 16> Elts;
14305 for (unsigned i = 0, e = VT.getVectorNumElements(); i != e; ++i)
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000014306 Elts.push_back(getShuffleScalarElt(N, i, DAG, 0));
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +000014307
Nate Begemanfdea31a2010-03-24 20:49:50 +000014308 return EltsFromConsecutiveLoads(VT, Elts, dl, DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +000014309}
Evan Chengd880b972008-05-09 21:53:03 +000014310
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000014311
Craig Topper55b24052012-09-11 06:15:32 +000014312/// PerformTruncateCombine - Converts truncate operation to
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000014313/// a sequence of vector shuffle operations.
14314/// It is possible when we truncate 256-bit vector to 128-bit vector
Craig Topper55b24052012-09-11 06:15:32 +000014315static SDValue PerformTruncateCombine(SDNode *N, SelectionDAG &DAG,
14316 TargetLowering::DAGCombinerInfo &DCI,
14317 const X86Subtarget *Subtarget) {
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000014318 if (!DCI.isBeforeLegalizeOps())
14319 return SDValue();
14320
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000014321 if (!Subtarget->hasFp256())
Craig Topper3ef43cf2012-04-24 06:36:35 +000014322 return SDValue();
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000014323
14324 EVT VT = N->getValueType(0);
14325 SDValue Op = N->getOperand(0);
14326 EVT OpVT = Op.getValueType();
14327 DebugLoc dl = N->getDebugLoc();
14328
14329 if ((VT == MVT::v4i32) && (OpVT == MVT::v4i64)) {
14330
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000014331 if (Subtarget->hasInt256()) {
Elena Demikhovsky1da58672012-04-22 09:39:03 +000014332 // AVX2: v4i64 -> v4i32
14333
14334 // VPERMD
14335 static const int ShufMask[] = {0, 2, 4, 6, -1, -1, -1, -1};
14336
14337 Op = DAG.getNode(ISD::BITCAST, dl, MVT::v8i32, Op);
14338 Op = DAG.getVectorShuffle(MVT::v8i32, dl, Op, DAG.getUNDEF(MVT::v8i32),
14339 ShufMask);
14340
Craig Topperd63fa652012-04-22 18:51:37 +000014341 return DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT, Op,
14342 DAG.getIntPtrConstant(0));
Elena Demikhovsky1da58672012-04-22 09:39:03 +000014343 }
14344
14345 // AVX: v4i64 -> v4i32
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000014346 SDValue OpLo = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v2i64, Op,
Craig Topperd63fa652012-04-22 18:51:37 +000014347 DAG.getIntPtrConstant(0));
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000014348
14349 SDValue OpHi = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v2i64, Op,
Craig Topperd63fa652012-04-22 18:51:37 +000014350 DAG.getIntPtrConstant(2));
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000014351
14352 OpLo = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, OpLo);
14353 OpHi = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, OpHi);
14354
14355 // PSHUFD
Craig Topper9e401f22012-04-21 18:58:38 +000014356 static const int ShufMask1[] = {0, 2, 0, 0};
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000014357
Craig Toppercacafd42012-08-14 08:18:43 +000014358 SDValue Undef = DAG.getUNDEF(VT);
14359 OpLo = DAG.getVectorShuffle(VT, dl, OpLo, Undef, ShufMask1);
14360 OpHi = DAG.getVectorShuffle(VT, dl, OpHi, Undef, ShufMask1);
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000014361
14362 // MOVLHPS
Craig Topper9e401f22012-04-21 18:58:38 +000014363 static const int ShufMask2[] = {0, 1, 4, 5};
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000014364
Elena Demikhovsky73252572012-02-01 10:33:05 +000014365 return DAG.getVectorShuffle(VT, dl, OpLo, OpHi, ShufMask2);
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000014366 }
Craig Topperd63fa652012-04-22 18:51:37 +000014367
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000014368 if ((VT == MVT::v8i16) && (OpVT == MVT::v8i32)) {
14369
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000014370 if (Subtarget->hasInt256()) {
Elena Demikhovsky1da58672012-04-22 09:39:03 +000014371 // AVX2: v8i32 -> v8i16
14372
14373 Op = DAG.getNode(ISD::BITCAST, dl, MVT::v32i8, Op);
Craig Topperd63fa652012-04-22 18:51:37 +000014374
Elena Demikhovsky1da58672012-04-22 09:39:03 +000014375 // PSHUFB
14376 SmallVector<SDValue,32> pshufbMask;
14377 for (unsigned i = 0; i < 2; ++i) {
14378 pshufbMask.push_back(DAG.getConstant(0x0, MVT::i8));
14379 pshufbMask.push_back(DAG.getConstant(0x1, MVT::i8));
14380 pshufbMask.push_back(DAG.getConstant(0x4, MVT::i8));
14381 pshufbMask.push_back(DAG.getConstant(0x5, MVT::i8));
14382 pshufbMask.push_back(DAG.getConstant(0x8, MVT::i8));
14383 pshufbMask.push_back(DAG.getConstant(0x9, MVT::i8));
14384 pshufbMask.push_back(DAG.getConstant(0xc, MVT::i8));
14385 pshufbMask.push_back(DAG.getConstant(0xd, MVT::i8));
14386 for (unsigned j = 0; j < 8; ++j)
14387 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
14388 }
Craig Topperd63fa652012-04-22 18:51:37 +000014389 SDValue BV = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v32i8,
14390 &pshufbMask[0], 32);
Elena Demikhovsky1da58672012-04-22 09:39:03 +000014391 Op = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v32i8, Op, BV);
14392
14393 Op = DAG.getNode(ISD::BITCAST, dl, MVT::v4i64, Op);
14394
14395 static const int ShufMask[] = {0, 2, -1, -1};
Craig Topperd63fa652012-04-22 18:51:37 +000014396 Op = DAG.getVectorShuffle(MVT::v4i64, dl, Op, DAG.getUNDEF(MVT::v4i64),
Elena Demikhovsky1da58672012-04-22 09:39:03 +000014397 &ShufMask[0]);
14398
Craig Topperd63fa652012-04-22 18:51:37 +000014399 Op = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v2i64, Op,
14400 DAG.getIntPtrConstant(0));
Elena Demikhovsky1da58672012-04-22 09:39:03 +000014401
14402 return DAG.getNode(ISD::BITCAST, dl, VT, Op);
14403 }
14404
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000014405 SDValue OpLo = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i32, Op,
Craig Topperd63fa652012-04-22 18:51:37 +000014406 DAG.getIntPtrConstant(0));
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000014407
14408 SDValue OpHi = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i32, Op,
Craig Topperd63fa652012-04-22 18:51:37 +000014409 DAG.getIntPtrConstant(4));
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000014410
14411 OpLo = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, OpLo);
14412 OpHi = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, OpHi);
14413
14414 // PSHUFB
Craig Topper9e401f22012-04-21 18:58:38 +000014415 static const int ShufMask1[] = {0, 1, 4, 5, 8, 9, 12, 13,
14416 -1, -1, -1, -1, -1, -1, -1, -1};
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000014417
Craig Toppercacafd42012-08-14 08:18:43 +000014418 SDValue Undef = DAG.getUNDEF(MVT::v16i8);
14419 OpLo = DAG.getVectorShuffle(MVT::v16i8, dl, OpLo, Undef, ShufMask1);
14420 OpHi = DAG.getVectorShuffle(MVT::v16i8, dl, OpHi, Undef, ShufMask1);
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000014421
14422 OpLo = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, OpLo);
14423 OpHi = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, OpHi);
14424
14425 // MOVLHPS
Craig Topper9e401f22012-04-21 18:58:38 +000014426 static const int ShufMask2[] = {0, 1, 4, 5};
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000014427
Elena Demikhovsky73252572012-02-01 10:33:05 +000014428 SDValue res = DAG.getVectorShuffle(MVT::v4i32, dl, OpLo, OpHi, ShufMask2);
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000014429 return DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, res);
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000014430 }
14431
14432 return SDValue();
14433}
14434
Craig Topper89f4e662012-03-20 07:17:59 +000014435/// XFormVExtractWithShuffleIntoLoad - Check if a vector extract from a target
14436/// specific shuffle of a load can be folded into a single element load.
14437/// Similar handling for VECTOR_SHUFFLE is performed by DAGCombiner, but
14438/// shuffles have been customed lowered so we need to handle those here.
14439static SDValue XFormVExtractWithShuffleIntoLoad(SDNode *N, SelectionDAG &DAG,
14440 TargetLowering::DAGCombinerInfo &DCI) {
14441 if (DCI.isBeforeLegalizeOps())
14442 return SDValue();
14443
14444 SDValue InVec = N->getOperand(0);
14445 SDValue EltNo = N->getOperand(1);
14446
14447 if (!isa<ConstantSDNode>(EltNo))
14448 return SDValue();
14449
14450 EVT VT = InVec.getValueType();
14451
14452 bool HasShuffleIntoBitcast = false;
14453 if (InVec.getOpcode() == ISD::BITCAST) {
14454 // Don't duplicate a load with other uses.
14455 if (!InVec.hasOneUse())
14456 return SDValue();
14457 EVT BCVT = InVec.getOperand(0).getValueType();
14458 if (BCVT.getVectorNumElements() != VT.getVectorNumElements())
14459 return SDValue();
14460 InVec = InVec.getOperand(0);
14461 HasShuffleIntoBitcast = true;
14462 }
14463
14464 if (!isTargetShuffle(InVec.getOpcode()))
14465 return SDValue();
14466
14467 // Don't duplicate a load with other uses.
14468 if (!InVec.hasOneUse())
14469 return SDValue();
14470
14471 SmallVector<int, 16> ShuffleMask;
14472 bool UnaryShuffle;
Craig Topperd978c542012-05-06 19:46:21 +000014473 if (!getTargetShuffleMask(InVec.getNode(), VT.getSimpleVT(), ShuffleMask,
14474 UnaryShuffle))
Craig Topper89f4e662012-03-20 07:17:59 +000014475 return SDValue();
14476
14477 // Select the input vector, guarding against out of range extract vector.
14478 unsigned NumElems = VT.getVectorNumElements();
14479 int Elt = cast<ConstantSDNode>(EltNo)->getZExtValue();
14480 int Idx = (Elt > (int)NumElems) ? -1 : ShuffleMask[Elt];
14481 SDValue LdNode = (Idx < (int)NumElems) ? InVec.getOperand(0)
14482 : InVec.getOperand(1);
14483
14484 // If inputs to shuffle are the same for both ops, then allow 2 uses
14485 unsigned AllowedUses = InVec.getOperand(0) == InVec.getOperand(1) ? 2 : 1;
14486
14487 if (LdNode.getOpcode() == ISD::BITCAST) {
14488 // Don't duplicate a load with other uses.
14489 if (!LdNode.getNode()->hasNUsesOfValue(AllowedUses, 0))
14490 return SDValue();
14491
14492 AllowedUses = 1; // only allow 1 load use if we have a bitcast
14493 LdNode = LdNode.getOperand(0);
14494 }
14495
14496 if (!ISD::isNormalLoad(LdNode.getNode()))
14497 return SDValue();
14498
14499 LoadSDNode *LN0 = cast<LoadSDNode>(LdNode);
14500
14501 if (!LN0 ||!LN0->hasNUsesOfValue(AllowedUses, 0) || LN0->isVolatile())
14502 return SDValue();
14503
14504 if (HasShuffleIntoBitcast) {
14505 // If there's a bitcast before the shuffle, check if the load type and
14506 // alignment is valid.
14507 unsigned Align = LN0->getAlignment();
14508 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Micah Villmow3574eca2012-10-08 16:38:25 +000014509 unsigned NewAlign = TLI.getDataLayout()->
Craig Topper89f4e662012-03-20 07:17:59 +000014510 getABITypeAlignment(VT.getTypeForEVT(*DAG.getContext()));
14511
14512 if (NewAlign > Align || !TLI.isOperationLegalOrCustom(ISD::LOAD, VT))
14513 return SDValue();
14514 }
14515
14516 // All checks match so transform back to vector_shuffle so that DAG combiner
14517 // can finish the job
14518 DebugLoc dl = N->getDebugLoc();
14519
14520 // Create shuffle node taking into account the case that its a unary shuffle
14521 SDValue Shuffle = (UnaryShuffle) ? DAG.getUNDEF(VT) : InVec.getOperand(1);
14522 Shuffle = DAG.getVectorShuffle(InVec.getValueType(), dl,
14523 InVec.getOperand(0), Shuffle,
14524 &ShuffleMask[0]);
14525 Shuffle = DAG.getNode(ISD::BITCAST, dl, VT, Shuffle);
14526 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, N->getValueType(0), Shuffle,
14527 EltNo);
14528}
14529
Bruno Cardoso Lopesb3e06692010-09-03 19:55:05 +000014530/// PerformEXTRACT_VECTOR_ELTCombine - Detect vector gather/scatter index
14531/// generation and convert it from being a bunch of shuffles and extracts
14532/// to a simple store and scalar loads to extract the elements.
Dan Gohman1bbf72b2010-03-15 23:23:03 +000014533static SDValue PerformEXTRACT_VECTOR_ELTCombine(SDNode *N, SelectionDAG &DAG,
Craig Topper89f4e662012-03-20 07:17:59 +000014534 TargetLowering::DAGCombinerInfo &DCI) {
14535 SDValue NewOp = XFormVExtractWithShuffleIntoLoad(N, DAG, DCI);
14536 if (NewOp.getNode())
14537 return NewOp;
14538
Dan Gohman1bbf72b2010-03-15 23:23:03 +000014539 SDValue InputVector = N->getOperand(0);
Manman Ren4c74a952012-10-30 22:15:38 +000014540 // Detect whether we are trying to convert from mmx to i32 and the bitcast
14541 // from mmx to v2i32 has a single usage.
14542 if (InputVector.getNode()->getOpcode() == llvm::ISD::BITCAST &&
14543 InputVector.getNode()->getOperand(0).getValueType() == MVT::x86mmx &&
14544 InputVector.hasOneUse() && N->getValueType(0) == MVT::i32)
14545 return DAG.getNode(X86ISD::MMX_MOVD2W, InputVector.getDebugLoc(),
14546 N->getValueType(0),
14547 InputVector.getNode()->getOperand(0));
Dan Gohman1bbf72b2010-03-15 23:23:03 +000014548
14549 // Only operate on vectors of 4 elements, where the alternative shuffling
14550 // gets to be more expensive.
14551 if (InputVector.getValueType() != MVT::v4i32)
14552 return SDValue();
14553
14554 // Check whether every use of InputVector is an EXTRACT_VECTOR_ELT with a
14555 // single use which is a sign-extend or zero-extend, and all elements are
14556 // used.
14557 SmallVector<SDNode *, 4> Uses;
14558 unsigned ExtractedElements = 0;
14559 for (SDNode::use_iterator UI = InputVector.getNode()->use_begin(),
14560 UE = InputVector.getNode()->use_end(); UI != UE; ++UI) {
14561 if (UI.getUse().getResNo() != InputVector.getResNo())
14562 return SDValue();
14563
14564 SDNode *Extract = *UI;
14565 if (Extract->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
14566 return SDValue();
14567
14568 if (Extract->getValueType(0) != MVT::i32)
14569 return SDValue();
14570 if (!Extract->hasOneUse())
14571 return SDValue();
14572 if (Extract->use_begin()->getOpcode() != ISD::SIGN_EXTEND &&
14573 Extract->use_begin()->getOpcode() != ISD::ZERO_EXTEND)
14574 return SDValue();
14575 if (!isa<ConstantSDNode>(Extract->getOperand(1)))
14576 return SDValue();
14577
14578 // Record which element was extracted.
14579 ExtractedElements |=
14580 1 << cast<ConstantSDNode>(Extract->getOperand(1))->getZExtValue();
14581
14582 Uses.push_back(Extract);
14583 }
14584
14585 // If not all the elements were used, this may not be worthwhile.
14586 if (ExtractedElements != 15)
14587 return SDValue();
14588
14589 // Ok, we've now decided to do the transformation.
14590 DebugLoc dl = InputVector.getDebugLoc();
14591
14592 // Store the value to a temporary stack slot.
14593 SDValue StackPtr = DAG.CreateStackTemporary(InputVector.getValueType());
Chris Lattner8026a9d2010-09-21 17:50:43 +000014594 SDValue Ch = DAG.getStore(DAG.getEntryNode(), dl, InputVector, StackPtr,
14595 MachinePointerInfo(), false, false, 0);
Dan Gohman1bbf72b2010-03-15 23:23:03 +000014596
14597 // Replace each use (extract) with a load of the appropriate element.
14598 for (SmallVectorImpl<SDNode *>::iterator UI = Uses.begin(),
14599 UE = Uses.end(); UI != UE; ++UI) {
14600 SDNode *Extract = *UI;
14601
Nadav Rotem86694292011-05-17 08:31:57 +000014602 // cOMpute the element's address.
Dan Gohman1bbf72b2010-03-15 23:23:03 +000014603 SDValue Idx = Extract->getOperand(1);
14604 unsigned EltSize =
14605 InputVector.getValueType().getVectorElementType().getSizeInBits()/8;
14606 uint64_t Offset = EltSize * cast<ConstantSDNode>(Idx)->getZExtValue();
Craig Topper89f4e662012-03-20 07:17:59 +000014607 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Dan Gohman1bbf72b2010-03-15 23:23:03 +000014608 SDValue OffsetVal = DAG.getConstant(Offset, TLI.getPointerTy());
14609
Nadav Rotem86694292011-05-17 08:31:57 +000014610 SDValue ScalarAddr = DAG.getNode(ISD::ADD, dl, TLI.getPointerTy(),
Chris Lattner51abfe42010-09-21 06:02:19 +000014611 StackPtr, OffsetVal);
Dan Gohman1bbf72b2010-03-15 23:23:03 +000014612
14613 // Load the scalar.
Eric Christopher90eb4022010-07-22 00:26:08 +000014614 SDValue LoadScalar = DAG.getLoad(Extract->getValueType(0), dl, Ch,
Chris Lattner51abfe42010-09-21 06:02:19 +000014615 ScalarAddr, MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000014616 false, false, false, 0);
Dan Gohman1bbf72b2010-03-15 23:23:03 +000014617
14618 // Replace the exact with the load.
14619 DAG.ReplaceAllUsesOfValueWith(SDValue(Extract, 0), LoadScalar);
14620 }
14621
14622 // The replacement was made in place; don't return anything.
14623 return SDValue();
14624}
14625
Duncan Sands6bcd2192011-09-17 16:49:39 +000014626/// PerformSELECTCombine - Do target-specific dag combines on SELECT and VSELECT
14627/// nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000014628static SDValue PerformSELECTCombine(SDNode *N, SelectionDAG &DAG,
Nadav Rotemcc616562012-01-15 19:27:55 +000014629 TargetLowering::DAGCombinerInfo &DCI,
Chris Lattner47b4ce82009-03-11 05:48:52 +000014630 const X86Subtarget *Subtarget) {
14631 DebugLoc DL = N->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +000014632 SDValue Cond = N->getOperand(0);
Chris Lattner47b4ce82009-03-11 05:48:52 +000014633 // Get the LHS/RHS of the select.
14634 SDValue LHS = N->getOperand(1);
14635 SDValue RHS = N->getOperand(2);
Bruno Cardoso Lopes149f29f2011-09-20 22:34:45 +000014636 EVT VT = LHS.getValueType();
Eric Christopherfd179292009-08-27 18:07:15 +000014637
Dan Gohman670e5392009-09-21 18:03:22 +000014638 // If we have SSE[12] support, try to form min/max nodes. SSE min/max
Dan Gohman8ce05da2010-02-22 04:03:39 +000014639 // instructions match the semantics of the common C idiom x<y?x:y but not
14640 // x<=y?x:y, because of how they handle negative zero (which can be
14641 // ignored in unsafe-math mode).
Benjamin Kramer2c2ccbf2011-09-22 03:27:22 +000014642 if (Cond.getOpcode() == ISD::SETCC && VT.isFloatingPoint() &&
14643 VT != MVT::f80 && DAG.getTargetLoweringInfo().isTypeLegal(VT) &&
Craig Topper1accb7e2012-01-10 06:54:16 +000014644 (Subtarget->hasSSE2() ||
14645 (Subtarget->hasSSE1() && VT.getScalarType() == MVT::f32))) {
Chris Lattner47b4ce82009-03-11 05:48:52 +000014646 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000014647
Chris Lattner47b4ce82009-03-11 05:48:52 +000014648 unsigned Opcode = 0;
Dan Gohman670e5392009-09-21 18:03:22 +000014649 // Check for x CC y ? x : y.
Dan Gohmane8326932010-02-24 06:52:40 +000014650 if (DAG.isEqualTo(LHS, Cond.getOperand(0)) &&
14651 DAG.isEqualTo(RHS, Cond.getOperand(1))) {
Chris Lattner47b4ce82009-03-11 05:48:52 +000014652 switch (CC) {
14653 default: break;
Dan Gohman670e5392009-09-21 18:03:22 +000014654 case ISD::SETULT:
Dan Gohmane8326932010-02-24 06:52:40 +000014655 // Converting this to a min would handle NaNs incorrectly, and swapping
14656 // the operands would cause it to handle comparisons between positive
14657 // and negative zero incorrectly.
Evan Cheng60108e92010-07-15 22:07:12 +000014658 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)) {
Nick Lewycky8a8d4792011-12-02 22:16:29 +000014659 if (!DAG.getTarget().Options.UnsafeFPMath &&
Dan Gohmane8326932010-02-24 06:52:40 +000014660 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
14661 break;
14662 std::swap(LHS, RHS);
14663 }
Dan Gohman670e5392009-09-21 18:03:22 +000014664 Opcode = X86ISD::FMIN;
14665 break;
14666 case ISD::SETOLE:
Dan Gohmane8326932010-02-24 06:52:40 +000014667 // Converting this to a min would handle comparisons between positive
14668 // and negative zero incorrectly.
Nick Lewycky8a8d4792011-12-02 22:16:29 +000014669 if (!DAG.getTarget().Options.UnsafeFPMath &&
Dan Gohmane8326932010-02-24 06:52:40 +000014670 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(RHS))
14671 break;
Dan Gohman670e5392009-09-21 18:03:22 +000014672 Opcode = X86ISD::FMIN;
14673 break;
Chris Lattner47b4ce82009-03-11 05:48:52 +000014674 case ISD::SETULE:
Dan Gohmane8326932010-02-24 06:52:40 +000014675 // Converting this to a min would handle both negative zeros and NaNs
14676 // incorrectly, but we can swap the operands to fix both.
14677 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000014678 case ISD::SETOLT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000014679 case ISD::SETLT:
Dan Gohman670e5392009-09-21 18:03:22 +000014680 case ISD::SETLE:
Chris Lattner47b4ce82009-03-11 05:48:52 +000014681 Opcode = X86ISD::FMIN;
14682 break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000014683
Dan Gohman670e5392009-09-21 18:03:22 +000014684 case ISD::SETOGE:
Dan Gohmane8326932010-02-24 06:52:40 +000014685 // Converting this to a max would handle comparisons between positive
14686 // and negative zero incorrectly.
Nick Lewycky8a8d4792011-12-02 22:16:29 +000014687 if (!DAG.getTarget().Options.UnsafeFPMath &&
Evan Chengdd5663c2011-08-04 18:38:15 +000014688 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000014689 break;
Dan Gohman670e5392009-09-21 18:03:22 +000014690 Opcode = X86ISD::FMAX;
14691 break;
Chris Lattner47b4ce82009-03-11 05:48:52 +000014692 case ISD::SETUGT:
Dan Gohmane8326932010-02-24 06:52:40 +000014693 // Converting this to a max would handle NaNs incorrectly, and swapping
14694 // the operands would cause it to handle comparisons between positive
14695 // and negative zero incorrectly.
Evan Cheng60108e92010-07-15 22:07:12 +000014696 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)) {
Nick Lewycky8a8d4792011-12-02 22:16:29 +000014697 if (!DAG.getTarget().Options.UnsafeFPMath &&
Dan Gohmane8326932010-02-24 06:52:40 +000014698 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
14699 break;
14700 std::swap(LHS, RHS);
14701 }
Dan Gohman670e5392009-09-21 18:03:22 +000014702 Opcode = X86ISD::FMAX;
14703 break;
14704 case ISD::SETUGE:
Dan Gohmane8326932010-02-24 06:52:40 +000014705 // Converting this to a max would handle both negative zeros and NaNs
14706 // incorrectly, but we can swap the operands to fix both.
14707 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000014708 case ISD::SETOGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000014709 case ISD::SETGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000014710 case ISD::SETGE:
14711 Opcode = X86ISD::FMAX;
14712 break;
Chris Lattner83e6c992006-10-04 06:57:07 +000014713 }
Dan Gohman670e5392009-09-21 18:03:22 +000014714 // Check for x CC y ? y : x -- a min/max with reversed arms.
Dan Gohmane8326932010-02-24 06:52:40 +000014715 } else if (DAG.isEqualTo(LHS, Cond.getOperand(1)) &&
14716 DAG.isEqualTo(RHS, Cond.getOperand(0))) {
Chris Lattner47b4ce82009-03-11 05:48:52 +000014717 switch (CC) {
14718 default: break;
Dan Gohman670e5392009-09-21 18:03:22 +000014719 case ISD::SETOGE:
Dan Gohmane8326932010-02-24 06:52:40 +000014720 // Converting this to a min would handle comparisons between positive
14721 // and negative zero incorrectly, and swapping the operands would
14722 // cause it to handle NaNs incorrectly.
Nick Lewycky8a8d4792011-12-02 22:16:29 +000014723 if (!DAG.getTarget().Options.UnsafeFPMath &&
Dan Gohmane8326932010-02-24 06:52:40 +000014724 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS))) {
Evan Cheng60108e92010-07-15 22:07:12 +000014725 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000014726 break;
14727 std::swap(LHS, RHS);
14728 }
Dan Gohman670e5392009-09-21 18:03:22 +000014729 Opcode = X86ISD::FMIN;
Dan Gohman8d44b282009-09-03 20:34:31 +000014730 break;
Dan Gohman670e5392009-09-21 18:03:22 +000014731 case ISD::SETUGT:
Dan Gohmane8326932010-02-24 06:52:40 +000014732 // Converting this to a min would handle NaNs incorrectly.
Nick Lewycky8a8d4792011-12-02 22:16:29 +000014733 if (!DAG.getTarget().Options.UnsafeFPMath &&
Dan Gohmane8326932010-02-24 06:52:40 +000014734 (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)))
14735 break;
Dan Gohman670e5392009-09-21 18:03:22 +000014736 Opcode = X86ISD::FMIN;
14737 break;
14738 case ISD::SETUGE:
Dan Gohmane8326932010-02-24 06:52:40 +000014739 // Converting this to a min would handle both negative zeros and NaNs
14740 // incorrectly, but we can swap the operands to fix both.
14741 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000014742 case ISD::SETOGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000014743 case ISD::SETGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000014744 case ISD::SETGE:
14745 Opcode = X86ISD::FMIN;
14746 break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000014747
Dan Gohman670e5392009-09-21 18:03:22 +000014748 case ISD::SETULT:
Dan Gohmane8326932010-02-24 06:52:40 +000014749 // Converting this to a max would handle NaNs incorrectly.
Evan Cheng60108e92010-07-15 22:07:12 +000014750 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000014751 break;
Dan Gohman670e5392009-09-21 18:03:22 +000014752 Opcode = X86ISD::FMAX;
Dan Gohman8d44b282009-09-03 20:34:31 +000014753 break;
Dan Gohman670e5392009-09-21 18:03:22 +000014754 case ISD::SETOLE:
Dan Gohmane8326932010-02-24 06:52:40 +000014755 // Converting this to a max would handle comparisons between positive
14756 // and negative zero incorrectly, and swapping the operands would
14757 // cause it to handle NaNs incorrectly.
Nick Lewycky8a8d4792011-12-02 22:16:29 +000014758 if (!DAG.getTarget().Options.UnsafeFPMath &&
Dan Gohmane8326932010-02-24 06:52:40 +000014759 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(RHS)) {
Evan Cheng60108e92010-07-15 22:07:12 +000014760 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000014761 break;
14762 std::swap(LHS, RHS);
14763 }
Dan Gohman670e5392009-09-21 18:03:22 +000014764 Opcode = X86ISD::FMAX;
14765 break;
14766 case ISD::SETULE:
Dan Gohmane8326932010-02-24 06:52:40 +000014767 // Converting this to a max would handle both negative zeros and NaNs
14768 // incorrectly, but we can swap the operands to fix both.
14769 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000014770 case ISD::SETOLT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000014771 case ISD::SETLT:
Dan Gohman670e5392009-09-21 18:03:22 +000014772 case ISD::SETLE:
Chris Lattner47b4ce82009-03-11 05:48:52 +000014773 Opcode = X86ISD::FMAX;
14774 break;
14775 }
Chris Lattner83e6c992006-10-04 06:57:07 +000014776 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000014777
Chris Lattner47b4ce82009-03-11 05:48:52 +000014778 if (Opcode)
14779 return DAG.getNode(Opcode, DL, N->getValueType(0), LHS, RHS);
Chris Lattner83e6c992006-10-04 06:57:07 +000014780 }
Eric Christopherfd179292009-08-27 18:07:15 +000014781
Chris Lattnerd1980a52009-03-12 06:52:53 +000014782 // If this is a select between two integer constants, try to do some
14783 // optimizations.
Chris Lattnercee56e72009-03-13 05:53:31 +000014784 if (ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(LHS)) {
14785 if (ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(RHS))
Chris Lattnerd1980a52009-03-12 06:52:53 +000014786 // Don't do this for crazy integer types.
14787 if (DAG.getTargetLoweringInfo().isTypeLegal(LHS.getValueType())) {
14788 // If this is efficiently invertible, canonicalize the LHSC/RHSC values
Chris Lattnercee56e72009-03-13 05:53:31 +000014789 // so that TrueC (the true value) is larger than FalseC.
Chris Lattnerd1980a52009-03-12 06:52:53 +000014790 bool NeedsCondInvert = false;
Eric Christopherfd179292009-08-27 18:07:15 +000014791
Chris Lattnercee56e72009-03-13 05:53:31 +000014792 if (TrueC->getAPIntValue().ult(FalseC->getAPIntValue()) &&
Chris Lattnerd1980a52009-03-12 06:52:53 +000014793 // Efficiently invertible.
14794 (Cond.getOpcode() == ISD::SETCC || // setcc -> invertible.
14795 (Cond.getOpcode() == ISD::XOR && // xor(X, C) -> invertible.
14796 isa<ConstantSDNode>(Cond.getOperand(1))))) {
14797 NeedsCondInvert = true;
Chris Lattnercee56e72009-03-13 05:53:31 +000014798 std::swap(TrueC, FalseC);
Chris Lattnerd1980a52009-03-12 06:52:53 +000014799 }
Eric Christopherfd179292009-08-27 18:07:15 +000014800
Chris Lattnerd1980a52009-03-12 06:52:53 +000014801 // Optimize C ? 8 : 0 -> zext(C) << 3. Likewise for any pow2/0.
Chris Lattnercee56e72009-03-13 05:53:31 +000014802 if (FalseC->getAPIntValue() == 0 &&
14803 TrueC->getAPIntValue().isPowerOf2()) {
Chris Lattnerd1980a52009-03-12 06:52:53 +000014804 if (NeedsCondInvert) // Invert the condition if needed.
14805 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
14806 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000014807
Chris Lattnerd1980a52009-03-12 06:52:53 +000014808 // Zero extend the condition if needed.
14809 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, LHS.getValueType(), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000014810
Chris Lattnercee56e72009-03-13 05:53:31 +000014811 unsigned ShAmt = TrueC->getAPIntValue().logBase2();
Chris Lattnerd1980a52009-03-12 06:52:53 +000014812 return DAG.getNode(ISD::SHL, DL, LHS.getValueType(), Cond,
Owen Anderson825b72b2009-08-11 20:47:22 +000014813 DAG.getConstant(ShAmt, MVT::i8));
Chris Lattnerd1980a52009-03-12 06:52:53 +000014814 }
Eric Christopherfd179292009-08-27 18:07:15 +000014815
Chris Lattner97a29a52009-03-13 05:22:11 +000014816 // Optimize Cond ? cst+1 : cst -> zext(setcc(C)+cst.
Chris Lattnercee56e72009-03-13 05:53:31 +000014817 if (FalseC->getAPIntValue()+1 == TrueC->getAPIntValue()) {
Chris Lattner97a29a52009-03-13 05:22:11 +000014818 if (NeedsCondInvert) // Invert the condition if needed.
14819 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
14820 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000014821
Chris Lattner97a29a52009-03-13 05:22:11 +000014822 // Zero extend the condition if needed.
Chris Lattnercee56e72009-03-13 05:53:31 +000014823 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL,
14824 FalseC->getValueType(0), Cond);
Chris Lattner97a29a52009-03-13 05:22:11 +000014825 return DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
Chris Lattnercee56e72009-03-13 05:53:31 +000014826 SDValue(FalseC, 0));
Chris Lattner97a29a52009-03-13 05:22:11 +000014827 }
Eric Christopherfd179292009-08-27 18:07:15 +000014828
Chris Lattnercee56e72009-03-13 05:53:31 +000014829 // Optimize cases that will turn into an LEA instruction. This requires
14830 // an i32 or i64 and an efficient multiplier (1, 2, 3, 4, 5, 8, 9).
Owen Anderson825b72b2009-08-11 20:47:22 +000014831 if (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i64) {
Chris Lattnercee56e72009-03-13 05:53:31 +000014832 uint64_t Diff = TrueC->getZExtValue()-FalseC->getZExtValue();
Owen Anderson825b72b2009-08-11 20:47:22 +000014833 if (N->getValueType(0) == MVT::i32) Diff = (unsigned)Diff;
Eric Christopherfd179292009-08-27 18:07:15 +000014834
Chris Lattnercee56e72009-03-13 05:53:31 +000014835 bool isFastMultiplier = false;
14836 if (Diff < 10) {
14837 switch ((unsigned char)Diff) {
14838 default: break;
14839 case 1: // result = add base, cond
14840 case 2: // result = lea base( , cond*2)
14841 case 3: // result = lea base(cond, cond*2)
14842 case 4: // result = lea base( , cond*4)
14843 case 5: // result = lea base(cond, cond*4)
14844 case 8: // result = lea base( , cond*8)
14845 case 9: // result = lea base(cond, cond*8)
14846 isFastMultiplier = true;
14847 break;
14848 }
14849 }
Eric Christopherfd179292009-08-27 18:07:15 +000014850
Chris Lattnercee56e72009-03-13 05:53:31 +000014851 if (isFastMultiplier) {
14852 APInt Diff = TrueC->getAPIntValue()-FalseC->getAPIntValue();
14853 if (NeedsCondInvert) // Invert the condition if needed.
14854 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
14855 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000014856
Chris Lattnercee56e72009-03-13 05:53:31 +000014857 // Zero extend the condition if needed.
14858 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, FalseC->getValueType(0),
14859 Cond);
14860 // Scale the condition by the difference.
14861 if (Diff != 1)
14862 Cond = DAG.getNode(ISD::MUL, DL, Cond.getValueType(), Cond,
14863 DAG.getConstant(Diff, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000014864
Chris Lattnercee56e72009-03-13 05:53:31 +000014865 // Add the base if non-zero.
14866 if (FalseC->getAPIntValue() != 0)
14867 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
14868 SDValue(FalseC, 0));
14869 return Cond;
14870 }
Eric Christopherfd179292009-08-27 18:07:15 +000014871 }
Chris Lattnerd1980a52009-03-12 06:52:53 +000014872 }
14873 }
Eric Christopherfd179292009-08-27 18:07:15 +000014874
Evan Cheng56f582d2012-01-04 01:41:39 +000014875 // Canonicalize max and min:
14876 // (x > y) ? x : y -> (x >= y) ? x : y
14877 // (x < y) ? x : y -> (x <= y) ? x : y
14878 // This allows use of COND_S / COND_NS (see TranslateX86CC) which eliminates
14879 // the need for an extra compare
14880 // against zero. e.g.
14881 // (x - y) > 0 : (x - y) ? 0 -> (x - y) >= 0 : (x - y) ? 0
14882 // subl %esi, %edi
14883 // testl %edi, %edi
14884 // movl $0, %eax
14885 // cmovgl %edi, %eax
14886 // =>
14887 // xorl %eax, %eax
14888 // subl %esi, $edi
14889 // cmovsl %eax, %edi
14890 if (N->getOpcode() == ISD::SELECT && Cond.getOpcode() == ISD::SETCC &&
14891 DAG.isEqualTo(LHS, Cond.getOperand(0)) &&
14892 DAG.isEqualTo(RHS, Cond.getOperand(1))) {
14893 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
14894 switch (CC) {
14895 default: break;
14896 case ISD::SETLT:
14897 case ISD::SETGT: {
14898 ISD::CondCode NewCC = (CC == ISD::SETLT) ? ISD::SETLE : ISD::SETGE;
14899 Cond = DAG.getSetCC(Cond.getDebugLoc(), Cond.getValueType(),
14900 Cond.getOperand(0), Cond.getOperand(1), NewCC);
14901 return DAG.getNode(ISD::SELECT, DL, VT, Cond, LHS, RHS);
14902 }
14903 }
14904 }
14905
Nadav Rotemcc616562012-01-15 19:27:55 +000014906 // If we know that this node is legal then we know that it is going to be
14907 // matched by one of the SSE/AVX BLEND instructions. These instructions only
14908 // depend on the highest bit in each word. Try to use SimplifyDemandedBits
14909 // to simplify previous instructions.
14910 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
14911 if (N->getOpcode() == ISD::VSELECT && DCI.isBeforeLegalizeOps() &&
Nadav Rotembdcae382012-06-07 20:53:48 +000014912 !DCI.isBeforeLegalize() && TLI.isOperationLegal(ISD::VSELECT, VT)) {
Nadav Rotemcc616562012-01-15 19:27:55 +000014913 unsigned BitWidth = Cond.getValueType().getScalarType().getSizeInBits();
Nadav Rotembdcae382012-06-07 20:53:48 +000014914
14915 // Don't optimize vector selects that map to mask-registers.
14916 if (BitWidth == 1)
14917 return SDValue();
14918
Nadav Rotemcc616562012-01-15 19:27:55 +000014919 assert(BitWidth >= 8 && BitWidth <= 64 && "Invalid mask size");
14920 APInt DemandedMask = APInt::getHighBitsSet(BitWidth, 1);
14921
14922 APInt KnownZero, KnownOne;
14923 TargetLowering::TargetLoweringOpt TLO(DAG, DCI.isBeforeLegalize(),
14924 DCI.isBeforeLegalizeOps());
14925 if (TLO.ShrinkDemandedConstant(Cond, DemandedMask) ||
14926 TLI.SimplifyDemandedBits(Cond, DemandedMask, KnownZero, KnownOne, TLO))
14927 DCI.CommitTargetLoweringOpt(TLO);
14928 }
14929
Dan Gohman475871a2008-07-27 21:46:04 +000014930 return SDValue();
Chris Lattner83e6c992006-10-04 06:57:07 +000014931}
14932
Michael Liao2a33cec2012-08-10 19:58:13 +000014933// Check whether a boolean test is testing a boolean value generated by
14934// X86ISD::SETCC. If so, return the operand of that SETCC and proper condition
14935// code.
14936//
14937// Simplify the following patterns:
14938// (Op (CMP (SETCC Cond EFLAGS) 1) EQ) or
14939// (Op (CMP (SETCC Cond EFLAGS) 0) NEQ)
14940// to (Op EFLAGS Cond)
14941//
14942// (Op (CMP (SETCC Cond EFLAGS) 0) EQ) or
14943// (Op (CMP (SETCC Cond EFLAGS) 1) NEQ)
14944// to (Op EFLAGS !Cond)
14945//
14946// where Op could be BRCOND or CMOV.
14947//
Michael Liaodbf8b5b2012-08-28 03:34:40 +000014948static SDValue checkBoolTestSetCCCombine(SDValue Cmp, X86::CondCode &CC) {
Michael Liao2a33cec2012-08-10 19:58:13 +000014949 // Quit if not CMP and SUB with its value result used.
14950 if (Cmp.getOpcode() != X86ISD::CMP &&
14951 (Cmp.getOpcode() != X86ISD::SUB || Cmp.getNode()->hasAnyUseOfValue(0)))
14952 return SDValue();
14953
14954 // Quit if not used as a boolean value.
14955 if (CC != X86::COND_E && CC != X86::COND_NE)
14956 return SDValue();
14957
14958 // Check CMP operands. One of them should be 0 or 1 and the other should be
14959 // an SetCC or extended from it.
14960 SDValue Op1 = Cmp.getOperand(0);
14961 SDValue Op2 = Cmp.getOperand(1);
14962
14963 SDValue SetCC;
14964 const ConstantSDNode* C = 0;
14965 bool needOppositeCond = (CC == X86::COND_E);
14966
14967 if ((C = dyn_cast<ConstantSDNode>(Op1)))
14968 SetCC = Op2;
14969 else if ((C = dyn_cast<ConstantSDNode>(Op2)))
14970 SetCC = Op1;
14971 else // Quit if all operands are not constants.
14972 return SDValue();
14973
14974 if (C->getZExtValue() == 1)
14975 needOppositeCond = !needOppositeCond;
14976 else if (C->getZExtValue() != 0)
14977 // Quit if the constant is neither 0 or 1.
14978 return SDValue();
14979
14980 // Skip 'zext' node.
14981 if (SetCC.getOpcode() == ISD::ZERO_EXTEND)
14982 SetCC = SetCC.getOperand(0);
14983
Michael Liao7fdc66b2012-09-10 16:36:16 +000014984 switch (SetCC.getOpcode()) {
14985 case X86ISD::SETCC:
14986 // Set the condition code or opposite one if necessary.
14987 CC = X86::CondCode(SetCC.getConstantOperandVal(0));
14988 if (needOppositeCond)
14989 CC = X86::GetOppositeBranchCondition(CC);
14990 return SetCC.getOperand(1);
14991 case X86ISD::CMOV: {
14992 // Check whether false/true value has canonical one, i.e. 0 or 1.
14993 ConstantSDNode *FVal = dyn_cast<ConstantSDNode>(SetCC.getOperand(0));
14994 ConstantSDNode *TVal = dyn_cast<ConstantSDNode>(SetCC.getOperand(1));
14995 // Quit if true value is not a constant.
14996 if (!TVal)
14997 return SDValue();
14998 // Quit if false value is not a constant.
14999 if (!FVal) {
15000 // A special case for rdrand, where 0 is set if false cond is found.
15001 SDValue Op = SetCC.getOperand(0);
15002 if (Op.getOpcode() != X86ISD::RDRAND)
15003 return SDValue();
15004 }
15005 // Quit if false value is not the constant 0 or 1.
15006 bool FValIsFalse = true;
15007 if (FVal && FVal->getZExtValue() != 0) {
15008 if (FVal->getZExtValue() != 1)
15009 return SDValue();
15010 // If FVal is 1, opposite cond is needed.
15011 needOppositeCond = !needOppositeCond;
15012 FValIsFalse = false;
15013 }
15014 // Quit if TVal is not the constant opposite of FVal.
15015 if (FValIsFalse && TVal->getZExtValue() != 1)
15016 return SDValue();
15017 if (!FValIsFalse && TVal->getZExtValue() != 0)
15018 return SDValue();
15019 CC = X86::CondCode(SetCC.getConstantOperandVal(2));
15020 if (needOppositeCond)
15021 CC = X86::GetOppositeBranchCondition(CC);
15022 return SetCC.getOperand(3);
15023 }
15024 }
Michael Liao2a33cec2012-08-10 19:58:13 +000015025
Michael Liao7fdc66b2012-09-10 16:36:16 +000015026 return SDValue();
Michael Liao2a33cec2012-08-10 19:58:13 +000015027}
15028
Chris Lattnerd1980a52009-03-12 06:52:53 +000015029/// Optimize X86ISD::CMOV [LHS, RHS, CONDCODE (e.g. X86::COND_NE), CONDVAL]
15030static SDValue PerformCMOVCombine(SDNode *N, SelectionDAG &DAG,
Michael Liaodbf8b5b2012-08-28 03:34:40 +000015031 TargetLowering::DAGCombinerInfo &DCI,
15032 const X86Subtarget *Subtarget) {
Chris Lattnerd1980a52009-03-12 06:52:53 +000015033 DebugLoc DL = N->getDebugLoc();
Eric Christopherfd179292009-08-27 18:07:15 +000015034
Chris Lattnerd1980a52009-03-12 06:52:53 +000015035 // If the flag operand isn't dead, don't touch this CMOV.
15036 if (N->getNumValues() == 2 && !SDValue(N, 1).use_empty())
15037 return SDValue();
Eric Christopherfd179292009-08-27 18:07:15 +000015038
Evan Chengb5a55d92011-05-24 01:48:22 +000015039 SDValue FalseOp = N->getOperand(0);
15040 SDValue TrueOp = N->getOperand(1);
15041 X86::CondCode CC = (X86::CondCode)N->getConstantOperandVal(2);
15042 SDValue Cond = N->getOperand(3);
Michael Liao2a33cec2012-08-10 19:58:13 +000015043
Evan Chengb5a55d92011-05-24 01:48:22 +000015044 if (CC == X86::COND_E || CC == X86::COND_NE) {
15045 switch (Cond.getOpcode()) {
15046 default: break;
15047 case X86ISD::BSR:
15048 case X86ISD::BSF:
15049 // If operand of BSR / BSF are proven never zero, then ZF cannot be set.
15050 if (DAG.isKnownNeverZero(Cond.getOperand(0)))
15051 return (CC == X86::COND_E) ? FalseOp : TrueOp;
15052 }
15053 }
15054
Michael Liao2a33cec2012-08-10 19:58:13 +000015055 SDValue Flags;
15056
Michael Liaodbf8b5b2012-08-28 03:34:40 +000015057 Flags = checkBoolTestSetCCCombine(Cond, CC);
Michael Liao9eac20a2012-08-11 23:47:06 +000015058 if (Flags.getNode() &&
15059 // Extra check as FCMOV only supports a subset of X86 cond.
Michael Liao7859f432012-09-06 07:11:22 +000015060 (FalseOp.getValueType() != MVT::f80 || hasFPCMov(CC))) {
Michael Liaodbf8b5b2012-08-28 03:34:40 +000015061 SDValue Ops[] = { FalseOp, TrueOp,
15062 DAG.getConstant(CC, MVT::i8), Flags };
15063 return DAG.getNode(X86ISD::CMOV, DL, N->getVTList(),
15064 Ops, array_lengthof(Ops));
15065 }
15066
Chris Lattnerd1980a52009-03-12 06:52:53 +000015067 // If this is a select between two integer constants, try to do some
15068 // optimizations. Note that the operands are ordered the opposite of SELECT
15069 // operands.
Evan Chengb5a55d92011-05-24 01:48:22 +000015070 if (ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(TrueOp)) {
15071 if (ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(FalseOp)) {
Chris Lattnerd1980a52009-03-12 06:52:53 +000015072 // Canonicalize the TrueC/FalseC values so that TrueC (the true value) is
15073 // larger than FalseC (the false value).
Chris Lattnerd1980a52009-03-12 06:52:53 +000015074 if (TrueC->getAPIntValue().ult(FalseC->getAPIntValue())) {
15075 CC = X86::GetOppositeBranchCondition(CC);
15076 std::swap(TrueC, FalseC);
NAKAMURA Takumie2687452012-10-16 06:28:34 +000015077 std::swap(TrueOp, FalseOp);
Chris Lattnerd1980a52009-03-12 06:52:53 +000015078 }
Eric Christopherfd179292009-08-27 18:07:15 +000015079
Chris Lattnerd1980a52009-03-12 06:52:53 +000015080 // Optimize C ? 8 : 0 -> zext(setcc(C)) << 3. Likewise for any pow2/0.
Chris Lattnercee56e72009-03-13 05:53:31 +000015081 // This is efficient for any integer data type (including i8/i16) and
15082 // shift amount.
Chris Lattnerd1980a52009-03-12 06:52:53 +000015083 if (FalseC->getAPIntValue() == 0 && TrueC->getAPIntValue().isPowerOf2()) {
Owen Anderson825b72b2009-08-11 20:47:22 +000015084 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
15085 DAG.getConstant(CC, MVT::i8), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000015086
Chris Lattnerd1980a52009-03-12 06:52:53 +000015087 // Zero extend the condition if needed.
15088 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, TrueC->getValueType(0), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000015089
Chris Lattnerd1980a52009-03-12 06:52:53 +000015090 unsigned ShAmt = TrueC->getAPIntValue().logBase2();
15091 Cond = DAG.getNode(ISD::SHL, DL, Cond.getValueType(), Cond,
Owen Anderson825b72b2009-08-11 20:47:22 +000015092 DAG.getConstant(ShAmt, MVT::i8));
Chris Lattnerd1980a52009-03-12 06:52:53 +000015093 if (N->getNumValues() == 2) // Dead flag value?
15094 return DCI.CombineTo(N, Cond, SDValue());
15095 return Cond;
15096 }
Eric Christopherfd179292009-08-27 18:07:15 +000015097
Chris Lattnercee56e72009-03-13 05:53:31 +000015098 // Optimize Cond ? cst+1 : cst -> zext(setcc(C)+cst. This is efficient
15099 // for any integer data type, including i8/i16.
Chris Lattner97a29a52009-03-13 05:22:11 +000015100 if (FalseC->getAPIntValue()+1 == TrueC->getAPIntValue()) {
Owen Anderson825b72b2009-08-11 20:47:22 +000015101 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
15102 DAG.getConstant(CC, MVT::i8), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000015103
Chris Lattner97a29a52009-03-13 05:22:11 +000015104 // Zero extend the condition if needed.
Chris Lattnercee56e72009-03-13 05:53:31 +000015105 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL,
15106 FalseC->getValueType(0), Cond);
Chris Lattner97a29a52009-03-13 05:22:11 +000015107 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
15108 SDValue(FalseC, 0));
Eric Christopherfd179292009-08-27 18:07:15 +000015109
Chris Lattner97a29a52009-03-13 05:22:11 +000015110 if (N->getNumValues() == 2) // Dead flag value?
15111 return DCI.CombineTo(N, Cond, SDValue());
15112 return Cond;
15113 }
Eric Christopherfd179292009-08-27 18:07:15 +000015114
Chris Lattnercee56e72009-03-13 05:53:31 +000015115 // Optimize cases that will turn into an LEA instruction. This requires
15116 // an i32 or i64 and an efficient multiplier (1, 2, 3, 4, 5, 8, 9).
Owen Anderson825b72b2009-08-11 20:47:22 +000015117 if (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i64) {
Chris Lattnercee56e72009-03-13 05:53:31 +000015118 uint64_t Diff = TrueC->getZExtValue()-FalseC->getZExtValue();
Owen Anderson825b72b2009-08-11 20:47:22 +000015119 if (N->getValueType(0) == MVT::i32) Diff = (unsigned)Diff;
Eric Christopherfd179292009-08-27 18:07:15 +000015120
Chris Lattnercee56e72009-03-13 05:53:31 +000015121 bool isFastMultiplier = false;
15122 if (Diff < 10) {
15123 switch ((unsigned char)Diff) {
15124 default: break;
15125 case 1: // result = add base, cond
15126 case 2: // result = lea base( , cond*2)
15127 case 3: // result = lea base(cond, cond*2)
15128 case 4: // result = lea base( , cond*4)
15129 case 5: // result = lea base(cond, cond*4)
15130 case 8: // result = lea base( , cond*8)
15131 case 9: // result = lea base(cond, cond*8)
15132 isFastMultiplier = true;
15133 break;
15134 }
15135 }
Eric Christopherfd179292009-08-27 18:07:15 +000015136
Chris Lattnercee56e72009-03-13 05:53:31 +000015137 if (isFastMultiplier) {
15138 APInt Diff = TrueC->getAPIntValue()-FalseC->getAPIntValue();
Owen Anderson825b72b2009-08-11 20:47:22 +000015139 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
15140 DAG.getConstant(CC, MVT::i8), Cond);
Chris Lattnercee56e72009-03-13 05:53:31 +000015141 // Zero extend the condition if needed.
15142 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, FalseC->getValueType(0),
15143 Cond);
15144 // Scale the condition by the difference.
15145 if (Diff != 1)
15146 Cond = DAG.getNode(ISD::MUL, DL, Cond.getValueType(), Cond,
15147 DAG.getConstant(Diff, Cond.getValueType()));
15148
15149 // Add the base if non-zero.
15150 if (FalseC->getAPIntValue() != 0)
15151 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
15152 SDValue(FalseC, 0));
15153 if (N->getNumValues() == 2) // Dead flag value?
15154 return DCI.CombineTo(N, Cond, SDValue());
15155 return Cond;
15156 }
Eric Christopherfd179292009-08-27 18:07:15 +000015157 }
Chris Lattnerd1980a52009-03-12 06:52:53 +000015158 }
15159 }
NAKAMURA Takumie2687452012-10-16 06:28:34 +000015160
15161 // Handle these cases:
15162 // (select (x != c), e, c) -> select (x != c), e, x),
15163 // (select (x == c), c, e) -> select (x == c), x, e)
15164 // where the c is an integer constant, and the "select" is the combination
15165 // of CMOV and CMP.
15166 //
15167 // The rationale for this change is that the conditional-move from a constant
15168 // needs two instructions, however, conditional-move from a register needs
15169 // only one instruction.
15170 //
15171 // CAVEAT: By replacing a constant with a symbolic value, it may obscure
15172 // some instruction-combining opportunities. This opt needs to be
15173 // postponed as late as possible.
15174 //
15175 if (!DCI.isBeforeLegalize() && !DCI.isBeforeLegalizeOps()) {
15176 // the DCI.xxxx conditions are provided to postpone the optimization as
15177 // late as possible.
15178
15179 ConstantSDNode *CmpAgainst = 0;
15180 if ((Cond.getOpcode() == X86ISD::CMP || Cond.getOpcode() == X86ISD::SUB) &&
15181 (CmpAgainst = dyn_cast<ConstantSDNode>(Cond.getOperand(1))) &&
15182 dyn_cast<ConstantSDNode>(Cond.getOperand(0)) == 0) {
15183
15184 if (CC == X86::COND_NE &&
15185 CmpAgainst == dyn_cast<ConstantSDNode>(FalseOp)) {
15186 CC = X86::GetOppositeBranchCondition(CC);
15187 std::swap(TrueOp, FalseOp);
15188 }
15189
15190 if (CC == X86::COND_E &&
15191 CmpAgainst == dyn_cast<ConstantSDNode>(TrueOp)) {
15192 SDValue Ops[] = { FalseOp, Cond.getOperand(0),
15193 DAG.getConstant(CC, MVT::i8), Cond };
15194 return DAG.getNode(X86ISD::CMOV, DL, N->getVTList (), Ops,
15195 array_lengthof(Ops));
15196 }
15197 }
15198 }
15199
Chris Lattnerd1980a52009-03-12 06:52:53 +000015200 return SDValue();
15201}
15202
15203
Evan Cheng0b0cd912009-03-28 05:57:29 +000015204/// PerformMulCombine - Optimize a single multiply with constant into two
15205/// in order to implement it with two cheaper instructions, e.g.
15206/// LEA + SHL, LEA + LEA.
15207static SDValue PerformMulCombine(SDNode *N, SelectionDAG &DAG,
15208 TargetLowering::DAGCombinerInfo &DCI) {
Evan Cheng0b0cd912009-03-28 05:57:29 +000015209 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
15210 return SDValue();
15211
Owen Andersone50ed302009-08-10 22:56:29 +000015212 EVT VT = N->getValueType(0);
Owen Anderson825b72b2009-08-11 20:47:22 +000015213 if (VT != MVT::i64)
Evan Cheng0b0cd912009-03-28 05:57:29 +000015214 return SDValue();
15215
15216 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1));
15217 if (!C)
15218 return SDValue();
15219 uint64_t MulAmt = C->getZExtValue();
15220 if (isPowerOf2_64(MulAmt) || MulAmt == 3 || MulAmt == 5 || MulAmt == 9)
15221 return SDValue();
15222
15223 uint64_t MulAmt1 = 0;
15224 uint64_t MulAmt2 = 0;
15225 if ((MulAmt % 9) == 0) {
15226 MulAmt1 = 9;
15227 MulAmt2 = MulAmt / 9;
15228 } else if ((MulAmt % 5) == 0) {
15229 MulAmt1 = 5;
15230 MulAmt2 = MulAmt / 5;
15231 } else if ((MulAmt % 3) == 0) {
15232 MulAmt1 = 3;
15233 MulAmt2 = MulAmt / 3;
15234 }
15235 if (MulAmt2 &&
15236 (isPowerOf2_64(MulAmt2) || MulAmt2 == 3 || MulAmt2 == 5 || MulAmt2 == 9)){
15237 DebugLoc DL = N->getDebugLoc();
15238
15239 if (isPowerOf2_64(MulAmt2) &&
15240 !(N->hasOneUse() && N->use_begin()->getOpcode() == ISD::ADD))
15241 // If second multiplifer is pow2, issue it first. We want the multiply by
15242 // 3, 5, or 9 to be folded into the addressing mode unless the lone use
15243 // is an add.
15244 std::swap(MulAmt1, MulAmt2);
15245
15246 SDValue NewMul;
Eric Christopherfd179292009-08-27 18:07:15 +000015247 if (isPowerOf2_64(MulAmt1))
Evan Cheng0b0cd912009-03-28 05:57:29 +000015248 NewMul = DAG.getNode(ISD::SHL, DL, VT, N->getOperand(0),
Owen Anderson825b72b2009-08-11 20:47:22 +000015249 DAG.getConstant(Log2_64(MulAmt1), MVT::i8));
Evan Cheng0b0cd912009-03-28 05:57:29 +000015250 else
Evan Cheng73f24c92009-03-30 21:36:47 +000015251 NewMul = DAG.getNode(X86ISD::MUL_IMM, DL, VT, N->getOperand(0),
Evan Cheng0b0cd912009-03-28 05:57:29 +000015252 DAG.getConstant(MulAmt1, VT));
15253
Eric Christopherfd179292009-08-27 18:07:15 +000015254 if (isPowerOf2_64(MulAmt2))
Evan Cheng0b0cd912009-03-28 05:57:29 +000015255 NewMul = DAG.getNode(ISD::SHL, DL, VT, NewMul,
Owen Anderson825b72b2009-08-11 20:47:22 +000015256 DAG.getConstant(Log2_64(MulAmt2), MVT::i8));
Eric Christopherfd179292009-08-27 18:07:15 +000015257 else
Evan Cheng73f24c92009-03-30 21:36:47 +000015258 NewMul = DAG.getNode(X86ISD::MUL_IMM, DL, VT, NewMul,
Evan Cheng0b0cd912009-03-28 05:57:29 +000015259 DAG.getConstant(MulAmt2, VT));
15260
15261 // Do not add new nodes to DAG combiner worklist.
15262 DCI.CombineTo(N, NewMul, false);
15263 }
15264 return SDValue();
15265}
15266
Evan Chengad9c0a32009-12-15 00:53:42 +000015267static SDValue PerformSHLCombine(SDNode *N, SelectionDAG &DAG) {
15268 SDValue N0 = N->getOperand(0);
15269 SDValue N1 = N->getOperand(1);
15270 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
15271 EVT VT = N0.getValueType();
15272
15273 // fold (shl (and (setcc_c), c1), c2) -> (and setcc_c, (c1 << c2))
15274 // since the result of setcc_c is all zero's or all ones.
Nadav Rotemfb0dfbb2011-10-30 13:24:22 +000015275 if (VT.isInteger() && !VT.isVector() &&
15276 N1C && N0.getOpcode() == ISD::AND &&
Evan Chengad9c0a32009-12-15 00:53:42 +000015277 N0.getOperand(1).getOpcode() == ISD::Constant) {
15278 SDValue N00 = N0.getOperand(0);
15279 if (N00.getOpcode() == X86ISD::SETCC_CARRY ||
15280 ((N00.getOpcode() == ISD::ANY_EXTEND ||
15281 N00.getOpcode() == ISD::ZERO_EXTEND) &&
15282 N00.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY)) {
15283 APInt Mask = cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
15284 APInt ShAmt = N1C->getAPIntValue();
15285 Mask = Mask.shl(ShAmt);
15286 if (Mask != 0)
15287 return DAG.getNode(ISD::AND, N->getDebugLoc(), VT,
15288 N00, DAG.getConstant(Mask, VT));
15289 }
15290 }
15291
Nadav Rotemfb0dfbb2011-10-30 13:24:22 +000015292
15293 // Hardware support for vector shifts is sparse which makes us scalarize the
15294 // vector operations in many cases. Also, on sandybridge ADD is faster than
15295 // shl.
15296 // (shl V, 1) -> add V,V
15297 if (isSplatVector(N1.getNode())) {
15298 assert(N0.getValueType().isVector() && "Invalid vector shift type");
15299 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1->getOperand(0));
15300 // We shift all of the values by one. In many cases we do not have
15301 // hardware support for this operation. This is better expressed as an ADD
15302 // of two values.
15303 if (N1C && (1 == N1C->getZExtValue())) {
15304 return DAG.getNode(ISD::ADD, N->getDebugLoc(), VT, N0, N0);
15305 }
15306 }
15307
Evan Chengad9c0a32009-12-15 00:53:42 +000015308 return SDValue();
15309}
Evan Cheng0b0cd912009-03-28 05:57:29 +000015310
Nate Begeman740ab032009-01-26 00:52:55 +000015311/// PerformShiftCombine - Transforms vector shift nodes to use vector shifts
15312/// when possible.
15313static SDValue PerformShiftCombine(SDNode* N, SelectionDAG &DAG,
Mon P Wang845b1892012-02-01 22:15:20 +000015314 TargetLowering::DAGCombinerInfo &DCI,
Nate Begeman740ab032009-01-26 00:52:55 +000015315 const X86Subtarget *Subtarget) {
Evan Chengad9c0a32009-12-15 00:53:42 +000015316 EVT VT = N->getValueType(0);
Nadav Rotemfb0dfbb2011-10-30 13:24:22 +000015317 if (N->getOpcode() == ISD::SHL) {
15318 SDValue V = PerformSHLCombine(N, DAG);
15319 if (V.getNode()) return V;
15320 }
Evan Chengad9c0a32009-12-15 00:53:42 +000015321
Nate Begeman740ab032009-01-26 00:52:55 +000015322 // On X86 with SSE2 support, we can transform this to a vector shift if
15323 // all elements are shifted by the same amount. We can't do this in legalize
15324 // because the a constant vector is typically transformed to a constant pool
15325 // so we have no knowledge of the shift amount.
Craig Topper1accb7e2012-01-10 06:54:16 +000015326 if (!Subtarget->hasSSE2())
Nate Begemanc2fd67f2009-01-26 03:15:31 +000015327 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +000015328
Craig Topper7be5dfd2011-11-12 09:58:49 +000015329 if (VT != MVT::v2i64 && VT != MVT::v4i32 && VT != MVT::v8i16 &&
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000015330 (!Subtarget->hasInt256() ||
Craig Topper7be5dfd2011-11-12 09:58:49 +000015331 (VT != MVT::v4i64 && VT != MVT::v8i32 && VT != MVT::v16i16)))
Nate Begemanc2fd67f2009-01-26 03:15:31 +000015332 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +000015333
Mon P Wang3becd092009-01-28 08:12:05 +000015334 SDValue ShAmtOp = N->getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +000015335 EVT EltVT = VT.getVectorElementType();
Chris Lattner47b4ce82009-03-11 05:48:52 +000015336 DebugLoc DL = N->getDebugLoc();
Mon P Wangefa42202009-09-03 19:56:25 +000015337 SDValue BaseShAmt = SDValue();
Mon P Wang3becd092009-01-28 08:12:05 +000015338 if (ShAmtOp.getOpcode() == ISD::BUILD_VECTOR) {
15339 unsigned NumElts = VT.getVectorNumElements();
15340 unsigned i = 0;
15341 for (; i != NumElts; ++i) {
15342 SDValue Arg = ShAmtOp.getOperand(i);
15343 if (Arg.getOpcode() == ISD::UNDEF) continue;
15344 BaseShAmt = Arg;
15345 break;
15346 }
Craig Topper37c26772012-01-17 04:44:50 +000015347 // Handle the case where the build_vector is all undef
15348 // FIXME: Should DAG allow this?
15349 if (i == NumElts)
15350 return SDValue();
15351
Mon P Wang3becd092009-01-28 08:12:05 +000015352 for (; i != NumElts; ++i) {
15353 SDValue Arg = ShAmtOp.getOperand(i);
15354 if (Arg.getOpcode() == ISD::UNDEF) continue;
15355 if (Arg != BaseShAmt) {
15356 return SDValue();
15357 }
15358 }
15359 } else if (ShAmtOp.getOpcode() == ISD::VECTOR_SHUFFLE &&
Nate Begeman9008ca62009-04-27 18:41:29 +000015360 cast<ShuffleVectorSDNode>(ShAmtOp)->isSplat()) {
Mon P Wangefa42202009-09-03 19:56:25 +000015361 SDValue InVec = ShAmtOp.getOperand(0);
15362 if (InVec.getOpcode() == ISD::BUILD_VECTOR) {
15363 unsigned NumElts = InVec.getValueType().getVectorNumElements();
15364 unsigned i = 0;
15365 for (; i != NumElts; ++i) {
15366 SDValue Arg = InVec.getOperand(i);
15367 if (Arg.getOpcode() == ISD::UNDEF) continue;
15368 BaseShAmt = Arg;
15369 break;
15370 }
15371 } else if (InVec.getOpcode() == ISD::INSERT_VECTOR_ELT) {
15372 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(InVec.getOperand(2))) {
Evan Chengae3ecf92010-02-16 21:09:44 +000015373 unsigned SplatIdx= cast<ShuffleVectorSDNode>(ShAmtOp)->getSplatIndex();
Mon P Wangefa42202009-09-03 19:56:25 +000015374 if (C->getZExtValue() == SplatIdx)
15375 BaseShAmt = InVec.getOperand(1);
15376 }
15377 }
Mon P Wang845b1892012-02-01 22:15:20 +000015378 if (BaseShAmt.getNode() == 0) {
15379 // Don't create instructions with illegal types after legalize
15380 // types has run.
15381 if (!DAG.getTargetLoweringInfo().isTypeLegal(EltVT) &&
15382 !DCI.isBeforeLegalize())
15383 return SDValue();
15384
Mon P Wangefa42202009-09-03 19:56:25 +000015385 BaseShAmt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, EltVT, ShAmtOp,
15386 DAG.getIntPtrConstant(0));
Mon P Wang845b1892012-02-01 22:15:20 +000015387 }
Mon P Wang3becd092009-01-28 08:12:05 +000015388 } else
Nate Begemanc2fd67f2009-01-26 03:15:31 +000015389 return SDValue();
Nate Begeman740ab032009-01-26 00:52:55 +000015390
Mon P Wangefa42202009-09-03 19:56:25 +000015391 // The shift amount is an i32.
Owen Anderson825b72b2009-08-11 20:47:22 +000015392 if (EltVT.bitsGT(MVT::i32))
15393 BaseShAmt = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, BaseShAmt);
15394 else if (EltVT.bitsLT(MVT::i32))
Mon P Wangefa42202009-09-03 19:56:25 +000015395 BaseShAmt = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::i32, BaseShAmt);
Nate Begeman740ab032009-01-26 00:52:55 +000015396
Nate Begemanc2fd67f2009-01-26 03:15:31 +000015397 // The shift amount is identical so we can do a vector shift.
15398 SDValue ValOp = N->getOperand(0);
15399 switch (N->getOpcode()) {
15400 default:
Torok Edwinc23197a2009-07-14 16:55:14 +000015401 llvm_unreachable("Unknown shift opcode!");
Nate Begemanc2fd67f2009-01-26 03:15:31 +000015402 case ISD::SHL:
Craig Toppered2e13d2012-01-22 19:15:14 +000015403 switch (VT.getSimpleVT().SimpleTy) {
15404 default: return SDValue();
15405 case MVT::v2i64:
15406 case MVT::v4i32:
15407 case MVT::v8i16:
15408 case MVT::v4i64:
15409 case MVT::v8i32:
15410 case MVT::v16i16:
15411 return getTargetVShiftNode(X86ISD::VSHLI, DL, VT, ValOp, BaseShAmt, DAG);
15412 }
Nate Begemanc2fd67f2009-01-26 03:15:31 +000015413 case ISD::SRA:
Craig Toppered2e13d2012-01-22 19:15:14 +000015414 switch (VT.getSimpleVT().SimpleTy) {
15415 default: return SDValue();
15416 case MVT::v4i32:
15417 case MVT::v8i16:
15418 case MVT::v8i32:
15419 case MVT::v16i16:
15420 return getTargetVShiftNode(X86ISD::VSRAI, DL, VT, ValOp, BaseShAmt, DAG);
15421 }
Nate Begemanc2fd67f2009-01-26 03:15:31 +000015422 case ISD::SRL:
Craig Toppered2e13d2012-01-22 19:15:14 +000015423 switch (VT.getSimpleVT().SimpleTy) {
15424 default: return SDValue();
15425 case MVT::v2i64:
15426 case MVT::v4i32:
15427 case MVT::v8i16:
15428 case MVT::v4i64:
15429 case MVT::v8i32:
15430 case MVT::v16i16:
15431 return getTargetVShiftNode(X86ISD::VSRLI, DL, VT, ValOp, BaseShAmt, DAG);
15432 }
Nate Begeman740ab032009-01-26 00:52:55 +000015433 }
Nate Begeman740ab032009-01-26 00:52:55 +000015434}
15435
Nate Begemanb65c1752010-12-17 22:55:37 +000015436
Stuart Hastings865f0932011-06-03 23:53:54 +000015437// CMPEQCombine - Recognize the distinctive (AND (setcc ...) (setcc ..))
15438// where both setccs reference the same FP CMP, and rewrite for CMPEQSS
15439// and friends. Likewise for OR -> CMPNEQSS.
15440static SDValue CMPEQCombine(SDNode *N, SelectionDAG &DAG,
15441 TargetLowering::DAGCombinerInfo &DCI,
15442 const X86Subtarget *Subtarget) {
15443 unsigned opcode;
15444
15445 // SSE1 supports CMP{eq|ne}SS, and SSE2 added CMP{eq|ne}SD, but
15446 // we're requiring SSE2 for both.
Craig Topper1accb7e2012-01-10 06:54:16 +000015447 if (Subtarget->hasSSE2() && isAndOrOfSetCCs(SDValue(N, 0U), opcode)) {
Stuart Hastings865f0932011-06-03 23:53:54 +000015448 SDValue N0 = N->getOperand(0);
15449 SDValue N1 = N->getOperand(1);
15450 SDValue CMP0 = N0->getOperand(1);
15451 SDValue CMP1 = N1->getOperand(1);
15452 DebugLoc DL = N->getDebugLoc();
15453
15454 // The SETCCs should both refer to the same CMP.
15455 if (CMP0.getOpcode() != X86ISD::CMP || CMP0 != CMP1)
15456 return SDValue();
15457
15458 SDValue CMP00 = CMP0->getOperand(0);
15459 SDValue CMP01 = CMP0->getOperand(1);
15460 EVT VT = CMP00.getValueType();
15461
15462 if (VT == MVT::f32 || VT == MVT::f64) {
15463 bool ExpectingFlags = false;
15464 // Check for any users that want flags:
15465 for (SDNode::use_iterator UI = N->use_begin(),
15466 UE = N->use_end();
15467 !ExpectingFlags && UI != UE; ++UI)
15468 switch (UI->getOpcode()) {
15469 default:
15470 case ISD::BR_CC:
15471 case ISD::BRCOND:
15472 case ISD::SELECT:
15473 ExpectingFlags = true;
15474 break;
15475 case ISD::CopyToReg:
15476 case ISD::SIGN_EXTEND:
15477 case ISD::ZERO_EXTEND:
15478 case ISD::ANY_EXTEND:
15479 break;
15480 }
15481
15482 if (!ExpectingFlags) {
15483 enum X86::CondCode cc0 = (enum X86::CondCode)N0.getConstantOperandVal(0);
15484 enum X86::CondCode cc1 = (enum X86::CondCode)N1.getConstantOperandVal(0);
15485
15486 if (cc1 == X86::COND_E || cc1 == X86::COND_NE) {
15487 X86::CondCode tmp = cc0;
15488 cc0 = cc1;
15489 cc1 = tmp;
15490 }
15491
15492 if ((cc0 == X86::COND_E && cc1 == X86::COND_NP) ||
15493 (cc0 == X86::COND_NE && cc1 == X86::COND_P)) {
15494 bool is64BitFP = (CMP00.getValueType() == MVT::f64);
15495 X86ISD::NodeType NTOperator = is64BitFP ?
15496 X86ISD::FSETCCsd : X86ISD::FSETCCss;
15497 // FIXME: need symbolic constants for these magic numbers.
15498 // See X86ATTInstPrinter.cpp:printSSECC().
15499 unsigned x86cc = (cc0 == X86::COND_E) ? 0 : 4;
15500 SDValue OnesOrZeroesF = DAG.getNode(NTOperator, DL, MVT::f32, CMP00, CMP01,
15501 DAG.getConstant(x86cc, MVT::i8));
15502 SDValue OnesOrZeroesI = DAG.getNode(ISD::BITCAST, DL, MVT::i32,
15503 OnesOrZeroesF);
15504 SDValue ANDed = DAG.getNode(ISD::AND, DL, MVT::i32, OnesOrZeroesI,
15505 DAG.getConstant(1, MVT::i32));
15506 SDValue OneBitOfTruth = DAG.getNode(ISD::TRUNCATE, DL, MVT::i8, ANDed);
15507 return OneBitOfTruth;
15508 }
15509 }
15510 }
15511 }
15512 return SDValue();
15513}
15514
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +000015515/// CanFoldXORWithAllOnes - Test whether the XOR operand is a AllOnes vector
15516/// so it can be folded inside ANDNP.
15517static bool CanFoldXORWithAllOnes(const SDNode *N) {
15518 EVT VT = N->getValueType(0);
15519
15520 // Match direct AllOnes for 128 and 256-bit vectors
15521 if (ISD::isBuildVectorAllOnes(N))
15522 return true;
15523
15524 // Look through a bit convert.
15525 if (N->getOpcode() == ISD::BITCAST)
15526 N = N->getOperand(0).getNode();
15527
15528 // Sometimes the operand may come from a insert_subvector building a 256-bit
15529 // allones vector
Craig Topper7a9a28b2012-08-12 02:23:29 +000015530 if (VT.is256BitVector() &&
Bill Wendling456a9252011-08-04 00:32:58 +000015531 N->getOpcode() == ISD::INSERT_SUBVECTOR) {
15532 SDValue V1 = N->getOperand(0);
15533 SDValue V2 = N->getOperand(1);
15534
15535 if (V1.getOpcode() == ISD::INSERT_SUBVECTOR &&
15536 V1.getOperand(0).getOpcode() == ISD::UNDEF &&
15537 ISD::isBuildVectorAllOnes(V1.getOperand(1).getNode()) &&
15538 ISD::isBuildVectorAllOnes(V2.getNode()))
15539 return true;
15540 }
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +000015541
15542 return false;
15543}
15544
Nate Begemanb65c1752010-12-17 22:55:37 +000015545static SDValue PerformAndCombine(SDNode *N, SelectionDAG &DAG,
15546 TargetLowering::DAGCombinerInfo &DCI,
15547 const X86Subtarget *Subtarget) {
15548 if (DCI.isBeforeLegalizeOps())
15549 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000015550
Stuart Hastings865f0932011-06-03 23:53:54 +000015551 SDValue R = CMPEQCombine(N, DAG, DCI, Subtarget);
15552 if (R.getNode())
15553 return R;
15554
Craig Topper54a11172011-10-14 07:06:56 +000015555 EVT VT = N->getValueType(0);
15556
Craig Topperb4c94572011-10-21 06:55:01 +000015557 // Create ANDN, BLSI, and BLSR instructions
15558 // BLSI is X & (-X)
15559 // BLSR is X & (X-1)
Craig Topper54a11172011-10-14 07:06:56 +000015560 if (Subtarget->hasBMI() && (VT == MVT::i32 || VT == MVT::i64)) {
15561 SDValue N0 = N->getOperand(0);
15562 SDValue N1 = N->getOperand(1);
15563 DebugLoc DL = N->getDebugLoc();
15564
15565 // Check LHS for not
15566 if (N0.getOpcode() == ISD::XOR && isAllOnes(N0.getOperand(1)))
15567 return DAG.getNode(X86ISD::ANDN, DL, VT, N0.getOperand(0), N1);
15568 // Check RHS for not
15569 if (N1.getOpcode() == ISD::XOR && isAllOnes(N1.getOperand(1)))
15570 return DAG.getNode(X86ISD::ANDN, DL, VT, N1.getOperand(0), N0);
15571
Craig Topperb4c94572011-10-21 06:55:01 +000015572 // Check LHS for neg
15573 if (N0.getOpcode() == ISD::SUB && N0.getOperand(1) == N1 &&
15574 isZero(N0.getOperand(0)))
15575 return DAG.getNode(X86ISD::BLSI, DL, VT, N1);
15576
15577 // Check RHS for neg
15578 if (N1.getOpcode() == ISD::SUB && N1.getOperand(1) == N0 &&
15579 isZero(N1.getOperand(0)))
15580 return DAG.getNode(X86ISD::BLSI, DL, VT, N0);
15581
15582 // Check LHS for X-1
15583 if (N0.getOpcode() == ISD::ADD && N0.getOperand(0) == N1 &&
15584 isAllOnes(N0.getOperand(1)))
15585 return DAG.getNode(X86ISD::BLSR, DL, VT, N1);
15586
15587 // Check RHS for X-1
15588 if (N1.getOpcode() == ISD::ADD && N1.getOperand(0) == N0 &&
15589 isAllOnes(N1.getOperand(1)))
15590 return DAG.getNode(X86ISD::BLSR, DL, VT, N0);
15591
Craig Topper54a11172011-10-14 07:06:56 +000015592 return SDValue();
15593 }
15594
Bruno Cardoso Lopes466b0222011-07-13 21:36:51 +000015595 // Want to form ANDNP nodes:
15596 // 1) In the hopes of then easily combining them with OR and AND nodes
15597 // to form PBLEND/PSIGN.
15598 // 2) To match ANDN packed intrinsics
Bruno Cardoso Lopes466b0222011-07-13 21:36:51 +000015599 if (VT != MVT::v2i64 && VT != MVT::v4i64)
Nate Begemanb65c1752010-12-17 22:55:37 +000015600 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000015601
Nate Begemanb65c1752010-12-17 22:55:37 +000015602 SDValue N0 = N->getOperand(0);
15603 SDValue N1 = N->getOperand(1);
15604 DebugLoc DL = N->getDebugLoc();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000015605
Nate Begemanb65c1752010-12-17 22:55:37 +000015606 // Check LHS for vnot
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000015607 if (N0.getOpcode() == ISD::XOR &&
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +000015608 //ISD::isBuildVectorAllOnes(N0.getOperand(1).getNode()))
15609 CanFoldXORWithAllOnes(N0.getOperand(1).getNode()))
Bruno Cardoso Lopesc1af4772011-07-13 21:36:47 +000015610 return DAG.getNode(X86ISD::ANDNP, DL, VT, N0.getOperand(0), N1);
Nate Begemanb65c1752010-12-17 22:55:37 +000015611
15612 // Check RHS for vnot
15613 if (N1.getOpcode() == ISD::XOR &&
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +000015614 //ISD::isBuildVectorAllOnes(N1.getOperand(1).getNode()))
15615 CanFoldXORWithAllOnes(N1.getOperand(1).getNode()))
Bruno Cardoso Lopesc1af4772011-07-13 21:36:47 +000015616 return DAG.getNode(X86ISD::ANDNP, DL, VT, N1.getOperand(0), N0);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000015617
Nate Begemanb65c1752010-12-17 22:55:37 +000015618 return SDValue();
15619}
15620
Evan Cheng760d1942010-01-04 21:22:48 +000015621static SDValue PerformOrCombine(SDNode *N, SelectionDAG &DAG,
Evan Cheng8b1190a2010-04-28 01:18:01 +000015622 TargetLowering::DAGCombinerInfo &DCI,
Evan Cheng760d1942010-01-04 21:22:48 +000015623 const X86Subtarget *Subtarget) {
Evan Cheng39cfeec2010-04-28 02:25:18 +000015624 if (DCI.isBeforeLegalizeOps())
Evan Cheng8b1190a2010-04-28 01:18:01 +000015625 return SDValue();
15626
Stuart Hastings865f0932011-06-03 23:53:54 +000015627 SDValue R = CMPEQCombine(N, DAG, DCI, Subtarget);
15628 if (R.getNode())
15629 return R;
15630
Evan Cheng760d1942010-01-04 21:22:48 +000015631 EVT VT = N->getValueType(0);
Evan Cheng760d1942010-01-04 21:22:48 +000015632
Evan Cheng760d1942010-01-04 21:22:48 +000015633 SDValue N0 = N->getOperand(0);
15634 SDValue N1 = N->getOperand(1);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000015635
Nate Begemanb65c1752010-12-17 22:55:37 +000015636 // look for psign/blend
Craig Topper1666cb62011-11-19 07:07:26 +000015637 if (VT == MVT::v2i64 || VT == MVT::v4i64) {
Craig Topperd0a31172012-01-10 06:37:29 +000015638 if (!Subtarget->hasSSSE3() ||
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000015639 (VT == MVT::v4i64 && !Subtarget->hasInt256()))
Craig Topper1666cb62011-11-19 07:07:26 +000015640 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000015641
Craig Topper1666cb62011-11-19 07:07:26 +000015642 // Canonicalize pandn to RHS
15643 if (N0.getOpcode() == X86ISD::ANDNP)
15644 std::swap(N0, N1);
Lang Hames9ffaa6a2012-01-10 22:53:20 +000015645 // or (and (m, y), (pandn m, x))
Craig Topper1666cb62011-11-19 07:07:26 +000015646 if (N0.getOpcode() == ISD::AND && N1.getOpcode() == X86ISD::ANDNP) {
15647 SDValue Mask = N1.getOperand(0);
15648 SDValue X = N1.getOperand(1);
15649 SDValue Y;
15650 if (N0.getOperand(0) == Mask)
15651 Y = N0.getOperand(1);
15652 if (N0.getOperand(1) == Mask)
15653 Y = N0.getOperand(0);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000015654
Craig Topper1666cb62011-11-19 07:07:26 +000015655 // Check to see if the mask appeared in both the AND and ANDNP and
15656 if (!Y.getNode())
15657 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000015658
Craig Topper1666cb62011-11-19 07:07:26 +000015659 // Validate that X, Y, and Mask are BIT_CONVERTS, and see through them.
Craig Topper1666cb62011-11-19 07:07:26 +000015660 // Look through mask bitcast.
Nadav Rotem4ac90812012-04-01 19:31:22 +000015661 if (Mask.getOpcode() == ISD::BITCAST)
15662 Mask = Mask.getOperand(0);
15663 if (X.getOpcode() == ISD::BITCAST)
15664 X = X.getOperand(0);
15665 if (Y.getOpcode() == ISD::BITCAST)
15666 Y = Y.getOperand(0);
15667
Craig Topper1666cb62011-11-19 07:07:26 +000015668 EVT MaskVT = Mask.getValueType();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000015669
Craig Toppered2e13d2012-01-22 19:15:14 +000015670 // Validate that the Mask operand is a vector sra node.
Craig Topper1666cb62011-11-19 07:07:26 +000015671 // FIXME: what to do for bytes, since there is a psignb/pblendvb, but
15672 // there is no psrai.b
Craig Topper7fb8b0c2012-01-23 06:46:22 +000015673 if (Mask.getOpcode() != X86ISD::VSRAI)
Craig Toppered2e13d2012-01-22 19:15:14 +000015674 return SDValue();
Craig Topper1666cb62011-11-19 07:07:26 +000015675
15676 // Check that the SRA is all signbits.
Craig Topper7fb8b0c2012-01-23 06:46:22 +000015677 SDValue SraC = Mask.getOperand(1);
Craig Topper1666cb62011-11-19 07:07:26 +000015678 unsigned SraAmt = cast<ConstantSDNode>(SraC)->getZExtValue();
15679 unsigned EltBits = MaskVT.getVectorElementType().getSizeInBits();
15680 if ((SraAmt + 1) != EltBits)
15681 return SDValue();
15682
15683 DebugLoc DL = N->getDebugLoc();
15684
15685 // Now we know we at least have a plendvb with the mask val. See if
15686 // we can form a psignb/w/d.
15687 // psign = x.type == y.type == mask.type && y = sub(0, x);
Craig Topper1666cb62011-11-19 07:07:26 +000015688 if (Y.getOpcode() == ISD::SUB && Y.getOperand(1) == X &&
15689 ISD::isBuildVectorAllZeros(Y.getOperand(0).getNode()) &&
Craig Toppered2e13d2012-01-22 19:15:14 +000015690 X.getValueType() == MaskVT && Y.getValueType() == MaskVT) {
15691 assert((EltBits == 8 || EltBits == 16 || EltBits == 32) &&
15692 "Unsupported VT for PSIGN");
Craig Topper7fb8b0c2012-01-23 06:46:22 +000015693 Mask = DAG.getNode(X86ISD::PSIGN, DL, MaskVT, X, Mask.getOperand(0));
Craig Toppered2e13d2012-01-22 19:15:14 +000015694 return DAG.getNode(ISD::BITCAST, DL, VT, Mask);
Craig Topper1666cb62011-11-19 07:07:26 +000015695 }
15696 // PBLENDVB only available on SSE 4.1
Craig Topperd0a31172012-01-10 06:37:29 +000015697 if (!Subtarget->hasSSE41())
Craig Topper1666cb62011-11-19 07:07:26 +000015698 return SDValue();
15699
15700 EVT BlendVT = (VT == MVT::v4i64) ? MVT::v32i8 : MVT::v16i8;
15701
15702 X = DAG.getNode(ISD::BITCAST, DL, BlendVT, X);
15703 Y = DAG.getNode(ISD::BITCAST, DL, BlendVT, Y);
15704 Mask = DAG.getNode(ISD::BITCAST, DL, BlendVT, Mask);
Nadav Rotem18197d72011-11-30 10:13:37 +000015705 Mask = DAG.getNode(ISD::VSELECT, DL, BlendVT, Mask, Y, X);
Craig Topper1666cb62011-11-19 07:07:26 +000015706 return DAG.getNode(ISD::BITCAST, DL, VT, Mask);
Nate Begemanb65c1752010-12-17 22:55:37 +000015707 }
15708 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000015709
Craig Topper1666cb62011-11-19 07:07:26 +000015710 if (VT != MVT::i16 && VT != MVT::i32 && VT != MVT::i64)
15711 return SDValue();
15712
Nate Begemanb65c1752010-12-17 22:55:37 +000015713 // fold (or (x << c) | (y >> (64 - c))) ==> (shld64 x, y, c)
Evan Cheng760d1942010-01-04 21:22:48 +000015714 if (N0.getOpcode() == ISD::SRL && N1.getOpcode() == ISD::SHL)
15715 std::swap(N0, N1);
15716 if (N0.getOpcode() != ISD::SHL || N1.getOpcode() != ISD::SRL)
15717 return SDValue();
Evan Cheng8b1190a2010-04-28 01:18:01 +000015718 if (!N0.hasOneUse() || !N1.hasOneUse())
15719 return SDValue();
Evan Cheng760d1942010-01-04 21:22:48 +000015720
15721 SDValue ShAmt0 = N0.getOperand(1);
15722 if (ShAmt0.getValueType() != MVT::i8)
15723 return SDValue();
15724 SDValue ShAmt1 = N1.getOperand(1);
15725 if (ShAmt1.getValueType() != MVT::i8)
15726 return SDValue();
15727 if (ShAmt0.getOpcode() == ISD::TRUNCATE)
15728 ShAmt0 = ShAmt0.getOperand(0);
15729 if (ShAmt1.getOpcode() == ISD::TRUNCATE)
15730 ShAmt1 = ShAmt1.getOperand(0);
15731
15732 DebugLoc DL = N->getDebugLoc();
15733 unsigned Opc = X86ISD::SHLD;
15734 SDValue Op0 = N0.getOperand(0);
15735 SDValue Op1 = N1.getOperand(0);
15736 if (ShAmt0.getOpcode() == ISD::SUB) {
15737 Opc = X86ISD::SHRD;
15738 std::swap(Op0, Op1);
15739 std::swap(ShAmt0, ShAmt1);
15740 }
15741
Evan Cheng8b1190a2010-04-28 01:18:01 +000015742 unsigned Bits = VT.getSizeInBits();
Evan Cheng760d1942010-01-04 21:22:48 +000015743 if (ShAmt1.getOpcode() == ISD::SUB) {
15744 SDValue Sum = ShAmt1.getOperand(0);
15745 if (ConstantSDNode *SumC = dyn_cast<ConstantSDNode>(Sum)) {
Dan Gohman4e39e9d2010-06-24 14:30:44 +000015746 SDValue ShAmt1Op1 = ShAmt1.getOperand(1);
15747 if (ShAmt1Op1.getNode()->getOpcode() == ISD::TRUNCATE)
15748 ShAmt1Op1 = ShAmt1Op1.getOperand(0);
15749 if (SumC->getSExtValue() == Bits && ShAmt1Op1 == ShAmt0)
Evan Cheng760d1942010-01-04 21:22:48 +000015750 return DAG.getNode(Opc, DL, VT,
15751 Op0, Op1,
15752 DAG.getNode(ISD::TRUNCATE, DL,
15753 MVT::i8, ShAmt0));
15754 }
15755 } else if (ConstantSDNode *ShAmt1C = dyn_cast<ConstantSDNode>(ShAmt1)) {
15756 ConstantSDNode *ShAmt0C = dyn_cast<ConstantSDNode>(ShAmt0);
15757 if (ShAmt0C &&
Evan Cheng8b1190a2010-04-28 01:18:01 +000015758 ShAmt0C->getSExtValue() + ShAmt1C->getSExtValue() == Bits)
Evan Cheng760d1942010-01-04 21:22:48 +000015759 return DAG.getNode(Opc, DL, VT,
15760 N0.getOperand(0), N1.getOperand(0),
15761 DAG.getNode(ISD::TRUNCATE, DL,
15762 MVT::i8, ShAmt0));
15763 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000015764
Evan Cheng760d1942010-01-04 21:22:48 +000015765 return SDValue();
15766}
15767
Manman Ren92363622012-06-07 22:39:10 +000015768// Generate NEG and CMOV for integer abs.
15769static SDValue performIntegerAbsCombine(SDNode *N, SelectionDAG &DAG) {
15770 EVT VT = N->getValueType(0);
15771
15772 // Since X86 does not have CMOV for 8-bit integer, we don't convert
15773 // 8-bit integer abs to NEG and CMOV.
15774 if (VT.isInteger() && VT.getSizeInBits() == 8)
15775 return SDValue();
15776
15777 SDValue N0 = N->getOperand(0);
15778 SDValue N1 = N->getOperand(1);
15779 DebugLoc DL = N->getDebugLoc();
15780
15781 // Check pattern of XOR(ADD(X,Y), Y) where Y is SRA(X, size(X)-1)
15782 // and change it to SUB and CMOV.
15783 if (VT.isInteger() && N->getOpcode() == ISD::XOR &&
15784 N0.getOpcode() == ISD::ADD &&
15785 N0.getOperand(1) == N1 &&
15786 N1.getOpcode() == ISD::SRA &&
15787 N1.getOperand(0) == N0.getOperand(0))
15788 if (ConstantSDNode *Y1C = dyn_cast<ConstantSDNode>(N1.getOperand(1)))
15789 if (Y1C->getAPIntValue() == VT.getSizeInBits()-1) {
15790 // Generate SUB & CMOV.
15791 SDValue Neg = DAG.getNode(X86ISD::SUB, DL, DAG.getVTList(VT, MVT::i32),
15792 DAG.getConstant(0, VT), N0.getOperand(0));
15793
15794 SDValue Ops[] = { N0.getOperand(0), Neg,
15795 DAG.getConstant(X86::COND_GE, MVT::i8),
15796 SDValue(Neg.getNode(), 1) };
15797 return DAG.getNode(X86ISD::CMOV, DL, DAG.getVTList(VT, MVT::Glue),
15798 Ops, array_lengthof(Ops));
15799 }
15800 return SDValue();
15801}
15802
Craig Topper3738ccd2011-12-27 06:27:23 +000015803// PerformXorCombine - Attempts to turn XOR nodes into BLSMSK nodes
Craig Topperb4c94572011-10-21 06:55:01 +000015804static SDValue PerformXorCombine(SDNode *N, SelectionDAG &DAG,
15805 TargetLowering::DAGCombinerInfo &DCI,
15806 const X86Subtarget *Subtarget) {
15807 if (DCI.isBeforeLegalizeOps())
15808 return SDValue();
15809
Manman Ren45d53b82012-06-08 18:58:26 +000015810 if (Subtarget->hasCMov()) {
15811 SDValue RV = performIntegerAbsCombine(N, DAG);
15812 if (RV.getNode())
15813 return RV;
15814 }
Manman Ren92363622012-06-07 22:39:10 +000015815
15816 // Try forming BMI if it is available.
15817 if (!Subtarget->hasBMI())
15818 return SDValue();
15819
Craig Topperb4c94572011-10-21 06:55:01 +000015820 EVT VT = N->getValueType(0);
15821
15822 if (VT != MVT::i32 && VT != MVT::i64)
15823 return SDValue();
15824
Craig Topper3738ccd2011-12-27 06:27:23 +000015825 assert(Subtarget->hasBMI() && "Creating BLSMSK requires BMI instructions");
15826
Craig Topperb4c94572011-10-21 06:55:01 +000015827 // Create BLSMSK instructions by finding X ^ (X-1)
15828 SDValue N0 = N->getOperand(0);
15829 SDValue N1 = N->getOperand(1);
15830 DebugLoc DL = N->getDebugLoc();
15831
15832 if (N0.getOpcode() == ISD::ADD && N0.getOperand(0) == N1 &&
15833 isAllOnes(N0.getOperand(1)))
15834 return DAG.getNode(X86ISD::BLSMSK, DL, VT, N1);
15835
15836 if (N1.getOpcode() == ISD::ADD && N1.getOperand(0) == N0 &&
15837 isAllOnes(N1.getOperand(1)))
15838 return DAG.getNode(X86ISD::BLSMSK, DL, VT, N0);
15839
15840 return SDValue();
15841}
15842
Nadav Rotem91e43fd2011-09-18 10:39:32 +000015843/// PerformLOADCombine - Do target-specific dag combines on LOAD nodes.
15844static SDValue PerformLOADCombine(SDNode *N, SelectionDAG &DAG,
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000015845 TargetLowering::DAGCombinerInfo &DCI,
15846 const X86Subtarget *Subtarget) {
Nadav Rotem91e43fd2011-09-18 10:39:32 +000015847 LoadSDNode *Ld = cast<LoadSDNode>(N);
15848 EVT RegVT = Ld->getValueType(0);
15849 EVT MemVT = Ld->getMemoryVT();
15850 DebugLoc dl = Ld->getDebugLoc();
15851 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
15852
15853 ISD::LoadExtType Ext = Ld->getExtensionType();
15854
Nadav Rotemca6f2962011-09-18 19:00:23 +000015855 // If this is a vector EXT Load then attempt to optimize it using a
Michael Liao35a56402012-10-17 03:59:18 +000015856 // shuffle. We need SSSE3 shuffles.
Nadav Rotem91e43fd2011-09-18 10:39:32 +000015857 // TODO: It is possible to support ZExt by zeroing the undef values
15858 // during the shuffle phase or after the shuffle.
Eli Friedmanda813f42011-12-28 21:24:44 +000015859 if (RegVT.isVector() && RegVT.isInteger() &&
Michael Liao35a56402012-10-17 03:59:18 +000015860 Ext == ISD::EXTLOAD && Subtarget->hasSSSE3()) {
Nadav Rotem91e43fd2011-09-18 10:39:32 +000015861 assert(MemVT != RegVT && "Cannot extend to the same type");
15862 assert(MemVT.isVector() && "Must load a vector from memory");
15863
15864 unsigned NumElems = RegVT.getVectorNumElements();
15865 unsigned RegSz = RegVT.getSizeInBits();
15866 unsigned MemSz = MemVT.getSizeInBits();
15867 assert(RegSz > MemSz && "Register size must be greater than the mem size");
Nadav Rotem91e43fd2011-09-18 10:39:32 +000015868
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000015869 // All sizes must be a power of two.
15870 if (!isPowerOf2_32(RegSz * MemSz * NumElems))
15871 return SDValue();
15872
15873 // Attempt to load the original value using scalar loads.
15874 // Find the largest scalar type that divides the total loaded size.
Nadav Rotem91e43fd2011-09-18 10:39:32 +000015875 MVT SclrLoadTy = MVT::i8;
15876 for (unsigned tp = MVT::FIRST_INTEGER_VALUETYPE;
15877 tp < MVT::LAST_INTEGER_VALUETYPE; ++tp) {
15878 MVT Tp = (MVT::SimpleValueType)tp;
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000015879 if (TLI.isTypeLegal(Tp) && ((MemSz % Tp.getSizeInBits()) == 0)) {
Nadav Rotem91e43fd2011-09-18 10:39:32 +000015880 SclrLoadTy = Tp;
Nadav Rotem91e43fd2011-09-18 10:39:32 +000015881 }
15882 }
15883
Nadav Rotem5cd95e12012-07-11 13:27:05 +000015884 // On 32bit systems, we can't save 64bit integers. Try bitcasting to F64.
15885 if (TLI.isTypeLegal(MVT::f64) && SclrLoadTy.getSizeInBits() < 64 &&
15886 (64 <= MemSz))
15887 SclrLoadTy = MVT::f64;
15888
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000015889 // Calculate the number of scalar loads that we need to perform
15890 // in order to load our vector from memory.
15891 unsigned NumLoads = MemSz / SclrLoadTy.getSizeInBits();
Nadav Rotem91e43fd2011-09-18 10:39:32 +000015892
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000015893 // Represent our vector as a sequence of elements which are the
15894 // largest scalar that we can load.
Nadav Rotem91e43fd2011-09-18 10:39:32 +000015895 EVT LoadUnitVecVT = EVT::getVectorVT(*DAG.getContext(), SclrLoadTy,
15896 RegSz/SclrLoadTy.getSizeInBits());
15897
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000015898 // Represent the data using the same element type that is stored in
15899 // memory. In practice, we ''widen'' MemVT.
Nadav Rotem91e43fd2011-09-18 10:39:32 +000015900 EVT WideVecVT = EVT::getVectorVT(*DAG.getContext(), MemVT.getScalarType(),
15901 RegSz/MemVT.getScalarType().getSizeInBits());
Nadav Rotem91e43fd2011-09-18 10:39:32 +000015902
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000015903 assert(WideVecVT.getSizeInBits() == LoadUnitVecVT.getSizeInBits() &&
15904 "Invalid vector type");
Nadav Rotem91e43fd2011-09-18 10:39:32 +000015905
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000015906 // We can't shuffle using an illegal type.
15907 if (!TLI.isTypeLegal(WideVecVT))
15908 return SDValue();
15909
15910 SmallVector<SDValue, 8> Chains;
15911 SDValue Ptr = Ld->getBasePtr();
15912 SDValue Increment = DAG.getConstant(SclrLoadTy.getSizeInBits()/8,
15913 TLI.getPointerTy());
15914 SDValue Res = DAG.getUNDEF(LoadUnitVecVT);
15915
15916 for (unsigned i = 0; i < NumLoads; ++i) {
15917 // Perform a single load.
15918 SDValue ScalarLoad = DAG.getLoad(SclrLoadTy, dl, Ld->getChain(),
15919 Ptr, Ld->getPointerInfo(),
15920 Ld->isVolatile(), Ld->isNonTemporal(),
15921 Ld->isInvariant(), Ld->getAlignment());
15922 Chains.push_back(ScalarLoad.getValue(1));
15923 // Create the first element type using SCALAR_TO_VECTOR in order to avoid
15924 // another round of DAGCombining.
15925 if (i == 0)
15926 Res = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, LoadUnitVecVT, ScalarLoad);
15927 else
15928 Res = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, LoadUnitVecVT, Res,
15929 ScalarLoad, DAG.getIntPtrConstant(i));
15930
15931 Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr, Increment);
15932 }
15933
15934 SDValue TF = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &Chains[0],
15935 Chains.size());
Nadav Rotem91e43fd2011-09-18 10:39:32 +000015936
15937 // Bitcast the loaded value to a vector of the original element type, in
15938 // the size of the target vector type.
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000015939 SDValue SlicedVec = DAG.getNode(ISD::BITCAST, dl, WideVecVT, Res);
Nadav Rotem91e43fd2011-09-18 10:39:32 +000015940 unsigned SizeRatio = RegSz/MemSz;
15941
15942 // Redistribute the loaded elements into the different locations.
15943 SmallVector<int, 8> ShuffleVec(NumElems * SizeRatio, -1);
Craig Topper31a207a2012-05-04 06:39:13 +000015944 for (unsigned i = 0; i != NumElems; ++i)
15945 ShuffleVec[i*SizeRatio] = i;
Nadav Rotem91e43fd2011-09-18 10:39:32 +000015946
15947 SDValue Shuff = DAG.getVectorShuffle(WideVecVT, dl, SlicedVec,
Craig Topperdf966f62012-04-22 19:17:57 +000015948 DAG.getUNDEF(WideVecVT),
15949 &ShuffleVec[0]);
Nadav Rotem91e43fd2011-09-18 10:39:32 +000015950
15951 // Bitcast to the requested type.
15952 Shuff = DAG.getNode(ISD::BITCAST, dl, RegVT, Shuff);
15953 // Replace the original load with the new sequence
15954 // and return the new chain.
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000015955 return DCI.CombineTo(N, Shuff, TF, true);
Nadav Rotem91e43fd2011-09-18 10:39:32 +000015956 }
15957
15958 return SDValue();
15959}
15960
Chris Lattner149a4e52008-02-22 02:09:43 +000015961/// PerformSTORECombine - Do target-specific dag combines on STORE nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000015962static SDValue PerformSTORECombine(SDNode *N, SelectionDAG &DAG,
Evan Cheng536e6672009-03-12 05:59:15 +000015963 const X86Subtarget *Subtarget) {
Nadav Rotem614061b2011-08-10 19:30:14 +000015964 StoreSDNode *St = cast<StoreSDNode>(N);
15965 EVT VT = St->getValue().getValueType();
15966 EVT StVT = St->getMemoryVT();
15967 DebugLoc dl = St->getDebugLoc();
Nadav Rotem5e742a32011-08-11 16:41:21 +000015968 SDValue StoredVal = St->getOperand(1);
15969 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
15970
Nick Lewycky8a8d4792011-12-02 22:16:29 +000015971 // If we are saving a concatenation of two XMM registers, perform two stores.
Nadav Rotem87d35e82012-05-19 20:30:08 +000015972 // On Sandy Bridge, 256-bit memory operations are executed by two
15973 // 128-bit ports. However, on Haswell it is better to issue a single 256-bit
15974 // memory operation.
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000015975 if (VT.is256BitVector() && !Subtarget->hasInt256() &&
Craig Topperb4a8aef2012-04-27 21:05:09 +000015976 StoredVal.getNode()->getOpcode() == ISD::CONCAT_VECTORS &&
15977 StoredVal.getNumOperands() == 2) {
Nadav Rotem5e742a32011-08-11 16:41:21 +000015978 SDValue Value0 = StoredVal.getOperand(0);
15979 SDValue Value1 = StoredVal.getOperand(1);
15980
15981 SDValue Stride = DAG.getConstant(16, TLI.getPointerTy());
15982 SDValue Ptr0 = St->getBasePtr();
15983 SDValue Ptr1 = DAG.getNode(ISD::ADD, dl, Ptr0.getValueType(), Ptr0, Stride);
15984
15985 SDValue Ch0 = DAG.getStore(St->getChain(), dl, Value0, Ptr0,
15986 St->getPointerInfo(), St->isVolatile(),
15987 St->isNonTemporal(), St->getAlignment());
15988 SDValue Ch1 = DAG.getStore(St->getChain(), dl, Value1, Ptr1,
15989 St->getPointerInfo(), St->isVolatile(),
15990 St->isNonTemporal(), St->getAlignment());
15991 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Ch0, Ch1);
15992 }
Nadav Rotem614061b2011-08-10 19:30:14 +000015993
15994 // Optimize trunc store (of multiple scalars) to shuffle and store.
15995 // First, pack all of the elements in one place. Next, store to memory
15996 // in fewer chunks.
15997 if (St->isTruncatingStore() && VT.isVector()) {
15998 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
15999 unsigned NumElems = VT.getVectorNumElements();
16000 assert(StVT != VT && "Cannot truncate to the same type");
16001 unsigned FromSz = VT.getVectorElementType().getSizeInBits();
16002 unsigned ToSz = StVT.getVectorElementType().getSizeInBits();
16003
16004 // From, To sizes and ElemCount must be pow of two
16005 if (!isPowerOf2_32(NumElems * FromSz * ToSz)) return SDValue();
Nadav Rotem9c6cdf42011-09-21 08:45:10 +000016006 // We are going to use the original vector elt for storing.
Nadav Rotem64ac73b2011-09-21 17:14:40 +000016007 // Accumulated smaller vector elements must be a multiple of the store size.
Nadav Rotem9c6cdf42011-09-21 08:45:10 +000016008 if (0 != (NumElems * FromSz) % ToSz) return SDValue();
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016009
Nadav Rotem614061b2011-08-10 19:30:14 +000016010 unsigned SizeRatio = FromSz / ToSz;
16011
16012 assert(SizeRatio * NumElems * ToSz == VT.getSizeInBits());
16013
16014 // Create a type on which we perform the shuffle
16015 EVT WideVecVT = EVT::getVectorVT(*DAG.getContext(),
16016 StVT.getScalarType(), NumElems*SizeRatio);
16017
16018 assert(WideVecVT.getSizeInBits() == VT.getSizeInBits());
16019
16020 SDValue WideVec = DAG.getNode(ISD::BITCAST, dl, WideVecVT, St->getValue());
16021 SmallVector<int, 8> ShuffleVec(NumElems * SizeRatio, -1);
Craig Topper31a207a2012-05-04 06:39:13 +000016022 for (unsigned i = 0; i != NumElems; ++i)
16023 ShuffleVec[i] = i * SizeRatio;
Nadav Rotem614061b2011-08-10 19:30:14 +000016024
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000016025 // Can't shuffle using an illegal type.
16026 if (!TLI.isTypeLegal(WideVecVT))
16027 return SDValue();
Nadav Rotem614061b2011-08-10 19:30:14 +000016028
16029 SDValue Shuff = DAG.getVectorShuffle(WideVecVT, dl, WideVec,
Craig Topperdf966f62012-04-22 19:17:57 +000016030 DAG.getUNDEF(WideVecVT),
16031 &ShuffleVec[0]);
Nadav Rotem614061b2011-08-10 19:30:14 +000016032 // At this point all of the data is stored at the bottom of the
16033 // register. We now need to save it to mem.
16034
16035 // Find the largest store unit
16036 MVT StoreType = MVT::i8;
16037 for (unsigned tp = MVT::FIRST_INTEGER_VALUETYPE;
16038 tp < MVT::LAST_INTEGER_VALUETYPE; ++tp) {
16039 MVT Tp = (MVT::SimpleValueType)tp;
Nadav Rotem5cd95e12012-07-11 13:27:05 +000016040 if (TLI.isTypeLegal(Tp) && Tp.getSizeInBits() <= NumElems * ToSz)
Nadav Rotem614061b2011-08-10 19:30:14 +000016041 StoreType = Tp;
16042 }
16043
Nadav Rotem5cd95e12012-07-11 13:27:05 +000016044 // On 32bit systems, we can't save 64bit integers. Try bitcasting to F64.
16045 if (TLI.isTypeLegal(MVT::f64) && StoreType.getSizeInBits() < 64 &&
16046 (64 <= NumElems * ToSz))
16047 StoreType = MVT::f64;
16048
Nadav Rotem614061b2011-08-10 19:30:14 +000016049 // Bitcast the original vector into a vector of store-size units
16050 EVT StoreVecVT = EVT::getVectorVT(*DAG.getContext(),
Nadav Rotem5cd95e12012-07-11 13:27:05 +000016051 StoreType, VT.getSizeInBits()/StoreType.getSizeInBits());
Nadav Rotem614061b2011-08-10 19:30:14 +000016052 assert(StoreVecVT.getSizeInBits() == VT.getSizeInBits());
16053 SDValue ShuffWide = DAG.getNode(ISD::BITCAST, dl, StoreVecVT, Shuff);
16054 SmallVector<SDValue, 8> Chains;
16055 SDValue Increment = DAG.getConstant(StoreType.getSizeInBits()/8,
16056 TLI.getPointerTy());
16057 SDValue Ptr = St->getBasePtr();
16058
16059 // Perform one or more big stores into memory.
Craig Topper31a207a2012-05-04 06:39:13 +000016060 for (unsigned i=0, e=(ToSz*NumElems)/StoreType.getSizeInBits(); i!=e; ++i) {
Nadav Rotem614061b2011-08-10 19:30:14 +000016061 SDValue SubVec = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl,
16062 StoreType, ShuffWide,
16063 DAG.getIntPtrConstant(i));
16064 SDValue Ch = DAG.getStore(St->getChain(), dl, SubVec, Ptr,
16065 St->getPointerInfo(), St->isVolatile(),
16066 St->isNonTemporal(), St->getAlignment());
16067 Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr, Increment);
16068 Chains.push_back(Ch);
16069 }
16070
16071 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &Chains[0],
16072 Chains.size());
16073 }
16074
16075
Chris Lattner149a4e52008-02-22 02:09:43 +000016076 // Turn load->store of MMX types into GPR load/stores. This avoids clobbering
16077 // the FP state in cases where an emms may be missing.
Dale Johannesen079f2a62008-02-25 19:20:14 +000016078 // A preferable solution to the general problem is to figure out the right
16079 // places to insert EMMS. This qualifies as a quick hack.
Evan Cheng536e6672009-03-12 05:59:15 +000016080
16081 // Similarly, turn load->store of i64 into double load/stores in 32-bit mode.
Evan Cheng536e6672009-03-12 05:59:15 +000016082 if (VT.getSizeInBits() != 64)
16083 return SDValue();
16084
Devang Patel578efa92009-06-05 21:57:13 +000016085 const Function *F = DAG.getMachineFunction().getFunction();
Bill Wendling67658342012-10-09 07:45:08 +000016086 bool NoImplicitFloatOps = F->getFnAttributes().
16087 hasAttribute(Attributes::NoImplicitFloat);
Nick Lewycky8a8d4792011-12-02 22:16:29 +000016088 bool F64IsLegal = !DAG.getTarget().Options.UseSoftFloat && !NoImplicitFloatOps
Craig Topper1accb7e2012-01-10 06:54:16 +000016089 && Subtarget->hasSSE2();
Evan Cheng536e6672009-03-12 05:59:15 +000016090 if ((VT.isVector() ||
Owen Anderson825b72b2009-08-11 20:47:22 +000016091 (VT == MVT::i64 && F64IsLegal && !Subtarget->is64Bit())) &&
Dale Johannesen079f2a62008-02-25 19:20:14 +000016092 isa<LoadSDNode>(St->getValue()) &&
16093 !cast<LoadSDNode>(St->getValue())->isVolatile() &&
16094 St->getChain().hasOneUse() && !St->isVolatile()) {
Gabor Greifba36cb52008-08-28 21:40:38 +000016095 SDNode* LdVal = St->getValue().getNode();
Dale Johannesen079f2a62008-02-25 19:20:14 +000016096 LoadSDNode *Ld = 0;
16097 int TokenFactorIndex = -1;
Dan Gohman475871a2008-07-27 21:46:04 +000016098 SmallVector<SDValue, 8> Ops;
Gabor Greifba36cb52008-08-28 21:40:38 +000016099 SDNode* ChainVal = St->getChain().getNode();
Dale Johannesen079f2a62008-02-25 19:20:14 +000016100 // Must be a store of a load. We currently handle two cases: the load
16101 // is a direct child, and it's under an intervening TokenFactor. It is
16102 // possible to dig deeper under nested TokenFactors.
Dale Johannesen14e2ea92008-02-25 22:29:22 +000016103 if (ChainVal == LdVal)
Dale Johannesen079f2a62008-02-25 19:20:14 +000016104 Ld = cast<LoadSDNode>(St->getChain());
16105 else if (St->getValue().hasOneUse() &&
16106 ChainVal->getOpcode() == ISD::TokenFactor) {
Chad Rosierc2348d52012-02-01 18:45:51 +000016107 for (unsigned i = 0, e = ChainVal->getNumOperands(); i != e; ++i) {
Gabor Greifba36cb52008-08-28 21:40:38 +000016108 if (ChainVal->getOperand(i).getNode() == LdVal) {
Dale Johannesen079f2a62008-02-25 19:20:14 +000016109 TokenFactorIndex = i;
16110 Ld = cast<LoadSDNode>(St->getValue());
16111 } else
16112 Ops.push_back(ChainVal->getOperand(i));
16113 }
16114 }
Dale Johannesen079f2a62008-02-25 19:20:14 +000016115
Evan Cheng536e6672009-03-12 05:59:15 +000016116 if (!Ld || !ISD::isNormalLoad(Ld))
16117 return SDValue();
Dale Johannesen079f2a62008-02-25 19:20:14 +000016118
Evan Cheng536e6672009-03-12 05:59:15 +000016119 // If this is not the MMX case, i.e. we are just turning i64 load/store
16120 // into f64 load/store, avoid the transformation if there are multiple
16121 // uses of the loaded value.
16122 if (!VT.isVector() && !Ld->hasNUsesOfValue(1, 0))
16123 return SDValue();
Dale Johannesen079f2a62008-02-25 19:20:14 +000016124
Evan Cheng536e6672009-03-12 05:59:15 +000016125 DebugLoc LdDL = Ld->getDebugLoc();
16126 DebugLoc StDL = N->getDebugLoc();
16127 // If we are a 64-bit capable x86, lower to a single movq load/store pair.
16128 // Otherwise, if it's legal to use f64 SSE instructions, use f64 load/store
16129 // pair instead.
16130 if (Subtarget->is64Bit() || F64IsLegal) {
Owen Anderson825b72b2009-08-11 20:47:22 +000016131 EVT LdVT = Subtarget->is64Bit() ? MVT::i64 : MVT::f64;
Chris Lattner51abfe42010-09-21 06:02:19 +000016132 SDValue NewLd = DAG.getLoad(LdVT, LdDL, Ld->getChain(), Ld->getBasePtr(),
16133 Ld->getPointerInfo(), Ld->isVolatile(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000016134 Ld->isNonTemporal(), Ld->isInvariant(),
16135 Ld->getAlignment());
Evan Cheng536e6672009-03-12 05:59:15 +000016136 SDValue NewChain = NewLd.getValue(1);
Dale Johannesen079f2a62008-02-25 19:20:14 +000016137 if (TokenFactorIndex != -1) {
Evan Cheng536e6672009-03-12 05:59:15 +000016138 Ops.push_back(NewChain);
Owen Anderson825b72b2009-08-11 20:47:22 +000016139 NewChain = DAG.getNode(ISD::TokenFactor, LdDL, MVT::Other, &Ops[0],
Dale Johannesen079f2a62008-02-25 19:20:14 +000016140 Ops.size());
16141 }
Evan Cheng536e6672009-03-12 05:59:15 +000016142 return DAG.getStore(NewChain, StDL, NewLd, St->getBasePtr(),
Chris Lattner51abfe42010-09-21 06:02:19 +000016143 St->getPointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +000016144 St->isVolatile(), St->isNonTemporal(),
16145 St->getAlignment());
Chris Lattner149a4e52008-02-22 02:09:43 +000016146 }
Evan Cheng536e6672009-03-12 05:59:15 +000016147
16148 // Otherwise, lower to two pairs of 32-bit loads / stores.
16149 SDValue LoAddr = Ld->getBasePtr();
Owen Anderson825b72b2009-08-11 20:47:22 +000016150 SDValue HiAddr = DAG.getNode(ISD::ADD, LdDL, MVT::i32, LoAddr,
16151 DAG.getConstant(4, MVT::i32));
Evan Cheng536e6672009-03-12 05:59:15 +000016152
Owen Anderson825b72b2009-08-11 20:47:22 +000016153 SDValue LoLd = DAG.getLoad(MVT::i32, LdDL, Ld->getChain(), LoAddr,
Chris Lattner51abfe42010-09-21 06:02:19 +000016154 Ld->getPointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +000016155 Ld->isVolatile(), Ld->isNonTemporal(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000016156 Ld->isInvariant(), Ld->getAlignment());
Owen Anderson825b72b2009-08-11 20:47:22 +000016157 SDValue HiLd = DAG.getLoad(MVT::i32, LdDL, Ld->getChain(), HiAddr,
Chris Lattner51abfe42010-09-21 06:02:19 +000016158 Ld->getPointerInfo().getWithOffset(4),
David Greene67c9d422010-02-15 16:53:33 +000016159 Ld->isVolatile(), Ld->isNonTemporal(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000016160 Ld->isInvariant(),
Evan Cheng536e6672009-03-12 05:59:15 +000016161 MinAlign(Ld->getAlignment(), 4));
16162
16163 SDValue NewChain = LoLd.getValue(1);
16164 if (TokenFactorIndex != -1) {
16165 Ops.push_back(LoLd);
16166 Ops.push_back(HiLd);
Owen Anderson825b72b2009-08-11 20:47:22 +000016167 NewChain = DAG.getNode(ISD::TokenFactor, LdDL, MVT::Other, &Ops[0],
Evan Cheng536e6672009-03-12 05:59:15 +000016168 Ops.size());
16169 }
16170
16171 LoAddr = St->getBasePtr();
Owen Anderson825b72b2009-08-11 20:47:22 +000016172 HiAddr = DAG.getNode(ISD::ADD, StDL, MVT::i32, LoAddr,
16173 DAG.getConstant(4, MVT::i32));
Evan Cheng536e6672009-03-12 05:59:15 +000016174
16175 SDValue LoSt = DAG.getStore(NewChain, StDL, LoLd, LoAddr,
Chris Lattner8026a9d2010-09-21 17:50:43 +000016176 St->getPointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +000016177 St->isVolatile(), St->isNonTemporal(),
16178 St->getAlignment());
Evan Cheng536e6672009-03-12 05:59:15 +000016179 SDValue HiSt = DAG.getStore(NewChain, StDL, HiLd, HiAddr,
Chris Lattner8026a9d2010-09-21 17:50:43 +000016180 St->getPointerInfo().getWithOffset(4),
Evan Cheng536e6672009-03-12 05:59:15 +000016181 St->isVolatile(),
David Greene67c9d422010-02-15 16:53:33 +000016182 St->isNonTemporal(),
Evan Cheng536e6672009-03-12 05:59:15 +000016183 MinAlign(St->getAlignment(), 4));
Owen Anderson825b72b2009-08-11 20:47:22 +000016184 return DAG.getNode(ISD::TokenFactor, StDL, MVT::Other, LoSt, HiSt);
Chris Lattner149a4e52008-02-22 02:09:43 +000016185 }
Dan Gohman475871a2008-07-27 21:46:04 +000016186 return SDValue();
Chris Lattner149a4e52008-02-22 02:09:43 +000016187}
16188
Duncan Sands17470be2011-09-22 20:15:48 +000016189/// isHorizontalBinOp - Return 'true' if this vector operation is "horizontal"
16190/// and return the operands for the horizontal operation in LHS and RHS. A
16191/// horizontal operation performs the binary operation on successive elements
16192/// of its first operand, then on successive elements of its second operand,
16193/// returning the resulting values in a vector. For example, if
16194/// A = < float a0, float a1, float a2, float a3 >
16195/// and
16196/// B = < float b0, float b1, float b2, float b3 >
16197/// then the result of doing a horizontal operation on A and B is
16198/// A horizontal-op B = < a0 op a1, a2 op a3, b0 op b1, b2 op b3 >.
16199/// In short, LHS and RHS are inspected to see if LHS op RHS is of the form
16200/// A horizontal-op B, for some already available A and B, and if so then LHS is
16201/// set to A, RHS to B, and the routine returns 'true'.
16202/// Note that the binary operation should have the property that if one of the
16203/// operands is UNDEF then the result is UNDEF.
Craig Topperbeabc6c2011-12-05 06:56:46 +000016204static bool isHorizontalBinOp(SDValue &LHS, SDValue &RHS, bool IsCommutative) {
Duncan Sands17470be2011-09-22 20:15:48 +000016205 // Look for the following pattern: if
16206 // A = < float a0, float a1, float a2, float a3 >
16207 // B = < float b0, float b1, float b2, float b3 >
16208 // and
16209 // LHS = VECTOR_SHUFFLE A, B, <0, 2, 4, 6>
16210 // RHS = VECTOR_SHUFFLE A, B, <1, 3, 5, 7>
16211 // then LHS op RHS = < a0 op a1, a2 op a3, b0 op b1, b2 op b3 >
16212 // which is A horizontal-op B.
16213
16214 // At least one of the operands should be a vector shuffle.
16215 if (LHS.getOpcode() != ISD::VECTOR_SHUFFLE &&
16216 RHS.getOpcode() != ISD::VECTOR_SHUFFLE)
16217 return false;
16218
16219 EVT VT = LHS.getValueType();
Craig Topperf8363302011-12-02 08:18:41 +000016220
16221 assert((VT.is128BitVector() || VT.is256BitVector()) &&
16222 "Unsupported vector type for horizontal add/sub");
16223
16224 // Handle 128 and 256-bit vector lengths. AVX defines horizontal add/sub to
16225 // operate independently on 128-bit lanes.
Craig Topperb72039c2011-11-30 09:10:50 +000016226 unsigned NumElts = VT.getVectorNumElements();
16227 unsigned NumLanes = VT.getSizeInBits()/128;
16228 unsigned NumLaneElts = NumElts / NumLanes;
Craig Topperf8363302011-12-02 08:18:41 +000016229 assert((NumLaneElts % 2 == 0) &&
16230 "Vector type should have an even number of elements in each lane");
16231 unsigned HalfLaneElts = NumLaneElts/2;
Duncan Sands17470be2011-09-22 20:15:48 +000016232
16233 // View LHS in the form
16234 // LHS = VECTOR_SHUFFLE A, B, LMask
16235 // If LHS is not a shuffle then pretend it is the shuffle
16236 // LHS = VECTOR_SHUFFLE LHS, undef, <0, 1, ..., N-1>
16237 // NOTE: in what follows a default initialized SDValue represents an UNDEF of
16238 // type VT.
16239 SDValue A, B;
Craig Topperb72039c2011-11-30 09:10:50 +000016240 SmallVector<int, 16> LMask(NumElts);
Duncan Sands17470be2011-09-22 20:15:48 +000016241 if (LHS.getOpcode() == ISD::VECTOR_SHUFFLE) {
16242 if (LHS.getOperand(0).getOpcode() != ISD::UNDEF)
16243 A = LHS.getOperand(0);
16244 if (LHS.getOperand(1).getOpcode() != ISD::UNDEF)
16245 B = LHS.getOperand(1);
Benjamin Kramered4c8c62012-01-15 13:16:05 +000016246 ArrayRef<int> Mask = cast<ShuffleVectorSDNode>(LHS.getNode())->getMask();
16247 std::copy(Mask.begin(), Mask.end(), LMask.begin());
Duncan Sands17470be2011-09-22 20:15:48 +000016248 } else {
16249 if (LHS.getOpcode() != ISD::UNDEF)
16250 A = LHS;
Craig Topperb72039c2011-11-30 09:10:50 +000016251 for (unsigned i = 0; i != NumElts; ++i)
Duncan Sands17470be2011-09-22 20:15:48 +000016252 LMask[i] = i;
16253 }
16254
16255 // Likewise, view RHS in the form
16256 // RHS = VECTOR_SHUFFLE C, D, RMask
16257 SDValue C, D;
Craig Topperb72039c2011-11-30 09:10:50 +000016258 SmallVector<int, 16> RMask(NumElts);
Duncan Sands17470be2011-09-22 20:15:48 +000016259 if (RHS.getOpcode() == ISD::VECTOR_SHUFFLE) {
16260 if (RHS.getOperand(0).getOpcode() != ISD::UNDEF)
16261 C = RHS.getOperand(0);
16262 if (RHS.getOperand(1).getOpcode() != ISD::UNDEF)
16263 D = RHS.getOperand(1);
Benjamin Kramered4c8c62012-01-15 13:16:05 +000016264 ArrayRef<int> Mask = cast<ShuffleVectorSDNode>(RHS.getNode())->getMask();
16265 std::copy(Mask.begin(), Mask.end(), RMask.begin());
Duncan Sands17470be2011-09-22 20:15:48 +000016266 } else {
16267 if (RHS.getOpcode() != ISD::UNDEF)
16268 C = RHS;
Craig Topperb72039c2011-11-30 09:10:50 +000016269 for (unsigned i = 0; i != NumElts; ++i)
Duncan Sands17470be2011-09-22 20:15:48 +000016270 RMask[i] = i;
16271 }
16272
16273 // Check that the shuffles are both shuffling the same vectors.
16274 if (!(A == C && B == D) && !(A == D && B == C))
16275 return false;
16276
16277 // If everything is UNDEF then bail out: it would be better to fold to UNDEF.
16278 if (!A.getNode() && !B.getNode())
16279 return false;
16280
16281 // If A and B occur in reverse order in RHS, then "swap" them (which means
16282 // rewriting the mask).
16283 if (A != C)
Craig Topperbeabc6c2011-12-05 06:56:46 +000016284 CommuteVectorShuffleMask(RMask, NumElts);
Duncan Sands17470be2011-09-22 20:15:48 +000016285
16286 // At this point LHS and RHS are equivalent to
16287 // LHS = VECTOR_SHUFFLE A, B, LMask
16288 // RHS = VECTOR_SHUFFLE A, B, RMask
16289 // Check that the masks correspond to performing a horizontal operation.
Craig Topperf8363302011-12-02 08:18:41 +000016290 for (unsigned i = 0; i != NumElts; ++i) {
Craig Topperbeabc6c2011-12-05 06:56:46 +000016291 int LIdx = LMask[i], RIdx = RMask[i];
Duncan Sands17470be2011-09-22 20:15:48 +000016292
Craig Topperf8363302011-12-02 08:18:41 +000016293 // Ignore any UNDEF components.
Craig Topperbeabc6c2011-12-05 06:56:46 +000016294 if (LIdx < 0 || RIdx < 0 ||
16295 (!A.getNode() && (LIdx < (int)NumElts || RIdx < (int)NumElts)) ||
16296 (!B.getNode() && (LIdx >= (int)NumElts || RIdx >= (int)NumElts)))
Craig Topperf8363302011-12-02 08:18:41 +000016297 continue;
Duncan Sands17470be2011-09-22 20:15:48 +000016298
Craig Topperf8363302011-12-02 08:18:41 +000016299 // Check that successive elements are being operated on. If not, this is
16300 // not a horizontal operation.
16301 unsigned Src = (i/HalfLaneElts) % 2; // each lane is split between srcs
16302 unsigned LaneStart = (i/NumLaneElts) * NumLaneElts;
Craig Topperbeabc6c2011-12-05 06:56:46 +000016303 int Index = 2*(i%HalfLaneElts) + NumElts*Src + LaneStart;
Craig Topperf8363302011-12-02 08:18:41 +000016304 if (!(LIdx == Index && RIdx == Index + 1) &&
Craig Topperbeabc6c2011-12-05 06:56:46 +000016305 !(IsCommutative && LIdx == Index + 1 && RIdx == Index))
Craig Topperf8363302011-12-02 08:18:41 +000016306 return false;
Duncan Sands17470be2011-09-22 20:15:48 +000016307 }
16308
16309 LHS = A.getNode() ? A : B; // If A is 'UNDEF', use B for it.
16310 RHS = B.getNode() ? B : A; // If B is 'UNDEF', use A for it.
16311 return true;
16312}
16313
16314/// PerformFADDCombine - Do target-specific dag combines on floating point adds.
16315static SDValue PerformFADDCombine(SDNode *N, SelectionDAG &DAG,
16316 const X86Subtarget *Subtarget) {
16317 EVT VT = N->getValueType(0);
16318 SDValue LHS = N->getOperand(0);
16319 SDValue RHS = N->getOperand(1);
16320
16321 // Try to synthesize horizontal adds from adds of shuffles.
Craig Topperd0a31172012-01-10 06:37:29 +000016322 if (((Subtarget->hasSSE3() && (VT == MVT::v4f32 || VT == MVT::v2f64)) ||
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000016323 (Subtarget->hasFp256() && (VT == MVT::v8f32 || VT == MVT::v4f64))) &&
Duncan Sands17470be2011-09-22 20:15:48 +000016324 isHorizontalBinOp(LHS, RHS, true))
16325 return DAG.getNode(X86ISD::FHADD, N->getDebugLoc(), VT, LHS, RHS);
16326 return SDValue();
16327}
16328
16329/// PerformFSUBCombine - Do target-specific dag combines on floating point subs.
16330static SDValue PerformFSUBCombine(SDNode *N, SelectionDAG &DAG,
16331 const X86Subtarget *Subtarget) {
16332 EVT VT = N->getValueType(0);
16333 SDValue LHS = N->getOperand(0);
16334 SDValue RHS = N->getOperand(1);
16335
16336 // Try to synthesize horizontal subs from subs of shuffles.
Craig Topperd0a31172012-01-10 06:37:29 +000016337 if (((Subtarget->hasSSE3() && (VT == MVT::v4f32 || VT == MVT::v2f64)) ||
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000016338 (Subtarget->hasFp256() && (VT == MVT::v8f32 || VT == MVT::v4f64))) &&
Duncan Sands17470be2011-09-22 20:15:48 +000016339 isHorizontalBinOp(LHS, RHS, false))
16340 return DAG.getNode(X86ISD::FHSUB, N->getDebugLoc(), VT, LHS, RHS);
16341 return SDValue();
16342}
16343
Chris Lattner6cf73262008-01-25 06:14:17 +000016344/// PerformFORCombine - Do target-specific dag combines on X86ISD::FOR and
16345/// X86ISD::FXOR nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000016346static SDValue PerformFORCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattner6cf73262008-01-25 06:14:17 +000016347 assert(N->getOpcode() == X86ISD::FOR || N->getOpcode() == X86ISD::FXOR);
16348 // F[X]OR(0.0, x) -> x
16349 // F[X]OR(x, 0.0) -> x
Chris Lattneraf723b92008-01-25 05:46:26 +000016350 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
16351 if (C->getValueAPF().isPosZero())
16352 return N->getOperand(1);
16353 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
16354 if (C->getValueAPF().isPosZero())
16355 return N->getOperand(0);
Dan Gohman475871a2008-07-27 21:46:04 +000016356 return SDValue();
Chris Lattneraf723b92008-01-25 05:46:26 +000016357}
16358
Nadav Rotemd60cb112012-08-19 13:06:16 +000016359/// PerformFMinFMaxCombine - Do target-specific dag combines on X86ISD::FMIN and
16360/// X86ISD::FMAX nodes.
16361static SDValue PerformFMinFMaxCombine(SDNode *N, SelectionDAG &DAG) {
16362 assert(N->getOpcode() == X86ISD::FMIN || N->getOpcode() == X86ISD::FMAX);
16363
16364 // Only perform optimizations if UnsafeMath is used.
16365 if (!DAG.getTarget().Options.UnsafeFPMath)
16366 return SDValue();
16367
16368 // If we run in unsafe-math mode, then convert the FMAX and FMIN nodes
Craig Topper8365e9b2012-09-01 06:33:50 +000016369 // into FMINC and FMAXC, which are Commutative operations.
Nadav Rotemd60cb112012-08-19 13:06:16 +000016370 unsigned NewOp = 0;
16371 switch (N->getOpcode()) {
16372 default: llvm_unreachable("unknown opcode");
16373 case X86ISD::FMIN: NewOp = X86ISD::FMINC; break;
16374 case X86ISD::FMAX: NewOp = X86ISD::FMAXC; break;
16375 }
16376
16377 return DAG.getNode(NewOp, N->getDebugLoc(), N->getValueType(0),
16378 N->getOperand(0), N->getOperand(1));
16379}
16380
16381
Chris Lattneraf723b92008-01-25 05:46:26 +000016382/// PerformFANDCombine - Do target-specific dag combines on X86ISD::FAND nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000016383static SDValue PerformFANDCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattneraf723b92008-01-25 05:46:26 +000016384 // FAND(0.0, x) -> 0.0
16385 // FAND(x, 0.0) -> 0.0
16386 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
16387 if (C->getValueAPF().isPosZero())
16388 return N->getOperand(0);
16389 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
16390 if (C->getValueAPF().isPosZero())
16391 return N->getOperand(1);
Dan Gohman475871a2008-07-27 21:46:04 +000016392 return SDValue();
Chris Lattneraf723b92008-01-25 05:46:26 +000016393}
16394
Dan Gohmane5af2d32009-01-29 01:59:02 +000016395static SDValue PerformBTCombine(SDNode *N,
16396 SelectionDAG &DAG,
16397 TargetLowering::DAGCombinerInfo &DCI) {
16398 // BT ignores high bits in the bit index operand.
16399 SDValue Op1 = N->getOperand(1);
16400 if (Op1.hasOneUse()) {
16401 unsigned BitWidth = Op1.getValueSizeInBits();
16402 APInt DemandedMask = APInt::getLowBitsSet(BitWidth, Log2_32(BitWidth));
16403 APInt KnownZero, KnownOne;
Evan Chenge5b51ac2010-04-17 06:13:15 +000016404 TargetLowering::TargetLoweringOpt TLO(DAG, !DCI.isBeforeLegalize(),
16405 !DCI.isBeforeLegalizeOps());
Dan Gohmand858e902010-04-17 15:26:15 +000016406 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Dan Gohmane5af2d32009-01-29 01:59:02 +000016407 if (TLO.ShrinkDemandedConstant(Op1, DemandedMask) ||
16408 TLI.SimplifyDemandedBits(Op1, DemandedMask, KnownZero, KnownOne, TLO))
16409 DCI.CommitTargetLoweringOpt(TLO);
16410 }
16411 return SDValue();
16412}
Chris Lattner83e6c992006-10-04 06:57:07 +000016413
Eli Friedman7a5e5552009-06-07 06:52:44 +000016414static SDValue PerformVZEXT_MOVLCombine(SDNode *N, SelectionDAG &DAG) {
16415 SDValue Op = N->getOperand(0);
Wesley Peckbf17cfa2010-11-23 03:31:01 +000016416 if (Op.getOpcode() == ISD::BITCAST)
Eli Friedman7a5e5552009-06-07 06:52:44 +000016417 Op = Op.getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +000016418 EVT VT = N->getValueType(0), OpVT = Op.getValueType();
Eli Friedman7a5e5552009-06-07 06:52:44 +000016419 if (Op.getOpcode() == X86ISD::VZEXT_LOAD &&
Eric Christopherfd179292009-08-27 18:07:15 +000016420 VT.getVectorElementType().getSizeInBits() ==
Eli Friedman7a5e5552009-06-07 06:52:44 +000016421 OpVT.getVectorElementType().getSizeInBits()) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +000016422 return DAG.getNode(ISD::BITCAST, N->getDebugLoc(), VT, Op);
Eli Friedman7a5e5552009-06-07 06:52:44 +000016423 }
16424 return SDValue();
16425}
16426
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +000016427static SDValue PerformSExtCombine(SDNode *N, SelectionDAG &DAG,
16428 TargetLowering::DAGCombinerInfo &DCI,
16429 const X86Subtarget *Subtarget) {
16430 if (!DCI.isBeforeLegalizeOps())
16431 return SDValue();
16432
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000016433 if (!Subtarget->hasFp256())
Elena Demikhovskyf6020402012-02-08 08:37:26 +000016434 return SDValue();
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +000016435
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +000016436 EVT VT = N->getValueType(0);
16437 SDValue Op = N->getOperand(0);
16438 EVT OpVT = Op.getValueType();
16439 DebugLoc dl = N->getDebugLoc();
16440
Elena Demikhovskyf6020402012-02-08 08:37:26 +000016441 if ((VT == MVT::v4i64 && OpVT == MVT::v4i32) ||
16442 (VT == MVT::v8i32 && OpVT == MVT::v8i16)) {
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +000016443
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000016444 if (Subtarget->hasInt256())
Elena Demikhovsky1da58672012-04-22 09:39:03 +000016445 return DAG.getNode(X86ISD::VSEXT_MOVL, dl, VT, Op);
Elena Demikhovsky1da58672012-04-22 09:39:03 +000016446
16447 // Optimize vectors in AVX mode
16448 // Sign extend v8i16 to v8i32 and
16449 // v4i32 to v4i64
16450 //
16451 // Divide input vector into two parts
16452 // for v4i32 the shuffle mask will be { 0, 1, -1, -1} {2, 3, -1, -1}
16453 // use vpmovsx instruction to extend v4i32 -> v2i64; v8i16 -> v4i32
16454 // concat the vectors to original VT
16455
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +000016456 unsigned NumElems = OpVT.getVectorNumElements();
Craig Toppercacafd42012-08-14 08:18:43 +000016457 SDValue Undef = DAG.getUNDEF(OpVT);
16458
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +000016459 SmallVector<int,8> ShufMask1(NumElems, -1);
Craig Topper3ef43cf2012-04-24 06:36:35 +000016460 for (unsigned i = 0; i != NumElems/2; ++i)
16461 ShufMask1[i] = i;
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +000016462
Craig Toppercacafd42012-08-14 08:18:43 +000016463 SDValue OpLo = DAG.getVectorShuffle(OpVT, dl, Op, Undef, &ShufMask1[0]);
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +000016464
16465 SmallVector<int,8> ShufMask2(NumElems, -1);
Craig Topper3ef43cf2012-04-24 06:36:35 +000016466 for (unsigned i = 0; i != NumElems/2; ++i)
16467 ShufMask2[i] = i + NumElems/2;
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +000016468
Craig Toppercacafd42012-08-14 08:18:43 +000016469 SDValue OpHi = DAG.getVectorShuffle(OpVT, dl, Op, Undef, &ShufMask2[0]);
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +000016470
Craig Topper3ef43cf2012-04-24 06:36:35 +000016471 EVT HalfVT = EVT::getVectorVT(*DAG.getContext(), VT.getScalarType(),
Elena Demikhovskyf6020402012-02-08 08:37:26 +000016472 VT.getVectorNumElements()/2);
16473
Craig Topper3ef43cf2012-04-24 06:36:35 +000016474 OpLo = DAG.getNode(X86ISD::VSEXT_MOVL, dl, HalfVT, OpLo);
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +000016475 OpHi = DAG.getNode(X86ISD::VSEXT_MOVL, dl, HalfVT, OpHi);
16476
16477 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT, OpLo, OpHi);
16478 }
16479 return SDValue();
16480}
16481
Michael Liaof6c24ee2012-08-10 14:39:24 +000016482static SDValue PerformFMACombine(SDNode *N, SelectionDAG &DAG,
Elena Demikhovsky1503aba2012-08-01 12:06:00 +000016483 const X86Subtarget* Subtarget) {
16484 DebugLoc dl = N->getDebugLoc();
16485 EVT VT = N->getValueType(0);
16486
Craig Topperb1bdd7d2012-08-30 06:56:15 +000016487 // Let legalize expand this if it isn't a legal type yet.
16488 if (!DAG.getTargetLoweringInfo().isTypeLegal(VT))
16489 return SDValue();
16490
Elena Demikhovsky1503aba2012-08-01 12:06:00 +000016491 EVT ScalarVT = VT.getScalarType();
Craig Topperbf404372012-08-31 15:40:30 +000016492 if ((ScalarVT != MVT::f32 && ScalarVT != MVT::f64) ||
16493 (!Subtarget->hasFMA() && !Subtarget->hasFMA4()))
Elena Demikhovsky1503aba2012-08-01 12:06:00 +000016494 return SDValue();
16495
16496 SDValue A = N->getOperand(0);
16497 SDValue B = N->getOperand(1);
16498 SDValue C = N->getOperand(2);
16499
16500 bool NegA = (A.getOpcode() == ISD::FNEG);
16501 bool NegB = (B.getOpcode() == ISD::FNEG);
16502 bool NegC = (C.getOpcode() == ISD::FNEG);
16503
Michael Liaof6c24ee2012-08-10 14:39:24 +000016504 // Negative multiplication when NegA xor NegB
16505 bool NegMul = (NegA != NegB);
Elena Demikhovsky1503aba2012-08-01 12:06:00 +000016506 if (NegA)
16507 A = A.getOperand(0);
16508 if (NegB)
16509 B = B.getOperand(0);
16510 if (NegC)
16511 C = C.getOperand(0);
16512
16513 unsigned Opcode;
16514 if (!NegMul)
Craig Topperbf404372012-08-31 15:40:30 +000016515 Opcode = (!NegC) ? X86ISD::FMADD : X86ISD::FMSUB;
Elena Demikhovsky1503aba2012-08-01 12:06:00 +000016516 else
Craig Topperbf404372012-08-31 15:40:30 +000016517 Opcode = (!NegC) ? X86ISD::FNMADD : X86ISD::FNMSUB;
16518
Elena Demikhovsky1503aba2012-08-01 12:06:00 +000016519 return DAG.getNode(Opcode, dl, VT, A, B, C);
16520}
16521
Elena Demikhovsky28d7e712012-01-24 13:54:13 +000016522static SDValue PerformZExtCombine(SDNode *N, SelectionDAG &DAG,
Craig Topperc16f8512012-04-25 06:39:39 +000016523 TargetLowering::DAGCombinerInfo &DCI,
Elena Demikhovsky28d7e712012-01-24 13:54:13 +000016524 const X86Subtarget *Subtarget) {
Evan Cheng2e489c42009-12-16 00:53:11 +000016525 // (i32 zext (and (i8 x86isd::setcc_carry), 1)) ->
16526 // (and (i32 x86isd::setcc_carry), 1)
16527 // This eliminates the zext. This transformation is necessary because
16528 // ISD::SETCC is always legalized to i8.
16529 DebugLoc dl = N->getDebugLoc();
16530 SDValue N0 = N->getOperand(0);
16531 EVT VT = N->getValueType(0);
Elena Demikhovsky28d7e712012-01-24 13:54:13 +000016532 EVT OpVT = N0.getValueType();
16533
Evan Cheng2e489c42009-12-16 00:53:11 +000016534 if (N0.getOpcode() == ISD::AND &&
16535 N0.hasOneUse() &&
16536 N0.getOperand(0).hasOneUse()) {
16537 SDValue N00 = N0.getOperand(0);
16538 if (N00.getOpcode() != X86ISD::SETCC_CARRY)
16539 return SDValue();
16540 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
16541 if (!C || C->getZExtValue() != 1)
16542 return SDValue();
16543 return DAG.getNode(ISD::AND, dl, VT,
16544 DAG.getNode(X86ISD::SETCC_CARRY, dl, VT,
16545 N00.getOperand(0), N00.getOperand(1)),
16546 DAG.getConstant(1, VT));
16547 }
Craig Topperd0cf5652012-04-21 18:13:35 +000016548
Elena Demikhovsky28d7e712012-01-24 13:54:13 +000016549 // Optimize vectors in AVX mode:
16550 //
16551 // v8i16 -> v8i32
16552 // Use vpunpcklwd for 4 lower elements v8i16 -> v4i32.
16553 // Use vpunpckhwd for 4 upper elements v8i16 -> v4i32.
16554 // Concat upper and lower parts.
16555 //
16556 // v4i32 -> v4i64
16557 // Use vpunpckldq for 4 lower elements v4i32 -> v2i64.
16558 // Use vpunpckhdq for 4 upper elements v4i32 -> v2i64.
16559 // Concat upper and lower parts.
16560 //
Craig Topperc16f8512012-04-25 06:39:39 +000016561 if (!DCI.isBeforeLegalizeOps())
16562 return SDValue();
Elena Demikhovsky28d7e712012-01-24 13:54:13 +000016563
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000016564 if (!Subtarget->hasFp256())
Craig Topperc16f8512012-04-25 06:39:39 +000016565 return SDValue();
Elena Demikhovsky28d7e712012-01-24 13:54:13 +000016566
Craig Topperc16f8512012-04-25 06:39:39 +000016567 if (((VT == MVT::v8i32) && (OpVT == MVT::v8i16)) ||
16568 ((VT == MVT::v4i64) && (OpVT == MVT::v4i32))) {
Elena Demikhovsky1da58672012-04-22 09:39:03 +000016569
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000016570 if (Subtarget->hasInt256())
Craig Topperc16f8512012-04-25 06:39:39 +000016571 return DAG.getNode(X86ISD::VZEXT_MOVL, dl, VT, N0);
Elena Demikhovsky28d7e712012-01-24 13:54:13 +000016572
Craig Topperc16f8512012-04-25 06:39:39 +000016573 SDValue ZeroVec = getZeroVector(OpVT, Subtarget, DAG, dl);
16574 SDValue OpLo = getUnpackl(DAG, dl, OpVT, N0, ZeroVec);
16575 SDValue OpHi = getUnpackh(DAG, dl, OpVT, N0, ZeroVec);
Elena Demikhovsky28d7e712012-01-24 13:54:13 +000016576
Craig Topperc16f8512012-04-25 06:39:39 +000016577 EVT HVT = EVT::getVectorVT(*DAG.getContext(), VT.getVectorElementType(),
16578 VT.getVectorNumElements()/2);
Elena Demikhovsky28d7e712012-01-24 13:54:13 +000016579
Craig Topperc16f8512012-04-25 06:39:39 +000016580 OpLo = DAG.getNode(ISD::BITCAST, dl, HVT, OpLo);
16581 OpHi = DAG.getNode(ISD::BITCAST, dl, HVT, OpHi);
16582
16583 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT, OpLo, OpHi);
Elena Demikhovsky28d7e712012-01-24 13:54:13 +000016584 }
16585
Evan Cheng2e489c42009-12-16 00:53:11 +000016586 return SDValue();
16587}
16588
Chad Rosiera73b6fc2012-04-27 22:33:25 +000016589// Optimize x == -y --> x+y == 0
16590// x != -y --> x+y != 0
16591static SDValue PerformISDSETCCCombine(SDNode *N, SelectionDAG &DAG) {
16592 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(2))->get();
16593 SDValue LHS = N->getOperand(0);
Chad Rosiera20e1e72012-08-01 18:39:17 +000016594 SDValue RHS = N->getOperand(1);
Chad Rosiera73b6fc2012-04-27 22:33:25 +000016595
16596 if ((CC == ISD::SETNE || CC == ISD::SETEQ) && LHS.getOpcode() == ISD::SUB)
16597 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(LHS.getOperand(0)))
16598 if (C->getAPIntValue() == 0 && LHS.hasOneUse()) {
16599 SDValue addV = DAG.getNode(ISD::ADD, N->getDebugLoc(),
16600 LHS.getValueType(), RHS, LHS.getOperand(1));
16601 return DAG.getSetCC(N->getDebugLoc(), N->getValueType(0),
16602 addV, DAG.getConstant(0, addV.getValueType()), CC);
16603 }
16604 if ((CC == ISD::SETNE || CC == ISD::SETEQ) && RHS.getOpcode() == ISD::SUB)
16605 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(RHS.getOperand(0)))
16606 if (C->getAPIntValue() == 0 && RHS.hasOneUse()) {
16607 SDValue addV = DAG.getNode(ISD::ADD, N->getDebugLoc(),
16608 RHS.getValueType(), LHS, RHS.getOperand(1));
16609 return DAG.getSetCC(N->getDebugLoc(), N->getValueType(0),
16610 addV, DAG.getConstant(0, addV.getValueType()), CC);
16611 }
16612 return SDValue();
16613}
16614
Shuxin Yanga5526a92012-10-31 23:11:48 +000016615// Helper function of PerformSETCCCombine. It is to materialize "setb reg"
16616// as "sbb reg,reg", since it can be extended without zext and produces
16617// an all-ones bit which is more useful than 0/1 in some cases.
16618static SDValue MaterializeSETB(DebugLoc DL, SDValue EFLAGS, SelectionDAG &DAG) {
16619 return DAG.getNode(ISD::AND, DL, MVT::i8,
16620 DAG.getNode(X86ISD::SETCC_CARRY, DL, MVT::i8,
16621 DAG.getConstant(X86::COND_B, MVT::i8), EFLAGS),
16622 DAG.getConstant(1, MVT::i8));
16623}
16624
Chris Lattnerc19d1c32010-12-19 22:08:31 +000016625// Optimize RES = X86ISD::SETCC CONDCODE, EFLAG_INPUT
Michael Liaodbf8b5b2012-08-28 03:34:40 +000016626static SDValue PerformSETCCCombine(SDNode *N, SelectionDAG &DAG,
16627 TargetLowering::DAGCombinerInfo &DCI,
16628 const X86Subtarget *Subtarget) {
Chris Lattnerc19d1c32010-12-19 22:08:31 +000016629 DebugLoc DL = N->getDebugLoc();
Michael Liao2a33cec2012-08-10 19:58:13 +000016630 X86::CondCode CC = X86::CondCode(N->getConstantOperandVal(0));
16631 SDValue EFLAGS = N->getOperand(1);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000016632
Shuxin Yanga5526a92012-10-31 23:11:48 +000016633 if (CC == X86::COND_A) {
16634 // Try to convert COND_A into COND_B in an attempt to facilitate
16635 // materializing "setb reg".
16636 //
16637 // Do not flip "e > c", where "c" is a constant, because Cmp instruction
16638 // cannot take an immediate as its first operand.
16639 //
16640 if (EFLAGS.getOpcode() == X86ISD::SUB && EFLAGS.hasOneUse() &&
16641 EFLAGS.getValueType().isInteger() &&
16642 !isa<ConstantSDNode>(EFLAGS.getOperand(1))) {
16643 SDValue NewSub = DAG.getNode(X86ISD::SUB, EFLAGS.getDebugLoc(),
16644 EFLAGS.getNode()->getVTList(),
16645 EFLAGS.getOperand(1), EFLAGS.getOperand(0));
16646 SDValue NewEFLAGS = SDValue(NewSub.getNode(), EFLAGS.getResNo());
16647 return MaterializeSETB(DL, NewEFLAGS, DAG);
16648 }
16649 }
16650
Chris Lattnerc19d1c32010-12-19 22:08:31 +000016651 // Materialize "setb reg" as "sbb reg,reg", since it can be extended without
16652 // a zext and produces an all-ones bit which is more useful than 0/1 in some
16653 // cases.
Michael Liao2a33cec2012-08-10 19:58:13 +000016654 if (CC == X86::COND_B)
Shuxin Yanga5526a92012-10-31 23:11:48 +000016655 return MaterializeSETB(DL, EFLAGS, DAG);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000016656
Michael Liao2a33cec2012-08-10 19:58:13 +000016657 SDValue Flags;
16658
Michael Liaodbf8b5b2012-08-28 03:34:40 +000016659 Flags = checkBoolTestSetCCCombine(EFLAGS, CC);
16660 if (Flags.getNode()) {
16661 SDValue Cond = DAG.getConstant(CC, MVT::i8);
16662 return DAG.getNode(X86ISD::SETCC, DL, N->getVTList(), Cond, Flags);
16663 }
16664
Michael Liao2a33cec2012-08-10 19:58:13 +000016665 return SDValue();
16666}
16667
16668// Optimize branch condition evaluation.
16669//
16670static SDValue PerformBrCondCombine(SDNode *N, SelectionDAG &DAG,
16671 TargetLowering::DAGCombinerInfo &DCI,
16672 const X86Subtarget *Subtarget) {
16673 DebugLoc DL = N->getDebugLoc();
16674 SDValue Chain = N->getOperand(0);
16675 SDValue Dest = N->getOperand(1);
16676 SDValue EFLAGS = N->getOperand(3);
16677 X86::CondCode CC = X86::CondCode(N->getConstantOperandVal(2));
16678
16679 SDValue Flags;
16680
Michael Liaodbf8b5b2012-08-28 03:34:40 +000016681 Flags = checkBoolTestSetCCCombine(EFLAGS, CC);
16682 if (Flags.getNode()) {
16683 SDValue Cond = DAG.getConstant(CC, MVT::i8);
16684 return DAG.getNode(X86ISD::BRCOND, DL, N->getVTList(), Chain, Dest, Cond,
16685 Flags);
16686 }
16687
Chris Lattnerc19d1c32010-12-19 22:08:31 +000016688 return SDValue();
16689}
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000016690
Benjamin Kramer1396c402011-06-18 11:09:41 +000016691static SDValue PerformSINT_TO_FPCombine(SDNode *N, SelectionDAG &DAG,
16692 const X86TargetLowering *XTLI) {
Stuart Hastingsf99a4b82011-06-06 23:15:58 +000016693 SDValue Op0 = N->getOperand(0);
Nadav Rotema3540772012-04-23 21:53:37 +000016694 EVT InVT = Op0->getValueType(0);
Nadav Rotema3540772012-04-23 21:53:37 +000016695
16696 // SINT_TO_FP(v4i8) -> SINT_TO_FP(SEXT(v4i8 to v4i32))
Craig Topper7fd5e162012-04-24 06:02:29 +000016697 if (InVT == MVT::v8i8 || InVT == MVT::v4i8) {
Nadav Rotema3540772012-04-23 21:53:37 +000016698 DebugLoc dl = N->getDebugLoc();
Craig Topper7fd5e162012-04-24 06:02:29 +000016699 MVT DstVT = InVT == MVT::v4i8 ? MVT::v4i32 : MVT::v8i32;
Nadav Rotema3540772012-04-23 21:53:37 +000016700 SDValue P = DAG.getNode(ISD::SIGN_EXTEND, dl, DstVT, Op0);
16701 return DAG.getNode(ISD::SINT_TO_FP, dl, N->getValueType(0), P);
16702 }
16703
Stuart Hastingsf99a4b82011-06-06 23:15:58 +000016704 // Transform (SINT_TO_FP (i64 ...)) into an x87 operation if we have
16705 // a 32-bit target where SSE doesn't support i64->FP operations.
16706 if (Op0.getOpcode() == ISD::LOAD) {
16707 LoadSDNode *Ld = cast<LoadSDNode>(Op0.getNode());
16708 EVT VT = Ld->getValueType(0);
16709 if (!Ld->isVolatile() && !N->getValueType(0).isVector() &&
16710 ISD::isNON_EXTLoad(Op0.getNode()) && Op0.hasOneUse() &&
16711 !XTLI->getSubtarget()->is64Bit() &&
16712 !DAG.getTargetLoweringInfo().isTypeLegal(VT)) {
Benjamin Kramer1396c402011-06-18 11:09:41 +000016713 SDValue FILDChain = XTLI->BuildFILD(SDValue(N, 0), Ld->getValueType(0),
16714 Ld->getChain(), Op0, DAG);
Stuart Hastingsf99a4b82011-06-06 23:15:58 +000016715 DAG.ReplaceAllUsesOfValueWith(Op0.getValue(1), FILDChain.getValue(1));
16716 return FILDChain;
16717 }
16718 }
16719 return SDValue();
16720}
16721
Chris Lattner23a01992010-12-20 01:37:09 +000016722// Optimize RES, EFLAGS = X86ISD::ADC LHS, RHS, EFLAGS
16723static SDValue PerformADCCombine(SDNode *N, SelectionDAG &DAG,
16724 X86TargetLowering::DAGCombinerInfo &DCI) {
16725 // If the LHS and RHS of the ADC node are zero, then it can't overflow and
16726 // the result is either zero or one (depending on the input carry bit).
16727 // Strength reduce this down to a "set on carry" aka SETCC_CARRY&1.
16728 if (X86::isZeroNode(N->getOperand(0)) &&
16729 X86::isZeroNode(N->getOperand(1)) &&
16730 // We don't have a good way to replace an EFLAGS use, so only do this when
16731 // dead right now.
16732 SDValue(N, 1).use_empty()) {
16733 DebugLoc DL = N->getDebugLoc();
16734 EVT VT = N->getValueType(0);
16735 SDValue CarryOut = DAG.getConstant(0, N->getValueType(1));
16736 SDValue Res1 = DAG.getNode(ISD::AND, DL, VT,
16737 DAG.getNode(X86ISD::SETCC_CARRY, DL, VT,
16738 DAG.getConstant(X86::COND_B,MVT::i8),
16739 N->getOperand(2)),
16740 DAG.getConstant(1, VT));
16741 return DCI.CombineTo(N, Res1, CarryOut);
16742 }
16743
16744 return SDValue();
16745}
16746
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000016747// fold (add Y, (sete X, 0)) -> adc 0, Y
16748// (add Y, (setne X, 0)) -> sbb -1, Y
16749// (sub (sete X, 0), Y) -> sbb 0, Y
16750// (sub (setne X, 0), Y) -> adc -1, Y
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000016751static SDValue OptimizeConditionalInDecrement(SDNode *N, SelectionDAG &DAG) {
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000016752 DebugLoc DL = N->getDebugLoc();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000016753
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000016754 // Look through ZExts.
16755 SDValue Ext = N->getOperand(N->getOpcode() == ISD::SUB ? 1 : 0);
16756 if (Ext.getOpcode() != ISD::ZERO_EXTEND || !Ext.hasOneUse())
16757 return SDValue();
16758
16759 SDValue SetCC = Ext.getOperand(0);
16760 if (SetCC.getOpcode() != X86ISD::SETCC || !SetCC.hasOneUse())
16761 return SDValue();
16762
16763 X86::CondCode CC = (X86::CondCode)SetCC.getConstantOperandVal(0);
16764 if (CC != X86::COND_E && CC != X86::COND_NE)
16765 return SDValue();
16766
16767 SDValue Cmp = SetCC.getOperand(1);
16768 if (Cmp.getOpcode() != X86ISD::CMP || !Cmp.hasOneUse() ||
Chris Lattner9cd3da42011-01-16 02:56:53 +000016769 !X86::isZeroNode(Cmp.getOperand(1)) ||
16770 !Cmp.getOperand(0).getValueType().isInteger())
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000016771 return SDValue();
16772
16773 SDValue CmpOp0 = Cmp.getOperand(0);
16774 SDValue NewCmp = DAG.getNode(X86ISD::CMP, DL, MVT::i32, CmpOp0,
16775 DAG.getConstant(1, CmpOp0.getValueType()));
16776
16777 SDValue OtherVal = N->getOperand(N->getOpcode() == ISD::SUB ? 0 : 1);
16778 if (CC == X86::COND_NE)
16779 return DAG.getNode(N->getOpcode() == ISD::SUB ? X86ISD::ADC : X86ISD::SBB,
16780 DL, OtherVal.getValueType(), OtherVal,
16781 DAG.getConstant(-1ULL, OtherVal.getValueType()), NewCmp);
16782 return DAG.getNode(N->getOpcode() == ISD::SUB ? X86ISD::SBB : X86ISD::ADC,
16783 DL, OtherVal.getValueType(), OtherVal,
16784 DAG.getConstant(0, OtherVal.getValueType()), NewCmp);
16785}
Chris Lattnerc19d1c32010-12-19 22:08:31 +000016786
Craig Topper54f952a2011-11-19 09:02:40 +000016787/// PerformADDCombine - Do target-specific dag combines on integer adds.
16788static SDValue PerformAddCombine(SDNode *N, SelectionDAG &DAG,
16789 const X86Subtarget *Subtarget) {
16790 EVT VT = N->getValueType(0);
16791 SDValue Op0 = N->getOperand(0);
16792 SDValue Op1 = N->getOperand(1);
16793
16794 // Try to synthesize horizontal adds from adds of shuffles.
Craig Topperd0a31172012-01-10 06:37:29 +000016795 if (((Subtarget->hasSSSE3() && (VT == MVT::v8i16 || VT == MVT::v4i32)) ||
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000016796 (Subtarget->hasInt256() && (VT == MVT::v16i16 || VT == MVT::v8i32))) &&
Craig Topper54f952a2011-11-19 09:02:40 +000016797 isHorizontalBinOp(Op0, Op1, true))
16798 return DAG.getNode(X86ISD::HADD, N->getDebugLoc(), VT, Op0, Op1);
16799
16800 return OptimizeConditionalInDecrement(N, DAG);
16801}
16802
16803static SDValue PerformSubCombine(SDNode *N, SelectionDAG &DAG,
16804 const X86Subtarget *Subtarget) {
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000016805 SDValue Op0 = N->getOperand(0);
16806 SDValue Op1 = N->getOperand(1);
16807
16808 // X86 can't encode an immediate LHS of a sub. See if we can push the
16809 // negation into a preceding instruction.
16810 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op0)) {
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000016811 // If the RHS of the sub is a XOR with one use and a constant, invert the
16812 // immediate. Then add one to the LHS of the sub so we can turn
16813 // X-Y -> X+~Y+1, saving one register.
16814 if (Op1->hasOneUse() && Op1.getOpcode() == ISD::XOR &&
16815 isa<ConstantSDNode>(Op1.getOperand(1))) {
Nick Lewycky726ebd62011-08-23 19:01:24 +000016816 APInt XorC = cast<ConstantSDNode>(Op1.getOperand(1))->getAPIntValue();
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000016817 EVT VT = Op0.getValueType();
16818 SDValue NewXor = DAG.getNode(ISD::XOR, Op1.getDebugLoc(), VT,
16819 Op1.getOperand(0),
16820 DAG.getConstant(~XorC, VT));
16821 return DAG.getNode(ISD::ADD, N->getDebugLoc(), VT, NewXor,
Nick Lewycky726ebd62011-08-23 19:01:24 +000016822 DAG.getConstant(C->getAPIntValue()+1, VT));
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000016823 }
16824 }
16825
Craig Topper54f952a2011-11-19 09:02:40 +000016826 // Try to synthesize horizontal adds from adds of shuffles.
16827 EVT VT = N->getValueType(0);
Craig Topperd0a31172012-01-10 06:37:29 +000016828 if (((Subtarget->hasSSSE3() && (VT == MVT::v8i16 || VT == MVT::v4i32)) ||
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000016829 (Subtarget->hasInt256() && (VT == MVT::v16i16 || VT == MVT::v8i32))) &&
Craig Topperb72039c2011-11-30 09:10:50 +000016830 isHorizontalBinOp(Op0, Op1, true))
Craig Topper54f952a2011-11-19 09:02:40 +000016831 return DAG.getNode(X86ISD::HSUB, N->getDebugLoc(), VT, Op0, Op1);
16832
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000016833 return OptimizeConditionalInDecrement(N, DAG);
16834}
16835
Michael Liaod9d09602012-10-23 17:34:00 +000016836/// performVZEXTCombine - Performs build vector combines
16837static SDValue performVZEXTCombine(SDNode *N, SelectionDAG &DAG,
16838 TargetLowering::DAGCombinerInfo &DCI,
16839 const X86Subtarget *Subtarget) {
16840 // (vzext (bitcast (vzext (x)) -> (vzext x)
16841 SDValue In = N->getOperand(0);
16842 while (In.getOpcode() == ISD::BITCAST)
16843 In = In.getOperand(0);
16844
16845 if (In.getOpcode() != X86ISD::VZEXT)
16846 return SDValue();
16847
16848 return DAG.getNode(X86ISD::VZEXT, N->getDebugLoc(), N->getValueType(0), In.getOperand(0));
16849}
16850
Dan Gohman475871a2008-07-27 21:46:04 +000016851SDValue X86TargetLowering::PerformDAGCombine(SDNode *N,
Evan Cheng9dd93b32008-11-05 06:03:38 +000016852 DAGCombinerInfo &DCI) const {
Evan Cheng206ee9d2006-07-07 08:33:52 +000016853 SelectionDAG &DAG = DCI.DAG;
16854 switch (N->getOpcode()) {
16855 default: break;
Dan Gohman1bbf72b2010-03-15 23:23:03 +000016856 case ISD::EXTRACT_VECTOR_ELT:
Craig Topper89f4e662012-03-20 07:17:59 +000016857 return PerformEXTRACT_VECTOR_ELTCombine(N, DAG, DCI);
Duncan Sands6bcd2192011-09-17 16:49:39 +000016858 case ISD::VSELECT:
Nadav Rotemcc616562012-01-15 19:27:55 +000016859 case ISD::SELECT: return PerformSELECTCombine(N, DAG, DCI, Subtarget);
Michael Liaodbf8b5b2012-08-28 03:34:40 +000016860 case X86ISD::CMOV: return PerformCMOVCombine(N, DAG, DCI, Subtarget);
Craig Topper54f952a2011-11-19 09:02:40 +000016861 case ISD::ADD: return PerformAddCombine(N, DAG, Subtarget);
16862 case ISD::SUB: return PerformSubCombine(N, DAG, Subtarget);
Chris Lattner23a01992010-12-20 01:37:09 +000016863 case X86ISD::ADC: return PerformADCCombine(N, DAG, DCI);
Evan Cheng0b0cd912009-03-28 05:57:29 +000016864 case ISD::MUL: return PerformMulCombine(N, DAG, DCI);
Nate Begeman740ab032009-01-26 00:52:55 +000016865 case ISD::SHL:
16866 case ISD::SRA:
Mon P Wang845b1892012-02-01 22:15:20 +000016867 case ISD::SRL: return PerformShiftCombine(N, DAG, DCI, Subtarget);
Nate Begemanb65c1752010-12-17 22:55:37 +000016868 case ISD::AND: return PerformAndCombine(N, DAG, DCI, Subtarget);
Evan Cheng8b1190a2010-04-28 01:18:01 +000016869 case ISD::OR: return PerformOrCombine(N, DAG, DCI, Subtarget);
Craig Topperb4c94572011-10-21 06:55:01 +000016870 case ISD::XOR: return PerformXorCombine(N, DAG, DCI, Subtarget);
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000016871 case ISD::LOAD: return PerformLOADCombine(N, DAG, DCI, Subtarget);
Evan Cheng7e2ff772008-05-08 00:57:18 +000016872 case ISD::STORE: return PerformSTORECombine(N, DAG, Subtarget);
Stuart Hastingsf99a4b82011-06-06 23:15:58 +000016873 case ISD::SINT_TO_FP: return PerformSINT_TO_FPCombine(N, DAG, this);
Duncan Sands17470be2011-09-22 20:15:48 +000016874 case ISD::FADD: return PerformFADDCombine(N, DAG, Subtarget);
16875 case ISD::FSUB: return PerformFSUBCombine(N, DAG, Subtarget);
Chris Lattner6cf73262008-01-25 06:14:17 +000016876 case X86ISD::FXOR:
Chris Lattneraf723b92008-01-25 05:46:26 +000016877 case X86ISD::FOR: return PerformFORCombine(N, DAG);
Nadav Rotemd60cb112012-08-19 13:06:16 +000016878 case X86ISD::FMIN:
16879 case X86ISD::FMAX: return PerformFMinFMaxCombine(N, DAG);
Chris Lattneraf723b92008-01-25 05:46:26 +000016880 case X86ISD::FAND: return PerformFANDCombine(N, DAG);
Dan Gohmane5af2d32009-01-29 01:59:02 +000016881 case X86ISD::BT: return PerformBTCombine(N, DAG, DCI);
Eli Friedman7a5e5552009-06-07 06:52:44 +000016882 case X86ISD::VZEXT_MOVL: return PerformVZEXT_MOVLCombine(N, DAG);
Elena Demikhovsky1da58672012-04-22 09:39:03 +000016883 case ISD::ANY_EXTEND:
Craig Topperc16f8512012-04-25 06:39:39 +000016884 case ISD::ZERO_EXTEND: return PerformZExtCombine(N, DAG, DCI, Subtarget);
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +000016885 case ISD::SIGN_EXTEND: return PerformSExtCombine(N, DAG, DCI, Subtarget);
Craig Topper55b24052012-09-11 06:15:32 +000016886 case ISD::TRUNCATE: return PerformTruncateCombine(N, DAG,DCI,Subtarget);
Chad Rosiera73b6fc2012-04-27 22:33:25 +000016887 case ISD::SETCC: return PerformISDSETCCCombine(N, DAG);
Michael Liaodbf8b5b2012-08-28 03:34:40 +000016888 case X86ISD::SETCC: return PerformSETCCCombine(N, DAG, DCI, Subtarget);
Michael Liao2a33cec2012-08-10 19:58:13 +000016889 case X86ISD::BRCOND: return PerformBrCondCombine(N, DAG, DCI, Subtarget);
Michael Liaod9d09602012-10-23 17:34:00 +000016890 case X86ISD::VZEXT: return performVZEXTCombine(N, DAG, DCI, Subtarget);
Craig Topperb3982da2011-12-31 23:50:21 +000016891 case X86ISD::SHUFP: // Handle all target specific shuffles
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +000016892 case X86ISD::PALIGN:
Craig Topper34671b82011-12-06 08:21:25 +000016893 case X86ISD::UNPCKH:
16894 case X86ISD::UNPCKL:
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000016895 case X86ISD::MOVHLPS:
16896 case X86ISD::MOVLHPS:
16897 case X86ISD::PSHUFD:
16898 case X86ISD::PSHUFHW:
16899 case X86ISD::PSHUFLW:
16900 case X86ISD::MOVSS:
16901 case X86ISD::MOVSD:
Craig Topper316cd2a2011-11-30 06:25:25 +000016902 case X86ISD::VPERMILP:
Craig Topperec24e612011-11-30 07:47:51 +000016903 case X86ISD::VPERM2X128:
Bruno Cardoso Lopes50b37c72011-08-15 21:45:54 +000016904 case ISD::VECTOR_SHUFFLE: return PerformShuffleCombine(N, DAG, DCI,Subtarget);
Elena Demikhovsky1503aba2012-08-01 12:06:00 +000016905 case ISD::FMA: return PerformFMACombine(N, DAG, Subtarget);
Evan Cheng206ee9d2006-07-07 08:33:52 +000016906 }
16907
Dan Gohman475871a2008-07-27 21:46:04 +000016908 return SDValue();
Evan Cheng206ee9d2006-07-07 08:33:52 +000016909}
16910
Evan Chenge5b51ac2010-04-17 06:13:15 +000016911/// isTypeDesirableForOp - Return true if the target has native support for
16912/// the specified value type and it is 'desirable' to use the type for the
16913/// given node type. e.g. On x86 i16 is legal, but undesirable since i16
16914/// instruction encodings are longer and some i16 instructions are slow.
16915bool X86TargetLowering::isTypeDesirableForOp(unsigned Opc, EVT VT) const {
16916 if (!isTypeLegal(VT))
16917 return false;
Evan Cheng2bce5f4b2010-04-28 08:30:49 +000016918 if (VT != MVT::i16)
Evan Chenge5b51ac2010-04-17 06:13:15 +000016919 return true;
16920
16921 switch (Opc) {
16922 default:
16923 return true;
Evan Cheng4c26e932010-04-19 19:29:22 +000016924 case ISD::LOAD:
16925 case ISD::SIGN_EXTEND:
16926 case ISD::ZERO_EXTEND:
16927 case ISD::ANY_EXTEND:
Evan Chenge5b51ac2010-04-17 06:13:15 +000016928 case ISD::SHL:
Evan Chenge5b51ac2010-04-17 06:13:15 +000016929 case ISD::SRL:
16930 case ISD::SUB:
16931 case ISD::ADD:
16932 case ISD::MUL:
16933 case ISD::AND:
16934 case ISD::OR:
16935 case ISD::XOR:
16936 return false;
16937 }
16938}
16939
16940/// IsDesirableToPromoteOp - This method query the target whether it is
Evan Cheng64b7bf72010-04-16 06:14:10 +000016941/// beneficial for dag combiner to promote the specified node. If true, it
16942/// should return the desired promotion type by reference.
Evan Chenge5b51ac2010-04-17 06:13:15 +000016943bool X86TargetLowering::IsDesirableToPromoteOp(SDValue Op, EVT &PVT) const {
Evan Cheng64b7bf72010-04-16 06:14:10 +000016944 EVT VT = Op.getValueType();
16945 if (VT != MVT::i16)
16946 return false;
16947
Evan Cheng4c26e932010-04-19 19:29:22 +000016948 bool Promote = false;
16949 bool Commute = false;
Evan Cheng64b7bf72010-04-16 06:14:10 +000016950 switch (Op.getOpcode()) {
Evan Cheng4c26e932010-04-19 19:29:22 +000016951 default: break;
16952 case ISD::LOAD: {
16953 LoadSDNode *LD = cast<LoadSDNode>(Op);
16954 // If the non-extending load has a single use and it's not live out, then it
16955 // might be folded.
Evan Cheng2bce5f4b2010-04-28 08:30:49 +000016956 if (LD->getExtensionType() == ISD::NON_EXTLOAD /*&&
16957 Op.hasOneUse()*/) {
16958 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
16959 UE = Op.getNode()->use_end(); UI != UE; ++UI) {
16960 // The only case where we'd want to promote LOAD (rather then it being
16961 // promoted as an operand is when it's only use is liveout.
16962 if (UI->getOpcode() != ISD::CopyToReg)
16963 return false;
16964 }
16965 }
Evan Cheng4c26e932010-04-19 19:29:22 +000016966 Promote = true;
16967 break;
16968 }
16969 case ISD::SIGN_EXTEND:
16970 case ISD::ZERO_EXTEND:
16971 case ISD::ANY_EXTEND:
16972 Promote = true;
16973 break;
Evan Chenge5b51ac2010-04-17 06:13:15 +000016974 case ISD::SHL:
Evan Cheng2bce5f4b2010-04-28 08:30:49 +000016975 case ISD::SRL: {
Evan Chenge5b51ac2010-04-17 06:13:15 +000016976 SDValue N0 = Op.getOperand(0);
16977 // Look out for (store (shl (load), x)).
Evan Chengc82c20b2010-04-24 04:44:57 +000016978 if (MayFoldLoad(N0) && MayFoldIntoStore(Op))
Evan Chenge5b51ac2010-04-17 06:13:15 +000016979 return false;
Evan Cheng4c26e932010-04-19 19:29:22 +000016980 Promote = true;
Evan Chenge5b51ac2010-04-17 06:13:15 +000016981 break;
16982 }
Evan Cheng64b7bf72010-04-16 06:14:10 +000016983 case ISD::ADD:
16984 case ISD::MUL:
16985 case ISD::AND:
16986 case ISD::OR:
Evan Cheng4c26e932010-04-19 19:29:22 +000016987 case ISD::XOR:
16988 Commute = true;
16989 // fallthrough
16990 case ISD::SUB: {
Evan Cheng64b7bf72010-04-16 06:14:10 +000016991 SDValue N0 = Op.getOperand(0);
16992 SDValue N1 = Op.getOperand(1);
Evan Chengc82c20b2010-04-24 04:44:57 +000016993 if (!Commute && MayFoldLoad(N1))
Evan Cheng64b7bf72010-04-16 06:14:10 +000016994 return false;
16995 // Avoid disabling potential load folding opportunities.
Evan Chengc82c20b2010-04-24 04:44:57 +000016996 if (MayFoldLoad(N0) && (!isa<ConstantSDNode>(N1) || MayFoldIntoStore(Op)))
Evan Cheng64b7bf72010-04-16 06:14:10 +000016997 return false;
Evan Chengc82c20b2010-04-24 04:44:57 +000016998 if (MayFoldLoad(N1) && (!isa<ConstantSDNode>(N0) || MayFoldIntoStore(Op)))
Evan Cheng64b7bf72010-04-16 06:14:10 +000016999 return false;
Evan Cheng4c26e932010-04-19 19:29:22 +000017000 Promote = true;
Evan Cheng64b7bf72010-04-16 06:14:10 +000017001 }
17002 }
17003
17004 PVT = MVT::i32;
Evan Cheng4c26e932010-04-19 19:29:22 +000017005 return Promote;
Evan Cheng64b7bf72010-04-16 06:14:10 +000017006}
17007
Evan Cheng60c07e12006-07-05 22:17:51 +000017008//===----------------------------------------------------------------------===//
17009// X86 Inline Assembly Support
17010//===----------------------------------------------------------------------===//
17011
Benjamin Kramer2ea4cdb2011-12-18 19:59:20 +000017012namespace {
17013 // Helper to match a string separated by whitespace.
Benjamin Kramer0581ed72011-12-18 20:51:31 +000017014 bool matchAsmImpl(StringRef s, ArrayRef<const StringRef *> args) {
Benjamin Kramer2ea4cdb2011-12-18 19:59:20 +000017015 s = s.substr(s.find_first_not_of(" \t")); // Skip leading whitespace.
Benjamin Kramere6cddb72011-12-17 14:36:05 +000017016
Benjamin Kramer0581ed72011-12-18 20:51:31 +000017017 for (unsigned i = 0, e = args.size(); i != e; ++i) {
Benjamin Kramer2ea4cdb2011-12-18 19:59:20 +000017018 StringRef piece(*args[i]);
17019 if (!s.startswith(piece)) // Check if the piece matches.
17020 return false;
17021
17022 s = s.substr(piece.size());
17023 StringRef::size_type pos = s.find_first_not_of(" \t");
17024 if (pos == 0) // We matched a prefix.
17025 return false;
17026
17027 s = s.substr(pos);
Benjamin Kramere6cddb72011-12-17 14:36:05 +000017028 }
17029
Benjamin Kramer2ea4cdb2011-12-18 19:59:20 +000017030 return s.empty();
Benjamin Kramere6cddb72011-12-17 14:36:05 +000017031 }
Benjamin Kramer0581ed72011-12-18 20:51:31 +000017032 const VariadicFunction1<bool, StringRef, StringRef, matchAsmImpl> matchAsm={};
Benjamin Kramere6cddb72011-12-17 14:36:05 +000017033}
17034
Chris Lattnerb8105652009-07-20 17:51:36 +000017035bool X86TargetLowering::ExpandInlineAsm(CallInst *CI) const {
17036 InlineAsm *IA = cast<InlineAsm>(CI->getCalledValue());
Chris Lattnerb8105652009-07-20 17:51:36 +000017037
17038 std::string AsmStr = IA->getAsmString();
17039
Benjamin Kramere6cddb72011-12-17 14:36:05 +000017040 IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
17041 if (!Ty || Ty->getBitWidth() % 16 != 0)
17042 return false;
17043
Chris Lattnerb8105652009-07-20 17:51:36 +000017044 // TODO: should remove alternatives from the asmstring: "foo {a|b}" -> "foo a"
Benjamin Kramerd4f19592010-01-11 18:03:24 +000017045 SmallVector<StringRef, 4> AsmPieces;
Peter Collingbourne98361182010-11-13 19:54:23 +000017046 SplitString(AsmStr, AsmPieces, ";\n");
Chris Lattnerb8105652009-07-20 17:51:36 +000017047
17048 switch (AsmPieces.size()) {
17049 default: return false;
17050 case 1:
Chris Lattner7a2bdde2011-04-15 05:18:47 +000017051 // FIXME: this should verify that we are targeting a 486 or better. If not,
Benjamin Kramere6cddb72011-12-17 14:36:05 +000017052 // we will turn this bswap into something that will be lowered to logical
17053 // ops instead of emitting the bswap asm. For now, we don't support 486 or
17054 // lower so don't worry about this.
Chris Lattnerb8105652009-07-20 17:51:36 +000017055 // bswap $0
Benjamin Kramer2ea4cdb2011-12-18 19:59:20 +000017056 if (matchAsm(AsmPieces[0], "bswap", "$0") ||
17057 matchAsm(AsmPieces[0], "bswapl", "$0") ||
17058 matchAsm(AsmPieces[0], "bswapq", "$0") ||
17059 matchAsm(AsmPieces[0], "bswap", "${0:q}") ||
17060 matchAsm(AsmPieces[0], "bswapl", "${0:q}") ||
17061 matchAsm(AsmPieces[0], "bswapq", "${0:q}")) {
Chris Lattnerb8105652009-07-20 17:51:36 +000017062 // No need to check constraints, nothing other than the equivalent of
17063 // "=r,0" would be valid here.
Evan Cheng55d42002011-01-08 01:24:27 +000017064 return IntrinsicLowering::LowerToByteSwap(CI);
Chris Lattnerb8105652009-07-20 17:51:36 +000017065 }
Benjamin Kramere6cddb72011-12-17 14:36:05 +000017066
Chris Lattnerb8105652009-07-20 17:51:36 +000017067 // rorw $$8, ${0:w} --> llvm.bswap.i16
Duncan Sandsb0bc6c32010-02-15 16:12:20 +000017068 if (CI->getType()->isIntegerTy(16) &&
Benjamin Kramere6cddb72011-12-17 14:36:05 +000017069 IA->getConstraintString().compare(0, 5, "=r,0,") == 0 &&
Benjamin Kramer2ea4cdb2011-12-18 19:59:20 +000017070 (matchAsm(AsmPieces[0], "rorw", "$$8,", "${0:w}") ||
17071 matchAsm(AsmPieces[0], "rolw", "$$8,", "${0:w}"))) {
Dan Gohman0ef701e2010-03-04 19:58:08 +000017072 AsmPieces.clear();
Evan Cheng55d42002011-01-08 01:24:27 +000017073 const std::string &ConstraintsStr = IA->getConstraintString();
17074 SplitString(StringRef(ConstraintsStr).substr(5), AsmPieces, ",");
Dan Gohman0ef701e2010-03-04 19:58:08 +000017075 std::sort(AsmPieces.begin(), AsmPieces.end());
17076 if (AsmPieces.size() == 4 &&
17077 AsmPieces[0] == "~{cc}" &&
17078 AsmPieces[1] == "~{dirflag}" &&
17079 AsmPieces[2] == "~{flags}" &&
Benjamin Kramere6cddb72011-12-17 14:36:05 +000017080 AsmPieces[3] == "~{fpsr}")
17081 return IntrinsicLowering::LowerToByteSwap(CI);
Chris Lattnerb8105652009-07-20 17:51:36 +000017082 }
17083 break;
17084 case 3:
Peter Collingbourne948cf022010-11-13 19:54:30 +000017085 if (CI->getType()->isIntegerTy(32) &&
Benjamin Kramere6cddb72011-12-17 14:36:05 +000017086 IA->getConstraintString().compare(0, 5, "=r,0,") == 0 &&
Benjamin Kramer2ea4cdb2011-12-18 19:59:20 +000017087 matchAsm(AsmPieces[0], "rorw", "$$8,", "${0:w}") &&
17088 matchAsm(AsmPieces[1], "rorl", "$$16,", "$0") &&
17089 matchAsm(AsmPieces[2], "rorw", "$$8,", "${0:w}")) {
Benjamin Kramere6cddb72011-12-17 14:36:05 +000017090 AsmPieces.clear();
17091 const std::string &ConstraintsStr = IA->getConstraintString();
17092 SplitString(StringRef(ConstraintsStr).substr(5), AsmPieces, ",");
17093 std::sort(AsmPieces.begin(), AsmPieces.end());
17094 if (AsmPieces.size() == 4 &&
17095 AsmPieces[0] == "~{cc}" &&
17096 AsmPieces[1] == "~{dirflag}" &&
17097 AsmPieces[2] == "~{flags}" &&
17098 AsmPieces[3] == "~{fpsr}")
17099 return IntrinsicLowering::LowerToByteSwap(CI);
Peter Collingbourne948cf022010-11-13 19:54:30 +000017100 }
Evan Cheng55d42002011-01-08 01:24:27 +000017101
17102 if (CI->getType()->isIntegerTy(64)) {
17103 InlineAsm::ConstraintInfoVector Constraints = IA->ParseConstraints();
17104 if (Constraints.size() >= 2 &&
17105 Constraints[0].Codes.size() == 1 && Constraints[0].Codes[0] == "A" &&
17106 Constraints[1].Codes.size() == 1 && Constraints[1].Codes[0] == "0") {
17107 // bswap %eax / bswap %edx / xchgl %eax, %edx -> llvm.bswap.i64
Benjamin Kramer2ea4cdb2011-12-18 19:59:20 +000017108 if (matchAsm(AsmPieces[0], "bswap", "%eax") &&
17109 matchAsm(AsmPieces[1], "bswap", "%edx") &&
17110 matchAsm(AsmPieces[2], "xchgl", "%eax,", "%edx"))
Benjamin Kramere6cddb72011-12-17 14:36:05 +000017111 return IntrinsicLowering::LowerToByteSwap(CI);
Chris Lattnerb8105652009-07-20 17:51:36 +000017112 }
17113 }
17114 break;
17115 }
17116 return false;
17117}
17118
17119
17120
Chris Lattnerf4dff842006-07-11 02:54:03 +000017121/// getConstraintType - Given a constraint letter, return the type of
17122/// constraint it is for this target.
17123X86TargetLowering::ConstraintType
Chris Lattner4234f572007-03-25 02:14:49 +000017124X86TargetLowering::getConstraintType(const std::string &Constraint) const {
17125 if (Constraint.size() == 1) {
17126 switch (Constraint[0]) {
Chris Lattner4234f572007-03-25 02:14:49 +000017127 case 'R':
Chris Lattner4234f572007-03-25 02:14:49 +000017128 case 'q':
17129 case 'Q':
John Thompson44ab89e2010-10-29 17:29:13 +000017130 case 'f':
17131 case 't':
17132 case 'u':
Dale Johannesen2ffbcac2008-04-01 00:57:48 +000017133 case 'y':
John Thompson44ab89e2010-10-29 17:29:13 +000017134 case 'x':
Chris Lattner4234f572007-03-25 02:14:49 +000017135 case 'Y':
Eric Christopher31b5f002011-07-07 22:29:07 +000017136 case 'l':
Chris Lattner4234f572007-03-25 02:14:49 +000017137 return C_RegisterClass;
John Thompson44ab89e2010-10-29 17:29:13 +000017138 case 'a':
17139 case 'b':
17140 case 'c':
17141 case 'd':
17142 case 'S':
17143 case 'D':
17144 case 'A':
17145 return C_Register;
17146 case 'I':
17147 case 'J':
17148 case 'K':
17149 case 'L':
17150 case 'M':
17151 case 'N':
17152 case 'G':
17153 case 'C':
Dale Johannesen78e3e522009-02-12 20:58:09 +000017154 case 'e':
17155 case 'Z':
17156 return C_Other;
Chris Lattner4234f572007-03-25 02:14:49 +000017157 default:
17158 break;
17159 }
Chris Lattnerf4dff842006-07-11 02:54:03 +000017160 }
Chris Lattner4234f572007-03-25 02:14:49 +000017161 return TargetLowering::getConstraintType(Constraint);
Chris Lattnerf4dff842006-07-11 02:54:03 +000017162}
17163
John Thompson44ab89e2010-10-29 17:29:13 +000017164/// Examine constraint type and operand type and determine a weight value.
John Thompsoneac6e1d2010-09-13 18:15:37 +000017165/// This object must already have been set up with the operand type
17166/// and the current alternative constraint selected.
John Thompson44ab89e2010-10-29 17:29:13 +000017167TargetLowering::ConstraintWeight
17168 X86TargetLowering::getSingleConstraintMatchWeight(
John Thompsoneac6e1d2010-09-13 18:15:37 +000017169 AsmOperandInfo &info, const char *constraint) const {
John Thompson44ab89e2010-10-29 17:29:13 +000017170 ConstraintWeight weight = CW_Invalid;
John Thompsoneac6e1d2010-09-13 18:15:37 +000017171 Value *CallOperandVal = info.CallOperandVal;
17172 // If we don't have a value, we can't do a match,
17173 // but allow it at the lowest weight.
17174 if (CallOperandVal == NULL)
John Thompson44ab89e2010-10-29 17:29:13 +000017175 return CW_Default;
Chris Lattnerdb125cf2011-07-18 04:54:35 +000017176 Type *type = CallOperandVal->getType();
John Thompsoneac6e1d2010-09-13 18:15:37 +000017177 // Look at the constraint type.
17178 switch (*constraint) {
17179 default:
John Thompson44ab89e2010-10-29 17:29:13 +000017180 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
17181 case 'R':
17182 case 'q':
17183 case 'Q':
17184 case 'a':
17185 case 'b':
17186 case 'c':
17187 case 'd':
17188 case 'S':
17189 case 'D':
17190 case 'A':
17191 if (CallOperandVal->getType()->isIntegerTy())
17192 weight = CW_SpecificReg;
17193 break;
17194 case 'f':
17195 case 't':
17196 case 'u':
17197 if (type->isFloatingPointTy())
17198 weight = CW_SpecificReg;
17199 break;
17200 case 'y':
Chris Lattner2a786eb2010-12-19 20:19:20 +000017201 if (type->isX86_MMXTy() && Subtarget->hasMMX())
John Thompson44ab89e2010-10-29 17:29:13 +000017202 weight = CW_SpecificReg;
17203 break;
17204 case 'x':
17205 case 'Y':
Craig Topper1accb7e2012-01-10 06:54:16 +000017206 if (((type->getPrimitiveSizeInBits() == 128) && Subtarget->hasSSE1()) ||
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000017207 ((type->getPrimitiveSizeInBits() == 256) && Subtarget->hasFp256()))
John Thompson44ab89e2010-10-29 17:29:13 +000017208 weight = CW_Register;
John Thompsoneac6e1d2010-09-13 18:15:37 +000017209 break;
17210 case 'I':
17211 if (ConstantInt *C = dyn_cast<ConstantInt>(info.CallOperandVal)) {
17212 if (C->getZExtValue() <= 31)
John Thompson44ab89e2010-10-29 17:29:13 +000017213 weight = CW_Constant;
John Thompsoneac6e1d2010-09-13 18:15:37 +000017214 }
17215 break;
John Thompson44ab89e2010-10-29 17:29:13 +000017216 case 'J':
17217 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
17218 if (C->getZExtValue() <= 63)
17219 weight = CW_Constant;
17220 }
17221 break;
17222 case 'K':
17223 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
17224 if ((C->getSExtValue() >= -0x80) && (C->getSExtValue() <= 0x7f))
17225 weight = CW_Constant;
17226 }
17227 break;
17228 case 'L':
17229 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
17230 if ((C->getZExtValue() == 0xff) || (C->getZExtValue() == 0xffff))
17231 weight = CW_Constant;
17232 }
17233 break;
17234 case 'M':
17235 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
17236 if (C->getZExtValue() <= 3)
17237 weight = CW_Constant;
17238 }
17239 break;
17240 case 'N':
17241 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
17242 if (C->getZExtValue() <= 0xff)
17243 weight = CW_Constant;
17244 }
17245 break;
17246 case 'G':
17247 case 'C':
17248 if (dyn_cast<ConstantFP>(CallOperandVal)) {
17249 weight = CW_Constant;
17250 }
17251 break;
17252 case 'e':
17253 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
17254 if ((C->getSExtValue() >= -0x80000000LL) &&
17255 (C->getSExtValue() <= 0x7fffffffLL))
17256 weight = CW_Constant;
17257 }
17258 break;
17259 case 'Z':
17260 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
17261 if (C->getZExtValue() <= 0xffffffff)
17262 weight = CW_Constant;
17263 }
17264 break;
John Thompsoneac6e1d2010-09-13 18:15:37 +000017265 }
17266 return weight;
17267}
17268
Dale Johannesenba2a0b92008-01-29 02:21:21 +000017269/// LowerXConstraint - try to replace an X constraint, which matches anything,
17270/// with another that has more specific requirements based on the type of the
17271/// corresponding operand.
Chris Lattner5e764232008-04-26 23:02:14 +000017272const char *X86TargetLowering::
Owen Andersone50ed302009-08-10 22:56:29 +000017273LowerXConstraint(EVT ConstraintVT) const {
Chris Lattner5e764232008-04-26 23:02:14 +000017274 // FP X constraints get lowered to SSE1/2 registers if available, otherwise
17275 // 'f' like normal targets.
Duncan Sands83ec4b62008-06-06 12:08:01 +000017276 if (ConstraintVT.isFloatingPoint()) {
Craig Topper1accb7e2012-01-10 06:54:16 +000017277 if (Subtarget->hasSSE2())
Chris Lattner5e764232008-04-26 23:02:14 +000017278 return "Y";
Craig Topper1accb7e2012-01-10 06:54:16 +000017279 if (Subtarget->hasSSE1())
Chris Lattner5e764232008-04-26 23:02:14 +000017280 return "x";
17281 }
Scott Michelfdc40a02009-02-17 22:15:04 +000017282
Chris Lattner5e764232008-04-26 23:02:14 +000017283 return TargetLowering::LowerXConstraint(ConstraintVT);
Dale Johannesenba2a0b92008-01-29 02:21:21 +000017284}
17285
Chris Lattner48884cd2007-08-25 00:47:38 +000017286/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
17287/// vector. If it is invalid, don't add anything to Ops.
Dan Gohman475871a2008-07-27 21:46:04 +000017288void X86TargetLowering::LowerAsmOperandForConstraint(SDValue Op,
Eric Christopher100c8332011-06-02 23:16:42 +000017289 std::string &Constraint,
Dan Gohman475871a2008-07-27 21:46:04 +000017290 std::vector<SDValue>&Ops,
Chris Lattner5e764232008-04-26 23:02:14 +000017291 SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +000017292 SDValue Result(0, 0);
Scott Michelfdc40a02009-02-17 22:15:04 +000017293
Eric Christopher100c8332011-06-02 23:16:42 +000017294 // Only support length 1 constraints for now.
17295 if (Constraint.length() > 1) return;
Eric Christopher471e4222011-06-08 23:55:35 +000017296
Eric Christopher100c8332011-06-02 23:16:42 +000017297 char ConstraintLetter = Constraint[0];
17298 switch (ConstraintLetter) {
Chris Lattner22aaf1d2006-10-31 20:13:11 +000017299 default: break;
Devang Patel84f7fd22007-03-17 00:13:28 +000017300 case 'I':
Chris Lattner188b9fe2007-03-25 01:57:35 +000017301 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +000017302 if (C->getZExtValue() <= 31) {
17303 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattner48884cd2007-08-25 00:47:38 +000017304 break;
17305 }
Devang Patel84f7fd22007-03-17 00:13:28 +000017306 }
Chris Lattner48884cd2007-08-25 00:47:38 +000017307 return;
Evan Cheng364091e2008-09-22 23:57:37 +000017308 case 'J':
17309 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattner2e06dd22009-06-15 04:39:05 +000017310 if (C->getZExtValue() <= 63) {
Chris Lattnere4935152009-06-15 04:01:39 +000017311 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
17312 break;
17313 }
17314 }
17315 return;
17316 case 'K':
17317 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Jakub Staszakdccd7f92012-11-06 23:52:19 +000017318 if (isInt<8>(C->getSExtValue())) {
Evan Cheng364091e2008-09-22 23:57:37 +000017319 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
17320 break;
17321 }
17322 }
17323 return;
Chris Lattner188b9fe2007-03-25 01:57:35 +000017324 case 'N':
17325 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +000017326 if (C->getZExtValue() <= 255) {
17327 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattner48884cd2007-08-25 00:47:38 +000017328 break;
17329 }
Chris Lattner188b9fe2007-03-25 01:57:35 +000017330 }
Chris Lattner48884cd2007-08-25 00:47:38 +000017331 return;
Dale Johannesen78e3e522009-02-12 20:58:09 +000017332 case 'e': {
17333 // 32-bit signed value
17334 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohman7720cb32010-06-18 14:01:07 +000017335 if (ConstantInt::isValueValidForType(Type::getInt32Ty(*DAG.getContext()),
17336 C->getSExtValue())) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000017337 // Widen to 64 bits here to get it sign extended.
Owen Anderson825b72b2009-08-11 20:47:22 +000017338 Result = DAG.getTargetConstant(C->getSExtValue(), MVT::i64);
Dale Johannesen78e3e522009-02-12 20:58:09 +000017339 break;
17340 }
17341 // FIXME gcc accepts some relocatable values here too, but only in certain
17342 // memory models; it's complicated.
17343 }
17344 return;
17345 }
17346 case 'Z': {
17347 // 32-bit unsigned value
17348 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohman7720cb32010-06-18 14:01:07 +000017349 if (ConstantInt::isValueValidForType(Type::getInt32Ty(*DAG.getContext()),
17350 C->getZExtValue())) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000017351 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
17352 break;
17353 }
17354 }
17355 // FIXME gcc accepts some relocatable values here too, but only in certain
17356 // memory models; it's complicated.
17357 return;
17358 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000017359 case 'i': {
Chris Lattner22aaf1d2006-10-31 20:13:11 +000017360 // Literal immediates are always ok.
Chris Lattner48884cd2007-08-25 00:47:38 +000017361 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Op)) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000017362 // Widen to 64 bits here to get it sign extended.
Owen Anderson825b72b2009-08-11 20:47:22 +000017363 Result = DAG.getTargetConstant(CST->getSExtValue(), MVT::i64);
Chris Lattner48884cd2007-08-25 00:47:38 +000017364 break;
17365 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000017366
Dale Johannesene5ff9ef2010-06-24 20:14:51 +000017367 // In any sort of PIC mode addresses need to be computed at runtime by
17368 // adding in a register or some sort of table lookup. These can't
17369 // be used as immediates.
Dale Johannesene2b448c2010-07-06 23:27:00 +000017370 if (Subtarget->isPICStyleGOT() || Subtarget->isPICStyleStubPIC())
Dale Johannesene5ff9ef2010-06-24 20:14:51 +000017371 return;
17372
Chris Lattnerdc43a882007-05-03 16:52:29 +000017373 // If we are in non-pic codegen mode, we allow the address of a global (with
17374 // an optional displacement) to be used with 'i'.
Chris Lattner49921962009-05-08 18:23:14 +000017375 GlobalAddressSDNode *GA = 0;
Chris Lattnerdc43a882007-05-03 16:52:29 +000017376 int64_t Offset = 0;
Scott Michelfdc40a02009-02-17 22:15:04 +000017377
Chris Lattner49921962009-05-08 18:23:14 +000017378 // Match either (GA), (GA+C), (GA+C1+C2), etc.
17379 while (1) {
17380 if ((GA = dyn_cast<GlobalAddressSDNode>(Op))) {
17381 Offset += GA->getOffset();
17382 break;
17383 } else if (Op.getOpcode() == ISD::ADD) {
17384 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
17385 Offset += C->getZExtValue();
17386 Op = Op.getOperand(0);
17387 continue;
17388 }
17389 } else if (Op.getOpcode() == ISD::SUB) {
17390 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
17391 Offset += -C->getZExtValue();
17392 Op = Op.getOperand(0);
17393 continue;
17394 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000017395 }
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000017396
Chris Lattner49921962009-05-08 18:23:14 +000017397 // Otherwise, this isn't something we can handle, reject it.
17398 return;
Chris Lattnerdc43a882007-05-03 16:52:29 +000017399 }
Eric Christopherfd179292009-08-27 18:07:15 +000017400
Dan Gohman46510a72010-04-15 01:51:59 +000017401 const GlobalValue *GV = GA->getGlobal();
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000017402 // If we require an extra load to get this address, as in PIC mode, we
17403 // can't accept it.
Chris Lattner36c25012009-07-10 07:34:39 +000017404 if (isGlobalStubReference(Subtarget->ClassifyGlobalReference(GV,
17405 getTargetMachine())))
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000017406 return;
Scott Michelfdc40a02009-02-17 22:15:04 +000017407
Devang Patel0d881da2010-07-06 22:08:15 +000017408 Result = DAG.getTargetGlobalAddress(GV, Op.getDebugLoc(),
17409 GA->getValueType(0), Offset);
Chris Lattner49921962009-05-08 18:23:14 +000017410 break;
Chris Lattner22aaf1d2006-10-31 20:13:11 +000017411 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000017412 }
Scott Michelfdc40a02009-02-17 22:15:04 +000017413
Gabor Greifba36cb52008-08-28 21:40:38 +000017414 if (Result.getNode()) {
Chris Lattner48884cd2007-08-25 00:47:38 +000017415 Ops.push_back(Result);
17416 return;
17417 }
Dale Johannesen1784d162010-06-25 21:55:36 +000017418 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
Chris Lattner22aaf1d2006-10-31 20:13:11 +000017419}
17420
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000017421std::pair<unsigned, const TargetRegisterClass*>
Chris Lattnerf76d1802006-07-31 23:26:50 +000017422X86TargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +000017423 EVT VT) const {
Chris Lattnerad043e82007-04-09 05:11:28 +000017424 // First, see if this is a constraint that directly corresponds to an LLVM
17425 // register class.
17426 if (Constraint.size() == 1) {
17427 // GCC Constraint Letters
17428 switch (Constraint[0]) {
17429 default: break;
Eric Christopherd176af82011-06-29 17:23:50 +000017430 // TODO: Slight differences here in allocation order and leaving
17431 // RIP in the class. Do they matter any more here than they do
17432 // in the normal allocation?
17433 case 'q': // GENERAL_REGS in 64-bit mode, Q_REGS in 32-bit mode.
17434 if (Subtarget->is64Bit()) {
Craig Topperc9099502012-04-20 06:31:50 +000017435 if (VT == MVT::i32 || VT == MVT::f32)
17436 return std::make_pair(0U, &X86::GR32RegClass);
17437 if (VT == MVT::i16)
17438 return std::make_pair(0U, &X86::GR16RegClass);
17439 if (VT == MVT::i8 || VT == MVT::i1)
17440 return std::make_pair(0U, &X86::GR8RegClass);
17441 if (VT == MVT::i64 || VT == MVT::f64)
17442 return std::make_pair(0U, &X86::GR64RegClass);
17443 break;
Eric Christopherd176af82011-06-29 17:23:50 +000017444 }
17445 // 32-bit fallthrough
17446 case 'Q': // Q_REGS
Nick Lewycky9bf45d02011-07-08 00:19:27 +000017447 if (VT == MVT::i32 || VT == MVT::f32)
Craig Topperc9099502012-04-20 06:31:50 +000017448 return std::make_pair(0U, &X86::GR32_ABCDRegClass);
17449 if (VT == MVT::i16)
17450 return std::make_pair(0U, &X86::GR16_ABCDRegClass);
17451 if (VT == MVT::i8 || VT == MVT::i1)
17452 return std::make_pair(0U, &X86::GR8_ABCD_LRegClass);
17453 if (VT == MVT::i64)
17454 return std::make_pair(0U, &X86::GR64_ABCDRegClass);
Eric Christopherd176af82011-06-29 17:23:50 +000017455 break;
Chris Lattner0f65cad2007-04-09 05:49:22 +000017456 case 'r': // GENERAL_REGS
Chris Lattner0f65cad2007-04-09 05:49:22 +000017457 case 'l': // INDEX_REGS
Eric Christopher5427ede2011-07-14 20:13:52 +000017458 if (VT == MVT::i8 || VT == MVT::i1)
Craig Topperc9099502012-04-20 06:31:50 +000017459 return std::make_pair(0U, &X86::GR8RegClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000017460 if (VT == MVT::i16)
Craig Topperc9099502012-04-20 06:31:50 +000017461 return std::make_pair(0U, &X86::GR16RegClass);
Eric Christopher2bbecd82011-05-19 21:33:47 +000017462 if (VT == MVT::i32 || VT == MVT::f32 || !Subtarget->is64Bit())
Craig Topperc9099502012-04-20 06:31:50 +000017463 return std::make_pair(0U, &X86::GR32RegClass);
17464 return std::make_pair(0U, &X86::GR64RegClass);
Dale Johannesen5f3663e2009-10-07 22:47:20 +000017465 case 'R': // LEGACY_REGS
Eric Christopher5427ede2011-07-14 20:13:52 +000017466 if (VT == MVT::i8 || VT == MVT::i1)
Craig Topperc9099502012-04-20 06:31:50 +000017467 return std::make_pair(0U, &X86::GR8_NOREXRegClass);
Dale Johannesen5f3663e2009-10-07 22:47:20 +000017468 if (VT == MVT::i16)
Craig Topperc9099502012-04-20 06:31:50 +000017469 return std::make_pair(0U, &X86::GR16_NOREXRegClass);
Dale Johannesen5f3663e2009-10-07 22:47:20 +000017470 if (VT == MVT::i32 || !Subtarget->is64Bit())
Craig Topperc9099502012-04-20 06:31:50 +000017471 return std::make_pair(0U, &X86::GR32_NOREXRegClass);
17472 return std::make_pair(0U, &X86::GR64_NOREXRegClass);
Chris Lattnerfce84ac2008-03-11 19:06:29 +000017473 case 'f': // FP Stack registers.
17474 // If SSE is enabled for this VT, use f80 to ensure the isel moves the
17475 // value to the correct fpstack register class.
Owen Anderson825b72b2009-08-11 20:47:22 +000017476 if (VT == MVT::f32 && !isScalarFPTypeInSSEReg(VT))
Craig Topperc9099502012-04-20 06:31:50 +000017477 return std::make_pair(0U, &X86::RFP32RegClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000017478 if (VT == MVT::f64 && !isScalarFPTypeInSSEReg(VT))
Craig Topperc9099502012-04-20 06:31:50 +000017479 return std::make_pair(0U, &X86::RFP64RegClass);
17480 return std::make_pair(0U, &X86::RFP80RegClass);
Chris Lattner6c284d72007-04-12 04:14:49 +000017481 case 'y': // MMX_REGS if MMX allowed.
17482 if (!Subtarget->hasMMX()) break;
Craig Topperc9099502012-04-20 06:31:50 +000017483 return std::make_pair(0U, &X86::VR64RegClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +000017484 case 'Y': // SSE_REGS if SSE2 allowed
Craig Topper1accb7e2012-01-10 06:54:16 +000017485 if (!Subtarget->hasSSE2()) break;
Chris Lattner0f65cad2007-04-09 05:49:22 +000017486 // FALL THROUGH.
Eric Christopher55487552012-01-07 01:02:09 +000017487 case 'x': // SSE_REGS if SSE1 allowed or AVX_REGS if AVX allowed
Craig Topper1accb7e2012-01-10 06:54:16 +000017488 if (!Subtarget->hasSSE1()) break;
Duncan Sands83ec4b62008-06-06 12:08:01 +000017489
Owen Anderson825b72b2009-08-11 20:47:22 +000017490 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner0f65cad2007-04-09 05:49:22 +000017491 default: break;
17492 // Scalar SSE types.
Owen Anderson825b72b2009-08-11 20:47:22 +000017493 case MVT::f32:
17494 case MVT::i32:
Craig Topperc9099502012-04-20 06:31:50 +000017495 return std::make_pair(0U, &X86::FR32RegClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000017496 case MVT::f64:
17497 case MVT::i64:
Craig Topperc9099502012-04-20 06:31:50 +000017498 return std::make_pair(0U, &X86::FR64RegClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +000017499 // Vector types.
Owen Anderson825b72b2009-08-11 20:47:22 +000017500 case MVT::v16i8:
17501 case MVT::v8i16:
17502 case MVT::v4i32:
17503 case MVT::v2i64:
17504 case MVT::v4f32:
17505 case MVT::v2f64:
Craig Topperc9099502012-04-20 06:31:50 +000017506 return std::make_pair(0U, &X86::VR128RegClass);
Eric Christopher55487552012-01-07 01:02:09 +000017507 // AVX types.
17508 case MVT::v32i8:
17509 case MVT::v16i16:
17510 case MVT::v8i32:
17511 case MVT::v4i64:
17512 case MVT::v8f32:
17513 case MVT::v4f64:
Craig Topperc9099502012-04-20 06:31:50 +000017514 return std::make_pair(0U, &X86::VR256RegClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +000017515 }
Chris Lattnerad043e82007-04-09 05:11:28 +000017516 break;
17517 }
17518 }
Scott Michelfdc40a02009-02-17 22:15:04 +000017519
Chris Lattnerf76d1802006-07-31 23:26:50 +000017520 // Use the default implementation in TargetLowering to convert the register
17521 // constraint into a member of a register class.
17522 std::pair<unsigned, const TargetRegisterClass*> Res;
17523 Res = TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
Chris Lattner1a60aa72006-10-31 19:42:44 +000017524
17525 // Not found as a standard register?
17526 if (Res.second == 0) {
Chris Lattner56d77c72009-09-13 22:41:48 +000017527 // Map st(0) -> st(7) -> ST0
17528 if (Constraint.size() == 7 && Constraint[0] == '{' &&
17529 tolower(Constraint[1]) == 's' &&
17530 tolower(Constraint[2]) == 't' &&
17531 Constraint[3] == '(' &&
17532 (Constraint[4] >= '0' && Constraint[4] <= '7') &&
17533 Constraint[5] == ')' &&
17534 Constraint[6] == '}') {
Daniel Dunbara279bc32009-09-20 02:20:51 +000017535
Chris Lattner56d77c72009-09-13 22:41:48 +000017536 Res.first = X86::ST0+Constraint[4]-'0';
Craig Topperc9099502012-04-20 06:31:50 +000017537 Res.second = &X86::RFP80RegClass;
Chris Lattner56d77c72009-09-13 22:41:48 +000017538 return Res;
17539 }
Daniel Dunbara279bc32009-09-20 02:20:51 +000017540
Chris Lattner56d77c72009-09-13 22:41:48 +000017541 // GCC allows "st(0)" to be called just plain "st".
Benjamin Kramer05872ea2009-11-12 20:36:59 +000017542 if (StringRef("{st}").equals_lower(Constraint)) {
Chris Lattner1a60aa72006-10-31 19:42:44 +000017543 Res.first = X86::ST0;
Craig Topperc9099502012-04-20 06:31:50 +000017544 Res.second = &X86::RFP80RegClass;
Chris Lattner56d77c72009-09-13 22:41:48 +000017545 return Res;
Chris Lattner1a60aa72006-10-31 19:42:44 +000017546 }
Chris Lattner56d77c72009-09-13 22:41:48 +000017547
17548 // flags -> EFLAGS
Benjamin Kramer05872ea2009-11-12 20:36:59 +000017549 if (StringRef("{flags}").equals_lower(Constraint)) {
Chris Lattner56d77c72009-09-13 22:41:48 +000017550 Res.first = X86::EFLAGS;
Craig Topperc9099502012-04-20 06:31:50 +000017551 Res.second = &X86::CCRRegClass;
Chris Lattner56d77c72009-09-13 22:41:48 +000017552 return Res;
17553 }
Daniel Dunbara279bc32009-09-20 02:20:51 +000017554
Dale Johannesen330169f2008-11-13 21:52:36 +000017555 // 'A' means EAX + EDX.
17556 if (Constraint == "A") {
17557 Res.first = X86::EAX;
Craig Topperc9099502012-04-20 06:31:50 +000017558 Res.second = &X86::GR32_ADRegClass;
Chris Lattner56d77c72009-09-13 22:41:48 +000017559 return Res;
Dale Johannesen330169f2008-11-13 21:52:36 +000017560 }
Chris Lattner1a60aa72006-10-31 19:42:44 +000017561 return Res;
17562 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000017563
Chris Lattnerf76d1802006-07-31 23:26:50 +000017564 // Otherwise, check to see if this is a register class of the wrong value
17565 // type. For example, we want to map "{ax},i32" -> {eax}, we don't want it to
17566 // turn into {ax},{dx}.
17567 if (Res.second->hasType(VT))
17568 return Res; // Correct type already, nothing to do.
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000017569
Chris Lattnerf76d1802006-07-31 23:26:50 +000017570 // All of the single-register GCC register classes map their values onto
17571 // 16-bit register pieces "ax","dx","cx","bx","si","di","bp","sp". If we
17572 // really want an 8-bit or 32-bit register, map to the appropriate register
17573 // class and return the appropriate register.
Craig Topperc9099502012-04-20 06:31:50 +000017574 if (Res.second == &X86::GR16RegClass) {
Owen Anderson825b72b2009-08-11 20:47:22 +000017575 if (VT == MVT::i8) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000017576 unsigned DestReg = 0;
17577 switch (Res.first) {
17578 default: break;
17579 case X86::AX: DestReg = X86::AL; break;
17580 case X86::DX: DestReg = X86::DL; break;
17581 case X86::CX: DestReg = X86::CL; break;
17582 case X86::BX: DestReg = X86::BL; break;
17583 }
17584 if (DestReg) {
17585 Res.first = DestReg;
Craig Topperc9099502012-04-20 06:31:50 +000017586 Res.second = &X86::GR8RegClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000017587 }
Owen Anderson825b72b2009-08-11 20:47:22 +000017588 } else if (VT == MVT::i32) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000017589 unsigned DestReg = 0;
17590 switch (Res.first) {
17591 default: break;
17592 case X86::AX: DestReg = X86::EAX; break;
17593 case X86::DX: DestReg = X86::EDX; break;
17594 case X86::CX: DestReg = X86::ECX; break;
17595 case X86::BX: DestReg = X86::EBX; break;
17596 case X86::SI: DestReg = X86::ESI; break;
17597 case X86::DI: DestReg = X86::EDI; break;
17598 case X86::BP: DestReg = X86::EBP; break;
17599 case X86::SP: DestReg = X86::ESP; break;
17600 }
17601 if (DestReg) {
17602 Res.first = DestReg;
Craig Topperc9099502012-04-20 06:31:50 +000017603 Res.second = &X86::GR32RegClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000017604 }
Owen Anderson825b72b2009-08-11 20:47:22 +000017605 } else if (VT == MVT::i64) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000017606 unsigned DestReg = 0;
17607 switch (Res.first) {
17608 default: break;
17609 case X86::AX: DestReg = X86::RAX; break;
17610 case X86::DX: DestReg = X86::RDX; break;
17611 case X86::CX: DestReg = X86::RCX; break;
17612 case X86::BX: DestReg = X86::RBX; break;
17613 case X86::SI: DestReg = X86::RSI; break;
17614 case X86::DI: DestReg = X86::RDI; break;
17615 case X86::BP: DestReg = X86::RBP; break;
17616 case X86::SP: DestReg = X86::RSP; break;
17617 }
17618 if (DestReg) {
17619 Res.first = DestReg;
Craig Topperc9099502012-04-20 06:31:50 +000017620 Res.second = &X86::GR64RegClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000017621 }
Chris Lattnerf76d1802006-07-31 23:26:50 +000017622 }
Craig Topperc9099502012-04-20 06:31:50 +000017623 } else if (Res.second == &X86::FR32RegClass ||
17624 Res.second == &X86::FR64RegClass ||
17625 Res.second == &X86::VR128RegClass) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000017626 // Handle references to XMM physical registers that got mapped into the
17627 // wrong class. This can happen with constraints like {xmm0} where the
17628 // target independent register mapper will just pick the first match it can
17629 // find, ignoring the required type.
Eli Friedman52d418d2012-06-25 23:42:33 +000017630
17631 if (VT == MVT::f32 || VT == MVT::i32)
Craig Topperc9099502012-04-20 06:31:50 +000017632 Res.second = &X86::FR32RegClass;
Eli Friedman52d418d2012-06-25 23:42:33 +000017633 else if (VT == MVT::f64 || VT == MVT::i64)
Craig Topperc9099502012-04-20 06:31:50 +000017634 Res.second = &X86::FR64RegClass;
17635 else if (X86::VR128RegClass.hasType(VT))
17636 Res.second = &X86::VR128RegClass;
Eli Friedman52d418d2012-06-25 23:42:33 +000017637 else if (X86::VR256RegClass.hasType(VT))
17638 Res.second = &X86::VR256RegClass;
Chris Lattnerf76d1802006-07-31 23:26:50 +000017639 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000017640
Chris Lattnerf76d1802006-07-31 23:26:50 +000017641 return Res;
17642}
Nadav Rotemb4b04c32012-11-03 00:39:56 +000017643
Nadav Roteme6237022012-11-05 19:32:46 +000017644//===----------------------------------------------------------------------===//
17645//
17646// X86 cost model.
17647//
17648//===----------------------------------------------------------------------===//
17649
17650struct X86CostTblEntry {
17651 int ISD;
17652 MVT Type;
17653 unsigned Cost;
17654};
17655
Nadav Rotemd8eae8b2012-11-06 23:36:00 +000017656static int
17657FindInTable(const X86CostTblEntry *Tbl, unsigned len, int ISD, MVT Ty) {
Nadav Rotem7ae3bcc2012-11-05 23:48:20 +000017658 for (unsigned int i = 0; i < len; ++i)
17659 if (Tbl[i].ISD == ISD && Tbl[i].Type == Ty)
17660 return i;
17661
17662 // Could not find an entry.
17663 return -1;
17664}
17665
Nadav Rotemb0428682012-11-06 19:33:53 +000017666struct X86TypeConversionCostTblEntry {
17667 int ISD;
17668 MVT Dst;
17669 MVT Src;
17670 unsigned Cost;
17671};
17672
Nadav Rotemd8eae8b2012-11-06 23:36:00 +000017673static int
17674FindInConvertTable(const X86TypeConversionCostTblEntry *Tbl, unsigned len,
17675 int ISD, MVT Dst, MVT Src) {
Nadav Rotemb0428682012-11-06 19:33:53 +000017676 for (unsigned int i = 0; i < len; ++i)
17677 if (Tbl[i].ISD == ISD && Tbl[i].Src == Src && Tbl[i].Dst == Dst)
17678 return i;
17679
17680 // Could not find an entry.
17681 return -1;
17682}
17683
Shuxin Yang84fca612012-11-29 19:38:54 +000017684ScalarTargetTransformInfo::PopcntHwSupport
17685X86ScalarTargetTransformImpl::getPopcntHwSupport(unsigned TyWidth) const {
17686 assert(isPowerOf2_32(TyWidth) && "Ty width must be power of 2");
17687 const X86Subtarget &ST = TLI->getTargetMachine().getSubtarget<X86Subtarget>();
17688
17689 // TODO: Currently the __builtin_popcount() implementation using SSE3
17690 // instructions is inefficient. Once the problem is fixed, we should
17691 // call ST.hasSSE3() instead of ST.hasSSE4().
17692 return ST.hasSSE41() ? Fast : None;
17693}
17694
Nadav Rotemb4b04c32012-11-03 00:39:56 +000017695unsigned
17696X86VectorTargetTransformInfo::getArithmeticInstrCost(unsigned Opcode,
17697 Type *Ty) const {
Nadav Roteme6237022012-11-05 19:32:46 +000017698 // Legalize the type.
Nadav Rotem887c1fe2012-11-05 23:57:45 +000017699 std::pair<unsigned, MVT> LT = getTypeLegalizationCost(Ty);
Nadav Roteme6237022012-11-05 19:32:46 +000017700
17701 int ISD = InstructionOpcodeToISD(Opcode);
17702 assert(ISD && "Invalid opcode");
17703
Nadav Rotemb0428682012-11-06 19:33:53 +000017704 const X86Subtarget &ST = TLI->getTargetMachine().getSubtarget<X86Subtarget>();
Nadav Rotemb4b04c32012-11-03 00:39:56 +000017705
Nadav Roteme6237022012-11-05 19:32:46 +000017706 static const X86CostTblEntry AVX1CostTable[] = {
17707 // We don't have to scalarize unsupported ops. We can issue two half-sized
17708 // operations and we only need to extract the upper YMM half.
17709 // Two ops + 1 extract + 1 insert = 4.
17710 { ISD::MUL, MVT::v8i32, 4 },
17711 { ISD::SUB, MVT::v8i32, 4 },
17712 { ISD::ADD, MVT::v8i32, 4 },
17713 { ISD::MUL, MVT::v4i64, 4 },
17714 { ISD::SUB, MVT::v4i64, 4 },
17715 { ISD::ADD, MVT::v4i64, 4 },
17716 };
Nadav Rotemb4b04c32012-11-03 00:39:56 +000017717
Nadav Roteme6237022012-11-05 19:32:46 +000017718 // Look for AVX1 lowering tricks.
Nadav Rotem7ae3bcc2012-11-05 23:48:20 +000017719 if (ST.hasAVX()) {
17720 int Idx = FindInTable(AVX1CostTable, array_lengthof(AVX1CostTable), ISD,
17721 LT.second);
17722 if (Idx != -1)
17723 return LT.first * AVX1CostTable[Idx].Cost;
17724 }
Nadav Roteme6237022012-11-05 19:32:46 +000017725 // Fallback to the default implementation.
Nadav Rotemb4b04c32012-11-03 00:39:56 +000017726 return VectorTargetTransformImpl::getArithmeticInstrCost(Opcode, Ty);
17727}
17728
17729unsigned
17730X86VectorTargetTransformInfo::getVectorInstrCost(unsigned Opcode, Type *Val,
Richard Smithe010eb32012-11-05 22:01:44 +000017731 unsigned Index) const {
Nadav Rotema4ab5292012-11-05 21:12:13 +000017732 assert(Val->isVectorTy() && "This must be a vector type");
17733
Nadav Rotem7ae3bcc2012-11-05 23:48:20 +000017734 if (Index != -1U) {
Nadav Rotema4ab5292012-11-05 21:12:13 +000017735 // Legalize the type.
Nadav Rotem887c1fe2012-11-05 23:57:45 +000017736 std::pair<unsigned, MVT> LT = getTypeLegalizationCost(Val);
Nadav Rotema4ab5292012-11-05 21:12:13 +000017737
17738 // This type is legalized to a scalar type.
17739 if (!LT.second.isVector())
17740 return 0;
17741
17742 // The type may be split. Normalize the index to the new type.
17743 unsigned Width = LT.second.getVectorNumElements();
17744 Index = Index % Width;
17745
17746 // Floating point scalars are already located in index #0.
17747 if (Val->getScalarType()->isFloatingPointTy() && Index == 0)
17748 return 0;
17749 }
17750
Nadav Rotemb4b04c32012-11-03 00:39:56 +000017751 return VectorTargetTransformImpl::getVectorInstrCost(Opcode, Val, Index);
17752}
17753
Nadav Rotem7ae3bcc2012-11-05 23:48:20 +000017754unsigned X86VectorTargetTransformInfo::getCmpSelInstrCost(unsigned Opcode,
17755 Type *ValTy,
17756 Type *CondTy) const {
17757 // Legalize the type.
Nadav Rotem887c1fe2012-11-05 23:57:45 +000017758 std::pair<unsigned, MVT> LT = getTypeLegalizationCost(ValTy);
Nadav Rotem7ae3bcc2012-11-05 23:48:20 +000017759
17760 MVT MTy = LT.second;
17761
17762 int ISD = InstructionOpcodeToISD(Opcode);
17763 assert(ISD && "Invalid opcode");
17764
17765 const X86Subtarget &ST =
17766 TLI->getTargetMachine().getSubtarget<X86Subtarget>();
17767
17768 static const X86CostTblEntry SSE42CostTbl[] = {
17769 { ISD::SETCC, MVT::v2f64, 1 },
17770 { ISD::SETCC, MVT::v4f32, 1 },
17771 { ISD::SETCC, MVT::v2i64, 1 },
17772 { ISD::SETCC, MVT::v4i32, 1 },
17773 { ISD::SETCC, MVT::v8i16, 1 },
17774 { ISD::SETCC, MVT::v16i8, 1 },
17775 };
17776
17777 static const X86CostTblEntry AVX1CostTbl[] = {
17778 { ISD::SETCC, MVT::v4f64, 1 },
17779 { ISD::SETCC, MVT::v8f32, 1 },
17780 // AVX1 does not support 8-wide integer compare.
17781 { ISD::SETCC, MVT::v4i64, 4 },
17782 { ISD::SETCC, MVT::v8i32, 4 },
17783 { ISD::SETCC, MVT::v16i16, 4 },
17784 { ISD::SETCC, MVT::v32i8, 4 },
17785 };
17786
17787 static const X86CostTblEntry AVX2CostTbl[] = {
17788 { ISD::SETCC, MVT::v4i64, 1 },
17789 { ISD::SETCC, MVT::v8i32, 1 },
17790 { ISD::SETCC, MVT::v16i16, 1 },
17791 { ISD::SETCC, MVT::v32i8, 1 },
17792 };
17793
17794 if (ST.hasSSE42()) {
17795 int Idx = FindInTable(SSE42CostTbl, array_lengthof(SSE42CostTbl), ISD, MTy);
17796 if (Idx != -1)
17797 return LT.first * SSE42CostTbl[Idx].Cost;
17798 }
17799
17800 if (ST.hasAVX()) {
17801 int Idx = FindInTable(AVX1CostTbl, array_lengthof(AVX1CostTbl), ISD, MTy);
17802 if (Idx != -1)
17803 return LT.first * AVX1CostTbl[Idx].Cost;
17804 }
17805
17806 if (ST.hasAVX2()) {
17807 int Idx = FindInTable(AVX2CostTbl, array_lengthof(AVX2CostTbl), ISD, MTy);
17808 if (Idx != -1)
17809 return LT.first * AVX2CostTbl[Idx].Cost;
17810 }
17811
17812 return VectorTargetTransformImpl::getCmpSelInstrCost(Opcode, ValTy, CondTy);
17813}
17814
Nadav Rotemb0428682012-11-06 19:33:53 +000017815unsigned X86VectorTargetTransformInfo::getCastInstrCost(unsigned Opcode,
17816 Type *Dst,
17817 Type *Src) const {
17818 int ISD = InstructionOpcodeToISD(Opcode);
17819 assert(ISD && "Invalid opcode");
Nadav Rotem7ae3bcc2012-11-05 23:48:20 +000017820
Nadav Rotemb0428682012-11-06 19:33:53 +000017821 EVT SrcTy = TLI->getValueType(Src);
17822 EVT DstTy = TLI->getValueType(Dst);
17823
17824 if (!SrcTy.isSimple() || !DstTy.isSimple())
17825 return VectorTargetTransformImpl::getCastInstrCost(Opcode, Dst, Src);
17826
17827 const X86Subtarget &ST = TLI->getTargetMachine().getSubtarget<X86Subtarget>();
17828
17829 static const X86TypeConversionCostTblEntry AVXConversionTbl[] = {
17830 { ISD::SIGN_EXTEND, MVT::v8i32, MVT::v8i16, 1 },
17831 { ISD::ZERO_EXTEND, MVT::v8i32, MVT::v8i16, 1 },
17832 { ISD::SIGN_EXTEND, MVT::v4i64, MVT::v4i32, 1 },
17833 { ISD::ZERO_EXTEND, MVT::v4i64, MVT::v4i32, 1 },
17834 { ISD::TRUNCATE, MVT::v4i32, MVT::v4i64, 1 },
17835 { ISD::TRUNCATE, MVT::v8i16, MVT::v8i32, 1 },
17836 { ISD::SINT_TO_FP, MVT::v8f32, MVT::v8i8, 1 },
17837 { ISD::SINT_TO_FP, MVT::v4f32, MVT::v4i8, 1 },
17838 { ISD::UINT_TO_FP, MVT::v8f32, MVT::v8i8, 1 },
17839 { ISD::UINT_TO_FP, MVT::v4f32, MVT::v4i8, 1 },
Nadav Rotemb14a5f52012-11-09 07:02:24 +000017840 { ISD::FP_TO_SINT, MVT::v8i8, MVT::v8f32, 1 },
17841 { ISD::FP_TO_SINT, MVT::v4i8, MVT::v4f32, 1 },
Nadav Rotemb0428682012-11-06 19:33:53 +000017842 { ISD::ZERO_EXTEND, MVT::v8i32, MVT::v8i1, 6 },
17843 { ISD::SIGN_EXTEND, MVT::v8i32, MVT::v8i1, 9 },
Nadav Rotema6fb97a2012-11-06 21:17:17 +000017844 { ISD::TRUNCATE, MVT::v8i32, MVT::v8i64, 3 },
Nadav Rotemb0428682012-11-06 19:33:53 +000017845 };
17846
17847 if (ST.hasAVX()) {
17848 int Idx = FindInConvertTable(AVXConversionTbl,
17849 array_lengthof(AVXConversionTbl),
17850 ISD, DstTy.getSimpleVT(), SrcTy.getSimpleVT());
17851 if (Idx != -1)
17852 return AVXConversionTbl[Idx].Cost;
17853 }
17854
17855 return VectorTargetTransformImpl::getCastInstrCost(Opcode, Dst, Src);
17856}
Nadav Rotem7ae3bcc2012-11-05 23:48:20 +000017857